summaryrefslogtreecommitdiff
path: root/ks_manager/ks_manager.c
blob: e183a79f683bff756e7304d9193a320eb5a8f5b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 *  Dynamic Binary Instrumentation Module based on KProbes
 *  modules/ks_manager/ks_manager.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) Samsung Electronics, 2013
 *
 * 2013         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
 *
 */

#include <linux/module.h>
#include <linux/slab.h>
#include <kprobe/swap_kprobes.h>
#include <kprobe/swap_kprobes_deps.h>
#include "ks_manager.h"

struct probe {
	struct hlist_node hlist;
	struct kern_probe p;
};

static HLIST_HEAD(list_probes);

static struct probe *create_probe(unsigned long addr, void *pre_handler,
				  void *jp_handler, void *rp_handler)
{
	struct probe *p = kzalloc(sizeof(*p), GFP_KERNEL);

	p->p.jp.kp.addr = p->p.rp.kp.addr = (void *)addr;
	p->p.jp.pre_entry = pre_handler;
	p->p.jp.entry = jp_handler;
	p->p.rp.handler = rp_handler;
	INIT_HLIST_NODE(&p->hlist);

	return p;
}

static void free_probe(struct probe *p)
{
	kfree(p);
}

static void add_probe_to_list(struct probe *p)
{
	hlist_add_head(&p->hlist, &list_probes);
}

static void remove_probe_to_list(struct probe *p)
{
	hlist_del(&p->hlist);
}

static struct probe *find_probe(unsigned long addr)
{
	struct probe *p;
	DECLARE_NODE_PTR_FOR_HLIST(node);

	/* check if such probe does exist */
	swap_hlist_for_each_entry(p, node, &list_probes, hlist)
		if ((unsigned long)p->p.jp.kp.addr == addr)
			return p;

	return NULL;
}

int ksm_register_probe(unsigned long addr, void *pre_handler,
		       void *jp_handler, void *rp_handler)
{
	int ret;
	struct probe *p;

	p = create_probe(addr, pre_handler, jp_handler, rp_handler);
	if (!p)
		return -ENOMEM;

	ret = swap_register_jprobe(&p->p.jp);
	if (ret)
		goto free;

	ret = swap_register_kretprobe(&p->p.rp);
	if (ret)
		goto unregister_jprobe;

	add_probe_to_list(p);
	return 0;

unregister_jprobe:
	swap_unregister_jprobe(&p->p.jp);
free:
	free_probe(p);
	return ret;
}
EXPORT_SYMBOL_GPL(ksm_register_probe);

static void do_ksm_unregister_probe(struct probe *p)
{
	remove_probe_to_list(p);
	swap_unregister_kretprobe(&p->p.rp);
	swap_unregister_jprobe(&p->p.jp);
	free_probe(p);
}

int ksm_unregister_probe(unsigned long addr)
{
	struct probe *p;

	p = find_probe(addr);
	if (p == NULL)
		return -EINVAL;

	do_ksm_unregister_probe(p);

	return 0;
}
EXPORT_SYMBOL_GPL(ksm_unregister_probe);

int ksm_unregister_probe_all(void)
{
	struct probe *p;
	struct hlist_node *n;
	DECLARE_NODE_PTR_FOR_HLIST(node);

	swap_hlist_for_each_entry_safe(p, node, n, &list_probes, hlist) {
		do_ksm_unregister_probe(p);
	}

	return 0;
}
EXPORT_SYMBOL_GPL(ksm_unregister_probe_all);

static int __init init_ks_manager(void)
{
       return 0;
}

static void __exit exit_ks_manager(void)
{
	ksm_unregister_probe_all();
}

module_init(init_ks_manager);
module_exit(exit_ks_manager);

MODULE_LICENSE ("GPL");