KVM: VMX: Add a helper for NMI handling
authorSean Christopherson <sean.j.christopherson@intel.com>
Sat, 22 Feb 2025 01:47:55 +0000 (09:47 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 14 Mar 2025 18:20:56 +0000 (14:20 -0400)
Add a helper to handles NMI exit.

TDX handles the NMI exit the same as VMX case.  Add a helper to share the
code with TDX, expose the helper in common.h.

No functional change intended.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
Co-developed-by: Binbin Wu <binbin.wu@linux.intel.com>
Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com>
Message-ID: <20250222014757.897978-15-binbin.wu@linux.intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/vmx/common.h
arch/x86/kvm/vmx/vmx.c

index 1f2c2b4ee1da306a90889b18be6e591bd0bceebc..8fe6a7fa1c68b72a12536125de1a1a359e76906c 100644 (file)
@@ -177,4 +177,6 @@ static inline void __vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu,
        kvm_vcpu_trigger_posted_interrupt(vcpu, POSTED_INTR_VECTOR);
 }
 
+noinstr void vmx_handle_nmi(struct kvm_vcpu *vcpu);
+
 #endif /* __KVM_X86_VMX_COMMON_H */
index 279ce9be8995919f0023d18523221bd1b5e479ea..600e6766024fea1cece174c5b7a7b60069e7d677 100644 (file)
@@ -7214,6 +7214,20 @@ static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu,
        }
 }
 
+noinstr void vmx_handle_nmi(struct kvm_vcpu *vcpu)
+{
+       if ((u16)vmx_get_exit_reason(vcpu).basic != EXIT_REASON_EXCEPTION_NMI ||
+           !is_nmi(vmx_get_intr_info(vcpu)))
+               return;
+
+       kvm_before_interrupt(vcpu, KVM_HANDLING_NMI);
+       if (cpu_feature_enabled(X86_FEATURE_FRED))
+               fred_entry_from_kvm(EVENT_TYPE_NMI, NMI_VECTOR);
+       else
+               vmx_do_nmi_irqoff();
+       kvm_after_interrupt(vcpu);
+}
+
 static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
                                        unsigned int flags)
 {
@@ -7257,15 +7271,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
        if (likely(!vmx_get_exit_reason(vcpu).failed_vmentry))
                vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
 
-       if ((u16)vmx_get_exit_reason(vcpu).basic == EXIT_REASON_EXCEPTION_NMI &&
-           is_nmi(vmx_get_intr_info(vcpu))) {
-               kvm_before_interrupt(vcpu, KVM_HANDLING_NMI);
-               if (cpu_feature_enabled(X86_FEATURE_FRED))
-                       fred_entry_from_kvm(EVENT_TYPE_NMI, NMI_VECTOR);
-               else
-                       vmx_do_nmi_irqoff();
-               kvm_after_interrupt(vcpu);
-       }
+       vmx_handle_nmi(vcpu);
 
 out:
        guest_state_exit_irqoff();