Virtual NMI
PATCH v2 1/2 x86/svm: Add Enumerations for the SVM virtual NMI
In reply to Abdelkareem AbdelsaamadIntroduce the cpuid bit for the SVM vNMI feature support for the x86\AMD platforms. The feature support is indicated by the CPUID Fn8000_000A_EDX[25] = 1.
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/svm.c | 1 + xen/arch/x86/hvm/svm/vmcb.h | 8 ++++++-- xen/arch/x86/include/asm/hvm/hvm.h | 4 +++- xen/arch/x86/include/asm/hvm/svm.h | 2 ++ 4 files changed, 12 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/svm/svm.c +1 -0
@@ -2533,6 +2533,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/hvm/svm/vmcb.h +6 -2
@@ -336,13 +336,17 @@ 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:3 ; u64 prio: 4; u64 ign_tpr: 1; u64 rsvd1: 3; u64 intr_masking: 1; u64 vgif_enable: 1; - u64 rsvd2: 6; + u64 vnmi_enable: 1; + u64 rsvd2: 5; u64 vector: 8; u64 rsvd3: 24; } fields;
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
v2 cover Virtual NMI
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]
[1] https://docs.amd.com/v/u/en-US/24593_3.43
---
Changes in v2:
- Merge the patches for
"svm/vnmi: introduce the vnmi bit support in the cpuid feature set" and
"svm/vnmi: add the definitions for the svm vnmi management bits in the VMCB" into the one patch
"x86/svm: Add Enumerations for the SVM virtual NMI".
- Change the patch subject "svm/vnmi: Add support for the SVM Virtual NMI" to "x86/svm: Use virtual NMI when available"
- Move the print out of the VNMI capability to the patch "x86/svm: Use virtual NMI when available" when it is being used.
- Remove the hvm_intblk_vnmi enumeration that was introduced for the separate masked vNMI handling.
- Introduce the hvm_function_table's is_vnmi_enabled callback to allow for the SVM implementation to verify that the VNMI is enabled by checking the VMCB bits in addition to the CPUID reported support.
- Change the the name of the hvm_function_table's callback is_vnmi_pending to is_vnmi_masked.
- The svm_inject_nmi function gains a check for the distinct NMI injection handling when the VNMI is enabled.
- Simplify the check of AMD SVM Hardware support for the VNMI. The check cpu_has_svm_vnmi is performed unconditionally.
- Simplify the introduced changes diff for the vintr_t union for the VNMI bits support.
- Adopt the Xen formatting guidelines for the control structures.
- CI tests:
https://gitlab.com/xen-project/people/aabdelsa/xen/-/pipelines/2325298577
---
Abdelkareem Abdelsaamad (2):
x86/svm: Add Enumerations for the SVM virtual NMI x86/svm: Use the virtual NMI when available
xen/arch/x86/hvm/svm/intr.c | 13 ++++++++++-- xen/arch/x86/hvm/svm/svm.c | 34 ++++++++++++++++++++++++++++-- xen/arch/x86/hvm/svm/vmcb.c | 2 ++ xen/arch/x86/hvm/svm/vmcb.h | 8 +++++-- xen/arch/x86/include/asm/hvm/hvm.h | 13 +++++++++++- xen/arch/x86/include/asm/hvm/svm.h | 2 ++ 6 files changed, 65 insertions(+), 7 deletions(-)
Signature · 1 line
2.52.0
PATCH v2 2/2 x86/svm: Use the virtual NMI when available
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/svm/intr.c | 13 ++++++++++-- xen/arch/x86/hvm/svm/svm.c | 33 ++++++++++++++++++++++++++++-- xen/arch/x86/hvm/svm/vmcb.c | 2 ++ xen/arch/x86/include/asm/hvm/hvm.h | 9 ++++++++ 4 files changed, 53 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/svm/intr.c +11 -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.is_vnmi_enabled (v) ) + { + 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 ;
xen/arch/x86/hvm/svm/svm.c +31 -2
@@ -297,6 +297,32 @@ 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_enabled(struct vcpu *vcpu) +{ +return ( hvm_funcs.caps.vNMI && + vcpu->arch.hvm.svm.vmcb->_vintr.fields.vnmi_enable ); +} + +static bool cf_check svm_is_vnmi_masked(struct vcpu *vcpu) +{ +if ( !svm_is_vnmi_enabled (vcpu) ) +return false; + +return ( vcpu->arch.hvm.svm.vmcb->_vintr.fields.vnmi_blocking ); +} + +static bool cf_check svm_set_vnmi_pending(struct vcpu *vcpu) +{ +if ( !svm_is_vnmi_enabled (vcpu) ) +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) {
@@ -545,7 +571,7 @@ static unsigned cf_check int svm_get_interrupt_shadow(struct vcpu *v)struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;unsigned int intr_shadow =0 ; -if ( vmcb->int_stat.intr_shadow ) +if ( vmcb->int_stat.intr_shadow ||svm_is_vnmi_masked (v) ) intr_shadow |=HVM_INTR_SHADOW_MOV_SS |HVM_INTR_SHADOW_STI ;if (vmcb_get_general1_intercepts (vmcb) &GENERAL1_INTERCEPT_IRET )
@@ -2464,7 +2490,9 @@ 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_enabled = svm_is_vnmi_enabled, + .is_vnmi_masked = svm_is_vnmi_masked, + .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,
@@ -2524,6 +2552,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
xen/arch/x86/hvm/svm/vmcb.c +2 -0
@@ -184,6 +184,8 @@ static int construct_vmcb(struct vcpu *v)if ( default_xen_spec_ctrl ==SPEC_CTRL_STIBP ) v->arch.msrs->spec_ctrl.raw =SPEC_CTRL_STIBP ; + vmcb->_vintr.fields.vnmi_enable = cpu_has_svm_vnmi; +return 0 ; }
xen/arch/x86/include/asm/hvm/hvm.h +9 -0
@@ -224,6 +224,15 @@ 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 virtual NMI is enabled. */ +bool (*is_vnmi_enabled)(struct vcpu *vcpu); +/* Whether or not a virtual NMI is masked in hardware. */ +bool (*is_vnmi_masked)(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
Re: [PATCH v2 1/2] x86/svm: Add Enumerations for the SVM virtual NMI
In reply to Abdelkareem Abdelsaamadxen/arch/x86/include/asm/hvm/hvm.h struct hvm_function_table { · line 121
Quoted 13 lines · Abdelkareem Abdelsaamad
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;
As said previously, vNMI is local to SVM, and needs to not touch common HVM logic.
Specifically, delete this vNMI boolean and all 3 hooks you add in the next patch (i.e. all changes to hvm.h). They're all layering violations.
~Andrew
Re: [PATCH v2 2/2] x86/svm: Use the virtual NMI when available
In reply to Abdelkareem AbdelsaamadQuoted 31 lines · Abdelkareem Abdelsaamad
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_tablecallbacks to support the SVM's vNMI to allow the Xen hypervisor to query if a vNMI is pending and to set VMCB's_vintrpending flag so the NMIs are serviced by hardware if/when the virtual NMIs become unblocked.Signed-off-by Abdelkareem Abdelsaamad <[email protected]>--- xen/arch/x86/hvm/svm/intr.c | 13 ++++++++++-- xen/arch/x86/hvm/svm/svm.c | 33 ++++++++++++++++++++++++++++-- xen/arch/x86/hvm/svm/vmcb.c | 2 ++ xen/arch/x86/include/asm/hvm/hvm.h | 9 ++++++++ 4 files changed, 53 insertions(+), 4 deletions(-)xen/arch/x86/hvm/svm/intr.c +2
-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;
xen/arch/x86/hvm/svm/intr.c line 44
Quoted 10 lines
xen/arch/x86/hvm/svm/intr.c +9
-0+if ( hvm_funcs.is_vnmi_enabled (v) ) + { + hvm_funcs.set_vnmi_pending (v); +return ; + } + + vmcb = v->arch.hvm.svm.vmcb; + general1_intercepts =vmcb_get_general1_intercepts (vmcb); +
There's no need to defer these assignments.
When the HVM hooks are deleted, the correct logic here is:
if ( vmcb->_vintr.fields.vnmi_enable )
{
vmcb->_vintr.fields.vnmi_pending = true;
return;
}
xen/arch/x86/hvm/svm/svm.c static unsigned cf_check int svm_get_interrupt_shadow(struct vcpu *v) · line 577
Quoted 13 lines
xen/arch/x86/hvm/svm/svm.c +1
-1@@ -545,7 +571,7 @@ static unsigned cf_check int svm_get_interrupt_shadow(struct vcpu *v)struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;unsigned int intr_shadow =0 ; -if ( vmcb->int_stat.intr_shadow ) +if ( vmcb->int_stat.intr_shadow ||svm_is_vnmi_masked (v) ) intr_shadow |=HVM_INTR_SHADOW_MOV_SS |HVM_INTR_SHADOW_STI ;if (vmcb_get_general1_intercepts (vmcb) &GENERAL1_INTERCEPT_IRET )
It's only HVM_INTR_SHADOW_NMI which vNMI applies to.
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -548,7 +548,9 @@ static unsigned cf_check int svm_get_interrupt_shadow(struct vcpu *v)
if ( vmcb->int_stat.intr_shadow )
intr_shadow |= HVM_INTR_SHADOW_MOV_SS | HVM_INTR_SHADOW_STI;
- if ( vmcb_get_general1_intercepts(vmcb) & GENERAL1_INTERCEPT_IRET ) + if ( vmcb->_vintr.fields.vnmi_enable
+ ? vmcb->_vintr.fields.vnmi_blocked
+ : (vmcb_get_general1_intercepts(vmcb) & GENERAL1_INTERCEPT_IRET) ) intr_shadow |= HVM_INTR_SHADOW_NMI;
return intr_shadow;
~Andrew