Linux Audio

Check our new training course

Loading...
v4.17
 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
28static nokprobe_inline
29void __skip_singlestep(struct kprobe *p, struct pt_regs *regs,
30		      struct kprobe_ctlblk *kcb, unsigned long orig_ip)
31{
32	/*
33	 * Emulate singlestep (and also recover regs->ip)
34	 * as if there is a 5byte nop
35	 */
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);
40	}
41	__this_cpu_write(current_kprobe, NULL);
42	if (orig_ip)
43		regs->ip = orig_ip;
44}
45
46int skip_singlestep(struct kprobe *p, struct pt_regs *regs,
47		    struct kprobe_ctlblk *kcb)
48{
49	if (kprobe_ftrace(p)) {
50		__skip_singlestep(p, regs, kcb, 0);
51		preempt_enable_no_resched();
52		return 1;
53	}
54	return 0;
55}
56NOKPROBE_SYMBOL(skip_singlestep);
57
58/* Ftrace callback handler for kprobes -- called under preepmt disabed */
59void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
60			   struct ftrace_ops *ops, struct pt_regs *regs)
61{
62	struct kprobe *p;
63	struct kprobe_ctlblk *kcb;
 
 
 
 
64
65	/* Preempt is disabled by ftrace */
66	p = get_kprobe((kprobe_opcode_t *)ip);
67	if (unlikely(!p) || kprobe_disabled(p))
68		return;
69
70	kcb = get_kprobe_ctlblk();
71	if (kprobe_running()) {
72		kprobes_inc_nmissed_count(p);
73	} else {
74		unsigned long orig_ip = regs->ip;
75		/* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
76		regs->ip = ip + sizeof(kprobe_opcode_t);
77
78		/* To emulate trap based kprobes, preempt_disable here */
79		preempt_disable();
80		__this_cpu_write(current_kprobe, p);
81		kcb->kprobe_status = KPROBE_HIT_ACTIVE;
82		if (!p->pre_handler || !p->pre_handler(p, regs)) {
83			__skip_singlestep(p, regs, kcb, orig_ip);
84			preempt_enable_no_resched();
85		}
86		/*
87		 * If pre_handler returns !0, it sets regs->ip and
88		 * resets current kprobe, and keep preempt count +1.
89		 */
90	}
 
 
91}
92NOKPROBE_SYMBOL(kprobe_ftrace_handler);
93
94int arch_prepare_kprobe_ftrace(struct kprobe *p)
95{
96	p->ainsn.insn = NULL;
97	p->ainsn.boostable = false;
98	return 0;
99}
v3.15
 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
28static int __skip_singlestep(struct kprobe *p, struct pt_regs *regs,
29			     struct kprobe_ctlblk *kcb)
 
30{
31	/*
32	 * Emulate singlestep (and also recover regs->ip)
33	 * as if there is a 5byte nop
34	 */
35	regs->ip = (unsigned long)p->addr + MCOUNT_INSN_SIZE;
36	if (unlikely(p->post_handler)) {
37		kcb->kprobe_status = KPROBE_HIT_SSDONE;
38		p->post_handler(p, regs, 0);
39	}
40	__this_cpu_write(current_kprobe, NULL);
41	return 1;
 
42}
43
44int __kprobes skip_singlestep(struct kprobe *p, struct pt_regs *regs,
45			      struct kprobe_ctlblk *kcb)
46{
47	if (kprobe_ftrace(p))
48		return __skip_singlestep(p, regs, kcb);
49	else
50		return 0;
 
 
51}
 
52
53/* Ftrace callback handler for kprobes */
54void __kprobes kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
55				     struct ftrace_ops *ops, struct pt_regs *regs)
56{
57	struct kprobe *p;
58	struct kprobe_ctlblk *kcb;
59	unsigned long flags;
60
61	/* Disable irq for emulating a breakpoint and avoiding preempt */
62	local_irq_save(flags);
63
 
64	p = get_kprobe((kprobe_opcode_t *)ip);
65	if (unlikely(!p) || kprobe_disabled(p))
66		goto end;
67
68	kcb = get_kprobe_ctlblk();
69	if (kprobe_running()) {
70		kprobes_inc_nmissed_count(p);
71	} else {
 
72		/* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
73		regs->ip = ip + sizeof(kprobe_opcode_t);
74
 
 
75		__this_cpu_write(current_kprobe, p);
76		kcb->kprobe_status = KPROBE_HIT_ACTIVE;
77		if (!p->pre_handler || !p->pre_handler(p, regs))
78			__skip_singlestep(p, regs, kcb);
 
 
79		/*
80		 * If pre_handler returns !0, it sets regs->ip and
81		 * resets current kprobe.
82		 */
83	}
84end:
85	local_irq_restore(flags);
86}
 
87
88int __kprobes arch_prepare_kprobe_ftrace(struct kprobe *p)
89{
90	p->ainsn.insn = NULL;
91	p->ainsn.boostable = -1;
92	return 0;
93}