KVM: selftests: Query module param to detect FEP in MSR filtering test
authorSean Christopherson <seanjc@google.com>
Tue, 9 Jan 2024 23:02:44 +0000 (15:02 -0800)
committerSean Christopherson <seanjc@google.com>
Tue, 30 Jan 2024 23:29:42 +0000 (15:29 -0800)
Add a helper to detect KVM support for forced emulation by querying the
module param, and use the helper to detect support for the MSR filtering
test instead of throwing a noodle/NOP at KVM to see if it sticks.

Cc: Aaron Lewis <aaronlewis@google.com>
Tested-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Link: https://lore.kernel.org/r/20240109230250.424295-25-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/x86_64/processor.h
tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c

index ee082ae58f404546a8db62e0305e146af05145ea..d211cea188bee57e49ef764f99f9742138f70ef5 100644 (file)
@@ -1222,6 +1222,11 @@ static inline bool kvm_is_pmu_enabled(void)
        return get_kvm_param_bool("enable_pmu");
 }
 
+static inline bool kvm_is_forced_emulation_enabled(void)
+{
+       return !!get_kvm_param_integer("force_emulation_prefix");
+}
+
 uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
                                    int *level);
 uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr);
index 3533dc2fbfeeb136b217eb79e819e2e374e2b0cd..9e12dbc47a72f23f4329393658bfca3d13d44006 100644 (file)
@@ -14,8 +14,7 @@
 
 /* Forced emulation prefix, used to invoke the emulator unconditionally. */
 #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
-#define KVM_FEP_LENGTH 5
-static int fep_available = 1;
+static bool fep_available;
 
 #define MSR_NON_EXISTENT 0x474f4f00
 
@@ -260,13 +259,6 @@ static void guest_code_filter_allow(void)
        GUEST_ASSERT(data == 2);
        GUEST_ASSERT(guest_exception_count == 0);
 
-       /*
-        * Test to see if the instruction emulator is available (ie: the module
-        * parameter 'kvm.force_emulation_prefix=1' is set).  This instruction
-        * will #UD if it isn't available.
-        */
-       __asm__ __volatile__(KVM_FEP "nop");
-
        if (fep_available) {
                /* Let userspace know we aren't done. */
                GUEST_SYNC(0);
@@ -388,12 +380,6 @@ static void guest_fep_gp_handler(struct ex_regs *regs)
                           &em_wrmsr_start, &em_wrmsr_end);
 }
 
-static void guest_ud_handler(struct ex_regs *regs)
-{
-       fep_available = 0;
-       regs->rip += KVM_FEP_LENGTH;
-}
-
 static void check_for_guest_assert(struct kvm_vcpu *vcpu)
 {
        struct ucall uc;
@@ -531,9 +517,11 @@ static void test_msr_filter_allow(void)
 {
        struct kvm_vcpu *vcpu;
        struct kvm_vm *vm;
+       uint64_t cmd;
        int rc;
 
        vm = vm_create_with_one_vcpu(&vcpu, guest_code_filter_allow);
+       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");
@@ -561,11 +549,11 @@ static void test_msr_filter_allow(void)
        run_guest_then_process_wrmsr(vcpu, MSR_NON_EXISTENT);
        run_guest_then_process_rdmsr(vcpu, MSR_NON_EXISTENT);
 
-       vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
        vcpu_run(vcpu);
-       vm_install_exception_handler(vm, UD_VECTOR, NULL);
+       cmd = process_ucall(vcpu);
 
-       if (process_ucall(vcpu) != UCALL_DONE) {
+       if (fep_available) {
+               TEST_ASSERT_EQ(cmd, UCALL_SYNC);
                vm_install_exception_handler(vm, GP_VECTOR, guest_fep_gp_handler);
 
                /* Process emulated rdmsr and wrmsr instructions. */
@@ -583,6 +571,7 @@ static void test_msr_filter_allow(void)
                /* Confirm the guest completed without issues. */
                run_guest_then_process_ucall_done(vcpu);
        } else {
+               TEST_ASSERT_EQ(cmd, UCALL_DONE);
                printf("To run the instruction emulated tests set the module parameter 'kvm.force_emulation_prefix=1'\n");
        }
 
@@ -804,6 +793,8 @@ static void test_user_exit_msr_flags(void)
 
 int main(int argc, char *argv[])
 {
+       fep_available = kvm_is_forced_emulation_enabled();
+
        test_msr_filter_allow();
 
        test_msr_filter_deny();