2 * Dynamic Ftrace based Kprobes Optimization
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) Hitachi Ltd., 2012
20 #include <linux/kprobes.h>
21 #include <linux/ptrace.h>
22 #include <linux/hardirq.h>
23 #include <linux/preempt.h>
24 #include <linux/ftrace.h>
28 static nokprobe_inline
29 void __skip_singlestep(struct kprobe *p, struct pt_regs *regs,
30 struct kprobe_ctlblk *kcb, unsigned long orig_ip)
33 * Emulate singlestep (and also recover regs->ip)
34 * as if there is a 5byte nop
36 regs->ip = (unsigned long)p->addr + MCOUNT_INSN_SIZE;
37 if (unlikely(p->post_handler)) {
38 kcb->kprobe_status = KPROBE_HIT_SSDONE;
39 p->post_handler(p, regs, 0);
41 __this_cpu_write(current_kprobe, NULL);
46 int skip_singlestep(struct kprobe *p, struct pt_regs *regs,
47 struct kprobe_ctlblk *kcb)
49 if (kprobe_ftrace(p)) {
50 __skip_singlestep(p, regs, kcb, 0);
51 preempt_enable_no_resched();
56 NOKPROBE_SYMBOL(skip_singlestep);
58 /* Ftrace callback handler for kprobes -- called under preepmt disabed */
59 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
60 struct ftrace_ops *ops, struct pt_regs *regs)
63 struct kprobe_ctlblk *kcb;
66 /* Disable irq for emulating a breakpoint and avoiding preempt */
67 local_irq_save(flags);
69 p = get_kprobe((kprobe_opcode_t *)ip);
70 if (unlikely(!p) || kprobe_disabled(p))
73 kcb = get_kprobe_ctlblk();
74 if (kprobe_running()) {
75 kprobes_inc_nmissed_count(p);
77 unsigned long orig_ip = regs->ip;
78 /* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
79 regs->ip = ip + sizeof(kprobe_opcode_t);
81 /* To emulate trap based kprobes, preempt_disable here */
83 __this_cpu_write(current_kprobe, p);
84 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
85 if (!p->pre_handler || !p->pre_handler(p, regs)) {
86 __skip_singlestep(p, regs, kcb, orig_ip);
87 preempt_enable_no_resched();
90 * If pre_handler returns !0, it sets regs->ip and
91 * resets current kprobe, and keep preempt count +1.
95 local_irq_restore(flags);
97 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
99 int arch_prepare_kprobe_ftrace(struct kprobe *p)
101 p->ainsn.insn = NULL;
102 p->ainsn.boostable = false;