2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5 * Pentium III FXSR, SSE support
6 * Gareth Hughes <gareth@valinux.com>, May 2000
10 * 'Traps.c' handles hardware traps and faults after we have saved some
13 #include <linux/moduleparam.h>
14 #include <linux/interrupt.h>
15 #include <linux/kallsyms.h>
16 #include <linux/spinlock.h>
17 #include <linux/kprobes.h>
18 #include <linux/uaccess.h>
19 #include <linux/utsname.h>
20 #include <linux/kdebug.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/ptrace.h>
24 #include <linux/string.h>
25 #include <linux/unwind.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/kexec.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/init.h>
32 #include <linux/bug.h>
33 #include <linux/nmi.h>
35 #include <linux/smp.h>
38 #if defined(CONFIG_EDAC)
39 #include <linux/edac.h>
42 #include <asm/stacktrace.h>
43 #include <asm/processor.h>
44 #include <asm/debugreg.h>
45 #include <asm/atomic.h>
46 #include <asm/system.h>
47 #include <asm/unwind.h>
50 #include <asm/pgalloc.h>
51 #include <asm/proto.h>
53 #include <asm/traps.h>
55 #include <mach_traps.h>
57 static int ignore_nmis;
59 static inline void conditional_sti(struct pt_regs *regs)
61 if (regs->flags & X86_EFLAGS_IF)
65 static inline void preempt_conditional_sti(struct pt_regs *regs)
68 if (regs->flags & X86_EFLAGS_IF)
72 static inline void preempt_conditional_cli(struct pt_regs *regs)
74 if (regs->flags & X86_EFLAGS_IF)
76 /* Make sure to not schedule here because we could be running
77 on an exception stack. */
82 do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
83 long error_code, siginfo_t *info)
85 struct task_struct *tsk = current;
91 * We want error_code and trap_no set for userspace faults and
92 * kernelspace faults which result in die(), but not
93 * kernelspace faults which are fixed up. die() gives the
94 * process no chance to handle the signal and notice the
95 * kernel fault information, so that won't result in polluting
96 * the information about previously queued, but not yet
97 * delivered, faults. See also do_general_protection below.
99 tsk->thread.error_code = error_code;
100 tsk->thread.trap_no = trapnr;
102 if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
103 printk_ratelimit()) {
105 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
106 tsk->comm, tsk->pid, str,
107 regs->ip, regs->sp, error_code);
108 print_vma_addr(" in ", regs->ip);
113 force_sig_info(signr, info, tsk);
115 force_sig(signr, tsk);
119 if (!fixup_exception(regs)) {
120 tsk->thread.error_code = error_code;
121 tsk->thread.trap_no = trapnr;
122 die(str, regs, error_code);
127 #define DO_ERROR(trapnr, signr, str, name) \
128 dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
130 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
133 conditional_sti(regs); \
134 do_trap(trapnr, signr, str, regs, error_code, NULL); \
137 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
138 dotraplinkage void do_##name(struct pt_regs *regs, long error_code) \
141 info.si_signo = signr; \
143 info.si_code = sicode; \
144 info.si_addr = (void __user *)siaddr; \
145 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
148 conditional_sti(regs); \
149 do_trap(trapnr, signr, str, regs, error_code, &info); \
152 DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
153 DO_ERROR(4, SIGSEGV, "overflow", overflow)
154 DO_ERROR(5, SIGSEGV, "bounds", bounds)
155 DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
156 DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
157 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
158 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
159 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
161 /* Runs on IST stack */
162 dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code)
164 if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
165 12, SIGBUS) == NOTIFY_STOP)
167 preempt_conditional_sti(regs);
168 do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
169 preempt_conditional_cli(regs);
172 dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
174 static const char str[] = "double fault";
175 struct task_struct *tsk = current;
177 /* Return not checked because double check cannot be ignored */
178 notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
180 tsk->thread.error_code = error_code;
181 tsk->thread.trap_no = 8;
183 /* This is always a kernel trap and never fixable (and thus must
186 die(str, regs, error_code);
189 dotraplinkage void __kprobes
190 do_general_protection(struct pt_regs *regs, long error_code)
192 struct task_struct *tsk;
194 conditional_sti(regs);
197 if (!user_mode(regs))
200 tsk->thread.error_code = error_code;
201 tsk->thread.trap_no = 13;
203 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
204 printk_ratelimit()) {
206 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
208 regs->ip, regs->sp, error_code);
209 print_vma_addr(" in ", regs->ip);
213 force_sig(SIGSEGV, tsk);
217 if (fixup_exception(regs))
220 tsk->thread.error_code = error_code;
221 tsk->thread.trap_no = 13;
222 if (notify_die(DIE_GPF, "general protection fault", regs,
223 error_code, 13, SIGSEGV) == NOTIFY_STOP)
225 die("general protection fault", regs, error_code);
228 static notrace __kprobes void
229 mem_parity_error(unsigned char reason, struct pt_regs *regs)
231 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
233 printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
235 #if defined(CONFIG_EDAC)
236 if (edac_handler_set()) {
237 edac_atomic_assert_error();
242 if (panic_on_unrecovered_nmi)
243 panic("NMI: Not continuing");
245 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
247 /* Clear and disable the memory parity error line. */
248 reason = (reason & 0xf) | 4;
252 static notrace __kprobes void
253 io_check_error(unsigned char reason, struct pt_regs *regs)
257 printk(KERN_EMERG "NMI: IOCK error (debug interrupt?)\n");
258 show_registers(regs);
260 /* Re-enable the IOCK line, wait for a few seconds */
261 reason = (reason & 0xf) | 8;
272 static notrace __kprobes void
273 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
275 if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
278 printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
280 printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
282 if (panic_on_unrecovered_nmi)
283 panic("NMI: Not continuing");
285 printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
288 /* Runs on IST stack. This code must keep interrupts off all the time.
289 Nested NMIs are prevented by the CPU. */
290 asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
292 unsigned char reason = 0;
295 cpu = smp_processor_id();
297 /* Only the BSP gets external NMIs from the system. */
299 reason = get_nmi_reason();
301 if (!(reason & 0xc0)) {
302 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
306 * Ok, so this is none of the documented NMI sources,
307 * so it must be the NMI watchdog.
309 if (nmi_watchdog_tick(regs, reason))
311 if (!do_nmi_callback(regs, cpu))
312 unknown_nmi_error(reason, regs);
316 if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
319 /* AK: following checks seem to be broken on modern chipsets. FIXME */
321 mem_parity_error(reason, regs);
323 io_check_error(reason, regs);
326 dotraplinkage notrace __kprobes void
327 do_nmi(struct pt_regs *regs, long error_code)
331 add_pda(__nmi_count, 1);
334 default_do_nmi(regs);
345 void restart_nmi(void)
351 /* runs on IST stack. */
352 dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
354 if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
358 preempt_conditional_sti(regs);
359 do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
360 preempt_conditional_cli(regs);
363 /* Help handler running on IST stack to switch back to user stack
364 for scheduling or signal handling. The actual stack switch is done in
366 asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
368 struct pt_regs *regs = eregs;
369 /* Did already sync */
370 if (eregs == (struct pt_regs *)eregs->sp)
372 /* Exception from user space */
373 else if (user_mode(eregs))
374 regs = task_pt_regs(current);
375 /* Exception from kernel and interrupts are enabled. Move to
376 kernel process stack. */
377 else if (eregs->flags & X86_EFLAGS_IF)
378 regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
384 /* runs on IST stack. */
385 dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code)
387 struct task_struct *tsk = current;
388 unsigned long condition;
391 get_debugreg(condition, 6);
394 * The processor cleared BTF, so don't mark that we need it set.
396 clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
397 tsk->thread.debugctlmsr = 0;
399 if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
400 SIGTRAP) == NOTIFY_STOP)
403 /* It's safe to allow irq's after DR6 has been saved */
404 preempt_conditional_sti(regs);
406 /* Mask out spurious debug traps due to lazy DR7 setting */
407 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
408 if (!tsk->thread.debugreg7)
412 /* Save debug status register where ptrace can see it */
413 tsk->thread.debugreg6 = condition;
416 * Single-stepping through TF: make sure we ignore any events in
417 * kernel space (but re-enable TF when returning to user mode).
419 if (condition & DR_STEP) {
420 if (!user_mode(regs))
421 goto clear_TF_reenable;
424 si_code = get_si_code(condition);
425 /* Ok, finally something we can handle */
426 send_sigtrap(tsk, regs, error_code, si_code);
429 * Disable additional traps. They'll be re-enabled when
430 * the signal is delivered.
434 preempt_conditional_cli(regs);
438 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
439 regs->flags &= ~X86_EFLAGS_TF;
440 preempt_conditional_cli(regs);
444 static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
446 if (fixup_exception(regs))
449 notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
450 /* Illegal floating point operation in the kernel */
451 current->thread.trap_no = trapnr;
457 * Note that we play around with the 'TS' bit in an attempt to get
458 * the correct behaviour even in the presence of the asynchronous
461 void math_error(void __user *ip)
463 struct task_struct *task;
465 unsigned short cwd, swd;
468 * Save the info for the exception handler and clear the error.
472 task->thread.trap_no = 16;
473 task->thread.error_code = 0;
474 info.si_signo = SIGFPE;
476 info.si_code = __SI_FAULT;
479 * (~cwd & swd) will mask out exceptions that are not set to unmasked
480 * status. 0x3f is the exception bits in these regs, 0x200 is the
481 * C1 reg you need in case of a stack fault, 0x040 is the stack
482 * fault bit. We should only be taking one exception at a time,
483 * so if this combination doesn't produce any single exception,
484 * then we have a bad program that isn't synchronizing its FPU usage
485 * and it will suffer the consequences since we won't be able to
486 * fully reproduce the context of the exception
488 cwd = get_fpu_cwd(task);
489 swd = get_fpu_swd(task);
490 switch (swd & ~cwd & 0x3f) {
491 case 0x000: /* No unmasked exception */
492 default: /* Multiple exceptions */
494 case 0x001: /* Invalid Op */
496 * swd & 0x240 == 0x040: Stack Underflow
497 * swd & 0x240 == 0x240: Stack Overflow
498 * User must clear the SF bit (0x40) if set
500 info.si_code = FPE_FLTINV;
502 case 0x002: /* Denormalize */
503 case 0x010: /* Underflow */
504 info.si_code = FPE_FLTUND;
506 case 0x004: /* Zero Divide */
507 info.si_code = FPE_FLTDIV;
509 case 0x008: /* Overflow */
510 info.si_code = FPE_FLTOVF;
512 case 0x020: /* Precision */
513 info.si_code = FPE_FLTRES;
516 force_sig_info(SIGFPE, &info, task);
519 dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
521 conditional_sti(regs);
522 if (!user_mode(regs) &&
523 kernel_math_error(regs, "kernel x87 math error", 16))
525 math_error((void __user *)regs->ip);
528 asmlinkage void bad_intr(void)
530 printk("bad interrupt");
533 static void simd_math_error(void __user *ip)
535 struct task_struct *task;
537 unsigned short mxcsr;
540 * Save the info for the exception handler and clear the error.
544 task->thread.trap_no = 19;
545 task->thread.error_code = 0;
546 info.si_signo = SIGFPE;
548 info.si_code = __SI_FAULT;
551 * The SIMD FPU exceptions are handled a little differently, as there
552 * is only a single status/control register. Thus, to determine which
553 * unmasked exception was caught we must mask the exception mask bits
554 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
556 mxcsr = get_fpu_mxcsr(task);
557 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
561 case 0x001: /* Invalid Op */
562 info.si_code = FPE_FLTINV;
564 case 0x002: /* Denormalize */
565 case 0x010: /* Underflow */
566 info.si_code = FPE_FLTUND;
568 case 0x004: /* Zero Divide */
569 info.si_code = FPE_FLTDIV;
571 case 0x008: /* Overflow */
572 info.si_code = FPE_FLTOVF;
574 case 0x020: /* Precision */
575 info.si_code = FPE_FLTRES;
578 force_sig_info(SIGFPE, &info, task);
582 do_simd_coprocessor_error(struct pt_regs *regs, long error_code)
584 conditional_sti(regs);
585 if (!user_mode(regs) &&
586 kernel_math_error(regs, "kernel simd math error", 19))
588 simd_math_error((void __user *)regs->ip);
592 do_spurious_interrupt_bug(struct pt_regs *regs, long error_code)
596 asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
600 asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
605 * 'math_state_restore()' saves the current math information in the
606 * old math state array, and gets the new ones from the current task
608 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
609 * Don't touch unless you *really* know how it works.
611 asmlinkage void math_state_restore(void)
613 struct thread_info *thread = current_thread_info();
614 struct task_struct *tsk = thread->task;
616 if (!tsk_used_math(tsk)) {
619 * does a slab alloc which can sleep
625 do_group_exit(SIGKILL);
631 clts(); /* Allow maths ops (or we recurse) */
633 * Paranoid restore. send a SIGSEGV if we fail to restore the state.
635 if (unlikely(restore_fpu_checking(tsk))) {
637 force_sig(SIGSEGV, tsk);
640 thread->status |= TS_USEDFPU; /* So we fnsave on switch_to() */
643 EXPORT_SYMBOL_GPL(math_state_restore);
645 dotraplinkage void __kprobes
646 do_device_not_available(struct pt_regs *regs, long error)
648 math_state_restore();
651 void __init trap_init(void)
653 set_intr_gate(0, ÷_error);
654 set_intr_gate_ist(1, &debug, DEBUG_STACK);
655 set_intr_gate_ist(2, &nmi, NMI_STACK);
656 /* int3 can be called from all */
657 set_system_intr_gate_ist(3, &int3, DEBUG_STACK);
658 /* int4 can be called from all */
659 set_system_intr_gate(4, &overflow);
660 set_intr_gate(5, &bounds);
661 set_intr_gate(6, &invalid_op);
662 set_intr_gate(7, &device_not_available);
663 set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
664 set_intr_gate(9, &coprocessor_segment_overrun);
665 set_intr_gate(10, &invalid_TSS);
666 set_intr_gate(11, &segment_not_present);
667 set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
668 set_intr_gate(13, &general_protection);
669 set_intr_gate(14, &page_fault);
670 set_intr_gate(15, &spurious_interrupt_bug);
671 set_intr_gate(16, &coprocessor_error);
672 set_intr_gate(17, &alignment_check);
673 #ifdef CONFIG_X86_MCE
674 set_intr_gate_ist(18, &machine_check, MCE_STACK);
676 set_intr_gate(19, &simd_coprocessor_error);
678 #ifdef CONFIG_IA32_EMULATION
679 set_system_intr_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
682 * Should be a barrier for any external CPU state: