Virtual NMI
cover Virtual NMI
From: Abdelkareem Abdelsaamad <[email protected]>
Currently, the Xen hypervisor delivers NMIs to the guests using the Event Injection mechanism. The Event Injection mechanism does not block the delivery of subsequent NMIs. So the Xen Hypervisor tracks the NMI delivery and its completion (by intercepting the IRET instruction) before sending a new NMI.
With the 4th generation AMD EPYC, a hardware NMI virtualization support, Virtual NMI (VNMI), is introduced. The vNMI allows the hypervisor to inject the NMI into the guest w/o using Event Injection mechanism. No need to track the guest NMI and intercepting the IRET instruction.
NMI Virtualization support is indicated by CPUID Fn8000_000A_EDX[VNMI] = 1.[1]
NMI Virtualization is enabled by setting V_NMI_ENABLE (bit 26 in offset 60h of the VMCB). Enabling NMI Virtualization requires the NMI intercept bit to be set. An attempt to run a guest with V_NMI_ENABLE without the NMI intercept bit set results in #VMEXIT(INVALID) [1]
Three new bits are added to the VMCB field at offset 60h to provide NMI virtualization hardware support:
V_NMI(11): Indicates whether a virtual NMI is pending in the guest. The processor will clear V_NMI once it takes the virtual NMI. V_NMI_MASK(12): Indicates whether virtual NMIs are masked. The processor will set V_NMI_MASK once it takes the virtual NMI. V_NMI_MASK is cleared when the guest successfully completes an IRET instruction or #VMEXIT occurs while delivering the virtual NMI.V_NMI_ENABLE(26): Enables NMI virtualization. [1]
--Abdelkareem Abdelsaamad
[1] https://docs.amd.com/v/u/en-US/24593_3.43
Abdelkareem Abdelsaamad (3):
svm/vnmi: introduce the vnmi bit support in the cpuid feature set svm/vnmi: add the definitions for the svm vnmi management bits in the
VMCB
svm/vnmi: Add support for the SVM Virtual NMI
xen/arch/x86/hvm/hvm.c | 29 +++++++++++++++++++++-------- xen/arch/x86/hvm/svm/intr.c | 16 ++++++++++++++-- xen/arch/x86/hvm/svm/svm.c | 25 ++++++++++++++++++++++++- xen/arch/x86/hvm/svm/vmcb.c | 3 +++ xen/arch/x86/hvm/svm/vmcb.h | 12 ++++++++---- xen/arch/x86/include/asm/hvm/hvm.h | 12 +++++++++++- xen/arch/x86/include/asm/hvm/svm.h | 2 ++ 7 files changed, 83 insertions(+), 16 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/3 svm/vnmi: introduce the vnmi bit support in the cpuid feature set
From: Abdelkareem Abdelsaamad <[email protected]>
Introduce the cpuid bit for SVM vNMI feature support in for the x86\AMD platforms.
--- xen/arch/x86/hvm/svm/svm.c | 2 ++ xen/arch/x86/include/asm/hvm/hvm.h | 4 +++- xen/arch/x86/include/asm/hvm/svm.h | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/svm/svm.c +2 -0
@@ -2524,6 +2524,7 @@ const struct hvm_function_table * __init start_svm(void)P (cpu_has_tsc_ratio,"TSC Rate MSR" );P (cpu_has_svm_sss,"NPT Supervisor Shadow Stack" );P (cpu_has_svm_spec_ctrl,"MSR_SPEC_CTRL virtualisation" ); +P (cpu_has_svm_vnmi,"Virtual NMI" );P (cpu_has_svm_bus_lock,"Bus Lock Filter" );#undef P
@@ -2533,6 +2534,7 @@ const struct hvm_function_table * __init start_svm(void) svm_function_table.caps.hap = cpu_has_svm_npt; svm_function_table.caps.hap_superpage_2mb = true; svm_function_table.caps.hap_superpage_1gb = cpu_has_page1gb; + svm_function_table.caps.vNMI = cpu_has_svm_vnmi;return &svm_function_table; }
xen/arch/x86/include/asm/hvm/hvm.h +3 -1
@@ -115,7 +115,9 @@ struct hvm_function_table { virtual_intr_delivery:1, /* Nested virt capabilities */ - nested_virt:1; + nested_virt:1, + /* virtual NMI support */ + vNMI:1; } caps;/*
xen/arch/x86/include/asm/hvm/svm.h +2 -0
@@ -37,6 +37,7 @@ extern u32 svm_feature_flags;#define SVM_FEATURE_VGIF 16/* Virtual GIF */ #define SVM_FEATURE_SSS 19/* NPT Supervisor Shadow Stacks */ #define SVM_FEATURE_SPEC_CTRL 20/* MSR_SPEC_CTRL virtualisation */ +#define SVM_FEATURE_VNMI 25/* Virtual NMI */ #define SVM_FEATURE_BUS_LOCK 29/* Bus Lock Threshold */ static inline bool cpu_has_svm_feature (unsigned int feat)
@@ -57,6 +58,7 @@ static inline bool cpu_has_svm_feature(unsigned int feat)#define cpu_has_svm_vloadsave cpu_has_svm_feature(SVM_FEATURE_VLOADSAVE)#define cpu_has_svm_sss cpu_has_svm_feature(SVM_FEATURE_SSS)#define cpu_has_svm_spec_ctrl cpu_has_svm_feature(SVM_FEATURE_SPEC_CTRL) +#define cpu_has_svm_vnmi cpu_has_svm_feature(SVM_FEATURE_VNMI)#define cpu_has_svm_bus_lock cpu_has_svm_feature(SVM_FEATURE_BUS_LOCK)#define MSR_INTERCEPT_NONE 0
Signature · 1 line
2.52.0
PATCH 2/3 svm/vnmi: add the definitions for the svm vnmi management bits in the VMCB
In reply to [email protected]From: Abdelkareem Abdelsaamad <[email protected]>
Add defines for the three SVM's Virtual NMI (vNMI) managements bits in the VMCB structure's vintr_t:
vintr_t(11) - Virtual NMI is pending.vintr_t(12) - Virtual NMI is masked.vintr_t(26) - Enable NMI virtualization.
--- xen/arch/x86/hvm/svm/vmcb.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/svm/vmcb.h +8 -4
@@ -336,15 +336,19 @@ typedef union u64 tpr: 8; u64 irq: 1; u64 vgif: 1; - u64 rsvd0: 6; + u64 rsvd0: 1; + u64 vnmi_pending: 1; + u64 vnmi_blocking:1; + u64 rsvd1: 3; u64 prio: 4; u64 ign_tpr: 1; - u64 rsvd1: 3; + u64 rsvd2: 3; u64 intr_masking: 1; u64 vgif_enable: 1; - u64 rsvd2: 6; + u64 vnmi_enable: 1; + u64 rsvd3: 5; u64 vector: 8; - u64 rsvd3: 24; + u64 rsvd4: 24; } fields; } vintr_t;
Signature · 1 line
2.52.0
PATCH 3/3 svm/vnmi: Add support for the SVM Virtual NMI
In reply to [email protected]From: Abdelkareem Abdelsaamad <[email protected]>
With vNMI, the pending NMI is simply stuffed into the VMCB and handed off to the hardware. This means that Xen needs to be able to set a vNMI pending on-demand, and also query if a vNMI is pending, e.g. to honor the "at most one NMI pending" rule and to preserve all NMIs across save and restore.
Introduce two new hvm_function_table callbacks to support the SVM's vNMI to allow the Xen hypervisor to query if a vNMI is pending and to set VMCB's _vintr pending flag so the NMIs are serviced by hardware if/when the virtual NMIs become unblocked.
--- xen/arch/x86/hvm/hvm.c | 29 +++++++++++++++++++++-------- xen/arch/x86/hvm/svm/intr.c | 16 ++++++++++++++-- xen/arch/x86/hvm/svm/svm.c | 23 ++++++++++++++++++++++- xen/arch/x86/hvm/svm/vmcb.c | 3 +++ xen/arch/x86/include/asm/hvm/hvm.h | 8 ++++++++ 5 files changed, 68 insertions(+), 11 deletions(-)
xen/arch/x86/hvm/hvm.c +21 -8
@@ -3904,10 +3904,29 @@ void hvm_ud_intercept(struct cpu_user_regs *regs) } } -enum hvm_intblk hvm_interrupt_blocked (struct vcpu *v,struct hvm_intack intack) +static enum hvm_intblk hvm_nmi_blocked (struct vcpu *v) {unsigned long intr_shadow; +if (hvm_funcs.caps.vNMI) + { +if (!hvm_funcs.is_vnmi_pending (v)) +return hvm_intblk_none; + +return hvm_intblk_vnmi; + } + + intr_shadow =alternative_call (hvm_funcs.get_interrupt_shadow, v); + +if ( intr_shadow & (HVM_INTR_SHADOW_STI |HVM_INTR_SHADOW_MOV_SS ) ) +return hvm_intblk_shadow; + +return ((intr_shadow &HVM_INTR_SHADOW_NMI ) ? + hvm_intblk_nmi_iret : hvm_intblk_none); +} + +enum hvm_intblk hvm_interrupt_blocked (struct vcpu *v,struct hvm_intack intack) +{ASSERT (v == current);if (nestedhvm_enabled (v->domain) ) {
@@ -3922,14 +3941,8 @@ enum hvm_intblk hvm_interrupt_blocked(struct vcpu *v, struct hvm_intack intack) !(guest_cpu_user_regs()->eflags & X86_EFLAGS_IF) )return hvm_intblk_rflags_ie; - intr_shadow =alternative_call (hvm_funcs.get_interrupt_shadow, v); - -if ( intr_shadow & (HVM_INTR_SHADOW_STI |HVM_INTR_SHADOW_MOV_SS ) ) -return hvm_intblk_shadow; -if ( intack.source == hvm_intsrc_nmi ) -return ((intr_shadow &HVM_INTR_SHADOW_NMI ) ? - hvm_intblk_nmi_iret : hvm_intblk_none); +return hvm_nmi_blocked (v);if ( intack.source == hvm_intsrc_lapic ) {
xen/arch/x86/hvm/svm/intr.c +14 -2
@@ -29,10 +29,19 @@static void svm_inject_nmi (struct vcpu *v) { -struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb; -u32 general1_intercepts =vmcb_get_general1_intercepts (vmcb); +struct vmcb_struct *vmcb; +u32 general1_intercepts;intinfo_t event; +if (hvm_funcs.caps.vNMI) + { + hvm_funcs.set_vnmi_pending (v); +return ; + } + + vmcb = v->arch.hvm.svm.vmcb; + general1_intercepts =vmcb_get_general1_intercepts (vmcb); + event.raw =0 ; event.v = true; event.type =X86_ET_NMI ;
@@ -176,6 +185,9 @@ void asmlinkage svm_intr_assist(void) } } +// vNMI is currently serviced in progress. +if (unlikely (intblk == hvm_intblk_vnmi))return ; +/* * Pending IRQs must be delayed if: * 1. An event is already pending. This is despite the fact that SVM
xen/arch/x86/hvm/svm/svm.c +22 -1
@@ -297,6 +297,26 @@ void svm_intercept_msr(struct vcpu *v, uint32_t msr, int flags)__clear_bit (msr *2 +1 , msr_bit); } +static bool cf_check svm_is_vnmi_pending(struct vcpu *vcpu) +{ +if (!hvm_funcs.caps.vNMI) +return false; + +return (vcpu->arch.hvm.svm.vmcb->_vintr.fields.vnmi_blocking); +} + +static bool cf_check svm_set_vnmi_pending(struct vcpu *vcpu) +{ +if (!hvm_funcs.caps.vNMI) +return false; + +if (vcpu->arch.hvm.svm.vmcb->_vintr.fields.vnmi_pending) +return false; + + vcpu->arch.hvm.svm.vmcb->_vintr.fields.vnmi_pending =1 ; +return true; +} +#ifdef CONFIG_VM_EVENT static void cf_check svm_enable_msr_interception(struct domain *d, uint32_t msr) {
@@ -2464,7 +2484,8 @@ static struct hvm_function_table __initdata_cf_clobber svm_function_table = {#endif .set_rdtsc_exiting = svm_set_rdtsc_exiting, .get_insn_bytes = svm_get_insn_bytes, - + .is_vnmi_pending = svm_is_vnmi_pending, + .set_vnmi_pending = svm_set_vnmi_pending, .nhvm_vcpu_initialise = nsvm_vcpu_initialise, .nhvm_vcpu_destroy = nsvm_vcpu_destroy, .nhvm_vcpu_reset = nsvm_vcpu_reset,
xen/arch/x86/hvm/svm/vmcb.c +3 -0
@@ -184,6 +184,9 @@ static int construct_vmcb(struct vcpu *v)if ( default_xen_spec_ctrl ==SPEC_CTRL_STIBP ) v->arch.msrs->spec_ctrl.raw =SPEC_CTRL_STIBP ; +if (hvm_funcs.caps.vNMI) + vmcb->_vintr.fields.vnmi_enable =1 ; +return 0 ; }
xen/arch/x86/include/asm/hvm/hvm.h +8 -0
@@ -67,6 +67,7 @@ enum hvm_intblk { hvm_intblk_rflags_ie,/* RFLAGS.IE == 0 */ hvm_intblk_tpr,/* LAPIC TPR too high */ hvm_intblk_nmi_iret,/* NMI blocked until IRET */ + hvm_intblk_vnmi,/* vNMI is currently blocked*/ hvm_intblk_arch, /* SVM/VMX specific reason */ };
@@ -224,6 +225,13 @@ struct hvm_function_table {int (*pi_update_irte)(const struct vcpu *v,const struct pirq *pirq,uint8_t gvec);void (*update_vlapic_mode)(struct vcpu *v); +/* Whether or not a virtual NMI is pending in hardware. */ +bool (*is_vnmi_pending)(struct vcpu *vcpu); +/* +* Attempt to pend a virtual NMI in harware. +* Returns %true on success +*/ +bool (*set_vnmi_pending)(struct vcpu *vcpu);/*Walk nested p2m */ int (*nhvm_hap_walk_L1_p2m)(struct vcpu *v, paddr_t L2_gpa,
Signature · 1 line
2.52.0
Quoted 6 lines · On 06/02/2026 3:53 pm, [email protected] wrote:
Abdelkareem Abdelsaamad (3):
svm/vnmi: introduce the vnmi bit support in the cpuid feature set svm/vnmi: add the definitions for the svm vnmi management bits in the
VMCB
svm/vnmi: Add support for the SVM Virtual NMI
Quoted 8 lines
xen/arch/x86/hvm/hvm.c | 29 +++++++++++++++++++++-------- xen/arch/x86/hvm/svm/intr.c | 16 ++++++++++++++-- xen/arch/x86/hvm/svm/svm.c | 25 ++++++++++++++++++++++++- xen/arch/x86/hvm/svm/vmcb.c | 3 +++ xen/arch/x86/hvm/svm/vmcb.h | 12 ++++++++---- xen/arch/x86/include/asm/hvm/hvm.h | 12 +++++++++++- xen/arch/x86/include/asm/hvm/svm.h | 2 ++ 7 files changed, 83 insertions(+), 16 deletions(-)
Patches 1 and 2 want merging. They're both enumerations and configuration bits, although the very first hunk of patch 1 (the P()) wants delaying until the final patch; we shouldn't print out the capability until it's being used.
The patch subjects want to be:
x86/svm: Enumerations for virtual NMI
and
x86/svm: Use virtual NMI when available
Everything here is local to SVM. Notably there should be no edits to hvm.c or hvm.h. By introducing hvm_intblk_vnmi, you break NMI injection in other cases. vNMI is just a hardware-optimised way of handling the hvm_intblk_nmi_iret case.
svm_inject_nmi() wants to gain a check to see whether vNMI is enabled, and in the case that it is, simply set vnmi_pending. You have this partially, but it needs to be dependent on the VMCB vNMI setting, not some global idea of enablement.
svm_get_interrupt_shadow() needs a similar adjustment to read vnmi_blocked rather than unconditionally depending on INTERCEPT_IRET.
In construct_vmcb(), you need to check cpu_has_svm_vnmi. I think this change is simple enough to be enabled unconditionally. (We'll need to change this in due course, but that's going to take other infrastructure which we don't have yet.)
I think that's everything that needs altering.
A couple of other minor notes:
In the vintr_t union, use an anonymous 3 bit field (literally "u64 :3;", which is valid syntax) instead of renumbering the rsvd$N fields. That will shrink the diff.
Xen's style has spaces inside the outermost brackets for control structures, and {'s on new lines. For the functions you're modifying, just copy the surrounding style.
~Andrew
Quoted 10 lines
Patches 1 and 2 want merging. They're both enumerations and configuration bits, although the very first hunk of patch 1 (the
P()) wants delaying until the final patch; we shouldn't print out the capability until it's being used.The patch subjects want to be:
x86/svm: Enumerations for virtual NMI
and
x86/svm: Use virtual NMI when available
Ack.
I Will merge these in V2.
I will move (the P()) hunk to patch 2.
Quoted 6 lines
Everything here is local to SVM. Notably there should be no edits to
hvm.corhvm.h. By introducinghvm_intblk_vnmi, you break NMI injection in other cases. vNMI is just a hardware-optimised way of handling thehvm_intblk_nmi_iretcase.
The handling for the introduced hvm_intblk_vnmi is separate from the other interrupts handling. I will remove the introduced hvm_intblk_vnmi and I will rather use the existing hvm_intblk_nmi_iret. This should have the side effect of setting the VMCB's interrupt-shadow. However, it should be benign side effect as this should not currently cause an actual interrupt shadow on the AMD platforms prior to the vNMI support.
Quoted 8 lines
svm_inject_nmi()wants to gain a check to see whether vNMI is enabled, and in the case that it is, simply setvnmi_pending. You have this partially, but it needs to be dependent on the VMCB vNMI setting, not some global idea of enablement.
I will add one more is_vnmi_enabled callback for the check functionality and I will use that introduced check in the svm_inject_nmi
Quoted 3 lines
svm_get_interrupt_shadow()needs a similar adjustment to readvnmi_blockedrather than unconditionally depending onINTERCEPT_IRET.
Ack, I will add the check in the V2
Quoted 5 lines
In
construct_vmcb(), you need to checkcpu_has_svm_vnmi. I think this change is simple enough to be enabled unconditionally. (We'll need to change this in due course, but that's going to take other infrastructure which we don't have yet.)
Ack, I will change it, as suggested, in the V2
Quoted 7 lines
A couple of other minor notes:
In the
vintr_tunion, use an anonymous 3 bit field (literally "u64 :3;", which is valid syntax) instead of renumbering the rsvd$N fields. That will shrink the diff.
Ack, I will change it, as suggested, in the V2
Quoted 4 lines
Xen's style has spaces inside the outermost brackets for control structures, and {'s on new lines. For the functions you're modifying, just copy the surrounding style.
Ack, I will stick to the formatting guideline in the V2
--Abdelkareem