From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.21 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796198692; b=YugVBaALpNe6BwiIhAmq7PtSRrsrMC2/+4aVuVQUnHCQPuWE+d39uD4MCV76RvaE/9XO rNTalT1lIqI+r1tgD1YPEcQT3AFQ8C+1NeitdvPFzfNzHDXENe8SZIoMA8JB100Dl1o1P YVgsw0UIQ//DV72WLscI1F4FpgpC4iQdYJOxN0swCbePRX2+A0JrNatNlpay/UhKl0TmY czSH8fvSBH7eGuvKmHDfyonOmAxmjn+nqFb6cmi1QIAGtkkPLUe1+hSuqHVi4bUnGkzJ8 hKdUfGw4kzGHo+WdZmGe4KYsY8RAO9SEnFmtjrQBXc13vLaIDuU7WH+wD0R7pleEYHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796198692; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=ea2LoXeSXdmN2RyDSyiFmQkCPec/utamPR3/dtP3xCM=; b=YLubzS/Tx9K1/y8pvPWDVvxlb9V2gGgic8+6KHdYPUFBDNQzM94ZVkvMCifqlpH2Mzcj FVRQMCwq1+tQn9tnqrN1wbNuiIsNyX1k6KKsV6mmx5iwShytBhz9ZDWQSXBiAIKwUQq5P FVrb+8jp1xx6yZe2lRcTXB4CcmX6vvi6R7e/TulEcHl5APq+cKUkfQODB3+ylbxIn/wlu 3WNYc5ViEpQ3nUuB/1/w85yg9OkkjDDTyHZgJwr7H9sv0q5FN33u+vgirh2WOiux7WcMb ons+o6keg9QhEz7kxzqaVv9S8GDewLN1R6hHDDjg3a4n39SZOspi8ysqYOFOTYB/PJg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b="T9Qj/hpR"; arc=none smtp.remote-ip=198.175.65.21 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8EA9606A31A; Wed, 2 Dec 2026 08:04:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D8EA9606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=T9Qj/hpR DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796198692; x=1827734692; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ea2LoXeSXdmN2RyDSyiFmQkCPec/utamPR3/dtP3xCM=; b=T9Qj/hpRWq+AzVktWwJifKePClpzZM5Xx9Ihi+mS4NLhQJnL/oeNVadb /tsI8k61oF/xazf6L2pv9XwaZUzRnuuxe6+6WMO247qYD9+5Ba8sBjgwx i11vFMaMhqt3uPU0yfDLI5l5QyQ5WL72jpuFpCnlc+aQlTJYbfnrAb6q8 EuKGlXjq5aYoj327MGAsUcr2WDySg9wzrYYX0oLgbKLJ9Z+9nFX9Shkek ZgWxpHPcTFD5EDlOd+d/dNbqLA2kDoo+WtvTQ7TbrivmOrbq3HgBeSS0Q u2LbsTTASVJqhBpwmI/h5PU6AMahIhyrysBHkvCfvAYtFgg2iJu0Ow79e g==; X-CSE-ConnectionGUID: lVizU5RyRKmurdgGRCSvKw== X-CSE-MsgGUID: WTXz9SDqRD+0rzJjjwzJEA== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5211610" X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="5211610" Received: from fmviesa013.fm.intel.com ([10.60.135.153]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 00:04:42 -0800 X-CSE-ConnectionGUID: KOF9zFFHQpe5xJyOSbyjBQ== X-CSE-MsgGUID: 1U/DmoJxTO6PYw3IJyjV+Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="15428623" Received: from sohilmeh.sc.intel.com ([172.25.103.65]) by fmviesa013.fm.intel.com with ESMTP; 02 Dec 2026 00:04:42 -0800 From: Sohil Mehta To: kvm@vger.kernel.org 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@kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 1/4] KVM: x86: Track the event type of a queued exception Date: Wed, 2 Dec 2026 00:02:21 -0800 Message-ID: <20261202080224.4165883-2-sohil.mehta@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261202080224.4165883-1-sohil.mehta@intel.com> References: <20261202080224.4165883-1-sohil.mehta@intel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 #include #include +#include #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