Merge "[REFACTOR] Buffer: move getting next queue element into separate function"
[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 <kprobe/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/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 static DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
79 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
80
81 struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
82 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
83
84 atomic_t kprobe_count;
85 EXPORT_SYMBOL_GPL(kprobe_count);
86
87
88 static void *(*module_alloc)(unsigned long size) = NULL;
89 static void *(*module_free)(struct module *mod, void *module_region) = NULL;
90
91 static void *__wrapper_module_alloc(unsigned long size)
92 {
93         return module_alloc(size);
94 }
95
96 static void *__wrapper_module_free(void *module_region)
97 {
98         return module_free(NULL, module_region);
99 }
100
101 static void *sm_alloc(struct slot_manager *sm)
102 {
103         return __wrapper_module_alloc(PAGE_SIZE);
104 }
105
106 static void sm_free(struct slot_manager *sm, void *ptr)
107 {
108         __wrapper_module_free(ptr);
109 }
110
111 static void init_sm(void)
112 {
113         sm.slot_size = KPROBES_TRAMP_LEN;
114         sm.alloc = sm_alloc;
115         sm.free = sm_free;
116         INIT_HLIST_HEAD(&sm.page_list);
117 }
118
119 static void exit_sm(void)
120 {
121         /* FIXME: free */
122 }
123
124 void kretprobe_assert(struct kretprobe_instance *ri, unsigned long orig_ret_address, unsigned long trampoline_address)
125 {
126         if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
127                 struct task_struct *task;
128                 if (ri == NULL) {
129                         panic("kretprobe BUG!: ri = NULL\n");
130                 }
131
132                 task = ri->task;
133
134                 if (task == NULL) {
135                         panic("kretprobe BUG!: task = NULL\n");
136                 }
137
138                 if (ri->rp == NULL) {
139                         panic("kretprobe BUG!: ri->rp = NULL\n");
140                 }
141
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);
144         }
145 }
146
147 /* We have preemption disabled.. so it is safe to use __ versions */
148 static inline void set_kprobe_instance(struct kprobe *kp)
149 {
150         __get_cpu_var(kprobe_instance) = kp;
151 }
152
153 static inline void reset_kprobe_instance(void)
154 {
155         __get_cpu_var(kprobe_instance) = NULL;
156 }
157
158 /* kprobe_running() will just return the current_kprobe on this CPU */
159 struct kprobe *kprobe_running(void)
160 {
161         return __get_cpu_var(current_kprobe);
162 }
163
164 void reset_current_kprobe(void)
165 {
166         __get_cpu_var(current_kprobe) = NULL;
167 }
168
169 struct kprobe_ctlblk *get_kprobe_ctlblk(void)
170 {
171         return &__get_cpu_var(kprobe_ctlblk);
172 }
173
174 /*
175  * This routine is called either:
176  *      - under the kprobe_mutex - during kprobe_[un]register()
177  *                              OR
178  *      - with preemption disabled - from arch/xxx/kernel/kprobes.c
179  */
180 struct kprobe *get_kprobe(void *addr)
181 {
182         struct hlist_head *head;
183         struct kprobe *p;
184         DECLARE_NODE_PTR_FOR_HLIST(node);
185
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) {
189                         return p;
190                 }
191         }
192
193         return NULL;
194 }
195
196 /*
197  * Aggregate handlers for multiple kprobes support - these handlers
198  * take care of invoking the individual kprobe handlers on p->list
199  */
200 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
201 {
202         struct kprobe *kp;
203         int ret;
204
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);
209                         if (ret)
210                                 return ret;
211                 }
212                 reset_kprobe_instance();
213         }
214
215         return 0;
216 }
217
218 static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, unsigned long flags)
219 {
220         struct kprobe *kp;
221
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();
227                 }
228         }
229 }
230
231 static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr)
232 {
233         struct kprobe *cur = __get_cpu_var(kprobe_instance);
234
235         /*
236          * if we faulted "during" the execution of a user specified
237          * probe handler, invoke just that probe's fault handler
238          */
239         if (cur && cur->fault_handler) {
240                 if (cur->fault_handler(cur, regs, trapnr))
241                         return 1;
242         }
243
244         return 0;
245 }
246
247 static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
248 {
249         struct kprobe *cur = __get_cpu_var(kprobe_instance);
250         int ret = 0;
251         DBPRINTF ("cur = 0x%p\n", cur);
252         if (cur)
253                 DBPRINTF ("cur = 0x%p cur->break_handler = 0x%p\n", cur, cur->break_handler);
254
255         if (cur && cur->break_handler) {
256                 if (cur->break_handler(cur, regs))
257                         ret = 1;
258         }
259         reset_kprobe_instance();
260
261         return ret;
262 }
263
264 /* Walks the list and increments nmissed count for multiprobe case */
265 void kprobes_inc_nmissed_count(struct kprobe *p)
266 {
267         struct kprobe *kp;
268         if (p->pre_handler != aggr_pre_handler) {
269                 p->nmissed++;
270         } else {
271                 list_for_each_entry_rcu(kp, &p->list, list) {
272                         ++kp->nmissed;
273                 }
274         }
275 }
276
277 /* Called with kretprobe_lock held */
278 struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
279 {
280         struct kretprobe_instance *ri;
281         DECLARE_NODE_PTR_FOR_HLIST(node);
282
283         swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
284                 return ri;
285         }
286
287         if (!alloc_nodes_kretprobe(rp)) {
288                 swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
289                         return ri;
290                 }
291         }
292
293         return NULL;
294 }
295 EXPORT_SYMBOL_GPL(get_free_rp_inst);
296
297 /* Called with kretprobe_lock held */
298 struct kretprobe_instance *get_free_rp_inst_no_alloc(struct kretprobe *rp)
299 {
300         struct kretprobe_instance *ri;
301         DECLARE_NODE_PTR_FOR_HLIST(node);
302
303         swap_hlist_for_each_entry(ri, node, &rp->free_instances, uflist) {
304                 return ri;
305         }
306
307         return NULL;
308 }
309
310 /* Called with kretprobe_lock held */
311 struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
312 {
313         struct kretprobe_instance *ri;
314         DECLARE_NODE_PTR_FOR_HLIST(node);
315
316         swap_hlist_for_each_entry(ri, node, &rp->used_instances, uflist) {
317                 return ri;
318         }
319
320         return NULL;
321 }
322 EXPORT_SYMBOL_GPL(get_used_rp_inst);
323
324 /* Called with kretprobe_lock held */
325 void add_rp_inst (struct kretprobe_instance *ri)
326 {
327         /*
328          * Remove rp inst off the free list -
329          * Add it back when probed function returns
330          */
331         hlist_del(&ri->uflist);
332
333         /* Add rp inst onto table */
334         INIT_HLIST_NODE(&ri->hlist);
335
336         hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
337
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);
341 }
342 EXPORT_SYMBOL_GPL(add_rp_inst);
343
344 /* Called with kretprobe_lock held */
345 void recycle_rp_inst(struct kretprobe_instance *ri)
346 {
347         if (ri->rp) {
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);
354         }
355 }
356 EXPORT_SYMBOL_GPL(recycle_rp_inst);
357
358 struct hlist_head *kretprobe_inst_table_head(void *hash_key)
359 {
360         return &kretprobe_inst_table[hash_ptr(hash_key, KPROBE_HASH_BITS)];
361 }
362 EXPORT_SYMBOL_GPL(kretprobe_inst_table_head);
363
364 void free_rp_inst(struct kretprobe *rp)
365 {
366         struct kretprobe_instance *ri;
367         while ((ri = get_free_rp_inst_no_alloc(rp)) != NULL) {
368                 hlist_del(&ri->uflist);
369                 kfree(ri);
370         }
371 }
372 EXPORT_SYMBOL_GPL(free_rp_inst);
373
374 /*
375  * Keep all fields in the kprobe consistent
376  */
377 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
378 {
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;
382 #ifdef CONFIG_ARM
383         p->safe_arm = old_p->safe_arm;
384         p->safe_thumb = old_p->safe_thumb;
385 #endif
386 }
387
388 /*
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
391  */
392 static int add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
393 {
394         if (p->break_handler) {
395                 if (old_p->break_handler) {
396                         return -EEXIST;
397                 }
398
399                 list_add_tail_rcu(&p->list, &old_p->list);
400                 old_p->break_handler = aggr_break_handler;
401         } else {
402                 list_add_rcu(&p->list, &old_p->list);
403         }
404
405         if (p->post_handler && !old_p->post_handler) {
406                 old_p->post_handler = aggr_post_handler;
407         }
408
409         return 0;
410 }
411
412 /**
413  * hlist_replace_rcu - replace old entry by new one
414  * @old : the element to be replaced
415  * @new : the new element to insert
416  *
417  * The @old entry will be replaced with the @new entry atomically.
418  */
419 inline void dbi_hlist_replace_rcu(struct hlist_node *old, struct hlist_node *new)
420 {
421         struct hlist_node *next = old->next;
422
423         new->next = next;
424         new->pprev = old->pprev;
425         smp_wmb();
426         if (next)
427                 new->next->pprev = &new->next;
428         if (new->pprev)
429                 *new->pprev = new;
430         old->pprev = LIST_POISON2;
431 }
432
433 /*
434  * Fill in the required fields of the "manager kprobe". Replace the
435  * earlier kprobe in the hlist with the manager kprobe
436  */
437 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
438 {
439         copy_kprobe(p, ap);
440         ap->addr = p->addr;
441         ap->pre_handler = aggr_pre_handler;
442         ap->fault_handler = aggr_fault_handler;
443         if (p->post_handler)
444                 ap->post_handler = aggr_post_handler;
445         if (p->break_handler)
446                 ap->break_handler = aggr_break_handler;
447
448         INIT_LIST_HEAD(&ap->list);
449         list_add_rcu(&p->list, &ap->list);
450
451         dbi_hlist_replace_rcu(&p->hlist, &ap->hlist);
452 }
453
454 /*
455  * This is the second or subsequent kprobe at the address - handle
456  * the intricacies
457  */
458 int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
459 {
460         int ret = 0;
461         struct kprobe *ap;
462         DBPRINTF ("start\n");
463
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");
467
468                 copy_kprobe(old_p, p);
469                 ret = add_new_kprobe(old_p, p);
470         } else {
471                 DBPRINTF ("kzalloc\n");
472 #ifdef kzalloc
473                 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
474 #else
475                 ap = kmalloc(sizeof(struct kprobe), GFP_KERNEL);
476                 if (ap)
477                         memset(ap, 0, sizeof(struct kprobe));
478 #endif
479                 if (!ap)
480                         return -ENOMEM;
481                 add_aggr_kprobe(ap, old_p);
482                 copy_kprobe(ap, p);
483                 DBPRINTF ("ap = %p p = %p old_p = %p \n", ap, p, old_p);
484                 ret = add_new_kprobe(ap, p);
485         }
486
487         return ret;
488 }
489 EXPORT_SYMBOL_GPL(register_aggr_kprobe);
490
491 static void remove_kprobe(struct kprobe *p)
492 {
493         /* TODO: check boostable for x86 and MIPS */
494         free_insn_slot(&sm, p->ainsn.insn);
495 }
496
497 int dbi_register_kprobe(struct kprobe *p)
498 {
499         struct kprobe *old_p;
500         int ret = 0;
501         /*
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.
505          */
506         if (p->symbol_name) {
507                 if (p->addr)
508                         return -EINVAL;
509                 p->addr = (kprobe_opcode_t *)swap_ksyms(p->symbol_name);
510         }
511
512         if (!p->addr)
513                 return -EINVAL;
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);
517
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;
521         p->count = 0;
522 #endif
523         p->mod_refcounted = 0;
524         p->nmissed = 0;
525         INIT_LIST_HEAD(&p->list);
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 static void dbi_unregister_valid_kprobe(struct kprobe *p, struct kprobe *old_p)
549 {
550         struct kprobe *list_p;
551
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);
557
558                 if (p != old_p)
559                         kfree(old_p);
560                 /* Synchronize and remove probe in bottom */
561         } else {
562                 list_del_rcu(&p->list);
563
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)
569                                         return;
570
571                         old_p->post_handler = NULL;
572                 }
573         }
574         /* Set NULL addr for reusability if symbol_name is used */
575         if (p->symbol_name)
576                 p->addr = NULL;
577 }
578
579 void dbi_unregister_kprobe(struct kprobe *kp)
580 {
581         struct kprobe *old_p, *list_p;
582
583         old_p = get_kprobe(kp->addr);
584         if (unlikely (!old_p))
585                 return;
586
587         if (kp != old_p) {
588                 list_for_each_entry_rcu(list_p, &old_p->list, list)
589                         if (list_p == kp)
590                                 /* kprobe p is a valid probe */
591                                 dbi_unregister_valid_kprobe(kp, old_p);
592                 return;
593         }
594
595         dbi_unregister_valid_kprobe(kp, old_p);
596 }
597
598 int dbi_register_jprobe(struct jprobe *jp)
599 {
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;
603
604         return dbi_register_kprobe(&jp->kp);
605 }
606
607 void dbi_unregister_jprobe(struct jprobe *jp)
608 {
609         dbi_unregister_kprobe(&jp->kp);
610 }
611
612 /*
613  * This kprobe pre_handler is registered with every kretprobe. When probe
614  * hits it will set up the return probe.
615  */
616 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
617 {
618         struct kretprobe *rp = container_of(p, struct kretprobe, kp);
619         struct kretprobe_instance *ri;
620         unsigned long flags = 0;
621
622         /* TODO: consider to only swap the RA after the last pre_handler fired */
623         spin_lock_irqsave(&kretprobe_lock, flags);
624
625         /* TODO: test - remove retprobe after func entry but before its exit */
626         if ((ri = get_free_rp_inst(rp)) != NULL) {
627                 ri->rp = rp;
628                 ri->task = current;
629
630                 if (rp->entry_handler) {
631                         rp->entry_handler(ri, regs);
632                 }
633
634                 arch_prepare_kretprobe(ri, regs);
635
636                 add_rp_inst(ri);
637         } else {
638                 ++rp->nmissed;
639         }
640
641         spin_unlock_irqrestore(&kretprobe_lock, flags);
642
643         return 0;
644 }
645
646 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
647 {
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;
652
653         struct kprobe_ctlblk *kcb;
654
655         struct hlist_node *tmp;
656         DECLARE_NODE_PTR_FOR_HLIST(node);
657
658         preempt_disable();
659         kcb = get_kprobe_ctlblk();
660
661         spin_lock_irqsave(&kretprobe_lock, flags);
662
663         /*
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.
668          */
669         head = kretprobe_inst_table_head(current);
670
671 #ifdef CONFIG_X86
672         regs->XREG(cs) = __KERNEL_CS | get_kernel_rpl();
673         regs->EREG(ip) = trampoline_address;
674         regs->ORIG_EAX_REG = 0xffffffff;
675 #endif
676
677         /*
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.
682          *
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
689          */
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 */
693                         continue;
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;
699                 }
700
701                 orig_ret_address = (unsigned long)ri->ret_addr;
702                 recycle_rp_inst(ri);
703                 if (orig_ret_address != trampoline_address)
704                         /*
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
708                          */
709                         break;
710         }
711         kretprobe_assert(ri, orig_ret_address, trampoline_address);
712
713         if (kcb->kprobe_status == KPROBE_REENTER) {
714                 restore_previous_kprobe(kcb);
715         } else {
716                 reset_current_kprobe();
717         }
718
719         spin_unlock_irqrestore(&kretprobe_lock, flags);
720         preempt_enable_no_resched();
721
722         /*
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)
726          */
727
728         return (int)orig_ret_address;
729 }
730
731 #define SCHED_RP_NR 200
732 #define COMMON_RP_NR 10
733
734 int alloc_nodes_kretprobe(struct kretprobe *rp)
735 {
736         int alloc_nodes;
737         struct kretprobe_instance *inst;
738         int i;
739
740         DBPRINTF("Alloc aditional mem for retprobes");
741
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;
745         } else {
746 #if 1//def CONFIG_PREEMPT
747                 rp->maxactive += max (COMMON_RP_NR, 2 * NR_CPUS);
748 #else
749                 rp->maxacpptive += NR_CPUS;
750 #endif
751                 alloc_nodes = COMMON_RP_NR;
752         }
753
754         for (i = 0; i < alloc_nodes; i++) {
755                 inst = kmalloc(sizeof(*inst) + rp->data_size, GFP_ATOMIC);
756                 if (inst == NULL) {
757                         free_rp_inst(rp);
758                         return -ENOMEM;
759                 }
760                 INIT_HLIST_NODE(&inst->uflist);
761                 hlist_add_head(&inst->uflist, &rp->free_instances);
762         }
763
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)));
765         return 0;
766 }
767
768 int dbi_register_kretprobe(struct kretprobe *rp)
769 {
770         int ret = 0;
771         struct kretprobe_instance *inst;
772         int i;
773         DBPRINTF ("START");
774
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;
779
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
783                 rp->maxactive = 0;
784         } else if ((unsigned long)rp->kp.addr == do_group_exit_addr) {
785                 rp->kp.pre_handler = NULL;
786                 rp->maxactive = 0;
787         } else if ((unsigned long)rp->kp.addr == sys_exit_group_addr) {
788                 rp->kp.pre_handler = NULL;
789                 rp->maxactive = 0;
790         } else if ((unsigned long)rp->kp.addr == sys_exit_addr) {
791                 rp->kp.pre_handler = NULL;
792                 rp->maxactive = 0;
793         } else if (rp->maxactive <= 0) {
794 #if 1//def CONFIG_PREEMPT
795                 rp->maxactive = max (COMMON_RP_NR, 2 * NR_CPUS);
796 #else
797                 rp->maxactive = NR_CPUS;
798 #endif
799         }
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);
804                 if (inst == NULL) {
805                         free_rp_inst(rp);
806                         return -ENOMEM;
807                 }
808                 INIT_HLIST_NODE(&inst->uflist);
809                 hlist_add_head(&inst->uflist, &rp->free_instances);
810         }
811
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)));
813         rp->nmissed = 0;
814         /* Establish function entry probe point */
815         if ((ret = dbi_register_kprobe(&rp->kp)) != 0)
816                 free_rp_inst(rp);
817
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)));
819
820         return ret;
821 }
822
823 static int dbi_disarm_krp_inst(struct kretprobe_instance *ri);
824
825 static void dbi_unregister_kretprobe_top(struct kretprobe *rp)
826 {
827         struct kretprobe_instance *ri;
828         DECLARE_NODE_PTR_FOR_HLIST(node);
829
830         dbi_unregister_kprobe(&rp->kp);
831
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);
837                 }
838         }
839 }
840
841 static void dbi_unregister_kretprobe_bottom(struct kretprobe *rp)
842 {
843         unsigned long flags;
844         struct kretprobe_instance *ri;
845
846         if (list_empty(&rp->kp.list))
847                 remove_kprobe(&rp->kp);
848
849         spin_lock_irqsave(&kretprobe_lock, flags);
850
851         while ((ri = get_used_rp_inst(rp)) != NULL) {
852                 recycle_rp_inst(ri);
853         }
854         free_rp_inst(rp);
855
856         spin_unlock_irqrestore(&kretprobe_lock, flags);
857 }
858
859 void dbi_unregister_kretprobes(struct kretprobe **rpp, size_t size)
860 {
861         size_t i;
862         unsigned long flags;
863
864         spin_lock_irqsave(&kretprobe_lock, flags);
865
866         for (i = 0; i < size; i++)
867                 dbi_unregister_kretprobe_top(rpp[i]);
868
869         spin_unlock_irqrestore(&kretprobe_lock, flags);
870
871         if (!in_atomic())
872                 synchronize_sched();
873
874         for (i = 0; i < size; i++)
875                 dbi_unregister_kretprobe_bottom(rpp[i]);
876 }
877
878 void dbi_unregister_kretprobe(struct kretprobe *rp)
879 {
880         dbi_unregister_kretprobes(&rp, 1);
881 }
882
883 struct kretprobe *clone_kretprobe(struct kretprobe *rp)
884 {
885         struct kprobe *old_p;
886         struct kretprobe *clone = NULL;
887         int ret;
888
889         clone = kmalloc(sizeof(struct kretprobe), GFP_KERNEL);
890         if (!clone) {
891                 DBPRINTF ("failed to alloc memory for clone probe %p!", rp->kp.addr);
892                 return NULL;
893         }
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);
900         if (old_p) {
901                 ret = register_aggr_kprobe(old_p, &clone->kp);
902                 if (ret) {
903                         kfree(clone);
904                         return NULL;
905                 }
906                 atomic_inc(&kprobe_count);
907         }
908
909         return clone;
910 }
911 EXPORT_SYMBOL_GPL(clone_kretprobe);
912
913 static void inline rm_task_trampoline(struct task_struct *p, struct kretprobe_instance *ri)
914 {
915         arch_set_task_pc(p, (unsigned long)ri->ret_addr);
916 }
917
918 static int dbi_disarm_krp_inst(struct kretprobe_instance *ri)
919 {
920         unsigned long *tramp = (unsigned long *)&kretprobe_trampoline;
921         unsigned long *sp = ri->sp;
922         unsigned long *found = NULL;
923         int retval = -ENOENT;
924
925         if (!sp) {
926                 unsigned long pc = arch_get_task_pc(ri->task);
927
928                 printk("---> [%d] %s (%d/%d): pc = %08lx, ra = %08lx, tramp= %08lx (%08lx)\n",
929                        task_cpu(ri->task),
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));
934
935                 /* __switch_to retprobe handling */
936                 if (pc == (unsigned long)tramp) {
937                         rm_task_trampoline(ri->task, ri);
938                         return 0;
939                 }
940
941                 return -EINVAL;
942         }
943
944         while (sp > ri->sp - RETPROBE_STACK_DEPTH) {
945                 if (*sp == (unsigned long)tramp) {
946                         found = sp;
947                         break;
948                 }
949                 sp--;
950         }
951
952         if (found) {
953                 printk("---> [%d] %s (%d/%d): tramp (%08lx) found at %08lx (%08lx /%+d) - %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)found, (long unsigned int)ri->sp,
958                        found - ri->sp, ri->rp ? ri->rp->kp.addr: NULL);
959                 *found = (unsigned long)ri->ret_addr;
960                 retval = 0;
961         } else {
962                 printk("---> [%d] %s (%d/%d): tramp (%08lx) NOT found at sp = %08lx - %p\n",
963                                 task_cpu(ri->task),
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);
967         }
968
969         return retval;
970 }
971
972 static int init_module_deps(void)
973 {
974         int ret;
975
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");
981
982         if (sched_addr == 0 ||
983             exit_addr == 0 ||
984             sys_exit_group_addr == 0 ||
985             do_group_exit_addr == 0 ||
986             sys_exit_addr == 0) {
987                 return -ESRCH;
988         }
989
990         ret = init_module_dependencies();
991         if (ret) {
992                 return ret;
993         }
994
995         return arch_init_module_deps();
996 }
997
998 static int __init init_kprobes(void)
999 {
1000         int i, err = 0;
1001
1002         module_alloc = (void *)swap_ksyms("module_alloc");
1003         if (!module_alloc) {
1004                 printk("module_alloc is not found! Oops.\n");
1005                 return -1;
1006         }
1007         module_free = (void *)swap_ksyms("module_free");
1008         if (!module_alloc) {
1009                 printk("module_free is not found! Oops.\n");
1010                 return -1;
1011         }
1012
1013         init_sm();
1014
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]);
1020         }
1021         atomic_set(&kprobe_count, 0);
1022
1023         err = init_module_deps();
1024         if (err) {
1025                 return err;
1026         }
1027
1028         err = arch_init_kprobes();
1029
1030         DBPRINTF ("init_kprobes: arch_init_kprobes - %d", err);
1031
1032         return err;
1033 }
1034
1035 static void __exit exit_kprobes(void)
1036 {
1037         arch_exit_kprobes();
1038         exit_sm();
1039 }
1040
1041 module_init(init_kprobes);
1042 module_exit(exit_kprobes);
1043
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);
1052
1053 MODULE_LICENSE("Dual BSD/GPL");