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>
33 #include <linux/bug.h>
36 #include <linux/ioport.h>
37 #include <linux/eisa.h>
41 #include <linux/mca.h>
44 #include <asm/processor.h>
45 #include <asm/system.h>
47 #include <asm/atomic.h>
48 #include <asm/debugreg.h>
52 #include <asm/unwind.h>
54 #include <asm/arch_hooks.h>
55 #include <asm/kdebug.h>
56 #include <asm/stacktrace.h>
58 #include <linux/module.h>
59 #include <linux/vs_context.h>
60 #include <linux/vserver/history.h>
62 #include "mach_traps.h"
64 int panic_on_unrecovered_nmi;
66 asmlinkage int system_call(void);
68 /* Do we ignore FPU interrupts ? */
69 char ignore_fpu_irq = 0;
71 #ifndef CONFIG_X86_NO_IDT
73 * The IDT has to be page-aligned to simplify the Pentium
74 * F0 0F bug workaround.. We have a special link segment
77 struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
80 asmlinkage void divide_error(void);
81 asmlinkage void debug(void);
82 asmlinkage void nmi(void);
83 asmlinkage void int3(void);
84 asmlinkage void overflow(void);
85 asmlinkage void bounds(void);
86 asmlinkage void invalid_op(void);
87 asmlinkage void device_not_available(void);
88 asmlinkage void coprocessor_segment_overrun(void);
89 asmlinkage void invalid_TSS(void);
90 asmlinkage void segment_not_present(void);
91 asmlinkage void stack_segment(void);
92 asmlinkage void general_protection(void);
93 asmlinkage void page_fault(void);
94 asmlinkage void coprocessor_error(void);
95 asmlinkage void simd_coprocessor_error(void);
96 asmlinkage void alignment_check(void);
98 asmlinkage void spurious_interrupt_bug(void);
100 asmlinkage void fixup_4gb_segment(void);
102 asmlinkage void machine_check(void);
104 int kstack_depth_to_print = 24;
105 ATOMIC_NOTIFIER_HEAD(i386die_chain);
107 extern char last_sysfs_file[];
109 int register_die_notifier(struct notifier_block *nb)
112 return atomic_notifier_chain_register(&i386die_chain, nb);
114 EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
116 int unregister_die_notifier(struct notifier_block *nb)
118 return atomic_notifier_chain_unregister(&i386die_chain, nb);
120 EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
122 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
124 return p > (void *)tinfo &&
125 p < (void *)tinfo + THREAD_SIZE - 3;
128 static inline unsigned long print_context_stack(struct thread_info *tinfo,
129 unsigned long *stack, unsigned long ebp,
130 struct stacktrace_ops *ops, void *data)
134 #ifdef CONFIG_FRAME_POINTER
135 while (valid_stack_ptr(tinfo, (void *)ebp)) {
136 unsigned long new_ebp;
137 addr = *(unsigned long *)(ebp + 4);
138 ops->address(data, addr);
140 * break out of recursive entries (such as
141 * end_of_stack_stop_unwind_function). Also,
142 * we can never allow a frame pointer to
145 new_ebp = *(unsigned long *)ebp;
151 while (valid_stack_ptr(tinfo, stack)) {
153 if (__kernel_text_address(addr))
154 ops->address(data, addr);
160 #define MSG(msg) ops->warning(data, msg)
162 void dump_trace(struct task_struct *task, struct pt_regs *regs,
163 unsigned long *stack,
164 struct stacktrace_ops *ops, void *data)
166 unsigned long ebp = 0;
174 if (task && task != current)
175 stack = (unsigned long *)task->thread.esp;
178 #ifdef CONFIG_FRAME_POINTER
180 if (task == current) {
181 /* Grab ebp right from our regs */
182 asm ("movl %%ebp, %0" : "=r" (ebp) : );
184 /* ebp is the last reg pushed by switch_to */
185 ebp = *(unsigned long *) task->thread.esp;
191 struct thread_info *context;
192 context = (struct thread_info *)
193 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
194 ebp = print_context_stack(context, stack, ebp, ops, data);
195 /* Should be after the line below, but somewhere
196 in early boot context comes out corrupted and we
197 can't reference it -AK */
198 if (ops->stack(data, "IRQ") < 0)
200 stack = (unsigned long*)context->previous_esp;
203 touch_nmi_watchdog();
206 EXPORT_SYMBOL(dump_trace);
209 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
212 print_symbol(msg, symbol);
216 static void print_trace_warning(void *data, char *msg)
218 printk("%s%s\n", (char *)data, msg);
221 static int print_trace_stack(void *data, char *name)
227 * Print one address/symbol entries per line.
229 static void print_trace_address(void *data, unsigned long addr)
231 printk("%s [<%08lx>] ", (char *)data, addr);
232 print_symbol("%s\n", addr);
235 static struct stacktrace_ops print_trace_ops = {
236 .warning = print_trace_warning,
237 .warning_symbol = print_trace_warning_symbol,
238 .stack = print_trace_stack,
239 .address = print_trace_address,
243 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
244 unsigned long * stack, char *log_lvl)
246 dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
247 printk("%s =======================\n", log_lvl);
250 void show_trace(struct task_struct *task, struct pt_regs *regs,
251 unsigned long * stack)
253 show_trace_log_lvl(task, regs, stack, "");
256 static void show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
257 unsigned long *esp, char *log_lvl)
259 unsigned long *stack;
264 esp = (unsigned long*)task->thread.esp;
266 esp = (unsigned long *)&esp;
270 for(i = 0; i < kstack_depth_to_print; i++) {
271 if (kstack_end(stack))
273 if (i && ((i % 8) == 0))
274 printk("\n%s ", log_lvl);
275 printk("%08lx ", *stack++);
277 printk("\n%sCall Trace:\n", log_lvl);
278 show_trace_log_lvl(task, regs, esp, log_lvl);
281 void show_stack(struct task_struct *task, unsigned long *esp)
284 show_stack_log_lvl(task, NULL, esp, "");
288 * The architecture-independent dump_stack generator
290 void dump_stack(void)
294 show_trace(current, NULL, &stack);
297 EXPORT_SYMBOL(dump_stack);
299 void show_registers(struct pt_regs *regs)
306 esp = (unsigned long) (®s->esp);
308 if (user_mode_vm(regs)) {
311 ss = regs->xss & 0xffff;
314 printk(KERN_EMERG "CPU: %d\n"
315 KERN_EMERG "EIP: %04x:[<%08lx>] %s VLI\n"
316 KERN_EMERG "EFLAGS: %08lx (%s %.*s)\n",
317 smp_processor_id(), 0xffff & regs->xcs, regs->eip,
318 print_tainted(), regs->eflags, init_utsname()->release,
319 (int)strcspn(init_utsname()->version, " "),
320 init_utsname()->version);
321 print_symbol(KERN_EMERG "EIP is at %s\n", regs->eip);
322 printk(KERN_EMERG "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
323 regs->eax, regs->ebx, regs->ecx, regs->edx);
324 printk(KERN_EMERG "esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
325 regs->esi, regs->edi, regs->ebp, esp);
326 printk(KERN_EMERG "ds: %04x es: %04x ss: %04x\n",
327 regs->xds & 0xffff, regs->xes & 0xffff, ss);
328 printk(KERN_EMERG "Process %.*s (pid: %d[#%u], ti=%p task=%p task.ti=%p)",
329 TASK_COMM_LEN, current->comm, current->pid, current->xid,
330 current_thread_info(), current, current->thread_info);
332 * When in-kernel, we also print out the stack and code at the
333 * time of the fault..
340 printk("\n" KERN_EMERG "Stack: ");
341 show_stack_log_lvl(NULL, regs, (unsigned long *)esp, KERN_EMERG);
343 printk(KERN_EMERG "Code: ");
345 eip = (u8 *)regs->eip - 43;
346 if (eip < (u8 *)PAGE_OFFSET ||
347 probe_kernel_address(eip, c)) {
348 /* try starting at EIP */
349 eip = (u8 *)regs->eip;
352 for (i = 0; i < code_bytes; i++, eip++) {
353 if (eip < (u8 *)PAGE_OFFSET ||
354 probe_kernel_address(eip, c)) {
355 printk(" Bad EIP value.");
358 if (eip == (u8 *)regs->eip)
359 printk("<%02x> ", c);
367 int is_valid_bugaddr(unsigned long eip)
371 if (eip < PAGE_OFFSET)
373 if (probe_kernel_address((unsigned short *)eip, ud2))
376 return ud2 == 0x0b0f;
380 * This is gone through when something in the kernel has done something bad and
381 * is about to be terminated.
383 void die(const char * str, struct pt_regs * regs, long err)
388 int lock_owner_depth;
390 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
392 .lock_owner_depth = 0
394 static int die_counter;
401 if (die.lock_owner != raw_smp_processor_id()) {
403 spin_lock_irqsave(&die.lock, flags);
404 die.lock_owner = smp_processor_id();
405 die.lock_owner_depth = 0;
409 local_save_flags(flags);
411 if (++die.lock_owner_depth < 3) {
416 report_bug(regs->eip);
418 printk(KERN_EMERG "%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
419 #ifdef CONFIG_PREEMPT
420 printk(KERN_EMERG "PREEMPT ");
429 #ifdef CONFIG_DEBUG_PAGEALLOC
432 printk("DEBUG_PAGEALLOC");
438 printk(KERN_ALERT "last sysfs file: %s\n", last_sysfs_file);
440 if (notify_die(DIE_OOPS, str, regs, err,
441 current->thread.trap_no, SIGSEGV) != NOTIFY_STOP) {
442 show_registers(regs);
444 /* Executive summary in case the oops scrolled away */
445 esp = (unsigned long) (®s->esp);
447 if (user_mode(regs)) {
449 ss = regs->xss & 0xffff;
451 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->eip);
452 print_symbol("%s", regs->eip);
453 printk(" SS:ESP %04x:%08lx\n", ss, esp);
458 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
462 spin_unlock_irqrestore(&die.lock, flags);
467 if (kexec_should_crash(current))
471 panic("Fatal exception in interrupt");
474 panic("Fatal exception");
480 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
482 if (!user_mode_vm(regs))
486 static void __kprobes do_trap(int trapnr, int signr, char *str, int vm86,
487 struct pt_regs * regs, long error_code,
490 struct task_struct *tsk = current;
491 tsk->thread.error_code = error_code;
492 tsk->thread.trap_no = trapnr;
494 if (regs->eflags & VM_MASK) {
500 if (!user_mode(regs))
505 force_sig_info(signr, info, tsk);
507 force_sig(signr, tsk);
512 if (!fixup_exception(regs))
513 die(str, regs, error_code);
518 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
519 if (ret) goto trap_signal;
524 #define DO_ERROR(trapnr, signr, str, name) \
525 fastcall void do_##name(struct pt_regs * regs, long error_code) \
527 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
530 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
533 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
534 fastcall void do_##name(struct pt_regs * regs, long error_code) \
537 info.si_signo = signr; \
539 info.si_code = sicode; \
540 info.si_addr = (void __user *)siaddr; \
541 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
544 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
547 #define DO_VM86_ERROR(trapnr, signr, str, name) \
548 fastcall void do_##name(struct pt_regs * regs, long error_code) \
550 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
553 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
556 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
557 fastcall void do_##name(struct pt_regs * regs, long error_code) \
560 info.si_signo = signr; \
562 info.si_code = sicode; \
563 info.si_addr = (void __user *)siaddr; \
564 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
567 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
570 DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
571 #ifndef CONFIG_KPROBES
572 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
574 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
575 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
576 DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->eip)
577 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
578 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
579 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
580 DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
581 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
585 * lazy-check for CS validity on exec-shield binaries:
587 * the original non-exec stack patch was written by
588 * Solar Designer <solar at openwall.com>. Thanks!
591 check_lazy_exec_limit(int cpu, struct pt_regs *regs, long error_code)
593 struct desc_struct *desc1, *desc2;
594 struct vm_area_struct *vma;
597 if (current->mm == NULL)
601 if (current->mm->context.exec_limit != -1UL) {
603 spin_lock(¤t->mm->page_table_lock);
604 for (vma = current->mm->mmap; vma; vma = vma->vm_next)
605 if ((vma->vm_flags & VM_EXEC) && (vma->vm_end > limit))
607 spin_unlock(¤t->mm->page_table_lock);
608 if (limit >= TASK_SIZE)
610 current->mm->context.exec_limit = limit;
612 set_user_cs(¤t->mm->context.user_cs, limit);
614 desc1 = ¤t->mm->context.user_cs;
615 desc2 = get_cpu_gdt_table(cpu) + GDT_ENTRY_DEFAULT_USER_CS;
617 if (desc1->a != desc2->a || desc1->b != desc2->b) {
619 * The CS was not in sync - reload it and retry the
620 * instruction. If the instruction still faults then
621 * we won't hit this branch next time around.
623 if (print_fatal_signals >= 2) {
624 printk("#GPF fixup (%ld[seg:%lx]) at %08lx, CPU#%d.\n", error_code, error_code/8, regs->eip, smp_processor_id());
625 printk(" exec_limit: %08lx, user_cs: %08lx/%08lx, CPU_cs: %08lx/%08lx.\n", current->mm->context.exec_limit, desc1->a, desc1->b, desc2->a, desc2->b);
627 load_user_cs_desc(cpu, current->mm);
635 * The fixup code for errors in iret jumps to here (iret_exc). It loses
636 * the original trap number and error code. The bogus trap 32 and error
637 * code 0 are what the vanilla kernel delivers via:
638 * DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0)
640 * In case of a general protection fault in the iret instruction, we
641 * need to check for a lazy CS update for exec-shield.
643 fastcall void do_iret_error(struct pt_regs *regs, long error_code)
645 int ok = check_lazy_exec_limit(get_cpu(), regs, error_code);
647 if (!ok && notify_die(DIE_TRAP, "iret exception", regs,
648 error_code, 32, SIGSEGV) != NOTIFY_STOP) {
650 info.si_signo = SIGSEGV;
652 info.si_code = ILL_BADSTK;
654 do_trap(32, SIGSEGV, "iret exception", 0, regs, error_code,
659 fastcall void __kprobes do_general_protection(struct pt_regs * regs,
665 current->thread.error_code = error_code;
666 current->thread.trap_no = 13;
668 if (regs->eflags & VM_MASK)
671 if (!user_mode(regs))
674 ok = check_lazy_exec_limit(cpu, regs, error_code);
681 if (print_fatal_signals) {
682 printk("#GPF(%ld[seg:%lx]) at %08lx, CPU#%d.\n", error_code, error_code/8, regs->eip, smp_processor_id());
683 printk(" exec_limit: %08lx, user_cs: %08lx/%08lx.\n", current->mm->context.exec_limit, current->mm->context.user_cs.a, current->mm->context.user_cs.b);
686 current->thread.error_code = error_code;
687 current->thread.trap_no = 13;
688 force_sig(SIGSEGV, current);
694 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
699 if (!fixup_exception(regs)) {
700 if (notify_die(DIE_GPF, "general protection fault", regs,
701 error_code, 13, SIGSEGV) == NOTIFY_STOP)
703 die("general protection fault", regs, error_code);
707 static __kprobes void
708 mem_parity_error(unsigned char reason, struct pt_regs * regs)
710 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
711 "CPU %d.\n", reason, smp_processor_id());
712 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
713 if (panic_on_unrecovered_nmi)
714 panic("NMI: Not continuing");
716 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
718 /* Clear and disable the memory parity error line. */
719 clear_mem_error(reason);
722 static __kprobes void
723 io_check_error(unsigned char reason, struct pt_regs * regs)
725 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
726 show_registers(regs);
728 /* Re-enable the IOCK line, wait for a few seconds */
729 clear_io_check_error(reason);
732 static __kprobes void
733 unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
736 /* Might actually be able to figure out what the guilty party
743 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x on "
744 "CPU %d.\n", reason, smp_processor_id());
745 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
746 if (panic_on_unrecovered_nmi)
747 panic("NMI: Not continuing");
749 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
752 static DEFINE_SPINLOCK(nmi_print_lock);
754 void __kprobes die_nmi(struct pt_regs *regs, const char *msg)
756 if (notify_die(DIE_NMIWATCHDOG, msg, regs, 0, 2, SIGINT) ==
760 spin_lock(&nmi_print_lock);
762 * We are in trouble anyway, lets at least try
763 * to get a message out.
766 printk(KERN_EMERG "%s", msg);
767 printk(" on CPU%d, eip %08lx, registers:\n",
768 smp_processor_id(), regs->eip);
769 show_registers(regs);
771 spin_unlock(&nmi_print_lock);
774 /* If we are in kernel we are probably nested up pretty bad
775 * and might aswell get out now while we still can.
777 if (!user_mode_vm(regs)) {
778 current->thread.trap_no = 2;
785 static __kprobes void default_do_nmi(struct pt_regs * regs)
787 unsigned char reason = 0;
789 /* Only the BSP gets external NMIs from the system. */
790 if (!smp_processor_id())
791 reason = get_nmi_reason();
793 if (!(reason & 0xc0)) {
794 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
797 #ifdef CONFIG_X86_LOCAL_APIC
799 * Ok, so this is none of the documented NMI sources,
800 * so it must be the NMI watchdog.
802 if (nmi_watchdog_tick(regs, reason))
804 if (!do_nmi_callback(regs, smp_processor_id()))
806 unknown_nmi_error(reason, regs);
810 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
813 mem_parity_error(reason, regs);
815 io_check_error(reason, regs);
817 * Reassert NMI in case it became active meanwhile
818 * as it's edge-triggered.
823 fastcall __kprobes void do_nmi(struct pt_regs * regs, long error_code)
829 cpu = smp_processor_id();
833 default_do_nmi(regs);
838 #ifdef CONFIG_KPROBES
839 fastcall void __kprobes do_int3(struct pt_regs *regs, long error_code)
841 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
844 /* This is an interrupt gate, because kprobes wants interrupts
845 disabled. Normal trap handlers don't. */
846 restore_interrupts(regs);
847 do_trap(3, SIGTRAP, "int3", 1, regs, error_code, NULL);
852 * Our handling of the processor debug registers is non-trivial.
853 * We do not clear them on entry and exit from the kernel. Therefore
854 * it is possible to get a watchpoint trap here from inside the kernel.
855 * However, the code in ./ptrace.c has ensured that the user can
856 * only set watchpoints on userspace addresses. Therefore the in-kernel
857 * watchpoint trap can only occur in code which is reading/writing
858 * from user space. Such code must not hold kernel locks (since it
859 * can equally take a page fault), therefore it is safe to call
860 * force_sig_info even though that claims and releases locks.
862 * Code in ./signal.c ensures that the debug control register
863 * is restored before we deliver any signal, and therefore that
864 * user code runs with the correct debug control register even though
867 * Being careful here means that we don't have to be as careful in a
868 * lot of more complicated places (task switching can be a bit lazy
869 * about restoring all the debug state, and ptrace doesn't have to
870 * find every occurrence of the TF bit that could be saved away even
873 fastcall void __kprobes do_debug(struct pt_regs * regs, long error_code)
875 unsigned int condition;
876 struct task_struct *tsk = current;
878 get_debugreg(condition, 6);
880 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
881 SIGTRAP) == NOTIFY_STOP)
883 /* It's safe to allow irq's after DR6 has been saved */
884 if (regs->eflags & X86_EFLAGS_IF)
887 /* Mask out spurious debug traps due to lazy DR7 setting */
888 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
889 if (!tsk->thread.debugreg[7])
893 if (regs->eflags & VM_MASK)
896 /* Save debug status register where ptrace can see it */
897 tsk->thread.debugreg[6] = condition;
900 * Single-stepping through TF: make sure we ignore any events in
901 * kernel space (but re-enable TF when returning to user mode).
903 if (condition & DR_STEP) {
905 * We already checked v86 mode above, so we can
906 * check for kernel mode by just checking the CPL
909 if (!user_mode(regs))
910 goto clear_TF_reenable;
913 /* Ok, finally something we can handle */
914 send_sigtrap(tsk, regs, error_code);
916 /* Disable additional traps. They'll be re-enabled when
917 * the signal is delivered.
924 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
928 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
929 regs->eflags &= ~TF_MASK;
934 * Note that we play around with the 'TS' bit in an attempt to get
935 * the correct behaviour even in the presence of the asynchronous
938 void math_error(void __user *eip)
940 struct task_struct * task;
942 unsigned short cwd, swd;
945 * Save the info for the exception handler and clear the error.
949 task->thread.trap_no = 16;
950 task->thread.error_code = 0;
951 info.si_signo = SIGFPE;
953 info.si_code = __SI_FAULT;
956 * (~cwd & swd) will mask out exceptions that are not set to unmasked
957 * status. 0x3f is the exception bits in these regs, 0x200 is the
958 * C1 reg you need in case of a stack fault, 0x040 is the stack
959 * fault bit. We should only be taking one exception at a time,
960 * so if this combination doesn't produce any single exception,
961 * then we have a bad program that isn't syncronizing its FPU usage
962 * and it will suffer the consequences since we won't be able to
963 * fully reproduce the context of the exception
965 cwd = get_fpu_cwd(task);
966 swd = get_fpu_swd(task);
967 switch (swd & ~cwd & 0x3f) {
968 case 0x000: /* No unmasked exception */
970 default: /* Multiple exceptions */
972 case 0x001: /* Invalid Op */
974 * swd & 0x240 == 0x040: Stack Underflow
975 * swd & 0x240 == 0x240: Stack Overflow
976 * User must clear the SF bit (0x40) if set
978 info.si_code = FPE_FLTINV;
980 case 0x002: /* Denormalize */
981 case 0x010: /* Underflow */
982 info.si_code = FPE_FLTUND;
984 case 0x004: /* Zero Divide */
985 info.si_code = FPE_FLTDIV;
987 case 0x008: /* Overflow */
988 info.si_code = FPE_FLTOVF;
990 case 0x020: /* Precision */
991 info.si_code = FPE_FLTRES;
994 force_sig_info(SIGFPE, &info, task);
997 fastcall void do_coprocessor_error(struct pt_regs * regs, long error_code)
1000 math_error((void __user *)regs->eip);
1003 static void simd_math_error(void __user *eip)
1005 struct task_struct * task;
1007 unsigned short mxcsr;
1010 * Save the info for the exception handler and clear the error.
1013 save_init_fpu(task);
1014 task->thread.trap_no = 19;
1015 task->thread.error_code = 0;
1016 info.si_signo = SIGFPE;
1018 info.si_code = __SI_FAULT;
1021 * The SIMD FPU exceptions are handled a little differently, as there
1022 * is only a single status/control register. Thus, to determine which
1023 * unmasked exception was caught we must mask the exception mask bits
1024 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1026 mxcsr = get_fpu_mxcsr(task);
1027 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
1031 case 0x001: /* Invalid Op */
1032 info.si_code = FPE_FLTINV;
1034 case 0x002: /* Denormalize */
1035 case 0x010: /* Underflow */
1036 info.si_code = FPE_FLTUND;
1038 case 0x004: /* Zero Divide */
1039 info.si_code = FPE_FLTDIV;
1041 case 0x008: /* Overflow */
1042 info.si_code = FPE_FLTOVF;
1044 case 0x020: /* Precision */
1045 info.si_code = FPE_FLTRES;
1048 force_sig_info(SIGFPE, &info, task);
1051 fastcall void do_simd_coprocessor_error(struct pt_regs * regs,
1055 /* Handle SIMD FPU exceptions on PIII+ processors. */
1057 simd_math_error((void __user *)regs->eip);
1060 * Handle strange cache flush from user space exception
1061 * in all other cases. This is undocumented behaviour.
1063 if (regs->eflags & VM_MASK) {
1064 handle_vm86_fault((struct kernel_vm86_regs *)regs,
1068 current->thread.trap_no = 19;
1069 current->thread.error_code = error_code;
1070 die_if_kernel("cache flush denied", regs, error_code);
1071 force_sig(SIGSEGV, current);
1076 fastcall void do_spurious_interrupt_bug(struct pt_regs * regs,
1080 /* No need to warn about this any longer. */
1081 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
1085 fastcall unsigned long patch_espfix_desc(unsigned long uesp,
1088 int cpu = smp_processor_id();
1089 struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
1090 struct desc_struct *gdt = (struct desc_struct *)cpu_gdt_descr->address;
1091 unsigned long base = (kesp - uesp) & -THREAD_SIZE;
1092 unsigned long new_kesp = kesp - base;
1093 unsigned long lim_pages = (new_kesp | (THREAD_SIZE - 1)) >> PAGE_SHIFT;
1094 __u64 desc = *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS];
1095 /* Set up base for espfix segment */
1096 desc &= 0x00f0ff0000000000ULL;
1097 desc |= ((((__u64)base) << 16) & 0x000000ffffff0000ULL) |
1098 ((((__u64)base) << 32) & 0xff00000000000000ULL) |
1099 ((((__u64)lim_pages) << 32) & 0x000f000000000000ULL) |
1100 (lim_pages & 0xffff);
1101 *(__u64 *)&gdt[GDT_ENTRY_ESPFIX_SS] = desc;
1107 * 'math_state_restore()' saves the current math information in the
1108 * old math state array, and gets the new ones from the current task
1110 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1111 * Don't touch unless you *really* know how it works.
1113 * Must be called with kernel preemption disabled (in this case,
1114 * local interrupts are disabled at the call-site in entry.S).
1116 asmlinkage void math_state_restore(void)
1118 struct thread_info *thread = current_thread_info();
1119 struct task_struct *tsk = thread->task;
1121 /* NB. 'clts' is done for us by Xen during virtual trap. */
1122 if (!tsk_used_math(tsk))
1125 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
1129 #ifndef CONFIG_MATH_EMULATION
1131 asmlinkage void math_emulate(long arg)
1133 printk(KERN_EMERG "math-emulation not enabled and no coprocessor found.\n");
1134 printk(KERN_EMERG "killing %s.\n",current->comm);
1135 force_sig(SIGFPE,current);
1139 #endif /* CONFIG_MATH_EMULATION */
1141 #ifdef CONFIG_X86_F00F_BUG
1142 void __init trap_init_f00f_bug(void)
1144 __set_fixmap(FIX_F00F_IDT, __pa(&idt_table), PAGE_KERNEL_RO);
1147 * Update the IDT descriptor and reload the IDT so that
1148 * it uses the read-only mapped virtual address.
1150 idt_descr.address = fix_to_virt(FIX_F00F_IDT);
1151 load_idt(&idt_descr);
1157 * NB. All these are "trap gates" (i.e. events_mask isn't set) except
1158 * for those that specify <dpl>|4 in the second field.
1160 static trap_info_t trap_table[] = {
1161 { 0, 0, __KERNEL_CS, (unsigned long)divide_error },
1162 { 1, 0|4, __KERNEL_CS, (unsigned long)debug },
1163 { 3, 3|4, __KERNEL_CS, (unsigned long)int3 },
1164 { 4, 3, __KERNEL_CS, (unsigned long)overflow },
1165 { 5, 0, __KERNEL_CS, (unsigned long)bounds },
1166 { 6, 0, __KERNEL_CS, (unsigned long)invalid_op },
1167 { 7, 0|4, __KERNEL_CS, (unsigned long)device_not_available },
1168 { 9, 0, __KERNEL_CS, (unsigned long)coprocessor_segment_overrun },
1169 { 10, 0, __KERNEL_CS, (unsigned long)invalid_TSS },
1170 { 11, 0, __KERNEL_CS, (unsigned long)segment_not_present },
1171 { 12, 0, __KERNEL_CS, (unsigned long)stack_segment },
1172 { 13, 0, __KERNEL_CS, (unsigned long)general_protection },
1173 { 14, 0|4, __KERNEL_CS, (unsigned long)page_fault },
1174 { 15, 0, __KERNEL_CS, (unsigned long)fixup_4gb_segment },
1175 { 16, 0, __KERNEL_CS, (unsigned long)coprocessor_error },
1176 { 17, 0, __KERNEL_CS, (unsigned long)alignment_check },
1177 #ifdef CONFIG_X86_MCE
1178 { 18, 0, __KERNEL_CS, (unsigned long)machine_check },
1180 { 19, 0, __KERNEL_CS, (unsigned long)simd_coprocessor_error },
1181 { SYSCALL_VECTOR, 3, __KERNEL_CS, (unsigned long)system_call },
1185 void __init trap_init(void)
1187 HYPERVISOR_set_trap_table(trap_table);
1191 * Verify that the FXSAVE/FXRSTOR data will be 16-byte aligned.
1192 * Generates a compile-time "error: zero width for bit-field" if
1193 * the alignment is wrong.
1195 struct fxsrAlignAssert {
1196 int _:!(offsetof(struct task_struct,
1197 thread.i387.fxsave) & 15);
1200 printk(KERN_INFO "Enabling fast FPU save and restore... ");
1201 set_in_cr4(X86_CR4_OSFXSR);
1205 printk(KERN_INFO "Enabling unmasked SIMD FPU exception "
1207 set_in_cr4(X86_CR4_OSXMMEXCPT);
1212 * Should be a barrier for any external CPU state.
1217 void smp_trap_init(trap_info_t *trap_ctxt)
1219 trap_info_t *t = trap_table;
1221 for (t = trap_table; t->address; t++) {
1222 trap_ctxt[t->vector].flags = t->flags;
1223 trap_ctxt[t->vector].cs = t->cs;
1224 trap_ctxt[t->vector].address = t->address;
1228 static int __init kstack_setup(char *s)
1230 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
1233 __setup("kstack=", kstack_setup);