[PATCH] Kprobes: rearrange preempt_disable/enable() calls
[platform/kernel/linux-arm64.git] / arch / ia64 / kernel / kprobes.c
index 5978823..1e80ec8 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <asm/pgtable.h>
 #include <asm/kdebug.h>
+#include <asm/sections.h>
 
 extern void jprobe_inst_return(void);
 
@@ -86,12 +87,25 @@ static enum instruction_type bundle_encoding[32][3] = {
  * is IP relative instruction and update the kprobe
  * inst flag accordingly
  */
-static void update_kprobe_inst_flag(uint template, uint  slot, uint major_opcode,
-       unsigned long kprobe_inst, struct kprobe *p)
+static void __kprobes update_kprobe_inst_flag(uint template, uint  slot,
+                                             uint major_opcode,
+                                             unsigned long kprobe_inst,
+                                             struct kprobe *p)
 {
        p->ainsn.inst_flag = 0;
        p->ainsn.target_br_reg = 0;
 
+       /* Check for Break instruction
+        * Bits 37:40 Major opcode to be zero
+        * Bits 27:32 X6 to be zero
+        * Bits 32:35 X3 to be zero
+        */
+       if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) {
+               /* is a break instruction */
+               p->ainsn.inst_flag |= INST_FLAG_BREAK_INST;
+               return;
+       }
+
        if (bundle_encoding[template][slot] == B) {
                switch (major_opcode) {
                  case INDIRECT_CALL_OPCODE:
@@ -125,8 +139,10 @@ static void update_kprobe_inst_flag(uint template, uint  slot, uint major_opcode
  * Returns 0 if supported
  * Returns -EINVAL if unsupported
  */
-static int unsupported_inst(uint template, uint  slot, uint major_opcode,
-       unsigned long kprobe_inst, struct kprobe *p)
+static int __kprobes unsupported_inst(uint template, uint  slot,
+                                     uint major_opcode,
+                                     unsigned long kprobe_inst,
+                                     struct kprobe *p)
 {
        unsigned long addr = (unsigned long)p->addr;
 
@@ -167,8 +183,9 @@ static int unsupported_inst(uint template, uint  slot, uint major_opcode,
  * on which we are inserting kprobe is cmp instruction
  * with ctype as unc.
  */
-static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
-unsigned long kprobe_inst)
+static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
+                                           uint major_opcode,
+                                           unsigned long kprobe_inst)
 {
        cmp_inst_t cmp_inst;
        uint ctype_unc = 0;
@@ -200,8 +217,10 @@ out:
  * In this function we override the bundle with
  * the break instruction at the given slot.
  */
-static void prepare_break_inst(uint template, uint  slot, uint major_opcode,
-       unsigned long kprobe_inst, struct kprobe *p)
+static void __kprobes prepare_break_inst(uint template, uint  slot,
+                                        uint major_opcode,
+                                        unsigned long kprobe_inst,
+                                        struct kprobe *p)
 {
        unsigned long break_inst = BREAK_INST;
        bundle_t *bundle = &p->ainsn.insn.bundle;
@@ -263,13 +282,34 @@ static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
        }
 }
 
-static int valid_kprobe_addr(int template, int slot, unsigned long addr)
+/* Returns non-zero if the addr is in the Interrupt Vector Table */
+static inline int in_ivt_functions(unsigned long addr)
+{
+       return (addr >= (unsigned long)__start_ivt_text
+               && addr < (unsigned long)__end_ivt_text);
+}
+
+static int __kprobes valid_kprobe_addr(int template, int slot,
+                                      unsigned long addr)
 {
        if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
-               printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n",
-                               addr);
+               printk(KERN_WARNING "Attempting to insert unaligned kprobe "
+                               "at 0x%lx\n", addr);
+               return -EINVAL;
+       }
+
+       if (in_ivt_functions(addr)) {
+               printk(KERN_WARNING "Kprobes can't be inserted inside "
+                               "IVT functions at 0x%lx\n", addr);
+               return -EINVAL;
+       }
+
+       if (slot == 1 && bundle_encoding[template][1] != L) {
+               printk(KERN_WARNING "Inserting kprobes on slot #1 "
+                      "is not supported\n");
                return -EINVAL;
        }
+
        return 0;
 }
 
@@ -290,7 +330,96 @@ static inline void set_current_kprobe(struct kprobe *p)
        current_kprobe = p;
 }
 
-int arch_prepare_kprobe(struct kprobe *p)
+static void kretprobe_trampoline(void)
+{
+}
+
+/*
+ * At this point the target function has been tricked into
+ * returning into our trampoline.  Lookup the associated instance
+ * and then:
+ *    - call the handler function
+ *    - cleanup by marking the instance as unused
+ *    - long jump back to the original return address
+ */
+int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
+{
+       struct kretprobe_instance *ri = NULL;
+       struct hlist_head *head;
+       struct hlist_node *node, *tmp;
+       unsigned long orig_ret_address = 0;
+       unsigned long trampoline_address =
+               ((struct fnptr *)kretprobe_trampoline)->ip;
+
+        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);
+
+               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;
+       }
+
+       BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
+       regs->cr_iip = orig_ret_address;
+
+       unlock_kprobes();
+       preempt_enable_no_resched();
+
+        /*
+         * By returning a non-zero value, we are telling
+         * kprobe_handler() that we have handled unlocking
+        * and re-enabling preemption
+         */
+        return 1;
+}
+
+void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
+                                     struct pt_regs *regs)
+{
+       struct kretprobe_instance *ri;
+
+       if ((ri = get_free_rp_inst(rp)) != NULL) {
+               ri->rp = rp;
+               ri->task = current;
+               ri->ret_addr = (kprobe_opcode_t *)regs->b0;
+
+               /* Replace the return addr with trampoline addr */
+               regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
+
+               add_rp_inst(ri);
+       } else {
+               rp->nmissed++;
+       }
+}
+
+int __kprobes arch_prepare_kprobe(struct kprobe *p)
 {
        unsigned long addr = (unsigned long) p->addr;
        unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
@@ -321,7 +450,7 @@ int arch_prepare_kprobe(struct kprobe *p)
        return 0;
 }
 
-void arch_arm_kprobe(struct kprobe *p)
+void __kprobes arch_arm_kprobe(struct kprobe *p)
 {
        unsigned long addr = (unsigned long)p->addr;
        unsigned long arm_addr = addr & ~0xFULL;
@@ -330,7 +459,7 @@ void arch_arm_kprobe(struct kprobe *p)
        flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
 }
 
-void arch_disarm_kprobe(struct kprobe *p)
+void __kprobes arch_disarm_kprobe(struct kprobe *p)
 {
        unsigned long addr = (unsigned long)p->addr;
        unsigned long arm_addr = addr & ~0xFULL;
@@ -340,7 +469,7 @@ void arch_disarm_kprobe(struct kprobe *p)
        flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
 }
 
-void arch_remove_kprobe(struct kprobe *p)
+void __kprobes arch_remove_kprobe(struct kprobe *p)
 {
 }
 
@@ -352,7 +481,7 @@ void arch_remove_kprobe(struct kprobe *p)
  * to original stack address, handle the case where we need to fixup the
  * relative IP address and/or fixup branch register.
  */
-static void resume_execution(struct kprobe *p, struct pt_regs *regs)
+static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
 {
        unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
        unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
@@ -419,13 +548,16 @@ turn_ss_off:
        ia64_psr(regs)->ss = 0;
 }
 
-static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
+static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
 {
        unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
        unsigned long slot = (unsigned long)p->addr & 0xf;
 
-       /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
-       regs->cr_iip = bundle_addr & ~0xFULL;
+       /* single step inline if break instruction */
+       if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
+               regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
+       else
+               regs->cr_iip = bundle_addr & ~0xFULL;
 
        if (slot > 2)
                slot = 0;
@@ -436,20 +568,52 @@ static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
        ia64_psr(regs)->ss = 1;
 }
 
-static int pre_kprobes_handler(struct die_args *args)
+static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
+{
+       unsigned int slot = ia64_psr(regs)->ri;
+       unsigned int template, major_opcode;
+       unsigned long kprobe_inst;
+       unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
+       bundle_t bundle;
+
+       memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
+       template = bundle.quad0.template;
+
+       /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
+       if (slot == 1 && bundle_encoding[template][1] == L)
+               slot++;
+
+       /* Get Kprobe probe instruction at given slot*/
+       get_kprobe_inst(&bundle, slot, &kprobe_inst, &major_opcode);
+
+       /* For break instruction,
+        * Bits 37:40 Major opcode to be zero
+        * Bits 27:32 X6 to be zero
+        * Bits 32:35 X3 to be zero
+        */
+       if (major_opcode || ((kprobe_inst >> 27) & 0x1FF) ) {
+               /* Not a break instruction */
+               return 0;
+       }
+
+       /* Is a break instruction */
+       return 1;
+}
+
+static int __kprobes pre_kprobes_handler(struct die_args *args)
 {
        struct kprobe *p;
        int ret = 0;
        struct pt_regs *regs = args->regs;
        kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
 
-       preempt_disable();
-
        /* Handle recursion cases */
        if (kprobe_running()) {
                p = get_kprobe(addr);
                if (p) {
-                       if (kprobe_status == KPROBE_HIT_SS) {
+                       if ( (kprobe_status == KPROBE_HIT_SS) &&
+                            (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
+                               ia64_psr(regs)->ss = 0;
                                unlock_kprobes();
                                goto no_kprobe;
                        }
@@ -483,17 +647,35 @@ static int pre_kprobes_handler(struct die_args *args)
        p = get_kprobe(addr);
        if (!p) {
                unlock_kprobes();
+               if (!is_ia64_break_inst(regs)) {
+                       /*
+                        * The breakpoint instruction was removed right
+                        * after we hit it.  Another cpu has removed
+                        * either a probepoint or a debugger breakpoint
+                        * at this address.  In either case, no further
+                        * handling of this interrupt is appropriate.
+                        */
+                       ret = 1;
+
+               }
+
+               /* Not one of our break, let kernel handle it */
                goto no_kprobe;
        }
 
+       /*
+        * This preempt_disable() matches the preempt_enable_no_resched()
+        * in post_kprobes_handler()
+        */
+       preempt_disable();
        kprobe_status = KPROBE_HIT_ACTIVE;
        set_current_kprobe(p);
 
        if (p->pre_handler && p->pre_handler(p, regs))
                /*
                 * Our pre-handler is specifically requesting that we just
-                * do a return.  This is handling the case where the
-                * pre-handler is really our special jprobe pre-handler.
+                * do a return.  This is used for both the jprobe pre-handler
+                * and the kretprobe trampoline
                 */
                return 1;
 
@@ -503,11 +685,10 @@ ss_probe:
        return 1;
 
 no_kprobe:
-       preempt_enable_no_resched();
        return ret;
 }
 
-static int post_kprobes_handler(struct pt_regs *regs)
+static int __kprobes post_kprobes_handler(struct pt_regs *regs)
 {
        if (!kprobe_running())
                return 0;
@@ -532,7 +713,7 @@ out:
        return 1;
 }
 
-static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
+static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
 {
        if (!kprobe_running())
                return 0;
@@ -550,29 +731,33 @@ static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
        return 0;
 }
 
-int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
-                            void *data)
+int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
+                                      unsigned long val, void *data)
 {
        struct die_args *args = (struct die_args *)data;
+       int ret = NOTIFY_DONE;
+
+       preempt_disable();
        switch(val) {
        case DIE_BREAK:
                if (pre_kprobes_handler(args))
-                       return NOTIFY_STOP;
+                       ret = NOTIFY_STOP;
                break;
        case DIE_SS:
                if (post_kprobes_handler(args->regs))
-                       return NOTIFY_STOP;
+                       ret = NOTIFY_STOP;
                break;
        case DIE_PAGE_FAULT:
                if (kprobes_fault_handler(args->regs, args->trapnr))
-                       return NOTIFY_STOP;
+                       ret = NOTIFY_STOP;
        default:
                break;
        }
-       return NOTIFY_DONE;
+       preempt_enable();
+       return ret;
 }
 
-int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
+int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
 {
        struct jprobe *jp = container_of(p, struct jprobe, kp);
        unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
@@ -594,8 +779,19 @@ int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
        return 1;
 }
 
-int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
+int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
 {
        *regs = jprobe_saved_regs;
        return 1;
 }
+
+static struct kprobe trampoline_p = {
+       .pre_handler = trampoline_probe_handler
+};
+
+int __init arch_init_kprobes(void)
+{
+       trampoline_p.addr =
+               (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
+       return register_kprobe(&trampoline_p);
+}