Skip to content

Commit e1ff115

Browse files
committed
KVM: selftests: Add global snapshot of kvm_is_forced_emulation_enabled()
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>
1 parent 73369ac commit e1ff115

File tree

4 files changed

+7
-11
lines changed

4 files changed

+7
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <stdbool.h>
66
#include <stdint.h>
77

8+
extern bool is_forced_emulation_enabled;
9+
810
struct kvm_vm_arch {
911
uint64_t c_bit;
1012
uint64_t s_bit;

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
vm_vaddr_t exception_handlers;
2424
bool host_cpu_is_amd;
2525
bool host_cpu_is_intel;
26+
bool is_forced_emulation_enabled;
2627

2728
static void regs_dump(FILE *stream, struct kvm_regs *regs, uint8_t indent)
2829
{
@@ -577,6 +578,7 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
577578
vm_create_irqchip(vm);
578579
sync_global_to_guest(vm, host_cpu_is_intel);
579580
sync_global_to_guest(vm, host_cpu_is_amd);
581+
sync_global_to_guest(vm, is_forced_emulation_enabled);
580582

581583
if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
582584
struct kvm_sev_init init = { 0 };
@@ -1348,6 +1350,7 @@ void kvm_selftest_arch_init(void)
13481350
{
13491351
host_cpu_is_intel = this_cpu_is_intel();
13501352
host_cpu_is_amd = this_cpu_is_amd();
1353+
is_forced_emulation_enabled = kvm_is_forced_emulation_enabled();
13511354
}
13521355

13531356
bool sys_clocksource_is_based_on_tsc(void)

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
static uint8_t kvm_pmu_version;
2121
static bool kvm_has_perf_caps;
22-
static bool is_forced_emulation_enabled;
2322

2423
static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu,
2524
void *guest_code,
@@ -33,7 +32,6 @@ static struct kvm_vm *pmu_vm_create_with_one_vcpu(struct kvm_vcpu **vcpu,
3332
vcpu_init_descriptor_tables(*vcpu);
3433

3534
sync_global_to_guest(vm, kvm_pmu_version);
36-
sync_global_to_guest(vm, is_forced_emulation_enabled);
3735

3836
/*
3937
* Set PERF_CAPABILITIES before PMU version as KVM disallows enabling
@@ -610,7 +608,6 @@ int main(int argc, char *argv[])
610608

611609
kvm_pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);
612610
kvm_has_perf_caps = kvm_cpu_has(X86_FEATURE_PDCM);
613-
is_forced_emulation_enabled = kvm_is_forced_emulation_enabled();
614611

615612
test_intel_counters();
616613

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include "kvm_util.h"
1212
#include "vmx.h"
1313

14-
static bool fep_available;
15-
1614
#define MSR_NON_EXISTENT 0x474f4f00
1715

1816
static u64 deny_bits = 0;
@@ -256,7 +254,7 @@ static void guest_code_filter_allow(void)
256254
GUEST_ASSERT(data == 2);
257255
GUEST_ASSERT(guest_exception_count == 0);
258256

259-
if (fep_available) {
257+
if (is_forced_emulation_enabled) {
260258
/* Let userspace know we aren't done. */
261259
GUEST_SYNC(0);
262260

@@ -518,8 +516,6 @@ KVM_ONE_VCPU_TEST(user_msr, msr_filter_allow, guest_code_filter_allow)
518516
uint64_t cmd;
519517
int rc;
520518

521-
sync_global_to_guest(vm, fep_available);
522-
523519
rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
524520
TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available");
525521
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)
549545
vcpu_run(vcpu);
550546
cmd = process_ucall(vcpu);
551547

552-
if (fep_available) {
548+
if (is_forced_emulation_enabled) {
553549
TEST_ASSERT_EQ(cmd, UCALL_SYNC);
554550
vm_install_exception_handler(vm, GP_VECTOR, guest_fep_gp_handler);
555551

@@ -772,7 +768,5 @@ KVM_ONE_VCPU_TEST(user_msr, user_exit_msr_flags, NULL)
772768

773769
int main(int argc, char *argv[])
774770
{
775-
fep_available = kvm_is_forced_emulation_enabled();
776-
777771
return test_harness_run(argc, argv);
778772
}

0 commit comments

Comments
 (0)