1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/spinlock.h>
14#include <linux/hardirq.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#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>
23#include <asm/code-patching.h>
24#include <asm/ftrace.h>
25#include <asm/syscall.h>
26
27
28#ifdef CONFIG_DYNAMIC_FTRACE
29static unsigned int
30ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
31{
32 unsigned int op;
33
34 addr = ppc_function_entry((void *)addr);
35
36
37 op = create_branch((unsigned int *)ip, addr, link ? 1 : 0);
38
39 return op;
40}
41
42static int
43ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new)
44{
45 unsigned int replaced;
46
47
48
49
50
51
52
53
54
55
56
57
58 if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE))
59 return -EFAULT;
60
61
62 if (replaced != old)
63 return -EINVAL;
64
65
66 if (patch_instruction((unsigned int *)ip, new))
67 return -EPERM;
68
69 return 0;
70}
71
72
73
74
75static int test_24bit_addr(unsigned long ip, unsigned long addr)
76{
77
78
79 return create_branch((unsigned int *)ip, addr, 0);
80}
81
82#ifdef CONFIG_MODULES
83
84static int is_bl_op(unsigned int op)
85{
86 return (op & 0xfc000003) == 0x48000001;
87}
88
89static unsigned long find_bl_target(unsigned long ip, unsigned int op)
90{
91 static int offset;
92
93 offset = (op & 0x03fffffc);
94
95 if (offset & 0x02000000)
96 offset |= 0xfe000000;
97
98 return ip + (long)offset;
99}
100
101#ifdef CONFIG_PPC64
102static int
103__ftrace_make_nop(struct module *mod,
104 struct dyn_ftrace *rec, unsigned long addr)
105{
106 unsigned int op;
107 unsigned int jmp[5];
108 unsigned long ptr;
109 unsigned long ip = rec->ip;
110 unsigned long tramp;
111 int offset;
112
113
114 if (probe_kernel_read(&op, (void *)ip, sizeof(int)))
115 return -EFAULT;
116
117
118 if (!is_bl_op(op)) {
119 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
120 return -EINVAL;
121 }
122
123
124 tramp = find_bl_target(ip, op);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 pr_devel("ip:%lx jumps to %lx r2: %lx", ip, tramp, mod->arch.toc);
141
142
143 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
144 printk(KERN_ERR "Failed to read %lx\n", tramp);
145 return -EFAULT;
146 }
147
148 pr_devel(" %08x %08x", jmp[0], jmp[1]);
149
150
151 if (((jmp[0] & 0xffff0000) != 0x3d820000) ||
152 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
153 (jmp[2] != 0xf8410028) ||
154 (jmp[3] != 0xe96c0020) ||
155 (jmp[4] != 0xe84c0028)) {
156 printk(KERN_ERR "Not a trampoline\n");
157 return -EINVAL;
158 }
159
160
161 offset = ((unsigned)((unsigned short)jmp[0]) << 16) +
162 (int)((short)jmp[1]);
163
164 pr_devel(" %x ", offset);
165
166
167 tramp = mod->arch.toc + offset + 32;
168 pr_devel("toc: %lx", tramp);
169
170 if (probe_kernel_read(jmp, (void *)tramp, 8)) {
171 printk(KERN_ERR "Failed to read %lx\n", tramp);
172 return -EFAULT;
173 }
174
175 pr_devel(" %08x %08x\n", jmp[0], jmp[1]);
176
177 ptr = ((unsigned long)jmp[0] << 32) + jmp[1];
178
179
180 if (ptr != ppc_function_entry((void *)addr)) {
181 printk(KERN_ERR "addr does not match %lx\n", ptr);
182 return -EINVAL;
183 }
184
185
186
187
188
189
190 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE))
191 return -EFAULT;
192
193 if (op != 0xe8410028) {
194 printk(KERN_ERR "Next line is not ld! (%08x)\n", op);
195 return -EINVAL;
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 op = 0x48000008;
212
213 if (patch_instruction((unsigned int *)ip, op))
214 return -EPERM;
215
216 return 0;
217}
218
219#else
220static int
221__ftrace_make_nop(struct module *mod,
222 struct dyn_ftrace *rec, unsigned long addr)
223{
224 unsigned int op;
225 unsigned int jmp[4];
226 unsigned long ip = rec->ip;
227 unsigned long tramp;
228
229 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
230 return -EFAULT;
231
232
233 if (!is_bl_op(op)) {
234 printk(KERN_ERR "Not expected bl: opcode is %x\n", op);
235 return -EINVAL;
236 }
237
238
239 tramp = find_bl_target(ip, op);
240
241
242
243
244
245
246
247
248
249 pr_devel("ip:%lx jumps to %lx", ip, tramp);
250
251
252 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
253 printk(KERN_ERR "Failed to read %lx\n", tramp);
254 return -EFAULT;
255 }
256
257 pr_devel(" %08x %08x ", jmp[0], jmp[1]);
258
259
260 if (((jmp[0] & 0xffff0000) != 0x3d800000) ||
261 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
262 (jmp[2] != 0x7d8903a6) ||
263 (jmp[3] != 0x4e800420)) {
264 printk(KERN_ERR "Not a trampoline\n");
265 return -EINVAL;
266 }
267
268 tramp = (jmp[1] & 0xffff) |
269 ((jmp[0] & 0xffff) << 16);
270 if (tramp & 0x8000)
271 tramp -= 0x10000;
272
273 pr_devel(" %lx ", tramp);
274
275 if (tramp != addr) {
276 printk(KERN_ERR
277 "Trampoline location %08lx does not match addr\n",
278 tramp);
279 return -EINVAL;
280 }
281
282 op = PPC_INST_NOP;
283
284 if (patch_instruction((unsigned int *)ip, op))
285 return -EPERM;
286
287 return 0;
288}
289#endif
290#endif
291
292int ftrace_make_nop(struct module *mod,
293 struct dyn_ftrace *rec, unsigned long addr)
294{
295 unsigned long ip = rec->ip;
296 unsigned int old, new;
297
298
299
300
301
302
303 if (test_24bit_addr(ip, addr)) {
304
305 old = ftrace_call_replace(ip, addr, 1);
306 new = PPC_INST_NOP;
307 return ftrace_modify_code(ip, old, new);
308 }
309
310#ifdef CONFIG_MODULES
311
312
313
314
315
316 if (!rec->arch.mod) {
317 if (!mod) {
318 printk(KERN_ERR "No module loaded addr=%lx\n",
319 addr);
320 return -EFAULT;
321 }
322 rec->arch.mod = mod;
323 } else if (mod) {
324 if (mod != rec->arch.mod) {
325 printk(KERN_ERR
326 "Record mod %p not equal to passed in mod %p\n",
327 rec->arch.mod, mod);
328 return -EINVAL;
329 }
330
331 } else
332 mod = rec->arch.mod;
333
334 return __ftrace_make_nop(mod, rec, addr);
335#else
336
337 return -EINVAL;
338#endif
339}
340
341#ifdef CONFIG_MODULES
342#ifdef CONFIG_PPC64
343static int
344__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
345{
346 unsigned int op[2];
347 unsigned long ip = rec->ip;
348
349
350 if (probe_kernel_read(op, (void *)ip, MCOUNT_INSN_SIZE * 2))
351 return -EFAULT;
352
353
354
355
356
357 if (((op[0] != 0x48000008) || (op[1] != 0xe8410028)) &&
358 ((op[0] != PPC_INST_NOP) || (op[1] != PPC_INST_NOP))) {
359 printk(KERN_ERR "Expected NOPs but have %x %x\n", op[0], op[1]);
360 return -EINVAL;
361 }
362
363
364 if (!rec->arch.mod->arch.tramp) {
365 printk(KERN_ERR "No ftrace trampoline\n");
366 return -EINVAL;
367 }
368
369
370 op[0] = create_branch((unsigned int *)ip,
371 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
372 if (!op[0]) {
373 printk(KERN_ERR "REL24 out of range!\n");
374 return -EINVAL;
375 }
376
377
378 op[1] = 0xe8410028;
379
380 pr_devel("write to %lx\n", rec->ip);
381
382 if (probe_kernel_write((void *)ip, op, MCOUNT_INSN_SIZE * 2))
383 return -EPERM;
384
385 flush_icache_range(ip, ip + 8);
386
387 return 0;
388}
389#else
390static int
391__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
392{
393 unsigned int op;
394 unsigned long ip = rec->ip;
395
396
397 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
398 return -EFAULT;
399
400
401 if (op != PPC_INST_NOP) {
402 printk(KERN_ERR "Expected NOP but have %x\n", op);
403 return -EINVAL;
404 }
405
406
407 if (!rec->arch.mod->arch.tramp) {
408 printk(KERN_ERR "No ftrace trampoline\n");
409 return -EINVAL;
410 }
411
412
413 op = create_branch((unsigned int *)ip,
414 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
415 if (!op) {
416 printk(KERN_ERR "REL24 out of range!\n");
417 return -EINVAL;
418 }
419
420 pr_devel("write to %lx\n", rec->ip);
421
422 if (patch_instruction((unsigned int *)ip, op))
423 return -EPERM;
424
425 return 0;
426}
427#endif
428#endif
429
430int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
431{
432 unsigned long ip = rec->ip;
433 unsigned int old, new;
434
435
436
437
438
439
440 if (test_24bit_addr(ip, addr)) {
441
442 old = PPC_INST_NOP;
443 new = ftrace_call_replace(ip, addr, 1);
444 return ftrace_modify_code(ip, old, new);
445 }
446
447#ifdef CONFIG_MODULES
448
449
450
451
452
453 if (!rec->arch.mod) {
454 printk(KERN_ERR "No module loaded\n");
455 return -EINVAL;
456 }
457
458 return __ftrace_make_call(rec, addr);
459#else
460
461 return -EINVAL;
462#endif
463}
464
465int ftrace_update_ftrace_func(ftrace_func_t func)
466{
467 unsigned long ip = (unsigned long)(&ftrace_call);
468 unsigned int old, new;
469 int ret;
470
471 old = *(unsigned int *)&ftrace_call;
472 new = ftrace_call_replace(ip, (unsigned long)func, 1);
473 ret = ftrace_modify_code(ip, old, new);
474
475 return ret;
476}
477
478static int __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
479{
480 unsigned long ftrace_addr = (unsigned long)FTRACE_ADDR;
481 int ret;
482
483 ret = ftrace_update_record(rec, enable);
484
485 switch (ret) {
486 case FTRACE_UPDATE_IGNORE:
487 return 0;
488 case FTRACE_UPDATE_MAKE_CALL:
489 return ftrace_make_call(rec, ftrace_addr);
490 case FTRACE_UPDATE_MAKE_NOP:
491 return ftrace_make_nop(NULL, rec, ftrace_addr);
492 }
493
494 return 0;
495}
496
497void ftrace_replace_code(int enable)
498{
499 struct ftrace_rec_iter *iter;
500 struct dyn_ftrace *rec;
501 int ret;
502
503 for (iter = ftrace_rec_iter_start(); iter;
504 iter = ftrace_rec_iter_next(iter)) {
505 rec = ftrace_rec_iter_record(iter);
506 ret = __ftrace_replace_code(rec, enable);
507 if (ret) {
508 ftrace_bug(ret, rec->ip);
509 return;
510 }
511 }
512}
513
514void arch_ftrace_update_code(int command)
515{
516 if (command & FTRACE_UPDATE_CALLS)
517 ftrace_replace_code(1);
518 else if (command & FTRACE_DISABLE_CALLS)
519 ftrace_replace_code(0);
520
521 if (command & FTRACE_UPDATE_TRACE_FUNC)
522 ftrace_update_ftrace_func(ftrace_trace_function);
523
524 if (command & FTRACE_START_FUNC_RET)
525 ftrace_enable_ftrace_graph_caller();
526 else if (command & FTRACE_STOP_FUNC_RET)
527 ftrace_disable_ftrace_graph_caller();
528}
529
530int __init ftrace_dyn_arch_init(void *data)
531{
532
533 unsigned long *p = data;
534
535 *p = 0;
536
537 return 0;
538}
539#endif
540
541#ifdef CONFIG_FUNCTION_GRAPH_TRACER
542
543#ifdef CONFIG_DYNAMIC_FTRACE
544extern void ftrace_graph_call(void);
545extern void ftrace_graph_stub(void);
546
547int ftrace_enable_ftrace_graph_caller(void)
548{
549 unsigned long ip = (unsigned long)(&ftrace_graph_call);
550 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
551 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
552 unsigned int old, new;
553
554 old = ftrace_call_replace(ip, stub, 0);
555 new = ftrace_call_replace(ip, addr, 0);
556
557 return ftrace_modify_code(ip, old, new);
558}
559
560int ftrace_disable_ftrace_graph_caller(void)
561{
562 unsigned long ip = (unsigned long)(&ftrace_graph_call);
563 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
564 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
565 unsigned int old, new;
566
567 old = ftrace_call_replace(ip, addr, 0);
568 new = ftrace_call_replace(ip, stub, 0);
569
570 return ftrace_modify_code(ip, old, new);
571}
572#endif
573
574#ifdef CONFIG_PPC64
575extern void mod_return_to_handler(void);
576#endif
577
578
579
580
581
582void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
583{
584 unsigned long old;
585 int faulted;
586 struct ftrace_graph_ent trace;
587 unsigned long return_hooker = (unsigned long)&return_to_handler;
588
589 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
590 return;
591
592#ifdef CONFIG_PPC64
593
594 if (REGION_ID(self_addr) != KERNEL_REGION_ID)
595 return_hooker = (unsigned long)&mod_return_to_handler;
596#endif
597
598 return_hooker = ppc_function_entry((void *)return_hooker);
599
600
601
602
603
604
605 asm volatile(
606 "1: " PPC_LL "%[old], 0(%[parent])\n"
607 "2: " PPC_STL "%[return_hooker], 0(%[parent])\n"
608 " li %[faulted], 0\n"
609 "3:\n"
610
611 ".section .fixup, \"ax\"\n"
612 "4: li %[faulted], 1\n"
613 " b 3b\n"
614 ".previous\n"
615
616 ".section __ex_table,\"a\"\n"
617 PPC_LONG_ALIGN "\n"
618 PPC_LONG "1b,4b\n"
619 PPC_LONG "2b,4b\n"
620 ".previous"
621
622 : [old] "=&r" (old), [faulted] "=r" (faulted)
623 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
624 : "memory"
625 );
626
627 if (unlikely(faulted)) {
628 ftrace_graph_stop();
629 WARN_ON(1);
630 return;
631 }
632
633 trace.func = self_addr;
634 trace.depth = current->curr_ret_stack + 1;
635
636
637 if (!ftrace_graph_entry(&trace)) {
638 *parent = old;
639 return;
640 }
641
642 if (ftrace_push_return_trace(old, self_addr, &trace.depth, 0) == -EBUSY)
643 *parent = old;
644}
645#endif
646
647#if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
648unsigned long __init arch_syscall_addr(int nr)
649{
650 return sys_call_table[nr*2];
651}
652#endif
653