xref: /linux/arch/arm64/kernel/stacktrace.c (revision 00389c58ffe993782a8ba4bb5a34a102b1f6fe24)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Stack tracing support
4  *
5  * Copyright (C) 2012 ARM Ltd.
6  */
7 #include <linux/kernel.h>
8 #include <linux/export.h>
9 #include <linux/ftrace.h>
10 #include <linux/kprobes.h>
11 #include <linux/rethook.h>
12 #include <linux/sched.h>
13 #include <linux/sched/debug.h>
14 #include <linux/sched/task_stack.h>
15 #include <linux/stacktrace.h>
16 
17 #include <asm/irq.h>
18 #include <asm/pointer_auth.h>
19 #include <asm/stack_pointer.h>
20 #include <asm/stacktrace.h>
21 
22 /*
23  * AArch64 PCS assigns the frame pointer to x29.
24  *
25  * A simple function prologue looks like this:
26  * 	sub	sp, sp, #0x10
27  *   	stp	x29, x30, [sp]
28  *	mov	x29, sp
29  *
30  * A simple function epilogue looks like this:
31  *	mov	sp, x29
32  *	ldp	x29, x30, [sp]
33  *	add	sp, sp, #0x10
34  */
35 
36 
37 static notrace void start_backtrace(struct stackframe *frame, unsigned long fp,
38 				    unsigned long pc)
39 {
40 	frame->fp = fp;
41 	frame->pc = pc;
42 #if defined(CONFIG_KRETPROBES) || defined(CONFIG_RETHOOK)
43 	frame->kr_cur = NULL;
44 #endif
45 
46 	/*
47 	 * Prime the first unwind.
48 	 *
49 	 * In unwind_frame() we'll check that the FP points to a valid stack,
50 	 * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be
51 	 * treated as a transition to whichever stack that happens to be. The
52 	 * prev_fp value won't be used, but we set it to 0 such that it is
53 	 * definitely not an accessible stack address.
54 	 */
55 	bitmap_zero(frame->stacks_done, __NR_STACK_TYPES);
56 	frame->prev_fp = 0;
57 	frame->prev_type = STACK_TYPE_UNKNOWN;
58 }
59 NOKPROBE_SYMBOL(start_backtrace);
60 
61 /*
62  * Unwind from one frame record (A) to the next frame record (B).
63  *
64  * We terminate early if the location of B indicates a malformed chain of frame
65  * records (e.g. a cycle), determined based on the location and fp value of A
66  * and the location (but not the fp value) of B.
67  */
68 static int notrace unwind_frame(struct task_struct *tsk,
69 				struct stackframe *frame)
70 {
71 	unsigned long fp = frame->fp;
72 	struct stack_info info;
73 
74 	if (!tsk)
75 		tsk = current;
76 
77 	/* Final frame; nothing to unwind */
78 	if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
79 		return -ENOENT;
80 
81 	if (fp & 0x7)
82 		return -EINVAL;
83 
84 	if (!on_accessible_stack(tsk, fp, 16, &info))
85 		return -EINVAL;
86 
87 	if (test_bit(info.type, frame->stacks_done))
88 		return -EINVAL;
89 
90 	/*
91 	 * As stacks grow downward, any valid record on the same stack must be
92 	 * at a strictly higher address than the prior record.
93 	 *
94 	 * Stacks can nest in several valid orders, e.g.
95 	 *
96 	 * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
97 	 * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
98 	 *
99 	 * ... but the nesting itself is strict. Once we transition from one
100 	 * stack to another, it's never valid to unwind back to that first
101 	 * stack.
102 	 */
103 	if (info.type == frame->prev_type) {
104 		if (fp <= frame->prev_fp)
105 			return -EINVAL;
106 	} else {
107 		set_bit(frame->prev_type, frame->stacks_done);
108 	}
109 
110 	/*
111 	 * Record this frame record's values and location. The prev_fp and
112 	 * prev_type are only meaningful to the next unwind_frame() invocation.
113 	 */
114 	frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
115 	frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
116 	frame->prev_fp = fp;
117 	frame->prev_type = info.type;
118 
119 	frame->pc = ptrauth_strip_insn_pac(frame->pc);
120 
121 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
122 	if (tsk->ret_stack &&
123 		(frame->pc == (unsigned long)return_to_handler)) {
124 		unsigned long orig_pc;
125 		/*
126 		 * This is a case where function graph tracer has
127 		 * modified a return address (LR) in a stack frame
128 		 * to hook a function return.
129 		 * So replace it to an original value.
130 		 */
131 		orig_pc = ftrace_graph_ret_addr(tsk, NULL, frame->pc,
132 						(void *)frame->fp);
133 		if (WARN_ON_ONCE(frame->pc == orig_pc))
134 			return -EINVAL;
135 		frame->pc = orig_pc;
136 	}
137 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
138 #ifdef CONFIG_KRETPROBES
139 	if (is_kretprobe_trampoline(frame->pc))
140 		frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur);
141 #endif
142 #ifdef CONFIG_RETHOOK
143 	if (is_rethook_trampoline(frame->pc))
144 		frame->pc = rethook_find_ret_addr(tsk, frame->fp, &frame->kr_cur);
145 #endif
146 
147 	return 0;
148 }
149 NOKPROBE_SYMBOL(unwind_frame);
150 
151 static void notrace walk_stackframe(struct task_struct *tsk,
152 				    struct stackframe *frame,
153 				    bool (*fn)(void *, unsigned long), void *data)
154 {
155 	while (1) {
156 		int ret;
157 
158 		if (!fn(data, frame->pc))
159 			break;
160 		ret = unwind_frame(tsk, frame);
161 		if (ret < 0)
162 			break;
163 	}
164 }
165 NOKPROBE_SYMBOL(walk_stackframe);
166 
167 static bool dump_backtrace_entry(void *arg, unsigned long where)
168 {
169 	char *loglvl = arg;
170 	printk("%s %pSb\n", loglvl, (void *)where);
171 	return true;
172 }
173 
174 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
175 		    const char *loglvl)
176 {
177 	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
178 
179 	if (regs && user_mode(regs))
180 		return;
181 
182 	if (!tsk)
183 		tsk = current;
184 
185 	if (!try_get_task_stack(tsk))
186 		return;
187 
188 	printk("%sCall trace:\n", loglvl);
189 	arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
190 
191 	put_task_stack(tsk);
192 }
193 
194 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
195 {
196 	dump_backtrace(NULL, tsk, loglvl);
197 	barrier();
198 }
199 
200 noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
201 			      void *cookie, struct task_struct *task,
202 			      struct pt_regs *regs)
203 {
204 	struct stackframe frame;
205 
206 	if (regs)
207 		start_backtrace(&frame, regs->regs[29], regs->pc);
208 	else if (task == current)
209 		start_backtrace(&frame,
210 				(unsigned long)__builtin_frame_address(1),
211 				(unsigned long)__builtin_return_address(0));
212 	else
213 		start_backtrace(&frame, thread_saved_fp(task),
214 				thread_saved_pc(task));
215 
216 	walk_stackframe(task, &frame, consume_entry, cookie);
217 }
218