xref: /optee_os/core/arch/arm/kernel/unwind_arm64.c (revision af8149de7c373fd859175f9989a64915832ee8c0)
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 
40 static bool copy_in_reg(uint64_t *reg, vaddr_t addr, bool kernel_data)
41 {
42 	if (!kernel_data)
43 		return !tee_svc_copy_from_user(reg, (void *)addr, sizeof(*reg));
44 
45 	memcpy(reg, (void *)addr, sizeof(*reg));
46 	return true;
47 }
48 
49 bool unwind_stack_arm64(struct unwind_state_arm64 *frame, bool kernel_stack,
50 			vaddr_t stack, size_t stack_size)
51 {
52 	vaddr_t fp = frame->fp;
53 
54 	if (!core_is_buffer_inside(fp, sizeof(uint64_t) * 3,
55 				   stack, stack_size)) {
56 		DMSG("FP out of bounds %#" PRIxVA, fp);
57 		return false;
58 	}
59 
60 	frame->sp = fp + 0x10;
61 	/* FP to previous frame (X29) */
62 	if (!copy_in_reg(&frame->fp, fp, kernel_stack))
63 		return false;
64 	/* LR (X30) */
65 	if (!copy_in_reg(&frame->pc, fp + 8, kernel_stack))
66 		return false;
67 	frame->pc -= 4;
68 
69 	return true;
70 }
71 
72 #if defined(CFG_UNWIND) && (TRACE_LEVEL > 0)
73 
74 void print_stack_arm64(int level, struct unwind_state_arm64 *state,
75 		       bool kernel_stack, vaddr_t stack, size_t stack_size)
76 {
77 	trace_printf_helper_raw(level, true, "Call stack:");
78 	do {
79 		trace_printf_helper_raw(level, true, " 0x%016" PRIx64,
80 					state->pc);
81 	} while (unwind_stack_arm64(state, kernel_stack, stack, stack_size));
82 }
83 
84 void print_kernel_stack(int level)
85 {
86 	struct unwind_state_arm64 state;
87 	uaddr_t stack = thread_stack_start();
88 	size_t stack_size = thread_stack_size();
89 
90 	memset(&state, 0, sizeof(state));
91 	state.pc = read_pc();
92 	state.fp = read_fp();
93 
94 	print_stack_arm64(level, &state,
95 			  true /*kernel_stack*/, stack, stack_size);
96 }
97 
98 #endif
99