kprobes: prevent probing of preempt_schedule()
[pandora-kernel.git] / kernel / kprobes.c
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.
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.
33  */
34 #include <linux/kprobes.h>
35 #include <linux/hash.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/stddef.h>
39 #include <linux/module.h>
40 #include <linux/moduleloader.h>
41 #include <linux/kallsyms.h>
42 #include <linux/freezer.h>
43 #include <linux/seq_file.h>
44 #include <linux/debugfs.h>
45 #include <linux/kdebug.h>
46
47 #include <asm-generic/sections.h>
48 #include <asm/cacheflush.h>
49 #include <asm/errno.h>
50 #include <asm/uaccess.h>
51
52 #define KPROBE_HASH_BITS 6
53 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
54
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
65 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
66 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
67
68 /* NOTE: change this value only with kprobe_mutex held */
69 static bool kprobe_enabled;
70
71 DEFINE_MUTEX(kprobe_mutex);             /* Protects kprobe_table */
72 DEFINE_SPINLOCK(kretprobe_lock);        /* Protects kretprobe_inst_table */
73 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
74
75 /*
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  */
82 struct kprobe_blackpoint kprobe_blacklist[] = {
83         {"preempt_schedule",},
84         {NULL}    /* Terminator */
85 };
86
87 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
88 /*
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
96 struct 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;
101         int ngarbage;
102 };
103
104 enum kprobe_slot_state {
105         SLOT_CLEAN = 0,
106         SLOT_DIRTY = 1,
107         SLOT_USED = 2,
108 };
109
110 static struct hlist_head kprobe_insn_pages;
111 static int kprobe_garbage_slots;
112 static int collect_garbage_slots(void);
113
114 static 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         }
130 loop_end:
131         thaw_processes();
132 #else
133         synchronize_sched();
134 #endif
135         return ret;
136 }
137
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  */
142 kprobe_opcode_t __kprobes *get_insn_slot(void)
143 {
144         struct kprobe_insn_page *kip;
145         struct hlist_node *pos;
146
147  retry:
148         hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
149                 if (kip->nused < INSNS_PER_PAGE) {
150                         int i;
151                         for (i = 0; i < INSNS_PER_PAGE; i++) {
152                                 if (kip->slot_used[i] == SLOT_CLEAN) {
153                                         kip->slot_used[i] = SLOT_USED;
154                                         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
163         /* 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. */
168         kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
169         if (!kip)
170                 return NULL;
171
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);
184         memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
185         kip->slot_used[0] = SLOT_USED;
186         kip->nused = 1;
187         kip->ngarbage = 0;
188         return kip->insns;
189 }
190
191 /* Return 1 if all garbages are collected, otherwise 0. */
192 static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
193 {
194         kip->slot_used[idx] = SLOT_CLEAN;
195         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
217 static 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
226         hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
227                 int i;
228                 if (kip->ngarbage == 0)
229                         continue;
230                 kip->ngarbage = 0;      /* we will collect all garbages */
231                 for (i = 0; i < INSNS_PER_PAGE; i++) {
232                         if (kip->slot_used[i] == SLOT_DIRTY &&
233                             collect_one_slot(kip, i))
234                                 break;
235                 }
236         }
237         kprobe_garbage_slots = 0;
238         return 0;
239 }
240
241 void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
242 {
243         struct kprobe_insn_page *kip;
244         struct hlist_node *pos;
245
246         hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
247                 if (kip->insns <= slot &&
248                     slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
249                         int i = (slot - kip->insns) / MAX_INSN_SIZE;
250                         if (dirty) {
251                                 kip->slot_used[i] = SLOT_DIRTY;
252                                 kip->ngarbage++;
253                         } else {
254                                 collect_one_slot(kip, i);
255                         }
256                         break;
257                 }
258         }
259
260         if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
261                 collect_garbage_slots();
262 }
263 #endif
264
265 /* We have preemption disabled.. so it is safe to use __ versions */
266 static inline void set_kprobe_instance(struct kprobe *kp)
267 {
268         __get_cpu_var(kprobe_instance) = kp;
269 }
270
271 static inline void reset_kprobe_instance(void)
272 {
273         __get_cpu_var(kprobe_instance) = NULL;
274 }
275
276 /*
277  * This routine is called either:
278  *      - under the kprobe_mutex - during kprobe_[un]register()
279  *                              OR
280  *      - with preemption disabled - from arch/xxx/kernel/kprobes.c
281  */
282 struct kprobe __kprobes *get_kprobe(void *addr)
283 {
284         struct hlist_head *head;
285         struct hlist_node *node;
286         struct kprobe *p;
287
288         head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
289         hlist_for_each_entry_rcu(p, node, head, hlist) {
290                 if (p->addr == addr)
291                         return p;
292         }
293         return NULL;
294 }
295
296 /*
297  * Aggregate handlers for multiple kprobes support - these handlers
298  * take care of invoking the individual kprobe handlers on p->list
299  */
300 static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
301 {
302         struct kprobe *kp;
303
304         list_for_each_entry_rcu(kp, &p->list, list) {
305                 if (kp->pre_handler) {
306                         set_kprobe_instance(kp);
307                         if (kp->pre_handler(kp, regs))
308                                 return 1;
309                 }
310                 reset_kprobe_instance();
311         }
312         return 0;
313 }
314
315 static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
316                                         unsigned long flags)
317 {
318         struct kprobe *kp;
319
320         list_for_each_entry_rcu(kp, &p->list, list) {
321                 if (kp->post_handler) {
322                         set_kprobe_instance(kp);
323                         kp->post_handler(kp, regs, flags);
324                         reset_kprobe_instance();
325                 }
326         }
327 }
328
329 static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
330                                         int trapnr)
331 {
332         struct kprobe *cur = __get_cpu_var(kprobe_instance);
333
334         /*
335          * if we faulted "during" the execution of a user specified
336          * probe handler, invoke just that probe's fault handler
337          */
338         if (cur && cur->fault_handler) {
339                 if (cur->fault_handler(cur, regs, trapnr))
340                         return 1;
341         }
342         return 0;
343 }
344
345 static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
346 {
347         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;
353         }
354         reset_kprobe_instance();
355         return ret;
356 }
357
358 /* Walks the list and increments nmissed count for multiprobe case */
359 void __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
371 /* Called with kretprobe_lock held */
372 void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
373                                 struct hlist_head *head)
374 {
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 */
385                 hlist_add_head(&ri->hlist, head);
386 }
387
388 struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
389 {
390         return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
391 }
392
393 /*
394  * 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.
398  */
399 void __kprobes kprobe_flush_task(struct task_struct *tk)
400 {
401         struct kretprobe_instance *ri;
402         struct hlist_head *head, empty_rp;
403         struct hlist_node *node, *tmp;
404         unsigned long flags = 0;
405
406         INIT_HLIST_HEAD(&empty_rp);
407         spin_lock_irqsave(&kretprobe_lock, flags);
408         head = kretprobe_inst_table_head(tk);
409         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
410                 if (ri->task == tk)
411                         recycle_rp_inst(ri, &empty_rp);
412         }
413         spin_unlock_irqrestore(&kretprobe_lock, flags);
414
415         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
416                 hlist_del(&ri->hlist);
417                 kfree(ri);
418         }
419 }
420
421 static inline void free_rp_inst(struct kretprobe *rp)
422 {
423         struct kretprobe_instance *ri;
424         struct hlist_node *pos, *next;
425
426         hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
427                 hlist_del(&ri->uflist);
428                 kfree(ri);
429         }
430 }
431
432 /*
433  * Keep all fields in the kprobe consistent
434  */
435 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
436 {
437         memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
438         memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
439 }
440
441 /*
442 * Add the new probe to old_p->list. Fail if this is the
443 * second jprobe at the address - two jprobes can't coexist
444 */
445 static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
446 {
447         if (p->break_handler) {
448                 if (old_p->break_handler)
449                         return -EEXIST;
450                 list_add_tail_rcu(&p->list, &old_p->list);
451                 old_p->break_handler = aggr_break_handler;
452         } else
453                 list_add_rcu(&p->list, &old_p->list);
454         if (p->post_handler && !old_p->post_handler)
455                 old_p->post_handler = aggr_post_handler;
456         return 0;
457 }
458
459 /*
460  * Fill in the required fields of the "manager kprobe". Replace the
461  * earlier kprobe in the hlist with the manager kprobe
462  */
463 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
464 {
465         copy_kprobe(p, ap);
466         flush_insn_slot(ap);
467         ap->addr = p->addr;
468         ap->pre_handler = aggr_pre_handler;
469         ap->fault_handler = aggr_fault_handler;
470         if (p->post_handler)
471                 ap->post_handler = aggr_post_handler;
472         if (p->break_handler)
473                 ap->break_handler = aggr_break_handler;
474
475         INIT_LIST_HEAD(&ap->list);
476         list_add_rcu(&p->list, &ap->list);
477
478         hlist_replace_rcu(&p->hlist, &ap->hlist);
479 }
480
481 /*
482  * This is the second or subsequent kprobe at the address - handle
483  * the intricacies
484  */
485 static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
486                                           struct kprobe *p)
487 {
488         int ret = 0;
489         struct kprobe *ap;
490
491         if (old_p->pre_handler == aggr_pre_handler) {
492                 copy_kprobe(old_p, p);
493                 ret = add_new_kprobe(old_p, p);
494         } else {
495                 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
496                 if (!ap)
497                         return -ENOMEM;
498                 add_aggr_kprobe(ap, old_p);
499                 copy_kprobe(ap, p);
500                 ret = add_new_kprobe(ap, p);
501         }
502         return ret;
503 }
504
505 static int __kprobes in_kprobes_functions(unsigned long addr)
506 {
507         struct kprobe_blackpoint *kb;
508
509         if (addr >= (unsigned long)__kprobes_text_start &&
510             addr < (unsigned long)__kprobes_text_end)
511                 return -EINVAL;
512         /*
513          * If there exists a kprobe_blacklist, verify and
514          * fail any probe registration in the prohibited area
515          */
516         for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
517                 if (kb->start_addr) {
518                         if (addr >= kb->start_addr &&
519                             addr < (kb->start_addr + kb->range))
520                                 return -EINVAL;
521                 }
522         }
523         return 0;
524 }
525
526 /*
527  * If we have a symbol_name argument, look it up and add the offset field
528  * to it. This way, we can specify a relative address to a symbol.
529  */
530 static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
531 {
532         kprobe_opcode_t *addr = p->addr;
533         if (p->symbol_name) {
534                 if (addr)
535                         return NULL;
536                 kprobe_lookup_name(p->symbol_name, addr);
537         }
538
539         if (!addr)
540                 return NULL;
541         return (kprobe_opcode_t *)(((char *)addr) + p->offset);
542 }
543
544 static int __kprobes __register_kprobe(struct kprobe *p,
545         unsigned long called_from)
546 {
547         int ret = 0;
548         struct kprobe *old_p;
549         struct module *probed_mod;
550         kprobe_opcode_t *addr;
551
552         addr = kprobe_addr(p);
553         if (!addr)
554                 return -EINVAL;
555         p->addr = addr;
556
557         if (!kernel_text_address((unsigned long) p->addr) ||
558             in_kprobes_functions((unsigned long) p->addr))
559                 return -EINVAL;
560
561         p->mod_refcounted = 0;
562
563         /*
564          * Check if are we probing a module.
565          */
566         probed_mod = module_text_address((unsigned long) p->addr);
567         if (probed_mod) {
568                 struct module *calling_mod = module_text_address(called_from);
569                 /*
570                  * We must allow modules to probe themself and in this case
571                  * avoid incrementing the module refcount, so as to allow
572                  * unloading of self probing modules.
573                  */
574                 if (calling_mod && calling_mod != probed_mod) {
575                         if (unlikely(!try_module_get(probed_mod)))
576                                 return -EINVAL;
577                         p->mod_refcounted = 1;
578                 } else
579                         probed_mod = NULL;
580         }
581
582         p->nmissed = 0;
583         mutex_lock(&kprobe_mutex);
584         old_p = get_kprobe(p->addr);
585         if (old_p) {
586                 ret = register_aggr_kprobe(old_p, p);
587                 goto out;
588         }
589
590         ret = arch_prepare_kprobe(p);
591         if (ret)
592                 goto out;
593
594         INIT_HLIST_NODE(&p->hlist);
595         hlist_add_head_rcu(&p->hlist,
596                        &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
597
598         if (kprobe_enabled)
599                 arch_arm_kprobe(p);
600
601 out:
602         mutex_unlock(&kprobe_mutex);
603
604         if (ret && probed_mod)
605                 module_put(probed_mod);
606         return ret;
607 }
608
609 int __kprobes register_kprobe(struct kprobe *p)
610 {
611         return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
612 }
613
614 void __kprobes unregister_kprobe(struct kprobe *p)
615 {
616         struct module *mod;
617         struct kprobe *old_p, *list_p;
618         int cleanup_p;
619
620         mutex_lock(&kprobe_mutex);
621         old_p = get_kprobe(p->addr);
622         if (unlikely(!old_p)) {
623                 mutex_unlock(&kprobe_mutex);
624                 return;
625         }
626         if (p != old_p) {
627                 list_for_each_entry_rcu(list_p, &old_p->list, list)
628                         if (list_p == p)
629                         /* kprobe p is a valid probe */
630                                 goto valid_p;
631                 mutex_unlock(&kprobe_mutex);
632                 return;
633         }
634 valid_p:
635         if (old_p == p ||
636             (old_p->pre_handler == aggr_pre_handler &&
637              p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
638                 /*
639                  * Only probe on the hash list. Disarm only if kprobes are
640                  * enabled - otherwise, the breakpoint would already have
641                  * been removed. We save on flushing icache.
642                  */
643                 if (kprobe_enabled)
644                         arch_disarm_kprobe(p);
645                 hlist_del_rcu(&old_p->hlist);
646                 cleanup_p = 1;
647         } else {
648                 list_del_rcu(&p->list);
649                 cleanup_p = 0;
650         }
651
652         mutex_unlock(&kprobe_mutex);
653
654         synchronize_sched();
655         if (p->mod_refcounted) {
656                 mod = module_text_address((unsigned long)p->addr);
657                 if (mod)
658                         module_put(mod);
659         }
660
661         if (cleanup_p) {
662                 if (p != old_p) {
663                         list_del_rcu(&p->list);
664                         kfree(old_p);
665                 }
666                 arch_remove_kprobe(p);
667         } else {
668                 mutex_lock(&kprobe_mutex);
669                 if (p->break_handler)
670                         old_p->break_handler = NULL;
671                 if (p->post_handler){
672                         list_for_each_entry_rcu(list_p, &old_p->list, list){
673                                 if (list_p->post_handler){
674                                         cleanup_p = 2;
675                                         break;
676                                 }
677                         }
678                         if (cleanup_p == 0)
679                                 old_p->post_handler = NULL;
680                 }
681                 mutex_unlock(&kprobe_mutex);
682         }
683 }
684
685 static struct notifier_block kprobe_exceptions_nb = {
686         .notifier_call = kprobe_exceptions_notify,
687         .priority = 0x7fffffff /* we need to be notified first */
688 };
689
690 unsigned long __weak arch_deref_entry_point(void *entry)
691 {
692         return (unsigned long)entry;
693 }
694
695 int __kprobes register_jprobe(struct jprobe *jp)
696 {
697         unsigned long addr = arch_deref_entry_point(jp->entry);
698
699         if (!kernel_text_address(addr))
700                 return -EINVAL;
701
702         /* Todo: Verify probepoint is a function entry point */
703         jp->kp.pre_handler = setjmp_pre_handler;
704         jp->kp.break_handler = longjmp_break_handler;
705
706         return __register_kprobe(&jp->kp,
707                 (unsigned long)__builtin_return_address(0));
708 }
709
710 void __kprobes unregister_jprobe(struct jprobe *jp)
711 {
712         unregister_kprobe(&jp->kp);
713 }
714
715 #ifdef CONFIG_KRETPROBES
716 /*
717  * This kprobe pre_handler is registered with every kretprobe. When probe
718  * hits it will set up the return probe.
719  */
720 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
721                                            struct pt_regs *regs)
722 {
723         struct kretprobe *rp = container_of(p, struct kretprobe, kp);
724         unsigned long flags = 0;
725
726         /*TODO: consider to only swap the RA after the last pre_handler fired */
727         spin_lock_irqsave(&kretprobe_lock, flags);
728         if (!hlist_empty(&rp->free_instances)) {
729                 struct kretprobe_instance *ri;
730
731                 ri = hlist_entry(rp->free_instances.first,
732                                  struct kretprobe_instance, uflist);
733                 ri->rp = rp;
734                 ri->task = current;
735
736                 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
737                         spin_unlock_irqrestore(&kretprobe_lock, flags);
738                         return 0;
739                 }
740
741                 arch_prepare_kretprobe(ri, regs);
742
743                 /* XXX(hch): why is there no hlist_move_head? */
744                 hlist_del(&ri->uflist);
745                 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
746                 hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
747         } else
748                 rp->nmissed++;
749         spin_unlock_irqrestore(&kretprobe_lock, flags);
750         return 0;
751 }
752
753 int __kprobes register_kretprobe(struct kretprobe *rp)
754 {
755         int ret = 0;
756         struct kretprobe_instance *inst;
757         int i;
758         void *addr;
759
760         if (kretprobe_blacklist_size) {
761                 addr = kprobe_addr(&rp->kp);
762                 if (!addr)
763                         return -EINVAL;
764
765                 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
766                         if (kretprobe_blacklist[i].addr == addr)
767                                 return -EINVAL;
768                 }
769         }
770
771         rp->kp.pre_handler = pre_handler_kretprobe;
772         rp->kp.post_handler = NULL;
773         rp->kp.fault_handler = NULL;
774         rp->kp.break_handler = NULL;
775
776         /* Pre-allocate memory for max kretprobe instances */
777         if (rp->maxactive <= 0) {
778 #ifdef CONFIG_PREEMPT
779                 rp->maxactive = max(10, 2 * NR_CPUS);
780 #else
781                 rp->maxactive = NR_CPUS;
782 #endif
783         }
784         INIT_HLIST_HEAD(&rp->used_instances);
785         INIT_HLIST_HEAD(&rp->free_instances);
786         for (i = 0; i < rp->maxactive; i++) {
787                 inst = kmalloc(sizeof(struct kretprobe_instance) +
788                                rp->data_size, GFP_KERNEL);
789                 if (inst == NULL) {
790                         free_rp_inst(rp);
791                         return -ENOMEM;
792                 }
793                 INIT_HLIST_NODE(&inst->uflist);
794                 hlist_add_head(&inst->uflist, &rp->free_instances);
795         }
796
797         rp->nmissed = 0;
798         /* Establish function entry probe point */
799         if ((ret = __register_kprobe(&rp->kp,
800                 (unsigned long)__builtin_return_address(0))) != 0)
801                 free_rp_inst(rp);
802         return ret;
803 }
804
805 #else /* CONFIG_KRETPROBES */
806 int __kprobes register_kretprobe(struct kretprobe *rp)
807 {
808         return -ENOSYS;
809 }
810
811 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
812                                            struct pt_regs *regs)
813 {
814         return 0;
815 }
816 #endif /* CONFIG_KRETPROBES */
817
818 void __kprobes unregister_kretprobe(struct kretprobe *rp)
819 {
820         unsigned long flags;
821         struct kretprobe_instance *ri;
822         struct hlist_node *pos, *next;
823
824         unregister_kprobe(&rp->kp);
825
826         /* No race here */
827         spin_lock_irqsave(&kretprobe_lock, flags);
828         hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
829                 ri->rp = NULL;
830                 hlist_del(&ri->uflist);
831         }
832         spin_unlock_irqrestore(&kretprobe_lock, flags);
833         free_rp_inst(rp);
834 }
835
836 static int __init init_kprobes(void)
837 {
838         int i, err = 0;
839         unsigned long offset = 0, size = 0;
840         char *modname, namebuf[128];
841         const char *symbol_name;
842         void *addr;
843         struct kprobe_blackpoint *kb;
844
845         /* FIXME allocate the probe table, currently defined statically */
846         /* initialize all list heads */
847         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
848                 INIT_HLIST_HEAD(&kprobe_table[i]);
849                 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
850         }
851
852         /*
853          * Lookup and populate the kprobe_blacklist.
854          *
855          * Unlike the kretprobe blacklist, we'll need to determine
856          * the range of addresses that belong to the said functions,
857          * since a kprobe need not necessarily be at the beginning
858          * of a function.
859          */
860         for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
861                 kprobe_lookup_name(kb->name, addr);
862                 if (!addr)
863                         continue;
864
865                 kb->start_addr = (unsigned long)addr;
866                 symbol_name = kallsyms_lookup(kb->start_addr,
867                                 &size, &offset, &modname, namebuf);
868                 if (!symbol_name)
869                         kb->range = 0;
870                 else
871                         kb->range = size;
872         }
873
874         if (kretprobe_blacklist_size) {
875                 /* lookup the function address from its name */
876                 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
877                         kprobe_lookup_name(kretprobe_blacklist[i].name,
878                                            kretprobe_blacklist[i].addr);
879                         if (!kretprobe_blacklist[i].addr)
880                                 printk("kretprobe: lookup failed: %s\n",
881                                        kretprobe_blacklist[i].name);
882                 }
883         }
884
885         /* By default, kprobes are enabled */
886         kprobe_enabled = true;
887
888         err = arch_init_kprobes();
889         if (!err)
890                 err = register_die_notifier(&kprobe_exceptions_nb);
891
892         if (!err)
893                 init_test_probes();
894         return err;
895 }
896
897 #ifdef CONFIG_DEBUG_FS
898 static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
899                 const char *sym, int offset,char *modname)
900 {
901         char *kprobe_type;
902
903         if (p->pre_handler == pre_handler_kretprobe)
904                 kprobe_type = "r";
905         else if (p->pre_handler == setjmp_pre_handler)
906                 kprobe_type = "j";
907         else
908                 kprobe_type = "k";
909         if (sym)
910                 seq_printf(pi, "%p  %s  %s+0x%x  %s\n", p->addr, kprobe_type,
911                         sym, offset, (modname ? modname : " "));
912         else
913                 seq_printf(pi, "%p  %s  %p\n", p->addr, kprobe_type, p->addr);
914 }
915
916 static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
917 {
918         return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
919 }
920
921 static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
922 {
923         (*pos)++;
924         if (*pos >= KPROBE_TABLE_SIZE)
925                 return NULL;
926         return pos;
927 }
928
929 static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
930 {
931         /* Nothing to do */
932 }
933
934 static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
935 {
936         struct hlist_head *head;
937         struct hlist_node *node;
938         struct kprobe *p, *kp;
939         const char *sym = NULL;
940         unsigned int i = *(loff_t *) v;
941         unsigned long offset = 0;
942         char *modname, namebuf[128];
943
944         head = &kprobe_table[i];
945         preempt_disable();
946         hlist_for_each_entry_rcu(p, node, head, hlist) {
947                 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
948                                         &offset, &modname, namebuf);
949                 if (p->pre_handler == aggr_pre_handler) {
950                         list_for_each_entry_rcu(kp, &p->list, list)
951                                 report_probe(pi, kp, sym, offset, modname);
952                 } else
953                         report_probe(pi, p, sym, offset, modname);
954         }
955         preempt_enable();
956         return 0;
957 }
958
959 static struct seq_operations kprobes_seq_ops = {
960         .start = kprobe_seq_start,
961         .next  = kprobe_seq_next,
962         .stop  = kprobe_seq_stop,
963         .show  = show_kprobe_addr
964 };
965
966 static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
967 {
968         return seq_open(filp, &kprobes_seq_ops);
969 }
970
971 static struct file_operations debugfs_kprobes_operations = {
972         .open           = kprobes_open,
973         .read           = seq_read,
974         .llseek         = seq_lseek,
975         .release        = seq_release,
976 };
977
978 static void __kprobes enable_all_kprobes(void)
979 {
980         struct hlist_head *head;
981         struct hlist_node *node;
982         struct kprobe *p;
983         unsigned int i;
984
985         mutex_lock(&kprobe_mutex);
986
987         /* If kprobes are already enabled, just return */
988         if (kprobe_enabled)
989                 goto already_enabled;
990
991         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
992                 head = &kprobe_table[i];
993                 hlist_for_each_entry_rcu(p, node, head, hlist)
994                         arch_arm_kprobe(p);
995         }
996
997         kprobe_enabled = true;
998         printk(KERN_INFO "Kprobes globally enabled\n");
999
1000 already_enabled:
1001         mutex_unlock(&kprobe_mutex);
1002         return;
1003 }
1004
1005 static void __kprobes disable_all_kprobes(void)
1006 {
1007         struct hlist_head *head;
1008         struct hlist_node *node;
1009         struct kprobe *p;
1010         unsigned int i;
1011
1012         mutex_lock(&kprobe_mutex);
1013
1014         /* If kprobes are already disabled, just return */
1015         if (!kprobe_enabled)
1016                 goto already_disabled;
1017
1018         kprobe_enabled = false;
1019         printk(KERN_INFO "Kprobes globally disabled\n");
1020         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1021                 head = &kprobe_table[i];
1022                 hlist_for_each_entry_rcu(p, node, head, hlist) {
1023                         if (!arch_trampoline_kprobe(p))
1024                                 arch_disarm_kprobe(p);
1025                 }
1026         }
1027
1028         mutex_unlock(&kprobe_mutex);
1029         /* Allow all currently running kprobes to complete */
1030         synchronize_sched();
1031         return;
1032
1033 already_disabled:
1034         mutex_unlock(&kprobe_mutex);
1035         return;
1036 }
1037
1038 /*
1039  * XXX: The debugfs bool file interface doesn't allow for callbacks
1040  * when the bool state is switched. We can reuse that facility when
1041  * available
1042  */
1043 static ssize_t read_enabled_file_bool(struct file *file,
1044                char __user *user_buf, size_t count, loff_t *ppos)
1045 {
1046         char buf[3];
1047
1048         if (kprobe_enabled)
1049                 buf[0] = '1';
1050         else
1051                 buf[0] = '0';
1052         buf[1] = '\n';
1053         buf[2] = 0x00;
1054         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1055 }
1056
1057 static ssize_t write_enabled_file_bool(struct file *file,
1058                const char __user *user_buf, size_t count, loff_t *ppos)
1059 {
1060         char buf[32];
1061         int buf_size;
1062
1063         buf_size = min(count, (sizeof(buf)-1));
1064         if (copy_from_user(buf, user_buf, buf_size))
1065                 return -EFAULT;
1066
1067         switch (buf[0]) {
1068         case 'y':
1069         case 'Y':
1070         case '1':
1071                 enable_all_kprobes();
1072                 break;
1073         case 'n':
1074         case 'N':
1075         case '0':
1076                 disable_all_kprobes();
1077                 break;
1078         }
1079
1080         return count;
1081 }
1082
1083 static struct file_operations fops_kp = {
1084         .read =         read_enabled_file_bool,
1085         .write =        write_enabled_file_bool,
1086 };
1087
1088 static int __kprobes debugfs_kprobe_init(void)
1089 {
1090         struct dentry *dir, *file;
1091         unsigned int value = 1;
1092
1093         dir = debugfs_create_dir("kprobes", NULL);
1094         if (!dir)
1095                 return -ENOMEM;
1096
1097         file = debugfs_create_file("list", 0444, dir, NULL,
1098                                 &debugfs_kprobes_operations);
1099         if (!file) {
1100                 debugfs_remove(dir);
1101                 return -ENOMEM;
1102         }
1103
1104         file = debugfs_create_file("enabled", 0600, dir,
1105                                         &value, &fops_kp);
1106         if (!file) {
1107                 debugfs_remove(dir);
1108                 return -ENOMEM;
1109         }
1110
1111         return 0;
1112 }
1113
1114 late_initcall(debugfs_kprobe_init);
1115 #endif /* CONFIG_DEBUG_FS */
1116
1117 module_init(init_kprobes);
1118
1119 EXPORT_SYMBOL_GPL(register_kprobe);
1120 EXPORT_SYMBOL_GPL(unregister_kprobe);
1121 EXPORT_SYMBOL_GPL(register_jprobe);
1122 EXPORT_SYMBOL_GPL(unregister_jprobe);
1123 #ifdef CONFIG_KPROBES
1124 EXPORT_SYMBOL_GPL(jprobe_return);
1125 #endif
1126
1127 #ifdef CONFIG_KPROBES
1128 EXPORT_SYMBOL_GPL(register_kretprobe);
1129 EXPORT_SYMBOL_GPL(unregister_kretprobe);
1130 #endif