blob: 17ec4afb0994e48adc1c7a9cfde971f04b08d512 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070039#include <linux/moduleloader.h>
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070040#include <linux/kallsyms.h>
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080041#include <linux/freezer.h>
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -070042#include <asm-generic/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/cacheflush.h>
44#include <asm/errno.h>
45#include <asm/kdebug.h>
46
47#define KPROBE_HASH_BITS 6
48#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
49
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -070050
51/*
52 * Some oddball architectures like 64bit powerpc have function descriptors
53 * so this must be overridable.
54 */
55#ifndef kprobe_lookup_name
56#define kprobe_lookup_name(name, addr) \
57 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
58#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
Hien Nguyenb94cce92005-06-23 00:09:19 -070061static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -070062static atomic_t kprobe_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -080064DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -080065DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -080066static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -070068static struct notifier_block kprobe_page_fault_nb = {
69 .notifier_call = kprobe_exceptions_notify,
70 .priority = 0x7fffffff /* we need to notified first */
71};
72
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -080073#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070074/*
75 * kprobe->ainsn.insn points to the copy of the instruction to be
76 * single-stepped. x86_64, POWER4 and above have no-exec support and
77 * stepping on the instruction on a vmalloced/kmalloced/data page
78 * is a recipe for disaster
79 */
80#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
81
82struct kprobe_insn_page {
83 struct hlist_node hlist;
84 kprobe_opcode_t *insns; /* Page of instruction slots */
85 char slot_used[INSNS_PER_PAGE];
86 int nused;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080087 int ngarbage;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070088};
89
90static struct hlist_head kprobe_insn_pages;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -080091static int kprobe_garbage_slots;
92static int collect_garbage_slots(void);
93
94static int __kprobes check_safety(void)
95{
96 int ret = 0;
97#if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
98 ret = freeze_processes();
99 if (ret == 0) {
100 struct task_struct *p, *q;
101 do_each_thread(p, q) {
102 if (p != current && p->state == TASK_RUNNING &&
103 p->pid != 0) {
104 printk("Check failed: %s is running\n",p->comm);
105 ret = -1;
106 goto loop_end;
107 }
108 } while_each_thread(p, q);
109 }
110loop_end:
111 thaw_processes();
112#else
113 synchronize_sched();
114#endif
115 return ret;
116}
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700117
118/**
119 * get_insn_slot() - Find a slot on an executable page for an instruction.
120 * We allocate an executable page if there's no room on existing ones.
121 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700122kprobe_opcode_t __kprobes *get_insn_slot(void)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700123{
124 struct kprobe_insn_page *kip;
125 struct hlist_node *pos;
126
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800127 retry:
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700128 hlist_for_each(pos, &kprobe_insn_pages) {
129 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
130 if (kip->nused < INSNS_PER_PAGE) {
131 int i;
132 for (i = 0; i < INSNS_PER_PAGE; i++) {
133 if (!kip->slot_used[i]) {
134 kip->slot_used[i] = 1;
135 kip->nused++;
136 return kip->insns + (i * MAX_INSN_SIZE);
137 }
138 }
139 /* Surprise! No unused slots. Fix kip->nused. */
140 kip->nused = INSNS_PER_PAGE;
141 }
142 }
143
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800144 /* If there are any garbage slots, collect it and try again. */
145 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
146 goto retry;
147 }
148 /* All out of space. Need to allocate a new page. Use slot 0. */
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700149 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
150 if (!kip) {
151 return NULL;
152 }
153
154 /*
155 * Use module_alloc so this page is within +/- 2GB of where the
156 * kernel image and loaded module images reside. This is required
157 * so x86_64 can correctly handle the %rip-relative fixups.
158 */
159 kip->insns = module_alloc(PAGE_SIZE);
160 if (!kip->insns) {
161 kfree(kip);
162 return NULL;
163 }
164 INIT_HLIST_NODE(&kip->hlist);
165 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
166 memset(kip->slot_used, 0, INSNS_PER_PAGE);
167 kip->slot_used[0] = 1;
168 kip->nused = 1;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800169 kip->ngarbage = 0;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700170 return kip->insns;
171}
172
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800173/* Return 1 if all garbages are collected, otherwise 0. */
174static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
175{
176 kip->slot_used[idx] = 0;
177 kip->nused--;
178 if (kip->nused == 0) {
179 /*
180 * Page is no longer in use. Free it unless
181 * it's the last one. We keep the last one
182 * so as not to have to set it up again the
183 * next time somebody inserts a probe.
184 */
185 hlist_del(&kip->hlist);
186 if (hlist_empty(&kprobe_insn_pages)) {
187 INIT_HLIST_NODE(&kip->hlist);
188 hlist_add_head(&kip->hlist,
189 &kprobe_insn_pages);
190 } else {
191 module_free(NULL, kip->insns);
192 kfree(kip);
193 }
194 return 1;
195 }
196 return 0;
197}
198
199static int __kprobes collect_garbage_slots(void)
200{
201 struct kprobe_insn_page *kip;
202 struct hlist_node *pos, *next;
203
204 /* Ensure no-one is preepmted on the garbages */
205 if (check_safety() != 0)
206 return -EAGAIN;
207
208 hlist_for_each_safe(pos, next, &kprobe_insn_pages) {
209 int i;
210 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
211 if (kip->ngarbage == 0)
212 continue;
213 kip->ngarbage = 0; /* we will collect all garbages */
214 for (i = 0; i < INSNS_PER_PAGE; i++) {
215 if (kip->slot_used[i] == -1 &&
216 collect_one_slot(kip, i))
217 break;
218 }
219 }
220 kprobe_garbage_slots = 0;
221 return 0;
222}
223
224void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700225{
226 struct kprobe_insn_page *kip;
227 struct hlist_node *pos;
228
229 hlist_for_each(pos, &kprobe_insn_pages) {
230 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
231 if (kip->insns <= slot &&
232 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
233 int i = (slot - kip->insns) / MAX_INSN_SIZE;
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800234 if (dirty) {
235 kip->slot_used[i] = -1;
236 kip->ngarbage++;
237 } else {
238 collect_one_slot(kip, i);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700239 }
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800240 break;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700241 }
242 }
Masami Hiramatsub4c6c342006-12-06 20:38:11 -0800243 if (dirty && (++kprobe_garbage_slots > INSNS_PER_PAGE)) {
244 collect_garbage_slots();
245 }
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700246}
Anil S Keshavamurthy2d14e392006-01-09 20:52:41 -0800247#endif
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700248
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800249/* We have preemption disabled.. so it is safe to use __ versions */
250static inline void set_kprobe_instance(struct kprobe *kp)
251{
252 __get_cpu_var(kprobe_instance) = kp;
253}
254
255static inline void reset_kprobe_instance(void)
256{
257 __get_cpu_var(kprobe_instance) = NULL;
258}
259
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800260/*
261 * This routine is called either:
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800262 * - under the kprobe_mutex - during kprobe_[un]register()
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800263 * OR
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800264 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800265 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700266struct kprobe __kprobes *get_kprobe(void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct hlist_head *head;
269 struct hlist_node *node;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800270 struct kprobe *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800273 hlist_for_each_entry_rcu(p, node, head, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (p->addr == addr)
275 return p;
276 }
277 return NULL;
278}
279
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700280/*
281 * Aggregate handlers for multiple kprobes support - these handlers
282 * take care of invoking the individual kprobe handlers on p->list
283 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700284static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700285{
286 struct kprobe *kp;
287
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800288 list_for_each_entry_rcu(kp, &p->list, list) {
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700289 if (kp->pre_handler) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800290 set_kprobe_instance(kp);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700291 if (kp->pre_handler(kp, regs))
292 return 1;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700293 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800294 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700295 }
296 return 0;
297}
298
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700299static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
300 unsigned long flags)
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->post_handler) {
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800306 set_kprobe_instance(kp);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700307 kp->post_handler(kp, regs, flags);
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800308 reset_kprobe_instance();
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700309 }
310 }
311 return;
312}
313
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700314static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
315 int trapnr)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700316{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800317 struct kprobe *cur = __get_cpu_var(kprobe_instance);
318
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700319 /*
320 * if we faulted "during" the execution of a user specified
321 * probe handler, invoke just that probe's fault handler
322 */
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800323 if (cur && cur->fault_handler) {
324 if (cur->fault_handler(cur, regs, trapnr))
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700325 return 1;
326 }
327 return 0;
328}
329
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700330static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700331{
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800332 struct kprobe *cur = __get_cpu_var(kprobe_instance);
333 int ret = 0;
334
335 if (cur && cur->break_handler) {
336 if (cur->break_handler(cur, regs))
337 ret = 1;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700338 }
Ananth N Mavinakayanahallie6584522005-11-07 01:00:07 -0800339 reset_kprobe_instance();
340 return ret;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700341}
342
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800343/* Walks the list and increments nmissed count for multiprobe case */
344void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
345{
346 struct kprobe *kp;
347 if (p->pre_handler != aggr_pre_handler) {
348 p->nmissed++;
349 } else {
350 list_for_each_entry_rcu(kp, &p->list, list)
351 kp->nmissed++;
352 }
353 return;
354}
355
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800356/* Called with kretprobe_lock held */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700357struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700358{
359 struct hlist_node *node;
360 struct kretprobe_instance *ri;
361 hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
362 return ri;
363 return NULL;
364}
365
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800366/* Called with kretprobe_lock held */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700367static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe
368 *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700369{
370 struct hlist_node *node;
371 struct kretprobe_instance *ri;
372 hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
373 return ri;
374 return NULL;
375}
376
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800377/* Called with kretprobe_lock held */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700378void __kprobes add_rp_inst(struct kretprobe_instance *ri)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700379{
Hien Nguyenb94cce92005-06-23 00:09:19 -0700380 /*
381 * Remove rp inst off the free list -
382 * Add it back when probed function returns
383 */
384 hlist_del(&ri->uflist);
Rusty Lynch802eae72005-06-27 15:17:08 -0700385
Hien Nguyenb94cce92005-06-23 00:09:19 -0700386 /* Add rp inst onto table */
387 INIT_HLIST_NODE(&ri->hlist);
388 hlist_add_head(&ri->hlist,
Rusty Lynch802eae72005-06-27 15:17:08 -0700389 &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700390
391 /* Also add this rp inst to the used list. */
392 INIT_HLIST_NODE(&ri->uflist);
393 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
394}
395
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800396/* Called with kretprobe_lock held */
bibo,mao99219a32006-10-02 02:17:35 -0700397void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
398 struct hlist_head *head)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700399{
400 /* remove rp inst off the rprobe_inst_table */
401 hlist_del(&ri->hlist);
402 if (ri->rp) {
403 /* remove rp inst off the used list */
404 hlist_del(&ri->uflist);
405 /* put rp inst back onto the free list */
406 INIT_HLIST_NODE(&ri->uflist);
407 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
408 } else
409 /* Unregistering */
bibo,mao99219a32006-10-02 02:17:35 -0700410 hlist_add_head(&ri->hlist, head);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700411}
412
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700413struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700414{
415 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
416}
417
Hien Nguyenb94cce92005-06-23 00:09:19 -0700418/*
bibo maoc6fd91f2006-03-26 01:38:20 -0800419 * This function is called from finish_task_switch when task tk becomes dead,
420 * so that we can recycle any function-return probe instances associated
421 * with this task. These left over instances represent probed functions
422 * that have been called but will never return.
Hien Nguyenb94cce92005-06-23 00:09:19 -0700423 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700424void __kprobes kprobe_flush_task(struct task_struct *tk)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700425{
bibo,mao62c27be2006-10-02 02:17:33 -0700426 struct kretprobe_instance *ri;
bibo,mao99219a32006-10-02 02:17:35 -0700427 struct hlist_head *head, empty_rp;
Rusty Lynch802eae72005-06-27 15:17:08 -0700428 struct hlist_node *node, *tmp;
Hien Nguyen0aa55e42005-06-23 00:09:26 -0700429 unsigned long flags = 0;
Rusty Lynch802eae72005-06-27 15:17:08 -0700430
bibo,mao99219a32006-10-02 02:17:35 -0700431 INIT_HLIST_HEAD(&empty_rp);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800432 spin_lock_irqsave(&kretprobe_lock, flags);
bibo,mao62c27be2006-10-02 02:17:33 -0700433 head = kretprobe_inst_table_head(tk);
434 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
435 if (ri->task == tk)
bibo,mao99219a32006-10-02 02:17:35 -0700436 recycle_rp_inst(ri, &empty_rp);
bibo,mao62c27be2006-10-02 02:17:33 -0700437 }
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800438 spin_unlock_irqrestore(&kretprobe_lock, flags);
bibo,mao99219a32006-10-02 02:17:35 -0700439
440 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
441 hlist_del(&ri->hlist);
442 kfree(ri);
443 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700444}
445
Hien Nguyenb94cce92005-06-23 00:09:19 -0700446static inline void free_rp_inst(struct kretprobe *rp)
447{
448 struct kretprobe_instance *ri;
449 while ((ri = get_free_rp_inst(rp)) != NULL) {
450 hlist_del(&ri->uflist);
451 kfree(ri);
452 }
453}
454
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700455/*
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700456 * Keep all fields in the kprobe consistent
457 */
458static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
459{
460 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
461 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
462}
463
464/*
465* Add the new probe to old_p->list. Fail if this is the
466* second jprobe at the address - two jprobes can't coexist
467*/
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700468static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700469{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700470 if (p->break_handler) {
mao, bibo36721652006-06-26 00:25:22 -0700471 if (old_p->break_handler)
472 return -EEXIST;
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800473 list_add_tail_rcu(&p->list, &old_p->list);
mao, bibo36721652006-06-26 00:25:22 -0700474 old_p->break_handler = aggr_break_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700475 } else
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800476 list_add_rcu(&p->list, &old_p->list);
mao, bibo36721652006-06-26 00:25:22 -0700477 if (p->post_handler && !old_p->post_handler)
478 old_p->post_handler = aggr_post_handler;
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700479 return 0;
480}
481
482/*
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700483 * Fill in the required fields of the "manager kprobe". Replace the
484 * earlier kprobe in the hlist with the manager kprobe
485 */
486static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
487{
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700488 copy_kprobe(p, ap);
bibo, maoa9ad9652006-07-30 03:03:26 -0700489 flush_insn_slot(ap);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700490 ap->addr = p->addr;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700491 ap->pre_handler = aggr_pre_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700492 ap->fault_handler = aggr_fault_handler;
mao, bibo36721652006-06-26 00:25:22 -0700493 if (p->post_handler)
494 ap->post_handler = aggr_post_handler;
495 if (p->break_handler)
496 ap->break_handler = aggr_break_handler;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700497
498 INIT_LIST_HEAD(&ap->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800499 list_add_rcu(&p->list, &ap->list);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700500
Keshavamurthy Anil Sadad0f32005-12-12 00:37:12 -0800501 hlist_replace_rcu(&p->hlist, &ap->hlist);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700502}
503
504/*
505 * This is the second or subsequent kprobe at the address - handle
506 * the intricacies
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700507 */
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700508static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
509 struct kprobe *p)
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700510{
511 int ret = 0;
512 struct kprobe *ap;
513
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700514 if (old_p->pre_handler == aggr_pre_handler) {
515 copy_kprobe(old_p, p);
516 ret = add_new_kprobe(old_p, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700517 } else {
Keshavamurthy Anil Sa0d50062006-01-09 20:52:46 -0800518 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700519 if (!ap)
520 return -ENOMEM;
521 add_aggr_kprobe(ap, old_p);
Prasanna S Panchamukhi8b0914e2005-06-23 00:09:41 -0700522 copy_kprobe(ap, p);
523 ret = add_new_kprobe(ap, p);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700524 }
525 return ret;
526}
527
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700528static int __kprobes in_kprobes_functions(unsigned long addr)
529{
530 if (addr >= (unsigned long)__kprobes_text_start
531 && addr < (unsigned long)__kprobes_text_end)
532 return -EINVAL;
533 return 0;
534}
535
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800536static int __kprobes __register_kprobe(struct kprobe *p,
537 unsigned long called_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 int ret = 0;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700540 struct kprobe *old_p;
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800541 struct module *probed_mod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Ananth N Mavinakayanahalli3a872d82006-10-02 02:17:30 -0700543 /*
544 * If we have a symbol_name argument look it up,
545 * and add it to the address. That way the addr
546 * field can either be global or relative to a symbol.
547 */
548 if (p->symbol_name) {
549 if (p->addr)
550 return -EINVAL;
551 kprobe_lookup_name(p->symbol_name, p->addr);
552 }
553
554 if (!p->addr)
555 return -EINVAL;
556 p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
557
Mao, Bibob3e55c72005-12-12 00:37:00 -0800558 if ((!kernel_text_address((unsigned long) p->addr)) ||
559 in_kprobes_functions((unsigned long) p->addr))
560 return -EINVAL;
561
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800562 p->mod_refcounted = 0;
563 /* Check are we probing a module */
564 if ((probed_mod = module_text_address((unsigned long) p->addr))) {
565 struct module *calling_mod = module_text_address(called_from);
566 /* We must allow modules to probe themself and
567 * in this case avoid incrementing the module refcount,
568 * so as to allow unloading of self probing modules.
569 */
570 if (calling_mod && (calling_mod != probed_mod)) {
571 if (unlikely(!try_module_get(probed_mod)))
572 return -EINVAL;
573 p->mod_refcounted = 1;
574 } else
575 probed_mod = NULL;
576 }
Mao, Bibob3e55c72005-12-12 00:37:00 -0800577
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800578 p->nmissed = 0;
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800579 mutex_lock(&kprobe_mutex);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700580 old_p = get_kprobe(p->addr);
581 if (old_p) {
582 ret = register_aggr_kprobe(old_p, p);
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -0700583 if (!ret)
584 atomic_inc(&kprobe_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto out;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800588 if ((ret = arch_prepare_kprobe(p)) != 0)
589 goto out;
590
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700591 INIT_HLIST_NODE(&p->hlist);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800592 hlist_add_head_rcu(&p->hlist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
594
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -0700595 if (atomic_add_return(1, &kprobe_count) == \
596 (ARCH_INACTIVE_KPROBE_COUNT + 1))
597 register_page_fault_notifier(&kprobe_page_fault_nb);
598
bibo,mao62c27be2006-10-02 02:17:33 -0700599 arch_arm_kprobe(p);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601out:
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800602 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800603
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800604 if (ret && probed_mod)
605 module_put(probed_mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return ret;
607}
608
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800609int __kprobes register_kprobe(struct kprobe *p)
610{
611 return __register_kprobe(p,
612 (unsigned long)__builtin_return_address(0));
613}
614
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700615void __kprobes unregister_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Mao, Bibob3e55c72005-12-12 00:37:00 -0800617 struct module *mod;
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800618 struct kprobe *old_p, *list_p;
619 int cleanup_p;
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700620
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800621 mutex_lock(&kprobe_mutex);
Ananth N Mavinakayanahalli64f562c2005-05-05 16:15:42 -0700622 old_p = get_kprobe(p->addr);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800623 if (unlikely(!old_p)) {
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800624 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800625 return;
626 }
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800627 if (p != old_p) {
628 list_for_each_entry_rcu(list_p, &old_p->list, list)
629 if (list_p == p)
630 /* kprobe p is a valid probe */
631 goto valid_p;
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800632 mutex_unlock(&kprobe_mutex);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800633 return;
634 }
635valid_p:
636 if ((old_p == p) || ((old_p->pre_handler == aggr_pre_handler) &&
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800637 (p->list.next == &old_p->list) &&
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800638 (p->list.prev == &old_p->list))) {
639 /* Only probe on the hash list */
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800640 arch_disarm_kprobe(p);
641 hlist_del_rcu(&old_p->hlist);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800642 cleanup_p = 1;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800643 } else {
644 list_del_rcu(&p->list);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800645 cleanup_p = 0;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800646 }
Mao, Bibob3e55c72005-12-12 00:37:00 -0800647
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800648 mutex_unlock(&kprobe_mutex);
Mao, Bibob3e55c72005-12-12 00:37:00 -0800649
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800650 synchronize_sched();
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800651 if (p->mod_refcounted &&
652 (mod = module_text_address((unsigned long)p->addr)))
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800653 module_put(mod);
654
655 if (cleanup_p) {
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800656 if (p != old_p) {
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800657 list_del_rcu(&p->list);
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800658 kfree(old_p);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800659 }
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800660 arch_remove_kprobe(p);
mao, bibo36721652006-06-26 00:25:22 -0700661 } else {
662 mutex_lock(&kprobe_mutex);
663 if (p->break_handler)
664 old_p->break_handler = NULL;
665 if (p->post_handler){
666 list_for_each_entry_rcu(list_p, &old_p->list, list){
667 if (list_p->post_handler){
668 cleanup_p = 2;
669 break;
670 }
671 }
672 if (cleanup_p == 0)
673 old_p->post_handler = NULL;
674 }
675 mutex_unlock(&kprobe_mutex);
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800676 }
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -0700677
678 /* Call unregister_page_fault_notifier()
679 * if no probes are active
680 */
681 mutex_lock(&kprobe_mutex);
682 if (atomic_add_return(-1, &kprobe_count) == \
683 ARCH_INACTIVE_KPROBE_COUNT)
684 unregister_page_fault_notifier(&kprobe_page_fault_nb);
685 mutex_unlock(&kprobe_mutex);
686 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static struct notifier_block kprobe_exceptions_nb = {
690 .notifier_call = kprobe_exceptions_notify,
Anil S Keshavamurthy3d5631e2006-06-26 00:25:28 -0700691 .priority = 0x7fffffff /* we need to be notified first */
692};
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700695int __kprobes register_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 /* Todo: Verify probepoint is a function entry point */
698 jp->kp.pre_handler = setjmp_pre_handler;
699 jp->kp.break_handler = longjmp_break_handler;
700
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800701 return __register_kprobe(&jp->kp,
702 (unsigned long)__builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700705void __kprobes unregister_jprobe(struct jprobe *jp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 unregister_kprobe(&jp->kp);
708}
709
Hien Nguyenb94cce92005-06-23 00:09:19 -0700710#ifdef ARCH_SUPPORTS_KRETPROBES
711
Adrian Bunke65cefe2006-02-03 03:03:42 -0800712/*
713 * This kprobe pre_handler is registered with every kretprobe. When probe
714 * hits it will set up the return probe.
715 */
716static int __kprobes pre_handler_kretprobe(struct kprobe *p,
717 struct pt_regs *regs)
718{
719 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
720 unsigned long flags = 0;
721
722 /*TODO: consider to only swap the RA after the last pre_handler fired */
723 spin_lock_irqsave(&kretprobe_lock, flags);
724 arch_prepare_kretprobe(rp, regs);
725 spin_unlock_irqrestore(&kretprobe_lock, flags);
726 return 0;
727}
728
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700729int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700730{
731 int ret = 0;
732 struct kretprobe_instance *inst;
733 int i;
734
735 rp->kp.pre_handler = pre_handler_kretprobe;
Ananth N Mavinakayanahalli7522a842006-04-20 02:43:11 -0700736 rp->kp.post_handler = NULL;
737 rp->kp.fault_handler = NULL;
738 rp->kp.break_handler = NULL;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700739
740 /* Pre-allocate memory for max kretprobe instances */
741 if (rp->maxactive <= 0) {
742#ifdef CONFIG_PREEMPT
743 rp->maxactive = max(10, 2 * NR_CPUS);
744#else
745 rp->maxactive = NR_CPUS;
746#endif
747 }
748 INIT_HLIST_HEAD(&rp->used_instances);
749 INIT_HLIST_HEAD(&rp->free_instances);
750 for (i = 0; i < rp->maxactive; i++) {
751 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
752 if (inst == NULL) {
753 free_rp_inst(rp);
754 return -ENOMEM;
755 }
756 INIT_HLIST_NODE(&inst->uflist);
757 hlist_add_head(&inst->uflist, &rp->free_instances);
758 }
759
760 rp->nmissed = 0;
761 /* Establish function entry probe point */
Keshavamurthy Anil Sdf019b12006-01-11 12:17:41 -0800762 if ((ret = __register_kprobe(&rp->kp,
763 (unsigned long)__builtin_return_address(0))) != 0)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700764 free_rp_inst(rp);
765 return ret;
766}
767
768#else /* ARCH_SUPPORTS_KRETPROBES */
769
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700770int __kprobes register_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700771{
772 return -ENOSYS;
773}
774
775#endif /* ARCH_SUPPORTS_KRETPROBES */
776
Prasanna S Panchamukhid0aaff92005-09-06 15:19:26 -0700777void __kprobes unregister_kretprobe(struct kretprobe *rp)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700778{
779 unsigned long flags;
780 struct kretprobe_instance *ri;
781
782 unregister_kprobe(&rp->kp);
783 /* No race here */
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800784 spin_lock_irqsave(&kretprobe_lock, flags);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700785 while ((ri = get_used_rp_inst(rp)) != NULL) {
786 ri->rp = NULL;
787 hlist_del(&ri->uflist);
788 }
Ananth N Mavinakayanahalli3516a462005-11-07 01:00:13 -0800789 spin_unlock_irqrestore(&kretprobe_lock, flags);
Ananth N Mavinakayanahalli278ff952006-02-03 03:03:43 -0800790 free_rp_inst(rp);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793static int __init init_kprobes(void)
794{
795 int i, err = 0;
796
797 /* FIXME allocate the probe table, currently defined statically */
798 /* initialize all list heads */
Hien Nguyenb94cce92005-06-23 00:09:19 -0700799 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 INIT_HLIST_HEAD(&kprobe_table[i]);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700801 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
802 }
Anil S Keshavamurthye6f47f92006-06-26 00:25:29 -0700803 atomic_set(&kprobe_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Rusty Lynch67729262005-07-05 18:54:50 -0700805 err = arch_init_kprobes();
Rusty Lynch802eae72005-06-27 15:17:08 -0700806 if (!err)
807 err = register_die_notifier(&kprobe_exceptions_nb);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return err;
810}
811
812__initcall(init_kprobes);
813
814EXPORT_SYMBOL_GPL(register_kprobe);
815EXPORT_SYMBOL_GPL(unregister_kprobe);
816EXPORT_SYMBOL_GPL(register_jprobe);
817EXPORT_SYMBOL_GPL(unregister_jprobe);
818EXPORT_SYMBOL_GPL(jprobe_return);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700819EXPORT_SYMBOL_GPL(register_kretprobe);
820EXPORT_SYMBOL_GPL(unregister_kretprobe);
821