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> | |
10 | #include <linux/module.h> | |
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 | ||
878719e8 | 19 | #include "dumpstack.h" |
6fcbede3 | 20 | |
b8030906 IM |
21 | #define N_EXCEPTION_STACKS_END \ |
22 | (N_EXCEPTION_STACKS + DEBUG_STKSZ/EXCEPTION_STKSZ - 2) | |
0406ca6d FW |
23 | |
24 | static char x86_stack_ids[][8] = { | |
b8030906 IM |
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", | |
6fcbede3 | 30 | #if DEBUG_STKSZ > EXCEPTION_STKSZ |
b8030906 IM |
31 | [ N_EXCEPTION_STACKS ... |
32 | N_EXCEPTION_STACKS_END ] = "#DB[?]" | |
6fcbede3 | 33 | #endif |
b8030906 | 34 | }; |
0406ca6d | 35 | |
0406ca6d | 36 | static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack, |
b8030906 | 37 | unsigned *usedp, char **idp) |
0406ca6d | 38 | { |
6fcbede3 AH |
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; | |
0406ca6d | 67 | *idp = x86_stack_ids[k]; |
6fcbede3 AH |
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; | |
0406ca6d FW |
87 | x86_stack_ids[j][4] = '1' + |
88 | (j - N_EXCEPTION_STACKS); | |
6fcbede3 AH |
89 | } while (stack < end - EXCEPTION_STKSZ); |
90 | if (*usedp & (1U << j)) | |
91 | break; | |
92 | *usedp |= 1U << j; | |
0406ca6d | 93 | *idp = x86_stack_ids[j]; |
6fcbede3 AH |
94 | return (unsigned long *)end; |
95 | } | |
96 | #endif | |
97 | } | |
98 | return NULL; | |
99 | } | |
100 | ||
af2d8289 FW |
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 | * We are returning from the irq stack and go to the previous one. | |
110 | * If the previous stack is also in the irq stack, then bp in the first | |
111 | * frame of the irq stack points to the previous, interrupted one. | |
112 | * Otherwise we have another level of indirection: We first save | |
113 | * the bp of the previous stack, then we switch the stack to the irq one | |
114 | * and save a new bp that links to the previous one. | |
115 | * (See save_args()) | |
116 | */ | |
117 | static inline unsigned long | |
118 | fixup_bp_irq_link(unsigned long bp, unsigned long *stack, | |
119 | unsigned long *irq_stack, unsigned long *irq_stack_end) | |
120 | { | |
121 | #ifdef CONFIG_FRAME_POINTER | |
122 | struct stack_frame *frame = (struct stack_frame *)bp; | |
123 | ||
124 | if (!in_irq_stack(stack, irq_stack, irq_stack_end)) | |
125 | return (unsigned long)frame->next_frame; | |
126 | #endif | |
127 | return bp; | |
128 | } | |
129 | ||
6fcbede3 AH |
130 | /* |
131 | * x86-64 can have up to three kernel stacks: | |
132 | * process stack | |
133 | * interrupt stack | |
134 | * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack | |
135 | */ | |
136 | ||
6fcbede3 AH |
137 | void dump_trace(struct task_struct *task, struct pt_regs *regs, |
138 | unsigned long *stack, unsigned long bp, | |
139 | const struct stacktrace_ops *ops, void *data) | |
140 | { | |
141 | const unsigned cpu = get_cpu(); | |
26f80bd6 BG |
142 | unsigned long *irq_stack_end = |
143 | (unsigned long *)per_cpu(irq_stack_ptr, cpu); | |
6fcbede3 AH |
144 | unsigned used = 0; |
145 | struct thread_info *tinfo; | |
7ee991fb | 146 | int graph = 0; |
6fcbede3 AH |
147 | |
148 | if (!task) | |
149 | task = current; | |
150 | ||
151 | if (!stack) { | |
152 | unsigned long dummy; | |
153 | stack = &dummy; | |
154 | if (task && task != current) | |
155 | stack = (unsigned long *)task->thread.sp; | |
156 | } | |
157 | ||
158 | #ifdef CONFIG_FRAME_POINTER | |
159 | if (!bp) { | |
160 | if (task == current) { | |
161 | /* Grab bp right from our regs */ | |
8a541665 | 162 | get_bp(bp); |
6fcbede3 AH |
163 | } else { |
164 | /* bp is the last reg pushed by switch_to */ | |
165 | bp = *(unsigned long *) task->thread.sp; | |
166 | } | |
167 | } | |
168 | #endif | |
169 | ||
170 | /* | |
171 | * Print function call entries in all stacks, starting at the | |
172 | * current stack address. If the stacks consist of nested | |
173 | * exceptions | |
174 | */ | |
175 | tinfo = task_thread_info(task); | |
176 | for (;;) { | |
177 | char *id; | |
178 | unsigned long *estack_end; | |
179 | estack_end = in_exception_stack(cpu, (unsigned long)stack, | |
180 | &used, &id); | |
181 | ||
182 | if (estack_end) { | |
183 | if (ops->stack(data, id) < 0) | |
184 | break; | |
185 | ||
61c1917f FW |
186 | bp = ops->walk_stack(tinfo, stack, bp, ops, |
187 | data, estack_end, &graph); | |
6fcbede3 AH |
188 | ops->stack(data, "<EOE>"); |
189 | /* | |
190 | * We link to the next stack via the | |
191 | * second-to-last pointer (index -2 to end) in the | |
192 | * exception stack: | |
193 | */ | |
194 | stack = (unsigned long *) estack_end[-2]; | |
195 | continue; | |
196 | } | |
26f80bd6 BG |
197 | if (irq_stack_end) { |
198 | unsigned long *irq_stack; | |
199 | irq_stack = irq_stack_end - | |
200 | (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack); | |
6fcbede3 | 201 | |
af2d8289 | 202 | if (in_irq_stack(stack, irq_stack, irq_stack_end)) { |
6fcbede3 AH |
203 | if (ops->stack(data, "IRQ") < 0) |
204 | break; | |
205 | bp = print_context_stack(tinfo, stack, bp, | |
26f80bd6 | 206 | ops, data, irq_stack_end, &graph); |
6fcbede3 AH |
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 | */ | |
26f80bd6 | 212 | stack = (unsigned long *) (irq_stack_end[-1]); |
af2d8289 FW |
213 | bp = fixup_bp_irq_link(bp, stack, irq_stack, |
214 | irq_stack_end); | |
26f80bd6 | 215 | irq_stack_end = NULL; |
6fcbede3 AH |
216 | ops->stack(data, "EOI"); |
217 | continue; | |
218 | } | |
219 | } | |
220 | break; | |
221 | } | |
222 | ||
223 | /* | |
224 | * This handles the process stack: | |
225 | */ | |
7ee991fb | 226 | bp = print_context_stack(tinfo, stack, bp, ops, data, NULL, &graph); |
6fcbede3 AH |
227 | put_cpu(); |
228 | } | |
229 | EXPORT_SYMBOL(dump_trace); | |
230 | ||
878719e8 | 231 | void |
6fcbede3 | 232 | show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, |
b8030906 | 233 | unsigned long *sp, unsigned long bp, char *log_lvl) |
6fcbede3 | 234 | { |
67f2de0b IM |
235 | unsigned long *irq_stack_end; |
236 | unsigned long *irq_stack; | |
6fcbede3 | 237 | unsigned long *stack; |
67f2de0b | 238 | int cpu; |
6fcbede3 | 239 | int i; |
67f2de0b IM |
240 | |
241 | preempt_disable(); | |
242 | cpu = smp_processor_id(); | |
243 | ||
244 | irq_stack_end = (unsigned long *)(per_cpu(irq_stack_ptr, cpu)); | |
245 | irq_stack = (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE); | |
6fcbede3 AH |
246 | |
247 | /* | |
67f2de0b IM |
248 | * Debugging aid: "show_stack(NULL, NULL);" prints the |
249 | * back trace for this cpu: | |
6fcbede3 | 250 | */ |
6fcbede3 AH |
251 | if (sp == NULL) { |
252 | if (task) | |
253 | sp = (unsigned long *)task->thread.sp; | |
254 | else | |
255 | sp = (unsigned long *)&sp; | |
256 | } | |
257 | ||
258 | stack = sp; | |
259 | for (i = 0; i < kstack_depth_to_print; i++) { | |
26f80bd6 BG |
260 | if (stack >= irq_stack && stack <= irq_stack_end) { |
261 | if (stack == irq_stack_end) { | |
262 | stack = (unsigned long *) (irq_stack_end[-1]); | |
6fcbede3 AH |
263 | printk(" <EOI> "); |
264 | } | |
265 | } else { | |
266 | if (((long) stack & (THREAD_SIZE-1)) == 0) | |
267 | break; | |
268 | } | |
8a541665 | 269 | if (i && ((i % STACKSLOTS_PER_LINE) == 0)) |
ca0a8164 | 270 | printk("\n%s", log_lvl); |
6fcbede3 AH |
271 | printk(" %016lx", *stack++); |
272 | touch_nmi_watchdog(); | |
273 | } | |
67f2de0b IM |
274 | preempt_enable(); |
275 | ||
6fcbede3 AH |
276 | printk("\n"); |
277 | show_trace_log_lvl(task, regs, sp, bp, log_lvl); | |
278 | } | |
279 | ||
6fcbede3 AH |
280 | void show_registers(struct pt_regs *regs) |
281 | { | |
282 | int i; | |
283 | unsigned long sp; | |
284 | const int cpu = smp_processor_id(); | |
c6f5e0ac | 285 | struct task_struct *cur = current; |
6fcbede3 AH |
286 | |
287 | sp = regs->sp; | |
288 | printk("CPU %d ", cpu); | |
f266d7f5 | 289 | print_modules(); |
6fcbede3 AH |
290 | __show_regs(regs, 1); |
291 | printk("Process %s (pid: %d, threadinfo %p, task %p)\n", | |
292 | cur->comm, cur->pid, task_thread_info(cur), cur); | |
293 | ||
294 | /* | |
295 | * When in-kernel, we also print out the stack and code at the | |
296 | * time of the fault.. | |
297 | */ | |
298 | if (!user_mode(regs)) { | |
299 | unsigned int code_prologue = code_bytes * 43 / 64; | |
300 | unsigned int code_len = code_bytes; | |
301 | unsigned char c; | |
302 | u8 *ip; | |
303 | ||
ca0a8164 | 304 | printk(KERN_EMERG "Stack:\n"); |
6fcbede3 | 305 | show_stack_log_lvl(NULL, regs, (unsigned long *)sp, |
ca0a8164 | 306 | regs->bp, KERN_EMERG); |
6fcbede3 AH |
307 | |
308 | printk(KERN_EMERG "Code: "); | |
309 | ||
310 | ip = (u8 *)regs->ip - code_prologue; | |
311 | if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { | |
8a541665 | 312 | /* try starting at IP */ |
6fcbede3 AH |
313 | ip = (u8 *)regs->ip; |
314 | code_len = code_len - code_prologue + 1; | |
315 | } | |
316 | for (i = 0; i < code_len; i++, ip++) { | |
317 | if (ip < (u8 *)PAGE_OFFSET || | |
318 | probe_kernel_address(ip, c)) { | |
319 | printk(" Bad RIP value."); | |
320 | break; | |
321 | } | |
322 | if (ip == (u8 *)regs->ip) | |
323 | printk("<%02x> ", c); | |
324 | else | |
325 | printk("%02x ", c); | |
326 | } | |
327 | } | |
328 | printk("\n"); | |
329 | } | |
330 | ||
331 | int is_valid_bugaddr(unsigned long ip) | |
332 | { | |
333 | unsigned short ud2; | |
334 | ||
335 | if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2))) | |
336 | return 0; | |
337 | ||
338 | return ud2 == 0x0b0f; | |
339 | } |