KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs
PATCH 1/1 KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs
The SDM requires blocking by NMI to be clear when VM-Entry injects an NMI only if the "virtual NMIs" VM-execution control is 1. If virtual NMIs are disabled, injecting an NMI while blocking by NMI is set is legal.
KVM enables virtual NMIs in vmcs02 regardless of vmcs12 (and vmcs01 always uses virtual NMIs if enable_vnmi=true), but copies vmcs12's guest interruptibility state and VM-Entry interruption information verbatim. If L1 doesn't use virtual NMIs and injects an NMI while blocking by NMI is set, the CPU thus rejects vmcs02 and KVM reflects a bogus "VM-entry failure due to invalid guest state" to L1.
Clear blocking by NMI in vmcs02 in that case. Delivering the injected NMI blocks NMIs, i.e. L2 still runs with blocking by NMI set, which is the state L2 would be in on bare metal. Leave nmi_known_unmasked alone, it's derived from vmcs12's original value and so remains false.
--- arch/x86/kvm/vmx/nested.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/nested.c +20 -4
@@ -2563,14 +2563,30 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0* Interrupt/Exception Fields */ if (vmx->vcpu.arch.nested_run_pending) { -vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , - vmcs12->vm_entry_intr_info_field); +u32 intr_info = vmcs12->vm_entry_intr_info_field; +u32 interruptibility = vmcs12->guest_interruptibility_info; + +/* +* vmcs02 uses virtual NMIs if KVM does (i.e. enable_vnmi=true), +* regardless of vmcs12, in which case the CPU rejects VM-Enter +* if an NMI is being injected while blocking by NMI is set. +* Architecturally, there is no such restriction for L1 if +* vmcs12 doesn't use virtual NMIs, so clear blocking by NMI in +* vmcs02 if L1 is injecting an NMI. Delivering the NMI blocks +* NMIs, i.e. L2 still ends up with blocking by NMI set, the same +* as it would on bare metal. Note, nmi_known_unmasked is derived +* from vmcs12's original value and so stays false, as required. +*/ +if ((pin_controls_get (vmx) &PIN_BASED_VIRTUAL_NMIS ) && + !nested_cpu_has_virtual_nmis (vmcs12) &&is_nmi (intr_info)) + interruptibility &= ~GUEST_INTR_STATE_NMI ; + +vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , intr_info);vmcs_write32 (VM_ENTRY_EXCEPTION_ERROR_CODE , vmcs12->vm_entry_exception_error_code);vmcs_write32 (VM_ENTRY_INSTRUCTION_LEN , vmcs12->vm_entry_instruction_len); -vmcs_write32 (GUEST_INTERRUPTIBILITY_INFO , - vmcs12->guest_interruptibility_info); +vmcs_write32 (GUEST_INTERRUPTIBILITY_INFO , interruptibility); vmx->loaded_vmcs->nmi_known_unmasked = !(vmcs12->guest_interruptibility_info &GUEST_INTR_STATE_NMI ); }else {
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: nVMX: premature GUEST_INTR_STATE_NMI clear in prepare_vmcs02_early() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: nVMX: premature GUEST_INTR_STATE_NMI clear in prepare_vmcs02_early()
--
commit 0a8a662b2bf25c4df8703f3943666ec724f2d386
Author: Manuel Andreas <[email protected]>
KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs
This commit clears GUEST_INTR_STATE_NMI in vmcs02 when L1 injects an NMI while NMI-blocked without virtual NMIs enabled in vmcs12, avoiding a hardware VM-Entry failure when KVM enables virtual NMIs in vmcs02.
Quoted 29 lines
arch/x86/kvm/vmx/nested.c +19
-2@@ -2586,14 +2586,30 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0* Interrupt/Exception Fields */ if (vmx->vcpu.arch.nested_run_pending) { -vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , - vmcs12->vm_entry_intr_info_field); +u32 intr_info = vmcs12->vm_entry_intr_info_field; +u32 interruptibility = vmcs12->guest_interruptibility_info; + +/* +* vmcs02 uses virtual NMIs if KVM does (i.e. enable_vnmi=true), +* regardless of vmcs12, in which case the CPU rejects VM-Enter +* if an NMI is being injected while blocking by NMI is set. +* Architecturally, there is no such restriction for L1 if +* vmcs12 doesn't use virtual NMIs, so clear blocking by NMI in +* vmcs02 if L1 is injecting an NMI. Delivering the NMI blocks +* NMIs, i.e. L2 still ends up with blocking by NMI set, the same +* as it would on bare metal. Note, nmi_known_unmasked is derived +* from vmcs12's original value and so stays false, as required. +*/ +if ((pin_controls_get (vmx) &PIN_BASED_VIRTUAL_NMIS ) && + !nested_cpu_has_virtual_nmis (vmcs12) &&is_nmi (intr_info)) + interruptibility &= ~GUEST_INTR_STATE_NMI ; + +vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , intr_info);
arch/x86/kvm/vmx/nested.c static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0 · line 2615
Quoted 10 lines
arch/x86/kvm/vmx/nested.c +1
-2vmcs_write32 (VM_ENTRY_EXCEPTION_ERROR_CODE , vmcs12->vm_entry_exception_error_code);vmcs_write32 (VM_ENTRY_INSTRUCTION_LEN , vmcs12->vm_entry_instruction_len); -vmcs_write32 (GUEST_INTERRUPTIBILITY_INFO , - vmcs12->guest_interruptibility_info); +vmcs_write32 (GUEST_INTERRUPTIBILITY_INFO , interruptibility); vmx->loaded_vmcs->nmi_known_unmasked = !(vmcs12->guest_interruptibility_info &GUEST_INTR_STATE_NMI ); }else {
[Severity: Medium]
Can clearing GUEST_INTR_STATE_NMI in vmcs02 during prepare_vmcs02_early() corrupt vmx->loaded_vmcs->nmi_known_unmasked and vmcs12->guest_interruptibility_info before hardware VM-Entry delivers the NMI?
In vmx_get_nmi_mask(), nmi_known_unmasked == false is treated as a cache miss that reads GUEST_INTERRUPTIBILITY_INFO from the active VMCS and latches nmi_known_unmasked:
arch/x86/kvm/vmx/vmx.c:vmx_get_nmi_mask() {
if (vmx->loaded_vmcs->nmi_known_unmasked)
return false;
masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
vmx->loaded_vmcs->nmi_known_unmasked = !masked;
return masked;
}If vmx_get_nmi_mask() runs between prepare_vmcs02_early() and vmx_vcpu_run() (for example via process_nmi() when servicing KVM_REQ_NMI in vcpu_enter_guest(), or via KVM_GET_VCPU_EVENTS), it reads the cleared GUEST_INTR_STATE_NMI bit from vmcs02, returns false, and flips nmi_known_unmasked to true. After hardware VM-Entry delivers the NMI and re-sets GUEST_INTR_STATE_NMI, nmi_known_unmasked stays true, causing subsequent vmx_get_nmi_mask() and vmx_recover_nmi_blocking() calls on L2 VM-Exits to treat NMIs as unmasked.
In addition, if sync_vmcs02_to_vmcs12() runs before the NMI is delivered (such as on a hardware guest-state VM-Entry failure in __nested_vmx_vmexit() or during vmx_get_nested_state()), it overwrites vmcs12->guest_interruptibility_info with the cleared bit from vmcs02, and if a VM-Exit occurs while vectoring the injected NMI, __vmx_complete_interrupts() clears GUEST_INTR_STATE_NMI via vmx_set_nmi_mask(vcpu, false), losing L1's pre-existing NMI blocking.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1