From patchwork Tue Oct 1 15:57:56 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandeepa Prabhu X-Patchwork-Id: 2970761 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B04869F245 for ; Tue, 1 Oct 2013 16:00:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6FDBD203FD for ; Tue, 1 Oct 2013 16:00:14 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6CF9A20414 for ; Tue, 1 Oct 2013 16:00:09 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VR2Mo-0004Sr-51; Tue, 01 Oct 2013 16:00:06 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1VR2Ml-0005eD-Mj; Tue, 01 Oct 2013 16:00:03 +0000 Received: from mail-pa0-f44.google.com ([209.85.220.44]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1VR2Mh-0005cb-5E for linux-arm-kernel@lists.infradead.org; Tue, 01 Oct 2013 16:00:00 +0000 Received: by mail-pa0-f44.google.com with SMTP id lf10so7672374pab.3 for ; Tue, 01 Oct 2013 08:59:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=n9kIGbgyfHDDmDC3hmrwmTMzEwc77SerksLgV5JyQn0=; b=EK/ZlomcjHeY3Od9QS++zhDSgrXbAjt2EflWYe1Z/56R3doLzral1S5ifNm0VbhTy4 N9ahSLCPb98lzkGNH5F28/1N44itq/7jjwrqU7/l3VcoAY6p+Eq+TJ0HbbDbG38tGP4M v6NEf5bcYPDDUgZQScmaCESMPQQlFYSoK+lAnZiPSbwHFQRqJ0GEVBwR/QknVyGG1R8D /w/5qpiOw9w6D+q8hnUseAFXdwPJuOo3OD0rTthmf947neKzPwZP3TrnG9QZLxdBZduw Qf2B0LyF39A+CuPlw4FrQjgscTEKF3HXQdwivAkoY6TpBwNY5GF1abuFz+2mzQ9I+ufu Wmyw== X-Gm-Message-State: ALoCoQng9SEHglGEcDxhLZ7ALb3WLN2Qr78jmtME5CC1lncEG0Epj8356d6knUFB2Up3ONOKyXmy X-Received: by 10.66.191.5 with SMTP id gu5mr34195024pac.126.1380643175870; Tue, 01 Oct 2013 08:59:35 -0700 (PDT) Received: from linaro-workstation.ban.broadcom.com ([202.122.18.226]) by mx.google.com with ESMTPSA id nj9sm7593660pbc.13.1969.12.31.16.00.00 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 01 Oct 2013 08:59:34 -0700 (PDT) From: Sandeepa Prabhu To: linux-arm-kernel@lists.infradead.org Subject: [PATCH RFC v3 1/5] AArch64: Add single-step and breakpoint handler hooks Date: Tue, 1 Oct 2013 21:27:56 +0530 Message-Id: <1380643080-8984-2-git-send-email-sandeepa.prabhu@linaro.org> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1380643080-8984-1-git-send-email-sandeepa.prabhu@linaro.org> References: <1380643080-8984-1-git-send-email-sandeepa.prabhu@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20131001_115959_389280_261646D6 X-CRM114-Status: GOOD ( 15.29 ) X-Spam-Score: -2.6 (--) Cc: tixy@linaro.org, deepak.saxena@linaro.org, linaro-kernel@lists.linaro.org, patches@linaro.org, Sandeepa Prabhu , catalin.marinas@arm.com, will.deacon@arm.com, Deepak Saxena X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Signed-off-by: Deepak Saxena --- arch/arm64/include/asm/debug-monitors.h | 23 +++++++++ arch/arm64/kernel/debug-monitors.c | 85 +++++++++++++++++++++++++++++++-- arch/arm64/kernel/entry.S | 2 + 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/debug-monitors.h b/arch/arm64/include/asm/debug-monitors.h index a2232d0..8e354b3 100644 --- a/arch/arm64/include/asm/debug-monitors.h +++ b/arch/arm64/include/asm/debug-monitors.h @@ -16,6 +16,8 @@ #ifndef __ASM_DEBUG_MONITORS_H #define __ASM_DEBUG_MONITORS_H +#include + #ifdef __KERNEL__ #define DBG_ESR_EVT(x) (((x) >> 27) & 0x7) @@ -62,6 +64,27 @@ struct task_struct; #define DBG_ARCH_ID_RESERVED 0 /* In case of ptrace ABI updates. */ +#define DEBUG_HOOK_HANDLED 0 +#define DEBUG_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); diff --git a/arch/arm64/kernel/debug-monitors.c b/arch/arm64/kernel/debug-monitors.c index cbfacf7..fbbf824 100644 --- a/arch/arm64/kernel/debug-monitors.c +++ b/arch/arm64/kernel/debug-monitors.c @@ -188,6 +188,43 @@ static void clear_regs_spsr_ss(struct pt_regs *regs) regs->pstate = spsr; } +/* EL1 Single Step Handler hooks */ +static LIST_HEAD(step_hook); + +void register_step_hook(struct step_hook *hook) +{ + list_add_rcu(&hook->node, &step_hook); +} + +void unregister_step_hook(struct step_hook *hook) +{ + list_del_rcu(&hook->node); +} + +/* + * 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 = DEBUG_HOOK_ERROR; + + rcu_read_lock(); + + list_for_each_entry_rcu(hook, &step_hook, node) { + retval = hook->fn(regs, esr); + if (retval == DEBUG_HOOK_HANDLED) + break; + } + + rcu_read_unlock(); + + return retval; +} + static int single_step_handler(unsigned long addr, unsigned int esr, struct pt_regs *regs) { @@ -215,8 +252,11 @@ static int single_step_handler(unsigned long addr, unsigned int esr, */ user_rewind_single_step(current); } else { - /* TODO: route to KGDB */ - pr_warning("Unexpected kernel single-step exception at EL1\n"); + /* Call single step handlers for kgdb/kprobes */ + if (call_step_hook(regs, esr) == DEBUG_HOOK_HANDLED) + return 0; + + pr_warn("unexpected single step exception at %lx!\n", addr); /* * Re-enable stepping since we know that we will be * returning to regs. @@ -227,11 +267,50 @@ static int single_step_handler(unsigned long addr, unsigned int esr, return 0; } + +static LIST_HEAD(break_hook); +static DEFINE_RAW_SPINLOCK(break_hook_lock); + +void register_break_hook(struct break_hook *hook) +{ + raw_spin_lock(&break_hook_lock); + list_add(&hook->node, &break_hook); + raw_spin_unlock(&break_hook_lock); +} + +void unregister_break_hook(struct break_hook *hook) +{ + raw_spin_lock(&break_hook_lock); + list_del(&hook->node); + raw_spin_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; + + raw_spin_lock(&break_hook_lock); + list_for_each_entry(hook, &break_hook, node) + if ((esr & hook->esr_mask) == hook->esr_val) + fn = hook->fn; + raw_spin_unlock(&break_hook_lock); + + return fn ? fn(regs, esr) : DEBUG_HOOK_ERROR; +} + static int brk_handler(unsigned long addr, unsigned int esr, struct pt_regs *regs) { siginfo_t info; + /* call single step handlers for kgdb/kprobes */ + if (call_break_hook(regs, esr) == DEBUG_HOOK_HANDLED) + return 0; + + pr_warn("unexpected brk exception at %llx, esr=0x%x\n", + instruction_pointer(regs), esr); + if (!user_mode(regs)) return -EFAULT; @@ -291,7 +370,7 @@ static int __init debug_traps_init(void) hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP, TRAP_HWBKPT, "single-step handler"); hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP, - TRAP_BRKPT, "ptrace BRK handler"); + TRAP_BRKPT, "AArch64 BRK handler"); return 0; } arch_initcall(debug_traps_init); diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index 3881fd1..7fbc510 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -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