2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5 #include <linux/kallsyms.h>
6 #include <linux/kprobes.h>
7 #include <linux/uaccess.h>
8 #include <linux/utsname.h>
9 #include <linux/hardirq.h>
10 #include <linux/kdebug.h>
11 #include <linux/module.h>
12 #include <linux/ptrace.h>
13 #include <linux/kexec.h>
14 #include <linux/bug.h>
15 #include <linux/nmi.h>
17 #include <asm/stacktrace.h>
19 #define STACKSLOTS_PER_LINE 4
20 #define get_bp(bp) asm("movq %%rbp, %0" : "=r" (bp) :)
22 int panic_on_unrecovered_nmi
;
23 int kstack_depth_to_print
= 3 * STACKSLOTS_PER_LINE
;
24 static unsigned int code_bytes
= 64;
25 static int die_counter
;
27 void printk_address(unsigned long address
, int reliable
)
29 printk(" [<%p>] %s%pS\n", (void *) address
,
30 reliable
? "" : "? ", (void *) address
);
33 static unsigned long *in_exception_stack(unsigned cpu
, unsigned long stack
,
34 unsigned *usedp
, char **idp
)
36 static char ids
[][8] = {
37 [DEBUG_STACK
- 1] = "#DB",
38 [NMI_STACK
- 1] = "NMI",
39 [DOUBLEFAULT_STACK
- 1] = "#DF",
40 [STACKFAULT_STACK
- 1] = "#SS",
41 [MCE_STACK
- 1] = "#MC",
42 #if DEBUG_STKSZ > EXCEPTION_STKSZ
43 [N_EXCEPTION_STACKS
...
44 N_EXCEPTION_STACKS
+ DEBUG_STKSZ
/ EXCEPTION_STKSZ
- 2] = "#DB[?]"
50 * Iterate over all exception stacks, and figure out whether
51 * 'stack' is in one of them:
53 for (k
= 0; k
< N_EXCEPTION_STACKS
; k
++) {
54 unsigned long end
= per_cpu(orig_ist
, cpu
).ist
[k
];
56 * Is 'stack' above this exception frame's end?
57 * If yes then skip to the next frame.
62 * Is 'stack' above this exception frame's start address?
63 * If yes then we found the right frame.
65 if (stack
>= end
- EXCEPTION_STKSZ
) {
67 * Make sure we only iterate through an exception
68 * stack once. If it comes up for the second time
69 * then there's something wrong going on - just
70 * break out and return NULL:
72 if (*usedp
& (1U << k
))
76 return (unsigned long *)end
;
79 * If this is a debug stack, and if it has a larger size than
80 * the usual exception stacks, then 'stack' might still
81 * be within the lower portion of the debug stack:
83 #if DEBUG_STKSZ > EXCEPTION_STKSZ
84 if (k
== DEBUG_STACK
- 1 && stack
>= end
- DEBUG_STKSZ
) {
85 unsigned j
= N_EXCEPTION_STACKS
- 1;
88 * Black magic. A large debug stack is composed of
89 * multiple exception stack entries, which we
90 * iterate through now. Dont look:
94 end
-= EXCEPTION_STKSZ
;
95 ids
[j
][4] = '1' + (j
- N_EXCEPTION_STACKS
);
96 } while (stack
< end
- EXCEPTION_STKSZ
);
97 if (*usedp
& (1U << j
))
101 return (unsigned long *)end
;
109 * x86-64 can have up to three kernel stacks:
112 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
115 static inline int valid_stack_ptr(struct thread_info
*tinfo
,
116 void *p
, unsigned int size
, void *end
)
120 if (p
< end
&& p
>= (end
-THREAD_SIZE
))
125 return p
> t
&& p
< t
+ THREAD_SIZE
- size
;
128 /* The form of the top of the frame on the stack */
130 struct stack_frame
*next_frame
;
131 unsigned long return_address
;
134 static inline unsigned long
135 print_context_stack(struct thread_info
*tinfo
,
136 unsigned long *stack
, unsigned long bp
,
137 const struct stacktrace_ops
*ops
, void *data
,
140 struct stack_frame
*frame
= (struct stack_frame
*)bp
;
142 while (valid_stack_ptr(tinfo
, stack
, sizeof(*stack
), end
)) {
146 if (__kernel_text_address(addr
)) {
147 if ((unsigned long) stack
== bp
+ sizeof(long)) {
148 ops
->address(data
, addr
, 1);
149 frame
= frame
->next_frame
;
150 bp
= (unsigned long) frame
;
152 ops
->address(data
, addr
, bp
== 0);
160 void dump_trace(struct task_struct
*task
, struct pt_regs
*regs
,
161 unsigned long *stack
, unsigned long bp
,
162 const struct stacktrace_ops
*ops
, void *data
)
164 const unsigned cpu
= get_cpu();
165 unsigned long *irqstack_end
= (unsigned long *)cpu_pda(cpu
)->irqstackptr
;
167 struct thread_info
*tinfo
;
175 if (task
&& task
!= current
)
176 stack
= (unsigned long *)task
->thread
.sp
;
179 #ifdef CONFIG_FRAME_POINTER
181 if (task
== current
) {
182 /* Grab bp right from our regs */
185 /* bp is the last reg pushed by switch_to */
186 bp
= *(unsigned long *) task
->thread
.sp
;
192 * Print function call entries in all stacks, starting at the
193 * current stack address. If the stacks consist of nested
196 tinfo
= task_thread_info(task
);
199 unsigned long *estack_end
;
200 estack_end
= in_exception_stack(cpu
, (unsigned long)stack
,
204 if (ops
->stack(data
, id
) < 0)
207 bp
= print_context_stack(tinfo
, stack
, bp
, ops
,
209 ops
->stack(data
, "<EOE>");
211 * We link to the next stack via the
212 * second-to-last pointer (index -2 to end) in the
215 stack
= (unsigned long *) estack_end
[-2];
219 unsigned long *irqstack
;
220 irqstack
= irqstack_end
-
221 (IRQSTACKSIZE
- 64) / sizeof(*irqstack
);
223 if (stack
>= irqstack
&& stack
< irqstack_end
) {
224 if (ops
->stack(data
, "IRQ") < 0)
226 bp
= print_context_stack(tinfo
, stack
, bp
,
227 ops
, data
, irqstack_end
);
229 * We link to the next stack (which would be
230 * the process stack normally) the last
231 * pointer (index -1 to end) in the IRQ stack:
233 stack
= (unsigned long *) (irqstack_end
[-1]);
235 ops
->stack(data
, "EOI");
243 * This handles the process stack:
245 bp
= print_context_stack(tinfo
, stack
, bp
, ops
, data
, NULL
);
248 EXPORT_SYMBOL(dump_trace
);
251 print_trace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
254 print_symbol(msg
, symbol
);
258 static void print_trace_warning(void *data
, char *msg
)
260 printk("%s%s\n", (char *)data
, msg
);
263 static int print_trace_stack(void *data
, char *name
)
265 printk("%s <%s> ", (char *)data
, name
);
270 * Print one address/symbol entries per line.
272 static void print_trace_address(void *data
, unsigned long addr
, int reliable
)
274 touch_nmi_watchdog();
276 printk_address(addr
, reliable
);
279 static const struct stacktrace_ops print_trace_ops
= {
280 .warning
= print_trace_warning
,
281 .warning_symbol
= print_trace_warning_symbol
,
282 .stack
= print_trace_stack
,
283 .address
= print_trace_address
,
287 show_trace_log_lvl(struct task_struct
*task
, struct pt_regs
*regs
,
288 unsigned long *stack
, unsigned long bp
, char *log_lvl
)
290 printk("%sCall Trace:\n", log_lvl
);
291 dump_trace(task
, regs
, stack
, bp
, &print_trace_ops
, log_lvl
);
294 void show_trace(struct task_struct
*task
, struct pt_regs
*regs
,
295 unsigned long *stack
, unsigned long bp
)
297 show_trace_log_lvl(task
, regs
, stack
, bp
, "");
301 show_stack_log_lvl(struct task_struct
*task
, struct pt_regs
*regs
,
302 unsigned long *sp
, unsigned long bp
, char *log_lvl
)
304 unsigned long *stack
;
306 const int cpu
= smp_processor_id();
307 unsigned long *irqstack_end
=
308 (unsigned long *) (cpu_pda(cpu
)->irqstackptr
);
309 unsigned long *irqstack
=
310 (unsigned long *) (cpu_pda(cpu
)->irqstackptr
- IRQSTACKSIZE
);
313 * debugging aid: "show_stack(NULL, NULL);" prints the
314 * back trace for this cpu.
319 sp
= (unsigned long *)task
->thread
.sp
;
321 sp
= (unsigned long *)&sp
;
325 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
326 if (stack
>= irqstack
&& stack
<= irqstack_end
) {
327 if (stack
== irqstack_end
) {
328 stack
= (unsigned long *) (irqstack_end
[-1]);
332 if (((long) stack
& (THREAD_SIZE
-1)) == 0)
335 if (i
&& ((i
% STACKSLOTS_PER_LINE
) == 0))
336 printk("\n%s", log_lvl
);
337 printk(" %016lx", *stack
++);
338 touch_nmi_watchdog();
341 show_trace_log_lvl(task
, regs
, sp
, bp
, log_lvl
);
344 void show_stack(struct task_struct
*task
, unsigned long *sp
)
346 show_stack_log_lvl(task
, NULL
, sp
, 0, "");
350 * The architecture-independent dump_stack generator
352 void dump_stack(void)
354 unsigned long bp
= 0;
357 #ifdef CONFIG_FRAME_POINTER
362 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
363 current
->pid
, current
->comm
, print_tainted(),
364 init_utsname()->release
,
365 (int)strcspn(init_utsname()->version
, " "),
366 init_utsname()->version
);
367 show_trace(NULL
, NULL
, &stack
, bp
);
369 EXPORT_SYMBOL(dump_stack
);
371 void show_registers(struct pt_regs
*regs
)
375 const int cpu
= smp_processor_id();
376 struct task_struct
*cur
= cpu_pda(cpu
)->pcurrent
;
379 printk("CPU %d ", cpu
);
380 __show_regs(regs
, 1);
381 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
382 cur
->comm
, cur
->pid
, task_thread_info(cur
), cur
);
385 * When in-kernel, we also print out the stack and code at the
386 * time of the fault..
388 if (!user_mode(regs
)) {
389 unsigned int code_prologue
= code_bytes
* 43 / 64;
390 unsigned int code_len
= code_bytes
;
394 printk(KERN_EMERG
"Stack:\n");
395 show_stack_log_lvl(NULL
, regs
, (unsigned long *)sp
,
396 regs
->bp
, KERN_EMERG
);
398 printk(KERN_EMERG
"Code: ");
400 ip
= (u8
*)regs
->ip
- code_prologue
;
401 if (ip
< (u8
*)PAGE_OFFSET
|| probe_kernel_address(ip
, c
)) {
402 /* try starting at IP */
404 code_len
= code_len
- code_prologue
+ 1;
406 for (i
= 0; i
< code_len
; i
++, ip
++) {
407 if (ip
< (u8
*)PAGE_OFFSET
||
408 probe_kernel_address(ip
, c
)) {
409 printk(" Bad RIP value.");
412 if (ip
== (u8
*)regs
->ip
)
413 printk("<%02x> ", c
);
421 int is_valid_bugaddr(unsigned long ip
)
425 if (__copy_from_user(&ud2
, (const void __user
*) ip
, sizeof(ud2
)))
428 return ud2
== 0x0b0f;
431 static raw_spinlock_t die_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
432 static int die_owner
= -1;
433 static unsigned int die_nest_count
;
435 unsigned __kprobes
long oops_begin(void)
442 /* racy, but better than risking deadlock. */
443 raw_local_irq_save(flags
);
444 cpu
= smp_processor_id();
445 if (!__raw_spin_trylock(&die_lock
)) {
446 if (cpu
== die_owner
)
447 /* nested oops. should stop eventually */;
449 __raw_spin_lock(&die_lock
);
458 void __kprobes
oops_end(unsigned long flags
, struct pt_regs
*regs
, int signr
)
464 /* Nest count reaches zero, release the lock. */
465 __raw_spin_unlock(&die_lock
);
466 raw_local_irq_restore(flags
);
472 panic("Fatal exception in interrupt");
474 panic("Fatal exception");
479 int __kprobes
__die(const char *str
, struct pt_regs
*regs
, long err
)
481 printk(KERN_EMERG
"%s: %04lx [#%d] ", str
, err
& 0xffff, ++die_counter
);
482 #ifdef CONFIG_PREEMPT
488 #ifdef CONFIG_DEBUG_PAGEALLOC
489 printk("DEBUG_PAGEALLOC");
492 if (notify_die(DIE_OOPS
, str
, regs
, err
,
493 current
->thread
.trap_no
, SIGSEGV
) == NOTIFY_STOP
)
496 show_registers(regs
);
497 add_taint(TAINT_DIE
);
498 /* Executive summary in case the oops scrolled away */
499 printk(KERN_ALERT
"RIP ");
500 printk_address(regs
->ip
, 1);
501 printk(" RSP <%016lx>\n", regs
->sp
);
502 if (kexec_should_crash(current
))
507 void die(const char *str
, struct pt_regs
*regs
, long err
)
509 unsigned long flags
= oops_begin();
511 if (!user_mode(regs
))
512 report_bug(regs
->ip
, regs
);
514 if (__die(str
, regs
, err
))
516 oops_end(flags
, regs
, SIGSEGV
);
519 notrace __kprobes
void
520 die_nmi(char *str
, struct pt_regs
*regs
, int do_panic
)
524 if (notify_die(DIE_NMIWATCHDOG
, str
, regs
, 0, 2, SIGINT
) == NOTIFY_STOP
)
527 flags
= oops_begin();
529 * We are in trouble anyway, lets at least try
530 * to get a message out.
532 printk(KERN_EMERG
"%s", str
);
533 printk(" on CPU%d, ip %08lx, registers:\n",
534 smp_processor_id(), regs
->ip
);
535 show_registers(regs
);
536 if (kexec_should_crash(current
))
538 if (do_panic
|| panic_on_oops
)
539 panic("Non maskable interrupt");
540 oops_end(flags
, NULL
, SIGBUS
);
546 static int __init
oops_setup(char *s
)
550 if (!strcmp(s
, "panic"))
554 early_param("oops", oops_setup
);
556 static int __init
kstack_setup(char *s
)
560 kstack_depth_to_print
= simple_strtoul(s
, NULL
, 0);
563 early_param("kstack", kstack_setup
);
565 static int __init
code_bytes_setup(char *s
)
567 code_bytes
= simple_strtoul(s
, NULL
, 0);
568 if (code_bytes
> 8192)
573 __setup("code_bytes=", code_bytes_setup
);