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