1 /*
2  * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 
11 #include <arch_helpers.h>
12 #include <common/debug.h>
13 #include <drivers/console.h>
14 
15 /* Maximum number of entries in the backtrace to display */
16 #define UNWIND_LIMIT	20U
17 
18 /*
19  * If -fno-omit-frame-pointer is used:
20  *
21  * - AArch64: The AAPCS defines the format of the frame records and mandates the
22  *   usage of r29 as frame pointer.
23  *
24  * - AArch32: The format of the frame records is not defined in the AAPCS.
25  *   However, at least GCC and Clang use the same format. When they are forced
26  *   to only generate A32 code (with -marm), they use r11 as frame pointer and a
27  *   similar format as in AArch64. If interworking with T32 is enabled, the
28  *   frame pointer is r7 and the format is  different. This is not supported by
29  *   this implementation of backtrace, so it is needed to use -marm.
30  */
31 
32 /* Frame records form a linked list in the stack */
33 struct frame_record {
34 	/* Previous frame record in the list */
35 	struct frame_record *parent;
36 	/* Return address of the function at this level */
37 	uintptr_t return_addr;
38 };
39 
get_el_str(unsigned int el)40 const char *get_el_str(unsigned int el)
41 {
42 	if (el == 3U) {
43 		return "EL3";
44 	} else if (el == 2U) {
45 		return "EL2";
46 	} else {
47 		return "S-EL1";
48 	}
49 }
50 
51 /*
52  * Returns true if the address points to a virtual address that can be read at
53  * the current EL, false otherwise.
54  */
55 #ifdef __aarch64__
is_address_readable(uintptr_t addr)56 static bool is_address_readable(uintptr_t addr)
57 {
58 	unsigned int el = get_current_el();
59 
60 #if ENABLE_PAUTH
61 	/*
62 	 * When pointer authentication is enabled, the LR value saved on the
63 	 * stack contains a PAC. It must be stripped to retrieve the return
64 	 * address.
65 	 */
66 	xpaci(addr);
67 #endif
68 	if (el == 3U) {
69 		ats1e3r(addr);
70 	} else if (el == 2U) {
71 		ats1e2r(addr);
72 	} else {
73 		AT(ats1e1r, addr);
74 	}
75 
76 	isb();
77 
78 	/* If PAR.F == 1 the address translation was aborted.  */
79 	if ((read_par_el1() & PAR_F_MASK) != 0U)
80 		return false;
81 
82 	return true;
83 }
84 #else /* !__aarch64__ */
is_address_readable(uintptr_t addr)85 static bool is_address_readable(uintptr_t addr)
86 {
87 	unsigned int el = get_current_el();
88 
89 	if (el == 3U) {
90 		write_ats1cpr(addr);
91 	} else if (el == 2U) {
92 		write_ats1hr(addr);
93 	} else {
94 		write_ats1cpr(addr);
95 	}
96 
97 	isb();
98 
99 	/* If PAR.F == 1 the address translation was aborted.  */
100 	if ((read64_par() & PAR_F_MASK) != 0U)
101 		return false;
102 
103 	return true;
104 }
105 #endif /* __aarch64__ */
106 
107 /*
108  * Returns true if all the bytes in a given object are in mapped memory and an
109  * LDR using this pointer would succeed, false otherwise.
110  */
is_valid_object(uintptr_t addr,size_t size)111 static bool is_valid_object(uintptr_t addr, size_t size)
112 {
113 	assert(size > 0U);
114 
115 	if (addr == 0U)
116 		return false;
117 
118 	/* Detect overflows */
119 	if ((addr + size) < addr)
120 		return false;
121 
122 	/* A pointer not aligned properly could trigger an alignment fault. */
123 	if ((addr & (sizeof(uintptr_t) - 1U)) != 0U)
124 		return false;
125 
126 	/* Check that all the object is readable */
127 	for (size_t i = 0; i < size; i++) {
128 		if (!is_address_readable(addr + i))
129 			return false;
130 	}
131 
132 	return true;
133 }
134 
135 /*
136  * Returns true if the specified address is correctly aligned and points to a
137  * valid memory region.
138  */
is_valid_jump_address(uintptr_t addr)139 static bool is_valid_jump_address(uintptr_t addr)
140 {
141 	if (addr == 0U)
142 		return false;
143 
144 	/* Check alignment. Both A64 and A32 use 32-bit opcodes */
145 	if ((addr & (sizeof(uint32_t) - 1U)) != 0U)
146 		return false;
147 
148 	if (!is_address_readable(addr))
149 		return false;
150 
151 	return true;
152 }
153 
154 /*
155  * Returns true if the pointer points at a valid frame record, false otherwise.
156  */
is_valid_frame_record(struct frame_record * fr)157 static bool is_valid_frame_record(struct frame_record *fr)
158 {
159 	return is_valid_object((uintptr_t)fr, sizeof(struct frame_record));
160 }
161 
162 /*
163  * Adjust the frame-pointer-register value by 4 bytes on AArch32 to have the
164  * same layout as AArch64.
165  */
adjust_frame_record(struct frame_record * fr)166 static struct frame_record *adjust_frame_record(struct frame_record *fr)
167 {
168 #ifdef __aarch64__
169 	return fr;
170 #else
171 	return (struct frame_record *)((uintptr_t)fr - 4U);
172 #endif
173 }
174 
unwind_stack(struct frame_record * fr,uintptr_t current_pc,uintptr_t link_register)175 static void unwind_stack(struct frame_record *fr, uintptr_t current_pc,
176 			 uintptr_t link_register)
177 {
178 	uintptr_t call_site;
179 	static const char *backtrace_str = "%u: %s: 0x%lx\n";
180 	const char *el_str = get_el_str(get_current_el());
181 
182 	if (!is_valid_frame_record(fr)) {
183 		printf("ERROR: Corrupted frame pointer (frame record address = %p)\n",
184 		       fr);
185 		return;
186 	}
187 
188 	if (fr->return_addr != link_register) {
189 		printf("ERROR: Corrupted stack (frame record address = %p)\n",
190 		       fr);
191 		return;
192 	}
193 
194 	/* The level 0 of the backtrace is the current backtrace function */
195 	printf(backtrace_str, 0U, el_str, current_pc);
196 
197 	/*
198 	 * The last frame record pointer in the linked list at the beginning of
199 	 * the stack should be NULL unless stack is corrupted.
200 	 */
201 	for (unsigned int i = 1U; i < UNWIND_LIMIT; i++) {
202 		/* If an invalid frame record is found, exit. */
203 		if (!is_valid_frame_record(fr))
204 			return;
205 		/*
206 		 * A32 and A64 are fixed length so the address from where the
207 		 * call was made is the instruction before the return address,
208 		 * which is always 4 bytes before it.
209 		 */
210 		call_site = fr->return_addr - 4U;
211 
212 #if ENABLE_PAUTH
213 		/*
214 		 * When pointer authentication is enabled, the LR value saved on
215 		 * the stack contains a PAC. It must be stripped to retrieve the
216 		 * return address.
217 		 */
218 		xpaci(call_site);
219 #endif
220 		/*
221 		 * If the address is invalid it means that the frame record is
222 		 * probably corrupted.
223 		 */
224 		if (!is_valid_jump_address(call_site))
225 			return;
226 
227 		printf(backtrace_str, i, el_str, call_site);
228 
229 		fr = adjust_frame_record(fr->parent);
230 	}
231 
232 	printf("ERROR: Max backtrace depth reached\n");
233 }
234 
235 /*
236  * Display a backtrace. The cookie string parameter is displayed along the
237  * trace to help filter the log messages.
238  *
239  * Many things can prevent displaying the expected backtrace. For example,
240  * compiler optimizations can use a branch instead of branch with link when it
241  * detects a tail call. The backtrace level for this caller will not be
242  * displayed, as it does not appear in the call stack anymore. Also, assembly
243  * functions will not be displayed unless they setup AAPCS compliant frame
244  * records on AArch64 and compliant with GCC-specific frame record format on
245  * AArch32.
246  *
247  * Usage of the trace: addr2line can be used to map the addresses to function
248  * and source code location when given the ELF file compiled with debug
249  * information. The "-i" flag is highly recommended to improve display of
250  * inlined function. The *.dump files generated when building each image can
251  * also be used.
252  *
253  * WARNING: In case of corrupted stack, this function could display security
254  * sensitive information past the beginning of the stack so it must not be used
255  * in production build. This function is only compiled in when ENABLE_BACKTRACE
256  * is set to 1.
257  */
backtrace(const char * cookie)258 void backtrace(const char *cookie)
259 {
260 	uintptr_t return_address = (uintptr_t)__builtin_return_address(0U);
261 	struct frame_record *fr = __builtin_frame_address(0U);
262 
263 	/* Printing the backtrace may crash the system, flush before starting */
264 	console_flush();
265 
266 	fr = adjust_frame_record(fr);
267 
268 	printf("BACKTRACE: START: %s\n", cookie);
269 
270 	unwind_stack(fr, (uintptr_t)&backtrace, return_address);
271 
272 	printf("BACKTRACE: END: %s\n", cookie);
273 }
274