Commit | Line | Data |
---|---|---|
60ffc30d CM |
1 | /* |
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> | |
20380bb3 | 20 | #include <linux/ftrace.h> |
60ffc30d | 21 | #include <linux/sched.h> |
b17b0153 | 22 | #include <linux/sched/debug.h> |
68db0cf1 | 23 | #include <linux/sched/task_stack.h> |
60ffc30d CM |
24 | #include <linux/stacktrace.h> |
25 | ||
132cd887 | 26 | #include <asm/irq.h> |
a9ea0017 | 27 | #include <asm/stack_pointer.h> |
60ffc30d CM |
28 | #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 | */ | |
fe13f95b | 43 | int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) |
60ffc30d | 44 | { |
60ffc30d | 45 | unsigned long fp = frame->fp; |
c7365330 AB |
46 | |
47 | if (fp & 0xf) | |
48 | return -EINVAL; | |
132cd887 | 49 | |
b5e7307d MR |
50 | if (!tsk) |
51 | tsk = current; | |
52 | ||
8a1ccfbc | 53 | if (!on_accessible_stack(tsk, fp, NULL)) |
60ffc30d CM |
54 | return -EINVAL; |
55 | ||
bcaf669b YS |
56 | frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp)); |
57 | frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8)); | |
60ffc30d | 58 | |
20380bb3 | 59 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
b5e7307d | 60 | if (tsk->ret_stack && |
20380bb3 | 61 | (frame->pc == (unsigned long)return_to_handler)) { |
a448276c | 62 | struct ftrace_ret_stack *ret_stack; |
20380bb3 AT |
63 | /* |
64 | * This is a case where function graph tracer has | |
65 | * modified a return address (LR) in a stack frame | |
66 | * to hook a function return. | |
67 | * So replace it to an original value. | |
68 | */ | |
a448276c SRV |
69 | ret_stack = ftrace_graph_get_ret_stack(tsk, frame->graph++); |
70 | if (WARN_ON_ONCE(!ret_stack)) | |
71 | return -EINVAL; | |
72 | frame->pc = ret_stack->ret; | |
20380bb3 AT |
73 | } |
74 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
75 | ||
132cd887 | 76 | /* |
73267498 AB |
77 | * Frames created upon entry from EL0 have NULL FP and PC values, so |
78 | * don't bother reporting these. Frames created by __noreturn functions | |
79 | * might have a valid FP even if PC is bogus, so only terminate where | |
80 | * both are NULL. | |
132cd887 | 81 | */ |
73267498 AB |
82 | if (!frame->fp && !frame->pc) |
83 | return -EINVAL; | |
132cd887 | 84 | |
60ffc30d CM |
85 | return 0; |
86 | } | |
87 | ||
fe13f95b | 88 | void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame, |
60ffc30d CM |
89 | int (*fn)(struct stackframe *, void *), void *data) |
90 | { | |
91 | while (1) { | |
92 | int ret; | |
93 | ||
94 | if (fn(frame, data)) | |
95 | break; | |
fe13f95b | 96 | ret = unwind_frame(tsk, frame); |
60ffc30d CM |
97 | if (ret < 0) |
98 | break; | |
99 | } | |
100 | } | |
60ffc30d CM |
101 | |
102 | #ifdef CONFIG_STACKTRACE | |
103 | struct stack_trace_data { | |
104 | struct stack_trace *trace; | |
105 | unsigned int no_sched_functions; | |
106 | unsigned int skip; | |
107 | }; | |
108 | ||
109 | static int save_trace(struct stackframe *frame, void *d) | |
110 | { | |
111 | struct stack_trace_data *data = d; | |
112 | struct stack_trace *trace = data->trace; | |
113 | unsigned long addr = frame->pc; | |
114 | ||
115 | if (data->no_sched_functions && in_sched_functions(addr)) | |
116 | return 0; | |
117 | if (data->skip) { | |
118 | data->skip--; | |
119 | return 0; | |
120 | } | |
121 | ||
122 | trace->entries[trace->nr_entries++] = addr; | |
123 | ||
124 | return trace->nr_entries >= trace->max_entries; | |
125 | } | |
126 | ||
98ab10e9 PA |
127 | void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace) |
128 | { | |
129 | struct stack_trace_data data; | |
130 | struct stackframe frame; | |
131 | ||
132 | data.trace = trace; | |
133 | data.skip = trace->skip; | |
134 | data.no_sched_functions = 0; | |
135 | ||
136 | frame.fp = regs->regs[29]; | |
98ab10e9 PA |
137 | frame.pc = regs->pc; |
138 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER | |
a448276c | 139 | frame.graph = 0; |
98ab10e9 PA |
140 | #endif |
141 | ||
142 | walk_stackframe(current, &frame, save_trace, &data); | |
143 | if (trace->nr_entries < trace->max_entries) | |
144 | trace->entries[trace->nr_entries++] = ULONG_MAX; | |
145 | } | |
c82fd1e6 | 146 | EXPORT_SYMBOL_GPL(save_stack_trace_regs); |
98ab10e9 | 147 | |
bb53c820 PG |
148 | static noinline void __save_stack_trace(struct task_struct *tsk, |
149 | struct stack_trace *trace, unsigned int nosched) | |
60ffc30d CM |
150 | { |
151 | struct stack_trace_data data; | |
152 | struct stackframe frame; | |
153 | ||
9bbd4c56 MR |
154 | if (!try_get_task_stack(tsk)) |
155 | return; | |
156 | ||
60ffc30d CM |
157 | data.trace = trace; |
158 | data.skip = trace->skip; | |
bb53c820 | 159 | data.no_sched_functions = nosched; |
60ffc30d CM |
160 | |
161 | if (tsk != current) { | |
60ffc30d | 162 | frame.fp = thread_saved_fp(tsk); |
60ffc30d CM |
163 | frame.pc = thread_saved_pc(tsk); |
164 | } else { | |
bb53c820 PG |
165 | /* We don't want this function nor the caller */ |
166 | data.skip += 2; | |
60ffc30d | 167 | frame.fp = (unsigned long)__builtin_frame_address(0); |
bb53c820 | 168 | frame.pc = (unsigned long)__save_stack_trace; |
60ffc30d | 169 | } |
20380bb3 | 170 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
a448276c | 171 | frame.graph = 0; |
20380bb3 | 172 | #endif |
60ffc30d | 173 | |
fe13f95b | 174 | walk_stackframe(tsk, &frame, save_trace, &data); |
60ffc30d CM |
175 | if (trace->nr_entries < trace->max_entries) |
176 | trace->entries[trace->nr_entries++] = ULONG_MAX; | |
9bbd4c56 MR |
177 | |
178 | put_task_stack(tsk); | |
60ffc30d | 179 | } |
e27c7fa0 | 180 | EXPORT_SYMBOL_GPL(save_stack_trace_tsk); |
60ffc30d | 181 | |
bb53c820 PG |
182 | void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace) |
183 | { | |
184 | __save_stack_trace(tsk, trace, 1); | |
185 | } | |
186 | ||
60ffc30d CM |
187 | void save_stack_trace(struct stack_trace *trace) |
188 | { | |
bb53c820 | 189 | __save_stack_trace(current, trace, 0); |
60ffc30d | 190 | } |
bb53c820 | 191 | |
60ffc30d CM |
192 | EXPORT_SYMBOL_GPL(save_stack_trace); |
193 | #endif |