blob: c9b1547f65a52da419331e1f11fe0ac62afab2e2 [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 *
Steven Rostedt6794c782009-02-09 21:10:27 -08008 * Added function graph tracer code, taken from x86 that was written
9 * by Frederic Weisbecker, and ported to PPC by Steven Rostedt.
10 *
Steven Rostedt4e491d12008-05-14 23:49:44 -040011 */
12
13#include <linux/spinlock.h>
14#include <linux/hardirq.h>
Steven Rostedte4486fe2008-11-14 16:21:20 -080015#include <linux/uaccess.h>
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080016#include <linux/module.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040017#include <linux/ftrace.h>
18#include <linux/percpu.h>
19#include <linux/init.h>
20#include <linux/list.h>
21
22#include <asm/cacheflush.h>
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080023#include <asm/code-patching.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053024#include <asm/ftrace.h>
Steven Rostedt4e491d12008-05-14 23:49:44 -040025
Steven Rostedt4e491d12008-05-14 23:49:44 -040026#ifdef CONFIG_PPC32
27# define GET_ADDR(addr) addr
28#else
29/* PowerPC64's functions are data that points to the functions */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -080030# define GET_ADDR(addr) (*(unsigned long *)addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040031#endif
32
Steven Rostedt6794c782009-02-09 21:10:27 -080033#ifdef CONFIG_DYNAMIC_FTRACE
34static unsigned int ftrace_nop = PPC_NOP_INSTR;
Abhishek Sagar395a59d2008-06-21 23:47:27 +053035
Steven Rostedt15adc042008-10-23 09:33:08 -040036static unsigned int ftrace_calc_offset(long ip, long addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040037{
Abhishek Sagar395a59d2008-06-21 23:47:27 +053038 return (int)(addr - ip);
Steven Rostedt4e491d12008-05-14 23:49:44 -040039}
40
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080041static unsigned char *ftrace_nop_replace(void)
Steven Rostedt4e491d12008-05-14 23:49:44 -040042{
43 return (char *)&ftrace_nop;
44}
45
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080046static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt4e491d12008-05-14 23:49:44 -040047{
48 static unsigned int op;
49
Steven Rostedtccbfac22008-05-22 14:31:07 -040050 /*
51 * It would be nice to just use create_function_call, but that will
52 * update the code itself. Here we need to just return the
53 * instruction that is going to be modified, without modifying the
54 * code.
55 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040056 addr = GET_ADDR(addr);
57
58 /* Set to "bl addr" */
Steven Rostedtccbfac22008-05-22 14:31:07 -040059 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
Steven Rostedt4e491d12008-05-14 23:49:44 -040060
61 /*
62 * No locking needed, this must be called via kstop_machine
63 * which in essence is like running on a uniprocessor machine.
64 */
65 return (unsigned char *)&op;
66}
67
68#ifdef CONFIG_PPC64
69# define _ASM_ALIGN " .align 3 "
70# define _ASM_PTR " .llong "
71#else
72# define _ASM_ALIGN " .align 2 "
73# define _ASM_PTR " .long "
74#endif
75
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080076static int
Steven Rostedt4e491d12008-05-14 23:49:44 -040077ftrace_modify_code(unsigned long ip, unsigned char *old_code,
78 unsigned char *new_code)
79{
Steven Rostedte4486fe2008-11-14 16:21:20 -080080 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt4e491d12008-05-14 23:49:44 -040081
Steven Rostedt4e491d12008-05-14 23:49:44 -040082 /*
83 * Note: Due to modules and __init, code can
84 * disappear and change, we need to protect against faulting
Steven Rostedte4486fe2008-11-14 16:21:20 -080085 * as well as code changing. We do this by using the
86 * probe_kernel_* functions.
Steven Rostedt4e491d12008-05-14 23:49:44 -040087 *
88 * No real locking needed, this code is run through
Steven Rostedte4486fe2008-11-14 16:21:20 -080089 * kstop_machine, or before SMP starts.
Steven Rostedt4e491d12008-05-14 23:49:44 -040090 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040091
Steven Rostedte4486fe2008-11-14 16:21:20 -080092 /* read the text we want to modify */
93 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
94 return -EFAULT;
Steven Rostedt4e491d12008-05-14 23:49:44 -040095
Steven Rostedte4486fe2008-11-14 16:21:20 -080096 /* Make sure it is what we expect it to be */
97 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
98 return -EINVAL;
Steven Rostedt4e491d12008-05-14 23:49:44 -040099
Steven Rostedte4486fe2008-11-14 16:21:20 -0800100 /* replace the text with the new text */
101 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
102 return -EPERM;
103
104 flush_icache_range(ip, ip + 8);
105
106 return 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400107}
108
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800109/*
110 * Helper functions that are the same for both PPC64 and PPC32.
111 */
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800112static int test_24bit_addr(unsigned long ip, unsigned long addr)
113{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800114
Steven Rostedt0029ff82008-11-25 14:06:19 -0800115 /* use the create_branch to verify that this offset can be branched */
116 return create_branch((unsigned int *)ip, addr, 0);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800117}
118
Steven Rostedt17be5b32009-02-05 21:33:09 -0800119#ifdef CONFIG_MODULES
120
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800121static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800122{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800123 return (op & 0xfc000003) == 0x48000001;
124}
125
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800126static unsigned long find_bl_target(unsigned long ip, unsigned int op)
127{
128 static int offset;
129
130 offset = (op & 0x03fffffc);
131 /* make it signed */
132 if (offset & 0x02000000)
133 offset |= 0xfe000000;
134
135 return ip + (long)offset;
136}
137
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800138#ifdef CONFIG_PPC64
139static int
140__ftrace_make_nop(struct module *mod,
141 struct dyn_ftrace *rec, unsigned long addr)
142{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800143 unsigned int op;
144 unsigned int jmp[5];
145 unsigned long ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800146 unsigned long ip = rec->ip;
147 unsigned long tramp;
148 int offset;
149
150 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800151 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800152 return -EFAULT;
153
154 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800155 if (!is_bl_op(op)) {
156 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800157 return -EINVAL;
158 }
159
160 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800161 tramp = find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800162
163 /*
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800164 * On PPC64 the trampoline looks like:
165 * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
166 * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
167 * Where the bytes 2,3,6 and 7 make up the 32bit offset
168 * to the TOC that holds the pointer.
169 * to jump to.
170 * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
171 * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
172 * The actually address is 32 bytes from the offset
173 * into the TOC.
174 * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800175 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800176
Steven Rostedt44e1d062009-02-04 18:29:03 -0800177 pr_debug("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800178
179 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800180 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800181 printk(KERN_ERR "Failed to read %lx\n", tramp);
182 return -EFAULT;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800183 }
184
Steven Rostedt44e1d062009-02-04 18:29:03 -0800185 pr_debug(" %08x %08x", jmp[0], jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800186
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800187 /* verify that this is what we expect it to be */
188 if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
189 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
190 (jmp[2] != 0xf8410028) ||
191 (jmp[3] != 0xe96c0020) ||
192 (jmp[4] != 0xe84c0028)) {
193 printk(KERN_ERR "Not a trampoline\n");
194 return -EINVAL;
195 }
196
Steven Rostedtf25f9072009-02-07 20:22:40 +0000197 /* The bottom half is signed extended */
198 offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
199 (int)((short)jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800200
Steven Rostedt44e1d062009-02-04 18:29:03 -0800201 pr_debug(" %x ", offset);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800202
203 /* get the address this jumps too */
204 tramp = mod->arch.toc + offset + 32;
Steven Rostedt44e1d062009-02-04 18:29:03 -0800205 pr_debug("toc: %lx", tramp);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800206
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 Rostedt44e1d062009-02-04 18:29:03 -0800212 pr_debug(" %08x %08x\n", jmp[0], jmp[1]);
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800213
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
Steven Rostedt44e1d062009-02-04 18:29:03 -0800289 pr_debug("ip:%lx jumps to %lx", ip, tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500290
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 Rostedt44e1d062009-02-04 18:29:03 -0800297 pr_debug(" %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
Steven Rostedt44e1d062009-02-04 18:29:03 -0800313 pr_debug(" %x ", tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500314
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 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800332#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800333
334int ftrace_make_nop(struct module *mod,
335 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800336{
337 unsigned char *old, *new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800338 unsigned long ip = rec->ip;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800339
340 /*
341 * If the calling address is more that 24 bits away,
342 * then we had to use a trampoline to make the call.
343 * Otherwise just update the call site.
344 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800345 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800346 /* within range */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800347 old = ftrace_call_replace(ip, addr);
348 new = ftrace_nop_replace();
349 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800350 }
351
Steven Rostedt17be5b32009-02-05 21:33:09 -0800352#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800353 /*
354 * Out of range jumps are called from modules.
355 * We should either already have a pointer to the module
356 * or it has been passed in.
357 */
358 if (!rec->arch.mod) {
359 if (!mod) {
360 printk(KERN_ERR "No module loaded addr=%lx\n",
361 addr);
362 return -EFAULT;
363 }
364 rec->arch.mod = mod;
365 } else if (mod) {
366 if (mod != rec->arch.mod) {
367 printk(KERN_ERR
368 "Record mod %p not equal to passed in mod %p\n",
369 rec->arch.mod, mod);
370 return -EINVAL;
371 }
372 /* nothing to do if mod == rec->arch.mod */
373 } else
374 mod = rec->arch.mod;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800375
376 return __ftrace_make_nop(mod, rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800377#else
378 /* We should not get here without modules */
379 return -EINVAL;
380#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800381}
382
Steven Rostedt17be5b32009-02-05 21:33:09 -0800383#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800384#ifdef CONFIG_PPC64
385static int
386__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
387{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800388 unsigned int op[2];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800389 unsigned long ip = rec->ip;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800390
391 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800392 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800393 return -EFAULT;
394
395 /*
396 * It should be pointing to two nops or
397 * b +8; ld r2,40(r1)
398 */
399 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
400 ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
401 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
402 return -EINVAL;
403 }
404
405 /* If we never set up a trampoline to ftrace_caller, then bail */
406 if (!rec->arch.mod->arch.tramp) {
407 printk(KERN_ERR "No ftrace trampoline\n");
408 return -EINVAL;
409 }
410
Steven Rostedt0029ff82008-11-25 14:06:19 -0800411 /* create the branch to the trampoline */
412 op[0] = create_branch((unsigned int *)ip,
413 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
414 if (!op[0]) {
415 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800416 return -EINVAL;
417 }
418
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800419 /* ld r2,40(r1) */
420 op[1] = 0xe8410028;
421
Steven Rostedt44e1d062009-02-04 18:29:03 -0800422 pr_debug("write to %lx\n", rec->ip);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800423
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800424 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800425 return -EPERM;
426
Steven Rostedtec682ce2008-11-25 10:22:48 -0800427 flush_icache_range(ip, ip + 8);
428
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800429 return 0;
430}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800431#else
432static int
433__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
434{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800435 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500436 unsigned long ip = rec->ip;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500437
438 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800439 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500440 return -EFAULT;
441
442 /* It should be pointing to a nop */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800443 if (op != PPC_NOP_INSTR) {
444 printk(KERN_ERR "Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500445 return -EINVAL;
446 }
447
448 /* If we never set up a trampoline to ftrace_caller, then bail */
449 if (!rec->arch.mod->arch.tramp) {
450 printk(KERN_ERR "No ftrace trampoline\n");
451 return -EINVAL;
452 }
453
Steven Rostedt0029ff82008-11-25 14:06:19 -0800454 /* create the branch to the trampoline */
455 op = create_branch((unsigned int *)ip,
456 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
457 if (!op) {
458 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500459 return -EINVAL;
460 }
461
Steven Rostedt44e1d062009-02-04 18:29:03 -0800462 pr_debug("write to %lx\n", rec->ip);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500463
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800464 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500465 return -EPERM;
466
Steven Rostedtec682ce2008-11-25 10:22:48 -0800467 flush_icache_range(ip, ip + 8);
468
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800469 return 0;
470}
471#endif /* CONFIG_PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800472#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800473
474int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
475{
476 unsigned char *old, *new;
477 unsigned long ip = rec->ip;
478
479 /*
480 * If the calling address is more that 24 bits away,
481 * then we had to use a trampoline to make the call.
482 * Otherwise just update the call site.
483 */
484 if (test_24bit_addr(ip, addr)) {
485 /* within range */
486 old = ftrace_nop_replace();
487 new = ftrace_call_replace(ip, addr);
488 return ftrace_modify_code(ip, old, new);
489 }
490
Steven Rostedt17be5b32009-02-05 21:33:09 -0800491#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800492 /*
493 * Out of range jumps are called from modules.
494 * Being that we are converting from nop, it had better
495 * already have a module defined.
496 */
497 if (!rec->arch.mod) {
498 printk(KERN_ERR "No module loaded\n");
499 return -EINVAL;
500 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800501
502 return __ftrace_make_call(rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800503#else
504 /* We should not get here without modules */
505 return -EINVAL;
506#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800507}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800508
Steven Rostedt15adc042008-10-23 09:33:08 -0400509int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400510{
511 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530512 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400513 int ret;
514
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530515 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400516 new = ftrace_call_replace(ip, (unsigned long)func);
517 ret = ftrace_modify_code(ip, old, new);
518
519 return ret;
520}
521
Steven Rostedt4e491d12008-05-14 23:49:44 -0400522int __init ftrace_dyn_arch_init(void *data)
523{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800524 /* caller expects data to be zero */
525 unsigned long *p = data;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400526
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800527 *p = 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400528
529 return 0;
530}
Steven Rostedt6794c782009-02-09 21:10:27 -0800531#endif /* CONFIG_DYNAMIC_FTRACE */
532
533#ifdef CONFIG_FUNCTION_GRAPH_TRACER
534
535/*
536 * Hook the return address and push it in the stack of return addrs
537 * in current thread info.
538 */
539void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
540{
541 unsigned long old;
542 unsigned long long calltime;
543 int faulted;
544 struct ftrace_graph_ent trace;
545 unsigned long return_hooker = (unsigned long)
546 &return_to_handler;
547
548 if (unlikely(atomic_read(&current->tracing_graph_pause)))
549 return;
550
551 return_hooker = GET_ADDR(return_hooker);
552
553 /*
554 * Protect against fault, even if it shouldn't
555 * happen. This tool is too much intrusive to
556 * ignore such a protection.
557 */
558 asm volatile(
559 "1: " PPC_LL "%[old], 0(%[parent])\n"
560 "2: " PPC_STL "%[return_hooker], 0(%[parent])\n"
561 " li %[faulted], 0\n"
562 "3:"
563
564 ".section .fixup, \"ax\"\n"
565 "4: li %[faulted], 1\n"
566 " b 3b\n"
567 ".previous\n"
568
569 ".section __ex_table,\"a\"\n"
570 PPC_LONG_ALIGN "\n"
571 PPC_LONG "1b,4b\n"
572 PPC_LONG "2b,4b\n"
573 ".previous"
574
575 : [old] "=r" (old), [faulted] "=r" (faulted)
576 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
577 : "memory"
578 );
579
580 if (unlikely(faulted)) {
581 ftrace_graph_stop();
582 WARN_ON(1);
583 return;
584 }
585
586 calltime = cpu_clock(raw_smp_processor_id());
587
588 if (ftrace_push_return_trace(old, calltime,
589 self_addr, &trace.depth) == -EBUSY) {
590 *parent = old;
591 return;
592 }
593
594 trace.func = self_addr;
595
596 /* Only trace if the calling function expects to */
597 if (!ftrace_graph_entry(&trace)) {
598 current->curr_ret_stack--;
599 *parent = old;
600 }
601}
602#endif /* CONFIG_FUNCTION_GRAPH_TRACER */