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