Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / dumpstack.c
1 /*
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/ftrace.h>
14 #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 #include <linux/unwind.h>
21
22
23 int panic_on_unrecovered_nmi;
24 int panic_on_io_nmi;
25 unsigned int code_bytes = 64;
26 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
27 #ifdef CONFIG_STACK_UNWIND
28 static int call_trace = 1;
29 #else
30 #define call_trace (-1)
31 #endif
32 static int die_counter;
33
34 void printk_address(unsigned long address, int reliable)
35 {
36         printk(" [<%p>] %s%pB\n", (void *) address,
37                         reliable ? "" : "? ", (void *) address);
38 }
39
40 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
41 static void
42 print_ftrace_graph_addr(unsigned long addr, void *data,
43                         const struct stacktrace_ops *ops,
44                         struct thread_info *tinfo, int *graph)
45 {
46         struct task_struct *task;
47         unsigned long ret_addr;
48         int index;
49
50         if (addr != (unsigned long)return_to_handler)
51                 return;
52
53         task = tinfo->task;
54         index = task->curr_ret_stack;
55
56         if (!task->ret_stack || index < *graph)
57                 return;
58
59         index -= *graph;
60         ret_addr = task->ret_stack[index].ret;
61
62         ops->address(data, ret_addr, 1);
63
64         (*graph)++;
65 }
66 #else
67 static inline void
68 print_ftrace_graph_addr(unsigned long addr, void *data,
69                         const struct stacktrace_ops *ops,
70                         struct thread_info *tinfo, int *graph)
71 { }
72 #endif
73
74 int asmlinkage dump_trace_unwind(struct unwind_frame_info *info,
75                       const struct stacktrace_ops *ops, void *data)
76 {
77         int n = 0;
78 #ifdef CONFIG_STACK_UNWIND
79         unsigned long sp = UNW_SP(info);
80
81         if (arch_unw_user_mode(info))
82                 return -1;
83         while (unwind(info) == 0 && UNW_PC(info)) {
84                 n++;
85                 ops->address(data, UNW_PC(info), 1);
86                 if (arch_unw_user_mode(info))
87                         break;
88                 if ((sp & ~(PAGE_SIZE - 1)) == (UNW_SP(info) & ~(PAGE_SIZE - 1))
89                     && sp > UNW_SP(info))
90                         break;
91                 sp = UNW_SP(info);
92         }
93 #endif
94         return n;
95 }
96
97 int try_stack_unwind(struct task_struct *task, struct pt_regs *regs,
98                      unsigned long **stack, unsigned long *bp,
99                      const struct stacktrace_ops *ops, void *data)
100 {
101 #ifdef CONFIG_STACK_UNWIND
102         int unw_ret = 0;
103         struct unwind_frame_info info;
104         if (call_trace < 0)
105                 return 0;
106
107         if (regs) {
108                 if (unwind_init_frame_info(&info, task, regs) == 0)
109                         unw_ret = dump_trace_unwind(&info, ops, data);
110         } else if (task == current)
111                 unw_ret = unwind_init_running(&info, dump_trace_unwind, ops, data);
112 #ifdef CONFIG_SMP
113         else if (task->on_cpu)
114                 /* nothing */;
115 #endif
116         else if (unwind_init_blocked(&info, task) == 0)
117                 unw_ret = dump_trace_unwind(&info, ops, data);
118         if (unw_ret > 0) {
119                 if (call_trace == 1 && !arch_unw_user_mode(&info)) {
120                         ops->warning_symbol(data, "DWARF2 unwinder stuck at %s\n",
121                                             UNW_PC(&info));
122                         if (UNW_SP(&info) >= PAGE_OFFSET) {
123                                 ops->warning(data, "Leftover inexact backtrace:\n");
124                                 *stack = (void *)UNW_SP(&info);
125                                 *bp = UNW_FP(&info);
126                                 return 0;
127                         }
128                 } else if (call_trace >= 1)
129                         return -1;
130                 ops->warning(data, "Full inexact backtrace again:\n");
131         } else
132                 ops->warning(data, "Inexact backtrace:\n");
133 #endif
134         return 0;
135 }
136
137 /*
138  * x86-64 can have up to three kernel stacks:
139  * process stack
140  * interrupt stack
141  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
142  */
143
144 static inline int valid_stack_ptr(struct thread_info *tinfo,
145                         void *p, unsigned int size, void *end)
146 {
147         void *t = tinfo;
148         if (end) {
149                 if (p < end && p >= (end-THREAD_SIZE))
150                         return 1;
151                 else
152                         return 0;
153         }
154         return p > t && p < t + THREAD_SIZE - size;
155 }
156
157 unsigned long
158 print_context_stack(struct thread_info *tinfo,
159                 unsigned long *stack, unsigned long bp,
160                 const struct stacktrace_ops *ops, void *data,
161                 unsigned long *end, int *graph)
162 {
163         struct stack_frame *frame = (struct stack_frame *)bp;
164
165         while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
166                 unsigned long addr;
167
168                 addr = *stack;
169                 if (__kernel_text_address(addr)) {
170                         if ((unsigned long) stack == bp + sizeof(long)) {
171                                 ops->address(data, addr, 1);
172                                 frame = frame->next_frame;
173                                 bp = (unsigned long) frame;
174                         } else {
175                                 ops->address(data, addr, 0);
176                         }
177                         print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
178                 }
179                 stack++;
180         }
181         return bp;
182 }
183 EXPORT_SYMBOL_GPL(print_context_stack);
184
185 unsigned long
186 print_context_stack_bp(struct thread_info *tinfo,
187                        unsigned long *stack, unsigned long bp,
188                        const struct stacktrace_ops *ops, void *data,
189                        unsigned long *end, int *graph)
190 {
191         struct stack_frame *frame = (struct stack_frame *)bp;
192         unsigned long *ret_addr = &frame->return_address;
193
194         while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
195                 unsigned long addr = *ret_addr;
196
197                 if (!__kernel_text_address(addr))
198                         break;
199
200                 ops->address(data, addr, 1);
201                 frame = frame->next_frame;
202                 ret_addr = &frame->return_address;
203                 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
204         }
205
206         return (unsigned long)frame;
207 }
208 EXPORT_SYMBOL_GPL(print_context_stack_bp);
209
210
211 static void
212 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
213 {
214         printk(data);
215         print_symbol(msg, symbol);
216         printk("\n");
217 }
218
219 static void print_trace_warning(void *data, char *msg)
220 {
221         printk("%s%s\n", (char *)data, msg);
222 }
223
224 static int print_trace_stack(void *data, char *name)
225 {
226         printk("%s <%s> ", (char *)data, name);
227         return 0;
228 }
229
230 /*
231  * Print one address/symbol entries per line.
232  */
233 static void print_trace_address(void *data, unsigned long addr, int reliable)
234 {
235         touch_nmi_watchdog();
236         printk(data);
237         printk_address(addr, reliable);
238 }
239
240 static const struct stacktrace_ops print_trace_ops = {
241         .warning                = print_trace_warning,
242         .warning_symbol         = print_trace_warning_symbol,
243         .stack                  = print_trace_stack,
244         .address                = print_trace_address,
245         .walk_stack             = print_context_stack,
246 };
247
248 void
249 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
250                 unsigned long *stack, unsigned long bp, char *log_lvl)
251 {
252         printk("%sCall Trace:\n", log_lvl);
253         dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
254 }
255
256 void show_trace(struct task_struct *task, struct pt_regs *regs,
257                 unsigned long *stack, unsigned long bp)
258 {
259         show_trace_log_lvl(task, regs, stack, bp, "");
260 }
261
262 void show_stack(struct task_struct *task, unsigned long *sp)
263 {
264         show_stack_log_lvl(task, NULL, sp, 0, "");
265 }
266
267 /*
268  * The architecture-independent dump_stack generator
269  */
270 void dump_stack(void)
271 {
272         unsigned long bp;
273         unsigned long stack;
274
275         bp = stack_frame(current, NULL);
276         printk("Pid: %d, comm: %.20s %s %s %.*s\n",
277                 current->pid, current->comm, print_tainted(),
278                 init_utsname()->release,
279                 (int)strcspn(init_utsname()->version, " "),
280                 init_utsname()->version);
281         show_trace(NULL, NULL, &stack, bp);
282 }
283 EXPORT_SYMBOL(dump_stack);
284
285 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
286 static int die_owner = -1;
287 static unsigned int die_nest_count;
288
289 unsigned __kprobes long oops_begin(void)
290 {
291         int cpu;
292         unsigned long flags;
293
294         oops_enter();
295
296         /* racy, but better than risking deadlock. */
297         raw_local_irq_save(flags);
298         cpu = smp_processor_id();
299         if (!arch_spin_trylock(&die_lock)) {
300                 if (cpu == die_owner)
301                         /* nested oops. should stop eventually */;
302                 else
303                         arch_spin_lock(&die_lock);
304         }
305         die_nest_count++;
306         die_owner = cpu;
307         console_verbose();
308         bust_spinlocks(1);
309         return flags;
310 }
311 EXPORT_SYMBOL_GPL(oops_begin);
312
313 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
314 {
315         if (regs && kexec_should_crash(current))
316                 crash_kexec(regs);
317
318         bust_spinlocks(0);
319         die_owner = -1;
320         add_taint(TAINT_DIE);
321         die_nest_count--;
322         if (!die_nest_count)
323                 /* Nest count reaches zero, release the lock. */
324                 arch_spin_unlock(&die_lock);
325         raw_local_irq_restore(flags);
326         oops_exit();
327
328         if (!signr)
329                 return;
330         if (in_interrupt())
331                 panic("Fatal exception in interrupt");
332         if (panic_on_oops)
333                 panic("Fatal exception");
334         do_exit(signr);
335 }
336
337 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
338 {
339 #ifdef CONFIG_X86_32
340         unsigned short ss;
341         unsigned long sp;
342 #endif
343         printk(KERN_DEFAULT
344                "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
345 #ifdef CONFIG_PREEMPT
346         printk("PREEMPT ");
347 #endif
348 #ifdef CONFIG_SMP
349         printk("SMP ");
350 #endif
351 #ifdef CONFIG_DEBUG_PAGEALLOC
352         printk("DEBUG_PAGEALLOC");
353 #endif
354         printk("\n");
355         if (notify_die(DIE_OOPS, str, regs, err,
356                         current->thread.trap_nr, SIGSEGV) == NOTIFY_STOP)
357                 return 1;
358
359         show_registers(regs);
360 #ifdef CONFIG_X86_32
361         if (user_mode_vm(regs)) {
362                 sp = regs->sp;
363                 ss = regs->ss & 0xffff;
364         } else {
365                 sp = kernel_stack_pointer(regs);
366                 savesegment(ss, ss);
367         }
368         printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
369         print_symbol("%s", regs->ip);
370         printk(" SS:ESP %04x:%08lx\n", ss, sp);
371 #else
372         /* Executive summary in case the oops scrolled away */
373         printk(KERN_ALERT "RIP ");
374         printk_address(regs->ip, 1);
375         printk(" RSP <%016lx>\n", regs->sp);
376 #endif
377         return 0;
378 }
379
380 /*
381  * This is gone through when something in the kernel has done something bad
382  * and is about to be terminated:
383  */
384 void die(const char *str, struct pt_regs *regs, long err)
385 {
386         unsigned long flags = oops_begin();
387         int sig = SIGSEGV;
388
389         if (!user_mode_vm(regs))
390                 report_bug(regs->ip, regs);
391
392         if (__die(str, regs, err))
393                 sig = 0;
394         oops_end(flags, regs, sig);
395 }
396
397 static int __init kstack_setup(char *s)
398 {
399         if (!s)
400                 return -EINVAL;
401         kstack_depth_to_print = simple_strtoul(s, NULL, 0);
402         return 0;
403 }
404 early_param("kstack", kstack_setup);
405
406 static int __init code_bytes_setup(char *s)
407 {
408         code_bytes = simple_strtoul(s, NULL, 0);
409         if (code_bytes > 8192)
410                 code_bytes = 8192;
411
412         return 1;
413 }
414 __setup("code_bytes=", code_bytes_setup);
415
416 #ifdef CONFIG_STACK_UNWIND
417 static int __init call_trace_setup(char *s)
418 {
419         if (!s)
420                 return -EINVAL;
421         if (strcmp(s, "old") == 0)
422                 call_trace = -1;
423         else if (strcmp(s, "both") == 0)
424                 call_trace = 0;
425         else if (strcmp(s, "newfallback") == 0)
426                 call_trace = 1;
427         else if (strcmp(s, "new") == 0)
428                 call_trace = 2;
429         return 0;
430 }
431 early_param("call_trace", call_trace_setup);
432 #endif