Skip to contentSkip to conversation

xen: vmx: Use an INT 2 call to process real NMI's instead of self_nmi() in VMEXIT handler

PATCH Superseded Malcolm Crossley 5 participants 5 messages Nov 13, 2012

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: vmx: Use an INT 2 call to process real NMI's instead of self_nmi() in VMEXIT handler

The self_nmi() code cause's an NMI to be triggered by sending an APIC message to the local processor. Unfortunately there is a delay in the delivery of the APIC message and we may already have re-entered the HVM guest by the time the NMI is taken. This results in the VMEXIT code calling the self_nmi() function again. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.

Volume 3 Chapter 27 Section 1 of the Intel SDM states:

An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.

So we believe it is safe to directly invoke the INT call as NMI's should already be blocked.

The INT 2 call will perform an IRET which will unblock later calls to the NMI handler, according to Intel SDM Volume 3 Chapter 6.7.1.

xen/arch/x86/hvm/vmx/vmx.c +1 -1
diff -r 62885b3c34c8 -r e1fbee58b25c xen/arch/x86/hvm/vmx/vmx.c
@@ -2442,7 +2442,7 @@ void vmx_vmexit_handler(struct cpu_user_
                  (X86_EVENTTYPE_NMI << 8) )
                 goto exit_and_crash;
             HVMTRACE_0D(NMI);
-            self_nmi(); /* Real NMI, vector 2: normal processing. */
+            asm("int $2"); /* Real NMI, vector 2: normal processing. */
             break;
         case TRAP_machine_check:
             HVMTRACE_0D(MCE);
#1 lore raw
Quoted 25 lines · Malcolm Crossley

The self_nmi() code cause's an NMI to be triggered by sending an APIC message to the local processor. Unfortunately there is a delay in the delivery of the APIC message and we may already have re-entered the HVM guest by the time the NMI is taken. This results in the VMEXIT code calling the self_nmi() function again. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.

Volume 3 Chapter 27 Section 1 of the Intel SDM states:

An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.

So we believe it is safe to directly invoke the INT call as NMI's should already be blocked.

The INT 2 call will perform an IRET which will unblock later calls to the NMI handler, according to Intel SDM Volume 3 Chapter 6.7.1.

diff -r 62885b3c34c8 -r e1fbee58b25c xen/arch/x86/hvm/vmx/vmx.c

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2445

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2442,7 +2442,7 @@ void vmx_vmexit_handler(struct cpu_user_
                  (X86_EVENTTYPE_NMI << 8) )
                 goto exit_and_crash;
             HVMTRACE_0D(NMI);
-            self_nmi(); /* Real NMI, vector 2: normal processing. */
+            asm("int $2"); /* Real NMI, vector 2: normal processing. */
               asm volatile("...")

I think? Otherwise this could potentially get hoisted up

Do we need any clobbers? Specifically I'm thinking of memory since taking an int2 ought to preserve register state.

Ian.

#2 lore raw
Quoted 34 lines · Ian Campbell
Quoted 25 lines · Malcolm Crossley

The self_nmi() code cause's an NMI to be triggered by sending an APIC message to the local processor. Unfortunately there is a delay in the delivery of the APIC message and we may already have re-entered the HVM guest by the time the NMI is taken. This results in the VMEXIT code calling the self_nmi() function again. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.

Volume 3 Chapter 27 Section 1 of the Intel SDM states:

An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.

So we believe it is safe to directly invoke the INT call as NMI's should already be blocked.

The INT 2 call will perform an IRET which will unblock later calls to the NMI handler, according to Intel SDM Volume 3 Chapter 6.7.1.

diff -r 62885b3c34c8 -r e1fbee58b25c xen/arch/x86/hvm/vmx/vmx.c

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2445

Quoted 5 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2442,7 +2442,7 @@ void vmx_vmexit_handler(struct cpu_user_
                  (X86_EVENTTYPE_NMI << 8) )
                 goto exit_and_crash;
             HVMTRACE_0D(NMI);
-            self_nmi(); /* Real NMI, vector 2: normal processing. */
+            asm("int $2"); /* Real NMI, vector 2: normal processing. */
               asm volatile("...")
Quoted 1 line

I think? Otherwise this could potentially get hoisted up

Good catch. Hoisted would be fine, but it could also be entirely discarded. :)

Quoted 2 lines

Do we need any clobbers? Specifically I'm thinking of memory since taking an int2 ought to preserve register state.

I don't think so - nothing on this path depends on the actual behaviour of the NMI handler.

Tim.

#3 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 12 lines · Ian Campbell

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2445

Quoted 8 lines · Malcolm Crossley
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2442,7 +2442,7 @@ void vmx_vmexit_handler(struct cpu_user_
                  (X86_EVENTTYPE_NMI << 8) )
                 goto exit_and_crash;
             HVMTRACE_0D(NMI);
-            self_nmi(); /* Real NMI, vector 2: normal processing. */
+            asm("int $2"); /* Real NMI, vector 2: normal processing. */
               asm volatile("...")
Quoted 1 line

I think? Otherwise this could potentially get hoisted up

Quoted 8 lines

Good catch. Hoisted would be fine, but it could also be entirely discarded. :)

Quoted 2 lines

Do we need any clobbers? Specifically I'm thinking of memory since taking an int2 ought to preserve register state.

I don't think so - nothing on this path depends on the actual behaviour of the NMI handler.

We should still add it, as the NMI handler does modify global memory. Even if you can't spot any dependency now, it would be a rather hard to debug problem if there was, or if there ever gets something added to that effect.

Jan

Acked-by
#4 lore raw
In reply to Tim Deegan
Quoted 12 lines · Tim Deegan
Quoted 12 lines

xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2445

Quoted 9 lines

diff -r 62885b3c34c8 -r e1fbee58b25c xen/arch/x86/hvm/vmx/vmx.c

xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2442,7 +2442,7 @@ void vmx_vmexit_handler(struct cpu_user_
                  (X86_EVENTTYPE_NMI << 8) )
                 goto exit_and_crash;
             HVMTRACE_0D(NMI);
-            self_nmi(); /* Real NMI, vector 2: normal processing. */
+            asm("int $2"); /* Real NMI, vector 2: normal processing. */
               asm volatile("...")
Quoted 4 lines
Quoted 1 line

I think? Otherwise this could potentially get hoisted up

Good catch. Hoisted would be fine, but it could also be entirely discarded. :)

Parameter-less asm blocks are a special case that will never be considered side-effect free I believe. Still 'asm volatile' would be our stylistic choice in this case anyway.

And with that: