Skip to contentSkip to conversation

KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception

PATCH In review Sohil Mehta 3 participants 9 messages Dec 2 – Dec 4

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception

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/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/lkml/[email protected]/
[4]: https://lore.kernel.org/all/[email protected]/
[5]: https://lore.kernel.org/all/[email protected]/

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

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 KVM: x86: Track the event type of a queued exception

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.

---
 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(-)
arch/x86/include/asm/kvm_host.h +2 -0
index 3efef3415f8e..0b8d854f4a2f 100644
@@ -687,6 +687,8 @@ struct kvm_queued_exception {
 	u32 error_code;
 	unsigned long payload;
 	bool has_payload;
+	/* Architectural EVENT_TYPE_* */
+	u8 event_type;
 };
 
 /*
arch/x86/kvm/svm/svm.c +1 -1
index 721dc5e7e13a..bd775f02bd49 100644
@@ -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:
arch/x86/kvm/vmx/vmx.c +3 -1
index 68fa251dda60..a6c0e8ccdfad 100644
@@ -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:
arch/x86/kvm/x86.c +22 -10
index 2cd9ee316aad..c27813bde407 100644
@@ -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;
 	}
arch/x86/kvm/x86.h +11 -1
index 43850b524774..00218af728d4 100644
@@ -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,
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is interrupted

In reply to Sohil Mehta

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.

---
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(-)
arch/x86/kvm/vmx/nested.c +10 -3
index 1a98f28651f7..6cb07873998d 100644
@@ -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;
arch/x86/kvm/vmx/vmx.c +10 -2
index a6c0e8ccdfad..fdc531a61fd3 100644
@@ -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;
arch/x86/kvm/x86.c +6 -5
index c27813bde407..b58518a777de 100644
@@ -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;
 		/*
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/4] KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is interrupted

Quoted 11 lines · Sohil Mehta
arch/x86/kvm/vmx/nested.c +2 -1
index 1a98f28651f7..6cb07873998d 100644
@@ -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 =

arch/x86/kvm/vmx/nested.c static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, · line 3983

Quoted 10 lines
arch/x86/kvm/vmx/nested.c +5 -1
+				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;
+		}

Can we derive the type in idt_vectoring from the event type?

		idt_vectoring |= vcpu->arch.exception.event_type << 8;

And then set vm_exit_instruction_len only if the event is a software exception or a privileged software exception.

		if (vcpu->arch.exception.event_type == EVENT_TYPE_PRIV_SWEXC ||
		    vcpu->arch.exception.event_type == EVENT_TYPE_SWEXC)
			vmcs12->vm_exit_instruction_len =
				vcpu->arch.event_exit_inst_len;

This is much more readable than the if/else chain. The same applies to nested_vmx_inject_exception_vmexit() and vmx_inject_exception() below.

arch/x86/kvm/vmx/vmx.c void vmx_inject_exception(struct kvm_vcpu *vcpu) · line 1976

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +1 -1
index a6c0e8ccdfad..fdc531a61fd3 100644
@@ -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 the event type is the only source of the type here, you don't need to worry about the order of the event type and vector checks, and this comment can go.

Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +5 -0
+	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)) {
 
arch/x86/kvm/x86.c +0 -0
index c27813bde407..b58518a777de 100644

arch/x86/kvm/x86.c static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, · line 3012

Quoted 4 lines
arch/x86/kvm/x86.c +2 -1
@@ -3010,12 +3010,13 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
 	 */
-	if (!kvm_exception_is_soft(ex->vector)) {
+	if (!kvm_exception_is_soft(ex->vector) &&
+	    ex->event_type != EVENT_TYPE_PRIV_SWEXC) {

With the event type, this becomes a check for a software exception or a privileged software exception.

	ex->event_type == EVENT_TYPE_PRIV_SWEXC ||
	ex->event_type == EVENT_TYPE_SWEXC

That check then occurs in a few places, so we can probably add a helper for it.

#4 lore raw

PATCH 3/4 KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception

In reply to Sohil Mehta

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.

---
 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(-)
arch/x86/kvm/vmx/vmx.c +21 -15
index fdc531a61fd3..72d16282b559 100644
@@ -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;
arch/x86/kvm/x86.c +7 -0
index b58518a777de..44d24f1b8109 100644
@@ -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)
 {
arch/x86/kvm/x86.h +2 -0
index 00218af728d4..91941f12b835 100644
@@ -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,
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception

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() --

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 <[email protected]>

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.

Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +0 -0
index fdc531a61fd30..72d16282b5598 100644

[ ... ]

Quoted 20 lines
arch/x86/kvm/vmx/vmx.c +7 -6
@@ -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.
 			 */

arch/x86/kvm/vmx/vmx.c static int handle_exception_nmi(struct kvm_vcpu *vcpu) · line 5552

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +8 -2
-			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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#6 lore raw

Re: [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception

+ Everyone from the main thread (because I am not sure how to dispose this)

Quoted 7 lines · On 12/2/2026 12:35 AM, [email protected] wrote:

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()
--

Quoted 5 lines

[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?

The window seems real. It happens because patch 2 leaves a queued INT1 out of the vCPU events like INT3 and INTO, but those don't carry a payload. So the payload of a #DB that isn't saved still leaks out, to userspace or into DR6. However, I don't see how the guest state can be corrupted.

My LLM says:
- Without KVM_CAP_EXCEPTION_PAYLOAD, KVM writes the payload to DR6 when userspace saves the vCPU state. After a restore, the guest re-executes the ICEBP, and the new #DB writes the same DR6 bits again.

- With KVM_CAP_EXCEPTION_PAYLOAD, KVM_GET_VCPU_EVENTS reports the payload without a pending or injected exception, and KVM_SET_VCPU_EVENTS drops it.

IIUC, the fix would be to leave the payload of a queued INT1 out of the vCPU events too, and not deliver it early when userspace saves the vCPU state. Maybe something like this (tested with a throwaway selftest), folded into patch 2:

arch/x86/kvm/x86.c +3 -0
@@ -2978,6 +2978,17 @@ static struct kvm_queued_exception *kvm_
 	return &vcpu->arch.exception;
 }
 
+/*
+ * The vCPU events API doesn't provide the instruction length that software
+ * exceptions and INT1 need, so KVM doesn't report them. As long as the

guest
+ * RIP isn't advanced, the guest encounters the exception again.
+ */
+static bool kvm_exception_is_reportable(struct kvm_queued_exception *ex) +{
+ return !kvm_exception_is_soft(ex->vector) &&
+ ex->event_type != EVENT_TYPE_PRIV_SWEXC;
+}
+
void kvm_handle_exception_payload_quirk(struct kvm_vcpu *vcpu) {
struct kvm_queued_exception *ex = kvm_get_exception_to_save(vcpu);
@@ -2996,7 +3007,7 @@ void kvm_handle_exception_payload_quirk(
* safely defer delivery of the payload.
*/
if (!vcpu->kvm->arch.exception_payload_enabled &&
- ex->pending && ex->has_payload)
+ ex->pending && ex->has_payload && kvm_exception_is_reportable(ex))
kvm_deliver_exception_payload(vcpu, ex);
}

@@ -3016,14 +3027,7 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_

memset(events, 0, sizeof(*events));

- /*
- * 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) &&
- ex->event_type != EVENT_TYPE_PRIV_SWEXC) {
+ if (kvm_exception_is_reportable(ex)) {
events->exception.injected = ex->injected; events->exception.pending = ex->pending; /*
@@ -3033,12 +3037,12 @@ static void kvm_vcpu_ioctl_x86_get_vcpu_
*/
if (!vcpu->kvm->arch.exception_payload_enabled)
events->exception.injected |= ex->pending;
+ events->exception_has_payload = ex->has_payload;
+ events->exception_payload = ex->payload;
}
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;

events->interrupt.injected =
vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;

I think even though it doesn't fix a guest-visible problem, it does make the handling of exception payloads more consistent with the API's limitations. KVM_GET_VCPU_EVENTS stops reporting a payload without an exception, and saving the vCPU state no longer writes DR6 for a #DB that isn't saved.

This isn't really my area of expertise, so I would appreciate feedback on this. Does this seem worth fixing?

#7 lore raw

Re: [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception

Quoted 12 lines · Sohil Mehta
Quoted 5 lines

[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?

The window seems real. It happens because patch 2 leaves a queued INT1 out of the vCPU events like INT3 and INTO, but those don't carry a payload. So the payload of a #DB that isn't saved still leaks out, to userspace or into DR6. However, I don't see how the guest state can be corrupted.

Quoted 5 lines

My LLM says:
- Without KVM_CAP_EXCEPTION_PAYLOAD, KVM writes the payload to DR6 when userspace saves the vCPU state. After a restore, the guest re-executes the ICEBP, and the new #DB writes the same DR6 bits again.

I think it can be guest visible. After a restore, if an interrupt is injected before the guest re-executes the ICEBP, the interrupt handler can see the wrong DR6.

I am not sure how severe it is, though.

Quoted 2 lines

This isn't really my area of expertise, so I would appreciate feedback on this. Does this seem worth fixing?

I think it is worth fixing. It isn't about how much impact a wrong DR6 has. The logic of delivering the payload early should be explainable, and it should only apply to hardware exceptions. Software exceptions, including privileged ones, aren't saved/restored, so there is no reason to deliver their payload early at all.

#8 lore raw

PATCH 4/4 KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2

In reply to Sohil Mehta

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.

---
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
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 95867977beb3..d65cc2ac35ed 100644
@@ -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
tools/testing/selftests/kvm/include/x86/vmx.h +7 -0
index 8fd97434eee5..6e6e4bc97f15 100644
@@ -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 {
tools/testing/selftests/kvm/x86/vmx_icebp_test.c new file +174 -0

174 lines not on this page · raw message

Signature · 1 line
2.43.0