1 // SPDX-License-Identifier: GPL-2.0-only
3 * Stack tracing support
5 * Copyright (C) 2012 ARM Ltd.
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>
17 #include <asm/pointer_auth.h>
18 #include <asm/stack_pointer.h>
19 #include <asm/stacktrace.h>
22 * AArch64 PCS assigns the frame pointer to x29.
24 * A simple function prologue looks like this:
29 * A simple function epilogue looks like this:
36 * Unwind from one frame record (A) to the next frame record (B).
38 * We terminate early if the location of B indicates a malformed chain of frame
39 * records (e.g. a cycle), determined based on the location and fp value of A
40 * and the location (but not the fp value) of B.
42 int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
44 unsigned long fp = frame->fp;
45 struct stack_info info;
47 /* Terminal record; nothing to unwind */
57 if (!on_accessible_stack(tsk, fp, &info))
60 if (test_bit(info.type, frame->stacks_done))
64 * As stacks grow downward, any valid record on the same stack must be
65 * at a strictly higher address than the prior record.
67 * Stacks can nest in several valid orders, e.g.
69 * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
70 * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
72 * ... but the nesting itself is strict. Once we transition from one
73 * stack to another, it's never valid to unwind back to that first
76 if (info.type == frame->prev_type) {
77 if (fp <= frame->prev_fp)
80 set_bit(frame->prev_type, frame->stacks_done);
84 * Record this frame record's values and location. The prev_fp and
85 * prev_type are only meaningful to the next unwind_frame() invocation.
87 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
88 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
90 frame->prev_type = info.type;
92 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
94 (ptrauth_strip_insn_pac(frame->pc) == (unsigned long)return_to_handler)) {
95 struct ftrace_ret_stack *ret_stack;
97 * This is a case where function graph tracer has
98 * modified a return address (LR) in a stack frame
99 * to hook a function return.
100 * So replace it to an original value.
102 ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph++);
103 if (WARN_ON_ONCE(!ret_stack))
105 frame->pc = ret_stack->ret;
107 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
109 frame->pc = ptrauth_strip_insn_pac(frame->pc);
113 NOKPROBE_SYMBOL(unwind_frame);
115 void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
116 bool (*fn)(void *, unsigned long), void *data)
121 if (!fn(data, frame->pc))
123 ret = unwind_frame(tsk, frame);
128 NOKPROBE_SYMBOL(walk_stackframe);
130 static void dump_backtrace_entry(unsigned long where, const char *loglvl)
132 printk("%s %pS\n", loglvl, (void *)where);
135 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
138 struct stackframe frame;
141 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
152 if (!try_get_task_stack(tsk))
155 if (tsk == current) {
156 start_backtrace(&frame,
157 (unsigned long)__builtin_frame_address(0),
158 (unsigned long)dump_backtrace);
161 * task blocked in __switch_to
163 start_backtrace(&frame,
164 thread_saved_fp(tsk),
165 thread_saved_pc(tsk));
168 printk("%sCall trace:\n", loglvl);
170 /* skip until specified stack frame */
172 dump_backtrace_entry(frame.pc, loglvl);
173 } else if (frame.fp == regs->regs[29]) {
176 * Mostly, this is the case where this function is
177 * called in panic/abort. As exception handler's
178 * stack frame does not contain the corresponding pc
179 * at which an exception has taken place, use regs->pc
182 dump_backtrace_entry(regs->pc, loglvl);
184 } while (!unwind_frame(tsk, &frame));
189 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
191 dump_backtrace(NULL, tsk, loglvl);
195 #ifdef CONFIG_STACKTRACE
197 noinline void arch_stack_walk(stack_trace_consume_fn consume_entry,
198 void *cookie, struct task_struct *task,
199 struct pt_regs *regs)
201 struct stackframe frame;
204 start_backtrace(&frame, regs->regs[29], regs->pc);
205 else if (task == current)
206 start_backtrace(&frame,
207 (unsigned long)__builtin_frame_address(1),
208 (unsigned long)__builtin_return_address(0));
210 start_backtrace(&frame, thread_saved_fp(task),
211 thread_saved_pc(task));
213 walk_stackframe(task, &frame, consume_entry, cookie);