blob: 81d9262acaf0b1409b70eb658e96f06cd376296c [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
AKASHI Takahiro132cd882015-12-04 11:02:26 +000053 /*
Yang Shia80a0eb2016-02-11 13:53:10 -080054 * Switching between stacks is valid when tracing current and in
55 * non-preemptible context.
AKASHI Takahiro132cd882015-12-04 11:02:26 +000056 */
Ard Biesheuvelc7365332017-07-22 12:48:34 +010057 if (!(tsk == current && !preemptible() && on_irq_stack(fp)) &&
58 !on_task_stack(tsk, fp))
Catalin Marinas60ffc302012-03-05 11:49:27 +000059 return -EINVAL;
60
61 frame->sp = fp + 0x10;
Yang Shibcaf6692016-02-08 09:13:09 -080062 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
63 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8));
Catalin Marinas60ffc302012-03-05 11:49:27 +000064
AKASHI Takahiro20380bb2015-12-15 17:33:41 +090065#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Mark Rutlandb5e73072016-09-23 17:55:05 +010066 if (tsk->ret_stack &&
AKASHI Takahiro20380bb2015-12-15 17:33:41 +090067 (frame->pc == (unsigned long)return_to_handler)) {
68 /*
69 * This is a case where function graph tracer has
70 * modified a return address (LR) in a stack frame
71 * to hook a function return.
72 * So replace it to an original value.
73 */
74 frame->pc = tsk->ret_stack[frame->graph--].ret;
75 }
76#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
77
AKASHI Takahiro132cd882015-12-04 11:02:26 +000078 /*
Ard Biesheuvel73267492017-07-22 18:45:33 +010079 * Frames created upon entry from EL0 have NULL FP and PC values, so
80 * don't bother reporting these. Frames created by __noreturn functions
81 * might have a valid FP even if PC is bogus, so only terminate where
82 * both are NULL.
AKASHI Takahiro132cd882015-12-04 11:02:26 +000083 */
Ard Biesheuvel73267492017-07-22 18:45:33 +010084 if (!frame->fp && !frame->pc)
85 return -EINVAL;
AKASHI Takahiro132cd882015-12-04 11:02:26 +000086
Catalin Marinas60ffc302012-03-05 11:49:27 +000087 return 0;
88}
89
AKASHI Takahirofe13f952015-12-15 17:33:40 +090090void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
Catalin Marinas60ffc302012-03-05 11:49:27 +000091 int (*fn)(struct stackframe *, void *), void *data)
92{
93 while (1) {
94 int ret;
95
96 if (fn(frame, data))
97 break;
AKASHI Takahirofe13f952015-12-15 17:33:40 +090098 ret = unwind_frame(tsk, frame);
Catalin Marinas60ffc302012-03-05 11:49:27 +000099 if (ret < 0)
100 break;
101 }
102}
Catalin Marinas60ffc302012-03-05 11:49:27 +0000103
104#ifdef CONFIG_STACKTRACE
105struct stack_trace_data {
106 struct stack_trace *trace;
107 unsigned int no_sched_functions;
108 unsigned int skip;
109};
110
111static int save_trace(struct stackframe *frame, void *d)
112{
113 struct stack_trace_data *data = d;
114 struct stack_trace *trace = data->trace;
115 unsigned long addr = frame->pc;
116
117 if (data->no_sched_functions && in_sched_functions(addr))
118 return 0;
119 if (data->skip) {
120 data->skip--;
121 return 0;
122 }
123
124 trace->entries[trace->nr_entries++] = addr;
125
126 return trace->nr_entries >= trace->max_entries;
127}
128
Pratyush Anand98ab10e2016-09-05 08:03:16 +0530129void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
130{
131 struct stack_trace_data data;
132 struct stackframe frame;
133
134 data.trace = trace;
135 data.skip = trace->skip;
136 data.no_sched_functions = 0;
137
138 frame.fp = regs->regs[29];
139 frame.sp = regs->sp;
140 frame.pc = regs->pc;
141#ifdef CONFIG_FUNCTION_GRAPH_TRACER
142 frame.graph = current->curr_ret_stack;
143#endif
144
145 walk_stackframe(current, &frame, save_trace, &data);
146 if (trace->nr_entries < trace->max_entries)
147 trace->entries[trace->nr_entries++] = ULONG_MAX;
148}
149
Catalin Marinas60ffc302012-03-05 11:49:27 +0000150void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
151{
152 struct stack_trace_data data;
153 struct stackframe frame;
154
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000155 if (!try_get_task_stack(tsk))
156 return;
157
Catalin Marinas60ffc302012-03-05 11:49:27 +0000158 data.trace = trace;
159 data.skip = trace->skip;
160
161 if (tsk != current) {
162 data.no_sched_functions = 1;
163 frame.fp = thread_saved_fp(tsk);
164 frame.sp = thread_saved_sp(tsk);
165 frame.pc = thread_saved_pc(tsk);
166 } else {
Catalin Marinas60ffc302012-03-05 11:49:27 +0000167 data.no_sched_functions = 0;
168 frame.fp = (unsigned long)__builtin_frame_address(0);
Behan Websterbb28cec2014-08-27 05:29:30 +0100169 frame.sp = current_stack_pointer;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000170 frame.pc = (unsigned long)save_stack_trace_tsk;
171 }
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900172#ifdef CONFIG_FUNCTION_GRAPH_TRACER
173 frame.graph = tsk->curr_ret_stack;
174#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +0000175
AKASHI Takahirofe13f952015-12-15 17:33:40 +0900176 walk_stackframe(tsk, &frame, save_trace, &data);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000177 if (trace->nr_entries < trace->max_entries)
178 trace->entries[trace->nr_entries++] = ULONG_MAX;
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000179
180 put_task_stack(tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000181}
Dustin Browne27c7fa2017-06-13 11:40:56 -0700182EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000183
184void save_stack_trace(struct stack_trace *trace)
185{
186 save_stack_trace_tsk(current, trace);
187}
188EXPORT_SYMBOL_GPL(save_stack_trace);
189#endif