Skip to content

Commit 24c276a

Browse files
mhiramatKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
arm64: rethook: Replace kretprobe trampoline with rethook
Replace the kretprobe's trampoline code with the rethook on arm64. The rethook on arm64 is almost renamed from kretprobe trampoline code. The mechanism is completely same. Signed-off-by: Masami Hiramatsu <[email protected]>
1 parent dbf236b commit 24c276a

File tree

9 files changed

+41
-27
lines changed

9 files changed

+41
-27
lines changed

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ config ARM64
204204
select HAVE_SYSCALL_TRACEPOINTS
205205
select HAVE_KPROBES
206206
select HAVE_KRETPROBES
207+
select HAVE_RETHOOK
207208
select HAVE_GENERIC_VDSO
208209
select IOMMU_DMA if IOMMU_SUPPORT
209210
select IRQ_DOMAIN

arch/arm64/include/asm/kprobes.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ void arch_remove_kprobe(struct kprobe *);
3939
int kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr);
4040
int kprobe_exceptions_notify(struct notifier_block *self,
4141
unsigned long val, void *data);
42-
void __kretprobe_trampoline(void);
43-
void __kprobes *trampoline_probe_handler(struct pt_regs *regs);
4442

4543
#endif /* CONFIG_KPROBES */
4644
#endif /* _ARM_KPROBES_H */

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_RETHOOK)
6262
struct llist_node *kr_cur;
6363
#endif
6464
};

arch/arm64/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ obj-$(CONFIG_ACPI_NUMA) += acpi_numa.o
6060
obj-$(CONFIG_ARM64_ACPI_PARKING_PROTOCOL) += acpi_parking_protocol.o
6161
obj-$(CONFIG_PARAVIRT) += paravirt.o
6262
obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
63+
obj-$(CONFIG_RETHOOK) += rethook.o rethook_trampoline.o
6364
obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o
6465
obj-$(CONFIG_ELF_CORE) += elfcore.o
6566
obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \

arch/arm64/kernel/probes/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-$(CONFIG_KPROBES) += kprobes.o decode-insn.o \
3-
kprobes_trampoline.o \
43
simulate-insn.o
54
obj-$(CONFIG_UPROBES) += uprobes.o decode-insn.o \
65
simulate-insn.o

arch/arm64/kernel/probes/kprobes.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -399,21 +399,6 @@ int __init arch_populate_kprobe_blacklist(void)
399399
return ret;
400400
}
401401

402-
void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
403-
{
404-
return (void *)kretprobe_trampoline_handler(regs, (void *)regs->regs[29]);
405-
}
406-
407-
void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
408-
struct pt_regs *regs)
409-
{
410-
ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
411-
ri->fp = (void *)regs->regs[29];
412-
413-
/* replace return addr (x30) with trampoline */
414-
regs->regs[30] = (long)&__kretprobe_trampoline;
415-
}
416-
417402
int __kprobes arch_trampoline_kprobe(struct kprobe *p)
418403
{
419404
return 0;

arch/arm64/kernel/rethook.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
unsigned long __used arch_rethook_trampoline_callback(struct pt_regs *regs)
14+
{
15+
return rethook_trampoline_handler(regs, regs->regs[29]);
16+
}
17+
NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);
18+
19+
int arch_rethook_prepare(struct rethook_node *rhn, struct pt_regs *regs, bool mcount)
20+
{
21+
rhn->ret_addr = regs->regs[30];
22+
rhn->frame = regs->regs[29];
23+
24+
/* replace return addr (x30) with trampoline */
25+
regs->regs[30] = (u64)arch_rethook_trampoline;
26+
return 0;
27+
}
28+
NOKPROBE_SYMBOL(arch_rethook_prepare);

arch/arm64/kernel/probes/kprobes_trampoline.S renamed to arch/arm64/kernel/rethook_trampoline.S

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
/*
3-
* trampoline entry and return code for kretprobes.
3+
* trampoline entry and return code for rethook.
4+
* Renamed from arch/arm64/kernel/probes/kprobes_trampoline.S
45
*/
56

67
#include <linux/linkage.h>
@@ -61,7 +62,7 @@
6162
ldp x28, x29, [sp, #S_X28]
6263
.endm
6364

64-
SYM_CODE_START(__kretprobe_trampoline)
65+
SYM_CODE_START(arch_rethook_trampoline)
6566
sub sp, sp, #PT_REGS_SIZE
6667

6768
save_all_base_regs
@@ -70,7 +71,7 @@ SYM_CODE_START(__kretprobe_trampoline)
7071
add x29, sp, #S_FP
7172

7273
mov x0, sp
73-
bl trampoline_probe_handler
74+
bl arch_rethook_trampoline_callback
7475
/*
7576
* Replace trampoline address in lr with actual orig_ret_addr return
7677
* address.
@@ -83,4 +84,4 @@ SYM_CODE_START(__kretprobe_trampoline)
8384
add sp, sp, #PT_REGS_SIZE
8485
ret
8586

86-
SYM_CODE_END(__kretprobe_trampoline)
87+
SYM_CODE_END(arch_rethook_trampoline)

arch/arm64/kernel/stacktrace.c

Lines changed: 5 additions & 4 deletions
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_RETHOOK)
4243
frame->kr_cur = NULL;
4344
#endif
4445

@@ -134,9 +135,9 @@ static int notrace unwind_frame(struct task_struct *tsk,
134135
frame->pc = orig_pc;
135136
}
136137
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
137-
#ifdef CONFIG_KRETPROBES
138-
if (is_kretprobe_trampoline(frame->pc))
139-
frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur);
138+
#ifdef CONFIG_RETHOOK
139+
if (is_rethook_trampoline(frame->pc))
140+
frame->pc = rethook_find_ret_addr(tsk, frame->fp, &frame->kr_cur);
140141
#endif
141142

142143
return 0;

0 commit comments

Comments
 (0)