Skip to content

Commit 0326cc6

Browse files
committed
KVM: selftests: Query module param to detect FEP in MSR filtering test
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 <[email protected]> Tested-by: Dapeng Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 45e4755 commit 0326cc6

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,11 @@ static inline bool kvm_is_pmu_enabled(void)
12221222
return get_kvm_param_bool("enable_pmu");
12231223
}
12241224

1225+
static inline bool kvm_is_forced_emulation_enabled(void)
1226+
{
1227+
return !!get_kvm_param_integer("force_emulation_prefix");
1228+
}
1229+
12251230
uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
12261231
int *level);
12271232
uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr);

tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
/* Forced emulation prefix, used to invoke the emulator unconditionally. */
1616
#define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
17-
#define KVM_FEP_LENGTH 5
18-
static int fep_available = 1;
17+
static bool fep_available;
1918

2019
#define MSR_NON_EXISTENT 0x474f4f00
2120

@@ -260,13 +259,6 @@ static void guest_code_filter_allow(void)
260259
GUEST_ASSERT(data == 2);
261260
GUEST_ASSERT(guest_exception_count == 0);
262261

263-
/*
264-
* Test to see if the instruction emulator is available (ie: the module
265-
* parameter 'kvm.force_emulation_prefix=1' is set). This instruction
266-
* will #UD if it isn't available.
267-
*/
268-
__asm__ __volatile__(KVM_FEP "nop");
269-
270262
if (fep_available) {
271263
/* Let userspace know we aren't done. */
272264
GUEST_SYNC(0);
@@ -388,12 +380,6 @@ static void guest_fep_gp_handler(struct ex_regs *regs)
388380
&em_wrmsr_start, &em_wrmsr_end);
389381
}
390382

391-
static void guest_ud_handler(struct ex_regs *regs)
392-
{
393-
fep_available = 0;
394-
regs->rip += KVM_FEP_LENGTH;
395-
}
396-
397383
static void check_for_guest_assert(struct kvm_vcpu *vcpu)
398384
{
399385
struct ucall uc;
@@ -531,9 +517,11 @@ static void test_msr_filter_allow(void)
531517
{
532518
struct kvm_vcpu *vcpu;
533519
struct kvm_vm *vm;
520+
uint64_t cmd;
534521
int rc;
535522

536523
vm = vm_create_with_one_vcpu(&vcpu, guest_code_filter_allow);
524+
sync_global_to_guest(vm, fep_available);
537525

538526
rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
539527
TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available");
@@ -561,11 +549,11 @@ static void test_msr_filter_allow(void)
561549
run_guest_then_process_wrmsr(vcpu, MSR_NON_EXISTENT);
562550
run_guest_then_process_rdmsr(vcpu, MSR_NON_EXISTENT);
563551

564-
vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
565552
vcpu_run(vcpu);
566-
vm_install_exception_handler(vm, UD_VECTOR, NULL);
553+
cmd = process_ucall(vcpu);
567554

568-
if (process_ucall(vcpu) != UCALL_DONE) {
555+
if (fep_available) {
556+
TEST_ASSERT_EQ(cmd, UCALL_SYNC);
569557
vm_install_exception_handler(vm, GP_VECTOR, guest_fep_gp_handler);
570558

571559
/* Process emulated rdmsr and wrmsr instructions. */
@@ -583,6 +571,7 @@ static void test_msr_filter_allow(void)
583571
/* Confirm the guest completed without issues. */
584572
run_guest_then_process_ucall_done(vcpu);
585573
} else {
574+
TEST_ASSERT_EQ(cmd, UCALL_DONE);
586575
printf("To run the instruction emulated tests set the module parameter 'kvm.force_emulation_prefix=1'\n");
587576
}
588577

@@ -804,6 +793,8 @@ static void test_user_exit_msr_flags(void)
804793

805794
int main(int argc, char *argv[])
806795
{
796+
fep_available = kvm_is_forced_emulation_enabled();
797+
807798
test_msr_filter_allow();
808799

809800
test_msr_filter_deny();

0 commit comments

Comments
 (0)