ftrace, powerpc: replace debug macro with proper pr_deug
[GitHub/mt8127/android_kernel_alcatel_ttab.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 23static unsigned int ftrace_nop = PPC_NOP_INSTR;
4e491d14
SR
24
25#ifdef CONFIG_PPC32
26# define GET_ADDR(addr) addr
27#else
28/* PowerPC64's functions are data that points to the functions */
f48cb8b4 29# define GET_ADDR(addr) (*(unsigned long *)addr)
4e491d14
SR
30#endif
31
395a59d0 32
15adc048 33static unsigned int ftrace_calc_offset(long ip, long addr)
4e491d14 34{
395a59d0 35 return (int)(addr - ip);
4e491d14
SR
36}
37
8fd6e5a8 38static unsigned char *ftrace_nop_replace(void)
4e491d14
SR
39{
40 return (char *)&ftrace_nop;
41}
42
8fd6e5a8 43static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
4e491d14
SR
44{
45 static unsigned int op;
46
ccbfac29
SR
47 /*
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 */
4e491d14
SR
53 addr = GET_ADDR(addr);
54
55 /* Set to "bl addr" */
ccbfac29 56 op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
4e491d14
SR
57
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
8fd6e5a8 73static int
4e491d14
SR
74ftrace_modify_code(unsigned long ip, unsigned char *old_code,
75 unsigned char *new_code)
76{
e4486fe3 77 unsigned char replaced[MCOUNT_INSN_SIZE];
4e491d14 78
4e491d14
SR
79 /*
80 * Note: Due to modules and __init, code can
81 * disappear and change, we need to protect against faulting
e4486fe3
SR
82 * as well as code changing. We do this by using the
83 * probe_kernel_* functions.
4e491d14
SR
84 *
85 * No real locking needed, this code is run through
e4486fe3 86 * kstop_machine, or before SMP starts.
4e491d14 87 */
e4486fe3
SR
88
89 /* read the text we want to modify */
90 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
91 return -EFAULT;
92
93 /* Make sure it is what we expect it to be */
94 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
95 return -EINVAL;
96
97 /* 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;
4e491d14
SR
104}
105
f48cb8b4
SR
106/*
107 * Helper functions that are the same for both PPC64 and PPC32.
108 */
8fd6e5a8
SR
109static int test_24bit_addr(unsigned long ip, unsigned long addr)
110{
8fd6e5a8 111
0029ff87
SR
112 /* use the create_branch to verify that this offset can be branched */
113 return create_branch((unsigned int *)ip, addr, 0);
8fd6e5a8
SR
114}
115
f48cb8b4
SR
116static int is_bl_op(unsigned int op)
117{
118 return (op & 0xfc000003) == 0x48000001;
119}
120
f48cb8b4
SR
121static 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
f48cb8b4
SR
133#ifdef CONFIG_PPC64
134static int
135__ftrace_make_nop(struct module *mod,
136 struct dyn_ftrace *rec, unsigned long addr)
137{
d9af12b7
SR
138 unsigned int op;
139 unsigned int jmp[5];
140 unsigned long ptr;
f48cb8b4
SR
141 unsigned long ip = rec->ip;
142 unsigned long tramp;
143 int offset;
144
145 /* read where this goes */
d9af12b7 146 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
f48cb8b4
SR
147 return -EFAULT;
148
149 /* Make sure that that this is still a 24bit jump */
d9af12b7
SR
150 if (!is_bl_op(op)) {
151 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
f48cb8b4
SR
152 return -EINVAL;
153 }
154
155 /* lets find where the pointer goes */
d9af12b7 156 tramp = find_bl_target(ip, op);
f48cb8b4
SR
157
158 /*
159 * 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)
170 */
171
44e1d064 172 pr_debug("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
f48cb8b4
SR
173
174 /* Find where the trampoline jumps to */
d9af12b7 175 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
f48cb8b4
SR
176 printk(KERN_ERR "Failed to read %lx\n", tramp);
177 return -EFAULT;
178 }
179
44e1d064 180 pr_debug(" %08x %08x", jmp[0], jmp[1]);
d9af12b7
SR
181
182 /* 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 }
f48cb8b4 191
f25f9074
SR
192 /* The bottom half is signed extended */
193 offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
194 (int)((short)jmp[1]);
f48cb8b4 195
44e1d064 196 pr_debug(" %x ", offset);
f48cb8b4
SR
197
198 /* get the address this jumps too */
199 tramp = mod->arch.toc + offset + 32;
44e1d064 200 pr_debug("toc: %lx", tramp);
f48cb8b4
SR
201
202 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
203 printk(KERN_ERR "Failed to read %lx\n", tramp);
204 return -EFAULT;
205 }
206
44e1d064 207 pr_debug(" %08x %08x\n", jmp[0], jmp[1]);
d9af12b7
SR
208
209 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
f48cb8b4
SR
210
211 /* This should match what was called */
d9af12b7
SR
212 if (ptr != GET_ADDR(addr)) {
213 printk(KERN_ERR "addr does not match %lx\n", ptr);
f48cb8b4
SR
214 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 */
d9af12b7 222 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
f48cb8b4
SR
223 return -EFAULT;
224
d9af12b7
SR
225 if (op != 0xe8410028) {
226 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
f48cb8b4
SR
227 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 */
d9af12b7 243 op = 0x48000008; /* b +8 */
f48cb8b4 244
d9af12b7 245 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
f48cb8b4
SR
246 return -EPERM;
247
ec682cef
SR
248
249 flush_icache_range(ip, ip + 8);
250
f48cb8b4
SR
251 return 0;
252}
253
254#else /* !PPC64 */
255static int
256__ftrace_make_nop(struct module *mod,
257 struct dyn_ftrace *rec, unsigned long addr)
258{
d9af12b7
SR
259 unsigned int op;
260 unsigned int jmp[4];
7cc45e64
SR
261 unsigned long ip = rec->ip;
262 unsigned long tramp;
7cc45e64 263
d9af12b7 264 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
7cc45e64
SR
265 return -EFAULT;
266
267 /* Make sure that that this is still a 24bit jump */
d9af12b7
SR
268 if (!is_bl_op(op)) {
269 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
7cc45e64
SR
270 return -EINVAL;
271 }
272
273 /* lets find where the pointer goes */
d9af12b7 274 tramp = find_bl_target(ip, op);
7cc45e64
SR
275
276 /*
277 * On PPC32 the trampoline looks like:
d9af12b7
SR
278 * 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
7cc45e64
SR
282 */
283
44e1d064 284 pr_debug("ip:%lx jumps to %lx", ip, tramp);
7cc45e64
SR
285
286 /* Find where the trampoline jumps to */
d9af12b7 287 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
7cc45e64
SR
288 printk(KERN_ERR "Failed to read %lx\n", tramp);
289 return -EFAULT;
290 }
291
44e1d064 292 pr_debug(" %08x %08x ", jmp[0], jmp[1]);
d9af12b7
SR
293
294 /* 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 }
7cc45e64 302
d9af12b7
SR
303 tramp = (jmp[1] & 0xffff) |
304 ((jmp[0] & 0xffff) << 16);
7cc45e64
SR
305 if (tramp & 0x8000)
306 tramp -= 0x10000;
307
44e1d064 308 pr_debug(" %x ", tramp);
7cc45e64
SR
309
310 if (tramp != addr) {
311 printk(KERN_ERR
312 "Trampoline location %08lx does not match addr\n",
313 tramp);
314 return -EINVAL;
315 }
316
d9af12b7 317 op = PPC_NOP_INSTR;
7cc45e64 318
d9af12b7 319 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
7cc45e64
SR
320 return -EPERM;
321
ec682cef
SR
322 flush_icache_range(ip, ip + 8);
323
f48cb8b4
SR
324 return 0;
325}
326#endif /* PPC64 */
327
8fd6e5a8
SR
328int ftrace_make_nop(struct module *mod,
329 struct dyn_ftrace *rec, unsigned long addr)
330{
331 unsigned char *old, *new;
f48cb8b4 332 unsigned long ip = rec->ip;
8fd6e5a8
SR
333
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 */
f48cb8b4 339 if (test_24bit_addr(ip, addr)) {
8fd6e5a8 340 /* within range */
f48cb8b4 341 old = ftrace_call_replace(ip, addr);
8fd6e5a8 342 new = ftrace_nop_replace();
f48cb8b4
SR
343 return ftrace_modify_code(ip, old, new);
344 }
345
f48cb8b4
SR
346 /*
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;
f48cb8b4
SR
368
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{
d9af12b7 377 unsigned int op[2];
f48cb8b4 378 unsigned long ip = rec->ip;
f48cb8b4
SR
379
380 /* read where this goes */
d9af12b7 381 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
f48cb8b4
SR
382 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
0029ff87
SR
400 /* 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");
f48cb8b4 405 return -EINVAL;
8fd6e5a8
SR
406 }
407
f48cb8b4
SR
408 /* ld r2,40(r1) */
409 op[1] = 0xe8410028;
410
44e1d064 411 pr_debug("write to %lx\n", rec->ip);
f48cb8b4 412
d9af12b7 413 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
f48cb8b4
SR
414 return -EPERM;
415
ec682cef
SR
416 flush_icache_range(ip, ip + 8);
417
8fd6e5a8
SR
418 return 0;
419}
f48cb8b4
SR
420#else
421static int
422__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
423{
d9af12b7 424 unsigned int op;
7cc45e64 425 unsigned long ip = rec->ip;
7cc45e64
SR
426
427 /* read where this goes */
d9af12b7 428 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
7cc45e64
SR
429 return -EFAULT;
430
431 /* It should be pointing to a nop */
d9af12b7
SR
432 if (op != PPC_NOP_INSTR) {
433 printk(KERN_ERR "Expected NOP but have %x\n", op);
7cc45e64
SR
434 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
0029ff87
SR
443 /* 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");
7cc45e64
SR
448 return -EINVAL;
449 }
450
44e1d064 451 pr_debug("write to %lx\n", rec->ip);
7cc45e64 452
d9af12b7 453 if (probe_kernel_write((void *)ip, &op, MCOUNT_INSN_SIZE))
7cc45e64
SR
454 return -EPERM;
455
ec682cef
SR
456 flush_icache_range(ip, ip + 8);
457
f48cb8b4
SR
458 return 0;
459}
460#endif /* CONFIG_PPC64 */
8fd6e5a8
SR
461
462int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
463{
464 unsigned char *old, *new;
f48cb8b4 465 unsigned long ip = rec->ip;
8fd6e5a8
SR
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 */
f48cb8b4 472 if (test_24bit_addr(ip, addr)) {
8fd6e5a8
SR
473 /* within range */
474 old = ftrace_nop_replace();
f48cb8b4
SR
475 new = ftrace_call_replace(ip, addr);
476 return ftrace_modify_code(ip, old, new);
8fd6e5a8
SR
477 }
478
f48cb8b4
SR
479 /*
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 }
f48cb8b4
SR
488
489 return __ftrace_make_call(rec, addr);
8fd6e5a8
SR
490}
491
15adc048 492int ftrace_update_ftrace_func(ftrace_func_t func)
4e491d14
SR
493{
494 unsigned long ip = (unsigned long)(&ftrace_call);
395a59d0 495 unsigned char old[MCOUNT_INSN_SIZE], *new;
4e491d14
SR
496 int ret;
497
395a59d0 498 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
4e491d14
SR
499 new = ftrace_call_replace(ip, (unsigned long)func);
500 ret = ftrace_modify_code(ip, old, new);
501
502 return ret;
503}
504
4e491d14
SR
505int __init ftrace_dyn_arch_init(void *data)
506{
8fd6e5a8
SR
507 /* caller expects data to be zero */
508 unsigned long *p = data;
4e491d14 509
8fd6e5a8 510 *p = 0;
4e491d14
SR
511
512 return 0;
513}