x86_64: remove empty lines from stack traces/oopses
[safe/jmp/linux-2.6] / arch / x86 / kernel / traps_64.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  *
5  *  Pentium III FXSR, SSE support
6  *      Gareth Hughes <gareth@valinux.com>, May 2000
7  */
8
9 /*
10  * 'Traps.c' handles hardware traps and faults after we have saved some
11  * state in 'entry.S'.
12  */
13 #include <linux/moduleparam.h>
14 #include <linux/interrupt.h>
15 #include <linux/kallsyms.h>
16 #include <linux/spinlock.h>
17 #include <linux/kprobes.h>
18 #include <linux/uaccess.h>
19 #include <linux/utsname.h>
20 #include <linux/kdebug.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/ptrace.h>
24 #include <linux/string.h>
25 #include <linux/unwind.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/kexec.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/init.h>
32 #include <linux/bug.h>
33 #include <linux/nmi.h>
34 #include <linux/mm.h>
35
36 #if defined(CONFIG_EDAC)
37 #include <linux/edac.h>
38 #endif
39
40 #include <asm/stacktrace.h>
41 #include <asm/processor.h>
42 #include <asm/debugreg.h>
43 #include <asm/atomic.h>
44 #include <asm/system.h>
45 #include <asm/unwind.h>
46 #include <asm/desc.h>
47 #include <asm/i387.h>
48 #include <asm/nmi.h>
49 #include <asm/smp.h>
50 #include <asm/io.h>
51 #include <asm/pgalloc.h>
52 #include <asm/proto.h>
53 #include <asm/pda.h>
54 #include <asm/traps.h>
55
56 #include <mach_traps.h>
57
58 int panic_on_unrecovered_nmi;
59 int kstack_depth_to_print = 12;
60 static unsigned int code_bytes = 64;
61 static int ignore_nmis;
62 static int die_counter;
63
64 static inline void conditional_sti(struct pt_regs *regs)
65 {
66         if (regs->flags & X86_EFLAGS_IF)
67                 local_irq_enable();
68 }
69
70 static inline void preempt_conditional_sti(struct pt_regs *regs)
71 {
72         inc_preempt_count();
73         if (regs->flags & X86_EFLAGS_IF)
74                 local_irq_enable();
75 }
76
77 static inline void preempt_conditional_cli(struct pt_regs *regs)
78 {
79         if (regs->flags & X86_EFLAGS_IF)
80                 local_irq_disable();
81         /* Make sure to not schedule here because we could be running
82            on an exception stack. */
83         dec_preempt_count();
84 }
85
86 void printk_address(unsigned long address, int reliable)
87 {
88         printk(" [<%016lx>] %s%pS\n", address, reliable ? "": "? ", (void *) address);
89 }
90
91 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
92                                         unsigned *usedp, char **idp)
93 {
94         static char ids[][8] = {
95                 [DEBUG_STACK - 1] = "#DB",
96                 [NMI_STACK - 1] = "NMI",
97                 [DOUBLEFAULT_STACK - 1] = "#DF",
98                 [STACKFAULT_STACK - 1] = "#SS",
99                 [MCE_STACK - 1] = "#MC",
100 #if DEBUG_STKSZ > EXCEPTION_STKSZ
101                 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
102 #endif
103         };
104         unsigned k;
105
106         /*
107          * Iterate over all exception stacks, and figure out whether
108          * 'stack' is in one of them:
109          */
110         for (k = 0; k < N_EXCEPTION_STACKS; k++) {
111                 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
112                 /*
113                  * Is 'stack' above this exception frame's end?
114                  * If yes then skip to the next frame.
115                  */
116                 if (stack >= end)
117                         continue;
118                 /*
119                  * Is 'stack' above this exception frame's start address?
120                  * If yes then we found the right frame.
121                  */
122                 if (stack >= end - EXCEPTION_STKSZ) {
123                         /*
124                          * Make sure we only iterate through an exception
125                          * stack once. If it comes up for the second time
126                          * then there's something wrong going on - just
127                          * break out and return NULL:
128                          */
129                         if (*usedp & (1U << k))
130                                 break;
131                         *usedp |= 1U << k;
132                         *idp = ids[k];
133                         return (unsigned long *)end;
134                 }
135                 /*
136                  * If this is a debug stack, and if it has a larger size than
137                  * the usual exception stacks, then 'stack' might still
138                  * be within the lower portion of the debug stack:
139                  */
140 #if DEBUG_STKSZ > EXCEPTION_STKSZ
141                 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
142                         unsigned j = N_EXCEPTION_STACKS - 1;
143
144                         /*
145                          * Black magic. A large debug stack is composed of
146                          * multiple exception stack entries, which we
147                          * iterate through now. Dont look:
148                          */
149                         do {
150                                 ++j;
151                                 end -= EXCEPTION_STKSZ;
152                                 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
153                         } while (stack < end - EXCEPTION_STKSZ);
154                         if (*usedp & (1U << j))
155                                 break;
156                         *usedp |= 1U << j;
157                         *idp = ids[j];
158                         return (unsigned long *)end;
159                 }
160 #endif
161         }
162         return NULL;
163 }
164
165 /*
166  * x86-64 can have up to three kernel stacks: 
167  * process stack
168  * interrupt stack
169  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
170  */
171
172 static inline int valid_stack_ptr(struct thread_info *tinfo,
173                         void *p, unsigned int size, void *end)
174 {
175         void *t = tinfo;
176         if (end) {
177                 if (p < end && p >= (end-THREAD_SIZE))
178                         return 1;
179                 else
180                         return 0;
181         }
182         return p > t && p < t + THREAD_SIZE - size;
183 }
184
185 /* The form of the top of the frame on the stack */
186 struct stack_frame {
187         struct stack_frame *next_frame;
188         unsigned long return_address;
189 };
190
191 static inline unsigned long
192 print_context_stack(struct thread_info *tinfo,
193                 unsigned long *stack, unsigned long bp,
194                 const struct stacktrace_ops *ops, void *data,
195                 unsigned long *end)
196 {
197         struct stack_frame *frame = (struct stack_frame *)bp;
198
199         while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
200                 unsigned long addr;
201
202                 addr = *stack;
203                 if (__kernel_text_address(addr)) {
204                         if ((unsigned long) stack == bp + 8) {
205                                 ops->address(data, addr, 1);
206                                 frame = frame->next_frame;
207                                 bp = (unsigned long) frame;
208                         } else {
209                                 ops->address(data, addr, bp == 0);
210                         }
211                 }
212                 stack++;
213         }
214         return bp;
215 }
216
217 void dump_trace(struct task_struct *task, struct pt_regs *regs,
218                 unsigned long *stack, unsigned long bp,
219                 const struct stacktrace_ops *ops, void *data)
220 {
221         const unsigned cpu = get_cpu();
222         unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
223         unsigned used = 0;
224         struct thread_info *tinfo;
225
226         if (!task)
227                 task = current;
228
229         if (!stack) {
230                 unsigned long dummy;
231                 stack = &dummy;
232                 if (task && task != current)
233                         stack = (unsigned long *)task->thread.sp;
234         }
235
236 #ifdef CONFIG_FRAME_POINTER
237         if (!bp) {
238                 if (task == current) {
239                         /* Grab bp right from our regs */
240                         asm("movq %%rbp, %0" : "=r" (bp) :);
241                 } else {
242                         /* bp is the last reg pushed by switch_to */
243                         bp = *(unsigned long *) task->thread.sp;
244                 }
245         }
246 #endif
247
248         /*
249          * Print function call entries in all stacks, starting at the
250          * current stack address. If the stacks consist of nested
251          * exceptions
252          */
253         tinfo = task_thread_info(task);
254         for (;;) {
255                 char *id;
256                 unsigned long *estack_end;
257                 estack_end = in_exception_stack(cpu, (unsigned long)stack,
258                                                 &used, &id);
259
260                 if (estack_end) {
261                         if (ops->stack(data, id) < 0)
262                                 break;
263
264                         bp = print_context_stack(tinfo, stack, bp, ops,
265                                                         data, estack_end);
266                         ops->stack(data, "<EOE>");
267                         /*
268                          * We link to the next stack via the
269                          * second-to-last pointer (index -2 to end) in the
270                          * exception stack:
271                          */
272                         stack = (unsigned long *) estack_end[-2];
273                         continue;
274                 }
275                 if (irqstack_end) {
276                         unsigned long *irqstack;
277                         irqstack = irqstack_end -
278                                 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
279
280                         if (stack >= irqstack && stack < irqstack_end) {
281                                 if (ops->stack(data, "IRQ") < 0)
282                                         break;
283                                 bp = print_context_stack(tinfo, stack, bp,
284                                                 ops, data, irqstack_end);
285                                 /*
286                                  * We link to the next stack (which would be
287                                  * the process stack normally) the last
288                                  * pointer (index -1 to end) in the IRQ stack:
289                                  */
290                                 stack = (unsigned long *) (irqstack_end[-1]);
291                                 irqstack_end = NULL;
292                                 ops->stack(data, "EOI");
293                                 continue;
294                         }
295                 }
296                 break;
297         }
298
299         /*
300          * This handles the process stack:
301          */
302         bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
303         put_cpu();
304 }
305 EXPORT_SYMBOL(dump_trace);
306
307 static void
308 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
309 {
310         print_symbol(msg, symbol);
311         printk("\n");
312 }
313
314 static void print_trace_warning(void *data, char *msg)
315 {
316         printk("%s\n", msg);
317 }
318
319 static int print_trace_stack(void *data, char *name)
320 {
321         printk(" <%s> ", name);
322         return 0;
323 }
324
325 static void print_trace_address(void *data, unsigned long addr, int reliable)
326 {
327         touch_nmi_watchdog();
328         printk_address(addr, reliable);
329 }
330
331 static const struct stacktrace_ops print_trace_ops = {
332         .warning = print_trace_warning,
333         .warning_symbol = print_trace_warning_symbol,
334         .stack = print_trace_stack,
335         .address = print_trace_address,
336 };
337
338 static void
339 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
340                 unsigned long *stack, unsigned long bp, char *log_lvl)
341 {
342         printk("Call Trace:\n");
343         dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
344 }
345
346 void show_trace(struct task_struct *task, struct pt_regs *regs,
347                 unsigned long *stack, unsigned long bp)
348 {
349         show_trace_log_lvl(task, regs, stack, bp, "");
350 }
351
352 static void
353 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
354                 unsigned long *sp, unsigned long bp, char *log_lvl)
355 {
356         unsigned long *stack;
357         int i;
358         const int cpu = smp_processor_id();
359         unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
360         unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
361
362         // debugging aid: "show_stack(NULL, NULL);" prints the
363         // back trace for this cpu.
364
365         if (sp == NULL) {
366                 if (task)
367                         sp = (unsigned long *)task->thread.sp;
368                 else
369                         sp = (unsigned long *)&sp;
370         }
371
372         stack = sp;
373         for (i = 0; i < kstack_depth_to_print; i++) {
374                 if (stack >= irqstack && stack <= irqstack_end) {
375                         if (stack == irqstack_end) {
376                                 stack = (unsigned long *) (irqstack_end[-1]);
377                                 printk(" <EOI> ");
378                         }
379                 } else {
380                 if (((long) stack & (THREAD_SIZE-1)) == 0)
381                         break;
382                 }
383                 if (i && ((i % 4) == 0))
384                         printk("\n");
385                 printk(" %016lx", *stack++);
386                 touch_nmi_watchdog();
387         }
388         printk("\n");
389         show_trace_log_lvl(task, regs, sp, bp, log_lvl);
390 }
391
392 void show_stack(struct task_struct *task, unsigned long *sp)
393 {
394         show_stack_log_lvl(task, NULL, sp, 0, "");
395 }
396
397 /*
398  * The architecture-independent dump_stack generator
399  */
400 void dump_stack(void)
401 {
402         unsigned long bp = 0;
403         unsigned long stack;
404
405 #ifdef CONFIG_FRAME_POINTER
406         if (!bp)
407                 asm("movq %%rbp, %0" : "=r" (bp):);
408 #endif
409
410         printk("Pid: %d, comm: %.20s %s %s %.*s\n",
411                 current->pid, current->comm, print_tainted(),
412                 init_utsname()->release,
413                 (int)strcspn(init_utsname()->version, " "),
414                 init_utsname()->version);
415         show_trace(NULL, NULL, &stack, bp);
416 }
417
418 EXPORT_SYMBOL(dump_stack);
419
420 void show_registers(struct pt_regs *regs)
421 {
422         int i;
423         unsigned long sp;
424         const int cpu = smp_processor_id();
425         struct task_struct *cur = cpu_pda(cpu)->pcurrent;
426
427         sp = regs->sp;
428         printk("CPU %d ", cpu);
429         __show_regs(regs);
430         printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
431                 cur->comm, cur->pid, task_thread_info(cur), cur);
432
433         /*
434          * When in-kernel, we also print out the stack and code at the
435          * time of the fault..
436          */
437         if (!user_mode(regs)) {
438                 unsigned int code_prologue = code_bytes * 43 / 64;
439                 unsigned int code_len = code_bytes;
440                 unsigned char c;
441                 u8 *ip;
442
443                 printk("Stack: ");
444                 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
445                                 regs->bp, "");
446
447                 printk(KERN_EMERG "Code: ");
448
449                 ip = (u8 *)regs->ip - code_prologue;
450                 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
451                         /* try starting at RIP */
452                         ip = (u8 *)regs->ip;
453                         code_len = code_len - code_prologue + 1;
454                 }
455                 for (i = 0; i < code_len; i++, ip++) {
456                         if (ip < (u8 *)PAGE_OFFSET ||
457                                         probe_kernel_address(ip, c)) {
458                                 printk(" Bad RIP value.");
459                                 break;
460                         }
461                         if (ip == (u8 *)regs->ip)
462                                 printk("<%02x> ", c);
463                         else
464                                 printk("%02x ", c);
465                 }
466         }
467         printk("\n");
468 }
469
470 int is_valid_bugaddr(unsigned long ip)
471 {
472         unsigned short ud2;
473
474         if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
475                 return 0;
476
477         return ud2 == 0x0b0f;
478 }
479
480 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
481 static int die_owner = -1;
482 static unsigned int die_nest_count;
483
484 unsigned __kprobes long oops_begin(void)
485 {
486         int cpu;
487         unsigned long flags;
488
489         oops_enter();
490
491         /* racy, but better than risking deadlock. */
492         raw_local_irq_save(flags);
493         cpu = smp_processor_id();
494         if (!__raw_spin_trylock(&die_lock)) {
495                 if (cpu == die_owner) 
496                         /* nested oops. should stop eventually */;
497                 else
498                         __raw_spin_lock(&die_lock);
499         }
500         die_nest_count++;
501         die_owner = cpu;
502         console_verbose();
503         bust_spinlocks(1);
504         return flags;
505 }
506
507 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
508 {
509         die_owner = -1;
510         bust_spinlocks(0);
511         die_nest_count--;
512         if (!die_nest_count)
513                 /* Nest count reaches zero, release the lock. */
514                 __raw_spin_unlock(&die_lock);
515         raw_local_irq_restore(flags);
516         if (!regs) {
517                 oops_exit();
518                 return;
519         }
520         if (panic_on_oops)
521                 panic("Fatal exception");
522         oops_exit();
523         do_exit(signr);
524 }
525
526 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
527 {
528         printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
529 #ifdef CONFIG_PREEMPT
530         printk("PREEMPT ");
531 #endif
532 #ifdef CONFIG_SMP
533         printk("SMP ");
534 #endif
535 #ifdef CONFIG_DEBUG_PAGEALLOC
536         printk("DEBUG_PAGEALLOC");
537 #endif
538         printk("\n");
539         if (notify_die(DIE_OOPS, str, regs, err,
540                         current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
541                 return 1;
542
543         show_registers(regs);
544         add_taint(TAINT_DIE);
545         /* Executive summary in case the oops scrolled away */
546         printk(KERN_ALERT "RIP ");
547         printk_address(regs->ip, 1);
548         printk(" RSP <%016lx>\n", regs->sp);
549         if (kexec_should_crash(current))
550                 crash_kexec(regs);
551         return 0;
552 }
553
554 void die(const char *str, struct pt_regs *regs, long err)
555 {
556         unsigned long flags = oops_begin();
557
558         if (!user_mode(regs))
559                 report_bug(regs->ip, regs);
560
561         if (__die(str, regs, err))
562                 regs = NULL;
563         oops_end(flags, regs, SIGSEGV);
564 }
565
566 notrace __kprobes void
567 die_nmi(char *str, struct pt_regs *regs, int do_panic)
568 {
569         unsigned long flags;
570
571         if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
572                 return;
573
574         flags = oops_begin();
575         /*
576          * We are in trouble anyway, lets at least try
577          * to get a message out.
578          */
579         printk(KERN_EMERG "%s", str);
580         printk(" on CPU%d, ip %08lx, registers:\n",
581                 smp_processor_id(), regs->ip);
582         show_registers(regs);
583         if (kexec_should_crash(current))
584                 crash_kexec(regs);
585         if (do_panic || panic_on_oops)
586                 panic("Non maskable interrupt");
587         oops_end(flags, NULL, SIGBUS);
588         nmi_exit();
589         local_irq_enable();
590         do_exit(SIGBUS);
591 }
592
593 static void __kprobes
594 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
595         long error_code, siginfo_t *info)
596 {
597         struct task_struct *tsk = current;
598
599         if (!user_mode(regs))
600                 goto kernel_trap;
601
602         /*
603          * We want error_code and trap_no set for userspace faults and
604          * kernelspace faults which result in die(), but not
605          * kernelspace faults which are fixed up.  die() gives the
606          * process no chance to handle the signal and notice the
607          * kernel fault information, so that won't result in polluting
608          * the information about previously queued, but not yet
609          * delivered, faults.  See also do_general_protection below.
610          */
611         tsk->thread.error_code = error_code;
612         tsk->thread.trap_no = trapnr;
613
614         if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
615             printk_ratelimit()) {
616                 printk(KERN_INFO
617                        "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
618                        tsk->comm, tsk->pid, str,
619                        regs->ip, regs->sp, error_code);
620                 print_vma_addr(" in ", regs->ip);
621                 printk("\n");
622         }
623
624         if (info)
625                 force_sig_info(signr, info, tsk);
626         else
627                 force_sig(signr, tsk);
628         return;
629
630 kernel_trap:
631         if (!fixup_exception(regs)) {
632                 tsk->thread.error_code = error_code;
633                 tsk->thread.trap_no = trapnr;
634                 die(str, regs, error_code);
635         }
636         return;
637 }
638
639 #define DO_ERROR(trapnr, signr, str, name) \
640 asmlinkage void do_##name(struct pt_regs * regs, long error_code)       \
641 {                                                                       \
642         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
643                                                         == NOTIFY_STOP) \
644                 return;                                                 \
645         conditional_sti(regs);                                          \
646         do_trap(trapnr, signr, str, regs, error_code, NULL);            \
647 }
648
649 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr)         \
650 asmlinkage void do_##name(struct pt_regs * regs, long error_code)       \
651 {                                                                       \
652         siginfo_t info;                                                 \
653         info.si_signo = signr;                                          \
654         info.si_errno = 0;                                              \
655         info.si_code = sicode;                                          \
656         info.si_addr = (void __user *)siaddr;                           \
657         trace_hardirqs_fixup();                                         \
658         if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr)  \
659                                                         == NOTIFY_STOP) \
660                 return;                                                 \
661         conditional_sti(regs);                                          \
662         do_trap(trapnr, signr, str, regs, error_code, &info);           \
663 }
664
665 DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
666 DO_ERROR(4, SIGSEGV, "overflow", overflow)
667 DO_ERROR(5, SIGSEGV, "bounds", bounds)
668 DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
669 DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
670 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
671 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
672 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
673
674 /* Runs on IST stack */
675 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
676 {
677         if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
678                         12, SIGBUS) == NOTIFY_STOP)
679                 return;
680         preempt_conditional_sti(regs);
681         do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
682         preempt_conditional_cli(regs);
683 }
684
685 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
686 {
687         static const char str[] = "double fault";
688         struct task_struct *tsk = current;
689
690         /* Return not checked because double check cannot be ignored */
691         notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
692
693         tsk->thread.error_code = error_code;
694         tsk->thread.trap_no = 8;
695
696         /* This is always a kernel trap and never fixable (and thus must
697            never return). */
698         for (;;)
699                 die(str, regs, error_code);
700 }
701
702 asmlinkage void __kprobes
703 do_general_protection(struct pt_regs *regs, long error_code)
704 {
705         struct task_struct *tsk;
706
707         conditional_sti(regs);
708
709         tsk = current;
710         if (!user_mode(regs))
711                 goto gp_in_kernel;
712
713         tsk->thread.error_code = error_code;
714         tsk->thread.trap_no = 13;
715
716         if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
717                         printk_ratelimit()) {
718                 printk(KERN_INFO
719                         "%s[%d] general protection ip:%lx sp:%lx error:%lx",
720                         tsk->comm, tsk->pid,
721                         regs->ip, regs->sp, error_code);
722                 print_vma_addr(" in ", regs->ip);
723                 printk("\n");
724         }
725
726         force_sig(SIGSEGV, tsk);
727         return;
728
729 gp_in_kernel:
730         if (fixup_exception(regs))
731                 return;
732
733         tsk->thread.error_code = error_code;
734         tsk->thread.trap_no = 13;
735         if (notify_die(DIE_GPF, "general protection fault", regs,
736                                 error_code, 13, SIGSEGV) == NOTIFY_STOP)
737                 return;
738         die("general protection fault", regs, error_code);
739 }
740
741 static notrace __kprobes void
742 mem_parity_error(unsigned char reason, struct pt_regs *regs)
743 {
744         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
745                 reason);
746         printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
747
748 #if defined(CONFIG_EDAC)
749         if (edac_handler_set()) {
750                 edac_atomic_assert_error();
751                 return;
752         }
753 #endif
754
755         if (panic_on_unrecovered_nmi)
756                 panic("NMI: Not continuing");
757
758         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
759
760         /* Clear and disable the memory parity error line. */
761         reason = (reason & 0xf) | 4;
762         outb(reason, 0x61);
763 }
764
765 static notrace __kprobes void
766 io_check_error(unsigned char reason, struct pt_regs *regs)
767 {
768         printk("NMI: IOCK error (debug interrupt?)\n");
769         show_registers(regs);
770
771         /* Re-enable the IOCK line, wait for a few seconds */
772         reason = (reason & 0xf) | 8;
773         outb(reason, 0x61);
774         mdelay(2000);
775         reason &= ~8;
776         outb(reason, 0x61);
777 }
778
779 static notrace __kprobes void
780 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
781 {
782         if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
783                 return;
784         printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
785                 reason);
786         printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
787
788         if (panic_on_unrecovered_nmi)
789                 panic("NMI: Not continuing");
790
791         printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
792 }
793
794 /* Runs on IST stack. This code must keep interrupts off all the time.
795    Nested NMIs are prevented by the CPU. */
796 asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
797 {
798         unsigned char reason = 0;
799         int cpu;
800
801         cpu = smp_processor_id();
802
803         /* Only the BSP gets external NMIs from the system. */
804         if (!cpu)
805                 reason = get_nmi_reason();
806
807         if (!(reason & 0xc0)) {
808                 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
809                                                                 == NOTIFY_STOP)
810                         return;
811                 /*
812                  * Ok, so this is none of the documented NMI sources,
813                  * so it must be the NMI watchdog.
814                  */
815                 if (nmi_watchdog_tick(regs, reason))
816                         return;
817                 if (!do_nmi_callback(regs, cpu))
818                         unknown_nmi_error(reason, regs);
819
820                 return;
821         }
822         if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
823                 return;
824
825         /* AK: following checks seem to be broken on modern chipsets. FIXME */
826         if (reason & 0x80)
827                 mem_parity_error(reason, regs);
828         if (reason & 0x40)
829                 io_check_error(reason, regs);
830 }
831
832 asmlinkage notrace __kprobes void
833 do_nmi(struct pt_regs *regs, long error_code)
834 {
835         nmi_enter();
836
837         add_pda(__nmi_count, 1);
838
839         if (!ignore_nmis)
840                 default_do_nmi(regs);
841
842         nmi_exit();
843 }
844
845 void stop_nmi(void)
846 {
847         acpi_nmi_disable();
848         ignore_nmis++;
849 }
850
851 void restart_nmi(void)
852 {
853         ignore_nmis--;
854         acpi_nmi_enable();
855 }
856
857 /* runs on IST stack. */
858 asmlinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
859 {
860         trace_hardirqs_fixup();
861
862         if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
863                         == NOTIFY_STOP)
864                 return;
865
866         preempt_conditional_sti(regs);
867         do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
868         preempt_conditional_cli(regs);
869 }
870
871 /* Help handler running on IST stack to switch back to user stack
872    for scheduling or signal handling. The actual stack switch is done in
873    entry.S */
874 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
875 {
876         struct pt_regs *regs = eregs;
877         /* Did already sync */
878         if (eregs == (struct pt_regs *)eregs->sp)
879                 ;
880         /* Exception from user space */
881         else if (user_mode(eregs))
882                 regs = task_pt_regs(current);
883         /* Exception from kernel and interrupts are enabled. Move to
884            kernel process stack. */
885         else if (eregs->flags & X86_EFLAGS_IF)
886                 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
887         if (eregs != regs)
888                 *regs = *eregs;
889         return regs;
890 }
891
892 /* runs on IST stack. */
893 asmlinkage void __kprobes do_debug(struct pt_regs * regs,
894                                    unsigned long error_code)
895 {
896         struct task_struct *tsk = current;
897         unsigned long condition;
898         siginfo_t info;
899
900         trace_hardirqs_fixup();
901
902         get_debugreg(condition, 6);
903
904         /*
905          * The processor cleared BTF, so don't mark that we need it set.
906          */
907         clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
908         tsk->thread.debugctlmsr = 0;
909
910         if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
911                                                 SIGTRAP) == NOTIFY_STOP)
912                 return;
913
914         preempt_conditional_sti(regs);
915
916         /* Mask out spurious debug traps due to lazy DR7 setting */
917         if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
918                 if (!tsk->thread.debugreg7)
919                         goto clear_dr7;
920         }
921
922         tsk->thread.debugreg6 = condition;
923
924         /*
925          * Single-stepping through TF: make sure we ignore any events in
926          * kernel space (but re-enable TF when returning to user mode).
927          */
928         if (condition & DR_STEP) {
929                 if (!user_mode(regs))
930                         goto clear_TF_reenable;
931         }
932
933         /* Ok, finally something we can handle */
934         tsk->thread.trap_no = 1;
935         tsk->thread.error_code = error_code;
936         info.si_signo = SIGTRAP;
937         info.si_errno = 0;
938         info.si_code = TRAP_BRKPT;
939         info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
940         force_sig_info(SIGTRAP, &info, tsk);
941
942 clear_dr7:
943         set_debugreg(0, 7);
944         preempt_conditional_cli(regs);
945         return;
946
947 clear_TF_reenable:
948         set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
949         regs->flags &= ~X86_EFLAGS_TF;
950         preempt_conditional_cli(regs);
951         return;
952 }
953
954 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
955 {
956         if (fixup_exception(regs))
957                 return 1;
958
959         notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
960         /* Illegal floating point operation in the kernel */
961         current->thread.trap_no = trapnr;
962         die(str, regs, 0);
963         return 0;
964 }
965
966 /*
967  * Note that we play around with the 'TS' bit in an attempt to get
968  * the correct behaviour even in the presence of the asynchronous
969  * IRQ13 behaviour
970  */
971 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
972 {
973         void __user *ip = (void __user *)(regs->ip);
974         struct task_struct *task;
975         siginfo_t info;
976         unsigned short cwd, swd;
977
978         conditional_sti(regs);
979         if (!user_mode(regs) &&
980             kernel_math_error(regs, "kernel x87 math error", 16))
981                 return;
982
983         /*
984          * Save the info for the exception handler and clear the error.
985          */
986         task = current;
987         save_init_fpu(task);
988         task->thread.trap_no = 16;
989         task->thread.error_code = 0;
990         info.si_signo = SIGFPE;
991         info.si_errno = 0;
992         info.si_code = __SI_FAULT;
993         info.si_addr = ip;
994         /*
995          * (~cwd & swd) will mask out exceptions that are not set to unmasked
996          * status.  0x3f is the exception bits in these regs, 0x200 is the
997          * C1 reg you need in case of a stack fault, 0x040 is the stack
998          * fault bit.  We should only be taking one exception at a time,
999          * so if this combination doesn't produce any single exception,
1000          * then we have a bad program that isn't synchronizing its FPU usage
1001          * and it will suffer the consequences since we won't be able to
1002          * fully reproduce the context of the exception
1003          */
1004         cwd = get_fpu_cwd(task);
1005         swd = get_fpu_swd(task);
1006         switch (swd & ~cwd & 0x3f) {
1007         case 0x000: /* No unmasked exception */
1008         default: /* Multiple exceptions */
1009                 break;
1010         case 0x001: /* Invalid Op */
1011                 /*
1012                  * swd & 0x240 == 0x040: Stack Underflow
1013                  * swd & 0x240 == 0x240: Stack Overflow
1014                  * User must clear the SF bit (0x40) if set
1015                  */
1016                 info.si_code = FPE_FLTINV;
1017                 break;
1018         case 0x002: /* Denormalize */
1019         case 0x010: /* Underflow */
1020                 info.si_code = FPE_FLTUND;
1021                 break;
1022         case 0x004: /* Zero Divide */
1023                 info.si_code = FPE_FLTDIV;
1024                 break;
1025         case 0x008: /* Overflow */
1026                 info.si_code = FPE_FLTOVF;
1027                 break;
1028         case 0x020: /* Precision */
1029                 info.si_code = FPE_FLTRES;
1030                 break;
1031         }
1032         force_sig_info(SIGFPE, &info, task);
1033 }
1034
1035 asmlinkage void bad_intr(void)
1036 {
1037         printk("bad interrupt"); 
1038 }
1039
1040 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1041 {
1042         void __user *ip = (void __user *)(regs->ip);
1043         struct task_struct *task;
1044         siginfo_t info;
1045         unsigned short mxcsr;
1046
1047         conditional_sti(regs);
1048         if (!user_mode(regs) &&
1049                 kernel_math_error(regs, "kernel simd math error", 19))
1050                 return;
1051
1052         /*
1053          * Save the info for the exception handler and clear the error.
1054          */
1055         task = current;
1056         save_init_fpu(task);
1057         task->thread.trap_no = 19;
1058         task->thread.error_code = 0;
1059         info.si_signo = SIGFPE;
1060         info.si_errno = 0;
1061         info.si_code = __SI_FAULT;
1062         info.si_addr = ip;
1063         /*
1064          * The SIMD FPU exceptions are handled a little differently, as there
1065          * is only a single status/control register.  Thus, to determine which
1066          * unmasked exception was caught we must mask the exception mask bits
1067          * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1068          */
1069         mxcsr = get_fpu_mxcsr(task);
1070         switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1071         case 0x000:
1072         default:
1073                 break;
1074         case 0x001: /* Invalid Op */
1075                 info.si_code = FPE_FLTINV;
1076                 break;
1077         case 0x002: /* Denormalize */
1078         case 0x010: /* Underflow */
1079                 info.si_code = FPE_FLTUND;
1080                 break;
1081         case 0x004: /* Zero Divide */
1082                 info.si_code = FPE_FLTDIV;
1083                 break;
1084         case 0x008: /* Overflow */
1085                 info.si_code = FPE_FLTOVF;
1086                 break;
1087         case 0x020: /* Precision */
1088                 info.si_code = FPE_FLTRES;
1089                 break;
1090         }
1091         force_sig_info(SIGFPE, &info, task);
1092 }
1093
1094 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1095 {
1096 }
1097
1098 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1099 {
1100 }
1101
1102 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1103 {
1104 }
1105
1106 /*
1107  * 'math_state_restore()' saves the current math information in the
1108  * old math state array, and gets the new ones from the current task
1109  *
1110  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1111  * Don't touch unless you *really* know how it works.
1112  */
1113 asmlinkage void math_state_restore(void)
1114 {
1115         struct task_struct *me = current;
1116
1117         if (!used_math()) {
1118                 local_irq_enable();
1119                 /*
1120                  * does a slab alloc which can sleep
1121                  */
1122                 if (init_fpu(me)) {
1123                         /*
1124                          * ran out of memory!
1125                          */
1126                         do_group_exit(SIGKILL);
1127                         return;
1128                 }
1129                 local_irq_disable();
1130         }
1131
1132         clts();                         /* Allow maths ops (or we recurse) */
1133         restore_fpu_checking(&me->thread.xstate->fxsave);
1134         task_thread_info(me)->status |= TS_USEDFPU;
1135         me->fpu_counter++;
1136 }
1137 EXPORT_SYMBOL_GPL(math_state_restore);
1138
1139 void __init trap_init(void)
1140 {
1141         set_intr_gate(0, &divide_error);
1142         set_intr_gate_ist(1, &debug, DEBUG_STACK);
1143         set_intr_gate_ist(2, &nmi, NMI_STACK);
1144         set_system_gate_ist(3, &int3, DEBUG_STACK); /* int3 can be called from all */
1145         set_system_gate(4, &overflow); /* int4 can be called from all */
1146         set_intr_gate(5, &bounds);
1147         set_intr_gate(6, &invalid_op);
1148         set_intr_gate(7, &device_not_available);
1149         set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
1150         set_intr_gate(9, &coprocessor_segment_overrun);
1151         set_intr_gate(10, &invalid_TSS);
1152         set_intr_gate(11, &segment_not_present);
1153         set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
1154         set_intr_gate(13, &general_protection);
1155         set_intr_gate(14, &page_fault);
1156         set_intr_gate(15, &spurious_interrupt_bug);
1157         set_intr_gate(16, &coprocessor_error);
1158         set_intr_gate(17, &alignment_check);
1159 #ifdef CONFIG_X86_MCE
1160         set_intr_gate_ist(18, &machine_check, MCE_STACK);
1161 #endif
1162         set_intr_gate(19, &simd_coprocessor_error);
1163
1164 #ifdef CONFIG_IA32_EMULATION
1165         set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1166 #endif
1167         /*
1168          * initialize the per thread extended state:
1169          */
1170         init_thread_xstate();
1171         /*
1172          * Should be a barrier for any external CPU state:
1173          */
1174         cpu_init();
1175 }
1176
1177 static int __init oops_setup(char *s)
1178 {
1179         if (!s)
1180                 return -EINVAL;
1181         if (!strcmp(s, "panic"))
1182                 panic_on_oops = 1;
1183         return 0;
1184 }
1185 early_param("oops", oops_setup);
1186
1187 static int __init kstack_setup(char *s)
1188 {
1189         if (!s)
1190                 return -EINVAL;
1191         kstack_depth_to_print = simple_strtoul(s, NULL, 0);
1192         return 0;
1193 }
1194 early_param("kstack", kstack_setup);
1195
1196 static int __init code_bytes_setup(char *s)
1197 {
1198         code_bytes = simple_strtoul(s, NULL, 0);
1199         if (code_bytes > 8192)
1200                 code_bytes = 8192;
1201
1202         return 1;
1203 }
1204 __setup("code_bytes=", code_bytes_setup);