blob: c56bc2873030d65d35e9c97f7b50e990ad4de2f9 [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}
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100112EXPORT_SYMBOL_GPL(print_context_stack);
113
114unsigned long
115print_context_stack_bp(struct thread_info *tinfo,
116 unsigned long *stack, unsigned long bp,
117 const struct stacktrace_ops *ops, void *data,
118 unsigned long *end, int *graph)
119{
120 struct stack_frame *frame = (struct stack_frame *)bp;
121 unsigned long *ret_addr = &frame->return_address;
122
123 while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
124 unsigned long addr = *ret_addr;
125
126 if (__kernel_text_address(addr)) {
127 ops->address(data, addr, 1);
128 frame = frame->next_frame;
129 ret_addr = &frame->return_address;
130 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
131 }
132 }
133 return (unsigned long)frame;
134}
135EXPORT_SYMBOL_GPL(print_context_stack_bp);
Neil Horman878719e2008-10-23 10:40:06 -0400136
137
138static void
139print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
140{
141 printk(data);
142 print_symbol(msg, symbol);
143 printk("\n");
144}
145
146static void print_trace_warning(void *data, char *msg)
147{
148 printk("%s%s\n", (char *)data, msg);
149}
150
151static int print_trace_stack(void *data, char *name)
152{
153 printk("%s <%s> ", (char *)data, name);
154 return 0;
155}
156
157/*
158 * Print one address/symbol entries per line.
159 */
160static void print_trace_address(void *data, unsigned long addr, int reliable)
161{
162 touch_nmi_watchdog();
163 printk(data);
164 printk_address(addr, reliable);
165}
166
167static const struct stacktrace_ops print_trace_ops = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +0100168 .warning = print_trace_warning,
169 .warning_symbol = print_trace_warning_symbol,
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100170 .stack = print_trace_stack,
171 .address = print_trace_address,
Frederic Weisbecker61c19172009-12-17 05:40:33 +0100172 .walk_stack = print_context_stack,
Neil Horman878719e2008-10-23 10:40:06 -0400173};
174
175void
176show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
177 unsigned long *stack, unsigned long bp, char *log_lvl)
178{
179 printk("%sCall Trace:\n", log_lvl);
180 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
181}
182
183void show_trace(struct task_struct *task, struct pt_regs *regs,
184 unsigned long *stack, unsigned long bp)
185{
186 show_trace_log_lvl(task, regs, stack, bp, "");
187}
188
189void show_stack(struct task_struct *task, unsigned long *sp)
190{
191 show_stack_log_lvl(task, NULL, sp, 0, "");
192}
193
194/*
195 * The architecture-independent dump_stack generator
196 */
197void dump_stack(void)
198{
199 unsigned long bp = 0;
200 unsigned long stack;
201
202#ifdef CONFIG_FRAME_POINTER
203 if (!bp)
204 get_bp(bp);
205#endif
206
207 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
208 current->pid, current->comm, print_tainted(),
209 init_utsname()->release,
210 (int)strcspn(init_utsname()->version, " "),
211 init_utsname()->version);
212 show_trace(NULL, NULL, &stack, bp);
213}
214EXPORT_SYMBOL(dump_stack);
215
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100216static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Neil Horman878719e2008-10-23 10:40:06 -0400217static int die_owner = -1;
218static unsigned int die_nest_count;
219
220unsigned __kprobes long oops_begin(void)
221{
222 int cpu;
223 unsigned long flags;
224
Markus Metzgerb1818742009-01-19 10:31:01 +0100225 /* notify the hw-branch tracer so it may disable tracing and
226 add the last trace to the trace buffer -
227 the earlier this happens, the more useful the trace. */
228 trace_hw_branch_oops();
229
Neil Horman878719e2008-10-23 10:40:06 -0400230 oops_enter();
231
232 /* racy, but better than risking deadlock. */
233 raw_local_irq_save(flags);
234 cpu = smp_processor_id();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100235 if (!arch_spin_trylock(&die_lock)) {
Neil Horman878719e2008-10-23 10:40:06 -0400236 if (cpu == die_owner)
237 /* nested oops. should stop eventually */;
238 else
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100239 arch_spin_lock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400240 }
241 die_nest_count++;
242 die_owner = cpu;
243 console_verbose();
244 bust_spinlocks(1);
245 return flags;
246}
247
248void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
249{
250 if (regs && kexec_should_crash(current))
251 crash_kexec(regs);
252
253 bust_spinlocks(0);
254 die_owner = -1;
255 add_taint(TAINT_DIE);
256 die_nest_count--;
257 if (!die_nest_count)
258 /* Nest count reaches zero, release the lock. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100259 arch_spin_unlock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400260 raw_local_irq_restore(flags);
261 oops_exit();
262
263 if (!signr)
264 return;
265 if (in_interrupt())
266 panic("Fatal exception in interrupt");
267 if (panic_on_oops)
268 panic("Fatal exception");
269 do_exit(signr);
270}
271
272int __kprobes __die(const char *str, struct pt_regs *regs, long err)
273{
274#ifdef CONFIG_X86_32
275 unsigned short ss;
276 unsigned long sp;
277#endif
278 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
279#ifdef CONFIG_PREEMPT
280 printk("PREEMPT ");
281#endif
282#ifdef CONFIG_SMP
283 printk("SMP ");
284#endif
285#ifdef CONFIG_DEBUG_PAGEALLOC
286 printk("DEBUG_PAGEALLOC");
287#endif
288 printk("\n");
289 sysfs_printk_last_file();
290 if (notify_die(DIE_OOPS, str, regs, err,
291 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
292 return 1;
293
294 show_registers(regs);
295#ifdef CONFIG_X86_32
H. Peter Anvina343c752009-10-12 14:11:09 -0700296 if (user_mode_vm(regs)) {
Neil Horman878719e2008-10-23 10:40:06 -0400297 sp = regs->sp;
298 ss = regs->ss & 0xffff;
H. Peter Anvina343c752009-10-12 14:11:09 -0700299 } else {
300 sp = kernel_stack_pointer(regs);
301 savesegment(ss, ss);
Neil Horman878719e2008-10-23 10:40:06 -0400302 }
303 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
304 print_symbol("%s", regs->ip);
305 printk(" SS:ESP %04x:%08lx\n", ss, sp);
306#else
307 /* Executive summary in case the oops scrolled away */
308 printk(KERN_ALERT "RIP ");
309 printk_address(regs->ip, 1);
310 printk(" RSP <%016lx>\n", regs->sp);
311#endif
312 return 0;
313}
314
315/*
316 * This is gone through when something in the kernel has done something bad
317 * and is about to be terminated:
318 */
319void die(const char *str, struct pt_regs *regs, long err)
320{
321 unsigned long flags = oops_begin();
322 int sig = SIGSEGV;
323
324 if (!user_mode_vm(regs))
325 report_bug(regs->ip, regs);
326
327 if (__die(str, regs, err))
328 sig = 0;
329 oops_end(flags, regs, sig);
330}
331
332void notrace __kprobes
333die_nmi(char *str, struct pt_regs *regs, int do_panic)
334{
335 unsigned long flags;
336
337 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
338 return;
339
340 /*
341 * We are in trouble anyway, lets at least try
342 * to get a message out.
343 */
344 flags = oops_begin();
345 printk(KERN_EMERG "%s", str);
346 printk(" on CPU%d, ip %08lx, registers:\n",
347 smp_processor_id(), regs->ip);
348 show_registers(regs);
349 oops_end(flags, regs, 0);
350 if (do_panic || panic_on_oops)
351 panic("Non maskable interrupt");
352 nmi_exit();
353 local_irq_enable();
354 do_exit(SIGBUS);
355}
356
357static int __init oops_setup(char *s)
358{
359 if (!s)
360 return -EINVAL;
361 if (!strcmp(s, "panic"))
362 panic_on_oops = 1;
363 return 0;
364}
365early_param("oops", oops_setup);
366
367static int __init kstack_setup(char *s)
368{
369 if (!s)
370 return -EINVAL;
371 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
372 return 0;
373}
374early_param("kstack", kstack_setup);
375
376static int __init code_bytes_setup(char *s)
377{
378 code_bytes = simple_strtoul(s, NULL, 0);
379 if (code_bytes > 8192)
380 code_bytes = 8192;
381
382 return 1;
383}
384__setup("code_bytes=", code_bytes_setup);