blob: beaf51fb3088b6549be07dfe3b3cec784073dcf9 [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 /*
79 * Check whether we are going to walk through from interrupt stack
80 * to task stack.
81 * If we reach the end of the stack - and its an interrupt stack,
James Morse971c67c2015-12-15 11:21:25 +000082 * unpack the dummy frame to find the original elr.
James Morse1ffe1992015-12-10 10:22:40 +000083 *
84 * Check the frame->fp we read from the bottom of the irq_stack,
85 * and the original task stack pointer are both in current->stack.
AKASHI Takahiro132cd882015-12-04 11:02:26 +000086 */
Ard Biesheuvelc7365332017-07-22 12:48:34 +010087 if (frame->sp == IRQ_STACK_PTR()) {
James Morse971c67c2015-12-15 11:21:25 +000088 struct pt_regs *irq_args;
Ard Biesheuvelc7365332017-07-22 12:48:34 +010089 unsigned long orig_sp = IRQ_STACK_TO_TASK_STACK(frame->sp);
James Morse1ffe1992015-12-10 10:22:40 +000090
James Morse971c67c2015-12-15 11:21:25 +000091 if (object_is_on_stack((void *)orig_sp) &&
92 object_is_on_stack((void *)frame->fp)) {
James Morse1ffe1992015-12-10 10:22:40 +000093 frame->sp = orig_sp;
James Morse971c67c2015-12-15 11:21:25 +000094
95 /* orig_sp is the saved pt_regs, find the elr */
96 irq_args = (struct pt_regs *)orig_sp;
97 frame->pc = irq_args->pc;
98 } else {
99 /*
100 * This frame has a non-standard format, and we
101 * didn't fix it, because the data looked wrong.
102 * Refuse to output this frame.
103 */
104 return -EINVAL;
105 }
James Morse1ffe1992015-12-10 10:22:40 +0000106 }
AKASHI Takahiro132cd882015-12-04 11:02:26 +0000107
Catalin Marinas60ffc302012-03-05 11:49:27 +0000108 return 0;
109}
110
AKASHI Takahirofe13f952015-12-15 17:33:40 +0900111void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
Catalin Marinas60ffc302012-03-05 11:49:27 +0000112 int (*fn)(struct stackframe *, void *), void *data)
113{
114 while (1) {
115 int ret;
116
117 if (fn(frame, data))
118 break;
AKASHI Takahirofe13f952015-12-15 17:33:40 +0900119 ret = unwind_frame(tsk, frame);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000120 if (ret < 0)
121 break;
122 }
123}
Catalin Marinas60ffc302012-03-05 11:49:27 +0000124
125#ifdef CONFIG_STACKTRACE
126struct stack_trace_data {
127 struct stack_trace *trace;
128 unsigned int no_sched_functions;
129 unsigned int skip;
130};
131
132static int save_trace(struct stackframe *frame, void *d)
133{
134 struct stack_trace_data *data = d;
135 struct stack_trace *trace = data->trace;
136 unsigned long addr = frame->pc;
137
138 if (data->no_sched_functions && in_sched_functions(addr))
139 return 0;
140 if (data->skip) {
141 data->skip--;
142 return 0;
143 }
144
145 trace->entries[trace->nr_entries++] = addr;
146
147 return trace->nr_entries >= trace->max_entries;
148}
149
Pratyush Anand98ab10e2016-09-05 08:03:16 +0530150void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
151{
152 struct stack_trace_data data;
153 struct stackframe frame;
154
155 data.trace = trace;
156 data.skip = trace->skip;
157 data.no_sched_functions = 0;
158
159 frame.fp = regs->regs[29];
160 frame.sp = regs->sp;
161 frame.pc = regs->pc;
162#ifdef CONFIG_FUNCTION_GRAPH_TRACER
163 frame.graph = current->curr_ret_stack;
164#endif
165
166 walk_stackframe(current, &frame, save_trace, &data);
167 if (trace->nr_entries < trace->max_entries)
168 trace->entries[trace->nr_entries++] = ULONG_MAX;
169}
170
Catalin Marinas60ffc302012-03-05 11:49:27 +0000171void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
172{
173 struct stack_trace_data data;
174 struct stackframe frame;
175
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000176 if (!try_get_task_stack(tsk))
177 return;
178
Catalin Marinas60ffc302012-03-05 11:49:27 +0000179 data.trace = trace;
180 data.skip = trace->skip;
181
182 if (tsk != current) {
183 data.no_sched_functions = 1;
184 frame.fp = thread_saved_fp(tsk);
185 frame.sp = thread_saved_sp(tsk);
186 frame.pc = thread_saved_pc(tsk);
187 } else {
Catalin Marinas60ffc302012-03-05 11:49:27 +0000188 data.no_sched_functions = 0;
189 frame.fp = (unsigned long)__builtin_frame_address(0);
Behan Websterbb28cec2014-08-27 05:29:30 +0100190 frame.sp = current_stack_pointer;
Catalin Marinas60ffc302012-03-05 11:49:27 +0000191 frame.pc = (unsigned long)save_stack_trace_tsk;
192 }
AKASHI Takahiro20380bb2015-12-15 17:33:41 +0900193#ifdef CONFIG_FUNCTION_GRAPH_TRACER
194 frame.graph = tsk->curr_ret_stack;
195#endif
Catalin Marinas60ffc302012-03-05 11:49:27 +0000196
AKASHI Takahirofe13f952015-12-15 17:33:40 +0900197 walk_stackframe(tsk, &frame, save_trace, &data);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000198 if (trace->nr_entries < trace->max_entries)
199 trace->entries[trace->nr_entries++] = ULONG_MAX;
Mark Rutland9bbd4c52016-11-03 20:23:08 +0000200
201 put_task_stack(tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000202}
Dustin Browne27c7fa2017-06-13 11:40:56 -0700203EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
Catalin Marinas60ffc302012-03-05 11:49:27 +0000204
205void save_stack_trace(struct stack_trace *trace)
206{
207 save_stack_trace_tsk(current, trace);
208}
209EXPORT_SYMBOL_GPL(save_stack_trace);
210#endif