From patchwork Wed Dec 4 05:50:20 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandeepa Prabhu X-Patchwork-Id: 22007 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qe0-f69.google.com (mail-qe0-f69.google.com [209.85.128.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id BAD7F202DA for ; Wed, 4 Dec 2013 05:51:10 +0000 (UTC) Received: by mail-qe0-f69.google.com with SMTP id 1sf30012455qec.0 for ; Tue, 03 Dec 2013 21:51:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=gFFA+KdF0yQERVIKsafMelI4izHDO6gZOTNrnNKQPaY=; b=Y/xoqMyzibguHro/D4iVefpy43QvBbCqCC6A90MWWf27ISPYL92UIoLnuuqPXV2sKN J0XXIUZaJSAid9JyM1m+175EDQY3JZOWfiY4Ijcs8BHhk24Q2Rf8dq6V/2Ynu18YiDOv zgSN2UWJjYYWrUIY5cByHMjsodjjLx+aV/Vo25gsa7OcY5vsGqFFJrrkVjbBFSId0kwF SqlshUYWnYFYfdqt38VbdNUHeHNSx0nY5qMMz7XEK1uT4NkhkH92SVqZaCjvaHPM7eXk fEfBUC+d4SjYNyf6jvRilcDfNqLPmKQ4Ek3b7hNrF/uSKFqKEdbZmsf069GJ/SCq7erP Fozw== X-Gm-Message-State: ALoCoQmjTQlvwgpICgQQAv2IY3tsQ+Sf2BSz0Xx7K2QMMYMKw76GMSWhYKmhG6V80fXZbCcKEvvV X-Received: by 10.58.188.113 with SMTP id fz17mr25123464vec.26.1386136270145; Tue, 03 Dec 2013 21:51:10 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.2.69 with SMTP id 5ls149199qes.98.gmail; Tue, 03 Dec 2013 21:51:10 -0800 (PST) X-Received: by 10.52.164.203 with SMTP id ys11mr186277vdb.37.1386136270033; Tue, 03 Dec 2013 21:51:10 -0800 (PST) Received: from mail-vc0-f175.google.com (mail-vc0-f175.google.com [209.85.220.175]) by mx.google.com with ESMTPS id uw1si7423733vdc.6.2013.12.03.21.51.09 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 03 Dec 2013 21:51:10 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.175 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.175; Received: by mail-vc0-f175.google.com with SMTP id ld13so10941026vcb.34 for ; Tue, 03 Dec 2013 21:51:09 -0800 (PST) X-Received: by 10.52.249.3 with SMTP id yq3mr55588vdc.55.1386136269825; Tue, 03 Dec 2013 21:51:09 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp266140vcz; Tue, 3 Dec 2013 21:51:09 -0800 (PST) X-Received: by 10.67.21.130 with SMTP id hk2mr80103616pad.76.1386136268591; Tue, 03 Dec 2013 21:51:08 -0800 (PST) Received: from mail-pb0-f45.google.com (mail-pb0-f45.google.com [209.85.160.45]) by mx.google.com with ESMTPS id tt8si14511641pbc.78.2013.12.03.21.51.08 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 03 Dec 2013 21:51:08 -0800 (PST) Received-SPF: neutral (google.com: 209.85.160.45 is neither permitted nor denied by best guess record for domain of sandeepa.prabhu@linaro.org) client-ip=209.85.160.45; Received: by mail-pb0-f45.google.com with SMTP id rp16so22708022pbb.18 for ; Tue, 03 Dec 2013 21:51:08 -0800 (PST) X-Received: by 10.66.147.193 with SMTP id tm1mr80438728pab.56.1386136268117; Tue, 03 Dec 2013 21:51:08 -0800 (PST) Received: from linaro-workstation.ban.broadcom.com ([202.122.18.226]) by mx.google.com with ESMTPSA id p1sm134524542pbo.12.2013.12.03.21.51.03 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 03 Dec 2013 21:51:06 -0800 (PST) From: Sandeepa Prabhu To: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org, patches@linaro.org, linaro-kernel@lists.linaro.org, catalin.marinas@arm.com, will.deacon@arm.com, Vijaya.Kumar@caviumnetworks.com, dsaxena@linaro.org, jason.wessel@windriver.com, steve.capper@linaro.org, Sandeepa Prabhu Subject: [PATCH v4] arm64: support single-step and breakpoint handler hooks Date: Wed, 4 Dec 2013 11:20:20 +0530 Message-Id: <1386136220-15171-2-git-send-email-sandeepa.prabhu@linaro.org> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1386136220-15171-1-git-send-email-sandeepa.prabhu@linaro.org> References: <1386136220-15171-1-git-send-email-sandeepa.prabhu@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: sandeepa.prabhu@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.175 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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 Acked-by: Will Deacon --- arch/arm64/include/asm/debug-monitors.h | 21 ++++++++ arch/arm64/kernel/debug-monitors.c | 88 ++++++++++++++++++++++++++++++++- arch/arm64/kernel/entry.S | 2 + 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/debug-monitors.h b/arch/arm64/include/asm/debug-monitors.h index a2232d0..6231479 100644 --- a/arch/arm64/include/asm/debug-monitors.h +++ b/arch/arm64/include/asm/debug-monitors.h @@ -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); diff --git a/arch/arm64/kernel/debug-monitors.c b/arch/arm64/kernel/debug-monitors.c index 4ae6857..636ba8b 100644 --- a/arch/arm64/kernel/debug-monitors.c +++ b/arch/arm64/kernel/debug-monitors.c @@ -187,6 +187,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) { @@ -214,7 +256,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 @@ -226,11 +270,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; diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index 4d2c6f3..32d7fe6 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