1 ////////////////////////////////////////////////////////////////////////////////////
3 // FILE: us_proc_inst.c
6 // This file is C source for SWAP driver.
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
14 // REVISION DATE: 2008.12.02
16 ////////////////////////////////////////////////////////////////////////////////////
19 #include "us_proc_inst.h"
21 #include "../kprobe/dbi_kprobes_deps.h"
22 #include "../uprobe/swap_uprobes.h"
24 #include "sspt/sspt.h"
26 #include <dbi_insn_slots.h>
28 #define mm_read_lock(task, mm, atomic, lock) \
29 mm = atomic ? task->active_mm : get_task_mm(task); \
32 panic("ERRR mm_read_lock: mm == NULL\n"); \
36 lock = down_read_trylock(&mm->mmap_sem); \
39 down_read(&mm->mmap_sem); \
42 #define mm_read_unlock(mm, atomic, lock) \
44 up_read(&mm->mmap_sem); \
51 unsigned long ujprobe_event_pre_handler (struct us_ip *ip, struct pt_regs *regs);
52 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6);
53 int uretprobe_event_handler(struct uretprobe_instance *probe, struct pt_regs *regs, struct us_ip *ip);
56 LIST_HEAD(proc_probes_list);
58 #define print_event(fmt, args...) \
61 sprintf(buf, fmt, ##args); \
62 pack_event_info(US_PROBE_ID, RECORD_ENTRY, "ds", 0x0badc0de, buf); \
65 static inline int is_libonly(void)
67 return !strcmp(us_proc_info.path,"*");
70 // is user-space instrumentation
71 static inline int is_us_instrumentation(void)
73 return !!us_proc_info.path;
76 static unsigned long alloc_user_pages(struct task_struct *task, unsigned long len, unsigned long prot, unsigned long flags)
78 unsigned long ret = 0;
79 struct task_struct *otask = current;
81 int atomic = in_atomic();
83 mm = atomic ? task->active_mm : get_task_mm (task);
86 if (!down_write_trylock(&mm->mmap_sem)) {
89 up_read(&mm->mmap_sem);
90 down_write(&mm->mmap_sem);
95 // FIXME: its seems to be bad decision to replace 'current' pointer temporarily
96 current_thread_info()->task = task;
97 ret = do_mmap_pgoff(NULL, 0, len, prot, flags, 0);
98 current_thread_info()->task = otask;
100 downgrade_write (&mm->mmap_sem);
104 printk("proc %d has no mm", task->tgid);
110 static void *sm_alloc_us(struct slot_manager *sm)
112 struct task_struct *task = sm->data;
114 return (void *)alloc_user_pages(task, PAGE_SIZE,
115 PROT_EXEC|PROT_READ|PROT_WRITE,
116 MAP_ANONYMOUS|MAP_PRIVATE);
119 static void sm_free_us(struct slot_manager *sm, void *ptr)
121 struct task_struct *task = sm->data;
124 * E. G.: This code provides kernel dump because of rescheduling while atomic.
125 * As workaround, this code was commented. In this case we will have memory leaks
126 * for instrumented process, but instrumentation process should functionate correctly.
127 * Planned that good solution for this problem will be done during redesigning KProbe
128 * for improving supportability and performance.
131 mm = get_task_mm(task);
133 down_write(&mm->mmap_sem);
134 do_munmap(mm, (unsigned long)(ptr), PAGE_SIZE);
135 up_write(&mm->mmap_sem);
139 /* FIXME: implement the removal of memory for task */
142 struct slot_manager *create_sm_us(struct task_struct *task)
144 struct slot_manager *sm = kmalloc(sizeof(*sm), GFP_ATOMIC);
145 sm->slot_size = UPROBES_TRAMP_LEN;
146 sm->alloc = sm_alloc_us;
147 sm->free = sm_free_us;
148 INIT_HLIST_NODE(&sm->page_list);
152 void free_sm_us(struct slot_manager *sm)
161 static int entry_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs);
162 static int ret_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs);
164 static struct kretprobe pf_kretprobe = {
165 .entry_handler = entry_handler_pf,
166 .handler = ret_handler_pf,
167 .data_size = sizeof(struct pf_data)
170 static void copy_process_ret_pre_code(struct task_struct *p);
172 static int ret_handler_cp(struct kretprobe_instance *ri, struct pt_regs *regs)
174 struct task_struct* task = (struct task_struct *)regs_return_value(regs);
176 copy_process_ret_pre_code(task);
181 static struct kretprobe cp_kretprobe = {
182 .handler = ret_handler_cp,
185 static void jmm_release(struct task_struct *tsk, struct mm_struct *mm);
187 static struct jprobe mr_jprobe = {
191 static int jdo_munmap(struct mm_struct *mm, unsigned long start, size_t len);
193 static struct jprobe unmap_jprobe = {
197 static struct sspt_procs *get_proc_probes_by_task(struct task_struct *task)
199 struct sspt_procs *procs, *tmp;
202 if (task != current) {
203 printk("ERROR get_proc_probes_by_task: \'task != current\'\n");
207 return us_proc_info.pp;
210 list_for_each_entry_safe(procs, tmp, &proc_probes_list, list) {
211 if (procs->tgid == task->tgid) {
219 static void add_proc_probes(struct task_struct *task, struct sspt_procs *procs)
221 list_add_tail(&procs->list, &proc_probes_list);
224 static struct sspt_procs *get_proc_probes_by_task_or_new(struct task_struct *task)
226 struct sspt_procs *procs = get_proc_probes_by_task(task);
228 procs = sspt_procs_copy(us_proc_info.pp, task);
229 procs->sm = create_sm_us(task);
230 add_proc_probes(task, procs);
236 struct dentry *dentry_by_path(const char *path)
238 struct dentry *dentry;
239 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
241 if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
242 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
244 if (path_lookup(path, LOOKUP_FOLLOW, &nd) != 0) {
245 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
246 EPRINTF("failed to lookup dentry for path %s!", path);
250 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
253 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
254 dentry = nd.path.dentry;
256 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
257 dentry = st_path.dentry;
259 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
263 static int check_vma(struct vm_area_struct *vma)
265 return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || (vma->vm_flags & VM_ACCOUNT) ||
266 !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
267 !(vma->vm_flags & (VM_READ | VM_MAYREAD)));
270 static int find_task_by_path (const char *path, struct task_struct **p_task, struct list_head *tids)
273 struct task_struct *task;
274 struct vm_area_struct *vma;
275 struct mm_struct *mm;
276 struct dentry *dentry = dentry_by_path(path);
280 /* find corresponding dir entry, this is also check for valid path */
281 // TODO: test - try to instrument process with non-existing path
282 // TODO: test - try to instrument process with existing path and delete file just after start
283 if (dentry == NULL) {
288 for_each_process (task) {
290 if ( 0 != inst_pid && ( inst_pid != task->pid ) )
293 mm = get_task_mm(task);
298 if (check_vma(vma)) {
299 if (vma->vm_file->f_dentry == dentry) {
302 get_task_struct (task);
309 // only decrement usage count on mm since we cannot sleep here
310 atomic_dec(&mm->mm_users);
317 DPRINTF ("found pid %d for %s.", (*p_task)->pid, path);
318 *p_task = (*p_task)->group_leader;
319 gl_nNotifyTgid = (*p_task)->tgid;
321 DPRINTF ("pid for %s not found!", path);
327 static void set_mapping_file(struct sspt_file *file,
328 const struct sspt_procs *procs,
329 const struct task_struct *task,
330 const struct vm_area_struct *vma);
332 int install_otg_ip(unsigned long addr,
333 kprobe_pre_entry_handler_t pre_handler,
334 unsigned long jp_handler,
335 uretprobe_handler_t rp_handler)
338 struct task_struct *task = current->group_leader;
339 struct mm_struct *mm = task->mm;
342 struct vm_area_struct *vma = find_vma(mm, addr);
343 if (vma && (vma->vm_flags & VM_EXEC) &&
344 vma->vm_file && vma->vm_file->f_dentry) {
345 unsigned long offset_addr = addr - vma->vm_start;
346 struct dentry *dentry = vma->vm_file->f_dentry;
347 char *name = dentry->d_iname;
348 struct sspt_procs *procs = get_proc_probes_by_task(task);
349 struct ip_data pd = {
350 .offset = offset_addr,
351 .pre_handler = pre_handler,
352 .jp_handler = jp_handler,
353 .rp_handler = rp_handler,
357 struct sspt_file *file = sspt_procs_find_file_or_new(procs, dentry, name);
358 struct sspt_page *page = sspt_get_page(file, offset_addr);
359 struct us_ip *ip = sspt_find_ip(page, offset_addr & ~PAGE_MASK);
362 set_mapping_file(file, procs, task, vma);
367 // TODO: sspt_procs_find_file_or_new --> sspt_procs_find_file ?!
368 struct sspt_file *file = sspt_procs_find_file_or_new(procs, dentry, name);
369 sspt_file_add_ip(file, &pd);
371 /* if addr mapping, that probe install, else it be installed in do_page_fault handler */
372 if (page_present(mm, addr)) {
373 ip = sspt_find_ip(page, offset_addr & ~PAGE_MASK);
374 sspt_set_ip_addr(ip, page, file);
377 ret = register_usprobe_my(task, ip);
379 sspt_page_installed(page);
381 printk("ERROR install_otg_ip: ret=%d\n", ret);
392 EXPORT_SYMBOL_GPL(install_otg_ip);
394 int init_helper(void)
397 addr = swap_ksyms("do_page_fault");
399 EPRINTF("Cannot find address for page fault function!");
402 pf_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
404 addr = swap_ksyms("copy_process");
406 EPRINTF("Cannot find address for copy_process function!");
409 cp_kretprobe.kp.addr = (kprobe_opcode_t *)addr;
411 addr = swap_ksyms("mm_release");
413 EPRINTF("Cannot find address for mm_release function!");
416 mr_jprobe.kp.addr = (kprobe_opcode_t *)addr;
418 addr = swap_ksyms("do_munmap");
420 EPRINTF("Cannot find address for do_munmap function!");
423 unmap_jprobe.kp.addr = (kprobe_opcode_t *)addr;
428 void uninit_helper(void)
432 static int register_helper_ks_probes(void)
436 /* install jprobe on 'do_munmap' to detect when for remove user space probes */
437 ret = dbi_register_jprobe(&unmap_jprobe);
439 EPRINTF("dbi_register_jprobe(do_munmap) result=%d!", ret);
443 /* install jprobe on 'mm_release' to detect when for remove user space probes */
444 ret = dbi_register_jprobe(&mr_jprobe);
446 EPRINTF("dbi_register_jprobe(mm_release) result=%d!", ret);
447 goto unregister_unmap;
451 /* install kretprobe on 'copy_process' */
452 ret = dbi_register_kretprobe(&cp_kretprobe);
454 EPRINTF("dbi_register_kretprobe(copy_process) result=%d!", ret);
458 /* install kretprobe on 'do_page_fault' to detect when they will be loaded */
459 ret = dbi_register_kretprobe(&pf_kretprobe);
461 EPRINTF("dbi_register_kretprobe(do_page_fault) result=%d!", ret);
468 dbi_unregister_kretprobe(&cp_kretprobe);
471 dbi_unregister_jprobe(&mr_jprobe);
474 dbi_unregister_jprobe(&unmap_jprobe);
479 static void unregister_helper_ks_probes(void)
481 /* uninstall kretprobe with 'do_page_fault' */
482 dbi_unregister_kretprobe(&pf_kretprobe);
484 /* uninstall kretprobe with 'copy_process' */
485 dbi_unregister_kretprobe(&cp_kretprobe);
487 /* uninstall jprobe with 'mm_release' */
488 dbi_unregister_jprobe(&mr_jprobe);
490 /* uninstall jprobe with 'do_munmap' */
491 dbi_unregister_jprobe(&unmap_jprobe);
494 static int uninstall_us_proc_probes(struct task_struct *task, struct sspt_procs *procs, enum US_FLAGS flag);
496 int deinst_usr_space_proc (void)
498 int iRet = 0, found = 0;
499 struct task_struct *task = NULL;
501 if (!is_us_instrumentation()) {
506 EPRINTF ("uninstall_kernel_probe(do_munmap) result=%d!", iRet);
509 struct sspt_procs *procs;
511 for_each_process(task) {
512 procs = get_proc_probes_by_task(task);
514 int ret = uninstall_us_proc_probes(task, procs, US_UNREGS_PROBE);
516 EPRINTF ("failed to uninstall IPs (%d)!", ret);
519 dbi_unregister_all_uprobes(task, 1);
525 if (us_proc_info.tgid == 0)
528 for_each_process (task)
530 if (task->tgid == us_proc_info.tgid)
533 get_task_struct (task);
542 ret = uninstall_us_proc_probes(task, us_proc_info.pp, US_UNREGS_PROBE);
544 EPRINTF ("failed to uninstall IPs %d!", ret);
547 put_task_struct (task);
549 printk("### 1 ### dbi_unregister_all_uprobes:\n");
550 dbi_unregister_all_uprobes(task, 1);
551 us_proc_info.tgid = 0;
552 for(i = 0; i < us_proc_info.libs_count; i++)
553 us_proc_info.p_libs[i].loaded = 0;
557 unregister_helper_ks_probes();
562 static void install_proc_probes(struct task_struct *task, struct sspt_procs *procs, int atomic);
564 int inst_usr_space_proc (void)
567 struct task_struct *task = NULL;
569 if (!is_us_instrumentation()) {
573 DPRINTF("User space instr");
575 ret = register_helper_ks_probes();
580 for (i = 0; i < us_proc_info.libs_count; i++) {
581 us_proc_info.p_libs[i].loaded = 0;
583 /* check whether process is already running
584 * 1) if process is running - look for the libraries in the process maps
585 * 1.1) check if page for symbol does exist
586 * 1.1.1) if page exists - instrument it
587 * 1.1.2) if page does not exist - make sure that do_page_fault handler is installed
588 * 2) if process is not running - make sure that do_page_fault handler is installed
593 // FIXME: clear_task_inst_info();
594 for_each_process (task) {
595 struct sspt_procs *procs;
597 if (task->flags & PF_KTHREAD){
598 DPRINTF("ignored kernel thread %d\n",
603 procs = get_proc_probes_by_task_or_new(task);
604 DPRINTF("trying process");
605 install_proc_probes(task, procs, 1);
606 //put_task_struct (task);
611 ret = find_task_by_path (us_proc_info.path, &task, NULL);
614 DPRINTF("task found. installing probes");
615 us_proc_info.tgid = task->pid;
616 us_proc_info.pp->sm = create_sm_us(task);
617 install_proc_probes(task, us_proc_info.pp, 0);
618 put_task_struct (task);
625 unsigned long imi_sum_time = 0;
626 unsigned long imi_sum_hit = 0;
628 static void set_mapping_file(struct sspt_file *file,
629 const struct sspt_procs *procs,
630 const struct task_struct *task,
631 const struct vm_area_struct *vma)
633 int app_flag = (vma->vm_file->f_dentry == procs->dentry);
635 file->vm_start = vma->vm_start;
636 file->vm_end = vma->vm_end;
638 pack_event_info(DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
639 task->tgid, file->name, vma->vm_start,
640 vma->vm_end - vma->vm_start, app_flag);
643 void print_vma(struct mm_struct *mm);
645 static int register_us_page_probe(struct sspt_page *page,
646 const struct sspt_file *file,
647 struct task_struct *task)
650 struct us_ip *ip, *n;
652 spin_lock(&page->lock);
654 if (sspt_page_is_install(page)) {
655 printk("page %lx in %s task[tgid=%u, pid=%u] already installed\n",
656 page->offset, file->dentry->d_iname, task->tgid, task->pid);
661 sspt_page_assert_install(page);
662 sspt_set_all_ip_addr(page, file);
664 list_for_each_entry_safe(ip, n, &page->ip_list, list) {
665 err = register_usprobe_my(task, ip);
666 if (err == -ENOEXEC) {
671 EPRINTF("Failed to install probe");
675 sspt_page_installed(page);
676 spin_unlock(&page->lock);
681 static int unregister_us_page_probe(struct task_struct *task,
682 struct sspt_page *page, enum US_FLAGS flag)
687 spin_lock(&page->lock);
688 if (!sspt_page_is_install(page)) {
689 spin_unlock(&page->lock);
693 list_for_each_entry(ip, &page->ip_list, list) {
694 err = unregister_usprobe_my(task, ip, flag);
701 if (flag != US_DISARM) {
702 sspt_page_uninstalled(page);
704 spin_unlock(&page->lock);
709 static void install_page_probes(unsigned long page_addr, struct task_struct *task, struct sspt_procs *procs, int atomic)
712 struct mm_struct *mm;
713 struct vm_area_struct *vma;
715 mm_read_lock(task, mm, atomic, lock);
717 vma = find_vma(mm, page_addr);
718 if (vma && check_vma(vma)) {
719 struct dentry *dentry = vma->vm_file->f_dentry;
720 struct sspt_file *file = sspt_procs_find_file(procs, dentry);
722 struct sspt_page *page;
724 set_mapping_file(file, procs, task, vma);
728 page = sspt_find_page_mapped(file, page_addr);
730 register_us_page_probe(page, file, task);
735 mm_read_unlock(mm, atomic, lock);
738 static void install_file_probes(struct task_struct *task, struct mm_struct *mm, struct sspt_file *file)
740 struct sspt_page *page = NULL;
741 struct hlist_node *node = NULL;
742 struct hlist_head *head = NULL;
743 int i, table_size = (1 << file->page_probes_hash_bits);
745 for (i = 0; i < table_size; ++i) {
746 head = &file->page_probes_table[i];
747 swap_hlist_for_each_entry_rcu(page, node, head, hlist) {
748 register_us_page_probe(page, file, task);
753 static void install_proc_probes(struct task_struct *task, struct sspt_procs *procs, int atomic)
756 struct vm_area_struct *vma;
757 struct mm_struct *mm;
759 mm_read_lock(task, mm, atomic, lock);
761 for (vma = mm->mmap; vma; vma = vma->vm_next) {
762 if (check_vma(vma)) {
763 struct dentry *dentry = vma->vm_file->f_dentry;
764 struct sspt_file *file = sspt_procs_find_file(procs, dentry);
767 set_mapping_file(file, procs, task, vma);
771 install_file_probes(task, mm, file);
776 mm_read_unlock(mm, atomic, lock);
779 static int check_install_pages_in_file(struct task_struct *task, struct sspt_file *file)
782 int table_size = (1 << file->page_probes_hash_bits);
783 struct sspt_page *page;
784 struct hlist_node *node, *tmp;
785 struct hlist_head *head;
787 for (i = 0; i < table_size; ++i) {
788 head = &file->page_probes_table[i];
789 swap_hlist_for_each_entry_safe (page, node, tmp, head, hlist) {
799 static int unregister_us_file_probes(struct task_struct *task, struct sspt_file *file, enum US_FLAGS flag)
802 int table_size = (1 << file->page_probes_hash_bits);
803 struct sspt_page *page;
804 struct hlist_node *node, *tmp;
805 struct hlist_head *head;
807 for (i = 0; i < table_size; ++i) {
808 head = &file->page_probes_table[i];
809 swap_hlist_for_each_entry_safe (page, node, tmp, head, hlist) {
810 err = unregister_us_page_probe(task, page, flag);
818 if (flag != US_DISARM) {
825 static int uninstall_us_proc_probes(struct task_struct *task, struct sspt_procs *procs, enum US_FLAGS flag)
828 struct sspt_file *file;
830 list_for_each_entry_rcu(file, &procs->file_list, list) {
831 err = unregister_us_file_probes(task, file, flag);
841 static pid_t find_proc_by_task(const struct task_struct *task, struct dentry *dentry)
843 struct vm_area_struct *vma;
844 struct mm_struct *mm = task->active_mm;
849 for (vma = mm->mmap; vma; vma = vma->vm_next) {
850 if (check_vma(vma)) {
851 if (vma->vm_file->f_dentry == dentry) {
860 static int entry_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
862 struct pf_data *data = (struct pf_data *)ri->data;
865 data->addr = regs->ARM_r0;
869 /* Detects when IPs are really loaded into phy mem and installs probes. */
870 static int ret_handler_pf(struct kretprobe_instance *ri, struct pt_regs *regs)
872 struct task_struct *task = current->group_leader;
873 struct mm_struct *mm = task->mm;
874 struct sspt_procs *procs = NULL;
876 * Because process threads have same address space
877 * we instrument only group_leader of all this threads
879 struct pf_data *data;
880 unsigned long addr = 0;
884 struct timeval imi_tv1;
885 struct timeval imi_tv2;
886 #define USEC_IN_SEC_NUM 1000000
888 if (task->flags & PF_KTHREAD) {
892 if (!is_us_instrumentation()) {
896 data = (struct pf_data *)ri->data;
899 valid_addr = mm && page_present(mm, addr);
905 procs = get_proc_probes_by_task_or_new(task);
908 if (us_proc_info.tgid == 0) {
909 pid_t tgid = find_proc_by_task(task, us_proc_info.m_f_dentry);
911 us_proc_info.tgid = gl_nNotifyTgid = tgid;
913 us_proc_info.pp->sm = create_sm_us(task);
914 /* install probes in already mapped memory */
915 install_proc_probes(task, us_proc_info.pp, 1);
919 if (us_proc_info.tgid == task->tgid) {
920 procs = us_proc_info.pp;
925 unsigned long page = addr & PAGE_MASK;
928 do_gettimeofday(&imi_tv1);
929 install_page_probes(page, task, procs, 1);
930 do_gettimeofday(&imi_tv2);
932 imi_sum_time += ((imi_tv2.tv_sec - imi_tv1.tv_sec) * USEC_IN_SEC_NUM +
933 (imi_tv2.tv_usec - imi_tv1.tv_usec));
940 void print_vma(struct mm_struct *mm)
942 struct vm_area_struct *vma;
943 printk("### print_vma: START\n");\
944 printk("### print_vma: START\n");
946 for (vma = mm->mmap; vma; vma = vma->vm_next) {
947 char *x = vma->vm_flags & VM_EXEC ? "x" : "-";
948 char *r = vma->vm_flags & VM_READ ? "r" : "-";
949 char *w = vma->vm_flags & VM_WRITE ? "w" : "-";
950 char *name = vma->vm_file ? (char *)vma->vm_file->f_dentry->d_iname : "N/A";
952 printk("### [%8lx..%8lx] %s%s%s pgoff=\'%8lu\' %s\n",
953 vma->vm_start, vma->vm_end, x, r, w, vma->vm_pgoff, name);
955 printk("### print_vma: END\n");
958 static int remove_unmap_probes(struct task_struct *task, struct sspt_procs *procs, unsigned long start, size_t len)
960 struct mm_struct *mm = task->mm;
961 struct vm_area_struct *vma;
963 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE - start) {
967 if ((len = PAGE_ALIGN(len)) == 0) {
971 vma = find_vma(mm, start);
972 if (vma && check_vma(vma)) {
973 struct sspt_file *file;
974 unsigned long end = start + len;
975 struct dentry *dentry = vma->vm_file->f_dentry;
977 file = sspt_procs_find_file(procs, dentry);
979 if (vma->vm_start == start || vma->vm_end == end) {
980 unregister_us_file_probes(task, file, US_UNREGS_PROBE);
983 unsigned long page_addr;
984 struct sspt_page *page;
986 for (page_addr = vma->vm_start; page_addr < vma->vm_end; page_addr += PAGE_SIZE) {
987 page = sspt_find_page_mapped(file, page_addr);
989 unregister_us_page_probe(task, page, US_UNREGS_PROBE);
993 if (check_install_pages_in_file(task, file)) {
1003 /* Detects when target removes IPs. */
1004 static int jdo_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1006 struct sspt_procs *procs = NULL;
1007 struct task_struct *task = current;
1009 //if user-space instrumentation is not set
1010 if (!is_us_instrumentation()) {
1015 procs = get_proc_probes_by_task(task);
1017 if (task->tgid == us_proc_info.tgid) {
1018 procs = us_proc_info.pp;
1023 if (remove_unmap_probes(task, procs, start, len)) {
1024 printk("ERROR do_munmap: start=%lx, len=%x\n", start, len);
1029 dbi_jprobe_return();
1033 /* Detects when target process removes IPs. */
1034 static void jmm_release(struct task_struct *task, struct mm_struct *mm)
1036 struct sspt_procs *procs = NULL;
1038 if (!is_us_instrumentation() || task->tgid != task->pid) {
1043 procs = get_proc_probes_by_task(task);
1045 if (task->tgid == us_proc_info.tgid) {
1046 procs = get_proc_probes_by_task(task);
1047 us_proc_info.tgid = 0;
1052 int ret = uninstall_us_proc_probes(task, procs, US_UNREGS_PROBE);
1054 EPRINTF ("failed to uninstall IPs (%d)!", ret);
1057 dbi_unregister_all_uprobes(task, 1);
1061 dbi_jprobe_return();
1064 static void recover_child(struct task_struct *child_task, struct sspt_procs *procs)
1066 uninstall_us_proc_probes(child_task, procs, US_DISARM);
1067 dbi_disarm_urp_inst_for_task(current, child_task);
1070 static void rm_uprobes_child(struct task_struct *new_task)
1073 struct sspt_procs *procs = get_proc_probes_by_task(current);
1075 recover_child(new_task, procs);
1078 if(us_proc_info.tgid == current->tgid) {
1079 recover_child(new_task, us_proc_info.pp);
1084 /* Delete uprobs in children at fork */
1085 static void copy_process_ret_pre_code(struct task_struct *p)
1090 if(p->mm != current->mm) // check flags CLONE_VM
1091 rm_uprobes_child(p);
1094 static DEFINE_PER_CPU(struct us_ip *, gpCurIp) = NULL;
1095 EXPORT_PER_CPU_SYMBOL_GPL(gpCurIp);
1096 static DEFINE_PER_CPU(struct pt_regs *, gpUserRegs) = NULL;
1097 EXPORT_PER_CPU_SYMBOL_GPL(gpUserRegs);
1099 unsigned long ujprobe_event_pre_handler(struct us_ip *ip, struct pt_regs *regs)
1101 __get_cpu_var (gpCurIp) = ip;
1102 __get_cpu_var (gpUserRegs) = regs;
1106 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
1108 struct us_ip *ip = __get_cpu_var(gpCurIp);
1109 unsigned long addr = (unsigned long)ip->jprobe.up.kp.addr;
1111 #if defined(CONFIG_ARM)
1112 if (ip->offset & 0x01)
1114 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr | 0x01, arg1, arg2, arg3, arg4, arg5, arg6);
1116 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1119 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1121 // Mr_Nobody: uncomment for valencia
1122 //unregister_usprobe(current, ip, 1);
1123 dbi_uprobe_return ();
1126 static void send_plt(struct us_ip *ip)
1128 unsigned long addr = (unsigned long)ip->jprobe.up.kp.addr;
1129 struct vm_area_struct *vma = find_vma(current->mm, addr);
1131 if (vma && check_vma(vma)) {
1133 unsigned long real_addr;
1134 unsigned long real_got = current->mm->exe_file == vma->vm_file ?
1136 ip->got_addr + vma->vm_start;
1138 if (!read_proc_vm_atomic(current, real_got, &real_addr, sizeof(real_addr))) {
1139 printk("Failed to read got %lx at memory address %lx!\n", ip->got_addr, real_got);
1143 vma = find_vma(current->mm, real_addr);
1144 if (vma && (vma->vm_start <= real_addr) && (vma->vm_end > real_addr)) {
1145 name = vma->vm_file ? vma->vm_file->f_dentry->d_iname : NULL;
1147 printk("Failed to get vma, includes %lx address\n", real_addr);
1152 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppsp", addr, real_addr, name, real_addr - vma->vm_start);
1154 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppp", addr, real_addr, real_addr - vma->vm_start);
1159 int uretprobe_event_handler(struct uretprobe_instance *probe, struct pt_regs *regs, struct us_ip *ip)
1161 int retval = regs_return_value(regs);
1162 unsigned long addr = (unsigned long)ip->jprobe.up.kp.addr;
1164 if (ip->got_addr && ip->flag_got == 0) {
1169 #if defined(CONFIG_ARM)
1170 if (ip->offset & 0x01)
1172 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr | 0x01, retval);
1174 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1177 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1179 // Mr_Nobody: uncomment for valencia
1180 //unregister_usprobe(current, ip, 1);
1184 int register_usprobe(struct task_struct *task, struct us_ip *ip, int atomic)
1188 if (ip->jprobe.entry == NULL) {
1189 ip->jprobe.entry = (void *)ujprobe_event_handler;
1190 DPRINTF("Set default event handler for %x\n", ip->offset);
1193 if (ip->jprobe.pre_entry == NULL) {
1194 ip->jprobe.pre_entry = (uprobe_pre_entry_handler_t)ujprobe_event_pre_handler;
1195 DPRINTF("Set default pre handler for %x\n", ip->offset);
1198 ip->jprobe.priv_arg = ip;
1199 ip->jprobe.up.task = task;
1200 ip->jprobe.up.sm = ip->page->file->procs->sm;
1201 ret = dbi_register_ujprobe(&ip->jprobe, atomic);
1203 if (ret == -ENOEXEC) {
1204 pack_event_info(ERR_MSG_ID, RECORD_ENTRY, "dp",
1206 ip->jprobe.up.kp.addr);
1208 DPRINTF ("dbi_register_ujprobe() failure %d", ret);
1212 if (ip->flag_retprobe) {
1213 // Mr_Nobody: comment for valencia
1214 if (ip->retprobe.handler == NULL) {
1215 ip->retprobe.handler = (uretprobe_handler_t)uretprobe_event_handler;
1216 DPRINTF("Set default ret event handler for %x\n", ip->offset);
1219 ip->retprobe.priv_arg = ip;
1220 ip->retprobe.up.task = task;
1221 ip->retprobe.up.sm = ip->page->file->procs->sm;
1222 ret = dbi_register_uretprobe(&ip->retprobe, atomic);
1224 EPRINTF ("dbi_register_uretprobe() failure %d", ret);
1232 int unregister_usprobe(struct task_struct *task, struct us_ip *ip, int atomic)
1234 dbi_unregister_ujprobe(&ip->jprobe, atomic);
1236 if (ip->flag_retprobe) {
1237 dbi_unregister_uretprobe(&ip->retprobe, atomic);