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>
28 #include <ksyms/ksyms.h>
29 #include <kprobe/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 unsigned long addr = (unsigned long)ksp->rp.kp.addr;
101 int sub_type = ksp->sub_type;
103 entry_event(fmt, addr, regs, PT_KS, sub_type);
109 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
111 struct kretprobe *rp = ri->rp;
114 unsigned long func_addr = (unsigned long)rp->kp.addr;
115 unsigned long ret_addr = (unsigned long)ri->ret_addr;
117 exit_event('x', regs, func_addr, ret_addr);
122 /* ========================= HANDLERS ========================= */
127 /* ====================== SWITCH_CONTEXT ======================= */
128 static int switch_entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
135 static int switch_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
142 struct kretprobe switch_rp = {
143 .entry_handler = switch_entry_handler,
144 .handler = switch_ret_handler
147 static DEFINE_MUTEX(mutex_sc_enable);
148 static int sc_enable = 0;
150 int init_switch_context(void)
154 addr = swap_ksyms("__switch_to");
156 printk("ERROR: not found '__switch_to'\n");
160 switch_rp.kp.addr = (kprobe_opcode_t *)addr;
165 void exit_switch_context(void)
168 dbi_unregister_kretprobe(&switch_rp);
171 static int register_switch_context(void)
175 mutex_lock(&mutex_sc_enable);
177 printk("switch context profiling is already run!\n");
181 ret = dbi_register_kretprobe(&switch_rp);
186 mutex_unlock(&mutex_sc_enable);
191 static int unregister_switch_context(void)
195 mutex_lock(&mutex_sc_enable);
196 if (sc_enable == 0) {
197 printk("switch context profiling is not running!\n");
202 dbi_unregister_kretprobe(&switch_rp);
206 mutex_unlock(&mutex_sc_enable);
210 /* ====================== SWITCH_CONTEXT ======================= */
216 static int register_syscall(size_t id)
219 printk("register_syscall: %s\n", get_sys_name(id));
221 if (ksp[id].rp.kp.addr == NULL)
224 ksp[id].rp.entry_handler = entry_handler;
225 ksp[id].rp.handler = ret_handler;
227 ret = dbi_register_kretprobe(&ksp[id].rp);
232 static int unregister_syscall(size_t id)
234 printk("unregister_syscall: %s\n", get_sys_name(id));
236 if (ksp[id].rp.kp.addr == NULL)
239 dbi_unregister_kretprobe(&ksp[id].rp);
244 static void set_pst(struct feature *f, size_t id)
246 ksp[id].sub_type |= f->sub_type;
249 static void unset_pst(struct feature *f, size_t id)
251 ksp[id].sub_type &= !f->sub_type;
254 static void do_uninstall_features(struct feature *f, size_t i)
258 const size_t end = ((size_t) 0) - 1;
260 for (; i != end; --i) {
261 id = f->feature_list[i];
263 if (get_counter(id) == 0) {
264 printk("syscall %s not installed\n",
271 if (get_counter(id) == 0) {
272 ret = unregister_syscall(id);
274 printk("syscall %s uninstall error, ret=%d\n",
275 get_sys_name(id), ret);
283 static int do_install_features(struct feature *f)
288 for (i = 0; i < f->cnt; ++i) {
289 id = f->feature_list[i];
292 if (get_counter(id) == 0) {
293 ret = register_syscall(id);
295 printk("syscall %s install error, ret=%d\n",
296 get_sys_name(id), ret);
298 do_uninstall_features(f, --i);
309 static DEFINE_MUTEX(mutex_features);
311 static int install_features(struct feature *f)
315 mutex_lock(&mutex_features);
317 printk("energy profiling is already run!\n");
322 ret = do_install_features(f);
326 mutex_unlock(&mutex_features);
330 static int uninstall_features(struct feature *f)
334 mutex_lock(&mutex_features);
335 if (f->enable == 0) {
336 printk("feature[%d] is not running!\n", feature_index(f));
341 do_uninstall_features(f, f->cnt - 1);
345 mutex_unlock(&mutex_features);
349 static struct feature *get_feature(enum feature_id id)
351 if (id < 0 || id >= (int)feature_cnt)
354 return &features[id];
357 int set_feature(enum feature_id id)
361 if (id == FID_SWITCH) {
362 return register_switch_context();
369 return install_features(f);
371 EXPORT_SYMBOL_GPL(set_feature);
373 int unset_feature(enum feature_id id)
377 if (id == FID_SWITCH) {
378 return unregister_switch_context();
385 return uninstall_features(f);
387 EXPORT_SYMBOL_GPL(unset_feature);
389 static int init_syscall_features(void)
392 unsigned long addr, ni_syscall;
395 ni_syscall = swap_ksyms("sys_ni_syscall");
397 for (i = 0; i < syscall_name_cnt; ++i) {
398 name = get_sys_name(i);
399 addr = swap_ksyms(name);
401 printk("%s() not found\n", name);
405 if (ni_syscall == addr) {
406 printk("INFO: %s is not install\n", name);
410 ksp[i].rp.kp.addr = (kprobe_opcode_t *)addr;
416 static void uninit_syscall_features(void)
420 for (id = 0; id < syscall_name_cnt; ++id) {
421 if (get_counter(id) > 0)
422 unregister_syscall(id);
426 static int __init init_ks_feature(void)
430 ret = init_switch_context();
434 ret = init_syscall_features();
436 exit_switch_context();
441 static void __exit exit_ks_feature(void)
443 uninit_syscall_features();
444 exit_switch_context();
447 module_init(init_ks_feature);
448 module_exit(exit_ks_feature);
450 MODULE_LICENSE("GPL");
453 static void print_feature(struct feature *f)
457 for (i = 0; i < f->cnt; ++i) {
458 printk(" feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
462 void print_features(void)
466 printk("print_features:\n");
467 for (i = 0; i < feature_cnt; ++i) {
468 printk("feature: %d\n", i);
469 print_feature(&features[i]);
473 void print_all_syscall(void)
477 printk("SYSCALL:\n");
478 for (i = 0; i < syscall_name_cnt; ++i) {
479 printk(" [%2d] %s\n", get_counter(i), get_sys_name(i));