[REFACTOR] move trampoline for thumb in uprobe module
[kernel/swap-modules.git] / kprobe / arch / asm-arm / dbi_kprobes.c
1 /*
2  *  Dynamic Binary Instrumentation Module based on KProbes
3  *  modules/kprobe/arch/asm-arm/dbi_kprobes.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2006-2010
20  *
21  * 2006-2007    Ekaterina Gorelkina <e.gorelkina@samsung.com>: initial implementation for ARM/MIPS
22  * 2008-2009    Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
23  *              Probes initial implementation; Support x86.
24  * 2010         Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
25  * 2010-2011    Alexander Shirshikov <a.shirshikov@samsung.com>: initial implementation for Thumb
26  * 2012         Stanislav Andreev <s.andreev@samsung.com>: added time debug profiling support; BUG() message fix
27  * 2012         Stanislav Andreev <s.andreev@samsung.com>: redesign of kprobe functionality -
28  *              kprobe_handler() now called via undefined instruction hooks
29  * 2012         Stanislav Andreev <s.andreev@samsung.com>: hash tables search implemented for uprobes
30  */
31
32 #include <linux/module.h>
33 #include <linux/mm.h>
34
35 #include "dbi_kprobes.h"
36 #include "dbi_kprobes_arm.h"
37 #include "../dbi_kprobes.h"
38 #include "../../dbi_kprobes.h"
39
40 #include "../../dbi_kdebug.h"
41 #include "../../dbi_insn_slots.h"
42 #include "../../dbi_kprobes_deps.h"
43 #include <ksyms.h>
44
45 #include <asm/cacheflush.h>
46
47 #ifdef TRAP_OVERHEAD_DEBUG
48 #include <linux/pid.h>
49 #include <linux/signal.h>
50 #endif
51
52 #ifdef OVERHEAD_DEBUG
53 #include <linux/time.h>
54 #endif
55
56 #include <asm/traps.h>
57 #include <asm/ptrace.h>
58 #include <linux/list.h>
59 #include <linux/hash.h>
60
61 #define SUPRESS_BUG_MESSAGES
62
63 extern struct kprobe * per_cpu__current_kprobe;
64 extern struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
65
66 static void (*__swap_register_undef_hook)(struct undef_hook *hook);
67 static void (*__swap_unregister_undef_hook)(struct undef_hook *hook);
68
69 #ifdef OVERHEAD_DEBUG
70 unsigned long swap_sum_time = 0;
71 unsigned long swap_sum_hit = 0;
72 EXPORT_SYMBOL_GPL (swap_sum_time);
73 EXPORT_SYMBOL_GPL (swap_sum_hit);
74 #endif
75
76 static struct kprobe trampoline_p =
77 {
78         .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
79         .pre_handler = trampoline_probe_handler
80 };
81
82 int prep_pc_dep_insn_execbuf(kprobe_opcode_t *insns, kprobe_opcode_t insn, int uregs)
83 {
84         int i;
85
86         if (uregs & 0x10)
87         {
88                 int reg_mask = 0x1;
89                 //search in reg list
90                 for (i = 0; i < 13; i++, reg_mask <<= 1)
91                 {
92                         if (!(insn & reg_mask))
93                                 break;
94                 }
95         }
96         else
97         {
98                 for (i = 0; i < 13; i++)
99                 {
100                         if ((uregs & 0x1) && (ARM_INSN_REG_RN (insn) == i))
101                                 continue;
102                         if ((uregs & 0x2) && (ARM_INSN_REG_RD (insn) == i))
103                                 continue;
104                         if ((uregs & 0x4) && (ARM_INSN_REG_RS (insn) == i))
105                                 continue;
106                         if ((uregs & 0x8) && (ARM_INSN_REG_RM (insn) == i))
107                                 continue;
108                         break;
109                 }
110         }
111         if (i == 13)
112         {
113                 DBPRINTF ("there are no free register %x in insn %lx!", uregs, insn);
114                 return -EINVAL;
115         }
116         DBPRINTF ("prep_pc_dep_insn_execbuf: using R%d, changing regs %x", i, uregs);
117
118         // set register to save
119         ARM_INSN_REG_SET_RD (insns[0], i);
120         // set register to load address to
121         ARM_INSN_REG_SET_RD (insns[1], i);
122         // set instruction to execute and patch it
123         if (uregs & 0x10)
124         {
125                 ARM_INSN_REG_CLEAR_MR (insn, 15);
126                 ARM_INSN_REG_SET_MR (insn, i);
127         }
128         else
129         {
130                 if ((uregs & 0x1) && (ARM_INSN_REG_RN (insn) == 15))
131                         ARM_INSN_REG_SET_RN (insn, i);
132                 if ((uregs & 0x2) && (ARM_INSN_REG_RD (insn) == 15))
133                         ARM_INSN_REG_SET_RD (insn, i);
134                 if ((uregs & 0x4) && (ARM_INSN_REG_RS (insn) == 15))
135                         ARM_INSN_REG_SET_RS (insn, i);
136                 if ((uregs & 0x8) && (ARM_INSN_REG_RM (insn) == 15))
137                         ARM_INSN_REG_SET_RM (insn, i);
138         }
139         insns[UPROBES_TRAMP_INSN_IDX] = insn;
140         // set register to restore
141         ARM_INSN_REG_SET_RD (insns[3], i);
142         return 0;
143 }
144 EXPORT_SYMBOL_GPL(prep_pc_dep_insn_execbuf);
145
146 int arch_check_insn_arm(struct arch_specific_insn *ainsn)
147 {
148         int ret = 0;
149
150         // check instructions that can change PC by nature
151         if (
152 //              ARM_INSN_MATCH (UNDEF, ainsn->insn_arm[0]) ||
153                 ARM_INSN_MATCH (AUNDEF, ainsn->insn_arm[0]) ||
154                 ARM_INSN_MATCH (SWI, ainsn->insn_arm[0]) ||
155                 ARM_INSN_MATCH (BREAK, ainsn->insn_arm[0]) ||
156                 ARM_INSN_MATCH (BL, ainsn->insn_arm[0]) ||
157                 ARM_INSN_MATCH (BLX1, ainsn->insn_arm[0]) ||
158                 ARM_INSN_MATCH (BLX2, ainsn->insn_arm[0]) ||
159                 ARM_INSN_MATCH (BX, ainsn->insn_arm[0]) ||
160                 ARM_INSN_MATCH (BXJ, ainsn->insn_arm[0]))
161         {
162                 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
163                 ret = -EFAULT;
164         }
165 #ifndef CONFIG_CPU_V7
166         // check instructions that can write result to PC
167         else if ((ARM_INSN_MATCH (DPIS, ainsn->insn_arm[0]) ||
168                                 ARM_INSN_MATCH (DPRS, ainsn->insn_arm[0]) ||
169                                 ARM_INSN_MATCH (DPI, ainsn->insn_arm[0]) ||
170                                 ARM_INSN_MATCH (LIO, ainsn->insn_arm[0]) ||
171                                 ARM_INSN_MATCH (LRO, ainsn->insn_arm[0])) &&
172                         (ARM_INSN_REG_RD (ainsn->insn_arm[0]) == 15))
173         {
174                 DBPRINTF ("Bad arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
175                 ret = -EFAULT;
176         }
177 #endif // CONFIG_CPU_V7
178         // check special instruction loads store multiple registers
179         else if ((ARM_INSN_MATCH (LM, ainsn->insn_arm[0]) || ARM_INSN_MATCH (SM, ainsn->insn_arm[0])) &&
180                         // store pc or load to pc
181                         (ARM_INSN_REG_MR (ainsn->insn_arm[0], 15) ||
182                          // store/load with pc update
183                          ((ARM_INSN_REG_RN (ainsn->insn_arm[0]) == 15) && (ainsn->insn_arm[0] & 0x200000))))
184         {
185                 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
186                 ret = -EFAULT;
187         }
188         return ret;
189 }
190 EXPORT_SYMBOL_GPL(arch_check_insn_arm);
191
192 int arch_prepare_kretprobe (struct kretprobe *p)
193 {
194         DBPRINTF("Warrning: arch_prepare_kretprobe is not implemented\n");
195         return 0;
196 }
197
198 int arch_prepare_kprobe (struct kprobe *p)
199 {
200         kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
201         int uregs, pc_dep, ret = 0;
202     kprobe_opcode_t insn[MAX_INSN_SIZE];
203     struct arch_specific_insn ainsn;
204
205     /* insn: must be on special executable page on i386. */
206     p->ainsn.insn = get_insn_slot(NULL, &kprobe_insn_pages, 0);
207     if (!p->ainsn.insn)
208         return -ENOMEM;
209
210     memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
211     ainsn.insn_arm = ainsn.insn = insn;
212     ret = arch_check_insn_arm (&ainsn);
213     if (!ret)
214     {
215         p->opcode = *p->addr;
216         uregs = pc_dep = 0;
217
218         // Rn, Rm ,Rd
219         if(ARM_INSN_MATCH (DPIS, insn[0]) || ARM_INSN_MATCH (LRO, insn[0]) ||
220            ARM_INSN_MATCH (SRO, insn[0]))
221         {
222             uregs = 0xb;
223             if( (ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
224                 (ARM_INSN_MATCH (SRO, insn[0]) && (ARM_INSN_REG_RD (insn[0]) == 15)) )
225             {
226                 DBPRINTF ("Unboostable insn %lx, DPIS/LRO/SRO\n", insn[0]);
227                 pc_dep = 1;
228             }
229         }
230         // Rn ,Rd
231         else if(ARM_INSN_MATCH (DPI, insn[0]) || ARM_INSN_MATCH (LIO, insn[0]) ||
232                 ARM_INSN_MATCH (SIO, insn[0]))
233         {
234             uregs = 0x3;
235             if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_MATCH (SIO, insn[0]) &&
236                         (ARM_INSN_REG_RD (insn[0]) == 15)))
237             {
238                 pc_dep = 1;
239                 DBPRINTF ("Unboostable insn %lx/%p, DPI/LIO/SIO\n", insn[0], p);
240             }
241         }
242         // Rn, Rm, Rs
243         else if(ARM_INSN_MATCH (DPRS, insn[0]))
244         {
245             uregs = 0xd;
246             if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
247                 (ARM_INSN_REG_RS (insn[0]) == 15))
248             {
249                 pc_dep = 1;
250                 DBPRINTF ("Unboostable insn %lx, DPRS\n", insn[0]);
251             }
252         }
253         // register list
254         else if(ARM_INSN_MATCH (SM, insn[0]))
255         {
256             uregs = 0x10;
257             if (ARM_INSN_REG_MR (insn[0], 15))
258             {
259                 DBPRINTF ("Unboostable insn %lx, SM\n", insn[0]);
260                 pc_dep = 1;
261             }
262         }
263         // check instructions that can write result to SP andu uses PC
264         if (pc_dep  && (ARM_INSN_REG_RD (ainsn.insn[0]) == 13))
265         {
266             free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
267             ret = -EFAULT;
268         }
269         else
270         {
271             if (uregs && pc_dep)
272             {
273                 memcpy (insns, pc_dep_insn_execbuf, sizeof (insns));
274                 if (prep_pc_dep_insn_execbuf (insns, insn[0], uregs) != 0)
275                 {
276                     DBPRINTF ("failed to prepare exec buffer for insn %lx!", insn[0]);
277                     free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
278                     return -EINVAL;
279                 }
280                 insns[6] = (kprobe_opcode_t) (p->addr + 2);
281             }
282             else
283             {
284                 memcpy (insns, gen_insn_execbuf, sizeof (insns));
285                 insns[KPROBES_TRAMP_INSN_IDX] = insn[0];
286             }
287             insns[7] = (kprobe_opcode_t) (p->addr + 1);
288             DBPRINTF ("arch_prepare_kprobe: insn %lx", insn[0]);
289             DBPRINTF ("arch_prepare_kprobe: to %p - %lx %lx %lx %lx %lx %lx %lx %lx %lx",
290                     p->ainsn.insn, insns[0], insns[1], insns[2], insns[3], insns[4],
291                     insns[5], insns[6], insns[7], insns[8]);
292             memcpy (p->ainsn.insn, insns, sizeof(insns));
293             flush_icache_range((long unsigned)p->ainsn.insn, (long unsigned)(p->ainsn.insn) + sizeof(insns));
294 #ifdef BOARD_tegra
295             flush_cache_all();
296 #endif
297         }
298     }
299     else
300     {
301         free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
302         printk("arch_prepare_kprobe: instruction 0x%lx not instrumentation, addr=0x%p\n", insn[0], p->addr);
303     }
304
305     return ret;
306 }
307
308 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
309 {
310         if (p->ss_addr) {
311                 regs->ARM_pc = (unsigned long)p->ss_addr;
312                 p->ss_addr = NULL;
313         } else {
314                 regs->ARM_pc = (unsigned long)p->ainsn.insn;
315         }
316 }
317 EXPORT_SYMBOL_GPL(prepare_singlestep);
318
319 void save_previous_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p_run)
320 {
321         kcb->prev_kprobe.kp = kprobe_running();
322         kcb->prev_kprobe.status = kcb->kprobe_status;
323 }
324
325 void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
326 {
327         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
328         kcb->kprobe_status = kcb->prev_kprobe.status;
329 }
330
331 void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
332 {
333         __get_cpu_var(current_kprobe) = p;
334         DBPRINTF ("set_current_kprobe: p=%p addr=%p\n", p, p->addr);
335 }
336
337 #ifdef TRAP_OVERHEAD_DEBUG
338 static unsigned long trap_handler_counter_debug = 0;
339 #define SAMPLING_COUNTER                               100000
340 #endif
341
342 static int kprobe_handler(struct pt_regs *regs)
343 {
344         struct kprobe *p, *cur;
345         struct kprobe_ctlblk *kcb;
346
347         kcb = get_kprobe_ctlblk();
348         cur = kprobe_running();
349         p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc, 0);
350
351         if (p) {
352                 if (cur) {
353                         /* Kprobe is pending, so we're recursing. */
354                         switch (kcb->kprobe_status) {
355                         case KPROBE_HIT_ACTIVE:
356                         case KPROBE_HIT_SSDONE:
357                                 /* A pre- or post-handler probe got us here. */
358                                 kprobes_inc_nmissed_count(p);
359                                 save_previous_kprobe(kcb, NULL);
360                                 set_current_kprobe(p, 0, 0);
361                                 kcb->kprobe_status = KPROBE_REENTER;
362                                 prepare_singlestep(p, regs);
363                                 restore_previous_kprobe(kcb);
364                                 break;
365                         default:
366                                 /* impossible cases */
367                                 BUG();
368                         }
369                 } else {
370                         set_current_kprobe(p, 0, 0);
371                         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
372
373                         if (!p->pre_handler || !p->pre_handler(p, regs)) {
374                                 kcb->kprobe_status = KPROBE_HIT_SS;
375                                 prepare_singlestep(p, regs);
376                                 reset_current_kprobe();
377                         }
378                 }
379         } else {
380                 goto no_kprobe;
381         }
382
383         return 0;
384
385 no_kprobe:
386         printk("no_kprobe\n");
387         return 1;
388 }
389
390 int kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
391 {
392         int ret;
393         unsigned long flags;
394
395 #ifdef SUPRESS_BUG_MESSAGES
396         int swap_oops_in_progress;
397         /* oops_in_progress used to avoid BUG() messages
398          * that slow down kprobe_handler() execution */
399         swap_oops_in_progress = oops_in_progress;
400         oops_in_progress = 1;
401 #endif
402
403         local_irq_save(flags);
404         preempt_disable();
405         ret = kprobe_handler(regs);
406         preempt_enable_no_resched();
407         local_irq_restore(flags);
408
409 #ifdef SUPRESS_BUG_MESSAGES
410         oops_in_progress = swap_oops_in_progress;
411 #endif
412
413         return ret;
414 }
415
416 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
417 {
418         struct jprobe *jp = container_of(p, struct jprobe, kp);
419         kprobe_pre_entry_handler_t pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
420         entry_point_t entry = (entry_point_t)jp->entry;
421         pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
422
423         if (p->tgid) {
424                 panic("setjmp_pre_handler: p->tgid == 0");
425         }
426
427         if (((unsigned long)p->addr == sched_addr) && sched_rp) {
428                 struct thread_info *tinfo = (struct thread_info *)regs->ARM_r2;
429                 patch_suspended_task(sched_rp, tinfo->task);
430         }
431
432         if (pre_entry) {
433                 p->ss_addr = (void *)pre_entry (jp->priv_arg, regs);
434         }
435
436         if (entry) {
437                 entry(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2,
438                       regs->ARM_r3, regs->ARM_r4, regs->ARM_r5);
439         } else {
440                 dbi_jprobe_return();
441         }
442
443         return 0;
444 }
445
446 void dbi_jprobe_return (void)
447 {
448 }
449
450 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
451 {
452 # ifndef REENTER
453         //kprobe_opcode_t insn = BREAKPOINT_INSTRUCTION;
454         kprobe_opcode_t insns[2];
455
456         if (p->pid)
457         {
458                 insns[0] = BREAKPOINT_INSTRUCTION;
459                 insns[1] = p->opcode;
460                 //p->opcode = *p->addr;
461                 if (read_proc_vm_atomic (current, (unsigned long) (p->addr), &(p->opcode), sizeof (p->opcode)) < sizeof (p->opcode))
462                 {
463                         printk ("ERROR[%lu]: failed to read vm of proc %s/%u addr %p.", nCount, current->comm, current->pid, p->addr);
464                         return -1;
465                 }
466                 //*p->addr = BREAKPOINT_INSTRUCTION;
467                 //*(p->addr+1) = p->opcode;
468                 if (write_proc_vm_atomic (current, (unsigned long) (p->addr), insns, sizeof (insns)) < sizeof (insns))
469                 {
470                         printk ("ERROR[%lu]: failed to write vm of proc %s/%u addr %p.", nCount, current->comm, current->pid, p->addr);
471                         return -1;
472                 }
473         }
474         else
475         {
476                 DBPRINTF ("p->opcode = 0x%lx *p->addr = 0x%lx p->addr = 0x%p\n", p->opcode, *p->addr, p->addr);
477                 *(p->addr + 1) = p->opcode;
478                 p->opcode = *p->addr;
479                 *p->addr = BREAKPOINT_INSTRUCTION;
480
481                 flush_icache_range ((unsigned int) p->addr, (unsigned int) (((unsigned int) p->addr) + (sizeof (kprobe_opcode_t) * 2)));
482         }
483
484         reset_current_kprobe();
485
486 #endif //REENTER
487
488         return 0;
489 }
490 EXPORT_SYMBOL_GPL(longjmp_break_handler);
491
492 void arch_arm_kprobe (struct kprobe *p)
493 {
494         *p->addr = BREAKPOINT_INSTRUCTION;
495         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
496 }
497
498 void arch_disarm_kprobe (struct kprobe *p)
499 {
500         *p->addr = p->opcode;
501         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
502 }
503
504
505 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
506 {
507         struct kretprobe_instance *ri = NULL;
508         struct hlist_head *head;
509         struct hlist_node *node, *tmp;
510         unsigned long flags, orig_ret_address = 0;
511         unsigned long trampoline_address = (unsigned long) &kretprobe_trampoline;
512
513         struct kretprobe *crp = NULL;
514         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
515
516         DBPRINTF ("start");
517
518         if (p && p->tgid){
519                 // in case of user space retprobe trampoline is at the Nth instruction of US tramp
520                 if (!thumb_mode( regs ))
521                         trampoline_address = (unsigned long)(p->ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
522                 else
523                         trampoline_address = (unsigned long)(p->ainsn.insn) + 0x1b;
524         }
525
526         spin_lock_irqsave (&kretprobe_lock, flags);
527
528         /*
529          * We are using different hash keys (current and mm) for finding kernel
530          * space and user space probes.  Kernel space probes can change mm field in
531          * task_struct.  User space probes can be shared between threads of one
532          * process so they have different current but same mm.
533          */
534         if (p && p->tgid) {
535                 head = kretprobe_inst_table_head(current->mm);
536         } else {
537                 head = kretprobe_inst_table_head(current);
538         }
539
540         /*
541          * It is possible to have multiple instances associated with a given
542          * task either because an multiple functions in the call path
543          * have a return probe installed on them, and/or more then one
544          * return probe was registered for a target function.
545          *
546          * We can handle this because:
547          *     - instances are always inserted at the head of the list
548          *     - when multiple return probes are registered for the same
549          *       function, the first instance's ret_addr will point to the
550          *       real return address, and all the rest will point to
551          *       kretprobe_trampoline
552          */
553         hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
554         {
555                 if (ri->task != current)
556                         /* another task is sharing our hash bucket */
557                         continue;
558                 if (ri->rp && ri->rp->handler){
559                         ri->rp->handler (ri, regs, ri->rp->priv_arg);
560                 }
561
562                 orig_ret_address = (unsigned long) ri->ret_addr;
563                 recycle_rp_inst (ri);
564                 if (orig_ret_address != trampoline_address)
565                         /*
566                          * This is the real return address. Any other
567                          * instances associated with this task are for
568                          * other calls deeper on the call stack
569                          */
570                         break;
571         }
572         kretprobe_assert (ri, orig_ret_address, trampoline_address);
573         //BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
574         //E.G. Check this code in case of __switch_to function instrumentation -- currently this code generates dump in this case
575         //if (trampoline_address != (unsigned long) &kretprobe_trampoline){
576         //if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
577         //if (ri->rp) BUG_ON (ri->rp->kp.tgid == 0);
578         //else if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
579         //}
580         if ((ri->rp && ri->rp->kp.tgid) || (ri->rp2 && ri->rp2->kp.tgid))
581                 BUG_ON (trampoline_address == (unsigned long) &kretprobe_trampoline);
582
583         regs->uregs[14] = orig_ret_address;
584         DBPRINTF ("regs->uregs[14] = 0x%lx\n", regs->uregs[14]);
585         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
586
587         if (trampoline_address != (unsigned long) &kretprobe_trampoline)
588         {
589                 regs->uregs[15] = orig_ret_address;
590         }else{
591                 if (!thumb_mode( regs )) regs->uregs[15] += 4;
592                 else regs->uregs[15] += 2;
593         }
594
595         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
596
597         if(p){ // ARM, MIPS, X86 user space
598                 if (thumb_mode( regs ) && !(regs->uregs[14] & 0x01))
599                 {
600                         regs->ARM_cpsr &= 0xFFFFFFDF;
601                 }else{
602                         if (user_mode( regs ) && (regs->uregs[14] & 0x01))
603                         {
604                                 regs->ARM_cpsr |= 0x20;
605                         }
606                 }
607
608                 //TODO: test - enter function, delete us retprobe, exit function
609                 // for user space retprobes only - deferred deletion
610
611                 if (trampoline_address != (unsigned long) &kretprobe_trampoline)
612                 {
613                         // if we are not at the end of the list and current retprobe should be disarmed
614                         if (node && ri->rp2)
615                         {
616                                 struct hlist_node *current_node = node;
617                                 crp = ri->rp2;
618                                 /*sprintf(die_msg, "deferred disarm p->addr = %p [%lx %lx %lx]\n",
619                                   crp->kp.addr, *kaddrs[0], *kaddrs[1], *kaddrs[2]);
620                                   DIE(die_msg, regs); */
621                                 // look for other instances for the same retprobe
622                                 hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
623                                 {
624                                         /*
625                                          * Trying to find another retprobe instance associated with
626                                          * the same retprobe.
627                                          */
628                                         if (ri->rp2 == crp && node != current_node)
629                                                 break;
630                                 }
631
632                                 if (!node)
633                                 {
634                                         // if there are no more instances for this retprobe
635                                         // delete retprobe
636                                         struct kprobe *is_p = &crp->kp;
637                                         DBPRINTF ("defered retprobe deletion p->addr = %p", crp->kp.addr);
638                                         /*
639                                           If there is no any retprobe instances of this retprobe
640                                           we can free the resources related to the probe.
641                                          */
642                                         if (!(hlist_unhashed(&is_p->is_hlist_arm))) {
643                                                 hlist_del_rcu(&is_p->is_hlist_arm);
644                                         }
645                                         if (!(hlist_unhashed(&is_p->is_hlist_thumb))) {
646                                                 hlist_del_rcu(&is_p->is_hlist_thumb);
647                                         }
648
649                                         dbi_unregister_kprobe(&crp->kp, current);
650                                         kfree (crp);
651                                 }
652                                 hlist_del(current_node);
653                         }
654                 }
655
656                 if (kcb->kprobe_status == KPROBE_REENTER) {
657                         restore_previous_kprobe(kcb);
658                 } else {
659                         reset_current_kprobe();
660                 }
661         }
662
663         spin_unlock_irqrestore (&kretprobe_lock, flags);
664
665         /*
666          * By returning a non-zero value, we are telling
667          * kprobe_handler() that we don't want the post_handler
668          * to run (and have re-enabled preemption)
669          */
670
671         return 1;
672 }
673 EXPORT_SYMBOL_GPL(trampoline_probe_handler);
674
675 void  __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
676 {
677         struct kretprobe_instance *ri;
678
679         DBPRINTF ("start\n");
680         //TODO: test - remove retprobe after func entry but before its exit
681         if ((ri = get_free_rp_inst (rp)) != NULL)
682         {
683                 ri->rp = rp;
684                 ri->rp2 = NULL;
685                 ri->task = current;
686                 ri->ret_addr = (kprobe_opcode_t *) regs->uregs[14];
687                 ri->sp = (kprobe_opcode_t *)regs->ARM_sp; //uregs[13];
688
689                 /* Set flag of current mode */
690                 ri->sp = (kprobe_opcode_t *)((long)ri->sp | !!thumb_mode(regs));
691
692                 if (rp->kp.tgid) {
693                         panic("__arch_prepare_kretprobe: rp->kp.tgid != 0");
694                 }
695
696                 /* Replace the return addr with trampoline addr */
697                 regs->uregs[14] = (unsigned long) &kretprobe_trampoline;
698
699 //              DBPRINTF ("ret addr set to %p->%lx\n", ri->ret_addr, regs->uregs[14]);
700                 add_rp_inst (ri);
701         }
702         else {
703                 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
704                 rp->nmissed++;
705         }
706 }
707
708
709 int asm_init_module_dependencies(void)
710 {
711         //No module dependencies
712         return 0;
713 }
714
715 void swap_register_undef_hook(struct undef_hook *hook)
716 {
717         __swap_register_undef_hook(hook);
718 }
719 EXPORT_SYMBOL_GPL(swap_register_undef_hook);
720
721 void swap_unregister_undef_hook(struct undef_hook *hook)
722 {
723         __swap_unregister_undef_hook(hook);
724 }
725 EXPORT_SYMBOL_GPL(swap_unregister_undef_hook);
726
727 // kernel probes hook
728 static struct undef_hook undef_ho_k = {
729     .instr_mask = 0xffffffff,
730     .instr_val  = BREAKPOINT_INSTRUCTION,
731     .cpsr_mask  = MODE_MASK,
732     .cpsr_val   = SVC_MODE,
733     .fn         = kprobe_trap_handler
734 };
735
736 int __init arch_init_kprobes (void)
737 {
738         int ret = 0;
739
740         if (arch_init_module_dependencies())
741         {
742                 DBPRINTF ("Unable to init module dependencies\n");
743                 return -1;
744         }
745
746         // Register hooks (kprobe_handler)
747         __swap_register_undef_hook = swap_ksyms("register_undef_hook");
748         if (__swap_register_undef_hook == NULL) {
749                 printk("no register_undef_hook symbol found!\n");
750                 return -1;
751         }
752
753         // Unregister hooks (kprobe_handler)
754         __swap_unregister_undef_hook = swap_ksyms("unregister_undef_hook");
755         if (__swap_unregister_undef_hook == NULL) {
756                 printk("no unregister_undef_hook symbol found!\n");
757                 return -1;
758         }
759
760         swap_register_undef_hook(&undef_ho_k);
761         if ((ret = dbi_register_kprobe (&trampoline_p)) != 0) {
762                 //dbi_unregister_jprobe(&do_exit_p, 0);
763                 return ret;
764         }
765         return ret;
766 }
767
768 void __exit dbi_arch_exit_kprobes (void)
769 {
770         swap_unregister_undef_hook(&undef_ho_k);
771 }
772
773 //EXPORT_SYMBOL_GPL (dbi_arch_exit_kprobes);
774
775 /* export symbol for dbi_kprobes_arm.h */
776 EXPORT_SYMBOL_GPL(gen_insn_execbuf);
777 EXPORT_SYMBOL_GPL(pc_dep_insn_execbuf);