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