Skip to contentSkip to conversation

Virtual NMI

PATCH Superseded Abdelkareem Abdelsaamad 3 participants 6 messages Feb 18 – Feb 19

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 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 v4:
- Delete the changes in svm_get_interrupt_shadow. The vNMI handling for multiple\concurrent NMIs should not be considered blocked if vnmi_pending and vnmi_blockking are set.
- Fix formatting indentation in the svm_inject_nmi function.
- CI tests:
https://gitlab.com/xen-project/people/aabdelsa/xen/-/pipelines/2334257666
---
Changes in v3:
- Delete the introduced hvm_function_table::vNMI boolean and all the 3 vNMI hooks, hvm_function_table::is_vnmi_enabled, hvm_function_table::is_vnmi_masked and hvm_function_table::set_vnmi_pending.
- Use the HVM_INTR_SHADOW_NMI to indicate that the NMI is blocked rather than the usage of HVM_INTR_SHADOW_MOV_SS | HVM_INTR_SHADOW_STI.
- CI tests:
https://gitlab.com/xen-project/people/aabdelsa/xen/-/pipelines/2327265341
---
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        | 8 ++++++++
 xen/arch/x86/hvm/svm/svm.c         | 1 +
 xen/arch/x86/hvm/svm/vmcb.c        | 2 ++
 xen/arch/x86/hvm/svm/vmcb.h        | 8 ++++++--
 xen/arch/x86/include/asm/hvm/svm.h | 2 ++
 5 files changed, 19 insertions(+), 2 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v4 1/2 x86/svm: Add Enumerations for the SVM virtual NMI

Introduce 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/vmcb.h        | 8 ++++++--
 xen/arch/x86/include/asm/hvm/svm.h | 2 ++
 2 files changed, 8 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/svm/vmcb.h +6 -2
index 69f6047394..13ccfd3ff9 100644
@@ -336,13 +336,17 @@ typedef union
         u64 tpr:          8;
         u64 irq:          1;
         u64 vgif:         1;
-        u64 rsvd0:        6;
+        u64 :             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 :             5;
         u64 vector:       8;
         u64 rsvd3:       24;
     } fields;
xen/arch/x86/include/asm/hvm/svm.h +2 -0
index 15f0268be7..a35a61273b 100644
@@ -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
#3 lore raw

PATCH v4 2/2 x86/svm: Use the virtual NMI when available

In reply to Abdelkareem Abdelsaamad

With the Virtual NMI (vNMI), the pending NMI is simply stuffed into the VMCB and handed off to the hardware. There is no need for the artificial tracking of the NMI handling completion with the IRET instruction interception.

Adjust the svm_inject_nmi to rather inject the NMIs using the vNMI Hardware accelerated feature when the AMD platform supports the vNMI.

---
 xen/arch/x86/hvm/svm/intr.c | 8 ++++++++
 xen/arch/x86/hvm/svm/svm.c  | 1 +
 xen/arch/x86/hvm/svm/vmcb.c | 2 ++
 3 files changed, 11 insertions(+)
xen/arch/x86/hvm/svm/intr.c +8 -0
index 6453a46b85..996362f8a6 100644
@@ -33,6 +33,14 @@ static void svm_inject_nmi(struct vcpu *v)
     u32 general1_intercepts = vmcb_get_general1_intercepts(vmcb);
     intinfo_t event;
 
+    if ( vmcb->_vintr.fields.vnmi_enable )
+    {
+        if ( !vmcb->_vintr.fields.vnmi_pending )
+            vmcb->_vintr.fields.vnmi_pending = 1;
+
+        return;
+    }
+
     event.raw = 0;
     event.v = true;
     event.type = X86_ET_NMI;
xen/arch/x86/hvm/svm/svm.c +1 -0
index 18ba837738..815565c33f 100644
@@ -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
 
xen/arch/x86/hvm/svm/vmcb.c +2 -0
index e583ef8548..e90bbac332 100644
@@ -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;
 }
 
Signature · 1 line
2.52.0
Acked-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] x86/svm: Add Enumerations for the SVM virtual NMI

In reply to Abdelkareem Abdelsaamad
Quoted 7 lines · Abdelkareem Abdelsaamad

Introduce 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:

Quoted 6 lines

vintr_t(11) - Virtual NMI is pending.
vintr_t(12) - Virtual NMI is masked.
vintr_t(26) - Enable NMI virtualization.

#4 lore raw

Re: [PATCH v4 2/2] x86/svm: Use the virtual NMI when available

In reply to Abdelkareem Abdelsaamad

xen/arch/x86/hvm/svm/intr.c static void svm_inject_nmi(struct vcpu *v) · line 39

Quoted 10 lines · Abdelkareem Abdelsaamad
xen/arch/x86/hvm/svm/intr.c +4 -0
@@ -33,6 +33,14 @@ static void svm_inject_nmi(struct vcpu *v)
     u32 general1_intercepts = vmcb_get_general1_intercepts(vmcb);
     intinfo_t event;
 
+    if ( vmcb->_vintr.fields.vnmi_enable )
+    {
+        if ( !vmcb->_vintr.fields.vnmi_pending )
+            vmcb->_vintr.fields.vnmi_pending = 1;

Why the (inner) conditional?

Also, as I think Teddy had already indicated before, might we better not use the _vintr field directly, but rather use the vmcb_[gs]et_vintr() wrappers? I notice nestedsvm.c open-codes many of the accesses, but elsewhere I see just one such case. The field name has a leading underscore for this reason, iirc.

Jan

#5 lore raw

Re: [PATCH v4 2/2] x86/svm: Use the virtual NMI when available

Quoted 7 lines · Jan Beulich
Quoted 6 lines · Abdelkareem Abdelsaamad
xen/arch/x86/hvm/svm/intr.c +0 -0
@@ -33,6 +33,14 @@ static void svm_inject_nmi(struct vcpu *v)
     u32 general1_intercepts = vmcb_get_general1_intercepts(vmcb);
     intinfo_t event;
 
Quoted 8 lines

xen/arch/x86/hvm/svm/intr.c static void svm_inject_nmi(struct vcpu *v) · line 39

Quoted 4 lines
xen/arch/x86/hvm/svm/intr.c +4 -0
+    if ( vmcb->_vintr.fields.vnmi_enable )
+    {
+        if ( !vmcb->_vintr.fields.vnmi_pending )
+            vmcb->_vintr.fields.vnmi_pending = 1;

Why the (inner) conditional?

Also, as I think Teddy had already indicated before, might we better not use the _vintr field directly, but rather use the vmcb_[gs]et_vintr() wrappers?

vNMI is not covered by the TPR cleanbit, and needs to avoid that setter.  This register is a complete mess, given how it's grown over the generations.

Leave it opencoded for now, and I'll see about cleaning things up when getting properly into the nested virt work.

~Andrew