Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / arch / powerpc / kernel / ftrace.c
CommitLineData
4e491d14
SR
1/*
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>
e4486fe3 12#include <linux/uaccess.h>
f48cb8b4 13#include <linux/module.h>
4e491d14
SR
14#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>
f48cb8b4 20#include <asm/code-patching.h>
395a59d0 21#include <asm/ftrace.h>
4e491d14 22
f48cb8b4
SR
23#if 0
24#define DEBUGP printk
25#else
26#define DEBUGP(fmt , ...) do { } while (0)
27#endif
4e491d14 28
f48cb8b4 29static unsigned int ftrace_nop = PPC_NOP_INSTR;
4e491d14
SR
30
31#ifdef CONFIG_PPC32
32# define GET_ADDR(addr) addr
33#else
34/* PowerPC64's functions are data that points to the functions */
f48cb8b4 35# define GET_ADDR(addr) (*(unsigned long *)addr)
4e491d14
SR
36#endif
37
395a59d0 38
15adc048 39static unsigned int ftrace_calc_offset(long ip, long addr)
4e491d14 40{
395a59d0 41 return (int)(addr - ip);
4e491d14
SR
42}
43
8fd6e5a8 44static unsigned char *ftrace_nop_replace(void)
4e491d14
SR
45{
46 return (char *)&ftrace_nop;
47}
48
8fd6e5a8 49static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
4e491d14
SR
50{
51 static unsigned int op;
52
ccbfac29
SR
53 /*
54 * It would be nice to just use create_function_call, but that will
55 * update the code itself. Here we need to just return the
56 * instruction that is going to be modified, without modifying the
57 * code.
58 */
4e491d14
SR
59 addr = GET_ADDR(addr);
60
61 /* Set to "bl addr" */
ccbfac29 62 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
4e491d14
SR
63
64 /*
65 * No locking needed, this must be called via kstop_machine
66 * which in essence is like running on a uniprocessor machine.
67 */
68 return (unsigned char *)&op;
69}
70
71#ifdef CONFIG_PPC64
72# define _ASM_ALIGN " .align 3 "
73# define _ASM_PTR " .llong "
74#else
75# define _ASM_ALIGN " .align 2 "
76# define _ASM_PTR " .long "
77#endif
78
8fd6e5a8 79static int
4e491d14
SR
80ftrace_modify_code(unsigned long ip, unsigned char *old_code,
81 unsigned char *new_code)
82{
e4486fe3 83 unsigned char replaced[MCOUNT_INSN_SIZE];
4e491d14 84
4e491d14
SR
85 /*
86 * Note: Due to modules and __init, code can
87 * disappear and change, we need to protect against faulting
e4486fe3
SR
88 * as well as code changing. We do this by using the
89 * probe_kernel_* functions.
4e491d14
SR
90 *
91 * No real locking needed, this code is run through
e4486fe3 92 * kstop_machine, or before SMP starts.
4e491d14 93 */
e4486fe3
SR
94
95 /* read the text we want to modify */
96 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
97 return -EFAULT;
98
99 /* Make sure it is what we expect it to be */
100 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
101 return -EINVAL;
102
103 /* replace the text with the new text */
104 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
105 return -EPERM;
106
107 flush_icache_range(ip, ip + 8);
108
109 return 0;
4e491d14
SR
110}
111
f48cb8b4
SR
112/*
113 * Helper functions that are the same for both PPC64 and PPC32.
114 */
8fd6e5a8
SR
115static int test_24bit_addr(unsigned long ip, unsigned long addr)
116{
8fd6e5a8 117
0029ff87
SR
118 /* use the create_branch to verify that this offset can be branched */
119 return create_branch((unsigned int *)ip, addr, 0);
8fd6e5a8
SR
120}
121
f48cb8b4
SR
122static int is_bl_op(unsigned int op)
123{
124 return (op & 0xfc000003) == 0x48000001;
125}
126
f48cb8b4
SR
127static unsigned long find_bl_target(unsigned long ip, unsigned int op)
128{
129 static int offset;
130
131 offset = (op & 0x03fffffc);
132 /* make it signed */
133 if (offset & 0x02000000)
134 offset |= 0xfe000000;
135
136 return ip + (long)offset;
137}
138
f48cb8b4
SR
139#ifdef CONFIG_PPC64
140static int
141__ftrace_make_nop(struct module *mod,
142 struct dyn_ftrace *rec, unsigned long addr)
143{
d9af12b7
SR
144 unsigned int op;
145 unsigned int jmp[5];
146 unsigned long ptr;
f48cb8b4
SR
147 unsigned long ip = rec->ip;
148 unsigned long tramp;
149 int offset;
150
151 /* read where this goes */
d9af12b7 152 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
f48cb8b4
SR
153 return -EFAULT;
154
155 /* Make sure that that this is still a 24bit jump */
d9af12b7
SR
156 if (!is_bl_op(op)) {
157 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
f48cb8b4
SR
158 return -EINVAL;
159 }
160
161 /* lets find where the pointer goes */
d9af12b7 162 tramp = find_bl_target(ip, op);
f48cb8b4
SR
163
164 /*
165 * On PPC64 the trampoline looks like:
166 * 0x3d, 0x82, 0x00, 0x00, addis r12,r2, <high>
167 * 0x39, 0x8c, 0x00, 0x00, addi r12,r12, <low>
168 * Where the bytes 2,3,6 and 7 make up the 32bit offset
169 * to the TOC that holds the pointer.
170 * to jump to.
171 * 0xf8, 0x41, 0x00, 0x28, std r2,40(r1)
172 * 0xe9, 0x6c, 0x00, 0x20, ld r11,32(r12)
173 * The actually address is 32 bytes from the offset
174 * into the TOC.
175 * 0xe8, 0x4c, 0x00, 0x28, ld r2,40(r12)
176 */
177
178 DEBUGP("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
179
180 /* Find where the trampoline jumps to */
d9af12b7 181 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
f48cb8b4
SR
182 printk(KERN_ERR "Failed to read %lx\n", tramp);
183 return -EFAULT;
184 }
185
d9af12b7
SR
186 DEBUGP(" %08x %08x", jmp[0], jmp[1]);
187
188 /* verify that this is what we expect it to be */
189 if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
190 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
191 (jmp[2] != 0xf8410028) ||
192 (jmp[3] != 0xe96c0020) ||
193 (jmp[4] != 0xe84c0028)) {
194 printk(KERN_ERR "Not a trampoline\n");
195 return -EINVAL;
196 }
f48cb8b4 197
f25f9074
SR
198 /* The bottom half is signed extended */
199 offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
200 (int)((short)jmp[1]);
f48cb8b4
SR
201
202 DEBUGP(" %x ", offset);
203
204 /* get the address this jumps too */
205 tramp = mod->arch.toc + offset + 32;
206 DEBUGP("toc: %lx", tramp);
207
208 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
209 printk(KERN_ERR "Failed to read %lx\n", tramp);
210 return -EFAULT;
211 }
212
d9af12b7
SR
213 DEBUGP(" %08x %08x\n", jmp[0], jmp[1]);
214
215 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
f48cb8b4
SR
216
217 /* This should match what was called */
d9af12b7
SR
218 if (ptr != GET_ADDR(addr)) {
219 printk(KERN_ERR "addr does not match %lx\n", ptr);
f48cb8b4
SR
220 return -EINVAL;
221 }
222
223 /*
224 * We want to nop the line, but the next line is
225 * 0xe8, 0x41, 0x00, 0x28 ld r2,40(r1)
226 * This needs to be turned to a nop too.
227 */
d9af12b7 228 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
f48cb8b4
SR
229 return -EFAULT;
230
d9af12b7
SR
231 if (op != 0xe8410028) {
232 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
f48cb8b4
SR
233 return -EINVAL;
234 }
235
236 /*
237 * Milton Miller pointed out that we can not blindly do nops.
238 * If a task was preempted when calling a trace function,
239 * the nops will remove the way to restore the TOC in r2
240 * and the r2 TOC will get corrupted.
241 */
242
243 /*
244 * Replace:
245 * bl <tramp> <==== will be replaced with "b 1f"
246 * ld r2,40(r1)
247 * 1:
248 */
d9af12b7 249 op = 0x48000008; /* b +8 */
f48cb8b4 250
d9af12b7 251 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
f48cb8b4
SR
252 return -EPERM;
253
ec682cef
SR
254
255 flush_icache_range(ip, ip + 8);
256
f48cb8b4
SR
257 return 0;
258}
259
260#else /* !PPC64 */
261static int
262__ftrace_make_nop(struct module *mod,
263 struct dyn_ftrace *rec, unsigned long addr)
264{
d9af12b7
SR
265 unsigned int op;
266 unsigned int jmp[4];
7cc45e64
SR
267 unsigned long ip = rec->ip;
268 unsigned long tramp;
7cc45e64 269
d9af12b7 270 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
7cc45e64
SR
271 return -EFAULT;
272
273 /* Make sure that that this is still a 24bit jump */
d9af12b7
SR
274 if (!is_bl_op(op)) {
275 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
7cc45e64
SR
276 return -EINVAL;
277 }
278
279 /* lets find where the pointer goes */
d9af12b7 280 tramp = find_bl_target(ip, op);
7cc45e64
SR
281
282 /*
283 * On PPC32 the trampoline looks like:
d9af12b7
SR
284 * 0x3d, 0x60, 0x00, 0x00 lis r11,sym@ha
285 * 0x39, 0x6b, 0x00, 0x00 addi r11,r11,sym@l
286 * 0x7d, 0x69, 0x03, 0xa6 mtctr r11
287 * 0x4e, 0x80, 0x04, 0x20 bctr
7cc45e64
SR
288 */
289
290 DEBUGP("ip:%lx jumps to %lx", ip, tramp);
291
292 /* Find where the trampoline jumps to */
d9af12b7 293 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
7cc45e64
SR
294 printk(KERN_ERR "Failed to read %lx\n", tramp);
295 return -EFAULT;
296 }
297
d9af12b7
SR
298 DEBUGP(" %08x %08x ", jmp[0], jmp[1]);
299
300 /* verify that this is what we expect it to be */
301 if (((jmp[0] & 0xffff0000) != 0x3d600000) ||
302 ((jmp[1] & 0xffff0000) != 0x396b0000) ||
303 (jmp[2] != 0x7d6903a6) ||
304 (jmp[3] != 0x4e800420)) {
305 printk(KERN_ERR "Not a trampoline\n");
306 return -EINVAL;
307 }
7cc45e64 308
d9af12b7
SR
309 tramp = (jmp[1] & 0xffff) |
310 ((jmp[0] & 0xffff) << 16);
7cc45e64
SR
311 if (tramp & 0x8000)
312 tramp -= 0x10000;
313
314 DEBUGP(" %x ", tramp);
315
316 if (tramp != addr) {
317 printk(KERN_ERR
318 "Trampoline location %08lx does not match addr\n",
319 tramp);
320 return -EINVAL;
321 }
322
d9af12b7 323 op = PPC_NOP_INSTR;
7cc45e64 324
d9af12b7 325 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
7cc45e64
SR
326 return -EPERM;
327
ec682cef
SR
328 flush_icache_range(ip, ip + 8);
329
f48cb8b4
SR
330 return 0;
331}
332#endif /* PPC64 */
333
8fd6e5a8
SR
334int ftrace_make_nop(struct module *mod,
335 struct dyn_ftrace *rec, unsigned long addr)
336{
337 unsigned char *old, *new;
f48cb8b4 338 unsigned long ip = rec->ip;
8fd6e5a8
SR
339
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 */
f48cb8b4 345 if (test_24bit_addr(ip, addr)) {
8fd6e5a8 346 /* within range */
f48cb8b4 347 old = ftrace_call_replace(ip, addr);
8fd6e5a8 348 new = ftrace_nop_replace();
f48cb8b4
SR
349 return ftrace_modify_code(ip, old, new);
350 }
351
f48cb8b4
SR
352 /*
353 * Out of range jumps are called from modules.
354 * We should either already have a pointer to the module
355 * or it has been passed in.
356 */
357 if (!rec->arch.mod) {
358 if (!mod) {
359 printk(KERN_ERR "No module loaded addr=%lx\n",
360 addr);
361 return -EFAULT;
362 }
363 rec->arch.mod = mod;
364 } else if (mod) {
365 if (mod != rec->arch.mod) {
366 printk(KERN_ERR
367 "Record mod %p not equal to passed in mod %p\n",
368 rec->arch.mod, mod);
369 return -EINVAL;
370 }
371 /* nothing to do if mod == rec->arch.mod */
372 } else
373 mod = rec->arch.mod;
f48cb8b4
SR
374
375 return __ftrace_make_nop(mod, rec, addr);
376
377}
378
379#ifdef CONFIG_PPC64
380static int
381__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
382{
d9af12b7 383 unsigned int op[2];
f48cb8b4 384 unsigned long ip = rec->ip;
f48cb8b4
SR
385
386 /* read where this goes */
d9af12b7 387 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
f48cb8b4
SR
388 return -EFAULT;
389
390 /*
391 * It should be pointing to two nops or
392 * b +8; ld r2,40(r1)
393 */
394 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
395 ((op[0] != PPC_NOP_INSTR) || (op[1] != PPC_NOP_INSTR))) {
396 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
397 return -EINVAL;
398 }
399
400 /* If we never set up a trampoline to ftrace_caller, then bail */
401 if (!rec->arch.mod->arch.tramp) {
402 printk(KERN_ERR "No ftrace trampoline\n");
403 return -EINVAL;
404 }
405
0029ff87
SR
406 /* create the branch to the trampoline */
407 op[0] = create_branch((unsigned int *)ip,
408 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
409 if (!op[0]) {
410 printk(KERN_ERR "REL24 out of range!\n");
f48cb8b4 411 return -EINVAL;
8fd6e5a8
SR
412 }
413
f48cb8b4
SR
414 /* ld r2,40(r1) */
415 op[1] = 0xe8410028;
416
417 DEBUGP("write to %lx\n", rec->ip);
418
d9af12b7 419 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
f48cb8b4
SR
420 return -EPERM;
421
ec682cef
SR
422 flush_icache_range(ip, ip + 8);
423
8fd6e5a8
SR
424 return 0;
425}
f48cb8b4
SR
426#else
427static int
428__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
429{
d9af12b7 430 unsigned int op;
7cc45e64 431 unsigned long ip = rec->ip;
7cc45e64
SR
432
433 /* read where this goes */
d9af12b7 434 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
7cc45e64
SR
435 return -EFAULT;
436
437 /* It should be pointing to a nop */
d9af12b7
SR
438 if (op != PPC_NOP_INSTR) {
439 printk(KERN_ERR "Expected NOP but have %x\n", op);
7cc45e64
SR
440 return -EINVAL;
441 }
442
443 /* If we never set up a trampoline to ftrace_caller, then bail */
444 if (!rec->arch.mod->arch.tramp) {
445 printk(KERN_ERR "No ftrace trampoline\n");
446 return -EINVAL;
447 }
448
0029ff87
SR
449 /* create the branch to the trampoline */
450 op = create_branch((unsigned int *)ip,
451 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
452 if (!op) {
453 printk(KERN_ERR "REL24 out of range!\n");
7cc45e64
SR
454 return -EINVAL;
455 }
456
7cc45e64
SR
457 DEBUGP("write to %lx\n", rec->ip);
458
d9af12b7 459 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
7cc45e64
SR
460 return -EPERM;
461
ec682cef
SR
462 flush_icache_range(ip, ip + 8);
463
f48cb8b4
SR
464 return 0;
465}
466#endif /* CONFIG_PPC64 */
8fd6e5a8
SR
467
468int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
469{
470 unsigned char *old, *new;
f48cb8b4 471 unsigned long ip = rec->ip;
8fd6e5a8
SR
472
473 /*
474 * If the calling address is more that 24 bits away,
475 * then we had to use a trampoline to make the call.
476 * Otherwise just update the call site.
477 */
f48cb8b4 478 if (test_24bit_addr(ip, addr)) {
8fd6e5a8
SR
479 /* within range */
480 old = ftrace_nop_replace();
f48cb8b4
SR
481 new = ftrace_call_replace(ip, addr);
482 return ftrace_modify_code(ip, old, new);
8fd6e5a8
SR
483 }
484
f48cb8b4
SR
485 /*
486 * Out of range jumps are called from modules.
487 * Being that we are converting from nop, it had better
488 * already have a module defined.
489 */
490 if (!rec->arch.mod) {
491 printk(KERN_ERR "No module loaded\n");
492 return -EINVAL;
493 }
f48cb8b4
SR
494
495 return __ftrace_make_call(rec, addr);
8fd6e5a8
SR
496}
497
15adc048 498int ftrace_update_ftrace_func(ftrace_func_t func)
4e491d14
SR
499{
500 unsigned long ip = (unsigned long)(&ftrace_call);
395a59d0 501 unsigned char old[MCOUNT_INSN_SIZE], *new;
4e491d14
SR
502 int ret;
503
395a59d0 504 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
4e491d14
SR
505 new = ftrace_call_replace(ip, (unsigned long)func);
506 ret = ftrace_modify_code(ip, old, new);
507
508 return ret;
509}
510
4e491d14
SR
511int __init ftrace_dyn_arch_init(void *data)
512{
8fd6e5a8
SR
513 /* caller expects data to be zero */
514 unsigned long *p = data;
4e491d14 515
8fd6e5a8 516 *p = 0;
4e491d14
SR
517
518 return 0;
519}