kprobes: codingstyle cleanups
[powerpc.git] / kernel / 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  * 2002-Oct     Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22  *              Probes initial implementation (includes suggestions from
23  *              Rusty Russell).
24  * 2004-Aug     Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
25  *              hlists and exceptions notifier as suggested by Andi Kleen.
26  * 2004-July    Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
27  *              interface to access function arguments.
28  * 2004-Sep     Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
29  *              exceptions notifier to be first on the priority list.
30  * 2005-May     Hien Nguyen <hien@us.ibm.com>, Jim Keniston
31  *              <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
32  *              <prasanna@in.ibm.com> added function-return probes.
33  */
34 #include <linux/kprobes.h>
35 #include <linux/hash.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/stddef.h>
39 #include <linux/module.h>
40 #include <linux/moduleloader.h>
41 #include <linux/kallsyms.h>
42 #include <linux/freezer.h>
43 #include <linux/seq_file.h>
44 #include <linux/debugfs.h>
45 #include <linux/kdebug.h>
46 #include <asm-generic/sections.h>
47 #include <asm/cacheflush.h>
48 #include <asm/errno.h>
49
50 #define KPROBE_HASH_BITS 6
51 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
52
53
54 /*
55  * Some oddball architectures like 64bit powerpc have function descriptors
56  * so this must be overridable.
57  */
58 #ifndef kprobe_lookup_name
59 #define kprobe_lookup_name(name, addr) \
60         addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
61 #endif
62
63 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
64 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
65 static atomic_t kprobe_count;
66
67 DEFINE_MUTEX(kprobe_mutex);             /* Protects kprobe_table */
68 DEFINE_SPINLOCK(kretprobe_lock);        /* Protects kretprobe_inst_table */
69 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
70
71 static struct notifier_block kprobe_page_fault_nb = {
72         .notifier_call = kprobe_exceptions_notify,
73         .priority = 0x7fffffff /* we need to notified first */
74 };
75
76 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
77 /*
78  * kprobe->ainsn.insn points to the copy of the instruction to be
79  * single-stepped. x86_64, POWER4 and above have no-exec support and
80  * stepping on the instruction on a vmalloced/kmalloced/data page
81  * is a recipe for disaster
82  */
83 #define INSNS_PER_PAGE  (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
84
85 struct kprobe_insn_page {
86         struct hlist_node hlist;
87         kprobe_opcode_t *insns;         /* Page of instruction slots */
88         char slot_used[INSNS_PER_PAGE];
89         int nused;
90         int ngarbage;
91 };
92
93 enum kprobe_slot_state {
94         SLOT_CLEAN = 0,
95         SLOT_DIRTY = 1,
96         SLOT_USED = 2,
97 };
98
99 static struct hlist_head kprobe_insn_pages;
100 static int kprobe_garbage_slots;
101 static int collect_garbage_slots(void);
102
103 static int __kprobes check_safety(void)
104 {
105         int ret = 0;
106 #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
107         ret = freeze_processes();
108         if (ret == 0) {
109                 struct task_struct *p, *q;
110                 do_each_thread(p, q) {
111                         if (p != current && p->state == TASK_RUNNING &&
112                             p->pid != 0) {
113                                 printk("Check failed: %s is running\n",p->comm);
114                                 ret = -1;
115                                 goto loop_end;
116                         }
117                 } while_each_thread(p, q);
118         }
119 loop_end:
120         thaw_processes();
121 #else
122         synchronize_sched();
123 #endif
124         return ret;
125 }
126
127 /**
128  * get_insn_slot() - Find a slot on an executable page for an instruction.
129  * We allocate an executable page if there's no room on existing ones.
130  */
131 kprobe_opcode_t __kprobes *get_insn_slot(void)
132 {
133         struct kprobe_insn_page *kip;
134         struct hlist_node *pos;
135
136  retry:
137         hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
138                 if (kip->nused < INSNS_PER_PAGE) {
139                         int i;
140                         for (i = 0; i < INSNS_PER_PAGE; i++) {
141                                 if (kip->slot_used[i] == SLOT_CLEAN) {
142                                         kip->slot_used[i] = SLOT_USED;
143                                         kip->nused++;
144                                         return kip->insns + (i * MAX_INSN_SIZE);
145                                 }
146                         }
147                         /* Surprise!  No unused slots.  Fix kip->nused. */
148                         kip->nused = INSNS_PER_PAGE;
149                 }
150         }
151
152         /* If there are any garbage slots, collect it and try again. */
153         if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
154                 goto retry;
155         }
156         /* All out of space.  Need to allocate a new page. Use slot 0. */
157         kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
158         if (!kip)
159                 return NULL;
160
161         /*
162          * Use module_alloc so this page is within +/- 2GB of where the
163          * kernel image and loaded module images reside. This is required
164          * so x86_64 can correctly handle the %rip-relative fixups.
165          */
166         kip->insns = module_alloc(PAGE_SIZE);
167         if (!kip->insns) {
168                 kfree(kip);
169                 return NULL;
170         }
171         INIT_HLIST_NODE(&kip->hlist);
172         hlist_add_head(&kip->hlist, &kprobe_insn_pages);
173         memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
174         kip->slot_used[0] = SLOT_USED;
175         kip->nused = 1;
176         kip->ngarbage = 0;
177         return kip->insns;
178 }
179
180 /* Return 1 if all garbages are collected, otherwise 0. */
181 static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
182 {
183         kip->slot_used[idx] = SLOT_CLEAN;
184         kip->nused--;
185         if (kip->nused == 0) {
186                 /*
187                  * Page is no longer in use.  Free it unless
188                  * it's the last one.  We keep the last one
189                  * so as not to have to set it up again the
190                  * next time somebody inserts a probe.
191                  */
192                 hlist_del(&kip->hlist);
193                 if (hlist_empty(&kprobe_insn_pages)) {
194                         INIT_HLIST_NODE(&kip->hlist);
195                         hlist_add_head(&kip->hlist,
196                                        &kprobe_insn_pages);
197                 } else {
198                         module_free(NULL, kip->insns);
199                         kfree(kip);
200                 }
201                 return 1;
202         }
203         return 0;
204 }
205
206 static int __kprobes collect_garbage_slots(void)
207 {
208         struct kprobe_insn_page *kip;
209         struct hlist_node *pos, *next;
210
211         /* Ensure no-one is preepmted on the garbages */
212         if (check_safety() != 0)
213                 return -EAGAIN;
214
215         hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
216                 int i;
217                 if (kip->ngarbage == 0)
218                         continue;
219                 kip->ngarbage = 0;      /* we will collect all garbages */
220                 for (i = 0; i < INSNS_PER_PAGE; i++) {
221                         if (kip->slot_used[i] == SLOT_DIRTY &&
222                             collect_one_slot(kip, i))
223                                 break;
224                 }
225         }
226         kprobe_garbage_slots = 0;
227         return 0;
228 }
229
230 void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
231 {
232         struct kprobe_insn_page *kip;
233         struct hlist_node *pos;
234
235         hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
236                 if (kip->insns <= slot &&
237                     slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
238                         int i = (slot - kip->insns) / MAX_INSN_SIZE;
239                         if (dirty) {
240                                 kip->slot_used[i] = SLOT_DIRTY;
241                                 kip->ngarbage++;
242                         } else {
243                                 collect_one_slot(kip, i);
244                         }
245                         break;
246                 }
247         }
248
249         if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
250                 collect_garbage_slots();
251 }
252 #endif
253
254 /* We have preemption disabled.. so it is safe to use __ versions */
255 static inline void set_kprobe_instance(struct kprobe *kp)
256 {
257         __get_cpu_var(kprobe_instance) = kp;
258 }
259
260 static inline void reset_kprobe_instance(void)
261 {
262         __get_cpu_var(kprobe_instance) = NULL;
263 }
264
265 /*
266  * This routine is called either:
267  *      - under the kprobe_mutex - during kprobe_[un]register()
268  *                              OR
269  *      - with preemption disabled - from arch/xxx/kernel/kprobes.c
270  */
271 struct kprobe __kprobes *get_kprobe(void *addr)
272 {
273         struct hlist_head *head;
274         struct hlist_node *node;
275         struct kprobe *p;
276
277         head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
278         hlist_for_each_entry_rcu(p, node, head, hlist) {
279                 if (p->addr == addr)
280                         return p;
281         }
282         return NULL;
283 }
284
285 /*
286  * Aggregate handlers for multiple kprobes support - these handlers
287  * take care of invoking the individual kprobe handlers on p->list
288  */
289 static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
290 {
291         struct kprobe *kp;
292
293         list_for_each_entry_rcu(kp, &p->list, list) {
294                 if (kp->pre_handler) {
295                         set_kprobe_instance(kp);
296                         if (kp->pre_handler(kp, regs))
297                                 return 1;
298                 }
299                 reset_kprobe_instance();
300         }
301         return 0;
302 }
303
304 static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
305                                         unsigned long flags)
306 {
307         struct kprobe *kp;
308
309         list_for_each_entry_rcu(kp, &p->list, list) {
310                 if (kp->post_handler) {
311                         set_kprobe_instance(kp);
312                         kp->post_handler(kp, regs, flags);
313                         reset_kprobe_instance();
314                 }
315         }
316 }
317
318 static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
319                                         int trapnr)
320 {
321         struct kprobe *cur = __get_cpu_var(kprobe_instance);
322
323         /*
324          * if we faulted "during" the execution of a user specified
325          * probe handler, invoke just that probe's fault handler
326          */
327         if (cur && cur->fault_handler) {
328                 if (cur->fault_handler(cur, regs, trapnr))
329                         return 1;
330         }
331         return 0;
332 }
333
334 static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
335 {
336         struct kprobe *cur = __get_cpu_var(kprobe_instance);
337         int ret = 0;
338
339         if (cur && cur->break_handler) {
340                 if (cur->break_handler(cur, regs))
341                         ret = 1;
342         }
343         reset_kprobe_instance();
344         return ret;
345 }
346
347 /* Walks the list and increments nmissed count for multiprobe case */
348 void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
349 {
350         struct kprobe *kp;
351         if (p->pre_handler != aggr_pre_handler) {
352                 p->nmissed++;
353         } else {
354                 list_for_each_entry_rcu(kp, &p->list, list)
355                         kp->nmissed++;
356         }
357         return;
358 }
359
360 /* Called with kretprobe_lock held */
361 struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp)
362 {
363         struct hlist_node *node;
364         struct kretprobe_instance *ri;
365         hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
366                 return ri;
367         return NULL;
368 }
369
370 /* Called with kretprobe_lock held */
371 static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe
372                                                               *rp)
373 {
374         struct hlist_node *node;
375         struct kretprobe_instance *ri;
376         hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
377                 return ri;
378         return NULL;
379 }
380
381 /* Called with kretprobe_lock held */
382 void __kprobes add_rp_inst(struct kretprobe_instance *ri)
383 {
384         /*
385          * Remove rp inst off the free list -
386          * Add it back when probed function returns
387          */
388         hlist_del(&ri->uflist);
389
390         /* Add rp inst onto table */
391         INIT_HLIST_NODE(&ri->hlist);
392         hlist_add_head(&ri->hlist,
393                         &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
394
395         /* Also add this rp inst to the used list. */
396         INIT_HLIST_NODE(&ri->uflist);
397         hlist_add_head(&ri->uflist, &ri->rp->used_instances);
398 }
399
400 /* Called with kretprobe_lock held */
401 void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
402                                 struct hlist_head *head)
403 {
404         /* remove rp inst off the rprobe_inst_table */
405         hlist_del(&ri->hlist);
406         if (ri->rp) {
407                 /* remove rp inst off the used list */
408                 hlist_del(&ri->uflist);
409                 /* put rp inst back onto the free list */
410                 INIT_HLIST_NODE(&ri->uflist);
411                 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
412         } else
413                 /* Unregistering */
414                 hlist_add_head(&ri->hlist, head);
415 }
416
417 struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
418 {
419         return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
420 }
421
422 /*
423  * This function is called from finish_task_switch when task tk becomes dead,
424  * so that we can recycle any function-return probe instances associated
425  * with this task. These left over instances represent probed functions
426  * that have been called but will never return.
427  */
428 void __kprobes kprobe_flush_task(struct task_struct *tk)
429 {
430         struct kretprobe_instance *ri;
431         struct hlist_head *head, empty_rp;
432         struct hlist_node *node, *tmp;
433         unsigned long flags = 0;
434
435         INIT_HLIST_HEAD(&empty_rp);
436         spin_lock_irqsave(&kretprobe_lock, flags);
437         head = kretprobe_inst_table_head(tk);
438         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
439                 if (ri->task == tk)
440                         recycle_rp_inst(ri, &empty_rp);
441         }
442         spin_unlock_irqrestore(&kretprobe_lock, flags);
443
444         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
445                 hlist_del(&ri->hlist);
446                 kfree(ri);
447         }
448 }
449
450 static inline void free_rp_inst(struct kretprobe *rp)
451 {
452         struct kretprobe_instance *ri;
453         while ((ri = get_free_rp_inst(rp)) != NULL) {
454                 hlist_del(&ri->uflist);
455                 kfree(ri);
456         }
457 }
458
459 /*
460  * Keep all fields in the kprobe consistent
461  */
462 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
463 {
464         memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
465         memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
466 }
467
468 /*
469 * Add the new probe to old_p->list. Fail if this is the
470 * second jprobe at the address - two jprobes can't coexist
471 */
472 static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
473 {
474         if (p->break_handler) {
475                 if (old_p->break_handler)
476                         return -EEXIST;
477                 list_add_tail_rcu(&p->list, &old_p->list);
478                 old_p->break_handler = aggr_break_handler;
479         } else
480                 list_add_rcu(&p->list, &old_p->list);
481         if (p->post_handler && !old_p->post_handler)
482                 old_p->post_handler = aggr_post_handler;
483         return 0;
484 }
485
486 /*
487  * Fill in the required fields of the "manager kprobe". Replace the
488  * earlier kprobe in the hlist with the manager kprobe
489  */
490 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
491 {
492         copy_kprobe(p, ap);
493         flush_insn_slot(ap);
494         ap->addr = p->addr;
495         ap->pre_handler = aggr_pre_handler;
496         ap->fault_handler = aggr_fault_handler;
497         if (p->post_handler)
498                 ap->post_handler = aggr_post_handler;
499         if (p->break_handler)
500                 ap->break_handler = aggr_break_handler;
501
502         INIT_LIST_HEAD(&ap->list);
503         list_add_rcu(&p->list, &ap->list);
504
505         hlist_replace_rcu(&p->hlist, &ap->hlist);
506 }
507
508 /*
509  * This is the second or subsequent kprobe at the address - handle
510  * the intricacies
511  */
512 static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
513                                           struct kprobe *p)
514 {
515         int ret = 0;
516         struct kprobe *ap;
517
518         if (old_p->pre_handler == aggr_pre_handler) {
519                 copy_kprobe(old_p, p);
520                 ret = add_new_kprobe(old_p, p);
521         } else {
522                 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
523                 if (!ap)
524                         return -ENOMEM;
525                 add_aggr_kprobe(ap, old_p);
526                 copy_kprobe(ap, p);
527                 ret = add_new_kprobe(ap, p);
528         }
529         return ret;
530 }
531
532 static int __kprobes in_kprobes_functions(unsigned long addr)
533 {
534         if (addr >= (unsigned long)__kprobes_text_start &&
535             addr < (unsigned long)__kprobes_text_end)
536                 return -EINVAL;
537         return 0;
538 }
539
540 static int __kprobes __register_kprobe(struct kprobe *p,
541         unsigned long called_from)
542 {
543         int ret = 0;
544         struct kprobe *old_p;
545         struct module *probed_mod;
546
547         /*
548          * If we have a symbol_name argument look it up,
549          * and add it to the address.  That way the addr
550          * field can either be global or relative to a symbol.
551          */
552         if (p->symbol_name) {
553                 if (p->addr)
554                         return -EINVAL;
555                 kprobe_lookup_name(p->symbol_name, p->addr);
556         }
557
558         if (!p->addr)
559                 return -EINVAL;
560         p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
561
562         if (!kernel_text_address((unsigned long) p->addr) ||
563             in_kprobes_functions((unsigned long) p->addr))
564                 return -EINVAL;
565
566         p->mod_refcounted = 0;
567
568         /*
569          * Check if are we probing a module.
570          */
571         probed_mod = module_text_address((unsigned long) p->addr);
572         if (probed_mod) {
573                 struct module *calling_mod = module_text_address(called_from);
574                 /*
575                  * We must allow modules to probe themself and in this case
576                  * avoid incrementing the module refcount, so as to allow
577                  * unloading of self probing modules.
578                  */
579                 if (calling_mod && calling_mod != probed_mod) {
580                         if (unlikely(!try_module_get(probed_mod)))
581                                 return -EINVAL;
582                         p->mod_refcounted = 1;
583                 } else
584                         probed_mod = NULL;
585         }
586
587         p->nmissed = 0;
588         mutex_lock(&kprobe_mutex);
589         old_p = get_kprobe(p->addr);
590         if (old_p) {
591                 ret = register_aggr_kprobe(old_p, p);
592                 if (!ret)
593                         atomic_inc(&kprobe_count);
594                 goto out;
595         }
596
597         ret = arch_prepare_kprobe(p);
598         if (ret)
599                 goto out;
600
601         INIT_HLIST_NODE(&p->hlist);
602         hlist_add_head_rcu(&p->hlist,
603                        &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
604
605         if (atomic_add_return(1, &kprobe_count) == \
606                                 (ARCH_INACTIVE_KPROBE_COUNT + 1))
607                 register_page_fault_notifier(&kprobe_page_fault_nb);
608
609         arch_arm_kprobe(p);
610
611 out:
612         mutex_unlock(&kprobe_mutex);
613
614         if (ret && probed_mod)
615                 module_put(probed_mod);
616         return ret;
617 }
618
619 int __kprobes register_kprobe(struct kprobe *p)
620 {
621         return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
622 }
623
624 void __kprobes unregister_kprobe(struct kprobe *p)
625 {
626         struct module *mod;
627         struct kprobe *old_p, *list_p;
628         int cleanup_p;
629
630         mutex_lock(&kprobe_mutex);
631         old_p = get_kprobe(p->addr);
632         if (unlikely(!old_p)) {
633                 mutex_unlock(&kprobe_mutex);
634                 return;
635         }
636         if (p != old_p) {
637                 list_for_each_entry_rcu(list_p, &old_p->list, list)
638                         if (list_p == p)
639                         /* kprobe p is a valid probe */
640                                 goto valid_p;
641                 mutex_unlock(&kprobe_mutex);
642                 return;
643         }
644 valid_p:
645         if (old_p == p ||
646             (old_p->pre_handler == aggr_pre_handler &&
647              p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
648                 /* Only probe on the hash list */
649                 arch_disarm_kprobe(p);
650                 hlist_del_rcu(&old_p->hlist);
651                 cleanup_p = 1;
652         } else {
653                 list_del_rcu(&p->list);
654                 cleanup_p = 0;
655         }
656
657         mutex_unlock(&kprobe_mutex);
658
659         synchronize_sched();
660         if (p->mod_refcounted) {
661                 mod = module_text_address((unsigned long)p->addr);
662                 if (mod)
663                         module_put(mod);
664         }
665
666         if (cleanup_p) {
667                 if (p != old_p) {
668                         list_del_rcu(&p->list);
669                         kfree(old_p);
670                 }
671                 arch_remove_kprobe(p);
672         } else {
673                 mutex_lock(&kprobe_mutex);
674                 if (p->break_handler)
675                         old_p->break_handler = NULL;
676                 if (p->post_handler){
677                         list_for_each_entry_rcu(list_p, &old_p->list, list){
678                                 if (list_p->post_handler){
679                                         cleanup_p = 2;
680                                         break;
681                                 }
682                         }
683                         if (cleanup_p == 0)
684                                 old_p->post_handler = NULL;
685                 }
686                 mutex_unlock(&kprobe_mutex);
687         }
688
689         /* Call unregister_page_fault_notifier()
690          * if no probes are active
691          */
692         mutex_lock(&kprobe_mutex);
693         if (atomic_add_return(-1, &kprobe_count) == \
694                                 ARCH_INACTIVE_KPROBE_COUNT)
695                 unregister_page_fault_notifier(&kprobe_page_fault_nb);
696         mutex_unlock(&kprobe_mutex);
697         return;
698 }
699
700 static struct notifier_block kprobe_exceptions_nb = {
701         .notifier_call = kprobe_exceptions_notify,
702         .priority = 0x7fffffff /* we need to be notified first */
703 };
704
705
706 int __kprobes register_jprobe(struct jprobe *jp)
707 {
708         /* Todo: Verify probepoint is a function entry point */
709         jp->kp.pre_handler = setjmp_pre_handler;
710         jp->kp.break_handler = longjmp_break_handler;
711
712         return __register_kprobe(&jp->kp,
713                 (unsigned long)__builtin_return_address(0));
714 }
715
716 void __kprobes unregister_jprobe(struct jprobe *jp)
717 {
718         unregister_kprobe(&jp->kp);
719 }
720
721 #ifdef ARCH_SUPPORTS_KRETPROBES
722
723 /*
724  * This kprobe pre_handler is registered with every kretprobe. When probe
725  * hits it will set up the return probe.
726  */
727 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
728                                            struct pt_regs *regs)
729 {
730         struct kretprobe *rp = container_of(p, struct kretprobe, kp);
731         unsigned long flags = 0;
732
733         /*TODO: consider to only swap the RA after the last pre_handler fired */
734         spin_lock_irqsave(&kretprobe_lock, flags);
735         arch_prepare_kretprobe(rp, regs);
736         spin_unlock_irqrestore(&kretprobe_lock, flags);
737         return 0;
738 }
739
740 int __kprobes register_kretprobe(struct kretprobe *rp)
741 {
742         int ret = 0;
743         struct kretprobe_instance *inst;
744         int i;
745
746         rp->kp.pre_handler = pre_handler_kretprobe;
747         rp->kp.post_handler = NULL;
748         rp->kp.fault_handler = NULL;
749         rp->kp.break_handler = NULL;
750
751         /* Pre-allocate memory for max kretprobe instances */
752         if (rp->maxactive <= 0) {
753 #ifdef CONFIG_PREEMPT
754                 rp->maxactive = max(10, 2 * NR_CPUS);
755 #else
756                 rp->maxactive = NR_CPUS;
757 #endif
758         }
759         INIT_HLIST_HEAD(&rp->used_instances);
760         INIT_HLIST_HEAD(&rp->free_instances);
761         for (i = 0; i < rp->maxactive; i++) {
762                 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
763                 if (inst == NULL) {
764                         free_rp_inst(rp);
765                         return -ENOMEM;
766                 }
767                 INIT_HLIST_NODE(&inst->uflist);
768                 hlist_add_head(&inst->uflist, &rp->free_instances);
769         }
770
771         rp->nmissed = 0;
772         /* Establish function entry probe point */
773         if ((ret = __register_kprobe(&rp->kp,
774                 (unsigned long)__builtin_return_address(0))) != 0)
775                 free_rp_inst(rp);
776         return ret;
777 }
778
779 #else /* ARCH_SUPPORTS_KRETPROBES */
780
781 int __kprobes register_kretprobe(struct kretprobe *rp)
782 {
783         return -ENOSYS;
784 }
785
786 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
787                                            struct pt_regs *regs)
788 {
789         return 0;
790 }
791
792 #endif /* ARCH_SUPPORTS_KRETPROBES */
793
794 void __kprobes unregister_kretprobe(struct kretprobe *rp)
795 {
796         unsigned long flags;
797         struct kretprobe_instance *ri;
798
799         unregister_kprobe(&rp->kp);
800         /* No race here */
801         spin_lock_irqsave(&kretprobe_lock, flags);
802         while ((ri = get_used_rp_inst(rp)) != NULL) {
803                 ri->rp = NULL;
804                 hlist_del(&ri->uflist);
805         }
806         spin_unlock_irqrestore(&kretprobe_lock, flags);
807         free_rp_inst(rp);
808 }
809
810 static int __init init_kprobes(void)
811 {
812         int i, err = 0;
813
814         /* FIXME allocate the probe table, currently defined statically */
815         /* initialize all list heads */
816         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
817                 INIT_HLIST_HEAD(&kprobe_table[i]);
818                 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
819         }
820         atomic_set(&kprobe_count, 0);
821
822         err = arch_init_kprobes();
823         if (!err)
824                 err = register_die_notifier(&kprobe_exceptions_nb);
825
826         return err;
827 }
828
829 #ifdef CONFIG_DEBUG_FS
830 static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
831                const char *sym, int offset,char *modname)
832 {
833         char *kprobe_type;
834
835         if (p->pre_handler == pre_handler_kretprobe)
836                 kprobe_type = "r";
837         else if (p->pre_handler == setjmp_pre_handler)
838                 kprobe_type = "j";
839         else
840                 kprobe_type = "k";
841         if (sym)
842                 seq_printf(pi, "%p  %s  %s+0x%x  %s\n", p->addr, kprobe_type,
843                         sym, offset, (modname ? modname : " "));
844         else
845                 seq_printf(pi, "%p  %s  %p\n", p->addr, kprobe_type, p->addr);
846 }
847
848 static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
849 {
850         return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
851 }
852
853 static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
854 {
855         (*pos)++;
856         if (*pos >= KPROBE_TABLE_SIZE)
857                 return NULL;
858         return pos;
859 }
860
861 static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
862 {
863         /* Nothing to do */
864 }
865
866 static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
867 {
868         struct hlist_head *head;
869         struct hlist_node *node;
870         struct kprobe *p, *kp;
871         const char *sym = NULL;
872         unsigned int i = *(loff_t *) v;
873         unsigned long offset = 0;
874         char *modname, namebuf[128];
875
876         head = &kprobe_table[i];
877         preempt_disable();
878         hlist_for_each_entry_rcu(p, node, head, hlist) {
879                 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
880                                         &offset, &modname, namebuf);
881                 if (p->pre_handler == aggr_pre_handler) {
882                         list_for_each_entry_rcu(kp, &p->list, list)
883                                 report_probe(pi, kp, sym, offset, modname);
884                 } else
885                         report_probe(pi, p, sym, offset, modname);
886         }
887         preempt_enable();
888         return 0;
889 }
890
891 static struct seq_operations kprobes_seq_ops = {
892         .start = kprobe_seq_start,
893         .next  = kprobe_seq_next,
894         .stop  = kprobe_seq_stop,
895         .show  = show_kprobe_addr
896 };
897
898 static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
899 {
900         return seq_open(filp, &kprobes_seq_ops);
901 }
902
903 static struct file_operations debugfs_kprobes_operations = {
904         .open           = kprobes_open,
905         .read           = seq_read,
906         .llseek         = seq_lseek,
907         .release        = seq_release,
908 };
909
910 static int __kprobes debugfs_kprobe_init(void)
911 {
912         struct dentry *dir, *file;
913
914         dir = debugfs_create_dir("kprobes", NULL);
915         if (!dir)
916                 return -ENOMEM;
917
918         file = debugfs_create_file("list", 0444, dir, NULL,
919                                 &debugfs_kprobes_operations);
920         if (!file) {
921                 debugfs_remove(dir);
922                 return -ENOMEM;
923         }
924
925         return 0;
926 }
927
928 late_initcall(debugfs_kprobe_init);
929 #endif /* CONFIG_DEBUG_FS */
930
931 module_init(init_kprobes);
932
933 EXPORT_SYMBOL_GPL(register_kprobe);
934 EXPORT_SYMBOL_GPL(unregister_kprobe);
935 EXPORT_SYMBOL_GPL(register_jprobe);
936 EXPORT_SYMBOL_GPL(unregister_jprobe);
937 EXPORT_SYMBOL_GPL(jprobe_return);
938 EXPORT_SYMBOL_GPL(register_kretprobe);
939 EXPORT_SYMBOL_GPL(unregister_kretprobe);