KVM: selftests: Overhaul Part II - CPUID
Sean Christopherson
reviewerv2 cover KVM: selftests: Overhaul Part II - CPUID
Rework (x86) KVM selftests' handling of CPUID to track CPUID on a per-vCPU basis, and add X86_FEATURE_* / CPUID support in the style of KVM-Unit-Tests, e.g. add kvm_cpu_has(), vcpu_{clear,set}_cpuid_feature(), this_cpu_has(), etc...
The two main goals are to simplify checking or modifying CPUID-based features, and to eliminate the absolutely awful behavior of modifying the global "cpuid" that is cached by kvm_get_supported_cpuid().
Build tested on all architectures, all tests except AMX run on x86 (SEV INIT_EX wasn't broken, PEBKAC).
v2: Rebased to kvm/queue.
v1: https://lore.kernel.org/all/[email protected]
Sean Christopherson (42):
KVM: selftests: Set KVM's supported CPUID as vCPU's CPUID during
recreate
KVM: sefltests: Use CPUID_XSAVE and CPUID_OSXAVE instead of
X86_FEATURE_*
KVM: selftests: Add framework to query KVM CPUID bits
KVM: selftests: Use kvm_cpu_has() in the SEV migration test
KVM: selftests: Use kvm_cpu_has() for nested SVM checks
KVM: selftests: Use kvm_cpu_has() for nested VMX checks
KVM: selftests: Use kvm_cpu_has() to query PDCM in PMU selftest
KVM: selftests: Drop redundant vcpu_set_cpuid() from PMU selftest
KVM: selftests: Use kvm_cpu_has() for XSAVES in XSS MSR test
KVM: selftests: Check for _both_ XTILE data and cfg in AMX test
KVM: selftests: Use kvm_cpu_has() in AMX test
KVM: selftests: Use kvm_cpu_has() for XSAVE in cr4_cpuid_sync_test
KVM: selftests: Remove the obsolete/dead MMU role test
KVM: selftests: Use kvm_cpu_has() for KVM's PV steal time
KVM: selftests: Use kvm_cpu_has() for nSVM soft INT injection test
KVM: selftests: Verify that kvm_cpuid2.entries layout is unchanged by
KVM
KVM: selftests: Split out kvm_cpuid2_size() from allocate_kvm_cpuid2()
KVM: selftests: Cache CPUID in struct kvm_vcpu
KVM: selftests: Don't use a static local in
vcpu_get_supported_hv_cpuid()
KVM: selftests: Rename and tweak get_cpuid() to get_cpuid_entry()
KVM: selftests: Use get_cpuid_entry() in
kvm_get_supported_cpuid_index()
KVM: selftests: Add helpers to get and modify a vCPU's CPUID entries
KVM: selftests: Use vm->pa_bits to generate reserved PA bits
KVM: selftests: Add and use helper to set vCPU's CPUID maxphyaddr
KVM: selftests: Use vcpu_get_cpuid_entry() in PV features test (sort
of)
KVM: selftests: Use vCPU's CPUID directly in Hyper-V test
KVM: selftests: Use vcpu_get_cpuid_entry() in CPUID test
KVM: selftests: Use vcpu_{set,clear}_cpuid_feature() in nVMX state
test
KVM: selftests: Use vcpu_clear_cpuid_feature() to clear x2APIC
KVM: selftests: Make get_supported_cpuid() returns "const"
KVM: selftests: Set input function/index in raw CPUID helper(s)
KVM: selftests: Add this_cpu_has() to query X86_FEATURE_* via cpuid()
KVM: selftests: Use this_cpu_has() in CR4/CPUID sync test
KVM: selftests: Use this_cpu_has() to detect SVM support in L1
KVM: selftests: Drop unnecessary use of
kvm_get_supported_cpuid_index()
KVM: selftests: Rename kvm_get_supported_cpuid_index() to
__..._entry() KVM: selftests: Inline "get max CPUID leaf" helpers
KVM: selftests: Check KVM's supported CPUID, not host CPUID, for XFD
KVM: selftests: Skip AMX test if ARCH_REQ_XCOMP_GUEST_PERM isn't
supported
KVM: selftests: Clean up requirements for XFD-aware XSAVE features
KVM: selftests: Use the common cpuid() helper in
cpu_vendor_string_is()
KVM: selftests: Drop unused SVM_CPUID_FUNC macro
tools/testing/selftests/kvm/.gitignore | 1 - tools/testing/selftests/kvm/Makefile | 1 - .../selftests/kvm/include/kvm_util_base.h | 14 + .../selftests/kvm/include/x86_64/processor.h | 297 +++++++++++++++--- .../selftests/kvm/include/x86_64/svm.h | 2 - .../selftests/kvm/include/x86_64/svm_util.h | 15 - .../selftests/kvm/include/x86_64/vmx.h | 2 - tools/testing/selftests/kvm/lib/kvm_util.c | 17 +- .../selftests/kvm/lib/x86_64/perf_test_util.c | 2 +- .../selftests/kvm/lib/x86_64/processor.c | 289 +++++++---------- tools/testing/selftests/kvm/lib/x86_64/svm.c | 13 - tools/testing/selftests/kvm/lib/x86_64/vmx.c | 12 - tools/testing/selftests/kvm/steal_time.c | 4 +- tools/testing/selftests/kvm/x86_64/amx_test.c | 48 +-- .../testing/selftests/kvm/x86_64/cpuid_test.c | 89 +++--- .../kvm/x86_64/cr4_cpuid_sync_test.c | 21 +- .../kvm/x86_64/emulator_error_test.c | 10 +- .../testing/selftests/kvm/x86_64/evmcs_test.c | 2 +- .../selftests/kvm/x86_64/hyperv_cpuid.c | 14 +- .../selftests/kvm/x86_64/hyperv_features.c | 126 ++++---- .../selftests/kvm/x86_64/hyperv_svm_test.c | 2 +- .../selftests/kvm/x86_64/kvm_pv_test.c | 14 +- .../selftests/kvm/x86_64/mmu_role_test.c | 137 -------- .../kvm/x86_64/pmu_event_filter_test.c | 14 +- .../selftests/kvm/x86_64/set_sregs_test.c | 28 +- .../selftests/kvm/x86_64/sev_migrate_tests.c | 13 +- tools/testing/selftests/kvm/x86_64/smm_test.c | 9 +- .../testing/selftests/kvm/x86_64/state_test.c | 7 +- .../selftests/kvm/x86_64/svm_int_ctl_test.c | 2 +- .../kvm/x86_64/svm_nested_soft_inject_test.c | 10 +- .../selftests/kvm/x86_64/svm_vmcall_test.c | 2 +- .../kvm/x86_64/triple_fault_event_test.c | 2 +- .../kvm/x86_64/vmx_apic_access_test.c | 2 +- .../kvm/x86_64/vmx_close_while_nested_test.c | 2 +- .../selftests/kvm/x86_64/vmx_dirty_log_test.c | 2 +- .../x86_64/vmx_invalid_nested_guest_state.c | 2 +- .../kvm/x86_64/vmx_nested_tsc_scaling_test.c | 2 +- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 14 +- .../kvm/x86_64/vmx_preemption_timer_test.c | 4 +- .../kvm/x86_64/vmx_set_nested_state_test.c | 22 +- .../kvm/x86_64/vmx_tsc_adjust_test.c | 2 +- .../selftests/kvm/x86_64/xapic_state_test.c | 10 +- .../selftests/kvm/x86_64/xss_msr_test.c | 8 +- 43 files changed, 562 insertions(+), 727 deletions(-) delete mode 100644 tools/testing/selftests/kvm/x86_64/mmu_role_test.c
base-commit: 8baacf67c76c560fed954ac972b63e6e59a6fba0
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 1/42 KVM: selftests: Set KVM's supported CPUID as vCPU's CPUID during recreate
On x86-64, set KVM's supported CPUID as the vCPU's CPUID when recreating a VM+vCPU to deduplicate code for state save/restore tests, and to provide symmetry of sorts with respect to vm_create_with_one_vcpu(). The extra KVM_SET_CPUID2 call is wasteful for Hyper-V, but ultimately is nothing more than an expensive nop, and overriding the vCPU's CPUID with the Hyper-V CPUID information is the only known scenario where a state save/restore test wouldn't need/want the default CPUID.
Opportunistically use __weak for the default vm_compute_max_gfn(), it's provided by tools' compiler.h.
--- tools/testing/selftests/kvm/include/kvm_util_base.h | 9 +++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 10 ++++++++-- tools/testing/selftests/kvm/lib/x86_64/processor.c | 9 +++++++++ tools/testing/selftests/kvm/x86_64/amx_test.c | 1 - tools/testing/selftests/kvm/x86_64/smm_test.c | 1 - tools/testing/selftests/kvm/x86_64/state_test.c | 1 - .../selftests/kvm/x86_64/vmx_preemption_timer_test.c | 2 -- 7 files changed, 26 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +9 -0
@@ -681,6 +681,15 @@ static inline struct kvm_vcpu *vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,return vm_arch_vcpu_add (vm, vcpu_id, guest_code); } +/* Re-create a vCPU after restarting a VM, e.g. for state save/restore tests. */ +struct kvm_vcpu *vm_arch_vcpu_recreate (struct kvm_vm *vm,uint32_t vcpu_id); + +static inline struct kvm_vcpu *vm_vcpu_recreate (struct kvm_vm *vm, +uint32_t vcpu_id) +{ +return vm_arch_vcpu_recreate (vm, vcpu_id); +} +void virt_arch_pgd_alloc (struct kvm_vm *vm);static inline void virt_pgd_alloc (struct kvm_vm *vm)
tools/testing/selftests/kvm/lib/kvm_util.c +8 -2
@@ -392,11 +392,17 @@ void kvm_vm_restart(struct kvm_vm *vmp) } } +__weak struct kvm_vcpu *vm_arch_vcpu_recreate (struct kvm_vm *vm, +uint32_t vcpu_id) +{ +return __vm_vcpu_add (vm, vcpu_id); +} +struct kvm_vcpu *vm_recreate_with_one_vcpu (struct kvm_vm *vm) {kvm_vm_restart (vm); -return __vm_vcpu_add (vm,0 ); +return vm_vcpu_recreate (vm,0 ); }/*
@@ -1813,7 +1819,7 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)return addr_gpa2hva (vm,addr_gva2gpa (vm, gva)); } -unsigned long __attribute__((weak))vm_compute_max_gfn (struct kvm_vm *vm) +unsigned long __weak vm_compute_max_gfn (struct kvm_vm *vm) {return ((1ULL << vm->pa_bits) >> vm->page_shift) -1 ; }
tools/testing/selftests/kvm/lib/x86_64/processor.c +9 -0
@@ -663,6 +663,15 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,return vcpu; } +struct kvm_vcpu *vm_arch_vcpu_recreate (struct kvm_vm *vm,uint32_t vcpu_id) +{ +struct kvm_vcpu *vcpu =__vm_vcpu_add (vm, vcpu_id); + +vcpu_set_cpuid (vcpu,kvm_get_supported_cpuid ()); + +return vcpu; +} +/* * Allocate an instance of struct kvm_cpuid2 *
tools/testing/selftests/kvm/x86_64/amx_test.c +0 -1
@@ -425,7 +425,6 @@ int main(int argc, char *argv[])/* Restore state in a new VM. */ vcpu =vm_recreate_with_one_vcpu (vm); -vcpu_set_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_load_state (vcpu, state); run = vcpu->run;kvm_x86_state_cleanup (state);
tools/testing/selftests/kvm/x86_64/smm_test.c +0 -1
@@ -205,7 +205,6 @@ int main(int argc, char *argv[])kvm_vm_release (vm); vcpu =vm_recreate_with_one_vcpu (vm); -vcpu_set_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_load_state (vcpu, state); run = vcpu->run;kvm_x86_state_cleanup (state);
tools/testing/selftests/kvm/x86_64/state_test.c +0 -1
@@ -214,7 +214,6 @@ int main(int argc, char *argv[])/* Restore state in a new VM. */ vcpu =vm_recreate_with_one_vcpu (vm); -vcpu_set_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_load_state (vcpu, state); run = vcpu->run;kvm_x86_state_cleanup (state);
tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c +0 -2
@@ -237,8 +237,6 @@ int main(int argc, char *argv[])/* Restore state in a new VM. */ vcpu =vm_recreate_with_one_vcpu (vm); - -vcpu_set_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_load_state (vcpu, state); run = vcpu->run;kvm_x86_state_cleanup (state);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 2/42 KVM: sefltests: Use CPUID_XSAVE and CPUID_OSXAVE instead of X86_FEATURE_*
In reply to Sean ChristophersonRename X86_FEATURE_* macros to CPUID_* in the AMX and CR4/CPUID sync tests to free up the X86_FEATURE_* names for KVM-Unit-Tests style CPUID automagic where the function, leaf, register, and bit for the feature is embedded in its macro value.
No functional change intended.
--- tools/testing/selftests/kvm/include/x86_64/processor.h | 4 ++++ tools/testing/selftests/kvm/x86_64/amx_test.c | 9 +++------ tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c | 7 ++----- .../selftests/kvm/x86_64/svm_nested_soft_inject_test.c | 3 +-- 4 files changed, 10 insertions(+), 13 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +4 -0
@@ -48,6 +48,7 @@#define CPUID_SMX (1ul << 6)#define CPUID_PCID (1ul << 17)#define CPUID_XSAVE (1ul << 26) +#define CPUID_OSXSAVE (1ul << 27)/* CPUID.7.EBX */ #define CPUID_FSGSBASE (1ul << 0)
@@ -62,6 +63,9 @@/* CPUID.0x8000_0001.EDX */ #define CPUID_GBPAGES (1ul << 26) +/* CPUID.0x8000_000A.EDX */ +#define CPUID_NRIPS BIT(3) +/* Page table bitfield declarations */ #define PTE_PRESENT_MASK BIT_ULL(0)#define PTE_WRITABLE_MASK BIT_ULL(1)
tools/testing/selftests/kvm/x86_64/amx_test.c +3 -6
@@ -25,9 +25,6 @@# error This test is 64-bit only#endif -#define X86_FEATURE_XSAVE (1 << 26) -#define X86_FEATURE_OSXSAVE (1 << 27) -#define NUM_TILES 8#define TILE_SIZE 1024#define XSAVE_SIZE ((NUM_TILES * TILE_SIZE) + PAGE_SIZE)
@@ -128,9 +125,9 @@ static inline void check_cpuid_xsave(void) eax =1 ; ecx =0 ;cpuid (&eax, &ebx, &ecx, &edx); -if (!(ecx &X86_FEATURE_XSAVE )) +if (!(ecx &CPUID_XSAVE ))GUEST_ASSERT (!"cpuid: no CPU xsave support!" ); -if (!(ecx &X86_FEATURE_OSXSAVE )) +if (!(ecx &CPUID_OSXSAVE ))GUEST_ASSERT (!"cpuid: no OS xsave support!" ); }
@@ -333,7 +330,7 @@ int main(int argc, char *argv[]) vm =vm_create_with_one_vcpu (&vcpu, guest_code); entry =kvm_get_supported_cpuid_entry (1 ); -TEST_REQUIRE (entry->ecx &X86_FEATURE_XSAVE ); +TEST_REQUIRE (entry->ecx &CPUID_XSAVE );TEST_REQUIRE (kvm_get_cpuid_max_basic () >=0xd );
tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c +2 -5
@@ -19,9 +19,6 @@#include "kvm_util.h" #include "processor.h" -#define X86_FEATURE_XSAVE (1<<26) -#define X86_FEATURE_OSXSAVE (1<<27) -static inline bool cr4_cpuid_is_sync (void ) {int func, subfunc;
@@ -36,7 +33,7 @@ static inline bool cr4_cpuid_is_sync(void) cr4 =get_cr4 (); -return (!!(ecx &X86_FEATURE_OSXSAVE )) == (!!(cr4 &X86_CR4_OSXSAVE )); +return (!!(ecx &CPUID_OSXSAVE )) == (!!(cr4 &X86_CR4_OSXSAVE )); }static void guest_code (void )
@@ -70,7 +67,7 @@ int main(int argc, char *argv[])struct ucall uc; entry =kvm_get_supported_cpuid_entry (1 ); -TEST_REQUIRE (entry->ecx &X86_FEATURE_XSAVE ); +TEST_REQUIRE (entry->ecx &CPUID_XSAVE );/* Tell stdout not to buffer its content */ setbuf (stdout,NULL );
tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c +1 -2
@@ -19,7 +19,6 @@#include "test_util.h" #define INT_NR 0x20 -#define X86_FEATURE_NRIPS BIT(3)static_assert (ATOMIC_INT_LOCK_FREE ==2 ,"atomic int is not lockless" );
@@ -204,7 +203,7 @@ int main(int argc, char *argv[])nested_svm_check_supported (); cpuid =kvm_get_supported_cpuid_entry (0x8000000a ); -TEST_ASSERT (cpuid->edx &X86_FEATURE_NRIPS , +TEST_ASSERT (cpuid->edx &CPUID_NRIPS ,"KVM with nSVM is supposed to unconditionally advertise nRIP Save\n" );atomic_init (&nmi_stage,0 );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 3/42 KVM: selftests: Add framework to query KVM CPUID bits
In reply to Sean ChristophersonAdd X86_FEATURE_* magic in the style of KVM-Unit-Tests' implementation, where the CPUID function, index, output register, and output bit position are embedded in the macro value. Add kvm_cpu_has() to query KVM's supported CPUID and use it set_sregs_test, which is the most prolific user of manual feature querying.
Opportunstically rename calc_cr4_feature_bits() to calc_supported_cr4_feature_bits() to better capture how the CR4 bits are chosen.
--- .../selftests/kvm/include/x86_64/processor.h | 106 ++++++++++++++++-- .../selftests/kvm/lib/x86_64/processor.c | 22 ++++ .../selftests/kvm/x86_64/set_sregs_test.c | 28 ++--- 3 files changed, 128 insertions(+), 28 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +94 -12
@@ -43,23 +43,96 @@#define X86_CR4_SMAP (1ul << 21)#define X86_CR4_PKE (1ul << 22) +/* Note, these are ordered alphabetically to match kvm_cpuid_entry2. Eww. */ +enum cpuid_output_regs { +KVM_CPUID_EAX , +KVM_CPUID_EBX , +KVM_CPUID_ECX , +KVM_CPUID_EDX +}; + +/* +* Pack the information into a 64-bit value so that each X86_FEATURE_XXX can be +* passed by value with no overhead. +*/ +struct kvm_x86_cpu_feature { +u32 function; +u16 index; +u8 reg; +u8 bit; +}; +#define KVM_X86_CPU_FEATURE (fn, idx, gpr, __bit) \ +({ \ + struct kvm_x86_cpu_feature feature = { \ + .function = fn, \ + .index = idx, \ + .reg = KVM_CPUID_##gpr, \ + .bit = __bit, \ + }; \ + \ + feature; \ +}) + +/* +* Basic Leafs, a.k.a. Intel defined +*/ +#define X86_FEATURE_MWAIT KVM_X86_CPU_FEATURE(0x1, 0, ECX, 3) +#define X86_FEATURE_VMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 5) +#define X86_FEATURE_SMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 6) +#define X86_FEATURE_PCID KVM_X86_CPU_FEATURE(0x1, 0, ECX, 17) +#define X86_FEATURE_MOVBE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 22) +#define X86_FEATURE_TSC_DEADLINE_TIMER KVM_X86_CPU_FEATURE(0x1, 0, ECX, 24) +#define X86_FEATURE_XSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 26) +#define X86_FEATURE_OSXSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 27) +#define X86_FEATURE_RDRAND KVM_X86_CPU_FEATURE(0x1, 0, ECX, 30) +#define X86_FEATURE_MCE KVM_X86_CPU_FEATURE(0x1, 0, EDX, 7) +#define X86_FEATURE_APIC KVM_X86_CPU_FEATURE(0x1, 0, EDX, 9) +#define X86_FEATURE_CLFLUSH KVM_X86_CPU_FEATURE(0x1, 0, EDX, 19) +#define X86_FEATURE_XMM KVM_X86_CPU_FEATURE(0x1, 0, EDX, 25) +#define X86_FEATURE_XMM2 KVM_X86_CPU_FEATURE(0x1, 0, EDX, 26) +#define X86_FEATURE_FSGSBASE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 0) +#define X86_FEATURE_TSC_ADJUST KVM_X86_CPU_FEATURE(0x7, 0, EBX, 1) +#define X86_FEATURE_HLE KVM_X86_CPU_FEATURE(0x7, 0, EBX, 4) +#define X86_FEATURE_SMEP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 7) +#define X86_FEATURE_INVPCID KVM_X86_CPU_FEATURE(0x7, 0, EBX, 10) +#define X86_FEATURE_RTM KVM_X86_CPU_FEATURE(0x7, 0, EBX, 11) +#define X86_FEATURE_SMAP KVM_X86_CPU_FEATURE(0x7, 0, EBX, 20) +#define X86_FEATURE_PCOMMIT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 22) +#define X86_FEATURE_CLFLUSHOPT KVM_X86_CPU_FEATURE(0x7, 0, EBX, 23) +#define X86_FEATURE_CLWB KVM_X86_CPU_FEATURE(0x7, 0, EBX, 24) +#define X86_FEATURE_UMIP KVM_X86_CPU_FEATURE(0x7, 0, ECX, 2) +#define X86_FEATURE_PKU KVM_X86_CPU_FEATURE(0x7, 0, ECX, 3) +#define X86_FEATURE_LA57 KVM_X86_CPU_FEATURE(0x7, 0, ECX, 16) +#define X86_FEATURE_RDPID KVM_X86_CPU_FEATURE(0x7, 0, ECX, 22) +#define X86_FEATURE_SHSTK KVM_X86_CPU_FEATURE(0x7, 0, ECX, 7) +#define X86_FEATURE_IBT KVM_X86_CPU_FEATURE(0x7, 0, EDX, 20) +#define X86_FEATURE_SPEC_CTRL KVM_X86_CPU_FEATURE(0x7, 0, EDX, 26) +#define X86_FEATURE_ARCH_CAPABILITIES KVM_X86_CPU_FEATURE(0x7, 0, EDX, 29) +#define X86_FEATURE_PKS KVM_X86_CPU_FEATURE(0x7, 0, ECX, 31) + +/* +* Extended Leafs, a.k.a. AMD defined +*/ +#define X86_FEATURE_SVM KVM_X86_CPU_FEATURE(0x80000001, 0, ECX, 2) +#define X86_FEATURE_NX KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 20) +#define X86_FEATURE_GBPAGES KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 26) +#define X86_FEATURE_RDTSCP KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 27) +#define X86_FEATURE_LM KVM_X86_CPU_FEATURE(0x80000001, 0, EDX, 29) +#define X86_FEATURE_RDPRU KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 4) +#define X86_FEATURE_AMD_IBPB KVM_X86_CPU_FEATURE(0x80000008, 0, EBX, 12) +#define X86_FEATURE_NPT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 0) +#define X86_FEATURE_LBRV KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 1) +#define X86_FEATURE_NRIPS KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 3) +#define X86_FEATURE_TSCRATEMSR KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 4) +#define X86_FEATURE_PAUSEFILTER KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10) +#define X86_FEATURE_PFTHRESHOLD KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12) +#define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16) +/* CPUID.1.ECX */ #define CPUID_VMX (1ul << 5) -#define CPUID_SMX (1ul << 6) -#define CPUID_PCID (1ul << 17)#define CPUID_XSAVE (1ul << 26)#define CPUID_OSXSAVE (1ul << 27) -/* CPUID.7.EBX */ -#define CPUID_FSGSBASE (1ul << 0) -#define CPUID_SMEP (1ul << 7) -#define CPUID_SMAP (1ul << 20) - -/* CPUID.7.ECX */ -#define CPUID_UMIP (1ul << 2) -#define CPUID_PKU (1ul << 3) -#define CPUID_LA57 (1ul << 16) -/* CPUID.0x8000_0001.EDX */ #define CPUID_GBPAGES (1ul << 26)
@@ -488,6 +561,15 @@ static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs) }struct kvm_cpuid2 *kvm_get_supported_cpuid (void ); + +bool kvm_cpuid_has (const struct kvm_cpuid2 *cpuid, +struct kvm_x86_cpu_feature feature); + +static inline bool kvm_cpu_has (struct kvm_x86_cpu_feature feature) +{ +return kvm_cpuid_has (kvm_get_supported_cpuid (), feature); +} +struct kvm_cpuid2 *vcpu_get_cpuid (struct kvm_vcpu *vcpu); static inline int __vcpu_set_cpuid(struct kvm_vcpu *vcpu,
tools/testing/selftests/kvm/lib/x86_64/processor.c +22 -0
@@ -734,6 +734,28 @@ struct kvm_cpuid2 *kvm_get_supported_cpuid(void)return cpuid; } +bool kvm_cpuid_has (const struct kvm_cpuid2 *cpuid, +struct kvm_x86_cpu_feature feature) +{ +const struct kvm_cpuid_entry2 *entry; +int i; + +for (i =0 ; i < cpuid->nent; i++) { + entry = &cpuid->entries[i]; + +/* +* The output registers in kvm_cpuid_entry2 are in alphabetical +* order, but kvm_x86_cpu_feature matches that mess, so yay +* pointer shenanigans! +*/ +if (entry->function == feature.function && + entry->index == feature.index) +return (&entry->eax)[feature.reg] &BIT (feature.bit); + } + +return false; +} +uint64_t kvm_get_feature_msr (uint64_t msr_index) {struct {
tools/testing/selftests/kvm/x86_64/set_sregs_test.c +12 -16
@@ -43,36 +43,32 @@ static void test_cr4_feature_bit(struct kvm_vcpu *vcpu, struct kvm_sregs *orig,TEST_ASSERT (!memcmp (&sregs, orig,sizeof (sregs)),"KVM modified sregs" ); } -static uint64_t calc_cr4_feature_bits (struct kvm_vm *vm) +static uint64_t calc_supported_cr4_feature_bits (void ) { -struct kvm_cpuid_entry2 *cpuid_1, *cpuid_7;uint64_t cr4; - cpuid_1 =kvm_get_supported_cpuid_entry (1 ); - cpuid_7 =kvm_get_supported_cpuid_entry (7 ); - cr4 =X86_CR4_VME |X86_CR4_PVI |X86_CR4_TSD |X86_CR4_DE |X86_CR4_PSE |X86_CR4_PAE |X86_CR4_MCE |X86_CR4_PGE |X86_CR4_PCE |X86_CR4_OSFXSR |X86_CR4_OSXMMEXCPT ; -if (cpuid_7->ecx &CPUID_UMIP ) +if (kvm_cpu_has (X86_FEATURE_UMIP )) cr4 |=X86_CR4_UMIP ; -if (cpuid_7->ecx &CPUID_LA57 ) +if (kvm_cpu_has (X86_FEATURE_LA57 )) cr4 |=X86_CR4_LA57 ; -if (cpuid_1->ecx &CPUID_VMX ) +if (kvm_cpu_has (X86_FEATURE_VMX )) cr4 |=X86_CR4_VMXE ; -if (cpuid_1->ecx &CPUID_SMX ) +if (kvm_cpu_has (X86_FEATURE_SMX )) cr4 |=X86_CR4_SMXE ; -if (cpuid_7->ebx &CPUID_FSGSBASE ) +if (kvm_cpu_has (X86_FEATURE_FSGSBASE )) cr4 |=X86_CR4_FSGSBASE ; -if (cpuid_1->ecx &CPUID_PCID ) +if (kvm_cpu_has (X86_FEATURE_PCID )) cr4 |=X86_CR4_PCIDE ; -if (cpuid_1->ecx &CPUID_XSAVE ) +if (kvm_cpu_has (X86_FEATURE_XSAVE )) cr4 |=X86_CR4_OSXSAVE ; -if (cpuid_7->ebx &CPUID_SMEP ) +if (kvm_cpu_has (X86_FEATURE_SMEP )) cr4 |=X86_CR4_SMEP ; -if (cpuid_7->ebx &CPUID_SMAP ) +if (kvm_cpu_has (X86_FEATURE_SMAP )) cr4 |=X86_CR4_SMAP ; -if (cpuid_7->ecx &CPUID_PKU ) +if (kvm_cpu_has (X86_FEATURE_PKU )) cr4 |=X86_CR4_PKE ;return cr4;
@@ -99,7 +95,7 @@ int main(int argc, char *argv[])vcpu_sregs_get (vcpu, &sregs); - sregs.cr4 |=calc_cr4_feature_bits (vm); + sregs.cr4 |=calc_supported_cr4_feature_bits (); cr4 = sregs.cr4; rc =_vcpu_sregs_set (vcpu, &sregs);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 4/42 KVM: selftests: Use kvm_cpu_has() in the SEV migration test
In reply to Sean ChristophersonUse kvm_cpu_has() in the SEV migration test instead of open coding equivalent functionality using kvm_get_supported_cpuid_entry().
No functional change intended.
--- .../selftests/kvm/include/x86_64/processor.h | 2 ++ .../selftests/kvm/x86_64/sev_migrate_tests.c | 13 ++----------- 2 files changed, 4 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +2 -0
@@ -127,6 +127,8 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_PAUSEFILTER KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10)#define X86_FEATURE_PFTHRESHOLD KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12)#define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16) +#define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1) +#define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)/* CPUID.1.ECX */ #define CPUID_VMX (1ul << 5)
tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c +2 -11
@@ -393,23 +393,14 @@ static void test_sev_move_copy(void)kvm_vm_free (sev_vm); } -#define X86_FEATURE_SEV (1 << 1) -#define X86_FEATURE_SEV_ES (1 << 3) -int main (int argc,char *argv[]) { -struct kvm_cpuid_entry2 *cpuid; -TEST_REQUIRE (kvm_has_cap (KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM ));TEST_REQUIRE (kvm_has_cap (KVM_CAP_VM_COPY_ENC_CONTEXT_FROM )); - cpuid =kvm_get_supported_cpuid_entry (0x80000000 ); -TEST_REQUIRE (cpuid->eax >=0x8000001f ); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SEV )); - cpuid =kvm_get_supported_cpuid_entry (0x8000001f ); -TEST_REQUIRE (cpuid->eax &X86_FEATURE_SEV ); - - have_sev_es = !!(cpuid->eax &X86_FEATURE_SEV_ES ); + have_sev_es =kvm_cpu_has (X86_FEATURE_SEV_ES );if (kvm_check_cap (KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM )) {test_sev_migrate_from (/* es= */ false);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 5/42 KVM: selftests: Use kvm_cpu_has() for nested SVM checks
In reply to Sean ChristophersonUse kvm_cpu_has() to check for nested SVM support, and drop the helpers now that their functionality is trivial to implement.
No functional change intended.
--- .../testing/selftests/kvm/include/x86_64/svm_util.h | 2 -- tools/testing/selftests/kvm/lib/x86_64/svm.c | 13 ------------- .../testing/selftests/kvm/x86_64/hyperv_svm_test.c | 2 +- tools/testing/selftests/kvm/x86_64/smm_test.c | 2 +- tools/testing/selftests/kvm/x86_64/state_test.c | 2 +- .../testing/selftests/kvm/x86_64/svm_int_ctl_test.c | 2 +- .../kvm/x86_64/svm_nested_soft_inject_test.c | 2 +- .../testing/selftests/kvm/x86_64/svm_vmcall_test.c | 2 +- 8 files changed, 6 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/include/x86_64/svm_util.h +0 -2
@@ -51,8 +51,6 @@ struct svm_test_data {struct svm_test_data *vcpu_alloc_svm (struct kvm_vm *vm,vm_vaddr_t *p_svm_gva);void generic_svm_setup (struct svm_test_data *svm,void *guest_rip,void *guest_rsp);void run_guest (struct vmcb *vmcb,uint64_t vmcb_gpa); -bool nested_svm_supported (void ); -void nested_svm_check_supported (void );static inline bool cpu_has_svm (void ) {
tools/testing/selftests/kvm/lib/x86_64/svm.c +0 -13
@@ -164,19 +164,6 @@ void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa) : "r15", "memory"); } -bool nested_svm_supported (void ) -{ -struct kvm_cpuid_entry2 *entry = -kvm_get_supported_cpuid_entry (0x80000001 ); - -return entry->ecx &CPUID_SVM ; -} - -void nested_svm_check_supported (void ) -{ -TEST_REQUIRE (nested_svm_supported ()); -} -/* * Open SEV_DEV_PATH if available, otherwise exit the entire program. *
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +1 -1
@@ -127,7 +127,7 @@ int main(int argc, char *argv[])struct ucall uc;int stage; -TEST_REQUIRE (nested_svm_supported ()); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM ));/* Create VM */ vm =vm_create_with_one_vcpu (&vcpu, guest_code);
tools/testing/selftests/kvm/x86_64/smm_test.c +1 -1
@@ -154,7 +154,7 @@ int main(int argc, char *argv[])vcpu_set_msr (vcpu,MSR_IA32_SMBASE ,SMRAM_GPA );if (kvm_check_cap (KVM_CAP_NESTED_STATE )) { -if (nested_svm_supported ()) +if (kvm_cpu_has (X86_FEATURE_SVM ))vcpu_alloc_svm (vm, &nested_gva);else if (nested_vmx_supported ())vcpu_alloc_vmx (vm, &nested_gva);
tools/testing/selftests/kvm/x86_64/state_test.c +1 -1
@@ -170,7 +170,7 @@ int main(int argc, char *argv[])vcpu_regs_get (vcpu, ®s1);if (kvm_check_cap (KVM_CAP_NESTED_STATE )) { -if (nested_svm_supported ()) +if (kvm_cpu_has (X86_FEATURE_SVM ))vcpu_alloc_svm (vm, &nested_gva);else if (nested_vmx_supported ())vcpu_alloc_vmx (vm, &nested_gva);
tools/testing/selftests/kvm/x86_64/svm_int_ctl_test.c +1 -1
@@ -90,7 +90,7 @@ int main(int argc, char *argv[])struct kvm_vm *vm;struct ucall uc; -nested_svm_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); vm =vm_create_with_one_vcpu (&vcpu, l1_guest_code);
tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c +1 -1
@@ -200,7 +200,7 @@ int main(int argc, char *argv[])/* Tell stdout not to buffer its content */ setbuf (stdout,NULL ); -nested_svm_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); cpuid =kvm_get_supported_cpuid_entry (0x8000000a ); TEST_ASSERT(cpuid->edx & CPUID_NRIPS,
tools/testing/selftests/kvm/x86_64/svm_vmcall_test.c +1 -1
@@ -39,7 +39,7 @@ int main(int argc, char *argv[])vm_vaddr_t svm_gva;struct kvm_vm *vm; -nested_svm_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); vm =vm_create_with_one_vcpu (&vcpu, l1_guest_code);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 6/42 KVM: selftests: Use kvm_cpu_has() for nested VMX checks
In reply to Sean ChristophersonUse kvm_cpu_has() to check for nested VMX support, and drop the helpers now that their functionality is trivial to implement.
No functional change intended.
--- tools/testing/selftests/kvm/include/x86_64/vmx.h | 2 -- .../selftests/kvm/lib/x86_64/perf_test_util.c | 2 +- tools/testing/selftests/kvm/lib/x86_64/vmx.c | 12 ------------ tools/testing/selftests/kvm/x86_64/evmcs_test.c | 2 +- tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 4 ++-- tools/testing/selftests/kvm/x86_64/smm_test.c | 2 +- tools/testing/selftests/kvm/x86_64/state_test.c | 2 +- .../selftests/kvm/x86_64/triple_fault_event_test.c | 2 +- .../selftests/kvm/x86_64/vmx_apic_access_test.c | 2 +- .../kvm/x86_64/vmx_close_while_nested_test.c | 2 +- .../selftests/kvm/x86_64/vmx_dirty_log_test.c | 2 +- .../kvm/x86_64/vmx_invalid_nested_guest_state.c | 2 +- .../kvm/x86_64/vmx_nested_tsc_scaling_test.c | 2 +- .../selftests/kvm/x86_64/vmx_preemption_timer_test.c | 2 +- .../selftests/kvm/x86_64/vmx_set_nested_state_test.c | 2 +- .../selftests/kvm/x86_64/vmx_tsc_adjust_test.c | 2 +- 16 files changed, 15 insertions(+), 29 deletions(-)
tools/testing/selftests/kvm/include/x86_64/vmx.h +0 -2
@@ -607,8 +607,6 @@ bool prepare_for_vmx_operation(struct vmx_pages *vmx);void prepare_vmcs (struct vmx_pages *vmx,void *guest_rip,void *guest_rsp);bool load_vmcs (struct vmx_pages *vmx); -bool nested_vmx_supported (void ); -void nested_vmx_check_supported (void );bool ept_1g_pages_supported (void ); void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
tools/testing/selftests/kvm/lib/x86_64/perf_test_util.c +1 -1
@@ -84,7 +84,7 @@ void perf_test_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vcvm_vaddr_t vmx_gva;int vcpu_id; -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX ));for (vcpu_id =0 ; vcpu_id < nr_vcpus; vcpu_id++) { vmx =vcpu_alloc_vmx (vm, &vmx_gva);
tools/testing/selftests/kvm/lib/x86_64/vmx.c +0 -12
@@ -382,18 +382,6 @@ void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp)init_vmcs_guest_state (guest_rip, guest_rsp); } -bool nested_vmx_supported (void ) -{ -struct kvm_cpuid_entry2 *entry =kvm_get_supported_cpuid_entry (1 ); - -return entry->ecx &CPUID_VMX ; -} - -void nested_vmx_check_supported (void ) -{ -TEST_REQUIRE (nested_vmx_supported ()); -} - static void nested_create_pte(struct kvm_vm *vm, struct eptPageTableEntry *pte, uint64_t nested_paddr,
tools/testing/selftests/kvm/x86_64/evmcs_test.c +1 -1
@@ -208,7 +208,7 @@ int main(int argc, char *argv[]) vm =vm_create_with_one_vcpu (&vcpu, guest_code); -TEST_REQUIRE (nested_vmx_supported ()); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX ));TEST_REQUIRE (kvm_has_cap (KVM_CAP_NESTED_STATE ));TEST_REQUIRE (kvm_has_cap (KVM_CAP_HYPERV_ENLIGHTENED_VMCS ));
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +2 -2
@@ -148,7 +148,7 @@ int main(int argc, char *argv[])test_hv_cpuid (hv_cpuid_entries, false);free (hv_cpuid_entries); -if (!nested_vmx_supported () || +if (!kvm_cpu_has (X86_FEATURE_VMX ) || !kvm_has_cap (KVM_CAP_HYPERV_ENLIGHTENED_VMCS )) {print_skip ("Enlightened VMCS is unsupported" ); goto do_sys;
@@ -168,7 +168,7 @@ int main(int argc, char *argv[])test_hv_cpuid_e2big (vm,NULL ); hv_cpuid_entries =kvm_get_supported_hv_cpuid (); -test_hv_cpuid (hv_cpuid_entries,nested_vmx_supported ()); +test_hv_cpuid (hv_cpuid_entries,kvm_cpu_has (X86_FEATURE_VMX )); out:kvm_vm_free (vm);
tools/testing/selftests/kvm/x86_64/smm_test.c +1 -1
@@ -156,7 +156,7 @@ int main(int argc, char *argv[])if (kvm_check_cap (KVM_CAP_NESTED_STATE )) {if (kvm_cpu_has (X86_FEATURE_SVM ))vcpu_alloc_svm (vm, &nested_gva); -else if (nested_vmx_supported ()) +else if (kvm_cpu_has (X86_FEATURE_VMX ))vcpu_alloc_vmx (vm, &nested_gva); }
tools/testing/selftests/kvm/x86_64/state_test.c +1 -1
@@ -172,7 +172,7 @@ int main(int argc, char *argv[])if (kvm_check_cap (KVM_CAP_NESTED_STATE )) {if (kvm_cpu_has (X86_FEATURE_SVM ))vcpu_alloc_svm (vm, &nested_gva); -else if (nested_vmx_supported ()) +else if (kvm_cpu_has (X86_FEATURE_VMX ))vcpu_alloc_vmx (vm, &nested_gva); }
tools/testing/selftests/kvm/x86_64/triple_fault_event_test.c +1 -1
@@ -46,7 +46,7 @@ int main(void)vm_vaddr_t vmx_pages_gva;struct ucall uc; -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX ));TEST_REQUIRE (kvm_has_cap (KVM_CAP_X86_TRIPLE_FAULT_EVENT ));
tools/testing/selftests/kvm/x86_64/vmx_apic_access_test.c +1 -1
@@ -80,7 +80,7 @@ int main(int argc, char *argv[])struct kvm_vcpu *vcpu;struct kvm_vm *vm; -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX )); vm =vm_create_with_one_vcpu (&vcpu, l1_guest_code);
tools/testing/selftests/kvm/x86_64/vmx_close_while_nested_test.c +1 -1
@@ -51,7 +51,7 @@ int main(int argc, char *argv[])struct kvm_vcpu *vcpu;struct kvm_vm *vm; -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX )); vm =vm_create_with_one_vcpu (&vcpu, l1_guest_code);
tools/testing/selftests/kvm/x86_64/vmx_dirty_log_test.c +1 -1
@@ -77,7 +77,7 @@ int main(int argc, char *argv[])struct ucall uc;bool done = false; -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX ));/* Create VM */ vm =vm_create_with_one_vcpu (&vcpu, l1_guest_code);
tools/testing/selftests/kvm/x86_64/vmx_invalid_nested_guest_state.c +1 -1
@@ -58,7 +58,7 @@ int main(int argc, char *argv[])struct kvm_run *run;struct ucall uc; -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX )); vm =vm_create_with_one_vcpu (&vcpu, l1_guest_code);
tools/testing/selftests/kvm/x86_64/vmx_nested_tsc_scaling_test.c +1 -1
@@ -150,7 +150,7 @@ int main(int argc, char *argv[])uint64_t l1_tsc_freq =0 ;uint64_t l2_tsc_freq =0 ; -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX ));TEST_REQUIRE (kvm_has_cap (KVM_CAP_TSC_CONTROL ));stable_tsc_check_supported ();
tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c +1 -1
@@ -167,7 +167,7 @@ int main(int argc, char *argv[])* AMD currently does not implement any VMX features, so for now we * just early out. */ -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX ));TEST_REQUIRE (kvm_has_cap (KVM_CAP_NESTED_STATE ));
tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c +1 -1
@@ -273,7 +273,7 @@ int main(int argc, char *argv[])* AMD currently does not implement set_nested_state, so for now we * just early out. */ -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX )); vm =vm_create_with_one_vcpu (&vcpu,NULL );
tools/testing/selftests/kvm/x86_64/vmx_tsc_adjust_test.c +1 -1
@@ -127,7 +127,7 @@ int main(int argc, char *argv[])vm_vaddr_t vmx_pages_gva;struct kvm_vcpu *vcpu; -nested_vmx_check_supported (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX )); vm =vm_create_with_one_vcpu (&vcpu, (void *) l1_guest_code);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 7/42 KVM: selftests: Use kvm_cpu_has() to query PDCM in PMU selftest
In reply to Sean ChristophersonUse kvm_cpu_has() in the PMU test to query PDCM support instead of open coding equivalent functionality using kvm_get_supported_cpuid_index().
No functional change intended.
--- tools/testing/selftests/kvm/include/x86_64/processor.h | 1 + tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -79,6 +79,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_MWAIT KVM_X86_CPU_FEATURE(0x1, 0, ECX, 3)#define X86_FEATURE_VMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 5)#define X86_FEATURE_SMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 6) +#define X86_FEATURE_PDCM KVM_X86_CPU_FEATURE(0x1, 0, ECX, 15)#define X86_FEATURE_PCID KVM_X86_CPU_FEATURE(0x1, 0, ECX, 17)#define X86_FEATURE_MOVBE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 22)#define X86_FEATURE_TSC_DEADLINE_TIMER KVM_X86_CPU_FEATURE(0x1, 0, ECX, 24)
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +2 -5
@@ -17,7 +17,6 @@#include "kvm_util.h" #include "vmx.h" -#define X86_FEATURE_PDCM (1<<15)#define PMU_CAP_FW_WRITES (1ULL << 13)#define PMU_CAP_LBR_FMT 0x3f
@@ -55,7 +54,6 @@ static void guest_code(void)int main (int argc,char *argv[]) {struct kvm_cpuid2 *cpuid; -struct kvm_cpuid_entry2 *entry_1_0;struct kvm_cpuid_entry2 *entry_a_0;struct kvm_vm *vm;struct kvm_vcpu *vcpu;
@@ -70,11 +68,10 @@ int main(int argc, char *argv[]) vm =vm_create_with_one_vcpu (&vcpu, guest_code); cpuid =kvm_get_supported_cpuid (); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_PDCM )); +TEST_REQUIRE (kvm_get_cpuid_max_basic () >=0xa ); - - entry_1_0 =kvm_get_supported_cpuid_index (1 ,0 ); entry_a_0 =kvm_get_supported_cpuid_index (0xa ,0 ); -TEST_REQUIRE (entry_1_0->ecx &X86_FEATURE_PDCM ); eax.full = entry_a_0->eax;__TEST_REQUIRE (eax.split.version_id,"PMU is not supported by the vCPU" );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 8/42 KVM: selftests: Drop redundant vcpu_set_cpuid() from PMU selftest
In reply to Sean ChristophersonDrop a redundant vcpu_set_cpuid() from the PMU test. The vCPU's CPUID is set to KVM's supported CPUID by vm_create_with_one_vcpu(), which was also true back when the helper was named vm_create_default().
--- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 3 --- 1 file changed, 3 deletions(-)
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +0 -3
@@ -53,7 +53,6 @@ static void guest_code(void)int main (int argc,char *argv[]) { -struct kvm_cpuid2 *cpuid;struct kvm_cpuid_entry2 *entry_a_0;struct kvm_vm *vm;struct kvm_vcpu *vcpu;
@@ -66,7 +65,6 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_with_one_vcpu (&vcpu, guest_code); - cpuid =kvm_get_supported_cpuid ();TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_PDCM ));
@@ -77,7 +75,6 @@ int main(int argc, char *argv[])__TEST_REQUIRE (eax.split.version_id,"PMU is not supported by the vCPU" );/* testcase 1, set capabilities when we have PDCM bit */ -vcpu_set_cpuid (vcpu, cpuid);vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,PMU_CAP_FW_WRITES );/* check capabilities can be retrieved with KVM_GET_MSR */
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 9/42 KVM: selftests: Use kvm_cpu_has() for XSAVES in XSS MSR test
In reply to Sean ChristophersonUse kvm_cpu_has() in the XSS MSR test instead of open coding equivalent functionality using kvm_get_supported_cpuid_index().
No functional change intended.
--- tools/testing/selftests/kvm/include/x86_64/processor.h | 1 + tools/testing/selftests/kvm/x86_64/xss_msr_test.c | 8 +------- 2 files changed, 2 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -110,6 +110,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_SPEC_CTRL KVM_X86_CPU_FEATURE(0x7, 0, EDX, 26)#define X86_FEATURE_ARCH_CAPABILITIES KVM_X86_CPU_FEATURE(0x7, 0, EDX, 29)#define X86_FEATURE_PKS KVM_X86_CPU_FEATURE(0x7, 0, ECX, 31) +#define X86_FEATURE_XSAVES KVM_X86_CPU_FEATURE(0xD, 1, EAX, 3)/* * Extended Leafs, a.k.a. AMD defined
tools/testing/selftests/kvm/x86_64/xss_msr_test.c +1 -7
@@ -14,11 +14,8 @@#define MSR_BITS 64 -#define X86_FEATURE_XSAVES (1<<3) -int main (int argc,char *argv[]) { -struct kvm_cpuid_entry2 *entry;bool xss_in_msr_list;struct kvm_vm *vm;struct kvm_vcpu *vcpu;
@@ -28,10 +25,7 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_with_one_vcpu (&vcpu,NULL ); -TEST_REQUIRE (kvm_get_cpuid_max_basic () >=0xd ); - - entry =kvm_get_supported_cpuid_index (0xd ,1 ); -TEST_REQUIRE (entry->eax &X86_FEATURE_XSAVES ); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_XSAVES )); xss_val =vcpu_get_msr (vcpu,MSR_IA32_XSS ); TEST_ASSERT(xss_val == 0,
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 10/42 KVM: selftests: Check for _both_ XTILE data and cfg in AMX test
In reply to Sean ChristophersonCheck for _both_ XTILE data and cfg support in the AMX test instead of checking for _either_ feature. Practically speaking, no sane CPU or vCPU will support one but not the other, but the effective "or" behavior is subtle and technically incorrect.
--- tools/testing/selftests/kvm/x86_64/amx_test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/x86_64/amx_test.c +2 -1
@@ -335,7 +335,8 @@ int main(int argc, char *argv[])TEST_REQUIRE (kvm_get_cpuid_max_basic () >=0xd ); entry =kvm_get_supported_cpuid_index (0xd ,0 ); -TEST_REQUIRE (entry->eax &XFEATURE_MASK_XTILE ); +TEST_REQUIRE (entry->eax &XFEATURE_MASK_XTILECFG ); +TEST_REQUIRE (entry->eax &XFEATURE_MASK_XTILEDATA );/* Get xsave/restore max size */ xsave_restore_size = entry->ecx;
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 11/42 KVM: selftests: Use kvm_cpu_has() in AMX test
In reply to Sean ChristophersonUse kvm_cpu_has() in the AMX test instead of open coding equivalent functionality using kvm_get_supported_cpuid_entry() and kvm_get_supported_cpuid_index().
No functional change intended.
--- .../selftests/kvm/include/x86_64/processor.h | 3 +++ tools/testing/selftests/kvm/x86_64/amx_test.c | 17 ++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +3 -0
@@ -107,9 +107,12 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_RDPID KVM_X86_CPU_FEATURE(0x7, 0, ECX, 22)#define X86_FEATURE_SHSTK KVM_X86_CPU_FEATURE(0x7, 0, ECX, 7)#define X86_FEATURE_IBT KVM_X86_CPU_FEATURE(0x7, 0, EDX, 20) +#define X86_FEATURE_AMX_TILE KVM_X86_CPU_FEATURE(0x7, 0, EDX, 24)#define X86_FEATURE_SPEC_CTRL KVM_X86_CPU_FEATURE(0x7, 0, EDX, 26)#define X86_FEATURE_ARCH_CAPABILITIES KVM_X86_CPU_FEATURE(0x7, 0, EDX, 29)#define X86_FEATURE_PKS KVM_X86_CPU_FEATURE(0x7, 0, ECX, 31) +#define X86_FEATURE_XTILECFG KVM_X86_CPU_FEATURE(0xD, 0, EAX, 17) +#define X86_FEATURE_XTILEDATA KVM_X86_CPU_FEATURE(0xD, 0, EAX, 18)#define X86_FEATURE_XSAVES KVM_X86_CPU_FEATURE(0xD, 1, EAX, 3)/*
tools/testing/selftests/kvm/x86_64/amx_test.c +6 -11
@@ -312,13 +312,12 @@ void guest_nm_handler(struct ex_regs *regs)int main (int argc,char *argv[]) { -struct kvm_cpuid_entry2 *entry;struct kvm_regs regs1, regs2;struct kvm_vcpu *vcpu;struct kvm_vm *vm;struct kvm_run *run;struct kvm_x86_state *state; -int xsave_restore_size =0 ; +int xsave_restore_size;vm_vaddr_t amx_cfg, tiledata, xsavedata;struct ucall uc;u32 amx_offset;
@@ -329,17 +328,13 @@ int main(int argc, char *argv[])/* Create VM */ vm =vm_create_with_one_vcpu (&vcpu, guest_code); - entry =kvm_get_supported_cpuid_entry (1 ); -TEST_REQUIRE (entry->ecx &CPUID_XSAVE ); - -TEST_REQUIRE (kvm_get_cpuid_max_basic () >=0xd ); - - entry =kvm_get_supported_cpuid_index (0xd ,0 ); -TEST_REQUIRE (entry->eax &XFEATURE_MASK_XTILECFG ); -TEST_REQUIRE (entry->eax &XFEATURE_MASK_XTILEDATA ); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_XSAVE )); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_AMX_TILE )); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_XTILECFG )); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_XTILEDATA ));/* Get xsave/restore max size */ - xsave_restore_size = entry->ecx; + xsave_restore_size =kvm_get_supported_cpuid_index (0xd ,0 )->ecx; run = vcpu->run;vcpu_regs_get (vcpu, ®s1);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 12/42 KVM: selftests: Use kvm_cpu_has() for XSAVE in cr4_cpuid_sync_test
In reply to Sean ChristophersonUse kvm_cpu_has() in the CR4/CPUID sync test instead of open coding equivalent functionality using kvm_get_supported_cpuid_entry().
No functional change intended.
--- tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c +1 -3
@@ -63,11 +63,9 @@ int main(int argc, char *argv[])struct kvm_run *run;struct kvm_vm *vm;struct kvm_sregs sregs; -struct kvm_cpuid_entry2 *entry;struct ucall uc; - entry =kvm_get_supported_cpuid_entry (1 ); -TEST_REQUIRE (entry->ecx &CPUID_XSAVE ); +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_XSAVE ));/* Tell stdout not to buffer its content */ setbuf (stdout,NULL );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 13/42 KVM: selftests: Remove the obsolete/dead MMU role test
In reply to Sean ChristophersonRemove the MMU role test, which was made obsolete by KVM commit feb627e8d6f6 ("KVM: x86: Forbid KVM_SET_CPUID{,2} after KVM_RUN"). The ongoing costs of keeping the test updated far outweigh any benefits, e.g. the test _might_ be useful as an example or for documentation purposes, but otherwise the test is dead weight.
--- tools/testing/selftests/kvm/.gitignore | 1 - tools/testing/selftests/kvm/Makefile | 1 - .../selftests/kvm/include/x86_64/processor.h | 3 - .../selftests/kvm/x86_64/mmu_role_test.c | 137 ------------------ 4 files changed, 142 deletions(-) delete mode 100644 tools/testing/selftests/kvm/x86_64/mmu_role_test.c
tools/testing/selftests/kvm/.gitignore +0 -1
@@ -27,7 +27,6 @@ /x86_64/hyperv_svm_test /x86_64/max_vcpuid_cap_test /x86_64/mmio_warning_test -/x86_64/mmu_role_test /x86_64/platform_info_test /x86_64/pmu_event_filter_test /x86_64/set_boot_cpu_id
tools/testing/selftests/kvm/Makefile +0 -1
@@ -83,7 +83,6 @@ TEST_GEN_PROGS_x86_64 += x86_64/hyperv_svm_test TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test -TEST_GEN_PROGS_x86_64 += x86_64/mmu_role_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id
tools/testing/selftests/kvm/include/x86_64/processor.h +0 -3
@@ -140,9 +140,6 @@ struct kvm_x86_cpu_feature {#define CPUID_XSAVE (1ul << 26)#define CPUID_OSXSAVE (1ul << 27) -/* CPUID.0x8000_0001.EDX */ -#define CPUID_GBPAGES (1ul << 26) -/* CPUID.0x8000_000A.EDX */ #define CPUID_NRIPS BIT(3)
tools/testing/selftests/kvm/x86_64/mmu_role_test.c deleted +0 -137
137 lines not on this page · raw message
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 14/42 KVM: selftests: Use kvm_cpu_has() for KVM's PV steal time
In reply to Sean ChristophersonUse kvm_cpu_has() in the stea-ltime test instead of open coding equivalent functionality using kvm_get_supported_cpuid_entry().
Opportunistically define all of KVM's paravirt CPUID-based features.
No functional change intended.
--- .../selftests/kvm/include/x86_64/processor.h | 22 +++++++++++++++++++ tools/testing/selftests/kvm/steal_time.c | 4 +--- 2 files changed, 23 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +22 -0
@@ -135,6 +135,28 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)#define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3) +/* +* KVM defined paravirt features. +*/ +#define X86_FEATURE_KVM_CLOCKSOURCE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 0) +#define X86_FEATURE_KVM_NOP_IO_DELAY KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 1) +#define X86_FEATURE_KVM_MMU_OP KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 2) +#define X86_FEATURE_KVM_CLOCKSOURCE2 KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 3) +#define X86_FEATURE_KVM_ASYNC_PF KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 4) +#define X86_FEATURE_KVM_STEAL_TIME KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 5) +#define X86_FEATURE_KVM_PV_EOI KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 6) +#define X86_FEATURE_KVM_PV_UNHALT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 7) +/* Bit 8 apparently isn't used?!?! */ +#define X86_FEATURE_KVM_PV_TLB_FLUSH KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 9) +#define X86_FEATURE_KVM_ASYNC_PF_VMEXIT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 10) +#define X86_FEATURE_KVM_PV_SEND_IPI KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 11) +#define X86_FEATURE_KVM_POLL_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 12) +#define X86_FEATURE_KVM_PV_SCHED_YIELD KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 13) +#define X86_FEATURE_KVM_ASYNC_PF_INT KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 14) +#define X86_FEATURE_KVM_MSI_EXT_DEST_ID KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 15) +#define X86_FEATURE_KVM_HC_MAP_GPA_RANGE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 16) +#define X86_FEATURE_KVM_MIGRATION_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 17) +/* CPUID.1.ECX */ #define CPUID_VMX (1ul << 5)#define CPUID_XSAVE (1ul << 26)
tools/testing/selftests/kvm/steal_time.c +1 -3
@@ -60,9 +60,7 @@ static void guest_code(int cpu)static bool is_steal_time_supported (struct kvm_vcpu *vcpu) { -struct kvm_cpuid_entry2 *cpuid =kvm_get_supported_cpuid_entry (KVM_CPUID_FEATURES ); - -return cpuid && (cpuid->eax &KVM_FEATURE_STEAL_TIME ); +return kvm_cpu_has (X86_FEATURE_KVM_STEAL_TIME ); }static void steal_time_init (struct kvm_vcpu *vcpu,uint32_t i)
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 15/42 KVM: selftests: Use kvm_cpu_has() for nSVM soft INT injection test
In reply to Sean ChristophersonUse kvm_cpu_has() to query for NRIPS support instead of open coding equivalent functionality using kvm_get_supported_cpuid_entry().
--- tools/testing/selftests/kvm/include/x86_64/processor.h | 3 --- .../selftests/kvm/x86_64/svm_nested_soft_inject_test.c | 7 ++----- 2 files changed, 2 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +0 -3
@@ -162,9 +162,6 @@ struct kvm_x86_cpu_feature {#define CPUID_XSAVE (1ul << 26)#define CPUID_OSXSAVE (1ul << 27) -/* CPUID.0x8000_000A.EDX */ -#define CPUID_NRIPS BIT(3) -/* Page table bitfield declarations */ #define PTE_PRESENT_MASK BIT_ULL(0)#define PTE_WRITABLE_MASK BIT_ULL(1)
tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c +2 -5
@@ -195,16 +195,13 @@ static void run_test(bool is_nmi)int main (int argc,char *argv[]) { -struct kvm_cpuid_entry2 *cpuid; -/* Tell stdout not to buffer its content */ setbuf (stdout,NULL );TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); - cpuid =kvm_get_supported_cpuid_entry (0x8000000a ); -TEST_ASSERT (cpuid->edx &CPUID_NRIPS , -"KVM with nSVM is supposed to unconditionally advertise nRIP Save\n" ); +TEST_ASSERT (kvm_cpu_has (X86_FEATURE_NRIPS ), +"KVM with nSVM is supposed to unconditionally advertise nRIP Save" );atomic_init (&nmi_stage,0 );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 16/42 KVM: selftests: Verify that kvm_cpuid2.entries layout is unchanged by KVM
In reply to Sean ChristophersonIn the CPUID test, verify that KVM doesn't modify the kvm_cpuid2.entries layout, i.e. that the order of entries and their flags is identical between what the test provides via KVM_SET_CPUID2 and what KVM returns via KVM_GET_CPUID2.
Asserting that the layouts match simplifies the test as there's no need to iterate over both arrays.
--- .../testing/selftests/kvm/x86_64/cpuid_test.c | 53 ++++++++----------- 1 file changed, 23 insertions(+), 30 deletions(-)
tools/testing/selftests/kvm/x86_64/cpuid_test.c +23 -30
@@ -79,41 +79,34 @@ static bool is_cpuid_mangled(struct kvm_cpuid_entry2 *entrie)return false; } -static void check_cpuid (struct kvm_cpuid2 *cpuid,struct kvm_cpuid_entry2 *entrie) -{ -int i; - -for (i =0 ; i < cpuid->nent; i++) { -if (cpuid->entries[i].function == entrie->function && - cpuid->entries[i].index == entrie->index) { -if (is_cpuid_mangled (entrie)) -return ; - -TEST_ASSERT (cpuid->entries[i].eax == entrie->eax && - cpuid->entries[i].ebx == entrie->ebx && - cpuid->entries[i].ecx == entrie->ecx && - cpuid->entries[i].edx == entrie->edx, -"CPUID 0x%x.%x differ: 0x%x:0x%x:0x%x:0x%x vs 0x%x:0x%x:0x%x:0x%x" , - entrie->function, entrie->index, - cpuid->entries[i].eax, cpuid->entries[i].ebx, - cpuid->entries[i].ecx, cpuid->entries[i].edx, - entrie->eax, entrie->ebx, entrie->ecx, entrie->edx); -return ; - } - } - -TEST_ASSERT (false,"CPUID 0x%x.%x not found" , entrie->function, entrie->index); -} -static void compare_cpuids (struct kvm_cpuid2 *cpuid1,struct kvm_cpuid2 *cpuid2) { +struct kvm_cpuid_entry2 *e1, *e2;int i; -for (i =0 ; i < cpuid1->nent; i++) -check_cpuid (cpuid2, &cpuid1->entries[i]); +TEST_ASSERT (cpuid1->nent == cpuid2->nent, +"CPUID nent mismatch: %d vs. %d" , cpuid1->nent, cpuid2->nent); -for (i =0 ; i < cpuid2->nent; i++) -check_cpuid (cpuid1, &cpuid2->entries[i]); +for (i =0 ; i < cpuid1->nent; i++) { + e1 = &cpuid1->entries[i]; + e2 = &cpuid2->entries[i]; + +TEST_ASSERT (e1->function == e2->function && + e1->index == e2->index && e1->flags == e2->flags, +"CPUID entries[%d] mismtach: 0x%x.%d.%x vs. 0x%x.%d.%x\n" , + i, e1->function, e1->index, e1->flags, + e2->function, e2->index, e2->flags); + +if (is_cpuid_mangled (e1)) +continue ; + +TEST_ASSERT (e1->eax == e2->eax && e1->ebx == e2->ebx && + e1->ecx == e2->ecx && e1->edx == e2->edx, +"CPUID 0x%x.%x differ: 0x%x:0x%x:0x%x:0x%x vs 0x%x:0x%x:0x%x:0x%x" , + e1->function, e1->index, + e1->eax, e1->ebx, e1->ecx, e1->edx, + e2->eax, e2->ebx, e2->ecx, e2->edx); + } }static void run_vcpu (struct kvm_vcpu *vcpu,int stage)
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 17/42 KVM: selftests: Split out kvm_cpuid2_size() from allocate_kvm_cpuid2()
In reply to Sean ChristophersonSplit out the computation of the effective size of a kvm_cpuid2 struct from allocate_kvm_cpuid2(), and modify both to take an arbitrary number of entries. Future commits will add caching of a vCPU's CPUID model, and will (a) be able to precisely size the entries array, and (b) will need to know the effective size of the struct in order to copy to/from the cache.
Expose the helpers so that the Hyper-V Features test can use them in the (somewhat distant) future. The Hyper-V test very, very subtly relies on propagating CPUID info across vCPU instances, and will need to make a copy of the previous vCPU's CPUID information when it switches to using the per-vCPU cache. Alternatively, KVM could provide helpers to duplicate and/or copy a kvm_cpuid2 instance, but each is literally a single line of code if the helpers are exposed, and it's not like the size of kvm_cpuid2 is secret knowledge.
--- .../selftests/kvm/include/x86_64/processor.h | 23 +++++++++ .../selftests/kvm/lib/x86_64/processor.c | 48 +++---------------- 2 files changed, 30 insertions(+), 41 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +23 -0
@@ -593,6 +593,29 @@ static inline bool kvm_cpu_has(struct kvm_x86_cpu_feature feature)return kvm_cpuid_has (kvm_get_supported_cpuid (), feature); } +static inline size_t kvm_cpuid2_size (int nr_entries) +{ +return sizeof (struct kvm_cpuid2 ) + +sizeof (struct kvm_cpuid_entry2 ) * nr_entries; +} + +/* +* Allocate a "struct kvm_cpuid2* instance, with the 0-length arrary of +* entries sized to hold @nr_entries. The caller is responsible for freeing +* the struct. +*/ +static inline struct kvm_cpuid2 *allocate_kvm_cpuid2 (int nr_entries) +{ +struct kvm_cpuid2 *cpuid; + + cpuid =malloc (kvm_cpuid2_size (nr_entries)); +TEST_ASSERT (cpuid,"-ENOMEM when allocating kvm_cpuid2" ); + + cpuid->nent = nr_entries; + +return cpuid; +} +struct kvm_cpuid2 *vcpu_get_cpuid (struct kvm_vcpu *vcpu); static inline int __vcpu_set_cpuid(struct kvm_vcpu *vcpu,
tools/testing/selftests/kvm/lib/x86_64/processor.c +7 -41
90 lines not on this page · raw message
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 18/42 KVM: selftests: Cache CPUID in struct kvm_vcpu
In reply to Sean ChristophersonCache a vCPU's CPUID information in "struct kvm_vcpu" to allow fixing the mess where tests, often unknowingly, modify the global/static "cpuid" allocated by kvm_get_supported_cpuid().
Add vcpu_init_cpuid() to handle stuffing an entirely different CPUID model, e.g. during vCPU creation or when switching to the Hyper-V enabled CPUID model. Automatically refresh the cache on vcpu_set_cpuid() so that any adjustments made by KVM are always reflected in the cache. Drop vcpu_get_cpuid() entirely to force tests to use the cache, and to allow adding e.g. vcpu_get_cpuid_entry() in the future without creating a conflicting set of APIs where vcpu_get_cpuid() does KVM_GET_CPUID2, but vcpu_get_cpuid_entry() does not.
Opportunistically convert the VMX nested state test and KVM PV test to manipulating the vCPU's CPUID (because it's easy), but use vcpu_init_cpuid() for the Hyper-V features test and "emulator error" test to effectively retain their current behavior as they're less trivial to convert.
--- .../selftests/kvm/include/kvm_util_base.h | 5 +++ .../selftests/kvm/include/x86_64/processor.h | 25 +++++++---- tools/testing/selftests/kvm/lib/kvm_util.c | 7 ++++ .../selftests/kvm/lib/x86_64/processor.c | 42 +++++++++---------- .../testing/selftests/kvm/x86_64/cpuid_test.c | 18 ++++---- .../kvm/x86_64/emulator_error_test.c | 2 +- .../selftests/kvm/x86_64/hyperv_features.c | 2 +- .../selftests/kvm/x86_64/kvm_pv_test.c | 6 +-- .../kvm/x86_64/vmx_set_nested_state_test.c | 6 +-- .../selftests/kvm/x86_64/xapic_state_test.c | 4 +- 10 files changed, 68 insertions(+), 49 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +5 -0
@@ -50,6 +50,9 @@ struct kvm_vcpu {int fd;struct kvm_vm *vm;struct kvm_run *run; +#ifdef __x86_64__ +struct kvm_cpuid2 *cpuid; +#endif struct kvm_dirty_gfn *dirty_gfns;uint32_t fetch_index;uint32_t dirty_gfns_count;
@@ -690,6 +693,8 @@ static inline struct kvm_vcpu *vm_vcpu_recreate(struct kvm_vm *vm,return vm_arch_vcpu_recreate (vm, vcpu_id); } +void vcpu_arch_free (struct kvm_vcpu *vcpu); +void virt_arch_pgd_alloc (struct kvm_vm *vm);static inline void virt_pgd_alloc (struct kvm_vm *vm)
tools/testing/selftests/kvm/include/x86_64/processor.h +18 -7
@@ -616,18 +616,29 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)return cpuid; } -struct kvm_cpuid2 *vcpu_get_cpuid (struct kvm_vcpu *vcpu); +void vcpu_init_cpuid (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid); -static inline int __vcpu_set_cpuid (struct kvm_vcpu *vcpu, -struct kvm_cpuid2 *cpuid) +static inline int __vcpu_set_cpuid (struct kvm_vcpu *vcpu) { -return __vcpu_ioctl (vcpu,KVM_SET_CPUID2 , cpuid); +int r; + +TEST_ASSERT (vcpu->cpuid,"Must do vcpu_init_cpuid() first" ); + r =__vcpu_ioctl (vcpu,KVM_SET_CPUID2 , vcpu->cpuid); +if (r) +return r; + +/* On success, refresh the cache to pick up adjustments made by KVM. */ +vcpu_ioctl (vcpu,KVM_GET_CPUID2 , vcpu->cpuid); +return 0 ; } -static inline void vcpu_set_cpuid (struct kvm_vcpu *vcpu, -struct kvm_cpuid2 *cpuid) +static inline void vcpu_set_cpuid (struct kvm_vcpu *vcpu) { -vcpu_ioctl (vcpu,KVM_SET_CPUID2 , cpuid); +TEST_ASSERT (vcpu->cpuid,"Must do vcpu_init_cpuid() first" ); +vcpu_ioctl (vcpu,KVM_SET_CPUID2 , vcpu->cpuid); + +/* Refresh the cache to pick up adjustments made by KVM. */ +vcpu_ioctl (vcpu,KVM_GET_CPUID2 , vcpu->cpuid); } struct kvm_cpuid_entry2 *
tools/testing/selftests/kvm/lib/kvm_util.c +7 -0
@@ -476,6 +476,11 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,return ®ion->region; } +__weak void vcpu_arch_free (struct kvm_vcpu *vcpu) +{ + +} +/* * VM VCPU Remove *
@@ -505,6 +510,8 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu)TEST_ASSERT (!ret,__KVM_SYSCALL_ERROR ("close()" , ret));list_del (&vcpu->list); + +vcpu_arch_free (vcpu);free (vcpu); }
tools/testing/selftests/kvm/lib/x86_64/processor.c +20 -22
76 lines not on this page · raw message
tools/testing/selftests/kvm/x86_64/cpuid_test.c +9 -9
@@ -145,21 +145,22 @@ struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, structreturn guest_cpuids; } -static void set_cpuid_after_run (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid) +static void set_cpuid_after_run (struct kvm_vcpu *vcpu) { +struct kvm_cpuid2 *cpuid = vcpu->cpuid;struct kvm_cpuid_entry2 *ent;int rc;u32 eax, ebx, x;/* Setting unmodified CPUID is allowed */ - rc =__vcpu_set_cpuid (vcpu, cpuid); + rc =__vcpu_set_cpuid (vcpu);TEST_ASSERT (!rc,"Setting unmodified CPUID after KVM_RUN failed: %d" , rc);/* Changing CPU features is forbidden */ ent =get_cpuid (cpuid,0x7 ,0 ); ebx = ent->ebx; ent->ebx--; - rc =__vcpu_set_cpuid (vcpu, cpuid); + rc =__vcpu_set_cpuid (vcpu);TEST_ASSERT (rc,"Changing CPU features should fail" ); ent->ebx = ebx;
@@ -168,14 +169,14 @@ static void set_cpuid_after_run(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid) eax = ent->eax; x = eax &0xff ; ent->eax = (eax & ~0xffu ) | (x -1 ); - rc =__vcpu_set_cpuid (vcpu, cpuid); + rc =__vcpu_set_cpuid (vcpu);TEST_ASSERT (rc,"Changing MAXPHYADDR should fail" ); ent->eax = eax; }int main (void ) { -struct kvm_cpuid2 *supp_cpuid, *cpuid2; +struct kvm_cpuid2 *supp_cpuid;struct kvm_vcpu *vcpu;vm_vaddr_t cpuid_gva;struct kvm_vm *vm;
@@ -184,18 +185,17 @@ int main(void) vm =vm_create_with_one_vcpu (&vcpu, guest_main); supp_cpuid =kvm_get_supported_cpuid (); - cpuid2 =vcpu_get_cpuid (vcpu); -compare_cpuids (supp_cpuid, cpuid2); +compare_cpuids (supp_cpuid, vcpu->cpuid); -vcpu_alloc_cpuid (vm, &cpuid_gva, cpuid2); +vcpu_alloc_cpuid (vm, &cpuid_gva, vcpu->cpuid);vcpu_args_set (vcpu,1 , cpuid_gva);for (stage =0 ; stage <3 ; stage++)run_vcpu (vcpu, stage); -set_cpuid_after_run (vcpu, cpuid2); +set_cpuid_after_run (vcpu);kvm_vm_free (vm); }
tools/testing/selftests/kvm/x86_64/emulator_error_test.c +1 -1
@@ -172,7 +172,7 @@ int main(int argc, char *argv[]) entry->eax = (entry->eax &0xffffff00 ) |MAXPHYADDR ;set_cpuid (cpuid, entry); -vcpu_set_cpuid (vcpu, cpuid); +vcpu_init_cpuid (vcpu, cpuid); rc =kvm_check_cap (KVM_CAP_EXIT_ON_EMULATION_FAILURE );TEST_ASSERT (rc,"KVM_CAP_EXIT_ON_EMULATION_FAILURE is unavailable" );
tools/testing/selftests/kvm/x86_64/hyperv_features.c +1 -1
@@ -161,7 +161,7 @@ static void hv_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, "failed to set HYPERV_CPUID_ENLIGHTMENT_INFO leaf");TEST_ASSERT (set_cpuid (cpuid, dbg),"failed to set HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES leaf" ); -vcpu_set_cpuid (vcpu, cpuid); +vcpu_init_cpuid (vcpu, cpuid); }static void guest_test_msrs_access (void )
tools/testing/selftests/kvm/x86_64/kvm_pv_test.c +2 -4
@@ -200,7 +200,6 @@ static void enter_guest(struct kvm_vcpu *vcpu)int main (void ) { -struct kvm_cpuid2 *best;struct kvm_vcpu *vcpu;struct kvm_vm *vm;
@@ -210,9 +209,8 @@ int main(void)vcpu_enable_cap (vcpu,KVM_CAP_ENFORCE_PV_FEATURE_CPUID ,1 ); - best =kvm_get_supported_cpuid (); -clear_kvm_cpuid_features (best); -vcpu_set_cpuid (vcpu, best); +clear_kvm_cpuid_features (vcpu->cpuid); +vcpu_set_cpuid (vcpu);vm_init_descriptor_tables (vm);vcpu_init_descriptor_tables (vcpu);
tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c +3 -3
@@ -121,7 +121,7 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)test_nested_state (vcpu, state);/* Enable VMX in the guest CPUID. */ -vcpu_set_cpuid (vcpu,kvm_get_supported_cpuid ()); +vcpu_set_cpuid (vcpu);/* * Setting vmxon_pa == -1ull and vmcs_pa == -1ull exits early without
@@ -245,7 +245,7 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)void disable_vmx (struct kvm_vcpu *vcpu) { -struct kvm_cpuid2 *cpuid =kvm_get_supported_cpuid (); +struct kvm_cpuid2 *cpuid = vcpu->cpuid;int i;for (i =0 ; i < cpuid->nent; ++i)
@@ -255,7 +255,7 @@ void disable_vmx(struct kvm_vcpu *vcpu)TEST_ASSERT (i != cpuid->nent,"CPUID function 1 not found" ); cpuid->entries[i].ecx &= ~CPUID_VMX ; -vcpu_set_cpuid (vcpu, cpuid); +vcpu_set_cpuid (vcpu); cpuid->entries[i].ecx |=CPUID_VMX ; }
tools/testing/selftests/kvm/x86_64/xapic_state_test.c +2 -2
@@ -138,13 +138,13 @@ int main(int argc, char *argv[]) vm =vm_create_with_one_vcpu (&x.vcpu, xapic_guest_code); x.is_x2apic = false; - cpuid =vcpu_get_cpuid (x.vcpu); + cpuid = x.vcpu->cpuid;for (i =0 ; i < cpuid->nent; i++) {if (cpuid->entries[i].function ==1 )break ; } cpuid->entries[i].ecx &= ~BIT (21 ); -vcpu_set_cpuid (x.vcpu, cpuid); +vcpu_set_cpuid (x.vcpu);virt_pg_map (vm,APIC_DEFAULT_GPA ,APIC_DEFAULT_GPA );test_icr (&x);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 19/42 KVM: selftests: Don't use a static local in vcpu_get_supported_hv_cpuid()
In reply to Sean ChristophersonDon't use a static variable for the Hyper-V supported CPUID array, the helper unconditionally reallocates the array on every invocation (and all callers free the array immediately after use). The array is intentionally recreated and refilled because the set of supported CPUID features is dependent on vCPU state, e.g. whether or not eVMCS has been enabled.
--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +1 -3
@@ -1288,9 +1288,7 @@ void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu)struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid (struct kvm_vcpu *vcpu) { -static struct kvm_cpuid2 *cpuid; - - cpuid =allocate_kvm_cpuid2 (MAX_NR_CPUID_ENTRIES ); +struct kvm_cpuid2 *cpuid =allocate_kvm_cpuid2 (MAX_NR_CPUID_ENTRIES );vcpu_ioctl (vcpu,KVM_GET_SUPPORTED_HV_CPUID , cpuid);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 20/42 KVM: selftests: Rename and tweak get_cpuid() to get_cpuid_entry()
In reply to Sean ChristophersonRename get_cpuid() to get_cpuid_entry() to better reflect its behavior. Leave set_cpuid() as is to avoid unnecessary churn, that helper will soon be removed entirely.
Oppurtunistically tweak the implementation to avoid using a temporary variable in anticipation of taggin the input @cpuid with "const".
No functional change intended.
--- .../testing/selftests/kvm/include/x86_64/processor.h | 4 ++-- tools/testing/selftests/kvm/lib/x86_64/processor.c | 11 +++++------ tools/testing/selftests/kvm/x86_64/cpuid_test.c | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +2 -2
@@ -692,8 +692,8 @@ void vm_set_page_table_entry(struct kvm_vm *vm, struct kvm_vcpu *vcpu,/* * get_cpuid() - find matching CPUID entry and return pointer to it. */ -struct kvm_cpuid_entry2 *get_cpuid (struct kvm_cpuid2 *cpuid,uint32_t function, -uint32_t index); +struct kvm_cpuid_entry2 *get_cpuid_entry (struct kvm_cpuid2 *cpuid, +uint32_t function,uint32_t index);/* * set_cpuid() - overwrites a matching cpuid entry with the provided value. * matches based on ent->function && ent->index. returns true
tools/testing/selftests/kvm/lib/x86_64/processor.c +5 -6
@@ -1190,16 +1190,15 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu) } } -struct kvm_cpuid_entry2 *get_cpuid (struct kvm_cpuid2 *cpuid,uint32_t function, -uint32_t index) +struct kvm_cpuid_entry2 *get_cpuid_entry (struct kvm_cpuid2 *cpuid, +uint32_t function,uint32_t index) {int i;for (i =0 ; i < cpuid->nent; i++) { -struct kvm_cpuid_entry2 *cur = &cpuid->entries[i]; - -if (cur->function == function && cur->index == index) -return cur; +if (cpuid->entries[i].function == function && + cpuid->entries[i].index == index) +return &cpuid->entries[i]; }TEST_FAIL ("CPUID function 0x%x index 0x%x not found " , function, index);
tools/testing/selftests/kvm/x86_64/cpuid_test.c +2 -2
@@ -157,7 +157,7 @@ static void set_cpuid_after_run(struct kvm_vcpu *vcpu)TEST_ASSERT (!rc,"Setting unmodified CPUID after KVM_RUN failed: %d" , rc);/* Changing CPU features is forbidden */ - ent =get_cpuid (cpuid,0x7 ,0 ); + ent =get_cpuid_entry (cpuid,0x7 ,0 ); ebx = ent->ebx; ent->ebx--; rc =__vcpu_set_cpuid (vcpu);
@@ -165,7 +165,7 @@ static void set_cpuid_after_run(struct kvm_vcpu *vcpu) ent->ebx = ebx;/* Changing MAXPHYADDR is forbidden */ - ent =get_cpuid (cpuid,0x80000008 ,0 ); + ent =get_cpuid_entry (cpuid,0x80000008 ,0 ); eax = ent->eax; x = eax &0xff ; ent->eax = (eax & ~0xffu ) | (x -1 );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 21/42 KVM: selftests: Use get_cpuid_entry() in kvm_get_supported_cpuid_index()
In reply to Sean ChristophersonUse get_cpuid_entry() in kvm_get_supported_cpuid_index() to replace functionally identical code.
--- .../selftests/kvm/include/x86_64/processor.h | 14 ++++---- .../selftests/kvm/lib/x86_64/processor.c | 32 ------------------- 2 files changed, 7 insertions(+), 39 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +7 -7
@@ -616,6 +616,8 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)return cpuid; } +struct kvm_cpuid_entry2 *get_cpuid_entry (struct kvm_cpuid2 *cpuid, +uint32_t function,uint32_t index);void vcpu_init_cpuid (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid);static inline int __vcpu_set_cpuid (struct kvm_vcpu *vcpu)
@@ -641,8 +643,11 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu)vcpu_ioctl (vcpu,KVM_GET_CPUID2 , vcpu->cpuid); } -struct kvm_cpuid_entry2 * -kvm_get_supported_cpuid_index (uint32_t function,uint32_t index); +static inline struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_index (uint32_t function, +uint32_t index) +{ +return get_cpuid_entry (kvm_get_supported_cpuid (), function, index); +}static inline struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_entry (uint32_t function)
@@ -689,11 +694,6 @@ uint64_t vm_get_page_table_entry(struct kvm_vm *vm, struct kvm_vcpu *vcpu,void vm_set_page_table_entry (struct kvm_vm *vm,struct kvm_vcpu *vcpu,uint64_t vaddr,uint64_t pte); -/* -* get_cpuid() - find matching CPUID entry and return pointer to it. -*/ -struct kvm_cpuid_entry2 *get_cpuid_entry (struct kvm_cpuid2 *cpuid, -uint32_t function,uint32_t index);/* * set_cpuid() - overwrites a matching cpuid entry with the provided value. * matches based on ent->function && ent->index. returns true
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -32
@@ -766,38 +766,6 @@ void vcpu_init_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid)vcpu_set_cpuid (vcpu); } -/* -* Locate a cpuid entry. -* -* Input Args: -* function: The function of the cpuid entry to find. -* index: The index of the cpuid entry. -* -* Output Args: None -* -* Return: A pointer to the cpuid entry. Never returns NULL. -*/ -struct kvm_cpuid_entry2 * -kvm_get_supported_cpuid_index (uint32_t function,uint32_t index) -{ -struct kvm_cpuid2 *cpuid; -struct kvm_cpuid_entry2 *entry =NULL ; -int i; - - cpuid =kvm_get_supported_cpuid (); -for (i =0 ; i < cpuid->nent; i++) { -if (cpuid->entries[i].function == function && - cpuid->entries[i].index == index) { - entry = &cpuid->entries[i]; -break ; - } - } - -TEST_ASSERT (entry,"Guest CPUID entry not found: (EAX=%x, ECX=%x)." , - function, index); -return entry; -} -uint64_t vcpu_get_msr (struct kvm_vcpu *vcpu,uint64_t msr_index) {struct {
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 22/42 KVM: selftests: Add helpers to get and modify a vCPU's CPUID entries
In reply to Sean ChristophersonAdd helpers to get a specific CPUID entry for a given vCPU, and to toggle a specific CPUID-based feature for a vCPU. The helpers will reduce the amount of boilerplate code needed to tweak a vCPU's CPUID model, improve code clarity, and most importantly move tests away from modifying the static "cpuid" returned by kvm_get_supported_cpuid().
--- .../selftests/kvm/include/x86_64/processor.h | 30 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 18 +++++++++++ 2 files changed, 48 insertions(+)
tools/testing/selftests/kvm/include/x86_64/processor.h +30 -0
@@ -620,6 +620,19 @@ struct kvm_cpuid_entry2 *get_cpuid_entry(struct kvm_cpuid2 *cpuid, uint32_t function, uint32_t index);void vcpu_init_cpuid (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid); +static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry (struct kvm_vcpu *vcpu, +uint32_t function, +uint32_t index) +{ +return get_cpuid_entry (vcpu->cpuid, function, index); +} + +static inline struct kvm_cpuid_entry2 *vcpu_get_cpuid_entry (struct kvm_vcpu *vcpu, +uint32_t function) +{ +return __vcpu_get_cpuid_entry (vcpu, function,0 ); +} +static inline int __vcpu_set_cpuid (struct kvm_vcpu *vcpu) {int r;
@@ -643,6 +656,23 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu)vcpu_ioctl (vcpu,KVM_GET_CPUID2 , vcpu->cpuid); } +void vcpu_set_or_clear_cpuid_feature (struct kvm_vcpu *vcpu, +struct kvm_x86_cpu_feature feature, +bool set); + +static inline void vcpu_set_cpuid_feature (struct kvm_vcpu *vcpu, +struct kvm_x86_cpu_feature feature) +{ +vcpu_set_or_clear_cpuid_feature (vcpu, feature, true); + +} + +static inline void vcpu_clear_cpuid_feature (struct kvm_vcpu *vcpu, +struct kvm_x86_cpu_feature feature) +{ +vcpu_set_or_clear_cpuid_feature (vcpu, feature, false); +} +static inline struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_index (uint32_t function,uint32_t index) {
tools/testing/selftests/kvm/lib/x86_64/processor.c +18 -0
@@ -766,6 +766,24 @@ void vcpu_init_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid)vcpu_set_cpuid (vcpu); } +void vcpu_set_or_clear_cpuid_feature (struct kvm_vcpu *vcpu, +struct kvm_x86_cpu_feature feature, +bool set) +{ +struct kvm_cpuid_entry2 *entry; +u32 *reg; + + entry =__vcpu_get_cpuid_entry (vcpu, feature.function, feature.index); + reg = (&entry->eax) + feature.reg; + +if (set) + *reg |=BIT (feature.bit); +else + *reg &= ~BIT (feature.bit); + +vcpu_set_cpuid (vcpu); +} +uint64_t vcpu_get_msr (struct kvm_vcpu *vcpu,uint64_t msr_index) {struct {
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 23/42 KVM: selftests: Use vm->pa_bits to generate reserved PA bits
In reply to Sean ChristophersonUse vm->pa_bits to generate the mask of physical address bits that are reserved in page table entries. vm->pa_bits is set when the VM is created, i.e. it's guaranteed to be valid when populating page tables.
--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +2 -6
@@ -221,16 +221,12 @@ static uint64_t *_vm_get_page_table_entry(struct kvm_vm *vm,uint16_t index[4 ];uint64_t *pml4e, *pdpe, *pde;uint64_t *pte; -struct kvm_cpuid_entry2 *entry;struct kvm_sregs sregs; -int max_phy_addr;uint64_t rsvd_mask =0 ; - entry =kvm_get_supported_cpuid_index (0x80000008 ,0 ); - max_phy_addr = entry->eax &0x000000ff ;/* Set the high bits in the reserved mask. */ -if (max_phy_addr <52 ) - rsvd_mask =GENMASK_ULL (51 , max_phy_addr); +if (vm->pa_bits <52 ) + rsvd_mask =GENMASK_ULL (51 , vm->pa_bits);/* * SDM vol 3, fig 4-11 "Formats of CR3 and Paging-Structure Entries
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 24/42 KVM: selftests: Add and use helper to set vCPU's CPUID maxphyaddr
In reply to Sean ChristophersonAdd a helper to set a vCPU's guest.MAXPHYADDR, and use it in the test that verifies the emulator returns an error on an unknown instruction when KVM emulates in response to an EPT violation with a GPA that is legal in hardware but illegal with respect to the guest's MAXPHYADDR.
Add a helper even though there's only a single user at this time. Before its removal, mmu_role_test also stuffed guest.MAXPHYADDR, and the helper provides a small amount of clarity.
More importantly, this eliminates a set_cpuid() user and an instance of modifying kvm_get_supported_cpuid()'s static "cpuid".
--- tools/testing/selftests/kvm/include/x86_64/processor.h | 2 ++ tools/testing/selftests/kvm/lib/x86_64/processor.c | 8 ++++++++ .../testing/selftests/kvm/x86_64/emulator_error_test.c | 10 +--------- 3 files changed, 11 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +2 -0
@@ -656,6 +656,8 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu)vcpu_ioctl (vcpu,KVM_GET_CPUID2 , vcpu->cpuid); } +void vcpu_set_cpuid_maxphyaddr (struct kvm_vcpu *vcpu,uint8_t maxphyaddr); +void vcpu_set_or_clear_cpuid_feature (struct kvm_vcpu *vcpu,struct kvm_x86_cpu_feature feature,bool set);
tools/testing/selftests/kvm/lib/x86_64/processor.c +8 -0
@@ -762,6 +762,14 @@ void vcpu_init_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid)vcpu_set_cpuid (vcpu); } +void vcpu_set_cpuid_maxphyaddr (struct kvm_vcpu *vcpu,uint8_t maxphyaddr) +{ +struct kvm_cpuid_entry2 *entry =vcpu_get_cpuid_entry (vcpu,0x80000008 ); + + entry->eax = (entry->eax & ~0xff ) | maxphyaddr; +vcpu_set_cpuid (vcpu); +} +void vcpu_set_or_clear_cpuid_feature (struct kvm_vcpu *vcpu,struct kvm_x86_cpu_feature feature,bool set)
tools/testing/selftests/kvm/x86_64/emulator_error_test.c +1 -9
@@ -151,8 +151,6 @@ static uint64_t process_ucall(struct kvm_vcpu *vcpu)int main (int argc,char *argv[]) { -struct kvm_cpuid_entry2 *entry; -struct kvm_cpuid2 *cpuid;struct kvm_vcpu *vcpu;struct kvm_vm *vm;uint64_t gpa, pte;
@@ -166,13 +164,7 @@ int main(int argc, char *argv[]) vm =vm_create_with_one_vcpu (&vcpu, guest_code); - cpuid =kvm_get_supported_cpuid (); - - entry =kvm_get_supported_cpuid_index (0x80000008 ,0 ); - entry->eax = (entry->eax &0xffffff00 ) |MAXPHYADDR ; -set_cpuid (cpuid, entry); - -vcpu_init_cpuid (vcpu, cpuid); +vcpu_set_cpuid_maxphyaddr (vcpu,MAXPHYADDR ); rc =kvm_check_cap (KVM_CAP_EXIT_ON_EMULATION_FAILURE );TEST_ASSERT (rc,"KVM_CAP_EXIT_ON_EMULATION_FAILURE is unavailable" );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 25/42 KVM: selftests: Use vcpu_get_cpuid_entry() in PV features test (sort of)
In reply to Sean ChristophersonAdd a new helper, vcpu_clear_cpuid_entry(), to do a RMW operation on the vCPU's CPUID model to clear a given CPUID entry, and use it to clear KVM's paravirt feature instead of operating on kvm_get_supported_cpuid()'s static "cpuid" variable. This also eliminates a user of
the soon-be-defunct set_cpuid() helper.
--- .../testing/selftests/kvm/include/x86_64/processor.h | 1 + tools/testing/selftests/kvm/lib/x86_64/processor.c | 11 +++++++++++ tools/testing/selftests/kvm/x86_64/kvm_pv_test.c | 12 +----------- 3 files changed, 13 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -658,6 +658,7 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu)void vcpu_set_cpuid_maxphyaddr (struct kvm_vcpu *vcpu,uint8_t maxphyaddr); +void vcpu_clear_cpuid_entry (struct kvm_vcpu *vcpu,uint32_t function);void vcpu_set_or_clear_cpuid_feature (struct kvm_vcpu *vcpu,struct kvm_x86_cpu_feature feature,bool set);
tools/testing/selftests/kvm/lib/x86_64/processor.c +11 -0
@@ -770,6 +770,17 @@ void vcpu_set_cpuid_maxphyaddr(struct kvm_vcpu *vcpu, uint8_t maxphyaddr)vcpu_set_cpuid (vcpu); } +void vcpu_clear_cpuid_entry (struct kvm_vcpu *vcpu,uint32_t function) +{ +struct kvm_cpuid_entry2 *entry =vcpu_get_cpuid_entry (vcpu, function); + + entry->eax =0 ; + entry->ebx =0 ; + entry->ecx =0 ; + entry->edx =0 ; +vcpu_set_cpuid (vcpu); +} +void vcpu_set_or_clear_cpuid_feature (struct kvm_vcpu *vcpu,struct kvm_x86_cpu_feature feature,bool set)
tools/testing/selftests/kvm/x86_64/kvm_pv_test.c +1 -11
@@ -142,15 +142,6 @@ static void guest_main(void)GUEST_DONE (); } -static void clear_kvm_cpuid_features (struct kvm_cpuid2 *cpuid) -{ -struct kvm_cpuid_entry2 ent = {0 }; - - ent.function =KVM_CPUID_FEATURES ; -TEST_ASSERT (set_cpuid (cpuid, &ent), -"failed to clear KVM_CPUID_FEATURES leaf" ); -} -static void pr_msr (struct ucall *uc) {struct msr_data *msr = (struct msr_data *)uc->args[0 ];
@@ -209,8 +200,7 @@ int main(void)vcpu_enable_cap (vcpu,KVM_CAP_ENFORCE_PV_FEATURE_CPUID ,1 ); -clear_kvm_cpuid_features (vcpu->cpuid); -vcpu_set_cpuid (vcpu); +vcpu_clear_cpuid_entry (vcpu,KVM_CPUID_FEATURES );vm_init_descriptor_tables (vm);vcpu_init_descriptor_tables (vcpu);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 26/42 KVM: selftests: Use vCPU's CPUID directly in Hyper-V test
In reply to Sean ChristophersonUse the vCPU's persistent CPUID array directly when manipulating the set of exposed Hyper-V CPUID features. Drop set_cpuid() to route all future modification through the vCPU helpers; the Hyper-V features test was the last user.
--- .../selftests/kvm/include/x86_64/processor.h | 9 -- .../selftests/kvm/lib/x86_64/processor.c | 18 --- .../selftests/kvm/x86_64/hyperv_features.c | 126 +++++++++--------- 3 files changed, 64 insertions(+), 89 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +0 -9
@@ -727,15 +727,6 @@ uint64_t vm_get_page_table_entry(struct kvm_vm *vm, struct kvm_vcpu *vcpu,void vm_set_page_table_entry (struct kvm_vm *vm,struct kvm_vcpu *vcpu,uint64_t vaddr,uint64_t pte); -/* -* set_cpuid() - overwrites a matching cpuid entry with the provided value. -* matches based on ent->function && ent->index. returns true -* if a match was found and successfully overwritten. -* @cpuid: the kvm cpuid list to modify. -* @ent: cpuid entry to insert -*/ -bool set_cpuid (struct kvm_cpuid2 *cpuid,struct kvm_cpuid_entry2 *ent); -uint64_t kvm_hypercall (uint64_t nr,uint64_t a0,uint64_t a1,uint64_t a2,uint64_t a3);
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -18
@@ -1207,24 +1207,6 @@ struct kvm_cpuid_entry2 *get_cpuid_entry(struct kvm_cpuid2 *cpuid,return NULL ; } -bool set_cpuid (struct kvm_cpuid2 *cpuid, -struct kvm_cpuid_entry2 *ent) -{ -int i; - -for (i =0 ; i < cpuid->nent; i++) { -struct kvm_cpuid_entry2 *cur = &cpuid->entries[i]; - -if (cur->function != ent->function || cur->index != ent->index) -continue ; - -memcpy (cur, ent,sizeof (struct kvm_cpuid_entry2 )); -return true; - } - -return false; -} -uint64_t kvm_hypercall (uint64_t nr,uint64_t a0,uint64_t a1,uint64_t a2,uint64_t a3) {
tools/testing/selftests/kvm/x86_64/hyperv_features.c +64 -62
321 lines not on this page · raw message
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 27/42 KVM: selftests: Use vcpu_get_cpuid_entry() in CPUID test
In reply to Sean ChristophersonUse vcpu_get_cpuid_entry() instead of an open coded equivalent in the CPUID test.
No functional change intended.
--- tools/testing/selftests/kvm/x86_64/cpuid_test.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86_64/cpuid_test.c +2 -3
@@ -147,7 +147,6 @@ struct kvm_cpuid2 *vcpu_alloc_cpuid(struct kvm_vm *vm, vm_vaddr_t *p_gva, structstatic void set_cpuid_after_run (struct kvm_vcpu *vcpu) { -struct kvm_cpuid2 *cpuid = vcpu->cpuid;struct kvm_cpuid_entry2 *ent;int rc;u32 eax, ebx, x;
@@ -157,7 +156,7 @@ static void set_cpuid_after_run(struct kvm_vcpu *vcpu)TEST_ASSERT (!rc,"Setting unmodified CPUID after KVM_RUN failed: %d" , rc);/* Changing CPU features is forbidden */ - ent =get_cpuid_entry (cpuid,0x7 ,0 ); + ent =vcpu_get_cpuid_entry (vcpu,0x7 ); ebx = ent->ebx; ent->ebx--; rc =__vcpu_set_cpuid (vcpu);
@@ -165,7 +164,7 @@ static void set_cpuid_after_run(struct kvm_vcpu *vcpu) ent->ebx = ebx;/* Changing MAXPHYADDR is forbidden */ - ent =get_cpuid_entry (cpuid,0x80000008 ,0 ); + ent =vcpu_get_cpuid_entry (vcpu,0x80000008 ); eax = ent->eax; x = eax &0xff ; ent->eax = (eax & ~0xffu ) | (x -1 );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 28/42 KVM: selftests: Use vcpu_{set,clear}_cpuid_feature() in nVMX state test
In reply to Sean ChristophersonUse vcpu_{set,clear}_cpuid_feature() to toggle nested VMX support in the vCPU CPUID module in the nVMX state test. Drop CPUID_VMX as there are no longer any users.
--- .../selftests/kvm/include/x86_64/processor.h | 1 - .../kvm/x86_64/vmx_set_nested_state_test.c | 20 ++----------------- 2 files changed, 2 insertions(+), 19 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +0 -1
@@ -158,7 +158,6 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_KVM_MIGRATION_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 17)/* CPUID.1.ECX */ -#define CPUID_VMX (1ul << 5)#define CPUID_XSAVE (1ul << 26)#define CPUID_OSXSAVE (1ul << 27)
tools/testing/selftests/kvm/x86_64/vmx_set_nested_state_test.c +2 -18
@@ -121,7 +121,7 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)test_nested_state (vcpu, state);/* Enable VMX in the guest CPUID. */ -vcpu_set_cpuid (vcpu); +vcpu_set_cpuid_feature (vcpu,X86_FEATURE_VMX );/* * Setting vmxon_pa == -1ull and vmcs_pa == -1ull exits early without
@@ -243,22 +243,6 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)free (state); } -void disable_vmx (struct kvm_vcpu *vcpu) -{ -struct kvm_cpuid2 *cpuid = vcpu->cpuid; -int i; - -for (i =0 ; i < cpuid->nent; ++i) -if (cpuid->entries[i].function ==1 && - cpuid->entries[i].index ==0 ) -break ; -TEST_ASSERT (i != cpuid->nent,"CPUID function 1 not found" ); - - cpuid->entries[i].ecx &= ~CPUID_VMX ; -vcpu_set_cpuid (vcpu); - cpuid->entries[i].ecx |=CPUID_VMX ; -} -int main (int argc,char *argv[]) {struct kvm_vm *vm;
@@ -280,7 +264,7 @@ int main(int argc, char *argv[])/* * First run tests with VMX disabled to check error handling. */ -disable_vmx (vcpu); +vcpu_clear_cpuid_feature (vcpu,X86_FEATURE_VMX );/* Passing a NULL kvm_nested_state causes a EFAULT. */ test_nested_state_expect_efault (vcpu,NULL );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 29/42 KVM: selftests: Use vcpu_clear_cpuid_feature() to clear x2APIC
In reply to Sean ChristophersonAdd X86_FEATURE_X2APIC and use vcpu_clear_cpuid_feature() to clear x2APIC support in the xAPIC state test.
--- tools/testing/selftests/kvm/include/x86_64/processor.h | 1 + tools/testing/selftests/kvm/x86_64/xapic_state_test.c | 10 +--------- 2 files changed, 2 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -81,6 +81,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_SMX KVM_X86_CPU_FEATURE(0x1, 0, ECX, 6)#define X86_FEATURE_PDCM KVM_X86_CPU_FEATURE(0x1, 0, ECX, 15)#define X86_FEATURE_PCID KVM_X86_CPU_FEATURE(0x1, 0, ECX, 17) +#define X86_FEATURE_X2APIC KVM_X86_CPU_FEATURE(0x1, 0, ECX, 21)#define X86_FEATURE_MOVBE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 22)#define X86_FEATURE_TSC_DEADLINE_TIMER KVM_X86_CPU_FEATURE(0x1, 0, ECX, 24)#define X86_FEATURE_XSAVE KVM_X86_CPU_FEATURE(0x1, 0, ECX, 26)
tools/testing/selftests/kvm/x86_64/xapic_state_test.c +1 -9
@@ -122,9 +122,7 @@ int main(int argc, char *argv[]) .vcpu = NULL, .is_x2apic = true, }; -struct kvm_cpuid2 *cpuid;struct kvm_vm *vm; -int i; vm =vm_create_with_one_vcpu (&x.vcpu, x2apic_guest_code);test_icr (&x);
@@ -138,13 +136,7 @@ int main(int argc, char *argv[]) vm =vm_create_with_one_vcpu (&x.vcpu, xapic_guest_code); x.is_x2apic = false; - cpuid = x.vcpu->cpuid; -for (i =0 ; i < cpuid->nent; i++) { -if (cpuid->entries[i].function ==1 ) -break ; - } - cpuid->entries[i].ecx &= ~BIT (21 ); -vcpu_set_cpuid (x.vcpu); +vcpu_clear_cpuid_feature (x.vcpu,X86_FEATURE_X2APIC );virt_pg_map (vm,APIC_DEFAULT_GPA ,APIC_DEFAULT_GPA );test_icr (&x);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 30/42 KVM: selftests: Make get_supported_cpuid() returns "const"
In reply to Sean ChristophersonTag the returned CPUID pointers from kvm_get_supported_cpuid(), kvm_get_supported_hv_cpuid(), and vcpu_get_supported_hv_cpuid() "const" to prevent reintroducing the broken pattern of modifying the static "cpuid" variable used by kvm_get_supported_cpuid() to cache the results of KVM_GET_SUPPORTED_CPUID.
Update downstream consumers as needed.
--- .../selftests/kvm/include/x86_64/processor.h | 24 ++++++++--------- .../selftests/kvm/lib/x86_64/processor.c | 27 ++++++------------- .../testing/selftests/kvm/x86_64/cpuid_test.c | 12 ++++----- .../selftests/kvm/x86_64/hyperv_cpuid.c | 10 +++---- .../kvm/x86_64/pmu_event_filter_test.c | 10 +++---- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 2 +- 6 files changed, 36 insertions(+), 49 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +12 -12
@@ -583,7 +583,9 @@ static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs)vcpu_ioctl (vcpu,KVM_SET_XCRS , xcrs); } -struct kvm_cpuid2 *kvm_get_supported_cpuid (void ); +const struct kvm_cpuid2 *kvm_get_supported_cpuid (void ); +const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid (void ); +const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid (struct kvm_vcpu *vcpu);bool kvm_cpuid_has (const struct kvm_cpuid2 *cpuid,struct kvm_x86_cpu_feature feature);
@@ -616,15 +618,17 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)return cpuid; } -struct kvm_cpuid_entry2 *get_cpuid_entry (struct kvm_cpuid2 *cpuid, -uint32_t function,uint32_t index); -void vcpu_init_cpuid (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid); +const struct kvm_cpuid_entry2 *get_cpuid_entry (const struct kvm_cpuid2 *cpuid, +uint32_t function,uint32_t index); +void vcpu_init_cpuid (struct kvm_vcpu *vcpu,const struct kvm_cpuid2 *cpuid); +void vcpu_set_hv_cpuid (struct kvm_vcpu *vcpu);static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry (struct kvm_vcpu *vcpu,uint32_t function,uint32_t index) { -return get_cpuid_entry (vcpu->cpuid, function, index); +return (struct kvm_cpuid_entry2 *)get_cpuid_entry (vcpu->cpuid, + function, index); } static inline struct kvm_cpuid_entry2 *vcpu_get_cpuid_entry(struct kvm_vcpu *vcpu,
@@ -676,14 +680,13 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,vcpu_set_or_clear_cpuid_feature (vcpu, feature, false); } -static inline struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_index (uint32_t function, -uint32_t index) +static inline const struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_index (uint32_t function, +uint32_t index) {return get_cpuid_entry (kvm_get_supported_cpuid (), function, index); } -static inline struct kvm_cpuid_entry2 * -kvm_get_supported_cpuid_entry (uint32_t function) +static inline const struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_entry (uint32_t function) {return kvm_get_supported_cpuid_index (function,0 ); }
@@ -730,9 +733,6 @@ void vm_set_page_table_entry(struct kvm_vm *vm, struct kvm_vcpu *vcpu,uint64_t kvm_hypercall (uint64_t nr,uint64_t a0,uint64_t a1,uint64_t a2,uint64_t a3); -struct kvm_cpuid2 *kvm_get_supported_hv_cpuid (void ); -void vcpu_set_hv_cpuid (struct kvm_vcpu *vcpu); -struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid (struct kvm_vcpu *vcpu);void vm_xsave_req_perm (int bit);enum pg_level {
tools/testing/selftests/kvm/lib/x86_64/processor.c +8 -19
@@ -676,18 +676,7 @@ void vcpu_arch_free(struct kvm_vcpu *vcpu)free (vcpu->cpuid); } -/* -* KVM Supported CPUID Get -* -* Input Args: None -* -* Output Args: -* -* Return: The supported KVM CPUID -* -* Get the guest CPUID supported by KVM. -*/ -struct kvm_cpuid2 *kvm_get_supported_cpuid (void ) +const struct kvm_cpuid2 *kvm_get_supported_cpuid (void ) {static struct kvm_cpuid2 *cpuid;int kvm_fd;
@@ -745,7 +734,7 @@ uint64_t kvm_get_feature_msr(uint64_t msr_index)return buffer.entry.data; } -void vcpu_init_cpuid (struct kvm_vcpu *vcpu,struct kvm_cpuid2 *cpuid) +void vcpu_init_cpuid (struct kvm_vcpu *vcpu,const struct kvm_cpuid2 *cpuid) {TEST_ASSERT (cpuid != vcpu->cpuid,"@cpuid can't be the vCPU's CPUID" );
@@ -1079,7 +1068,7 @@ uint32_t kvm_get_cpuid_max_extended(void)void kvm_get_cpu_address_width (unsigned int *pa_bits,unsigned int *va_bits) { -struct kvm_cpuid_entry2 *entry; +const struct kvm_cpuid_entry2 *entry;bool pae;/* SDM 4.1.4 */
@@ -1191,8 +1180,8 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu) } } -struct kvm_cpuid_entry2 *get_cpuid_entry (struct kvm_cpuid2 *cpuid, -uint32_t function,uint32_t index) +const struct kvm_cpuid_entry2 *get_cpuid_entry (const struct kvm_cpuid2 *cpuid, +uint32_t function,uint32_t index) {int i;
@@ -1218,7 +1207,7 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,return r; } -struct kvm_cpuid2 *kvm_get_supported_hv_cpuid (void ) +const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid (void ) {static struct kvm_cpuid2 *cpuid;int kvm_fd;
@@ -1238,7 +1227,7 @@ struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void)void vcpu_set_hv_cpuid (struct kvm_vcpu *vcpu) {static struct kvm_cpuid2 *cpuid_full; -struct kvm_cpuid2 *cpuid_sys, *cpuid_hv; +const struct kvm_cpuid2 *cpuid_sys, *cpuid_hv;int i, nent =0 ;if (!cpuid_full) {
@@ -1268,7 +1257,7 @@ void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu)vcpu_init_cpuid (vcpu, cpuid_full); } -struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid (struct kvm_vcpu *vcpu) +const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid (struct kvm_vcpu *vcpu) {struct kvm_cpuid2 *cpuid =allocate_kvm_cpuid2 (MAX_NR_CPUID_ENTRIES );
tools/testing/selftests/kvm/x86_64/cpuid_test.c +5 -7
@@ -66,7 +66,7 @@ static void guest_main(struct kvm_cpuid2 *guest_cpuid)GUEST_DONE (); } -static bool is_cpuid_mangled (struct kvm_cpuid_entry2 *entrie) +static bool is_cpuid_mangled (const struct kvm_cpuid_entry2 *entrie) {int i;
@@ -79,9 +79,10 @@ static bool is_cpuid_mangled(struct kvm_cpuid_entry2 *entrie)return false; } -static void compare_cpuids (struct kvm_cpuid2 *cpuid1,struct kvm_cpuid2 *cpuid2) +static void compare_cpuids (const struct kvm_cpuid2 *cpuid1, +const struct kvm_cpuid2 *cpuid2) { -struct kvm_cpuid_entry2 *e1, *e2; +const struct kvm_cpuid_entry2 *e1, *e2;int i; TEST_ASSERT(cpuid1->nent == cpuid2->nent,
@@ -175,7 +176,6 @@ static void set_cpuid_after_run(struct kvm_vcpu *vcpu)int main (void ) { -struct kvm_cpuid2 *supp_cpuid;struct kvm_vcpu *vcpu;vm_vaddr_t cpuid_gva;struct kvm_vm *vm;
@@ -183,9 +183,7 @@ int main(void) vm =vm_create_with_one_vcpu (&vcpu, guest_main); - supp_cpuid =kvm_get_supported_cpuid (); - -compare_cpuids (supp_cpuid, vcpu->cpuid); +compare_cpuids (kvm_get_supported_cpuid (), vcpu->cpuid);vcpu_alloc_cpuid (vm, &cpuid_gva, vcpu->cpuid);
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +5 -5
@@ -43,7 +43,7 @@ static bool smt_possible(void)return res; } -static void test_hv_cpuid (struct kvm_cpuid2 *hv_cpuid_entries, +static void test_hv_cpuid (const struct kvm_cpuid2 *hv_cpuid_entries,bool evmcs_expected) {int i;
@@ -56,7 +56,7 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, nent_expected, hv_cpuid_entries->nent);for (i =0 ; i < hv_cpuid_entries->nent; i++) { -struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i]; +const struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i]; TEST_ASSERT((entry->function >= 0x40000000) && (entry->function <= 0x40000082),
@@ -131,7 +131,7 @@ void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)int main (int argc,char *argv[]) {struct kvm_vm *vm; -struct kvm_cpuid2 *hv_cpuid_entries; +const struct kvm_cpuid2 *hv_cpuid_entries;struct kvm_vcpu *vcpu;/* Tell stdout not to buffer its content */
@@ -146,7 +146,7 @@ int main(int argc, char *argv[]) hv_cpuid_entries =vcpu_get_supported_hv_cpuid (vcpu);test_hv_cpuid (hv_cpuid_entries, false); -free (hv_cpuid_entries); +free ((void *)hv_cpuid_entries);if (!kvm_cpu_has (X86_FEATURE_VMX ) || !kvm_has_cap (KVM_CAP_HYPERV_ENLIGHTENED_VMCS )) {
@@ -156,7 +156,7 @@ int main(int argc, char *argv[])vcpu_enable_evmcs (vcpu); hv_cpuid_entries =vcpu_get_supported_hv_cpuid (vcpu);test_hv_cpuid (hv_cpuid_entries, true); -free (hv_cpuid_entries); +free ((void *)hv_cpuid_entries); do_sys:/* Test system ioctl version */
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c +5 -5
@@ -384,7 +384,7 @@ static void test_pmu_config_disable(void (*guest_code)(void))* counter per logical processor, an EBX bit vector of length greater * than 5, and EBX[5] clear. */ -static bool check_intel_pmu_leaf (struct kvm_cpuid_entry2 *entry) +static bool check_intel_pmu_leaf (const struct kvm_cpuid_entry2 *entry) {union cpuid10_eax eax = { .full = entry->eax };union cpuid10_ebx ebx = { .full = entry->ebx };
@@ -400,10 +400,10 @@ static bool check_intel_pmu_leaf(struct kvm_cpuid_entry2 *entry)*/ static bool use_intel_pmu (void ) { -struct kvm_cpuid_entry2 *entry; +const struct kvm_cpuid_entry2 *entry; entry =kvm_get_supported_cpuid_index (0xa ,0 ); -return is_intel_cpu () && entry &&check_intel_pmu_leaf (entry); +return is_intel_cpu () &&check_intel_pmu_leaf (entry); }static bool is_zen1 (uint32_t eax)
@@ -432,10 +432,10 @@ static bool is_zen3(uint32_t eax)*/ static bool use_amd_pmu (void ) { -struct kvm_cpuid_entry2 *entry; +const struct kvm_cpuid_entry2 *entry; entry =kvm_get_supported_cpuid_index (1 ,0 ); -return is_amd_cpu () && entry && +return is_amd_cpu () && (is_zen1 (entry->eax) ||is_zen2 (entry->eax) ||is_zen3 (entry->eax));
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +1 -1
@@ -53,7 +53,7 @@ static void guest_code(void)int main (int argc,char *argv[]) { -struct kvm_cpuid_entry2 *entry_a_0; +const struct kvm_cpuid_entry2 *entry_a_0;struct kvm_vm *vm;struct kvm_vcpu *vcpu;int ret;
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 31/42 KVM: selftests: Set input function/index in raw CPUID helper(s)
In reply to Sean ChristophersonSet the function/index for CPUID in the helper instead of relying on the caller to do so. In addition to reducing the risk of consuming an uninitialized ECX, having the function/index embedded in the call makes it easier to understand what is being checked.
--- .../selftests/kvm/include/x86_64/processor.h | 16 +++++++++++++--- .../selftests/kvm/lib/x86_64/processor.c | 13 ++++--------- tools/testing/selftests/kvm/x86_64/amx_test.c | 19 ++++--------------- .../testing/selftests/kvm/x86_64/cpuid_test.c | 11 +++++------ 4 files changed, 26 insertions(+), 33 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +13 -3
@@ -402,10 +402,13 @@ static inline void outl(uint16_t port, uint32_t value) __asm__ __volatile__("outl %%eax, %%dx" : :"d" (port),"a" (value)); } -static inline void cpuid (uint32_t *eax,uint32_t *ebx, -uint32_t *ecx,uint32_t *edx) +static inline void __cpuid (uint32_t function,uint32_t index, +uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) { -/* ecx is often an input as well as an output. */ + *eax = function; + *ecx = index; + asm volatile("cpuid" : "=a" (*eax), "=b" (*ebx),
@@ -415,6 +418,13 @@ static inline void cpuid(uint32_t *eax, uint32_t *ebx, : "memory"); } +static inline void cpuid (uint32_t function, +uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) +{ +return __cpuid (function,0 , eax, ebx, ecx, edx); +} +#define SET_XMM (__var, __xmm) \ asm volatile("movq %0, %%"#__xmm : : "r"(__var) : #__xmm)
tools/testing/selftests/kvm/lib/x86_64/processor.c +4 -9
@@ -1284,9 +1284,7 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)/* Before family 17h, the HyperTransport area is just below 1T. */ ht_gfn = (1 <<28 ) - num_ht_pages; - eax =1 ; - ecx =0 ; -cpuid (&eax, &ebx, &ecx, &edx); +cpuid (1 , &eax, &ebx, &ecx, &edx);if (x86_family (eax) <0x17 ) goto done;
@@ -1295,18 +1293,15 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm)* reduced due to SME by bits 11:6 of CPUID[0x8000001f].EBX. Use * the old conservative value if MAXPHYADDR is not enumerated. */ - eax =0x80000000 ; -cpuid (&eax, &ebx, &ecx, &edx); +cpuid (0x80000000 , &eax, &ebx, &ecx, &edx); max_ext_leaf = eax;if (max_ext_leaf <0x80000008 ) goto done; - eax =0x80000008 ; -cpuid (&eax, &ebx, &ecx, &edx); +cpuid (0x80000008 , &eax, &ebx, &ecx, &edx); max_pfn = (1ULL << ((eax &0xff ) - vm->page_shift)) -1 ;if (max_ext_leaf >=0x8000001f ) { - eax =0x8000001f ; -cpuid (&eax, &ebx, &ecx, &edx); +cpuid (0x8000001f , &eax, &ebx, &ecx, &edx); max_pfn >>= (ebx >>6 ) &0x3f ; }
tools/testing/selftests/kvm/x86_64/amx_test.c +4 -15
@@ -122,9 +122,7 @@ static inline void check_cpuid_xsave(void) {uint32_t eax, ebx, ecx, edx; - eax =1 ; - ecx =0 ; -cpuid (&eax, &ebx, &ecx, &edx); +cpuid (1 , &eax, &ebx, &ecx, &edx);if (!(ecx &CPUID_XSAVE ))GUEST_ASSERT (!"cpuid: no CPU xsave support!" );if (!(ecx &CPUID_OSXSAVE ))
@@ -140,10 +138,7 @@ static bool enum_xtile_config(void) {u32 eax, ebx, ecx, edx; - eax =TILE_CPUID ; - ecx =TILE_PALETTE_CPUID_SUBLEAVE ; - -cpuid (&eax, &ebx, &ecx, &edx); +__cpuid (TILE_CPUID ,TILE_PALETTE_CPUID_SUBLEAVE , &eax, &ebx, &ecx, &edx);if (!eax || !ebx || !ecx)return false;
@@ -165,10 +160,7 @@ static bool enum_xsave_tile(void) {u32 eax, ebx, ecx, edx; - eax =XSTATE_CPUID ; - ecx =XFEATURE_XTILEDATA ; - -cpuid (&eax, &ebx, &ecx, &edx); +__cpuid (XSTATE_CPUID ,XFEATURE_XTILEDATA , &eax, &ebx, &ecx, &edx);if (!eax || !ebx)return false;
@@ -183,10 +175,7 @@ static bool check_xsave_size(void)u32 eax, ebx, ecx, edx;bool valid = false; - eax =XSTATE_CPUID ; - ecx =XSTATE_USER_STATE_SUBLEAVE ; - -cpuid (&eax, &ebx, &ecx, &edx); +__cpuid (XSTATE_CPUID ,XSTATE_USER_STATE_SUBLEAVE , &eax, &ebx, &ecx, &edx);if (ebx && ebx <=XSAVE_SIZE ) valid = true;
tools/testing/selftests/kvm/x86_64/cpuid_test.c +5 -6
@@ -31,10 +31,9 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)u32 eax, ebx, ecx, edx;for (i =0 ; i < guest_cpuid->nent; i++) { - eax = guest_cpuid->entries[i].function; - ecx = guest_cpuid->entries[i].index; - -cpuid (&eax, &ebx, &ecx, &edx); +__cpuid (guest_cpuid->entries[i].function, + guest_cpuid->entries[i].index, + &eax, &ebx, &ecx, &edx); GUEST_ASSERT(eax == guest_cpuid->entries[i].eax && ebx == guest_cpuid->entries[i].ebx &&
@@ -46,9 +45,9 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)static void test_cpuid_40000000 (struct kvm_cpuid2 *guest_cpuid) { -u32 eax =0x40000000 , ebx, ecx =0 , edx; +u32 eax, ebx, ecx, edx; -cpuid (&eax, &ebx, &ecx, &edx); +cpuid (0x40000000 , &eax, &ebx, &ecx, &edx);GUEST_ASSERT (eax ==0x40000001 ); }
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 32/42 KVM: selftests: Add this_cpu_has() to query X86_FEATURE_* via cpuid()
In reply to Sean ChristophersonAdd this_cpu_has() to query an X86_FEATURE_* via cpuid(), i.e. to query a feature from L1 (or L2) guest code. Arbitrarily select the AMX test to be the first user.
--- .../testing/selftests/kvm/include/x86_64/processor.h | 12 +++++++++++- tools/testing/selftests/kvm/x86_64/amx_test.c | 9 ++------- 2 files changed, 13 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +11 -1
@@ -159,7 +159,6 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_KVM_MIGRATION_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 17)/* CPUID.1.ECX */ -#define CPUID_XSAVE (1ul << 26)#define CPUID_OSXSAVE (1ul << 27)/* Page table bitfield declarations */
@@ -425,6 +424,17 @@ static inline void cpuid(uint32_t function,return __cpuid (function,0 , eax, ebx, ecx, edx); } +static inline bool this_cpu_has (struct kvm_x86_cpu_feature feature) +{ +uint32_t gprs[4 ]; + +__cpuid (feature.function, feature.index, + &gprs[KVM_CPUID_EAX ], &gprs[KVM_CPUID_EBX ], + &gprs[KVM_CPUID_ECX ], &gprs[KVM_CPUID_EDX ]); + +return gprs[feature.reg] &BIT (feature.bit); +} +#define SET_XMM (__var, __xmm) \ asm volatile("movq %0, %%"#__xmm : : "r"(__var) : #__xmm)
tools/testing/selftests/kvm/x86_64/amx_test.c +2 -7
@@ -120,13 +120,8 @@ static inline void __xsavec(struct xsave_data *data, uint64_t rfbm)static inline void check_cpuid_xsave (void ) { -uint32_t eax, ebx, ecx, edx; - -cpuid (1 , &eax, &ebx, &ecx, &edx); -if (!(ecx &CPUID_XSAVE )) -GUEST_ASSERT (!"cpuid: no CPU xsave support!" ); -if (!(ecx &CPUID_OSXSAVE )) -GUEST_ASSERT (!"cpuid: no OS xsave support!" ); +GUEST_ASSERT (this_cpu_has (X86_FEATURE_XSAVE )); +GUEST_ASSERT (this_cpu_has (X86_FEATURE_OSXSAVE )); }static bool check_xsave_supports_xtile (void )
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 33/42 KVM: selftests: Use this_cpu_has() in CR4/CPUID sync test
In reply to Sean ChristophersonUse this_cpu_has() to query OSXSAVE from the L1 guest in the CR4=>CPUID sync test.
--- .../selftests/kvm/include/x86_64/processor.h | 3 --- .../selftests/kvm/x86_64/cr4_cpuid_sync_test.c | 14 ++------------ 2 files changed, 2 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +0 -3
@@ -158,9 +158,6 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_KVM_HC_MAP_GPA_RANGE KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 16)#define X86_FEATURE_KVM_MIGRATION_CONTROL KVM_X86_CPU_FEATURE(0x40000001, 0, EAX, 17) -/* CPUID.1.ECX */ -#define CPUID_OSXSAVE (1ul << 27) -/* Page table bitfield declarations */ #define PTE_PRESENT_MASK BIT_ULL(0)#define PTE_WRITABLE_MASK BIT_ULL(1)
tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c +2 -12
@@ -21,19 +21,9 @@static inline bool cr4_cpuid_is_sync (void ) { -int func, subfunc; -uint32_t eax, ebx, ecx, edx; -uint64_t cr4; +uint64_t cr4 =get_cr4 (); - func =0x1 ; - subfunc =0x0 ; - __asm__ __volatile__("cpuid" - :"=a" (eax),"=b" (ebx),"=c" (ecx),"=d" (edx) - :"a" (func),"c" (subfunc)); - - cr4 =get_cr4 (); - -return (!!(ecx &CPUID_OSXSAVE )) == (!!(cr4 &X86_CR4_OSXSAVE )); +return (this_cpu_has (X86_FEATURE_OSXSAVE ) == !!(cr4 &X86_CR4_OSXSAVE )); }static void guest_code (void )
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 34/42 KVM: selftests: Use this_cpu_has() to detect SVM support in L1
In reply to Sean ChristophersonReplace an evil open coded instance of querying CPUID from L1 with this_cpu_has(X86_FEATURE_SVM).
No functional change intended.
--- .../testing/selftests/kvm/include/x86_64/svm_util.h | 13 ------------- tools/testing/selftests/kvm/x86_64/smm_test.c | 4 ++-- tools/testing/selftests/kvm/x86_64/state_test.c | 2 +- 3 files changed, 3 insertions(+), 16 deletions(-)
tools/testing/selftests/kvm/include/x86_64/svm_util.h +0 -13
@@ -13,9 +13,6 @@#include "svm.h" #include "processor.h" -#define CPUID_SVM_BIT 2 -#define CPUID_SVM BIT_ULL(CPUID_SVM_BIT) -#define SVM_EXIT_EXCP_BASE 0x040#define SVM_EXIT_HLT 0x078#define SVM_EXIT_MSR 0x07c
@@ -52,16 +49,6 @@ struct svm_test_data *vcpu_alloc_svm(struct kvm_vm *vm, vm_vaddr_t *p_svm_gva);void generic_svm_setup (struct svm_test_data *svm,void *guest_rip,void *guest_rsp);void run_guest (struct vmcb *vmcb,uint64_t vmcb_gpa); -static inline bool cpu_has_svm (void ) -{ -u32 eax =0x80000001 , ecx; - - asm("cpuid" : -"=a" (eax),"=c" (ecx) :"0" (eax) :"ebx" ,"edx" ); - -return ecx &CPUID_SVM ; -} -int open_sev_dev_path_or_exit (void );#endif /* SELFTEST_KVM_SVM_UTILS_H */
tools/testing/selftests/kvm/x86_64/smm_test.c +2 -2
@@ -83,7 +83,7 @@ static void guest_code(void *arg)sync_with_host (4 );if (arg) { -if (cpu_has_svm ()) { +if (this_cpu_has (X86_FEATURE_SVM )) {generic_svm_setup (svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); }else {
@@ -99,7 +99,7 @@ static void guest_code(void *arg)sync_with_host (7 ); -if (cpu_has_svm ()) { +if (this_cpu_has (X86_FEATURE_SVM )) {run_guest (svm->vmcb, svm->vmcb_gpa);run_guest (svm->vmcb, svm->vmcb_gpa); }else {
tools/testing/selftests/kvm/x86_64/state_test.c +1 -1
@@ -142,7 +142,7 @@ static void __attribute__((__flatten__)) guest_code(void *arg)GUEST_SYNC (2 );if (arg) { -if (cpu_has_svm ()) +if (this_cpu_has (X86_FEATURE_SVM ))svm_l1_guest_code (arg);else vmx_l1_guest_code (arg);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 35/42 KVM: selftests: Drop unnecessary use of kvm_get_supported_cpuid_index()
In reply to Sean ChristophersonUse kvm_get_supported_cpuid_entry() instead of kvm_get_supported_cpuid_index() when passing in '0' for the index, which just so happens to be the case in all remaining users of kvm_get_supported_cpuid_index() except kvm_get_supported_cpuid_entry().
Keep the helper as there may be users in the future, and it's not doing any harm.
--- tools/testing/selftests/kvm/x86_64/amx_test.c | 2 +- tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c | 4 ++-- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/x86_64/amx_test.c +1 -1
@@ -318,7 +318,7 @@ int main(int argc, char *argv[])TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_XTILEDATA ));/* Get xsave/restore max size */ - xsave_restore_size =kvm_get_supported_cpuid_index (0xd ,0 )->ecx; + xsave_restore_size =kvm_get_supported_cpuid_entry (0xd )->ecx; run = vcpu->run;vcpu_regs_get (vcpu, ®s1);
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c +2 -2
@@ -402,7 +402,7 @@ static bool use_intel_pmu(void) {const struct kvm_cpuid_entry2 *entry; - entry =kvm_get_supported_cpuid_index (0xa ,0 ); + entry =kvm_get_supported_cpuid_entry (0xa );return is_intel_cpu () &&check_intel_pmu_leaf (entry); }
@@ -434,7 +434,7 @@ static bool use_amd_pmu(void) {const struct kvm_cpuid_entry2 *entry; - entry =kvm_get_supported_cpuid_index (1 ,0 ); + entry =kvm_get_supported_cpuid_entry (1 ); return is_amd_cpu() && (is_zen1(entry->eax) || is_zen2(entry->eax) ||
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +1 -1
@@ -69,7 +69,7 @@ int main(int argc, char *argv[])TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_PDCM ));TEST_REQUIRE (kvm_get_cpuid_max_basic () >=0xa ); - entry_a_0 =kvm_get_supported_cpuid_index (0xa ,0 ); + entry_a_0 =kvm_get_supported_cpuid_entry (0xa ); eax.full = entry_a_0->eax;__TEST_REQUIRE (eax.split.version_id,"PMU is not supported by the vCPU" );
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 36/42 KVM: selftests: Rename kvm_get_supported_cpuid_index() to __..._entry()
In reply to Sean ChristophersonRename kvm_get_supported_cpuid_index() to __kvm_get_supported_cpuid_entry() to better show its relationship to kvm_get_supported_cpuid_entry(), and because the helper returns a CPUID entry, not the index of an entry.
No functional change intended.
--- tools/testing/selftests/kvm/include/x86_64/processor.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +3 -3
@@ -697,15 +697,15 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,vcpu_set_or_clear_cpuid_feature (vcpu, feature, false); } -static inline const struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_index (uint32_t function, -uint32_t index) +static inline const struct kvm_cpuid_entry2 *__kvm_get_supported_cpuid_entry (uint32_t function, +uint32_t index) {return get_cpuid_entry (kvm_get_supported_cpuid (), function, index); }static inline const struct kvm_cpuid_entry2 *kvm_get_supported_cpuid_entry (uint32_t function) { -return kvm_get_supported_cpuid_index (function,0 ); +return __kvm_get_supported_cpuid_entry (function,0 ); }uint64_t vcpu_get_msr (struct kvm_vcpu *vcpu,uint64_t msr_index);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 37/42 KVM: selftests: Inline "get max CPUID leaf" helpers
In reply to Sean ChristophersonMake the "get max CPUID leaf" helpers static inline, there's no reason to bury the one liners in processor.c.
--- .../testing/selftests/kvm/include/x86_64/processor.h | 11 +++++++++-- tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ---------- 2 files changed, 9 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +9 -2
@@ -719,9 +719,16 @@ static inline void vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index,TEST_ASSERT (r ==1 ,KVM_IOCTL_ERROR (KVM_SET_MSRS , r)); } +static inline uint32_t kvm_get_cpuid_max_basic (void ) +{ +return kvm_get_supported_cpuid_entry (0 )->eax; +} + +static inline uint32_t kvm_get_cpuid_max_extended (void ) +{ +return kvm_get_supported_cpuid_entry (0x80000000 )->eax; +} -uint32_t kvm_get_cpuid_max_basic (void ); -uint32_t kvm_get_cpuid_max_extended (void );void kvm_get_cpu_address_width (unsigned int *pa_bits,unsigned int *va_bits);bool vm_is_unrestricted_guest (struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -10
@@ -1056,16 +1056,6 @@ bool is_amd_cpu(void)return cpu_vendor_string_is ("AuthenticAMD" ); } -uint32_t kvm_get_cpuid_max_basic (void ) -{ -return kvm_get_supported_cpuid_entry (0 )->eax; -} - -uint32_t kvm_get_cpuid_max_extended (void ) -{ -return kvm_get_supported_cpuid_entry (0x80000000 )->eax; -} -void kvm_get_cpu_address_width (unsigned int *pa_bits,unsigned int *va_bits) {const struct kvm_cpuid_entry2 *entry;
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 38/42 KVM: selftests: Check KVM's supported CPUID, not host CPUID, for XFD
In reply to Sean ChristophersonUse kvm_cpu_has() to check for XFD supported in vm_xsave_req_perm(), simply checking host CPUID doesn't guarantee KVM supports AMX/XFD.
Opportunistically hoist the check above the bit check; if XFD isn't supported, it's far better to get a "not supported at all" message, as opposed to a "feature X isn't supported" message".
--- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/lib/x86_64/processor.c | 19 ++----------------- 2 files changed, 3 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -115,6 +115,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_XTILECFG KVM_X86_CPU_FEATURE(0xD, 0, EAX, 17)#define X86_FEATURE_XTILEDATA KVM_X86_CPU_FEATURE(0xD, 0, EAX, 18)#define X86_FEATURE_XSAVES KVM_X86_CPU_FEATURE(0xD, 1, EAX, 3) +#define X86_FEATURE_XFD KVM_X86_CPU_FEATURE(0xD, 1, EAX, 4)/* * Extended Leafs, a.k.a. AMD defined
tools/testing/selftests/kvm/lib/x86_64/processor.c +2 -17
@@ -578,21 +578,6 @@ static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu)vcpu_sregs_set (vcpu, &sregs); } -#define CPUID_XFD_BIT (1 << 4) -static bool is_xfd_supported (void ) -{ -int eax, ebx, ecx, edx; -const int leaf =0xd , subleaf =0x1 ; - - __asm__ __volatile__( -"cpuid" - :/* output */ "=a" (eax),"=b" (ebx), -"=c" (ecx),"=d" (edx) - :/* input */ "0" (leaf),"2" (subleaf)); - -return !!(eax &CPUID_XFD_BIT ); -} -void vm_xsave_req_perm (int bit) {int kvm_fd;
@@ -604,6 +589,8 @@ void vm_xsave_req_perm(int bit) .addr = (unsigned long) &bitmask }; +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_XFD )); + kvm_fd =open_kvm_dev_path_or_exit (); rc =__kvm_ioctl (kvm_fd,KVM_GET_DEVICE_ATTR , &attr);close (kvm_fd);
@@ -614,8 +601,6 @@ void vm_xsave_req_perm(int bit)TEST_REQUIRE (bitmask & (1ULL << bit)); -TEST_REQUIRE (is_xfd_supported ()); - rc =syscall (SYS_arch_prctl,ARCH_REQ_XCOMP_GUEST_PERM , bit);/*
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 39/42 KVM: selftests: Skip AMX test if ARCH_REQ_XCOMP_GUEST_PERM isn't supported
In reply to Sean ChristophersonSkip the AMX test instead of silently returning if the host kernel doesn't support ARCH_REQ_XCOMP_GUEST_PERM. KVM didn't support XFD until v5.17, so it's extremely unlikely allowing the test to run on a pre-v5.15 kernel is the right thing to do.
--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +1 -8
@@ -601,14 +601,7 @@ void vm_xsave_req_perm(int bit)TEST_REQUIRE (bitmask & (1ULL << bit)); - rc =syscall (SYS_arch_prctl,ARCH_REQ_XCOMP_GUEST_PERM , bit); - -/* -* The older kernel version(<5.15) can't support -* ARCH_REQ_XCOMP_GUEST_PERM and directly return. -*/ -if (rc) -return ; +TEST_REQUIRE (!syscall (SYS_arch_prctl,ARCH_REQ_XCOMP_GUEST_PERM , bit)); rc =syscall (SYS_arch_prctl,ARCH_GET_XCOMP_GUEST_PERM , &bitmask);TEST_ASSERT (rc ==0 ,"prctl(ARCH_GET_XCOMP_GUEST_PERM) error: %ld" , rc);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 40/42 KVM: selftests: Clean up requirements for XFD-aware XSAVE features
In reply to Sean ChristophersonProvide informative error messages for the various checks related to requesting access to XSAVE features that are buried behind XSAVE Feature Disabling (XFD).
Opportunistically rename the helper to have "require" in the name so that it's somewhat obvious that the helper may skip the test.
--- tools/testing/selftests/kvm/include/x86_64/processor.h | 5 ++++- tools/testing/selftests/kvm/lib/x86_64/processor.c | 8 +++++--- tools/testing/selftests/kvm/x86_64/amx_test.c | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +4 -1
@@ -758,7 +758,10 @@ void vm_set_page_table_entry(struct kvm_vm *vm, struct kvm_vcpu *vcpu,uint64_t kvm_hypercall (uint64_t nr,uint64_t a0,uint64_t a1,uint64_t a2,uint64_t a3); -void vm_xsave_req_perm (int bit); +void __vm_xsave_require_permission (int bit,const char *name); + +#define vm_xsave_require_permission (perm) \ + __vm_xsave_require_permission(perm, #perm)enum pg_level {PG_LEVEL_NONE ,
tools/testing/selftests/kvm/lib/x86_64/processor.c +5 -3
@@ -578,7 +578,7 @@ static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu)vcpu_sregs_set (vcpu, &sregs); } -void vm_xsave_req_perm (int bit) +void __vm_xsave_require_permission (int bit,const char *name) {int kvm_fd;u64 bitmask;
@@ -596,10 +596,12 @@ void vm_xsave_req_perm(int bit)close (kvm_fd);if (rc ==-1 && (errno ==ENXIO || errno ==EINVAL )) -exit (KSFT_SKIP ); +__TEST_REQUIRE (0 ,"KVM_X86_XCOMP_GUEST_SUPP not supported" ); +TEST_ASSERT (rc ==0 ,"KVM_GET_DEVICE_ATTR(0, KVM_X86_XCOMP_GUEST_SUPP) error: %ld" , rc); -TEST_REQUIRE (bitmask & (1ULL << bit)); +__TEST_REQUIRE (bitmask & (1ULL << bit), +"Required XSAVE feature '%s' not supported" , name);TEST_REQUIRE (!syscall (SYS_arch_prctl,ARCH_REQ_XCOMP_GUEST_PERM , bit));
tools/testing/selftests/kvm/x86_64/amx_test.c +1 -1
@@ -307,7 +307,7 @@ int main(int argc, char *argv[])u32 amx_offset;int stage, ret; -vm_xsave_req_perm (XSTATE_XTILE_DATA_BIT ); +vm_xsave_require_permission (XSTATE_XTILE_DATA_BIT );/* Create VM */ vm =vm_create_with_one_vcpu (&vcpu, guest_code);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 41/42 KVM: selftests: Use the common cpuid() helper in cpu_vendor_string_is()
In reply to Sean ChristophersonUse cpuid() to get CPUID.0x0 in cpu_vendor_string_is(), thus eliminating the last open coded usage of CPUID (ignoring debug_regs.c, which emits CPUID from the guest to trigger a VM-Exit and doesn't actually care about the results of CPUID).
--- tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +2 -8
@@ -1011,15 +1011,9 @@ void kvm_x86_state_cleanup(struct kvm_x86_state *state)static bool cpu_vendor_string_is (const char *vendor) {const uint32_t *chunk = (const uint32_t *)vendor; -int eax, ebx, ecx, edx; -const int leaf =0 ; - - __asm__ __volatile__( -"cpuid" - :/* output */ "=a" (eax),"=b" (ebx), -"=c" (ecx),"=d" (edx) - :/* input */ "0" (leaf),"2" (0 )); +uint32_t eax, ebx, ecx, edx; +cpuid (0 , &eax, &ebx, &ecx, &edx);return (ebx == chunk[0 ] && edx == chunk[1 ] && ecx == chunk[2 ]); }
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Sean Christopherson
reviewerPATCH v2 42/42 KVM: selftests: Drop unused SVM_CPUID_FUNC macro
In reply to Sean ChristophersonDrop SVM_CPUID_FUNC to reduce the probability of tests open coding CPUID checks instead of using kvm_cpu_has() or this_cpu_has().
--- tools/testing/selftests/kvm/include/x86_64/svm.h | 2 -- 1 file changed, 2 deletions(-)
tools/testing/selftests/kvm/include/x86_64/svm.h +0 -2
@@ -218,8 +218,6 @@ struct __attribute__ ((__packed__)) vmcb {struct vmcb_save_area save; }; -#define SVM_CPUID_FUNC 0x8000000a -#define SVM_VM_CR_SVM_DISABLE 4#define SVM_SELECTOR_S_SHIFT 4
Signature · 1 line
2.36.1.476.g0c4daa206d-goog