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;
29 extern unsigned long imi_sum_time;
30 extern unsigned long imi_sum_hit;
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
34 static BLOCKING_NOTIFIER_HEAD(swap_notifier_list);
37 EXPORT_SYMBOL_GPL(gl_nNotifyTgid);
39 DECLARE_WAIT_QUEUE_HEAD (notification_waiters_queue);
40 volatile unsigned notification_count;
42 static int device_mmap (struct file *filp, struct vm_area_struct *vma);
43 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
44 static int device_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
46 static long device_ioctl (struct file *file, unsigned int cmd, unsigned long arg);
48 static int device_open(struct inode *, struct file *);
49 static int device_release(struct inode *, struct file *);
50 static ssize_t device_read(struct file *, char *, size_t, loff_t *);
51 static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
53 static int gl_nDeviceOpened = 0;
54 static struct file_operations device_fops = {
57 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
58 .ioctl = device_ioctl,
60 .unlocked_ioctl = device_ioctl,
63 .write = device_write,
65 .release = device_release
68 typedef void (* dbi_module_callback)();
70 int device_init (void)
73 int nRetVal = register_chrdev(device_major, device_name, &device_fops);
75 EPRINTF("Cannot register character device! [%s, %d]", device_name, device_major);
76 nReserved = register_chrdev(0, device_name, &device_fops);
79 unregister_chrdev(nReserved, device_name);
80 EPRINTF("Please, create a new device node with major number [%d],\n\tand pass it as module parameter!", nReserved);
83 } else if(nRetVal > 0) {
84 EPRINTF("Cannot register this device major number! [%d]\n\tTrying a new one. [%d]", device_major, nRetVal);
85 device_major = nRetVal;
90 void device_down (void)
92 unregister_chrdev(device_major, device_name);
95 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
96 void swap_register_notify (struct notifier_block *nb)
98 blocking_notifier_chain_register(&swap_notifier_list, nb);
100 EXPORT_SYMBOL_GPL(swap_register_notify);
102 void swap_unregister_notify (struct notifier_block *nb)
104 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
106 EXPORT_SYMBOL_GPL(swap_unregister_notify);
109 void notify_user (event_id_t event_id)
111 ec_info.events_counters[event_id] += 1;
113 if (EVENT_EC_PROBE_RECORD == event_id)
115 // EC_PROBE_RECORD events happen to often. To reduce overhead user
116 // space will be notified only once per each EVENTS_AGGREGATION_USEC
117 static uint64_t timestamp_usec = 0;
119 uint64_t current_usec;
124 do_gettimeofday (&tv);
125 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
127 if (current_usec < timestamp_usec)
129 // Note: time from do_gettimeofday() may go backward
130 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
134 delta_usec = current_usec - timestamp_usec;
135 if (EVENTS_AGGREGATION_USEC > delta_usec)
137 // wait the time left
139 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
140 #endif /* defined(__DEBUG) */
141 return; // supress notification
144 timestamp_usec = current_usec; // remember new time for the future use
145 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
146 return; // supress notification
147 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
148 return; // supress notification
151 ++notification_count;
152 wake_up_interruptible (¬ification_waiters_queue);
155 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
158 EPRINTF("Null pointer to buffer!");
161 return remap_vmalloc_range (vma, p_buffer, 0);
164 static int device_open(struct inode *inode, struct file *file)
166 /*if (gl_nDeviceOpened)
170 try_module_get(THIS_MODULE);
174 static int device_release(struct inode *inode, struct file *file)
177 module_put(THIS_MODULE);
181 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
183 EPRINTF("Operation <<read>> not supported!");
187 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
189 EPRINTF("Operation <<write>> not supported!");
193 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
194 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
196 static long device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
199 unsigned long spinlock_flags = 0L;
201 // DPRINTF("Command=%d", cmd);
204 case EC_IOCTL_SET_EC_MODE:
206 ioctl_general_t param;
207 unsigned long nIgnoredBytes = 0;
208 memset(¶m, '0', sizeof(ioctl_general_t));
209 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
210 if (nIgnoredBytes > 0) {
214 if(SetECMode(param.m_unsignedLong) == -1) {
219 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
222 case EC_IOCTL_GET_EC_MODE:
224 ioctl_general_t param;
225 unsigned long nIgnoredBytes = 0;
226 memset(¶m, '0', sizeof(ioctl_general_t));
227 param.m_unsignedLong = GetECMode();
228 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
229 if (nIgnoredBytes > 0) {
234 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
237 case EC_IOCTL_SET_BUFFER_SIZE:
239 ioctl_general_t param;
240 unsigned long nIgnoredBytes = 0;
241 memset(¶m, '0', sizeof(ioctl_general_t));
242 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
243 if (nIgnoredBytes > 0) {
247 if (SetBufferSize(param.m_unsignedLong) == -1) {
252 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
255 case EC_IOCTL_GET_BUFFER_SIZE:
257 ioctl_general_t param;
258 unsigned long nIgnoredBytes = 0;
259 memset(¶m, '0', sizeof(ioctl_general_t));
260 param.m_unsignedLong = GetBufferSize();
261 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
262 if (nIgnoredBytes > 0) {
267 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
270 case EC_IOCTL_RESET_BUFFER:
272 if (ResetBuffer() == -1) {
277 DPRINTF("Reset Buffer");
280 case EC_IOCTL_GET_EC_INFO:
282 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
287 // DPRINTF("Get Buffer Status"); // Frequent call
290 case EC_IOCTL_CONSUME_BUFFER:
292 static ec_info_t ec_info_copy;
293 int nIgnoredBytes = 0;
294 #ifndef __DISABLE_RELAYFS
295 struct rchan* pRelayChannel = NULL;
296 struct rchan_buf *buf = NULL;
297 unsigned int nNumOfSubbufs = 0;
298 void* pConsume = NULL;
299 unsigned int nPaddingLength = 0;
300 unsigned int nSubbufSize = 0;
301 unsigned int nDataSize = 0;
302 unsigned int nEffectSize = 0;
303 unsigned int nSubbufDiscardedCount = 0;
305 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
306 if(nIgnoredBytes > 0)
308 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
313 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
314 if((ec_info_copy.m_nMode & MODEMASK_MULTIPLE_BUFFER) == 0) {
316 if(ec_info.after_last > ec_info.first) {
317 ec_info.buffer_effect = ec_info.buffer_size;
319 if (ec_info.after_last == ec_info.buffer_effect) {
322 ec_info.first = ec_info_copy.after_last;
324 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
328 #ifndef __DISABLE_RELAYFS
329 pRelayChannel = GetRelayChannel();
330 if(pRelayChannel == NULL) {
331 EPRINTF("Null pointer to relay channel!");
335 buf = pRelayChannel->buf[0];
336 nNumOfSubbufs = pRelayChannel->n_subbufs;
338 nSubbufSize = pRelayChannel->subbuf_size;
339 pConsume = buf->start + buf->subbufs_consumed % nNumOfSubbufs * nSubbufSize;
340 memcpy(&nPaddingLength, pConsume, sizeof(unsigned int));
341 memcpy(&nSubbufDiscardedCount, pConsume + sizeof(unsigned int), sizeof(unsigned int));
342 nEffectSize = nSubbufSize - nPaddingLength;
343 nDataSize = nEffectSize - RELAY_SUBBUF_HEADER_SIZE;
344 relay_subbufs_consumed(pRelayChannel, 0, 1);
345 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
346 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
347 ec_info.buffer_effect -= nEffectSize;
348 ec_info.trace_size -= nDataSize;
349 buf->dentry->d_inode->i_size = ec_info.trace_size;
352 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
354 // DPRINTF("Consume Buffer"); // Frequent call
357 case EC_IOCTL_ADD_PROBE:
359 unsigned long addr = arg;
360 result = add_probe(addr);
364 //@AGv: remove_probe expects probe address instead of name
365 /*case EC_IOCTL_REMOVE_PROBE:
367 char *probe_name = (char *) arg;
368 result = remove_probe (probe_name);
372 case EC_IOCTL_SET_APPDEPS:
375 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
377 EPRINTF("Cannot copy deps size!");
381 DPRINTF("Deps size has been copied (%d)", size);
384 DPRINTF("Deps are size of 0");
388 deps = vmalloc(size);
390 EPRINTF("Cannot alloc mem for deps!");
394 DPRINTF("Mem for deps has been allocated");
396 result = copy_from_user(deps, (void *)arg, size);
398 EPRINTF("Cannot copy deps!");
402 DPRINTF("Deps has been copied successfully");
406 case EC_IOCTL_SET_PID:
410 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
412 EPRINTF("Cannot copy pid!");
419 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
423 case EC_IOCTL_SET_PROFILEBUNDLE:
427 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
429 EPRINTF("Cannot copy bundle size!");
433 DPRINTF("Bundle size has been copied");
435 bundle = vmalloc(size);
436 if (bundle == NULL) {
437 EPRINTF("Cannot alloc mem for bundle!");
441 DPRINTF("Mem for bundle has been alloced");
443 result = copy_from_user(bundle, (void *)arg, size);
445 EPRINTF("Cannot copy bundle!");
449 DPRINTF("Bundle has been copied successfully");
451 if (link_bundle() == -1) {
452 EPRINTF("Cannot link profile bundle!");
459 case EC_IOCTL_RESET_PROBES:
461 result = reset_probes();
465 case EC_IOCTL_UPDATE_CONDS:
468 struct cond *c, *c_tmp, *p_cond;
469 unsigned char *p_data;
472 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
477 /* first, delete all the conds */
478 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
482 /* second, add new conds */
483 p_data = (unsigned char *)(arg + sizeof(int));
484 for (i = 0; i < args_cnt; i++) {
485 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
487 DPRINTF("Cannot alloc cond!");
491 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
493 DPRINTF("Cannot copy cond from user!");
498 list_add(&(p_cond->list), &(cond_list.list));
499 p_data += sizeof(struct event_tmpl);
503 case EC_IOCTL_ATTACH:
505 unsigned long dbi_flags;
506 struct dbi_modules_handlers *local_mh;
507 struct dbi_modules_handlers_info *local_mhi;
509 dbi_module_callback dmc_start;
511 // call "start"-callback for all modules according module priority
512 local_mh = get_dbi_modules_handlers();
513 spin_lock_irqsave(&local_mh->lock, dbi_flags);
514 for (j = 0; j <= MAX_PRIORITY; j++) {
515 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
516 if (local_mhi->dbi_module_priority_start == j) {
517 if (local_mhi->dbi_module_callback_start != NULL) {
518 printk("Started module callback (start) %s\n", local_mhi->dbi_module->name);
519 dmc_start = (dbi_module_callback )local_mhi->dbi_module_callback_start;
525 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
527 result = ec_user_attach ();
528 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
529 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
530 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
532 DPRINTF("Attach Probes");
535 case EC_IOCTL_ACTIVATE:
536 result = ec_user_activate ();
537 DPRINTF("Activate Probes");
539 case EC_IOCTL_STOP_AND_DETACH:
541 unsigned long nIgnoredBytes = 0;
542 unsigned long dbi_flags;
543 struct dbi_modules_handlers *local_mh;
544 struct dbi_modules_handlers_info *local_mhi;
545 unsigned int local_module_refcount = 0;
547 dbi_module_callback dmc_stop;
549 #ifdef OVERHEAD_DEBUG
550 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
551 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
556 printk("\n### imi_sum_time = %ld in install_mapped_ips()\n", imi_sum_time);
557 printk("### imi_sum_hit = %ld in install_mapped_ips()\n", imi_sum_hit);
559 if (imi_sum_hit != 0) {
560 printk("### time = %ld in install_mapped_ips()\n", imi_sum_time/imi_sum_hit);
566 local_mh = get_dbi_modules_handlers();
567 if(ec_user_stop() != 0) {
571 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
572 if(nIgnoredBytes > 0) {
578 DPRINTF("Stop and Detach Probes");
579 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
580 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
581 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
583 // call "stop"-callback for all modules according module priority
584 spin_lock_irqsave(&local_mh->lock, dbi_flags);
585 for (j = 0; j <= MAX_PRIORITY; j++) {
586 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
587 if (local_mhi->dbi_module_priority_stop == j) {
588 if (local_mhi->dbi_module_callback_stop != NULL) {
589 printk("Started module callback (stop) %s\n", local_mhi->dbi_module->name);
590 dmc_stop = (dbi_module_callback )local_mhi->dbi_module_callback_stop;
596 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
598 spin_lock_irqsave(&local_mh->lock, dbi_flags);
599 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
600 local_module_refcount = module_refcount(local_mhi->dbi_module);
601 if (local_module_refcount == 1) {
602 module_put(local_mhi->dbi_module);
604 else if (local_module_refcount > 1) {
605 printk("local_module_refcount too much - force set refcount to zero\n");
606 while (local_module_refcount--)
607 module_put(local_mhi->dbi_module);
610 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
613 case EC_IOCTL_WAIT_NOTIFICATION:
615 static ec_info_t ec_info_copy;
617 ioctl_wait_notification_t ioctl_args;
619 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
626 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
629 result = -EINTR; // woken by signal (ERESTARTSYS 512)
633 ioctl_args.notification_count = notification_count;
635 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
642 // FIXME: synchronization is necessary here (ec_info must be locked).
643 // ENTER_CRITICAL_SECTION
644 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
645 // LEAVE_CRITICAL_SECTION
647 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
650 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
657 case EC_IOCTL_US_EVENT:
659 ioctl_us_event_t ioctl_args;
660 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
664 EPRINTF ("copy_from_user() failure");
668 if(ioctl_args.len == 0){
670 EPRINTF ("invalid event length!");
673 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
676 EPRINTF ("failed to alloc mem for event!");
679 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
682 EPRINTF ("failed to copy event from user space!");
685 result = put_us_event(buf, ioctl_args.len);
690 // DPRINTF("User Space Event"); // Frequent call
694 case EC_IOCTL_SET_EVENT_MASK:
697 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
704 result = set_event_mask (mask);
709 DPRINTF("Set Event Mask = %d", mask);
713 case EC_IOCTL_GET_EVENT_MASK:
716 result = get_event_mask(&mask);
721 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
726 DPRINTF("Get Event Mask = %d", mask);
730 case EC_IOCTL_SET_PREDEF_UPROBES:
732 ioctl_predef_uprobes_info_t data;
733 result = copy_from_user (&data, (void *) arg, sizeof (data));
740 result = set_predef_uprobes (&data);
745 DPRINTF("Set Predefined User Space Probes");
749 case EC_IOCTL_GET_PREDEF_UPROBES:
751 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
756 DPRINTF("Get Predefined User Space Probes");
760 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
763 result = get_predef_uprobes_size(&size);
768 result = copy_to_user ((void *) arg, &size, sizeof (size));
773 DPRINTF("Get Size of Predefined User Space Probes");
778 EPRINTF ("Unknown driver command = %u", cmd);