[REFACTOR] remove arch_prepare_kretprobe()
[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_kprobe (struct kprobe *p)
193 {
194         kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
195         int uregs, pc_dep, ret = 0;
196     kprobe_opcode_t insn[MAX_INSN_SIZE];
197     struct arch_specific_insn ainsn;
198
199     /* insn: must be on special executable page on i386. */
200     p->ainsn.insn = get_insn_slot(NULL, &kprobe_insn_pages, 0);
201     if (!p->ainsn.insn)
202         return -ENOMEM;
203
204     memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
205     ainsn.insn_arm = ainsn.insn = insn;
206     ret = arch_check_insn_arm (&ainsn);
207     if (!ret)
208     {
209         p->opcode = *p->addr;
210         uregs = pc_dep = 0;
211
212         // Rn, Rm ,Rd
213         if(ARM_INSN_MATCH (DPIS, insn[0]) || ARM_INSN_MATCH (LRO, insn[0]) ||
214            ARM_INSN_MATCH (SRO, insn[0]))
215         {
216             uregs = 0xb;
217             if( (ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
218                 (ARM_INSN_MATCH (SRO, insn[0]) && (ARM_INSN_REG_RD (insn[0]) == 15)) )
219             {
220                 DBPRINTF ("Unboostable insn %lx, DPIS/LRO/SRO\n", insn[0]);
221                 pc_dep = 1;
222             }
223         }
224         // Rn ,Rd
225         else if(ARM_INSN_MATCH (DPI, insn[0]) || ARM_INSN_MATCH (LIO, insn[0]) ||
226                 ARM_INSN_MATCH (SIO, insn[0]))
227         {
228             uregs = 0x3;
229             if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_MATCH (SIO, insn[0]) &&
230                         (ARM_INSN_REG_RD (insn[0]) == 15)))
231             {
232                 pc_dep = 1;
233                 DBPRINTF ("Unboostable insn %lx/%p, DPI/LIO/SIO\n", insn[0], p);
234             }
235         }
236         // Rn, Rm, Rs
237         else if(ARM_INSN_MATCH (DPRS, insn[0]))
238         {
239             uregs = 0xd;
240             if ((ARM_INSN_REG_RN (insn[0]) == 15) || (ARM_INSN_REG_RM (insn[0]) == 15) ||
241                 (ARM_INSN_REG_RS (insn[0]) == 15))
242             {
243                 pc_dep = 1;
244                 DBPRINTF ("Unboostable insn %lx, DPRS\n", insn[0]);
245             }
246         }
247         // register list
248         else if(ARM_INSN_MATCH (SM, insn[0]))
249         {
250             uregs = 0x10;
251             if (ARM_INSN_REG_MR (insn[0], 15))
252             {
253                 DBPRINTF ("Unboostable insn %lx, SM\n", insn[0]);
254                 pc_dep = 1;
255             }
256         }
257         // check instructions that can write result to SP andu uses PC
258         if (pc_dep  && (ARM_INSN_REG_RD (ainsn.insn[0]) == 13))
259         {
260             free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
261             ret = -EFAULT;
262         }
263         else
264         {
265             if (uregs && pc_dep)
266             {
267                 memcpy (insns, pc_dep_insn_execbuf, sizeof (insns));
268                 if (prep_pc_dep_insn_execbuf (insns, insn[0], uregs) != 0)
269                 {
270                     DBPRINTF ("failed to prepare exec buffer for insn %lx!", insn[0]);
271                     free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
272                     return -EINVAL;
273                 }
274                 insns[6] = (kprobe_opcode_t) (p->addr + 2);
275             }
276             else
277             {
278                 memcpy (insns, gen_insn_execbuf, sizeof (insns));
279                 insns[KPROBES_TRAMP_INSN_IDX] = insn[0];
280             }
281             insns[7] = (kprobe_opcode_t) (p->addr + 1);
282             DBPRINTF ("arch_prepare_kprobe: insn %lx", insn[0]);
283             DBPRINTF ("arch_prepare_kprobe: to %p - %lx %lx %lx %lx %lx %lx %lx %lx %lx",
284                     p->ainsn.insn, insns[0], insns[1], insns[2], insns[3], insns[4],
285                     insns[5], insns[6], insns[7], insns[8]);
286             memcpy (p->ainsn.insn, insns, sizeof(insns));
287             flush_icache_range((long unsigned)p->ainsn.insn, (long unsigned)(p->ainsn.insn) + sizeof(insns));
288 #ifdef BOARD_tegra
289             flush_cache_all();
290 #endif
291         }
292     }
293     else
294     {
295         free_insn_slot(&kprobe_insn_pages, NULL, p->ainsn.insn);
296         printk("arch_prepare_kprobe: instruction 0x%lx not instrumentation, addr=0x%p\n", insn[0], p->addr);
297     }
298
299     return ret;
300 }
301
302 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
303 {
304         if (p->ss_addr) {
305                 regs->ARM_pc = (unsigned long)p->ss_addr;
306                 p->ss_addr = NULL;
307         } else {
308                 regs->ARM_pc = (unsigned long)p->ainsn.insn;
309         }
310 }
311 EXPORT_SYMBOL_GPL(prepare_singlestep);
312
313 void save_previous_kprobe(struct kprobe_ctlblk *kcb, struct kprobe *p_run)
314 {
315         kcb->prev_kprobe.kp = kprobe_running();
316         kcb->prev_kprobe.status = kcb->kprobe_status;
317 }
318
319 void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
320 {
321         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
322         kcb->kprobe_status = kcb->prev_kprobe.status;
323 }
324
325 void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
326 {
327         __get_cpu_var(current_kprobe) = p;
328         DBPRINTF ("set_current_kprobe: p=%p addr=%p\n", p, p->addr);
329 }
330
331 #ifdef TRAP_OVERHEAD_DEBUG
332 static unsigned long trap_handler_counter_debug = 0;
333 #define SAMPLING_COUNTER                               100000
334 #endif
335
336 static int kprobe_handler(struct pt_regs *regs)
337 {
338         struct kprobe *p, *cur;
339         struct kprobe_ctlblk *kcb;
340
341         kcb = get_kprobe_ctlblk();
342         cur = kprobe_running();
343         p = get_kprobe((void *)regs->ARM_pc);
344
345         if (p) {
346                 if (cur) {
347                         /* Kprobe is pending, so we're recursing. */
348                         switch (kcb->kprobe_status) {
349                         case KPROBE_HIT_ACTIVE:
350                         case KPROBE_HIT_SSDONE:
351                                 /* A pre- or post-handler probe got us here. */
352                                 kprobes_inc_nmissed_count(p);
353                                 save_previous_kprobe(kcb, NULL);
354                                 set_current_kprobe(p, 0, 0);
355                                 kcb->kprobe_status = KPROBE_REENTER;
356                                 prepare_singlestep(p, regs);
357                                 restore_previous_kprobe(kcb);
358                                 break;
359                         default:
360                                 /* impossible cases */
361                                 BUG();
362                         }
363                 } else {
364                         set_current_kprobe(p, 0, 0);
365                         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
366
367                         if (!p->pre_handler || !p->pre_handler(p, regs)) {
368                                 kcb->kprobe_status = KPROBE_HIT_SS;
369                                 prepare_singlestep(p, regs);
370                                 reset_current_kprobe();
371                         }
372                 }
373         } else {
374                 goto no_kprobe;
375         }
376
377         return 0;
378
379 no_kprobe:
380         printk("no_kprobe\n");
381         return 1;
382 }
383
384 int kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
385 {
386         int ret;
387         unsigned long flags;
388
389 #ifdef SUPRESS_BUG_MESSAGES
390         int swap_oops_in_progress;
391         /* oops_in_progress used to avoid BUG() messages
392          * that slow down kprobe_handler() execution */
393         swap_oops_in_progress = oops_in_progress;
394         oops_in_progress = 1;
395 #endif
396
397         local_irq_save(flags);
398         preempt_disable();
399         ret = kprobe_handler(regs);
400         preempt_enable_no_resched();
401         local_irq_restore(flags);
402
403 #ifdef SUPRESS_BUG_MESSAGES
404         oops_in_progress = swap_oops_in_progress;
405 #endif
406
407         return ret;
408 }
409
410 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
411 {
412         struct jprobe *jp = container_of(p, struct jprobe, kp);
413         kprobe_pre_entry_handler_t pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
414         entry_point_t entry = (entry_point_t)jp->entry;
415         pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
416
417         if (((unsigned long)p->addr == sched_addr) && sched_rp) {
418                 struct thread_info *tinfo = (struct thread_info *)regs->ARM_r2;
419                 patch_suspended_task(sched_rp, tinfo->task);
420         }
421
422         if (pre_entry) {
423                 p->ss_addr = (void *)pre_entry (jp->priv_arg, regs);
424         }
425
426         if (entry) {
427                 entry(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2,
428                       regs->ARM_r3, regs->ARM_r4, regs->ARM_r5);
429         } else {
430                 dbi_jprobe_return();
431         }
432
433         return 0;
434 }
435
436 void dbi_jprobe_return (void)
437 {
438 }
439
440 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
441 {
442         return 0;
443 }
444 EXPORT_SYMBOL_GPL(longjmp_break_handler);
445
446 void arch_arm_kprobe (struct kprobe *p)
447 {
448         *p->addr = BREAKPOINT_INSTRUCTION;
449         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
450 }
451
452 void arch_disarm_kprobe (struct kprobe *p)
453 {
454         *p->addr = p->opcode;
455         flush_icache_range ((unsigned long) p->addr, (unsigned long) p->addr + sizeof (kprobe_opcode_t));
456 }
457
458
459 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
460 {
461         struct kretprobe_instance *ri = NULL;
462         struct hlist_head *head;
463         struct hlist_node *node, *tmp;
464         unsigned long flags, orig_ret_address = 0;
465         unsigned long trampoline_address = (unsigned long) &kretprobe_trampoline;
466
467         struct kretprobe *crp = NULL;
468         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
469
470         DBPRINTF ("start");
471
472         spin_lock_irqsave (&kretprobe_lock, flags);
473
474         /*
475          * We are using different hash keys (current and mm) for finding kernel
476          * space and user space probes.  Kernel space probes can change mm field in
477          * task_struct.  User space probes can be shared between threads of one
478          * process so they have different current but same mm.
479          */
480         head = kretprobe_inst_table_head(current);
481
482         /*
483          * It is possible to have multiple instances associated with a given
484          * task either because an multiple functions in the call path
485          * have a return probe installed on them, and/or more then one
486          * return probe was registered for a target function.
487          *
488          * We can handle this because:
489          *     - instances are always inserted at the head of the list
490          *     - when multiple return probes are registered for the same
491          *       function, the first instance's ret_addr will point to the
492          *       real return address, and all the rest will point to
493          *       kretprobe_trampoline
494          */
495         hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
496         {
497                 if (ri->task != current)
498                         /* another task is sharing our hash bucket */
499                         continue;
500                 if (ri->rp && ri->rp->handler){
501                         ri->rp->handler (ri, regs, ri->rp->priv_arg);
502                 }
503
504                 orig_ret_address = (unsigned long) ri->ret_addr;
505                 recycle_rp_inst (ri);
506                 if (orig_ret_address != trampoline_address)
507                         /*
508                          * This is the real return address. Any other
509                          * instances associated with this task are for
510                          * other calls deeper on the call stack
511                          */
512                         break;
513         }
514         kretprobe_assert (ri, orig_ret_address, trampoline_address);
515
516         regs->uregs[14] = orig_ret_address;
517         DBPRINTF ("regs->uregs[14] = 0x%lx\n", regs->uregs[14]);
518         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
519
520         if (trampoline_address != (unsigned long) &kretprobe_trampoline)
521         {
522                 regs->uregs[15] = orig_ret_address;
523         }else{
524                 if (!thumb_mode( regs )) regs->uregs[15] += 4;
525                 else regs->uregs[15] += 2;
526         }
527
528         DBPRINTF ("regs->uregs[15] = 0x%lx\n", regs->uregs[15]);
529
530         if(p){ // ARM, MIPS, X86 user space
531                 if (thumb_mode( regs ) && !(regs->uregs[14] & 0x01))
532                 {
533                         regs->ARM_cpsr &= 0xFFFFFFDF;
534                 }else{
535                         if (user_mode( regs ) && (regs->uregs[14] & 0x01))
536                         {
537                                 regs->ARM_cpsr |= 0x20;
538                         }
539                 }
540
541                 if (kcb->kprobe_status == KPROBE_REENTER) {
542                         restore_previous_kprobe(kcb);
543                 } else {
544                         reset_current_kprobe();
545                 }
546         }
547
548         spin_unlock_irqrestore (&kretprobe_lock, flags);
549
550         /*
551          * By returning a non-zero value, we are telling
552          * kprobe_handler() that we don't want the post_handler
553          * to run (and have re-enabled preemption)
554          */
555
556         return 1;
557 }
558 EXPORT_SYMBOL_GPL(trampoline_probe_handler);
559
560 void  __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
561 {
562         struct kretprobe_instance *ri;
563
564         DBPRINTF ("start\n");
565         //TODO: test - remove retprobe after func entry but before its exit
566         if ((ri = get_free_rp_inst (rp)) != NULL)
567         {
568                 ri->rp = rp;
569                 ri->task = current;
570                 ri->ret_addr = (kprobe_opcode_t *) regs->uregs[14];
571                 ri->sp = (kprobe_opcode_t *)regs->ARM_sp; //uregs[13];
572
573                 /* Set flag of current mode */
574                 ri->sp = (kprobe_opcode_t *)((long)ri->sp | !!thumb_mode(regs));
575
576                 /* Replace the return addr with trampoline addr */
577                 regs->uregs[14] = (unsigned long) &kretprobe_trampoline;
578
579 //              DBPRINTF ("ret addr set to %p->%lx\n", ri->ret_addr, regs->uregs[14]);
580                 add_rp_inst (ri);
581         }
582         else {
583                 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
584                 rp->nmissed++;
585         }
586 }
587
588 void swap_register_undef_hook(struct undef_hook *hook)
589 {
590         __swap_register_undef_hook(hook);
591 }
592 EXPORT_SYMBOL_GPL(swap_register_undef_hook);
593
594 void swap_unregister_undef_hook(struct undef_hook *hook)
595 {
596         __swap_unregister_undef_hook(hook);
597 }
598 EXPORT_SYMBOL_GPL(swap_unregister_undef_hook);
599
600 // kernel probes hook
601 static struct undef_hook undef_ho_k = {
602     .instr_mask = 0xffffffff,
603     .instr_val  = BREAKPOINT_INSTRUCTION,
604     .cpsr_mask  = MODE_MASK,
605     .cpsr_val   = SVC_MODE,
606     .fn         = kprobe_trap_handler
607 };
608
609 int arch_init_kprobes(void)
610 {
611         int ret = 0;
612
613         // Register hooks (kprobe_handler)
614         __swap_register_undef_hook = swap_ksyms("register_undef_hook");
615         if (__swap_register_undef_hook == NULL) {
616                 printk("no register_undef_hook symbol found!\n");
617                 return -1;
618         }
619
620         // Unregister hooks (kprobe_handler)
621         __swap_unregister_undef_hook = swap_ksyms("unregister_undef_hook");
622         if (__swap_unregister_undef_hook == NULL) {
623                 printk("no unregister_undef_hook symbol found!\n");
624                 return -1;
625         }
626
627         swap_register_undef_hook(&undef_ho_k);
628         if ((ret = dbi_register_kprobe (&trampoline_p)) != 0) {
629                 //dbi_unregister_jprobe(&do_exit_p, 0);
630                 return ret;
631         }
632         return ret;
633 }
634
635 void arch_exit_kprobes(void)
636 {
637         swap_unregister_undef_hook(&undef_ho_k);
638 }
639
640 /* export symbol for trampoline_arm.h */
641 EXPORT_SYMBOL_GPL(gen_insn_execbuf);
642 EXPORT_SYMBOL_GPL(pc_dep_insn_execbuf);