[PATCH v3 04/15] KVM: selftests: Test MSR accesses which userspace leaves alone
From: David Woodhouse
Date: Fri Nov 27 2026 - 10:28:39 EST
From: David Woodhouse <dwmw@xxxxxxxxxxxx>
Cover intercepted and force-emulated RDMSR and WRMSR: without
KVM_CAP_X86_MSR_EXIT_INSN any error is a #GP; with it, each KVM_INSN_*
value, undefined ones, and KVM_INSN_RESUME with nothing changed, with a
queued #NM, with RIP moved, and across a single-step.
Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/x86/msr_exit_insn_test.c | 308 ++++++++++++++++++
2 files changed, 309 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86/msr_exit_insn_test.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 17a4256e9081..f0cb8cf748dd 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -96,6 +96,7 @@ TEST_GEN_PROGS_x86 += x86/kvm_clock_test
TEST_GEN_PROGS_x86 += x86/kvm_pv_test
TEST_GEN_PROGS_x86 += x86/kvm_buslock_test
TEST_GEN_PROGS_x86 += x86/monitor_mwait_test
+TEST_GEN_PROGS_x86 += x86/msr_exit_insn_test
TEST_GEN_PROGS_x86 += x86/msrs_test
TEST_GEN_PROGS_x86 += x86/nested_close_kvm_test
TEST_GEN_PROGS_x86 += x86/nested_dirty_log_test
diff --git a/tools/testing/selftests/kvm/x86/msr_exit_insn_test.c b/tools/testing/selftests/kvm/x86/msr_exit_insn_test.c
new file mode 100644
index 000000000000..9ff6c88de38a
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/msr_exit_insn_test.c
@@ -0,0 +1,308 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test KVM_CAP_X86_MSR_EXIT_INSN: without it, any non-zero 'error' of
+ * KVM_EXIT_X86_RDMSR and KVM_EXIT_X86_WRMSR is a #GP. With it, 'error'
+ * takes KVM_INSN_*: the #GP is KVM_INSN_GP, KVM_INSN_UD injects #UD,
+ * undefined values fail KVM_RUN, and KVM_INSN_RESUME leaves the
+ * instruction unretired: RIP and the registers stay as they were and no
+ * #GP is injected, so the guest runs the instruction again, or takes the
+ * exception userspace queued, or resumes where userspace moved RIP. Both
+ * intercepted and, with kvm.force_emulation_prefix, emulated accesses are
+ * covered. KVM steps over the prefix before it emulates, so an emulated
+ * access which is left alone is intercepted when the guest executes it
+ * again.
+ *
+ * Copyright © 2026 Amazon.com, Inc. or its affiliates.
+ */
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+
+#define TEST_MSR 0x474f4f00 /* Not implemented by KVM. */
+
+#define MSR_INSN_LEN 2
+
+#define WRITE_VAL 0x1122334455667788ULL
+#define DATA_IGNORED 0xa1a2a3a4a5a6a7a8ULL
+#define DATA_RET0 0xb1b2b3b4b5b6b7b8ULL
+#define DATA_RETRY 0xc1c2c3c4c5c6c7c8ULL
+#define DATA_MOVED 0xd1d2d3d4d5d6d7d8ULL
+#define DATA_STEP 0xe1e2e3e4e5e6e7e8ULL
+#define DATA_BAD 0xbad0bad1bad2bad3ULL
+
+static u8 guest_rdmsr(bool fep, u64 *val)
+{
+ *val = 0;
+ return fep ? rdmsr_safe_fep(TEST_MSR, val) : rdmsr_safe(TEST_MSR, val);
+}
+
+static u8 guest_wrmsr(bool fep)
+{
+ if (fep)
+ return kvm_asm_safe_fep("wrmsr", "a"(WRITE_VAL & -1u),
+ "d"(WRITE_VAL >> 32), "c"(TEST_MSR));
+
+ return wrmsr_safe(TEST_MSR, WRITE_VAL);
+}
+
+/* One read, then one write, which must end as userspace decided. */
+static void guest_access(bool fep, u8 vector, u64 data)
+{
+ u64 val;
+
+ GUEST_ASSERT_EQ(guest_rdmsr(fep, &val), vector);
+ if (!vector)
+ GUEST_ASSERT_EQ(val, data);
+ GUEST_ASSERT_EQ(guest_wrmsr(fep), vector);
+}
+
+static void guest_accesses(bool fep)
+{
+ /* Not enabled: zero completes, and anything else is a #GP. */
+ guest_access(fep, 0, DATA_IGNORED);
+ guest_access(fep, GP_VECTOR, 0);
+ guest_access(fep, GP_VECTOR, 0);
+ guest_access(fep, GP_VECTOR, 0);
+ GUEST_SYNC(fep);
+
+ /* Enabled: completed, then failed with #GP, then with #UD. */
+ guest_access(fep, 0, DATA_RET0);
+ guest_access(fep, GP_VECTOR, 0);
+ guest_access(fep, UD_VECTOR, 0);
+
+ /* Left alone, or refused by KVM_RUN: the instruction runs again. */
+ guest_access(fep, 0, DATA_RETRY);
+
+ /* Left alone with #NM queued by userspace. */
+ guest_access(fep, NM_VECTOR, 0);
+
+ /* Left alone and retired by userspace. */
+ guest_access(fep, 0, DATA_MOVED);
+
+ /* Left alone and then completed while single-stepping. */
+ guest_access(fep, 0, DATA_STEP);
+ GUEST_SYNC(fep);
+}
+
+static void guest_code(void)
+{
+ guest_accesses(false);
+ if (is_forced_emulation_enabled)
+ guest_accesses(true);
+ GUEST_DONE();
+}
+
+static void expect_msr_exit(struct kvm_vcpu *vcpu, bool write)
+{
+ struct kvm_run *run = vcpu->run;
+ struct ucall uc;
+
+ vcpu_run(vcpu);
+ if (run->exit_reason == KVM_EXIT_IO && get_ucall(vcpu, &uc) == UCALL_ABORT)
+ REPORT_GUEST_ASSERT(uc);
+
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, write ? KVM_EXIT_X86_WRMSR :
+ KVM_EXIT_X86_RDMSR);
+ TEST_ASSERT_EQ(run->msr.index, TEST_MSR);
+ TEST_ASSERT_EQ(run->msr.reason, KVM_MSR_EXIT_REASON_UNKNOWN);
+ TEST_ASSERT_EQ(run->msr.error, KVM_INSN_COMPLETE);
+ if (write)
+ TEST_ASSERT_EQ(run->msr.data, WRITE_VAL);
+}
+
+static void expect_sync(struct kvm_vcpu *vcpu, bool fep)
+{
+ struct ucall uc;
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_SYNC:
+ TEST_ASSERT_EQ(uc.args[1], fep);
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ default:
+ TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+ }
+}
+
+/*
+ * Leave the access alone, with 'data' set to what must not reach the guest,
+ * or give KVM_RUN a value it refuses, and check that the access comes back
+ * exactly as it was.
+ */
+static void leave_and_retake(struct kvm_vcpu *vcpu, bool write, u8 error)
+{
+ struct kvm_run *run = vcpu->run;
+ struct kvm_regs before, after;
+
+ /* The #UD exit of a forced emulation sets RF; an intercept does not. */
+ vcpu_regs_get(vcpu, &before);
+ before.rflags &= ~X86_EFLAGS_RF;
+ run->msr.error = error;
+ if (!write)
+ run->msr.data = DATA_BAD;
+
+ if (error > KVM_INSN_RESUME) {
+ TEST_ASSERT(__vcpu_run(vcpu) == -1 && errno == EINVAL,
+ "KVM_RUN accepted 'error' %u", error);
+ vcpu_regs_get(vcpu, &after);
+ after.rflags &= ~X86_EFLAGS_RF;
+ TEST_ASSERT(!memcmp(&before, &after, sizeof(before)),
+ "Registers changed by a failed KVM_RUN");
+ }
+
+ expect_msr_exit(vcpu, write);
+ vcpu_regs_get(vcpu, &after);
+ after.rflags &= ~X86_EFLAGS_RF;
+ TEST_ASSERT(!memcmp(&before, &after, sizeof(before)),
+ "Registers changed over a %s left alone: RIP 0x%llx -> 0x%llx, RAX 0x%llx -> 0x%llx, RDX 0x%llx -> 0x%llx, RFLAGS 0x%llx -> 0x%llx",
+ write ? "WRMSR" : "RDMSR", before.rip, after.rip,
+ before.rax, after.rax, before.rdx, after.rdx,
+ before.rflags, after.rflags);
+}
+
+static void complete(struct kvm_vcpu *vcpu, bool write, u8 error, u64 data)
+{
+ vcpu->run->msr.error = error;
+ if (!write)
+ vcpu->run->msr.data = data;
+}
+
+static void run_accesses(struct kvm_vcpu *vcpu, bool fep)
+{
+ struct kvm_guest_debug debug = {};
+ struct kvm_run *run = vcpu->run;
+ struct kvm_vcpu_events events;
+ struct kvm_regs regs;
+ int write;
+
+ /* Not enabled: any non-zero 'error' is a #GP. */
+ vm_enable_cap(vcpu->vm, KVM_CAP_X86_MSR_EXIT_INSN, 0);
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ complete(vcpu, write, 0, DATA_IGNORED);
+ }
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ complete(vcpu, write, KVM_INSN_RESUME, DATA_BAD);
+ }
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ complete(vcpu, write, KVM_INSN_UD, DATA_BAD);
+ }
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ complete(vcpu, write, 0x80, DATA_BAD);
+ }
+ expect_sync(vcpu, fep);
+ vm_enable_cap(vcpu->vm, KVM_CAP_X86_MSR_EXIT_INSN, 1);
+
+ /* Complete and #GP: as before. And #UD. */
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ complete(vcpu, write, KVM_INSN_COMPLETE, DATA_RET0);
+ }
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ complete(vcpu, write, KVM_INSN_GP, DATA_BAD);
+ }
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ complete(vcpu, write, KVM_INSN_UD, DATA_BAD);
+ }
+
+ /*
+ * Resume: no #GP, nothing written, the same exit again. A reserved
+ * value fails KVM_RUN and leaves the guest the same way.
+ */
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ leave_and_retake(vcpu, write, KVM_INSN_RESUME);
+ leave_and_retake(vcpu, write, KVM_INSN_RESUME + 1);
+ leave_and_retake(vcpu, write, KVM_INSN_RESUME);
+ leave_and_retake(vcpu, write, 0xff);
+ complete(vcpu, write, KVM_INSN_COMPLETE, DATA_RETRY);
+ }
+
+ /* Resume with an exception queued: taken at the instruction. */
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ run->msr.error = KVM_INSN_RESUME;
+ vcpu_events_get(vcpu, &events);
+ TEST_ASSERT(!events.exception.pending && !events.exception.injected,
+ "Exception queued at an MSR exit");
+ events.exception.injected = 1;
+ events.exception.nr = NM_VECTOR;
+ events.exception.has_error_code = 0;
+ vcpu_events_set(vcpu, &events);
+ }
+
+ /* Resume with RIP moved: userspace performed the access. */
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ complete(vcpu, write, KVM_INSN_RESUME, DATA_BAD);
+ vcpu_regs_get(vcpu, ®s);
+ regs.rip += MSR_INSN_LEN;
+ if (!write) {
+ regs.rax = DATA_MOVED & -1u;
+ regs.rdx = DATA_MOVED >> 32;
+ }
+ vcpu_regs_set(vcpu, ®s);
+ }
+
+ /*
+ * Single-step: an access which is left alone has not retired, so
+ * there is no debug exit until it is completed.
+ */
+ for (write = 0; write <= 1; write++) {
+ expect_msr_exit(vcpu, write);
+ vcpu_regs_get(vcpu, ®s);
+ debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
+ vcpu_guest_debug_set(vcpu, &debug);
+ leave_and_retake(vcpu, write, KVM_INSN_RESUME);
+ complete(vcpu, write, KVM_INSN_COMPLETE, DATA_STEP);
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_DEBUG);
+ TEST_ASSERT_EQ(run->debug.arch.pc, regs.rip + MSR_INSN_LEN);
+ debug.control = 0;
+ vcpu_guest_debug_set(vcpu, &debug);
+ }
+ expect_sync(vcpu, fep);
+}
+
+int main(int argc, char *argv[])
+{
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ struct ucall uc;
+
+ TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_USER_SPACE_MSR));
+ TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_MSR_EXIT_INSN));
+ /* With kvm.ignore_msrs the access to TEST_MSR never exits. */
+ TEST_REQUIRE(!kvm_is_ignore_msrs());
+ TEST_ASSERT_EQ(kvm_check_cap(KVM_CAP_X86_MSR_EXIT_INSN), 1);
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+ TEST_ASSERT(__vm_enable_cap(vm, KVM_CAP_X86_MSR_EXIT_INSN, 2) == -1 &&
+ errno == EINVAL,
+ "KVM_CAP_X86_MSR_EXIT_INSN accepted an unknown argument");
+ vm_enable_cap(vm, KVM_CAP_X86_USER_SPACE_MSR, KVM_MSR_EXIT_REASON_UNKNOWN);
+
+ run_accesses(vcpu, false);
+ if (is_forced_emulation_enabled)
+ run_accesses(vcpu, true);
+ else
+ pr_info("Set kvm.force_emulation_prefix=1 to test emulated MSR accesses\n");
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+ if (get_ucall(vcpu, &uc) == UCALL_ABORT)
+ REPORT_GUEST_ASSERT(uc);
+ TEST_ASSERT_EQ(uc.cmd, UCALL_DONE);
+
+ kvm_vm_free(vm);
+ return 0;
+}
--
2.50.1