1
2
3
4
5
6
7
8
9
10
11#include <linux/uaccess.h>
12#include <linux/init.h>
13#include <linux/ftrace.h>
14
15#include <asm/asm.h>
16#include <asm/asm-offsets.h>
17#include <asm/cacheflush.h>
18#include <asm/uasm.h>
19
20#include <asm-generic/sections.h>
21
22#if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
23#define MCOUNT_OFFSET_INSNS 5
24#else
25#define MCOUNT_OFFSET_INSNS 4
26#endif
27
28
29
30
31
32
33
34static inline int in_kernel_space(unsigned long ip)
35{
36 if (ip >= (unsigned long)_stext &&
37 ip <= (unsigned long)_etext)
38 return 1;
39 return 0;
40}
41
42#ifdef CONFIG_DYNAMIC_FTRACE
43
44#define JAL 0x0c000000
45#define ADDR_MASK 0x03ffffff
46#define JUMP_RANGE_MASK ((1UL << 28) - 1)
47
48#define INSN_NOP 0x00000000
49#define INSN_JAL(addr) \
50 ((unsigned int)(JAL | (((addr) >> 2) & ADDR_MASK)))
51
52static unsigned int insn_jal_ftrace_caller __read_mostly;
53static unsigned int insn_lui_v1_hi16_mcount __read_mostly;
54static unsigned int insn_j_ftrace_graph_caller __maybe_unused __read_mostly;
55
56static inline void ftrace_dyn_arch_init_insns(void)
57{
58 u32 *buf;
59 unsigned int v1;
60
61
62 v1 = 3;
63 buf = (u32 *)&insn_lui_v1_hi16_mcount;
64 UASM_i_LA_mostly(&buf, v1, MCOUNT_ADDR);
65
66
67 buf = (u32 *)&insn_jal_ftrace_caller;
68 uasm_i_jal(&buf, (FTRACE_ADDR + 8) & JUMP_RANGE_MASK);
69
70#ifdef CONFIG_FUNCTION_GRAPH_TRACER
71
72 buf = (u32 *)&insn_j_ftrace_graph_caller;
73 uasm_i_j(&buf, (unsigned long)ftrace_graph_caller & JUMP_RANGE_MASK);
74#endif
75}
76
77static int ftrace_modify_code(unsigned long ip, unsigned int new_code)
78{
79 int faulted;
80
81
82 safe_store_code(new_code, ip, faulted);
83
84 if (unlikely(faulted))
85 return -EFAULT;
86
87 flush_icache_range(ip, ip + 8);
88
89 return 0;
90}
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121#define INSN_B_1F (0x10000000 | MCOUNT_OFFSET_INSNS)
122
123int ftrace_make_nop(struct module *mod,
124 struct dyn_ftrace *rec, unsigned long addr)
125{
126 unsigned int new;
127 unsigned long ip = rec->ip;
128
129
130
131
132
133 new = in_kernel_space(ip) ? INSN_NOP : INSN_B_1F;
134
135 return ftrace_modify_code(ip, new);
136}
137
138int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
139{
140 unsigned int new;
141 unsigned long ip = rec->ip;
142
143 new = in_kernel_space(ip) ? insn_jal_ftrace_caller :
144 insn_lui_v1_hi16_mcount;
145
146 return ftrace_modify_code(ip, new);
147}
148
149#define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
150
151int ftrace_update_ftrace_func(ftrace_func_t func)
152{
153 unsigned int new;
154
155 new = INSN_JAL((unsigned long)func);
156
157 return ftrace_modify_code(FTRACE_CALL_IP, new);
158}
159
160int __init ftrace_dyn_arch_init(void *data)
161{
162
163 ftrace_dyn_arch_init_insns();
164
165
166 ftrace_modify_code(MCOUNT_ADDR, INSN_NOP);
167
168
169 *(unsigned long *)data = 0;
170
171 return 0;
172}
173#endif
174
175#ifdef CONFIG_FUNCTION_GRAPH_TRACER
176
177#ifdef CONFIG_DYNAMIC_FTRACE
178
179extern void ftrace_graph_call(void);
180#define FTRACE_GRAPH_CALL_IP ((unsigned long)(&ftrace_graph_call))
181
182int ftrace_enable_ftrace_graph_caller(void)
183{
184 return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
185 insn_j_ftrace_graph_caller);
186}
187
188int ftrace_disable_ftrace_graph_caller(void)
189{
190 return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, INSN_NOP);
191}
192
193#endif
194
195#ifndef KBUILD_MCOUNT_RA_ADDRESS
196
197#define S_RA_SP (0xafbf << 16)
198#define S_R_SP (0xafb0 << 16)
199#define OFFSET_MASK 0xffff
200
201unsigned long ftrace_get_parent_ra_addr(unsigned long self_ra, unsigned long
202 old_parent_ra, unsigned long parent_ra_addr, unsigned long fp)
203{
204 unsigned long sp, ip, tmp;
205 unsigned int code;
206 int faulted;
207
208
209
210
211
212
213 ip = self_ra - (in_kernel_space(self_ra) ? 16 : 24);
214
215
216
217
218
219 do {
220
221 safe_load_code(code, ip, faulted);
222
223 if (unlikely(faulted))
224 return 0;
225
226
227
228
229
230 if ((code & S_R_SP) != S_R_SP)
231 return parent_ra_addr;
232
233
234 ip -= 4;
235 } while ((code & S_RA_SP) != S_RA_SP);
236
237 sp = fp + (code & OFFSET_MASK);
238
239
240 safe_load_stack(tmp, sp, faulted);
241 if (unlikely(faulted))
242 return 0;
243
244 if (tmp == old_parent_ra)
245 return sp;
246 return 0;
247}
248
249#endif
250
251
252
253
254
255void prepare_ftrace_return(unsigned long *parent_ra_addr, unsigned long self_ra,
256 unsigned long fp)
257{
258 unsigned long old_parent_ra;
259 struct ftrace_graph_ent trace;
260 unsigned long return_hooker = (unsigned long)
261 &return_to_handler;
262 int faulted, insns;
263
264 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
265 return;
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 safe_load_stack(old_parent_ra, parent_ra_addr, faulted);
286 if (unlikely(faulted))
287 goto out;
288#ifndef KBUILD_MCOUNT_RA_ADDRESS
289 parent_ra_addr = (unsigned long *)ftrace_get_parent_ra_addr(self_ra,
290 old_parent_ra, (unsigned long)parent_ra_addr, fp);
291
292
293
294
295 if (parent_ra_addr == 0)
296 goto out;
297#endif
298
299 safe_store_stack(return_hooker, parent_ra_addr, faulted);
300 if (unlikely(faulted))
301 goto out;
302
303 if (ftrace_push_return_trace(old_parent_ra, self_ra, &trace.depth, fp)
304 == -EBUSY) {
305 *parent_ra_addr = old_parent_ra;
306 return;
307 }
308
309
310
311
312
313
314
315 insns = in_kernel_space(self_ra) ? 2 : MCOUNT_OFFSET_INSNS + 1;
316 trace.func = self_ra - (MCOUNT_INSN_SIZE * insns);
317
318
319 if (!ftrace_graph_entry(&trace)) {
320 current->curr_ret_stack--;
321 *parent_ra_addr = old_parent_ra;
322 }
323 return;
324out:
325 ftrace_graph_stop();
326 WARN_ON(1);
327}
328#endif
329