Skip to contentSkip to conversation

i386: remove NMI deferral

PATCH In review Jan Beulich 2 participants 2 messages Jun 19, 2007 – Jun 20, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i386: remove NMI deferral

.. by instead making sure selector registers are always stored/ restored correctly despite the potential for an NMI (and also MCE, with a subsequent patch) to kick in.

The idea is to always check values read from %ds and %es against __HYPERVISOR_DS, and only store into the current frame (all normal handlers) or the outer-most one (NMI and MCE) if the value read is different. That way, any NMI or MCE occurring during frame setup will store selectors not saved so far on behalf of the interrupted handler, with that interrupted handler either having managed to read the guest selector (in which case it can store it regardless of whether NMI/MCE kicked in between the read and the store) or finding __HYPERVISOR_DS already in the register, in which case it'll know not to store (as the nested handler would have done the store).

For the restore portion this makes use of the fact that there's exactly one such code sequence, and by moving the selector restore part past all other restores (including all stack pointer adjustments) the NMI/MCE handlers can safely detect whether any selector would have been restored already (by range checking EIP) and move EIP back to the beginning of the selector restore sequence without having to play with the stack pointer itself or any other gpr.

xen/arch/x86/x86_32/entry.S +66 -47
Index: 2007-06-18/xen/arch/x86/x86_32/entry.S
===================================================================
@@ -77,14 +77,29 @@
 restore_all_guest:
         ASSERT_INTERRUPTS_DISABLED
         testl $X86_EFLAGS_VM,UREGS_eflags(%esp)
-        jnz  restore_all_vm86
-#ifdef CONFIG_X86_SUPERVISOR_MODE_KERNEL
+#ifndef CONFIG_X86_SUPERVISOR_MODE_KERNEL
+        popl %ebx
+        popl %ecx
+        popl %edx
+        popl %esi
+        popl %edi
+        popl %ebp
+        popl %eax
+        leal 4(%esp),%esp
+        jnz  .Lrestore_iret_guest
+.Lrestore_sregs_guest:
+.Lft1:  mov  UREGS_ds-UREGS_eip(%esp),%ds
+.Lft2:  mov  UREGS_es-UREGS_eip(%esp),%es
+.Lft3:  mov  UREGS_fs-UREGS_eip(%esp),%fs
+.Lft4:  mov  UREGS_gs-UREGS_eip(%esp),%gs
+.Lrestore_iret_guest:
+#else
+        jnz   restore_all_vm86
         testl $2,UREGS_cs(%esp)
         jnz   1f
         call  restore_ring0_guest
         jmp   restore_all_vm86
 1:
-#endif
 .Lft1:  mov  UREGS_ds(%esp),%ds
 .Lft2:  mov  UREGS_es(%esp),%es
 .Lft3:  mov  UREGS_fs(%esp),%fs
@@ -98,6 +113,7 @@ restore_all_vm86:
         popl %ebp
         popl %eax
         addl $4,%esp
+#endif
 .Lft5:  iret
 .section .fixup,"ax"
 .Lfx5:  subl  $28,%esp
@@ -109,9 +125,13 @@ restore_all_vm86:
         movl  %edx,UREGS_edx+4(%esp)
         movl  %ecx,UREGS_ecx+4(%esp)
         movl  %ebx,UREGS_ebx+4(%esp)
+#ifndef CONFIG_X86_SUPERVISOR_MODE_KERNEL
+.equ .Lfx1, .Lfx5
+#else
 .Lfx1:  SET_XEN_SEGMENTS(a)
         movl  %eax,%fs
         movl  %eax,%gs
+#endif
         sti
         popl  %esi
         pushfl                         # EFLAGS
@@ -169,8 +189,8 @@ restore_all_xen:
 ENTRY(hypercall)
         subl $4,%esp
         FIXUP_RING0_GUEST_STACK
-        SAVE_ALL(b)
-        sti
+        SAVE_ALL(1f,1f)
+1:      sti
         GET_CURRENT(%ebx)
         cmpl  $NR_hypercalls,%eax
         jae   bad_hypercall
@@ -420,9 +440,13 @@ ENTRY(divide_error)
         ALIGN
 handle_exception:
         FIXUP_RING0_GUEST_STACK
-        SAVE_ALL_NOSEGREGS(a)
-        SET_XEN_SEGMENTS(a)
-        testb $X86_EFLAGS_IF>>8,UREGS_eflags+1(%esp)
+        SAVE_ALL(1f,2f)
+        .text 1
+1:      mov   %ecx,%ds
+        mov   %ecx,%es
+        jmp   2f
+        .previous
+2:      testb $X86_EFLAGS_IF>>8,UREGS_eflags+1(%esp)
         jz    exception_with_ints_disabled
         sti                             # re-enable interrupts
 1:      xorl  %eax,%eax
@@ -542,9 +566,9 @@ ENTRY(spurious_interrupt_bug)
         jmp   handle_exception
 
 ENTRY(early_page_fault)
-        SAVE_ALL_NOSEGREGS(a)
-        movl  %esp,%edx
-        pushl %edx
+        SAVE_ALL(1f,1f)
+1:      movl  %esp,%eax
+        pushl %eax
         call  do_early_page_fault
         addl  $4,%esp
         jmp   restore_all_xen
@@ -555,49 +579,44 @@ ENTRY(nmi)
         iret
 #else
         # Save state but do not trash the segment registers!
-        # We may otherwise be unable to reload them or copy them to ring 1. 
+        pushl $TRAP_nmi<<16
+        SAVE_ALL(.Lnmi_xen,.Lnmi_common)
+.Lnmi_common:
+        movl  %esp,%eax
         pushl %eax
-        SAVE_ALL_NOSEGREGS(a)
-
-        # We can only process the NMI if:
-        #  A. We are the outermost Xen activation (in which case we have
-        #     the selectors safely saved on our stack)
-        #  B. DS and ES contain sane Xen values.
-        # In all other cases we bail without touching DS-GS, as we have
-        # interrupted an enclosing Xen activation in tricky prologue or
-        # epilogue code.
-        movl  UREGS_eflags(%esp),%eax
-        movb  UREGS_cs(%esp),%al
-        testl $(3|X86_EFLAGS_VM),%eax
-        jnz   continue_nmi
-        movl  %ds,%eax
-        cmpw  $(__HYPERVISOR_DS),%ax
-        jne   defer_nmi
-        movl  %es,%eax
-        cmpw  $(__HYPERVISOR_DS),%ax
-        jne   defer_nmi
-
-continue_nmi:
-        SET_XEN_SEGMENTS(d)
-        movl  %esp,%edx
-        pushl %edx
         call  do_nmi
         addl  $4,%esp
         jmp   ret_from_intr
-
-defer_nmi:
-        movl  $FIXMAP_apic_base,%eax
-        # apic_wait_icr_idle()
-1:      movl  %ss:APIC_ICR(%eax),%ebx
-        testl $APIC_ICR_BUSY,%ebx
-        jnz   1b
-        # __send_IPI_shortcut(APIC_DEST_SELF, TRAP_deferred_nmi)
-        movl  $(APIC_DM_FIXED | APIC_DEST_SELF | APIC_DEST_PHYSICAL | \
-                TRAP_deferred_nmi),%ss:APIC_ICR(%eax)
-        jmp   restore_all_xen
+.Lnmi_xen:
+        GET_GUEST_REGS(%ebx)
+        testl $X86_EFLAGS_VM,%ss:UREGS_eflags(%ebx)
+        mov   %ds,%eax
+        mov   %es,%edx
+        jnz   .Lnmi_vm86
+        cmpw  %ax,%cx
+        mov   %ecx,%ds
+        cmovel UREGS_ds(%ebx),%eax
+        cmpw  %dx,%cx
+        movl  %eax,UREGS_ds(%ebx)
+        cmovel UREGS_es(%ebx),%edx
+        mov   %ecx,%es
+        movl  $.Lrestore_sregs_guest,%ecx
+        movl  %edx,UREGS_es(%ebx)
+        cmpl  %ecx,UREGS_eip(%esp)
+        jbe   .Lnmi_common
+        cmpl  $.Lrestore_iret_guest,UREGS_eip(%esp)
+        ja    .Lnmi_common
+        movl  %ecx,UREGS_eip(%esp)
+        jmp   .Lnmi_common
+.Lnmi_vm86:
+        mov   %ecx,%ds
+        mov   %ecx,%es
+        jmp   .Lnmi_common
 #endif /* !CONFIG_X86_SUPERVISOR_MODE_KERNEL */
 
 ENTRY(setup_vm86_frame)
+        mov %ecx,%ds
+        mov %ecx,%es
         # Copies the entire stack frame forwards by 16 bytes.
         .macro copy_vm86_words count=18
         .if \count
xen/arch/x86/x86_32/traps.c +0 -11
Index: 2007-06-18/xen/arch/x86/x86_32/traps.c
===================================================================
@@ -232,15 +232,6 @@ unsigned long do_iret(void)
     return 0;
 }
 
-#include <asm/asm_defns.h>
-BUILD_SMP_INTERRUPT(deferred_nmi, TRAP_deferred_nmi)
-fastcall void smp_deferred_nmi(struct cpu_user_regs *regs)
-{
-    asmlinkage void do_nmi(struct cpu_user_regs *);
-    ack_APIC_irq();
-    do_nmi(regs);
-}
-
 void __init percpu_traps_init(void)
 {
     struct tss_struct *tss = &doublefault_tss;
@@ -252,8 +243,6 @@ void __init percpu_traps_init(void)
     /* The hypercall entry vector is only accessible from ring 1. */
     _set_gate(idt_table+HYPERCALL_VECTOR, 14, 1, &hypercall);
 
-    set_intr_gate(TRAP_deferred_nmi, &deferred_nmi);
-
     /*
      * Make a separate task for double faults. This will get us debug output if
      * we blow the kernel stack.
xen/include/asm-x86/processor.h +0 -1
Index: 2007-06-18/xen/include/asm-x86/processor.h
===================================================================
@@ -104,7 +104,6 @@
 #define TRAP_alignment_check  17
 #define TRAP_machine_check    18
 #define TRAP_simd_error       19
-#define TRAP_deferred_nmi     31
 
 /* Set for entry via SYSCALL. Informs return code to use SYSRETQ not IRETQ. */
 /* NB. Same as VGCF_in_syscall. No bits in common with any other TRAP_ defn. */
xen/include/asm-x86/x86_32/asm_defns.h +23 -22

77 lines not on this page · raw message

#1 lore raw

xen/include/asm-x86/x86_32/asm_defns.h line 58

Quoted 9 lines · Jan Beulich
xen/include/asm-x86/x86_32/asm_defns.h +7 -0
+        cmpw %cx,%di;                                   \
+        mov %ecx,%ds;                                   \
         mov %fs,UREGS_fs(%esp);                         \
+        cmovel UREGS_ds(%esp),%edi;                     \
+        cmpw %cx,%si;                                   \
+        mov %edi,UREGS_ds(%esp);                        \
+        cmovel UREGS_es(%esp),%esi;                     \
+        mov %ecx,%es;                                   \
         mov %gs,UREGS_gs(%esp);                         \

It looks like this section of SAVE_ALL gets executed only if we interrupted the guest in protected mode (i.e., not vm86 mode, and not an outer Xen context). So I'm not sure how the cmove's will ever be a no-op -- how could we have UREGS_cs&3!=0, yet have HYPERVISOR_DS in %ds or %es? I guess I must be missing something fundamental about how this patch works...

The cmove's themselves raise another question -- can we really rely on all 32-bit CPUs that Xen is likely to run on to actually have CMOV support? It seems possible, but I'm not certain.

-- Keir