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