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 vaddr_t *unw_get_kernel_stack(void) 41 { 42 size_t n = 0; 43 size_t size = 0; 44 vaddr_t *tmp = NULL; 45 vaddr_t *addr = NULL; 46 uaddr_t stack = thread_stack_start(); 47 size_t stack_size = thread_stack_size(); 48 struct unwind_state_arm64 state = { 49 .pc = read_pc(), 50 .fp = read_fp() 51 }; 52 53 while (unwind_stack_arm64(&state, stack, stack_size)) { 54 tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t)); 55 if (!tmp) 56 goto err; 57 addr = tmp; 58 addr[n] = state.pc; 59 n++; 60 } 61 62 if (addr) { 63 tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t)); 64 if (!tmp) 65 goto err; 66 addr = tmp; 67 addr[n] = 0; 68 } 69 70 return addr; 71 err: 72 EMSG("Out of memory"); 73 free(addr); 74 return NULL; 75 } 76 77 void print_kernel_stack(void) 78 { 79 struct unwind_state_arm64 state = { }; 80 vaddr_t stack_start = 0; 81 vaddr_t stack_end = 0; 82 83 state.pc = read_pc(); 84 state.fp = read_fp(); 85 86 trace_printf_helper_raw(TRACE_ERROR, true, 87 "TEE load address @ %#"PRIxVA, VCORE_START_VA); 88 get_stack_hard_limits(&stack_start, &stack_end); 89 print_stack_arm64(&state, stack_start, stack_end - stack_start); 90 } 91