Skip to content

Commit 83acdce

Browse files
mhiramatAlexei Starovoitov
authored andcommitted
arm64: rethook: Add arm64 rethook implementation
Add rethook arm64 implementation. Most of the code has been copied from kretprobes on arm64. 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/164735287344.1084943.9787335632585653418.stgit@devnote2
1 parent 75caf33 commit 83acdce

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ config ARM64
201201
select HAVE_SYSCALL_TRACEPOINTS
202202
select HAVE_KPROBES
203203
select HAVE_KRETPROBES
204+
select HAVE_RETHOOK
204205
select HAVE_GENERIC_VDSO
205206
select IOMMU_DMA if IOMMU_SUPPORT
206207
select IRQ_DOMAIN

arch/arm64/include/asm/stacktrace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct stackframe {
5858
DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES);
5959
unsigned long prev_fp;
6060
enum stack_type prev_type;
61-
#ifdef CONFIG_KRETPROBES
61+
#if defined(CONFIG_KRETPROBES) || defined(CONFIG_RETHOOK)
6262
struct llist_node *kr_cur;
6363
#endif
6464
};

arch/arm64/kernel/probes/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ obj-$(CONFIG_KPROBES) += kprobes.o decode-insn.o \
44
simulate-insn.o
55
obj-$(CONFIG_UPROBES) += uprobes.o decode-insn.o \
66
simulate-insn.o
7+
obj-$(CONFIG_RETHOOK) += rethook.o rethook_trampoline.o

arch/arm64/kernel/probes/rethook.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Generic return hook for arm64.
4+
* Most of the code is copied from arch/arm64/kernel/probes/kprobes.c
5+
*/
6+
7+
#include <linux/kprobes.h>
8+
#include <linux/rethook.h>
9+
10+
/* This is called from arch_rethook_trampoline() */
11+
unsigned long __used arch_rethook_trampoline_callback(struct pt_regs *regs)
12+
{
13+
return rethook_trampoline_handler(regs, regs->regs[29]);
14+
}
15+
NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);
16+
17+
void arch_rethook_prepare(struct rethook_node *rhn, struct pt_regs *regs, bool mcount)
18+
{
19+
rhn->ret_addr = regs->regs[30];
20+
rhn->frame = regs->regs[29];
21+
22+
/* replace return addr (x30) with trampoline */
23+
regs->regs[30] = (u64)arch_rethook_trampoline;
24+
}
25+
NOKPROBE_SYMBOL(arch_rethook_prepare);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* trampoline entry and return code for rethook.
4+
* Copied from arch/arm64/kernel/probes/kprobes_trampoline.S
5+
*/
6+
7+
#include <linux/linkage.h>
8+
#include <asm/asm-offsets.h>
9+
#include <asm/assembler.h>
10+
11+
.text
12+
13+
.macro save_all_base_regs
14+
stp x0, x1, [sp, #S_X0]
15+
stp x2, x3, [sp, #S_X2]
16+
stp x4, x5, [sp, #S_X4]
17+
stp x6, x7, [sp, #S_X6]
18+
stp x8, x9, [sp, #S_X8]
19+
stp x10, x11, [sp, #S_X10]
20+
stp x12, x13, [sp, #S_X12]
21+
stp x14, x15, [sp, #S_X14]
22+
stp x16, x17, [sp, #S_X16]
23+
stp x18, x19, [sp, #S_X18]
24+
stp x20, x21, [sp, #S_X20]
25+
stp x22, x23, [sp, #S_X22]
26+
stp x24, x25, [sp, #S_X24]
27+
stp x26, x27, [sp, #S_X26]
28+
stp x28, x29, [sp, #S_X28]
29+
add x0, sp, #PT_REGS_SIZE
30+
stp lr, x0, [sp, #S_LR]
31+
/*
32+
* Construct a useful saved PSTATE
33+
*/
34+
mrs x0, nzcv
35+
mrs x1, daif
36+
orr x0, x0, x1
37+
mrs x1, CurrentEL
38+
orr x0, x0, x1
39+
mrs x1, SPSel
40+
orr x0, x0, x1
41+
stp xzr, x0, [sp, #S_PC]
42+
.endm
43+
44+
.macro restore_all_base_regs
45+
ldr x0, [sp, #S_PSTATE]
46+
and x0, x0, #(PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT)
47+
msr nzcv, x0
48+
ldp x0, x1, [sp, #S_X0]
49+
ldp x2, x3, [sp, #S_X2]
50+
ldp x4, x5, [sp, #S_X4]
51+
ldp x6, x7, [sp, #S_X6]
52+
ldp x8, x9, [sp, #S_X8]
53+
ldp x10, x11, [sp, #S_X10]
54+
ldp x12, x13, [sp, #S_X12]
55+
ldp x14, x15, [sp, #S_X14]
56+
ldp x16, x17, [sp, #S_X16]
57+
ldp x18, x19, [sp, #S_X18]
58+
ldp x20, x21, [sp, #S_X20]
59+
ldp x22, x23, [sp, #S_X22]
60+
ldp x24, x25, [sp, #S_X24]
61+
ldp x26, x27, [sp, #S_X26]
62+
ldp x28, x29, [sp, #S_X28]
63+
.endm
64+
65+
SYM_CODE_START(arch_rethook_trampoline)
66+
sub sp, sp, #PT_REGS_SIZE
67+
68+
save_all_base_regs
69+
70+
/* Setup a frame pointer. */
71+
add x29, sp, #S_FP
72+
73+
mov x0, sp
74+
bl arch_rethook_trampoline_callback
75+
/*
76+
* Replace trampoline address in lr with actual orig_ret_addr return
77+
* address.
78+
*/
79+
mov lr, x0
80+
81+
/* The frame pointer (x29) is restored with other registers. */
82+
restore_all_base_regs
83+
84+
add sp, sp, #PT_REGS_SIZE
85+
ret
86+
87+
SYM_CODE_END(arch_rethook_trampoline)

arch/arm64/kernel/stacktrace.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/export.h>
99
#include <linux/ftrace.h>
1010
#include <linux/kprobes.h>
11+
#include <linux/rethook.h>
1112
#include <linux/sched.h>
1213
#include <linux/sched/debug.h>
1314
#include <linux/sched/task_stack.h>
@@ -38,7 +39,7 @@ static notrace void start_backtrace(struct stackframe *frame, unsigned long fp,
3839
{
3940
frame->fp = fp;
4041
frame->pc = pc;
41-
#ifdef CONFIG_KRETPROBES
42+
#if defined(CONFIG_KRETPROBES) || defined(CONFIG_RETHOOK)
4243
frame->kr_cur = NULL;
4344
#endif
4445

@@ -138,6 +139,10 @@ static int notrace unwind_frame(struct task_struct *tsk,
138139
if (is_kretprobe_trampoline(frame->pc))
139140
frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur);
140141
#endif
142+
#ifdef CONFIG_RETHOOK
143+
if (is_rethook_trampoline(frame->pc))
144+
frame->pc = rethook_find_ret_addr(tsk, frame->fp, &frame->kr_cur);
145+
#endif
141146

142147
return 0;
143148
}

0 commit comments

Comments
 (0)