Skip to contentSkip to conversation

KVM: selftests: Re-enable hyperv_evmcs/hyperv_svm_test on bare metal

PATCH In review Vitaly Kuznetsov 2 participants 4 messages Aug 16, 2024 – Aug 23, 2024

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

cover KVM: selftests: Re-enable hyperv_evmcs/hyperv_svm_test on bare metal

Commit 6dac1195181c ("KVM: selftests: Make Hyper-V tests explicitly require KVM Hyper-V support") wrongfully added KVM_CAP_HYPERV_DIRECT_TLBFLUSH requirement to hyperv_evmcs/hyperv_svm_test tests. The capability is only set when KVM runs on top of Hyper-V. The result is that both tests just skip when launched on bare metal. Add the required infrastructure and check for the correct CPUID bit in KVM_GET_SUPPORTED_HV_CPUID instead.

Vitaly Kuznetsov (2):
KVM: selftests: Move Hyper-V specific functions out of processor.c
KVM: selftests: Re-enable hyperv_evmcs/hyperv_svm_test on bare metal

 .../selftests/kvm/include/x86_64/hyperv.h     | 18 +++++
 .../selftests/kvm/include/x86_64/processor.h  |  7 +-
 .../testing/selftests/kvm/lib/x86_64/hyperv.c | 67 +++++++++++++++++++
 .../selftests/kvm/lib/x86_64/processor.c      | 61 -----------------
 .../selftests/kvm/x86_64/hyperv_evmcs.c       |  2 +-
 .../selftests/kvm/x86_64/hyperv_svm_test.c    |  2 +-
 .../selftests/kvm/x86_64/xen_vmcall_test.c    |  1 +
 7 files changed, 92 insertions(+), 66 deletions(-)
Signature · 1 line
2.46.0
#1 lore raw

PATCH 1/2 KVM: selftests: Move Hyper-V specific functions out of processor.c

Since there is 'hyperv.c' for Hyper-V specific functions already, move Hyper-V specific functions out of processor.c there.

No functional change intended.

---
 .../selftests/kvm/include/x86_64/hyperv.h     |  4 ++
 .../selftests/kvm/include/x86_64/processor.h  |  7 ++-
 .../testing/selftests/kvm/lib/x86_64/hyperv.c | 59 ++++++++++++++++++
 .../selftests/kvm/lib/x86_64/processor.c      | 61 -------------------
 .../selftests/kvm/x86_64/xen_vmcall_test.c    |  1 +
 5 files changed, 68 insertions(+), 64 deletions(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
index fa65b908b13e..a2e7cf7ee0ad 100644
@@ -343,4 +343,8 @@ struct hyperv_test_pages *vcpu_alloc_hyperv_test_pages(struct kvm_vm *vm,
 /* HV_X64_MSR_TSC_INVARIANT_CONTROL bits */
 #define HV_INVARIANT_TSC_EXPOSED               BIT_ULL(0)
 
+const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void);
+const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu);
+void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu);
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
tools/testing/selftests/kvm/include/x86_64/processor.h +4 -3
index a0c1440017bb..e247f99e0473 100644
@@ -25,6 +25,10 @@ extern bool host_cpu_is_intel;
 extern bool host_cpu_is_amd;
 extern uint64_t guest_tsc_khz;
 
+#ifndef MAX_NR_CPUID_ENTRIES
+#define MAX_NR_CPUID_ENTRIES 100
+#endif
+
 /* Forced emulation prefix, used to invoke the emulator unconditionally. */
 #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
 
@@ -908,8 +912,6 @@ static inline void vcpu_xcrs_set(struct kvm_vcpu *vcpu, struct kvm_xcrs *xcrs)
 const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,
 					       uint32_t function, uint32_t index);
 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);
 
 static inline uint32_t kvm_cpu_fms(void)
 {
@@ -1009,7 +1011,6 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)
 }
 
 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,
tools/testing/selftests/kvm/lib/x86_64/hyperv.c +59 -0

65 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -61

73 lines not on this page · raw message

tools/testing/selftests/kvm/x86_64/xen_vmcall_test.c +1 -0
index e149d0574961..2585087cdf5c 100644
@@ -10,6 +10,7 @@
 #include "test_util.h"
 #include "kvm_util.h"
 #include "processor.h"
+#include "hyperv.h"
 
 #define HCALL_REGION_GPA	0xc0000000ULL
 #define HCALL_REGION_SLOT	10
Signature · 1 line
2.46.0
#2 lore raw

PATCH 2/2 KVM: selftests: Re-enable hyperv_evmcs/hyperv_svm_test on bare metal

In reply to Vitaly Kuznetsov

KVM_CAP_HYPERV_DIRECT_TLBFLUSH is only reported when KVM runs on top of Hyper-V and hyperv_evmcs/hyperv_svm_test don't need that, these tests check that the feature is properly emulated for Hyper-V on KVM guests. There's no corresponding CAP for that, the feature is reported in KVM_GET_SUPPORTED_HV_CPUID.

Hyper-V specific CPUIDs are not reported by KVM_GET_SUPPORTED_CPUID, implement dedicated kvm_hv_cpu_has() helper to do the job.

Fixes 6dac1195181c ("KVM: selftests: Make Hyper-V tests explicitly require KVM Hyper-V support")
---
 .../testing/selftests/kvm/include/x86_64/hyperv.h  | 14 ++++++++++++++
 tools/testing/selftests/kvm/lib/x86_64/hyperv.c    |  8 ++++++++
 tools/testing/selftests/kvm/x86_64/hyperv_evmcs.c  |  2 +-
 .../testing/selftests/kvm/x86_64/hyperv_svm_test.c |  2 +-
 4 files changed, 24 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +14 -0
index a2e7cf7ee0ad..6849e2552f1b 100644
@@ -186,6 +186,18 @@
 #define HV_X64_ENLIGHTENED_VMCS_RECOMMENDED		\
 	KVM_X86_CPU_FEATURE(HYPERV_CPUID_ENLIGHTMENT_INFO, 0, EAX, 14)
 
+/* HYPERV_CPUID_NESTED_FEATURES.EAX */
+#define HV_X64_NESTED_DIRECT_FLUSH			\
+	KVM_X86_CPU_FEATURE(HYPERV_CPUID_NESTED_FEATURES, 0, EAX, 17)
+#define HV_X64_NESTED_GUEST_MAPPING_FLUSH		\
+	KVM_X86_CPU_FEATURE(HYPERV_CPUID_NESTED_FEATURES, 0, EAX, 18)
+#define HV_X64_NESTED_MSR_BITMAP			\
+	KVM_X86_CPU_FEATURE(HYPERV_CPUID_NESTED_FEATURES, 0, EAX, 19)
+
+/* HYPERV_CPUID_NESTED_FEATURES.EBX */
+#define HV_X64_NESTED_EVMCS1_PERF_GLOBAL_CTRL		\
+	KVM_X86_CPU_FEATURE(HYPERV_CPUID_NESTED_FEATURES, 0, EBX, 0)
+
 /* HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES.EAX */
 #define HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING	\
 	KVM_X86_CPU_FEATURE(HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES, 0, EAX, 1)
@@ -347,4 +359,6 @@ const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void);
 const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu);
 void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu);
 
+bool kvm_hv_cpu_has(struct kvm_x86_cpu_feature feature);
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
tools/testing/selftests/kvm/lib/x86_64/hyperv.c +8 -0
index b4a5e4ad7105..15bc8cd583aa 100644
@@ -67,6 +67,14 @@ const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu)
 	return cpuid;
 }
 
+bool kvm_hv_cpu_has(struct kvm_x86_cpu_feature feature)
+{
+	if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID))
+		return false;
+
+	return kvm_cpuid_has(kvm_get_supported_hv_cpuid(), feature);
+}
+
 struct hyperv_test_pages *vcpu_alloc_hyperv_test_pages(struct kvm_vm *vm,
 						       vm_vaddr_t *p_hv_pages_gva)
 {
tools/testing/selftests/kvm/x86_64/hyperv_evmcs.c +1 -1
index e192720bfe14..74cf19661309 100644
@@ -242,7 +242,7 @@ int main(int argc, char *argv[])
 	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));
-	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_DIRECT_TLBFLUSH));
+	TEST_REQUIRE(kvm_hv_cpu_has(HV_X64_NESTED_DIRECT_FLUSH));
 
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +1 -1
index b987a3d79715..0ddb63229bcb 100644
@@ -157,7 +157,7 @@ int main(int argc, char *argv[])
 	int stage;
 
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
-	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_DIRECT_TLBFLUSH));
+	TEST_REQUIRE(kvm_hv_cpu_has(HV_X64_NESTED_DIRECT_FLUSH));
 
 	/* Create VM */
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
Signature · 1 line
2.46.0
#3 lore raw
In reply to Vitaly Kuznetsov
Quoted 8 lines · Vitaly Kuznetsov

Commit 6dac1195181c ("KVM: selftests: Make Hyper-V tests explicitly require KVM Hyper-V support") wrongfully added KVM_CAP_HYPERV_DIRECT_TLBFLUSH requirement to hyperv_evmcs/hyperv_svm_test tests. The capability is only set when KVM runs on top of Hyper-V. The result is that both tests just skip when launched on bare metal. Add the required infrastructure and check for the correct CPUID bit in KVM_GET_SUPPORTED_HV_CPUID instead.

[...]

Applied to kvm-x86 selftests, thanks!

[1/2] KVM: selftests: Move Hyper-V specific functions out of processor.c

      https://github.com/kvm-x86/linux/commit/24a7e944966c
[2/2] KVM: selftests: Re-enable hyperv_evmcs/hyperv_svm_test on bare metal
      https://github.com/kvm-x86/linux/commit/d8414067cc17

--
https://github.com/kvm-x86/linux/tree/next