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 int device_init (void)
71 int nRetVal = register_chrdev(device_major, device_name, &device_fops);
73 EPRINTF("Cannot register character device! [%s, %d]", device_name, device_major);
74 nReserved = register_chrdev(0, device_name, &device_fops);
77 unregister_chrdev(nReserved, device_name);
78 EPRINTF("Please, create a new device node with major number [%d],\n\tand pass it as module parameter!", nReserved);
81 } else if(nRetVal > 0) {
82 EPRINTF("Cannot register this device major number! [%d]\n\tTrying a new one. [%d]", device_major, nRetVal);
83 device_major = nRetVal;
88 void device_down (void)
90 unregister_chrdev(device_major, device_name);
93 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
94 void swap_register_notify (struct notifier_block *nb)
96 blocking_notifier_chain_register(&swap_notifier_list, nb);
98 EXPORT_SYMBOL_GPL(swap_register_notify);
100 void swap_unregister_notify (struct notifier_block *nb)
102 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
104 EXPORT_SYMBOL_GPL(swap_unregister_notify);
107 void notify_user (event_id_t event_id)
109 ec_info.events_counters[event_id] += 1;
111 if (EVENT_EC_PROBE_RECORD == event_id)
113 // EC_PROBE_RECORD events happen to often. To reduce overhead user
114 // space will be notified only once per each EVENTS_AGGREGATION_USEC
115 static uint64_t timestamp_usec = 0;
117 uint64_t current_usec;
122 do_gettimeofday (&tv);
123 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
125 if (current_usec < timestamp_usec)
127 // Note: time from do_gettimeofday() may go backward
128 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
132 delta_usec = current_usec - timestamp_usec;
133 if (EVENTS_AGGREGATION_USEC > delta_usec)
135 // wait the time left
137 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
138 #endif /* defined(__DEBUG) */
139 return; // supress notification
142 timestamp_usec = current_usec; // remember new time for the future use
143 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
144 return; // supress notification
145 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
146 return; // supress notification
149 ++notification_count;
150 wake_up_interruptible (¬ification_waiters_queue);
153 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
156 EPRINTF("Null pointer to buffer!");
159 return remap_vmalloc_range (vma, p_buffer, 0);
162 static int device_open(struct inode *inode, struct file *file)
164 /*if (gl_nDeviceOpened)
168 try_module_get(THIS_MODULE);
172 static int device_release(struct inode *inode, struct file *file)
175 module_put(THIS_MODULE);
179 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
181 EPRINTF("Operation <<read>> not supported!");
185 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
187 EPRINTF("Operation <<write>> not supported!");
190 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
191 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
193 static long device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
196 unsigned long spinlock_flags = 0L;
198 // DPRINTF("Command=%d", cmd);
201 case EC_IOCTL_SET_EC_MODE:
203 ioctl_general_t param;
204 unsigned long nIgnoredBytes = 0;
205 memset(¶m, '0', sizeof(ioctl_general_t));
206 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
207 if (nIgnoredBytes > 0) {
211 if(SetECMode(param.m_unsignedLong) == -1) {
216 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
219 case EC_IOCTL_GET_EC_MODE:
221 ioctl_general_t param;
222 unsigned long nIgnoredBytes = 0;
223 memset(¶m, '0', sizeof(ioctl_general_t));
224 param.m_unsignedLong = GetECMode();
225 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
226 if (nIgnoredBytes > 0) {
231 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
234 case EC_IOCTL_SET_BUFFER_SIZE:
236 ioctl_general_t param;
237 unsigned long nIgnoredBytes = 0;
238 memset(¶m, '0', sizeof(ioctl_general_t));
239 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
240 if (nIgnoredBytes > 0) {
244 if (SetBufferSize(param.m_unsignedLong) == -1) {
249 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
252 case EC_IOCTL_GET_BUFFER_SIZE:
254 ioctl_general_t param;
255 unsigned long nIgnoredBytes = 0;
256 memset(¶m, '0', sizeof(ioctl_general_t));
257 param.m_unsignedLong = GetBufferSize();
258 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
259 if (nIgnoredBytes > 0) {
264 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
267 case EC_IOCTL_RESET_BUFFER:
269 if (ResetBuffer() == -1) {
274 DPRINTF("Reset Buffer");
277 case EC_IOCTL_GET_EC_INFO:
279 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
284 // DPRINTF("Get Buffer Status"); // Frequent call
287 case EC_IOCTL_CONSUME_BUFFER:
289 static ec_info_t ec_info_copy;
290 int nIgnoredBytes = 0;
291 #ifndef __DISABLE_RELAYFS
292 struct rchan* pRelayChannel = NULL;
293 struct rchan_buf *buf = NULL;
294 unsigned int nNumOfSubbufs = 0;
295 void* pConsume = NULL;
296 unsigned int nPaddingLength = 0;
297 unsigned int nSubbufSize = 0;
298 unsigned int nDataSize = 0;
299 unsigned int nEffectSize = 0;
300 unsigned int nSubbufDiscardedCount = 0;
302 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
303 if(nIgnoredBytes > 0)
305 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
310 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
311 if((ec_info_copy.m_nMode & MODEMASK_MULTIPLE_BUFFER) == 0) {
313 if(ec_info.after_last > ec_info.first) {
314 ec_info.buffer_effect = ec_info.buffer_size;
316 if (ec_info.after_last == ec_info.buffer_effect) {
319 ec_info.first = ec_info_copy.after_last;
321 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
325 #ifndef __DISABLE_RELAYFS
326 pRelayChannel = GetRelayChannel();
327 if(pRelayChannel == NULL) {
328 EPRINTF("Null pointer to relay channel!");
332 buf = pRelayChannel->buf[0];
333 nNumOfSubbufs = pRelayChannel->n_subbufs;
335 nSubbufSize = pRelayChannel->subbuf_size;
336 pConsume = buf->start + buf->subbufs_consumed % nNumOfSubbufs * nSubbufSize;
337 memcpy(&nPaddingLength, pConsume, sizeof(unsigned int));
338 memcpy(&nSubbufDiscardedCount, pConsume + sizeof(unsigned int), sizeof(unsigned int));
339 nEffectSize = nSubbufSize - nPaddingLength;
340 nDataSize = nEffectSize - RELAY_SUBBUF_HEADER_SIZE;
341 relay_subbufs_consumed(pRelayChannel, 0, 1);
342 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
343 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
344 ec_info.buffer_effect -= nEffectSize;
345 ec_info.trace_size -= nDataSize;
346 buf->dentry->d_inode->i_size = ec_info.trace_size;
349 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
351 // DPRINTF("Consume Buffer"); // Frequent call
354 case EC_IOCTL_ADD_PROBE:
356 unsigned long addr = arg;
357 result = add_probe(addr);
361 //@AGv: remove_probe expects probe address instead of name
362 /*case EC_IOCTL_REMOVE_PROBE:
364 char *probe_name = (char *) arg;
365 result = remove_probe (probe_name);
369 case EC_IOCTL_SET_APPDEPS:
372 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
374 EPRINTF("Cannot copy deps size!");
378 DPRINTF("Deps size has been copied (%d)", size);
381 DPRINTF("Deps are size of 0");
385 deps = vmalloc(size);
387 EPRINTF("Cannot alloc mem for deps!");
391 DPRINTF("Mem for deps has been allocated");
393 result = copy_from_user(deps, (void *)arg, size);
395 EPRINTF("Cannot copy deps!");
399 DPRINTF("Deps has been copied successfully");
403 case EC_IOCTL_SET_PID:
407 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
409 EPRINTF("Cannot copy pid!");
416 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
420 case EC_IOCTL_SET_PROFILEBUNDLE:
424 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
426 EPRINTF("Cannot copy bundle size!");
430 DPRINTF("Bundle size has been copied");
432 bundle = vmalloc(size);
433 if (bundle == NULL) {
434 EPRINTF("Cannot alloc mem for bundle!");
438 DPRINTF("Mem for bundle has been alloced");
440 result = copy_from_user(bundle, (void *)arg, size);
442 EPRINTF("Cannot copy bundle!");
446 DPRINTF("Bundle has been copied successfully");
448 if (link_bundle() == -1) {
449 EPRINTF("Cannot link profile bundle!");
456 case EC_IOCTL_RESET_PROBES:
458 result = reset_probes();
462 case EC_IOCTL_UPDATE_CONDS:
465 struct cond *c, *c_tmp, *p_cond;
466 unsigned char *p_data;
469 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
474 /* first, delete all the conds */
475 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
479 /* second, add new conds */
480 p_data = (unsigned char *)(arg + sizeof(int));
481 for (i = 0; i < args_cnt; i++) {
482 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
484 DPRINTF("Cannot alloc cond!");
488 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
490 DPRINTF("Cannot copy cond from user!");
495 list_add(&(p_cond->list), &(cond_list.list));
496 p_data += sizeof(struct event_tmpl);
500 case EC_IOCTL_ATTACH:
501 result = ec_user_attach ();
502 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
503 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
504 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
506 DPRINTF("Attach Probes");
508 case EC_IOCTL_ACTIVATE:
509 result = ec_user_activate ();
510 DPRINTF("Activate Probes");
512 case EC_IOCTL_STOP_AND_DETACH:
514 unsigned long nIgnoredBytes = 0;
515 unsigned long dbi_flags;
516 struct dbi_modules_handlers *local_mh;
517 struct dbi_modules_handlers_info *local_mhi;
518 unsigned int local_module_refcount = 0;
520 #ifdef OVERHEAD_DEBUG
521 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
522 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
527 printk("\n### imi_sum_time = %ld in install_mapped_ips()\n", imi_sum_time);
528 printk("### imi_sum_hit = %ld in install_mapped_ips()\n", imi_sum_hit);
530 if (imi_sum_hit != 0) {
531 printk("### time = %ld in install_mapped_ips()\n", imi_sum_time/imi_sum_hit);
537 if(ec_user_stop() != 0) {
541 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
542 if(nIgnoredBytes > 0) {
548 DPRINTF("Stop and Detach Probes");
549 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
550 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
551 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
554 local_mh = get_dbi_modules_handlers();
555 spin_lock_irqsave(&local_mh->lock, dbi_flags);
556 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
557 local_module_refcount = module_refcount(local_mhi->dbi_module);
558 if (local_module_refcount == 1) {
559 module_put(local_mhi->dbi_module);
561 else if (local_module_refcount > 1) {
562 printk("local_module_refcount too much - force set refcount to zero\n");
563 while (local_module_refcount--)
564 module_put(local_mhi->dbi_module);
567 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
570 case EC_IOCTL_WAIT_NOTIFICATION:
572 static ec_info_t ec_info_copy;
574 ioctl_wait_notification_t ioctl_args;
576 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
583 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
586 result = -EINTR; // woken by signal (ERESTARTSYS 512)
590 ioctl_args.notification_count = notification_count;
592 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
599 // FIXME: synchronization is necessary here (ec_info must be locked).
600 // ENTER_CRITICAL_SECTION
601 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
602 // LEAVE_CRITICAL_SECTION
604 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
607 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
614 case EC_IOCTL_US_EVENT:
616 ioctl_us_event_t ioctl_args;
617 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
621 EPRINTF ("copy_from_user() failure");
625 if(ioctl_args.len == 0){
627 EPRINTF ("invalid event length!");
630 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
633 EPRINTF ("failed to alloc mem for event!");
636 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
639 EPRINTF ("failed to copy event from user space!");
642 result = put_us_event(buf, ioctl_args.len);
647 // DPRINTF("User Space Event"); // Frequent call
651 case EC_IOCTL_SET_EVENT_MASK:
654 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
661 result = set_event_mask (mask);
666 DPRINTF("Set Event Mask = %d", mask);
670 case EC_IOCTL_GET_EVENT_MASK:
673 result = get_event_mask(&mask);
678 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
683 DPRINTF("Get Event Mask = %d", mask);
687 case EC_IOCTL_SET_PREDEF_UPROBES:
689 ioctl_predef_uprobes_info_t data;
690 result = copy_from_user (&data, (void *) arg, sizeof (data));
697 result = set_predef_uprobes (&data);
702 DPRINTF("Set Predefined User Space Probes");
706 case EC_IOCTL_GET_PREDEF_UPROBES:
708 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
713 DPRINTF("Get Predefined User Space Probes");
717 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
720 result = get_predef_uprobes_size(&size);
725 result = copy_to_user ((void *) arg, &size, sizeof (size));
730 DPRINTF("Get Size of Predefined User Space Probes");
735 EPRINTF ("Unknown driver command = %u", cmd);