blob: d816e714f2f489911d95c9092e4b04fe031c64e7 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Code for replacing ftrace calls with jumps.
4 *
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 *
7 * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
8 *
9 * Added function graph tracer code, taken from x86 that was written
10 * by Frederic Weisbecker, and ported to PPC by Steven Rostedt.
11 *
12 */
13
14#define pr_fmt(fmt) "ftrace-powerpc: " fmt
15
16#include <linux/spinlock.h>
17#include <linux/hardirq.h>
18#include <linux/uaccess.h>
19#include <linux/module.h>
20#include <linux/ftrace.h>
21#include <linux/percpu.h>
22#include <linux/init.h>
23#include <linux/list.h>
24
25#include <asm/asm-prototypes.h>
26#include <asm/cacheflush.h>
27#include <asm/code-patching.h>
28#include <asm/ftrace.h>
29#include <asm/syscall.h>
30
31
32#ifdef CONFIG_DYNAMIC_FTRACE
33
34/*
35 * We generally only have a single long_branch tramp and at most 2 or 3 plt
36 * tramps generated. But, we don't use the plt tramps currently. We also allot
37 * 2 tramps after .text and .init.text. So, we only end up with around 3 usable
38 * tramps in total. Set aside 8 just to be sure.
39 */
40#define NUM_FTRACE_TRAMPS 8
41static unsigned long ftrace_tramps[NUM_FTRACE_TRAMPS];
42
43static unsigned int
44ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
45{
46 unsigned int op;
47
48 addr = ppc_function_entry((void *)addr);
49
50 /* if (link) set op to 'bl' else 'b' */
51 op = create_branch((unsigned int *)ip, addr, link ? 1 : 0);
52
53 return op;
54}
55
56static int
57ftrace_modify_code(unsigned long ip, unsigned int old, unsigned int new)
58{
59 unsigned int replaced;
60
61 /*
62 * Note:
63 * We are paranoid about modifying text, as if a bug was to happen, it
64 * could cause us to read or write to someplace that could cause harm.
65 * Carefully read and modify the code with probe_kernel_*(), and make
66 * sure what we read is what we expected it to be before modifying it.
67 */
68
69 /* read the text we want to modify */
70 if (probe_kernel_read(&replaced, (void *)ip, MCOUNT_INSN_SIZE))
71 return -EFAULT;
72
73 /* Make sure it is what we expect it to be */
74 if (replaced != old) {
75 pr_err("%p: replaced (%#x) != old (%#x)",
76 (void *)ip, replaced, old);
77 return -EINVAL;
78 }
79
80 /* replace the text with the new text */
81 if (patch_instruction((unsigned int *)ip, new))
82 return -EPERM;
83
84 return 0;
85}
86
87/*
88 * Helper functions that are the same for both PPC64 and PPC32.
89 */
90static int test_24bit_addr(unsigned long ip, unsigned long addr)
91{
92 addr = ppc_function_entry((void *)addr);
93
94 /* use the create_branch to verify that this offset can be branched */
95 return create_branch((unsigned int *)ip, addr, 0);
96}
97
98static int is_bl_op(unsigned int op)
99{
100 return (op & 0xfc000003) == 0x48000001;
101}
102
103static int is_b_op(unsigned int op)
104{
105 return (op & 0xfc000003) == 0x48000000;
106}
107
108static unsigned long find_bl_target(unsigned long ip, unsigned int op)
109{
110 int offset;
111
112 offset = (op & 0x03fffffc);
113 /* make it signed */
114 if (offset & 0x02000000)
115 offset |= 0xfe000000;
116
117 return ip + (long)offset;
118}
119
120#ifdef CONFIG_MODULES
121#ifdef CONFIG_PPC64
122static int
123__ftrace_make_nop(struct module *mod,
124 struct dyn_ftrace *rec, unsigned long addr)
125{
126 unsigned long entry, ptr, tramp;
127 unsigned long ip = rec->ip;
128 unsigned int op, pop;
129
130 /* read where this goes */
131 if (probe_kernel_read(&op, (void *)ip, sizeof(int))) {
132 pr_err("Fetching opcode failed.\n");
133 return -EFAULT;
134 }
135
136 /* Make sure that that this is still a 24bit jump */
137 if (!is_bl_op(op)) {
138 pr_err("Not expected bl: opcode is %x\n", op);
139 return -EINVAL;
140 }
141
142 /* lets find where the pointer goes */
143 tramp = find_bl_target(ip, op);
144
145 pr_devel("ip:%lx jumps to %lx", ip, tramp);
146
147 if (module_trampoline_target(mod, tramp, &ptr)) {
148 pr_err("Failed to get trampoline target\n");
149 return -EFAULT;
150 }
151
152 pr_devel("trampoline target %lx", ptr);
153
154 entry = ppc_global_function_entry((void *)addr);
155 /* This should match what was called */
156 if (ptr != entry) {
157 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
158 return -EINVAL;
159 }
160
161#ifdef CONFIG_MPROFILE_KERNEL
162 /* When using -mkernel_profile there is no load to jump over */
163 pop = PPC_INST_NOP;
164
165 if (probe_kernel_read(&op, (void *)(ip - 4), 4)) {
166 pr_err("Fetching instruction at %lx failed.\n", ip - 4);
167 return -EFAULT;
168 }
169
170 /* We expect either a mflr r0, or a std r0, LRSAVE(r1) */
171 if (op != PPC_INST_MFLR && op != PPC_INST_STD_LR) {
172 pr_err("Unexpected instruction %08x around bl _mcount\n", op);
173 return -EINVAL;
174 }
175#else
176 /*
177 * Our original call site looks like:
178 *
179 * bl <tramp>
180 * ld r2,XX(r1)
181 *
182 * Milton Miller pointed out that we can not simply nop the branch.
183 * If a task was preempted when calling a trace function, the nops
184 * will remove the way to restore the TOC in r2 and the r2 TOC will
185 * get corrupted.
186 *
187 * Use a b +8 to jump over the load.
188 */
189
190 pop = PPC_INST_BRANCH | 8; /* b +8 */
191
192 /*
193 * Check what is in the next instruction. We can see ld r2,40(r1), but
194 * on first pass after boot we will see mflr r0.
195 */
196 if (probe_kernel_read(&op, (void *)(ip+4), MCOUNT_INSN_SIZE)) {
197 pr_err("Fetching op failed.\n");
198 return -EFAULT;
199 }
200
201 if (op != PPC_INST_LD_TOC) {
202 pr_err("Expected %08x found %08x\n", PPC_INST_LD_TOC, op);
203 return -EINVAL;
204 }
205#endif /* CONFIG_MPROFILE_KERNEL */
206
207 if (patch_instruction((unsigned int *)ip, pop)) {
208 pr_err("Patching NOP failed.\n");
209 return -EPERM;
210 }
211
212 return 0;
213}
214
215#else /* !PPC64 */
216static int
217__ftrace_make_nop(struct module *mod,
218 struct dyn_ftrace *rec, unsigned long addr)
219{
220 unsigned int op;
221 unsigned int jmp[4];
222 unsigned long ip = rec->ip;
223 unsigned long tramp;
224
225 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
226 return -EFAULT;
227
228 /* Make sure that that this is still a 24bit jump */
229 if (!is_bl_op(op)) {
230 pr_err("Not expected bl: opcode is %x\n", op);
231 return -EINVAL;
232 }
233
234 /* lets find where the pointer goes */
235 tramp = find_bl_target(ip, op);
236
237 /*
238 * On PPC32 the trampoline looks like:
239 * 0x3d, 0x80, 0x00, 0x00 lis r12,sym@ha
240 * 0x39, 0x8c, 0x00, 0x00 addi r12,r12,sym@l
241 * 0x7d, 0x89, 0x03, 0xa6 mtctr r12
242 * 0x4e, 0x80, 0x04, 0x20 bctr
243 */
244
245 pr_devel("ip:%lx jumps to %lx", ip, tramp);
246
247 /* Find where the trampoline jumps to */
248 if (probe_kernel_read(jmp, (void *)tramp, sizeof(jmp))) {
249 pr_err("Failed to read %lx\n", tramp);
250 return -EFAULT;
251 }
252
253 pr_devel(" %08x %08x ", jmp[0], jmp[1]);
254
255 /* verify that this is what we expect it to be */
256 if (((jmp[0] & 0xffff0000) != 0x3d800000) ||
257 ((jmp[1] & 0xffff0000) != 0x398c0000) ||
258 (jmp[2] != 0x7d8903a6) ||
259 (jmp[3] != 0x4e800420)) {
260 pr_err("Not a trampoline\n");
261 return -EINVAL;
262 }
263
264 tramp = (jmp[1] & 0xffff) |
265 ((jmp[0] & 0xffff) << 16);
266 if (tramp & 0x8000)
267 tramp -= 0x10000;
268
269 pr_devel(" %lx ", tramp);
270
271 if (tramp != addr) {
272 pr_err("Trampoline location %08lx does not match addr\n",
273 tramp);
274 return -EINVAL;
275 }
276
277 op = PPC_INST_NOP;
278
279 if (patch_instruction((unsigned int *)ip, op))
280 return -EPERM;
281
282 return 0;
283}
284#endif /* PPC64 */
285#endif /* CONFIG_MODULES */
286
287static unsigned long find_ftrace_tramp(unsigned long ip)
288{
289 int i;
290
291 /*
292 * We have the compiler generated long_branch tramps at the end
293 * and we prefer those
294 */
295 for (i = NUM_FTRACE_TRAMPS - 1; i >= 0; i--)
296 if (!ftrace_tramps[i])
297 continue;
298 else if (create_branch((void *)ip, ftrace_tramps[i], 0))
299 return ftrace_tramps[i];
300
301 return 0;
302}
303
304static int add_ftrace_tramp(unsigned long tramp)
305{
306 int i;
307
308 for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
309 if (!ftrace_tramps[i]) {
310 ftrace_tramps[i] = tramp;
311 return 0;
312 }
313
314 return -1;
315}
316
317/*
318 * If this is a compiler generated long_branch trampoline (essentially, a
319 * trampoline that has a branch to _mcount()), we re-write the branch to
320 * instead go to ftrace_[regs_]caller() and note down the location of this
321 * trampoline.
322 */
323static int setup_mcount_compiler_tramp(unsigned long tramp)
324{
325 int i, op;
326 unsigned long ptr;
327 static unsigned long ftrace_plt_tramps[NUM_FTRACE_TRAMPS];
328
329 /* Is this a known long jump tramp? */
330 for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
331 if (ftrace_tramps[i] == tramp)
332 return 0;
333
334 /* Is this a known plt tramp? */
335 for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
336 if (!ftrace_plt_tramps[i])
337 break;
338 else if (ftrace_plt_tramps[i] == tramp)
339 return -1;
340
341 /* New trampoline -- read where this goes */
342 if (probe_kernel_read(&op, (void *)tramp, sizeof(int))) {
343 pr_debug("Fetching opcode failed.\n");
344 return -1;
345 }
346
347 /* Is this a 24 bit branch? */
348 if (!is_b_op(op)) {
349 pr_debug("Trampoline is not a long branch tramp.\n");
350 return -1;
351 }
352
353 /* lets find where the pointer goes */
354 ptr = find_bl_target(tramp, op);
355
356 if (ptr != ppc_global_function_entry((void *)_mcount)) {
357 pr_debug("Trampoline target %p is not _mcount\n", (void *)ptr);
358 return -1;
359 }
360
361 /* Let's re-write the tramp to go to ftrace_[regs_]caller */
362#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
363 ptr = ppc_global_function_entry((void *)ftrace_regs_caller);
364#else
365 ptr = ppc_global_function_entry((void *)ftrace_caller);
366#endif
367 if (!create_branch((void *)tramp, ptr, 0)) {
368 pr_debug("%ps is not reachable from existing mcount tramp\n",
369 (void *)ptr);
370 return -1;
371 }
372
373 if (patch_branch((unsigned int *)tramp, ptr, 0)) {
374 pr_debug("REL24 out of range!\n");
375 return -1;
376 }
377
378 if (add_ftrace_tramp(tramp)) {
379 pr_debug("No tramp locations left\n");
380 return -1;
381 }
382
383 return 0;
384}
385
386static int __ftrace_make_nop_kernel(struct dyn_ftrace *rec, unsigned long addr)
387{
388 unsigned long tramp, ip = rec->ip;
389 unsigned int op;
390
391 /* Read where this goes */
392 if (probe_kernel_read(&op, (void *)ip, sizeof(int))) {
393 pr_err("Fetching opcode failed.\n");
394 return -EFAULT;
395 }
396
397 /* Make sure that that this is still a 24bit jump */
398 if (!is_bl_op(op)) {
399 pr_err("Not expected bl: opcode is %x\n", op);
400 return -EINVAL;
401 }
402
403 /* Let's find where the pointer goes */
404 tramp = find_bl_target(ip, op);
405
406 pr_devel("ip:%lx jumps to %lx", ip, tramp);
407
408 if (setup_mcount_compiler_tramp(tramp)) {
409 /* Are other trampolines reachable? */
410 if (!find_ftrace_tramp(ip)) {
411 pr_err("No ftrace trampolines reachable from %ps\n",
412 (void *)ip);
413 return -EINVAL;
414 }
415 }
416
417 if (patch_instruction((unsigned int *)ip, PPC_INST_NOP)) {
418 pr_err("Patching NOP failed.\n");
419 return -EPERM;
420 }
421
422 return 0;
423}
424
425int ftrace_make_nop(struct module *mod,
426 struct dyn_ftrace *rec, unsigned long addr)
427{
428 unsigned long ip = rec->ip;
429 unsigned int old, new;
430
431 /*
432 * If the calling address is more that 24 bits away,
433 * then we had to use a trampoline to make the call.
434 * Otherwise just update the call site.
435 */
436 if (test_24bit_addr(ip, addr)) {
437 /* within range */
438 old = ftrace_call_replace(ip, addr, 1);
439 new = PPC_INST_NOP;
440 return ftrace_modify_code(ip, old, new);
441 } else if (core_kernel_text(ip))
442 return __ftrace_make_nop_kernel(rec, addr);
443
444#ifdef CONFIG_MODULES
445 /*
446 * Out of range jumps are called from modules.
447 * We should either already have a pointer to the module
448 * or it has been passed in.
449 */
450 if (!rec->arch.mod) {
451 if (!mod) {
452 pr_err("No module loaded addr=%lx\n", addr);
453 return -EFAULT;
454 }
455 rec->arch.mod = mod;
456 } else if (mod) {
457 if (mod != rec->arch.mod) {
458 pr_err("Record mod %p not equal to passed in mod %p\n",
459 rec->arch.mod, mod);
460 return -EINVAL;
461 }
462 /* nothing to do if mod == rec->arch.mod */
463 } else
464 mod = rec->arch.mod;
465
466 return __ftrace_make_nop(mod, rec, addr);
467#else
468 /* We should not get here without modules */
469 return -EINVAL;
470#endif /* CONFIG_MODULES */
471}
472
473#ifdef CONFIG_MODULES
474#ifdef CONFIG_PPC64
475/*
476 * Examine the existing instructions for __ftrace_make_call.
477 * They should effectively be a NOP, and follow formal constraints,
478 * depending on the ABI. Return false if they don't.
479 */
480#ifndef CONFIG_MPROFILE_KERNEL
481static int
482expected_nop_sequence(void *ip, unsigned int op0, unsigned int op1)
483{
484 /*
485 * We expect to see:
486 *
487 * b +8
488 * ld r2,XX(r1)
489 *
490 * The load offset is different depending on the ABI. For simplicity
491 * just mask it out when doing the compare.
492 */
493 if ((op0 != 0x48000008) || ((op1 & 0xffff0000) != 0xe8410000))
494 return 0;
495 return 1;
496}
497#else
498static int
499expected_nop_sequence(void *ip, unsigned int op0, unsigned int op1)
500{
501 /* look for patched "NOP" on ppc64 with -mprofile-kernel */
502 if (op0 != PPC_INST_NOP)
503 return 0;
504 return 1;
505}
506#endif
507
508static int
509__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
510{
511 unsigned int op[2];
512 void *ip = (void *)rec->ip;
513 unsigned long entry, ptr, tramp;
514 struct module *mod = rec->arch.mod;
515
516 /* read where this goes */
517 if (probe_kernel_read(op, ip, sizeof(op)))
518 return -EFAULT;
519
520 if (!expected_nop_sequence(ip, op[0], op[1])) {
521 pr_err("Unexpected call sequence at %p: %x %x\n",
522 ip, op[0], op[1]);
523 return -EINVAL;
524 }
525
526 /* If we never set up ftrace trampoline(s), then bail */
527#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
528 if (!mod->arch.tramp || !mod->arch.tramp_regs) {
529#else
530 if (!mod->arch.tramp) {
531#endif
532 pr_err("No ftrace trampoline\n");
533 return -EINVAL;
534 }
535
536#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
537 if (rec->flags & FTRACE_FL_REGS)
538 tramp = mod->arch.tramp_regs;
539 else
540#endif
541 tramp = mod->arch.tramp;
542
543 if (module_trampoline_target(mod, tramp, &ptr)) {
544 pr_err("Failed to get trampoline target\n");
545 return -EFAULT;
546 }
547
548 pr_devel("trampoline target %lx", ptr);
549
550 entry = ppc_global_function_entry((void *)addr);
551 /* This should match what was called */
552 if (ptr != entry) {
553 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
554 return -EINVAL;
555 }
556
557 /* Ensure branch is within 24 bits */
558 if (!create_branch(ip, tramp, BRANCH_SET_LINK)) {
559 pr_err("Branch out of range\n");
560 return -EINVAL;
561 }
562
563 if (patch_branch(ip, tramp, BRANCH_SET_LINK)) {
564 pr_err("REL24 out of range!\n");
565 return -EINVAL;
566 }
567
568 return 0;
569}
570
571#else /* !CONFIG_PPC64: */
572static int
573__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
574{
575 unsigned int op;
576 unsigned long ip = rec->ip;
577
578 /* read where this goes */
579 if (probe_kernel_read(&op, (void *)ip, MCOUNT_INSN_SIZE))
580 return -EFAULT;
581
582 /* It should be pointing to a nop */
583 if (op != PPC_INST_NOP) {
584 pr_err("Expected NOP but have %x\n", op);
585 return -EINVAL;
586 }
587
588 /* If we never set up a trampoline to ftrace_caller, then bail */
589 if (!rec->arch.mod->arch.tramp) {
590 pr_err("No ftrace trampoline\n");
591 return -EINVAL;
592 }
593
594 /* create the branch to the trampoline */
595 op = create_branch((unsigned int *)ip,
596 rec->arch.mod->arch.tramp, BRANCH_SET_LINK);
597 if (!op) {
598 pr_err("REL24 out of range!\n");
599 return -EINVAL;
600 }
601
602 pr_devel("write to %lx\n", rec->ip);
603
604 if (patch_instruction((unsigned int *)ip, op))
605 return -EPERM;
606
607 return 0;
608}
609#endif /* CONFIG_PPC64 */
610#endif /* CONFIG_MODULES */
611
612static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr)
613{
614 unsigned int op;
615 void *ip = (void *)rec->ip;
616 unsigned long tramp, entry, ptr;
617
618 /* Make sure we're being asked to patch branch to a known ftrace addr */
619 entry = ppc_global_function_entry((void *)ftrace_caller);
620 ptr = ppc_global_function_entry((void *)addr);
621
622 if (ptr != entry) {
623#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
624 entry = ppc_global_function_entry((void *)ftrace_regs_caller);
625 if (ptr != entry) {
626#endif
627 pr_err("Unknown ftrace addr to patch: %ps\n", (void *)ptr);
628 return -EINVAL;
629#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
630 }
631#endif
632 }
633
634 /* Make sure we have a nop */
635 if (probe_kernel_read(&op, ip, sizeof(op))) {
636 pr_err("Unable to read ftrace location %p\n", ip);
637 return -EFAULT;
638 }
639
640 if (op != PPC_INST_NOP) {
641 pr_err("Unexpected call sequence at %p: %x\n", ip, op);
642 return -EINVAL;
643 }
644
645 tramp = find_ftrace_tramp((unsigned long)ip);
646 if (!tramp) {
647 pr_err("No ftrace trampolines reachable from %ps\n", ip);
648 return -EINVAL;
649 }
650
651 if (patch_branch(ip, tramp, BRANCH_SET_LINK)) {
652 pr_err("Error patching branch to ftrace tramp!\n");
653 return -EINVAL;
654 }
655
656 return 0;
657}
658
659int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
660{
661 unsigned long ip = rec->ip;
662 unsigned int old, new;
663
664 /*
665 * If the calling address is more that 24 bits away,
666 * then we had to use a trampoline to make the call.
667 * Otherwise just update the call site.
668 */
669 if (test_24bit_addr(ip, addr)) {
670 /* within range */
671 old = PPC_INST_NOP;
672 new = ftrace_call_replace(ip, addr, 1);
673 return ftrace_modify_code(ip, old, new);
674 } else if (core_kernel_text(ip))
675 return __ftrace_make_call_kernel(rec, addr);
676
677#ifdef CONFIG_MODULES
678 /*
679 * Out of range jumps are called from modules.
680 * Being that we are converting from nop, it had better
681 * already have a module defined.
682 */
683 if (!rec->arch.mod) {
684 pr_err("No module loaded\n");
685 return -EINVAL;
686 }
687
688 return __ftrace_make_call(rec, addr);
689#else
690 /* We should not get here without modules */
691 return -EINVAL;
692#endif /* CONFIG_MODULES */
693}
694
695#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
696#ifdef CONFIG_MODULES
697static int
698__ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
699 unsigned long addr)
700{
701 unsigned int op;
702 unsigned long ip = rec->ip;
703 unsigned long entry, ptr, tramp;
704 struct module *mod = rec->arch.mod;
705
706 /* If we never set up ftrace trampolines, then bail */
707 if (!mod->arch.tramp || !mod->arch.tramp_regs) {
708 pr_err("No ftrace trampoline\n");
709 return -EINVAL;
710 }
711
712 /* read where this goes */
713 if (probe_kernel_read(&op, (void *)ip, sizeof(int))) {
714 pr_err("Fetching opcode failed.\n");
715 return -EFAULT;
716 }
717
718 /* Make sure that that this is still a 24bit jump */
719 if (!is_bl_op(op)) {
720 pr_err("Not expected bl: opcode is %x\n", op);
721 return -EINVAL;
722 }
723
724 /* lets find where the pointer goes */
725 tramp = find_bl_target(ip, op);
726 entry = ppc_global_function_entry((void *)old_addr);
727
728 pr_devel("ip:%lx jumps to %lx", ip, tramp);
729
730 if (tramp != entry) {
731 /* old_addr is not within range, so we must have used a trampoline */
732 if (module_trampoline_target(mod, tramp, &ptr)) {
733 pr_err("Failed to get trampoline target\n");
734 return -EFAULT;
735 }
736
737 pr_devel("trampoline target %lx", ptr);
738
739 /* This should match what was called */
740 if (ptr != entry) {
741 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
742 return -EINVAL;
743 }
744 }
745
746 /* The new target may be within range */
747 if (test_24bit_addr(ip, addr)) {
748 /* within range */
749 if (patch_branch((unsigned int *)ip, addr, BRANCH_SET_LINK)) {
750 pr_err("REL24 out of range!\n");
751 return -EINVAL;
752 }
753
754 return 0;
755 }
756
757 if (rec->flags & FTRACE_FL_REGS)
758 tramp = mod->arch.tramp_regs;
759 else
760 tramp = mod->arch.tramp;
761
762 if (module_trampoline_target(mod, tramp, &ptr)) {
763 pr_err("Failed to get trampoline target\n");
764 return -EFAULT;
765 }
766
767 pr_devel("trampoline target %lx", ptr);
768
769 entry = ppc_global_function_entry((void *)addr);
770 /* This should match what was called */
771 if (ptr != entry) {
772 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
773 return -EINVAL;
774 }
775
776 /* Ensure branch is within 24 bits */
777 if (!create_branch((unsigned int *)ip, tramp, BRANCH_SET_LINK)) {
778 pr_err("Branch out of range\n");
779 return -EINVAL;
780 }
781
782 if (patch_branch((unsigned int *)ip, tramp, BRANCH_SET_LINK)) {
783 pr_err("REL24 out of range!\n");
784 return -EINVAL;
785 }
786
787 return 0;
788}
789#endif
790
791int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
792 unsigned long addr)
793{
794 unsigned long ip = rec->ip;
795 unsigned int old, new;
796
797 /*
798 * If the calling address is more that 24 bits away,
799 * then we had to use a trampoline to make the call.
800 * Otherwise just update the call site.
801 */
802 if (test_24bit_addr(ip, addr) && test_24bit_addr(ip, old_addr)) {
803 /* within range */
804 old = ftrace_call_replace(ip, old_addr, 1);
805 new = ftrace_call_replace(ip, addr, 1);
806 return ftrace_modify_code(ip, old, new);
807 } else if (core_kernel_text(ip)) {
808 /*
809 * We always patch out of range locations to go to the regs
810 * variant, so there is nothing to do here
811 */
812 return 0;
813 }
814
815#ifdef CONFIG_MODULES
816 /*
817 * Out of range jumps are called from modules.
818 */
819 if (!rec->arch.mod) {
820 pr_err("No module loaded\n");
821 return -EINVAL;
822 }
823
824 return __ftrace_modify_call(rec, old_addr, addr);
825#else
826 /* We should not get here without modules */
827 return -EINVAL;
828#endif /* CONFIG_MODULES */
829}
830#endif
831
832int ftrace_update_ftrace_func(ftrace_func_t func)
833{
834 unsigned long ip = (unsigned long)(&ftrace_call);
835 unsigned int old, new;
836 int ret;
837
838 old = *(unsigned int *)&ftrace_call;
839 new = ftrace_call_replace(ip, (unsigned long)func, 1);
840 ret = ftrace_modify_code(ip, old, new);
841
842#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
843 /* Also update the regs callback function */
844 if (!ret) {
845 ip = (unsigned long)(&ftrace_regs_call);
846 old = *(unsigned int *)&ftrace_regs_call;
847 new = ftrace_call_replace(ip, (unsigned long)func, 1);
848 ret = ftrace_modify_code(ip, old, new);
849 }
850#endif
851
852 return ret;
853}
854
855/*
856 * Use the default ftrace_modify_all_code, but without
857 * stop_machine().
858 */
859void arch_ftrace_update_code(int command)
860{
861 ftrace_modify_all_code(command);
862}
863
864#ifdef CONFIG_PPC64
865#define PACATOC offsetof(struct paca_struct, kernel_toc)
866
867extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[];
868
869void ftrace_free_init_tramp(void)
870{
871 int i;
872
873 for (i = 0; i < NUM_FTRACE_TRAMPS && ftrace_tramps[i]; i++)
874 if (ftrace_tramps[i] == (unsigned long)ftrace_tramp_init) {
875 ftrace_tramps[i] = 0;
876 return;
877 }
878}
879
880int __init ftrace_dyn_arch_init(void)
881{
882 int i;
883 unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init };
884 u32 stub_insns[] = {
885 0xe98d0000 | PACATOC, /* ld r12,PACATOC(r13) */
886 0x3d8c0000, /* addis r12,r12,<high> */
887 0x398c0000, /* addi r12,r12,<low> */
888 0x7d8903a6, /* mtctr r12 */
889 0x4e800420, /* bctr */
890 };
891#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
892 unsigned long addr = ppc_global_function_entry((void *)ftrace_regs_caller);
893#else
894 unsigned long addr = ppc_global_function_entry((void *)ftrace_caller);
895#endif
896 long reladdr = addr - kernel_toc_addr();
897
898 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
899 pr_err("Address of %ps out of range of kernel_toc.\n",
900 (void *)addr);
901 return -1;
902 }
903
904 for (i = 0; i < 2; i++) {
905 memcpy(tramp[i], stub_insns, sizeof(stub_insns));
906 tramp[i][1] |= PPC_HA(reladdr);
907 tramp[i][2] |= PPC_LO(reladdr);
908 add_ftrace_tramp((unsigned long)tramp[i]);
909 }
910
911 return 0;
912}
913#else
914int __init ftrace_dyn_arch_init(void)
915{
916 return 0;
917}
918#endif
919#endif /* CONFIG_DYNAMIC_FTRACE */
920
921#ifdef CONFIG_FUNCTION_GRAPH_TRACER
922
923extern void ftrace_graph_call(void);
924extern void ftrace_graph_stub(void);
925
926int ftrace_enable_ftrace_graph_caller(void)
927{
928 unsigned long ip = (unsigned long)(&ftrace_graph_call);
929 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
930 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
931 unsigned int old, new;
932
933 old = ftrace_call_replace(ip, stub, 0);
934 new = ftrace_call_replace(ip, addr, 0);
935
936 return ftrace_modify_code(ip, old, new);
937}
938
939int ftrace_disable_ftrace_graph_caller(void)
940{
941 unsigned long ip = (unsigned long)(&ftrace_graph_call);
942 unsigned long addr = (unsigned long)(&ftrace_graph_caller);
943 unsigned long stub = (unsigned long)(&ftrace_graph_stub);
944 unsigned int old, new;
945
946 old = ftrace_call_replace(ip, addr, 0);
947 new = ftrace_call_replace(ip, stub, 0);
948
949 return ftrace_modify_code(ip, old, new);
950}
951
952/*
953 * Hook the return address and push it in the stack of return addrs
954 * in current thread info. Return the address we want to divert to.
955 */
956unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,
957 unsigned long sp)
958{
959 unsigned long return_hooker;
960
961 if (unlikely(ftrace_graph_is_dead()))
962 goto out;
963
964 if (unlikely(atomic_read(&current->tracing_graph_pause)))
965 goto out;
966
967 return_hooker = ppc_function_entry(return_to_handler);
968
969 if (!function_graph_enter(parent, ip, 0, (unsigned long *)sp))
970 parent = return_hooker;
971out:
972 return parent;
973}
974#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
975
976#ifdef PPC64_ELF_ABI_v1
977char *arch_ftrace_match_adjust(char *str, const char *search)
978{
979 if (str[0] == '.' && search[0] != '.')
980 return str + 1;
981 else
982 return str;
983}
984#endif /* PPC64_ELF_ABI_v1 */