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 | 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 | |
1aabc599 | 114 | analyze_stack(int cpu, struct task_struct *task, unsigned long *stack, |
63c95654 SRRH |
115 | unsigned long **stack_end, unsigned long *irq_stack, |
116 | unsigned *used, char **id) | |
2223f6f6 | 117 | { |
2223f6f6 | 118 | unsigned long addr; |
2223f6f6 SR |
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, | |
1aabc599 | 125 | used, id); |
2223f6f6 SR |
126 | if (*stack_end) |
127 | return STACK_IS_EXCEPTION; | |
128 | ||
63c95654 SRRH |
129 | if (!irq_stack) |
130 | return STACK_IS_NORMAL; | |
2223f6f6 | 131 | |
63c95654 | 132 | *stack_end = irq_stack; |
4950d6d4 | 133 | irq_stack -= (IRQ_STACK_SIZE / sizeof(long)); |
2223f6f6 SR |
134 | |
135 | if (in_irq_stack(stack, irq_stack, *stack_end)) | |
136 | return STACK_IS_IRQ; | |
137 | ||
138 | return STACK_IS_UNKNOWN; | |
139 | } | |
140 | ||
6fcbede3 AH |
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 | ||
e8e999cf NK |
148 | void dump_trace(struct task_struct *task, struct pt_regs *regs, |
149 | unsigned long *stack, unsigned long bp, | |
6fcbede3 AH |
150 | const struct stacktrace_ops *ops, void *data) |
151 | { | |
152 | const unsigned cpu = get_cpu(); | |
63c95654 | 153 | unsigned long *irq_stack = (unsigned long *)per_cpu(irq_stack_ptr, cpu); |
1aabc599 | 154 | unsigned used = 0; |
2223f6f6 SR |
155 | int graph = 0; |
156 | int done = 0; | |
6fcbede3 | 157 | |
4b8afafb JP |
158 | task = task ? : current; |
159 | stack = stack ? : get_stack_pointer(task, regs); | |
160 | bp = bp ? : (unsigned long)get_frame_pointer(task, regs); | |
6fcbede3 | 161 | |
6fcbede3 AH |
162 | /* |
163 | * Print function call entries in all stacks, starting at the | |
164 | * current stack address. If the stacks consist of nested | |
165 | * exceptions | |
166 | */ | |
2223f6f6 SR |
167 | while (!done) { |
168 | unsigned long *stack_end; | |
169 | enum stack_type stype; | |
6fcbede3 | 170 | char *id; |
6fcbede3 | 171 | |
63c95654 SRRH |
172 | stype = analyze_stack(cpu, task, stack, &stack_end, |
173 | irq_stack, &used, &id); | |
2223f6f6 SR |
174 | |
175 | /* Default finish unless specified to continue */ | |
176 | done = 1; | |
177 | ||
178 | switch (stype) { | |
179 | ||
180 | /* Break out early if we are on the thread stack */ | |
181 | case STACK_IS_NORMAL: | |
182 | break; | |
183 | ||
184 | case STACK_IS_EXCEPTION: | |
185 | ||
6fcbede3 AH |
186 | if (ops->stack(data, id) < 0) |
187 | break; | |
188 | ||
da01e18a | 189 | bp = ops->walk_stack(task, stack, bp, ops, |
2223f6f6 | 190 | data, stack_end, &graph); |
6225f323 | 191 | ops->stack(data, "EOE"); |
6fcbede3 AH |
192 | /* |
193 | * We link to the next stack via the | |
194 | * second-to-last pointer (index -2 to end) in the | |
195 | * exception stack: | |
196 | */ | |
2223f6f6 SR |
197 | stack = (unsigned long *) stack_end[-2]; |
198 | done = 0; | |
199 | break; | |
200 | ||
201 | case STACK_IS_IRQ: | |
202 | ||
203 | if (ops->stack(data, "IRQ") < 0) | |
204 | break; | |
da01e18a | 205 | bp = ops->walk_stack(task, stack, bp, |
2223f6f6 SR |
206 | ops, data, stack_end, &graph); |
207 | /* | |
208 | * We link to the next stack (which would be | |
209 | * the process stack normally) the last | |
210 | * pointer (index -1 to end) in the IRQ stack: | |
211 | */ | |
212 | stack = (unsigned long *) (stack_end[-1]); | |
63c95654 | 213 | irq_stack = NULL; |
2223f6f6 SR |
214 | ops->stack(data, "EOI"); |
215 | done = 0; | |
216 | break; | |
217 | ||
218 | case STACK_IS_UNKNOWN: | |
219 | ops->stack(data, "UNK"); | |
220 | break; | |
6fcbede3 | 221 | } |
6fcbede3 AH |
222 | } |
223 | ||
224 | /* | |
225 | * This handles the process stack: | |
226 | */ | |
da01e18a | 227 | bp = ops->walk_stack(task, stack, bp, ops, data, NULL, &graph); |
6fcbede3 AH |
228 | put_cpu(); |
229 | } | |
230 | EXPORT_SYMBOL(dump_trace); | |
231 | ||
878719e8 | 232 | void |
6fcbede3 | 233 | show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, |
e8e999cf | 234 | unsigned long *sp, unsigned long bp, char *log_lvl) |
6fcbede3 | 235 | { |
67f2de0b IM |
236 | unsigned long *irq_stack_end; |
237 | unsigned long *irq_stack; | |
6fcbede3 | 238 | unsigned long *stack; |
67f2de0b | 239 | int cpu; |
6fcbede3 | 240 | int i; |
67f2de0b IM |
241 | |
242 | preempt_disable(); | |
243 | cpu = smp_processor_id(); | |
244 | ||
4950d6d4 JP |
245 | irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu)); |
246 | irq_stack = irq_stack_end - (IRQ_STACK_SIZE / sizeof(long)); | |
6fcbede3 | 247 | |
4b8afafb | 248 | sp = sp ? : get_stack_pointer(task, regs); |
6fcbede3 AH |
249 | |
250 | stack = sp; | |
251 | for (i = 0; i < kstack_depth_to_print; i++) { | |
98f30b12 AL |
252 | unsigned long word; |
253 | ||
26f80bd6 BG |
254 | if (stack >= irq_stack && stack <= irq_stack_end) { |
255 | if (stack == irq_stack_end) { | |
256 | stack = (unsigned long *) (irq_stack_end[-1]); | |
c767a54b | 257 | pr_cont(" <EOI> "); |
6fcbede3 AH |
258 | } |
259 | } else { | |
04769ae3 | 260 | if (kstack_end(stack)) |
6fcbede3 AH |
261 | break; |
262 | } | |
98f30b12 AL |
263 | |
264 | if (probe_kernel_address(stack, word)) | |
265 | break; | |
266 | ||
1fc7f61c AS |
267 | if ((i % STACKSLOTS_PER_LINE) == 0) { |
268 | if (i != 0) | |
269 | pr_cont("\n"); | |
98f30b12 | 270 | printk("%s %016lx", log_lvl, word); |
1fc7f61c | 271 | } else |
98f30b12 AL |
272 | pr_cont(" %016lx", word); |
273 | ||
274 | stack++; | |
6fcbede3 AH |
275 | touch_nmi_watchdog(); |
276 | } | |
67f2de0b IM |
277 | preempt_enable(); |
278 | ||
c767a54b | 279 | pr_cont("\n"); |
e8e999cf | 280 | show_trace_log_lvl(task, regs, sp, bp, log_lvl); |
6fcbede3 AH |
281 | } |
282 | ||
57da8b96 | 283 | void show_regs(struct pt_regs *regs) |
6fcbede3 AH |
284 | { |
285 | int i; | |
6fcbede3 | 286 | |
a43cb95d | 287 | show_regs_print_info(KERN_DEFAULT); |
6fcbede3 | 288 | __show_regs(regs, 1); |
6fcbede3 AH |
289 | |
290 | /* | |
291 | * When in-kernel, we also print out the stack and code at the | |
292 | * time of the fault.. | |
293 | */ | |
294 | if (!user_mode(regs)) { | |
295 | unsigned int code_prologue = code_bytes * 43 / 64; | |
296 | unsigned int code_len = code_bytes; | |
297 | unsigned char c; | |
298 | u8 *ip; | |
299 | ||
b0f4c4b3 | 300 | printk(KERN_DEFAULT "Stack:\n"); |
5a8ff54c | 301 | show_stack_log_lvl(NULL, regs, NULL, 0, KERN_DEFAULT); |
6fcbede3 | 302 | |
b0f4c4b3 | 303 | printk(KERN_DEFAULT "Code: "); |
6fcbede3 AH |
304 | |
305 | ip = (u8 *)regs->ip - code_prologue; | |
306 | if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { | |
8a541665 | 307 | /* try starting at IP */ |
6fcbede3 AH |
308 | ip = (u8 *)regs->ip; |
309 | code_len = code_len - code_prologue + 1; | |
310 | } | |
311 | for (i = 0; i < code_len; i++, ip++) { | |
312 | if (ip < (u8 *)PAGE_OFFSET || | |
313 | probe_kernel_address(ip, c)) { | |
c767a54b | 314 | pr_cont(" Bad RIP value."); |
6fcbede3 AH |
315 | break; |
316 | } | |
317 | if (ip == (u8 *)regs->ip) | |
c767a54b | 318 | pr_cont("<%02x> ", c); |
6fcbede3 | 319 | else |
c767a54b | 320 | pr_cont("%02x ", c); |
6fcbede3 AH |
321 | } |
322 | } | |
c767a54b | 323 | pr_cont("\n"); |
6fcbede3 AH |
324 | } |
325 | ||
326 | int is_valid_bugaddr(unsigned long ip) | |
327 | { | |
328 | unsigned short ud2; | |
329 | ||
330 | if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2))) | |
331 | return 0; | |
332 | ||
333 | return ud2 == 0x0b0f; | |
334 | } |