Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / arch / arm64 / kernel / stacktrace.c
blobc3b6c63ea5fb3abe0f154544aee2eb09063c0eb8
1 /*
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:
29 * sub sp, sp, #0x10
30 * stp x29, x30, [sp]
31 * mov x29, sp
33 * A simple function epilogue looks like this:
34 * mov sp, x29
35 * ldp x29, x30, [sp]
36 * add sp, sp, #0x10
38 int unwind_frame(struct stackframe *frame)
40 unsigned long high, low;
41 unsigned long fp = frame->fp;
43 low = frame->sp;
44 high = ALIGN(low, THREAD_SIZE);
46 if (fp < low || fp > high - 0x18 || fp & 0xf)
47 return -EINVAL;
49 frame->sp = fp + 0x10;
50 frame->fp = *(unsigned long *)(fp);
51 frame->pc = *(unsigned long *)(fp + 8);
53 return 0;
56 void notrace walk_stackframe(struct stackframe *frame,
57 int (*fn)(struct stackframe *, void *), void *data)
59 while (1) {
60 int ret;
62 if (fn(frame, data))
63 break;
64 ret = unwind_frame(frame);
65 if (ret < 0)
66 break;
69 EXPORT_SYMBOL(walk_stackframe);
71 #ifdef CONFIG_STACKTRACE
72 struct stack_trace_data {
73 struct stack_trace *trace;
74 unsigned int no_sched_functions;
75 unsigned int skip;
78 static int save_trace(struct stackframe *frame, void *d)
80 struct stack_trace_data *data = d;
81 struct stack_trace *trace = data->trace;
82 unsigned long addr = frame->pc;
84 if (data->no_sched_functions && in_sched_functions(addr))
85 return 0;
86 if (data->skip) {
87 data->skip--;
88 return 0;
91 trace->entries[trace->nr_entries++] = addr;
93 return trace->nr_entries >= trace->max_entries;
96 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
98 struct stack_trace_data data;
99 struct stackframe frame;
101 data.trace = trace;
102 data.skip = trace->skip;
104 if (tsk != current) {
105 data.no_sched_functions = 1;
106 frame.fp = thread_saved_fp(tsk);
107 frame.sp = thread_saved_sp(tsk);
108 frame.pc = thread_saved_pc(tsk);
109 } else {
110 register unsigned long current_sp asm("sp");
111 data.no_sched_functions = 0;
112 frame.fp = (unsigned long)__builtin_frame_address(0);
113 frame.sp = current_sp;
114 frame.pc = (unsigned long)save_stack_trace_tsk;
117 walk_stackframe(&frame, save_trace, &data);
118 if (trace->nr_entries < trace->max_entries)
119 trace->entries[trace->nr_entries++] = ULONG_MAX;
122 void save_stack_trace(struct stack_trace *trace)
124 save_stack_trace_tsk(current, trace);
126 EXPORT_SYMBOL_GPL(save_stack_trace);
127 #endif