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