All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hao Zhang <hao_zhang_kdev@163.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] KVM: x86: Emulate SYSENTER in 64-bit mode for Intel-compatible vCPUs
Date: Wed, 21 Oct 2026 10:10:03 +0800	[thread overview]
Message-ID: <atge--CosCmrOcx6@192.168.1.215> (raw)

From: Hao Zhang <zhanghao1@kylinos.cn>

Intel allows SYSENTER in both IA-32e submodes, whereas AMD raises #UD
in either submode.  On AMD hosts, KVM intercepts #UD and emulates
SYSENTER to support Intel-compatible vCPUs.

However, em_sysenter() unconditionally rejects execution from 64-bit
mode.  As a result, a valid SYSENTER in an Intel-compatible guest can
fail emulation instead of entering the guest's kernel.  For a
non-nested guest at CPL0, this results in a
KVM_INTERNAL_ERROR_EMULATION exit to userspace even when
KVM_CAP_EXIT_ON_EMULATION_FAILURE is not enabled.

Remove the unconditional rejection.  The existing IA-32e path already
loads the full-width SYSENTER EIP and ESP values and installs the
required CS and SS attributes.

Also reject SYSENTER for AMD-compatible vCPUs whenever EFER.LMA is
set, covering both compatibility and 64-bit modes.  This preserves
AMD's architectural #UD behavior instead of attempting Intel-style
emulation or reporting an emulation failure.

See the SYSENTER instruction descriptions in Intel SDM Volume 2 and
AMD APM Volume 3.

Signed-off-by: Hao Zhang <zhanghao1@kylinos.cn>
---
 arch/x86/kvm/emulate.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index c1b21282187f..695957ebd871 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -2413,17 +2413,13 @@ static int em_sysenter(struct x86_emulate_ctxt *ctxt)
 		return emulate_gp(ctxt, 0);
 
 	/*
-	 * Intel's architecture allows SYSENTER in compatibility mode, but AMD
-	 * does not.  Note, AMD does allow SYSENTER in legacy protected mode.
+	 * Intel allows SYSENTER in both IA-32e submodes, but AMD does not
+	 * allow SYSENTER in either.  Both allow it in legacy protected mode.
 	 */
-	if ((ctxt->mode != X86EMUL_MODE_PROT64) && (efer & EFER_LMA) &&
+	if ((efer & EFER_LMA) &&
 	    !ctxt->ops->guest_cpuid_is_intel_compatible(ctxt))
 		return emulate_ud(ctxt);
 
-	/* sysenter/sysexit have not been tested in 64bit mode. */
-	if (ctxt->mode == X86EMUL_MODE_PROT64)
-		return X86EMUL_UNHANDLEABLE;
-
 	ops->get_msr(ctxt, MSR_IA32_SYSENTER_CS, &msr_data);
 	if ((msr_data & 0xfffc) == 0x0)
 		return emulate_gp(ctxt, 0);

base-commit: 6c377d19d4a5116d9bec5203aa3c6c11523e7898
-- 
2.15.0


                 reply	other threads:[~2026-10-21  2:10 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=atge--CosCmrOcx6@192.168.1.215 \
    --to=hao_zhang_kdev@163.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.