* [PATCH 1/4] KVM: x86: Track the event type of a queued exception
2026-12-02 8:02 [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception Sohil Mehta
@ 2026-12-02 8:02 ` 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
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Sohil Mehta @ 2026-12-02 8:02 UTC (permalink / raw)
To: kvm
Cc: Paolo Bonzini, Sean Christopherson, Shuah Khan, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Xin Li, Peter Zijlstra, Andrew Cooper, Tom Lendacky,
Nikunj A Dadhania, Shivansh Dhiman, Neeraj Upadhyay,
David Woodhouse, Chao Gao, Binbin Wu, Maciej Wieczor-Retman,
Chang S . Bae, Sohil Mehta, Yosry Ahmed, David Matlack, x86,
linux-kernel, linux-kselftest
KVM derives the event type of an exception from its vector when it
injects the exception. However, the vector can't tell an INT1 (ICEBP)
from any other #DB. An INT1 raises a #DB that is a privileged software
exception, while every other #DB is a hardware exception.
Add the event type to the queued exception, so that KVM can inject an
INT1 as what it is. Derive it from the vector, except in
__vmx_complete_interrupts(), where VMX requeues an exception whose
delivery was interrupted. Take the type from the IDT-vectoring
information there.
The event type isn't part of the vCPU events uAPI, so derive it from the
vector when userspace sets an exception.
No functional change intended.
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
arch/x86/include/asm/kvm_host.h | 2 ++
arch/x86/kvm/svm/svm.c | 2 +-
arch/x86/kvm/vmx/vmx.c | 4 +++-
arch/x86/kvm/x86.c | 32 ++++++++++++++++++++++----------
arch/x86/kvm/x86.h | 12 +++++++++++-
5 files changed, 39 insertions(+), 13 deletions(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 3efef3415f8e..0b8d854f4a2f 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -687,6 +687,8 @@ struct kvm_queued_exception {
u32 error_code;
unsigned long payload;
bool has_payload;
+ /* Architectural EVENT_TYPE_* */
+ u8 event_type;
};
/*
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 721dc5e7e13a..bd775f02bd49 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -4502,7 +4502,7 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
kvm_requeue_exception(vcpu, vector,
exitintinfo & SVM_EXITINTINFO_VALID_ERR,
- error_code);
+ error_code, kvm_exception_event_type(vector));
break;
}
case SVM_EXITINTINFO_TYPE_INTR:
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 68fa251dda60..a6c0e8ccdfad 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -7189,6 +7189,8 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
fallthrough;
case INTR_TYPE_HARD_EXCEPTION: {
+ /* VMX's INTR_TYPE_* are the architectural EVENT_TYPE_* << 8. */
+ u8 event_type = type >> 8;
u32 error_code = 0;
if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK)
@@ -7196,7 +7198,7 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
kvm_requeue_exception(vcpu, vector,
idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK,
- error_code);
+ error_code, event_type);
break;
}
case INTR_TYPE_SOFT_INTR:
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2cd9ee316aad..c27813bde407 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -454,7 +454,8 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_deliver_exception_payload);
static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vector,
bool has_error_code, u32 error_code,
- bool has_payload, unsigned long payload)
+ bool has_payload, unsigned long payload,
+ u8 event_type)
{
struct kvm_queued_exception *ex = &vcpu->arch.exception_vmexit;
@@ -467,11 +468,13 @@ static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vecto
ex->error_code = error_code;
ex->has_payload = has_payload;
ex->payload = payload;
+ ex->event_type = event_type;
}
static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,
bool has_error, u32 error_code,
- bool has_payload, unsigned long payload)
+ bool has_payload, unsigned long payload,
+ u8 event_type)
{
u32 prev_nr;
int class1, class2;
@@ -485,7 +488,7 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,
if (is_guest_mode(vcpu) &&
kvm_nested_call(is_exception_vmexit)(vcpu, nr, error_code)) {
kvm_queue_exception_vmexit(vcpu, nr, has_error, error_code,
- has_payload, payload);
+ has_payload, payload, event_type);
return;
}
@@ -499,6 +502,7 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,
vcpu->arch.exception.error_code = error_code;
vcpu->arch.exception.has_payload = has_payload;
vcpu->arch.exception.payload = payload;
+ vcpu->arch.exception.event_type = event_type;
return;
}
@@ -531,7 +535,8 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned int nr,
void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
{
- kvm_multiple_exception(vcpu, nr, false, 0, false, 0);
+ kvm_multiple_exception(vcpu, nr, false, 0, false, 0,
+ kvm_exception_event_type(nr));
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception);
@@ -539,18 +544,20 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception);
void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
unsigned long payload)
{
- kvm_multiple_exception(vcpu, nr, false, 0, true, payload);
+ kvm_multiple_exception(vcpu, nr, false, 0, true, payload,
+ kvm_exception_event_type(nr));
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_p);
static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
u32 error_code, unsigned long payload)
{
- kvm_multiple_exception(vcpu, nr, true, error_code, true, payload);
+ kvm_multiple_exception(vcpu, nr, true, error_code, true, payload,
+ kvm_exception_event_type(nr));
}
void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,
- bool has_error_code, u32 error_code)
+ bool has_error_code, u32 error_code, u8 event_type)
{
/*
@@ -575,6 +582,7 @@ void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,
vcpu->arch.exception.error_code = error_code;
vcpu->arch.exception.has_payload = false;
vcpu->arch.exception.payload = 0;
+ vcpu->arch.exception.event_type = event_type;
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_requeue_exception);
@@ -601,7 +609,8 @@ void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault,
if (is_guest_mode(vcpu) && fault->async_page_fault)
kvm_queue_exception_vmexit(vcpu, PF_VECTOR,
true, fault->error_code,
- true, fault->address);
+ true, fault->address,
+ kvm_exception_event_type(PF_VECTOR));
else
kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
fault->address);
@@ -639,7 +648,8 @@ void kvm_inject_nmi(struct kvm_vcpu *vcpu)
void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
{
- kvm_multiple_exception(vcpu, nr, true, error_code, false, 0);
+ kvm_multiple_exception(vcpu, nr, true, error_code, false, 0,
+ kvm_exception_event_type(nr));
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_queue_exception_e);
@@ -3100,6 +3110,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
vcpu->arch.exception.error_code = events->exception.error_code;
vcpu->arch.exception.has_payload = events->exception_has_payload;
vcpu->arch.exception.payload = events->exception_payload;
+ vcpu->arch.exception.event_type = kvm_exception_event_type(events->exception.nr);
vcpu->arch.interrupt.injected = events->interrupt.injected;
vcpu->arch.interrupt.nr = events->interrupt.nr;
@@ -9006,7 +9017,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
kvm_nested_call(is_exception_vmexit)(vcpu, ex->vector, ex->error_code)) {
kvm_queue_exception_vmexit(vcpu, ex->vector,
ex->has_error_code, ex->error_code,
- ex->has_payload, ex->payload);
+ ex->has_payload, ex->payload,
+ ex->event_type);
ex->injected = false;
ex->pending = false;
}
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index 43850b524774..00218af728d4 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -6,6 +6,7 @@
#include <asm/fpu/xstate.h>
#include <asm/mce.h>
#include <asm/pvclock.h>
+#include <asm/trapnr.h>
#include "msrs.h"
#include "mmu.h"
#include "regs.h"
@@ -185,6 +186,15 @@ static inline bool kvm_exception_is_soft(unsigned int nr)
return (nr == BP_VECTOR) || (nr == OF_VECTOR);
}
+/*
+ * The vector can't identify the #DB of an INT1, a privileged software
+ * exception, so that one has to be queued with its event type explicitly.
+ */
+static inline u8 kvm_exception_event_type(unsigned int nr)
+{
+ return kvm_exception_is_soft(nr) ? EVENT_TYPE_SWEXC : EVENT_TYPE_HWEXC;
+}
+
static inline bool x86_exception_has_error_code(unsigned int vector)
{
static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
@@ -506,7 +516,7 @@ 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_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,
- bool has_error_code, u32 error_code);
+ bool has_error_code, u32 error_code, u8 event_type);
void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault,
bool from_hardware);
void __kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH 2/4] KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is interrupted
2026-12-02 8:02 [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception 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 ` Sohil Mehta
2026-12-02 8:02 ` [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception Sohil Mehta
2026-12-02 8:02 ` [PATCH 4/4] KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2 Sohil Mehta
3 siblings, 0 replies; 6+ messages in thread
From: Sohil Mehta @ 2026-12-02 8:02 UTC (permalink / raw)
To: kvm
Cc: Paolo Bonzini, Sean Christopherson, Shuah Khan, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Xin Li, Peter Zijlstra, Andrew Cooper, Tom Lendacky,
Nikunj A Dadhania, Shivansh Dhiman, Neeraj Upadhyay,
David Woodhouse, Chao Gao, Binbin Wu, Maciej Wieczor-Retman,
Chang S . Bae, Sohil Mehta, Yosry Ahmed, David Matlack, x86,
linux-kernel, linux-kselftest
Currently, KVM doesn't inject a privileged software exception itself,
but L1 can inject an INT1 into L2. If a VM-exit interrupts the delivery
of the INT1, KVM drops it, as it requeues only hardware and software
exceptions. For example, a delivery is interrupted when it touches an
IDT or stack page that isn't mapped yet.
If KVM handles the VM-exit itself, L2 then resumes without the INT1. If
L1 handles it, L1 doesn't find the INT1 in the IDT-vectoring information
and can't re-inject it.
Handle an INT1 the way KVM handles a software exception, which also
needs its instruction length. Requeue it with its length, re-inject it
with its own event type, and report it to L1 with that type and length.
Check the event type before the vector, as L1 can inject a privileged
software exception with any vector.
When the INT1 goes through a task gate, KVM emulates the task switch.
Clear the requeued INT1 there, so that KVM doesn't inject it twice. And
leave a queued INT1 out of the vCPU events, as the uAPI doesn't provide
a mechanism to carry its length.
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
Patch 3 relies on this patch, as it makes KVM inject an ICEBP's #DB as
a privileged software exception. Without this patch, KVM would inject
that #DB as a hardware exception with RIP still on the ICEBP. The guest
would then execute the ICEBP forever, and patch 4's test would hang.
No Fixes tag, as KVM has dropped this event type ever since nested VMX
started passing L1's injected events through to L2.
---
arch/x86/kvm/vmx/nested.c | 13 ++++++++++---
arch/x86/kvm/vmx/vmx.c | 12 ++++++++++--
arch/x86/kvm/x86.c | 11 ++++++-----
3 files changed, 26 insertions(+), 10 deletions(-)
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 1a98f28651f7..6cb07873998d 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -3970,12 +3970,17 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
nr = vcpu->arch.exception.vector;
idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
- if (kvm_exception_is_soft(nr)) {
+ if (vcpu->arch.exception.event_type == EVENT_TYPE_PRIV_SWEXC) {
+ vmcs12->vm_exit_instruction_len =
+ vcpu->arch.event_exit_inst_len;
+ idt_vectoring |= INTR_TYPE_PRIV_SW_EXCEPTION;
+ } else if (kvm_exception_is_soft(nr)) {
vmcs12->vm_exit_instruction_len =
vcpu->arch.event_exit_inst_len;
idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
- } else
+ } else {
idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
+ }
if (vcpu->arch.exception.has_error_code) {
idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
@@ -4085,7 +4090,9 @@ static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu)
intr_info |= INTR_INFO_DELIVER_CODE_MASK;
}
- if (kvm_exception_is_soft(ex->vector))
+ if (ex->event_type == EVENT_TYPE_PRIV_SWEXC)
+ intr_info |= INTR_TYPE_PRIV_SW_EXCEPTION;
+ else if (kvm_exception_is_soft(ex->vector))
intr_info |= INTR_TYPE_SOFT_EXCEPTION;
else
intr_info |= INTR_TYPE_HARD_EXCEPTION;
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index a6c0e8ccdfad..fdc531a61fd3 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -1973,12 +1973,18 @@ void vmx_inject_exception(struct kvm_vcpu *vcpu)
WARN_ON_ONCE(vmx->vt.emulation_required);
- if (kvm_exception_is_soft(ex->vector)) {
+ /* L1 can inject a privileged software exception with any vector. */
+ if (ex->event_type == EVENT_TYPE_PRIV_SWEXC) {
+ vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
+ vmx->vcpu.arch.event_exit_inst_len);
+ intr_info |= INTR_TYPE_PRIV_SW_EXCEPTION;
+ } else if (kvm_exception_is_soft(ex->vector)) {
vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
vmx->vcpu.arch.event_exit_inst_len);
intr_info |= INTR_TYPE_SOFT_EXCEPTION;
- } else
+ } else {
intr_info |= INTR_TYPE_HARD_EXCEPTION;
+ }
vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
@@ -5946,6 +5952,7 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)
vmcs_read32(IDT_VECTORING_ERROR_CODE);
}
fallthrough;
+ case INTR_TYPE_PRIV_SW_EXCEPTION:
case INTR_TYPE_SOFT_EXCEPTION:
kvm_clear_exception_queue(vcpu);
break;
@@ -7185,6 +7192,7 @@ static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
*/
vmx_set_nmi_mask(vcpu, false);
break;
+ case INTR_TYPE_PRIV_SW_EXCEPTION:
case INTR_TYPE_SOFT_EXCEPTION:
vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
fallthrough;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index c27813bde407..b58518a777de 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -3010,12 +3010,13 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
memset(events, 0, sizeof(*events));
/*
- * The API doesn't provide the instruction length for software
- * exceptions, so don't report them. As long as the guest RIP
- * isn't advanced, we should expect to encounter the exception
- * again.
+ * The API doesn't provide the instruction length that software
+ * exceptions and INT1 need, so don't report them. As long as the
+ * guest RIP isn't advanced, we should expect to encounter the
+ * exception again.
*/
- if (!kvm_exception_is_soft(ex->vector)) {
+ if (!kvm_exception_is_soft(ex->vector) &&
+ ex->event_type != EVENT_TYPE_PRIV_SWEXC) {
events->exception.injected = ex->injected;
events->exception.pending = ex->pending;
/*
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception
2026-12-02 8:02 [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception 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
2026-12-02 8:35 ` sashiko-bot
2026-12-02 8:02 ` [PATCH 4/4] KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2 Sohil Mehta
3 siblings, 1 reply; 6+ messages in thread
From: Sohil Mehta @ 2026-12-02 8:02 UTC (permalink / raw)
To: kvm
Cc: Paolo Bonzini, Sean Christopherson, Shuah Khan, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Xin Li, Peter Zijlstra, Andrew Cooper, Tom Lendacky,
Nikunj A Dadhania, Shivansh Dhiman, Neeraj Upadhyay,
David Woodhouse, Chao Gao, Binbin Wu, Maciej Wieczor-Retman,
Chang S . Bae, Sohil Mehta, Yosry Ahmed, David Matlack, x86,
linux-kernel, linux-kselftest
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
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception
2026-12-02 8:02 ` [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception Sohil Mehta
@ 2026-12-02 8:35 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-02 8:35 UTC (permalink / raw)
To: Sohil Mehta, kvm
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: x86: unhandled ICEBP payload in
kvm_vcpu_ioctl_x86_get_vcpu_events()
--
commit 52a98c13d9a8d8b249575bef58fd19c330f0a0f1
Author: Sohil Mehta <sohil.mehta@intel.com>
KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception
This commit re-injects an intercepted ICEBP #DB on VMX as a privileged
software exception with the VM-exit instruction length instead of skipping
the instruction and queueing a hardware exception.
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index fdc531a61fd30..72d16282b5598 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
[ ... ]
> @@ -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;
> + }
[Severity: Medium]
Can queueing an ICEBP #DB with a payload and EVENT_TYPE_PRIV_SWEXC corrupt
guest state if KVM exits to userspace (for example on a pending signal)
before injecting the exception?
Previously, exceptions omitted from KVM_GET_VCPU_EVENTS never had both
ex->pending and ex->has_payload set. Now an intercepted ICEBP queues a
pending EVENT_TYPE_PRIV_SWEXC with ex->has_payload = true while leaving
GUEST_RIP at the ICEBP instruction:
arch/x86/kvm/x86.c:kvm_handle_exception_payload_quirk() {
...
if (!vcpu->kvm->arch.exception_payload_enabled &&
ex->pending && ex->has_payload)
kvm_deliver_exception_payload(vcpu, ex);
}
arch/x86/kvm/x86.c:kvm_vcpu_ioctl_x86_get_vcpu_events() {
...
if (!kvm_exception_is_soft(ex->vector) &&
ex->event_type != EVENT_TYPE_PRIV_SWEXC) {
events->exception.injected = ex->injected;
events->exception.pending = ex->pending;
...
}
events->exception.nr = ex->vector;
events->exception.has_error_code = ex->has_error_code;
events->exception.error_code = ex->error_code;
events->exception_has_payload = ex->has_payload;
events->exception_payload = ex->payload;
...
}
When KVM_CAP_EXCEPTION_PAYLOAD is disabled,
kvm_handle_exception_payload_quirk() delivers the DR6 payload into
vcpu->arch.dr6 (clearing B0..B3 and updating DR6_RTM) even though
KVM_GET_VCPU_EVENTS drops the exception so the guest will re-execute ICEBP.
When KVM_CAP_EXCEPTION_PAYLOAD is enabled, KVM_GET_VCPU_EVENTS reports
events->exception_has_payload = 1 with pending = 0 and injected = 0.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202080224.4165883-1-sohil.mehta@intel.com?part=3
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 4/4] KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2
2026-12-02 8:02 [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception Sohil Mehta
` (2 preceding siblings ...)
2026-12-02 8:02 ` [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception Sohil Mehta
@ 2026-12-02 8:02 ` Sohil Mehta
3 siblings, 0 replies; 6+ messages in thread
From: Sohil Mehta @ 2026-12-02 8:02 UTC (permalink / raw)
To: kvm
Cc: Paolo Bonzini, Sean Christopherson, Shuah Khan, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
Xin Li, Peter Zijlstra, Andrew Cooper, Tom Lendacky,
Nikunj A Dadhania, Shivansh Dhiman, Neeraj Upadhyay,
David Woodhouse, Chao Gao, Binbin Wu, Maciej Wieczor-Retman,
Chang S . Bae, Sohil Mehta, Yosry Ahmed, David Matlack, x86,
linux-kernel, linux-kselftest
KVM always intercepts #DB on VMX, so it handles every ICEBP that L2
executes, unless L1 intercepts #DB itself. L1 must still see the #DB of
an ICEBP like it would on bare metal.
Add a nested VMX test with three cases. Each one ends with L1 resuming
L2 with the event, and L2's #DB handler returning past the ICEBP:
1) L1 intercepts #DB. The VM-exit reports a privileged software
exception of length 1, at the ICEBP. This is the reference.
2) L1 doesn't intercept #DB, and the #DB's delivery faults on an
unmapped stack. The #PF VM-exit to L1 must report the #DB as in
case 1, in the IDT-vectoring information.
3) L1 injects an INT1 itself, and its delivery faults the same way.
The test needs nested VMX, but not FRED.
Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
Without patch 3, L1 finds a hardware exception for the ICEBP's #DB in
case 2. Without patch 2, L2 loops on the ICEBP in case 1, and the test
hangs.
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
tools/testing/selftests/kvm/include/x86/vmx.h | 7 +
.../selftests/kvm/x86/vmx_icebp_test.c | 174 ++++++++++++++++++
3 files changed, 182 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86/vmx_icebp_test.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 95867977beb3..d65cc2ac35ed 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -143,6 +143,7 @@ TEST_GEN_PROGS_x86 += x86/userspace_msr_exit_test
TEST_GEN_PROGS_x86 += x86/vmx_apic_access_test
TEST_GEN_PROGS_x86 += x86/vmx_apicv_updates_test
TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
+TEST_GEN_PROGS_x86 += x86/vmx_icebp_test
TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
TEST_GEN_PROGS_x86 += x86/vmx_nested_la57_state_test
diff --git a/tools/testing/selftests/kvm/include/x86/vmx.h b/tools/testing/selftests/kvm/include/x86/vmx.h
index 8fd97434eee5..6e6e4bc97f15 100644
--- a/tools/testing/selftests/kvm/include/x86/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86/vmx.h
@@ -99,6 +99,13 @@
#define VMX_EPT_VPID_CAP_1G_PAGES 0x00020000
#define VMX_EPT_VPID_CAP_AD_BITS 0x00200000
+#define INTR_INFO_VECTOR_MASK 0x000000ff
+#define INTR_INFO_INTR_TYPE_MASK 0x00000700
+#define INTR_INFO_DELIVER_CODE_MASK 0x00000800
+#define INTR_INFO_VALID_MASK 0x80000000
+
+#define INTR_TYPE_PRIV_SW_EXCEPTION 0x00000500
+
#define EXIT_REASON_FAILED_VMENTRY 0x80000000
enum vmcs_field {
diff --git a/tools/testing/selftests/kvm/x86/vmx_icebp_test.c b/tools/testing/selftests/kvm/x86/vmx_icebp_test.c
new file mode 100644
index 000000000000..0d54c9838777
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/vmx_icebp_test.c
@@ -0,0 +1,174 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Verify that L1 sees the #DB of an ICEBP (INT1) in L2 as a privileged software
+ * exception, like it would on bare metal, both when L1 intercepts the #DB and
+ * when its delivery faults into L1.
+ */
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "vmx.h"
+
+/* A canonical address that the guest page tables don't map. */
+#define UNMAPPED_RSP 0x700000000000
+
+static volatile int db_count;
+static volatile u64 db_rip;
+
+static void l2_db_handler(struct ex_regs *regs)
+{
+ db_count++;
+ db_rip = regs->rip;
+}
+
+/*
+ * L2 code. The #DB handler returns to the VMCALL after each ICEBP, and to the
+ * VMCALL after the NOP at which L1 injects an INT1. It's plain asm so that
+ * nothing touches the unmapped stack before the ICEBP or the injected INT1.
+ */
+extern char l2_icebp[], l2_icebp_bad_stack[], l2_icebp_bad_stack_insn[], l2_int1[];
+asm(".pushsection .text\n"
+ "l2_icebp:\n"
+ " .byte 0xf1\n"
+ " vmcall\n"
+ " ud2\n"
+ "l2_icebp_bad_stack:\n"
+ " movabs $" __stringify(UNMAPPED_RSP) ", %rsp\n"
+ "l2_icebp_bad_stack_insn:\n"
+ " .byte 0xf1\n"
+ " vmcall\n"
+ " ud2\n"
+ "l2_int1:\n"
+ " nop\n"
+ " vmcall\n"
+ " ud2\n"
+ ".popsection");
+
+/* Re-inject the event that L1 was told about, and check its delivery. */
+static void reinject(struct vmx_pages *vmx, u32 intr_info, u32 insn_len,
+ u64 expected_rip)
+{
+ db_count = 0;
+ vmwrite(EXCEPTION_BITMAP, 0);
+ vmwrite(VM_ENTRY_INTR_INFO_FIELD,
+ intr_info & (INTR_INFO_VECTOR_MASK | INTR_INFO_INTR_TYPE_MASK |
+ INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK));
+ vmwrite(VM_ENTRY_INSTRUCTION_LEN, insn_len);
+ vmwrite(GUEST_RSP, (u64)vmx->stack);
+
+ vmresume();
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_VMCALL);
+ GUEST_ASSERT_EQ(db_count, 1);
+ GUEST_ASSERT_EQ(db_rip, expected_rip);
+}
+
+/*
+ * Check the #PF exit that interrupted the delivery of an INT1 at @rip, then
+ * re-inject the INT1 and check that L2 takes it.
+ */
+static void check_int1_vectoring(struct vmx_pages *vmx, u64 rip)
+{
+ u32 idt_info = vmread(IDT_VECTORING_INFO_FIELD);
+
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_EXCEPTION_NMI);
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_INTR_INFO) & INTR_INFO_VECTOR_MASK, PF_VECTOR);
+ __GUEST_ASSERT(idt_info & INTR_INFO_VALID_MASK,
+ "No event in the IDT-vectoring info (0x%x)", idt_info);
+ GUEST_ASSERT_EQ(idt_info & INTR_INFO_VECTOR_MASK, DB_VECTOR);
+ GUEST_ASSERT_EQ(idt_info & INTR_INFO_INTR_TYPE_MASK, INTR_TYPE_PRIV_SW_EXCEPTION);
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_INSTRUCTION_LEN), 1);
+ GUEST_ASSERT_EQ(vmread(GUEST_RIP), rip);
+
+ reinject(vmx, idt_info, vmread(VM_EXIT_INSTRUCTION_LEN), rip + 1);
+}
+
+static void l1_icebp_intercepted(struct vmx_pages *vmx)
+{
+ u32 intr_info;
+
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+
+ prepare_vmcs(vmx, l2_icebp);
+ vmwrite(EXCEPTION_BITMAP, BIT(DB_VECTOR));
+
+ vmlaunch();
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_REASON), EXIT_REASON_EXCEPTION_NMI);
+ intr_info = vmread(VM_EXIT_INTR_INFO);
+ GUEST_ASSERT_EQ(intr_info & INTR_INFO_VECTOR_MASK, DB_VECTOR);
+ GUEST_ASSERT_EQ(intr_info & INTR_INFO_INTR_TYPE_MASK, INTR_TYPE_PRIV_SW_EXCEPTION);
+ GUEST_ASSERT_EQ(vmread(VM_EXIT_INSTRUCTION_LEN), 1);
+ GUEST_ASSERT_EQ(vmread(GUEST_RIP), (u64)l2_icebp);
+
+ reinject(vmx, intr_info, 1, (u64)l2_icebp + 1);
+ GUEST_DONE();
+}
+
+static void l1_icebp_delivery_fault(struct vmx_pages *vmx)
+{
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+
+ prepare_vmcs(vmx, l2_icebp_bad_stack);
+ vmwrite(EXCEPTION_BITMAP, BIT(PF_VECTOR));
+
+ vmlaunch();
+ check_int1_vectoring(vmx, (u64)l2_icebp_bad_stack_insn);
+ GUEST_DONE();
+}
+
+static void l1_int1_delivery_fault(struct vmx_pages *vmx)
+{
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+
+ prepare_vmcs(vmx, l2_int1);
+ vmwrite(EXCEPTION_BITMAP, BIT(PF_VECTOR));
+ vmwrite(GUEST_RSP, UNMAPPED_RSP);
+ vmwrite(VM_ENTRY_INTR_INFO_FIELD,
+ INTR_INFO_VALID_MASK | INTR_TYPE_PRIV_SW_EXCEPTION | DB_VECTOR);
+ vmwrite(VM_ENTRY_INSTRUCTION_LEN, 1);
+
+ vmlaunch();
+ check_int1_vectoring(vmx, (u64)l2_int1);
+ GUEST_DONE();
+}
+
+static void run_test(void *l1_guest_code)
+{
+ struct kvm_vcpu *vcpu;
+ gva_t vmx_pages_gva;
+ struct kvm_vm *vm;
+ struct ucall uc;
+
+ vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
+ vcpu_alloc_vmx(vm, &vmx_pages_gva);
+ vcpu_args_set(vcpu, 1, vmx_pages_gva);
+ vm_install_exception_handler(vm, DB_VECTOR, l2_db_handler);
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_DONE:
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ default:
+ TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+ }
+
+ kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
+
+ run_test(l1_icebp_intercepted);
+ run_test(l1_icebp_delivery_fault);
+ run_test(l1_int1_delivery_fault);
+
+ return 0;
+}
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread