mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sohil Mehta <sohil.mehta@intel.com>
To: kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"H . Peter Anvin" <hpa@zytor.com>, Xin Li <xin@zytor.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Nikunj A Dadhania <nikunj@amd.com>,
	Shivansh Dhiman <shivansh.dhiman@amd.com>,
	Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>,
	David Woodhouse <dwmw@amazon.co.uk>,
	Chao Gao <chao.gao@intel.com>,
	Binbin Wu <binbin.wu@linux.intel.com>,
	Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>,
	"Chang S . Bae" <chang.seok.bae@intel.com>,
	Sohil Mehta <sohil.mehta@intel.com>,
	Yosry Ahmed <yosry@kernel.org>,
	David Matlack <dmatlack@google.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception
Date: Wed,  2 Dec 2026 00:02:23 -0800	[thread overview]
Message-ID: <20261202080224.4165883-4-sohil.mehta@intel.com> (raw)
In-Reply-To: <20261202080224.4165883-1-sohil.mehta@intel.com>

ICEBP (INT1) generates a #DB with the event type of a privileged software
exception. KVM always intercepts #DB on VMX, and handles the #DB of an
ICEBP by skipping the instruction and injecting a hardware exception. A
guest that delivers events through the IDT can't tell the difference.

However, L1 can. If the delivery of the #DB to L2 causes a VM-exit to
L1, L1 finds a hardware exception in the IDT-vectoring information,
without an instruction length, and L2's RIP already past the ICEBP.
With FRED, which pushes the event type onto the stack, the guest itself
will see a hardware exception too.

Re-inject the #DB as a privileged software exception instead, with the
instruction length from the VM-exit, and let VM-entry advance RIP past
the ICEBP. Keep clearing STI and MOV SS blocking, as skipping the
instruction did.

If userspace saves the vCPU before the #DB is delivered, KVM doesn't
report the #DB, as for INT3 and INTO, and the guest re-executes the
ICEBP. RIP still points at it.

An ICEBP in enclave mode causes a VM-exit as a hardware exception, so
it's unaffected. SVM is unchanged, as it can inject a privileged
software exception only for FRED guests.

Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reported-by: David Woodhouse <dwmw2@infradead.org>
Closes: https://lore.kernel.org/all/7f93eb25874ddd13a1ad6e3c75785f11041c8b7f.camel@infradead.org/
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
 arch/x86/kvm/vmx/vmx.c | 36 +++++++++++++++++++++---------------
 arch/x86/kvm/x86.c     |  7 +++++++
 arch/x86/kvm/x86.h     |  2 ++
 3 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index fdc531a61fd3..72d16282b559 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -1883,13 +1883,12 @@ void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
 	/*
 	 * Per the SDM, MTF takes priority over debug-trap exceptions besides
 	 * TSS T-bit traps and ICEBP (INT1).  KVM doesn't emulate T-bit traps
-	 * or ICEBP (in the emulator proper), and skipping of ICEBP after an
-	 * intercepted #DB deliberately avoids single-step #DB and MTF updates
-	 * as ICEBP is higher priority than both.  As instruction emulation is
-	 * completed at this point (i.e. KVM is at the instruction boundary),
-	 * any #DB exception pending delivery must be a debug-trap of lower
-	 * priority than MTF.  Record the pending MTF state to be delivered in
-	 * vmx_check_nested_events().
+	 * or ICEBP (in the emulator proper), and doesn't skip an intercepted
+	 * ICEBP either, so T-bit traps and ICEBP never get here.  As
+	 * instruction emulation is completed at this point (i.e. KVM is at the
+	 * instruction boundary), any #DB exception pending delivery must be a
+	 * debug-trap of lower priority than MTF.  Record the pending MTF state
+	 * to be delivered in vmx_check_nested_events().
 	 */
 	if (nested_cpu_has_mtf(vmcs12) &&
 	    (!vcpu->arch.exception.pending ||
@@ -5533,17 +5532,24 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 		if (!(vcpu->guest_debug &
 		      (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
 			/*
-			 * If the #DB was due to ICEBP, a.k.a. INT1, skip the
-			 * instruction.  ICEBP generates a trap-like #DB, but
+			 * ICEBP, a.k.a. INT1, generates a trap-like #DB, but
 			 * despite its interception control being tied to #DB,
 			 * is an instruction intercept, i.e. the VM-Exit occurs
-			 * on the ICEBP itself.  Use the inner "skip" helper to
-			 * avoid single-step #DB and MTF updates, as ICEBP is
-			 * higher priority.  Note, skipping ICEBP still clears
-			 * STI and MOVSS blocking.
+			 * on the ICEBP itself. Re-inject the #DB as what it
+			 * is, a privileged software exception, so that VM-Entry
+			 * advances RIP past the ICEBP using its length. Clear
+			 * STI and MOVSS blocking, as retiring the ICEBP would.
+			 * Note, an ICEBP in enclave mode exits as a hardware
+			 * exception, so it never gets here.
 			 */
-			if (is_icebp(intr_info))
-				WARN_ON_ONCE(!skip_emulated_instruction(vcpu));
+			if (is_icebp(intr_info)) {
+				vcpu->arch.event_exit_inst_len =
+					vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
+				vmx_set_interrupt_shadow(vcpu, 0);
+				kvm_queue_exception_p_type(vcpu, DB_VECTOR, dr6,
+							   EVENT_TYPE_PRIV_SWEXC);
+				return 1;
+			}
 
 			kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
 			return 1;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index b58518a777de..44d24f1b8109 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -549,6 +549,13 @@ void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_p);
 
+void kvm_queue_exception_p_type(struct kvm_vcpu *vcpu, unsigned int nr,
+				unsigned long payload, u8 event_type)
+{
+	kvm_multiple_exception(vcpu, nr, false, 0, true, payload, event_type);
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_p_type);
+
 static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
 				    u32 error_code, unsigned long payload)
 {
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index 00218af728d4..91941f12b835 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -515,6 +515,8 @@ int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err);
 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
 void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, unsigned long payload);
+void kvm_queue_exception_p_type(struct kvm_vcpu *vcpu, unsigned int nr,
+				unsigned long payload, u8 event_type);
 void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,
 			   bool has_error_code, u32 error_code, u8 event_type);
 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault,
-- 
2.43.0


  parent reply	other threads:[~2026-12-02  8:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  8:02 [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP " Sohil Mehta
2026-12-02  8:02 ` [PATCH 1/4] KVM: x86: Track the event type of a queued exception Sohil Mehta
2026-12-02  8:02 ` [PATCH 2/4] KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is interrupted Sohil Mehta
2026-12-02  8:02 ` Sohil Mehta [this message]
2026-12-02  8:02 ` [PATCH 4/4] KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2 Sohil Mehta

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=20261202080224.4165883-4-sohil.mehta@intel.com \
    --to=sohil.mehta@intel.com \
    --cc=Neeraj.Upadhyay@amd.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=binbin.wu@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=chang.seok.bae@intel.com \
    --cc=chao.gao@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=dmatlack@google.com \
    --cc=dwmw@amazon.co.uk \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=maciej.wieczor-retman@intel.com \
    --cc=mingo@redhat.com \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    --cc=shivansh.dhiman@amd.com \
    --cc=skhan@linuxfoundation.org \
    --cc=tglx@kernel.org \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    --cc=xin@zytor.com \
    --cc=yosry@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

all inboxes | Powered by JetHome®