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 int nRetVal = register_chrdev(device_major, device_name, &device_fops);
70 EPRINTF("Cannot register character device! [%s, %d]", device_name, device_major);
71 nReserved = register_chrdev(0, device_name, &device_fops);
74 unregister_chrdev(nReserved, device_name);
75 EPRINTF("Please, create a new device node with major number [%d],\n\tand pass it as module parameter!", nReserved);
78 } else if(nRetVal > 0) {
79 EPRINTF("Cannot register this device major number! [%d]\n\tTrying a new one. [%d]", device_major, nRetVal);
80 device_major = nRetVal;
85 void device_down (void)
87 unregister_chrdev(device_major, device_name);
90 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
91 void swap_register_notify (struct notifier_block *nb)
93 blocking_notifier_chain_register(&swap_notifier_list, nb);
95 EXPORT_SYMBOL_GPL(swap_register_notify);
97 void swap_unregister_notify (struct notifier_block *nb)
99 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
101 EXPORT_SYMBOL_GPL(swap_unregister_notify);
104 void notify_user (event_id_t event_id)
106 ec_info.events_counters[event_id] += 1;
108 if (EVENT_EC_PROBE_RECORD == event_id)
110 // EC_PROBE_RECORD events happen to often. To reduce overhead user
111 // space will be notified only once per each EVENTS_AGGREGATION_USEC
112 static uint64_t timestamp_usec = 0;
114 uint64_t current_usec;
119 do_gettimeofday (&tv);
120 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
122 if (current_usec < timestamp_usec)
124 // Note: time from do_gettimeofday() may go backward
125 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
129 delta_usec = current_usec - timestamp_usec;
130 if (EVENTS_AGGREGATION_USEC > delta_usec)
132 // wait the time left
134 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
135 #endif /* defined(__DEBUG) */
136 return; // supress notification
139 timestamp_usec = current_usec; // remember new time for the future use
140 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
141 return; // supress notification
142 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
143 return; // supress notification
146 ++notification_count;
147 wake_up_interruptible (¬ification_waiters_queue);
150 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
153 EPRINTF("Null pointer to buffer!");
156 return remap_vmalloc_range (vma, p_buffer, 0);
159 static int device_open(struct inode *inode, struct file *file)
161 /*if (gl_nDeviceOpened)
165 try_module_get(THIS_MODULE);
169 static int device_release(struct inode *inode, struct file *file)
172 module_put(THIS_MODULE);
176 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
178 EPRINTF("Operation <<read>> not supported!");
182 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
184 EPRINTF("Operation <<write>> not supported!");
188 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
189 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
191 static long device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
194 unsigned long spinlock_flags = 0L;
196 // DPRINTF("Command=%d", cmd);
199 case EC_IOCTL_SET_EC_MODE:
201 ioctl_general_t param;
202 unsigned long nIgnoredBytes = 0;
203 memset(¶m, '0', sizeof(ioctl_general_t));
204 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
205 if (nIgnoredBytes > 0) {
209 if(SetECMode(param.m_unsignedLong) == -1) {
214 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
217 case EC_IOCTL_GET_EC_MODE:
219 ioctl_general_t param;
220 unsigned long nIgnoredBytes = 0;
221 memset(¶m, '0', sizeof(ioctl_general_t));
222 param.m_unsignedLong = GetECMode();
223 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
224 if (nIgnoredBytes > 0) {
229 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
232 case EC_IOCTL_SET_BUFFER_SIZE:
234 ioctl_general_t param;
235 unsigned long nIgnoredBytes = 0;
236 memset(¶m, '0', sizeof(ioctl_general_t));
237 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
238 if (nIgnoredBytes > 0) {
242 if (SetBufferSize(param.m_unsignedLong) == -1) {
247 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
250 case EC_IOCTL_GET_BUFFER_SIZE:
252 ioctl_general_t param;
253 unsigned long nIgnoredBytes = 0;
254 memset(¶m, '0', sizeof(ioctl_general_t));
255 param.m_unsignedLong = GetBufferSize();
256 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
257 if (nIgnoredBytes > 0) {
262 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
265 case EC_IOCTL_RESET_BUFFER:
267 if (ResetBuffer() == -1) {
272 DPRINTF("Reset Buffer");
275 case EC_IOCTL_GET_EC_INFO:
277 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
282 // DPRINTF("Get Buffer Status"); // Frequent call
285 case EC_IOCTL_CONSUME_BUFFER:
287 static ec_info_t ec_info_copy;
288 int nIgnoredBytes = 0;
290 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
291 if(nIgnoredBytes > 0)
293 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
298 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
301 if(ec_info.after_last > ec_info.first) {
302 ec_info.buffer_effect = ec_info.buffer_size;
304 if (ec_info.after_last == ec_info.buffer_effect) {
307 ec_info.first = ec_info_copy.after_last;
309 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
311 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
313 // DPRINTF("Consume Buffer"); // Frequent call
316 case EC_IOCTL_ADD_PROBE:
318 unsigned long addr = arg;
319 result = add_probe(addr);
323 //@AGv: remove_probe expects probe address instead of name
324 /*case EC_IOCTL_REMOVE_PROBE:
326 char *probe_name = (char *) arg;
327 result = remove_probe (probe_name);
331 case EC_IOCTL_SET_APPDEPS:
334 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
336 EPRINTF("Cannot copy deps size!");
340 DPRINTF("Deps size has been copied (%d)", size);
343 DPRINTF("Deps are size of 0");
347 deps = vmalloc(size);
349 EPRINTF("Cannot alloc mem for deps!");
353 DPRINTF("Mem for deps has been allocated");
355 result = copy_from_user(deps, (void *)arg, size);
357 EPRINTF("Cannot copy deps!");
361 DPRINTF("Deps has been copied successfully");
365 case EC_IOCTL_SET_PID:
369 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
371 EPRINTF("Cannot copy pid!");
378 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
382 case EC_IOCTL_SET_PROFILEBUNDLE:
386 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
388 EPRINTF("Cannot copy bundle size!");
392 DPRINTF("Bundle size has been copied");
394 bundle = vmalloc(size);
395 if (bundle == NULL) {
396 EPRINTF("Cannot alloc mem for bundle!");
400 DPRINTF("Mem for bundle has been alloced");
402 result = copy_from_user(bundle, (void *)arg, size);
404 EPRINTF("Cannot copy bundle!");
408 DPRINTF("Bundle has been copied successfully");
410 if (link_bundle() == -1) {
411 EPRINTF("Cannot link profile bundle!");
418 case EC_IOCTL_RESET_PROBES:
420 result = reset_probes();
424 case EC_IOCTL_UPDATE_CONDS:
427 struct cond *c, *c_tmp, *p_cond;
428 unsigned char *p_data;
431 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
436 /* first, delete all the conds */
437 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
441 /* second, add new conds */
442 p_data = (unsigned char *)(arg + sizeof(int));
443 for (i = 0; i < args_cnt; i++) {
444 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
446 DPRINTF("Cannot alloc cond!");
450 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
452 DPRINTF("Cannot copy cond from user!");
457 list_add(&(p_cond->list), &(cond_list.list));
458 p_data += sizeof(struct event_tmpl);
462 case EC_IOCTL_ATTACH:
464 unsigned long dbi_flags;
465 struct dbi_modules_handlers *local_mh;
466 struct dbi_modules_handlers_info *local_mhi;
468 dbi_module_callback dmc_start;
470 // call "start"-callback for all modules according module priority
471 local_mh = get_dbi_modules_handlers();
472 spin_lock_irqsave(&local_mh->lock, dbi_flags);
473 for (j = 0; j <= MAX_PRIORITY; j++) {
474 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
475 if (local_mhi->dbi_module_priority_start == j) {
476 if (local_mhi->dbi_module_callback_start != NULL) {
477 printk("Started module callback (start) %s\n", local_mhi->dbi_module->name);
478 dmc_start = (dbi_module_callback )local_mhi->dbi_module_callback_start;
484 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
486 result = ec_user_attach ();
487 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
488 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
489 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
491 DPRINTF("Attach Probes");
494 case EC_IOCTL_ACTIVATE:
495 result = ec_user_activate ();
496 DPRINTF("Activate Probes");
498 case EC_IOCTL_STOP_AND_DETACH:
500 unsigned long nIgnoredBytes = 0;
501 unsigned long dbi_flags;
502 struct dbi_modules_handlers *local_mh;
503 struct dbi_modules_handlers_info *local_mhi;
504 unsigned int local_module_refcount = 0;
506 dbi_module_callback dmc_stop;
508 #ifdef OVERHEAD_DEBUG
509 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
510 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
514 local_mh = get_dbi_modules_handlers();
515 if(ec_user_stop() != 0) {
519 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
520 if(nIgnoredBytes > 0) {
526 DPRINTF("Stop and Detach Probes");
527 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
528 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
529 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
531 // call "stop"-callback for all modules according module priority
532 spin_lock_irqsave(&local_mh->lock, dbi_flags);
533 for (j = 0; j <= MAX_PRIORITY; j++) {
534 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
535 if (local_mhi->dbi_module_priority_stop == j) {
536 if (local_mhi->dbi_module_callback_stop != NULL) {
537 printk("Started module callback (stop) %s\n", local_mhi->dbi_module->name);
538 dmc_stop = (dbi_module_callback )local_mhi->dbi_module_callback_stop;
544 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
546 spin_lock_irqsave(&local_mh->lock, dbi_flags);
547 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
548 local_module_refcount = module_refcount(local_mhi->dbi_module);
549 if (local_module_refcount == 1) {
550 module_put(local_mhi->dbi_module);
552 else if (local_module_refcount > 1) {
553 printk("local_module_refcount too much - force set refcount to zero\n");
554 while (local_module_refcount--)
555 module_put(local_mhi->dbi_module);
558 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
561 case EC_IOCTL_WAIT_NOTIFICATION:
563 static ec_info_t ec_info_copy;
565 ioctl_wait_notification_t ioctl_args;
567 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
574 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
577 result = -EINTR; // woken by signal (ERESTARTSYS 512)
581 ioctl_args.notification_count = notification_count;
583 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
590 // FIXME: synchronization is necessary here (ec_info must be locked).
591 // ENTER_CRITICAL_SECTION
592 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
593 // LEAVE_CRITICAL_SECTION
595 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
598 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
605 case EC_IOCTL_US_EVENT:
607 ioctl_us_event_t ioctl_args;
608 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
612 EPRINTF ("copy_from_user() failure");
616 if(ioctl_args.len == 0){
618 EPRINTF ("invalid event length!");
621 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
624 EPRINTF ("failed to alloc mem for event!");
627 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
630 EPRINTF ("failed to copy event from user space!");
633 result = put_us_event(buf, ioctl_args.len);
638 // DPRINTF("User Space Event"); // Frequent call
642 case EC_IOCTL_SET_EVENT_MASK:
645 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
652 result = set_event_mask (mask);
657 DPRINTF("Set Event Mask = %d", mask);
661 case EC_IOCTL_GET_EVENT_MASK:
664 result = get_event_mask(&mask);
669 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
674 DPRINTF("Get Event Mask = %d", mask);
678 case EC_IOCTL_SET_PREDEF_UPROBES:
680 ioctl_predef_uprobes_info_t data;
681 result = copy_from_user (&data, (void *) arg, sizeof (data));
688 result = set_predef_uprobes (&data);
693 DPRINTF("Set Predefined User Space Probes");
697 case EC_IOCTL_GET_PREDEF_UPROBES:
699 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
704 DPRINTF("Get Predefined User Space Probes");
708 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
711 result = get_predef_uprobes_size(&size);
716 result = copy_to_user ((void *) arg, &size, sizeof (size));
721 DPRINTF("Get Size of Predefined User Space Probes");
726 EPRINTF ("Unknown driver command = %u", cmd);