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
50 #include "dbi_krobes.h"
51 #include "../dbi_krobes.h"
52 #include "../../dbi_kprobes.h"
54 #include "../../dbi_kdebug.h"
55 #include "../../dbi_insn_slots.h"
56 #include "../../dbi_kprobes_deps.h"
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
64 void kretprobe_trampoline_holder (void)
66 asm volatile (".global kretprobe_trampoline\n"
67 "kretprobe_trampoline:\n"
69 /* skip cs, eip, orig_eax */
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"
95 /* skip eip, orig_eax, es, ds, fs */
102 struct kprobe trampoline_p =
104 .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
105 .pre_handler = trampoline_probe_handler
108 /* insert a jmp code */
109 static __always_inline void set_jmp_op (void *from, void *to)
115 } __attribute__ ((packed)) * jop;
116 jop = (struct __arch_jmp_op *) from;
117 jop->raddr = (long) (to) - ((long) (from) + 5);
118 jop->op = RELATIVEJUMP_INSTRUCTION;
121 static void set_user_jmp_op (void *from, void *to)
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);
136 * returns non-zero if opcodes can be boosted.
138 static __always_inline int can_boost (kprobe_opcode_t * opcodes)
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)) \
147 * Undefined/reserved opcodes, conditional jump, Opcode Extension
148 * Groups, and some special opcodes can not be boost.
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 */
173 kprobe_opcode_t opcode;
174 kprobe_opcode_t *orig_opcodes = opcodes;
176 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
178 opcode = *(opcodes++);
180 /* 2nd-byte opcode */
183 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
185 return test_bit (*opcodes, twobyte_is_boostable);
188 switch (opcode & 0xf0)
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);
196 return 0; /* can't boost conditional jump */
198 /* can't boost software-interruptions */
199 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
201 /* can boost AA* and XLAT */
202 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
204 /* can boost in/out and absolute jmps */
205 return ((opcode & 0x04) || opcode == 0xea);
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));
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);
220 * returns non-zero if opcode modifies the interrupt flag.
222 static int __kprobes is_IF_modifier (kprobe_opcode_t opcode)
228 case 0xcf: /* iret/iretd */
229 case 0x9d: /* popf/popfd */
235 int arch_check_insn (struct arch_specific_insn *ainsn)
237 DPRINF("Warrning: arch_check_insn is not implemented for x86\n");
241 int arch_prepare_kretprobe (struct kretprobe *p)
243 DPRINF("Warrning: arch_prepare_kretprobe is not implemented\n");
248 int arch_prepare_kprobe (struct kprobe *p)
250 kprobe_opcode_t insns[KPROBES_TRAMP_LEN];
254 if ((unsigned long) p->addr & 0x01)
256 DBPRINTF ("Attempt to register kprobe at an unaligned address\n");
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);
269 memcpy (insn, p->addr, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
271 ret = arch_check_insn (&ainsn);
274 p->opcode = *p->addr;
277 if (can_boost (p->addr))
278 p->ainsn.boostable = 0;
280 p->ainsn.boostable = -1;
281 memcpy (p->ainsn.insn, insn, MAX_INSN_SIZE * sizeof (kprobe_opcode_t));
285 free_insn_slot (&kprobe_insn_pages, NULL, p->ainsn.insn, 0);
291 int arch_prepare_uprobe (struct kprobe *p, struct task_struct *task, int atomic)
294 kprobe_opcode_t insns[UPROBES_TRAMP_LEN];
298 kprobe_opcode_t insn[MAX_INSN_SIZE];
299 struct arch_specific_insn ainsn;
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);
304 ret = arch_check_insn (&ainsn);
308 p->ainsn.insn = get_insn_slot(task, atomic);
311 if (can_boost (insn))
312 p->ainsn.boostable = 0;
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;
318 if (!write_proc_vm_atomic (task, (unsigned long) p->ainsn.insn, insns, sizeof (insns)))
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);
331 int arch_prepare_uretprobe (struct kretprobe *p, struct task_struct *task)
333 DPRINF("Warrning: arch_prepare_uretprobe is not implemented\n");
337 void prepare_singlestep (struct kprobe *p, struct pt_regs *regs)
342 regs->EREG (ip) = (unsigned long)p->ss_addr;
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");
355 regs->EREG (ip) = (unsigned long) p->ainsn.insn;
360 void save_previous_kprobe (struct kprobe_ctlblk *kcb, struct kprobe *cur_p)
362 if (kcb->prev_kprobe.kp != NULL)
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);
370 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
371 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
375 void restore_previous_kprobe (struct kprobe_ctlblk *kcb)
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;
385 void set_current_kprobe (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
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;
394 int kprobe_handler (struct pt_regs *regs)
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;
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);
409 kcb = get_kprobe_ctlblk ();
411 if (user_mode_vm(regs))
413 //printk("exception[%lu] from user mode %s/%u/%u addr %p.\n", nCount, current->comm, current->pid, current->tgid, addr);
417 /* Check we're not actually recursing */
418 if (kprobe_running ())
420 DBPRINTF ("lock???");
421 p = get_kprobe (addr, pid, current);
424 DBPRINTF ("reenter p = %p", p);
426 if (kcb->kprobe_status == KPROBE_HIT_SS && *p->ainsn.insn == BREAKPOINT_INSTRUCTION)
428 regs->EREG (flags) &= ~TF_MASK;
429 regs->EREG (flags) |= kcb->kprobe_saved_eflags;
434 //#warning BREAKPOINT_INSTRUCTION user mode handling is missed!!!
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.
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;
453 if (*addr != BREAKPOINT_INSTRUCTION)
455 /* The breakpoint instruction was removed by
456 * another cpu right after we hit, no further
457 * handling of this interrupt is appropriate
459 regs->EREG (ip) -= sizeof (kprobe_opcode_t);
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);
471 save_previous_kprobe (kcb, p);
472 kcb->kprobe_status = KPROBE_REENTER;
475 DBPRINTF ("uretprobe %p\n", addr);
479 p = __get_cpu_var (current_kprobe);
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))
485 DBPRINTF ("kprobe_running !!! goto ss");
488 DBPRINTF ("kprobe_running !!! goto no");
489 DBPRINTF ("no_kprobe");
495 DBPRINTF ("get_kprobe %p", addr);
497 p = get_kprobe (addr, pid, current);
501 if (*addr != BREAKPOINT_INSTRUCTION)
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.
512 regs->EREG (ip) -= sizeof (kprobe_opcode_t);
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);
522 // Not one of ours: let kernel handle it
523 DBPRINTF ("no_kprobe");
524 //printk("no_kprobe2 ret = %d\n", ret);
528 DBPRINTF ("uretprobe %p\n", addr);
531 /* Not one of ours: let kernel handle it */
532 DBPRINTF ("no_kprobe");
536 set_current_kprobe (p, regs, kcb);
538 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
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);
547 if (ret == 2) { // we have alreadyc called the handler, so just single step the instruction
548 DBPRINTF ("p->pre_handler[%lu] 2", nCount);
551 DBPRINTF ("p->pre_handler[%lu] 1", nCount);
552 /* handler has already set things up, so skip ss setup */
555 DBPRINTF ("p->pre_handler[%lu] 0", nCount);
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);
561 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
562 if (p->ainsn.boostable == 1 && !p->post_handler)
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 ();
570 #endif // !CONFIG_PREEMPT
571 prepare_singlestep (p, regs);
572 kcb->kprobe_status = KPROBE_HIT_SS;
577 preempt_enable_no_resched ();
581 int setjmp_pre_handler (struct kprobe *p, struct pt_regs *regs)
583 struct jprobe *jp = container_of (p, struct jprobe, kp);
584 kprobe_pre_entry_handler_t pre_entry;
587 unsigned long addr, args[6];
588 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
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;
594 regs->EREG (flags) &= ~IF_MASK;
595 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
596 trace_hardirqs_off ();
598 if (p->tgid == current->tgid)
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);
604 p->ss_addr = pre_entry (jp->priv_arg, regs);
606 entry (args[0], args[1], args[2], args[3], args[4], args[5]);
609 arch_uprobe_return ();
614 kcb->jprobe_saved_regs = *regs;
615 kcb->jprobe_saved_esp = ®s->EREG (sp);
616 addr = (unsigned long) (kcb->jprobe_saved_esp);
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
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 ();
631 p->ss_addr = pre_entry (jp->priv_arg, regs);
632 regs->EREG (ip) = (unsigned long) (jp->entry);
638 void __kprobes jprobe_return (void)
640 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
642 asm volatile(" xchgl %%ebx,%%esp \n"
644 " .globl jprobe_return_end \n"
645 " jprobe_return_end: \n"
646 " nop \n"::"b" (kcb->jprobe_saved_esp):"memory");
649 void __kprobes arch_uprobe_return (void)
651 DPRINTF("arch_uprobe_return (void) is empty");
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.
662 * This function prepares to return from the post-single-step
663 * interrupt. We have to fix up the stack as follows:
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.
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.
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.
676 * This function also checks instruction size for preparing direct execution.
678 static void __kprobes resume_execution (struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
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];
685 regs->EREG (flags) &= ~TF_MASK;
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);
695 tos = (unsigned long *) ®s->EREG (sp);
696 insns[0] = p->ainsn.insn[0];
697 insns[1] = p->ainsn.insn[1];
702 case 0x9c: /* pushfl */
703 *tos &= ~(TF_MASK | IF_MASK);
704 *tos |= kcb->kprobe_old_eflags;
706 case 0xc2: /* iret/ret/lret */
711 case 0xea: /* jmp absolute -- eip is correct */
712 /* eip is already adjusted, no more changes required */
713 p->ainsn.boostable = 1;
715 case 0xe8: /* call relative - Fix return addr */
716 *tos = orig_eip + (*tos - copy_eip);
718 case 0x9a: /* call absolute -- same as call absolute, indirect */
719 *tos = orig_eip + (*tos - copy_eip);
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));
726 if ((insns[1] & 0x30) == 0x10)
729 * call absolute, indirect
730 * Fix return addr; eip is correct.
731 * But this is not boostable
733 *tos = orig_eip + (*tos - copy_eip);
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));
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;
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));
756 if (p->ainsn.boostable == 0)
758 if ((regs->EREG (ip) > copy_eip) && (regs->EREG (ip) - copy_eip) + 5 < MAX_INSN_SIZE)
761 * These instructions can be executed directly if it
762 * jumps back to correct address.
765 set_user_jmp_op ((void *) regs->EREG (ip), (void *) orig_eip + (regs->EREG (ip) - copy_eip));
767 set_jmp_op ((void *) regs->EREG (ip), (void *) orig_eip + (regs->EREG (ip) - copy_eip));
768 p->ainsn.boostable = 1;
772 p->ainsn.boostable = -1;
776 regs->EREG (ip) = orig_eip + (regs->EREG (ip) - copy_eip);
783 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
784 * remain disabled thoroughout this function.
786 static int __kprobes post_kprobe_handler (struct pt_regs *regs)
788 struct kprobe *cur = kprobe_running ();
789 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
793 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler)
795 kcb->kprobe_status = KPROBE_HIT_SSDONE;
796 cur->post_handler (cur, regs, 0);
799 resume_execution (cur, regs, kcb);
800 regs->EREG (flags) |= kcb->kprobe_saved_eflags;
802 trace_hardirqs_fixup_flags (regs->EREG (flags));
804 /*Restore back the original saved kprobes variables and continue. */
805 if (kcb->kprobe_status == KPROBE_REENTER)
807 restore_previous_kprobe (kcb);
810 reset_current_kprobe ();
812 preempt_enable_no_resched ();
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.
819 if (regs->EREG (flags) & TF_MASK)
825 int __kprobes kprobe_fault_handler (struct pt_regs *regs, int trapnr)
827 struct kprobe *cur = kprobe_running ();
828 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk ();
830 switch (kcb->kprobe_status)
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
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);
846 reset_current_kprobe ();
847 preempt_enable_no_resched ();
849 case KPROBE_HIT_ACTIVE:
850 case KPROBE_HIT_SSDONE:
852 * We increment the nmissed count for accounting,
853 * we can also use npre/npostfault count for accouting
854 * these specific fault cases.
856 kprobes_inc_nmissed_count (cur);
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.
865 if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr))
869 * In case the user-specified fault handler returned
870 * zero, try to fix up.
872 if (fixup_exception (regs))
876 * fixup_exception() could not handle it,
877 * Let do_page_fault() fix it.
886 int kprobe_exceptions_notify (struct notifier_block *self, unsigned long val, void *data)
888 struct die_args *args = (struct die_args *) data;
889 int ret = NOTIFY_DONE;
891 DBPRINTF ("val = %ld, data = 0x%X", val, (unsigned int) data);
893 /*if (args->regs && user_mode_vm (args->regs))
896 DBPRINTF ("switch (val) %lu %d %d", val, DIE_INT3, DIE_TRAP);
899 //#ifdef CONFIG_KPROBES
904 DBPRINTF ("before kprobe_handler ret=%d %p", ret, args->regs);
905 if (kprobe_handler (args->regs))
907 DBPRINTF ("after kprobe_handler ret=%d %p", ret, args->regs);
910 if (post_kprobe_handler (args->regs))
914 // kprobe_running() needs smp_processor_id()
916 if (kprobe_running () && kprobe_fault_handler (args->regs, args->trapnr))
923 DBPRINTF ("ret=%d", ret);
924 if(ret == NOTIFY_STOP)
925 handled_exceptions++;
930 int longjmp_break_handler (struct kprobe *p, struct pt_regs *regs)
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);
937 DBPRINTF ("p = %p\n", p);
939 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end))
941 if ((unsigned long *)(®s->EREG(sp)) != kcb->jprobe_saved_esp)
943 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
944 printk ("current esp %p does not match saved esp %p\n", ®s->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);
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 ();
959 void __kprobes arch_arm_kprobe (struct kprobe *p)
961 text_poke (p->addr, ((unsigned char[])
962 {BREAKPOINT_INSTRUCTION}), 1);
965 void __kprobes arch_disarm_kprobe (struct kprobe *p)
967 text_poke (p->addr, &p->opcode, 1);
970 void *__kprobes trampoline_probe_handler_x86 (struct pt_regs *regs)
972 return (void *)trampoline_probe_handler(NULL, regs);
979 * Called when the probe at kretprobe trampoline is hit
981 int __kprobes trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs)
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 ();
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);
998 INIT_HLIST_HEAD (&empty_rp);
999 spin_lock_irqsave (&kretprobe_lock, flags);
1000 head = kretprobe_inst_table_head (current);
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;
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.
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
1023 hlist_for_each_entry_safe (ri, node, tmp, head, hlist)
1025 if (ri->task != current)
1026 /* another task is sharing our hash bucket */
1028 if (ri->rp && ri->rp->handler){
1030 if(!p){ // X86 kernel space
1031 __get_cpu_var (current_kprobe) = &ri->rp->kp;
1032 get_kprobe_ctlblk ()->kprobe_status = KPROBE_HIT_ACTIVE;
1035 ri->rp->handler (ri, regs, ri->rp->priv_arg);
1037 if(!p) // X86 kernel space
1038 __get_cpu_var (current_kprobe) = NULL;
1042 orig_ret_address = (unsigned long) ri->ret_addr;
1043 recycle_rp_inst (ri, &empty_rp);
1044 if (orig_ret_address != trampoline_address)
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
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);
1059 if ((ri->rp && ri->rp->kp.tgid) || (ri->rp2 && ri->rp2->kp.tgid))
1060 BUG_ON (trampoline_address == (unsigned long) &kretprobe_trampoline);
1062 if(p){ // X86 user space
1063 regs->EREG(ip) = orig_ret_address;
1064 //printk (" uretprobe regs->eip = 0x%lx\n", regs->EREG(ip));
1067 if(p){ // ARM, MIPS, X86 user space
1068 if (kcb->kprobe_status == KPROBE_REENTER)
1069 restore_previous_kprobe (kcb);
1071 reset_current_kprobe ();
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)
1077 // if we are not at the end of the list and current retprobe should be disarmed
1078 if (node && 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)
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
1093 { // if there are no more instances for this retprobe
1095 DBPRINTF ("defered retprobe deletion p->addr = %p", crp->kp.addr);
1096 unregister_uprobe (&crp->kp, current, 1);
1103 spin_unlock_irqrestore (&kretprobe_lock, flags);
1104 hlist_for_each_entry_safe (ri, node, tmp, &empty_rp, hlist)
1106 hlist_del (&ri->hlist);
1110 if(!p) // X86 kernel space
1111 return (int)orig_ret_address;
1113 preempt_enable_no_resched ();
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)
1122 void __kprobes __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs)
1124 struct kretprobe_instance *ri;
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)
1134 /* Replace the return addr with trampoline addr */
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);
1147 unsigned long *sara = (unsigned long *)®s->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);
1156 DBPRINTF ("WARNING: missed retprobe %p\n", rp->kp.addr);
1161 int asm_init_module_dependencies()
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!!!
1169 INIT_MOD_DEP_VAR(text_poke, text_poke);
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);
1180 int __init arch_init_kprobes (void)
1182 if (!arch_init_module_dependencies())
1184 DBPRINTF ("Unable to init module dependencies\n");
1188 return register_die_notifier (&kprobe_exceptions_nb);
1191 void __exit arch_exit_kprobes (void)
1193 unregister_die_notifier (&kprobe_exceptions_nb);
1196 EXPORT_SYMBOL_GPL (arch_uprobe_return);
1197 EXPORT_SYMBOL_GPL (arch_exit_kprobes);