blob: e4103e085681143e5f285c91d428f152d7013a6d [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Catalin Marinas60ffc302012-03-05 11:49:27 +00002/*
3 * Stack tracing support
4 *
5 * Copyright (C) 2012 ARM Ltd.
Catalin Marinas60ffc302012-03-05 11:49:27 +00006 */
7#include <linux/kernel.h>
8#include <linux/export.h>
AKASHI Takahiro20380bb2015-12-15 17:33:41 +09009#include <linux/ftrace.h>
Masami Hiramatsuee07b932019-07-25 17:16:05 +090010#include <linux/kprobes.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000011#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010012#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010013#include <linux/sched/task_stack.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000014#include <linux/stacktrace.h>
15
AKASHI Takahiro132cd882015-12-04 11:02:26 +000016#include <asm/irq.h>
Mark Rutland04ad99a2020-03-13 14:34:59 +053017#include <asm/pointer_auth.h>
Mark Rutlanda9ea0012016-11-03 20:23:05 +000018#include <asm/stack_pointer.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000019#include <asm/stacktrace.h>
20
21/*
22 * AArch64 PCS assigns the frame pointer to x29.
23 *
24 * A simple function prologue looks like this:
25 * sub sp, sp, #0x10
26 * stp x29, x30, [sp]
27 * mov x29, sp
28 *
29 * A simple function epilogue looks like this:
30 * mov sp, x29
31 * ldp x29, x30, [sp]
32 * add sp, sp, #0x10
33 */
Mark Rutland592700f2019-07-02 14:07:29 +010034
Mark Brownb07f3492021-03-19 17:40:22 +000035
Masami Hiramatsu1e0924b2022-01-24 17:17:54 +090036static notrace void start_backtrace(struct stackframe *frame, unsigned long fp,
37 unsigned long pc)
Mark Brownb07f3492021-03-19 17:40:22 +000038{
39 frame->fp = fp;
40 frame->pc = pc;
Masami Hiramatsucd9bc2c2021-10-21 09:55:09 +090041#ifdef CONFIG_KRETPROBES
42 frame->kr_cur = NULL;
43#endif
Mark Brownb07f3492021-03-19 17:40:22 +000044
45 /*
46 * Prime the first unwind.
47 *
48 * In unwind_frame() we'll check that the FP points to a valid stack,
49 * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be
50 * treated as a transition to whichever stack that happens to be. The
51 * prev_fp value won't be used, but we set it to 0 such that it is
52 * definitely not an accessible stack address.
53 */
54 bitmap_zero(frame->stacks_done, __NR_STACK_TYPES);
55 frame->prev_fp = 0;
56 frame->prev_type = STACK_TYPE_UNKNOWN;
57}
Masami Hiramatsu1e0924b2022-01-24 17:17:54 +090058NOKPROBE_SYMBOL(start_backtrace);
Mark Brownb07f3492021-03-19 17:40:22 +000059
Mark Rutland592700f2019-07-02 14:07:29 +010060/*
61 * Unwind from one frame record (A) to the next frame record (B).
62 *
63 * We terminate early if the location of B indicates a malformed chain of frame
64 * records (e.g. a cycle), determined based on the location and fp value of A
65 * and the location (but not the fp value) of B.
66 */
Mark Rutlandd2d1d262021-11-29 14:28:49 +000067static int notrace unwind_frame(struct task_struct *tsk,
68 struct stackframe *frame)
Catalin Marinas60ffc302012-03-05 11:49:27 +000069{
Catalin Marinas60ffc302012-03-05 11:49:27 +000070 unsigned long fp = frame->fp;
Mark Rutland592700f2019-07-02 14:07:29 +010071 struct stack_info info;
Ard Biesheuvelc7365332017-07-22 12:48:34 +010072
Mark Rutlandb5e73072016-09-23 17:55:05 +010073 if (!tsk)
74 tsk = current;
75
Madhavan T. Venkataraman7d7b7202021-05-10 12:00:26 +010076 /* Final frame; nothing to unwind */
77 if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
78 return -ENOENT;
79
Peter Collingbourne33c222a2021-05-26 10:49:26 -070080 if (fp & 0x7)
Madhavan T. Venkataraman7d7b7202021-05-10 12:00:26 +010081 return -EINVAL;
82
Peter Collingbourne76734d22021-05-26 10:49:25 -070083 if (!on_accessible_stack(tsk, fp, 16, &info))
Catalin Marinas60ffc302012-03-05 11:49:27 +000084 return -EINVAL;
85
Mark Rutland592700f2019-07-02 14:07:29 +010086 if (test_bit(info.type, frame->stacks_done))
87 return -EINVAL;
88
89 /*
90 * As stacks grow downward, any valid record on the same stack must be
91 * at a strictly higher address than the prior record.
92 *
93 * Stacks can nest in several valid orders, e.g.
94 *
95 * TASK -> IRQ -> OVERFLOW -> SDEI_NORMAL
96 * TASK -> SDEI_NORMAL -> SDEI_CRITICAL -> OVERFLOW
97 *
98 * ... but the nesting itself is strict. Once we transition from one
99 * stack to another, it's never valid to unwind back to that first
100 * stack.
101 */
102 if (info.type == frame->prev_type) {
103 if (fp <= frame->prev_fp)
104 return -EINVAL;
105 } else {
106 set_bit(frame->prev_type, frame->stacks_done);
107 }
108
109 /*
110 * Record this frame record's values and location. The prev_fp and
111 * prev_type are only meaningful to the next unwind_frame() invocation.
112 */
Yang Shibcaf6692016-02-08 09:13:09 -0800113 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
114 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
Mark Rutland592700f2019-07-02 14:07:29 +0100115 frame->prev_fp = fp;
116 frame->prev_type = info.type;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000117
Mark Rutlandc6d3cd32021-10-29 17:22:45 +0100118 frame->pc = ptrauth_strip_insn_pac(frame->pc);
119
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900120#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Mark Rutlandb5e73072016-09-23 17:55:05 +0100121 if (tsk->ret_stack &&
Mark Rutlandc6d3cd32021-10-29 17:22:45 +0100122 (frame->pc == (unsigned long)return_to_handler)) {
123 unsigned long orig_pc;
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900124 /*
125 * This is a case where function graph tracer has
126 * modified a return address (LR) in a stack frame
127 * to hook a function return.
128 * So replace it to an original value.
129 */
Mark Rutlandc6d3cd32021-10-29 17:22:45 +0100130 orig_pc = ftrace_graph_ret_addr(tsk, NULL, frame->pc,
131 (void *)frame->fp);
132 if (WARN_ON_ONCE(frame->pc == orig_pc))
Steven Rostedt (VMware)a4482762018-12-07 13:13:28 -0500133 return -EINVAL;
Mark Rutlandc6d3cd32021-10-29 17:22:45 +0100134 frame->pc = orig_pc;
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900135 }
136#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Masami Hiramatsucd9bc2c2021-10-21 09:55:09 +0900137#ifdef CONFIG_KRETPROBES
138 if (is_kretprobe_trampoline(frame->pc))
139 frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur);
140#endif
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900141
Catalin Marinas60ffc302012-03-05 11:49:27 +0000142 return 0;
143}
Masami Hiramatsuee07b932019-07-25 17:16:05 +0900144NOKPROBE_SYMBOL(unwind_frame);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000145
Mark Rutlandd2d1d262021-11-29 14:28:49 +0000146static void notrace walk_stackframe(struct task_struct *tsk,
147 struct stackframe *frame,
148 bool (*fn)(void *, unsigned long), void *data)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000149{
150 while (1) {
151 int ret;
152
Mark Brownbaa2cd42020-09-14 16:34:08 +0100153 if (!fn(data, frame->pc))
Catalin Marinas60ffc302012-03-05 11:49:27 +0000154 break;
AKASHI Takahirofe13f952015-12-15 17:33:40 +0900155 ret = unwind_frame(tsk, frame);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000156 if (ret < 0)
157 break;
158 }
159}
Masami Hiramatsuee07b932019-07-25 17:16:05 +0900160NOKPROBE_SYMBOL(walk_stackframe);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000161
Madhavan T. Venkataraman2dad6dc2021-11-29 14:28:48 +0000162static bool dump_backtrace_entry(void *arg, unsigned long where)
Mark Brown9e0f0852020-09-21 13:23:41 +0100163{
Madhavan T. Venkataraman2dad6dc2021-11-29 14:28:48 +0000164 char *loglvl = arg;
Stephen Boydf61b8702021-07-07 18:09:24 -0700165 printk("%s %pSb\n", loglvl, (void *)where);
Madhavan T. Venkataraman2dad6dc2021-11-29 14:28:48 +0000166 return true;
Mark Brown9e0f0852020-09-21 13:23:41 +0100167}
168
169void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
170 const char *loglvl)
171{
Mark Brown9e0f0852020-09-21 13:23:41 +0100172 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
173
Madhavan T. Venkataraman2dad6dc2021-11-29 14:28:48 +0000174 if (regs && user_mode(regs))
175 return;
Mark Brown9e0f0852020-09-21 13:23:41 +0100176
177 if (!tsk)
178 tsk = current;
179
180 if (!try_get_task_stack(tsk))
181 return;
182
Mark Brown9e0f0852020-09-21 13:23:41 +0100183 printk("%sCall trace:\n", loglvl);
Madhavan T. Venkataraman2dad6dc2021-11-29 14:28:48 +0000184 arch_stack_walk(dump_backtrace_entry, (void *)loglvl, tsk, regs);
Mark Brown9e0f0852020-09-21 13:23:41 +0100185
186 put_task_stack(tsk);
187}
188
189void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
190{
191 dump_backtrace(NULL, tsk, loglvl);
192 barrier();
193}
194
Mark Rutland0c327062021-08-02 17:48:45 +0100195noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry,
Mark Rutlandc607ab42021-03-19 18:41:06 +0000196 void *cookie, struct task_struct *task,
197 struct pt_regs *regs)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000198{
Pratyush Anand98ab10e2016-09-05 08:03:16 +0530199 struct stackframe frame;
200
Mark Brown5fc57df2020-09-14 16:34:09 +0100201 if (regs)
202 start_backtrace(&frame, regs->regs[29], regs->pc);
203 else if (task == current)
Dave Martinf3dcbe62019-07-02 14:07:28 +0100204 start_backtrace(&frame,
Mark Rutlandc607ab42021-03-19 18:41:06 +0000205 (unsigned long)__builtin_frame_address(1),
206 (unsigned long)__builtin_return_address(0));
Mark Brown5fc57df2020-09-14 16:34:09 +0100207 else
208 start_backtrace(&frame, thread_saved_fp(task),
209 thread_saved_pc(task));
Catalin Marinas60ffc302012-03-05 11:49:27 +0000210
Mark Brown5fc57df2020-09-14 16:34:09 +0100211 walk_stackframe(task, &frame, consume_entry, cookie);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000212}