• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/efi.h>
9 #include <linux/export.h>
10 #include <linux/ftrace.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/efi.h>
17 #include <asm/irq.h>
18 #include <asm/stack_pointer.h>
19 #include <asm/stacktrace.h>
20 
21 /*
22  * Start an unwind from a pt_regs.
23  *
24  * The unwind will begin at the PC within the regs.
25  *
26  * The regs must be on a stack currently owned by the calling task.
27  */
28 static __always_inline void
unwind_init_from_regs(struct unwind_state * state,struct pt_regs * regs)29 unwind_init_from_regs(struct unwind_state *state,
30 		      struct pt_regs *regs)
31 {
32 	unwind_init_common(state, current);
33 
34 	state->fp = regs->regs[29];
35 	state->pc = regs->pc;
36 }
37 
38 /*
39  * Start an unwind from a caller.
40  *
41  * The unwind will begin at the caller of whichever function this is inlined
42  * into.
43  *
44  * The function which invokes this must be noinline.
45  */
46 static __always_inline void
unwind_init_from_caller(struct unwind_state * state)47 unwind_init_from_caller(struct unwind_state *state)
48 {
49 	unwind_init_common(state, current);
50 
51 	state->fp = (unsigned long)__builtin_frame_address(1);
52 	state->pc = (unsigned long)__builtin_return_address(0);
53 }
54 
55 /*
56  * Start an unwind from a blocked task.
57  *
58  * The unwind will begin at the blocked tasks saved PC (i.e. the caller of
59  * cpu_switch_to()).
60  *
61  * The caller should ensure the task is blocked in cpu_switch_to() for the
62  * duration of the unwind, or the unwind will be bogus. It is never valid to
63  * call this for the current task.
64  */
65 static __always_inline void
unwind_init_from_task(struct unwind_state * state,struct task_struct * task)66 unwind_init_from_task(struct unwind_state *state,
67 		      struct task_struct *task)
68 {
69 	unwind_init_common(state, task);
70 
71 	state->fp = thread_saved_fp(task);
72 	state->pc = thread_saved_pc(task);
73 }
74 
75 static __always_inline int
unwind_recover_return_address(struct unwind_state * state)76 unwind_recover_return_address(struct unwind_state *state)
77 {
78 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
79 	if (state->task->ret_stack &&
80 	    (state->pc == (unsigned long)return_to_handler)) {
81 		unsigned long orig_pc;
82 		orig_pc = ftrace_graph_ret_addr(state->task, NULL, state->pc,
83 						(void *)state->fp);
84 		if (WARN_ON_ONCE(state->pc == orig_pc))
85 			return -EINVAL;
86 		state->pc = orig_pc;
87 	}
88 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
89 
90 #ifdef CONFIG_KRETPROBES
91 	if (is_kretprobe_trampoline(state->pc)) {
92 		state->pc = kretprobe_find_ret_addr(state->task,
93 						    (void *)state->fp,
94 						    &state->kr_cur);
95 	}
96 #endif /* CONFIG_KRETPROBES */
97 
98 	return 0;
99 }
100 
101 /*
102  * Unwind from one frame record (A) to the next frame record (B).
103  *
104  * We terminate early if the location of B indicates a malformed chain of frame
105  * records (e.g. a cycle), determined based on the location and fp value of A
106  * and the location (but not the fp value) of B.
107  */
108 static __always_inline int
unwind_next(struct unwind_state * state)109 unwind_next(struct unwind_state *state)
110 {
111 	struct task_struct *tsk = state->task;
112 	unsigned long fp = state->fp;
113 	int err;
114 
115 	/* Final frame; nothing to unwind */
116 	if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
117 		return -ENOENT;
118 
119 	err = unwind_next_frame_record(state);
120 	if (err)
121 		return err;
122 
123 	state->pc = ptrauth_strip_kernel_insn_pac(state->pc);
124 
125 	return unwind_recover_return_address(state);
126 }
127 
128 static __always_inline void
unwind(struct unwind_state * state,stack_trace_consume_fn consume_entry,void * cookie)129 unwind(struct unwind_state *state, stack_trace_consume_fn consume_entry,
130        void *cookie)
131 {
132 	if (unwind_recover_return_address(state))
133 		return;
134 
135 	while (1) {
136 		int ret;
137 
138 		if (!consume_entry(cookie, state->pc))
139 			break;
140 		ret = unwind_next(state);
141 		if (ret < 0)
142 			break;
143 	}
144 }
145 
146 /*
147  * Per-cpu stacks are only accessible when unwinding the current task in a
148  * non-preemptible context.
149  */
150 #define STACKINFO_CPU(name)					\
151 	({							\
152 		((task == current) && !preemptible())		\
153 			? stackinfo_get_##name()		\
154 			: stackinfo_get_unknown();		\
155 	})
156 
157 /*
158  * SDEI stacks are only accessible when unwinding the current task in an NMI
159  * context.
160  */
161 #define STACKINFO_SDEI(name)					\
162 	({							\
163 		((task == current) && in_nmi())			\
164 			? stackinfo_get_sdei_##name()		\
165 			: stackinfo_get_unknown();		\
166 	})
167 
168 #define STACKINFO_EFI						\
169 	({							\
170 		((task == current) && current_in_efi())		\
171 			? stackinfo_get_efi()			\
172 			: stackinfo_get_unknown();		\
173 	})
174 
arch_stack_walk(stack_trace_consume_fn consume_entry,void * cookie,struct task_struct * task,struct pt_regs * regs)175 noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry,
176 			      void *cookie, struct task_struct *task,
177 			      struct pt_regs *regs)
178 {
179 	struct stack_info stacks[] = {
180 		stackinfo_get_task(task),
181 		STACKINFO_CPU(irq),
182 #if defined(CONFIG_VMAP_STACK)
183 		STACKINFO_CPU(overflow),
184 #endif
185 #if defined(CONFIG_VMAP_STACK) && defined(CONFIG_ARM_SDE_INTERFACE)
186 		STACKINFO_SDEI(normal),
187 		STACKINFO_SDEI(critical),
188 #endif
189 #ifdef CONFIG_EFI
190 		STACKINFO_EFI,
191 #endif
192 	};
193 	struct unwind_state state = {
194 		.stacks = stacks,
195 		.nr_stacks = ARRAY_SIZE(stacks),
196 	};
197 
198 	if (regs) {
199 		if (task != current)
200 			return;
201 		unwind_init_from_regs(&state, regs);
202 	} else if (task == current) {
203 		unwind_init_from_caller(&state);
204 	} else {
205 		unwind_init_from_task(&state, task);
206 	}
207 
208 	unwind(&state, consume_entry, cookie);
209 }
210 EXPORT_SYMBOL_GPL(arch_stack_walk);
211 
dump_backtrace_entry(void * arg,unsigned long where)212 static bool dump_backtrace_entry(void *arg, unsigned long where)
213 {
214 	char *loglvl = arg;
215 	printk("%s %pSb\n", loglvl, (void *)where);
216 	return true;
217 }
218 
dump_backtrace(struct pt_regs * regs,struct task_struct * tsk,const char * loglvl)219 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
220 		    const char *loglvl)
221 {
222 	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
223 
224 	if (regs && user_mode(regs))
225 		return;
226 
227 	if (!tsk)
228 		tsk = current;
229 
230 	if (!try_get_task_stack(tsk))
231 		return;
232 
233 	printk("%sCall trace:\n", loglvl);
234 	arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
235 
236 	put_task_stack(tsk);
237 }
238 EXPORT_SYMBOL_GPL(dump_backtrace);
239 
show_stack(struct task_struct * tsk,unsigned long * sp,const char * loglvl)240 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
241 {
242 	dump_backtrace(NULL, tsk, loglvl);
243 	barrier();
244 }
245 
246 /*
247  * The struct defined for userspace stack frame in AARCH64 mode.
248  */
249 struct frame_tail {
250 	struct frame_tail	__user *fp;
251 	unsigned long		lr;
252 } __attribute__((packed));
253 
254 /*
255  * Get the return address for a single stackframe and return a pointer to the
256  * next frame tail.
257  */
258 static struct frame_tail __user *
unwind_user_frame(struct frame_tail __user * tail,void * cookie,stack_trace_consume_fn consume_entry)259 unwind_user_frame(struct frame_tail __user *tail, void *cookie,
260 	       stack_trace_consume_fn consume_entry)
261 {
262 	struct frame_tail buftail;
263 	unsigned long err;
264 	unsigned long lr;
265 
266 	/* Also check accessibility of one struct frame_tail beyond */
267 	if (!access_ok(tail, sizeof(buftail)))
268 		return NULL;
269 
270 	pagefault_disable();
271 	err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
272 	pagefault_enable();
273 
274 	if (err)
275 		return NULL;
276 
277 	lr = ptrauth_strip_user_insn_pac(buftail.lr);
278 
279 	if (!consume_entry(cookie, lr))
280 		return NULL;
281 
282 	/*
283 	 * Frame pointers should strictly progress back up the stack
284 	 * (towards higher addresses).
285 	 */
286 	if (tail >= buftail.fp)
287 		return NULL;
288 
289 	return buftail.fp;
290 }
291 
292 #ifdef CONFIG_COMPAT
293 /*
294  * The registers we're interested in are at the end of the variable
295  * length saved register structure. The fp points at the end of this
296  * structure so the address of this struct is:
297  * (struct compat_frame_tail *)(xxx->fp)-1
298  *
299  * This code has been adapted from the ARM OProfile support.
300  */
301 struct compat_frame_tail {
302 	compat_uptr_t	fp; /* a (struct compat_frame_tail *) in compat mode */
303 	u32		sp;
304 	u32		lr;
305 } __attribute__((packed));
306 
307 static struct compat_frame_tail __user *
unwind_compat_user_frame(struct compat_frame_tail __user * tail,void * cookie,stack_trace_consume_fn consume_entry)308 unwind_compat_user_frame(struct compat_frame_tail __user *tail, void *cookie,
309 				stack_trace_consume_fn consume_entry)
310 {
311 	struct compat_frame_tail buftail;
312 	unsigned long err;
313 
314 	/* Also check accessibility of one struct frame_tail beyond */
315 	if (!access_ok(tail, sizeof(buftail)))
316 		return NULL;
317 
318 	pagefault_disable();
319 	err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
320 	pagefault_enable();
321 
322 	if (err)
323 		return NULL;
324 
325 	if (!consume_entry(cookie, buftail.lr))
326 		return NULL;
327 
328 	/*
329 	 * Frame pointers should strictly progress back up the stack
330 	 * (towards higher addresses).
331 	 */
332 	if (tail + 1 >= (struct compat_frame_tail __user *)
333 			compat_ptr(buftail.fp))
334 		return NULL;
335 
336 	return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
337 }
338 #endif /* CONFIG_COMPAT */
339 
340 
arch_stack_walk_user(stack_trace_consume_fn consume_entry,void * cookie,const struct pt_regs * regs)341 void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
342 					const struct pt_regs *regs)
343 {
344 	if (!consume_entry(cookie, regs->pc))
345 		return;
346 
347 	if (!compat_user_mode(regs)) {
348 		/* AARCH64 mode */
349 		struct frame_tail __user *tail;
350 
351 		tail = (struct frame_tail __user *)regs->regs[29];
352 		while (tail && !((unsigned long)tail & 0x7))
353 			tail = unwind_user_frame(tail, cookie, consume_entry);
354 	} else {
355 #ifdef CONFIG_COMPAT
356 		/* AARCH32 compat mode */
357 		struct compat_frame_tail __user *tail;
358 
359 		tail = (struct compat_frame_tail __user *)regs->compat_fp - 1;
360 		while (tail && !((unsigned long)tail & 0x3))
361 			tail = unwind_compat_user_frame(tail, cookie, consume_entry);
362 #endif
363 	}
364 }
365