[PATCH v2 5/6] KVM: x86: WARN if MOV DR emulation hits a "too late" #GP

From: Sean Christopherson

Date: Wed Jun 03 2026 - 19:13:57 EST


WARN if ->set_dr() => kvm_set_dr() fails when emulating a MOV DR write,
as the emulator _must_ pre-check for #GPs in order to get the event
priority right when emulating MOV DR for L2 on SVM (all exceptions have
higher priority than the instruction intercept).

Opportunistically update the comment as the blurb about "#UD" being
checked is incomplete and misleading.

Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/kvm/emulate.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 1df2dcfd8bef..9d345ad25ac0 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -3298,8 +3298,12 @@ static int em_dr_write(struct x86_emulate_ctxt *ctxt)
else
val = ctxt->src.val & ~0U;

- /* #UD condition is already handled. */
- if (ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val))
+ /*
+ * A #GP due to an illegal value should be impossible at this point, as
+ * such #GPs have priority over MOV DR intercepts on SVM, i.e. KVM must
+ * manually check the value *before* emulating the write.
+ */
+ if (WARN_ON_ONCE(ctxt->ops->set_dr(ctxt, ctxt->modrm_reg, val)))
return emulate_gp(ctxt, 0);

/* Disable writeback. */
--
2.54.0.1032.g2f8565e1d1-goog