1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm/kernel/unwind.c
5 * Copyright (C) 2008 ARM Limited
7 * Stack unwinding support for ARM
9 * An ARM EABI version of gcc is required to generate the unwind
10 * tables. For information about the structure of the unwind tables,
11 * see "Exception Handling ABI for the ARM Architecture" at:
13 * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html
17 #if !defined (__ARM_EABI__)
18 #warning Your compiler does not have EABI support.
19 #warning ARM unwind is known to compile only with EABI compilers.
20 #warning Change compiler or disable ARM_UNWIND option.
21 #elif (__GNUC__ == 4 && __GNUC_MINOR__ <= 2) && !defined(__clang__)
22 #warning Your compiler is too buggy; it is known to not compile ARM unwind support.
23 #warning Change compiler or disable ARM_UNWIND option.
25 #endif /* __CHECKER__ */
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/export.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/list.h>
35 #include <asm/stacktrace.h>
36 #include <asm/traps.h>
37 #include <asm/unwind.h>
39 /* Dummy functions to avoid linker complaints */
40 void __aeabi_unwind_cpp_pr0(void)
43 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);
45 void __aeabi_unwind_cpp_pr1(void)
48 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);
50 void __aeabi_unwind_cpp_pr2(void)
53 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);
55 struct unwind_ctrl_block {
56 unsigned long vrs[16]; /* virtual register set */
57 const unsigned long *insn; /* pointer to the current instructions word */
58 unsigned long sp_high; /* highest value of sp allowed */
60 * 1 : check for stack overflow for each register pop.
61 * 0 : save overhead if there is plenty of stack remaining.
64 int entries; /* number of entries left to interpret */
65 int byte; /* current byte number in the instructions word */
69 #ifdef CONFIG_THUMB2_KERNEL
79 extern const struct unwind_idx __start_unwind_idx[];
80 static const struct unwind_idx *__origin_unwind_idx;
81 extern const struct unwind_idx __stop_unwind_idx[];
83 static DEFINE_RAW_SPINLOCK(unwind_lock);
84 static LIST_HEAD(unwind_tables);
86 /* Convert a prel31 symbol to an absolute address */
87 #define prel31_to_addr(ptr) \
89 /* sign-extend to 32 bits */ \
90 long offset = (((long)*(ptr)) << 1) >> 1; \
91 (unsigned long)(ptr) + offset; \
95 * Binary search in the unwind index. The entries are
96 * guaranteed to be sorted in ascending order by the linker.
99 * origin = first entry with positive offset (or stop if there is no such entry)
100 * stop - 1 = last entry
102 static const struct unwind_idx *search_index(unsigned long addr,
103 const struct unwind_idx *start,
104 const struct unwind_idx *origin,
105 const struct unwind_idx *stop)
107 unsigned long addr_prel31;
109 pr_debug("%s(%08lx, %p, %p, %p)\n",
110 __func__, addr, start, origin, stop);
113 * only search in the section with the matching sign. This way the
114 * prel31 numbers can be compared as unsigned longs.
116 if (addr < (unsigned long)start)
117 /* negative offsets: [start; origin) */
120 /* positive offsets: [origin; stop) */
123 /* prel31 for address relavive to start */
124 addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;
126 while (start < stop - 1) {
127 const struct unwind_idx *mid = start + ((stop - start) >> 1);
130 * As addr_prel31 is relative to start an offset is needed to
131 * make it relative to mid.
133 if (addr_prel31 - ((unsigned long)mid - (unsigned long)start) <
137 /* keep addr_prel31 relative to start */
138 addr_prel31 -= ((unsigned long)mid -
139 (unsigned long)start);
144 if (likely(start->addr_offset <= addr_prel31))
147 pr_warn("unwind: Unknown symbol address %08lx\n", addr);
152 static const struct unwind_idx *unwind_find_origin(
153 const struct unwind_idx *start, const struct unwind_idx *stop)
155 pr_debug("%s(%p, %p)\n", __func__, start, stop);
156 while (start < stop) {
157 const struct unwind_idx *mid = start + ((stop - start) >> 1);
159 if (mid->addr_offset >= 0x40000000)
160 /* negative offset */
163 /* positive offset */
166 pr_debug("%s -> %p\n", __func__, stop);
170 static const struct unwind_idx *unwind_find_idx(unsigned long addr)
172 const struct unwind_idx *idx = NULL;
175 pr_debug("%s(%08lx)\n", __func__, addr);
177 if (core_kernel_text(addr)) {
178 if (unlikely(!__origin_unwind_idx))
179 __origin_unwind_idx =
180 unwind_find_origin(__start_unwind_idx,
183 /* main unwind table */
184 idx = search_index(addr, __start_unwind_idx,
188 /* module unwind tables */
189 struct unwind_table *table;
191 raw_spin_lock_irqsave(&unwind_lock, flags);
192 list_for_each_entry(table, &unwind_tables, list) {
193 if (addr >= table->begin_addr &&
194 addr < table->end_addr) {
195 idx = search_index(addr, table->start,
198 /* Move-to-front to exploit common traces */
199 list_move(&table->list, &unwind_tables);
203 raw_spin_unlock_irqrestore(&unwind_lock, flags);
206 pr_debug("%s: idx = %p\n", __func__, idx);
210 static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
214 if (ctrl->entries <= 0) {
215 pr_warn("unwind: Corrupt unwind table\n");
219 ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
221 if (ctrl->byte == 0) {
231 /* Before poping a register check whether it is feasible or not */
232 static int unwind_pop_register(struct unwind_ctrl_block *ctrl,
233 unsigned long **vsp, unsigned int reg)
235 if (unlikely(ctrl->check_each_pop))
236 if (*vsp >= (unsigned long *)ctrl->sp_high)
239 ctrl->vrs[reg] = *(*vsp)++;
243 /* Helper functions to execute the instructions */
244 static int unwind_exec_pop_subset_r4_to_r13(struct unwind_ctrl_block *ctrl,
247 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
248 int load_sp, reg = 4;
250 load_sp = mask & (1 << (13 - 4));
253 if (unwind_pop_register(ctrl, &vsp, reg))
259 ctrl->vrs[SP] = (unsigned long)vsp;
264 static int unwind_exec_pop_r4_to_rN(struct unwind_ctrl_block *ctrl,
267 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
270 /* pop R4-R[4+bbb] */
271 for (reg = 4; reg <= 4 + (insn & 7); reg++)
272 if (unwind_pop_register(ctrl, &vsp, reg))
276 if (unwind_pop_register(ctrl, &vsp, 14))
279 ctrl->vrs[SP] = (unsigned long)vsp;
284 static int unwind_exec_pop_subset_r0_to_r3(struct unwind_ctrl_block *ctrl,
287 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
290 /* pop R0-R3 according to mask */
293 if (unwind_pop_register(ctrl, &vsp, reg))
298 ctrl->vrs[SP] = (unsigned long)vsp;
303 static unsigned long unwind_decode_uleb128(struct unwind_ctrl_block *ctrl)
305 unsigned long bytes = 0;
307 unsigned long result = 0;
310 * unwind_get_byte() will advance `ctrl` one instruction at a time, so
311 * loop until we get an instruction byte where bit 7 is not set.
313 * Note: This decodes a maximum of 4 bytes to output 28 bits data where
314 * max is 0xfffffff: that will cover a vsp increment of 1073742336, hence
315 * it is sufficient for unwinding the stack.
318 insn = unwind_get_byte(ctrl);
319 result |= (insn & 0x7f) << (bytes * 7);
321 } while (!!(insn & 0x80) && (bytes != sizeof(result)));
327 * Execute the current unwind instruction.
329 static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
331 unsigned long insn = unwind_get_byte(ctrl);
334 pr_debug("%s: insn = %08lx\n", __func__, insn);
336 if ((insn & 0xc0) == 0x00)
337 ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
338 else if ((insn & 0xc0) == 0x40)
339 ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
340 else if ((insn & 0xf0) == 0x80) {
343 insn = (insn << 8) | unwind_get_byte(ctrl);
344 mask = insn & 0x0fff;
346 pr_warn("unwind: 'Refuse to unwind' instruction %04lx\n",
351 ret = unwind_exec_pop_subset_r4_to_r13(ctrl, mask);
354 } else if ((insn & 0xf0) == 0x90 &&
355 (insn & 0x0d) != 0x0d)
356 ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
357 else if ((insn & 0xf0) == 0xa0) {
358 ret = unwind_exec_pop_r4_to_rN(ctrl, insn);
361 } else if (insn == 0xb0) {
362 if (ctrl->vrs[PC] == 0)
363 ctrl->vrs[PC] = ctrl->vrs[LR];
364 /* no further processing */
366 } else if (insn == 0xb1) {
367 unsigned long mask = unwind_get_byte(ctrl);
369 if (mask == 0 || mask & 0xf0) {
370 pr_warn("unwind: Spare encoding %04lx\n",
375 ret = unwind_exec_pop_subset_r0_to_r3(ctrl, mask);
378 } else if (insn == 0xb2) {
379 unsigned long uleb128 = unwind_decode_uleb128(ctrl);
381 ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
383 pr_warn("unwind: Unhandled instruction %02lx\n", insn);
387 pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
388 ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
395 * Unwind a single frame starting with *sp for the symbol at *pc. It
396 * updates the *pc and *sp with the new values.
398 int unwind_frame(struct stackframe *frame)
401 const struct unwind_idx *idx;
402 struct unwind_ctrl_block ctrl;
404 /* store the highest address on the stack to avoid crossing it*/
406 ctrl.sp_high = ALIGN(low, THREAD_SIZE);
408 pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
409 frame->pc, frame->lr, frame->sp);
411 if (!kernel_text_address(frame->pc))
414 idx = unwind_find_idx(frame->pc);
416 pr_warn("unwind: Index not found %08lx\n", frame->pc);
420 ctrl.vrs[FP] = frame->fp;
421 ctrl.vrs[SP] = frame->sp;
422 ctrl.vrs[LR] = frame->lr;
428 else if ((idx->insn & 0x80000000) == 0)
429 /* prel31 to the unwind table */
430 ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
431 else if ((idx->insn & 0xff000000) == 0x80000000)
432 /* only personality routine 0 supported in the index */
433 ctrl.insn = &idx->insn;
435 pr_warn("unwind: Unsupported personality routine %08lx in the index at %p\n",
440 /* check the personality routine */
441 if ((*ctrl.insn & 0xff000000) == 0x80000000) {
444 } else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
446 ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
448 pr_warn("unwind: Unsupported personality routine %08lx at %p\n",
449 *ctrl.insn, ctrl.insn);
453 ctrl.check_each_pop = 0;
455 while (ctrl.entries > 0) {
457 if ((ctrl.sp_high - ctrl.vrs[SP]) < sizeof(ctrl.vrs))
458 ctrl.check_each_pop = 1;
459 urc = unwind_exec_insn(&ctrl);
462 if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= ctrl.sp_high)
466 if (ctrl.vrs[PC] == 0)
467 ctrl.vrs[PC] = ctrl.vrs[LR];
469 /* check for infinite loop */
470 if (frame->pc == ctrl.vrs[PC])
473 frame->fp = ctrl.vrs[FP];
474 frame->sp = ctrl.vrs[SP];
475 frame->lr = ctrl.vrs[LR];
476 frame->pc = ctrl.vrs[PC];
481 void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
483 struct stackframe frame;
485 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
491 arm_get_current_stackframe(regs, &frame);
492 /* PC might be corrupted, use LR in that case. */
493 if (!kernel_text_address(regs->ARM_pc))
494 frame.pc = regs->ARM_lr;
495 } else if (tsk == current) {
496 frame.fp = (unsigned long)__builtin_frame_address(0);
497 frame.sp = current_stack_pointer;
498 frame.lr = (unsigned long)__builtin_return_address(0);
499 frame.pc = (unsigned long)unwind_backtrace;
501 /* task blocked in __switch_to */
502 frame.fp = thread_saved_fp(tsk);
503 frame.sp = thread_saved_sp(tsk);
505 * The function calling __switch_to cannot be a leaf function
506 * so LR is recovered from the stack.
509 frame.pc = thread_saved_pc(tsk);
514 unsigned long where = frame.pc;
516 urc = unwind_frame(&frame);
519 dump_backtrace_entry(where, frame.pc, frame.sp - 4);
523 struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
524 unsigned long text_addr,
525 unsigned long text_size)
528 struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
530 pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
531 text_addr, text_size);
536 tab->start = (const struct unwind_idx *)start;
537 tab->stop = (const struct unwind_idx *)(start + size);
538 tab->origin = unwind_find_origin(tab->start, tab->stop);
539 tab->begin_addr = text_addr;
540 tab->end_addr = text_addr + text_size;
542 raw_spin_lock_irqsave(&unwind_lock, flags);
543 list_add_tail(&tab->list, &unwind_tables);
544 raw_spin_unlock_irqrestore(&unwind_lock, flags);
549 void unwind_table_del(struct unwind_table *tab)
556 raw_spin_lock_irqsave(&unwind_lock, flags);
557 list_del(&tab->list);
558 raw_spin_unlock_irqrestore(&unwind_lock, flags);