]> err.no Git - linux-2.6/blobdiff - arch/sparc64/kernel/kprobes.c
Merge branch 'for-linus' of git://git.kernel.dk/linux-2.6-block
[linux-2.6] / arch / sparc64 / kernel / kprobes.c
index 8e75ed762fd80a8555d831c6b324b78282553b6c..34fc3ddd50026dbbc802ded84b7329382f76c17f 100644 (file)
@@ -6,7 +6,7 @@
 #include <linux/kernel.h>
 #include <linux/kprobes.h>
 #include <linux/module.h>
-#include <asm/kdebug.h>
+#include <linux/kdebug.h>
 #include <asm/signal.h>
 #include <asm/cacheflush.h>
 #include <asm/uaccess.h>
 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 
+struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
+
 int __kprobes arch_prepare_kprobe(struct kprobe *p)
 {
        p->ainsn.insn[0] = *p->addr;
+       flushi(&p->ainsn.insn[0]);
+
        p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
+       flushi(&p->ainsn.insn[1]);
+
        p->opcode = *p->addr;
        return 0;
 }
@@ -185,16 +191,19 @@ no_kprobe:
 /* If INSN is a relative control transfer instruction,
  * return the corrected branch destination value.
  *
- * The original INSN location was REAL_PC, it actually
- * executed at PC and produced destination address NPC.
+ * regs->tpc and regs->tnpc still hold the values of the
+ * program counters at the time of trap due to the execution
+ * of the BREAKPOINT_INSTRUCTION_2 at p->ainsn.insn[1]
+ * 
  */
-static unsigned long __kprobes relbranch_fixup(u32 insn, unsigned long real_pc,
-                                              unsigned long pc,
-                                              unsigned long npc)
+static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p,
+                                              struct pt_regs *regs)
 {
+       unsigned long real_pc = (unsigned long) p->addr;
+
        /* Branch not taken, no mods necessary.  */
-       if (npc == pc + 0x4UL)
-               return real_pc + 0x4UL;
+       if (regs->tnpc == regs->tpc + 0x4UL)
+               return real_pc + 0x8UL;
 
        /* The three cases are call, branch w/prediction,
         * and traditional branch.
@@ -202,14 +211,21 @@ static unsigned long __kprobes relbranch_fixup(u32 insn, unsigned long real_pc,
        if ((insn & 0xc0000000) == 0x40000000 ||
            (insn & 0xc1c00000) == 0x00400000 ||
            (insn & 0xc1c00000) == 0x00800000) {
+               unsigned long ainsn_addr;
+
+               ainsn_addr = (unsigned long) &p->ainsn.insn[0];
+
                /* The instruction did all the work for us
                 * already, just apply the offset to the correct
                 * instruction location.
                 */
-               return (real_pc + (npc - pc));
+               return (real_pc + (regs->tnpc - ainsn_addr));
        }
 
-       return real_pc + 0x4UL;
+       /* It is jmpl or some other absolute PC modification instruction,
+        * leave NPC as-is.
+        */
+       return regs->tnpc;
 }
 
 /* If INSN is an instruction which writes it's PC location
@@ -220,12 +236,12 @@ static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
 {
        unsigned long *slot = NULL;
 
-       /* Simplest cast is call, which always uses %o7 */
+       /* Simplest case is 'call', which always uses %o7 */
        if ((insn & 0xc0000000) == 0x40000000) {
                slot = &regs->u_regs[UREG_I7];
        }
 
-       /* Jmpl encodes the register inside of the opcode */
+       /* 'jmpl' encodes the register inside of the opcode */
        if ((insn & 0xc1f80000) == 0x81c00000) {
                unsigned long rd = ((insn >> 25) & 0x1f);
 
@@ -247,11 +263,11 @@ static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
 
 /*
  * Called after single-stepping.  p->addr is the address of the
- * instruction whose first byte has been replaced by the breakpoint
+ * instruction which has been replaced by the breakpoint
  * instruction.  To avoid the SMP problems that can occur when we
  * temporarily put back the original opcode to single-step, we
  * single-stepped a copy of the instruction.  The address of this
- * copy is p->ainsn.insn.
+ * copy is &p->ainsn.insn[0].
  *
  * This function prepares to return from the post-single-step
  * breakpoint trap.
@@ -261,11 +277,11 @@ static void __kprobes resume_execution(struct kprobe *p,
 {
        u32 insn = p->ainsn.insn[0];
 
+       regs->tnpc = relbranch_fixup(insn, p, regs);
+
+       /* This assignment must occur after relbranch_fixup() */
        regs->tpc = kcb->kprobe_orig_tnpc;
-       regs->tnpc = relbranch_fixup(insn,
-                                    (unsigned long) p->addr,
-                                    (unsigned long) &p->ainsn.insn[0],
-                                    regs->tnpc);
+
        retpc_fixup(regs, insn, (unsigned long) p->addr);
 
        regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
@@ -299,7 +315,7 @@ out:
        return 1;
 }
 
-static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
+int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
 {
        struct kprobe *cur = kprobe_running();
        struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
@@ -389,15 +405,6 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
                if (post_kprobe_handler(args->regs))
                        ret = NOTIFY_STOP;
                break;
-       case DIE_GPF:
-       case DIE_PAGE_FAULT:
-               /* kprobe_running() needs smp_processor_id() */
-               preempt_disable();
-               if (kprobe_running() &&
-                   kprobe_fault_handler(args->regs, args->trapnr))
-                       ret = NOTIFY_STOP;
-               preempt_enable();
-               break;
        default:
                break;
        }
@@ -430,17 +437,8 @@ int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
        struct jprobe *jp = container_of(p, struct jprobe, kp);
        struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
 
-       kcb->jprobe_saved_regs_location = regs;
        memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
 
-       /* Save a whole stack frame, this gets arguments
-        * pushed onto the stack after using up all the
-        * arg registers.
-        */
-       memcpy(&(kcb->jprobe_saved_stack),
-              (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
-              sizeof(kcb->jprobe_saved_stack));
-
        regs->tpc  = (unsigned long) jp->entry;
        regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
        regs->tstate |= TSTATE_PIL;
@@ -450,10 +448,19 @@ int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
 
 void __kprobes jprobe_return(void)
 {
-       __asm__ __volatile__(
-               ".globl jprobe_return_trap_instruction\n"
+       struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
+       register unsigned long orig_fp asm("g1");
+
+       orig_fp = kcb->jprobe_saved_regs.u_regs[UREG_FP];
+       __asm__ __volatile__("\n"
+"1:    cmp             %%sp, %0\n\t"
+       "blu,a,pt       %%xcc, 1b\n\t"
+       " restore\n\t"
+       ".globl         jprobe_return_trap_instruction\n"
 "jprobe_return_trap_instruction:\n\t"
-               "ta 0x70");
+       "ta             0x70"
+       : /* no outputs */
+       : "r" (orig_fp));
 }
 
 extern void jprobe_return_trap_instruction(void);
@@ -466,34 +473,124 @@ int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
        struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
 
        if (addr == (u32 *) jprobe_return_trap_instruction) {
-               if (kcb->jprobe_saved_regs_location != regs) {
-                       printk("JPROBE: Current regs (%p) does not match "
-                              "saved regs (%p).\n",
-                              regs, kcb->jprobe_saved_regs_location);
-                       printk("JPROBE: Saved registers\n");
-                       __show_regs(kcb->jprobe_saved_regs_location);
-                       printk("JPROBE: Current registers\n");
-                       __show_regs(regs);
-                       BUG();
-               }
-               /* Restore old register state.  Do pt_regs
-                * first so that UREG_FP is the original one for
-                * the stack frame restore.
-                */
                memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
-
-               memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
-                      &(kcb->jprobe_saved_stack),
-                      sizeof(kcb->jprobe_saved_stack));
-
                preempt_enable_no_resched();
                return 1;
        }
        return 0;
 }
 
-/* architecture specific initialization */
-int arch_init_kprobes(void)
+/* Called with kretprobe_lock held.  The value stored in the return
+ * address register is actually 2 instructions before where the
+ * callee will return to.  Sequences usually look something like this
+ *
+ *             call    some_function   <--- return register points here
+ *              nop                    <--- call delay slot
+ *             whatever                <--- where callee returns to
+ *
+ * To keep trampoline_probe_handler logic simpler, we normalize the
+ * value kept in ri->ret_addr so we don't need to keep adjusting it
+ * back and forth.
+ */
+void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
+                                     struct pt_regs *regs)
+{
+       ri->ret_addr = (kprobe_opcode_t *)(regs->u_regs[UREG_RETPC] + 8);
+
+       /* Replace the return addr with trampoline addr */
+       regs->u_regs[UREG_RETPC] =
+               ((unsigned long)kretprobe_trampoline) - 8;
+}
+
+/*
+ * Called when the probe at kretprobe trampoline is hit
+ */
+int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
+{
+       struct kretprobe_instance *ri = NULL;
+       struct hlist_head *head, empty_rp;
+       struct hlist_node *node, *tmp;
+       unsigned long flags, orig_ret_address = 0;
+       unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
+
+       INIT_HLIST_HEAD(&empty_rp);
+       spin_lock_irqsave(&kretprobe_lock, flags);
+       head = kretprobe_inst_table_head(current);
+
+       /*
+        * It is possible to have multiple instances associated with a given
+        * task either because an multiple functions in the call path
+        * have a return probe installed on them, and/or more then one return
+        * return probe was registered for a target function.
+        *
+        * We can handle this because:
+        *     - instances are always inserted at the head of the list
+        *     - when multiple return probes are registered for the same
+        *       function, the first instance's ret_addr will point to the
+        *       real return address, and all the rest will point to
+        *       kretprobe_trampoline
+        */
+       hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
+               if (ri->task != current)
+                       /* another task is sharing our hash bucket */
+                       continue;
+
+               if (ri->rp && ri->rp->handler)
+                       ri->rp->handler(ri, regs);
+
+               orig_ret_address = (unsigned long)ri->ret_addr;
+               recycle_rp_inst(ri, &empty_rp);
+
+               if (orig_ret_address != trampoline_address)
+                       /*
+                        * This is the real return address. Any other
+                        * instances associated with this task are for
+                        * other calls deeper on the call stack
+                        */
+                       break;
+       }
+
+       kretprobe_assert(ri, orig_ret_address, trampoline_address);
+       regs->tpc = orig_ret_address;
+       regs->tnpc = orig_ret_address + 4;
+
+       reset_current_kprobe();
+       spin_unlock_irqrestore(&kretprobe_lock, flags);
+       preempt_enable_no_resched();
+
+       hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
+               hlist_del(&ri->hlist);
+               kfree(ri);
+       }
+       /*
+        * By returning a non-zero value, we are telling
+        * kprobe_handler() that we don't want the post_handler
+        * to run (and have re-enabled preemption)
+        */
+       return 1;
+}
+
+void kretprobe_trampoline_holder(void)
+{
+       asm volatile(".global kretprobe_trampoline\n"
+                    "kretprobe_trampoline:\n"
+                    "\tnop\n"
+                    "\tnop\n");
+}
+static struct kprobe trampoline_p = {
+       .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
+       .pre_handler = trampoline_probe_handler
+};
+
+int __init arch_init_kprobes(void)
 {
+       return register_kprobe(&trampoline_p);
+}
+
+int __kprobes arch_trampoline_kprobe(struct kprobe *p)
+{
+       if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
+               return 1;
+
        return 0;
 }