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/ftrace.h>
14 #include <linux/kexec.h>
15 #include <linux/bug.h>
16 #include <linux/nmi.h>
17 #include <linux/sysfs.h>
19 #include <asm/stacktrace.h>
22 int panic_on_unrecovered_nmi
;
24 unsigned int code_bytes
= 64;
25 int kstack_depth_to_print
= 3 * STACKSLOTS_PER_LINE
;
26 static int die_counter
;
28 static void printk_stack_address(unsigned long address
, int reliable
,
31 printk("%s [<%p>] %s%pB\n",
32 (char *)data
, (void *)address
, reliable
? "" : "? ",
36 void printk_address(unsigned long address
)
38 pr_cont(" [<%p>] %pS\n", (void *)address
, (void *)address
);
41 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
43 print_ftrace_graph_addr(unsigned long addr
, void *data
,
44 const struct stacktrace_ops
*ops
,
45 struct thread_info
*tinfo
, int *graph
)
47 struct task_struct
*task
;
48 unsigned long ret_addr
;
51 if (addr
!= (unsigned long)return_to_handler
)
55 index
= task
->curr_ret_stack
;
57 if (!task
->ret_stack
|| index
< *graph
)
61 ret_addr
= task
->ret_stack
[index
].ret
;
63 ops
->address(data
, ret_addr
, 1);
69 print_ftrace_graph_addr(unsigned long addr
, void *data
,
70 const struct stacktrace_ops
*ops
,
71 struct thread_info
*tinfo
, int *graph
)
76 * x86-64 can have up to three kernel stacks:
79 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
82 static inline int valid_stack_ptr(struct thread_info
*tinfo
,
83 void *p
, unsigned int size
, void *end
)
87 if (p
< end
&& p
>= (end
-THREAD_SIZE
))
92 return p
> t
&& p
< t
+ THREAD_SIZE
- size
;
96 print_context_stack(struct thread_info
*tinfo
,
97 unsigned long *stack
, unsigned long bp
,
98 const struct stacktrace_ops
*ops
, void *data
,
99 unsigned long *end
, int *graph
)
101 struct stack_frame
*frame
= (struct stack_frame
*)bp
;
103 while (valid_stack_ptr(tinfo
, stack
, sizeof(*stack
), end
)) {
107 if (__kernel_text_address(addr
)) {
108 if ((unsigned long) stack
== bp
+ sizeof(long)) {
109 ops
->address(data
, addr
, 1);
110 frame
= frame
->next_frame
;
111 bp
= (unsigned long) frame
;
113 ops
->address(data
, addr
, 0);
115 print_ftrace_graph_addr(addr
, data
, ops
, tinfo
, graph
);
121 EXPORT_SYMBOL_GPL(print_context_stack
);
124 print_context_stack_bp(struct thread_info
*tinfo
,
125 unsigned long *stack
, unsigned long bp
,
126 const struct stacktrace_ops
*ops
, void *data
,
127 unsigned long *end
, int *graph
)
129 struct stack_frame
*frame
= (struct stack_frame
*)bp
;
130 unsigned long *ret_addr
= &frame
->return_address
;
132 while (valid_stack_ptr(tinfo
, ret_addr
, sizeof(*ret_addr
), end
)) {
133 unsigned long addr
= *ret_addr
;
135 if (!__kernel_text_address(addr
))
138 ops
->address(data
, addr
, 1);
139 frame
= frame
->next_frame
;
140 ret_addr
= &frame
->return_address
;
141 print_ftrace_graph_addr(addr
, data
, ops
, tinfo
, graph
);
144 return (unsigned long)frame
;
146 EXPORT_SYMBOL_GPL(print_context_stack_bp
);
148 static int print_trace_stack(void *data
, char *name
)
150 printk("%s <%s> ", (char *)data
, name
);
155 * Print one address/symbol entries per line.
157 static void print_trace_address(void *data
, unsigned long addr
, int reliable
)
159 touch_nmi_watchdog();
160 printk_stack_address(addr
, reliable
, data
);
163 static const struct stacktrace_ops print_trace_ops
= {
164 .stack
= print_trace_stack
,
165 .address
= print_trace_address
,
166 .walk_stack
= print_context_stack
,
170 show_trace_log_lvl(struct task_struct
*task
, struct pt_regs
*regs
,
171 unsigned long *stack
, unsigned long bp
, char *log_lvl
)
173 printk("%sCall Trace:\n", log_lvl
);
174 dump_trace(task
, regs
, stack
, bp
, &print_trace_ops
, log_lvl
);
177 void show_trace(struct task_struct
*task
, struct pt_regs
*regs
,
178 unsigned long *stack
, unsigned long bp
)
180 show_trace_log_lvl(task
, regs
, stack
, bp
, "");
183 void show_stack(struct task_struct
*task
, unsigned long *sp
)
185 unsigned long bp
= 0;
189 * Stack frames below this one aren't interesting. Don't show them
190 * if we're printing for %current.
192 if (!sp
&& (!task
|| task
== current
)) {
194 bp
= stack_frame(current
, NULL
);
197 show_stack_log_lvl(task
, NULL
, sp
, bp
, "");
200 static arch_spinlock_t die_lock
= __ARCH_SPIN_LOCK_UNLOCKED
;
201 static int die_owner
= -1;
202 static unsigned int die_nest_count
;
204 unsigned long oops_begin(void)
211 /* racy, but better than risking deadlock. */
212 raw_local_irq_save(flags
);
213 cpu
= smp_processor_id();
214 if (!arch_spin_trylock(&die_lock
)) {
215 if (cpu
== die_owner
)
216 /* nested oops. should stop eventually */;
218 arch_spin_lock(&die_lock
);
226 EXPORT_SYMBOL_GPL(oops_begin
);
227 NOKPROBE_SYMBOL(oops_begin
);
229 void oops_end(unsigned long flags
, struct pt_regs
*regs
, int signr
)
231 if (regs
&& kexec_should_crash(current
))
236 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
239 /* Nest count reaches zero, release the lock. */
240 arch_spin_unlock(&die_lock
);
241 raw_local_irq_restore(flags
);
247 panic("Fatal exception in interrupt");
249 panic("Fatal exception");
252 NOKPROBE_SYMBOL(oops_end
);
254 int __die(const char *str
, struct pt_regs
*regs
, long err
)
261 "%s: %04lx [#%d] ", str
, err
& 0xffff, ++die_counter
);
262 #ifdef CONFIG_PREEMPT
268 #ifdef CONFIG_DEBUG_PAGEALLOC
269 printk("DEBUG_PAGEALLOC ");
275 if (notify_die(DIE_OOPS
, str
, regs
, err
,
276 current
->thread
.trap_nr
, SIGSEGV
) == NOTIFY_STOP
)
282 if (user_mode(regs
)) {
284 ss
= regs
->ss
& 0xffff;
286 sp
= kernel_stack_pointer(regs
);
289 printk(KERN_EMERG
"EIP: [<%08lx>] ", regs
->ip
);
290 print_symbol("%s", regs
->ip
);
291 printk(" SS:ESP %04x:%08lx\n", ss
, sp
);
293 /* Executive summary in case the oops scrolled away */
294 printk(KERN_ALERT
"RIP ");
295 printk_address(regs
->ip
);
296 printk(" RSP <%016lx>\n", regs
->sp
);
300 NOKPROBE_SYMBOL(__die
);
303 * This is gone through when something in the kernel has done something bad
304 * and is about to be terminated:
306 void die(const char *str
, struct pt_regs
*regs
, long err
)
308 unsigned long flags
= oops_begin();
311 if (!user_mode(regs
))
312 report_bug(regs
->ip
, regs
);
314 if (__die(str
, regs
, err
))
316 oops_end(flags
, regs
, sig
);
319 static int __init
kstack_setup(char *s
)
327 ret
= kstrtoul(s
, 0, &val
);
330 kstack_depth_to_print
= val
;
333 early_param("kstack", kstack_setup
);
335 static int __init
code_bytes_setup(char *s
)
343 ret
= kstrtoul(s
, 0, &val
);
348 if (code_bytes
> 8192)
353 __setup("code_bytes=", code_bytes_setup
);