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 alignment_check(void);
75 asmlinkage void machine_check(void);
76 asmlinkage void spurious_interrupt_bug(void);
78 static unsigned int code_bytes = 64;
80 static inline void conditional_sti(struct pt_regs *regs)
82 if (regs->flags & X86_EFLAGS_IF)
86 static inline void preempt_conditional_sti(struct pt_regs *regs)
89 if (regs->flags & X86_EFLAGS_IF)
93 static inline void preempt_conditional_cli(struct pt_regs *regs)
95 if (regs->flags & X86_EFLAGS_IF)
97 /* Make sure to not schedule here because we could be running
98 on an exception stack. */
102 int kstack_depth_to_print = 12;
104 void printk_address(unsigned long address, int reliable)
106 #ifdef CONFIG_KALLSYMS
107 unsigned long offset = 0, symsize;
111 char namebuf[KSYM_NAME_LEN];
114 symname = kallsyms_lookup(address, &symsize, &offset,
117 printk(" [<%016lx>]\n", address);
121 strcpy(reliab, "? ");
124 modname = delim = "";
125 printk(" [<%016lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
126 address, reliab, delim, modname, delim, symname, offset, symsize);
128 printk(" [<%016lx>]\n", address);
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[?]"
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.
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))
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:
192 end -= EXCEPTION_STKSZ;
193 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
194 } while (stack < end - EXCEPTION_STKSZ);
195 if (*usedp & (1U << j))
199 return (unsigned long *)end;
206 #define MSG(txt) ops->warning(data, txt)
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;
235 static inline unsigned long print_context_stack(struct thread_info *tinfo,
236 unsigned long *stack, unsigned long bp,
237 const struct stacktrace_ops *ops, void *data,
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 *tsk, 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;
271 tinfo = task_thread_info(tsk);
276 if (tsk && tsk != current)
277 stack = (unsigned long *)tsk->thread.sp;
280 #ifdef CONFIG_FRAME_POINTER
282 if (tsk == current) {
283 /* Grab bp right from our regs */
284 asm("movq %%rbp, %0" : "=r" (bp):);
286 /* bp is the last reg pushed by switch_to */
287 bp = *(unsigned long *) tsk->thread.sp;
295 * Print function call entries in all stacks, starting at the
296 * current stack address. If the stacks consist of nested
301 unsigned long *estack_end;
302 estack_end = in_exception_stack(cpu, (unsigned long)stack,
306 if (ops->stack(data, id) < 0)
309 bp = print_context_stack(tinfo, stack, bp, ops,
311 ops->stack(data, "<EOE>");
313 * We link to the next stack via the
314 * second-to-last pointer (index -2 to end) in the
317 stack = (unsigned long *) estack_end[-2];
321 unsigned long *irqstack;
322 irqstack = irqstack_end -
323 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
325 if (stack >= irqstack && stack < irqstack_end) {
326 if (ops->stack(data, "IRQ") < 0)
328 bp = print_context_stack(tinfo, stack, bp,
329 ops, data, irqstack_end);
331 * We link to the next stack (which would be
332 * the process stack normally) the last
333 * pointer (index -1 to end) in the IRQ stack:
335 stack = (unsigned long *) (irqstack_end[-1]);
337 ops->stack(data, "EOI");
345 * This handles the process stack:
347 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
350 EXPORT_SYMBOL(dump_trace);
353 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
355 print_symbol(msg, symbol);
359 static void print_trace_warning(void *data, char *msg)
364 static int print_trace_stack(void *data, char *name)
366 printk(" <%s> ", name);
370 static void print_trace_address(void *data, unsigned long addr, int reliable)
372 touch_nmi_watchdog();
373 printk_address(addr, reliable);
376 static const struct stacktrace_ops print_trace_ops = {
377 .warning = print_trace_warning,
378 .warning_symbol = print_trace_warning_symbol,
379 .stack = print_trace_stack,
380 .address = print_trace_address,
384 show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack,
387 printk("\nCall Trace:\n");
388 dump_trace(tsk, regs, stack, bp, &print_trace_ops, NULL);
393 _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *sp,
396 unsigned long *stack;
398 const int cpu = smp_processor_id();
399 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
400 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
402 // debugging aid: "show_stack(NULL, NULL);" prints the
403 // back trace for this cpu.
407 sp = (unsigned long *)tsk->thread.sp;
409 sp = (unsigned long *)&sp;
413 for(i=0; i < kstack_depth_to_print; i++) {
414 if (stack >= irqstack && stack <= irqstack_end) {
415 if (stack == irqstack_end) {
416 stack = (unsigned long *) (irqstack_end[-1]);
420 if (((long) stack & (THREAD_SIZE-1)) == 0)
423 if (i && ((i % 4) == 0))
425 printk(" %016lx", *stack++);
426 touch_nmi_watchdog();
428 show_trace(tsk, regs, sp, bp);
431 void show_stack(struct task_struct *tsk, unsigned long * sp)
433 _show_stack(tsk, NULL, sp, 0);
437 * The architecture-independent dump_stack generator
439 void dump_stack(void)
442 unsigned long bp = 0;
444 #ifdef CONFIG_FRAME_POINTER
446 asm("movq %%rbp, %0" : "=r" (bp):);
449 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
450 current->pid, current->comm, print_tainted(),
451 init_utsname()->release,
452 (int)strcspn(init_utsname()->version, " "),
453 init_utsname()->version);
454 show_trace(NULL, NULL, &dummy, bp);
457 EXPORT_SYMBOL(dump_stack);
459 void show_registers(struct pt_regs *regs)
463 const int cpu = smp_processor_id();
464 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
466 unsigned int code_prologue = code_bytes * 43 / 64;
467 unsigned int code_len = code_bytes;
470 ip = (u8 *) regs->ip - code_prologue;
471 printk("CPU %d ", cpu);
473 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
474 cur->comm, cur->pid, task_thread_info(cur), cur);
477 * When in-kernel, we also print out the stack and code at the
478 * time of the fault..
480 if (!user_mode(regs)) {
483 _show_stack(NULL, regs, (unsigned long *)sp, regs->bp);
486 printk(KERN_EMERG "Code: ");
487 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
488 /* try starting at RIP */
489 ip = (u8 *) regs->ip;
490 code_len = code_len - code_prologue + 1;
492 for (i = 0; i < code_len; i++, ip++) {
493 if (ip < (u8 *)PAGE_OFFSET ||
494 probe_kernel_address(ip, c)) {
495 printk(" Bad RIP value.");
498 if (ip == (u8 *)regs->ip)
499 printk("<%02x> ", c);
507 int is_valid_bugaddr(unsigned long ip)
511 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
514 return ud2 == 0x0b0f;
517 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
518 static int die_owner = -1;
519 static unsigned int die_nest_count;
521 unsigned __kprobes long oops_begin(void)
528 /* racy, but better than risking deadlock. */
529 raw_local_irq_save(flags);
530 cpu = smp_processor_id();
531 if (!__raw_spin_trylock(&die_lock)) {
532 if (cpu == die_owner)
533 /* nested oops. should stop eventually */;
535 __raw_spin_lock(&die_lock);
544 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
550 /* Nest count reaches zero, release the lock. */
551 __raw_spin_unlock(&die_lock);
552 raw_local_irq_restore(flags);
558 panic("Fatal exception");
563 int __kprobes __die(const char * str, struct pt_regs * regs, long err)
565 static int die_counter;
566 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
567 #ifdef CONFIG_PREEMPT
573 #ifdef CONFIG_DEBUG_PAGEALLOC
574 printk("DEBUG_PAGEALLOC");
577 if (notify_die(DIE_OOPS, str, regs, err, 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) ==
611 flags = oops_begin();
613 * We are in trouble anyway, lets at least try
614 * to get a message out.
616 printk(str, smp_processor_id());
617 show_registers(regs);
618 if (kexec_should_crash(current))
620 if (do_panic || panic_on_oops)
621 panic("Non maskable interrupt");
622 oops_end(flags, NULL, SIGBUS);
628 static void __kprobes do_trap(int trapnr, int signr, char *str,
629 struct pt_regs * regs, long error_code,
632 struct task_struct *tsk = current;
634 if (user_mode(regs)) {
636 * We want error_code and trap_no set for userspace
637 * faults and kernelspace faults which result in
638 * die(), but not kernelspace faults which are fixed
639 * up. die() gives the process no chance to handle
640 * the signal and notice the kernel fault information,
641 * so that won't result in polluting the information
642 * about previously queued, but not yet delivered,
643 * faults. See also do_general_protection below.
645 tsk->thread.error_code = error_code;
646 tsk->thread.trap_no = trapnr;
648 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
649 printk_ratelimit()) {
651 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
652 tsk->comm, tsk->pid, str,
653 regs->ip, regs->sp, error_code);
654 print_vma_addr(" in ", regs->ip);
659 force_sig_info(signr, info, tsk);
661 force_sig(signr, tsk);
666 if (!fixup_exception(regs)) {
667 tsk->thread.error_code = error_code;
668 tsk->thread.trap_no = trapnr;
669 die(str, regs, error_code);
674 #define DO_ERROR(trapnr, signr, str, name) \
675 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
677 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
680 conditional_sti(regs); \
681 do_trap(trapnr, signr, str, regs, error_code, NULL); \
684 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
685 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
688 info.si_signo = signr; \
690 info.si_code = sicode; \
691 info.si_addr = (void __user *)siaddr; \
692 trace_hardirqs_fixup(); \
693 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
696 conditional_sti(regs); \
697 do_trap(trapnr, signr, str, regs, error_code, &info); \
700 DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
701 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
702 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
703 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
704 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
705 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
706 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
707 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
709 /* Runs on IST stack */
710 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
712 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
713 12, SIGBUS) == NOTIFY_STOP)
715 preempt_conditional_sti(regs);
716 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
717 preempt_conditional_cli(regs);
720 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
722 static const char str[] = "double fault";
723 struct task_struct *tsk = current;
725 /* Return not checked because double check cannot be ignored */
726 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
728 tsk->thread.error_code = error_code;
729 tsk->thread.trap_no = 8;
731 /* This is always a kernel trap and never fixable (and thus must
734 die(str, regs, error_code);
737 asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
740 struct task_struct *tsk = current;
742 conditional_sti(regs);
744 if (user_mode(regs)) {
745 tsk->thread.error_code = error_code;
746 tsk->thread.trap_no = 13;
748 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
749 printk_ratelimit()) {
751 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
753 regs->ip, regs->sp, error_code);
754 print_vma_addr(" in ", regs->ip);
758 force_sig(SIGSEGV, tsk);
762 if (fixup_exception(regs))
765 tsk->thread.error_code = error_code;
766 tsk->thread.trap_no = 13;
767 if (notify_die(DIE_GPF, "general protection fault", regs,
768 error_code, 13, SIGSEGV) == NOTIFY_STOP)
770 die("general protection fault", regs, error_code);
773 static notrace __kprobes void
774 mem_parity_error(unsigned char reason, struct pt_regs * regs)
776 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
778 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
780 #if defined(CONFIG_EDAC)
781 if(edac_handler_set()) {
782 edac_atomic_assert_error();
787 if (panic_on_unrecovered_nmi)
788 panic("NMI: Not continuing");
790 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
792 /* Clear and disable the memory parity error line. */
793 reason = (reason & 0xf) | 4;
797 static notrace __kprobes void
798 io_check_error(unsigned char reason, struct pt_regs * regs)
800 printk("NMI: IOCK error (debug interrupt?)\n");
801 show_registers(regs);
803 /* Re-enable the IOCK line, wait for a few seconds */
804 reason = (reason & 0xf) | 8;
811 static notrace __kprobes void
812 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
814 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
816 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
818 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
820 if (panic_on_unrecovered_nmi)
821 panic("NMI: Not continuing");
823 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
826 /* Runs on IST stack. This code must keep interrupts off all the time.
827 Nested NMIs are prevented by the CPU. */
828 asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
830 unsigned char reason = 0;
833 cpu = smp_processor_id();
835 /* Only the BSP gets external NMIs from the system. */
837 reason = get_nmi_reason();
839 if (!(reason & 0xc0)) {
840 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
844 * Ok, so this is none of the documented NMI sources,
845 * so it must be the NMI watchdog.
847 if (nmi_watchdog_tick(regs,reason))
849 if (!do_nmi_callback(regs,cpu))
850 unknown_nmi_error(reason, regs);
854 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
857 /* AK: following checks seem to be broken on modern chipsets. FIXME */
860 mem_parity_error(reason, regs);
862 io_check_error(reason, regs);
865 /* runs on IST stack. */
866 asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
868 trace_hardirqs_fixup();
870 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
873 preempt_conditional_sti(regs);
874 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
875 preempt_conditional_cli(regs);
878 /* Help handler running on IST stack to switch back to user stack
879 for scheduling or signal handling. The actual stack switch is done in
881 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
883 struct pt_regs *regs = eregs;
884 /* Did already sync */
885 if (eregs == (struct pt_regs *)eregs->sp)
887 /* Exception from user space */
888 else if (user_mode(eregs))
889 regs = task_pt_regs(current);
890 /* Exception from kernel and interrupts are enabled. Move to
891 kernel process stack. */
892 else if (eregs->flags & X86_EFLAGS_IF)
893 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
899 /* runs on IST stack. */
900 asmlinkage void __kprobes do_debug(struct pt_regs * regs,
901 unsigned long error_code)
903 unsigned long condition;
904 struct task_struct *tsk = current;
907 trace_hardirqs_fixup();
909 get_debugreg(condition, 6);
912 * The processor cleared BTF, so don't mark that we need it set.
914 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
915 tsk->thread.debugctlmsr = 0;
917 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
918 SIGTRAP) == NOTIFY_STOP)
921 preempt_conditional_sti(regs);
923 /* Mask out spurious debug traps due to lazy DR7 setting */
924 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
925 if (!tsk->thread.debugreg7) {
930 tsk->thread.debugreg6 = condition;
934 * Single-stepping through TF: make sure we ignore any events in
935 * kernel space (but re-enable TF when returning to user mode).
937 if (condition & DR_STEP) {
938 if (!user_mode(regs))
939 goto clear_TF_reenable;
942 /* Ok, finally something we can handle */
943 tsk->thread.trap_no = 1;
944 tsk->thread.error_code = error_code;
945 info.si_signo = SIGTRAP;
947 info.si_code = TRAP_BRKPT;
948 info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
949 force_sig_info(SIGTRAP, &info, tsk);
952 set_debugreg(0UL, 7);
953 preempt_conditional_cli(regs);
957 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
958 regs->flags &= ~X86_EFLAGS_TF;
959 preempt_conditional_cli(regs);
962 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
964 if (fixup_exception(regs))
967 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
968 /* Illegal floating point operation in the kernel */
969 current->thread.trap_no = trapnr;
975 * Note that we play around with the 'TS' bit in an attempt to get
976 * the correct behaviour even in the presence of the asynchronous
979 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
981 void __user *ip = (void __user *)(regs->ip);
982 struct task_struct * task;
984 unsigned short cwd, swd;
986 conditional_sti(regs);
987 if (!user_mode(regs) &&
988 kernel_math_error(regs, "kernel x87 math error", 16))
992 * Save the info for the exception handler and clear the error.
996 task->thread.trap_no = 16;
997 task->thread.error_code = 0;
998 info.si_signo = SIGFPE;
1000 info.si_code = __SI_FAULT;
1003 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1004 * status. 0x3f is the exception bits in these regs, 0x200 is the
1005 * C1 reg you need in case of a stack fault, 0x040 is the stack
1006 * fault bit. We should only be taking one exception at a time,
1007 * so if this combination doesn't produce any single exception,
1008 * then we have a bad program that isn't synchronizing its FPU usage
1009 * and it will suffer the consequences since we won't be able to
1010 * fully reproduce the context of the exception
1012 cwd = get_fpu_cwd(task);
1013 swd = get_fpu_swd(task);
1014 switch (swd & ~cwd & 0x3f) {
1018 case 0x001: /* Invalid Op */
1020 * swd & 0x240 == 0x040: Stack Underflow
1021 * swd & 0x240 == 0x240: Stack Overflow
1022 * User must clear the SF bit (0x40) if set
1024 info.si_code = FPE_FLTINV;
1026 case 0x002: /* Denormalize */
1027 case 0x010: /* Underflow */
1028 info.si_code = FPE_FLTUND;
1030 case 0x004: /* Zero Divide */
1031 info.si_code = FPE_FLTDIV;
1033 case 0x008: /* Overflow */
1034 info.si_code = FPE_FLTOVF;
1036 case 0x020: /* Precision */
1037 info.si_code = FPE_FLTRES;
1040 force_sig_info(SIGFPE, &info, task);
1043 asmlinkage void bad_intr(void)
1045 printk("bad interrupt");
1048 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
1050 void __user *ip = (void __user *)(regs->ip);
1051 struct task_struct * task;
1053 unsigned short mxcsr;
1055 conditional_sti(regs);
1056 if (!user_mode(regs) &&
1057 kernel_math_error(regs, "kernel simd math error", 19))
1061 * Save the info for the exception handler and clear the error.
1064 save_init_fpu(task);
1065 task->thread.trap_no = 19;
1066 task->thread.error_code = 0;
1067 info.si_signo = SIGFPE;
1069 info.si_code = __SI_FAULT;
1072 * The SIMD FPU exceptions are handled a little differently, as there
1073 * is only a single status/control register. Thus, to determine which
1074 * unmasked exception was caught we must mask the exception mask bits
1075 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1077 mxcsr = get_fpu_mxcsr(task);
1078 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1082 case 0x001: /* Invalid Op */
1083 info.si_code = FPE_FLTINV;
1085 case 0x002: /* Denormalize */
1086 case 0x010: /* Underflow */
1087 info.si_code = FPE_FLTUND;
1089 case 0x004: /* Zero Divide */
1090 info.si_code = FPE_FLTDIV;
1092 case 0x008: /* Overflow */
1093 info.si_code = FPE_FLTOVF;
1095 case 0x020: /* Precision */
1096 info.si_code = FPE_FLTRES;
1099 force_sig_info(SIGFPE, &info, task);
1102 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1106 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1110 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1115 * 'math_state_restore()' saves the current math information in the
1116 * old math state array, and gets the new ones from the current task
1118 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1119 * Don't touch unless you *really* know how it works.
1121 asmlinkage void math_state_restore(void)
1123 struct task_struct *me = current;
1128 * does a slab alloc which can sleep
1132 * ran out of memory!
1134 do_group_exit(SIGKILL);
1137 local_irq_disable();
1140 clts(); /* Allow maths ops (or we recurse) */
1141 restore_fpu_checking(&me->thread.xstate->fxsave);
1142 task_thread_info(me)->status |= TS_USEDFPU;
1145 EXPORT_SYMBOL_GPL(math_state_restore);
1147 void __init trap_init(void)
1149 set_intr_gate(0,÷_error);
1150 set_intr_gate_ist(1,&debug,DEBUG_STACK);
1151 set_intr_gate_ist(2,&nmi,NMI_STACK);
1152 set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
1153 set_system_gate(4,&overflow); /* int4 can be called from all */
1154 set_intr_gate(5,&bounds);
1155 set_intr_gate(6,&invalid_op);
1156 set_intr_gate(7,&device_not_available);
1157 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
1158 set_intr_gate(9,&coprocessor_segment_overrun);
1159 set_intr_gate(10,&invalid_TSS);
1160 set_intr_gate(11,&segment_not_present);
1161 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
1162 set_intr_gate(13,&general_protection);
1163 set_intr_gate(14,&page_fault);
1164 set_intr_gate(15,&spurious_interrupt_bug);
1165 set_intr_gate(16,&coprocessor_error);
1166 set_intr_gate(17,&alignment_check);
1167 #ifdef CONFIG_X86_MCE
1168 set_intr_gate_ist(18,&machine_check, MCE_STACK);
1170 set_intr_gate(19,&simd_coprocessor_error);
1172 #ifdef CONFIG_IA32_EMULATION
1173 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
1177 * initialize the per thread extended state:
1179 init_thread_xstate();
1181 * Should be a barrier for any external CPU state.
1187 static int __init oops_setup(char *s)
1191 if (!strcmp(s, "panic"))
1195 early_param("oops", oops_setup);
1197 static int __init kstack_setup(char *s)
1201 kstack_depth_to_print = simple_strtoul(s,NULL,0);
1204 early_param("kstack", kstack_setup);
1207 static int __init code_bytes_setup(char *s)
1209 code_bytes = simple_strtoul(s, NULL, 0);
1210 if (code_bytes > 8192)
1215 __setup("code_bytes=", code_bytes_setup);