2 * Kernel Probes (KProbes)
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/dbi_kprobes.h
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 * 2006-2007 Ekaterina Gorelkina <e.gorelkina@samsung.com>: initial implementation for ARM and MIPS
43 * 2008-2009 Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
44 * Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
45 * 2010 Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
49 #include "dbi_kprobes.h"
50 #include <kprobe/arch/asm/dbi_kprobes.h>
52 #include "dbi_kdebug.h"
53 #include "dbi_kprobes_deps.h"
54 #include "dbi_insn_slots.h"
55 #include <ksyms/ksyms.h>
57 #include <linux/version.h>
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
59 #include <linux/config.h>
62 #include <linux/hash.h>
63 #include <linux/module.h>
65 #include <linux/pagemap.h>
67 unsigned long sched_addr;
68 static unsigned long exit_addr;
69 static unsigned long do_group_exit_addr;
70 static unsigned long sys_exit_group_addr;
71 static unsigned long sys_exit_addr;
73 struct slot_manager sm;
75 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
76 static DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
78 static DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
79 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
81 struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
82 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
84 atomic_t kprobe_count;
85 EXPORT_SYMBOL_GPL(kprobe_count);
88 static void *(*module_alloc)(unsigned long size) = NULL;
89 static void *(*module_free)(struct module *mod, void *module_region) = NULL;
91 static void *__wrapper_module_alloc(unsigned long size)
93 return module_alloc(size);
96 static void *__wrapper_module_free(void *module_region)
98 return module_free(NULL, module_region);
101 static void *sm_alloc(struct slot_manager *sm)
103 return __wrapper_module_alloc(PAGE_SIZE);
106 static void sm_free(struct slot_manager *sm, void *ptr)
108 __wrapper_module_free(ptr);
111 static void init_sm(void)
113 sm.slot_size = KPROBES_TRAMP_LEN;
116 INIT_HLIST_HEAD(&sm.page_list);
119 static void exit_sm(void)
124 void kretprobe_assert(struct kretprobe_instance *ri, unsigned long orig_ret_address, unsigned long trampoline_address)
126 if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
127 struct task_struct *task;
129 panic("kretprobe BUG!: ri = NULL\n");
135 panic("kretprobe BUG!: task = NULL\n");
138 if (ri->rp == NULL) {
139 panic("kretprobe BUG!: ri->rp = NULL\n");
142 panic("kretprobe BUG!: Processing kretprobe %p @ %p (%d/%d - %s)\n",
143 ri->rp, ri->rp->kp.addr, ri->task->tgid, ri->task->pid, ri->task->comm);
147 /* We have preemption disabled.. so it is safe to use __ versions */
148 static inline void set_kprobe_instance(struct kprobe *kp)
150 __get_cpu_var(kprobe_instance) = kp;
153 static inline void reset_kprobe_instance(void)
155 __get_cpu_var(kprobe_instance) = NULL;
158 /* kprobe_running() will just return the current_kprobe on this CPU */
159 struct kprobe *kprobe_running(void)
161 return __get_cpu_var(current_kprobe);
164 void reset_current_kprobe(void)
166 __get_cpu_var(current_kprobe) = NULL;
169 struct kprobe_ctlblk *get_kprobe_ctlblk(void)
171 return &__get_cpu_var(kprobe_ctlblk);
175 * This routine is called either:
176 * - under the kprobe_mutex - during kprobe_[un]register()
178 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
180 struct kprobe *get_kprobe(void *addr)
182 struct hlist_head *head;
184 DECLARE_NODE_PTR_FOR_HLIST(node);
186 head = &kprobe_table[hash_ptr (addr, KPROBE_HASH_BITS)];
187 swap_hlist_for_each_entry_rcu(p, node, head, hlist) {
188 if (p->addr == addr) {
197 * Aggregate handlers for multiple kprobes support - these handlers
198 * take care of invoking the individual kprobe handlers on p->list
200 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
205 list_for_each_entry_rcu(kp, &p->list, list) {
206 if (kp->pre_handler) {
207 set_kprobe_instance(kp);
208 ret = kp->pre_handler(kp, regs);
212 reset_kprobe_instance();
218 static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, unsigned long flags)
222 list_for_each_entry_rcu(kp, &p->list, list) {
223 if (kp->post_handler) {
224 set_kprobe_instance(kp);
225 kp->post_handler(kp, regs, flags);
226 reset_kprobe_instance();
231 static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr)
233 struct kprobe *cur = __get_cpu_var(kprobe_instance);
236 * if we faulted "during" the execution of a user specified
237 * probe handler, invoke just that probe's fault handler
239 if (cur && cur->fault_handler) {
240 if (cur->fault_handler(cur, regs, trapnr))
247 static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
249 struct kprobe *cur = __get_cpu_var(kprobe_instance);
251 DBPRINTF ("cur = 0x%p\n", cur);
253 DBPRINTF ("cur = 0x%p cur->break_handler = 0x%p\n", cur, cur->break_handler);
255 if (cur && cur->break_handler) {
256 if (cur->break_handler(cur, regs))
259 reset_kprobe_instance();
264 /* Walks the list and increments nmissed count for multiprobe case */
265 void kprobes_inc_nmissed_count(struct kprobe *p)
268 if (p->pre_handler != aggr_pre_handler) {
271 list_for_each_entry_rcu(kp, &p->list, list) {
277 /* Called with kretprobe_lock held */
278 struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
280 struct kretprobe_instance *ri;
281 DECLARE_NODE_PTR_FOR_HLIST(node);
283 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
287 if (!alloc_nodes_kretprobe(rp)) {
288 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
295 EXPORT_SYMBOL_GPL(get_free_rp_inst);
297 /* Called with kretprobe_lock held */
298 struct kretprobe_instance *get_free_rp_inst_no_alloc(struct kretprobe *rp)
300 struct kretprobe_instance *ri;
301 DECLARE_NODE_PTR_FOR_HLIST(node);
303 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
310 /* Called with kretprobe_lock held */
311 struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
313 struct kretprobe_instance *ri;
314 DECLARE_NODE_PTR_FOR_HLIST(node);
316 swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
322 EXPORT_SYMBOL_GPL(get_used_rp_inst);
324 /* Called with kretprobe_lock held */
325 void add_rp_inst (struct kretprobe_instance *ri)
328 * Remove rp inst off the free list -
329 * Add it back when probed function returns
331 hlist_del(&ri->uflist);
333 /* Add rp inst onto table */
334 INIT_HLIST_NODE(&ri->hlist);
336 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
338 /* Also add this rp inst to the used list. */
339 INIT_HLIST_NODE(&ri->uflist);
340 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
342 EXPORT_SYMBOL_GPL(add_rp_inst);
344 /* Called with kretprobe_lock held */
345 void recycle_rp_inst(struct kretprobe_instance *ri)
348 hlist_del(&ri->hlist);
349 /* remove rp inst off the used list */
350 hlist_del(&ri->uflist);
351 /* put rp inst back onto the free list */
352 INIT_HLIST_NODE(&ri->uflist);
353 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
356 EXPORT_SYMBOL_GPL(recycle_rp_inst);
358 struct hlist_head *kretprobe_inst_table_head(void *hash_key)
360 return &kretprobe_inst_table[hash_ptr(hash_key, KPROBE_HASH_BITS)];
362 EXPORT_SYMBOL_GPL(kretprobe_inst_table_head);
364 void free_rp_inst(struct kretprobe *rp)
366 struct kretprobe_instance *ri;
367 while ((ri = get_free_rp_inst_no_alloc(rp)) != NULL) {
368 hlist_del(&ri->uflist);
372 EXPORT_SYMBOL_GPL(free_rp_inst);
375 * Keep all fields in the kprobe consistent
377 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
379 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
380 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
381 p->ss_addr = old_p->ss_addr;
383 p->safe_arm = old_p->safe_arm;
384 p->safe_thumb = old_p->safe_thumb;
389 * Add the new probe to old_p->list. Fail if this is the
390 * second jprobe at the address - two jprobes can't coexist
392 static int add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
394 if (p->break_handler) {
395 if (old_p->break_handler) {
399 list_add_tail_rcu(&p->list, &old_p->list);
400 old_p->break_handler = aggr_break_handler;
402 list_add_rcu(&p->list, &old_p->list);
405 if (p->post_handler && !old_p->post_handler) {
406 old_p->post_handler = aggr_post_handler;
413 * hlist_replace_rcu - replace old entry by new one
414 * @old : the element to be replaced
415 * @new : the new element to insert
417 * The @old entry will be replaced with the @new entry atomically.
419 inline void dbi_hlist_replace_rcu(struct hlist_node *old, struct hlist_node *new)
421 struct hlist_node *next = old->next;
424 new->pprev = old->pprev;
427 new->next->pprev = &new->next;
430 old->pprev = LIST_POISON2;
434 * Fill in the required fields of the "manager kprobe". Replace the
435 * earlier kprobe in the hlist with the manager kprobe
437 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
441 ap->pre_handler = aggr_pre_handler;
442 ap->fault_handler = aggr_fault_handler;
444 ap->post_handler = aggr_post_handler;
445 if (p->break_handler)
446 ap->break_handler = aggr_break_handler;
448 INIT_LIST_HEAD(&ap->list);
449 list_add_rcu(&p->list, &ap->list);
451 dbi_hlist_replace_rcu(&p->hlist, &ap->hlist);
455 * This is the second or subsequent kprobe at the address - handle
458 int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
462 DBPRINTF ("start\n");
464 DBPRINTF ("p = %p old_p = %p \n", p, old_p);
465 if (old_p->pre_handler == aggr_pre_handler) {
466 DBPRINTF ("aggr_pre_handler \n");
468 copy_kprobe(old_p, p);
469 ret = add_new_kprobe(old_p, p);
471 DBPRINTF ("kzalloc\n");
473 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
475 ap = kmalloc(sizeof(struct kprobe), GFP_KERNEL);
477 memset(ap, 0, sizeof(struct kprobe));
481 add_aggr_kprobe(ap, old_p);
483 DBPRINTF ("ap = %p p = %p old_p = %p \n", ap, p, old_p);
484 ret = add_new_kprobe(ap, p);
489 EXPORT_SYMBOL_GPL(register_aggr_kprobe);
491 static void remove_kprobe(struct kprobe *p)
493 /* TODO: check boostable for x86 and MIPS */
494 free_insn_slot(&sm, p->ainsn.insn);
497 int dbi_register_kprobe(struct kprobe *p)
499 struct kprobe *old_p;
502 * If we have a symbol_name argument look it up,
503 * and add it to the address. That way the addr
504 * field can either be global or relative to a symbol.
506 if (p->symbol_name) {
509 p->addr = (kprobe_opcode_t *)swap_ksyms(p->symbol_name);
514 DBPRINTF ("p->addr = 0x%p\n", p->addr);
515 p->addr = (kprobe_opcode_t *)(((char *)p->addr) + p->offset);
516 DBPRINTF ("p->addr = 0x%p p = 0x%p\n", p->addr, p);
518 #ifdef KPROBES_PROFILE
519 p->start_tm.tv_sec = p->start_tm.tv_usec = 0;
520 p->hnd_tm_sum.tv_sec = p->hnd_tm_sum.tv_usec = 0;
523 p->mod_refcounted = 0;
525 INIT_LIST_HEAD(&p->list);
527 old_p = get_kprobe(p->addr);
529 ret = register_aggr_kprobe(old_p, p);
531 atomic_inc(&kprobe_count);
535 if ((ret = arch_prepare_kprobe(p, &sm)) != 0)
538 DBPRINTF ("before out ret = 0x%x\n", ret);
539 INIT_HLIST_NODE(&p->hlist);
540 hlist_add_head_rcu(&p->hlist, &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
544 DBPRINTF ("out ret = 0x%x\n", ret);
548 static void dbi_unregister_valid_kprobe(struct kprobe *p, struct kprobe *old_p)
550 struct kprobe *list_p;
552 if ((old_p == p) || ((old_p->pre_handler == aggr_pre_handler) &&
553 (p->list.next == &old_p->list) && (p->list.prev == &old_p->list))) {
554 /* Only probe on the hash list */
555 arch_disarm_kprobe(p);
556 hlist_del_rcu(&old_p->hlist);
560 /* Synchronize and remove probe in bottom */
562 list_del_rcu(&p->list);
564 if (p->break_handler)
565 old_p->break_handler = NULL;
566 if (p->post_handler) {
567 list_for_each_entry_rcu(list_p, &old_p->list, list)
568 if (list_p->post_handler)
571 old_p->post_handler = NULL;
574 /* Set NULL addr for reusability if symbol_name is used */
579 void dbi_unregister_kprobe(struct kprobe *kp)
581 struct kprobe *old_p, *list_p;
583 old_p = get_kprobe(kp->addr);
584 if (unlikely (!old_p))
588 list_for_each_entry_rcu(list_p, &old_p->list, list)
590 /* kprobe p is a valid probe */
591 dbi_unregister_valid_kprobe(kp, old_p);
595 dbi_unregister_valid_kprobe(kp, old_p);
598 int dbi_register_jprobe(struct jprobe *jp)
600 /* Todo: Verify probepoint is a function entry point */
601 jp->kp.pre_handler = setjmp_pre_handler;
602 jp->kp.break_handler = longjmp_break_handler;
604 return dbi_register_kprobe(&jp->kp);
607 void dbi_unregister_jprobe(struct jprobe *jp)
609 dbi_unregister_kprobe(&jp->kp);
613 * This kprobe pre_handler is registered with every kretprobe. When probe
614 * hits it will set up the return probe.
616 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
618 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
619 struct kretprobe_instance *ri;
620 unsigned long flags = 0;
622 /* TODO: consider to only swap the RA after the last pre_handler fired */
623 spin_lock_irqsave(&kretprobe_lock, flags);
625 /* TODO: test - remove retprobe after func entry but before its exit */
626 if ((ri = get_free_rp_inst(rp)) != NULL) {
630 if (rp->entry_handler) {
631 rp->entry_handler(ri, regs);
634 arch_prepare_kretprobe(ri, regs);
641 spin_unlock_irqrestore(&kretprobe_lock, flags);
646 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
648 struct kretprobe_instance *ri = NULL;
649 struct hlist_head *head;
650 unsigned long flags, orig_ret_address = 0;
651 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
653 struct kprobe_ctlblk *kcb;
655 struct hlist_node *tmp;
656 DECLARE_NODE_PTR_FOR_HLIST(node);
659 kcb = get_kprobe_ctlblk();
661 spin_lock_irqsave(&kretprobe_lock, flags);
664 * We are using different hash keys (current and mm) for finding kernel
665 * space and user space probes. Kernel space probes can change mm field in
666 * task_struct. User space probes can be shared between threads of one
667 * process so they have different current but same mm.
669 head = kretprobe_inst_table_head(current);
672 regs->XREG(cs) = __KERNEL_CS | get_kernel_rpl();
673 regs->EREG(ip) = trampoline_address;
674 regs->ORIG_EAX_REG = 0xffffffff;
678 * It is possible to have multiple instances associated with a given
679 * task either because an multiple functions in the call path
680 * have a return probe installed on them, and/or more then one
681 * return probe was registered for a target function.
683 * We can handle this because:
684 * - instances are always inserted at the head of the list
685 * - when multiple return probes are registered for the same
686 * function, the first instance's ret_addr will point to the
687 * real return address, and all the rest will point to
688 * kretprobe_trampoline
690 swap_hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
691 if (ri->task != current)
692 /* another task is sharing our hash bucket */
694 if (ri->rp && ri->rp->handler) {
695 __get_cpu_var(current_kprobe) = &ri->rp->kp;
696 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
697 ri->rp->handler(ri, regs);
698 __get_cpu_var(current_kprobe) = NULL;
701 orig_ret_address = (unsigned long)ri->ret_addr;
703 if (orig_ret_address != trampoline_address)
705 * This is the real return address. Any other
706 * instances associated with this task are for
707 * other calls deeper on the call stack
711 kretprobe_assert(ri, orig_ret_address, trampoline_address);
713 if (kcb->kprobe_status == KPROBE_REENTER) {
714 restore_previous_kprobe(kcb);
716 reset_current_kprobe();
719 spin_unlock_irqrestore(&kretprobe_lock, flags);
720 preempt_enable_no_resched();
723 * By returning a non-zero value, we are telling
724 * kprobe_handler() that we don't want the post_handler
725 * to run (and have re-enabled preemption)
728 return (int)orig_ret_address;
731 #define SCHED_RP_NR 200
732 #define COMMON_RP_NR 10
734 int alloc_nodes_kretprobe(struct kretprobe *rp)
737 struct kretprobe_instance *inst;
740 DBPRINTF("Alloc aditional mem for retprobes");
742 if ((unsigned long)rp->kp.addr == sched_addr) {
743 rp->maxactive += SCHED_RP_NR;//max (100, 2 * NR_CPUS);
744 alloc_nodes = SCHED_RP_NR;
746 #if 1//def CONFIG_PREEMPT
747 rp->maxactive += max (COMMON_RP_NR, 2 * NR_CPUS);
749 rp->maxacpptive += NR_CPUS;
751 alloc_nodes = COMMON_RP_NR;
754 for (i = 0; i < alloc_nodes; i++) {
755 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_ATOMIC);
760 INIT_HLIST_NODE(&inst->uflist);
761 hlist_add_head(&inst->uflist, &rp->free_instances);
764 DBPRINTF ("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr, (unsigned long) (*(rp->kp.addr)), (unsigned long) (*(rp->kp.addr + 1)), (unsigned long) (*(rp->kp.addr + 2)));
768 int dbi_register_kretprobe(struct kretprobe *rp)
771 struct kretprobe_instance *inst;
775 rp->kp.pre_handler = pre_handler_kretprobe;
776 rp->kp.post_handler = NULL;
777 rp->kp.fault_handler = NULL;
778 rp->kp.break_handler = NULL;
780 /* Pre-allocate memory for max kretprobe instances */
781 if ((unsigned long)rp->kp.addr == exit_addr) {
782 rp->kp.pre_handler = NULL; //not needed for do_exit
784 } else if ((unsigned long)rp->kp.addr == do_group_exit_addr) {
785 rp->kp.pre_handler = NULL;
787 } else if ((unsigned long)rp->kp.addr == sys_exit_group_addr) {
788 rp->kp.pre_handler = NULL;
790 } else if ((unsigned long)rp->kp.addr == sys_exit_addr) {
791 rp->kp.pre_handler = NULL;
793 } else if (rp->maxactive <= 0) {
794 #if 1//def CONFIG_PREEMPT
795 rp->maxactive = max (COMMON_RP_NR, 2 * NR_CPUS);
797 rp->maxactive = NR_CPUS;
800 INIT_HLIST_HEAD(&rp->used_instances);
801 INIT_HLIST_HEAD(&rp->free_instances);
802 for (i = 0; i < rp->maxactive; i++) {
803 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_KERNEL);
808 INIT_HLIST_NODE(&inst->uflist);
809 hlist_add_head(&inst->uflist, &rp->free_instances);
812 DBPRINTF ("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr, (unsigned long) (*(rp->kp.addr)), (unsigned long) (*(rp->kp.addr + 1)), (unsigned long) (*(rp->kp.addr + 2)));
814 /* Establish function entry probe point */
815 if ((ret = dbi_register_kprobe(&rp->kp)) != 0)
818 DBPRINTF ("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr, (unsigned long) (*(rp->kp.addr)), (unsigned long) (*(rp->kp.addr + 1)), (unsigned long) (*(rp->kp.addr + 2)));
823 static int dbi_disarm_krp_inst(struct kretprobe_instance *ri);
825 static void dbi_unregister_kretprobe_top(struct kretprobe *rp)
827 struct kretprobe_instance *ri;
828 DECLARE_NODE_PTR_FOR_HLIST(node);
830 dbi_unregister_kprobe(&rp->kp);
832 swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
833 if (!dbi_disarm_krp_inst(ri)) {
834 printk("%s (%d/%d): cannot disarm krp instance (%08lx)\n",
835 ri->task->comm, ri->task->tgid, ri->task->pid,
836 (unsigned long)rp->kp.addr);
841 static void dbi_unregister_kretprobe_bottom(struct kretprobe *rp)
844 struct kretprobe_instance *ri;
846 if (list_empty(&rp->kp.list))
847 remove_kprobe(&rp->kp);
849 spin_lock_irqsave(&kretprobe_lock, flags);
851 while ((ri = get_used_rp_inst(rp)) != NULL) {
856 spin_unlock_irqrestore(&kretprobe_lock, flags);
859 void dbi_unregister_kretprobes(struct kretprobe **rpp, size_t size)
864 spin_lock_irqsave(&kretprobe_lock, flags);
866 for (i = 0; i < size; i++)
867 dbi_unregister_kretprobe_top(rpp[i]);
869 spin_unlock_irqrestore(&kretprobe_lock, flags);
874 for (i = 0; i < size; i++)
875 dbi_unregister_kretprobe_bottom(rpp[i]);
878 void dbi_unregister_kretprobe(struct kretprobe *rp)
880 dbi_unregister_kretprobes(&rp, 1);
883 struct kretprobe *clone_kretprobe(struct kretprobe *rp)
885 struct kprobe *old_p;
886 struct kretprobe *clone = NULL;
889 clone = kmalloc(sizeof(struct kretprobe), GFP_KERNEL);
891 DBPRINTF ("failed to alloc memory for clone probe %p!", rp->kp.addr);
894 memcpy(clone, rp, sizeof(struct kretprobe));
895 clone->kp.pre_handler = pre_handler_kretprobe;
896 clone->kp.post_handler = NULL;
897 clone->kp.fault_handler = NULL;
898 clone->kp.break_handler = NULL;
899 old_p = get_kprobe(rp->kp.addr);
901 ret = register_aggr_kprobe(old_p, &clone->kp);
906 atomic_inc(&kprobe_count);
911 EXPORT_SYMBOL_GPL(clone_kretprobe);
913 static void inline rm_task_trampoline(struct task_struct *p, struct kretprobe_instance *ri)
915 arch_set_task_pc(p, (unsigned long)ri->ret_addr);
918 static int dbi_disarm_krp_inst(struct kretprobe_instance *ri)
920 unsigned long *tramp = (unsigned long *)&kretprobe_trampoline;
921 unsigned long *sp = ri->sp;
922 unsigned long *found = NULL;
923 int retval = -ENOENT;
926 unsigned long pc = arch_get_task_pc(ri->task);
928 printk("---> [%d] %s (%d/%d): pc = %08lx, ra = %08lx, tramp= %08lx (%08lx)\n",
930 ri->task->comm, ri->task->tgid, ri->task->pid,
931 pc, (long unsigned int)ri->ret_addr,
932 (long unsigned int)tramp,
933 (long unsigned int)(ri->rp ? ri->rp->kp.addr: NULL));
935 /* __switch_to retprobe handling */
936 if (pc == (unsigned long)tramp) {
937 rm_task_trampoline(ri->task, ri);
944 while (sp > ri->sp - RETPROBE_STACK_DEPTH) {
945 if (*sp == (unsigned long)tramp) {
953 printk("---> [%d] %s (%d/%d): tramp (%08lx) found at %08lx (%08lx /%+d) - %p\n",
955 ri->task->comm, ri->task->tgid, ri->task->pid,
956 (long unsigned int)tramp,
957 (long unsigned int)found, (long unsigned int)ri->sp,
958 found - ri->sp, ri->rp ? ri->rp->kp.addr: NULL);
959 *found = (unsigned long)ri->ret_addr;
962 printk("---> [%d] %s (%d/%d): tramp (%08lx) NOT found at sp = %08lx - %p\n",
964 ri->task->comm, ri->task->tgid, ri->task->pid,
965 (long unsigned int)tramp,
966 (long unsigned int)ri->sp, ri->rp ? ri->rp->kp.addr: NULL);
972 static int init_module_deps(void)
976 sched_addr = swap_ksyms("__switch_to");
977 exit_addr = swap_ksyms("do_exit");
978 sys_exit_group_addr = swap_ksyms("sys_exit_group");
979 do_group_exit_addr = swap_ksyms("do_group_exit");
980 sys_exit_addr = swap_ksyms("sys_exit");
982 if (sched_addr == 0 ||
984 sys_exit_group_addr == 0 ||
985 do_group_exit_addr == 0 ||
986 sys_exit_addr == 0) {
990 ret = init_module_dependencies();
995 return arch_init_module_deps();
998 static int __init init_kprobes(void)
1002 module_alloc = (void *)swap_ksyms("module_alloc");
1003 if (!module_alloc) {
1004 printk("module_alloc is not found! Oops.\n");
1007 module_free = (void *)swap_ksyms("module_free");
1008 if (!module_alloc) {
1009 printk("module_free is not found! Oops.\n");
1015 /* FIXME allocate the probe table, currently defined statically */
1016 /* initialize all list heads */
1017 for (i = 0; i < KPROBE_TABLE_SIZE; ++i) {
1018 INIT_HLIST_HEAD(&kprobe_table[i]);
1019 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
1021 atomic_set(&kprobe_count, 0);
1023 err = init_module_deps();
1028 err = arch_init_kprobes();
1030 DBPRINTF ("init_kprobes: arch_init_kprobes - %d", err);
1035 static void __exit exit_kprobes(void)
1037 arch_exit_kprobes();
1041 module_init(init_kprobes);
1042 module_exit(exit_kprobes);
1044 EXPORT_SYMBOL_GPL(dbi_register_kprobe);
1045 EXPORT_SYMBOL_GPL(dbi_unregister_kprobe);
1046 EXPORT_SYMBOL_GPL(dbi_register_jprobe);
1047 EXPORT_SYMBOL_GPL(dbi_unregister_jprobe);
1048 EXPORT_SYMBOL_GPL(dbi_jprobe_return);
1049 EXPORT_SYMBOL_GPL(dbi_register_kretprobe);
1050 EXPORT_SYMBOL_GPL(dbi_unregister_kretprobes);
1051 EXPORT_SYMBOL_GPL(dbi_unregister_kretprobe);
1053 MODULE_LICENSE("Dual BSD/GPL");