Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6
[linux-2.6-block.git] / kernel / kprobes.c
CommitLineData
1da177e4
LT
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.
b94cce92
HN
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.
1da177e4
LT
33 */
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>
42
43#define KPROBE_HASH_BITS 6
44#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
45
46static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
b94cce92 47static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
1da177e4
LT
48
49unsigned int kprobe_cpu = NR_CPUS;
50static DEFINE_SPINLOCK(kprobe_lock);
64f562c6 51static struct kprobe *curr_kprobe;
1da177e4
LT
52
53/* Locks kprobe: irqs must be disabled */
54void lock_kprobes(void)
55{
56 spin_lock(&kprobe_lock);
57 kprobe_cpu = smp_processor_id();
58}
59
60void unlock_kprobes(void)
61{
62 kprobe_cpu = NR_CPUS;
63 spin_unlock(&kprobe_lock);
64}
65
66/* You have to be holding the kprobe_lock */
67struct kprobe *get_kprobe(void *addr)
68{
69 struct hlist_head *head;
70 struct hlist_node *node;
71
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);
75 if (p->addr == addr)
76 return p;
77 }
78 return NULL;
79}
80
64f562c6
AM
81/*
82 * Aggregate handlers for multiple kprobes support - these handlers
83 * take care of invoking the individual kprobe handlers on p->list
84 */
b94cce92 85static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
64f562c6
AM
86{
87 struct kprobe *kp;
88
89 list_for_each_entry(kp, &p->list, list) {
90 if (kp->pre_handler) {
91 curr_kprobe = kp;
8b0914ea
PP
92 if (kp->pre_handler(kp, regs))
93 return 1;
64f562c6 94 }
8b0914ea 95 curr_kprobe = NULL;
64f562c6
AM
96 }
97 return 0;
98}
99
b94cce92
HN
100static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
101 unsigned long flags)
64f562c6
AM
102{
103 struct kprobe *kp;
104
105 list_for_each_entry(kp, &p->list, list) {
106 if (kp->post_handler) {
107 curr_kprobe = kp;
108 kp->post_handler(kp, regs, flags);
109 curr_kprobe = NULL;
110 }
111 }
112 return;
113}
114
b94cce92
HN
115static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
116 int trapnr)
64f562c6
AM
117{
118 /*
119 * if we faulted "during" the execution of a user specified
120 * probe handler, invoke just that probe's fault handler
121 */
122 if (curr_kprobe && curr_kprobe->fault_handler) {
123 if (curr_kprobe->fault_handler(curr_kprobe, regs, trapnr))
124 return 1;
125 }
126 return 0;
127}
128
8b0914ea
PP
129static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
130{
131 struct kprobe *kp = curr_kprobe;
132 if (curr_kprobe && kp->break_handler) {
133 if (kp->break_handler(kp, regs)) {
134 curr_kprobe = NULL;
135 return 1;
136 }
137 }
138 curr_kprobe = NULL;
139 return 0;
140}
141
b94cce92
HN
142struct kprobe trampoline_p = {
143 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
144 .pre_handler = trampoline_probe_handler,
145 .post_handler = trampoline_post_handler
146};
147
148struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
149{
150 struct hlist_node *node;
151 struct kretprobe_instance *ri;
152 hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
153 return ri;
154 return NULL;
155}
156
157static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
158{
159 struct hlist_node *node;
160 struct kretprobe_instance *ri;
161 hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
162 return ri;
163 return NULL;
164}
165
166struct kretprobe_instance *get_rp_inst(void *sara)
167{
168 struct hlist_head *head;
169 struct hlist_node *node;
170 struct task_struct *tsk;
171 struct kretprobe_instance *ri;
172
173 tsk = arch_get_kprobe_task(sara);
174 head = &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
175 hlist_for_each_entry(ri, node, head, hlist) {
176 if (ri->stack_addr == sara)
177 return ri;
178 }
179 return NULL;
180}
181
182void add_rp_inst(struct kretprobe_instance *ri)
183{
184 struct task_struct *tsk;
185 /*
186 * Remove rp inst off the free list -
187 * Add it back when probed function returns
188 */
189 hlist_del(&ri->uflist);
190 tsk = arch_get_kprobe_task(ri->stack_addr);
191 /* Add rp inst onto table */
192 INIT_HLIST_NODE(&ri->hlist);
193 hlist_add_head(&ri->hlist,
194 &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)]);
195
196 /* Also add this rp inst to the used list. */
197 INIT_HLIST_NODE(&ri->uflist);
198 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
199}
200
201void recycle_rp_inst(struct kretprobe_instance *ri)
202{
203 /* remove rp inst off the rprobe_inst_table */
204 hlist_del(&ri->hlist);
205 if (ri->rp) {
206 /* remove rp inst off the used list */
207 hlist_del(&ri->uflist);
208 /* put rp inst back onto the free list */
209 INIT_HLIST_NODE(&ri->uflist);
210 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
211 } else
212 /* Unregistering */
213 kfree(ri);
214}
215
216struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk)
217{
218 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
219}
220
221struct kretprobe_instance *get_rp_inst_tsk(struct task_struct *tk)
222{
223 struct task_struct *tsk;
224 struct hlist_head *head;
225 struct hlist_node *node;
226 struct kretprobe_instance *ri;
227
228 head = &kretprobe_inst_table[hash_ptr(tk, KPROBE_HASH_BITS)];
229
230 hlist_for_each_entry(ri, node, head, hlist) {
231 tsk = arch_get_kprobe_task(ri->stack_addr);
232 if (tsk == tk)
233 return ri;
234 }
235 return NULL;
236}
237
238/*
239 * This function is called from do_exit or do_execv when task tk's stack is
240 * about to be recycled. Recycle any function-return probe instances
241 * associated with this task. These represent probed functions that have
242 * been called but may never return.
243 */
244void kprobe_flush_task(struct task_struct *tk)
245{
0aa55e4d
HN
246 unsigned long flags = 0;
247 spin_lock_irqsave(&kprobe_lock, flags);
248 arch_kprobe_flush_task(tk);
249 spin_unlock_irqrestore(&kprobe_lock, flags);
b94cce92
HN
250}
251
252/*
253 * This kprobe pre_handler is registered with every kretprobe. When probe
254 * hits it will set up the return probe.
255 */
256static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
257{
258 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
259
260 /*TODO: consider to only swap the RA after the last pre_handler fired */
261 arch_prepare_kretprobe(rp, regs);
262 return 0;
263}
264
265static inline void free_rp_inst(struct kretprobe *rp)
266{
267 struct kretprobe_instance *ri;
268 while ((ri = get_free_rp_inst(rp)) != NULL) {
269 hlist_del(&ri->uflist);
270 kfree(ri);
271 }
272}
273
8b0914ea
PP
274/*
275 * Keep all fields in the kprobe consistent
276 */
277static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
278{
279 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
280 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
281}
282
283/*
284* Add the new probe to old_p->list. Fail if this is the
285* second jprobe at the address - two jprobes can't coexist
286*/
287static int add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
288{
289 struct kprobe *kp;
290
291 if (p->break_handler) {
292 list_for_each_entry(kp, &old_p->list, list) {
293 if (kp->break_handler)
294 return -EEXIST;
295 }
296 list_add_tail(&p->list, &old_p->list);
297 } else
298 list_add(&p->list, &old_p->list);
299 return 0;
300}
301
64f562c6
AM
302/*
303 * Fill in the required fields of the "manager kprobe". Replace the
304 * earlier kprobe in the hlist with the manager kprobe
305 */
306static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
307{
8b0914ea 308 copy_kprobe(p, ap);
64f562c6 309 ap->addr = p->addr;
64f562c6
AM
310 ap->pre_handler = aggr_pre_handler;
311 ap->post_handler = aggr_post_handler;
312 ap->fault_handler = aggr_fault_handler;
8b0914ea 313 ap->break_handler = aggr_break_handler;
64f562c6
AM
314
315 INIT_LIST_HEAD(&ap->list);
316 list_add(&p->list, &ap->list);
317
318 INIT_HLIST_NODE(&ap->hlist);
319 hlist_del(&p->hlist);
320 hlist_add_head(&ap->hlist,
321 &kprobe_table[hash_ptr(ap->addr, KPROBE_HASH_BITS)]);
322}
323
324/*
325 * This is the second or subsequent kprobe at the address - handle
326 * the intricacies
327 * TODO: Move kcalloc outside the spinlock
328 */
329static int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
330{
331 int ret = 0;
332 struct kprobe *ap;
333
8b0914ea
PP
334 if (old_p->pre_handler == aggr_pre_handler) {
335 copy_kprobe(old_p, p);
336 ret = add_new_kprobe(old_p, p);
64f562c6
AM
337 } else {
338 ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC);
339 if (!ap)
340 return -ENOMEM;
341 add_aggr_kprobe(ap, old_p);
8b0914ea
PP
342 copy_kprobe(ap, p);
343 ret = add_new_kprobe(ap, p);
64f562c6
AM
344 }
345 return ret;
346}
347
348/* kprobe removal house-keeping routines */
349static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags)
350{
7e1048b1 351 arch_disarm_kprobe(p);
64f562c6 352 hlist_del(&p->hlist);
64f562c6
AM
353 spin_unlock_irqrestore(&kprobe_lock, flags);
354 arch_remove_kprobe(p);
355}
356
357static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
358 struct kprobe *p, unsigned long flags)
359{
360 list_del(&p->list);
361 if (list_empty(&old_p->list)) {
362 cleanup_kprobe(old_p, flags);
363 kfree(old_p);
364 } else
365 spin_unlock_irqrestore(&kprobe_lock, flags);
366}
367
1da177e4
LT
368int register_kprobe(struct kprobe *p)
369{
370 int ret = 0;
371 unsigned long flags = 0;
64f562c6 372 struct kprobe *old_p;
1da177e4
LT
373
374 if ((ret = arch_prepare_kprobe(p)) != 0) {
375 goto rm_kprobe;
376 }
377 spin_lock_irqsave(&kprobe_lock, flags);
64f562c6 378 old_p = get_kprobe(p->addr);
ea32c65c 379 p->nmissed = 0;
64f562c6
AM
380 if (old_p) {
381 ret = register_aggr_kprobe(old_p, p);
1da177e4
LT
382 goto out;
383 }
1da177e4 384
64f562c6
AM
385 arch_copy_kprobe(p);
386 INIT_HLIST_NODE(&p->hlist);
1da177e4
LT
387 hlist_add_head(&p->hlist,
388 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
389
7e1048b1
RL
390 arch_arm_kprobe(p);
391
1da177e4
LT
392out:
393 spin_unlock_irqrestore(&kprobe_lock, flags);
394rm_kprobe:
395 if (ret == -EEXIST)
396 arch_remove_kprobe(p);
397 return ret;
398}
399
400void unregister_kprobe(struct kprobe *p)
401{
402 unsigned long flags;
64f562c6
AM
403 struct kprobe *old_p;
404
1da177e4 405 spin_lock_irqsave(&kprobe_lock, flags);
64f562c6
AM
406 old_p = get_kprobe(p->addr);
407 if (old_p) {
408 if (old_p->pre_handler == aggr_pre_handler)
409 cleanup_aggr_kprobe(old_p, p, flags);
410 else
411 cleanup_kprobe(p, flags);
412 } else
04dea5f9 413 spin_unlock_irqrestore(&kprobe_lock, flags);
1da177e4
LT
414}
415
416static struct notifier_block kprobe_exceptions_nb = {
417 .notifier_call = kprobe_exceptions_notify,
418 .priority = 0x7fffffff /* we need to notified first */
419};
420
421int register_jprobe(struct jprobe *jp)
422{
423 /* Todo: Verify probepoint is a function entry point */
424 jp->kp.pre_handler = setjmp_pre_handler;
425 jp->kp.break_handler = longjmp_break_handler;
426
427 return register_kprobe(&jp->kp);
428}
429
430void unregister_jprobe(struct jprobe *jp)
431{
432 unregister_kprobe(&jp->kp);
433}
434
b94cce92
HN
435#ifdef ARCH_SUPPORTS_KRETPROBES
436
437int register_kretprobe(struct kretprobe *rp)
438{
439 int ret = 0;
440 struct kretprobe_instance *inst;
441 int i;
442
443 rp->kp.pre_handler = pre_handler_kretprobe;
444
445 /* Pre-allocate memory for max kretprobe instances */
446 if (rp->maxactive <= 0) {
447#ifdef CONFIG_PREEMPT
448 rp->maxactive = max(10, 2 * NR_CPUS);
449#else
450 rp->maxactive = NR_CPUS;
451#endif
452 }
453 INIT_HLIST_HEAD(&rp->used_instances);
454 INIT_HLIST_HEAD(&rp->free_instances);
455 for (i = 0; i < rp->maxactive; i++) {
456 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
457 if (inst == NULL) {
458 free_rp_inst(rp);
459 return -ENOMEM;
460 }
461 INIT_HLIST_NODE(&inst->uflist);
462 hlist_add_head(&inst->uflist, &rp->free_instances);
463 }
464
465 rp->nmissed = 0;
466 /* Establish function entry probe point */
467 if ((ret = register_kprobe(&rp->kp)) != 0)
468 free_rp_inst(rp);
469 return ret;
470}
471
472#else /* ARCH_SUPPORTS_KRETPROBES */
473
474int register_kretprobe(struct kretprobe *rp)
475{
476 return -ENOSYS;
477}
478
479#endif /* ARCH_SUPPORTS_KRETPROBES */
480
481void unregister_kretprobe(struct kretprobe *rp)
482{
483 unsigned long flags;
484 struct kretprobe_instance *ri;
485
486 unregister_kprobe(&rp->kp);
487 /* No race here */
488 spin_lock_irqsave(&kprobe_lock, flags);
489 free_rp_inst(rp);
490 while ((ri = get_used_rp_inst(rp)) != NULL) {
491 ri->rp = NULL;
492 hlist_del(&ri->uflist);
493 }
494 spin_unlock_irqrestore(&kprobe_lock, flags);
495}
496
1da177e4
LT
497static int __init init_kprobes(void)
498{
499 int i, err = 0;
500
501 /* FIXME allocate the probe table, currently defined statically */
502 /* initialize all list heads */
b94cce92 503 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1da177e4 504 INIT_HLIST_HEAD(&kprobe_table[i]);
b94cce92
HN
505 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
506 }
1da177e4
LT
507
508 err = register_die_notifier(&kprobe_exceptions_nb);
b94cce92
HN
509 /* Register the trampoline probe for return probe */
510 register_kprobe(&trampoline_p);
1da177e4
LT
511 return err;
512}
513
514__initcall(init_kprobes);
515
516EXPORT_SYMBOL_GPL(register_kprobe);
517EXPORT_SYMBOL_GPL(unregister_kprobe);
518EXPORT_SYMBOL_GPL(register_jprobe);
519EXPORT_SYMBOL_GPL(unregister_jprobe);
520EXPORT_SYMBOL_GPL(jprobe_return);
b94cce92
HN
521EXPORT_SYMBOL_GPL(register_kretprobe);
522EXPORT_SYMBOL_GPL(unregister_kretprobe);
523