thinkpad-acpi: improve Kconfig help text
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / kprobes.c
blobb96da68e770bb8783f945aaaaa2271518d3e4c40
1 /*
2 * Kernel Probes (KProbes)
3 * kernel/kprobes.c
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.
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.
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.
19 * Copyright (C) IBM Corporation, 2002, 2004
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.
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>
47 #include <asm-generic/sections.h>
48 #include <asm/cacheflush.h>
49 #include <asm/errno.h>
50 #include <asm/uaccess.h>
52 #define KPROBE_HASH_BITS 6
53 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
57 * Some oddball architectures like 64bit powerpc have function descriptors
58 * so this must be overridable.
60 #ifndef kprobe_lookup_name
61 #define kprobe_lookup_name(name, addr) \
62 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
63 #endif
65 static int kprobes_initialized;
66 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
67 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
69 /* NOTE: change this value only with kprobe_mutex held */
70 static bool kprobe_enabled;
72 DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
73 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
74 static struct {
75 spinlock_t lock ____cacheline_aligned_in_smp;
76 } kretprobe_table_locks[KPROBE_TABLE_SIZE];
78 static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
80 return &(kretprobe_table_locks[hash].lock);
84 * Normally, functions that we'd want to prohibit kprobes in, are marked
85 * __kprobes. But, there are cases where such functions already belong to
86 * a different section (__sched for preempt_schedule)
88 * For such cases, we now have a blacklist
90 static struct kprobe_blackpoint kprobe_blacklist[] = {
91 {"preempt_schedule",},
92 {NULL} /* Terminator */
95 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
97 * kprobe->ainsn.insn points to the copy of the instruction to be
98 * single-stepped. x86_64, POWER4 and above have no-exec support and
99 * stepping on the instruction on a vmalloced/kmalloced/data page
100 * is a recipe for disaster
102 #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
104 struct kprobe_insn_page {
105 struct hlist_node hlist;
106 kprobe_opcode_t *insns; /* Page of instruction slots */
107 char slot_used[INSNS_PER_PAGE];
108 int nused;
109 int ngarbage;
112 enum kprobe_slot_state {
113 SLOT_CLEAN = 0,
114 SLOT_DIRTY = 1,
115 SLOT_USED = 2,
118 static struct hlist_head kprobe_insn_pages;
119 static int kprobe_garbage_slots;
120 static int collect_garbage_slots(void);
122 static int __kprobes check_safety(void)
124 int ret = 0;
125 #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
126 ret = freeze_processes();
127 if (ret == 0) {
128 struct task_struct *p, *q;
129 do_each_thread(p, q) {
130 if (p != current && p->state == TASK_RUNNING &&
131 p->pid != 0) {
132 printk("Check failed: %s is running\n",p->comm);
133 ret = -1;
134 goto loop_end;
136 } while_each_thread(p, q);
138 loop_end:
139 thaw_processes();
140 #else
141 synchronize_sched();
142 #endif
143 return ret;
147 * get_insn_slot() - Find a slot on an executable page for an instruction.
148 * We allocate an executable page if there's no room on existing ones.
150 kprobe_opcode_t __kprobes *get_insn_slot(void)
152 struct kprobe_insn_page *kip;
153 struct hlist_node *pos;
155 retry:
156 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
157 if (kip->nused < INSNS_PER_PAGE) {
158 int i;
159 for (i = 0; i < INSNS_PER_PAGE; i++) {
160 if (kip->slot_used[i] == SLOT_CLEAN) {
161 kip->slot_used[i] = SLOT_USED;
162 kip->nused++;
163 return kip->insns + (i * MAX_INSN_SIZE);
166 /* Surprise! No unused slots. Fix kip->nused. */
167 kip->nused = INSNS_PER_PAGE;
171 /* If there are any garbage slots, collect it and try again. */
172 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
173 goto retry;
175 /* All out of space. Need to allocate a new page. Use slot 0. */
176 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
177 if (!kip)
178 return NULL;
181 * Use module_alloc so this page is within +/- 2GB of where the
182 * kernel image and loaded module images reside. This is required
183 * so x86_64 can correctly handle the %rip-relative fixups.
185 kip->insns = module_alloc(PAGE_SIZE);
186 if (!kip->insns) {
187 kfree(kip);
188 return NULL;
190 INIT_HLIST_NODE(&kip->hlist);
191 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
192 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
193 kip->slot_used[0] = SLOT_USED;
194 kip->nused = 1;
195 kip->ngarbage = 0;
196 return kip->insns;
199 /* Return 1 if all garbages are collected, otherwise 0. */
200 static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
202 kip->slot_used[idx] = SLOT_CLEAN;
203 kip->nused--;
204 if (kip->nused == 0) {
206 * Page is no longer in use. Free it unless
207 * it's the last one. We keep the last one
208 * so as not to have to set it up again the
209 * next time somebody inserts a probe.
211 hlist_del(&kip->hlist);
212 if (hlist_empty(&kprobe_insn_pages)) {
213 INIT_HLIST_NODE(&kip->hlist);
214 hlist_add_head(&kip->hlist,
215 &kprobe_insn_pages);
216 } else {
217 module_free(NULL, kip->insns);
218 kfree(kip);
220 return 1;
222 return 0;
225 static int __kprobes collect_garbage_slots(void)
227 struct kprobe_insn_page *kip;
228 struct hlist_node *pos, *next;
230 /* Ensure no-one is preepmted on the garbages */
231 if (check_safety() != 0)
232 return -EAGAIN;
234 hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
235 int i;
236 if (kip->ngarbage == 0)
237 continue;
238 kip->ngarbage = 0; /* we will collect all garbages */
239 for (i = 0; i < INSNS_PER_PAGE; i++) {
240 if (kip->slot_used[i] == SLOT_DIRTY &&
241 collect_one_slot(kip, i))
242 break;
245 kprobe_garbage_slots = 0;
246 return 0;
249 void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
251 struct kprobe_insn_page *kip;
252 struct hlist_node *pos;
254 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
255 if (kip->insns <= slot &&
256 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
257 int i = (slot - kip->insns) / MAX_INSN_SIZE;
258 if (dirty) {
259 kip->slot_used[i] = SLOT_DIRTY;
260 kip->ngarbage++;
261 } else {
262 collect_one_slot(kip, i);
264 break;
268 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
269 collect_garbage_slots();
271 #endif
273 /* We have preemption disabled.. so it is safe to use __ versions */
274 static inline void set_kprobe_instance(struct kprobe *kp)
276 __get_cpu_var(kprobe_instance) = kp;
279 static inline void reset_kprobe_instance(void)
281 __get_cpu_var(kprobe_instance) = NULL;
285 * This routine is called either:
286 * - under the kprobe_mutex - during kprobe_[un]register()
287 * OR
288 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
290 struct kprobe __kprobes *get_kprobe(void *addr)
292 struct hlist_head *head;
293 struct hlist_node *node;
294 struct kprobe *p;
296 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
297 hlist_for_each_entry_rcu(p, node, head, hlist) {
298 if (p->addr == addr)
299 return p;
301 return NULL;
305 * Aggregate handlers for multiple kprobes support - these handlers
306 * take care of invoking the individual kprobe handlers on p->list
308 static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
310 struct kprobe *kp;
312 list_for_each_entry_rcu(kp, &p->list, list) {
313 if (kp->pre_handler) {
314 set_kprobe_instance(kp);
315 if (kp->pre_handler(kp, regs))
316 return 1;
318 reset_kprobe_instance();
320 return 0;
323 static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
324 unsigned long flags)
326 struct kprobe *kp;
328 list_for_each_entry_rcu(kp, &p->list, list) {
329 if (kp->post_handler) {
330 set_kprobe_instance(kp);
331 kp->post_handler(kp, regs, flags);
332 reset_kprobe_instance();
337 static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
338 int trapnr)
340 struct kprobe *cur = __get_cpu_var(kprobe_instance);
343 * if we faulted "during" the execution of a user specified
344 * probe handler, invoke just that probe's fault handler
346 if (cur && cur->fault_handler) {
347 if (cur->fault_handler(cur, regs, trapnr))
348 return 1;
350 return 0;
353 static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
355 struct kprobe *cur = __get_cpu_var(kprobe_instance);
356 int ret = 0;
358 if (cur && cur->break_handler) {
359 if (cur->break_handler(cur, regs))
360 ret = 1;
362 reset_kprobe_instance();
363 return ret;
366 /* Walks the list and increments nmissed count for multiprobe case */
367 void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
369 struct kprobe *kp;
370 if (p->pre_handler != aggr_pre_handler) {
371 p->nmissed++;
372 } else {
373 list_for_each_entry_rcu(kp, &p->list, list)
374 kp->nmissed++;
376 return;
379 void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
380 struct hlist_head *head)
382 struct kretprobe *rp = ri->rp;
384 /* remove rp inst off the rprobe_inst_table */
385 hlist_del(&ri->hlist);
386 INIT_HLIST_NODE(&ri->hlist);
387 if (likely(rp)) {
388 spin_lock(&rp->lock);
389 hlist_add_head(&ri->hlist, &rp->free_instances);
390 spin_unlock(&rp->lock);
391 } else
392 /* Unregistering */
393 hlist_add_head(&ri->hlist, head);
396 void kretprobe_hash_lock(struct task_struct *tsk,
397 struct hlist_head **head, unsigned long *flags)
399 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
400 spinlock_t *hlist_lock;
402 *head = &kretprobe_inst_table[hash];
403 hlist_lock = kretprobe_table_lock_ptr(hash);
404 spin_lock_irqsave(hlist_lock, *flags);
407 static void kretprobe_table_lock(unsigned long hash, unsigned long *flags)
409 spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
410 spin_lock_irqsave(hlist_lock, *flags);
413 void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags)
415 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
416 spinlock_t *hlist_lock;
418 hlist_lock = kretprobe_table_lock_ptr(hash);
419 spin_unlock_irqrestore(hlist_lock, *flags);
422 void kretprobe_table_unlock(unsigned long hash, unsigned long *flags)
424 spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
425 spin_unlock_irqrestore(hlist_lock, *flags);
429 * This function is called from finish_task_switch when task tk becomes dead,
430 * so that we can recycle any function-return probe instances associated
431 * with this task. These left over instances represent probed functions
432 * that have been called but will never return.
434 void __kprobes kprobe_flush_task(struct task_struct *tk)
436 struct kretprobe_instance *ri;
437 struct hlist_head *head, empty_rp;
438 struct hlist_node *node, *tmp;
439 unsigned long hash, flags = 0;
441 if (unlikely(!kprobes_initialized))
442 /* Early boot. kretprobe_table_locks not yet initialized. */
443 return;
445 hash = hash_ptr(tk, KPROBE_HASH_BITS);
446 head = &kretprobe_inst_table[hash];
447 kretprobe_table_lock(hash, &flags);
448 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
449 if (ri->task == tk)
450 recycle_rp_inst(ri, &empty_rp);
452 kretprobe_table_unlock(hash, &flags);
453 INIT_HLIST_HEAD(&empty_rp);
454 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
455 hlist_del(&ri->hlist);
456 kfree(ri);
460 static inline void free_rp_inst(struct kretprobe *rp)
462 struct kretprobe_instance *ri;
463 struct hlist_node *pos, *next;
465 hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
466 hlist_del(&ri->hlist);
467 kfree(ri);
471 static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
473 unsigned long flags, hash;
474 struct kretprobe_instance *ri;
475 struct hlist_node *pos, *next;
476 struct hlist_head *head;
478 /* No race here */
479 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
480 kretprobe_table_lock(hash, &flags);
481 head = &kretprobe_inst_table[hash];
482 hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
483 if (ri->rp == rp)
484 ri->rp = NULL;
486 kretprobe_table_unlock(hash, &flags);
488 free_rp_inst(rp);
492 * Keep all fields in the kprobe consistent
494 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
496 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
497 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
501 * Add the new probe to old_p->list. Fail if this is the
502 * second jprobe at the address - two jprobes can't coexist
504 static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
506 if (p->break_handler) {
507 if (old_p->break_handler)
508 return -EEXIST;
509 list_add_tail_rcu(&p->list, &old_p->list);
510 old_p->break_handler = aggr_break_handler;
511 } else
512 list_add_rcu(&p->list, &old_p->list);
513 if (p->post_handler && !old_p->post_handler)
514 old_p->post_handler = aggr_post_handler;
515 return 0;
519 * Fill in the required fields of the "manager kprobe". Replace the
520 * earlier kprobe in the hlist with the manager kprobe
522 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
524 copy_kprobe(p, ap);
525 flush_insn_slot(ap);
526 ap->addr = p->addr;
527 ap->pre_handler = aggr_pre_handler;
528 ap->fault_handler = aggr_fault_handler;
529 if (p->post_handler)
530 ap->post_handler = aggr_post_handler;
531 if (p->break_handler)
532 ap->break_handler = aggr_break_handler;
534 INIT_LIST_HEAD(&ap->list);
535 list_add_rcu(&p->list, &ap->list);
537 hlist_replace_rcu(&p->hlist, &ap->hlist);
541 * This is the second or subsequent kprobe at the address - handle
542 * the intricacies
544 static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
545 struct kprobe *p)
547 int ret = 0;
548 struct kprobe *ap;
550 if (old_p->pre_handler == aggr_pre_handler) {
551 copy_kprobe(old_p, p);
552 ret = add_new_kprobe(old_p, p);
553 } else {
554 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
555 if (!ap)
556 return -ENOMEM;
557 add_aggr_kprobe(ap, old_p);
558 copy_kprobe(ap, p);
559 ret = add_new_kprobe(ap, p);
561 return ret;
564 static int __kprobes in_kprobes_functions(unsigned long addr)
566 struct kprobe_blackpoint *kb;
568 if (addr >= (unsigned long)__kprobes_text_start &&
569 addr < (unsigned long)__kprobes_text_end)
570 return -EINVAL;
572 * If there exists a kprobe_blacklist, verify and
573 * fail any probe registration in the prohibited area
575 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
576 if (kb->start_addr) {
577 if (addr >= kb->start_addr &&
578 addr < (kb->start_addr + kb->range))
579 return -EINVAL;
582 return 0;
586 * If we have a symbol_name argument, look it up and add the offset field
587 * to it. This way, we can specify a relative address to a symbol.
589 static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
591 kprobe_opcode_t *addr = p->addr;
592 if (p->symbol_name) {
593 if (addr)
594 return NULL;
595 kprobe_lookup_name(p->symbol_name, addr);
598 if (!addr)
599 return NULL;
600 return (kprobe_opcode_t *)(((char *)addr) + p->offset);
603 static int __kprobes __register_kprobe(struct kprobe *p,
604 unsigned long called_from)
606 int ret = 0;
607 struct kprobe *old_p;
608 struct module *probed_mod;
609 kprobe_opcode_t *addr;
611 addr = kprobe_addr(p);
612 if (!addr)
613 return -EINVAL;
614 p->addr = addr;
616 preempt_disable();
617 if (!__kernel_text_address((unsigned long) p->addr) ||
618 in_kprobes_functions((unsigned long) p->addr)) {
619 preempt_enable();
620 return -EINVAL;
623 p->mod_refcounted = 0;
626 * Check if are we probing a module.
628 probed_mod = __module_text_address((unsigned long) p->addr);
629 if (probed_mod) {
630 struct module *calling_mod;
631 calling_mod = __module_text_address(called_from);
633 * We must allow modules to probe themself and in this case
634 * avoid incrementing the module refcount, so as to allow
635 * unloading of self probing modules.
637 if (calling_mod && calling_mod != probed_mod) {
638 if (unlikely(!try_module_get(probed_mod))) {
639 preempt_enable();
640 return -EINVAL;
642 p->mod_refcounted = 1;
643 } else
644 probed_mod = NULL;
646 preempt_enable();
648 p->nmissed = 0;
649 INIT_LIST_HEAD(&p->list);
650 mutex_lock(&kprobe_mutex);
651 old_p = get_kprobe(p->addr);
652 if (old_p) {
653 ret = register_aggr_kprobe(old_p, p);
654 goto out;
657 ret = arch_prepare_kprobe(p);
658 if (ret)
659 goto out;
661 INIT_HLIST_NODE(&p->hlist);
662 hlist_add_head_rcu(&p->hlist,
663 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
665 if (kprobe_enabled)
666 arch_arm_kprobe(p);
668 out:
669 mutex_unlock(&kprobe_mutex);
671 if (ret && probed_mod)
672 module_put(probed_mod);
673 return ret;
677 * Unregister a kprobe without a scheduler synchronization.
679 static int __kprobes __unregister_kprobe_top(struct kprobe *p)
681 struct kprobe *old_p, *list_p;
683 old_p = get_kprobe(p->addr);
684 if (unlikely(!old_p))
685 return -EINVAL;
687 if (p != old_p) {
688 list_for_each_entry_rcu(list_p, &old_p->list, list)
689 if (list_p == p)
690 /* kprobe p is a valid probe */
691 goto valid_p;
692 return -EINVAL;
694 valid_p:
695 if (old_p == p ||
696 (old_p->pre_handler == aggr_pre_handler &&
697 list_is_singular(&old_p->list))) {
699 * Only probe on the hash list. Disarm only if kprobes are
700 * enabled - otherwise, the breakpoint would already have
701 * been removed. We save on flushing icache.
703 if (kprobe_enabled)
704 arch_disarm_kprobe(p);
705 hlist_del_rcu(&old_p->hlist);
706 } else {
707 if (p->break_handler)
708 old_p->break_handler = NULL;
709 if (p->post_handler) {
710 list_for_each_entry_rcu(list_p, &old_p->list, list) {
711 if ((list_p != p) && (list_p->post_handler))
712 goto noclean;
714 old_p->post_handler = NULL;
716 noclean:
717 list_del_rcu(&p->list);
719 return 0;
722 static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
724 struct module *mod;
725 struct kprobe *old_p;
727 if (p->mod_refcounted) {
729 * Since we've already incremented refcount,
730 * we don't need to disable preemption.
732 mod = module_text_address((unsigned long)p->addr);
733 if (mod)
734 module_put(mod);
737 if (list_empty(&p->list) || list_is_singular(&p->list)) {
738 if (!list_empty(&p->list)) {
739 /* "p" is the last child of an aggr_kprobe */
740 old_p = list_entry(p->list.next, struct kprobe, list);
741 list_del(&p->list);
742 kfree(old_p);
744 arch_remove_kprobe(p);
748 static int __register_kprobes(struct kprobe **kps, int num,
749 unsigned long called_from)
751 int i, ret = 0;
753 if (num <= 0)
754 return -EINVAL;
755 for (i = 0; i < num; i++) {
756 ret = __register_kprobe(kps[i], called_from);
757 if (ret < 0) {
758 if (i > 0)
759 unregister_kprobes(kps, i);
760 break;
763 return ret;
767 * Registration and unregistration functions for kprobe.
769 int __kprobes register_kprobe(struct kprobe *p)
771 return __register_kprobes(&p, 1,
772 (unsigned long)__builtin_return_address(0));
775 void __kprobes unregister_kprobe(struct kprobe *p)
777 unregister_kprobes(&p, 1);
780 int __kprobes register_kprobes(struct kprobe **kps, int num)
782 return __register_kprobes(kps, num,
783 (unsigned long)__builtin_return_address(0));
786 void __kprobes unregister_kprobes(struct kprobe **kps, int num)
788 int i;
790 if (num <= 0)
791 return;
792 mutex_lock(&kprobe_mutex);
793 for (i = 0; i < num; i++)
794 if (__unregister_kprobe_top(kps[i]) < 0)
795 kps[i]->addr = NULL;
796 mutex_unlock(&kprobe_mutex);
798 synchronize_sched();
799 for (i = 0; i < num; i++)
800 if (kps[i]->addr)
801 __unregister_kprobe_bottom(kps[i]);
804 static struct notifier_block kprobe_exceptions_nb = {
805 .notifier_call = kprobe_exceptions_notify,
806 .priority = 0x7fffffff /* we need to be notified first */
809 unsigned long __weak arch_deref_entry_point(void *entry)
811 return (unsigned long)entry;
814 static int __register_jprobes(struct jprobe **jps, int num,
815 unsigned long called_from)
817 struct jprobe *jp;
818 int ret = 0, i;
820 if (num <= 0)
821 return -EINVAL;
822 for (i = 0; i < num; i++) {
823 unsigned long addr;
824 jp = jps[i];
825 addr = arch_deref_entry_point(jp->entry);
827 if (!kernel_text_address(addr))
828 ret = -EINVAL;
829 else {
830 /* Todo: Verify probepoint is a function entry point */
831 jp->kp.pre_handler = setjmp_pre_handler;
832 jp->kp.break_handler = longjmp_break_handler;
833 ret = __register_kprobe(&jp->kp, called_from);
835 if (ret < 0) {
836 if (i > 0)
837 unregister_jprobes(jps, i);
838 break;
841 return ret;
844 int __kprobes register_jprobe(struct jprobe *jp)
846 return __register_jprobes(&jp, 1,
847 (unsigned long)__builtin_return_address(0));
850 void __kprobes unregister_jprobe(struct jprobe *jp)
852 unregister_jprobes(&jp, 1);
855 int __kprobes register_jprobes(struct jprobe **jps, int num)
857 return __register_jprobes(jps, num,
858 (unsigned long)__builtin_return_address(0));
861 void __kprobes unregister_jprobes(struct jprobe **jps, int num)
863 int i;
865 if (num <= 0)
866 return;
867 mutex_lock(&kprobe_mutex);
868 for (i = 0; i < num; i++)
869 if (__unregister_kprobe_top(&jps[i]->kp) < 0)
870 jps[i]->kp.addr = NULL;
871 mutex_unlock(&kprobe_mutex);
873 synchronize_sched();
874 for (i = 0; i < num; i++) {
875 if (jps[i]->kp.addr)
876 __unregister_kprobe_bottom(&jps[i]->kp);
880 #ifdef CONFIG_KRETPROBES
882 * This kprobe pre_handler is registered with every kretprobe. When probe
883 * hits it will set up the return probe.
885 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
886 struct pt_regs *regs)
888 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
889 unsigned long hash, flags = 0;
890 struct kretprobe_instance *ri;
892 /*TODO: consider to only swap the RA after the last pre_handler fired */
893 hash = hash_ptr(current, KPROBE_HASH_BITS);
894 spin_lock_irqsave(&rp->lock, flags);
895 if (!hlist_empty(&rp->free_instances)) {
896 ri = hlist_entry(rp->free_instances.first,
897 struct kretprobe_instance, hlist);
898 hlist_del(&ri->hlist);
899 spin_unlock_irqrestore(&rp->lock, flags);
901 ri->rp = rp;
902 ri->task = current;
904 if (rp->entry_handler && rp->entry_handler(ri, regs))
905 return 0;
907 arch_prepare_kretprobe(ri, regs);
909 /* XXX(hch): why is there no hlist_move_head? */
910 INIT_HLIST_NODE(&ri->hlist);
911 kretprobe_table_lock(hash, &flags);
912 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
913 kretprobe_table_unlock(hash, &flags);
914 } else {
915 rp->nmissed++;
916 spin_unlock_irqrestore(&rp->lock, flags);
918 return 0;
921 static int __kprobes __register_kretprobe(struct kretprobe *rp,
922 unsigned long called_from)
924 int ret = 0;
925 struct kretprobe_instance *inst;
926 int i;
927 void *addr;
929 if (kretprobe_blacklist_size) {
930 addr = kprobe_addr(&rp->kp);
931 if (!addr)
932 return -EINVAL;
934 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
935 if (kretprobe_blacklist[i].addr == addr)
936 return -EINVAL;
940 rp->kp.pre_handler = pre_handler_kretprobe;
941 rp->kp.post_handler = NULL;
942 rp->kp.fault_handler = NULL;
943 rp->kp.break_handler = NULL;
945 /* Pre-allocate memory for max kretprobe instances */
946 if (rp->maxactive <= 0) {
947 #ifdef CONFIG_PREEMPT
948 rp->maxactive = max(10, 2 * NR_CPUS);
949 #else
950 rp->maxactive = NR_CPUS;
951 #endif
953 spin_lock_init(&rp->lock);
954 INIT_HLIST_HEAD(&rp->free_instances);
955 for (i = 0; i < rp->maxactive; i++) {
956 inst = kmalloc(sizeof(struct kretprobe_instance) +
957 rp->data_size, GFP_KERNEL);
958 if (inst == NULL) {
959 free_rp_inst(rp);
960 return -ENOMEM;
962 INIT_HLIST_NODE(&inst->hlist);
963 hlist_add_head(&inst->hlist, &rp->free_instances);
966 rp->nmissed = 0;
967 /* Establish function entry probe point */
968 ret = __register_kprobe(&rp->kp, called_from);
969 if (ret != 0)
970 free_rp_inst(rp);
971 return ret;
974 static int __register_kretprobes(struct kretprobe **rps, int num,
975 unsigned long called_from)
977 int ret = 0, i;
979 if (num <= 0)
980 return -EINVAL;
981 for (i = 0; i < num; i++) {
982 ret = __register_kretprobe(rps[i], called_from);
983 if (ret < 0) {
984 if (i > 0)
985 unregister_kretprobes(rps, i);
986 break;
989 return ret;
992 int __kprobes register_kretprobe(struct kretprobe *rp)
994 return __register_kretprobes(&rp, 1,
995 (unsigned long)__builtin_return_address(0));
998 void __kprobes unregister_kretprobe(struct kretprobe *rp)
1000 unregister_kretprobes(&rp, 1);
1003 int __kprobes register_kretprobes(struct kretprobe **rps, int num)
1005 return __register_kretprobes(rps, num,
1006 (unsigned long)__builtin_return_address(0));
1009 void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1011 int i;
1013 if (num <= 0)
1014 return;
1015 mutex_lock(&kprobe_mutex);
1016 for (i = 0; i < num; i++)
1017 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
1018 rps[i]->kp.addr = NULL;
1019 mutex_unlock(&kprobe_mutex);
1021 synchronize_sched();
1022 for (i = 0; i < num; i++) {
1023 if (rps[i]->kp.addr) {
1024 __unregister_kprobe_bottom(&rps[i]->kp);
1025 cleanup_rp_inst(rps[i]);
1030 #else /* CONFIG_KRETPROBES */
1031 int __kprobes register_kretprobe(struct kretprobe *rp)
1033 return -ENOSYS;
1036 int __kprobes register_kretprobes(struct kretprobe **rps, int num)
1038 return -ENOSYS;
1040 void __kprobes unregister_kretprobe(struct kretprobe *rp)
1044 void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1048 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
1049 struct pt_regs *regs)
1051 return 0;
1054 #endif /* CONFIG_KRETPROBES */
1056 static int __init init_kprobes(void)
1058 int i, err = 0;
1059 unsigned long offset = 0, size = 0;
1060 char *modname, namebuf[128];
1061 const char *symbol_name;
1062 void *addr;
1063 struct kprobe_blackpoint *kb;
1065 /* FIXME allocate the probe table, currently defined statically */
1066 /* initialize all list heads */
1067 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1068 INIT_HLIST_HEAD(&kprobe_table[i]);
1069 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
1070 spin_lock_init(&(kretprobe_table_locks[i].lock));
1074 * Lookup and populate the kprobe_blacklist.
1076 * Unlike the kretprobe blacklist, we'll need to determine
1077 * the range of addresses that belong to the said functions,
1078 * since a kprobe need not necessarily be at the beginning
1079 * of a function.
1081 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
1082 kprobe_lookup_name(kb->name, addr);
1083 if (!addr)
1084 continue;
1086 kb->start_addr = (unsigned long)addr;
1087 symbol_name = kallsyms_lookup(kb->start_addr,
1088 &size, &offset, &modname, namebuf);
1089 if (!symbol_name)
1090 kb->range = 0;
1091 else
1092 kb->range = size;
1095 if (kretprobe_blacklist_size) {
1096 /* lookup the function address from its name */
1097 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1098 kprobe_lookup_name(kretprobe_blacklist[i].name,
1099 kretprobe_blacklist[i].addr);
1100 if (!kretprobe_blacklist[i].addr)
1101 printk("kretprobe: lookup failed: %s\n",
1102 kretprobe_blacklist[i].name);
1106 /* By default, kprobes are enabled */
1107 kprobe_enabled = true;
1109 err = arch_init_kprobes();
1110 if (!err)
1111 err = register_die_notifier(&kprobe_exceptions_nb);
1112 kprobes_initialized = (err == 0);
1114 if (!err)
1115 init_test_probes();
1116 return err;
1119 #ifdef CONFIG_DEBUG_FS
1120 static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
1121 const char *sym, int offset,char *modname)
1123 char *kprobe_type;
1125 if (p->pre_handler == pre_handler_kretprobe)
1126 kprobe_type = "r";
1127 else if (p->pre_handler == setjmp_pre_handler)
1128 kprobe_type = "j";
1129 else
1130 kprobe_type = "k";
1131 if (sym)
1132 seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
1133 sym, offset, (modname ? modname : " "));
1134 else
1135 seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
1138 static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
1140 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
1143 static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
1145 (*pos)++;
1146 if (*pos >= KPROBE_TABLE_SIZE)
1147 return NULL;
1148 return pos;
1151 static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
1153 /* Nothing to do */
1156 static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
1158 struct hlist_head *head;
1159 struct hlist_node *node;
1160 struct kprobe *p, *kp;
1161 const char *sym = NULL;
1162 unsigned int i = *(loff_t *) v;
1163 unsigned long offset = 0;
1164 char *modname, namebuf[128];
1166 head = &kprobe_table[i];
1167 preempt_disable();
1168 hlist_for_each_entry_rcu(p, node, head, hlist) {
1169 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
1170 &offset, &modname, namebuf);
1171 if (p->pre_handler == aggr_pre_handler) {
1172 list_for_each_entry_rcu(kp, &p->list, list)
1173 report_probe(pi, kp, sym, offset, modname);
1174 } else
1175 report_probe(pi, p, sym, offset, modname);
1177 preempt_enable();
1178 return 0;
1181 static struct seq_operations kprobes_seq_ops = {
1182 .start = kprobe_seq_start,
1183 .next = kprobe_seq_next,
1184 .stop = kprobe_seq_stop,
1185 .show = show_kprobe_addr
1188 static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
1190 return seq_open(filp, &kprobes_seq_ops);
1193 static struct file_operations debugfs_kprobes_operations = {
1194 .open = kprobes_open,
1195 .read = seq_read,
1196 .llseek = seq_lseek,
1197 .release = seq_release,
1200 static void __kprobes enable_all_kprobes(void)
1202 struct hlist_head *head;
1203 struct hlist_node *node;
1204 struct kprobe *p;
1205 unsigned int i;
1207 mutex_lock(&kprobe_mutex);
1209 /* If kprobes are already enabled, just return */
1210 if (kprobe_enabled)
1211 goto already_enabled;
1213 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1214 head = &kprobe_table[i];
1215 hlist_for_each_entry_rcu(p, node, head, hlist)
1216 arch_arm_kprobe(p);
1219 kprobe_enabled = true;
1220 printk(KERN_INFO "Kprobes globally enabled\n");
1222 already_enabled:
1223 mutex_unlock(&kprobe_mutex);
1224 return;
1227 static void __kprobes disable_all_kprobes(void)
1229 struct hlist_head *head;
1230 struct hlist_node *node;
1231 struct kprobe *p;
1232 unsigned int i;
1234 mutex_lock(&kprobe_mutex);
1236 /* If kprobes are already disabled, just return */
1237 if (!kprobe_enabled)
1238 goto already_disabled;
1240 kprobe_enabled = false;
1241 printk(KERN_INFO "Kprobes globally disabled\n");
1242 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1243 head = &kprobe_table[i];
1244 hlist_for_each_entry_rcu(p, node, head, hlist) {
1245 if (!arch_trampoline_kprobe(p))
1246 arch_disarm_kprobe(p);
1250 mutex_unlock(&kprobe_mutex);
1251 /* Allow all currently running kprobes to complete */
1252 synchronize_sched();
1253 return;
1255 already_disabled:
1256 mutex_unlock(&kprobe_mutex);
1257 return;
1261 * XXX: The debugfs bool file interface doesn't allow for callbacks
1262 * when the bool state is switched. We can reuse that facility when
1263 * available
1265 static ssize_t read_enabled_file_bool(struct file *file,
1266 char __user *user_buf, size_t count, loff_t *ppos)
1268 char buf[3];
1270 if (kprobe_enabled)
1271 buf[0] = '1';
1272 else
1273 buf[0] = '0';
1274 buf[1] = '\n';
1275 buf[2] = 0x00;
1276 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1279 static ssize_t write_enabled_file_bool(struct file *file,
1280 const char __user *user_buf, size_t count, loff_t *ppos)
1282 char buf[32];
1283 int buf_size;
1285 buf_size = min(count, (sizeof(buf)-1));
1286 if (copy_from_user(buf, user_buf, buf_size))
1287 return -EFAULT;
1289 switch (buf[0]) {
1290 case 'y':
1291 case 'Y':
1292 case '1':
1293 enable_all_kprobes();
1294 break;
1295 case 'n':
1296 case 'N':
1297 case '0':
1298 disable_all_kprobes();
1299 break;
1302 return count;
1305 static struct file_operations fops_kp = {
1306 .read = read_enabled_file_bool,
1307 .write = write_enabled_file_bool,
1310 static int __kprobes debugfs_kprobe_init(void)
1312 struct dentry *dir, *file;
1313 unsigned int value = 1;
1315 dir = debugfs_create_dir("kprobes", NULL);
1316 if (!dir)
1317 return -ENOMEM;
1319 file = debugfs_create_file("list", 0444, dir, NULL,
1320 &debugfs_kprobes_operations);
1321 if (!file) {
1322 debugfs_remove(dir);
1323 return -ENOMEM;
1326 file = debugfs_create_file("enabled", 0600, dir,
1327 &value, &fops_kp);
1328 if (!file) {
1329 debugfs_remove(dir);
1330 return -ENOMEM;
1333 return 0;
1336 late_initcall(debugfs_kprobe_init);
1337 #endif /* CONFIG_DEBUG_FS */
1339 module_init(init_kprobes);
1341 EXPORT_SYMBOL_GPL(register_kprobe);
1342 EXPORT_SYMBOL_GPL(unregister_kprobe);
1343 EXPORT_SYMBOL_GPL(register_kprobes);
1344 EXPORT_SYMBOL_GPL(unregister_kprobes);
1345 EXPORT_SYMBOL_GPL(register_jprobe);
1346 EXPORT_SYMBOL_GPL(unregister_jprobe);
1347 EXPORT_SYMBOL_GPL(register_jprobes);
1348 EXPORT_SYMBOL_GPL(unregister_jprobes);
1349 EXPORT_SYMBOL_GPL(jprobe_return);
1350 EXPORT_SYMBOL_GPL(register_kretprobe);
1351 EXPORT_SYMBOL_GPL(unregister_kretprobe);
1352 EXPORT_SYMBOL_GPL(register_kretprobes);
1353 EXPORT_SYMBOL_GPL(unregister_kretprobes);