blob: d6fb146c0d8be8754c11dd710f7cc5792fc63cce [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
Neil Horman878719e2008-10-23 10:40:06 -040021
22int panic_on_unrecovered_nmi;
Kurt Garloff5211a242009-06-24 14:32:11 -070023int panic_on_io_nmi;
Neil Horman878719e2008-10-23 10:40:06 -040024unsigned int code_bytes = 64;
25int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
26static int die_counter;
27
28void printk_address(unsigned long address, int reliable)
29{
30 printk(" [<%p>] %s%pS\n", (void *) address,
31 reliable ? "" : "? ", (void *) address);
32}
33
Steven Rostedt7ee991f2008-12-02 23:50:04 -050034#ifdef CONFIG_FUNCTION_GRAPH_TRACER
35static void
36print_ftrace_graph_addr(unsigned long addr, void *data,
37 const struct stacktrace_ops *ops,
38 struct thread_info *tinfo, int *graph)
39{
40 struct task_struct *task = tinfo->task;
41 unsigned long ret_addr;
42 int index = task->curr_ret_stack;
43
44 if (addr != (unsigned long)return_to_handler)
45 return;
46
47 if (!task->ret_stack || index < *graph)
48 return;
49
50 index -= *graph;
51 ret_addr = task->ret_stack[index].ret;
52
53 ops->address(data, ret_addr, 1);
54
55 (*graph)++;
56}
57#else
58static inline void
59print_ftrace_graph_addr(unsigned long addr, void *data,
60 const struct stacktrace_ops *ops,
61 struct thread_info *tinfo, int *graph)
62{ }
63#endif
64
Neil Horman878719e2008-10-23 10:40:06 -040065/*
66 * x86-64 can have up to three kernel stacks:
67 * process stack
68 * interrupt stack
69 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
70 */
71
72static inline int valid_stack_ptr(struct thread_info *tinfo,
73 void *p, unsigned int size, void *end)
74{
75 void *t = tinfo;
76 if (end) {
77 if (p < end && p >= (end-THREAD_SIZE))
78 return 1;
79 else
80 return 0;
81 }
82 return p > t && p < t + THREAD_SIZE - size;
83}
84
85unsigned long
86print_context_stack(struct thread_info *tinfo,
87 unsigned long *stack, unsigned long bp,
88 const struct stacktrace_ops *ops, void *data,
Steven Rostedt7ee991f2008-12-02 23:50:04 -050089 unsigned long *end, int *graph)
Neil Horman878719e2008-10-23 10:40:06 -040090{
91 struct stack_frame *frame = (struct stack_frame *)bp;
92
93 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
94 unsigned long addr;
95
96 addr = *stack;
97 if (__kernel_text_address(addr)) {
98 if ((unsigned long) stack == bp + sizeof(long)) {
99 ops->address(data, addr, 1);
100 frame = frame->next_frame;
101 bp = (unsigned long) frame;
102 } else {
Arjan van de Ven2c344e92009-02-07 12:23:37 -0800103 ops->address(data, addr, 0);
Neil Horman878719e2008-10-23 10:40:06 -0400104 }
Steven Rostedt7ee991f2008-12-02 23:50:04 -0500105 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
Neil Horman878719e2008-10-23 10:40:06 -0400106 }
107 stack++;
108 }
109 return bp;
110}
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100111EXPORT_SYMBOL_GPL(print_context_stack);
112
113unsigned long
114print_context_stack_bp(struct thread_info *tinfo,
115 unsigned long *stack, unsigned long bp,
116 const struct stacktrace_ops *ops, void *data,
117 unsigned long *end, int *graph)
118{
119 struct stack_frame *frame = (struct stack_frame *)bp;
120 unsigned long *ret_addr = &frame->return_address;
121
122 while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
123 unsigned long addr = *ret_addr;
124
Frederic Weisbeckerc2c5d452009-12-31 03:52:25 +0100125 if (!__kernel_text_address(addr))
126 break;
127
128 ops->address(data, addr, 1);
129 frame = frame->next_frame;
130 ret_addr = &frame->return_address;
131 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100132 }
Frederic Weisbeckerc2c5d452009-12-31 03:52:25 +0100133
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100134 return (unsigned long)frame;
135}
136EXPORT_SYMBOL_GPL(print_context_stack_bp);
Neil Horman878719e2008-10-23 10:40:06 -0400137
138
139static void
140print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
141{
142 printk(data);
143 print_symbol(msg, symbol);
144 printk("\n");
145}
146
147static void print_trace_warning(void *data, char *msg)
148{
149 printk("%s%s\n", (char *)data, msg);
150}
151
152static int print_trace_stack(void *data, char *name)
153{
154 printk("%s <%s> ", (char *)data, name);
155 return 0;
156}
157
158/*
159 * Print one address/symbol entries per line.
160 */
161static void print_trace_address(void *data, unsigned long addr, int reliable)
162{
163 touch_nmi_watchdog();
164 printk(data);
165 printk_address(addr, reliable);
166}
167
168static const struct stacktrace_ops print_trace_ops = {
Frederic Weisbecker61c19172009-12-17 05:40:33 +0100169 .warning = print_trace_warning,
170 .warning_symbol = print_trace_warning_symbol,
Frederic Weisbecker06d65bd2009-12-17 05:40:34 +0100171 .stack = print_trace_stack,
172 .address = print_trace_address,
Frederic Weisbecker61c19172009-12-17 05:40:33 +0100173 .walk_stack = print_context_stack,
Neil Horman878719e2008-10-23 10:40:06 -0400174};
175
176void
177show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
Soeren Sandmann Pedersen9c0729d2010-11-05 05:59:39 -0400178 unsigned long *stack, char *log_lvl)
Neil Horman878719e2008-10-23 10:40:06 -0400179{
180 printk("%sCall Trace:\n", log_lvl);
Soeren Sandmann Pedersen9c0729d2010-11-05 05:59:39 -0400181 dump_trace(task, regs, stack, &print_trace_ops, log_lvl);
Neil Horman878719e2008-10-23 10:40:06 -0400182}
183
184void show_trace(struct task_struct *task, struct pt_regs *regs,
Soeren Sandmann Pedersen9c0729d2010-11-05 05:59:39 -0400185 unsigned long *stack)
Neil Horman878719e2008-10-23 10:40:06 -0400186{
Soeren Sandmann Pedersen9c0729d2010-11-05 05:59:39 -0400187 show_trace_log_lvl(task, regs, stack, "");
Neil Horman878719e2008-10-23 10:40:06 -0400188}
189
190void show_stack(struct task_struct *task, unsigned long *sp)
191{
Soeren Sandmann Pedersen9c0729d2010-11-05 05:59:39 -0400192 show_stack_log_lvl(task, NULL, sp, "");
Neil Horman878719e2008-10-23 10:40:06 -0400193}
194
195/*
196 * The architecture-independent dump_stack generator
197 */
198void dump_stack(void)
199{
Neil Horman878719e2008-10-23 10:40:06 -0400200 unsigned long stack;
201
Neil Horman878719e2008-10-23 10:40:06 -0400202 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
203 current->pid, current->comm, print_tainted(),
204 init_utsname()->release,
205 (int)strcspn(init_utsname()->version, " "),
206 init_utsname()->version);
Soeren Sandmann Pedersen9c0729d2010-11-05 05:59:39 -0400207 show_trace(NULL, NULL, &stack);
Neil Horman878719e2008-10-23 10:40:06 -0400208}
209EXPORT_SYMBOL(dump_stack);
210
Thomas Gleixneredc35bd2009-12-03 12:38:57 +0100211static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Neil Horman878719e2008-10-23 10:40:06 -0400212static int die_owner = -1;
213static unsigned int die_nest_count;
214
215unsigned __kprobes long oops_begin(void)
216{
217 int cpu;
218 unsigned long flags;
219
Neil Horman878719e2008-10-23 10:40:06 -0400220 oops_enter();
221
222 /* racy, but better than risking deadlock. */
223 raw_local_irq_save(flags);
224 cpu = smp_processor_id();
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100225 if (!arch_spin_trylock(&die_lock)) {
Neil Horman878719e2008-10-23 10:40:06 -0400226 if (cpu == die_owner)
227 /* nested oops. should stop eventually */;
228 else
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100229 arch_spin_lock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400230 }
231 die_nest_count++;
232 die_owner = cpu;
233 console_verbose();
234 bust_spinlocks(1);
235 return flags;
236}
237
238void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
239{
240 if (regs && kexec_should_crash(current))
241 crash_kexec(regs);
242
243 bust_spinlocks(0);
244 die_owner = -1;
245 add_taint(TAINT_DIE);
246 die_nest_count--;
247 if (!die_nest_count)
248 /* Nest count reaches zero, release the lock. */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100249 arch_spin_unlock(&die_lock);
Neil Horman878719e2008-10-23 10:40:06 -0400250 raw_local_irq_restore(flags);
251 oops_exit();
252
253 if (!signr)
254 return;
255 if (in_interrupt())
256 panic("Fatal exception in interrupt");
257 if (panic_on_oops)
258 panic("Fatal exception");
259 do_exit(signr);
260}
261
262int __kprobes __die(const char *str, struct pt_regs *regs, long err)
263{
264#ifdef CONFIG_X86_32
265 unsigned short ss;
266 unsigned long sp;
267#endif
268 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
269#ifdef CONFIG_PREEMPT
270 printk("PREEMPT ");
271#endif
272#ifdef CONFIG_SMP
273 printk("SMP ");
274#endif
275#ifdef CONFIG_DEBUG_PAGEALLOC
276 printk("DEBUG_PAGEALLOC");
277#endif
278 printk("\n");
279 sysfs_printk_last_file();
280 if (notify_die(DIE_OOPS, str, regs, err,
281 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
282 return 1;
283
284 show_registers(regs);
285#ifdef CONFIG_X86_32
H. Peter Anvina343c752009-10-12 14:11:09 -0700286 if (user_mode_vm(regs)) {
Neil Horman878719e2008-10-23 10:40:06 -0400287 sp = regs->sp;
288 ss = regs->ss & 0xffff;
H. Peter Anvina343c752009-10-12 14:11:09 -0700289 } else {
290 sp = kernel_stack_pointer(regs);
291 savesegment(ss, ss);
Neil Horman878719e2008-10-23 10:40:06 -0400292 }
293 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
294 print_symbol("%s", regs->ip);
295 printk(" SS:ESP %04x:%08lx\n", ss, sp);
296#else
297 /* Executive summary in case the oops scrolled away */
298 printk(KERN_ALERT "RIP ");
299 printk_address(regs->ip, 1);
300 printk(" RSP <%016lx>\n", regs->sp);
301#endif
302 return 0;
303}
304
305/*
306 * This is gone through when something in the kernel has done something bad
307 * and is about to be terminated:
308 */
309void die(const char *str, struct pt_regs *regs, long err)
310{
311 unsigned long flags = oops_begin();
312 int sig = SIGSEGV;
313
314 if (!user_mode_vm(regs))
315 report_bug(regs->ip, regs);
316
317 if (__die(str, regs, err))
318 sig = 0;
319 oops_end(flags, regs, sig);
320}
321
322void notrace __kprobes
323die_nmi(char *str, struct pt_regs *regs, int do_panic)
324{
325 unsigned long flags;
326
327 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
328 return;
329
330 /*
331 * We are in trouble anyway, lets at least try
332 * to get a message out.
333 */
334 flags = oops_begin();
335 printk(KERN_EMERG "%s", str);
336 printk(" on CPU%d, ip %08lx, registers:\n",
337 smp_processor_id(), regs->ip);
338 show_registers(regs);
339 oops_end(flags, regs, 0);
340 if (do_panic || panic_on_oops)
341 panic("Non maskable interrupt");
342 nmi_exit();
343 local_irq_enable();
344 do_exit(SIGBUS);
345}
346
347static int __init oops_setup(char *s)
348{
349 if (!s)
350 return -EINVAL;
351 if (!strcmp(s, "panic"))
352 panic_on_oops = 1;
353 return 0;
354}
355early_param("oops", oops_setup);
356
357static int __init kstack_setup(char *s)
358{
359 if (!s)
360 return -EINVAL;
361 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
362 return 0;
363}
364early_param("kstack", kstack_setup);
365
366static int __init code_bytes_setup(char *s)
367{
368 code_bytes = simple_strtoul(s, NULL, 0);
369 if (code_bytes > 8192)
370 code_bytes = 8192;
371
372 return 1;
373}
374__setup("code_bytes=", code_bytes_setup);