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"
22 #include <linux/notifier.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 int 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 int device_init (void)
72 int nRetVal = register_chrdev(device_major, device_name, &device_fops);
74 EPRINTF("Cannot register character device! [%s, %d]", device_name, device_major);
75 nReserved = register_chrdev(0, device_name, &device_fops);
78 unregister_chrdev(nReserved, device_name);
79 EPRINTF("Please, create a new device node with major number [%d],\n\tand pass it as module parameter!", nReserved);
82 } else if(nRetVal > 0) {
83 EPRINTF("Cannot register this device major number! [%d]\n\tTrying a new one. [%d]", device_major, nRetVal);
84 device_major = nRetVal;
89 void device_down (void)
91 unregister_chrdev(device_major, device_name);
94 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
95 void swap_register_notify (struct notifier_block *nb)
97 blocking_notifier_chain_register(&swap_notifier_list, nb);
99 EXPORT_SYMBOL_GPL(swap_register_notify);
101 void swap_unregister_notify (struct notifier_block *nb)
103 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
105 EXPORT_SYMBOL_GPL(swap_unregister_notify);
108 void notify_user (event_id_t event_id)
110 ec_info.events_counters[event_id] += 1;
112 if (EVENT_EC_PROBE_RECORD == event_id)
114 // EC_PROBE_RECORD events happen to often. To reduce overhead user
115 // space will be notified only once per each EVENTS_AGGREGATION_USEC
116 static uint64_t timestamp_usec = 0;
118 uint64_t current_usec;
123 do_gettimeofday (&tv);
124 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
126 if (current_usec < timestamp_usec)
128 // Note: time from do_gettimeofday() may go backward
129 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
133 delta_usec = current_usec - timestamp_usec;
134 if (EVENTS_AGGREGATION_USEC > delta_usec)
136 // wait the time left
138 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
139 #endif /* defined(__DEBUG) */
140 return; // supress notification
143 timestamp_usec = current_usec; // remember new time for the future use
144 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
145 return; // supress notification
146 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
147 return; // supress notification
150 ++notification_count;
151 wake_up_interruptible (¬ification_waiters_queue);
154 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
157 EPRINTF("Null pointer to buffer!");
160 return remap_vmalloc_range (vma, p_buffer, 0);
163 static int device_open(struct inode *inode, struct file *file)
165 /*if (gl_nDeviceOpened)
169 try_module_get(THIS_MODULE);
173 static int device_release(struct inode *inode, struct file *file)
176 module_put(THIS_MODULE);
180 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
182 EPRINTF("Operation <<read>> not supported!");
186 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
188 EPRINTF("Operation <<write>> not supported!");
191 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
192 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
194 static int device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
197 unsigned long spinlock_flags = 0L;
199 // DPRINTF("Command=%d", cmd);
202 case EC_IOCTL_SET_EC_MODE:
204 ioctl_general_t param;
205 unsigned long nIgnoredBytes = 0;
206 memset(¶m, '0', sizeof(ioctl_general_t));
207 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
208 if (nIgnoredBytes > 0) {
212 if(SetECMode(param.m_unsignedLong) == -1) {
217 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
220 case EC_IOCTL_GET_EC_MODE:
222 ioctl_general_t param;
223 unsigned long nIgnoredBytes = 0;
224 memset(¶m, '0', sizeof(ioctl_general_t));
225 param.m_unsignedLong = GetECMode();
226 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
227 if (nIgnoredBytes > 0) {
232 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
235 case EC_IOCTL_SET_BUFFER_SIZE:
237 ioctl_general_t param;
238 unsigned long nIgnoredBytes = 0;
239 memset(¶m, '0', sizeof(ioctl_general_t));
240 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
241 if (nIgnoredBytes > 0) {
245 if (SetBufferSize(param.m_unsignedLong) == -1) {
250 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
253 case EC_IOCTL_GET_BUFFER_SIZE:
255 ioctl_general_t param;
256 unsigned long nIgnoredBytes = 0;
257 memset(¶m, '0', sizeof(ioctl_general_t));
258 param.m_unsignedLong = GetBufferSize();
259 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
260 if (nIgnoredBytes > 0) {
265 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
268 case EC_IOCTL_RESET_BUFFER:
270 if (ResetBuffer() == -1) {
275 DPRINTF("Reset Buffer");
278 case EC_IOCTL_GET_EC_INFO:
280 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
285 // DPRINTF("Get Buffer Status"); // Frequent call
288 case EC_IOCTL_CONSUME_BUFFER:
290 static ec_info_t ec_info_copy;
291 int nIgnoredBytes = 0;
292 #ifndef __DISABLE_RELAYFS
293 struct rchan* pRelayChannel = NULL;
294 struct rchan_buf *buf = NULL;
295 unsigned int nNumOfSubbufs = 0;
296 void* pConsume = NULL;
297 unsigned int nPaddingLength = 0;
298 unsigned int nSubbufSize = 0;
299 unsigned int nDataSize = 0;
300 unsigned int nEffectSize = 0;
301 unsigned int nSubbufDiscardedCount = 0;
303 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
304 if(nIgnoredBytes > 0)
306 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
311 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
312 if((ec_info_copy.m_nMode & MODEMASK_MULTIPLE_BUFFER) == 0) {
314 if(ec_info.after_last > ec_info.first) {
315 ec_info.buffer_effect = ec_info.buffer_size;
317 if (ec_info.after_last == ec_info.buffer_effect) {
320 ec_info.first = ec_info_copy.after_last;
322 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
326 #ifndef __DISABLE_RELAYFS
327 pRelayChannel = GetRelayChannel();
328 if(pRelayChannel == NULL) {
329 EPRINTF("Null pointer to relay channel!");
333 buf = pRelayChannel->buf[0];
334 nNumOfSubbufs = pRelayChannel->n_subbufs;
336 nSubbufSize = pRelayChannel->subbuf_size;
337 pConsume = buf->start + buf->subbufs_consumed % nNumOfSubbufs * nSubbufSize;
338 memcpy(&nPaddingLength, pConsume, sizeof(unsigned int));
339 memcpy(&nSubbufDiscardedCount, pConsume + sizeof(unsigned int), sizeof(unsigned int));
340 nEffectSize = nSubbufSize - nPaddingLength;
341 nDataSize = nEffectSize - RELAY_SUBBUF_HEADER_SIZE;
342 relay_subbufs_consumed(pRelayChannel, 0, 1);
343 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
344 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
345 ec_info.buffer_effect -= nEffectSize;
346 ec_info.trace_size -= nDataSize;
347 buf->dentry->d_inode->i_size = ec_info.trace_size;
350 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
352 // DPRINTF("Consume Buffer"); // Frequent call
355 case EC_IOCTL_ADD_PROBE:
357 unsigned long addr = arg;
358 result = add_probe(addr);
362 //@AGv: remove_probe expects probe address instead of name
363 /*case EC_IOCTL_REMOVE_PROBE:
365 char *probe_name = (char *) arg;
366 result = remove_probe (probe_name);
370 case EC_IOCTL_SET_APPDEPS:
373 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
375 EPRINTF("Cannot copy deps size!");
379 DPRINTF("Deps size has been copied (%d)", size);
382 DPRINTF("Deps are size of 0");
386 deps = vmalloc(size);
388 EPRINTF("Cannot alloc mem for deps!");
392 DPRINTF("Mem for deps has been allocated");
394 result = copy_from_user(deps, (void *)arg, size);
396 EPRINTF("Cannot copy deps!");
400 DPRINTF("Deps has been copied successfully");
404 case EC_IOCTL_SET_PID:
408 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
410 EPRINTF("Cannot copy pid!");
417 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
421 case EC_IOCTL_SET_PROFILEBUNDLE:
425 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
427 EPRINTF("Cannot copy bundle size!");
431 DPRINTF("Bundle size has been copied");
433 bundle = vmalloc(size);
434 if (bundle == NULL) {
435 EPRINTF("Cannot alloc mem for bundle!");
439 DPRINTF("Mem for bundle has been alloced");
441 result = copy_from_user(bundle, (void *)arg, size);
443 EPRINTF("Cannot copy bundle!");
447 DPRINTF("Bundle has been copied successfully");
449 if (link_bundle() == -1) {
450 EPRINTF("Cannot link profile bundle!");
457 case EC_IOCTL_RESET_PROBES:
459 result = reset_probes();
463 case EC_IOCTL_UPDATE_CONDS:
466 struct cond *c, *c_tmp, *p_cond;
467 unsigned char *p_data;
470 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
475 /* first, delete all the conds */
476 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
480 /* second, add new conds */
481 p_data = (unsigned char *)(arg + sizeof(int));
482 for (i = 0; i < args_cnt; i++) {
483 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
485 DPRINTF("Cannot alloc cond!");
489 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
491 DPRINTF("Cannot copy cond from user!");
496 list_add(&(p_cond->list), &(cond_list.list));
497 p_data += sizeof(struct event_tmpl);
501 case EC_IOCTL_ATTACH:
502 result = ec_user_attach ();
503 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
504 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
505 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
507 DPRINTF("Attach Probes");
509 case EC_IOCTL_ACTIVATE:
510 result = ec_user_activate ();
511 DPRINTF("Activate Probes");
513 case EC_IOCTL_STOP_AND_DETACH:
515 unsigned long nIgnoredBytes = 0;
516 unsigned long dbi_flags;
517 struct dbi_modules_handlers *local_mh;
518 struct dbi_modules_handlers_info *local_mhi;
519 unsigned int local_module_refcount = 0;
521 #ifdef OVERHEAD_DEBUG
522 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
523 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
528 printk("\n### imi_sum_time = %ld in install_mapped_ips()\n", imi_sum_time);
529 printk("### imi_sum_hit = %ld in install_mapped_ips()\n", imi_sum_hit);
531 if (imi_sum_hit != 0) {
532 printk("### time = %ld in install_mapped_ips()\n", imi_sum_time/imi_sum_hit);
538 if(ec_user_stop() != 0) {
542 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
543 if(nIgnoredBytes > 0) {
549 DPRINTF("Stop and Detach Probes");
550 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
551 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
552 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
555 local_mh = get_dbi_modules_handlers();
556 spin_lock_irqsave(&local_mh->lock, dbi_flags);
557 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
558 local_module_refcount = module_refcount(local_mhi->dbi_module);
559 if (local_module_refcount == 1) {
560 module_put(local_mhi->dbi_module);
562 else if (local_module_refcount > 1) {
563 printk("local_module_refcount too much - force set refcount to zero\n");
564 while (local_module_refcount--)
565 module_put(local_mhi->dbi_module);
568 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
571 case EC_IOCTL_WAIT_NOTIFICATION:
573 static ec_info_t ec_info_copy;
575 ioctl_wait_notification_t ioctl_args;
577 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
584 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
587 result = -EINTR; // woken by signal (ERESTARTSYS 512)
591 ioctl_args.notification_count = notification_count;
593 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
600 // FIXME: synchronization is necessary here (ec_info must be locked).
601 // ENTER_CRITICAL_SECTION
602 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
603 // LEAVE_CRITICAL_SECTION
605 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
608 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
615 case EC_IOCTL_US_EVENT:
617 ioctl_us_event_t ioctl_args;
618 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
622 EPRINTF ("copy_from_user() failure");
626 if(ioctl_args.len == 0){
628 EPRINTF ("invalid event length!");
631 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
634 EPRINTF ("failed to alloc mem for event!");
637 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
640 EPRINTF ("failed to copy event from user space!");
643 result = put_us_event(buf, ioctl_args.len);
648 // DPRINTF("User Space Event"); // Frequent call
652 case EC_IOCTL_SET_EVENT_MASK:
655 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
662 result = set_event_mask (mask);
667 DPRINTF("Set Event Mask = %d", mask);
671 case EC_IOCTL_GET_EVENT_MASK:
674 result = get_event_mask(&mask);
679 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
684 DPRINTF("Get Event Mask = %d", mask);
688 case EC_IOCTL_SET_PREDEF_UPROBES:
690 ioctl_predef_uprobes_info_t data;
691 result = copy_from_user (&data, (void *) arg, sizeof (data));
698 result = set_predef_uprobes (&data);
703 DPRINTF("Set Predefined User Space Probes");
707 case EC_IOCTL_GET_PREDEF_UPROBES:
709 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
714 DPRINTF("Get Predefined User Space Probes");
718 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
721 result = get_predef_uprobes_size(&size);
726 result = copy_to_user ((void *) arg, &size, sizeof (size));
731 DPRINTF("Get Size of Predefined User Space Probes");
736 EPRINTF ("Unknown driver command = %u", cmd);