Skip to content

Commit 0dc016d

Browse files
WangNan0tixy
authored andcommitted
ARM: kprobes: enable OPTPROBES for ARM 32
This patch introduce kprobeopt for ARM 32. Limitations: - Currently only kernel compiled with ARM ISA is supported. - Offset between probe point and optinsn slot must not larger than 32MiB. Masami Hiramatsu suggests replacing 2 words, it will make things complex. Futher patch can make such optimization. Kprobe opt on ARM is relatively simpler than kprobe opt on x86 because ARM instruction is always 4 bytes aligned and 4 bytes long. This patch replace probed instruction by a 'b', branch to trampoline code and then calls optimized_callback(). optimized_callback() calls opt_pre_handler() to execute kprobe handler. It also emulate/simulate replaced instruction. When unregistering kprobe, the deferred manner of unoptimizer may leave branch instruction before optimizer is called. Different from x86_64, which only copy the probed insn after optprobe_template_end and reexecute them, this patch call singlestep to emulate/simulate the insn directly. Futher patch can optimize this behavior. Signed-off-by: Wang Nan <[email protected]> Acked-by: Masami Hiramatsu <[email protected]> Cc: Will Deacon <[email protected]> Reviewed-by: Jon Medhurst (Tixy) <[email protected]> Signed-off-by: Jon Medhurst <[email protected]>
1 parent cbf6ab5 commit 0dc016d

File tree

10 files changed

+377
-12
lines changed

10 files changed

+377
-12
lines changed

arch/arm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ config ARM
6060
select HAVE_MEMBLOCK
6161
select HAVE_MOD_ARCH_SPECIFIC if ARM_UNWIND
6262
select HAVE_OPROFILE if (HAVE_PERF_EVENTS)
63+
select HAVE_OPTPROBES if !THUMB2_KERNEL
6364
select HAVE_PERF_EVENTS
6465
select HAVE_PERF_REGS
6566
select HAVE_PERF_USER_STACK_DUMP
File renamed without changes.

arch/arm/include/asm/kprobes.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,34 @@ int kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr);
5050
int kprobe_exceptions_notify(struct notifier_block *self,
5151
unsigned long val, void *data);
5252

53+
/* optinsn template addresses */
54+
extern __visible kprobe_opcode_t optprobe_template_entry;
55+
extern __visible kprobe_opcode_t optprobe_template_val;
56+
extern __visible kprobe_opcode_t optprobe_template_call;
57+
extern __visible kprobe_opcode_t optprobe_template_end;
58+
extern __visible kprobe_opcode_t optprobe_template_sub_sp;
59+
extern __visible kprobe_opcode_t optprobe_template_add_sp;
60+
61+
#define MAX_OPTIMIZED_LENGTH 4
62+
#define MAX_OPTINSN_SIZE \
63+
((unsigned long)&optprobe_template_end - \
64+
(unsigned long)&optprobe_template_entry)
65+
#define RELATIVEJUMP_SIZE 4
66+
67+
struct arch_optimized_insn {
68+
/*
69+
* copy of the original instructions.
70+
* Different from x86, ARM kprobe_opcode_t is u32.
71+
*/
72+
#define MAX_COPIED_INSN DIV_ROUND_UP(RELATIVEJUMP_SIZE, sizeof(kprobe_opcode_t))
73+
kprobe_opcode_t copied_insn[MAX_COPIED_INSN];
74+
/* detour code buffer */
75+
kprobe_opcode_t *insn;
76+
/*
77+
* We always copy one instruction on ARM,
78+
* so size will always be 4, and unlike x86, there is no
79+
* need for a size field.
80+
*/
81+
};
5382

5483
#endif /* _ARM_KPROBES_H */

arch/arm/kernel/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o insn.o
5252
obj-$(CONFIG_JUMP_LABEL) += jump_label.o insn.o patch.o
5353
obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
5454
# Main staffs in KPROBES are in arch/arm/probes/ .
55-
obj-$(CONFIG_KPROBES) += patch.o
55+
obj-$(CONFIG_KPROBES) += patch.o insn.o
5656
obj-$(CONFIG_OABI_COMPAT) += sys_oabi-compat.o
5757
obj-$(CONFIG_ARM_THUMBEE) += thumbee.o
5858
obj-$(CONFIG_KGDB) += kgdb.o patch.o

arch/arm/kernel/ftrace.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
#include <asm/cacheflush.h>
2121
#include <asm/opcodes.h>
2222
#include <asm/ftrace.h>
23-
24-
#include "insn.h"
23+
#include <asm/insn.h>
2524

2625
#ifdef CONFIG_THUMB2_KERNEL
2726
#define NOP 0xf85deb04 /* pop.w {lr} */

arch/arm/kernel/jump_label.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include <linux/kernel.h>
22
#include <linux/jump_label.h>
33
#include <asm/patch.h>
4-
5-
#include "insn.h"
4+
#include <asm/insn.h>
65

76
#ifdef HAVE_JUMP_LABEL
87

arch/arm/probes/kprobes/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ obj-$(CONFIG_KPROBES) += actions-thumb.o checkers-thumb.o
77
test-kprobes-objs += test-thumb.o
88
else
99
obj-$(CONFIG_KPROBES) += actions-arm.o checkers-arm.o
10+
obj-$(CONFIG_OPTPROBES) += opt-arm.o
1011
test-kprobes-objs += test-arm.o
1112
endif

arch/arm/probes/kprobes/core.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,31 @@ void __kprobes arch_arm_kprobe(struct kprobe *p)
163163
* memory. It is also needed to atomically set the two half-words of a 32-bit
164164
* Thumb breakpoint.
165165
*/
166-
int __kprobes __arch_disarm_kprobe(void *p)
167-
{
168-
struct kprobe *kp = p;
169-
void *addr = (void *)((uintptr_t)kp->addr & ~1);
170-
171-
__patch_text(addr, kp->opcode);
166+
struct patch {
167+
void *addr;
168+
unsigned int insn;
169+
};
172170

171+
static int __kprobes_remove_breakpoint(void *data)
172+
{
173+
struct patch *p = data;
174+
__patch_text(p->addr, p->insn);
173175
return 0;
174176
}
175177

178+
void __kprobes kprobes_remove_breakpoint(void *addr, unsigned int insn)
179+
{
180+
struct patch p = {
181+
.addr = addr,
182+
.insn = insn,
183+
};
184+
stop_machine(__kprobes_remove_breakpoint, &p, cpu_online_mask);
185+
}
186+
176187
void __kprobes arch_disarm_kprobe(struct kprobe *p)
177188
{
178-
stop_machine(__arch_disarm_kprobe, p, cpu_online_mask);
189+
kprobes_remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
190+
p->opcode);
179191
}
180192

181193
void __kprobes arch_remove_kprobe(struct kprobe *p)

arch/arm/probes/kprobes/core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#define KPROBE_THUMB16_BREAKPOINT_INSTRUCTION 0xde18
3131
#define KPROBE_THUMB32_BREAKPOINT_INSTRUCTION 0xf7f0a018
3232

33+
extern void kprobes_remove_breakpoint(void *addr, unsigned int insn);
34+
3335
enum probes_insn __kprobes
3436
kprobe_decode_ldmstm(kprobe_opcode_t insn, struct arch_probes_insn *asi,
3537
const struct decode_header *h);

0 commit comments

Comments
 (0)