regs->eip = (unsigned long)current_text_addr();
 }
 
-static void crash_save_self(void)
+/* CPU does not save ss and esp on stack if execution is already
+ * running in kernel mode at the time of NMI occurrence. This code
+ * fixes it.
+ */
+static void crash_setup_regs(struct pt_regs *newregs, struct pt_regs *oldregs)
+{
+       memcpy(newregs, oldregs, sizeof(*newregs));
+       newregs->esp = (unsigned long)&(oldregs->esp);
+       __asm__ __volatile__("xorl %eax, %eax;");
+       __asm__ __volatile__ ("movw %%ss, %%ax;" :"=a"(newregs->xss));
+}
+
+/* We may have saved_regs from where the error came from
+ * or it is NULL if via a direct panic().
+ */
+static void crash_save_self(struct pt_regs *saved_regs)
 {
        struct pt_regs regs;
        int cpu;
        cpu = smp_processor_id();
-       crash_get_current_regs(®s);
+
+       if (saved_regs)
+               crash_setup_regs(®s, saved_regs);
+       else
+               crash_get_current_regs(®s);
        crash_save_this_cpu(®s, cpu);
 }
 
                return 1;
        local_irq_disable();
 
-       /* CPU does not save ss and esp on stack if execution is already
-        * running in kernel mode at the time of NMI occurrence. This code
-        * fixes it.
-        */
        if (!user_mode(regs)) {
-               memcpy(&fixed_regs, regs, sizeof(*regs));
-               fixed_regs.esp = (unsigned long)&(regs->esp);
-               __asm__ __volatile__("xorl %eax, %eax;");
-               __asm__ __volatile__ ("movw %%ss, %%ax;" :"=a"(fixed_regs.xss));
+               crash_setup_regs(&fixed_regs, regs);
                regs = &fixed_regs;
        }
        crash_save_this_cpu(regs, cpu);
 }
 #endif
 
-void machine_crash_shutdown(void)
+void machine_crash_shutdown(struct pt_regs *regs)
 {
        /* This function is only called after the system
         * has paniced or is otherwise in a critical state.
 #if defined(CONFIG_X86_IO_APIC)
        disable_IO_APIC();
 #endif
-       crash_save_self();
+       crash_save_self(regs);
 }
 
 #include <linux/ptrace.h>
 #include <linux/utsname.h>
 #include <linux/kprobes.h>
+#include <linux/kexec.h>
 
 #ifdef CONFIG_EISA
 #include <linux/ioport.h>
        printk("Kernel BUG\n");
 }
 
+/* This is gone through when something in the kernel
+ * has done something bad and is about to be terminated.
+*/
 void die(const char * str, struct pt_regs * regs, long err)
 {
        static struct {
        bust_spinlocks(0);
        die.lock_owner = -1;
        spin_unlock_irq(&die.lock);
+
+       if (kexec_should_crash(current))
+               crash_kexec(regs);
+
        if (in_interrupt())
                panic("Fatal exception in interrupt");
 
        console_silent();
        spin_unlock(&nmi_print_lock);
        bust_spinlocks(0);
+
+       /* If we are in kernel we are probably nested up pretty bad
+        * and might aswell get out now while we still can.
+       */
+       if (!user_mode(regs)) {
+               current->thread.trap_no = 2;
+               crash_kexec(regs);
+       }
+
        do_exit(SIGSEGV);
 }
 
 
        }
 }
 
-void machine_crash_shutdown(void)
+void machine_crash_shutdown(struct pt_regs *regs)
 {
        if (ppc_md.machine_crash_shutdown) {
                ppc_md.machine_crash_shutdown();
 
  * and if what it will achieve. Letting it be now to compile the code
  * in generic kexec environment
  */
-void machine_crash_shutdown(void)
+void machine_crash_shutdown(struct pt_regs *regs)
 {
        /* do nothing right now */
        /* smp_relase_cpus() if we want smp on panic kernel */
 
 
 note_buf_t crash_notes[NR_CPUS];
 
-void machine_crash_shutdown(void)
+void machine_crash_shutdown(struct pt_regs *regs)
 {
 }
 
 
 note_buf_t crash_notes[NR_CPUS];
 
-void machine_crash_shutdown(void)
+void machine_crash_shutdown(struct pt_regs *regs)
 {
        /* This function is only called after the system
         * has paniced or is otherwise in a critical state.
 
 static void sysrq_handle_crashdump(int key, struct pt_regs *pt_regs,
                                struct tty_struct *tty)
 {
-       crash_kexec();
+       crash_kexec(pt_regs);
 }
 static struct sysrq_key_op sysrq_crashdump_op = {
        .handler        = sysrq_handle_crashdump,
 
        unsigned long flags);
 #endif
 extern struct page *kimage_alloc_control_pages(struct kimage *image, unsigned int order);
-extern void crash_kexec(void);
+extern void crash_kexec(struct pt_regs *);
+int kexec_should_crash(struct task_struct *);
 extern struct kimage *kexec_image;
 
 #define KEXEC_ON_CRASH  0x00000001
 extern struct resource crashk_res;
 
 #else /* !CONFIG_KEXEC */
-static inline void crash_kexec(void) { }
+struct pt_regs;
+struct task_struct;
+static inline void crash_kexec(struct pt_regs *regs) { }
+static inline int kexec_should_crash(struct task_struct *p) { return 0; }
 #endif /* CONFIG_KEXEC */
 #endif /* LINUX_KEXEC_H */
 
 extern void machine_power_off(void);
 
 extern void machine_shutdown(void);
-extern void machine_crash_shutdown(void);
+struct pt_regs;
+extern void machine_crash_shutdown(struct pt_regs *);
 
 #endif
 
 
 #include <linux/reboot.h>
 #include <linux/syscalls.h>
 #include <linux/ioport.h>
+#include <linux/hardirq.h>
+
 #include <asm/page.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
        .flags = IORESOURCE_BUSY | IORESOURCE_MEM
 };
 
+int kexec_should_crash(struct task_struct *p)
+{
+       if (in_interrupt() || !p->pid || p->pid == 1 || panic_on_oops)
+               return 1;
+       return 0;
+}
+
 /*
  * When kexec transitions to the new kernel there is a one-to-one
  * mapping between physical and virtual addresses.  On processors
 }
 #endif
 
-void crash_kexec(void)
+void crash_kexec(struct pt_regs *regs)
 {
        struct kimage *image;
        int locked;
        if (!locked) {
                image = xchg(&kexec_crash_image, NULL);
                if (image) {
-                       machine_crash_shutdown();
+                       machine_crash_shutdown(regs);
                        machine_kexec(image);
                }
                xchg(&kexec_lock, 0);
 
         * everything else.
         * Do we want to call this before we try to display a message?
         */
-       crash_kexec();
+       crash_kexec(NULL);
 
 #ifdef CONFIG_SMP
        /*