2 * Stack tracing support
4 * Copyright (C) 2012 ARM Ltd.
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.
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.
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/>.
18 #include <linux/kernel.h>
19 #include <linux/export.h>
20 #include <linux/sched.h>
21 #include <linux/stacktrace.h>
23 #include <asm/stacktrace.h>
26 * AArch64 PCS assigns the frame pointer to x29.
28 * A simple function prologue looks like this:
33 * A simple function epilogue looks like this:
38 int notrace unwind_frame(struct stackframe *frame)
40 unsigned long high, low;
41 unsigned long fp = frame->fp;
44 high = ALIGN(low, THREAD_SIZE);
46 if (fp < low || fp > high - 0x18 || fp & 0xf)
49 frame->sp = fp + 0x10;
50 frame->fp = *(unsigned long *)(fp);
52 * -4 here because we care about the PC at time of bl,
53 * not where the return will go.
55 frame->pc = *(unsigned long *)(fp + 8) - 4;
60 void notrace walk_stackframe(struct stackframe *frame,
61 int (*fn)(struct stackframe *, void *), void *data)
68 ret = unwind_frame(frame);
73 EXPORT_SYMBOL(walk_stackframe);
75 #ifdef CONFIG_STACKTRACE
76 struct stack_trace_data {
77 struct stack_trace *trace;
78 unsigned int no_sched_functions;
82 static int save_trace(struct stackframe *frame, void *d)
84 struct stack_trace_data *data = d;
85 struct stack_trace *trace = data->trace;
86 unsigned long addr = frame->pc;
88 if (data->no_sched_functions && in_sched_functions(addr))
95 trace->entries[trace->nr_entries++] = addr;
97 return trace->nr_entries >= trace->max_entries;
100 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
102 struct stack_trace_data data;
103 struct stackframe frame;
106 data.skip = trace->skip;
108 if (tsk != current) {
109 data.no_sched_functions = 1;
110 frame.fp = thread_saved_fp(tsk);
111 frame.sp = thread_saved_sp(tsk);
112 frame.pc = thread_saved_pc(tsk);
114 data.no_sched_functions = 0;
115 frame.fp = (unsigned long)__builtin_frame_address(0);
116 frame.sp = current_stack_pointer;
117 frame.pc = (unsigned long)save_stack_trace_tsk;
120 walk_stackframe(&frame, save_trace, &data);
121 if (trace->nr_entries < trace->max_entries)
122 trace->entries[trace->nr_entries++] = ULONG_MAX;
125 void save_stack_trace(struct stack_trace *trace)
127 save_stack_trace_tsk(current, trace);
129 EXPORT_SYMBOL_GPL(save_stack_trace);