2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5 #include <linux/kallsyms.h>
6 #include <linux/kprobes.h>
7 #include <linux/uaccess.h>
8 #include <linux/hardirq.h>
9 #include <linux/kdebug.h>
10 #include <linux/module.h>
11 #include <linux/ptrace.h>
12 #include <linux/kexec.h>
13 #include <linux/sysfs.h>
14 #include <linux/bug.h>
15 #include <linux/nmi.h>
17 #include <linux/unwind.h>
18 #include <asm/stacktrace.h>
21 #define N_EXCEPTION_STACKS_END \
22 (N_EXCEPTION_STACKS + DEBUG_STKSZ/EXCEPTION_STKSZ - 2)
24 static char x86_stack_ids[][8] = {
25 [ DEBUG_STACK-1 ] = "#DB",
26 [ NMI_STACK-1 ] = "NMI",
27 [ DOUBLEFAULT_STACK-1 ] = "#DF",
28 [ STACKFAULT_STACK-1 ] = "#SS",
29 [ MCE_STACK-1 ] = "#MC",
30 #if DEBUG_STKSZ > EXCEPTION_STKSZ
31 [ N_EXCEPTION_STACKS ...
32 N_EXCEPTION_STACKS_END ] = "#DB[?]"
36 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
37 unsigned *usedp, char **idp)
42 * Iterate over all exception stacks, and figure out whether
43 * 'stack' is in one of them:
45 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
46 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
48 * Is 'stack' above this exception frame's end?
49 * If yes then skip to the next frame.
54 * Is 'stack' above this exception frame's start address?
55 * If yes then we found the right frame.
57 if (stack >= end - EXCEPTION_STKSZ) {
59 * Make sure we only iterate through an exception
60 * stack once. If it comes up for the second time
61 * then there's something wrong going on - just
62 * break out and return NULL:
64 if (*usedp & (1U << k))
67 *idp = x86_stack_ids[k];
68 return (unsigned long *)end;
71 * If this is a debug stack, and if it has a larger size than
72 * the usual exception stacks, then 'stack' might still
73 * be within the lower portion of the debug stack:
75 #if DEBUG_STKSZ > EXCEPTION_STKSZ
76 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
77 unsigned j = N_EXCEPTION_STACKS - 1;
80 * Black magic. A large debug stack is composed of
81 * multiple exception stack entries, which we
82 * iterate through now. Dont look:
86 end -= EXCEPTION_STKSZ;
87 x86_stack_ids[j][4] = '1' +
88 (j - N_EXCEPTION_STACKS);
89 } while (stack < end - EXCEPTION_STKSZ);
90 if (*usedp & (1U << j))
93 *idp = x86_stack_ids[j];
94 return (unsigned long *)end;
102 in_irq_stack(unsigned long *stack, unsigned long *irq_stack,
103 unsigned long *irq_stack_end)
105 return (stack >= irq_stack && stack < irq_stack_end);
109 * x86-64 can have up to three kernel stacks:
112 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
115 void dump_trace(struct task_struct *task, struct pt_regs *regs,
116 unsigned long *stack, unsigned long bp,
117 const struct stacktrace_ops *ops, void *data)
119 const unsigned cpu = get_cpu();
120 unsigned long *irq_stack_end =
121 (unsigned long *)per_cpu(irq_stack_ptr, cpu);
123 struct thread_info *tinfo;
130 bp = stack_frame(task, regs);
131 if (try_stack_unwind(task, regs, &stack, &bp, ops, data)) {
138 stack = (unsigned long *)regs->sp;
139 else if (task != current)
140 stack = (unsigned long *)task->thread.sp;
146 bp = stack_frame(task, regs);
148 * Print function call entries in all stacks, starting at the
149 * current stack address. If the stacks consist of nested
152 tinfo = task_thread_info(task);
155 unsigned long *estack_end;
156 estack_end = in_exception_stack(cpu, (unsigned long)stack,
160 if (ops->stack(data, id) < 0)
163 bp = ops->walk_stack(tinfo, stack, bp, ops,
164 data, estack_end, &graph);
165 ops->stack(data, "<EOE>");
167 * We link to the next stack via the
168 * second-to-last pointer (index -2 to end) in the
171 stack = (unsigned long *) estack_end[-2];
175 unsigned long *irq_stack;
176 irq_stack = irq_stack_end -
177 (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
179 if (in_irq_stack(stack, irq_stack, irq_stack_end)) {
180 if (ops->stack(data, "IRQ") < 0)
182 bp = ops->walk_stack(tinfo, stack, bp,
183 ops, data, irq_stack_end, &graph);
185 * We link to the next stack (which would be
186 * the process stack normally) the last
187 * pointer (index -1 to end) in the IRQ stack:
189 stack = (unsigned long *) (irq_stack_end[-1]);
190 irq_stack_end = NULL;
191 ops->stack(data, "EOI");
199 * This handles the process stack:
201 bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
204 EXPORT_SYMBOL(dump_trace);
207 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
208 unsigned long *sp, unsigned long bp, char *log_lvl)
210 unsigned long *irq_stack_end;
211 unsigned long *irq_stack;
212 unsigned long *stack;
217 cpu = smp_processor_id();
219 irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
220 irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
223 * Debugging aid: "show_stack(NULL, NULL);" prints the
224 * back trace for this cpu:
228 sp = (unsigned long *)task->thread.sp;
230 sp = (unsigned long *)&sp;
234 for (i = 0; i < kstack_depth_to_print; i++) {
235 if (stack >= irq_stack && stack <= irq_stack_end) {
236 if (stack == irq_stack_end) {
237 stack = (unsigned long *) (irq_stack_end[-1]);
238 printk(KERN_CONT " <EOI> ");
241 if (((long) stack & (THREAD_SIZE-1)) == 0)
244 if (i && ((i % STACKSLOTS_PER_LINE) == 0))
245 printk(KERN_CONT "\n");
246 printk(KERN_CONT " %016lx", *stack++);
247 touch_nmi_watchdog();
251 printk(KERN_CONT "\n");
252 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
255 void show_registers(struct pt_regs *regs)
259 const int cpu = smp_processor_id();
260 struct task_struct *cur = current;
263 printk("CPU %d ", cpu);
265 __show_regs(regs, 1);
266 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
267 cur->comm, cur->pid, task_thread_info(cur), cur);
270 * When in-kernel, we also print out the stack and code at the
271 * time of the fault..
273 if (!user_mode(regs)) {
274 unsigned int code_prologue = code_bytes * 43 / 64;
275 unsigned int code_len = code_bytes;
279 printk(KERN_DEFAULT "Stack:\n");
280 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
283 printk(KERN_DEFAULT "Code: ");
285 ip = (u8 *)regs->ip - code_prologue;
286 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
287 /* try starting at IP */
289 code_len = code_len - code_prologue + 1;
291 for (i = 0; i < code_len; i++, ip++) {
292 if (ip < (u8 *)PAGE_OFFSET ||
293 probe_kernel_address(ip, c)) {
294 printk(KERN_CONT " Bad RIP value.");
297 if (ip == (u8 *)regs->ip)
298 printk(KERN_CONT "<%02x> ", c);
300 printk(KERN_CONT "%02x ", c);
303 printk(KERN_CONT "\n");
306 int is_valid_bugaddr(unsigned long ip)
310 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
313 return ud2 == 0x0b0f;