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.
22 #endif /* __CHECKER__ */
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/export.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
32 #include <asm/stacktrace.h>
33 #include <asm/traps.h>
34 #include <asm/unwind.h>
36 /* Dummy functions to avoid linker complaints */
37 void __aeabi_unwind_cpp_pr0(void)
40 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);
42 void __aeabi_unwind_cpp_pr1(void)
45 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);
47 void __aeabi_unwind_cpp_pr2(void)
50 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);
52 struct unwind_ctrl_block {
53 unsigned long vrs[16]; /* virtual register set */
54 const unsigned long *insn; /* pointer to the current instructions word */
55 unsigned long sp_high; /* highest value of sp allowed */
57 * 1 : check for stack overflow for each register pop.
58 * 0 : save overhead if there is plenty of stack remaining.
61 int entries; /* number of entries left to interpret */
62 int byte; /* current byte number in the instructions word */
66 #ifdef CONFIG_THUMB2_KERNEL
76 extern const struct unwind_idx __start_unwind_idx[];
77 static const struct unwind_idx *__origin_unwind_idx;
78 extern const struct unwind_idx __stop_unwind_idx[];
80 static DEFINE_RAW_SPINLOCK(unwind_lock);
81 static LIST_HEAD(unwind_tables);
83 /* Convert a prel31 symbol to an absolute address */
84 #define prel31_to_addr(ptr) \
86 /* sign-extend to 32 bits */ \
87 long offset = (((long)*(ptr)) << 1) >> 1; \
88 (unsigned long)(ptr) + offset; \
92 * Binary search in the unwind index. The entries are
93 * guaranteed to be sorted in ascending order by the linker.
96 * origin = first entry with positive offset (or stop if there is no such entry)
97 * stop - 1 = last entry
99 static const struct unwind_idx *search_index(unsigned long addr,
100 const struct unwind_idx *start,
101 const struct unwind_idx *origin,
102 const struct unwind_idx *stop)
104 unsigned long addr_prel31;
106 pr_debug("%s(%08lx, %p, %p, %p)\n",
107 __func__, addr, start, origin, stop);
110 * only search in the section with the matching sign. This way the
111 * prel31 numbers can be compared as unsigned longs.
113 if (addr < (unsigned long)start)
114 /* negative offsets: [start; origin) */
117 /* positive offsets: [origin; stop) */
120 /* prel31 for address relavive to start */
121 addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;
123 while (start < stop - 1) {
124 const struct unwind_idx *mid = start + ((stop - start) >> 1);
127 * As addr_prel31 is relative to start an offset is needed to
128 * make it relative to mid.
130 if (addr_prel31 - ((unsigned long)mid - (unsigned long)start) <
134 /* keep addr_prel31 relative to start */
135 addr_prel31 -= ((unsigned long)mid -
136 (unsigned long)start);
141 if (likely(start->addr_offset <= addr_prel31))
144 pr_warn("unwind: Unknown symbol address %08lx\n", addr);
149 static const struct unwind_idx *unwind_find_origin(
150 const struct unwind_idx *start, const struct unwind_idx *stop)
152 pr_debug("%s(%p, %p)\n", __func__, start, stop);
153 while (start < stop) {
154 const struct unwind_idx *mid = start + ((stop - start) >> 1);
156 if (mid->addr_offset >= 0x40000000)
157 /* negative offset */
160 /* positive offset */
163 pr_debug("%s -> %p\n", __func__, stop);
167 static const struct unwind_idx *unwind_find_idx(unsigned long addr)
169 const struct unwind_idx *idx = NULL;
172 pr_debug("%s(%08lx)\n", __func__, addr);
174 if (core_kernel_text(addr)) {
175 if (unlikely(!__origin_unwind_idx))
176 __origin_unwind_idx =
177 unwind_find_origin(__start_unwind_idx,
180 /* main unwind table */
181 idx = search_index(addr, __start_unwind_idx,
185 /* module unwind tables */
186 struct unwind_table *table;
188 raw_spin_lock_irqsave(&unwind_lock, flags);
189 list_for_each_entry(table, &unwind_tables, list) {
190 if (addr >= table->begin_addr &&
191 addr < table->end_addr) {
192 idx = search_index(addr, table->start,
195 /* Move-to-front to exploit common traces */
196 list_move(&table->list, &unwind_tables);
200 raw_spin_unlock_irqrestore(&unwind_lock, flags);
203 pr_debug("%s: idx = %p\n", __func__, idx);
207 static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
211 if (ctrl->entries <= 0) {
212 pr_warn("unwind: Corrupt unwind table\n");
216 ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
218 if (ctrl->byte == 0) {
228 /* Before poping a register check whether it is feasible or not */
229 static int unwind_pop_register(struct unwind_ctrl_block *ctrl,
230 unsigned long **vsp, unsigned int reg)
232 if (unlikely(ctrl->check_each_pop))
233 if (*vsp >= (unsigned long *)ctrl->sp_high)
236 /* Use READ_ONCE_NOCHECK here to avoid this memory access
237 * from being tracked by KASAN.
239 ctrl->vrs[reg] = READ_ONCE_NOCHECK(*(*vsp));
244 /* Helper functions to execute the instructions */
245 static int unwind_exec_pop_subset_r4_to_r13(struct unwind_ctrl_block *ctrl,
248 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
249 int load_sp, reg = 4;
251 load_sp = mask & (1 << (13 - 4));
254 if (unwind_pop_register(ctrl, &vsp, reg))
260 ctrl->vrs[SP] = (unsigned long)vsp;
265 static int unwind_exec_pop_r4_to_rN(struct unwind_ctrl_block *ctrl,
268 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
271 /* pop R4-R[4+bbb] */
272 for (reg = 4; reg <= 4 + (insn & 7); reg++)
273 if (unwind_pop_register(ctrl, &vsp, reg))
277 if (unwind_pop_register(ctrl, &vsp, 14))
280 ctrl->vrs[SP] = (unsigned long)vsp;
285 static int unwind_exec_pop_subset_r0_to_r3(struct unwind_ctrl_block *ctrl,
288 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
291 /* pop R0-R3 according to mask */
294 if (unwind_pop_register(ctrl, &vsp, reg))
299 ctrl->vrs[SP] = (unsigned long)vsp;
305 * Execute the current unwind instruction.
307 static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
309 unsigned long insn = unwind_get_byte(ctrl);
312 pr_debug("%s: insn = %08lx\n", __func__, insn);
314 if ((insn & 0xc0) == 0x00)
315 ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
316 else if ((insn & 0xc0) == 0x40)
317 ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
318 else if ((insn & 0xf0) == 0x80) {
321 insn = (insn << 8) | unwind_get_byte(ctrl);
322 mask = insn & 0x0fff;
324 pr_warn("unwind: 'Refuse to unwind' instruction %04lx\n",
329 ret = unwind_exec_pop_subset_r4_to_r13(ctrl, mask);
332 } else if ((insn & 0xf0) == 0x90 &&
333 (insn & 0x0d) != 0x0d)
334 ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
335 else if ((insn & 0xf0) == 0xa0) {
336 ret = unwind_exec_pop_r4_to_rN(ctrl, insn);
339 } else if (insn == 0xb0) {
340 if (ctrl->vrs[PC] == 0)
341 ctrl->vrs[PC] = ctrl->vrs[LR];
342 /* no further processing */
344 } else if (insn == 0xb1) {
345 unsigned long mask = unwind_get_byte(ctrl);
347 if (mask == 0 || mask & 0xf0) {
348 pr_warn("unwind: Spare encoding %04lx\n",
353 ret = unwind_exec_pop_subset_r0_to_r3(ctrl, mask);
356 } else if (insn == 0xb2) {
357 unsigned long uleb128 = unwind_get_byte(ctrl);
359 ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
361 pr_warn("unwind: Unhandled instruction %02lx\n", insn);
365 pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
366 ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
373 * Unwind a single frame starting with *sp for the symbol at *pc. It
374 * updates the *pc and *sp with the new values.
376 int unwind_frame(struct stackframe *frame)
379 const struct unwind_idx *idx;
380 struct unwind_ctrl_block ctrl;
382 /* store the highest address on the stack to avoid crossing it*/
384 ctrl.sp_high = ALIGN(low, THREAD_SIZE);
386 pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
387 frame->pc, frame->lr, frame->sp);
389 if (!kernel_text_address(frame->pc))
392 idx = unwind_find_idx(frame->pc);
394 pr_warn("unwind: Index not found %08lx\n", frame->pc);
398 ctrl.vrs[FP] = frame->fp;
399 ctrl.vrs[SP] = frame->sp;
400 ctrl.vrs[LR] = frame->lr;
406 else if ((idx->insn & 0x80000000) == 0)
407 /* prel31 to the unwind table */
408 ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn);
409 else if ((idx->insn & 0xff000000) == 0x80000000)
410 /* only personality routine 0 supported in the index */
411 ctrl.insn = &idx->insn;
413 pr_warn("unwind: Unsupported personality routine %08lx in the index at %p\n",
418 /* check the personality routine */
419 if ((*ctrl.insn & 0xff000000) == 0x80000000) {
422 } else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
424 ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
426 pr_warn("unwind: Unsupported personality routine %08lx at %p\n",
427 *ctrl.insn, ctrl.insn);
431 ctrl.check_each_pop = 0;
433 while (ctrl.entries > 0) {
435 if ((ctrl.sp_high - ctrl.vrs[SP]) < sizeof(ctrl.vrs))
436 ctrl.check_each_pop = 1;
437 urc = unwind_exec_insn(&ctrl);
440 if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= ctrl.sp_high)
444 if (ctrl.vrs[PC] == 0)
445 ctrl.vrs[PC] = ctrl.vrs[LR];
447 /* check for infinite loop */
448 if (frame->pc == ctrl.vrs[PC] && frame->sp == ctrl.vrs[SP])
451 frame->fp = ctrl.vrs[FP];
452 frame->sp = ctrl.vrs[SP];
453 frame->lr = ctrl.vrs[LR];
454 frame->pc = ctrl.vrs[PC];
459 void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk,
462 struct stackframe frame;
464 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
470 arm_get_current_stackframe(regs, &frame);
471 /* PC might be corrupted, use LR in that case. */
472 if (!kernel_text_address(regs->ARM_pc))
473 frame.pc = regs->ARM_lr;
474 } else if (tsk == current) {
475 frame.fp = (unsigned long)__builtin_frame_address(0);
476 frame.sp = current_stack_pointer;
477 frame.lr = (unsigned long)__builtin_return_address(0);
478 frame.pc = (unsigned long)unwind_backtrace;
480 /* task blocked in __switch_to */
481 frame.fp = thread_saved_fp(tsk);
482 frame.sp = thread_saved_sp(tsk);
484 * The function calling __switch_to cannot be a leaf function
485 * so LR is recovered from the stack.
488 frame.pc = thread_saved_pc(tsk);
493 unsigned long where = frame.pc;
495 urc = unwind_frame(&frame);
498 dump_backtrace_entry(where, frame.pc, frame.sp - 4, loglvl);
502 struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
503 unsigned long text_addr,
504 unsigned long text_size)
507 struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
509 pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
510 text_addr, text_size);
515 tab->start = (const struct unwind_idx *)start;
516 tab->stop = (const struct unwind_idx *)(start + size);
517 tab->origin = unwind_find_origin(tab->start, tab->stop);
518 tab->begin_addr = text_addr;
519 tab->end_addr = text_addr + text_size;
521 raw_spin_lock_irqsave(&unwind_lock, flags);
522 list_add_tail(&tab->list, &unwind_tables);
523 raw_spin_unlock_irqrestore(&unwind_lock, flags);
528 void unwind_table_del(struct unwind_table *tab)
535 raw_spin_lock_irqsave(&unwind_lock, flags);
536 list_del(&tab->list);
537 raw_spin_unlock_irqrestore(&unwind_lock, flags);