Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Manuel Andreas <manuel.andreas@tum.de>
To: kvm@vger.kernel.org
Cc: seanjc@google.com, pbonzini@redhat.com,
	Manuel Andreas <manuel.andreas@tum.de>
Subject: [PATCH] KVM: nVMX: Allow NMI injection while NMI-blocked if vmcs12 doesn't use virtual NMIs
Date: Tue, 24 Nov 2026 14:37:04 +0100	[thread overview]
Message-ID: <20261124133704.5563-1-manuel.andreas@tum.de> (raw)

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


                 reply	other threads:[~2026-11-24 13:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124133704.5563-1-manuel.andreas@tum.de \
    --to=manuel.andreas@tum.de \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox