Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs
@ 2026-11-24 13:37 Manuel Andreas
  2026-11-24 13:56 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Manuel Andreas @ 2026-11-24 13:37 UTC (permalink / raw)
  To: kvm; +Cc: seanjc, pbonzini, Manuel Andreas

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.

Signed-off-by: Manuel Andreas <manuel.andreas@tum.de>
---
 arch/x86/kvm/vmx/nested.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 1a98f28651f7..7229f4b98eee 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -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 {
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 13:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 13:37 [PATCH] KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs Manuel Andreas
2026-11-24 13:56 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox