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 int device_init (void)
66 int nRetVal = register_chrdev(device_major, device_name, &device_fops);
68 EPRINTF("Cannot register character device! [%s, %d]", device_name, device_major);
69 nReserved = register_chrdev(0, device_name, &device_fops);
72 unregister_chrdev(nReserved, device_name);
73 EPRINTF("Please, create a new device node with major number [%d],\n\tand pass it as module parameter!", nReserved);
76 } else if(nRetVal > 0) {
77 EPRINTF("Cannot register this device major number! [%d]\n\tTrying a new one. [%d]", device_major, nRetVal);
78 device_major = nRetVal;
83 void device_down (void)
85 unregister_chrdev(device_major, device_name);
88 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
89 void swap_register_notify (struct notifier_block *nb)
91 blocking_notifier_chain_register(&swap_notifier_list, nb);
93 EXPORT_SYMBOL_GPL(swap_register_notify);
95 void swap_unregister_notify (struct notifier_block *nb)
97 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
99 EXPORT_SYMBOL_GPL(swap_unregister_notify);
102 void notify_user (event_id_t event_id)
104 ec_info.events_counters[event_id] += 1;
106 if (EVENT_EC_PROBE_RECORD == event_id)
108 // EC_PROBE_RECORD events happen to often. To reduce overhead user
109 // space will be notified only once per each EVENTS_AGGREGATION_USEC
110 static uint64_t timestamp_usec = 0;
112 uint64_t current_usec;
117 do_gettimeofday (&tv);
118 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
120 if (current_usec < timestamp_usec)
122 // Note: time from do_gettimeofday() may go backward
123 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
127 delta_usec = current_usec - timestamp_usec;
128 if (EVENTS_AGGREGATION_USEC > delta_usec)
130 // wait the time left
132 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
133 #endif /* defined(__DEBUG) */
134 return; // supress notification
137 timestamp_usec = current_usec; // remember new time for the future use
138 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
139 return; // supress notification
140 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
141 return; // supress notification
144 ++notification_count;
145 wake_up_interruptible (¬ification_waiters_queue);
148 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
151 EPRINTF("Null pointer to buffer!");
154 return remap_vmalloc_range (vma, p_buffer, 0);
157 static int device_open(struct inode *inode, struct file *file)
159 /*if (gl_nDeviceOpened)
163 try_module_get(THIS_MODULE);
167 static int device_release(struct inode *inode, struct file *file)
170 module_put(THIS_MODULE);
174 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
176 EPRINTF("Operation <<read>> not supported!");
180 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
182 EPRINTF("Operation <<write>> not supported!");
185 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
186 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
188 static long device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
191 unsigned long spinlock_flags = 0L;
193 // DPRINTF("Command=%d", cmd);
196 case EC_IOCTL_SET_EC_MODE:
198 ioctl_general_t param;
199 unsigned long nIgnoredBytes = 0;
200 memset(¶m, '0', sizeof(ioctl_general_t));
201 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
202 if (nIgnoredBytes > 0) {
206 if(SetECMode(param.m_unsignedLong) == -1) {
211 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
214 case EC_IOCTL_GET_EC_MODE:
216 ioctl_general_t param;
217 unsigned long nIgnoredBytes = 0;
218 memset(¶m, '0', sizeof(ioctl_general_t));
219 param.m_unsignedLong = GetECMode();
220 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
221 if (nIgnoredBytes > 0) {
226 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
229 case EC_IOCTL_SET_BUFFER_SIZE:
231 ioctl_general_t param;
232 unsigned long nIgnoredBytes = 0;
233 memset(¶m, '0', sizeof(ioctl_general_t));
234 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
235 if (nIgnoredBytes > 0) {
239 if (SetBufferSize(param.m_unsignedLong) == -1) {
244 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
247 case EC_IOCTL_GET_BUFFER_SIZE:
249 ioctl_general_t param;
250 unsigned long nIgnoredBytes = 0;
251 memset(¶m, '0', sizeof(ioctl_general_t));
252 param.m_unsignedLong = GetBufferSize();
253 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
254 if (nIgnoredBytes > 0) {
259 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
262 case EC_IOCTL_RESET_BUFFER:
264 if (ResetBuffer() == -1) {
269 DPRINTF("Reset Buffer");
272 case EC_IOCTL_GET_EC_INFO:
274 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
279 // DPRINTF("Get Buffer Status"); // Frequent call
282 case EC_IOCTL_CONSUME_BUFFER:
284 static ec_info_t ec_info_copy;
285 int nIgnoredBytes = 0;
286 #ifndef __DISABLE_RELAYFS
287 struct rchan* pRelayChannel = NULL;
288 struct rchan_buf *buf = NULL;
289 unsigned int nNumOfSubbufs = 0;
290 void* pConsume = NULL;
291 unsigned int nPaddingLength = 0;
292 unsigned int nSubbufSize = 0;
293 unsigned int nDataSize = 0;
294 unsigned int nEffectSize = 0;
295 unsigned int nSubbufDiscardedCount = 0;
297 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
298 if(nIgnoredBytes > 0)
300 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
305 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
306 if((ec_info_copy.m_nMode & MODEMASK_MULTIPLE_BUFFER) == 0) {
308 if(ec_info.after_last > ec_info.first) {
309 ec_info.buffer_effect = ec_info.buffer_size;
311 if (ec_info.after_last == ec_info.buffer_effect) {
314 ec_info.first = ec_info_copy.after_last;
316 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
320 #ifndef __DISABLE_RELAYFS
321 pRelayChannel = GetRelayChannel();
322 if(pRelayChannel == NULL) {
323 EPRINTF("Null pointer to relay channel!");
327 buf = pRelayChannel->buf[0];
328 nNumOfSubbufs = pRelayChannel->n_subbufs;
330 nSubbufSize = pRelayChannel->subbuf_size;
331 pConsume = buf->start + buf->subbufs_consumed % nNumOfSubbufs * nSubbufSize;
332 memcpy(&nPaddingLength, pConsume, sizeof(unsigned int));
333 memcpy(&nSubbufDiscardedCount, pConsume + sizeof(unsigned int), sizeof(unsigned int));
334 nEffectSize = nSubbufSize - nPaddingLength;
335 nDataSize = nEffectSize - RELAY_SUBBUF_HEADER_SIZE;
336 relay_subbufs_consumed(pRelayChannel, 0, 1);
337 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
338 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
339 ec_info.buffer_effect -= nEffectSize;
340 ec_info.trace_size -= nDataSize;
341 buf->dentry->d_inode->i_size = ec_info.trace_size;
344 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
346 // DPRINTF("Consume Buffer"); // Frequent call
349 case EC_IOCTL_ADD_PROBE:
351 unsigned long addr = arg;
352 result = add_probe(addr);
356 //@AGv: remove_probe expects probe address instead of name
357 /*case EC_IOCTL_REMOVE_PROBE:
359 char *probe_name = (char *) arg;
360 result = remove_probe (probe_name);
364 case EC_IOCTL_SET_APPDEPS:
367 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
369 EPRINTF("Cannot copy deps size!");
373 DPRINTF("Deps size has been copied (%d)", size);
376 DPRINTF("Deps are size of 0");
380 deps = vmalloc(size);
382 EPRINTF("Cannot alloc mem for deps!");
386 DPRINTF("Mem for deps has been allocated");
388 result = copy_from_user(deps, (void *)arg, size);
390 EPRINTF("Cannot copy deps!");
394 DPRINTF("Deps has been copied successfully");
398 case EC_IOCTL_SET_PID:
402 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
404 EPRINTF("Cannot copy pid!");
411 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
415 case EC_IOCTL_SET_PROFILEBUNDLE:
419 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
421 EPRINTF("Cannot copy bundle size!");
425 DPRINTF("Bundle size has been copied");
427 bundle = vmalloc(size);
428 if (bundle == NULL) {
429 EPRINTF("Cannot alloc mem for bundle!");
433 DPRINTF("Mem for bundle has been alloced");
435 result = copy_from_user(bundle, (void *)arg, size);
437 EPRINTF("Cannot copy bundle!");
441 DPRINTF("Bundle has been copied successfully");
443 if (link_bundle() == -1) {
444 EPRINTF("Cannot link profile bundle!");
451 case EC_IOCTL_RESET_PROBES:
453 result = reset_probes();
457 case EC_IOCTL_UPDATE_CONDS:
460 struct cond *c, *c_tmp, *p_cond;
461 unsigned char *p_data;
464 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
469 /* first, delete all the conds */
470 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
474 /* second, add new conds */
475 p_data = (unsigned char *)(arg + sizeof(int));
476 for (i = 0; i < args_cnt; i++) {
477 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
479 DPRINTF("Cannot alloc cond!");
483 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
485 DPRINTF("Cannot copy cond from user!");
490 list_add(&(p_cond->list), &(cond_list.list));
491 p_data += sizeof(struct event_tmpl);
495 case EC_IOCTL_ATTACH:
496 result = ec_user_attach ();
497 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
498 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
499 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
501 DPRINTF("Attach Probes");
503 case EC_IOCTL_ACTIVATE:
504 result = ec_user_activate ();
505 DPRINTF("Activate Probes");
507 case EC_IOCTL_STOP_AND_DETACH:
509 unsigned long nIgnoredBytes = 0;
510 unsigned long dbi_flags;
511 struct dbi_modules_handlers *local_mh;
512 struct dbi_modules_handlers_info *local_mhi;
513 unsigned int local_module_refcount = 0;
515 #ifdef OVERHEAD_DEBUG
516 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
517 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
521 if(ec_user_stop() != 0) {
525 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
526 if(nIgnoredBytes > 0) {
532 DPRINTF("Stop and Detach Probes");
533 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
534 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
535 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
538 local_mh = get_dbi_modules_handlers();
539 spin_lock_irqsave(&local_mh->lock, dbi_flags);
540 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
541 local_module_refcount = module_refcount(local_mhi->dbi_module);
542 if (local_module_refcount == 1) {
543 module_put(local_mhi->dbi_module);
545 else if (local_module_refcount > 1) {
546 printk("local_module_refcount too much - force set refcount to zero\n");
547 while (local_module_refcount--)
548 module_put(local_mhi->dbi_module);
551 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
554 case EC_IOCTL_WAIT_NOTIFICATION:
556 static ec_info_t ec_info_copy;
558 ioctl_wait_notification_t ioctl_args;
560 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
567 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
570 result = -EINTR; // woken by signal (ERESTARTSYS 512)
574 ioctl_args.notification_count = notification_count;
576 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
583 // FIXME: synchronization is necessary here (ec_info must be locked).
584 // ENTER_CRITICAL_SECTION
585 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
586 // LEAVE_CRITICAL_SECTION
588 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
591 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
598 case EC_IOCTL_US_EVENT:
600 ioctl_us_event_t ioctl_args;
601 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
605 EPRINTF ("copy_from_user() failure");
609 if(ioctl_args.len == 0){
611 EPRINTF ("invalid event length!");
614 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
617 EPRINTF ("failed to alloc mem for event!");
620 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
623 EPRINTF ("failed to copy event from user space!");
626 result = put_us_event(buf, ioctl_args.len);
631 // DPRINTF("User Space Event"); // Frequent call
635 case EC_IOCTL_SET_EVENT_MASK:
638 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
645 result = set_event_mask (mask);
650 DPRINTF("Set Event Mask = %d", mask);
654 case EC_IOCTL_GET_EVENT_MASK:
657 result = get_event_mask(&mask);
662 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
667 DPRINTF("Get Event Mask = %d", mask);
671 case EC_IOCTL_SET_PREDEF_UPROBES:
673 ioctl_predef_uprobes_info_t data;
674 result = copy_from_user (&data, (void *) arg, sizeof (data));
681 result = set_predef_uprobes (&data);
686 DPRINTF("Set Predefined User Space Probes");
690 case EC_IOCTL_GET_PREDEF_UPROBES:
692 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
697 DPRINTF("Get Predefined User Space Probes");
701 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
704 result = get_predef_uprobes_size(&size);
709 result = copy_to_user ((void *) arg, &size, sizeof (size));
714 DPRINTF("Get Size of Predefined User Space Probes");
719 EPRINTF ("Unknown driver command = %u", cmd);