x86: turn 64 bit x86 HANDLE_STACK into print_context_stack like 32 bit has
[wrt350n-kernel.git] / arch / x86 / kernel / traps_64.c
blobb8303ed950571c758784bf27cdd2007f84b66021
1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
7 */
9 /*
10 * 'Traps.c' handles hardware traps and faults after we have saved some
11 * state in 'entry.S'.
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>
36 #if defined(CONFIG_EDAC)
37 #include <linux/edac.h>
38 #endif
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>
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);
77 static inline void conditional_sti(struct pt_regs *regs)
79 if (regs->flags & X86_EFLAGS_IF)
80 local_irq_enable();
83 static inline void preempt_conditional_sti(struct pt_regs *regs)
85 preempt_disable();
86 if (regs->flags & X86_EFLAGS_IF)
87 local_irq_enable();
90 static inline void preempt_conditional_cli(struct pt_regs *regs)
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();
99 int kstack_depth_to_print = 12;
101 #ifdef CONFIG_KALLSYMS
102 void printk_address(unsigned long address, int reliable)
104 unsigned long offset = 0, symsize;
105 const char *symname;
106 char *modname;
107 char *delim = ":";
108 char namebuf[128];
109 char reliab[4] = "";;
111 symname = kallsyms_lookup(address, &symsize, &offset,
112 &modname, namebuf);
113 if (!symname) {
114 printk(" [<%016lx>]\n", address);
115 return;
117 if (!reliable)
118 strcpy(reliab, "? ");
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);
125 #else
126 void printk_address(unsigned long address, int reliable)
128 printk(" [<%016lx>]\n", address);
130 #endif
132 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
133 unsigned *usedp, char **idp)
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
145 unsigned k;
148 * Iterate over all exception stacks, and figure out whether
149 * 'stack' is in one of them:
151 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
152 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
154 * Is 'stack' above this exception frame's end?
155 * If yes then skip to the next frame.
157 if (stack >= end)
158 continue;
160 * Is 'stack' above this exception frame's start address?
161 * If yes then we found the right frame.
163 if (stack >= end - EXCEPTION_STKSZ) {
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:
170 if (*usedp & (1U << k))
171 break;
172 *usedp |= 1U << k;
173 *idp = ids[k];
174 return (unsigned long *)end;
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:
181 #if DEBUG_STKSZ > EXCEPTION_STKSZ
182 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
183 unsigned j = N_EXCEPTION_STACKS - 1;
186 * Black magic. A large debug stack is composed of
187 * multiple exception stack entries, which we
188 * iterate through now. Dont look:
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;
201 #endif
203 return NULL;
206 #define MSG(txt) ops->warning(data, txt)
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
215 static inline int valid_stack_ptr(struct thread_info *tinfo,
216 void *p, unsigned int size, void *end)
218 void *t = (void *)tinfo;
219 if (end) {
220 if (p < end && p >= (end-THREAD_SIZE))
221 return 1;
222 else
223 return 0;
225 return p > t && p < t + THREAD_SIZE - size;
228 static inline unsigned long print_context_stack(struct thread_info *tinfo,
229 unsigned long *stack, unsigned long bp,
230 const struct stacktrace_ops *ops, void *data,
231 unsigned long *end)
234 * Print function call entries within a stack. 'cond' is the
235 * "end of stackframe" condition, that the 'stack++'
236 * iteration will eventually trigger.
238 while (valid_stack_ptr(tinfo, stack, 3, end)) {
239 unsigned long addr = *stack++;
240 /* Use unlocked access here because except for NMIs
241 we should be already protected against module unloads */
242 if (__kernel_text_address(addr)) {
244 * If the address is either in the text segment of the
245 * kernel, or in the region which contains vmalloc'ed
246 * memory, it *may* be the address of a calling
247 * routine; if so, print it so that someone tracing
248 * down the cause of the crash will be able to figure
249 * out the call path that was taken.
251 ops->address(data, addr, 1);
254 return bp;
257 void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
258 unsigned long *stack, unsigned long bp,
259 const struct stacktrace_ops *ops, void *data)
261 const unsigned cpu = get_cpu();
262 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
263 unsigned used = 0;
264 struct thread_info *tinfo;
266 if (!tsk)
267 tsk = current;
268 tinfo = task_thread_info(tsk);
270 if (!stack) {
271 unsigned long dummy;
272 stack = &dummy;
273 if (tsk && tsk != current)
274 stack = (unsigned long *)tsk->thread.sp;
279 * Print function call entries in all stacks, starting at the
280 * current stack address. If the stacks consist of nested
281 * exceptions
283 for (;;) {
284 char *id;
285 unsigned long *estack_end;
286 estack_end = in_exception_stack(cpu, (unsigned long)stack,
287 &used, &id);
289 if (estack_end) {
290 if (ops->stack(data, id) < 0)
291 break;
293 print_context_stack(tinfo, stack, 0, ops,
294 data, estack_end);
295 ops->stack(data, "<EOE>");
297 * We link to the next stack via the
298 * second-to-last pointer (index -2 to end) in the
299 * exception stack:
301 stack = (unsigned long *) estack_end[-2];
302 continue;
304 if (irqstack_end) {
305 unsigned long *irqstack;
306 irqstack = irqstack_end -
307 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
309 if (stack >= irqstack && stack < irqstack_end) {
310 if (ops->stack(data, "IRQ") < 0)
311 break;
312 print_context_stack(tinfo, stack, 0, ops,
313 data, irqstack_end);
315 * We link to the next stack (which would be
316 * the process stack normally) the last
317 * pointer (index -1 to end) in the IRQ stack:
319 stack = (unsigned long *) (irqstack_end[-1]);
320 irqstack_end = NULL;
321 ops->stack(data, "EOI");
322 continue;
325 break;
329 * This handles the process stack:
331 print_context_stack(tinfo, stack, 0, ops, data, NULL);
332 put_cpu();
334 EXPORT_SYMBOL(dump_trace);
336 static void
337 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
339 print_symbol(msg, symbol);
340 printk("\n");
343 static void print_trace_warning(void *data, char *msg)
345 printk("%s\n", msg);
348 static int print_trace_stack(void *data, char *name)
350 printk(" <%s> ", name);
351 return 0;
354 static void print_trace_address(void *data, unsigned long addr, int reliable)
356 touch_nmi_watchdog();
357 printk_address(addr, reliable);
360 static const struct stacktrace_ops print_trace_ops = {
361 .warning = print_trace_warning,
362 .warning_symbol = print_trace_warning_symbol,
363 .stack = print_trace_stack,
364 .address = print_trace_address,
367 void
368 show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
369 unsigned long bp)
371 printk("\nCall Trace:\n");
372 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
373 printk("\n");
376 static void
377 _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
378 unsigned long bp)
380 unsigned long *stack;
381 int i;
382 const int cpu = smp_processor_id();
383 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
384 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
386 // debugging aid: "show_stack(NULL, NULL);" prints the
387 // back trace for this cpu.
389 if (sp == NULL) {
390 if (tsk)
391 sp = (unsigned long *)tsk->thread.sp;
392 else
393 sp = (unsigned long *)&sp;
396 stack = sp;
397 for(i=0; i < kstack_depth_to_print; i++) {
398 if (stack >= irqstack && stack <= irqstack_end) {
399 if (stack == irqstack_end) {
400 stack = (unsigned long *) (irqstack_end[-1]);
401 printk(" <EOI> ");
403 } else {
404 if (((long) stack & (THREAD_SIZE-1)) == 0)
405 break;
407 if (i && ((i % 4) == 0))
408 printk("\n");
409 printk(" %016lx", *stack++);
410 touch_nmi_watchdog();
412 show_trace(tsk, regs, sp, bp);
415 void show_stack(struct task_struct *tsk, unsigned long * sp)
417 _show_stack(tsk, NULL, sp, 0);
421 * The architecture-independent dump_stack generator
423 void dump_stack(void)
425 unsigned long dummy;
426 unsigned long bp = 0;
428 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
429 current->pid, current->comm, print_tainted(),
430 init_utsname()->release,
431 (int)strcspn(init_utsname()->version, " "),
432 init_utsname()->version);
433 show_trace(NULL, NULL, &dummy, bp);
436 EXPORT_SYMBOL(dump_stack);
438 void show_registers(struct pt_regs *regs)
440 int i;
441 int in_kernel = !user_mode(regs);
442 unsigned long sp;
443 const int cpu = smp_processor_id();
444 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
446 sp = regs->sp;
447 printk("CPU %d ", cpu);
448 __show_regs(regs);
449 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
450 cur->comm, cur->pid, task_thread_info(cur), cur);
453 * When in-kernel, we also print out the stack and code at the
454 * time of the fault..
456 if (in_kernel) {
457 printk("Stack: ");
458 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
460 printk("\nCode: ");
461 if (regs->ip < PAGE_OFFSET)
462 goto bad;
464 for (i=0; i<20; i++) {
465 unsigned char c;
466 if (__get_user(c, &((unsigned char*)regs->ip)[i])) {
467 bad:
468 printk(" Bad RIP value.");
469 break;
471 printk("%02x ", c);
474 printk("\n");
477 int is_valid_bugaddr(unsigned long ip)
479 unsigned short ud2;
481 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
482 return 0;
484 return ud2 == 0x0b0f;
487 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
488 static int die_owner = -1;
489 static unsigned int die_nest_count;
491 unsigned __kprobes long oops_begin(void)
493 int cpu;
494 unsigned long flags;
496 oops_enter();
498 /* racy, but better than risking deadlock. */
499 raw_local_irq_save(flags);
500 cpu = smp_processor_id();
501 if (!__raw_spin_trylock(&die_lock)) {
502 if (cpu == die_owner)
503 /* nested oops. should stop eventually */;
504 else
505 __raw_spin_lock(&die_lock);
507 die_nest_count++;
508 die_owner = cpu;
509 console_verbose();
510 bust_spinlocks(1);
511 return flags;
514 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
516 die_owner = -1;
517 bust_spinlocks(0);
518 die_nest_count--;
519 if (!die_nest_count)
520 /* Nest count reaches zero, release the lock. */
521 __raw_spin_unlock(&die_lock);
522 raw_local_irq_restore(flags);
523 if (!regs) {
524 oops_exit();
525 return;
527 if (panic_on_oops)
528 panic("Fatal exception");
529 oops_exit();
530 do_exit(signr);
533 int __kprobes __die(const char * str, struct pt_regs * regs, long err)
535 static int die_counter;
536 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
537 #ifdef CONFIG_PREEMPT
538 printk("PREEMPT ");
539 #endif
540 #ifdef CONFIG_SMP
541 printk("SMP ");
542 #endif
543 #ifdef CONFIG_DEBUG_PAGEALLOC
544 printk("DEBUG_PAGEALLOC");
545 #endif
546 printk("\n");
547 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
548 return 1;
549 show_registers(regs);
550 add_taint(TAINT_DIE);
551 /* Executive summary in case the oops scrolled away */
552 printk(KERN_ALERT "RIP ");
553 printk_address(regs->ip, regs->bp);
554 printk(" RSP <%016lx>\n", regs->sp);
555 if (kexec_should_crash(current))
556 crash_kexec(regs);
557 return 0;
560 void die(const char * str, struct pt_regs * regs, long err)
562 unsigned long flags = oops_begin();
564 if (!user_mode(regs))
565 report_bug(regs->ip, regs);
567 if (__die(str, regs, err))
568 regs = NULL;
569 oops_end(flags, regs, SIGSEGV);
572 void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
574 unsigned long flags = oops_begin();
577 * We are in trouble anyway, lets at least try
578 * to get a message out.
580 printk(str, smp_processor_id());
581 show_registers(regs);
582 if (kexec_should_crash(current))
583 crash_kexec(regs);
584 if (do_panic || panic_on_oops)
585 panic("Non maskable interrupt");
586 oops_end(flags, NULL, SIGBUS);
587 nmi_exit();
588 local_irq_enable();
589 do_exit(SIGBUS);
592 static void __kprobes do_trap(int trapnr, int signr, char *str,
593 struct pt_regs * regs, long error_code,
594 siginfo_t *info)
596 struct task_struct *tsk = current;
598 if (user_mode(regs)) {
600 * We want error_code and trap_no set for userspace
601 * faults and kernelspace faults which result in
602 * die(), but not kernelspace faults which are fixed
603 * up. die() gives the process no chance to handle
604 * the signal and notice the kernel fault information,
605 * so that won't result in polluting the information
606 * about previously queued, but not yet delivered,
607 * faults. See also do_general_protection below.
609 tsk->thread.error_code = error_code;
610 tsk->thread.trap_no = trapnr;
612 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
613 printk_ratelimit())
614 printk(KERN_INFO
615 "%s[%d] trap %s ip:%lx sp:%lx error:%lx\n",
616 tsk->comm, tsk->pid, str,
617 regs->ip, regs->sp, error_code);
619 if (info)
620 force_sig_info(signr, info, tsk);
621 else
622 force_sig(signr, tsk);
623 return;
627 if (!fixup_exception(regs)) {
628 tsk->thread.error_code = error_code;
629 tsk->thread.trap_no = trapnr;
630 die(str, regs, error_code);
632 return;
635 #define DO_ERROR(trapnr, signr, str, name) \
636 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
638 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
639 == NOTIFY_STOP) \
640 return; \
641 conditional_sti(regs); \
642 do_trap(trapnr, signr, str, regs, error_code, NULL); \
645 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
646 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
648 siginfo_t info; \
649 info.si_signo = signr; \
650 info.si_errno = 0; \
651 info.si_code = sicode; \
652 info.si_addr = (void __user *)siaddr; \
653 trace_hardirqs_fixup(); \
654 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
655 == NOTIFY_STOP) \
656 return; \
657 conditional_sti(regs); \
658 do_trap(trapnr, signr, str, regs, error_code, &info); \
661 DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
662 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
663 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
664 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
665 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
666 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
667 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
668 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
669 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
670 DO_ERROR(18, SIGSEGV, "reserved", reserved)
672 /* Runs on IST stack */
673 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
675 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
676 12, SIGBUS) == NOTIFY_STOP)
677 return;
678 preempt_conditional_sti(regs);
679 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
680 preempt_conditional_cli(regs);
683 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
685 static const char str[] = "double fault";
686 struct task_struct *tsk = current;
688 /* Return not checked because double check cannot be ignored */
689 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
691 tsk->thread.error_code = error_code;
692 tsk->thread.trap_no = 8;
694 /* This is always a kernel trap and never fixable (and thus must
695 never return). */
696 for (;;)
697 die(str, regs, error_code);
700 asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
701 long error_code)
703 struct task_struct *tsk = current;
705 conditional_sti(regs);
707 if (user_mode(regs)) {
708 tsk->thread.error_code = error_code;
709 tsk->thread.trap_no = 13;
711 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
712 printk_ratelimit())
713 printk(KERN_INFO
714 "%s[%d] general protection ip:%lx sp:%lx error:%lx\n",
715 tsk->comm, tsk->pid,
716 regs->ip, regs->sp, error_code);
718 force_sig(SIGSEGV, tsk);
719 return;
722 if (fixup_exception(regs))
723 return;
725 tsk->thread.error_code = error_code;
726 tsk->thread.trap_no = 13;
727 if (notify_die(DIE_GPF, "general protection fault", regs,
728 error_code, 13, SIGSEGV) == NOTIFY_STOP)
729 return;
730 die("general protection fault", regs, error_code);
733 static __kprobes void
734 mem_parity_error(unsigned char reason, struct pt_regs * regs)
736 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
737 reason);
738 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
740 #if defined(CONFIG_EDAC)
741 if(edac_handler_set()) {
742 edac_atomic_assert_error();
743 return;
745 #endif
747 if (panic_on_unrecovered_nmi)
748 panic("NMI: Not continuing");
750 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
752 /* Clear and disable the memory parity error line. */
753 reason = (reason & 0xf) | 4;
754 outb(reason, 0x61);
757 static __kprobes void
758 io_check_error(unsigned char reason, struct pt_regs * regs)
760 printk("NMI: IOCK error (debug interrupt?)\n");
761 show_registers(regs);
763 /* Re-enable the IOCK line, wait for a few seconds */
764 reason = (reason & 0xf) | 8;
765 outb(reason, 0x61);
766 mdelay(2000);
767 reason &= ~8;
768 outb(reason, 0x61);
771 static __kprobes void
772 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
774 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
775 reason);
776 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
778 if (panic_on_unrecovered_nmi)
779 panic("NMI: Not continuing");
781 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
784 /* Runs on IST stack. This code must keep interrupts off all the time.
785 Nested NMIs are prevented by the CPU. */
786 asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
788 unsigned char reason = 0;
789 int cpu;
791 cpu = smp_processor_id();
793 /* Only the BSP gets external NMIs from the system. */
794 if (!cpu)
795 reason = get_nmi_reason();
797 if (!(reason & 0xc0)) {
798 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
799 == NOTIFY_STOP)
800 return;
802 * Ok, so this is none of the documented NMI sources,
803 * so it must be the NMI watchdog.
805 if (nmi_watchdog_tick(regs,reason))
806 return;
807 if (!do_nmi_callback(regs,cpu))
808 unknown_nmi_error(reason, regs);
810 return;
812 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
813 return;
815 /* AK: following checks seem to be broken on modern chipsets. FIXME */
817 if (reason & 0x80)
818 mem_parity_error(reason, regs);
819 if (reason & 0x40)
820 io_check_error(reason, regs);
823 /* runs on IST stack. */
824 asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
826 trace_hardirqs_fixup();
828 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
829 return;
831 preempt_conditional_sti(regs);
832 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
833 preempt_conditional_cli(regs);
836 /* Help handler running on IST stack to switch back to user stack
837 for scheduling or signal handling. The actual stack switch is done in
838 entry.S */
839 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
841 struct pt_regs *regs = eregs;
842 /* Did already sync */
843 if (eregs == (struct pt_regs *)eregs->sp)
845 /* Exception from user space */
846 else if (user_mode(eregs))
847 regs = task_pt_regs(current);
848 /* Exception from kernel and interrupts are enabled. Move to
849 kernel process stack. */
850 else if (eregs->flags & X86_EFLAGS_IF)
851 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
852 if (eregs != regs)
853 *regs = *eregs;
854 return regs;
857 /* runs on IST stack. */
858 asmlinkage void __kprobes do_debug(struct pt_regs * regs,
859 unsigned long error_code)
861 unsigned long condition;
862 struct task_struct *tsk = current;
863 siginfo_t info;
865 trace_hardirqs_fixup();
867 get_debugreg(condition, 6);
870 * The processor cleared BTF, so don't mark that we need it set.
872 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
873 tsk->thread.debugctlmsr = 0;
875 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
876 SIGTRAP) == NOTIFY_STOP)
877 return;
879 preempt_conditional_sti(regs);
881 /* Mask out spurious debug traps due to lazy DR7 setting */
882 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
883 if (!tsk->thread.debugreg7) {
884 goto clear_dr7;
888 tsk->thread.debugreg6 = condition;
892 * Single-stepping through TF: make sure we ignore any events in
893 * kernel space (but re-enable TF when returning to user mode).
895 if (condition & DR_STEP) {
896 if (!user_mode(regs))
897 goto clear_TF_reenable;
900 /* Ok, finally something we can handle */
901 tsk->thread.trap_no = 1;
902 tsk->thread.error_code = error_code;
903 info.si_signo = SIGTRAP;
904 info.si_errno = 0;
905 info.si_code = TRAP_BRKPT;
906 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
907 force_sig_info(SIGTRAP, &info, tsk);
909 clear_dr7:
910 set_debugreg(0UL, 7);
911 preempt_conditional_cli(regs);
912 return;
914 clear_TF_reenable:
915 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
916 regs->flags &= ~X86_EFLAGS_TF;
917 preempt_conditional_cli(regs);
920 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
922 if (fixup_exception(regs))
923 return 1;
925 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
926 /* Illegal floating point operation in the kernel */
927 current->thread.trap_no = trapnr;
928 die(str, regs, 0);
929 return 0;
933 * Note that we play around with the 'TS' bit in an attempt to get
934 * the correct behaviour even in the presence of the asynchronous
935 * IRQ13 behaviour
937 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
939 void __user *ip = (void __user *)(regs->ip);
940 struct task_struct * task;
941 siginfo_t info;
942 unsigned short cwd, swd;
944 conditional_sti(regs);
945 if (!user_mode(regs) &&
946 kernel_math_error(regs, "kernel x87 math error", 16))
947 return;
950 * Save the info for the exception handler and clear the error.
952 task = current;
953 save_init_fpu(task);
954 task->thread.trap_no = 16;
955 task->thread.error_code = 0;
956 info.si_signo = SIGFPE;
957 info.si_errno = 0;
958 info.si_code = __SI_FAULT;
959 info.si_addr = ip;
961 * (~cwd & swd) will mask out exceptions that are not set to unmasked
962 * status. 0x3f is the exception bits in these regs, 0x200 is the
963 * C1 reg you need in case of a stack fault, 0x040 is the stack
964 * fault bit. We should only be taking one exception at a time,
965 * so if this combination doesn't produce any single exception,
966 * then we have a bad program that isn't synchronizing its FPU usage
967 * and it will suffer the consequences since we won't be able to
968 * fully reproduce the context of the exception
970 cwd = get_fpu_cwd(task);
971 swd = get_fpu_swd(task);
972 switch (swd & ~cwd & 0x3f) {
973 case 0x000:
974 default:
975 break;
976 case 0x001: /* Invalid Op */
978 * swd & 0x240 == 0x040: Stack Underflow
979 * swd & 0x240 == 0x240: Stack Overflow
980 * User must clear the SF bit (0x40) if set
982 info.si_code = FPE_FLTINV;
983 break;
984 case 0x002: /* Denormalize */
985 case 0x010: /* Underflow */
986 info.si_code = FPE_FLTUND;
987 break;
988 case 0x004: /* Zero Divide */
989 info.si_code = FPE_FLTDIV;
990 break;
991 case 0x008: /* Overflow */
992 info.si_code = FPE_FLTOVF;
993 break;
994 case 0x020: /* Precision */
995 info.si_code = FPE_FLTRES;
996 break;
998 force_sig_info(SIGFPE, &info, task);
1001 asmlinkage void bad_intr(void)
1003 printk("bad interrupt");
1006 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1008 void __user *ip = (void __user *)(regs->ip);
1009 struct task_struct * task;
1010 siginfo_t info;
1011 unsigned short mxcsr;
1013 conditional_sti(regs);
1014 if (!user_mode(regs) &&
1015 kernel_math_error(regs, "kernel simd math error", 19))
1016 return;
1019 * Save the info for the exception handler and clear the error.
1021 task = current;
1022 save_init_fpu(task);
1023 task->thread.trap_no = 19;
1024 task->thread.error_code = 0;
1025 info.si_signo = SIGFPE;
1026 info.si_errno = 0;
1027 info.si_code = __SI_FAULT;
1028 info.si_addr = ip;
1030 * The SIMD FPU exceptions are handled a little differently, as there
1031 * is only a single status/control register. Thus, to determine which
1032 * unmasked exception was caught we must mask the exception mask bits
1033 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1035 mxcsr = get_fpu_mxcsr(task);
1036 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1037 case 0x000:
1038 default:
1039 break;
1040 case 0x001: /* Invalid Op */
1041 info.si_code = FPE_FLTINV;
1042 break;
1043 case 0x002: /* Denormalize */
1044 case 0x010: /* Underflow */
1045 info.si_code = FPE_FLTUND;
1046 break;
1047 case 0x004: /* Zero Divide */
1048 info.si_code = FPE_FLTDIV;
1049 break;
1050 case 0x008: /* Overflow */
1051 info.si_code = FPE_FLTOVF;
1052 break;
1053 case 0x020: /* Precision */
1054 info.si_code = FPE_FLTRES;
1055 break;
1057 force_sig_info(SIGFPE, &info, task);
1060 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1064 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1068 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1073 * 'math_state_restore()' saves the current math information in the
1074 * old math state array, and gets the new ones from the current task
1076 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1077 * Don't touch unless you *really* know how it works.
1079 asmlinkage void math_state_restore(void)
1081 struct task_struct *me = current;
1082 clts(); /* Allow maths ops (or we recurse) */
1084 if (!used_math())
1085 init_fpu(me);
1086 restore_fpu_checking(&me->thread.i387.fxsave);
1087 task_thread_info(me)->status |= TS_USEDFPU;
1088 me->fpu_counter++;
1090 EXPORT_SYMBOL_GPL(math_state_restore);
1092 void __init trap_init(void)
1094 set_intr_gate(0,&divide_error);
1095 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1096 set_intr_gate_ist(2,&nmi,NMI_STACK);
1097 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
1098 set_system_gate(4,&overflow); /* int4 can be called from all */
1099 set_intr_gate(5,&bounds);
1100 set_intr_gate(6,&invalid_op);
1101 set_intr_gate(7,&device_not_available);
1102 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1103 set_intr_gate(9,&coprocessor_segment_overrun);
1104 set_intr_gate(10,&invalid_TSS);
1105 set_intr_gate(11,&segment_not_present);
1106 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1107 set_intr_gate(13,&general_protection);
1108 set_intr_gate(14,&page_fault);
1109 set_intr_gate(15,&spurious_interrupt_bug);
1110 set_intr_gate(16,&coprocessor_error);
1111 set_intr_gate(17,&alignment_check);
1112 #ifdef CONFIG_X86_MCE
1113 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1114 #endif
1115 set_intr_gate(19,&simd_coprocessor_error);
1117 #ifdef CONFIG_IA32_EMULATION
1118 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1119 #endif
1122 * Should be a barrier for any external CPU state.
1124 cpu_init();
1128 static int __init oops_setup(char *s)
1130 if (!s)
1131 return -EINVAL;
1132 if (!strcmp(s, "panic"))
1133 panic_on_oops = 1;
1134 return 0;
1136 early_param("oops", oops_setup);
1138 static int __init kstack_setup(char *s)
1140 if (!s)
1141 return -EINVAL;
1142 kstack_depth_to_print = simple_strtoul(s,NULL,0);
1143 return 0;
1145 early_param("kstack", kstack_setup);