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/systm.h>
36 #include <sys/linker.h>
38 #include <machine/stack.h>
40 #include "linker_if.h"
43 * Definitions for the instruction interpreter.
45 * The ARM EABI specifies how to perform the frame unwinding in the
46 * Exception Handling ABI for the ARM Architecture document. To perform
47 * the unwind we need to know the initial frame pointer, stack pointer,
48 * link register and program counter. We then find the entry within the
49 * index table that points to the function the program counter is within.
50 * This gives us either a list of three instructions to process, a 31-bit
51 * relative offset to a table of instructions, or a value telling us
52 * we can't unwind any further.
54 * When we have the instructions to process we need to decode them
55 * following table 4 in section 9.3. This describes a collection of bit
56 * patterns to encode that steps to take to update the stack pointer and
57 * link register to the correct values at the start of the function.
60 /* A special case when we are unable to unwind past this function */
61 #define EXIDX_CANTUNWIND 1
64 * These are set in the linker script. Their addresses will be
65 * either the start or end of the exception table or index.
67 extern int exidx_start, exidx_end;
71 * These are the only entry types that have been seen in the kernel.
73 #define ENTRY_MASK 0xff000000
74 #define ENTRY_ARM_SU16 0x80000000
75 #define ENTRY_ARM_LU16 0x81000000
77 /* Instruction masks. */
78 #define INSN_VSP_MASK 0xc0
79 #define INSN_VSP_SIZE_MASK 0x3f
80 #define INSN_STD_MASK 0xf0
81 #define INSN_STD_DATA_MASK 0x0f
82 #define INSN_POP_TYPE_MASK 0x08
83 #define INSN_POP_COUNT_MASK 0x07
84 #define INSN_VSP_LARGE_INC_MASK 0xff
86 /* Instruction definitions */
87 #define INSN_VSP_INC 0x00
88 #define INSN_VSP_DEC 0x40
89 #define INSN_POP_MASKED 0x80
90 #define INSN_VSP_REG 0x90
91 #define INSN_POP_COUNT 0xa0
92 #define INSN_FINISH 0xb0
93 #define INSN_POP_REGS 0xb1
94 #define INSN_VSP_LARGE_INC 0xb2
96 /* An item in the exception index table */
102 /* Expand a 31-bit signed value to a 32-bit signed value */
103 static __inline int32_t
104 expand_prel31(uint32_t prel31)
107 return ((int32_t)(prel31 & 0x7fffffffu) << 1) / 2;
110 struct search_context {
117 module_search(linker_file_t lf, void *context)
119 struct search_context *sc = context;
120 linker_symval_t symval;
123 if (lf->address <= (caddr_t)sc->addr &&
124 (lf->address + lf->size) >= (caddr_t)sc->addr) {
125 if ((LINKER_LOOKUP_SYMBOL(lf, "__exidx_start", &sym) == 0 ||
126 LINKER_LOOKUP_SYMBOL(lf, "exidx_start", &sym) == 0) &&
127 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0)
128 sc->exidx_start = symval.value;
130 if ((LINKER_LOOKUP_SYMBOL(lf, "__exidx_end", &sym) == 0 ||
131 LINKER_LOOKUP_SYMBOL(lf, "exidx_end", &sym) == 0) &&
132 LINKER_SYMBOL_VALUES(lf, sym, &symval) == 0)
133 sc->exidx_end = symval.value;
135 if (sc->exidx_start != NULL && sc->exidx_end != NULL)
137 panic("Invalid module %s, no unwind tables\n", lf->filename);
143 * Perform a binary search of the index table to find the function
144 * with the largest address that doesn't exceed addr.
146 static struct unwind_idx *
147 find_index(uint32_t addr, int search_modules)
149 struct search_context sc;
150 caddr_t idx_start, idx_end;
151 unsigned int min, mid, max;
152 struct unwind_idx *start;
153 struct unwind_idx *item;
157 start = (struct unwind_idx *)&exidx_start;
158 idx_start = (caddr_t)&exidx_start;
159 idx_end = (caddr_t)&exidx_end;
161 /* This may acquire a lock */
162 if (search_modules) {
163 bzero(&sc, sizeof(sc));
165 if (linker_file_foreach(module_search, &sc) != 0 &&
166 sc.exidx_start != NULL && sc.exidx_end != NULL) {
167 start = (struct unwind_idx *)sc.exidx_start;
168 idx_start = sc.exidx_start;
169 idx_end = sc.exidx_end;
174 max = (idx_end - idx_start) / sizeof(struct unwind_idx);
177 mid = min + (max - min + 1) / 2;
181 prel31_addr = expand_prel31(item->offset);
182 func_addr = (uint32_t)&item->offset + prel31_addr;
184 if (func_addr <= addr) {
194 /* Reads the next byte from the instruction list */
196 unwind_exec_read_byte(struct unwind_state *state)
200 /* Read the unwind instruction */
201 insn = (*state->insn) >> (state->byte * 8);
203 /* Update the location of the next instruction */
204 if (state->byte == 0) {
214 /* Executes the next instruction on the list */
216 unwind_exec_insn(struct unwind_state *state)
219 uint32_t *vsp = (uint32_t *)state->registers[SP];
222 /* This should never happen */
223 if (state->entries == 0)
226 /* Read the next instruction */
227 insn = unwind_exec_read_byte(state);
229 if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
230 state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
232 } else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
233 state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
235 } else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
236 unsigned int mask, reg;
239 mask = unwind_exec_read_byte(state);
240 mask |= (insn & INSN_STD_DATA_MASK) << 8;
242 /* We have a refuse to unwind instruction */
249 /* Load the registers */
250 for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
252 state->registers[reg] = *vsp++;
253 state->update_mask |= 1 << reg;
255 /* If we have updated SP kep its value */
261 } else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
262 ((insn & INSN_STD_DATA_MASK) != 13) &&
263 ((insn & INSN_STD_DATA_MASK) != 15)) {
265 state->registers[SP] =
266 state->registers[insn & INSN_STD_DATA_MASK];
268 } else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
269 unsigned int count, reg;
271 /* Read how many registers to load */
272 count = insn & INSN_POP_COUNT_MASK;
277 /* Pop the registers */
278 for (reg = 4; reg <= 4 + count; reg++) {
279 state->registers[reg] = *vsp++;
280 state->update_mask |= 1 << reg;
283 /* Check if we are in the pop r14 version */
284 if ((insn & INSN_POP_TYPE_MASK) != 0) {
285 state->registers[14] = *vsp++;
288 } else if (insn == INSN_FINISH) {
289 /* Stop processing */
292 } else if (insn == INSN_POP_REGS) {
293 unsigned int mask, reg;
295 mask = unwind_exec_read_byte(state);
296 if (mask == 0 || (mask & 0xf0) != 0)
302 /* Load the registers */
303 for (reg = 0; mask && reg < 4; mask >>= 1, reg++) {
305 state->registers[reg] = *vsp++;
306 state->update_mask |= 1 << reg;
310 } else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
311 unsigned int uleb128;
313 /* Read the increment value */
314 uleb128 = unwind_exec_read_byte(state);
316 state->registers[SP] += 0x204 + (uleb128 << 2);
319 /* We hit a new instruction that needs to be implemented */
321 db_printf("Unhandled instruction %.2x\n", insn);
327 state->registers[SP] = (uint32_t)vsp;
331 db_printf("fp = %08x, sp = %08x, lr = %08x, pc = %08x\n",
332 state->registers[FP], state->registers[SP], state->registers[LR],
333 state->registers[PC]);
339 /* Performs the unwind of a function */
341 unwind_tab(struct unwind_state *state)
345 /* Set PC to a known value */
346 state->registers[PC] = 0;
348 /* Read the personality */
349 entry = *state->insn & ENTRY_MASK;
351 if (entry == ENTRY_ARM_SU16) {
354 } else if (entry == ENTRY_ARM_LU16) {
356 state->entries = ((*state->insn >> 16) & 0xFF) + 1;
359 db_printf("Unknown entry: %x\n", entry);
364 while (state->entries > 0) {
365 if (unwind_exec_insn(state) != 0)
370 * The program counter was not updated, load it from the link register.
372 if (state->registers[PC] == 0) {
373 state->registers[PC] = state->registers[LR];
376 * If the program counter changed, flag it in the update mask.
378 if (state->start_pc != state->registers[PC])
379 state->update_mask |= 1 << PC;
386 unwind_stack_one(struct unwind_state *state, int can_lock)
388 struct unwind_idx *index;
391 /* Reset the mask of updated registers */
392 state->update_mask = 0;
394 /* The pc value is correct and will be overwritten, save it */
395 state->start_pc = state->registers[PC];
397 /* Find the item to run */
398 index = find_index(state->start_pc, can_lock);
401 if (index->insn != EXIDX_CANTUNWIND) {
402 if (index->insn & (1U << 31)) {
403 /* The data is within the instruction */
404 state->insn = &index->insn;
406 /* A prel31 offset to the unwind table */
407 state->insn = (uint32_t *)
408 ((uintptr_t)&index->insn +
409 expand_prel31(index->insn));
411 /* Run the unwind function */
412 finished = unwind_tab(state);
415 /* This is the top of the stack, finish */
416 if (index->insn == EXIDX_CANTUNWIND)