2 * Copyright 2013-2014 Andrew Turner.
3 * Copyright 2013-2014 Ian Lepore.
4 * Copyright 2013-2014 Rui Paulo.
5 * Copyright 2013 Eitan Adler.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/linker.h>
37 #include <sys/malloc.h>
38 #include <sys/queue.h>
39 #include <sys/systm.h>
41 #include <machine/machdep.h>
42 #include <machine/stack.h>
44 #include "linker_if.h"
47 * Definitions for the instruction interpreter.
49 * The ARM EABI specifies how to perform the frame unwinding in the
50 * Exception Handling ABI for the ARM Architecture document. To perform
51 * the unwind we need to know the initial frame pointer, stack pointer,
52 * link register and program counter. We then find the entry within the
53 * index table that points to the function the program counter is within.
54 * This gives us either a list of three instructions to process, a 31-bit
55 * relative offset to a table of instructions, or a value telling us
56 * we can't unwind any further.
58 * When we have the instructions to process we need to decode them
59 * following table 4 in section 9.3. This describes a collection of bit
60 * patterns to encode that steps to take to update the stack pointer and
61 * link register to the correct values at the start of the function.
64 /* A special case when we are unable to unwind past this function */
65 #define EXIDX_CANTUNWIND 1
69 * These are the only entry types that have been seen in the kernel.
71 #define ENTRY_MASK 0xff000000
72 #define ENTRY_ARM_SU16 0x80000000
73 #define ENTRY_ARM_LU16 0x81000000
75 /* Instruction masks. */
76 #define INSN_VSP_MASK 0xc0
77 #define INSN_VSP_SIZE_MASK 0x3f
78 #define INSN_STD_MASK 0xf0
79 #define INSN_STD_DATA_MASK 0x0f
80 #define INSN_POP_TYPE_MASK 0x08
81 #define INSN_POP_COUNT_MASK 0x07
82 #define INSN_VSP_LARGE_INC_MASK 0xff
84 /* Instruction definitions */
85 #define INSN_VSP_INC 0x00
86 #define INSN_VSP_DEC 0x40
87 #define INSN_POP_MASKED 0x80
88 #define INSN_VSP_REG 0x90
89 #define INSN_POP_COUNT 0xa0
90 #define INSN_FINISH 0xb0
91 #define INSN_POP_REGS 0xb1
92 #define INSN_VSP_LARGE_INC 0xb2
94 /* An item in the exception index table */
101 * Local cache of unwind info for loaded modules.
103 * To unwind the stack through the code in a loaded module, we need to access
104 * the module's exidx unwind data. To locate that data, one must search the
105 * elf section headers for the SHT_ARM_EXIDX section. Those headers are
106 * available at the time the module is being loaded, but are discarded by time
107 * the load process has completed. Code in kern/link_elf.c locates the data we
108 * need and stores it into the linker_file structure before calling the arm
109 * machdep routine for handling loaded modules (in arm/elf_machdep.c). That
110 * function calls into this code to pass along the unwind info, which we save
111 * into one of these module_info structures.
113 * Because we have to help stack(9) gather stack info at any time, including in
114 * contexts where sleeping is not allowed, we cannot use linker_file_foreach()
115 * to walk the kernel's list of linker_file structs, because doing so requires
116 * acquiring an exclusive sx_lock. So instead, we keep a local list of these
117 * structures, one for each loaded module (and one for the kernel itself that we
118 * synthesize at init time). New entries are added to the end of this list as
119 * needed, but entries are never deleted from the list. Instead, they are
120 * cleared out in-place to mark them as unused. That means the code doing stack
121 * unwinding can always safely walk the list without locking, because the
122 * structure of the list never changes in a way that would cause the walker to
125 * A cleared-out entry on the list has module start=UINTPTR_MAX and end=0, so
126 * start <= addr < end cannot be true for any value of addr being searched for.
127 * We also don't have to worry about races where we look up the unwind info just
128 * before a module is unloaded and try to access it concurrently with or just
129 * after the unloading happens in another thread, because that means the path of
130 * execution leads through a now-unloaded module, and that's already well into
131 * undefined-behavior territory.
133 * List entries marked as unused get reused when new modules are loaded. We
134 * don't worry about holding a few unused bytes of memory in the list after
135 * unloading a module.
138 uintptr_t module_start; /* Start of loaded module */
139 uintptr_t module_end; /* End of loaded module */
140 uintptr_t exidx_start; /* Start of unwind data */
141 uintptr_t exidx_end; /* End of unwind data */
142 STAILQ_ENTRY(module_info)
143 link; /* Link to next entry */
145 static STAILQ_HEAD(, module_info) module_list;
148 * Hide ugly casting in somewhat-less-ugly macros.
149 * CADDR - cast a pointer or number to caddr_t.
150 * UADDR - cast a pointer or number to uintptr_t.
152 #define CADDR(addr) ((caddr_t)(void*)(uintptr_t)(addr))
153 #define UADDR(addr) ((uintptr_t)(addr))
156 * Clear the info in an existing module_info entry on the list. The
157 * module_start/end addresses are set to values that cannot match any real
158 * memory address. The entry remains on the list, but will be ignored until it
159 * is populated with new data.
162 clear_module_info(struct module_info *info)
164 info->module_start = UINTPTR_MAX;
165 info->module_end = 0;
169 * Populate an existing module_info entry (which is already on the list) with
170 * the info for a new module.
173 populate_module_info(struct module_info *info, linker_file_t lf)
177 * Careful! The module_start and module_end fields must not be set
178 * until all other data in the structure is valid.
180 info->exidx_start = UADDR(lf->exidx_addr);
181 info->exidx_end = UADDR(lf->exidx_addr) + lf->exidx_size;
182 info->module_start = UADDR(lf->address);
183 info->module_end = UADDR(lf->address) + lf->size;
187 * Create a new empty module_info entry and add it to the tail of the list.
189 static struct module_info *
190 create_module_info(void)
192 struct module_info *info;
194 info = malloc(sizeof(*info), M_CACHE, M_WAITOK | M_ZERO);
195 clear_module_info(info);
196 STAILQ_INSERT_TAIL(&module_list, info, link);
201 * Search for a module_info entry on the list whose address range contains the
202 * given address. If the search address is zero (no module will be loaded at
203 * zero), then we're looking for an empty item to reuse, which is indicated by
204 * module_start being set to UINTPTR_MAX in the entry.
206 static struct module_info *
207 find_module_info(uintptr_t addr)
209 struct module_info *info;
211 STAILQ_FOREACH(info, &module_list, link) {
212 if ((addr >= info->module_start && addr < info->module_end) ||
213 (addr == 0 && info->module_start == UINTPTR_MAX))
220 * Handle the loading of a new module by populating a module_info for it. This
221 * is called for both preloaded and dynamically loaded modules.
224 unwind_module_loaded(struct linker_file *lf)
226 struct module_info *info;
229 * A module that contains only data may have no unwind info; don't
230 * create any module info for it.
232 if (lf->exidx_size == 0)
236 * Find an unused entry in the existing list to reuse. If we don't find
237 * one, create a new one and link it into the list. This is the only
238 * place the module_list is modified. Adding a new entry to the list
239 * will not perturb any other threads currently walking the list. This
240 * function is invoked while kern_linker is still holding its lock
241 * to prevent its module list from being modified, so we don't have to
242 * worry about racing other threads doing an insert concurrently.
244 if ((info = find_module_info(0)) == NULL) {
245 info = create_module_info();
247 populate_module_info(info, lf);
250 /* Handle the unloading of a module. */
252 unwind_module_unloaded(struct linker_file *lf)
254 struct module_info *info;
257 * A module that contains only data may have no unwind info and there
258 * won't be a list entry for it.
260 if (lf->exidx_size == 0)
264 * When a module is unloaded, we clear the info out of its entry in the
265 * module list, making that entry available for later reuse.
267 if ((info = find_module_info(UADDR(lf->address))) == NULL) {
268 printf("arm unwind: module '%s' not on list at unload time\n",
272 clear_module_info(info);
276 * Initialization must run fairly early, as soon as malloc(9) is available, and
277 * definitely before witness, which uses stack(9). We synthesize a module_info
278 * entry for the kernel, because unwind_module_loaded() doesn't get called for
279 * it. Also, it is unlike other modules in that the elf metadata for locating
280 * the unwind tables might be stripped, so instead we have to use the
281 * _exidx_start/end symbols created by ldscript.arm.
284 module_info_init(void *arg __unused)
286 struct linker_file thekernel;
288 STAILQ_INIT(&module_list);
290 thekernel.filename = "kernel";
291 thekernel.address = CADDR(&_start);
292 thekernel.size = UADDR(&_end) - UADDR(&_start);
293 thekernel.exidx_addr = CADDR(&_exidx_start);
294 thekernel.exidx_size = UADDR(&_exidx_end) - UADDR(&_exidx_start);
295 populate_module_info(create_module_info(), &thekernel);
299 SYSINIT(unwind_init, SI_SUB_KMEM, SI_ORDER_ANY, module_info_init, NULL);
301 /* Expand a 31-bit signed value to a 32-bit signed value */
302 static __inline int32_t
303 expand_prel31(uint32_t prel31)
306 return ((int32_t)(prel31 & 0x7fffffffu) << 1) / 2;
310 * Perform a binary search of the index table to find the function
311 * with the largest address that doesn't exceed addr.
313 static struct unwind_idx *
314 find_index(uint32_t addr)
316 struct module_info *info;
317 unsigned int min, mid, max;
318 struct unwind_idx *start;
319 struct unwind_idx *item;
323 info = find_module_info(addr);
328 max = (info->exidx_end - info->exidx_start) / sizeof(struct unwind_idx);
329 start = (struct unwind_idx *)CADDR(info->exidx_start);
332 mid = min + (max - min + 1) / 2;
336 prel31_addr = expand_prel31(item->offset);
337 func_addr = (uint32_t)&item->offset + prel31_addr;
339 if (func_addr <= addr) {
349 /* Reads the next byte from the instruction list */
351 unwind_exec_read_byte(struct unwind_state *state)
355 /* Read the unwind instruction */
356 insn = (*state->insn) >> (state->byte * 8);
358 /* Update the location of the next instruction */
359 if (state->byte == 0) {
369 /* Executes the next instruction on the list */
371 unwind_exec_insn(struct unwind_state *state)
374 uint32_t *vsp = (uint32_t *)state->registers[SP];
377 /* This should never happen */
378 if (state->entries == 0)
381 /* Read the next instruction */
382 insn = unwind_exec_read_byte(state);
384 if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
385 state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
387 } else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
388 state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
390 } else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
391 unsigned int mask, reg;
394 mask = unwind_exec_read_byte(state);
395 mask |= (insn & INSN_STD_DATA_MASK) << 8;
397 /* We have a refuse to unwind instruction */
404 /* Load the registers */
405 for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
407 state->registers[reg] = *vsp++;
408 state->update_mask |= 1 << reg;
410 /* If we have updated SP kep its value */
416 } else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
417 ((insn & INSN_STD_DATA_MASK) != 13) &&
418 ((insn & INSN_STD_DATA_MASK) != 15)) {
420 state->registers[SP] =
421 state->registers[insn & INSN_STD_DATA_MASK];
423 } else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
424 unsigned int count, reg;
426 /* Read how many registers to load */
427 count = insn & INSN_POP_COUNT_MASK;
432 /* Pop the registers */
433 for (reg = 4; reg <= 4 + count; reg++) {
434 state->registers[reg] = *vsp++;
435 state->update_mask |= 1 << reg;
438 /* Check if we are in the pop r14 version */
439 if ((insn & INSN_POP_TYPE_MASK) != 0) {
440 state->registers[14] = *vsp++;
443 } else if (insn == INSN_FINISH) {
444 /* Stop processing */
447 } else if (insn == INSN_POP_REGS) {
448 unsigned int mask, reg;
450 mask = unwind_exec_read_byte(state);
451 if (mask == 0 || (mask & 0xf0) != 0)
457 /* Load the registers */
458 for (reg = 0; mask && reg < 4; mask >>= 1, reg++) {
460 state->registers[reg] = *vsp++;
461 state->update_mask |= 1 << reg;
465 } else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
466 unsigned int uleb128;
468 /* Read the increment value */
469 uleb128 = unwind_exec_read_byte(state);
471 state->registers[SP] += 0x204 + (uleb128 << 2);
474 /* We hit a new instruction that needs to be implemented */
476 db_printf("Unhandled instruction %.2x\n", insn);
482 state->registers[SP] = (uint32_t)vsp;
486 db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
487 state->registers[FP], state->registers[SP], state->registers[LR],
488 state->registers[PC]);
494 /* Performs the unwind of a function */
496 unwind_tab(struct unwind_state *state)
500 /* Set PC to a known value */
501 state->registers[PC] = 0;
503 /* Read the personality */
504 entry = *state->insn & ENTRY_MASK;
506 if (entry == ENTRY_ARM_SU16) {
509 } else if (entry == ENTRY_ARM_LU16) {
511 state->entries = ((*state->insn >> 16) & 0xFF) + 1;
514 db_printf("Unknown entry: %x\n", entry);
519 while (state->entries > 0) {
520 if (unwind_exec_insn(state) != 0)
525 * The program counter was not updated, load it from the link register.
527 if (state->registers[PC] == 0) {
528 state->registers[PC] = state->registers[LR];
531 * If the program counter changed, flag it in the update mask.
533 if (state->start_pc != state->registers[PC])
534 state->update_mask |= 1 << PC;
541 * Unwind a single stack frame.
542 * Return 0 on success or 1 if the stack cannot be unwound any further.
544 * XXX The can_lock argument is no longer germane; a sweep of callers should be
545 * made to remove it after this new code has proven itself for a while.
548 unwind_stack_one(struct unwind_state *state, int can_lock __unused)
550 struct unwind_idx *index;
552 /* Reset the mask of updated registers */
553 state->update_mask = 0;
555 /* The pc value is correct and will be overwritten, save it */
556 state->start_pc = state->registers[PC];
558 /* Find the item to run */
559 index = find_index(state->start_pc);
560 if (index == NULL || index->insn == EXIDX_CANTUNWIND)
563 if (index->insn & (1U << 31)) {
564 /* The data is within the instruction */
565 state->insn = &index->insn;
567 /* A prel31 offset to the unwind table */
568 state->insn = (uint32_t *)
569 ((uintptr_t)&index->insn +
570 expand_prel31(index->insn));
573 /* Run the unwind function, return its finished/not-finished status. */
574 return (unwind_tab(state));