2 * arch/arm/kernel/unwind.c
4 * Copyright (C) 2008 ARM Limited
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.
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.
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
20 * Stack unwinding support for ARM
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:
26 * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html
30 #if !defined (__ARM_EABI__)
31 #warning Your compiler does not have EABI support.
32 #warning ARM unwind is known to compile only with EABI compilers.
33 #warning Change compiler or disable ARM_UNWIND option.
34 #elif (__GNUC__ == 4 && __GNUC_MINOR__ <= 2) && !defined(__clang__)
35 #warning Your compiler is too buggy; it is known to not compile ARM unwind support.
36 #warning Change compiler or disable ARM_UNWIND option.
38 #endif /* __CHECKER__ */
40 #include <linux/kernel.h>
41 #include <linux/init.h>
42 #include <linux/export.h>
43 #include <linux/sched.h>
44 #include <linux/slab.h>
45 #include <linux/spinlock.h>
46 #include <linux/list.h>
48 #include <asm/stacktrace.h>
49 #include <asm/traps.h>
50 #include <asm/unwind.h>
52 /* Dummy functions to avoid linker complaints */
53 void __aeabi_unwind_cpp_pr0(void)
56 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);
58 void __aeabi_unwind_cpp_pr1(void)
61 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);
63 void __aeabi_unwind_cpp_pr2(void)
66 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);
68 struct unwind_ctrl_block {
69 unsigned long vrs[16]; /* virtual register set */
70 const unsigned long *insn; /* pointer to the current instructions word */
71 unsigned long sp_high; /* highest value of sp allowed */
73 * 1 : check for stack overflow for each register pop.
74 * 0 : save overhead if there is plenty of stack remaining.
77 int entries; /* number of entries left to interpret */
78 int byte; /* current byte number in the instructions word */
82 #ifdef CONFIG_THUMB2_KERNEL
92 extern const struct unwind_idx __start_unwind_idx[];
93 static const struct unwind_idx *__origin_unwind_idx;
94 extern const struct unwind_idx __stop_unwind_idx[];
96 static DEFINE_RAW_SPINLOCK(unwind_lock);
97 static LIST_HEAD(unwind_tables);
99 /* Convert a prel31 symbol to an absolute address */
100 #define prel31_to_addr(ptr) \
102 /* sign-extend to 32 bits */ \
103 long offset = (((long)*(ptr)) << 1) >> 1; \
104 (unsigned long)(ptr) + offset; \
108 * Binary search in the unwind index. The entries are
109 * guaranteed to be sorted in ascending order by the linker.
111 * start = first entry
112 * origin = first entry with positive offset (or stop if there is no such entry)
113 * stop - 1 = last entry
115 static const struct unwind_idx *search_index(unsigned long addr,
116 const struct unwind_idx *start,
117 const struct unwind_idx *origin,
118 const struct unwind_idx *stop)
120 unsigned long addr_prel31;
122 pr_debug("%s(%08lx, %p, %p, %p)\n",
123 __func__, addr, start, origin, stop);
126 * only search in the section with the matching sign. This way the
127 * prel31 numbers can be compared as unsigned longs.
129 if (addr < (unsigned long)start)
130 /* negative offsets: [start; origin) */
133 /* positive offsets: [origin; stop) */
136 /* prel31 for address relavive to start */
137 addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;
139 while (start < stop - 1) {
140 const struct unwind_idx *mid = start + ((stop - start) >> 1);
143 * As addr_prel31 is relative to start an offset is needed to
144 * make it relative to mid.
146 if (addr_prel31 - ((unsigned long)mid - (unsigned long)start) <
150 /* keep addr_prel31 relative to start */
151 addr_prel31 -= ((unsigned long)mid -
152 (unsigned long)start);
157 if (likely(start->addr_offset <= addr_prel31))
160 pr_warn("unwind: Unknown symbol address %08lx\n", addr);
165 static const struct unwind_idx *unwind_find_origin(
166 const struct unwind_idx *start, const struct unwind_idx *stop)
168 pr_debug("%s(%p, %p)\n", __func__, start, stop);
169 while (start < stop) {
170 const struct unwind_idx *mid = start + ((stop - start) >> 1);
172 if (mid->addr_offset >= 0x40000000)
173 /* negative offset */
176 /* positive offset */
179 pr_debug("%s -> %p\n", __func__, stop);
183 static const struct unwind_idx *unwind_find_idx(unsigned long addr)
185 const struct unwind_idx *idx = NULL;
188 pr_debug("%s(%08lx)\n", __func__, addr);
190 if (core_kernel_text(addr)) {
191 if (unlikely(!__origin_unwind_idx))
192 __origin_unwind_idx =
193 unwind_find_origin(__start_unwind_idx,
196 /* main unwind table */
197 idx = search_index(addr, __start_unwind_idx,
201 /* module unwind tables */
202 struct unwind_table *table;
204 raw_spin_lock_irqsave(&unwind_lock, flags);
205 list_for_each_entry(table, &unwind_tables, list) {
206 if (addr >= table->begin_addr &&
207 addr < table->end_addr) {
208 idx = search_index(addr, table->start,
211 /* Move-to-front to exploit common traces */
212 list_move(&table->list, &unwind_tables);
216 raw_spin_unlock_irqrestore(&unwind_lock, flags);
219 pr_debug("%s: idx = %p\n", __func__, idx);
223 static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
227 if (ctrl->entries <= 0) {
228 pr_warn("unwind: Corrupt unwind table\n");
232 ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
234 if (ctrl->byte == 0) {
244 /* Before poping a register check whether it is feasible or not */
245 static int unwind_pop_register(struct unwind_ctrl_block *ctrl,
246 unsigned long **vsp, unsigned int reg)
248 if (unlikely(ctrl->check_each_pop))
249 if (*vsp >= (unsigned long *)ctrl->sp_high)
252 ctrl->vrs[reg] = *(*vsp)++;
256 /* Helper functions to execute the instructions */
257 static int unwind_exec_pop_subset_r4_to_r13(struct unwind_ctrl_block *ctrl,
260 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
261 int load_sp, reg = 4;
263 load_sp = mask & (1 << (13 - 4));
266 if (unwind_pop_register(ctrl, &vsp, reg))
272 ctrl->vrs[SP] = (unsigned long)vsp;
277 static int unwind_exec_pop_r4_to_rN(struct unwind_ctrl_block *ctrl,
280 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
283 /* pop R4-R[4+bbb] */
284 for (reg = 4; reg <= 4 + (insn & 7); reg++)
285 if (unwind_pop_register(ctrl, &vsp, reg))
289 if (unwind_pop_register(ctrl, &vsp, 14))
292 ctrl->vrs[SP] = (unsigned long)vsp;
297 static int unwind_exec_pop_subset_r0_to_r3(struct unwind_ctrl_block *ctrl,
300 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
303 /* pop R0-R3 according to mask */
306 if (unwind_pop_register(ctrl, &vsp, reg))
311 ctrl->vrs[SP] = (unsigned long)vsp;
316 static unsigned long unwind_decode_uleb128(struct unwind_ctrl_block *ctrl)
318 unsigned long bytes = 0;
320 unsigned long result = 0;
323 * unwind_get_byte() will advance `ctrl` one instruction at a time, so
324 * loop until we get an instruction byte where bit 7 is not set.
326 * Note: This decodes a maximum of 4 bytes to output 28 bits data where
327 * max is 0xfffffff: that will cover a vsp increment of 1073742336, hence
328 * it is sufficient for unwinding the stack.
331 insn = unwind_get_byte(ctrl);
332 result |= (insn & 0x7f) << (bytes * 7);
334 } while (!!(insn & 0x80) && (bytes != sizeof(result)));
340 * Execute the current unwind instruction.
342 static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
344 unsigned long insn = unwind_get_byte(ctrl);
347 pr_debug("%s: insn = %08lx\n", __func__, insn);
349 if ((insn & 0xc0) == 0x00)
350 ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
351 else if ((insn & 0xc0) == 0x40)
352 ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
353 else if ((insn & 0xf0) == 0x80) {
356 insn = (insn << 8) | unwind_get_byte(ctrl);
357 mask = insn & 0x0fff;
359 pr_warn("unwind: 'Refuse to unwind' instruction %04lx\n",
364 ret = unwind_exec_pop_subset_r4_to_r13(ctrl, mask);
367 } else if ((insn & 0xf0) == 0x90 &&
368 (insn & 0x0d) != 0x0d)
369 ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
370 else if ((insn & 0xf0) == 0xa0) {
371 ret = unwind_exec_pop_r4_to_rN(ctrl, insn);
374 } else if (insn == 0xb0) {
375 if (ctrl->vrs[PC] == 0)
376 ctrl->vrs[PC] = ctrl->vrs[LR];
377 /* no further processing */
379 } else if (insn == 0xb1) {
380 unsigned long mask = unwind_get_byte(ctrl);
382 if (mask == 0 || mask & 0xf0) {
383 pr_warn("unwind: Spare encoding %04lx\n",
388 ret = unwind_exec_pop_subset_r0_to_r3(ctrl, mask);
391 } else if (insn == 0xb2) {
392 unsigned long uleb128 = unwind_decode_uleb128(ctrl);
394 ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
396 pr_warn("unwind: Unhandled instruction %02lx\n", insn);
400 pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
401 ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
408 * Unwind a single frame starting with *sp for the symbol at *pc. It
409 * updates the *pc and *sp with the new values.
411 int unwind_frame(struct stackframe *frame)
414 const struct unwind_idx *idx;
415 struct unwind_ctrl_block ctrl;
417 /* store the highest address on the stack to avoid crossing it*/
419 ctrl.sp_high = ALIGN(low, THREAD_SIZE);
421 pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
422 frame->pc, frame->lr, frame->sp);
424 if (!kernel_text_address(frame->pc))
427 idx = unwind_find_idx(frame->pc);
429 pr_warn("unwind: Index not found %08lx\n", frame->pc);
433 ctrl.vrs[FP] = frame->fp;
434 ctrl.vrs[SP] = frame->sp;
435 ctrl.vrs[LR] = frame->lr;
441 else if ((idx->insn & 0x80000000) == 0)
442 /* prel31 to the unwind table */
443 ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
444 else if ((idx->insn & 0xff000000) == 0x80000000)
445 /* only personality routine 0 supported in the index */
446 ctrl.insn = &idx->insn;
448 pr_warn("unwind: Unsupported personality routine %08lx in the index at %p\n",
453 /* check the personality routine */
454 if ((*ctrl.insn & 0xff000000) == 0x80000000) {
457 } else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
459 ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
461 pr_warn("unwind: Unsupported personality routine %08lx at %p\n",
462 *ctrl.insn, ctrl.insn);
466 ctrl.check_each_pop = 0;
468 while (ctrl.entries > 0) {
470 if ((ctrl.sp_high - ctrl.vrs[SP]) < sizeof(ctrl.vrs))
471 ctrl.check_each_pop = 1;
472 urc = unwind_exec_insn(&ctrl);
475 if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= ctrl.sp_high)
479 if (ctrl.vrs[PC] == 0)
480 ctrl.vrs[PC] = ctrl.vrs[LR];
482 /* check for infinite loop */
483 if (frame->pc == ctrl.vrs[PC])
486 frame->fp = ctrl.vrs[FP];
487 frame->sp = ctrl.vrs[SP];
488 frame->lr = ctrl.vrs[LR];
489 frame->pc = ctrl.vrs[PC];
494 void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
496 struct stackframe frame;
498 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
504 arm_get_current_stackframe(regs, &frame);
505 /* PC might be corrupted, use LR in that case. */
506 if (!kernel_text_address(regs->ARM_pc))
507 frame.pc = regs->ARM_lr;
508 } else if (tsk == current) {
509 frame.fp = (unsigned long)__builtin_frame_address(0);
510 frame.sp = current_stack_pointer;
511 frame.lr = (unsigned long)__builtin_return_address(0);
512 frame.pc = (unsigned long)unwind_backtrace;
514 /* task blocked in __switch_to */
515 frame.fp = thread_saved_fp(tsk);
516 frame.sp = thread_saved_sp(tsk);
518 * The function calling __switch_to cannot be a leaf function
519 * so LR is recovered from the stack.
522 frame.pc = thread_saved_pc(tsk);
527 unsigned long where = frame.pc;
529 urc = unwind_frame(&frame);
532 dump_backtrace_entry(where, frame.pc, frame.sp - 4);
536 struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
537 unsigned long text_addr,
538 unsigned long text_size)
541 struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
543 pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
544 text_addr, text_size);
549 tab->start = (const struct unwind_idx *)start;
550 tab->stop = (const struct unwind_idx *)(start + size);
551 tab->origin = unwind_find_origin(tab->start, tab->stop);
552 tab->begin_addr = text_addr;
553 tab->end_addr = text_addr + text_size;
555 raw_spin_lock_irqsave(&unwind_lock, flags);
556 list_add_tail(&tab->list, &unwind_tables);
557 raw_spin_unlock_irqrestore(&unwind_lock, flags);
562 void unwind_table_del(struct unwind_table *tab)
569 raw_spin_lock_irqsave(&unwind_lock, flags);
570 list_del(&tab->list);
571 raw_spin_unlock_irqrestore(&unwind_lock, flags);