blob: ea454a004406311abf0cb6de99e27133ac5b4207 [file] [log] [blame]
Steven Rostedt4e491d12008-05-14 23:49:44 -04001/*
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>
Steven Rostedte4486fe2008-11-14 16:21:20 -080012#include <linux/uaccess.h>
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080013#include <linux/module.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040014#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>
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080020#include <asm/code-patching.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053021#include <asm/ftrace.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040022
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080023#if 0
24#define DEBUGP printk
25#else
26#define DEBUGP(fmt , ...) do { } while (0)
27#endif
Steven Rostedt4e491d12008-05-14 23:49:44 -040028
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080029static unsigned int ftrace_nop = PPC_NOP_INSTR;
Steven Rostedt4e491d12008-05-14 23:49:44 -040030
31#ifdef CONFIG_PPC32
32# define GET_ADDR(addr) addr
33#else
34/* PowerPC64's functions are data that points to the functions */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080035# define GET_ADDR(addr) (*(unsigned long *)addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040036#endif
37
Abhishek Sagar395a59d2008-06-21 23:47:27 +053038
Steven Rostedt15adc042008-10-23 09:33:08 -040039static unsigned int ftrace_calc_offset(long ip, long addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040040{
Abhishek Sagar395a59d2008-06-21 23:47:27 +053041 return (int)(addr - ip);
Steven Rostedt4e491d12008-05-14 23:49:44 -040042}
43
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080044static unsigned char *ftrace_nop_replace(void)
Steven Rostedt4e491d12008-05-14 23:49:44 -040045{
46 return (char *)&ftrace_nop;
47}
48
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080049static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040050{
51 static unsigned int op;
52
Steven Rostedtccbfac22008-05-22 14:31:07 -040053 /*
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
57 * code.
58 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040059 addr = GET_ADDR(addr);
60
61 /* Set to "bl addr" */
Steven Rostedtccbfac22008-05-22 14:31:07 -040062 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
Steven Rostedt4e491d12008-05-14 23:49:44 -040063
64 /*
65 * No locking needed, this must be called via kstop_machine
66 * which in essence is like running on a uniprocessor machine.
67 */
68 return (unsigned char *)&op;
69}
70
71#ifdef CONFIG_PPC64
72# define _ASM_ALIGN " .align 3 "
73# define _ASM_PTR " .llong "
74#else
75# define _ASM_ALIGN " .align 2 "
76# define _ASM_PTR " .long "
77#endif
78
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080079static int
Steven Rostedt4e491d12008-05-14 23:49:44 -040080ftrace_modify_code(unsigned long ip, unsigned char *old_code,
81 unsigned char *new_code)
82{
Steven Rostedte4486fe2008-11-14 16:21:20 -080083 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt4e491d12008-05-14 23:49:44 -040084
Steven Rostedt4e491d12008-05-14 23:49:44 -040085 /*
86 * Note: Due to modules and __init, code can
87 * disappear and change, we need to protect against faulting
Steven Rostedte4486fe2008-11-14 16:21:20 -080088 * as well as code changing. We do this by using the
89 * probe_kernel_* functions.
Steven Rostedt4e491d12008-05-14 23:49:44 -040090 *
91 * No real locking needed, this code is run through
Steven Rostedte4486fe2008-11-14 16:21:20 -080092 * kstop_machine, or before SMP starts.
Steven Rostedt4e491d12008-05-14 23:49:44 -040093 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040094
Steven Rostedte4486fe2008-11-14 16:21:20 -080095 /* read the text we want to modify */
96 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
97 return -EFAULT;
Steven Rostedt4e491d12008-05-14 23:49:44 -040098
Steven Rostedte4486fe2008-11-14 16:21:20 -080099 /* Make sure it is what we expect it to be */
100 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
101 return -EINVAL;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400102
Steven Rostedte4486fe2008-11-14 16:21:20 -0800103 /* replace the text with the new text */
104 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
105 return -EPERM;
106
107 flush_icache_range(ip, ip + 8);
108
109 return 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400110}
111
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800112/*
113 * Helper functions that are the same for both PPC64 and PPC32.
114 */
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800115static int test_24bit_addr(unsigned long ip, unsigned long addr)
116{
117 long diff;
118
119 /*
120 * Can we get to addr from ip in 24 bits?
121 * (26 really, since we mulitply by 4 for 4 byte alignment)
122 */
123 diff = addr - ip;
124
125 /*
126 * Return true if diff is less than 1 << 25
127 * and greater than -1 << 26.
128 */
129 return (diff < (1 << 25)) && (diff > (-1 << 26));
130}
131
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800132static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800133{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800134 return (op & 0xfc000003) == 0x48000001;
135}
136
137static int test_offset(unsigned long offset)
138{
139 return (offset + 0x2000000 > 0x3ffffff) || ((offset & 3) != 0);
140}
141
142static unsigned long find_bl_target(unsigned long ip, unsigned int op)
143{
144 static int offset;
145
146 offset = (op & 0x03fffffc);
147 /* make it signed */
148 if (offset & 0x02000000)
149 offset |= 0xfe000000;
150
151 return ip + (long)offset;
152}
153
154static unsigned int branch_offset(unsigned long offset)
155{
156 /* return "bl ip+offset" */
157 return 0x48000001 | (offset & 0x03fffffc);
158}
159
160#ifdef CONFIG_PPC64
161static int
162__ftrace_make_nop(struct module *mod,
163 struct dyn_ftrace *rec, unsigned long addr)
164{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800165 unsigned int op;
166 unsigned int jmp[5];
167 unsigned long ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800168 unsigned long ip = rec->ip;
169 unsigned long tramp;
170 int offset;
171
172 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800173 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800174 return -EFAULT;
175
176 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800177 if (!is_bl_op(op)) {
178 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800179 return -EINVAL;
180 }
181
182 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800183 tramp = find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800184
185 /*
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800186 * On PPC64 the trampoline looks like:
187 * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
188 * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
189 * Where the bytes 2,3,6 and 7 make up the 32bit offset
190 * to the TOC that holds the pointer.
191 * to jump to.
192 * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
193 * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
194 * The actually address is 32 bytes from the offset
195 * into the TOC.
196 * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800197 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800198
199 DEBUGP("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
200
201 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800202 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800203 printk(KERN_ERR "Failed to read %lx\n", tramp);
204 return -EFAULT;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800205 }
206
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800207 DEBUGP(" %08x %08x", jmp[0], jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800208
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800209 /* verify that this is what we expect it to be */
210 if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
211 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
212 (jmp[2] != 0xf8410028) ||
213 (jmp[3] != 0xe96c0020) ||
214 (jmp[4] != 0xe84c0028)) {
215 printk(KERN_ERR "Not a trampoline\n");
216 return -EINVAL;
217 }
218
219 offset = (unsigned)((unsigned short)jmp[0]) << 16 |
220 (unsigned)((unsigned short)jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800221
222 DEBUGP(" %x ", offset);
223
224 /* get the address this jumps too */
225 tramp = mod->arch.toc + offset + 32;
226 DEBUGP("toc: %lx", tramp);
227
228 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
229 printk(KERN_ERR "Failed to read %lx\n", tramp);
230 return -EFAULT;
231 }
232
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800233 DEBUGP(" %08x %08x\n", jmp[0], jmp[1]);
234
235 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800236
237 /* This should match what was called */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800238 if (ptr != GET_ADDR(addr)) {
239 printk(KERN_ERR "addr does not match %lx\n", ptr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800240 return -EINVAL;
241 }
242
243 /*
244 * We want to nop the line, but the next line is
245 * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
246 * This needs to be turned to a nop too.
247 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800248 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800249 return -EFAULT;
250
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800251 if (op != 0xe8410028) {
252 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800253 return -EINVAL;
254 }
255
256 /*
257 * Milton Miller pointed out that we can not blindly do nops.
258 * If a task was preempted when calling a trace function,
259 * the nops will remove the way to restore the TOC in r2
260 * and the r2 TOC will get corrupted.
261 */
262
263 /*
264 * Replace:
265 * bl <tramp> <==== will be replaced with "b 1f"
266 * ld r2,40(r1)
267 * 1:
268 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800269 op = 0x48000008; /* b +8 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800270
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800271 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800272 return -EPERM;
273
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800274 return 0;
275}
276
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800277#else /* !PPC64 */
278static int
279__ftrace_make_nop(struct module *mod,
280 struct dyn_ftrace *rec, unsigned long addr)
281{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800282 unsigned int op;
283 unsigned int jmp[4];
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500284 unsigned long ip = rec->ip;
285 unsigned long tramp;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500286
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800287 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500288 return -EFAULT;
289
290 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800291 if (!is_bl_op(op)) {
292 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500293 return -EINVAL;
294 }
295
296 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800297 tramp = find_bl_target(ip, op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500298
299 /*
300 * On PPC32 the trampoline looks like:
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800301 * 0x3d, 0x60, 0x00, 0x00 lis r11,sym@ha
302 * 0x39, 0x6b, 0x00, 0x00 addi r11,r11,sym@l
303 * 0x7d, 0x69, 0x03, 0xa6 mtctr r11
304 * 0x4e, 0x80, 0x04, 0x20 bctr
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500305 */
306
307 DEBUGP("ip:%lx jumps to %lx", ip, tramp);
308
309 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800310 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500311 printk(KERN_ERR "Failed to read %lx\n", tramp);
312 return -EFAULT;
313 }
314
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800315 DEBUGP(" %08x %08x ", jmp[0], jmp[1]);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500316
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800317 /* verify that this is what we expect it to be */
318 if (((jmp[0] & 0xffff0000) != 0x3d600000) ||
319 ((jmp[1] & 0xffff0000) != 0x396b0000) ||
320 (jmp[2] != 0x7d6903a6) ||
321 (jmp[3] != 0x4e800420)) {
322 printk(KERN_ERR "Not a trampoline\n");
323 return -EINVAL;
324 }
325
326 tramp = (jmp[1] & 0xffff) |
327 ((jmp[0] & 0xffff) << 16);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500328 if (tramp & 0x8000)
329 tramp -= 0x10000;
330
331 DEBUGP(" %x ", tramp);
332
333 if (tramp != addr) {
334 printk(KERN_ERR
335 "Trampoline location %08lx does not match addr\n",
336 tramp);
337 return -EINVAL;
338 }
339
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800340 op = PPC_NOP_INSTR;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500341
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800342 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500343 return -EPERM;
344
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800345 return 0;
346}
347#endif /* PPC64 */
348
349int ftrace_make_nop(struct module *mod,
350 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800351{
352 unsigned char *old, *new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800353 unsigned long ip = rec->ip;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800354
355 /*
356 * If the calling address is more that 24 bits away,
357 * then we had to use a trampoline to make the call.
358 * Otherwise just update the call site.
359 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800360 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800361 /* within range */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800362 old = ftrace_call_replace(ip, addr);
363 new = ftrace_nop_replace();
364 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800365 }
366
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800367 /*
368 * Out of range jumps are called from modules.
369 * We should either already have a pointer to the module
370 * or it has been passed in.
371 */
372 if (!rec->arch.mod) {
373 if (!mod) {
374 printk(KERN_ERR "No module loaded addr=%lx\n",
375 addr);
376 return -EFAULT;
377 }
378 rec->arch.mod = mod;
379 } else if (mod) {
380 if (mod != rec->arch.mod) {
381 printk(KERN_ERR
382 "Record mod %p not equal to passed in mod %p\n",
383 rec->arch.mod, mod);
384 return -EINVAL;
385 }
386 /* nothing to do if mod == rec->arch.mod */
387 } else
388 mod = rec->arch.mod;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800389
390 return __ftrace_make_nop(mod, rec, addr);
391
392}
393
394#ifdef CONFIG_PPC64
395static int
396__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
397{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800398 unsigned int op[2];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800399 unsigned long ip = rec->ip;
400 unsigned long offset;
401
402 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800403 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800404 return -EFAULT;
405
406 /*
407 * It should be pointing to two nops or
408 * b +8; ld r2,40(r1)
409 */
410 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
411 ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
412 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
413 return -EINVAL;
414 }
415
416 /* If we never set up a trampoline to ftrace_caller, then bail */
417 if (!rec->arch.mod->arch.tramp) {
418 printk(KERN_ERR "No ftrace trampoline\n");
419 return -EINVAL;
420 }
421
422 /* now calculate a jump to the ftrace caller trampoline */
423 offset = rec->arch.mod->arch.tramp - ip;
424
425 if (test_offset(offset)) {
426 printk(KERN_ERR "REL24 %li out of range!\n",
427 (long int)offset);
428 return -EINVAL;
429 }
430
431 /* Set to "bl addr" */
432 op[0] = branch_offset(offset);
433 /* ld r2,40(r1) */
434 op[1] = 0xe8410028;
435
436 DEBUGP("write to %lx\n", rec->ip);
437
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800438 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800439 return -EPERM;
440
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800441 return 0;
442}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800443#else
444static int
445__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
446{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800447 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500448 unsigned long ip = rec->ip;
449 unsigned long offset;
450
451 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800452 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500453 return -EFAULT;
454
455 /* It should be pointing to a nop */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800456 if (op != PPC_NOP_INSTR) {
457 printk(KERN_ERR "Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500458 return -EINVAL;
459 }
460
461 /* If we never set up a trampoline to ftrace_caller, then bail */
462 if (!rec->arch.mod->arch.tramp) {
463 printk(KERN_ERR "No ftrace trampoline\n");
464 return -EINVAL;
465 }
466
467 /* now calculate a jump to the ftrace caller trampoline */
468 offset = rec->arch.mod->arch.tramp - ip;
469
470 if (test_offset(offset)) {
471 printk(KERN_ERR "REL24 %li out of range!\n",
472 (long int)offset);
473 return -EINVAL;
474 }
475
476 /* Set to "bl addr" */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800477 op = branch_offset(offset);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500478
479 DEBUGP("write to %lx\n", rec->ip);
480
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800481 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500482 return -EPERM;
483
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800484 return 0;
485}
486#endif /* CONFIG_PPC64 */
487
488int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
489{
490 unsigned char *old, *new;
491 unsigned long ip = rec->ip;
492
493 /*
494 * If the calling address is more that 24 bits away,
495 * then we had to use a trampoline to make the call.
496 * Otherwise just update the call site.
497 */
498 if (test_24bit_addr(ip, addr)) {
499 /* within range */
500 old = ftrace_nop_replace();
501 new = ftrace_call_replace(ip, addr);
502 return ftrace_modify_code(ip, old, new);
503 }
504
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800505 /*
506 * Out of range jumps are called from modules.
507 * Being that we are converting from nop, it had better
508 * already have a module defined.
509 */
510 if (!rec->arch.mod) {
511 printk(KERN_ERR "No module loaded\n");
512 return -EINVAL;
513 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800514
515 return __ftrace_make_call(rec, addr);
516}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800517
Steven Rostedt15adc042008-10-23 09:33:08 -0400518int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400519{
520 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530521 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400522 int ret;
523
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530524 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400525 new = ftrace_call_replace(ip, (unsigned long)func);
526 ret = ftrace_modify_code(ip, old, new);
527
528 return ret;
529}
530
Steven Rostedt4e491d12008-05-14 23:49:44 -0400531int __init ftrace_dyn_arch_init(void *data)
532{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800533 /* caller expects data to be zero */
534 unsigned long *p = data;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400535
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800536 *p = 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400537
538 return 0;
539}