[REFACTOR] remove field priv_arg from struct kretprobe
[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)
94 {
95         struct kretprobe *rp = ri->rp;
96
97         if (rp) {
98                 struct ks_probe *ksp = container_of(rp, struct ks_probe, rp);
99                 const char *fmt = ksp->args;
100                 int sub_type = ksp->sub_type;
101
102                 entry_event(fmt, regs, PT_KS, sub_type);
103         }
104
105         return 0;
106 }
107
108 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
109 {
110         struct kretprobe *rp = ri->rp;
111
112         if (rp) {
113                 unsigned long func_addr = rp->kp.addr;
114
115                 exit_event(regs, func_addr);
116         }
117
118         return 0;
119 }
120 /* ========================= HANDLERS ========================= */
121
122
123
124
125 /* ====================== SWITCH_CONTEXT ======================= */
126 static int switch_pre_entry(void *priv_arg, struct pt_regs *regs)
127 {
128         switch_entry(regs);
129
130         return 0;
131 }
132
133 static int switch_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
134 {
135         switch_exit(regs);
136
137         return 0;
138 }
139
140 struct jprobe switch_jp = {
141         .pre_entry = switch_pre_entry,
142 };
143
144 struct kretprobe switch_rp = {
145         .handler = switch_ret_handler
146 };
147
148 int init_switch_context(void)
149 {
150         unsigned long addr;
151
152         addr = swap_ksyms("__switch_to");
153         if (addr == 0) {
154                 printk("ERROR: not found '__switch_to'\n");
155                 return -EINVAL;
156         }
157
158         switch_jp.kp.addr = (kprobe_opcode_t *)addr;
159         switch_rp.kp.addr = (kprobe_opcode_t *)addr;
160
161         return 0;
162 }
163
164 void exit_switch_context(void)
165 {
166 }
167
168 static int register_switch_context(void)
169 {
170         int ret;
171
172         ret = dbi_register_jprobe(&switch_jp);
173         if (ret) {
174                 return ret;
175         }
176
177         ret = dbi_register_kretprobe(&switch_rp);
178         if (ret) {
179                 dbi_unregister_jprobe(&switch_jp);
180         }
181
182         return ret;
183 }
184
185 static int unregister_switch_context(void)
186 {
187         dbi_unregister_kretprobe(&switch_rp);
188         dbi_unregister_jprobe(&switch_jp);
189
190         return 0;
191 }
192 /* ====================== SWITCH_CONTEXT ======================= */
193
194
195
196
197
198 static int register_syscall(size_t id)
199 {
200         int ret;
201         printk("register_syscall: %s\n", get_sys_name(id));
202
203         if (ksp[id].rp.kp.addr == NULL)
204                 return 0;
205
206         ksp[id].rp.entry_handler = entry_handler;
207         ksp[id].rp.handler = ret_handler;
208
209         ret = dbi_register_kretprobe(&ksp[id].rp);
210
211         return ret;
212 }
213
214 static int unregister_syscall(size_t id)
215 {
216         printk("unregister_syscall: %s\n", get_sys_name(id));
217
218         if (ksp[id].rp.kp.addr == NULL)
219                 return 0;
220
221         dbi_unregister_kretprobe(&ksp[id].rp);
222
223         return 0;
224 }
225
226 static void set_pst(struct feature *f, size_t id)
227 {
228         ksp[id].sub_type |= f->sub_type;
229 }
230
231 static void unset_pst(struct feature *f, size_t id)
232 {
233         ksp[id].sub_type &= !f->sub_type;
234 }
235
236 static int install_features(struct feature *f)
237 {
238         size_t i, id;
239
240         for (i = 0; i < f->cnt; ++i) {
241                 id = f->feature_list[i];
242                 set_pst(f, id);
243
244                 if (get_counter(id) == 0) {
245                         int ret = register_syscall(id);
246                         if (ret) {
247                                 printk("syscall %d install error, ret = %d\n",
248                                        id, ret);
249                                 return ret;
250                         }
251                 }
252
253                 inc_counter(id);
254         }
255
256         return 0;
257 }
258
259 static int uninstall_features(struct feature *f)
260 {
261         size_t i, id;
262
263         for (i = 0; i < f->cnt; ++i) {
264                 id = f->feature_list[i];
265
266                 if (get_counter(id) == 0) {
267                         /* TODO: error */
268                         return -EINVAL;
269                 }
270
271                 dec_counter(id);
272
273                 if (get_counter(id) == 0) {
274                         int ret = unregister_syscall(id);
275                         if (ret) {
276                                 /* TODO: error */
277                                 return ret;
278                         }
279                 }
280
281                 unset_pst(f, id);
282         }
283
284         return 0;
285 }
286
287 static struct feature *get_feature(enum feature_id id)
288 {
289         if (id < 0 || id >= (int)feature_cnt)
290                 return NULL;
291
292         return &features[id];
293 }
294
295 int set_feature(enum feature_id id)
296 {
297         struct feature *f;
298
299         if (id == FID_SWITCH) {
300                 return register_switch_context();
301         }
302
303         f = get_feature(id);
304         if (f == NULL)
305                 return -EINVAL;
306
307         return install_features(f);
308 }
309 EXPORT_SYMBOL_GPL(set_feature);
310
311 int unset_feature(enum feature_id id)
312 {
313         struct feature *f;
314
315         if (id == FID_SWITCH) {
316                 return unregister_switch_context();
317         }
318
319         f = get_feature(id);
320         if (f == NULL)
321                 return -EINVAL;
322
323         return uninstall_features(f);
324 }
325 EXPORT_SYMBOL_GPL(unset_feature);
326
327 static int __init init_ks_feature(void)
328 {
329         int i, ret;
330         unsigned long addr, ni_syscall;
331         char *name;
332
333         ret = init_switch_context();
334         if (ret)
335                 return ret;
336
337         ni_syscall = swap_ksyms("sys_ni_syscall");
338
339         for (i = 0; i < syscall_name_cnt; ++i) {
340                 name = get_sys_name(i);
341                 addr = swap_ksyms(name);
342                 if (addr == 0) {
343                         printk("%s() not found\n", name);
344                         return -EFAULT;
345                 }
346
347                 if (ni_syscall == addr) {
348                         printk("INFO: %s is not install\n", get_sys_name(i));
349                         addr = 0;
350                 }
351
352                 ksp[i].rp.kp.addr = addr;
353         }
354
355         return 0;
356 }
357
358 static void __exit exit_ks_feature(void)
359 {
360         int id;
361
362         for (id = 0; id < syscall_name_cnt; ++id) {
363                 if (get_counter(id) > 0)
364                         unregister_syscall(id);
365         }
366
367         exit_switch_context();
368 }
369
370 module_init(init_ks_feature);
371 module_exit(exit_ks_feature);
372
373 MODULE_LICENSE("GPL");
374
375 /* debug */
376 static void print_feature(struct feature *f)
377 {
378         size_t i;
379
380         for (i = 0; i < f->cnt; ++i) {
381                 printk("    feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
382         }
383 }
384
385 void print_features(void)
386 {
387         int i;
388
389         printk("print_features:\n");
390         for (i = 0; i < feature_cnt; ++i) {
391                 printk("feature: %d\n", i);
392                 print_feature(&features[i]);
393         }
394 }
395
396 void print_all_syscall(void)
397 {
398         int i;
399
400         printk("SYSCALL:\n");
401         for (i = 0; i < syscall_name_cnt; ++i) {
402                 printk("    [%2d] %s\n", get_counter(i), get_sys_name(i));
403         }
404 }
405 /* debug */