This source file includes following definitions.
- ftrace_modify_code
- ftrace_update_ftrace_func
- ftrace_make_call
- ftrace_make_nop
- arch_ftrace_update_code
- ftrace_dyn_arch_init
- prepare_ftrace_return
- ftrace_modify_graph_caller
- ftrace_enable_ftrace_graph_caller
- ftrace_disable_ftrace_graph_caller
1
2
3
4
5
6
7
8
9 #include <linux/ftrace.h>
10 #include <linux/module.h>
11 #include <linux/swab.h>
12 #include <linux/uaccess.h>
13
14 #include <asm/cacheflush.h>
15 #include <asm/debug-monitors.h>
16 #include <asm/ftrace.h>
17 #include <asm/insn.h>
18
19 #ifdef CONFIG_DYNAMIC_FTRACE
20
21
22
23
24 static int ftrace_modify_code(unsigned long pc, u32 old, u32 new,
25 bool validate)
26 {
27 u32 replaced;
28
29
30
31
32
33
34
35
36
37 if (validate) {
38 if (aarch64_insn_read((void *)pc, &replaced))
39 return -EFAULT;
40
41 if (replaced != old)
42 return -EINVAL;
43 }
44 if (aarch64_insn_patch_text_nosync((void *)pc, new))
45 return -EPERM;
46
47 return 0;
48 }
49
50
51
52
53 int ftrace_update_ftrace_func(ftrace_func_t func)
54 {
55 unsigned long pc;
56 u32 new;
57
58 pc = (unsigned long)&ftrace_call;
59 new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func,
60 AARCH64_INSN_BRANCH_LINK);
61
62 return ftrace_modify_code(pc, 0, new, false);
63 }
64
65
66
67
68 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
69 {
70 unsigned long pc = rec->ip;
71 u32 old, new;
72 long offset = (long)pc - (long)addr;
73
74 if (offset < -SZ_128M || offset >= SZ_128M) {
75 #ifdef CONFIG_ARM64_MODULE_PLTS
76 struct plt_entry trampoline, *dst;
77 struct module *mod;
78
79
80
81
82
83
84
85
86
87
88
89 preempt_disable();
90 mod = __module_text_address(pc);
91 preempt_enable();
92
93 if (WARN_ON(!mod))
94 return -EINVAL;
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 dst = mod->arch.ftrace_trampoline;
110 trampoline = get_plt_entry(addr, dst);
111 if (memcmp(dst, &trampoline, sizeof(trampoline))) {
112 if (plt_entry_is_initialized(dst)) {
113 pr_err("ftrace: far branches to multiple entry points unsupported inside a single module\n");
114 return -EINVAL;
115 }
116
117
118 module_disable_ro(mod);
119 *dst = trampoline;
120 module_enable_ro(mod, true);
121
122
123
124
125
126
127
128
129
130
131
132 flush_icache_range((unsigned long)&dst[0],
133 (unsigned long)&dst[1]);
134 }
135 addr = (unsigned long)dst;
136 #else
137 return -EINVAL;
138 #endif
139 }
140
141 old = aarch64_insn_gen_nop();
142 new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK);
143
144 return ftrace_modify_code(pc, old, new, true);
145 }
146
147
148
149
150 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
151 unsigned long addr)
152 {
153 unsigned long pc = rec->ip;
154 bool validate = true;
155 u32 old = 0, new;
156 long offset = (long)pc - (long)addr;
157
158 if (offset < -SZ_128M || offset >= SZ_128M) {
159 #ifdef CONFIG_ARM64_MODULE_PLTS
160 u32 replaced;
161
162
163
164
165
166
167 if (!mod) {
168 preempt_disable();
169 mod = __module_text_address(pc);
170 preempt_enable();
171
172 if (WARN_ON(!mod))
173 return -EINVAL;
174 }
175
176
177
178
179
180
181
182
183 if (aarch64_insn_read((void *)pc, &replaced))
184 return -EFAULT;
185
186 if (!aarch64_insn_is_bl(replaced) ||
187 !within_module(pc + aarch64_get_branch_offset(replaced),
188 mod))
189 return -EINVAL;
190
191 validate = false;
192 #else
193 return -EINVAL;
194 #endif
195 } else {
196 old = aarch64_insn_gen_branch_imm(pc, addr,
197 AARCH64_INSN_BRANCH_LINK);
198 }
199
200 new = aarch64_insn_gen_nop();
201
202 return ftrace_modify_code(pc, old, new, validate);
203 }
204
205 void arch_ftrace_update_code(int command)
206 {
207 command |= FTRACE_MAY_SLEEP;
208 ftrace_modify_all_code(command);
209 }
210
211 int __init ftrace_dyn_arch_init(void)
212 {
213 return 0;
214 }
215 #endif
216
217 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
218
219
220
221
222
223
224
225
226 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
227 unsigned long frame_pointer)
228 {
229 unsigned long return_hooker = (unsigned long)&return_to_handler;
230 unsigned long old;
231
232 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
233 return;
234
235
236
237
238
239
240 old = *parent;
241
242 if (!function_graph_enter(old, self_addr, frame_pointer, NULL))
243 *parent = return_hooker;
244 }
245
246 #ifdef CONFIG_DYNAMIC_FTRACE
247
248
249
250
251 static int ftrace_modify_graph_caller(bool enable)
252 {
253 unsigned long pc = (unsigned long)&ftrace_graph_call;
254 u32 branch, nop;
255
256 branch = aarch64_insn_gen_branch_imm(pc,
257 (unsigned long)ftrace_graph_caller,
258 AARCH64_INSN_BRANCH_NOLINK);
259 nop = aarch64_insn_gen_nop();
260
261 if (enable)
262 return ftrace_modify_code(pc, nop, branch, true);
263 else
264 return ftrace_modify_code(pc, branch, nop, true);
265 }
266
267 int ftrace_enable_ftrace_graph_caller(void)
268 {
269 return ftrace_modify_graph_caller(true);
270 }
271
272 int ftrace_disable_ftrace_graph_caller(void)
273 {
274 return ftrace_modify_graph_caller(false);
275 }
276 #endif
277 #endif