selftests: kvm: introduce cpu_has_svm() check
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
@@ -35,4 +35,14 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rvoid 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; + + asmvolatile ("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
@@ -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; - asmvolatile ("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
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
@@ -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
Paolo Bonzini
maintainerRe: [PATCH 2/2] selftests: kvm: fix smm test on SVM
Quoted 57 lines · Vitaly Kuznetsov
KVM_CAP_NESTED_STATEis now supported for AMD too but smm test acts like it is still Intel only.Signed-off-by Vitaly Kuznetsov <[email protected]>--- 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@@ -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
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 tosvm_util.h.Signed-off-by Vitaly Kuznetsov <[email protected]>--- 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
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_rvoid 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; + + asmvolatile ("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 ; +} +
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 tosvm_util.h.Signed-off-by Vitaly Kuznetsov <[email protected]>--- 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
-0tools/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_rvoid 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; + + asmvolatile ("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.hwould 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