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>
36 #if defined(CONFIG_EDAC)
37 #include <linux/edac.h>
40 #include <asm/stacktrace.h>
41 #include <asm/processor.h>
42 #include <asm/debugreg.h>
43 #include <asm/atomic.h>
44 #include <asm/system.h>
45 #include <asm/unwind.h>
51 #include <asm/pgalloc.h>
52 #include <asm/proto.h>
55 #include <mach_traps.h>
57 asmlinkage
void divide_error(void);
58 asmlinkage
void debug(void);
59 asmlinkage
void nmi(void);
60 asmlinkage
void int3(void);
61 asmlinkage
void overflow(void);
62 asmlinkage
void bounds(void);
63 asmlinkage
void invalid_op(void);
64 asmlinkage
void device_not_available(void);
65 asmlinkage
void double_fault(void);
66 asmlinkage
void coprocessor_segment_overrun(void);
67 asmlinkage
void invalid_TSS(void);
68 asmlinkage
void segment_not_present(void);
69 asmlinkage
void stack_segment(void);
70 asmlinkage
void general_protection(void);
71 asmlinkage
void page_fault(void);
72 asmlinkage
void coprocessor_error(void);
73 asmlinkage
void simd_coprocessor_error(void);
74 asmlinkage
void alignment_check(void);
75 asmlinkage
void spurious_interrupt_bug(void);
76 asmlinkage
void machine_check(void);
78 int panic_on_unrecovered_nmi
;
79 int kstack_depth_to_print
= 12;
80 static unsigned int code_bytes
= 64;
81 static int ignore_nmis
;
82 static int die_counter
;
84 static inline void conditional_sti(struct pt_regs
*regs
)
86 if (regs
->flags
& X86_EFLAGS_IF
)
90 static inline void preempt_conditional_sti(struct pt_regs
*regs
)
93 if (regs
->flags
& X86_EFLAGS_IF
)
97 static inline void preempt_conditional_cli(struct pt_regs
*regs
)
99 if (regs
->flags
& X86_EFLAGS_IF
)
101 /* Make sure to not schedule here because we could be running
102 on an exception stack. */
106 void printk_address(unsigned long address
, int reliable
)
108 printk(" [<%016lx>] %s%pS\n", address
, reliable
? "": "? ", (void *) address
);
111 static unsigned long *in_exception_stack(unsigned cpu
, unsigned long stack
,
112 unsigned *usedp
, char **idp
)
114 static char ids
[][8] = {
115 [DEBUG_STACK
- 1] = "#DB",
116 [NMI_STACK
- 1] = "NMI",
117 [DOUBLEFAULT_STACK
- 1] = "#DF",
118 [STACKFAULT_STACK
- 1] = "#SS",
119 [MCE_STACK
- 1] = "#MC",
120 #if DEBUG_STKSZ > EXCEPTION_STKSZ
121 [N_EXCEPTION_STACKS
... N_EXCEPTION_STACKS
+ DEBUG_STKSZ
/ EXCEPTION_STKSZ
- 2] = "#DB[?]"
127 * Iterate over all exception stacks, and figure out whether
128 * 'stack' is in one of them:
130 for (k
= 0; k
< N_EXCEPTION_STACKS
; k
++) {
131 unsigned long end
= per_cpu(orig_ist
, cpu
).ist
[k
];
133 * Is 'stack' above this exception frame's end?
134 * If yes then skip to the next frame.
139 * Is 'stack' above this exception frame's start address?
140 * If yes then we found the right frame.
142 if (stack
>= end
- EXCEPTION_STKSZ
) {
144 * Make sure we only iterate through an exception
145 * stack once. If it comes up for the second time
146 * then there's something wrong going on - just
147 * break out and return NULL:
149 if (*usedp
& (1U << k
))
153 return (unsigned long *)end
;
156 * If this is a debug stack, and if it has a larger size than
157 * the usual exception stacks, then 'stack' might still
158 * be within the lower portion of the debug stack:
160 #if DEBUG_STKSZ > EXCEPTION_STKSZ
161 if (k
== DEBUG_STACK
- 1 && stack
>= end
- DEBUG_STKSZ
) {
162 unsigned j
= N_EXCEPTION_STACKS
- 1;
165 * Black magic. A large debug stack is composed of
166 * multiple exception stack entries, which we
167 * iterate through now. Dont look:
171 end
-= EXCEPTION_STKSZ
;
172 ids
[j
][4] = '1' + (j
- N_EXCEPTION_STACKS
);
173 } while (stack
< end
- EXCEPTION_STKSZ
);
174 if (*usedp
& (1U << j
))
178 return (unsigned long *)end
;
186 * x86-64 can have up to three kernel stacks:
189 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
192 static inline int valid_stack_ptr(struct thread_info
*tinfo
,
193 void *p
, unsigned int size
, void *end
)
197 if (p
< end
&& p
>= (end
-THREAD_SIZE
))
202 return p
> t
&& p
< t
+ THREAD_SIZE
- size
;
205 /* The form of the top of the frame on the stack */
207 struct stack_frame
*next_frame
;
208 unsigned long return_address
;
211 static inline unsigned long
212 print_context_stack(struct thread_info
*tinfo
,
213 unsigned long *stack
, unsigned long bp
,
214 const struct stacktrace_ops
*ops
, void *data
,
217 struct stack_frame
*frame
= (struct stack_frame
*)bp
;
219 while (valid_stack_ptr(tinfo
, stack
, sizeof(*stack
), end
)) {
223 if (__kernel_text_address(addr
)) {
224 if ((unsigned long) stack
== bp
+ 8) {
225 ops
->address(data
, addr
, 1);
226 frame
= frame
->next_frame
;
227 bp
= (unsigned long) frame
;
229 ops
->address(data
, addr
, bp
== 0);
237 void dump_trace(struct task_struct
*task
, struct pt_regs
*regs
,
238 unsigned long *stack
, unsigned long bp
,
239 const struct stacktrace_ops
*ops
, void *data
)
241 const unsigned cpu
= get_cpu();
242 unsigned long *irqstack_end
= (unsigned long*)cpu_pda(cpu
)->irqstackptr
;
244 struct thread_info
*tinfo
;
252 if (task
&& task
!= current
)
253 stack
= (unsigned long *)task
->thread
.sp
;
256 #ifdef CONFIG_FRAME_POINTER
258 if (task
== current
) {
259 /* Grab bp right from our regs */
260 asm("movq %%rbp, %0" : "=r" (bp
) :);
262 /* bp is the last reg pushed by switch_to */
263 bp
= *(unsigned long *) task
->thread
.sp
;
269 * Print function call entries in all stacks, starting at the
270 * current stack address. If the stacks consist of nested
273 tinfo
= task_thread_info(task
);
276 unsigned long *estack_end
;
277 estack_end
= in_exception_stack(cpu
, (unsigned long)stack
,
281 if (ops
->stack(data
, id
) < 0)
284 bp
= print_context_stack(tinfo
, stack
, bp
, ops
,
286 ops
->stack(data
, "<EOE>");
288 * We link to the next stack via the
289 * second-to-last pointer (index -2 to end) in the
292 stack
= (unsigned long *) estack_end
[-2];
296 unsigned long *irqstack
;
297 irqstack
= irqstack_end
-
298 (IRQSTACKSIZE
- 64) / sizeof(*irqstack
);
300 if (stack
>= irqstack
&& stack
< irqstack_end
) {
301 if (ops
->stack(data
, "IRQ") < 0)
303 bp
= print_context_stack(tinfo
, stack
, bp
,
304 ops
, data
, irqstack_end
);
306 * We link to the next stack (which would be
307 * the process stack normally) the last
308 * pointer (index -1 to end) in the IRQ stack:
310 stack
= (unsigned long *) (irqstack_end
[-1]);
312 ops
->stack(data
, "EOI");
320 * This handles the process stack:
322 bp
= print_context_stack(tinfo
, stack
, bp
, ops
, data
, NULL
);
325 EXPORT_SYMBOL(dump_trace
);
328 print_trace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
330 print_symbol(msg
, symbol
);
334 static void print_trace_warning(void *data
, char *msg
)
339 static int print_trace_stack(void *data
, char *name
)
341 printk(" <%s> ", name
);
345 static void print_trace_address(void *data
, unsigned long addr
, int reliable
)
347 touch_nmi_watchdog();
348 printk_address(addr
, reliable
);
351 static const struct stacktrace_ops print_trace_ops
= {
352 .warning
= print_trace_warning
,
353 .warning_symbol
= print_trace_warning_symbol
,
354 .stack
= print_trace_stack
,
355 .address
= print_trace_address
,
358 void show_trace(struct task_struct
*task
, struct pt_regs
*regs
,
359 unsigned long *stack
, unsigned long bp
)
361 printk("\nCall Trace:\n");
362 dump_trace(task
, regs
, stack
, bp
, &print_trace_ops
, NULL
);
367 _show_stack(struct task_struct
*task
, struct pt_regs
*regs
,
368 unsigned long *sp
, unsigned long bp
)
370 unsigned long *stack
;
372 const int cpu
= smp_processor_id();
373 unsigned long *irqstack_end
= (unsigned long *) (cpu_pda(cpu
)->irqstackptr
);
374 unsigned long *irqstack
= (unsigned long *) (cpu_pda(cpu
)->irqstackptr
- IRQSTACKSIZE
);
376 // debugging aid: "show_stack(NULL, NULL);" prints the
377 // back trace for this cpu.
381 sp
= (unsigned long *)task
->thread
.sp
;
383 sp
= (unsigned long *)&sp
;
387 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
388 if (stack
>= irqstack
&& stack
<= irqstack_end
) {
389 if (stack
== irqstack_end
) {
390 stack
= (unsigned long *) (irqstack_end
[-1]);
394 if (((long) stack
& (THREAD_SIZE
-1)) == 0)
397 if (i
&& ((i
% 4) == 0))
399 printk(" %016lx", *stack
++);
400 touch_nmi_watchdog();
402 show_trace(task
, regs
, sp
, bp
);
405 void show_stack(struct task_struct
*task
, unsigned long *sp
)
407 _show_stack(task
, NULL
, sp
, 0);
411 * The architecture-independent dump_stack generator
413 void dump_stack(void)
415 unsigned long bp
= 0;
418 #ifdef CONFIG_FRAME_POINTER
420 asm("movq %%rbp, %0" : "=r" (bp
):);
423 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
424 current
->pid
, current
->comm
, print_tainted(),
425 init_utsname()->release
,
426 (int)strcspn(init_utsname()->version
, " "),
427 init_utsname()->version
);
428 show_trace(NULL
, NULL
, &stack
, bp
);
431 EXPORT_SYMBOL(dump_stack
);
433 void show_registers(struct pt_regs
*regs
)
437 const int cpu
= smp_processor_id();
438 struct task_struct
*cur
= cpu_pda(cpu
)->pcurrent
;
441 printk("CPU %d ", cpu
);
443 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
444 cur
->comm
, cur
->pid
, task_thread_info(cur
), cur
);
447 * When in-kernel, we also print out the stack and code at the
448 * time of the fault..
450 if (!user_mode(regs
)) {
451 unsigned int code_prologue
= code_bytes
* 43 / 64;
452 unsigned int code_len
= code_bytes
;
457 _show_stack(NULL
, regs
, (unsigned long *)sp
, regs
->bp
);
460 printk(KERN_EMERG
"Code: ");
462 ip
= (u8
*)regs
->ip
- code_prologue
;
463 if (ip
< (u8
*)PAGE_OFFSET
|| probe_kernel_address(ip
, c
)) {
464 /* try starting at RIP */
466 code_len
= code_len
- code_prologue
+ 1;
468 for (i
= 0; i
< code_len
; i
++, ip
++) {
469 if (ip
< (u8
*)PAGE_OFFSET
||
470 probe_kernel_address(ip
, c
)) {
471 printk(" Bad RIP value.");
474 if (ip
== (u8
*)regs
->ip
)
475 printk("<%02x> ", c
);
483 int is_valid_bugaddr(unsigned long ip
)
487 if (__copy_from_user(&ud2
, (const void __user
*) ip
, sizeof(ud2
)))
490 return ud2
== 0x0b0f;
493 static raw_spinlock_t die_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
494 static int die_owner
= -1;
495 static unsigned int die_nest_count
;
497 unsigned __kprobes
long oops_begin(void)
504 /* racy, but better than risking deadlock. */
505 raw_local_irq_save(flags
);
506 cpu
= smp_processor_id();
507 if (!__raw_spin_trylock(&die_lock
)) {
508 if (cpu
== die_owner
)
509 /* nested oops. should stop eventually */;
511 __raw_spin_lock(&die_lock
);
520 void __kprobes
oops_end(unsigned long flags
, struct pt_regs
*regs
, int signr
)
526 /* Nest count reaches zero, release the lock. */
527 __raw_spin_unlock(&die_lock
);
528 raw_local_irq_restore(flags
);
534 panic("Fatal exception");
539 int __kprobes
__die(const char *str
, struct pt_regs
*regs
, long err
)
541 printk(KERN_EMERG
"%s: %04lx [%u] ", str
, err
& 0xffff, ++die_counter
);
542 #ifdef CONFIG_PREEMPT
548 #ifdef CONFIG_DEBUG_PAGEALLOC
549 printk("DEBUG_PAGEALLOC");
552 if (notify_die(DIE_OOPS
, str
, regs
, err
,
553 current
->thread
.trap_no
, SIGSEGV
) == NOTIFY_STOP
)
556 show_registers(regs
);
557 add_taint(TAINT_DIE
);
558 /* Executive summary in case the oops scrolled away */
559 printk(KERN_ALERT
"RIP ");
560 printk_address(regs
->ip
, 1);
561 printk(" RSP <%016lx>\n", regs
->sp
);
562 if (kexec_should_crash(current
))
567 void die(const char *str
, struct pt_regs
*regs
, long err
)
569 unsigned long flags
= oops_begin();
571 if (!user_mode(regs
))
572 report_bug(regs
->ip
, regs
);
574 if (__die(str
, regs
, err
))
576 oops_end(flags
, regs
, SIGSEGV
);
579 notrace __kprobes
void
580 die_nmi(char *str
, struct pt_regs
*regs
, int do_panic
)
584 if (notify_die(DIE_NMIWATCHDOG
, str
, regs
, 0, 2, SIGINT
) == NOTIFY_STOP
)
587 flags
= oops_begin();
589 * We are in trouble anyway, lets at least try
590 * to get a message out.
592 printk(KERN_EMERG
"%s", str
);
593 printk(" on CPU%d, ip %08lx, registers:\n",
594 smp_processor_id(), regs
->ip
);
595 show_registers(regs
);
596 if (kexec_should_crash(current
))
598 if (do_panic
|| panic_on_oops
)
599 panic("Non maskable interrupt");
600 oops_end(flags
, NULL
, SIGBUS
);
606 static void __kprobes
607 do_trap(int trapnr
, int signr
, char *str
, struct pt_regs
*regs
,
608 long error_code
, siginfo_t
*info
)
610 struct task_struct
*tsk
= current
;
612 if (!user_mode(regs
))
616 * We want error_code and trap_no set for userspace faults and
617 * kernelspace faults which result in die(), but not
618 * kernelspace faults which are fixed up. die() gives the
619 * process no chance to handle the signal and notice the
620 * kernel fault information, so that won't result in polluting
621 * the information about previously queued, but not yet
622 * delivered, faults. See also do_general_protection below.
624 tsk
->thread
.error_code
= error_code
;
625 tsk
->thread
.trap_no
= trapnr
;
627 if (show_unhandled_signals
&& unhandled_signal(tsk
, signr
) &&
628 printk_ratelimit()) {
630 "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
631 tsk
->comm
, tsk
->pid
, str
,
632 regs
->ip
, regs
->sp
, error_code
);
633 print_vma_addr(" in ", regs
->ip
);
638 force_sig_info(signr
, info
, tsk
);
640 force_sig(signr
, tsk
);
644 if (!fixup_exception(regs
)) {
645 tsk
->thread
.error_code
= error_code
;
646 tsk
->thread
.trap_no
= trapnr
;
647 die(str
, regs
, error_code
);
652 #define DO_ERROR(trapnr, signr, str, name) \
653 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
655 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
658 conditional_sti(regs); \
659 do_trap(trapnr, signr, str, regs, error_code, NULL); \
662 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
663 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
666 info.si_signo = signr; \
668 info.si_code = sicode; \
669 info.si_addr = (void __user *)siaddr; \
670 trace_hardirqs_fixup(); \
671 if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
674 conditional_sti(regs); \
675 do_trap(trapnr, signr, str, regs, error_code, &info); \
678 DO_ERROR_INFO(0, SIGFPE
, "divide error", divide_error
, FPE_INTDIV
, regs
->ip
)
679 DO_ERROR(4, SIGSEGV
, "overflow", overflow
)
680 DO_ERROR(5, SIGSEGV
, "bounds", bounds
)
681 DO_ERROR_INFO(6, SIGILL
, "invalid opcode", invalid_op
, ILL_ILLOPN
, regs
->ip
)
682 DO_ERROR(9, SIGFPE
, "coprocessor segment overrun", coprocessor_segment_overrun
)
683 DO_ERROR(10, SIGSEGV
, "invalid TSS", invalid_TSS
)
684 DO_ERROR(11, SIGBUS
, "segment not present", segment_not_present
)
685 DO_ERROR_INFO(17, SIGBUS
, "alignment check", alignment_check
, BUS_ADRALN
, 0)
687 /* Runs on IST stack */
688 asmlinkage
void do_stack_segment(struct pt_regs
*regs
, long error_code
)
690 if (notify_die(DIE_TRAP
, "stack segment", regs
, error_code
,
691 12, SIGBUS
) == NOTIFY_STOP
)
693 preempt_conditional_sti(regs
);
694 do_trap(12, SIGBUS
, "stack segment", regs
, error_code
, NULL
);
695 preempt_conditional_cli(regs
);
698 asmlinkage
void do_double_fault(struct pt_regs
* regs
, long error_code
)
700 static const char str
[] = "double fault";
701 struct task_struct
*tsk
= current
;
703 /* Return not checked because double check cannot be ignored */
704 notify_die(DIE_TRAP
, str
, regs
, error_code
, 8, SIGSEGV
);
706 tsk
->thread
.error_code
= error_code
;
707 tsk
->thread
.trap_no
= 8;
709 /* This is always a kernel trap and never fixable (and thus must
712 die(str
, regs
, error_code
);
715 asmlinkage
void __kprobes
716 do_general_protection(struct pt_regs
*regs
, long error_code
)
718 struct task_struct
*tsk
;
720 conditional_sti(regs
);
723 if (!user_mode(regs
))
726 tsk
->thread
.error_code
= error_code
;
727 tsk
->thread
.trap_no
= 13;
729 if (show_unhandled_signals
&& unhandled_signal(tsk
, SIGSEGV
) &&
730 printk_ratelimit()) {
732 "%s[%d] general protection ip:%lx sp:%lx error:%lx",
734 regs
->ip
, regs
->sp
, error_code
);
735 print_vma_addr(" in ", regs
->ip
);
739 force_sig(SIGSEGV
, tsk
);
743 if (fixup_exception(regs
))
746 tsk
->thread
.error_code
= error_code
;
747 tsk
->thread
.trap_no
= 13;
748 if (notify_die(DIE_GPF
, "general protection fault", regs
,
749 error_code
, 13, SIGSEGV
) == NOTIFY_STOP
)
751 die("general protection fault", regs
, error_code
);
754 static notrace __kprobes
void
755 mem_parity_error(unsigned char reason
, struct pt_regs
*regs
)
757 printk(KERN_EMERG
"Uhhuh. NMI received for unknown reason %02x.\n",
759 printk(KERN_EMERG
"You have some hardware problem, likely on the PCI bus.\n");
761 #if defined(CONFIG_EDAC)
762 if (edac_handler_set()) {
763 edac_atomic_assert_error();
768 if (panic_on_unrecovered_nmi
)
769 panic("NMI: Not continuing");
771 printk(KERN_EMERG
"Dazed and confused, but trying to continue\n");
773 /* Clear and disable the memory parity error line. */
774 reason
= (reason
& 0xf) | 4;
778 static notrace __kprobes
void
779 io_check_error(unsigned char reason
, struct pt_regs
*regs
)
781 printk("NMI: IOCK error (debug interrupt?)\n");
782 show_registers(regs
);
784 /* Re-enable the IOCK line, wait for a few seconds */
785 reason
= (reason
& 0xf) | 8;
792 static notrace __kprobes
void
793 unknown_nmi_error(unsigned char reason
, struct pt_regs
* regs
)
795 if (notify_die(DIE_NMIUNKNOWN
, "nmi", regs
, reason
, 2, SIGINT
) == NOTIFY_STOP
)
797 printk(KERN_EMERG
"Uhhuh. NMI received for unknown reason %02x.\n",
799 printk(KERN_EMERG
"Do you have a strange power saving mode enabled?\n");
801 if (panic_on_unrecovered_nmi
)
802 panic("NMI: Not continuing");
804 printk(KERN_EMERG
"Dazed and confused, but trying to continue\n");
807 /* Runs on IST stack. This code must keep interrupts off all the time.
808 Nested NMIs are prevented by the CPU. */
809 asmlinkage notrace __kprobes
void default_do_nmi(struct pt_regs
*regs
)
811 unsigned char reason
= 0;
814 cpu
= smp_processor_id();
816 /* Only the BSP gets external NMIs from the system. */
818 reason
= get_nmi_reason();
820 if (!(reason
& 0xc0)) {
821 if (notify_die(DIE_NMI_IPI
, "nmi_ipi", regs
, reason
, 2, SIGINT
)
825 * Ok, so this is none of the documented NMI sources,
826 * so it must be the NMI watchdog.
828 if (nmi_watchdog_tick(regs
, reason
))
830 if (!do_nmi_callback(regs
, cpu
))
831 unknown_nmi_error(reason
, regs
);
835 if (notify_die(DIE_NMI
, "nmi", regs
, reason
, 2, SIGINT
) == NOTIFY_STOP
)
838 /* AK: following checks seem to be broken on modern chipsets. FIXME */
840 mem_parity_error(reason
, regs
);
842 io_check_error(reason
, regs
);
845 asmlinkage notrace __kprobes
void
846 do_nmi(struct pt_regs
*regs
, long error_code
)
850 add_pda(__nmi_count
, 1);
853 default_do_nmi(regs
);
864 void restart_nmi(void)
870 /* runs on IST stack. */
871 asmlinkage
void __kprobes
do_int3(struct pt_regs
*regs
, long error_code
)
873 trace_hardirqs_fixup();
875 if (notify_die(DIE_INT3
, "int3", regs
, error_code
, 3, SIGTRAP
)
879 preempt_conditional_sti(regs
);
880 do_trap(3, SIGTRAP
, "int3", regs
, error_code
, NULL
);
881 preempt_conditional_cli(regs
);
884 /* Help handler running on IST stack to switch back to user stack
885 for scheduling or signal handling. The actual stack switch is done in
887 asmlinkage __kprobes
struct pt_regs
*sync_regs(struct pt_regs
*eregs
)
889 struct pt_regs
*regs
= eregs
;
890 /* Did already sync */
891 if (eregs
== (struct pt_regs
*)eregs
->sp
)
893 /* Exception from user space */
894 else if (user_mode(eregs
))
895 regs
= task_pt_regs(current
);
896 /* Exception from kernel and interrupts are enabled. Move to
897 kernel process stack. */
898 else if (eregs
->flags
& X86_EFLAGS_IF
)
899 regs
= (struct pt_regs
*)(eregs
->sp
-= sizeof(struct pt_regs
));
905 /* runs on IST stack. */
906 asmlinkage
void __kprobes
do_debug(struct pt_regs
* regs
,
907 unsigned long error_code
)
909 struct task_struct
*tsk
= current
;
910 unsigned long condition
;
913 trace_hardirqs_fixup();
915 get_debugreg(condition
, 6);
918 * The processor cleared BTF, so don't mark that we need it set.
920 clear_tsk_thread_flag(tsk
, TIF_DEBUGCTLMSR
);
921 tsk
->thread
.debugctlmsr
= 0;
923 if (notify_die(DIE_DEBUG
, "debug", regs
, condition
, error_code
,
924 SIGTRAP
) == NOTIFY_STOP
)
927 preempt_conditional_sti(regs
);
929 /* Mask out spurious debug traps due to lazy DR7 setting */
930 if (condition
& (DR_TRAP0
|DR_TRAP1
|DR_TRAP2
|DR_TRAP3
)) {
931 if (!tsk
->thread
.debugreg7
)
935 tsk
->thread
.debugreg6
= condition
;
938 * Single-stepping through TF: make sure we ignore any events in
939 * kernel space (but re-enable TF when returning to user mode).
941 if (condition
& DR_STEP
) {
942 if (!user_mode(regs
))
943 goto clear_TF_reenable
;
946 /* Ok, finally something we can handle */
947 tsk
->thread
.trap_no
= 1;
948 tsk
->thread
.error_code
= error_code
;
949 info
.si_signo
= SIGTRAP
;
951 info
.si_code
= TRAP_BRKPT
;
952 info
.si_addr
= user_mode(regs
) ? (void __user
*)regs
->ip
: NULL
;
953 force_sig_info(SIGTRAP
, &info
, tsk
);
957 preempt_conditional_cli(regs
);
961 set_tsk_thread_flag(tsk
, TIF_SINGLESTEP
);
962 regs
->flags
&= ~X86_EFLAGS_TF
;
963 preempt_conditional_cli(regs
);
967 static int kernel_math_error(struct pt_regs
*regs
, const char *str
, int trapnr
)
969 if (fixup_exception(regs
))
972 notify_die(DIE_GPF
, str
, regs
, 0, trapnr
, SIGFPE
);
973 /* Illegal floating point operation in the kernel */
974 current
->thread
.trap_no
= trapnr
;
980 * Note that we play around with the 'TS' bit in an attempt to get
981 * the correct behaviour even in the presence of the asynchronous
984 asmlinkage
void do_coprocessor_error(struct pt_regs
*regs
)
986 void __user
*ip
= (void __user
*)(regs
->ip
);
987 struct task_struct
*task
;
989 unsigned short cwd
, swd
;
991 conditional_sti(regs
);
992 if (!user_mode(regs
) &&
993 kernel_math_error(regs
, "kernel x87 math error", 16))
997 * Save the info for the exception handler and clear the error.
1000 save_init_fpu(task
);
1001 task
->thread
.trap_no
= 16;
1002 task
->thread
.error_code
= 0;
1003 info
.si_signo
= SIGFPE
;
1005 info
.si_code
= __SI_FAULT
;
1008 * (~cwd & swd) will mask out exceptions that are not set to unmasked
1009 * status. 0x3f is the exception bits in these regs, 0x200 is the
1010 * C1 reg you need in case of a stack fault, 0x040 is the stack
1011 * fault bit. We should only be taking one exception at a time,
1012 * so if this combination doesn't produce any single exception,
1013 * then we have a bad program that isn't synchronizing its FPU usage
1014 * and it will suffer the consequences since we won't be able to
1015 * fully reproduce the context of the exception
1017 cwd
= get_fpu_cwd(task
);
1018 swd
= get_fpu_swd(task
);
1019 switch (swd
& ~cwd
& 0x3f) {
1020 case 0x000: /* No unmasked exception */
1021 default: /* Multiple exceptions */
1023 case 0x001: /* Invalid Op */
1025 * swd & 0x240 == 0x040: Stack Underflow
1026 * swd & 0x240 == 0x240: Stack Overflow
1027 * User must clear the SF bit (0x40) if set
1029 info
.si_code
= FPE_FLTINV
;
1031 case 0x002: /* Denormalize */
1032 case 0x010: /* Underflow */
1033 info
.si_code
= FPE_FLTUND
;
1035 case 0x004: /* Zero Divide */
1036 info
.si_code
= FPE_FLTDIV
;
1038 case 0x008: /* Overflow */
1039 info
.si_code
= FPE_FLTOVF
;
1041 case 0x020: /* Precision */
1042 info
.si_code
= FPE_FLTRES
;
1045 force_sig_info(SIGFPE
, &info
, task
);
1048 asmlinkage
void bad_intr(void)
1050 printk("bad interrupt");
1053 asmlinkage
void do_simd_coprocessor_error(struct pt_regs
*regs
)
1055 void __user
*ip
= (void __user
*)(regs
->ip
);
1056 struct task_struct
*task
;
1058 unsigned short mxcsr
;
1060 conditional_sti(regs
);
1061 if (!user_mode(regs
) &&
1062 kernel_math_error(regs
, "kernel simd math error", 19))
1066 * Save the info for the exception handler and clear the error.
1069 save_init_fpu(task
);
1070 task
->thread
.trap_no
= 19;
1071 task
->thread
.error_code
= 0;
1072 info
.si_signo
= SIGFPE
;
1074 info
.si_code
= __SI_FAULT
;
1077 * The SIMD FPU exceptions are handled a little differently, as there
1078 * is only a single status/control register. Thus, to determine which
1079 * unmasked exception was caught we must mask the exception mask bits
1080 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
1082 mxcsr
= get_fpu_mxcsr(task
);
1083 switch (~((mxcsr
& 0x1f80) >> 7) & (mxcsr
& 0x3f)) {
1087 case 0x001: /* Invalid Op */
1088 info
.si_code
= FPE_FLTINV
;
1090 case 0x002: /* Denormalize */
1091 case 0x010: /* Underflow */
1092 info
.si_code
= FPE_FLTUND
;
1094 case 0x004: /* Zero Divide */
1095 info
.si_code
= FPE_FLTDIV
;
1097 case 0x008: /* Overflow */
1098 info
.si_code
= FPE_FLTOVF
;
1100 case 0x020: /* Precision */
1101 info
.si_code
= FPE_FLTRES
;
1104 force_sig_info(SIGFPE
, &info
, task
);
1107 asmlinkage
void do_spurious_interrupt_bug(struct pt_regs
* regs
)
1111 asmlinkage
void __attribute__((weak
)) smp_thermal_interrupt(void)
1115 asmlinkage
void __attribute__((weak
)) mce_threshold_interrupt(void)
1120 * 'math_state_restore()' saves the current math information in the
1121 * old math state array, and gets the new ones from the current task
1123 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
1124 * Don't touch unless you *really* know how it works.
1126 asmlinkage
void math_state_restore(void)
1128 struct task_struct
*me
= current
;
1133 * does a slab alloc which can sleep
1137 * ran out of memory!
1139 do_group_exit(SIGKILL
);
1142 local_irq_disable();
1145 clts(); /* Allow maths ops (or we recurse) */
1146 restore_fpu_checking(&me
->thread
.xstate
->fxsave
);
1147 task_thread_info(me
)->status
|= TS_USEDFPU
;
1150 EXPORT_SYMBOL_GPL(math_state_restore
);
1152 void __init
trap_init(void)
1154 set_intr_gate(0, ÷_error
);
1155 set_intr_gate_ist(1, &debug
, DEBUG_STACK
);
1156 set_intr_gate_ist(2, &nmi
, NMI_STACK
);
1157 set_system_gate_ist(3, &int3
, DEBUG_STACK
); /* int3 can be called from all */
1158 set_system_gate(4, &overflow
); /* int4 can be called from all */
1159 set_intr_gate(5, &bounds
);
1160 set_intr_gate(6, &invalid_op
);
1161 set_intr_gate(7, &device_not_available
);
1162 set_intr_gate_ist(8, &double_fault
, DOUBLEFAULT_STACK
);
1163 set_intr_gate(9, &coprocessor_segment_overrun
);
1164 set_intr_gate(10, &invalid_TSS
);
1165 set_intr_gate(11, &segment_not_present
);
1166 set_intr_gate_ist(12, &stack_segment
, STACKFAULT_STACK
);
1167 set_intr_gate(13, &general_protection
);
1168 set_intr_gate(14, &page_fault
);
1169 set_intr_gate(15, &spurious_interrupt_bug
);
1170 set_intr_gate(16, &coprocessor_error
);
1171 set_intr_gate(17, &alignment_check
);
1172 #ifdef CONFIG_X86_MCE
1173 set_intr_gate_ist(18, &machine_check
, MCE_STACK
);
1175 set_intr_gate(19, &simd_coprocessor_error
);
1177 #ifdef CONFIG_IA32_EMULATION
1178 set_system_gate(IA32_SYSCALL_VECTOR
, ia32_syscall
);
1181 * initialize the per thread extended state:
1183 init_thread_xstate();
1185 * Should be a barrier for any external CPU state:
1190 static int __init
oops_setup(char *s
)
1194 if (!strcmp(s
, "panic"))
1198 early_param("oops", oops_setup
);
1200 static int __init
kstack_setup(char *s
)
1204 kstack_depth_to_print
= simple_strtoul(s
, NULL
, 0);
1207 early_param("kstack", kstack_setup
);
1209 static int __init
code_bytes_setup(char *s
)
1211 code_bytes
= simple_strtoul(s
, NULL
, 0);
1212 if (code_bytes
> 8192)
1217 __setup("code_bytes=", code_bytes_setup
);