[REFACTOR] remove code is not used
[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  * 2012         Stanislav Andreev <s.andreev@samsung.com>: added time debug profiling support; BUG() message fix
46  */
47
48 #include<linux/module.h>
49 #include <linux/kdebug.h>
50
51 #include "dbi_kprobes.h"
52 #include "../dbi_kprobes.h"
53 #include "../../dbi_kprobes.h"
54
55 #include "../../dbi_kdebug.h"
56 #include "../../dbi_insn_slots.h"
57 #include "../../dbi_kprobes_deps.h"
58 #define SUPRESS_BUG_MESSAGES
59
60 extern struct kprobe * per_cpu__current_kprobe;
61 extern struct kprobe * per_cpu__current_kprobe;
62 extern struct kprobe * current_kprobe;
63
64 DECLARE_MOD_FUNC_DEP(module_alloc, void *, unsigned long size);
65 DECLARE_MOD_FUNC_DEP(module_free, void, struct module *mod, void *module_region);
66 DECLARE_MOD_FUNC_DEP(fixup_exception, int, struct pt_regs * regs);
67
68 DECLARE_MOD_FUNC_DEP(freeze_processes, int, void);
69 DECLARE_MOD_FUNC_DEP(thaw_processes, void, void);
70
71 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
72 DECLARE_MOD_FUNC_DEP(text_poke, void, void *addr, unsigned char *opcode, int len);
73 #else
74 DECLARE_MOD_FUNC_DEP(text_poke, void *, void *addr, const void *opcode, size_t len);
75 #endif
76 DECLARE_MOD_FUNC_DEP(show_registers, void, struct pt_regs * regs);
77
78 DECLARE_MOD_DEP_WRAPPER (module_alloc, void *, unsigned long size)
79 IMP_MOD_DEP_WRAPPER (module_alloc, size)
80
81 DECLARE_MOD_DEP_WRAPPER (module_free, void, struct module *mod, void *module_region)
82 IMP_MOD_DEP_WRAPPER (module_free, mod, module_region)
83
84 DECLARE_MOD_DEP_WRAPPER (fixup_exception, int, struct pt_regs * regs)
85 IMP_MOD_DEP_WRAPPER (fixup_exception, regs)
86
87 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
88 DECLARE_MOD_DEP_WRAPPER(text_poke, \
89                         void, void *addr, unsigned char *opcode, int len)
90 #else
91 DECLARE_MOD_DEP_WRAPPER(text_poke, \
92                         void *, void *addr, const void *opcode, size_t len)
93 #endif
94 IMP_MOD_DEP_WRAPPER(text_poke, addr, opcode, len)
95
96 DECLARE_MOD_DEP_WRAPPER(show_registers, void, struct pt_regs * regs)
97 IMP_MOD_DEP_WRAPPER(show_registers, regs)
98
99 /*
100  * Function return probe trampoline:
101  *      - init_kprobes() establishes a probepoint here
102  *      - When the probed function returns, this probe
103  *        causes the handlers to fire
104  */
105 static __used void kretprobe_trampoline_holder(void)
106 {
107         asm volatile(".global kretprobe_trampoline\n"
108                         "kretprobe_trampoline:\n"
109                         "       pushf\n"
110                         /* skip cs, ip, orig_ax and gs. */
111                         "       subl $16, %esp\n"
112                         "       pushl %fs\n"
113                         "       pushl %es\n"
114                         "       pushl %ds\n"
115                         "       pushl %eax\n"
116                         "       pushl %ebp\n"
117                         "       pushl %edi\n"
118                         "       pushl %esi\n"
119                         "       pushl %edx\n"
120                         "       pushl %ecx\n"
121                         "       pushl %ebx\n"
122                         "       movl %esp, %eax\n"
123                         "       call trampoline_probe_handler_x86\n"
124                         /* move eflags to cs */
125                         "       movl 56(%esp), %edx\n"
126                         "       movl %edx, 52(%esp)\n"
127                         /* replace saved flags with true return address. */
128                         "       movl %eax, 56(%esp)\n"
129                         "       popl %ebx\n" ""
130                         "       popl %ecx\n"
131                         "       popl %edx\n"
132                         "       popl %esi\n"
133                         "       popl %edi\n"
134                         "       popl %ebp\n"
135                         "       popl %eax\n"
136                         /* skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/
137                         "       addl $24, %esp\n"
138                         "       popf\n"
139                         "       ret\n");
140 }
141
142 void kretprobe_trampoline(void);
143
144 /* insert a jmp code */
145 static __always_inline void set_jmp_op (void *from, void *to)
146 {
147         struct __arch_jmp_op
148         {
149                 char op;
150                 long raddr;
151         } __attribute__ ((packed)) * jop;
152         jop = (struct __arch_jmp_op *) from;
153         jop->raddr = (long) (to) - ((long) (from) + 5);
154         jop->op = RELATIVEJUMP_INSTRUCTION;
155 }
156
157 /*
158  * returns non-zero if opcodes can be boosted.
159  */
160 int can_boost(kprobe_opcode_t *opcodes)
161 {
162 #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf)                \
163         (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
164           (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
165           (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
166           (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
167          << (row % 32))
168         /*
169          * Undefined/reserved opcodes, conditional jump, Opcode Extension
170          * Groups, and some special opcodes can not be boost.
171          */
172         static const unsigned long twobyte_is_boostable[256 / 32] = {
173                 /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
174                 /*      -------------------------------         */
175                 W (0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) |      /* 00 */
176                         W (0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),       /* 10 */
177                 W (0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |      /* 20 */
178                         W (0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),       /* 30 */
179                 W (0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) |      /* 40 */
180                         W (0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),       /* 50 */
181                 W (0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) |      /* 60 */
182                         W (0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1),       /* 70 */
183                 W (0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |      /* 80 */
184                         W (0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),       /* 90 */
185                 W (0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) |      /* a0 */
186                         W (0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1),       /* b0 */
187                 W (0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) |      /* c0 */
188                         W (0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1),       /* d0 */
189                 W (0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) |      /* e0 */
190                         W (0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0)        /* f0 */
191                         /*      -------------------------------         */
192                         /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
193         };
194 #undef W
195         kprobe_opcode_t opcode;
196         kprobe_opcode_t *orig_opcodes = opcodes;
197 retry:
198         if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
199                 return 0;
200         opcode = *(opcodes++);
201
202         /* 2nd-byte opcode */
203         if (opcode == 0x0f)
204         {
205                 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
206                         return 0;
207                 return test_bit (*opcodes, twobyte_is_boostable);
208         }
209
210         switch (opcode & 0xf0)
211         {
212                 case 0x60:
213                         if (0x63 < opcode && opcode < 0x67)
214                                 goto retry;     /* prefixes */
215                         /* can't boost Address-size override and bound */
216                         return (opcode != 0x62 && opcode != 0x67);
217                 case 0x70:
218                         return 0;       /* can't boost conditional jump */
219                 case 0xc0:
220                         /* can't boost software-interruptions */
221                         return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
222                 case 0xd0:
223                         /* can boost AA* and XLAT */
224                         return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
225                 case 0xe0:
226                         /* can boost in/out and absolute jmps */
227                         return ((opcode & 0x04) || opcode == 0xea);
228                 case 0xf0:
229                         if ((opcode & 0x0c) == 0 && opcode != 0xf1)
230                                 goto retry;     /* lock/rep(ne) prefix */
231                         /* clear and set flags can be boost */
232                         return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
233                 default:
234                         if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
235                                 goto retry;     /* prefixes */
236                         /* can't boost CS override and call */
237                         return (opcode != 0x2e && opcode != 0x9a);
238         }
239 }
240 EXPORT_SYMBOL_GPL(can_boost);
241
242 /*
243  * returns non-zero if opcode modifies the interrupt flag.
244  */
245 static int is_IF_modifier (kprobe_opcode_t opcode)
246 {
247         switch (opcode)
248         {
249                 case 0xfa:              /* cli */
250                 case 0xfb:              /* sti */
251                 case 0xcf:              /* iret/iretd */
252                 case 0x9d:              /* popf/popfd */
253                         return 1;
254         }
255         return 0;
256 }
257
258 int arch_prepare_kprobe(struct kprobe *p, struct slot_manager *sm)
259 {
260         kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
261
262         int ret = 0;
263
264         if ((unsigned long) p->addr & 0x01)
265         {
266                 DBPRINTF ("Attempt to register kprobe at an unaligned address\n");
267                 //ret = -EINVAL;
268         }
269
270
271         if (!ret)
272         {
273                 kprobe_opcode_t insn[MAX_INSN_SIZE];
274                 struct arch_specific_insn ainsn;
275                 /* insn: must be on special executable page on i386. */
276                 p->ainsn.insn = alloc_insn_slot(sm);
277                 if (!p->ainsn.insn)
278                         return -ENOMEM;
279                 memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
280                 ainsn.insn = insn;
281                 ret = arch_check_insn (&ainsn);
282                 if (!ret)
283                 {
284                         p->opcode = *p->addr;
285                 }
286
287                 if (can_boost (p->addr))
288                         p->ainsn.boostable = 0;
289                 else
290                         p->ainsn.boostable = -1;
291                 memcpy (p->ainsn.insn, insn, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
292         }
293         else
294         {
295                 free_insn_slot(sm, p->ainsn.insn);
296         }
297
298         return ret;
299 }
300
301 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
302 {
303         if(p->ss_addr)
304         {
305                 regs->EREG (ip) = (unsigned long)p->ss_addr;
306                 p->ss_addr = NULL;
307         }
308         else
309         {
310                 regs->EREG (flags) |= TF_MASK;
311                 regs->EREG (flags) &= ~IF_MASK;
312                 /*single step inline if the instruction is an int3 */
313                 if (p->opcode == BREAKPOINT_INSTRUCTION){
314                         regs->EREG (ip) = (unsigned long) p->addr;
315                         //printk("break_insn!!!\n");
316                 }
317                 else
318                         regs->EREG (ip) = (unsigned long) p->ainsn.insn;
319         }
320 }
321 EXPORT_SYMBOL_GPL(prepare_singlestep);
322
323 void save_previous_kprobe (struct kprobe_ctlblk *kcb, struct kprobe *cur_p)
324 {
325         if (kcb->prev_kprobe.kp != NULL)
326         {
327                 panic("no space to save new probe[]: task = %d/%s, prev %p, current %p, new %p,",
328                                 current->pid, current->comm, kcb->prev_kprobe.kp->addr,
329                                 kprobe_running()->addr, cur_p->addr);
330         }
331
332
333         kcb->prev_kprobe.kp = kprobe_running();
334         kcb->prev_kprobe.status = kcb->kprobe_status;
335
336 }
337
338 void restore_previous_kprobe (struct kprobe_ctlblk *kcb)
339 {
340         __get_cpu_var (current_kprobe) = kcb->prev_kprobe.kp;
341         kcb->kprobe_status = kcb->prev_kprobe.status;
342         kcb->prev_kprobe.kp = NULL;
343         kcb->prev_kprobe.status = 0;
344 }
345
346 void set_current_kprobe (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
347 {
348         __get_cpu_var (current_kprobe) = p;
349         DBPRINTF ("set_current_kprobe[]: p=%p addr=%p\n", p, p->addr);
350         kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags = (regs->EREG (flags) & (TF_MASK | IF_MASK));
351         if (is_IF_modifier (p->opcode))
352                 kcb->kprobe_saved_eflags &= ~IF_MASK;
353 }
354
355 int kprobe_handler (struct pt_regs *regs)
356 {
357         struct kprobe *p = 0;
358         int ret = 0, reenter = 0;
359         kprobe_opcode_t *addr = NULL;
360         struct kprobe_ctlblk *kcb;
361 #ifdef SUPRESS_BUG_MESSAGES
362         int swap_oops_in_progress;
363 #endif
364
365         /* We're in an interrupt, but this is clear and BUG()-safe. */
366         addr = (kprobe_opcode_t *) (regs->EREG (ip) - sizeof (kprobe_opcode_t));
367         DBPRINTF ("KPROBE: regs->eip = 0x%lx addr = 0x%p\n", regs->EREG (ip), addr);
368 #ifdef SUPRESS_BUG_MESSAGES
369         // oops_in_progress used to avoid BUG() messages that slow down kprobe_handler() execution
370         swap_oops_in_progress = oops_in_progress;
371         oops_in_progress = 1;
372 #endif
373         preempt_disable ();
374
375         kcb = get_kprobe_ctlblk ();
376
377         /* Check we're not actually recursing */
378         if (kprobe_running()) {
379                 p = get_kprobe(addr);
380                 if (p) {
381                         if (kcb->kprobe_status == KPROBE_HIT_SS && *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
382                                 regs->EREG(flags) &= ~TF_MASK;
383                                 regs->EREG(flags) |= kcb->kprobe_saved_eflags;
384                                 goto no_kprobe;
385                         }
386
387
388                         /* We have reentered the kprobe_handler(), since
389                          * another probe was hit while within the handler.
390                          * We here save the original kprobes variables and
391                          * just single step on the instruction of the new probe
392                          * without calling any user handlers.
393                          */
394                         save_previous_kprobe (kcb, p);
395                         set_current_kprobe (p, regs, kcb);
396                         kprobes_inc_nmissed_count (p);
397                         prepare_singlestep (p, regs);
398                         kcb->kprobe_status = KPROBE_REENTER;
399                         // FIXME should we enable preemption here??...
400                         //preempt_enable_no_resched ();
401 #ifdef SUPRESS_BUG_MESSAGES
402                         oops_in_progress = swap_oops_in_progress;
403 #endif
404                         return 1;
405                 } else {
406                         if (*addr != BREAKPOINT_INSTRUCTION) {
407                                 /* The breakpoint instruction was removed by
408                                  * another cpu right after we hit, no further
409                                  * handling of this interrupt is appropriate
410                                  */
411                                 regs->EREG(ip) -= sizeof(kprobe_opcode_t);
412                                 ret = 1;
413                                 goto no_kprobe;
414                         }
415
416                         p = __get_cpu_var(current_kprobe);
417                         if (p->break_handler && p->break_handler(p, regs))
418                                 goto ss_probe;
419
420                         goto no_kprobe;
421                 }
422         }
423
424         DBPRINTF ("get_kprobe %p", addr);
425         if (!p)
426                 p = get_kprobe(addr);
427
428         if (!p) {
429                 if (*addr != BREAKPOINT_INSTRUCTION) {
430                         /*
431                          * The breakpoint instruction was removed right
432                          * after we hit it.  Another cpu has removed
433                          * either a probepoint or a debugger breakpoint
434                          * at this address.  In either case, no further
435                          * handling of this interrupt is appropriate.
436                          * Back up over the (now missing) int3 and run
437                          * the original instruction.
438                          */
439                         regs->EREG(ip) -= sizeof(kprobe_opcode_t);
440                         ret = 1;
441                 }
442
443                 if (!p) {
444                         /* Not one of ours: let kernel handle it */
445                         DBPRINTF ("no_kprobe");
446                         goto no_kprobe;
447                 }
448         }
449
450         set_current_kprobe (p, regs, kcb);
451
452         if(!reenter)
453                 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
454
455         if (p->pre_handler)
456                 ret = p->pre_handler(p, regs);
457
458         if (ret)
459         {
460                 if (ret == 2) { // we have alreadyc called the handler, so just single step the instruction
461                         DBPRINTF ("p->pre_handler[] 2");
462                         goto ss_probe;
463                 }
464                 DBPRINTF ("p->pre_handler[] 1");
465                 // FIXME should we enable preemption here??...
466                 //preempt_enable_no_resched ();
467 #ifdef SUPRESS_BUG_MESSAGES
468                 oops_in_progress = swap_oops_in_progress;
469 #endif
470                 /* handler has already set things up, so skip ss setup */
471                 prepare_singlestep(p, regs);
472                 return 1;
473         }
474         DBPRINTF ("p->pre_handler[] 0");
475
476 ss_probe:
477         DBPRINTF ("p = %p\n", p);
478         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);
479
480 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
481         if (p->ainsn.boostable == 1 && !p->post_handler)
482         {
483                 /* Boost up -- we can execute copied instructions directly */
484                 reset_current_kprobe ();
485                 regs->EREG (ip) = (unsigned long) p->ainsn.insn;
486                 preempt_enable_no_resched ();
487 #ifdef SUPRESS_BUG_MESSAGES
488                 oops_in_progress = swap_oops_in_progress;
489 #endif
490                 return 1;
491         }
492 #endif // !CONFIG_PREEMPT
493         prepare_singlestep (p, regs);
494         kcb->kprobe_status = KPROBE_HIT_SS;
495         // FIXME should we enable preemption here??...
496         //preempt_enable_no_resched ();
497 #ifdef SUPRESS_BUG_MESSAGES
498         oops_in_progress = swap_oops_in_progress;
499 #endif
500         return 1;
501
502 no_kprobe:
503
504         preempt_enable_no_resched ();
505 #ifdef SUPRESS_BUG_MESSAGES
506         oops_in_progress = swap_oops_in_progress;
507 #endif
508         return ret;
509 }
510
511 int setjmp_pre_handler (struct kprobe *p, struct pt_regs *regs)
512 {
513         struct jprobe *jp = container_of (p, struct jprobe, kp);
514         kprobe_pre_entry_handler_t pre_entry;
515         entry_point_t entry;
516
517         unsigned long addr, args[6];
518         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
519
520         DBPRINTF ("setjmp_pre_handler %p:%d", p->addr, p->tgid);
521         pre_entry = (kprobe_pre_entry_handler_t) jp->pre_entry;
522         entry = (entry_point_t) jp->entry;
523
524         /* handle __switch_to probe */
525         if ((p->addr == sched_addr) && sched_rp) {
526                 /* FIXME: Actually 2nd parameter is not used for x86 */
527                 patch_suspended_task(sched_rp, (struct task_struct *)regs->dx, regs);
528         }
529
530         kcb->jprobe_saved_regs = *regs;
531         kcb->jprobe_saved_esp = &regs->EREG(sp);
532         addr = (unsigned long)(kcb->jprobe_saved_esp);
533
534         /* TBD: As Linus pointed out, gcc assumes that the callee
535          * owns the argument space and could overwrite it, e.g.
536          * tailcall optimization. So, to be absolutely safe
537          * we also save and restore enough stack bytes to cover
538          * the argument area. */
539         memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, MIN_STACK_SIZE (addr));
540         regs->EREG(flags) &= ~IF_MASK;
541 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
542         trace_hardirqs_off();
543 #endif
544         if (pre_entry)
545                 p->ss_addr = pre_entry(jp->priv_arg, regs);
546
547         regs->EREG(ip) = (unsigned long)(jp->entry);
548
549         return 1;
550 }
551
552 void dbi_jprobe_return (void)
553 {
554         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
555
556         asm volatile("       xchgl   %%ebx,%%esp     \n"
557                         "       int3                    \n"
558                         "       .globl dbi_jprobe_return_end    \n"
559                         "       dbi_jprobe_return_end:  \n"
560                         "       nop                     \n"::"b" (kcb->jprobe_saved_esp):"memory");
561 }
562
563 void arch_ujprobe_return(void)
564 {
565 }
566
567 /*
568  * Called after single-stepping.  p->addr is the address of the
569  * instruction whose first byte has been replaced by the "int 3"
570  * instruction.  To avoid the SMP problems that can occur when we
571  * temporarily put back the original opcode to single-step, we
572  * single-stepped a copy of the instruction.  The address of this
573  * copy is p->ainsn.insn.
574  *
575  * This function prepares to return from the post-single-step
576  * interrupt.  We have to fix up the stack as follows:
577  *
578  * 0) Except in the case of absolute or indirect jump or call instructions,
579  * the new eip is relative to the copied instruction.  We need to make
580  * it relative to the original instruction.
581  *
582  * 1) If the single-stepped instruction was pushfl, then the TF and IF
583  * flags are set in the just-pushed eflags, and may need to be cleared.
584  *
585  * 2) If the single-stepped instruction was a call, the return address
586  * that is atop the stack is the address following the copied instruction.
587  * We need to make it the address following the original instruction.
588  *
589  * This function also checks instruction size for preparing direct execution.
590  */
591 static void resume_execution (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
592 {
593         unsigned long *tos, tos_dword = 0;
594         unsigned long copy_eip = (unsigned long) p->ainsn.insn;
595         unsigned long orig_eip = (unsigned long) p->addr;
596         kprobe_opcode_t insns[2];
597
598         regs->EREG (flags) &= ~TF_MASK;
599
600         tos = (unsigned long *)&regs->EREG(sp);
601         insns[0] = p->ainsn.insn[0];
602         insns[1] = p->ainsn.insn[1];
603
604         switch (insns[0])
605         {
606                 case 0x9c:              /* pushfl */
607                         *tos &= ~(TF_MASK | IF_MASK);
608                         *tos |= kcb->kprobe_old_eflags;
609                         break;
610                 case 0xc2:              /* iret/ret/lret */
611                 case 0xc3:
612                 case 0xca:
613                 case 0xcb:
614                 case 0xcf:
615                 case 0xea:              /* jmp absolute -- eip is correct */
616                         /* eip is already adjusted, no more changes required */
617                         p->ainsn.boostable = 1;
618                         goto no_change;
619                 case 0xe8:              /* call relative - Fix return addr */
620                         *tos = orig_eip + (*tos - copy_eip);
621                         break;
622                 case 0x9a:              /* call absolute -- same as call absolute, indirect */
623                         *tos = orig_eip + (*tos - copy_eip);
624                         goto no_change;
625                 case 0xff:
626                         if ((insns[1] & 0x30) == 0x10)
627                         {
628                                 /*
629                                  * call absolute, indirect
630                                  * Fix return addr; eip is correct.
631                                  * But this is not boostable
632                                  */
633                                 *tos = orig_eip + (*tos - copy_eip);
634                                 goto no_change;
635                         }
636                         else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
637                                         ((insns[1] & 0x31) == 0x21))
638                         {               /* jmp far, absolute indirect */
639                                 /* eip is correct. And this is boostable */
640                                 p->ainsn.boostable = 1;
641                                 goto no_change;
642                         }
643                 default:
644                         break;
645         }
646
647         if (p->ainsn.boostable == 0)
648         {
649                 if ((regs->EREG (ip) > copy_eip) && (regs->EREG (ip) - copy_eip) + 5 < MAX_INSN_SIZE)
650                 {
651                         /*
652                          * These instructions can be executed directly if it
653                          * jumps back to correct address.
654                          */
655                         set_jmp_op((void *)regs->EREG(ip), (void *)orig_eip + (regs->EREG(ip) - copy_eip));
656                         p->ainsn.boostable = 1;
657                 }
658                 else
659                 {
660                         p->ainsn.boostable = -1;
661                 }
662         }
663
664         regs->EREG (ip) = orig_eip + (regs->EREG (ip) - copy_eip);
665
666 no_change:
667         return;
668 }
669
670 /*
671  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
672  * remain disabled thoroughout this function.
673  */
674 static int post_kprobe_handler (struct pt_regs *regs)
675 {
676         struct kprobe *cur = kprobe_running ();
677         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
678
679         if (!cur)
680                 return 0;
681         if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler)
682         {
683                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
684                 cur->post_handler (cur, regs, 0);
685         }
686
687         resume_execution (cur, regs, kcb);
688         regs->EREG (flags) |= kcb->kprobe_saved_eflags;
689 #ifndef CONFIG_X86
690         trace_hardirqs_fixup_flags (regs->EREG (flags));
691 #endif // CONFIG_X86
692         /*Restore back the original saved kprobes variables and continue. */
693         if (kcb->kprobe_status == KPROBE_REENTER)
694         {
695                 restore_previous_kprobe (kcb);
696                 goto out;
697         }
698         reset_current_kprobe ();
699 out:
700         preempt_enable_no_resched ();
701
702         /*
703          * if somebody else is singlestepping across a probe point, eflags
704          * will have TF set, in which case, continue the remaining processing
705          * of do_debug, as if this is not a probe hit.
706          */
707         if (regs->EREG (flags) & TF_MASK)
708                 return 0;
709
710         return 1;
711 }
712
713 int kprobe_fault_handler (struct pt_regs *regs, int trapnr)
714 {
715         struct kprobe *cur = kprobe_running ();
716         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
717
718         switch (kcb->kprobe_status)
719         {
720                 case KPROBE_HIT_SS:
721                 case KPROBE_REENTER:
722                         /*
723                          * We are here because the instruction being single
724                          * stepped caused a page fault. We reset the current
725                          * kprobe and the eip points back to the probe address
726                          * and allow the page fault handler to continue as a
727                          * normal page fault.
728                          */
729                         regs->EREG (ip) = (unsigned long) cur->addr;
730                         regs->EREG (flags) |= kcb->kprobe_old_eflags;
731                         if (kcb->kprobe_status == KPROBE_REENTER)
732                                 restore_previous_kprobe (kcb);
733                         else
734                                 reset_current_kprobe ();
735                         preempt_enable_no_resched ();
736                         break;
737                 case KPROBE_HIT_ACTIVE:
738                 case KPROBE_HIT_SSDONE:
739                         /*
740                          * We increment the nmissed count for accounting,
741                          * we can also use npre/npostfault count for accouting
742                          * these specific fault cases.
743                          */
744                         kprobes_inc_nmissed_count (cur);
745
746                         /*
747                          * We come here because instructions in the pre/post
748                          * handler caused the page_fault, this could happen
749                          * if handler tries to access user space by
750                          * copy_from_user(), get_user() etc. Let the
751                          * user-specified handler try to fix it first.
752                          */
753                         if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr))
754                                 return 1;
755
756                         /*
757                          * In case the user-specified fault handler returned
758                          * zero, try to fix up.
759                          */
760                         if (fixup_exception (regs))
761                                 return 1;
762
763                         /*
764                          * fixup_exception() could not handle it,
765                          * Let do_page_fault() fix it.
766                          */
767                         break;
768                 default:
769                         break;
770         }
771         return 0;
772 }
773
774 int kprobe_exceptions_notify (struct notifier_block *self, unsigned long val, void *data)
775 {
776         struct die_args *args = (struct die_args *) data;
777         int ret = NOTIFY_DONE;
778
779         DBPRINTF ("val = %ld, data = 0x%X", val, (unsigned int) data);
780
781         if (args->regs && user_mode_vm(args->regs))
782                 return ret;
783
784         DBPRINTF ("switch (val) %lu %d %d", val, DIE_INT3, DIE_TRAP);
785         switch (val)
786         {
787 #ifdef CONFIG_KPROBES
788                 case DIE_INT3:
789 #else
790                 case DIE_TRAP:
791 #endif
792                         DBPRINTF ("before kprobe_handler ret=%d %p", ret, args->regs);
793                         if (kprobe_handler (args->regs))
794                                 ret = NOTIFY_STOP;
795                         DBPRINTF ("after kprobe_handler ret=%d %p", ret, args->regs);
796                         break;
797                 case DIE_DEBUG:
798                         if (post_kprobe_handler (args->regs))
799                                 ret = NOTIFY_STOP;
800                         break;
801                 case DIE_GPF:
802                         // kprobe_running() needs smp_processor_id()
803                         preempt_disable ();
804                         if (kprobe_running () && kprobe_fault_handler (args->regs, args->trapnr))
805                                 ret = NOTIFY_STOP;
806                         preempt_enable ();
807                         break;
808                 default:
809                         break;
810         }
811         DBPRINTF ("ret=%d", ret);
812         /* if(ret == NOTIFY_STOP) */
813         /*      handled_exceptions++; */
814
815         return ret;
816 }
817
818 static struct notifier_block kprobe_exceptions_nb = {
819         .notifier_call = kprobe_exceptions_notify,
820         .priority = INT_MAX
821 };
822
823 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
824 {
825         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
826         u8 *addr = (u8 *) (regs->EREG (ip) - 1);
827         unsigned long stack_addr = (unsigned long) (kcb->jprobe_saved_esp);
828         struct jprobe *jp = container_of (p, struct jprobe, kp);
829
830         DBPRINTF ("p = %p\n", p);
831
832         if ((addr > (u8 *) dbi_jprobe_return) && (addr < (u8 *) dbi_jprobe_return_end))
833         {
834                 if ((unsigned long *)(&regs->EREG(sp)) != kcb->jprobe_saved_esp)
835                 {
836                         struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
837                         printk ("current esp %p does not match saved esp %p\n", &regs->EREG (sp), kcb->jprobe_saved_esp);
838                         printk ("Saved registers for jprobe %p\n", jp);
839                         show_registers (saved_regs);
840                         printk ("Current registers\n");
841                         show_registers (regs);
842                         panic("BUG");
843                         //BUG ();
844                 }
845                 *regs = kcb->jprobe_saved_regs;
846                 memcpy ((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, MIN_STACK_SIZE (stack_addr));
847                 preempt_enable_no_resched ();
848                 return 1;
849         }
850 }
851
852 void arch_arm_kprobe (struct kprobe *p)
853 {
854         text_poke (p->addr, ((unsigned char[])
855                                 {BREAKPOINT_INSTRUCTION}), 1);
856 }
857
858 void arch_disarm_kprobe (struct kprobe *p)
859 {
860         text_poke (p->addr, &p->opcode, 1);
861 }
862
863 static __used void *trampoline_probe_handler_x86(struct pt_regs *regs)
864 {
865         return (void *)trampoline_probe_handler(NULL, regs);
866 }
867
868 void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
869 {
870         unsigned long *sara = (unsigned long *)&regs->EREG(sp);
871         ri->ret_addr = (kprobe_opcode_t *)*sara;
872         ri->sp = &regs->EREG(sp);
873
874         /* Replace the return addr with trampoline addr */
875         *sara = (unsigned long)&kretprobe_trampoline;
876 }
877
878 int arch_init_module_deps()
879 {
880         INIT_MOD_DEP_VAR(module_alloc, module_alloc);
881         INIT_MOD_DEP_VAR(module_free, module_free);
882         INIT_MOD_DEP_VAR(fixup_exception, fixup_exception);
883 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
884 # error this kernel version has no text_poke function which is necessaryf for x86 ach!!!
885 #else
886         INIT_MOD_DEP_VAR(text_poke, text_poke);
887 #endif
888         INIT_MOD_DEP_VAR(show_registers, show_registers);
889 #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
890         INIT_MOD_DEP_VAR(freeze_processes, freeze_processes);
891         INIT_MOD_DEP_VAR(thaw_processes, thaw_processes);
892 #endif
893
894         return 0;
895 }
896
897 int arch_init_kprobes(void)
898 {
899         return register_die_notifier (&kprobe_exceptions_nb);
900 }
901
902 void arch_exit_kprobes(void)
903 {
904         unregister_die_notifier (&kprobe_exceptions_nb);
905 }