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