Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / x86 / kernel / traps_64.c
blob0454666819117b93d768de5f7f39304130143b0e
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 unsigned int code_bytes = 64;
79 static inline void conditional_sti(struct pt_regs *regs)
81 if (regs->flags & X86_EFLAGS_IF)
82 local_irq_enable();
85 static inline void preempt_conditional_sti(struct pt_regs *regs)
87 inc_preempt_count();
88 if (regs->flags & X86_EFLAGS_IF)
89 local_irq_enable();
92 static inline void preempt_conditional_cli(struct pt_regs *regs)
94 if (regs->flags & X86_EFLAGS_IF)
95 local_irq_disable();
96 /* Make sure to not schedule here because we could be running
97 on an exception stack. */
98 dec_preempt_count();
101 int kstack_depth_to_print = 12;
103 void printk_address(unsigned long address, int reliable)
105 #ifdef CONFIG_KALLSYMS
106 unsigned long offset = 0, symsize;
107 const char *symname;
108 char *modname;
109 char *delim = ":";
110 char namebuf[KSYM_NAME_LEN];
111 char reliab[4] = "";
113 symname = kallsyms_lookup(address, &symsize, &offset,
114 &modname, namebuf);
115 if (!symname) {
116 printk(" [<%016lx>]\n", address);
117 return;
119 if (!reliable)
120 strcpy(reliab, "? ");
122 if (!modname)
123 modname = delim = "";
124 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
125 address, reliab, delim, modname, delim, symname, offset, symsize);
126 #else
127 printk(" [<%016lx>]\n", address);
128 #endif
131 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
132 unsigned *usedp, char **idp)
134 static char ids[][8] = {
135 [DEBUG_STACK - 1] = "#DB",
136 [NMI_STACK - 1] = "NMI",
137 [DOUBLEFAULT_STACK - 1] = "#DF",
138 [STACKFAULT_STACK - 1] = "#SS",
139 [MCE_STACK - 1] = "#MC",
140 #if DEBUG_STKSZ > EXCEPTION_STKSZ
141 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
142 #endif
144 unsigned k;
147 * Iterate over all exception stacks, and figure out whether
148 * 'stack' is in one of them:
150 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
151 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
153 * Is 'stack' above this exception frame's end?
154 * If yes then skip to the next frame.
156 if (stack >= end)
157 continue;
159 * Is 'stack' above this exception frame's start address?
160 * If yes then we found the right frame.
162 if (stack >= end - EXCEPTION_STKSZ) {
164 * Make sure we only iterate through an exception
165 * stack once. If it comes up for the second time
166 * then there's something wrong going on - just
167 * break out and return NULL:
169 if (*usedp & (1U << k))
170 break;
171 *usedp |= 1U << k;
172 *idp = ids[k];
173 return (unsigned long *)end;
176 * If this is a debug stack, and if it has a larger size than
177 * the usual exception stacks, then 'stack' might still
178 * be within the lower portion of the debug stack:
180 #if DEBUG_STKSZ > EXCEPTION_STKSZ
181 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
182 unsigned j = N_EXCEPTION_STACKS - 1;
185 * Black magic. A large debug stack is composed of
186 * multiple exception stack entries, which we
187 * iterate through now. Dont look:
189 do {
190 ++j;
191 end -= EXCEPTION_STKSZ;
192 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
193 } while (stack < end - EXCEPTION_STKSZ);
194 if (*usedp & (1U << j))
195 break;
196 *usedp |= 1U << j;
197 *idp = ids[j];
198 return (unsigned long *)end;
200 #endif
202 return NULL;
205 #define MSG(txt) ops->warning(data, txt)
208 * x86-64 can have up to three kernel stacks:
209 * process stack
210 * interrupt stack
211 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
214 static inline int valid_stack_ptr(struct thread_info *tinfo,
215 void *p, unsigned int size, void *end)
217 void *t = tinfo;
218 if (end) {
219 if (p < end && p >= (end-THREAD_SIZE))
220 return 1;
221 else
222 return 0;
224 return p > t && p < t + THREAD_SIZE - size;
227 /* The form of the top of the frame on the stack */
228 struct stack_frame {
229 struct stack_frame *next_frame;
230 unsigned long return_address;
234 static inline unsigned long print_context_stack(struct thread_info *tinfo,
235 unsigned long *stack, unsigned long bp,
236 const struct stacktrace_ops *ops, void *data,
237 unsigned long *end)
239 struct stack_frame *frame = (struct stack_frame *)bp;
241 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
242 unsigned long addr;
244 addr = *stack;
245 if (__kernel_text_address(addr)) {
246 if ((unsigned long) stack == bp + 8) {
247 ops->address(data, addr, 1);
248 frame = frame->next_frame;
249 bp = (unsigned long) frame;
250 } else {
251 ops->address(data, addr, bp == 0);
254 stack++;
256 return bp;
259 void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
260 unsigned long *stack, unsigned long bp,
261 const struct stacktrace_ops *ops, void *data)
263 const unsigned cpu = get_cpu();
264 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
265 unsigned used = 0;
266 struct thread_info *tinfo;
268 if (!tsk)
269 tsk = current;
270 tinfo = task_thread_info(tsk);
272 if (!stack) {
273 unsigned long dummy;
274 stack = &dummy;
275 if (tsk && tsk != current)
276 stack = (unsigned long *)tsk->thread.sp;
279 #ifdef CONFIG_FRAME_POINTER
280 if (!bp) {
281 if (tsk == current) {
282 /* Grab bp right from our regs */
283 asm("movq %%rbp, %0" : "=r" (bp):);
284 } else {
285 /* bp is the last reg pushed by switch_to */
286 bp = *(unsigned long *) tsk->thread.sp;
289 #endif
294 * Print function call entries in all stacks, starting at the
295 * current stack address. If the stacks consist of nested
296 * exceptions
298 for (;;) {
299 char *id;
300 unsigned long *estack_end;
301 estack_end = in_exception_stack(cpu, (unsigned long)stack,
302 &used, &id);
304 if (estack_end) {
305 if (ops->stack(data, id) < 0)
306 break;
308 bp = print_context_stack(tinfo, stack, bp, ops,
309 data, estack_end);
310 ops->stack(data, "<EOE>");
312 * We link to the next stack via the
313 * second-to-last pointer (index -2 to end) in the
314 * exception stack:
316 stack = (unsigned long *) estack_end[-2];
317 continue;
319 if (irqstack_end) {
320 unsigned long *irqstack;
321 irqstack = irqstack_end -
322 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
324 if (stack >= irqstack && stack < irqstack_end) {
325 if (ops->stack(data, "IRQ") < 0)
326 break;
327 bp = print_context_stack(tinfo, stack, bp,
328 ops, data, irqstack_end);
330 * We link to the next stack (which would be
331 * the process stack normally) the last
332 * pointer (index -1 to end) in the IRQ stack:
334 stack = (unsigned long *) (irqstack_end[-1]);
335 irqstack_end = NULL;
336 ops->stack(data, "EOI");
337 continue;
340 break;
344 * This handles the process stack:
346 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
347 put_cpu();
349 EXPORT_SYMBOL(dump_trace);
351 static void
352 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
354 print_symbol(msg, symbol);
355 printk("\n");
358 static void print_trace_warning(void *data, char *msg)
360 printk("%s\n", msg);
363 static int print_trace_stack(void *data, char *name)
365 printk(" <%s> ", name);
366 return 0;
369 static void print_trace_address(void *data, unsigned long addr, int reliable)
371 touch_nmi_watchdog();
372 printk_address(addr, reliable);
375 static const struct stacktrace_ops print_trace_ops = {
376 .warning = print_trace_warning,
377 .warning_symbol = print_trace_warning_symbol,
378 .stack = print_trace_stack,
379 .address = print_trace_address,
382 void
383 show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
384 unsigned long bp)
386 printk("\nCall Trace:\n");
387 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
388 printk("\n");
391 static void
392 _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
393 unsigned long bp)
395 unsigned long *stack;
396 int i;
397 const int cpu = smp_processor_id();
398 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
399 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
401 // debugging aid: "show_stack(NULL, NULL);" prints the
402 // back trace for this cpu.
404 if (sp == NULL) {
405 if (tsk)
406 sp = (unsigned long *)tsk->thread.sp;
407 else
408 sp = (unsigned long *)&sp;
411 stack = sp;
412 for(i=0; i < kstack_depth_to_print; i++) {
413 if (stack >= irqstack && stack <= irqstack_end) {
414 if (stack == irqstack_end) {
415 stack = (unsigned long *) (irqstack_end[-1]);
416 printk(" <EOI> ");
418 } else {
419 if (((long) stack & (THREAD_SIZE-1)) == 0)
420 break;
422 if (i && ((i % 4) == 0))
423 printk("\n");
424 printk(" %016lx", *stack++);
425 touch_nmi_watchdog();
427 show_trace(tsk, regs, sp, bp);
430 void show_stack(struct task_struct *tsk, unsigned long * sp)
432 _show_stack(tsk, NULL, sp, 0);
436 * The architecture-independent dump_stack generator
438 void dump_stack(void)
440 unsigned long dummy;
441 unsigned long bp = 0;
443 #ifdef CONFIG_FRAME_POINTER
444 if (!bp)
445 asm("movq %%rbp, %0" : "=r" (bp):);
446 #endif
448 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
449 current->pid, current->comm, print_tainted(),
450 init_utsname()->release,
451 (int)strcspn(init_utsname()->version, " "),
452 init_utsname()->version);
453 show_trace(NULL, NULL, &dummy, bp);
456 EXPORT_SYMBOL(dump_stack);
458 void show_registers(struct pt_regs *regs)
460 int i;
461 unsigned long sp;
462 const int cpu = smp_processor_id();
463 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
464 u8 *ip;
465 unsigned int code_prologue = code_bytes * 43 / 64;
466 unsigned int code_len = code_bytes;
468 sp = regs->sp;
469 ip = (u8 *) regs->ip - code_prologue;
470 printk("CPU %d ", cpu);
471 __show_regs(regs);
472 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
473 cur->comm, cur->pid, task_thread_info(cur), cur);
476 * When in-kernel, we also print out the stack and code at the
477 * time of the fault..
479 if (!user_mode(regs)) {
480 unsigned char c;
481 printk("Stack: ");
482 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
483 printk("\n");
485 printk(KERN_EMERG "Code: ");
486 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
487 /* try starting at RIP */
488 ip = (u8 *) regs->ip;
489 code_len = code_len - code_prologue + 1;
491 for (i = 0; i < code_len; i++, ip++) {
492 if (ip < (u8 *)PAGE_OFFSET ||
493 probe_kernel_address(ip, c)) {
494 printk(" Bad RIP value.");
495 break;
497 if (ip == (u8 *)regs->ip)
498 printk("<%02x> ", c);
499 else
500 printk("%02x ", c);
503 printk("\n");
506 int is_valid_bugaddr(unsigned long ip)
508 unsigned short ud2;
510 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
511 return 0;
513 return ud2 == 0x0b0f;
516 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
517 static int die_owner = -1;
518 static unsigned int die_nest_count;
520 unsigned __kprobes long oops_begin(void)
522 int cpu;
523 unsigned long flags;
525 oops_enter();
527 /* racy, but better than risking deadlock. */
528 raw_local_irq_save(flags);
529 cpu = smp_processor_id();
530 if (!__raw_spin_trylock(&die_lock)) {
531 if (cpu == die_owner)
532 /* nested oops. should stop eventually */;
533 else
534 __raw_spin_lock(&die_lock);
536 die_nest_count++;
537 die_owner = cpu;
538 console_verbose();
539 bust_spinlocks(1);
540 return flags;
543 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
545 die_owner = -1;
546 bust_spinlocks(0);
547 die_nest_count--;
548 if (!die_nest_count)
549 /* Nest count reaches zero, release the lock. */
550 __raw_spin_unlock(&die_lock);
551 raw_local_irq_restore(flags);
552 if (!regs) {
553 oops_exit();
554 return;
556 if (panic_on_oops)
557 panic("Fatal exception");
558 oops_exit();
559 do_exit(signr);
562 int __kprobes __die(const char * str, struct pt_regs * regs, long err)
564 static int die_counter;
565 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
566 #ifdef CONFIG_PREEMPT
567 printk("PREEMPT ");
568 #endif
569 #ifdef CONFIG_SMP
570 printk("SMP ");
571 #endif
572 #ifdef CONFIG_DEBUG_PAGEALLOC
573 printk("DEBUG_PAGEALLOC");
574 #endif
575 printk("\n");
576 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
577 return 1;
578 show_registers(regs);
579 add_taint(TAINT_DIE);
580 /* Executive summary in case the oops scrolled away */
581 printk(KERN_ALERT "RIP ");
582 printk_address(regs->ip, 1);
583 printk(" RSP <%016lx>\n", regs->sp);
584 if (kexec_should_crash(current))
585 crash_kexec(regs);
586 return 0;
589 void die(const char * str, struct pt_regs * regs, long err)
591 unsigned long flags = oops_begin();
593 if (!user_mode(regs))
594 report_bug(regs->ip, regs);
596 if (__die(str, regs, err))
597 regs = NULL;
598 oops_end(flags, regs, SIGSEGV);
601 void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
603 unsigned long flags = oops_begin();
606 * We are in trouble anyway, lets at least try
607 * to get a message out.
609 printk(str, smp_processor_id());
610 show_registers(regs);
611 if (kexec_should_crash(current))
612 crash_kexec(regs);
613 if (do_panic || panic_on_oops)
614 panic("Non maskable interrupt");
615 oops_end(flags, NULL, SIGBUS);
616 nmi_exit();
617 local_irq_enable();
618 do_exit(SIGBUS);
621 static void __kprobes do_trap(int trapnr, int signr, char *str,
622 struct pt_regs * regs, long error_code,
623 siginfo_t *info)
625 struct task_struct *tsk = current;
627 if (user_mode(regs)) {
629 * We want error_code and trap_no set for userspace
630 * faults and kernelspace faults which result in
631 * die(), but not kernelspace faults which are fixed
632 * up. die() gives the process no chance to handle
633 * the signal and notice the kernel fault information,
634 * so that won't result in polluting the information
635 * about previously queued, but not yet delivered,
636 * faults. See also do_general_protection below.
638 tsk->thread.error_code = error_code;
639 tsk->thread.trap_no = trapnr;
641 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
642 printk_ratelimit()) {
643 printk(KERN_INFO
644 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
645 tsk->comm, tsk->pid, str,
646 regs->ip, regs->sp, error_code);
647 print_vma_addr(" in ", regs->ip);
648 printk("\n");
651 if (info)
652 force_sig_info(signr, info, tsk);
653 else
654 force_sig(signr, tsk);
655 return;
659 if (!fixup_exception(regs)) {
660 tsk->thread.error_code = error_code;
661 tsk->thread.trap_no = trapnr;
662 die(str, regs, error_code);
664 return;
667 #define DO_ERROR(trapnr, signr, str, name) \
668 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
670 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
671 == NOTIFY_STOP) \
672 return; \
673 conditional_sti(regs); \
674 do_trap(trapnr, signr, str, regs, error_code, NULL); \
677 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
678 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
680 siginfo_t info; \
681 info.si_signo = signr; \
682 info.si_errno = 0; \
683 info.si_code = sicode; \
684 info.si_addr = (void __user *)siaddr; \
685 trace_hardirqs_fixup(); \
686 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
687 == NOTIFY_STOP) \
688 return; \
689 conditional_sti(regs); \
690 do_trap(trapnr, signr, str, regs, error_code, &info); \
693 DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
694 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
695 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
696 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
697 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
698 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
699 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
700 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
701 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
702 DO_ERROR(18, SIGSEGV, "reserved", reserved)
704 /* Runs on IST stack */
705 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
707 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
708 12, SIGBUS) == NOTIFY_STOP)
709 return;
710 preempt_conditional_sti(regs);
711 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
712 preempt_conditional_cli(regs);
715 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
717 static const char str[] = "double fault";
718 struct task_struct *tsk = current;
720 /* Return not checked because double check cannot be ignored */
721 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
723 tsk->thread.error_code = error_code;
724 tsk->thread.trap_no = 8;
726 /* This is always a kernel trap and never fixable (and thus must
727 never return). */
728 for (;;)
729 die(str, regs, error_code);
732 asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
733 long error_code)
735 struct task_struct *tsk = current;
737 conditional_sti(regs);
739 if (user_mode(regs)) {
740 tsk->thread.error_code = error_code;
741 tsk->thread.trap_no = 13;
743 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
744 printk_ratelimit()) {
745 printk(KERN_INFO
746 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
747 tsk->comm, tsk->pid,
748 regs->ip, regs->sp, error_code);
749 print_vma_addr(" in ", regs->ip);
750 printk("\n");
753 force_sig(SIGSEGV, tsk);
754 return;
757 if (fixup_exception(regs))
758 return;
760 tsk->thread.error_code = error_code;
761 tsk->thread.trap_no = 13;
762 if (notify_die(DIE_GPF, "general protection fault", regs,
763 error_code, 13, SIGSEGV) == NOTIFY_STOP)
764 return;
765 die("general protection fault", regs, error_code);
768 static __kprobes void
769 mem_parity_error(unsigned char reason, struct pt_regs * regs)
771 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
772 reason);
773 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
775 #if defined(CONFIG_EDAC)
776 if(edac_handler_set()) {
777 edac_atomic_assert_error();
778 return;
780 #endif
782 if (panic_on_unrecovered_nmi)
783 panic("NMI: Not continuing");
785 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
787 /* Clear and disable the memory parity error line. */
788 reason = (reason & 0xf) | 4;
789 outb(reason, 0x61);
792 static __kprobes void
793 io_check_error(unsigned char reason, struct pt_regs * regs)
795 printk("NMI: IOCK error (debug interrupt?)\n");
796 show_registers(regs);
798 /* Re-enable the IOCK line, wait for a few seconds */
799 reason = (reason & 0xf) | 8;
800 outb(reason, 0x61);
801 mdelay(2000);
802 reason &= ~8;
803 outb(reason, 0x61);
806 static __kprobes void
807 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
809 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
810 reason);
811 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
813 if (panic_on_unrecovered_nmi)
814 panic("NMI: Not continuing");
816 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
819 /* Runs on IST stack. This code must keep interrupts off all the time.
820 Nested NMIs are prevented by the CPU. */
821 asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
823 unsigned char reason = 0;
824 int cpu;
826 cpu = smp_processor_id();
828 /* Only the BSP gets external NMIs from the system. */
829 if (!cpu)
830 reason = get_nmi_reason();
832 if (!(reason & 0xc0)) {
833 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
834 == NOTIFY_STOP)
835 return;
837 * Ok, so this is none of the documented NMI sources,
838 * so it must be the NMI watchdog.
840 if (nmi_watchdog_tick(regs,reason))
841 return;
842 if (!do_nmi_callback(regs,cpu))
843 unknown_nmi_error(reason, regs);
845 return;
847 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
848 return;
850 /* AK: following checks seem to be broken on modern chipsets. FIXME */
852 if (reason & 0x80)
853 mem_parity_error(reason, regs);
854 if (reason & 0x40)
855 io_check_error(reason, regs);
858 /* runs on IST stack. */
859 asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
861 trace_hardirqs_fixup();
863 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
864 return;
866 preempt_conditional_sti(regs);
867 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
868 preempt_conditional_cli(regs);
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)
876 struct pt_regs *regs = eregs;
877 /* Did already sync */
878 if (eregs == (struct pt_regs *)eregs->sp)
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;
892 /* runs on IST stack. */
893 asmlinkage void __kprobes do_debug(struct pt_regs * regs,
894 unsigned long error_code)
896 unsigned long condition;
897 struct task_struct *tsk = current;
898 siginfo_t info;
900 trace_hardirqs_fixup();
902 get_debugreg(condition, 6);
905 * The processor cleared BTF, so don't mark that we need it set.
907 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
908 tsk->thread.debugctlmsr = 0;
910 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
911 SIGTRAP) == NOTIFY_STOP)
912 return;
914 preempt_conditional_sti(regs);
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;
923 tsk->thread.debugreg6 = condition;
927 * Single-stepping through TF: make sure we ignore any events in
928 * kernel space (but re-enable TF when returning to user mode).
930 if (condition & DR_STEP) {
931 if (!user_mode(regs))
932 goto clear_TF_reenable;
935 /* Ok, finally something we can handle */
936 tsk->thread.trap_no = 1;
937 tsk->thread.error_code = error_code;
938 info.si_signo = SIGTRAP;
939 info.si_errno = 0;
940 info.si_code = TRAP_BRKPT;
941 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
942 force_sig_info(SIGTRAP, &info, tsk);
944 clear_dr7:
945 set_debugreg(0UL, 7);
946 preempt_conditional_cli(regs);
947 return;
949 clear_TF_reenable:
950 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
951 regs->flags &= ~X86_EFLAGS_TF;
952 preempt_conditional_cli(regs);
955 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
957 if (fixup_exception(regs))
958 return 1;
960 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
961 /* Illegal floating point operation in the kernel */
962 current->thread.trap_no = trapnr;
963 die(str, regs, 0);
964 return 0;
968 * Note that we play around with the 'TS' bit in an attempt to get
969 * the correct behaviour even in the presence of the asynchronous
970 * IRQ13 behaviour
972 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
974 void __user *ip = (void __user *)(regs->ip);
975 struct task_struct * task;
976 siginfo_t info;
977 unsigned short cwd, swd;
979 conditional_sti(regs);
980 if (!user_mode(regs) &&
981 kernel_math_error(regs, "kernel x87 math error", 16))
982 return;
985 * Save the info for the exception handler and clear the error.
987 task = current;
988 save_init_fpu(task);
989 task->thread.trap_no = 16;
990 task->thread.error_code = 0;
991 info.si_signo = SIGFPE;
992 info.si_errno = 0;
993 info.si_code = __SI_FAULT;
994 info.si_addr = ip;
996 * (~cwd & swd) will mask out exceptions that are not set to unmasked
997 * status. 0x3f is the exception bits in these regs, 0x200 is the
998 * C1 reg you need in case of a stack fault, 0x040 is the stack
999 * fault bit. We should only be taking one exception at a time,
1000 * so if this combination doesn't produce any single exception,
1001 * then we have a bad program that isn't synchronizing its FPU usage
1002 * and it will suffer the consequences since we won't be able to
1003 * fully reproduce the context of the exception
1005 cwd = get_fpu_cwd(task);
1006 swd = get_fpu_swd(task);
1007 switch (swd & ~cwd & 0x3f) {
1008 case 0x000:
1009 default:
1010 break;
1011 case 0x001: /* Invalid Op */
1013 * swd & 0x240 == 0x040: Stack Underflow
1014 * swd & 0x240 == 0x240: Stack Overflow
1015 * User must clear the SF bit (0x40) if set
1017 info.si_code = FPE_FLTINV;
1018 break;
1019 case 0x002: /* Denormalize */
1020 case 0x010: /* Underflow */
1021 info.si_code = FPE_FLTUND;
1022 break;
1023 case 0x004: /* Zero Divide */
1024 info.si_code = FPE_FLTDIV;
1025 break;
1026 case 0x008: /* Overflow */
1027 info.si_code = FPE_FLTOVF;
1028 break;
1029 case 0x020: /* Precision */
1030 info.si_code = FPE_FLTRES;
1031 break;
1033 force_sig_info(SIGFPE, &info, task);
1036 asmlinkage void bad_intr(void)
1038 printk("bad interrupt");
1041 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1043 void __user *ip = (void __user *)(regs->ip);
1044 struct task_struct * task;
1045 siginfo_t info;
1046 unsigned short mxcsr;
1048 conditional_sti(regs);
1049 if (!user_mode(regs) &&
1050 kernel_math_error(regs, "kernel simd math error", 19))
1051 return;
1054 * Save the info for the exception handler and clear the error.
1056 task = current;
1057 save_init_fpu(task);
1058 task->thread.trap_no = 19;
1059 task->thread.error_code = 0;
1060 info.si_signo = SIGFPE;
1061 info.si_errno = 0;
1062 info.si_code = __SI_FAULT;
1063 info.si_addr = ip;
1065 * The SIMD FPU exceptions are handled a little differently, as there
1066 * is only a single status/control register. Thus, to determine which
1067 * unmasked exception was caught we must mask the exception mask bits
1068 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1070 mxcsr = get_fpu_mxcsr(task);
1071 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1072 case 0x000:
1073 default:
1074 break;
1075 case 0x001: /* Invalid Op */
1076 info.si_code = FPE_FLTINV;
1077 break;
1078 case 0x002: /* Denormalize */
1079 case 0x010: /* Underflow */
1080 info.si_code = FPE_FLTUND;
1081 break;
1082 case 0x004: /* Zero Divide */
1083 info.si_code = FPE_FLTDIV;
1084 break;
1085 case 0x008: /* Overflow */
1086 info.si_code = FPE_FLTOVF;
1087 break;
1088 case 0x020: /* Precision */
1089 info.si_code = FPE_FLTRES;
1090 break;
1092 force_sig_info(SIGFPE, &info, task);
1095 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1099 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1103 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1108 * 'math_state_restore()' saves the current math information in the
1109 * old math state array, and gets the new ones from the current task
1111 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1112 * Don't touch unless you *really* know how it works.
1114 asmlinkage void math_state_restore(void)
1116 struct task_struct *me = current;
1117 clts(); /* Allow maths ops (or we recurse) */
1119 if (!used_math())
1120 init_fpu(me);
1121 restore_fpu_checking(&me->thread.i387.fxsave);
1122 task_thread_info(me)->status |= TS_USEDFPU;
1123 me->fpu_counter++;
1125 EXPORT_SYMBOL_GPL(math_state_restore);
1127 void __init trap_init(void)
1129 set_intr_gate(0,&divide_error);
1130 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1131 set_intr_gate_ist(2,&nmi,NMI_STACK);
1132 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
1133 set_system_gate(4,&overflow); /* int4 can be called from all */
1134 set_intr_gate(5,&bounds);
1135 set_intr_gate(6,&invalid_op);
1136 set_intr_gate(7,&device_not_available);
1137 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1138 set_intr_gate(9,&coprocessor_segment_overrun);
1139 set_intr_gate(10,&invalid_TSS);
1140 set_intr_gate(11,&segment_not_present);
1141 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1142 set_intr_gate(13,&general_protection);
1143 set_intr_gate(14,&page_fault);
1144 set_intr_gate(15,&spurious_interrupt_bug);
1145 set_intr_gate(16,&coprocessor_error);
1146 set_intr_gate(17,&alignment_check);
1147 #ifdef CONFIG_X86_MCE
1148 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1149 #endif
1150 set_intr_gate(19,&simd_coprocessor_error);
1152 #ifdef CONFIG_IA32_EMULATION
1153 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1154 #endif
1157 * Should be a barrier for any external CPU state.
1159 cpu_init();
1163 static int __init oops_setup(char *s)
1165 if (!s)
1166 return -EINVAL;
1167 if (!strcmp(s, "panic"))
1168 panic_on_oops = 1;
1169 return 0;
1171 early_param("oops", oops_setup);
1173 static int __init kstack_setup(char *s)
1175 if (!s)
1176 return -EINVAL;
1177 kstack_depth_to_print = simple_strtoul(s,NULL,0);
1178 return 0;
1180 early_param("kstack", kstack_setup);
1183 static int __init code_bytes_setup(char *s)
1185 code_bytes = simple_strtoul(s, NULL, 0);
1186 if (code_bytes > 8192)
1187 code_bytes = 8192;
1189 return 1;
1191 __setup("code_bytes=", code_bytes_setup);