kprobes: Introduce generic insn_slot framework
[linux-2.6.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 <linux/memory.h>
47 #include <linux/ftrace.h>
48
49 #include <asm-generic/sections.h>
50 #include <asm/cacheflush.h>
51 #include <asm/errno.h>
52 #include <asm/uaccess.h>
53
54 #define KPROBE_HASH_BITS 6
55 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
56
57
58 /*
59  * Some oddball architectures like 64bit powerpc have function descriptors
60  * so this must be overridable.
61  */
62 #ifndef kprobe_lookup_name
63 #define kprobe_lookup_name(name, addr) \
64         addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
65 #endif
66
67 static int kprobes_initialized;
68 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
69 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
70
71 /* NOTE: change this value only with kprobe_mutex held */
72 static bool kprobes_all_disarmed;
73
74 static DEFINE_MUTEX(kprobe_mutex);      /* Protects kprobe_table */
75 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
76 static struct {
77         spinlock_t lock ____cacheline_aligned_in_smp;
78 } kretprobe_table_locks[KPROBE_TABLE_SIZE];
79
80 static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
81 {
82         return &(kretprobe_table_locks[hash].lock);
83 }
84
85 /*
86  * Normally, functions that we'd want to prohibit kprobes in, are marked
87  * __kprobes. But, there are cases where such functions already belong to
88  * a different section (__sched for preempt_schedule)
89  *
90  * For such cases, we now have a blacklist
91  */
92 static struct kprobe_blackpoint kprobe_blacklist[] = {
93         {"preempt_schedule",},
94         {"native_get_debugreg",},
95         {"irq_entries_start",},
96         {"common_interrupt",},
97         {"mcount",},    /* mcount can be called from everywhere */
98         {NULL}    /* Terminator */
99 };
100
101 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
102 /*
103  * kprobe->ainsn.insn points to the copy of the instruction to be
104  * single-stepped. x86_64, POWER4 and above have no-exec support and
105  * stepping on the instruction on a vmalloced/kmalloced/data page
106  * is a recipe for disaster
107  */
108 struct kprobe_insn_page {
109         struct list_head list;
110         kprobe_opcode_t *insns;         /* Page of instruction slots */
111         int nused;
112         int ngarbage;
113         char slot_used[];
114 };
115
116 #define KPROBE_INSN_PAGE_SIZE(slots)                    \
117         (offsetof(struct kprobe_insn_page, slot_used) + \
118          (sizeof(char) * (slots)))
119
120 struct kprobe_insn_cache {
121         struct list_head pages; /* list of kprobe_insn_page */
122         size_t insn_size;       /* size of instruction slot */
123         int nr_garbage;
124 };
125
126 static int slots_per_page(struct kprobe_insn_cache *c)
127 {
128         return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
129 }
130
131 enum kprobe_slot_state {
132         SLOT_CLEAN = 0,
133         SLOT_DIRTY = 1,
134         SLOT_USED = 2,
135 };
136
137 static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_slots */
138 static struct kprobe_insn_cache kprobe_insn_slots = {
139         .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
140         .insn_size = MAX_INSN_SIZE,
141         .nr_garbage = 0,
142 };
143 static int __kprobes collect_garbage_slots(struct kprobe_insn_cache *c);
144
145 /**
146  * __get_insn_slot() - Find a slot on an executable page for an instruction.
147  * We allocate an executable page if there's no room on existing ones.
148  */
149 static kprobe_opcode_t __kprobes *__get_insn_slot(struct kprobe_insn_cache *c)
150 {
151         struct kprobe_insn_page *kip;
152
153  retry:
154         list_for_each_entry(kip, &c->pages, list) {
155                 if (kip->nused < slots_per_page(c)) {
156                         int i;
157                         for (i = 0; i < slots_per_page(c); i++) {
158                                 if (kip->slot_used[i] == SLOT_CLEAN) {
159                                         kip->slot_used[i] = SLOT_USED;
160                                         kip->nused++;
161                                         return kip->insns + (i * c->insn_size);
162                                 }
163                         }
164                         /* kip->nused is broken. Fix it. */
165                         kip->nused = slots_per_page(c);
166                         WARN_ON(1);
167                 }
168         }
169
170         /* If there are any garbage slots, collect it and try again. */
171         if (c->nr_garbage && collect_garbage_slots(c) == 0)
172                 goto retry;
173
174         /* All out of space.  Need to allocate a new page. */
175         kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
176         if (!kip)
177                 return NULL;
178
179         /*
180          * Use module_alloc so this page is within +/- 2GB of where the
181          * kernel image and loaded module images reside. This is required
182          * so x86_64 can correctly handle the %rip-relative fixups.
183          */
184         kip->insns = module_alloc(PAGE_SIZE);
185         if (!kip->insns) {
186                 kfree(kip);
187                 return NULL;
188         }
189         INIT_LIST_HEAD(&kip->list);
190         memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
191         kip->slot_used[0] = SLOT_USED;
192         kip->nused = 1;
193         kip->ngarbage = 0;
194         list_add(&kip->list, &c->pages);
195         return kip->insns;
196 }
197
198
199 kprobe_opcode_t __kprobes *get_insn_slot(void)
200 {
201         kprobe_opcode_t *ret = NULL;
202
203         mutex_lock(&kprobe_insn_mutex);
204         ret = __get_insn_slot(&kprobe_insn_slots);
205         mutex_unlock(&kprobe_insn_mutex);
206
207         return ret;
208 }
209
210 /* Return 1 if all garbages are collected, otherwise 0. */
211 static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
212 {
213         kip->slot_used[idx] = SLOT_CLEAN;
214         kip->nused--;
215         if (kip->nused == 0) {
216                 /*
217                  * Page is no longer in use.  Free it unless
218                  * it's the last one.  We keep the last one
219                  * so as not to have to set it up again the
220                  * next time somebody inserts a probe.
221                  */
222                 if (!list_is_singular(&kip->list)) {
223                         list_del(&kip->list);
224                         module_free(NULL, kip->insns);
225                         kfree(kip);
226                 }
227                 return 1;
228         }
229         return 0;
230 }
231
232 static int __kprobes collect_garbage_slots(struct kprobe_insn_cache *c)
233 {
234         struct kprobe_insn_page *kip, *next;
235
236         /* Ensure no-one is interrupted on the garbages */
237         synchronize_sched();
238
239         list_for_each_entry_safe(kip, next, &c->pages, list) {
240                 int i;
241                 if (kip->ngarbage == 0)
242                         continue;
243                 kip->ngarbage = 0;      /* we will collect all garbages */
244                 for (i = 0; i < slots_per_page(c); i++) {
245                         if (kip->slot_used[i] == SLOT_DIRTY &&
246                             collect_one_slot(kip, i))
247                                 break;
248                 }
249         }
250         c->nr_garbage = 0;
251         return 0;
252 }
253
254 static void __kprobes __free_insn_slot(struct kprobe_insn_cache *c,
255                                        kprobe_opcode_t *slot, int dirty)
256 {
257         struct kprobe_insn_page *kip;
258
259         list_for_each_entry(kip, &c->pages, list) {
260                 long idx = ((long)slot - (long)kip->insns) / c->insn_size;
261                 if (idx >= 0 && idx < slots_per_page(c)) {
262                         WARN_ON(kip->slot_used[idx] != SLOT_USED);
263                         if (dirty) {
264                                 kip->slot_used[idx] = SLOT_DIRTY;
265                                 kip->ngarbage++;
266                                 if (++c->nr_garbage > slots_per_page(c))
267                                         collect_garbage_slots(c);
268                         } else
269                                 collect_one_slot(kip, idx);
270                         return;
271                 }
272         }
273         /* Could not free this slot. */
274         WARN_ON(1);
275 }
276
277 void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
278 {
279         mutex_lock(&kprobe_insn_mutex);
280         __free_insn_slot(&kprobe_insn_slots, slot, dirty);
281         mutex_unlock(&kprobe_insn_mutex);
282 }
283 #endif
284
285 /* We have preemption disabled.. so it is safe to use __ versions */
286 static inline void set_kprobe_instance(struct kprobe *kp)
287 {
288         __get_cpu_var(kprobe_instance) = kp;
289 }
290
291 static inline void reset_kprobe_instance(void)
292 {
293         __get_cpu_var(kprobe_instance) = NULL;
294 }
295
296 /*
297  * This routine is called either:
298  *      - under the kprobe_mutex - during kprobe_[un]register()
299  *                              OR
300  *      - with preemption disabled - from arch/xxx/kernel/kprobes.c
301  */
302 struct kprobe __kprobes *get_kprobe(void *addr)
303 {
304         struct hlist_head *head;
305         struct hlist_node *node;
306         struct kprobe *p;
307
308         head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
309         hlist_for_each_entry_rcu(p, node, head, hlist) {
310                 if (p->addr == addr)
311                         return p;
312         }
313         return NULL;
314 }
315
316 /* Arm a kprobe with text_mutex */
317 static void __kprobes arm_kprobe(struct kprobe *kp)
318 {
319         mutex_lock(&text_mutex);
320         arch_arm_kprobe(kp);
321         mutex_unlock(&text_mutex);
322 }
323
324 /* Disarm a kprobe with text_mutex */
325 static void __kprobes disarm_kprobe(struct kprobe *kp)
326 {
327         mutex_lock(&text_mutex);
328         arch_disarm_kprobe(kp);
329         mutex_unlock(&text_mutex);
330 }
331
332 /*
333  * Aggregate handlers for multiple kprobes support - these handlers
334  * take care of invoking the individual kprobe handlers on p->list
335  */
336 static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
337 {
338         struct kprobe *kp;
339
340         list_for_each_entry_rcu(kp, &p->list, list) {
341                 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
342                         set_kprobe_instance(kp);
343                         if (kp->pre_handler(kp, regs))
344                                 return 1;
345                 }
346                 reset_kprobe_instance();
347         }
348         return 0;
349 }
350
351 static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
352                                         unsigned long flags)
353 {
354         struct kprobe *kp;
355
356         list_for_each_entry_rcu(kp, &p->list, list) {
357                 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
358                         set_kprobe_instance(kp);
359                         kp->post_handler(kp, regs, flags);
360                         reset_kprobe_instance();
361                 }
362         }
363 }
364
365 static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
366                                         int trapnr)
367 {
368         struct kprobe *cur = __get_cpu_var(kprobe_instance);
369
370         /*
371          * if we faulted "during" the execution of a user specified
372          * probe handler, invoke just that probe's fault handler
373          */
374         if (cur && cur->fault_handler) {
375                 if (cur->fault_handler(cur, regs, trapnr))
376                         return 1;
377         }
378         return 0;
379 }
380
381 static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
382 {
383         struct kprobe *cur = __get_cpu_var(kprobe_instance);
384         int ret = 0;
385
386         if (cur && cur->break_handler) {
387                 if (cur->break_handler(cur, regs))
388                         ret = 1;
389         }
390         reset_kprobe_instance();
391         return ret;
392 }
393
394 /* Walks the list and increments nmissed count for multiprobe case */
395 void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
396 {
397         struct kprobe *kp;
398         if (p->pre_handler != aggr_pre_handler) {
399                 p->nmissed++;
400         } else {
401                 list_for_each_entry_rcu(kp, &p->list, list)
402                         kp->nmissed++;
403         }
404         return;
405 }
406
407 void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
408                                 struct hlist_head *head)
409 {
410         struct kretprobe *rp = ri->rp;
411
412         /* remove rp inst off the rprobe_inst_table */
413         hlist_del(&ri->hlist);
414         INIT_HLIST_NODE(&ri->hlist);
415         if (likely(rp)) {
416                 spin_lock(&rp->lock);
417                 hlist_add_head(&ri->hlist, &rp->free_instances);
418                 spin_unlock(&rp->lock);
419         } else
420                 /* Unregistering */
421                 hlist_add_head(&ri->hlist, head);
422 }
423
424 void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
425                          struct hlist_head **head, unsigned long *flags)
426 {
427         unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
428         spinlock_t *hlist_lock;
429
430         *head = &kretprobe_inst_table[hash];
431         hlist_lock = kretprobe_table_lock_ptr(hash);
432         spin_lock_irqsave(hlist_lock, *flags);
433 }
434
435 static void __kprobes kretprobe_table_lock(unsigned long hash,
436         unsigned long *flags)
437 {
438         spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
439         spin_lock_irqsave(hlist_lock, *flags);
440 }
441
442 void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
443         unsigned long *flags)
444 {
445         unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
446         spinlock_t *hlist_lock;
447
448         hlist_lock = kretprobe_table_lock_ptr(hash);
449         spin_unlock_irqrestore(hlist_lock, *flags);
450 }
451
452 void __kprobes kretprobe_table_unlock(unsigned long hash, unsigned long *flags)
453 {
454         spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
455         spin_unlock_irqrestore(hlist_lock, *flags);
456 }
457
458 /*
459  * This function is called from finish_task_switch when task tk becomes dead,
460  * so that we can recycle any function-return probe instances associated
461  * with this task. These left over instances represent probed functions
462  * that have been called but will never return.
463  */
464 void __kprobes kprobe_flush_task(struct task_struct *tk)
465 {
466         struct kretprobe_instance *ri;
467         struct hlist_head *head, empty_rp;
468         struct hlist_node *node, *tmp;
469         unsigned long hash, flags = 0;
470
471         if (unlikely(!kprobes_initialized))
472                 /* Early boot.  kretprobe_table_locks not yet initialized. */
473                 return;
474
475         hash = hash_ptr(tk, KPROBE_HASH_BITS);
476         head = &kretprobe_inst_table[hash];
477         kretprobe_table_lock(hash, &flags);
478         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
479                 if (ri->task == tk)
480                         recycle_rp_inst(ri, &empty_rp);
481         }
482         kretprobe_table_unlock(hash, &flags);
483         INIT_HLIST_HEAD(&empty_rp);
484         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
485                 hlist_del(&ri->hlist);
486                 kfree(ri);
487         }
488 }
489
490 static inline void free_rp_inst(struct kretprobe *rp)
491 {
492         struct kretprobe_instance *ri;
493         struct hlist_node *pos, *next;
494
495         hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
496                 hlist_del(&ri->hlist);
497                 kfree(ri);
498         }
499 }
500
501 static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
502 {
503         unsigned long flags, hash;
504         struct kretprobe_instance *ri;
505         struct hlist_node *pos, *next;
506         struct hlist_head *head;
507
508         /* No race here */
509         for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
510                 kretprobe_table_lock(hash, &flags);
511                 head = &kretprobe_inst_table[hash];
512                 hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
513                         if (ri->rp == rp)
514                                 ri->rp = NULL;
515                 }
516                 kretprobe_table_unlock(hash, &flags);
517         }
518         free_rp_inst(rp);
519 }
520
521 /*
522  * Keep all fields in the kprobe consistent
523  */
524 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
525 {
526         memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
527         memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
528 }
529
530 /*
531 * Add the new probe to ap->list. Fail if this is the
532 * second jprobe at the address - two jprobes can't coexist
533 */
534 static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
535 {
536         BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
537         if (p->break_handler) {
538                 if (ap->break_handler)
539                         return -EEXIST;
540                 list_add_tail_rcu(&p->list, &ap->list);
541                 ap->break_handler = aggr_break_handler;
542         } else
543                 list_add_rcu(&p->list, &ap->list);
544         if (p->post_handler && !ap->post_handler)
545                 ap->post_handler = aggr_post_handler;
546
547         if (kprobe_disabled(ap) && !kprobe_disabled(p)) {
548                 ap->flags &= ~KPROBE_FLAG_DISABLED;
549                 if (!kprobes_all_disarmed)
550                         /* Arm the breakpoint again. */
551                         arm_kprobe(ap);
552         }
553         return 0;
554 }
555
556 /*
557  * Fill in the required fields of the "manager kprobe". Replace the
558  * earlier kprobe in the hlist with the manager kprobe
559  */
560 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
561 {
562         copy_kprobe(p, ap);
563         flush_insn_slot(ap);
564         ap->addr = p->addr;
565         ap->flags = p->flags;
566         ap->pre_handler = aggr_pre_handler;
567         ap->fault_handler = aggr_fault_handler;
568         /* We don't care the kprobe which has gone. */
569         if (p->post_handler && !kprobe_gone(p))
570                 ap->post_handler = aggr_post_handler;
571         if (p->break_handler && !kprobe_gone(p))
572                 ap->break_handler = aggr_break_handler;
573
574         INIT_LIST_HEAD(&ap->list);
575         list_add_rcu(&p->list, &ap->list);
576
577         hlist_replace_rcu(&p->hlist, &ap->hlist);
578 }
579
580 /*
581  * This is the second or subsequent kprobe at the address - handle
582  * the intricacies
583  */
584 static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
585                                           struct kprobe *p)
586 {
587         int ret = 0;
588         struct kprobe *ap = old_p;
589
590         if (old_p->pre_handler != aggr_pre_handler) {
591                 /* If old_p is not an aggr_probe, create new aggr_kprobe. */
592                 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
593                 if (!ap)
594                         return -ENOMEM;
595                 add_aggr_kprobe(ap, old_p);
596         }
597
598         if (kprobe_gone(ap)) {
599                 /*
600                  * Attempting to insert new probe at the same location that
601                  * had a probe in the module vaddr area which already
602                  * freed. So, the instruction slot has already been
603                  * released. We need a new slot for the new probe.
604                  */
605                 ret = arch_prepare_kprobe(ap);
606                 if (ret)
607                         /*
608                          * Even if fail to allocate new slot, don't need to
609                          * free aggr_probe. It will be used next time, or
610                          * freed by unregister_kprobe.
611                          */
612                         return ret;
613
614                 /*
615                  * Clear gone flag to prevent allocating new slot again, and
616                  * set disabled flag because it is not armed yet.
617                  */
618                 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
619                             | KPROBE_FLAG_DISABLED;
620         }
621
622         copy_kprobe(ap, p);
623         return add_new_kprobe(ap, p);
624 }
625
626 /* Try to disable aggr_kprobe, and return 1 if succeeded.*/
627 static int __kprobes try_to_disable_aggr_kprobe(struct kprobe *p)
628 {
629         struct kprobe *kp;
630
631         list_for_each_entry_rcu(kp, &p->list, list) {
632                 if (!kprobe_disabled(kp))
633                         /*
634                          * There is an active probe on the list.
635                          * We can't disable aggr_kprobe.
636                          */
637                         return 0;
638         }
639         p->flags |= KPROBE_FLAG_DISABLED;
640         return 1;
641 }
642
643 static int __kprobes in_kprobes_functions(unsigned long addr)
644 {
645         struct kprobe_blackpoint *kb;
646
647         if (addr >= (unsigned long)__kprobes_text_start &&
648             addr < (unsigned long)__kprobes_text_end)
649                 return -EINVAL;
650         /*
651          * If there exists a kprobe_blacklist, verify and
652          * fail any probe registration in the prohibited area
653          */
654         for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
655                 if (kb->start_addr) {
656                         if (addr >= kb->start_addr &&
657                             addr < (kb->start_addr + kb->range))
658                                 return -EINVAL;
659                 }
660         }
661         return 0;
662 }
663
664 /*
665  * If we have a symbol_name argument, look it up and add the offset field
666  * to it. This way, we can specify a relative address to a symbol.
667  */
668 static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
669 {
670         kprobe_opcode_t *addr = p->addr;
671         if (p->symbol_name) {
672                 if (addr)
673                         return NULL;
674                 kprobe_lookup_name(p->symbol_name, addr);
675         }
676
677         if (!addr)
678                 return NULL;
679         return (kprobe_opcode_t *)(((char *)addr) + p->offset);
680 }
681
682 /* Check passed kprobe is valid and return kprobe in kprobe_table. */
683 static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
684 {
685         struct kprobe *old_p, *list_p;
686
687         old_p = get_kprobe(p->addr);
688         if (unlikely(!old_p))
689                 return NULL;
690
691         if (p != old_p) {
692                 list_for_each_entry_rcu(list_p, &old_p->list, list)
693                         if (list_p == p)
694                         /* kprobe p is a valid probe */
695                                 goto valid;
696                 return NULL;
697         }
698 valid:
699         return old_p;
700 }
701
702 /* Return error if the kprobe is being re-registered */
703 static inline int check_kprobe_rereg(struct kprobe *p)
704 {
705         int ret = 0;
706         struct kprobe *old_p;
707
708         mutex_lock(&kprobe_mutex);
709         old_p = __get_valid_kprobe(p);
710         if (old_p)
711                 ret = -EINVAL;
712         mutex_unlock(&kprobe_mutex);
713         return ret;
714 }
715
716 int __kprobes register_kprobe(struct kprobe *p)
717 {
718         int ret = 0;
719         struct kprobe *old_p;
720         struct module *probed_mod;
721         kprobe_opcode_t *addr;
722
723         addr = kprobe_addr(p);
724         if (!addr)
725                 return -EINVAL;
726         p->addr = addr;
727
728         ret = check_kprobe_rereg(p);
729         if (ret)
730                 return ret;
731
732         preempt_disable();
733         if (!kernel_text_address((unsigned long) p->addr) ||
734             in_kprobes_functions((unsigned long) p->addr) ||
735             ftrace_text_reserved(p->addr, p->addr)) {
736                 preempt_enable();
737                 return -EINVAL;
738         }
739
740         /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
741         p->flags &= KPROBE_FLAG_DISABLED;
742
743         /*
744          * Check if are we probing a module.
745          */
746         probed_mod = __module_text_address((unsigned long) p->addr);
747         if (probed_mod) {
748                 /*
749                  * We must hold a refcount of the probed module while updating
750                  * its code to prohibit unexpected unloading.
751                  */
752                 if (unlikely(!try_module_get(probed_mod))) {
753                         preempt_enable();
754                         return -EINVAL;
755                 }
756                 /*
757                  * If the module freed .init.text, we couldn't insert
758                  * kprobes in there.
759                  */
760                 if (within_module_init((unsigned long)p->addr, probed_mod) &&
761                     probed_mod->state != MODULE_STATE_COMING) {
762                         module_put(probed_mod);
763                         preempt_enable();
764                         return -EINVAL;
765                 }
766         }
767         preempt_enable();
768
769         p->nmissed = 0;
770         INIT_LIST_HEAD(&p->list);
771         mutex_lock(&kprobe_mutex);
772         old_p = get_kprobe(p->addr);
773         if (old_p) {
774                 ret = register_aggr_kprobe(old_p, p);
775                 goto out;
776         }
777
778         mutex_lock(&text_mutex);
779         ret = arch_prepare_kprobe(p);
780         if (ret)
781                 goto out_unlock_text;
782
783         INIT_HLIST_NODE(&p->hlist);
784         hlist_add_head_rcu(&p->hlist,
785                        &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
786
787         if (!kprobes_all_disarmed && !kprobe_disabled(p))
788                 arch_arm_kprobe(p);
789
790 out_unlock_text:
791         mutex_unlock(&text_mutex);
792 out:
793         mutex_unlock(&kprobe_mutex);
794
795         if (probed_mod)
796                 module_put(probed_mod);
797
798         return ret;
799 }
800 EXPORT_SYMBOL_GPL(register_kprobe);
801
802 /*
803  * Unregister a kprobe without a scheduler synchronization.
804  */
805 static int __kprobes __unregister_kprobe_top(struct kprobe *p)
806 {
807         struct kprobe *old_p, *list_p;
808
809         old_p = __get_valid_kprobe(p);
810         if (old_p == NULL)
811                 return -EINVAL;
812
813         if (old_p == p ||
814             (old_p->pre_handler == aggr_pre_handler &&
815              list_is_singular(&old_p->list))) {
816                 /*
817                  * Only probe on the hash list. Disarm only if kprobes are
818                  * enabled and not gone - otherwise, the breakpoint would
819                  * already have been removed. We save on flushing icache.
820                  */
821                 if (!kprobes_all_disarmed && !kprobe_disabled(old_p))
822                         disarm_kprobe(p);
823                 hlist_del_rcu(&old_p->hlist);
824         } else {
825                 if (p->break_handler && !kprobe_gone(p))
826                         old_p->break_handler = NULL;
827                 if (p->post_handler && !kprobe_gone(p)) {
828                         list_for_each_entry_rcu(list_p, &old_p->list, list) {
829                                 if ((list_p != p) && (list_p->post_handler))
830                                         goto noclean;
831                         }
832                         old_p->post_handler = NULL;
833                 }
834 noclean:
835                 list_del_rcu(&p->list);
836                 if (!kprobe_disabled(old_p)) {
837                         try_to_disable_aggr_kprobe(old_p);
838                         if (!kprobes_all_disarmed && kprobe_disabled(old_p))
839                                 disarm_kprobe(old_p);
840                 }
841         }
842         return 0;
843 }
844
845 static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
846 {
847         struct kprobe *old_p;
848
849         if (list_empty(&p->list))
850                 arch_remove_kprobe(p);
851         else if (list_is_singular(&p->list)) {
852                 /* "p" is the last child of an aggr_kprobe */
853                 old_p = list_entry(p->list.next, struct kprobe, list);
854                 list_del(&p->list);
855                 arch_remove_kprobe(old_p);
856                 kfree(old_p);
857         }
858 }
859
860 int __kprobes register_kprobes(struct kprobe **kps, int num)
861 {
862         int i, ret = 0;
863
864         if (num <= 0)
865                 return -EINVAL;
866         for (i = 0; i < num; i++) {
867                 ret = register_kprobe(kps[i]);
868                 if (ret < 0) {
869                         if (i > 0)
870                                 unregister_kprobes(kps, i);
871                         break;
872                 }
873         }
874         return ret;
875 }
876 EXPORT_SYMBOL_GPL(register_kprobes);
877
878 void __kprobes unregister_kprobe(struct kprobe *p)
879 {
880         unregister_kprobes(&p, 1);
881 }
882 EXPORT_SYMBOL_GPL(unregister_kprobe);
883
884 void __kprobes unregister_kprobes(struct kprobe **kps, int num)
885 {
886         int i;
887
888         if (num <= 0)
889                 return;
890         mutex_lock(&kprobe_mutex);
891         for (i = 0; i < num; i++)
892                 if (__unregister_kprobe_top(kps[i]) < 0)
893                         kps[i]->addr = NULL;
894         mutex_unlock(&kprobe_mutex);
895
896         synchronize_sched();
897         for (i = 0; i < num; i++)
898                 if (kps[i]->addr)
899                         __unregister_kprobe_bottom(kps[i]);
900 }
901 EXPORT_SYMBOL_GPL(unregister_kprobes);
902
903 static struct notifier_block kprobe_exceptions_nb = {
904         .notifier_call = kprobe_exceptions_notify,
905         .priority = 0x7fffffff /* we need to be notified first */
906 };
907
908 unsigned long __weak arch_deref_entry_point(void *entry)
909 {
910         return (unsigned long)entry;
911 }
912
913 int __kprobes register_jprobes(struct jprobe **jps, int num)
914 {
915         struct jprobe *jp;
916         int ret = 0, i;
917
918         if (num <= 0)
919                 return -EINVAL;
920         for (i = 0; i < num; i++) {
921                 unsigned long addr;
922                 jp = jps[i];
923                 addr = arch_deref_entry_point(jp->entry);
924
925                 if (!kernel_text_address(addr))
926                         ret = -EINVAL;
927                 else {
928                         /* Todo: Verify probepoint is a function entry point */
929                         jp->kp.pre_handler = setjmp_pre_handler;
930                         jp->kp.break_handler = longjmp_break_handler;
931                         ret = register_kprobe(&jp->kp);
932                 }
933                 if (ret < 0) {
934                         if (i > 0)
935                                 unregister_jprobes(jps, i);
936                         break;
937                 }
938         }
939         return ret;
940 }
941 EXPORT_SYMBOL_GPL(register_jprobes);
942
943 int __kprobes register_jprobe(struct jprobe *jp)
944 {
945         return register_jprobes(&jp, 1);
946 }
947 EXPORT_SYMBOL_GPL(register_jprobe);
948
949 void __kprobes unregister_jprobe(struct jprobe *jp)
950 {
951         unregister_jprobes(&jp, 1);
952 }
953 EXPORT_SYMBOL_GPL(unregister_jprobe);
954
955 void __kprobes unregister_jprobes(struct jprobe **jps, int num)
956 {
957         int i;
958
959         if (num <= 0)
960                 return;
961         mutex_lock(&kprobe_mutex);
962         for (i = 0; i < num; i++)
963                 if (__unregister_kprobe_top(&jps[i]->kp) < 0)
964                         jps[i]->kp.addr = NULL;
965         mutex_unlock(&kprobe_mutex);
966
967         synchronize_sched();
968         for (i = 0; i < num; i++) {
969                 if (jps[i]->kp.addr)
970                         __unregister_kprobe_bottom(&jps[i]->kp);
971         }
972 }
973 EXPORT_SYMBOL_GPL(unregister_jprobes);
974
975 #ifdef CONFIG_KRETPROBES
976 /*
977  * This kprobe pre_handler is registered with every kretprobe. When probe
978  * hits it will set up the return probe.
979  */
980 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
981                                            struct pt_regs *regs)
982 {
983         struct kretprobe *rp = container_of(p, struct kretprobe, kp);
984         unsigned long hash, flags = 0;
985         struct kretprobe_instance *ri;
986
987         /*TODO: consider to only swap the RA after the last pre_handler fired */
988         hash = hash_ptr(current, KPROBE_HASH_BITS);
989         spin_lock_irqsave(&rp->lock, flags);
990         if (!hlist_empty(&rp->free_instances)) {
991                 ri = hlist_entry(rp->free_instances.first,
992                                 struct kretprobe_instance, hlist);
993                 hlist_del(&ri->hlist);
994                 spin_unlock_irqrestore(&rp->lock, flags);
995
996                 ri->rp = rp;
997                 ri->task = current;
998
999                 if (rp->entry_handler && rp->entry_handler(ri, regs))
1000                         return 0;
1001
1002                 arch_prepare_kretprobe(ri, regs);
1003
1004                 /* XXX(hch): why is there no hlist_move_head? */
1005                 INIT_HLIST_NODE(&ri->hlist);
1006                 kretprobe_table_lock(hash, &flags);
1007                 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
1008                 kretprobe_table_unlock(hash, &flags);
1009         } else {
1010                 rp->nmissed++;
1011                 spin_unlock_irqrestore(&rp->lock, flags);
1012         }
1013         return 0;
1014 }
1015
1016 int __kprobes register_kretprobe(struct kretprobe *rp)
1017 {
1018         int ret = 0;
1019         struct kretprobe_instance *inst;
1020         int i;
1021         void *addr;
1022
1023         if (kretprobe_blacklist_size) {
1024                 addr = kprobe_addr(&rp->kp);
1025                 if (!addr)
1026                         return -EINVAL;
1027
1028                 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1029                         if (kretprobe_blacklist[i].addr == addr)
1030                                 return -EINVAL;
1031                 }
1032         }
1033
1034         rp->kp.pre_handler = pre_handler_kretprobe;
1035         rp->kp.post_handler = NULL;
1036         rp->kp.fault_handler = NULL;
1037         rp->kp.break_handler = NULL;
1038
1039         /* Pre-allocate memory for max kretprobe instances */
1040         if (rp->maxactive <= 0) {
1041 #ifdef CONFIG_PREEMPT
1042                 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
1043 #else
1044                 rp->maxactive = num_possible_cpus();
1045 #endif
1046         }
1047         spin_lock_init(&rp->lock);
1048         INIT_HLIST_HEAD(&rp->free_instances);
1049         for (i = 0; i < rp->maxactive; i++) {
1050                 inst = kmalloc(sizeof(struct kretprobe_instance) +
1051                                rp->data_size, GFP_KERNEL);
1052                 if (inst == NULL) {
1053                         free_rp_inst(rp);
1054                         return -ENOMEM;
1055                 }
1056                 INIT_HLIST_NODE(&inst->hlist);
1057                 hlist_add_head(&inst->hlist, &rp->free_instances);
1058         }
1059
1060         rp->nmissed = 0;
1061         /* Establish function entry probe point */
1062         ret = register_kprobe(&rp->kp);
1063         if (ret != 0)
1064                 free_rp_inst(rp);
1065         return ret;
1066 }
1067 EXPORT_SYMBOL_GPL(register_kretprobe);
1068
1069 int __kprobes register_kretprobes(struct kretprobe **rps, int num)
1070 {
1071         int ret = 0, i;
1072
1073         if (num <= 0)
1074                 return -EINVAL;
1075         for (i = 0; i < num; i++) {
1076                 ret = register_kretprobe(rps[i]);
1077                 if (ret < 0) {
1078                         if (i > 0)
1079                                 unregister_kretprobes(rps, i);
1080                         break;
1081                 }
1082         }
1083         return ret;
1084 }
1085 EXPORT_SYMBOL_GPL(register_kretprobes);
1086
1087 void __kprobes unregister_kretprobe(struct kretprobe *rp)
1088 {
1089         unregister_kretprobes(&rp, 1);
1090 }
1091 EXPORT_SYMBOL_GPL(unregister_kretprobe);
1092
1093 void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1094 {
1095         int i;
1096
1097         if (num <= 0)
1098                 return;
1099         mutex_lock(&kprobe_mutex);
1100         for (i = 0; i < num; i++)
1101                 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
1102                         rps[i]->kp.addr = NULL;
1103         mutex_unlock(&kprobe_mutex);
1104
1105         synchronize_sched();
1106         for (i = 0; i < num; i++) {
1107                 if (rps[i]->kp.addr) {
1108                         __unregister_kprobe_bottom(&rps[i]->kp);
1109                         cleanup_rp_inst(rps[i]);
1110                 }
1111         }
1112 }
1113 EXPORT_SYMBOL_GPL(unregister_kretprobes);
1114
1115 #else /* CONFIG_KRETPROBES */
1116 int __kprobes register_kretprobe(struct kretprobe *rp)
1117 {
1118         return -ENOSYS;
1119 }
1120 EXPORT_SYMBOL_GPL(register_kretprobe);
1121
1122 int __kprobes register_kretprobes(struct kretprobe **rps, int num)
1123 {
1124         return -ENOSYS;
1125 }
1126 EXPORT_SYMBOL_GPL(register_kretprobes);
1127
1128 void __kprobes unregister_kretprobe(struct kretprobe *rp)
1129 {
1130 }
1131 EXPORT_SYMBOL_GPL(unregister_kretprobe);
1132
1133 void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1134 {
1135 }
1136 EXPORT_SYMBOL_GPL(unregister_kretprobes);
1137
1138 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
1139                                            struct pt_regs *regs)
1140 {
1141         return 0;
1142 }
1143
1144 #endif /* CONFIG_KRETPROBES */
1145
1146 /* Set the kprobe gone and remove its instruction buffer. */
1147 static void __kprobes kill_kprobe(struct kprobe *p)
1148 {
1149         struct kprobe *kp;
1150
1151         p->flags |= KPROBE_FLAG_GONE;
1152         if (p->pre_handler == aggr_pre_handler) {
1153                 /*
1154                  * If this is an aggr_kprobe, we have to list all the
1155                  * chained probes and mark them GONE.
1156                  */
1157                 list_for_each_entry_rcu(kp, &p->list, list)
1158                         kp->flags |= KPROBE_FLAG_GONE;
1159                 p->post_handler = NULL;
1160                 p->break_handler = NULL;
1161         }
1162         /*
1163          * Here, we can remove insn_slot safely, because no thread calls
1164          * the original probed function (which will be freed soon) any more.
1165          */
1166         arch_remove_kprobe(p);
1167 }
1168
1169 void __kprobes dump_kprobe(struct kprobe *kp)
1170 {
1171         printk(KERN_WARNING "Dumping kprobe:\n");
1172         printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
1173                kp->symbol_name, kp->addr, kp->offset);
1174 }
1175
1176 /* Module notifier call back, checking kprobes on the module */
1177 static int __kprobes kprobes_module_callback(struct notifier_block *nb,
1178                                              unsigned long val, void *data)
1179 {
1180         struct module *mod = data;
1181         struct hlist_head *head;
1182         struct hlist_node *node;
1183         struct kprobe *p;
1184         unsigned int i;
1185         int checkcore = (val == MODULE_STATE_GOING);
1186
1187         if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
1188                 return NOTIFY_DONE;
1189
1190         /*
1191          * When MODULE_STATE_GOING was notified, both of module .text and
1192          * .init.text sections would be freed. When MODULE_STATE_LIVE was
1193          * notified, only .init.text section would be freed. We need to
1194          * disable kprobes which have been inserted in the sections.
1195          */
1196         mutex_lock(&kprobe_mutex);
1197         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1198                 head = &kprobe_table[i];
1199                 hlist_for_each_entry_rcu(p, node, head, hlist)
1200                         if (within_module_init((unsigned long)p->addr, mod) ||
1201                             (checkcore &&
1202                              within_module_core((unsigned long)p->addr, mod))) {
1203                                 /*
1204                                  * The vaddr this probe is installed will soon
1205                                  * be vfreed buy not synced to disk. Hence,
1206                                  * disarming the breakpoint isn't needed.
1207                                  */
1208                                 kill_kprobe(p);
1209                         }
1210         }
1211         mutex_unlock(&kprobe_mutex);
1212         return NOTIFY_DONE;
1213 }
1214
1215 static struct notifier_block kprobe_module_nb = {
1216         .notifier_call = kprobes_module_callback,
1217         .priority = 0
1218 };
1219
1220 static int __init init_kprobes(void)
1221 {
1222         int i, err = 0;
1223         unsigned long offset = 0, size = 0;
1224         char *modname, namebuf[128];
1225         const char *symbol_name;
1226         void *addr;
1227         struct kprobe_blackpoint *kb;
1228
1229         /* FIXME allocate the probe table, currently defined statically */
1230         /* initialize all list heads */
1231         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1232                 INIT_HLIST_HEAD(&kprobe_table[i]);
1233                 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
1234                 spin_lock_init(&(kretprobe_table_locks[i].lock));
1235         }
1236
1237         /*
1238          * Lookup and populate the kprobe_blacklist.
1239          *
1240          * Unlike the kretprobe blacklist, we'll need to determine
1241          * the range of addresses that belong to the said functions,
1242          * since a kprobe need not necessarily be at the beginning
1243          * of a function.
1244          */
1245         for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
1246                 kprobe_lookup_name(kb->name, addr);
1247                 if (!addr)
1248                         continue;
1249
1250                 kb->start_addr = (unsigned long)addr;
1251                 symbol_name = kallsyms_lookup(kb->start_addr,
1252                                 &size, &offset, &modname, namebuf);
1253                 if (!symbol_name)
1254                         kb->range = 0;
1255                 else
1256                         kb->range = size;
1257         }
1258
1259         if (kretprobe_blacklist_size) {
1260                 /* lookup the function address from its name */
1261                 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1262                         kprobe_lookup_name(kretprobe_blacklist[i].name,
1263                                            kretprobe_blacklist[i].addr);
1264                         if (!kretprobe_blacklist[i].addr)
1265                                 printk("kretprobe: lookup failed: %s\n",
1266                                        kretprobe_blacklist[i].name);
1267                 }
1268         }
1269
1270         /* By default, kprobes are armed */
1271         kprobes_all_disarmed = false;
1272
1273         err = arch_init_kprobes();
1274         if (!err)
1275                 err = register_die_notifier(&kprobe_exceptions_nb);
1276         if (!err)
1277                 err = register_module_notifier(&kprobe_module_nb);
1278
1279         kprobes_initialized = (err == 0);
1280
1281         if (!err)
1282                 init_test_probes();
1283         return err;
1284 }
1285
1286 #ifdef CONFIG_DEBUG_FS
1287 static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
1288                 const char *sym, int offset,char *modname)
1289 {
1290         char *kprobe_type;
1291
1292         if (p->pre_handler == pre_handler_kretprobe)
1293                 kprobe_type = "r";
1294         else if (p->pre_handler == setjmp_pre_handler)
1295                 kprobe_type = "j";
1296         else
1297                 kprobe_type = "k";
1298         if (sym)
1299                 seq_printf(pi, "%p  %s  %s+0x%x  %s %s%s\n",
1300                         p->addr, kprobe_type, sym, offset,
1301                         (modname ? modname : " "),
1302                         (kprobe_gone(p) ? "[GONE]" : ""),
1303                         ((kprobe_disabled(p) && !kprobe_gone(p)) ?
1304                          "[DISABLED]" : ""));
1305         else
1306                 seq_printf(pi, "%p  %s  %p %s%s\n",
1307                         p->addr, kprobe_type, p->addr,
1308                         (kprobe_gone(p) ? "[GONE]" : ""),
1309                         ((kprobe_disabled(p) && !kprobe_gone(p)) ?
1310                          "[DISABLED]" : ""));
1311 }
1312
1313 static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
1314 {
1315         return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
1316 }
1317
1318 static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
1319 {
1320         (*pos)++;
1321         if (*pos >= KPROBE_TABLE_SIZE)
1322                 return NULL;
1323         return pos;
1324 }
1325
1326 static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
1327 {
1328         /* Nothing to do */
1329 }
1330
1331 static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
1332 {
1333         struct hlist_head *head;
1334         struct hlist_node *node;
1335         struct kprobe *p, *kp;
1336         const char *sym = NULL;
1337         unsigned int i = *(loff_t *) v;
1338         unsigned long offset = 0;
1339         char *modname, namebuf[128];
1340
1341         head = &kprobe_table[i];
1342         preempt_disable();
1343         hlist_for_each_entry_rcu(p, node, head, hlist) {
1344                 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
1345                                         &offset, &modname, namebuf);
1346                 if (p->pre_handler == aggr_pre_handler) {
1347                         list_for_each_entry_rcu(kp, &p->list, list)
1348                                 report_probe(pi, kp, sym, offset, modname);
1349                 } else
1350                         report_probe(pi, p, sym, offset, modname);
1351         }
1352         preempt_enable();
1353         return 0;
1354 }
1355
1356 static const struct seq_operations kprobes_seq_ops = {
1357         .start = kprobe_seq_start,
1358         .next  = kprobe_seq_next,
1359         .stop  = kprobe_seq_stop,
1360         .show  = show_kprobe_addr
1361 };
1362
1363 static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
1364 {
1365         return seq_open(filp, &kprobes_seq_ops);
1366 }
1367
1368 static const struct file_operations debugfs_kprobes_operations = {
1369         .open           = kprobes_open,
1370         .read           = seq_read,
1371         .llseek         = seq_lseek,
1372         .release        = seq_release,
1373 };
1374
1375 /* Disable one kprobe */
1376 int __kprobes disable_kprobe(struct kprobe *kp)
1377 {
1378         int ret = 0;
1379         struct kprobe *p;
1380
1381         mutex_lock(&kprobe_mutex);
1382
1383         /* Check whether specified probe is valid. */
1384         p = __get_valid_kprobe(kp);
1385         if (unlikely(p == NULL)) {
1386                 ret = -EINVAL;
1387                 goto out;
1388         }
1389
1390         /* If the probe is already disabled (or gone), just return */
1391         if (kprobe_disabled(kp))
1392                 goto out;
1393
1394         kp->flags |= KPROBE_FLAG_DISABLED;
1395         if (p != kp)
1396                 /* When kp != p, p is always enabled. */
1397                 try_to_disable_aggr_kprobe(p);
1398
1399         if (!kprobes_all_disarmed && kprobe_disabled(p))
1400                 disarm_kprobe(p);
1401 out:
1402         mutex_unlock(&kprobe_mutex);
1403         return ret;
1404 }
1405 EXPORT_SYMBOL_GPL(disable_kprobe);
1406
1407 /* Enable one kprobe */
1408 int __kprobes enable_kprobe(struct kprobe *kp)
1409 {
1410         int ret = 0;
1411         struct kprobe *p;
1412
1413         mutex_lock(&kprobe_mutex);
1414
1415         /* Check whether specified probe is valid. */
1416         p = __get_valid_kprobe(kp);
1417         if (unlikely(p == NULL)) {
1418                 ret = -EINVAL;
1419                 goto out;
1420         }
1421
1422         if (kprobe_gone(kp)) {
1423                 /* This kprobe has gone, we couldn't enable it. */
1424                 ret = -EINVAL;
1425                 goto out;
1426         }
1427
1428         if (!kprobes_all_disarmed && kprobe_disabled(p))
1429                 arm_kprobe(p);
1430
1431         p->flags &= ~KPROBE_FLAG_DISABLED;
1432         if (p != kp)
1433                 kp->flags &= ~KPROBE_FLAG_DISABLED;
1434 out:
1435         mutex_unlock(&kprobe_mutex);
1436         return ret;
1437 }
1438 EXPORT_SYMBOL_GPL(enable_kprobe);
1439
1440 static void __kprobes arm_all_kprobes(void)
1441 {
1442         struct hlist_head *head;
1443         struct hlist_node *node;
1444         struct kprobe *p;
1445         unsigned int i;
1446
1447         mutex_lock(&kprobe_mutex);
1448
1449         /* If kprobes are armed, just return */
1450         if (!kprobes_all_disarmed)
1451                 goto already_enabled;
1452
1453         mutex_lock(&text_mutex);
1454         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1455                 head = &kprobe_table[i];
1456                 hlist_for_each_entry_rcu(p, node, head, hlist)
1457                         if (!kprobe_disabled(p))
1458                                 arch_arm_kprobe(p);
1459         }
1460         mutex_unlock(&text_mutex);
1461
1462         kprobes_all_disarmed = false;
1463         printk(KERN_INFO "Kprobes globally enabled\n");
1464
1465 already_enabled:
1466         mutex_unlock(&kprobe_mutex);
1467         return;
1468 }
1469
1470 static void __kprobes disarm_all_kprobes(void)
1471 {
1472         struct hlist_head *head;
1473         struct hlist_node *node;
1474         struct kprobe *p;
1475         unsigned int i;
1476
1477         mutex_lock(&kprobe_mutex);
1478
1479         /* If kprobes are already disarmed, just return */
1480         if (kprobes_all_disarmed)
1481                 goto already_disabled;
1482
1483         kprobes_all_disarmed = true;
1484         printk(KERN_INFO "Kprobes globally disabled\n");
1485         mutex_lock(&text_mutex);
1486         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1487                 head = &kprobe_table[i];
1488                 hlist_for_each_entry_rcu(p, node, head, hlist) {
1489                         if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
1490                                 arch_disarm_kprobe(p);
1491                 }
1492         }
1493
1494         mutex_unlock(&text_mutex);
1495         mutex_unlock(&kprobe_mutex);
1496         /* Allow all currently running kprobes to complete */
1497         synchronize_sched();
1498         return;
1499
1500 already_disabled:
1501         mutex_unlock(&kprobe_mutex);
1502         return;
1503 }
1504
1505 /*
1506  * XXX: The debugfs bool file interface doesn't allow for callbacks
1507  * when the bool state is switched. We can reuse that facility when
1508  * available
1509  */
1510 static ssize_t read_enabled_file_bool(struct file *file,
1511                char __user *user_buf, size_t count, loff_t *ppos)
1512 {
1513         char buf[3];
1514
1515         if (!kprobes_all_disarmed)
1516                 buf[0] = '1';
1517         else
1518                 buf[0] = '0';
1519         buf[1] = '\n';
1520         buf[2] = 0x00;
1521         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1522 }
1523
1524 static ssize_t write_enabled_file_bool(struct file *file,
1525                const char __user *user_buf, size_t count, loff_t *ppos)
1526 {
1527         char buf[32];
1528         int buf_size;
1529
1530         buf_size = min(count, (sizeof(buf)-1));
1531         if (copy_from_user(buf, user_buf, buf_size))
1532                 return -EFAULT;
1533
1534         switch (buf[0]) {
1535         case 'y':
1536         case 'Y':
1537         case '1':
1538                 arm_all_kprobes();
1539                 break;
1540         case 'n':
1541         case 'N':
1542         case '0':
1543                 disarm_all_kprobes();
1544                 break;
1545         }
1546
1547         return count;
1548 }
1549
1550 static const struct file_operations fops_kp = {
1551         .read =         read_enabled_file_bool,
1552         .write =        write_enabled_file_bool,
1553 };
1554
1555 static int __kprobes debugfs_kprobe_init(void)
1556 {
1557         struct dentry *dir, *file;
1558         unsigned int value = 1;
1559
1560         dir = debugfs_create_dir("kprobes", NULL);
1561         if (!dir)
1562                 return -ENOMEM;
1563
1564         file = debugfs_create_file("list", 0444, dir, NULL,
1565                                 &debugfs_kprobes_operations);
1566         if (!file) {
1567                 debugfs_remove(dir);
1568                 return -ENOMEM;
1569         }
1570
1571         file = debugfs_create_file("enabled", 0600, dir,
1572                                         &value, &fops_kp);
1573         if (!file) {
1574                 debugfs_remove(dir);
1575                 return -ENOMEM;
1576         }
1577
1578         return 0;
1579 }
1580
1581 late_initcall(debugfs_kprobe_init);
1582 #endif /* CONFIG_DEBUG_FS */
1583
1584 module_init(init_kprobes);
1585
1586 /* defined in arch/.../kernel/kprobes.c */
1587 EXPORT_SYMBOL_GPL(jprobe_return);