input: add I2C to config since the driver makes several i2c*() calls
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / kprobes.c
CommitLineData
1da177e4
LT
1/*
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.
b94cce92
HN
30 * 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.
1da177e4
LT
33 */
34#include <linux/kprobes.h>
1da177e4
LT
35#include <linux/hash.h>
36#include <linux/init.h>
4e57b681 37#include <linux/slab.h>
e3869792 38#include <linux/stddef.h>
1da177e4 39#include <linux/module.h>
9ec4b1f3 40#include <linux/moduleloader.h>
3a872d89 41#include <linux/kallsyms.h>
b4c6c34a 42#include <linux/freezer.h>
346fd59b
SD
43#include <linux/seq_file.h>
44#include <linux/debugfs.h>
1eeb66a1 45#include <linux/kdebug.h>
bf8f6e5b 46
d0aaff97 47#include <asm-generic/sections.h>
1da177e4
LT
48#include <asm/cacheflush.h>
49#include <asm/errno.h>
bf8f6e5b 50#include <asm/uaccess.h>
1da177e4
LT
51
52#define KPROBE_HASH_BITS 6
53#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
54
3a872d89
AM
55
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
1da177e4 65static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
b94cce92 66static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
1da177e4 67
bf8f6e5b
AM
68/* NOTE: change this value only with kprobe_mutex held */
69static bool kprobe_enabled;
70
7a7d1cf9 71DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
3516a460 72DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
e6584523 73static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
1da177e4 74
2d14e39d 75#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
9ec4b1f3
AM
76/*
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;
b4c6c34a 89 int ngarbage;
9ec4b1f3
AM
90};
91
ab40c5c6
MH
92enum kprobe_slot_state {
93 SLOT_CLEAN = 0,
94 SLOT_DIRTY = 1,
95 SLOT_USED = 2,
96};
97
9ec4b1f3 98static struct hlist_head kprobe_insn_pages;
b4c6c34a
MH
99static 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}
9ec4b1f3
AM
125
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 */
d0aaff97 130kprobe_opcode_t __kprobes *get_insn_slot(void)
9ec4b1f3
AM
131{
132 struct kprobe_insn_page *kip;
133 struct hlist_node *pos;
134
6f716acd 135 retry:
b0bb5016 136 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
9ec4b1f3
AM
137 if (kip->nused < INSNS_PER_PAGE) {
138 int i;
139 for (i = 0; i < INSNS_PER_PAGE; i++) {
ab40c5c6
MH
140 if (kip->slot_used[i] == SLOT_CLEAN) {
141 kip->slot_used[i] = SLOT_USED;
9ec4b1f3
AM
142 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
b4c6c34a
MH
151 /* 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. */
9ec4b1f3 156 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
6f716acd 157 if (!kip)
9ec4b1f3 158 return NULL;
9ec4b1f3
AM
159
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);
ab40c5c6
MH
172 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
173 kip->slot_used[0] = SLOT_USED;
9ec4b1f3 174 kip->nused = 1;
b4c6c34a 175 kip->ngarbage = 0;
9ec4b1f3
AM
176 return kip->insns;
177}
178
b4c6c34a
MH
179/* Return 1 if all garbages are collected, otherwise 0. */
180static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
181{
ab40c5c6 182 kip->slot_used[idx] = SLOT_CLEAN;
b4c6c34a
MH
183 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
b0bb5016 214 hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
b4c6c34a 215 int i;
b4c6c34a
MH
216 if (kip->ngarbage == 0)
217 continue;
218 kip->ngarbage = 0; /* we will collect all garbages */
219 for (i = 0; i < INSNS_PER_PAGE; i++) {
ab40c5c6 220 if (kip->slot_used[i] == SLOT_DIRTY &&
b4c6c34a
MH
221 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)
9ec4b1f3
AM
230{
231 struct kprobe_insn_page *kip;
232 struct hlist_node *pos;
233
b0bb5016 234 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
9ec4b1f3
AM
235 if (kip->insns <= slot &&
236 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
237 int i = (slot - kip->insns) / MAX_INSN_SIZE;
b4c6c34a 238 if (dirty) {
ab40c5c6 239 kip->slot_used[i] = SLOT_DIRTY;
b4c6c34a
MH
240 kip->ngarbage++;
241 } else {
242 collect_one_slot(kip, i);
9ec4b1f3 243 }
b4c6c34a 244 break;
9ec4b1f3
AM
245 }
246 }
6f716acd
CH
247
248 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
b4c6c34a 249 collect_garbage_slots();
9ec4b1f3 250}
2d14e39d 251#endif
9ec4b1f3 252
e6584523
AM
253/* 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
3516a460
AM
264/*
265 * This routine is called either:
49a2a1b8 266 * - under the kprobe_mutex - during kprobe_[un]register()
3516a460 267 * OR
d217d545 268 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
3516a460 269 */
d0aaff97 270struct kprobe __kprobes *get_kprobe(void *addr)
1da177e4
LT
271{
272 struct hlist_head *head;
273 struct hlist_node *node;
3516a460 274 struct kprobe *p;
1da177e4
LT
275
276 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
3516a460 277 hlist_for_each_entry_rcu(p, node, head, hlist) {
1da177e4
LT
278 if (p->addr == addr)
279 return p;
280 }
281 return NULL;
282}
283
64f562c6
AM
284/*
285 * Aggregate handlers for multiple kprobes support - these handlers
286 * take care of invoking the individual kprobe handlers on p->list
287 */
d0aaff97 288static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
64f562c6
AM
289{
290 struct kprobe *kp;
291
3516a460 292 list_for_each_entry_rcu(kp, &p->list, list) {
64f562c6 293 if (kp->pre_handler) {
e6584523 294 set_kprobe_instance(kp);
8b0914ea
PP
295 if (kp->pre_handler(kp, regs))
296 return 1;
64f562c6 297 }
e6584523 298 reset_kprobe_instance();
64f562c6
AM
299 }
300 return 0;
301}
302
d0aaff97
PP
303static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
304 unsigned long flags)
64f562c6
AM
305{
306 struct kprobe *kp;
307
3516a460 308 list_for_each_entry_rcu(kp, &p->list, list) {
64f562c6 309 if (kp->post_handler) {
e6584523 310 set_kprobe_instance(kp);
64f562c6 311 kp->post_handler(kp, regs, flags);
e6584523 312 reset_kprobe_instance();
64f562c6
AM
313 }
314 }
64f562c6
AM
315}
316
d0aaff97
PP
317static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
318 int trapnr)
64f562c6 319{
e6584523
AM
320 struct kprobe *cur = __get_cpu_var(kprobe_instance);
321
64f562c6
AM
322 /*
323 * if we faulted "during" the execution of a user specified
324 * probe handler, invoke just that probe's fault handler
325 */
e6584523
AM
326 if (cur && cur->fault_handler) {
327 if (cur->fault_handler(cur, regs, trapnr))
64f562c6
AM
328 return 1;
329 }
330 return 0;
331}
332
d0aaff97 333static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
8b0914ea 334{
e6584523
AM
335 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;
8b0914ea 341 }
e6584523
AM
342 reset_kprobe_instance();
343 return ret;
8b0914ea
PP
344}
345
bf8d5c52
KA
346/* 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
3516a460 359/* Called with kretprobe_lock held */
99219a3f 360void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
361 struct hlist_head *head)
b94cce92
HN
362{
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 */
99219a3f 373 hlist_add_head(&ri->hlist, head);
b94cce92
HN
374}
375
d0aaff97 376struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
b94cce92
HN
377{
378 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
379}
380
b94cce92 381/*
c6fd91f0 382 * 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.
b94cce92 386 */
d0aaff97 387void __kprobes kprobe_flush_task(struct task_struct *tk)
b94cce92 388{
62c27be0 389 struct kretprobe_instance *ri;
99219a3f 390 struct hlist_head *head, empty_rp;
802eae7c 391 struct hlist_node *node, *tmp;
0aa55e4d 392 unsigned long flags = 0;
802eae7c 393
99219a3f 394 INIT_HLIST_HEAD(&empty_rp);
3516a460 395 spin_lock_irqsave(&kretprobe_lock, flags);
62c27be0 396 head = kretprobe_inst_table_head(tk);
397 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
398 if (ri->task == tk)
99219a3f 399 recycle_rp_inst(ri, &empty_rp);
62c27be0 400 }
3516a460 401 spin_unlock_irqrestore(&kretprobe_lock, flags);
99219a3f 402
403 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
404 hlist_del(&ri->hlist);
405 kfree(ri);
406 }
b94cce92
HN
407}
408
b94cce92
HN
409static inline void free_rp_inst(struct kretprobe *rp)
410{
411 struct kretprobe_instance *ri;
4c4308cb
CH
412 struct hlist_node *pos, *next;
413
414 hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
b94cce92
HN
415 hlist_del(&ri->uflist);
416 kfree(ri);
417 }
418}
419
8b0914ea
PP
420/*
421 * 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*/
d0aaff97 433static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
8b0914ea 434{
8b0914ea 435 if (p->break_handler) {
36721656 436 if (old_p->break_handler)
437 return -EEXIST;
3516a460 438 list_add_tail_rcu(&p->list, &old_p->list);
36721656 439 old_p->break_handler = aggr_break_handler;
8b0914ea 440 } else
3516a460 441 list_add_rcu(&p->list, &old_p->list);
36721656 442 if (p->post_handler && !old_p->post_handler)
443 old_p->post_handler = aggr_post_handler;
8b0914ea
PP
444 return 0;
445}
446
64f562c6
AM
447/*
448 * 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{
8b0914ea 453 copy_kprobe(p, ap);
a9ad965e 454 flush_insn_slot(ap);
64f562c6 455 ap->addr = p->addr;
64f562c6 456 ap->pre_handler = aggr_pre_handler;
64f562c6 457 ap->fault_handler = aggr_fault_handler;
36721656 458 if (p->post_handler)
459 ap->post_handler = aggr_post_handler;
460 if (p->break_handler)
461 ap->break_handler = aggr_break_handler;
64f562c6
AM
462
463 INIT_LIST_HEAD(&ap->list);
3516a460 464 list_add_rcu(&p->list, &ap->list);
64f562c6 465
adad0f33 466 hlist_replace_rcu(&p->hlist, &ap->hlist);
64f562c6
AM
467}
468
469/*
470 * This is the second or subsequent kprobe at the address - handle
471 * the intricacies
64f562c6 472 */
d0aaff97
PP
473static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
474 struct kprobe *p)
64f562c6
AM
475{
476 int ret = 0;
477 struct kprobe *ap;
478
8b0914ea
PP
479 if (old_p->pre_handler == aggr_pre_handler) {
480 copy_kprobe(old_p, p);
481 ret = add_new_kprobe(old_p, p);
64f562c6 482 } else {
a0d50069 483 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
64f562c6
AM
484 if (!ap)
485 return -ENOMEM;
486 add_aggr_kprobe(ap, old_p);
8b0914ea
PP
487 copy_kprobe(ap, p);
488 ret = add_new_kprobe(ap, p);
64f562c6
AM
489 }
490 return ret;
491}
492
d0aaff97
PP
493static int __kprobes in_kprobes_functions(unsigned long addr)
494{
6f716acd
CH
495 if (addr >= (unsigned long)__kprobes_text_start &&
496 addr < (unsigned long)__kprobes_text_end)
d0aaff97
PP
497 return -EINVAL;
498 return 0;
499}
500
df019b1d
KA
501static int __kprobes __register_kprobe(struct kprobe *p,
502 unsigned long called_from)
1da177e4
LT
503{
504 int ret = 0;
64f562c6 505 struct kprobe *old_p;
df019b1d 506 struct module *probed_mod;
b3e55c72 507
3a872d89
AM
508 /*
509 * If we have a symbol_name argument look it up,
510 * and add it to the address. That way the addr
511 * field can either be global or relative to a symbol.
512 */
513 if (p->symbol_name) {
514 if (p->addr)
515 return -EINVAL;
516 kprobe_lookup_name(p->symbol_name, p->addr);
517 }
518
519 if (!p->addr)
520 return -EINVAL;
521 p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
522
6f716acd
CH
523 if (!kernel_text_address((unsigned long) p->addr) ||
524 in_kprobes_functions((unsigned long) p->addr))
b3e55c72
MB
525 return -EINVAL;
526
df019b1d 527 p->mod_refcounted = 0;
6f716acd
CH
528
529 /*
530 * Check if are we probing a module.
531 */
532 probed_mod = module_text_address((unsigned long) p->addr);
533 if (probed_mod) {
df019b1d 534 struct module *calling_mod = module_text_address(called_from);
6f716acd
CH
535 /*
536 * We must allow modules to probe themself and in this case
537 * avoid incrementing the module refcount, so as to allow
538 * unloading of self probing modules.
df019b1d 539 */
6f716acd 540 if (calling_mod && calling_mod != probed_mod) {
df019b1d
KA
541 if (unlikely(!try_module_get(probed_mod)))
542 return -EINVAL;
543 p->mod_refcounted = 1;
544 } else
545 probed_mod = NULL;
546 }
1da177e4 547
3516a460 548 p->nmissed = 0;
7a7d1cf9 549 mutex_lock(&kprobe_mutex);
64f562c6
AM
550 old_p = get_kprobe(p->addr);
551 if (old_p) {
552 ret = register_aggr_kprobe(old_p, p);
1da177e4
LT
553 goto out;
554 }
1da177e4 555
6f716acd
CH
556 ret = arch_prepare_kprobe(p);
557 if (ret)
49a2a1b8
AK
558 goto out;
559
64f562c6 560 INIT_HLIST_NODE(&p->hlist);
3516a460 561 hlist_add_head_rcu(&p->hlist,
1da177e4
LT
562 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
563
74a0b576 564 if (kprobe_enabled)
bf8f6e5b 565 arch_arm_kprobe(p);
74a0b576 566
1da177e4 567out:
7a7d1cf9 568 mutex_unlock(&kprobe_mutex);
49a2a1b8 569
df019b1d
KA
570 if (ret && probed_mod)
571 module_put(probed_mod);
1da177e4
LT
572 return ret;
573}
574
df019b1d
KA
575int __kprobes register_kprobe(struct kprobe *p)
576{
6f716acd 577 return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
df019b1d
KA
578}
579
d0aaff97 580void __kprobes unregister_kprobe(struct kprobe *p)
1da177e4 581{
b3e55c72 582 struct module *mod;
f709b122
KA
583 struct kprobe *old_p, *list_p;
584 int cleanup_p;
64f562c6 585
7a7d1cf9 586 mutex_lock(&kprobe_mutex);
64f562c6 587 old_p = get_kprobe(p->addr);
49a2a1b8 588 if (unlikely(!old_p)) {
7a7d1cf9 589 mutex_unlock(&kprobe_mutex);
49a2a1b8
AK
590 return;
591 }
f709b122
KA
592 if (p != old_p) {
593 list_for_each_entry_rcu(list_p, &old_p->list, list)
594 if (list_p == p)
595 /* kprobe p is a valid probe */
596 goto valid_p;
7a7d1cf9 597 mutex_unlock(&kprobe_mutex);
f709b122
KA
598 return;
599 }
600valid_p:
6f716acd
CH
601 if (old_p == p ||
602 (old_p->pre_handler == aggr_pre_handler &&
603 p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
bf8f6e5b
AM
604 /*
605 * Only probe on the hash list. Disarm only if kprobes are
606 * enabled - otherwise, the breakpoint would already have
607 * been removed. We save on flushing icache.
608 */
609 if (kprobe_enabled)
610 arch_disarm_kprobe(p);
49a2a1b8 611 hlist_del_rcu(&old_p->hlist);
f709b122 612 cleanup_p = 1;
49a2a1b8
AK
613 } else {
614 list_del_rcu(&p->list);
f709b122 615 cleanup_p = 0;
49a2a1b8 616 }
3516a460 617
7a7d1cf9 618 mutex_unlock(&kprobe_mutex);
b3e55c72 619
49a2a1b8 620 synchronize_sched();
6f716acd
CH
621 if (p->mod_refcounted) {
622 mod = module_text_address((unsigned long)p->addr);
623 if (mod)
624 module_put(mod);
625 }
b3e55c72 626
49a2a1b8 627 if (cleanup_p) {
f709b122 628 if (p != old_p) {
49a2a1b8 629 list_del_rcu(&p->list);
3516a460 630 kfree(old_p);
49a2a1b8 631 }
0498b635 632 arch_remove_kprobe(p);
36721656 633 } else {
634 mutex_lock(&kprobe_mutex);
635 if (p->break_handler)
636 old_p->break_handler = NULL;
637 if (p->post_handler){
638 list_for_each_entry_rcu(list_p, &old_p->list, list){
639 if (list_p->post_handler){
640 cleanup_p = 2;
641 break;
642 }
643 }
644 if (cleanup_p == 0)
645 old_p->post_handler = NULL;
646 }
647 mutex_unlock(&kprobe_mutex);
49a2a1b8 648 }
1da177e4
LT
649}
650
651static struct notifier_block kprobe_exceptions_nb = {
3d5631e0
AK
652 .notifier_call = kprobe_exceptions_notify,
653 .priority = 0x7fffffff /* we need to be notified first */
654};
655
3d7e3382
ME
656unsigned long __weak arch_deref_entry_point(void *entry)
657{
658 return (unsigned long)entry;
659}
1da177e4 660
d0aaff97 661int __kprobes register_jprobe(struct jprobe *jp)
1da177e4 662{
3d7e3382
ME
663 unsigned long addr = arch_deref_entry_point(jp->entry);
664
665 if (!kernel_text_address(addr))
666 return -EINVAL;
667
1da177e4
LT
668 /* Todo: Verify probepoint is a function entry point */
669 jp->kp.pre_handler = setjmp_pre_handler;
670 jp->kp.break_handler = longjmp_break_handler;
671
df019b1d
KA
672 return __register_kprobe(&jp->kp,
673 (unsigned long)__builtin_return_address(0));
1da177e4
LT
674}
675
d0aaff97 676void __kprobes unregister_jprobe(struct jprobe *jp)
1da177e4
LT
677{
678 unregister_kprobe(&jp->kp);
679}
680
9edddaa2 681#ifdef CONFIG_KRETPROBES
e65cefe8
AB
682/*
683 * This kprobe pre_handler is registered with every kretprobe. When probe
684 * hits it will set up the return probe.
685 */
686static int __kprobes pre_handler_kretprobe(struct kprobe *p,
687 struct pt_regs *regs)
688{
689 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
690 unsigned long flags = 0;
691
692 /*TODO: consider to only swap the RA after the last pre_handler fired */
693 spin_lock_irqsave(&kretprobe_lock, flags);
4c4308cb
CH
694 if (!hlist_empty(&rp->free_instances)) {
695 struct kretprobe_instance *ri;
696
697 ri = hlist_entry(rp->free_instances.first,
698 struct kretprobe_instance, uflist);
699 ri->rp = rp;
700 ri->task = current;
f47cd9b5
AS
701
702 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
703 spin_unlock_irqrestore(&kretprobe_lock, flags);
704 return 0;
705 }
706
4c4308cb
CH
707 arch_prepare_kretprobe(ri, regs);
708
709 /* XXX(hch): why is there no hlist_move_head? */
710 hlist_del(&ri->uflist);
711 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
712 hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
713 } else
714 rp->nmissed++;
e65cefe8
AB
715 spin_unlock_irqrestore(&kretprobe_lock, flags);
716 return 0;
717}
718
d0aaff97 719int __kprobes register_kretprobe(struct kretprobe *rp)
b94cce92
HN
720{
721 int ret = 0;
722 struct kretprobe_instance *inst;
723 int i;
f438d914
MH
724 void *addr = rp->kp.addr;
725
726 if (kretprobe_blacklist_size) {
727 if (addr == NULL)
728 kprobe_lookup_name(rp->kp.symbol_name, addr);
729 addr += rp->kp.offset;
730
731 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
732 if (kretprobe_blacklist[i].addr == addr)
733 return -EINVAL;
734 }
735 }
b94cce92
HN
736
737 rp->kp.pre_handler = pre_handler_kretprobe;
7522a842
AM
738 rp->kp.post_handler = NULL;
739 rp->kp.fault_handler = NULL;
740 rp->kp.break_handler = NULL;
b94cce92
HN
741
742 /* Pre-allocate memory for max kretprobe instances */
743 if (rp->maxactive <= 0) {
744#ifdef CONFIG_PREEMPT
745 rp->maxactive = max(10, 2 * NR_CPUS);
746#else
747 rp->maxactive = NR_CPUS;
748#endif
749 }
750 INIT_HLIST_HEAD(&rp->used_instances);
751 INIT_HLIST_HEAD(&rp->free_instances);
752 for (i = 0; i < rp->maxactive; i++) {
f47cd9b5
AS
753 inst = kmalloc(sizeof(struct kretprobe_instance) +
754 rp->data_size, GFP_KERNEL);
b94cce92
HN
755 if (inst == NULL) {
756 free_rp_inst(rp);
757 return -ENOMEM;
758 }
759 INIT_HLIST_NODE(&inst->uflist);
760 hlist_add_head(&inst->uflist, &rp->free_instances);
761 }
762
763 rp->nmissed = 0;
764 /* Establish function entry probe point */
df019b1d
KA
765 if ((ret = __register_kprobe(&rp->kp,
766 (unsigned long)__builtin_return_address(0))) != 0)
b94cce92
HN
767 free_rp_inst(rp);
768 return ret;
769}
770
9edddaa2 771#else /* CONFIG_KRETPROBES */
d0aaff97 772int __kprobes register_kretprobe(struct kretprobe *rp)
b94cce92
HN
773{
774 return -ENOSYS;
775}
776
346fd59b
SD
777static int __kprobes pre_handler_kretprobe(struct kprobe *p,
778 struct pt_regs *regs)
779{
780 return 0;
781}
9edddaa2 782#endif /* CONFIG_KRETPROBES */
b94cce92 783
d0aaff97 784void __kprobes unregister_kretprobe(struct kretprobe *rp)
b94cce92
HN
785{
786 unsigned long flags;
787 struct kretprobe_instance *ri;
4c4308cb 788 struct hlist_node *pos, *next;
b94cce92
HN
789
790 unregister_kprobe(&rp->kp);
4c4308cb 791
b94cce92 792 /* No race here */
3516a460 793 spin_lock_irqsave(&kretprobe_lock, flags);
4c4308cb 794 hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
b94cce92
HN
795 ri->rp = NULL;
796 hlist_del(&ri->uflist);
797 }
3516a460 798 spin_unlock_irqrestore(&kretprobe_lock, flags);
278ff953 799 free_rp_inst(rp);
b94cce92
HN
800}
801
1da177e4
LT
802static int __init init_kprobes(void)
803{
804 int i, err = 0;
805
806 /* FIXME allocate the probe table, currently defined statically */
807 /* initialize all list heads */
b94cce92 808 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1da177e4 809 INIT_HLIST_HEAD(&kprobe_table[i]);
b94cce92
HN
810 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
811 }
1da177e4 812
f438d914
MH
813 if (kretprobe_blacklist_size) {
814 /* lookup the function address from its name */
815 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
816 kprobe_lookup_name(kretprobe_blacklist[i].name,
817 kretprobe_blacklist[i].addr);
818 if (!kretprobe_blacklist[i].addr)
819 printk("kretprobe: lookup failed: %s\n",
820 kretprobe_blacklist[i].name);
821 }
822 }
823
bf8f6e5b
AM
824 /* By default, kprobes are enabled */
825 kprobe_enabled = true;
826
6772926b 827 err = arch_init_kprobes();
802eae7c
RL
828 if (!err)
829 err = register_die_notifier(&kprobe_exceptions_nb);
830
8c1c9356
AM
831 if (!err)
832 init_test_probes();
1da177e4
LT
833 return err;
834}
835
346fd59b
SD
836#ifdef CONFIG_DEBUG_FS
837static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
bf8f6e5b 838 const char *sym, int offset,char *modname)
346fd59b
SD
839{
840 char *kprobe_type;
841
842 if (p->pre_handler == pre_handler_kretprobe)
843 kprobe_type = "r";
844 else if (p->pre_handler == setjmp_pre_handler)
845 kprobe_type = "j";
846 else
847 kprobe_type = "k";
848 if (sym)
849 seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
850 sym, offset, (modname ? modname : " "));
851 else
852 seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
853}
854
855static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
856{
857 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
858}
859
860static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
861{
862 (*pos)++;
863 if (*pos >= KPROBE_TABLE_SIZE)
864 return NULL;
865 return pos;
866}
867
868static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
869{
870 /* Nothing to do */
871}
872
873static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
874{
875 struct hlist_head *head;
876 struct hlist_node *node;
877 struct kprobe *p, *kp;
878 const char *sym = NULL;
879 unsigned int i = *(loff_t *) v;
ffb45122 880 unsigned long offset = 0;
346fd59b
SD
881 char *modname, namebuf[128];
882
883 head = &kprobe_table[i];
884 preempt_disable();
885 hlist_for_each_entry_rcu(p, node, head, hlist) {
ffb45122 886 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
346fd59b
SD
887 &offset, &modname, namebuf);
888 if (p->pre_handler == aggr_pre_handler) {
889 list_for_each_entry_rcu(kp, &p->list, list)
890 report_probe(pi, kp, sym, offset, modname);
891 } else
892 report_probe(pi, p, sym, offset, modname);
893 }
894 preempt_enable();
895 return 0;
896}
897
898static struct seq_operations kprobes_seq_ops = {
899 .start = kprobe_seq_start,
900 .next = kprobe_seq_next,
901 .stop = kprobe_seq_stop,
902 .show = show_kprobe_addr
903};
904
905static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
906{
907 return seq_open(filp, &kprobes_seq_ops);
908}
909
910static struct file_operations debugfs_kprobes_operations = {
911 .open = kprobes_open,
912 .read = seq_read,
913 .llseek = seq_lseek,
914 .release = seq_release,
915};
916
bf8f6e5b
AM
917static void __kprobes enable_all_kprobes(void)
918{
919 struct hlist_head *head;
920 struct hlist_node *node;
921 struct kprobe *p;
922 unsigned int i;
923
924 mutex_lock(&kprobe_mutex);
925
926 /* If kprobes are already enabled, just return */
927 if (kprobe_enabled)
928 goto already_enabled;
929
bf8f6e5b
AM
930 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
931 head = &kprobe_table[i];
932 hlist_for_each_entry_rcu(p, node, head, hlist)
933 arch_arm_kprobe(p);
934 }
935
936 kprobe_enabled = true;
937 printk(KERN_INFO "Kprobes globally enabled\n");
938
939already_enabled:
940 mutex_unlock(&kprobe_mutex);
941 return;
942}
943
944static void __kprobes disable_all_kprobes(void)
945{
946 struct hlist_head *head;
947 struct hlist_node *node;
948 struct kprobe *p;
949 unsigned int i;
950
951 mutex_lock(&kprobe_mutex);
952
953 /* If kprobes are already disabled, just return */
954 if (!kprobe_enabled)
955 goto already_disabled;
956
957 kprobe_enabled = false;
958 printk(KERN_INFO "Kprobes globally disabled\n");
959 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
960 head = &kprobe_table[i];
961 hlist_for_each_entry_rcu(p, node, head, hlist) {
962 if (!arch_trampoline_kprobe(p))
963 arch_disarm_kprobe(p);
964 }
965 }
966
967 mutex_unlock(&kprobe_mutex);
968 /* Allow all currently running kprobes to complete */
969 synchronize_sched();
74a0b576 970 return;
bf8f6e5b
AM
971
972already_disabled:
973 mutex_unlock(&kprobe_mutex);
974 return;
975}
976
977/*
978 * XXX: The debugfs bool file interface doesn't allow for callbacks
979 * when the bool state is switched. We can reuse that facility when
980 * available
981 */
982static ssize_t read_enabled_file_bool(struct file *file,
983 char __user *user_buf, size_t count, loff_t *ppos)
984{
985 char buf[3];
986
987 if (kprobe_enabled)
988 buf[0] = '1';
989 else
990 buf[0] = '0';
991 buf[1] = '\n';
992 buf[2] = 0x00;
993 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
994}
995
996static ssize_t write_enabled_file_bool(struct file *file,
997 const char __user *user_buf, size_t count, loff_t *ppos)
998{
999 char buf[32];
1000 int buf_size;
1001
1002 buf_size = min(count, (sizeof(buf)-1));
1003 if (copy_from_user(buf, user_buf, buf_size))
1004 return -EFAULT;
1005
1006 switch (buf[0]) {
1007 case 'y':
1008 case 'Y':
1009 case '1':
1010 enable_all_kprobes();
1011 break;
1012 case 'n':
1013 case 'N':
1014 case '0':
1015 disable_all_kprobes();
1016 break;
1017 }
1018
1019 return count;
1020}
1021
1022static struct file_operations fops_kp = {
1023 .read = read_enabled_file_bool,
1024 .write = write_enabled_file_bool,
1025};
1026
346fd59b
SD
1027static int __kprobes debugfs_kprobe_init(void)
1028{
1029 struct dentry *dir, *file;
bf8f6e5b 1030 unsigned int value = 1;
346fd59b
SD
1031
1032 dir = debugfs_create_dir("kprobes", NULL);
1033 if (!dir)
1034 return -ENOMEM;
1035
e3869792 1036 file = debugfs_create_file("list", 0444, dir, NULL,
346fd59b
SD
1037 &debugfs_kprobes_operations);
1038 if (!file) {
1039 debugfs_remove(dir);
1040 return -ENOMEM;
1041 }
1042
bf8f6e5b
AM
1043 file = debugfs_create_file("enabled", 0600, dir,
1044 &value, &fops_kp);
1045 if (!file) {
1046 debugfs_remove(dir);
1047 return -ENOMEM;
1048 }
1049
346fd59b
SD
1050 return 0;
1051}
1052
1053late_initcall(debugfs_kprobe_init);
1054#endif /* CONFIG_DEBUG_FS */
1055
1056module_init(init_kprobes);
1da177e4
LT
1057
1058EXPORT_SYMBOL_GPL(register_kprobe);
1059EXPORT_SYMBOL_GPL(unregister_kprobe);
1060EXPORT_SYMBOL_GPL(register_jprobe);
1061EXPORT_SYMBOL_GPL(unregister_jprobe);
cd5bfea2 1062#ifdef CONFIG_KPROBES
1da177e4 1063EXPORT_SYMBOL_GPL(jprobe_return);
cd5bfea2
PC
1064#endif
1065
1066#ifdef CONFIG_KPROBES
b94cce92
HN
1067EXPORT_SYMBOL_GPL(register_kretprobe);
1068EXPORT_SYMBOL_GPL(unregister_kretprobe);
cd5bfea2 1069#endif