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