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 <linux/slab.h>
29 #include <asm/errno.h>
30 #include <ksyms/ksyms.h>
31 #include <kprobe/dbi_kprobes.h>
32 #include <writer/swap_writer_module.h>
33 #include "ks_features.h"
34 #include "syscall_list.h"
35 #include "features_data.c"
44 #define CREATE_RP(name) \
46 .entry_handler = NULL, \
50 #define X(name, args) #name
51 static const char *const syscall_name[] = {
57 syscall_name_cnt = sizeof(syscall_name) / sizeof(char *)
61 #define X(name, args__) \
63 .rp = CREATE_RP(name), \
66 .sub_type = PST_NONE \
69 static struct ks_probe ksp[] = {
74 static const char *get_sys_name(size_t id)
76 return syscall_name[id];
79 static int get_counter(size_t id)
81 return ksp[id].counter;
84 static void inc_counter(size_t id)
89 static void dec_counter(size_t id)
94 /* ========================= HANDLERS ========================= */
95 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
97 struct kretprobe *rp = ri->rp;
100 struct ks_probe *ksp = container_of(rp, struct ks_probe, rp);
101 const char *fmt = ksp->args;
102 unsigned long addr = (unsigned long)ksp->rp.kp.addr;
103 int sub_type = ksp->sub_type;
105 entry_event(fmt, addr, regs, PT_KS, sub_type);
111 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
113 struct kretprobe *rp = ri->rp;
116 unsigned long func_addr = (unsigned long)rp->kp.addr;
117 unsigned long ret_addr = (unsigned long)ri->ret_addr;
119 exit_event('x', regs, func_addr, ret_addr);
124 /* ========================= HANDLERS ========================= */
129 /* ====================== SWITCH_CONTEXT ======================= */
130 static int switch_entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
137 static int switch_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
144 struct kretprobe switch_rp = {
145 .entry_handler = switch_entry_handler,
146 .handler = switch_ret_handler
149 static DEFINE_MUTEX(mutex_sc_enable);
150 static int sc_enable = 0;
152 int init_switch_context(void)
156 addr = swap_ksyms("__switch_to");
158 printk("ERROR: not found '__switch_to'\n");
162 switch_rp.kp.addr = (kprobe_opcode_t *)addr;
167 void exit_switch_context(void)
170 dbi_unregister_kretprobe(&switch_rp);
173 static int register_switch_context(void)
177 mutex_lock(&mutex_sc_enable);
179 printk("switch context profiling is already run!\n");
183 ret = dbi_register_kretprobe(&switch_rp);
188 mutex_unlock(&mutex_sc_enable);
193 static int unregister_switch_context(void)
197 mutex_lock(&mutex_sc_enable);
198 if (sc_enable == 0) {
199 printk("switch context profiling is not running!\n");
204 dbi_unregister_kretprobe(&switch_rp);
208 mutex_unlock(&mutex_sc_enable);
212 /* ====================== SWITCH_CONTEXT ======================= */
218 static int register_syscall(size_t id)
221 printk("register_syscall: %s\n", get_sys_name(id));
223 if (ksp[id].rp.kp.addr == NULL)
226 ksp[id].rp.entry_handler = entry_handler;
227 ksp[id].rp.handler = ret_handler;
229 ret = dbi_register_kretprobe(&ksp[id].rp);
235 static int unregister_syscall(size_t id)
237 printk("unregister_syscall: %s\n", get_sys_name(id));
239 if (ksp[id].rp.kp.addr == NULL)
242 dbi_unregister_kretprobe(&ksp[id].rp);
247 static int unregister_multiple_syscalls(size_t *id_p, size_t cnt)
249 struct kretprobe **rpp;
250 const size_t end = ((size_t) 0) - 1;
255 return unregister_syscall(id_p[0]);
259 rpp = kmalloc(GFP_KERNEL, sizeof(&(((struct ks_probe *) 0)->rp)) * cnt);
261 for (; cnt != end; --cnt) {
262 ret = unregister_syscall(id_p[cnt]);
269 for (; cnt != end; --cnt) {
271 if (ksp[id].rp.kp.addr != NULL) {
272 rpp[i] = &ksp[id].rp;
277 dbi_unregister_kretprobes(rpp, i);
283 static void set_pst(struct feature *f, size_t id)
285 ksp[id].sub_type |= f->sub_type;
288 static void unset_pst(struct feature *f, size_t id)
290 ksp[id].sub_type &= !f->sub_type;
293 static void do_uninstall_features(struct feature *f, size_t i)
299 const size_t end = ((size_t) 0) - 1;
301 id_p = kmalloc(GFP_KERNEL, sizeof(id) * (i + 1));
302 /* NULL check is below in loop */
304 for (; i != end; --i) {
305 id = f->feature_list[i];
307 if (get_counter(id) == 0) {
308 printk("syscall %s not installed\n",
316 if (get_counter(id) == 0) {
321 ret = unregister_syscall(id);
323 printk("syscall %s uninstall error, ret=%d\n",
324 get_sys_name(id), ret);
332 unregister_multiple_syscalls(id_p, cnt);
337 static int do_install_features(struct feature *f)
342 for (i = 0; i < f->cnt; ++i) {
343 id = f->feature_list[i];
346 if (get_counter(id) == 0) {
347 ret = register_syscall(id);
349 printk("syscall %s install error, ret=%d\n",
350 get_sys_name(id), ret);
352 do_uninstall_features(f, --i);
363 static DEFINE_MUTEX(mutex_features);
365 static int install_features(struct feature *f)
369 mutex_lock(&mutex_features);
371 printk("energy profiling is already run!\n");
376 ret = do_install_features(f);
380 mutex_unlock(&mutex_features);
384 static int uninstall_features(struct feature *f)
388 mutex_lock(&mutex_features);
389 if (f->enable == 0) {
390 printk("feature[%d] is not running!\n", feature_index(f));
394 do_uninstall_features(f, f->cnt - 1);
397 mutex_unlock(&mutex_features);
402 static struct feature *get_feature(enum feature_id id)
404 if (id < 0 || id >= (int)feature_cnt)
407 return &features[id];
410 int set_feature(enum feature_id id)
414 if (id == FID_SWITCH) {
415 return register_switch_context();
422 return install_features(f);
424 EXPORT_SYMBOL_GPL(set_feature);
426 int unset_feature(enum feature_id id)
430 if (id == FID_SWITCH) {
431 return unregister_switch_context();
438 return uninstall_features(f);
440 EXPORT_SYMBOL_GPL(unset_feature);
442 static int init_syscall_features(void)
445 unsigned long addr, ni_syscall;
448 ni_syscall = swap_ksyms("sys_ni_syscall");
450 for (i = 0; i < syscall_name_cnt; ++i) {
451 name = get_sys_name(i);
452 addr = swap_ksyms(name);
454 printk("%s() not found\n", name);
458 if (ni_syscall == addr) {
459 printk("INFO: %s is not install\n", name);
463 ksp[i].rp.kp.addr = (kprobe_opcode_t *)addr;
469 static void uninit_syscall_features(void)
473 for (id = 0; id < syscall_name_cnt; ++id) {
474 if (get_counter(id) > 0)
475 unregister_syscall(id);
479 static int __init init_ks_feature(void)
483 ret = init_switch_context();
487 ret = init_syscall_features();
489 exit_switch_context();
494 static void __exit exit_ks_feature(void)
496 uninit_syscall_features();
497 exit_switch_context();
500 module_init(init_ks_feature);
501 module_exit(exit_ks_feature);
503 MODULE_LICENSE("GPL");
506 static void print_feature(struct feature *f)
510 for (i = 0; i < f->cnt; ++i) {
511 printk(" feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
515 void print_features(void)
519 printk("print_features:\n");
520 for (i = 0; i < feature_cnt; ++i) {
521 printk("feature: %d\n", i);
522 print_feature(&features[i]);
526 void print_all_syscall(void)
530 printk("SYSCALL:\n");
531 for (i = 0; i < syscall_name_cnt; ++i) {
532 printk(" [%2d] %s\n", get_counter(i), get_sys_name(i));