blob: 692fbf75ab49d1d8ae9fd01a582b84b93af8e756 [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;
264 ap->opcode = p->opcode;
265 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{
307 *p->addr = p->opcode;
308 hlist_del(&p->hlist);
309 flush_icache_range((unsigned long) p->addr,
310 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
311 spin_unlock_irqrestore(&kprobe_lock, flags);
312 arch_remove_kprobe(p);
313}
314
315static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
316 struct kprobe *p, unsigned long flags)
317{
318 list_del(&p->list);
319 if (list_empty(&old_p->list)) {
320 cleanup_kprobe(old_p, flags);
321 kfree(old_p);
322 } else
323 spin_unlock_irqrestore(&kprobe_lock, flags);
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326int register_kprobe(struct kprobe *p)
327{
328 int ret = 0;
329 unsigned long flags = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700330 struct kprobe *old_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if ((ret = arch_prepare_kprobe(p)) != 0) {
333 goto rm_kprobe;
334 }
335 spin_lock_irqsave(&kprobe_lock, flags);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700336 old_p = get_kprobe(p->addr);
337 if (old_p) {
338 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 goto out;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700342 arch_copy_kprobe(p);
343 INIT_HLIST_NODE(&p->hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 hlist_add_head(&p->hlist,
345 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
346
347 p->opcode = *p->addr;
348 *p->addr = BREAKPOINT_INSTRUCTION;
349 flush_icache_range((unsigned long) p->addr,
350 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
351out:
352 spin_unlock_irqrestore(&kprobe_lock, flags);
353rm_kprobe:
354 if (ret == -EEXIST)
355 arch_remove_kprobe(p);
356 return ret;
357}
358
359void unregister_kprobe(struct kprobe *p)
360{
361 unsigned long flags;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700362 struct kprobe *old_p;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 spin_lock_irqsave(&kprobe_lock, flags);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700365 old_p = get_kprobe(p->addr);
366 if (old_p) {
367 if (old_p->pre_handler == aggr_pre_handler)
368 cleanup_aggr_kprobe(old_p, p, flags);
369 else
370 cleanup_kprobe(p, flags);
371 } else
Prasanna S Panchamukhi04dea5f2005-05-05 16:15:41 -0700372 spin_unlock_irqrestore(&kprobe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static struct notifier_block kprobe_exceptions_nb = {
376 .notifier_call = kprobe_exceptions_notify,
377 .priority = 0x7fffffff /* we need to notified first */
378};
379
380int register_jprobe(struct jprobe *jp)
381{
382 /* Todo: Verify probepoint is a function entry point */
383 jp->kp.pre_handler = setjmp_pre_handler;
384 jp->kp.break_handler = longjmp_break_handler;
385
386 return register_kprobe(&jp->kp);
387}
388
389void unregister_jprobe(struct jprobe *jp)
390{
391 unregister_kprobe(&jp->kp);
392}
393
Hien Nguyenb94cce92005-06-23 00:09:19 -0700394#ifdef ARCH_SUPPORTS_KRETPROBES
395
396int register_kretprobe(struct kretprobe *rp)
397{
398 int ret = 0;
399 struct kretprobe_instance *inst;
400 int i;
401
402 rp->kp.pre_handler = pre_handler_kretprobe;
403
404 /* Pre-allocate memory for max kretprobe instances */
405 if (rp->maxactive <= 0) {
406#ifdef CONFIG_PREEMPT
407 rp->maxactive = max(10, 2 * NR_CPUS);
408#else
409 rp->maxactive = NR_CPUS;
410#endif
411 }
412 INIT_HLIST_HEAD(&rp->used_instances);
413 INIT_HLIST_HEAD(&rp->free_instances);
414 for (i = 0; i < rp->maxactive; i++) {
415 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
416 if (inst == NULL) {
417 free_rp_inst(rp);
418 return -ENOMEM;
419 }
420 INIT_HLIST_NODE(&inst->uflist);
421 hlist_add_head(&inst->uflist, &rp->free_instances);
422 }
423
424 rp->nmissed = 0;
425 /* Establish function entry probe point */
426 if ((ret = register_kprobe(&rp->kp)) != 0)
427 free_rp_inst(rp);
428 return ret;
429}
430
431#else /* ARCH_SUPPORTS_KRETPROBES */
432
433int register_kretprobe(struct kretprobe *rp)
434{
435 return -ENOSYS;
436}
437
438#endif /* ARCH_SUPPORTS_KRETPROBES */
439
440void unregister_kretprobe(struct kretprobe *rp)
441{
442 unsigned long flags;
443 struct kretprobe_instance *ri;
444
445 unregister_kprobe(&rp->kp);
446 /* No race here */
447 spin_lock_irqsave(&kprobe_lock, flags);
448 free_rp_inst(rp);
449 while ((ri = get_used_rp_inst(rp)) != NULL) {
450 ri->rp = NULL;
451 hlist_del(&ri->uflist);
452 }
453 spin_unlock_irqrestore(&kprobe_lock, flags);
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static int __init init_kprobes(void)
457{
458 int i, err = 0;
459
460 /* FIXME allocate the probe table, currently defined statically */
461 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700462 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700464 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 err = register_die_notifier(&kprobe_exceptions_nb);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700468 /* Register the trampoline probe for return probe */
469 register_kprobe(&trampoline_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return err;
471}
472
473__initcall(init_kprobes);
474
475EXPORT_SYMBOL_GPL(register_kprobe);
476EXPORT_SYMBOL_GPL(unregister_kprobe);
477EXPORT_SYMBOL_GPL(register_jprobe);
478EXPORT_SYMBOL_GPL(unregister_jprobe);
479EXPORT_SYMBOL_GPL(jprobe_return);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700480EXPORT_SYMBOL_GPL(register_kretprobe);
481EXPORT_SYMBOL_GPL(unregister_kretprobe);
482