1 ////////////////////////////////////////////////////////////////////////////////////
3 // FILE: device_driver.c
6 // This file is C source for SWAP driver.
8 // SEE ALSO: device_driver.h
9 // AUTHOR: L.Komkov, S.Dianov, S.Grekhov, A.Gerenkov
10 // COMPANY NAME: Samsung Research Center in Moscow
11 // DEPT NAME: Advanced Software Group
12 // CREATED: 2008.02.15
14 // REVISION DATE: 2008.12.03
16 ////////////////////////////////////////////////////////////////////////////////////
19 #include "device_driver.h" // device driver
20 #include "handlers_core.h"
21 #include <linux/notifier.h>
22 #include "sspt/sspt_procs.h"
25 extern unsigned long swap_sum_time;
26 extern unsigned long swap_sum_hit;
30 extern unsigned long imi_sum_time;
31 extern unsigned long imi_sum_hit;
34 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
35 static BLOCKING_NOTIFIER_HEAD(swap_notifier_list);
38 EXPORT_SYMBOL_GPL(gl_nNotifyTgid);
40 DECLARE_WAIT_QUEUE_HEAD (notification_waiters_queue);
41 volatile unsigned notification_count;
43 static int device_mmap (struct file *filp, struct vm_area_struct *vma);
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
45 static int device_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
47 static long device_ioctl (struct file *file, unsigned int cmd, unsigned long arg);
49 static int device_open(struct inode *, struct file *);
50 static int device_release(struct inode *, struct file *);
51 static ssize_t device_read(struct file *, char *, size_t, loff_t *);
52 static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
54 static int gl_nDeviceOpened = 0;
55 static struct file_operations device_fops = {
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
59 .ioctl = device_ioctl,
61 .unlocked_ioctl = device_ioctl,
64 .write = device_write,
66 .release = device_release
69 typedef void (* dbi_module_callback)();
71 int device_init (void)
74 nReserved = register_chrdev(0, device_name, &device_fops);
77 unregister_chrdev(nReserved, device_name);
78 EPRINTF("Cannot register character device!");
81 EPRINTF("New device node with major number [%d], was created\n", nReserved);
82 device_major = nReserved;
86 void device_down (void)
88 unregister_chrdev(device_major, device_name);
91 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
92 void swap_register_notify (struct notifier_block *nb)
94 blocking_notifier_chain_register(&swap_notifier_list, nb);
96 EXPORT_SYMBOL_GPL(swap_register_notify);
98 void swap_unregister_notify (struct notifier_block *nb)
100 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
102 EXPORT_SYMBOL_GPL(swap_unregister_notify);
105 void notify_user (event_id_t event_id)
107 ec_info.events_counters[event_id] += 1;
109 if (EVENT_EC_PROBE_RECORD == event_id)
111 // EC_PROBE_RECORD events happen to often. To reduce overhead user
112 // space will be notified only once per each EVENTS_AGGREGATION_USEC
113 static uint64_t timestamp_usec = 0;
115 uint64_t current_usec;
120 do_gettimeofday (&tv);
121 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
123 if (current_usec < timestamp_usec)
125 // Note: time from do_gettimeofday() may go backward
126 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
130 delta_usec = current_usec - timestamp_usec;
131 if (EVENTS_AGGREGATION_USEC > delta_usec)
133 // wait the time left
135 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
136 #endif /* defined(__DEBUG) */
137 return; // supress notification
140 timestamp_usec = current_usec; // remember new time for the future use
141 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
142 return; // supress notification
143 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
144 return; // supress notification
147 ++notification_count;
148 wake_up_interruptible (¬ification_waiters_queue);
151 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
154 EPRINTF("Null pointer to buffer!");
157 return remap_vmalloc_range (vma, p_buffer, 0);
160 static int device_open(struct inode *inode, struct file *file)
162 /*if (gl_nDeviceOpened)
166 try_module_get(THIS_MODULE);
170 static int device_release(struct inode *inode, struct file *file)
173 module_put(THIS_MODULE);
177 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
179 EPRINTF("Operation <<read>> not supported!");
183 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
185 EPRINTF("Operation <<write>> not supported!");
189 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
190 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
192 static long device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
195 unsigned long spinlock_flags = 0L;
197 // DPRINTF("Command=%d", cmd);
198 printk("#1# Command=%d\n", cmd);
199 printk("#2# Command=%d\n", cmd);
203 case EC_IOCTL_SET_EC_MODE:
205 ioctl_general_t param;
206 unsigned long nIgnoredBytes = 0;
207 memset(¶m, '0', sizeof(ioctl_general_t));
208 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
209 if (nIgnoredBytes > 0) {
213 if(SetECMode(param.m_unsignedLong) == -1) {
218 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
221 case EC_IOCTL_GET_EC_MODE:
223 ioctl_general_t param;
224 unsigned long nIgnoredBytes = 0;
225 memset(¶m, '0', sizeof(ioctl_general_t));
226 param.m_unsignedLong = GetECMode();
227 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
228 if (nIgnoredBytes > 0) {
233 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
236 case EC_IOCTL_SET_BUFFER_SIZE:
238 ioctl_general_t param;
239 unsigned long nIgnoredBytes = 0;
240 memset(¶m, '0', sizeof(ioctl_general_t));
241 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
242 if (nIgnoredBytes > 0) {
246 if (SetBufferSize(param.m_unsignedLong) == -1) {
251 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
254 case EC_IOCTL_GET_BUFFER_SIZE:
256 ioctl_general_t param;
257 unsigned long nIgnoredBytes = 0;
258 memset(¶m, '0', sizeof(ioctl_general_t));
259 param.m_unsignedLong = GetBufferSize();
260 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
261 if (nIgnoredBytes > 0) {
266 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
269 case EC_IOCTL_RESET_BUFFER:
271 if (ResetBuffer() == -1) {
276 DPRINTF("Reset Buffer");
279 case EC_IOCTL_GET_EC_INFO:
281 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
286 // DPRINTF("Get Buffer Status"); // Frequent call
289 case EC_IOCTL_CONSUME_BUFFER:
291 static ec_info_t ec_info_copy;
292 int nIgnoredBytes = 0;
294 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
295 if(nIgnoredBytes > 0)
297 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
302 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
305 if(ec_info.after_last > ec_info.first) {
306 ec_info.buffer_effect = ec_info.buffer_size;
308 if (ec_info.after_last == ec_info.buffer_effect) {
311 ec_info.first = ec_info_copy.after_last;
313 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
315 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
317 // DPRINTF("Consume Buffer"); // Frequent call
320 case EC_IOCTL_ADD_PROBE:
322 unsigned long addr = arg;
323 result = add_probe(addr);
327 //@AGv: remove_probe expects probe address instead of name
328 /*case EC_IOCTL_REMOVE_PROBE:
330 char *probe_name = (char *) arg;
331 result = remove_probe (probe_name);
335 case EC_IOCTL_SET_APPDEPS:
338 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
340 EPRINTF("Cannot copy deps size!");
344 DPRINTF("Deps size has been copied (%d)", size);
347 DPRINTF("Deps are size of 0");
351 deps = vmalloc(size);
353 EPRINTF("Cannot alloc mem for deps!");
357 DPRINTF("Mem for deps has been allocated");
359 result = copy_from_user(deps, (void *)arg, size);
361 EPRINTF("Cannot copy deps!");
365 DPRINTF("Deps has been copied successfully");
369 case EC_IOCTL_SET_PID:
373 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
375 EPRINTF("Cannot copy pid!");
382 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
386 case EC_IOCTL_SET_PROFILEBUNDLE:
390 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
392 EPRINTF("Cannot copy bundle size!");
396 DPRINTF("Bundle size has been copied");
398 bundle = vmalloc(size);
399 if (bundle == NULL) {
400 EPRINTF("Cannot alloc mem for bundle!");
404 DPRINTF("Mem for bundle has been alloced");
406 result = copy_from_user(bundle, (void *)arg, size);
408 EPRINTF("Cannot copy bundle!");
412 DPRINTF("Bundle has been copied successfully");
414 if (link_bundle() == -1) {
415 EPRINTF("Cannot link profile bundle!");
422 case EC_IOCTL_RESET_PROBES:
424 result = reset_probes();
428 case EC_IOCTL_UPDATE_CONDS:
431 struct cond *c, *c_tmp, *p_cond;
432 unsigned char *p_data;
435 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
440 /* first, delete all the conds */
441 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
445 /* second, add new conds */
446 p_data = (unsigned char *)(arg + sizeof(int));
447 for (i = 0; i < args_cnt; i++) {
448 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
450 DPRINTF("Cannot alloc cond!");
454 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
456 DPRINTF("Cannot copy cond from user!");
461 list_add(&(p_cond->list), &(cond_list.list));
462 p_data += sizeof(struct event_tmpl);
466 case EC_IOCTL_ATTACH:
468 unsigned long dbi_flags;
469 struct dbi_modules_handlers *local_mh;
470 struct dbi_modules_handlers_info *local_mhi;
472 dbi_module_callback dmc_start;
474 // call "start"-callback for all modules according module priority
475 local_mh = get_dbi_modules_handlers();
476 spin_lock_irqsave(&local_mh->lock, dbi_flags);
477 for (j = 0; j <= MAX_PRIORITY; j++) {
478 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
479 if (local_mhi->dbi_module_priority_start == j) {
480 if (local_mhi->dbi_module_callback_start != NULL) {
481 printk("Started module callback (start) %s\n", local_mhi->dbi_module->name);
482 dmc_start = (dbi_module_callback )local_mhi->dbi_module_callback_start;
488 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
490 result = ec_user_attach ();
491 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
492 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
493 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
495 DPRINTF("Attach Probes");
498 case EC_IOCTL_ACTIVATE:
499 result = ec_user_activate ();
500 DPRINTF("Activate Probes");
502 case EC_IOCTL_STOP_AND_DETACH:
504 unsigned long nIgnoredBytes = 0;
505 unsigned long dbi_flags;
506 struct dbi_modules_handlers *local_mh;
507 struct dbi_modules_handlers_info *local_mhi;
508 unsigned int local_module_refcount = 0;
510 dbi_module_callback dmc_stop;
512 #ifdef OVERHEAD_DEBUG
513 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
514 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
519 printk("\n### imi_sum_time = %ld in install_mapped_ips()\n", imi_sum_time);
520 printk("### imi_sum_hit = %ld in install_mapped_ips()\n", imi_sum_hit);
522 if (imi_sum_hit != 0) {
523 printk("### time = %ld in install_mapped_ips()\n", imi_sum_time/imi_sum_hit);
529 local_mh = get_dbi_modules_handlers();
530 if(ec_user_stop() != 0) {
534 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
535 if(nIgnoredBytes > 0) {
540 sspt_procs_free_all();
544 DPRINTF("Stop and Detach Probes");
545 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
546 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
547 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
549 // call "stop"-callback for all modules according module priority
550 spin_lock_irqsave(&local_mh->lock, dbi_flags);
551 for (j = 0; j <= MAX_PRIORITY; j++) {
552 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
553 if (local_mhi->dbi_module_priority_stop == j) {
554 if (local_mhi->dbi_module_callback_stop != NULL) {
555 printk("Started module callback (stop) %s\n", local_mhi->dbi_module->name);
556 dmc_stop = (dbi_module_callback )local_mhi->dbi_module_callback_stop;
562 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
564 spin_lock_irqsave(&local_mh->lock, dbi_flags);
565 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
566 local_module_refcount = module_refcount(local_mhi->dbi_module);
567 if (local_module_refcount == 1) {
568 module_put(local_mhi->dbi_module);
570 else if (local_module_refcount > 1) {
571 printk("local_module_refcount too much - force set refcount to zero\n");
572 while (local_module_refcount--)
573 module_put(local_mhi->dbi_module);
576 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
579 case EC_IOCTL_WAIT_NOTIFICATION:
581 static ec_info_t ec_info_copy;
583 ioctl_wait_notification_t ioctl_args;
585 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
592 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
595 result = -EINTR; // woken by signal (ERESTARTSYS 512)
599 ioctl_args.notification_count = notification_count;
601 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
608 // FIXME: synchronization is necessary here (ec_info must be locked).
609 // ENTER_CRITICAL_SECTION
610 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
611 // LEAVE_CRITICAL_SECTION
613 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
616 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
623 case EC_IOCTL_US_EVENT:
625 ioctl_us_event_t ioctl_args;
626 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
630 EPRINTF ("copy_from_user() failure");
634 if(ioctl_args.len == 0){
636 EPRINTF ("invalid event length!");
639 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
642 EPRINTF ("failed to alloc mem for event!");
645 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
648 EPRINTF ("failed to copy event from user space!");
651 result = put_us_event(buf, ioctl_args.len);
656 // DPRINTF("User Space Event"); // Frequent call
660 case EC_IOCTL_SET_EVENT_MASK:
663 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
670 result = set_event_mask (mask);
675 DPRINTF("Set Event Mask = %d", mask);
679 case EC_IOCTL_GET_EVENT_MASK:
682 result = get_event_mask(&mask);
687 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
692 DPRINTF("Get Event Mask = %d", mask);
696 case EC_IOCTL_SET_PREDEF_UPROBES:
701 ioctl_predef_uprobes_info_t data;
702 result = copy_from_user (&data, (void *) arg, sizeof (data));
709 result = set_predef_uprobes (&data);
714 DPRINTF("Set Predefined User Space Probes");
718 case EC_IOCTL_GET_PREDEF_UPROBES:
723 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
728 DPRINTF("Get Predefined User Space Probes");
732 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
735 result = get_predef_uprobes_size(&size);
740 result = copy_to_user ((void *) arg, &size, sizeof (size));
745 DPRINTF("Get Size of Predefined User Space Probes");
750 EPRINTF ("Unknown driver command = %u", cmd);