Merge branches 'acpi-ec', 'acpi-video', 'acpi-button' and 'acpi-thermal'
[linux-2.6-block.git] / arch / x86 / kernel / dumpstack_64.c
CommitLineData
6fcbede3
AH
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>
6fcbede3
AH
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>
b8030906 13#include <linux/sysfs.h>
6fcbede3
AH
14#include <linux/bug.h>
15#include <linux/nmi.h>
16
17#include <asm/stacktrace.h>
18
6fcbede3 19
b8030906
IM
20#define N_EXCEPTION_STACKS_END \
21 (N_EXCEPTION_STACKS + DEBUG_STKSZ/EXCEPTION_STKSZ - 2)
0406ca6d
FW
22
23static char x86_stack_ids[][8] = {
b8030906
IM
24 [ DEBUG_STACK-1 ] = "#DB",
25 [ NMI_STACK-1 ] = "NMI",
26 [ DOUBLEFAULT_STACK-1 ] = "#DF",
b8030906 27 [ MCE_STACK-1 ] = "#MC",
6fcbede3 28#if DEBUG_STKSZ > EXCEPTION_STKSZ
b8030906
IM
29 [ N_EXCEPTION_STACKS ...
30 N_EXCEPTION_STACKS_END ] = "#DB[?]"
6fcbede3 31#endif
b8030906 32};
0406ca6d 33
0406ca6d 34static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
b8030906 35 unsigned *usedp, char **idp)
0406ca6d 36{
6fcbede3
AH
37 unsigned k;
38
39 /*
40 * Iterate over all exception stacks, and figure out whether
41 * 'stack' is in one of them:
42 */
43 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
44 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
45 /*
46 * Is 'stack' above this exception frame's end?
47 * If yes then skip to the next frame.
48 */
49 if (stack >= end)
50 continue;
51 /*
52 * Is 'stack' above this exception frame's start address?
53 * If yes then we found the right frame.
54 */
55 if (stack >= end - EXCEPTION_STKSZ) {
56 /*
57 * Make sure we only iterate through an exception
58 * stack once. If it comes up for the second time
59 * then there's something wrong going on - just
60 * break out and return NULL:
61 */
62 if (*usedp & (1U << k))
63 break;
64 *usedp |= 1U << k;
0406ca6d 65 *idp = x86_stack_ids[k];
6fcbede3
AH
66 return (unsigned long *)end;
67 }
68 /*
69 * If this is a debug stack, and if it has a larger size than
70 * the usual exception stacks, then 'stack' might still
71 * be within the lower portion of the debug stack:
72 */
73#if DEBUG_STKSZ > EXCEPTION_STKSZ
74 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
75 unsigned j = N_EXCEPTION_STACKS - 1;
76
77 /*
78 * Black magic. A large debug stack is composed of
79 * multiple exception stack entries, which we
80 * iterate through now. Dont look:
81 */
82 do {
83 ++j;
84 end -= EXCEPTION_STKSZ;
0406ca6d
FW
85 x86_stack_ids[j][4] = '1' +
86 (j - N_EXCEPTION_STACKS);
6fcbede3
AH
87 } while (stack < end - EXCEPTION_STKSZ);
88 if (*usedp & (1U << j))
89 break;
90 *usedp |= 1U << j;
0406ca6d 91 *idp = x86_stack_ids[j];
6fcbede3
AH
92 return (unsigned long *)end;
93 }
94#endif
95 }
96 return NULL;
97}
98
af2d8289
FW
99static inline int
100in_irq_stack(unsigned long *stack, unsigned long *irq_stack,
101 unsigned long *irq_stack_end)
102{
103 return (stack >= irq_stack && stack < irq_stack_end);
104}
105
2223f6f6
SR
106static const unsigned long irq_stack_size =
107 (IRQ_STACK_SIZE - 64) / sizeof(unsigned long);
108
109enum stack_type {
110 STACK_IS_UNKNOWN,
111 STACK_IS_NORMAL,
112 STACK_IS_EXCEPTION,
113 STACK_IS_IRQ,
114};
115
116static enum stack_type
1aabc599 117analyze_stack(int cpu, struct task_struct *task, unsigned long *stack,
63c95654
SRRH
118 unsigned long **stack_end, unsigned long *irq_stack,
119 unsigned *used, char **id)
2223f6f6 120{
2223f6f6 121 unsigned long addr;
2223f6f6
SR
122
123 addr = ((unsigned long)stack & (~(THREAD_SIZE - 1)));
124 if ((unsigned long)task_stack_page(task) == addr)
125 return STACK_IS_NORMAL;
126
127 *stack_end = in_exception_stack(cpu, (unsigned long)stack,
1aabc599 128 used, id);
2223f6f6
SR
129 if (*stack_end)
130 return STACK_IS_EXCEPTION;
131
63c95654
SRRH
132 if (!irq_stack)
133 return STACK_IS_NORMAL;
2223f6f6 134
63c95654
SRRH
135 *stack_end = irq_stack;
136 irq_stack = irq_stack - irq_stack_size;
2223f6f6
SR
137
138 if (in_irq_stack(stack, irq_stack, *stack_end))
139 return STACK_IS_IRQ;
140
141 return STACK_IS_UNKNOWN;
142}
143
6fcbede3
AH
144/*
145 * x86-64 can have up to three kernel stacks:
146 * process stack
147 * interrupt stack
148 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
149 */
150
e8e999cf
NK
151void dump_trace(struct task_struct *task, struct pt_regs *regs,
152 unsigned long *stack, unsigned long bp,
6fcbede3
AH
153 const struct stacktrace_ops *ops, void *data)
154{
155 const unsigned cpu = get_cpu();
63c95654 156 unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
2e5aa682 157 unsigned long dummy;
1aabc599 158 unsigned used = 0;
2223f6f6
SR
159 int graph = 0;
160 int done = 0;
6fcbede3
AH
161
162 if (!task)
163 task = current;
164
165 if (!stack) {
47ce11a2
FW
166 if (regs)
167 stack = (unsigned long *)regs->sp;
d0caf292 168 else if (task != current)
6fcbede3 169 stack = (unsigned long *)task->thread.sp;
47ce11a2
FW
170 else
171 stack = &dummy;
6fcbede3
AH
172 }
173
e8e999cf
NK
174 if (!bp)
175 bp = stack_frame(task, regs);
6fcbede3
AH
176 /*
177 * Print function call entries in all stacks, starting at the
178 * current stack address. If the stacks consist of nested
179 * exceptions
180 */
2223f6f6
SR
181 while (!done) {
182 unsigned long *stack_end;
183 enum stack_type stype;
6fcbede3 184 char *id;
6fcbede3 185
63c95654
SRRH
186 stype = analyze_stack(cpu, task, stack, &stack_end,
187 irq_stack, &used, &id);
2223f6f6
SR
188
189 /* Default finish unless specified to continue */
190 done = 1;
191
192 switch (stype) {
193
194 /* Break out early if we are on the thread stack */
195 case STACK_IS_NORMAL:
196 break;
197
198 case STACK_IS_EXCEPTION:
199
6fcbede3
AH
200 if (ops->stack(data, id) < 0)
201 break;
202
da01e18a 203 bp = ops->walk_stack(task, stack, bp, ops,
2223f6f6 204 data, stack_end, &graph);
6fcbede3
AH
205 ops->stack(data, "<EOE>");
206 /*
207 * We link to the next stack via the
208 * second-to-last pointer (index -2 to end) in the
209 * exception stack:
210 */
2223f6f6
SR
211 stack = (unsigned long *) stack_end[-2];
212 done = 0;
213 break;
214
215 case STACK_IS_IRQ:
216
217 if (ops->stack(data, "IRQ") < 0)
218 break;
da01e18a 219 bp = ops->walk_stack(task, stack, bp,
2223f6f6
SR
220 ops, data, stack_end, &graph);
221 /*
222 * We link to the next stack (which would be
223 * the process stack normally) the last
224 * pointer (index -1 to end) in the IRQ stack:
225 */
226 stack = (unsigned long *) (stack_end[-1]);
63c95654 227 irq_stack = NULL;
2223f6f6
SR
228 ops->stack(data, "EOI");
229 done = 0;
230 break;
231
232 case STACK_IS_UNKNOWN:
233 ops->stack(data, "UNK");
234 break;
6fcbede3 235 }
6fcbede3
AH
236 }
237
238 /*
239 * This handles the process stack:
240 */
da01e18a 241 bp = ops->walk_stack(task, stack, bp, ops, data, NULL, &graph);
6fcbede3
AH
242 put_cpu();
243}
244EXPORT_SYMBOL(dump_trace);
245
878719e8 246void
6fcbede3 247show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
e8e999cf 248 unsigned long *sp, unsigned long bp, char *log_lvl)
6fcbede3 249{
67f2de0b
IM
250 unsigned long *irq_stack_end;
251 unsigned long *irq_stack;
6fcbede3 252 unsigned long *stack;
67f2de0b 253 int cpu;
6fcbede3 254 int i;
67f2de0b
IM
255
256 preempt_disable();
257 cpu = smp_processor_id();
258
259 irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
260 irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE);
6fcbede3
AH
261
262 /*
67f2de0b
IM
263 * Debugging aid: "show_stack(NULL, NULL);" prints the
264 * back trace for this cpu:
6fcbede3 265 */
6fcbede3
AH
266 if (sp == NULL) {
267 if (task)
268 sp = (unsigned long *)task->thread.sp;
269 else
270 sp = (unsigned long *)&sp;
271 }
272
273 stack = sp;
274 for (i = 0; i < kstack_depth_to_print; i++) {
26f80bd6
BG
275 if (stack >= irq_stack && stack <= irq_stack_end) {
276 if (stack == irq_stack_end) {
277 stack = (unsigned long *) (irq_stack_end[-1]);
c767a54b 278 pr_cont(" <EOI> ");
6fcbede3
AH
279 }
280 } else {
04769ae3 281 if (kstack_end(stack))
6fcbede3
AH
282 break;
283 }
1fc7f61c
AS
284 if ((i % STACKSLOTS_PER_LINE) == 0) {
285 if (i != 0)
286 pr_cont("\n");
287 printk("%s %016lx", log_lvl, *stack++);
288 } else
289 pr_cont(" %016lx", *stack++);
6fcbede3
AH
290 touch_nmi_watchdog();
291 }
67f2de0b
IM
292 preempt_enable();
293
c767a54b 294 pr_cont("\n");
e8e999cf 295 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
6fcbede3
AH
296}
297
57da8b96 298void show_regs(struct pt_regs *regs)
6fcbede3
AH
299{
300 int i;
301 unsigned long sp;
6fcbede3
AH
302
303 sp = regs->sp;
a43cb95d 304 show_regs_print_info(KERN_DEFAULT);
6fcbede3 305 __show_regs(regs, 1);
6fcbede3
AH
306
307 /*
308 * When in-kernel, we also print out the stack and code at the
309 * time of the fault..
310 */
311 if (!user_mode(regs)) {
312 unsigned int code_prologue = code_bytes * 43 / 64;
313 unsigned int code_len = code_bytes;
314 unsigned char c;
315 u8 *ip;
316
b0f4c4b3 317 printk(KERN_DEFAULT "Stack:\n");
6fcbede3 318 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
b0f4c4b3 319 0, KERN_DEFAULT);
6fcbede3 320
b0f4c4b3 321 printk(KERN_DEFAULT "Code: ");
6fcbede3
AH
322
323 ip = (u8 *)regs->ip - code_prologue;
324 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
8a541665 325 /* try starting at IP */
6fcbede3
AH
326 ip = (u8 *)regs->ip;
327 code_len = code_len - code_prologue + 1;
328 }
329 for (i = 0; i < code_len; i++, ip++) {
330 if (ip < (u8 *)PAGE_OFFSET ||
331 probe_kernel_address(ip, c)) {
c767a54b 332 pr_cont(" Bad RIP value.");
6fcbede3
AH
333 break;
334 }
335 if (ip == (u8 *)regs->ip)
c767a54b 336 pr_cont("<%02x> ", c);
6fcbede3 337 else
c767a54b 338 pr_cont("%02x ", c);
6fcbede3
AH
339 }
340 }
c767a54b 341 pr_cont("\n");
6fcbede3
AH
342}
343
344int is_valid_bugaddr(unsigned long ip)
345{
346 unsigned short ud2;
347
348 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
349 return 0;
350
351 return ud2 == 0x0b0f;
352}