blob: 004b8aa6a35f01e8ac3696cf39794328b7669beb [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>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +02008#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/bug.h>
14#include <linux/nmi.h>
Andrew Mortonae872212007-08-24 16:11:54 -070015#include <linux/sysfs.h>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020016
17#include <asm/stacktrace.h>
18
Neil Horman878719e2008-10-23 10:40:06 -040019#include "dumpstack.h"
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020020
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020021
22static char x86_stack_ids[][8] = {
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020023 [DEBUG_STACK - 1] = "#DB",
24 [NMI_STACK - 1] = "NMI",
25 [DOUBLEFAULT_STACK - 1] = "#DF",
26 [STACKFAULT_STACK - 1] = "#SS",
27 [MCE_STACK - 1] = "#MC",
28#if DEBUG_STKSZ > EXCEPTION_STKSZ
29 [N_EXCEPTION_STACKS ...
30 N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
31#endif
32 };
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020033
34int x86_is_stack_id(int id, char *name)
35{
36 return x86_stack_ids[id - 1] == name;
37}
38
39static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
40 unsigned *usedp, char **idp)
41{
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020042 unsigned k;
43
44 /*
45 * Iterate over all exception stacks, and figure out whether
46 * 'stack' is in one of them:
47 */
48 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
49 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
50 /*
51 * Is 'stack' above this exception frame's end?
52 * If yes then skip to the next frame.
53 */
54 if (stack >= end)
55 continue;
56 /*
57 * Is 'stack' above this exception frame's start address?
58 * If yes then we found the right frame.
59 */
60 if (stack >= end - EXCEPTION_STKSZ) {
61 /*
62 * Make sure we only iterate through an exception
63 * stack once. If it comes up for the second time
64 * then there's something wrong going on - just
65 * break out and return NULL:
66 */
67 if (*usedp & (1U << k))
68 break;
69 *usedp |= 1U << k;
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020070 *idp = x86_stack_ids[k];
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020071 return (unsigned long *)end;
72 }
73 /*
74 * If this is a debug stack, and if it has a larger size than
75 * the usual exception stacks, then 'stack' might still
76 * be within the lower portion of the debug stack:
77 */
78#if DEBUG_STKSZ > EXCEPTION_STKSZ
79 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
80 unsigned j = N_EXCEPTION_STACKS - 1;
81
82 /*
83 * Black magic. A large debug stack is composed of
84 * multiple exception stack entries, which we
85 * iterate through now. Dont look:
86 */
87 do {
88 ++j;
89 end -= EXCEPTION_STKSZ;
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020090 x86_stack_ids[j][4] = '1' +
91 (j - N_EXCEPTION_STACKS);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020092 } while (stack < end - EXCEPTION_STKSZ);
93 if (*usedp & (1U << j))
94 break;
95 *usedp |= 1U << j;
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020096 *idp = x86_stack_ids[j];
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020097 return (unsigned long *)end;
98 }
99#endif
100 }
101 return NULL;
102}
103
Frederic Weisbeckeraf2d8282009-12-06 05:34:27 +0100104static inline int
105in_irq_stack(unsigned long *stack, unsigned long *irq_stack,
106 unsigned long *irq_stack_end)
107{
108 return (stack >= irq_stack && stack < irq_stack_end);
109}
110
111/*
112 * We are returning from the irq stack and go to the previous one.
113 * If the previous stack is also in the irq stack, then bp in the first
114 * frame of the irq stack points to the previous, interrupted one.
115 * Otherwise we have another level of indirection: We first save
116 * the bp of the previous stack, then we switch the stack to the irq one
117 * and save a new bp that links to the previous one.
118 * (See save_args())
119 */
120static inline unsigned long
121fixup_bp_irq_link(unsigned long bp, unsigned long *stack,
122 unsigned long *irq_stack, unsigned long *irq_stack_end)
123{
124#ifdef CONFIG_FRAME_POINTER
125 struct stack_frame *frame = (struct stack_frame *)bp;
126
127 if (!in_irq_stack(stack, irq_stack, irq_stack_end))
128 return (unsigned long)frame->next_frame;
129#endif
130 return bp;
131}
132
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200133/*
134 * x86-64 can have up to three kernel stacks:
135 * process stack
136 * interrupt stack
137 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
138 */
139
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200140void dump_trace(struct task_struct *task, struct pt_regs *regs,
141 unsigned long *stack, unsigned long bp,
142 const struct stacktrace_ops *ops, void *data)
143{
144 const unsigned cpu = get_cpu();
Brian Gerst26f80bd2009-01-19 00:38:58 +0900145 unsigned long *irq_stack_end =
146 (unsigned long *)per_cpu(irq_stack_ptr, cpu);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200147 unsigned used = 0;
148 struct thread_info *tinfo;
Steven Rostedt7ee991f2008-12-02 23:50:04 -0500149 int graph = 0;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200150
151 if (!task)
152 task = current;
153
154 if (!stack) {
155 unsigned long dummy;
156 stack = &dummy;
157 if (task && task != current)
158 stack = (unsigned long *)task->thread.sp;
159 }
160
161#ifdef CONFIG_FRAME_POINTER
162 if (!bp) {
163 if (task == current) {
164 /* Grab bp right from our regs */
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200165 get_bp(bp);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200166 } else {
167 /* bp is the last reg pushed by switch_to */
168 bp = *(unsigned long *) task->thread.sp;
169 }
170 }
171#endif
172
173 /*
174 * Print function call entries in all stacks, starting at the
175 * current stack address. If the stacks consist of nested
176 * exceptions
177 */
178 tinfo = task_thread_info(task);
179 for (;;) {
180 char *id;
181 unsigned long *estack_end;
182 estack_end = in_exception_stack(cpu, (unsigned long)stack,
183 &used, &id);
184
185 if (estack_end) {
186 if (ops->stack(data, id) < 0)
187 break;
188
189 bp = print_context_stack(tinfo, stack, bp, ops,
Steven Rostedt7ee991f2008-12-02 23:50:04 -0500190 data, estack_end, &graph);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200191 ops->stack(data, "<EOE>");
192 /*
193 * We link to the next stack via the
194 * second-to-last pointer (index -2 to end) in the
195 * exception stack:
196 */
197 stack = (unsigned long *) estack_end[-2];
198 continue;
199 }
Brian Gerst26f80bd2009-01-19 00:38:58 +0900200 if (irq_stack_end) {
201 unsigned long *irq_stack;
202 irq_stack = irq_stack_end -
203 (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200204
Frederic Weisbeckeraf2d8282009-12-06 05:34:27 +0100205 if (in_irq_stack(stack, irq_stack, irq_stack_end)) {
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200206 if (ops->stack(data, "IRQ") < 0)
207 break;
208 bp = print_context_stack(tinfo, stack, bp,
Brian Gerst26f80bd2009-01-19 00:38:58 +0900209 ops, data, irq_stack_end, &graph);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200210 /*
211 * We link to the next stack (which would be
212 * the process stack normally) the last
213 * pointer (index -1 to end) in the IRQ stack:
214 */
Brian Gerst26f80bd2009-01-19 00:38:58 +0900215 stack = (unsigned long *) (irq_stack_end[-1]);
Frederic Weisbeckeraf2d8282009-12-06 05:34:27 +0100216 bp = fixup_bp_irq_link(bp, stack, irq_stack,
217 irq_stack_end);
Brian Gerst26f80bd2009-01-19 00:38:58 +0900218 irq_stack_end = NULL;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200219 ops->stack(data, "EOI");
220 continue;
221 }
222 }
223 break;
224 }
225
226 /*
227 * This handles the process stack:
228 */
Steven Rostedt7ee991f2008-12-02 23:50:04 -0500229 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL, &graph);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200230 put_cpu();
231}
232EXPORT_SYMBOL(dump_trace);
233
Neil Horman878719e2008-10-23 10:40:06 -0400234void
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200235show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
236 unsigned long *sp, unsigned long bp, char *log_lvl)
237{
238 unsigned long *stack;
239 int i;
240 const int cpu = smp_processor_id();
Brian Gerst26f80bd2009-01-19 00:38:58 +0900241 unsigned long *irq_stack_end =
242 (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
243 unsigned long *irq_stack =
244 (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200245
246 /*
247 * debugging aid: "show_stack(NULL, NULL);" prints the
248 * back trace for this cpu.
249 */
250
251 if (sp == NULL) {
252 if (task)
253 sp = (unsigned long *)task->thread.sp;
254 else
255 sp = (unsigned long *)&sp;
256 }
257
258 stack = sp;
259 for (i = 0; i < kstack_depth_to_print; i++) {
Brian Gerst26f80bd2009-01-19 00:38:58 +0900260 if (stack >= irq_stack && stack <= irq_stack_end) {
261 if (stack == irq_stack_end) {
262 stack = (unsigned long *) (irq_stack_end[-1]);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200263 printk(" <EOI> ");
264 }
265 } else {
266 if (((long) stack & (THREAD_SIZE-1)) == 0)
267 break;
268 }
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200269 if (i && ((i % STACKSLOTS_PER_LINE) == 0))
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200270 printk("\n%s", log_lvl);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200271 printk(" %016lx", *stack++);
272 touch_nmi_watchdog();
273 }
274 printk("\n");
275 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
276}
277
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200278void show_registers(struct pt_regs *regs)
279{
280 int i;
281 unsigned long sp;
282 const int cpu = smp_processor_id();
Brian Gerstc6f5e0a2009-01-19 00:38:58 +0900283 struct task_struct *cur = current;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200284
285 sp = regs->sp;
286 printk("CPU %d ", cpu);
287 __show_regs(regs, 1);
288 printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
289 cur->comm, cur->pid, task_thread_info(cur), cur);
290
291 /*
292 * When in-kernel, we also print out the stack and code at the
293 * time of the fault..
294 */
295 if (!user_mode(regs)) {
296 unsigned int code_prologue = code_bytes * 43 / 64;
297 unsigned int code_len = code_bytes;
298 unsigned char c;
299 u8 *ip;
300
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200301 printk(KERN_EMERG "Stack:\n");
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200302 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200303 regs->bp, KERN_EMERG);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200304
305 printk(KERN_EMERG "Code: ");
306
307 ip = (u8 *)regs->ip - code_prologue;
308 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200309 /* try starting at IP */
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200310 ip = (u8 *)regs->ip;
311 code_len = code_len - code_prologue + 1;
312 }
313 for (i = 0; i < code_len; i++, ip++) {
314 if (ip < (u8 *)PAGE_OFFSET ||
315 probe_kernel_address(ip, c)) {
316 printk(" Bad RIP value.");
317 break;
318 }
319 if (ip == (u8 *)regs->ip)
320 printk("<%02x> ", c);
321 else
322 printk("%02x ", c);
323 }
324 }
325 printk("\n");
326}
327
328int is_valid_bugaddr(unsigned long ip)
329{
330 unsigned short ud2;
331
332 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
333 return 0;
334
335 return ud2 == 0x0b0f;
336}
337