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