blob: 77a7d186db1c81e59e2bfae7a33ed401ac4df84b [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 -080023static unsigned int ftrace_nop = PPC_NOP_INSTR;
Steven Rostedt4e491d12008-05-14 23:49:44 -040024
25#ifdef CONFIG_PPC32
26# define GET_ADDR(addr) addr
27#else
28/* PowerPC64's functions are data that points to the functions */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080029# define GET_ADDR(addr) (*(unsigned long *)addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040030#endif
31
Abhishek Sagar395a59d2008-06-21 23:47:27 +053032
Steven Rostedt15adc042008-10-23 09:33:08 -040033static unsigned int ftrace_calc_offset(long ip, long addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040034{
Abhishek Sagar395a59d2008-06-21 23:47:27 +053035 return (int)(addr - ip);
Steven Rostedt4e491d12008-05-14 23:49:44 -040036}
37
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080038static unsigned char *ftrace_nop_replace(void)
Steven Rostedt4e491d12008-05-14 23:49:44 -040039{
40 return (char *)&ftrace_nop;
41}
42
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080043static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040044{
45 static unsigned int op;
46
Steven Rostedtccbfac22008-05-22 14:31:07 -040047 /*
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 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040053 addr = GET_ADDR(addr);
54
55 /* Set to "bl addr" */
Steven Rostedtccbfac22008-05-22 14:31:07 -040056 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
Steven Rostedt4e491d12008-05-14 23:49:44 -040057
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
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080073static int
Steven Rostedt4e491d12008-05-14 23:49:44 -040074ftrace_modify_code(unsigned long ip, unsigned char *old_code,
75 unsigned char *new_code)
76{
Steven Rostedte4486fe2008-11-14 16:21:20 -080077 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt4e491d12008-05-14 23:49:44 -040078
Steven Rostedt4e491d12008-05-14 23:49:44 -040079 /*
80 * Note: Due to modules and __init, code can
81 * disappear and change, we need to protect against faulting
Steven Rostedte4486fe2008-11-14 16:21:20 -080082 * as well as code changing. We do this by using the
83 * probe_kernel_* functions.
Steven Rostedt4e491d12008-05-14 23:49:44 -040084 *
85 * No real locking needed, this code is run through
Steven Rostedte4486fe2008-11-14 16:21:20 -080086 * kstop_machine, or before SMP starts.
Steven Rostedt4e491d12008-05-14 23:49:44 -040087 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040088
Steven Rostedte4486fe2008-11-14 16:21:20 -080089 /* read the text we want to modify */
90 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
91 return -EFAULT;
Steven Rostedt4e491d12008-05-14 23:49:44 -040092
Steven Rostedte4486fe2008-11-14 16:21:20 -080093 /* Make sure it is what we expect it to be */
94 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
95 return -EINVAL;
Steven Rostedt4e491d12008-05-14 23:49:44 -040096
Steven Rostedte4486fe2008-11-14 16:21:20 -080097 /* 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;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400104}
105
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800106/*
107 * Helper functions that are the same for both PPC64 and PPC32.
108 */
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800109static int test_24bit_addr(unsigned long ip, unsigned long addr)
110{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800111
Steven Rostedt0029ff82008-11-25 14:06:19 -0800112 /* use the create_branch to verify that this offset can be branched */
113 return create_branch((unsigned int *)ip, addr, 0);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800114}
115
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800116static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800117{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800118 return (op & 0xfc000003) == 0x48000001;
119}
120
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800121static 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
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800133#ifdef CONFIG_PPC64
134static int
135__ftrace_make_nop(struct module *mod,
136 struct dyn_ftrace *rec, unsigned long addr)
137{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800138 unsigned int op;
139 unsigned int jmp[5];
140 unsigned long ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800141 unsigned long ip = rec->ip;
142 unsigned long tramp;
143 int offset;
144
145 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800146 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800147 return -EFAULT;
148
149 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800150 if (!is_bl_op(op)) {
151 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800152 return -EINVAL;
153 }
154
155 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800156 tramp = find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800157
158 /*
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800159 * 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)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800170 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800171
Steven Rostedt44e1d062009-02-04 18:29:03 -0800172 pr_debug("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800173
174 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800175 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800176 printk(KERN_ERR "Failed to read %lx\n", tramp);
177 return -EFAULT;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800178 }
179
Steven Rostedt44e1d062009-02-04 18:29:03 -0800180 pr_debug(" %08x %08x", jmp[0], jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800181
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800182 /* 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
Steven Rostedtf25f9072009-02-07 20:22:40 +0000192 /* The bottom half is signed extended */
193 offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
194 (int)((short)jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800195
Steven Rostedt44e1d062009-02-04 18:29:03 -0800196 pr_debug(" %x ", offset);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800197
198 /* get the address this jumps too */
199 tramp = mod->arch.toc + offset + 32;
Steven Rostedt44e1d062009-02-04 18:29:03 -0800200 pr_debug("toc: %lx", tramp);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800201
202 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
203 printk(KERN_ERR "Failed to read %lx\n", tramp);
204 return -EFAULT;
205 }
206
Steven Rostedt44e1d062009-02-04 18:29:03 -0800207 pr_debug(" %08x %08x\n", jmp[0], jmp[1]);
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800208
209 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800210
211 /* This should match what was called */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800212 if (ptr != GET_ADDR(addr)) {
213 printk(KERN_ERR "addr does not match %lx\n", ptr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800214 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 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800222 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800223 return -EFAULT;
224
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800225 if (op != 0xe8410028) {
226 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800227 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 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800243 op = 0x48000008; /* b +8 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800244
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800245 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800246 return -EPERM;
247
Steven Rostedtec682ce2008-11-25 10:22:48 -0800248
249 flush_icache_range(ip, ip + 8);
250
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800251 return 0;
252}
253
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800254#else /* !PPC64 */
255static int
256__ftrace_make_nop(struct module *mod,
257 struct dyn_ftrace *rec, unsigned long addr)
258{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800259 unsigned int op;
260 unsigned int jmp[4];
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500261 unsigned long ip = rec->ip;
262 unsigned long tramp;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500263
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800264 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500265 return -EFAULT;
266
267 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800268 if (!is_bl_op(op)) {
269 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500270 return -EINVAL;
271 }
272
273 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800274 tramp = find_bl_target(ip, op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500275
276 /*
277 * On PPC32 the trampoline looks like:
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800278 * 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
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500282 */
283
Steven Rostedt44e1d062009-02-04 18:29:03 -0800284 pr_debug("ip:%lx jumps to %lx", ip, tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500285
286 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800287 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500288 printk(KERN_ERR "Failed to read %lx\n", tramp);
289 return -EFAULT;
290 }
291
Steven Rostedt44e1d062009-02-04 18:29:03 -0800292 pr_debug(" %08x %08x ", jmp[0], jmp[1]);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500293
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800294 /* 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);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500305 if (tramp & 0x8000)
306 tramp -= 0x10000;
307
Steven Rostedt44e1d062009-02-04 18:29:03 -0800308 pr_debug(" %x ", tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500309
310 if (tramp != addr) {
311 printk(KERN_ERR
312 "Trampoline location %08lx does not match addr\n",
313 tramp);
314 return -EINVAL;
315 }
316
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800317 op = PPC_NOP_INSTR;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500318
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800319 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500320 return -EPERM;
321
Steven Rostedtec682ce2008-11-25 10:22:48 -0800322 flush_icache_range(ip, ip + 8);
323
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800324 return 0;
325}
326#endif /* PPC64 */
327
328int ftrace_make_nop(struct module *mod,
329 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800330{
331 unsigned char *old, *new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800332 unsigned long ip = rec->ip;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800333
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 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800339 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800340 /* within range */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800341 old = ftrace_call_replace(ip, addr);
342 new = ftrace_nop_replace();
343 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800344 }
345
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800346 /*
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;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800368
369 return __ftrace_make_nop(mod, rec, addr);
370
371}
372
373#ifdef CONFIG_PPC64
374static int
375__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
376{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800377 unsigned int op[2];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800378 unsigned long ip = rec->ip;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800379
380 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800381 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800382 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
Steven Rostedt0029ff82008-11-25 14:06:19 -0800400 /* 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");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800405 return -EINVAL;
406 }
407
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800408 /* ld r2,40(r1) */
409 op[1] = 0xe8410028;
410
Steven Rostedt44e1d062009-02-04 18:29:03 -0800411 pr_debug("write to %lx\n", rec->ip);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800412
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800413 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800414 return -EPERM;
415
Steven Rostedtec682ce2008-11-25 10:22:48 -0800416 flush_icache_range(ip, ip + 8);
417
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800418 return 0;
419}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800420#else
421static int
422__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
423{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800424 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500425 unsigned long ip = rec->ip;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500426
427 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800428 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500429 return -EFAULT;
430
431 /* It should be pointing to a nop */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800432 if (op != PPC_NOP_INSTR) {
433 printk(KERN_ERR "Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500434 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
Steven Rostedt0029ff82008-11-25 14:06:19 -0800443 /* 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");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500448 return -EINVAL;
449 }
450
Steven Rostedt44e1d062009-02-04 18:29:03 -0800451 pr_debug("write to %lx\n", rec->ip);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500452
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800453 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500454 return -EPERM;
455
Steven Rostedtec682ce2008-11-25 10:22:48 -0800456 flush_icache_range(ip, ip + 8);
457
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800458 return 0;
459}
460#endif /* CONFIG_PPC64 */
461
462int 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
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800479 /*
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 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800488
489 return __ftrace_make_call(rec, addr);
490}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800491
Steven Rostedt15adc042008-10-23 09:33:08 -0400492int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400493{
494 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530495 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400496 int ret;
497
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530498 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400499 new = ftrace_call_replace(ip, (unsigned long)func);
500 ret = ftrace_modify_code(ip, old, new);
501
502 return ret;
503}
504
Steven Rostedt4e491d12008-05-14 23:49:44 -0400505int __init ftrace_dyn_arch_init(void *data)
506{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800507 /* caller expects data to be zero */
508 unsigned long *p = data;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400509
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800510 *p = 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400511
512 return 0;
513}