blob: e91ae34f9684fae97e0ed7fecd4f623488a5a733 [file] [log] [blame]
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +02001/*
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>
13#include <linux/kexec.h>
14#include <linux/bug.h>
15#include <linux/nmi.h>
Andrew Mortonae872212007-08-24 16:11:54 -070016#include <linux/sysfs.h>
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020017
18#include <asm/stacktrace.h>
19
Alexander van Heukelum8a541662008-10-04 23:12:46 +020020#define STACKSLOTS_PER_LINE 8
21#define get_bp(bp) asm("movl %%ebp, %0" : "=r" (bp) :)
22
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020023int panic_on_unrecovered_nmi;
Alexander van Heukelum8a541662008-10-04 23:12:46 +020024int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020025static unsigned int code_bytes = 64;
26static int die_counter;
27
28void printk_address(unsigned long address, int reliable)
29{
Alexander van Heukelum16182792008-10-04 23:12:41 +020030 printk(" [<%p>] %s%pS\n", (void *) address,
31 reliable ? "" : "? ", (void *) address);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020032}
33
34static inline int valid_stack_ptr(struct thread_info *tinfo,
Alexander van Heukelum3a185122008-10-04 23:12:42 +020035 void *p, unsigned int size, void *end)
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020036{
37 void *t = tinfo;
Alexander van Heukelum3a185122008-10-04 23:12:42 +020038 if (end) {
39 if (p < end && p >= (end-THREAD_SIZE))
40 return 1;
41 else
42 return 0;
43 }
44 return p > t && p < t + THREAD_SIZE - size;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020045}
46
47/* The form of the top of the frame on the stack */
48struct stack_frame {
49 struct stack_frame *next_frame;
50 unsigned long return_address;
51};
52
53static inline unsigned long
54print_context_stack(struct thread_info *tinfo,
55 unsigned long *stack, unsigned long bp,
Alexander van Heukelum3a185122008-10-04 23:12:42 +020056 const struct stacktrace_ops *ops, void *data,
57 unsigned long *end)
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020058{
59 struct stack_frame *frame = (struct stack_frame *)bp;
60
Alexander van Heukelum3a185122008-10-04 23:12:42 +020061 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020062 unsigned long addr;
63
64 addr = *stack;
65 if (__kernel_text_address(addr)) {
Alexander van Heukelum3a185122008-10-04 23:12:42 +020066 if ((unsigned long) stack == bp + sizeof(long)) {
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020067 ops->address(data, addr, 1);
68 frame = frame->next_frame;
69 bp = (unsigned long) frame;
70 } else {
71 ops->address(data, addr, bp == 0);
72 }
73 }
74 stack++;
75 }
76 return bp;
77}
78
79void dump_trace(struct task_struct *task, struct pt_regs *regs,
80 unsigned long *stack, unsigned long bp,
81 const struct stacktrace_ops *ops, void *data)
82{
83 if (!task)
84 task = current;
85
86 if (!stack) {
87 unsigned long dummy;
88 stack = &dummy;
Alexander van Heukelum8a541662008-10-04 23:12:46 +020089 if (task && task != current)
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020090 stack = (unsigned long *)task->thread.sp;
91 }
92
93#ifdef CONFIG_FRAME_POINTER
94 if (!bp) {
95 if (task == current) {
96 /* Grab bp right from our regs */
Alexander van Heukelum8a541662008-10-04 23:12:46 +020097 get_bp(bp);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020098 } else {
99 /* bp is the last reg pushed by switch_to */
100 bp = *(unsigned long *) task->thread.sp;
101 }
102 }
103#endif
104
105 for (;;) {
106 struct thread_info *context;
107
108 context = (struct thread_info *)
109 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
Alexander van Heukelum3a185122008-10-04 23:12:42 +0200110 bp = print_context_stack(context, stack, bp, ops, data, NULL);
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200111
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200112 stack = (unsigned long *)context->previous_esp;
113 if (!stack)
114 break;
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200115 if (ops->stack(data, "IRQ") < 0)
116 break;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200117 touch_nmi_watchdog();
118 }
119}
120EXPORT_SYMBOL(dump_trace);
121
122static void
123print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
124{
125 printk(data);
126 print_symbol(msg, symbol);
127 printk("\n");
128}
129
130static void print_trace_warning(void *data, char *msg)
131{
132 printk("%s%s\n", (char *)data, msg);
133}
134
135static int print_trace_stack(void *data, char *name)
136{
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200137 printk("%s <%s> ", (char *)data, name);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200138 return 0;
139}
140
141/*
142 * Print one address/symbol entries per line.
143 */
144static void print_trace_address(void *data, unsigned long addr, int reliable)
145{
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200146 touch_nmi_watchdog();
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200147 printk(data);
148 printk_address(addr, reliable);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200149}
150
151static const struct stacktrace_ops print_trace_ops = {
152 .warning = print_trace_warning,
153 .warning_symbol = print_trace_warning_symbol,
154 .stack = print_trace_stack,
155 .address = print_trace_address,
156};
157
158static void
159show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
160 unsigned long *stack, unsigned long bp, char *log_lvl)
161{
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200162 printk("%sCall Trace:\n", log_lvl);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200163 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200164}
165
166void show_trace(struct task_struct *task, struct pt_regs *regs,
167 unsigned long *stack, unsigned long bp)
168{
169 show_trace_log_lvl(task, regs, stack, bp, "");
170}
171
172static void
173show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200174 unsigned long *sp, unsigned long bp, char *log_lvl)
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200175{
176 unsigned long *stack;
177 int i;
178
179 if (sp == NULL) {
180 if (task)
181 sp = (unsigned long *)task->thread.sp;
182 else
183 sp = (unsigned long *)&sp;
184 }
185
186 stack = sp;
187 for (i = 0; i < kstack_depth_to_print; i++) {
188 if (kstack_end(stack))
189 break;
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200190 if (i && ((i % STACKSLOTS_PER_LINE) == 0))
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200191 printk("\n%s", log_lvl);
192 printk(" %08lx", *stack++);
193 touch_nmi_watchdog();
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200194 }
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200195 printk("\n");
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200196 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
197}
198
199void show_stack(struct task_struct *task, unsigned long *sp)
200{
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200201 show_stack_log_lvl(task, NULL, sp, 0, "");
202}
203
204/*
205 * The architecture-independent dump_stack generator
206 */
207void dump_stack(void)
208{
209 unsigned long bp = 0;
210 unsigned long stack;
211
212#ifdef CONFIG_FRAME_POINTER
213 if (!bp)
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200214 get_bp(bp);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200215#endif
216
217 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
218 current->pid, current->comm, print_tainted(),
219 init_utsname()->release,
220 (int)strcspn(init_utsname()->version, " "),
221 init_utsname()->version);
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200222 show_trace(NULL, NULL, &stack, bp);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200223}
224
225EXPORT_SYMBOL(dump_stack);
226
227void show_registers(struct pt_regs *regs)
228{
229 int i;
230
231 print_modules();
232 __show_regs(regs, 0);
233
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200234 printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)\n",
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200235 TASK_COMM_LEN, current->comm, task_pid_nr(current),
236 current_thread_info(), current, task_thread_info(current));
237 /*
238 * When in-kernel, we also print out the stack and code at the
239 * time of the fault..
240 */
241 if (!user_mode_vm(regs)) {
242 unsigned int code_prologue = code_bytes * 43 / 64;
243 unsigned int code_len = code_bytes;
244 unsigned char c;
245 u8 *ip;
246
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200247 printk(KERN_EMERG "Stack:\n");
248 show_stack_log_lvl(NULL, regs, &regs->sp,
249 0, KERN_EMERG);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200250
251 printk(KERN_EMERG "Code: ");
252
253 ip = (u8 *)regs->ip - code_prologue;
254 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200255 /* try starting at IP */
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200256 ip = (u8 *)regs->ip;
257 code_len = code_len - code_prologue + 1;
258 }
259 for (i = 0; i < code_len; i++, ip++) {
260 if (ip < (u8 *)PAGE_OFFSET ||
261 probe_kernel_address(ip, c)) {
262 printk(" Bad EIP value.");
263 break;
264 }
265 if (ip == (u8 *)regs->ip)
266 printk("<%02x> ", c);
267 else
268 printk("%02x ", c);
269 }
270 }
271 printk("\n");
272}
273
274int is_valid_bugaddr(unsigned long ip)
275{
276 unsigned short ud2;
277
278 if (ip < PAGE_OFFSET)
279 return 0;
280 if (probe_kernel_address((unsigned short *)ip, ud2))
281 return 0;
282
283 return ud2 == 0x0b0f;
284}
285
286static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
287static int die_owner = -1;
288static unsigned int die_nest_count;
289
290unsigned __kprobes long oops_begin(void)
291{
Alexander van Heukelume4955cf2008-10-22 12:00:12 +0200292 int cpu;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200293 unsigned long flags;
294
295 oops_enter();
296
Alexander van Heukelume4955cf2008-10-22 12:00:12 +0200297 /* racy, but better than risking deadlock. */
298 raw_local_irq_save(flags);
299 cpu = smp_processor_id();
300 if (!__raw_spin_trylock(&die_lock)) {
301 if (cpu == die_owner)
302 /* nested oops. should stop eventually */;
303 else
304 __raw_spin_lock(&die_lock);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200305 }
306 die_nest_count++;
Alexander van Heukelume4955cf2008-10-22 12:00:12 +0200307 die_owner = cpu;
308 console_verbose();
309 bust_spinlocks(1);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200310 return flags;
311}
312
313void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
314{
Alexander van Heukelumb4b8f872008-10-22 12:00:08 +0200315 if (regs && kexec_should_crash(current))
316 crash_kexec(regs);
317
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200318 bust_spinlocks(0);
319 die_owner = -1;
320 add_taint(TAINT_DIE);
Alexander van Heukelume4955cf2008-10-22 12:00:12 +0200321 die_nest_count--;
322 if (!die_nest_count)
323 /* Nest count reaches zero, release the lock. */
324 __raw_spin_unlock(&die_lock);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200325 raw_local_irq_restore(flags);
Alexander van Heukelum10b14cb2008-10-22 12:00:11 +0200326 oops_exit();
Alexander van Heukelume4955cf2008-10-22 12:00:12 +0200327
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200328 if (!signr)
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200329 return;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200330 if (in_interrupt())
331 panic("Fatal exception in interrupt");
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200332 if (panic_on_oops)
333 panic("Fatal exception");
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200334 do_exit(signr);
335}
336
337int __kprobes __die(const char *str, struct pt_regs *regs, long err)
338{
339 unsigned short ss;
340 unsigned long sp;
341
342 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
343#ifdef CONFIG_PREEMPT
344 printk("PREEMPT ");
345#endif
346#ifdef CONFIG_SMP
347 printk("SMP ");
348#endif
349#ifdef CONFIG_DEBUG_PAGEALLOC
350 printk("DEBUG_PAGEALLOC");
351#endif
352 printk("\n");
Andrew Mortonae872212007-08-24 16:11:54 -0700353 sysfs_printk_last_file();
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200354 if (notify_die(DIE_OOPS, str, regs, err,
355 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
356 return 1;
357
358 show_registers(regs);
359 /* Executive summary in case the oops scrolled away */
360 sp = (unsigned long) (&regs->sp);
361 savesegment(ss, ss);
362 if (user_mode(regs)) {
363 sp = regs->sp;
364 ss = regs->ss & 0xffff;
365 }
366 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
367 print_symbol("%s", regs->ip);
368 printk(" SS:ESP %04x:%08lx\n", ss, sp);
369 return 0;
370}
371
372/*
373 * This is gone through when something in the kernel has done something bad
374 * and is about to be terminated:
375 */
376void die(const char *str, struct pt_regs *regs, long err)
377{
378 unsigned long flags = oops_begin();
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200379 int sig = SIGSEGV;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200380
381 if (die_nest_count < 3) {
382 report_bug(regs->ip, regs);
383
384 if (__die(str, regs, err))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200385 sig = 0;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200386 } else {
387 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
388 }
389
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200390 oops_end(flags, regs, sig);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200391}
392
Alexander van Heukelumdd6e4eb2008-10-04 23:12:40 +0200393void notrace __kprobes
394die_nmi(char *str, struct pt_regs *regs, int do_panic)
395{
Alexander van Heukelume06ca432008-10-22 12:00:13 +0200396 unsigned long flags;
397
Alexander van Heukelumdd6e4eb2008-10-04 23:12:40 +0200398 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
399 return;
400
Alexander van Heukelumdd6e4eb2008-10-04 23:12:40 +0200401 /*
Alexander van Heukelume06ca432008-10-22 12:00:13 +0200402 * We are in trouble anyway, lets at least try
403 * to get a message out.
404 */
405 flags = oops_begin();
Alexander van Heukelumdd6e4eb2008-10-04 23:12:40 +0200406 printk(KERN_EMERG "%s", str);
407 printk(" on CPU%d, ip %08lx, registers:\n",
408 smp_processor_id(), regs->ip);
409 show_registers(regs);
Alexander van Heukelume06ca432008-10-22 12:00:13 +0200410 oops_end(flags, regs, 0);
411 if (do_panic || panic_on_oops)
Alexander van Heukelumdd6e4eb2008-10-04 23:12:40 +0200412 panic("Non maskable interrupt");
Alexander van Heukelume06ca432008-10-22 12:00:13 +0200413 nmi_exit();
414 local_irq_enable();
415 do_exit(SIGBUS);
Alexander van Heukelumdd6e4eb2008-10-04 23:12:40 +0200416}
417
Alexander van Heukelum802a67d2008-10-04 23:12:45 +0200418static int __init oops_setup(char *s)
419{
420 if (!s)
421 return -EINVAL;
422 if (!strcmp(s, "panic"))
423 panic_on_oops = 1;
424 return 0;
425}
426early_param("oops", oops_setup);
427
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200428static int __init kstack_setup(char *s)
429{
Alexander van Heukelum802a67d2008-10-04 23:12:45 +0200430 if (!s)
431 return -EINVAL;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200432 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
Alexander van Heukelum802a67d2008-10-04 23:12:45 +0200433 return 0;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200434}
Alexander van Heukelum802a67d2008-10-04 23:12:45 +0200435early_param("kstack", kstack_setup);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200436
437static int __init code_bytes_setup(char *s)
438{
439 code_bytes = simple_strtoul(s, NULL, 0);
440 if (code_bytes > 8192)
441 code_bytes = 8192;
442
443 return 1;
444}
445__setup("code_bytes=", code_bytes_setup);