blob: 7723dadf25be462d6ce3b621e9bf6079295fa587 [file] [log] [blame]
Catalin Marinas60ffc302012-03-05 11:49:27 +00001/*
2 * Stack tracing support
3 *
4 * Copyright (C) 2012 ARM Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include <linux/kernel.h>
19#include <linux/export.h>
AKASHI Takahiro20380bb2015-12-15 17:33:41 +090020#include <linux/ftrace.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000021#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010022#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010023#include <linux/sched/task_stack.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000024#include <linux/stacktrace.h>
25
AKASHI Takahiro132cd882015-12-04 11:02:26 +000026#include <asm/irq.h>
Mark Rutlanda9ea0012016-11-03 20:23:05 +000027#include <asm/stack_pointer.h>
Catalin Marinas60ffc302012-03-05 11:49:27 +000028#include <asm/stacktrace.h>
29
30/*
31 * AArch64 PCS assigns the frame pointer to x29.
32 *
33 * A simple function prologue looks like this:
34 * sub sp, sp, #0x10
35 * stp x29, x30, [sp]
36 * mov x29, sp
37 *
38 * A simple function epilogue looks like this:
39 * mov sp, x29
40 * ldp x29, x30, [sp]
41 * add sp, sp, #0x10
42 */
AKASHI Takahirofe13f952015-12-15 17:33:40 +090043int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
Catalin Marinas60ffc302012-03-05 11:49:27 +000044{
Catalin Marinas60ffc302012-03-05 11:49:27 +000045 unsigned long fp = frame->fp;
Ard Biesheuvelc7365332017-07-22 12:48:34 +010046
47 if (fp & 0xf)
48 return -EINVAL;
AKASHI Takahiro132cd882015-12-04 11:02:26 +000049
Mark Rutlandb5e73072016-09-23 17:55:05 +010050 if (!tsk)
51 tsk = current;
52
Laura Abbott8a1ccfb2018-07-20 14:41:53 -070053 if (!on_accessible_stack(tsk, fp, NULL))
Catalin Marinas60ffc302012-03-05 11:49:27 +000054 return -EINVAL;
55
Yang Shibcaf6692016-02-08 09:13:09 -080056 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
57 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
Catalin Marinas60ffc302012-03-05 11:49:27 +000058
AKASHI Takahiro20380bb2015-12-15 17:33:41 +090059#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Mark Rutlandb5e73072016-09-23 17:55:05 +010060 if (tsk->ret_stack &&
AKASHI Takahiro20380bb2015-12-15 17:33:41 +090061 (frame->pc == (unsigned long)return_to_handler)) {
Pratyush Anand9f416312018-02-05 14:28:01 +010062 if (WARN_ON_ONCE(frame->graph == -1))
63 return -EINVAL;
AKASHI Takahiro20380bb2015-12-15 17:33:41 +090064 /*
65 * This is a case where function graph tracer has
66 * modified a return address (LR) in a stack frame
67 * to hook a function return.
68 * So replace it to an original value.
69 */
70 frame->pc = tsk->ret_stack[frame->graph--].ret;
71 }
72#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
73
AKASHI Takahiro132cd882015-12-04 11:02:26 +000074 /*
Ard Biesheuvel73267492017-07-22 18:45:33 +010075 * Frames created upon entry from EL0 have NULL FP and PC values, so
76 * don't bother reporting these. Frames created by __noreturn functions
77 * might have a valid FP even if PC is bogus, so only terminate where
78 * both are NULL.
AKASHI Takahiro132cd882015-12-04 11:02:26 +000079 */
Ard Biesheuvel73267492017-07-22 18:45:33 +010080 if (!frame->fp && !frame->pc)
81 return -EINVAL;
AKASHI Takahiro132cd882015-12-04 11:02:26 +000082
Catalin Marinas60ffc302012-03-05 11:49:27 +000083 return 0;
84}
85
AKASHI Takahirofe13f952015-12-15 17:33:40 +090086void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
Catalin Marinas60ffc302012-03-05 11:49:27 +000087 int (*fn)(struct stackframe *, void *), void *data)
88{
89 while (1) {
90 int ret;
91
92 if (fn(frame, data))
93 break;
AKASHI Takahirofe13f952015-12-15 17:33:40 +090094 ret = unwind_frame(tsk, frame);
Catalin Marinas60ffc302012-03-05 11:49:27 +000095 if (ret < 0)
96 break;
97 }
98}
Catalin Marinas60ffc302012-03-05 11:49:27 +000099
100#ifdef CONFIG_STACKTRACE
101struct stack_trace_data {
102 struct stack_trace *trace;
103 unsigned int no_sched_functions;
104 unsigned int skip;
105};
106
107static int save_trace(struct stackframe *frame, void *d)
108{
109 struct stack_trace_data *data = d;
110 struct stack_trace *trace = data->trace;
111 unsigned long addr = frame->pc;
112
113 if (data->no_sched_functions && in_sched_functions(addr))
114 return 0;
115 if (data->skip) {
116 data->skip--;
117 return 0;
118 }
119
120 trace->entries[trace->nr_entries++] = addr;
121
122 return trace->nr_entries >= trace->max_entries;
123}
124
Pratyush Anand98ab10e2016-09-05 08:03:16 +0530125void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
126{
127 struct stack_trace_data data;
128 struct stackframe frame;
129
130 data.trace = trace;
131 data.skip = trace->skip;
132 data.no_sched_functions = 0;
133
134 frame.fp = regs->regs[29];
Pratyush Anand98ab10e2016-09-05 08:03:16 +0530135 frame.pc = regs->pc;
136#ifdef CONFIG_FUNCTION_GRAPH_TRACER
137 frame.graph = current->curr_ret_stack;
138#endif
139
140 walk_stackframe(current, &frame, save_trace, &data);
141 if (trace->nr_entries < trace->max_entries)
142 trace->entries[trace->nr_entries++] = ULONG_MAX;
143}
144
Prakash Guptabb53c822017-09-13 16:28:32 -0700145static noinline void __save_stack_trace(struct task_struct *tsk,
146 struct stack_trace *trace, unsigned int nosched)
Catalin Marinas60ffc302012-03-05 11:49:27 +0000147{
148 struct stack_trace_data data;
149 struct stackframe frame;
150
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000151 if (!try_get_task_stack(tsk))
152 return;
153
Catalin Marinas60ffc302012-03-05 11:49:27 +0000154 data.trace = trace;
155 data.skip = trace->skip;
Prakash Guptabb53c822017-09-13 16:28:32 -0700156 data.no_sched_functions = nosched;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000157
158 if (tsk != current) {
Catalin Marinas60ffc302012-03-05 11:49:27 +0000159 frame.fp = thread_saved_fp(tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000160 frame.pc = thread_saved_pc(tsk);
161 } else {
Prakash Guptabb53c822017-09-13 16:28:32 -0700162 /* We don't want this function nor the caller */
163 data.skip += 2;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000164 frame.fp = (unsigned long)__builtin_frame_address(0);
Prakash Guptabb53c822017-09-13 16:28:32 -0700165 frame.pc = (unsigned long)__save_stack_trace;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000166 }
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900167#ifdef CONFIG_FUNCTION_GRAPH_TRACER
168 frame.graph = tsk->curr_ret_stack;
169#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +0000170
AKASHI Takahirofe13f952015-12-15 17:33:40 +0900171 walk_stackframe(tsk, &frame, save_trace, &data);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000172 if (trace->nr_entries < trace->max_entries)
173 trace->entries[trace->nr_entries++] = ULONG_MAX;
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000174
175 put_task_stack(tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000176}
Dustin Browne27c7fa2017-06-13 11:40:56 -0700177EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000178
Prakash Guptabb53c822017-09-13 16:28:32 -0700179void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
180{
181 __save_stack_trace(tsk, trace, 1);
182}
183
Catalin Marinas60ffc302012-03-05 11:49:27 +0000184void save_stack_trace(struct stack_trace *trace)
185{
Prakash Guptabb53c822017-09-13 16:28:32 -0700186 __save_stack_trace(current, trace, 0);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000187}
Prakash Guptabb53c822017-09-13 16:28:32 -0700188
Catalin Marinas60ffc302012-03-05 11:49:27 +0000189EXPORT_SYMBOL_GPL(save_stack_trace);
190#endif