xref: /openbmc/linux/arch/arm64/kernel/stacktrace.c (revision 592700f0)
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/sched.h>
11 #include <linux/sched/debug.h>
12 #include <linux/sched/task_stack.h>
13 #include <linux/stacktrace.h>
14 
15 #include <asm/irq.h>
16 #include <asm/stack_pointer.h>
17 #include <asm/stacktrace.h>
18 
19 /*
20  * AArch64 PCS assigns the frame pointer to x29.
21  *
22  * A simple function prologue looks like this:
23  * 	sub	sp, sp, #0x10
24  *   	stp	x29, x30, [sp]
25  *	mov	x29, sp
26  *
27  * A simple function epilogue looks like this:
28  *	mov	sp, x29
29  *	ldp	x29, x30, [sp]
30  *	add	sp, sp, #0x10
31  */
32 
33 /*
34  * Unwind from one frame record (A) to the next frame record (B).
35  *
36  * We terminate early if the location of B indicates a malformed chain of frame
37  * records (e.g. a cycle), determined based on the location and fp value of A
38  * and the location (but not the fp value) of B.
39  */
40 int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
41 {
42 	unsigned long fp = frame->fp;
43 	struct stack_info info;
44 
45 	if (fp & 0xf)
46 		return -EINVAL;
47 
48 	if (!tsk)
49 		tsk = current;
50 
51 	if (!on_accessible_stack(tsk, fp, &info))
52 		return -EINVAL;
53 
54 	if (test_bit(info.type, frame->stacks_done))
55 		return -EINVAL;
56 
57 	/*
58 	 * As stacks grow downward, any valid record on the same stack must be
59 	 * at a strictly higher address than the prior record.
60 	 *
61 	 * Stacks can nest in several valid orders, e.g.
62 	 *
63 	 * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
64 	 * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
65 	 *
66 	 * ... but the nesting itself is strict. Once we transition from one
67 	 * stack to another, it's never valid to unwind back to that first
68 	 * stack.
69 	 */
70 	if (info.type == frame->prev_type) {
71 		if (fp <= frame->prev_fp)
72 			return -EINVAL;
73 	} else {
74 		set_bit(frame->prev_type, frame->stacks_done);
75 	}
76 
77 	/*
78 	 * Record this frame record's values and location. The prev_fp and
79 	 * prev_type are only meaningful to the next unwind_frame() invocation.
80 	 */
81 	frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
82 	frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
83 	frame->prev_fp = fp;
84 	frame->prev_type = info.type;
85 
86 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
87 	if (tsk->ret_stack &&
88 			(frame->pc == (unsigned long)return_to_handler)) {
89 		struct ftrace_ret_stack *ret_stack;
90 		/*
91 		 * This is a case where function graph tracer has
92 		 * modified a return address (LR) in a stack frame
93 		 * to hook a function return.
94 		 * So replace it to an original value.
95 		 */
96 		ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph++);
97 		if (WARN_ON_ONCE(!ret_stack))
98 			return -EINVAL;
99 		frame->pc = ret_stack->ret;
100 	}
101 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
102 
103 	/*
104 	 * Frames created upon entry from EL0 have NULL FP and PC values, so
105 	 * don't bother reporting these. Frames created by __noreturn functions
106 	 * might have a valid FP even if PC is bogus, so only terminate where
107 	 * both are NULL.
108 	 */
109 	if (!frame->fp && !frame->pc)
110 		return -EINVAL;
111 
112 	return 0;
113 }
114 
115 void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
116 		     int (*fn)(struct stackframe *, void *), void *data)
117 {
118 	while (1) {
119 		int ret;
120 
121 		if (fn(frame, data))
122 			break;
123 		ret = unwind_frame(tsk, frame);
124 		if (ret < 0)
125 			break;
126 	}
127 }
128 
129 #ifdef CONFIG_STACKTRACE
130 struct stack_trace_data {
131 	struct stack_trace *trace;
132 	unsigned int no_sched_functions;
133 	unsigned int skip;
134 };
135 
136 static int save_trace(struct stackframe *frame, void *d)
137 {
138 	struct stack_trace_data *data = d;
139 	struct stack_trace *trace = data->trace;
140 	unsigned long addr = frame->pc;
141 
142 	if (data->no_sched_functions && in_sched_functions(addr))
143 		return 0;
144 	if (data->skip) {
145 		data->skip--;
146 		return 0;
147 	}
148 
149 	trace->entries[trace->nr_entries++] = addr;
150 
151 	return trace->nr_entries >= trace->max_entries;
152 }
153 
154 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
155 {
156 	struct stack_trace_data data;
157 	struct stackframe frame;
158 
159 	data.trace = trace;
160 	data.skip = trace->skip;
161 	data.no_sched_functions = 0;
162 
163 	start_backtrace(&frame, regs->regs[29], regs->pc);
164 	walk_stackframe(current, &frame, save_trace, &data);
165 }
166 EXPORT_SYMBOL_GPL(save_stack_trace_regs);
167 
168 static noinline void __save_stack_trace(struct task_struct *tsk,
169 	struct stack_trace *trace, unsigned int nosched)
170 {
171 	struct stack_trace_data data;
172 	struct stackframe frame;
173 
174 	if (!try_get_task_stack(tsk))
175 		return;
176 
177 	data.trace = trace;
178 	data.skip = trace->skip;
179 	data.no_sched_functions = nosched;
180 
181 	if (tsk != current) {
182 		start_backtrace(&frame, thread_saved_fp(tsk),
183 				thread_saved_pc(tsk));
184 	} else {
185 		/* We don't want this function nor the caller */
186 		data.skip += 2;
187 		start_backtrace(&frame,
188 				(unsigned long)__builtin_frame_address(0),
189 				(unsigned long)__save_stack_trace);
190 	}
191 
192 	walk_stackframe(tsk, &frame, save_trace, &data);
193 
194 	put_task_stack(tsk);
195 }
196 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
197 
198 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
199 {
200 	__save_stack_trace(tsk, trace, 1);
201 }
202 
203 void save_stack_trace(struct stack_trace *trace)
204 {
205 	__save_stack_trace(current, trace, 0);
206 }
207 
208 EXPORT_SYMBOL_GPL(save_stack_trace);
209 #endif
210