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
10 * $Id: traps.c,v 1.36 2002/03/24 11:09:10 ak Exp $
14 * 'Traps.c' handles hardware traps and faults after we have saved some
17 #include <linux/config.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/ptrace.h>
23 #include <linux/timer.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
35 #include <asm/atomic.h>
36 #include <asm/debugreg.h>
39 #include <asm/kdebug.h>
40 #include <asm/processor.h>
43 #include <asm/pgalloc.h>
45 #include <asm/proto.h>
47 #include <linux/irq.h>
49 extern struct gate_struct idt_table[256];
51 asmlinkage void divide_error(void);
52 asmlinkage void debug(void);
53 asmlinkage void nmi(void);
54 asmlinkage void int3(void);
55 asmlinkage void overflow(void);
56 asmlinkage void bounds(void);
57 asmlinkage void invalid_op(void);
58 asmlinkage void device_not_available(void);
59 asmlinkage void double_fault(void);
60 asmlinkage void coprocessor_segment_overrun(void);
61 asmlinkage void invalid_TSS(void);
62 asmlinkage void segment_not_present(void);
63 asmlinkage void stack_segment(void);
64 asmlinkage void general_protection(void);
65 asmlinkage void page_fault(void);
66 asmlinkage void coprocessor_error(void);
67 asmlinkage void simd_coprocessor_error(void);
68 asmlinkage void reserved(void);
69 asmlinkage void alignment_check(void);
70 asmlinkage void machine_check(void);
71 asmlinkage void spurious_interrupt_bug(void);
72 asmlinkage void call_debug(void);
74 struct notifier_block *die_chain;
76 static inline void conditional_sti(struct pt_regs *regs)
78 if (regs->eflags & X86_EFLAGS_IF)
82 static int kstack_depth_to_print = 10;
84 #ifdef CONFIG_KALLSYMS
85 #include <linux/kallsyms.h>
86 int printk_address(unsigned long address)
88 unsigned long offset = 0, symsize;
94 symname = kallsyms_lookup(address, &symsize, &offset, &modname, namebuf);
96 return printk("[<%016lx>]", address);
99 return printk("<%016lx>{%s%s%s%s%+ld}",
100 address,delim,modname,delim,symname,offset);
103 int printk_address(unsigned long address)
105 return printk("[<%016lx>]", address);
109 unsigned long *in_exception_stack(int cpu, unsigned long stack)
112 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
113 unsigned long end = init_tss[cpu].ist[k] + EXCEPTION_STKSZ;
115 if (stack >= init_tss[cpu].ist[k] && stack <= end)
116 return (unsigned long *)end;
122 * x86-64 can have upto three kernel stacks:
125 * severe exception (double fault, nmi, stack fault) hardware stack
126 * Check and process them in order.
129 void show_trace(unsigned long *stack)
132 unsigned long *irqstack, *irqstack_end, *estack_end;
133 const int cpu = safe_smp_processor_id();
136 printk("\nCall Trace:");
139 estack_end = in_exception_stack(cpu, (unsigned long)stack);
141 while (stack < estack_end) {
143 if (kernel_text_address(addr)) {
144 i += printk_address(addr);
152 i += printk(" <EOE> ");
154 stack = (unsigned long *) estack_end[-2];
157 irqstack_end = (unsigned long *) (cpu_pda[cpu].irqstackptr);
158 irqstack = (unsigned long *) (cpu_pda[cpu].irqstackptr - IRQSTACKSIZE + 64);
160 if (stack >= irqstack && stack < irqstack_end) {
162 while (stack < irqstack_end) {
165 * If the address is either in the text segment of the
166 * kernel, or in the region which contains vmalloc'ed
167 * memory, it *may* be the address of a calling
168 * routine; if so, print it so that someone tracing
169 * down the cause of the crash will be able to figure
170 * out the call path that was taken.
172 if (kernel_text_address(addr)) {
173 i += printk_address(addr);
181 stack = (unsigned long *) (irqstack_end[-1]);
186 while (((long) stack & (THREAD_SIZE-1)) != 0) {
188 if (kernel_text_address(addr)) {
189 i += printk_address(addr);
200 void show_stack(struct task_struct *tsk, unsigned long * rsp)
202 unsigned long *stack;
204 const int cpu = safe_smp_processor_id();
205 unsigned long *irqstack_end = (unsigned long *) (cpu_pda[cpu].irqstackptr);
206 unsigned long *irqstack = (unsigned long *) (cpu_pda[cpu].irqstackptr - IRQSTACKSIZE);
208 // debugging aid: "show_stack(NULL, NULL);" prints the
209 // back trace for this cpu.
213 rsp = (unsigned long *)tsk->thread.rsp;
215 rsp = (unsigned long *)&rsp;
219 for(i=0; i < kstack_depth_to_print; i++) {
220 if (stack >= irqstack && stack <= irqstack_end) {
221 if (stack == irqstack_end) {
222 stack = (unsigned long *) (irqstack_end[-1]);
226 if (((long) stack & (THREAD_SIZE-1)) == 0)
229 if (i && ((i % 4) == 0))
231 printk("%016lx ", *stack++);
233 show_trace((unsigned long *)rsp);
237 * The architecture-independent dump_stack generator
239 void dump_stack(void)
245 EXPORT_SYMBOL(dump_stack);
247 void show_registers(struct pt_regs *regs)
250 int in_kernel = (regs->cs & 3) == 0;
252 const int cpu = safe_smp_processor_id();
253 struct task_struct *cur = cpu_pda[cpu].pcurrent;
257 printk("CPU %d ", cpu);
259 printk("Process %s (pid: %d, stackpage=%08lx)\n",
260 cur->comm, cur->pid, 4096+(unsigned long)cur);
263 * When in-kernel, we also print out the stack and code at the
264 * time of the fault..
269 show_stack(NULL, (unsigned long*)rsp);
272 if(regs->rip < PAGE_OFFSET)
278 if(__get_user(c, &((unsigned char*)regs->rip)[i])) {
280 printk(" Bad RIP value.");
289 void handle_BUG(struct pt_regs *regs)
296 if (__copy_from_user(&f, (struct bug_frame *) regs->rip,
297 sizeof(struct bug_frame)))
299 if ((unsigned long)f.filename < __PAGE_OFFSET ||
300 f.ud2[0] != 0x0f || f.ud2[1] != 0x0b)
302 if (__get_user(tmp, f.filename))
303 f.filename = "unmapped filename";
304 printk("----------- [cut here ] --------- [please bite here ] ---------\n");
305 printk("Kernel BUG at %.50s:%d\n", f.filename, f.line);
308 void out_of_line_bug(void)
313 static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
314 static int die_owner = -1;
316 void oops_begin(void)
318 int cpu = safe_smp_processor_id();
319 /* racy, but better than risking deadlock. */
321 if (!spin_trylock(&die_lock)) {
322 if (cpu == die_owner)
323 /* nested oops. should stop eventually */;
325 spin_lock(&die_lock);
336 spin_unlock(&die_lock);
337 local_irq_enable(); /* make sure back scroll still works */
342 void __die(const char * str, struct pt_regs * regs, long err)
344 static int die_counter;
345 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
346 #ifdef CONFIG_PREEMPT
352 #ifdef CONFIG_DEBUG_PAGEALLOC
353 printk("DEBUG_PAGEALLOC");
356 notify_die(DIE_OOPS, (char *)str, regs, err, 255, SIGSEGV);
357 show_registers(regs);
358 /* Executive summary in case the oops scrolled away */
360 printk_address(regs->rip);
361 printk(" RSP <%016lx>\n", regs->rsp);
364 void die(const char * str, struct pt_regs * regs, long err)
368 __die(str, regs, err);
372 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
374 if (!(regs->eflags & VM_MASK) && (regs->cs == __KERNEL_CS))
378 static inline unsigned long get_cr2(void)
380 unsigned long address;
382 /* get the address */
383 __asm__("movq %%cr2,%0":"=r" (address));
387 static void do_trap(int trapnr, int signr, char *str,
388 struct pt_regs * regs, long error_code, siginfo_t *info)
390 conditional_sti(regs);
392 #ifdef CONFIG_CHECKING
395 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
396 rdmsrl(MSR_GS_BASE, gs);
397 if (gs != (unsigned long)pda) {
398 wrmsrl(MSR_GS_BASE, pda);
399 printk("%s: wrong gs %lx expected %p rip %lx\n", str, gs, pda,
405 if ((regs->cs & 3) != 0) {
406 struct task_struct *tsk = current;
408 if (exception_trace && unhandled_signal(tsk, signr))
410 "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
411 tsk->comm, tsk->pid, str,
412 regs->rip,regs->rsp,error_code);
414 tsk->thread.error_code = error_code;
415 tsk->thread.trap_no = trapnr;
417 force_sig_info(signr, info, tsk);
419 force_sig(signr, tsk);
426 const struct exception_table_entry *fixup;
427 fixup = search_exception_tables(regs->rip);
429 regs->rip = fixup->fixup;
431 die(str, regs, error_code);
436 #define DO_ERROR(trapnr, signr, str, name) \
437 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
439 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) == NOTIFY_BAD) \
441 do_trap(trapnr, signr, str, regs, error_code, NULL); \
444 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
445 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
448 info.si_signo = signr; \
450 info.si_code = sicode; \
451 info.si_addr = (void *)siaddr; \
452 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) == NOTIFY_BAD) \
454 do_trap(trapnr, signr, str, regs, error_code, &info); \
457 DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
458 DO_ERROR( 3, SIGTRAP, "int3", int3);
459 DO_ERROR( 4, SIGSEGV, "overflow", overflow)
460 DO_ERROR( 5, SIGSEGV, "bounds", bounds)
461 DO_ERROR_INFO( 6, SIGILL, "invalid operand", invalid_op, ILL_ILLOPN, regs->rip)
462 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
463 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
464 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
465 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
466 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, get_cr2())
467 DO_ERROR(18, SIGSEGV, "reserved", reserved)
469 #define DO_ERROR_STACK(trapnr, signr, str, name) \
470 asmlinkage void *do_##name(struct pt_regs * regs, long error_code) \
472 struct pt_regs *pr = ((struct pt_regs *)(current->thread.rsp0))-1; \
473 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) == NOTIFY_BAD) \
475 if (regs->cs & 3) { \
476 memcpy(pr, regs, sizeof(struct pt_regs)); \
479 do_trap(trapnr, signr, str, regs, error_code, NULL); \
483 DO_ERROR_STACK(12, SIGBUS, "stack segment", stack_segment)
484 DO_ERROR_STACK( 8, SIGSEGV, "double fault", double_fault)
486 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
488 conditional_sti(regs);
490 #ifdef CONFIG_CHECKING
493 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
494 rdmsrl(MSR_GS_BASE, gs);
495 if (gs != (unsigned long)pda) {
496 wrmsrl(MSR_GS_BASE, pda);
498 printk("general protection handler: wrong gs %lx expected %p\n", gs, pda);
504 if ((regs->cs & 3)!=0) {
505 struct task_struct *tsk = current;
507 if (exception_trace && unhandled_signal(tsk, SIGSEGV))
509 "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
511 regs->rip,regs->rsp,error_code);
513 tsk->thread.error_code = error_code;
514 tsk->thread.trap_no = 13;
515 force_sig(SIGSEGV, tsk);
521 const struct exception_table_entry *fixup;
522 fixup = search_exception_tables(regs->rip);
524 regs->rip = fixup->fixup;
527 notify_die(DIE_GPF, "general protection fault", regs, error_code,
529 die("general protection fault", regs, error_code);
533 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
535 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
536 printk("You probably have a hardware problem with your RAM chips\n");
538 /* Clear and disable the memory parity error line. */
539 reason = (reason & 0xf) | 4;
543 static void io_check_error(unsigned char reason, struct pt_regs * regs)
545 printk("NMI: IOCK error (debug interrupt?)\n");
546 show_registers(regs);
548 /* Re-enable the IOCK line, wait for a few seconds */
549 reason = (reason & 0xf) | 8;
556 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
557 { printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
558 printk("Dazed and confused, but trying to continue\n");
559 printk("Do you have a strange power saving mode enabled?\n");
562 asmlinkage void default_do_nmi(struct pt_regs * regs)
564 unsigned char reason = inb(0x61);
566 if (!(reason & 0xc0)) {
567 if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 0, SIGINT) == NOTIFY_BAD)
569 #ifdef CONFIG_X86_LOCAL_APIC
571 * Ok, so this is none of the documented NMI sources,
572 * so it must be the NMI watchdog.
574 if (nmi_watchdog > 0) {
575 nmi_watchdog_tick(regs,reason);
579 unknown_nmi_error(reason, regs);
582 if (notify_die(DIE_NMI, "nmi", regs, reason, 0, SIGINT) == NOTIFY_BAD)
585 mem_parity_error(reason, regs);
587 io_check_error(reason, regs);
590 * Reassert NMI in case it became active meanwhile
591 * as it's edge-triggered.
594 inb(0x71); /* dummy */
596 inb(0x71); /* dummy */
599 /* runs on IST stack. */
600 asmlinkage void *do_debug(struct pt_regs * regs, unsigned long error_code)
603 unsigned long condition;
604 struct task_struct *tsk = current;
607 pr = (struct pt_regs *)(current->thread.rsp0)-1;
609 memcpy(pr, regs, sizeof(struct pt_regs));
613 #ifdef CONFIG_CHECKING
615 /* RED-PEN interaction with debugger - could destroy gs */
617 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
618 rdmsrl(MSR_GS_BASE, gs);
619 if (gs != (unsigned long)pda) {
620 wrmsrl(MSR_GS_BASE, pda);
621 printk("debug handler: wrong gs %lx expected %p\n", gs, pda);
626 asm("movq %%db6,%0" : "=r" (condition));
628 conditional_sti(regs);
630 /* Mask out spurious debug traps due to lazy DR7 setting */
631 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
632 if (!tsk->thread.debugreg7) {
637 tsk->thread.debugreg6 = condition;
639 /* Mask out spurious TF errors due to lazy TF clearing */
640 if (condition & DR_STEP) {
642 * The TF error should be masked out only if the current
643 * process is not traced and if the TRAP flag has been set
644 * previously by a tracing process (condition detected by
645 * the PT_DTRACE flag); remember that the i386 TRAP flag
646 * can be modified by the process itself in user mode,
647 * allowing programs to debug themselves without the ptrace()
650 if ((regs->cs & 3) == 0)
651 goto clear_TF_reenable;
652 if ((tsk->ptrace & (PT_DTRACE|PT_PTRACED)) == PT_DTRACE)
656 /* Ok, finally something we can handle */
657 tsk->thread.trap_no = 1;
658 tsk->thread.error_code = error_code;
659 info.si_signo = SIGTRAP;
661 info.si_code = TRAP_BRKPT;
662 if ((regs->cs & 3) == 0)
665 info.si_addr = (void *)regs->rip;
666 force_sig_info(SIGTRAP, &info, tsk);
668 asm volatile("movq %0,%%db7"::"r"(0UL));
669 notify_die(DIE_DEBUG, "debug", regs, condition, 1, SIGTRAP);
673 printk("clear_tf_reenable\n");
674 set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
677 /* RED-PEN could cause spurious errors */
678 if (notify_die(DIE_DEBUG, "debug2", regs, condition, 1, SIGTRAP)
680 regs->eflags &= ~TF_MASK;
685 * Note that we play around with the 'TS' bit in an attempt to get
686 * the correct behaviour even in the presence of the asynchronous
689 void math_error(void *rip)
691 struct task_struct * task;
693 unsigned short cwd, swd;
695 * Save the info for the exception handler and clear the error.
699 task->thread.trap_no = 16;
700 task->thread.error_code = 0;
701 info.si_signo = SIGFPE;
703 info.si_code = __SI_FAULT;
706 * (~cwd & swd) will mask out exceptions that are not set to unmasked
707 * status. 0x3f is the exception bits in these regs, 0x200 is the
708 * C1 reg you need in case of a stack fault, 0x040 is the stack
709 * fault bit. We should only be taking one exception at a time,
710 * so if this combination doesn't produce any single exception,
711 * then we have a bad program that isn't synchronizing its FPU usage
712 * and it will suffer the consequences since we won't be able to
713 * fully reproduce the context of the exception
715 cwd = get_fpu_cwd(task);
716 swd = get_fpu_swd(task);
717 switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
721 case 0x001: /* Invalid Op */
722 case 0x041: /* Stack Fault */
723 case 0x241: /* Stack Fault | Direction */
724 info.si_code = FPE_FLTINV;
726 case 0x002: /* Denormalize */
727 case 0x010: /* Underflow */
728 info.si_code = FPE_FLTUND;
730 case 0x004: /* Zero Divide */
731 info.si_code = FPE_FLTDIV;
733 case 0x008: /* Overflow */
734 info.si_code = FPE_FLTOVF;
736 case 0x020: /* Precision */
737 info.si_code = FPE_FLTRES;
740 force_sig_info(SIGFPE, &info, task);
743 asmlinkage void do_coprocessor_error(struct pt_regs * regs)
745 conditional_sti(regs);
746 math_error((void *)regs->rip);
749 asmlinkage void bad_intr(void)
751 printk("bad interrupt");
754 static inline void simd_math_error(void *rip)
756 struct task_struct * task;
758 unsigned short mxcsr;
761 * Save the info for the exception handler and clear the error.
765 task->thread.trap_no = 19;
766 task->thread.error_code = 0;
767 info.si_signo = SIGFPE;
769 info.si_code = __SI_FAULT;
772 * The SIMD FPU exceptions are handled a little differently, as there
773 * is only a single status/control register. Thus, to determine which
774 * unmasked exception was caught we must mask the exception mask bits
775 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
777 mxcsr = get_fpu_mxcsr(task);
778 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
782 case 0x001: /* Invalid Op */
783 info.si_code = FPE_FLTINV;
785 case 0x002: /* Denormalize */
786 case 0x010: /* Underflow */
787 info.si_code = FPE_FLTUND;
789 case 0x004: /* Zero Divide */
790 info.si_code = FPE_FLTDIV;
792 case 0x008: /* Overflow */
793 info.si_code = FPE_FLTOVF;
795 case 0x020: /* Precision */
796 info.si_code = FPE_FLTRES;
799 force_sig_info(SIGFPE, &info, task);
802 asmlinkage void do_simd_coprocessor_error(struct pt_regs * regs)
804 conditional_sti(regs);
805 simd_math_error((void *)regs->rip);
808 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
813 * 'math_state_restore()' saves the current math information in the
814 * old math state array, and gets the new ones from the current task
816 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
817 * Don't touch unless you *really* know how it works.
819 asmlinkage void math_state_restore(void)
821 struct task_struct *me = current;
822 clts(); /* Allow maths ops (or we recurse) */
826 restore_fpu_checking(&me->thread.i387.fxsave);
827 me->thread_info->status |= TS_USEDFPU;
830 void do_call_debug(struct pt_regs *regs)
832 notify_die(DIE_CALL, "debug call", regs, 0, 255, SIGINT);
835 void __init trap_init(void)
837 set_intr_gate(0,÷_error);
838 set_intr_gate_ist(1,&debug,DEBUG_STACK);
839 set_intr_gate_ist(2,&nmi,NMI_STACK);
840 set_system_gate(3,&int3); /* int3-5 can be called from all */
841 set_system_gate(4,&overflow);
842 set_system_gate(5,&bounds);
843 set_intr_gate(6,&invalid_op);
844 set_intr_gate(7,&device_not_available);
845 set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
846 set_intr_gate(9,&coprocessor_segment_overrun);
847 set_intr_gate(10,&invalid_TSS);
848 set_intr_gate(11,&segment_not_present);
849 set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
850 set_intr_gate(13,&general_protection);
851 set_intr_gate(14,&page_fault);
852 set_intr_gate(15,&spurious_interrupt_bug);
853 set_intr_gate(16,&coprocessor_error);
854 set_intr_gate(17,&alignment_check);
855 set_intr_gate_ist(18,&machine_check, MCE_STACK);
856 set_intr_gate(19,&simd_coprocessor_error);
858 #ifdef CONFIG_IA32_EMULATION
859 set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
862 set_intr_gate(KDB_VECTOR, call_debug);
865 * Should be a barrier for any external CPU state.
871 /* Actual parsing is done early in setup.c. */
872 static int __init oops_dummy(char *s)
877 __setup("oops=", oops_dummy);