2 * kprobe/swap_kprobes.c
3 * @author Ekaterina Gorelkina <e.gorelkina@samsung.com>: initial implementation for ARM and MIPS
4 * @author Alexey Gerenkov <a.gerenkov@samsung.com> User-Space Probes initial implementation;
5 * Support x86/ARM/MIPS for both user and kernel spaces.
6 * @author Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * Copyright (C) IBM Corporation, 2002, 2004
27 * Copyright (C) Samsung Electronics, 2006-2010
29 * @section DESCRIPTION
31 * SWAP kprobe implementation. Dynamic kernel functions instrumentation.
34 #include <linux/version.h>
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
36 #include <linux/config.h>
39 #include <linux/hash.h>
40 #include <linux/module.h>
42 #include <linux/pagemap.h>
43 #include <linux/stop_machine.h>
44 #include <linux/delay.h>
45 #include <ksyms/ksyms.h>
46 #include <master/swap_initializer.h>
47 #include <swap-asm/swap_kprobes.h>
49 #include "swap_slots.h"
51 #include "swap_td_raw.h"
52 #include "swap_kdebug.h"
53 #include "swap_kprobes.h"
54 #include "swap_kprobes_deps.h"
58 * @brief Scheduler address.
60 unsigned long sched_addr;
61 static unsigned long exit_addr;
62 static unsigned long do_group_exit_addr;
63 static unsigned long sys_exit_group_addr;
64 static unsigned long sys_exit_addr;
68 * @brief Current slot manager. Slots are the places where trampolines are
71 struct slot_manager sm;
73 static DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
75 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
76 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
80 * @brief Count of kprobes.
82 atomic_t kprobe_count;
83 EXPORT_SYMBOL_GPL(kprobe_count);
86 static void *(*module_alloc)(unsigned long size);
87 static void *(*module_free)(struct module *mod, void *module_region);
89 static void *__wrapper_module_alloc(unsigned long size)
91 return module_alloc(size);
94 static void *__wrapper_module_free(void *module_region)
96 return module_free(NULL, module_region);
99 static void *sm_alloc(struct slot_manager *sm)
101 return __wrapper_module_alloc(PAGE_SIZE);
104 static void sm_free(struct slot_manager *sm, void *ptr)
106 __wrapper_module_free(ptr);
109 static void init_sm(void)
111 sm.slot_size = KPROBES_TRAMP_LEN;
114 INIT_HLIST_HEAD(&sm.page_list);
117 static void exit_sm(void)
122 static struct hlist_head *kpt_head_by_addr(unsigned long addr)
124 return &kprobe_table[hash_ptr((void *)addr, KPROBE_HASH_BITS)];
127 static void kretprobe_assert(struct kretprobe_instance *ri,
128 unsigned long orig_ret_address,
129 unsigned long trampoline_address)
131 if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
132 struct task_struct *task;
134 panic("kretprobe BUG!: ri = NULL\n");
139 panic("kretprobe BUG!: task = NULL\n");
142 panic("kretprobe BUG!: ri->rp = NULL\n");
144 panic("kretprobe BUG!: "
145 "Processing kretprobe %p @ %08lx (%d/%d - %s)\n",
146 ri->rp, ri->rp->kp.addr, ri->task->tgid,
147 ri->task->pid, ri->task->comm);
152 struct kp_core *running;
153 struct kp_core_ctlblk ctlblk;
156 static void ktd_cur_init(struct task_struct *task, void *data)
158 struct kpc_data *d = (struct kpc_data *)data;
160 memset(d, 0, sizeof(*d));
163 static void ktd_cur_exit(struct task_struct *task, void *data)
165 struct kpc_data *d = (struct kpc_data *)data;
167 WARN(d->running, "running probe is not NULL");
170 struct ktask_data ktd_cur = {
171 .init = ktd_cur_init,
172 .exit = ktd_cur_exit,
173 .size = sizeof(struct kpc_data),
176 static DEFINE_PER_CPU(struct kpc_data, per_cpu_kpc_data);
178 static struct kpc_data *kp_core_data(void)
181 return (struct kpc_data *)swap_ktd(&ktd_cur, current);
183 return &__get_cpu_var(per_cpu_kpc_data);
186 static int kprobe_cur_reg(void)
188 return swap_ktd_reg(&ktd_cur);
191 static void kprobe_cur_unreg(void)
193 swap_ktd_unreg(&ktd_cur);
196 struct kp_core *kp_core_running(void)
198 return kp_core_data()->running;
201 void kp_core_running_set(struct kp_core *p)
203 kp_core_data()->running = p;
207 * @brief Gets kp_core_ctlblk for the current CPU.
209 * @return Current CPU struct kp_core_ctlblk.
211 struct kp_core_ctlblk *kp_core_ctlblk(void)
213 return &kp_core_data()->ctlblk;
217 * This routine is called either:
218 * - under the kprobe_mutex - during kprobe_[un]register()
220 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
224 * @brief Gets kp_core.
226 * @param addr Probe address.
227 * @return kprobe_core for addr.
229 struct kp_core *kp_core_by_addr(unsigned long addr)
231 struct hlist_head *head;
232 struct kp_core *core;
233 DECLARE_NODE_PTR_FOR_HLIST(node);
235 head = kpt_head_by_addr(addr);
236 swap_hlist_for_each_entry_rcu(core, node, head, hlist) {
237 if (core->addr == addr)
245 static int alloc_nodes_kretprobe(struct kretprobe *rp);
247 /* Called with kretprobe_lock held */
248 static struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
250 struct kretprobe_instance *ri;
251 DECLARE_NODE_PTR_FOR_HLIST(node);
253 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
257 if (!alloc_nodes_kretprobe(rp)) {
258 swap_hlist_for_each_entry(ri, node, &rp->free_instances,
267 /* Called with kretprobe_lock held */
268 static struct kretprobe_instance *
269 get_free_rp_inst_no_alloc(struct kretprobe *rp)
271 struct kretprobe_instance *ri;
272 DECLARE_NODE_PTR_FOR_HLIST(node);
274 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
281 /* Called with kretprobe_lock held */
282 static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
284 struct kretprobe_instance *ri;
285 DECLARE_NODE_PTR_FOR_HLIST(node);
287 swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
294 /* Called with kretprobe_lock held */
295 static void add_rp_inst(struct kretprobe_instance *ri)
298 * Remove rp inst off the free list -
299 * Add it back when probed function returns
301 hlist_del(&ri->uflist);
303 /* Add rp inst onto table */
304 INIT_HLIST_NODE(&ri->hlist);
306 hlist_add_head(&ri->hlist,
307 &kretprobe_inst_table[hash_ptr(ri->task,
310 /* Also add this rp inst to the used list. */
311 INIT_HLIST_NODE(&ri->uflist);
312 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
315 /* Called with kretprobe_lock held */
316 static void recycle_rp_inst(struct kretprobe_instance *ri)
319 hlist_del(&ri->hlist);
320 /* remove rp inst off the used list */
321 hlist_del(&ri->uflist);
322 /* put rp inst back onto the free list */
323 INIT_HLIST_NODE(&ri->uflist);
324 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
328 static struct hlist_head *kretprobe_inst_table_head(void *hash_key)
330 return &kretprobe_inst_table[hash_ptr(hash_key, KPROBE_HASH_BITS)];
333 static void free_rp_inst(struct kretprobe *rp)
335 struct kretprobe_instance *ri;
336 while ((ri = get_free_rp_inst_no_alloc(rp)) != NULL) {
337 hlist_del(&ri->uflist);
342 static void kp_core_remove(struct kp_core *core)
344 /* TODO: check boostable for x86 and MIPS */
345 swap_slot_free(&sm, core->ainsn.insn);
348 static void kp_core_wait(struct kp_core *p)
352 while (atomic_read(&p->usage)) {
354 ms += ms < 7 ? 1 : 0;
358 static struct kp_core *kp_core_create(unsigned long addr)
360 struct kp_core *core;
362 core = kzalloc(sizeof(*core), GFP_KERNEL);
364 INIT_HLIST_NODE(&core->hlist);
366 atomic_set(&core->usage, 0);
367 rwlock_init(&core->handlers.lock);
373 static void kp_core_free(struct kp_core *core)
375 WARN_ON(atomic_read(&core->usage));
379 static int pre_handler_one(struct kp_core *core, struct pt_regs *regs)
382 struct kprobe *p = core->handlers.kps[0];
384 if (p && p->pre_handler)
385 ret = p->pre_handler(p, regs);
390 static int pre_handler_multi(struct kp_core *core, struct pt_regs *regs)
394 /* TODO: add sync use kprobe */
395 for (i = 0; i < ARRAY_SIZE(core->handlers.kps); ++i) {
396 struct kprobe *p = core->handlers.kps[i];
398 if (p && p->pre_handler) {
399 ret = p->pre_handler(p, regs);
408 static int kp_core_add_kprobe(struct kp_core *core, struct kprobe *p)
412 struct kp_handlers *h = &core->handlers;
414 write_lock_irqsave(&h->lock, flags);
415 if (h->pre == NULL) {
416 h->pre = pre_handler_one;
417 } else if (h->pre == pre_handler_one) {
418 h->pre = pre_handler_multi;
421 for (i = 0; i < ARRAY_SIZE(core->handlers.kps); ++i) {
422 if (core->handlers.kps[i])
425 core->handlers.kps[i] = p;
429 pr_err("all kps slots is busy\n");
432 write_unlock_irqrestore(&h->lock, flags);
436 static void kp_core_del_kprobe(struct kp_core *core, struct kprobe *p)
440 struct kp_handlers *h = &core->handlers;
442 write_lock_irqsave(&h->lock, flags);
443 for (i = 0; i < ARRAY_SIZE(h->kps); ++i) {
447 if (h->kps[i] == NULL)
450 write_unlock_irqrestore(&h->lock, flags);
452 if (cnt == ARRAY_SIZE(h->kps)) {
453 arch_kp_core_disarm(core);
456 hlist_del_rcu(&core->hlist);
460 kp_core_remove(core);
465 static DEFINE_MUTEX(kp_mtx);
467 * @brief Registers kprobe.
469 * @param p Pointer to the target kprobe.
470 * @return 0 on success, error code on error.
472 int swap_register_kprobe(struct kprobe *p)
474 struct kp_core *core;
478 * If we have a symbol_name argument look it up,
479 * and add it to the address. That way the addr
480 * field can either be global or relative to a symbol.
482 if (p->symbol_name) {
485 p->addr = swap_ksyms(p->symbol_name);
491 addr = p->addr + p->offset;
494 core = kp_core_by_addr(addr);
496 core = kp_core_create(addr);
498 pr_err("Out of memory\n");
503 ret = arch_kp_core_prepare(core, &sm);
507 ret = kp_core_add_kprobe(core, p);
513 hlist_add_head_rcu(&core->hlist, kpt_head_by_addr(core->addr));
514 arch_kp_core_arm(core);
516 ret = kp_core_add_kprobe(core, p);
520 mutex_unlock(&kp_mtx);
523 EXPORT_SYMBOL_GPL(swap_register_kprobe);
526 * @brief Unregistes kprobe.
528 * @param kp Pointer to the target kprobe.
531 void swap_unregister_kprobe(struct kprobe *p)
533 unsigned long addr = p->addr + p->offset;
534 struct kp_core *core;
537 core = kp_core_by_addr(addr);
538 BUG_ON(core == NULL);
540 kp_core_del_kprobe(core, p);
541 mutex_unlock(&kp_mtx);
543 /* Set 0 addr for reusability if symbol_name is used */
547 EXPORT_SYMBOL_GPL(swap_unregister_kprobe);
550 * @brief Registers jprobe.
552 * @param jp Pointer to the target jprobe.
553 * @return swap_register_kprobe result.
555 int swap_register_jprobe(struct jprobe *jp)
557 /* Todo: Verify probepoint is a function entry point */
558 jp->kp.pre_handler = swap_setjmp_pre_handler;
560 return swap_register_kprobe(&jp->kp);
562 EXPORT_SYMBOL_GPL(swap_register_jprobe);
565 * @brief Unregisters jprobe.
567 * @param jp Pointer to the target jprobe.
570 void swap_unregister_jprobe(struct jprobe *jp)
572 swap_unregister_kprobe(&jp->kp);
574 EXPORT_SYMBOL_GPL(swap_unregister_jprobe);
577 * This kprobe pre_handler is registered with every kretprobe. When probe
578 * hits it will set up the return probe.
580 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
582 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
583 struct kretprobe_instance *ri;
584 unsigned long flags = 0;
586 /* TODO: consider to only swap the RA
587 * after the last pre_handler fired */
588 spin_lock_irqsave(&kretprobe_lock, flags);
590 /* TODO: test - remove retprobe after func entry but before its exit */
591 ri = get_free_rp_inst(rp);
598 if (rp->entry_handler)
599 skip = rp->entry_handler(ri, regs);
605 swap_arch_prepare_kretprobe(ri, regs);
612 spin_unlock_irqrestore(&kretprobe_lock, flags);
618 * @brief Trampoline probe handler.
620 * @param p Pointer to the fired kprobe.
621 * @param regs Pointer to CPU registers data.
622 * @return orig_ret_address
624 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
626 struct kretprobe_instance *ri = NULL;
627 struct hlist_head *head;
628 unsigned long flags, orig_ret_address = 0;
629 unsigned long trampoline_address;
631 struct kp_core_ctlblk *kcb;
633 struct hlist_node *tmp;
634 DECLARE_NODE_PTR_FOR_HLIST(node);
636 trampoline_address = (unsigned long)&swap_kretprobe_trampoline;
638 kcb = kp_core_ctlblk();
640 spin_lock_irqsave(&kretprobe_lock, flags);
643 * We are using different hash keys (current and mm) for finding kernel
644 * space and user space probes. Kernel space probes can change mm field
645 * in task_struct. User space probes can be shared between threads of
646 * one process so they have different current but same mm.
648 head = kretprobe_inst_table_head(current);
651 regs->XREG(cs) = __KERNEL_CS | get_kernel_rpl();
652 regs->EREG(ip) = trampoline_address;
653 regs->ORIG_EAX_REG = 0xffffffff;
657 * It is possible to have multiple instances associated with a given
658 * task either because an multiple functions in the call path
659 * have a return probe installed on them, and/or more then one
660 * return probe was registered for a target function.
662 * We can handle this because:
663 * - instances are always inserted at the head of the list
664 * - when multiple return probes are registered for the same
665 * function, the first instance's ret_addr will point to the
666 * real return address, and all the rest will point to
667 * kretprobe_trampoline
669 swap_hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
670 if (ri->task != current)
671 /* another task is sharing our hash bucket */
673 if (ri->rp && ri->rp->handler) {
675 * Set fake current probe, we don't
676 * want to go into recursion
678 kp_core_running_set((struct kp_core *)0xfffff);
679 kcb->kp_core_status = KPROBE_HIT_ACTIVE;
680 ri->rp->handler(ri, regs);
681 kp_core_running_set(NULL);
684 orig_ret_address = (unsigned long)ri->ret_addr;
686 if (orig_ret_address != trampoline_address)
688 * This is the real return address. Any other
689 * instances associated with this task are for
690 * other calls deeper on the call stack
694 kretprobe_assert(ri, orig_ret_address, trampoline_address);
696 if (kcb->kp_core_status == KPROBE_REENTER)
697 restore_previous_kp_core(kcb);
699 kp_core_running_set(NULL);
701 spin_unlock_irqrestore(&kretprobe_lock, flags);
704 * By returning a non-zero value, we are telling
705 * kprobe_handler() that we don't want the post_handler
706 * to run (and have re-enabled preemption)
709 return (int)orig_ret_address;
712 #define SCHED_RP_NR 200
713 #define COMMON_RP_NR 10
715 static int alloc_nodes_kretprobe(struct kretprobe *rp)
718 struct kretprobe_instance *inst;
721 DBPRINTF("Alloc aditional mem for retprobes");
723 if (rp->kp.addr == sched_addr) {
724 rp->maxactive += SCHED_RP_NR; /* max (100, 2 * NR_CPUS); */
725 alloc_nodes = SCHED_RP_NR;
727 #if 1/* def CONFIG_PREEMPT */
728 rp->maxactive += max(COMMON_RP_NR, 2 * NR_CPUS);
730 rp->maxacpptive += NR_CPUS;
732 alloc_nodes = COMMON_RP_NR;
735 for (i = 0; i < alloc_nodes; i++) {
736 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_ATOMIC);
741 INIT_HLIST_NODE(&inst->uflist);
742 hlist_add_head(&inst->uflist, &rp->free_instances);
745 DBPRINTF("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr,
746 (unsigned long) (*(rp->kp.addr)),
747 (unsigned long) (*(rp->kp.addr + 1)),
748 (unsigned long) (*(rp->kp.addr + 2)));
753 * @brief Registers kretprobes.
755 * @param rp Pointer to the target kretprobe.
756 * @return 0 on success, error code on error.
758 int swap_register_kretprobe(struct kretprobe *rp)
761 struct kretprobe_instance *inst;
765 rp->kp.pre_handler = pre_handler_kretprobe;
767 /* Pre-allocate memory for max kretprobe instances */
768 if (rp->kp.addr == exit_addr) {
769 rp->kp.pre_handler = NULL; /* not needed for do_exit */
771 } else if (rp->kp.addr == do_group_exit_addr) {
772 rp->kp.pre_handler = NULL;
774 } else if (rp->kp.addr == sys_exit_group_addr) {
775 rp->kp.pre_handler = NULL;
777 } else if (rp->kp.addr == sys_exit_addr) {
778 rp->kp.pre_handler = NULL;
780 } else if (rp->maxactive <= 0) {
781 #if 1/* def CONFIG_PREEMPT */
782 rp->maxactive = max(COMMON_RP_NR, 2 * NR_CPUS);
784 rp->maxactive = NR_CPUS;
787 INIT_HLIST_HEAD(&rp->used_instances);
788 INIT_HLIST_HEAD(&rp->free_instances);
789 for (i = 0; i < rp->maxactive; i++) {
790 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_KERNEL);
795 INIT_HLIST_NODE(&inst->uflist);
796 hlist_add_head(&inst->uflist, &rp->free_instances);
799 DBPRINTF("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr,
800 (unsigned long) (*(rp->kp.addr)),
801 (unsigned long) (*(rp->kp.addr + 1)),
802 (unsigned long) (*(rp->kp.addr + 2)));
804 /* Establish function entry probe point */
805 ret = swap_register_kprobe(&rp->kp);
809 DBPRINTF("addr=%p, *addr=[%lx %lx %lx]", rp->kp.addr,
810 (unsigned long) (*(rp->kp.addr)),
811 (unsigned long) (*(rp->kp.addr + 1)),
812 (unsigned long) (*(rp->kp.addr + 2)));
816 EXPORT_SYMBOL_GPL(swap_register_kretprobe);
818 static int swap_disarm_krp_inst(struct kretprobe_instance *ri);
820 static void swap_disarm_krp(struct kretprobe *rp)
822 struct kretprobe_instance *ri;
823 DECLARE_NODE_PTR_FOR_HLIST(node);
825 swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
826 if (swap_disarm_krp_inst(ri) != 0) {
827 printk(KERN_INFO "%s (%d/%d): cannot disarm "
828 "krp instance (%08lx)\n",
829 ri->task->comm, ri->task->tgid, ri->task->pid,
836 struct unreg_krp_args {
837 struct kretprobe **rps;
842 static int __swap_unregister_kretprobes_top(void *data)
844 struct unreg_krp_args *args = data;
845 struct kretprobe **rps = args->rps;
846 size_t size = args->size;
847 int rp_disarm = args->rp_disarm;
849 const size_t end = ((size_t) 0) - 1;
851 for (--size; size != end; --size) {
853 spin_lock_irqsave(&kretprobe_lock, flags);
854 swap_disarm_krp(rps[size]);
855 spin_unlock_irqrestore(&kretprobe_lock, flags);
863 * @brief Kretprobes unregister top. Unregisters kprobes.
865 * @param rps Pointer to the array of pointers to the target kretprobes.
866 * @param size Size of rps array.
867 * @param rp_disarm Disarm flag. If set kretprobe is disarmed.
870 void swap_unregister_kretprobes_top(struct kretprobe **rps, size_t size,
873 struct unreg_krp_args args = {
876 .rp_disarm = rp_disarm,
878 const size_t end = ((size_t)0) - 1;
880 for (--size; size != end; --size)
881 swap_unregister_kprobe(&rps[size]->kp);
886 ret = stop_machine(__swap_unregister_kretprobes_top,
889 pr_err("%s failed (%d)\n", __func__, ret);
891 __swap_unregister_kretprobes_top(&args);
894 EXPORT_SYMBOL_GPL(swap_unregister_kretprobes_top);
897 * @brief swap_unregister_kretprobes_top wrapper for a single kretprobe.
899 * @param rp Pointer to the target kretprobe.
900 * @param rp_disarm Disarm flag.
903 void swap_unregister_kretprobe_top(struct kretprobe *rp, int rp_disarm)
905 swap_unregister_kretprobes_top(&rp, 1, rp_disarm);
907 EXPORT_SYMBOL_GPL(swap_unregister_kretprobe_top);
910 * @brief Kretprobe unregister bottom. Here is kretprobe memory is released.
912 * @param rp Pointer to the target kretprobe.
915 void swap_unregister_kretprobe_bottom(struct kretprobe *rp)
918 struct kretprobe_instance *ri;
920 spin_lock_irqsave(&kretprobe_lock, flags);
922 while ((ri = get_used_rp_inst(rp)) != NULL)
926 spin_unlock_irqrestore(&kretprobe_lock, flags);
928 EXPORT_SYMBOL_GPL(swap_unregister_kretprobe_bottom);
931 * @brief swap_unregister_kretprobe_bottom wrapper for several kretprobes.
933 * @param rps Pointer to the array of the target kretprobes pointers.
934 * @param size Size of rps array.
937 void swap_unregister_kretprobes_bottom(struct kretprobe **rps, size_t size)
939 const size_t end = ((size_t) 0) - 1;
941 for (--size; size != end; --size)
942 swap_unregister_kretprobe_bottom(rps[size]);
944 EXPORT_SYMBOL_GPL(swap_unregister_kretprobes_bottom);
947 * @brief Unregisters kretprobes.
949 * @param rpp Pointer to the array of the target kretprobes pointers.
950 * @param size Size of rpp array.
953 void swap_unregister_kretprobes(struct kretprobe **rpp, size_t size)
955 swap_unregister_kretprobes_top(rpp, size, 1);
960 swap_unregister_kretprobes_bottom(rpp, size);
962 EXPORT_SYMBOL_GPL(swap_unregister_kretprobes);
965 * @brief swap_unregister_kretprobes wrapper for a single kretprobe.
967 * @param rp Pointer to the target kretprobe.
970 void swap_unregister_kretprobe(struct kretprobe *rp)
972 swap_unregister_kretprobes(&rp, 1);
974 EXPORT_SYMBOL_GPL(swap_unregister_kretprobe);
976 static inline void rm_task_trampoline(struct task_struct *p,
977 struct kretprobe_instance *ri)
979 arch_set_task_pc(p, (unsigned long)ri->ret_addr);
982 static int swap_disarm_krp_inst(struct kretprobe_instance *ri)
984 unsigned long *tramp = (unsigned long *)&swap_kretprobe_trampoline;
985 unsigned long *sp = ri->sp;
986 unsigned long *found = NULL;
987 int retval = -ENOENT;
990 unsigned long pc = arch_get_task_pc(ri->task);
992 printk(KERN_INFO "---> [%d] %s (%d/%d): pc = %08lx, ra = %08lx, tramp= %08lx (%08lx)\n",
994 ri->task->comm, ri->task->tgid, ri->task->pid,
995 pc, (long unsigned int)ri->ret_addr,
996 (long unsigned int)tramp,
997 (ri->rp ? ri->rp->kp.addr : 0));
999 /* __switch_to retprobe handling */
1000 if (pc == (unsigned long)tramp) {
1001 rm_task_trampoline(ri->task, ri);
1008 while (sp > ri->sp - RETPROBE_STACK_DEPTH) {
1009 if (*sp == (unsigned long)tramp) {
1017 printk(KERN_INFO "---> [%d] %s (%d/%d): tramp (%08lx) "
1018 "found at %08lx (%08lx /%+d) - %08lx\n",
1020 ri->task->comm, ri->task->tgid, ri->task->pid,
1021 (long unsigned int)tramp,
1022 (long unsigned int)found, (long unsigned int)ri->sp,
1023 found - ri->sp, ri->rp ? ri->rp->kp.addr : 0);
1024 *found = (unsigned long)ri->ret_addr;
1027 printk(KERN_INFO "---> [%d] %s (%d/%d): tramp (%08lx) "
1028 "NOT found at sp = %08lx - %08lx\n",
1030 ri->task->comm, ri->task->tgid, ri->task->pid,
1031 (long unsigned int)tramp,
1032 (long unsigned int)ri->sp,
1033 ri->rp ? ri->rp->kp.addr : 0);
1039 static void krp_inst_flush(struct task_struct *task)
1041 unsigned long flags;
1042 struct kretprobe_instance *ri;
1043 struct hlist_node *tmp;
1044 struct hlist_head *head;
1045 DECLARE_NODE_PTR_FOR_HLIST(node);
1047 spin_lock_irqsave(&kretprobe_lock, flags);
1048 head = kretprobe_inst_table_head(task);
1049 swap_hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
1050 if (ri->task == task) {
1051 printk("task[%u %u %s]: flush krp_inst, ret_addr=%p\n",
1052 task->tgid, task->pid, task->comm,
1054 recycle_rp_inst(ri);
1057 spin_unlock_irqrestore(&kretprobe_lock, flags);
1060 /* Handler is called the last because it is registered the first */
1061 static int put_task_handler(struct kprobe *p, struct pt_regs *regs)
1063 struct task_struct *t = (struct task_struct *)swap_get_karg(regs, 0);
1067 swap_ktd_put_task(t);
1072 static struct kprobe put_task_kp = {
1073 .pre_handler = put_task_handler,
1076 static int init_module_deps(void)
1080 sched_addr = swap_ksyms("__switch_to");
1081 exit_addr = swap_ksyms("do_exit");
1082 sys_exit_group_addr = swap_ksyms("sys_exit_group");
1083 do_group_exit_addr = swap_ksyms("do_group_exit");
1084 sys_exit_addr = swap_ksyms("sys_exit");
1086 if (sched_addr == 0 ||
1088 sys_exit_group_addr == 0 ||
1089 do_group_exit_addr == 0 ||
1090 sys_exit_addr == 0) {
1094 ret = init_module_dependencies();
1098 return arch_init_module_deps();
1101 static int once(void)
1106 sym = "module_alloc";
1107 module_alloc = (void *)swap_ksyms(sym);
1108 if (module_alloc == NULL)
1111 sym = "module_free";
1112 module_free = (void *)swap_ksyms(sym);
1113 if (module_alloc == NULL)
1116 sym = "__put_task_struct";
1117 put_task_kp.addr = swap_ksyms(sym);
1118 if (put_task_kp.addr == 0)
1121 ret = init_module_deps();
1126 * FIXME allocate the probe table, currently defined statically
1127 * initialize all list heads
1129 for (i = 0; i < KPROBE_TABLE_SIZE; ++i) {
1130 INIT_HLIST_HEAD(&kprobe_table[i]);
1131 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
1137 printk(KERN_INFO "ERROR: symbol '%s' not found\n", sym);
1141 static int init_kprobes(void)
1146 atomic_set(&kprobe_count, 0);
1148 ret = swap_td_raw_init();
1152 ret = swap_arch_init_kprobes();
1156 ret = swap_ktd_init();
1160 ret = kprobe_cur_reg();
1164 ret = swap_register_kprobe(&put_task_kp);
1173 swap_ktd_uninit_top();
1174 swap_ktd_uninit_bottom();
1176 swap_arch_exit_kprobes();
1178 swap_td_raw_uninit();
1182 static void exit_kprobes(void)
1184 swap_ktd_uninit_top();
1185 swap_unregister_kprobe(&put_task_kp);
1187 swap_ktd_uninit_bottom();
1188 swap_arch_exit_kprobes();
1189 swap_td_raw_uninit();
1193 SWAP_LIGHT_INIT_MODULE(once, init_kprobes, exit_kprobes, NULL, NULL);
1195 MODULE_LICENSE("GPL");