Skip to contentSkip to conversation

x86: fix MCE/NMI injection

PATCH In review Jan Beulich 2 participants 3 messages Dec 1, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: fix MCE/NMI injection

This attempts to address all the concerns raised in
http://lists.xensource.com/archives/html/xen-devel/2009-11/msg01195.html,
but I'm nevertheless still not convinced that all aspects of the injection handling really work reliably. In particular, while the patch here on top of the fixes for the problems menioned in the referenced mail also adds code to keep send_guest_trap() from injecting multiple events at a time, I don't think the is the right mechanism - it should be possible to handle NMI/MCE nested within each other.

Another fix on top of the ones for the earlier described problems is that the vCPU affinity restore logic didn't account for software injected NMIs - these never set cpu_affinity_tmp, but due to it most likely being different from cpu_affinity it would have got restored (to a potentially random value) nevertheless.

xen/arch/x86/cpu/mcheck/mctelem.c +0 -9
@@ -122,15 +122,6 @@ static struct mc_telem_ctl {
 /* Lock protecting all processing lists */
 static DEFINE_SPINLOCK(processing_lock);
 
-static void *cmpxchgptr(void *ptr, void *old, void *new)
-{
-	unsigned long *ulp = (unsigned long *)ptr;
-	unsigned long a = (unsigned long)old;
-	unsigned long b = (unsigned long)new;
-
-	return (void *)cmpxchg(ulp, a, b);
-}
-
 static void mctelem_xchg_head(struct mctelem_ent **headp,
 				struct mctelem_ent **old,
 				struct mctelem_ent *new)
xen/arch/x86/nmi.c +3 -2
@@ -475,10 +475,11 @@ static void do_nmi_stats(unsigned char k
          ((v = d->vcpu[0]) == NULL) )
         return;
 
-    if ( v->nmi_pending || (v->trap_priority >= VCPU_TRAP_NMI) )
+    i = v->async_exception_mask & (1 << VCPU_TRAP_NMI);
+    if ( v->nmi_pending || i )
         printk("dom0 vpu0: NMI %s%s\n",
                v->nmi_pending ? "pending " : "",
-               (v->trap_priority >= VCPU_TRAP_NMI)  ? "masked " : "");
+               i ? "masked " : "");
     else
         printk("dom0 vcpu0: NMI neither pending nor masked\n");
 }
xen/arch/x86/traps.c +86 -6

136 lines not on this page · raw message

xen/arch/x86/x86_32/asm-offsets.c +3 -2
@@ -68,8 +68,9 @@ void __dummy__(void)
     OFFSET(VCPU_guest_context_flags, struct vcpu, arch.guest_context.flags);
     OFFSET(VCPU_nmi_pending, struct vcpu, nmi_pending);
     OFFSET(VCPU_mce_pending, struct vcpu, mce_pending);
-    OFFSET(VCPU_old_trap_priority, struct vcpu, old_trap_priority);
-    OFFSET(VCPU_trap_priority, struct vcpu, trap_priority);
+    OFFSET(VCPU_nmi_old_mask, struct vcpu, nmi_state.old_mask);
+    OFFSET(VCPU_mce_old_mask, struct vcpu, mce_state.old_mask);
+    OFFSET(VCPU_async_exception_mask, struct vcpu, async_exception_mask);
     DEFINE(VCPU_TRAP_NMI, VCPU_TRAP_NMI);
     DEFINE(VCPU_TRAP_MCE, VCPU_TRAP_MCE);
     DEFINE(_VGCF_failsafe_disables_events, _VGCF_failsafe_disables_events);
xen/arch/x86/x86_32/entry.S +12 -10
@@ -259,31 +259,33 @@ process_softirqs:
         ALIGN
 /* %ebx: struct vcpu */
 process_mce:
-        cmpw $VCPU_TRAP_MCE,VCPU_trap_priority(%ebx)
-        jae  test_guest_events
+        testb $1 << VCPU_TRAP_MCE,VCPU_async_exception_mask(%ebx)
+        jnz  test_guest_events
         sti
         movb $0,VCPU_mce_pending(%ebx)
         call set_guest_machinecheck_trapbounce
         test %eax,%eax
         jz   test_all_events
-        movw VCPU_trap_priority(%ebx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%ebx)       # iret hypercall
-        movw $VCPU_TRAP_MCE,VCPU_trap_priority(%ebx)
+        movzbl VCPU_async_exception_mask(%ebx),%edx # save mask for the
+        movb %dl,VCPU_mce_old_mask(%ebx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_MCE,%edx
+        movb %dl,VCPU_async_exception_mask(%ebx)
         jmp process_trap
 
         ALIGN
 /* %ebx: struct vcpu */
 process_nmi:
-        cmpw $VCPU_TRAP_NMI,VCPU_trap_priority(%ebx)
-        jae  test_guest_events
+        cmpw $1 << VCPU_TRAP_NMI,VCPU_async_exception_mask(%ebx)
+        jnz  test_guest_events
         sti
         movb $0,VCPU_nmi_pending(%ebx)
         call set_guest_nmi_trapbounce
         test %eax,%eax
         jz   test_all_events
-        movw VCPU_trap_priority(%ebx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%ebx)       # iret hypercall
-        movw $VCPU_TRAP_NMI,VCPU_trap_priority(%ebx)
+        movzbl VCPU_async_exception_mask(%ebx),%edx # save mask for the
+        movb %dl,VCPU_nmi_old_mask(%ebx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_NMI,%edx
+        movb %dl,VCPU_async_exception_mask(%ebx)
         /* FALLTHROUGH */
 process_trap:
         leal VCPU_trap_bounce(%ebx),%edx
xen/arch/x86/x86_32/traps.c +3 -8
@@ -13,6 +13,7 @@
 #include <xen/nmi.h>
 #include <asm/current.h>
 #include <asm/flushtlb.h>
+#include <asm/traps.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/support.h>
 
@@ -274,17 +275,11 @@ unsigned long do_iret(void)
             goto exit_and_crash;
     }
 
-    /* Restore affinity.  */
-    if ((v->trap_priority >= VCPU_TRAP_NMI)
-       && !cpus_equal(v->cpu_affinity_tmp, v->cpu_affinity))
-        vcpu_set_affinity(v, &v->cpu_affinity_tmp);
-
-    /* Restore previous trap priority */
-    v->trap_priority = v->old_trap_priority;
-
     /* Restore upcall mask from supplied EFLAGS.IF. */
     vcpu_info(v, evtchn_upcall_mask) = !(eflags & X86_EFLAGS_IF);
 
+    async_exception_cleanup(v);
+
     /*
      * The hypercall exit path will overwrite EAX with this return
      * value.
xen/arch/x86/x86_64/asm-offsets.c +3 -2
@@ -93,8 +93,9 @@ void __dummy__(void)
     OFFSET(VCPU_guest_context_flags, struct vcpu, arch.guest_context.flags);
     OFFSET(VCPU_nmi_pending, struct vcpu, nmi_pending);
     OFFSET(VCPU_mce_pending, struct vcpu, mce_pending);
-    OFFSET(VCPU_old_trap_priority, struct vcpu, old_trap_priority);
-    OFFSET(VCPU_trap_priority, struct vcpu, trap_priority);
+    OFFSET(VCPU_nmi_old_mask, struct vcpu, nmi_state.old_mask);
+    OFFSET(VCPU_mce_old_mask, struct vcpu, mce_state.old_mask);
+    OFFSET(VCPU_async_exception_mask, struct vcpu, async_exception_mask);
     DEFINE(VCPU_TRAP_NMI, VCPU_TRAP_NMI);
     DEFINE(VCPU_TRAP_MCE, VCPU_TRAP_MCE);
     DEFINE(_VGCF_failsafe_disables_events, _VGCF_failsafe_disables_events);
xen/arch/x86/x86_64/compat/entry.S +12 -10
@@ -132,31 +132,33 @@ compat_process_softirqs:
 	ALIGN
 /* %rbx: struct vcpu */
 compat_process_mce:
-        cmpw $VCPU_TRAP_MCE,VCPU_trap_priority(%rbx)
-        jae  compat_test_guest_events
+        testb $1 << VCPU_TRAP_MCE,VCPU_async_exception_mask(%rbx)
+        jnz  compat_test_guest_events
         sti
         movb $0,VCPU_mce_pending(%rbx)
         call set_guest_machinecheck_trapbounce
         testl %eax,%eax
         jz    compat_test_all_events
-        movw VCPU_trap_priority(%rbx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%rbx)       # iret hypercall
-        movw  $VCPU_TRAP_MCE,VCPU_trap_priority(%rbx)
+        movzbl VCPU_async_exception_mask(%rbx),%edx # save mask for the
+        movb %dl,VCPU_mce_old_mask(%rbx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_MCE,%edx
+        movb %dl,VCPU_async_exception_mask(%rbx)
         jmp   compat_process_trap
 
 	ALIGN
 /* %rbx: struct vcpu */
 compat_process_nmi:
-        cmpw $VCPU_TRAP_NMI,VCPU_trap_priority(%rbx)
-        jae   compat_test_guest_events
+        cmpw $1 << VCPU_TRAP_NMI,VCPU_async_exception_mask(%rbx)
+        jnz  compat_test_guest_events
         sti
         movb  $0,VCPU_nmi_pending(%rbx)
         call  set_guest_nmi_trapbounce
         testl %eax,%eax
         jz    compat_test_all_events
-        movw VCPU_trap_priority(%rbx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%rbx)       # iret hypercall
-        movw  $VCPU_TRAP_NMI,VCPU_trap_priority(%rbx)
+        movzbl VCPU_async_exception_mask(%rbx),%edx # save mask for the
+        movb %dl,VCPU_nmi_old_mask(%rbx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_NMI,%edx
+        movb %dl,VCPU_async_exception_mask(%rbx)
         /* FALLTHROUGH */
 compat_process_trap:
         leaq  VCPU_trap_bounce(%rbx),%rdx
xen/arch/x86/x86_64/compat/traps.c +2 -8
@@ -147,17 +147,11 @@ unsigned int compat_iret(void)
     else
         regs->_esp += 16;
 
-    /* Restore affinity.  */
-    if ((v->trap_priority >= VCPU_TRAP_NMI)
-       && !cpus_equal(v->cpu_affinity_tmp, v->cpu_affinity))
-        vcpu_set_affinity(v, &v->cpu_affinity_tmp);
-
-    /* Restore previous trap priority */
-    v->trap_priority = v->old_trap_priority;
-
     /* Restore upcall mask from supplied EFLAGS.IF. */
     vcpu_info(v, evtchn_upcall_mask) = !(eflags & X86_EFLAGS_IF);
 
+    async_exception_cleanup(v);
+
     /*
      * The hypercall exit path will overwrite EAX with this return
      * value.
xen/arch/x86/x86_64/entry.S +12 -10
@@ -234,31 +234,33 @@ process_softirqs:
         ALIGN
 /* %rbx: struct vcpu */
 process_mce:
-        cmpw $VCPU_TRAP_MCE,VCPU_trap_priority(%rbx)
-        jae  test_guest_events
+        testb $1 << VCPU_TRAP_MCE,VCPU_async_exception_mask(%rbx)
+        jnz  test_guest_events
         sti
         movb $0,VCPU_mce_pending(%rbx)
         call set_guest_machinecheck_trapbounce
         test %eax,%eax
         jz   test_all_events
-        movw VCPU_trap_priority(%rbx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%rbx)       # iret hypercall
-        movw $VCPU_TRAP_MCE,VCPU_trap_priority(%rbx)
+        movzbl VCPU_async_exception_mask(%rbx),%edx # save mask for the
+        movb %dl,VCPU_mce_old_mask(%rbx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_MCE,%edx
+        movb %dl,VCPU_async_exception_mask(%rbx)
         jmp  process_trap
 
         ALIGN
 /* %rbx: struct vcpu */
 process_nmi:
-        cmpw $VCPU_TRAP_NMI,VCPU_trap_priority(%rbx)
-        jae  test_guest_events
+        cmpw $1 << VCPU_TRAP_NMI,VCPU_async_exception_mask(%rbx)
+        jnz  test_guest_events
         sti
         movb $0,VCPU_nmi_pending(%rbx)
         call set_guest_nmi_trapbounce
         test %eax,%eax
         jz   test_all_events
-        movw VCPU_trap_priority(%rbx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%rbx)       # iret hypercall
-        movw $VCPU_TRAP_NMI,VCPU_trap_priority(%rbx)
+        movzbl VCPU_async_exception_mask(%rbx),%edx # save mask for the
+        movb %dl,VCPU_nmi_old_mask(%rbx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_NMI,%edx
+        movb %dl,VCPU_async_exception_mask(%rbx)
         /* FALLTHROUGH */
 process_trap:
         leaq VCPU_trap_bounce(%rbx),%rdx
xen/arch/x86/x86_64/traps.c +2 -54

71 lines not on this page · raw message

xen/common/domain.c +7 -4
@@ -726,10 +726,11 @@ void vcpu_reset(struct vcpu *v)
     v->fpu_initialised = 0;
     v->fpu_dirtied     = 0;
     v->is_initialised  = 0;
-    v->nmi_pending     = 0;
-    v->mce_pending     = 0;
-    v->old_trap_priority = VCPU_TRAP_NONE;
-    v->trap_priority   = VCPU_TRAP_NONE;
+#ifdef VCPU_TRAP_LAST
+    v->async_exception_mask = 0;
+    memset(v->async_exception_state, 0, sizeof(v->async_exception_state));
+#endif
+    cpus_clear(v->cpu_affinity_tmp);
     clear_bit(_VPF_blocked, &v->pause_flags);
 
     domain_unlock(v->domain);
@@ -855,6 +856,7 @@ long do_vcpu_op(int cmd, int vcpuid, XEN
 
         break;
 
+#ifdef VCPU_TRAP_NMI
     case VCPUOP_send_nmi:
         if ( !guest_handle_is_null(arg) )
             return -EINVAL;
@@ -863,6 +865,7 @@ long do_vcpu_op(int cmd, int vcpuid, XEN
             vcpu_kick(v);
 
         break;
+#endif
 
     default:
         rc = arch_do_vcpu_op(cmd, v, arg);
xen/include/asm-x86/domain.h +10 -0
@@ -18,6 +18,16 @@
 #endif
 #define is_pv_32on64_vcpu(v)   (is_pv_32on64_domain((v)->domain))
 
+#define VCPU_TRAP_NMI          1
+#define VCPU_TRAP_MCE          2
+#define VCPU_TRAP_LAST         VCPU_TRAP_MCE
+
+#define nmi_state              async_exception_state(VCPU_TRAP_NMI)
+#define mce_state              async_exception_state(VCPU_TRAP_MCE)
+
+#define nmi_pending            nmi_state.pending
+#define mce_pending            mce_state.pending
+
 struct trap_bounce {
     uint32_t      error_code;
     uint8_t       flags; /* TBF_ */
xen/include/asm-x86/system.h +7 -0
@@ -134,6 +134,13 @@ static always_inline unsigned long __cmp
 
 #define __HAVE_ARCH_CMPXCHG
 
+#define cmpxchgptr(ptr,o,n) ({                                          \
+    const __typeof__(**(ptr)) *__o = (o);                               \
+    __typeof__(**(ptr)) *__n = (n);                                     \
+    ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)__o,            \
+                                   (unsigned long)__n,sizeof(*(ptr)))); \
+})
+
 /*
  * Both Intel and AMD agree that, from a programmer's viewpoint:
  *  Loads cannot be reordered relative to other loads.
xen/include/asm-x86/traps.h +2 -0
@@ -29,6 +29,8 @@ struct softirq_trap {
 struct cpu_user_regs;
 
 extern void machine_check_vector(struct cpu_user_regs *regs, long error_code);
+
+void async_exception_cleanup(struct vcpu *);
  
 /**
  * guest_has_trap_callback
xen/include/xen/sched.h +9 -13
@@ -113,20 +113,16 @@ struct vcpu 
     bool_t           is_initialised;
     /* Currently running on a CPU? */
     bool_t           is_running;
-    /* MCE callback pending for this VCPU? */
-    bool_t           mce_pending;
-    /* NMI callback pending for this VCPU? */
-    bool_t           nmi_pending;
-
-    /* Higher priorized traps may interrupt lower priorized traps,
-     * lower priorized traps wait until higher priorized traps finished.
-     * Note: This concept is known as "system priority level" (spl)
-     * in the UNIX world. */
-    uint16_t         old_trap_priority;
-    uint16_t         trap_priority;
+
+#ifdef VCPU_TRAP_LAST
 #define VCPU_TRAP_NONE    0
-#define VCPU_TRAP_NMI     1
-#define VCPU_TRAP_MCE     2
+    struct {
+        bool_t           pending;
+        uint8_t          old_mask;
+    }                async_exception_state[VCPU_TRAP_LAST];
+#define async_exception_state(t) async_exception_state[(t)-1]
+    uint8_t          async_exception_mask;
+#endif
 
     /* Require shutdown to be deferred for some asynchronous operation? */
     bool_t           defer_shutdown;

This attempts to address all the concerns raised in
http://lists.xensource.com/archives/html/xen-devel/2009-11/msg01195.html,
but I'm nevertheless still not convinced that all aspects of the injection handling really work reliably. In particular, while the patch here on top of the fixes for the problems menioned in the referenced mail also adds code to keep send_guest_trap() from injecting multiple events at a time, I don't think the is the right mechanism - it should be possible to handle NMI/MCE nested within each other.

Another fix on top of the ones for the earlier described problems is that the vCPU affinity restore logic didn't account for software injected NMIs - these never set cpu_affinity_tmp, but due to it most likely being different from cpu_affinity it would have got restored (to a potentially random value) nevertheless.

xen/arch/x86/cpu/mcheck/mctelem.c +0 -9
@@ -122,15 +122,6 @@ static struct mc_telem_ctl {
 /* Lock protecting all processing lists */
 static DEFINE_SPINLOCK(processing_lock);
 
-static void *cmpxchgptr(void *ptr, void *old, void *new)
-{
-	unsigned long *ulp = (unsigned long *)ptr;
-	unsigned long a = (unsigned long)old;
-	unsigned long b = (unsigned long)new;
-
-	return (void *)cmpxchg(ulp, a, b);
-}
-
 static void mctelem_xchg_head(struct mctelem_ent **headp,
 				struct mctelem_ent **old,
 				struct mctelem_ent *new)
xen/arch/x86/nmi.c +3 -2
@@ -475,10 +475,11 @@ static void do_nmi_stats(unsigned char k
          ((v = d->vcpu[0]) == NULL) )
         return;
 
-    if ( v->nmi_pending || (v->trap_priority >= VCPU_TRAP_NMI) )
+    i = v->async_exception_mask & (1 << VCPU_TRAP_NMI);
+    if ( v->nmi_pending || i )
         printk("dom0 vpu0: NMI %s%s\n",
                v->nmi_pending ? "pending " : "",
-               (v->trap_priority >= VCPU_TRAP_NMI)  ? "masked " : "");
+               i ? "masked " : "");
     else
         printk("dom0 vcpu0: NMI neither pending nor masked\n");
 }
xen/arch/x86/traps.c +86 -6

136 lines not on this page · raw message

xen/arch/x86/x86_32/asm-offsets.c +3 -2
@@ -68,8 +68,9 @@ void __dummy__(void)
     OFFSET(VCPU_guest_context_flags, struct vcpu, arch.guest_context.flags);
     OFFSET(VCPU_nmi_pending, struct vcpu, nmi_pending);
     OFFSET(VCPU_mce_pending, struct vcpu, mce_pending);
-    OFFSET(VCPU_old_trap_priority, struct vcpu, old_trap_priority);
-    OFFSET(VCPU_trap_priority, struct vcpu, trap_priority);
+    OFFSET(VCPU_nmi_old_mask, struct vcpu, nmi_state.old_mask);
+    OFFSET(VCPU_mce_old_mask, struct vcpu, mce_state.old_mask);
+    OFFSET(VCPU_async_exception_mask, struct vcpu, async_exception_mask);
     DEFINE(VCPU_TRAP_NMI, VCPU_TRAP_NMI);
     DEFINE(VCPU_TRAP_MCE, VCPU_TRAP_MCE);
     DEFINE(_VGCF_failsafe_disables_events, _VGCF_failsafe_disables_events);
xen/arch/x86/x86_32/entry.S +12 -10
@@ -259,31 +259,33 @@ process_softirqs:
         ALIGN
 /* %ebx: struct vcpu */
 process_mce:
-        cmpw $VCPU_TRAP_MCE,VCPU_trap_priority(%ebx)
-        jae  test_guest_events
+        testb $1 << VCPU_TRAP_MCE,VCPU_async_exception_mask(%ebx)
+        jnz  test_guest_events
         sti
         movb $0,VCPU_mce_pending(%ebx)
         call set_guest_machinecheck_trapbounce
         test %eax,%eax
         jz   test_all_events
-        movw VCPU_trap_priority(%ebx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%ebx)       # iret hypercall
-        movw $VCPU_TRAP_MCE,VCPU_trap_priority(%ebx)
+        movzbl VCPU_async_exception_mask(%ebx),%edx # save mask for the
+        movb %dl,VCPU_mce_old_mask(%ebx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_MCE,%edx
+        movb %dl,VCPU_async_exception_mask(%ebx)
         jmp process_trap
 
         ALIGN
 /* %ebx: struct vcpu */
 process_nmi:
-        cmpw $VCPU_TRAP_NMI,VCPU_trap_priority(%ebx)
-        jae  test_guest_events
+        cmpw $1 << VCPU_TRAP_NMI,VCPU_async_exception_mask(%ebx)
+        jnz  test_guest_events
         sti
         movb $0,VCPU_nmi_pending(%ebx)
         call set_guest_nmi_trapbounce
         test %eax,%eax
         jz   test_all_events
-        movw VCPU_trap_priority(%ebx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%ebx)       # iret hypercall
-        movw $VCPU_TRAP_NMI,VCPU_trap_priority(%ebx)
+        movzbl VCPU_async_exception_mask(%ebx),%edx # save mask for the
+        movb %dl,VCPU_nmi_old_mask(%ebx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_NMI,%edx
+        movb %dl,VCPU_async_exception_mask(%ebx)
         /* FALLTHROUGH */
 process_trap:
         leal VCPU_trap_bounce(%ebx),%edx
xen/arch/x86/x86_32/traps.c +3 -8
@@ -13,6 +13,7 @@
 #include <xen/nmi.h>
 #include <asm/current.h>
 #include <asm/flushtlb.h>
+#include <asm/traps.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/support.h>
 
@@ -274,17 +275,11 @@ unsigned long do_iret(void)
             goto exit_and_crash;
     }
 
-    /* Restore affinity.  */
-    if ((v->trap_priority >= VCPU_TRAP_NMI)
-       && !cpus_equal(v->cpu_affinity_tmp, v->cpu_affinity))
-        vcpu_set_affinity(v, &v->cpu_affinity_tmp);
-
-    /* Restore previous trap priority */
-    v->trap_priority = v->old_trap_priority;
-
     /* Restore upcall mask from supplied EFLAGS.IF. */
     vcpu_info(v, evtchn_upcall_mask) = !(eflags & X86_EFLAGS_IF);
 
+    async_exception_cleanup(v);
+
     /*
      * The hypercall exit path will overwrite EAX with this return
      * value.
xen/arch/x86/x86_64/asm-offsets.c +3 -2
@@ -93,8 +93,9 @@ void __dummy__(void)
     OFFSET(VCPU_guest_context_flags, struct vcpu, arch.guest_context.flags);
     OFFSET(VCPU_nmi_pending, struct vcpu, nmi_pending);
     OFFSET(VCPU_mce_pending, struct vcpu, mce_pending);
-    OFFSET(VCPU_old_trap_priority, struct vcpu, old_trap_priority);
-    OFFSET(VCPU_trap_priority, struct vcpu, trap_priority);
+    OFFSET(VCPU_nmi_old_mask, struct vcpu, nmi_state.old_mask);
+    OFFSET(VCPU_mce_old_mask, struct vcpu, mce_state.old_mask);
+    OFFSET(VCPU_async_exception_mask, struct vcpu, async_exception_mask);
     DEFINE(VCPU_TRAP_NMI, VCPU_TRAP_NMI);
     DEFINE(VCPU_TRAP_MCE, VCPU_TRAP_MCE);
     DEFINE(_VGCF_failsafe_disables_events, _VGCF_failsafe_disables_events);
xen/arch/x86/x86_64/compat/entry.S +12 -10
@@ -132,31 +132,33 @@ compat_process_softirqs:
 	ALIGN
 /* %rbx: struct vcpu */
 compat_process_mce:
-        cmpw $VCPU_TRAP_MCE,VCPU_trap_priority(%rbx)
-        jae  compat_test_guest_events
+        testb $1 << VCPU_TRAP_MCE,VCPU_async_exception_mask(%rbx)
+        jnz  compat_test_guest_events
         sti
         movb $0,VCPU_mce_pending(%rbx)
         call set_guest_machinecheck_trapbounce
         testl %eax,%eax
         jz    compat_test_all_events
-        movw VCPU_trap_priority(%rbx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%rbx)       # iret hypercall
-        movw  $VCPU_TRAP_MCE,VCPU_trap_priority(%rbx)
+        movzbl VCPU_async_exception_mask(%rbx),%edx # save mask for the
+        movb %dl,VCPU_mce_old_mask(%rbx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_MCE,%edx
+        movb %dl,VCPU_async_exception_mask(%rbx)
         jmp   compat_process_trap
 
 	ALIGN
 /* %rbx: struct vcpu */
 compat_process_nmi:
-        cmpw $VCPU_TRAP_NMI,VCPU_trap_priority(%rbx)
-        jae   compat_test_guest_events
+        cmpw $1 << VCPU_TRAP_NMI,VCPU_async_exception_mask(%rbx)
+        jnz  compat_test_guest_events
         sti
         movb  $0,VCPU_nmi_pending(%rbx)
         call  set_guest_nmi_trapbounce
         testl %eax,%eax
         jz    compat_test_all_events
-        movw VCPU_trap_priority(%rbx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%rbx)       # iret hypercall
-        movw  $VCPU_TRAP_NMI,VCPU_trap_priority(%rbx)
+        movzbl VCPU_async_exception_mask(%rbx),%edx # save mask for the
+        movb %dl,VCPU_nmi_old_mask(%rbx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_NMI,%edx
+        movb %dl,VCPU_async_exception_mask(%rbx)
         /* FALLTHROUGH */
 compat_process_trap:
         leaq  VCPU_trap_bounce(%rbx),%rdx
xen/arch/x86/x86_64/compat/traps.c +2 -8
@@ -147,17 +147,11 @@ unsigned int compat_iret(void)
     else
         regs->_esp += 16;
 
-    /* Restore affinity.  */
-    if ((v->trap_priority >= VCPU_TRAP_NMI)
-       && !cpus_equal(v->cpu_affinity_tmp, v->cpu_affinity))
-        vcpu_set_affinity(v, &v->cpu_affinity_tmp);
-
-    /* Restore previous trap priority */
-    v->trap_priority = v->old_trap_priority;
-
     /* Restore upcall mask from supplied EFLAGS.IF. */
     vcpu_info(v, evtchn_upcall_mask) = !(eflags & X86_EFLAGS_IF);
 
+    async_exception_cleanup(v);
+
     /*
      * The hypercall exit path will overwrite EAX with this return
      * value.
xen/arch/x86/x86_64/entry.S +12 -10
@@ -234,31 +234,33 @@ process_softirqs:
         ALIGN
 /* %rbx: struct vcpu */
 process_mce:
-        cmpw $VCPU_TRAP_MCE,VCPU_trap_priority(%rbx)
-        jae  test_guest_events
+        testb $1 << VCPU_TRAP_MCE,VCPU_async_exception_mask(%rbx)
+        jnz  test_guest_events
         sti
         movb $0,VCPU_mce_pending(%rbx)
         call set_guest_machinecheck_trapbounce
         test %eax,%eax
         jz   test_all_events
-        movw VCPU_trap_priority(%rbx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%rbx)       # iret hypercall
-        movw $VCPU_TRAP_MCE,VCPU_trap_priority(%rbx)
+        movzbl VCPU_async_exception_mask(%rbx),%edx # save mask for the
+        movb %dl,VCPU_mce_old_mask(%rbx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_MCE,%edx
+        movb %dl,VCPU_async_exception_mask(%rbx)
         jmp  process_trap
 
         ALIGN
 /* %rbx: struct vcpu */
 process_nmi:
-        cmpw $VCPU_TRAP_NMI,VCPU_trap_priority(%rbx)
-        jae  test_guest_events
+        cmpw $1 << VCPU_TRAP_NMI,VCPU_async_exception_mask(%rbx)
+        jnz  test_guest_events
         sti
         movb $0,VCPU_nmi_pending(%rbx)
         call set_guest_nmi_trapbounce
         test %eax,%eax
         jz   test_all_events
-        movw VCPU_trap_priority(%rbx),%dx           # safe priority for the
-        movw %dx,VCPU_old_trap_priority(%rbx)       # iret hypercall
-        movw $VCPU_TRAP_NMI,VCPU_trap_priority(%rbx)
+        movzbl VCPU_async_exception_mask(%rbx),%edx # save mask for the
+        movb %dl,VCPU_nmi_old_mask(%rbx)            # iret hypercall
+        orl  $1 << VCPU_TRAP_NMI,%edx
+        movb %dl,VCPU_async_exception_mask(%rbx)
         /* FALLTHROUGH */
 process_trap:
         leaq VCPU_trap_bounce(%rbx),%rdx
xen/arch/x86/x86_64/traps.c +2 -54

71 lines not on this page · raw message

xen/common/domain.c +7 -4
@@ -726,10 +726,11 @@ void vcpu_reset(struct vcpu *v)
     v->fpu_initialised = 0;
     v->fpu_dirtied     = 0;
     v->is_initialised  = 0;
-    v->nmi_pending     = 0;
-    v->mce_pending     = 0;
-    v->old_trap_priority = VCPU_TRAP_NONE;
-    v->trap_priority   = VCPU_TRAP_NONE;
+#ifdef VCPU_TRAP_LAST
+    v->async_exception_mask = 0;
+    memset(v->async_exception_state, 0, sizeof(v->async_exception_state));
+#endif
+    cpus_clear(v->cpu_affinity_tmp);
     clear_bit(_VPF_blocked, &v->pause_flags);
 
     domain_unlock(v->domain);
@@ -855,6 +856,7 @@ long do_vcpu_op(int cmd, int vcpuid, XEN
 
         break;
 
+#ifdef VCPU_TRAP_NMI
     case VCPUOP_send_nmi:
         if ( !guest_handle_is_null(arg) )
             return -EINVAL;
@@ -863,6 +865,7 @@ long do_vcpu_op(int cmd, int vcpuid, XEN
             vcpu_kick(v);
 
         break;
+#endif
 
     default:
         rc = arch_do_vcpu_op(cmd, v, arg);
xen/include/asm-x86/domain.h +10 -0
@@ -18,6 +18,16 @@
 #endif
 #define is_pv_32on64_vcpu(v)   (is_pv_32on64_domain((v)->domain))
 
+#define VCPU_TRAP_NMI          1
+#define VCPU_TRAP_MCE          2
+#define VCPU_TRAP_LAST         VCPU_TRAP_MCE
+
+#define nmi_state              async_exception_state(VCPU_TRAP_NMI)
+#define mce_state              async_exception_state(VCPU_TRAP_MCE)
+
+#define nmi_pending            nmi_state.pending
+#define mce_pending            mce_state.pending
+
 struct trap_bounce {
     uint32_t      error_code;
     uint8_t       flags; /* TBF_ */
xen/include/asm-x86/system.h +7 -0
@@ -134,6 +134,13 @@ static always_inline unsigned long __cmp
 
 #define __HAVE_ARCH_CMPXCHG
 
+#define cmpxchgptr(ptr,o,n) ({                                          \
+    const __typeof__(**(ptr)) *__o = (o);                               \
+    __typeof__(**(ptr)) *__n = (n);                                     \
+    ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)__o,            \
+                                   (unsigned long)__n,sizeof(*(ptr)))); \
+})
+
 /*
  * Both Intel and AMD agree that, from a programmer's viewpoint:
  *  Loads cannot be reordered relative to other loads.
xen/include/asm-x86/traps.h +2 -0
@@ -29,6 +29,8 @@ struct softirq_trap {
 struct cpu_user_regs;
 
 extern void machine_check_vector(struct cpu_user_regs *regs, long error_code);
+
+void async_exception_cleanup(struct vcpu *);
  
 /**
  * guest_has_trap_callback
xen/include/xen/sched.h +9 -13
@@ -113,20 +113,16 @@ struct vcpu 
     bool_t           is_initialised;
     /* Currently running on a CPU? */
     bool_t           is_running;
-    /* MCE callback pending for this VCPU? */
-    bool_t           mce_pending;
-    /* NMI callback pending for this VCPU? */
-    bool_t           nmi_pending;
-
-    /* Higher priorized traps may interrupt lower priorized traps,
-     * lower priorized traps wait until higher priorized traps finished.
-     * Note: This concept is known as "system priority level" (spl)
-     * in the UNIX world. */
-    uint16_t         old_trap_priority;
-    uint16_t         trap_priority;
+
+#ifdef VCPU_TRAP_LAST
 #define VCPU_TRAP_NONE    0
-#define VCPU_TRAP_NMI     1
-#define VCPU_TRAP_MCE     2
+    struct {
+        bool_t           pending;
+        uint8_t          old_mask;
+    }                async_exception_state[VCPU_TRAP_LAST];
+#define async_exception_state(t) async_exception_state[(t)-1]
+    uint8_t          async_exception_mask;
+#endif
 
     /* Require shutdown to be deferred for some asynchronous operation? */
     bool_t           defer_shutdown;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 10 lines · On Tuesday 01 December 2009 10:36:58 Jan Beulich wrote:

This attempts to address all the concerns raised in
http://lists.xensource.com/archives/html/xen-devel/2009-11/msg01195.html,
but I'm nevertheless still not convinced that all aspects of the injection handling really work reliably. In particular, while the patch here on top of the fixes for the problems menioned in the referenced mail also adds code to keep send_guest_trap() from injecting multiple events at a time, I don't think the is the right mechanism - it should be possible to handle NMI/MCE nested within each other.

Quoted 7 lines

Another fix on top of the ones for the earlier described problems is that the vCPU affinity restore logic didn't account for software injected NMIs - these never set cpu_affinity_tmp, but due to it most likely being different from cpu_affinity it would have got restored (to a potentially random value) nevertheless.

The patch looks good. Just a minor nit: your cmpxchgptr() implementation looks very complicated and unreadable.

Christoph

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Karl-Hammerschmidt-Str. 34, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Andrew Bowd, Thomas M. McCoy, Giuliano Meroni
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
#2 lore raw
Quoted 3 lines
Quoted 1 line
Quoted 1 line

Christoph Egger <[email protected]> 01.12.09 11:39 >>>

Just a minor nit: your cmpxchgptr() implementation looks very complicated and unreadable.

Do you have suggestions for how to improve it without losing the type correctness checks? (And I really just cloned x86-64's cmpxchg(), so it shouldn't be that much worse than already existing code.)

Jan