blob: 7fd32944ceac9dd087344e8c0a73d6ebe93da8ff [file] [log] [blame]
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +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>
16
17#include <asm/stacktrace.h>
18
19int panic_on_unrecovered_nmi;
20int kstack_depth_to_print = 12;
21static unsigned int code_bytes = 64;
22static int die_counter;
23
24void printk_address(unsigned long address, int reliable)
25{
Alexander van Heukelum16182792008-10-04 23:12:41 +020026 printk(" [<%p>] %s%pS\n", (void *) address,
27 reliable ? "" : "? ", (void *) address);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020028}
29
30static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
31 unsigned *usedp, char **idp)
32{
33 static char ids[][8] = {
34 [DEBUG_STACK - 1] = "#DB",
35 [NMI_STACK - 1] = "NMI",
36 [DOUBLEFAULT_STACK - 1] = "#DF",
37 [STACKFAULT_STACK - 1] = "#SS",
38 [MCE_STACK - 1] = "#MC",
39#if DEBUG_STKSZ > EXCEPTION_STKSZ
40 [N_EXCEPTION_STACKS ...
41 N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
42#endif
43 };
44 unsigned k;
45
46 /*
47 * Iterate over all exception stacks, and figure out whether
48 * 'stack' is in one of them:
49 */
50 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
51 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
52 /*
53 * Is 'stack' above this exception frame's end?
54 * If yes then skip to the next frame.
55 */
56 if (stack >= end)
57 continue;
58 /*
59 * Is 'stack' above this exception frame's start address?
60 * If yes then we found the right frame.
61 */
62 if (stack >= end - EXCEPTION_STKSZ) {
63 /*
64 * Make sure we only iterate through an exception
65 * stack once. If it comes up for the second time
66 * then there's something wrong going on - just
67 * break out and return NULL:
68 */
69 if (*usedp & (1U << k))
70 break;
71 *usedp |= 1U << k;
72 *idp = ids[k];
73 return (unsigned long *)end;
74 }
75 /*
76 * If this is a debug stack, and if it has a larger size than
77 * the usual exception stacks, then 'stack' might still
78 * be within the lower portion of the debug stack:
79 */
80#if DEBUG_STKSZ > EXCEPTION_STKSZ
81 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
82 unsigned j = N_EXCEPTION_STACKS - 1;
83
84 /*
85 * Black magic. A large debug stack is composed of
86 * multiple exception stack entries, which we
87 * iterate through now. Dont look:
88 */
89 do {
90 ++j;
91 end -= EXCEPTION_STKSZ;
92 ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
93 } while (stack < end - EXCEPTION_STKSZ);
94 if (*usedp & (1U << j))
95 break;
96 *usedp |= 1U << j;
97 *idp = ids[j];
98 return (unsigned long *)end;
99 }
100#endif
101 }
102 return NULL;
103}
104
105/*
106 * x86-64 can have up to three kernel stacks:
107 * process stack
108 * interrupt stack
109 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
110 */
111
112static inline int valid_stack_ptr(struct thread_info *tinfo,
113 void *p, unsigned int size, void *end)
114{
115 void *t = tinfo;
116 if (end) {
117 if (p < end && p >= (end-THREAD_SIZE))
118 return 1;
119 else
120 return 0;
121 }
122 return p > t && p < t + THREAD_SIZE - size;
123}
124
125/* The form of the top of the frame on the stack */
126struct stack_frame {
127 struct stack_frame *next_frame;
128 unsigned long return_address;
129};
130
131static inline unsigned long
132print_context_stack(struct thread_info *tinfo,
133 unsigned long *stack, unsigned long bp,
134 const struct stacktrace_ops *ops, void *data,
135 unsigned long *end)
136{
137 struct stack_frame *frame = (struct stack_frame *)bp;
138
139 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
140 unsigned long addr;
141
142 addr = *stack;
143 if (__kernel_text_address(addr)) {
Alexander van Heukelum3a185122008-10-04 23:12:42 +0200144 if ((unsigned long) stack == bp + sizeof(long)) {
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200145 ops->address(data, addr, 1);
146 frame = frame->next_frame;
147 bp = (unsigned long) frame;
148 } else {
149 ops->address(data, addr, bp == 0);
150 }
151 }
152 stack++;
153 }
154 return bp;
155}
156
157void dump_trace(struct task_struct *task, struct pt_regs *regs,
158 unsigned long *stack, unsigned long bp,
159 const struct stacktrace_ops *ops, void *data)
160{
161 const unsigned cpu = get_cpu();
162 unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
163 unsigned used = 0;
164 struct thread_info *tinfo;
165
166 if (!task)
167 task = current;
168
169 if (!stack) {
170 unsigned long dummy;
171 stack = &dummy;
172 if (task && task != current)
173 stack = (unsigned long *)task->thread.sp;
174 }
175
176#ifdef CONFIG_FRAME_POINTER
177 if (!bp) {
178 if (task == current) {
179 /* Grab bp right from our regs */
180 asm("movq %%rbp, %0" : "=r" (bp) : );
181 } else {
182 /* bp is the last reg pushed by switch_to */
183 bp = *(unsigned long *) task->thread.sp;
184 }
185 }
186#endif
187
188 /*
189 * Print function call entries in all stacks, starting at the
190 * current stack address. If the stacks consist of nested
191 * exceptions
192 */
193 tinfo = task_thread_info(task);
194 for (;;) {
195 char *id;
196 unsigned long *estack_end;
197 estack_end = in_exception_stack(cpu, (unsigned long)stack,
198 &used, &id);
199
200 if (estack_end) {
201 if (ops->stack(data, id) < 0)
202 break;
203
204 bp = print_context_stack(tinfo, stack, bp, ops,
205 data, estack_end);
206 ops->stack(data, "<EOE>");
207 /*
208 * We link to the next stack via the
209 * second-to-last pointer (index -2 to end) in the
210 * exception stack:
211 */
212 stack = (unsigned long *) estack_end[-2];
213 continue;
214 }
215 if (irqstack_end) {
216 unsigned long *irqstack;
217 irqstack = irqstack_end -
218 (IRQSTACKSIZE - 64) / sizeof(*irqstack);
219
220 if (stack >= irqstack && stack < irqstack_end) {
221 if (ops->stack(data, "IRQ") < 0)
222 break;
223 bp = print_context_stack(tinfo, stack, bp,
224 ops, data, irqstack_end);
225 /*
226 * We link to the next stack (which would be
227 * the process stack normally) the last
228 * pointer (index -1 to end) in the IRQ stack:
229 */
230 stack = (unsigned long *) (irqstack_end[-1]);
231 irqstack_end = NULL;
232 ops->stack(data, "EOI");
233 continue;
234 }
235 }
236 break;
237 }
238
239 /*
240 * This handles the process stack:
241 */
242 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL);
243 put_cpu();
244}
245EXPORT_SYMBOL(dump_trace);
246
247static void
248print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
249{
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200250 printk(data);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200251 print_symbol(msg, symbol);
252 printk("\n");
253}
254
255static void print_trace_warning(void *data, char *msg)
256{
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200257 printk("%s%s\n", (char *)data, msg);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200258}
259
260static int print_trace_stack(void *data, char *name)
261{
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200262 printk("%s <%s> ", (char *)data, name);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200263 return 0;
264}
265
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200266/*
267 * Print one address/symbol entries per line.
268 */
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200269static void print_trace_address(void *data, unsigned long addr, int reliable)
270{
271 touch_nmi_watchdog();
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200272 printk(data);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200273 printk_address(addr, reliable);
274}
275
276static const struct stacktrace_ops print_trace_ops = {
277 .warning = print_trace_warning,
278 .warning_symbol = print_trace_warning_symbol,
279 .stack = print_trace_stack,
280 .address = print_trace_address,
281};
282
283static void
284show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
285 unsigned long *stack, unsigned long bp, char *log_lvl)
286{
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200287 printk("%sCall Trace:\n", log_lvl);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200288 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
289}
290
291void show_trace(struct task_struct *task, struct pt_regs *regs,
292 unsigned long *stack, unsigned long bp)
293{
294 show_trace_log_lvl(task, regs, stack, bp, "");
295}
296
297static void
298show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
299 unsigned long *sp, unsigned long bp, char *log_lvl)
300{
301 unsigned long *stack;
302 int i;
303 const int cpu = smp_processor_id();
304 unsigned long *irqstack_end =
305 (unsigned long *) (cpu_pda(cpu)->irqstackptr);
306 unsigned long *irqstack =
307 (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
308
309 /*
310 * debugging aid: "show_stack(NULL, NULL);" prints the
311 * back trace for this cpu.
312 */
313
314 if (sp == NULL) {
315 if (task)
316 sp = (unsigned long *)task->thread.sp;
317 else
318 sp = (unsigned long *)&sp;
319 }
320
321 stack = sp;
322 for (i = 0; i < kstack_depth_to_print; i++) {
323 if (stack >= irqstack && stack <= irqstack_end) {
324 if (stack == irqstack_end) {
325 stack = (unsigned long *) (irqstack_end[-1]);
326 printk(" <EOI> ");
327 }
328 } else {
329 if (((long) stack & (THREAD_SIZE-1)) == 0)
330 break;
331 }
332 if (i && ((i % 4) == 0))
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200333 printk("\n%s", log_lvl);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200334 printk(" %016lx", *stack++);
335 touch_nmi_watchdog();
336 }
337 printk("\n");
338 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
339}
340
341void show_stack(struct task_struct *task, unsigned long *sp)
342{
343 show_stack_log_lvl(task, NULL, sp, 0, "");
344}
345
346/*
347 * The architecture-independent dump_stack generator
348 */
349void dump_stack(void)
350{
351 unsigned long bp = 0;
352 unsigned long stack;
353
354#ifdef CONFIG_FRAME_POINTER
355 if (!bp)
356 asm("movq %%rbp, %0" : "=r" (bp) : );
357#endif
358
359 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
360 current->pid, current->comm, print_tainted(),
361 init_utsname()->release,
362 (int)strcspn(init_utsname()->version, " "),
363 init_utsname()->version);
364 show_trace(NULL, NULL, &stack, bp);
365}
366EXPORT_SYMBOL(dump_stack);
367
368void show_registers(struct pt_regs *regs)
369{
370 int i;
371 unsigned long sp;
372 const int cpu = smp_processor_id();
373 struct task_struct *cur = cpu_pda(cpu)->pcurrent;
374
375 sp = regs->sp;
376 printk("CPU %d ", cpu);
377 __show_regs(regs, 1);
378 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
379 cur->comm, cur->pid, task_thread_info(cur), cur);
380
381 /*
382 * When in-kernel, we also print out the stack and code at the
383 * time of the fault..
384 */
385 if (!user_mode(regs)) {
386 unsigned int code_prologue = code_bytes * 43 / 64;
387 unsigned int code_len = code_bytes;
388 unsigned char c;
389 u8 *ip;
390
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200391 printk(KERN_EMERG "Stack:\n");
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200392 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200393 regs->bp, KERN_EMERG);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200394
395 printk(KERN_EMERG "Code: ");
396
397 ip = (u8 *)regs->ip - code_prologue;
398 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
399 /* try starting at RIP */
400 ip = (u8 *)regs->ip;
401 code_len = code_len - code_prologue + 1;
402 }
403 for (i = 0; i < code_len; i++, ip++) {
404 if (ip < (u8 *)PAGE_OFFSET ||
405 probe_kernel_address(ip, c)) {
406 printk(" Bad RIP value.");
407 break;
408 }
409 if (ip == (u8 *)regs->ip)
410 printk("<%02x> ", c);
411 else
412 printk("%02x ", c);
413 }
414 }
415 printk("\n");
416}
417
418int is_valid_bugaddr(unsigned long ip)
419{
420 unsigned short ud2;
421
422 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
423 return 0;
424
425 return ud2 == 0x0b0f;
426}
427
428static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
429static int die_owner = -1;
430static unsigned int die_nest_count;
431
432unsigned __kprobes long oops_begin(void)
433{
434 int cpu;
435 unsigned long flags;
436
437 oops_enter();
438
439 /* racy, but better than risking deadlock. */
440 raw_local_irq_save(flags);
441 cpu = smp_processor_id();
442 if (!__raw_spin_trylock(&die_lock)) {
443 if (cpu == die_owner)
444 /* nested oops. should stop eventually */;
445 else
446 __raw_spin_lock(&die_lock);
447 }
448 die_nest_count++;
449 die_owner = cpu;
450 console_verbose();
451 bust_spinlocks(1);
452 return flags;
453}
454
455void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
456{
457 die_owner = -1;
458 bust_spinlocks(0);
459 die_nest_count--;
460 if (!die_nest_count)
461 /* Nest count reaches zero, release the lock. */
462 __raw_spin_unlock(&die_lock);
463 raw_local_irq_restore(flags);
464 if (!regs) {
465 oops_exit();
466 return;
467 }
468 if (in_interrupt())
469 panic("Fatal exception in interrupt");
470 if (panic_on_oops)
471 panic("Fatal exception");
472 oops_exit();
473 do_exit(signr);
474}
475
476int __kprobes __die(const char *str, struct pt_regs *regs, long err)
477{
478 printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
479#ifdef CONFIG_PREEMPT
480 printk("PREEMPT ");
481#endif
482#ifdef CONFIG_SMP
483 printk("SMP ");
484#endif
485#ifdef CONFIG_DEBUG_PAGEALLOC
486 printk("DEBUG_PAGEALLOC");
487#endif
488 printk("\n");
489 if (notify_die(DIE_OOPS, str, regs, err,
490 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
491 return 1;
492
493 show_registers(regs);
494 add_taint(TAINT_DIE);
495 /* Executive summary in case the oops scrolled away */
496 printk(KERN_ALERT "RIP ");
497 printk_address(regs->ip, 1);
498 printk(" RSP <%016lx>\n", regs->sp);
499 if (kexec_should_crash(current))
500 crash_kexec(regs);
501 return 0;
502}
503
504void die(const char *str, struct pt_regs *regs, long err)
505{
506 unsigned long flags = oops_begin();
507
508 if (!user_mode(regs))
509 report_bug(regs->ip, regs);
510
511 if (__die(str, regs, err))
512 regs = NULL;
513 oops_end(flags, regs, SIGSEGV);
514}
515
516notrace __kprobes void
517die_nmi(char *str, struct pt_regs *regs, int do_panic)
518{
519 unsigned long flags;
520
521 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
522 return;
523
524 flags = oops_begin();
525 /*
526 * We are in trouble anyway, lets at least try
527 * to get a message out.
528 */
529 printk(KERN_EMERG "%s", str);
530 printk(" on CPU%d, ip %08lx, registers:\n",
531 smp_processor_id(), regs->ip);
532 show_registers(regs);
533 if (kexec_should_crash(current))
534 crash_kexec(regs);
535 if (do_panic || panic_on_oops)
536 panic("Non maskable interrupt");
537 oops_end(flags, NULL, SIGBUS);
538 nmi_exit();
539 local_irq_enable();
540 do_exit(SIGBUS);
541}
542
543static int __init oops_setup(char *s)
544{
545 if (!s)
546 return -EINVAL;
547 if (!strcmp(s, "panic"))
548 panic_on_oops = 1;
549 return 0;
550}
551early_param("oops", oops_setup);
552
553static int __init kstack_setup(char *s)
554{
555 if (!s)
556 return -EINVAL;
557 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
558 return 0;
559}
560early_param("kstack", kstack_setup);
561
562static int __init code_bytes_setup(char *s)
563{
564 code_bytes = simple_strtoul(s, NULL, 0);
565 if (code_bytes > 8192)
566 code_bytes = 8192;
567
568 return 1;
569}
570__setup("code_bytes=", code_bytes_setup);