mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception
@ 2026-12-02  8:02 Sohil Mehta
  2026-12-02  8:02 ` [PATCH 1/4] KVM: x86: Track the event type of a queued exception Sohil Mehta
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ 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

TL;DR
-----
This series tracks the event type of a queued exception, and uses it to
requeue and re-inject a privileged software exception as one. This lays
groundwork for FRED guest support [1] and fixes nested-VMX edge cases.

Background
----------
David's int1_fred_test [2] found the issue by checking the event type in the
FRED stack frame. The issue isn't specific to FRED but it becomes highly
visible with FRED virtualization.

ICEBP (INT1) generates a #DB with the event type of a privileged software
exception. KVM loses that type in two ways. It re-injects the #DB of an
intercepted ICEBP as a hardware exception. And it drops a privileged software
exception altogether if a VM-exit interrupts its delivery.

The existing impact described below is limited, but it's still worth
addressing. As fixing this seems necessary [3] for the upcoming FRED guest
support, I am posting it separately for easier review and merge.

Impact
------
Without FRED, a guest can't see the event type through the IDT, so currently
only nested guests are affected:

 - If the delivery of an ICEBP's #DB to L2 causes a VM-exit to L1, L1
   finds a hardware exception in the IDT-vectoring information, with no
   instruction length and L2's RIP already past the ICEBP. An L1 that
   re-injects that event still delivers the #DB with the right return
   RIP.
 - An INT1 that L1 injects into L2 is dropped if a VM-exit interrupts
   its delivery. KVM doesn't re-inject it, and if the VM-exit goes to
   L1, L1 doesn't find it in the IDT-vectoring information. If L1
   injected it at an ICEBP, L2 re-executes the ICEBP and takes the #DB
   late. Otherwise, the #DB is lost.

Neither issue breaks a common L1. An L1 normally injects an INT1 only to
re-inject the #DB of an ICEBP, at the ICEBP, where a dropped INT1 is
only delayed. So the series has no Fixes tags.

However, with upcoming FRED guest support [1], every ICEBP in a FRED guest
shows event type 3 instead of 5 in the FRED stack frame. Unlike the IDT, FRED
gives the guest the type of every event. Linux's FRED entry code dispatches on
the type before the vector, so KVM should deliver the type that bare metal
would. Currently, Linux sends both types of #DB to the same handler, but other
guests may not.

Patches
-------
Patch 1 adds the event type to the queued exception, with no functional
change.

Patch 2 uses the event type to requeue a privileged software exception
whose delivery is interrupted, rather than drop it, and to inject it
with its own type and length.

Patch 3 builds on that to fix the other issue. KVM now re-injects
the #DB of an intercepted ICEBP as a privileged software exception, and
VM-entry advances RIP past the ICEBP, as Andy Cooper suggested [4].

Patch 4 adds a nested VMX selftest for both fixes. It doesn't need FRED.

AI was used for the initial prototype but the patches were heavily modified
before submission. The series has no dependencies and is based on kvm-x86-next
(kvm-x86-next-2026.11.11).

Testing
-------
Tested the series on a Clearwater Forest system (with ept=1 and ept=0):

New L2 ICEBP selftest:
   It passes. Without patch 3, L1 sees a hardware exception for the ICEBP's
   #DB. Without patch 2, L2 loops on the ICEBP and the test hangs.
Throwaway save/restore selftest:
   It saves and restores a vCPU while the #DB of an ICEBP is in flight, and
   checks that the guest takes exactly one #DB. It passes. If KVM reported the
   queued INT1 in the vCPU events, the guest would take the #DB twice. I can
   post it if anyone wants it upstream.
KVM unit tests:
   The debug, realmode, vmx and i386 taskswitch tests show no regressions.
   They cover ICEBP with single-step and other paths that the series touches.
KVM selftests:
   They show no regressions.
David's FRED event type test [2]:
   It passes with the FRED guest support series on top.

Notes/Opens
-----------
Save/restore: The vCPU events uAPI carries neither the event type nor the
instruction length, so patch 2 leaves a queued INT1 out, like INT3 and INTO,
and the guest re-executes the ICEBP. In a rare case, an INT1 that L1 injects
at a RIP without an ICEBP can be lost across save/restore. It's the same as a
software exception that L1 injects. Adding the type and length to the uAPI
would fix both, but that doesn't seem worth a uAPI change.

vm86: Without unrestricted guest, KVM runs a real-mode guest in vm86
mode and forwards the #DB of an ICEBP to it without advancing RIP, so
the guest executes the ICEBP forever. This predates the series, as
fd2a445a94d2 ("KVM: VMX: Advance rip to after an ICEBP instruction")
advanced RIP only outside vm86 mode. The series leaves vm86 mode alone,
as nothing queues a privileged software exception there today. I can
post a fix separately if someone says it's worth it.

SVM: SVM can inject a privileged software exception only for FRED guests. So
for now, SVM keeps injecting a hardware exception for the #DB of an ICEBP. The
SVM FRED series [5] asked for a generic fix for KVM-injected INT1s. Now, SVM
FRED can inject type 5 based on the event type that this series adds.

Links
-----
[1]: https://lore.kernel.org/lkml/20260911213659.2025974-1-sohil.mehta@intel.com/
[2]: https://lore.kernel.org/all/7f93eb25874ddd13a1ad6e3c75785f11041c8b7f.camel@infradead.org/
[3]: https://lore.kernel.org/lkml/fcfea4a5e25a9f828de317cc2c0b133eabd91745.camel@infradead.org/
[4]: https://lore.kernel.org/all/f4cb5f8e-caf5-4513-9538-edaaea20de2d@citrix.com/
[5]: https://lore.kernel.org/all/20260925114017.2208531-1-shivansh.dhiman@amd.com/


Sohil Mehta (4):
  KVM: x86: Track the event type of a queued exception
  KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is
    interrupted
  KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception
  KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2

 arch/x86/include/asm/kvm_host.h               |   2 +
 arch/x86/kvm/svm/svm.c                        |   2 +-
 arch/x86/kvm/vmx/nested.c                     |  13 +-
 arch/x86/kvm/vmx/vmx.c                        |  52 ++++--
 arch/x86/kvm/x86.c                            |  50 +++--
 arch/x86/kvm/x86.h                            |  14 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/include/x86/vmx.h |   7 +
 .../selftests/kvm/x86/vmx_icebp_test.c        | 174 ++++++++++++++++++
 9 files changed, 277 insertions(+), 38 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/vmx_icebp_test.c


base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [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; 5+ 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	[flat|nested] 5+ 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; 5+ 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	[flat|nested] 5+ 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:02 ` [PATCH 4/4] KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2 Sohil Mehta
  3 siblings, 0 replies; 5+ 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	[flat|nested] 5+ 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; 5+ 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	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-02  8:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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

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®