]>
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> |
e3869792 | 38 | #include <linux/stddef.h> |
1da177e4 | 39 | #include <linux/module.h> |
9ec4b1f3 | 40 | #include <linux/moduleloader.h> |
3a872d89 | 41 | #include <linux/kallsyms.h> |
b4c6c34a | 42 | #include <linux/freezer.h> |
346fd59b SD |
43 | #include <linux/seq_file.h> |
44 | #include <linux/debugfs.h> | |
1eeb66a1 | 45 | #include <linux/kdebug.h> |
d0aaff97 | 46 | #include <asm-generic/sections.h> |
1da177e4 LT |
47 | #include <asm/cacheflush.h> |
48 | #include <asm/errno.h> | |
1da177e4 LT |
49 | |
50 | #define KPROBE_HASH_BITS 6 | |
51 | #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) | |
52 | ||
3a872d89 AM |
53 | |
54 | /* | |
55 | * Some oddball architectures like 64bit powerpc have function descriptors | |
56 | * so this must be overridable. | |
57 | */ | |
58 | #ifndef kprobe_lookup_name | |
59 | #define kprobe_lookup_name(name, addr) \ | |
60 | addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name))) | |
61 | #endif | |
62 | ||
1da177e4 | 63 | static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; |
b94cce92 | 64 | static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE]; |
e6f47f97 | 65 | static atomic_t kprobe_count; |
1da177e4 | 66 | |
7a7d1cf9 | 67 | DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */ |
3516a460 | 68 | DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */ |
e6584523 | 69 | static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; |
1da177e4 | 70 | |
e6f47f97 AK |
71 | static struct notifier_block kprobe_page_fault_nb = { |
72 | .notifier_call = kprobe_exceptions_notify, | |
73 | .priority = 0x7fffffff /* we need to notified first */ | |
74 | }; | |
75 | ||
2d14e39d | 76 | #ifdef __ARCH_WANT_KPROBES_INSN_SLOT |
9ec4b1f3 AM |
77 | /* |
78 | * kprobe->ainsn.insn points to the copy of the instruction to be | |
79 | * single-stepped. x86_64, POWER4 and above have no-exec support and | |
80 | * stepping on the instruction on a vmalloced/kmalloced/data page | |
81 | * is a recipe for disaster | |
82 | */ | |
83 | #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t))) | |
84 | ||
85 | struct kprobe_insn_page { | |
86 | struct hlist_node hlist; | |
87 | kprobe_opcode_t *insns; /* Page of instruction slots */ | |
88 | char slot_used[INSNS_PER_PAGE]; | |
89 | int nused; | |
b4c6c34a | 90 | int ngarbage; |
9ec4b1f3 AM |
91 | }; |
92 | ||
ab40c5c6 MH |
93 | enum kprobe_slot_state { |
94 | SLOT_CLEAN = 0, | |
95 | SLOT_DIRTY = 1, | |
96 | SLOT_USED = 2, | |
97 | }; | |
98 | ||
9ec4b1f3 | 99 | static struct hlist_head kprobe_insn_pages; |
b4c6c34a MH |
100 | static int kprobe_garbage_slots; |
101 | static int collect_garbage_slots(void); | |
102 | ||
103 | static int __kprobes check_safety(void) | |
104 | { | |
105 | int ret = 0; | |
106 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM) | |
107 | ret = freeze_processes(); | |
108 | if (ret == 0) { | |
109 | struct task_struct *p, *q; | |
110 | do_each_thread(p, q) { | |
111 | if (p != current && p->state == TASK_RUNNING && | |
112 | p->pid != 0) { | |
113 | printk("Check failed: %s is running\n",p->comm); | |
114 | ret = -1; | |
115 | goto loop_end; | |
116 | } | |
117 | } while_each_thread(p, q); | |
118 | } | |
119 | loop_end: | |
120 | thaw_processes(); | |
121 | #else | |
122 | synchronize_sched(); | |
123 | #endif | |
124 | return ret; | |
125 | } | |
9ec4b1f3 AM |
126 | |
127 | /** | |
128 | * get_insn_slot() - Find a slot on an executable page for an instruction. | |
129 | * We allocate an executable page if there's no room on existing ones. | |
130 | */ | |
d0aaff97 | 131 | kprobe_opcode_t __kprobes *get_insn_slot(void) |
9ec4b1f3 AM |
132 | { |
133 | struct kprobe_insn_page *kip; | |
134 | struct hlist_node *pos; | |
135 | ||
6f716acd | 136 | retry: |
b0bb5016 | 137 | hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) { |
9ec4b1f3 AM |
138 | if (kip->nused < INSNS_PER_PAGE) { |
139 | int i; | |
140 | for (i = 0; i < INSNS_PER_PAGE; i++) { | |
ab40c5c6 MH |
141 | if (kip->slot_used[i] == SLOT_CLEAN) { |
142 | kip->slot_used[i] = SLOT_USED; | |
9ec4b1f3 AM |
143 | kip->nused++; |
144 | return kip->insns + (i * MAX_INSN_SIZE); | |
145 | } | |
146 | } | |
147 | /* Surprise! No unused slots. Fix kip->nused. */ | |
148 | kip->nused = INSNS_PER_PAGE; | |
149 | } | |
150 | } | |
151 | ||
b4c6c34a MH |
152 | /* If there are any garbage slots, collect it and try again. */ |
153 | if (kprobe_garbage_slots && collect_garbage_slots() == 0) { | |
154 | goto retry; | |
155 | } | |
156 | /* All out of space. Need to allocate a new page. Use slot 0. */ | |
9ec4b1f3 | 157 | kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL); |
6f716acd | 158 | if (!kip) |
9ec4b1f3 | 159 | return NULL; |
9ec4b1f3 AM |
160 | |
161 | /* | |
162 | * Use module_alloc so this page is within +/- 2GB of where the | |
163 | * kernel image and loaded module images reside. This is required | |
164 | * so x86_64 can correctly handle the %rip-relative fixups. | |
165 | */ | |
166 | kip->insns = module_alloc(PAGE_SIZE); | |
167 | if (!kip->insns) { | |
168 | kfree(kip); | |
169 | return NULL; | |
170 | } | |
171 | INIT_HLIST_NODE(&kip->hlist); | |
172 | hlist_add_head(&kip->hlist, &kprobe_insn_pages); | |
ab40c5c6 MH |
173 | memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE); |
174 | kip->slot_used[0] = SLOT_USED; | |
9ec4b1f3 | 175 | kip->nused = 1; |
b4c6c34a | 176 | kip->ngarbage = 0; |
9ec4b1f3 AM |
177 | return kip->insns; |
178 | } | |
179 | ||
b4c6c34a MH |
180 | /* Return 1 if all garbages are collected, otherwise 0. */ |
181 | static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx) | |
182 | { | |
ab40c5c6 | 183 | kip->slot_used[idx] = SLOT_CLEAN; |
b4c6c34a MH |
184 | kip->nused--; |
185 | if (kip->nused == 0) { | |
186 | /* | |
187 | * Page is no longer in use. Free it unless | |
188 | * it's the last one. We keep the last one | |
189 | * so as not to have to set it up again the | |
190 | * next time somebody inserts a probe. | |
191 | */ | |
192 | hlist_del(&kip->hlist); | |
193 | if (hlist_empty(&kprobe_insn_pages)) { | |
194 | INIT_HLIST_NODE(&kip->hlist); | |
195 | hlist_add_head(&kip->hlist, | |
196 | &kprobe_insn_pages); | |
197 | } else { | |
198 | module_free(NULL, kip->insns); | |
199 | kfree(kip); | |
200 | } | |
201 | return 1; | |
202 | } | |
203 | return 0; | |
204 | } | |
205 | ||
206 | static int __kprobes collect_garbage_slots(void) | |
207 | { | |
208 | struct kprobe_insn_page *kip; | |
209 | struct hlist_node *pos, *next; | |
210 | ||
211 | /* Ensure no-one is preepmted on the garbages */ | |
212 | if (check_safety() != 0) | |
213 | return -EAGAIN; | |
214 | ||
b0bb5016 | 215 | hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) { |
b4c6c34a | 216 | int i; |
b4c6c34a MH |
217 | if (kip->ngarbage == 0) |
218 | continue; | |
219 | kip->ngarbage = 0; /* we will collect all garbages */ | |
220 | for (i = 0; i < INSNS_PER_PAGE; i++) { | |
ab40c5c6 | 221 | if (kip->slot_used[i] == SLOT_DIRTY && |
b4c6c34a MH |
222 | collect_one_slot(kip, i)) |
223 | break; | |
224 | } | |
225 | } | |
226 | kprobe_garbage_slots = 0; | |
227 | return 0; | |
228 | } | |
229 | ||
230 | void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty) | |
9ec4b1f3 AM |
231 | { |
232 | struct kprobe_insn_page *kip; | |
233 | struct hlist_node *pos; | |
234 | ||
b0bb5016 | 235 | hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) { |
9ec4b1f3 AM |
236 | if (kip->insns <= slot && |
237 | slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) { | |
238 | int i = (slot - kip->insns) / MAX_INSN_SIZE; | |
b4c6c34a | 239 | if (dirty) { |
ab40c5c6 | 240 | kip->slot_used[i] = SLOT_DIRTY; |
b4c6c34a MH |
241 | kip->ngarbage++; |
242 | } else { | |
243 | collect_one_slot(kip, i); | |
9ec4b1f3 | 244 | } |
b4c6c34a | 245 | break; |
9ec4b1f3 AM |
246 | } |
247 | } | |
6f716acd CH |
248 | |
249 | if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE) | |
b4c6c34a | 250 | collect_garbage_slots(); |
9ec4b1f3 | 251 | } |
2d14e39d | 252 | #endif |
9ec4b1f3 | 253 | |
e6584523 AM |
254 | /* We have preemption disabled.. so it is safe to use __ versions */ |
255 | static inline void set_kprobe_instance(struct kprobe *kp) | |
256 | { | |
257 | __get_cpu_var(kprobe_instance) = kp; | |
258 | } | |
259 | ||
260 | static inline void reset_kprobe_instance(void) | |
261 | { | |
262 | __get_cpu_var(kprobe_instance) = NULL; | |
263 | } | |
264 | ||
3516a460 AM |
265 | /* |
266 | * This routine is called either: | |
49a2a1b8 | 267 | * - under the kprobe_mutex - during kprobe_[un]register() |
3516a460 | 268 | * OR |
d217d545 | 269 | * - with preemption disabled - from arch/xxx/kernel/kprobes.c |
3516a460 | 270 | */ |
d0aaff97 | 271 | struct kprobe __kprobes *get_kprobe(void *addr) |
1da177e4 LT |
272 | { |
273 | struct hlist_head *head; | |
274 | struct hlist_node *node; | |
3516a460 | 275 | struct kprobe *p; |
1da177e4 LT |
276 | |
277 | head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; | |
3516a460 | 278 | hlist_for_each_entry_rcu(p, node, head, hlist) { |
1da177e4 LT |
279 | if (p->addr == addr) |
280 | return p; | |
281 | } | |
282 | return NULL; | |
283 | } | |
284 | ||
64f562c6 AM |
285 | /* |
286 | * Aggregate handlers for multiple kprobes support - these handlers | |
287 | * take care of invoking the individual kprobe handlers on p->list | |
288 | */ | |
d0aaff97 | 289 | static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs) |
64f562c6 AM |
290 | { |
291 | struct kprobe *kp; | |
292 | ||
3516a460 | 293 | list_for_each_entry_rcu(kp, &p->list, list) { |
64f562c6 | 294 | if (kp->pre_handler) { |
e6584523 | 295 | set_kprobe_instance(kp); |
8b0914ea PP |
296 | if (kp->pre_handler(kp, regs)) |
297 | return 1; | |
64f562c6 | 298 | } |
e6584523 | 299 | reset_kprobe_instance(); |
64f562c6 AM |
300 | } |
301 | return 0; | |
302 | } | |
303 | ||
d0aaff97 PP |
304 | static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs, |
305 | unsigned long flags) | |
64f562c6 AM |
306 | { |
307 | struct kprobe *kp; | |
308 | ||
3516a460 | 309 | list_for_each_entry_rcu(kp, &p->list, list) { |
64f562c6 | 310 | if (kp->post_handler) { |
e6584523 | 311 | set_kprobe_instance(kp); |
64f562c6 | 312 | kp->post_handler(kp, regs, flags); |
e6584523 | 313 | reset_kprobe_instance(); |
64f562c6 AM |
314 | } |
315 | } | |
64f562c6 AM |
316 | } |
317 | ||
d0aaff97 PP |
318 | static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, |
319 | int trapnr) | |
64f562c6 | 320 | { |
e6584523 AM |
321 | struct kprobe *cur = __get_cpu_var(kprobe_instance); |
322 | ||
64f562c6 AM |
323 | /* |
324 | * if we faulted "during" the execution of a user specified | |
325 | * probe handler, invoke just that probe's fault handler | |
326 | */ | |
e6584523 AM |
327 | if (cur && cur->fault_handler) { |
328 | if (cur->fault_handler(cur, regs, trapnr)) | |
64f562c6 AM |
329 | return 1; |
330 | } | |
331 | return 0; | |
332 | } | |
333 | ||
d0aaff97 | 334 | static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs) |
8b0914ea | 335 | { |
e6584523 AM |
336 | struct kprobe *cur = __get_cpu_var(kprobe_instance); |
337 | int ret = 0; | |
338 | ||
339 | if (cur && cur->break_handler) { | |
340 | if (cur->break_handler(cur, regs)) | |
341 | ret = 1; | |
8b0914ea | 342 | } |
e6584523 AM |
343 | reset_kprobe_instance(); |
344 | return ret; | |
8b0914ea PP |
345 | } |
346 | ||
bf8d5c52 KA |
347 | /* Walks the list and increments nmissed count for multiprobe case */ |
348 | void __kprobes kprobes_inc_nmissed_count(struct kprobe *p) | |
349 | { | |
350 | struct kprobe *kp; | |
351 | if (p->pre_handler != aggr_pre_handler) { | |
352 | p->nmissed++; | |
353 | } else { | |
354 | list_for_each_entry_rcu(kp, &p->list, list) | |
355 | kp->nmissed++; | |
356 | } | |
357 | return; | |
358 | } | |
359 | ||
3516a460 | 360 | /* Called with kretprobe_lock held */ |
d0aaff97 | 361 | struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp) |
b94cce92 HN |
362 | { |
363 | struct hlist_node *node; | |
364 | struct kretprobe_instance *ri; | |
365 | hlist_for_each_entry(ri, node, &rp->free_instances, uflist) | |
366 | return ri; | |
367 | return NULL; | |
368 | } | |
369 | ||
3516a460 | 370 | /* Called with kretprobe_lock held */ |
d0aaff97 PP |
371 | static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe |
372 | *rp) | |
b94cce92 HN |
373 | { |
374 | struct hlist_node *node; | |
375 | struct kretprobe_instance *ri; | |
376 | hlist_for_each_entry(ri, node, &rp->used_instances, uflist) | |
377 | return ri; | |
378 | return NULL; | |
379 | } | |
380 | ||
3516a460 | 381 | /* Called with kretprobe_lock held */ |
d0aaff97 | 382 | void __kprobes add_rp_inst(struct kretprobe_instance *ri) |
b94cce92 | 383 | { |
b94cce92 HN |
384 | /* |
385 | * Remove rp inst off the free list - | |
386 | * Add it back when probed function returns | |
387 | */ | |
388 | hlist_del(&ri->uflist); | |
802eae7c | 389 | |
b94cce92 HN |
390 | /* Add rp inst onto table */ |
391 | INIT_HLIST_NODE(&ri->hlist); | |
392 | hlist_add_head(&ri->hlist, | |
802eae7c | 393 | &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]); |
b94cce92 HN |
394 | |
395 | /* Also add this rp inst to the used list. */ | |
396 | INIT_HLIST_NODE(&ri->uflist); | |
397 | hlist_add_head(&ri->uflist, &ri->rp->used_instances); | |
398 | } | |
399 | ||
3516a460 | 400 | /* Called with kretprobe_lock held */ |
99219a3f | 401 | void __kprobes recycle_rp_inst(struct kretprobe_instance *ri, |
402 | struct hlist_head *head) | |
b94cce92 HN |
403 | { |
404 | /* remove rp inst off the rprobe_inst_table */ | |
405 | hlist_del(&ri->hlist); | |
406 | if (ri->rp) { | |
407 | /* remove rp inst off the used list */ | |
408 | hlist_del(&ri->uflist); | |
409 | /* put rp inst back onto the free list */ | |
410 | INIT_HLIST_NODE(&ri->uflist); | |
411 | hlist_add_head(&ri->uflist, &ri->rp->free_instances); | |
412 | } else | |
413 | /* Unregistering */ | |
99219a3f | 414 | hlist_add_head(&ri->hlist, head); |
b94cce92 HN |
415 | } |
416 | ||
d0aaff97 | 417 | struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk) |
b94cce92 HN |
418 | { |
419 | return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)]; | |
420 | } | |
421 | ||
b94cce92 | 422 | /* |
c6fd91f0 | 423 | * This function is called from finish_task_switch when task tk becomes dead, |
424 | * so that we can recycle any function-return probe instances associated | |
425 | * with this task. These left over instances represent probed functions | |
426 | * that have been called but will never return. | |
b94cce92 | 427 | */ |
d0aaff97 | 428 | void __kprobes kprobe_flush_task(struct task_struct *tk) |
b94cce92 | 429 | { |
62c27be0 | 430 | struct kretprobe_instance *ri; |
99219a3f | 431 | struct hlist_head *head, empty_rp; |
802eae7c | 432 | struct hlist_node *node, *tmp; |
0aa55e4d | 433 | unsigned long flags = 0; |
802eae7c | 434 | |
99219a3f | 435 | INIT_HLIST_HEAD(&empty_rp); |
3516a460 | 436 | spin_lock_irqsave(&kretprobe_lock, flags); |
62c27be0 | 437 | head = kretprobe_inst_table_head(tk); |
438 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
439 | if (ri->task == tk) | |
99219a3f | 440 | recycle_rp_inst(ri, &empty_rp); |
62c27be0 | 441 | } |
3516a460 | 442 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
99219a3f | 443 | |
444 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { | |
445 | hlist_del(&ri->hlist); | |
446 | kfree(ri); | |
447 | } | |
b94cce92 HN |
448 | } |
449 | ||
b94cce92 HN |
450 | static inline void free_rp_inst(struct kretprobe *rp) |
451 | { | |
452 | struct kretprobe_instance *ri; | |
453 | while ((ri = get_free_rp_inst(rp)) != NULL) { | |
454 | hlist_del(&ri->uflist); | |
455 | kfree(ri); | |
456 | } | |
457 | } | |
458 | ||
8b0914ea PP |
459 | /* |
460 | * Keep all fields in the kprobe consistent | |
461 | */ | |
462 | static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p) | |
463 | { | |
464 | memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t)); | |
465 | memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn)); | |
466 | } | |
467 | ||
468 | /* | |
469 | * Add the new probe to old_p->list. Fail if this is the | |
470 | * second jprobe at the address - two jprobes can't coexist | |
471 | */ | |
d0aaff97 | 472 | static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p) |
8b0914ea | 473 | { |
8b0914ea | 474 | if (p->break_handler) { |
36721656 | 475 | if (old_p->break_handler) |
476 | return -EEXIST; | |
3516a460 | 477 | list_add_tail_rcu(&p->list, &old_p->list); |
36721656 | 478 | old_p->break_handler = aggr_break_handler; |
8b0914ea | 479 | } else |
3516a460 | 480 | list_add_rcu(&p->list, &old_p->list); |
36721656 | 481 | if (p->post_handler && !old_p->post_handler) |
482 | old_p->post_handler = aggr_post_handler; | |
8b0914ea PP |
483 | return 0; |
484 | } | |
485 | ||
64f562c6 AM |
486 | /* |
487 | * Fill in the required fields of the "manager kprobe". Replace the | |
488 | * earlier kprobe in the hlist with the manager kprobe | |
489 | */ | |
490 | static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p) | |
491 | { | |
8b0914ea | 492 | copy_kprobe(p, ap); |
a9ad965e | 493 | flush_insn_slot(ap); |
64f562c6 | 494 | ap->addr = p->addr; |
64f562c6 | 495 | ap->pre_handler = aggr_pre_handler; |
64f562c6 | 496 | ap->fault_handler = aggr_fault_handler; |
36721656 | 497 | if (p->post_handler) |
498 | ap->post_handler = aggr_post_handler; | |
499 | if (p->break_handler) | |
500 | ap->break_handler = aggr_break_handler; | |
64f562c6 AM |
501 | |
502 | INIT_LIST_HEAD(&ap->list); | |
3516a460 | 503 | list_add_rcu(&p->list, &ap->list); |
64f562c6 | 504 | |
adad0f33 | 505 | hlist_replace_rcu(&p->hlist, &ap->hlist); |
64f562c6 AM |
506 | } |
507 | ||
508 | /* | |
509 | * This is the second or subsequent kprobe at the address - handle | |
510 | * the intricacies | |
64f562c6 | 511 | */ |
d0aaff97 PP |
512 | static int __kprobes register_aggr_kprobe(struct kprobe *old_p, |
513 | struct kprobe *p) | |
64f562c6 AM |
514 | { |
515 | int ret = 0; | |
516 | struct kprobe *ap; | |
517 | ||
8b0914ea PP |
518 | if (old_p->pre_handler == aggr_pre_handler) { |
519 | copy_kprobe(old_p, p); | |
520 | ret = add_new_kprobe(old_p, p); | |
64f562c6 | 521 | } else { |
a0d50069 | 522 | ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL); |
64f562c6 AM |
523 | if (!ap) |
524 | return -ENOMEM; | |
525 | add_aggr_kprobe(ap, old_p); | |
8b0914ea PP |
526 | copy_kprobe(ap, p); |
527 | ret = add_new_kprobe(ap, p); | |
64f562c6 AM |
528 | } |
529 | return ret; | |
530 | } | |
531 | ||
d0aaff97 PP |
532 | static int __kprobes in_kprobes_functions(unsigned long addr) |
533 | { | |
6f716acd CH |
534 | if (addr >= (unsigned long)__kprobes_text_start && |
535 | addr < (unsigned long)__kprobes_text_end) | |
d0aaff97 PP |
536 | return -EINVAL; |
537 | return 0; | |
538 | } | |
539 | ||
df019b1d KA |
540 | static int __kprobes __register_kprobe(struct kprobe *p, |
541 | unsigned long called_from) | |
1da177e4 LT |
542 | { |
543 | int ret = 0; | |
64f562c6 | 544 | struct kprobe *old_p; |
df019b1d | 545 | struct module *probed_mod; |
b3e55c72 | 546 | |
3a872d89 AM |
547 | /* |
548 | * If we have a symbol_name argument look it up, | |
549 | * and add it to the address. That way the addr | |
550 | * field can either be global or relative to a symbol. | |
551 | */ | |
552 | if (p->symbol_name) { | |
553 | if (p->addr) | |
554 | return -EINVAL; | |
555 | kprobe_lookup_name(p->symbol_name, p->addr); | |
556 | } | |
557 | ||
558 | if (!p->addr) | |
559 | return -EINVAL; | |
560 | p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset); | |
561 | ||
6f716acd CH |
562 | if (!kernel_text_address((unsigned long) p->addr) || |
563 | in_kprobes_functions((unsigned long) p->addr)) | |
b3e55c72 MB |
564 | return -EINVAL; |
565 | ||
df019b1d | 566 | p->mod_refcounted = 0; |
6f716acd CH |
567 | |
568 | /* | |
569 | * Check if are we probing a module. | |
570 | */ | |
571 | probed_mod = module_text_address((unsigned long) p->addr); | |
572 | if (probed_mod) { | |
df019b1d | 573 | struct module *calling_mod = module_text_address(called_from); |
6f716acd CH |
574 | /* |
575 | * We must allow modules to probe themself and in this case | |
576 | * avoid incrementing the module refcount, so as to allow | |
577 | * unloading of self probing modules. | |
df019b1d | 578 | */ |
6f716acd | 579 | if (calling_mod && calling_mod != probed_mod) { |
df019b1d KA |
580 | if (unlikely(!try_module_get(probed_mod))) |
581 | return -EINVAL; | |
582 | p->mod_refcounted = 1; | |
583 | } else | |
584 | probed_mod = NULL; | |
585 | } | |
1da177e4 | 586 | |
3516a460 | 587 | p->nmissed = 0; |
7a7d1cf9 | 588 | mutex_lock(&kprobe_mutex); |
64f562c6 AM |
589 | old_p = get_kprobe(p->addr); |
590 | if (old_p) { | |
591 | ret = register_aggr_kprobe(old_p, p); | |
e6f47f97 AK |
592 | if (!ret) |
593 | atomic_inc(&kprobe_count); | |
1da177e4 LT |
594 | goto out; |
595 | } | |
1da177e4 | 596 | |
6f716acd CH |
597 | ret = arch_prepare_kprobe(p); |
598 | if (ret) | |
49a2a1b8 AK |
599 | goto out; |
600 | ||
64f562c6 | 601 | INIT_HLIST_NODE(&p->hlist); |
3516a460 | 602 | hlist_add_head_rcu(&p->hlist, |
1da177e4 LT |
603 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); |
604 | ||
e6f47f97 AK |
605 | if (atomic_add_return(1, &kprobe_count) == \ |
606 | (ARCH_INACTIVE_KPROBE_COUNT + 1)) | |
607 | register_page_fault_notifier(&kprobe_page_fault_nb); | |
608 | ||
62c27be0 | 609 | arch_arm_kprobe(p); |
7e1048b1 | 610 | |
1da177e4 | 611 | out: |
7a7d1cf9 | 612 | mutex_unlock(&kprobe_mutex); |
49a2a1b8 | 613 | |
df019b1d KA |
614 | if (ret && probed_mod) |
615 | module_put(probed_mod); | |
1da177e4 LT |
616 | return ret; |
617 | } | |
618 | ||
df019b1d KA |
619 | int __kprobes register_kprobe(struct kprobe *p) |
620 | { | |
6f716acd | 621 | return __register_kprobe(p, (unsigned long)__builtin_return_address(0)); |
df019b1d KA |
622 | } |
623 | ||
d0aaff97 | 624 | void __kprobes unregister_kprobe(struct kprobe *p) |
1da177e4 | 625 | { |
b3e55c72 | 626 | struct module *mod; |
f709b122 KA |
627 | struct kprobe *old_p, *list_p; |
628 | int cleanup_p; | |
64f562c6 | 629 | |
7a7d1cf9 | 630 | mutex_lock(&kprobe_mutex); |
64f562c6 | 631 | old_p = get_kprobe(p->addr); |
49a2a1b8 | 632 | if (unlikely(!old_p)) { |
7a7d1cf9 | 633 | mutex_unlock(&kprobe_mutex); |
49a2a1b8 AK |
634 | return; |
635 | } | |
f709b122 KA |
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; | |
7a7d1cf9 | 641 | mutex_unlock(&kprobe_mutex); |
f709b122 KA |
642 | return; |
643 | } | |
644 | valid_p: | |
6f716acd CH |
645 | if (old_p == p || |
646 | (old_p->pre_handler == aggr_pre_handler && | |
647 | p->list.next == &old_p->list && p->list.prev == &old_p->list)) { | |
f709b122 | 648 | /* Only probe on the hash list */ |
49a2a1b8 AK |
649 | arch_disarm_kprobe(p); |
650 | hlist_del_rcu(&old_p->hlist); | |
f709b122 | 651 | cleanup_p = 1; |
49a2a1b8 AK |
652 | } else { |
653 | list_del_rcu(&p->list); | |
f709b122 | 654 | cleanup_p = 0; |
49a2a1b8 | 655 | } |
3516a460 | 656 | |
7a7d1cf9 | 657 | mutex_unlock(&kprobe_mutex); |
b3e55c72 | 658 | |
49a2a1b8 | 659 | synchronize_sched(); |
6f716acd CH |
660 | if (p->mod_refcounted) { |
661 | mod = module_text_address((unsigned long)p->addr); | |
662 | if (mod) | |
663 | module_put(mod); | |
664 | } | |
b3e55c72 | 665 | |
49a2a1b8 | 666 | if (cleanup_p) { |
f709b122 | 667 | if (p != old_p) { |
49a2a1b8 | 668 | list_del_rcu(&p->list); |
3516a460 | 669 | kfree(old_p); |
49a2a1b8 | 670 | } |
0498b635 | 671 | arch_remove_kprobe(p); |
36721656 | 672 | } else { |
673 | mutex_lock(&kprobe_mutex); | |
674 | if (p->break_handler) | |
675 | old_p->break_handler = NULL; | |
676 | if (p->post_handler){ | |
677 | list_for_each_entry_rcu(list_p, &old_p->list, list){ | |
678 | if (list_p->post_handler){ | |
679 | cleanup_p = 2; | |
680 | break; | |
681 | } | |
682 | } | |
683 | if (cleanup_p == 0) | |
684 | old_p->post_handler = NULL; | |
685 | } | |
686 | mutex_unlock(&kprobe_mutex); | |
49a2a1b8 | 687 | } |
e6f47f97 AK |
688 | |
689 | /* Call unregister_page_fault_notifier() | |
690 | * if no probes are active | |
691 | */ | |
692 | mutex_lock(&kprobe_mutex); | |
693 | if (atomic_add_return(-1, &kprobe_count) == \ | |
694 | ARCH_INACTIVE_KPROBE_COUNT) | |
695 | unregister_page_fault_notifier(&kprobe_page_fault_nb); | |
696 | mutex_unlock(&kprobe_mutex); | |
697 | return; | |
1da177e4 LT |
698 | } |
699 | ||
700 | static struct notifier_block kprobe_exceptions_nb = { | |
3d5631e0 AK |
701 | .notifier_call = kprobe_exceptions_notify, |
702 | .priority = 0x7fffffff /* we need to be notified first */ | |
703 | }; | |
704 | ||
1da177e4 | 705 | |
d0aaff97 | 706 | int __kprobes register_jprobe(struct jprobe *jp) |
1da177e4 LT |
707 | { |
708 | /* Todo: Verify probepoint is a function entry point */ | |
709 | jp->kp.pre_handler = setjmp_pre_handler; | |
710 | jp->kp.break_handler = longjmp_break_handler; | |
711 | ||
df019b1d KA |
712 | return __register_kprobe(&jp->kp, |
713 | (unsigned long)__builtin_return_address(0)); | |
1da177e4 LT |
714 | } |
715 | ||
d0aaff97 | 716 | void __kprobes unregister_jprobe(struct jprobe *jp) |
1da177e4 LT |
717 | { |
718 | unregister_kprobe(&jp->kp); | |
719 | } | |
720 | ||
b94cce92 HN |
721 | #ifdef ARCH_SUPPORTS_KRETPROBES |
722 | ||
e65cefe8 AB |
723 | /* |
724 | * This kprobe pre_handler is registered with every kretprobe. When probe | |
725 | * hits it will set up the return probe. | |
726 | */ | |
727 | static int __kprobes pre_handler_kretprobe(struct kprobe *p, | |
728 | struct pt_regs *regs) | |
729 | { | |
730 | struct kretprobe *rp = container_of(p, struct kretprobe, kp); | |
731 | unsigned long flags = 0; | |
732 | ||
733 | /*TODO: consider to only swap the RA after the last pre_handler fired */ | |
734 | spin_lock_irqsave(&kretprobe_lock, flags); | |
735 | arch_prepare_kretprobe(rp, regs); | |
736 | spin_unlock_irqrestore(&kretprobe_lock, flags); | |
737 | return 0; | |
738 | } | |
739 | ||
d0aaff97 | 740 | int __kprobes register_kretprobe(struct kretprobe *rp) |
b94cce92 HN |
741 | { |
742 | int ret = 0; | |
743 | struct kretprobe_instance *inst; | |
744 | int i; | |
745 | ||
746 | rp->kp.pre_handler = pre_handler_kretprobe; | |
7522a842 AM |
747 | rp->kp.post_handler = NULL; |
748 | rp->kp.fault_handler = NULL; | |
749 | rp->kp.break_handler = NULL; | |
b94cce92 HN |
750 | |
751 | /* Pre-allocate memory for max kretprobe instances */ | |
752 | if (rp->maxactive <= 0) { | |
753 | #ifdef CONFIG_PREEMPT | |
754 | rp->maxactive = max(10, 2 * NR_CPUS); | |
755 | #else | |
756 | rp->maxactive = NR_CPUS; | |
757 | #endif | |
758 | } | |
759 | INIT_HLIST_HEAD(&rp->used_instances); | |
760 | INIT_HLIST_HEAD(&rp->free_instances); | |
761 | for (i = 0; i < rp->maxactive; i++) { | |
762 | inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL); | |
763 | if (inst == NULL) { | |
764 | free_rp_inst(rp); | |
765 | return -ENOMEM; | |
766 | } | |
767 | INIT_HLIST_NODE(&inst->uflist); | |
768 | hlist_add_head(&inst->uflist, &rp->free_instances); | |
769 | } | |
770 | ||
771 | rp->nmissed = 0; | |
772 | /* Establish function entry probe point */ | |
df019b1d KA |
773 | if ((ret = __register_kprobe(&rp->kp, |
774 | (unsigned long)__builtin_return_address(0))) != 0) | |
b94cce92 HN |
775 | free_rp_inst(rp); |
776 | return ret; | |
777 | } | |
778 | ||
779 | #else /* ARCH_SUPPORTS_KRETPROBES */ | |
780 | ||
d0aaff97 | 781 | int __kprobes register_kretprobe(struct kretprobe *rp) |
b94cce92 HN |
782 | { |
783 | return -ENOSYS; | |
784 | } | |
785 | ||
346fd59b SD |
786 | static int __kprobes pre_handler_kretprobe(struct kprobe *p, |
787 | struct pt_regs *regs) | |
788 | { | |
789 | return 0; | |
790 | } | |
791 | ||
b94cce92 HN |
792 | #endif /* ARCH_SUPPORTS_KRETPROBES */ |
793 | ||
d0aaff97 | 794 | void __kprobes unregister_kretprobe(struct kretprobe *rp) |
b94cce92 HN |
795 | { |
796 | unsigned long flags; | |
797 | struct kretprobe_instance *ri; | |
798 | ||
799 | unregister_kprobe(&rp->kp); | |
800 | /* No race here */ | |
3516a460 | 801 | spin_lock_irqsave(&kretprobe_lock, flags); |
b94cce92 HN |
802 | while ((ri = get_used_rp_inst(rp)) != NULL) { |
803 | ri->rp = NULL; | |
804 | hlist_del(&ri->uflist); | |
805 | } | |
3516a460 | 806 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
278ff953 | 807 | free_rp_inst(rp); |
b94cce92 HN |
808 | } |
809 | ||
1da177e4 LT |
810 | static int __init init_kprobes(void) |
811 | { | |
812 | int i, err = 0; | |
813 | ||
814 | /* FIXME allocate the probe table, currently defined statically */ | |
815 | /* initialize all list heads */ | |
b94cce92 | 816 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
1da177e4 | 817 | INIT_HLIST_HEAD(&kprobe_table[i]); |
b94cce92 HN |
818 | INIT_HLIST_HEAD(&kretprobe_inst_table[i]); |
819 | } | |
e6f47f97 | 820 | atomic_set(&kprobe_count, 0); |
1da177e4 | 821 | |
6772926b | 822 | err = arch_init_kprobes(); |
802eae7c RL |
823 | if (!err) |
824 | err = register_die_notifier(&kprobe_exceptions_nb); | |
825 | ||
1da177e4 LT |
826 | return err; |
827 | } | |
828 | ||
346fd59b SD |
829 | #ifdef CONFIG_DEBUG_FS |
830 | static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p, | |
831 | const char *sym, int offset,char *modname) | |
832 | { | |
833 | char *kprobe_type; | |
834 | ||
835 | if (p->pre_handler == pre_handler_kretprobe) | |
836 | kprobe_type = "r"; | |
837 | else if (p->pre_handler == setjmp_pre_handler) | |
838 | kprobe_type = "j"; | |
839 | else | |
840 | kprobe_type = "k"; | |
841 | if (sym) | |
842 | seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type, | |
843 | sym, offset, (modname ? modname : " ")); | |
844 | else | |
845 | seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr); | |
846 | } | |
847 | ||
848 | static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos) | |
849 | { | |
850 | return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL; | |
851 | } | |
852 | ||
853 | static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos) | |
854 | { | |
855 | (*pos)++; | |
856 | if (*pos >= KPROBE_TABLE_SIZE) | |
857 | return NULL; | |
858 | return pos; | |
859 | } | |
860 | ||
861 | static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v) | |
862 | { | |
863 | /* Nothing to do */ | |
864 | } | |
865 | ||
866 | static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v) | |
867 | { | |
868 | struct hlist_head *head; | |
869 | struct hlist_node *node; | |
870 | struct kprobe *p, *kp; | |
871 | const char *sym = NULL; | |
872 | unsigned int i = *(loff_t *) v; | |
ffb45122 | 873 | unsigned long offset = 0; |
346fd59b SD |
874 | char *modname, namebuf[128]; |
875 | ||
876 | head = &kprobe_table[i]; | |
877 | preempt_disable(); | |
878 | hlist_for_each_entry_rcu(p, node, head, hlist) { | |
ffb45122 | 879 | sym = kallsyms_lookup((unsigned long)p->addr, NULL, |
346fd59b SD |
880 | &offset, &modname, namebuf); |
881 | if (p->pre_handler == aggr_pre_handler) { | |
882 | list_for_each_entry_rcu(kp, &p->list, list) | |
883 | report_probe(pi, kp, sym, offset, modname); | |
884 | } else | |
885 | report_probe(pi, p, sym, offset, modname); | |
886 | } | |
887 | preempt_enable(); | |
888 | return 0; | |
889 | } | |
890 | ||
891 | static struct seq_operations kprobes_seq_ops = { | |
892 | .start = kprobe_seq_start, | |
893 | .next = kprobe_seq_next, | |
894 | .stop = kprobe_seq_stop, | |
895 | .show = show_kprobe_addr | |
896 | }; | |
897 | ||
898 | static int __kprobes kprobes_open(struct inode *inode, struct file *filp) | |
899 | { | |
900 | return seq_open(filp, &kprobes_seq_ops); | |
901 | } | |
902 | ||
903 | static struct file_operations debugfs_kprobes_operations = { | |
904 | .open = kprobes_open, | |
905 | .read = seq_read, | |
906 | .llseek = seq_lseek, | |
907 | .release = seq_release, | |
908 | }; | |
909 | ||
910 | static int __kprobes debugfs_kprobe_init(void) | |
911 | { | |
912 | struct dentry *dir, *file; | |
913 | ||
914 | dir = debugfs_create_dir("kprobes", NULL); | |
915 | if (!dir) | |
916 | return -ENOMEM; | |
917 | ||
e3869792 | 918 | file = debugfs_create_file("list", 0444, dir, NULL, |
346fd59b SD |
919 | &debugfs_kprobes_operations); |
920 | if (!file) { | |
921 | debugfs_remove(dir); | |
922 | return -ENOMEM; | |
923 | } | |
924 | ||
925 | return 0; | |
926 | } | |
927 | ||
928 | late_initcall(debugfs_kprobe_init); | |
929 | #endif /* CONFIG_DEBUG_FS */ | |
930 | ||
931 | module_init(init_kprobes); | |
1da177e4 LT |
932 | |
933 | EXPORT_SYMBOL_GPL(register_kprobe); | |
934 | EXPORT_SYMBOL_GPL(unregister_kprobe); | |
935 | EXPORT_SYMBOL_GPL(register_jprobe); | |
936 | EXPORT_SYMBOL_GPL(unregister_jprobe); | |
937 | EXPORT_SYMBOL_GPL(jprobe_return); | |
b94cce92 HN |
938 | EXPORT_SYMBOL_GPL(register_kretprobe); |
939 | EXPORT_SYMBOL_GPL(unregister_kretprobe); |