[PATCH] Kprobes: Use RCU for (un)register synchronization - arch changes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / i386 / kernel / kprobes.c
blobad469299267a106061c8706244f7f2398897e478
1 /*
2 * Kernel Probes (KProbes)
3 * arch/i386/kernel/kprobes.c
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.
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.
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.
19 * Copyright (C) IBM Corporation, 2002, 2004
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
23 * Rusty Russell).
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
26 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
27 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
28 * <prasanna@in.ibm.com> added function-return probes.
31 #include <linux/config.h>
32 #include <linux/kprobes.h>
33 #include <linux/ptrace.h>
34 #include <linux/preempt.h>
35 #include <asm/cacheflush.h>
36 #include <asm/kdebug.h>
37 #include <asm/desc.h>
39 void jprobe_return_end(void);
41 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
42 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
45 * returns non-zero if opcode modifies the interrupt flag.
47 static inline int is_IF_modifier(kprobe_opcode_t opcode)
49 switch (opcode) {
50 case 0xfa: /* cli */
51 case 0xfb: /* sti */
52 case 0xcf: /* iret/iretd */
53 case 0x9d: /* popf/popfd */
54 return 1;
56 return 0;
59 int __kprobes arch_prepare_kprobe(struct kprobe *p)
61 return 0;
64 void __kprobes arch_copy_kprobe(struct kprobe *p)
66 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
67 p->opcode = *p->addr;
70 void __kprobes arch_arm_kprobe(struct kprobe *p)
72 *p->addr = BREAKPOINT_INSTRUCTION;
73 flush_icache_range((unsigned long) p->addr,
74 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
77 void __kprobes arch_disarm_kprobe(struct kprobe *p)
79 *p->addr = p->opcode;
80 flush_icache_range((unsigned long) p->addr,
81 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
84 void __kprobes arch_remove_kprobe(struct kprobe *p)
88 static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
90 kcb->prev_kprobe.kp = kprobe_running();
91 kcb->prev_kprobe.status = kcb->kprobe_status;
92 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
93 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
96 static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
98 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
99 kcb->kprobe_status = kcb->prev_kprobe.status;
100 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
101 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
104 static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
105 struct kprobe_ctlblk *kcb)
107 __get_cpu_var(current_kprobe) = p;
108 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
109 = (regs->eflags & (TF_MASK | IF_MASK));
110 if (is_IF_modifier(p->opcode))
111 kcb->kprobe_saved_eflags &= ~IF_MASK;
114 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
116 regs->eflags |= TF_MASK;
117 regs->eflags &= ~IF_MASK;
118 /*single step inline if the instruction is an int3*/
119 if (p->opcode == BREAKPOINT_INSTRUCTION)
120 regs->eip = (unsigned long)p->addr;
121 else
122 regs->eip = (unsigned long)&p->ainsn.insn;
125 /* Called with kretprobe_lock held */
126 void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
127 struct pt_regs *regs)
129 unsigned long *sara = (unsigned long *)&regs->esp;
130 struct kretprobe_instance *ri;
132 if ((ri = get_free_rp_inst(rp)) != NULL) {
133 ri->rp = rp;
134 ri->task = current;
135 ri->ret_addr = (kprobe_opcode_t *) *sara;
137 /* Replace the return addr with trampoline addr */
138 *sara = (unsigned long) &kretprobe_trampoline;
140 add_rp_inst(ri);
141 } else {
142 rp->nmissed++;
147 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
148 * remain disabled thorough out this function.
150 static int __kprobes kprobe_handler(struct pt_regs *regs)
152 struct kprobe *p;
153 int ret = 0;
154 kprobe_opcode_t *addr = NULL;
155 unsigned long *lp;
156 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
158 /* Check if the application is using LDT entry for its code segment and
159 * calculate the address by reading the base address from the LDT entry.
161 if ((regs->xcs & 4) && (current->mm)) {
162 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
163 + (char *) current->mm->context.ldt);
164 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
165 sizeof(kprobe_opcode_t));
166 } else {
167 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
169 /* Check we're not actually recursing */
170 if (kprobe_running()) {
171 p = get_kprobe(addr);
172 if (p) {
173 if (kcb->kprobe_status == KPROBE_HIT_SS &&
174 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
175 regs->eflags &= ~TF_MASK;
176 regs->eflags |= kcb->kprobe_saved_eflags;
177 goto no_kprobe;
179 /* We have reentered the kprobe_handler(), since
180 * another probe was hit while within the handler.
181 * We here save the original kprobes variables and
182 * just single step on the instruction of the new probe
183 * without calling any user handlers.
185 save_previous_kprobe(kcb);
186 set_current_kprobe(p, regs, kcb);
187 p->nmissed++;
188 prepare_singlestep(p, regs);
189 kcb->kprobe_status = KPROBE_REENTER;
190 return 1;
191 } else {
192 p = __get_cpu_var(current_kprobe);
193 if (p->break_handler && p->break_handler(p, regs)) {
194 goto ss_probe;
197 goto no_kprobe;
200 p = get_kprobe(addr);
201 if (!p) {
202 if (regs->eflags & VM_MASK) {
203 /* We are in virtual-8086 mode. Return 0 */
204 goto no_kprobe;
207 if (*addr != BREAKPOINT_INSTRUCTION) {
209 * The breakpoint instruction was removed right
210 * after we hit it. Another cpu has removed
211 * either a probepoint or a debugger breakpoint
212 * at this address. In either case, no further
213 * handling of this interrupt is appropriate.
214 * Back up over the (now missing) int3 and run
215 * the original instruction.
217 regs->eip -= sizeof(kprobe_opcode_t);
218 ret = 1;
220 /* Not one of ours: let kernel handle it */
221 goto no_kprobe;
225 * This preempt_disable() matches the preempt_enable_no_resched()
226 * in post_kprobe_handler()
228 preempt_disable();
229 set_current_kprobe(p, regs, kcb);
230 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
232 if (p->pre_handler && p->pre_handler(p, regs))
233 /* handler has already set things up, so skip ss setup */
234 return 1;
236 ss_probe:
237 prepare_singlestep(p, regs);
238 kcb->kprobe_status = KPROBE_HIT_SS;
239 return 1;
241 no_kprobe:
242 return ret;
246 * For function-return probes, init_kprobes() establishes a probepoint
247 * here. When a retprobed function returns, this probe is hit and
248 * trampoline_probe_handler() runs, calling the kretprobe's handler.
250 void kretprobe_trampoline_holder(void)
252 asm volatile ( ".global kretprobe_trampoline\n"
253 "kretprobe_trampoline: \n"
254 "nop\n");
258 * Called when we hit the probe point at kretprobe_trampoline
260 int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
262 struct kretprobe_instance *ri = NULL;
263 struct hlist_head *head;
264 struct hlist_node *node, *tmp;
265 unsigned long flags, orig_ret_address = 0;
266 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
268 spin_lock_irqsave(&kretprobe_lock, flags);
269 head = kretprobe_inst_table_head(current);
272 * It is possible to have multiple instances associated with a given
273 * task either because an multiple functions in the call path
274 * have a return probe installed on them, and/or more then one return
275 * return probe was registered for a target function.
277 * We can handle this because:
278 * - instances are always inserted at the head of the list
279 * - when multiple return probes are registered for the same
280 * function, the first instance's ret_addr will point to the
281 * real return address, and all the rest will point to
282 * kretprobe_trampoline
284 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
285 if (ri->task != current)
286 /* another task is sharing our hash bucket */
287 continue;
289 if (ri->rp && ri->rp->handler)
290 ri->rp->handler(ri, regs);
292 orig_ret_address = (unsigned long)ri->ret_addr;
293 recycle_rp_inst(ri);
295 if (orig_ret_address != trampoline_address)
297 * This is the real return address. Any other
298 * instances associated with this task are for
299 * other calls deeper on the call stack
301 break;
304 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
305 regs->eip = orig_ret_address;
307 reset_current_kprobe();
308 spin_unlock_irqrestore(&kretprobe_lock, flags);
309 preempt_enable_no_resched();
312 * By returning a non-zero value, we are telling
313 * kprobe_handler() that we have handled unlocking
314 * and re-enabling preemption
316 return 1;
320 * Called after single-stepping. p->addr is the address of the
321 * instruction whose first byte has been replaced by the "int 3"
322 * instruction. To avoid the SMP problems that can occur when we
323 * temporarily put back the original opcode to single-step, we
324 * single-stepped a copy of the instruction. The address of this
325 * copy is p->ainsn.insn.
327 * This function prepares to return from the post-single-step
328 * interrupt. We have to fix up the stack as follows:
330 * 0) Except in the case of absolute or indirect jump or call instructions,
331 * the new eip is relative to the copied instruction. We need to make
332 * it relative to the original instruction.
334 * 1) If the single-stepped instruction was pushfl, then the TF and IF
335 * flags are set in the just-pushed eflags, and may need to be cleared.
337 * 2) If the single-stepped instruction was a call, the return address
338 * that is atop the stack is the address following the copied instruction.
339 * We need to make it the address following the original instruction.
341 static void __kprobes resume_execution(struct kprobe *p,
342 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
344 unsigned long *tos = (unsigned long *)&regs->esp;
345 unsigned long next_eip = 0;
346 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
347 unsigned long orig_eip = (unsigned long)p->addr;
349 switch (p->ainsn.insn[0]) {
350 case 0x9c: /* pushfl */
351 *tos &= ~(TF_MASK | IF_MASK);
352 *tos |= kcb->kprobe_old_eflags;
353 break;
354 case 0xc3: /* ret/lret */
355 case 0xcb:
356 case 0xc2:
357 case 0xca:
358 regs->eflags &= ~TF_MASK;
359 /* eip is already adjusted, no more changes required*/
360 return;
361 case 0xe8: /* call relative - Fix return addr */
362 *tos = orig_eip + (*tos - copy_eip);
363 break;
364 case 0xff:
365 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
366 /* call absolute, indirect */
367 /* Fix return addr; eip is correct. */
368 next_eip = regs->eip;
369 *tos = orig_eip + (*tos - copy_eip);
370 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
371 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
372 /* eip is correct. */
373 next_eip = regs->eip;
375 break;
376 case 0xea: /* jmp absolute -- eip is correct */
377 next_eip = regs->eip;
378 break;
379 default:
380 break;
383 regs->eflags &= ~TF_MASK;
384 if (next_eip) {
385 regs->eip = next_eip;
386 } else {
387 regs->eip = orig_eip + (regs->eip - copy_eip);
392 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
393 * remain disabled thoroughout this function.
395 static inline int post_kprobe_handler(struct pt_regs *regs)
397 struct kprobe *cur = kprobe_running();
398 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
400 if (!cur)
401 return 0;
403 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
404 kcb->kprobe_status = KPROBE_HIT_SSDONE;
405 cur->post_handler(cur, regs, 0);
408 resume_execution(cur, regs, kcb);
409 regs->eflags |= kcb->kprobe_saved_eflags;
411 /*Restore back the original saved kprobes variables and continue. */
412 if (kcb->kprobe_status == KPROBE_REENTER) {
413 restore_previous_kprobe(kcb);
414 goto out;
416 reset_current_kprobe();
417 out:
418 preempt_enable_no_resched();
421 * if somebody else is singlestepping across a probe point, eflags
422 * will have TF set, in which case, continue the remaining processing
423 * of do_debug, as if this is not a probe hit.
425 if (regs->eflags & TF_MASK)
426 return 0;
428 return 1;
431 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
433 struct kprobe *cur = kprobe_running();
434 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
436 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
437 return 1;
439 if (kcb->kprobe_status & KPROBE_HIT_SS) {
440 resume_execution(cur, regs, kcb);
441 regs->eflags |= kcb->kprobe_old_eflags;
443 reset_current_kprobe();
444 preempt_enable_no_resched();
446 return 0;
450 * Wrapper routine to for handling exceptions.
452 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
453 unsigned long val, void *data)
455 struct die_args *args = (struct die_args *)data;
456 int ret = NOTIFY_DONE;
458 rcu_read_lock();
459 switch (val) {
460 case DIE_INT3:
461 if (kprobe_handler(args->regs))
462 ret = NOTIFY_STOP;
463 break;
464 case DIE_DEBUG:
465 if (post_kprobe_handler(args->regs))
466 ret = NOTIFY_STOP;
467 break;
468 case DIE_GPF:
469 case DIE_PAGE_FAULT:
470 if (kprobe_running() &&
471 kprobe_fault_handler(args->regs, args->trapnr))
472 ret = NOTIFY_STOP;
473 break;
474 default:
475 break;
477 rcu_read_unlock();
478 return ret;
481 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
483 struct jprobe *jp = container_of(p, struct jprobe, kp);
484 unsigned long addr;
485 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
487 kcb->jprobe_saved_regs = *regs;
488 kcb->jprobe_saved_esp = &regs->esp;
489 addr = (unsigned long)(kcb->jprobe_saved_esp);
492 * TBD: As Linus pointed out, gcc assumes that the callee
493 * owns the argument space and could overwrite it, e.g.
494 * tailcall optimization. So, to be absolutely safe
495 * we also save and restore enough stack bytes to cover
496 * the argument area.
498 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
499 MIN_STACK_SIZE(addr));
500 regs->eflags &= ~IF_MASK;
501 regs->eip = (unsigned long)(jp->entry);
502 return 1;
505 void __kprobes jprobe_return(void)
507 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
509 asm volatile (" xchgl %%ebx,%%esp \n"
510 " int3 \n"
511 " .globl jprobe_return_end \n"
512 " jprobe_return_end: \n"
513 " nop \n"::"b"
514 (kcb->jprobe_saved_esp):"memory");
517 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
519 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
520 u8 *addr = (u8 *) (regs->eip - 1);
521 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
522 struct jprobe *jp = container_of(p, struct jprobe, kp);
524 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
525 if (&regs->esp != kcb->jprobe_saved_esp) {
526 struct pt_regs *saved_regs =
527 container_of(kcb->jprobe_saved_esp,
528 struct pt_regs, esp);
529 printk("current esp %p does not match saved esp %p\n",
530 &regs->esp, kcb->jprobe_saved_esp);
531 printk("Saved registers for jprobe %p\n", jp);
532 show_registers(saved_regs);
533 printk("Current registers\n");
534 show_registers(regs);
535 BUG();
537 *regs = kcb->jprobe_saved_regs;
538 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
539 MIN_STACK_SIZE(stack_addr));
540 return 1;
542 return 0;
545 static struct kprobe trampoline_p = {
546 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
547 .pre_handler = trampoline_probe_handler
550 int __init arch_init_kprobes(void)
552 return register_kprobe(&trampoline_p);