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)
95 struct kretprobe *rp = ri->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;
102 entry_event(fmt, regs, PT_KS, sub_type);
108 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
110 struct kretprobe *rp = ri->rp;
113 unsigned long func_addr = rp->kp.addr;
115 exit_event(regs, func_addr);
120 /* ========================= HANDLERS ========================= */
125 /* ====================== SWITCH_CONTEXT ======================= */
126 static int switch_pre_entry(void *priv_arg, struct pt_regs *regs)
133 static int switch_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs, void *priv_arg)
140 struct jprobe switch_jp = {
141 .pre_entry = switch_pre_entry,
144 struct kretprobe switch_rp = {
145 .handler = switch_ret_handler
148 int init_switch_context(void)
152 addr = swap_ksyms("__switch_to");
154 printk("ERROR: not found '__switch_to'\n");
158 switch_jp.kp.addr = (kprobe_opcode_t *)addr;
159 switch_rp.kp.addr = (kprobe_opcode_t *)addr;
164 void exit_switch_context(void)
168 static int register_switch_context(void)
172 ret = dbi_register_jprobe(&switch_jp);
177 ret = dbi_register_kretprobe(&switch_rp);
179 dbi_unregister_jprobe(&switch_jp);
185 static int unregister_switch_context(void)
187 dbi_unregister_kretprobe(&switch_rp);
188 dbi_unregister_jprobe(&switch_jp);
192 /* ====================== SWITCH_CONTEXT ======================= */
198 static int register_syscall(size_t id)
201 printk("register_syscall: %s\n", get_sys_name(id));
203 if (ksp[id].rp.kp.addr == NULL)
206 ksp[id].rp.entry_handler = entry_handler;
207 ksp[id].rp.handler = ret_handler;
209 ret = dbi_register_kretprobe(&ksp[id].rp);
214 static int unregister_syscall(size_t id)
216 printk("unregister_syscall: %s\n", get_sys_name(id));
218 if (ksp[id].rp.kp.addr == NULL)
221 dbi_unregister_kretprobe(&ksp[id].rp);
226 static void set_pst(struct feature *f, size_t id)
228 ksp[id].sub_type |= f->sub_type;
231 static void unset_pst(struct feature *f, size_t id)
233 ksp[id].sub_type &= !f->sub_type;
236 static int install_features(struct feature *f)
240 for (i = 0; i < f->cnt; ++i) {
241 id = f->feature_list[i];
244 if (get_counter(id) == 0) {
245 int ret = register_syscall(id);
247 printk("syscall %d install error, ret = %d\n",
259 static int uninstall_features(struct feature *f)
263 for (i = 0; i < f->cnt; ++i) {
264 id = f->feature_list[i];
266 if (get_counter(id) == 0) {
273 if (get_counter(id) == 0) {
274 int ret = unregister_syscall(id);
287 static struct feature *get_feature(enum feature_id id)
289 if (id < 0 || id >= (int)feature_cnt)
292 return &features[id];
295 int set_feature(enum feature_id id)
299 if (id == FID_SWITCH) {
300 return register_switch_context();
307 return install_features(f);
309 EXPORT_SYMBOL_GPL(set_feature);
311 int unset_feature(enum feature_id id)
315 if (id == FID_SWITCH) {
316 return unregister_switch_context();
323 return uninstall_features(f);
325 EXPORT_SYMBOL_GPL(unset_feature);
327 static int __init init_ks_feature(void)
330 unsigned long addr, ni_syscall;
333 ret = init_switch_context();
337 ni_syscall = swap_ksyms("sys_ni_syscall");
339 for (i = 0; i < syscall_name_cnt; ++i) {
340 name = get_sys_name(i);
341 addr = swap_ksyms(name);
343 printk("%s() not found\n", name);
347 if (ni_syscall == addr) {
348 printk("INFO: %s is not install\n", get_sys_name(i));
352 ksp[i].rp.kp.addr = addr;
358 static void __exit exit_ks_feature(void)
362 for (id = 0; id < syscall_name_cnt; ++id) {
363 if (get_counter(id) > 0)
364 unregister_syscall(id);
367 exit_switch_context();
370 module_init(init_ks_feature);
371 module_exit(exit_ks_feature);
373 MODULE_LICENSE("GPL");
376 static void print_feature(struct feature *f)
380 for (i = 0; i < f->cnt; ++i) {
381 printk(" feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
385 void print_features(void)
389 printk("print_features:\n");
390 for (i = 0; i < feature_cnt; ++i) {
391 printk("feature: %d\n", i);
392 print_feature(&features[i]);
396 void print_all_syscall(void)
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));