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 int panic_on_unrecovered_nmi
;
20 int kstack_depth_to_print
= 12;
21 static unsigned int code_bytes
= 64;
22 static int die_counter
;
24 void printk_address(unsigned long address
, int reliable
)
26 printk(" [<%016lx>] %s%pS\n",
27 address
, reliable
? "" : "? ", (void *) address
);
30 static unsigned long *in_exception_stack(unsigned cpu
, unsigned long stack
,
31 unsigned *usedp
, char **idp
)
33 static char ids
[][8] = {
34 [DEBUG_STACK
- 1] = "#DB",
35 [NMI_STACK
- 1] = "NMI",
36 [DOUBLEFAULT_STACK
- 1] = "#DF",
37 [STACKFAULT_STACK
- 1] = "#SS",
38 [MCE_STACK
- 1] = "#MC",
39 #if DEBUG_STKSZ > EXCEPTION_STKSZ
40 [N_EXCEPTION_STACKS
...
41 N_EXCEPTION_STACKS
+ DEBUG_STKSZ
/ EXCEPTION_STKSZ
- 2] = "#DB[?]"
47 * Iterate over all exception stacks, and figure out whether
48 * 'stack' is in one of them:
50 for (k
= 0; k
< N_EXCEPTION_STACKS
; k
++) {
51 unsigned long end
= per_cpu(orig_ist
, cpu
).ist
[k
];
53 * Is 'stack' above this exception frame's end?
54 * If yes then skip to the next frame.
59 * Is 'stack' above this exception frame's start address?
60 * If yes then we found the right frame.
62 if (stack
>= end
- EXCEPTION_STKSZ
) {
64 * Make sure we only iterate through an exception
65 * stack once. If it comes up for the second time
66 * then there's something wrong going on - just
67 * break out and return NULL:
69 if (*usedp
& (1U << k
))
73 return (unsigned long *)end
;
76 * If this is a debug stack, and if it has a larger size than
77 * the usual exception stacks, then 'stack' might still
78 * be within the lower portion of the debug stack:
80 #if DEBUG_STKSZ > EXCEPTION_STKSZ
81 if (k
== DEBUG_STACK
- 1 && stack
>= end
- DEBUG_STKSZ
) {
82 unsigned j
= N_EXCEPTION_STACKS
- 1;
85 * Black magic. A large debug stack is composed of
86 * multiple exception stack entries, which we
87 * iterate through now. Dont look:
91 end
-= EXCEPTION_STKSZ
;
92 ids
[j
][4] = '1' + (j
- N_EXCEPTION_STACKS
);
93 } while (stack
< end
- EXCEPTION_STKSZ
);
94 if (*usedp
& (1U << j
))
98 return (unsigned long *)end
;
106 * x86-64 can have up to three kernel stacks:
109 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
112 static inline int valid_stack_ptr(struct thread_info
*tinfo
,
113 void *p
, unsigned int size
, void *end
)
117 if (p
< end
&& p
>= (end
-THREAD_SIZE
))
122 return p
> t
&& p
< t
+ THREAD_SIZE
- size
;
125 /* The form of the top of the frame on the stack */
127 struct stack_frame
*next_frame
;
128 unsigned long return_address
;
131 static inline unsigned long
132 print_context_stack(struct thread_info
*tinfo
,
133 unsigned long *stack
, unsigned long bp
,
134 const struct stacktrace_ops
*ops
, void *data
,
137 struct stack_frame
*frame
= (struct stack_frame
*)bp
;
139 while (valid_stack_ptr(tinfo
, stack
, sizeof(*stack
), end
)) {
143 if (__kernel_text_address(addr
)) {
144 if ((unsigned long) stack
== bp
+ 8) {
145 ops
->address(data
, addr
, 1);
146 frame
= frame
->next_frame
;
147 bp
= (unsigned long) frame
;
149 ops
->address(data
, addr
, bp
== 0);
157 void dump_trace(struct task_struct
*task
, struct pt_regs
*regs
,
158 unsigned long *stack
, unsigned long bp
,
159 const struct stacktrace_ops
*ops
, void *data
)
161 const unsigned cpu
= get_cpu();
162 unsigned long *irqstack_end
= (unsigned long *)cpu_pda(cpu
)->irqstackptr
;
164 struct thread_info
*tinfo
;
172 if (task
&& task
!= current
)
173 stack
= (unsigned long *)task
->thread
.sp
;
176 #ifdef CONFIG_FRAME_POINTER
178 if (task
== current
) {
179 /* Grab bp right from our regs */
180 asm("movq %%rbp, %0" : "=r" (bp
) : );
182 /* bp is the last reg pushed by switch_to */
183 bp
= *(unsigned long *) task
->thread
.sp
;
189 * Print function call entries in all stacks, starting at the
190 * current stack address. If the stacks consist of nested
193 tinfo
= task_thread_info(task
);
196 unsigned long *estack_end
;
197 estack_end
= in_exception_stack(cpu
, (unsigned long)stack
,
201 if (ops
->stack(data
, id
) < 0)
204 bp
= print_context_stack(tinfo
, stack
, bp
, ops
,
206 ops
->stack(data
, "<EOE>");
208 * We link to the next stack via the
209 * second-to-last pointer (index -2 to end) in the
212 stack
= (unsigned long *) estack_end
[-2];
216 unsigned long *irqstack
;
217 irqstack
= irqstack_end
-
218 (IRQSTACKSIZE
- 64) / sizeof(*irqstack
);
220 if (stack
>= irqstack
&& stack
< irqstack_end
) {
221 if (ops
->stack(data
, "IRQ") < 0)
223 bp
= print_context_stack(tinfo
, stack
, bp
,
224 ops
, data
, irqstack_end
);
226 * We link to the next stack (which would be
227 * the process stack normally) the last
228 * pointer (index -1 to end) in the IRQ stack:
230 stack
= (unsigned long *) (irqstack_end
[-1]);
232 ops
->stack(data
, "EOI");
240 * This handles the process stack:
242 bp
= print_context_stack(tinfo
, stack
, bp
, ops
, data
, NULL
);
245 EXPORT_SYMBOL(dump_trace
);
248 print_trace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
250 print_symbol(msg
, symbol
);
254 static void print_trace_warning(void *data
, char *msg
)
259 static int print_trace_stack(void *data
, char *name
)
261 printk(" <%s> ", name
);
265 static void print_trace_address(void *data
, unsigned long addr
, int reliable
)
267 touch_nmi_watchdog();
268 printk_address(addr
, reliable
);
271 static const struct stacktrace_ops print_trace_ops
= {
272 .warning
= print_trace_warning
,
273 .warning_symbol
= print_trace_warning_symbol
,
274 .stack
= print_trace_stack
,
275 .address
= print_trace_address
,
279 show_trace_log_lvl(struct task_struct
*task
, struct pt_regs
*regs
,
280 unsigned long *stack
, unsigned long bp
, char *log_lvl
)
282 printk("Call Trace:\n");
283 dump_trace(task
, regs
, stack
, bp
, &print_trace_ops
, log_lvl
);
286 void show_trace(struct task_struct
*task
, struct pt_regs
*regs
,
287 unsigned long *stack
, unsigned long bp
)
289 show_trace_log_lvl(task
, regs
, stack
, bp
, "");
293 show_stack_log_lvl(struct task_struct
*task
, struct pt_regs
*regs
,
294 unsigned long *sp
, unsigned long bp
, char *log_lvl
)
296 unsigned long *stack
;
298 const int cpu
= smp_processor_id();
299 unsigned long *irqstack_end
=
300 (unsigned long *) (cpu_pda(cpu
)->irqstackptr
);
301 unsigned long *irqstack
=
302 (unsigned long *) (cpu_pda(cpu
)->irqstackptr
- IRQSTACKSIZE
);
305 * debugging aid: "show_stack(NULL, NULL);" prints the
306 * back trace for this cpu.
311 sp
= (unsigned long *)task
->thread
.sp
;
313 sp
= (unsigned long *)&sp
;
317 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
318 if (stack
>= irqstack
&& stack
<= irqstack_end
) {
319 if (stack
== irqstack_end
) {
320 stack
= (unsigned long *) (irqstack_end
[-1]);
324 if (((long) stack
& (THREAD_SIZE
-1)) == 0)
327 if (i
&& ((i
% 4) == 0))
329 printk(" %016lx", *stack
++);
330 touch_nmi_watchdog();
333 show_trace_log_lvl(task
, regs
, sp
, bp
, log_lvl
);
336 void show_stack(struct task_struct
*task
, unsigned long *sp
)
338 show_stack_log_lvl(task
, NULL
, sp
, 0, "");
342 * The architecture-independent dump_stack generator
344 void dump_stack(void)
346 unsigned long bp
= 0;
349 #ifdef CONFIG_FRAME_POINTER
351 asm("movq %%rbp, %0" : "=r" (bp
) : );
354 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
355 current
->pid
, current
->comm
, print_tainted(),
356 init_utsname()->release
,
357 (int)strcspn(init_utsname()->version
, " "),
358 init_utsname()->version
);
359 show_trace(NULL
, NULL
, &stack
, bp
);
361 EXPORT_SYMBOL(dump_stack
);
363 void show_registers(struct pt_regs
*regs
)
367 const int cpu
= smp_processor_id();
368 struct task_struct
*cur
= cpu_pda(cpu
)->pcurrent
;
371 printk("CPU %d ", cpu
);
372 __show_regs(regs
, 1);
373 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
374 cur
->comm
, cur
->pid
, task_thread_info(cur
), cur
);
377 * When in-kernel, we also print out the stack and code at the
378 * time of the fault..
380 if (!user_mode(regs
)) {
381 unsigned int code_prologue
= code_bytes
* 43 / 64;
382 unsigned int code_len
= code_bytes
;
387 show_stack_log_lvl(NULL
, regs
, (unsigned long *)sp
,
390 printk(KERN_EMERG
"Code: ");
392 ip
= (u8
*)regs
->ip
- code_prologue
;
393 if (ip
< (u8
*)PAGE_OFFSET
|| probe_kernel_address(ip
, c
)) {
394 /* try starting at RIP */
396 code_len
= code_len
- code_prologue
+ 1;
398 for (i
= 0; i
< code_len
; i
++, ip
++) {
399 if (ip
< (u8
*)PAGE_OFFSET
||
400 probe_kernel_address(ip
, c
)) {
401 printk(" Bad RIP value.");
404 if (ip
== (u8
*)regs
->ip
)
405 printk("<%02x> ", c
);
413 int is_valid_bugaddr(unsigned long ip
)
417 if (__copy_from_user(&ud2
, (const void __user
*) ip
, sizeof(ud2
)))
420 return ud2
== 0x0b0f;
423 static raw_spinlock_t die_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
424 static int die_owner
= -1;
425 static unsigned int die_nest_count
;
427 unsigned __kprobes
long oops_begin(void)
434 /* racy, but better than risking deadlock. */
435 raw_local_irq_save(flags
);
436 cpu
= smp_processor_id();
437 if (!__raw_spin_trylock(&die_lock
)) {
438 if (cpu
== die_owner
)
439 /* nested oops. should stop eventually */;
441 __raw_spin_lock(&die_lock
);
450 void __kprobes
oops_end(unsigned long flags
, struct pt_regs
*regs
, int signr
)
456 /* Nest count reaches zero, release the lock. */
457 __raw_spin_unlock(&die_lock
);
458 raw_local_irq_restore(flags
);
464 panic("Fatal exception in interrupt");
466 panic("Fatal exception");
471 int __kprobes
__die(const char *str
, struct pt_regs
*regs
, long err
)
473 printk(KERN_EMERG
"%s: %04lx [%u] ", str
, err
& 0xffff, ++die_counter
);
474 #ifdef CONFIG_PREEMPT
480 #ifdef CONFIG_DEBUG_PAGEALLOC
481 printk("DEBUG_PAGEALLOC");
484 if (notify_die(DIE_OOPS
, str
, regs
, err
,
485 current
->thread
.trap_no
, SIGSEGV
) == NOTIFY_STOP
)
488 show_registers(regs
);
489 add_taint(TAINT_DIE
);
490 /* Executive summary in case the oops scrolled away */
491 printk(KERN_ALERT
"RIP ");
492 printk_address(regs
->ip
, 1);
493 printk(" RSP <%016lx>\n", regs
->sp
);
494 if (kexec_should_crash(current
))
499 void die(const char *str
, struct pt_regs
*regs
, long err
)
501 unsigned long flags
= oops_begin();
503 if (!user_mode(regs
))
504 report_bug(regs
->ip
, regs
);
506 if (__die(str
, regs
, err
))
508 oops_end(flags
, regs
, SIGSEGV
);
511 notrace __kprobes
void
512 die_nmi(char *str
, struct pt_regs
*regs
, int do_panic
)
516 if (notify_die(DIE_NMIWATCHDOG
, str
, regs
, 0, 2, SIGINT
) == NOTIFY_STOP
)
519 flags
= oops_begin();
521 * We are in trouble anyway, lets at least try
522 * to get a message out.
524 printk(KERN_EMERG
"%s", str
);
525 printk(" on CPU%d, ip %08lx, registers:\n",
526 smp_processor_id(), regs
->ip
);
527 show_registers(regs
);
528 if (kexec_should_crash(current
))
530 if (do_panic
|| panic_on_oops
)
531 panic("Non maskable interrupt");
532 oops_end(flags
, NULL
, SIGBUS
);
538 static int __init
oops_setup(char *s
)
542 if (!strcmp(s
, "panic"))
546 early_param("oops", oops_setup
);
548 static int __init
kstack_setup(char *s
)
552 kstack_depth_to_print
= simple_strtoul(s
, NULL
, 0);
555 early_param("kstack", kstack_setup
);
557 static int __init
code_bytes_setup(char *s
)
559 code_bytes
= simple_strtoul(s
, NULL
, 0);
560 if (code_bytes
> 8192)
565 __setup("code_bytes=", code_bytes_setup
);