1 // SPDX-License-Identifier: BSD-2-Clause 2 /*- 3 * Copyright (c) 2015 Linaro Limited 4 * Copyright (c) 2015 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by Semihalf under 8 * the sponsorship of the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <arm.h> 33 #include <kernel/thread.h> 34 #include <kernel/unwind.h> 35 #include <kernel/tee_misc.h> 36 #include <string.h> 37 #include <tee/tee_svc.h> 38 #include <trace.h> 39 #include <util.h> 40 41 #include "unwind_private.h" 42 43 static void copy_in_reg(uint64_t *reg, vaddr_t addr) 44 { 45 memcpy(reg, (void *)addr, sizeof(*reg)); 46 } 47 48 bool unwind_stack_arm64(struct unwind_state_arm64 *frame, 49 vaddr_t stack, size_t stack_size) 50 { 51 vaddr_t fp = frame->fp; 52 53 if (!core_is_buffer_inside(fp, sizeof(uint64_t) * 3, 54 stack, stack_size)) 55 return false; 56 57 frame->sp = fp + 0x10; 58 /* FP to previous frame (X29) */ 59 copy_in_reg(&frame->fp, fp); 60 /* LR (X30) */ 61 copy_in_reg(&frame->pc, fp + 8); 62 frame->pc -= 4; 63 64 return true; 65 } 66 67 #if (TRACE_LEVEL > 0) 68 69 void print_stack_arm64(int level, struct unwind_state_arm64 *state, 70 vaddr_t stack, size_t stack_size) 71 { 72 trace_printf_helper_raw(level, true, "Call stack:"); 73 74 do { 75 trace_printf_helper_raw(level, true, " 0x%016" PRIx64, 76 state->pc); 77 } while (unwind_stack_arm64(state, stack, stack_size)); 78 } 79 80 void print_kernel_stack(int level) 81 { 82 struct unwind_state_arm64 state; 83 uaddr_t stack = thread_stack_start(); 84 size_t stack_size = thread_stack_size(); 85 86 memset(&state, 0, sizeof(state)); 87 state.pc = read_pc(); 88 state.fp = read_fp(); 89 90 print_stack_arm64(level, &state, stack, stack_size); 91 } 92 93 #endif 94 95 vaddr_t *unw_get_kernel_stack(void) 96 { 97 size_t n = 0; 98 size_t size = 0; 99 vaddr_t *tmp = NULL; 100 vaddr_t *addr = NULL; 101 struct unwind_state_arm64 state = { 0 }; 102 uaddr_t stack = thread_stack_start(); 103 size_t stack_size = thread_stack_size(); 104 105 state.pc = read_pc(); 106 state.fp = read_fp(); 107 108 while (unwind_stack_arm64(&state, stack, stack_size)) { 109 tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t)); 110 if (!tmp) 111 goto err; 112 addr = tmp; 113 addr[n] = state.pc; 114 n++; 115 } 116 117 if (addr) { 118 tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t)); 119 if (!tmp) 120 goto err; 121 addr = tmp; 122 addr[n] = 0; 123 } 124 125 return addr; 126 err: 127 EMSG("Out of memory"); 128 free(addr); 129 return NULL; 130 } 131