blob: fcfb580c3afc847e60fc9fa4f682b33c25650963 [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>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070046
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -070047#include <asm-generic/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/cacheflush.h>
49#include <asm/errno.h>
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070050#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define KPROBE_HASH_BITS 6
53#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
54
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070055
56/*
57 * Some oddball architectures like 64bit powerpc have function descriptors
58 * so this must be overridable.
59 */
60#ifndef kprobe_lookup_name
61#define kprobe_lookup_name(name, addr) \
62 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
63#endif
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070066static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -070068/* NOTE: change this value only with kprobe_mutex held */
69static bool kprobe_enabled;
70
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -080071DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -080072DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080073static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080075#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070076/*
77 * kprobe->ainsn.insn points to the copy of the instruction to be
78 * single-stepped. x86_64, POWER4 and above have no-exec support and
79 * stepping on the instruction on a vmalloced/kmalloced/data page
80 * is a recipe for disaster
81 */
82#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
83
84struct kprobe_insn_page {
85 struct hlist_node hlist;
86 kprobe_opcode_t *insns; /* Page of instruction slots */
87 char slot_used[INSNS_PER_PAGE];
88 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080089 int ngarbage;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070090};
91
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -080092enum kprobe_slot_state {
93 SLOT_CLEAN = 0,
94 SLOT_DIRTY = 1,
95 SLOT_USED = 2,
96};
97
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070098static struct hlist_head kprobe_insn_pages;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080099static int kprobe_garbage_slots;
100static int collect_garbage_slots(void);
101
102static int __kprobes check_safety(void)
103{
104 int ret = 0;
105#if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
106 ret = freeze_processes();
107 if (ret == 0) {
108 struct task_struct *p, *q;
109 do_each_thread(p, q) {
110 if (p != current && p->state == TASK_RUNNING &&
111 p->pid != 0) {
112 printk("Check failed: %s is running\n",p->comm);
113 ret = -1;
114 goto loop_end;
115 }
116 } while_each_thread(p, q);
117 }
118loop_end:
119 thaw_processes();
120#else
121 synchronize_sched();
122#endif
123 return ret;
124}
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700125
126/**
127 * get_insn_slot() - Find a slot on an executable page for an instruction.
128 * We allocate an executable page if there's no room on existing ones.
129 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700130kprobe_opcode_t __kprobes *get_insn_slot(void)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700131{
132 struct kprobe_insn_page *kip;
133 struct hlist_node *pos;
134
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700135 retry:
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700136 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700137 if (kip->nused < INSNS_PER_PAGE) {
138 int i;
139 for (i = 0; i < INSNS_PER_PAGE; i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800140 if (kip->slot_used[i] == SLOT_CLEAN) {
141 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700142 kip->nused++;
143 return kip->insns + (i * MAX_INSN_SIZE);
144 }
145 }
146 /* Surprise! No unused slots. Fix kip->nused. */
147 kip->nused = INSNS_PER_PAGE;
148 }
149 }
150
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800151 /* If there are any garbage slots, collect it and try again. */
152 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
153 goto retry;
154 }
155 /* All out of space. Need to allocate a new page. Use slot 0. */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700156 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700157 if (!kip)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700158 return NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700159
160 /*
161 * Use module_alloc so this page is within +/- 2GB of where the
162 * kernel image and loaded module images reside. This is required
163 * so x86_64 can correctly handle the %rip-relative fixups.
164 */
165 kip->insns = module_alloc(PAGE_SIZE);
166 if (!kip->insns) {
167 kfree(kip);
168 return NULL;
169 }
170 INIT_HLIST_NODE(&kip->hlist);
171 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800172 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
173 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700174 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800175 kip->ngarbage = 0;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700176 return kip->insns;
177}
178
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800179/* Return 1 if all garbages are collected, otherwise 0. */
180static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
181{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800182 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800183 kip->nused--;
184 if (kip->nused == 0) {
185 /*
186 * Page is no longer in use. Free it unless
187 * it's the last one. We keep the last one
188 * so as not to have to set it up again the
189 * next time somebody inserts a probe.
190 */
191 hlist_del(&kip->hlist);
192 if (hlist_empty(&kprobe_insn_pages)) {
193 INIT_HLIST_NODE(&kip->hlist);
194 hlist_add_head(&kip->hlist,
195 &kprobe_insn_pages);
196 } else {
197 module_free(NULL, kip->insns);
198 kfree(kip);
199 }
200 return 1;
201 }
202 return 0;
203}
204
205static int __kprobes collect_garbage_slots(void)
206{
207 struct kprobe_insn_page *kip;
208 struct hlist_node *pos, *next;
209
210 /* Ensure no-one is preepmted on the garbages */
211 if (check_safety() != 0)
212 return -EAGAIN;
213
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700214 hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800215 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800216 if (kip->ngarbage == 0)
217 continue;
218 kip->ngarbage = 0; /* we will collect all garbages */
219 for (i = 0; i < INSNS_PER_PAGE; i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800220 if (kip->slot_used[i] == SLOT_DIRTY &&
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800221 collect_one_slot(kip, i))
222 break;
223 }
224 }
225 kprobe_garbage_slots = 0;
226 return 0;
227}
228
229void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700230{
231 struct kprobe_insn_page *kip;
232 struct hlist_node *pos;
233
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700234 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700235 if (kip->insns <= slot &&
236 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
237 int i = (slot - kip->insns) / MAX_INSN_SIZE;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800238 if (dirty) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800239 kip->slot_used[i] = SLOT_DIRTY;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800240 kip->ngarbage++;
241 } else {
242 collect_one_slot(kip, i);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700243 }
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800244 break;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700245 }
246 }
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700247
248 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800249 collect_garbage_slots();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700250}
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800251#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700252
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800253/* We have preemption disabled.. so it is safe to use __ versions */
254static inline void set_kprobe_instance(struct kprobe *kp)
255{
256 __get_cpu_var(kprobe_instance) = kp;
257}
258
259static inline void reset_kprobe_instance(void)
260{
261 __get_cpu_var(kprobe_instance) = NULL;
262}
263
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800264/*
265 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800266 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800267 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800268 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800269 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700270struct kprobe __kprobes *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct hlist_head *head;
273 struct hlist_node *node;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800274 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800277 hlist_for_each_entry_rcu(p, node, head, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (p->addr == addr)
279 return p;
280 }
281 return NULL;
282}
283
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700284/*
285 * Aggregate handlers for multiple kprobes support - these handlers
286 * take care of invoking the individual kprobe handlers on p->list
287 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700288static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700289{
290 struct kprobe *kp;
291
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800292 list_for_each_entry_rcu(kp, &p->list, list) {
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700293 if (kp->pre_handler) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800294 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700295 if (kp->pre_handler(kp, regs))
296 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700297 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800298 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700299 }
300 return 0;
301}
302
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700303static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
304 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700305{
306 struct kprobe *kp;
307
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800308 list_for_each_entry_rcu(kp, &p->list, list) {
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700309 if (kp->post_handler) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800310 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700311 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800312 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700313 }
314 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700315}
316
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700317static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
318 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700319{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800320 struct kprobe *cur = __get_cpu_var(kprobe_instance);
321
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700322 /*
323 * if we faulted "during" the execution of a user specified
324 * probe handler, invoke just that probe's fault handler
325 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800326 if (cur && cur->fault_handler) {
327 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700328 return 1;
329 }
330 return 0;
331}
332
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700333static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700334{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800335 struct kprobe *cur = __get_cpu_var(kprobe_instance);
336 int ret = 0;
337
338 if (cur && cur->break_handler) {
339 if (cur->break_handler(cur, regs))
340 ret = 1;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700341 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800342 reset_kprobe_instance();
343 return ret;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700344}
345
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800346/* Walks the list and increments nmissed count for multiprobe case */
347void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
348{
349 struct kprobe *kp;
350 if (p->pre_handler != aggr_pre_handler) {
351 p->nmissed++;
352 } else {
353 list_for_each_entry_rcu(kp, &p->list, list)
354 kp->nmissed++;
355 }
356 return;
357}
358
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800359/* Called with kretprobe_lock held */
bibo,mao99219a32006-10-02 02:17:35 -0700360void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
361 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700362{
363 /* remove rp inst off the rprobe_inst_table */
364 hlist_del(&ri->hlist);
365 if (ri->rp) {
366 /* remove rp inst off the used list */
367 hlist_del(&ri->uflist);
368 /* put rp inst back onto the free list */
369 INIT_HLIST_NODE(&ri->uflist);
370 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
371 } else
372 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -0700373 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700374}
375
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700376struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700377{
378 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
379}
380
Hien Nguyenb94cce92005-06-23 00:09:19 -0700381/*
bibo maoc6fd91f2006-03-26 01:38:20 -0800382 * This function is called from finish_task_switch when task tk becomes dead,
383 * so that we can recycle any function-return probe instances associated
384 * with this task. These left over instances represent probed functions
385 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -0700386 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700387void __kprobes kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700388{
bibo,mao62c27be2006-10-02 02:17:33 -0700389 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -0700390 struct hlist_head *head, empty_rp;
Rusty Lynch802eae72005-06-27 15:17:08 -0700391 struct hlist_node *node, *tmp;
Hien Nguyen0aa55e42005-06-23 00:09:26 -0700392 unsigned long flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -0700393
bibo,mao99219a32006-10-02 02:17:35 -0700394 INIT_HLIST_HEAD(&empty_rp);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800395 spin_lock_irqsave(&kretprobe_lock, flags);
bibo,mao62c27be2006-10-02 02:17:33 -0700396 head = kretprobe_inst_table_head(tk);
397 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
398 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -0700399 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -0700400 }
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800401 spin_unlock_irqrestore(&kretprobe_lock, flags);
bibo,mao99219a32006-10-02 02:17:35 -0700402
403 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
404 hlist_del(&ri->hlist);
405 kfree(ri);
406 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700407}
408
Hien Nguyenb94cce92005-06-23 00:09:19 -0700409static inline void free_rp_inst(struct kretprobe *rp)
410{
411 struct kretprobe_instance *ri;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700412 struct hlist_node *pos, *next;
413
414 hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
Hien Nguyenb94cce92005-06-23 00:09:19 -0700415 hlist_del(&ri->uflist);
416 kfree(ri);
417 }
418}
419
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700420/*
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700421 * Keep all fields in the kprobe consistent
422 */
423static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
424{
425 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
426 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
427}
428
429/*
430* Add the new probe to old_p->list. Fail if this is the
431* second jprobe at the address - two jprobes can't coexist
432*/
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700433static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700434{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700435 if (p->break_handler) {
mao, bibo36721652006-06-26 00:25:22 -0700436 if (old_p->break_handler)
437 return -EEXIST;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800438 list_add_tail_rcu(&p->list, &old_p->list);
mao, bibo36721652006-06-26 00:25:22 -0700439 old_p->break_handler = aggr_break_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700440 } else
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800441 list_add_rcu(&p->list, &old_p->list);
mao, bibo36721652006-06-26 00:25:22 -0700442 if (p->post_handler && !old_p->post_handler)
443 old_p->post_handler = aggr_post_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700444 return 0;
445}
446
447/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700448 * Fill in the required fields of the "manager kprobe". Replace the
449 * earlier kprobe in the hlist with the manager kprobe
450 */
451static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
452{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700453 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -0700454 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700455 ap->addr = p->addr;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700456 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700457 ap->fault_handler = aggr_fault_handler;
mao, bibo36721652006-06-26 00:25:22 -0700458 if (p->post_handler)
459 ap->post_handler = aggr_post_handler;
460 if (p->break_handler)
461 ap->break_handler = aggr_break_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700462
463 INIT_LIST_HEAD(&ap->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800464 list_add_rcu(&p->list, &ap->list);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700465
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -0800466 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700467}
468
469/*
470 * This is the second or subsequent kprobe at the address - handle
471 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700472 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700473static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
474 struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700475{
476 int ret = 0;
477 struct kprobe *ap;
478
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700479 if (old_p->pre_handler == aggr_pre_handler) {
480 copy_kprobe(old_p, p);
481 ret = add_new_kprobe(old_p, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700482 } else {
Keshavamurthy Anil Sa0d50062006-01-09 20:52:46 -0800483 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700484 if (!ap)
485 return -ENOMEM;
486 add_aggr_kprobe(ap, old_p);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700487 copy_kprobe(ap, p);
488 ret = add_new_kprobe(ap, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700489 }
490 return ret;
491}
492
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700493static int __kprobes in_kprobes_functions(unsigned long addr)
494{
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700495 if (addr >= (unsigned long)__kprobes_text_start &&
496 addr < (unsigned long)__kprobes_text_end)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700497 return -EINVAL;
498 return 0;
499}
500
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800501/*
502 * If we have a symbol_name argument, look it up and add the offset field
503 * to it. This way, we can specify a relative address to a symbol.
504 */
505static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
506{
507 kprobe_opcode_t *addr = p->addr;
508 if (p->symbol_name) {
509 if (addr)
510 return NULL;
511 kprobe_lookup_name(p->symbol_name, addr);
512 }
513
514 if (!addr)
515 return NULL;
516 return (kprobe_opcode_t *)(((char *)addr) + p->offset);
517}
518
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800519static int __kprobes __register_kprobe(struct kprobe *p,
520 unsigned long called_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 int ret = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700523 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800524 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800525 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800527 addr = kprobe_addr(p);
528 if (!addr)
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700529 return -EINVAL;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800530 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700531
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700532 if (!kernel_text_address((unsigned long) p->addr) ||
533 in_kprobes_functions((unsigned long) p->addr))
Mao, Bibob3e55c72005-12-12 00:37:00 -0800534 return -EINVAL;
535
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800536 p->mod_refcounted = 0;
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700537
538 /*
539 * Check if are we probing a module.
540 */
541 probed_mod = module_text_address((unsigned long) p->addr);
542 if (probed_mod) {
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800543 struct module *calling_mod = module_text_address(called_from);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700544 /*
545 * We must allow modules to probe themself and in this case
546 * avoid incrementing the module refcount, so as to allow
547 * unloading of self probing modules.
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800548 */
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700549 if (calling_mod && calling_mod != probed_mod) {
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800550 if (unlikely(!try_module_get(probed_mod)))
551 return -EINVAL;
552 p->mod_refcounted = 1;
553 } else
554 probed_mod = NULL;
555 }
Mao, Bibob3e55c72005-12-12 00:37:00 -0800556
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800557 p->nmissed = 0;
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800558 mutex_lock(&kprobe_mutex);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700559 old_p = get_kprobe(p->addr);
560 if (old_p) {
561 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto out;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700565 ret = arch_prepare_kprobe(p);
566 if (ret)
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800567 goto out;
568
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700569 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800570 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
572
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700573 if (kprobe_enabled)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700574 arch_arm_kprobe(p);
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800577 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800578
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800579 if (ret && probed_mod)
580 module_put(probed_mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return ret;
582}
583
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800584int __kprobes register_kprobe(struct kprobe *p)
585{
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700586 return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800587}
588
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700589void __kprobes unregister_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Mao, Bibob3e55c72005-12-12 00:37:00 -0800591 struct module *mod;
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800592 struct kprobe *old_p, *list_p;
593 int cleanup_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700594
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800595 mutex_lock(&kprobe_mutex);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700596 old_p = get_kprobe(p->addr);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800597 if (unlikely(!old_p)) {
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800598 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800599 return;
600 }
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800601 if (p != old_p) {
602 list_for_each_entry_rcu(list_p, &old_p->list, list)
603 if (list_p == p)
604 /* kprobe p is a valid probe */
605 goto valid_p;
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800606 mutex_unlock(&kprobe_mutex);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800607 return;
608 }
609valid_p:
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700610 if (old_p == p ||
611 (old_p->pre_handler == aggr_pre_handler &&
612 p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700613 /*
614 * Only probe on the hash list. Disarm only if kprobes are
615 * enabled - otherwise, the breakpoint would already have
616 * been removed. We save on flushing icache.
617 */
618 if (kprobe_enabled)
619 arch_disarm_kprobe(p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800620 hlist_del_rcu(&old_p->hlist);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800621 cleanup_p = 1;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800622 } else {
623 list_del_rcu(&p->list);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800624 cleanup_p = 0;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800625 }
Mao, Bibob3e55c72005-12-12 00:37:00 -0800626
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800627 mutex_unlock(&kprobe_mutex);
Mao, Bibob3e55c72005-12-12 00:37:00 -0800628
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800629 synchronize_sched();
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700630 if (p->mod_refcounted) {
631 mod = module_text_address((unsigned long)p->addr);
632 if (mod)
633 module_put(mod);
634 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800635
636 if (cleanup_p) {
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800637 if (p != old_p) {
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800638 list_del_rcu(&p->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800639 kfree(old_p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800640 }
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800641 arch_remove_kprobe(p);
mao, bibo36721652006-06-26 00:25:22 -0700642 } else {
643 mutex_lock(&kprobe_mutex);
644 if (p->break_handler)
645 old_p->break_handler = NULL;
646 if (p->post_handler){
647 list_for_each_entry_rcu(list_p, &old_p->list, list){
648 if (list_p->post_handler){
649 cleanup_p = 2;
650 break;
651 }
652 }
653 if (cleanup_p == 0)
654 old_p->post_handler = NULL;
655 }
656 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660static struct notifier_block kprobe_exceptions_nb = {
661 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -0700662 .priority = 0x7fffffff /* we need to be notified first */
663};
664
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700665unsigned long __weak arch_deref_entry_point(void *entry)
666{
667 return (unsigned long)entry;
668}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700670int __kprobes register_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700672 unsigned long addr = arch_deref_entry_point(jp->entry);
673
674 if (!kernel_text_address(addr))
675 return -EINVAL;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* Todo: Verify probepoint is a function entry point */
678 jp->kp.pre_handler = setjmp_pre_handler;
679 jp->kp.break_handler = longjmp_break_handler;
680
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800681 return __register_kprobe(&jp->kp,
682 (unsigned long)__builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700685void __kprobes unregister_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 unregister_kprobe(&jp->kp);
688}
689
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -0800690#ifdef CONFIG_KRETPROBES
Adrian Bunke65cefe2006-02-03 03:03:42 -0800691/*
692 * This kprobe pre_handler is registered with every kretprobe. When probe
693 * hits it will set up the return probe.
694 */
695static int __kprobes pre_handler_kretprobe(struct kprobe *p,
696 struct pt_regs *regs)
697{
698 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
699 unsigned long flags = 0;
700
701 /*TODO: consider to only swap the RA after the last pre_handler fired */
702 spin_lock_irqsave(&kretprobe_lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700703 if (!hlist_empty(&rp->free_instances)) {
704 struct kretprobe_instance *ri;
705
706 ri = hlist_entry(rp->free_instances.first,
707 struct kretprobe_instance, uflist);
708 ri->rp = rp;
709 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800710
711 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
712 spin_unlock_irqrestore(&kretprobe_lock, flags);
713 return 0;
714 }
715
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700716 arch_prepare_kretprobe(ri, regs);
717
718 /* XXX(hch): why is there no hlist_move_head? */
719 hlist_del(&ri->uflist);
720 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
721 hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
722 } else
723 rp->nmissed++;
Adrian Bunke65cefe2006-02-03 03:03:42 -0800724 spin_unlock_irqrestore(&kretprobe_lock, flags);
725 return 0;
726}
727
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700728int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700729{
730 int ret = 0;
731 struct kretprobe_instance *inst;
732 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800733 void *addr;
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700734
735 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800736 addr = kprobe_addr(&rp->kp);
737 if (!addr)
738 return -EINVAL;
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700739
740 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
741 if (kretprobe_blacklist[i].addr == addr)
742 return -EINVAL;
743 }
744 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700745
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++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800762 inst = kmalloc(sizeof(struct kretprobe_instance) +
763 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700764 if (inst == NULL) {
765 free_rp_inst(rp);
766 return -ENOMEM;
767 }
768 INIT_HLIST_NODE(&inst->uflist);
769 hlist_add_head(&inst->uflist, &rp->free_instances);
770 }
771
772 rp->nmissed = 0;
773 /* Establish function entry probe point */
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800774 if ((ret = __register_kprobe(&rp->kp,
775 (unsigned long)__builtin_return_address(0))) != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700776 free_rp_inst(rp);
777 return ret;
778}
779
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -0800780#else /* CONFIG_KRETPROBES */
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}
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -0800791#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700792
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700793void __kprobes unregister_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700794{
795 unsigned long flags;
796 struct kretprobe_instance *ri;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700797 struct hlist_node *pos, *next;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700798
799 unregister_kprobe(&rp->kp);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700800
Hien Nguyenb94cce92005-06-23 00:09:19 -0700801 /* No race here */
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800802 spin_lock_irqsave(&kretprobe_lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700803 hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
Hien Nguyenb94cce92005-06-23 00:09:19 -0700804 ri->rp = NULL;
805 hlist_del(&ri->uflist);
806 }
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800807 spin_unlock_irqrestore(&kretprobe_lock, flags);
Ananth N Mavinakayanahalli278ff952006-02-03 03:03:43 -0800808 free_rp_inst(rp);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static int __init init_kprobes(void)
812{
813 int i, err = 0;
814
815 /* FIXME allocate the probe table, currently defined statically */
816 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700817 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700819 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700822 if (kretprobe_blacklist_size) {
823 /* lookup the function address from its name */
824 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
825 kprobe_lookup_name(kretprobe_blacklist[i].name,
826 kretprobe_blacklist[i].addr);
827 if (!kretprobe_blacklist[i].addr)
828 printk("kretprobe: lookup failed: %s\n",
829 kretprobe_blacklist[i].name);
830 }
831 }
832
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700833 /* By default, kprobes are enabled */
834 kprobe_enabled = true;
835
Rusty Lynch67729262005-07-05 18:54:50 -0700836 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -0700837 if (!err)
838 err = register_die_notifier(&kprobe_exceptions_nb);
839
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +0100840 if (!err)
841 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return err;
843}
844
Srinivasa Ds346fd592007-02-20 13:57:54 -0800845#ifdef CONFIG_DEBUG_FS
846static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700847 const char *sym, int offset,char *modname)
Srinivasa Ds346fd592007-02-20 13:57:54 -0800848{
849 char *kprobe_type;
850
851 if (p->pre_handler == pre_handler_kretprobe)
852 kprobe_type = "r";
853 else if (p->pre_handler == setjmp_pre_handler)
854 kprobe_type = "j";
855 else
856 kprobe_type = "k";
857 if (sym)
858 seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
859 sym, offset, (modname ? modname : " "));
860 else
861 seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
862}
863
864static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
865{
866 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
867}
868
869static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
870{
871 (*pos)++;
872 if (*pos >= KPROBE_TABLE_SIZE)
873 return NULL;
874 return pos;
875}
876
877static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
878{
879 /* Nothing to do */
880}
881
882static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
883{
884 struct hlist_head *head;
885 struct hlist_node *node;
886 struct kprobe *p, *kp;
887 const char *sym = NULL;
888 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700889 unsigned long offset = 0;
Srinivasa Ds346fd592007-02-20 13:57:54 -0800890 char *modname, namebuf[128];
891
892 head = &kprobe_table[i];
893 preempt_disable();
894 hlist_for_each_entry_rcu(p, node, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700895 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -0800896 &offset, &modname, namebuf);
897 if (p->pre_handler == aggr_pre_handler) {
898 list_for_each_entry_rcu(kp, &p->list, list)
899 report_probe(pi, kp, sym, offset, modname);
900 } else
901 report_probe(pi, p, sym, offset, modname);
902 }
903 preempt_enable();
904 return 0;
905}
906
907static struct seq_operations kprobes_seq_ops = {
908 .start = kprobe_seq_start,
909 .next = kprobe_seq_next,
910 .stop = kprobe_seq_stop,
911 .show = show_kprobe_addr
912};
913
914static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
915{
916 return seq_open(filp, &kprobes_seq_ops);
917}
918
919static struct file_operations debugfs_kprobes_operations = {
920 .open = kprobes_open,
921 .read = seq_read,
922 .llseek = seq_lseek,
923 .release = seq_release,
924};
925
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700926static void __kprobes enable_all_kprobes(void)
927{
928 struct hlist_head *head;
929 struct hlist_node *node;
930 struct kprobe *p;
931 unsigned int i;
932
933 mutex_lock(&kprobe_mutex);
934
935 /* If kprobes are already enabled, just return */
936 if (kprobe_enabled)
937 goto already_enabled;
938
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700939 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
940 head = &kprobe_table[i];
941 hlist_for_each_entry_rcu(p, node, head, hlist)
942 arch_arm_kprobe(p);
943 }
944
945 kprobe_enabled = true;
946 printk(KERN_INFO "Kprobes globally enabled\n");
947
948already_enabled:
949 mutex_unlock(&kprobe_mutex);
950 return;
951}
952
953static void __kprobes disable_all_kprobes(void)
954{
955 struct hlist_head *head;
956 struct hlist_node *node;
957 struct kprobe *p;
958 unsigned int i;
959
960 mutex_lock(&kprobe_mutex);
961
962 /* If kprobes are already disabled, just return */
963 if (!kprobe_enabled)
964 goto already_disabled;
965
966 kprobe_enabled = false;
967 printk(KERN_INFO "Kprobes globally disabled\n");
968 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
969 head = &kprobe_table[i];
970 hlist_for_each_entry_rcu(p, node, head, hlist) {
971 if (!arch_trampoline_kprobe(p))
972 arch_disarm_kprobe(p);
973 }
974 }
975
976 mutex_unlock(&kprobe_mutex);
977 /* Allow all currently running kprobes to complete */
978 synchronize_sched();
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700979 return;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700980
981already_disabled:
982 mutex_unlock(&kprobe_mutex);
983 return;
984}
985
986/*
987 * XXX: The debugfs bool file interface doesn't allow for callbacks
988 * when the bool state is switched. We can reuse that facility when
989 * available
990 */
991static ssize_t read_enabled_file_bool(struct file *file,
992 char __user *user_buf, size_t count, loff_t *ppos)
993{
994 char buf[3];
995
996 if (kprobe_enabled)
997 buf[0] = '1';
998 else
999 buf[0] = '0';
1000 buf[1] = '\n';
1001 buf[2] = 0x00;
1002 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1003}
1004
1005static ssize_t write_enabled_file_bool(struct file *file,
1006 const char __user *user_buf, size_t count, loff_t *ppos)
1007{
1008 char buf[32];
1009 int buf_size;
1010
1011 buf_size = min(count, (sizeof(buf)-1));
1012 if (copy_from_user(buf, user_buf, buf_size))
1013 return -EFAULT;
1014
1015 switch (buf[0]) {
1016 case 'y':
1017 case 'Y':
1018 case '1':
1019 enable_all_kprobes();
1020 break;
1021 case 'n':
1022 case 'N':
1023 case '0':
1024 disable_all_kprobes();
1025 break;
1026 }
1027
1028 return count;
1029}
1030
1031static struct file_operations fops_kp = {
1032 .read = read_enabled_file_bool,
1033 .write = write_enabled_file_bool,
1034};
1035
Srinivasa Ds346fd592007-02-20 13:57:54 -08001036static int __kprobes debugfs_kprobe_init(void)
1037{
1038 struct dentry *dir, *file;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001039 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08001040
1041 dir = debugfs_create_dir("kprobes", NULL);
1042 if (!dir)
1043 return -ENOMEM;
1044
Randy Dunlape3869792007-05-08 00:27:01 -07001045 file = debugfs_create_file("list", 0444, dir, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08001046 &debugfs_kprobes_operations);
1047 if (!file) {
1048 debugfs_remove(dir);
1049 return -ENOMEM;
1050 }
1051
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001052 file = debugfs_create_file("enabled", 0600, dir,
1053 &value, &fops_kp);
1054 if (!file) {
1055 debugfs_remove(dir);
1056 return -ENOMEM;
1057 }
1058
Srinivasa Ds346fd592007-02-20 13:57:54 -08001059 return 0;
1060}
1061
1062late_initcall(debugfs_kprobe_init);
1063#endif /* CONFIG_DEBUG_FS */
1064
1065module_init(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067EXPORT_SYMBOL_GPL(register_kprobe);
1068EXPORT_SYMBOL_GPL(unregister_kprobe);
1069EXPORT_SYMBOL_GPL(register_jprobe);
1070EXPORT_SYMBOL_GPL(unregister_jprobe);
Peter Chubbcd5bfea2007-08-10 13:01:10 -07001071#ifdef CONFIG_KPROBES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072EXPORT_SYMBOL_GPL(jprobe_return);
Peter Chubbcd5bfea2007-08-10 13:01:10 -07001073#endif
1074
1075#ifdef CONFIG_KPROBES
Hien Nguyenb94cce92005-06-23 00:09:19 -07001076EXPORT_SYMBOL_GPL(register_kretprobe);
1077EXPORT_SYMBOL_GPL(unregister_kretprobe);
Peter Chubbcd5bfea2007-08-10 13:01:10 -07001078#endif