blob: fb1b9bead242140034890b8d89c5cad8d12d96dc [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * arch/arm/kernel/unwind.c
4 *
5 * Copyright (C) 2008 ARM Limited
6 *
7 * Stack unwinding support for ARM
8 *
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:
12 *
13 * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html
14 */
15
16#ifndef __CHECKER__
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.
24#endif
25#endif /* __CHECKER__ */
26
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>
34
35#include <asm/stacktrace.h>
36#include <asm/traps.h>
37#include <asm/unwind.h>
38
39/* Dummy functions to avoid linker complaints */
40void __aeabi_unwind_cpp_pr0(void)
41{
42};
43EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);
44
45void __aeabi_unwind_cpp_pr1(void)
46{
47};
48EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);
49
50void __aeabi_unwind_cpp_pr2(void)
51{
52};
53EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);
54
55struct 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 */
60 /*
61 * 1 : check for stack overflow for each register pop.
62 * 0 : save overhead if there is plenty of stack remaining.
63 */
64 int check_each_pop;
65 int entries; /* number of entries left to interpret */
66 int byte; /* current byte number in the instructions word */
67};
68
69enum regs {
70#ifdef CONFIG_THUMB2_KERNEL
71 FP = 7,
72#else
73 FP = 11,
74#endif
75 SP = 13,
76 LR = 14,
77 PC = 15
78};
79
80extern const struct unwind_idx __start_unwind_idx[];
81static const struct unwind_idx *__origin_unwind_idx;
82extern const struct unwind_idx __stop_unwind_idx[];
83
84static DEFINE_RAW_SPINLOCK(unwind_lock);
85static LIST_HEAD(unwind_tables);
86
87/* Convert a prel31 symbol to an absolute address */
88#define prel31_to_addr(ptr) \
89({ \
90 /* sign-extend to 32 bits */ \
91 long offset = (((long)*(ptr)) << 1) >> 1; \
92 (unsigned long)(ptr) + offset; \
93})
94
95/*
96 * Binary search in the unwind index. The entries are
97 * guaranteed to be sorted in ascending order by the linker.
98 *
99 * start = first entry
100 * origin = first entry with positive offset (or stop if there is no such entry)
101 * stop - 1 = last entry
102 */
103static 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)
107{
108 unsigned long addr_prel31;
109
110 pr_debug("%s(%08lx, %p, %p, %p)\n",
111 __func__, addr, start, origin, stop);
112
113 /*
114 * only search in the section with the matching sign. This way the
115 * prel31 numbers can be compared as unsigned longs.
116 */
117 if (addr < (unsigned long)start)
118 /* negative offsets: [start; origin) */
119 stop = origin;
120 else
121 /* positive offsets: [origin; stop) */
122 start = origin;
123
124 /* prel31 for address relavive to start */
125 addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;
126
127 while (start < stop - 1) {
128 const struct unwind_idx *mid = start + ((stop - start) >> 1);
129
130 /*
131 * As addr_prel31 is relative to start an offset is needed to
132 * make it relative to mid.
133 */
134 if (addr_prel31 - ((unsigned long)mid - (unsigned long)start) <
135 mid->addr_offset)
136 stop = mid;
137 else {
138 /* keep addr_prel31 relative to start */
139 addr_prel31 -= ((unsigned long)mid -
140 (unsigned long)start);
141 start = mid;
142 }
143 }
144
145 if (likely(start->addr_offset <= addr_prel31))
146 return start;
147 else {
148 pr_warn("unwind: Unknown symbol address %08lx\n", addr);
149 return NULL;
150 }
151}
152
153static const struct unwind_idx *unwind_find_origin(
154 const struct unwind_idx *start, const struct unwind_idx *stop)
155{
156 pr_debug("%s(%p, %p)\n", __func__, start, stop);
157 while (start < stop) {
158 const struct unwind_idx *mid = start + ((stop - start) >> 1);
159
160 if (mid->addr_offset >= 0x40000000)
161 /* negative offset */
162 start = mid + 1;
163 else
164 /* positive offset */
165 stop = mid;
166 }
167 pr_debug("%s -> %p\n", __func__, stop);
168 return stop;
169}
170
171static const struct unwind_idx *unwind_find_idx(unsigned long addr)
172{
173 const struct unwind_idx *idx = NULL;
174 unsigned long flags;
175
176 pr_debug("%s(%08lx)\n", __func__, addr);
177
178 if (core_kernel_text(addr)) {
179 if (unlikely(!__origin_unwind_idx))
180 __origin_unwind_idx =
181 unwind_find_origin(__start_unwind_idx,
182 __stop_unwind_idx);
183
184 /* main unwind table */
185 idx = search_index(addr, __start_unwind_idx,
186 __origin_unwind_idx,
187 __stop_unwind_idx);
188 } else {
189 /* module unwind tables */
190 struct unwind_table *table;
191
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,
197 table->origin,
198 table->stop);
199 /* Move-to-front to exploit common traces */
200 list_move(&table->list, &unwind_tables);
201 break;
202 }
203 }
204 raw_spin_unlock_irqrestore(&unwind_lock, flags);
205 }
206
207 pr_debug("%s: idx = %p\n", __func__, idx);
208 return idx;
209}
210
211static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl)
212{
213 unsigned long ret;
214
215 if (ctrl->entries <= 0) {
216 pr_warn("unwind: Corrupt unwind table\n");
217 return 0;
218 }
219
220 ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
221
222 if (ctrl->byte == 0) {
223 ctrl->insn++;
224 ctrl->entries--;
225 ctrl->byte = 3;
226 } else
227 ctrl->byte--;
228
229 return ret;
230}
231
232/* Before poping a register check whether it is feasible or not */
233static int unwind_pop_register(struct unwind_ctrl_block *ctrl,
234 unsigned long **vsp, unsigned int reg)
235{
236 if (unlikely(ctrl->check_each_pop))
237 if (*vsp >= (unsigned long *)ctrl->sp_high)
238 return -URC_FAILURE;
239
240 ctrl->vrs[reg] = *(*vsp)++;
241 if (reg == 14)
242 ctrl->lr_addr = *vsp;
243 return URC_OK;
244}
245
246/* Helper functions to execute the instructions */
247static int unwind_exec_pop_subset_r4_to_r13(struct unwind_ctrl_block *ctrl,
248 unsigned long mask)
249{
250 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
251 int load_sp, reg = 4;
252
253 load_sp = mask & (1 << (13 - 4));
254 while (mask) {
255 if (mask & 1)
256 if (unwind_pop_register(ctrl, &vsp, reg))
257 return -URC_FAILURE;
258 mask >>= 1;
259 reg++;
260 }
261 if (!load_sp)
262 ctrl->vrs[SP] = (unsigned long)vsp;
263
264 return URC_OK;
265}
266
267static int unwind_exec_pop_r4_to_rN(struct unwind_ctrl_block *ctrl,
268 unsigned long insn)
269{
270 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
271 int reg;
272
273 /* pop R4-R[4+bbb] */
274 for (reg = 4; reg <= 4 + (insn & 7); reg++)
275 if (unwind_pop_register(ctrl, &vsp, reg))
276 return -URC_FAILURE;
277
278 if (insn & 0x8)
279 if (unwind_pop_register(ctrl, &vsp, 14))
280 return -URC_FAILURE;
281
282 ctrl->vrs[SP] = (unsigned long)vsp;
283
284 return URC_OK;
285}
286
287static int unwind_exec_pop_subset_r0_to_r3(struct unwind_ctrl_block *ctrl,
288 unsigned long mask)
289{
290 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP];
291 int reg = 0;
292
293 /* pop R0-R3 according to mask */
294 while (mask) {
295 if (mask & 1)
296 if (unwind_pop_register(ctrl, &vsp, reg))
297 return -URC_FAILURE;
298 mask >>= 1;
299 reg++;
300 }
301 ctrl->vrs[SP] = (unsigned long)vsp;
302
303 return URC_OK;
304}
305
306static unsigned long unwind_decode_uleb128(struct unwind_ctrl_block *ctrl)
307{
308 unsigned long bytes = 0;
309 unsigned long insn;
310 unsigned long result = 0;
311
312 /*
313 * unwind_get_byte() will advance `ctrl` one instruction at a time, so
314 * loop until we get an instruction byte where bit 7 is not set.
315 *
316 * Note: This decodes a maximum of 4 bytes to output 28 bits data where
317 * max is 0xfffffff: that will cover a vsp increment of 1073742336, hence
318 * it is sufficient for unwinding the stack.
319 */
320 do {
321 insn = unwind_get_byte(ctrl);
322 result |= (insn & 0x7f) << (bytes * 7);
323 bytes++;
324 } while (!!(insn & 0x80) && (bytes != sizeof(result)));
325
326 return result;
327}
328
329/*
330 * Execute the current unwind instruction.
331 */
332static int unwind_exec_insn(struct unwind_ctrl_block *ctrl)
333{
334 unsigned long insn = unwind_get_byte(ctrl);
335 int ret = URC_OK;
336
337 pr_debug("%s: insn = %08lx\n", __func__, insn);
338
339 if ((insn & 0xc0) == 0x00)
340 ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;
341 else if ((insn & 0xc0) == 0x40)
342 ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4;
343 else if ((insn & 0xf0) == 0x80) {
344 unsigned long mask;
345
346 insn = (insn << 8) | unwind_get_byte(ctrl);
347 mask = insn & 0x0fff;
348 if (mask == 0) {
349 pr_warn("unwind: 'Refuse to unwind' instruction %04lx\n",
350 insn);
351 return -URC_FAILURE;
352 }
353
354 ret = unwind_exec_pop_subset_r4_to_r13(ctrl, mask);
355 if (ret)
356 goto error;
357 } else if ((insn & 0xf0) == 0x90 &&
358 (insn & 0x0d) != 0x0d)
359 ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f];
360 else if ((insn & 0xf0) == 0xa0) {
361 ret = unwind_exec_pop_r4_to_rN(ctrl, insn);
362 if (ret)
363 goto error;
364 } else if (insn == 0xb0) {
365 if (ctrl->vrs[PC] == 0)
366 ctrl->vrs[PC] = ctrl->vrs[LR];
367 /* no further processing */
368 ctrl->entries = 0;
369 } else if (insn == 0xb1) {
370 unsigned long mask = unwind_get_byte(ctrl);
371
372 if (mask == 0 || mask & 0xf0) {
373 pr_warn("unwind: Spare encoding %04lx\n",
374 (insn << 8) | mask);
375 return -URC_FAILURE;
376 }
377
378 ret = unwind_exec_pop_subset_r0_to_r3(ctrl, mask);
379 if (ret)
380 goto error;
381 } else if (insn == 0xb2) {
382 unsigned long uleb128 = unwind_decode_uleb128(ctrl);
383
384 ctrl->vrs[SP] += 0x204 + (uleb128 << 2);
385 } else {
386 pr_warn("unwind: Unhandled instruction %02lx\n", insn);
387 return -URC_FAILURE;
388 }
389
390 pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__,
391 ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]);
392
393error:
394 return ret;
395}
396
397/*
398 * Unwind a single frame starting with *sp for the symbol at *pc. It
399 * updates the *pc and *sp with the new values.
400 */
401int unwind_frame(struct stackframe *frame)
402{
403 unsigned long low;
404 const struct unwind_idx *idx;
405 struct unwind_ctrl_block ctrl;
406
407 /* store the highest address on the stack to avoid crossing it*/
408 low = frame->sp;
409 ctrl.sp_high = ALIGN(low, THREAD_SIZE);
410
411 pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__,
412 frame->pc, frame->lr, frame->sp);
413
414 idx = unwind_find_idx(frame->pc);
415 if (!idx) {
416 pr_warn("unwind: Index not found %08lx\n", frame->pc);
417 return -URC_FAILURE;
418 }
419
420 ctrl.vrs[FP] = frame->fp;
421 ctrl.vrs[SP] = frame->sp;
422 ctrl.vrs[LR] = frame->lr;
423 ctrl.vrs[PC] = 0;
424
425 if (idx->insn == 1)
426 /* can't unwind */
427 return -URC_FAILURE;
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;
434 else {
435 pr_warn("unwind: Unsupported personality routine %08lx in the index at %p\n",
436 idx->insn, idx);
437 return -URC_FAILURE;
438 }
439
440 /* check the personality routine */
441 if ((*ctrl.insn & 0xff000000) == 0x80000000) {
442 ctrl.byte = 2;
443 ctrl.entries = 1;
444 } else if ((*ctrl.insn & 0xff000000) == 0x81000000) {
445 ctrl.byte = 1;
446 ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16);
447 } else {
448 pr_warn("unwind: Unsupported personality routine %08lx at %p\n",
449 *ctrl.insn, ctrl.insn);
450 return -URC_FAILURE;
451 }
452
453 ctrl.check_each_pop = 0;
454
455 while (ctrl.entries > 0) {
456 int urc;
457 if ((ctrl.sp_high - ctrl.vrs[SP]) < sizeof(ctrl.vrs))
458 ctrl.check_each_pop = 1;
459 urc = unwind_exec_insn(&ctrl);
460 if (urc < 0)
461 return urc;
462 if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= ctrl.sp_high)
463 return -URC_FAILURE;
464 }
465
466 if (ctrl.vrs[PC] == 0)
467 ctrl.vrs[PC] = ctrl.vrs[LR];
468
469 /* check for infinite loop */
470 if (frame->pc == ctrl.vrs[PC])
471 return -URC_FAILURE;
472
473 frame->fp = ctrl.vrs[FP];
474 frame->sp = ctrl.vrs[SP];
475 frame->lr = ctrl.vrs[LR];
476 frame->pc = ctrl.vrs[PC];
477 frame->lr_addr = ctrl.lr_addr;
478
479 return URC_OK;
480}
481
482void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
483{
484 struct stackframe frame;
485
486 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
487
488 if (!tsk)
489 tsk = current;
490
491 if (regs) {
492 arm_get_current_stackframe(regs, &frame);
493 /* PC might be corrupted, use LR in that case. */
494 if (!kernel_text_address(regs->ARM_pc))
495 frame.pc = regs->ARM_lr;
496 } else if (tsk == current) {
497 frame.fp = (unsigned long)__builtin_frame_address(0);
498 frame.sp = current_stack_pointer;
499 frame.lr = (unsigned long)__builtin_return_address(0);
500 frame.pc = (unsigned long)unwind_backtrace;
501 } else {
502 /* task blocked in __switch_to */
503 frame.fp = thread_saved_fp(tsk);
504 frame.sp = thread_saved_sp(tsk);
505 /*
506 * The function calling __switch_to cannot be a leaf function
507 * so LR is recovered from the stack.
508 */
509 frame.lr = 0;
510 frame.pc = thread_saved_pc(tsk);
511 }
512
513 while (1) {
514 int urc;
515 unsigned long where = frame.pc;
516
517 urc = unwind_frame(&frame);
518 if (urc < 0)
519 break;
520 dump_backtrace_entry(where, frame.pc, frame.sp - 4);
521 }
522}
523
524struct unwind_table *unwind_table_add(unsigned long start, unsigned long size,
525 unsigned long text_addr,
526 unsigned long text_size)
527{
528 unsigned long flags;
529 struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL);
530
531 pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size,
532 text_addr, text_size);
533
534 if (!tab)
535 return tab;
536
537 tab->start = (const struct unwind_idx *)start;
538 tab->stop = (const struct unwind_idx *)(start + size);
539 tab->origin = unwind_find_origin(tab->start, tab->stop);
540 tab->begin_addr = text_addr;
541 tab->end_addr = text_addr + text_size;
542
543 raw_spin_lock_irqsave(&unwind_lock, flags);
544 list_add_tail(&tab->list, &unwind_tables);
545 raw_spin_unlock_irqrestore(&unwind_lock, flags);
546
547 return tab;
548}
549
550void unwind_table_del(struct unwind_table *tab)
551{
552 unsigned long flags;
553
554 if (!tab)
555 return;
556
557 raw_spin_lock_irqsave(&unwind_lock, flags);
558 list_del(&tab->list);
559 raw_spin_unlock_irqrestore(&unwind_lock, flags);
560
561 kfree(tab);
562}