xref: /optee_os/core/arch/arm/kernel/unwind_arm64.c (revision 5b25c76ac40f830867e3d60800120ffd7874e8dc)
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/tee_misc.h>
35 #include <kernel/tee_ta_manager.h>
36 #include <kernel/thread.h>
37 #include <kernel/unwind.h>
38 #include <string.h>
39 #include <tee/tee_svc.h>
40 #include <trace.h>
41 #include <user_ta_header.h>
42 #include <util.h>
43 
44 #include "unwind_private.h"
45 
46 static void copy_in_reg(uint64_t *reg, vaddr_t addr)
47 {
48 	memcpy(reg, (void *)addr, sizeof(*reg));
49 }
50 
51 #ifdef CFG_SYSCALL_FTRACE
52 static void ftrace_core_map_lr(uint64_t *lr)
53 {
54 	struct ftrace_buf *fbuf = NULL;
55 	struct tee_ta_session *s = NULL;
56 
57 	if (tee_ta_get_current_session(&s) != TEE_SUCCESS)
58 		return;
59 
60 	if (!s->fbuf)
61 		return;
62 
63 	fbuf = s->fbuf;
64 
65 	/*
66 	 * Function tracer inserts return hook (addr: &__ftrace_return)
67 	 * via modifying lr values in the stack frames. And during aborts,
68 	 * stack trace picks these modified lr values which needs to be
69 	 * replaced with original lr value. So here we use the ftrace return
70 	 * stack to retrieve original lr value but we need to first check if
71 	 * it has actually been modified or not in case TA is profiled
72 	 * partially.
73 	 */
74 	if ((*lr == (uint64_t)&__ftrace_return) &&
75 	    fbuf->lr_idx < fbuf->ret_idx) {
76 		fbuf->lr_idx++;
77 		*lr = fbuf->ret_stack[fbuf->ret_idx - fbuf->lr_idx];
78 	}
79 }
80 #else
81 static void ftrace_core_map_lr(uint64_t *lr __unused)
82 {
83 }
84 #endif
85 
86 bool unwind_stack_arm64(struct unwind_state_arm64 *frame,
87 			vaddr_t stack, size_t stack_size)
88 {
89 	vaddr_t fp = frame->fp;
90 
91 	if (!core_is_buffer_inside(fp, sizeof(uint64_t) * 3,
92 				   stack, stack_size))
93 		return false;
94 
95 	frame->sp = fp + 0x10;
96 	/* FP to previous frame (X29) */
97 	copy_in_reg(&frame->fp, fp);
98 	/* LR (X30) */
99 	copy_in_reg(&frame->pc, fp + 8);
100 
101 	ftrace_core_map_lr(&frame->pc);
102 
103 	frame->pc -= 4;
104 
105 	return true;
106 }
107 
108 #if (TRACE_LEVEL > 0)
109 
110 void print_stack_arm64(int level, struct unwind_state_arm64 *state,
111 		       vaddr_t stack, size_t stack_size)
112 {
113 	trace_printf_helper_raw(level, true, "TEE load address @ %#"PRIxVA,
114 				VCORE_START_VA);
115 	trace_printf_helper_raw(level, true, "Call stack:");
116 
117 	do {
118 		trace_printf_helper_raw(level, true, " 0x%016" PRIx64,
119 					state->pc);
120 	} while (unwind_stack_arm64(state, stack, stack_size));
121 }
122 
123 void print_kernel_stack(int level)
124 {
125 	struct unwind_state_arm64 state;
126 	uaddr_t stack = thread_stack_start();
127 	size_t stack_size = thread_stack_size();
128 
129 	memset(&state, 0, sizeof(state));
130 	state.pc = read_pc();
131 	state.fp = read_fp();
132 
133 	print_stack_arm64(level, &state, stack, stack_size);
134 }
135 
136 #endif
137 
138 vaddr_t *unw_get_kernel_stack(void)
139 {
140 	size_t n = 0;
141 	size_t size = 0;
142 	vaddr_t *tmp = NULL;
143 	vaddr_t *addr = NULL;
144 	struct unwind_state_arm64 state = { 0 };
145 	uaddr_t stack = thread_stack_start();
146 	size_t stack_size = thread_stack_size();
147 
148 	state.pc = read_pc();
149 	state.fp = read_fp();
150 
151 	while (unwind_stack_arm64(&state, stack, stack_size)) {
152 		tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t));
153 		if (!tmp)
154 			goto err;
155 		addr = tmp;
156 		addr[n] = state.pc;
157 		n++;
158 	}
159 
160 	if (addr) {
161 		tmp = unw_grow(addr, &size, (n + 1) * sizeof(vaddr_t));
162 		if (!tmp)
163 			goto err;
164 		addr = tmp;
165 		addr[n] = 0;
166 	}
167 
168 	return addr;
169 err:
170 	EMSG("Out of memory");
171 	free(addr);
172 	return NULL;
173 }
174