2 * Kernel Probes (KProbes)
3 * arch/x86/kernel/kprobes.c
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.
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.
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.
19 * Copyright (C) IBM Corporation, 2002, 2004
23 * Dynamic Binary Instrumentation Module based on KProbes
24 * modules/kprobe/arch/asm-x86/dbi_kprobes.c
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.
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.
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.
40 * Copyright (C) Samsung Electronics, 2006-2010
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
48 #include<linux/module.h>
49 #include <linux/kdebug.h>
51 #include "dbi_kprobes.h"
52 #include "../dbi_kprobes.h"
53 #include "../../dbi_kprobes.h"
55 #include "../../dbi_kdebug.h"
56 #include "../../dbi_insn_slots.h"
57 #include "../../dbi_kprobes_deps.h"
58 #define SUPRESS_BUG_MESSAGES
60 extern struct kprobe * per_cpu__current_kprobe;
62 extern struct kprobe * per_cpu__current_kprobe;
64 extern struct kprobe * current_kprobe;
66 #define SAVE_REGS_STRING \
67 /* Skip cs, ip, orig_ax. */ \
84 #define RESTORE_REGS_STRING \
100 /* Skip orig_ax, ip, cs */ \
103 DECLARE_MOD_FUNC_DEP(module_alloc, void *, unsigned long size);
104 DECLARE_MOD_FUNC_DEP(module_free, void, struct module *mod, void *module_region);
105 DECLARE_MOD_FUNC_DEP(fixup_exception, int, struct pt_regs * regs);
107 DECLARE_MOD_FUNC_DEP(freeze_processes, int, void);
108 DECLARE_MOD_FUNC_DEP(thaw_processes, void, void);
110 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
111 DECLARE_MOD_FUNC_DEP(text_poke, void, void *addr, unsigned char *opcode, int len);
113 DECLARE_MOD_FUNC_DEP(text_poke, void *, void *addr, const void *opcode, size_t len);
115 DECLARE_MOD_FUNC_DEP(show_registers, void, struct pt_regs * regs);
117 DECLARE_MOD_DEP_WRAPPER (module_alloc, void *, unsigned long size)
118 IMP_MOD_DEP_WRAPPER (module_alloc, size)
120 DECLARE_MOD_DEP_WRAPPER (module_free, void, struct module *mod, void *module_region)
121 IMP_MOD_DEP_WRAPPER (module_free, mod, module_region)
123 DECLARE_MOD_DEP_WRAPPER (fixup_exception, int, struct pt_regs * regs)
124 IMP_MOD_DEP_WRAPPER (fixup_exception, regs)
126 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
127 DECLARE_MOD_DEP_WRAPPER(text_poke, \
128 void, void *addr, unsigned char *opcode, int len)
130 DECLARE_MOD_DEP_WRAPPER(text_poke, \
131 void *, void *addr, const void *opcode, size_t len)
133 IMP_MOD_DEP_WRAPPER(text_poke, addr, opcode, len)
135 DECLARE_MOD_DEP_WRAPPER(show_registers, void, struct pt_regs * regs)
136 IMP_MOD_DEP_WRAPPER(show_registers, regs)
139 * Function return probe trampoline:
140 * - init_kprobes() establishes a probepoint here
141 * - When the probed function returns, this probe
142 * causes the handlers to fire
144 static __used void kretprobe_trampoline_holder(void)
146 asm volatile(".global kretprobe_trampoline\n"
147 "kretprobe_trampoline:\n"
149 /* skip cs, eip, orig_eax */
162 " call trampoline_probe_handler_x86\n"
163 /* move eflags to cs */
164 " movl 52(%esp), %edx\n"
165 " movl %edx, 48(%esp)\n"
166 /* save true return address on eflags */
167 " movl %eax, 52(%esp)\n"
175 /* skip eip, orig_eax, es, ds, fs */
181 void kretprobe_trampoline(void);
183 struct kprobe trampoline_p =
185 .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
186 .pre_handler = trampoline_probe_handler
189 /* insert a jmp code */
190 static __always_inline void set_jmp_op (void *from, void *to)
196 } __attribute__ ((packed)) * jop;
197 jop = (struct __arch_jmp_op *) from;
198 jop->raddr = (long) (to) - ((long) (from) + 5);
199 jop->op = RELATIVEJUMP_INSTRUCTION;
202 static void set_user_jmp_op (void *from, void *to)
208 } __attribute__ ((packed)) jop;
209 //jop = (struct __arch_jmp_op *) from;
210 jop.raddr = (long) (to) - ((long) (from) + 5);
211 jop.op = RELATIVEJUMP_INSTRUCTION;
212 if (!write_proc_vm_atomic (current, (unsigned long)from, &jop, sizeof(jop)))
213 panic ("failed to write jump opcode to user space %p!\n", from);
217 * returns non-zero if opcodes can be boosted.
219 static __always_inline int can_boost (kprobe_opcode_t * opcodes)
221 #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
222 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
223 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
224 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
225 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
228 * Undefined/reserved opcodes, conditional jump, Opcode Extension
229 * Groups, and some special opcodes can not be boost.
231 static const unsigned long twobyte_is_boostable[256 / 32] = {
232 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
233 /* ------------------------------- */
234 W (0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
235 W (0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), /* 10 */
236 W (0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
237 W (0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), /* 30 */
238 W (0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
239 W (0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), /* 50 */
240 W (0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
241 W (0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1), /* 70 */
242 W (0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
243 W (0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), /* 90 */
244 W (0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
245 W (0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1), /* b0 */
246 W (0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
247 W (0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1), /* d0 */
248 W (0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
249 W (0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
250 /* ------------------------------- */
251 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
254 kprobe_opcode_t opcode;
255 kprobe_opcode_t *orig_opcodes = opcodes;
257 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
259 opcode = *(opcodes++);
261 /* 2nd-byte opcode */
264 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
266 return test_bit (*opcodes, twobyte_is_boostable);
269 switch (opcode & 0xf0)
272 if (0x63 < opcode && opcode < 0x67)
273 goto retry; /* prefixes */
274 /* can't boost Address-size override and bound */
275 return (opcode != 0x62 && opcode != 0x67);
277 return 0; /* can't boost conditional jump */
279 /* can't boost software-interruptions */
280 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
282 /* can boost AA* and XLAT */
283 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
285 /* can boost in/out and absolute jmps */
286 return ((opcode & 0x04) || opcode == 0xea);
288 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
289 goto retry; /* lock/rep(ne) prefix */
290 /* clear and set flags can be boost */
291 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
293 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
294 goto retry; /* prefixes */
295 /* can't boost CS override and call */
296 return (opcode != 0x2e && opcode != 0x9a);
301 * returns non-zero if opcode modifies the interrupt flag.
303 static int is_IF_modifier (kprobe_opcode_t opcode)
309 case 0xcf: /* iret/iretd */
310 case 0x9d: /* popf/popfd */
316 int arch_check_insn (struct arch_specific_insn *ainsn)
318 DBPRINTF("Warrning: arch_check_insn is not implemented for x86\n");
322 int arch_prepare_kprobe(struct kprobe *p, struct slot_manager *sm)
324 kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
328 if ((unsigned long) p->addr & 0x01)
330 DBPRINTF ("Attempt to register kprobe at an unaligned address\n");
337 kprobe_opcode_t insn[MAX_INSN_SIZE];
338 struct arch_specific_insn ainsn;
339 /* insn: must be on special executable page on i386. */
340 p->ainsn.insn = alloc_insn_slot(sm);
343 memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
345 ret = arch_check_insn (&ainsn);
348 p->opcode = *p->addr;
351 if (can_boost (p->addr))
352 p->ainsn.boostable = 0;
354 p->ainsn.boostable = -1;
355 memcpy (p->ainsn.insn, insn, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
359 free_insn_slot(sm, p->ainsn.insn);
365 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
369 regs->EREG (ip) = (unsigned long)p->ss_addr;
374 regs->EREG (flags) |= TF_MASK;
375 regs->EREG (flags) &= ~IF_MASK;
376 /*single step inline if the instruction is an int3 */
377 if (p->opcode == BREAKPOINT_INSTRUCTION){
378 regs->EREG (ip) = (unsigned long) p->addr;
379 //printk("break_insn!!!\n");
382 regs->EREG (ip) = (unsigned long) p->ainsn.insn;
387 void save_previous_kprobe (struct kprobe_ctlblk *kcb, struct kprobe *cur_p)
389 if (kcb->prev_kprobe.kp != NULL)
391 panic("no space to save new probe[]: task = %d/%s, prev %p, current %p, new %p,",
392 current->pid, current->comm, kcb->prev_kprobe.kp->addr,
393 kprobe_running()->addr, cur_p->addr);
397 kcb->prev_kprobe.kp = kprobe_running();
398 kcb->prev_kprobe.status = kcb->kprobe_status;
402 void restore_previous_kprobe (struct kprobe_ctlblk *kcb)
404 __get_cpu_var (current_kprobe) = kcb->prev_kprobe.kp;
405 kcb->kprobe_status = kcb->prev_kprobe.status;
406 kcb->prev_kprobe.kp = NULL;
407 kcb->prev_kprobe.status = 0;
410 void set_current_kprobe (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
412 __get_cpu_var (current_kprobe) = p;
413 DBPRINTF ("set_current_kprobe[]: p=%p addr=%p\n", p, p->addr);
414 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags = (regs->EREG (flags) & (TF_MASK | IF_MASK));
415 if (is_IF_modifier (p->opcode))
416 kcb->kprobe_saved_eflags &= ~IF_MASK;
419 int kprobe_handler (struct pt_regs *regs)
421 struct kprobe *p = 0;
422 int ret = 0, reenter = 0;
423 kprobe_opcode_t *addr = NULL;
424 struct kprobe_ctlblk *kcb;
425 #ifdef SUPRESS_BUG_MESSAGES
426 int swap_oops_in_progress;
429 /* We're in an interrupt, but this is clear and BUG()-safe. */
430 addr = (kprobe_opcode_t *) (regs->EREG (ip) - sizeof (kprobe_opcode_t));
431 DBPRINTF ("KPROBE: regs->eip = 0x%lx addr = 0x%p\n", regs->EREG (ip), addr);
432 #ifdef SUPRESS_BUG_MESSAGES
433 // oops_in_progress used to avoid BUG() messages that slow down kprobe_handler() execution
434 swap_oops_in_progress = oops_in_progress;
435 oops_in_progress = 1;
439 kcb = get_kprobe_ctlblk ();
441 /* Check we're not actually recursing */
442 if (kprobe_running()) {
443 p = get_kprobe(addr);
445 if (kcb->kprobe_status == KPROBE_HIT_SS && *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
446 regs->EREG(flags) &= ~TF_MASK;
447 regs->EREG(flags) |= kcb->kprobe_saved_eflags;
452 /* We have reentered the kprobe_handler(), since
453 * another probe was hit while within the handler.
454 * We here save the original kprobes variables and
455 * just single step on the instruction of the new probe
456 * without calling any user handlers.
458 save_previous_kprobe (kcb, p);
459 set_current_kprobe (p, regs, kcb);
460 kprobes_inc_nmissed_count (p);
461 prepare_singlestep (p, regs);
462 kcb->kprobe_status = KPROBE_REENTER;
463 // FIXME should we enable preemption here??...
464 //preempt_enable_no_resched ();
465 #ifdef SUPRESS_BUG_MESSAGES
466 oops_in_progress = swap_oops_in_progress;
470 if (*addr != BREAKPOINT_INSTRUCTION) {
471 /* The breakpoint instruction was removed by
472 * another cpu right after we hit, no further
473 * handling of this interrupt is appropriate
475 regs->EREG(ip) -= sizeof(kprobe_opcode_t);
480 p = __get_cpu_var(current_kprobe);
481 if (p->break_handler && p->break_handler(p, regs))
488 DBPRINTF ("get_kprobe %p", addr);
490 p = get_kprobe(addr);
493 if (*addr != BREAKPOINT_INSTRUCTION) {
495 * The breakpoint instruction was removed right
496 * after we hit it. Another cpu has removed
497 * either a probepoint or a debugger breakpoint
498 * at this address. In either case, no further
499 * handling of this interrupt is appropriate.
500 * Back up over the (now missing) int3 and run
501 * the original instruction.
503 regs->EREG(ip) -= sizeof(kprobe_opcode_t);
508 /* Not one of ours: let kernel handle it */
509 DBPRINTF ("no_kprobe");
514 set_current_kprobe (p, regs, kcb);
517 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
520 ret = p->pre_handler(p, regs);
524 if (ret == 2) { // we have alreadyc called the handler, so just single step the instruction
525 DBPRINTF ("p->pre_handler[] 2");
528 DBPRINTF ("p->pre_handler[] 1");
529 // FIXME should we enable preemption here??...
530 //preempt_enable_no_resched ();
531 #ifdef SUPRESS_BUG_MESSAGES
532 oops_in_progress = swap_oops_in_progress;
534 /* handler has already set things up, so skip ss setup */
537 DBPRINTF ("p->pre_handler[] 0");
540 DBPRINTF ("p = %p\n", p);
541 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);
543 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
544 if (p->ainsn.boostable == 1 && !p->post_handler)
546 /* Boost up -- we can execute copied instructions directly */
547 reset_current_kprobe ();
548 regs->EREG (ip) = (unsigned long) p->ainsn.insn;
549 preempt_enable_no_resched ();
550 #ifdef SUPRESS_BUG_MESSAGES
551 oops_in_progress = swap_oops_in_progress;
555 #endif // !CONFIG_PREEMPT
556 prepare_singlestep (p, regs);
557 kcb->kprobe_status = KPROBE_HIT_SS;
558 // FIXME should we enable preemption here??...
559 //preempt_enable_no_resched ();
560 #ifdef SUPRESS_BUG_MESSAGES
561 oops_in_progress = swap_oops_in_progress;
567 preempt_enable_no_resched ();
568 #ifdef SUPRESS_BUG_MESSAGES
569 oops_in_progress = swap_oops_in_progress;
574 int setjmp_pre_handler (struct kprobe *p, struct pt_regs *regs)
576 struct jprobe *jp = container_of (p, struct jprobe, kp);
577 kprobe_pre_entry_handler_t pre_entry;
580 unsigned long addr, args[6];
581 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
583 DBPRINTF ("setjmp_pre_handler %p:%d", p->addr, p->tgid);
584 pre_entry = (kprobe_pre_entry_handler_t) jp->pre_entry;
585 entry = (entry_point_t) jp->entry;
587 /* handle __switch_to probe */
588 if ((p->addr == sched_addr) && sched_rp) {
589 /* FIXME: Actually 2nd parameter is not used for x86 */
590 patch_suspended_task(sched_rp, (struct task_struct *)regs->dx, regs);
593 kcb->jprobe_saved_regs = *regs;
594 kcb->jprobe_saved_esp = ®s->EREG(sp);
595 addr = (unsigned long)(kcb->jprobe_saved_esp);
597 /* TBD: As Linus pointed out, gcc assumes that the callee
598 * owns the argument space and could overwrite it, e.g.
599 * tailcall optimization. So, to be absolutely safe
600 * we also save and restore enough stack bytes to cover
601 * the argument area. */
602 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, MIN_STACK_SIZE (addr));
603 regs->EREG(flags) &= ~IF_MASK;
604 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
605 trace_hardirqs_off();
608 p->ss_addr = pre_entry(jp->priv_arg, regs);
610 regs->EREG(ip) = (unsigned long)(jp->entry);
615 void dbi_jprobe_return (void)
617 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
619 asm volatile(" xchgl %%ebx,%%esp \n"
621 " .globl dbi_jprobe_return_end \n"
622 " dbi_jprobe_return_end: \n"
623 " nop \n"::"b" (kcb->jprobe_saved_esp):"memory");
626 void arch_ujprobe_return(void)
631 * Called after single-stepping. p->addr is the address of the
632 * instruction whose first byte has been replaced by the "int 3"
633 * instruction. To avoid the SMP problems that can occur when we
634 * temporarily put back the original opcode to single-step, we
635 * single-stepped a copy of the instruction. The address of this
636 * copy is p->ainsn.insn.
638 * This function prepares to return from the post-single-step
639 * interrupt. We have to fix up the stack as follows:
641 * 0) Except in the case of absolute or indirect jump or call instructions,
642 * the new eip is relative to the copied instruction. We need to make
643 * it relative to the original instruction.
645 * 1) If the single-stepped instruction was pushfl, then the TF and IF
646 * flags are set in the just-pushed eflags, and may need to be cleared.
648 * 2) If the single-stepped instruction was a call, the return address
649 * that is atop the stack is the address following the copied instruction.
650 * We need to make it the address following the original instruction.
652 * This function also checks instruction size for preparing direct execution.
654 static void resume_execution (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
656 unsigned long *tos, tos_dword = 0;
657 unsigned long copy_eip = (unsigned long) p->ainsn.insn;
658 unsigned long orig_eip = (unsigned long) p->addr;
659 kprobe_opcode_t insns[2];
661 regs->EREG (flags) &= ~TF_MASK;
663 tos = (unsigned long *)®s->EREG(sp);
664 insns[0] = p->ainsn.insn[0];
665 insns[1] = p->ainsn.insn[1];
669 case 0x9c: /* pushfl */
670 *tos &= ~(TF_MASK | IF_MASK);
671 *tos |= kcb->kprobe_old_eflags;
673 case 0xc2: /* iret/ret/lret */
678 case 0xea: /* jmp absolute -- eip is correct */
679 /* eip is already adjusted, no more changes required */
680 p->ainsn.boostable = 1;
682 case 0xe8: /* call relative - Fix return addr */
683 *tos = orig_eip + (*tos - copy_eip);
685 case 0x9a: /* call absolute -- same as call absolute, indirect */
686 *tos = orig_eip + (*tos - copy_eip);
689 if ((insns[1] & 0x30) == 0x10)
692 * call absolute, indirect
693 * Fix return addr; eip is correct.
694 * But this is not boostable
696 *tos = orig_eip + (*tos - copy_eip);
699 else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
700 ((insns[1] & 0x31) == 0x21))
701 { /* jmp far, absolute indirect */
702 /* eip is correct. And this is boostable */
703 p->ainsn.boostable = 1;
710 if (p->ainsn.boostable == 0)
712 if ((regs->EREG (ip) > copy_eip) && (regs->EREG (ip) - copy_eip) + 5 < MAX_INSN_SIZE)
715 * These instructions can be executed directly if it
716 * jumps back to correct address.
718 set_jmp_op((void *)regs->EREG(ip), (void *)orig_eip + (regs->EREG(ip) - copy_eip));
719 p->ainsn.boostable = 1;
723 p->ainsn.boostable = -1;
727 regs->EREG (ip) = orig_eip + (regs->EREG (ip) - copy_eip);
734 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
735 * remain disabled thoroughout this function.
737 static int post_kprobe_handler (struct pt_regs *regs)
739 struct kprobe *cur = kprobe_running ();
740 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
744 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler)
746 kcb->kprobe_status = KPROBE_HIT_SSDONE;
747 cur->post_handler (cur, regs, 0);
750 resume_execution (cur, regs, kcb);
751 regs->EREG (flags) |= kcb->kprobe_saved_eflags;
753 trace_hardirqs_fixup_flags (regs->EREG (flags));
755 /*Restore back the original saved kprobes variables and continue. */
756 if (kcb->kprobe_status == KPROBE_REENTER)
758 restore_previous_kprobe (kcb);
761 reset_current_kprobe ();
763 preempt_enable_no_resched ();
766 * if somebody else is singlestepping across a probe point, eflags
767 * will have TF set, in which case, continue the remaining processing
768 * of do_debug, as if this is not a probe hit.
770 if (regs->EREG (flags) & TF_MASK)
776 int kprobe_fault_handler (struct pt_regs *regs, int trapnr)
778 struct kprobe *cur = kprobe_running ();
779 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
781 switch (kcb->kprobe_status)
786 * We are here because the instruction being single
787 * stepped caused a page fault. We reset the current
788 * kprobe and the eip points back to the probe address
789 * and allow the page fault handler to continue as a
792 regs->EREG (ip) = (unsigned long) cur->addr;
793 regs->EREG (flags) |= kcb->kprobe_old_eflags;
794 if (kcb->kprobe_status == KPROBE_REENTER)
795 restore_previous_kprobe (kcb);
797 reset_current_kprobe ();
798 preempt_enable_no_resched ();
800 case KPROBE_HIT_ACTIVE:
801 case KPROBE_HIT_SSDONE:
803 * We increment the nmissed count for accounting,
804 * we can also use npre/npostfault count for accouting
805 * these specific fault cases.
807 kprobes_inc_nmissed_count (cur);
810 * We come here because instructions in the pre/post
811 * handler caused the page_fault, this could happen
812 * if handler tries to access user space by
813 * copy_from_user(), get_user() etc. Let the
814 * user-specified handler try to fix it first.
816 if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr))
820 * In case the user-specified fault handler returned
821 * zero, try to fix up.
823 if (fixup_exception (regs))
827 * fixup_exception() could not handle it,
828 * Let do_page_fault() fix it.
837 int kprobe_exceptions_notify (struct notifier_block *self, unsigned long val, void *data)
839 struct die_args *args = (struct die_args *) data;
840 int ret = NOTIFY_DONE;
842 DBPRINTF ("val = %ld, data = 0x%X", val, (unsigned int) data);
844 if (args->regs && user_mode_vm(args->regs))
847 DBPRINTF ("switch (val) %lu %d %d", val, DIE_INT3, DIE_TRAP);
850 #ifdef CONFIG_KPROBES
855 DBPRINTF ("before kprobe_handler ret=%d %p", ret, args->regs);
856 if (kprobe_handler (args->regs))
858 DBPRINTF ("after kprobe_handler ret=%d %p", ret, args->regs);
861 if (post_kprobe_handler (args->regs))
865 // kprobe_running() needs smp_processor_id()
867 if (kprobe_running () && kprobe_fault_handler (args->regs, args->trapnr))
874 DBPRINTF ("ret=%d", ret);
875 /* if(ret == NOTIFY_STOP) */
876 /* handled_exceptions++; */
881 static struct notifier_block kprobe_exceptions_nb = {
882 .notifier_call = kprobe_exceptions_notify,
886 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
888 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
889 u8 *addr = (u8 *) (regs->EREG (ip) - 1);
890 unsigned long stack_addr = (unsigned long) (kcb->jprobe_saved_esp);
891 struct jprobe *jp = container_of (p, struct jprobe, kp);
893 DBPRINTF ("p = %p\n", p);
895 if ((addr > (u8 *) dbi_jprobe_return) && (addr < (u8 *) dbi_jprobe_return_end))
897 if ((unsigned long *)(®s->EREG(sp)) != kcb->jprobe_saved_esp)
899 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
900 printk ("current esp %p does not match saved esp %p\n", ®s->EREG (sp), kcb->jprobe_saved_esp);
901 printk ("Saved registers for jprobe %p\n", jp);
902 show_registers (saved_regs);
903 printk ("Current registers\n");
904 show_registers (regs);
908 *regs = kcb->jprobe_saved_regs;
909 memcpy ((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, MIN_STACK_SIZE (stack_addr));
910 preempt_enable_no_resched ();
915 void arch_arm_kprobe (struct kprobe *p)
917 text_poke (p->addr, ((unsigned char[])
918 {BREAKPOINT_INSTRUCTION}), 1);
921 void arch_disarm_kprobe (struct kprobe *p)
923 text_poke (p->addr, &p->opcode, 1);
926 static __used void *trampoline_probe_handler_x86(struct pt_regs *regs)
928 return (void *)trampoline_probe_handler(NULL, regs);
931 void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
933 unsigned long *sara = (unsigned long *)®s->EREG(sp);
934 ri->ret_addr = (kprobe_opcode_t *)*sara;
935 ri->sp = regs->EREG(sp);
937 /* Replace the return addr with trampoline addr */
938 *sara = (unsigned long)&kretprobe_trampoline;
941 int arch_init_module_deps()
943 INIT_MOD_DEP_VAR(module_alloc, module_alloc);
944 INIT_MOD_DEP_VAR(module_free, module_free);
945 INIT_MOD_DEP_VAR(fixup_exception, fixup_exception);
946 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
947 # error this kernel version has no text_poke function which is necessaryf for x86 ach!!!
949 INIT_MOD_DEP_VAR(text_poke, text_poke);
951 INIT_MOD_DEP_VAR(show_registers, show_registers);
952 #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
953 INIT_MOD_DEP_VAR(freeze_processes, freeze_processes);
954 INIT_MOD_DEP_VAR(thaw_processes, thaw_processes);
960 int arch_init_kprobes(void)
962 return register_die_notifier (&kprobe_exceptions_nb);
965 void arch_exit_kprobes(void)
967 unregister_die_notifier (&kprobe_exceptions_nb);