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