Skip to contentSkip to conversation

KVM: VMX: simplify NMI mask management

PATCH In review Avi Kivity 2 participants 6 messages Mar 23, 2011

Subsystems (3)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 1/1 KVM: VMX: simplify NMI mask management

Use vmx_set_nmi_mask() instead of open-coding management of the hardware bit and the software hint (nmi_known_unmasked).

There's a slight change of behaviour when running without hardware virtual NMI support - we now clear the NMI mask if NMI delivery faulted in that case as well. This improves emulation accuracy.

---
 arch/x86/kvm/vmx.c |   10 ++--------
 1 files changed, 2 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx.c +2 -8
index 1bdb49d..2b99ae7 100644
@@ -3532,11 +3532,7 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)
 		switch (type) {
 		case INTR_TYPE_NMI_INTR:
 			vcpu->arch.nmi_injected = false;
-			if (cpu_has_virtual_nmis()) {
-				vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
-					      GUEST_INTR_STATE_NMI);
-				vmx->nmi_known_unmasked = false;
-			}
+			vmx_set_nmi_mask(vcpu, true);
 			break;
 		case INTR_TYPE_EXT_INTR:
 		case INTR_TYPE_SOFT_INTR:
@@ -3991,9 +3987,7 @@ static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
 		 * Clear bit "block by NMI" before VM entry if a NMI
 		 * delivery faulted.
 		 */
-		vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
-				GUEST_INTR_STATE_NMI);
-		vmx->nmi_known_unmasked = true;
+		vmx_set_nmi_mask(&vmx->vcpu, false);
 		break;
 	case INTR_TYPE_SOFT_EXCEPTION:
 		vmx->vcpu.arch.event_exit_inst_len =
Signature · 1 line
1.7.1
#1 lore raw
Quoted 31 lines · Avi Kivity

Use vmx_set_nmi_mask() instead of open-coding management of the hardware bit and the software hint (nmi_known_unmasked).

There's a slight change of behaviour when running without hardware virtual NMI support - we now clear the NMI mask if NMI delivery faulted in that case as well. This improves emulation accuracy.

---
 arch/x86/kvm/vmx.c |   10 ++--------
 1 files changed, 2 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx.c +1 -5
index 1bdb49d..2b99ae7 100644
@@ -3532,11 +3532,7 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)
 		switch (type) {
 		case INTR_TYPE_NMI_INTR:
 			vcpu->arch.nmi_injected = false;
-			if (cpu_has_virtual_nmis()) {
-				vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
-					      GUEST_INTR_STATE_NMI);
-				vmx->nmi_known_unmasked = false;
-			}
+			vmx_set_nmi_mask(vcpu, true);
 			break;
 		case INTR_TYPE_EXT_INTR:
 		case INTR_TYPE_SOFT_INTR:

arch/x86/kvm/vmx.c static void __vmx_complete_interrupts(struct vcpu_vmx *vmx, · line 3993

Quoted 10 lines
arch/x86/kvm/vmx.c +1 -3
@@ -3991,9 +3987,7 @@ static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
 		 * Clear bit "block by NMI" before VM entry if a NMI
 		 * delivery faulted.
 		 */
-		vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
-				GUEST_INTR_STATE_NMI);
-		vmx->nmi_known_unmasked = true;
+		vmx_set_nmi_mask(&vmx->vcpu, false);
 		break;
 	case INTR_TYPE_SOFT_EXCEPTION:
 		vmx->vcpu.arch.event_exit_inst_len =

Looks consistent to me, but maybe you should update the comment above that's now also applied to the emulated mask.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#2 lore raw
Quoted 12 lines · Jan Kiszka
Quoted 11 lines

@@ -3991,9 +3987,7 @@ static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
* Clear bit "block by NMI" before VM entry if a NMI
* delivery faulted.
*/
- vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
- GUEST_INTR_STATE_NMI);
- vmx->nmi_known_unmasked = true;
+ vmx_set_nmi_mask(&vmx->vcpu, false);
break;
case INTR_TYPE_SOFT_EXCEPTION:
vmx->vcpu.arch.event_exit_inst_len =

Quoted 2 lines

Looks consistent to me, but maybe you should update the comment above that's now also applied to the emulated mask.

In what way?

/*
* SDM 3: 27.7.1.2 (September 2008)
* Clear bit "block by NMI" before VM entry if a NMI
* delivery faulted.
*/

Seems pretty descriptive.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw
Quoted 18 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 11 lines

@@ -3991,9 +3987,7 @@ static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
* Clear bit "block by NMI" before VM entry if a NMI
* delivery faulted.
*/
- vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
- GUEST_INTR_STATE_NMI);
- vmx->nmi_known_unmasked = true;
+ vmx_set_nmi_mask(&vmx->vcpu, false);
break;
case INTR_TYPE_SOFT_EXCEPTION:
vmx->vcpu.arch.event_exit_inst_len =

Quoted 2 lines

Looks consistent to me, but maybe you should update the comment above that's now also applied to the emulated mask.

In what way?

Quoted 7 lines

/*
* SDM 3: 27.7.1.2 (September 2008)
* Clear bit "block by NMI" before VM entry if a NMI
* delivery faulted.
*/

Seems pretty descriptive.

IIRC, the above only applies to CPUs that support VNMI and is meaningless for older ones. But vmx_set_nmi_mask is not void for those old CPUs.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
author
#4 lore raw
Quoted 25 lines · Jan Kiszka
Quoted 24 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 11 lines

@@ -3991,9 +3987,7 @@ static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
* Clear bit "block by NMI" before VM entry if a NMI
* delivery faulted.
*/
- vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
- GUEST_INTR_STATE_NMI);
- vmx->nmi_known_unmasked = true;

  +		vmx_set_nmi_mask(&vmx->vcpu, false);
   		break;
   	case INTR_TYPE_SOFT_EXCEPTION:
   		vmx->vcpu.arch.event_exit_inst_len =
Quoted 2 lines

Looks consistent to me, but maybe you should update the comment above that's now also applied to the emulated mask.

In what way?

/*
* SDM 3: 27.7.1.2 (September 2008)
* Clear bit "block by NMI" before VM entry if a NMI
* delivery faulted.
*/

Quoted 5 lines
Quoted 1 line

Seems pretty descriptive.

IIRC, the above only applies to CPUs that support VNMI and is meaningless for older ones. But vmx_set_nmi_mask is not void for those old CPUs.

That's what I pointed out in the second paragraph of the commit log - that it's desired for non-vnmi capable cpus:

static void vmx_inject_nmi(struct kvm_vcpu *vcpu)

{
    struct vcpu_vmx *vmx = to_vmx(vcpu);

    if (!cpu_has_virtual_nmis()) {
        /*
         * Tracking the NMI-blocked state in software is built upon
         * finding the next open IRQ window. This, in turn, depends on
         * well-behaving guests: They have to keep IRQs disabled at
         * least as long as the NMI handler runs. Otherwise we may
         * cause NMI nesting, maybe breaking the guest. But as this is
         * highly unlikely, we can live with the residual risk.
         */
        vmx->soft_vnmi_blocked = 1;
        vmx->vnmi_blocked_time = 0;
    }

We block nmi here, and if we later take a fault, nmi remains both undelivered and blocked.

The SDM refers to the fact the the cpu blocks NMI first and checks for exceptions during delivery later. That happens to match the soft-vnmi implementation, so the comment remains valid.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
Quoted 52 lines · Avi Kivity
Quoted 25 lines · Jan Kiszka
Quoted 24 lines · Avi Kivity
Quoted 12 lines · Jan Kiszka
Quoted 11 lines

@@ -3991,9 +3987,7 @@ static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
* Clear bit "block by NMI" before VM entry if a NMI
* delivery faulted.
*/
- vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
- GUEST_INTR_STATE_NMI);
- vmx->nmi_known_unmasked = true;

  +		vmx_set_nmi_mask(&vmx->vcpu, false);
   		break;
   	case INTR_TYPE_SOFT_EXCEPTION:
   		vmx->vcpu.arch.event_exit_inst_len =
Quoted 2 lines

Looks consistent to me, but maybe you should update the comment above that's now also applied to the emulated mask.

In what way?

/*
* SDM 3: 27.7.1.2 (September 2008)
* Clear bit "block by NMI" before VM entry if a NMI
* delivery faulted.
*/

Quoted 5 lines
Quoted 1 line

Seems pretty descriptive.

IIRC, the above only applies to CPUs that support VNMI and is meaningless for older ones. But vmx_set_nmi_mask is not void for those old CPUs.

That's what I pointed out in the second paragraph of the commit log - that it's desired for non-vnmi capable cpus:

static void vmx_inject_nmi(struct kvm_vcpu *vcpu)

{
     struct vcpu_vmx *vmx = to_vmx(vcpu);

     if (!cpu_has_virtual_nmis()) {
         /*
          * Tracking the NMI-blocked state in software is built upon
          * finding the next open IRQ window. This, in turn, depends on
          * well-behaving guests: They have to keep IRQs disabled at
          * least as long as the NMI handler runs. Otherwise we may
          * cause NMI nesting, maybe breaking the guest. But as this is
          * highly unlikely, we can live with the residual risk.
          */
         vmx->soft_vnmi_blocked = 1;
         vmx->vnmi_blocked_time = 0;
     }
Quoted 7 lines

We block nmi here, and if we later take a fault, nmi remains both undelivered and blocked.

The SDM refers to the fact the the cpu blocks NMI first and checks for exceptions during delivery later. That happens to match the soft-vnmi implementation, so the comment remains valid.

Makes sense now.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux