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)
30 pr_cont(" [<%p>] %s%pB\n",
31 (void *)address, reliable ? "" : "? ", (void *)address);
34 void printk_address(unsigned long address)
36 pr_cont(" [<%p>] %pS\n", (void *)address, (void *)address);
39 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
41 print_ftrace_graph_addr(unsigned long addr, void *data,
42 const struct stacktrace_ops *ops,
43 struct thread_info *tinfo, int *graph)
45 struct task_struct *task;
46 unsigned long ret_addr;
49 if (addr != (unsigned long)return_to_handler)
53 index = task->curr_ret_stack;
55 if (!task->ret_stack || index < *graph)
59 ret_addr = task->ret_stack[index].ret;
61 ops->address(data, ret_addr, 1);
67 print_ftrace_graph_addr(unsigned long addr, void *data,
68 const struct stacktrace_ops *ops,
69 struct thread_info *tinfo, int *graph)
74 * x86-64 can have up to three kernel stacks:
77 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
80 static inline int valid_stack_ptr(struct thread_info *tinfo,
81 void *p, unsigned int size, void *end)
85 if (p < end && p >= (end-THREAD_SIZE))
90 return p > t && p < t + THREAD_SIZE - size;
94 print_context_stack(struct thread_info *tinfo,
95 unsigned long *stack, unsigned long bp,
96 const struct stacktrace_ops *ops, void *data,
97 unsigned long *end, int *graph)
99 struct stack_frame *frame = (struct stack_frame *)bp;
101 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
105 if (__kernel_text_address(addr)) {
106 if ((unsigned long) stack == bp + sizeof(long)) {
107 ops->address(data, addr, 1);
108 frame = frame->next_frame;
109 bp = (unsigned long) frame;
111 ops->address(data, addr, 0);
113 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
119 EXPORT_SYMBOL_GPL(print_context_stack);
122 print_context_stack_bp(struct thread_info *tinfo,
123 unsigned long *stack, unsigned long bp,
124 const struct stacktrace_ops *ops, void *data,
125 unsigned long *end, int *graph)
127 struct stack_frame *frame = (struct stack_frame *)bp;
128 unsigned long *ret_addr = &frame->return_address;
130 while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
131 unsigned long addr = *ret_addr;
133 if (!__kernel_text_address(addr))
136 ops->address(data, addr, 1);
137 frame = frame->next_frame;
138 ret_addr = &frame->return_address;
139 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
142 return (unsigned long)frame;
144 EXPORT_SYMBOL_GPL(print_context_stack_bp);
146 static int print_trace_stack(void *data, char *name)
148 printk("%s <%s> ", (char *)data, name);
153 * Print one address/symbol entries per line.
155 static void print_trace_address(void *data, unsigned long addr, int reliable)
157 touch_nmi_watchdog();
159 printk_stack_address(addr, reliable);
162 static const struct stacktrace_ops print_trace_ops = {
163 .stack = print_trace_stack,
164 .address = print_trace_address,
165 .walk_stack = print_context_stack,
169 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
170 unsigned long *stack, unsigned long bp, char *log_lvl)
172 printk("%sCall Trace:\n", log_lvl);
173 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
176 void show_trace(struct task_struct *task, struct pt_regs *regs,
177 unsigned long *stack, unsigned long bp)
179 show_trace_log_lvl(task, regs, stack, bp, "");
182 void show_stack(struct task_struct *task, unsigned long *sp)
184 unsigned long bp = 0;
188 * Stack frames below this one aren't interesting. Don't show them
189 * if we're printing for %current.
191 if (!sp && (!task || task == current)) {
193 bp = stack_frame(current, NULL);
196 show_stack_log_lvl(task, NULL, sp, bp, "");
199 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
200 static int die_owner = -1;
201 static unsigned int die_nest_count;
203 unsigned long oops_begin(void)
210 /* racy, but better than risking deadlock. */
211 raw_local_irq_save(flags);
212 cpu = smp_processor_id();
213 if (!arch_spin_trylock(&die_lock)) {
214 if (cpu == die_owner)
215 /* nested oops. should stop eventually */;
217 arch_spin_lock(&die_lock);
225 EXPORT_SYMBOL_GPL(oops_begin);
226 NOKPROBE_SYMBOL(oops_begin);
228 void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
230 if (regs && kexec_should_crash(current))
235 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
238 /* Nest count reaches zero, release the lock. */
239 arch_spin_unlock(&die_lock);
240 raw_local_irq_restore(flags);
246 panic("Fatal exception in interrupt");
248 panic("Fatal exception");
251 NOKPROBE_SYMBOL(oops_end);
253 int __die(const char *str, struct pt_regs *regs, long err)
260 "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
261 #ifdef CONFIG_PREEMPT
267 #ifdef CONFIG_DEBUG_PAGEALLOC
268 printk("DEBUG_PAGEALLOC ");
274 if (notify_die(DIE_OOPS, str, regs, err,
275 current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
281 if (user_mode_vm(regs)) {
283 ss = regs->ss & 0xffff;
285 sp = kernel_stack_pointer(regs);
288 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
289 print_symbol("%s", regs->ip);
290 printk(" SS:ESP %04x:%08lx\n", ss, sp);
292 /* Executive summary in case the oops scrolled away */
293 printk(KERN_ALERT "RIP ");
294 printk_address(regs->ip);
295 printk(" RSP <%016lx>\n", regs->sp);
299 NOKPROBE_SYMBOL(__die);
302 * This is gone through when something in the kernel has done something bad
303 * and is about to be terminated:
305 void die(const char *str, struct pt_regs *regs, long err)
307 unsigned long flags = oops_begin();
310 if (!user_mode_vm(regs))
311 report_bug(regs->ip, regs);
313 if (__die(str, regs, err))
315 oops_end(flags, regs, sig);
318 static int __init kstack_setup(char *s)
326 ret = kstrtoul(s, 0, &val);
329 kstack_depth_to_print = val;
332 early_param("kstack", kstack_setup);
334 static int __init code_bytes_setup(char *s)
342 ret = kstrtoul(s, 0, &val);
347 if (code_bytes > 8192)
352 __setup("code_bytes=", code_bytes_setup);