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> |
Randy Dunlap | e386979 | 2007-05-08 00:27:01 -0700 | [diff] [blame] | 38 | #include <linux/stddef.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/module.h> |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 40 | #include <linux/moduleloader.h> |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 41 | #include <linux/kallsyms.h> |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 42 | #include <linux/freezer.h> |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 43 | #include <linux/seq_file.h> |
| 44 | #include <linux/debugfs.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 45 | #include <linux/kdebug.h> |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 46 | |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 47 | #include <asm-generic/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/cacheflush.h> |
| 49 | #include <asm/errno.h> |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 50 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | #define KPROBE_HASH_BITS 6 |
| 53 | #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) |
| 54 | |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Some oddball architectures like 64bit powerpc have function descriptors |
| 58 | * so this must be overridable. |
| 59 | */ |
| 60 | #ifndef kprobe_lookup_name |
| 61 | #define kprobe_lookup_name(name, addr) \ |
| 62 | addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name))) |
| 63 | #endif |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 66 | static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 68 | /* NOTE: change this value only with kprobe_mutex held */ |
| 69 | static bool kprobe_enabled; |
| 70 | |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 71 | DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */ |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 72 | DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */ |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 73 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Normally, functions that we'd want to prohibit kprobes in, are marked |
| 77 | * __kprobes. But, there are cases where such functions already belong to |
| 78 | * a different section (__sched for preempt_schedule) |
| 79 | * |
| 80 | * For such cases, we now have a blacklist |
| 81 | */ |
Daniel Guilak | 544304b | 2008-07-10 09:38:19 -0700 | [diff] [blame^] | 82 | static struct kprobe_blackpoint kprobe_blacklist[] = { |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 83 | {"preempt_schedule",}, |
| 84 | {NULL} /* Terminator */ |
| 85 | }; |
| 86 | |
Anil S Keshavamurthy | 2d14e39 | 2006-01-09 20:52:41 -0800 | [diff] [blame] | 87 | #ifdef __ARCH_WANT_KPROBES_INSN_SLOT |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 88 | /* |
| 89 | * kprobe->ainsn.insn points to the copy of the instruction to be |
| 90 | * single-stepped. x86_64, POWER4 and above have no-exec support and |
| 91 | * stepping on the instruction on a vmalloced/kmalloced/data page |
| 92 | * is a recipe for disaster |
| 93 | */ |
| 94 | #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t))) |
| 95 | |
| 96 | struct kprobe_insn_page { |
| 97 | struct hlist_node hlist; |
| 98 | kprobe_opcode_t *insns; /* Page of instruction slots */ |
| 99 | char slot_used[INSNS_PER_PAGE]; |
| 100 | int nused; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 101 | int ngarbage; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 104 | enum kprobe_slot_state { |
| 105 | SLOT_CLEAN = 0, |
| 106 | SLOT_DIRTY = 1, |
| 107 | SLOT_USED = 2, |
| 108 | }; |
| 109 | |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 110 | static struct hlist_head kprobe_insn_pages; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 111 | static int kprobe_garbage_slots; |
| 112 | static int collect_garbage_slots(void); |
| 113 | |
| 114 | static int __kprobes check_safety(void) |
| 115 | { |
| 116 | int ret = 0; |
| 117 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM) |
| 118 | ret = freeze_processes(); |
| 119 | if (ret == 0) { |
| 120 | struct task_struct *p, *q; |
| 121 | do_each_thread(p, q) { |
| 122 | if (p != current && p->state == TASK_RUNNING && |
| 123 | p->pid != 0) { |
| 124 | printk("Check failed: %s is running\n",p->comm); |
| 125 | ret = -1; |
| 126 | goto loop_end; |
| 127 | } |
| 128 | } while_each_thread(p, q); |
| 129 | } |
| 130 | loop_end: |
| 131 | thaw_processes(); |
| 132 | #else |
| 133 | synchronize_sched(); |
| 134 | #endif |
| 135 | return ret; |
| 136 | } |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * get_insn_slot() - Find a slot on an executable page for an instruction. |
| 140 | * We allocate an executable page if there's no room on existing ones. |
| 141 | */ |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 142 | kprobe_opcode_t __kprobes *get_insn_slot(void) |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 143 | { |
| 144 | struct kprobe_insn_page *kip; |
| 145 | struct hlist_node *pos; |
| 146 | |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 147 | retry: |
Christoph Hellwig | b0bb501 | 2007-05-08 00:34:11 -0700 | [diff] [blame] | 148 | hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) { |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 149 | if (kip->nused < INSNS_PER_PAGE) { |
| 150 | int i; |
| 151 | for (i = 0; i < INSNS_PER_PAGE; i++) { |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 152 | if (kip->slot_used[i] == SLOT_CLEAN) { |
| 153 | kip->slot_used[i] = SLOT_USED; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 154 | kip->nused++; |
| 155 | return kip->insns + (i * MAX_INSN_SIZE); |
| 156 | } |
| 157 | } |
| 158 | /* Surprise! No unused slots. Fix kip->nused. */ |
| 159 | kip->nused = INSNS_PER_PAGE; |
| 160 | } |
| 161 | } |
| 162 | |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 163 | /* If there are any garbage slots, collect it and try again. */ |
| 164 | if (kprobe_garbage_slots && collect_garbage_slots() == 0) { |
| 165 | goto retry; |
| 166 | } |
| 167 | /* All out of space. Need to allocate a new page. Use slot 0. */ |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 168 | kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL); |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 169 | if (!kip) |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 170 | return NULL; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * Use module_alloc so this page is within +/- 2GB of where the |
| 174 | * kernel image and loaded module images reside. This is required |
| 175 | * so x86_64 can correctly handle the %rip-relative fixups. |
| 176 | */ |
| 177 | kip->insns = module_alloc(PAGE_SIZE); |
| 178 | if (!kip->insns) { |
| 179 | kfree(kip); |
| 180 | return NULL; |
| 181 | } |
| 182 | INIT_HLIST_NODE(&kip->hlist); |
| 183 | hlist_add_head(&kip->hlist, &kprobe_insn_pages); |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 184 | memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE); |
| 185 | kip->slot_used[0] = SLOT_USED; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 186 | kip->nused = 1; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 187 | kip->ngarbage = 0; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 188 | return kip->insns; |
| 189 | } |
| 190 | |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 191 | /* Return 1 if all garbages are collected, otherwise 0. */ |
| 192 | static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx) |
| 193 | { |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 194 | kip->slot_used[idx] = SLOT_CLEAN; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 195 | kip->nused--; |
| 196 | if (kip->nused == 0) { |
| 197 | /* |
| 198 | * Page is no longer in use. Free it unless |
| 199 | * it's the last one. We keep the last one |
| 200 | * so as not to have to set it up again the |
| 201 | * next time somebody inserts a probe. |
| 202 | */ |
| 203 | hlist_del(&kip->hlist); |
| 204 | if (hlist_empty(&kprobe_insn_pages)) { |
| 205 | INIT_HLIST_NODE(&kip->hlist); |
| 206 | hlist_add_head(&kip->hlist, |
| 207 | &kprobe_insn_pages); |
| 208 | } else { |
| 209 | module_free(NULL, kip->insns); |
| 210 | kfree(kip); |
| 211 | } |
| 212 | return 1; |
| 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int __kprobes collect_garbage_slots(void) |
| 218 | { |
| 219 | struct kprobe_insn_page *kip; |
| 220 | struct hlist_node *pos, *next; |
| 221 | |
| 222 | /* Ensure no-one is preepmted on the garbages */ |
| 223 | if (check_safety() != 0) |
| 224 | return -EAGAIN; |
| 225 | |
Christoph Hellwig | b0bb501 | 2007-05-08 00:34:11 -0700 | [diff] [blame] | 226 | hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) { |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 227 | int i; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 228 | if (kip->ngarbage == 0) |
| 229 | continue; |
| 230 | kip->ngarbage = 0; /* we will collect all garbages */ |
| 231 | for (i = 0; i < INSNS_PER_PAGE; i++) { |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 232 | if (kip->slot_used[i] == SLOT_DIRTY && |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 233 | collect_one_slot(kip, i)) |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | kprobe_garbage_slots = 0; |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty) |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 242 | { |
| 243 | struct kprobe_insn_page *kip; |
| 244 | struct hlist_node *pos; |
| 245 | |
Christoph Hellwig | b0bb501 | 2007-05-08 00:34:11 -0700 | [diff] [blame] | 246 | hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) { |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 247 | if (kip->insns <= slot && |
| 248 | slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) { |
| 249 | int i = (slot - kip->insns) / MAX_INSN_SIZE; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 250 | if (dirty) { |
Masami Hiramatsu | ab40c5c | 2007-01-30 14:36:06 -0800 | [diff] [blame] | 251 | kip->slot_used[i] = SLOT_DIRTY; |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 252 | kip->ngarbage++; |
| 253 | } else { |
| 254 | collect_one_slot(kip, i); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 255 | } |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 256 | break; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 259 | |
| 260 | if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE) |
Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 261 | collect_garbage_slots(); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 262 | } |
Anil S Keshavamurthy | 2d14e39 | 2006-01-09 20:52:41 -0800 | [diff] [blame] | 263 | #endif |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 264 | |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 265 | /* We have preemption disabled.. so it is safe to use __ versions */ |
| 266 | static inline void set_kprobe_instance(struct kprobe *kp) |
| 267 | { |
| 268 | __get_cpu_var(kprobe_instance) = kp; |
| 269 | } |
| 270 | |
| 271 | static inline void reset_kprobe_instance(void) |
| 272 | { |
| 273 | __get_cpu_var(kprobe_instance) = NULL; |
| 274 | } |
| 275 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 276 | /* |
| 277 | * This routine is called either: |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 278 | * - under the kprobe_mutex - during kprobe_[un]register() |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 279 | * OR |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 280 | * - with preemption disabled - from arch/xxx/kernel/kprobes.c |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 281 | */ |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 282 | struct kprobe __kprobes *get_kprobe(void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
| 284 | struct hlist_head *head; |
| 285 | struct hlist_node *node; |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 286 | struct kprobe *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 289 | hlist_for_each_entry_rcu(p, node, head, hlist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | if (p->addr == addr) |
| 291 | return p; |
| 292 | } |
| 293 | return NULL; |
| 294 | } |
| 295 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 296 | /* |
| 297 | * Aggregate handlers for multiple kprobes support - these handlers |
| 298 | * take care of invoking the individual kprobe handlers on p->list |
| 299 | */ |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 300 | 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] | 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->pre_handler) { |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 306 | set_kprobe_instance(kp); |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 307 | if (kp->pre_handler(kp, regs)) |
| 308 | return 1; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 309 | } |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 310 | reset_kprobe_instance(); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 315 | static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs, |
| 316 | unsigned long flags) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 317 | { |
| 318 | struct kprobe *kp; |
| 319 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 320 | list_for_each_entry_rcu(kp, &p->list, list) { |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 321 | if (kp->post_handler) { |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 322 | set_kprobe_instance(kp); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 323 | kp->post_handler(kp, regs, flags); |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 324 | reset_kprobe_instance(); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 329 | static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, |
| 330 | int trapnr) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -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 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 334 | /* |
| 335 | * if we faulted "during" the execution of a user specified |
| 336 | * probe handler, invoke just that probe's fault handler |
| 337 | */ |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 338 | if (cur && cur->fault_handler) { |
| 339 | if (cur->fault_handler(cur, regs, trapnr)) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 340 | return 1; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 345 | 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] | 346 | { |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 347 | struct kprobe *cur = __get_cpu_var(kprobe_instance); |
| 348 | int ret = 0; |
| 349 | |
| 350 | if (cur && cur->break_handler) { |
| 351 | if (cur->break_handler(cur, regs)) |
| 352 | ret = 1; |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 353 | } |
Ananth N Mavinakayanahalli | e658452 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 354 | reset_kprobe_instance(); |
| 355 | return ret; |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 358 | /* Walks the list and increments nmissed count for multiprobe case */ |
| 359 | void __kprobes kprobes_inc_nmissed_count(struct kprobe *p) |
| 360 | { |
| 361 | struct kprobe *kp; |
| 362 | if (p->pre_handler != aggr_pre_handler) { |
| 363 | p->nmissed++; |
| 364 | } else { |
| 365 | list_for_each_entry_rcu(kp, &p->list, list) |
| 366 | kp->nmissed++; |
| 367 | } |
| 368 | return; |
| 369 | } |
| 370 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 371 | /* Called with kretprobe_lock held */ |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 372 | void __kprobes recycle_rp_inst(struct kretprobe_instance *ri, |
| 373 | struct hlist_head *head) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 374 | { |
| 375 | /* remove rp inst off the rprobe_inst_table */ |
| 376 | hlist_del(&ri->hlist); |
| 377 | if (ri->rp) { |
| 378 | /* remove rp inst off the used list */ |
| 379 | hlist_del(&ri->uflist); |
| 380 | /* put rp inst back onto the free list */ |
| 381 | INIT_HLIST_NODE(&ri->uflist); |
| 382 | hlist_add_head(&ri->uflist, &ri->rp->free_instances); |
| 383 | } else |
| 384 | /* Unregistering */ |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 385 | hlist_add_head(&ri->hlist, head); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 388 | struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 389 | { |
| 390 | return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)]; |
| 391 | } |
| 392 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 393 | /* |
bibo mao | c6fd91f | 2006-03-26 01:38:20 -0800 | [diff] [blame] | 394 | * This function is called from finish_task_switch when task tk becomes dead, |
| 395 | * so that we can recycle any function-return probe instances associated |
| 396 | * with this task. These left over instances represent probed functions |
| 397 | * that have been called but will never return. |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 398 | */ |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 399 | void __kprobes kprobe_flush_task(struct task_struct *tk) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 400 | { |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 401 | struct kretprobe_instance *ri; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 402 | struct hlist_head *head, empty_rp; |
Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 403 | struct hlist_node *node, *tmp; |
Hien Nguyen | 0aa55e4 | 2005-06-23 00:09:26 -0700 | [diff] [blame] | 404 | unsigned long flags = 0; |
Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 405 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 406 | INIT_HLIST_HEAD(&empty_rp); |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 407 | spin_lock_irqsave(&kretprobe_lock, flags); |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 408 | head = kretprobe_inst_table_head(tk); |
| 409 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
| 410 | if (ri->task == tk) |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 411 | recycle_rp_inst(ri, &empty_rp); |
bibo,mao | 62c27be | 2006-10-02 02:17:33 -0700 | [diff] [blame] | 412 | } |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 413 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 414 | |
| 415 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
| 416 | hlist_del(&ri->hlist); |
| 417 | kfree(ri); |
| 418 | } |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 421 | static inline void free_rp_inst(struct kretprobe *rp) |
| 422 | { |
| 423 | struct kretprobe_instance *ri; |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 424 | struct hlist_node *pos, *next; |
| 425 | |
| 426 | hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) { |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 427 | hlist_del(&ri->uflist); |
| 428 | kfree(ri); |
| 429 | } |
| 430 | } |
| 431 | |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 432 | static void __kprobes cleanup_rp_inst(struct kretprobe *rp) |
| 433 | { |
| 434 | unsigned long flags; |
| 435 | struct kretprobe_instance *ri; |
| 436 | struct hlist_node *pos, *next; |
| 437 | /* No race here */ |
| 438 | spin_lock_irqsave(&kretprobe_lock, flags); |
| 439 | hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) { |
| 440 | ri->rp = NULL; |
| 441 | hlist_del(&ri->uflist); |
| 442 | } |
| 443 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
| 444 | free_rp_inst(rp); |
| 445 | } |
| 446 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 447 | /* |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 448 | * Keep all fields in the kprobe consistent |
| 449 | */ |
| 450 | static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p) |
| 451 | { |
| 452 | memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t)); |
| 453 | memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn)); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Add the new probe to old_p->list. Fail if this is the |
| 458 | * second jprobe at the address - two jprobes can't coexist |
| 459 | */ |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 460 | 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] | 461 | { |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 462 | if (p->break_handler) { |
mao, bibo | 3672165 | 2006-06-26 00:25:22 -0700 | [diff] [blame] | 463 | if (old_p->break_handler) |
| 464 | return -EEXIST; |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 465 | list_add_tail_rcu(&p->list, &old_p->list); |
mao, bibo | 3672165 | 2006-06-26 00:25:22 -0700 | [diff] [blame] | 466 | old_p->break_handler = aggr_break_handler; |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 467 | } else |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 468 | list_add_rcu(&p->list, &old_p->list); |
mao, bibo | 3672165 | 2006-06-26 00:25:22 -0700 | [diff] [blame] | 469 | if (p->post_handler && !old_p->post_handler) |
| 470 | old_p->post_handler = aggr_post_handler; |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /* |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 475 | * Fill in the required fields of the "manager kprobe". Replace the |
| 476 | * earlier kprobe in the hlist with the manager kprobe |
| 477 | */ |
| 478 | static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p) |
| 479 | { |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 480 | copy_kprobe(p, ap); |
bibo, mao | a9ad965 | 2006-07-30 03:03:26 -0700 | [diff] [blame] | 481 | flush_insn_slot(ap); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 482 | ap->addr = p->addr; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 483 | ap->pre_handler = aggr_pre_handler; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 484 | ap->fault_handler = aggr_fault_handler; |
mao, bibo | 3672165 | 2006-06-26 00:25:22 -0700 | [diff] [blame] | 485 | if (p->post_handler) |
| 486 | ap->post_handler = aggr_post_handler; |
| 487 | if (p->break_handler) |
| 488 | ap->break_handler = aggr_break_handler; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 489 | |
| 490 | INIT_LIST_HEAD(&ap->list); |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 491 | list_add_rcu(&p->list, &ap->list); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 492 | |
Keshavamurthy Anil S | adad0f3 | 2005-12-12 00:37:12 -0800 | [diff] [blame] | 493 | hlist_replace_rcu(&p->hlist, &ap->hlist); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | /* |
| 497 | * This is the second or subsequent kprobe at the address - handle |
| 498 | * the intricacies |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 499 | */ |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 500 | static int __kprobes register_aggr_kprobe(struct kprobe *old_p, |
| 501 | struct kprobe *p) |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 502 | { |
| 503 | int ret = 0; |
| 504 | struct kprobe *ap; |
| 505 | |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 506 | if (old_p->pre_handler == aggr_pre_handler) { |
| 507 | copy_kprobe(old_p, p); |
| 508 | ret = add_new_kprobe(old_p, p); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 509 | } else { |
Keshavamurthy Anil S | a0d5006 | 2006-01-09 20:52:46 -0800 | [diff] [blame] | 510 | ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 511 | if (!ap) |
| 512 | return -ENOMEM; |
| 513 | add_aggr_kprobe(ap, old_p); |
Prasanna S Panchamukhi | 8b0914e | 2005-06-23 00:09:41 -0700 | [diff] [blame] | 514 | copy_kprobe(ap, p); |
| 515 | ret = add_new_kprobe(ap, p); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 516 | } |
| 517 | return ret; |
| 518 | } |
| 519 | |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 520 | static int __kprobes in_kprobes_functions(unsigned long addr) |
| 521 | { |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 522 | struct kprobe_blackpoint *kb; |
| 523 | |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 524 | if (addr >= (unsigned long)__kprobes_text_start && |
| 525 | addr < (unsigned long)__kprobes_text_end) |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 526 | return -EINVAL; |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 527 | /* |
| 528 | * If there exists a kprobe_blacklist, verify and |
| 529 | * fail any probe registration in the prohibited area |
| 530 | */ |
| 531 | for (kb = kprobe_blacklist; kb->name != NULL; kb++) { |
| 532 | if (kb->start_addr) { |
| 533 | if (addr >= kb->start_addr && |
| 534 | addr < (kb->start_addr + kb->range)) |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | } |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 541 | /* |
| 542 | * If we have a symbol_name argument, look it up and add the offset field |
| 543 | * to it. This way, we can specify a relative address to a symbol. |
| 544 | */ |
| 545 | static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p) |
| 546 | { |
| 547 | kprobe_opcode_t *addr = p->addr; |
| 548 | if (p->symbol_name) { |
| 549 | if (addr) |
| 550 | return NULL; |
| 551 | kprobe_lookup_name(p->symbol_name, addr); |
| 552 | } |
| 553 | |
| 554 | if (!addr) |
| 555 | return NULL; |
| 556 | return (kprobe_opcode_t *)(((char *)addr) + p->offset); |
| 557 | } |
| 558 | |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 559 | static int __kprobes __register_kprobe(struct kprobe *p, |
| 560 | unsigned long called_from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
| 562 | int ret = 0; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 563 | struct kprobe *old_p; |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 564 | struct module *probed_mod; |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 565 | kprobe_opcode_t *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 567 | addr = kprobe_addr(p); |
| 568 | if (!addr) |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 569 | return -EINVAL; |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 570 | p->addr = addr; |
Ananth N Mavinakayanahalli | 3a872d8 | 2006-10-02 02:17:30 -0700 | [diff] [blame] | 571 | |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 572 | if (!kernel_text_address((unsigned long) p->addr) || |
| 573 | in_kprobes_functions((unsigned long) p->addr)) |
Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 574 | return -EINVAL; |
| 575 | |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 576 | p->mod_refcounted = 0; |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * Check if are we probing a module. |
| 580 | */ |
| 581 | probed_mod = module_text_address((unsigned long) p->addr); |
| 582 | if (probed_mod) { |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 583 | struct module *calling_mod = module_text_address(called_from); |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 584 | /* |
| 585 | * We must allow modules to probe themself and in this case |
| 586 | * avoid incrementing the module refcount, so as to allow |
| 587 | * unloading of self probing modules. |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 588 | */ |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 589 | if (calling_mod && calling_mod != probed_mod) { |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 590 | if (unlikely(!try_module_get(probed_mod))) |
| 591 | return -EINVAL; |
| 592 | p->mod_refcounted = 1; |
| 593 | } else |
| 594 | probed_mod = NULL; |
| 595 | } |
Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 596 | |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 597 | p->nmissed = 0; |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 598 | INIT_LIST_HEAD(&p->list); |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 599 | mutex_lock(&kprobe_mutex); |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 600 | old_p = get_kprobe(p->addr); |
| 601 | if (old_p) { |
| 602 | ret = register_aggr_kprobe(old_p, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | goto out; |
| 604 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 606 | ret = arch_prepare_kprobe(p); |
| 607 | if (ret) |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 608 | goto out; |
| 609 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 610 | INIT_HLIST_NODE(&p->hlist); |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 611 | hlist_add_head_rcu(&p->hlist, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); |
| 613 | |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 614 | if (kprobe_enabled) |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 615 | arch_arm_kprobe(p); |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | out: |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 618 | mutex_unlock(&kprobe_mutex); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 619 | |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 620 | if (ret && probed_mod) |
| 621 | module_put(probed_mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return ret; |
| 623 | } |
| 624 | |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 625 | /* |
| 626 | * Unregister a kprobe without a scheduler synchronization. |
| 627 | */ |
| 628 | static int __kprobes __unregister_kprobe_top(struct kprobe *p) |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 629 | { |
Keshavamurthy Anil S | f709b12 | 2006-01-09 20:52:44 -0800 | [diff] [blame] | 630 | struct kprobe *old_p, *list_p; |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 631 | |
Ananth N Mavinakayanahalli | 64f562c | 2005-05-05 16:15:42 -0700 | [diff] [blame] | 632 | old_p = get_kprobe(p->addr); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 633 | if (unlikely(!old_p)) |
| 634 | return -EINVAL; |
| 635 | |
Keshavamurthy Anil S | f709b12 | 2006-01-09 20:52:44 -0800 | [diff] [blame] | 636 | if (p != old_p) { |
| 637 | list_for_each_entry_rcu(list_p, &old_p->list, list) |
| 638 | if (list_p == p) |
| 639 | /* kprobe p is a valid probe */ |
| 640 | goto valid_p; |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 641 | return -EINVAL; |
Keshavamurthy Anil S | f709b12 | 2006-01-09 20:52:44 -0800 | [diff] [blame] | 642 | } |
| 643 | valid_p: |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 644 | if (old_p == p || |
| 645 | (old_p->pre_handler == aggr_pre_handler && |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 646 | list_is_singular(&old_p->list))) { |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 647 | /* |
| 648 | * Only probe on the hash list. Disarm only if kprobes are |
| 649 | * enabled - otherwise, the breakpoint would already have |
| 650 | * been removed. We save on flushing icache. |
| 651 | */ |
| 652 | if (kprobe_enabled) |
| 653 | arch_disarm_kprobe(p); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 654 | hlist_del_rcu(&old_p->hlist); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 655 | } else { |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 656 | if (p->break_handler) |
| 657 | old_p->break_handler = NULL; |
| 658 | if (p->post_handler) { |
| 659 | list_for_each_entry_rcu(list_p, &old_p->list, list) { |
| 660 | if ((list_p != p) && (list_p->post_handler)) |
| 661 | goto noclean; |
| 662 | } |
| 663 | old_p->post_handler = NULL; |
| 664 | } |
| 665 | noclean: |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 666 | list_del_rcu(&p->list); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 667 | } |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 668 | return 0; |
| 669 | } |
Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 670 | |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 671 | static void __kprobes __unregister_kprobe_bottom(struct kprobe *p) |
| 672 | { |
| 673 | struct module *mod; |
| 674 | struct kprobe *old_p; |
Mao, Bibo | b3e55c7 | 2005-12-12 00:37:00 -0800 | [diff] [blame] | 675 | |
Christoph Hellwig | 6f716ac | 2007-05-08 00:34:13 -0700 | [diff] [blame] | 676 | if (p->mod_refcounted) { |
| 677 | mod = module_text_address((unsigned long)p->addr); |
| 678 | if (mod) |
| 679 | module_put(mod); |
| 680 | } |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 681 | |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 682 | if (list_empty(&p->list) || list_is_singular(&p->list)) { |
| 683 | if (!list_empty(&p->list)) { |
| 684 | /* "p" is the last child of an aggr_kprobe */ |
| 685 | old_p = list_entry(p->list.next, struct kprobe, list); |
| 686 | list_del(&p->list); |
Ananth N Mavinakayanahalli | 3516a46 | 2005-11-07 01:00:13 -0800 | [diff] [blame] | 687 | kfree(old_p); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 688 | } |
Ananth N Mavinakayanahalli | 0498b63 | 2006-01-09 20:52:46 -0800 | [diff] [blame] | 689 | arch_remove_kprobe(p); |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 693 | static int __register_kprobes(struct kprobe **kps, int num, |
| 694 | unsigned long called_from) |
| 695 | { |
| 696 | int i, ret = 0; |
| 697 | |
| 698 | if (num <= 0) |
| 699 | return -EINVAL; |
| 700 | for (i = 0; i < num; i++) { |
| 701 | ret = __register_kprobe(kps[i], called_from); |
Masami Hiramatsu | 67dddaa | 2008-06-12 15:21:35 -0700 | [diff] [blame] | 702 | if (ret < 0) { |
| 703 | if (i > 0) |
| 704 | unregister_kprobes(kps, i); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | } |
| 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Registration and unregistration functions for kprobe. |
| 713 | */ |
| 714 | int __kprobes register_kprobe(struct kprobe *p) |
| 715 | { |
| 716 | return __register_kprobes(&p, 1, |
| 717 | (unsigned long)__builtin_return_address(0)); |
| 718 | } |
| 719 | |
| 720 | void __kprobes unregister_kprobe(struct kprobe *p) |
| 721 | { |
| 722 | unregister_kprobes(&p, 1); |
| 723 | } |
| 724 | |
| 725 | int __kprobes register_kprobes(struct kprobe **kps, int num) |
| 726 | { |
| 727 | return __register_kprobes(kps, num, |
| 728 | (unsigned long)__builtin_return_address(0)); |
| 729 | } |
| 730 | |
| 731 | void __kprobes unregister_kprobes(struct kprobe **kps, int num) |
| 732 | { |
| 733 | int i; |
| 734 | |
| 735 | if (num <= 0) |
| 736 | return; |
| 737 | mutex_lock(&kprobe_mutex); |
| 738 | for (i = 0; i < num; i++) |
| 739 | if (__unregister_kprobe_top(kps[i]) < 0) |
| 740 | kps[i]->addr = NULL; |
| 741 | mutex_unlock(&kprobe_mutex); |
| 742 | |
| 743 | synchronize_sched(); |
| 744 | for (i = 0; i < num; i++) |
| 745 | if (kps[i]->addr) |
| 746 | __unregister_kprobe_bottom(kps[i]); |
| 747 | } |
| 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | static struct notifier_block kprobe_exceptions_nb = { |
| 750 | .notifier_call = kprobe_exceptions_notify, |
Anil S Keshavamurthy | 3d5631e | 2006-06-26 00:25:28 -0700 | [diff] [blame] | 751 | .priority = 0x7fffffff /* we need to be notified first */ |
| 752 | }; |
| 753 | |
Michael Ellerman | 3d7e338 | 2007-07-19 01:48:11 -0700 | [diff] [blame] | 754 | unsigned long __weak arch_deref_entry_point(void *entry) |
| 755 | { |
| 756 | return (unsigned long)entry; |
| 757 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
Masami Hiramatsu | 26b31c1 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 759 | static int __register_jprobes(struct jprobe **jps, int num, |
| 760 | unsigned long called_from) |
| 761 | { |
| 762 | struct jprobe *jp; |
| 763 | int ret = 0, i; |
| 764 | |
| 765 | if (num <= 0) |
| 766 | return -EINVAL; |
| 767 | for (i = 0; i < num; i++) { |
| 768 | unsigned long addr; |
| 769 | jp = jps[i]; |
| 770 | addr = arch_deref_entry_point(jp->entry); |
| 771 | |
| 772 | if (!kernel_text_address(addr)) |
| 773 | ret = -EINVAL; |
| 774 | else { |
| 775 | /* Todo: Verify probepoint is a function entry point */ |
| 776 | jp->kp.pre_handler = setjmp_pre_handler; |
| 777 | jp->kp.break_handler = longjmp_break_handler; |
| 778 | ret = __register_kprobe(&jp->kp, called_from); |
| 779 | } |
Masami Hiramatsu | 67dddaa | 2008-06-12 15:21:35 -0700 | [diff] [blame] | 780 | if (ret < 0) { |
| 781 | if (i > 0) |
| 782 | unregister_jprobes(jps, i); |
Masami Hiramatsu | 26b31c1 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 783 | break; |
| 784 | } |
| 785 | } |
| 786 | return ret; |
| 787 | } |
| 788 | |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 789 | int __kprobes register_jprobe(struct jprobe *jp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
Masami Hiramatsu | 26b31c1 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 791 | return __register_jprobes(&jp, 1, |
Keshavamurthy Anil S | df019b1 | 2006-01-11 12:17:41 -0800 | [diff] [blame] | 792 | (unsigned long)__builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 795 | void __kprobes unregister_jprobe(struct jprobe *jp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
Masami Hiramatsu | 26b31c1 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 797 | unregister_jprobes(&jp, 1); |
| 798 | } |
| 799 | |
| 800 | int __kprobes register_jprobes(struct jprobe **jps, int num) |
| 801 | { |
| 802 | return __register_jprobes(jps, num, |
| 803 | (unsigned long)__builtin_return_address(0)); |
| 804 | } |
| 805 | |
| 806 | void __kprobes unregister_jprobes(struct jprobe **jps, int num) |
| 807 | { |
| 808 | int i; |
| 809 | |
| 810 | if (num <= 0) |
| 811 | return; |
| 812 | mutex_lock(&kprobe_mutex); |
| 813 | for (i = 0; i < num; i++) |
| 814 | if (__unregister_kprobe_top(&jps[i]->kp) < 0) |
| 815 | jps[i]->kp.addr = NULL; |
| 816 | mutex_unlock(&kprobe_mutex); |
| 817 | |
| 818 | synchronize_sched(); |
| 819 | for (i = 0; i < num; i++) { |
| 820 | if (jps[i]->kp.addr) |
| 821 | __unregister_kprobe_bottom(&jps[i]->kp); |
| 822 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 825 | #ifdef CONFIG_KRETPROBES |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 826 | /* |
| 827 | * This kprobe pre_handler is registered with every kretprobe. When probe |
| 828 | * hits it will set up the return probe. |
| 829 | */ |
| 830 | static int __kprobes pre_handler_kretprobe(struct kprobe *p, |
| 831 | struct pt_regs *regs) |
| 832 | { |
| 833 | struct kretprobe *rp = container_of(p, struct kretprobe, kp); |
| 834 | unsigned long flags = 0; |
| 835 | |
| 836 | /*TODO: consider to only swap the RA after the last pre_handler fired */ |
| 837 | spin_lock_irqsave(&kretprobe_lock, flags); |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 838 | if (!hlist_empty(&rp->free_instances)) { |
| 839 | struct kretprobe_instance *ri; |
| 840 | |
| 841 | ri = hlist_entry(rp->free_instances.first, |
| 842 | struct kretprobe_instance, uflist); |
| 843 | ri->rp = rp; |
| 844 | ri->task = current; |
Abhishek Sagar | f47cd9b | 2008-02-06 01:38:22 -0800 | [diff] [blame] | 845 | |
| 846 | if (rp->entry_handler && rp->entry_handler(ri, regs)) { |
| 847 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
| 848 | return 0; |
| 849 | } |
| 850 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 851 | arch_prepare_kretprobe(ri, regs); |
| 852 | |
| 853 | /* XXX(hch): why is there no hlist_move_head? */ |
| 854 | hlist_del(&ri->uflist); |
| 855 | hlist_add_head(&ri->uflist, &ri->rp->used_instances); |
| 856 | hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task)); |
| 857 | } else |
| 858 | rp->nmissed++; |
Adrian Bunk | e65cefe | 2006-02-03 03:03:42 -0800 | [diff] [blame] | 859 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
| 860 | return 0; |
| 861 | } |
| 862 | |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 863 | static int __kprobes __register_kretprobe(struct kretprobe *rp, |
| 864 | unsigned long called_from) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 865 | { |
| 866 | int ret = 0; |
| 867 | struct kretprobe_instance *inst; |
| 868 | int i; |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 869 | void *addr; |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 870 | |
| 871 | if (kretprobe_blacklist_size) { |
Masami Hiramatsu | b2a5cd6 | 2008-03-04 14:29:44 -0800 | [diff] [blame] | 872 | addr = kprobe_addr(&rp->kp); |
| 873 | if (!addr) |
| 874 | return -EINVAL; |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 875 | |
| 876 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
| 877 | if (kretprobe_blacklist[i].addr == addr) |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | } |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 881 | |
| 882 | rp->kp.pre_handler = pre_handler_kretprobe; |
Ananth N Mavinakayanahalli | 7522a84 | 2006-04-20 02:43:11 -0700 | [diff] [blame] | 883 | rp->kp.post_handler = NULL; |
| 884 | rp->kp.fault_handler = NULL; |
| 885 | rp->kp.break_handler = NULL; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 886 | |
| 887 | /* Pre-allocate memory for max kretprobe instances */ |
| 888 | if (rp->maxactive <= 0) { |
| 889 | #ifdef CONFIG_PREEMPT |
| 890 | rp->maxactive = max(10, 2 * NR_CPUS); |
| 891 | #else |
| 892 | rp->maxactive = NR_CPUS; |
| 893 | #endif |
| 894 | } |
| 895 | INIT_HLIST_HEAD(&rp->used_instances); |
| 896 | INIT_HLIST_HEAD(&rp->free_instances); |
| 897 | for (i = 0; i < rp->maxactive; i++) { |
Abhishek Sagar | f47cd9b | 2008-02-06 01:38:22 -0800 | [diff] [blame] | 898 | inst = kmalloc(sizeof(struct kretprobe_instance) + |
| 899 | rp->data_size, GFP_KERNEL); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 900 | if (inst == NULL) { |
| 901 | free_rp_inst(rp); |
| 902 | return -ENOMEM; |
| 903 | } |
| 904 | INIT_HLIST_NODE(&inst->uflist); |
| 905 | hlist_add_head(&inst->uflist, &rp->free_instances); |
| 906 | } |
| 907 | |
| 908 | rp->nmissed = 0; |
| 909 | /* Establish function entry probe point */ |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 910 | ret = __register_kprobe(&rp->kp, called_from); |
| 911 | if (ret != 0) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 912 | free_rp_inst(rp); |
| 913 | return ret; |
| 914 | } |
| 915 | |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 916 | static int __register_kretprobes(struct kretprobe **rps, int num, |
| 917 | unsigned long called_from) |
| 918 | { |
| 919 | int ret = 0, i; |
| 920 | |
| 921 | if (num <= 0) |
| 922 | return -EINVAL; |
| 923 | for (i = 0; i < num; i++) { |
| 924 | ret = __register_kretprobe(rps[i], called_from); |
Masami Hiramatsu | 67dddaa | 2008-06-12 15:21:35 -0700 | [diff] [blame] | 925 | if (ret < 0) { |
| 926 | if (i > 0) |
| 927 | unregister_kretprobes(rps, i); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 928 | break; |
| 929 | } |
| 930 | } |
| 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | int __kprobes register_kretprobe(struct kretprobe *rp) |
| 935 | { |
| 936 | return __register_kretprobes(&rp, 1, |
| 937 | (unsigned long)__builtin_return_address(0)); |
| 938 | } |
| 939 | |
| 940 | void __kprobes unregister_kretprobe(struct kretprobe *rp) |
| 941 | { |
| 942 | unregister_kretprobes(&rp, 1); |
| 943 | } |
| 944 | |
| 945 | int __kprobes register_kretprobes(struct kretprobe **rps, int num) |
| 946 | { |
| 947 | return __register_kretprobes(rps, num, |
| 948 | (unsigned long)__builtin_return_address(0)); |
| 949 | } |
| 950 | |
| 951 | void __kprobes unregister_kretprobes(struct kretprobe **rps, int num) |
| 952 | { |
| 953 | int i; |
| 954 | |
| 955 | if (num <= 0) |
| 956 | return; |
| 957 | mutex_lock(&kprobe_mutex); |
| 958 | for (i = 0; i < num; i++) |
| 959 | if (__unregister_kprobe_top(&rps[i]->kp) < 0) |
| 960 | rps[i]->kp.addr = NULL; |
| 961 | mutex_unlock(&kprobe_mutex); |
| 962 | |
| 963 | synchronize_sched(); |
| 964 | for (i = 0; i < num; i++) { |
| 965 | if (rps[i]->kp.addr) { |
| 966 | __unregister_kprobe_bottom(&rps[i]->kp); |
| 967 | cleanup_rp_inst(rps[i]); |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 972 | #else /* CONFIG_KRETPROBES */ |
Prasanna S Panchamukhi | d0aaff9 | 2005-09-06 15:19:26 -0700 | [diff] [blame] | 973 | int __kprobes register_kretprobe(struct kretprobe *rp) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 974 | { |
| 975 | return -ENOSYS; |
| 976 | } |
| 977 | |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 978 | int __kprobes register_kretprobes(struct kretprobe **rps, int num) |
| 979 | { |
| 980 | return -ENOSYS; |
| 981 | } |
| 982 | void __kprobes unregister_kretprobe(struct kretprobe *rp) |
| 983 | { |
| 984 | } |
| 985 | |
| 986 | void __kprobes unregister_kretprobes(struct kretprobe **rps, int num) |
| 987 | { |
| 988 | } |
| 989 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 990 | static int __kprobes pre_handler_kretprobe(struct kprobe *p, |
| 991 | struct pt_regs *regs) |
| 992 | { |
| 993 | return 0; |
| 994 | } |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 995 | |
Ananth N Mavinakayanahalli | 9edddaa | 2008-03-04 14:28:37 -0800 | [diff] [blame] | 996 | #endif /* CONFIG_KRETPROBES */ |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | static int __init init_kprobes(void) |
| 999 | { |
| 1000 | int i, err = 0; |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1001 | unsigned long offset = 0, size = 0; |
| 1002 | char *modname, namebuf[128]; |
| 1003 | const char *symbol_name; |
| 1004 | void *addr; |
| 1005 | struct kprobe_blackpoint *kb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
| 1007 | /* FIXME allocate the probe table, currently defined statically */ |
| 1008 | /* initialize all list heads */ |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1009 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | INIT_HLIST_HEAD(&kprobe_table[i]); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1011 | INIT_HLIST_HEAD(&kretprobe_inst_table[i]); |
| 1012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Srinivasa Ds | 3d8d996 | 2008-04-28 02:14:26 -0700 | [diff] [blame] | 1014 | /* |
| 1015 | * Lookup and populate the kprobe_blacklist. |
| 1016 | * |
| 1017 | * Unlike the kretprobe blacklist, we'll need to determine |
| 1018 | * the range of addresses that belong to the said functions, |
| 1019 | * since a kprobe need not necessarily be at the beginning |
| 1020 | * of a function. |
| 1021 | */ |
| 1022 | for (kb = kprobe_blacklist; kb->name != NULL; kb++) { |
| 1023 | kprobe_lookup_name(kb->name, addr); |
| 1024 | if (!addr) |
| 1025 | continue; |
| 1026 | |
| 1027 | kb->start_addr = (unsigned long)addr; |
| 1028 | symbol_name = kallsyms_lookup(kb->start_addr, |
| 1029 | &size, &offset, &modname, namebuf); |
| 1030 | if (!symbol_name) |
| 1031 | kb->range = 0; |
| 1032 | else |
| 1033 | kb->range = size; |
| 1034 | } |
| 1035 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 1036 | if (kretprobe_blacklist_size) { |
| 1037 | /* lookup the function address from its name */ |
| 1038 | for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { |
| 1039 | kprobe_lookup_name(kretprobe_blacklist[i].name, |
| 1040 | kretprobe_blacklist[i].addr); |
| 1041 | if (!kretprobe_blacklist[i].addr) |
| 1042 | printk("kretprobe: lookup failed: %s\n", |
| 1043 | kretprobe_blacklist[i].name); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1047 | /* By default, kprobes are enabled */ |
| 1048 | kprobe_enabled = true; |
| 1049 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 1050 | err = arch_init_kprobes(); |
Rusty Lynch | 802eae7 | 2005-06-27 15:17:08 -0700 | [diff] [blame] | 1051 | if (!err) |
| 1052 | err = register_die_notifier(&kprobe_exceptions_nb); |
| 1053 | |
Ananth N Mavinakayanahalli | 8c1c935 | 2008-01-30 13:32:53 +0100 | [diff] [blame] | 1054 | if (!err) |
| 1055 | init_test_probes(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | return err; |
| 1057 | } |
| 1058 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 1059 | #ifdef CONFIG_DEBUG_FS |
| 1060 | static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p, |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1061 | const char *sym, int offset,char *modname) |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 1062 | { |
| 1063 | char *kprobe_type; |
| 1064 | |
| 1065 | if (p->pre_handler == pre_handler_kretprobe) |
| 1066 | kprobe_type = "r"; |
| 1067 | else if (p->pre_handler == setjmp_pre_handler) |
| 1068 | kprobe_type = "j"; |
| 1069 | else |
| 1070 | kprobe_type = "k"; |
| 1071 | if (sym) |
| 1072 | seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type, |
| 1073 | sym, offset, (modname ? modname : " ")); |
| 1074 | else |
| 1075 | seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr); |
| 1076 | } |
| 1077 | |
| 1078 | static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos) |
| 1079 | { |
| 1080 | return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL; |
| 1081 | } |
| 1082 | |
| 1083 | static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos) |
| 1084 | { |
| 1085 | (*pos)++; |
| 1086 | if (*pos >= KPROBE_TABLE_SIZE) |
| 1087 | return NULL; |
| 1088 | return pos; |
| 1089 | } |
| 1090 | |
| 1091 | static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v) |
| 1092 | { |
| 1093 | /* Nothing to do */ |
| 1094 | } |
| 1095 | |
| 1096 | static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v) |
| 1097 | { |
| 1098 | struct hlist_head *head; |
| 1099 | struct hlist_node *node; |
| 1100 | struct kprobe *p, *kp; |
| 1101 | const char *sym = NULL; |
| 1102 | unsigned int i = *(loff_t *) v; |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 1103 | unsigned long offset = 0; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 1104 | char *modname, namebuf[128]; |
| 1105 | |
| 1106 | head = &kprobe_table[i]; |
| 1107 | preempt_disable(); |
| 1108 | hlist_for_each_entry_rcu(p, node, head, hlist) { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 1109 | sym = kallsyms_lookup((unsigned long)p->addr, NULL, |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 1110 | &offset, &modname, namebuf); |
| 1111 | if (p->pre_handler == aggr_pre_handler) { |
| 1112 | list_for_each_entry_rcu(kp, &p->list, list) |
| 1113 | report_probe(pi, kp, sym, offset, modname); |
| 1114 | } else |
| 1115 | report_probe(pi, p, sym, offset, modname); |
| 1116 | } |
| 1117 | preempt_enable(); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | static struct seq_operations kprobes_seq_ops = { |
| 1122 | .start = kprobe_seq_start, |
| 1123 | .next = kprobe_seq_next, |
| 1124 | .stop = kprobe_seq_stop, |
| 1125 | .show = show_kprobe_addr |
| 1126 | }; |
| 1127 | |
| 1128 | static int __kprobes kprobes_open(struct inode *inode, struct file *filp) |
| 1129 | { |
| 1130 | return seq_open(filp, &kprobes_seq_ops); |
| 1131 | } |
| 1132 | |
| 1133 | static struct file_operations debugfs_kprobes_operations = { |
| 1134 | .open = kprobes_open, |
| 1135 | .read = seq_read, |
| 1136 | .llseek = seq_lseek, |
| 1137 | .release = seq_release, |
| 1138 | }; |
| 1139 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1140 | static void __kprobes enable_all_kprobes(void) |
| 1141 | { |
| 1142 | struct hlist_head *head; |
| 1143 | struct hlist_node *node; |
| 1144 | struct kprobe *p; |
| 1145 | unsigned int i; |
| 1146 | |
| 1147 | mutex_lock(&kprobe_mutex); |
| 1148 | |
| 1149 | /* If kprobes are already enabled, just return */ |
| 1150 | if (kprobe_enabled) |
| 1151 | goto already_enabled; |
| 1152 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1153 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 1154 | head = &kprobe_table[i]; |
| 1155 | hlist_for_each_entry_rcu(p, node, head, hlist) |
| 1156 | arch_arm_kprobe(p); |
| 1157 | } |
| 1158 | |
| 1159 | kprobe_enabled = true; |
| 1160 | printk(KERN_INFO "Kprobes globally enabled\n"); |
| 1161 | |
| 1162 | already_enabled: |
| 1163 | mutex_unlock(&kprobe_mutex); |
| 1164 | return; |
| 1165 | } |
| 1166 | |
| 1167 | static void __kprobes disable_all_kprobes(void) |
| 1168 | { |
| 1169 | struct hlist_head *head; |
| 1170 | struct hlist_node *node; |
| 1171 | struct kprobe *p; |
| 1172 | unsigned int i; |
| 1173 | |
| 1174 | mutex_lock(&kprobe_mutex); |
| 1175 | |
| 1176 | /* If kprobes are already disabled, just return */ |
| 1177 | if (!kprobe_enabled) |
| 1178 | goto already_disabled; |
| 1179 | |
| 1180 | kprobe_enabled = false; |
| 1181 | printk(KERN_INFO "Kprobes globally disabled\n"); |
| 1182 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 1183 | head = &kprobe_table[i]; |
| 1184 | hlist_for_each_entry_rcu(p, node, head, hlist) { |
| 1185 | if (!arch_trampoline_kprobe(p)) |
| 1186 | arch_disarm_kprobe(p); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | mutex_unlock(&kprobe_mutex); |
| 1191 | /* Allow all currently running kprobes to complete */ |
| 1192 | synchronize_sched(); |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 1193 | return; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1194 | |
| 1195 | already_disabled: |
| 1196 | mutex_unlock(&kprobe_mutex); |
| 1197 | return; |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * XXX: The debugfs bool file interface doesn't allow for callbacks |
| 1202 | * when the bool state is switched. We can reuse that facility when |
| 1203 | * available |
| 1204 | */ |
| 1205 | static ssize_t read_enabled_file_bool(struct file *file, |
| 1206 | char __user *user_buf, size_t count, loff_t *ppos) |
| 1207 | { |
| 1208 | char buf[3]; |
| 1209 | |
| 1210 | if (kprobe_enabled) |
| 1211 | buf[0] = '1'; |
| 1212 | else |
| 1213 | buf[0] = '0'; |
| 1214 | buf[1] = '\n'; |
| 1215 | buf[2] = 0x00; |
| 1216 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 1217 | } |
| 1218 | |
| 1219 | static ssize_t write_enabled_file_bool(struct file *file, |
| 1220 | const char __user *user_buf, size_t count, loff_t *ppos) |
| 1221 | { |
| 1222 | char buf[32]; |
| 1223 | int buf_size; |
| 1224 | |
| 1225 | buf_size = min(count, (sizeof(buf)-1)); |
| 1226 | if (copy_from_user(buf, user_buf, buf_size)) |
| 1227 | return -EFAULT; |
| 1228 | |
| 1229 | switch (buf[0]) { |
| 1230 | case 'y': |
| 1231 | case 'Y': |
| 1232 | case '1': |
| 1233 | enable_all_kprobes(); |
| 1234 | break; |
| 1235 | case 'n': |
| 1236 | case 'N': |
| 1237 | case '0': |
| 1238 | disable_all_kprobes(); |
| 1239 | break; |
| 1240 | } |
| 1241 | |
| 1242 | return count; |
| 1243 | } |
| 1244 | |
| 1245 | static struct file_operations fops_kp = { |
| 1246 | .read = read_enabled_file_bool, |
| 1247 | .write = write_enabled_file_bool, |
| 1248 | }; |
| 1249 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 1250 | static int __kprobes debugfs_kprobe_init(void) |
| 1251 | { |
| 1252 | struct dentry *dir, *file; |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1253 | unsigned int value = 1; |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 1254 | |
| 1255 | dir = debugfs_create_dir("kprobes", NULL); |
| 1256 | if (!dir) |
| 1257 | return -ENOMEM; |
| 1258 | |
Randy Dunlap | e386979 | 2007-05-08 00:27:01 -0700 | [diff] [blame] | 1259 | file = debugfs_create_file("list", 0444, dir, NULL, |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 1260 | &debugfs_kprobes_operations); |
| 1261 | if (!file) { |
| 1262 | debugfs_remove(dir); |
| 1263 | return -ENOMEM; |
| 1264 | } |
| 1265 | |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 1266 | file = debugfs_create_file("enabled", 0600, dir, |
| 1267 | &value, &fops_kp); |
| 1268 | if (!file) { |
| 1269 | debugfs_remove(dir); |
| 1270 | return -ENOMEM; |
| 1271 | } |
| 1272 | |
Srinivasa Ds | 346fd59 | 2007-02-20 13:57:54 -0800 | [diff] [blame] | 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | late_initcall(debugfs_kprobe_init); |
| 1277 | #endif /* CONFIG_DEBUG_FS */ |
| 1278 | |
| 1279 | module_init(init_kprobes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
| 1281 | EXPORT_SYMBOL_GPL(register_kprobe); |
| 1282 | EXPORT_SYMBOL_GPL(unregister_kprobe); |
Masami Hiramatsu | 9861668 | 2008-04-28 02:14:28 -0700 | [diff] [blame] | 1283 | EXPORT_SYMBOL_GPL(register_kprobes); |
| 1284 | EXPORT_SYMBOL_GPL(unregister_kprobes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | EXPORT_SYMBOL_GPL(register_jprobe); |
| 1286 | EXPORT_SYMBOL_GPL(unregister_jprobe); |
Masami Hiramatsu | 26b31c1 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 1287 | EXPORT_SYMBOL_GPL(register_jprobes); |
| 1288 | EXPORT_SYMBOL_GPL(unregister_jprobes); |
Peter Chubb | cd5bfea | 2007-08-10 13:01:10 -0700 | [diff] [blame] | 1289 | #ifdef CONFIG_KPROBES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | EXPORT_SYMBOL_GPL(jprobe_return); |
Peter Chubb | cd5bfea | 2007-08-10 13:01:10 -0700 | [diff] [blame] | 1291 | #endif |
| 1292 | |
| 1293 | #ifdef CONFIG_KPROBES |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 1294 | EXPORT_SYMBOL_GPL(register_kretprobe); |
| 1295 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |
Masami Hiramatsu | 4a296e0 | 2008-04-28 02:14:29 -0700 | [diff] [blame] | 1296 | EXPORT_SYMBOL_GPL(register_kretprobes); |
| 1297 | EXPORT_SYMBOL_GPL(unregister_kretprobes); |
Peter Chubb | cd5bfea | 2007-08-10 13:01:10 -0700 | [diff] [blame] | 1298 | #endif |