x86/traps: Enable DEBUG_STACK after cpu_init() for TRAP_DB/BP
[pandora-kernel.git] / arch / x86 / kernel / traps.c
index 7176f84..4281988 100644 (file)
@@ -110,15 +110,11 @@ static inline void preempt_conditional_cli(struct pt_regs *regs)
 
 enum ctx_state ist_enter(struct pt_regs *regs)
 {
-       /*
-        * We are atomic because we're on the IST stack (or we're on x86_32,
-        * in which case we still shouldn't schedule.
-        */
-       preempt_count_add(HARDIRQ_OFFSET);
+       enum ctx_state prev_state;
 
        if (user_mode_vm(regs)) {
                /* Other than that, we're just an exception. */
-               return exception_enter();
+               prev_state = exception_enter();
        } else {
                /*
                 * We might have interrupted pretty much anything.  In
@@ -127,12 +123,27 @@ enum ctx_state ist_enter(struct pt_regs *regs)
                 * but we need to notify RCU.
                 */
                rcu_nmi_enter();
-               return IN_KERNEL;  /* the value is irrelevant. */
+               prev_state = IN_KERNEL;  /* the value is irrelevant. */
        }
+
+       /*
+        * We are atomic because we're on the IST stack (or we're on x86_32,
+        * in which case we still shouldn't schedule).
+        *
+        * This must be after exception_enter(), because exception_enter()
+        * won't do anything if in_interrupt() returns true.
+        */
+       preempt_count_add(HARDIRQ_OFFSET);
+
+       /* This code is a bit fragile.  Test it. */
+       rcu_lockdep_assert(rcu_is_watching(), "ist_enter didn't work");
+
+       return prev_state;
 }
 
 void ist_exit(struct pt_regs *regs, enum ctx_state prev_state)
 {
+       /* Must be before exception_exit. */
        preempt_count_sub(HARDIRQ_OFFSET);
 
        if (user_mode_vm(regs))
@@ -848,18 +859,16 @@ void math_state_restore(void)
                local_irq_disable();
        }
 
+       /* Avoid __kernel_fpu_begin() right after __thread_fpu_begin() */
+       kernel_fpu_disable();
        __thread_fpu_begin(tsk);
-
-       /*
-        * Paranoid restore. send a SIGSEGV if we fail to restore the state.
-        */
        if (unlikely(restore_fpu_checking(tsk))) {
                drop_init_fpu(tsk);
                force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
-               return;
+       } else {
+               tsk->thread.fpu_counter++;
        }
-
-       tsk->thread.fpu_counter++;
+       kernel_fpu_enable();
 }
 EXPORT_SYMBOL_GPL(math_state_restore);
 
@@ -916,9 +925,17 @@ dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code)
 /* Set of traps needed for early debugging. */
 void __init early_trap_init(void)
 {
-       set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
+       /*
+        * Don't set ist to DEBUG_STACK as it doesn't work until TSS is
+        * ready in cpu_init() <-- trap_init(). Before trap_init(), CPU
+        * runs at ring 0 so it is impossible to hit an invalid stack.
+        * Using the original stack works well enough at this early
+        * stage. DEBUG_STACK will be equipped after cpu_init() in
+        * trap_init().
+        */
+       set_intr_gate_ist(X86_TRAP_DB, &debug, 0);
        /* int3 can be called from all */
-       set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
+       set_system_intr_gate_ist(X86_TRAP_BP, &int3, 0);
 #ifdef CONFIG_X86_32
        set_intr_gate(X86_TRAP_PF, page_fault);
 #endif
@@ -996,6 +1013,15 @@ void __init trap_init(void)
         */
        cpu_init();
 
+       /*
+        * X86_TRAP_DB and X86_TRAP_BP have been set
+        * in early_trap_init(). However, DEBUG_STACK works only after
+        * cpu_init() loads TSS. See comments in early_trap_init().
+        */
+       set_intr_gate_ist(X86_TRAP_DB, &debug, DEBUG_STACK);
+       /* int3 can be called from all */
+       set_system_intr_gate_ist(X86_TRAP_BP, &int3, DEBUG_STACK);
+
        x86_init.irqs.trap_init();
 
 #ifdef CONFIG_X86_64