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