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/unwind.h> 34 #include <kernel/thread.h> 35 #include <string.h> 36 #include <trace.h> 37 38 bool unwind_stack_arm64(struct unwind_state_arm64 *frame, uaddr_t stack, 39 size_t stack_size) 40 { 41 uint64_t fp; 42 43 fp = frame->fp; 44 if (fp < stack || fp >= stack + stack_size) 45 return false; 46 47 frame->sp = fp + 0x10; 48 /* FP to previous frame (X29) */ 49 frame->fp = *(uint64_t *)(fp); 50 /* LR (X30) */ 51 frame->pc = *(uint64_t *)(fp + 8) - 4; 52 53 return true; 54 } 55 56 #if defined(CFG_UNWIND) && (TRACE_LEVEL > 0) 57 58 void print_stack_arm64(int level, struct unwind_state_arm64 *state, 59 uaddr_t stack, size_t stack_size) 60 { 61 trace_printf_helper_raw(level, true, "Call stack:"); 62 do { 63 trace_printf_helper_raw(level, true, " 0x%016" PRIx64, 64 state->pc); 65 } while (stack && unwind_stack_arm64(state, stack, stack_size)); 66 } 67 68 void print_kernel_stack(int level) 69 { 70 struct unwind_state_arm64 state; 71 uaddr_t stack = thread_stack_start(); 72 size_t stack_size = thread_stack_size(); 73 74 memset(&state, 0, sizeof(state)); 75 state.pc = read_pc(); 76 state.fp = read_fp(); 77 78 print_stack_arm64(level, &state, stack, stack_size); 79 } 80 81 #endif 82