xref: /openbmc/linux/arch/arm64/kernel/stacktrace.c (revision c4f7ac64)
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 
45 	/*
46 	 * Prime the first unwind.
47 	 *
48 	 * In unwind_frame() we'll check that the FP points to a valid stack,
49 	 * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be
50 	 * treated as a transition to whichever stack that happens to be. The
51 	 * prev_fp value won't be used, but we set it to 0 such that it is
52 	 * definitely not an accessible stack address.
53 	 */
54 	bitmap_zero(frame->stacks_done, __NR_STACK_TYPES);
55 	frame->prev_fp = 0;
56 	frame->prev_type = STACK_TYPE_UNKNOWN;
57 }
58 
59 /*
60  * Unwind from one frame record (A) to the next frame record (B).
61  *
62  * We terminate early if the location of B indicates a malformed chain of frame
63  * records (e.g. a cycle), determined based on the location and fp value of A
64  * and the location (but not the fp value) of B.
65  */
66 int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
67 {
68 	unsigned long fp = frame->fp;
69 	struct stack_info info;
70 
71 	if (fp & 0xf)
72 		return -EINVAL;
73 
74 	if (!tsk)
75 		tsk = current;
76 
77 	if (!on_accessible_stack(tsk, fp, &info))
78 		return -EINVAL;
79 
80 	if (test_bit(info.type, frame->stacks_done))
81 		return -EINVAL;
82 
83 	/*
84 	 * As stacks grow downward, any valid record on the same stack must be
85 	 * at a strictly higher address than the prior record.
86 	 *
87 	 * Stacks can nest in several valid orders, e.g.
88 	 *
89 	 * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
90 	 * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
91 	 *
92 	 * ... but the nesting itself is strict. Once we transition from one
93 	 * stack to another, it's never valid to unwind back to that first
94 	 * stack.
95 	 */
96 	if (info.type == frame->prev_type) {
97 		if (fp <= frame->prev_fp)
98 			return -EINVAL;
99 	} else {
100 		set_bit(frame->prev_type, frame->stacks_done);
101 	}
102 
103 	/*
104 	 * Record this frame record's values and location. The prev_fp and
105 	 * prev_type are only meaningful to the next unwind_frame() invocation.
106 	 */
107 	frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
108 	frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
109 	frame->prev_fp = fp;
110 	frame->prev_type = info.type;
111 
112 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
113 	if (tsk->ret_stack &&
114 		(ptrauth_strip_insn_pac(frame->pc) == (unsigned long)return_to_handler)) {
115 		struct ftrace_ret_stack *ret_stack;
116 		/*
117 		 * This is a case where function graph tracer has
118 		 * modified a return address (LR) in a stack frame
119 		 * to hook a function return.
120 		 * So replace it to an original value.
121 		 */
122 		ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph++);
123 		if (WARN_ON_ONCE(!ret_stack))
124 			return -EINVAL;
125 		frame->pc = ret_stack->ret;
126 	}
127 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
128 
129 	frame->pc = ptrauth_strip_insn_pac(frame->pc);
130 
131 	/*
132 	 * This is a terminal record, so we have finished unwinding.
133 	 */
134 	if (!frame->fp && !frame->pc)
135 		return -ENOENT;
136 
137 	return 0;
138 }
139 NOKPROBE_SYMBOL(unwind_frame);
140 
141 void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
142 			     bool (*fn)(void *, unsigned long), void *data)
143 {
144 	while (1) {
145 		int ret;
146 
147 		if (!fn(data, frame->pc))
148 			break;
149 		ret = unwind_frame(tsk, frame);
150 		if (ret < 0)
151 			break;
152 	}
153 }
154 NOKPROBE_SYMBOL(walk_stackframe);
155 
156 static void dump_backtrace_entry(unsigned long where, const char *loglvl)
157 {
158 	printk("%s %pS\n", loglvl, (void *)where);
159 }
160 
161 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
162 		    const char *loglvl)
163 {
164 	struct stackframe frame;
165 	int skip = 0;
166 
167 	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
168 
169 	if (regs) {
170 		if (user_mode(regs))
171 			return;
172 		skip = 1;
173 	}
174 
175 	if (!tsk)
176 		tsk = current;
177 
178 	if (!try_get_task_stack(tsk))
179 		return;
180 
181 	if (tsk == current) {
182 		start_backtrace(&frame,
183 				(unsigned long)__builtin_frame_address(0),
184 				(unsigned long)dump_backtrace);
185 	} else {
186 		/*
187 		 * task blocked in __switch_to
188 		 */
189 		start_backtrace(&frame,
190 				thread_saved_fp(tsk),
191 				thread_saved_pc(tsk));
192 	}
193 
194 	printk("%sCall trace:\n", loglvl);
195 	do {
196 		/* skip until specified stack frame */
197 		if (!skip) {
198 			dump_backtrace_entry(frame.pc, loglvl);
199 		} else if (frame.fp == regs->regs[29]) {
200 			skip = 0;
201 			/*
202 			 * Mostly, this is the case where this function is
203 			 * called in panic/abort. As exception handler's
204 			 * stack frame does not contain the corresponding pc
205 			 * at which an exception has taken place, use regs->pc
206 			 * instead.
207 			 */
208 			dump_backtrace_entry(regs->pc, loglvl);
209 		}
210 	} while (!unwind_frame(tsk, &frame));
211 
212 	put_task_stack(tsk);
213 }
214 
215 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
216 {
217 	dump_backtrace(NULL, tsk, loglvl);
218 	barrier();
219 }
220 
221 #ifdef CONFIG_STACKTRACE
222 
223 noinline void arch_stack_walk(stack_trace_consume_fn consume_entry,
224 			      void *cookie, struct task_struct *task,
225 			      struct pt_regs *regs)
226 {
227 	struct stackframe frame;
228 
229 	if (regs)
230 		start_backtrace(&frame, regs->regs[29], regs->pc);
231 	else if (task == current)
232 		start_backtrace(&frame,
233 				(unsigned long)__builtin_frame_address(1),
234 				(unsigned long)__builtin_return_address(0));
235 	else
236 		start_backtrace(&frame, thread_saved_fp(task),
237 				thread_saved_pc(task));
238 
239 	walk_stackframe(task, &frame, consume_entry, cookie);
240 }
241 
242 #endif
243