blob: 8aaa119b7cad2a8ce39841a820806305506cacc3 [file] [log] [blame]
Neil Horman878719e2008-10-23 10:40:06 -04001/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4 */
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>
Steven Rostedt712406a2009-02-09 10:54:03 -080013#include <linux/ftrace.h>
Neil Horman878719e2008-10-23 10:40:06 -040014#include <linux/kexec.h>
15#include <linux/bug.h>
16#include <linux/nmi.h>
17#include <linux/sysfs.h>
18
19#include <asm/stacktrace.h>
20
21#include "dumpstack.h"
22
23int panic_on_unrecovered_nmi;
Kurt Garloff5211a242009-06-24 14:32:11 -070024int panic_on_io_nmi;
Neil Horman878719e2008-10-23 10:40:06 -040025unsigned int code_bytes = 64;
26int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
27static int die_counter;
28
29void printk_address(unsigned long address, int reliable)
30{
31 printk(" [<%p>] %s%pS\n", (void *) address,
32 reliable ? "" : "? ", (void *) address);
33}
34
Steven Rostedt7ee991f2008-12-02 23:50:04 -050035#ifdef CONFIG_FUNCTION_GRAPH_TRACER
36static void
37print_ftrace_graph_addr(unsigned long addr, void *data,
38 const struct stacktrace_ops *ops,
39 struct thread_info *tinfo, int *graph)
40{
41 struct task_struct *task = tinfo->task;
42 unsigned long ret_addr;
43 int index = task->curr_ret_stack;
44
45 if (addr != (unsigned long)return_to_handler)
46 return;
47
48 if (!task->ret_stack || index < *graph)
49 return;
50
51 index -= *graph;
52 ret_addr = task->ret_stack[index].ret;
53
54 ops->address(data, ret_addr, 1);
55
56 (*graph)++;
57}
58#else
59static inline void
60print_ftrace_graph_addr(unsigned long addr, void *data,
61 const struct stacktrace_ops *ops,
62 struct thread_info *tinfo, int *graph)
63{ }
64#endif
65
Neil Horman878719e2008-10-23 10:40:06 -040066/*
67 * x86-64 can have up to three kernel stacks:
68 * process stack
69 * interrupt stack
70 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
71 */
72
73static inline int valid_stack_ptr(struct thread_info *tinfo,
74 void *p, unsigned int size, void *end)
75{
76 void *t = tinfo;
77 if (end) {
78 if (p < end && p >= (end-THREAD_SIZE))
79 return 1;
80 else
81 return 0;
82 }
83 return p > t && p < t + THREAD_SIZE - size;
84}
85
86unsigned long
87print_context_stack(struct thread_info *tinfo,
88 unsigned long *stack, unsigned long bp,
89 const struct stacktrace_ops *ops, void *data,
Steven Rostedt7ee991f2008-12-02 23:50:04 -050090 unsigned long *end, int *graph)
Neil Horman878719e2008-10-23 10:40:06 -040091{
92 struct stack_frame *frame = (struct stack_frame *)bp;
93
94 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
95 unsigned long addr;
96
97 addr = *stack;
98 if (__kernel_text_address(addr)) {
99 if ((unsigned long) stack == bp + sizeof(long)) {
100 ops->address(data, addr, 1);
101 frame = frame->next_frame;
102 bp = (unsigned long) frame;
103 } else {
Arjan van de Ven2c344e92009-02-07 12:23:37 -0800104 ops->address(data, addr, 0);
Neil Horman878719e2008-10-23 10:40:06 -0400105 }
Steven Rostedt7ee991f2008-12-02 23:50:04 -0500106 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
Neil Horman878719e2008-10-23 10:40:06 -0400107 }
108 stack++;
109 }
110 return bp;
111}
112
113
114static void
115print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
116{
117 printk(data);
118 print_symbol(msg, symbol);
119 printk("\n");
120}
121
122static void print_trace_warning(void *data, char *msg)
123{
124 printk("%s%s\n", (char *)data, msg);
125}
126
127static int print_trace_stack(void *data, char *name)
128{
129 printk("%s <%s> ", (char *)data, name);
130 return 0;
131}
132
133/*
134 * Print one address/symbol entries per line.
135 */
136static void print_trace_address(void *data, unsigned long addr, int reliable)
137{
138 touch_nmi_watchdog();
139 printk(data);
140 printk_address(addr, reliable);
141}
142
143static const struct stacktrace_ops print_trace_ops = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +0100144 .warning = print_trace_warning,
145 .warning_symbol = print_trace_warning_symbol,
146 .stack = print_trace_stack,
147 .address = print_trace_address,
148 .walk_stack = print_context_stack,
Neil Horman878719e2008-10-23 10:40:06 -0400149};
150
151void
152show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
153 unsigned long *stack, unsigned long bp, char *log_lvl)
154{
155 printk("%sCall Trace:\n", log_lvl);
156 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
157}
158
159void show_trace(struct task_struct *task, struct pt_regs *regs,
160 unsigned long *stack, unsigned long bp)
161{
162 show_trace_log_lvl(task, regs, stack, bp, "");
163}
164
165void show_stack(struct task_struct *task, unsigned long *sp)
166{
167 show_stack_log_lvl(task, NULL, sp, 0, "");
168}
169
170/*
171 * The architecture-independent dump_stack generator
172 */
173void dump_stack(void)
174{
175 unsigned long bp = 0;
176 unsigned long stack;
177
178#ifdef CONFIG_FRAME_POINTER
179 if (!bp)
180 get_bp(bp);
181#endif
182
183 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
184 current->pid, current->comm, print_tainted(),
185 init_utsname()->release,
186 (int)strcspn(init_utsname()->version, " "),
187 init_utsname()->version);
188 show_trace(NULL, NULL, &stack, bp);
189}
190EXPORT_SYMBOL(dump_stack);
191
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100192static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Neil Horman878719e2008-10-23 10:40:06 -0400193static int die_owner = -1;
194static unsigned int die_nest_count;
195
196unsigned __kprobes long oops_begin(void)
197{
198 int cpu;
199 unsigned long flags;
200
Markus Metzgerb1818742009-01-19 10:31:01 +0100201 /* notify the hw-branch tracer so it may disable tracing and
202 add the last trace to the trace buffer -
203 the earlier this happens, the more useful the trace. */
204 trace_hw_branch_oops();
205
Neil Horman878719e2008-10-23 10:40:06 -0400206 oops_enter();
207
208 /* racy, but better than risking deadlock. */
209 raw_local_irq_save(flags);
210 cpu = smp_processor_id();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100211 if (!arch_spin_trylock(&die_lock)) {
Neil Horman878719e2008-10-23 10:40:06 -0400212 if (cpu == die_owner)
213 /* nested oops. should stop eventually */;
214 else
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100215 arch_spin_lock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400216 }
217 die_nest_count++;
218 die_owner = cpu;
219 console_verbose();
220 bust_spinlocks(1);
221 return flags;
222}
223
224void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
225{
226 if (regs && kexec_should_crash(current))
227 crash_kexec(regs);
228
229 bust_spinlocks(0);
230 die_owner = -1;
231 add_taint(TAINT_DIE);
232 die_nest_count--;
233 if (!die_nest_count)
234 /* Nest count reaches zero, release the lock. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100235 arch_spin_unlock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400236 raw_local_irq_restore(flags);
237 oops_exit();
238
239 if (!signr)
240 return;
241 if (in_interrupt())
242 panic("Fatal exception in interrupt");
243 if (panic_on_oops)
244 panic("Fatal exception");
245 do_exit(signr);
246}
247
248int __kprobes __die(const char *str, struct pt_regs *regs, long err)
249{
250#ifdef CONFIG_X86_32
251 unsigned short ss;
252 unsigned long sp;
253#endif
254 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
255#ifdef CONFIG_PREEMPT
256 printk("PREEMPT ");
257#endif
258#ifdef CONFIG_SMP
259 printk("SMP ");
260#endif
261#ifdef CONFIG_DEBUG_PAGEALLOC
262 printk("DEBUG_PAGEALLOC");
263#endif
264 printk("\n");
265 sysfs_printk_last_file();
266 if (notify_die(DIE_OOPS, str, regs, err,
267 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
268 return 1;
269
270 show_registers(regs);
271#ifdef CONFIG_X86_32
H. Peter Anvina343c752009-10-12 14:11:09 -0700272 if (user_mode_vm(regs)) {
Neil Horman878719e2008-10-23 10:40:06 -0400273 sp = regs->sp;
274 ss = regs->ss & 0xffff;
H. Peter Anvina343c752009-10-12 14:11:09 -0700275 } else {
276 sp = kernel_stack_pointer(regs);
277 savesegment(ss, ss);
Neil Horman878719e2008-10-23 10:40:06 -0400278 }
279 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
280 print_symbol("%s", regs->ip);
281 printk(" SS:ESP %04x:%08lx\n", ss, sp);
282#else
283 /* Executive summary in case the oops scrolled away */
284 printk(KERN_ALERT "RIP ");
285 printk_address(regs->ip, 1);
286 printk(" RSP <%016lx>\n", regs->sp);
287#endif
288 return 0;
289}
290
291/*
292 * This is gone through when something in the kernel has done something bad
293 * and is about to be terminated:
294 */
295void die(const char *str, struct pt_regs *regs, long err)
296{
297 unsigned long flags = oops_begin();
298 int sig = SIGSEGV;
299
300 if (!user_mode_vm(regs))
301 report_bug(regs->ip, regs);
302
303 if (__die(str, regs, err))
304 sig = 0;
305 oops_end(flags, regs, sig);
306}
307
308void notrace __kprobes
309die_nmi(char *str, struct pt_regs *regs, int do_panic)
310{
311 unsigned long flags;
312
313 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
314 return;
315
316 /*
317 * We are in trouble anyway, lets at least try
318 * to get a message out.
319 */
320 flags = oops_begin();
321 printk(KERN_EMERG "%s", str);
322 printk(" on CPU%d, ip %08lx, registers:\n",
323 smp_processor_id(), regs->ip);
324 show_registers(regs);
325 oops_end(flags, regs, 0);
326 if (do_panic || panic_on_oops)
327 panic("Non maskable interrupt");
328 nmi_exit();
329 local_irq_enable();
330 do_exit(SIGBUS);
331}
332
333static int __init oops_setup(char *s)
334{
335 if (!s)
336 return -EINVAL;
337 if (!strcmp(s, "panic"))
338 panic_on_oops = 1;
339 return 0;
340}
341early_param("oops", oops_setup);
342
343static int __init kstack_setup(char *s)
344{
345 if (!s)
346 return -EINVAL;
347 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
348 return 0;
349}
350early_param("kstack", kstack_setup);
351
352static int __init code_bytes_setup(char *s)
353{
354 code_bytes = simple_strtoul(s, NULL, 0);
355 if (code_bytes > 8192)
356 code_bytes = 8192;
357
358 return 1;
359}
360__setup("code_bytes=", code_bytes_setup);