[PATCH v3 03/15] KVM: x86: Let userspace leave an MSR access alone
From: David Woodhouse
Date: Fri Nov 27 2026 - 10:27:49 EST
From: David Woodhouse <dwmw@xxxxxxxxxxxx>
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@xxxxxxxxxxxx>
---
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