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>
31 #include <linux/module.h>
33 #include <asm/stacktrace.h>
34 #include <asm/traps.h>
35 #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 */
59 unsigned long *lr_addr
; /* address of LR value on the stack */
61 * 1 : check for stack overflow for each register pop.
62 * 0 : save overhead if there is plenty of stack remaining.
65 int entries
; /* number of entries left to interpret */
66 int byte
; /* current byte number in the instructions word */
70 #ifdef CONFIG_THUMB2_KERNEL
80 extern const struct unwind_idx __start_unwind_idx
[];
81 static const struct unwind_idx
*__origin_unwind_idx
;
82 extern const struct unwind_idx __stop_unwind_idx
[];
84 static DEFINE_RAW_SPINLOCK(unwind_lock
);
85 static LIST_HEAD(unwind_tables
);
87 /* Convert a prel31 symbol to an absolute address */
88 #define prel31_to_addr(ptr) \
90 /* sign-extend to 32 bits */ \
91 long offset = (((long)*(ptr)) << 1) >> 1; \
92 (unsigned long)(ptr) + offset; \
96 * Binary search in the unwind index. The entries are
97 * guaranteed to be sorted in ascending order by the linker.
100 * origin = first entry with positive offset (or stop if there is no such entry)
101 * stop - 1 = last entry
103 static const struct unwind_idx
*search_index(unsigned long addr
,
104 const struct unwind_idx
*start
,
105 const struct unwind_idx
*origin
,
106 const struct unwind_idx
*stop
)
108 unsigned long addr_prel31
;
110 pr_debug("%s(%08lx, %p, %p, %p)\n",
111 __func__
, addr
, start
, origin
, stop
);
114 * only search in the section with the matching sign. This way the
115 * prel31 numbers can be compared as unsigned longs.
117 if (addr
< (unsigned long)start
)
118 /* negative offsets: [start; origin) */
121 /* positive offsets: [origin; stop) */
124 /* prel31 for address relavive to start */
125 addr_prel31
= (addr
- (unsigned long)start
) & 0x7fffffff;
127 while (start
< stop
- 1) {
128 const struct unwind_idx
*mid
= start
+ ((stop
- start
) >> 1);
131 * As addr_prel31 is relative to start an offset is needed to
132 * make it relative to mid.
134 if (addr_prel31
- ((unsigned long)mid
- (unsigned long)start
) <
138 /* keep addr_prel31 relative to start */
139 addr_prel31
-= ((unsigned long)mid
-
140 (unsigned long)start
);
145 if (likely(start
->addr_offset
<= addr_prel31
))
148 pr_warn("unwind: Unknown symbol address %08lx\n", addr
);
153 static const struct unwind_idx
*unwind_find_origin(
154 const struct unwind_idx
*start
, const struct unwind_idx
*stop
)
156 pr_debug("%s(%p, %p)\n", __func__
, start
, stop
);
157 while (start
< stop
) {
158 const struct unwind_idx
*mid
= start
+ ((stop
- start
) >> 1);
160 if (mid
->addr_offset
>= 0x40000000)
161 /* negative offset */
164 /* positive offset */
167 pr_debug("%s -> %p\n", __func__
, stop
);
171 static const struct unwind_idx
*unwind_find_idx(unsigned long addr
)
173 const struct unwind_idx
*idx
= NULL
;
176 pr_debug("%s(%08lx)\n", __func__
, addr
);
178 if (core_kernel_text(addr
)) {
179 if (unlikely(!__origin_unwind_idx
))
180 __origin_unwind_idx
=
181 unwind_find_origin(__start_unwind_idx
,
184 /* main unwind table */
185 idx
= search_index(addr
, __start_unwind_idx
,
189 /* module unwind tables */
190 struct unwind_table
*table
;
192 raw_spin_lock_irqsave(&unwind_lock
, flags
);
193 list_for_each_entry(table
, &unwind_tables
, list
) {
194 if (addr
>= table
->begin_addr
&&
195 addr
< table
->end_addr
) {
196 idx
= search_index(addr
, table
->start
,
199 /* Move-to-front to exploit common traces */
200 list_move(&table
->list
, &unwind_tables
);
204 raw_spin_unlock_irqrestore(&unwind_lock
, flags
);
207 pr_debug("%s: idx = %p\n", __func__
, idx
);
211 static unsigned long unwind_get_byte(struct unwind_ctrl_block
*ctrl
)
215 if (ctrl
->entries
<= 0) {
216 pr_warn("unwind: Corrupt unwind table\n");
220 ret
= (*ctrl
->insn
>> (ctrl
->byte
* 8)) & 0xff;
222 if (ctrl
->byte
== 0) {
232 /* Before poping a register check whether it is feasible or not */
233 static int unwind_pop_register(struct unwind_ctrl_block
*ctrl
,
234 unsigned long **vsp
, unsigned int reg
)
236 if (unlikely(ctrl
->check_each_pop
))
237 if (*vsp
>= (unsigned long *)ctrl
->sp_high
)
240 /* Use READ_ONCE_NOCHECK here to avoid this memory access
241 * from being tracked by KASAN.
243 ctrl
->vrs
[reg
] = READ_ONCE_NOCHECK(*(*vsp
));
245 ctrl
->lr_addr
= *vsp
;
250 /* Helper functions to execute the instructions */
251 static int unwind_exec_pop_subset_r4_to_r13(struct unwind_ctrl_block
*ctrl
,
254 unsigned long *vsp
= (unsigned long *)ctrl
->vrs
[SP
];
255 int load_sp
, reg
= 4;
257 load_sp
= mask
& (1 << (13 - 4));
260 if (unwind_pop_register(ctrl
, &vsp
, reg
))
266 ctrl
->vrs
[SP
] = (unsigned long)vsp
;
272 static int unwind_exec_pop_r4_to_rN(struct unwind_ctrl_block
*ctrl
,
275 unsigned long *vsp
= (unsigned long *)ctrl
->vrs
[SP
];
278 /* pop R4-R[4+bbb] */
279 for (reg
= 4; reg
<= 4 + (insn
& 7); reg
++)
280 if (unwind_pop_register(ctrl
, &vsp
, reg
))
284 if (unwind_pop_register(ctrl
, &vsp
, 14))
287 ctrl
->vrs
[SP
] = (unsigned long)vsp
;
292 static int unwind_exec_pop_subset_r0_to_r3(struct unwind_ctrl_block
*ctrl
,
295 unsigned long *vsp
= (unsigned long *)ctrl
->vrs
[SP
];
298 /* pop R0-R3 according to mask */
301 if (unwind_pop_register(ctrl
, &vsp
, reg
))
306 ctrl
->vrs
[SP
] = (unsigned long)vsp
;
311 static unsigned long unwind_decode_uleb128(struct unwind_ctrl_block
*ctrl
)
313 unsigned long bytes
= 0;
315 unsigned long result
= 0;
318 * unwind_get_byte() will advance `ctrl` one instruction at a time, so
319 * loop until we get an instruction byte where bit 7 is not set.
321 * Note: This decodes a maximum of 4 bytes to output 28 bits data where
322 * max is 0xfffffff: that will cover a vsp increment of 1073742336, hence
323 * it is sufficient for unwinding the stack.
326 insn
= unwind_get_byte(ctrl
);
327 result
|= (insn
& 0x7f) << (bytes
* 7);
329 } while (!!(insn
& 0x80) && (bytes
!= sizeof(result
)));
335 * Execute the current unwind instruction.
337 static int unwind_exec_insn(struct unwind_ctrl_block
*ctrl
)
339 unsigned long insn
= unwind_get_byte(ctrl
);
342 pr_debug("%s: insn = %08lx\n", __func__
, insn
);
344 if ((insn
& 0xc0) == 0x00)
345 ctrl
->vrs
[SP
] += ((insn
& 0x3f) << 2) + 4;
346 else if ((insn
& 0xc0) == 0x40) {
347 ctrl
->vrs
[SP
] -= ((insn
& 0x3f) << 2) + 4;
348 } else if ((insn
& 0xf0) == 0x80) {
351 insn
= (insn
<< 8) | unwind_get_byte(ctrl
);
352 mask
= insn
& 0x0fff;
354 pr_warn("unwind: 'Refuse to unwind' instruction %04lx\n",
359 ret
= unwind_exec_pop_subset_r4_to_r13(ctrl
, mask
);
362 } else if ((insn
& 0xf0) == 0x90 &&
363 (insn
& 0x0d) != 0x0d) {
364 ctrl
->vrs
[SP
] = ctrl
->vrs
[insn
& 0x0f];
365 } else if ((insn
& 0xf0) == 0xa0) {
366 ret
= unwind_exec_pop_r4_to_rN(ctrl
, insn
);
369 } else if (insn
== 0xb0) {
370 if (ctrl
->vrs
[PC
] == 0)
371 ctrl
->vrs
[PC
] = ctrl
->vrs
[LR
];
372 /* no further processing */
374 } else if (insn
== 0xb1) {
375 unsigned long mask
= unwind_get_byte(ctrl
);
377 if (mask
== 0 || mask
& 0xf0) {
378 pr_warn("unwind: Spare encoding %04lx\n",
383 ret
= unwind_exec_pop_subset_r0_to_r3(ctrl
, mask
);
386 } else if (insn
== 0xb2) {
387 unsigned long uleb128
= unwind_decode_uleb128(ctrl
);
389 ctrl
->vrs
[SP
] += 0x204 + (uleb128
<< 2);
391 pr_warn("unwind: Unhandled instruction %02lx\n", insn
);
395 pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__
,
396 ctrl
->vrs
[FP
], ctrl
->vrs
[SP
], ctrl
->vrs
[LR
], ctrl
->vrs
[PC
]);
403 * Unwind a single frame starting with *sp for the symbol at *pc. It
404 * updates the *pc and *sp with the new values.
406 int unwind_frame(struct stackframe
*frame
)
408 const struct unwind_idx
*idx
;
409 struct unwind_ctrl_block ctrl
;
410 unsigned long sp_low
;
412 /* store the highest address on the stack to avoid crossing it*/
414 ctrl
.sp_high
= ALIGN(sp_low
- THREAD_SIZE
, THREAD_ALIGN
)
417 pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__
,
418 frame
->pc
, frame
->lr
, frame
->sp
);
420 idx
= unwind_find_idx(frame
->pc
);
422 if (frame
->pc
&& kernel_text_address(frame
->pc
)) {
423 if (in_module_plt(frame
->pc
) && frame
->pc
!= frame
->lr
) {
425 * Quoting Ard: Veneers only set PC using a
426 * PC+immediate LDR, and so they don't affect
427 * the state of the stack or the register file
429 frame
->pc
= frame
->lr
;
432 pr_warn("unwind: Index not found %08lx\n", frame
->pc
);
437 ctrl
.vrs
[FP
] = frame
->fp
;
438 ctrl
.vrs
[SP
] = frame
->sp
;
439 ctrl
.vrs
[LR
] = frame
->lr
;
445 else if (frame
->pc
== prel31_to_addr(&idx
->addr_offset
)) {
447 * Unwinding is tricky when we're halfway through the prologue,
448 * since the stack frame that the unwinder expects may not be
449 * fully set up yet. However, one thing we do know for sure is
450 * that if we are unwinding from the very first instruction of
451 * a function, we are still effectively in the stack frame of
452 * the caller, and the unwind info has no relevance yet.
454 if (frame
->pc
== frame
->lr
)
456 frame
->pc
= frame
->lr
;
458 } else if ((idx
->insn
& 0x80000000) == 0)
459 /* prel31 to the unwind table */
460 ctrl
.insn
= (unsigned long *)prel31_to_addr(&idx
->insn
);
461 else if ((idx
->insn
& 0xff000000) == 0x80000000)
462 /* only personality routine 0 supported in the index */
463 ctrl
.insn
= &idx
->insn
;
465 pr_warn("unwind: Unsupported personality routine %08lx in the index at %p\n",
470 /* check the personality routine */
471 if ((*ctrl
.insn
& 0xff000000) == 0x80000000) {
474 } else if ((*ctrl
.insn
& 0xff000000) == 0x81000000) {
476 ctrl
.entries
= 1 + ((*ctrl
.insn
& 0x00ff0000) >> 16);
478 pr_warn("unwind: Unsupported personality routine %08lx at %p\n",
479 *ctrl
.insn
, ctrl
.insn
);
483 ctrl
.check_each_pop
= 0;
485 if (prel31_to_addr(&idx
->addr_offset
) == (u32
)&call_with_stack
) {
487 * call_with_stack() is the only place where we permit SP to
488 * jump from one stack to another, and since we know it is
489 * guaranteed to happen, set up the SP bounds accordingly.
492 ctrl
.sp_high
= ALIGN(frame
->fp
, THREAD_SIZE
);
495 while (ctrl
.entries
> 0) {
497 if ((ctrl
.sp_high
- ctrl
.vrs
[SP
]) < sizeof(ctrl
.vrs
))
498 ctrl
.check_each_pop
= 1;
499 urc
= unwind_exec_insn(&ctrl
);
502 if (ctrl
.vrs
[SP
] < sp_low
|| ctrl
.vrs
[SP
] > ctrl
.sp_high
)
506 if (ctrl
.vrs
[PC
] == 0)
507 ctrl
.vrs
[PC
] = ctrl
.vrs
[LR
];
509 /* check for infinite loop */
510 if (frame
->pc
== ctrl
.vrs
[PC
] && frame
->sp
== ctrl
.vrs
[SP
])
513 frame
->fp
= ctrl
.vrs
[FP
];
514 frame
->sp
= ctrl
.vrs
[SP
];
515 frame
->lr
= ctrl
.vrs
[LR
];
516 frame
->pc
= ctrl
.vrs
[PC
];
517 frame
->lr_addr
= ctrl
.lr_addr
;
522 void unwind_backtrace(struct pt_regs
*regs
, struct task_struct
*tsk
,
525 struct stackframe frame
;
527 printk("%sCall trace: ", loglvl
);
529 pr_debug("%s(regs = %p tsk = %p)\n", __func__
, regs
, tsk
);
535 arm_get_current_stackframe(regs
, &frame
);
536 /* PC might be corrupted, use LR in that case. */
537 if (!kernel_text_address(regs
->ARM_pc
))
538 frame
.pc
= regs
->ARM_lr
;
539 } else if (tsk
== current
) {
540 frame
.fp
= (unsigned long)__builtin_frame_address(0);
541 frame
.sp
= current_stack_pointer
;
542 frame
.lr
= (unsigned long)__builtin_return_address(0);
543 /* We are saving the stack and execution state at this
544 * point, so we should ensure that frame.pc is within
545 * this block of code.
548 frame
.pc
= (unsigned long)&&here
;
550 /* task blocked in __switch_to */
551 frame
.fp
= thread_saved_fp(tsk
);
552 frame
.sp
= thread_saved_sp(tsk
);
554 * The function calling __switch_to cannot be a leaf function
555 * so LR is recovered from the stack.
558 frame
.pc
= thread_saved_pc(tsk
);
563 unsigned long where
= frame
.pc
;
565 urc
= unwind_frame(&frame
);
568 dump_backtrace_entry(where
, frame
.pc
, frame
.sp
- 4, loglvl
);
572 struct unwind_table
*unwind_table_add(unsigned long start
, unsigned long size
,
573 unsigned long text_addr
,
574 unsigned long text_size
)
577 struct unwind_table
*tab
= kmalloc(sizeof(*tab
), GFP_KERNEL
);
579 pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__
, start
, size
,
580 text_addr
, text_size
);
585 tab
->start
= (const struct unwind_idx
*)start
;
586 tab
->stop
= (const struct unwind_idx
*)(start
+ size
);
587 tab
->origin
= unwind_find_origin(tab
->start
, tab
->stop
);
588 tab
->begin_addr
= text_addr
;
589 tab
->end_addr
= text_addr
+ text_size
;
591 raw_spin_lock_irqsave(&unwind_lock
, flags
);
592 list_add_tail(&tab
->list
, &unwind_tables
);
593 raw_spin_unlock_irqrestore(&unwind_lock
, flags
);
598 void unwind_table_del(struct unwind_table
*tab
)
605 raw_spin_lock_irqsave(&unwind_lock
, flags
);
606 list_del(&tab
->list
);
607 raw_spin_unlock_irqrestore(&unwind_lock
, flags
);