| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 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. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ | 
|  | 34 | #include <linux/kprobes.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/hash.h> | 
|  | 36 | #include <linux/init.h> | 
| Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 37 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/module.h> | 
| Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 39 | #include <linux/moduleloader.h> | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 40 | #include <asm-generic/sections.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/cacheflush.h> | 
|  | 42 | #include <asm/errno.h> | 
|  | 43 | #include <asm/kdebug.h> | 
|  | 44 |  | 
|  | 45 | #define KPROBE_HASH_BITS 6 | 
|  | 46 | #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) | 
|  | 47 |  | 
|  | 48 | static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 49 | static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 51 | static DEFINE_SPINLOCK(kprobe_lock);	/* Protects kprobe_table */ | 
|  | 52 | DEFINE_SPINLOCK(kretprobe_lock);	/* Protects kretprobe_inst_table */ | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 53 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 |  | 
| Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 55 | /* | 
|  | 56 | * kprobe->ainsn.insn points to the copy of the instruction to be | 
|  | 57 | * single-stepped. x86_64, POWER4 and above have no-exec support and | 
|  | 58 | * stepping on the instruction on a vmalloced/kmalloced/data page | 
|  | 59 | * is a recipe for disaster | 
|  | 60 | */ | 
|  | 61 | #define INSNS_PER_PAGE	(PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t))) | 
|  | 62 |  | 
|  | 63 | struct kprobe_insn_page { | 
|  | 64 | struct hlist_node hlist; | 
|  | 65 | kprobe_opcode_t *insns;		/* Page of instruction slots */ | 
|  | 66 | char slot_used[INSNS_PER_PAGE]; | 
|  | 67 | int nused; | 
|  | 68 | }; | 
|  | 69 |  | 
|  | 70 | static struct hlist_head kprobe_insn_pages; | 
|  | 71 |  | 
|  | 72 | /** | 
|  | 73 | * get_insn_slot() - Find a slot on an executable page for an instruction. | 
|  | 74 | * We allocate an executable page if there's no room on existing ones. | 
|  | 75 | */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 76 | kprobe_opcode_t __kprobes *get_insn_slot(void) | 
| Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 77 | { | 
|  | 78 | struct kprobe_insn_page *kip; | 
|  | 79 | struct hlist_node *pos; | 
|  | 80 |  | 
|  | 81 | hlist_for_each(pos, &kprobe_insn_pages) { | 
|  | 82 | kip = hlist_entry(pos, struct kprobe_insn_page, hlist); | 
|  | 83 | if (kip->nused < INSNS_PER_PAGE) { | 
|  | 84 | int i; | 
|  | 85 | for (i = 0; i < INSNS_PER_PAGE; i++) { | 
|  | 86 | if (!kip->slot_used[i]) { | 
|  | 87 | kip->slot_used[i] = 1; | 
|  | 88 | kip->nused++; | 
|  | 89 | return kip->insns + (i * MAX_INSN_SIZE); | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 | /* Surprise!  No unused slots.  Fix kip->nused. */ | 
|  | 93 | kip->nused = INSNS_PER_PAGE; | 
|  | 94 | } | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | /* All out of space.  Need to allocate a new page. Use slot 0.*/ | 
|  | 98 | kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL); | 
|  | 99 | if (!kip) { | 
|  | 100 | return NULL; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | /* | 
|  | 104 | * Use module_alloc so this page is within +/- 2GB of where the | 
|  | 105 | * kernel image and loaded module images reside. This is required | 
|  | 106 | * so x86_64 can correctly handle the %rip-relative fixups. | 
|  | 107 | */ | 
|  | 108 | kip->insns = module_alloc(PAGE_SIZE); | 
|  | 109 | if (!kip->insns) { | 
|  | 110 | kfree(kip); | 
|  | 111 | return NULL; | 
|  | 112 | } | 
|  | 113 | INIT_HLIST_NODE(&kip->hlist); | 
|  | 114 | hlist_add_head(&kip->hlist, &kprobe_insn_pages); | 
|  | 115 | memset(kip->slot_used, 0, INSNS_PER_PAGE); | 
|  | 116 | kip->slot_used[0] = 1; | 
|  | 117 | kip->nused = 1; | 
|  | 118 | return kip->insns; | 
|  | 119 | } | 
|  | 120 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 121 | void __kprobes free_insn_slot(kprobe_opcode_t *slot) | 
| Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 122 | { | 
|  | 123 | struct kprobe_insn_page *kip; | 
|  | 124 | struct hlist_node *pos; | 
|  | 125 |  | 
|  | 126 | hlist_for_each(pos, &kprobe_insn_pages) { | 
|  | 127 | kip = hlist_entry(pos, struct kprobe_insn_page, hlist); | 
|  | 128 | if (kip->insns <= slot && | 
|  | 129 | slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) { | 
|  | 130 | int i = (slot - kip->insns) / MAX_INSN_SIZE; | 
|  | 131 | kip->slot_used[i] = 0; | 
|  | 132 | kip->nused--; | 
|  | 133 | if (kip->nused == 0) { | 
|  | 134 | /* | 
|  | 135 | * Page is no longer in use.  Free it unless | 
|  | 136 | * it's the last one.  We keep the last one | 
|  | 137 | * so as not to have to set it up again the | 
|  | 138 | * next time somebody inserts a probe. | 
|  | 139 | */ | 
|  | 140 | hlist_del(&kip->hlist); | 
|  | 141 | if (hlist_empty(&kprobe_insn_pages)) { | 
|  | 142 | INIT_HLIST_NODE(&kip->hlist); | 
|  | 143 | hlist_add_head(&kip->hlist, | 
|  | 144 | &kprobe_insn_pages); | 
|  | 145 | } else { | 
|  | 146 | module_free(NULL, kip->insns); | 
|  | 147 | kfree(kip); | 
|  | 148 | } | 
|  | 149 | } | 
|  | 150 | return; | 
|  | 151 | } | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 |  | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 155 | /* We have preemption disabled.. so it is safe to use __ versions */ | 
|  | 156 | static inline void set_kprobe_instance(struct kprobe *kp) | 
|  | 157 | { | 
|  | 158 | __get_cpu_var(kprobe_instance) = kp; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | static inline void reset_kprobe_instance(void) | 
|  | 162 | { | 
|  | 163 | __get_cpu_var(kprobe_instance) = NULL; | 
|  | 164 | } | 
|  | 165 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 166 | /* | 
|  | 167 | * This routine is called either: | 
|  | 168 | * 	- under the kprobe_lock spinlock - during kprobe_[un]register() | 
|  | 169 | * 				OR | 
| Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 170 | * 	- with preemption disabled - from arch/xxx/kernel/kprobes.c | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 171 | */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 172 | struct kprobe __kprobes *get_kprobe(void *addr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { | 
|  | 174 | struct hlist_head *head; | 
|  | 175 | struct hlist_node *node; | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 176 | struct kprobe *p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 179 | hlist_for_each_entry_rcu(p, node, head, hlist) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (p->addr == addr) | 
|  | 181 | return p; | 
|  | 182 | } | 
|  | 183 | return NULL; | 
|  | 184 | } | 
|  | 185 |  | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 186 | /* | 
|  | 187 | * Aggregate handlers for multiple kprobes support - these handlers | 
|  | 188 | * take care of invoking the individual kprobe handlers on p->list | 
|  | 189 | */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 190 | static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs) | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 191 | { | 
|  | 192 | struct kprobe *kp; | 
|  | 193 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 194 | list_for_each_entry_rcu(kp, &p->list, list) { | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 195 | if (kp->pre_handler) { | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 196 | set_kprobe_instance(kp); | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 197 | if (kp->pre_handler(kp, regs)) | 
|  | 198 | return 1; | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 199 | } | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 200 | reset_kprobe_instance(); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 201 | } | 
|  | 202 | return 0; | 
|  | 203 | } | 
|  | 204 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 205 | static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs, | 
|  | 206 | unsigned long flags) | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 207 | { | 
|  | 208 | struct kprobe *kp; | 
|  | 209 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 210 | list_for_each_entry_rcu(kp, &p->list, list) { | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 211 | if (kp->post_handler) { | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 212 | set_kprobe_instance(kp); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 213 | kp->post_handler(kp, regs, flags); | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 214 | reset_kprobe_instance(); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 215 | } | 
|  | 216 | } | 
|  | 217 | return; | 
|  | 218 | } | 
|  | 219 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 220 | static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, | 
|  | 221 | int trapnr) | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 222 | { | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 223 | struct kprobe *cur = __get_cpu_var(kprobe_instance); | 
|  | 224 |  | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 225 | /* | 
|  | 226 | * if we faulted "during" the execution of a user specified | 
|  | 227 | * probe handler, invoke just that probe's fault handler | 
|  | 228 | */ | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 229 | if (cur && cur->fault_handler) { | 
|  | 230 | if (cur->fault_handler(cur, regs, trapnr)) | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 231 | return 1; | 
|  | 232 | } | 
|  | 233 | return 0; | 
|  | 234 | } | 
|  | 235 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 236 | static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs) | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 237 | { | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 238 | struct kprobe *cur = __get_cpu_var(kprobe_instance); | 
|  | 239 | int ret = 0; | 
|  | 240 |  | 
|  | 241 | if (cur && cur->break_handler) { | 
|  | 242 | if (cur->break_handler(cur, regs)) | 
|  | 243 | ret = 1; | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 244 | } | 
| Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 245 | reset_kprobe_instance(); | 
|  | 246 | return ret; | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 247 | } | 
|  | 248 |  | 
| Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 249 | /* Walks the list and increments nmissed count for multiprobe case */ | 
|  | 250 | void __kprobes kprobes_inc_nmissed_count(struct kprobe *p) | 
|  | 251 | { | 
|  | 252 | struct kprobe *kp; | 
|  | 253 | if (p->pre_handler != aggr_pre_handler) { | 
|  | 254 | p->nmissed++; | 
|  | 255 | } else { | 
|  | 256 | list_for_each_entry_rcu(kp, &p->list, list) | 
|  | 257 | kp->nmissed++; | 
|  | 258 | } | 
|  | 259 | return; | 
|  | 260 | } | 
|  | 261 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 262 | /* Called with kretprobe_lock held */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 263 | struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 264 | { | 
|  | 265 | struct hlist_node *node; | 
|  | 266 | struct kretprobe_instance *ri; | 
|  | 267 | hlist_for_each_entry(ri, node, &rp->free_instances, uflist) | 
|  | 268 | return ri; | 
|  | 269 | return NULL; | 
|  | 270 | } | 
|  | 271 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 272 | /* Called with kretprobe_lock held */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 273 | static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe | 
|  | 274 | *rp) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 275 | { | 
|  | 276 | struct hlist_node *node; | 
|  | 277 | struct kretprobe_instance *ri; | 
|  | 278 | hlist_for_each_entry(ri, node, &rp->used_instances, uflist) | 
|  | 279 | return ri; | 
|  | 280 | return NULL; | 
|  | 281 | } | 
|  | 282 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 283 | /* Called with kretprobe_lock held */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 284 | void __kprobes add_rp_inst(struct kretprobe_instance *ri) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 285 | { | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 286 | /* | 
|  | 287 | * Remove rp inst off the free list - | 
|  | 288 | * Add it back when probed function returns | 
|  | 289 | */ | 
|  | 290 | hlist_del(&ri->uflist); | 
| Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 291 |  | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 292 | /* Add rp inst onto table */ | 
|  | 293 | INIT_HLIST_NODE(&ri->hlist); | 
|  | 294 | hlist_add_head(&ri->hlist, | 
| Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 295 | &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]); | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 296 |  | 
|  | 297 | /* Also add this rp inst to the used list. */ | 
|  | 298 | INIT_HLIST_NODE(&ri->uflist); | 
|  | 299 | hlist_add_head(&ri->uflist, &ri->rp->used_instances); | 
|  | 300 | } | 
|  | 301 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 302 | /* Called with kretprobe_lock held */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 303 | void __kprobes recycle_rp_inst(struct kretprobe_instance *ri) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 304 | { | 
|  | 305 | /* remove rp inst off the rprobe_inst_table */ | 
|  | 306 | hlist_del(&ri->hlist); | 
|  | 307 | if (ri->rp) { | 
|  | 308 | /* remove rp inst off the used list */ | 
|  | 309 | hlist_del(&ri->uflist); | 
|  | 310 | /* put rp inst back onto the free list */ | 
|  | 311 | INIT_HLIST_NODE(&ri->uflist); | 
|  | 312 | hlist_add_head(&ri->uflist, &ri->rp->free_instances); | 
|  | 313 | } else | 
|  | 314 | /* Unregistering */ | 
|  | 315 | kfree(ri); | 
|  | 316 | } | 
|  | 317 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 318 | struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 319 | { | 
|  | 320 | return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)]; | 
|  | 321 | } | 
|  | 322 |  | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 323 | /* | 
| Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 324 | * This function is called from exit_thread or flush_thread when task tk's | 
|  | 325 | * stack is being recycled so that we can recycle any function-return probe | 
|  | 326 | * instances associated with this task. These left over instances represent | 
|  | 327 | * probed functions that have been called but will never return. | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 328 | */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 329 | void __kprobes kprobe_flush_task(struct task_struct *tk) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 330 | { | 
| Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 331 | struct kretprobe_instance *ri; | 
|  | 332 | struct hlist_head *head; | 
|  | 333 | struct hlist_node *node, *tmp; | 
| Hien Nguyen | 0aa55e4 | 2005-06-23 00:09:26 -0700 | [diff] [blame] | 334 | unsigned long flags = 0; | 
| Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 335 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 336 | spin_lock_irqsave(&kretprobe_lock, flags); | 
| Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 337 | head = kretprobe_inst_table_head(current); | 
|  | 338 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | 
|  | 339 | if (ri->task == tk) | 
|  | 340 | recycle_rp_inst(ri); | 
|  | 341 | } | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 342 | spin_unlock_irqrestore(&kretprobe_lock, flags); | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 343 | } | 
|  | 344 |  | 
|  | 345 | /* | 
|  | 346 | * This kprobe pre_handler is registered with every kretprobe. When probe | 
|  | 347 | * hits it will set up the return probe. | 
|  | 348 | */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 349 | static int __kprobes pre_handler_kretprobe(struct kprobe *p, | 
|  | 350 | struct pt_regs *regs) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 351 | { | 
|  | 352 | struct kretprobe *rp = container_of(p, struct kretprobe, kp); | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 353 | unsigned long flags = 0; | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 354 |  | 
|  | 355 | /*TODO: consider to only swap the RA after the last pre_handler fired */ | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 356 | spin_lock_irqsave(&kretprobe_lock, flags); | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 357 | arch_prepare_kretprobe(rp, regs); | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 358 | spin_unlock_irqrestore(&kretprobe_lock, flags); | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 359 | return 0; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | static inline void free_rp_inst(struct kretprobe *rp) | 
|  | 363 | { | 
|  | 364 | struct kretprobe_instance *ri; | 
|  | 365 | while ((ri = get_free_rp_inst(rp)) != NULL) { | 
|  | 366 | hlist_del(&ri->uflist); | 
|  | 367 | kfree(ri); | 
|  | 368 | } | 
|  | 369 | } | 
|  | 370 |  | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 371 | /* | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 372 | * Keep all fields in the kprobe consistent | 
|  | 373 | */ | 
|  | 374 | static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p) | 
|  | 375 | { | 
|  | 376 | memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t)); | 
|  | 377 | memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn)); | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | /* | 
|  | 381 | * Add the new probe to old_p->list. Fail if this is the | 
|  | 382 | * second jprobe at the address - two jprobes can't coexist | 
|  | 383 | */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 384 | static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p) | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 385 | { | 
|  | 386 | struct kprobe *kp; | 
|  | 387 |  | 
|  | 388 | if (p->break_handler) { | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 389 | list_for_each_entry_rcu(kp, &old_p->list, list) { | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 390 | if (kp->break_handler) | 
|  | 391 | return -EEXIST; | 
|  | 392 | } | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 393 | list_add_tail_rcu(&p->list, &old_p->list); | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 394 | } else | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 395 | list_add_rcu(&p->list, &old_p->list); | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 396 | return 0; | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | /* | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 400 | * Fill in the required fields of the "manager kprobe". Replace the | 
|  | 401 | * earlier kprobe in the hlist with the manager kprobe | 
|  | 402 | */ | 
|  | 403 | static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p) | 
|  | 404 | { | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 405 | copy_kprobe(p, ap); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 406 | ap->addr = p->addr; | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 407 | ap->pre_handler = aggr_pre_handler; | 
|  | 408 | ap->post_handler = aggr_post_handler; | 
|  | 409 | ap->fault_handler = aggr_fault_handler; | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 410 | ap->break_handler = aggr_break_handler; | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 411 |  | 
|  | 412 | INIT_LIST_HEAD(&ap->list); | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 413 | list_add_rcu(&p->list, &ap->list); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 414 |  | 
| Keshavamurthy Anil S | adad0f3 | 2005-12-12 00:37:12 -0800 | [diff] [blame] | 415 | hlist_replace_rcu(&p->hlist, &ap->hlist); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 416 | } | 
|  | 417 |  | 
|  | 418 | /* | 
|  | 419 | * This is the second or subsequent kprobe at the address - handle | 
|  | 420 | * the intricacies | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 421 | * TODO: Move kcalloc outside the spin_lock | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 422 | */ | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 423 | static int __kprobes register_aggr_kprobe(struct kprobe *old_p, | 
|  | 424 | struct kprobe *p) | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 425 | { | 
|  | 426 | int ret = 0; | 
|  | 427 | struct kprobe *ap; | 
|  | 428 |  | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 429 | if (old_p->pre_handler == aggr_pre_handler) { | 
|  | 430 | copy_kprobe(old_p, p); | 
|  | 431 | ret = add_new_kprobe(old_p, p); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 432 | } else { | 
|  | 433 | ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC); | 
|  | 434 | if (!ap) | 
|  | 435 | return -ENOMEM; | 
|  | 436 | add_aggr_kprobe(ap, old_p); | 
| Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 437 | copy_kprobe(ap, p); | 
|  | 438 | ret = add_new_kprobe(ap, p); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 439 | } | 
|  | 440 | return ret; | 
|  | 441 | } | 
|  | 442 |  | 
|  | 443 | /* kprobe removal house-keeping routines */ | 
|  | 444 | static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags) | 
|  | 445 | { | 
| Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 446 | arch_disarm_kprobe(p); | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 447 | hlist_del_rcu(&p->hlist); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 448 | spin_unlock_irqrestore(&kprobe_lock, flags); | 
|  | 449 | arch_remove_kprobe(p); | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | static inline void cleanup_aggr_kprobe(struct kprobe *old_p, | 
|  | 453 | struct kprobe *p, unsigned long flags) | 
|  | 454 | { | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 455 | list_del_rcu(&p->list); | 
|  | 456 | if (list_empty(&old_p->list)) | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 457 | cleanup_kprobe(old_p, flags); | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 458 | else | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 459 | spin_unlock_irqrestore(&kprobe_lock, flags); | 
|  | 460 | } | 
|  | 461 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 462 | static int __kprobes in_kprobes_functions(unsigned long addr) | 
|  | 463 | { | 
|  | 464 | if (addr >= (unsigned long)__kprobes_text_start | 
|  | 465 | && addr < (unsigned long)__kprobes_text_end) | 
|  | 466 | return -EINVAL; | 
|  | 467 | return 0; | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | int __kprobes register_kprobe(struct kprobe *p) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { | 
|  | 472 | int ret = 0; | 
|  | 473 | unsigned long flags = 0; | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 474 | struct kprobe *old_p; | 
| Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 475 | struct module *mod; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 |  | 
| Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 477 | if ((!kernel_text_address((unsigned long) p->addr)) || | 
|  | 478 | in_kprobes_functions((unsigned long) p->addr)) | 
|  | 479 | return -EINVAL; | 
|  | 480 |  | 
|  | 481 | if ((mod = module_text_address((unsigned long) p->addr)) && | 
|  | 482 | (unlikely(!try_module_get(mod)))) | 
|  | 483 | return -EINVAL; | 
|  | 484 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 485 | if ((ret = arch_prepare_kprobe(p)) != 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | goto rm_kprobe; | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 487 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 488 | p->nmissed = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | spin_lock_irqsave(&kprobe_lock, flags); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 490 | old_p = get_kprobe(p->addr); | 
|  | 491 | if (old_p) { | 
|  | 492 | ret = register_aggr_kprobe(old_p, p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | goto out; | 
|  | 494 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 |  | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 496 | arch_copy_kprobe(p); | 
|  | 497 | INIT_HLIST_NODE(&p->hlist); | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 498 | hlist_add_head_rcu(&p->hlist, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); | 
|  | 500 |  | 
| Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 501 | arch_arm_kprobe(p); | 
|  | 502 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | out: | 
|  | 504 | spin_unlock_irqrestore(&kprobe_lock, flags); | 
|  | 505 | rm_kprobe: | 
|  | 506 | if (ret == -EEXIST) | 
|  | 507 | arch_remove_kprobe(p); | 
| Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 508 | if (ret && mod) | 
|  | 509 | module_put(mod); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return ret; | 
|  | 511 | } | 
|  | 512 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 513 | void __kprobes unregister_kprobe(struct kprobe *p) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { | 
|  | 515 | unsigned long flags; | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 516 | struct kprobe *old_p; | 
| Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 517 | struct module *mod; | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 518 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | spin_lock_irqsave(&kprobe_lock, flags); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 520 | old_p = get_kprobe(p->addr); | 
|  | 521 | if (old_p) { | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 522 | /* cleanup_*_kprobe() does the spin_unlock_irqrestore */ | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 523 | if (old_p->pre_handler == aggr_pre_handler) | 
|  | 524 | cleanup_aggr_kprobe(old_p, p, flags); | 
|  | 525 | else | 
|  | 526 | cleanup_kprobe(p, flags); | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 527 |  | 
|  | 528 | synchronize_sched(); | 
| Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 529 |  | 
|  | 530 | if ((mod = module_text_address((unsigned long)p->addr))) | 
|  | 531 | module_put(mod); | 
|  | 532 |  | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 533 | if (old_p->pre_handler == aggr_pre_handler && | 
|  | 534 | list_empty(&old_p->list)) | 
|  | 535 | kfree(old_p); | 
| Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 536 | } else | 
| Prasanna S Panchamukhi | 04dea5f | 2005-05-05 16:15:41 -0700 | [diff] [blame] | 537 | spin_unlock_irqrestore(&kprobe_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } | 
|  | 539 |  | 
|  | 540 | static struct notifier_block kprobe_exceptions_nb = { | 
|  | 541 | .notifier_call = kprobe_exceptions_notify, | 
|  | 542 | .priority = 0x7fffffff /* we need to notified first */ | 
|  | 543 | }; | 
|  | 544 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 545 | int __kprobes register_jprobe(struct jprobe *jp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { | 
|  | 547 | /* Todo: Verify probepoint is a function entry point */ | 
|  | 548 | jp->kp.pre_handler = setjmp_pre_handler; | 
|  | 549 | jp->kp.break_handler = longjmp_break_handler; | 
|  | 550 |  | 
|  | 551 | return register_kprobe(&jp->kp); | 
|  | 552 | } | 
|  | 553 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 554 | void __kprobes unregister_jprobe(struct jprobe *jp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { | 
|  | 556 | unregister_kprobe(&jp->kp); | 
|  | 557 | } | 
|  | 558 |  | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 559 | #ifdef ARCH_SUPPORTS_KRETPROBES | 
|  | 560 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 561 | int __kprobes register_kretprobe(struct kretprobe *rp) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 562 | { | 
|  | 563 | int ret = 0; | 
|  | 564 | struct kretprobe_instance *inst; | 
|  | 565 | int i; | 
|  | 566 |  | 
|  | 567 | rp->kp.pre_handler = pre_handler_kretprobe; | 
|  | 568 |  | 
|  | 569 | /* Pre-allocate memory for max kretprobe instances */ | 
|  | 570 | if (rp->maxactive <= 0) { | 
|  | 571 | #ifdef CONFIG_PREEMPT | 
|  | 572 | rp->maxactive = max(10, 2 * NR_CPUS); | 
|  | 573 | #else | 
|  | 574 | rp->maxactive = NR_CPUS; | 
|  | 575 | #endif | 
|  | 576 | } | 
|  | 577 | INIT_HLIST_HEAD(&rp->used_instances); | 
|  | 578 | INIT_HLIST_HEAD(&rp->free_instances); | 
|  | 579 | for (i = 0; i < rp->maxactive; i++) { | 
|  | 580 | inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL); | 
|  | 581 | if (inst == NULL) { | 
|  | 582 | free_rp_inst(rp); | 
|  | 583 | return -ENOMEM; | 
|  | 584 | } | 
|  | 585 | INIT_HLIST_NODE(&inst->uflist); | 
|  | 586 | hlist_add_head(&inst->uflist, &rp->free_instances); | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | rp->nmissed = 0; | 
|  | 590 | /* Establish function entry probe point */ | 
|  | 591 | if ((ret = register_kprobe(&rp->kp)) != 0) | 
|  | 592 | free_rp_inst(rp); | 
|  | 593 | return ret; | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | #else /* ARCH_SUPPORTS_KRETPROBES */ | 
|  | 597 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 598 | int __kprobes register_kretprobe(struct kretprobe *rp) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 599 | { | 
|  | 600 | return -ENOSYS; | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | #endif /* ARCH_SUPPORTS_KRETPROBES */ | 
|  | 604 |  | 
| Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 605 | void __kprobes unregister_kretprobe(struct kretprobe *rp) | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 606 | { | 
|  | 607 | unsigned long flags; | 
|  | 608 | struct kretprobe_instance *ri; | 
|  | 609 |  | 
|  | 610 | unregister_kprobe(&rp->kp); | 
|  | 611 | /* No race here */ | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 612 | spin_lock_irqsave(&kretprobe_lock, flags); | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 613 | free_rp_inst(rp); | 
|  | 614 | while ((ri = get_used_rp_inst(rp)) != NULL) { | 
|  | 615 | ri->rp = NULL; | 
|  | 616 | hlist_del(&ri->uflist); | 
|  | 617 | } | 
| Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 618 | spin_unlock_irqrestore(&kretprobe_lock, flags); | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 619 | } | 
|  | 620 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | static int __init init_kprobes(void) | 
|  | 622 | { | 
|  | 623 | int i, err = 0; | 
|  | 624 |  | 
|  | 625 | /* FIXME allocate the probe table, currently defined statically */ | 
|  | 626 | /* initialize all list heads */ | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 627 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | INIT_HLIST_HEAD(&kprobe_table[i]); | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 629 | INIT_HLIST_HEAD(&kretprobe_inst_table[i]); | 
|  | 630 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 |  | 
| Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 632 | err = arch_init_kprobes(); | 
| Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 633 | if (!err) | 
|  | 634 | err = register_die_notifier(&kprobe_exceptions_nb); | 
|  | 635 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return err; | 
|  | 637 | } | 
|  | 638 |  | 
|  | 639 | __initcall(init_kprobes); | 
|  | 640 |  | 
|  | 641 | EXPORT_SYMBOL_GPL(register_kprobe); | 
|  | 642 | EXPORT_SYMBOL_GPL(unregister_kprobe); | 
|  | 643 | EXPORT_SYMBOL_GPL(register_jprobe); | 
|  | 644 | EXPORT_SYMBOL_GPL(unregister_jprobe); | 
|  | 645 | EXPORT_SYMBOL_GPL(jprobe_return); | 
| Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 646 | EXPORT_SYMBOL_GPL(register_kretprobe); | 
|  | 647 | EXPORT_SYMBOL_GPL(unregister_kretprobe); | 
|  | 648 |  |