blob: 4112175183d36e294ff4ce8dd794f140c55d87a7 [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 Rostedt17be5b32009-02-05 21:33:09 -0800116#ifdef CONFIG_MODULES
117
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800118static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800119{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800120 return (op & 0xfc000003) == 0x48000001;
121}
122
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800123static unsigned long find_bl_target(unsigned long ip, unsigned int op)
124{
125 static int offset;
126
127 offset = (op & 0x03fffffc);
128 /* make it signed */
129 if (offset & 0x02000000)
130 offset |= 0xfe000000;
131
132 return ip + (long)offset;
133}
134
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800135#ifdef CONFIG_PPC64
136static int
137__ftrace_make_nop(struct module *mod,
138 struct dyn_ftrace *rec, unsigned long addr)
139{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800140 unsigned int op;
141 unsigned int jmp[5];
142 unsigned long ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800143 unsigned long ip = rec->ip;
144 unsigned long tramp;
145 int offset;
146
147 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800148 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800149 return -EFAULT;
150
151 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800152 if (!is_bl_op(op)) {
153 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800154 return -EINVAL;
155 }
156
157 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800158 tramp = find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800159
160 /*
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800161 * On PPC64 the trampoline looks like:
162 * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
163 * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
164 * Where the bytes 2,3,6 and 7 make up the 32bit offset
165 * to the TOC that holds the pointer.
166 * to jump to.
167 * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
168 * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
169 * The actually address is 32 bytes from the offset
170 * into the TOC.
171 * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800172 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800173
Steven Rostedt44e1d062009-02-04 18:29:03 -0800174 pr_debug("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800175
176 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800177 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800178 printk(KERN_ERR "Failed to read %lx\n", tramp);
179 return -EFAULT;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800180 }
181
Steven Rostedt44e1d062009-02-04 18:29:03 -0800182 pr_debug(" %08x %08x", jmp[0], jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800183
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800184 /* verify that this is what we expect it to be */
185 if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
186 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
187 (jmp[2] != 0xf8410028) ||
188 (jmp[3] != 0xe96c0020) ||
189 (jmp[4] != 0xe84c0028)) {
190 printk(KERN_ERR "Not a trampoline\n");
191 return -EINVAL;
192 }
193
Steven Rostedtf25f9072009-02-07 20:22:40 +0000194 /* The bottom half is signed extended */
195 offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
196 (int)((short)jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800197
Steven Rostedt44e1d062009-02-04 18:29:03 -0800198 pr_debug(" %x ", offset);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800199
200 /* get the address this jumps too */
201 tramp = mod->arch.toc + offset + 32;
Steven Rostedt44e1d062009-02-04 18:29:03 -0800202 pr_debug("toc: %lx", tramp);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800203
204 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
205 printk(KERN_ERR "Failed to read %lx\n", tramp);
206 return -EFAULT;
207 }
208
Steven Rostedt44e1d062009-02-04 18:29:03 -0800209 pr_debug(" %08x %08x\n", jmp[0], jmp[1]);
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800210
211 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800212
213 /* This should match what was called */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800214 if (ptr != GET_ADDR(addr)) {
215 printk(KERN_ERR "addr does not match %lx\n", ptr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800216 return -EINVAL;
217 }
218
219 /*
220 * We want to nop the line, but the next line is
221 * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
222 * This needs to be turned to a nop too.
223 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800224 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800225 return -EFAULT;
226
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800227 if (op != 0xe8410028) {
228 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800229 return -EINVAL;
230 }
231
232 /*
233 * Milton Miller pointed out that we can not blindly do nops.
234 * If a task was preempted when calling a trace function,
235 * the nops will remove the way to restore the TOC in r2
236 * and the r2 TOC will get corrupted.
237 */
238
239 /*
240 * Replace:
241 * bl <tramp> <==== will be replaced with "b 1f"
242 * ld r2,40(r1)
243 * 1:
244 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800245 op = 0x48000008; /* b +8 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800246
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800247 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800248 return -EPERM;
249
Steven Rostedtec682ce2008-11-25 10:22:48 -0800250
251 flush_icache_range(ip, ip + 8);
252
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800253 return 0;
254}
255
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800256#else /* !PPC64 */
257static int
258__ftrace_make_nop(struct module *mod,
259 struct dyn_ftrace *rec, unsigned long addr)
260{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800261 unsigned int op;
262 unsigned int jmp[4];
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500263 unsigned long ip = rec->ip;
264 unsigned long tramp;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500265
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800266 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500267 return -EFAULT;
268
269 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800270 if (!is_bl_op(op)) {
271 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500272 return -EINVAL;
273 }
274
275 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800276 tramp = find_bl_target(ip, op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500277
278 /*
279 * On PPC32 the trampoline looks like:
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800280 * 0x3d, 0x60, 0x00, 0x00 lis r11,sym@ha
281 * 0x39, 0x6b, 0x00, 0x00 addi r11,r11,sym@l
282 * 0x7d, 0x69, 0x03, 0xa6 mtctr r11
283 * 0x4e, 0x80, 0x04, 0x20 bctr
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500284 */
285
Steven Rostedt44e1d062009-02-04 18:29:03 -0800286 pr_debug("ip:%lx jumps to %lx", ip, tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500287
288 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800289 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500290 printk(KERN_ERR "Failed to read %lx\n", tramp);
291 return -EFAULT;
292 }
293
Steven Rostedt44e1d062009-02-04 18:29:03 -0800294 pr_debug(" %08x %08x ", jmp[0], jmp[1]);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500295
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800296 /* verify that this is what we expect it to be */
297 if (((jmp[0] & 0xffff0000) != 0x3d600000) ||
298 ((jmp[1] & 0xffff0000) != 0x396b0000) ||
299 (jmp[2] != 0x7d6903a6) ||
300 (jmp[3] != 0x4e800420)) {
301 printk(KERN_ERR "Not a trampoline\n");
302 return -EINVAL;
303 }
304
305 tramp = (jmp[1] & 0xffff) |
306 ((jmp[0] & 0xffff) << 16);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500307 if (tramp & 0x8000)
308 tramp -= 0x10000;
309
Steven Rostedt44e1d062009-02-04 18:29:03 -0800310 pr_debug(" %x ", tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500311
312 if (tramp != addr) {
313 printk(KERN_ERR
314 "Trampoline location %08lx does not match addr\n",
315 tramp);
316 return -EINVAL;
317 }
318
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800319 op = PPC_NOP_INSTR;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500320
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800321 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500322 return -EPERM;
323
Steven Rostedtec682ce2008-11-25 10:22:48 -0800324 flush_icache_range(ip, ip + 8);
325
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800326 return 0;
327}
328#endif /* PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800329#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800330
331int ftrace_make_nop(struct module *mod,
332 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800333{
334 unsigned char *old, *new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800335 unsigned long ip = rec->ip;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800336
337 /*
338 * If the calling address is more that 24 bits away,
339 * then we had to use a trampoline to make the call.
340 * Otherwise just update the call site.
341 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800342 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800343 /* within range */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800344 old = ftrace_call_replace(ip, addr);
345 new = ftrace_nop_replace();
346 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800347 }
348
Steven Rostedt17be5b32009-02-05 21:33:09 -0800349#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800350 /*
351 * Out of range jumps are called from modules.
352 * We should either already have a pointer to the module
353 * or it has been passed in.
354 */
355 if (!rec->arch.mod) {
356 if (!mod) {
357 printk(KERN_ERR "No module loaded addr=%lx\n",
358 addr);
359 return -EFAULT;
360 }
361 rec->arch.mod = mod;
362 } else if (mod) {
363 if (mod != rec->arch.mod) {
364 printk(KERN_ERR
365 "Record mod %p not equal to passed in mod %p\n",
366 rec->arch.mod, mod);
367 return -EINVAL;
368 }
369 /* nothing to do if mod == rec->arch.mod */
370 } else
371 mod = rec->arch.mod;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800372
373 return __ftrace_make_nop(mod, rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800374#else
375 /* We should not get here without modules */
376 return -EINVAL;
377#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800378}
379
Steven Rostedt17be5b32009-02-05 21:33:09 -0800380#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800381#ifdef CONFIG_PPC64
382static int
383__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
384{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800385 unsigned int op[2];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800386 unsigned long ip = rec->ip;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800387
388 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800389 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800390 return -EFAULT;
391
392 /*
393 * It should be pointing to two nops or
394 * b +8; ld r2,40(r1)
395 */
396 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
397 ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
398 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
399 return -EINVAL;
400 }
401
402 /* If we never set up a trampoline to ftrace_caller, then bail */
403 if (!rec->arch.mod->arch.tramp) {
404 printk(KERN_ERR "No ftrace trampoline\n");
405 return -EINVAL;
406 }
407
Steven Rostedt0029ff82008-11-25 14:06:19 -0800408 /* create the branch to the trampoline */
409 op[0] = create_branch((unsigned int *)ip,
410 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
411 if (!op[0]) {
412 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800413 return -EINVAL;
414 }
415
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800416 /* ld r2,40(r1) */
417 op[1] = 0xe8410028;
418
Steven Rostedt44e1d062009-02-04 18:29:03 -0800419 pr_debug("write to %lx\n", rec->ip);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800420
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800421 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800422 return -EPERM;
423
Steven Rostedtec682ce2008-11-25 10:22:48 -0800424 flush_icache_range(ip, ip + 8);
425
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800426 return 0;
427}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800428#else
429static int
430__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
431{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800432 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500433 unsigned long ip = rec->ip;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500434
435 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800436 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500437 return -EFAULT;
438
439 /* It should be pointing to a nop */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800440 if (op != PPC_NOP_INSTR) {
441 printk(KERN_ERR "Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500442 return -EINVAL;
443 }
444
445 /* If we never set up a trampoline to ftrace_caller, then bail */
446 if (!rec->arch.mod->arch.tramp) {
447 printk(KERN_ERR "No ftrace trampoline\n");
448 return -EINVAL;
449 }
450
Steven Rostedt0029ff82008-11-25 14:06:19 -0800451 /* create the branch to the trampoline */
452 op = create_branch((unsigned int *)ip,
453 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
454 if (!op) {
455 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500456 return -EINVAL;
457 }
458
Steven Rostedt44e1d062009-02-04 18:29:03 -0800459 pr_debug("write to %lx\n", rec->ip);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500460
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800461 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500462 return -EPERM;
463
Steven Rostedtec682ce2008-11-25 10:22:48 -0800464 flush_icache_range(ip, ip + 8);
465
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800466 return 0;
467}
468#endif /* CONFIG_PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800469#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800470
471int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
472{
473 unsigned char *old, *new;
474 unsigned long ip = rec->ip;
475
476 /*
477 * If the calling address is more that 24 bits away,
478 * then we had to use a trampoline to make the call.
479 * Otherwise just update the call site.
480 */
481 if (test_24bit_addr(ip, addr)) {
482 /* within range */
483 old = ftrace_nop_replace();
484 new = ftrace_call_replace(ip, addr);
485 return ftrace_modify_code(ip, old, new);
486 }
487
Steven Rostedt17be5b32009-02-05 21:33:09 -0800488#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800489 /*
490 * Out of range jumps are called from modules.
491 * Being that we are converting from nop, it had better
492 * already have a module defined.
493 */
494 if (!rec->arch.mod) {
495 printk(KERN_ERR "No module loaded\n");
496 return -EINVAL;
497 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800498
499 return __ftrace_make_call(rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800500#else
501 /* We should not get here without modules */
502 return -EINVAL;
503#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800504}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800505
Steven Rostedt15adc042008-10-23 09:33:08 -0400506int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400507{
508 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530509 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400510 int ret;
511
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530512 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400513 new = ftrace_call_replace(ip, (unsigned long)func);
514 ret = ftrace_modify_code(ip, old, new);
515
516 return ret;
517}
518
Steven Rostedt4e491d12008-05-14 23:49:44 -0400519int __init ftrace_dyn_arch_init(void *data)
520{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800521 /* caller expects data to be zero */
522 unsigned long *p = data;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400523
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800524 *p = 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400525
526 return 0;
527}