2 * Kernel Probes (KProbes)
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
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/spinlock.h>
36 #include <linux/hash.h>
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <asm/cacheflush.h>
40 #include <asm/errno.h>
41 #include <asm/kdebug.h>
43 #define KPROBE_HASH_BITS 6
44 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
46 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
47 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
49 unsigned int kprobe_cpu = NR_CPUS;
50 static DEFINE_SPINLOCK(kprobe_lock);
51 static struct kprobe *curr_kprobe;
53 /* Locks kprobe: irqs must be disabled */
54 void lock_kprobes(void)
56 spin_lock(&kprobe_lock);
57 kprobe_cpu = smp_processor_id();
60 void unlock_kprobes(void)
63 spin_unlock(&kprobe_lock);
66 /* You have to be holding the kprobe_lock */
67 struct kprobe *get_kprobe(void *addr)
69 struct hlist_head *head;
70 struct hlist_node *node;
72 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
73 hlist_for_each(node, head) {
74 struct kprobe *p = hlist_entry(node, struct kprobe, hlist);
82 * Aggregate handlers for multiple kprobes support - these handlers
83 * take care of invoking the individual kprobe handlers on p->list
85 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
89 list_for_each_entry(kp, &p->list, list) {
90 if (kp->pre_handler) {
92 kp->pre_handler(kp, regs);
99 static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
104 list_for_each_entry(kp, &p->list, list) {
105 if (kp->post_handler) {
107 kp->post_handler(kp, regs, flags);
114 static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
118 * if we faulted "during" the execution of a user specified
119 * probe handler, invoke just that probe's fault handler
121 if (curr_kprobe && curr_kprobe->fault_handler) {
122 if (curr_kprobe->fault_handler(curr_kprobe, regs, trapnr))
128 struct kprobe trampoline_p = {
129 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
130 .pre_handler = trampoline_probe_handler,
131 .post_handler = trampoline_post_handler
134 struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
136 struct hlist_node *node;
137 struct kretprobe_instance *ri;
138 hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
143 static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
145 struct hlist_node *node;
146 struct kretprobe_instance *ri;
147 hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
152 struct kretprobe_instance *get_rp_inst(void *sara)
154 struct hlist_head *head;
155 struct hlist_node *node;
156 struct task_struct *tsk;
157 struct kretprobe_instance *ri;
159 tsk = arch_get_kprobe_task(sara);
160 head = &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
161 hlist_for_each_entry(ri, node, head, hlist) {
162 if (ri->stack_addr == sara)
168 void add_rp_inst(struct kretprobe_instance *ri)
170 struct task_struct *tsk;
172 * Remove rp inst off the free list -
173 * Add it back when probed function returns
175 hlist_del(&ri->uflist);
176 tsk = arch_get_kprobe_task(ri->stack_addr);
177 /* Add rp inst onto table */
178 INIT_HLIST_NODE(&ri->hlist);
179 hlist_add_head(&ri->hlist,
180 &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)]);
182 /* Also add this rp inst to the used list. */
183 INIT_HLIST_NODE(&ri->uflist);
184 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
187 void recycle_rp_inst(struct kretprobe_instance *ri)
189 /* remove rp inst off the rprobe_inst_table */
190 hlist_del(&ri->hlist);
192 /* remove rp inst off the used list */
193 hlist_del(&ri->uflist);
194 /* put rp inst back onto the free list */
195 INIT_HLIST_NODE(&ri->uflist);
196 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
202 struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk)
204 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
207 struct kretprobe_instance *get_rp_inst_tsk(struct task_struct *tk)
209 struct task_struct *tsk;
210 struct hlist_head *head;
211 struct hlist_node *node;
212 struct kretprobe_instance *ri;
214 head = &kretprobe_inst_table[hash_ptr(tk, KPROBE_HASH_BITS)];
216 hlist_for_each_entry(ri, node, head, hlist) {
217 tsk = arch_get_kprobe_task(ri->stack_addr);
225 * This function is called from do_exit or do_execv when task tk's stack is
226 * about to be recycled. Recycle any function-return probe instances
227 * associated with this task. These represent probed functions that have
228 * been called but may never return.
230 void kprobe_flush_task(struct task_struct *tk)
232 arch_kprobe_flush_task(tk, &kprobe_lock);
236 * This kprobe pre_handler is registered with every kretprobe. When probe
237 * hits it will set up the return probe.
239 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
241 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
243 /*TODO: consider to only swap the RA after the last pre_handler fired */
244 arch_prepare_kretprobe(rp, regs);
248 static inline void free_rp_inst(struct kretprobe *rp)
250 struct kretprobe_instance *ri;
251 while ((ri = get_free_rp_inst(rp)) != NULL) {
252 hlist_del(&ri->uflist);
258 * Fill in the required fields of the "manager kprobe". Replace the
259 * earlier kprobe in the hlist with the manager kprobe
261 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
264 memcpy(&ap->opcode, &p->opcode, sizeof(kprobe_opcode_t));
265 memcpy(&ap->ainsn, &p->ainsn, sizeof(struct arch_specific_insn));
267 ap->pre_handler = aggr_pre_handler;
268 ap->post_handler = aggr_post_handler;
269 ap->fault_handler = aggr_fault_handler;
271 INIT_LIST_HEAD(&ap->list);
272 list_add(&p->list, &ap->list);
274 INIT_HLIST_NODE(&ap->hlist);
275 hlist_del(&p->hlist);
276 hlist_add_head(&ap->hlist,
277 &kprobe_table[hash_ptr(ap->addr, KPROBE_HASH_BITS)]);
281 * This is the second or subsequent kprobe at the address - handle
283 * TODO: Move kcalloc outside the spinlock
285 static int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
290 if (old_p->break_handler || p->break_handler) {
291 ret = -EEXIST; /* kprobe and jprobe can't (yet) coexist */
292 } else if (old_p->pre_handler == aggr_pre_handler) {
293 list_add(&p->list, &old_p->list);
295 ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC);
298 add_aggr_kprobe(ap, old_p);
299 list_add(&p->list, &ap->list);
304 /* kprobe removal house-keeping routines */
305 static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags)
307 arch_disarm_kprobe(p);
308 hlist_del(&p->hlist);
309 spin_unlock_irqrestore(&kprobe_lock, flags);
310 arch_remove_kprobe(p);
313 static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
314 struct kprobe *p, unsigned long flags)
317 if (list_empty(&old_p->list)) {
318 cleanup_kprobe(old_p, flags);
321 spin_unlock_irqrestore(&kprobe_lock, flags);
324 int register_kprobe(struct kprobe *p)
327 unsigned long flags = 0;
328 struct kprobe *old_p;
330 if ((ret = arch_prepare_kprobe(p)) != 0) {
333 spin_lock_irqsave(&kprobe_lock, flags);
334 old_p = get_kprobe(p->addr);
336 ret = register_aggr_kprobe(old_p, p);
341 INIT_HLIST_NODE(&p->hlist);
342 hlist_add_head(&p->hlist,
343 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
348 spin_unlock_irqrestore(&kprobe_lock, flags);
351 arch_remove_kprobe(p);
355 void unregister_kprobe(struct kprobe *p)
358 struct kprobe *old_p;
360 spin_lock_irqsave(&kprobe_lock, flags);
361 old_p = get_kprobe(p->addr);
363 if (old_p->pre_handler == aggr_pre_handler)
364 cleanup_aggr_kprobe(old_p, p, flags);
366 cleanup_kprobe(p, flags);
368 spin_unlock_irqrestore(&kprobe_lock, flags);
371 static struct notifier_block kprobe_exceptions_nb = {
372 .notifier_call = kprobe_exceptions_notify,
373 .priority = 0x7fffffff /* we need to notified first */
376 int register_jprobe(struct jprobe *jp)
378 /* Todo: Verify probepoint is a function entry point */
379 jp->kp.pre_handler = setjmp_pre_handler;
380 jp->kp.break_handler = longjmp_break_handler;
382 return register_kprobe(&jp->kp);
385 void unregister_jprobe(struct jprobe *jp)
387 unregister_kprobe(&jp->kp);
390 #ifdef ARCH_SUPPORTS_KRETPROBES
392 int register_kretprobe(struct kretprobe *rp)
395 struct kretprobe_instance *inst;
398 rp->kp.pre_handler = pre_handler_kretprobe;
400 /* Pre-allocate memory for max kretprobe instances */
401 if (rp->maxactive <= 0) {
402 #ifdef CONFIG_PREEMPT
403 rp->maxactive = max(10, 2 * NR_CPUS);
405 rp->maxactive = NR_CPUS;
408 INIT_HLIST_HEAD(&rp->used_instances);
409 INIT_HLIST_HEAD(&rp->free_instances);
410 for (i = 0; i < rp->maxactive; i++) {
411 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
416 INIT_HLIST_NODE(&inst->uflist);
417 hlist_add_head(&inst->uflist, &rp->free_instances);
421 /* Establish function entry probe point */
422 if ((ret = register_kprobe(&rp->kp)) != 0)
427 #else /* ARCH_SUPPORTS_KRETPROBES */
429 int register_kretprobe(struct kretprobe *rp)
434 #endif /* ARCH_SUPPORTS_KRETPROBES */
436 void unregister_kretprobe(struct kretprobe *rp)
439 struct kretprobe_instance *ri;
441 unregister_kprobe(&rp->kp);
443 spin_lock_irqsave(&kprobe_lock, flags);
445 while ((ri = get_used_rp_inst(rp)) != NULL) {
447 hlist_del(&ri->uflist);
449 spin_unlock_irqrestore(&kprobe_lock, flags);
452 static int __init init_kprobes(void)
456 /* FIXME allocate the probe table, currently defined statically */
457 /* initialize all list heads */
458 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
459 INIT_HLIST_HEAD(&kprobe_table[i]);
460 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
463 err = register_die_notifier(&kprobe_exceptions_nb);
464 /* Register the trampoline probe for return probe */
465 register_kprobe(&trampoline_p);
469 __initcall(init_kprobes);
471 EXPORT_SYMBOL_GPL(register_kprobe);
472 EXPORT_SYMBOL_GPL(unregister_kprobe);
473 EXPORT_SYMBOL_GPL(register_jprobe);
474 EXPORT_SYMBOL_GPL(unregister_jprobe);
475 EXPORT_SYMBOL_GPL(jprobe_return);
476 EXPORT_SYMBOL_GPL(register_kretprobe);
477 EXPORT_SYMBOL_GPL(unregister_kretprobe);