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
29 unwind_init_from_regs(struct unwind_state *state,
32 unwind_init_common(state, current);
34 state->fp = regs->regs[29];
39 * Start an unwind from a caller.
41 * The unwind will begin at the caller of whichever function this is inlined
44 * The function which invokes this must be noinline.
46 static __always_inline void
47 unwind_init_from_caller(struct unwind_state *state)
49 unwind_init_common(state, current);
51 state->fp = (unsigned long)__builtin_frame_address(1);
52 state->pc = (unsigned long)__builtin_return_address(0);
56 * Start an unwind from a blocked task.
58 * The unwind will begin at the blocked tasks saved PC (i.e. the caller of
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.
65 static __always_inline void
66 unwind_init_from_task(struct unwind_state *state,
67 struct task_struct *task)
69 unwind_init_common(state, task);
71 state->fp = thread_saved_fp(task);
72 state->pc = thread_saved_pc(task);
75 static __always_inline int
76 unwind_recover_return_address(struct unwind_state *state)
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,
84 if (WARN_ON_ONCE(state->pc == orig_pc))
88 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
90 #ifdef CONFIG_KRETPROBES
91 if (is_kretprobe_trampoline(state->pc)) {
92 state->pc = kretprobe_find_ret_addr(state->task,
96 #endif /* CONFIG_KRETPROBES */
102 * Unwind from one frame record (A) to the next frame record (B).
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.
108 static __always_inline int
109 unwind_next(struct unwind_state *state)
111 struct task_struct *tsk = state->task;
112 unsigned long fp = state->fp;
115 /* Final frame; nothing to unwind */
116 if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
119 err = unwind_next_frame_record(state);
123 state->pc = ptrauth_strip_kernel_insn_pac(state->pc);
125 return unwind_recover_return_address(state);
128 static __always_inline void
129 unwind(struct unwind_state *state, stack_trace_consume_fn consume_entry,
132 if (unwind_recover_return_address(state))
138 if (!consume_entry(cookie, state->pc))
140 ret = unwind_next(state);
147 * Per-cpu stacks are only accessible when unwinding the current task in a
148 * non-preemptible context.
150 #define STACKINFO_CPU(name) \
152 ((task == current) && !preemptible()) \
153 ? stackinfo_get_##name() \
154 : stackinfo_get_unknown(); \
158 * SDEI stacks are only accessible when unwinding the current task in an NMI
161 #define STACKINFO_SDEI(name) \
163 ((task == current) && in_nmi()) \
164 ? stackinfo_get_sdei_##name() \
165 : stackinfo_get_unknown(); \
168 #define STACKINFO_EFI \
170 ((task == current) && current_in_efi()) \
171 ? stackinfo_get_efi() \
172 : stackinfo_get_unknown(); \
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)
179 struct stack_info stacks[] = {
180 stackinfo_get_task(task),
182 #if defined(CONFIG_VMAP_STACK)
183 STACKINFO_CPU(overflow),
185 #if defined(CONFIG_VMAP_STACK) && defined(CONFIG_ARM_SDE_INTERFACE)
186 STACKINFO_SDEI(normal),
187 STACKINFO_SDEI(critical),
193 struct unwind_state state = {
195 .nr_stacks = ARRAY_SIZE(stacks),
201 unwind_init_from_regs(&state, regs);
202 } else if (task == current) {
203 unwind_init_from_caller(&state);
205 unwind_init_from_task(&state, task);
208 unwind(&state, consume_entry, cookie);
211 static bool dump_backtrace_entry(void *arg, unsigned long where)
214 printk("%s %pSb\n", loglvl, (void *)where);
218 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
221 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
223 if (regs && user_mode(regs))
229 if (!try_get_task_stack(tsk))
232 printk("%sCall trace:\n", loglvl);
233 arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
238 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
240 dump_backtrace(NULL, tsk, loglvl);