2 * linux/arch/i386/traps.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
11 * 'Traps.c' handles hardware traps and faults after we have saved some
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.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/highmem.h>
25 #include <linux/kallsyms.h>
26 #include <linux/ptrace.h>
27 #include <linux/utsname.h>
28 #include <linux/kprobes.h>
29 #include <linux/kexec.h>
30 #include <linux/unwind.h>
31 #include <linux/uaccess.h>
32 #include <linux/nmi.h>
35 #include <linux/ioport.h>
36 #include <linux/eisa.h>
40 #include <linux/mca.h>
43 #include <asm/processor.h>
44 #include <asm/system.h>
46 #include <asm/atomic.h>
47 #include <asm/debugreg.h>
51 #include <asm/unwind.h>
53 #include <asm/arch_hooks.h>
54 #include <asm/kdebug.h>
55 #include <asm/stacktrace.h>
57 #include <linux/module.h>
59 #include "mach_traps.h"
61 int panic_on_unrecovered_nmi;
63 asmlinkage int system_call(void);
65 /* Do we ignore FPU interrupts ? */
66 char ignore_fpu_irq = 0;
69 * The IDT has to be page-aligned to simplify the Pentium
70 * F0 0F bug workaround.. We have a special link segment
73 struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
75 asmlinkage void divide_error(void);
76 asmlinkage void debug(void);
77 asmlinkage void nmi(void);
78 asmlinkage void int3(void);
79 asmlinkage void overflow(void);
80 asmlinkage void bounds(void);
81 asmlinkage void invalid_op(void);
82 asmlinkage void device_not_available(void);
83 asmlinkage void coprocessor_segment_overrun(void);
84 asmlinkage void invalid_TSS(void);
85 asmlinkage void segment_not_present(void);
86 asmlinkage void stack_segment(void);
87 asmlinkage void general_protection(void);
88 asmlinkage void page_fault(void);
89 asmlinkage void coprocessor_error(void);
90 asmlinkage void simd_coprocessor_error(void);
91 asmlinkage void alignment_check(void);
92 asmlinkage void spurious_interrupt_bug(void);
93 asmlinkage void machine_check(void);
95 int kstack_depth_to_print = 24;
96 #ifdef CONFIG_STACK_UNWIND
97 static int call_trace = 1;
99 #define call_trace (-1)
101 ATOMIC_NOTIFIER_HEAD(i386die_chain);
103 int register_die_notifier(struct notifier_block *nb)
106 return atomic_notifier_chain_register(&i386die_chain, nb);
108 EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
110 int unregister_die_notifier(struct notifier_block *nb)
112 return atomic_notifier_chain_unregister(&i386die_chain, nb);
114 EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
116 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
118 return p > (void *)tinfo &&
119 p < (void *)tinfo + THREAD_SIZE - 3;
122 static inline unsigned long print_context_stack(struct thread_info *tinfo,
123 unsigned long *stack, unsigned long ebp,
124 struct stacktrace_ops *ops, void *data)
128 #ifdef CONFIG_FRAME_POINTER
129 while (valid_stack_ptr(tinfo, (void *)ebp)) {
130 unsigned long new_ebp;
131 addr = *(unsigned long *)(ebp + 4);
132 ops->address(data, addr);
134 * break out of recursive entries (such as
135 * end_of_stack_stop_unwind_function). Also,
136 * we can never allow a frame pointer to
139 new_ebp = *(unsigned long *)ebp;
145 while (valid_stack_ptr(tinfo, stack)) {
147 if (__kernel_text_address(addr))
148 ops->address(data, addr);
154 struct ops_and_data {
155 struct stacktrace_ops *ops;
159 static asmlinkage int
160 dump_trace_unwind(struct unwind_frame_info *info, void *data)
162 struct ops_and_data *oad = (struct ops_and_data *)data;
164 unsigned long sp = UNW_SP(info);
166 if (arch_unw_user_mode(info))
168 while (unwind(info) == 0 && UNW_PC(info)) {
170 oad->ops->address(oad->data, UNW_PC(info));
171 if (arch_unw_user_mode(info))
173 if ((sp & ~(PAGE_SIZE - 1)) == (UNW_SP(info) & ~(PAGE_SIZE - 1))
174 && sp > UNW_SP(info))
181 #define MSG(msg) ops->warning(data, msg)
183 void dump_trace(struct task_struct *task, struct pt_regs *regs,
184 unsigned long *stack,
185 struct stacktrace_ops *ops, void *data)
187 unsigned long ebp = 0;
192 if (call_trace >= 0) {
194 struct unwind_frame_info info;
195 struct ops_and_data oad = { .ops = ops, .data = data };
198 if (unwind_init_frame_info(&info, task, regs) == 0)
199 unw_ret = dump_trace_unwind(&info, &oad);
200 } else if (task == current)
201 unw_ret = unwind_init_running(&info, dump_trace_unwind,
204 if (unwind_init_blocked(&info, task) == 0)
205 unw_ret = dump_trace_unwind(&info, &oad);
208 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
209 ops->warning_symbol(data,
210 "DWARF2 unwinder stuck at %s",
212 if (UNW_SP(&info) >= PAGE_OFFSET) {
213 MSG("Leftover inexact backtrace:");
214 stack = (void *)UNW_SP(&info);
219 MSG("Full inexact backtrace again:");
220 } else if (call_trace >= 1)
223 MSG("Full inexact backtrace again:");
225 MSG("Inexact backtrace:");
230 if (task && task != current)
231 stack = (unsigned long *)task->thread.esp;
234 #ifdef CONFIG_FRAME_POINTER
236 if (task == current) {
237 /* Grab ebp right from our regs */
238 asm ("movl %%ebp, %0" : "=r" (ebp) : );
240 /* ebp is the last reg pushed by switch_to */
241 ebp = *(unsigned long *) task->thread.esp;
247 struct thread_info *context;
248 context = (struct thread_info *)
249 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
250 ebp = print_context_stack(context, stack, ebp, ops, data);
251 /* Should be after the line below, but somewhere
252 in early boot context comes out corrupted and we
253 can't reference it -AK */
254 if (ops->stack(data, "IRQ") < 0)
256 stack = (unsigned long*)context->previous_esp;
259 touch_nmi_watchdog();
262 EXPORT_SYMBOL(dump_trace);
265 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
268 print_symbol(msg, symbol);
272 static void print_trace_warning(void *data, char *msg)
274 printk("%s%s\n", (char *)data, msg);
277 static int print_trace_stack(void *data, char *name)
283 * Print one address/symbol entries per line.
285 static void print_trace_address(void *data, unsigned long addr)
287 printk("%s [<%08lx>] ", (char *)data, addr);
288 print_symbol("%s\n", addr);
291 static struct stacktrace_ops print_trace_ops = {
292 .warning = print_trace_warning,
293 .warning_symbol = print_trace_warning_symbol,
294 .stack = print_trace_stack,
295 .address = print_trace_address,
299 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
300 unsigned long * stack, char *log_lvl)
302 dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
303 printk("%s =======================\n", log_lvl);
306 void show_trace(struct task_struct *task, struct pt_regs *regs,
307 unsigned long * stack)
309 show_trace_log_lvl(task, regs, stack, "");
312 static void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
313 unsigned long *esp, char *log_lvl)
315 unsigned long *stack;
320 esp = (unsigned long*)task->thread.esp;
322 esp = (unsigned long *)&esp;
326 for(i = 0; i < kstack_depth_to_print; i++) {
327 if (kstack_end(stack))
329 if (i && ((i % 8) == 0))
330 printk("\n%s ", log_lvl);
331 printk("%08lx ", *stack++);
333 printk("\n%sCall Trace:\n", log_lvl);
334 show_trace_log_lvl(task, regs, esp, log_lvl);
337 void show_stack(struct task_struct *task, unsigned long *esp)
340 show_stack_log_lvl(task, NULL, esp, "");
344 * The architecture-independent dump_stack generator
346 void dump_stack(void)
350 show_trace(current, NULL, &stack);
353 EXPORT_SYMBOL(dump_stack);
355 void show_registers(struct pt_regs *regs)
362 esp = (unsigned long) (®s->esp);
364 if (user_mode_vm(regs)) {
367 ss = regs->xss & 0xffff;
370 printk(KERN_EMERG "CPU: %d\n"
371 KERN_EMERG "EIP: %04x:[<%08lx>] %s VLI\n"
372 KERN_EMERG "EFLAGS: %08lx (%s %.*s)\n",
373 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
374 print_tainted(), regs->eflags, init_utsname()->release,
375 (int)strcspn(init_utsname()->version, " "),
376 init_utsname()->version);
377 print_symbol(KERN_EMERG "EIP is at %s\n", regs->eip);
378 printk(KERN_EMERG "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
379 regs->eax, regs->ebx, regs->ecx, regs->edx);
380 printk(KERN_EMERG "esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
381 regs->esi, regs->edi, regs->ebp, esp);
382 printk(KERN_EMERG "ds: %04x es: %04x ss: %04x\n",
383 regs->xds & 0xffff, regs->xes & 0xffff, ss);
384 printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)",
385 TASK_COMM_LEN, current->comm, current->pid,
386 current_thread_info(), current, current->thread_info);
388 * When in-kernel, we also print out the stack and code at the
389 * time of the fault..
396 printk("\n" KERN_EMERG "Stack: ");
397 show_stack_log_lvl(NULL, regs, (unsigned long *)esp, KERN_EMERG);
399 printk(KERN_EMERG "Code: ");
401 eip = (u8 *)regs->eip - 43;
402 if (eip < (u8 *)PAGE_OFFSET ||
403 probe_kernel_address(eip, c)) {
404 /* try starting at EIP */
405 eip = (u8 *)regs->eip;
408 for (i = 0; i < code_bytes; i++, eip++) {
409 if (eip < (u8 *)PAGE_OFFSET ||
410 probe_kernel_address(eip, c)) {
411 printk(" Bad EIP value.");
414 if (eip == (u8 *)regs->eip)
415 printk("<%02x> ", c);
423 static void handle_BUG(struct pt_regs *regs)
425 unsigned long eip = regs->eip;
428 if (eip < PAGE_OFFSET)
430 if (probe_kernel_address((unsigned short *)eip, ud2))
435 printk(KERN_EMERG "------------[ cut here ]------------\n");
437 #ifdef CONFIG_DEBUG_BUGVERBOSE
443 if (probe_kernel_address((unsigned short *)(eip + 2), line))
445 if (probe_kernel_address((char **)(eip + 4), file) ||
446 (unsigned long)file < PAGE_OFFSET ||
447 probe_kernel_address(file, c))
448 file = "<bad filename>";
450 printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line);
454 printk(KERN_EMERG "Kernel BUG at [verbose debug info unavailable]\n");
457 /* This is gone through when something in the kernel
458 * has done something bad and is about to be terminated.
460 void die(const char * str, struct pt_regs * regs, long err)
465 int lock_owner_depth;
467 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
469 .lock_owner_depth = 0
471 static int die_counter;
476 if (die.lock_owner != raw_smp_processor_id()) {
478 spin_lock_irqsave(&die.lock, flags);
479 die.lock_owner = smp_processor_id();
480 die.lock_owner_depth = 0;
484 local_save_flags(flags);
486 if (++die.lock_owner_depth < 3) {
492 printk(KERN_EMERG "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
493 #ifdef CONFIG_PREEMPT
494 printk(KERN_EMERG "PREEMPT ");
503 #ifdef CONFIG_DEBUG_PAGEALLOC
506 printk("DEBUG_PAGEALLOC");
511 if (notify_die(DIE_OOPS, str, regs, err,
512 current->thread.trap_no, SIGSEGV) !=
514 show_registers(regs);
515 /* Executive summary in case the oops scrolled away */
516 esp = (unsigned long) (®s->esp);
518 if (user_mode(regs)) {
520 ss = regs->xss & 0xffff;
522 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->eip);
523 print_symbol("%s", regs->eip);
524 printk(" SS:ESP %04x:%08lx\n", ss, esp);
529 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
533 spin_unlock_irqrestore(&die.lock, flags);
538 if (kexec_should_crash(current))
542 panic("Fatal exception in interrupt");
545 panic("Fatal exception");
551 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
553 if (!user_mode_vm(regs))
557 static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
558 struct pt_regs * regs, long error_code,
561 struct task_struct *tsk = current;
562 tsk->thread.error_code = error_code;
563 tsk->thread.trap_no = trapnr;
565 if (regs->eflags & VM_MASK) {
571 if (!user_mode(regs))
576 force_sig_info(signr, info, tsk);
578 force_sig(signr, tsk);
583 if (!fixup_exception(regs))
584 die(str, regs, error_code);
589 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
590 if (ret) goto trap_signal;
595 #define DO_ERROR(trapnr, signr, str, name) \
596 fastcall void do_##name(struct pt_regs * regs, long error_code) \
598 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
601 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
604 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
605 fastcall void do_##name(struct pt_regs * regs, long error_code) \
608 info.si_signo = signr; \
610 info.si_code = sicode; \
611 info.si_addr = (void __user *)siaddr; \
612 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
615 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
618 #define DO_VM86_ERROR(trapnr, signr, str, name) \
619 fastcall void do_##name(struct pt_regs * regs, long error_code) \
621 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
624 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
627 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
628 fastcall void do_##name(struct pt_regs * regs, long error_code) \
631 info.si_signo = signr; \
633 info.si_code = sicode; \
634 info.si_addr = (void __user *)siaddr; \
635 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
638 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
641 DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
642 #ifndef CONFIG_KPROBES
643 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
645 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
646 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
647 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)
648 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
649 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
650 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
651 DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
652 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
653 DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
655 fastcall void __kprobes do_general_protection(struct pt_regs * regs,
659 struct tss_struct *tss = &per_cpu(init_tss, cpu);
660 struct thread_struct *thread = ¤t->thread;
663 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
664 * invalid offset set (the LAZY one) and the faulting thread has
665 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS
666 * and we set the offset field correctly. Then we let the CPU to
667 * restart the faulting instruction.
669 if (tss->io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
670 thread->io_bitmap_ptr) {
671 memcpy(tss->io_bitmap, thread->io_bitmap_ptr,
672 thread->io_bitmap_max);
674 * If the previously set map was extending to higher ports
675 * than the current one, pad extra space with 0xff (no access).
677 if (thread->io_bitmap_max < tss->io_bitmap_max)
678 memset((char *) tss->io_bitmap +
679 thread->io_bitmap_max, 0xff,
680 tss->io_bitmap_max - thread->io_bitmap_max);
681 tss->io_bitmap_max = thread->io_bitmap_max;
682 tss->io_bitmap_base = IO_BITMAP_OFFSET;
683 tss->io_bitmap_owner = thread;
689 current->thread.error_code = error_code;
690 current->thread.trap_no = 13;
692 if (regs->eflags & VM_MASK)
695 if (!user_mode(regs))
698 current->thread.error_code = error_code;
699 current->thread.trap_no = 13;
700 force_sig(SIGSEGV, current);
705 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
709 if (!fixup_exception(regs)) {
710 if (notify_die(DIE_GPF, "general protection fault", regs,
711 error_code, 13, SIGSEGV) == NOTIFY_STOP)
713 die("general protection fault", regs, error_code);
717 static __kprobes void
718 mem_parity_error(unsigned char reason, struct pt_regs * regs)
720 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
721 "CPU %d.\n", reason, smp_processor_id());
722 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
723 if (panic_on_unrecovered_nmi)
724 panic("NMI: Not continuing");
726 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
728 /* Clear and disable the memory parity error line. */
729 clear_mem_error(reason);
732 static __kprobes void
733 io_check_error(unsigned char reason, struct pt_regs * regs)
737 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
738 show_registers(regs);
740 /* Re-enable the IOCK line, wait for a few seconds */
741 reason = (reason & 0xf) | 8;
744 while (--i) udelay(1000);
749 static __kprobes void
750 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
753 /* Might actually be able to figure out what the guilty party
760 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
761 "CPU %d.\n", reason, smp_processor_id());
762 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
763 if (panic_on_unrecovered_nmi)
764 panic("NMI: Not continuing");
766 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
769 static DEFINE_SPINLOCK(nmi_print_lock);
771 void __kprobes die_nmi(struct pt_regs *regs, const char *msg)
773 if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
777 spin_lock(&nmi_print_lock);
779 * We are in trouble anyway, lets at least try
780 * to get a message out.
783 printk(KERN_EMERG "%s", msg);
784 printk(" on CPU%d, eip %08lx, registers:\n",
785 smp_processor_id(), regs->eip);
786 show_registers(regs);
788 spin_unlock(&nmi_print_lock);
791 /* If we are in kernel we are probably nested up pretty bad
792 * and might aswell get out now while we still can.
794 if (!user_mode_vm(regs)) {
795 current->thread.trap_no = 2;
802 static __kprobes void default_do_nmi(struct pt_regs * regs)
804 unsigned char reason = 0;
806 /* Only the BSP gets external NMIs from the system. */
807 if (!smp_processor_id())
808 reason = get_nmi_reason();
810 if (!(reason & 0xc0)) {
811 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
814 #ifdef CONFIG_X86_LOCAL_APIC
816 * Ok, so this is none of the documented NMI sources,
817 * so it must be the NMI watchdog.
819 if (nmi_watchdog_tick(regs, reason))
821 if (!do_nmi_callback(regs, smp_processor_id()))
823 unknown_nmi_error(reason, regs);
827 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
830 mem_parity_error(reason, regs);
832 io_check_error(reason, regs);
834 * Reassert NMI in case it became active meanwhile
835 * as it's edge-triggered.
840 fastcall __kprobes void do_nmi(struct pt_regs * regs, long error_code)
846 cpu = smp_processor_id();
850 default_do_nmi(regs);
855 #ifdef CONFIG_KPROBES
856 fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
858 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
861 /* This is an interrupt gate, because kprobes wants interrupts
862 disabled. Normal trap handlers don't. */
863 restore_interrupts(regs);
864 do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
869 * Our handling of the processor debug registers is non-trivial.
870 * We do not clear them on entry and exit from the kernel. Therefore
871 * it is possible to get a watchpoint trap here from inside the kernel.
872 * However, the code in ./ptrace.c has ensured that the user can
873 * only set watchpoints on userspace addresses. Therefore the in-kernel
874 * watchpoint trap can only occur in code which is reading/writing
875 * from user space. Such code must not hold kernel locks (since it
876 * can equally take a page fault), therefore it is safe to call
877 * force_sig_info even though that claims and releases locks.
879 * Code in ./signal.c ensures that the debug control register
880 * is restored before we deliver any signal, and therefore that
881 * user code runs with the correct debug control register even though
884 * Being careful here means that we don't have to be as careful in a
885 * lot of more complicated places (task switching can be a bit lazy
886 * about restoring all the debug state, and ptrace doesn't have to
887 * find every occurrence of the TF bit that could be saved away even
890 fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
892 unsigned int condition;
893 struct task_struct *tsk = current;
895 get_debugreg(condition, 6);
897 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
898 SIGTRAP) == NOTIFY_STOP)
900 /* It's safe to allow irq's after DR6 has been saved */
901 if (regs->eflags & X86_EFLAGS_IF)
904 /* Mask out spurious debug traps due to lazy DR7 setting */
905 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
906 if (!tsk->thread.debugreg[7])
910 if (regs->eflags & VM_MASK)
913 /* Save debug status register where ptrace can see it */
914 tsk->thread.debugreg[6] = condition;
917 * Single-stepping through TF: make sure we ignore any events in
918 * kernel space (but re-enable TF when returning to user mode).
920 if (condition & DR_STEP) {
922 * We already checked v86 mode above, so we can
923 * check for kernel mode by just checking the CPL
926 if (!user_mode(regs))
927 goto clear_TF_reenable;
930 /* Ok, finally something we can handle */
931 send_sigtrap(tsk, regs, error_code);
933 /* Disable additional traps. They'll be re-enabled when
934 * the signal is delivered.
941 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
945 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
946 regs->eflags &= ~TF_MASK;
951 * Note that we play around with the 'TS' bit in an attempt to get
952 * the correct behaviour even in the presence of the asynchronous
955 void math_error(void __user *eip)
957 struct task_struct * task;
959 unsigned short cwd, swd;
962 * Save the info for the exception handler and clear the error.
966 task->thread.trap_no = 16;
967 task->thread.error_code = 0;
968 info.si_signo = SIGFPE;
970 info.si_code = __SI_FAULT;
973 * (~cwd & swd) will mask out exceptions that are not set to unmasked
974 * status. 0x3f is the exception bits in these regs, 0x200 is the
975 * C1 reg you need in case of a stack fault, 0x040 is the stack
976 * fault bit. We should only be taking one exception at a time,
977 * so if this combination doesn't produce any single exception,
978 * then we have a bad program that isn't syncronizing its FPU usage
979 * and it will suffer the consequences since we won't be able to
980 * fully reproduce the context of the exception
982 cwd = get_fpu_cwd(task);
983 swd = get_fpu_swd(task);
984 switch (swd & ~cwd & 0x3f) {
985 case 0x000: /* No unmasked exception */
987 default: /* Multiple exceptions */
989 case 0x001: /* Invalid Op */
991 * swd & 0x240 == 0x040: Stack Underflow
992 * swd & 0x240 == 0x240: Stack Overflow
993 * User must clear the SF bit (0x40) if set
995 info.si_code = FPE_FLTINV;
997 case 0x002: /* Denormalize */
998 case 0x010: /* Underflow */
999 info.si_code = FPE_FLTUND;
1001 case 0x004: /* Zero Divide */
1002 info.si_code = FPE_FLTDIV;
1004 case 0x008: /* Overflow */
1005 info.si_code = FPE_FLTOVF;
1007 case 0x020: /* Precision */
1008 info.si_code = FPE_FLTRES;
1011 force_sig_info(SIGFPE, &info, task);
1014 fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
1017 math_error((void __user *)regs->eip);
1020 static void simd_math_error(void __user *eip)
1022 struct task_struct * task;
1024 unsigned short mxcsr;
1027 * Save the info for the exception handler and clear the error.
1030 save_init_fpu(task);
1031 task->thread.trap_no = 19;
1032 task->thread.error_code = 0;
1033 info.si_signo = SIGFPE;
1035 info.si_code = __SI_FAULT;
1038 * The SIMD FPU exceptions are handled a little differently, as there
1039 * is only a single status/control register. Thus, to determine which
1040 * unmasked exception was caught we must mask the exception mask bits
1041 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1043 mxcsr = get_fpu_mxcsr(task);
1044 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1048 case 0x001: /* Invalid Op */
1049 info.si_code = FPE_FLTINV;
1051 case 0x002: /* Denormalize */
1052 case 0x010: /* Underflow */
1053 info.si_code = FPE_FLTUND;
1055 case 0x004: /* Zero Divide */
1056 info.si_code = FPE_FLTDIV;
1058 case 0x008: /* Overflow */
1059 info.si_code = FPE_FLTOVF;
1061 case 0x020: /* Precision */
1062 info.si_code = FPE_FLTRES;
1065 force_sig_info(SIGFPE, &info, task);
1068 fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
1072 /* Handle SIMD FPU exceptions on PIII+ processors. */
1074 simd_math_error((void __user *)regs->eip);
1077 * Handle strange cache flush from user space exception
1078 * in all other cases. This is undocumented behaviour.
1080 if (regs->eflags & VM_MASK) {
1081 handle_vm86_fault((struct kernel_vm86_regs *)regs,
1085 current->thread.trap_no = 19;
1086 current->thread.error_code = error_code;
1087 die_if_kernel("cache flush denied", regs, error_code);
1088 force_sig(SIGSEGV, current);
1092 fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
1096 /* No need to warn about this any longer. */
1097 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
1101 fastcall unsigned long patch_espfix_desc(unsigned long uesp,
1104 int cpu = smp_processor_id();
1105 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
1106 struct desc_struct *gdt = (struct desc_struct *)cpu_gdt_descr->address;
1107 unsigned long base = (kesp - uesp) & -THREAD_SIZE;
1108 unsigned long new_kesp = kesp - base;
1109 unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
1110 __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
1111 /* Set up base for espfix segment */
1112 desc &= 0x00f0ff0000000000ULL;
1113 desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
1114 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
1115 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
1116 (lim_pages & 0xffff);
1117 *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
1122 * 'math_state_restore()' saves the current math information in the
1123 * old math state array, and gets the new ones from the current task
1125 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1126 * Don't touch unless you *really* know how it works.
1128 * Must be called with kernel preemption disabled (in this case,
1129 * local interrupts are disabled at the call-site in entry.S).
1131 asmlinkage void math_state_restore(void)
1133 struct thread_info *thread = current_thread_info();
1134 struct task_struct *tsk = thread->task;
1136 clts(); /* Allow maths ops (or we recurse) */
1137 if (!tsk_used_math(tsk))
1140 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
1144 #ifndef CONFIG_MATH_EMULATION
1146 asmlinkage void math_emulate(long arg)
1148 printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
1149 printk(KERN_EMERG "killing %s.\n",current->comm);
1150 force_sig(SIGFPE,current);
1154 #endif /* CONFIG_MATH_EMULATION */
1156 #ifdef CONFIG_X86_F00F_BUG
1157 void __init trap_init_f00f_bug(void)
1159 __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
1162 * Update the IDT descriptor and reload the IDT so that
1163 * it uses the read-only mapped virtual address.
1165 idt_descr.address = fix_to_virt(FIX_F00F_IDT);
1166 load_idt(&idt_descr);
1171 * This needs to use 'idt_table' rather than 'idt', and
1172 * thus use the _nonmapped_ version of the IDT, as the
1173 * Pentium F0 0F bugfix can have resulted in the mapped
1174 * IDT being write-protected.
1176 void set_intr_gate(unsigned int n, void *addr)
1178 _set_gate(n, DESCTYPE_INT, addr, __KERNEL_CS);
1182 * This routine sets up an interrupt gate at directory privilege level 3.
1184 static inline void set_system_intr_gate(unsigned int n, void *addr)
1186 _set_gate(n, DESCTYPE_INT | DESCTYPE_DPL3, addr, __KERNEL_CS);
1189 static void __init set_trap_gate(unsigned int n, void *addr)
1191 _set_gate(n, DESCTYPE_TRAP, addr, __KERNEL_CS);
1194 static void __init set_system_gate(unsigned int n, void *addr)
1196 _set_gate(n, DESCTYPE_TRAP | DESCTYPE_DPL3, addr, __KERNEL_CS);
1199 static void __init set_task_gate(unsigned int n, unsigned int gdt_entry)
1201 _set_gate(n, DESCTYPE_TASK, (void *)0, (gdt_entry<<3));
1205 void __init trap_init(void)
1208 void __iomem *p = ioremap(0x0FFFD9, 4);
1209 if (readl(p) == 'E'+('I'<<8)+('S'<<16)+('A'<<24)) {
1215 #ifdef CONFIG_X86_LOCAL_APIC
1216 init_apic_mappings();
1219 set_trap_gate(0,÷_error);
1220 set_intr_gate(1,&debug);
1221 set_intr_gate(2,&nmi);
1222 set_system_intr_gate(3, &int3); /* int3/4 can be called from all */
1223 set_system_gate(4,&overflow);
1224 set_trap_gate(5,&bounds);
1225 set_trap_gate(6,&invalid_op);
1226 set_trap_gate(7,&device_not_available);
1227 set_task_gate(8,GDT_ENTRY_DOUBLEFAULT_TSS);
1228 set_trap_gate(9,&coprocessor_segment_overrun);
1229 set_trap_gate(10,&invalid_TSS);
1230 set_trap_gate(11,&segment_not_present);
1231 set_trap_gate(12,&stack_segment);
1232 set_trap_gate(13,&general_protection);
1233 set_intr_gate(14,&page_fault);
1234 set_trap_gate(15,&spurious_interrupt_bug);
1235 set_trap_gate(16,&coprocessor_error);
1236 set_trap_gate(17,&alignment_check);
1237 #ifdef CONFIG_X86_MCE
1238 set_trap_gate(18,&machine_check);
1240 set_trap_gate(19,&simd_coprocessor_error);
1244 * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
1245 * Generates a compile-time "error: zero width for bit-field" if
1246 * the alignment is wrong.
1248 struct fxsrAlignAssert {
1249 int _:!(offsetof(struct task_struct,
1250 thread.i387.fxsave) & 15);
1253 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1254 set_in_cr4(X86_CR4_OSFXSR);
1258 printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
1260 set_in_cr4(X86_CR4_OSXMMEXCPT);
1264 set_system_gate(SYSCALL_VECTOR,&system_call);
1267 * Should be a barrier for any external CPU state.
1274 static int __init kstack_setup(char *s)
1276 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
1279 __setup("kstack=", kstack_setup);
1281 #ifdef CONFIG_STACK_UNWIND
1282 static int __init call_trace_setup(char *s)
1284 if (strcmp(s, "old") == 0)
1286 else if (strcmp(s, "both") == 0)
1288 else if (strcmp(s, "newfallback") == 0)
1290 else if (strcmp(s, "new") == 2)
1294 __setup("call_trace=", call_trace_setup);