xref: /openbmc/linux/arch/arm64/kernel/stacktrace.c (revision fe7498ef)
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/sched.h>
12 #include <linux/sched/debug.h>
13 #include <linux/sched/task_stack.h>
14 #include <linux/stacktrace.h>
15 
16 #include <asm/irq.h>
17 #include <asm/pointer_auth.h>
18 #include <asm/stack_pointer.h>
19 #include <asm/stacktrace.h>
20 
21 /*
22  * AArch64 PCS assigns the frame pointer to x29.
23  *
24  * A simple function prologue looks like this:
25  * 	sub	sp, sp, #0x10
26  *   	stp	x29, x30, [sp]
27  *	mov	x29, sp
28  *
29  * A simple function epilogue looks like this:
30  *	mov	sp, x29
31  *	ldp	x29, x30, [sp]
32  *	add	sp, sp, #0x10
33  */
34 
35 
36 void start_backtrace(struct stackframe *frame, unsigned long fp,
37 		     unsigned long pc)
38 {
39 	frame->fp = fp;
40 	frame->pc = pc;
41 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
42 	frame->graph = 0;
43 #endif
44 #ifdef CONFIG_KRETPROBES
45 	frame->kr_cur = NULL;
46 #endif
47 
48 	/*
49 	 * Prime the first unwind.
50 	 *
51 	 * In unwind_frame() we'll check that the FP points to a valid stack,
52 	 * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be
53 	 * treated as a transition to whichever stack that happens to be. The
54 	 * prev_fp value won't be used, but we set it to 0 such that it is
55 	 * definitely not an accessible stack address.
56 	 */
57 	bitmap_zero(frame->stacks_done, __NR_STACK_TYPES);
58 	frame->prev_fp = 0;
59 	frame->prev_type = STACK_TYPE_UNKNOWN;
60 }
61 
62 /*
63  * Unwind from one frame record (A) to the next frame record (B).
64  *
65  * We terminate early if the location of B indicates a malformed chain of frame
66  * records (e.g. a cycle), determined based on the location and fp value of A
67  * and the location (but not the fp value) of B.
68  */
69 int notrace unwind_frame(struct task_struct *tsk, 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 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
120 	if (tsk->ret_stack &&
121 		(ptrauth_strip_insn_pac(frame->pc) == (unsigned long)return_to_handler)) {
122 		struct ftrace_ret_stack *ret_stack;
123 		/*
124 		 * This is a case where function graph tracer has
125 		 * modified a return address (LR) in a stack frame
126 		 * to hook a function return.
127 		 * So replace it to an original value.
128 		 */
129 		ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph++);
130 		if (WARN_ON_ONCE(!ret_stack))
131 			return -EINVAL;
132 		frame->pc = ret_stack->ret;
133 	}
134 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
135 #ifdef CONFIG_KRETPROBES
136 	if (is_kretprobe_trampoline(frame->pc))
137 		frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur);
138 #endif
139 
140 	frame->pc = ptrauth_strip_insn_pac(frame->pc);
141 
142 	return 0;
143 }
144 NOKPROBE_SYMBOL(unwind_frame);
145 
146 void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
147 			     bool (*fn)(void *, unsigned long), void *data)
148 {
149 	while (1) {
150 		int ret;
151 
152 		if (!fn(data, frame->pc))
153 			break;
154 		ret = unwind_frame(tsk, frame);
155 		if (ret < 0)
156 			break;
157 	}
158 }
159 NOKPROBE_SYMBOL(walk_stackframe);
160 
161 static void dump_backtrace_entry(unsigned long where, const char *loglvl)
162 {
163 	printk("%s %pSb\n", loglvl, (void *)where);
164 }
165 
166 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
167 		    const char *loglvl)
168 {
169 	struct stackframe frame;
170 	int skip = 0;
171 
172 	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
173 
174 	if (regs) {
175 		if (user_mode(regs))
176 			return;
177 		skip = 1;
178 	}
179 
180 	if (!tsk)
181 		tsk = current;
182 
183 	if (!try_get_task_stack(tsk))
184 		return;
185 
186 	if (tsk == current) {
187 		start_backtrace(&frame,
188 				(unsigned long)__builtin_frame_address(0),
189 				(unsigned long)dump_backtrace);
190 	} else {
191 		/*
192 		 * task blocked in __switch_to
193 		 */
194 		start_backtrace(&frame,
195 				thread_saved_fp(tsk),
196 				thread_saved_pc(tsk));
197 	}
198 
199 	printk("%sCall trace:\n", loglvl);
200 	do {
201 		/* skip until specified stack frame */
202 		if (!skip) {
203 			dump_backtrace_entry(frame.pc, loglvl);
204 		} else if (frame.fp == regs->regs[29]) {
205 			skip = 0;
206 			/*
207 			 * Mostly, this is the case where this function is
208 			 * called in panic/abort. As exception handler's
209 			 * stack frame does not contain the corresponding pc
210 			 * at which an exception has taken place, use regs->pc
211 			 * instead.
212 			 */
213 			dump_backtrace_entry(regs->pc, loglvl);
214 		}
215 	} while (!unwind_frame(tsk, &frame));
216 
217 	put_task_stack(tsk);
218 }
219 
220 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
221 {
222 	dump_backtrace(NULL, tsk, loglvl);
223 	barrier();
224 }
225 
226 #ifdef CONFIG_STACKTRACE
227 
228 noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
229 			      void *cookie, struct task_struct *task,
230 			      struct pt_regs *regs)
231 {
232 	struct stackframe frame;
233 
234 	if (regs)
235 		start_backtrace(&frame, regs->regs[29], regs->pc);
236 	else if (task == current)
237 		start_backtrace(&frame,
238 				(unsigned long)__builtin_frame_address(1),
239 				(unsigned long)__builtin_return_address(0));
240 	else
241 		start_backtrace(&frame, thread_saved_fp(task),
242 				thread_saved_pc(task));
243 
244 	walk_stackframe(task, &frame, consume_entry, cookie);
245 }
246 
247 #endif
248