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
10 * 'Traps.c' handles hardware traps and faults after we have saved some
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>
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 #include <mach_traps.h>
38 #if defined(CONFIG_EDAC)
39 #include <linux/edac.h>
42 #include <asm/system.h>
44 #include <asm/atomic.h>
45 #include <asm/debugreg.h>
48 #include <asm/processor.h>
49 #include <asm/unwind.h>
51 #include <asm/pgalloc.h>
53 #include <asm/proto.h>
55 #include <asm/stacktrace.h>
57 asmlinkage void divide_error(void);
58 asmlinkage void debug(void);
59 asmlinkage void nmi(void);
60 asmlinkage void int3(void);
61 asmlinkage void overflow(void);
62 asmlinkage void bounds(void);
63 asmlinkage void invalid_op(void);
64 asmlinkage void device_not_available(void);
65 asmlinkage void double_fault(void);
66 asmlinkage void coprocessor_segment_overrun(void);
67 asmlinkage void invalid_TSS(void);
68 asmlinkage void segment_not_present(void);
69 asmlinkage void stack_segment(void);
70 asmlinkage void general_protection(void);
71 asmlinkage void page_fault(void);
72 asmlinkage void coprocessor_error(void);
73 asmlinkage void simd_coprocessor_error(void);
74 asmlinkage void reserved(void);
75 asmlinkage void alignment_check(void);
76 asmlinkage void machine_check(void);
77 asmlinkage void spurious_interrupt_bug(void);
79 int panic_on_unrecovered_nmi;
80 static unsigned int code_bytes = 64;
81 static unsigned ignore_nmis;
83 static inline void conditional_sti(struct pt_regs *regs)
85 if (regs->flags & X86_EFLAGS_IF)
89 static inline void preempt_conditional_sti(struct pt_regs *regs)
92 if (regs->flags & X86_EFLAGS_IF)
96 static inline void preempt_conditional_cli(struct pt_regs *regs)
98 if (regs->flags & X86_EFLAGS_IF)
100 /* Make sure to not schedule here because we could be running
101 on an exception stack. */
105 int kstack_depth_to_print = 12;
107 void printk_address(unsigned long address, int reliable)
109 #ifdef CONFIG_KALLSYMS
110 unsigned long offset = 0, symsize;
114 char namebuf[KSYM_NAME_LEN];
117 symname = kallsyms_lookup(address, &symsize, &offset,
120 printk(" [<%016lx>]\n", address);
124 strcpy(reliab, "? ");
127 modname = delim = "";
128 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
129 address, reliab, delim, modname, delim, symname, offset, symsize);
131 printk(" [<%016lx>]\n", address);
135 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
136 unsigned *usedp, char **idp)
138 static char ids[][8] = {
139 [DEBUG_STACK - 1] = "#DB",
140 [NMI_STACK - 1] = "NMI",
141 [DOUBLEFAULT_STACK - 1] = "#DF",
142 [STACKFAULT_STACK - 1] = "#SS",
143 [MCE_STACK - 1] = "#MC",
144 #if DEBUG_STKSZ > EXCEPTION_STKSZ
145 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
151 * Iterate over all exception stacks, and figure out whether
152 * 'stack' is in one of them:
154 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
155 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
157 * Is 'stack' above this exception frame's end?
158 * If yes then skip to the next frame.
163 * Is 'stack' above this exception frame's start address?
164 * If yes then we found the right frame.
166 if (stack >= end - EXCEPTION_STKSZ) {
168 * Make sure we only iterate through an exception
169 * stack once. If it comes up for the second time
170 * then there's something wrong going on - just
171 * break out and return NULL:
173 if (*usedp & (1U << k))
177 return (unsigned long *)end;
180 * If this is a debug stack, and if it has a larger size than
181 * the usual exception stacks, then 'stack' might still
182 * be within the lower portion of the debug stack:
184 #if DEBUG_STKSZ > EXCEPTION_STKSZ
185 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
186 unsigned j = N_EXCEPTION_STACKS - 1;
189 * Black magic. A large debug stack is composed of
190 * multiple exception stack entries, which we
191 * iterate through now. Dont look:
195 end -= EXCEPTION_STKSZ;
196 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
197 } while (stack < end - EXCEPTION_STKSZ);
198 if (*usedp & (1U << j))
202 return (unsigned long *)end;
209 #define MSG(txt) ops->warning(data, txt)
212 * x86-64 can have up to three kernel stacks:
215 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
218 static inline int valid_stack_ptr(struct thread_info *tinfo,
219 void *p, unsigned int size, void *end)
223 if (p < end && p >= (end-THREAD_SIZE))
228 return p > t && p < t + THREAD_SIZE - size;
231 /* The form of the top of the frame on the stack */
233 struct stack_frame *next_frame;
234 unsigned long return_address;
238 static inline unsigned long print_context_stack(struct thread_info *tinfo,
239 unsigned long *stack, unsigned long bp,
240 const struct stacktrace_ops *ops, void *data,
243 struct stack_frame *frame = (struct stack_frame *)bp;
245 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
249 if (__kernel_text_address(addr)) {
250 if ((unsigned long) stack == bp + 8) {
251 ops->address(data, addr, 1);
252 frame = frame->next_frame;
253 bp = (unsigned long) frame;
255 ops->address(data, addr, bp == 0);
263 void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
264 unsigned long *stack, unsigned long bp,
265 const struct stacktrace_ops *ops, void *data)
267 const unsigned cpu = get_cpu();
268 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
270 struct thread_info *tinfo;
274 tinfo = task_thread_info(tsk);
279 if (tsk && tsk != current)
280 stack = (unsigned long *)tsk->thread.sp;
283 #ifdef CONFIG_FRAME_POINTER
285 if (tsk == current) {
286 /* Grab bp right from our regs */
287 asm("movq %%rbp, %0" : "=r" (bp):);
289 /* bp is the last reg pushed by switch_to */
290 bp = *(unsigned long *) tsk->thread.sp;
298 * Print function call entries in all stacks, starting at the
299 * current stack address. If the stacks consist of nested
304 unsigned long *estack_end;
305 estack_end = in_exception_stack(cpu, (unsigned long)stack,
309 if (ops->stack(data, id) < 0)
312 bp = print_context_stack(tinfo, stack, bp, ops,
314 ops->stack(data, "<EOE>");
316 * We link to the next stack via the
317 * second-to-last pointer (index -2 to end) in the
320 stack = (unsigned long *) estack_end[-2];
324 unsigned long *irqstack;
325 irqstack = irqstack_end -
326 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
328 if (stack >= irqstack && stack < irqstack_end) {
329 if (ops->stack(data, "IRQ") < 0)
331 bp = print_context_stack(tinfo, stack, bp,
332 ops, data, irqstack_end);
334 * We link to the next stack (which would be
335 * the process stack normally) the last
336 * pointer (index -1 to end) in the IRQ stack:
338 stack = (unsigned long *) (irqstack_end[-1]);
340 ops->stack(data, "EOI");
348 * This handles the process stack:
350 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
353 EXPORT_SYMBOL(dump_trace);
356 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
358 print_symbol(msg, symbol);
362 static void print_trace_warning(void *data, char *msg)
367 static int print_trace_stack(void *data, char *name)
369 printk(" <%s> ", name);
373 static void print_trace_address(void *data, unsigned long addr, int reliable)
375 touch_nmi_watchdog();
376 printk_address(addr, reliable);
379 static const struct stacktrace_ops print_trace_ops = {
380 .warning = print_trace_warning,
381 .warning_symbol = print_trace_warning_symbol,
382 .stack = print_trace_stack,
383 .address = print_trace_address,
387 show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
390 printk("\nCall Trace:\n");
391 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
396 _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
399 unsigned long *stack;
401 const int cpu = smp_processor_id();
402 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
403 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
405 // debugging aid: "show_stack(NULL, NULL);" prints the
406 // back trace for this cpu.
410 sp = (unsigned long *)tsk->thread.sp;
412 sp = (unsigned long *)&sp;
416 for(i=0; i < kstack_depth_to_print; i++) {
417 if (stack >= irqstack && stack <= irqstack_end) {
418 if (stack == irqstack_end) {
419 stack = (unsigned long *) (irqstack_end[-1]);
423 if (((long) stack & (THREAD_SIZE-1)) == 0)
426 if (i && ((i % 4) == 0))
428 printk(" %016lx", *stack++);
429 touch_nmi_watchdog();
431 show_trace(tsk, regs, sp, bp);
434 void show_stack(struct task_struct *tsk, unsigned long * sp)
436 _show_stack(tsk, NULL, sp, 0);
440 * The architecture-independent dump_stack generator
442 void dump_stack(void)
445 unsigned long bp = 0;
447 #ifdef CONFIG_FRAME_POINTER
449 asm("movq %%rbp, %0" : "=r" (bp):);
452 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
453 current->pid, current->comm, print_tainted(),
454 init_utsname()->release,
455 (int)strcspn(init_utsname()->version, " "),
456 init_utsname()->version);
457 show_trace(NULL, NULL, &dummy, bp);
460 EXPORT_SYMBOL(dump_stack);
462 void show_registers(struct pt_regs *regs)
466 const int cpu = smp_processor_id();
467 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
469 unsigned int code_prologue = code_bytes * 43 / 64;
470 unsigned int code_len = code_bytes;
473 ip = (u8 *) regs->ip - code_prologue;
474 printk("CPU %d ", cpu);
476 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
477 cur->comm, cur->pid, task_thread_info(cur), cur);
480 * When in-kernel, we also print out the stack and code at the
481 * time of the fault..
483 if (!user_mode(regs)) {
486 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
489 printk(KERN_EMERG "Code: ");
490 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
491 /* try starting at RIP */
492 ip = (u8 *) regs->ip;
493 code_len = code_len - code_prologue + 1;
495 for (i = 0; i < code_len; i++, ip++) {
496 if (ip < (u8 *)PAGE_OFFSET ||
497 probe_kernel_address(ip, c)) {
498 printk(" Bad RIP value.");
501 if (ip == (u8 *)regs->ip)
502 printk("<%02x> ", c);
510 int is_valid_bugaddr(unsigned long ip)
514 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
517 return ud2 == 0x0b0f;
520 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
521 static int die_owner = -1;
522 static unsigned int die_nest_count;
524 unsigned __kprobes long oops_begin(void)
531 /* racy, but better than risking deadlock. */
532 raw_local_irq_save(flags);
533 cpu = smp_processor_id();
534 if (!__raw_spin_trylock(&die_lock)) {
535 if (cpu == die_owner)
536 /* nested oops. should stop eventually */;
538 __raw_spin_lock(&die_lock);
547 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
553 /* Nest count reaches zero, release the lock. */
554 __raw_spin_unlock(&die_lock);
555 raw_local_irq_restore(flags);
561 panic("Fatal exception");
566 int __kprobes __die(const char * str, struct pt_regs * regs, long err)
568 static int die_counter;
569 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
570 #ifdef CONFIG_PREEMPT
576 #ifdef CONFIG_DEBUG_PAGEALLOC
577 printk("DEBUG_PAGEALLOC");
580 if (notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
582 show_registers(regs);
583 add_taint(TAINT_DIE);
584 /* Executive summary in case the oops scrolled away */
585 printk(KERN_ALERT "RIP ");
586 printk_address(regs->ip, 1);
587 printk(" RSP <%016lx>\n", regs->sp);
588 if (kexec_should_crash(current))
593 void die(const char * str, struct pt_regs * regs, long err)
595 unsigned long flags = oops_begin();
597 if (!user_mode(regs))
598 report_bug(regs->ip, regs);
600 if (__die(str, regs, err))
602 oops_end(flags, regs, SIGSEGV);
605 notrace __kprobes void
606 die_nmi(char *str, struct pt_regs *regs, int do_panic)
610 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) ==
614 flags = oops_begin();
616 * We are in trouble anyway, lets at least try
617 * to get a message out.
619 printk(KERN_EMERG "%s", str);
620 printk(" on CPU%d, ip %08lx, registers:\n",
621 smp_processor_id(), regs->ip);
622 show_registers(regs);
623 if (kexec_should_crash(current))
625 if (do_panic || panic_on_oops)
626 panic("Non maskable interrupt");
627 oops_end(flags, NULL, SIGBUS);
633 static void __kprobes do_trap(int trapnr, int signr, char *str,
634 struct pt_regs * regs, long error_code,
637 struct task_struct *tsk = current;
639 if (user_mode(regs)) {
641 * We want error_code and trap_no set for userspace
642 * faults and kernelspace faults which result in
643 * die(), but not kernelspace faults which are fixed
644 * up. die() gives the process no chance to handle
645 * the signal and notice the kernel fault information,
646 * so that won't result in polluting the information
647 * about previously queued, but not yet delivered,
648 * faults. See also do_general_protection below.
650 tsk->thread.error_code = error_code;
651 tsk->thread.trap_no = trapnr;
653 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
654 printk_ratelimit()) {
656 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
657 tsk->comm, tsk->pid, str,
658 regs->ip, regs->sp, error_code);
659 print_vma_addr(" in ", regs->ip);
664 force_sig_info(signr, info, tsk);
666 force_sig(signr, tsk);
671 if (!fixup_exception(regs)) {
672 tsk->thread.error_code = error_code;
673 tsk->thread.trap_no = trapnr;
674 die(str, regs, error_code);
679 #define DO_ERROR(trapnr, signr, str, name) \
680 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
682 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
685 conditional_sti(regs); \
686 do_trap(trapnr, signr, str, regs, error_code, NULL); \
689 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
690 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
693 info.si_signo = signr; \
695 info.si_code = sicode; \
696 info.si_addr = (void __user *)siaddr; \
697 trace_hardirqs_fixup(); \
698 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
701 conditional_sti(regs); \
702 do_trap(trapnr, signr, str, regs, error_code, &info); \
705 DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
706 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
707 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
708 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
709 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
710 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
711 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
712 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
713 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
714 DO_ERROR(18, SIGSEGV, "reserved", reserved)
716 /* Runs on IST stack */
717 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
719 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
720 12, SIGBUS) == NOTIFY_STOP)
722 preempt_conditional_sti(regs);
723 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
724 preempt_conditional_cli(regs);
727 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
729 static const char str[] = "double fault";
730 struct task_struct *tsk = current;
732 /* Return not checked because double check cannot be ignored */
733 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
735 tsk->thread.error_code = error_code;
736 tsk->thread.trap_no = 8;
738 /* This is always a kernel trap and never fixable (and thus must
741 die(str, regs, error_code);
744 asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
747 struct task_struct *tsk = current;
749 conditional_sti(regs);
751 if (user_mode(regs)) {
752 tsk->thread.error_code = error_code;
753 tsk->thread.trap_no = 13;
755 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
756 printk_ratelimit()) {
758 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
760 regs->ip, regs->sp, error_code);
761 print_vma_addr(" in ", regs->ip);
765 force_sig(SIGSEGV, tsk);
769 if (fixup_exception(regs))
772 tsk->thread.error_code = error_code;
773 tsk->thread.trap_no = 13;
774 if (notify_die(DIE_GPF, "general protection fault", regs,
775 error_code, 13, SIGSEGV) == NOTIFY_STOP)
777 die("general protection fault", regs, error_code);
780 static notrace __kprobes void
781 mem_parity_error(unsigned char reason, struct pt_regs * regs)
783 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
785 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
787 #if defined(CONFIG_EDAC)
788 if(edac_handler_set()) {
789 edac_atomic_assert_error();
794 if (panic_on_unrecovered_nmi)
795 panic("NMI: Not continuing");
797 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
799 /* Clear and disable the memory parity error line. */
800 reason = (reason & 0xf) | 4;
804 static notrace __kprobes void
805 io_check_error(unsigned char reason, struct pt_regs * regs)
807 printk("NMI: IOCK error (debug interrupt?)\n");
808 show_registers(regs);
810 /* Re-enable the IOCK line, wait for a few seconds */
811 reason = (reason & 0xf) | 8;
818 static notrace __kprobes void
819 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
821 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
823 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
825 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
827 if (panic_on_unrecovered_nmi)
828 panic("NMI: Not continuing");
830 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
833 /* Runs on IST stack. This code must keep interrupts off all the time.
834 Nested NMIs are prevented by the CPU. */
835 asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
837 unsigned char reason = 0;
840 cpu = smp_processor_id();
842 /* Only the BSP gets external NMIs from the system. */
844 reason = get_nmi_reason();
846 if (!(reason & 0xc0)) {
847 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
851 * Ok, so this is none of the documented NMI sources,
852 * so it must be the NMI watchdog.
854 if (nmi_watchdog_tick(regs,reason))
856 if (!do_nmi_callback(regs,cpu))
857 unknown_nmi_error(reason, regs);
861 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
864 /* AK: following checks seem to be broken on modern chipsets. FIXME */
867 mem_parity_error(reason, regs);
869 io_check_error(reason, regs);
872 asmlinkage notrace __kprobes void
873 do_nmi(struct pt_regs *regs, long error_code)
876 add_pda(__nmi_count, 1);
878 default_do_nmi(regs);
888 void restart_nmi(void)
894 /* runs on IST stack. */
895 asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
897 trace_hardirqs_fixup();
899 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
902 preempt_conditional_sti(regs);
903 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
904 preempt_conditional_cli(regs);
907 /* Help handler running on IST stack to switch back to user stack
908 for scheduling or signal handling. The actual stack switch is done in
910 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
912 struct pt_regs *regs = eregs;
913 /* Did already sync */
914 if (eregs == (struct pt_regs *)eregs->sp)
916 /* Exception from user space */
917 else if (user_mode(eregs))
918 regs = task_pt_regs(current);
919 /* Exception from kernel and interrupts are enabled. Move to
920 kernel process stack. */
921 else if (eregs->flags & X86_EFLAGS_IF)
922 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
928 /* runs on IST stack. */
929 asmlinkage void __kprobes do_debug(struct pt_regs * regs,
930 unsigned long error_code)
932 unsigned long condition;
933 struct task_struct *tsk = current;
936 trace_hardirqs_fixup();
938 get_debugreg(condition, 6);
941 * The processor cleared BTF, so don't mark that we need it set.
943 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
944 tsk->thread.debugctlmsr = 0;
946 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
947 SIGTRAP) == NOTIFY_STOP)
950 preempt_conditional_sti(regs);
952 /* Mask out spurious debug traps due to lazy DR7 setting */
953 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
954 if (!tsk->thread.debugreg7) {
959 tsk->thread.debugreg6 = condition;
963 * Single-stepping through TF: make sure we ignore any events in
964 * kernel space (but re-enable TF when returning to user mode).
966 if (condition & DR_STEP) {
967 if (!user_mode(regs))
968 goto clear_TF_reenable;
971 /* Ok, finally something we can handle */
972 tsk->thread.trap_no = 1;
973 tsk->thread.error_code = error_code;
974 info.si_signo = SIGTRAP;
976 info.si_code = TRAP_BRKPT;
977 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
978 force_sig_info(SIGTRAP, &info, tsk);
981 set_debugreg(0UL, 7);
982 preempt_conditional_cli(regs);
986 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
987 regs->flags &= ~X86_EFLAGS_TF;
988 preempt_conditional_cli(regs);
991 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
993 if (fixup_exception(regs))
996 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
997 /* Illegal floating point operation in the kernel */
998 current->thread.trap_no = trapnr;
1004 * Note that we play around with the 'TS' bit in an attempt to get
1005 * the correct behaviour even in the presence of the asynchronous
1008 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
1010 void __user *ip = (void __user *)(regs->ip);
1011 struct task_struct * task;
1013 unsigned short cwd, swd;
1015 conditional_sti(regs);
1016 if (!user_mode(regs) &&
1017 kernel_math_error(regs, "kernel x87 math error", 16))
1021 * Save the info for the exception handler and clear the error.
1024 save_init_fpu(task);
1025 task->thread.trap_no = 16;
1026 task->thread.error_code = 0;
1027 info.si_signo = SIGFPE;
1029 info.si_code = __SI_FAULT;
1032 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1033 * status. 0x3f is the exception bits in these regs, 0x200 is the
1034 * C1 reg you need in case of a stack fault, 0x040 is the stack
1035 * fault bit. We should only be taking one exception at a time,
1036 * so if this combination doesn't produce any single exception,
1037 * then we have a bad program that isn't synchronizing its FPU usage
1038 * and it will suffer the consequences since we won't be able to
1039 * fully reproduce the context of the exception
1041 cwd = get_fpu_cwd(task);
1042 swd = get_fpu_swd(task);
1043 switch (swd & ~cwd & 0x3f) {
1047 case 0x001: /* Invalid Op */
1049 * swd & 0x240 == 0x040: Stack Underflow
1050 * swd & 0x240 == 0x240: Stack Overflow
1051 * User must clear the SF bit (0x40) if set
1053 info.si_code = FPE_FLTINV;
1055 case 0x002: /* Denormalize */
1056 case 0x010: /* Underflow */
1057 info.si_code = FPE_FLTUND;
1059 case 0x004: /* Zero Divide */
1060 info.si_code = FPE_FLTDIV;
1062 case 0x008: /* Overflow */
1063 info.si_code = FPE_FLTOVF;
1065 case 0x020: /* Precision */
1066 info.si_code = FPE_FLTRES;
1069 force_sig_info(SIGFPE, &info, task);
1072 asmlinkage void bad_intr(void)
1074 printk("bad interrupt");
1077 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1079 void __user *ip = (void __user *)(regs->ip);
1080 struct task_struct * task;
1082 unsigned short mxcsr;
1084 conditional_sti(regs);
1085 if (!user_mode(regs) &&
1086 kernel_math_error(regs, "kernel simd math error", 19))
1090 * Save the info for the exception handler and clear the error.
1093 save_init_fpu(task);
1094 task->thread.trap_no = 19;
1095 task->thread.error_code = 0;
1096 info.si_signo = SIGFPE;
1098 info.si_code = __SI_FAULT;
1101 * The SIMD FPU exceptions are handled a little differently, as there
1102 * is only a single status/control register. Thus, to determine which
1103 * unmasked exception was caught we must mask the exception mask bits
1104 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1106 mxcsr = get_fpu_mxcsr(task);
1107 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1111 case 0x001: /* Invalid Op */
1112 info.si_code = FPE_FLTINV;
1114 case 0x002: /* Denormalize */
1115 case 0x010: /* Underflow */
1116 info.si_code = FPE_FLTUND;
1118 case 0x004: /* Zero Divide */
1119 info.si_code = FPE_FLTDIV;
1121 case 0x008: /* Overflow */
1122 info.si_code = FPE_FLTOVF;
1124 case 0x020: /* Precision */
1125 info.si_code = FPE_FLTRES;
1128 force_sig_info(SIGFPE, &info, task);
1131 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1135 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1139 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1144 * 'math_state_restore()' saves the current math information in the
1145 * old math state array, and gets the new ones from the current task
1147 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1148 * Don't touch unless you *really* know how it works.
1150 asmlinkage void math_state_restore(void)
1152 struct task_struct *me = current;
1157 * does a slab alloc which can sleep
1161 * ran out of memory!
1163 do_group_exit(SIGKILL);
1166 local_irq_disable();
1169 clts(); /* Allow maths ops (or we recurse) */
1170 restore_fpu_checking(&me->thread.xstate->fxsave);
1171 task_thread_info(me)->status |= TS_USEDFPU;
1174 EXPORT_SYMBOL_GPL(math_state_restore);
1176 void __init trap_init(void)
1178 set_intr_gate(0,÷_error);
1179 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1180 set_intr_gate_ist(2,&nmi,NMI_STACK);
1181 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
1182 set_system_gate(4,&overflow); /* int4 can be called from all */
1183 set_intr_gate(5,&bounds);
1184 set_intr_gate(6,&invalid_op);
1185 set_intr_gate(7,&device_not_available);
1186 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1187 set_intr_gate(9,&coprocessor_segment_overrun);
1188 set_intr_gate(10,&invalid_TSS);
1189 set_intr_gate(11,&segment_not_present);
1190 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1191 set_intr_gate(13,&general_protection);
1192 set_intr_gate(14,&page_fault);
1193 set_intr_gate(15,&spurious_interrupt_bug);
1194 set_intr_gate(16,&coprocessor_error);
1195 set_intr_gate(17,&alignment_check);
1196 #ifdef CONFIG_X86_MCE
1197 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1199 set_intr_gate(19,&simd_coprocessor_error);
1201 #ifdef CONFIG_IA32_EMULATION
1202 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1206 * initialize the per thread extended state:
1208 init_thread_xstate();
1210 * Should be a barrier for any external CPU state.
1216 static int __init oops_setup(char *s)
1220 if (!strcmp(s, "panic"))
1224 early_param("oops", oops_setup);
1226 static int __init kstack_setup(char *s)
1230 kstack_depth_to_print = simple_strtoul(s,NULL,0);
1233 early_param("kstack", kstack_setup);
1236 static int __init code_bytes_setup(char *s)
1238 code_bytes = simple_strtoul(s, NULL, 0);
1239 if (code_bytes > 8192)
1244 __setup("code_bytes=", code_bytes_setup);