From: David Woodhouse <dwmw2@infradead.org>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Sean Christopherson" <seanjc@google.com>,
"Vitaly Kuznetsov" <vkuznets@redhat.com>,
"Marcelo Tosatti" <mtosatti@redhat.com>,
"Joerg Roedel" <joro@8bytes.org>,
"Tom Lendacky" <thomas.lendacky@amd.com>,
"James Bottomley" <James.Bottomley@HansenPartnership.com>,
"Thara Gopinath" <tgopinath@linux.microsoft.com>,
"Wei Liu" <wei.liu@kernel.org>,
"Michael Kelley" <mhklinux@outlook.com>,
"Nicolas Saenz Julienne" <nsaenz@amazon.com>,
"Nikolas Wipper" <nik.wipper@gmx.de>,
"Mushahid Hussain" <hmushi@amazon.co.uk>,
"Sriram Nambakam" <snambakam@linux.microsoft.com>,
"Zack Rusin" <zack.rusin@broadcom.com>,
"Doug Covelli" <doug.covelli@broadcom.com>,
"Jim Mattson" <jmattson@google.com>,
"Thomas Gleixner" <tglx@kernel.org>,
"Ingo Molnar" <mingo@redhat.com>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Paul Durrant" <paul@xen.org>, "Xin Li" <xin@zytor.com>,
"Francesco Lavra" <flavra@baylibre.com>,
"Radim Krčmář" <rkrcmar@ventanamicro.com>,
"Liran Alon" <liran.alon@oracle.com>,
"Arbel Moshe" <arbel.moshe@oracle.com>,
"David Woodhouse" <dwmw@amazon.co.uk>
Subject: [PATCH v3 04/15] KVM: selftests: Test MSR accesses which userspace leaves alone
Date: Fri, 27 Nov 2026 15:27:19 +0000 [thread overview]
Message-ID: <20261127152730.99319-5-dwmw2@infradead.org> (raw)
In-Reply-To: <20261127152730.99319-1-dwmw2@infradead.org>
From: David Woodhouse <dwmw@amazon.co.uk>
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@amazon.co.uk>
---
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
next prev parent reply other threads:[~2026-11-27 15:27 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 15:27 [PATCH v3 00/15] KVM: x86: Exit handling for Hyper-V hcalls, register intercept David Woodhouse
2026-11-27 15:27 ` [PATCH v3 01/15] KVM: selftests: Make the x86 SSE register helpers volatile David Woodhouse
2026-11-27 15:27 ` [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction David Woodhouse
2026-11-27 15:27 ` [PATCH v3 03/15] KVM: x86: Let userspace leave an MSR access alone David Woodhouse
2026-11-27 15:27 ` David Woodhouse [this message]
2026-11-27 15:27 ` [PATCH v3 05/15] KVM: x86: hyper-v: Add KVM_CAP_HYPERV_FORWARD_HCALL David Woodhouse
2026-11-27 15:27 ` [PATCH v3 06/15] KVM: x86: hyper-v: Let userspace leave a hypercall uncompleted David Woodhouse
2026-11-27 15:27 ` [PATCH v3 07/15] KVM: x86: hyper-v: Advertise XMM fast hypercall output David Woodhouse
2026-11-27 15:27 ` [PATCH v3 08/15] KVM: selftests: Add Hyper-V hypercall forwarding test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 09/15] KVM: x86: Add a per-vCPU register write filter David Woodhouse
2026-11-27 15:27 ` [PATCH v3 10/15] KVM: x86: Filter emulated CR0 and CR4 writes David Woodhouse
2026-11-27 15:27 ` [PATCH v3 11/15] KVM: VMX: Support CR0 and CR4 write filters David Woodhouse
2026-11-27 15:27 ` [PATCH v3 12/15] KVM: SVM: " David Woodhouse
2026-11-27 15:27 ` [PATCH v3 13/15] KVM: x86: Add descriptor-table registers to the write filter David Woodhouse
2026-11-27 15:27 ` [PATCH v3 14/15] KVM: selftests: Add x86 register write filter test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 15/15] KVM: selftests: Test the register write filter with a nested guest David Woodhouse
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127152730.99319-5-dwmw2@infradead.org \
--to=dwmw2@infradead.org \
--cc=James.Bottomley@HansenPartnership.com \
--cc=arbel.moshe@oracle.com \
--cc=bp@alien8.de \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.com \
--cc=doug.covelli@broadcom.com \
--cc=dwmw@amazon.co.uk \
--cc=flavra@baylibre.com \
--cc=hmushi@amazon.co.uk \
--cc=hpa@zytor.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=liran.alon@oracle.com \
--cc=mhklinux@outlook.com \
--cc=mingo@redhat.com \
--cc=mtosatti@redhat.com \
--cc=nik.wipper@gmx.de \
--cc=nsaenz@amazon.com \
--cc=paul@xen.org \
--cc=pbonzini@redhat.com \
--cc=rdunlap@infradead.org \
--cc=rkrcmar@ventanamicro.com \
--cc=seanjc@google.com \
--cc=skhan@linuxfoundation.org \
--cc=snambakam@linux.microsoft.com \
--cc=tglx@kernel.org \
--cc=tgopinath@linux.microsoft.com \
--cc=thomas.lendacky@amd.com \
--cc=vkuznets@redhat.com \
--cc=wei.liu@kernel.org \
--cc=x86@kernel.org \
--cc=xin@zytor.com \
--cc=zack.rusin@broadcom.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®