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 <asm/stacktrace.h>
19 #include "dumpstack.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 * We are returning from the irq stack and go to the previous one.
110 * If the previous stack is also in the irq stack, then bp in the first
111 * frame of the irq stack points to the previous, interrupted one.
112 * Otherwise we have another level of indirection: We first save
113 * the bp of the previous stack, then we switch the stack to the irq one
114 * and save a new bp that links to the previous one.
117 static inline unsigned long
118 fixup_bp_irq_link(unsigned long bp, unsigned long *stack,
119 unsigned long *irq_stack, unsigned long *irq_stack_end)
121 #ifdef CONFIG_FRAME_POINTER
122 struct stack_frame *frame = (struct stack_frame *)bp;
124 if (!in_irq_stack(stack, irq_stack, irq_stack_end))
125 return (unsigned long)frame->next_frame;
131 * x86-64 can have up to three kernel stacks:
134 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
137 void dump_trace(struct task_struct *task, struct pt_regs *regs,
138 unsigned long *stack, unsigned long bp,
139 const struct stacktrace_ops *ops, void *data)
141 const unsigned cpu = get_cpu();
142 unsigned long *irq_stack_end =
143 (unsigned long *)per_cpu(irq_stack_ptr, cpu);
145 struct thread_info *tinfo;
154 if (task && task != current)
155 stack = (unsigned long *)task->thread.sp;
158 #ifdef CONFIG_FRAME_POINTER
160 if (task == current) {
161 /* Grab bp right from our regs */
164 /* bp is the last reg pushed by switch_to */
165 bp = *(unsigned long *) task->thread.sp;
171 * Print function call entries in all stacks, starting at the
172 * current stack address. If the stacks consist of nested
175 tinfo = task_thread_info(task);
178 unsigned long *estack_end;
179 estack_end = in_exception_stack(cpu, (unsigned long)stack,
183 if (ops->stack(data, id) < 0)
186 bp = ops->walk_stack(tinfo, stack, bp, ops,
187 data, estack_end, &graph);
188 ops->stack(data, "<EOE>");
190 * We link to the next stack via the
191 * second-to-last pointer (index -2 to end) in the
194 stack = (unsigned long *) estack_end[-2];
198 unsigned long *irq_stack;
199 irq_stack = irq_stack_end -
200 (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
202 if (in_irq_stack(stack, irq_stack, irq_stack_end)) {
203 if (ops->stack(data, "IRQ") < 0)
205 bp = print_context_stack(tinfo, stack, bp,
206 ops, data, irq_stack_end, &graph);
208 * We link to the next stack (which would be
209 * the process stack normally) the last
210 * pointer (index -1 to end) in the IRQ stack:
212 stack = (unsigned long *) (irq_stack_end[-1]);
213 bp = fixup_bp_irq_link(bp, stack, irq_stack,
215 irq_stack_end = NULL;
216 ops->stack(data, "EOI");
224 * This handles the process stack:
226 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL, &graph);
229 EXPORT_SYMBOL(dump_trace);
232 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
233 unsigned long *sp, unsigned long bp, char *log_lvl)
235 unsigned long *irq_stack_end;
236 unsigned long *irq_stack;
237 unsigned long *stack;
242 cpu = smp_processor_id();
244 irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
245 irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
248 * Debugging aid: "show_stack(NULL, NULL);" prints the
249 * back trace for this cpu:
253 sp = (unsigned long *)task->thread.sp;
255 sp = (unsigned long *)&sp;
259 for (i = 0; i < kstack_depth_to_print; i++) {
260 if (stack >= irq_stack && stack <= irq_stack_end) {
261 if (stack == irq_stack_end) {
262 stack = (unsigned long *) (irq_stack_end[-1]);
266 if (((long) stack & (THREAD_SIZE-1)) == 0)
269 if (i && ((i % STACKSLOTS_PER_LINE) == 0))
270 printk("\n%s", log_lvl);
271 printk(" %016lx", *stack++);
272 touch_nmi_watchdog();
277 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
280 void show_registers(struct pt_regs *regs)
284 const int cpu = smp_processor_id();
285 struct task_struct *cur = current;
288 printk("CPU %d ", cpu);
290 __show_regs(regs, 1);
291 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
292 cur->comm, cur->pid, task_thread_info(cur), cur);
295 * When in-kernel, we also print out the stack and code at the
296 * time of the fault..
298 if (!user_mode(regs)) {
299 unsigned int code_prologue = code_bytes * 43 / 64;
300 unsigned int code_len = code_bytes;
304 printk(KERN_EMERG "Stack:\n");
305 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
306 regs->bp, KERN_EMERG);
308 printk(KERN_EMERG "Code: ");
310 ip = (u8 *)regs->ip - code_prologue;
311 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
312 /* try starting at IP */
314 code_len = code_len - code_prologue + 1;
316 for (i = 0; i < code_len; i++, ip++) {
317 if (ip < (u8 *)PAGE_OFFSET ||
318 probe_kernel_address(ip, c)) {
319 printk(" Bad RIP value.");
322 if (ip == (u8 *)regs->ip)
323 printk("<%02x> ", c);
331 int is_valid_bugaddr(unsigned long ip)
335 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
338 return ud2 == 0x0b0f;