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=1796198693; b=eo4wOLht5HZ3OazABcpz+ZMPe0f03X+DpTPF4jaEaUtMSRLtLdiTfAGKsza3k0uWJYF4 B3+gSloyX9hcbY6xetODY0QhlC1jsnNI416sIDOg1xcZorqb9q25DAn6EeB+aVNcP21LC su4kV8HgARAFO7Lba2GjBO9p1xxVNHp4DPNN84/qFXnVhdMyRQFq6Jlvfmb+lzMPaaLS+ ZbgD3SgEepLlItxeMpL7Xj8vxy/HasaXUgezIwHQSHg77F33Iu/Zz+jgEgLo9V4CQehi2 SJWQ+99vVjB2vKOy9Ix3Lbikh8H+i7loiyvfA8YjzoMHhwrw4B5fmHt30OQVZ6eGvyQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796198693; 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=GIE3YtOBP29lFyC6Y+5znOL51frLRXt5kSMonXI54Ew=; b=v9et6p8J6tadX+NoqvdVSL7moLXHLa4MofxJPEPGdX4EarfWINPCDvNSRpZT0ajS9c2S RMn/VBcyXN5YXaHbpKTR7FTcnb+6Swce6crl98fmYN88SO6xm0hxo/64Fx5seYO+HCCY+ 7IPQmM/QG36+rYYOCgzvn89r2ksEYfpxQK6PLpS+jvyd4srKJ8jIOXKbAvafAlbyevyMe nzAeIGji30o5Vh+Ju637EgAP1hmydN0SK/0zDHY/MiZ2w5APOpk8+WSmPCyKd7Dlpswtj 1ftzFhBlizxdbXDmVuhYHuiLGWRR19kdkJ5Kn7eJADBu7iyYZ7bU6YGtSzxUrfrrfzQ== 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=CRoOyljL; 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 5BBE2606A337; Wed, 2 Dec 2026 08:04:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5BBE2606A337 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=CRoOyljL DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796198694; x=1827734694; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GIE3YtOBP29lFyC6Y+5znOL51frLRXt5kSMonXI54Ew=; b=CRoOyljLipDMyI5GiA3HVrdo4G4VlEBLmOvBeF/cj75r+7+1JZq8ej0T WwI4vsDBjfBxxMI4/1wfPxGs84MavF3oFALG0/RVcdxiB7xAaxjzSQKfQ rwHHFEod2d1NoqEXYsyD/iR/6cSP6/6IMjpoT1L0qIbeVIYTBlqhqSILe js93JJ5HmjmCy5nOf94hTy3qbJx8lJ2y/V+DMIRUjcfRG6mWgq+sHqDLi +KdnbHaWKW+Tq/mHFYCI8z0FBq/+SrYpdDHoy837XX35GYv+B3EYMVqLd cbt6ZmavYpKMNtDY9lyjFP3vrxTzWK9HJpJlzN/i/FaNwTr5z1rY/HaiE g==; X-CSE-ConnectionGUID: g/EpU2GIQiGNqsNklyTFgg== X-CSE-MsgGUID: ybcMx6F8RwOGMX8T6cR9eQ== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5211625" X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="5211625" 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:43 -0800 X-CSE-ConnectionGUID: hRHPf98wQh+N/Yas687NQA== X-CSE-MsgGUID: iIqfpK8HT8uc+xUO7itslQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="15428629" 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 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception Date: Wed, 2 Dec 2026 00:02:23 -0800 Message-ID: <20261202080224.4165883-4-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 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 Reported-by: David Woodhouse Closes: https://lore.kernel.org/all/7f93eb25874ddd13a1ad6e3c75785f11041c8b7f.camel@infradead.org/ Signed-off-by: Sohil Mehta --- 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