066eb5c77fd66e8eeeecfc22d20a65c1f82ddecd
[cascardo/linux.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/export.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 <asm/stacktrace.h>
18
19
20 #define N_EXCEPTION_STACKS_END \
21                 (N_EXCEPTION_STACKS + DEBUG_STKSZ/EXCEPTION_STKSZ - 2)
22
23 static char x86_stack_ids[][8] = {
24                 [ DEBUG_STACK-1                 ]       = "#DB",
25                 [ NMI_STACK-1                   ]       = "NMI",
26                 [ DOUBLEFAULT_STACK-1           ]       = "#DF",
27                 [ MCE_STACK-1                   ]       = "#MC",
28 #if DEBUG_STKSZ > EXCEPTION_STKSZ
29                 [ N_EXCEPTION_STACKS ...
30                   N_EXCEPTION_STACKS_END        ]       = "#DB[?]"
31 #endif
32 };
33
34 static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
35                                          unsigned *usedp, char **idp)
36 {
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;
65                         *idp = x86_stack_ids[k];
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;
85                                 x86_stack_ids[j][4] = '1' +
86                                                 (j - N_EXCEPTION_STACKS);
87                         } while (stack < end - EXCEPTION_STKSZ);
88                         if (*usedp & (1U << j))
89                                 break;
90                         *usedp |= 1U << j;
91                         *idp = x86_stack_ids[j];
92                         return (unsigned long *)end;
93                 }
94 #endif
95         }
96         return NULL;
97 }
98
99 static inline int
100 in_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
106 enum stack_type {
107         STACK_IS_UNKNOWN,
108         STACK_IS_NORMAL,
109         STACK_IS_EXCEPTION,
110         STACK_IS_IRQ,
111 };
112
113 static enum stack_type
114 analyze_stack(int cpu, struct task_struct *task, unsigned long *stack,
115               unsigned long **stack_end, unsigned long *irq_stack,
116               unsigned *used, char **id)
117 {
118         unsigned long addr;
119
120         addr = ((unsigned long)stack & (~(THREAD_SIZE - 1)));
121         if ((unsigned long)task_stack_page(task) == addr)
122                 return STACK_IS_NORMAL;
123
124         *stack_end = in_exception_stack(cpu, (unsigned long)stack,
125                                         used, id);
126         if (*stack_end)
127                 return STACK_IS_EXCEPTION;
128
129         if (!irq_stack)
130                 return STACK_IS_NORMAL;
131
132         *stack_end = irq_stack;
133         irq_stack -= (IRQ_STACK_SIZE / sizeof(long));
134
135         if (in_irq_stack(stack, irq_stack, *stack_end))
136                 return STACK_IS_IRQ;
137
138         return STACK_IS_UNKNOWN;
139 }
140
141 /*
142  * x86-64 can have up to three kernel stacks:
143  * process stack
144  * interrupt stack
145  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
146  */
147
148 void dump_trace(struct task_struct *task, struct pt_regs *regs,
149                 unsigned long *stack, unsigned long bp,
150                 const struct stacktrace_ops *ops, void *data)
151 {
152         const unsigned cpu = get_cpu();
153         unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu);
154         unsigned long dummy;
155         unsigned used = 0;
156         int graph = 0;
157         int done = 0;
158
159         if (!task)
160                 task = current;
161
162         if (!stack) {
163                 if (regs)
164                         stack = (unsigned long *)regs->sp;
165                 else if (task != current)
166                         stack = (unsigned long *)task->thread.sp;
167                 else
168                         stack = &dummy;
169         }
170
171         if (!bp)
172                 bp = stack_frame(task, regs);
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         while (!done) {
179                 unsigned long *stack_end;
180                 enum stack_type stype;
181                 char *id;
182
183                 stype = analyze_stack(cpu, task, stack, &stack_end,
184                                       irq_stack, &used, &id);
185
186                 /* Default finish unless specified to continue */
187                 done = 1;
188
189                 switch (stype) {
190
191                 /* Break out early if we are on the thread stack */
192                 case STACK_IS_NORMAL:
193                         break;
194
195                 case STACK_IS_EXCEPTION:
196
197                         if (ops->stack(data, id) < 0)
198                                 break;
199
200                         bp = ops->walk_stack(task, stack, bp, ops,
201                                              data, stack_end, &graph);
202                         ops->stack(data, "EOE");
203                         /*
204                          * We link to the next stack via the
205                          * second-to-last pointer (index -2 to end) in the
206                          * exception stack:
207                          */
208                         stack = (unsigned long *) stack_end[-2];
209                         done = 0;
210                         break;
211
212                 case STACK_IS_IRQ:
213
214                         if (ops->stack(data, "IRQ") < 0)
215                                 break;
216                         bp = ops->walk_stack(task, stack, bp,
217                                      ops, data, stack_end, &graph);
218                         /*
219                          * We link to the next stack (which would be
220                          * the process stack normally) the last
221                          * pointer (index -1 to end) in the IRQ stack:
222                          */
223                         stack = (unsigned long *) (stack_end[-1]);
224                         irq_stack = NULL;
225                         ops->stack(data, "EOI");
226                         done = 0;
227                         break;
228
229                 case STACK_IS_UNKNOWN:
230                         ops->stack(data, "UNK");
231                         break;
232                 }
233         }
234
235         /*
236          * This handles the process stack:
237          */
238         bp = ops->walk_stack(task, stack, bp, ops, data, NULL, &graph);
239         put_cpu();
240 }
241 EXPORT_SYMBOL(dump_trace);
242
243 void
244 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
245                    unsigned long *sp, unsigned long bp, char *log_lvl)
246 {
247         unsigned long *irq_stack_end;
248         unsigned long *irq_stack;
249         unsigned long *stack;
250         int cpu;
251         int i;
252
253         preempt_disable();
254         cpu = smp_processor_id();
255
256         irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu));
257         irq_stack     = irq_stack_end - (IRQ_STACK_SIZE / sizeof(long));
258
259         /*
260          * Debugging aid: "show_stack(NULL, NULL);" prints the
261          * back trace for this cpu:
262          */
263         if (sp == NULL) {
264                 if (regs)
265                         sp = (unsigned long *)regs->sp;
266                 else if (task)
267                         sp = (unsigned long *)task->thread.sp;
268                 else
269                         sp = (unsigned long *)&sp;
270         }
271
272         stack = sp;
273         for (i = 0; i < kstack_depth_to_print; i++) {
274                 unsigned long word;
275
276                 if (stack >= irq_stack && stack <= irq_stack_end) {
277                         if (stack == irq_stack_end) {
278                                 stack = (unsigned long *) (irq_stack_end[-1]);
279                                 pr_cont(" <EOI> ");
280                         }
281                 } else {
282                 if (kstack_end(stack))
283                         break;
284                 }
285
286                 if (probe_kernel_address(stack, word))
287                         break;
288
289                 if ((i % STACKSLOTS_PER_LINE) == 0) {
290                         if (i != 0)
291                                 pr_cont("\n");
292                         printk("%s %016lx", log_lvl, word);
293                 } else
294                         pr_cont(" %016lx", word);
295
296                 stack++;
297                 touch_nmi_watchdog();
298         }
299         preempt_enable();
300
301         pr_cont("\n");
302         show_trace_log_lvl(task, regs, sp, bp, log_lvl);
303 }
304
305 void show_regs(struct pt_regs *regs)
306 {
307         int i;
308         unsigned long sp;
309
310         sp = regs->sp;
311         show_regs_print_info(KERN_DEFAULT);
312         __show_regs(regs, 1);
313
314         /*
315          * When in-kernel, we also print out the stack and code at the
316          * time of the fault..
317          */
318         if (!user_mode(regs)) {
319                 unsigned int code_prologue = code_bytes * 43 / 64;
320                 unsigned int code_len = code_bytes;
321                 unsigned char c;
322                 u8 *ip;
323
324                 printk(KERN_DEFAULT "Stack:\n");
325                 show_stack_log_lvl(NULL, regs, (unsigned long *)sp,
326                                    0, KERN_DEFAULT);
327
328                 printk(KERN_DEFAULT "Code: ");
329
330                 ip = (u8 *)regs->ip - code_prologue;
331                 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
332                         /* try starting at IP */
333                         ip = (u8 *)regs->ip;
334                         code_len = code_len - code_prologue + 1;
335                 }
336                 for (i = 0; i < code_len; i++, ip++) {
337                         if (ip < (u8 *)PAGE_OFFSET ||
338                                         probe_kernel_address(ip, c)) {
339                                 pr_cont(" Bad RIP value.");
340                                 break;
341                         }
342                         if (ip == (u8 *)regs->ip)
343                                 pr_cont("<%02x> ", c);
344                         else
345                                 pr_cont("%02x ", c);
346                 }
347         }
348         pr_cont("\n");
349 }
350
351 int is_valid_bugaddr(unsigned long ip)
352 {
353         unsigned short ud2;
354
355         if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
356                 return 0;
357
358         return ud2 == 0x0b0f;
359 }