Skip to contentSkip to conversation

selftests: kvm: introduce cpu_has_svm() check

PATCH In review Vitaly Kuznetsov 3 participants 5 messages May 29, 2020 – Jun 1, 2020

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

PATCH 1/2 selftests: kvm: introduce cpu_has_svm() check

More tests may want to check if the CPU is Intel or AMD in guest code, separate cpu_has_svm() and put it as static inline to svm_util.h.

---
 tools/testing/selftests/kvm/include/x86_64/svm_util.h | 10 ++++++++++
 tools/testing/selftests/kvm/x86_64/state_test.c       |  9 +--------
 2 files changed, 11 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/x86_64/svm_util.h +10 -0
index cd037917fece..b1057773206a 100644
@@ -35,4 +35,14 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r
 void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
 void nested_svm_check_supported(void);
 
+static inline bool cpu_has_svm(void)
+{
+	u32 eax = 0x80000001, ecx;
+
+	asm volatile("cpuid" :
+		     "=a" (eax), "=c" (ecx) : "0" (eax) : "ebx", "edx");
+
+	return ecx & CPUID_SVM;
+}
+
 #endif /* SELFTEST_KVM_SVM_UTILS_H */
tools/testing/selftests/kvm/x86_64/state_test.c +1 -8
index af8b6df6a13e..d43b6f99b66c 100644
@@ -137,20 +137,13 @@ static void vmx_l1_guest_code(struct vmx_pages *vmx_pages)
 	GUEST_ASSERT(vmresume());
 }
 
-static u32 cpuid_ecx(u32 eax)
-{
-	u32 ecx;
-	asm volatile("cpuid" : "=a" (eax), "=c" (ecx) : "0" (eax) : "ebx", "edx");
-	return ecx;
-}
-
 static void __attribute__((__flatten__)) guest_code(void *arg)
 {
 	GUEST_SYNC(1);
 	GUEST_SYNC(2);
 
 	if (arg) {
-		if (cpuid_ecx(0x80000001) & CPUID_SVM)
+		if (cpu_has_svm())
 			svm_l1_guest_code(arg);
 		else
 			vmx_l1_guest_code(arg);
Signature · 1 line
2.25.4
#1 lore raw

PATCH 2/2 selftests: kvm: fix smm test on SVM

KVM_CAP_NESTED_STATE is now supported for AMD too but smm test acts like it is still Intel only.

---
 tools/testing/selftests/kvm/x86_64/smm_test.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/x86_64/smm_test.c +13 -6
index 8230b6bc6b8f..6f8f478b3ceb 100644
@@ -17,6 +17,7 @@
 #include "kvm_util.h"
 
 #include "vmx.h"
+#include "svm_util.h"
 
 #define VCPU_ID	      1
 
@@ -58,7 +59,7 @@ void self_smi(void)
 	      APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI);
 }
 
-void guest_code(struct vmx_pages *vmx_pages)
+void guest_code(void *arg)
 {
 	uint64_t apicbase = rdmsr(MSR_IA32_APICBASE);
 
@@ -72,8 +73,11 @@ void guest_code(struct vmx_pages *vmx_pages)
 
 	sync_with_host(4);
 
-	if (vmx_pages) {
-		GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
+	if (arg) {
+		if (cpu_has_svm())
+			generic_svm_setup(arg, NULL, NULL);
+		else
+			GUEST_ASSERT(prepare_for_vmx_operation(arg));
 
 		sync_with_host(5);
 
@@ -87,7 +91,7 @@ void guest_code(struct vmx_pages *vmx_pages)
 
 int main(int argc, char *argv[])
 {
-	vm_vaddr_t vmx_pages_gva = 0;
+	vm_vaddr_t nested_gva = 0;
 
 	struct kvm_regs regs;
 	struct kvm_vm *vm;
@@ -114,8 +118,11 @@ int main(int argc, char *argv[])
 	vcpu_set_msr(vm, VCPU_ID, MSR_IA32_SMBASE, SMRAM_GPA);
 
 	if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
-		vcpu_alloc_vmx(vm, &vmx_pages_gva);
-		vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
+		if (kvm_get_supported_cpuid_entry(0x80000001)->ecx & CPUID_SVM)
+			vcpu_alloc_svm(vm, &nested_gva);
+		else
+			vcpu_alloc_vmx(vm, &nested_gva);
+		vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
 	} else {
 		pr_info("will skip SMM test with VMX enabled\n");
 		vcpu_args_set(vm, VCPU_ID, 1, 0);
Signature · 1 line
2.25.4
maintainer
#2 lore raw

Re: [PATCH 2/2] selftests: kvm: fix smm test on SVM

Quoted 57 lines · Vitaly Kuznetsov

KVM_CAP_NESTED_STATE is now supported for AMD too but smm test acts like it is still Intel only.

---
 tools/testing/selftests/kvm/x86_64/smm_test.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/x86_64/smm_test.c +8 -4
index 8230b6bc6b8f..6f8f478b3ceb 100644
@@ -17,6 +17,7 @@
 #include "kvm_util.h"
 
 #include "vmx.h"
+#include "svm_util.h"
 
 #define VCPU_ID	      1
 
@@ -58,7 +59,7 @@ void self_smi(void)
 	      APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI);
 }
 
-void guest_code(struct vmx_pages *vmx_pages)
+void guest_code(void *arg)
 {
 	uint64_t apicbase = rdmsr(MSR_IA32_APICBASE);
 
@@ -72,8 +73,11 @@ void guest_code(struct vmx_pages *vmx_pages)
 
 	sync_with_host(4);
 
-	if (vmx_pages) {
-		GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
+	if (arg) {
+		if (cpu_has_svm())
+			generic_svm_setup(arg, NULL, NULL);
+		else
+			GUEST_ASSERT(prepare_for_vmx_operation(arg));
 
 		sync_with_host(5);
 
@@ -87,7 +91,7 @@ void guest_code(struct vmx_pages *vmx_pages)
 
 int main(int argc, char *argv[])
 {
-	vm_vaddr_t vmx_pages_gva = 0;
+	vm_vaddr_t nested_gva = 0;
 
 	struct kvm_regs regs;
 	struct kvm_vm *vm;
@@ -114,8 +118,11 @@ int main(int argc, char *argv[])
 	vcpu_set_msr(vm, VCPU_ID, MSR_IA32_SMBASE, SMRAM_GPA);
 
 	if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {

tools/testing/selftests/kvm/x86_64/smm_test.c int main(int argc, char *argv[]) · line 128

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/smm_test.c +5 -2
-		vcpu_alloc_vmx(vm, &vmx_pages_gva);
-		vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
+		if (kvm_get_supported_cpuid_entry(0x80000001)->ecx & CPUID_SVM)
+			vcpu_alloc_svm(vm, &nested_gva);
+		else
+			vcpu_alloc_vmx(vm, &nested_gva);
+		vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
 	} else {
 		pr_info("will skip SMM test with VMX enabled\n");
 		vcpu_args_set(vm, VCPU_ID, 1, 0);

Thanks, I'll include this in v3 of the nSVM series.

Paolo

#3 lore raw
In reply to Vitaly Kuznetsov
Quoted 15 lines · Vitaly Kuznetsov

More tests may want to check if the CPU is Intel or AMD in guest code, separate cpu_has_svm() and put it as static inline to svm_util.h.

---
 tools/testing/selftests/kvm/include/x86_64/svm_util.h | 10 ++++++++++
 tools/testing/selftests/kvm/x86_64/state_test.c       |  9 +--------
 2 files changed, 11 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/x86_64/svm_util.h +0 -0
index cd037917fece..b1057773206a 100644

tools/testing/selftests/kvm/include/x86_64/svm_util.h void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r · line 43

Quoted 9 lines
tools/testing/selftests/kvm/include/x86_64/svm_util.h +6 -0
@@ -35,4 +35,14 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r
 void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
 void nested_svm_check_supported(void);
 
+static inline bool cpu_has_svm(void)
+{
+	u32 eax = 0x80000001, ecx;
+
+	asm volatile("cpuid" :
+		     "=a" (eax), "=c" (ecx) : "0" (eax) : "ebx", "edx");
	u32 eax, ecx;

	asm("cpuid" : "=a" (eax), "=c" (ecx) : "a" (0x80000001) : "ebx", "edx");

The volatile shouldn't be needed, e.g. no one should be using this purely for its seralization properties, and I don't see any reason to put the leaf number into a variable.

Alternatively, adding a proper cpuid framework to processor.h would likely be useful in the long run.

Quoted 4 lines
tools/testing/selftests/kvm/include/x86_64/svm_util.h +4 -0
+
+	return ecx & CPUID_SVM;
+}
+
#4 lore raw
Quoted 30 lines · Sean Christopherson
Quoted 15 lines · Vitaly Kuznetsov

More tests may want to check if the CPU is Intel or AMD in guest code, separate cpu_has_svm() and put it as static inline to svm_util.h.

---
 tools/testing/selftests/kvm/include/x86_64/svm_util.h | 10 ++++++++++
 tools/testing/selftests/kvm/x86_64/state_test.c       |  9 +--------
 2 files changed, 11 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/x86_64/svm_util.h +0 -0
index cd037917fece..b1057773206a 100644

tools/testing/selftests/kvm/include/x86_64/svm_util.h void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r · line 43

Quoted 9 lines
tools/testing/selftests/kvm/include/x86_64/svm_util.h +6 -0
@@ -35,4 +35,14 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r
 void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
 void nested_svm_check_supported(void);
 
+static inline bool cpu_has_svm(void)
+{
+	u32 eax = 0x80000001, ecx;
+
+	asm volatile("cpuid" :
+		     "=a" (eax), "=c" (ecx) : "0" (eax) : "ebx", "edx");
	u32 eax, ecx;

	asm("cpuid" : "=a" (eax), "=c" (ecx) : "a" (0x80000001) : "ebx", "edx");
Quoted 7 lines

The volatile shouldn't be needed, e.g. no one should be using this purely for its seralization properties, and I don't see any reason to put the leaf number into a variable.

Alternatively, adding a proper cpuid framework to processor.h would likely be useful in the long run.

All true, even better would be to find a way to include the definition of native_cpuid*() from arch/x86/include/asm/processor.h but I didn't explore these options yet, was trying to address the immediate issue with Paolo's SVM series. It can probably be done when there is a second user of cpuid in tests which needs to check something different from SVM bit.

Signature · 1 line
Vitaly