]>
Commit | Line | Data |
---|---|---|
bff595c1 CM |
1 | /* |
2 | * arch/arm/kernel/unwind.c | |
3 | * | |
4 | * Copyright (C) 2008 ARM Limited | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | * | |
19 | * | |
20 | * Stack unwinding support for ARM | |
21 | * | |
22 | * An ARM EABI version of gcc is required to generate the unwind | |
23 | * tables. For information about the structure of the unwind tables, | |
24 | * see "Exception Handling ABI for the ARM Architecture" at: | |
25 | * | |
26 | * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html | |
27 | */ | |
28 | ||
29 | #include <linux/kernel.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/module.h> | |
32 | #include <linux/sched.h> | |
33 | #include <linux/slab.h> | |
34 | #include <linux/spinlock.h> | |
35 | #include <linux/list.h> | |
36 | ||
37 | #include <asm/stacktrace.h> | |
38 | #include <asm/traps.h> | |
39 | #include <asm/unwind.h> | |
40 | ||
41 | /* Dummy functions to avoid linker complaints */ | |
42 | void __aeabi_unwind_cpp_pr0(void) | |
43 | { | |
44 | }; | |
45 | EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0); | |
46 | ||
47 | void __aeabi_unwind_cpp_pr1(void) | |
48 | { | |
49 | }; | |
50 | EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1); | |
51 | ||
52 | void __aeabi_unwind_cpp_pr2(void) | |
53 | { | |
54 | }; | |
55 | EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2); | |
56 | ||
57 | struct unwind_ctrl_block { | |
58 | unsigned long vrs[16]; /* virtual register set */ | |
59 | unsigned long *insn; /* pointer to the current instructions word */ | |
60 | int entries; /* number of entries left to interpret */ | |
61 | int byte; /* current byte number in the instructions word */ | |
62 | }; | |
63 | ||
64 | enum regs { | |
b86040a5 CM |
65 | #ifdef CONFIG_THUMB2_KERNEL |
66 | FP = 7, | |
67 | #else | |
bff595c1 | 68 | FP = 11, |
b86040a5 | 69 | #endif |
bff595c1 CM |
70 | SP = 13, |
71 | LR = 14, | |
72 | PC = 15 | |
73 | }; | |
74 | ||
75 | extern struct unwind_idx __start_unwind_idx[]; | |
76 | extern struct unwind_idx __stop_unwind_idx[]; | |
77 | ||
78 | static DEFINE_SPINLOCK(unwind_lock); | |
79 | static LIST_HEAD(unwind_tables); | |
80 | ||
81 | /* Convert a prel31 symbol to an absolute address */ | |
82 | #define prel31_to_addr(ptr) \ | |
83 | ({ \ | |
84 | /* sign-extend to 32 bits */ \ | |
85 | long offset = (((long)*(ptr)) << 1) >> 1; \ | |
86 | (unsigned long)(ptr) + offset; \ | |
87 | }) | |
88 | ||
89 | /* | |
90 | * Binary search in the unwind index. The entries entries are | |
91 | * guaranteed to be sorted in ascending order by the linker. | |
92 | */ | |
93 | static struct unwind_idx *search_index(unsigned long addr, | |
94 | struct unwind_idx *first, | |
95 | struct unwind_idx *last) | |
96 | { | |
97 | pr_debug("%s(%08lx, %p, %p)\n", __func__, addr, first, last); | |
98 | ||
99 | if (addr < first->addr) { | |
100 | pr_warning("unwind: Unknown symbol address %08lx\n", addr); | |
101 | return NULL; | |
102 | } else if (addr >= last->addr) | |
103 | return last; | |
104 | ||
105 | while (first < last - 1) { | |
106 | struct unwind_idx *mid = first + ((last - first + 1) >> 1); | |
107 | ||
108 | if (addr < mid->addr) | |
109 | last = mid; | |
110 | else | |
111 | first = mid; | |
112 | } | |
113 | ||
114 | return first; | |
115 | } | |
116 | ||
117 | static struct unwind_idx *unwind_find_idx(unsigned long addr) | |
118 | { | |
119 | struct unwind_idx *idx = NULL; | |
120 | unsigned long flags; | |
121 | ||
122 | pr_debug("%s(%08lx)\n", __func__, addr); | |
123 | ||
124 | if (core_kernel_text(addr)) | |
125 | /* main unwind table */ | |
126 | idx = search_index(addr, __start_unwind_idx, | |
127 | __stop_unwind_idx - 1); | |
128 | else { | |
129 | /* module unwind tables */ | |
130 | struct unwind_table *table; | |
131 | ||
132 | spin_lock_irqsave(&unwind_lock, flags); | |
133 | list_for_each_entry(table, &unwind_tables, list) { | |
134 | if (addr >= table->begin_addr && | |
135 | addr < table->end_addr) { | |
136 | idx = search_index(addr, table->start, | |
137 | table->stop - 1); | |
138 | break; | |
139 | } | |
140 | } | |
141 | spin_unlock_irqrestore(&unwind_lock, flags); | |
142 | } | |
143 | ||
144 | pr_debug("%s: idx = %p\n", __func__, idx); | |
145 | return idx; | |
146 | } | |
147 | ||
148 | static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl) | |
149 | { | |
150 | unsigned long ret; | |
151 | ||
152 | if (ctrl->entries <= 0) { | |
153 | pr_warning("unwind: Corrupt unwind table\n"); | |
154 | return 0; | |
155 | } | |
156 | ||
157 | ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff; | |
158 | ||
159 | if (ctrl->byte == 0) { | |
160 | ctrl->insn++; | |
161 | ctrl->entries--; | |
162 | ctrl->byte = 3; | |
163 | } else | |
164 | ctrl->byte--; | |
165 | ||
166 | return ret; | |
167 | } | |
168 | ||
169 | /* | |
170 | * Execute the current unwind instruction. | |
171 | */ | |
172 | static int unwind_exec_insn(struct unwind_ctrl_block *ctrl) | |
173 | { | |
174 | unsigned long insn = unwind_get_byte(ctrl); | |
175 | ||
176 | pr_debug("%s: insn = %08lx\n", __func__, insn); | |
177 | ||
178 | if ((insn & 0xc0) == 0x00) | |
179 | ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4; | |
180 | else if ((insn & 0xc0) == 0x40) | |
181 | ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4; | |
182 | else if ((insn & 0xf0) == 0x80) { | |
183 | unsigned long mask; | |
184 | unsigned long *vsp = (unsigned long *)ctrl->vrs[SP]; | |
185 | int load_sp, reg = 4; | |
186 | ||
187 | insn = (insn << 8) | unwind_get_byte(ctrl); | |
188 | mask = insn & 0x0fff; | |
189 | if (mask == 0) { | |
190 | pr_warning("unwind: 'Refuse to unwind' instruction %04lx\n", | |
191 | insn); | |
192 | return -URC_FAILURE; | |
193 | } | |
194 | ||
195 | /* pop R4-R15 according to mask */ | |
196 | load_sp = mask & (1 << (13 - 4)); | |
197 | while (mask) { | |
198 | if (mask & 1) | |
199 | ctrl->vrs[reg] = *vsp++; | |
200 | mask >>= 1; | |
201 | reg++; | |
202 | } | |
203 | if (!load_sp) | |
204 | ctrl->vrs[SP] = (unsigned long)vsp; | |
205 | } else if ((insn & 0xf0) == 0x90 && | |
206 | (insn & 0x0d) != 0x0d) | |
207 | ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f]; | |
208 | else if ((insn & 0xf0) == 0xa0) { | |
209 | unsigned long *vsp = (unsigned long *)ctrl->vrs[SP]; | |
210 | int reg; | |
211 | ||
212 | /* pop R4-R[4+bbb] */ | |
213 | for (reg = 4; reg <= 4 + (insn & 7); reg++) | |
214 | ctrl->vrs[reg] = *vsp++; | |
215 | if (insn & 0x80) | |
216 | ctrl->vrs[14] = *vsp++; | |
217 | ctrl->vrs[SP] = (unsigned long)vsp; | |
218 | } else if (insn == 0xb0) { | |
c894ed69 CM |
219 | if (ctrl->vrs[PC] == 0) |
220 | ctrl->vrs[PC] = ctrl->vrs[LR]; | |
bff595c1 CM |
221 | /* no further processing */ |
222 | ctrl->entries = 0; | |
223 | } else if (insn == 0xb1) { | |
224 | unsigned long mask = unwind_get_byte(ctrl); | |
225 | unsigned long *vsp = (unsigned long *)ctrl->vrs[SP]; | |
226 | int reg = 0; | |
227 | ||
228 | if (mask == 0 || mask & 0xf0) { | |
229 | pr_warning("unwind: Spare encoding %04lx\n", | |
230 | (insn << 8) | mask); | |
231 | return -URC_FAILURE; | |
232 | } | |
233 | ||
234 | /* pop R0-R3 according to mask */ | |
235 | while (mask) { | |
236 | if (mask & 1) | |
237 | ctrl->vrs[reg] = *vsp++; | |
238 | mask >>= 1; | |
239 | reg++; | |
240 | } | |
241 | ctrl->vrs[SP] = (unsigned long)vsp; | |
242 | } else if (insn == 0xb2) { | |
243 | unsigned long uleb128 = unwind_get_byte(ctrl); | |
244 | ||
245 | ctrl->vrs[SP] += 0x204 + (uleb128 << 2); | |
246 | } else { | |
247 | pr_warning("unwind: Unhandled instruction %02lx\n", insn); | |
248 | return -URC_FAILURE; | |
249 | } | |
250 | ||
251 | pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__, | |
252 | ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]); | |
253 | ||
254 | return URC_OK; | |
255 | } | |
256 | ||
257 | /* | |
258 | * Unwind a single frame starting with *sp for the symbol at *pc. It | |
259 | * updates the *pc and *sp with the new values. | |
260 | */ | |
261 | int unwind_frame(struct stackframe *frame) | |
262 | { | |
263 | unsigned long high, low; | |
264 | struct unwind_idx *idx; | |
265 | struct unwind_ctrl_block ctrl; | |
266 | ||
267 | /* only go to a higher address on the stack */ | |
268 | low = frame->sp; | |
269 | high = ALIGN(low, THREAD_SIZE) + THREAD_SIZE; | |
270 | ||
271 | pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__, | |
272 | frame->pc, frame->lr, frame->sp); | |
273 | ||
274 | if (!kernel_text_address(frame->pc)) | |
275 | return -URC_FAILURE; | |
276 | ||
277 | idx = unwind_find_idx(frame->pc); | |
278 | if (!idx) { | |
279 | pr_warning("unwind: Index not found %08lx\n", frame->pc); | |
280 | return -URC_FAILURE; | |
281 | } | |
282 | ||
283 | ctrl.vrs[FP] = frame->fp; | |
284 | ctrl.vrs[SP] = frame->sp; | |
285 | ctrl.vrs[LR] = frame->lr; | |
286 | ctrl.vrs[PC] = 0; | |
287 | ||
288 | if (idx->insn == 1) | |
289 | /* can't unwind */ | |
290 | return -URC_FAILURE; | |
291 | else if ((idx->insn & 0x80000000) == 0) | |
292 | /* prel31 to the unwind table */ | |
293 | ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn); | |
294 | else if ((idx->insn & 0xff000000) == 0x80000000) | |
295 | /* only personality routine 0 supported in the index */ | |
296 | ctrl.insn = &idx->insn; | |
297 | else { | |
298 | pr_warning("unwind: Unsupported personality routine %08lx in the index at %p\n", | |
299 | idx->insn, idx); | |
300 | return -URC_FAILURE; | |
301 | } | |
302 | ||
303 | /* check the personality routine */ | |
304 | if ((*ctrl.insn & 0xff000000) == 0x80000000) { | |
305 | ctrl.byte = 2; | |
306 | ctrl.entries = 1; | |
307 | } else if ((*ctrl.insn & 0xff000000) == 0x81000000) { | |
308 | ctrl.byte = 1; | |
309 | ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16); | |
310 | } else { | |
311 | pr_warning("unwind: Unsupported personality routine %08lx at %p\n", | |
312 | *ctrl.insn, ctrl.insn); | |
313 | return -URC_FAILURE; | |
314 | } | |
315 | ||
316 | while (ctrl.entries > 0) { | |
c894ed69 | 317 | int urc = unwind_exec_insn(&ctrl); |
bff595c1 CM |
318 | if (urc < 0) |
319 | return urc; | |
c894ed69 CM |
320 | if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= high) |
321 | return -URC_FAILURE; | |
bff595c1 CM |
322 | } |
323 | ||
324 | if (ctrl.vrs[PC] == 0) | |
325 | ctrl.vrs[PC] = ctrl.vrs[LR]; | |
326 | ||
c894ed69 CM |
327 | /* check for infinite loop */ |
328 | if (frame->pc == ctrl.vrs[PC]) | |
329 | return -URC_FAILURE; | |
330 | ||
bff595c1 CM |
331 | frame->fp = ctrl.vrs[FP]; |
332 | frame->sp = ctrl.vrs[SP]; | |
333 | frame->lr = ctrl.vrs[LR]; | |
334 | frame->pc = ctrl.vrs[PC]; | |
335 | ||
336 | return URC_OK; | |
337 | } | |
338 | ||
339 | void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk) | |
340 | { | |
341 | struct stackframe frame; | |
bff595c1 CM |
342 | register unsigned long current_sp asm ("sp"); |
343 | ||
344 | pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk); | |
345 | ||
346 | if (!tsk) | |
347 | tsk = current; | |
348 | ||
349 | if (regs) { | |
350 | frame.fp = regs->ARM_fp; | |
351 | frame.sp = regs->ARM_sp; | |
352 | frame.lr = regs->ARM_lr; | |
353 | frame.pc = regs->ARM_pc; | |
354 | } else if (tsk == current) { | |
355 | frame.fp = (unsigned long)__builtin_frame_address(0); | |
356 | frame.sp = current_sp; | |
357 | frame.lr = (unsigned long)__builtin_return_address(0); | |
358 | frame.pc = (unsigned long)unwind_backtrace; | |
359 | } else { | |
360 | /* task blocked in __switch_to */ | |
361 | frame.fp = thread_saved_fp(tsk); | |
362 | frame.sp = thread_saved_sp(tsk); | |
363 | /* | |
364 | * The function calling __switch_to cannot be a leaf function | |
365 | * so LR is recovered from the stack. | |
366 | */ | |
367 | frame.lr = 0; | |
368 | frame.pc = thread_saved_pc(tsk); | |
369 | } | |
370 | ||
bff595c1 CM |
371 | while (1) { |
372 | int urc; | |
373 | unsigned long where = frame.pc; | |
374 | ||
375 | urc = unwind_frame(&frame); | |
376 | if (urc < 0) | |
377 | break; | |
378 | dump_backtrace_entry(where, frame.pc, frame.sp - 4); | |
379 | } | |
380 | } | |
381 | ||
382 | struct unwind_table *unwind_table_add(unsigned long start, unsigned long size, | |
383 | unsigned long text_addr, | |
384 | unsigned long text_size) | |
385 | { | |
386 | unsigned long flags; | |
387 | struct unwind_idx *idx; | |
388 | struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL); | |
389 | ||
390 | pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size, | |
391 | text_addr, text_size); | |
392 | ||
393 | if (!tab) | |
394 | return tab; | |
395 | ||
396 | tab->start = (struct unwind_idx *)start; | |
397 | tab->stop = (struct unwind_idx *)(start + size); | |
398 | tab->begin_addr = text_addr; | |
399 | tab->end_addr = text_addr + text_size; | |
400 | ||
401 | /* Convert the symbol addresses to absolute values */ | |
402 | for (idx = tab->start; idx < tab->stop; idx++) | |
403 | idx->addr = prel31_to_addr(&idx->addr); | |
404 | ||
405 | spin_lock_irqsave(&unwind_lock, flags); | |
406 | list_add_tail(&tab->list, &unwind_tables); | |
407 | spin_unlock_irqrestore(&unwind_lock, flags); | |
408 | ||
409 | return tab; | |
410 | } | |
411 | ||
412 | void unwind_table_del(struct unwind_table *tab) | |
413 | { | |
414 | unsigned long flags; | |
415 | ||
416 | if (!tab) | |
417 | return; | |
418 | ||
419 | spin_lock_irqsave(&unwind_lock, flags); | |
420 | list_del(&tab->list); | |
421 | spin_unlock_irqrestore(&unwind_lock, flags); | |
422 | ||
423 | kfree(tab); | |
424 | } | |
425 | ||
426 | int __init unwind_init(void) | |
427 | { | |
428 | struct unwind_idx *idx; | |
429 | ||
430 | /* Convert the symbol addresses to absolute values */ | |
431 | for (idx = __start_unwind_idx; idx < __stop_unwind_idx; idx++) | |
432 | idx->addr = prel31_to_addr(&idx->addr); | |
433 | ||
434 | pr_debug("unwind: ARM stack unwinding initialised\n"); | |
435 | ||
436 | return 0; | |
437 | } |