Skip to contentSkip to conversation

s390x: stsi: Define vm_is_kvm to be used in different tests

PATCH In review Pierre Morel 3 participants 4 messages Feb 16, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover s390x: stsi: Define vm_is_kvm to be used in different tests

Hi all,

In this new version we suppress vm_is_vm to use directly stsi_get_fc().

Regards,
Pierre

Pierre Morel (1):
s390x: stsi: Define vm_is_kvm to be used in different tests

 lib/s390x/stsi.h | 32 ++++++++++++++++++++++++++++++
 lib/s390x/vm.c   | 51 ++++++++++++++++++++++++++++++++++++++++++++++--
 lib/s390x/vm.h   |  2 ++
 s390x/stsi.c     | 23 ++--------------------
 4 files changed, 85 insertions(+), 23 deletions(-)
 create mode 100644 lib/s390x/stsi.h
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/1 s390x: stsi: Define vm_is_kvm to be used in different tests

Several tests are in need of a way to check on which hypervisor and virtualization level they are running on to be able to fence certain tests. This patch adds functions that return true if a vm is running under KVM, LPAR or generally as a level 2 guest.

To check if we're running under KVM we use the STSI 3.2.2 instruction, let's define it's response structure in a central header.

---
 lib/s390x/stsi.h | 32 ++++++++++++++++++++++++++++++
 lib/s390x/vm.c   | 51 ++++++++++++++++++++++++++++++++++++++++++++++--
 lib/s390x/vm.h   |  2 ++
 s390x/stsi.c     | 23 ++--------------------
 4 files changed, 85 insertions(+), 23 deletions(-)
 create mode 100644 lib/s390x/stsi.h
lib/s390x/stsi.h new file +32 -0
new file mode 100644
index 00000000..bebc492d
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Structures used to Store System Information
+ *
+ * Copyright IBM Corp. 2022
+ */
+
+#ifndef _S390X_STSI_H_
+#define _S390X_STSI_H_
+
+struct sysinfo_3_2_2 {
+	uint8_t reserved[31];
+	uint8_t count;
+	struct {
+		uint8_t reserved2[4];
+		uint16_t total_cpus;
+		uint16_t conf_cpus;
+		uint16_t standby_cpus;
+		uint16_t reserved_cpus;
+		uint8_t name[8];
+		uint32_t caf;
+		uint8_t cpi[16];
+		uint8_t reserved5[3];
+		uint8_t ext_name_encoding;
+		uint32_t reserved3;
+		uint8_t uuid[16];
+	} vm[8];
+	uint8_t reserved4[1504];
+	uint8_t ext_names[8][256];
+};
+
+#endif  /* _S390X_STSI_H_ */
lib/s390x/vm.c +49 -2

67 lines not on this page · raw message

lib/s390x/vm.h +2 -0
index 7abba0cc..4456b48c 100644
@@ -9,5 +9,7 @@
 #define _S390X_VM_H_
 
 bool vm_is_tcg(void);
+bool vm_is_kvm(void);
+bool vm_is_lpar(void);
 
 #endif  /* _S390X_VM_H_ */
s390x/stsi.c +2 -21
index 391f8849..dccc53e7 100644
@@ -13,27 +13,8 @@
 #include <asm/asm-offsets.h>
 #include <asm/interrupt.h>
 #include <smp.h>
+#include <stsi.h>
 
-struct stsi_322 {
-	uint8_t reserved[31];
-	uint8_t count;
-	struct {
-		uint8_t reserved2[4];
-		uint16_t total_cpus;
-		uint16_t conf_cpus;
-		uint16_t standby_cpus;
-		uint16_t reserved_cpus;
-		uint8_t name[8];
-		uint32_t caf;
-		uint8_t cpi[16];
-		uint8_t reserved5[3];
-		uint8_t ext_name_encoding;
-		uint32_t reserved3;
-		uint8_t uuid[16];
-	} vm[8];
-	uint8_t reserved4[1504];
-	uint8_t ext_names[8][256];
-};
 static uint8_t pagebuf[PAGE_SIZE * 2] __attribute__((aligned(PAGE_SIZE * 2)));
 
 static void test_specs(void)
@@ -91,7 +72,7 @@ static void test_3_2_2(void)
 	/* EBCDIC for "KVM/" */
 	const uint8_t cpi_kvm[] = { 0xd2, 0xe5, 0xd4, 0x61 };
 	const char vm_name_ext[] = "kvm-unit-test";
-	struct stsi_322 *data = (void *)pagebuf;
+	struct sysinfo_3_2_2 *data = (void *)pagebuf;
 
 	report_prefix_push("3.2.2");
 
Signature · 1 line
2.27.0
#2 lore raw
Quoted 10 lines · On Wed, 16 Feb 2022 13:34:02 +0100 Pierre Morel <[email protected]> wrote:

Several tests are in need of a way to check on which hypervisor and virtualization level they are running on to be able to fence certain tests. This patch adds functions that return true if a vm is running under KVM, LPAR or generally as a level 2 guest.

To check if we're running under KVM we use the STSI 3.2.2 instruction, let's define it's response structure in a central header.

I will queue this, thanks

Quoted 174 lines
---
 lib/s390x/stsi.h | 32 ++++++++++++++++++++++++++++++
 lib/s390x/vm.c   | 51 ++++++++++++++++++++++++++++++++++++++++++++++--
 lib/s390x/vm.h   |  2 ++
 s390x/stsi.c     | 23 ++--------------------
 4 files changed, 85 insertions(+), 23 deletions(-)
 create mode 100644 lib/s390x/stsi.h
lib/s390x/stsi.h new file +32 -0
new file mode 100644
index 00000000..bebc492d
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Structures used to Store System Information
+ *
+ * Copyright IBM Corp. 2022
+ */
+
+#ifndef _S390X_STSI_H_
+#define _S390X_STSI_H_
+
+struct sysinfo_3_2_2 {
+	uint8_t reserved[31];
+	uint8_t count;
+	struct {
+		uint8_t reserved2[4];
+		uint16_t total_cpus;
+		uint16_t conf_cpus;
+		uint16_t standby_cpus;
+		uint16_t reserved_cpus;
+		uint8_t name[8];
+		uint32_t caf;
+		uint8_t cpi[16];
+		uint8_t reserved5[3];
+		uint8_t ext_name_encoding;
+		uint32_t reserved3;
+		uint8_t uuid[16];
+	} vm[8];
+	uint8_t reserved4[1504];
+	uint8_t ext_names[8][256];
+};
+
+#endif  /* _S390X_STSI_H_ */
lib/s390x/vm.c +49 -2
index a5b92863..33fb1c45 100644
@@ -12,6 +12,7 @@
 #include <alloc_page.h>
 #include <asm/arch_def.h>
 #include "vm.h"
+#include "stsi.h"
 
 /**
  * Detect whether we are running with TCG (instead of KVM)
@@ -26,9 +27,13 @@ bool vm_is_tcg(void)
 	if (initialized)
 		return is_tcg;
 
+	if (stsi_get_fc() != 3) {
+		initialized = true;
+		return is_tcg;
+	}
+
 	buf = alloc_page();
-	if (!buf)
-		return false;
+	assert(buf);
 
 	if (stsi(buf, 1, 1, 1))
 		goto out;
@@ -43,3 +48,45 @@ out:
 	free_page(buf);
 	return is_tcg;
 }
+
+/**
+ * Detect whether we are running with KVM
+ */
+bool vm_is_kvm(void)
+{
+	/* EBCDIC for "KVM/" */
+	const uint8_t kvm_ebcdic[] = { 0xd2, 0xe5, 0xd4, 0x61 };
+	static bool initialized;
+	static bool is_kvm;
+	struct sysinfo_3_2_2 *stsi_322;
+
+	if (initialized)
+		return is_kvm;
+
+	if (stsi_get_fc() != 3 || vm_is_tcg()) {
+		initialized = true;
+		return is_kvm;
+	}
+
+	stsi_322 = alloc_page();
+	assert(stsi_322);
+
+	if (stsi(stsi_322, 3, 2, 2))
+		goto out;
+
+	/*
+	 * If the manufacturer string is "KVM/" in EBCDIC, then we
+	 * are on KVM.
+	 */
+	is_kvm = !memcmp(&stsi_322->vm[0].cpi, kvm_ebcdic, sizeof(kvm_ebcdic));
+	initialized = true;
+out:
+	free_page(stsi_322);
+	return is_kvm;
+}
+
+bool vm_is_lpar(void)
+{
+	return stsi_get_fc() == 2;
+}
+
lib/s390x/vm.h +2 -0
index 7abba0cc..4456b48c 100644
@@ -9,5 +9,7 @@
 #define _S390X_VM_H_
 
 bool vm_is_tcg(void);
+bool vm_is_kvm(void);
+bool vm_is_lpar(void);
 
 #endif  /* _S390X_VM_H_ */
s390x/stsi.c +2 -21
index 391f8849..dccc53e7 100644
@@ -13,27 +13,8 @@
 #include <asm/asm-offsets.h>
 #include <asm/interrupt.h>
 #include <smp.h>
+#include <stsi.h>
 
-struct stsi_322 {
-	uint8_t reserved[31];
-	uint8_t count;
-	struct {
-		uint8_t reserved2[4];
-		uint16_t total_cpus;
-		uint16_t conf_cpus;
-		uint16_t standby_cpus;
-		uint16_t reserved_cpus;
-		uint8_t name[8];
-		uint32_t caf;
-		uint8_t cpi[16];
-		uint8_t reserved5[3];
-		uint8_t ext_name_encoding;
-		uint32_t reserved3;
-		uint8_t uuid[16];
-	} vm[8];
-	uint8_t reserved4[1504];
-	uint8_t ext_names[8][256];
-};
 static uint8_t pagebuf[PAGE_SIZE * 2] __attribute__((aligned(PAGE_SIZE * 2)));
 
 static void test_specs(void)
@@ -91,7 +72,7 @@ static void test_3_2_2(void)
 	/* EBCDIC for "KVM/" */
 	const uint8_t cpi_kvm[] = { 0xd2, 0xe5, 0xd4, 0x61 };
 	const char vm_name_ext[] = "kvm-unit-test";
-	struct stsi_322 *data = (void *)pagebuf;
+	struct sysinfo_3_2_2 *data = (void *)pagebuf;
 
 	report_prefix_push("3.2.2");
 
Reviewed-by on v1 1/1
#3 lore raw
In reply to Pierre Morel
Quoted 10 lines · Pierre Morel

Several tests are in need of a way to check on which hypervisor and virtualization level they are running on to be able to fence certain tests. This patch adds functions that return true if a vm is running under KVM, LPAR or generally as a level 2 guest.

To check if we're running under KVM we use the STSI 3.2.2 instruction, let's define it's response structure in a central header.

Thanks for being so patient with us Pierre :-)

Quoted 174 lines
---
  lib/s390x/stsi.h | 32 ++++++++++++++++++++++++++++++
  lib/s390x/vm.c   | 51 ++++++++++++++++++++++++++++++++++++++++++++++--
  lib/s390x/vm.h   |  2 ++
  s390x/stsi.c     | 23 ++--------------------
  4 files changed, 85 insertions(+), 23 deletions(-)

create mode 100644 lib/s390x/stsi.h

lib/s390x/stsi.h new file +32 -0
new file mode 100644
index 00000000..bebc492d
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Structures used to Store System Information
+ *
+ * Copyright IBM Corp. 2022
+ */
+
+#ifndef _S390X_STSI_H_
+#define _S390X_STSI_H_
+
+struct sysinfo_3_2_2 {
+	uint8_t reserved[31];
+	uint8_t count;
+	struct {
+		uint8_t reserved2[4];
+		uint16_t total_cpus;
+		uint16_t conf_cpus;
+		uint16_t standby_cpus;
+		uint16_t reserved_cpus;
+		uint8_t name[8];
+		uint32_t caf;
+		uint8_t cpi[16];
+		uint8_t reserved5[3];
+		uint8_t ext_name_encoding;
+		uint32_t reserved3;
+		uint8_t uuid[16];
+	} vm[8];
+	uint8_t reserved4[1504];
+	uint8_t ext_names[8][256];
+};
+
+#endif  /* _S390X_STSI_H_ */
lib/s390x/vm.c +49 -2
index a5b92863..33fb1c45 100644
@@ -12,6 +12,7 @@
  #include <alloc_page.h>
  #include <asm/arch_def.h>
  #include "vm.h"
+#include "stsi.h"
 
  /**
   * Detect whether we are running with TCG (instead of KVM)
@@ -26,9 +27,13 @@ bool vm_is_tcg(void)
  	if (initialized)
  		return is_tcg;
 
+	if (stsi_get_fc() != 3) {
+		initialized = true;
+		return is_tcg;
+	}
+
  	buf = alloc_page();
-	if (!buf)
-		return false;
+	assert(buf);
 
  	if (stsi(buf, 1, 1, 1))
  		goto out;
@@ -43,3 +48,45 @@ out:
  	free_page(buf);
  	return is_tcg;
  }
+
+/**
+ * Detect whether we are running with KVM
+ */
+bool vm_is_kvm(void)
+{
+	/* EBCDIC for "KVM/" */
+	const uint8_t kvm_ebcdic[] = { 0xd2, 0xe5, 0xd4, 0x61 };
+	static bool initialized;
+	static bool is_kvm;
+	struct sysinfo_3_2_2 *stsi_322;
+
+	if (initialized)
+		return is_kvm;
+
+	if (stsi_get_fc() != 3 || vm_is_tcg()) {
+		initialized = true;
+		return is_kvm;
+	}
+
+	stsi_322 = alloc_page();
+	assert(stsi_322);
+
+	if (stsi(stsi_322, 3, 2, 2))
+		goto out;
+
+	/*
+	 * If the manufacturer string is "KVM/" in EBCDIC, then we
+	 * are on KVM.
+	 */
+	is_kvm = !memcmp(&stsi_322->vm[0].cpi, kvm_ebcdic, sizeof(kvm_ebcdic));
+	initialized = true;
+out:
+	free_page(stsi_322);
+	return is_kvm;
+}
+
+bool vm_is_lpar(void)
+{
+	return stsi_get_fc() == 2;
+}
+
lib/s390x/vm.h +2 -0
index 7abba0cc..4456b48c 100644
@@ -9,5 +9,7 @@
  #define _S390X_VM_H_
 
  bool vm_is_tcg(void);
+bool vm_is_kvm(void);
+bool vm_is_lpar(void);
 
  #endif  /* _S390X_VM_H_ */
s390x/stsi.c +2 -21
index 391f8849..dccc53e7 100644
@@ -13,27 +13,8 @@
  #include <asm/asm-offsets.h>
  #include <asm/interrupt.h>
  #include <smp.h>
+#include <stsi.h>
 
-struct stsi_322 {
-	uint8_t reserved[31];
-	uint8_t count;
-	struct {
-		uint8_t reserved2[4];
-		uint16_t total_cpus;
-		uint16_t conf_cpus;
-		uint16_t standby_cpus;
-		uint16_t reserved_cpus;
-		uint8_t name[8];
-		uint32_t caf;
-		uint8_t cpi[16];
-		uint8_t reserved5[3];
-		uint8_t ext_name_encoding;
-		uint32_t reserved3;
-		uint8_t uuid[16];
-	} vm[8];
-	uint8_t reserved4[1504];
-	uint8_t ext_names[8][256];
-};
  static uint8_t pagebuf[PAGE_SIZE * 2] __attribute__((aligned(PAGE_SIZE * 2)));
 
  static void test_specs(void)
@@ -91,7 +72,7 @@ static void test_3_2_2(void)
  	/* EBCDIC for "KVM/" */
  	const uint8_t cpi_kvm[] = { 0xd2, 0xe5, 0xd4, 0x61 };
  	const char vm_name_ext[] = "kvm-unit-test";
-	struct stsi_322 *data = (void *)pagebuf;
+	struct sysinfo_3_2_2 *data = (void *)pagebuf;
 
  	report_prefix_push("3.2.2");