1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Dynamic Ftrace based Kprobes Optimization
5 * Copyright (C) Hitachi Ltd., 2012
7 #include <linux/kprobes.h>
8 #include <linux/ptrace.h>
9 #include <linux/hardirq.h>
10 #include <linux/preempt.h>
11 #include <linux/ftrace.h>
15 /* Ftrace callback handler for kprobes -- called under preepmt disabed */
16 void kprobe_ftrace_handler(unsigned long ip
, unsigned long parent_ip
,
17 struct ftrace_ops
*ops
, struct pt_regs
*regs
)
20 struct kprobe_ctlblk
*kcb
;
22 /* Preempt is disabled by ftrace */
23 p
= get_kprobe((kprobe_opcode_t
*)ip
);
24 if (unlikely(!p
) || kprobe_disabled(p
))
27 kcb
= get_kprobe_ctlblk();
28 if (kprobe_running()) {
29 kprobes_inc_nmissed_count(p
);
31 unsigned long orig_ip
= regs
->ip
;
32 /* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
33 regs
->ip
= ip
+ sizeof(kprobe_opcode_t
);
35 __this_cpu_write(current_kprobe
, p
);
36 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
37 if (!p
->pre_handler
|| !p
->pre_handler(p
, regs
)) {
39 * Emulate singlestep (and also recover regs->ip)
40 * as if there is a 5byte nop
42 regs
->ip
= (unsigned long)p
->addr
+ MCOUNT_INSN_SIZE
;
43 if (unlikely(p
->post_handler
)) {
44 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
45 p
->post_handler(p
, regs
, 0);
50 * If pre_handler returns !0, it changes regs->ip. We have to
51 * skip emulating post_handler.
53 __this_cpu_write(current_kprobe
, NULL
);
56 NOKPROBE_SYMBOL(kprobe_ftrace_handler
);
58 int arch_prepare_kprobe_ftrace(struct kprobe
*p
)
61 p
->ainsn
.boostable
= false;