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/linker.h> 34 #include <kernel/thread.h> 35 #include <kernel/unwind.h> 36 #include <unw/unwind.h> 37 38 #include "unwind_private.h" 39 40 #if defined(CFG_CORE_PAUTH) 41 void pauth_strip_pac(uint64_t *lr) 42 { 43 const uint64_t va_mask = GENMASK_64(CFG_LPAE_ADDR_SPACE_BITS - 1, 0); 44 45 *lr = *lr & va_mask; 46 } 47 #endif 48 49 vaddr_t *unw_get_kernel_stack(void) 50 { 51 size_t n = 0; 52 size_t size = 0; 53 vaddr_t *tmp = NULL; 54 vaddr_t *addr = NULL; 55 uaddr_t stack = thread_stack_start(); 56 size_t stack_size = thread_stack_size(); 57 struct unwind_state_arm64 state = { 58 .pc = read_pc(), 59 .fp = read_fp() 60 }; 61 62 while (unwind_stack_arm64(&state, stack, stack_size)) { 63 tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t)); 64 if (!tmp) 65 goto err; 66 addr = tmp; 67 addr[n] = state.pc; 68 n++; 69 } 70 71 if (addr) { 72 tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t)); 73 if (!tmp) 74 goto err; 75 addr = tmp; 76 addr[n] = 0; 77 } 78 79 return addr; 80 err: 81 EMSG("Out of memory"); 82 free(addr); 83 return NULL; 84 } 85 86 #if defined(CFG_UNWIND) && (TRACE_LEVEL > 0) 87 void print_kernel_stack(void) 88 { 89 struct unwind_state_arm64 state = { }; 90 vaddr_t stack_start = 0; 91 vaddr_t stack_end = 0; 92 93 state.pc = read_pc(); 94 state.fp = read_fp(); 95 96 trace_printf_helper_raw(TRACE_ERROR, true, 97 "TEE load address @ %#"PRIxVA, VCORE_START_VA); 98 get_stack_hard_limits(&stack_start, &stack_end); 99 print_stack_arm64(&state, stack_start, stack_end - stack_start); 100 } 101 #endif 102