[REFACTOR] remove field 'tgid' from 'struct kprobe'
[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 "trampoline_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((void *)regs->ARM_pc);
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 (((unsigned long)p->addr == sched_addr) && sched_rp) {
424                 struct thread_info *tinfo = (struct thread_info *)regs->ARM_r2;
425                 patch_suspended_task(sched_rp, tinfo->task);
426         }
427
428         if (pre_entry) {
429                 p->ss_addr = (void *)pre_entry (jp->priv_arg, regs);
430         }
431
432         if (entry) {
433                 entry(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2,
434                       regs->ARM_r3, regs->ARM_r4, regs->ARM_r5);
435         } else {
436                 dbi_jprobe_return();
437         }
438
439         return 0;
440 }
441
442 void dbi_jprobe_return (void)
443 {
444 }
445
446 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
447 {
448 # ifndef REENTER
449         //kprobe_opcode_t insn = BREAKPOINT_INSTRUCTION;
450         kprobe_opcode_t insns[2];
451
452         if (p->pid)
453         {
454                 insns[0] = BREAKPOINT_INSTRUCTION;
455                 insns[1] = p->opcode;
456                 //p->opcode = *p->addr;
457                 if (read_proc_vm_atomic (current, (unsigned long) (p->addr), &(p->opcode), sizeof (p->opcode)) < sizeof (p->opcode))
458                 {
459                         printk ("ERROR[%lu]: failed to read vm of proc %s/%u addr %p.", nCount, current->comm, current->pid, p->addr);
460                         return -1;
461                 }
462                 //*p->addr = BREAKPOINT_INSTRUCTION;
463                 //*(p->addr+1) = p->opcode;
464                 if (write_proc_vm_atomic (current, (unsigned long) (p->addr), insns, sizeof (insns)) < sizeof (insns))
465                 {
466                         printk ("ERROR[%lu]: failed to write vm of proc %s/%u addr %p.", nCount, current->comm, current->pid, p->addr);
467                         return -1;
468                 }
469         }
470         else
471         {
472                 DBPRINTF ("p->opcode = 0x%lx *p->addr = 0x%lx p->addr = 0x%p\n", p->opcode, *p->addr, p->addr);
473                 *(p->addr + 1) = p->opcode;
474                 p->opcode = *p->addr;
475                 *p->addr = BREAKPOINT_INSTRUCTION;
476
477                 flush_icache_range ((unsigned int) p->addr, (unsigned int) (((unsigned int) p->addr) + (sizeof (kprobe_opcode_t) * 2)));
478         }
479
480         reset_current_kprobe();
481
482 #endif //REENTER
483
484         return 0;
485 }
486 EXPORT_SYMBOL_GPL(longjmp_break_handler);
487
488 void arch_arm_kprobe (struct kprobe *p)
489 {
490         *p->addr = BREAKPOINT_INSTRUCTION;
491         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
492 }
493
494 void arch_disarm_kprobe (struct kprobe *p)
495 {
496         *p->addr = p->opcode;
497         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
498 }
499
500
501 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
502 {
503         struct kretprobe_instance *ri = NULL;
504         struct hlist_head *head;
505         struct hlist_node *node, *tmp;
506         unsigned long flags, orig_ret_address = 0;
507         unsigned long trampoline_address = (unsigned long) &kretprobe_trampoline;
508
509         struct kretprobe *crp = NULL;
510         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
511
512         DBPRINTF ("start");
513
514         spin_lock_irqsave (&kretprobe_lock, flags);
515
516         /*
517          * We are using different hash keys (current and mm) for finding kernel
518          * space and user space probes.  Kernel space probes can change mm field in
519          * task_struct.  User space probes can be shared between threads of one
520          * process so they have different current but same mm.
521          */
522         head = kretprobe_inst_table_head(current);
523
524         /*
525          * It is possible to have multiple instances associated with a given
526          * task either because an multiple functions in the call path
527          * have a return probe installed on them, and/or more then one
528          * return probe was registered for a target function.
529          *
530          * We can handle this because:
531          *     - instances are always inserted at the head of the list
532          *     - when multiple return probes are registered for the same
533          *       function, the first instance's ret_addr will point to the
534          *       real return address, and all the rest will point to
535          *       kretprobe_trampoline
536          */
537         hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
538         {
539                 if (ri->task != current)
540                         /* another task is sharing our hash bucket */
541                         continue;
542                 if (ri->rp && ri->rp->handler){
543                         ri->rp->handler (ri, regs, ri->rp->priv_arg);
544                 }
545
546                 orig_ret_address = (unsigned long) ri->ret_addr;
547                 recycle_rp_inst (ri);
548                 if (orig_ret_address != trampoline_address)
549                         /*
550                          * This is the real return address. Any other
551                          * instances associated with this task are for
552                          * other calls deeper on the call stack
553                          */
554                         break;
555         }
556         kretprobe_assert (ri, orig_ret_address, trampoline_address);
557         //BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
558         //E.G. Check this code in case of __switch_to function instrumentation -- currently this code generates dump in this case
559         //if (trampoline_address != (unsigned long) &kretprobe_trampoline){
560         //if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
561         //if (ri->rp) BUG_ON (ri->rp->kp.tgid == 0);
562         //else if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
563         //}
564
565         regs->uregs[14] = orig_ret_address;
566         DBPRINTF ("regs->uregs[14] = 0x%lx\n", regs->uregs[14]);
567         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
568
569         if (trampoline_address != (unsigned long) &kretprobe_trampoline)
570         {
571                 regs->uregs[15] = orig_ret_address;
572         }else{
573                 if (!thumb_mode( regs )) regs->uregs[15] += 4;
574                 else regs->uregs[15] += 2;
575         }
576
577         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
578
579         if(p){ // ARM, MIPS, X86 user space
580                 if (thumb_mode( regs ) && !(regs->uregs[14] & 0x01))
581                 {
582                         regs->ARM_cpsr &= 0xFFFFFFDF;
583                 }else{
584                         if (user_mode( regs ) && (regs->uregs[14] & 0x01))
585                         {
586                                 regs->ARM_cpsr |= 0x20;
587                         }
588                 }
589
590                 //TODO: test - enter function, delete us retprobe, exit function
591                 // for user space retprobes only - deferred deletion
592
593                 if (trampoline_address != (unsigned long) &kretprobe_trampoline)
594                 {
595                         // if we are not at the end of the list and current retprobe should be disarmed
596                         if (node && ri->rp2)
597                         {
598                                 struct hlist_node *current_node = node;
599                                 crp = ri->rp2;
600                                 /*sprintf(die_msg, "deferred disarm p->addr = %p [%lx %lx %lx]\n",
601                                   crp->kp.addr, *kaddrs[0], *kaddrs[1], *kaddrs[2]);
602                                   DIE(die_msg, regs); */
603                                 // look for other instances for the same retprobe
604                                 hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
605                                 {
606                                         /*
607                                          * Trying to find another retprobe instance associated with
608                                          * the same retprobe.
609                                          */
610                                         if (ri->rp2 == crp && node != current_node)
611                                                 break;
612                                 }
613
614                                 if (!node)
615                                 {
616                                         // if there are no more instances for this retprobe
617                                         // delete retprobe
618                                         struct kprobe *is_p = &crp->kp;
619                                         DBPRINTF ("defered retprobe deletion p->addr = %p", crp->kp.addr);
620                                         /*
621                                           If there is no any retprobe instances of this retprobe
622                                           we can free the resources related to the probe.
623                                          */
624                                         if (!(hlist_unhashed(&is_p->is_hlist_arm))) {
625                                                 hlist_del_rcu(&is_p->is_hlist_arm);
626                                         }
627                                         if (!(hlist_unhashed(&is_p->is_hlist_thumb))) {
628                                                 hlist_del_rcu(&is_p->is_hlist_thumb);
629                                         }
630
631                                         dbi_unregister_kprobe(&crp->kp, current);
632                                         kfree (crp);
633                                 }
634                                 hlist_del(current_node);
635                         }
636                 }
637
638                 if (kcb->kprobe_status == KPROBE_REENTER) {
639                         restore_previous_kprobe(kcb);
640                 } else {
641                         reset_current_kprobe();
642                 }
643         }
644
645         spin_unlock_irqrestore (&kretprobe_lock, flags);
646
647         /*
648          * By returning a non-zero value, we are telling
649          * kprobe_handler() that we don't want the post_handler
650          * to run (and have re-enabled preemption)
651          */
652
653         return 1;
654 }
655 EXPORT_SYMBOL_GPL(trampoline_probe_handler);
656
657 void  __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
658 {
659         struct kretprobe_instance *ri;
660
661         DBPRINTF ("start\n");
662         //TODO: test - remove retprobe after func entry but before its exit
663         if ((ri = get_free_rp_inst (rp)) != NULL)
664         {
665                 ri->rp = rp;
666                 ri->rp2 = NULL;
667                 ri->task = current;
668                 ri->ret_addr = (kprobe_opcode_t *) regs->uregs[14];
669                 ri->sp = (kprobe_opcode_t *)regs->ARM_sp; //uregs[13];
670
671                 /* Set flag of current mode */
672                 ri->sp = (kprobe_opcode_t *)((long)ri->sp | !!thumb_mode(regs));
673
674                 /* Replace the return addr with trampoline addr */
675                 regs->uregs[14] = (unsigned long) &kretprobe_trampoline;
676
677 //              DBPRINTF ("ret addr set to %p->%lx\n", ri->ret_addr, regs->uregs[14]);
678                 add_rp_inst (ri);
679         }
680         else {
681                 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
682                 rp->nmissed++;
683         }
684 }
685
686
687 int asm_init_module_dependencies(void)
688 {
689         //No module dependencies
690         return 0;
691 }
692
693 void swap_register_undef_hook(struct undef_hook *hook)
694 {
695         __swap_register_undef_hook(hook);
696 }
697 EXPORT_SYMBOL_GPL(swap_register_undef_hook);
698
699 void swap_unregister_undef_hook(struct undef_hook *hook)
700 {
701         __swap_unregister_undef_hook(hook);
702 }
703 EXPORT_SYMBOL_GPL(swap_unregister_undef_hook);
704
705 // kernel probes hook
706 static struct undef_hook undef_ho_k = {
707     .instr_mask = 0xffffffff,
708     .instr_val  = BREAKPOINT_INSTRUCTION,
709     .cpsr_mask  = MODE_MASK,
710     .cpsr_val   = SVC_MODE,
711     .fn         = kprobe_trap_handler
712 };
713
714 int __init arch_init_kprobes (void)
715 {
716         int ret = 0;
717
718         if (arch_init_module_dependencies())
719         {
720                 DBPRINTF ("Unable to init module dependencies\n");
721                 return -1;
722         }
723
724         // Register hooks (kprobe_handler)
725         __swap_register_undef_hook = swap_ksyms("register_undef_hook");
726         if (__swap_register_undef_hook == NULL) {
727                 printk("no register_undef_hook symbol found!\n");
728                 return -1;
729         }
730
731         // Unregister hooks (kprobe_handler)
732         __swap_unregister_undef_hook = swap_ksyms("unregister_undef_hook");
733         if (__swap_unregister_undef_hook == NULL) {
734                 printk("no unregister_undef_hook symbol found!\n");
735                 return -1;
736         }
737
738         swap_register_undef_hook(&undef_ho_k);
739         if ((ret = dbi_register_kprobe (&trampoline_p)) != 0) {
740                 //dbi_unregister_jprobe(&do_exit_p, 0);
741                 return ret;
742         }
743         return ret;
744 }
745
746 void __exit dbi_arch_exit_kprobes (void)
747 {
748         swap_unregister_undef_hook(&undef_ho_k);
749 }
750
751 //EXPORT_SYMBOL_GPL (dbi_arch_exit_kprobes);
752
753 /* export symbol for trampoline_arm.h */
754 EXPORT_SYMBOL_GPL(gen_insn_execbuf);
755 EXPORT_SYMBOL_GPL(pc_dep_insn_execbuf);