From: Alexander Nyberg <alexn@telia.com>

If we are faulting in kernel it is quite possible this will lead to a
panic.  Save trap number, cr2 (in case of page fault) and error_code in the
current thread (these fields already exist for signal delivery but are not
used here).  

This helps later kdump crash analyzing from user-space (a script has been
submitted to dig this info out in gdb).

Signed-off-by: Alexander Nyberg <alexn@telia.com>
Cc: <fastboot@lists.osdl.org>
Signed-off-by: Andrew Morton <akpm@osdl.org>
---

 arch/i386/kernel/traps.c |   12 ++++++++----
 arch/i386/mm/fault.c     |    3 +++
 2 files changed, 11 insertions(+), 4 deletions(-)

diff -puN arch/i386/kernel/traps.c~kdump-save-trap-information-for-later-analysis arch/i386/kernel/traps.c
--- 25/arch/i386/kernel/traps.c~kdump-save-trap-information-for-later-analysis	2005-06-18 02:55:57.000000000 -0700
+++ 25-akpm/arch/i386/kernel/traps.c	2005-06-18 02:55:57.000000000 -0700
@@ -411,6 +411,10 @@ static inline void die_if_kernel(const c
 static void do_trap(int trapnr, int signr, char *str, int vm86,
 			   struct pt_regs * regs, long error_code, siginfo_t *info)
 {
+	struct task_struct *tsk = current;
+	tsk->thread.error_code = error_code;
+	tsk->thread.trap_no = trapnr;
+
 	if (regs->eflags & VM_MASK) {
 		if (vm86)
 			goto vm86_trap;
@@ -421,9 +425,6 @@ static void do_trap(int trapnr, int sign
 		goto kernel_trap;
 
 	trap_signal: {
-		struct task_struct *tsk = current;
-		tsk->thread.error_code = error_code;
-		tsk->thread.trap_no = trapnr;
 		if (info)
 			force_sig_info(signr, info, tsk);
 		else
@@ -538,6 +539,9 @@ fastcall void do_general_protection(stru
 	}
 	put_cpu();
 
+	current->thread.error_code = error_code;
+	current->thread.trap_no = 13;
+
 	if (regs->eflags & VM_MASK)
 		goto gp_in_vm86;
 
@@ -983,9 +987,9 @@ fastcall void do_simd_coprocessor_error(
 					  error_code);
 			return;
 		}
-		die_if_kernel("cache flush denied", regs, error_code);
 		current->thread.trap_no = 19;
 		current->thread.error_code = error_code;
+		die_if_kernel("cache flush denied", regs, error_code);
 		force_sig(SIGSEGV, current);
 	}
 }
diff -puN arch/i386/mm/fault.c~kdump-save-trap-information-for-later-analysis arch/i386/mm/fault.c
--- 25/arch/i386/mm/fault.c~kdump-save-trap-information-for-later-analysis	2005-06-18 02:55:57.000000000 -0700
+++ 25-akpm/arch/i386/mm/fault.c	2005-06-18 02:55:57.000000000 -0700
@@ -469,6 +469,9 @@ no_context:
 		printk(KERN_ALERT "*pte = %08lx\n", page);
 	}
 #endif
+	tsk->thread.cr2 = address;
+	tsk->thread.trap_no = 14;
+	tsk->thread.error_code = error_code;
 	die("Oops", regs, error_code);
 	bust_spinlocks(0);
 	do_exit(SIGKILL);
_