[REFACTORE] rename dbi_arch_exit_kprobes()
[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         return 0;
449 }
450 EXPORT_SYMBOL_GPL(longjmp_break_handler);
451
452 void arch_arm_kprobe (struct kprobe *p)
453 {
454         *p->addr = BREAKPOINT_INSTRUCTION;
455         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
456 }
457
458 void arch_disarm_kprobe (struct kprobe *p)
459 {
460         *p->addr = p->opcode;
461         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
462 }
463
464
465 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
466 {
467         struct kretprobe_instance *ri = NULL;
468         struct hlist_head *head;
469         struct hlist_node *node, *tmp;
470         unsigned long flags, orig_ret_address = 0;
471         unsigned long trampoline_address = (unsigned long) &kretprobe_trampoline;
472
473         struct kretprobe *crp = NULL;
474         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
475
476         DBPRINTF ("start");
477
478         spin_lock_irqsave (&kretprobe_lock, flags);
479
480         /*
481          * We are using different hash keys (current and mm) for finding kernel
482          * space and user space probes.  Kernel space probes can change mm field in
483          * task_struct.  User space probes can be shared between threads of one
484          * process so they have different current but same mm.
485          */
486         head = kretprobe_inst_table_head(current);
487
488         /*
489          * It is possible to have multiple instances associated with a given
490          * task either because an multiple functions in the call path
491          * have a return probe installed on them, and/or more then one
492          * return probe was registered for a target function.
493          *
494          * We can handle this because:
495          *     - instances are always inserted at the head of the list
496          *     - when multiple return probes are registered for the same
497          *       function, the first instance's ret_addr will point to the
498          *       real return address, and all the rest will point to
499          *       kretprobe_trampoline
500          */
501         hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
502         {
503                 if (ri->task != current)
504                         /* another task is sharing our hash bucket */
505                         continue;
506                 if (ri->rp && ri->rp->handler){
507                         ri->rp->handler (ri, regs, ri->rp->priv_arg);
508                 }
509
510                 orig_ret_address = (unsigned long) ri->ret_addr;
511                 recycle_rp_inst (ri);
512                 if (orig_ret_address != trampoline_address)
513                         /*
514                          * This is the real return address. Any other
515                          * instances associated with this task are for
516                          * other calls deeper on the call stack
517                          */
518                         break;
519         }
520         kretprobe_assert (ri, orig_ret_address, trampoline_address);
521
522         regs->uregs[14] = orig_ret_address;
523         DBPRINTF ("regs->uregs[14] = 0x%lx\n", regs->uregs[14]);
524         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
525
526         if (trampoline_address != (unsigned long) &kretprobe_trampoline)
527         {
528                 regs->uregs[15] = orig_ret_address;
529         }else{
530                 if (!thumb_mode( regs )) regs->uregs[15] += 4;
531                 else regs->uregs[15] += 2;
532         }
533
534         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
535
536         if(p){ // ARM, MIPS, X86 user space
537                 if (thumb_mode( regs ) && !(regs->uregs[14] & 0x01))
538                 {
539                         regs->ARM_cpsr &= 0xFFFFFFDF;
540                 }else{
541                         if (user_mode( regs ) && (regs->uregs[14] & 0x01))
542                         {
543                                 regs->ARM_cpsr |= 0x20;
544                         }
545                 }
546
547                 if (kcb->kprobe_status == KPROBE_REENTER) {
548                         restore_previous_kprobe(kcb);
549                 } else {
550                         reset_current_kprobe();
551                 }
552         }
553
554         spin_unlock_irqrestore (&kretprobe_lock, flags);
555
556         /*
557          * By returning a non-zero value, we are telling
558          * kprobe_handler() that we don't want the post_handler
559          * to run (and have re-enabled preemption)
560          */
561
562         return 1;
563 }
564 EXPORT_SYMBOL_GPL(trampoline_probe_handler);
565
566 void  __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
567 {
568         struct kretprobe_instance *ri;
569
570         DBPRINTF ("start\n");
571         //TODO: test - remove retprobe after func entry but before its exit
572         if ((ri = get_free_rp_inst (rp)) != NULL)
573         {
574                 ri->rp = rp;
575                 ri->task = current;
576                 ri->ret_addr = (kprobe_opcode_t *) regs->uregs[14];
577                 ri->sp = (kprobe_opcode_t *)regs->ARM_sp; //uregs[13];
578
579                 /* Set flag of current mode */
580                 ri->sp = (kprobe_opcode_t *)((long)ri->sp | !!thumb_mode(regs));
581
582                 /* Replace the return addr with trampoline addr */
583                 regs->uregs[14] = (unsigned long) &kretprobe_trampoline;
584
585 //              DBPRINTF ("ret addr set to %p->%lx\n", ri->ret_addr, regs->uregs[14]);
586                 add_rp_inst (ri);
587         }
588         else {
589                 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
590                 rp->nmissed++;
591         }
592 }
593
594 void swap_register_undef_hook(struct undef_hook *hook)
595 {
596         __swap_register_undef_hook(hook);
597 }
598 EXPORT_SYMBOL_GPL(swap_register_undef_hook);
599
600 void swap_unregister_undef_hook(struct undef_hook *hook)
601 {
602         __swap_unregister_undef_hook(hook);
603 }
604 EXPORT_SYMBOL_GPL(swap_unregister_undef_hook);
605
606 // kernel probes hook
607 static struct undef_hook undef_ho_k = {
608     .instr_mask = 0xffffffff,
609     .instr_val  = BREAKPOINT_INSTRUCTION,
610     .cpsr_mask  = MODE_MASK,
611     .cpsr_val   = SVC_MODE,
612     .fn         = kprobe_trap_handler
613 };
614
615 int arch_init_kprobes(void)
616 {
617         int ret = 0;
618
619         // Register hooks (kprobe_handler)
620         __swap_register_undef_hook = swap_ksyms("register_undef_hook");
621         if (__swap_register_undef_hook == NULL) {
622                 printk("no register_undef_hook symbol found!\n");
623                 return -1;
624         }
625
626         // Unregister hooks (kprobe_handler)
627         __swap_unregister_undef_hook = swap_ksyms("unregister_undef_hook");
628         if (__swap_unregister_undef_hook == NULL) {
629                 printk("no unregister_undef_hook symbol found!\n");
630                 return -1;
631         }
632
633         swap_register_undef_hook(&undef_ho_k);
634         if ((ret = dbi_register_kprobe (&trampoline_p)) != 0) {
635                 //dbi_unregister_jprobe(&do_exit_p, 0);
636                 return ret;
637         }
638         return ret;
639 }
640
641 void arch_exit_kprobes(void)
642 {
643         swap_unregister_undef_hook(&undef_ho_k);
644 }
645
646 /* export symbol for trampoline_arm.h */
647 EXPORT_SYMBOL_GPL(gen_insn_execbuf);
648 EXPORT_SYMBOL_GPL(pc_dep_insn_execbuf);