blob: dddd99bbbd8d8c1599de912e75dc2307268dead6 [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 Rostedt46542882009-02-10 22:19:54 -080046static unsigned char *
47ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
Steven Rostedt4e491d12008-05-14 23:49:44 -040048{
49 static unsigned int op;
50
Steven Rostedtccbfac22008-05-22 14:31:07 -040051 /*
52 * It would be nice to just use create_function_call, but that will
53 * update the code itself. Here we need to just return the
54 * instruction that is going to be modified, without modifying the
55 * code.
56 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040057 addr = GET_ADDR(addr);
58
Steven Rostedt46542882009-02-10 22:19:54 -080059 /* if (link) set op to 'bl' else 'b' */
60 op = 0x48000000 | (link ? 1 : 0);
61 op |= (ftrace_calc_offset(ip, addr) & 0x03fffffc);
Steven Rostedt4e491d12008-05-14 23:49:44 -040062
63 /*
64 * No locking needed, this must be called via kstop_machine
65 * which in essence is like running on a uniprocessor machine.
66 */
67 return (unsigned char *)&op;
68}
69
70#ifdef CONFIG_PPC64
71# define _ASM_ALIGN " .align 3 "
72# define _ASM_PTR " .llong "
73#else
74# define _ASM_ALIGN " .align 2 "
75# define _ASM_PTR " .long "
76#endif
77
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -080078static int
Steven Rostedt4e491d12008-05-14 23:49:44 -040079ftrace_modify_code(unsigned long ip, unsigned char *old_code,
80 unsigned char *new_code)
81{
Steven Rostedte4486fe2008-11-14 16:21:20 -080082 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt4e491d12008-05-14 23:49:44 -040083
Steven Rostedt4e491d12008-05-14 23:49:44 -040084 /*
85 * Note: Due to modules and __init, code can
86 * disappear and change, we need to protect against faulting
Steven Rostedte4486fe2008-11-14 16:21:20 -080087 * as well as code changing. We do this by using the
88 * probe_kernel_* functions.
Steven Rostedt4e491d12008-05-14 23:49:44 -040089 *
90 * No real locking needed, this code is run through
Steven Rostedte4486fe2008-11-14 16:21:20 -080091 * kstop_machine, or before SMP starts.
Steven Rostedt4e491d12008-05-14 23:49:44 -040092 */
Steven Rostedt4e491d12008-05-14 23:49:44 -040093
Steven Rostedte4486fe2008-11-14 16:21:20 -080094 /* read the text we want to modify */
95 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
96 return -EFAULT;
Steven Rostedt4e491d12008-05-14 23:49:44 -040097
Steven Rostedte4486fe2008-11-14 16:21:20 -080098 /* Make sure it is what we expect it to be */
99 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
100 return -EINVAL;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400101
Steven Rostedte4486fe2008-11-14 16:21:20 -0800102 /* replace the text with the new text */
103 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
104 return -EPERM;
105
106 flush_icache_range(ip, ip + 8);
107
108 return 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400109}
110
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800111/*
112 * Helper functions that are the same for both PPC64 and PPC32.
113 */
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800114static int test_24bit_addr(unsigned long ip, unsigned long addr)
115{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800116
Steven Rostedt0029ff82008-11-25 14:06:19 -0800117 /* use the create_branch to verify that this offset can be branched */
118 return create_branch((unsigned int *)ip, addr, 0);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800119}
120
Steven Rostedt17be5b32009-02-05 21:33:09 -0800121#ifdef CONFIG_MODULES
122
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800123static int is_bl_op(unsigned int op)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800124{
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800125 return (op & 0xfc000003) == 0x48000001;
126}
127
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800128static unsigned long find_bl_target(unsigned long ip, unsigned int op)
129{
130 static int offset;
131
132 offset = (op & 0x03fffffc);
133 /* make it signed */
134 if (offset & 0x02000000)
135 offset |= 0xfe000000;
136
137 return ip + (long)offset;
138}
139
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800140#ifdef CONFIG_PPC64
141static int
142__ftrace_make_nop(struct module *mod,
143 struct dyn_ftrace *rec, unsigned long addr)
144{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800145 unsigned int op;
146 unsigned int jmp[5];
147 unsigned long ptr;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800148 unsigned long ip = rec->ip;
149 unsigned long tramp;
150 int offset;
151
152 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800153 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800154 return -EFAULT;
155
156 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800157 if (!is_bl_op(op)) {
158 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800159 return -EINVAL;
160 }
161
162 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800163 tramp = find_bl_target(ip, op);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800164
165 /*
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800166 * On PPC64 the trampoline looks like:
167 * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
168 * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
169 * Where the bytes 2,3,6 and 7 make up the 32bit offset
170 * to the TOC that holds the pointer.
171 * to jump to.
172 * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
173 * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
174 * The actually address is 32 bytes from the offset
175 * into the TOC.
176 * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800177 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800178
Steven Rostedt44e1d062009-02-04 18:29:03 -0800179 pr_debug("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800180
181 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800182 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800183 printk(KERN_ERR "Failed to read %lx\n", tramp);
184 return -EFAULT;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800185 }
186
Steven Rostedt44e1d062009-02-04 18:29:03 -0800187 pr_debug(" %08x %08x", jmp[0], jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800188
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800189 /* verify that this is what we expect it to be */
190 if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
191 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
192 (jmp[2] != 0xf8410028) ||
193 (jmp[3] != 0xe96c0020) ||
194 (jmp[4] != 0xe84c0028)) {
195 printk(KERN_ERR "Not a trampoline\n");
196 return -EINVAL;
197 }
198
Steven Rostedtf25f9072009-02-07 20:22:40 +0000199 /* The bottom half is signed extended */
200 offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
201 (int)((short)jmp[1]);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800202
Steven Rostedt44e1d062009-02-04 18:29:03 -0800203 pr_debug(" %x ", offset);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800204
205 /* get the address this jumps too */
206 tramp = mod->arch.toc + offset + 32;
Steven Rostedt44e1d062009-02-04 18:29:03 -0800207 pr_debug("toc: %lx", tramp);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800208
209 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
210 printk(KERN_ERR "Failed to read %lx\n", tramp);
211 return -EFAULT;
212 }
213
Steven Rostedt44e1d062009-02-04 18:29:03 -0800214 pr_debug(" %08x %08x\n", jmp[0], jmp[1]);
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800215
216 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800217
218 /* This should match what was called */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800219 if (ptr != GET_ADDR(addr)) {
220 printk(KERN_ERR "addr does not match %lx\n", ptr);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800221 return -EINVAL;
222 }
223
224 /*
225 * We want to nop the line, but the next line is
226 * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
227 * This needs to be turned to a nop too.
228 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800229 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800230 return -EFAULT;
231
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800232 if (op != 0xe8410028) {
233 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800234 return -EINVAL;
235 }
236
237 /*
238 * Milton Miller pointed out that we can not blindly do nops.
239 * If a task was preempted when calling a trace function,
240 * the nops will remove the way to restore the TOC in r2
241 * and the r2 TOC will get corrupted.
242 */
243
244 /*
245 * Replace:
246 * bl <tramp> <==== will be replaced with "b 1f"
247 * ld r2,40(r1)
248 * 1:
249 */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800250 op = 0x48000008; /* b +8 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800251
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800252 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800253 return -EPERM;
254
Steven Rostedtec682ce2008-11-25 10:22:48 -0800255
256 flush_icache_range(ip, ip + 8);
257
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800258 return 0;
259}
260
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800261#else /* !PPC64 */
262static int
263__ftrace_make_nop(struct module *mod,
264 struct dyn_ftrace *rec, unsigned long addr)
265{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800266 unsigned int op;
267 unsigned int jmp[4];
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500268 unsigned long ip = rec->ip;
269 unsigned long tramp;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500270
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800271 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500272 return -EFAULT;
273
274 /* Make sure that that this is still a 24bit jump */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800275 if (!is_bl_op(op)) {
276 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500277 return -EINVAL;
278 }
279
280 /* lets find where the pointer goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800281 tramp = find_bl_target(ip, op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500282
283 /*
284 * On PPC32 the trampoline looks like:
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800285 * 0x3d, 0x60, 0x00, 0x00 lis r11,sym@ha
286 * 0x39, 0x6b, 0x00, 0x00 addi r11,r11,sym@l
287 * 0x7d, 0x69, 0x03, 0xa6 mtctr r11
288 * 0x4e, 0x80, 0x04, 0x20 bctr
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500289 */
290
Steven Rostedt44e1d062009-02-04 18:29:03 -0800291 pr_debug("ip:%lx jumps to %lx", ip, tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500292
293 /* Find where the trampoline jumps to */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800294 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500295 printk(KERN_ERR "Failed to read %lx\n", tramp);
296 return -EFAULT;
297 }
298
Steven Rostedt44e1d062009-02-04 18:29:03 -0800299 pr_debug(" %08x %08x ", jmp[0], jmp[1]);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500300
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800301 /* verify that this is what we expect it to be */
302 if (((jmp[0] & 0xffff0000) != 0x3d600000) ||
303 ((jmp[1] & 0xffff0000) != 0x396b0000) ||
304 (jmp[2] != 0x7d6903a6) ||
305 (jmp[3] != 0x4e800420)) {
306 printk(KERN_ERR "Not a trampoline\n");
307 return -EINVAL;
308 }
309
310 tramp = (jmp[1] & 0xffff) |
311 ((jmp[0] & 0xffff) << 16);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500312 if (tramp & 0x8000)
313 tramp -= 0x10000;
314
Steven Rostedt44e1d062009-02-04 18:29:03 -0800315 pr_debug(" %x ", tramp);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500316
317 if (tramp != addr) {
318 printk(KERN_ERR
319 "Trampoline location %08lx does not match addr\n",
320 tramp);
321 return -EINVAL;
322 }
323
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800324 op = PPC_NOP_INSTR;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500325
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800326 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500327 return -EPERM;
328
Steven Rostedtec682ce2008-11-25 10:22:48 -0800329 flush_icache_range(ip, ip + 8);
330
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800331 return 0;
332}
333#endif /* PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800334#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800335
336int ftrace_make_nop(struct module *mod,
337 struct dyn_ftrace *rec, unsigned long addr)
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800338{
339 unsigned char *old, *new;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800340 unsigned long ip = rec->ip;
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800341
342 /*
343 * If the calling address is more that 24 bits away,
344 * then we had to use a trampoline to make the call.
345 * Otherwise just update the call site.
346 */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800347 if (test_24bit_addr(ip, addr)) {
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800348 /* within range */
Steven Rostedt46542882009-02-10 22:19:54 -0800349 old = ftrace_call_replace(ip, addr, 1);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800350 new = ftrace_nop_replace();
351 return ftrace_modify_code(ip, old, new);
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800352 }
353
Steven Rostedt17be5b32009-02-05 21:33:09 -0800354#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800355 /*
356 * Out of range jumps are called from modules.
357 * We should either already have a pointer to the module
358 * or it has been passed in.
359 */
360 if (!rec->arch.mod) {
361 if (!mod) {
362 printk(KERN_ERR "No module loaded addr=%lx\n",
363 addr);
364 return -EFAULT;
365 }
366 rec->arch.mod = mod;
367 } else if (mod) {
368 if (mod != rec->arch.mod) {
369 printk(KERN_ERR
370 "Record mod %p not equal to passed in mod %p\n",
371 rec->arch.mod, mod);
372 return -EINVAL;
373 }
374 /* nothing to do if mod == rec->arch.mod */
375 } else
376 mod = rec->arch.mod;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800377
378 return __ftrace_make_nop(mod, rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800379#else
380 /* We should not get here without modules */
381 return -EINVAL;
382#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800383}
384
Steven Rostedt17be5b32009-02-05 21:33:09 -0800385#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800386#ifdef CONFIG_PPC64
387static int
388__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
389{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800390 unsigned int op[2];
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800391 unsigned long ip = rec->ip;
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800392
393 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800394 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800395 return -EFAULT;
396
397 /*
398 * It should be pointing to two nops or
399 * b +8; ld r2,40(r1)
400 */
401 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
402 ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
403 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
404 return -EINVAL;
405 }
406
407 /* If we never set up a trampoline to ftrace_caller, then bail */
408 if (!rec->arch.mod->arch.tramp) {
409 printk(KERN_ERR "No ftrace trampoline\n");
410 return -EINVAL;
411 }
412
Steven Rostedt0029ff82008-11-25 14:06:19 -0800413 /* create the branch to the trampoline */
414 op[0] = create_branch((unsigned int *)ip,
415 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
416 if (!op[0]) {
417 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800418 return -EINVAL;
419 }
420
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800421 /* ld r2,40(r1) */
422 op[1] = 0xe8410028;
423
Steven Rostedt44e1d062009-02-04 18:29:03 -0800424 pr_debug("write to %lx\n", rec->ip);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800425
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800426 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800427 return -EPERM;
428
Steven Rostedtec682ce2008-11-25 10:22:48 -0800429 flush_icache_range(ip, ip + 8);
430
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800431 return 0;
432}
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800433#else
434static int
435__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
436{
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800437 unsigned int op;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500438 unsigned long ip = rec->ip;
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500439
440 /* read where this goes */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800441 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500442 return -EFAULT;
443
444 /* It should be pointing to a nop */
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800445 if (op != PPC_NOP_INSTR) {
446 printk(KERN_ERR "Expected NOP but have %x\n", op);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500447 return -EINVAL;
448 }
449
450 /* If we never set up a trampoline to ftrace_caller, then bail */
451 if (!rec->arch.mod->arch.tramp) {
452 printk(KERN_ERR "No ftrace trampoline\n");
453 return -EINVAL;
454 }
455
Steven Rostedt0029ff82008-11-25 14:06:19 -0800456 /* create the branch to the trampoline */
457 op = create_branch((unsigned int *)ip,
458 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
459 if (!op) {
460 printk(KERN_ERR "REL24 out of range!\n");
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500461 return -EINVAL;
462 }
463
Steven Rostedt44e1d062009-02-04 18:29:03 -0800464 pr_debug("write to %lx\n", rec->ip);
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500465
Steven Rostedtd9af12b72008-11-25 06:39:18 -0800466 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
Steven Rostedt7cc45e62008-11-15 02:39:05 -0500467 return -EPERM;
468
Steven Rostedtec682ce2008-11-25 10:22:48 -0800469 flush_icache_range(ip, ip + 8);
470
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800471 return 0;
472}
473#endif /* CONFIG_PPC64 */
Steven Rostedt17be5b32009-02-05 21:33:09 -0800474#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800475
476int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
477{
478 unsigned char *old, *new;
479 unsigned long ip = rec->ip;
480
481 /*
482 * If the calling address is more that 24 bits away,
483 * then we had to use a trampoline to make the call.
484 * Otherwise just update the call site.
485 */
486 if (test_24bit_addr(ip, addr)) {
487 /* within range */
488 old = ftrace_nop_replace();
Steven Rostedt46542882009-02-10 22:19:54 -0800489 new = ftrace_call_replace(ip, addr, 1);
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800490 return ftrace_modify_code(ip, old, new);
491 }
492
Steven Rostedt17be5b32009-02-05 21:33:09 -0800493#ifdef CONFIG_MODULES
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800494 /*
495 * Out of range jumps are called from modules.
496 * Being that we are converting from nop, it had better
497 * already have a module defined.
498 */
499 if (!rec->arch.mod) {
500 printk(KERN_ERR "No module loaded\n");
501 return -EINVAL;
502 }
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800503
504 return __ftrace_make_call(rec, addr);
Steven Rostedt17be5b32009-02-05 21:33:09 -0800505#else
506 /* We should not get here without modules */
507 return -EINVAL;
508#endif /* CONFIG_MODULES */
Steven Rostedtf48cb8b2008-11-14 20:47:03 -0800509}
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800510
Steven Rostedt15adc042008-10-23 09:33:08 -0400511int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedt4e491d12008-05-14 23:49:44 -0400512{
513 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530514 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400515 int ret;
516
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530517 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedt46542882009-02-10 22:19:54 -0800518 new = ftrace_call_replace(ip, (unsigned long)func, 1);
Steven Rostedt4e491d12008-05-14 23:49:44 -0400519 ret = ftrace_modify_code(ip, old, new);
520
521 return ret;
522}
523
Steven Rostedt4e491d12008-05-14 23:49:44 -0400524int __init ftrace_dyn_arch_init(void *data)
525{
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800526 /* caller expects data to be zero */
527 unsigned long *p = data;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400528
Steven Rostedt8fd6e5a2008-11-14 16:21:19 -0800529 *p = 0;
Steven Rostedt4e491d12008-05-14 23:49:44 -0400530
531 return 0;
532}
Steven Rostedt6794c782009-02-09 21:10:27 -0800533#endif /* CONFIG_DYNAMIC_FTRACE */
534
535#ifdef CONFIG_FUNCTION_GRAPH_TRACER
536
Steven Rostedt46542882009-02-10 22:19:54 -0800537#ifdef CONFIG_DYNAMIC_FTRACE
538extern void ftrace_graph_call(void);
539extern void ftrace_graph_stub(void);
540
541int ftrace_enable_ftrace_graph_caller(void)
542{
543 unsigned long ip = (unsigned long)(&ftrace_graph_call);
544 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
545 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
546 unsigned char old[MCOUNT_INSN_SIZE], *new;
547
548 new = ftrace_call_replace(ip, stub, 0);
549 memcpy(old, new, MCOUNT_INSN_SIZE);
550 new = ftrace_call_replace(ip, addr, 0);
551
552 return ftrace_modify_code(ip, old, new);
553}
554
555int ftrace_disable_ftrace_graph_caller(void)
556{
557 unsigned long ip = (unsigned long)(&ftrace_graph_call);
558 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
559 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
560 unsigned char old[MCOUNT_INSN_SIZE], *new;
561
562 new = ftrace_call_replace(ip, addr, 0);
563 memcpy(old, new, MCOUNT_INSN_SIZE);
564 new = ftrace_call_replace(ip, stub, 0);
565
566 return ftrace_modify_code(ip, old, new);
567}
568#endif /* CONFIG_DYNAMIC_FTRACE */
569
Steven Rostedtbb725342009-02-11 12:45:49 -0800570#ifdef CONFIG_PPC64
571extern void mod_return_to_handler(void);
572#endif
573
Steven Rostedt6794c782009-02-09 21:10:27 -0800574/*
575 * Hook the return address and push it in the stack of return addrs
576 * in current thread info.
577 */
578void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
579{
580 unsigned long old;
581 unsigned long long calltime;
582 int faulted;
583 struct ftrace_graph_ent trace;
Steven Rostedtbb725342009-02-11 12:45:49 -0800584 unsigned long return_hooker = (unsigned long)&return_to_handler;
Steven Rostedt6794c782009-02-09 21:10:27 -0800585
586 if (unlikely(atomic_read(&current->tracing_graph_pause)))
587 return;
588
Steven Rostedtbb725342009-02-11 12:45:49 -0800589#if CONFIG_PPC64
590 /* non core kernel code needs to save and restore the TOC */
591 if (REGION_ID(self_addr) != KERNEL_REGION_ID)
592 return_hooker = (unsigned long)&mod_return_to_handler;
593#endif
594
Steven Rostedt6794c782009-02-09 21:10:27 -0800595 return_hooker = GET_ADDR(return_hooker);
596
597 /*
598 * Protect against fault, even if it shouldn't
599 * happen. This tool is too much intrusive to
600 * ignore such a protection.
601 */
602 asm volatile(
603 "1: " PPC_LL "%[old], 0(%[parent])\n"
604 "2: " PPC_STL "%[return_hooker], 0(%[parent])\n"
605 " li %[faulted], 0\n"
Steven Rostedtfad4f472009-02-11 19:10:57 -0500606 "3:\n"
Steven Rostedt6794c782009-02-09 21:10:27 -0800607
608 ".section .fixup, \"ax\"\n"
609 "4: li %[faulted], 1\n"
610 " b 3b\n"
611 ".previous\n"
612
613 ".section __ex_table,\"a\"\n"
614 PPC_LONG_ALIGN "\n"
615 PPC_LONG "1b,4b\n"
616 PPC_LONG "2b,4b\n"
617 ".previous"
618
619 : [old] "=r" (old), [faulted] "=r" (faulted)
620 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
621 : "memory"
622 );
623
624 if (unlikely(faulted)) {
625 ftrace_graph_stop();
626 WARN_ON(1);
627 return;
628 }
629
630 calltime = cpu_clock(raw_smp_processor_id());
631
632 if (ftrace_push_return_trace(old, calltime,
633 self_addr, &trace.depth) == -EBUSY) {
634 *parent = old;
635 return;
636 }
637
638 trace.func = self_addr;
639
640 /* Only trace if the calling function expects to */
641 if (!ftrace_graph_entry(&trace)) {
642 current->curr_ret_stack--;
643 *parent = old;
644 }
645}
646#endif /* CONFIG_FUNCTION_GRAPH_TRACER */