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

From: Sohil Mehta

Date: Wed Dec 02 2026 - 03:05:15 EST


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@xxxxxxxxx>
---
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