ACPI, APEI, Generic Hardware Error Source POLL/IRQ/NMI notification type support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / dumpstack.c
blobd34cf80ec402baedf19c84727a4b5cd1f469a352
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>
19 #include <asm/stacktrace.h>
22 int panic_on_unrecovered_nmi;
23 int panic_on_io_nmi;
24 unsigned int code_bytes = 64;
25 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
26 static int die_counter;
28 void printk_address(unsigned long address, int reliable)
30 printk(" [<%p>] %s%pS\n", (void *) address,
31 reliable ? "" : "? ", (void *) address);
34 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
35 static void
36 print_ftrace_graph_addr(unsigned long addr, void *data,
37 const struct stacktrace_ops *ops,
38 struct thread_info *tinfo, int *graph)
40 struct task_struct *task = tinfo->task;
41 unsigned long ret_addr;
42 int index = task->curr_ret_stack;
44 if (addr != (unsigned long)return_to_handler)
45 return;
47 if (!task->ret_stack || index < *graph)
48 return;
50 index -= *graph;
51 ret_addr = task->ret_stack[index].ret;
53 ops->address(data, ret_addr, 1);
55 (*graph)++;
57 #else
58 static inline void
59 print_ftrace_graph_addr(unsigned long addr, void *data,
60 const struct stacktrace_ops *ops,
61 struct thread_info *tinfo, int *graph)
62 { }
63 #endif
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
72 static inline int valid_stack_ptr(struct thread_info *tinfo,
73 void *p, unsigned int size, void *end)
75 void *t = tinfo;
76 if (end) {
77 if (p < end && p >= (end-THREAD_SIZE))
78 return 1;
79 else
80 return 0;
82 return p > t && p < t + THREAD_SIZE - size;
85 unsigned long
86 print_context_stack(struct thread_info *tinfo,
87 unsigned long *stack, unsigned long bp,
88 const struct stacktrace_ops *ops, void *data,
89 unsigned long *end, int *graph)
91 struct stack_frame *frame = (struct stack_frame *)bp;
93 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
94 unsigned long addr;
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 {
103 ops->address(data, addr, 0);
105 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
107 stack++;
109 return bp;
111 EXPORT_SYMBOL_GPL(print_context_stack);
113 unsigned long
114 print_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)
119 struct stack_frame *frame = (struct stack_frame *)bp;
120 unsigned long *ret_addr = &frame->return_address;
122 while (valid_stack_ptr(tinfo, ret_addr, sizeof(*ret_addr), end)) {
123 unsigned long addr = *ret_addr;
125 if (!__kernel_text_address(addr))
126 break;
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);
134 return (unsigned long)frame;
136 EXPORT_SYMBOL_GPL(print_context_stack_bp);
139 static void
140 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
142 printk(data);
143 print_symbol(msg, symbol);
144 printk("\n");
147 static void print_trace_warning(void *data, char *msg)
149 printk("%s%s\n", (char *)data, msg);
152 static int print_trace_stack(void *data, char *name)
154 printk("%s <%s> ", (char *)data, name);
155 return 0;
159 * Print one address/symbol entries per line.
161 static void print_trace_address(void *data, unsigned long addr, int reliable)
163 touch_nmi_watchdog();
164 printk(data);
165 printk_address(addr, reliable);
168 static const struct stacktrace_ops print_trace_ops = {
169 .warning = print_trace_warning,
170 .warning_symbol = print_trace_warning_symbol,
171 .stack = print_trace_stack,
172 .address = print_trace_address,
173 .walk_stack = print_context_stack,
176 void
177 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
178 unsigned long *stack, unsigned long bp, char *log_lvl)
180 printk("%sCall Trace:\n", log_lvl);
181 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
184 void show_trace(struct task_struct *task, struct pt_regs *regs,
185 unsigned long *stack, unsigned long bp)
187 show_trace_log_lvl(task, regs, stack, bp, "");
190 void show_stack(struct task_struct *task, unsigned long *sp)
192 show_stack_log_lvl(task, NULL, sp, 0, "");
196 * The architecture-independent dump_stack generator
198 void dump_stack(void)
200 unsigned long bp = 0;
201 unsigned long stack;
203 #ifdef CONFIG_FRAME_POINTER
204 if (!bp)
205 get_bp(bp);
206 #endif
208 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
209 current->pid, current->comm, print_tainted(),
210 init_utsname()->release,
211 (int)strcspn(init_utsname()->version, " "),
212 init_utsname()->version);
213 show_trace(NULL, NULL, &stack, bp);
215 EXPORT_SYMBOL(dump_stack);
217 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
218 static int die_owner = -1;
219 static unsigned int die_nest_count;
221 unsigned __kprobes long oops_begin(void)
223 int cpu;
224 unsigned long flags;
226 oops_enter();
228 /* racy, but better than risking deadlock. */
229 raw_local_irq_save(flags);
230 cpu = smp_processor_id();
231 if (!arch_spin_trylock(&die_lock)) {
232 if (cpu == die_owner)
233 /* nested oops. should stop eventually */;
234 else
235 arch_spin_lock(&die_lock);
237 die_nest_count++;
238 die_owner = cpu;
239 console_verbose();
240 bust_spinlocks(1);
241 return flags;
243 EXPORT_SYMBOL_GPL(oops_begin);
245 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
247 if (regs && kexec_should_crash(current))
248 crash_kexec(regs);
250 bust_spinlocks(0);
251 die_owner = -1;
252 add_taint(TAINT_DIE);
253 die_nest_count--;
254 if (!die_nest_count)
255 /* Nest count reaches zero, release the lock. */
256 arch_spin_unlock(&die_lock);
257 raw_local_irq_restore(flags);
258 oops_exit();
260 if (!signr)
261 return;
262 if (in_interrupt())
263 panic("Fatal exception in interrupt");
264 if (panic_on_oops)
265 panic("Fatal exception");
266 do_exit(signr);
269 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
271 #ifdef CONFIG_X86_32
272 unsigned short ss;
273 unsigned long sp;
274 #endif
275 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
276 #ifdef CONFIG_PREEMPT
277 printk("PREEMPT ");
278 #endif
279 #ifdef CONFIG_SMP
280 printk("SMP ");
281 #endif
282 #ifdef CONFIG_DEBUG_PAGEALLOC
283 printk("DEBUG_PAGEALLOC");
284 #endif
285 printk("\n");
286 sysfs_printk_last_file();
287 if (notify_die(DIE_OOPS, str, regs, err,
288 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
289 return 1;
291 show_registers(regs);
292 #ifdef CONFIG_X86_32
293 if (user_mode_vm(regs)) {
294 sp = regs->sp;
295 ss = regs->ss & 0xffff;
296 } else {
297 sp = kernel_stack_pointer(regs);
298 savesegment(ss, ss);
300 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
301 print_symbol("%s", regs->ip);
302 printk(" SS:ESP %04x:%08lx\n", ss, sp);
303 #else
304 /* Executive summary in case the oops scrolled away */
305 printk(KERN_ALERT "RIP ");
306 printk_address(regs->ip, 1);
307 printk(" RSP <%016lx>\n", regs->sp);
308 #endif
309 return 0;
313 * This is gone through when something in the kernel has done something bad
314 * and is about to be terminated:
316 void die(const char *str, struct pt_regs *regs, long err)
318 unsigned long flags = oops_begin();
319 int sig = SIGSEGV;
321 if (!user_mode_vm(regs))
322 report_bug(regs->ip, regs);
324 if (__die(str, regs, err))
325 sig = 0;
326 oops_end(flags, regs, sig);
329 void notrace __kprobes
330 die_nmi(char *str, struct pt_regs *regs, int do_panic)
332 unsigned long flags;
334 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
335 return;
338 * We are in trouble anyway, lets at least try
339 * to get a message out.
341 flags = oops_begin();
342 printk(KERN_EMERG "%s", str);
343 printk(" on CPU%d, ip %08lx, registers:\n",
344 smp_processor_id(), regs->ip);
345 show_registers(regs);
346 oops_end(flags, regs, 0);
347 if (do_panic || panic_on_oops)
348 panic("Non maskable interrupt");
349 nmi_exit();
350 local_irq_enable();
351 do_exit(SIGBUS);
354 static int __init oops_setup(char *s)
356 if (!s)
357 return -EINVAL;
358 if (!strcmp(s, "panic"))
359 panic_on_oops = 1;
360 return 0;
362 early_param("oops", oops_setup);
364 static int __init kstack_setup(char *s)
366 if (!s)
367 return -EINVAL;
368 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
369 return 0;
371 early_param("kstack", kstack_setup);
373 static int __init code_bytes_setup(char *s)
375 code_bytes = simple_strtoul(s, NULL, 0);
376 if (code_bytes > 8192)
377 code_bytes = 8192;
379 return 1;
381 __setup("code_bytes=", code_bytes_setup);