powerpc/ftrace: Minimise number of #ifdefs
[linux-2.6-block.git] / arch / powerpc / kernel / trace / ftrace.c
1 // 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/cacheflush.h>
26 #include <asm/code-patching.h>
27 #include <asm/ftrace.h>
28 #include <asm/syscall.h>
29 #include <asm/inst.h>
30
31 /*
32  * We generally only have a single long_branch tramp and at most 2 or 3 plt
33  * tramps generated. But, we don't use the plt tramps currently. We also allot
34  * 2 tramps after .text and .init.text. So, we only end up with around 3 usable
35  * tramps in total. Set aside 8 just to be sure.
36  */
37 #define NUM_FTRACE_TRAMPS       8
38 static unsigned long ftrace_tramps[NUM_FTRACE_TRAMPS];
39
40 static ppc_inst_t
41 ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
42 {
43         ppc_inst_t op;
44
45         addr = ppc_function_entry((void *)addr);
46
47         /* if (link) set op to 'bl' else 'b' */
48         create_branch(&op, (u32 *)ip, addr, link ? BRANCH_SET_LINK : 0);
49
50         return op;
51 }
52
53 static inline int
54 ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
55 {
56         ppc_inst_t replaced;
57
58         /*
59          * Note:
60          * We are paranoid about modifying text, as if a bug was to happen, it
61          * could cause us to read or write to someplace that could cause harm.
62          * Carefully read and modify the code with probe_kernel_*(), and make
63          * sure what we read is what we expected it to be before modifying it.
64          */
65
66         /* read the text we want to modify */
67         if (copy_inst_from_kernel_nofault(&replaced, (void *)ip))
68                 return -EFAULT;
69
70         /* Make sure it is what we expect it to be */
71         if (!ppc_inst_equal(replaced, old)) {
72                 pr_err("%p: replaced (%s) != old (%s)",
73                 (void *)ip, ppc_inst_as_str(replaced), ppc_inst_as_str(old));
74                 return -EINVAL;
75         }
76
77         /* replace the text with the new text */
78         return patch_instruction((u32 *)ip, new);
79 }
80
81 /*
82  * Helper functions that are the same for both PPC64 and PPC32.
83  */
84 static int test_24bit_addr(unsigned long ip, unsigned long addr)
85 {
86         addr = ppc_function_entry((void *)addr);
87
88         return is_offset_in_branch_range(addr - ip);
89 }
90
91 static int is_bl_op(ppc_inst_t op)
92 {
93         return (ppc_inst_val(op) & ~PPC_LI_MASK) == PPC_RAW_BL(0);
94 }
95
96 static int is_b_op(ppc_inst_t op)
97 {
98         return (ppc_inst_val(op) & ~PPC_LI_MASK) == PPC_RAW_BRANCH(0);
99 }
100
101 static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
102 {
103         int offset;
104
105         offset = PPC_LI(ppc_inst_val(op));
106         /* make it signed */
107         if (offset & 0x02000000)
108                 offset |= 0xfe000000;
109
110         return ip + (long)offset;
111 }
112
113 #ifdef CONFIG_MODULES
114 static int
115 __ftrace_make_nop(struct module *mod,
116                   struct dyn_ftrace *rec, unsigned long addr)
117 {
118         unsigned long entry, ptr, tramp;
119         unsigned long ip = rec->ip;
120         ppc_inst_t op, pop;
121
122         /* read where this goes */
123         if (copy_inst_from_kernel_nofault(&op, (void *)ip)) {
124                 pr_err("Fetching opcode failed.\n");
125                 return -EFAULT;
126         }
127
128         /* Make sure that that this is still a 24bit jump */
129         if (!is_bl_op(op)) {
130                 pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
131                 return -EINVAL;
132         }
133
134         /* lets find where the pointer goes */
135         tramp = find_bl_target(ip, op);
136
137         pr_devel("ip:%lx jumps to %lx", ip, tramp);
138
139         if (module_trampoline_target(mod, tramp, &ptr)) {
140                 pr_err("Failed to get trampoline target\n");
141                 return -EFAULT;
142         }
143
144         pr_devel("trampoline target %lx", ptr);
145
146         entry = ppc_global_function_entry((void *)addr);
147         /* This should match what was called */
148         if (ptr != entry) {
149                 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
150                 return -EINVAL;
151         }
152
153         if (IS_ENABLED(CONFIG_MPROFILE_KERNEL)) {
154                 if (copy_inst_from_kernel_nofault(&op, (void *)(ip - 4))) {
155                         pr_err("Fetching instruction at %lx failed.\n", ip - 4);
156                         return -EFAULT;
157                 }
158
159                 /* We expect either a mflr r0, or a std r0, LRSAVE(r1) */
160                 if (!ppc_inst_equal(op, ppc_inst(PPC_RAW_MFLR(_R0))) &&
161                     !ppc_inst_equal(op, ppc_inst(PPC_INST_STD_LR))) {
162                         pr_err("Unexpected instruction %s around bl _mcount\n",
163                                ppc_inst_as_str(op));
164                         return -EINVAL;
165                 }
166         } else if (IS_ENABLED(CONFIG_PPC64)) {
167                 /*
168                  * Check what is in the next instruction. We can see ld r2,40(r1), but
169                  * on first pass after boot we will see mflr r0.
170                  */
171                 if (copy_inst_from_kernel_nofault(&op, (void *)(ip + 4))) {
172                         pr_err("Fetching op failed.\n");
173                         return -EFAULT;
174                 }
175
176                 if (!ppc_inst_equal(op,  ppc_inst(PPC_INST_LD_TOC))) {
177                         pr_err("Expected %08lx found %s\n", PPC_INST_LD_TOC, ppc_inst_as_str(op));
178                         return -EINVAL;
179                 }
180         }
181
182         /*
183          * When using -mprofile-kernel or PPC32 there is no load to jump over.
184          *
185          * Otherwise our original call site looks like:
186          *
187          * bl <tramp>
188          * ld r2,XX(r1)
189          *
190          * Milton Miller pointed out that we can not simply nop the branch.
191          * If a task was preempted when calling a trace function, the nops
192          * will remove the way to restore the TOC in r2 and the r2 TOC will
193          * get corrupted.
194          *
195          * Use a b +8 to jump over the load.
196          */
197         if (IS_ENABLED(CONFIG_MPROFILE_KERNEL) || IS_ENABLED(CONFIG_PPC32))
198                 pop = ppc_inst(PPC_RAW_NOP());
199         else
200                 pop = ppc_inst(PPC_RAW_BRANCH(8));      /* b +8 */
201
202         if (patch_instruction((u32 *)ip, pop)) {
203                 pr_err("Patching NOP failed.\n");
204                 return -EPERM;
205         }
206
207         return 0;
208 }
209 #else
210 static int __ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
211 {
212         return 0;
213 }
214 #endif /* CONFIG_MODULES */
215
216 static unsigned long find_ftrace_tramp(unsigned long ip)
217 {
218         int i;
219
220         /*
221          * We have the compiler generated long_branch tramps at the end
222          * and we prefer those
223          */
224         for (i = NUM_FTRACE_TRAMPS - 1; i >= 0; i--)
225                 if (!ftrace_tramps[i])
226                         continue;
227                 else if (is_offset_in_branch_range(ftrace_tramps[i] - ip))
228                         return ftrace_tramps[i];
229
230         return 0;
231 }
232
233 static int add_ftrace_tramp(unsigned long tramp)
234 {
235         int i;
236
237         for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
238                 if (!ftrace_tramps[i]) {
239                         ftrace_tramps[i] = tramp;
240                         return 0;
241                 }
242
243         return -1;
244 }
245
246 /*
247  * If this is a compiler generated long_branch trampoline (essentially, a
248  * trampoline that has a branch to _mcount()), we re-write the branch to
249  * instead go to ftrace_[regs_]caller() and note down the location of this
250  * trampoline.
251  */
252 static int setup_mcount_compiler_tramp(unsigned long tramp)
253 {
254         int i;
255         ppc_inst_t op;
256         unsigned long ptr;
257
258         /* Is this a known long jump tramp? */
259         for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
260                 if (!ftrace_tramps[i])
261                         break;
262                 else if (ftrace_tramps[i] == tramp)
263                         return 0;
264
265         /* New trampoline -- read where this goes */
266         if (copy_inst_from_kernel_nofault(&op, (void *)tramp)) {
267                 pr_debug("Fetching opcode failed.\n");
268                 return -1;
269         }
270
271         /* Is this a 24 bit branch? */
272         if (!is_b_op(op)) {
273                 pr_debug("Trampoline is not a long branch tramp.\n");
274                 return -1;
275         }
276
277         /* lets find where the pointer goes */
278         ptr = find_bl_target(tramp, op);
279
280         if (ptr != ppc_global_function_entry((void *)_mcount)) {
281                 pr_debug("Trampoline target %p is not _mcount\n", (void *)ptr);
282                 return -1;
283         }
284
285         /* Let's re-write the tramp to go to ftrace_[regs_]caller */
286         if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
287                 ptr = ppc_global_function_entry((void *)ftrace_regs_caller);
288         else
289                 ptr = ppc_global_function_entry((void *)ftrace_caller);
290
291         if (patch_branch((u32 *)tramp, ptr, 0)) {
292                 pr_debug("REL24 out of range!\n");
293                 return -1;
294         }
295
296         if (add_ftrace_tramp(tramp)) {
297                 pr_debug("No tramp locations left\n");
298                 return -1;
299         }
300
301         return 0;
302 }
303
304 static int __ftrace_make_nop_kernel(struct dyn_ftrace *rec, unsigned long addr)
305 {
306         unsigned long tramp, ip = rec->ip;
307         ppc_inst_t op;
308
309         /* Read where this goes */
310         if (copy_inst_from_kernel_nofault(&op, (void *)ip)) {
311                 pr_err("Fetching opcode failed.\n");
312                 return -EFAULT;
313         }
314
315         /* Make sure that that this is still a 24bit jump */
316         if (!is_bl_op(op)) {
317                 pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
318                 return -EINVAL;
319         }
320
321         /* Let's find where the pointer goes */
322         tramp = find_bl_target(ip, op);
323
324         pr_devel("ip:%lx jumps to %lx", ip, tramp);
325
326         if (setup_mcount_compiler_tramp(tramp)) {
327                 /* Are other trampolines reachable? */
328                 if (!find_ftrace_tramp(ip)) {
329                         pr_err("No ftrace trampolines reachable from %ps\n",
330                                         (void *)ip);
331                         return -EINVAL;
332                 }
333         }
334
335         if (patch_instruction((u32 *)ip, ppc_inst(PPC_RAW_NOP()))) {
336                 pr_err("Patching NOP failed.\n");
337                 return -EPERM;
338         }
339
340         return 0;
341 }
342
343 int ftrace_make_nop(struct module *mod,
344                     struct dyn_ftrace *rec, unsigned long addr)
345 {
346         unsigned long ip = rec->ip;
347         ppc_inst_t old, new;
348
349         /*
350          * If the calling address is more that 24 bits away,
351          * then we had to use a trampoline to make the call.
352          * Otherwise just update the call site.
353          */
354         if (test_24bit_addr(ip, addr)) {
355                 /* within range */
356                 old = ftrace_call_replace(ip, addr, 1);
357                 new = ppc_inst(PPC_RAW_NOP());
358                 return ftrace_modify_code(ip, old, new);
359         } else if (core_kernel_text(ip)) {
360                 return __ftrace_make_nop_kernel(rec, addr);
361         } else if (!IS_ENABLED(CONFIG_MODULES)) {
362                 return -EINVAL;
363         }
364
365         /*
366          * Out of range jumps are called from modules.
367          * We should either already have a pointer to the module
368          * or it has been passed in.
369          */
370         if (!rec->arch.mod) {
371                 if (!mod) {
372                         pr_err("No module loaded addr=%lx\n", addr);
373                         return -EFAULT;
374                 }
375                 rec->arch.mod = mod;
376         } else if (mod) {
377                 if (mod != rec->arch.mod) {
378                         pr_err("Record mod %p not equal to passed in mod %p\n",
379                                rec->arch.mod, mod);
380                         return -EINVAL;
381                 }
382                 /* nothing to do if mod == rec->arch.mod */
383         } else
384                 mod = rec->arch.mod;
385
386         return __ftrace_make_nop(mod, rec, addr);
387 }
388
389 #ifdef CONFIG_MODULES
390 /*
391  * Examine the existing instructions for __ftrace_make_call.
392  * They should effectively be a NOP, and follow formal constraints,
393  * depending on the ABI. Return false if they don't.
394  */
395 static bool expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
396 {
397         if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V1))
398                 return ppc_inst_equal(op0, ppc_inst(PPC_RAW_BRANCH(8))) &&
399                        ppc_inst_equal(op1, ppc_inst(PPC_INST_LD_TOC));
400         else
401                 return ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP()));
402 }
403
404 static int
405 __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
406 {
407         ppc_inst_t op[2];
408         void *ip = (void *)rec->ip;
409         unsigned long entry, ptr, tramp;
410         struct module *mod = rec->arch.mod;
411
412         /* read where this goes */
413         if (copy_inst_from_kernel_nofault(op, ip))
414                 return -EFAULT;
415
416         if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V1) &&
417             copy_inst_from_kernel_nofault(op + 1, ip + 4))
418                 return -EFAULT;
419
420         if (!expected_nop_sequence(ip, op[0], op[1])) {
421                 pr_err("Unexpected call sequence at %p: %s %s\n",
422                 ip, ppc_inst_as_str(op[0]), ppc_inst_as_str(op[1]));
423                 return -EINVAL;
424         }
425
426         /* If we never set up ftrace trampoline(s), then bail */
427         if (!mod->arch.tramp ||
428             (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && !mod->arch.tramp_regs)) {
429                 pr_err("No ftrace trampoline\n");
430                 return -EINVAL;
431         }
432
433         if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && rec->flags & FTRACE_FL_REGS)
434                 tramp = mod->arch.tramp_regs;
435         else
436                 tramp = mod->arch.tramp;
437
438         if (module_trampoline_target(mod, tramp, &ptr)) {
439                 pr_err("Failed to get trampoline target\n");
440                 return -EFAULT;
441         }
442
443         pr_devel("trampoline target %lx", ptr);
444
445         entry = ppc_global_function_entry((void *)addr);
446         /* This should match what was called */
447         if (ptr != entry) {
448                 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
449                 return -EINVAL;
450         }
451
452         if (patch_branch(ip, tramp, BRANCH_SET_LINK)) {
453                 pr_err("REL24 out of range!\n");
454                 return -EINVAL;
455         }
456
457         return 0;
458 }
459 #else
460 static int __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
461 {
462         return 0;
463 }
464 #endif /* CONFIG_MODULES */
465
466 static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr)
467 {
468         ppc_inst_t op;
469         void *ip = (void *)rec->ip;
470         unsigned long tramp, entry, ptr;
471
472         /* Make sure we're being asked to patch branch to a known ftrace addr */
473         entry = ppc_global_function_entry((void *)ftrace_caller);
474         ptr = ppc_global_function_entry((void *)addr);
475
476         if (ptr != entry && IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
477                 entry = ppc_global_function_entry((void *)ftrace_regs_caller);
478
479         if (ptr != entry) {
480                 pr_err("Unknown ftrace addr to patch: %ps\n", (void *)ptr);
481                 return -EINVAL;
482         }
483
484         /* Make sure we have a nop */
485         if (copy_inst_from_kernel_nofault(&op, ip)) {
486                 pr_err("Unable to read ftrace location %p\n", ip);
487                 return -EFAULT;
488         }
489
490         if (!ppc_inst_equal(op, ppc_inst(PPC_RAW_NOP()))) {
491                 pr_err("Unexpected call sequence at %p: %s\n", ip, ppc_inst_as_str(op));
492                 return -EINVAL;
493         }
494
495         tramp = find_ftrace_tramp((unsigned long)ip);
496         if (!tramp) {
497                 pr_err("No ftrace trampolines reachable from %ps\n", ip);
498                 return -EINVAL;
499         }
500
501         if (patch_branch(ip, tramp, BRANCH_SET_LINK)) {
502                 pr_err("Error patching branch to ftrace tramp!\n");
503                 return -EINVAL;
504         }
505
506         return 0;
507 }
508
509 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
510 {
511         unsigned long ip = rec->ip;
512         ppc_inst_t old, new;
513
514         /*
515          * If the calling address is more that 24 bits away,
516          * then we had to use a trampoline to make the call.
517          * Otherwise just update the call site.
518          */
519         if (test_24bit_addr(ip, addr)) {
520                 /* within range */
521                 old = ppc_inst(PPC_RAW_NOP());
522                 new = ftrace_call_replace(ip, addr, 1);
523                 return ftrace_modify_code(ip, old, new);
524         } else if (core_kernel_text(ip)) {
525                 return __ftrace_make_call_kernel(rec, addr);
526         } else if (!IS_ENABLED(CONFIG_MODULES)) {
527                 /* We should not get here without modules */
528                 return -EINVAL;
529         }
530
531         /*
532          * Out of range jumps are called from modules.
533          * Being that we are converting from nop, it had better
534          * already have a module defined.
535          */
536         if (!rec->arch.mod) {
537                 pr_err("No module loaded\n");
538                 return -EINVAL;
539         }
540
541         return __ftrace_make_call(rec, addr);
542 }
543
544 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
545 #ifdef CONFIG_MODULES
546 static int
547 __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
548                                         unsigned long addr)
549 {
550         ppc_inst_t op;
551         unsigned long ip = rec->ip;
552         unsigned long entry, ptr, tramp;
553         struct module *mod = rec->arch.mod;
554
555         /* If we never set up ftrace trampolines, then bail */
556         if (!mod->arch.tramp || !mod->arch.tramp_regs) {
557                 pr_err("No ftrace trampoline\n");
558                 return -EINVAL;
559         }
560
561         /* read where this goes */
562         if (copy_inst_from_kernel_nofault(&op, (void *)ip)) {
563                 pr_err("Fetching opcode failed.\n");
564                 return -EFAULT;
565         }
566
567         /* Make sure that that this is still a 24bit jump */
568         if (!is_bl_op(op)) {
569                 pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
570                 return -EINVAL;
571         }
572
573         /* lets find where the pointer goes */
574         tramp = find_bl_target(ip, op);
575         entry = ppc_global_function_entry((void *)old_addr);
576
577         pr_devel("ip:%lx jumps to %lx", ip, tramp);
578
579         if (tramp != entry) {
580                 /* old_addr is not within range, so we must have used a trampoline */
581                 if (module_trampoline_target(mod, tramp, &ptr)) {
582                         pr_err("Failed to get trampoline target\n");
583                         return -EFAULT;
584                 }
585
586                 pr_devel("trampoline target %lx", ptr);
587
588                 /* This should match what was called */
589                 if (ptr != entry) {
590                         pr_err("addr %lx does not match expected %lx\n", ptr, entry);
591                         return -EINVAL;
592                 }
593         }
594
595         /* The new target may be within range */
596         if (test_24bit_addr(ip, addr)) {
597                 /* within range */
598                 if (patch_branch((u32 *)ip, addr, BRANCH_SET_LINK)) {
599                         pr_err("REL24 out of range!\n");
600                         return -EINVAL;
601                 }
602
603                 return 0;
604         }
605
606         if (rec->flags & FTRACE_FL_REGS)
607                 tramp = mod->arch.tramp_regs;
608         else
609                 tramp = mod->arch.tramp;
610
611         if (module_trampoline_target(mod, tramp, &ptr)) {
612                 pr_err("Failed to get trampoline target\n");
613                 return -EFAULT;
614         }
615
616         pr_devel("trampoline target %lx", ptr);
617
618         entry = ppc_global_function_entry((void *)addr);
619         /* This should match what was called */
620         if (ptr != entry) {
621                 pr_err("addr %lx does not match expected %lx\n", ptr, entry);
622                 return -EINVAL;
623         }
624
625         if (patch_branch((u32 *)ip, tramp, BRANCH_SET_LINK)) {
626                 pr_err("REL24 out of range!\n");
627                 return -EINVAL;
628         }
629
630         return 0;
631 }
632 #else
633 static int __ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, unsigned long addr)
634 {
635         return 0;
636 }
637 #endif
638
639 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
640                         unsigned long addr)
641 {
642         unsigned long ip = rec->ip;
643         ppc_inst_t old, new;
644
645         /*
646          * If the calling address is more that 24 bits away,
647          * then we had to use a trampoline to make the call.
648          * Otherwise just update the call site.
649          */
650         if (test_24bit_addr(ip, addr) && test_24bit_addr(ip, old_addr)) {
651                 /* within range */
652                 old = ftrace_call_replace(ip, old_addr, 1);
653                 new = ftrace_call_replace(ip, addr, 1);
654                 return ftrace_modify_code(ip, old, new);
655         } else if (core_kernel_text(ip)) {
656                 /*
657                  * We always patch out of range locations to go to the regs
658                  * variant, so there is nothing to do here
659                  */
660                 return 0;
661         } else if (!IS_ENABLED(CONFIG_MODULES)) {
662                 /* We should not get here without modules */
663                 return -EINVAL;
664         }
665
666         /*
667          * Out of range jumps are called from modules.
668          */
669         if (!rec->arch.mod) {
670                 pr_err("No module loaded\n");
671                 return -EINVAL;
672         }
673
674         return __ftrace_modify_call(rec, old_addr, addr);
675 }
676 #endif
677
678 int ftrace_update_ftrace_func(ftrace_func_t func)
679 {
680         unsigned long ip = (unsigned long)(&ftrace_call);
681         ppc_inst_t old, new;
682         int ret;
683
684         old = ppc_inst_read((u32 *)&ftrace_call);
685         new = ftrace_call_replace(ip, (unsigned long)func, 1);
686         ret = ftrace_modify_code(ip, old, new);
687
688         /* Also update the regs callback function */
689         if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && !ret) {
690                 ip = (unsigned long)(&ftrace_regs_call);
691                 old = ppc_inst_read((u32 *)&ftrace_regs_call);
692                 new = ftrace_call_replace(ip, (unsigned long)func, 1);
693                 ret = ftrace_modify_code(ip, old, new);
694         }
695
696         return ret;
697 }
698
699 /*
700  * Use the default ftrace_modify_all_code, but without
701  * stop_machine().
702  */
703 void arch_ftrace_update_code(int command)
704 {
705         ftrace_modify_all_code(command);
706 }
707
708 #ifdef CONFIG_PPC64
709 #define PACATOC offsetof(struct paca_struct, kernel_toc)
710
711 extern unsigned int ftrace_tramp_text[], ftrace_tramp_init[];
712
713 int __init ftrace_dyn_arch_init(void)
714 {
715         int i;
716         unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init };
717         u32 stub_insns[] = {
718                 PPC_RAW_LD(_R12, _R13, PACATOC),
719                 PPC_RAW_ADDIS(_R12, _R12, 0),
720                 PPC_RAW_ADDI(_R12, _R12, 0),
721                 PPC_RAW_MTCTR(_R12),
722                 PPC_RAW_BCTR()
723         };
724         unsigned long addr;
725         long reladdr;
726
727         if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
728                 addr = ppc_global_function_entry((void *)ftrace_regs_caller);
729         else
730                 addr = ppc_global_function_entry((void *)ftrace_caller);
731
732         reladdr = addr - kernel_toc_addr();
733
734         if (reladdr >= SZ_2G || reladdr < -(long)SZ_2G) {
735                 pr_err("Address of %ps out of range of kernel_toc.\n",
736                                 (void *)addr);
737                 return -1;
738         }
739
740         for (i = 0; i < 2; i++) {
741                 memcpy(tramp[i], stub_insns, sizeof(stub_insns));
742                 tramp[i][1] |= PPC_HA(reladdr);
743                 tramp[i][2] |= PPC_LO(reladdr);
744                 add_ftrace_tramp((unsigned long)tramp[i]);
745         }
746
747         return 0;
748 }
749 #endif
750
751 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
752
753 extern void ftrace_graph_call(void);
754 extern void ftrace_graph_stub(void);
755
756 static int ftrace_modify_ftrace_graph_caller(bool enable)
757 {
758         unsigned long ip = (unsigned long)(&ftrace_graph_call);
759         unsigned long addr = (unsigned long)(&ftrace_graph_caller);
760         unsigned long stub = (unsigned long)(&ftrace_graph_stub);
761         ppc_inst_t old, new;
762
763         if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_ARGS))
764                 return 0;
765
766         old = ftrace_call_replace(ip, enable ? stub : addr, 0);
767         new = ftrace_call_replace(ip, enable ? addr : stub, 0);
768
769         return ftrace_modify_code(ip, old, new);
770 }
771
772 int ftrace_enable_ftrace_graph_caller(void)
773 {
774         return ftrace_modify_ftrace_graph_caller(true);
775 }
776
777 int ftrace_disable_ftrace_graph_caller(void)
778 {
779         return ftrace_modify_ftrace_graph_caller(false);
780 }
781
782 /*
783  * Hook the return address and push it in the stack of return addrs
784  * in current thread info. Return the address we want to divert to.
785  */
786 static unsigned long
787 __prepare_ftrace_return(unsigned long parent, unsigned long ip, unsigned long sp)
788 {
789         unsigned long return_hooker;
790         int bit;
791
792         if (unlikely(ftrace_graph_is_dead()))
793                 goto out;
794
795         if (unlikely(atomic_read(&current->tracing_graph_pause)))
796                 goto out;
797
798         bit = ftrace_test_recursion_trylock(ip, parent);
799         if (bit < 0)
800                 goto out;
801
802         return_hooker = ppc_function_entry(return_to_handler);
803
804         if (!function_graph_enter(parent, ip, 0, (unsigned long *)sp))
805                 parent = return_hooker;
806
807         ftrace_test_recursion_unlock(bit);
808 out:
809         return parent;
810 }
811
812 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
813 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
814                        struct ftrace_ops *op, struct ftrace_regs *fregs)
815 {
816         fregs->regs.link = __prepare_ftrace_return(parent_ip, ip, fregs->regs.gpr[1]);
817 }
818 #else
819 unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,
820                                     unsigned long sp)
821 {
822         return __prepare_ftrace_return(parent, ip, sp);
823 }
824 #endif
825 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
826
827 #ifdef CONFIG_PPC64_ELF_ABI_V1
828 char *arch_ftrace_match_adjust(char *str, const char *search)
829 {
830         if (str[0] == '.' && search[0] != '.')
831                 return str + 1;
832         else
833                 return str;
834 }
835 #endif /* CONFIG_PPC64_ELF_ABI_V1 */