From patchwork Tue Aug 6 06:12:06 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandeepa Prabhu X-Patchwork-Id: 2839205 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id ABDCDBF535 for ; Tue, 6 Aug 2013 06:13:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 91B3F20124 for ; Tue, 6 Aug 2013 06:13:05 +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 5B0BA20116 for ; Tue, 6 Aug 2013 06:13:04 +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 1V6aVw-00085p-UI; Tue, 06 Aug 2013 06:13:01 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1V6aVu-0002To-Pe; Tue, 06 Aug 2013 06:12:58 +0000 Received: from mail-pd0-f175.google.com ([209.85.192.175]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1V6aVr-0002TH-BB for linux-arm-kernel@lists.infradead.org; Tue, 06 Aug 2013 06:12:56 +0000 Received: by mail-pd0-f175.google.com with SMTP id 5so4139489pdd.6 for ; Mon, 05 Aug 2013 23:12:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:x-gm-message-state; bh=BMKGrXW3It1y5b9slSK6PYAtq3ZOvAr9iVh7fFzbV0I=; b=hNdj1ffQE0PW5C239StCQT74hA82NUpFNnZ82f1OfjMFz+lHv1FjnIwB43KJ12Lie9 Azqpu73ncnibxHcHrJmwpSgpJF2ruCSRzPmthjeLLjW+ly0S6j4fp7sC/nZBARhMjVkv J6RjlNhnFQUgyQOqXRu+MeS3fVGEsavBSkERnwNArDCRFHeoFRPdPAcpJ2/+Vj4hpAsq jZAX6wLSvtGn0RfjW6v5pl4ePqpxalyX6QXycIvvtA/Cmee2kKCvrcB2YoIJaOdvLXaE vA4rcVM7rDon0g189gfCVuI9/SCOFWVDy0vxeqxGoWwGhRoE/+TnnB2wp0fSP7NebBX/ hjCw== X-Received: by 10.68.189.103 with SMTP id gh7mr25928653pbc.142.1375769550850; Mon, 05 Aug 2013 23:12:30 -0700 (PDT) Received: from linaro-workstation.ban.broadcom.com ([202.122.18.226]) by mx.google.com with ESMTPSA id eq5sm3148182pbc.15.2013.08.05.23.12.26 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 05 Aug 2013 23:12:29 -0700 (PDT) From: Sandeepa Prabhu To: linux-arm-kernel@lists.infradead.org Subject: [PATCH] AArch64: Add single-step and breakpoint handler hooks Date: Tue, 6 Aug 2013 11:42:06 +0530 Message-Id: <1375769526-3526-1-git-send-email-sandeepa.prabhu@linaro.org> X-Mailer: git-send-email 1.8.1.2 X-Gm-Message-State: ALoCoQmhHT9j5X9iq7u+Mvtzb3CSwH2KRBQRKUQFPBkOY9avhw8xW+p543K5bNx7PixNWEppDeo/ X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130806_021255_559433_2747F185 X-CRM114-Status: GOOD ( 14.59 ) X-Spam-Score: -1.9 (-) Cc: 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.2 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 pass the correct break/step address to the handlers, i.e. FAR_EL1 if exception is watchpoint, ELR_EL1 for all other debug exceptions. Signed-off-by: Sandeepa Prabhu Signed-off-by: Deepak Saxena --- arch/arm64/include/asm/debug-monitors.h | 20 +++++++ arch/arm64/kernel/debug-monitors.c | 102 +++++++++++++++++++++++++++++++- arch/arm64/kernel/entry.S | 6 +- 3 files changed, 124 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/debug-monitors.h b/arch/arm64/include/asm/debug-monitors.h index a2232d0..aff3a76 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,24 @@ struct task_struct; #define DBG_ARCH_ID_RESERVED 0 /* In case of ptrace ABI updates. */ +struct step_hook { + struct list_head node; + int (*fn)(struct pt_regs *regs, unsigned int insn, unsigned long addr); +}; + +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_magic; + u32 esr_mask; + int (*fn)(struct pt_regs *regs, unsigned int insn, unsigned long addr); +}; + +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..2846327 100644 --- a/arch/arm64/kernel/debug-monitors.c +++ b/arch/arm64/kernel/debug-monitors.c @@ -188,6 +188,54 @@ static void clear_regs_spsr_ss(struct pt_regs *regs) regs->pstate = spsr; } +/* EL1 Single Step Handler hooks */ +static LIST_HEAD(step_hook); +static DEFINE_RAW_SPINLOCK(step_lock); + +void register_step_hook(struct step_hook *hook) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&step_lock, flags); + list_add(&hook->node, &step_hook); + raw_spin_unlock_irqrestore(&step_lock, flags); +} + +void unregister_step_hook(struct step_hook *hook) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&step_lock, flags); + list_del(&hook->node); + raw_spin_unlock_irqrestore(&step_lock, flags); +} + +/* + * 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, unsigned long addr) +{ + struct step_hook *hook; + unsigned long flags; + + raw_spin_lock_irqsave(&step_lock, flags); + list_for_each_entry(hook, &step_hook, node) { + raw_spin_unlock_irqrestore(&step_lock, flags); + + if (hook->fn(regs, esr, addr) == 0) + return 0; + + raw_spin_lock_irqsave(&step_lock, flags); + } + raw_spin_unlock_irqrestore(&step_lock, flags); + + return 1; +} + static int single_step_handler(unsigned long addr, unsigned int esr, struct pt_regs *regs) { @@ -215,8 +263,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, addr, esr) == 0) + 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 +278,56 @@ static int single_step_handler(unsigned long addr, unsigned int esr, return 0; } + +static LIST_HEAD(break_hook); +static DEFINE_RAW_SPINLOCK(break_lock); + +void register_break_hook(struct break_hook *hook) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&break_lock, flags); + list_add(&hook->node, &break_hook); + raw_spin_unlock_irqrestore(&break_lock, flags); +} + +void unregister_break_hook(struct break_hook *hook) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&break_lock, flags); + list_del(&hook->node); + raw_spin_unlock_irqrestore(&break_lock, flags); +} + +static int call_break_hook(struct pt_regs *regs, + unsigned int esr, unsigned long addr) +{ + struct break_hook *hook; + unsigned long flags; + int (*fn)(struct pt_regs *regs, + unsigned int esr, unsigned long addr) = NULL; + + raw_spin_lock_irqsave(&break_lock, flags); + list_for_each_entry(hook, &break_hook, node) + if ((esr & hook->esr_mask) == hook->esr_magic) + fn = hook->fn; + raw_spin_unlock_irqrestore(&break_lock, flags); + + return fn ? fn(regs, esr, addr) : 1; +} + 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, addr) == 0) + return 0; + + pr_warn("unexpected brk exception at %lx, esr=0x%x\n", addr, esr); + if (!user_mode(regs)) return -EFAULT; @@ -291,7 +387,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 6ad781b..e7350bd 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -288,8 +288,12 @@ el1_dbg: /* * Debug exception handling */ + mrs x25, far_el1 //far for watchpt + cmp x24, #ESR_EL1_EC_WATCHPT_EL1 + csel x0, x25, x22, eq //addr: x25->far_el1, x22->elr_el1 + b.ge do_dbg tbz x24, #0, el1_inv // EL1 only - mrs x0, far_el1 +do_dbg: mov x2, sp // struct pt_regs bl do_debug_exception