xref: /freebsd/sys/arm/arm/unwind.c (revision 0b9c12fa976446705635a94984f45232f034eecf)
1 /*
2  * Copyright 2013-2014 Andrew Turner.
3  * Copyright 2013-2014 Ian Lepore.
4  * Copyright 2013-2014 Rui Paulo.
5  * Copyright 2013 Eitan Adler.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/linker.h>
34 #include <sys/malloc.h>
35 #include <sys/proc.h>
36 #include <sys/queue.h>
37 #include <sys/systm.h>
38 
39 #include <machine/machdep.h>
40 #include <machine/stack.h>
41 
42 #include "linker_if.h"
43 
44 /*
45  * Definitions for the instruction interpreter.
46  *
47  * The ARM EABI specifies how to perform the frame unwinding in the
48  * Exception Handling ABI for the ARM Architecture document. To perform
49  * the unwind we need to know the initial frame pointer, stack pointer,
50  * link register and program counter. We then find the entry within the
51  * index table that points to the function the program counter is within.
52  * This gives us either a list of three instructions to process, a 31-bit
53  * relative offset to a table of instructions, or a value telling us
54  * we can't unwind any further.
55  *
56  * When we have the instructions to process we need to decode them
57  * following table 4 in section 9.3. This describes a collection of bit
58  * patterns to encode that steps to take to update the stack pointer and
59  * link register to the correct values at the start of the function.
60  */
61 
62 /* A special case when we are unable to unwind past this function */
63 #define	EXIDX_CANTUNWIND	1
64 
65 /*
66  * Entry types.
67  * These are the only entry types that have been seen in the kernel.
68  */
69 #define	ENTRY_MASK	0xff000000
70 #define	ENTRY_ARM_SU16	0x80000000
71 #define	ENTRY_ARM_LU16	0x81000000
72 
73 /* Instruction masks. */
74 #define	INSN_VSP_MASK		0xc0
75 #define	INSN_VSP_SIZE_MASK	0x3f
76 #define	INSN_STD_MASK		0xf0
77 #define	INSN_STD_DATA_MASK	0x0f
78 #define	INSN_POP_TYPE_MASK	0x08
79 #define	INSN_POP_COUNT_MASK	0x07
80 #define	INSN_VSP_LARGE_INC_MASK	0xff
81 
82 /* Instruction definitions */
83 #define	INSN_VSP_INC		0x00
84 #define	INSN_VSP_DEC		0x40
85 #define	INSN_POP_MASKED		0x80
86 #define	INSN_VSP_REG		0x90
87 #define	INSN_POP_COUNT		0xa0
88 #define	INSN_FINISH		0xb0
89 #define	INSN_POP_REGS		0xb1
90 #define	INSN_VSP_LARGE_INC	0xb2
91 
92 /* An item in the exception index table */
93 struct unwind_idx {
94 	uint32_t offset;
95 	uint32_t insn;
96 };
97 
98 /*
99  * Local cache of unwind info for loaded modules.
100  *
101  * To unwind the stack through the code in a loaded module, we need to access
102  * the module's exidx unwind data.  To locate that data, one must search the
103  * elf section headers for the SHT_ARM_EXIDX section.  Those headers are
104  * available at the time the module is being loaded, but are discarded by time
105  * the load process has completed.  Code in kern/link_elf.c locates the data we
106  * need and stores it into the linker_file structure before calling the arm
107  * machdep routine for handling loaded modules (in arm/elf_machdep.c).  That
108  * function calls into this code to pass along the unwind info, which we save
109  * into one of these module_info structures.
110  *
111  * Because we have to help stack(9) gather stack info at any time, including in
112  * contexts where sleeping is not allowed, we cannot use linker_file_foreach()
113  * to walk the kernel's list of linker_file structs, because doing so requires
114  * acquiring an exclusive sx_lock.  So instead, we keep a local list of these
115  * structures, one for each loaded module (and one for the kernel itself that we
116  * synthesize at init time).  New entries are added to the end of this list as
117  * needed, but entries are never deleted from the list.  Instead, they are
118  * cleared out in-place to mark them as unused.  That means the code doing stack
119  * unwinding can always safely walk the list without locking, because the
120  * structure of the list never changes in a way that would cause the walker to
121  * follow a bad link.
122  *
123  * A cleared-out entry on the list has module start=UINTPTR_MAX and end=0, so
124  * start <= addr < end cannot be true for any value of addr being searched for.
125  * We also don't have to worry about races where we look up the unwind info just
126  * before a module is unloaded and try to access it concurrently with or just
127  * after the unloading happens in another thread, because that means the path of
128  * execution leads through a now-unloaded module, and that's already well into
129  * undefined-behavior territory.
130  *
131  * List entries marked as unused get reused when new modules are loaded.  We
132  * don't worry about holding a few unused bytes of memory in the list after
133  * unloading a module.
134  */
135 struct module_info {
136 	uintptr_t	module_start;   /* Start of loaded module */
137 	uintptr_t	module_end;     /* End of loaded module */
138 	uintptr_t	exidx_start;    /* Start of unwind data */
139 	uintptr_t	exidx_end;      /* End of unwind data */
140 	STAILQ_ENTRY(module_info)
141 			link;           /* Link to next entry */
142 };
143 static STAILQ_HEAD(, module_info) module_list;
144 
145 /*
146  * Hide ugly casting in somewhat-less-ugly macros.
147  *  CADDR - cast a pointer or number to caddr_t.
148  *  UADDR - cast a pointer or number to uintptr_t.
149  */
150 #define	CADDR(addr)	((caddr_t)(void*)(uintptr_t)(addr))
151 #define	UADDR(addr)	((uintptr_t)(addr))
152 
153 /*
154  * Clear the info in an existing module_info entry on the list.  The
155  * module_start/end addresses are set to values that cannot match any real
156  * memory address.  The entry remains on the list, but will be ignored until it
157  * is populated with new data.
158  */
159 static void
clear_module_info(struct module_info * info)160 clear_module_info(struct module_info *info)
161 {
162 	info->module_start = UINTPTR_MAX;
163 	info->module_end   = 0;
164 }
165 
166 /*
167  * Populate an existing module_info entry (which is already on the list) with
168  * the info for a new module.
169  */
170 static void
populate_module_info(struct module_info * info,linker_file_t lf)171 populate_module_info(struct module_info *info, linker_file_t lf)
172 {
173 
174 	/*
175 	 * Careful!  The module_start and module_end fields must not be set
176 	 * until all other data in the structure is valid.
177 	 */
178 	info->exidx_start  = UADDR(lf->exidx_addr);
179 	info->exidx_end    = UADDR(lf->exidx_addr) + lf->exidx_size;
180 	info->module_start = UADDR(lf->address);
181 	info->module_end   = UADDR(lf->address) + lf->size;
182 }
183 
184 /*
185  * Create a new empty module_info entry and add it to the tail of the list.
186  */
187 static struct module_info *
create_module_info(void)188 create_module_info(void)
189 {
190 	struct module_info *info;
191 
192 	info = malloc(sizeof(*info), M_CACHE, M_WAITOK | M_ZERO);
193 	clear_module_info(info);
194 	STAILQ_INSERT_TAIL(&module_list, info, link);
195 	return (info);
196 }
197 
198 /*
199  * Search for a module_info entry on the list whose address range contains the
200  * given address.  If the search address is zero (no module will be loaded at
201  * zero), then we're looking for an empty item to reuse, which is indicated by
202  * module_start being set to UINTPTR_MAX in the entry.
203  */
204 static struct module_info *
find_module_info(uintptr_t addr)205 find_module_info(uintptr_t addr)
206 {
207 	struct module_info *info;
208 
209 	STAILQ_FOREACH(info, &module_list, link) {
210 		if ((addr >= info->module_start && addr < info->module_end) ||
211 		    (addr == 0 && info->module_start == UINTPTR_MAX))
212 			return (info);
213 	}
214 	return (NULL);
215 }
216 
217 /*
218  * Handle the loading of a new module by populating a module_info for it.  This
219  * is called for both preloaded and dynamically loaded modules.
220  */
221 void
unwind_module_loaded(struct linker_file * lf)222 unwind_module_loaded(struct linker_file *lf)
223 {
224 	struct module_info *info;
225 
226 	/*
227 	 * A module that contains only data may have no unwind info; don't
228 	 * create any module info for it.
229 	 */
230 	if (lf->exidx_size == 0)
231 		return;
232 
233 	/*
234 	 * Find an unused entry in the existing list to reuse.  If we don't find
235 	 * one, create a new one and link it into the list.  This is the only
236 	 * place the module_list is modified.  Adding a new entry to the list
237 	 * will not perturb any other threads currently walking the list.  This
238 	 * function is invoked while kern_linker is still holding its lock
239 	 * to prevent its module list from being modified, so we don't have to
240 	 * worry about racing other threads doing an insert concurrently.
241 	 */
242 	if ((info = find_module_info(0)) == NULL) {
243 		info = create_module_info();
244 	}
245 	populate_module_info(info, lf);
246 }
247 
248 /* Handle the unloading of a module. */
249 void
unwind_module_unloaded(struct linker_file * lf)250 unwind_module_unloaded(struct linker_file *lf)
251 {
252 	struct module_info *info;
253 
254 	/*
255 	 * A module that contains only data may have no unwind info and there
256 	 * won't be a list entry for it.
257 	 */
258 	if (lf->exidx_size == 0)
259 		return;
260 
261 	/*
262 	 * When a module is unloaded, we clear the info out of its entry in the
263 	 * module list, making that entry available for later reuse.
264 	 */
265 	if ((info = find_module_info(UADDR(lf->address))) == NULL) {
266 		printf("arm unwind: module '%s' not on list at unload time\n",
267 		    lf->filename);
268 		return;
269 	}
270 	clear_module_info(info);
271 }
272 
273 /*
274  * Initialization must run fairly early, as soon as malloc(9) is available, and
275  * definitely before witness, which uses stack(9).  We synthesize a module_info
276  * entry for the kernel, because unwind_module_loaded() doesn't get called for
277  * it.  Also, it is unlike other modules in that the elf metadata for locating
278  * the unwind tables might be stripped, so instead we have to use the
279  * _exidx_start/end symbols created by ldscript.arm.
280  */
281 static void
module_info_init(void * arg __unused)282 module_info_init(void *arg __unused)
283 {
284 	struct linker_file thekernel;
285 
286 	STAILQ_INIT(&module_list);
287 
288 	thekernel.filename   = "kernel";
289 	thekernel.address    = CADDR(&_start);
290 	thekernel.size       = UADDR(&_end) - UADDR(&_start);
291 	thekernel.exidx_addr = CADDR(&_exidx_start);
292 	thekernel.exidx_size = UADDR(&_exidx_end) - UADDR(&_exidx_start);
293 	populate_module_info(create_module_info(), &thekernel);
294 }
295 SYSINIT(unwind_init, SI_SUB_KMEM, SI_ORDER_ANY, module_info_init, NULL);
296 
297 /* Expand a 31-bit signed value to a 32-bit signed value */
298 static __inline int32_t
expand_prel31(uint32_t prel31)299 expand_prel31(uint32_t prel31)
300 {
301 
302 	return ((int32_t)(prel31 & 0x7fffffffu) << 1) / 2;
303 }
304 
305 /*
306  * Perform a binary search of the index table to find the function
307  * with the largest address that doesn't exceed addr.
308  */
309 static struct unwind_idx *
find_index(uint32_t addr)310 find_index(uint32_t addr)
311 {
312 	struct module_info *info;
313 	unsigned int min, mid, max;
314 	struct unwind_idx *start;
315 	struct unwind_idx *item;
316 	int32_t prel31_addr;
317 	uint32_t func_addr;
318 
319 	info = find_module_info(addr);
320 	if (info == NULL)
321 		return NULL;
322 
323 	min = 0;
324 	max = (info->exidx_end - info->exidx_start) / sizeof(struct unwind_idx);
325 	start = (struct unwind_idx *)CADDR(info->exidx_start);
326 
327 	while (min != max) {
328 		mid = min + (max - min + 1) / 2;
329 
330 		item = &start[mid];
331 
332 		prel31_addr = expand_prel31(item->offset);
333 		func_addr = (uint32_t)&item->offset + prel31_addr;
334 
335 		if (func_addr <= addr) {
336 			min = mid;
337 		} else {
338 			max = mid - 1;
339 		}
340 	}
341 
342 	return &start[min];
343 }
344 
345 /* Reads the next byte from the instruction list */
346 static uint8_t
unwind_exec_read_byte(struct unwind_state * state)347 unwind_exec_read_byte(struct unwind_state *state)
348 {
349 	uint8_t insn;
350 
351 	/* Read the unwind instruction */
352 	insn = (*state->insn) >> (state->byte * 8);
353 
354 	/* Update the location of the next instruction */
355 	if (state->byte == 0) {
356 		state->byte = 3;
357 		state->insn++;
358 		state->entries--;
359 	} else
360 		state->byte--;
361 
362 	return insn;
363 }
364 
365 /* Executes the next instruction on the list */
366 static int
unwind_exec_insn(struct unwind_state * state)367 unwind_exec_insn(struct unwind_state *state)
368 {
369 	struct thread *td = curthread;
370 	unsigned int insn;
371 	uint32_t *vsp = (uint32_t *)state->registers[SP];
372 	int update_vsp = 0;
373 
374 	/* This should never happen */
375 	if (state->entries == 0)
376 		return 1;
377 
378 	/* Read the next instruction */
379 	insn = unwind_exec_read_byte(state);
380 
381 	if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
382 		state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
383 
384 	} else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
385 		state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
386 
387 	} else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
388 		unsigned int mask, reg;
389 
390 		/* Load the mask */
391 		mask = unwind_exec_read_byte(state);
392 		mask |= (insn & INSN_STD_DATA_MASK) << 8;
393 
394 		/* We have a refuse to unwind instruction */
395 		if (mask == 0)
396 			return 1;
397 
398 		if (!__is_aligned(vsp, sizeof(register_t)))
399 			return 1;
400 
401 		/* Update SP */
402 		update_vsp = 1;
403 
404 		/* Load the registers */
405 		for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
406 			if (mask & 1) {
407 				if (!kstack_contains(td, (uintptr_t)vsp,
408 				    sizeof(*vsp)))
409 					return 1;
410 
411 				state->registers[reg] = *vsp++;
412 				state->update_mask |= 1 << reg;
413 
414 				/* If we have updated SP kep its value */
415 				if (reg == SP)
416 					update_vsp = 0;
417 			}
418 		}
419 
420 	} else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
421 	    ((insn & INSN_STD_DATA_MASK) != 13) &&
422 	    ((insn & INSN_STD_DATA_MASK) != 15)) {
423 		/* sp = register */
424 		state->registers[SP] =
425 		    state->registers[insn & INSN_STD_DATA_MASK];
426 
427 	} else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
428 		unsigned int count, reg;
429 
430 		/* Read how many registers to load */
431 		count = insn & INSN_POP_COUNT_MASK;
432 
433 		if (!__is_aligned(vsp, sizeof(register_t)))
434 			return 1;
435 
436 		/* Update sp */
437 		update_vsp = 1;
438 
439 		/* Pop the registers */
440 		if (!kstack_contains(td, (uintptr_t)vsp,
441 		    sizeof(*vsp) * (4 + count)))
442 			return 1;
443 		for (reg = 4; reg <= 4 + count; reg++) {
444 			state->registers[reg] = *vsp++;
445 			state->update_mask |= 1 << reg;
446 		}
447 
448 		/* Check if we are in the pop r14 version */
449 		if ((insn & INSN_POP_TYPE_MASK) != 0) {
450 			if (!kstack_contains(td, (uintptr_t)vsp, sizeof(*vsp)))
451 				return 1;
452 			state->registers[14] = *vsp++;
453 		}
454 
455 	} else if (insn == INSN_FINISH) {
456 		/* Stop processing */
457 		state->entries = 0;
458 
459 	} else if (insn == INSN_POP_REGS) {
460 		unsigned int mask, reg;
461 
462 		mask = unwind_exec_read_byte(state);
463 		if (mask == 0 || (mask & 0xf0) != 0)
464 			return 1;
465 
466 		if (!__is_aligned(vsp, sizeof(register_t)))
467 			return 1;
468 
469 		/* Update SP */
470 		update_vsp = 1;
471 
472 		/* Load the registers */
473 		for (reg = 0; mask && reg < 4; mask >>= 1, reg++) {
474 			if (mask & 1) {
475 				if (!kstack_contains(td, (uintptr_t)vsp,
476 				    sizeof(*vsp)))
477 					return 1;
478 				state->registers[reg] = *vsp++;
479 				state->update_mask |= 1 << reg;
480 			}
481 		}
482 
483 	} else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
484 		unsigned int uleb128;
485 
486 		/* Read the increment value */
487 		uleb128 = unwind_exec_read_byte(state);
488 
489 		state->registers[SP] += 0x204 + (uleb128 << 2);
490 
491 	} else {
492 		/* We hit a new instruction that needs to be implemented */
493 #if 0
494 		db_printf("Unhandled instruction %.2x\n", insn);
495 #endif
496 		return 1;
497 	}
498 
499 	if (update_vsp) {
500 		state->registers[SP] = (uint32_t)vsp;
501 	}
502 
503 #if 0
504 	db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
505 	    state->registers[FP], state->registers[SP], state->registers[LR],
506 	    state->registers[PC]);
507 #endif
508 
509 	return 0;
510 }
511 
512 /* Performs the unwind of a function */
513 static int
unwind_tab(struct unwind_state * state)514 unwind_tab(struct unwind_state *state)
515 {
516 	uint32_t entry;
517 
518 	/* Set PC to a known value */
519 	state->registers[PC] = 0;
520 
521 	/* Read the personality */
522 	entry = *state->insn & ENTRY_MASK;
523 
524 	if (entry == ENTRY_ARM_SU16) {
525 		state->byte = 2;
526 		state->entries = 1;
527 	} else if (entry == ENTRY_ARM_LU16) {
528 		state->byte = 1;
529 		state->entries = ((*state->insn >> 16) & 0xFF) + 1;
530 	} else {
531 #if 0
532 		db_printf("Unknown entry: %x\n", entry);
533 #endif
534 		return 1;
535 	}
536 
537 	while (state->entries > 0) {
538 		if (unwind_exec_insn(state) != 0)
539 			return 1;
540 	}
541 
542 	/*
543 	 * The program counter was not updated, load it from the link register.
544 	 */
545 	if (state->registers[PC] == 0) {
546 		state->registers[PC] = state->registers[LR];
547 
548 		/*
549 		 * If the program counter changed, flag it in the update mask.
550 		 */
551 		if (state->start_pc != state->registers[PC])
552 			state->update_mask |= 1 << PC;
553 	}
554 
555 	return 0;
556 }
557 
558 /*
559  * Unwind a single stack frame.
560  * Return 0 on success or 1 if the stack cannot be unwound any further.
561  *
562  * XXX The can_lock argument is no longer germane; a sweep of callers should be
563  * made to remove it after this new code has proven itself for a while.
564  */
565 int
unwind_stack_one(struct unwind_state * state,int can_lock __unused)566 unwind_stack_one(struct unwind_state *state, int can_lock __unused)
567 {
568 	struct unwind_idx *index;
569 
570 	/* Reset the mask of updated registers */
571 	state->update_mask = 0;
572 
573 	/* The pc value is correct and will be overwritten, save it */
574 	state->start_pc = state->registers[PC];
575 
576 	/* Find the item to run */
577 	index = find_index(state->start_pc);
578 	if (index == NULL || index->insn == EXIDX_CANTUNWIND)
579 		return 1;
580 
581 	if (index->insn & (1U << 31)) {
582 		/* The data is within the instruction */
583 		state->insn = &index->insn;
584 	} else {
585 		/* A prel31 offset to the unwind table */
586 		state->insn = (uint32_t *)
587 		    ((uintptr_t)&index->insn +
588 		     expand_prel31(index->insn));
589 	}
590 
591 	/* Run the unwind function, return its finished/not-finished status. */
592 	return (unwind_tab(state));
593 }
594