Skip to content

Commit eeb456e

Browse files
sean-jcgregkh
authored andcommitted
KVM: VMX: Refresh GUEST_PENDING_DBG_EXCEPTIONS.BS on all injected #DBs
commit c5bad4f upstream. Move KVM's stuffing of GUEST_PENDING_DBG_EXCEPTIONS.BS when RFLAGS.TF=1 and MOV/POP SS or STI blocking is active into the exception injection code so that KVM fixes up the VMCS for all injected #DBs, not only those that are reflected back into the guest after #DB interception. E.g. if KVM queues a #DB in the emulator, or more importantly if userspace does save/restore exactly on the #DB+shadow boundary, then KVM needs to massage the VMCS to avoid the VM-Entry consistency check. Opportunistically update the wording of the comment to describe the behavior as a workaround of flawed CPU behavior/architecture, to make it clear that the *only* thing KVM is doing is fudging around a consistency check. Per the SDM: There are no pending debug exceptions after VM entry if any of the following are true: * The VM entry is vectoring with one of the following interruption types: external interrupt, non-maskable interrupt (NMI), hardware exception, or privileged software exception. I.e. forcing GUEST_PENDING_DBG_EXCEPTIONS.BS does *not* impact guest- visible behavior. Fixes: b9bed78 ("KVM: VMX: Set vmcs.PENDING_DBG.BS on #DB in STI/MOVSS blocking shadow") Cc: stable@vger.kernel.org Reported-by: Hou Wenlong <houwenlong.hwl@antgroup.com> Closes: https://lore.kernel.org/all/b1a294bc9ed4dae532474a5dc6c8cb6e5962de7c.1757416809.git.houwenlong.hwl@antgroup.com Reviewed-by: Hou Wenlong <houwenlong.hwl@antgroup.com> Link: https://patch.msgid.link/20260515222638.1949982-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 35f3ea7 commit eeb456e

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

arch/x86/kvm/vmx/vmx.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,24 @@ void vmx_inject_exception(struct kvm_vcpu *vcpu)
17901790
u32 intr_info = ex->vector | INTR_INFO_VALID_MASK;
17911791
struct vcpu_vmx *vmx = to_vmx(vcpu);
17921792

1793+
/*
1794+
* When injecting a #DB, single-stepping is enabled in RFLAGS, and STI
1795+
* or MOV-SS blocking is active, set vmcs.PENDING_DBG_EXCEPTIONS.BS to
1796+
* prevent a false positive from VM-Entry consistency check. VM-Entry
1797+
* asserts that a single-step #DB _must_ be pending in this scenario,
1798+
* as the previous instruction cannot have toggled RFLAGS.TF 0=>1
1799+
* (because STI and POP/MOV don't modify RFLAGS), therefore the one
1800+
* instruction delay when activating single-step breakpoints must have
1801+
* already expired. However, the CPU isn't smart enough to peek at
1802+
* vmcs.VM_ENTRY_INTR_INFO_FIELD and so doesn't realize that yes, there
1803+
* is indeed a #DB pending/imminent.
1804+
*/
1805+
if (ex->vector == DB_VECTOR &&
1806+
(vmx_get_rflags(vcpu) & X86_EFLAGS_TF) &&
1807+
vmx_get_interrupt_shadow(vcpu))
1808+
vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
1809+
vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS) | DR6_BS);
1810+
17931811
kvm_deliver_exception_payload(vcpu, ex);
17941812

17951813
if (ex->has_error_code) {
@@ -5286,26 +5304,9 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
52865304
* avoid single-step #DB and MTF updates, as ICEBP is
52875305
* higher priority. Note, skipping ICEBP still clears
52885306
* STI and MOVSS blocking.
5289-
*
5290-
* For all other #DBs, set vmcs.PENDING_DBG_EXCEPTIONS.BS
5291-
* if single-step is enabled in RFLAGS and STI or MOVSS
5292-
* blocking is active, as the CPU doesn't set the bit
5293-
* on VM-Exit due to #DB interception. VM-Entry has a
5294-
* consistency check that a single-step #DB is pending
5295-
* in this scenario as the previous instruction cannot
5296-
* have toggled RFLAGS.TF 0=>1 (because STI and POP/MOV
5297-
* don't modify RFLAGS), therefore the one instruction
5298-
* delay when activating single-step breakpoints must
5299-
* have already expired. Note, the CPU sets/clears BS
5300-
* as appropriate for all other VM-Exits types.
53015307
*/
53025308
if (is_icebp(intr_info))
53035309
WARN_ON(!skip_emulated_instruction(vcpu));
5304-
else if ((vmx_get_rflags(vcpu) & X86_EFLAGS_TF) &&
5305-
(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5306-
(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)))
5307-
vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
5308-
vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS) | DR6_BS);
53095310

53105311
kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
53115312
return 1;

0 commit comments

Comments
 (0)