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/moduleparam.h>
14 #include <linux/interrupt.h>
15 #include <linux/kallsyms.h>
16 #include <linux/spinlock.h>
17 #include <linux/kprobes.h>
18 #include <linux/uaccess.h>
19 #include <linux/utsname.h>
20 #include <linux/kdebug.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/ptrace.h>
24 #include <linux/string.h>
25 #include <linux/unwind.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/kexec.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/init.h>
32 #include <linux/bug.h>
33 #include <linux/nmi.h>
36 #if defined(CONFIG_EDAC)
37 #include <linux/edac.h>
40 #include <asm/stacktrace.h>
41 #include <asm/processor.h>
42 #include <asm/debugreg.h>
43 #include <asm/atomic.h>
44 #include <asm/system.h>
45 #include <asm/unwind.h>
51 #include <asm/pgalloc.h>
52 #include <asm/proto.h>
55 #include <mach_traps.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 alignment_check(void);
75 asmlinkage void spurious_interrupt_bug(void);
76 asmlinkage void machine_check(void);
78 int panic_on_unrecovered_nmi;
79 int kstack_depth_to_print = 12;
80 static unsigned int code_bytes = 64;
81 static int ignore_nmis;
82 static int die_counter;
84 static inline void conditional_sti(struct pt_regs *regs)
86 if (regs->flags & X86_EFLAGS_IF)
90 static inline void preempt_conditional_sti(struct pt_regs *regs)
93 if (regs->flags & X86_EFLAGS_IF)
97 static inline void preempt_conditional_cli(struct pt_regs *regs)
99 if (regs->flags & X86_EFLAGS_IF)
101 /* Make sure to not schedule here because we could be running
102 on an exception stack. */
106 void printk_address(unsigned long address, int reliable)
108 #ifdef CONFIG_KALLSYMS
109 unsigned long offset = 0, symsize;
113 char namebuf[KSYM_NAME_LEN];
116 symname = kallsyms_lookup(address, &symsize, &offset,
119 printk(" [<%016lx>]\n", address);
123 strcpy(reliab, "? ");
126 modname = delim = "";
127 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
128 address, reliab, delim, modname, delim, symname, offset, symsize);
130 printk(" [<%016lx>]\n", address);
134 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
135 unsigned *usedp, char **idp)
137 static char ids[][8] = {
138 [DEBUG_STACK - 1] = "#DB",
139 [NMI_STACK - 1] = "NMI",
140 [DOUBLEFAULT_STACK - 1] = "#DF",
141 [STACKFAULT_STACK - 1] = "#SS",
142 [MCE_STACK - 1] = "#MC",
143 #if DEBUG_STKSZ > EXCEPTION_STKSZ
144 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
150 * Iterate over all exception stacks, and figure out whether
151 * 'stack' is in one of them:
153 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
154 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
156 * Is 'stack' above this exception frame's end?
157 * If yes then skip to the next frame.
162 * Is 'stack' above this exception frame's start address?
163 * If yes then we found the right frame.
165 if (stack >= end - EXCEPTION_STKSZ) {
167 * Make sure we only iterate through an exception
168 * stack once. If it comes up for the second time
169 * then there's something wrong going on - just
170 * break out and return NULL:
172 if (*usedp & (1U << k))
176 return (unsigned long *)end;
179 * If this is a debug stack, and if it has a larger size than
180 * the usual exception stacks, then 'stack' might still
181 * be within the lower portion of the debug stack:
183 #if DEBUG_STKSZ > EXCEPTION_STKSZ
184 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
185 unsigned j = N_EXCEPTION_STACKS - 1;
188 * Black magic. A large debug stack is composed of
189 * multiple exception stack entries, which we
190 * iterate through now. Dont look:
194 end -= EXCEPTION_STKSZ;
195 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
196 } while (stack < end - EXCEPTION_STKSZ);
197 if (*usedp & (1U << j))
201 return (unsigned long *)end;
209 * x86-64 can have up to three kernel stacks:
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)
220 if (p < end && p >= (end-THREAD_SIZE))
225 return p > t && p < t + THREAD_SIZE - size;
228 /* The form of the top of the frame on the stack */
230 struct stack_frame *next_frame;
231 unsigned long return_address;
234 static inline unsigned long
235 print_context_stack(struct thread_info *tinfo,
236 unsigned long *stack, unsigned long bp,
237 const struct stacktrace_ops *ops, void *data,
240 struct stack_frame *frame = (struct stack_frame *)bp;
242 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
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;
252 ops->address(data, addr, bp == 0);
260 void dump_trace(struct task_struct *task, struct pt_regs *regs,
261 unsigned long *stack, unsigned long bp,
262 const struct stacktrace_ops *ops, void *data)
264 const unsigned cpu = get_cpu();
265 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
267 struct thread_info *tinfo;
275 if (task && task != current)
276 stack = (unsigned long *)task->thread.sp;
279 #ifdef CONFIG_FRAME_POINTER
281 if (task == current) {
282 /* Grab bp right from our regs */
283 asm("movq %%rbp, %0" : "=r" (bp) :);
285 /* bp is the last reg pushed by switch_to */
286 bp = *(unsigned long *) task->thread.sp;
292 * Print function call entries in all stacks, starting at the
293 * current stack address. If the stacks consist of nested
296 tinfo = task_thread_info(task);
299 unsigned long *estack_end;
300 estack_end = in_exception_stack(cpu, (unsigned long)stack,
304 if (ops->stack(data, id) < 0)
307 bp = print_context_stack(tinfo, stack, bp, ops,
309 ops->stack(data, "<EOE>");
311 * We link to the next stack via the
312 * second-to-last pointer (index -2 to end) in the
315 stack = (unsigned long *) estack_end[-2];
319 unsigned long *irqstack;
320 irqstack = irqstack_end -
321 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
323 if (stack >= irqstack && stack < irqstack_end) {
324 if (ops->stack(data, "IRQ") < 0)
326 bp = print_context_stack(tinfo, stack, bp,
327 ops, data, irqstack_end);
329 * We link to the next stack (which would be
330 * the process stack normally) the last
331 * pointer (index -1 to end) in the IRQ stack:
333 stack = (unsigned long *) (irqstack_end[-1]);
335 ops->stack(data, "EOI");
343 * This handles the process stack:
345 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
348 EXPORT_SYMBOL(dump_trace);
351 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
353 print_symbol(msg, symbol);
357 static void print_trace_warning(void *data, char *msg)
362 static int print_trace_stack(void *data, char *name)
364 printk(" <%s> ", name);
368 static void print_trace_address(void *data, unsigned long addr, int reliable)
370 touch_nmi_watchdog();
371 printk_address(addr, reliable);
374 static const struct stacktrace_ops print_trace_ops = {
375 .warning = print_trace_warning,
376 .warning_symbol = print_trace_warning_symbol,
377 .stack = print_trace_stack,
378 .address = print_trace_address,
381 void show_trace(struct task_struct *task, struct pt_regs *regs,
382 unsigned long *stack, unsigned long bp)
384 printk("\nCall Trace:\n");
385 dump_trace(task, regs, stack, bp, &print_trace_ops, NULL);
390 _show_stack(struct task_struct *task, struct pt_regs *regs,
391 unsigned long *sp, unsigned long bp)
393 unsigned long *stack;
395 const int cpu = smp_processor_id();
396 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
397 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
399 // debugging aid: "show_stack(NULL, NULL);" prints the
400 // back trace for this cpu.
404 sp = (unsigned long *)task->thread.sp;
406 sp = (unsigned long *)&sp;
410 for (i = 0; i < kstack_depth_to_print; i++) {
411 if (stack >= irqstack && stack <= irqstack_end) {
412 if (stack == irqstack_end) {
413 stack = (unsigned long *) (irqstack_end[-1]);
417 if (((long) stack & (THREAD_SIZE-1)) == 0)
420 if (i && ((i % 4) == 0))
422 printk(" %016lx", *stack++);
423 touch_nmi_watchdog();
425 show_trace(task, regs, sp, bp);
428 void show_stack(struct task_struct *task, unsigned long *sp)
430 _show_stack(task, NULL, sp, 0);
434 * The architecture-independent dump_stack generator
436 void dump_stack(void)
438 unsigned long bp = 0;
441 #ifdef CONFIG_FRAME_POINTER
443 asm("movq %%rbp, %0" : "=r" (bp):);
446 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
447 current->pid, current->comm, print_tainted(),
448 init_utsname()->release,
449 (int)strcspn(init_utsname()->version, " "),
450 init_utsname()->version);
451 show_trace(NULL, NULL, &stack, bp);
454 EXPORT_SYMBOL(dump_stack);
456 void show_registers(struct pt_regs *regs)
460 const int cpu = smp_processor_id();
461 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
464 printk("CPU %d ", cpu);
466 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
467 cur->comm, cur->pid, task_thread_info(cur), cur);
470 * When in-kernel, we also print out the stack and code at the
471 * time of the fault..
473 if (!user_mode(regs)) {
474 unsigned int code_prologue = code_bytes * 43 / 64;
475 unsigned int code_len = code_bytes;
480 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
483 printk(KERN_EMERG "Code: ");
485 ip = (u8 *)regs->ip - code_prologue;
486 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
487 /* try starting at RIP */
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.");
497 if (ip == (u8 *)regs->ip)
498 printk("<%02x> ", c);
506 int is_valid_bugaddr(unsigned long ip)
510 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
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)
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 */;
534 __raw_spin_lock(&die_lock);
543 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
549 /* Nest count reaches zero, release the lock. */
550 __raw_spin_unlock(&die_lock);
551 raw_local_irq_restore(flags);
557 panic("Fatal exception");
562 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
564 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
565 #ifdef CONFIG_PREEMPT
571 #ifdef CONFIG_DEBUG_PAGEALLOC
572 printk("DEBUG_PAGEALLOC");
575 if (notify_die(DIE_OOPS, str, regs, err,
576 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
579 show_registers(regs);
580 add_taint(TAINT_DIE);
581 /* Executive summary in case the oops scrolled away */
582 printk(KERN_ALERT "RIP ");
583 printk_address(regs->ip, 1);
584 printk(" RSP <%016lx>\n", regs->sp);
585 if (kexec_should_crash(current))
590 void die(const char *str, struct pt_regs *regs, long err)
592 unsigned long flags = oops_begin();
594 if (!user_mode(regs))
595 report_bug(regs->ip, regs);
597 if (__die(str, regs, err))
599 oops_end(flags, regs, SIGSEGV);
602 notrace __kprobes void
603 die_nmi(char *str, struct pt_regs *regs, int do_panic)
607 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
610 flags = oops_begin();
612 * We are in trouble anyway, lets at least try
613 * to get a message out.
615 printk(KERN_EMERG "%s", str);
616 printk(" on CPU%d, ip %08lx, registers:\n",
617 smp_processor_id(), regs->ip);
618 show_registers(regs);
619 if (kexec_should_crash(current))
621 if (do_panic || panic_on_oops)
622 panic("Non maskable interrupt");
623 oops_end(flags, NULL, SIGBUS);
629 static void __kprobes
630 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
631 long error_code, siginfo_t *info)
633 struct task_struct *tsk = current;
635 if (!user_mode(regs))
639 * We want error_code and trap_no set for userspace faults and
640 * kernelspace faults which result in die(), but not
641 * kernelspace faults which are fixed up. die() gives the
642 * process no chance to handle the signal and notice the
643 * kernel fault information, so that won't result in polluting
644 * the information about previously queued, but not yet
645 * delivered, faults. See also do_general_protection below.
647 tsk->thread.error_code = error_code;
648 tsk->thread.trap_no = trapnr;
650 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
651 printk_ratelimit()) {
653 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
654 tsk->comm, tsk->pid, str,
655 regs->ip, regs->sp, error_code);
656 print_vma_addr(" in ", regs->ip);
661 force_sig_info(signr, info, tsk);
663 force_sig(signr, tsk);
667 if (!fixup_exception(regs)) {
668 tsk->thread.error_code = error_code;
669 tsk->thread.trap_no = trapnr;
670 die(str, regs, error_code);
675 #define DO_ERROR(trapnr, signr, str, name) \
676 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
678 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
681 conditional_sti(regs); \
682 do_trap(trapnr, signr, str, regs, error_code, NULL); \
685 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
686 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
689 info.si_signo = signr; \
691 info.si_code = sicode; \
692 info.si_addr = (void __user *)siaddr; \
693 trace_hardirqs_fixup(); \
694 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
697 conditional_sti(regs); \
698 do_trap(trapnr, signr, str, regs, error_code, &info); \
701 DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
702 DO_ERROR(4, SIGSEGV, "overflow", overflow)
703 DO_ERROR(5, SIGSEGV, "bounds", bounds)
704 DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
705 DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
706 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
707 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
708 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
710 /* Runs on IST stack */
711 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
713 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
714 12, SIGBUS) == NOTIFY_STOP)
716 preempt_conditional_sti(regs);
717 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
718 preempt_conditional_cli(regs);
721 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
723 static const char str[] = "double fault";
724 struct task_struct *tsk = current;
726 /* Return not checked because double check cannot be ignored */
727 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
729 tsk->thread.error_code = error_code;
730 tsk->thread.trap_no = 8;
732 /* This is always a kernel trap and never fixable (and thus must
735 die(str, regs, error_code);
738 asmlinkage void __kprobes
739 do_general_protection(struct pt_regs *regs, long error_code)
741 struct task_struct *tsk;
743 conditional_sti(regs);
746 if (!user_mode(regs))
749 tsk->thread.error_code = error_code;
750 tsk->thread.trap_no = 13;
752 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
753 printk_ratelimit()) {
755 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
757 regs->ip, regs->sp, error_code);
758 print_vma_addr(" in ", regs->ip);
762 force_sig(SIGSEGV, tsk);
766 if (fixup_exception(regs))
769 tsk->thread.error_code = error_code;
770 tsk->thread.trap_no = 13;
771 if (notify_die(DIE_GPF, "general protection fault", regs,
772 error_code, 13, SIGSEGV) == NOTIFY_STOP)
774 die("general protection fault", regs, error_code);
777 static notrace __kprobes void
778 mem_parity_error(unsigned char reason, struct pt_regs *regs)
780 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
782 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
784 #if defined(CONFIG_EDAC)
785 if (edac_handler_set()) {
786 edac_atomic_assert_error();
791 if (panic_on_unrecovered_nmi)
792 panic("NMI: Not continuing");
794 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
796 /* Clear and disable the memory parity error line. */
797 reason = (reason & 0xf) | 4;
801 static notrace __kprobes void
802 io_check_error(unsigned char reason, struct pt_regs *regs)
804 printk("NMI: IOCK error (debug interrupt?)\n");
805 show_registers(regs);
807 /* Re-enable the IOCK line, wait for a few seconds */
808 reason = (reason & 0xf) | 8;
815 static notrace __kprobes void
816 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
818 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
820 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
822 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
824 if (panic_on_unrecovered_nmi)
825 panic("NMI: Not continuing");
827 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
830 /* Runs on IST stack. This code must keep interrupts off all the time.
831 Nested NMIs are prevented by the CPU. */
832 asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
834 unsigned char reason = 0;
837 cpu = smp_processor_id();
839 /* Only the BSP gets external NMIs from the system. */
841 reason = get_nmi_reason();
843 if (!(reason & 0xc0)) {
844 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
848 * Ok, so this is none of the documented NMI sources,
849 * so it must be the NMI watchdog.
851 if (nmi_watchdog_tick(regs, reason))
853 if (!do_nmi_callback(regs, cpu))
854 unknown_nmi_error(reason, regs);
858 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
861 /* AK: following checks seem to be broken on modern chipsets. FIXME */
863 mem_parity_error(reason, regs);
865 io_check_error(reason, regs);
868 asmlinkage notrace __kprobes void
869 do_nmi(struct pt_regs *regs, long error_code)
873 add_pda(__nmi_count, 1);
876 default_do_nmi(regs);
887 void restart_nmi(void)
893 /* runs on IST stack. */
894 asmlinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
896 trace_hardirqs_fixup();
898 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
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 struct task_struct *tsk = current;
933 unsigned long condition;
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)
958 tsk->thread.debugreg6 = condition;
961 * Single-stepping through TF: make sure we ignore any events in
962 * kernel space (but re-enable TF when returning to user mode).
964 if (condition & DR_STEP) {
965 if (!user_mode(regs))
966 goto clear_TF_reenable;
969 /* Ok, finally something we can handle */
970 tsk->thread.trap_no = 1;
971 tsk->thread.error_code = error_code;
972 info.si_signo = SIGTRAP;
974 info.si_code = TRAP_BRKPT;
975 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
976 force_sig_info(SIGTRAP, &info, tsk);
980 preempt_conditional_cli(regs);
984 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
985 regs->flags &= ~X86_EFLAGS_TF;
986 preempt_conditional_cli(regs);
990 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
992 if (fixup_exception(regs))
995 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
996 /* Illegal floating point operation in the kernel */
997 current->thread.trap_no = trapnr;
1003 * Note that we play around with the 'TS' bit in an attempt to get
1004 * the correct behaviour even in the presence of the asynchronous
1007 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
1009 void __user *ip = (void __user *)(regs->ip);
1010 struct task_struct *task;
1012 unsigned short cwd, swd;
1014 conditional_sti(regs);
1015 if (!user_mode(regs) &&
1016 kernel_math_error(regs, "kernel x87 math error", 16))
1020 * Save the info for the exception handler and clear the error.
1023 save_init_fpu(task);
1024 task->thread.trap_no = 16;
1025 task->thread.error_code = 0;
1026 info.si_signo = SIGFPE;
1028 info.si_code = __SI_FAULT;
1031 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1032 * status. 0x3f is the exception bits in these regs, 0x200 is the
1033 * C1 reg you need in case of a stack fault, 0x040 is the stack
1034 * fault bit. We should only be taking one exception at a time,
1035 * so if this combination doesn't produce any single exception,
1036 * then we have a bad program that isn't synchronizing its FPU usage
1037 * and it will suffer the consequences since we won't be able to
1038 * fully reproduce the context of the exception
1040 cwd = get_fpu_cwd(task);
1041 swd = get_fpu_swd(task);
1042 switch (swd & ~cwd & 0x3f) {
1043 case 0x000: /* No unmasked exception */
1044 default: /* Multiple exceptions */
1046 case 0x001: /* Invalid Op */
1048 * swd & 0x240 == 0x040: Stack Underflow
1049 * swd & 0x240 == 0x240: Stack Overflow
1050 * User must clear the SF bit (0x40) if set
1052 info.si_code = FPE_FLTINV;
1054 case 0x002: /* Denormalize */
1055 case 0x010: /* Underflow */
1056 info.si_code = FPE_FLTUND;
1058 case 0x004: /* Zero Divide */
1059 info.si_code = FPE_FLTDIV;
1061 case 0x008: /* Overflow */
1062 info.si_code = FPE_FLTOVF;
1064 case 0x020: /* Precision */
1065 info.si_code = FPE_FLTRES;
1068 force_sig_info(SIGFPE, &info, task);
1071 asmlinkage void bad_intr(void)
1073 printk("bad interrupt");
1076 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1078 void __user *ip = (void __user *)(regs->ip);
1079 struct task_struct *task;
1081 unsigned short mxcsr;
1083 conditional_sti(regs);
1084 if (!user_mode(regs) &&
1085 kernel_math_error(regs, "kernel simd math error", 19))
1089 * Save the info for the exception handler and clear the error.
1092 save_init_fpu(task);
1093 task->thread.trap_no = 19;
1094 task->thread.error_code = 0;
1095 info.si_signo = SIGFPE;
1097 info.si_code = __SI_FAULT;
1100 * The SIMD FPU exceptions are handled a little differently, as there
1101 * is only a single status/control register. Thus, to determine which
1102 * unmasked exception was caught we must mask the exception mask bits
1103 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1105 mxcsr = get_fpu_mxcsr(task);
1106 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1110 case 0x001: /* Invalid Op */
1111 info.si_code = FPE_FLTINV;
1113 case 0x002: /* Denormalize */
1114 case 0x010: /* Underflow */
1115 info.si_code = FPE_FLTUND;
1117 case 0x004: /* Zero Divide */
1118 info.si_code = FPE_FLTDIV;
1120 case 0x008: /* Overflow */
1121 info.si_code = FPE_FLTOVF;
1123 case 0x020: /* Precision */
1124 info.si_code = FPE_FLTRES;
1127 force_sig_info(SIGFPE, &info, task);
1130 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1134 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1138 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1143 * 'math_state_restore()' saves the current math information in the
1144 * old math state array, and gets the new ones from the current task
1146 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1147 * Don't touch unless you *really* know how it works.
1149 asmlinkage void math_state_restore(void)
1151 struct task_struct *me = current;
1156 * does a slab alloc which can sleep
1160 * ran out of memory!
1162 do_group_exit(SIGKILL);
1165 local_irq_disable();
1168 clts(); /* Allow maths ops (or we recurse) */
1169 restore_fpu_checking(&me->thread.xstate->fxsave);
1170 task_thread_info(me)->status |= TS_USEDFPU;
1173 EXPORT_SYMBOL_GPL(math_state_restore);
1175 void __init trap_init(void)
1177 set_intr_gate(0, ÷_error);
1178 set_intr_gate_ist(1, &debug, DEBUG_STACK);
1179 set_intr_gate_ist(2, &nmi, NMI_STACK);
1180 set_system_gate_ist(3, &int3, DEBUG_STACK); /* int3 can be called from all */
1181 set_system_gate(4, &overflow); /* int4 can be called from all */
1182 set_intr_gate(5, &bounds);
1183 set_intr_gate(6, &invalid_op);
1184 set_intr_gate(7, &device_not_available);
1185 set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
1186 set_intr_gate(9, &coprocessor_segment_overrun);
1187 set_intr_gate(10, &invalid_TSS);
1188 set_intr_gate(11, &segment_not_present);
1189 set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
1190 set_intr_gate(13, &general_protection);
1191 set_intr_gate(14, &page_fault);
1192 set_intr_gate(15, &spurious_interrupt_bug);
1193 set_intr_gate(16, &coprocessor_error);
1194 set_intr_gate(17, &alignment_check);
1195 #ifdef CONFIG_X86_MCE
1196 set_intr_gate_ist(18, &machine_check, MCE_STACK);
1198 set_intr_gate(19, &simd_coprocessor_error);
1200 #ifdef CONFIG_IA32_EMULATION
1201 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1204 * initialize the per thread extended state:
1206 init_thread_xstate();
1208 * Should be a barrier for any external CPU state:
1213 static int __init oops_setup(char *s)
1217 if (!strcmp(s, "panic"))
1221 early_param("oops", oops_setup);
1223 static int __init kstack_setup(char *s)
1227 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
1230 early_param("kstack", kstack_setup);
1232 static int __init code_bytes_setup(char *s)
1234 code_bytes = simple_strtoul(s, NULL, 0);
1235 if (code_bytes > 8192)
1240 __setup("code_bytes=", code_bytes_setup);