1 // SPDX-License-Identifier: GPL-2.0-only
3 * Stack tracing support
5 * Copyright (C) 2012 ARM Ltd.
7 #include <linux/kernel.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>
18 #include <asm/stack_pointer.h>
19 #include <asm/stacktrace.h>
22 * Start an unwind from a pt_regs.
24 * The unwind will begin at the PC within the regs.
26 * The regs must be on a stack currently owned by the calling task.
28 static __always_inline void unwind_init_from_regs(struct unwind_state *state,
31 unwind_init_common(state, current);
33 state->fp = regs->regs[29];
38 * Start an unwind from a caller.
40 * The unwind will begin at the caller of whichever function this is inlined
43 * The function which invokes this must be noinline.
45 static __always_inline void unwind_init_from_caller(struct unwind_state *state)
47 unwind_init_common(state, current);
49 state->fp = (unsigned long)__builtin_frame_address(1);
50 state->pc = (unsigned long)__builtin_return_address(0);
54 * Start an unwind from a blocked task.
56 * The unwind will begin at the blocked tasks saved PC (i.e. the caller of
59 * The caller should ensure the task is blocked in cpu_switch_to() for the
60 * duration of the unwind, or the unwind will be bogus. It is never valid to
61 * call this for the current task.
63 static __always_inline void unwind_init_from_task(struct unwind_state *state,
64 struct task_struct *task)
66 unwind_init_common(state, task);
68 state->fp = thread_saved_fp(task);
69 state->pc = thread_saved_pc(task);
73 * Unwind from one frame record (A) to the next frame record (B).
75 * We terminate early if the location of B indicates a malformed chain of frame
76 * records (e.g. a cycle), determined based on the location and fp value of A
77 * and the location (but not the fp value) of B.
79 static int notrace unwind_next(struct unwind_state *state)
81 struct task_struct *tsk = state->task;
82 unsigned long fp = state->fp;
85 /* Final frame; nothing to unwind */
86 if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
89 err = unwind_next_frame_record(state);
93 state->pc = ptrauth_strip_insn_pac(state->pc);
95 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
97 (state->pc == (unsigned long)return_to_handler)) {
98 unsigned long orig_pc;
100 * This is a case where function graph tracer has
101 * modified a return address (LR) in a stack frame
102 * to hook a function return.
103 * So replace it to an original value.
105 orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc,
107 if (WARN_ON_ONCE(state->pc == orig_pc))
111 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
112 #ifdef CONFIG_KRETPROBES
113 if (is_kretprobe_trampoline(state->pc))
114 state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur);
119 NOKPROBE_SYMBOL(unwind_next);
121 static void notrace unwind(struct unwind_state *state,
122 stack_trace_consume_fn consume_entry, void *cookie)
127 if (!consume_entry(cookie, state->pc))
129 ret = unwind_next(state);
134 NOKPROBE_SYMBOL(unwind);
136 static bool dump_backtrace_entry(void *arg, unsigned long where)
139 printk("%s %pSb\n", loglvl, (void *)where);
143 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
146 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
148 if (regs && user_mode(regs))
154 if (!try_get_task_stack(tsk))
157 printk("%sCall trace:\n", loglvl);
158 arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
163 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
165 dump_backtrace(NULL, tsk, loglvl);
170 * Per-cpu stacks are only accessible when unwinding the current task in a
171 * non-preemptible context.
173 #define STACKINFO_CPU(name) \
175 ((task == current) && !preemptible()) \
176 ? stackinfo_get_##name() \
177 : stackinfo_get_unknown(); \
181 * SDEI stacks are only accessible when unwinding the current task in an NMI
184 #define STACKINFO_SDEI(name) \
186 ((task == current) && in_nmi()) \
187 ? stackinfo_get_sdei_##name() \
188 : stackinfo_get_unknown(); \
191 #define STACKINFO_EFI \
193 ((task == current) && current_in_efi()) \
194 ? stackinfo_get_efi() \
195 : stackinfo_get_unknown(); \
198 noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry,
199 void *cookie, struct task_struct *task,
200 struct pt_regs *regs)
202 struct stack_info stacks[] = {
203 stackinfo_get_task(task),
205 #if defined(CONFIG_VMAP_STACK)
206 STACKINFO_CPU(overflow),
208 #if defined(CONFIG_VMAP_STACK) && defined(CONFIG_ARM_SDE_INTERFACE)
209 STACKINFO_SDEI(normal),
210 STACKINFO_SDEI(critical),
216 struct unwind_state state = {
218 .nr_stacks = ARRAY_SIZE(stacks),
224 unwind_init_from_regs(&state, regs);
225 } else if (task == current) {
226 unwind_init_from_caller(&state);
228 unwind_init_from_task(&state, task);
231 unwind(&state, consume_entry, cookie);