blob: 5e3144ad9b64dc4ad7b25be5a3a9af6220f6dc08 [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
Srinivasa Ds3d8d9962008-04-28 02:14:26 -070075/*
76 * Normally, functions that we'd want to prohibit kprobes in, are marked
77 * __kprobes. But, there are cases where such functions already belong to
78 * a different section (__sched for preempt_schedule)
79 *
80 * For such cases, we now have a blacklist
81 */
82struct kprobe_blackpoint kprobe_blacklist[] = {
83 {"preempt_schedule",},
84 {NULL} /* Terminator */
85};
86
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080087#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070088/*
89 * kprobe->ainsn.insn points to the copy of the instruction to be
90 * single-stepped. x86_64, POWER4 and above have no-exec support and
91 * stepping on the instruction on a vmalloced/kmalloced/data page
92 * is a recipe for disaster
93 */
94#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
95
96struct kprobe_insn_page {
97 struct hlist_node hlist;
98 kprobe_opcode_t *insns; /* Page of instruction slots */
99 char slot_used[INSNS_PER_PAGE];
100 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800101 int ngarbage;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700102};
103
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800104enum kprobe_slot_state {
105 SLOT_CLEAN = 0,
106 SLOT_DIRTY = 1,
107 SLOT_USED = 2,
108};
109
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700110static struct hlist_head kprobe_insn_pages;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800111static int kprobe_garbage_slots;
112static int collect_garbage_slots(void);
113
114static int __kprobes check_safety(void)
115{
116 int ret = 0;
117#if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
118 ret = freeze_processes();
119 if (ret == 0) {
120 struct task_struct *p, *q;
121 do_each_thread(p, q) {
122 if (p != current && p->state == TASK_RUNNING &&
123 p->pid != 0) {
124 printk("Check failed: %s is running\n",p->comm);
125 ret = -1;
126 goto loop_end;
127 }
128 } while_each_thread(p, q);
129 }
130loop_end:
131 thaw_processes();
132#else
133 synchronize_sched();
134#endif
135 return ret;
136}
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700137
138/**
139 * get_insn_slot() - Find a slot on an executable page for an instruction.
140 * We allocate an executable page if there's no room on existing ones.
141 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700142kprobe_opcode_t __kprobes *get_insn_slot(void)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700143{
144 struct kprobe_insn_page *kip;
145 struct hlist_node *pos;
146
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700147 retry:
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700148 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700149 if (kip->nused < INSNS_PER_PAGE) {
150 int i;
151 for (i = 0; i < INSNS_PER_PAGE; i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800152 if (kip->slot_used[i] == SLOT_CLEAN) {
153 kip->slot_used[i] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700154 kip->nused++;
155 return kip->insns + (i * MAX_INSN_SIZE);
156 }
157 }
158 /* Surprise! No unused slots. Fix kip->nused. */
159 kip->nused = INSNS_PER_PAGE;
160 }
161 }
162
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800163 /* If there are any garbage slots, collect it and try again. */
164 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
165 goto retry;
166 }
167 /* All out of space. Need to allocate a new page. Use slot 0. */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700168 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700169 if (!kip)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700170 return NULL;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700171
172 /*
173 * Use module_alloc so this page is within +/- 2GB of where the
174 * kernel image and loaded module images reside. This is required
175 * so x86_64 can correctly handle the %rip-relative fixups.
176 */
177 kip->insns = module_alloc(PAGE_SIZE);
178 if (!kip->insns) {
179 kfree(kip);
180 return NULL;
181 }
182 INIT_HLIST_NODE(&kip->hlist);
183 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800184 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
185 kip->slot_used[0] = SLOT_USED;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700186 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800187 kip->ngarbage = 0;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700188 return kip->insns;
189}
190
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800191/* Return 1 if all garbages are collected, otherwise 0. */
192static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
193{
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800194 kip->slot_used[idx] = SLOT_CLEAN;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800195 kip->nused--;
196 if (kip->nused == 0) {
197 /*
198 * Page is no longer in use. Free it unless
199 * it's the last one. We keep the last one
200 * so as not to have to set it up again the
201 * next time somebody inserts a probe.
202 */
203 hlist_del(&kip->hlist);
204 if (hlist_empty(&kprobe_insn_pages)) {
205 INIT_HLIST_NODE(&kip->hlist);
206 hlist_add_head(&kip->hlist,
207 &kprobe_insn_pages);
208 } else {
209 module_free(NULL, kip->insns);
210 kfree(kip);
211 }
212 return 1;
213 }
214 return 0;
215}
216
217static int __kprobes collect_garbage_slots(void)
218{
219 struct kprobe_insn_page *kip;
220 struct hlist_node *pos, *next;
221
222 /* Ensure no-one is preepmted on the garbages */
223 if (check_safety() != 0)
224 return -EAGAIN;
225
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700226 hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800227 int i;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800228 if (kip->ngarbage == 0)
229 continue;
230 kip->ngarbage = 0; /* we will collect all garbages */
231 for (i = 0; i < INSNS_PER_PAGE; i++) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800232 if (kip->slot_used[i] == SLOT_DIRTY &&
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800233 collect_one_slot(kip, i))
234 break;
235 }
236 }
237 kprobe_garbage_slots = 0;
238 return 0;
239}
240
241void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700242{
243 struct kprobe_insn_page *kip;
244 struct hlist_node *pos;
245
Christoph Hellwigb0bb5012007-05-08 00:34:11 -0700246 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700247 if (kip->insns <= slot &&
248 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
249 int i = (slot - kip->insns) / MAX_INSN_SIZE;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800250 if (dirty) {
Masami Hiramatsuab40c5c2007-01-30 14:36:06 -0800251 kip->slot_used[i] = SLOT_DIRTY;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800252 kip->ngarbage++;
253 } else {
254 collect_one_slot(kip, i);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700255 }
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800256 break;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700257 }
258 }
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700259
260 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800261 collect_garbage_slots();
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700262}
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800263#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700264
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800265/* We have preemption disabled.. so it is safe to use __ versions */
266static inline void set_kprobe_instance(struct kprobe *kp)
267{
268 __get_cpu_var(kprobe_instance) = kp;
269}
270
271static inline void reset_kprobe_instance(void)
272{
273 __get_cpu_var(kprobe_instance) = NULL;
274}
275
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800276/*
277 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800278 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800279 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800280 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800281 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700282struct kprobe __kprobes *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct hlist_head *head;
285 struct hlist_node *node;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800286 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800289 hlist_for_each_entry_rcu(p, node, head, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (p->addr == addr)
291 return p;
292 }
293 return NULL;
294}
295
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700296/*
297 * Aggregate handlers for multiple kprobes support - these handlers
298 * take care of invoking the individual kprobe handlers on p->list
299 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700300static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700301{
302 struct kprobe *kp;
303
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800304 list_for_each_entry_rcu(kp, &p->list, list) {
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700305 if (kp->pre_handler) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800306 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700307 if (kp->pre_handler(kp, regs))
308 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700309 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800310 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700311 }
312 return 0;
313}
314
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700315static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
316 unsigned long flags)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700317{
318 struct kprobe *kp;
319
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800320 list_for_each_entry_rcu(kp, &p->list, list) {
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700321 if (kp->post_handler) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800322 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700323 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800324 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700325 }
326 }
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700327}
328
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700329static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
330 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700331{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800332 struct kprobe *cur = __get_cpu_var(kprobe_instance);
333
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700334 /*
335 * if we faulted "during" the execution of a user specified
336 * probe handler, invoke just that probe's fault handler
337 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800338 if (cur && cur->fault_handler) {
339 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700340 return 1;
341 }
342 return 0;
343}
344
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700345static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700346{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800347 struct kprobe *cur = __get_cpu_var(kprobe_instance);
348 int ret = 0;
349
350 if (cur && cur->break_handler) {
351 if (cur->break_handler(cur, regs))
352 ret = 1;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700353 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800354 reset_kprobe_instance();
355 return ret;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700356}
357
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800358/* Walks the list and increments nmissed count for multiprobe case */
359void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
360{
361 struct kprobe *kp;
362 if (p->pre_handler != aggr_pre_handler) {
363 p->nmissed++;
364 } else {
365 list_for_each_entry_rcu(kp, &p->list, list)
366 kp->nmissed++;
367 }
368 return;
369}
370
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800371/* Called with kretprobe_lock held */
bibo,mao99219a32006-10-02 02:17:35 -0700372void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
373 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700374{
375 /* remove rp inst off the rprobe_inst_table */
376 hlist_del(&ri->hlist);
377 if (ri->rp) {
378 /* remove rp inst off the used list */
379 hlist_del(&ri->uflist);
380 /* put rp inst back onto the free list */
381 INIT_HLIST_NODE(&ri->uflist);
382 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
383 } else
384 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -0700385 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700386}
387
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700388struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700389{
390 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
391}
392
Hien Nguyenb94cce92005-06-23 00:09:19 -0700393/*
bibo maoc6fd91f2006-03-26 01:38:20 -0800394 * This function is called from finish_task_switch when task tk becomes dead,
395 * so that we can recycle any function-return probe instances associated
396 * with this task. These left over instances represent probed functions
397 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -0700398 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700399void __kprobes kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700400{
bibo,mao62c27be2006-10-02 02:17:33 -0700401 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -0700402 struct hlist_head *head, empty_rp;
Rusty Lynch802eae72005-06-27 15:17:08 -0700403 struct hlist_node *node, *tmp;
Hien Nguyen0aa55e42005-06-23 00:09:26 -0700404 unsigned long flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -0700405
bibo,mao99219a32006-10-02 02:17:35 -0700406 INIT_HLIST_HEAD(&empty_rp);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800407 spin_lock_irqsave(&kretprobe_lock, flags);
bibo,mao62c27be2006-10-02 02:17:33 -0700408 head = kretprobe_inst_table_head(tk);
409 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
410 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -0700411 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -0700412 }
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800413 spin_unlock_irqrestore(&kretprobe_lock, flags);
bibo,mao99219a32006-10-02 02:17:35 -0700414
415 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
416 hlist_del(&ri->hlist);
417 kfree(ri);
418 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700419}
420
Hien Nguyenb94cce92005-06-23 00:09:19 -0700421static inline void free_rp_inst(struct kretprobe *rp)
422{
423 struct kretprobe_instance *ri;
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700424 struct hlist_node *pos, *next;
425
426 hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
Hien Nguyenb94cce92005-06-23 00:09:19 -0700427 hlist_del(&ri->uflist);
428 kfree(ri);
429 }
430}
431
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700432static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
433{
434 unsigned long flags;
435 struct kretprobe_instance *ri;
436 struct hlist_node *pos, *next;
437 /* No race here */
438 spin_lock_irqsave(&kretprobe_lock, flags);
439 hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
440 ri->rp = NULL;
441 hlist_del(&ri->uflist);
442 }
443 spin_unlock_irqrestore(&kretprobe_lock, flags);
444 free_rp_inst(rp);
445}
446
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700447/*
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700448 * Keep all fields in the kprobe consistent
449 */
450static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
451{
452 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
453 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
454}
455
456/*
457* Add the new probe to old_p->list. Fail if this is the
458* second jprobe at the address - two jprobes can't coexist
459*/
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700460static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700461{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700462 if (p->break_handler) {
mao, bibo36721652006-06-26 00:25:22 -0700463 if (old_p->break_handler)
464 return -EEXIST;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800465 list_add_tail_rcu(&p->list, &old_p->list);
mao, bibo36721652006-06-26 00:25:22 -0700466 old_p->break_handler = aggr_break_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700467 } else
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800468 list_add_rcu(&p->list, &old_p->list);
mao, bibo36721652006-06-26 00:25:22 -0700469 if (p->post_handler && !old_p->post_handler)
470 old_p->post_handler = aggr_post_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700471 return 0;
472}
473
474/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700475 * Fill in the required fields of the "manager kprobe". Replace the
476 * earlier kprobe in the hlist with the manager kprobe
477 */
478static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
479{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700480 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -0700481 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700482 ap->addr = p->addr;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700483 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700484 ap->fault_handler = aggr_fault_handler;
mao, bibo36721652006-06-26 00:25:22 -0700485 if (p->post_handler)
486 ap->post_handler = aggr_post_handler;
487 if (p->break_handler)
488 ap->break_handler = aggr_break_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700489
490 INIT_LIST_HEAD(&ap->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800491 list_add_rcu(&p->list, &ap->list);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700492
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -0800493 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700494}
495
496/*
497 * This is the second or subsequent kprobe at the address - handle
498 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700499 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700500static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
501 struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700502{
503 int ret = 0;
504 struct kprobe *ap;
505
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700506 if (old_p->pre_handler == aggr_pre_handler) {
507 copy_kprobe(old_p, p);
508 ret = add_new_kprobe(old_p, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700509 } else {
Keshavamurthy Anil Sa0d50062006-01-09 20:52:46 -0800510 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700511 if (!ap)
512 return -ENOMEM;
513 add_aggr_kprobe(ap, old_p);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700514 copy_kprobe(ap, p);
515 ret = add_new_kprobe(ap, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700516 }
517 return ret;
518}
519
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700520static int __kprobes in_kprobes_functions(unsigned long addr)
521{
Srinivasa Ds3d8d9962008-04-28 02:14:26 -0700522 struct kprobe_blackpoint *kb;
523
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700524 if (addr >= (unsigned long)__kprobes_text_start &&
525 addr < (unsigned long)__kprobes_text_end)
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700526 return -EINVAL;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -0700527 /*
528 * If there exists a kprobe_blacklist, verify and
529 * fail any probe registration in the prohibited area
530 */
531 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
532 if (kb->start_addr) {
533 if (addr >= kb->start_addr &&
534 addr < (kb->start_addr + kb->range))
535 return -EINVAL;
536 }
537 }
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700538 return 0;
539}
540
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800541/*
542 * If we have a symbol_name argument, look it up and add the offset field
543 * to it. This way, we can specify a relative address to a symbol.
544 */
545static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
546{
547 kprobe_opcode_t *addr = p->addr;
548 if (p->symbol_name) {
549 if (addr)
550 return NULL;
551 kprobe_lookup_name(p->symbol_name, addr);
552 }
553
554 if (!addr)
555 return NULL;
556 return (kprobe_opcode_t *)(((char *)addr) + p->offset);
557}
558
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800559static int __kprobes __register_kprobe(struct kprobe *p,
560 unsigned long called_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 int ret = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700563 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800564 struct module *probed_mod;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800565 kprobe_opcode_t *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800567 addr = kprobe_addr(p);
568 if (!addr)
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700569 return -EINVAL;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800570 p->addr = addr;
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700571
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700572 if (!kernel_text_address((unsigned long) p->addr) ||
573 in_kprobes_functions((unsigned long) p->addr))
Mao, Bibob3e55c72005-12-12 00:37:00 -0800574 return -EINVAL;
575
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800576 p->mod_refcounted = 0;
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700577
578 /*
579 * Check if are we probing a module.
580 */
581 probed_mod = module_text_address((unsigned long) p->addr);
582 if (probed_mod) {
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800583 struct module *calling_mod = module_text_address(called_from);
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700584 /*
585 * We must allow modules to probe themself and in this case
586 * avoid incrementing the module refcount, so as to allow
587 * unloading of self probing modules.
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800588 */
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700589 if (calling_mod && calling_mod != probed_mod) {
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800590 if (unlikely(!try_module_get(probed_mod)))
591 return -EINVAL;
592 p->mod_refcounted = 1;
593 } else
594 probed_mod = NULL;
595 }
Mao, Bibob3e55c72005-12-12 00:37:00 -0800596
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800597 p->nmissed = 0;
Masami Hiramatsu98616682008-04-28 02:14:28 -0700598 INIT_LIST_HEAD(&p->list);
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800599 mutex_lock(&kprobe_mutex);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700600 old_p = get_kprobe(p->addr);
601 if (old_p) {
602 ret = register_aggr_kprobe(old_p, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 goto out;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700606 ret = arch_prepare_kprobe(p);
607 if (ret)
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800608 goto out;
609
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700610 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800611 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
613
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700614 if (kprobe_enabled)
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700615 arch_arm_kprobe(p);
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800618 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800619
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800620 if (ret && probed_mod)
621 module_put(probed_mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return ret;
623}
624
Masami Hiramatsu98616682008-04-28 02:14:28 -0700625/*
626 * Unregister a kprobe without a scheduler synchronization.
627 */
628static int __kprobes __unregister_kprobe_top(struct kprobe *p)
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800629{
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800630 struct kprobe *old_p, *list_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700631
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700632 old_p = get_kprobe(p->addr);
Masami Hiramatsu98616682008-04-28 02:14:28 -0700633 if (unlikely(!old_p))
634 return -EINVAL;
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;
Masami Hiramatsu98616682008-04-28 02:14:28 -0700641 return -EINVAL;
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800642 }
643valid_p:
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700644 if (old_p == p ||
645 (old_p->pre_handler == aggr_pre_handler &&
Masami Hiramatsu98616682008-04-28 02:14:28 -0700646 list_is_singular(&old_p->list))) {
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700647 /*
648 * Only probe on the hash list. Disarm only if kprobes are
649 * enabled - otherwise, the breakpoint would already have
650 * been removed. We save on flushing icache.
651 */
652 if (kprobe_enabled)
653 arch_disarm_kprobe(p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800654 hlist_del_rcu(&old_p->hlist);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800655 } else {
Masami Hiramatsu98616682008-04-28 02:14:28 -0700656 if (p->break_handler)
657 old_p->break_handler = NULL;
658 if (p->post_handler) {
659 list_for_each_entry_rcu(list_p, &old_p->list, list) {
660 if ((list_p != p) && (list_p->post_handler))
661 goto noclean;
662 }
663 old_p->post_handler = NULL;
664 }
665noclean:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800666 list_del_rcu(&p->list);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800667 }
Masami Hiramatsu98616682008-04-28 02:14:28 -0700668 return 0;
669}
Mao, Bibob3e55c72005-12-12 00:37:00 -0800670
Masami Hiramatsu98616682008-04-28 02:14:28 -0700671static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
672{
673 struct module *mod;
674 struct kprobe *old_p;
Mao, Bibob3e55c72005-12-12 00:37:00 -0800675
Christoph Hellwig6f716ac2007-05-08 00:34:13 -0700676 if (p->mod_refcounted) {
677 mod = module_text_address((unsigned long)p->addr);
678 if (mod)
679 module_put(mod);
680 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800681
Masami Hiramatsu98616682008-04-28 02:14:28 -0700682 if (list_empty(&p->list) || list_is_singular(&p->list)) {
683 if (!list_empty(&p->list)) {
684 /* "p" is the last child of an aggr_kprobe */
685 old_p = list_entry(p->list.next, struct kprobe, list);
686 list_del(&p->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800687 kfree(old_p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800688 }
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800689 arch_remove_kprobe(p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Masami Hiramatsu98616682008-04-28 02:14:28 -0700693static int __register_kprobes(struct kprobe **kps, int num,
694 unsigned long called_from)
695{
696 int i, ret = 0;
697
698 if (num <= 0)
699 return -EINVAL;
700 for (i = 0; i < num; i++) {
701 ret = __register_kprobe(kps[i], called_from);
702 if (ret < 0 && i > 0) {
703 unregister_kprobes(kps, i);
704 break;
705 }
706 }
707 return ret;
708}
709
710/*
711 * Registration and unregistration functions for kprobe.
712 */
713int __kprobes register_kprobe(struct kprobe *p)
714{
715 return __register_kprobes(&p, 1,
716 (unsigned long)__builtin_return_address(0));
717}
718
719void __kprobes unregister_kprobe(struct kprobe *p)
720{
721 unregister_kprobes(&p, 1);
722}
723
724int __kprobes register_kprobes(struct kprobe **kps, int num)
725{
726 return __register_kprobes(kps, num,
727 (unsigned long)__builtin_return_address(0));
728}
729
730void __kprobes unregister_kprobes(struct kprobe **kps, int num)
731{
732 int i;
733
734 if (num <= 0)
735 return;
736 mutex_lock(&kprobe_mutex);
737 for (i = 0; i < num; i++)
738 if (__unregister_kprobe_top(kps[i]) < 0)
739 kps[i]->addr = NULL;
740 mutex_unlock(&kprobe_mutex);
741
742 synchronize_sched();
743 for (i = 0; i < num; i++)
744 if (kps[i]->addr)
745 __unregister_kprobe_bottom(kps[i]);
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static struct notifier_block kprobe_exceptions_nb = {
749 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -0700750 .priority = 0x7fffffff /* we need to be notified first */
751};
752
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700753unsigned long __weak arch_deref_entry_point(void *entry)
754{
755 return (unsigned long)entry;
756}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700758int __kprobes register_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Michael Ellerman3d7e3382007-07-19 01:48:11 -0700760 unsigned long addr = arch_deref_entry_point(jp->entry);
761
762 if (!kernel_text_address(addr))
763 return -EINVAL;
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /* Todo: Verify probepoint is a function entry point */
766 jp->kp.pre_handler = setjmp_pre_handler;
767 jp->kp.break_handler = longjmp_break_handler;
768
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800769 return __register_kprobe(&jp->kp,
770 (unsigned long)__builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700773void __kprobes unregister_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 unregister_kprobe(&jp->kp);
776}
777
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -0800778#ifdef CONFIG_KRETPROBES
Adrian Bunke65cefe2006-02-03 03:03:42 -0800779/*
780 * This kprobe pre_handler is registered with every kretprobe. When probe
781 * hits it will set up the return probe.
782 */
783static int __kprobes pre_handler_kretprobe(struct kprobe *p,
784 struct pt_regs *regs)
785{
786 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
787 unsigned long flags = 0;
788
789 /*TODO: consider to only swap the RA after the last pre_handler fired */
790 spin_lock_irqsave(&kretprobe_lock, flags);
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700791 if (!hlist_empty(&rp->free_instances)) {
792 struct kretprobe_instance *ri;
793
794 ri = hlist_entry(rp->free_instances.first,
795 struct kretprobe_instance, uflist);
796 ri->rp = rp;
797 ri->task = current;
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800798
799 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
800 spin_unlock_irqrestore(&kretprobe_lock, flags);
801 return 0;
802 }
803
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700804 arch_prepare_kretprobe(ri, regs);
805
806 /* XXX(hch): why is there no hlist_move_head? */
807 hlist_del(&ri->uflist);
808 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
809 hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
810 } else
811 rp->nmissed++;
Adrian Bunke65cefe2006-02-03 03:03:42 -0800812 spin_unlock_irqrestore(&kretprobe_lock, flags);
813 return 0;
814}
815
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700816static int __kprobes __register_kretprobe(struct kretprobe *rp,
817 unsigned long called_from)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700818{
819 int ret = 0;
820 struct kretprobe_instance *inst;
821 int i;
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800822 void *addr;
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700823
824 if (kretprobe_blacklist_size) {
Masami Hiramatsub2a5cd62008-03-04 14:29:44 -0800825 addr = kprobe_addr(&rp->kp);
826 if (!addr)
827 return -EINVAL;
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700828
829 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
830 if (kretprobe_blacklist[i].addr == addr)
831 return -EINVAL;
832 }
833 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700834
835 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -0700836 rp->kp.post_handler = NULL;
837 rp->kp.fault_handler = NULL;
838 rp->kp.break_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700839
840 /* Pre-allocate memory for max kretprobe instances */
841 if (rp->maxactive <= 0) {
842#ifdef CONFIG_PREEMPT
843 rp->maxactive = max(10, 2 * NR_CPUS);
844#else
845 rp->maxactive = NR_CPUS;
846#endif
847 }
848 INIT_HLIST_HEAD(&rp->used_instances);
849 INIT_HLIST_HEAD(&rp->free_instances);
850 for (i = 0; i < rp->maxactive; i++) {
Abhishek Sagarf47cd9b2008-02-06 01:38:22 -0800851 inst = kmalloc(sizeof(struct kretprobe_instance) +
852 rp->data_size, GFP_KERNEL);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700853 if (inst == NULL) {
854 free_rp_inst(rp);
855 return -ENOMEM;
856 }
857 INIT_HLIST_NODE(&inst->uflist);
858 hlist_add_head(&inst->uflist, &rp->free_instances);
859 }
860
861 rp->nmissed = 0;
862 /* Establish function entry probe point */
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700863 ret = __register_kprobe(&rp->kp, called_from);
864 if (ret != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700865 free_rp_inst(rp);
866 return ret;
867}
868
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700869static int __register_kretprobes(struct kretprobe **rps, int num,
870 unsigned long called_from)
871{
872 int ret = 0, i;
873
874 if (num <= 0)
875 return -EINVAL;
876 for (i = 0; i < num; i++) {
877 ret = __register_kretprobe(rps[i], called_from);
878 if (ret < 0 && i > 0) {
879 unregister_kretprobes(rps, i);
880 break;
881 }
882 }
883 return ret;
884}
885
886int __kprobes register_kretprobe(struct kretprobe *rp)
887{
888 return __register_kretprobes(&rp, 1,
889 (unsigned long)__builtin_return_address(0));
890}
891
892void __kprobes unregister_kretprobe(struct kretprobe *rp)
893{
894 unregister_kretprobes(&rp, 1);
895}
896
897int __kprobes register_kretprobes(struct kretprobe **rps, int num)
898{
899 return __register_kretprobes(rps, num,
900 (unsigned long)__builtin_return_address(0));
901}
902
903void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
904{
905 int i;
906
907 if (num <= 0)
908 return;
909 mutex_lock(&kprobe_mutex);
910 for (i = 0; i < num; i++)
911 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
912 rps[i]->kp.addr = NULL;
913 mutex_unlock(&kprobe_mutex);
914
915 synchronize_sched();
916 for (i = 0; i < num; i++) {
917 if (rps[i]->kp.addr) {
918 __unregister_kprobe_bottom(&rps[i]->kp);
919 cleanup_rp_inst(rps[i]);
920 }
921 }
922}
923
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -0800924#else /* CONFIG_KRETPROBES */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700925int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700926{
927 return -ENOSYS;
928}
929
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700930int __kprobes register_kretprobes(struct kretprobe **rps, int num)
931{
932 return -ENOSYS;
933}
934void __kprobes unregister_kretprobe(struct kretprobe *rp)
935{
936}
937
938void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
939{
940}
941
Srinivasa Ds346fd592007-02-20 13:57:54 -0800942static int __kprobes pre_handler_kretprobe(struct kprobe *p,
943 struct pt_regs *regs)
944{
945 return 0;
946}
Masami Hiramatsu4a296e02008-04-28 02:14:29 -0700947
Ananth N Mavinakayanahalli9edddaa2008-03-04 14:28:37 -0800948#endif /* CONFIG_KRETPROBES */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static int __init init_kprobes(void)
951{
952 int i, err = 0;
Srinivasa Ds3d8d9962008-04-28 02:14:26 -0700953 unsigned long offset = 0, size = 0;
954 char *modname, namebuf[128];
955 const char *symbol_name;
956 void *addr;
957 struct kprobe_blackpoint *kb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* FIXME allocate the probe table, currently defined statically */
960 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700961 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700963 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Srinivasa Ds3d8d9962008-04-28 02:14:26 -0700966 /*
967 * Lookup and populate the kprobe_blacklist.
968 *
969 * Unlike the kretprobe blacklist, we'll need to determine
970 * the range of addresses that belong to the said functions,
971 * since a kprobe need not necessarily be at the beginning
972 * of a function.
973 */
974 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
975 kprobe_lookup_name(kb->name, addr);
976 if (!addr)
977 continue;
978
979 kb->start_addr = (unsigned long)addr;
980 symbol_name = kallsyms_lookup(kb->start_addr,
981 &size, &offset, &modname, namebuf);
982 if (!symbol_name)
983 kb->range = 0;
984 else
985 kb->range = size;
986 }
987
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700988 if (kretprobe_blacklist_size) {
989 /* lookup the function address from its name */
990 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
991 kprobe_lookup_name(kretprobe_blacklist[i].name,
992 kretprobe_blacklist[i].addr);
993 if (!kretprobe_blacklist[i].addr)
994 printk("kretprobe: lookup failed: %s\n",
995 kretprobe_blacklist[i].name);
996 }
997 }
998
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700999 /* By default, kprobes are enabled */
1000 kprobe_enabled = true;
1001
Rusty Lynch67729262005-07-05 18:54:50 -07001002 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -07001003 if (!err)
1004 err = register_die_notifier(&kprobe_exceptions_nb);
1005
Ananth N Mavinakayanahalli8c1c9352008-01-30 13:32:53 +01001006 if (!err)
1007 init_test_probes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return err;
1009}
1010
Srinivasa Ds346fd592007-02-20 13:57:54 -08001011#ifdef CONFIG_DEBUG_FS
1012static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001013 const char *sym, int offset,char *modname)
Srinivasa Ds346fd592007-02-20 13:57:54 -08001014{
1015 char *kprobe_type;
1016
1017 if (p->pre_handler == pre_handler_kretprobe)
1018 kprobe_type = "r";
1019 else if (p->pre_handler == setjmp_pre_handler)
1020 kprobe_type = "j";
1021 else
1022 kprobe_type = "k";
1023 if (sym)
1024 seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
1025 sym, offset, (modname ? modname : " "));
1026 else
1027 seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
1028}
1029
1030static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
1031{
1032 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
1033}
1034
1035static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
1036{
1037 (*pos)++;
1038 if (*pos >= KPROBE_TABLE_SIZE)
1039 return NULL;
1040 return pos;
1041}
1042
1043static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
1044{
1045 /* Nothing to do */
1046}
1047
1048static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
1049{
1050 struct hlist_head *head;
1051 struct hlist_node *node;
1052 struct kprobe *p, *kp;
1053 const char *sym = NULL;
1054 unsigned int i = *(loff_t *) v;
Alexey Dobriyanffb45122007-05-08 00:28:41 -07001055 unsigned long offset = 0;
Srinivasa Ds346fd592007-02-20 13:57:54 -08001056 char *modname, namebuf[128];
1057
1058 head = &kprobe_table[i];
1059 preempt_disable();
1060 hlist_for_each_entry_rcu(p, node, head, hlist) {
Alexey Dobriyanffb45122007-05-08 00:28:41 -07001061 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08001062 &offset, &modname, namebuf);
1063 if (p->pre_handler == aggr_pre_handler) {
1064 list_for_each_entry_rcu(kp, &p->list, list)
1065 report_probe(pi, kp, sym, offset, modname);
1066 } else
1067 report_probe(pi, p, sym, offset, modname);
1068 }
1069 preempt_enable();
1070 return 0;
1071}
1072
1073static struct seq_operations kprobes_seq_ops = {
1074 .start = kprobe_seq_start,
1075 .next = kprobe_seq_next,
1076 .stop = kprobe_seq_stop,
1077 .show = show_kprobe_addr
1078};
1079
1080static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
1081{
1082 return seq_open(filp, &kprobes_seq_ops);
1083}
1084
1085static struct file_operations debugfs_kprobes_operations = {
1086 .open = kprobes_open,
1087 .read = seq_read,
1088 .llseek = seq_lseek,
1089 .release = seq_release,
1090};
1091
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001092static void __kprobes enable_all_kprobes(void)
1093{
1094 struct hlist_head *head;
1095 struct hlist_node *node;
1096 struct kprobe *p;
1097 unsigned int i;
1098
1099 mutex_lock(&kprobe_mutex);
1100
1101 /* If kprobes are already enabled, just return */
1102 if (kprobe_enabled)
1103 goto already_enabled;
1104
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001105 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1106 head = &kprobe_table[i];
1107 hlist_for_each_entry_rcu(p, node, head, hlist)
1108 arch_arm_kprobe(p);
1109 }
1110
1111 kprobe_enabled = true;
1112 printk(KERN_INFO "Kprobes globally enabled\n");
1113
1114already_enabled:
1115 mutex_unlock(&kprobe_mutex);
1116 return;
1117}
1118
1119static void __kprobes disable_all_kprobes(void)
1120{
1121 struct hlist_head *head;
1122 struct hlist_node *node;
1123 struct kprobe *p;
1124 unsigned int i;
1125
1126 mutex_lock(&kprobe_mutex);
1127
1128 /* If kprobes are already disabled, just return */
1129 if (!kprobe_enabled)
1130 goto already_disabled;
1131
1132 kprobe_enabled = false;
1133 printk(KERN_INFO "Kprobes globally disabled\n");
1134 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1135 head = &kprobe_table[i];
1136 hlist_for_each_entry_rcu(p, node, head, hlist) {
1137 if (!arch_trampoline_kprobe(p))
1138 arch_disarm_kprobe(p);
1139 }
1140 }
1141
1142 mutex_unlock(&kprobe_mutex);
1143 /* Allow all currently running kprobes to complete */
1144 synchronize_sched();
Christoph Hellwig74a0b572007-10-16 01:24:07 -07001145 return;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001146
1147already_disabled:
1148 mutex_unlock(&kprobe_mutex);
1149 return;
1150}
1151
1152/*
1153 * XXX: The debugfs bool file interface doesn't allow for callbacks
1154 * when the bool state is switched. We can reuse that facility when
1155 * available
1156 */
1157static ssize_t read_enabled_file_bool(struct file *file,
1158 char __user *user_buf, size_t count, loff_t *ppos)
1159{
1160 char buf[3];
1161
1162 if (kprobe_enabled)
1163 buf[0] = '1';
1164 else
1165 buf[0] = '0';
1166 buf[1] = '\n';
1167 buf[2] = 0x00;
1168 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1169}
1170
1171static ssize_t write_enabled_file_bool(struct file *file,
1172 const char __user *user_buf, size_t count, loff_t *ppos)
1173{
1174 char buf[32];
1175 int buf_size;
1176
1177 buf_size = min(count, (sizeof(buf)-1));
1178 if (copy_from_user(buf, user_buf, buf_size))
1179 return -EFAULT;
1180
1181 switch (buf[0]) {
1182 case 'y':
1183 case 'Y':
1184 case '1':
1185 enable_all_kprobes();
1186 break;
1187 case 'n':
1188 case 'N':
1189 case '0':
1190 disable_all_kprobes();
1191 break;
1192 }
1193
1194 return count;
1195}
1196
1197static struct file_operations fops_kp = {
1198 .read = read_enabled_file_bool,
1199 .write = write_enabled_file_bool,
1200};
1201
Srinivasa Ds346fd592007-02-20 13:57:54 -08001202static int __kprobes debugfs_kprobe_init(void)
1203{
1204 struct dentry *dir, *file;
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001205 unsigned int value = 1;
Srinivasa Ds346fd592007-02-20 13:57:54 -08001206
1207 dir = debugfs_create_dir("kprobes", NULL);
1208 if (!dir)
1209 return -ENOMEM;
1210
Randy Dunlape3869792007-05-08 00:27:01 -07001211 file = debugfs_create_file("list", 0444, dir, NULL,
Srinivasa Ds346fd592007-02-20 13:57:54 -08001212 &debugfs_kprobes_operations);
1213 if (!file) {
1214 debugfs_remove(dir);
1215 return -ENOMEM;
1216 }
1217
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001218 file = debugfs_create_file("enabled", 0600, dir,
1219 &value, &fops_kp);
1220 if (!file) {
1221 debugfs_remove(dir);
1222 return -ENOMEM;
1223 }
1224
Srinivasa Ds346fd592007-02-20 13:57:54 -08001225 return 0;
1226}
1227
1228late_initcall(debugfs_kprobe_init);
1229#endif /* CONFIG_DEBUG_FS */
1230
1231module_init(init_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233EXPORT_SYMBOL_GPL(register_kprobe);
1234EXPORT_SYMBOL_GPL(unregister_kprobe);
Masami Hiramatsu98616682008-04-28 02:14:28 -07001235EXPORT_SYMBOL_GPL(register_kprobes);
1236EXPORT_SYMBOL_GPL(unregister_kprobes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237EXPORT_SYMBOL_GPL(register_jprobe);
1238EXPORT_SYMBOL_GPL(unregister_jprobe);
Peter Chubbcd5bfea2007-08-10 13:01:10 -07001239#ifdef CONFIG_KPROBES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240EXPORT_SYMBOL_GPL(jprobe_return);
Peter Chubbcd5bfea2007-08-10 13:01:10 -07001241#endif
1242
1243#ifdef CONFIG_KPROBES
Hien Nguyenb94cce92005-06-23 00:09:19 -07001244EXPORT_SYMBOL_GPL(register_kretprobe);
1245EXPORT_SYMBOL_GPL(unregister_kretprobe);
Masami Hiramatsu4a296e02008-04-28 02:14:29 -07001246EXPORT_SYMBOL_GPL(register_kretprobes);
1247EXPORT_SYMBOL_GPL(unregister_kretprobes);
Peter Chubbcd5bfea2007-08-10 13:01:10 -07001248#endif