- Update to 3.3-rc3.
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / dumpstack_64.c
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/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>
16
17 #include <linux/unwind.h>
18 #include <asm/stacktrace.h>
19
20
21 #define N_EXCEPTION_STACKS_END \
22                 (N_EXCEPTION_STACKS + DEBUG_STKSZ/EXCEPTION_STKSZ - 2)
23
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[?]"
33 #endif
34 };
35
36 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
37                                          unsigned *usedp, char **idp)
38 {
39         unsigned k;
40
41         /*
42          * Iterate over all exception stacks, and figure out whether
43          * 'stack' is in one of them:
44          */
45         for (k = 0; k < N_EXCEPTION_STACKS; k++) {
46                 unsigned long end = per_cpu(orig_ist, cpu).ist[k];
47                 /*
48                  * Is 'stack' above this exception frame's end?
49                  * If yes then skip to the next frame.
50                  */
51                 if (stack >= end)
52                         continue;
53                 /*
54                  * Is 'stack' above this exception frame's start address?
55                  * If yes then we found the right frame.
56                  */
57                 if (stack >= end - EXCEPTION_STKSZ) {
58                         /*
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:
63                          */
64                         if (*usedp & (1U << k))
65                                 break;
66                         *usedp |= 1U << k;
67                         *idp = x86_stack_ids[k];
68                         return (unsigned long *)end;
69                 }
70                 /*
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:
74                  */
75 #if DEBUG_STKSZ > EXCEPTION_STKSZ
76                 if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
77                         unsigned j = N_EXCEPTION_STACKS - 1;
78
79                         /*
80                          * Black magic. A large debug stack is composed of
81                          * multiple exception stack entries, which we
82                          * iterate through now. Dont look:
83                          */
84                         do {
85                                 ++j;
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))
91                                 break;
92                         *usedp |= 1U << j;
93                         *idp = x86_stack_ids[j];
94                         return (unsigned long *)end;
95                 }
96 #endif
97         }
98         return NULL;
99 }
100
101 static inline int
102 in_irq_stack(unsigned long *stack, unsigned long *irq_stack,
103              unsigned long *irq_stack_end)
104 {
105         return (stack >= irq_stack && stack < irq_stack_end);
106 }
107
108 /*
109  * x86-64 can have up to three kernel stacks:
110  * process stack
111  * interrupt stack
112  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
113  */
114
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)
118 {
119         const unsigned cpu = get_cpu();
120         unsigned long *irq_stack_end =
121                 (unsigned long *)per_cpu(irq_stack_ptr, cpu);
122         unsigned used = 0;
123         struct thread_info *tinfo;
124         int graph = 0;
125         unsigned long dummy;
126
127         if (!task)
128                 task = current;
129
130         bp = stack_frame(task, regs);
131         if (try_stack_unwind(task, regs, &stack, &bp, ops, data)) {
132                 put_cpu();
133                 return;
134         }
135
136         if (!stack) {
137                 if (regs)
138                         stack = (unsigned long *)regs->sp;
139                 else if (task != current)
140                         stack = (unsigned long *)task->thread.sp;
141                 else
142                         stack = &dummy;
143         }
144
145         if (!bp)
146                 bp = stack_frame(task, regs);
147         /*
148          * Print function call entries in all stacks, starting at the
149          * current stack address. If the stacks consist of nested
150          * exceptions
151          */
152         tinfo = task_thread_info(task);
153         for (;;) {
154                 char *id;
155                 unsigned long *estack_end;
156                 estack_end = in_exception_stack(cpu, (unsigned long)stack,
157                                                 &used, &id);
158
159                 if (estack_end) {
160                         if (ops->stack(data, id) < 0)
161                                 break;
162
163                         bp = ops->walk_stack(tinfo, stack, bp, ops,
164                                              data, estack_end, &graph);
165                         ops->stack(data, "<EOE>");
166                         /*
167                          * We link to the next stack via the
168                          * second-to-last pointer (index -2 to end) in the
169                          * exception stack:
170                          */
171                         stack = (unsigned long *) estack_end[-2];
172                         continue;
173                 }
174                 if (irq_stack_end) {
175                         unsigned long *irq_stack;
176                         irq_stack = irq_stack_end -
177                                 (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack);
178
179                         if (in_irq_stack(stack, irq_stack, irq_stack_end)) {
180                                 if (ops->stack(data, "IRQ") < 0)
181                                         break;
182                                 bp = ops->walk_stack(tinfo, stack, bp,
183                                         ops, data, irq_stack_end, &graph);
184                                 /*
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:
188                                  */
189                                 stack = (unsigned long *) (irq_stack_end[-1]);
190                                 irq_stack_end = NULL;
191                                 ops->stack(data, "EOI");
192                                 continue;
193                         }
194                 }
195                 break;
196         }
197
198         /*
199          * This handles the process stack:
200          */
201         bp = ops->walk_stack(tinfo, stack, bp, ops, data, NULL, &graph);
202         put_cpu();
203 }
204 EXPORT_SYMBOL(dump_trace);
205
206 void
207 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
208                    unsigned long *sp, unsigned long bp, char *log_lvl)
209 {
210         unsigned long *irq_stack_end;
211         unsigned long *irq_stack;
212         unsigned long *stack;
213         int cpu;
214         int i;
215
216         preempt_disable();
217         cpu = smp_processor_id();
218
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);
221
222         /*
223          * Debugging aid: "show_stack(NULL, NULL);" prints the
224          * back trace for this cpu:
225          */
226         if (sp == NULL) {
227                 if (task)
228                         sp = (unsigned long *)task->thread.sp;
229                 else
230                         sp = (unsigned long *)&sp;
231         }
232
233         stack = 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> ");
239                         }
240                 } else {
241                 if (((long) stack & (THREAD_SIZE-1)) == 0)
242                         break;
243                 }
244                 if (i && ((i % STACKSLOTS_PER_LINE) == 0))
245                         printk(KERN_CONT "\n");
246                 printk(KERN_CONT " %016lx", *stack++);
247                 touch_nmi_watchdog();
248         }
249         preempt_enable();
250
251         printk(KERN_CONT "\n");
252         show_trace_log_lvl(task, regs, sp, bp, log_lvl);
253 }
254
255 void show_registers(struct pt_regs *regs)
256 {
257         int i;
258         unsigned long sp;
259         const int cpu = smp_processor_id();
260         struct task_struct *cur = current;
261
262         sp = regs->sp;
263         printk("CPU %d ", cpu);
264         print_modules();
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);
268
269         /*
270          * When in-kernel, we also print out the stack and code at the
271          * time of the fault..
272          */
273         if (!user_mode(regs)) {
274                 unsigned int code_prologue = code_bytes * 43 / 64;
275                 unsigned int code_len = code_bytes;
276                 unsigned char c;
277                 u8 *ip;
278
279                 printk(KERN_DEFAULT "Stack:\n");
280                 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
281                                    0, KERN_DEFAULT);
282
283                 printk(KERN_DEFAULT "Code: ");
284
285                 ip = (u8 *)regs->ip - code_prologue;
286                 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
287                         /* try starting at IP */
288                         ip = (u8 *)regs->ip;
289                         code_len = code_len - code_prologue + 1;
290                 }
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.");
295                                 break;
296                         }
297                         if (ip == (u8 *)regs->ip)
298                                 printk(KERN_CONT "<%02x> ", c);
299                         else
300                                 printk(KERN_CONT "%02x ", c);
301                 }
302         }
303         printk(KERN_CONT "\n");
304 }
305
306 int is_valid_bugaddr(unsigned long ip)
307 {
308         unsigned short ud2;
309
310         if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
311                 return 0;
312
313         return ud2 == 0x0b0f;
314 }