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