Redesign KProbe module (separating core and arch parts).
[kernel/swap-modules.git] / kprobe / arch / asm-x86 / dbi_kprobes.c
1 /*
2  *  Kernel Probes (KProbes)
3  *  arch/x86/kernel/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) IBM Corporation, 2002, 2004
20  */
21
22 /*
23  *  Dynamic Binary Instrumentation Module based on KProbes
24  *  modules/kprobe/arch/asm-x86/dbi_kprobes.c
25  *
26  * This program is free software; you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation; either version 2 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program; if not, write to the Free Software
38  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39  *
40  * Copyright (C) Samsung Electronics, 2006-2010
41  *
42  * 2008-2009    Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
43  *              Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
44  * 2010         Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts 
45  *
46
47  */
48
49
50 #include "dbi_krobes.h"
51 #include "../dbi_krobes.h"
52 #include "../../dbi_kprobes.h"
53
54 #include "../../dbi_kdebug.h"
55 #include "../../dbi_insn_slots.h"
56 #include "../../dbi_kprobes_deps.h"
57
58 /*
59  * Function return probe trampoline:
60  *      - init_kprobes() establishes a probepoint here
61  *      - When the probed function returns, this probe
62  *              causes the handlers to fire
63  */
64 void kretprobe_trampoline_holder (void)
65 {
66         asm volatile (".global kretprobe_trampoline\n" 
67                         "kretprobe_trampoline:\n"
68                         "       pushf\n"
69                         /* skip cs, eip, orig_eax */
70                         "       subl $12, %esp\n"
71                         "       pushl %fs\n"
72                         "       pushl %ds\n"
73                         "       pushl %es\n"
74                         "       pushl %eax\n" 
75                         "       pushl %ebp\n" 
76                         "       pushl %edi\n" 
77                         "       pushl %esi\n" 
78                         "       pushl %edx\n" 
79                         "       pushl %ecx\n" 
80                         "       pushl %ebx\n" 
81                         "       movl %esp, %eax\n" 
82                         "       call trampoline_probe_handler_x86\n"
83                         /* move eflags to cs */
84                         "       movl 52(%esp), %edx\n" 
85                         "       movl %edx, 48(%esp)\n"
86                         /* save true return address on eflags */
87                         "       movl %eax, 52(%esp)\n" 
88                         "       popl %ebx\n" ""
89                         "       popl %ecx\n" 
90                         "       popl %edx\n" 
91                         "       popl %esi\n" 
92                         "       popl %edi\n" 
93                         "       popl %ebp\n" 
94                         "       popl %eax\n"
95                         /* skip eip, orig_eax, es, ds, fs */
96                         "       addl $20, %esp\n" 
97                         "       popf\n" 
98                         "       ret\n");
99 }
100
101
102 struct kprobe trampoline_p =
103 {
104         .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
105         .pre_handler = trampoline_probe_handler
106 };
107
108 /* insert a jmp code */
109 static __always_inline void set_jmp_op (void *from, void *to)
110 {
111         struct __arch_jmp_op
112         {
113                 char op;
114                 long raddr;
115         } __attribute__ ((packed)) * jop;
116         jop = (struct __arch_jmp_op *) from;
117         jop->raddr = (long) (to) - ((long) (from) + 5);
118         jop->op = RELATIVEJUMP_INSTRUCTION;
119 }
120
121 static void set_user_jmp_op (void *from, void *to)
122 {
123         struct __arch_jmp_op
124         {
125                 char op;
126                 long raddr;
127         } __attribute__ ((packed)) jop;
128         //jop = (struct __arch_jmp_op *) from;
129         jop.raddr = (long) (to) - ((long) (from) + 5);
130         jop.op = RELATIVEJUMP_INSTRUCTION;
131         if (!write_proc_vm_atomic (current, (unsigned long)from, &jop, sizeof(jop)))
132                 panic ("failed to write jump opcode to user space %p!\n", from);        
133 }
134
135 /*
136  * returns non-zero if opcodes can be boosted.
137  */
138 static __always_inline int can_boost (kprobe_opcode_t * opcodes)
139 {
140 #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf)                \
141         (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
142           (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
143           (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
144           (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
145          << (row % 32))
146         /*
147          * Undefined/reserved opcodes, conditional jump, Opcode Extension
148          * Groups, and some special opcodes can not be boost.
149          */
150         static const unsigned long twobyte_is_boostable[256 / 32] = {
151                 /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
152                 /*      -------------------------------         */
153                 W (0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) |      /* 00 */
154                         W (0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),       /* 10 */
155                 W (0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |      /* 20 */
156                         W (0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),       /* 30 */
157                 W (0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) |      /* 40 */
158                         W (0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),       /* 50 */
159                 W (0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) |      /* 60 */
160                         W (0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1),       /* 70 */
161                 W (0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |      /* 80 */
162                         W (0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),       /* 90 */
163                 W (0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) |      /* a0 */
164                         W (0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1),       /* b0 */
165                 W (0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) |      /* c0 */
166                         W (0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1),       /* d0 */
167                 W (0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) |      /* e0 */
168                         W (0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0)        /* f0 */
169                         /*      -------------------------------         */
170                         /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
171         };
172 #undef W
173         kprobe_opcode_t opcode;
174         kprobe_opcode_t *orig_opcodes = opcodes;
175 retry:
176         if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
177                 return 0;
178         opcode = *(opcodes++);
179
180         /* 2nd-byte opcode */
181         if (opcode == 0x0f)
182         {
183                 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
184                         return 0;
185                 return test_bit (*opcodes, twobyte_is_boostable);
186         }
187
188         switch (opcode & 0xf0)
189         {
190                 case 0x60:
191                         if (0x63 < opcode && opcode < 0x67)
192                                 goto retry;     /* prefixes */
193                         /* can't boost Address-size override and bound */
194                         return (opcode != 0x62 && opcode != 0x67);
195                 case 0x70:
196                         return 0;       /* can't boost conditional jump */
197                 case 0xc0:
198                         /* can't boost software-interruptions */
199                         return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
200                 case 0xd0:
201                         /* can boost AA* and XLAT */
202                         return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
203                 case 0xe0:
204                         /* can boost in/out and absolute jmps */
205                         return ((opcode & 0x04) || opcode == 0xea);
206                 case 0xf0:
207                         if ((opcode & 0x0c) == 0 && opcode != 0xf1)
208                                 goto retry;     /* lock/rep(ne) prefix */
209                         /* clear and set flags can be boost */
210                         return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
211                 default:
212                         if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
213                                 goto retry;     /* prefixes */
214                         /* can't boost CS override and call */
215                         return (opcode != 0x2e && opcode != 0x9a);
216         }
217 }
218
219 /*
220  * returns non-zero if opcode modifies the interrupt flag.
221  */
222 static int __kprobes is_IF_modifier (kprobe_opcode_t opcode)
223 {
224         switch (opcode)
225         {
226                 case 0xfa:              /* cli */
227                 case 0xfb:              /* sti */
228                 case 0xcf:              /* iret/iretd */
229                 case 0x9d:              /* popf/popfd */
230                         return 1;
231         }
232         return 0;
233 }
234
235 int arch_check_insn (struct arch_specific_insn *ainsn)
236 {
237         DPRINF("Warrning: arch_check_insn is not implemented for x86\n");
238         return 0;
239 }
240
241 int arch_prepare_kretprobe (struct kretprobe *p)
242 {
243         DPRINF("Warrning: arch_prepare_kretprobe is not implemented\n");
244         return 0;
245 }
246
247
248 int arch_prepare_kprobe (struct kprobe *p)
249 {
250         kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
251
252         int ret = 0;
253
254         if ((unsigned long) p->addr & 0x01)
255         {
256                 DBPRINTF ("Attempt to register kprobe at an unaligned address\n");
257                 ret = -EINVAL;
258         }
259
260
261         if (!ret)
262         {
263                 kprobe_opcode_t insn[MAX_INSN_SIZE];
264                 struct arch_specific_insn ainsn;
265                 /* insn: must be on special executable page on i386. */
266                 p->ainsn.insn = get_insn_slot (NULL, 0);
267                 if (!p->ainsn.insn)
268                         return -ENOMEM;
269                 memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
270                 ainsn.insn = insn;
271                 ret = arch_check_insn (&ainsn);
272                 if (!ret)
273                 {
274                         p->opcode = *p->addr;
275                 }
276
277                 if (can_boost (p->addr))
278                         p->ainsn.boostable = 0;
279                 else
280                         p->ainsn.boostable = -1;
281                 memcpy (p->ainsn.insn, insn, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
282         }
283         else
284         {
285                 free_insn_slot (&kprobe_insn_pages, NULL, p->ainsn.insn, 0);
286         }
287
288         return ret;
289 }
290
291 int arch_prepare_uprobe (struct kprobe *p, struct task_struct *task, int atomic)
292 {
293         int ret = 0;
294         kprobe_opcode_t insns[UPROBES_TRAMP_LEN];
295
296         if (!ret)
297         {
298                 kprobe_opcode_t insn[MAX_INSN_SIZE];
299                 struct arch_specific_insn ainsn;
300
301                 if (!read_proc_vm_atomic (task, (unsigned long) p->addr, &insn, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
302                         panic ("failed to read memory %p!\n", p->addr);
303                 ainsn.insn = insn;
304                 ret = arch_check_insn (&ainsn);
305                 if (!ret)
306                 {
307                         p->opcode = insn[0];
308                         p->ainsn.insn = get_insn_slot(task, atomic);
309                         if (!p->ainsn.insn)
310                                 return -ENOMEM;
311                         if (can_boost (insn))
312                                 p->ainsn.boostable = 0;
313                         else
314                                 p->ainsn.boostable = -1;
315                         memcpy (&insns[UPROBES_TRAMP_INSN_IDX], insn, MAX_INSN_SIZE*sizeof(kprobe_opcode_t));
316                         insns[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
317
318                         if (!write_proc_vm_atomic (task, (unsigned long) p->ainsn.insn, insns, sizeof (insns)))
319                         {
320                                 panic("failed to write memory %p!\n", p->ainsn.insn);
321                                 DBPRINTF ("failed to write insn slot to process memory: insn %p, addr %p, probe %p!", insn, p->ainsn.insn, p->addr);
322                                 free_insn_slot (&uprobe_insn_pages, task, p->ainsn.insn, 0);
323                                 return -EINVAL;
324                         }
325                 }
326         }
327
328         return ret;
329 }
330
331 int arch_prepare_uretprobe (struct kretprobe *p, struct task_struct *task)
332 {
333         DPRINF("Warrning: arch_prepare_uretprobe is not implemented\n");
334         return 0;
335 }
336
337 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
338 {
339
340         if(p->ss_addr)
341         {
342                 regs->EREG (ip) = (unsigned long)p->ss_addr;
343                 p->ss_addr = NULL;
344         }
345         else
346         {
347                 regs->EREG (flags) |= TF_MASK;
348                 regs->EREG (flags) &= ~IF_MASK;
349                 /*single step inline if the instruction is an int3 */
350                 if (p->opcode == BREAKPOINT_INSTRUCTION){
351                         regs->EREG (ip) = (unsigned long) p->addr;
352                         //printk("break_insn!!!\n");
353                 }
354                 else
355                         regs->EREG (ip) = (unsigned long) p->ainsn.insn;
356         } 
357 }
358
359
360 void save_previous_kprobe (struct kprobe_ctlblk *kcb, struct kprobe *cur_p)
361 {
362         if (kcb->prev_kprobe.kp != NULL)
363         {
364                 panic ("no space to save new probe[%lu]: task = %d/%s, prev %d/%p, current %d/%p, new %d/%p,",
365                                 nCount, current->pid, current->comm, kcb->prev_kprobe.kp->tgid, kcb->prev_kprobe.kp->addr, 
366                                 kprobe_running()->tgid, kprobe_running()->addr, cur_p->tgid, cur_p->addr);
367         }
368
369
370         kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
371         kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
372
373 }
374
375 void restore_previous_kprobe (struct kprobe_ctlblk *kcb)
376 {
377         __get_cpu_var (current_kprobe) = kcb->prev_kprobe.kp;
378         kcb->kprobe_status = kcb->prev_kprobe.status;
379         kcb->prev_kprobe.kp = NULL;
380         kcb->prev_kprobe.status = 0;
381         kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
382         kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
383 }
384
385 void set_current_kprobe (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
386 {
387         __get_cpu_var (current_kprobe) = p;
388         DBPRINTF ("set_current_kprobe[%lu]: p=%p addr=%p\n", nCount, p, p->addr);
389         kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags = (regs->EREG (flags) & (TF_MASK | IF_MASK));
390         if (is_IF_modifier (p->opcode))
391                 kcb->kprobe_saved_eflags &= ~IF_MASK;
392 }
393
394 int kprobe_handler (struct pt_regs *regs)
395 {
396         struct kprobe *p = 0;
397         int ret = 0, pid = 0, retprobe = 0, reenter = 0;
398         kprobe_opcode_t *addr = NULL;
399         struct kprobe_ctlblk *kcb;      
400
401         nCount++;
402
403         /* We're in an interrupt, but this is clear and BUG()-safe. */
404         addr = (kprobe_opcode_t *) (regs->EREG (ip) - sizeof (kprobe_opcode_t));
405         DBPRINTF ("KPROBE[%lu]: regs->eip = 0x%lx addr = 0x%p\n", nCount, regs->EREG (ip), addr);
406
407         preempt_disable ();
408
409         kcb = get_kprobe_ctlblk ();
410
411         if (user_mode_vm(regs))
412         {
413                 //printk("exception[%lu] from user mode %s/%u/%u addr %p.\n", nCount, current->comm, current->pid, current->tgid, addr);
414                 pid = current->tgid;
415         }
416
417         /* Check we're not actually recursing */
418         if (kprobe_running ())
419         {
420                 DBPRINTF ("lock???");
421                 p = get_kprobe (addr, pid, current);
422                 if (p)
423                 {
424                         DBPRINTF ("reenter p = %p", p);
425                         if(!pid){
426                                 if (kcb->kprobe_status == KPROBE_HIT_SS && *p->ainsn.insn == BREAKPOINT_INSTRUCTION)
427                                 {
428                                         regs->EREG (flags) &= ~TF_MASK;
429                                         regs->EREG (flags) |= kcb->kprobe_saved_eflags;
430                                         goto no_kprobe;
431                                 }
432                         }
433                         else {
434                                 //#warning BREAKPOINT_INSTRUCTION user mode handling is missed!!! 
435                         }
436
437                         /* We have reentered the kprobe_handler(), since
438                          * another probe was hit while within the handler.
439                          * We here save the original kprobes variables and
440                          * just single step on the instruction of the new probe
441                          * without calling any user handlers.
442                          */
443                         save_previous_kprobe (kcb, p);
444                         set_current_kprobe (p, regs, kcb);
445                         kprobes_inc_nmissed_count (p);
446                         prepare_singlestep (p, regs);
447                         kcb->kprobe_status = KPROBE_REENTER;
448                         return 1;
449                 }
450                 else
451                 {
452                         if(!pid){
453                                 if (*addr != BREAKPOINT_INSTRUCTION)
454                                 {
455                                         /* The breakpoint instruction was removed by
456                                          * another cpu right after we hit, no further
457                                          * handling of this interrupt is appropriate
458                                          */
459                                         regs->EREG (ip) -= sizeof (kprobe_opcode_t);
460                                         ret = 1;
461                                         goto no_kprobe;
462                                 }
463                         }
464                         else {
465                                 //#warning BREAKPOINT_INSTRUCTION user mode handling is missed!!! 
466                                 //we can reenter probe upon uretprobe exception   
467                                 DBPRINTF ("check for UNDEF_INSTRUCTION %p\n", addr);
468                                 // UNDEF_INSTRUCTION from user space
469                                 p = get_kprobe_by_insn_slot (addr-UPROBES_TRAMP_RET_BREAK_IDX, pid, current);
470                                 if (p) {
471                                         save_previous_kprobe (kcb, p);
472                                         kcb->kprobe_status = KPROBE_REENTER;
473                                         reenter = 1;
474                                         retprobe = 1;
475                                         DBPRINTF ("uretprobe %p\n", addr);
476                                 }
477                         }
478                         if(!p){
479                                 p = __get_cpu_var (current_kprobe);
480                                 if(p->tgid)
481                                         panic("after uhandler");
482                                 DBPRINTF ("kprobe_running !!! p = 0x%p p->break_handler = 0x%p", p, p->break_handler);
483                                 if (p->break_handler && p->break_handler (p, regs))
484                                 {
485                                         DBPRINTF ("kprobe_running !!! goto ss");
486                                         goto ss_probe;
487                                 }
488                                 DBPRINTF ("kprobe_running !!! goto no");
489                                 DBPRINTF ("no_kprobe");
490                                 goto no_kprobe;
491                         }
492                 }
493         }
494
495         DBPRINTF ("get_kprobe %p", addr);
496         if (!p)
497                 p = get_kprobe (addr, pid, current);
498         if (!p)
499         {
500                 if(!pid){
501                         if (*addr != BREAKPOINT_INSTRUCTION)
502                         {
503                                 /*
504                                  * The breakpoint instruction was removed right
505                                  * after we hit it.  Another cpu has removed
506                                  * either a probepoint or a debugger breakpoint
507                                  * at this address.  In either case, no further
508                                  * handling of this interrupt is appropriate.
509                                  * Back up over the (now missing) int3 and run
510                                  * the original instruction.
511                                  */
512                                 regs->EREG (ip) -= sizeof (kprobe_opcode_t);
513                                 ret = 1;
514                         }
515                 }
516                 else {
517                         //#warning BREAKPOINT_INSTRUCTION user mode handling is missed!!! 
518                         DBPRINTF ("search UNDEF_INSTRUCTION %p\n", addr);
519                         // UNDEF_INSTRUCTION from user space
520                         p = get_kprobe_by_insn_slot (addr-UPROBES_TRAMP_RET_BREAK_IDX, pid, current);
521                         if (!p) {
522                                 // Not one of ours: let kernel handle it
523                                 DBPRINTF ("no_kprobe");
524                                 //printk("no_kprobe2 ret = %d\n", ret);
525                                 goto no_kprobe;
526                         }
527                         retprobe = 1;
528                         DBPRINTF ("uretprobe %p\n", addr);
529                 }
530                 if(!p) {
531                         /* Not one of ours: let kernel handle it */
532                         DBPRINTF ("no_kprobe");
533                         goto no_kprobe;
534                 }
535         }
536         set_current_kprobe (p, regs, kcb);
537         if(!reenter)
538                 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
539
540         if (retprobe)           //(einsn == UNDEF_INSTRUCTION)
541                 ret = trampoline_probe_handler (p, regs);
542         else if (p->pre_handler)
543                 ret = p->pre_handler (p, regs);
544
545         if (ret)
546         {
547                 if (ret == 2) { // we have alreadyc called the handler, so just single step the instruction
548                         DBPRINTF ("p->pre_handler[%lu] 2", nCount);
549                         goto ss_probe;
550                 }
551                 DBPRINTF ("p->pre_handler[%lu] 1", nCount);
552                 /* handler has already set things up, so skip ss setup */
553                 return 1;
554         }
555         DBPRINTF ("p->pre_handler[%lu] 0", nCount);
556
557 ss_probe:
558         DBPRINTF ("p = %p\n", p);
559         DBPRINTF ("p->opcode = 0x%lx *p->addr = 0x%lx p->addr = 0x%p\n", (unsigned long) p->opcode, p->tgid ? 0 : (unsigned long) (*p->addr), p->addr);
560
561 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
562         if (p->ainsn.boostable == 1 && !p->post_handler)
563         {
564                 /* Boost up -- we can execute copied instructions directly */
565                 reset_current_kprobe ();
566                 regs->EREG (ip) = (unsigned long) p->ainsn.insn;
567                 preempt_enable_no_resched ();
568                 return 1;
569         }
570 #endif // !CONFIG_PREEMPT
571         prepare_singlestep (p, regs);
572         kcb->kprobe_status = KPROBE_HIT_SS;
573         return 1;
574
575 no_kprobe:
576
577         preempt_enable_no_resched ();
578         return ret;
579 }
580
581 int setjmp_pre_handler (struct kprobe *p, struct pt_regs *regs) 
582 {
583         struct jprobe *jp = container_of (p, struct jprobe, kp);
584         kprobe_pre_entry_handler_t pre_entry;
585         entry_point_t entry;
586
587         unsigned long addr, args[6];    
588         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
589
590         DBPRINTF ("setjmp_pre_handler %p:%d", p->addr, p->tgid);
591         pre_entry = (kprobe_pre_entry_handler_t) jp->pre_entry;
592         entry = (entry_point_t) jp->entry;
593         if(p->tgid) {
594                 regs->EREG (flags) &= ~IF_MASK;
595 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
596                 trace_hardirqs_off ();
597 #endif
598                 if (p->tgid == current->tgid)
599                 {
600                         // read first 6 args from stack
601                         if (!read_proc_vm_atomic (current, regs->EREG(sp)+4, args, sizeof(args)))
602                                 panic ("failed to read user space func arguments %lx!\n", regs->EREG(sp)+4);
603                         if (pre_entry)
604                                 p->ss_addr = pre_entry (jp->priv_arg, regs);
605                         if (entry)
606                                 entry (args[0], args[1], args[2], args[3], args[4], args[5]);
607                 }
608                 else
609                         arch_uprobe_return ();
610
611                 return 2;
612         }
613         else {
614                 kcb->jprobe_saved_regs = *regs;
615                 kcb->jprobe_saved_esp = &regs->EREG (sp);
616                 addr = (unsigned long) (kcb->jprobe_saved_esp);
617
618                 /*
619                  * TBD: As Linus pointed out, gcc assumes that the callee
620                  * owns the argument space and could overwrite it, e.g.
621                  * tailcall optimization. So, to be absolutely safe
622                  * we also save and restore enough stack bytes to cover
623                  * the argument area.
624                  */
625                 memcpy (kcb->jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE (addr));
626                 regs->EREG (flags) &= ~IF_MASK;
627 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
628                 trace_hardirqs_off ();
629 #endif
630                 if (pre_entry)
631                         p->ss_addr = pre_entry (jp->priv_arg, regs);
632                 regs->EREG (ip) = (unsigned long) (jp->entry);
633         }
634
635         return 1;
636 }
637
638 void __kprobes jprobe_return (void)
639 {
640         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
641
642         asm volatile("       xchgl   %%ebx,%%esp     \n"
643                         "       int3                    \n"
644                         "       .globl jprobe_return_end        \n"
645                         "       jprobe_return_end:      \n"
646                         "       nop                     \n"::"b" (kcb->jprobe_saved_esp):"memory");
647 }
648
649 void __kprobes arch_uprobe_return (void)
650 {
651         DPRINTF("arch_uprobe_return (void) is empty");
652 }
653
654 /*
655  * Called after single-stepping.  p->addr is the address of the
656  * instruction whose first byte has been replaced by the "int 3"
657  * instruction.  To avoid the SMP problems that can occur when we
658  * temporarily put back the original opcode to single-step, we
659  * single-stepped a copy of the instruction.  The address of this
660  * copy is p->ainsn.insn.
661  *
662  * This function prepares to return from the post-single-step
663  * interrupt.  We have to fix up the stack as follows:
664  *
665  * 0) Except in the case of absolute or indirect jump or call instructions,
666  * the new eip is relative to the copied instruction.  We need to make
667  * it relative to the original instruction.
668  *
669  * 1) If the single-stepped instruction was pushfl, then the TF and IF
670  * flags are set in the just-pushed eflags, and may need to be cleared.
671  *
672  * 2) If the single-stepped instruction was a call, the return address
673  * that is atop the stack is the address following the copied instruction.
674  * We need to make it the address following the original instruction.
675  *
676  * This function also checks instruction size for preparing direct execution.
677  */
678 static void __kprobes resume_execution (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
679 {
680         unsigned long *tos, tos_dword = 0;
681         unsigned long copy_eip = (unsigned long) p->ainsn.insn;
682         unsigned long orig_eip = (unsigned long) p->addr;
683         kprobe_opcode_t insns[2];
684
685         regs->EREG (flags) &= ~TF_MASK;
686
687         if(p->tgid){
688                 tos = (unsigned long *) &tos_dword;
689                 if (!read_proc_vm_atomic (current, regs->EREG (sp), &tos_dword, sizeof(tos_dword)))
690                         panic ("failed to read dword from top of the user space stack %lx!\n", regs->EREG (sp));
691                 if (!read_proc_vm_atomic (current, (unsigned long)p->ainsn.insn, insns, 2*sizeof(kprobe_opcode_t)))
692                         panic ("failed to read first 2 opcodes of instruction copy from user space %p!\n", p->ainsn.insn);
693         }
694         else {
695                 tos = (unsigned long *) &regs->EREG (sp);
696                 insns[0] = p->ainsn.insn[0];
697                 insns[1] = p->ainsn.insn[1];
698         }
699
700         switch (insns[0])
701         {
702                 case 0x9c:              /* pushfl */
703                         *tos &= ~(TF_MASK | IF_MASK);
704                         *tos |= kcb->kprobe_old_eflags;
705                         break;
706                 case 0xc2:              /* iret/ret/lret */
707                 case 0xc3:
708                 case 0xca:
709                 case 0xcb:
710                 case 0xcf:
711                 case 0xea:              /* jmp absolute -- eip is correct */
712                         /* eip is already adjusted, no more changes required */
713                         p->ainsn.boostable = 1;
714                         goto no_change;
715                 case 0xe8:              /* call relative - Fix return addr */
716                         *tos = orig_eip + (*tos - copy_eip);
717                         break;
718                 case 0x9a:              /* call absolute -- same as call absolute, indirect */
719                         *tos = orig_eip + (*tos - copy_eip);
720                         if(p->tgid){
721                                 if (!write_proc_vm_atomic (current, regs->EREG (sp), &tos_dword, sizeof(tos_dword)))
722                                         panic ("failed to write dword to top of the user space stack %lx!\n", regs->EREG (sp));
723                         }
724                         goto no_change;
725                 case 0xff:
726                         if ((insns[1] & 0x30) == 0x10)
727                         {
728                                 /*
729                                  * call absolute, indirect
730                                  * Fix return addr; eip is correct.
731                                  * But this is not boostable
732                                  */
733                                 *tos = orig_eip + (*tos - copy_eip);
734                                 if(p->tgid){
735                                         if (!write_proc_vm_atomic (current, regs->EREG (sp), &tos_dword, sizeof(tos_dword)))
736                                                 panic ("failed to write dword to top of the user space stack %lx!\n", regs->EREG (sp));
737                                 }
738                                 goto no_change;
739                         }
740                         else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
741                                         ((insns[1] & 0x31) == 0x21))
742                         {               /* jmp far, absolute indirect */
743                                 /* eip is correct. And this is boostable */
744                                 p->ainsn.boostable = 1;
745                                 goto no_change;
746                         }
747                 default:
748                         break;
749         }
750
751         if(p->tgid){
752                 if (!write_proc_vm_atomic (current, regs->EREG (sp), &tos_dword, sizeof(tos_dword)))
753                         panic ("failed to write dword to top of the user space stack %lx!\n", regs->EREG (sp));
754         }
755
756         if (p->ainsn.boostable == 0)
757         {
758                 if ((regs->EREG (ip) > copy_eip) && (regs->EREG (ip) - copy_eip) + 5 < MAX_INSN_SIZE)
759                 {
760                         /*
761                          * These instructions can be executed directly if it
762                          * jumps back to correct address.
763                          */                     
764                         if(p->tgid)
765                                 set_user_jmp_op ((void *) regs->EREG (ip), (void *) orig_eip + (regs->EREG (ip) - copy_eip));                           
766                         else
767                                 set_jmp_op ((void *) regs->EREG (ip), (void *) orig_eip + (regs->EREG (ip) - copy_eip));
768                         p->ainsn.boostable = 1;
769                 }
770                 else
771                 {
772                         p->ainsn.boostable = -1;
773                 }
774         }
775
776         regs->EREG (ip) = orig_eip + (regs->EREG (ip) - copy_eip);
777
778 no_change:
779         return;
780 }
781
782 /*
783  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
784  * remain disabled thoroughout this function.
785  */
786 static int __kprobes post_kprobe_handler (struct pt_regs *regs)
787 {
788         struct kprobe *cur = kprobe_running ();
789         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
790
791         if (!cur)
792                 return 0;
793         if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler)
794         {
795                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
796                 cur->post_handler (cur, regs, 0);
797         }
798
799         resume_execution (cur, regs, kcb);
800         regs->EREG (flags) |= kcb->kprobe_saved_eflags;
801 #ifndef CONFIG_X86
802         trace_hardirqs_fixup_flags (regs->EREG (flags));
803 #endif // CONFIG_X86
804         /*Restore back the original saved kprobes variables and continue. */
805         if (kcb->kprobe_status == KPROBE_REENTER)
806         {
807                 restore_previous_kprobe (kcb);
808                 goto out;
809         }
810         reset_current_kprobe ();
811 out:
812         preempt_enable_no_resched ();
813
814         /*
815          * if somebody else is singlestepping across a probe point, eflags
816          * will have TF set, in which case, continue the remaining processing
817          * of do_debug, as if this is not a probe hit.
818          */
819         if (regs->EREG (flags) & TF_MASK)
820                 return 0;
821
822         return 1;
823 }
824
825 int __kprobes kprobe_fault_handler (struct pt_regs *regs, int trapnr)
826 {
827         struct kprobe *cur = kprobe_running ();
828         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
829
830         switch (kcb->kprobe_status)
831         {
832                 case KPROBE_HIT_SS:
833                 case KPROBE_REENTER:
834                         /*
835                          * We are here because the instruction being single
836                          * stepped caused a page fault. We reset the current
837                          * kprobe and the eip points back to the probe address
838                          * and allow the page fault handler to continue as a
839                          * normal page fault.
840                          */
841                         regs->EREG (ip) = (unsigned long) cur->addr;
842                         regs->EREG (flags) |= kcb->kprobe_old_eflags;
843                         if (kcb->kprobe_status == KPROBE_REENTER)
844                                 restore_previous_kprobe (kcb);
845                         else
846                                 reset_current_kprobe ();
847                         preempt_enable_no_resched ();
848                         break;
849                 case KPROBE_HIT_ACTIVE:
850                 case KPROBE_HIT_SSDONE:
851                         /*
852                          * We increment the nmissed count for accounting,
853                          * we can also use npre/npostfault count for accouting
854                          * these specific fault cases.
855                          */
856                         kprobes_inc_nmissed_count (cur);
857
858                         /*
859                          * We come here because instructions in the pre/post
860                          * handler caused the page_fault, this could happen
861                          * if handler tries to access user space by
862                          * copy_from_user(), get_user() etc. Let the
863                          * user-specified handler try to fix it first.
864                          */
865                         if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr))
866                                 return 1;
867
868                         /*
869                          * In case the user-specified fault handler returned
870                          * zero, try to fix up.
871                          */
872                         if (fixup_exception (regs))
873                                 return 1;
874
875                         /*
876                          * fixup_exception() could not handle it,
877                          * Let do_page_fault() fix it.
878                          */
879                         break;
880                 default:
881                         break;
882         }
883         return 0;
884 }
885
886 int kprobe_exceptions_notify (struct notifier_block *self, unsigned long val, void *data)
887 {
888         struct die_args *args = (struct die_args *) data;
889         int ret = NOTIFY_DONE;
890
891         DBPRINTF ("val = %ld, data = 0x%X", val, (unsigned int) data);
892
893         /*if (args->regs && user_mode_vm (args->regs))
894           return ret;*/
895
896         DBPRINTF ("switch (val) %lu %d %d", val, DIE_INT3, DIE_TRAP);
897         switch (val)
898         {
899                 //#ifdef CONFIG_KPROBES
900                 //      case DIE_INT3:
901                 //#else
902                 case DIE_TRAP:
903                         //#endif
904                         DBPRINTF ("before kprobe_handler ret=%d %p", ret, args->regs);
905                         if (kprobe_handler (args->regs))
906                                 ret = NOTIFY_STOP;
907                         DBPRINTF ("after kprobe_handler ret=%d %p", ret, args->regs);
908                         break;
909                 case DIE_DEBUG:
910                         if (post_kprobe_handler (args->regs))
911                                 ret = NOTIFY_STOP;
912                         break;
913                 case DIE_GPF:
914                         // kprobe_running() needs smp_processor_id()
915                         preempt_disable ();
916                         if (kprobe_running () && kprobe_fault_handler (args->regs, args->trapnr))
917                                 ret = NOTIFY_STOP;
918                         preempt_enable ();
919                         break;
920                 default:
921                         break;
922         }
923         DBPRINTF ("ret=%d", ret);
924         if(ret == NOTIFY_STOP)
925                 handled_exceptions++;
926
927         return ret;
928 }
929
930 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
931 {
932         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
933         u8 *addr = (u8 *) (regs->EREG (ip) - 1);
934         unsigned long stack_addr = (unsigned long) (kcb->jprobe_saved_esp);
935         struct jprobe *jp = container_of (p, struct jprobe, kp);
936
937         DBPRINTF ("p = %p\n", p);
938
939         if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end))
940         {
941                 if ((unsigned long *)(&regs->EREG(sp)) != kcb->jprobe_saved_esp)
942                 {
943                         struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
944                         printk ("current esp %p does not match saved esp %p\n", &regs->EREG (sp), kcb->jprobe_saved_esp);
945                         printk ("Saved registers for jprobe %p\n", jp);
946                         show_registers (saved_regs);
947                         printk ("Current registers\n");
948                         show_registers (regs);
949                         panic("BUG");
950                         //BUG ();                       
951                 }
952                 *regs = kcb->jprobe_saved_regs;
953                 memcpy ((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, MIN_STACK_SIZE (stack_addr));
954                 preempt_enable_no_resched ();
955                 return 1;
956         }
957 }
958
959 void __kprobes arch_arm_kprobe (struct kprobe *p)
960 {
961         text_poke (p->addr, ((unsigned char[])
962                                 {BREAKPOINT_INSTRUCTION}), 1);
963 }
964
965 void __kprobes arch_disarm_kprobe (struct kprobe *p)
966 {
967         text_poke (p->addr, &p->opcode, 1);
968 }
969
970 void *__kprobes trampoline_probe_handler_x86 (struct pt_regs *regs)
971 {
972         return (void *)trampoline_probe_handler(NULL, regs);
973 }
974
975
976
977
978 /*
979  * Called when the probe at kretprobe trampoline is hit
980  */
981 int __kprobes trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
982 {
983         struct kretprobe_instance *ri = NULL; 
984         struct hlist_head *head, empty_rp; 
985         struct hlist_node *node, *tmp; 
986         unsigned long flags, orig_ret_address = 0;
987         unsigned long trampoline_address = (unsigned long) &kretprobe_trampoline;
988         struct kretprobe *crp = NULL; 
989         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
990
991         DBPRINTF ("start");
992
993         if (p && p->tgid){
994                 // in case of user space retprobe trampoline is at the Nth instruction of US tramp 
995                 trampoline_address = (unsigned long)(p->ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
996         }
997
998         INIT_HLIST_HEAD (&empty_rp); 
999         spin_lock_irqsave (&kretprobe_lock, flags); 
1000         head = kretprobe_inst_table_head (current);
1001
1002         if(!p){ // X86 kernel space
1003                 DBPRINTF ("regs %p", regs);
1004                 /* fixup registers */
1005                 regs->XREG (cs) = __KERNEL_CS | get_kernel_rpl (); 
1006                 regs->EREG (ip) = trampoline_address; 
1007                 regs->ORIG_EAX_REG = 0xffffffff;
1008         }
1009
1010         /*
1011          * It is possible to have multiple instances associated with a given
1012          * task either because an multiple functions in the call path
1013          * have a return probe installed on them, and/or more then one 
1014          * return probe was registered for a target function.
1015          *
1016          * We can handle this because:
1017          *     - instances are always inserted at the head of the list
1018          *     - when multiple return probes are registered for the same
1019          *       function, the first instance's ret_addr will point to the
1020          *       real return address, and all the rest will point to
1021          *       kretprobe_trampoline
1022          */
1023         hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
1024         {
1025                 if (ri->task != current)
1026                         /* another task is sharing our hash bucket */
1027                         continue; 
1028                 if (ri->rp && ri->rp->handler){
1029
1030                         if(!p){ // X86 kernel space
1031                                 __get_cpu_var (current_kprobe) = &ri->rp->kp; 
1032                                 get_kprobe_ctlblk ()->kprobe_status = KPROBE_HIT_ACTIVE;
1033                         }
1034
1035                         ri->rp->handler (ri, regs, ri->rp->priv_arg);
1036
1037                         if(!p) // X86 kernel space
1038                                 __get_cpu_var (current_kprobe) = NULL;
1039
1040                 }
1041
1042                 orig_ret_address = (unsigned long) ri->ret_addr; 
1043                 recycle_rp_inst (ri, &empty_rp); 
1044                 if (orig_ret_address != trampoline_address)
1045                         /*
1046                          * This is the real return address. Any other
1047                          * instances associated with this task are for
1048                          * other calls deeper on the call stack
1049                          */
1050                         break;
1051         }
1052         kretprobe_assert (ri, orig_ret_address, trampoline_address);
1053         //BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
1054         if (trampoline_address != (unsigned long) &kretprobe_trampoline){
1055                 if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
1056                 if (ri->rp) BUG_ON (ri->rp->kp.tgid == 0);
1057                 else if (ri->rp2) BUG_ON (ri->rp2->kp.tgid == 0);
1058         }
1059         if ((ri->rp && ri->rp->kp.tgid) || (ri->rp2 && ri->rp2->kp.tgid)) 
1060                 BUG_ON (trampoline_address == (unsigned long) &kretprobe_trampoline);
1061
1062         if(p){ // X86 user space
1063                 regs->EREG(ip) = orig_ret_address; 
1064                 //printk (" uretprobe regs->eip = 0x%lx\n", regs->EREG(ip));
1065         }
1066
1067         if(p){ // ARM, MIPS, X86 user space
1068                 if (kcb->kprobe_status == KPROBE_REENTER)
1069                         restore_previous_kprobe (kcb);
1070                 else
1071                         reset_current_kprobe ();
1072
1073                 //TODO: test - enter function, delete us retprobe, exit function 
1074                 // for user space retprobes only - deferred deletion
1075                 if (trampoline_address != (unsigned long) &kretprobe_trampoline)
1076                 {
1077                         // if we are not at the end of the list and current retprobe should be disarmed 
1078                         if (node && ri->rp2)
1079                         {
1080                                 crp = ri->rp2;
1081                                 /*sprintf(die_msg, "deferred disarm p->addr = %p [%lx %lx %lx]\n", 
1082                                   crp->kp.addr, *kaddrs[0], *kaddrs[1], *kaddrs[2]);
1083                                   DIE(die_msg, regs); */
1084                                 // look for other instances for the same retprobe
1085                                 hlist_for_each_entry_continue (ri, node, hlist)
1086                                 {
1087                                         if (ri->task != current) 
1088                                                 continue;       /* another task is sharing our hash bucket */
1089                                         if (ri->rp2 == crp)     //if instance belong to the same retprobe
1090                                                 break;
1091                                 }
1092                                 if (!node)
1093                                 {       // if there are no more instances for this retprobe
1094                                         // delete retprobe
1095                                         DBPRINTF ("defered retprobe deletion p->addr = %p", crp->kp.addr);
1096                                         unregister_uprobe (&crp->kp, current, 1);
1097                                         kfree (crp);
1098                                 }
1099                         }
1100                 }
1101         }
1102
1103         spin_unlock_irqrestore (&kretprobe_lock, flags); 
1104         hlist_for_each_entry_safe (ri, node, tmp, &empty_rp, hlist)
1105         {
1106                 hlist_del (&ri->hlist); 
1107                 kfree (ri);
1108         }
1109
1110         if(!p) // X86 kernel space
1111                 return (int)orig_ret_address;
1112
1113         preempt_enable_no_resched ();
1114         /*
1115          * By returning a non-zero value, we are telling
1116          * kprobe_handler() that we don't want the post_handler
1117          * to run (and have re-enabled preemption)
1118          */
1119         return 1;
1120 }
1121
1122 void __kprobes __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
1123 {
1124         struct kretprobe_instance *ri;
1125
1126         DBPRINTF ("start\n");
1127         //TODO: test - remove retprobe after func entry but before its exit
1128         if ((ri = get_free_rp_inst (rp)) != NULL)
1129         {
1130                 ri->rp = rp; 
1131                 ri->rp2 = NULL; 
1132                 ri->task = current;
1133
1134                 /* Replace the return addr with trampoline addr */
1135                 if (rp->kp.tgid){
1136                         unsigned long ra = (unsigned long) (rp->kp.ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);/*, stack[6];
1137                                                                                                                if (!read_proc_vm_atomic (current, regs->EREG(sp), stack, sizeof(stack)))
1138                                                                                                                panic ("failed to read user space func stack %lx!\n", regs->EREG(sp));
1139                                                                                                                printk("stack: %lx %lx %lx %lx %lx %lx\n", stack[0], stack[1], stack[2], stack[3], stack[4], stack[5]);*/
1140                         if (!read_proc_vm_atomic (current, regs->EREG(sp), &(ri->ret_addr), sizeof(ri->ret_addr)))
1141                                 panic ("failed to read user space func ra %lx!\n", regs->EREG(sp));
1142                         if (!write_proc_vm_atomic (current, regs->EREG(sp), &ra, sizeof(ra)))
1143                                 panic ("failed to write user space func ra %lx!\n", regs->EREG(sp));
1144                         //printk("__arch_prepare_kretprobe: ra %lx %p->%lx\n",regs->EREG(sp), ri->ret_addr, ra);
1145                 }
1146                 else {
1147                         unsigned long *sara = (unsigned long *)&regs->EREG(sp);
1148                         ri->ret_addr = (kprobe_opcode_t *)*sara;
1149                         *sara = (unsigned long)&kretprobe_trampoline;
1150                         DBPRINTF ("ra loc %p, origr_ra %p new ra %lx\n", sara, ri->ret_addr, *sara);
1151                 }
1152
1153                 add_rp_inst (ri);
1154         }
1155         else {
1156                 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
1157                 rp->nmissed++;
1158         }
1159 }
1160
1161 int asm_init_module_dependencies()
1162 {
1163         INIT_MOD_DEP_VAR(module_alloc, module_alloc);
1164         INIT_MOD_DEP_VAR(module_free, module_free);
1165         INIT_MOD_DEP_VAR(fixup_exception, fixup_exception);
1166 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
1167 # error this kernel version has no text_poke function which is necessaryf for x86 ach!!!
1168 #else
1169         INIT_MOD_DEP_VAR(text_poke, text_poke);
1170 #endif
1171         INIT_MOD_DEP_VAR(show_registers, show_registers);
1172 #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
1173         INIT_MOD_DEP_VAR(freeze_processes, freeze_processes);
1174         INIT_MOD_DEP_VAR(thaw_processes, thaw_processes);
1175 #endif
1176
1177         return 0;
1178 }
1179
1180 int __init arch_init_kprobes (void)
1181 {
1182         if (!arch_init_module_dependencies())
1183         {
1184                 DBPRINTF ("Unable to init module dependencies\n"); 
1185                 return -1;
1186         }
1187
1188         return register_die_notifier (&kprobe_exceptions_nb);
1189 }
1190
1191 void __exit arch_exit_kprobes (void)
1192 {
1193         unregister_die_notifier (&kprobe_exceptions_nb);
1194 }
1195
1196 EXPORT_SYMBOL_GPL (arch_uprobe_return);
1197 EXPORT_SYMBOL_GPL (arch_exit_kprobes);