2 * linux/arch/x86-64/traps.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
7 * Pentium III FXSR, SSE support
8 * Gareth Hughes <gareth@valinux.com>, May 2000
12 * 'Traps.c' handles hardware traps and faults after we have saved some
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/timer.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/kallsyms.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/nmi.h>
30 #include <linux/kprobes.h>
31 #include <linux/kexec.h>
32 #include <linux/unwind.h>
33 #include <linux/uaccess.h>
34 #include <linux/bug.h>
36 #include <asm/system.h>
38 #include <asm/atomic.h>
39 #include <asm/debugreg.h>
42 #include <asm/kdebug.h>
43 #include <asm/processor.h>
44 #include <asm/unwind.h>
46 #include <asm/pgalloc.h>
48 #include <asm/proto.h>
50 #include <asm/stacktrace.h>
52 asmlinkage void divide_error(void);
53 asmlinkage void debug(void);
54 asmlinkage void nmi(void);
55 asmlinkage void int3(void);
56 asmlinkage void overflow(void);
57 asmlinkage void bounds(void);
58 asmlinkage void invalid_op(void);
59 asmlinkage void device_not_available(void);
60 asmlinkage void double_fault(void);
61 asmlinkage void coprocessor_segment_overrun(void);
62 asmlinkage void invalid_TSS(void);
63 asmlinkage void segment_not_present(void);
64 asmlinkage void stack_segment(void);
65 asmlinkage void general_protection(void);
66 asmlinkage void page_fault(void);
67 asmlinkage void coprocessor_error(void);
68 asmlinkage void simd_coprocessor_error(void);
69 asmlinkage void reserved(void);
70 asmlinkage void alignment_check(void);
71 asmlinkage void machine_check(void);
72 asmlinkage void spurious_interrupt_bug(void);
74 ATOMIC_NOTIFIER_HEAD(die_chain);
75 EXPORT_SYMBOL(die_chain);
77 extern char last_sysfs_file[];
79 int register_die_notifier(struct notifier_block *nb)
82 return atomic_notifier_chain_register(&die_chain, nb);
84 EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
86 int unregister_die_notifier(struct notifier_block *nb)
88 return atomic_notifier_chain_unregister(&die_chain, nb);
90 EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
92 static inline void conditional_sti(struct pt_regs *regs)
94 if (regs->eflags & X86_EFLAGS_IF)
98 static inline void preempt_conditional_sti(struct pt_regs *regs)
101 if (regs->eflags & X86_EFLAGS_IF)
105 static inline void preempt_conditional_cli(struct pt_regs *regs)
107 if (regs->eflags & X86_EFLAGS_IF)
109 /* Make sure to not schedule here because we could be running
110 on an exception stack. */
111 preempt_enable_no_resched();
114 int kstack_depth_to_print = 12;
116 #ifdef CONFIG_KALLSYMS
117 void printk_address(unsigned long address)
119 unsigned long offset = 0, symsize;
125 symname = kallsyms_lookup(address, &symsize, &offset,
128 printk(" [<%016lx>]\n", address);
132 modname = delim = "";
133 printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
134 address, delim, modname, delim, symname, offset, symsize);
137 void printk_address(unsigned long address)
139 printk(" [<%016lx>]\n", address);
143 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
144 unsigned *usedp, char **idp)
146 #ifndef CONFIG_X86_NO_TSS
147 static char ids[][8] = {
148 [DEBUG_STACK - 1] = "#DB",
149 [NMI_STACK - 1] = "NMI",
150 [DOUBLEFAULT_STACK - 1] = "#DF",
151 [STACKFAULT_STACK - 1] = "#SS",
152 [MCE_STACK - 1] = "#MC",
153 #if DEBUG_STKSZ > EXCEPTION_STKSZ
154 [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
160 * Iterate over all exception stacks, and figure out whether
161 * 'stack' is in one of them:
163 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
164 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
166 * Is 'stack' above this exception frame's end?
167 * If yes then skip to the next frame.
172 * Is 'stack' above this exception frame's start address?
173 * If yes then we found the right frame.
175 if (stack >= end - EXCEPTION_STKSZ) {
177 * Make sure we only iterate through an exception
178 * stack once. If it comes up for the second time
179 * then there's something wrong going on - just
180 * break out and return NULL:
182 if (*usedp & (1U << k))
186 return (unsigned long *)end;
189 * If this is a debug stack, and if it has a larger size than
190 * the usual exception stacks, then 'stack' might still
191 * be within the lower portion of the debug stack:
193 #if DEBUG_STKSZ > EXCEPTION_STKSZ
194 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
195 unsigned j = N_EXCEPTION_STACKS - 1;
198 * Black magic. A large debug stack is composed of
199 * multiple exception stack entries, which we
200 * iterate through now. Dont look:
204 end -= EXCEPTION_STKSZ;
205 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
206 } while (stack < end - EXCEPTION_STKSZ);
207 if (*usedp & (1U << j))
211 return (unsigned long *)end;
219 #define MSG(txt) ops->warning(data, txt)
222 * x86-64 can have upto three kernel stacks:
225 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
228 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
230 void *t = (void *)tinfo;
231 return p > t && p < t + THREAD_SIZE - 3;
234 void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
235 unsigned long *stack,
236 struct stacktrace_ops *ops, void *data)
238 const unsigned cpu = get_cpu();
239 unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
241 struct thread_info *tinfo;
249 if (tsk && tsk != current)
250 stack = (unsigned long *)tsk->thread.rsp;
254 * Print function call entries within a stack. 'cond' is the
255 * "end of stackframe" condition, that the 'stack++'
256 * iteration will eventually trigger.
258 #define HANDLE_STACK(cond) \
260 unsigned long addr = *stack++; \
261 /* Use unlocked access here because except for NMIs \
262 we should be already protected against module unloads */ \
263 if (__kernel_text_address(addr)) { \
265 * If the address is either in the text segment of the \
266 * kernel, or in the region which contains vmalloc'ed \
267 * memory, it *may* be the address of a calling \
268 * routine; if so, print it so that someone tracing \
269 * down the cause of the crash will be able to figure \
270 * out the call path that was taken. \
272 ops->address(data, addr); \
277 * Print function call entries in all stacks, starting at the
278 * current stack address. If the stacks consist of nested
283 unsigned long *estack_end;
284 estack_end = in_exception_stack(cpu, (unsigned long)stack,
288 if (ops->stack(data, id) < 0)
290 HANDLE_STACK (stack < estack_end);
291 ops->stack(data, "<EOE>");
293 * We link to the next stack via the
294 * second-to-last pointer (index -2 to end) in the
297 stack = (unsigned long *) estack_end[-2];
301 unsigned long *irqstack;
302 irqstack = irqstack_end -
303 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
305 if (stack >= irqstack && stack < irqstack_end) {
306 if (ops->stack(data, "IRQ") < 0)
308 HANDLE_STACK (stack < irqstack_end);
310 * We link to the next stack (which would be
311 * the process stack normally) the last
312 * pointer (index -1 to end) in the IRQ stack:
314 stack = (unsigned long *) (irqstack_end[-1]);
316 ops->stack(data, "EOI");
324 * This handles the process stack:
326 tinfo = task_thread_info(tsk);
327 HANDLE_STACK (valid_stack_ptr(tinfo, stack));
331 EXPORT_SYMBOL(dump_trace);
334 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
336 print_symbol(msg, symbol);
340 static void print_trace_warning(void *data, char *msg)
345 static int print_trace_stack(void *data, char *name)
347 printk(" <%s> ", name);
351 static void print_trace_address(void *data, unsigned long addr)
353 printk_address(addr);
356 static struct stacktrace_ops print_trace_ops = {
357 .warning = print_trace_warning,
358 .warning_symbol = print_trace_warning_symbol,
359 .stack = print_trace_stack,
360 .address = print_trace_address,
364 show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack)
366 printk("\nCall Trace:\n");
367 dump_trace(tsk, regs, stack, &print_trace_ops, NULL);
372 _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *rsp)
374 unsigned long *stack;
376 const int cpu = smp_processor_id();
377 unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
378 unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
380 // debugging aid: "show_stack(NULL, NULL);" prints the
381 // back trace for this cpu.
385 rsp = (unsigned long *)tsk->thread.rsp;
387 rsp = (unsigned long *)&rsp;
391 for(i=0; i < kstack_depth_to_print; i++) {
392 if (stack >= irqstack && stack <= irqstack_end) {
393 if (stack == irqstack_end) {
394 stack = (unsigned long *) (irqstack_end[-1]);
398 if (((long) stack & (THREAD_SIZE-1)) == 0)
401 if (i && ((i % 4) == 0))
403 printk(" %016lx", *stack++);
404 touch_nmi_watchdog();
406 show_trace(tsk, regs, rsp);
409 void show_stack(struct task_struct *tsk, unsigned long * rsp)
411 _show_stack(tsk, NULL, rsp);
415 * The architecture-independent dump_stack generator
417 void dump_stack(void)
420 show_trace(NULL, NULL, &dummy);
423 EXPORT_SYMBOL(dump_stack);
425 void show_registers(struct pt_regs *regs)
428 int in_kernel = !user_mode(regs);
430 const int cpu = smp_processor_id();
431 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
435 printk("CPU %d ", cpu);
437 printk("Process %s (pid: %d[#%u], threadinfo %p, task %p)\n",
438 cur->comm, cur->pid, cur->xid,
439 task_thread_info(cur), cur);
442 * When in-kernel, we also print out the stack and code at the
443 * time of the fault..
448 _show_stack(NULL, regs, (unsigned long*)rsp);
451 if (regs->rip < PAGE_OFFSET)
454 for (i=0; i<20; i++) {
456 if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
458 printk(" Bad RIP value.");
467 int is_valid_bugaddr(unsigned long rip)
471 if (__copy_from_user(&ud2, (const void __user *) rip, sizeof(ud2)))
474 return ud2 == 0x0b0f;
478 void out_of_line_bug(void)
482 EXPORT_SYMBOL(out_of_line_bug);
485 static DEFINE_SPINLOCK(die_lock);
486 static int die_owner = -1;
487 static unsigned int die_nest_count;
489 unsigned __kprobes long oops_begin(void)
491 int cpu = smp_processor_id();
496 /* racy, but better than risking deadlock. */
497 local_irq_save(flags);
498 if (!spin_trylock(&die_lock)) {
499 if (cpu == die_owner)
500 /* nested oops. should stop eventually */;
502 spin_lock(&die_lock);
511 void __kprobes oops_end(unsigned long flags)
517 /* We still own the lock */
518 local_irq_restore(flags);
520 /* Nest count reaches zero, release the lock. */
521 spin_unlock_irqrestore(&die_lock, flags);
523 panic("Fatal exception");
527 void __kprobes __die(const char * str, struct pt_regs * regs, long err)
529 static int die_counter;
530 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
531 #ifdef CONFIG_PREEMPT
537 #ifdef CONFIG_DEBUG_PAGEALLOC
538 printk("DEBUG_PAGEALLOC");
542 printk(KERN_ALERT "last sysfs file: %s\n", last_sysfs_file);
544 notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
545 show_registers(regs);
546 /* Executive summary in case the oops scrolled away */
547 printk(KERN_ALERT "RIP ");
548 printk_address(regs->rip);
549 printk(" RSP <%016lx>\n", regs->rsp);
550 if (kexec_should_crash(current))
554 void die(const char * str, struct pt_regs * regs, long err)
556 unsigned long flags = oops_begin();
558 if (!user_mode(regs))
559 report_bug(regs->rip);
561 __die(str, regs, err);
566 void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
568 unsigned long flags = oops_begin();
571 * We are in trouble anyway, lets at least try
572 * to get a message out.
574 printk(str, smp_processor_id());
575 show_registers(regs);
576 if (kexec_should_crash(current))
578 if (do_panic || panic_on_oops)
579 panic("Non maskable interrupt");
586 static void __kprobes do_trap(int trapnr, int signr, char *str,
587 struct pt_regs * regs, long error_code,
590 struct task_struct *tsk = current;
592 tsk->thread.error_code = error_code;
593 tsk->thread.trap_no = trapnr;
595 if (user_mode(regs)) {
596 if (exception_trace && unhandled_signal(tsk, signr))
598 "%s[%d:#%u] trap %s rip:%lx rsp:%lx error:%lx\n",
599 tsk->comm, tsk->pid, tsk->xid, str,
600 regs->rip, regs->rsp, error_code);
603 force_sig_info(signr, info, tsk);
605 force_sig(signr, tsk);
612 const struct exception_table_entry *fixup;
613 fixup = search_exception_tables(regs->rip);
615 regs->rip = fixup->fixup;
617 die(str, regs, error_code);
622 #define DO_ERROR(trapnr, signr, str, name) \
623 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
625 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
628 conditional_sti(regs); \
629 do_trap(trapnr, signr, str, regs, error_code, NULL); \
632 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
633 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
636 info.si_signo = signr; \
638 info.si_code = sicode; \
639 info.si_addr = (void __user *)siaddr; \
640 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
643 conditional_sti(regs); \
644 do_trap(trapnr, signr, str, regs, error_code, &info); \
647 DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
648 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
649 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
650 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
651 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
652 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
653 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
654 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
655 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
656 DO_ERROR(18, SIGSEGV, "reserved", reserved)
658 /* Runs on IST stack */
659 asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
661 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
662 12, SIGBUS) == NOTIFY_STOP)
664 preempt_conditional_sti(regs);
665 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
666 preempt_conditional_cli(regs);
669 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
671 static const char str[] = "double fault";
672 struct task_struct *tsk = current;
674 /* Return not checked because double check cannot be ignored */
675 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
677 tsk->thread.error_code = error_code;
678 tsk->thread.trap_no = 8;
680 /* This is always a kernel trap and never fixable (and thus must
683 die(str, regs, error_code);
686 asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
689 struct task_struct *tsk = current;
691 conditional_sti(regs);
693 tsk->thread.error_code = error_code;
694 tsk->thread.trap_no = 13;
696 if (user_mode(regs)) {
697 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
699 "%s[%d:#%u] general protection rip:%lx rsp:%lx error:%lx\n",
700 tsk->comm, tsk->pid, tsk->xid,
701 regs->rip, regs->rsp, error_code);
703 force_sig(SIGSEGV, tsk);
709 const struct exception_table_entry *fixup;
710 fixup = search_exception_tables(regs->rip);
712 regs->rip = fixup->fixup;
715 if (notify_die(DIE_GPF, "general protection fault", regs,
716 error_code, 13, SIGSEGV) == NOTIFY_STOP)
718 die("general protection fault", regs, error_code);
722 static __kprobes void
723 mem_parity_error(unsigned char reason, struct pt_regs * regs)
725 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
727 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
729 if (panic_on_unrecovered_nmi)
730 panic("NMI: Not continuing");
732 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
735 /* Clear and disable the memory parity error line. */
736 reason = (reason & 0xf) | 4;
741 static __kprobes void
742 io_check_error(unsigned char reason, struct pt_regs * regs)
744 printk("NMI: IOCK error (debug interrupt?)\n");
745 show_registers(regs);
748 /* Re-enable the IOCK line, wait for a few seconds */
749 reason = (reason & 0xf) | 8;
757 static __kprobes void
758 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
760 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
762 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
764 if (panic_on_unrecovered_nmi)
765 panic("NMI: Not continuing");
767 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
770 /* Runs on IST stack. This code must keep interrupts off all the time.
771 Nested NMIs are prevented by the CPU. */
772 asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
774 unsigned char reason = 0;
777 cpu = smp_processor_id();
779 /* Only the BSP gets external NMIs from the system. */
781 reason = get_nmi_reason();
783 if (!(reason & 0xc0)) {
784 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
788 * Ok, so this is none of the documented NMI sources,
789 * so it must be the NMI watchdog.
791 if (nmi_watchdog_tick(regs,reason))
793 if (!do_nmi_callback(regs,cpu))
794 unknown_nmi_error(reason, regs);
798 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
801 /* AK: following checks seem to be broken on modern chipsets. FIXME */
804 mem_parity_error(reason, regs);
806 io_check_error(reason, regs);
809 /* runs on IST stack. */
810 asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
812 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
815 preempt_conditional_sti(regs);
816 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
817 preempt_conditional_cli(regs);
820 /* Help handler running on IST stack to switch back to user stack
821 for scheduling or signal handling. The actual stack switch is done in
823 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
825 struct pt_regs *regs = eregs;
826 /* Did already sync */
827 if (eregs == (struct pt_regs *)eregs->rsp)
829 /* Exception from user space */
830 else if (user_mode(eregs))
831 regs = task_pt_regs(current);
832 /* Exception from kernel and interrupts are enabled. Move to
833 kernel process stack. */
834 else if (eregs->eflags & X86_EFLAGS_IF)
835 regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
841 /* runs on IST stack. */
842 asmlinkage void __kprobes do_debug(struct pt_regs * regs,
843 unsigned long error_code)
845 unsigned long condition;
846 struct task_struct *tsk = current;
849 get_debugreg(condition, 6);
851 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
852 SIGTRAP) == NOTIFY_STOP)
855 preempt_conditional_sti(regs);
857 /* Mask out spurious debug traps due to lazy DR7 setting */
858 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
859 if (!tsk->thread.debugreg7) {
864 tsk->thread.debugreg6 = condition;
866 /* Mask out spurious TF errors due to lazy TF clearing */
867 if (condition & DR_STEP) {
869 * The TF error should be masked out only if the current
870 * process is not traced and if the TRAP flag has been set
871 * previously by a tracing process (condition detected by
872 * the PT_DTRACE flag); remember that the i386 TRAP flag
873 * can be modified by the process itself in user mode,
874 * allowing programs to debug themselves without the ptrace()
877 if (!user_mode(regs))
878 goto clear_TF_reenable;
881 /* Ok, finally something we can handle */
882 tsk->thread.trap_no = 1;
883 tsk->thread.error_code = error_code;
884 info.si_signo = SIGTRAP;
886 info.si_code = TRAP_BRKPT;
887 info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
888 force_sig_info(SIGTRAP, &info, tsk);
891 set_debugreg(0UL, 7);
892 preempt_conditional_cli(regs);
896 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
897 regs->eflags &= ~TF_MASK;
898 preempt_conditional_cli(regs);
901 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
903 const struct exception_table_entry *fixup;
904 fixup = search_exception_tables(regs->rip);
906 regs->rip = fixup->fixup;
909 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
910 /* Illegal floating point operation in the kernel */
911 current->thread.trap_no = trapnr;
917 * Note that we play around with the 'TS' bit in an attempt to get
918 * the correct behaviour even in the presence of the asynchronous
921 asmlinkage void do_coprocessor_error(struct pt_regs *regs)
923 void __user *rip = (void __user *)(regs->rip);
924 struct task_struct * task;
926 unsigned short cwd, swd;
928 conditional_sti(regs);
929 if (!user_mode(regs) &&
930 kernel_math_error(regs, "kernel x87 math error", 16))
934 * Save the info for the exception handler and clear the error.
938 task->thread.trap_no = 16;
939 task->thread.error_code = 0;
940 info.si_signo = SIGFPE;
942 info.si_code = __SI_FAULT;
945 * (~cwd & swd) will mask out exceptions that are not set to unmasked
946 * status. 0x3f is the exception bits in these regs, 0x200 is the
947 * C1 reg you need in case of a stack fault, 0x040 is the stack
948 * fault bit. We should only be taking one exception at a time,
949 * so if this combination doesn't produce any single exception,
950 * then we have a bad program that isn't synchronizing its FPU usage
951 * and it will suffer the consequences since we won't be able to
952 * fully reproduce the context of the exception
954 cwd = get_fpu_cwd(task);
955 swd = get_fpu_swd(task);
956 switch (swd & ~cwd & 0x3f) {
960 case 0x001: /* Invalid Op */
962 * swd & 0x240 == 0x040: Stack Underflow
963 * swd & 0x240 == 0x240: Stack Overflow
964 * User must clear the SF bit (0x40) if set
966 info.si_code = FPE_FLTINV;
968 case 0x002: /* Denormalize */
969 case 0x010: /* Underflow */
970 info.si_code = FPE_FLTUND;
972 case 0x004: /* Zero Divide */
973 info.si_code = FPE_FLTDIV;
975 case 0x008: /* Overflow */
976 info.si_code = FPE_FLTOVF;
978 case 0x020: /* Precision */
979 info.si_code = FPE_FLTRES;
982 force_sig_info(SIGFPE, &info, task);
985 asmlinkage void bad_intr(void)
987 printk("bad interrupt");
990 asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
992 void __user *rip = (void __user *)(regs->rip);
993 struct task_struct * task;
995 unsigned short mxcsr;
997 conditional_sti(regs);
998 if (!user_mode(regs) &&
999 kernel_math_error(regs, "kernel simd math error", 19))
1003 * Save the info for the exception handler and clear the error.
1006 save_init_fpu(task);
1007 task->thread.trap_no = 19;
1008 task->thread.error_code = 0;
1009 info.si_signo = SIGFPE;
1011 info.si_code = __SI_FAULT;
1014 * The SIMD FPU exceptions are handled a little differently, as there
1015 * is only a single status/control register. Thus, to determine which
1016 * unmasked exception was caught we must mask the exception mask bits
1017 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1019 mxcsr = get_fpu_mxcsr(task);
1020 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1024 case 0x001: /* Invalid Op */
1025 info.si_code = FPE_FLTINV;
1027 case 0x002: /* Denormalize */
1028 case 0x010: /* Underflow */
1029 info.si_code = FPE_FLTUND;
1031 case 0x004: /* Zero Divide */
1032 info.si_code = FPE_FLTDIV;
1034 case 0x008: /* Overflow */
1035 info.si_code = FPE_FLTOVF;
1037 case 0x020: /* Precision */
1038 info.si_code = FPE_FLTRES;
1041 force_sig_info(SIGFPE, &info, task);
1044 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
1049 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
1054 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
1059 * 'math_state_restore()' saves the current math information in the
1060 * old math state array, and gets the new ones from the current task
1062 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1063 * Don't touch unless you *really* know how it works.
1065 asmlinkage void math_state_restore(void)
1067 struct task_struct *me = current;
1068 /* clts(); */ /* 'clts' is done for us by Xen during virtual trap. */
1072 restore_fpu_checking(&me->thread.i387.fxsave);
1073 task_thread_info(me)->status |= TS_USEDFPU;
1079 * NB. All these are "interrupt gates" (i.e. events_mask is set) because we
1080 * specify <dpl>|4 in the second field.
1082 static trap_info_t trap_table[] = {
1083 { 0, 0|4, __KERNEL_CS, (unsigned long)divide_error },
1084 { 1, 0|4, __KERNEL_CS, (unsigned long)debug },
1085 { 3, 3|4, __KERNEL_CS, (unsigned long)int3 },
1086 { 4, 3|4, __KERNEL_CS, (unsigned long)overflow },
1087 { 5, 0|4, __KERNEL_CS, (unsigned long)bounds },
1088 { 6, 0|4, __KERNEL_CS, (unsigned long)invalid_op },
1089 { 7, 0|4, __KERNEL_CS, (unsigned long)device_not_available },
1090 { 9, 0|4, __KERNEL_CS, (unsigned long)coprocessor_segment_overrun},
1091 { 10, 0|4, __KERNEL_CS, (unsigned long)invalid_TSS },
1092 { 11, 0|4, __KERNEL_CS, (unsigned long)segment_not_present },
1093 { 12, 0|4, __KERNEL_CS, (unsigned long)stack_segment },
1094 { 13, 0|4, __KERNEL_CS, (unsigned long)general_protection },
1095 { 14, 0|4, __KERNEL_CS, (unsigned long)page_fault },
1096 { 15, 0|4, __KERNEL_CS, (unsigned long)spurious_interrupt_bug },
1097 { 16, 0|4, __KERNEL_CS, (unsigned long)coprocessor_error },
1098 { 17, 0|4, __KERNEL_CS, (unsigned long)alignment_check },
1099 #ifdef CONFIG_X86_MCE
1100 { 18, 0|4, __KERNEL_CS, (unsigned long)machine_check },
1102 { 19, 0|4, __KERNEL_CS, (unsigned long)simd_coprocessor_error },
1103 #ifdef CONFIG_IA32_EMULATION
1104 { IA32_SYSCALL_VECTOR, 3|4, __KERNEL_CS, (unsigned long)ia32_syscall},
1109 void __init trap_init(void)
1113 ret = HYPERVISOR_set_trap_table(trap_table);
1116 printk("HYPERVISOR_set_trap_table faild: error %d\n",
1120 * Should be a barrier for any external CPU state.
1125 void smp_trap_init(trap_info_t *trap_ctxt)
1127 trap_info_t *t = trap_table;
1129 for (t = trap_table; t->address; t++) {
1130 trap_ctxt[t->vector].flags = t->flags;
1131 trap_ctxt[t->vector].cs = t->cs;
1132 trap_ctxt[t->vector].address = t->address;
1137 static int __init oops_setup(char *s)
1141 if (!strcmp(s, "panic"))
1145 early_param("oops", oops_setup);
1147 static int __init kstack_setup(char *s)
1151 kstack_depth_to_print = simple_strtoul(s,NULL,0);
1154 early_param("kstack", kstack_setup);