1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * x86 implementation of rethook. Mostly copied from arch/x86/kernel/kprobes/core.c.
6 #include <linux/rethook.h>
7 #include <linux/kprobes.h>
8 #include <linux/objtool.h>
10 #include "kprobes/common.h"
12 __visible
void arch_rethook_trampoline_callback(struct pt_regs
*regs
);
14 #ifndef ANNOTATE_NOENDBR
15 #define ANNOTATE_NOENDBR
19 * When a target function returns, this code saves registers and calls
20 * arch_rethook_trampoline_callback(), which calls the rethook handler.
24 ".global arch_rethook_trampoline\n"
25 ".type arch_rethook_trampoline, @function\n"
26 "arch_rethook_trampoline:\n"
28 ANNOTATE_NOENDBR
/* This is only jumped from ret instruction */
29 /* Push a fake return address to tell the unwinder it's a rethook. */
30 " pushq $arch_rethook_trampoline\n"
32 " pushq $" __stringify(__KERNEL_DS
) "\n"
33 /* Save the 'sp - 16', this will be fixed later. */
38 " call arch_rethook_trampoline_callback\n"
40 /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
44 /* Push a fake return address to tell the unwinder it's a rethook. */
45 " pushl $arch_rethook_trampoline\n"
48 /* Save the 'sp - 8', this will be fixed later. */
53 " call arch_rethook_trampoline_callback\n"
55 /* In the callback function, 'regs->flags' is copied to 'regs->ss'. */
60 ".size arch_rethook_trampoline, .-arch_rethook_trampoline\n"
62 NOKPROBE_SYMBOL(arch_rethook_trampoline
);
65 * Called from arch_rethook_trampoline
67 __used __visible
void arch_rethook_trampoline_callback(struct pt_regs
*regs
)
69 unsigned long *frame_pointer
;
72 regs
->cs
= __KERNEL_CS
;
76 regs
->ip
= (unsigned long)&arch_rethook_trampoline
;
78 regs
->sp
+= 2*sizeof(long);
79 frame_pointer
= (long *)(regs
+ 1);
82 * The return address at 'frame_pointer' is recovered by the
83 * arch_rethook_fixup_return() which called from this
84 * rethook_trampoline_handler().
86 rethook_trampoline_handler(regs
, (unsigned long)frame_pointer
);
89 * Copy FLAGS to 'pt_regs::ss' so that arch_rethook_trapmoline()
90 * can do RET right after POPF.
92 *(unsigned long *)®s
->ss
= regs
->flags
;
94 NOKPROBE_SYMBOL(arch_rethook_trampoline_callback
);
97 * arch_rethook_trampoline() skips updating frame pointer. The frame pointer
98 * saved in arch_rethook_trampoline_callback() points to the real caller
99 * function's frame pointer. Thus the arch_rethook_trampoline() doesn't have
100 * a standard stack frame with CONFIG_FRAME_POINTER=y.
101 * Let's mark it non-standard function. Anyway, FP unwinder can correctly
102 * unwind without the hint.
104 STACK_FRAME_NON_STANDARD_FP(arch_rethook_trampoline
);
106 /* This is called from rethook_trampoline_handler(). */
107 void arch_rethook_fixup_return(struct pt_regs
*regs
,
108 unsigned long correct_ret_addr
)
110 unsigned long *frame_pointer
= (void *)(regs
+ 1);
112 /* Replace fake return address with real one. */
113 *frame_pointer
= correct_ret_addr
;
115 NOKPROBE_SYMBOL(arch_rethook_fixup_return
);
117 void arch_rethook_prepare(struct rethook_node
*rh
, struct pt_regs
*regs
, bool mcount
)
119 unsigned long *stack
= (unsigned long *)regs
->sp
;
121 rh
->ret_addr
= stack
[0];
122 rh
->frame
= regs
->sp
;
124 /* Replace the return addr with trampoline addr */
125 stack
[0] = (unsigned long) arch_rethook_trampoline
;
127 NOKPROBE_SYMBOL(arch_rethook_prepare
);