2 * ks_features/ks_features.c
3 * @author Vyacheslav Cherkashin: SWAP ks_features implement
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright (C) Samsung Electronics, 2013
25 * @section DESCRIPTION
27 * SWAP kernel features
31 #include <linux/module.h>
32 #include <linux/slab.h>
34 #include <asm/errno.h>
35 #include <ksyms/ksyms.h>
36 #include <kprobe/swap_kprobes.h>
37 #include <writer/swap_writer_module.h>
38 #include <writer/event_filter.h>
39 #include "ks_features.h"
40 #include "syscall_list.h"
41 #include "features_data.c"
46 * @brief Kernel-space probe. Struct used as a container of syscall probes.
48 * Pointer to kretprobe.
49 * @var ks_probe::counter
50 * Installed probes counter.
52 * Pointer to args format string.
53 * @var ks_probe::sub_type
63 #define CREATE_RP(name) \
65 .entry_handler = NULL, \
69 #define X(name, args) #name
70 static const char *const syscall_name[] = {
77 * Syscall name count defenition
80 syscall_name_cnt = sizeof(syscall_name) / sizeof(char *)
84 #define X(name, args__) \
86 .rp = CREATE_RP(name), \
89 .sub_type = PST_NONE \
92 static struct ks_probe ksp[] = {
97 static const char *get_sys_name(size_t id)
99 return syscall_name[id];
102 static int get_counter(size_t id)
104 return ksp[id].counter;
107 static void inc_counter(size_t id)
112 static void dec_counter(size_t id)
117 /* ========================= HANDLERS ========================= */
118 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
120 struct kretprobe *rp = ri->rp;
122 if (rp && check_event(current)) {
123 struct ks_probe *ksp = container_of(rp, struct ks_probe, rp);
124 const char *fmt = ksp->args;
125 unsigned long addr = (unsigned long)ksp->rp.kp.addr;
126 int sub_type = ksp->sub_type;
128 entry_event(fmt, addr, regs, PT_KS, sub_type);
134 static int ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
136 struct kretprobe *rp = ri->rp;
138 if (rp && check_event(current)) {
139 struct ks_probe *ksp = container_of(rp, struct ks_probe, rp);
140 unsigned long func_addr = (unsigned long)rp->kp.addr;
141 unsigned long ret_addr = (unsigned long)ri->ret_addr;
142 int sub_type = ksp->sub_type;
144 exit_event('x', regs, PT_KS, sub_type, func_addr, ret_addr);
149 /* ========================= HANDLERS ========================= */
154 /* ====================== SWITCH_CONTEXT ======================= */
155 static int switch_entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
157 if (check_event(current))
163 static int switch_ret_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
165 if (check_event(current))
173 * Kretprobe for scheduler.
175 struct kretprobe switch_rp = {
176 .entry_handler = switch_entry_handler,
177 .handler = switch_ret_handler
180 static DEFINE_MUTEX(mutex_sc_enable);
181 static int sc_enable = 0;
184 * @brief Get scheduler address.
186 * @return 0 on success, negative error code on error.
188 int init_switch_context(void)
192 addr = swap_ksyms("__switch_to");
194 printk("ERROR: not found '__switch_to'\n");
198 switch_rp.kp.addr = (kprobe_opcode_t *)addr;
204 * @brief Unregisters probe on context switching.
208 void exit_switch_context(void)
211 swap_unregister_kretprobe(&switch_rp);
214 static int register_switch_context(void)
218 mutex_lock(&mutex_sc_enable);
220 printk("switch context profiling is already run!\n");
224 ret = swap_register_kretprobe(&switch_rp);
229 mutex_unlock(&mutex_sc_enable);
234 static int unregister_switch_context(void)
238 mutex_lock(&mutex_sc_enable);
239 if (sc_enable == 0) {
240 printk("switch context profiling is not running!\n");
245 swap_unregister_kretprobe(&switch_rp);
249 mutex_unlock(&mutex_sc_enable);
253 /* ====================== SWITCH_CONTEXT ======================= */
259 static int register_syscall(size_t id)
262 printk("register_syscall: %s\n", get_sys_name(id));
264 if (ksp[id].rp.kp.addr == NULL)
267 ksp[id].rp.entry_handler = entry_handler;
268 ksp[id].rp.handler = ret_handler;
270 ret = swap_register_kretprobe(&ksp[id].rp);
276 static int unregister_syscall(size_t id)
278 printk("unregister_syscall: %s\n", get_sys_name(id));
280 if (ksp[id].rp.kp.addr == NULL)
283 swap_unregister_kretprobe(&ksp[id].rp);
288 static int unregister_multiple_syscalls(size_t *id_p, size_t cnt)
290 struct kretprobe **rpp;
291 const size_t end = ((size_t) 0) - 1;
296 return unregister_syscall(id_p[0]);
300 rpp = kmalloc(GFP_KERNEL, sizeof(&(((struct ks_probe *) 0)->rp)) * cnt);
302 for (; cnt != end; --cnt) {
303 ret = unregister_syscall(id_p[cnt]);
310 for (; cnt != end; --cnt) {
312 if (ksp[id].rp.kp.addr != NULL) {
313 rpp[i] = &ksp[id].rp;
318 swap_unregister_kretprobes(rpp, i);
324 static void set_pst(struct feature *f, size_t id)
326 ksp[id].sub_type |= f->sub_type;
329 static void unset_pst(struct feature *f, size_t id)
331 ksp[id].sub_type &= !f->sub_type;
334 static void do_uninstall_features(struct feature *f, size_t i)
340 const size_t end = ((size_t) 0) - 1;
342 id_p = kmalloc(GFP_KERNEL, sizeof(id) * (i + 1));
343 /* NULL check is below in loop */
345 for (; i != end; --i) {
346 id = f->feature_list[i];
348 if (get_counter(id) == 0) {
349 printk("syscall %s not installed\n",
357 if (get_counter(id) == 0) {
362 ret = unregister_syscall(id);
364 printk("syscall %s uninstall error, ret=%d\n",
365 get_sys_name(id), ret);
373 unregister_multiple_syscalls(id_p, cnt);
378 static int do_install_features(struct feature *f)
383 for (i = 0; i < f->cnt; ++i) {
384 id = f->feature_list[i];
387 if (get_counter(id) == 0) {
388 ret = register_syscall(id);
390 printk("syscall %s install error, ret=%d\n",
391 get_sys_name(id), ret);
393 do_uninstall_features(f, --i);
404 static DEFINE_MUTEX(mutex_features);
406 static int install_features(struct feature *f)
410 mutex_lock(&mutex_features);
412 printk("energy profiling is already run!\n");
417 ret = do_install_features(f);
421 mutex_unlock(&mutex_features);
425 static int uninstall_features(struct feature *f)
429 mutex_lock(&mutex_features);
430 if (f->enable == 0) {
431 printk("feature[%d] is not running!\n", feature_index(f));
435 do_uninstall_features(f, f->cnt - 1);
438 mutex_unlock(&mutex_features);
443 static struct feature *get_feature(enum feature_id id)
445 if (id < 0 || id >= (int)feature_cnt)
448 return &features[id];
452 * @brief Sets probes related to specified feature.
454 * @param id Feature id.
455 * @return 0 on success, negative error code on error.
457 int set_feature(enum feature_id id)
464 ret = file_ops_init();
467 ret = register_switch_context();
471 ret = f ? install_features(f): -EINVAL;
477 EXPORT_SYMBOL_GPL(set_feature);
480 * @brief Unsets probes related to specified feature.
482 * @param id Feature id.
483 * @return 0 on success, negative error code on error.
485 int unset_feature(enum feature_id id)
495 ret = unregister_switch_context();
499 ret = f ? uninstall_features(f): -EINVAL;
505 EXPORT_SYMBOL_GPL(unset_feature);
507 static int init_syscall_features(void)
510 unsigned long addr, ni_syscall;
513 ni_syscall = swap_ksyms("sys_ni_syscall");
515 for (i = 0; i < syscall_name_cnt; ++i) {
516 name = get_sys_name(i);
517 addr = swap_ksyms(name);
519 printk("INFO: %s() not found\n", name);
520 } else if (ni_syscall == addr) {
521 printk("INFO: %s is not install\n", name);
525 ksp[i].rp.kp.addr = (kprobe_opcode_t *)addr;
531 static void uninit_syscall_features(void)
535 for (id = 0; id < syscall_name_cnt; ++id) {
536 if (get_counter(id) > 0)
537 unregister_syscall(id);
541 static int __init init_ks_feature(void)
545 ret = init_switch_context();
549 ret = init_syscall_features();
551 exit_switch_context();
556 static void __exit exit_ks_feature(void)
558 uninit_syscall_features();
559 exit_switch_context();
562 module_init(init_ks_feature);
563 module_exit(exit_ks_feature);
565 MODULE_LICENSE("GPL");
568 static void print_feature(struct feature *f)
572 for (i = 0; i < f->cnt; ++i) {
573 printk(" feature[%3u]: %s\n", i, get_sys_name(f->feature_list[i]));
578 * @brief Prints features.
582 void print_features(void)
586 printk("print_features:\n");
587 for (i = 0; i < feature_cnt; ++i) {
588 printk("feature: %d\n", i);
589 print_feature(&features[i]);
594 * @brief Prints all syscalls.
598 void print_all_syscall(void)
602 printk("SYSCALL:\n");
603 for (i = 0; i < syscall_name_cnt; ++i) {
604 printk(" [%2d] %s\n", get_counter(i), get_sys_name(i));