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