3 * modules/ks_features/ks_features.c
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.
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.
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.
19 * Copyright (C) Samsung Electronics, 2013
21 * 2013 Vyacheslav Cherkashin: SWAP ks_features implement
26 #include <linux/module.h>
27 #include <asm/errno.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"
42 #define CREATE_RP(name) \
44 .entry_handler = NULL, \
48 #define X(name, args) #name
49 static const char *const syscall_name[] = {
55 syscall_name_cnt = sizeof(syscall_name) / sizeof(char *)
59 #define X(name, args__) \
61 .rp = CREATE_RP(name), \
64 .sub_type = PST_NONE \
67 static struct ks_probe ksp[] = {
72 static const char *get_sys_name(size_t id)
74 return syscall_name[id];
77 static int get_counter(size_t id)
79 return ksp[id].counter;
82 static void inc_counter(size_t id)
87 static void dec_counter(size_t id)
92 /* ========================= HANDLERS ========================= */
93 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
95 struct ks_probe *ksp = (struct ks_probe *)priv_arg;
96 const char *fmt = ksp->args;
97 int sub_type = ksp->sub_type;
99 entry_event(fmt, regs, PT_KS, sub_type);
104 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
106 struct ks_probe *ksp = (struct ks_probe *)priv_arg;
112 /* ========================= HANDLERS ========================= */
117 /* ====================== SWITCH_CONTEXT ======================= */
118 static int switch_pre_entry(void *priv_arg, struct pt_regs *regs)
125 static int switch_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
132 struct jprobe switch_jp = {
133 .pre_entry = switch_pre_entry,
136 struct kretprobe switch_rp = {
137 .handler = switch_ret_handler
140 int init_switch_context(void)
144 addr = swap_ksyms("__switch_to");
146 printk("ERROR: not found '__switch_to'\n");
150 switch_jp.kp.addr = (kprobe_opcode_t *)addr;
151 switch_rp.kp.addr = (kprobe_opcode_t *)addr;
156 void exit_switch_context(void)
160 static int register_switch_context(void)
164 ret = dbi_register_jprobe(&switch_jp);
169 ret = dbi_register_kretprobe(&switch_rp);
171 dbi_unregister_jprobe(&switch_jp);
177 static int unregister_switch_context(void)
179 dbi_unregister_kretprobe(&switch_rp);
180 dbi_unregister_jprobe(&switch_jp);
184 /* ====================== SWITCH_CONTEXT ======================= */
190 static int register_syscall(size_t id)
193 printk("register_syscall: %s\n", get_sys_name(id));
195 if (ksp[id].rp.kp.addr == NULL)
198 ksp[id].rp.entry_handler = entry_handler;
199 ksp[id].rp.handler = ret_handler;
200 ksp[id].rp.priv_arg = &ksp[id];
202 ret = dbi_register_kretprobe(&ksp[id].rp);
207 static int unregister_syscall(size_t id)
209 printk("unregister_syscall: %s\n", get_sys_name(id));
211 if (ksp[id].rp.kp.addr == NULL)
214 dbi_unregister_kretprobe(&ksp[id].rp);
219 static void set_pst(struct feature *f, size_t id)
221 ksp[id].sub_type |= f->sub_type;
224 static void unset_pst(struct feature *f, size_t id)
226 ksp[id].sub_type &= !f->sub_type;
229 static int install_features(struct feature *f)
233 for (i = 0; i < f->cnt; ++i) {
234 id = f->feature_list[i];
237 if (get_counter(id) == 0) {
238 int ret = register_syscall(id);
240 printk("syscall %d install error, ret = %d\n",
252 static int uninstall_features(struct feature *f)
256 for (i = 0; i < f->cnt; ++i) {
257 id = f->feature_list[i];
259 if (get_counter(id) == 0) {
266 if (get_counter(id) == 0) {
267 int ret = unregister_syscall(id);
280 static struct feature *get_feature(enum feature_id id)
282 if (id < 0 || id >= (int)feature_cnt)
285 return &features[id];
288 int set_feature(enum feature_id id)
292 if (id == FID_SWITCH) {
293 return register_switch_context();
300 return install_features(f);
302 EXPORT_SYMBOL_GPL(set_feature);
304 int unset_feature(enum feature_id id)
308 if (id == FID_SWITCH) {
309 return unregister_switch_context();
316 return uninstall_features(f);
318 EXPORT_SYMBOL_GPL(unset_feature);
320 static int __init init_ks_feature(void)
323 unsigned long addr, ni_syscall;
326 ret = init_switch_context();
330 ni_syscall = swap_ksyms("sys_ni_syscall");
332 for (i = 0; i < syscall_name_cnt; ++i) {
333 name = get_sys_name(i);
334 addr = swap_ksyms(name);
336 printk("%s() not found\n", name);
340 if (ni_syscall == addr) {
341 printk("INFO: %s is not install\n", get_sys_name(i));
345 ksp[i].rp.kp.addr = addr;
351 static void __exit exit_ks_feature(void)
355 for (id = 0; id < syscall_name_cnt; ++id) {
356 if (get_counter(id) > 0)
357 unregister_syscall(id);
360 exit_switch_context();
363 module_init(init_ks_feature);
364 module_exit(exit_ks_feature);
366 MODULE_LICENSE("GPL");
369 static void print_feature(struct feature *f)
373 for (i = 0; i < f->cnt; ++i) {
374 printk(" feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
378 void print_features(void)
382 printk("print_features:\n");
383 for (i = 0; i < feature_cnt; ++i) {
384 printk("feature: %d\n", i);
385 print_feature(&features[i]);
389 void print_all_syscall(void)
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));