mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 03/15] KVM: x86: Let userspace leave an MSR access alone
Date: Fri, 27 Nov 2026 15:27:18 +0000	[thread overview]
Message-ID: <20261127152730.99319-4-dwmw2@infradead.org> (raw)
In-Reply-To: <20261127152730.99319-1-dwmw2@infradead.org>

From: David Woodhouse <dwmw@amazon.co.uk>

A VMM completing KVM_EXIT_X86_RDMSR or KVM_EXIT_X86_WRMSR can only have
KVM retire the instruction or inject #GP.  It cannot raise another
exception, or hand the access to something which retires it later.

With KVM_CAP_X86_MSR_EXIT_INSN, 'error' takes KVM_INSN_*: zero and one
mean what they did, KVM_INSN_UD injects #UD and KVM_INSN_RESUME makes
KVM do nothing.  Other values fail KVM_RUN.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
 Documentation/virt/kvm/api.rst  | 34 ++++++++++++++-
 arch/x86/include/asm/kvm_host.h |  2 +
 arch/x86/kvm/msrs.c             | 75 ++++++++++++++++++++++++++-------
 arch/x86/kvm/x86.c              |  8 ++++
 include/uapi/linux/kvm.h        |  3 +-
 5 files changed, 105 insertions(+), 17 deletions(-)

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index d15dddd79e5b..6d4a11ce368c 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -7441,7 +7441,7 @@ non-zero value, indicating that FEAT_LS64* is enabled.
 
 		/* KVM_EXIT_X86_RDMSR / KVM_EXIT_X86_WRMSR */
 		struct {
-			__u8 error; /* user -> kernel */
+			__u8 error; /* in: KVM_INSN_* */
 			__u8 pad[7];
 			__u32 reason; /* kernel -> user */
 			__u32 index; /* kernel -> user */
@@ -7477,6 +7477,17 @@ wants to write. Once finished processing the event, userspace must continue
 vCPU execution. If the MSR write was unsuccessful, userspace also sets the
 "error" field to "1".
 
+Any non-zero "error" injects the #GP, unless KVM_CAP_X86_MSR_EXIT_INSN
+is enabled.  "error" then takes one of the KVM_INSN_* values (see
+:ref:`the description of completion <kvm_insn_action>`), of which zero
+and one are the success and the #GP described above.  KVM_INSN_UD
+injects #UD instead, and KVM_INSN_RESUME tells KVM to do nothing for the
+access: "data" is ignored, no register is written, nothing is injected
+and RIP is left alone.  For a guest whose register state is protected
+(SEV-ES, SEV-SNP and TDX, a debuggable TD included) KVM can neither
+leave the access unanswered nor inject #UD, and every non-zero value
+fails the access like KVM_INSN_GP, with or without the capability.
+
 See KVM_X86_SET_MSR_FILTER for details on the interaction with MSR filtering.
 
 ::
@@ -9231,6 +9242,27 @@ enabled, cmma can't be enabled anymore and pfmfi and the storage key
 interpretation are disabled. If cmma has already been enabled or the
 hpage_2g module parameter is not set to 1, -EINVAL is returned.
 
+7.48 KVM_CAP_X86_MSR_EXIT_INSN
+------------------------------
+
+:Architectures: x86
+:Target: VM
+:Parameters: args[0] is 1 to enable, 0 to disable
+:Returns: 0 on success, -EINVAL if args[0] is any other value
+
+With this capability enabled, the "error" field of every
+KVM_EXIT_X86_RDMSR and KVM_EXIT_X86_WRMSR exit takes a KVM_INSN_* value,
+whichever of the KVM_MSR_EXIT_REASON_* reasons caused the exit.
+KVM_INSN_UD, and KVM_INSN_RESUME, which leaves the access for userspace
+to resolve, become available, and other values fail KVM_RUN with -EINVAL
+instead of injecting #GP.  It is disabled by default because existing
+VMMs may pass any non-zero value for a failed access.  The capability
+has no effect on which accesses exit; that is still selected with
+KVM_CAP_X86_USER_SPACE_MSR and KVM_X86_SET_MSR_FILTER.
+
+A guest whose register state is protected is the exception: see the
+description of those exits.
+
 8. Other capabilities.
 ======================
 
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 1ee4b127004d..0e8150116e20 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1297,6 +1297,8 @@ struct kvm_arch {
 	 * the opportunity to look at it.
 	 */
 	bool exit_on_emulation_error;
+	/* msr.error takes KVM_INSN_*, see KVM_CAP_X86_MSR_EXIT_INSN */
+	bool user_space_msr_insn;
 
 	/* Deflect RDMSR and WRMSR to user space when they trigger a #GP */
 	u32 user_space_msr_mask;
diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c
index e91650af44c2..6b1b93592c2e 100644
--- a/arch/x86/kvm/msrs.c
+++ b/arch/x86/kvm/msrs.c
@@ -952,12 +952,40 @@ fastpath_t handle_fastpath_wrmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int reg)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(handle_fastpath_wrmsr_imm);
 
+/*
+ * Act on what userspace answered in msr.error.  Returns 1 if nothing is left
+ * to do for the access, or -EINVAL; otherwise 0, with @fail set for the
+ * common completion, which retires the instruction or injects #GP by
+ * whatever means the guest needs.
+ *
+ * Without KVM_CAP_X86_MSR_EXIT_INSN any error is a #GP.  So it is for a
+ * guest with protected state, which kvm_insn_uncompleted() only completes.
+ */
+static int kvm_msr_exit_answer(struct kvm_vcpu *vcpu, bool *fail)
+{
+	u8 error = READ_ONCE(vcpu->run->msr.error);
+
+	*fail = error;
+
+	if (!vcpu->kvm->arch.user_space_msr_insn ||
+	    error == KVM_INSN_COMPLETE || error == KVM_INSN_GP)
+		return 0;
+
+	return kvm_insn_uncompleted(vcpu, error);
+}
+
 static void complete_userspace_rdmsr(struct kvm_vcpu *vcpu)
 {
-	if (!vcpu->run->msr.error) {
-		kvm_eax_write(vcpu, vcpu->run->msr.data);
-		kvm_edx_write(vcpu, vcpu->run->msr.data >> 32);
-	}
+	u64 data = READ_ONCE(vcpu->run->msr.data);
+
+	kvm_eax_write(vcpu, data);
+	kvm_edx_write(vcpu, data >> 32);
+}
+
+static void complete_userspace_rdmsr_imm(struct kvm_vcpu *vcpu)
+{
+	kvm_register_write(vcpu, vcpu->arch.cui_rdmsr_imm_reg,
+			   READ_ONCE(vcpu->run->msr.data));
 }
 
 static int complete_emulated_insn_gp(struct kvm_vcpu *vcpu, int err)
@@ -971,35 +999,52 @@ static int complete_emulated_insn_gp(struct kvm_vcpu *vcpu, int err)
 				       EMULTYPE_COMPLETE_USER_EXIT);
 }
 
+/*
+ * The one completion of an MSR exit: @emulated says whether the emulator or
+ * an intercept raised it, and @result writes what a successful read returned.
+ */
+static int complete_msr_access(struct kvm_vcpu *vcpu, bool emulated,
+			       void (*result)(struct kvm_vcpu *vcpu))
+{
+	bool fail;
+	int r;
+
+	r = kvm_msr_exit_answer(vcpu, &fail);
+	if (r)
+		return r;
+
+	if (!fail && result)
+		result(vcpu);
+
+	if (emulated)
+		return complete_emulated_insn_gp(vcpu, fail);
+
+	return kvm_x86_call(complete_emulated_msr)(vcpu, fail);
+}
+
 static int complete_emulated_msr_access(struct kvm_vcpu *vcpu)
 {
-	return complete_emulated_insn_gp(vcpu, vcpu->run->msr.error);
+	return complete_msr_access(vcpu, true, NULL);
 }
 
 static int complete_emulated_rdmsr(struct kvm_vcpu *vcpu)
 {
-	complete_userspace_rdmsr(vcpu);
-	return complete_emulated_msr_access(vcpu);
+	return complete_msr_access(vcpu, true, complete_userspace_rdmsr);
 }
 
 static int complete_fast_msr_access(struct kvm_vcpu *vcpu)
 {
-	return kvm_x86_call(complete_emulated_msr)(vcpu, vcpu->run->msr.error);
+	return complete_msr_access(vcpu, false, NULL);
 }
 
 static int complete_fast_rdmsr(struct kvm_vcpu *vcpu)
 {
-	complete_userspace_rdmsr(vcpu);
-	return complete_fast_msr_access(vcpu);
+	return complete_msr_access(vcpu, false, complete_userspace_rdmsr);
 }
 
 static int complete_fast_rdmsr_imm(struct kvm_vcpu *vcpu)
 {
-	if (!vcpu->run->msr.error)
-		kvm_register_write(vcpu, vcpu->arch.cui_rdmsr_imm_reg,
-				   vcpu->run->msr.data);
-
-	return complete_fast_msr_access(vcpu);
+	return complete_msr_access(vcpu, false, complete_userspace_rdmsr_imm);
 }
 
 static u64 kvm_msr_reason(int r)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 26e7beaff8a0..31c6759f38b9 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2276,6 +2276,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_SET_GUEST_DEBUG:
 	case KVM_CAP_LAST_CPU:
 	case KVM_CAP_X86_USER_SPACE_MSR:
+	case KVM_CAP_X86_MSR_EXIT_INSN:
 	case KVM_CAP_X86_MSR_FILTER:
 	case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
 #ifdef CONFIG_X86_SGX_KVM
@@ -4091,6 +4092,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		kvm->arch.user_space_msr_mask = cap->args[0];
 		r = 0;
 		break;
+	case KVM_CAP_X86_MSR_EXIT_INSN:
+		r = -EINVAL;
+		if (cap->args[0] > 1)
+			break;
+		kvm->arch.user_space_msr_insn = cap->args[0];
+		r = 0;
+		break;
 	case KVM_CAP_X86_BUS_LOCK_EXIT:
 		r = -EINVAL;
 		if (cap->args[0] & ~KVM_BUS_LOCK_DETECTION_VALID_MODE)
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 0a4c548cb329..a146c0487960 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -431,7 +431,7 @@ struct kvm_run {
 		} arm_nisv;
 		/* KVM_EXIT_X86_RDMSR / KVM_EXIT_X86_WRMSR */
 		struct {
-			__u8 error; /* user -> kernel */
+			__u8 error; /* in: KVM_INSN_* */
 			__u8 pad[7];
 #define KVM_MSR_EXIT_REASON_INVAL	(1 << 0)
 #define KVM_MSR_EXIT_REASON_UNKNOWN	(1 << 1)
@@ -1009,6 +1009,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_PPC_COMPAT_CAPS 250
 #define KVM_CAP_ARM_PMU_V3_STRICT 251
 #define KVM_CAP_GUEST_MEMFD_MEMORY_ATTRIBUTES 252
+#define KVM_CAP_X86_MSR_EXIT_INSN 253
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
-- 
2.50.1


  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 ` David Woodhouse [this message]
2026-11-27 15:27 ` [PATCH v3 04/15] KVM: selftests: Test MSR accesses which userspace leaves alone David Woodhouse
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-4-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®