Skip to content

Commit 02752bd

Browse files
mhiramatAlexei Starovoitov
authored andcommitted
powerpc: Add rethook support
Add rethook powerpc64 implementation. Most of the code has been copied from kretprobes on powerpc64. Signed-off-by: Masami Hiramatsu <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]> Tested-by: Steven Rostedt (Google) <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/164735288495.1084943.539630613772422267.stgit@devnote2
1 parent 83acdce commit 02752bd

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

arch/powerpc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ config PPC
229229
select HAVE_PERF_EVENTS_NMI if PPC64
230230
select HAVE_PERF_REGS
231231
select HAVE_PERF_USER_STACK_DUMP
232+
select HAVE_RETHOOK if KPROBES
232233
select HAVE_REGS_AND_STACK_ACCESS_API
233234
select HAVE_RELIABLE_STACKTRACE
234235
select HAVE_RSEQ

arch/powerpc/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ obj-$(CONFIG_SMP) += smp.o
115115
obj-$(CONFIG_KPROBES) += kprobes.o
116116
obj-$(CONFIG_OPTPROBES) += optprobes.o optprobes_head.o
117117
obj-$(CONFIG_KPROBES_ON_FTRACE) += kprobes-ftrace.o
118+
obj-$(CONFIG_RETHOOK) += rethook.o
118119
obj-$(CONFIG_UPROBES) += uprobes.o
119120
obj-$(CONFIG_PPC_UDBG_16550) += legacy_serial.o udbg_16550.o
120121
obj-$(CONFIG_SWIOTLB) += dma-swiotlb.o

arch/powerpc/kernel/rethook.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
unsigned long orig_ret_address;
28+
29+
orig_ret_address = rethook_trampoline_handler(regs, 0);
30+
/*
31+
* We get here through one of two paths:
32+
* 1. by taking a trap -> kprobe_handler() -> here
33+
* 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here
34+
*
35+
* When going back through (1), we need regs->nip to be setup properly
36+
* as it is used to determine the return address from the trap.
37+
* For (2), since nip is not honoured with optprobes, we instead setup
38+
* the link register properly so that the subsequent 'blr' in
39+
* __kretprobe_trampoline jumps back to the right instruction.
40+
*
41+
* For nip, we should set the address to the previous instruction since
42+
* we end up emulating it in kprobe_handler(), which increments the nip
43+
* again.
44+
*/
45+
regs_set_return_ip(regs, orig_ret_address - 4);
46+
regs->link = orig_ret_address;
47+
48+
return 0;
49+
}
50+
NOKPROBE_SYMBOL(trampoline_rethook_handler);
51+
52+
void arch_rethook_prepare(struct rethook_node *rh, struct pt_regs *regs, bool mcount)
53+
{
54+
rh->ret_addr = regs->link;
55+
rh->frame = 0;
56+
57+
/* Replace the return addr with trampoline addr */
58+
regs->link = (unsigned long)arch_rethook_trampoline;
59+
}
60+
NOKPROBE_SYMBOL(arch_prepare_kretprobe);
61+
62+
static struct kprobe trampoline_p = {
63+
.addr = (kprobe_opcode_t *) &arch_rethook_trampoline,
64+
.pre_handler = trampoline_rethook_handler
65+
};
66+
67+
static int init_arch_rethook(void)
68+
{
69+
return register_kprobe(&trampoline_p);
70+
}
71+
72+
core_initcall(init_arch_rethook);

0 commit comments

Comments
 (0)