ftrace, powerpc: replace debug macro with proper pr_deug
[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  */
9
10 #include <linux/spinlock.h>
11 #include <linux/hardirq.h>
12 #include <linux/uaccess.h>
13 #include <linux/module.h>
14 #include <linux/ftrace.h>
15 #include <linux/percpu.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18
19 #include <asm/cacheflush.h>
20 #include <asm/code-patching.h>
21 #include <asm/ftrace.h>
22
23 static unsigned int ftrace_nop = PPC_NOP_INSTR;
24
25 #ifdef CONFIG_PPC32
26 # define GET_ADDR(addr) addr
27 #else
28 /* PowerPC64's functions are data that points to the functions */
29 # define GET_ADDR(addr) (*(unsigned long *)addr)
30 #endif
31
32
33 static unsigned int ftrace_calc_offset(long ip, long addr)
34 {
35         return (int)(addr - ip);
36 }
37
38 static unsigned char *ftrace_nop_replace(void)
39 {
40         return (char *)&ftrace_nop;
41 }
42
43 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
44 {
45         static unsigned int op;
46
47         /*
48          * It would be nice to just use create_function_call, but that will
49          * update the code itself. Here we need to just return the
50          * instruction that is going to be modified, without modifying the
51          * code.
52          */
53         addr = GET_ADDR(addr);
54
55         /* Set to "bl addr" */
56         op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
57
58         /*
59          * No locking needed, this must be called via kstop_machine
60          * which in essence is like running on a uniprocessor machine.
61          */
62         return (unsigned char *)&op;
63 }
64
65 #ifdef CONFIG_PPC64
66 # define _ASM_ALIGN     " .align 3 "
67 # define _ASM_PTR       " .llong "
68 #else
69 # define _ASM_ALIGN     " .align 2 "
70 # define _ASM_PTR       " .long "
71 #endif
72
73 static int
74 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
75                    unsigned char *new_code)
76 {
77         unsigned char replaced[MCOUNT_INSN_SIZE];
78
79         /*
80          * Note: Due to modules and __init, code can
81          *  disappear and change, we need to protect against faulting
82          *  as well as code changing. We do this by using the
83          *  probe_kernel_* functions.
84          *
85          * No real locking needed, this code is run through
86          * kstop_machine, or before SMP starts.
87          */
88
89         /* read the text we want to modify */
90         if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
91                 return -EFAULT;
92
93         /* Make sure it is what we expect it to be */
94         if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
95                 return -EINVAL;
96
97         /* replace the text with the new text */
98         if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
99                 return -EPERM;
100
101         flush_icache_range(ip, ip + 8);
102
103         return 0;
104 }
105
106 /*
107  * Helper functions that are the same for both PPC64 and PPC32.
108  */
109 static int test_24bit_addr(unsigned long ip, unsigned long addr)
110 {
111
112         /* use the create_branch to verify that this offset can be branched */
113         return create_branch((unsigned int *)ip, addr, 0);
114 }
115
116 static int is_bl_op(unsigned int op)
117 {
118         return (op & 0xfc000003) == 0x48000001;
119 }
120
121 static unsigned long find_bl_target(unsigned long ip, unsigned int op)
122 {
123         static int offset;
124
125         offset = (op & 0x03fffffc);
126         /* make it signed */
127         if (offset & 0x02000000)
128                 offset |= 0xfe000000;
129
130         return ip + (long)offset;
131 }
132
133 #ifdef CONFIG_PPC64
134 static int
135 __ftrace_make_nop(struct module *mod,
136                   struct dyn_ftrace *rec, unsigned long addr)
137 {
138         unsigned int op;
139         unsigned int jmp[5];
140         unsigned long ptr;
141         unsigned long ip = rec->ip;
142         unsigned long tramp;
143         int offset;
144
145         /* read where this goes */
146         if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
147                 return -EFAULT;
148
149         /* Make sure that that this is still a 24bit jump */
150         if (!is_bl_op(op)) {
151                 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
152                 return -EINVAL;
153         }
154
155         /* lets find where the pointer goes */
156         tramp = find_bl_target(ip, op);
157
158         /*
159          * On PPC64 the trampoline looks like:
160          * 0x3d, 0x82, 0x00, 0x00,    addis   r12,r2, <high>
161          * 0x39, 0x8c, 0x00, 0x00,    addi    r12,r12, <low>
162          *   Where the bytes 2,3,6 and 7 make up the 32bit offset
163          *   to the TOC that holds the pointer.
164          *   to jump to.
165          * 0xf8, 0x41, 0x00, 0x28,    std     r2,40(r1)
166          * 0xe9, 0x6c, 0x00, 0x20,    ld      r11,32(r12)
167          *   The actually address is 32 bytes from the offset
168          *   into the TOC.
169          * 0xe8, 0x4c, 0x00, 0x28,    ld      r2,40(r12)
170          */
171
172         pr_debug("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
173
174         /* Find where the trampoline jumps to */
175         if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
176                 printk(KERN_ERR "Failed to read %lx\n", tramp);
177                 return -EFAULT;
178         }
179
180         pr_debug(" %08x %08x", jmp[0], jmp[1]);
181
182         /* verify that this is what we expect it to be */
183         if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
184             ((jmp[1] & 0xffff0000) != 0x398c0000) ||
185             (jmp[2] != 0xf8410028) ||
186             (jmp[3] != 0xe96c0020) ||
187             (jmp[4] != 0xe84c0028)) {
188                 printk(KERN_ERR "Not a trampoline\n");
189                 return -EINVAL;
190         }
191
192         /* The bottom half is signed extended */
193         offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
194                 (int)((short)jmp[1]);
195
196         pr_debug(" %x ", offset);
197
198         /* get the address this jumps too */
199         tramp = mod->arch.toc + offset + 32;
200         pr_debug("toc: %lx", tramp);
201
202         if (probe_kernel_read(jmp, (void *)tramp, 8)) {
203                 printk(KERN_ERR "Failed to read %lx\n", tramp);
204                 return -EFAULT;
205         }
206
207         pr_debug(" %08x %08x\n", jmp[0], jmp[1]);
208
209         ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
210
211         /* This should match what was called */
212         if (ptr != GET_ADDR(addr)) {
213                 printk(KERN_ERR "addr does not match %lx\n", ptr);
214                 return -EINVAL;
215         }
216
217         /*
218          * We want to nop the line, but the next line is
219          *  0xe8, 0x41, 0x00, 0x28   ld r2,40(r1)
220          * This needs to be turned to a nop too.
221          */
222         if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
223                 return -EFAULT;
224
225         if (op != 0xe8410028) {
226                 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
227                 return -EINVAL;
228         }
229
230         /*
231          * Milton Miller pointed out that we can not blindly do nops.
232          * If a task was preempted when calling a trace function,
233          * the nops will remove the way to restore the TOC in r2
234          * and the r2 TOC will get corrupted.
235          */
236
237         /*
238          * Replace:
239          *   bl <tramp>  <==== will be replaced with "b 1f"
240          *   ld r2,40(r1)
241          *  1:
242          */
243         op = 0x48000008;        /* b +8 */
244
245         if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
246                 return -EPERM;
247
248
249         flush_icache_range(ip, ip + 8);
250
251         return 0;
252 }
253
254 #else /* !PPC64 */
255 static int
256 __ftrace_make_nop(struct module *mod,
257                   struct dyn_ftrace *rec, unsigned long addr)
258 {
259         unsigned int op;
260         unsigned int jmp[4];
261         unsigned long ip = rec->ip;
262         unsigned long tramp;
263
264         if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
265                 return -EFAULT;
266
267         /* Make sure that that this is still a 24bit jump */
268         if (!is_bl_op(op)) {
269                 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
270                 return -EINVAL;
271         }
272
273         /* lets find where the pointer goes */
274         tramp = find_bl_target(ip, op);
275
276         /*
277          * On PPC32 the trampoline looks like:
278          *  0x3d, 0x60, 0x00, 0x00  lis r11,sym@ha
279          *  0x39, 0x6b, 0x00, 0x00  addi r11,r11,sym@l
280          *  0x7d, 0x69, 0x03, 0xa6  mtctr r11
281          *  0x4e, 0x80, 0x04, 0x20  bctr
282          */
283
284         pr_debug("ip:%lx jumps to %lx", ip, tramp);
285
286         /* Find where the trampoline jumps to */
287         if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
288                 printk(KERN_ERR "Failed to read %lx\n", tramp);
289                 return -EFAULT;
290         }
291
292         pr_debug(" %08x %08x ", jmp[0], jmp[1]);
293
294         /* verify that this is what we expect it to be */
295         if (((jmp[0] & 0xffff0000) != 0x3d600000) ||
296             ((jmp[1] & 0xffff0000) != 0x396b0000) ||
297             (jmp[2] != 0x7d6903a6) ||
298             (jmp[3] != 0x4e800420)) {
299                 printk(KERN_ERR "Not a trampoline\n");
300                 return -EINVAL;
301         }
302
303         tramp = (jmp[1] & 0xffff) |
304                 ((jmp[0] & 0xffff) << 16);
305         if (tramp & 0x8000)
306                 tramp -= 0x10000;
307
308         pr_debug(" %x ", tramp);
309
310         if (tramp != addr) {
311                 printk(KERN_ERR
312                        "Trampoline location %08lx does not match addr\n",
313                        tramp);
314                 return -EINVAL;
315         }
316
317         op = PPC_NOP_INSTR;
318
319         if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
320                 return -EPERM;
321
322         flush_icache_range(ip, ip + 8);
323
324         return 0;
325 }
326 #endif /* PPC64 */
327
328 int ftrace_make_nop(struct module *mod,
329                     struct dyn_ftrace *rec, unsigned long addr)
330 {
331         unsigned char *old, *new;
332         unsigned long ip = rec->ip;
333
334         /*
335          * If the calling address is more that 24 bits away,
336          * then we had to use a trampoline to make the call.
337          * Otherwise just update the call site.
338          */
339         if (test_24bit_addr(ip, addr)) {
340                 /* within range */
341                 old = ftrace_call_replace(ip, addr);
342                 new = ftrace_nop_replace();
343                 return ftrace_modify_code(ip, old, new);
344         }
345
346         /*
347          * Out of range jumps are called from modules.
348          * We should either already have a pointer to the module
349          * or it has been passed in.
350          */
351         if (!rec->arch.mod) {
352                 if (!mod) {
353                         printk(KERN_ERR "No module loaded addr=%lx\n",
354                                addr);
355                         return -EFAULT;
356                 }
357                 rec->arch.mod = mod;
358         } else if (mod) {
359                 if (mod != rec->arch.mod) {
360                         printk(KERN_ERR
361                                "Record mod %p not equal to passed in mod %p\n",
362                                rec->arch.mod, mod);
363                         return -EINVAL;
364                 }
365                 /* nothing to do if mod == rec->arch.mod */
366         } else
367                 mod = rec->arch.mod;
368
369         return __ftrace_make_nop(mod, rec, addr);
370
371 }
372
373 #ifdef CONFIG_PPC64
374 static int
375 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
376 {
377         unsigned int op[2];
378         unsigned long ip = rec->ip;
379
380         /* read where this goes */
381         if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
382                 return -EFAULT;
383
384         /*
385          * It should be pointing to two nops or
386          *  b +8; ld r2,40(r1)
387          */
388         if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
389             ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
390                 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
391                 return -EINVAL;
392         }
393
394         /* If we never set up a trampoline to ftrace_caller, then bail */
395         if (!rec->arch.mod->arch.tramp) {
396                 printk(KERN_ERR "No ftrace trampoline\n");
397                 return -EINVAL;
398         }
399
400         /* create the branch to the trampoline */
401         op[0] = create_branch((unsigned int *)ip,
402                               rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
403         if (!op[0]) {
404                 printk(KERN_ERR "REL24 out of range!\n");
405                 return -EINVAL;
406         }
407
408         /* ld r2,40(r1) */
409         op[1] = 0xe8410028;
410
411         pr_debug("write to %lx\n", rec->ip);
412
413         if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
414                 return -EPERM;
415
416         flush_icache_range(ip, ip + 8);
417
418         return 0;
419 }
420 #else
421 static int
422 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
423 {
424         unsigned int op;
425         unsigned long ip = rec->ip;
426
427         /* read where this goes */
428         if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
429                 return -EFAULT;
430
431         /* It should be pointing to a nop */
432         if (op != PPC_NOP_INSTR) {
433                 printk(KERN_ERR "Expected NOP but have %x\n", op);
434                 return -EINVAL;
435         }
436
437         /* If we never set up a trampoline to ftrace_caller, then bail */
438         if (!rec->arch.mod->arch.tramp) {
439                 printk(KERN_ERR "No ftrace trampoline\n");
440                 return -EINVAL;
441         }
442
443         /* create the branch to the trampoline */
444         op = create_branch((unsigned int *)ip,
445                            rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
446         if (!op) {
447                 printk(KERN_ERR "REL24 out of range!\n");
448                 return -EINVAL;
449         }
450
451         pr_debug("write to %lx\n", rec->ip);
452
453         if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
454                 return -EPERM;
455
456         flush_icache_range(ip, ip + 8);
457
458         return 0;
459 }
460 #endif /* CONFIG_PPC64 */
461
462 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
463 {
464         unsigned char *old, *new;
465         unsigned long ip = rec->ip;
466
467         /*
468          * If the calling address is more that 24 bits away,
469          * then we had to use a trampoline to make the call.
470          * Otherwise just update the call site.
471          */
472         if (test_24bit_addr(ip, addr)) {
473                 /* within range */
474                 old = ftrace_nop_replace();
475                 new = ftrace_call_replace(ip, addr);
476                 return ftrace_modify_code(ip, old, new);
477         }
478
479         /*
480          * Out of range jumps are called from modules.
481          * Being that we are converting from nop, it had better
482          * already have a module defined.
483          */
484         if (!rec->arch.mod) {
485                 printk(KERN_ERR "No module loaded\n");
486                 return -EINVAL;
487         }
488
489         return __ftrace_make_call(rec, addr);
490 }
491
492 int ftrace_update_ftrace_func(ftrace_func_t func)
493 {
494         unsigned long ip = (unsigned long)(&ftrace_call);
495         unsigned char old[MCOUNT_INSN_SIZE], *new;
496         int ret;
497
498         memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
499         new = ftrace_call_replace(ip, (unsigned long)func);
500         ret = ftrace_modify_code(ip, old, new);
501
502         return ret;
503 }
504
505 int __init ftrace_dyn_arch_init(void *data)
506 {
507         /* caller expects data to be zero */
508         unsigned long *p = data;
509
510         *p = 0;
511
512         return 0;
513 }