2 * Code for replacing ftrace calls with jumps.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
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>
19 #include <asm/cacheflush.h>
20 #include <asm/code-patching.h>
21 #include <asm/ftrace.h>
26 #define DEBUGP(fmt , ...) do { } while (0)
29 static unsigned int ftrace_nop = PPC_NOP_INSTR;
32 # define GET_ADDR(addr) addr
34 /* PowerPC64's functions are data that points to the functions */
35 # define GET_ADDR(addr) (*(unsigned long *)addr)
39 static unsigned int ftrace_calc_offset(long ip, long addr)
41 return (int)(addr - ip);
44 static unsigned char *ftrace_nop_replace(void)
46 return (char *)&ftrace_nop;
49 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
51 static unsigned int op;
54 * It would be nice to just use create_function_call, but that will
55 * update the code itself. Here we need to just return the
56 * instruction that is going to be modified, without modifying the
59 addr = GET_ADDR(addr);
61 /* Set to "bl addr" */
62 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
65 * No locking needed, this must be called via kstop_machine
66 * which in essence is like running on a uniprocessor machine.
68 return (unsigned char *)&op;
72 # define _ASM_ALIGN " .align 3 "
73 # define _ASM_PTR " .llong "
75 # define _ASM_ALIGN " .align 2 "
76 # define _ASM_PTR " .long "
80 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
81 unsigned char *new_code)
83 unsigned char replaced[MCOUNT_INSN_SIZE];
86 * Note: Due to modules and __init, code can
87 * disappear and change, we need to protect against faulting
88 * as well as code changing. We do this by using the
89 * probe_kernel_* functions.
91 * No real locking needed, this code is run through
92 * kstop_machine, or before SMP starts.
95 /* read the text we want to modify */
96 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
99 /* Make sure it is what we expect it to be */
100 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
103 /* replace the text with the new text */
104 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
107 flush_icache_range(ip, ip + 8);
113 * Helper functions that are the same for both PPC64 and PPC32.
115 static int test_24bit_addr(unsigned long ip, unsigned long addr)
120 * Can we get to addr from ip in 24 bits?
121 * (26 really, since we mulitply by 4 for 4 byte alignment)
126 * Return true if diff is less than 1 << 25
127 * and greater than -1 << 26.
129 return (diff < (1 << 25)) && (diff > (-1 << 26));
132 static int is_bl_op(unsigned int op)
134 return (op & 0xfc000003) == 0x48000001;
137 static int test_offset(unsigned long offset)
139 return (offset + 0x2000000 > 0x3ffffff) || ((offset & 3) != 0);
142 static unsigned long find_bl_target(unsigned long ip, unsigned int op)
146 offset = (op & 0x03fffffc);
148 if (offset & 0x02000000)
149 offset |= 0xfe000000;
151 return ip + (long)offset;
154 static unsigned int branch_offset(unsigned long offset)
156 /* return "bl ip+offset" */
157 return 0x48000001 | (offset & 0x03fffffc);
162 __ftrace_make_nop(struct module *mod,
163 struct dyn_ftrace *rec, unsigned long addr)
165 unsigned char replaced[MCOUNT_INSN_SIZE * 2];
166 unsigned int *op = (unsigned *)&replaced;
167 unsigned char jmp[8];
168 unsigned long *ptr = (unsigned long *)&jmp;
169 unsigned long ip = rec->ip;
173 /* read where this goes */
174 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
177 /* Make sure that that this is still a 24bit jump */
178 if (!is_bl_op(*op)) {
179 printk(KERN_ERR "Not expected bl: opcode is %x\n", *op);
183 /* lets find where the pointer goes */
184 tramp = find_bl_target(ip, *op);
187 * On PPC64 the trampoline looks like:
188 * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
189 * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
190 * Where the bytes 2,3,6 and 7 make up the 32bit offset
191 * to the TOC that holds the pointer.
193 * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
194 * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
195 * The actually address is 32 bytes from the offset
197 * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
200 DEBUGP("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
202 /* Find where the trampoline jumps to */
203 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
204 printk(KERN_ERR "Failed to read %lx\n", tramp);
209 (unsigned)(*ptr >> 32),
212 offset = (unsigned)jmp[2] << 24 |
213 (unsigned)jmp[3] << 16 |
214 (unsigned)jmp[6] << 8 |
217 DEBUGP(" %x ", offset);
219 /* get the address this jumps too */
220 tramp = mod->arch.toc + offset + 32;
221 DEBUGP("toc: %lx", tramp);
223 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
224 printk(KERN_ERR "Failed to read %lx\n", tramp);
228 DEBUGP(" %08x %08x\n",
229 (unsigned)(*ptr >> 32),
232 /* This should match what was called */
233 if (*ptr != GET_ADDR(addr)) {
234 printk(KERN_ERR "addr does not match %lx\n", *ptr);
239 * We want to nop the line, but the next line is
240 * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
241 * This needs to be turned to a nop too.
243 if (probe_kernel_read(replaced, (void *)(ip+4), MCOUNT_INSN_SIZE))
246 if (*op != 0xe8410028) {
247 printk(KERN_ERR "Next line is not ld! (%08x)\n", *op);
252 * Milton Miller pointed out that we can not blindly do nops.
253 * If a task was preempted when calling a trace function,
254 * the nops will remove the way to restore the TOC in r2
255 * and the r2 TOC will get corrupted.
260 * bl <tramp> <==== will be replaced with "b 1f"
264 op[0] = 0x48000008; /* b +8 */
266 if (probe_kernel_write((void *)ip, replaced, MCOUNT_INSN_SIZE))
274 __ftrace_make_nop(struct module *mod,
275 struct dyn_ftrace *rec, unsigned long addr)
277 unsigned char replaced[MCOUNT_INSN_SIZE];
278 unsigned int *op = (unsigned *)&replaced;
279 unsigned char jmp[8];
280 unsigned int *ptr = (unsigned int *)&jmp;
281 unsigned long ip = rec->ip;
285 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
288 /* Make sure that that this is still a 24bit jump */
289 if (!is_bl_op(*op)) {
290 printk(KERN_ERR "Not expected bl: opcode is %x\n", *op);
294 /* lets find where the pointer goes */
295 tramp = find_bl_target(ip, *op);
298 * On PPC32 the trampoline looks like:
305 DEBUGP("ip:%lx jumps to %lx", ip, tramp);
307 /* Find where the trampoline jumps to */
308 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
309 printk(KERN_ERR "Failed to read %lx\n", tramp);
313 DEBUGP(" %08x %08x ", ptr[0], ptr[1]);
315 tramp = (ptr[1] & 0xffff) |
316 ((ptr[0] & 0xffff) << 16);
320 DEBUGP(" %x ", tramp);
324 "Trampoline location %08lx does not match addr\n",
329 op[0] = PPC_NOP_INSTR;
331 if (probe_kernel_write((void *)ip, replaced, MCOUNT_INSN_SIZE))
338 int ftrace_make_nop(struct module *mod,
339 struct dyn_ftrace *rec, unsigned long addr)
341 unsigned char *old, *new;
342 unsigned long ip = rec->ip;
345 * If the calling address is more that 24 bits away,
346 * then we had to use a trampoline to make the call.
347 * Otherwise just update the call site.
349 if (test_24bit_addr(ip, addr)) {
351 old = ftrace_call_replace(ip, addr);
352 new = ftrace_nop_replace();
353 return ftrace_modify_code(ip, old, new);
357 * Out of range jumps are called from modules.
358 * We should either already have a pointer to the module
359 * or it has been passed in.
361 if (!rec->arch.mod) {
363 printk(KERN_ERR "No module loaded addr=%lx\n",
369 if (mod != rec->arch.mod) {
371 "Record mod %p not equal to passed in mod %p\n",
375 /* nothing to do if mod == rec->arch.mod */
379 return __ftrace_make_nop(mod, rec, addr);
385 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
387 unsigned char replaced[MCOUNT_INSN_SIZE * 2];
388 unsigned int *op = (unsigned *)&replaced;
389 unsigned long ip = rec->ip;
390 unsigned long offset;
392 /* read where this goes */
393 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE * 2))
397 * It should be pointing to two nops or
400 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
401 ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
402 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
406 /* If we never set up a trampoline to ftrace_caller, then bail */
407 if (!rec->arch.mod->arch.tramp) {
408 printk(KERN_ERR "No ftrace trampoline\n");
412 /* now calculate a jump to the ftrace caller trampoline */
413 offset = rec->arch.mod->arch.tramp - ip;
415 if (test_offset(offset)) {
416 printk(KERN_ERR "REL24 %li out of range!\n",
421 /* Set to "bl addr" */
422 op[0] = branch_offset(offset);
426 DEBUGP("write to %lx\n", rec->ip);
428 if (probe_kernel_write((void *)ip, replaced, MCOUNT_INSN_SIZE * 2))
435 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
437 unsigned char replaced[MCOUNT_INSN_SIZE];
438 unsigned int *op = (unsigned *)&replaced;
439 unsigned long ip = rec->ip;
440 unsigned long offset;
442 /* read where this goes */
443 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
446 /* It should be pointing to a nop */
447 if (op[0] != PPC_NOP_INSTR) {
448 printk(KERN_ERR "Expected NOP but have %x\n", op[0]);
452 /* If we never set up a trampoline to ftrace_caller, then bail */
453 if (!rec->arch.mod->arch.tramp) {
454 printk(KERN_ERR "No ftrace trampoline\n");
458 /* now calculate a jump to the ftrace caller trampoline */
459 offset = rec->arch.mod->arch.tramp - ip;
461 if (test_offset(offset)) {
462 printk(KERN_ERR "REL24 %li out of range!\n",
467 /* Set to "bl addr" */
468 op[0] = branch_offset(offset);
470 DEBUGP("write to %lx\n", rec->ip);
472 if (probe_kernel_write((void *)ip, replaced, MCOUNT_INSN_SIZE))
477 #endif /* CONFIG_PPC64 */
479 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
481 unsigned char *old, *new;
482 unsigned long ip = rec->ip;
485 * If the calling address is more that 24 bits away,
486 * then we had to use a trampoline to make the call.
487 * Otherwise just update the call site.
489 if (test_24bit_addr(ip, addr)) {
491 old = ftrace_nop_replace();
492 new = ftrace_call_replace(ip, addr);
493 return ftrace_modify_code(ip, old, new);
497 * Out of range jumps are called from modules.
498 * Being that we are converting from nop, it had better
499 * already have a module defined.
501 if (!rec->arch.mod) {
502 printk(KERN_ERR "No module loaded\n");
506 return __ftrace_make_call(rec, addr);
509 int ftrace_update_ftrace_func(ftrace_func_t func)
511 unsigned long ip = (unsigned long)(&ftrace_call);
512 unsigned char old[MCOUNT_INSN_SIZE], *new;
515 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
516 new = ftrace_call_replace(ip, (unsigned long)func);
517 ret = ftrace_modify_code(ip, old, new);
522 int __init ftrace_dyn_arch_init(void *data)
524 /* caller expects data to be zero */
525 unsigned long *p = data;