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