xref: /linux/arch/powerpc/kernel/rethook.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1*19f1bc3fSAbhishek Dubey // SPDX-License-Identifier: GPL-2.0-only
2*19f1bc3fSAbhishek Dubey /*
3*19f1bc3fSAbhishek Dubey  * PowerPC implementation of rethook. This depends on kprobes.
4*19f1bc3fSAbhishek Dubey  */
5*19f1bc3fSAbhishek Dubey 
6*19f1bc3fSAbhishek Dubey #include <linux/kprobes.h>
7*19f1bc3fSAbhishek Dubey #include <linux/rethook.h>
8*19f1bc3fSAbhishek Dubey 
9*19f1bc3fSAbhishek Dubey /*
10*19f1bc3fSAbhishek Dubey  * Function return trampoline:
11*19f1bc3fSAbhishek Dubey  *     - init_kprobes() establishes a probepoint here
12*19f1bc3fSAbhishek Dubey  *     - When the probed function returns, this probe
13*19f1bc3fSAbhishek Dubey  *         causes the handlers to fire
14*19f1bc3fSAbhishek Dubey  */
15*19f1bc3fSAbhishek Dubey asm(".global arch_rethook_trampoline\n"
16*19f1bc3fSAbhishek Dubey 	".type arch_rethook_trampoline, @function\n"
17*19f1bc3fSAbhishek Dubey 	"arch_rethook_trampoline:\n"
18*19f1bc3fSAbhishek Dubey 	"nop\n"
19*19f1bc3fSAbhishek Dubey 	"blr\n"
20*19f1bc3fSAbhishek Dubey 	".size arch_rethook_trampoline, .-arch_rethook_trampoline\n");
21*19f1bc3fSAbhishek Dubey 
22*19f1bc3fSAbhishek Dubey /*
23*19f1bc3fSAbhishek Dubey  * Called when the probe at kretprobe trampoline is hit
24*19f1bc3fSAbhishek Dubey  */
trampoline_rethook_handler(struct kprobe * p,struct pt_regs * regs)25*19f1bc3fSAbhishek Dubey static int trampoline_rethook_handler(struct kprobe *p, struct pt_regs *regs)
26*19f1bc3fSAbhishek Dubey {
27*19f1bc3fSAbhishek Dubey 	return !rethook_trampoline_handler(regs, regs->gpr[1]);
28*19f1bc3fSAbhishek Dubey }
29*19f1bc3fSAbhishek Dubey NOKPROBE_SYMBOL(trampoline_rethook_handler);
30*19f1bc3fSAbhishek Dubey 
arch_rethook_prepare(struct rethook_node * rh,struct pt_regs * regs,bool mcount)31*19f1bc3fSAbhishek Dubey void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount)
32*19f1bc3fSAbhishek Dubey {
33*19f1bc3fSAbhishek Dubey 	rh->ret_addr = regs->link;
34*19f1bc3fSAbhishek Dubey 	rh->frame = regs->gpr[1];
35*19f1bc3fSAbhishek Dubey 
36*19f1bc3fSAbhishek Dubey 	/* Replace the return addr with trampoline addr */
37*19f1bc3fSAbhishek Dubey 	regs->link = (unsigned long)arch_rethook_trampoline;
38*19f1bc3fSAbhishek Dubey }
39*19f1bc3fSAbhishek Dubey NOKPROBE_SYMBOL(arch_rethook_prepare);
40*19f1bc3fSAbhishek Dubey 
41*19f1bc3fSAbhishek Dubey /* This is called from rethook_trampoline_handler(). */
arch_rethook_fixup_return(struct pt_regs * regs,unsigned long orig_ret_address)42*19f1bc3fSAbhishek Dubey void arch_rethook_fixup_return(struct pt_regs *regs, unsigned long orig_ret_address)
43*19f1bc3fSAbhishek Dubey {
44*19f1bc3fSAbhishek Dubey 	/*
45*19f1bc3fSAbhishek Dubey 	 * We get here through one of two paths:
46*19f1bc3fSAbhishek Dubey 	 * 1. by taking a trap -> kprobe_handler() -> here
47*19f1bc3fSAbhishek Dubey 	 * 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here
48*19f1bc3fSAbhishek Dubey 	 *
49*19f1bc3fSAbhishek Dubey 	 * When going back through (1), we need regs->nip to be setup properly
50*19f1bc3fSAbhishek Dubey 	 * as it is used to determine the return address from the trap.
51*19f1bc3fSAbhishek Dubey 	 * For (2), since nip is not honoured with optprobes, we instead setup
52*19f1bc3fSAbhishek Dubey 	 * the link register properly so that the subsequent 'blr' in
53*19f1bc3fSAbhishek Dubey 	 * arch_rethook_trampoline jumps back to the right instruction.
54*19f1bc3fSAbhishek Dubey 	 *
55*19f1bc3fSAbhishek Dubey 	 * For nip, we should set the address to the previous instruction since
56*19f1bc3fSAbhishek Dubey 	 * we end up emulating it in kprobe_handler(), which increments the nip
57*19f1bc3fSAbhishek Dubey 	 * again.
58*19f1bc3fSAbhishek Dubey 	 */
59*19f1bc3fSAbhishek Dubey 	regs_set_return_ip(regs, orig_ret_address - 4);
60*19f1bc3fSAbhishek Dubey 	regs->link = orig_ret_address;
61*19f1bc3fSAbhishek Dubey }
62*19f1bc3fSAbhishek Dubey NOKPROBE_SYMBOL(arch_rethook_fixup_return);
63*19f1bc3fSAbhishek Dubey 
64*19f1bc3fSAbhishek Dubey static struct kprobe trampoline_p = {
65*19f1bc3fSAbhishek Dubey 	.addr = (kprobe_opcode_t *) &arch_rethook_trampoline,
66*19f1bc3fSAbhishek Dubey 	.pre_handler = trampoline_rethook_handler
67*19f1bc3fSAbhishek Dubey };
68*19f1bc3fSAbhishek Dubey 
69*19f1bc3fSAbhishek Dubey /* rethook initializer */
arch_init_kprobes(void)70*19f1bc3fSAbhishek Dubey int __init arch_init_kprobes(void)
71*19f1bc3fSAbhishek Dubey {
72*19f1bc3fSAbhishek Dubey 	return register_kprobe(&trampoline_p);
73*19f1bc3fSAbhishek Dubey }
74