Skip to contentSkip to conversation

Virtual NMI feature

PATCH Superseded Santosh Shukla 3 participants 31 messages Jun 2, 2022 – Sep 5, 2022

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Virtual NMI feature

Currently, NMI is delivered to the guest using the Event Injection mechanism [1]. The Event Injection mechanism does not block the delivery of subsequent NMIs. So the Hypervisor needs to track the NMI delivery and its completion(by intercepting IRET) before sending a new NMI.

Virtual NMI (VNMI) allows the hypervisor to inject the NMI into the guest w/o using Event Injection mechanism meaning not required to track the guest NMI and intercepting the IRET. To achieve that,
VNMI feature provides virtualized NMI and NMI_MASK capability bits in VMCB intr_control -
V_NMI(11) - Indicates whether a virtual NMI is pending in the guest. V_NMI_MASK(12) - Indicates whether virtual NMI is masked in the guest. V_NMI_ENABLE(26) - Enables the NMI virtualization feature for the guest.

When Hypervisor wants to inject NMI, it will set V_NMI bit, Processor will clear the V_NMI bit and Set the V_NMI_MASK which means the Guest is handling NMI, After the guest handled the NMI, The processor will clear the V_NMI_MASK on the successful completion of IRET instruction Or if VMEXIT occurs while delivering the virtual NMI.

To enable the VNMI capability, Hypervisor need to program V_NMI_ENABLE bit 1.

The presence of this feature is indicated via the CPUID function 0x8000000A_EDX[25].

Testing -
* Used qemu's inject_nmi for testing.
* tested with and w/o AVIC case.
* tested with kvm-unit-test

Thanks,
Santosh
[1] https://www.amd.com/system/files/TechDocs/40332.pdf - APM Vol2, ch-15.20 - "Event Injection".

Santosh Shukla (7):
x86/cpu: Add CPUID feature bit for VNMI
KVM: SVM: Add VNMI bit definition
KVM: SVM: Add VNMI support in get/set_nmi_mask
KVM: SVM: Report NMI not allowed when Guest busy handling VNMI
KVM: SVM: Add VNMI support in inject_nmi
KVM: nSVM: implement nested VNMI
KVM: SVM: Enable VNMI feature

 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/svm.h         |  7 +++++
 arch/x86/kvm/svm/nested.c          |  8 +++++
 arch/x86/kvm/svm/svm.c             | 47 ++++++++++++++++++++++++++++--
 arch/x86/kvm/svm/svm.h             |  1 +
 5 files changed, 62 insertions(+), 2 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/7 x86/cpu: Add CPUID feature bit for VNMI

VNMI feature allows the hypervisor to inject NMI into the guest w/o using Event injection mechanism, The benefit of using VNMI over the event Injection that does not require tracking the Guest's NMI state and intercepting the IRET for the NMI completion. VNMI achieves that by exposing 3 capability bits in VMCB intr_cntrl which helps with virtualizing NMI injection and NMI_Masking.

The presence of this feature is indicated via the CPUID function 0x8000000A_EDX[25].

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 393f2bbb5e3a..c8775b25856b 100644
@@ -346,6 +346,7 @@
 #define X86_FEATURE_V_VMSAVE_VMLOAD	(15*32+15) /* Virtual VMSAVE VMLOAD */
 #define X86_FEATURE_VGIF		(15*32+16) /* Virtual GIF */
 #define X86_FEATURE_V_SPEC_CTRL		(15*32+20) /* Virtual SPEC_CTRL */
+#define X86_FEATURE_V_NMI		(15*32+25) /* Virtual NMI */
 #define X86_FEATURE_SVME_ADDR_CHK	(15*32+28) /* "" SVME addr check */
 
 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ECX), word 16 */
Signature · 1 line
2.25.1
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] x86/cpu: Add CPUID feature bit for VNMI

Quoted 25 lines · Santosh Shukla

VNMI feature allows the hypervisor to inject NMI into the guest w/o using Event injection mechanism, The benefit of using VNMI over the event Injection that does not require tracking the Guest's NMI state and intercepting the IRET for the NMI completion. VNMI achieves that by exposing 3 capability bits in VMCB intr_cntrl which helps with virtualizing NMI injection and NMI_Masking.

The presence of this feature is indicated via the CPUID function 0x8000000A_EDX[25].

---
 arch/x86/include/asm/cpufeatures.h | 1 +

 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 393f2bbb5e3a..c8775b25856b 100644
@@ -346,6 +346,7 @@
  #define X86_FEATURE_V_VMSAVE_VMLOAD    (15*32+15) /* Virtual VMSAVE VMLOAD */
  #define X86_FEATURE_VGIF               (15*32+16) /* Virtual GIF */
  #define X86_FEATURE_V_SPEC_CTRL                (15*32+20) /* Virtual SPEC_CTRL */
+#define X86_FEATURE_V_NMI              (15*32+25) /* Virtual NMI */
  #define X86_FEATURE_SVME_ADDR_CHK      (15*32+28) /* "" SVME addr check */
Quoted 2 lines


 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ECX), word 16 */

I also think that AMD should publish some sort of a 'future ISA' spec like Intel does, so that we could avoid mistakes in reviweing the code.

Other than that:

Best regards,
Maxim Levitsky

#3 lore raw

PATCH 2/7 KVM: SVM: Add VNMI bit definition

In reply to Santosh Shukla

VNMI exposes 3 capability bits (V_NMI, V_NMI_MASK, and V_NMI_ENABLE) to virtualize NMI and NMI_MASK, Those capability bits are part of VMCB::intr_ctrl -
V_NMI(11) - Indicates whether a virtual NMI is pending in the guest. V_NMI_MASK(12) - Indicates whether virtual NMI is masked in the guest. V_NMI_ENABLE(26) - Enables the NMI virtualization feature for the guest.

When Hypervisor wants to inject NMI, it will set V_NMI bit, Processor will clear the V_NMI bit and Set the V_NMI_MASK which means the Guest is handling NMI, After the guest handled the NMI, The processor will clear the V_NMI_MASK on the successful completion of IRET instruction Or if VMEXIT occurs while delivering the virtual NMI.

To enable the VNMI capability, Hypervisor need to program V_NMI_ENABLE bit 1.

---
 arch/x86/include/asm/svm.h | 7 +++++++
 arch/x86/kvm/svm/svm.c     | 6 ++++++
 2 files changed, 13 insertions(+)
arch/x86/include/asm/svm.h +7 -0
index 1b07fba11704..22d918555df0 100644
@@ -195,6 +195,13 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 #define AVIC_ENABLE_SHIFT 31
 #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
 
+#define V_NMI_PENDING_SHIFT 11
+#define V_NMI_PENDING (1 << V_NMI_PENDING_SHIFT)
+#define V_NMI_MASK_SHIFT 12
+#define V_NMI_MASK (1 << V_NMI_MASK_SHIFT)
+#define V_NMI_ENABLE_SHIFT 26
+#define V_NMI_ENABLE (1 << V_NMI_ENABLE_SHIFT)
+
 #define LBR_CTL_ENABLE_MASK BIT_ULL(0)
 #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
 
arch/x86/kvm/svm/svm.c +6 -0
index 200045f71df0..860f28c668bd 100644
@@ -198,6 +198,8 @@ module_param(dump_invalid_vmcb, bool, 0644);
 bool intercept_smi = true;
 module_param(intercept_smi, bool, 0444);
 
+static bool vnmi;
+module_param(vnmi, bool, 0444);
 
 static bool svm_gp_erratum_intercept = true;
 
@@ -4930,6 +4932,10 @@ static __init int svm_hardware_setup(void)
 		svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL;
 	}
 
+	vnmi = vnmi && boot_cpu_has(X86_FEATURE_V_NMI);
+	if (vnmi)
+		pr_info("V_NMI enabled\n");
+
 	if (vls) {
 		if (!npt_enabled ||
 		    !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Signature · 1 line
2.25.1
Reviewed-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] KVM: SVM: Add VNMI bit definition

Quoted 4 lines · Santosh Shukla

VNMI exposes 3 capability bits (V_NMI, V_NMI_MASK, and V_NMI_ENABLE) to virtualize NMI and NMI_MASK, Those capability bits are part of VMCB::intr_ctrl -
V_NMI(11) - Indicates whether a virtual NMI is pending in the guest.

So this is like bit in IRR

Quoted 1 line

V_NMI_MASK(12) - Indicates whether virtual NMI is masked in the guest.

And that is like bit in ISR.

Question: what are the interactions with GIF/vGIF and this feature?

Quoted 51 lines

V_NMI_ENABLE(26) - Enables the NMI virtualization feature for the guest.

When Hypervisor wants to inject NMI, it will set V_NMI bit, Processor will clear the V_NMI bit and Set the V_NMI_MASK which means the Guest is handling NMI, After the guest handled the NMI, The processor will clear the V_NMI_MASK on the successful completion of IRET instruction Or if VMEXIT occurs while delivering the virtual NMI.

To enable the VNMI capability, Hypervisor need to program V_NMI_ENABLE bit 1.

---
 arch/x86/include/asm/svm.h | 7 +++++++
 arch/x86/kvm/svm/svm.c     | 6 ++++++

 2 files changed, 13 insertions(+)
arch/x86/include/asm/svm.h +0 -0
index 1b07fba11704..22d918555df0 100644
@@ -195,6 +195,13 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
  #define AVIC_ENABLE_SHIFT 31
  #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)

arch/x86/include/asm/svm.h +7 -0
+#define V_NMI_PENDING_SHIFT 11
+#define V_NMI_PENDING (1 << V_NMI_PENDING_SHIFT)
+#define V_NMI_MASK_SHIFT 12
+#define V_NMI_MASK (1 << V_NMI_MASK_SHIFT)
+#define V_NMI_ENABLE_SHIFT 26
+#define V_NMI_ENABLE (1 << V_NMI_ENABLE_SHIFT)
+

 #define LBR_CTL_ENABLE_MASK BIT_ULL(0) #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)

arch/x86/kvm/svm/svm.c +0 -0
index 200045f71df0..860f28c668bd 100644
@@ -198,6 +198,8 @@ module_param(dump_invalid_vmcb, bool, 0644);
  bool intercept_smi = true;
  module_param(intercept_smi, bool, 0444);

arch/x86/kvm/svm/svm.c +2 -0
+static bool vnmi;
+module_param(vnmi, bool, 0444);


 static bool svm_gp_erratum_intercept = true;

arch/x86/kvm/svm/svm.c +0 -0
@@ -4930,6 +4932,10 @@ static __init int svm_hardware_setup(void)
                 svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL;
         }
Quoted 8 lines


+       vnmi = vnmi && boot_cpu_has(X86_FEATURE_V_NMI); +       if (vnmi)
+               pr_info("V_NMI enabled\n");
+
        if (vls) {
                if (!npt_enabled || !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||

Best regards,
Maxim Levitsky

#5 lore raw

Re: [PATCH 2/7] KVM: SVM: Add VNMI bit definition

Quoted 5 lines · Maxim Levitsky
Quoted 4 lines · Santosh Shukla

VNMI exposes 3 capability bits (V_NMI, V_NMI_MASK, and V_NMI_ENABLE) to virtualize NMI and NMI_MASK, Those capability bits are part of VMCB::intr_ctrl -
V_NMI(11) - Indicates whether a virtual NMI is pending in the guest.

Quoted 7 lines

So this is like bit in IRR

Quoted 1 line

V_NMI_MASK(12) - Indicates whether virtual NMI is masked in the guest.

And that is like bit in ISR.

Question: what are the interactions with GIF/vGIF and this feature?

Hi Maxim,

The quick answer is, that V_NMI will respect the VGIF enable controls. More info about interaction in subsequent patch reply.

Thanks,
Santosh

Quoted 66 lines
Quoted 51 lines

V_NMI_ENABLE(26) - Enables the NMI virtualization feature for the guest.

When Hypervisor wants to inject NMI, it will set V_NMI bit, Processor will clear the V_NMI bit and Set the V_NMI_MASK which means the Guest is handling NMI, After the guest handled the NMI, The processor will clear the V_NMI_MASK on the successful completion of IRET instruction Or if VMEXIT occurs while delivering the virtual NMI.

To enable the VNMI capability, Hypervisor need to program V_NMI_ENABLE bit 1.

---
 arch/x86/include/asm/svm.h | 7 +++++++
 arch/x86/kvm/svm/svm.c     | 6 ++++++

 2 files changed, 13 insertions(+)
arch/x86/include/asm/svm.h +0 -0
index 1b07fba11704..22d918555df0 100644
@@ -195,6 +195,13 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
  #define AVIC_ENABLE_SHIFT 31
  #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)

arch/x86/include/asm/svm.h +7 -0
+#define V_NMI_PENDING_SHIFT 11
+#define V_NMI_PENDING (1 << V_NMI_PENDING_SHIFT)
+#define V_NMI_MASK_SHIFT 12
+#define V_NMI_MASK (1 << V_NMI_MASK_SHIFT)
+#define V_NMI_ENABLE_SHIFT 26
+#define V_NMI_ENABLE (1 << V_NMI_ENABLE_SHIFT)
+

 #define LBR_CTL_ENABLE_MASK BIT_ULL(0) #define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)

arch/x86/kvm/svm/svm.c +0 -0
index 200045f71df0..860f28c668bd 100644
@@ -198,6 +198,8 @@ module_param(dump_invalid_vmcb, bool, 0644);
  bool intercept_smi = true;
  module_param(intercept_smi, bool, 0444);

arch/x86/kvm/svm/svm.c +2 -0
+static bool vnmi;
+module_param(vnmi, bool, 0444);


 static bool svm_gp_erratum_intercept = true;

arch/x86/kvm/svm/svm.c +0 -0
@@ -4930,6 +4932,10 @@ static __init int svm_hardware_setup(void)
                 svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL;
         }
Quoted 8 lines


+       vnmi = vnmi && boot_cpu_has(X86_FEATURE_V_NMI); +       if (vnmi)
+               pr_info("V_NMI enabled\n");
+
        if (vls) {
                if (!npt_enabled || !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||

Best regards,
Maxim Levitsky

#6 lore raw

PATCH 3/7 KVM: SVM: Add VNMI support in get/set_nmi_mask

In reply to Santosh Shukla

VMCB intr_ctrl bit12 (V_NMI_MASK) is set by the processor when handling NMI in guest and is cleared after the NMI is handled. Treat V_NMI_MASK as read-only in the hypervisor and do not populate set accessors.

---
 arch/x86/kvm/svm/svm.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +19 -1
index 860f28c668bd..d67a54517d95 100644
@@ -323,6 +323,16 @@ static int is_external_interrupt(u32 info)
 	return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
 }
 
+static bool is_vnmi_enabled(struct vmcb *vmcb)
+{
+	return vnmi && (vmcb->control.int_ctl & V_NMI_ENABLE);
+}
+
+static bool is_vnmi_mask_set(struct vmcb *vmcb)
+{
+	return !!(vmcb->control.int_ctl & V_NMI_MASK);
+}
+
 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -3502,13 +3512,21 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 
 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
 {
-	return !!(vcpu->arch.hflags & HF_NMI_MASK);
+	struct vcpu_svm *svm = to_svm(vcpu);
+
+	if (is_vnmi_enabled(svm->vmcb))
+		return is_vnmi_mask_set(svm->vmcb);
+	else
+		return !!(vcpu->arch.hflags & HF_NMI_MASK);
 }
 
 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
+	if (is_vnmi_enabled(svm->vmcb))
+		return;
+
 	if (masked) {
 		vcpu->arch.hflags |= HF_NMI_MASK;
 		if (!sev_es_guest(vcpu->kvm))
Signature · 1 line
2.25.1
#7 lore raw

Re: [PATCH 3/7] KVM: SVM: Add VNMI support in get/set_nmi_mask

Quoted 17 lines · Santosh Shukla

VMCB intr_ctrl bit12 (V_NMI_MASK) is set by the processor when handling NMI in guest and is cleared after the NMI is handled. Treat V_NMI_MASK as read-only in the hypervisor and do not populate set accessors.

---
 arch/x86/kvm/svm/svm.c | 20 +++++++++++++++++++-

 1 file changed, 19 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +0 -0
index 860f28c668bd..d67a54517d95 100644
@@ -323,6 +323,16 @@ static int is_external_interrupt(u32 info)
         return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  }

arch/x86/kvm/svm/svm.c static int is_external_interrupt(u32 info) · line 329

Quoted 4 lines
arch/x86/kvm/svm/svm.c +4 -0
+static bool is_vnmi_enabled(struct vmcb *vmcb)
+{
+       return vnmi && (vmcb->control.int_ctl & V_NMI_ENABLE);
+}

Following Paolo's suggestion I recently removed vgif_enabled(), based on the logic that vgif_enabled == vgif, because
we always enable vGIF for L1 as long as 'vgif' module param is set, which is set unless either hardware or user cleared it.

Note that here vmcb is the current vmcb, which can be vmcb02, and it might be wrong

Quoted 20 lines
arch/x86/kvm/svm/svm.c +6 -0
+
+static bool is_vnmi_mask_set(struct vmcb *vmcb)
+{
+       return !!(vmcb->control.int_ctl & V_NMI_MASK);
+}
+

 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) {
        struct vcpu_svm *svm = to_svm(vcpu);

arch/x86/kvm/svm/svm.c +0 -0
@@ -3502,13 +3512,21 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)


 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) {
-       return !!(vcpu->arch.hflags & HF_NMI_MASK); +       struct vcpu_svm *svm = to_svm(vcpu);
+
+       if (is_vnmi_enabled(svm->vmcb))
+               return is_vnmi_mask_set(svm->vmcb); +       else
+               return !!(vcpu->arch.hflags & HF_NMI_MASK);

Quoted 8 lines

 }

 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) {
        struct vcpu_svm *svm = to_svm(vcpu);

+       if (is_vnmi_enabled(svm->vmcb))
+               return;

What if the KVM wants to mask NMI, shoudn't we update the V_NMI_MASK value in int_ctl instead of doing nothing?

Best regards,
Maxim Levitsky

Quoted 4 lines

+
        if (masked) {
                vcpu->arch.hflags |= HF_NMI_MASK;
                if (!sev_es_guest(vcpu->kvm))

#8 lore raw

Re: [PATCH 3/7] KVM: SVM: Add VNMI support in get/set_nmi_mask

Quoted 19 lines · Maxim Levitsky
Quoted 18 lines · Santosh Shukla

VMCB intr_ctrl bit12 (V_NMI_MASK) is set by the processor when handling NMI in guest and is cleared after the NMI is handled. Treat V_NMI_MASK as read-only in the hypervisor and do not populate set accessors.

---
 arch/x86/kvm/svm/svm.c | 20 +++++++++++++++++++-

 1 file changed, 19 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +0 -0
index 860f28c668bd..d67a54517d95 100644
@@ -323,6 +323,16 @@ static int is_external_interrupt(u32 info)
         return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  }


+static bool is_vnmi_enabled(struct vmcb *vmcb)

Quoted 9 lines

arch/x86/kvm/svm/svm.c static int is_external_interrupt(u32 info) · line 329

Quoted 3 lines
arch/x86/kvm/svm/svm.c +3 -0
+{
+       return vnmi && (vmcb->control.int_ctl & V_NMI_ENABLE);
+}

Following Paolo's suggestion I recently removed vgif_enabled(), based on the logic that vgif_enabled == vgif, because
we always enable vGIF for L1 as long as 'vgif' module param is set, which is set unless either hardware or user cleared it.

Yes. In v2, Thanks!.

Quoted 45 lines

Note that here vmcb is the current vmcb, which can be vmcb02, and it might be wrong

Quoted 20 lines
arch/x86/kvm/svm/svm.c +6 -0
+
+static bool is_vnmi_mask_set(struct vmcb *vmcb)
+{
+       return !!(vmcb->control.int_ctl & V_NMI_MASK);
+}
+

 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) {
        struct vcpu_svm *svm = to_svm(vcpu);

arch/x86/kvm/svm/svm.c +0 -0
@@ -3502,13 +3512,21 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)


 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) {
-       return !!(vcpu->arch.hflags & HF_NMI_MASK); +       struct vcpu_svm *svm = to_svm(vcpu);
+
+       if (is_vnmi_enabled(svm->vmcb))
+               return is_vnmi_mask_set(svm->vmcb); +       else
+               return !!(vcpu->arch.hflags & HF_NMI_MASK);

Quoted 8 lines

 }

 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) {
        struct vcpu_svm *svm = to_svm(vcpu);

+       if (is_vnmi_enabled(svm->vmcb))
+               return;

What if the KVM wants to mask NMI, shoudn't we update the V_NMI_MASK value in int_ctl instead of doing nothing?

Best regards,
Maxim Levitsky

Quoted 4 lines

+
        if (masked) {
                vcpu->arch.hflags |= HF_NMI_MASK;
                if (!sev_es_guest(vcpu->kvm))

#9 lore raw

Re: [PATCH 3/7] KVM: SVM: Add VNMI support in get/set_nmi_mask

Quoted 31 lines · Santosh
Quoted 19 lines · Maxim Levitsky
Quoted 18 lines · Santosh Shukla

VMCB intr_ctrl bit12 (V_NMI_MASK) is set by the processor when handling NMI in guest and is cleared after the NMI is handled. Treat V_NMI_MASK as read-only in the hypervisor and do not populate set accessors.

---
 arch/x86/kvm/svm/svm.c | 20 +++++++++++++++++++-

 1 file changed, 19 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +0 -0
index 860f28c668bd..d67a54517d95 100644
@@ -323,6 +323,16 @@ static int is_external_interrupt(u32 info)
         return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  }


+static bool is_vnmi_enabled(struct vmcb *vmcb)

Quoted 9 lines

arch/x86/kvm/svm/svm.c static int is_external_interrupt(u32 info) · line 329

Quoted 3 lines
arch/x86/kvm/svm/svm.c +3 -0
+{
+       return vnmi && (vmcb->control.int_ctl & V_NMI_ENABLE);
+}

Following Paolo's suggestion I recently removed vgif_enabled(), based on the logic that vgif_enabled == vgif, because
we always enable vGIF for L1 as long as 'vgif' module param is set, which is set unless either hardware or user cleared it.

Quoted 37 lines

Yes. In v2, Thanks!.

Quoted 35 lines

Note that here vmcb is the current vmcb, which can be vmcb02, and it might be wrong

Quoted 20 lines
arch/x86/kvm/svm/svm.c +6 -0
+
+static bool is_vnmi_mask_set(struct vmcb *vmcb)
+{
+       return !!(vmcb->control.int_ctl & V_NMI_MASK);
+}
+

 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu) {
        struct vcpu_svm *svm = to_svm(vcpu);

arch/x86/kvm/svm/svm.c +0 -0
@@ -3502,13 +3512,21 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)


 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) {
-       return !!(vcpu->arch.hflags & HF_NMI_MASK); +       struct vcpu_svm *svm = to_svm(vcpu);
+
+       if (is_vnmi_enabled(svm->vmcb))
+               return is_vnmi_mask_set(svm->vmcb); +       else
+               return !!(vcpu->arch.hflags & HF_NMI_MASK);

Quoted 8 lines

 }

 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) {
        struct vcpu_svm *svm = to_svm(vcpu);

+       if (is_vnmi_enabled(svm->vmcb))
+               return;

What if the KVM wants to mask NMI, shoudn't we update the V_NMI_MASK value in int_ctl instead of doing nothing?

V_NMI_MASK is cpu controlled meaning HW sets the mask while processing event and clears right after processing, so in away its Read-only for hypervisor.

Quoted 10 lines
Quoted 10 lines

Best regards,
Maxim Levitsky

Quoted 4 lines

+
        if (masked) {
                vcpu->arch.hflags |= HF_NMI_MASK;
                if (!sev_es_guest(vcpu->kvm))

#10 lore raw

Re: [PATCH 3/7] KVM: SVM: Add VNMI support in get/set_nmi_mask

Quoted 70 lines · Santosh
Quoted 30 lines · Santosh
Quoted 18 lines · Maxim Levitsky
Quoted 17 lines · Santosh Shukla

VMCB intr_ctrl bit12 (V_NMI_MASK) is set by the processor when handling NMI in guest and is cleared after the NMI is handled. Treat V_NMI_MASK as read-only in the hypervisor and do not populate set accessors.

---
 arch/x86/kvm/svm/svm.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +0 -0
index 860f28c668bd..d67a54517d95 100644
@@ -323,6 +323,16 @@ static int is_external_interrupt(u32 info)
        return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
 }
 
Quoted 10 lines

arch/x86/kvm/svm/svm.c static int is_external_interrupt(u32 info) · line 329

Quoted 4 lines
arch/x86/kvm/svm/svm.c +4 -0
+static bool is_vnmi_enabled(struct vmcb *vmcb)
+{
+       return vnmi && (vmcb->control.int_ctl & V_NMI_ENABLE);
+}

Following Paolo's suggestion I recently removed vgif_enabled(), based on the logic that vgif_enabled == vgif, because
we always enable vGIF for L1 as long as 'vgif' module param is set, which is set unless either hardware or user cleared it.

Quoted 37 lines

Yes. In v2, Thanks!.

Quoted 35 lines

Note that here vmcb is the current vmcb, which can be vmcb02, and it might be wrong

Quoted 18 lines
arch/x86/kvm/svm/svm.c +10 -1
+
+static bool is_vnmi_mask_set(struct vmcb *vmcb)
+{
+       return !!(vmcb->control.int_ctl & V_NMI_MASK);
+}
+
 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
@@ -3502,13 +3512,21 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 
 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
 {
-       return !!(vcpu->arch.hflags & HF_NMI_MASK);
+       struct vcpu_svm *svm = to_svm(vcpu);
+
+       if (is_vnmi_enabled(svm->vmcb))
+               return is_vnmi_mask_set(svm->vmcb);

arch/x86/kvm/svm/svm.c static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection) · line 3528

Quoted 10 lines
arch/x86/kvm/svm/svm.c +4 -0
+       else
+               return !!(vcpu->arch.hflags & HF_NMI_MASK);
 }
 
 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
+       if (is_vnmi_enabled(svm->vmcb))
+               return;

What if the KVM wants to mask NMI, shoudn't we update the V_NMI_MASK value in int_ctl instead of doing nothing?

Quoted 2 lines

V_NMI_MASK is cpu controlled meaning HW sets the mask while processing event and clears right after processing, so in away its Read-only for hypervisor.

And yet, svm_set_nmi_mask is called when KVM wants to explicitly mask NMI without injecting a NMI, it does this when entering (emulated) SMI.

So the KVM has to set V_NMI_MASK here, becaue no real NMI is injected, and thus the CPU will not set this bit itself.

Best regards,
Maxim Levitsky

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Best regards,
Maxim Levitsky

Quoted 4 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if (masked) {
                vcpu->arch.hflags |= HF_NMI_MASK;
                if (!sev_es_guest(vcpu->kvm))
#11 lore raw

Re: [PATCH 3/7] KVM: SVM: Add VNMI support in get/set_nmi_mask

Quoted 71 lines · Maxim Levitsky
Quoted 70 lines · Santosh
Quoted 67 lines · Santosh
Quoted 18 lines · Maxim Levitsky
Quoted 17 lines · Santosh Shukla

VMCB intr_ctrl bit12 (V_NMI_MASK) is set by the processor when handling NMI in guest and is cleared after the NMI is handled. Treat V_NMI_MASK as read-only in the hypervisor and do not populate set accessors.

---
 arch/x86/kvm/svm/svm.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +0 -0
index 860f28c668bd..d67a54517d95 100644
@@ -323,6 +323,16 @@ static int is_external_interrupt(u32 info)
        return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
 }
 
Quoted 10 lines

arch/x86/kvm/svm/svm.c static int is_external_interrupt(u32 info) · line 329

Quoted 4 lines
arch/x86/kvm/svm/svm.c +4 -0
+static bool is_vnmi_enabled(struct vmcb *vmcb)
+{
+       return vnmi && (vmcb->control.int_ctl & V_NMI_ENABLE);
+}

Following Paolo's suggestion I recently removed vgif_enabled(), based on the logic that vgif_enabled == vgif, because
we always enable vGIF for L1 as long as 'vgif' module param is set, which is set unless either hardware or user cleared it.

Yes. In v2, Thanks!.

Quoted 35 lines

Note that here vmcb is the current vmcb, which can be vmcb02, and it might be wrong

Quoted 18 lines
arch/x86/kvm/svm/svm.c +10 -1
+
+static bool is_vnmi_mask_set(struct vmcb *vmcb)
+{
+       return !!(vmcb->control.int_ctl & V_NMI_MASK);
+}
+
 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
@@ -3502,13 +3512,21 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 
 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
 {
-       return !!(vcpu->arch.hflags & HF_NMI_MASK);
+       struct vcpu_svm *svm = to_svm(vcpu);
+
+       if (is_vnmi_enabled(svm->vmcb))
+               return is_vnmi_mask_set(svm->vmcb);

arch/x86/kvm/svm/svm.c static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection) · line 3528

Quoted 10 lines
arch/x86/kvm/svm/svm.c +4 -0
+       else
+               return !!(vcpu->arch.hflags & HF_NMI_MASK);
 }
 
 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
+       if (is_vnmi_enabled(svm->vmcb))
+               return;

What if the KVM wants to mask NMI, shoudn't we update the V_NMI_MASK value in int_ctl instead of doing nothing?

Quoted 9 lines
Quoted 2 lines

V_NMI_MASK is cpu controlled meaning HW sets the mask while processing event and clears right after processing, so in away its Read-only for hypervisor.

And yet, svm_set_nmi_mask is called when KVM wants to explicitly mask NMI without injecting a NMI, it does this when entering (emulated) SMI.

So the KVM has to set V_NMI_MASK here, becaue no real NMI is injected, and thus the CPU will not set this bit itself.

Yes, we will handle smm case in v3.

Thanks,
Santosh

Quoted 13 lines

Best regards,
Maxim Levitsky

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Best regards,
Maxim Levitsky

Quoted 4 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if (masked) {
                vcpu->arch.hflags |= HF_NMI_MASK;
                if (!sev_es_guest(vcpu->kvm))
#12 lore raw

Re: [PATCH 3/7] KVM: SVM: Add VNMI support in get/set_nmi_mask

In reply to Shukla, Santosh
Quoted 70 lines · Santosh
Quoted 30 lines · Santosh
Quoted 18 lines · Maxim Levitsky
Quoted 17 lines · Santosh Shukla

VMCB intr_ctrl bit12 (V_NMI_MASK) is set by the processor when handling NMI in guest and is cleared after the NMI is handled. Treat V_NMI_MASK as read-only in the hypervisor and do not populate set accessors.

---
 arch/x86/kvm/svm/svm.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +0 -0
index 860f28c668bd..d67a54517d95 100644
@@ -323,6 +323,16 @@ static int is_external_interrupt(u32 info)
        return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
 }
 
Quoted 10 lines

arch/x86/kvm/svm/svm.c static int is_external_interrupt(u32 info) · line 329

Quoted 4 lines
arch/x86/kvm/svm/svm.c +4 -0
+static bool is_vnmi_enabled(struct vmcb *vmcb)
+{
+       return vnmi && (vmcb->control.int_ctl & V_NMI_ENABLE);
+}

Following Paolo's suggestion I recently removed vgif_enabled(), based on the logic that vgif_enabled == vgif, because
we always enable vGIF for L1 as long as 'vgif' module param is set, which is set unless either hardware or user cleared it.

Quoted 37 lines

Yes. In v2, Thanks!.

Quoted 35 lines

Note that here vmcb is the current vmcb, which can be vmcb02, and it might be wrong

Quoted 18 lines
arch/x86/kvm/svm/svm.c +10 -1
+
+static bool is_vnmi_mask_set(struct vmcb *vmcb)
+{
+       return !!(vmcb->control.int_ctl & V_NMI_MASK);
+}
+
 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
@@ -3502,13 +3512,21 @@ static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 
 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
 {
-       return !!(vcpu->arch.hflags & HF_NMI_MASK);
+       struct vcpu_svm *svm = to_svm(vcpu);
+
+       if (is_vnmi_enabled(svm->vmcb))
+               return is_vnmi_mask_set(svm->vmcb);

arch/x86/kvm/svm/svm.c static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection) · line 3528

Quoted 10 lines
arch/x86/kvm/svm/svm.c +4 -0
+       else
+               return !!(vcpu->arch.hflags & HF_NMI_MASK);
 }
 
 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
+       if (is_vnmi_enabled(svm->vmcb))
+               return;

What if the KVM wants to mask NMI, shoudn't we update the V_NMI_MASK value in int_ctl instead of doing nothing?

Quoted 2 lines

V_NMI_MASK is cpu controlled meaning HW sets the mask while processing event and clears right after processing, so in away its Read-only for hypervisor.

And yet, svm_set_nmi_mask is called when KVM wants to explicitly mask NMI without injecting a NMI, it does this when entering (emulated) SMI.

So the KVM has to set V_NMI_MASK here, even though no real NMI was received.

Best regards,
Maxim Levitsky

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Best regards,
Maxim Levitsky

Quoted 4 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if (masked) {
                vcpu->arch.hflags |= HF_NMI_MASK;
                if (!sev_es_guest(vcpu->kvm))
#13 lore raw

PATCH 4/7 KVM: SVM: Report NMI not allowed when Guest busy handling VNMI

In reply to Santosh Shukla

In the VNMI case, Report NMI is not allowed when the processor set the V_NMI_MASK to 1 which means the Guest is busy handling VNMI.

---
 arch/x86/kvm/svm/svm.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kvm/svm/svm.c +6 -0
index d67a54517d95..a405e414cae4 100644
@@ -3483,6 +3483,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
 	struct vmcb *vmcb = svm->vmcb;
 	bool ret;
 
+	if (is_vnmi_enabled(vmcb) && is_vnmi_mask_set(vmcb))
+		return true;
+
 	if (!gif_set(svm))
 		return true;
 
@@ -3618,6 +3621,9 @@ static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
+	if (is_vnmi_enabled(svm->vmcb) && is_vnmi_mask_set(svm->vmcb))
+		return;
+
 	if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
 		return; /* IRET will cause a vm exit */
 
Signature · 1 line
2.25.1
#14 lore raw

Re: [PATCH 4/7] KVM: SVM: Report NMI not allowed when Guest busy handling VNMI

Quoted 15 lines · Santosh Shukla

In the VNMI case, Report NMI is not allowed when the processor set the V_NMI_MASK to 1 which means the Guest is busy handling VNMI.

---
 arch/x86/kvm/svm/svm.c | 6 ++++++

 1 file changed, 6 insertions(+)
arch/x86/kvm/svm/svm.c +0 -0
index d67a54517d95..a405e414cae4 100644
@@ -3483,6 +3483,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
         struct vmcb *vmcb = svm->vmcb;
         bool ret;
Quoted 3 lines


+       if (is_vnmi_enabled(vmcb) && is_vnmi_mask_set(vmcb)) +               return true;

How does this interact with GIF? if the guest does clgi, will the CPU update the V_NMI_MASK on its own If vGIF is enabled?

What happens if vGIF is disabled and vNMI is enabled? KVM then intercepts the stgi/clgi, and it should then update the V_NMI_MASK?

Quoted 10 lines

+
        if (!gif_set(svm))
                return true;

arch/x86/kvm/svm/svm.c +0 -0
@@ -3618,6 +3621,9 @@ static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
  {
         struct vcpu_svm *svm = to_svm(vcpu);


+       if (is_vnmi_enabled(svm->vmcb) && is_vnmi_mask_set(svm->vmcb)) +               return;

This might have hidden assumption that we will only enable NMI window when vNMI is masked.

Quoted 4 lines

+
        if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK) return; /* IRET will cause a vm exit */

Best regards,
Maxim Levitsky

#15 lore raw

Re: [PATCH 4/7] KVM: SVM: Report NMI not allowed when Guest busy handling VNMI

Quoted 29 lines · Maxim Levitsky
Quoted 15 lines · Santosh Shukla

In the VNMI case, Report NMI is not allowed when the processor set the V_NMI_MASK to 1 which means the Guest is busy handling VNMI.

---
 arch/x86/kvm/svm/svm.c | 6 ++++++

 1 file changed, 6 insertions(+)
arch/x86/kvm/svm/svm.c +0 -0
index d67a54517d95..a405e414cae4 100644
@@ -3483,6 +3483,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
         struct vmcb *vmcb = svm->vmcb;
         bool ret;
Quoted 3 lines


+       if (is_vnmi_enabled(vmcb) && is_vnmi_mask_set(vmcb)) +               return true;

How does this interact with GIF? if the guest does clgi, will the CPU update the V_NMI_MASK on its own If vGIF is enabled?

What happens if vGIF is disabled and vNMI is enabled? KVM then intercepts the stgi/clgi, and it should then update the V_NMI_MASK?

Quoted 12 lines
Quoted 10 lines

+
        if (!gif_set(svm))
                return true;

arch/x86/kvm/svm/svm.c +0 -0
@@ -3618,6 +3621,9 @@ static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
  {
         struct vcpu_svm *svm = to_svm(vcpu);


+       if (is_vnmi_enabled(svm->vmcb) && is_vnmi_mask_set(svm->vmcb)) +               return;

This might have hidden assumption that we will only enable NMI window when vNMI is masked.

Also what if vNMI is already pending?

Best regards,
Maxim Levitsky

Quoted 11 lines
Quoted 7 lines

+
        if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK) return; /* IRET will cause a vm exit */

Best regards,
        Maxim Levitsky

#16 lore raw

Re: [PATCH 4/7] KVM: SVM: Report NMI not allowed when Guest busy handling VNMI

Quoted 10 lines · Maxim Levitsky
Quoted 9 lines · Maxim Levitsky
Quoted 8 lines · Santosh Shukla

In the VNMI case, Report NMI is not allowed when the processor set the V_NMI_MASK to 1 which means the Guest is busy handling VNMI.

---
 arch/x86/kvm/svm/svm.c | 6 ++++++

 1 file changed, 6 insertions(+)
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/svm/svm.c +0 -0
index d67a54517d95..a405e414cae4 100644
@@ -3483,6 +3483,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
Quoted 9 lines
Quoted 5 lines
arch/x86/kvm/svm/svm.c +0 -0
         struct vmcb *vmcb = svm->vmcb;
         bool ret;


+       if (is_vnmi_enabled(vmcb) && is_vnmi_mask_set(vmcb)) +               return true;

How does this interact with GIF? if the guest does clgi, will the CPU update the V_NMI_MASK on its own If vGIF is enabled?

Yes.

Quoted 3 lines
Quoted 3 lines

What happens if vGIF is disabled and vNMI is enabled? KVM then intercepts the stgi/clgi, and it should then update the V_NMI_MASK?

No.

For both case - HW takes the V_NMI event at the boundary of VMRUN instruction.

Quoted 16 lines
Quoted 15 lines
Quoted 10 lines

+
        if (!gif_set(svm))
                return true;

arch/x86/kvm/svm/svm.c +0 -0
@@ -3618,6 +3621,9 @@ static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
  {
         struct vcpu_svm *svm = to_svm(vcpu);
Quoted 3 lines


+       if (is_vnmi_enabled(svm->vmcb) && is_vnmi_mask_set(svm->vmcb)) +               return;

This might have hidden assumption that we will only enable NMI window when vNMI is masked.

Quoted 2 lines

Also what if vNMI is already pending?

If V_NMI_MASK set, that means V_NMI is pending, if so then inject another V_NMI in next VMRUN.

Thanks,
Santosh

Quoted 15 lines

Best regards,
Maxim Levitsky

Quoted 11 lines
Quoted 7 lines

+
        if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK) return; /* IRET will cause a vm exit */

Best regards,
        Maxim Levitsky

#17 lore raw

Re: [PATCH 4/7] KVM: SVM: Report NMI not allowed when Guest busy handling VNMI

Quoted 26 lines · Santosh
Quoted 18 lines · Maxim Levitsky
Quoted 17 lines · Maxim Levitsky
Quoted 16 lines · Santosh Shukla

In the VNMI case, Report NMI is not allowed when the processor set the V_NMI_MASK to 1 which means the Guest is busy handling VNMI.

---
 arch/x86/kvm/svm/svm.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kvm/svm/svm.c +0 -0
index d67a54517d95..a405e414cae4 100644
@@ -3483,6 +3483,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
        struct vmcb *vmcb = svm->vmcb;
        bool ret;
 
Quoted 6 lines
Quoted 6 lines

arch/x86/kvm/svm/svm.c bool svm_nmi_blocked(struct kvm_vcpu *vcpu) · line 3487

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+       if (is_vnmi_enabled(vmcb) && is_vnmi_mask_set(vmcb))
+               return true;

How does this interact with GIF? if the guest does clgi, will the CPU update the V_NMI_MASK on its own If vGIF is enabled?

Quoted 8 lines

Yes.

Quoted 3 lines
Quoted 3 lines

What happens if vGIF is disabled and vNMI is enabled? KVM then intercepts the stgi/clgi, and it should then update the V_NMI_MASK?

No.

For both case - HW takes the V_NMI event at the boundary of VMRUN instruction.

How that is possible? if vGIF is disabled in L1, then L1 can't execute STGI/CLGI - that means that the CPU can't update the V_NMI, as it never sees the STGI/CLGI beeing executed.

Best regards,
Maxim Levitsky

Quoted 34 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if (!gif_set(svm))
                return true;
 
@@ -3618,6 +3621,9 @@ static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
Quoted 7 lines
Quoted 4 lines

arch/x86/kvm/svm/svm.c static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) · line 3625

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+       if (is_vnmi_enabled(svm->vmcb) && is_vnmi_mask_set(svm->vmcb))
+               return;

This might have hidden assumption that we will only enable NMI window when vNMI is masked.

Also what if vNMI is already pending?

If V_NMI_MASK set, that means V_NMI is pending, if so then inject another V_NMI in next VMRUN.

Thanks,
Santosh

Quoted 11 lines

Best regards,
Maxim Levitsky

Quoted 9 lines

arch/x86/kvm/svm/svm.c static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) · line 3629

Quoted 6 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
                return; /* IRET will cause a vm exit */
 

Best regards,
Maxim Levitsky

#18 lore raw

Re: [PATCH 4/7] KVM: SVM: Report NMI not allowed when Guest busy handling VNMI

Quoted 32 lines · Maxim Levitsky
Quoted 31 lines · Santosh
Quoted 18 lines · Maxim Levitsky
Quoted 17 lines · Maxim Levitsky
Quoted 16 lines · Santosh Shukla

In the VNMI case, Report NMI is not allowed when the processor set the V_NMI_MASK to 1 which means the Guest is busy handling VNMI.

---
 arch/x86/kvm/svm/svm.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kvm/svm/svm.c +0 -0
index d67a54517d95..a405e414cae4 100644
@@ -3483,6 +3483,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
        struct vmcb *vmcb = svm->vmcb;
        bool ret;
 
Quoted 6 lines
Quoted 6 lines

arch/x86/kvm/svm/svm.c bool svm_nmi_blocked(struct kvm_vcpu *vcpu) · line 3487

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+       if (is_vnmi_enabled(vmcb) && is_vnmi_mask_set(vmcb))
+               return true;

How does this interact with GIF? if the guest does clgi, will the CPU update the V_NMI_MASK on its own If vGIF is enabled?

Yes.

Quoted 3 lines
Quoted 3 lines

What happens if vGIF is disabled and vNMI is enabled? KVM then intercepts the stgi/clgi, and it should then update the V_NMI_MASK?

Quoted 8 lines
Quoted 3 lines

No.

For both case - HW takes the V_NMI event at the boundary of VMRUN instruction.

How that is possible? if vGIF is disabled in L1, then L1 can't execute STGI/CLGI - that means that the CPU can't update the V_NMI, as it never sees the STGI/CLGI beeing executed.

If vGIF is disabled then HW will take the vnmi event at the boundary of vmrun instruction.

Thanks,
Santosh

Quoted 39 lines

Best regards,
Maxim Levitsky

Quoted 34 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if (!gif_set(svm))
                return true;
 
@@ -3618,6 +3621,9 @@ static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
Quoted 7 lines
Quoted 4 lines

arch/x86/kvm/svm/svm.c static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) · line 3625

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+       if (is_vnmi_enabled(svm->vmcb) && is_vnmi_mask_set(svm->vmcb))
+               return;

This might have hidden assumption that we will only enable NMI window when vNMI is masked.

Also what if vNMI is already pending?

If V_NMI_MASK set, that means V_NMI is pending, if so then inject another V_NMI in next VMRUN.

Thanks,
Santosh

Quoted 11 lines

Best regards,
Maxim Levitsky

Quoted 9 lines

arch/x86/kvm/svm/svm.c static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) · line 3629

Quoted 6 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
                return; /* IRET will cause a vm exit */
 

Best regards,
Maxim Levitsky

#19 lore raw

Re: [PATCH 4/7] KVM: SVM: Report NMI not allowed when Guest busy handling VNMI

Quoted 35 lines · Santosh
Quoted 33 lines · Maxim Levitsky
Quoted 32 lines · Santosh
Quoted 18 lines · Maxim Levitsky
Quoted 17 lines · Maxim Levitsky
Quoted 16 lines · Santosh Shukla

In the VNMI case, Report NMI is not allowed when the processor set the V_NMI_MASK to 1 which means the Guest is busy handling VNMI.

---
 arch/x86/kvm/svm/svm.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kvm/svm/svm.c +0 -0
index d67a54517d95..a405e414cae4 100644
@@ -3483,6 +3483,9 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
        struct vmcb *vmcb = svm->vmcb;
        bool ret;
 
Quoted 6 lines
Quoted 6 lines

arch/x86/kvm/svm/svm.c bool svm_nmi_blocked(struct kvm_vcpu *vcpu) · line 3487

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+       if (is_vnmi_enabled(vmcb) && is_vnmi_mask_set(vmcb))
+               return true;

How does this interact with GIF? if the guest does clgi, will the CPU update the V_NMI_MASK on its own If vGIF is enabled?

Yes.

Quoted 3 lines
Quoted 3 lines

What happens if vGIF is disabled and vNMI is enabled? KVM then intercepts the stgi/clgi, and it should then update the V_NMI_MASK?

No.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

For both case - HW takes the V_NMI event at the boundary of VMRUN instruction.

How that is possible? if vGIF is disabled in L1, then L1 can't execute STGI/CLGI - that means that the CPU can't update the V_NMI, as it never sees the STGI/CLGI beeing executed.

If vGIF is disabled then HW will take the vnmi event at the boundary of vmrun instruction.

I think I understand now, if vGIF is enabled, and V_NMI_MASK is set, and the guest does STGI, then nothing new should be injected.

If V_NMI_MASK is not set, then svm_nmi_blocked will respect the HF_GIF_MASK, and on STGI interception, the new NMI will be injected on VM entry by setting the V_NMI_PENDING.

So looks like it should work.

Thanks,
Best regards,
Maxim Levitsky

Quoted 38 lines

Thanks,
Santosh

Quoted 34 lines

Best regards,
Maxim Levitsky

Quoted 31 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if (!gif_set(svm))
                return true;
 
@@ -3618,6 +3621,9 @@ static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
Quoted 7 lines
Quoted 4 lines

arch/x86/kvm/svm/svm.c static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) · line 3625

Quoted 2 lines
arch/x86/kvm/svm/svm.c +2 -0
+       if (is_vnmi_enabled(svm->vmcb) && is_vnmi_mask_set(svm->vmcb))
+               return;

This might have hidden assumption that we will only enable NMI window when vNMI is masked.

Also what if vNMI is already pending?

If V_NMI_MASK set, that means V_NMI is pending, if so then inject another V_NMI in next VMRUN.

Thanks,
Santosh

Quoted 10 lines

Best regards,
Maxim Levitsky

Quoted 8 lines

arch/x86/kvm/svm/svm.c static void svm_enable_nmi_window(struct kvm_vcpu *vcpu) · line 3629

Quoted 5 lines
arch/x86/kvm/svm/svm.c +1 -0
+
        if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
                return; /* IRET will cause a vm exit */
 

Best regards,
Maxim Levitsky

#20 lore raw

PATCH 5/7 KVM: SVM: Add VNMI support in inject_nmi

In reply to Santosh Shukla

Inject the NMI by setting V_NMI in the VMCB interrupt control. processor will clear V_NMI to acknowledge processing has started and will keep the V_NMI_MASK set until the processor is done with processing the NMI event.

---
 arch/x86/kvm/svm/svm.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +6 -1
index a405e414cae4..200f979169e0 100644
@@ -3385,11 +3385,16 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
+	++vcpu->stat.nmi_injections;
+	if (is_vnmi_enabled(svm->vmcb)) {
+		svm->vmcb->control.int_ctl |= V_NMI_PENDING;
+		return;
+	}
+
 	svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
 	vcpu->arch.hflags |= HF_NMI_MASK;
 	if (!sev_es_guest(vcpu->kvm))
 		svm_set_intercept(svm, INTERCEPT_IRET);
-	++vcpu->stat.nmi_injections;
 }
 
 static void svm_inject_irq(struct kvm_vcpu *vcpu)
Signature · 1 line
2.25.1
#21 lore raw

Re: [PATCH 5/7] KVM: SVM: Add VNMI support in inject_nmi

Quoted 16 lines · Santosh Shukla

Inject the NMI by setting V_NMI in the VMCB interrupt control. processor will clear V_NMI to acknowledge processing has started and will keep the V_NMI_MASK set until the processor is done with processing the NMI event.

---
 arch/x86/kvm/svm/svm.c | 7 ++++++-

 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +0 -0
index a405e414cae4..200f979169e0 100644
@@ -3385,11 +3385,16 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  {
         struct vcpu_svm *svm = to_svm(vcpu);
Quoted 6 lines


+       ++vcpu->stat.nmi_injections;
+       if (is_vnmi_enabled(svm->vmcb)) {
+               svm->vmcb->control.int_ctl |= V_NMI_PENDING; +               return;
+       }

Here I would advice to have a warning to check if vNMI is already pending.

Also we need to check what happens if we make vNMI pending and get #SMI, while in #NMI handler, or if #NMI is blocked due to interrupt window.

Best regards,
Maxim Levitsky

Quoted 9 lines

+
        svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; vcpu->arch.hflags |= HF_NMI_MASK;
        if (!sev_es_guest(vcpu->kvm))
                svm_set_intercept(svm, INTERCEPT_IRET);
-       ++vcpu->stat.nmi_injections;
 }

 static void svm_inject_irq(struct kvm_vcpu *vcpu)

#22 lore raw

Re: [PATCH 5/7] KVM: SVM: Add VNMI support in inject_nmi

Quoted 10 lines · Maxim Levitsky
Quoted 9 lines · Santosh Shukla

Inject the NMI by setting V_NMI in the VMCB interrupt control. processor will clear V_NMI to acknowledge processing has started and will keep the V_NMI_MASK set until the processor is done with processing the NMI event.

---
 arch/x86/kvm/svm/svm.c | 7 ++++++-

 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 15 lines
Quoted 7 lines
arch/x86/kvm/svm/svm.c +0 -0
index a405e414cae4..200f979169e0 100644
@@ -3385,11 +3385,16 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  {
         struct vcpu_svm *svm = to_svm(vcpu);
Quoted 6 lines


+       ++vcpu->stat.nmi_injections;
+       if (is_vnmi_enabled(svm->vmcb)) {
+               svm->vmcb->control.int_ctl |= V_NMI_PENDING; +               return;
+       }

Here I would advice to have a warning to check if vNMI is already pending.

Yes, in v2.

Quoted 3 lines

Also we need to check what happens if we make vNMI pending and get #SMI, while in #NMI handler, or if #NMI is blocked due to interrupt window.

V_NMI_MASK should be saved as 1 in the save area and hypervisor will resume the NMI handler after handling the SMI.

Thanks,
Santosh

Quoted 15 lines

Best regards,
Maxim Levitsky

Quoted 9 lines

+
        svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; vcpu->arch.hflags |= HF_NMI_MASK;
        if (!sev_es_guest(vcpu->kvm))
                svm_set_intercept(svm, INTERCEPT_IRET);
-       ++vcpu->stat.nmi_injections;
 }

 static void svm_inject_irq(struct kvm_vcpu *vcpu)

#23 lore raw

Re: [PATCH 5/7] KVM: SVM: Add VNMI support in inject_nmi

Quoted 27 lines · Santosh
Quoted 18 lines · Maxim Levitsky
Quoted 17 lines · Santosh Shukla

Inject the NMI by setting V_NMI in the VMCB interrupt control. processor will clear V_NMI to acknowledge processing has started and will keep the V_NMI_MASK set until the processor is done with processing the NMI event.

---
 arch/x86/kvm/svm/svm.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +0 -0
index a405e414cae4..200f979169e0 100644
@@ -3385,11 +3385,16 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
Quoted 7 lines

arch/x86/kvm/svm/svm.c static void svm_inject_nmi(struct kvm_vcpu *vcpu) · line 3392

Quoted 5 lines
arch/x86/kvm/svm/svm.c +5 -0
+       ++vcpu->stat.nmi_injections;
+       if (is_vnmi_enabled(svm->vmcb)) {
+               svm->vmcb->control.int_ctl |= V_NMI_PENDING;
+               return;
+       }

Here I would advice to have a warning to check if vNMI is already pending.

Quoted 8 lines

Yes, in v2.

Quoted 3 lines

Also we need to check what happens if we make vNMI pending and get #SMI, while in #NMI handler, or if #NMI is blocked due to interrupt window.

V_NMI_MASK should be saved as 1 in the save area and hypervisor will resume the NMI handler after handling the SMI.

Answering my own question, because I did some homework in this area while working on that SMI int shadow bug:

Actually what will happen (now I checked) is that we have a special KVM host state flag (called X86EMUL_SMM_MASK, and HF_SMM_INSIDE_NMI_MASK), and what we do is that if we receive SMI while in NMI handler, we don't mask the NMI again, on RSM we don't unmask NMI.

Also, as I found out the hard way recently, the NMI is not blocked by the interrupt shadow.

I don't think that anything is saved to the SMRAM in this case.

Best regards,
Maxim Levitsky

Quoted 17 lines

Thanks,
Santosh

Quoted 13 lines

Best regards,
Maxim Levitsky

Quoted 9 lines
arch/x86/kvm/svm/svm.c +1 -1
+
        svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
        vcpu->arch.hflags |= HF_NMI_MASK;
        if (!sev_es_guest(vcpu->kvm))
                svm_set_intercept(svm, INTERCEPT_IRET);
-       ++vcpu->stat.nmi_injections;
 }
 
 static void svm_inject_irq(struct kvm_vcpu *vcpu)
#24 lore raw

PATCH 6/7 KVM: nSVM: implement nested VNMI

In reply to Santosh Shukla

Currently nested_vmcb02_prepare_control func checks and programs bits (V_TPR,_INTR, _IRQ) in nested mode, To support nested VNMI, extending the check for VNMI bits if VNMI is enabled.

Tested with the KVM-unit-test that is developed for this purpose.

---
 arch/x86/kvm/svm/nested.c | 8 ++++++++
 arch/x86/kvm/svm/svm.c    | 5 +++++
 arch/x86/kvm/svm/svm.h    | 1 +
 3 files changed, 14 insertions(+)
arch/x86/kvm/svm/nested.c +8 -0
index bed5e1692cef..ce83739bae50 100644
@@ -608,6 +608,11 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
 	}
 }
 
+static inline bool nested_vnmi_enabled(struct vcpu_svm *svm)
+{
+	return svm->vnmi_enabled && (svm->nested.ctl.int_ctl & V_NMI_ENABLE);
+}
+
 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 {
 	u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;
@@ -627,6 +632,9 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	else
 		int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
 
+	if (nested_vnmi_enabled(svm))
+		int_ctl_vmcb12_bits |= (V_NMI_PENDING | V_NMI_ENABLE);
+
 	/* Copied from vmcb01.  msrpm_base can be overwritten later.  */
 	vmcb02->control.nested_ctl = vmcb01->control.nested_ctl;
 	vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;
arch/x86/kvm/svm/svm.c +5 -0
index 200f979169e0..c91af728420b 100644
@@ -4075,6 +4075,8 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
 
 	svm->vgif_enabled = vgif && guest_cpuid_has(vcpu, X86_FEATURE_VGIF);
 
+	svm->vnmi_enabled = vnmi && guest_cpuid_has(vcpu, X86_FEATURE_V_NMI);
+
 	svm_recalc_instruction_intercepts(vcpu, svm);
 
 	/* For sev guests, the memory encryption bit is not reserved in CR3.  */
@@ -4831,6 +4833,9 @@ static __init void svm_set_cpu_caps(void)
 		if (vgif)
 			kvm_cpu_cap_set(X86_FEATURE_VGIF);
 
+		if (vnmi)
+			kvm_cpu_cap_set(X86_FEATURE_V_NMI);
+
 		/* Nested VM can receive #VMEXIT instead of triggering #GP */
 		kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
 	}
arch/x86/kvm/svm/svm.h +1 -0
index 21c5460e947a..f926c77bf857 100644
@@ -240,6 +240,7 @@ struct vcpu_svm {
 	bool pause_filter_enabled         : 1;
 	bool pause_threshold_enabled      : 1;
 	bool vgif_enabled                 : 1;
+	bool vnmi_enabled                 : 1;
 
 	u32 ldr_reg;
 	u32 dfr_reg;
Signature · 1 line
2.25.1
#25 lore raw

Re: [PATCH 6/7] KVM: nSVM: implement nested VNMI

Quoted 30 lines · Santosh Shukla

Currently nested_vmcb02_prepare_control func checks and programs bits (V_TPR,_INTR, _IRQ) in nested mode, To support nested VNMI, extending the check for VNMI bits if VNMI is enabled.

Tested with the KVM-unit-test that is developed for this purpose.

---
 arch/x86/kvm/svm/nested.c | 8 ++++++++
 arch/x86/kvm/svm/svm.c    | 5 +++++
 arch/x86/kvm/svm/svm.h    | 1 +

 3 files changed, 14 insertions(+)
arch/x86/kvm/svm/nested.c +0 -0
index bed5e1692cef..ce83739bae50 100644
@@ -608,6 +608,11 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
         }
  }

arch/x86/kvm/svm/nested.c +5 -0
+static inline bool nested_vnmi_enabled(struct vcpu_svm *svm)
+{
+       return svm->vnmi_enabled && (svm->nested.ctl.int_ctl & V_NMI_ENABLE);
+}
+

 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) {
        u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;

arch/x86/kvm/svm/nested.c +0 -0
@@ -627,6 +632,9 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Quoted 5 lines

        else
                int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
+       if (nested_vnmi_enabled(svm))
+               int_ctl_vmcb12_bits |= (V_NMI_PENDING | V_NMI_ENABLE);

This is for sure not enough - we also need to at least copy V_NMI_PENDING/V_NMI_MASK back to vmc12 on vmexit, and also think about what happens with L1's VNMI while L2 is running.

E.g functions like is_vnmi_mask_set, likely should always reference vmcb01, and I *think* that while L2 is running L1's vNMI should be sort of 'inhibited' like I did with AVIC.

For example the svm_nmi_blocked should probably first check for 'is_guest_mode(vcpu) && nested_exit_on_nmi(svm)' and only then start checking for vNMI.

There also are interactions with vGIF and nested vGIF that should be checked as well.

Finally the patch series needs tests, several tests, including a test when a nested guest runs and the L1 receives NMI, and check that it works both when L1 intercepts NMI and doesn't intercept NMIs, and if vNMI is enabled L1, and both enabled and not enabled in L2.

Best regards,
Maxim Levitsky

Quoted 39 lines

+
        /* Copied from vmcb01.  msrpm_base can be overwritten later.  */ vmcb02->control.nested_ctl = vmcb01->control.nested_ctl; vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;

arch/x86/kvm/svm/svm.c +0 -0
index 200f979169e0..c91af728420b 100644
@@ -4075,6 +4075,8 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)


        svm->vgif_enabled = vgif && guest_cpuid_has(vcpu, X86_FEATURE_VGIF);
+       svm->vnmi_enabled = vnmi && guest_cpuid_has(vcpu, X86_FEATURE_V_NMI); +
        svm_recalc_instruction_intercepts(vcpu, svm);

        /* For sev guests, the memory encryption bit is not reserved in CR3.  */

arch/x86/kvm/svm/svm.c +0 -0
@@ -4831,6 +4833,9 @@ static __init void svm_set_cpu_caps(void)
                 if (vgif)
                         kvm_cpu_cap_set(X86_FEATURE_VGIF);


+               if (vnmi)
+                       kvm_cpu_cap_set(X86_FEATURE_V_NMI);
+
                /* Nested VM can receive #VMEXIT instead of triggering #GP */ kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
        }

arch/x86/kvm/svm/svm.h +1 -0
index 21c5460e947a..f926c77bf857 100644
@@ -240,6 +240,7 @@ struct vcpu_svm {
         bool pause_filter_enabled         : 1;
         bool pause_threshold_enabled      : 1;
         bool vgif_enabled                 : 1;
+       bool vnmi_enabled                 : 1;


        u32 ldr_reg;
        u32 dfr_reg;

#26 lore raw

Re: [PATCH 6/7] KVM: nSVM: implement nested VNMI

Quoted 43 lines · Maxim Levitsky
Quoted 30 lines · Santosh Shukla

Currently nested_vmcb02_prepare_control func checks and programs bits (V_TPR,_INTR, _IRQ) in nested mode, To support nested VNMI, extending the check for VNMI bits if VNMI is enabled.

Tested with the KVM-unit-test that is developed for this purpose.

---
 arch/x86/kvm/svm/nested.c | 8 ++++++++
 arch/x86/kvm/svm/svm.c    | 5 +++++
 arch/x86/kvm/svm/svm.h    | 1 +

 3 files changed, 14 insertions(+)
arch/x86/kvm/svm/nested.c +0 -0
index bed5e1692cef..ce83739bae50 100644
@@ -608,6 +608,11 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
         }
  }

arch/x86/kvm/svm/nested.c +5 -0
+static inline bool nested_vnmi_enabled(struct vcpu_svm *svm)
+{
+       return svm->vnmi_enabled && (svm->nested.ctl.int_ctl & V_NMI_ENABLE);
+}
+

 static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) {
        u32 int_ctl_vmcb01_bits = V_INTR_MASKING_MASK;

arch/x86/kvm/svm/nested.c +0 -0
@@ -627,6 +632,9 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
Quoted 5 lines

        else
                int_ctl_vmcb01_bits |= (V_GIF_MASK | V_GIF_ENABLE_MASK);
+       if (nested_vnmi_enabled(svm))
+               int_ctl_vmcb12_bits |= (V_NMI_PENDING | V_NMI_ENABLE);

This is for sure not enough - we also need to at least copy V_NMI_PENDING/V_NMI_MASK back to vmc12 on vmexit, and also think about what happens with L1's VNMI while L2 is running.

E.g functions like is_vnmi_mask_set, likely should always reference vmcb01, and I *think* that while L2 is running L1's vNMI should be sort of 'inhibited' like I did with AVIC.

Quoted 10 lines

For example the svm_nmi_blocked should probably first check for 'is_guest_mode(vcpu) && nested_exit_on_nmi(svm)' and only then start checking for vNMI.

There also are interactions with vGIF and nested vGIF that should be checked as well.

Finally the patch series needs tests, several tests, including a test when a nested guest runs and the L1 receives NMI, and check that it works both when L1 intercepts NMI and doesn't intercept NMIs, and if vNMI is enabled L1, and both enabled and not enabled in L2.

In V2.

Thank-you Maxim for the review comment.
Santosh.

Quoted 44 lines

Best regards,
Maxim Levitsky

Quoted 39 lines

+
        /* Copied from vmcb01.  msrpm_base can be overwritten later.  */ vmcb02->control.nested_ctl = vmcb01->control.nested_ctl; vmcb02->control.iopm_base_pa = vmcb01->control.iopm_base_pa;

arch/x86/kvm/svm/svm.c +0 -0
index 200f979169e0..c91af728420b 100644
@@ -4075,6 +4075,8 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)


        svm->vgif_enabled = vgif && guest_cpuid_has(vcpu, X86_FEATURE_VGIF);
+       svm->vnmi_enabled = vnmi && guest_cpuid_has(vcpu, X86_FEATURE_V_NMI); +
        svm_recalc_instruction_intercepts(vcpu, svm);

        /* For sev guests, the memory encryption bit is not reserved in CR3.  */

arch/x86/kvm/svm/svm.c +0 -0
@@ -4831,6 +4833,9 @@ static __init void svm_set_cpu_caps(void)
                 if (vgif)
                         kvm_cpu_cap_set(X86_FEATURE_VGIF);


+               if (vnmi)
+                       kvm_cpu_cap_set(X86_FEATURE_V_NMI);
+
                /* Nested VM can receive #VMEXIT instead of triggering #GP */ kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
        }

arch/x86/kvm/svm/svm.h +1 -0
index 21c5460e947a..f926c77bf857 100644
@@ -240,6 +240,7 @@ struct vcpu_svm {
         bool pause_filter_enabled         : 1;
         bool pause_threshold_enabled      : 1;
         bool vgif_enabled                 : 1;
+       bool vnmi_enabled                 : 1;


        u32 ldr_reg;
        u32 dfr_reg;

#27 lore raw

PATCH 7/7 KVM: SVM: Enable VNMI feature

In reply to Santosh Shukla

Enable the NMI virtualization (V_NMI_ENABLE) in the VMCB interrupt control when the vnmi module parameter is set.

---
 arch/x86/kvm/svm/svm.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/svm/svm.c +3 -0
index c91af728420b..69a98419203e 100644
@@ -1205,6 +1205,9 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
 	if (kvm_vcpu_apicv_active(vcpu))
 		avic_init_vmcb(svm, vmcb);
 
+	if (vnmi)
+		svm->vmcb->control.int_ctl |= V_NMI_ENABLE;
+
 	if (vgif) {
 		svm_clr_intercept(svm, INTERCEPT_STGI);
 		svm_clr_intercept(svm, INTERCEPT_CLGI);
Signature · 1 line
2.25.1
#28 lore raw
In reply to Santosh Shukla
Quoted 53 lines · Santosh Shukla

Currently, NMI is delivered to the guest using the Event Injection mechanism [1]. The Event Injection mechanism does not block the delivery of subsequent NMIs. So the Hypervisor needs to track the NMI delivery and its completion(by intercepting IRET) before sending a new NMI.

Virtual NMI (VNMI) allows the hypervisor to inject the NMI into the guest w/o using Event Injection mechanism meaning not required to track the guest NMI and intercepting the IRET. To achieve that,
VNMI feature provides virtualized NMI and NMI_MASK capability bits in VMCB intr_control -
V_NMI(11) - Indicates whether a virtual NMI is pending in the guest. V_NMI_MASK(12) - Indicates whether virtual NMI is masked in the guest. V_NMI_ENABLE(26) - Enables the NMI virtualization feature for the guest.

When Hypervisor wants to inject NMI, it will set V_NMI bit, Processor will clear the V_NMI bit and Set the V_NMI_MASK which means the Guest is handling NMI, After the guest handled the NMI, The processor will clear the V_NMI_MASK on the successful completion of IRET instruction Or if VMEXIT occurs while delivering the virtual NMI.

To enable the VNMI capability, Hypervisor need to program V_NMI_ENABLE bit 1.

The presence of this feature is indicated via the CPUID function 0x8000000A_EDX[25].

Testing -
* Used qemu's inject_nmi for testing.
* tested with and w/o AVIC case.
* tested with kvm-unit-test

Thanks,
Santosh
[1] https://www.amd.com/system/files/TechDocs/40332.pdf - APM Vol2, ch-15.20 - "Event Injection".

Santosh Shukla (7):
x86/cpu: Add CPUID feature bit for VNMI
KVM: SVM: Add VNMI bit definition
KVM: SVM: Add VNMI support in get/set_nmi_mask
KVM: SVM: Report NMI not allowed when Guest busy handling VNMI
KVM: SVM: Add VNMI support in inject_nmi
KVM: nSVM: implement nested VNMI
KVM: SVM: Enable VNMI feature

 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/svm.h         |  7 +++++
 arch/x86/kvm/svm/nested.c          |  8 +++++
 arch/x86/kvm/svm/svm.c             | 47 ++++++++++++++++++++++++++++--
 arch/x86/kvm/svm/svm.h             |  1 +
 5 files changed, 62 insertions(+), 2 deletions(-)
Quoted 2 lines

--
2.25.1

When will we see vNMI support in silicon? Genoa?

Where is this feature officially documented? Is there an AMD64 equivalent of the "Intel Architecture Instruction Set Extensions and Future Features" manual?

#29 lore raw
Quoted 54 lines · Jim Mattson
Quoted 53 lines · Santosh Shukla

Currently, NMI is delivered to the guest using the Event Injection mechanism [1]. The Event Injection mechanism does not block the delivery of subsequent NMIs. So the Hypervisor needs to track the NMI delivery and its completion(by intercepting IRET) before sending a new NMI.

Virtual NMI (VNMI) allows the hypervisor to inject the NMI into the guest w/o using Event Injection mechanism meaning not required to track the guest NMI and intercepting the IRET. To achieve that,
VNMI feature provides virtualized NMI and NMI_MASK capability bits in VMCB intr_control -
V_NMI(11) - Indicates whether a virtual NMI is pending in the guest. V_NMI_MASK(12) - Indicates whether virtual NMI is masked in the guest. V_NMI_ENABLE(26) - Enables the NMI virtualization feature for the guest.

When Hypervisor wants to inject NMI, it will set V_NMI bit, Processor will clear the V_NMI bit and Set the V_NMI_MASK which means the Guest is handling NMI, After the guest handled the NMI, The processor will clear the V_NMI_MASK on the successful completion of IRET instruction Or if VMEXIT occurs while delivering the virtual NMI.

To enable the VNMI capability, Hypervisor need to program V_NMI_ENABLE bit 1.

The presence of this feature is indicated via the CPUID function 0x8000000A_EDX[25].

Testing -
* Used qemu's inject_nmi for testing.
* tested with and w/o AVIC case.
* tested with kvm-unit-test

Thanks,
Santosh
[1] https://www.amd.com/system/files/TechDocs/40332.pdf
ch-15.20 - "Event Injection".

Santosh Shukla (7):
x86/cpu: Add CPUID feature bit for VNMI
KVM: SVM: Add VNMI bit definition
KVM: SVM: Add VNMI support in get/set_nmi_mask
KVM: SVM: Report NMI not allowed when Guest busy handling VNMI
KVM: SVM: Add VNMI support in inject_nmi
KVM: nSVM: implement nested VNMI
KVM: SVM: Enable VNMI feature

 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/svm.h         |  7 +++++
 arch/x86/kvm/svm/nested.c          |  8 +++++
 arch/x86/kvm/svm/svm.c             | 47 ++++++++++++++++++++++++++++--
 arch/x86/kvm/svm/svm.h             |  1 +
 5 files changed, 62 insertions(+), 2 deletions(-)
Quoted 8 lines
Quoted 2 lines

--
2.25.1

When will we see vNMI support in silicon? Genoa?

Where is this feature officially documented? Is there an AMD64 equivalent of the "Intel Architecture Instruction Set Extensions and Future Features" manual?

Hi Jim,

A new revision of the Architecture programmers manual (APM) is slated to be release soon and that is going to have all the details for the above questions.

Thanks,
Santosh

#30 lore raw
Quoted 10 lines · Santosh
Quoted 5 lines · Jim Mattson

When will we see vNMI support in silicon? Genoa?

Where is this feature officially documented? Is there an AMD64 equivalent of the "Intel Architecture Instruction Set Extensions and Future Features" manual?

Quoted 5 lines

Hi Jim,

A new revision of the Architecture programmers manual (APM) is slated to be release soon and that is going to have all the details for the above questions.

It's been about 3 months, and I haven't seen the new APM yet. Is there an anticipated release date? It's hard to do a proper review of new features without documentation.