The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: weichenchen <weichen.chen@linux.alibaba.com>
To: kvm@vger.kernel.org, seanjc@google.com, pbonzini@redhat.com
Cc: weichenchen <weichen.chen@linux.alibaba.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org, gleb@redhat.com,
	srutherford@google.com, avi@redhat.com
Subject: [PATCH v2] KVM: x86: Fix stalled descriptor table accesses to MMIO
Date: Thu, 19 Nov 2026 13:57:36 +0800	[thread overview]
Message-ID: <20261119055747.23819-1-weichen.chen@linux.alibaba.com> (raw)

Use the emulated memory accessors for the explicit operands of LGDT,
LIDT, SGDT and SIDT.  When the operand has no backing memslot, the
standard accessors return X86EMUL_IO_NEEDED without queuing an MMIO
fragment.  LIDT can repeat the generic operand read at the same RIP,
and SIDT can re-enter emulation without sending a write to userspace.

Check the segment bounds of both fields, then read the whole descriptor
in one emulated access.  On a single page, this lets KVM use the
faulting GPA for both fields without translating the base field after
userspace has handled part of the read.  For a split operand, translate
both pages before exiting to userspace.  Use the emulator's read cache
to resume after MMIO and skip the redundant generic source read.

Use the destination operand for common writeback of SGDT and SIDT.  This
keeps the entire descriptor available until userspace completes a
ten-byte MMIO write.

Fixes: c3cd7ffaf57a ("KVM: x86 emulator: x86_emulate_insn() return -1 only in case of emulation failure")
Fixes: 129a72a0d3c8 ("KVM: x86: Introduce segmented_write_std")
Signed-off-by: weichenchen <weichen.chen@linux.alibaba.com>
---
Changes in v2:
- Read the whole LGDT/LIDT operand in one read_emulated() call after
  validating both fields. This avoids a second GVA translation after
  userspace has handled the first part of a same-page read, as pointed
  out by Sashiko.
- v1: https://lore.kernel.org/kvm/20261119032912.94364-1-weichen.chen@linux.alibaba.com/

On Intel VMX/EPT, the 64-bit LIDT and SIDT probes reached the following
HLT with the expected data for RAM and MMIO operands. For same-page
MMIO, both transferred eight bytes followed by two. A TDP-style mock
with a saved GPA counted one GPA lookup and zero software translations
through completion for both loads at all three operand sizes.

Six KVM selftests passed, including a split RAM/MMIO LIDT and a segment
limit fault before any userspace MMIO exit. Eight kvm-unit-tests suites
at a2891d9528ef reported 445 passes, five skips and no failures. Both
vendor modules built with KVM_WERROR. Runtime testing used Intel.

Codex assisted with the emulator changes and regression testing.

 arch/x86/kvm/emulate.c | 49 +++++++++++++++++++-----------------------
 1 file changed, 22 insertions(+), 27 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index c1b2128218..3a9a67f4b0 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -918,23 +918,6 @@ static void *decode_register(struct x86_emulate_ctxt *ctxt, u8 modrm_reg,
 	return p;
 }
 
-static int read_descriptor(struct x86_emulate_ctxt *ctxt,
-			   struct segmented_address addr,
-			   u16 *size, unsigned long *address, int op_bytes)
-{
-	int rc;
-
-	if (op_bytes == 2)
-		op_bytes = 3;
-	*address = 0;
-	rc = segmented_read_std(ctxt, addr, size, 2);
-	if (rc != X86EMUL_CONTINUE)
-		return rc;
-	addr.ea += 2;
-	rc = segmented_read_std(ctxt, addr, address, op_bytes);
-	return rc;
-}
-
 EM_ASM_2(add);
 EM_ASM_2(or);
 EM_ASM_2(adc);
@@ -3447,10 +3430,9 @@ static int emulate_store_desc_ptr(struct x86_emulate_ctxt *ctxt,
 		ctxt->op_bytes = 4;
 		desc_ptr.address &= 0x00ffffff;
 	}
-	/* Disable writeback. */
-	ctxt->dst.type = OP_NONE;
-	return segmented_write_std(ctxt, ctxt->dst.addr.mem,
-				   &desc_ptr, 2 + ctxt->op_bytes);
+	ctxt->dst.bytes = 2 + ctxt->op_bytes;
+	memcpy(ctxt->dst.valptr, &desc_ptr, ctxt->dst.bytes);
+	return X86EMUL_CONTINUE;
 }
 
 static int em_sgdt(struct x86_emulate_ctxt *ctxt)
@@ -3465,14 +3447,27 @@ static int em_sidt(struct x86_emulate_ctxt *ctxt)
 
 static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
 {
-	struct desc_ptr desc_ptr;
+	struct segmented_address addr = ctxt->src.addr.mem;
+	struct desc_ptr desc_ptr = {};
+	unsigned long limit_addr, base_addr;
+	unsigned int base_bytes, desc_bytes;
 	int rc;
 
 	if (ctxt->mode == X86EMUL_MODE_PROT64)
 		ctxt->op_bytes = 8;
-	rc = read_descriptor(ctxt, ctxt->src.addr.mem,
-			     &desc_ptr.size, &desc_ptr.address,
-			     ctxt->op_bytes);
+	base_bytes = ctxt->op_bytes == 2 ? 3 : ctxt->op_bytes;
+	desc_bytes = sizeof(desc_ptr.size) + base_bytes;
+
+	/* Check both fields before an MMIO read can have side effects. */
+	rc = linearize(ctxt, addr, sizeof(desc_ptr.size), false, &limit_addr);
+	if (rc != X86EMUL_CONTINUE)
+		return rc;
+	addr.ea += sizeof(desc_ptr.size);
+	rc = linearize(ctxt, addr, base_bytes, false, &base_addr);
+	if (rc != X86EMUL_CONTINUE)
+		return rc;
+
+	rc = read_emulated(ctxt, limit_addr, &desc_ptr, desc_bytes);
 	if (rc != X86EMUL_CONTINUE)
 		return rc;
 	if (ctxt->mode == X86EMUL_MODE_PROT64 &&
@@ -4074,8 +4069,8 @@ static const struct opcode group6[] = {
 static const struct group_dual group7 = { {
 	II(Mov | DstMem,			em_sgdt, sgdt),
 	II(Mov | DstMem,			em_sidt, sidt),
-	II(SrcMem | Priv,			em_lgdt, lgdt),
-	II(SrcMem | Priv,			em_lidt, lidt),
+	II(SrcMem | Priv | NoAccess,		em_lgdt, lgdt),
+	II(SrcMem | Priv | NoAccess,		em_lidt, lidt),
 	II(SrcNone | DstMem | Mov,		em_smsw, smsw), N,
 	II(SrcMem16 | Mov | Priv,		em_lmsw, lmsw),
 	II(SrcMem | ByteOp | Priv | NoAccess,	em_invlpg, invlpg),

base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7
-- 
2.53.0


                 reply	other threads:[~2026-11-19  5:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261119055747.23819-1-weichen.chen@linux.alibaba.com \
    --to=weichen.chen@linux.alibaba.com \
    --cc=avi@redhat.com \
    --cc=gleb@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=srutherford@google.com \
    --cc=x86@kernel.org \
    /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