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