[REFACTOR] create uprobe_handler()
[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 unsigned int arr_traps_template[] = {
78                 0xe1a0c00d,    // mov          ip, sp
79                 0xe92dd800,    // stmdb        sp!, {fp, ip, lr, pc}
80                 0xe24cb004,    // sub          fp, ip, #4      ; 0x4
81                 0x00000000,    // b
82                 0xe3500000,    // cmp          r0, #0  ; 0x0
83                 0xe89da800,    // ldmia        sp, {fp, sp, pc}
84                 0x00000000,    // nop
85                 0xffffffff     // end
86 };
87
88
89 static struct kprobe trampoline_p =
90 {
91         .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
92         .pre_handler = trampoline_probe_handler
93 };
94
95 int prep_pc_dep_insn_execbuf(kprobe_opcode_t *insns, kprobe_opcode_t insn, int uregs)
96 {
97         int i;
98
99         if (uregs & 0x10)
100         {
101                 int reg_mask = 0x1;
102                 //search in reg list
103                 for (i = 0; i < 13; i++, reg_mask <<= 1)
104                 {
105                         if (!(insn & reg_mask))
106                                 break;
107                 }
108         }
109         else
110         {
111                 for (i = 0; i < 13; i++)
112                 {
113                         if ((uregs & 0x1) && (ARM_INSN_REG_RN (insn) == i))
114                                 continue;
115                         if ((uregs & 0x2) && (ARM_INSN_REG_RD (insn) == i))
116                                 continue;
117                         if ((uregs & 0x4) && (ARM_INSN_REG_RS (insn) == i))
118                                 continue;
119                         if ((uregs & 0x8) && (ARM_INSN_REG_RM (insn) == i))
120                                 continue;
121                         break;
122                 }
123         }
124         if (i == 13)
125         {
126                 DBPRINTF ("there are no free register %x in insn %lx!", uregs, insn);
127                 return -EINVAL;
128         }
129         DBPRINTF ("prep_pc_dep_insn_execbuf: using R%d, changing regs %x", i, uregs);
130
131         // set register to save
132         ARM_INSN_REG_SET_RD (insns[0], i);
133         // set register to load address to
134         ARM_INSN_REG_SET_RD (insns[1], i);
135         // set instruction to execute and patch it
136         if (uregs & 0x10)
137         {
138                 ARM_INSN_REG_CLEAR_MR (insn, 15);
139                 ARM_INSN_REG_SET_MR (insn, i);
140         }
141         else
142         {
143                 if ((uregs & 0x1) && (ARM_INSN_REG_RN (insn) == 15))
144                         ARM_INSN_REG_SET_RN (insn, i);
145                 if ((uregs & 0x2) && (ARM_INSN_REG_RD (insn) == 15))
146                         ARM_INSN_REG_SET_RD (insn, i);
147                 if ((uregs & 0x4) && (ARM_INSN_REG_RS (insn) == 15))
148                         ARM_INSN_REG_SET_RS (insn, i);
149                 if ((uregs & 0x8) && (ARM_INSN_REG_RM (insn) == 15))
150                         ARM_INSN_REG_SET_RM (insn, i);
151         }
152         insns[UPROBES_TRAMP_INSN_IDX] = insn;
153         // set register to restore
154         ARM_INSN_REG_SET_RD (insns[3], i);
155         return 0;
156 }
157 EXPORT_SYMBOL_GPL(prep_pc_dep_insn_execbuf);
158
159 int arch_check_insn_arm(struct arch_specific_insn *ainsn)
160 {
161         int ret = 0;
162
163         // check instructions that can change PC by nature
164         if (
165 //              ARM_INSN_MATCH (UNDEF, ainsn->insn_arm[0]) ||
166                 ARM_INSN_MATCH (AUNDEF, ainsn->insn_arm[0]) ||
167                 ARM_INSN_MATCH (SWI, ainsn->insn_arm[0]) ||
168                 ARM_INSN_MATCH (BREAK, ainsn->insn_arm[0]) ||
169                 ARM_INSN_MATCH (BL, ainsn->insn_arm[0]) ||
170                 ARM_INSN_MATCH (BLX1, ainsn->insn_arm[0]) ||
171                 ARM_INSN_MATCH (BLX2, ainsn->insn_arm[0]) ||
172                 ARM_INSN_MATCH (BX, ainsn->insn_arm[0]) ||
173                 ARM_INSN_MATCH (BXJ, ainsn->insn_arm[0]))
174         {
175                 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
176                 ret = -EFAULT;
177         }
178 #ifndef CONFIG_CPU_V7
179         // check instructions that can write result to PC
180         else if ((ARM_INSN_MATCH (DPIS, ainsn->insn_arm[0]) ||
181                                 ARM_INSN_MATCH (DPRS, ainsn->insn_arm[0]) ||
182                                 ARM_INSN_MATCH (DPI, ainsn->insn_arm[0]) ||
183                                 ARM_INSN_MATCH (LIO, ainsn->insn_arm[0]) ||
184                                 ARM_INSN_MATCH (LRO, ainsn->insn_arm[0])) &&
185                         (ARM_INSN_REG_RD (ainsn->insn_arm[0]) == 15))
186         {
187                 DBPRINTF ("Bad arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
188                 ret = -EFAULT;
189         }
190 #endif // CONFIG_CPU_V7
191         // check special instruction loads store multiple registers
192         else if ((ARM_INSN_MATCH (LM, ainsn->insn_arm[0]) || ARM_INSN_MATCH (SM, ainsn->insn_arm[0])) &&
193                         // store pc or load to pc
194                         (ARM_INSN_REG_MR (ainsn->insn_arm[0], 15) ||
195                          // store/load with pc update
196                          ((ARM_INSN_REG_RN (ainsn->insn_arm[0]) == 15) && (ainsn->insn_arm[0] & 0x200000))))
197         {
198                 DBPRINTF ("Bad insn arch_check_insn_arm: %lx\n", ainsn->insn_arm[0]);
199                 ret = -EFAULT;
200         }
201         return ret;
202 }
203 EXPORT_SYMBOL_GPL(arch_check_insn_arm);
204
205 int arch_prepare_kretprobe (struct kretprobe *p)
206 {
207         DBPRINTF("Warrning: arch_prepare_kretprobe is not implemented\n");
208         return 0;
209 }
210
211 int arch_prepare_kprobe (struct kprobe *p)
212 {
213         kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
214         int uregs, pc_dep, ret = 0;
215     kprobe_opcode_t insn[MAX_INSN_SIZE];
216     struct arch_specific_insn ainsn;
217
218     /* insn: must be on special executable page on i386. */
219     p->ainsn.insn = get_insn_slot(NULL, &kprobe_insn_pages, 0);
220     if (!p->ainsn.insn)
221         return -ENOMEM;
222
223     memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
224     ainsn.insn_arm = ainsn.insn = insn;
225     ret = arch_check_insn_arm (&ainsn);
226     if (!ret)
227     {
228         p->opcode = *p->addr;
229         uregs = pc_dep = 0;
230
231         // Rn, Rm ,Rd
232         if(ARM_INSN_MATCH (DPIS, insn[0]) || ARM_INSN_MATCH (LRO, insn[0]) ||
233            ARM_INSN_MATCH (SRO, insn[0]))
234         {
235             uregs = 0xb;
236             if( (ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
237                 (ARM_INSN_MATCH (SRO, insn[0]) && (ARM_INSN_REG_RD (insn[0]) == 15)) )
238             {
239                 DBPRINTF ("Unboostable insn %lx, DPIS/LRO/SRO\n", insn[0]);
240                 pc_dep = 1;
241             }
242         }
243         // Rn ,Rd
244         else if(ARM_INSN_MATCH (DPI, insn[0]) || ARM_INSN_MATCH (LIO, insn[0]) ||
245                 ARM_INSN_MATCH (SIO, insn[0]))
246         {
247             uregs = 0x3;
248             if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_MATCH (SIO, insn[0]) &&
249                         (ARM_INSN_REG_RD (insn[0]) == 15)))
250             {
251                 pc_dep = 1;
252                 DBPRINTF ("Unboostable insn %lx/%p, DPI/LIO/SIO\n", insn[0], p);
253             }
254         }
255         // Rn, Rm, Rs
256         else if(ARM_INSN_MATCH (DPRS, insn[0]))
257         {
258             uregs = 0xd;
259             if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
260                 (ARM_INSN_REG_RS (insn[0]) == 15))
261             {
262                 pc_dep = 1;
263                 DBPRINTF ("Unboostable insn %lx, DPRS\n", insn[0]);
264             }
265         }
266         // register list
267         else if(ARM_INSN_MATCH (SM, insn[0]))
268         {
269             uregs = 0x10;
270             if (ARM_INSN_REG_MR (insn[0], 15))
271             {
272                 DBPRINTF ("Unboostable insn %lx, SM\n", insn[0]);
273                 pc_dep = 1;
274             }
275         }
276         // check instructions that can write result to SP andu uses PC
277         if (pc_dep  && (ARM_INSN_REG_RD (ainsn.insn[0]) == 13))
278         {
279             free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
280             ret = -EFAULT;
281         }
282         else
283         {
284             if (uregs && pc_dep)
285             {
286                 memcpy (insns, pc_dep_insn_execbuf, sizeof (insns));
287                 if (prep_pc_dep_insn_execbuf (insns, insn[0], uregs) != 0)
288                 {
289                     DBPRINTF ("failed to prepare exec buffer for insn %lx!", insn[0]);
290                     free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
291                     return -EINVAL;
292                 }
293                 insns[6] = (kprobe_opcode_t) (p->addr + 2);
294             }
295             else
296             {
297                 memcpy (insns, gen_insn_execbuf, sizeof (insns));
298                 insns[KPROBES_TRAMP_INSN_IDX] = insn[0];
299             }
300             insns[7] = (kprobe_opcode_t) (p->addr + 1);
301             DBPRINTF ("arch_prepare_kprobe: insn %lx", insn[0]);
302             DBPRINTF ("arch_prepare_kprobe: to %p - %lx %lx %lx %lx %lx %lx %lx %lx %lx",
303                     p->ainsn.insn, insns[0], insns[1], insns[2], insns[3], insns[4],
304                     insns[5], insns[6], insns[7], insns[8]);
305             memcpy (p->ainsn.insn, insns, sizeof(insns));
306             flush_icache_range((long unsigned)p->ainsn.insn, (long unsigned)(p->ainsn.insn) + sizeof(insns));
307 #ifdef BOARD_tegra
308             flush_cache_all();
309 #endif
310         }
311     }
312     else
313     {
314         free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
315         printk("arch_prepare_kprobe: instruction 0x%lx not instrumentation, addr=0x%p\n", insn[0], p->addr);
316     }
317
318     return ret;
319 }
320
321 static unsigned int arch_construct_brunch (unsigned int base, unsigned int addr, int link)
322 {
323         kprobe_opcode_t insn;
324         unsigned int bpi = (unsigned int) base - (unsigned int) addr - 8;
325
326         insn = bpi >> 2;
327         DBPRINTF ("base=%x addr=%x base-addr-8=%x\n", base, addr, bpi);
328         if (abs (insn & 0xffffff) > 0xffffff)
329         {
330                 DBPRINTF ("ERROR: kprobe address out of range\n");
331                 BUG ();
332         }
333         insn = insn & 0xffffff;
334         insn = insn | ((link != 0) ? 0xeb000000 : 0xea000000);
335         DBPRINTF ("insn=%lX\n", insn);
336         return (unsigned int) insn;
337 }
338
339 int arch_prepare_uretprobe (struct kretprobe *p, struct task_struct *task)
340 {
341         DBPRINTF("Warrning: arch_prepare_uretprobe is not implemented\n");
342         return 0;
343 }
344 EXPORT_SYMBOL_GPL(arch_prepare_uretprobe);
345
346 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
347 {
348         if (p->ss_addr) {
349                 regs->ARM_pc = (unsigned long)p->ss_addr;
350                 p->ss_addr = NULL;
351         } else {
352                 regs->ARM_pc = (unsigned long)p->ainsn.insn;
353         }
354 }
355
356 void save_previous_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p_run)
357 {
358         if (p_run == NULL) {
359                 panic("arm_save_previous_kprobe: p_run == NULL\n");
360         }
361
362         if (kcb->prev_kprobe.kp != NULL) {
363                 DBPRINTF ("no space to save new probe[]: task = %d/%s", current->pid, current->comm);
364         }
365
366         kcb->prev_kprobe.kp = p_run;
367         kcb->prev_kprobe.status = kcb->kprobe_status;
368 }
369
370 void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
371 {
372         set_current_kprobe(kcb->prev_kprobe.kp, NULL, NULL);
373         kcb->kprobe_status = kcb->prev_kprobe.status;
374         kcb->prev_kprobe.kp = NULL;
375         kcb->prev_kprobe.status = 0;
376 }
377
378 void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
379 {
380         __get_cpu_var(current_kprobe) = p;
381         DBPRINTF ("set_current_kprobe: p=%p addr=%p\n", p, p->addr);
382 }
383 EXPORT_SYMBOL_GPL(set_current_kprobe);
384
385 #ifdef TRAP_OVERHEAD_DEBUG
386 static unsigned long trap_handler_counter_debug = 0;
387 #define SAMPLING_COUNTER                               100000
388 #endif
389
390 static int kprobe_handler(struct pt_regs *regs)
391 {
392         int err_out = 0;
393         char *msg_out = NULL;
394         kprobe_opcode_t *addr = (kprobe_opcode_t *) (regs->ARM_pc);
395
396         struct kprobe *p = NULL, *p_run = NULL;
397         int ret = 0, retprobe = 0, reenter = 0;
398         kprobe_opcode_t *ssaddr = NULL;
399         struct kprobe_ctlblk *kcb;
400
401 #ifdef SUPRESS_BUG_MESSAGES
402         int swap_oops_in_progress;
403         // oops_in_progress used to avoid BUG() messages that slow down kprobe_handler() execution
404         swap_oops_in_progress = oops_in_progress;
405         oops_in_progress = 1;
406 #endif
407 #ifdef TRAP_OVERHEAD_DEBUG
408         trap_handler_counter_debug++;
409         if ( trap_handler_counter_debug < SAMPLING_COUNTER ) {
410                 err_out = 0;
411         }
412         else {
413                 // XXX NOTE - user must care about catching signal via signal handler to avoid hanging!
414                 printk("Trap %ld reached - send SIGUSR1\n", trap_handler_counter_debug);
415                 kill_pid(get_task_pid(current, PIDTYPE_PID), SIGUSR1, 1);
416                 trap_handler_counter_debug = 0;
417                 err_out = 0;
418         }
419         return err_out;
420 #endif
421 #ifdef OVERHEAD_DEBUG
422         struct timeval swap_tv1;
423         struct timeval swap_tv2;
424 #define USEC_IN_SEC_NUM                         1000000
425         do_gettimeofday(&swap_tv1);
426 #endif
427         preempt_disable();
428
429         p = get_kprobe(addr, 0);
430
431         /* We're in an interrupt, but this is clear and BUG()-safe. */
432         kcb = get_kprobe_ctlblk ();
433
434         /* Check we're not actually recursing */
435         // TODO: event is not saving in trace
436         p_run = kprobe_running();
437         if (p_run)
438         {
439                 DBPRINTF("lock???");
440                 if (p)
441                 {
442                         if (addr == (kprobe_opcode_t *)kretprobe_trampoline) {
443                                 save_previous_kprobe(kcb, p_run);
444                                 kcb->kprobe_status = KPROBE_REENTER;
445                                 reenter = 1;
446                         } else {
447                                 /* We have reentered the kprobe_handler(), since
448                                  * another probe was hit while within the handler.
449                                  * We here save the original kprobes variables and
450                                  * just single step on the instruction of the new probe
451                                  * without calling any user handlers.
452                                  */
453                                 kprobes_inc_nmissed_count (p);
454                                 prepare_singlestep (p, regs);
455
456                                 err_out = 0;
457                                 goto out;
458                         }
459                 } else {
460                         if(!p) {
461                                 p = p_run;
462                                 DBPRINTF ("kprobe_running !!! p = 0x%p p->break_handler = 0x%p", p, p->break_handler);
463                                 /*if (p->break_handler && p->break_handler(p, regs)) {
464                                   DBPRINTF("kprobe_running !!! goto ss");
465                                   goto ss_probe;
466                                   } */
467                                 DBPRINTF ("unknown uprobe at %p cur at %p/%p\n", addr, p->addr, p->ainsn.insn);
468                                 ssaddr = p->ainsn.insn + KPROBES_TRAMP_SS_BREAK_IDX;
469                                 if (addr == ssaddr) {
470                                         regs->ARM_pc = (unsigned long) (p->addr + 1);
471                                         DBPRINTF ("finish step at %p cur at %p/%p, redirect to %lx\n", addr, p->addr, p->ainsn.insn, regs->ARM_pc);
472                                         if (kcb->kprobe_status == KPROBE_REENTER) {
473                                                 restore_previous_kprobe(kcb);
474                                         } else {
475                                                 reset_current_kprobe();
476                                         }
477                                 }
478                                 DBPRINTF ("kprobe_running !!! goto no");
479                                 ret = 1;
480                                 /* If it's not ours, can't be delete race, (we hold lock). */
481                                 DBPRINTF ("no_kprobe");
482                                 goto no_kprobe;
483                         }
484                 }
485         }
486
487         if (!p) {
488                 /* Not one of ours: let kernel handle it */
489                 DBPRINTF ("no_kprobe");
490                 goto no_kprobe;
491         }
492
493         set_current_kprobe(p, NULL, NULL);
494         if(!reenter)
495                 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
496         if (retprobe) {         //(einsn == UNDEF_INSTRUCTION)
497                 ret = trampoline_probe_handler (p, regs);
498         } else if (p->pre_handler) {
499                 ret = p->pre_handler (p, regs);
500                 if(p->pre_handler != trampoline_probe_handler) {
501                         reset_current_kprobe();
502                 }
503         }
504
505         if (ret) {
506                 /* handler has already set things up, so skip ss setup */
507                 err_out = 0;
508                 goto out;
509         }
510
511 no_kprobe:
512         msg_out = "no_kprobe\n";
513         err_out = 1;            // return with death
514         goto out;
515
516 out:
517         preempt_enable_no_resched();
518 #ifdef OVERHEAD_DEBUG
519         do_gettimeofday(&swap_tv2);
520         swap_sum_hit++;
521         swap_sum_time += ((swap_tv2.tv_sec - swap_tv1.tv_sec) *  USEC_IN_SEC_NUM +
522                 (swap_tv2.tv_usec - swap_tv1.tv_usec));
523 #endif
524 #ifdef SUPRESS_BUG_MESSAGES
525         oops_in_progress = swap_oops_in_progress;
526 #endif
527
528         if(msg_out) {
529                 printk(msg_out);
530         }
531
532         return err_out;
533 }
534
535 int kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
536 {
537         int ret;
538         unsigned long flags;
539         local_irq_save(flags);
540         ret = kprobe_handler(regs);
541         local_irq_restore(flags);
542         return ret;
543 }
544
545 int setjmp_pre_handler (struct kprobe *p, struct pt_regs *regs)
546 {
547         struct jprobe *jp = container_of (p, struct jprobe, kp);
548         kprobe_pre_entry_handler_t pre_entry;
549         entry_point_t entry;
550
551 # ifdef REENTER
552 //      p = kprobe_running(regs);
553 # endif
554
555         DBPRINTF ("pjp = 0x%p jp->entry = 0x%p", jp, jp->entry);
556         entry = (entry_point_t) jp->entry;
557         pre_entry = (kprobe_pre_entry_handler_t) jp->pre_entry;
558         //if(!entry)
559         //      DIE("entry NULL", regs)
560         DBPRINTF ("entry = 0x%p jp->entry = 0x%p", entry, jp->entry);
561
562         //call handler for all kernel probes and user space ones which belong to current tgid
563         if (!p->tgid || (p->tgid == current->tgid))
564         {
565                 if(!p->tgid && ((unsigned int)p->addr == sched_addr) && sched_rp) {
566                         struct thread_info *tinfo = (struct thread_info *)regs->ARM_r2;
567                         patch_suspended_task(sched_rp, tinfo->task);
568                 }
569                 if (pre_entry)
570                         p->ss_addr = (void *)pre_entry (jp->priv_arg, regs);
571                 if (entry){
572                         entry (regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3, regs->ARM_r4, regs->ARM_r5);
573                 }
574                 else {
575                         if (p->tgid)
576                                 dbi_arch_uprobe_return ();
577                         else
578                                 dbi_jprobe_return ();
579                 }
580         }
581         else if (p->tgid)
582                 dbi_arch_uprobe_return ();
583
584         prepare_singlestep (p, regs);
585
586         return 1;
587 }
588 EXPORT_SYMBOL_GPL(setjmp_pre_handler);
589
590 void dbi_jprobe_return (void)
591 {
592 }
593
594 void dbi_arch_uprobe_return (void)
595 {
596 }
597 EXPORT_SYMBOL_GPL(dbi_arch_uprobe_return);
598
599 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
600 {
601 # ifndef REENTER
602         //kprobe_opcode_t insn = BREAKPOINT_INSTRUCTION;
603         kprobe_opcode_t insns[2];
604
605         if (p->pid)
606         {
607                 insns[0] = BREAKPOINT_INSTRUCTION;
608                 insns[1] = p->opcode;
609                 //p->opcode = *p->addr;
610                 if (read_proc_vm_atomic (current, (unsigned long) (p->addr), &(p->opcode), sizeof (p->opcode)) < sizeof (p->opcode))
611                 {
612                         printk ("ERROR[%lu]: failed to read vm of proc %s/%u addr %p.", nCount, current->comm, current->pid, p->addr);
613                         return -1;
614                 }
615                 //*p->addr = BREAKPOINT_INSTRUCTION;
616                 //*(p->addr+1) = p->opcode;
617                 if (write_proc_vm_atomic (current, (unsigned long) (p->addr), insns, sizeof (insns)) < sizeof (insns))
618                 {
619                         printk ("ERROR[%lu]: failed to write vm of proc %s/%u addr %p.", nCount, current->comm, current->pid, p->addr);
620                         return -1;
621                 }
622         }
623         else
624         {
625                 DBPRINTF ("p->opcode = 0x%lx *p->addr = 0x%lx p->addr = 0x%p\n", p->opcode, *p->addr, p->addr);
626                 *(p->addr + 1) = p->opcode;
627                 p->opcode = *p->addr;
628                 *p->addr = BREAKPOINT_INSTRUCTION;
629
630                 flush_icache_range ((unsigned int) p->addr, (unsigned int) (((unsigned int) p->addr) + (sizeof (kprobe_opcode_t) * 2)));
631         }
632
633         reset_current_kprobe();
634
635 #endif //REENTER
636
637         return 0;
638 }
639 EXPORT_SYMBOL_GPL(longjmp_break_handler);
640
641 void arch_arm_kprobe (struct kprobe *p)
642 {
643         *p->addr = BREAKPOINT_INSTRUCTION;
644         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
645 }
646
647 void arch_disarm_kprobe (struct kprobe *p)
648 {
649         *p->addr = p->opcode;
650         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
651 }
652
653
654 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
655 {
656         struct kretprobe_instance *ri = NULL;
657         struct hlist_head *head;
658         struct hlist_node *node, *tmp;
659         unsigned long flags, orig_ret_address = 0;
660         unsigned long trampoline_address = (unsigned long) &kretprobe_trampoline;
661
662         struct kretprobe *crp = NULL;
663         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
664
665         DBPRINTF ("start");
666
667         if (p && p->tgid){
668                 // in case of user space retprobe trampoline is at the Nth instruction of US tramp
669                 if (!thumb_mode( regs ))
670                         trampoline_address = (unsigned long)(p->ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
671                 else
672                         trampoline_address = (unsigned long)(p->ainsn.insn) + 0x1b;
673         }
674
675         spin_lock_irqsave (&kretprobe_lock, flags);
676
677         /*
678          * We are using different hash keys (current and mm) for finding kernel
679          * space and user space probes.  Kernel space probes can change mm field in
680          * task_struct.  User space probes can be shared between threads of one
681          * process so they have different current but same mm.
682          */
683         if (p && p->tgid) {
684                 head = kretprobe_inst_table_head(current->mm);
685         } else {
686                 head = kretprobe_inst_table_head(current);
687         }
688
689         /*
690          * It is possible to have multiple instances associated with a given
691          * task either because an multiple functions in the call path
692          * have a return probe installed on them, and/or more then one
693          * return probe was registered for a target function.
694          *
695          * We can handle this because:
696          *     - instances are always inserted at the head of the list
697          *     - when multiple return probes are registered for the same
698          *       function, the first instance's ret_addr will point to the
699          *       real return address, and all the rest will point to
700          *       kretprobe_trampoline
701          */
702         hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
703         {
704                 if (ri->task != current)
705                         /* another task is sharing our hash bucket */
706                         continue;
707                 if (ri->rp && ri->rp->handler){
708                         ri->rp->handler (ri, regs, ri->rp->priv_arg);
709                 }
710
711                 orig_ret_address = (unsigned long) ri->ret_addr;
712                 recycle_rp_inst (ri);
713                 if (orig_ret_address != trampoline_address)
714                         /*
715                          * This is the real return address. Any other
716                          * instances associated with this task are for
717                          * other calls deeper on the call stack
718                          */
719                         break;
720         }
721         kretprobe_assert (ri, orig_ret_address, trampoline_address);
722         //BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
723         //E.G. Check this code in case of __switch_to function instrumentation -- currently this code generates dump in this case
724         //if (trampoline_address != (unsigned long) &kretprobe_trampoline){
725         //if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
726         //if (ri->rp) BUG_ON (ri->rp->kp.tgid == 0);
727         //else if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
728         //}
729         if ((ri->rp && ri->rp->kp.tgid) || (ri->rp2 && ri->rp2->kp.tgid))
730                 BUG_ON (trampoline_address == (unsigned long) &kretprobe_trampoline);
731
732         regs->uregs[14] = orig_ret_address;
733         DBPRINTF ("regs->uregs[14] = 0x%lx\n", regs->uregs[14]);
734         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
735
736         if (trampoline_address != (unsigned long) &kretprobe_trampoline)
737         {
738                 regs->uregs[15] = orig_ret_address;
739         }else{
740                 if (!thumb_mode( regs )) regs->uregs[15] += 4;
741                 else regs->uregs[15] += 2;
742         }
743
744         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
745
746         if(p){ // ARM, MIPS, X86 user space
747                 if (thumb_mode( regs ) && !(regs->uregs[14] & 0x01))
748                 {
749                         regs->ARM_cpsr &= 0xFFFFFFDF;
750                 }else{
751                         if (user_mode( regs ) && (regs->uregs[14] & 0x01))
752                         {
753                                 regs->ARM_cpsr |= 0x20;
754                         }
755                 }
756
757                 //TODO: test - enter function, delete us retprobe, exit function
758                 // for user space retprobes only - deferred deletion
759
760                 if (trampoline_address != (unsigned long) &kretprobe_trampoline)
761                 {
762                         // if we are not at the end of the list and current retprobe should be disarmed
763                         if (node && ri->rp2)
764                         {
765                                 struct hlist_node *current_node = node;
766                                 crp = ri->rp2;
767                                 /*sprintf(die_msg, "deferred disarm p->addr = %p [%lx %lx %lx]\n",
768                                   crp->kp.addr, *kaddrs[0], *kaddrs[1], *kaddrs[2]);
769                                   DIE(die_msg, regs); */
770                                 // look for other instances for the same retprobe
771                                 hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
772                                 {
773                                         /*
774                                          * Trying to find another retprobe instance associated with
775                                          * the same retprobe.
776                                          */
777                                         if (ri->rp2 == crp && node != current_node)
778                                                 break;
779                                 }
780
781                                 if (!node)
782                                 {
783                                         // if there are no more instances for this retprobe
784                                         // delete retprobe
785                                         struct kprobe *is_p = &crp->kp;
786                                         DBPRINTF ("defered retprobe deletion p->addr = %p", crp->kp.addr);
787                                         /*
788                                           If there is no any retprobe instances of this retprobe
789                                           we can free the resources related to the probe.
790                                          */
791                                         if (!(hlist_unhashed(&is_p->is_hlist_arm))) {
792                                                 hlist_del_rcu(&is_p->is_hlist_arm);
793                                         }
794                                         if (!(hlist_unhashed(&is_p->is_hlist_thumb))) {
795                                                 hlist_del_rcu(&is_p->is_hlist_thumb);
796                                         }
797
798                                         dbi_unregister_kprobe(&crp->kp, current);
799                                         kfree (crp);
800                                 }
801                                 hlist_del(current_node);
802                         }
803                 }
804
805                 if (kcb->kprobe_status == KPROBE_REENTER) {
806                         restore_previous_kprobe(kcb);
807                 } else {
808                         reset_current_kprobe();
809                 }
810         }
811
812         spin_unlock_irqrestore (&kretprobe_lock, flags);
813
814         /*
815          * By returning a non-zero value, we are telling
816          * kprobe_handler() that we don't want the post_handler
817          * to run (and have re-enabled preemption)
818          */
819
820         return 1;
821 }
822 EXPORT_SYMBOL_GPL(trampoline_probe_handler);
823
824 void  __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
825 {
826         struct kretprobe_instance *ri;
827
828         DBPRINTF ("start\n");
829         //TODO: test - remove retprobe after func entry but before its exit
830         if ((ri = get_free_rp_inst (rp)) != NULL)
831         {
832                 ri->rp = rp;
833                 ri->rp2 = NULL;
834                 ri->task = current;
835                 ri->ret_addr = (kprobe_opcode_t *) regs->uregs[14];
836                 ri->sp = (kprobe_opcode_t *)regs->ARM_sp; //uregs[13];
837
838                 if (rp->kp.tgid)
839                         if (!thumb_mode( regs ))
840                                 regs->uregs[14] = (unsigned long) (rp->kp.ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
841                         else
842                                 regs->uregs[14] = (unsigned long) (rp->kp.ainsn.insn) + 0x1b;
843
844                 else    /* Replace the return addr with trampoline addr */
845                         regs->uregs[14] = (unsigned long) &kretprobe_trampoline;
846
847 //              DBPRINTF ("ret addr set to %p->%lx\n", ri->ret_addr, regs->uregs[14]);
848                 add_rp_inst (ri);
849         }
850         else {
851                 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
852                 rp->nmissed++;
853         }
854 }
855
856
857 int asm_init_module_dependencies(void)
858 {
859         //No module dependencies
860         return 0;
861 }
862
863 void swap_register_undef_hook(struct undef_hook *hook)
864 {
865         __swap_register_undef_hook(hook);
866 }
867 EXPORT_SYMBOL_GPL(swap_register_undef_hook);
868
869 void swap_unregister_undef_hook(struct undef_hook *hook)
870 {
871         __swap_unregister_undef_hook(hook);
872 }
873 EXPORT_SYMBOL_GPL(swap_unregister_undef_hook);
874
875 // kernel probes hook
876 static struct undef_hook undef_ho_k = {
877     .instr_mask = 0xffffffff,
878     .instr_val  = BREAKPOINT_INSTRUCTION,
879     .cpsr_mask  = MODE_MASK,
880     .cpsr_val   = SVC_MODE,
881     .fn         = kprobe_trap_handler
882 };
883
884 int __init arch_init_kprobes (void)
885 {
886         unsigned int do_bp_handler = 0;
887         int ret = 0;
888
889         if (arch_init_module_dependencies())
890         {
891                 DBPRINTF ("Unable to init module dependencies\n");
892                 return -1;
893         }
894
895         do_bp_handler = swap_ksyms("do_undefinstr");
896         if (do_bp_handler == 0) {
897                 DBPRINTF("no do_undefinstr symbol found!");
898                 return -1;
899         }
900         arr_traps_template[NOTIFIER_CALL_CHAIN_INDEX] = arch_construct_brunch ((unsigned int)kprobe_handler, do_bp_handler + NOTIFIER_CALL_CHAIN_INDEX * 4, 1);
901         // Register hooks (kprobe_handler)
902         __swap_register_undef_hook = swap_ksyms("register_undef_hook");
903         if (__swap_register_undef_hook == NULL) {
904                 printk("no register_undef_hook symbol found!\n");
905                 return -1;
906         }
907
908         // Unregister hooks (kprobe_handler)
909         __swap_unregister_undef_hook = swap_ksyms("unregister_undef_hook");
910         if (__swap_unregister_undef_hook == NULL) {
911                 printk("no unregister_undef_hook symbol found!\n");
912                 return -1;
913         }
914
915         swap_register_undef_hook(&undef_ho_k);
916         if ((ret = dbi_register_kprobe (&trampoline_p)) != 0) {
917                 //dbi_unregister_jprobe(&do_exit_p, 0);
918                 return ret;
919         }
920         return ret;
921 }
922
923 void __exit dbi_arch_exit_kprobes (void)
924 {
925         swap_unregister_undef_hook(&undef_ho_k);
926 }
927
928 //EXPORT_SYMBOL_GPL (dbi_arch_uprobe_return);
929 //EXPORT_SYMBOL_GPL (dbi_arch_exit_kprobes);