blob: 22857003a65bf95b1858060d956bcb1293d673f6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/hash.h>
36#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080037#include <linux/slab.h>
Randy Dunlape3869792007-05-08 00:27:01 -070038#include <linux/stddef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070040#include <linux/moduleloader.h>
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070041#include <linux/kallsyms.h>
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080042#include <linux/freezer.h>
Srinivasa Ds346fd592007-02-20 13:57:54 -080043#include <linux/seq_file.h>
44#include <linux/debugfs.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070045#include <linux/kdebug.h>
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -070046#include <asm-generic/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/cacheflush.h>
48#include <asm/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define KPROBE_HASH_BITS 6
51#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
52
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070053
54/*
55 * Some oddball architectures like 64bit powerpc have function descriptors
56 * so this must be overridable.
57 */
58#ifndef kprobe_lookup_name
59#define kprobe_lookup_name(name, addr) \
60 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
61#endif
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070064static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -070065static atomic_t kprobe_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -080067DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -080068DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080069static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -070071static struct notifier_block kprobe_page_fault_nb = {
72 .notifier_call = kprobe_exceptions_notify,
73 .priority = 0x7fffffff /* we need to notified first */
74};
75
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080076#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070077/*
78 * kprobe->ainsn.insn points to the copy of the instruction to be
79 * single-stepped. x86_64, POWER4 and above have no-exec support and
80 * stepping on the instruction on a vmalloced/kmalloced/data page
81 * is a recipe for disaster
82 */
83#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
84
85struct kprobe_insn_page {
86 struct hlist_node hlist;
87 kprobe_opcode_t *insns; /* Page of instruction slots */
88 char slot_used[INSNS_PER_PAGE];
89 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080090 int ngarbage;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070091};
92
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -080093enum kprobe_slot_state {
94 SLOT_CLEAN = 0,
95 SLOT_DIRTY = 1,
96 SLOT_USED = 2,
97};
98
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070099static struct hlist_head kprobe_insn_pages;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800100static int kprobe_garbage_slots;
101static int collect_garbage_slots(void);
102
103static int __kprobes check_safety(void)
104{
105 int ret = 0;
106#if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
107 ret = freeze_processes();
108 if (ret == 0) {
109 struct task_struct *p, *q;
110 do_each_thread(p, q) {
111 if (p != current && p->state == TASK_RUNNING &&
112 p->pid != 0) {
113 printk("Check failed: %s is running\n",p->comm);
114 ret = -1;
115 goto loop_end;
116 }
117 } while_each_thread(p, q);
118 }
119loop_end:
120 thaw_processes();
121#else
122 synchronize_sched();
123#endif
124 return ret;
125}
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700126
127/**
128 * get_insn_slot() - Find a slot on an executable page for an instruction.
129 * We allocate an executable page if there's no room on existing ones.
130 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700131kprobe_opcode_t __kprobes *get_insn_slot(void)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700132{
133 struct kprobe_insn_page *kip;
134 struct hlist_node *pos;
135
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700136 retry:
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700137 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700138 if (kip->nused < INSNS_PER_PAGE) {
139 int i;
140 for (i = 0; i < INSNS_PER_PAGE; i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800141 if (kip->slot_used[i] == SLOT_CLEAN) {
142 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700143 kip->nused++;
144 return kip->insns + (i * MAX_INSN_SIZE);
145 }
146 }
147 /* Surprise! No unused slots. Fix kip->nused. */
148 kip->nused = INSNS_PER_PAGE;
149 }
150 }
151
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800152 /* If there are any garbage slots, collect it and try again. */
153 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
154 goto retry;
155 }
156 /* All out of space. Need to allocate a new page. Use slot 0. */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700157 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700158 if (!kip)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700159 return NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700160
161 /*
162 * Use module_alloc so this page is within +/- 2GB of where the
163 * kernel image and loaded module images reside. This is required
164 * so x86_64 can correctly handle the %rip-relative fixups.
165 */
166 kip->insns = module_alloc(PAGE_SIZE);
167 if (!kip->insns) {
168 kfree(kip);
169 return NULL;
170 }
171 INIT_HLIST_NODE(&kip->hlist);
172 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800173 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
174 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700175 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800176 kip->ngarbage = 0;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700177 return kip->insns;
178}
179
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800180/* Return 1 if all garbages are collected, otherwise 0. */
181static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
182{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800183 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800184 kip->nused--;
185 if (kip->nused == 0) {
186 /*
187 * Page is no longer in use. Free it unless
188 * it's the last one. We keep the last one
189 * so as not to have to set it up again the
190 * next time somebody inserts a probe.
191 */
192 hlist_del(&kip->hlist);
193 if (hlist_empty(&kprobe_insn_pages)) {
194 INIT_HLIST_NODE(&kip->hlist);
195 hlist_add_head(&kip->hlist,
196 &kprobe_insn_pages);
197 } else {
198 module_free(NULL, kip->insns);
199 kfree(kip);
200 }
201 return 1;
202 }
203 return 0;
204}
205
206static int __kprobes collect_garbage_slots(void)
207{
208 struct kprobe_insn_page *kip;
209 struct hlist_node *pos, *next;
210
211 /* Ensure no-one is preepmted on the garbages */
212 if (check_safety() != 0)
213 return -EAGAIN;
214
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700215 hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800216 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800217 if (kip->ngarbage == 0)
218 continue;
219 kip->ngarbage = 0; /* we will collect all garbages */
220 for (i = 0; i < INSNS_PER_PAGE; i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800221 if (kip->slot_used[i] == SLOT_DIRTY &&
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800222 collect_one_slot(kip, i))
223 break;
224 }
225 }
226 kprobe_garbage_slots = 0;
227 return 0;
228}
229
230void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700231{
232 struct kprobe_insn_page *kip;
233 struct hlist_node *pos;
234
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700235 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700236 if (kip->insns <= slot &&
237 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
238 int i = (slot - kip->insns) / MAX_INSN_SIZE;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800239 if (dirty) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800240 kip->slot_used[i] = SLOT_DIRTY;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800241 kip->ngarbage++;
242 } else {
243 collect_one_slot(kip, i);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700244 }
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800245 break;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700246 }
247 }
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700248
249 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800250 collect_garbage_slots();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700251}
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800252#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700253
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800254/* We have preemption disabled.. so it is safe to use __ versions */
255static inline void set_kprobe_instance(struct kprobe *kp)
256{
257 __get_cpu_var(kprobe_instance) = kp;
258}
259
260static inline void reset_kprobe_instance(void)
261{
262 __get_cpu_var(kprobe_instance) = NULL;
263}
264
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800265/*
266 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800267 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800268 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800269 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800270 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700271struct kprobe __kprobes *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct hlist_head *head;
274 struct hlist_node *node;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800275 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800278 hlist_for_each_entry_rcu(p, node, head, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (p->addr == addr)
280 return p;
281 }
282 return NULL;
283}
284
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700285/*
286 * Aggregate handlers for multiple kprobes support - these handlers
287 * take care of invoking the individual kprobe handlers on p->list
288 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700289static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700290{
291 struct kprobe *kp;
292
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800293 list_for_each_entry_rcu(kp, &p->list, list) {
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700294 if (kp->pre_handler) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800295 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700296 if (kp->pre_handler(kp, regs))
297 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700298 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800299 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700300 }
301 return 0;
302}
303
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700304static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
305 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700306{
307 struct kprobe *kp;
308
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800309 list_for_each_entry_rcu(kp, &p->list, list) {
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700310 if (kp->post_handler) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800311 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700312 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800313 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700314 }
315 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700316}
317
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700318static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
319 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700320{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800321 struct kprobe *cur = __get_cpu_var(kprobe_instance);
322
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700323 /*
324 * if we faulted "during" the execution of a user specified
325 * probe handler, invoke just that probe's fault handler
326 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800327 if (cur && cur->fault_handler) {
328 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700329 return 1;
330 }
331 return 0;
332}
333
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700334static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700335{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800336 struct kprobe *cur = __get_cpu_var(kprobe_instance);
337 int ret = 0;
338
339 if (cur && cur->break_handler) {
340 if (cur->break_handler(cur, regs))
341 ret = 1;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700342 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800343 reset_kprobe_instance();
344 return ret;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700345}
346
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800347/* Walks the list and increments nmissed count for multiprobe case */
348void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
349{
350 struct kprobe *kp;
351 if (p->pre_handler != aggr_pre_handler) {
352 p->nmissed++;
353 } else {
354 list_for_each_entry_rcu(kp, &p->list, list)
355 kp->nmissed++;
356 }
357 return;
358}
359
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800360/* Called with kretprobe_lock held */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700361struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700362{
363 struct hlist_node *node;
364 struct kretprobe_instance *ri;
365 hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
366 return ri;
367 return NULL;
368}
369
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800370/* Called with kretprobe_lock held */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700371static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe
372 *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700373{
374 struct hlist_node *node;
375 struct kretprobe_instance *ri;
376 hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
377 return ri;
378 return NULL;
379}
380
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800381/* Called with kretprobe_lock held */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700382void __kprobes add_rp_inst(struct kretprobe_instance *ri)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700383{
Hien Nguyenb94cce92005-06-23 00:09:19 -0700384 /*
385 * Remove rp inst off the free list -
386 * Add it back when probed function returns
387 */
388 hlist_del(&ri->uflist);
Rusty Lynch802eae72005-06-27 15:17:08 -0700389
Hien Nguyenb94cce92005-06-23 00:09:19 -0700390 /* Add rp inst onto table */
391 INIT_HLIST_NODE(&ri->hlist);
392 hlist_add_head(&ri->hlist,
Rusty Lynch802eae72005-06-27 15:17:08 -0700393 &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700394
395 /* Also add this rp inst to the used list. */
396 INIT_HLIST_NODE(&ri->uflist);
397 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
398}
399
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800400/* Called with kretprobe_lock held */
bibo,mao99219a32006-10-02 02:17:35 -0700401void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
402 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700403{
404 /* remove rp inst off the rprobe_inst_table */
405 hlist_del(&ri->hlist);
406 if (ri->rp) {
407 /* remove rp inst off the used list */
408 hlist_del(&ri->uflist);
409 /* put rp inst back onto the free list */
410 INIT_HLIST_NODE(&ri->uflist);
411 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
412 } else
413 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -0700414 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700415}
416
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700417struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700418{
419 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
420}
421
Hien Nguyenb94cce92005-06-23 00:09:19 -0700422/*
bibo maoc6fd91f2006-03-26 01:38:20 -0800423 * This function is called from finish_task_switch when task tk becomes dead,
424 * so that we can recycle any function-return probe instances associated
425 * with this task. These left over instances represent probed functions
426 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -0700427 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700428void __kprobes kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700429{
bibo,mao62c27be2006-10-02 02:17:33 -0700430 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -0700431 struct hlist_head *head, empty_rp;
Rusty Lynch802eae72005-06-27 15:17:08 -0700432 struct hlist_node *node, *tmp;
Hien Nguyen0aa55e42005-06-23 00:09:26 -0700433 unsigned long flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -0700434
bibo,mao99219a32006-10-02 02:17:35 -0700435 INIT_HLIST_HEAD(&empty_rp);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800436 spin_lock_irqsave(&kretprobe_lock, flags);
bibo,mao62c27be2006-10-02 02:17:33 -0700437 head = kretprobe_inst_table_head(tk);
438 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
439 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -0700440 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -0700441 }
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800442 spin_unlock_irqrestore(&kretprobe_lock, flags);
bibo,mao99219a32006-10-02 02:17:35 -0700443
444 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
445 hlist_del(&ri->hlist);
446 kfree(ri);
447 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700448}
449
Hien Nguyenb94cce92005-06-23 00:09:19 -0700450static inline void free_rp_inst(struct kretprobe *rp)
451{
452 struct kretprobe_instance *ri;
453 while ((ri = get_free_rp_inst(rp)) != NULL) {
454 hlist_del(&ri->uflist);
455 kfree(ri);
456 }
457}
458
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700459/*
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700460 * Keep all fields in the kprobe consistent
461 */
462static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
463{
464 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
465 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
466}
467
468/*
469* Add the new probe to old_p->list. Fail if this is the
470* second jprobe at the address - two jprobes can't coexist
471*/
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700472static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700473{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700474 if (p->break_handler) {
mao, bibo36721652006-06-26 00:25:22 -0700475 if (old_p->break_handler)
476 return -EEXIST;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800477 list_add_tail_rcu(&p->list, &old_p->list);
mao, bibo36721652006-06-26 00:25:22 -0700478 old_p->break_handler = aggr_break_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700479 } else
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800480 list_add_rcu(&p->list, &old_p->list);
mao, bibo36721652006-06-26 00:25:22 -0700481 if (p->post_handler && !old_p->post_handler)
482 old_p->post_handler = aggr_post_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700483 return 0;
484}
485
486/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700487 * Fill in the required fields of the "manager kprobe". Replace the
488 * earlier kprobe in the hlist with the manager kprobe
489 */
490static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
491{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700492 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -0700493 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700494 ap->addr = p->addr;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700495 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700496 ap->fault_handler = aggr_fault_handler;
mao, bibo36721652006-06-26 00:25:22 -0700497 if (p->post_handler)
498 ap->post_handler = aggr_post_handler;
499 if (p->break_handler)
500 ap->break_handler = aggr_break_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700501
502 INIT_LIST_HEAD(&ap->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800503 list_add_rcu(&p->list, &ap->list);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700504
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -0800505 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700506}
507
508/*
509 * This is the second or subsequent kprobe at the address - handle
510 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700511 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700512static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
513 struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700514{
515 int ret = 0;
516 struct kprobe *ap;
517
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700518 if (old_p->pre_handler == aggr_pre_handler) {
519 copy_kprobe(old_p, p);
520 ret = add_new_kprobe(old_p, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700521 } else {
Keshavamurthy Anil Sa0d50062006-01-09 20:52:46 -0800522 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700523 if (!ap)
524 return -ENOMEM;
525 add_aggr_kprobe(ap, old_p);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700526 copy_kprobe(ap, p);
527 ret = add_new_kprobe(ap, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700528 }
529 return ret;
530}
531
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700532static int __kprobes in_kprobes_functions(unsigned long addr)
533{
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700534 if (addr >= (unsigned long)__kprobes_text_start &&
535 addr < (unsigned long)__kprobes_text_end)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700536 return -EINVAL;
537 return 0;
538}
539
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800540static int __kprobes __register_kprobe(struct kprobe *p,
541 unsigned long called_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 int ret = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700544 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800545 struct module *probed_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700547 /*
548 * If we have a symbol_name argument look it up,
549 * and add it to the address. That way the addr
550 * field can either be global or relative to a symbol.
551 */
552 if (p->symbol_name) {
553 if (p->addr)
554 return -EINVAL;
555 kprobe_lookup_name(p->symbol_name, p->addr);
556 }
557
558 if (!p->addr)
559 return -EINVAL;
560 p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
561
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700562 if (!kernel_text_address((unsigned long) p->addr) ||
563 in_kprobes_functions((unsigned long) p->addr))
Mao, Bibob3e55c72005-12-12 00:37:00 -0800564 return -EINVAL;
565
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800566 p->mod_refcounted = 0;
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700567
568 /*
569 * Check if are we probing a module.
570 */
571 probed_mod = module_text_address((unsigned long) p->addr);
572 if (probed_mod) {
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800573 struct module *calling_mod = module_text_address(called_from);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700574 /*
575 * We must allow modules to probe themself and in this case
576 * avoid incrementing the module refcount, so as to allow
577 * unloading of self probing modules.
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800578 */
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700579 if (calling_mod && calling_mod != probed_mod) {
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800580 if (unlikely(!try_module_get(probed_mod)))
581 return -EINVAL;
582 p->mod_refcounted = 1;
583 } else
584 probed_mod = NULL;
585 }
Mao, Bibob3e55c72005-12-12 00:37:00 -0800586
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800587 p->nmissed = 0;
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800588 mutex_lock(&kprobe_mutex);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700589 old_p = get_kprobe(p->addr);
590 if (old_p) {
591 ret = register_aggr_kprobe(old_p, p);
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -0700592 if (!ret)
593 atomic_inc(&kprobe_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto out;
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700597 ret = arch_prepare_kprobe(p);
598 if (ret)
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800599 goto out;
600
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700601 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800602 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
604
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -0700605 if (atomic_add_return(1, &kprobe_count) == \
606 (ARCH_INACTIVE_KPROBE_COUNT + 1))
607 register_page_fault_notifier(&kprobe_page_fault_nb);
608
bibo,mao62c27be2006-10-02 02:17:33 -0700609 arch_arm_kprobe(p);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800612 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800613
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800614 if (ret && probed_mod)
615 module_put(probed_mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return ret;
617}
618
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800619int __kprobes register_kprobe(struct kprobe *p)
620{
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700621 return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800622}
623
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700624void __kprobes unregister_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Mao, Bibob3e55c72005-12-12 00:37:00 -0800626 struct module *mod;
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800627 struct kprobe *old_p, *list_p;
628 int cleanup_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700629
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800630 mutex_lock(&kprobe_mutex);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700631 old_p = get_kprobe(p->addr);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800632 if (unlikely(!old_p)) {
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800633 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800634 return;
635 }
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800636 if (p != old_p) {
637 list_for_each_entry_rcu(list_p, &old_p->list, list)
638 if (list_p == p)
639 /* kprobe p is a valid probe */
640 goto valid_p;
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800641 mutex_unlock(&kprobe_mutex);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800642 return;
643 }
644valid_p:
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700645 if (old_p == p ||
646 (old_p->pre_handler == aggr_pre_handler &&
647 p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800648 /* Only probe on the hash list */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800649 arch_disarm_kprobe(p);
650 hlist_del_rcu(&old_p->hlist);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800651 cleanup_p = 1;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800652 } else {
653 list_del_rcu(&p->list);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800654 cleanup_p = 0;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800655 }
Mao, Bibob3e55c72005-12-12 00:37:00 -0800656
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800657 mutex_unlock(&kprobe_mutex);
Mao, Bibob3e55c72005-12-12 00:37:00 -0800658
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800659 synchronize_sched();
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700660 if (p->mod_refcounted) {
661 mod = module_text_address((unsigned long)p->addr);
662 if (mod)
663 module_put(mod);
664 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800665
666 if (cleanup_p) {
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800667 if (p != old_p) {
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800668 list_del_rcu(&p->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800669 kfree(old_p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800670 }
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800671 arch_remove_kprobe(p);
mao, bibo36721652006-06-26 00:25:22 -0700672 } else {
673 mutex_lock(&kprobe_mutex);
674 if (p->break_handler)
675 old_p->break_handler = NULL;
676 if (p->post_handler){
677 list_for_each_entry_rcu(list_p, &old_p->list, list){
678 if (list_p->post_handler){
679 cleanup_p = 2;
680 break;
681 }
682 }
683 if (cleanup_p == 0)
684 old_p->post_handler = NULL;
685 }
686 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800687 }
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -0700688
689 /* Call unregister_page_fault_notifier()
690 * if no probes are active
691 */
692 mutex_lock(&kprobe_mutex);
693 if (atomic_add_return(-1, &kprobe_count) == \
694 ARCH_INACTIVE_KPROBE_COUNT)
695 unregister_page_fault_notifier(&kprobe_page_fault_nb);
696 mutex_unlock(&kprobe_mutex);
697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700static struct notifier_block kprobe_exceptions_nb = {
701 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -0700702 .priority = 0x7fffffff /* we need to be notified first */
703};
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700706int __kprobes register_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 /* Todo: Verify probepoint is a function entry point */
709 jp->kp.pre_handler = setjmp_pre_handler;
710 jp->kp.break_handler = longjmp_break_handler;
711
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800712 return __register_kprobe(&jp->kp,
713 (unsigned long)__builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700716void __kprobes unregister_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 unregister_kprobe(&jp->kp);
719}
720
Hien Nguyenb94cce92005-06-23 00:09:19 -0700721#ifdef ARCH_SUPPORTS_KRETPROBES
722
Adrian Bunke65cefe2006-02-03 03:03:42 -0800723/*
724 * This kprobe pre_handler is registered with every kretprobe. When probe
725 * hits it will set up the return probe.
726 */
727static int __kprobes pre_handler_kretprobe(struct kprobe *p,
728 struct pt_regs *regs)
729{
730 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
731 unsigned long flags = 0;
732
733 /*TODO: consider to only swap the RA after the last pre_handler fired */
734 spin_lock_irqsave(&kretprobe_lock, flags);
735 arch_prepare_kretprobe(rp, regs);
736 spin_unlock_irqrestore(&kretprobe_lock, flags);
737 return 0;
738}
739
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700740int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700741{
742 int ret = 0;
743 struct kretprobe_instance *inst;
744 int i;
745
746 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -0700747 rp->kp.post_handler = NULL;
748 rp->kp.fault_handler = NULL;
749 rp->kp.break_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700750
751 /* Pre-allocate memory for max kretprobe instances */
752 if (rp->maxactive <= 0) {
753#ifdef CONFIG_PREEMPT
754 rp->maxactive = max(10, 2 * NR_CPUS);
755#else
756 rp->maxactive = NR_CPUS;
757#endif
758 }
759 INIT_HLIST_HEAD(&rp->used_instances);
760 INIT_HLIST_HEAD(&rp->free_instances);
761 for (i = 0; i < rp->maxactive; i++) {
762 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
763 if (inst == NULL) {
764 free_rp_inst(rp);
765 return -ENOMEM;
766 }
767 INIT_HLIST_NODE(&inst->uflist);
768 hlist_add_head(&inst->uflist, &rp->free_instances);
769 }
770
771 rp->nmissed = 0;
772 /* Establish function entry probe point */
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800773 if ((ret = __register_kprobe(&rp->kp,
774 (unsigned long)__builtin_return_address(0))) != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700775 free_rp_inst(rp);
776 return ret;
777}
778
779#else /* ARCH_SUPPORTS_KRETPROBES */
780
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700781int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700782{
783 return -ENOSYS;
784}
785
Srinivasa Ds346fd592007-02-20 13:57:54 -0800786static int __kprobes pre_handler_kretprobe(struct kprobe *p,
787 struct pt_regs *regs)
788{
789 return 0;
790}
791
Hien Nguyenb94cce92005-06-23 00:09:19 -0700792#endif /* ARCH_SUPPORTS_KRETPROBES */
793
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700794void __kprobes unregister_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700795{
796 unsigned long flags;
797 struct kretprobe_instance *ri;
798
799 unregister_kprobe(&rp->kp);
800 /* No race here */
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800801 spin_lock_irqsave(&kretprobe_lock, flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700802 while ((ri = get_used_rp_inst(rp)) != NULL) {
803 ri->rp = NULL;
804 hlist_del(&ri->uflist);
805 }
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800806 spin_unlock_irqrestore(&kretprobe_lock, flags);
Ananth N Mavinakayanahalli278ff952006-02-03 03:03:43 -0800807 free_rp_inst(rp);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static int __init init_kprobes(void)
811{
812 int i, err = 0;
813
814 /* FIXME allocate the probe table, currently defined statically */
815 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700816 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700818 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
819 }
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -0700820 atomic_set(&kprobe_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Rusty Lynch67729262005-07-05 18:54:50 -0700822 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -0700823 if (!err)
824 err = register_die_notifier(&kprobe_exceptions_nb);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return err;
827}
828
Srinivasa Ds346fd592007-02-20 13:57:54 -0800829#ifdef CONFIG_DEBUG_FS
830static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
831 const char *sym, int offset,char *modname)
832{
833 char *kprobe_type;
834
835 if (p->pre_handler == pre_handler_kretprobe)
836 kprobe_type = "r";
837 else if (p->pre_handler == setjmp_pre_handler)
838 kprobe_type = "j";
839 else
840 kprobe_type = "k";
841 if (sym)
842 seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
843 sym, offset, (modname ? modname : " "));
844 else
845 seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
846}
847
848static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
849{
850 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
851}
852
853static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
854{
855 (*pos)++;
856 if (*pos >= KPROBE_TABLE_SIZE)
857 return NULL;
858 return pos;
859}
860
861static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
862{
863 /* Nothing to do */
864}
865
866static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
867{
868 struct hlist_head *head;
869 struct hlist_node *node;
870 struct kprobe *p, *kp;
871 const char *sym = NULL;
872 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700873 unsigned long offset = 0;
Srinivasa Ds346fd592007-02-20 13:57:54 -0800874 char *modname, namebuf[128];
875
876 head = &kprobe_table[i];
877 preempt_disable();
878 hlist_for_each_entry_rcu(p, node, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700879 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -0800880 &offset, &modname, namebuf);
881 if (p->pre_handler == aggr_pre_handler) {
882 list_for_each_entry_rcu(kp, &p->list, list)
883 report_probe(pi, kp, sym, offset, modname);
884 } else
885 report_probe(pi, p, sym, offset, modname);
886 }
887 preempt_enable();
888 return 0;
889}
890
891static struct seq_operations kprobes_seq_ops = {
892 .start = kprobe_seq_start,
893 .next = kprobe_seq_next,
894 .stop = kprobe_seq_stop,
895 .show = show_kprobe_addr
896};
897
898static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
899{
900 return seq_open(filp, &kprobes_seq_ops);
901}
902
903static struct file_operations debugfs_kprobes_operations = {
904 .open = kprobes_open,
905 .read = seq_read,
906 .llseek = seq_lseek,
907 .release = seq_release,
908};
909
910static int __kprobes debugfs_kprobe_init(void)
911{
912 struct dentry *dir, *file;
913
914 dir = debugfs_create_dir("kprobes", NULL);
915 if (!dir)
916 return -ENOMEM;
917
Randy Dunlape3869792007-05-08 00:27:01 -0700918 file = debugfs_create_file("list", 0444, dir, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -0800919 &debugfs_kprobes_operations);
920 if (!file) {
921 debugfs_remove(dir);
922 return -ENOMEM;
923 }
924
925 return 0;
926}
927
928late_initcall(debugfs_kprobe_init);
929#endif /* CONFIG_DEBUG_FS */
930
931module_init(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933EXPORT_SYMBOL_GPL(register_kprobe);
934EXPORT_SYMBOL_GPL(unregister_kprobe);
935EXPORT_SYMBOL_GPL(register_jprobe);
936EXPORT_SYMBOL_GPL(unregister_jprobe);
937EXPORT_SYMBOL_GPL(jprobe_return);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700938EXPORT_SYMBOL_GPL(register_kretprobe);
939EXPORT_SYMBOL_GPL(unregister_kretprobe);