2e708afe146d5644f163d3bfe2511f4806961823
[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 static char *exception_stack_names[N_EXCEPTION_STACKS] = {
20                 [ DOUBLEFAULT_STACK-1   ]       = "#DF",
21                 [ NMI_STACK-1           ]       = "NMI",
22                 [ DEBUG_STACK-1         ]       = "#DB",
23                 [ MCE_STACK-1           ]       = "#MC",
24 };
25
26 static unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = {
27         [0 ... N_EXCEPTION_STACKS - 1]          = EXCEPTION_STKSZ,
28         [DEBUG_STACK - 1]                       = DEBUG_STKSZ
29 };
30
31 void stack_type_str(enum stack_type type, const char **begin, const char **end)
32 {
33         BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
34
35         switch (type) {
36         case STACK_TYPE_IRQ:
37                 *begin = "IRQ";
38                 *end   = "EOI";
39                 break;
40         case STACK_TYPE_EXCEPTION ... STACK_TYPE_EXCEPTION_LAST:
41                 *begin = exception_stack_names[type - STACK_TYPE_EXCEPTION];
42                 *end   = "EOE";
43                 break;
44         default:
45                 *begin = NULL;
46                 *end   = NULL;
47         }
48 }
49
50 static bool in_exception_stack(unsigned long *stack, struct stack_info *info,
51                                unsigned long *visit_mask)
52 {
53         unsigned long *begin, *end;
54         struct pt_regs *regs;
55         unsigned k;
56
57         BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
58
59         for (k = 0; k < N_EXCEPTION_STACKS; k++) {
60                 end   = (unsigned long *)raw_cpu_ptr(&orig_ist)->ist[k];
61                 begin = end - (exception_stack_sizes[k] / sizeof(long));
62                 regs  = (struct pt_regs *)end - 1;
63
64                 if (stack < begin || stack >= end)
65                         continue;
66
67                 /*
68                  * Make sure we don't iterate through an exception stack more
69                  * than once.  If it comes up a second time then there's
70                  * something wrong going on - just break out and report an
71                  * unknown stack type.
72                  */
73                 if (*visit_mask & (1U << k))
74                         break;
75                 *visit_mask |= 1U << k;
76
77                 info->type      = STACK_TYPE_EXCEPTION + k;
78                 info->begin     = begin;
79                 info->end       = end;
80                 info->next_sp   = (unsigned long *)regs->sp;
81
82                 return true;
83         }
84
85         return false;
86 }
87
88 static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
89 {
90         unsigned long *end   = (unsigned long *)this_cpu_read(irq_stack_ptr);
91         unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
92
93         /*
94          * This is a software stack, so 'end' can be a valid stack pointer.
95          * It just means the stack is empty.
96          */
97         if (stack < begin || stack > end)
98                 return false;
99
100         info->type      = STACK_TYPE_IRQ;
101         info->begin     = begin;
102         info->end       = end;
103
104         /*
105          * The next stack pointer is the first thing pushed by the entry code
106          * after switching to the irq stack.
107          */
108         info->next_sp = (unsigned long *)*(end - 1);
109
110         return true;
111 }
112
113 int get_stack_info(unsigned long *stack, struct task_struct *task,
114                    struct stack_info *info, unsigned long *visit_mask)
115 {
116         if (!stack)
117                 goto unknown;
118
119         task = task ? : current;
120
121         if (in_task_stack(stack, task, info))
122                 return 0;
123
124         if (task != current)
125                 goto unknown;
126
127         if (in_exception_stack(stack, info, visit_mask))
128                 return 0;
129
130         if (in_irq_stack(stack, info))
131                 return 0;
132
133         return 0;
134
135 unknown:
136         info->type = STACK_TYPE_UNKNOWN;
137         return -EINVAL;
138 }
139
140 /*
141  * x86-64 can have up to three kernel stacks:
142  * process stack
143  * interrupt stack
144  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
145  */
146
147 void dump_trace(struct task_struct *task, struct pt_regs *regs,
148                 unsigned long *stack, unsigned long bp,
149                 const struct stacktrace_ops *ops, void *data)
150 {
151         unsigned long visit_mask = 0;
152         struct stack_info info;
153         int graph = 0;
154         int done = 0;
155
156         task = task ? : current;
157         stack = stack ? : get_stack_pointer(task, regs);
158         bp = bp ? : (unsigned long)get_frame_pointer(task, regs);
159
160         /*
161          * Print function call entries in all stacks, starting at the
162          * current stack address. If the stacks consist of nested
163          * exceptions
164          */
165         while (!done) {
166                 const char *begin_str, *end_str;
167
168                 get_stack_info(stack, task, &info, &visit_mask);
169
170                 /* Default finish unless specified to continue */
171                 done = 1;
172
173                 switch (info.type) {
174
175                 /* Break out early if we are on the thread stack */
176                 case STACK_TYPE_TASK:
177                         break;
178
179                 case STACK_TYPE_IRQ:
180                 case STACK_TYPE_EXCEPTION ... STACK_TYPE_EXCEPTION_LAST:
181
182                         stack_type_str(info.type, &begin_str, &end_str);
183
184                         if (ops->stack(data, begin_str) < 0)
185                                 break;
186
187                         bp = ops->walk_stack(task, stack, bp, ops,
188                                              data, &info, &graph);
189
190                         ops->stack(data, end_str);
191
192                         stack = info.next_sp;
193                         done = 0;
194                         break;
195
196                 default:
197                         ops->stack(data, "UNK");
198                         break;
199                 }
200         }
201
202         /*
203          * This handles the process stack:
204          */
205         bp = ops->walk_stack(task, stack, bp, ops, data, &info, &graph);
206 }
207 EXPORT_SYMBOL(dump_trace);
208
209 void
210 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
211                    unsigned long *sp, unsigned long bp, char *log_lvl)
212 {
213         unsigned long *irq_stack_end;
214         unsigned long *irq_stack;
215         unsigned long *stack;
216         int i;
217
218         irq_stack_end = (unsigned long *)this_cpu_read(irq_stack_ptr);
219         irq_stack     = irq_stack_end - (IRQ_STACK_SIZE / sizeof(long));
220
221         sp = sp ? : get_stack_pointer(task, regs);
222
223         stack = sp;
224         for (i = 0; i < kstack_depth_to_print; i++) {
225                 unsigned long word;
226
227                 if (stack >= irq_stack && stack <= irq_stack_end) {
228                         if (stack == irq_stack_end) {
229                                 stack = (unsigned long *) (irq_stack_end[-1]);
230                                 pr_cont(" <EOI> ");
231                         }
232                 } else {
233                 if (kstack_end(stack))
234                         break;
235                 }
236
237                 if (probe_kernel_address(stack, word))
238                         break;
239
240                 if ((i % STACKSLOTS_PER_LINE) == 0) {
241                         if (i != 0)
242                                 pr_cont("\n");
243                         printk("%s %016lx", log_lvl, word);
244                 } else
245                         pr_cont(" %016lx", word);
246
247                 stack++;
248                 touch_nmi_watchdog();
249         }
250
251         pr_cont("\n");
252         show_trace_log_lvl(task, regs, sp, bp, log_lvl);
253 }
254
255 void show_regs(struct pt_regs *regs)
256 {
257         int i;
258
259         show_regs_print_info(KERN_DEFAULT);
260         __show_regs(regs, 1);
261
262         /*
263          * When in-kernel, we also print out the stack and code at the
264          * time of the fault..
265          */
266         if (!user_mode(regs)) {
267                 unsigned int code_prologue = code_bytes * 43 / 64;
268                 unsigned int code_len = code_bytes;
269                 unsigned char c;
270                 u8 *ip;
271
272                 printk(KERN_DEFAULT "Stack:\n");
273                 show_stack_log_lvl(NULL, regs, NULL, 0, KERN_DEFAULT);
274
275                 printk(KERN_DEFAULT "Code: ");
276
277                 ip = (u8 *)regs->ip - code_prologue;
278                 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
279                         /* try starting at IP */
280                         ip = (u8 *)regs->ip;
281                         code_len = code_len - code_prologue + 1;
282                 }
283                 for (i = 0; i < code_len; i++, ip++) {
284                         if (ip < (u8 *)PAGE_OFFSET ||
285                                         probe_kernel_address(ip, c)) {
286                                 pr_cont(" Bad RIP value.");
287                                 break;
288                         }
289                         if (ip == (u8 *)regs->ip)
290                                 pr_cont("<%02x> ", c);
291                         else
292                                 pr_cont("%02x ", c);
293                 }
294         }
295         pr_cont("\n");
296 }
297
298 int is_valid_bugaddr(unsigned long ip)
299 {
300         unsigned short ud2;
301
302         if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
303                 return 0;
304
305         return ud2 == 0x0b0f;
306 }