1 // SPDX-License-Identifier: BSD-2-Clause
2 /*-
3  * Copyright (c) 2015-2019 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 <compiler.h>
33 #include <string.h>
34 #include <trace.h>
35 #include <types_ext.h>
36 #include <unw/unwind.h>
37 #include <util.h>
38 
ftrace_map_lr(uint64_t * lr __unused)39 void __weak ftrace_map_lr(uint64_t *lr __unused)
40 {}
41 
pauth_strip_pac(uint64_t * lr __unused)42 void __weak pauth_strip_pac(uint64_t *lr __unused)
43 {}
44 
copy_in_reg(uint64_t * reg,vaddr_t addr)45 static bool copy_in_reg(uint64_t *reg, vaddr_t addr)
46 {
47 	memcpy(reg, (void *)addr, sizeof(*reg));
48 	return true;
49 }
50 
unwind_stack_arm64(struct unwind_state_arm64 * frame,vaddr_t stack,size_t stack_size)51 bool unwind_stack_arm64(struct unwind_state_arm64 *frame,
52 			vaddr_t stack, size_t stack_size)
53 {
54 	vaddr_t fp = frame->fp;
55 
56 	if (fp < stack)
57 		return false;
58 	if (fp + sizeof(uint64_t) * 3 > stack + stack_size)
59 		return false;
60 
61 	frame->sp = fp + 0x10;
62 	/* FP to previous frame (X29) */
63 	if (!copy_in_reg(&frame->fp, fp))
64 		return false;
65 	if (!frame->fp)
66 		return false;
67 	/* LR (X30) */
68 	if (!copy_in_reg(&frame->pc, fp + 8))
69 		return false;
70 
71 	pauth_strip_pac(&frame->pc);
72 
73 	ftrace_map_lr(&frame->pc);
74 
75 	frame->pc -= 4;
76 
77 	return true;
78 }
79 
print_stack_arm64(struct unwind_state_arm64 * state,vaddr_t stack,size_t stack_size)80 void print_stack_arm64(struct unwind_state_arm64 *state,
81 		       vaddr_t stack, size_t stack_size)
82 {
83 	int width = 8;
84 
85 	trace_printf_helper_raw(TRACE_ERROR, true, "Call stack:");
86 
87 	ftrace_map_lr(&state->pc);
88 	do {
89 		trace_printf_helper_raw(TRACE_ERROR, true, " 0x%0*"PRIx64,
90 					width, state->pc);
91 	} while (unwind_stack_arm64(state, stack, stack_size));
92 }
93