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;
29 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
30 static BLOCKING_NOTIFIER_HEAD(swap_notifier_list);
33 EXPORT_SYMBOL_GPL(gl_nNotifyTgid);
35 DECLARE_WAIT_QUEUE_HEAD (notification_waiters_queue);
36 volatile unsigned notification_count;
38 static int device_mmap (struct file *filp, struct vm_area_struct *vma);
39 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
40 static int device_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
42 static int device_ioctl (struct file *file, unsigned int cmd, unsigned long arg);
44 static int device_open(struct inode *, struct file *);
45 static int device_release(struct inode *, struct file *);
46 static ssize_t device_read(struct file *, char *, size_t, loff_t *);
47 static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
49 static int gl_nDeviceOpened = 0;
50 static struct file_operations device_fops = {
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
54 .ioctl = device_ioctl,
56 .unlocked_ioctl = device_ioctl,
59 .write = device_write,
61 .release = device_release
64 int device_init (void)
67 int nRetVal = register_chrdev(device_major, device_name, &device_fops);
69 EPRINTF("Cannot register character device! [%s, %d]", device_name, device_major);
70 nReserved = register_chrdev(0, device_name, &device_fops);
73 unregister_chrdev(nReserved, device_name);
74 EPRINTF("Please, create a new device node with major number [%d],\n\tand pass it as module parameter!", nReserved);
77 } else if(nRetVal > 0) {
78 EPRINTF("Cannot register this device major number! [%d]\n\tTrying a new one. [%d]", device_major, nRetVal);
79 device_major = nRetVal;
84 void device_down (void)
86 unregister_chrdev(device_major, device_name);
89 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
90 void swap_register_notify (struct notifier_block *nb)
92 blocking_notifier_chain_register(&swap_notifier_list, nb);
94 EXPORT_SYMBOL_GPL(swap_register_notify);
96 void swap_unregister_notify (struct notifier_block *nb)
98 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
100 EXPORT_SYMBOL_GPL(swap_unregister_notify);
103 void notify_user (event_id_t event_id)
105 ec_info.events_counters[event_id] += 1;
107 if (EVENT_EC_PROBE_RECORD == event_id)
109 // EC_PROBE_RECORD events happen to often. To reduce overhead user
110 // space will be notified only once per each EVENTS_AGGREGATION_USEC
111 static uint64_t timestamp_usec = 0;
113 uint64_t current_usec;
118 do_gettimeofday (&tv);
119 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
121 if (current_usec < timestamp_usec)
123 // Note: time from do_gettimeofday() may go backward
124 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
128 delta_usec = current_usec - timestamp_usec;
129 if (EVENTS_AGGREGATION_USEC > delta_usec)
131 // wait the time left
133 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
134 #endif /* defined(__DEBUG) */
135 return; // supress notification
138 timestamp_usec = current_usec; // remember new time for the future use
139 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
140 return; // supress notification
141 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
142 return; // supress notification
145 ++notification_count;
146 wake_up_interruptible (¬ification_waiters_queue);
149 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
152 EPRINTF("Null pointer to buffer!");
155 return remap_vmalloc_range (vma, p_buffer, 0);
158 static int device_open(struct inode *inode, struct file *file)
160 /*if (gl_nDeviceOpened)
164 try_module_get(THIS_MODULE);
168 static int device_release(struct inode *inode, struct file *file)
171 module_put(THIS_MODULE);
175 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
177 EPRINTF("Operation <<read>> not supported!");
181 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
183 EPRINTF("Operation <<write>> not supported!");
186 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
187 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
189 static int device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
192 unsigned long spinlock_flags = 0L;
194 // DPRINTF("Command=%d", cmd);
197 case EC_IOCTL_SET_EC_MODE:
199 ioctl_general_t param;
200 unsigned long nIgnoredBytes = 0;
201 memset(¶m, '0', sizeof(ioctl_general_t));
202 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
203 if (nIgnoredBytes > 0) {
207 if(SetECMode(param.m_unsignedLong) == -1) {
212 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
215 case EC_IOCTL_GET_EC_MODE:
217 ioctl_general_t param;
218 unsigned long nIgnoredBytes = 0;
219 memset(¶m, '0', sizeof(ioctl_general_t));
220 param.m_unsignedLong = GetECMode();
221 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
222 if (nIgnoredBytes > 0) {
227 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
230 case EC_IOCTL_SET_BUFFER_SIZE:
232 ioctl_general_t param;
233 unsigned long nIgnoredBytes = 0;
234 memset(¶m, '0', sizeof(ioctl_general_t));
235 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
236 if (nIgnoredBytes > 0) {
240 if (SetBufferSize(param.m_unsignedLong) == -1) {
245 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
248 case EC_IOCTL_GET_BUFFER_SIZE:
250 ioctl_general_t param;
251 unsigned long nIgnoredBytes = 0;
252 memset(¶m, '0', sizeof(ioctl_general_t));
253 param.m_unsignedLong = GetBufferSize();
254 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
255 if (nIgnoredBytes > 0) {
260 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
263 case EC_IOCTL_RESET_BUFFER:
265 if (ResetBuffer() == -1) {
270 DPRINTF("Reset Buffer");
273 case EC_IOCTL_GET_EC_INFO:
275 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
280 // DPRINTF("Get Buffer Status"); // Frequent call
283 case EC_IOCTL_CONSUME_BUFFER:
285 static ec_info_t ec_info_copy;
286 int nIgnoredBytes = 0;
287 #ifndef __DISABLE_RELAYFS
288 struct rchan* pRelayChannel = NULL;
289 struct rchan_buf *buf = NULL;
290 unsigned int nNumOfSubbufs = 0;
291 void* pConsume = NULL;
292 unsigned int nPaddingLength = 0;
293 unsigned int nSubbufSize = 0;
294 unsigned int nDataSize = 0;
295 unsigned int nEffectSize = 0;
296 unsigned int nSubbufDiscardedCount = 0;
298 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
299 if(nIgnoredBytes > 0)
301 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
306 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
307 if((ec_info_copy.m_nMode & MODEMASK_MULTIPLE_BUFFER) == 0) {
309 if(ec_info.after_last > ec_info.first) {
310 ec_info.buffer_effect = ec_info.buffer_size;
312 if (ec_info.after_last == ec_info.buffer_effect) {
315 ec_info.first = ec_info_copy.after_last;
317 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
321 #ifndef __DISABLE_RELAYFS
322 pRelayChannel = GetRelayChannel();
323 if(pRelayChannel == NULL) {
324 EPRINTF("Null pointer to relay channel!");
328 buf = pRelayChannel->buf[0];
329 nNumOfSubbufs = pRelayChannel->n_subbufs;
331 nSubbufSize = pRelayChannel->subbuf_size;
332 pConsume = buf->start + buf->subbufs_consumed % nNumOfSubbufs * nSubbufSize;
333 memcpy(&nPaddingLength, pConsume, sizeof(unsigned int));
334 memcpy(&nSubbufDiscardedCount, pConsume + sizeof(unsigned int), sizeof(unsigned int));
335 nEffectSize = nSubbufSize - nPaddingLength;
336 nDataSize = nEffectSize - RELAY_SUBBUF_HEADER_SIZE;
337 relay_subbufs_consumed(pRelayChannel, 0, 1);
338 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
339 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
340 ec_info.buffer_effect -= nEffectSize;
341 ec_info.trace_size -= nDataSize;
342 buf->dentry->d_inode->i_size = ec_info.trace_size;
345 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
347 // DPRINTF("Consume Buffer"); // Frequent call
350 case EC_IOCTL_ADD_PROBE:
352 unsigned long addr = arg;
353 result = add_probe(addr);
357 //@AGv: remove_probe expects probe address instead of name
358 /*case EC_IOCTL_REMOVE_PROBE:
360 char *probe_name = (char *) arg;
361 result = remove_probe (probe_name);
365 case EC_IOCTL_SET_APPDEPS:
368 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
370 EPRINTF("Cannot copy deps size!");
374 DPRINTF("Deps size has been copied (%d)", size);
377 DPRINTF("Deps are size of 0");
381 deps = vmalloc(size);
383 EPRINTF("Cannot alloc mem for deps!");
387 DPRINTF("Mem for deps has been allocated");
389 result = copy_from_user(deps, (void *)arg, size);
391 EPRINTF("Cannot copy deps!");
395 DPRINTF("Deps has been copied successfully");
399 case EC_IOCTL_SET_PID:
403 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
405 EPRINTF("Cannot copy pid!");
412 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
416 case EC_IOCTL_SET_PROFILEBUNDLE:
420 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
422 EPRINTF("Cannot copy bundle size!");
426 DPRINTF("Bundle size has been copied");
428 bundle = vmalloc(size);
429 if (bundle == NULL) {
430 EPRINTF("Cannot alloc mem for bundle!");
434 DPRINTF("Mem for bundle has been alloced");
436 result = copy_from_user(bundle, (void *)arg, size);
438 EPRINTF("Cannot copy bundle!");
442 DPRINTF("Bundle has been copied successfully");
444 if (link_bundle() == -1) {
445 EPRINTF("Cannot link profile bundle!");
452 case EC_IOCTL_RESET_PROBES:
454 result = reset_probes();
458 case EC_IOCTL_UPDATE_CONDS:
461 struct cond *c, *c_tmp, *p_cond;
462 unsigned char *p_data;
465 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
470 /* first, delete all the conds */
471 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
475 /* second, add new conds */
476 p_data = (unsigned char *)(arg + sizeof(int));
477 for (i = 0; i < args_cnt; i++) {
478 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
480 DPRINTF("Cannot alloc cond!");
484 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
486 DPRINTF("Cannot copy cond from user!");
491 list_add(&(p_cond->list), &(cond_list.list));
492 p_data += sizeof(struct event_tmpl);
496 case EC_IOCTL_ATTACH:
497 result = ec_user_attach ();
498 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
499 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
500 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
502 DPRINTF("Attach Probes");
504 case EC_IOCTL_ACTIVATE:
505 result = ec_user_activate ();
506 DPRINTF("Activate Probes");
508 case EC_IOCTL_STOP_AND_DETACH:
510 unsigned long nIgnoredBytes = 0;
511 unsigned long dbi_flags;
512 struct dbi_modules_handlers *local_mh;
513 struct dbi_modules_handlers_info *local_mhi;
514 unsigned int local_module_refcount = 0;
516 #ifdef OVERHEAD_DEBUG
517 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
518 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
522 if(ec_user_stop() != 0) {
526 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
527 if(nIgnoredBytes > 0) {
533 DPRINTF("Stop and Detach Probes");
534 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
535 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
536 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
539 local_mh = get_dbi_modules_handlers();
540 spin_lock_irqsave(&local_mh->lock, dbi_flags);
541 list_for_each_entry_rcu(local_mhi, &local_mh->modules_handlers, dbi_list_head) {
542 local_module_refcount = module_refcount(local_mhi->dbi_module);
543 if (local_module_refcount == 1) {
544 module_put(local_mhi->dbi_module);
546 else if (local_module_refcount > 1) {
547 printk("local_module_refcount too much - force set refcount to zero\n");
548 while (local_module_refcount--)
549 module_put(local_mhi->dbi_module);
552 spin_unlock_irqrestore(&local_mh->lock, dbi_flags);
555 case EC_IOCTL_WAIT_NOTIFICATION:
557 static ec_info_t ec_info_copy;
559 ioctl_wait_notification_t ioctl_args;
561 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
568 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
571 result = -EINTR; // woken by signal (ERESTARTSYS 512)
575 ioctl_args.notification_count = notification_count;
577 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
584 // FIXME: synchronization is necessary here (ec_info must be locked).
585 // ENTER_CRITICAL_SECTION
586 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
587 // LEAVE_CRITICAL_SECTION
589 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
592 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
599 case EC_IOCTL_US_EVENT:
601 ioctl_us_event_t ioctl_args;
602 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
606 EPRINTF ("copy_from_user() failure");
610 if(ioctl_args.len == 0){
612 EPRINTF ("invalid event length!");
615 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
618 EPRINTF ("failed to alloc mem for event!");
621 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
624 EPRINTF ("failed to copy event from user space!");
627 result = put_us_event(buf, ioctl_args.len);
632 // DPRINTF("User Space Event"); // Frequent call
636 case EC_IOCTL_SET_EVENT_MASK:
639 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
646 result = set_event_mask (mask);
651 DPRINTF("Set Event Mask = %d", mask);
655 case EC_IOCTL_GET_EVENT_MASK:
658 result = get_event_mask(&mask);
663 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
668 DPRINTF("Get Event Mask = %d", mask);
672 case EC_IOCTL_SET_PREDEF_UPROBES:
674 ioctl_predef_uprobes_info_t data;
675 result = copy_from_user (&data, (void *) arg, sizeof (data));
682 result = set_predef_uprobes (&data);
687 DPRINTF("Set Predefined User Space Probes");
691 case EC_IOCTL_GET_PREDEF_UPROBES:
693 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
698 DPRINTF("Get Predefined User Space Probes");
702 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
705 result = get_predef_uprobes_size(&size);
710 result = copy_to_user ((void *) arg, &size, sizeof (size));
715 DPRINTF("Get Size of Predefined User Space Probes");
720 EPRINTF ("Unknown driver command = %u", cmd);