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
)
255 printk("NMI: IOCK error (debug interrupt?)\n");
256 show_registers(regs
);
258 /* Re-enable the IOCK line, wait for a few seconds */
259 reason
= (reason
& 0xf) | 8;
266 static notrace __kprobes
void
267 unknown_nmi_error(unsigned char reason
, struct pt_regs
*regs
)
269 if (notify_die(DIE_NMIUNKNOWN
, "nmi", regs
, reason
, 2, SIGINT
) ==
272 printk(KERN_EMERG
"Uhhuh. NMI received for unknown reason %02x.\n",
274 printk(KERN_EMERG
"Do you have a strange power saving mode enabled?\n");
276 if (panic_on_unrecovered_nmi
)
277 panic("NMI: Not continuing");
279 printk(KERN_EMERG
"Dazed and confused, but trying to continue\n");
282 /* Runs on IST stack. This code must keep interrupts off all the time.
283 Nested NMIs are prevented by the CPU. */
284 asmlinkage notrace __kprobes
void default_do_nmi(struct pt_regs
*regs
)
286 unsigned char reason
= 0;
289 cpu
= smp_processor_id();
291 /* Only the BSP gets external NMIs from the system. */
293 reason
= get_nmi_reason();
295 if (!(reason
& 0xc0)) {
296 if (notify_die(DIE_NMI_IPI
, "nmi_ipi", regs
, reason
, 2, SIGINT
)
300 * Ok, so this is none of the documented NMI sources,
301 * so it must be the NMI watchdog.
303 if (nmi_watchdog_tick(regs
, reason
))
305 if (!do_nmi_callback(regs
, cpu
))
306 unknown_nmi_error(reason
, regs
);
310 if (notify_die(DIE_NMI
, "nmi", regs
, reason
, 2, SIGINT
) == NOTIFY_STOP
)
313 /* AK: following checks seem to be broken on modern chipsets. FIXME */
315 mem_parity_error(reason
, regs
);
317 io_check_error(reason
, regs
);
320 dotraplinkage notrace __kprobes
void
321 do_nmi(struct pt_regs
*regs
, long error_code
)
325 add_pda(__nmi_count
, 1);
328 default_do_nmi(regs
);
339 void restart_nmi(void)
345 /* runs on IST stack. */
346 dotraplinkage
void __kprobes
do_int3(struct pt_regs
*regs
, long error_code
)
348 if (notify_die(DIE_INT3
, "int3", regs
, error_code
, 3, SIGTRAP
)
352 preempt_conditional_sti(regs
);
353 do_trap(3, SIGTRAP
, "int3", regs
, error_code
, NULL
);
354 preempt_conditional_cli(regs
);
357 /* Help handler running on IST stack to switch back to user stack
358 for scheduling or signal handling. The actual stack switch is done in
360 asmlinkage __kprobes
struct pt_regs
*sync_regs(struct pt_regs
*eregs
)
362 struct pt_regs
*regs
= eregs
;
363 /* Did already sync */
364 if (eregs
== (struct pt_regs
*)eregs
->sp
)
366 /* Exception from user space */
367 else if (user_mode(eregs
))
368 regs
= task_pt_regs(current
);
369 /* Exception from kernel and interrupts are enabled. Move to
370 kernel process stack. */
371 else if (eregs
->flags
& X86_EFLAGS_IF
)
372 regs
= (struct pt_regs
*)(eregs
->sp
-= sizeof(struct pt_regs
));
378 /* runs on IST stack. */
379 dotraplinkage
void __kprobes
do_debug(struct pt_regs
*regs
, long error_code
)
381 struct task_struct
*tsk
= current
;
382 unsigned long condition
;
385 get_debugreg(condition
, 6);
388 * The processor cleared BTF, so don't mark that we need it set.
390 clear_tsk_thread_flag(tsk
, TIF_DEBUGCTLMSR
);
391 tsk
->thread
.debugctlmsr
= 0;
393 if (notify_die(DIE_DEBUG
, "debug", regs
, condition
, error_code
,
394 SIGTRAP
) == NOTIFY_STOP
)
397 /* It's safe to allow irq's after DR6 has been saved */
398 preempt_conditional_sti(regs
);
400 /* Mask out spurious debug traps due to lazy DR7 setting */
401 if (condition
& (DR_TRAP0
|DR_TRAP1
|DR_TRAP2
|DR_TRAP3
)) {
402 if (!tsk
->thread
.debugreg7
)
406 /* Save debug status register where ptrace can see it */
407 tsk
->thread
.debugreg6
= condition
;
410 * Single-stepping through TF: make sure we ignore any events in
411 * kernel space (but re-enable TF when returning to user mode).
413 if (condition
& DR_STEP
) {
414 if (!user_mode(regs
))
415 goto clear_TF_reenable
;
418 si_code
= get_si_code(condition
);
419 /* Ok, finally something we can handle */
420 send_sigtrap(tsk
, regs
, error_code
, si_code
);
423 * Disable additional traps. They'll be re-enabled when
424 * the signal is delivered.
428 preempt_conditional_cli(regs
);
432 set_tsk_thread_flag(tsk
, TIF_SINGLESTEP
);
433 regs
->flags
&= ~X86_EFLAGS_TF
;
434 preempt_conditional_cli(regs
);
438 static int kernel_math_error(struct pt_regs
*regs
, const char *str
, int trapnr
)
440 if (fixup_exception(regs
))
443 notify_die(DIE_GPF
, str
, regs
, 0, trapnr
, SIGFPE
);
444 /* Illegal floating point operation in the kernel */
445 current
->thread
.trap_no
= trapnr
;
451 * Note that we play around with the 'TS' bit in an attempt to get
452 * the correct behaviour even in the presence of the asynchronous
455 void math_error(void __user
*ip
)
457 struct task_struct
*task
;
459 unsigned short cwd
, swd
;
462 * Save the info for the exception handler and clear the error.
466 task
->thread
.trap_no
= 16;
467 task
->thread
.error_code
= 0;
468 info
.si_signo
= SIGFPE
;
470 info
.si_code
= __SI_FAULT
;
473 * (~cwd & swd) will mask out exceptions that are not set to unmasked
474 * status. 0x3f is the exception bits in these regs, 0x200 is the
475 * C1 reg you need in case of a stack fault, 0x040 is the stack
476 * fault bit. We should only be taking one exception at a time,
477 * so if this combination doesn't produce any single exception,
478 * then we have a bad program that isn't synchronizing its FPU usage
479 * and it will suffer the consequences since we won't be able to
480 * fully reproduce the context of the exception
482 cwd
= get_fpu_cwd(task
);
483 swd
= get_fpu_swd(task
);
484 switch (swd
& ~cwd
& 0x3f) {
485 case 0x000: /* No unmasked exception */
486 default: /* Multiple exceptions */
488 case 0x001: /* Invalid Op */
490 * swd & 0x240 == 0x040: Stack Underflow
491 * swd & 0x240 == 0x240: Stack Overflow
492 * User must clear the SF bit (0x40) if set
494 info
.si_code
= FPE_FLTINV
;
496 case 0x002: /* Denormalize */
497 case 0x010: /* Underflow */
498 info
.si_code
= FPE_FLTUND
;
500 case 0x004: /* Zero Divide */
501 info
.si_code
= FPE_FLTDIV
;
503 case 0x008: /* Overflow */
504 info
.si_code
= FPE_FLTOVF
;
506 case 0x020: /* Precision */
507 info
.si_code
= FPE_FLTRES
;
510 force_sig_info(SIGFPE
, &info
, task
);
513 dotraplinkage
void do_coprocessor_error(struct pt_regs
*regs
, long error_code
)
515 conditional_sti(regs
);
516 if (!user_mode(regs
) &&
517 kernel_math_error(regs
, "kernel x87 math error", 16))
519 math_error((void __user
*)regs
->ip
);
522 asmlinkage
void bad_intr(void)
524 printk("bad interrupt");
527 static void simd_math_error(void __user
*ip
)
529 struct task_struct
*task
;
531 unsigned short mxcsr
;
534 * Save the info for the exception handler and clear the error.
538 task
->thread
.trap_no
= 19;
539 task
->thread
.error_code
= 0;
540 info
.si_signo
= SIGFPE
;
542 info
.si_code
= __SI_FAULT
;
545 * The SIMD FPU exceptions are handled a little differently, as there
546 * is only a single status/control register. Thus, to determine which
547 * unmasked exception was caught we must mask the exception mask bits
548 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
550 mxcsr
= get_fpu_mxcsr(task
);
551 switch (~((mxcsr
& 0x1f80) >> 7) & (mxcsr
& 0x3f)) {
555 case 0x001: /* Invalid Op */
556 info
.si_code
= FPE_FLTINV
;
558 case 0x002: /* Denormalize */
559 case 0x010: /* Underflow */
560 info
.si_code
= FPE_FLTUND
;
562 case 0x004: /* Zero Divide */
563 info
.si_code
= FPE_FLTDIV
;
565 case 0x008: /* Overflow */
566 info
.si_code
= FPE_FLTOVF
;
568 case 0x020: /* Precision */
569 info
.si_code
= FPE_FLTRES
;
572 force_sig_info(SIGFPE
, &info
, task
);
576 do_simd_coprocessor_error(struct pt_regs
*regs
, long error_code
)
578 conditional_sti(regs
);
579 if (!user_mode(regs
) &&
580 kernel_math_error(regs
, "kernel simd math error", 19))
582 simd_math_error((void __user
*)regs
->ip
);
586 do_spurious_interrupt_bug(struct pt_regs
*regs
, long error_code
)
590 asmlinkage
void __attribute__((weak
)) smp_thermal_interrupt(void)
594 asmlinkage
void __attribute__((weak
)) mce_threshold_interrupt(void)
599 * 'math_state_restore()' saves the current math information in the
600 * old math state array, and gets the new ones from the current task
602 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
603 * Don't touch unless you *really* know how it works.
605 asmlinkage
void math_state_restore(void)
607 struct task_struct
*me
= current
;
612 * does a slab alloc which can sleep
618 do_group_exit(SIGKILL
);
624 clts(); /* Allow maths ops (or we recurse) */
626 * Paranoid restore. send a SIGSEGV if we fail to restore the state.
628 if (unlikely(restore_fpu_checking(me
))) {
630 force_sig(SIGSEGV
, me
);
633 task_thread_info(me
)->status
|= TS_USEDFPU
;
636 EXPORT_SYMBOL_GPL(math_state_restore
);
638 dotraplinkage
void __kprobes
639 do_device_not_available(struct pt_regs
*regs
, long error
)
641 math_state_restore();
644 void __init
trap_init(void)
646 set_intr_gate(0, ÷_error
);
647 set_intr_gate_ist(1, &debug
, DEBUG_STACK
);
648 set_intr_gate_ist(2, &nmi
, NMI_STACK
);
649 /* int3 can be called from all */
650 set_system_gate_ist(3, &int3
, DEBUG_STACK
);
651 /* int4 can be called from all */
652 set_system_gate(4, &overflow
);
653 set_intr_gate(5, &bounds
);
654 set_intr_gate(6, &invalid_op
);
655 set_intr_gate(7, &device_not_available
);
656 set_intr_gate_ist(8, &double_fault
, DOUBLEFAULT_STACK
);
657 set_intr_gate(9, &coprocessor_segment_overrun
);
658 set_intr_gate(10, &invalid_TSS
);
659 set_intr_gate(11, &segment_not_present
);
660 set_intr_gate_ist(12, &stack_segment
, STACKFAULT_STACK
);
661 set_intr_gate(13, &general_protection
);
662 set_intr_gate(14, &page_fault
);
663 set_intr_gate(15, &spurious_interrupt_bug
);
664 set_intr_gate(16, &coprocessor_error
);
665 set_intr_gate(17, &alignment_check
);
666 #ifdef CONFIG_X86_MCE
667 set_intr_gate_ist(18, &machine_check
, MCE_STACK
);
669 set_intr_gate(19, &simd_coprocessor_error
);
671 #ifdef CONFIG_IA32_EMULATION
672 set_system_gate(IA32_SYSCALL_VECTOR
, ia32_syscall
);
675 * Should be a barrier for any external CPU state: