KVM: selftests: Add global snapshot of kvm_is_forced_emulation_enabled()
authorSean Christopherson <seanjc@google.com>
Thu, 14 Mar 2024 18:54:56 +0000 (11:54 -0700)
committerSean Christopherson <seanjc@google.com>
Mon, 29 Apr 2024 19:50:43 +0000 (12:50 -0700)
Add a global snapshot of kvm_is_forced_emulation_enabled() and sync it to
all VMs by default so that core library code can force emulation, e.g. to
allow for easier testing of the intersections between emulation and other
features in KVM.

Link: https://lore.kernel.org/r/20240314185459.2439072-4-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h
tools/testing/selftests/kvm/lib/x86_64/processor.c
tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c

index 9f1725192aa224be9a5ae12c5141903fb1a76ac5..41aba476640a34a70e929643627835bb0b7b8d5d 100644 (file)
@@ -5,6 +5,8 @@
 #include <stdbool.h>
 #include <stdint.h>
 
+extern bool is_forced_emulation_enabled;
+
 struct kvm_vm_arch {
        uint64_t c_bit;
        uint64_t s_bit;
index 9f87ca8b7ab6021e45b8104c471421396672337e..90aeacf114bf79d80da3bfdf9d19fef82dfa1c25 100644 (file)
@@ -23,6 +23,7 @@
 vm_vaddr_t exception_handlers;
 bool host_cpu_is_amd;
 bool host_cpu_is_intel;
+bool is_forced_emulation_enabled;
 
 static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
 {
@@ -577,6 +578,7 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
        vm_create_irqchip(vm);
        sync_global_to_guest(vm, host_cpu_is_intel);
        sync_global_to_guest(vm, host_cpu_is_amd);
+       sync_global_to_guest(vm, is_forced_emulation_enabled);
 
        if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
                struct kvm_sev_init init = { 0 };
@@ -1348,6 +1350,7 @@ void kvm_selftest_arch_init(void)
 {
        host_cpu_is_intel = this_cpu_is_intel();
        host_cpu_is_amd = this_cpu_is_amd();
+       is_forced_emulation_enabled = kvm_is_forced_emulation_enabled();
 }
 
 bool sys_clocksource_is_based_on_tsc(void)
index 842d87c8d6b60cdbc30ba9073404e37cdf5952ff..bad2ab1b981069acf7ca365199e1112fc8cc28d3 100644 (file)
@@ -19,7 +19,6 @@
 
 static uint8_t kvm_pmu_version;
 static bool kvm_has_perf_caps;
-static bool is_forced_emulation_enabled;
 
 static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu,
                                                  void *guest_code,
@@ -33,7 +32,6 @@ static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu,
        vcpu_init_descriptor_tables(*vcpu);
 
        sync_global_to_guest(vm, kvm_pmu_version);
-       sync_global_to_guest(vm, is_forced_emulation_enabled);
 
        /*
         * Set PERF_CAPABILITIES before PMU version as KVM disallows enabling
@@ -610,7 +608,6 @@ int main(int argc, char *argv[])
 
        kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);
        kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM);
-       is_forced_emulation_enabled = kvm_is_forced_emulation_enabled();
 
        test_intel_counters();
 
index 53afbea4df88a909547c128f8d78d6ea45545dcd..af9981a6642f238214e10647dde7b1cf06065125 100644 (file)
@@ -11,8 +11,6 @@
 #include "kvm_util.h"
 #include "vmx.h"
 
-static bool fep_available;
-
 #define MSR_NON_EXISTENT 0x474f4f00
 
 static u64 deny_bits = 0;
@@ -256,7 +254,7 @@ static void guest_code_filter_allow(void)
        GUEST_ASSERT(data == 2);
        GUEST_ASSERT(guest_exception_count == 0);
 
-       if (fep_available) {
+       if (is_forced_emulation_enabled) {
                /* Let userspace know we aren't done. */
                GUEST_SYNC(0);
 
@@ -518,8 +516,6 @@ KVM_ONE_VCPU_TEST(user_msr, msr_filter_allow, guest_code_filter_allow)
        uint64_t cmd;
        int rc;
 
-       sync_global_to_guest(vm, fep_available);
-
        rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
        TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available");
        vm_enable_cap(vm, KVM_CAP_X86_USER_SPACE_MSR, KVM_MSR_EXIT_REASON_FILTER);
@@ -549,7 +545,7 @@ KVM_ONE_VCPU_TEST(user_msr, msr_filter_allow, guest_code_filter_allow)
        vcpu_run(vcpu);
        cmd = process_ucall(vcpu);
 
-       if (fep_available) {
+       if (is_forced_emulation_enabled) {
                TEST_ASSERT_EQ(cmd, UCALL_SYNC);
                vm_install_exception_handler(vm, GP_VECTOR, guest_fep_gp_handler);
 
@@ -772,7 +768,5 @@ KVM_ONE_VCPU_TEST(user_msr, user_exit_msr_flags, NULL)
 
 int main(int argc, char *argv[])
 {
-       fep_available = kvm_is_forced_emulation_enabled();
-
        return test_harness_run(argc, argv);
 }