blob: f3ea492ab44dfcfc05e2fad4027e303f9b69d9dd [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>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070039#include <linux/moduleloader.h>
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -070040#include <asm-generic/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/cacheflush.h>
42#include <asm/errno.h>
43#include <asm/kdebug.h>
44
45#define KPROBE_HASH_BITS 6
46#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
47
48static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070049static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51unsigned int kprobe_cpu = NR_CPUS;
52static DEFINE_SPINLOCK(kprobe_lock);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -070053static struct kprobe *curr_kprobe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070055/*
56 * kprobe->ainsn.insn points to the copy of the instruction to be
57 * single-stepped. x86_64, POWER4 and above have no-exec support and
58 * stepping on the instruction on a vmalloced/kmalloced/data page
59 * is a recipe for disaster
60 */
61#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
62
63struct kprobe_insn_page {
64 struct hlist_node hlist;
65 kprobe_opcode_t *insns; /* Page of instruction slots */
66 char slot_used[INSNS_PER_PAGE];
67 int nused;
68};
69
70static struct hlist_head kprobe_insn_pages;
71
72/**
73 * get_insn_slot() - Find a slot on an executable page for an instruction.
74 * We allocate an executable page if there's no room on existing ones.
75 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -070076kprobe_opcode_t __kprobes *get_insn_slot(void)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070077{
78 struct kprobe_insn_page *kip;
79 struct hlist_node *pos;
80
81 hlist_for_each(pos, &kprobe_insn_pages) {
82 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
83 if (kip->nused < INSNS_PER_PAGE) {
84 int i;
85 for (i = 0; i < INSNS_PER_PAGE; i++) {
86 if (!kip->slot_used[i]) {
87 kip->slot_used[i] = 1;
88 kip->nused++;
89 return kip->insns + (i * MAX_INSN_SIZE);
90 }
91 }
92 /* Surprise! No unused slots. Fix kip->nused. */
93 kip->nused = INSNS_PER_PAGE;
94 }
95 }
96
97 /* All out of space. Need to allocate a new page. Use slot 0.*/
98 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
99 if (!kip) {
100 return NULL;
101 }
102
103 /*
104 * Use module_alloc so this page is within +/- 2GB of where the
105 * kernel image and loaded module images reside. This is required
106 * so x86_64 can correctly handle the %rip-relative fixups.
107 */
108 kip->insns = module_alloc(PAGE_SIZE);
109 if (!kip->insns) {
110 kfree(kip);
111 return NULL;
112 }
113 INIT_HLIST_NODE(&kip->hlist);
114 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
115 memset(kip->slot_used, 0, INSNS_PER_PAGE);
116 kip->slot_used[0] = 1;
117 kip->nused = 1;
118 return kip->insns;
119}
120
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700121void __kprobes free_insn_slot(kprobe_opcode_t *slot)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700122{
123 struct kprobe_insn_page *kip;
124 struct hlist_node *pos;
125
126 hlist_for_each(pos, &kprobe_insn_pages) {
127 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
128 if (kip->insns <= slot &&
129 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
130 int i = (slot - kip->insns) / MAX_INSN_SIZE;
131 kip->slot_used[i] = 0;
132 kip->nused--;
133 if (kip->nused == 0) {
134 /*
135 * Page is no longer in use. Free it unless
136 * it's the last one. We keep the last one
137 * so as not to have to set it up again the
138 * next time somebody inserts a probe.
139 */
140 hlist_del(&kip->hlist);
141 if (hlist_empty(&kprobe_insn_pages)) {
142 INIT_HLIST_NODE(&kip->hlist);
143 hlist_add_head(&kip->hlist,
144 &kprobe_insn_pages);
145 } else {
146 module_free(NULL, kip->insns);
147 kfree(kip);
148 }
149 }
150 return;
151 }
152 }
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Locks kprobe: irqs must be disabled */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700156void __kprobes lock_kprobes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700158 unsigned long flags = 0;
159
160 /* Avoiding local interrupts to happen right after we take the kprobe_lock
161 * and before we get a chance to update kprobe_cpu, this to prevent
162 * deadlock when we have a kprobe on ISR routine and a kprobe on task
163 * routine
164 */
165 local_irq_save(flags);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 spin_lock(&kprobe_lock);
168 kprobe_cpu = smp_processor_id();
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700169
170 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700173void __kprobes unlock_kprobes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700175 unsigned long flags = 0;
176
177 /* Avoiding local interrupts to happen right after we update
178 * kprobe_cpu and before we get a a chance to release kprobe_lock,
179 * this to prevent deadlock when we have a kprobe on ISR routine and
180 * a kprobe on task routine
181 */
182 local_irq_save(flags);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 kprobe_cpu = NR_CPUS;
185 spin_unlock(&kprobe_lock);
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700186
187 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/* You have to be holding the kprobe_lock */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700191struct kprobe __kprobes *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct hlist_head *head;
194 struct hlist_node *node;
195
196 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
197 hlist_for_each(node, head) {
198 struct kprobe *p = hlist_entry(node, struct kprobe, hlist);
199 if (p->addr == addr)
200 return p;
201 }
202 return NULL;
203}
204
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700205/*
206 * Aggregate handlers for multiple kprobes support - these handlers
207 * take care of invoking the individual kprobe handlers on p->list
208 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700209static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700210{
211 struct kprobe *kp;
212
213 list_for_each_entry(kp, &p->list, list) {
214 if (kp->pre_handler) {
215 curr_kprobe = kp;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700216 if (kp->pre_handler(kp, regs))
217 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700218 }
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700219 curr_kprobe = NULL;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700220 }
221 return 0;
222}
223
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700224static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
225 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700226{
227 struct kprobe *kp;
228
229 list_for_each_entry(kp, &p->list, list) {
230 if (kp->post_handler) {
231 curr_kprobe = kp;
232 kp->post_handler(kp, regs, flags);
233 curr_kprobe = NULL;
234 }
235 }
236 return;
237}
238
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700239static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
240 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700241{
242 /*
243 * if we faulted "during" the execution of a user specified
244 * probe handler, invoke just that probe's fault handler
245 */
246 if (curr_kprobe && curr_kprobe->fault_handler) {
247 if (curr_kprobe->fault_handler(curr_kprobe, regs, trapnr))
248 return 1;
249 }
250 return 0;
251}
252
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700253static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700254{
255 struct kprobe *kp = curr_kprobe;
256 if (curr_kprobe && kp->break_handler) {
257 if (kp->break_handler(kp, regs)) {
258 curr_kprobe = NULL;
259 return 1;
260 }
261 }
262 curr_kprobe = NULL;
263 return 0;
264}
265
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700266struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700267{
268 struct hlist_node *node;
269 struct kretprobe_instance *ri;
270 hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
271 return ri;
272 return NULL;
273}
274
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700275static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe
276 *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700277{
278 struct hlist_node *node;
279 struct kretprobe_instance *ri;
280 hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
281 return ri;
282 return NULL;
283}
284
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700285void __kprobes add_rp_inst(struct kretprobe_instance *ri)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700286{
Hien Nguyenb94cce92005-06-23 00:09:19 -0700287 /*
288 * Remove rp inst off the free list -
289 * Add it back when probed function returns
290 */
291 hlist_del(&ri->uflist);
Rusty Lynch802eae72005-06-27 15:17:08 -0700292
Hien Nguyenb94cce92005-06-23 00:09:19 -0700293 /* Add rp inst onto table */
294 INIT_HLIST_NODE(&ri->hlist);
295 hlist_add_head(&ri->hlist,
Rusty Lynch802eae72005-06-27 15:17:08 -0700296 &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700297
298 /* Also add this rp inst to the used list. */
299 INIT_HLIST_NODE(&ri->uflist);
300 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
301}
302
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700303void __kprobes recycle_rp_inst(struct kretprobe_instance *ri)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700304{
305 /* remove rp inst off the rprobe_inst_table */
306 hlist_del(&ri->hlist);
307 if (ri->rp) {
308 /* remove rp inst off the used list */
309 hlist_del(&ri->uflist);
310 /* put rp inst back onto the free list */
311 INIT_HLIST_NODE(&ri->uflist);
312 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
313 } else
314 /* Unregistering */
315 kfree(ri);
316}
317
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700318struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700319{
320 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
321}
322
Hien Nguyenb94cce92005-06-23 00:09:19 -0700323/*
Rusty Lynch802eae72005-06-27 15:17:08 -0700324 * This function is called from exit_thread or flush_thread when task tk's
325 * stack is being recycled so that we can recycle any function-return probe
326 * instances associated with this task. These left over instances represent
327 * probed functions that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -0700328 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700329void __kprobes kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700330{
Rusty Lynch802eae72005-06-27 15:17:08 -0700331 struct kretprobe_instance *ri;
332 struct hlist_head *head;
333 struct hlist_node *node, *tmp;
Hien Nguyen0aa55e42005-06-23 00:09:26 -0700334 unsigned long flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -0700335
Hien Nguyen0aa55e42005-06-23 00:09:26 -0700336 spin_lock_irqsave(&kprobe_lock, flags);
Rusty Lynch802eae72005-06-27 15:17:08 -0700337 head = kretprobe_inst_table_head(current);
338 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
339 if (ri->task == tk)
340 recycle_rp_inst(ri);
341 }
Hien Nguyen0aa55e42005-06-23 00:09:26 -0700342 spin_unlock_irqrestore(&kprobe_lock, flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700343}
344
345/*
346 * This kprobe pre_handler is registered with every kretprobe. When probe
347 * hits it will set up the return probe.
348 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700349static int __kprobes pre_handler_kretprobe(struct kprobe *p,
350 struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700351{
352 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
353
354 /*TODO: consider to only swap the RA after the last pre_handler fired */
355 arch_prepare_kretprobe(rp, regs);
356 return 0;
357}
358
359static inline void free_rp_inst(struct kretprobe *rp)
360{
361 struct kretprobe_instance *ri;
362 while ((ri = get_free_rp_inst(rp)) != NULL) {
363 hlist_del(&ri->uflist);
364 kfree(ri);
365 }
366}
367
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700368/*
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700369 * Keep all fields in the kprobe consistent
370 */
371static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
372{
373 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
374 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
375}
376
377/*
378* Add the new probe to old_p->list. Fail if this is the
379* second jprobe at the address - two jprobes can't coexist
380*/
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700381static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700382{
383 struct kprobe *kp;
384
385 if (p->break_handler) {
386 list_for_each_entry(kp, &old_p->list, list) {
387 if (kp->break_handler)
388 return -EEXIST;
389 }
390 list_add_tail(&p->list, &old_p->list);
391 } else
392 list_add(&p->list, &old_p->list);
393 return 0;
394}
395
396/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700397 * Fill in the required fields of the "manager kprobe". Replace the
398 * earlier kprobe in the hlist with the manager kprobe
399 */
400static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
401{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700402 copy_kprobe(p, ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700403 ap->addr = p->addr;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700404 ap->pre_handler = aggr_pre_handler;
405 ap->post_handler = aggr_post_handler;
406 ap->fault_handler = aggr_fault_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700407 ap->break_handler = aggr_break_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700408
409 INIT_LIST_HEAD(&ap->list);
410 list_add(&p->list, &ap->list);
411
412 INIT_HLIST_NODE(&ap->hlist);
413 hlist_del(&p->hlist);
414 hlist_add_head(&ap->hlist,
415 &kprobe_table[hash_ptr(ap->addr, KPROBE_HASH_BITS)]);
416}
417
418/*
419 * This is the second or subsequent kprobe at the address - handle
420 * the intricacies
421 * TODO: Move kcalloc outside the spinlock
422 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700423static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
424 struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700425{
426 int ret = 0;
427 struct kprobe *ap;
428
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700429 if (old_p->pre_handler == aggr_pre_handler) {
430 copy_kprobe(old_p, p);
431 ret = add_new_kprobe(old_p, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700432 } else {
433 ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC);
434 if (!ap)
435 return -ENOMEM;
436 add_aggr_kprobe(ap, old_p);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700437 copy_kprobe(ap, p);
438 ret = add_new_kprobe(ap, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700439 }
440 return ret;
441}
442
443/* kprobe removal house-keeping routines */
444static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags)
445{
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700446 arch_disarm_kprobe(p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700447 hlist_del(&p->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700448 spin_unlock_irqrestore(&kprobe_lock, flags);
449 arch_remove_kprobe(p);
450}
451
452static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
453 struct kprobe *p, unsigned long flags)
454{
455 list_del(&p->list);
456 if (list_empty(&old_p->list)) {
457 cleanup_kprobe(old_p, flags);
458 kfree(old_p);
459 } else
460 spin_unlock_irqrestore(&kprobe_lock, flags);
461}
462
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700463static int __kprobes in_kprobes_functions(unsigned long addr)
464{
465 if (addr >= (unsigned long)__kprobes_text_start
466 && addr < (unsigned long)__kprobes_text_end)
467 return -EINVAL;
468 return 0;
469}
470
471int __kprobes register_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int ret = 0;
474 unsigned long flags = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700475 struct kprobe *old_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700477 if ((ret = in_kprobes_functions((unsigned long) p->addr)) != 0)
478 return ret;
479 if ((ret = arch_prepare_kprobe(p)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto rm_kprobe;
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 spin_lock_irqsave(&kprobe_lock, flags);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700483 old_p = get_kprobe(p->addr);
Prasanna S Panchamukhiea32c652005-06-23 00:09:36 -0700484 p->nmissed = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700485 if (old_p) {
486 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto out;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700490 arch_copy_kprobe(p);
491 INIT_HLIST_NODE(&p->hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 hlist_add_head(&p->hlist,
493 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
494
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700495 arch_arm_kprobe(p);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497out:
498 spin_unlock_irqrestore(&kprobe_lock, flags);
499rm_kprobe:
500 if (ret == -EEXIST)
501 arch_remove_kprobe(p);
502 return ret;
503}
504
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700505void __kprobes unregister_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 unsigned long flags;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700508 struct kprobe *old_p;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 spin_lock_irqsave(&kprobe_lock, flags);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700511 old_p = get_kprobe(p->addr);
512 if (old_p) {
513 if (old_p->pre_handler == aggr_pre_handler)
514 cleanup_aggr_kprobe(old_p, p, flags);
515 else
516 cleanup_kprobe(p, flags);
517 } else
Prasanna S Panchamukhi04dea5f2005-05-05 16:15:41 -0700518 spin_unlock_irqrestore(&kprobe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static struct notifier_block kprobe_exceptions_nb = {
522 .notifier_call = kprobe_exceptions_notify,
523 .priority = 0x7fffffff /* we need to notified first */
524};
525
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700526int __kprobes register_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 /* Todo: Verify probepoint is a function entry point */
529 jp->kp.pre_handler = setjmp_pre_handler;
530 jp->kp.break_handler = longjmp_break_handler;
531
532 return register_kprobe(&jp->kp);
533}
534
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700535void __kprobes unregister_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 unregister_kprobe(&jp->kp);
538}
539
Hien Nguyenb94cce92005-06-23 00:09:19 -0700540#ifdef ARCH_SUPPORTS_KRETPROBES
541
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700542int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700543{
544 int ret = 0;
545 struct kretprobe_instance *inst;
546 int i;
547
548 rp->kp.pre_handler = pre_handler_kretprobe;
549
550 /* Pre-allocate memory for max kretprobe instances */
551 if (rp->maxactive <= 0) {
552#ifdef CONFIG_PREEMPT
553 rp->maxactive = max(10, 2 * NR_CPUS);
554#else
555 rp->maxactive = NR_CPUS;
556#endif
557 }
558 INIT_HLIST_HEAD(&rp->used_instances);
559 INIT_HLIST_HEAD(&rp->free_instances);
560 for (i = 0; i < rp->maxactive; i++) {
561 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
562 if (inst == NULL) {
563 free_rp_inst(rp);
564 return -ENOMEM;
565 }
566 INIT_HLIST_NODE(&inst->uflist);
567 hlist_add_head(&inst->uflist, &rp->free_instances);
568 }
569
570 rp->nmissed = 0;
571 /* Establish function entry probe point */
572 if ((ret = register_kprobe(&rp->kp)) != 0)
573 free_rp_inst(rp);
574 return ret;
575}
576
577#else /* ARCH_SUPPORTS_KRETPROBES */
578
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700579int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700580{
581 return -ENOSYS;
582}
583
584#endif /* ARCH_SUPPORTS_KRETPROBES */
585
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700586void __kprobes unregister_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700587{
588 unsigned long flags;
589 struct kretprobe_instance *ri;
590
591 unregister_kprobe(&rp->kp);
592 /* No race here */
593 spin_lock_irqsave(&kprobe_lock, flags);
594 free_rp_inst(rp);
595 while ((ri = get_used_rp_inst(rp)) != NULL) {
596 ri->rp = NULL;
597 hlist_del(&ri->uflist);
598 }
599 spin_unlock_irqrestore(&kprobe_lock, flags);
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static int __init init_kprobes(void)
603{
604 int i, err = 0;
605
606 /* FIXME allocate the probe table, currently defined statically */
607 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700608 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700610 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Rusty Lynch67729262005-07-05 18:54:50 -0700613 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -0700614 if (!err)
615 err = register_die_notifier(&kprobe_exceptions_nb);
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return err;
618}
619
620__initcall(init_kprobes);
621
622EXPORT_SYMBOL_GPL(register_kprobe);
623EXPORT_SYMBOL_GPL(unregister_kprobe);
624EXPORT_SYMBOL_GPL(register_jprobe);
625EXPORT_SYMBOL_GPL(unregister_jprobe);
626EXPORT_SYMBOL_GPL(jprobe_return);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700627EXPORT_SYMBOL_GPL(register_kretprobe);
628EXPORT_SYMBOL_GPL(unregister_kretprobe);
629