xref: /openbmc/linux/arch/x86/kernel/kprobes/ftrace.c (revision 93f5715e)
1 /*
2  * Dynamic Ftrace based Kprobes Optimization
3  *
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.
8  *
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.
13  *
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.
17  *
18  * Copyright (C) Hitachi Ltd., 2012
19  */
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>
25 
26 #include "common.h"
27 
28 /* Ftrace callback handler for kprobes -- called under preepmt disabed */
29 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
30 			   struct ftrace_ops *ops, struct pt_regs *regs)
31 {
32 	struct kprobe *p;
33 	struct kprobe_ctlblk *kcb;
34 
35 	/* Preempt is disabled by ftrace */
36 	p = get_kprobe((kprobe_opcode_t *)ip);
37 	if (unlikely(!p) || kprobe_disabled(p))
38 		return;
39 
40 	kcb = get_kprobe_ctlblk();
41 	if (kprobe_running()) {
42 		kprobes_inc_nmissed_count(p);
43 	} else {
44 		unsigned long orig_ip = regs->ip;
45 		/* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
46 		regs->ip = ip + sizeof(kprobe_opcode_t);
47 
48 		__this_cpu_write(current_kprobe, p);
49 		kcb->kprobe_status = KPROBE_HIT_ACTIVE;
50 		if (!p->pre_handler || !p->pre_handler(p, regs)) {
51 			/*
52 			 * Emulate singlestep (and also recover regs->ip)
53 			 * as if there is a 5byte nop
54 			 */
55 			regs->ip = (unsigned long)p->addr + MCOUNT_INSN_SIZE;
56 			if (unlikely(p->post_handler)) {
57 				kcb->kprobe_status = KPROBE_HIT_SSDONE;
58 				p->post_handler(p, regs, 0);
59 			}
60 			regs->ip = orig_ip;
61 		}
62 		/*
63 		 * If pre_handler returns !0, it changes regs->ip. We have to
64 		 * skip emulating post_handler.
65 		 */
66 		__this_cpu_write(current_kprobe, NULL);
67 	}
68 }
69 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
70 
71 int arch_prepare_kprobe_ftrace(struct kprobe *p)
72 {
73 	p->ainsn.insn = NULL;
74 	p->ainsn.boostable = false;
75 	return 0;
76 }
77