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