]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/kernel/ftrace.c
powerpc/ftrace: Use generic ftrace_modify_all_code()
[karo-tx-linux.git] / arch / powerpc / kernel / ftrace.c
1 /*
2  * Code for replacing ftrace calls with jumps.
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  *
6  * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
7  *
8  * Added function graph tracer code, taken from x86 that was written
9  * by Frederic Weisbecker, and ported to PPC by Steven Rostedt.
10  *
11  */
12
13 #define pr_fmt(fmt) "ftrace-powerpc: " fmt
14
15 #include <linux/spinlock.h>
16 #include <linux/hardirq.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/ftrace.h>
20 #include <linux/percpu.h>
21 #include <linux/init.h>
22 #include <linux/list.h>
23
24 #include <asm/cacheflush.h>
25 #include <asm/code-patching.h>
26 #include <asm/ftrace.h>
27 #include <asm/syscall.h>
28
29
30 #ifdef CONFIG_DYNAMIC_FTRACE
31 static unsigned int
32 ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
33 {
34         unsigned int op;
35
36         addr = ppc_function_entry((void *)addr);
37
38         /* if (link) set op to 'bl' else 'b' */
39         op = create_branch((unsigned int *)ip, addr, link ? 1 : 0);
40
41         return op;
42 }
43
44 static int
45 ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new)
46 {
47         unsigned int replaced;
48
49         /*
50          * Note: Due to modules and __init, code can
51          *  disappear and change, we need to protect against faulting
52          *  as well as code changing. We do this by using the
53          *  probe_kernel_* functions.
54          *
55          * No real locking needed, this code is run through
56          * kstop_machine, or before SMP starts.
57          */
58
59         /* read the text we want to modify */
60         if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE))
61                 return -EFAULT;
62
63         /* Make sure it is what we expect it to be */
64         if (replaced != old)
65                 return -EINVAL;
66
67         /* replace the text with the new text */
68         if (patch_instruction((unsigned int *)ip, new))
69                 return -EPERM;
70
71         return 0;
72 }
73
74 /*
75  * Helper functions that are the same for both PPC64 and PPC32.
76  */
77 static int test_24bit_addr(unsigned long ip, unsigned long addr)
78 {
79         addr = ppc_function_entry((void *)addr);
80
81         /* use the create_branch to verify that this offset can be branched */
82         return create_branch((unsigned int *)ip, addr, 0);
83 }
84
85 #ifdef CONFIG_MODULES
86
87 static int is_bl_op(unsigned int op)
88 {
89         return (op & 0xfc000003) == 0x48000001;
90 }
91
92 static unsigned long find_bl_target(unsigned long ip, unsigned int op)
93 {
94         static int offset;
95
96         offset = (op & 0x03fffffc);
97         /* make it signed */
98         if (offset & 0x02000000)
99                 offset |= 0xfe000000;
100
101         return ip + (long)offset;
102 }
103
104 #ifdef CONFIG_PPC64
105 static int
106 __ftrace_make_nop(struct module *mod,
107                   struct dyn_ftrace *rec, unsigned long addr)
108 {
109         unsigned long entry, ptr, tramp;
110         unsigned long ip = rec->ip;
111         unsigned int op;
112
113         /* read where this goes */
114         if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
115                 return -EFAULT;
116
117         /* Make sure that that this is still a 24bit jump */
118         if (!is_bl_op(op)) {
119                 pr_err("Not expected bl: opcode is %x\n", op);
120                 return -EINVAL;
121         }
122
123         /* lets find where the pointer goes */
124         tramp = find_bl_target(ip, op);
125
126         pr_devel("ip:%lx jumps to %lx", ip, tramp);
127
128         if (module_trampoline_target(mod, tramp, &ptr)) {
129                 pr_err("Failed to get trampoline target\n");
130                 return -EFAULT;
131         }
132
133         pr_devel("trampoline target %lx", ptr);
134
135         entry = ppc_global_function_entry((void *)addr);
136         /* This should match what was called */
137         if (ptr != entry) {
138                 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
139                 return -EINVAL;
140         }
141
142         /*
143          * Our original call site looks like:
144          *
145          * bl <tramp>
146          * ld r2,XX(r1)
147          *
148          * Milton Miller pointed out that we can not simply nop the branch.
149          * If a task was preempted when calling a trace function, the nops
150          * will remove the way to restore the TOC in r2 and the r2 TOC will
151          * get corrupted.
152          *
153          * Use a b +8 to jump over the load.
154          */
155         op = 0x48000008;        /* b +8 */
156
157         if (patch_instruction((unsigned int *)ip, op))
158                 return -EPERM;
159
160         return 0;
161 }
162
163 #else /* !PPC64 */
164 static int
165 __ftrace_make_nop(struct module *mod,
166                   struct dyn_ftrace *rec, unsigned long addr)
167 {
168         unsigned int op;
169         unsigned int jmp[4];
170         unsigned long ip = rec->ip;
171         unsigned long tramp;
172
173         if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
174                 return -EFAULT;
175
176         /* Make sure that that this is still a 24bit jump */
177         if (!is_bl_op(op)) {
178                 pr_err("Not expected bl: opcode is %x\n", op);
179                 return -EINVAL;
180         }
181
182         /* lets find where the pointer goes */
183         tramp = find_bl_target(ip, op);
184
185         /*
186          * On PPC32 the trampoline looks like:
187          *  0x3d, 0x80, 0x00, 0x00  lis r12,sym@ha
188          *  0x39, 0x8c, 0x00, 0x00  addi r12,r12,sym@l
189          *  0x7d, 0x89, 0x03, 0xa6  mtctr r12
190          *  0x4e, 0x80, 0x04, 0x20  bctr
191          */
192
193         pr_devel("ip:%lx jumps to %lx", ip, tramp);
194
195         /* Find where the trampoline jumps to */
196         if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
197                 pr_err("Failed to read %lx\n", tramp);
198                 return -EFAULT;
199         }
200
201         pr_devel(" %08x %08x ", jmp[0], jmp[1]);
202
203         /* verify that this is what we expect it to be */
204         if (((jmp[0] & 0xffff0000) != 0x3d800000) ||
205             ((jmp[1] & 0xffff0000) != 0x398c0000) ||
206             (jmp[2] != 0x7d8903a6) ||
207             (jmp[3] != 0x4e800420)) {
208                 pr_err("Not a trampoline\n");
209                 return -EINVAL;
210         }
211
212         tramp = (jmp[1] & 0xffff) |
213                 ((jmp[0] & 0xffff) << 16);
214         if (tramp & 0x8000)
215                 tramp -= 0x10000;
216
217         pr_devel(" %lx ", tramp);
218
219         if (tramp != addr) {
220                 pr_err("Trampoline location %08lx does not match addr\n",
221                        tramp);
222                 return -EINVAL;
223         }
224
225         op = PPC_INST_NOP;
226
227         if (patch_instruction((unsigned int *)ip, op))
228                 return -EPERM;
229
230         return 0;
231 }
232 #endif /* PPC64 */
233 #endif /* CONFIG_MODULES */
234
235 int ftrace_make_nop(struct module *mod,
236                     struct dyn_ftrace *rec, unsigned long addr)
237 {
238         unsigned long ip = rec->ip;
239         unsigned int old, new;
240
241         /*
242          * If the calling address is more that 24 bits away,
243          * then we had to use a trampoline to make the call.
244          * Otherwise just update the call site.
245          */
246         if (test_24bit_addr(ip, addr)) {
247                 /* within range */
248                 old = ftrace_call_replace(ip, addr, 1);
249                 new = PPC_INST_NOP;
250                 return ftrace_modify_code(ip, old, new);
251         }
252
253 #ifdef CONFIG_MODULES
254         /*
255          * Out of range jumps are called from modules.
256          * We should either already have a pointer to the module
257          * or it has been passed in.
258          */
259         if (!rec->arch.mod) {
260                 if (!mod) {
261                         pr_err("No module loaded addr=%lx\n", addr);
262                         return -EFAULT;
263                 }
264                 rec->arch.mod = mod;
265         } else if (mod) {
266                 if (mod != rec->arch.mod) {
267                         pr_err("Record mod %p not equal to passed in mod %p\n",
268                                rec->arch.mod, mod);
269                         return -EINVAL;
270                 }
271                 /* nothing to do if mod == rec->arch.mod */
272         } else
273                 mod = rec->arch.mod;
274
275         return __ftrace_make_nop(mod, rec, addr);
276 #else
277         /* We should not get here without modules */
278         return -EINVAL;
279 #endif /* CONFIG_MODULES */
280 }
281
282 #ifdef CONFIG_MODULES
283 #ifdef CONFIG_PPC64
284 static int
285 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
286 {
287         unsigned int op[2];
288         void *ip = (void *)rec->ip;
289
290         /* read where this goes */
291         if (probe_kernel_read(op, ip, sizeof(op)))
292                 return -EFAULT;
293
294         /*
295          * We expect to see:
296          *
297          * b +8
298          * ld r2,XX(r1)
299          *
300          * The load offset is different depending on the ABI. For simplicity
301          * just mask it out when doing the compare.
302          */
303         if ((op[0] != 0x48000008) || ((op[1] & 0xffff0000) != 0xe8410000)) {
304                 pr_err("Unexpected call sequence: %x %x\n", op[0], op[1]);
305                 return -EINVAL;
306         }
307
308         /* If we never set up a trampoline to ftrace_caller, then bail */
309         if (!rec->arch.mod->arch.tramp) {
310                 pr_err("No ftrace trampoline\n");
311                 return -EINVAL;
312         }
313
314         /* Ensure branch is within 24 bits */
315         if (!create_branch(ip, rec->arch.mod->arch.tramp, BRANCH_SET_LINK)) {
316                 pr_err("Branch out of range\n");
317                 return -EINVAL;
318         }
319
320         if (patch_branch(ip, rec->arch.mod->arch.tramp, BRANCH_SET_LINK)) {
321                 pr_err("REL24 out of range!\n");
322                 return -EINVAL;
323         }
324
325         return 0;
326 }
327 #else
328 static int
329 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
330 {
331         unsigned int op;
332         unsigned long ip = rec->ip;
333
334         /* read where this goes */
335         if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
336                 return -EFAULT;
337
338         /* It should be pointing to a nop */
339         if (op != PPC_INST_NOP) {
340                 pr_err("Expected NOP but have %x\n", op);
341                 return -EINVAL;
342         }
343
344         /* If we never set up a trampoline to ftrace_caller, then bail */
345         if (!rec->arch.mod->arch.tramp) {
346                 pr_err("No ftrace trampoline\n");
347                 return -EINVAL;
348         }
349
350         /* create the branch to the trampoline */
351         op = create_branch((unsigned int *)ip,
352                            rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
353         if (!op) {
354                 pr_err("REL24 out of range!\n");
355                 return -EINVAL;
356         }
357
358         pr_devel("write to %lx\n", rec->ip);
359
360         if (patch_instruction((unsigned int *)ip, op))
361                 return -EPERM;
362
363         return 0;
364 }
365 #endif /* CONFIG_PPC64 */
366 #endif /* CONFIG_MODULES */
367
368 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
369 {
370         unsigned long ip = rec->ip;
371         unsigned int old, new;
372
373         /*
374          * If the calling address is more that 24 bits away,
375          * then we had to use a trampoline to make the call.
376          * Otherwise just update the call site.
377          */
378         if (test_24bit_addr(ip, addr)) {
379                 /* within range */
380                 old = PPC_INST_NOP;
381                 new = ftrace_call_replace(ip, addr, 1);
382                 return ftrace_modify_code(ip, old, new);
383         }
384
385 #ifdef CONFIG_MODULES
386         /*
387          * Out of range jumps are called from modules.
388          * Being that we are converting from nop, it had better
389          * already have a module defined.
390          */
391         if (!rec->arch.mod) {
392                 pr_err("No module loaded\n");
393                 return -EINVAL;
394         }
395
396         return __ftrace_make_call(rec, addr);
397 #else
398         /* We should not get here without modules */
399         return -EINVAL;
400 #endif /* CONFIG_MODULES */
401 }
402
403 int ftrace_update_ftrace_func(ftrace_func_t func)
404 {
405         unsigned long ip = (unsigned long)(&ftrace_call);
406         unsigned int old, new;
407         int ret;
408
409         old = *(unsigned int *)&ftrace_call;
410         new = ftrace_call_replace(ip, (unsigned long)func, 1);
411         ret = ftrace_modify_code(ip, old, new);
412
413         return ret;
414 }
415
416 static int __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
417 {
418         unsigned long ftrace_addr = (unsigned long)FTRACE_ADDR;
419         int ret;
420
421         ret = ftrace_update_record(rec, enable);
422
423         switch (ret) {
424         case FTRACE_UPDATE_IGNORE:
425                 return 0;
426         case FTRACE_UPDATE_MAKE_CALL:
427                 return ftrace_make_call(rec, ftrace_addr);
428         case FTRACE_UPDATE_MAKE_NOP:
429                 return ftrace_make_nop(NULL, rec, ftrace_addr);
430         }
431
432         return 0;
433 }
434
435 void ftrace_replace_code(int enable)
436 {
437         struct ftrace_rec_iter *iter;
438         struct dyn_ftrace *rec;
439         int ret;
440
441         for (iter = ftrace_rec_iter_start(); iter;
442              iter = ftrace_rec_iter_next(iter)) {
443                 rec = ftrace_rec_iter_record(iter);
444                 ret = __ftrace_replace_code(rec, enable);
445                 if (ret) {
446                         ftrace_bug(ret, rec);
447                         return;
448                 }
449         }
450 }
451
452 /*
453  * Use the default ftrace_modify_all_code, but without
454  * stop_machine().
455  */
456 void arch_ftrace_update_code(int command)
457 {
458         ftrace_modify_all_code(command);
459 }
460
461 int __init ftrace_dyn_arch_init(void)
462 {
463         return 0;
464 }
465 #endif /* CONFIG_DYNAMIC_FTRACE */
466
467 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
468
469 #ifdef CONFIG_DYNAMIC_FTRACE
470 extern void ftrace_graph_call(void);
471 extern void ftrace_graph_stub(void);
472
473 int ftrace_enable_ftrace_graph_caller(void)
474 {
475         unsigned long ip = (unsigned long)(&ftrace_graph_call);
476         unsigned long addr = (unsigned long)(&ftrace_graph_caller);
477         unsigned long stub = (unsigned long)(&ftrace_graph_stub);
478         unsigned int old, new;
479
480         old = ftrace_call_replace(ip, stub, 0);
481         new = ftrace_call_replace(ip, addr, 0);
482
483         return ftrace_modify_code(ip, old, new);
484 }
485
486 int ftrace_disable_ftrace_graph_caller(void)
487 {
488         unsigned long ip = (unsigned long)(&ftrace_graph_call);
489         unsigned long addr = (unsigned long)(&ftrace_graph_caller);
490         unsigned long stub = (unsigned long)(&ftrace_graph_stub);
491         unsigned int old, new;
492
493         old = ftrace_call_replace(ip, addr, 0);
494         new = ftrace_call_replace(ip, stub, 0);
495
496         return ftrace_modify_code(ip, old, new);
497 }
498 #endif /* CONFIG_DYNAMIC_FTRACE */
499
500 /*
501  * Hook the return address and push it in the stack of return addrs
502  * in current thread info. Return the address we want to divert to.
503  */
504 unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip)
505 {
506         struct ftrace_graph_ent trace;
507         unsigned long return_hooker;
508
509         if (unlikely(ftrace_graph_is_dead()))
510                 goto out;
511
512         if (unlikely(atomic_read(&current->tracing_graph_pause)))
513                 goto out;
514
515         return_hooker = ppc_function_entry(return_to_handler);
516
517         trace.func = ip;
518         trace.depth = current->curr_ret_stack + 1;
519
520         /* Only trace if the calling function expects to */
521         if (!ftrace_graph_entry(&trace))
522                 goto out;
523
524         if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY)
525                 goto out;
526
527         parent = return_hooker;
528 out:
529         return parent;
530 }
531 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
532
533 #if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
534 unsigned long __init arch_syscall_addr(int nr)
535 {
536         return sys_call_table[nr*2];
537 }
538 #endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */