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