blob: e8e0ae8a6e141ce6230f5f20dc6e2778d5b6c39f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Hien Nguyenb94cce92005-06-23 00:09:19 -070030 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
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];
Hien Nguyenb94cce92005-06-23 00:09:19 -070047static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49unsigned int kprobe_cpu = NR_CPUS;
50static DEFINE_SPINLOCK(kprobe_lock);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -070051static struct kprobe *curr_kprobe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
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
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -070081/*
82 * Aggregate handlers for multiple kprobes support - these handlers
83 * take care of invoking the individual kprobe handlers on p->list
84 */
Hien Nguyenb94cce92005-06-23 00:09:19 -070085static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -070086{
87 struct kprobe *kp;
88
89 list_for_each_entry(kp, &p->list, list) {
90 if (kp->pre_handler) {
91 curr_kprobe = kp;
92 kp->pre_handler(kp, regs);
93 curr_kprobe = NULL;
94 }
95 }
96 return 0;
97}
98
Hien Nguyenb94cce92005-06-23 00:09:19 -070099static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
100 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700101{
102 struct kprobe *kp;
103
104 list_for_each_entry(kp, &p->list, list) {
105 if (kp->post_handler) {
106 curr_kprobe = kp;
107 kp->post_handler(kp, regs, flags);
108 curr_kprobe = NULL;
109 }
110 }
111 return;
112}
113
Hien Nguyenb94cce92005-06-23 00:09:19 -0700114static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
115 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700116{
117 /*
118 * if we faulted "during" the execution of a user specified
119 * probe handler, invoke just that probe's fault handler
120 */
121 if (curr_kprobe && curr_kprobe->fault_handler) {
122 if (curr_kprobe->fault_handler(curr_kprobe, regs, trapnr))
123 return 1;
124 }
125 return 0;
126}
127
Hien Nguyenb94cce92005-06-23 00:09:19 -0700128struct kprobe trampoline_p = {
129 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
130 .pre_handler = trampoline_probe_handler,
131 .post_handler = trampoline_post_handler
132};
133
134struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp)
135{
136 struct hlist_node *node;
137 struct kretprobe_instance *ri;
138 hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
139 return ri;
140 return NULL;
141}
142
143static struct kretprobe_instance *get_used_rp_inst(struct kretprobe *rp)
144{
145 struct hlist_node *node;
146 struct kretprobe_instance *ri;
147 hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
148 return ri;
149 return NULL;
150}
151
152struct kretprobe_instance *get_rp_inst(void *sara)
153{
154 struct hlist_head *head;
155 struct hlist_node *node;
156 struct task_struct *tsk;
157 struct kretprobe_instance *ri;
158
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)
163 return ri;
164 }
165 return NULL;
166}
167
168void add_rp_inst(struct kretprobe_instance *ri)
169{
170 struct task_struct *tsk;
171 /*
172 * Remove rp inst off the free list -
173 * Add it back when probed function returns
174 */
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)]);
181
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);
185}
186
187void recycle_rp_inst(struct kretprobe_instance *ri)
188{
189 /* remove rp inst off the rprobe_inst_table */
190 hlist_del(&ri->hlist);
191 if (ri->rp) {
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);
197 } else
198 /* Unregistering */
199 kfree(ri);
200}
201
202struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk)
203{
204 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
205}
206
207struct kretprobe_instance *get_rp_inst_tsk(struct task_struct *tk)
208{
209 struct task_struct *tsk;
210 struct hlist_head *head;
211 struct hlist_node *node;
212 struct kretprobe_instance *ri;
213
214 head = &kretprobe_inst_table[hash_ptr(tk, KPROBE_HASH_BITS)];
215
216 hlist_for_each_entry(ri, node, head, hlist) {
217 tsk = arch_get_kprobe_task(ri->stack_addr);
218 if (tsk == tk)
219 return ri;
220 }
221 return NULL;
222}
223
224/*
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.
229 */
230void kprobe_flush_task(struct task_struct *tk)
231{
232 arch_kprobe_flush_task(tk, &kprobe_lock);
233}
234
235/*
236 * This kprobe pre_handler is registered with every kretprobe. When probe
237 * hits it will set up the return probe.
238 */
239static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
240{
241 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
242
243 /*TODO: consider to only swap the RA after the last pre_handler fired */
244 arch_prepare_kretprobe(rp, regs);
245 return 0;
246}
247
248static inline void free_rp_inst(struct kretprobe *rp)
249{
250 struct kretprobe_instance *ri;
251 while ((ri = get_free_rp_inst(rp)) != NULL) {
252 hlist_del(&ri->uflist);
253 kfree(ri);
254 }
255}
256
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700257/*
258 * Fill in the required fields of the "manager kprobe". Replace the
259 * earlier kprobe in the hlist with the manager kprobe
260 */
261static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
262{
263 ap->addr = p->addr;
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700264 memcpy(&ap->opcode, &p->opcode, sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700265 memcpy(&ap->ainsn, &p->ainsn, sizeof(struct arch_specific_insn));
266
267 ap->pre_handler = aggr_pre_handler;
268 ap->post_handler = aggr_post_handler;
269 ap->fault_handler = aggr_fault_handler;
270
271 INIT_LIST_HEAD(&ap->list);
272 list_add(&p->list, &ap->list);
273
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)]);
278}
279
280/*
281 * This is the second or subsequent kprobe at the address - handle
282 * the intricacies
283 * TODO: Move kcalloc outside the spinlock
284 */
285static int register_aggr_kprobe(struct kprobe *old_p, struct kprobe *p)
286{
287 int ret = 0;
288 struct kprobe *ap;
289
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);
294 } else {
295 ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC);
296 if (!ap)
297 return -ENOMEM;
298 add_aggr_kprobe(ap, old_p);
299 list_add(&p->list, &ap->list);
300 }
301 return ret;
302}
303
304/* kprobe removal house-keeping routines */
305static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags)
306{
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700307 arch_disarm_kprobe(p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700308 hlist_del(&p->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700309 spin_unlock_irqrestore(&kprobe_lock, flags);
310 arch_remove_kprobe(p);
311}
312
313static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
314 struct kprobe *p, unsigned long flags)
315{
316 list_del(&p->list);
317 if (list_empty(&old_p->list)) {
318 cleanup_kprobe(old_p, flags);
319 kfree(old_p);
320 } else
321 spin_unlock_irqrestore(&kprobe_lock, flags);
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324int register_kprobe(struct kprobe *p)
325{
326 int ret = 0;
327 unsigned long flags = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700328 struct kprobe *old_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if ((ret = arch_prepare_kprobe(p)) != 0) {
331 goto rm_kprobe;
332 }
333 spin_lock_irqsave(&kprobe_lock, flags);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700334 old_p = get_kprobe(p->addr);
335 if (old_p) {
336 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto out;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700340 arch_copy_kprobe(p);
341 INIT_HLIST_NODE(&p->hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 hlist_add_head(&p->hlist,
343 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
344
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700345 arch_arm_kprobe(p);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347out:
348 spin_unlock_irqrestore(&kprobe_lock, flags);
349rm_kprobe:
350 if (ret == -EEXIST)
351 arch_remove_kprobe(p);
352 return ret;
353}
354
355void unregister_kprobe(struct kprobe *p)
356{
357 unsigned long flags;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700358 struct kprobe *old_p;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 spin_lock_irqsave(&kprobe_lock, flags);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700361 old_p = get_kprobe(p->addr);
362 if (old_p) {
363 if (old_p->pre_handler == aggr_pre_handler)
364 cleanup_aggr_kprobe(old_p, p, flags);
365 else
366 cleanup_kprobe(p, flags);
367 } else
Prasanna S Panchamukhi04dea5f2005-05-05 16:15:41 -0700368 spin_unlock_irqrestore(&kprobe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371static struct notifier_block kprobe_exceptions_nb = {
372 .notifier_call = kprobe_exceptions_notify,
373 .priority = 0x7fffffff /* we need to notified first */
374};
375
376int register_jprobe(struct jprobe *jp)
377{
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;
381
382 return register_kprobe(&jp->kp);
383}
384
385void unregister_jprobe(struct jprobe *jp)
386{
387 unregister_kprobe(&jp->kp);
388}
389
Hien Nguyenb94cce92005-06-23 00:09:19 -0700390#ifdef ARCH_SUPPORTS_KRETPROBES
391
392int register_kretprobe(struct kretprobe *rp)
393{
394 int ret = 0;
395 struct kretprobe_instance *inst;
396 int i;
397
398 rp->kp.pre_handler = pre_handler_kretprobe;
399
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);
404#else
405 rp->maxactive = NR_CPUS;
406#endif
407 }
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);
412 if (inst == NULL) {
413 free_rp_inst(rp);
414 return -ENOMEM;
415 }
416 INIT_HLIST_NODE(&inst->uflist);
417 hlist_add_head(&inst->uflist, &rp->free_instances);
418 }
419
420 rp->nmissed = 0;
421 /* Establish function entry probe point */
422 if ((ret = register_kprobe(&rp->kp)) != 0)
423 free_rp_inst(rp);
424 return ret;
425}
426
427#else /* ARCH_SUPPORTS_KRETPROBES */
428
429int register_kretprobe(struct kretprobe *rp)
430{
431 return -ENOSYS;
432}
433
434#endif /* ARCH_SUPPORTS_KRETPROBES */
435
436void unregister_kretprobe(struct kretprobe *rp)
437{
438 unsigned long flags;
439 struct kretprobe_instance *ri;
440
441 unregister_kprobe(&rp->kp);
442 /* No race here */
443 spin_lock_irqsave(&kprobe_lock, flags);
444 free_rp_inst(rp);
445 while ((ri = get_used_rp_inst(rp)) != NULL) {
446 ri->rp = NULL;
447 hlist_del(&ri->uflist);
448 }
449 spin_unlock_irqrestore(&kprobe_lock, flags);
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static int __init init_kprobes(void)
453{
454 int i, err = 0;
455
456 /* FIXME allocate the probe table, currently defined statically */
457 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700458 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700460 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 err = register_die_notifier(&kprobe_exceptions_nb);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700464 /* Register the trampoline probe for return probe */
465 register_kprobe(&trampoline_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return err;
467}
468
469__initcall(init_kprobes);
470
471EXPORT_SYMBOL_GPL(register_kprobe);
472EXPORT_SYMBOL_GPL(unregister_kprobe);
473EXPORT_SYMBOL_GPL(register_jprobe);
474EXPORT_SYMBOL_GPL(unregister_jprobe);
475EXPORT_SYMBOL_GPL(jprobe_return);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700476EXPORT_SYMBOL_GPL(register_kretprobe);
477EXPORT_SYMBOL_GPL(unregister_kretprobe);
478