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/ftrace.h> 34 #include <kernel/thread.h> 35 #include <kernel/unwind.h> 36 #include <kernel/tee_misc.h> 37 #include <string.h> 38 #include <tee/tee_svc.h> 39 #include <trace.h> 40 #include <util.h> 41 42 #include "unwind_private.h" 43 44 static bool copy_in_reg(uint64_t *reg, vaddr_t addr, bool kernel_data) 45 { 46 if (!kernel_data) 47 return !tee_svc_copy_from_user(reg, (void *)addr, sizeof(*reg)); 48 49 memcpy(reg, (void *)addr, sizeof(*reg)); 50 return true; 51 } 52 53 bool unwind_stack_arm64(struct unwind_state_arm64 *frame, bool kernel_stack, 54 vaddr_t stack, size_t stack_size) 55 { 56 vaddr_t fp = frame->fp; 57 58 if (!core_is_buffer_inside(fp, sizeof(uint64_t) * 3, 59 stack, stack_size)) 60 return false; 61 62 frame->sp = fp + 0x10; 63 /* FP to previous frame (X29) */ 64 if (!copy_in_reg(&frame->fp, fp, kernel_stack)) 65 return false; 66 /* LR (X30) */ 67 if (!copy_in_reg(&frame->pc, fp + 8, kernel_stack)) 68 return false; 69 70 if (!kernel_stack) 71 ftrace_ta_map_lr(&frame->pc); 72 73 frame->pc -= 4; 74 75 return true; 76 } 77 78 #if (TRACE_LEVEL > 0) 79 80 void print_stack_arm64(int level, struct unwind_state_arm64 *state, 81 bool kernel_stack, vaddr_t stack, size_t stack_size) 82 { 83 trace_printf_helper_raw(level, true, "Call stack:"); 84 85 if (!kernel_stack) 86 ftrace_ta_map_lr(&state->pc); 87 do { 88 trace_printf_helper_raw(level, true, " 0x%016" PRIx64, 89 state->pc); 90 } while (unwind_stack_arm64(state, kernel_stack, stack, stack_size)); 91 } 92 93 void print_kernel_stack(int level) 94 { 95 struct unwind_state_arm64 state; 96 uaddr_t stack = thread_stack_start(); 97 size_t stack_size = thread_stack_size(); 98 99 memset(&state, 0, sizeof(state)); 100 state.pc = read_pc(); 101 state.fp = read_fp(); 102 103 print_stack_arm64(level, &state, 104 true /*kernel_stack*/, stack, stack_size); 105 } 106 107 #endif 108 109 vaddr_t *unw_get_kernel_stack(void) 110 { 111 size_t n = 0; 112 size_t size = 0; 113 vaddr_t *tmp = NULL; 114 vaddr_t *addr = NULL; 115 struct unwind_state_arm64 state = { 0 }; 116 uaddr_t stack = thread_stack_start(); 117 size_t stack_size = thread_stack_size(); 118 119 state.pc = read_pc(); 120 state.fp = read_fp(); 121 122 while (unwind_stack_arm64(&state, true /*kernel stack*/, 123 stack, stack_size)) { 124 tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t)); 125 if (!tmp) 126 goto err; 127 addr = tmp; 128 addr[n] = state.pc; 129 n++; 130 } 131 132 if (addr) { 133 tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t)); 134 if (!tmp) 135 goto err; 136 addr = tmp; 137 addr[n] = 0; 138 } 139 140 return addr; 141 err: 142 EMSG("Out of memory"); 143 free(addr); 144 return NULL; 145 } 146