xref: /optee_os/core/arch/arm/kernel/unwind_arm32.c (revision 3d3b05918ec9052ba13de82fbcaba204766eb636)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2015 Linaro Limited
4  * Copyright 2013-2014 Andrew Turner.
5  * Copyright 2013-2014 Ian Lepore.
6  * Copyright 2013-2014 Rui Paulo.
7  * Copyright 2013 Eitan Adler.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are
12  * met:
13  *
14  *  1. Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *  2. Redistributions in binary form must reproduce the above copyright
17  *     notice, this list of conditions and the following disclaimer in the
18  *     documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <arm.h>
34 #include <kernel/linker.h>
35 #include <kernel/misc.h>
36 #include <kernel/tee_misc.h>
37 #include <kernel/unwind.h>
38 #include <string.h>
39 #include <tee_api_types.h>
40 #include <tee/tee_svc.h>
41 #include <trace.h>
42 #include <util.h>
43 
44 #include "unwind_private.h"
45 
46 /* The register names */
47 #define	FP	11
48 #define	SP	13
49 #define	LR	14
50 #define	PC	15
51 
52 /*
53  * Definitions for the instruction interpreter.
54  *
55  * The ARM EABI specifies how to perform the frame unwinding in the
56  * Exception Handling ABI for the ARM Architecture document. To perform
57  * the unwind we need to know the initial frame pointer, stack pointer,
58  * link register and program counter. We then find the entry within the
59  * index table that points to the function the program counter is within.
60  * This gives us either a list of three instructions to process, a 31-bit
61  * relative offset to a table of instructions, or a value telling us
62  * we can't unwind any further.
63  *
64  * When we have the instructions to process we need to decode them
65  * following table 4 in section 9.3. This describes a collection of bit
66  * patterns to encode that steps to take to update the stack pointer and
67  * link register to the correct values at the start of the function.
68  */
69 
70 /* A special case when we are unable to unwind past this function */
71 #define	EXIDX_CANTUNWIND	1
72 
73 /*
74  * Entry types.
75  * These are the only entry types that have been seen in the kernel.
76  */
77 #define	ENTRY_MASK	0xff000000
78 #define	ENTRY_ARM_SU16	0x80000000
79 #define	ENTRY_ARM_LU16	0x81000000
80 
81 /* Instruction masks. */
82 #define	INSN_VSP_MASK		0xc0
83 #define	INSN_VSP_SIZE_MASK	0x3f
84 #define	INSN_STD_MASK		0xf0
85 #define	INSN_STD_DATA_MASK	0x0f
86 #define	INSN_POP_TYPE_MASK	0x08
87 #define	INSN_POP_COUNT_MASK	0x07
88 #define	INSN_VSP_LARGE_INC_MASK	0xff
89 
90 /* Instruction definitions */
91 #define	INSN_VSP_INC		0x00
92 #define	INSN_VSP_DEC		0x40
93 #define	INSN_POP_MASKED		0x80
94 #define	INSN_VSP_REG		0x90
95 #define	INSN_POP_COUNT		0xa0
96 #define	INSN_FINISH		0xb0
97 #define	INSN_POP_REGS		0xb1
98 #define	INSN_VSP_LARGE_INC	0xb2
99 
100 /* An item in the exception index table */
101 struct unwind_idx {
102 	uint32_t offset;
103 	uint32_t insn;
104 };
105 
106 static bool copy_in(void *dst, const void *src, size_t n, bool kernel_data)
107 {
108 	if (!kernel_data)
109 		return !tee_svc_copy_from_user(dst, src, n);
110 
111 	memcpy(dst, src, n);
112 	return true;
113 }
114 
115 /* Expand a 31-bit signed value to a 32-bit signed value */
116 static int32_t expand_prel31(uint32_t prel31)
117 {
118 	return prel31 | SHIFT_U32(prel31 & BIT32(30), 1);
119 }
120 
121 /*
122  * Perform a binary search of the index table to find the function
123  * with the largest address that doesn't exceed addr.
124  */
125 static struct unwind_idx *find_index(uint32_t addr, vaddr_t exidx,
126 				     size_t exidx_sz)
127 {
128 	vaddr_t idx_start, idx_end;
129 	unsigned int min, mid, max;
130 	struct unwind_idx *start;
131 	struct unwind_idx *item;
132 	int32_t prel31_addr;
133 	vaddr_t func_addr;
134 
135 	start = (struct unwind_idx *)exidx;
136 	idx_start = exidx;
137 	idx_end = exidx + exidx_sz;
138 
139 	min = 0;
140 	max = (idx_end - idx_start) / sizeof(struct unwind_idx);
141 
142 	while (min != max) {
143 		mid = min + (max - min + 1) / 2;
144 
145 		item = &start[mid];
146 
147 		prel31_addr = expand_prel31(item->offset);
148 		func_addr = (vaddr_t)&item->offset + prel31_addr;
149 
150 		if (func_addr <= addr) {
151 			min = mid;
152 		} else {
153 			max = mid - 1;
154 		}
155 	}
156 
157 	return &start[min];
158 }
159 
160 /* Reads the next byte from the instruction list */
161 static bool unwind_exec_read_byte(struct unwind_state_arm32 *state,
162 				  uint32_t *ret_insn, bool kernel_stack)
163 {
164 	uint32_t insn;
165 
166 	if (!copy_in(&insn, (void *)state->insn, sizeof(insn), kernel_stack))
167 		return false;
168 
169 	/* Read the unwind instruction */
170 	*ret_insn = (insn >> (state->byte * 8)) & 0xff;
171 
172 	/* Update the location of the next instruction */
173 	if (state->byte == 0) {
174 		state->byte = 3;
175 		state->insn += sizeof(uint32_t);
176 		state->entries--;
177 	} else
178 		state->byte--;
179 
180 	return true;
181 }
182 
183 static bool pop_vsp(uint32_t *reg, vaddr_t *vsp, bool kernel_stack,
184 		    vaddr_t stack, size_t stack_size)
185 {
186 	if (!core_is_buffer_inside(*vsp, sizeof(*reg), stack, stack_size))
187 		return false;
188 
189 	if (!copy_in(reg, (void *)*vsp, sizeof(*reg), kernel_stack))
190 		return false;
191 	(*vsp) += sizeof(*reg);
192 	return true;
193 }
194 
195 /* Executes the next instruction on the list */
196 static bool unwind_exec_insn(struct unwind_state_arm32 *state,
197 			     bool kernel_stack, vaddr_t stack,
198 			     size_t stack_size)
199 {
200 	uint32_t insn;
201 	vaddr_t vsp = state->registers[SP];
202 	int update_vsp = 0;
203 
204 	/* Read the next instruction */
205 	if (!unwind_exec_read_byte(state, &insn, kernel_stack))
206 		return false;
207 
208 	if ((insn & INSN_VSP_MASK) == INSN_VSP_INC) {
209 		state->registers[SP] += ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
210 
211 	} else if ((insn & INSN_VSP_MASK) == INSN_VSP_DEC) {
212 		state->registers[SP] -= ((insn & INSN_VSP_SIZE_MASK) << 2) + 4;
213 
214 	} else if ((insn & INSN_STD_MASK) == INSN_POP_MASKED) {
215 		uint32_t mask;
216 		unsigned int reg;
217 
218 		/* Load the mask */
219 		if (!unwind_exec_read_byte(state, &mask, kernel_stack))
220 			return false;
221 		mask |= (insn & INSN_STD_DATA_MASK) << 8;
222 
223 		/* We have a refuse to unwind instruction */
224 		if (mask == 0)
225 			return false;
226 
227 		/* Update SP */
228 		update_vsp = 1;
229 
230 		/* Load the registers */
231 		for (reg = 4; mask && reg < 16; mask >>= 1, reg++) {
232 			if (mask & 1) {
233 				if (!pop_vsp(&state->registers[reg], &vsp,
234 					     kernel_stack, stack, stack_size))
235 					return false;
236 				state->update_mask |= 1 << reg;
237 
238 				/* If we have updated SP kep its value */
239 				if (reg == SP)
240 					update_vsp = 0;
241 			}
242 		}
243 
244 	} else if ((insn & INSN_STD_MASK) == INSN_VSP_REG &&
245 	    ((insn & INSN_STD_DATA_MASK) != 13) &&
246 	    ((insn & INSN_STD_DATA_MASK) != 15)) {
247 		/* sp = register */
248 		state->registers[SP] =
249 		    state->registers[insn & INSN_STD_DATA_MASK];
250 
251 	} else if ((insn & INSN_STD_MASK) == INSN_POP_COUNT) {
252 		unsigned int count, reg;
253 
254 		/* Read how many registers to load */
255 		count = insn & INSN_POP_COUNT_MASK;
256 
257 		/* Update sp */
258 		update_vsp = 1;
259 
260 		/* Pop the registers */
261 		for (reg = 4; reg <= 4 + count; reg++) {
262 			if (!pop_vsp(&state->registers[reg], &vsp,
263 				     kernel_stack, stack, stack_size))
264 				return false;
265 			state->update_mask |= 1 << reg;
266 		}
267 
268 		/* Check if we are in the pop r14 version */
269 		if ((insn & INSN_POP_TYPE_MASK) != 0) {
270 			if (!pop_vsp(&state->registers[14], &vsp, kernel_stack,
271 				     stack, stack_size))
272 				return false;
273 		}
274 
275 	} else if (insn == INSN_FINISH) {
276 		/* Stop processing */
277 		state->entries = 0;
278 
279 	} else if (insn == INSN_POP_REGS) {
280 		uint32_t mask;
281 		unsigned int reg;
282 
283 		if (!unwind_exec_read_byte(state, &mask, kernel_stack))
284 			return false;
285 		if (mask == 0 || (mask & 0xf0) != 0)
286 			return false;
287 
288 		/* Update SP */
289 		update_vsp = 1;
290 
291 		/* Load the registers */
292 		for (reg = 0; mask && reg < 4; mask >>= 1, reg++) {
293 			if (mask & 1) {
294 				if (!pop_vsp(&state->registers[reg], &vsp,
295 					     kernel_stack, stack, stack_size))
296 					return false;
297 				state->update_mask |= 1 << reg;
298 			}
299 		}
300 
301 	} else if ((insn & INSN_VSP_LARGE_INC_MASK) == INSN_VSP_LARGE_INC) {
302 		uint32_t uleb128;
303 
304 		/* Read the increment value */
305 		if (!unwind_exec_read_byte(state, &uleb128, kernel_stack))
306 			return false;
307 
308 		state->registers[SP] += 0x204 + (uleb128 << 2);
309 
310 	} else {
311 		/* We hit a new instruction that needs to be implemented */
312 		DMSG("Unhandled instruction %.2x", insn);
313 		return false;
314 	}
315 
316 	if (update_vsp)
317 		state->registers[SP] = vsp;
318 
319 	return true;
320 }
321 
322 /* Performs the unwind of a function */
323 static bool unwind_tab(struct unwind_state_arm32 *state, bool kernel_stack,
324 		       vaddr_t stack, size_t stack_size)
325 {
326 	uint32_t entry;
327 	uint32_t insn;
328 
329 	/* Set PC to a known value */
330 	state->registers[PC] = 0;
331 
332 	if (!copy_in(&insn, (void *)state->insn, sizeof(insn), kernel_stack)) {
333 		DMSG("Bad insn addr %p", (void *)state->insn);
334 		return true;
335 	}
336 
337 	/* Read the personality */
338 	entry = insn & ENTRY_MASK;
339 
340 	if (entry == ENTRY_ARM_SU16) {
341 		state->byte = 2;
342 		state->entries = 1;
343 	} else if (entry == ENTRY_ARM_LU16) {
344 		state->byte = 1;
345 		state->entries = ((insn >> 16) & 0xFF) + 1;
346 	} else {
347 		DMSG("Unknown entry: %x", entry);
348 		return true;
349 	}
350 
351 	while (state->entries > 0) {
352 		if (!unwind_exec_insn(state, kernel_stack, stack, stack_size))
353 			return true;
354 	}
355 
356 	/*
357 	 * The program counter was not updated, load it from the link register.
358 	 */
359 	if (state->registers[PC] == 0) {
360 		state->registers[PC] = state->registers[LR];
361 
362 		/*
363 		 * If the program counter changed, flag it in the update mask.
364 		 */
365 		if (state->start_pc != state->registers[PC])
366 			state->update_mask |= 1 << PC;
367 	}
368 
369 	return false;
370 }
371 
372 bool unwind_stack_arm32(struct unwind_state_arm32 *state, vaddr_t exidx,
373 			size_t exidx_sz, bool kernel_stack, vaddr_t stack,
374 			size_t stack_size)
375 {
376 	struct unwind_idx *index;
377 	bool finished;
378 
379 	if (!exidx_sz)
380 		return false;
381 
382 	/* Reset the mask of updated registers */
383 	state->update_mask = 0;
384 
385 	/* The pc value is correct and will be overwritten, save it */
386 	state->start_pc = state->registers[PC];
387 
388 	/* Find the item to run */
389 	index = find_index(state->start_pc, exidx, exidx_sz);
390 
391 	finished = false;
392 	if (index->insn != EXIDX_CANTUNWIND) {
393 		if (index->insn & (1U << 31)) {
394 			/* The data is within the instruction */
395 			state->insn = (vaddr_t)&index->insn;
396 		} else {
397 			/* A prel31 offset to the unwind table */
398 			state->insn = (vaddr_t)&index->insn +
399 				      expand_prel31(index->insn);
400 		}
401 
402 		/* Run the unwind function */
403 		finished = unwind_tab(state, kernel_stack, stack, stack_size);
404 	}
405 
406 	/* This is the top of the stack, finish */
407 	if (index->insn == EXIDX_CANTUNWIND)
408 		finished = true;
409 
410 	return !finished;
411 }
412 
413 static uint32_t offset_prel31(uint32_t addr, int32_t offset)
414 {
415 	return (addr + offset) & 0x7FFFFFFFUL;
416 }
417 
418 TEE_Result relocate_exidx(void *exidx, size_t exidx_sz, int32_t offset)
419 {
420 	size_t num_items = exidx_sz / sizeof(struct unwind_idx);
421 	struct unwind_idx *start = (struct unwind_idx *)exidx;
422 	size_t n;
423 
424 	for (n = 0; n < num_items; n++) {
425 		struct unwind_idx *item = &start[n];
426 
427 		if (item->offset & BIT32(31))
428 			return TEE_ERROR_BAD_FORMAT;
429 
430 		/* Offset to the start of the function has to be adjusted */
431 		item->offset = offset_prel31(item->offset, offset);
432 
433 		if (item->insn == EXIDX_CANTUNWIND)
434 			continue;
435 		if (item->insn & BIT32(31)) {
436 			/* insn is a table entry itself */
437 			continue;
438 		}
439 		/*
440 		 * insn is an offset to an entry in .ARM.extab so it has to be
441 		 * adjusted
442 		 */
443 		item->insn = offset_prel31(item->insn, offset);
444 	}
445 	return TEE_SUCCESS;
446 }
447 
448 #if (TRACE_LEVEL > 0)
449 
450 void print_stack_arm32(int level, struct unwind_state_arm32 *state,
451 		       vaddr_t exidx, size_t exidx_sz, bool kernel_stack,
452 		       vaddr_t stack, size_t stack_size)
453 {
454 	trace_printf_helper_raw(level, true, "Call stack:");
455 	do {
456 		trace_printf_helper_raw(level, true, " 0x%08" PRIx32,
457 					state->registers[PC]);
458 	} while (unwind_stack_arm32(state, exidx, exidx_sz,
459 				    kernel_stack, stack, stack_size));
460 }
461 
462 #endif
463 
464 #if defined(ARM32) && (TRACE_LEVEL > 0)
465 
466 void print_kernel_stack(int level)
467 {
468 	struct unwind_state_arm32 state;
469 	uaddr_t exidx = (vaddr_t)__exidx_start;
470 	size_t exidx_sz = (vaddr_t)__exidx_end - (vaddr_t)__exidx_start;
471 	vaddr_t stack = thread_stack_start();
472 	size_t stack_size = thread_stack_size();
473 
474 	memset(&state, 0, sizeof(state));
475 	/* r7: Thumb-style frame pointer */
476 	state.registers[7] = read_r7();
477 	/* r11: ARM-style frame pointer */
478 	state.registers[FP] = read_fp();
479 	state.registers[SP] = read_sp();
480 	state.registers[LR] = read_lr();
481 	state.registers[PC] = (uint32_t)print_kernel_stack;
482 
483 	print_stack_arm32(level, &state, exidx, exidx_sz,
484 			  true /*kernel_stack*/, stack, stack_size);
485 }
486 
487 #endif
488 
489 #if defined(ARM32)
490 vaddr_t *unw_get_kernel_stack(void)
491 {
492 	size_t n = 0;
493 	size_t size = 0;
494 	size_t exidx_sz = 0;
495 	vaddr_t *tmp = NULL;
496 	vaddr_t *addr = NULL;
497 	struct unwind_state_arm32 state = { 0 };
498 	uaddr_t exidx = (vaddr_t)__exidx_start;
499 	vaddr_t stack = thread_stack_start();
500 	size_t stack_size = thread_stack_size();
501 
502 	if (SUB_OVERFLOW((vaddr_t)__exidx_end, (vaddr_t)__exidx_start,
503 			 &exidx_sz))
504 		return NULL;
505 
506 	/* r7: Thumb-style frame pointer */
507 	state.registers[7] = read_r7();
508 	/* r11: ARM-style frame pointer */
509 	state.registers[FP] = read_fp();
510 	state.registers[SP] = read_sp();
511 	state.registers[LR] = read_lr();
512 	state.registers[PC] = (uint32_t)unw_get_kernel_stack;
513 
514 	while (unwind_stack_arm32(&state, exidx, exidx_sz,
515 				  true /*kernel stack*/, stack, stack_size)) {
516 		tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t));
517 		if (!tmp)
518 			goto err;
519 		addr = tmp;
520 		addr[n] = state.registers[PC];
521 		n++;
522 	}
523 
524 	if (addr) {
525 		tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t));
526 		if (!tmp)
527 			goto err;
528 		addr = tmp;
529 		addr[n] = 0;
530 	}
531 
532 	return addr;
533 err:
534 	EMSG("Out of memory");
535 	free(addr);
536 	return NULL;
537 }
538 #endif
539