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;
289 #ifndef __DISABLE_RELAYFS
290 struct rchan* pRelayChannel = NULL;
291 struct rchan_buf *buf = NULL;
292 unsigned int nNumOfSubbufs = 0;
293 void* pConsume = NULL;
294 unsigned int nPaddingLength = 0;
295 unsigned int nSubbufSize = 0;
296 unsigned int nDataSize = 0;
297 unsigned int nEffectSize = 0;
298 unsigned int nSubbufDiscardedCount = 0;
300 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
301 if(nIgnoredBytes > 0)
303 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
308 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
309 if((ec_info_copy.m_nMode & MODEMASK_MULTIPLE_BUFFER) == 0) {
311 if(ec_info.after_last > ec_info.first) {
312 ec_info.buffer_effect = ec_info.buffer_size;
314 if (ec_info.after_last == ec_info.buffer_effect) {
317 ec_info.first = ec_info_copy.after_last;
319 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
323 #ifndef __DISABLE_RELAYFS
324 pRelayChannel = GetRelayChannel();
325 if(pRelayChannel == NULL) {
326 EPRINTF("Null pointer to relay channel!");
330 buf = pRelayChannel->buf[0];
331 nNumOfSubbufs = pRelayChannel->n_subbufs;
333 nSubbufSize = pRelayChannel->subbuf_size;
334 pConsume = buf->start + buf->subbufs_consumed % nNumOfSubbufs * nSubbufSize;
335 memcpy(&nPaddingLength, pConsume, sizeof(unsigned int));
336 memcpy(&nSubbufDiscardedCount, pConsume + sizeof(unsigned int), sizeof(unsigned int));
337 nEffectSize = nSubbufSize - nPaddingLength;
338 nDataSize = nEffectSize - RELAY_SUBBUF_HEADER_SIZE;
339 relay_subbufs_consumed(pRelayChannel, 0, 1);
340 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
341 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
342 ec_info.buffer_effect -= nEffectSize;
343 ec_info.trace_size -= nDataSize;
344 buf->dentry->d_inode->i_size = ec_info.trace_size;
347 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
349 // DPRINTF("Consume Buffer"); // Frequent call
352 case EC_IOCTL_ADD_PROBE:
354 unsigned long addr = arg;
355 result = add_probe(addr);
359 //@AGv: remove_probe expects probe address instead of name
360 /*case EC_IOCTL_REMOVE_PROBE:
362 char *probe_name = (char *) arg;
363 result = remove_probe (probe_name);
367 case EC_IOCTL_SET_APPDEPS:
370 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
372 EPRINTF("Cannot copy deps size!");
376 DPRINTF("Deps size has been copied (%d)", size);
379 DPRINTF("Deps are size of 0");
383 deps = vmalloc(size);
385 EPRINTF("Cannot alloc mem for deps!");
389 DPRINTF("Mem for deps has been allocated");
391 result = copy_from_user(deps, (void *)arg, size);
393 EPRINTF("Cannot copy deps!");
397 DPRINTF("Deps has been copied successfully");
401 case EC_IOCTL_SET_PID:
405 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
407 EPRINTF("Cannot copy pid!");
414 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
418 case EC_IOCTL_SET_PROFILEBUNDLE:
422 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
424 EPRINTF("Cannot copy bundle size!");
428 DPRINTF("Bundle size has been copied");
430 bundle = vmalloc(size);
431 if (bundle == NULL) {
432 EPRINTF("Cannot alloc mem for bundle!");
436 DPRINTF("Mem for bundle has been alloced");
438 result = copy_from_user(bundle, (void *)arg, size);
440 EPRINTF("Cannot copy bundle!");
444 DPRINTF("Bundle has been copied successfully");
446 if (link_bundle() == -1) {
447 EPRINTF("Cannot link profile bundle!");
454 case EC_IOCTL_RESET_PROBES:
456 result = reset_probes();
460 case EC_IOCTL_UPDATE_CONDS:
463 struct cond *c, *c_tmp, *p_cond;
464 unsigned char *p_data;
467 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
472 /* first, delete all the conds */
473 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
477 /* second, add new conds */
478 p_data = (unsigned char *)(arg + sizeof(int));
479 for (i = 0; i < args_cnt; i++) {
480 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
482 DPRINTF("Cannot alloc cond!");
486 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
488 DPRINTF("Cannot copy cond from user!");
493 list_add(&(p_cond->list), &(cond_list.list));
494 p_data += sizeof(struct event_tmpl);
498 case EC_IOCTL_ATTACH:
500 unsigned long dbi_flags;
501 struct dbi_modules_handlers *local_mh;
502 struct dbi_modules_handlers_info *local_mhi;
504 dbi_module_callback dmc_start;
506 // call "start"-callback for all modules according module priority
507 local_mh = get_dbi_modules_handlers();
508 spin_lock_irqsave(&local_mh->lock, dbi_flags);
509 for (j = 0; j <= MAX_PRIORITY; j++) {
510 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
511 if (local_mhi->dbi_module_priority_start == j) {
512 if (local_mhi->dbi_module_callback_start != NULL) {
513 printk("Started module callback (start) %s\n", local_mhi->dbi_module->name);
514 dmc_start = (dbi_module_callback )local_mhi->dbi_module_callback_start;
520 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
522 result = ec_user_attach ();
523 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
524 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
525 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
527 DPRINTF("Attach Probes");
530 case EC_IOCTL_ACTIVATE:
531 result = ec_user_activate ();
532 DPRINTF("Activate Probes");
534 case EC_IOCTL_STOP_AND_DETACH:
536 unsigned long nIgnoredBytes = 0;
537 unsigned long dbi_flags;
538 struct dbi_modules_handlers *local_mh;
539 struct dbi_modules_handlers_info *local_mhi;
540 unsigned int local_module_refcount = 0;
542 dbi_module_callback dmc_stop;
544 #ifdef OVERHEAD_DEBUG
545 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
546 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
550 local_mh = get_dbi_modules_handlers();
551 if(ec_user_stop() != 0) {
555 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
556 if(nIgnoredBytes > 0) {
562 DPRINTF("Stop and Detach Probes");
563 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
564 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
565 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
567 // call "stop"-callback for all modules according module priority
568 spin_lock_irqsave(&local_mh->lock, dbi_flags);
569 for (j = 0; j <= MAX_PRIORITY; j++) {
570 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
571 if (local_mhi->dbi_module_priority_stop == j) {
572 if (local_mhi->dbi_module_callback_stop != NULL) {
573 printk("Started module callback (stop) %s\n", local_mhi->dbi_module->name);
574 dmc_stop = (dbi_module_callback )local_mhi->dbi_module_callback_stop;
580 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
582 spin_lock_irqsave(&local_mh->lock, dbi_flags);
583 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
584 local_module_refcount = module_refcount(local_mhi->dbi_module);
585 if (local_module_refcount == 1) {
586 module_put(local_mhi->dbi_module);
588 else if (local_module_refcount > 1) {
589 printk("local_module_refcount too much - force set refcount to zero\n");
590 while (local_module_refcount--)
591 module_put(local_mhi->dbi_module);
594 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
597 case EC_IOCTL_WAIT_NOTIFICATION:
599 static ec_info_t ec_info_copy;
601 ioctl_wait_notification_t ioctl_args;
603 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
610 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
613 result = -EINTR; // woken by signal (ERESTARTSYS 512)
617 ioctl_args.notification_count = notification_count;
619 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
626 // FIXME: synchronization is necessary here (ec_info must be locked).
627 // ENTER_CRITICAL_SECTION
628 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
629 // LEAVE_CRITICAL_SECTION
631 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
634 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
641 case EC_IOCTL_US_EVENT:
643 ioctl_us_event_t ioctl_args;
644 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
648 EPRINTF ("copy_from_user() failure");
652 if(ioctl_args.len == 0){
654 EPRINTF ("invalid event length!");
657 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
660 EPRINTF ("failed to alloc mem for event!");
663 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
666 EPRINTF ("failed to copy event from user space!");
669 result = put_us_event(buf, ioctl_args.len);
674 // DPRINTF("User Space Event"); // Frequent call
678 case EC_IOCTL_SET_EVENT_MASK:
681 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
688 result = set_event_mask (mask);
693 DPRINTF("Set Event Mask = %d", mask);
697 case EC_IOCTL_GET_EVENT_MASK:
700 result = get_event_mask(&mask);
705 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
710 DPRINTF("Get Event Mask = %d", mask);
714 case EC_IOCTL_SET_PREDEF_UPROBES:
716 ioctl_predef_uprobes_info_t data;
717 result = copy_from_user (&data, (void *) arg, sizeof (data));
724 result = set_predef_uprobes (&data);
729 DPRINTF("Set Predefined User Space Probes");
733 case EC_IOCTL_GET_PREDEF_UPROBES:
735 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
740 DPRINTF("Get Predefined User Space Probes");
744 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
747 result = get_predef_uprobes_size(&size);
752 result = copy_to_user ((void *) arg, &size, sizeof (size));
757 DPRINTF("Get Size of Predefined User Space Probes");
762 EPRINTF ("Unknown driver command = %u", cmd);