mini refactoring
[kernel/swap-modules.git] / driver / us_proc_inst.c
1 ////////////////////////////////////////////////////////////////////////////////////
2 //
3 //      FILE:           us_proc_inst.c
4 //
5 //      DESCRIPTION:
6 //      This file is C source for SWAP driver.
7 //
8 //      SEE ALSO:       us_proc_inst.h
9 //      AUTHOR:         A.Gerenkov, E. Gorelkina
10 //      COMPANY NAME:   Samsung Research Center in Moscow
11 //      DEPT NAME:      Advanced Software Group
12 //      CREATED:        2008.06.02
13 //      VERSION:        1.0
14 //      REVISION DATE:  2008.12.02
15 //
16 ////////////////////////////////////////////////////////////////////////////////////
17
18 #include "module.h"
19 #include "us_proc_inst.h"
20
21 #include "../kprobe/dbi_kprobes_deps.h"
22 #include "../kprobe/dbi_uprobes.h"
23
24 #define mm_read_lock(task, mm, atomic, lock)                    \
25         mm = atomic ? task->active_mm : get_task_mm(task);      \
26         if (mm == NULL) {                                       \
27                 /* FIXME: */                                    \
28                 panic("ERRR mm_read_lock: mm == NULL\n");       \
29         }                                                       \
30                                                                 \
31         if (atomic) {                                           \
32                 lock = down_read_trylock(&mm->mmap_sem);        \
33         } else {                                                \
34                 lock = 1;                                       \
35                 down_read(&mm->mmap_sem);                       \
36         }
37
38 #define mm_read_unlock(mm, atomic, lock)                        \
39         if (lock) {                                             \
40                 up_read(&mm->mmap_sem);                         \
41         }                                                       \
42                                                                 \
43         if (!atomic) {                                          \
44                 mmput(mm);                                      \
45         }
46
47 DEFINE_PER_CPU (us_proc_vtp_t *, gpVtp) = NULL;
48 DEFINE_PER_CPU (struct pt_regs *, gpCurVtpRegs) = NULL;
49
50 #if defined(CONFIG_MIPS)
51 #       define ARCH_REG_VAL(regs, idx)  regs->regs[idx]
52 #elif defined(CONFIG_ARM)
53 #       define ARCH_REG_VAL(regs, idx)  regs->uregs[idx]
54 #else
55 #       define ARCH_REG_VAL(regs, idx)  0
56 #       warning ARCH_REG_VAL is not implemented for this architecture. FBI will work improperly or even crash!!!
57 #endif // ARCH
58
59 unsigned long ujprobe_event_pre_handler (us_proc_ip_t * ip, struct pt_regs *regs);
60 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6);
61 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, us_proc_ip_t * ip);
62
63 static int register_usprobe(struct task_struct *task, us_proc_ip_t *ip, int atomic);
64 static int unregister_usprobe(struct task_struct *task, us_proc_ip_t * ip, int atomic, int no_rp2);
65
66 int us_proc_probes;
67
68 struct task_inst_info_node {
69         struct list_head      plist;
70         inst_us_proc_t *      task_inst_info;
71         int                   tgid;
72 };
73 LIST_HEAD(task_inst_info_list);
74
75 #ifdef SLP_APP
76 struct dentry *launchpad_daemon_dentry = NULL;
77 EXPORT_SYMBOL_GPL(launchpad_daemon_dentry);
78 #endif /* SLP_APP */
79
80 #ifdef ANDROID_APP
81 unsigned long android_app_vma_start = 0;
82 unsigned long android_app_vma_end = 0;
83 struct dentry *app_process_dentry = NULL;
84 #endif /* ANDROID_APP */
85
86 #ifdef __ANDROID
87 struct dentry *libdvm_dentry = NULL;
88 /* Defines below are for libdvm.so with md5sum:
89  * 5941c87b49198368e7db726c2977bf1d */
90 #define LIBDVM_ENTRY 0x30a64
91 #define LIBDVM_RETURN 0x30bdc
92 #endif /* __ANDROID */
93
94
95 static inline int is_libonly(void)
96 {
97         return !strcmp(us_proc_info.path,"*");
98 }
99
100 // is user-space instrumentation
101 static inline int is_us_instrumentation(void)
102 {
103         return !!us_proc_info.path;
104 }
105
106 us_proc_otg_ip_t *find_otg_probe(unsigned long addr)
107 {
108         us_proc_otg_ip_t *p;
109
110         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
111                 if (p->ip.offset == addr) {
112                         return p;
113                 }
114         }
115
116         return NULL;
117 }
118
119 int add_otg_probe_to_list(unsigned long addr, us_proc_otg_ip_t **pprobe)
120 {
121         us_proc_otg_ip_t *new_probe;
122         us_proc_otg_ip_t *probe;
123
124         if (pprobe) {
125                 *pprobe = NULL;
126         }
127         /* check if such probe does already exist */
128         probe = find_otg_probe(addr);
129         if (probe) {
130                 return 1;
131         }
132
133         new_probe = kmalloc(sizeof(us_proc_otg_ip_t), GFP_KERNEL);
134         if (!new_probe) {
135                 EPRINTF ("no memory for new probe!");
136                 return -ENOMEM;
137         }
138         memset(new_probe,0, sizeof(us_proc_otg_ip_t));
139
140         new_probe->ip.offset = addr;
141         new_probe->ip.jprobe.kp.addr =
142                 new_probe->ip.retprobe.kp.addr = (kprobe_opcode_t *)addr;
143         new_probe->ip.jprobe.priv_arg =
144                 new_probe->ip.retprobe.priv_arg = new_probe;
145
146         INIT_LIST_HEAD(&new_probe->list);
147         list_add_rcu(&new_probe->list, &otg_us_proc_info);
148
149         if (pprobe) {
150                 *pprobe = new_probe;
151         }
152         return 0;
153 }
154
155 int remove_otg_probe_from_list(unsigned long addr)
156 {
157         us_proc_otg_ip_t *p;
158
159         //check if such probe does exist
160         p = find_otg_probe(addr);
161         if (!p) {
162                 /* We do not care about it. Nothing bad. */
163                 return 0;
164         }
165
166         list_del_rcu(&p->list);
167
168         kfree (p);
169
170         return 0;
171 }
172
173
174 static struct proc_probes *proc_probes_copy(struct proc_probes *proc_p);
175 static void print_proc_probes(const struct proc_probes *proc_p);
176 /**
177  * Prepare copy of instrumentation data for task
178  * in case of library only instrumentation
179  */
180
181 inst_us_proc_t* copy_task_inst_info (struct task_struct *task, inst_us_proc_t * task_inst_info)
182 {
183         int i, j;
184         kprobe_opcode_t *entry_save;
185         kprobe_pre_entry_handler_t pre_entry_save;
186         kretprobe_handler_t handler_save;
187
188         inst_us_proc_t* copy_info = 0;
189
190         int unres_ips_count = 0, unres_vtps_count = 0;
191
192         copy_info = kmalloc (sizeof (inst_us_proc_t), GFP_ATOMIC);
193         memset ((void *) copy_info, 0, sizeof (inst_us_proc_t));
194
195         copy_info->path = task_inst_info->path;
196         copy_info->m_f_dentry = NULL;
197
198         copy_info->libs_count = task_inst_info->libs_count;
199         copy_info->p_libs =
200                 kmalloc (task_inst_info->libs_count * sizeof (us_proc_lib_t), GFP_ATOMIC);
201
202         if (!copy_info->p_libs) {
203                 DPRINTF ("No enough memory for copy_info->p_libs");
204                 return NULL;
205         }
206         memcpy (copy_info->p_libs, task_inst_info->p_libs,
207                         copy_info->libs_count * sizeof (us_proc_lib_t));
208
209         for (i = 0; i < copy_info->libs_count; i++) {
210                 if (copy_info->p_libs[i].ips_count > 0)
211                 {
212                         unres_ips_count += copy_info->p_libs[i].ips_count;
213
214                         copy_info->p_libs[i].p_ips =
215                                 kmalloc (copy_info->p_libs[i].ips_count * sizeof (us_proc_ip_t), GFP_ATOMIC);
216
217                         if (!copy_info->p_libs[i].p_ips) {
218                                 DPRINTF ("No enough memory for copy_info->p_libs[i].p_ips");
219                                 return NULL;
220                         }
221
222                         memcpy (copy_info->p_libs[i].p_ips, task_inst_info->p_libs[i].p_ips,
223                                         copy_info->p_libs[i].ips_count * sizeof (us_proc_ip_t));
224                         for (j = 0; j < copy_info->p_libs[i].ips_count; j++) {
225                                 // save handlers
226                                 entry_save = copy_info->p_libs[i].p_ips[j].jprobe.entry;
227                                 pre_entry_save = copy_info->p_libs[i].p_ips[j].jprobe.pre_entry;
228                                 handler_save = copy_info->p_libs[i].p_ips[j].retprobe.handler;
229
230                                 copy_info->p_libs[i].p_ips[j].installed = 0;
231                                 memset(&copy_info->p_libs[i].p_ips[j].jprobe, 0, sizeof(struct jprobe));
232                                 memset(&copy_info->p_libs[i].p_ips[j].retprobe, 0, sizeof(struct kretprobe));
233
234                                 // restore handlers
235                                 copy_info->p_libs[i].p_ips[j].jprobe.entry = entry_save;
236                                 copy_info->p_libs[i].p_ips[j].jprobe.pre_entry = pre_entry_save;
237                                 copy_info->p_libs[i].p_ips[j].retprobe.handler = handler_save;
238                         }
239
240                         unres_ips_count += copy_info->p_libs[i].ips_count;
241                 }
242
243                 for (j = 0; j < copy_info->p_libs[i].plt_count; j++)
244                 {
245                         copy_info->p_libs[i].p_plt[j].real_func_addr = 0;
246                 }
247
248                 if (copy_info->p_libs[i].vtps_count > 0) {
249                         unres_vtps_count += copy_info->p_libs[i].vtps_count;
250
251                         copy_info->p_libs[i].p_vtps =
252                                 kmalloc (copy_info->p_libs[i].vtps_count * sizeof (us_proc_vtp_t), GFP_ATOMIC);
253
254                         if (!copy_info->p_libs[i].p_vtps) {
255                                 DPRINTF ("No enough memory for copy_info->p_libs[i].p_vtps");
256                                 return NULL;
257                         }
258
259                         memcpy (copy_info->p_libs[i].p_vtps, task_inst_info->p_libs[i].p_vtps,
260                                         copy_info->p_libs[i].vtps_count * sizeof (us_proc_vtp_t));
261                         for (j = 0; j < copy_info->p_libs[i].vtps_count; j++) {
262                                 copy_info->p_libs[i].p_vtps[j].installed = 0;
263                                 memset (&copy_info->p_libs[i].p_vtps[j].jprobe, 0, sizeof(struct jprobe));
264                         }
265                         unres_vtps_count = copy_info->p_libs[i].vtps_count;
266                 }
267
268                 copy_info->p_libs[i].m_f_dentry = task_inst_info->p_libs[i].m_f_dentry;
269                 copy_info->p_libs[i].loaded = 0;
270
271                 copy_info->p_libs[i].vma_start = 0;
272                 copy_info->p_libs[i].vma_end = 0;
273         }
274         copy_info->unres_ips_count = unres_ips_count;
275         copy_info->unres_vtps_count = unres_vtps_count;
276
277         copy_info->pp = proc_probes_copy(task_inst_info->pp);
278
279 //      print_proc_probes(copy_info->pp);
280
281         return copy_info;
282 }
283
284 inst_us_proc_t* get_task_inst_node(struct task_struct *task)
285 {
286         struct task_inst_info_node *node, *tnode;
287
288         list_for_each_entry_safe(node, tnode, &task_inst_info_list, plist)
289         {
290                 if (node && task && node->tgid == task->tgid) {
291                         return node->task_inst_info;
292                 }
293         }
294         return NULL;
295 }
296
297 void put_task_inst_node(struct task_struct *task, inst_us_proc_t *task_inst_info)
298 {
299         struct task_inst_info_node * node;
300
301         node = kmalloc (sizeof(struct task_inst_info_node), GFP_ATOMIC);
302
303         node->tgid = task->tgid;
304         node->task_inst_info = task_inst_info;
305
306         list_add_tail (&(node->plist), &task_inst_info_list);
307 }
308
309
310 void clear_task_inst_info(void)
311 {
312         struct list_head *node, *tmp;
313
314         list_for_each_safe(node, tmp, &task_inst_info_list)
315                 list_del(node);
316 }
317
318 #ifdef SLP_APP
319 static int is_slp_app_with_dentry(struct vm_area_struct *vma,
320                                                                   struct dentry *dentry)
321 {
322         struct vm_area_struct *slp_app_vma = NULL;
323
324         if (vma->vm_file->f_dentry == launchpad_daemon_dentry) {
325                 slp_app_vma = vma;
326                 while (slp_app_vma) {
327                         if (slp_app_vma->vm_file) {
328                                 if (slp_app_vma->vm_file->f_dentry == dentry &&
329                                         slp_app_vma->vm_pgoff == 0) {
330                                         return 1;
331                                 }
332                         }
333                         slp_app_vma = slp_app_vma->vm_next;
334                 }
335         }
336
337         return 0;
338 }
339 #endif /* SLP_APP */
340
341 #ifdef ANDROID_APP
342 static int is_android_app_with_dentry(struct vm_area_struct *vma,
343                                                                           struct dentry *dentry)
344 {
345         struct vm_area_struct *android_app_vma = NULL;
346
347         if (vma->vm_file->f_dentry == app_process_dentry) {
348                 android_app_vma = vma;
349                 while (android_app_vma) {
350                         if (android_app_vma->vm_file) {
351                                 if (android_app_vma->vm_file->f_dentry == dentry) {
352                                         android_app_vma_start = android_app_vma->vm_start;
353                                         android_app_vma_end = android_app_vma->vm_end;
354                                         return 1;
355                                 }
356                         }
357                         android_app_vma = android_app_vma->vm_next;
358                 }
359         }
360
361         return 0;
362 }
363 #endif /* ANDROID_APP */
364
365 #ifdef __ANDROID
366 void find_libdvm_for_task(struct task_struct *task, inst_us_proc_t *info)
367 {
368         struct vm_area_struct *vma = NULL;
369         struct mm_struct *mm = NULL;
370
371         mm = get_task_mm(task);
372         if (mm) {
373                 vma = mm->mmap;
374                 while (vma) {
375                         if (vma->vm_file) {
376                                 if (vma->vm_file->f_dentry == libdvm_dentry) {
377                                         info->libdvm_start = vma->vm_start;
378                                         info->libdvm_end = vma->vm_end;
379                                         break;
380                                 }
381                         }
382                         vma = vma->vm_next;
383                 }
384                 mmput(mm);
385         }
386 }
387 #endif /* __ANDROID */
388
389 struct dentry *dentry_by_path(const char *path)
390 {
391         struct dentry *dentry;
392 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
393         struct path st_path;
394         if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
395 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
396         struct nameidata nd;
397         if (path_lookup(path, LOOKUP_FOLLOW, &nd) != 0) {
398 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
399                 EPRINTF("failed to lookup dentry for path %s!", path);
400                 return NULL;
401         }
402
403 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
404         dentry = nd.dentry;
405         path_release(&nd);
406 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
407         dentry = nd.path.dentry;
408         path_put(&nd.path);
409 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
410         dentry = st_path.dentry;
411         path_put(&st_path);
412 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
413         return dentry;
414 }
415
416 static int find_task_by_path (const char *path, struct task_struct **p_task, struct list_head *tids)
417 {
418         int found = 0;
419         struct task_struct *task;
420         struct vm_area_struct *vma;
421         struct mm_struct *mm;
422         struct dentry *dentry = dentry_by_path(path);
423
424         *p_task = 0;
425
426         /* find corresponding dir entry, this is also check for valid path */
427         // TODO: test - try to instrument process with non-existing path
428         // TODO: test - try to instrument process  with existing path and delete file just after start
429         if (dentry == NULL) {
430                 return -EINVAL;
431         }
432
433         rcu_read_lock();
434         for_each_process (task) {
435
436                 if  ( 0 != inst_pid && ( inst_pid != task->pid ) )
437                         continue;
438
439                 mm = get_task_mm(task);
440                 if (!mm)
441                         continue;
442                 vma = mm->mmap;
443                 while (vma) {
444                         if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
445                                 if (vma->vm_file->f_dentry == dentry) {
446                                         if (!*p_task) {
447                                                 *p_task = task;
448                                                 get_task_struct (task);
449                                         }
450                                                 //break;
451                                 }
452 #ifdef SLP_APP
453                                 if (!*p_task) {
454                                         if (is_slp_app_with_dentry(vma, dentry)) {
455                                                 *p_task = task;
456                                                 get_task_struct(task);
457                                         }
458                                 }
459 #endif /* SLP_APP */
460 #ifdef ANDROID_APP
461                                 if (!*p_task) {
462                                         if (is_android_app_with_dentry(vma, dentry)) {
463                                                 *p_task = task;
464                                                 get_task_struct(task);
465                                         }
466                                 }
467 #endif /* ANDROID_APP */
468                         }
469                         vma = vma->vm_next;
470                 }
471                 // only decrement usage count on mm since we cannot sleep here
472                 atomic_dec(&mm->mm_users);
473                 if (found)
474                         break;
475         }
476         rcu_read_unlock();
477
478         if (*p_task) {
479                 DPRINTF ("found pid %d for %s.", (*p_task)->pid, path);
480                 *p_task = (*p_task)->group_leader;
481                 gl_nNotifyTgid = (*p_task)->tgid;
482         } else {
483                 DPRINTF ("pid for %s not found!", path);
484         }
485
486         return 0;
487 }
488
489
490 static void us_vtp_event_pre_handler (us_proc_vtp_t * vtp, struct pt_regs *regs)
491 {
492         __get_cpu_var(gpVtp) = vtp;
493         __get_cpu_var(gpCurVtpRegs) = regs;
494 }
495
496 static void us_vtp_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
497 {
498         us_proc_vtp_t *vtp = __get_cpu_var(gpVtp);
499 #if !defined(CONFIG_X86)
500         struct pt_regs *regs = __get_cpu_var(gpCurVtpRegs);
501 #endif
502         char fmt[4];
503         unsigned long vaddr;
504         long ival;
505         char cval, *sval;
506         us_proc_vtp_data_t *vtp_data;
507 unsigned long ll;
508         fmt[0] = 'p';
509         fmt[3] = 0;
510         fmt[2] = 's';
511
512         list_for_each_entry_rcu (vtp_data, &vtp->list, list) {
513                 //              DPRINTF ("[%d]proc %s(%d): %lx", nCount++, current->comm, current->pid, vtp->addr);
514                 fmt[1] = vtp_data->type;
515                 if (vtp_data->reg == -1)
516                         vaddr = vtp_data->off;
517                 else
518                         vaddr = ARCH_REG_VAL (regs, vtp_data->reg) + vtp_data->off;
519                 //              DPRINTF ("VTP type '%c'", vtp_data->type);
520                 switch (vtp_data->type)
521                 {
522                         case 'd':
523                         case 'x':
524                         case 'p':
525                                 if (read_proc_vm_atomic (current, vaddr, &ival, sizeof (ival)) < sizeof (ival))
526                                         EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
527                                 else
528                                         pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
529                                 break;
530                         case 'f':
531                                 if (read_proc_vm_atomic (current, vaddr, &ival, sizeof (ival)) < sizeof (ival))
532                                         EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
533                                 else
534                                         pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
535                                 break;
536                         case 'c':
537                                 if (read_proc_vm_atomic (current, vaddr, &cval, sizeof (cval)) < sizeof (cval))
538                                         EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
539                                 else
540                                         pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, cval, vtp_data->name);
541                                 break;
542                         case 's':
543                                 if (current->active_mm) {
544                                         struct page *page;
545                                         struct vm_area_struct *vma;
546                                         void *maddr;
547                                         int len;
548                                         if (get_user_pages_atomic (current, current->active_mm, vaddr, 1, 0, 1, &page, &vma) <= 0) {
549                                                 EPRINTF ("get_user_pages_atomic failed for proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
550                                                 break;
551                                         }
552                                         maddr = kmap_atomic (page, KM_USER0);
553                                         len = strlen (maddr + (vaddr & ~PAGE_MASK));
554                                         sval = kmalloc (len + 1, GFP_KERNEL);
555                                         if (!sval)
556                                                 EPRINTF ("failed to alloc memory for string in proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
557                                         else {
558                                                 copy_from_user_page (vma, page, vaddr, sval, maddr + (vaddr & ~PAGE_MASK), len + 1);
559                                                 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, sval,  vtp_data->name);
560                                                 kfree (sval);
561                                         }
562                                         kunmap_atomic (maddr, KM_USER0);
563                                         page_cache_release (page);
564                                 }
565                                 else
566                                         EPRINTF ("task %s/%u has no mm!", current->comm, current->pid);
567                                 break;
568                         default:
569                                 EPRINTF ("unknown variable type '%c'", vtp_data->type);
570                 }
571         }
572         dbi_uprobe_return ();
573 }
574
575 static int install_mapped_ips (struct task_struct *task, inst_us_proc_t* task_inst_info, int atomic)
576 {
577         struct vm_area_struct *vma;
578         int i, k, err;
579         unsigned long addr;
580         unsigned int old_ips_count, old_vtps_count;
581         us_proc_otg_ip_t *p;
582         struct task_struct *t;
583         struct mm_struct *mm;
584
585         mm = atomic ? task->active_mm : get_task_mm (task);
586         if (!mm) {
587                 return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
588         }
589         old_ips_count = task_inst_info->unres_ips_count;
590         old_vtps_count = task_inst_info->unres_vtps_count;
591         if(!atomic)
592                 down_read (&mm->mmap_sem);
593         vma = mm->mmap;
594         while (vma) {
595                 // skip non-text section
596 #ifndef __ANDROID
597                 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file || (vma->vm_flags & VM_ACCOUNT) ||
598                         !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
599                         !(vma->vm_flags & (VM_READ | VM_MAYREAD))) {
600 #else // __ANDROID
601                 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file) {
602 #endif // __ANDROID
603                         vma = vma->vm_next;
604                         continue;
605                 }
606                 /**
607                  * After process was forked, some time it inherits parent process environment.
608                  * We need to renew instrumentation when we detect that process gets own environment.
609                  */
610                 for (i = 0; i < task_inst_info->libs_count; i++) {
611 //                      struct path tmp_path;
612 //                      tmp_path.dentry = task_inst_info->p_libs[i].m_f_dentry;
613 //                      tmp_path.mnt = task_inst_info->p_libs[i].m_vfs_mount;
614 //                      char* p_path = d_path ( &tmp_path, path_buffer, 255 );
615 //                      DPRINTF("f_dentry:%x m_f_dentry:%x path:%s", vma->vm_file->f_dentry,
616 //                              task_inst_info->p_libs[i].m_f_dentry, p_path );
617
618                         //TODO: test - try to instrument non-existing libs
619                         if (vma->vm_file->f_dentry == task_inst_info->p_libs[i].m_f_dentry) {
620 //                              DPRINTF("vm_flags:%x loaded:%x ips_count:%d vtps_count:%d",
621 //                                              vma->vm_flags, task_inst_info->p_libs[i].loaded,
622 //                                              task_inst_info->p_libs[i].ips_count, task_inst_info->p_libs[i].vtps_count );
623                                 if (!task_inst_info->p_libs[i].loaded) {
624 //                                      DPRINTF("!VM_EXECUTABLE && !loaded");
625                                         char *p;
626                                         int app_flag = (vma->vm_file->f_dentry == task_inst_info->m_f_dentry);
627                                         DPRINTF ("post dyn lib event %s/%s", current->comm, task_inst_info->p_libs[i].path);
628                                         // if we installed something, post library info for those IPs
629                                         p = strrchr(task_inst_info->p_libs[i].path, '/');
630                                         if(!p)
631                                                 p = task_inst_info->p_libs[i].path;
632                                         else
633                                                 p++;
634                                         task_inst_info->p_libs[i].loaded = 1;
635                                         task_inst_info->p_libs[i].vma_start = vma->vm_start;
636                                         task_inst_info->p_libs[i].vma_end = vma->vm_end;
637                                         task_inst_info->p_libs[i].vma_flag = vma->vm_flags;
638                                         pack_event_info (DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
639                                                         task->tgid, p, vma->vm_start, vma->vm_end-vma->vm_start, app_flag);
640                                 }
641                                 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++) {
642                                         DPRINTF("ips_count current:%d", k);
643                                         if (!task_inst_info->p_libs[i].p_ips[k].installed) {
644                                                 DPRINTF("!installed");
645                                                 addr = task_inst_info->p_libs[i].p_ips[k].offset;
646                                                 addr += vma->vm_start;
647                                                 if (page_present (mm, addr)) {
648                                                         DPRINTF ("pid %d, %s sym is loaded at %lx/%lx.",
649                                                                 task->pid, task_inst_info->p_libs[i].path,
650                                                                 task_inst_info->p_libs[i].p_ips[k].offset, addr);
651                                                         task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr = (kprobe_opcode_t *) addr;
652                                                         task_inst_info->p_libs[i].p_ips[k].retprobe.kp.addr = (kprobe_opcode_t *) addr;
653                                                         task_inst_info->unres_ips_count--;
654                                                         err = register_usprobe(task, &task_inst_info->p_libs[i].p_ips[k], atomic);
655                                                         if (err != 0) {
656                                                                 DPRINTF ("failed to install IP at %lx/%p. Error %d!",
657                                                                         task_inst_info->p_libs[i].p_ips[k].offset,
658                                                                         task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
659                                                         }
660                                                 }
661                                         }
662                                 }
663                                 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++) {
664                                         DPRINTF("vtps_count current:%d", k);
665                                         if (!task_inst_info->p_libs[i].p_vtps[k].installed) {
666                                                 DPRINTF("!installed");
667                                                 addr = task_inst_info->p_libs[i].p_vtps[k].addr;
668                                                 if (!(vma->vm_flags & VM_EXECUTABLE))
669                                                         addr += vma->vm_start;
670                                                 if (page_present (mm, addr)) {
671                                                         DPRINTF ("pid %d, %s sym is loaded at %lx/%lx.",
672                                                                 task->pid, task_inst_info->p_libs[i].path,
673                                                                 task_inst_info->p_libs[i].p_ips[k].offset, addr);
674                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.tgid = task_inst_info->tgid;
675                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr = (kprobe_opcode_t *) addr;
676                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.entry = (kprobe_opcode_t *) us_vtp_event_handler;
677                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.pre_entry = (kprobe_pre_entry_handler_t) us_vtp_event_pre_handler;
678                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.priv_arg = &task_inst_info->p_libs[i].p_vtps[k];
679                                                         task_inst_info->p_libs[i].p_vtps[k].installed = 1;
680                                                         task_inst_info->unres_vtps_count--;
681                                                         err = dbi_register_ujprobe(task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
682                                                         if ( err != 0 ) {
683                                                                 EPRINTF ("failed to install VTP at %p. Error %d!",
684                                                                                 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr, err);
685                                                         }
686                                                 }
687                                         }
688                                 }
689                         }
690                 }
691 #ifdef __ANDROID
692                 if (is_java_inst_enabled()
693                     && vma->vm_file->f_dentry == libdvm_dentry) {
694                         us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
695                         if (!entp->installed
696                             && task_inst_info->libdvm_start) {
697                                 unsigned long addr = LIBDVM_ENTRY + task_inst_info->libdvm_start;
698                                 if (page_present(mm, addr)) {
699                                         entp->jprobe.kp.tgid = task->tgid;
700                                         entp->jprobe.pre_entry = ujprobe_event_pre_handler;
701                                         entp->jprobe.entry = ujprobe_event_handler;
702                                         entp->jprobe.priv_arg = entp;
703                                         entp->jprobe.kp.addr = addr;
704                                         entp->retprobe.kp.tgid = task->tgid;
705                                         entp->retprobe.handler = uretprobe_event_handler;
706                                         entp->retprobe.priv_arg = entp;
707                                         entp->retprobe.kp.addr = addr;
708                                         err = register_usprobe(task, mm, entp, atomic, 0);
709                                         if (err != 0) {
710                                                 DPRINTF("failed to install IP at %p", addr);
711                                         }
712                                 }
713                                 entp->installed = 1;
714                         }
715                         us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
716                         if (!retp->installed
717                             && task_inst_info->libdvm_start) {
718                                 unsigned long addr = LIBDVM_RETURN + task_inst_info->libdvm_start;
719                                 if (page_present(mm, addr)) {
720                                         retp->jprobe.kp.tgid = task->tgid;
721                                         retp->jprobe.pre_entry = ujprobe_event_pre_handler;
722                                         retp->jprobe.entry = ujprobe_event_handler;
723                                         retp->jprobe.priv_arg = retp;
724                                         retp->jprobe.kp.addr = addr;
725                                         retp->retprobe.kp.tgid = task->tgid;
726                                         retp->retprobe.handler = uretprobe_event_handler;
727                                         retp->retprobe.priv_arg = retp;
728                                         retp->retprobe.kp.addr = addr;
729                                         err = register_usprobe(task, mm, retp, atomic, 0);
730                                         if (err != 0) {
731                                                 DPRINTF("failed to install IP at %p", addr);
732                                         }
733                                 }
734                                 retp->installed = 1;
735                         }
736                 }
737 #endif /* __ANDROID */
738                 vma = vma->vm_next;
739         }
740         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
741                 if (p->ip.installed) {
742                         continue;
743                 }
744
745                 if (!page_present(mm, p->ip.offset)) {
746                         DPRINTF("Page isn't present for %p.",
747                                 p->ip.offset);
748                         continue;
749                 }
750
751                 err = register_usprobe(task, &p->ip, atomic);
752                 if (err != 0) {
753                         DPRINTF("failed to install IP at %lx/%p. Error %d!",
754                                 p->ip.offset,
755                                 p->ip.jprobe.kp.addr, err);
756                         continue;
757                 }
758                 task_inst_info->unres_otg_ips_count--;
759         }
760         if (!atomic) {
761                 up_read (&mm->mmap_sem);
762                 mmput (mm);
763         }
764         return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
765 }
766
767 int install_otg_ip(unsigned long addr,
768                         kprobe_pre_entry_handler_t pre_handler,
769                         unsigned long jp_handler,
770                         kretprobe_handler_t rp_handler)
771 {
772         int err;
773         us_proc_otg_ip_t *pprobe;
774         struct task_struct *task = current->group_leader;
775         struct mm_struct *mm = task->mm;
776
777         /* Probe preparing */
778         err = add_otg_probe_to_list(addr, &pprobe);
779         if (err) {
780                 if (err == 1) {
781                         DPRINTF("OTG probe %p already installed.", addr);
782                         return 0;
783                 } else {
784                         DPRINTF("Failed to add new OTG probe, err=%d",err);
785                         return err;
786                 }
787         }
788
789         pprobe->ip.jprobe.pre_entry = pre_handler;
790         pprobe->ip.jprobe.entry = (kprobe_opcode_t *)jp_handler;
791         pprobe->ip.retprobe.handler = rp_handler;
792
793         pprobe->tgid = task->tgid;
794         if (!page_present(mm, addr)) {
795                 DPRINTF("Page isn't present for %p.", addr);
796
797                 us_proc_info.unres_otg_ips_count++;
798                 /* Probe will be installed in do_page_fault handler */
799                 return 0;
800         }
801         DPRINTF("Page present for %p.", addr);
802
803         /* Probe installing */
804         err = register_usprobe(task, &pprobe->ip, 1);
805         if (err != 0) {
806                 DPRINTF("failed to install IP at %lx/%p. Error %d!",
807                          addr, pprobe->ip.jprobe.kp.addr, err);
808                 return err;
809         }
810
811         return 0;
812 }
813 EXPORT_SYMBOL_GPL(install_otg_ip);
814
815
816 static int uninstall_mapped_ips (struct task_struct *task,  inst_us_proc_t* task_inst_info, int atomic)
817 {
818         int i, k, err;
819         us_proc_otg_ip_t *p;
820
821         for (i = 0; i < task_inst_info->libs_count; i++)
822         {
823                 DPRINTF ("clear lib %s.", task_inst_info->p_libs[i].path);
824                 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++)
825                 {
826                         if (task_inst_info->p_libs[i].p_ips[k].installed)
827                         {
828                                 DPRINTF ("remove IP at %p.", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr);
829                                 err = unregister_usprobe (task, &task_inst_info->p_libs[i].p_ips[k], atomic, 0);
830                                 if (err != 0)
831                                 {
832                                         EPRINTF ("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
833                                         continue;
834                                 }
835                                 task_inst_info->unres_ips_count++;
836                         }
837                 }
838                 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++)
839                 {
840                         if (task_inst_info->p_libs[i].p_vtps[k].installed)
841                         {
842                                 dbi_unregister_ujprobe (task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
843                                 task_inst_info->unres_vtps_count++;
844                                 task_inst_info->p_libs[i].p_vtps[k].installed = 0;
845                         }
846                 }
847                 task_inst_info->p_libs[i].loaded = 0;
848         }
849 #ifdef __ANDROID
850         if (is_java_inst_enabled()) {
851                 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
852                 if (entp->installed) {
853                         unregister_usprobe(task, entp, atomic);
854                         entp->installed = 0;
855                 }
856                 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
857                 if (retp->installed) {
858                         unregister_usprobe(task, retp, atomic);
859                         retp->installed = 0;
860                 }
861         }
862 #endif /* __ANDROID */
863         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
864                 if (!p->ip.installed) {
865                         continue;
866                 }
867                 DPRINTF("remove OTG IP at %p.", p->ip.offset);
868                 err = unregister_usprobe(task, &p->ip, atomic, 0);
869                 if (err != 0) {
870                         EPRINTF("failed to uninstall IP at %p. Error %d!",
871                                  p->ip.jprobe.kp.addr, err);
872                         continue;
873                 }
874         }
875
876         DPRINTF ("Ures IPs  %d.", task_inst_info->unres_ips_count);
877         DPRINTF ("Ures VTPs %d.", task_inst_info->unres_vtps_count);
878         return 0;
879 }
880
881 static int uninstall_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
882 {
883         kernel_probe_t *probe = NULL;
884         int iRet = 0;
885         if (probes_flags & kflag) {
886                 probe = find_probe(addr);
887                 if (probe) {
888                         iRet = remove_probe_from_list (addr);
889                         if (iRet)
890                                 EPRINTF ("remove_probe_from_list(0x%lx) result=%d!", addr, iRet);
891                         if (pprobe)
892                                 *pprobe = NULL;
893                 }
894                 probes_flags &= ~kflag;
895         }
896         if (us_proc_probes & uflag) {
897                 if (!(probes_flags & uflag)) {
898                         if (probe) {
899                                 iRet = unregister_kernel_probe(probe);
900                                 if (iRet) {
901                                         EPRINTF ("unregister_kernel_probe(0x%lx) result=%d!",
902                                                         addr, iRet);
903                                         return iRet;
904                                 }
905                         }
906                 }
907                 us_proc_probes &= ~uflag;
908         }
909         return iRet;
910 }
911
912
913 #include "new_dpf.h"
914
915 static int uninstall_us_proc_probes(struct task_struct *task, struct proc_probes *proc_p, enum US_FLAGS flag);
916
917 int deinst_usr_space_proc (void)
918 {
919         int iRet = 0, found = 0;
920         struct task_struct *task = 0;
921         inst_us_proc_t *task_inst_info = NULL;
922
923         if (!is_us_instrumentation()) {
924                 return 0;
925         }
926
927         iRet = uninstall_kernel_probe (pf_addr, US_PROC_PF_INSTLD,
928                         0, &pf_probe);
929         if (iRet)
930                 EPRINTF ("uninstall_kernel_probe(do_page_fault) result=%d!", iRet);
931
932         iRet = uninstall_kernel_probe (cp_addr, US_PROC_CP_INSTLD,
933                         0, &cp_probe);
934         if (iRet)
935                 EPRINTF ("uninstall_kernel_probe(copy_process) result=%d!", iRet);
936
937         iRet = uninstall_kernel_probe (mr_addr, US_PROC_MR_INSTLD,
938                         0, &mr_probe);
939         if (iRet)
940                 EPRINTF ("uninstall_kernel_probe(mm_release) result=%d!", iRet);
941
942         iRet = uninstall_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD,
943                         0, &exit_probe);
944         if (iRet)
945                 EPRINTF ("uninstall_kernel_probe(do_exit) result=%d!", iRet);
946
947         iRet = uninstall_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD,
948                         0, &unmap_probe);
949         if (iRet)
950                 EPRINTF ("uninstall_kernel_probe(do_munmap) result=%d!", iRet);
951
952         if (is_libonly())
953         {
954                 for_each_process (task)
955                 {
956                         task_inst_info = get_task_inst_node(task);
957                         if (task_inst_info)
958                         {
959                                 iRet = uninstall_us_proc_probes(task, task_inst_info->pp, US_UNREGS_PROBE);
960                                 if (iRet != 0)
961                                         EPRINTF ("failed to uninstall IPs (%d)!", iRet);
962                                 dbi_unregister_all_uprobes(task, 1);
963                         }
964                 }
965         }
966         else
967         {
968                 if (us_proc_info.tgid == 0)
969                         return 0;
970                         rcu_read_lock ();
971                 for_each_process (task)
972                 {
973                         if (task->tgid == us_proc_info.tgid)
974                         {
975                                 found = 1;
976                                 get_task_struct (task);
977                                 break;
978                         }
979                 }
980                 rcu_read_unlock ();
981                 if (found)
982                 {
983                         int i;
984                         // uninstall IPs
985                         iRet = uninstall_us_proc_probes(task, us_proc_info.pp, US_UNREGS_PROBE);
986                         if (iRet != 0)
987                         EPRINTF ("failed to uninstall IPs %d!", iRet);
988                         put_task_struct (task);
989
990                         printk("### 1 ### dbi_unregister_all_uprobes:\n");
991                         dbi_unregister_all_uprobes(task, 1);
992                         us_proc_info.tgid = 0;
993                         for(i = 0; i < us_proc_info.libs_count; i++)
994                                 us_proc_info.p_libs[i].loaded = 0;
995                 }
996         }
997
998         return iRet;
999 }
1000 static int install_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
1001 {
1002         kernel_probe_t *probe = NULL;
1003         int iRet = 0;
1004
1005         DPRINTF("us_proc_probes = 0x%x, uflag = 0x%x, "
1006                         "probes_flags = 0x%x, kflag = 0x%x",
1007                         us_proc_probes, uflag, probes_flags, kflag);
1008
1009         if (!(probes_flags & kflag)) {
1010                 iRet = add_probe_to_list (addr, &probe);
1011                 if (iRet) {
1012                         EPRINTF ("add_probe_to_list(0x%lx) result=%d!", addr, iRet);
1013                         return iRet;
1014                 }
1015                 probes_flags |= kflag;
1016         }
1017         if (!(us_proc_probes & uflag)) {
1018                 if (!(probes_flags & uflag)) {
1019                         iRet = register_kernel_probe (probe);
1020                         if (iRet) {
1021                                 EPRINTF ("register_kernel_probe(0x%lx) result=%d!", addr, iRet);
1022                                 return iRet;
1023                         }
1024                 }
1025                 us_proc_probes |= uflag;
1026         }
1027
1028         if (probe)
1029                 *pprobe = probe;
1030
1031         return 0;
1032 }
1033
1034 static void install_proc_probes(struct task_struct *task, struct proc_probes *proc_p, int atomic);
1035
1036 int inst_usr_space_proc (void)
1037 {
1038         int ret, i;
1039         struct task_struct *task = 0;
1040         inst_us_proc_t *task_inst_info = NULL;
1041
1042         if (!is_us_instrumentation()) {
1043                 return 0;
1044         }
1045
1046         DPRINTF("User space instr");
1047
1048 #ifdef SLP_APP
1049         launchpad_daemon_dentry = dentry_by_path("/usr/bin/launchpad_preloading_preinitializing_daemon");
1050         if (launchpad_daemon_dentry == NULL) {
1051                 return -EINVAL;
1052         }
1053
1054 #endif /* SLP_APP */
1055
1056 #ifdef ANDROID_APP
1057         app_process_dentry = dentry_by_path("/system/bin/app_process");
1058         if (app_process_dentry == NULL) {
1059                 return -EINVAL;
1060         }
1061
1062         android_app_vma_start = 0;
1063         android_app_vma_end = 0;
1064 #endif /* ANDROID_APP */
1065
1066 #ifdef __ANDROID
1067         if (is_java_inst_enabled()) {
1068                 libdvm_dentry = dentry_by_path("/system/lib/libdvm.so");
1069                 if (libdvm_dentry == NULL) {
1070                         return -EINVAL;
1071                 }
1072
1073                 memset(&us_proc_info.libdvm_entry_ip, 0, sizeof(us_proc_ip_t));
1074                 memset(&us_proc_info.libdvm_return_ip, 0, sizeof(us_proc_ip_t));
1075                 us_proc_info.libdvm_start = 0;
1076                 us_proc_info.libdvm_end = 0;
1077         }
1078 #endif /* __ANDROID */
1079
1080         for (i = 0; i < us_proc_info.libs_count; i++) {
1081                 us_proc_info.p_libs[i].loaded = 0;
1082         }
1083         /* check whether process is already running
1084          * 1) if process is running - look for the libraries in the process maps
1085          * 1.1) check if page for symbol does exist
1086          * 1.1.1) if page exists - instrument it
1087          * 1.1.2) if page does not exist - make sure that do_page_fault handler is installed
1088          * 2) if process is not running - make sure that do_page_fault handler is installed
1089          * */
1090
1091         if (is_libonly())
1092         {
1093                 clear_task_inst_info();
1094                 for_each_process (task) {
1095                         if (task->flags & PF_KTHREAD){
1096                                 DPRINTF("ignored kernel thread %d\n",
1097                                         task->pid);
1098                                 continue;
1099                         }
1100
1101                         task_inst_info = get_task_inst_node(task);
1102                         if (!task_inst_info) {
1103                                 task_inst_info =
1104                                         copy_task_inst_info(task,
1105                                                             &us_proc_info);
1106                                 put_task_inst_node(task, task_inst_info);
1107                         }
1108                         DPRINTF("trying process");
1109 #ifdef __ANDROID
1110                         if (is_java_inst_enabled()) {
1111                                 find_libdvm_for_task(task, task_inst_info);
1112                         }
1113 #endif /* __ANDROID */
1114                         install_proc_probes(task, task_inst_info->pp, 1);
1115                         //put_task_struct (task);
1116                         task_inst_info = NULL;
1117                 }
1118         }
1119         else
1120         {
1121                 ret = find_task_by_path (us_proc_info.path, &task, NULL);
1122                 if ( task  )
1123                 {
1124                         DPRINTF("task found. installing probes");
1125                         us_proc_info.tgid = task->pid;
1126 #ifdef __ANDROID
1127                         if (is_java_inst_enabled()) {
1128                                 find_libdvm_for_task(task, &us_proc_info);
1129                         }
1130 #endif /* __ANDROID */
1131                         install_proc_probes(task, us_proc_info.pp, 0);
1132                         put_task_struct (task);
1133                 }
1134         }
1135
1136         // enable 'do_page_fault' probe to detect when they will be loaded
1137         ret = install_kernel_probe (pf_addr, US_PROC_PF_INSTLD, 0, &pf_probe);
1138         if (ret != 0)
1139         {
1140                 EPRINTF ("install_kernel_probe(do_page_fault) result=%d!", ret);
1141                 return ret;
1142         }
1143         // enable 'do_exit' probe to detect for remove task_struct
1144         ret = install_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD, 0, &exit_probe);
1145         if (ret != 0)
1146         {
1147                 EPRINTF ("install_kernel_probe(do_exit) result=%d!", ret);
1148                 return ret;
1149         }
1150         /* enable 'copy_process' */
1151         ret = install_kernel_probe (cp_addr, US_PROC_CP_INSTLD, 0, &cp_probe);
1152         if (ret != 0)
1153         {
1154                 EPRINTF ("instpall_kernel_probe(copy_process) result=%d!", ret);
1155                 return ret;
1156         }
1157
1158         // enable 'mm_release' probe to detect when for remove user space probes
1159         ret = install_kernel_probe (mr_addr, US_PROC_MR_INSTLD, 0, &mr_probe);
1160         if (ret != 0)
1161         {
1162                 EPRINTF ("install_kernel_probe(mm_release) result=%d!", ret);
1163                 return ret;
1164         }
1165
1166         // enable 'do_munmap' probe to detect when for remove user space probes
1167         ret = install_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD, 0, &unmap_probe);
1168         if (ret != 0)
1169         {
1170                 EPRINTF ("install_kernel_probe(do_munmap) result=%d!", ret);
1171                 return ret;
1172         }
1173         return 0;
1174 }
1175
1176 #include "../../tools/gpmu/probes/entry_data.h"
1177
1178 extern storage_arg_t sa_dpf;
1179
1180 void do_page_fault_j_pre_code(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
1181 {
1182         struct task_struct *task = current->group_leader;
1183
1184         if (task->flags & PF_KTHREAD) {
1185                 DPRINTF("ignored kernel thread %d\n", task->pid);
1186                 return;
1187         }
1188
1189         if (is_us_instrumentation()) {
1190                 swap_put_entry_data((void *)addr, &sa_dpf);
1191         }
1192 }
1193 EXPORT_SYMBOL_GPL(do_page_fault_j_pre_code);
1194
1195
1196 unsigned long imi_sum_time = 0;
1197 unsigned long imi_sum_hit = 0;
1198 EXPORT_SYMBOL_GPL (imi_sum_time);
1199 EXPORT_SYMBOL_GPL (imi_sum_hit);
1200
1201 static void set_mapping_file(struct file_probes *file_p,
1202                 const struct proc_probes *proc_p,
1203                 const struct task_struct *task,
1204                 const struct vm_area_struct *vma)
1205 {
1206         int app_flag = (vma->vm_file->f_dentry == proc_p->dentry);
1207         char *p;
1208         // if we installed something, post library info for those IPs
1209         p = strrchr(file_p->path, '/');
1210         if(!p) {
1211                 p = file_p->path;
1212         } else {
1213                 p++;
1214         }
1215
1216         file_p->vm_start = vma->vm_start;
1217         file_p->vm_end = vma->vm_end;
1218         pack_event_info(DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
1219                         task->tgid, p, vma->vm_start,
1220                         vma->vm_end - vma->vm_start, app_flag);
1221 }
1222
1223 static int register_us_page_probe(struct page_probes *page_p,
1224                 const struct file_probes *file_p,
1225                 const struct task_struct *task)
1226 {
1227         int err = 0;
1228         us_proc_ip_t *ip;
1229
1230         spin_lock(&page_p->lock);
1231
1232         if (page_p_is_install(page_p)) {
1233                 printk("page %x in %s task[tgid=%u, pid=%u] already installed\n",
1234                                 page_p->offset, file_p->dentry->d_iname, task->tgid, task->pid);
1235                 return 0;
1236         }
1237
1238         page_p_assert_install(page_p);
1239         page_p_set_all_kp_addr(page_p, file_p);
1240
1241         list_for_each_entry(ip, &page_p->ip_list, list) {
1242                 err = register_usprobe_my(task, ip);
1243                 if (err != 0) {
1244                         //TODO: ERROR
1245                         return err;
1246                 }
1247         }
1248
1249         page_p_installed(page_p);
1250
1251         spin_unlock(&page_p->lock);
1252
1253         return 0;
1254 }
1255
1256 static int unregister_us_page_probe(const struct task_struct *task,
1257                 struct page_probes *page_p, enum US_FLAGS flag)
1258 {
1259         int err = 0;
1260         us_proc_ip_t *ip;
1261
1262         if (page_p->install == 0) {
1263                 return 0;
1264         }
1265
1266         list_for_each_entry(ip, &page_p->ip_list, list) {
1267                 err = unregister_usprobe_my(task, ip, flag);
1268                 if (err != 0) {
1269                         //TODO: ERROR
1270                         return err;
1271                 }
1272         }
1273
1274         if (flag != US_DISARM) {
1275                 page_p_uninstalled(page_p);
1276         }
1277
1278         return err;
1279 }
1280
1281 static int check_vma(struct vm_area_struct *vma)
1282 {
1283 #ifndef __ANDROID
1284         return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || (vma->vm_flags & VM_ACCOUNT) ||
1285                         !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
1286                         !(vma->vm_flags & (VM_READ | VM_MAYREAD)));
1287 #else // __ANDROID
1288         return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC));
1289 #endif // __ANDROID
1290 }
1291
1292
1293 static void install_page_probes(unsigned long page, struct task_struct *task, struct proc_probes *proc_p, int atomic)
1294 {
1295         int lock;
1296         struct mm_struct *mm;
1297         struct vm_area_struct *vma;
1298
1299         mm_read_lock(task, mm, atomic, lock);
1300
1301         vma = find_vma(mm, page);
1302         if (vma && check_vma(vma)) {
1303                 struct file_probes *file_p = proc_p_find_file_p(proc_p, vma);
1304                 if (file_p) {
1305                         struct page_probes *page_p;
1306                         if (!file_p->loaded) {
1307                                 set_mapping_file(file_p, proc_p, task, vma);
1308                                 file_p->loaded = 1;
1309                         }
1310
1311                         page_p = file_p_find_page_p(file_p, page);
1312                         if (page_p) {
1313                                 register_us_page_probe(page_p, file_p, task);
1314                         }
1315                 }
1316         }
1317
1318         mm_read_unlock(mm, atomic, lock);
1319 }
1320
1321 static void install_file_probes(struct task_struct *task, struct mm_struct *mm, struct file_probes *file_p)
1322 {
1323         struct page_probes *page_p = NULL;
1324         struct hlist_node *node = NULL;
1325         struct hlist_head *head = NULL;
1326         int i, table_size = (1 << file_p->page_probes_hash_bits);
1327
1328         for (i = 0; i < table_size; ++i) {
1329                 head = &file_p->page_probes_table[i];
1330                 hlist_for_each_entry_rcu(page_p, node, head, hlist) {
1331                         if (page_present(mm, page_p->offset)) {
1332                                 register_us_page_probe(page_p, file_p, task);
1333                         }
1334                 }
1335         }
1336 }
1337
1338 static void install_proc_probes(struct task_struct *task, struct proc_probes *proc_p, int atomic)
1339 {
1340         int lock;
1341         struct vm_area_struct *vma;
1342         struct mm_struct *mm;
1343
1344         mm_read_lock(task, mm, atomic, lock);
1345
1346         for (vma = mm->mmap; vma; vma = vma->vm_next) {
1347                 if (check_vma(vma)) {
1348                         struct file_probes *file_p = proc_p_find_file_p(proc_p, vma);
1349                         if (file_p) {
1350                                 if (!file_p->loaded) {
1351                                         set_mapping_file(file_p, proc_p, task, vma);
1352                                         file_p->loaded = 1;
1353                                 }
1354
1355                                 install_file_probes(task, mm, file_p);
1356                         }
1357                 }
1358         }
1359
1360         mm_read_unlock(mm, atomic, lock);
1361 }
1362
1363 static int unregister_us_file_probes(struct task_struct *task, struct file_probes *file_p, enum US_FLAGS flag)
1364 {
1365         int i, err = 0;
1366         int table_size = (1 << file_p->page_probes_hash_bits);
1367         struct page_probes *page_p;
1368         struct hlist_node *node, *tmp;
1369         struct hlist_head *head;
1370
1371         for (i = 0; i < table_size; ++i) {
1372                 head = &file_p->page_probes_table[i];
1373                 hlist_for_each_entry_safe (page_p, node, tmp, head, hlist) {
1374                         err = unregister_us_page_probe(task, page_p, flag);
1375                         if (err != 0) {
1376                                 // TODO: ERROR
1377                                 return err;
1378                         }
1379                 }
1380         }
1381
1382         if (flag != US_DISARM) {
1383                 file_p->loaded = 0;
1384         }
1385
1386         return err;
1387 }
1388
1389 static int uninstall_us_proc_probes(struct task_struct *task, struct proc_probes *proc_p, enum US_FLAGS flag)
1390 {
1391         int i, err = 0;
1392
1393         for (i = 0; i < proc_p->cnt; ++i) {
1394                 err = unregister_us_file_probes(task, proc_p->file_p[i], flag);
1395                 if (err != 0) {
1396                         // TODO:
1397                         return err;
1398                 }
1399         }
1400
1401         return err;
1402 }
1403
1404 static pid_t find_proc_by_task(const struct task_struct *task, const struct dentry *dentry)
1405 {
1406         struct vm_area_struct *vma;
1407         struct mm_struct *mm = task->active_mm;
1408         if (mm == NULL) {
1409                 return 0;
1410         }
1411
1412         for (vma = mm->mmap; vma; vma = vma->vm_next) {
1413                 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
1414                         if (vma->vm_file->f_dentry == dentry) {
1415                                 return task->tgid;
1416                         }
1417 #ifdef SLP_APP
1418                         if (is_slp_app_with_dentry(vma, dentry)) {
1419                                 return task->tgid;
1420                         }
1421 #endif /* SLP_APP */
1422 #ifdef ANDROID_APP
1423                         if (is_android_app_with_dentry(vma, dentry)) {
1424                                 return task->tgid;
1425                         }
1426 #endif /* ANDROID_APP */
1427                 }
1428         }
1429
1430         return 0;
1431 }
1432
1433 void do_page_fault_ret_pre_code (void)
1434 {
1435         struct mm_struct *mm;
1436         struct vm_area_struct *vma = 0;
1437         inst_us_proc_t *task_inst_info = NULL;
1438         /*
1439          * Because process threads have same address space
1440          * we instrument only group_leader of all this threads
1441          */
1442         struct task_struct *task = current->group_leader;
1443         unsigned long addr = 0;
1444
1445         // overhead
1446         struct timeval imi_tv1;
1447         struct timeval imi_tv2;
1448 #define USEC_IN_SEC_NUM                         1000000
1449
1450         if (task->flags & PF_KTHREAD) {
1451                 DPRINTF("ignored kernel thread %d\n", task->pid);
1452                 return;
1453         }
1454
1455         if (!is_us_instrumentation()) {
1456                 return;
1457         }
1458
1459         addr = (unsigned long)swap_get_entry_data(&sa_dpf);
1460
1461         if (addr == 0) {
1462                 printk("WARNING: do_page_fault_ret_pre_code addr = 0\n");
1463                 return;
1464         }
1465
1466         if (is_libonly()) {
1467                 task_inst_info = get_task_inst_node(task);
1468                 if (task_inst_info == NULL) {
1469                         task_inst_info = copy_task_inst_info(task, &us_proc_info);
1470                         put_task_inst_node(task, task_inst_info);
1471                 }
1472         } else {
1473                 if (!is_java_inst_enabled() &&
1474                         (us_proc_info.unres_ips_count +
1475                          us_proc_info.unres_vtps_count +
1476                          us_proc_info.unres_otg_ips_count) == 0) {
1477                         return;
1478                 }
1479
1480                 // find task
1481                 if (us_proc_info.tgid == 0) {
1482                         pid_t tgid = find_proc_by_task(task, us_proc_info.m_f_dentry);
1483                         if (tgid) {
1484                                 us_proc_info.tgid = gl_nNotifyTgid = tgid;
1485                         }
1486                 }
1487
1488                 if (us_proc_info.tgid == task->tgid) {
1489                         task_inst_info = &us_proc_info;
1490                 }
1491         }
1492
1493         if (task_inst_info) {
1494                 unsigned long page = addr & PAGE_MASK;
1495
1496 #ifdef __ANDROID
1497                 if (is_java_inst_enabled()) {
1498                         find_libdvm_for_task(task, &us_proc_info);
1499                 }
1500 #endif /* __ANDROID */
1501
1502                 // overhead
1503                 do_gettimeofday(&imi_tv1);
1504                 install_page_probes(page, task, task_inst_info->pp, 1);
1505                 do_gettimeofday(&imi_tv2);
1506                 imi_sum_hit++;
1507                 imi_sum_time += ((imi_tv2.tv_sec - imi_tv1.tv_sec) *  USEC_IN_SEC_NUM +
1508                                 (imi_tv2.tv_usec - imi_tv1.tv_usec));
1509         }
1510 }
1511
1512 EXPORT_SYMBOL_GPL(do_page_fault_ret_pre_code);
1513
1514
1515 void do_exit_probe_pre_code (void)
1516 {
1517         // TODO: remove task
1518 }
1519 EXPORT_SYMBOL_GPL(do_exit_probe_pre_code);
1520
1521 static int check_addr(unsigned long addr, unsigned long start, size_t len)
1522 {
1523         if ((addr >= start) && (addr < start + (unsigned long)len)) {
1524                 return 1;
1525         }
1526
1527         return 0;
1528 }
1529
1530 static int remove_unmap_probes(struct task_struct *task, inst_us_proc_t* task_inst_info, unsigned long start, size_t len)
1531 {
1532         int i, k, err;
1533         us_proc_otg_ip_t *p;
1534         unsigned long addr;
1535         const int atomic = 1;
1536
1537         for (i = 0; i < task_inst_info->libs_count; ++i) {
1538                 for (k = 0; k < task_inst_info->p_libs[i].ips_count; ++k) {
1539                         if (task_inst_info->p_libs[i].p_ips[k].installed) {
1540                                 addr = task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr;
1541                                 if (check_addr(addr, start, len)) {
1542                                         err = unregister_usprobe(task, &task_inst_info->p_libs[i].p_ips[k], atomic, 1);
1543                                         if (err != 0) {
1544                                                 EPRINTF("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
1545                                                 continue;
1546                                         }
1547                                         task_inst_info->unres_ips_count++;
1548                                 }
1549                         }
1550                 }
1551                 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; ++k) {
1552                         if (task_inst_info->p_libs[i].p_vtps[k].installed) {
1553                                 addr = task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr;
1554                                 if (check_addr(addr, start, len)) {
1555                                         dbi_unregister_ujprobe(task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
1556                                         task_inst_info->unres_vtps_count++;
1557                                         task_inst_info->p_libs[i].p_vtps[k].installed = 0;
1558                                 }
1559                         }
1560                 }
1561         }
1562 #ifdef __ANDROID
1563         if (is_java_inst_enabled()) {
1564                 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
1565                 if (entp->installed) {
1566                         addr = entp->jprobe.kp.addr;
1567                         if (check_addr(addr, start, len)) {
1568                                 unregister_usprobe(task, entp, atomic, 1);
1569                                 entp->installed = 0;
1570                         }
1571                 }
1572                 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
1573                 if (retp->installed) {
1574                         addr = retp->jprobe.kp.addr;
1575                         if (check_addr(addr, start, len)) {
1576                                 unregister_usprobe(task, retp, atomic, 1);
1577                                 retp->installed = 0;
1578                         }
1579                 }
1580         }
1581 #endif /* __ANDROID */
1582
1583         // remove OTG-probes
1584         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
1585                 if (!p->ip.installed) {
1586                         continue;
1587                 }
1588
1589                 addr = p->ip.jprobe.kp.addr;
1590                 if (check_addr(addr, start, len) == 0) {
1591                         continue;
1592                 }
1593
1594                 err = unregister_usprobe(task, &p->ip, atomic, 1);
1595                 if (err != 0) {
1596                         EPRINTF("failed to uninstall IP at %p. Error %d!",
1597                                  p->ip.jprobe.kp.addr, err);
1598                         continue;
1599                 }
1600
1601                 remove_otg_probe_from_list(p->ip.offset);
1602         }
1603
1604         return 0;
1605 }
1606
1607 void do_munmap_probe_pre_code(struct mm_struct *mm, unsigned long start, size_t len)
1608 {
1609         inst_us_proc_t *task_inst_info = NULL;
1610         struct task_struct *task = current;
1611
1612         //if user-space instrumentation is not set
1613         if (!us_proc_info.path || task->tgid != task->pid)
1614                 return;
1615
1616         if (!strcmp(us_proc_info.path,"*")) {
1617                 task_inst_info = get_task_inst_node(task);
1618         } else {
1619                 if (task->tgid == us_proc_info.tgid) {
1620                         task_inst_info = &us_proc_info;
1621                 }
1622         }
1623
1624         if (task_inst_info) {
1625                 remove_unmap_probes(task, task_inst_info, start, len);
1626         }
1627 }
1628 EXPORT_SYMBOL_GPL(do_munmap_probe_pre_code);
1629
1630 void mm_release_probe_pre_code(void)
1631 {
1632         int iRet;
1633         struct task_struct *task;
1634
1635         if (!is_us_instrumentation() || current->tgid != current->pid) {
1636                 return;
1637         }
1638
1639         if (is_libonly()) {
1640                 inst_us_proc_t *task_inst_info = get_task_inst_node(current);
1641                 if (task_inst_info) {
1642                         iRet = uninstall_us_proc_probes(current, task_inst_info->pp, US_NOT_RP2);
1643                         if (iRet != 0) {
1644                                 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1645                         }
1646
1647                         dbi_unregister_all_uprobes(current, 1);
1648                 }
1649         } else {
1650                 if (current->tgid == us_proc_info.tgid && current->tgid == current->pid) {
1651                         int i;
1652                         iRet = uninstall_us_proc_probes(current, us_proc_info.pp, US_NOT_RP2);
1653                         if (iRet != 0) {
1654                                 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1655                         }
1656
1657                         dbi_unregister_all_uprobes(current, 1);
1658                         us_proc_info.tgid = 0;
1659                         for(i = 0; i < us_proc_info.libs_count; i++) {
1660                                 us_proc_info.p_libs[i].loaded = 0;
1661                         }
1662                 }
1663         }
1664 }
1665 EXPORT_SYMBOL_GPL(mm_release_probe_pre_code);
1666
1667
1668 static void recover_child(struct task_struct *child_task, inst_us_proc_t *parent_iup)
1669 {
1670         uninstall_us_proc_probes(child_task, parent_iup->pp, US_DISARM);
1671 }
1672
1673 static void rm_uprobes_child(struct task_struct *new_task)
1674 {
1675         if (is_libonly()) {
1676                 inst_us_proc_t *task_inst_info = get_task_inst_node(current);
1677                 if(task_inst_info)
1678                         recover_child(new_task, task_inst_info);
1679         } else {
1680                 if(us_proc_info.tgid == current->tgid) {
1681                         recover_child(new_task, &us_proc_info);
1682                 }
1683         }
1684 }
1685
1686 void copy_process_ret_pre_code(struct task_struct *p)
1687 {
1688         if(!p || IS_ERR(p))
1689                 return;
1690
1691         if(p->mm != current->mm)    // check flags CLONE_VM
1692                 rm_uprobes_child(p);
1693 }
1694
1695
1696 DEFINE_PER_CPU (us_proc_ip_t *, gpCurIp) = NULL;
1697 EXPORT_PER_CPU_SYMBOL_GPL(gpCurIp);
1698 DEFINE_PER_CPU(struct pt_regs *, gpUserRegs) = NULL;
1699 EXPORT_PER_CPU_SYMBOL_GPL(gpUserRegs);
1700
1701
1702 unsigned long ujprobe_event_pre_handler (us_proc_ip_t * ip, struct pt_regs *regs)
1703 {
1704         __get_cpu_var (gpCurIp) = ip;
1705         __get_cpu_var (gpUserRegs) = regs;
1706         return 0;
1707 }
1708
1709 #ifdef __ANDROID
1710 int handle_java_event(unsigned long addr)
1711 {
1712         unsigned long start = 0;
1713         struct pt_regs *regs = __get_cpu_var(gpUserRegs);
1714
1715         if (is_libonly()) {
1716                 /* TODO: some stuff here */
1717         } else {
1718                 start = us_proc_info.libdvm_start;
1719         }
1720         unsigned long end = us_proc_info.libdvm_end;
1721
1722         if (addr == start + LIBDVM_ENTRY) {
1723                 unsigned long *p_met = (unsigned long *)regs->ARM_r0;
1724                 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1725                 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1726                 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1727                 if (!cl_name || !met_name) {
1728                         EPRINTF("warn: class name or method name null\n");
1729                 } else {
1730                         pack_event_info(JAVA_PROBE_ID, RECORD_ENTRY, "pss", addr, cl_name, met_name);
1731                 }
1732                 dbi_uprobe_return ();
1733                 return 1;
1734         }
1735
1736         if (addr == start + LIBDVM_RETURN) {
1737                 unsigned long *p_th = (unsigned long *)regs->ARM_r6;
1738                 unsigned long *p_st = p_th;
1739                 unsigned long *p_met = p_st ? (unsigned long *)p_st[2] : 0;
1740                 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1741                 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1742                 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1743                 if (!cl_name || !met_name) {
1744                         EPRINTF("warn: class name or method name null\n");
1745                 } else {
1746                         pack_event_info(JAVA_PROBE_ID, RECORD_RET, "pss", addr, cl_name, met_name);
1747                 }
1748                 dbi_uprobe_return ();
1749                 return 1;
1750         }
1751
1752         return 0;
1753 }
1754 #endif /* __ANDROID */
1755
1756 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
1757 {
1758         us_proc_ip_t *ip = __get_cpu_var (gpCurIp);
1759         unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1760
1761 #ifdef __ANDROID
1762         if (is_java_inst_enabled() && handle_java_event(addr)) {
1763                 return;
1764         }
1765 #endif /* __ANDROID */
1766
1767
1768 #if defined(CONFIG_ARM)
1769         if (ip->offset & 0x01)
1770         {
1771                 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr | 0x01, arg1, arg2, arg3, arg4, arg5, arg6);
1772         }else{
1773                 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1774         }
1775 #else
1776         pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1777 #endif
1778         // Mr_Nobody: uncomment for valencia
1779         //unregister_usprobe(current, ip, 1);
1780         dbi_uprobe_return ();
1781 }
1782
1783 void find_plt_address(unsigned long addr)
1784 {
1785         inst_us_proc_t *task_inst_info = NULL;
1786         int i;
1787         unsigned real_addr;
1788         struct vm_area_struct *vma;
1789         us_proc_lib_t *p_lib = NULL;
1790         char *szLibPath = NULL;
1791
1792         // Search for library structure to check whether this function plt or not
1793         if (strcmp(us_proc_info.path, "*")){
1794         // If app lib instrumentation
1795                 task_inst_info = &us_proc_info;
1796         } else {
1797         // If lib only instrumentation
1798                 task_inst_info = get_task_inst_node(current);
1799         }
1800         if ((task_inst_info != NULL) && (task_inst_info->is_plt != 0)) {
1801                 for (i = 0; i < task_inst_info->libs_count; i++)
1802                 {
1803                         if ((task_inst_info->p_libs[i].loaded)
1804                                 && (task_inst_info->p_libs[i].plt_count > 0)
1805                                 && (addr > task_inst_info->p_libs[i].vma_start)
1806                                 && (addr < task_inst_info->p_libs[i].vma_end))
1807                         {
1808                                 p_lib = &(task_inst_info->p_libs[i]);
1809                                 break;
1810                         }
1811                 }
1812                 if (p_lib != NULL) {
1813                         for (i = 0; i < p_lib->plt_count; i++)
1814                         {
1815                                 if (addr == p_lib->p_plt[i].func_addr + p_lib->vma_start) {
1816                                         unsigned long real_got;
1817                                         if (p_lib->vma_flag & VM_EXECUTABLE) {
1818                                                 real_got = p_lib->p_plt[i].got_addr;
1819                                         } else {
1820                                                 real_got = p_lib->p_plt[i].got_addr + p_lib->vma_start;
1821                                         }
1822                                         if (!read_proc_vm_atomic(current, (unsigned long)(real_got), &real_addr, sizeof(unsigned long))) {
1823                                                 printk("Failed to read got %p at memory address %p!\n", p_lib->p_plt[i].got_addr, real_got);
1824                                                 return;
1825                                         }
1826                                         if (real_addr != p_lib->p_plt[i].real_func_addr) {
1827                                                 p_lib->p_plt[i].real_func_addr =  real_addr;
1828                                                 vma = find_vma(current->mm, real_addr);
1829                                                 if ((vma != NULL) && (vma->vm_start <= real_addr) && (vma->vm_end > real_addr)) {
1830                                                         if (vma->vm_file != NULL) {
1831                                                                 szLibPath = &(vma->vm_file->f_dentry->d_iname);
1832                                                         }
1833                                                 } else {
1834                                                         printk("Failed to get vma, includes %x address\n", real_addr);
1835                                                         return;
1836                                                 }
1837                                                 if (szLibPath) {
1838                                                         pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppsp", addr, real_addr, szLibPath, real_addr - vma->vm_start);
1839                                                         return;
1840                                                 } else {
1841                                                         pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppp", addr, real_addr, real_addr - vma->vm_start);
1842                                                         return;
1843                                                 }
1844                                         } else {
1845                                                 return;
1846                                         }
1847                                 }
1848                         }
1849                 }
1850         }
1851 }
1852
1853 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, us_proc_ip_t * ip)
1854 {
1855         int retval = regs_return_value(regs);
1856         unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1857
1858         find_plt_address(addr);
1859
1860 #if defined(CONFIG_ARM)
1861         if (ip->offset & 0x01)
1862         {
1863                 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr | 0x01, retval);
1864         }else{
1865                 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1866         }
1867 #else
1868         pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1869 #endif
1870         // Mr_Nobody: uncomment for valencia
1871         //unregister_usprobe(current, ip, 1);
1872         return 0;
1873 }
1874
1875 static int register_usprobe(struct task_struct *task, us_proc_ip_t *ip, int atomic)
1876 {
1877         int ret = 0;
1878         ip->jprobe.kp.tgid = task->tgid;
1879
1880         if (ip->jprobe.entry == NULL) {
1881                 ip->jprobe.entry = (kprobe_opcode_t *)ujprobe_event_handler;
1882                 DPRINTF("Set default event handler for %x\n", ip->offset);
1883         }
1884
1885         if (ip->jprobe.pre_entry == NULL) {
1886                 ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t)ujprobe_event_pre_handler;
1887                 DPRINTF("Set default pre handler for %x\n", ip->offset);
1888         }
1889
1890         ip->jprobe.priv_arg = ip;
1891         ret = dbi_register_ujprobe(task, &ip->jprobe, atomic);
1892         if (ret) {
1893                 DPRINTF ("dbi_register_ujprobe() failure %d", ret);
1894                 return ret;
1895         }
1896
1897         // Mr_Nobody: comment for valencia
1898         ip->retprobe.kp.tgid = task->tgid;
1899         if (ip->retprobe.handler == NULL) {
1900                 ip->retprobe.handler = (kretprobe_handler_t)uretprobe_event_handler;
1901                 DPRINTF("Set default ret event handler for %x\n", ip->offset);
1902         }
1903
1904         ip->retprobe.priv_arg = ip;
1905         ret = dbi_register_uretprobe(task, &ip->retprobe, atomic);
1906         if (ret) {
1907                 EPRINTF ("dbi_register_uretprobe() failure %d", ret);
1908                 return ret;
1909         }
1910
1911         ip->installed = 1;
1912
1913         return 0;
1914 }
1915
1916 static int unregister_usprobe(struct task_struct *task, us_proc_ip_t * ip, int atomic, int not_rp2)
1917 {
1918         dbi_unregister_ujprobe(task, &ip->jprobe, atomic);
1919         dbi_unregister_uretprobe(task, &ip->retprobe, atomic, not_rp2);
1920
1921         ip->installed = 0;
1922
1923         return 0;
1924 }
1925
1926 unsigned long get_stack_size(struct task_struct *task,
1927                 struct pt_regs *regs)
1928 {
1929 #ifdef CONFIG_ADD_THREAD_STACK_INFO
1930         return (task->stack_start - dbi_get_stack_ptr(regs));
1931 #else
1932         struct vm_area_struct *vma = NULL;
1933         struct mm_struct *mm = NULL;
1934         unsigned long result = 0;
1935     int atomic = in_atomic();
1936
1937         mm = (atomic ? task->active_mm: get_task_mm(task));
1938
1939         if (mm) {
1940                 if (!atomic)
1941                         down_read(&mm->mmap_sem);
1942
1943                 vma = find_vma(mm, dbi_get_stack_ptr(regs));
1944
1945                 if (vma)
1946                         result = vma->vm_end - dbi_get_stack_ptr(regs);
1947                 else
1948                         result = 0;
1949
1950                 if (!atomic) {
1951                         up_read(&mm->mmap_sem);
1952                         mmput(mm);
1953                 }
1954         }
1955
1956         return result;
1957 #endif
1958 }
1959 EXPORT_SYMBOL_GPL(get_stack_size);
1960
1961 unsigned long get_stack(struct task_struct *task, struct pt_regs *regs,
1962                 char *buf, unsigned long sz)
1963 {
1964         unsigned long stack_sz = get_stack_size(task, regs);
1965         unsigned long real_sz = (stack_sz > sz ? sz: stack_sz);
1966         int res = read_proc_vm_atomic(task, dbi_get_stack_ptr(regs), buf, real_sz);
1967         return res;
1968 }
1969 EXPORT_SYMBOL_GPL(get_stack);
1970
1971 int dump_to_trace(probe_id_t probe_id, void *addr, const char *buf,
1972                 unsigned long sz)
1973 {
1974         unsigned long rest_sz = sz;
1975         const char *data = buf;
1976
1977         while (rest_sz >= EVENT_MAX_SIZE) {
1978                 pack_event_info(probe_id, RECORD_ENTRY, "pa",
1979                                 addr, EVENT_MAX_SIZE, data);
1980                 rest_sz -= EVENT_MAX_SIZE;
1981                 data += EVENT_MAX_SIZE;
1982         }
1983
1984         if (rest_sz > 0)
1985                 pack_event_info(probe_id, RECORD_ENTRY, "pa", addr, rest_sz, data);
1986
1987         return 0;
1988 }
1989 EXPORT_SYMBOL_GPL(dump_to_trace);
1990
1991 int dump_backtrace(probe_id_t probe_id, struct task_struct *task,
1992                 void *addr, struct pt_regs *regs, unsigned long sz)
1993 {
1994         unsigned long real_sz = 0;
1995         char *buf = NULL;
1996
1997         buf = (char *)kmalloc(sz, GFP_ATOMIC);
1998
1999         if (buf != NULL) {
2000                 real_sz = get_stack(task, regs, buf, sz);
2001                 if (real_sz > 0)
2002                         dump_to_trace(probe_id, addr, buf, real_sz);
2003                 kfree(buf);
2004                 return 0;
2005         } else {
2006                 return -1;
2007         }
2008 }
2009 EXPORT_SYMBOL_GPL(dump_backtrace);
2010
2011 unsigned long get_ret_addr(struct task_struct *task, us_proc_ip_t *ip)
2012 {
2013         unsigned long retaddr = 0;
2014         struct hlist_node *item, *tmp_node;
2015         struct kretprobe_instance *ri;
2016
2017         if (ip) {
2018                 hlist_for_each_safe (item, tmp_node, &ip->retprobe.used_instances) {
2019                         ri = hlist_entry (item, struct kretprobe_instance, uflist);
2020
2021                         if (ri->task && ri->task->pid == task->pid &&
2022                                         ri->task->tgid == task->tgid)
2023                                 retaddr = (unsigned long)ri->ret_addr;
2024                 }
2025         }
2026
2027         if (retaddr)
2028                 return retaddr;
2029         else
2030                 return dbi_get_ret_addr(task_pt_regs(task));
2031 }
2032 EXPORT_SYMBOL_GPL(get_ret_addr);
2033