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>
24 extern unsigned long swap_sum_time;
25 extern unsigned long swap_sum_hit;
28 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
29 static BLOCKING_NOTIFIER_HEAD(swap_notifier_list);
32 EXPORT_SYMBOL_GPL(gl_nNotifyTgid);
34 DECLARE_WAIT_QUEUE_HEAD (notification_waiters_queue);
35 volatile unsigned notification_count;
37 static int device_mmap (struct file *filp, struct vm_area_struct *vma);
38 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
39 static int device_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
41 static long device_ioctl (struct file *file, unsigned int cmd, unsigned long arg);
43 static int device_open(struct inode *, struct file *);
44 static int device_release(struct inode *, struct file *);
45 static ssize_t device_read(struct file *, char *, size_t, loff_t *);
46 static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
48 static int gl_nDeviceOpened = 0;
49 static struct file_operations device_fops = {
52 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
53 .ioctl = device_ioctl,
55 .unlocked_ioctl = device_ioctl,
58 .write = device_write,
60 .release = device_release
63 typedef void (* dbi_module_callback)();
65 int device_init (void)
68 nReserved = register_chrdev(0, device_name, &device_fops);
71 unregister_chrdev(nReserved, device_name);
72 EPRINTF("Cannot register character device!");
75 EPRINTF("New device node with major number [%d], was created\n", nReserved);
76 device_major = nReserved;
80 void device_down (void)
82 unregister_chrdev(device_major, device_name);
85 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
86 void swap_register_notify (struct notifier_block *nb)
88 blocking_notifier_chain_register(&swap_notifier_list, nb);
90 EXPORT_SYMBOL_GPL(swap_register_notify);
92 void swap_unregister_notify (struct notifier_block *nb)
94 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
96 EXPORT_SYMBOL_GPL(swap_unregister_notify);
99 void notify_user (event_id_t event_id)
101 ec_info.events_counters[event_id] += 1;
103 if (EVENT_EC_PROBE_RECORD == event_id)
105 // EC_PROBE_RECORD events happen to often. To reduce overhead user
106 // space will be notified only once per each EVENTS_AGGREGATION_USEC
107 static uint64_t timestamp_usec = 0;
109 uint64_t current_usec;
114 do_gettimeofday (&tv);
115 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
117 if (current_usec < timestamp_usec)
119 // Note: time from do_gettimeofday() may go backward
120 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
124 delta_usec = current_usec - timestamp_usec;
125 if (EVENTS_AGGREGATION_USEC > delta_usec)
127 // wait the time left
129 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
130 #endif /* defined(__DEBUG) */
131 return; // supress notification
134 timestamp_usec = current_usec; // remember new time for the future use
135 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
136 return; // supress notification
137 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
138 return; // supress notification
141 ++notification_count;
142 wake_up_interruptible (¬ification_waiters_queue);
145 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
148 EPRINTF("Null pointer to buffer!");
151 return remap_vmalloc_range (vma, p_buffer, 0);
154 static int device_open(struct inode *inode, struct file *file)
156 /*if (gl_nDeviceOpened)
160 try_module_get(THIS_MODULE);
164 static int device_release(struct inode *inode, struct file *file)
167 module_put(THIS_MODULE);
171 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
173 EPRINTF("Operation <<read>> not supported!");
177 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
179 EPRINTF("Operation <<write>> not supported!");
183 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
184 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
186 static long device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
189 unsigned long spinlock_flags = 0L;
191 // DPRINTF("Command=%d", cmd);
194 case EC_IOCTL_SET_EC_MODE:
196 ioctl_general_t param;
197 unsigned long nIgnoredBytes = 0;
198 memset(¶m, '0', sizeof(ioctl_general_t));
199 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
200 if (nIgnoredBytes > 0) {
204 if(SetECMode(param.m_unsignedLong) == -1) {
209 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
212 case EC_IOCTL_GET_EC_MODE:
214 ioctl_general_t param;
215 unsigned long nIgnoredBytes = 0;
216 memset(¶m, '0', sizeof(ioctl_general_t));
217 param.m_unsignedLong = GetECMode();
218 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
219 if (nIgnoredBytes > 0) {
224 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
227 case EC_IOCTL_SET_BUFFER_SIZE:
229 ioctl_general_t param;
230 unsigned long nIgnoredBytes = 0;
231 memset(¶m, '0', sizeof(ioctl_general_t));
232 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
233 if (nIgnoredBytes > 0) {
237 if (SetBufferSize(param.m_unsignedLong) == -1) {
242 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
245 case EC_IOCTL_GET_BUFFER_SIZE:
247 ioctl_general_t param;
248 unsigned long nIgnoredBytes = 0;
249 memset(¶m, '0', sizeof(ioctl_general_t));
250 param.m_unsignedLong = GetBufferSize();
251 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
252 if (nIgnoredBytes > 0) {
257 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
260 case EC_IOCTL_RESET_BUFFER:
262 if (ResetBuffer() == -1) {
267 DPRINTF("Reset Buffer");
270 case EC_IOCTL_GET_EC_INFO:
272 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
277 // DPRINTF("Get Buffer Status"); // Frequent call
280 case EC_IOCTL_CONSUME_BUFFER:
282 static ec_info_t ec_info_copy;
283 int nIgnoredBytes = 0;
285 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
286 if(nIgnoredBytes > 0)
288 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
293 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
296 if(ec_info.after_last > ec_info.first) {
297 ec_info.buffer_effect = ec_info.buffer_size;
299 if (ec_info.after_last == ec_info.buffer_effect) {
302 ec_info.first = ec_info_copy.after_last;
304 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
306 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
308 // DPRINTF("Consume Buffer"); // Frequent call
311 case EC_IOCTL_ADD_PROBE:
313 unsigned long addr = arg;
314 result = add_probe(addr);
318 //@AGv: remove_probe expects probe address instead of name
319 /*case EC_IOCTL_REMOVE_PROBE:
321 char *probe_name = (char *) arg;
322 result = remove_probe (probe_name);
326 case EC_IOCTL_SET_APPDEPS:
329 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
331 EPRINTF("Cannot copy deps size!");
335 DPRINTF("Deps size has been copied (%d)", size);
338 DPRINTF("Deps are size of 0");
342 deps = vmalloc(size);
344 EPRINTF("Cannot alloc mem for deps!");
348 DPRINTF("Mem for deps has been allocated");
350 result = copy_from_user(deps, (void *)arg, size);
352 EPRINTF("Cannot copy deps!");
356 DPRINTF("Deps has been copied successfully");
360 case EC_IOCTL_SET_PID:
364 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
366 EPRINTF("Cannot copy pid!");
373 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
377 case EC_IOCTL_SET_PROFILEBUNDLE:
381 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
383 EPRINTF("Cannot copy bundle size!");
387 DPRINTF("Bundle size has been copied");
389 bundle = vmalloc(size);
390 if (bundle == NULL) {
391 EPRINTF("Cannot alloc mem for bundle!");
395 DPRINTF("Mem for bundle has been alloced");
397 result = copy_from_user(bundle, (void *)arg, size);
399 EPRINTF("Cannot copy bundle!");
403 DPRINTF("Bundle has been copied successfully");
405 if (link_bundle() == -1) {
406 EPRINTF("Cannot link profile bundle!");
413 case EC_IOCTL_RESET_PROBES:
415 result = reset_probes();
419 case EC_IOCTL_UPDATE_CONDS:
422 struct cond *c, *c_tmp, *p_cond;
423 unsigned char *p_data;
426 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
431 /* first, delete all the conds */
432 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
436 /* second, add new conds */
437 p_data = (unsigned char *)(arg + sizeof(int));
438 for (i = 0; i < args_cnt; i++) {
439 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
441 DPRINTF("Cannot alloc cond!");
445 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
447 DPRINTF("Cannot copy cond from user!");
452 list_add(&(p_cond->list), &(cond_list.list));
453 p_data += sizeof(struct event_tmpl);
457 case EC_IOCTL_ATTACH:
459 unsigned long dbi_flags;
460 struct dbi_modules_handlers *local_mh;
461 struct dbi_modules_handlers_info *local_mhi;
463 dbi_module_callback dmc_start;
465 // call "start"-callback for all modules according module priority
466 local_mh = get_dbi_modules_handlers();
467 spin_lock_irqsave(&local_mh->lock, dbi_flags);
468 for (j = 0; j <= MAX_PRIORITY; j++) {
469 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
470 if (local_mhi->dbi_module_priority_start == j) {
471 if (local_mhi->dbi_module_callback_start != NULL) {
472 printk("Started module callback (start) %s\n", local_mhi->dbi_module->name);
473 dmc_start = (dbi_module_callback )local_mhi->dbi_module_callback_start;
479 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
481 result = ec_user_attach ();
482 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
483 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
484 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
486 DPRINTF("Attach Probes");
489 case EC_IOCTL_ACTIVATE:
490 result = ec_user_activate ();
491 DPRINTF("Activate Probes");
493 case EC_IOCTL_STOP_AND_DETACH:
495 unsigned long nIgnoredBytes = 0;
496 unsigned long dbi_flags;
497 struct dbi_modules_handlers *local_mh;
498 struct dbi_modules_handlers_info *local_mhi;
499 unsigned int local_module_refcount = 0;
501 dbi_module_callback dmc_stop;
503 #ifdef OVERHEAD_DEBUG
504 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
505 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
509 local_mh = get_dbi_modules_handlers();
510 if(ec_user_stop() != 0) {
514 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
515 if(nIgnoredBytes > 0) {
521 DPRINTF("Stop and Detach Probes");
522 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
523 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
524 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
526 // call "stop"-callback for all modules according module priority
527 spin_lock_irqsave(&local_mh->lock, dbi_flags);
528 for (j = 0; j <= MAX_PRIORITY; j++) {
529 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
530 if (local_mhi->dbi_module_priority_stop == j) {
531 if (local_mhi->dbi_module_callback_stop != NULL) {
532 printk("Started module callback (stop) %s\n", local_mhi->dbi_module->name);
533 dmc_stop = (dbi_module_callback )local_mhi->dbi_module_callback_stop;
539 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
541 spin_lock_irqsave(&local_mh->lock, dbi_flags);
542 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
543 local_module_refcount = module_refcount(local_mhi->dbi_module);
544 if (local_module_refcount == 1) {
545 module_put(local_mhi->dbi_module);
547 else if (local_module_refcount > 1) {
548 printk("local_module_refcount too much - force set refcount to zero\n");
549 while (local_module_refcount--)
550 module_put(local_mhi->dbi_module);
553 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
556 case EC_IOCTL_WAIT_NOTIFICATION:
558 static ec_info_t ec_info_copy;
560 ioctl_wait_notification_t ioctl_args;
562 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
569 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
572 result = -EINTR; // woken by signal (ERESTARTSYS 512)
576 ioctl_args.notification_count = notification_count;
578 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
585 // FIXME: synchronization is necessary here (ec_info must be locked).
586 // ENTER_CRITICAL_SECTION
587 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
588 // LEAVE_CRITICAL_SECTION
590 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
593 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
600 case EC_IOCTL_US_EVENT:
602 ioctl_us_event_t ioctl_args;
603 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
607 EPRINTF ("copy_from_user() failure");
611 if(ioctl_args.len == 0){
613 EPRINTF ("invalid event length!");
616 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
619 EPRINTF ("failed to alloc mem for event!");
622 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
625 EPRINTF ("failed to copy event from user space!");
628 result = put_us_event(buf, ioctl_args.len);
633 // DPRINTF("User Space Event"); // Frequent call
637 case EC_IOCTL_SET_EVENT_MASK:
640 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
647 result = set_event_mask (mask);
652 DPRINTF("Set Event Mask = %d", mask);
656 case EC_IOCTL_GET_EVENT_MASK:
659 result = get_event_mask(&mask);
664 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
669 DPRINTF("Get Event Mask = %d", mask);
673 case EC_IOCTL_SET_PREDEF_UPROBES:
675 ioctl_predef_uprobes_info_t data;
676 result = copy_from_user (&data, (void *) arg, sizeof (data));
683 result = set_predef_uprobes (&data);
688 DPRINTF("Set Predefined User Space Probes");
692 case EC_IOCTL_GET_PREDEF_UPROBES:
694 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
699 DPRINTF("Get Predefined User Space Probes");
703 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
706 result = get_predef_uprobes_size(&size);
711 result = copy_to_user ((void *) arg, &size, sizeof (size));
716 DPRINTF("Get Size of Predefined User Space Probes");
721 EPRINTF ("Unknown driver command = %u", cmd);