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
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 int 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);
71 EPRINTF("Please, create a new device node with major number [%d],\n\tand pass it as module parameter!", nReserved);
73 } else if(nRetVal > 0) {
74 EPRINTF("Cannot register this device major number! [%d]\n\tTrying a new one. [%d]", device_major, nRetVal);
75 device_major = nRetVal;
80 void device_down (void)
82 unregister_chrdev(device_major, device_name);
85 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
86 void swap_register_notify (struct notifier_block *nb)
88 blocking_notifier_chain_register(&swap_notifier_list, nb);
90 EXPORT_SYMBOL_GPL(swap_register_notify);
92 void swap_unregister_notify (struct notifier_block *nb)
94 blocking_notifier_chain_unregister(&swap_notifier_list, nb);
96 EXPORT_SYMBOL_GPL(swap_unregister_notify);
99 void notify_user (event_id_t event_id)
101 ec_info.events_counters[event_id] += 1;
103 if (EVENT_EC_PROBE_RECORD == event_id)
105 // EC_PROBE_RECORD events happen to often. To reduce overhead user
106 // space will be notified only once per each EVENTS_AGGREGATION_USEC
107 static uint64_t timestamp_usec = 0;
109 uint64_t current_usec;
114 do_gettimeofday (&tv);
115 current_usec = 1000000ULL * (unsigned) tv.tv_sec + (unsigned) tv.tv_usec;
117 if (current_usec < timestamp_usec)
119 // Note: time from do_gettimeofday() may go backward
120 EPRINTF ("current_usec=%llu timestamp_usec=%llu", current_usec, timestamp_usec);
124 delta_usec = current_usec - timestamp_usec;
125 if (EVENTS_AGGREGATION_USEC > delta_usec)
127 // wait the time left
129 unsigned UNUSED left_usec = EVENTS_AGGREGATION_USEC - delta_usec;
130 #endif /* defined(__DEBUG) */
131 return; // supress notification
134 timestamp_usec = current_usec; // remember new time for the future use
135 } else if (EVENT_EC_START_CONDITION_SEEN == event_id) {
136 return; // supress notification
137 } else if (EVENT_EC_STOP_CONDITION_SEEN == event_id) {
138 return; // supress notification
141 ++notification_count;
142 wake_up_interruptible (¬ification_waiters_queue);
145 static int device_mmap (struct file *filp UNUSED, struct vm_area_struct *vma)
148 EPRINTF("Null pointer to buffer!");
151 return remap_vmalloc_range (vma, p_buffer, 0);
154 static int device_open(struct inode *inode, struct file *file)
156 /*if (gl_nDeviceOpened)
160 try_module_get(THIS_MODULE);
164 static int device_release(struct inode *inode, struct file *file)
167 module_put(THIS_MODULE);
171 static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset)
173 EPRINTF("Operation <<read>> not supported!");
177 static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
179 EPRINTF("Operation <<write>> not supported!");
182 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
183 static int device_ioctl (struct inode *inode UNUSED, struct file *file UNUSED, unsigned int cmd, unsigned long arg)
185 static int device_ioctl (struct file *file UNUSED, unsigned int cmd, unsigned long arg)
188 unsigned long spinlock_flags = 0L;
190 // DPRINTF("Command=%d", cmd);
193 case EC_IOCTL_SET_EC_MODE:
195 ioctl_general_t param;
196 unsigned long nIgnoredBytes = 0;
197 memset(¶m, '0', sizeof(ioctl_general_t));
198 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
199 if (nIgnoredBytes > 0) {
203 if(SetECMode(param.m_unsignedLong) == -1) {
208 DPRINTF("Set EC Mode = %lu", param.m_unsignedLong);
211 case EC_IOCTL_GET_EC_MODE:
213 ioctl_general_t param;
214 unsigned long nIgnoredBytes = 0;
215 memset(¶m, '0', sizeof(ioctl_general_t));
216 param.m_unsignedLong = GetECMode();
217 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
218 if (nIgnoredBytes > 0) {
223 // DPRINTF("Get EC Mode = %lu", param.m_unsignedLong); // Frequent call
226 case EC_IOCTL_SET_BUFFER_SIZE:
228 ioctl_general_t param;
229 unsigned long nIgnoredBytes = 0;
230 memset(¶m, '0', sizeof(ioctl_general_t));
231 nIgnoredBytes = copy_from_user (¶m, (void*)arg, sizeof(ioctl_general_t));
232 if (nIgnoredBytes > 0) {
236 if (SetBufferSize(param.m_unsignedLong) == -1) {
241 DPRINTF("Set Buffer Size = %lu", param.m_unsignedLong);
244 case EC_IOCTL_GET_BUFFER_SIZE:
246 ioctl_general_t param;
247 unsigned long nIgnoredBytes = 0;
248 memset(¶m, '0', sizeof(ioctl_general_t));
249 param.m_unsignedLong = GetBufferSize();
250 nIgnoredBytes = copy_to_user ((void*)arg, ¶m, sizeof (ioctl_general_t));
251 if (nIgnoredBytes > 0) {
256 DPRINTF("Get Buffer Size = %lu", param.m_unsignedLong);
259 case EC_IOCTL_RESET_BUFFER:
261 if (ResetBuffer() == -1) {
266 DPRINTF("Reset Buffer");
269 case EC_IOCTL_GET_EC_INFO:
271 if (copy_ec_info_to_user_space ((ec_info_t *) arg) != 0) {
276 // DPRINTF("Get Buffer Status"); // Frequent call
279 case EC_IOCTL_CONSUME_BUFFER:
281 static ec_info_t ec_info_copy;
282 int nIgnoredBytes = 0;
283 #ifndef __DISABLE_RELAYFS
284 struct rchan* pRelayChannel = NULL;
285 struct rchan_buf *buf = NULL;
286 unsigned int nNumOfSubbufs = 0;
287 void* pConsume = NULL;
288 unsigned int nPaddingLength = 0;
289 unsigned int nSubbufSize = 0;
290 unsigned int nDataSize = 0;
291 unsigned int nEffectSize = 0;
292 unsigned int nSubbufDiscardedCount = 0;
294 nIgnoredBytes = copy_from_user (&ec_info_copy, (ec_info_t *) arg, sizeof (ec_info_t));
295 if(nIgnoredBytes > 0)
297 EPRINTF ("copy_from_user(%08X,%08X)=%d", (unsigned) arg, (unsigned) &ec_info_copy, nIgnoredBytes);
302 spin_lock_irqsave (&ec_spinlock, spinlock_flags);
303 if((ec_info_copy.m_nMode & MODEMASK_MULTIPLE_BUFFER) == 0) {
305 if(ec_info.after_last > ec_info.first) {
306 ec_info.buffer_effect = ec_info.buffer_size;
308 if (ec_info.after_last == ec_info.buffer_effect) {
311 ec_info.first = ec_info_copy.after_last;
313 ec_info.trace_size = ec_info.trace_size - ec_info_copy.trace_size;
317 #ifndef __DISABLE_RELAYFS
318 pRelayChannel = GetRelayChannel();
319 if(pRelayChannel == NULL) {
320 EPRINTF("Null pointer to relay channel!");
324 buf = pRelayChannel->buf[0];
325 nNumOfSubbufs = pRelayChannel->n_subbufs;
327 nSubbufSize = pRelayChannel->subbuf_size;
328 pConsume = buf->start + buf->subbufs_consumed % nNumOfSubbufs * nSubbufSize;
329 memcpy(&nPaddingLength, pConsume, sizeof(unsigned int));
330 memcpy(&nSubbufDiscardedCount, pConsume + sizeof(unsigned int), sizeof(unsigned int));
331 nEffectSize = nSubbufSize - nPaddingLength;
332 nDataSize = nEffectSize - RELAY_SUBBUF_HEADER_SIZE;
333 relay_subbufs_consumed(pRelayChannel, 0, 1);
334 ec_info.m_nBeginSubbufNum = buf->subbufs_consumed % nNumOfSubbufs;
335 ec_info.m_nEndSubbufNum = buf->subbufs_produced % nNumOfSubbufs;
336 ec_info.buffer_effect -= nEffectSize;
337 ec_info.trace_size -= nDataSize;
338 buf->dentry->d_inode->i_size = ec_info.trace_size;
341 spin_unlock_irqrestore (&ec_spinlock, spinlock_flags);
343 // DPRINTF("Consume Buffer"); // Frequent call
346 case EC_IOCTL_ADD_PROBE:
348 unsigned long addr = arg;
349 result = add_probe(addr);
353 //@AGv: remove_probe expects probe address instead of name
354 /*case EC_IOCTL_REMOVE_PROBE:
356 char *probe_name = (char *) arg;
357 result = remove_probe (probe_name);
361 case EC_IOCTL_SET_APPDEPS:
364 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
366 EPRINTF("Cannot copy deps size!");
370 DPRINTF("Deps size has been copied (%d)", size);
373 DPRINTF("Deps are size of 0");
377 deps = vmalloc(size);
379 EPRINTF("Cannot alloc mem for deps!");
383 DPRINTF("Mem for deps has been allocated");
385 result = copy_from_user(deps, (void *)arg, size);
387 EPRINTF("Cannot copy deps!");
391 DPRINTF("Deps has been copied successfully");
395 case EC_IOCTL_SET_PID:
399 result = copy_from_user(&_pid, (void *)arg, sizeof(unsigned int));
401 EPRINTF("Cannot copy pid!");
408 DPRINTF("EC_IOCTL_SET_PID pid:%d", inst_pid);
412 case EC_IOCTL_SET_PROFILEBUNDLE:
416 result = copy_from_user(&size, (void *)arg, sizeof(size_t));
418 EPRINTF("Cannot copy bundle size!");
422 DPRINTF("Bundle size has been copied");
424 bundle = vmalloc(size);
425 if (bundle == NULL) {
426 EPRINTF("Cannot alloc mem for bundle!");
430 DPRINTF("Mem for bundle has been alloced");
432 result = copy_from_user(bundle, (void *)arg, size);
434 EPRINTF("Cannot copy bundle!");
438 DPRINTF("Bundle has been copied successfully");
440 if (link_bundle() == -1) {
441 EPRINTF("Cannot link profile bundle!");
448 case EC_IOCTL_RESET_PROBES:
450 result = reset_probes();
454 case EC_IOCTL_UPDATE_CONDS:
457 struct cond *c, *c_tmp, *p_cond;
458 unsigned char *p_data;
461 err = copy_from_user(&args_cnt, (void *)arg, sizeof(int));
466 /* first, delete all the conds */
467 list_for_each_entry_safe(c, c_tmp, &cond_list.list, list) {
471 /* second, add new conds */
472 p_data = (unsigned char *)(arg + sizeof(int));
473 for (i = 0; i < args_cnt; i++) {
474 p_cond = kmalloc(sizeof(struct cond), GFP_KERNEL);
476 DPRINTF("Cannot alloc cond!");
480 err = copy_from_user(&p_cond->tmpl, p_data, sizeof(struct event_tmpl));
482 DPRINTF("Cannot copy cond from user!");
487 list_add(&(p_cond->list), &(cond_list.list));
488 p_data += sizeof(struct event_tmpl);
492 case EC_IOCTL_ATTACH:
493 result = ec_user_attach ();
494 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
495 DPRINTF("EC_IOCTL_ATTACH calling notification chain");
496 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_ATTACH, (void*)NULL);
498 DPRINTF("Attach Probes");
500 case EC_IOCTL_ACTIVATE:
501 result = ec_user_activate ();
502 DPRINTF("Activate Probes");
504 case EC_IOCTL_STOP_AND_DETACH:
506 unsigned long nIgnoredBytes = 0;
508 #ifdef OVERHEAD_DEBUG
509 printk("\nswap_sum_time = %ld in kprobe_handler()\n", swap_sum_time);
510 printk("swap_sum_hit = %ld in kprobe_handler()\n", swap_sum_hit);
514 if(ec_user_stop() != 0) {
518 nIgnoredBytes = copy_ec_info_to_user_space ((ec_info_t*)arg);
519 if(nIgnoredBytes > 0) {
525 DPRINTF("Stop and Detach Probes");
526 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
527 DPRINTF("EC_IOCTL_STOP_AND_DETACH calling notification chain");
528 blocking_notifier_call_chain(&swap_notifier_list, EC_IOCTL_STOP_AND_DETACH, (void*)&gl_nNotifyTgid);
532 case EC_IOCTL_WAIT_NOTIFICATION:
534 static ec_info_t ec_info_copy;
536 ioctl_wait_notification_t ioctl_args;
538 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
545 result = wait_event_interruptible (notification_waiters_queue, ioctl_args.notification_count != notification_count);
548 result = -EINTR; // woken by signal (ERESTARTSYS 512)
552 ioctl_args.notification_count = notification_count;
554 result = copy_to_user ((void *) arg, &ioctl_args, sizeof (ioctl_args));
561 // FIXME: synchronization is necessary here (ec_info must be locked).
562 // ENTER_CRITICAL_SECTION
563 memcpy (&ec_info_copy, &ec_info, sizeof (ec_info_copy));
564 // LEAVE_CRITICAL_SECTION
566 result = copy_to_user ((void *) ioctl_args.p_ec_info, &ec_info_copy, sizeof (ec_info_t));
569 EPRINTF ("copy_to_user(%08X,%08X)=%d", (unsigned) ioctl_args.p_ec_info, (unsigned) &ec_info_copy, result);
576 case EC_IOCTL_US_EVENT:
578 ioctl_us_event_t ioctl_args;
579 result = copy_from_user (&ioctl_args, (void *) arg, sizeof (ioctl_args));
583 EPRINTF ("copy_from_user() failure");
587 if(ioctl_args.len == 0){
589 EPRINTF ("invalid event length!");
592 char *buf = kmalloc(ioctl_args.len, GFP_KERNEL);
595 EPRINTF ("failed to alloc mem for event!");
598 result = copy_from_user (buf, (void *) ioctl_args.data, ioctl_args.len);
601 EPRINTF ("failed to copy event from user space!");
604 result = put_us_event(buf, ioctl_args.len);
609 // DPRINTF("User Space Event"); // Frequent call
613 case EC_IOCTL_SET_EVENT_MASK:
616 result = copy_from_user (&mask, (void *) arg, sizeof (mask));
623 result = set_event_mask (mask);
628 DPRINTF("Set Event Mask = %d", mask);
632 case EC_IOCTL_GET_EVENT_MASK:
635 result = get_event_mask(&mask);
640 result = copy_to_user ((void *) arg, &mask, sizeof (mask));
645 DPRINTF("Get Event Mask = %d", mask);
649 case EC_IOCTL_SET_PREDEF_UPROBES:
651 ioctl_predef_uprobes_info_t data;
652 result = copy_from_user (&data, (void *) arg, sizeof (data));
659 result = set_predef_uprobes (&data);
664 DPRINTF("Set Predefined User Space Probes");
668 case EC_IOCTL_GET_PREDEF_UPROBES:
670 result = get_predef_uprobes((ioctl_predef_uprobes_info_t *)arg);
675 DPRINTF("Get Predefined User Space Probes");
679 case EC_IOCTL_GET_PREDEF_UPROBES_SIZE:
682 result = get_predef_uprobes_size(&size);
687 result = copy_to_user ((void *) arg, &size, sizeof (size));
692 DPRINTF("Get Size of Predefined User Space Probes");
697 EPRINTF ("Unknown driver command = %u", cmd);