Merge commit 'eae30cdf42' into kernel
[kernel/swap-modules.git] / ks_features / ks_features.c
1 /*
2  *  SWAP kernel features
3  *  modules/ks_features/ks_features.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Vyacheslav Cherkashin: SWAP ks_features implement
22  *
23  */
24
25
26 #include <linux/module.h>
27 #include <asm/errno.h>
28 #include <ksyms.h>
29 #include <dbi_kprobes.h>
30 #include <writer/swap_writer_module.h>
31 #include "ks_features.h"
32 #include "syscall_list.h"
33 #include "features_data.c"
34
35 struct ks_probe {
36         struct kretprobe rp;
37         int counter;
38         char *args;
39         int sub_type;
40 };
41
42 #define CREATE_RP(name)                                         \
43 {                                                               \
44         .entry_handler = NULL,                                  \
45         .handler = NULL                                         \
46 }
47
48 #define X(name, args) #name
49 static const char *const syscall_name[] = {
50         SYSCALL_LIST
51 };
52 #undef X
53
54 enum {
55         syscall_name_cnt = sizeof(syscall_name) / sizeof(char *)
56 };
57
58
59 #define X(name, args__)                                         \
60 {                                                               \
61         .rp = CREATE_RP(name),                                  \
62         .counter = 0,                                           \
63         .args = #args__,                                        \
64         .sub_type = PST_NONE                                    \
65 }
66
67 static struct ks_probe ksp[] = {
68         SYSCALL_LIST
69 };
70 #undef X
71
72 static const char *get_sys_name(size_t id)
73 {
74         return syscall_name[id];
75 }
76
77 static int get_counter(size_t id)
78 {
79         return ksp[id].counter;
80 }
81
82 static void inc_counter(size_t id)
83 {
84         ++ksp[id].counter;
85 }
86
87 static void dec_counter(size_t id)
88 {
89         --ksp[id].counter;
90 }
91
92 /* ========================= HANDLERS ========================= */
93 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
94 {
95         struct ks_probe *ksp = (struct ks_probe *)priv_arg;
96         const char *fmt = ksp->args;
97         int sub_type = ksp->sub_type;
98
99         entry_event(fmt, regs, PT_KS, sub_type);
100
101         return 0;
102 }
103
104 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
105 {
106         struct ks_probe *ksp = (struct ks_probe *)priv_arg;
107
108         exit_event(regs);
109
110         return 0;
111 }
112 /* ========================= HANDLERS ========================= */
113
114
115
116
117 /* ====================== SWITCH_CONTEXT ======================= */
118 static int switch_pre_entry(void *priv_arg, struct pt_regs *regs)
119 {
120         switch_entry(regs);
121
122         return 0;
123 }
124
125 static int switch_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
126 {
127         switch_exit(regs);
128
129         return 0;
130 }
131
132 struct jprobe switch_jp = {
133         .pre_entry = switch_pre_entry,
134 };
135
136 struct kretprobe switch_rp = {
137         .handler = switch_ret_handler
138 };
139
140 int init_switch_context(void)
141 {
142         unsigned long addr;
143
144         addr = swap_ksyms("__switch_to");
145         if (addr == 0) {
146                 printk("ERROR: not found '__switch_to'\n");
147                 return -EINVAL;
148         }
149
150         switch_jp.kp.addr = (kprobe_opcode_t *)addr;
151         switch_rp.kp.addr = (kprobe_opcode_t *)addr;
152
153         return 0;
154 }
155
156 void exit_switch_context(void)
157 {
158 }
159
160 static int register_switch_context(void)
161 {
162         int ret;
163
164         ret = dbi_register_jprobe(&switch_jp);
165         if (ret) {
166                 return ret;
167         }
168
169         ret = dbi_register_kretprobe(&switch_rp);
170         if (ret) {
171                 dbi_unregister_jprobe(&switch_jp);
172         }
173
174         return ret;
175 }
176
177 static int unregister_switch_context(void)
178 {
179         dbi_unregister_kretprobe(&switch_rp);
180         dbi_unregister_jprobe(&switch_jp);
181
182         return 0;
183 }
184 /* ====================== SWITCH_CONTEXT ======================= */
185
186
187
188
189
190 static int register_syscall(size_t id)
191 {
192         int ret;
193         printk("register_syscall: %s\n", get_sys_name(id));
194
195         if (ksp[id].rp.kp.addr == NULL)
196                 return 0;
197
198         ksp[id].rp.entry_handler = entry_handler;
199         ksp[id].rp.handler = ret_handler;
200         ksp[id].rp.priv_arg = &ksp[id];
201
202         ret = dbi_register_kretprobe(&ksp[id].rp);
203
204         return ret;
205 }
206
207 static int unregister_syscall(size_t id)
208 {
209         printk("unregister_syscall: %s\n", get_sys_name(id));
210
211         if (ksp[id].rp.kp.addr == NULL)
212                 return 0;
213
214         dbi_unregister_kretprobe(&ksp[id].rp);
215
216         return 0;
217 }
218
219 static void set_pst(struct feature *f, size_t id)
220 {
221         ksp[id].sub_type |= f->sub_type;
222 }
223
224 static void unset_pst(struct feature *f, size_t id)
225 {
226         ksp[id].sub_type &= !f->sub_type;
227 }
228
229 static int install_features(struct feature *f)
230 {
231         size_t i, id;
232
233         for (i = 0; i < f->cnt; ++i) {
234                 id = f->feature_list[i];
235                 set_pst(f, id);
236
237                 if (get_counter(id) == 0) {
238                         int ret = register_syscall(id);
239                         if (ret) {
240                                 printk("syscall %d install error, ret = %d\n",
241                                        id, ret);
242                                 return ret;
243                         }
244                 }
245
246                 inc_counter(id);
247         }
248
249         return 0;
250 }
251
252 static int uninstall_features(struct feature *f)
253 {
254         size_t i, id;
255
256         for (i = 0; i < f->cnt; ++i) {
257                 id = f->feature_list[i];
258
259                 if (get_counter(id) == 0) {
260                         /* TODO: error */
261                         return -EINVAL;
262                 }
263
264                 dec_counter(id);
265
266                 if (get_counter(id) == 0) {
267                         int ret = unregister_syscall(id);
268                         if (ret) {
269                                 /* TODO: error */
270                                 return ret;
271                         }
272                 }
273
274                 unset_pst(f, id);
275         }
276
277         return 0;
278 }
279
280 static struct feature *get_feature(enum feature_id id)
281 {
282         if (id < 0 || id >= (int)feature_cnt)
283                 return NULL;
284
285         return &features[id];
286 }
287
288 int set_feature(enum feature_id id)
289 {
290         struct feature *f;
291
292         if (id == FID_SWITCH) {
293                 return register_switch_context();
294         }
295
296         f = get_feature(id);
297         if (f == NULL)
298                 return -EINVAL;
299
300         return install_features(f);
301 }
302 EXPORT_SYMBOL_GPL(set_feature);
303
304 int unset_feature(enum feature_id id)
305 {
306         struct feature *f;
307
308         if (id == FID_SWITCH) {
309                 return unregister_switch_context();
310         }
311
312         f = get_feature(id);
313         if (f == NULL)
314                 return -EINVAL;
315
316         return uninstall_features(f);
317 }
318 EXPORT_SYMBOL_GPL(unset_feature);
319
320 static int __init init_ks_feature(void)
321 {
322         int i, ret;
323         unsigned long addr, ni_syscall;
324         char *name;
325
326         ret = init_switch_context();
327         if (ret)
328                 return ret;
329
330         ni_syscall = swap_ksyms("sys_ni_syscall");
331
332         for (i = 0; i < syscall_name_cnt; ++i) {
333                 name = get_sys_name(i);
334                 addr = swap_ksyms(name);
335                 if (addr == 0) {
336                         printk("%s() not found\n", name);
337                         return -EFAULT;
338                 }
339
340                 if (ni_syscall == addr) {
341                         printk("INFO: %s is not install\n", get_sys_name(i));
342                         addr = 0;
343                 }
344
345                 ksp[i].rp.kp.addr = addr;
346         }
347
348         return 0;
349 }
350
351 static void __exit exit_ks_feature(void)
352 {
353         int id;
354
355         for (id = 0; id < syscall_name_cnt; ++id) {
356                 if (get_counter(id) > 0)
357                         unregister_syscall(id);
358         }
359
360         exit_switch_context();
361 }
362
363 module_init(init_ks_feature);
364 module_exit(exit_ks_feature);
365
366 MODULE_LICENSE("GPL");
367
368 /* debug */
369 static void print_feature(struct feature *f)
370 {
371         size_t i;
372
373         for (i = 0; i < f->cnt; ++i) {
374                 printk("    feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
375         }
376 }
377
378 void print_features(void)
379 {
380         int i;
381
382         printk("print_features:\n");
383         for (i = 0; i < feature_cnt; ++i) {
384                 printk("feature: %d\n", i);
385                 print_feature(&features[i]);
386         }
387 }
388
389 void print_all_syscall(void)
390 {
391         int i;
392
393         printk("SYSCALL:\n");
394         for (i = 0; i < syscall_name_cnt; ++i) {
395                 printk("    [%2d] %s\n", get_counter(i), get_sys_name(i));
396         }
397 }
398 /* debug */