blob: 5355244c99ff934abde18ce75336fe9a37689bcb [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{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800117
Steven Rostedt0029ff82008-11-25 14:06:19 -0800118 /* use the create_branch to verify that this offset can be branched */
119 return create_branch((unsigned int *)ip, addr, 0);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800120}
121
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800122static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800123{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800124 return (op & 0xfc000003) == 0x48000001;
125}
126
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800127static unsigned long find_bl_target(unsigned long ip, unsigned int op)
128{
129 static int offset;
130
131 offset = (op & 0x03fffffc);
132 /* make it signed */
133 if (offset & 0x02000000)
134 offset |= 0xfe000000;
135
136 return ip + (long)offset;
137}
138
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800139#ifdef CONFIG_PPC64
140static int
141__ftrace_make_nop(struct module *mod,
142 struct dyn_ftrace *rec, unsigned long addr)
143{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800144 unsigned int op;
145 unsigned int jmp[5];
146 unsigned long ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800147 unsigned long ip = rec->ip;
148 unsigned long tramp;
149 int offset;
150
151 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800152 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800153 return -EFAULT;
154
155 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800156 if (!is_bl_op(op)) {
157 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800158 return -EINVAL;
159 }
160
161 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800162 tramp = find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800163
164 /*
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800165 * On PPC64 the trampoline looks like:
166 * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
167 * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
168 * Where the bytes 2,3,6 and 7 make up the 32bit offset
169 * to the TOC that holds the pointer.
170 * to jump to.
171 * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
172 * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
173 * The actually address is 32 bytes from the offset
174 * into the TOC.
175 * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800176 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800177
178 DEBUGP("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
179
180 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800181 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800182 printk(KERN_ERR "Failed to read %lx\n", tramp);
183 return -EFAULT;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800184 }
185
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800186 DEBUGP(" %08x %08x", jmp[0], jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800187
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800188 /* verify that this is what we expect it to be */
189 if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
190 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
191 (jmp[2] != 0xf8410028) ||
192 (jmp[3] != 0xe96c0020) ||
193 (jmp[4] != 0xe84c0028)) {
194 printk(KERN_ERR "Not a trampoline\n");
195 return -EINVAL;
196 }
197
198 offset = (unsigned)((unsigned short)jmp[0]) << 16 |
199 (unsigned)((unsigned short)jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800200
201 DEBUGP(" %x ", offset);
202
203 /* get the address this jumps too */
204 tramp = mod->arch.toc + offset + 32;
205 DEBUGP("toc: %lx", tramp);
206
207 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
208 printk(KERN_ERR "Failed to read %lx\n", tramp);
209 return -EFAULT;
210 }
211
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800212 DEBUGP(" %08x %08x\n", jmp[0], jmp[1]);
213
214 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800215
216 /* This should match what was called */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800217 if (ptr != GET_ADDR(addr)) {
218 printk(KERN_ERR "addr does not match %lx\n", ptr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800219 return -EINVAL;
220 }
221
222 /*
223 * We want to nop the line, but the next line is
224 * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
225 * This needs to be turned to a nop too.
226 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800227 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800228 return -EFAULT;
229
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800230 if (op != 0xe8410028) {
231 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800232 return -EINVAL;
233 }
234
235 /*
236 * Milton Miller pointed out that we can not blindly do nops.
237 * If a task was preempted when calling a trace function,
238 * the nops will remove the way to restore the TOC in r2
239 * and the r2 TOC will get corrupted.
240 */
241
242 /*
243 * Replace:
244 * bl <tramp> <==== will be replaced with "b 1f"
245 * ld r2,40(r1)
246 * 1:
247 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800248 op = 0x48000008; /* b +8 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800249
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800250 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800251 return -EPERM;
252
Steven Rostedtec682ce2008-11-25 10:22:48 -0800253
254 flush_icache_range(ip, ip + 8);
255
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800256 return 0;
257}
258
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800259#else /* !PPC64 */
260static int
261__ftrace_make_nop(struct module *mod,
262 struct dyn_ftrace *rec, unsigned long addr)
263{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800264 unsigned int op;
265 unsigned int jmp[4];
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500266 unsigned long ip = rec->ip;
267 unsigned long tramp;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500268
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800269 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500270 return -EFAULT;
271
272 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800273 if (!is_bl_op(op)) {
274 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500275 return -EINVAL;
276 }
277
278 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800279 tramp = find_bl_target(ip, op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500280
281 /*
282 * On PPC32 the trampoline looks like:
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800283 * 0x3d, 0x60, 0x00, 0x00 lis r11,sym@ha
284 * 0x39, 0x6b, 0x00, 0x00 addi r11,r11,sym@l
285 * 0x7d, 0x69, 0x03, 0xa6 mtctr r11
286 * 0x4e, 0x80, 0x04, 0x20 bctr
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500287 */
288
289 DEBUGP("ip:%lx jumps to %lx", ip, tramp);
290
291 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800292 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500293 printk(KERN_ERR "Failed to read %lx\n", tramp);
294 return -EFAULT;
295 }
296
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800297 DEBUGP(" %08x %08x ", jmp[0], jmp[1]);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500298
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800299 /* verify that this is what we expect it to be */
300 if (((jmp[0] & 0xffff0000) != 0x3d600000) ||
301 ((jmp[1] & 0xffff0000) != 0x396b0000) ||
302 (jmp[2] != 0x7d6903a6) ||
303 (jmp[3] != 0x4e800420)) {
304 printk(KERN_ERR "Not a trampoline\n");
305 return -EINVAL;
306 }
307
308 tramp = (jmp[1] & 0xffff) |
309 ((jmp[0] & 0xffff) << 16);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500310 if (tramp & 0x8000)
311 tramp -= 0x10000;
312
313 DEBUGP(" %x ", tramp);
314
315 if (tramp != addr) {
316 printk(KERN_ERR
317 "Trampoline location %08lx does not match addr\n",
318 tramp);
319 return -EINVAL;
320 }
321
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800322 op = PPC_NOP_INSTR;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500323
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800324 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500325 return -EPERM;
326
Steven Rostedtec682ce2008-11-25 10:22:48 -0800327 flush_icache_range(ip, ip + 8);
328
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800329 return 0;
330}
331#endif /* PPC64 */
332
333int ftrace_make_nop(struct module *mod,
334 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800335{
336 unsigned char *old, *new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800337 unsigned long ip = rec->ip;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800338
339 /*
340 * If the calling address is more that 24 bits away,
341 * then we had to use a trampoline to make the call.
342 * Otherwise just update the call site.
343 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800344 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800345 /* within range */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800346 old = ftrace_call_replace(ip, addr);
347 new = ftrace_nop_replace();
348 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800349 }
350
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800351 /*
352 * Out of range jumps are called from modules.
353 * We should either already have a pointer to the module
354 * or it has been passed in.
355 */
356 if (!rec->arch.mod) {
357 if (!mod) {
358 printk(KERN_ERR "No module loaded addr=%lx\n",
359 addr);
360 return -EFAULT;
361 }
362 rec->arch.mod = mod;
363 } else if (mod) {
364 if (mod != rec->arch.mod) {
365 printk(KERN_ERR
366 "Record mod %p not equal to passed in mod %p\n",
367 rec->arch.mod, mod);
368 return -EINVAL;
369 }
370 /* nothing to do if mod == rec->arch.mod */
371 } else
372 mod = rec->arch.mod;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800373
374 return __ftrace_make_nop(mod, rec, addr);
375
376}
377
378#ifdef CONFIG_PPC64
379static int
380__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
381{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800382 unsigned int op[2];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800383 unsigned long ip = rec->ip;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800384
385 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800386 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800387 return -EFAULT;
388
389 /*
390 * It should be pointing to two nops or
391 * b +8; ld r2,40(r1)
392 */
393 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
394 ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
395 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
396 return -EINVAL;
397 }
398
399 /* If we never set up a trampoline to ftrace_caller, then bail */
400 if (!rec->arch.mod->arch.tramp) {
401 printk(KERN_ERR "No ftrace trampoline\n");
402 return -EINVAL;
403 }
404
Steven Rostedt0029ff82008-11-25 14:06:19 -0800405 /* create the branch to the trampoline */
406 op[0] = create_branch((unsigned int *)ip,
407 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
408 if (!op[0]) {
409 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800410 return -EINVAL;
411 }
412
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800413 /* ld r2,40(r1) */
414 op[1] = 0xe8410028;
415
416 DEBUGP("write to %lx\n", rec->ip);
417
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800418 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800419 return -EPERM;
420
Steven Rostedtec682ce2008-11-25 10:22:48 -0800421 flush_icache_range(ip, ip + 8);
422
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800423 return 0;
424}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800425#else
426static int
427__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
428{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800429 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500430 unsigned long ip = rec->ip;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500431
432 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800433 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500434 return -EFAULT;
435
436 /* It should be pointing to a nop */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800437 if (op != PPC_NOP_INSTR) {
438 printk(KERN_ERR "Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500439 return -EINVAL;
440 }
441
442 /* If we never set up a trampoline to ftrace_caller, then bail */
443 if (!rec->arch.mod->arch.tramp) {
444 printk(KERN_ERR "No ftrace trampoline\n");
445 return -EINVAL;
446 }
447
Steven Rostedt0029ff82008-11-25 14:06:19 -0800448 /* create the branch to the trampoline */
449 op = create_branch((unsigned int *)ip,
450 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
451 if (!op) {
452 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500453 return -EINVAL;
454 }
455
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500456 DEBUGP("write to %lx\n", rec->ip);
457
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800458 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500459 return -EPERM;
460
Steven Rostedtec682ce2008-11-25 10:22:48 -0800461 flush_icache_range(ip, ip + 8);
462
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800463 return 0;
464}
465#endif /* CONFIG_PPC64 */
466
467int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
468{
469 unsigned char *old, *new;
470 unsigned long ip = rec->ip;
471
472 /*
473 * If the calling address is more that 24 bits away,
474 * then we had to use a trampoline to make the call.
475 * Otherwise just update the call site.
476 */
477 if (test_24bit_addr(ip, addr)) {
478 /* within range */
479 old = ftrace_nop_replace();
480 new = ftrace_call_replace(ip, addr);
481 return ftrace_modify_code(ip, old, new);
482 }
483
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800484 /*
485 * Out of range jumps are called from modules.
486 * Being that we are converting from nop, it had better
487 * already have a module defined.
488 */
489 if (!rec->arch.mod) {
490 printk(KERN_ERR "No module loaded\n");
491 return -EINVAL;
492 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800493
494 return __ftrace_make_call(rec, addr);
495}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800496
Steven Rostedt15adc042008-10-23 09:33:08 -0400497int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400498{
499 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530500 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400501 int ret;
502
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530503 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400504 new = ftrace_call_replace(ip, (unsigned long)func);
505 ret = ftrace_modify_code(ip, old, new);
506
507 return ret;
508}
509
Steven Rostedt4e491d12008-05-14 23:49:44 -0400510int __init ftrace_dyn_arch_init(void *data)
511{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800512 /* caller expects data to be zero */
513 unsigned long *p = data;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400514
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800515 *p = 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400516
517 return 0;
518}