[REFACTOR] remove field priv_arg from struct kretprobe
[kernel/swap-modules.git] / kprobe / dbi_kprobes.c
1 /*
2  *  Kernel Probes (KProbes)
3  *  kernel/kprobes.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) IBM Corporation, 2002, 2004
20  */
21
22 /*
23  *  Dynamic Binary Instrumentation Module based on KProbes
24  *  modules/kprobe/dbi_kprobes.h
25  *
26  * This program is free software; you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation; either version 2 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program; if not, write to the Free Software
38  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39  *
40  * Copyright (C) Samsung Electronics, 2006-2010
41  *
42  * 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
46  *
47  */
48
49 #include "dbi_kprobes.h"
50 #include "arch/asm/dbi_kprobes.h"
51
52 #include "dbi_kdebug.h"
53 #include "dbi_kprobes_deps.h"
54 #include "dbi_insn_slots.h"
55 #include <ksyms.h>
56
57 #include <linux/version.h>
58 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
59 #include <linux/config.h>
60 #endif
61
62 #include <linux/hash.h>
63 #include <linux/module.h>
64 #include <linux/mm.h>
65 #include <linux/pagemap.h>
66
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;
72
73 struct slot_manager sm;
74
75 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
76 static DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
77
78 DEFINE_SPINLOCK(kretprobe_lock);        /* Protects kretprobe_inst_table */
79 EXPORT_SYMBOL_GPL(kretprobe_lock);
80 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
81
82 struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
83 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
84
85 atomic_t kprobe_count;
86 EXPORT_SYMBOL_GPL(kprobe_count);
87
88
89 static void *(*module_alloc)(unsigned long size) = NULL;
90 static void *(*module_free)(struct module *mod, void *module_region) = NULL;
91
92 static void *__wrapper_module_alloc(unsigned long size)
93 {
94         return module_alloc(size);
95 }
96
97 static void *__wrapper_module_free(void *module_region)
98 {
99         return module_free(NULL, module_region);
100 }
101
102 static void *sm_alloc(struct slot_manager *sm)
103 {
104         return __wrapper_module_alloc(PAGE_SIZE);
105 }
106
107 static void sm_free(struct slot_manager *sm, void *ptr)
108 {
109         __wrapper_module_free(ptr);
110 }
111
112 static void init_sm()
113 {
114         sm.slot_size = KPROBES_TRAMP_LEN;
115         sm.alloc = sm_alloc;
116         sm.free = sm_free;
117         INIT_HLIST_NODE(&sm.page_list);
118 }
119
120 static void exit_sm()
121 {
122         /* FIXME: free */
123 }
124
125 void kretprobe_assert(struct kretprobe_instance *ri, unsigned long orig_ret_address, unsigned long trampoline_address)
126 {
127         if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
128                 struct task_struct *task;
129                 if (ri == NULL) {
130                         panic("kretprobe BUG!: ri = NULL\n");
131                 }
132
133                 task = ri->task;
134
135                 if (task == NULL) {
136                         panic("kretprobe BUG!: task = NULL\n");
137                 }
138
139                 if (ri->rp == NULL) {
140                         panic("kretprobe BUG!: ri->rp = NULL\n");
141                 }
142
143                 panic("kretprobe BUG!: Processing kretprobe %p @ %p (%d/%d - %s)\n",
144                       ri->rp, ri->rp->kp.addr, ri->task->tgid, ri->task->pid, ri->task->comm);
145         }
146 }
147
148 /* We have preemption disabled.. so it is safe to use __ versions */
149 static inline void set_kprobe_instance(struct kprobe *kp)
150 {
151         __get_cpu_var(kprobe_instance) = kp;
152 }
153
154 static inline void reset_kprobe_instance(void)
155 {
156         __get_cpu_var(kprobe_instance) = NULL;
157 }
158
159 /* kprobe_running() will just return the current_kprobe on this CPU */
160 struct kprobe *kprobe_running(void)
161 {
162         return __get_cpu_var(current_kprobe);
163 }
164
165 void reset_current_kprobe(void)
166 {
167         __get_cpu_var(current_kprobe) = NULL;
168 }
169
170 struct kprobe_ctlblk *get_kprobe_ctlblk(void)
171 {
172         return &__get_cpu_var(kprobe_ctlblk);
173 }
174
175 /*
176  * This routine is called either:
177  *      - under the kprobe_mutex - during kprobe_[un]register()
178  *                              OR
179  *      - with preemption disabled - from arch/xxx/kernel/kprobes.c
180  */
181 struct kprobe *get_kprobe(void *addr)
182 {
183         struct hlist_head *head;
184         struct hlist_node *node;
185         struct kprobe *p;
186
187         head = &kprobe_table[hash_ptr (addr, KPROBE_HASH_BITS)];
188         swap_hlist_for_each_entry_rcu(p, node, head, hlist) {
189                 if (p->addr == addr) {
190                         return p;
191                 }
192         }
193
194         return NULL;
195 }
196
197 /*
198  * Aggregate handlers for multiple kprobes support - these handlers
199  * take care of invoking the individual kprobe handlers on p->list
200  */
201 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
202 {
203         struct kprobe *kp;
204         int ret;
205
206         list_for_each_entry_rcu(kp, &p->list, list) {
207                 if (kp->pre_handler) {
208                         set_kprobe_instance(kp);
209                         ret = kp->pre_handler(kp, regs);
210                         if (ret)
211                                 return ret;
212                 }
213                 reset_kprobe_instance();
214         }
215
216         return 0;
217 }
218
219 static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, unsigned long flags)
220 {
221         struct kprobe *kp;
222
223         list_for_each_entry_rcu(kp, &p->list, list) {
224                 if (kp->post_handler) {
225                         set_kprobe_instance(kp);
226                         kp->post_handler(kp, regs, flags);
227                         reset_kprobe_instance();
228                 }
229         }
230 }
231
232 static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr)
233 {
234         struct kprobe *cur = __get_cpu_var(kprobe_instance);
235
236         /*
237          * if we faulted "during" the execution of a user specified
238          * probe handler, invoke just that probe's fault handler
239          */
240         if (cur && cur->fault_handler) {
241                 if (cur->fault_handler(cur, regs, trapnr))
242                         return 1;
243         }
244
245         return 0;
246 }
247
248 static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
249 {
250         struct kprobe *cur = __get_cpu_var(kprobe_instance);
251         int ret = 0;
252         DBPRINTF ("cur = 0x%p\n", cur);
253         if (cur)
254                 DBPRINTF ("cur = 0x%p cur->break_handler = 0x%p\n", cur, cur->break_handler);
255
256         if (cur && cur->break_handler) {
257                 if (cur->break_handler(cur, regs))
258                         ret = 1;
259         }
260         reset_kprobe_instance();
261
262         return ret;
263 }
264
265 /* Walks the list and increments nmissed count for multiprobe case */
266 void kprobes_inc_nmissed_count(struct kprobe *p)
267 {
268         struct kprobe *kp;
269         if (p->pre_handler != aggr_pre_handler) {
270                 p->nmissed++;
271         } else {
272                 list_for_each_entry_rcu(kp, &p->list, list) {
273                         ++kp->nmissed;
274                 }
275         }
276 }
277
278 /* Called with kretprobe_lock held */
279 struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
280 {
281         struct hlist_node *node;
282         struct kretprobe_instance *ri;
283
284         swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
285                 return ri;
286         }
287
288         if (!alloc_nodes_kretprobe(rp)) {
289                 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
290                         return ri;
291                 }
292         }
293
294         return NULL;
295 }
296 EXPORT_SYMBOL_GPL(get_free_rp_inst);
297
298 /* Called with kretprobe_lock held */
299 struct kretprobe_instance *get_free_rp_inst_no_alloc(struct kretprobe *rp)
300 {
301         struct hlist_node *node;
302         struct kretprobe_instance *ri;
303
304         swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
305                 return ri;
306         }
307
308         return NULL;
309 }
310
311 /* Called with kretprobe_lock held */
312 struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
313 {
314         struct hlist_node *node;
315         struct kretprobe_instance *ri;
316
317         swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
318                 return ri;
319         }
320
321         return NULL;
322 }
323 EXPORT_SYMBOL_GPL(get_used_rp_inst);
324
325 /* Called with kretprobe_lock held */
326 void add_rp_inst (struct kretprobe_instance *ri)
327 {
328         /*
329          * Remove rp inst off the free list -
330          * Add it back when probed function returns
331          */
332         hlist_del(&ri->uflist);
333
334         /* Add rp inst onto table */
335         INIT_HLIST_NODE(&ri->hlist);
336
337         hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
338
339         /* Also add this rp inst to the used list. */
340         INIT_HLIST_NODE(&ri->uflist);
341         hlist_add_head(&ri->uflist, &ri->rp->used_instances);
342 }
343 EXPORT_SYMBOL_GPL(add_rp_inst);
344
345 /* Called with kretprobe_lock held */
346 void recycle_rp_inst(struct kretprobe_instance *ri)
347 {
348         if (ri->rp) {
349                 hlist_del(&ri->hlist);
350                 /* remove rp inst off the used list */
351                 hlist_del(&ri->uflist);
352                 /* put rp inst back onto the free list */
353                 INIT_HLIST_NODE(&ri->uflist);
354                 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
355         }
356 }
357 EXPORT_SYMBOL_GPL(recycle_rp_inst);
358
359 struct hlist_head *kretprobe_inst_table_head(void *hash_key)
360 {
361         return &kretprobe_inst_table[hash_ptr(hash_key, KPROBE_HASH_BITS)];
362 }
363 EXPORT_SYMBOL_GPL(kretprobe_inst_table_head);
364
365 void free_rp_inst(struct kretprobe *rp)
366 {
367         struct kretprobe_instance *ri;
368         while ((ri = get_free_rp_inst_no_alloc(rp)) != NULL) {
369                 hlist_del(&ri->uflist);
370                 kfree(ri);
371         }
372 }
373 EXPORT_SYMBOL_GPL(free_rp_inst);
374
375 /*
376  * Keep all fields in the kprobe consistent
377  */
378 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
379 {
380         memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
381         memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
382         p->ss_addr = old_p->ss_addr;
383 #ifdef CONFIG_ARM
384         p->safe_arm = old_p->safe_arm;
385         p->safe_thumb = old_p->safe_thumb;
386 #endif
387 }
388
389 /*
390  * Add the new probe to old_p->list. Fail if this is the
391  * second jprobe at the address - two jprobes can't coexist
392  */
393 static int add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
394 {
395         if (p->break_handler) {
396                 if (old_p->break_handler) {
397                         return -EEXIST;
398                 }
399
400                 list_add_tail_rcu(&p->list, &old_p->list);
401                 old_p->break_handler = aggr_break_handler;
402         } else {
403                 list_add_rcu(&p->list, &old_p->list);
404         }
405
406         if (p->post_handler && !old_p->post_handler) {
407                 old_p->post_handler = aggr_post_handler;
408         }
409
410         return 0;
411 }
412
413 /**
414  * hlist_replace_rcu - replace old entry by new one
415  * @old : the element to be replaced
416  * @new : the new element to insert
417  *
418  * The @old entry will be replaced with the @new entry atomically.
419  */
420 inline void dbi_hlist_replace_rcu(struct hlist_node *old, struct hlist_node *new)
421 {
422         struct hlist_node *next = old->next;
423
424         new->next = next;
425         new->pprev = old->pprev;
426         smp_wmb();
427         if (next)
428                 new->next->pprev = &new->next;
429         if (new->pprev)
430                 *new->pprev = new;
431         old->pprev = LIST_POISON2;
432 }
433
434 /*
435  * Fill in the required fields of the "manager kprobe". Replace the
436  * earlier kprobe in the hlist with the manager kprobe
437  */
438 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
439 {
440         copy_kprobe(p, ap);
441         ap->addr = p->addr;
442         ap->pre_handler = aggr_pre_handler;
443         ap->fault_handler = aggr_fault_handler;
444         if (p->post_handler)
445                 ap->post_handler = aggr_post_handler;
446         if (p->break_handler)
447                 ap->break_handler = aggr_break_handler;
448
449         INIT_LIST_HEAD(&ap->list);
450         list_add_rcu(&p->list, &ap->list);
451
452         dbi_hlist_replace_rcu(&p->hlist, &ap->hlist);
453 }
454
455 /*
456  * This is the second or subsequent kprobe at the address - handle
457  * the intricacies
458  */
459 int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
460 {
461         int ret = 0;
462         struct kprobe *ap;
463         DBPRINTF ("start\n");
464
465         DBPRINTF ("p = %p old_p = %p \n", p, old_p);
466         if (old_p->pre_handler == aggr_pre_handler) {
467                 DBPRINTF ("aggr_pre_handler \n");
468
469                 copy_kprobe(old_p, p);
470                 ret = add_new_kprobe(old_p, p);
471         } else {
472                 DBPRINTF ("kzalloc\n");
473 #ifdef kzalloc
474                 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
475 #else
476                 ap = kmalloc(sizeof(struct kprobe), GFP_KERNEL);
477                 if (ap)
478                         memset(ap, 0, sizeof(struct kprobe));
479 #endif
480                 if (!ap)
481                         return -ENOMEM;
482                 add_aggr_kprobe(ap, old_p);
483                 copy_kprobe(ap, p);
484                 DBPRINTF ("ap = %p p = %p old_p = %p \n", ap, p, old_p);
485                 ret = add_new_kprobe(ap, p);
486         }
487
488         return ret;
489 }
490 EXPORT_SYMBOL_GPL(register_aggr_kprobe);
491
492 static void remove_kprobe(struct kprobe *p)
493 {
494         /* TODO: check boostable for x86 and MIPS */
495         free_insn_slot(&sm, p->ainsn.insn);
496 }
497
498 int dbi_register_kprobe(struct kprobe *p)
499 {
500         struct kprobe *old_p;
501         int ret = 0;
502         /*
503          * If we have a symbol_name argument look it up,
504          * and add it to the address.  That way the addr
505          * field can either be global or relative to a symbol.
506          */
507         if (p->symbol_name) {
508                 if (p->addr)
509                         return -EINVAL;
510                 p->addr = (kprobe_opcode_t *)swap_ksyms(p->symbol_name);
511         }
512
513         if (!p->addr)
514                 return -EINVAL;
515         DBPRINTF ("p->addr = 0x%p\n", p->addr);
516         p->addr = (kprobe_opcode_t *)(((char *)p->addr) + p->offset);
517         DBPRINTF ("p->addr = 0x%p p = 0x%p\n", p->addr, p);
518
519 #ifdef KPROBES_PROFILE
520         p->start_tm.tv_sec = p->start_tm.tv_usec = 0;
521         p->hnd_tm_sum.tv_sec = p->hnd_tm_sum.tv_usec = 0;
522         p->count = 0;
523 #endif
524         p->mod_refcounted = 0;
525         p->nmissed = 0;
526
527         old_p = get_kprobe(p->addr);
528         if (old_p) {
529                 ret = register_aggr_kprobe(old_p, p);
530                 if (!ret)
531                         atomic_inc(&kprobe_count);
532                 goto out;
533         }
534
535         if ((ret = arch_prepare_kprobe(p, &sm)) != 0)
536                 goto out;
537
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)]);
541         arch_arm_kprobe(p);
542
543 out:
544         DBPRINTF ("out ret = 0x%x\n", ret);
545         return ret;
546 }
547
548 void dbi_unregister_kprobe(struct kprobe *p)
549 {
550         struct kprobe *old_p, *list_p;
551         int cleanup_p;
552
553         old_p = get_kprobe(p->addr);
554         DBPRINTF ("dbi_unregister_kprobe p=%p old_p=%p", p, old_p);
555         if (unlikely (!old_p))
556                 return;
557
558         if (p != old_p) {
559                 list_for_each_entry_rcu(list_p, &old_p->list, list)
560                         if (list_p == p)
561                                 /* kprobe p is a valid probe */
562                                 goto valid_p;
563                 return;
564         }
565
566 valid_p:
567         DBPRINTF ("dbi_unregister_kprobe valid_p");
568         if ((old_p == p) || ((old_p->pre_handler == aggr_pre_handler) &&
569             (p->list.next == &old_p->list) && (p->list.prev == &old_p->list))) {
570                 /* Only probe on the hash list */
571                 arch_disarm_kprobe(p);
572                 hlist_del_rcu(&old_p->hlist);
573                 cleanup_p = 1;
574         } else {
575                 list_del_rcu(&p->list);
576                 cleanup_p = 0;
577         }
578         DBPRINTF ("dbi_unregister_kprobe cleanup_p=%d", cleanup_p);
579
580         if (cleanup_p) {
581                 if (p != old_p) {
582                         list_del_rcu(&p->list);
583                         kfree(old_p);
584                 }
585
586                 if (!in_atomic()) {
587                         synchronize_sched();
588                 }
589
590                 remove_kprobe(p);
591         } else {
592                 if (p->break_handler)
593                         old_p->break_handler = NULL;
594                 if (p->post_handler) {
595                         list_for_each_entry_rcu(list_p, &old_p->list, list) {
596                                 if (list_p->post_handler) {
597                                         cleanup_p = 2;
598                                         break;
599                                 }
600                         }
601
602                         if (cleanup_p == 0)
603                                 old_p->post_handler = NULL;
604                 }
605         }
606 }
607
608 int dbi_register_jprobe(struct jprobe *jp)
609 {
610         /* Todo: Verify probepoint is a function entry point */
611         jp->kp.pre_handler = setjmp_pre_handler;
612         jp->kp.break_handler = longjmp_break_handler;
613
614         return dbi_register_kprobe(&jp->kp);
615 }
616
617 void dbi_unregister_jprobe(struct jprobe *jp)
618 {
619         dbi_unregister_kprobe(&jp->kp);
620 }
621
622 /*
623  * This kprobe pre_handler is registered with every kretprobe. When probe
624  * hits it will set up the return probe.
625  */
626 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
627 {
628         struct kretprobe *rp = container_of(p, struct kretprobe, kp);
629         struct kretprobe_instance *ri;
630         unsigned long flags = 0;
631
632         /* TODO: consider to only swap the RA after the last pre_handler fired */
633         spin_lock_irqsave(&kretprobe_lock, flags);
634
635         /* TODO: test - remove retprobe after func entry but before its exit */
636         if ((ri = get_free_rp_inst(rp)) != NULL) {
637                 ri->rp = rp;
638                 ri->task = current;
639
640                 if (rp->entry_handler) {
641                         rp->entry_handler(ri, regs);
642                 }
643
644                 arch_prepare_kretprobe(ri, regs);
645
646                 add_rp_inst(ri);
647         } else {
648                 ++rp->nmissed;
649         }
650
651         spin_unlock_irqrestore(&kretprobe_lock, flags);
652
653         return 0;
654 }
655
656 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
657 {
658         struct kretprobe_instance *ri = NULL;
659         struct hlist_head *head;
660         struct hlist_node *node, *tmp;
661         unsigned long flags, orig_ret_address = 0;
662         unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
663
664         struct kretprobe *crp = NULL;
665         struct kprobe_ctlblk *kcb;
666
667         preempt_disable();
668         kcb = get_kprobe_ctlblk();
669
670         spin_lock_irqsave(&kretprobe_lock, flags);
671
672         /*
673          * We are using different hash keys (current and mm) for finding kernel
674          * space and user space probes.  Kernel space probes can change mm field in
675          * task_struct.  User space probes can be shared between threads of one
676          * process so they have different current but same mm.
677          */
678         head = kretprobe_inst_table_head(current);
679
680 #ifdef CONFIG_X86
681         regs->XREG(cs) = __KERNEL_CS | get_kernel_rpl();
682         regs->EREG(ip) = trampoline_address;
683         regs->ORIG_EAX_REG = 0xffffffff;
684 #endif
685
686         /*
687          * It is possible to have multiple instances associated with a given
688          * task either because an multiple functions in the call path
689          * have a return probe installed on them, and/or more then one
690          * return probe was registered for a target function.
691          *
692          * We can handle this because:
693          *     - instances are always inserted at the head of the list
694          *     - when multiple return probes are registered for the same
695          *       function, the first instance's ret_addr will point to the
696          *       real return address, and all the rest will point to
697          *       kretprobe_trampoline
698          */
699         swap_hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
700                 if (ri->task != current)
701                         /* another task is sharing our hash bucket */
702                         continue;
703                 if (ri->rp && ri->rp->handler) {
704                         __get_cpu_var(current_kprobe) = &ri->rp->kp;
705                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
706                         ri->rp->handler(ri, regs);
707                         __get_cpu_var(current_kprobe) = NULL;
708                 }
709
710                 orig_ret_address = (unsigned long)ri->ret_addr;
711                 recycle_rp_inst(ri);
712                 if (orig_ret_address != trampoline_address)
713                         /*
714                          * This is the real return address. Any other
715                          * instances associated with this task are for
716                          * other calls deeper on the call stack
717                          */
718                         break;
719         }
720         kretprobe_assert(ri, orig_ret_address, trampoline_address);
721
722         if (kcb->kprobe_status == KPROBE_REENTER) {
723                 restore_previous_kprobe(kcb);
724         } else {
725                 reset_current_kprobe();
726         }
727
728         spin_unlock_irqrestore(&kretprobe_lock, flags);
729         preempt_enable_no_resched();
730
731         /*
732          * By returning a non-zero value, we are telling
733          * kprobe_handler() that we don't want the post_handler
734          * to run (and have re-enabled preemption)
735          */
736
737         return (int)orig_ret_address;
738 }
739
740 #define SCHED_RP_NR 200
741 #define COMMON_RP_NR 10
742
743 int alloc_nodes_kretprobe(struct kretprobe *rp)
744 {
745         int alloc_nodes;
746         struct kretprobe_instance *inst;
747         int i;
748
749         DBPRINTF("Alloc aditional mem for retprobes");
750
751         if ((unsigned long)rp->kp.addr == sched_addr) {
752                 rp->maxactive += SCHED_RP_NR;//max (100, 2 * NR_CPUS);
753                 alloc_nodes = SCHED_RP_NR;
754         } else {
755 #if 1//def CONFIG_PREEMPT
756                 rp->maxactive += max (COMMON_RP_NR, 2 * NR_CPUS);
757 #else
758                 rp->maxacpptive += NR_CPUS;
759 #endif
760                 alloc_nodes = COMMON_RP_NR;
761         }
762
763         for (i = 0; i < alloc_nodes; i++) {
764                 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_ATOMIC);
765                 if (inst == NULL) {
766                         free_rp_inst(rp);
767                         return -ENOMEM;
768                 }
769                 INIT_HLIST_NODE(&inst->uflist);
770                 hlist_add_head(&inst->uflist, &rp->free_instances);
771         }
772
773         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)));
774         return 0;
775 }
776
777 int dbi_register_kretprobe(struct kretprobe *rp)
778 {
779         int ret = 0;
780         struct kretprobe_instance *inst;
781         int i;
782         DBPRINTF ("START");
783
784         rp->kp.pre_handler = pre_handler_kretprobe;
785         rp->kp.post_handler = NULL;
786         rp->kp.fault_handler = NULL;
787         rp->kp.break_handler = NULL;
788
789         /* Pre-allocate memory for max kretprobe instances */
790         if ((unsigned long)rp->kp.addr == exit_addr) {
791                 rp->kp.pre_handler = NULL; //not needed for do_exit
792                 rp->maxactive = 0;
793         } else if ((unsigned long)rp->kp.addr == do_group_exit_addr) {
794                 rp->kp.pre_handler = NULL;
795                 rp->maxactive = 0;
796         } else if ((unsigned long)rp->kp.addr == sys_exit_group_addr) {
797                 rp->kp.pre_handler = NULL;
798                 rp->maxactive = 0;
799         } else if ((unsigned long)rp->kp.addr == sys_exit_addr) {
800                 rp->kp.pre_handler = NULL;
801                 rp->maxactive = 0;
802         } else if (rp->maxactive <= 0) {
803 #if 1//def CONFIG_PREEMPT
804                 rp->maxactive = max (COMMON_RP_NR, 2 * NR_CPUS);
805 #else
806                 rp->maxactive = NR_CPUS;
807 #endif
808         }
809         INIT_HLIST_HEAD(&rp->used_instances);
810         INIT_HLIST_HEAD(&rp->free_instances);
811         for (i = 0; i < rp->maxactive; i++) {
812                 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_KERNEL);
813                 if (inst == NULL) {
814                         free_rp_inst(rp);
815                         return -ENOMEM;
816                 }
817                 INIT_HLIST_NODE(&inst->uflist);
818                 hlist_add_head(&inst->uflist, &rp->free_instances);
819         }
820
821         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)));
822         rp->nmissed = 0;
823         /* Establish function entry probe point */
824         if ((ret = dbi_register_kprobe(&rp->kp)) != 0)
825                 free_rp_inst(rp);
826
827         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)));
828
829         return ret;
830 }
831
832 static int dbi_disarm_krp_inst(struct kretprobe_instance *ri);
833
834 static void dbi_unregister_kretprobe_top(struct kretprobe *rp)
835 {
836         unsigned long flags;
837         struct kretprobe_instance *ri;
838         struct hlist_node *node;
839
840         dbi_unregister_kprobe(&rp->kp);
841
842         /* No race here */
843         spin_lock_irqsave(&kretprobe_lock, flags);
844
845         swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
846                 if (!dbi_disarm_krp_inst(ri)) {
847                         printk("%s (%d/%d): cannot disarm krp instance (%08lx)\n",
848                                         ri->task->comm, ri->task->tgid, ri->task->pid,
849                                         (unsigned long)rp->kp.addr);
850                 }
851         }
852         spin_unlock_irqrestore(&kretprobe_lock, flags);
853 }
854
855 static void dbi_unregister_kretprobe_bottom(struct kretprobe *rp)
856 {
857         unsigned long flags;
858         struct kretprobe_instance *ri;
859
860         spin_lock_irqsave(&kretprobe_lock, flags);
861         while ((ri = get_used_rp_inst(rp)) != NULL) {
862                 recycle_rp_inst(ri);
863         }
864         spin_unlock_irqrestore(&kretprobe_lock, flags);
865         free_rp_inst(rp);
866 }
867
868 void dbi_unregister_kretprobe(struct kretprobe *rp)
869 {
870         dbi_unregister_kretprobe_top(rp);
871         dbi_unregister_kretprobe_bottom(rp);
872 }
873
874 struct kretprobe *clone_kretprobe(struct kretprobe *rp)
875 {
876         struct kprobe *old_p;
877         struct kretprobe *clone = NULL;
878         int ret;
879
880         clone = kmalloc(sizeof(struct kretprobe), GFP_KERNEL);
881         if (!clone) {
882                 DBPRINTF ("failed to alloc memory for clone probe %p!", rp->kp.addr);
883                 return NULL;
884         }
885         memcpy(clone, rp, sizeof(struct kretprobe));
886         clone->kp.pre_handler = pre_handler_kretprobe;
887         clone->kp.post_handler = NULL;
888         clone->kp.fault_handler = NULL;
889         clone->kp.break_handler = NULL;
890         old_p = get_kprobe(rp->kp.addr);
891         if (old_p) {
892                 ret = register_aggr_kprobe(old_p, &clone->kp);
893                 if (ret) {
894                         kfree(clone);
895                         return NULL;
896                 }
897                 atomic_inc(&kprobe_count);
898         }
899
900         return clone;
901 }
902 EXPORT_SYMBOL_GPL(clone_kretprobe);
903
904 static void inline rm_task_trampoline(struct task_struct *p, struct kretprobe_instance *ri)
905 {
906         arch_set_task_pc(p, (unsigned long)ri->ret_addr);
907 }
908
909 static int dbi_disarm_krp_inst(struct kretprobe_instance *ri)
910 {
911         unsigned long *tramp = (unsigned long *)&kretprobe_trampoline;
912         unsigned long *sp = ri->sp;
913         unsigned long *found = NULL;
914         int retval = -ENOENT;
915
916         if (!sp) {
917                 unsigned long pc = arch_get_task_pc(ri->task);
918
919                 printk("---> [%d] %s (%d/%d): pc = %08lx, ra = %08lx, tramp= %08lx (%08lx)\n",
920                        task_cpu(ri->task),
921                        ri->task->comm, ri->task->tgid, ri->task->pid,
922                        pc, (long unsigned int)ri->ret_addr,
923                        (long unsigned int)tramp,
924                        (long unsigned int)(ri->rp ? ri->rp->kp.addr: NULL));
925
926                 /* __switch_to retprobe handling */
927                 if (pc == (unsigned long)tramp) {
928                         rm_task_trampoline(ri->task, ri);
929                         return 0;
930                 }
931
932                 return -EINVAL;
933         }
934
935         while (sp > ri->sp - RETPROBE_STACK_DEPTH) {
936                 if (*sp == (unsigned long)tramp) {
937                         found = sp;
938                         break;
939                 }
940                 sp--;
941         }
942
943         if (found) {
944                 printk("---> [%d] %s (%d/%d): tramp (%08lx) found at %08lx (%08lx /%+d) - %p\n",
945                        task_cpu(ri->task),
946                        ri->task->comm, ri->task->tgid, ri->task->pid,
947                        (long unsigned int)tramp,
948                        (long unsigned int)found, (long unsigned int)ri->sp,
949                        found - ri->sp, ri->rp ? ri->rp->kp.addr: NULL);
950                 *found = (unsigned long)ri->ret_addr;
951                 retval = 0;
952         } else {
953                 printk("---> [%d] %s (%d/%d): tramp (%08lx) NOT found at sp = %08lx - %p\n",
954                                 task_cpu(ri->task),
955                                 ri->task->comm, ri->task->tgid, ri->task->pid,
956                                 (long unsigned int)tramp,
957                                 (long unsigned int)ri->sp, ri->rp ? ri->rp->kp.addr: NULL);
958         }
959
960         return retval;
961 }
962
963 static int init_module_deps(void)
964 {
965         int ret;
966
967         sched_addr = swap_ksyms("__switch_to");
968         exit_addr = swap_ksyms("do_exit");
969         sys_exit_group_addr = swap_ksyms("sys_exit_group");
970         do_group_exit_addr = swap_ksyms("do_group_exit");
971         sys_exit_addr = swap_ksyms("sys_exit");
972
973         if (sched_addr == 0 ||
974             exit_addr == 0 ||
975             sys_exit_group_addr == 0 ||
976             do_group_exit_addr == 0 ||
977             sys_exit_addr == 0) {
978                 return -ESRCH;
979         }
980
981         ret = init_module_dependencies();
982         if (ret) {
983                 return ret;
984         }
985
986         return arch_init_module_deps();
987 }
988
989 static int __init init_kprobes(void)
990 {
991         int i, err = 0;
992
993         module_alloc = (void *)swap_ksyms("module_alloc");
994         if (!module_alloc) {
995                 printk("module_alloc is not found! Oops.\n");
996                 return -1;
997         }
998         module_free = (void *)swap_ksyms("module_free");
999         if (!module_alloc) {
1000                 printk("module_free is not found! Oops.\n");
1001                 return -1;
1002         }
1003
1004         init_sm();
1005
1006         /* FIXME allocate the probe table, currently defined statically */
1007         /* initialize all list heads */
1008         for (i = 0; i < KPROBE_TABLE_SIZE; ++i) {
1009                 INIT_HLIST_HEAD(&kprobe_table[i]);
1010                 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
1011         }
1012         atomic_set(&kprobe_count, 0);
1013
1014         err = init_module_deps();
1015         if (err) {
1016                 return err;
1017         }
1018
1019         err = arch_init_kprobes();
1020
1021         DBPRINTF ("init_kprobes: arch_init_kprobes - %d", err);
1022
1023         return err;
1024 }
1025
1026 static void __exit exit_kprobes(void)
1027 {
1028         arch_exit_kprobes();
1029         exit_sm();
1030 }
1031
1032 module_init(init_kprobes);
1033 module_exit(exit_kprobes);
1034
1035 EXPORT_SYMBOL_GPL(dbi_register_kprobe);
1036 EXPORT_SYMBOL_GPL(dbi_unregister_kprobe);
1037 EXPORT_SYMBOL_GPL(dbi_register_jprobe);
1038 EXPORT_SYMBOL_GPL(dbi_unregister_jprobe);
1039 EXPORT_SYMBOL_GPL(dbi_jprobe_return);
1040 EXPORT_SYMBOL_GPL(dbi_register_kretprobe);
1041 EXPORT_SYMBOL_GPL(dbi_unregister_kretprobe);
1042
1043 MODULE_LICENSE("Dual BSD/GPL");