arm64: support single-step and breakpoint handler hooks
authorSandeepa Prabhu <sandeepa.prabhu@linaro.org>
Wed, 4 Dec 2013 05:50:20 +0000 (05:50 +0000)
committerMark Brown <broonie@linaro.org>
Mon, 19 May 2014 18:50:20 +0000 (19:50 +0100)
AArch64 Single Steping and Breakpoint debug exceptions will be
used by multiple debug framworks like kprobes & kgdb.

This patch implements the hooks for those frameworks to register
their own handlers for handling breakpoint and single step events.

Reworked the debug exception handler in entry.S: do_dbg to route
software breakpoint (BRK64) exception to do_debug_exception()

Signed-off-by: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
Signed-off-by: Deepak Saxena <dsaxena@linaro.org>
Acked-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
(cherry picked from commit ee6214cec7818867f368c35843ea1f3dffcbb57c)
Signed-off-by: Mark Brown <broonie@linaro.org>
arch/arm64/include/asm/debug-monitors.h
arch/arm64/kernel/debug-monitors.c
arch/arm64/kernel/entry.S

index ef8235c68c092674ed0c628733bbc7a6b02404b5..1bc1c4f15c145f498b94135e1732c274fa54995d 100644 (file)
@@ -62,6 +62,27 @@ struct task_struct;
 
 #define DBG_ARCH_ID_RESERVED   0       /* In case of ptrace ABI updates. */
 
+#define DBG_HOOK_HANDLED       0
+#define DBG_HOOK_ERROR         1
+
+struct step_hook {
+       struct list_head node;
+       int (*fn)(struct pt_regs *regs, unsigned int esr);
+};
+
+void register_step_hook(struct step_hook *hook);
+void unregister_step_hook(struct step_hook *hook);
+
+struct break_hook {
+       struct list_head node;
+       u32 esr_val;
+       u32 esr_mask;
+       int (*fn)(struct pt_regs *regs, unsigned int esr);
+};
+
+void register_break_hook(struct break_hook *hook);
+void unregister_break_hook(struct break_hook *hook);
+
 u8 debug_monitors_arch(void);
 
 void enable_debug_monitors(enum debug_el el);
index 08018e3df580c65d259470175fc95319d0589322..2867749b56be5773d677075ef2b20216fe9f2ba2 100644 (file)
@@ -188,6 +188,48 @@ static void clear_regs_spsr_ss(struct pt_regs *regs)
        regs->pstate = spsr;
 }
 
+/* EL1 Single Step Handler hooks */
+static LIST_HEAD(step_hook);
+DEFINE_RWLOCK(step_hook_lock);
+
+void register_step_hook(struct step_hook *hook)
+{
+       write_lock(&step_hook_lock);
+       list_add(&hook->node, &step_hook);
+       write_unlock(&step_hook_lock);
+}
+
+void unregister_step_hook(struct step_hook *hook)
+{
+       write_lock(&step_hook_lock);
+       list_del(&hook->node);
+       write_unlock(&step_hook_lock);
+}
+
+/*
+ * Call registered single step handers
+ * There is no Syndrome info to check for determining the handler.
+ * So we call all the registered handlers, until the right handler is
+ * found which returns zero.
+ */
+static int call_step_hook(struct pt_regs *regs, unsigned int esr)
+{
+       struct step_hook *hook;
+       int retval = DBG_HOOK_ERROR;
+
+       read_lock(&step_hook_lock);
+
+       list_for_each_entry(hook, &step_hook, node)     {
+               retval = hook->fn(regs, esr);
+               if (retval == DBG_HOOK_HANDLED)
+                       break;
+       }
+
+       read_unlock(&step_hook_lock);
+
+       return retval;
+}
+
 static int single_step_handler(unsigned long addr, unsigned int esr,
                               struct pt_regs *regs)
 {
@@ -215,7 +257,9 @@ static int single_step_handler(unsigned long addr, unsigned int esr,
                 */
                user_rewind_single_step(current);
        } else {
-               /* TODO: route to KGDB */
+               if (call_step_hook(regs, esr) == DBG_HOOK_HANDLED)
+                       return 0;
+
                pr_warning("Unexpected kernel single-step exception at EL1\n");
                /*
                 * Re-enable stepping since we know that we will be
@@ -227,11 +271,53 @@ static int single_step_handler(unsigned long addr, unsigned int esr,
        return 0;
 }
 
+/*
+ * Breakpoint handler is re-entrant as another breakpoint can
+ * hit within breakpoint handler, especically in kprobes.
+ * Use reader/writer locks instead of plain spinlock.
+ */
+static LIST_HEAD(break_hook);
+DEFINE_RWLOCK(break_hook_lock);
+
+void register_break_hook(struct break_hook *hook)
+{
+       write_lock(&break_hook_lock);
+       list_add(&hook->node, &break_hook);
+       write_unlock(&break_hook_lock);
+}
+
+void unregister_break_hook(struct break_hook *hook)
+{
+       write_lock(&break_hook_lock);
+       list_del(&hook->node);
+       write_unlock(&break_hook_lock);
+}
+
+static int call_break_hook(struct pt_regs *regs, unsigned int esr)
+{
+       struct break_hook *hook;
+       int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
+
+       read_lock(&break_hook_lock);
+       list_for_each_entry(hook, &break_hook, node)
+               if ((esr & hook->esr_mask) == hook->esr_val)
+                       fn = hook->fn;
+       read_unlock(&break_hook_lock);
+
+       return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
+}
+
 static int brk_handler(unsigned long addr, unsigned int esr,
                       struct pt_regs *regs)
 {
        siginfo_t info;
 
+       if (call_break_hook(regs, esr) == DBG_HOOK_HANDLED)
+               return 0;
+
+       pr_warn("unexpected brk exception at %lx, esr=0x%x\n",
+                       (long)instruction_pointer(regs), esr);
+
        if (!user_mode(regs))
                return -EFAULT;
 
index 1d1314280a03a678c4ef63092712d49079386cdd..6d5f1cb01b1149f6484f967eb2a64316b477cfea 100644 (file)
@@ -288,6 +288,8 @@ el1_dbg:
        /*
         * Debug exception handling
         */
+       cmp     x24, #ESR_EL1_EC_BRK64          // if BRK64
+       cinc    x24, x24, eq                    // set bit '0'
        tbz     x24, #0, el1_inv                // EL1 only
        mrs     x0, far_el1
        mov     x2, sp                          // struct pt_regs