Commit | Line | Data |
---|---|---|
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> | |
186f4360 | 10 | #include <linux/export.h> |
6fcbede3 AH |
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 | |
23 | static 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 | 34 | static 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 |
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 | ||
2223f6f6 SR |
106 | static const unsigned long irq_stack_size = |
107 | (IRQ_STACK_SIZE - 64) / sizeof(unsigned long); | |
108 | ||
109 | enum stack_type { | |
110 | STACK_IS_UNKNOWN, | |
111 | STACK_IS_NORMAL, | |
112 | STACK_IS_EXCEPTION, | |
113 | STACK_IS_IRQ, | |
114 | }; | |
115 | ||
116 | static enum stack_type | |
1aabc599 | 117 | analyze_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 |
151 | void 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 | } | |
244 | EXPORT_SYMBOL(dump_trace); | |
245 | ||
878719e8 | 246 | void |
6fcbede3 | 247 | show_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 | 266 | if (sp == NULL) { |
ef16dd0c AL |
267 | if (regs) |
268 | sp = (unsigned long *)regs->sp; | |
269 | else if (task) | |
6fcbede3 AH |
270 | sp = (unsigned long *)task->thread.sp; |
271 | else | |
272 | sp = (unsigned long *)&sp; | |
273 | } | |
274 | ||
275 | stack = sp; | |
276 | for (i = 0; i < kstack_depth_to_print; i++) { | |
98f30b12 AL |
277 | unsigned long word; |
278 | ||
26f80bd6 BG |
279 | if (stack >= irq_stack && stack <= irq_stack_end) { |
280 | if (stack == irq_stack_end) { | |
281 | stack = (unsigned long *) (irq_stack_end[-1]); | |
c767a54b | 282 | pr_cont(" <EOI> "); |
6fcbede3 AH |
283 | } |
284 | } else { | |
04769ae3 | 285 | if (kstack_end(stack)) |
6fcbede3 AH |
286 | break; |
287 | } | |
98f30b12 AL |
288 | |
289 | if (probe_kernel_address(stack, word)) | |
290 | break; | |
291 | ||
1fc7f61c AS |
292 | if ((i % STACKSLOTS_PER_LINE) == 0) { |
293 | if (i != 0) | |
294 | pr_cont("\n"); | |
98f30b12 | 295 | printk("%s %016lx", log_lvl, word); |
1fc7f61c | 296 | } else |
98f30b12 AL |
297 | pr_cont(" %016lx", word); |
298 | ||
299 | stack++; | |
6fcbede3 AH |
300 | touch_nmi_watchdog(); |
301 | } | |
67f2de0b IM |
302 | preempt_enable(); |
303 | ||
c767a54b | 304 | pr_cont("\n"); |
e8e999cf | 305 | show_trace_log_lvl(task, regs, sp, bp, log_lvl); |
6fcbede3 AH |
306 | } |
307 | ||
57da8b96 | 308 | void show_regs(struct pt_regs *regs) |
6fcbede3 AH |
309 | { |
310 | int i; | |
311 | unsigned long sp; | |
6fcbede3 AH |
312 | |
313 | sp = regs->sp; | |
a43cb95d | 314 | show_regs_print_info(KERN_DEFAULT); |
6fcbede3 | 315 | __show_regs(regs, 1); |
6fcbede3 AH |
316 | |
317 | /* | |
318 | * When in-kernel, we also print out the stack and code at the | |
319 | * time of the fault.. | |
320 | */ | |
321 | if (!user_mode(regs)) { | |
322 | unsigned int code_prologue = code_bytes * 43 / 64; | |
323 | unsigned int code_len = code_bytes; | |
324 | unsigned char c; | |
325 | u8 *ip; | |
326 | ||
b0f4c4b3 | 327 | printk(KERN_DEFAULT "Stack:\n"); |
6fcbede3 | 328 | show_stack_log_lvl(NULL, regs, (unsigned long *)sp, |
b0f4c4b3 | 329 | 0, KERN_DEFAULT); |
6fcbede3 | 330 | |
b0f4c4b3 | 331 | printk(KERN_DEFAULT "Code: "); |
6fcbede3 AH |
332 | |
333 | ip = (u8 *)regs->ip - code_prologue; | |
334 | if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { | |
8a541665 | 335 | /* try starting at IP */ |
6fcbede3 AH |
336 | ip = (u8 *)regs->ip; |
337 | code_len = code_len - code_prologue + 1; | |
338 | } | |
339 | for (i = 0; i < code_len; i++, ip++) { | |
340 | if (ip < (u8 *)PAGE_OFFSET || | |
341 | probe_kernel_address(ip, c)) { | |
c767a54b | 342 | pr_cont(" Bad RIP value."); |
6fcbede3 AH |
343 | break; |
344 | } | |
345 | if (ip == (u8 *)regs->ip) | |
c767a54b | 346 | pr_cont("<%02x> ", c); |
6fcbede3 | 347 | else |
c767a54b | 348 | pr_cont("%02x ", c); |
6fcbede3 AH |
349 | } |
350 | } | |
c767a54b | 351 | pr_cont("\n"); |
6fcbede3 AH |
352 | } |
353 | ||
354 | int is_valid_bugaddr(unsigned long ip) | |
355 | { | |
356 | unsigned short ud2; | |
357 | ||
358 | if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2))) | |
359 | return 0; | |
360 | ||
361 | return ud2 == 0x0b0f; | |
362 | } |