From patchwork Tue Mar 3 08:47:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 11417413 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 13186924 for ; Tue, 3 Mar 2020 08:47:11 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E4CED20873 for ; Tue, 3 Mar 2020 08:47:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="pTa9kWHz"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="FGcEnuKN" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E4CED20873 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=10msjy6t5ekelpmwLl+h0FieXlwcB/pHijoNh3D/fd4=; b=pTa9kWHzzzFNj17nLkQEkJBL/N yWaNxROXNUCqteYAYcjoXS4rAsPOKVU14NjQMvnlfUlryCp3n0Av8ugDvMZSq8ecqMGIJyL+07K1b rVbsvjD/m7ARcUmqGhLvip0UytRLTydJpMfKjirCgTelZ+/Ahd5lNPVL2YHv3zCkSONsiJfz996mq QF5ODbt1p9aPrc+9V6EkC/BCY7q0HNRWWnP+fkf8kNID9qjzPkduHfOKJuJvj36ZDmNoQChDUQNNx 8h6n+ggfT6mZRpsYzl4WYRI2lVTGKzoi+ZsXpVdyYDArikleZy3Qg0Sh4LIONB52ZutizSjg1xRln lVKuAWYA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93Cm-0000GS-4Z; Tue, 03 Mar 2020 08:47:08 +0000 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93Cj-0000GB-Pj for linux-riscv@lists.infradead.org; Tue, 03 Mar 2020 08:47:07 +0000 Received: by mail-pl1-x642.google.com with SMTP id p7so1015657pli.5 for ; Tue, 03 Mar 2020 00:47:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id; bh=10msjy6t5ekelpmwLl+h0FieXlwcB/pHijoNh3D/fd4=; b=FGcEnuKNaPrGGdBPllfmusOwXCOON/Cgi4Go07bdmaMJ1KtPL2RfRyYR6s+/iqBz5f XVMNhmWHbk+n3Ccc8OVXyn6sFR1+gjZZf9zcEODQOfM8ar0Le24ED4Z1+TC4Nnr/rF74 T0Dxgqbt+zJ4MR+E8mFOcfZSzXkKTRb17s3iH7lsw4DEEc42OvR3jGwN257RYAYU3Unk LtuMyYwK2yxYS99tZ+wurmD3ODDTIdHWLBugnxUP2MzCGuTCNwHCnaXdiOcjf/LE3W64 UFXkkFsi8UHfqx+yJ6qYIFT/pcv/A1esPVj1DlTn5dDefyIh4twOaCBJE56R+JxXxmJc 9f2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=10msjy6t5ekelpmwLl+h0FieXlwcB/pHijoNh3D/fd4=; b=Lye9OIL7WjBqSUHVEfiG8Uxhg9wmZ1ARompnYYsdZuNOQNmjz5vnijkKjHangnbbu4 ++vJwq5Drm7eFNJlBjFklQHEVBA79bmq0NJU+bUBw6t8mL0UZ0UABE7dGQQNwOgxitHM DUOTLmVDxlnvCKqGEPWQUHlbDLXF0wFMW+iNikJ5wyPFirukaSS29m4ckEDyhdoxrEKR qZnnB9lbSe+SRzddj0tVfP6HyY6JRl+DuaD0bRizzJvYI/zrAtsml5xV5tz+Ao+r9Vq5 rtxP0XW4aBGp96V0VKrbdXoOT2kaadIiYfX5B6+M2jBpg5zXh3+piwlmYH4wBMYjjy7d SGeA== X-Gm-Message-State: ANhLgQ0PAvxK+QLwfPAXrbJfTrncjYaXHxd4y9Y+23GAE+JERMFzMtCp kq5xNArmC4ugqQfUtlJdRgfM/A== X-Google-Smtp-Source: ADFU+vuv2bjEfMr3F5uM7po57DVZsSXSb7F/AwEPDSu2e6tRem4u/H17BOrNNX1KpUDgskzagfixSg== X-Received: by 2002:a17:902:8b89:: with SMTP id ay9mr3182043plb.309.1583225224700; Tue, 03 Mar 2020 00:47:04 -0800 (PST) Received: from VincentChen-ThinkPad-T480s.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id v8sm1699217pjr.10.2020.03.03.00.47.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 03 Mar 2020 00:47:04 -0800 (PST) From: Vincent Chen To: paul.walmsley@sifive.com, palmer@dabbelt.com, jason.wessel@windriver.com, daniel.thompson@linaro.org, dianders@chromium.org Subject: [PATCH 1/5] kgdb: Add kgdb_has_hit_break function Date: Tue, 3 Mar 2020 16:47:00 +0800 Message-Id: <1583225220-26137-1-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200303_004705_840501_9F467F94 X-CRM114-Status: UNSURE ( 9.46 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.3 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:642 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vincent Chen , linux-riscv@lists.infradead.org Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org The break instruction in RISC-V does not have an immediate value field, so the kernel cannot identify the purpose of each trap exception through the opcode. This makes the existing identification schemes in other architecture unsuitable for the RISC-V kernel. To solve this problem, this patch adds kgdb_has_hit_break(), which can help RISC-V kernel identify the KGDB trap exception. Signed-off-by: Vincent Chen Reviewed-by: Palmer Dabbelt Acked-by: Daniel Thompson --- kernel/debug/debug_core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/kernel/debug/debug_core.c b/kernel/debug/debug_core.c index 2b7c9b67931d..01bc3eea3d4d 100644 --- a/kernel/debug/debug_core.c +++ b/kernel/debug/debug_core.c @@ -417,6 +417,18 @@ int kgdb_isremovedbreak(unsigned long addr) return 0; } +int kgdb_has_hit_break(unsigned long addr) +{ + int i; + + for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) { + if (kgdb_break[i].state == BP_ACTIVE && + kgdb_break[i].bpt_addr == addr) + return 1; + } + return 0; +} + int dbg_remove_all_break(void) { int error; From patchwork Tue Mar 3 08:47:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 11417415 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0DD0F924 for ; Tue, 3 Mar 2020 08:47:25 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DD47020873 for ; Tue, 3 Mar 2020 08:47:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="a99EsCNB"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="CjY/rmTT" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DD47020873 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=8dGI+yQaABcAe7jQ079Fj2BiuvdqP5YNeqrUXQauCKY=; b=a99EsCNBacXikN4PpzEsF7SSD+ ZIe64v2mC4yifr/3FElu8v2j3EdLZEzCm1pFsZlYBYRVVXetJiEGAbpQP0xSciV2utvnDa8jgcpj8 bSTNIFgsqyr560Gv05/I7dXD8TkIjDuXR5re7JM57k0Qb+7ZbYroBBWbSpDPDgA97VW7v175ezKXp tHyMndldxiFHlKL4jcKrEolem0ur+MfjD6ukN+uWSgzaDFs1gqWGS/WOSrfr5rvviUsioazGfUdye jCF3CfLqVlGWNQGYS64W4V6vggNBbyhe6guD8HGZA26+nh+s20K/NJoJypATV+rnbpr0EHLXL2UaF qF10BmOA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93Cy-0000Ke-TV; Tue, 03 Mar 2020 08:47:20 +0000 Received: from mail-pf1-x443.google.com ([2607:f8b0:4864:20::443]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93Cv-0000K4-If for linux-riscv@lists.infradead.org; Tue, 03 Mar 2020 08:47:19 +0000 Received: by mail-pf1-x443.google.com with SMTP id s1so1107691pfh.10 for ; Tue, 03 Mar 2020 00:47:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id; bh=8dGI+yQaABcAe7jQ079Fj2BiuvdqP5YNeqrUXQauCKY=; b=CjY/rmTT6vtITP7ljO8ZE06FmB+OEPbJZk1r4t4XuUba+UhE573lDxcMJcNcwnicG9 LC6MWaNpMsB38rq5cTDRzWfuC1I2S34DYcGZkmSgrsGqGWpz+B8aG9iwWPwJ0LGVvZQV V4HLfzAM2FcnQbmo5hsHfjbOI3GnPKgbPga1DAN6B8aX3Kdrweo7Z0kI54aWOfn+T6U/ 8TCxislE3jso9WY5guDdoxfxdDFuJ4JGed2y2M7AkOGUaTuvnsO2ZU19ZeR0c+AQ876A nntrj1fx1q+60LXU/xA0iZCDxS0yagSa4iSCBoYpIAqtuBYPQxrpRSbIClHtoYDhVwWm Elrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=8dGI+yQaABcAe7jQ079Fj2BiuvdqP5YNeqrUXQauCKY=; b=rx4i2O6JcTttwTnsxRzZUu33dw/8oY+RhqYoVjJFYHU+VIP2owZ6JJMjgHQSmAUr8P zNRh5X5750HgpBL46aMIf4e00g7Fnp2CWhKFRKhyC4MJfQlud5JER3hYJz160ITB/HS9 YH8pd7Igaz8Qet0Ks3y97ofL5/Hinolh2viIA06LyUs4Ys34ZRVfJnMqQX3Ouqm9bOg8 xY2FimkHpSWhpXN9461LJPN5HZOxmHfPYpJlqo/5z0m+zTqx4S7euNZtyuzA0dLFeuDn ZRHmQHtqkff5HAhfmNBIUzrIgh1PmjcLTt014baLoy6ZEpAR8/zwPGOngRaFdgrsZnt2 JtZg== X-Gm-Message-State: ANhLgQ1E51JYWqAQui8eyHcGRhOTAZ5QX15pRDZhl9+S1F1UXx8LXYHe bncazSaN+2h3Mj0+swu0vK3lXg== X-Google-Smtp-Source: ADFU+vvDnfWqfBLbvyUXx60sfBXGZQeHFUOY/AuoVmQ1cBu1n4KTkMWZ6fYj2ColqmwemY9LtJbR1Q== X-Received: by 2002:a63:9d04:: with SMTP id i4mr3104154pgd.294.1583225236797; Tue, 03 Mar 2020 00:47:16 -0800 (PST) Received: from VincentChen-ThinkPad-T480s.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id t142sm15334637pgb.31.2020.03.03.00.47.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 03 Mar 2020 00:47:16 -0800 (PST) From: Vincent Chen To: paul.walmsley@sifive.com, palmer@dabbelt.com, jason.wessel@windriver.com, daniel.thompson@linaro.org, dianders@chromium.org Subject: [PATCH 2/5] riscv: Add KGDB support Date: Tue, 3 Mar 2020 16:47:11 +0800 Message-Id: <1583225231-26187-1-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200303_004717_625493_FBA1CA48 X-CRM114-Status: GOOD ( 16.04 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.3 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:443 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vincent Chen , linux-riscv@lists.infradead.org Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org The skeleton of RISC-V KGDB port. Signed-off-by: Vincent Chen Reviewed-by: Palmer Dabbelt --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/Kbuild | 1 - arch/riscv/include/asm/kdebug.h | 12 +++ arch/riscv/include/asm/kgdb.h | 71 ++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/kgdb.c | 199 ++++++++++++++++++++++++++++++++++++++++ arch/riscv/kernel/traps.c | 5 + 7 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/kdebug.h create mode 100644 arch/riscv/include/asm/kgdb.h create mode 100644 arch/riscv/kernel/kgdb.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 73f029eae0cc..108794f4aa45 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -66,6 +66,7 @@ config RISCV select ARCH_HAS_GCOV_PROFILE_ALL select HAVE_COPY_THREAD_TLS select HAVE_ARCH_KASAN if MMU && 64BIT + select HAVE_ARCH_KGDB config ARCH_MMAP_RND_BITS_MIN default 18 if 64BIT diff --git a/arch/riscv/include/asm/Kbuild b/arch/riscv/include/asm/Kbuild index ec0ca8c6ab64..625e46cbdec8 100644 --- a/arch/riscv/include/asm/Kbuild +++ b/arch/riscv/include/asm/Kbuild @@ -15,7 +15,6 @@ generic-y += hardirq.h generic-y += hw_irq.h generic-y += irq_regs.h generic-y += irq_work.h -generic-y += kdebug.h generic-y += kmap_types.h generic-y += kvm_para.h generic-y += local.h diff --git a/arch/riscv/include/asm/kdebug.h b/arch/riscv/include/asm/kdebug.h new file mode 100644 index 000000000000..85ac00411f6e --- /dev/null +++ b/arch/riscv/include/asm/kdebug.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _ASM_ARC_KDEBUG_H +#define _ASM_ARC_KDEBUG_H + +enum die_val { + DIE_UNUSED, + DIE_TRAP, + DIE_OOPS +}; + +#endif diff --git a/arch/riscv/include/asm/kgdb.h b/arch/riscv/include/asm/kgdb.h new file mode 100644 index 000000000000..e3b455932506 --- /dev/null +++ b/arch/riscv/include/asm/kgdb.h @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __ASM_KGDB_H_ +#define __ASM_KGDB_H_ + +#ifdef __KERNEL__ + +#define GDB_SIZEOF_REG sizeof(unsigned long) + +#define DBG_MAX_REG_NUM (33) +#define NUMREGBYTES ((DBG_MAX_REG_NUM) * GDB_SIZEOF_REG) +#define CACHE_FLUSH_IS_SAFE 1 +#define BUFMAX 2048 +#ifdef CONFIG_RISCV_ISA_C +#define BREAK_INSTR_SIZE 2 +#else +#define BREAK_INSTR_SIZE 4 +#endif +#define CACHE_FLUSH_IS_SAFE 1 + +#ifndef __ASSEMBLY__ + +extern int kgdb_has_hit_break(unsigned long addr); +extern unsigned long kgdb_compiled_break; + +static inline void arch_kgdb_breakpoint(void) +{ + asm(".global kgdb_compiled_break\n" + ".option norvc\n" + "kgdb_compiled_break: ebreak\n" + ".option rvc\n"); +} + +#endif /* !__ASSEMBLY__ */ + +#define PT_REG_ZERO "zero" +#define PT_REG_RA "ra" +#define PT_REG_SP "sp" +#define PT_REG_GP "gp" +#define PT_REG_TP "tp" +#define PT_REG_T0 "t0" +#define PT_REG_T1 "t1" +#define PT_REG_T2 "t2" +#define PT_REG_FP "fp" +#define PT_REG_S1 "s1" +#define PT_REG_A0 "a0" +#define PT_REG_A1 "a1" +#define PT_REG_A2 "a2" +#define PT_REG_A3 "a3" +#define PT_REG_A4 "a4" +#define PT_REG_A5 "a5" +#define PT_REG_A6 "a6" +#define PT_REG_A7 "a7" +#define PT_REG_S2 "s2" +#define PT_REG_S3 "s3" +#define PT_REG_S4 "s4" +#define PT_REG_S5 "s5" +#define PT_REG_S6 "s6" +#define PT_REG_S7 "s7" +#define PT_REG_S8 "s8" +#define PT_REG_S9 "s9" +#define PT_REG_S10 "s10" +#define PT_REG_S11 "s11" +#define PT_REG_T3 "t3" +#define PT_REG_T4 "t4" +#define PT_REG_T5 "t5" +#define PT_REG_T6 "t6" +#define PT_REG_SEPC "pc" + +#endif +#endif diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index f40205cb9a22..8956543f7d6b 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -42,5 +42,6 @@ obj-$(CONFIG_PERF_EVENTS) += perf_event.o obj-$(CONFIG_PERF_EVENTS) += perf_callchain.o obj-$(CONFIG_HAVE_PERF_REGS) += perf_regs.o obj-$(CONFIG_RISCV_SBI) += sbi.o +obj-$(CONFIG_KGDB) += kgdb.o clean: diff --git a/arch/riscv/kernel/kgdb.c b/arch/riscv/kernel/kgdb.c new file mode 100644 index 000000000000..0a3fe5d672f1 --- /dev/null +++ b/arch/riscv/kernel/kgdb.c @@ -0,0 +1,199 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include +#include +#include + +enum { + NOT_KGDB_BREAK = 0, + KGDB_SW_BREAK, + KGDB_COMPILED_BREAK, +}; + +struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = { + {PT_REG_ZERO, GDB_SIZEOF_REG, -1}, + {PT_REG_RA, GDB_SIZEOF_REG, offsetof(struct pt_regs, ra)}, + {PT_REG_SP, GDB_SIZEOF_REG, offsetof(struct pt_regs, sp)}, + {PT_REG_GP, GDB_SIZEOF_REG, offsetof(struct pt_regs, gp)}, + {PT_REG_TP, GDB_SIZEOF_REG, offsetof(struct pt_regs, tp)}, + {PT_REG_T0, GDB_SIZEOF_REG, offsetof(struct pt_regs, t0)}, + {PT_REG_T1, GDB_SIZEOF_REG, offsetof(struct pt_regs, t1)}, + {PT_REG_T2, GDB_SIZEOF_REG, offsetof(struct pt_regs, t2)}, + {PT_REG_FP, GDB_SIZEOF_REG, offsetof(struct pt_regs, s0)}, + {PT_REG_S1, GDB_SIZEOF_REG, offsetof(struct pt_regs, a1)}, + {PT_REG_A0, GDB_SIZEOF_REG, offsetof(struct pt_regs, a0)}, + {PT_REG_A1, GDB_SIZEOF_REG, offsetof(struct pt_regs, a1)}, + {PT_REG_A2, GDB_SIZEOF_REG, offsetof(struct pt_regs, a2)}, + {PT_REG_A3, GDB_SIZEOF_REG, offsetof(struct pt_regs, a3)}, + {PT_REG_A4, GDB_SIZEOF_REG, offsetof(struct pt_regs, a4)}, + {PT_REG_A5, GDB_SIZEOF_REG, offsetof(struct pt_regs, a5)}, + {PT_REG_A6, GDB_SIZEOF_REG, offsetof(struct pt_regs, a6)}, + {PT_REG_A7, GDB_SIZEOF_REG, offsetof(struct pt_regs, a7)}, + {PT_REG_S2, GDB_SIZEOF_REG, offsetof(struct pt_regs, s2)}, + {PT_REG_S3, GDB_SIZEOF_REG, offsetof(struct pt_regs, s3)}, + {PT_REG_S4, GDB_SIZEOF_REG, offsetof(struct pt_regs, s4)}, + {PT_REG_S5, GDB_SIZEOF_REG, offsetof(struct pt_regs, s5)}, + {PT_REG_S6, GDB_SIZEOF_REG, offsetof(struct pt_regs, s6)}, + {PT_REG_S7, GDB_SIZEOF_REG, offsetof(struct pt_regs, s7)}, + {PT_REG_S8, GDB_SIZEOF_REG, offsetof(struct pt_regs, s8)}, + {PT_REG_S9, GDB_SIZEOF_REG, offsetof(struct pt_regs, s9)}, + {PT_REG_S10, GDB_SIZEOF_REG, offsetof(struct pt_regs, s10)}, + {PT_REG_S11, GDB_SIZEOF_REG, offsetof(struct pt_regs, s11)}, + {PT_REG_T3, GDB_SIZEOF_REG, offsetof(struct pt_regs, t3)}, + {PT_REG_T4, GDB_SIZEOF_REG, offsetof(struct pt_regs, t4)}, + {PT_REG_T5, GDB_SIZEOF_REG, offsetof(struct pt_regs, t5)}, + {PT_REG_T6, GDB_SIZEOF_REG, offsetof(struct pt_regs, t6)}, + {PT_REG_SEPC, GDB_SIZEOF_REG, offsetof(struct pt_regs, epc)}, +}; + +char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs) +{ + if (regno >= DBG_MAX_REG_NUM || regno < 0) + return NULL; + + if (dbg_reg_def[regno].offset != -1) + memcpy(mem, (void *)regs + dbg_reg_def[regno].offset, + dbg_reg_def[regno].size); + else + memset(mem, 0, dbg_reg_def[regno].size); + return dbg_reg_def[regno].name; +} + +int dbg_set_reg(int regno, void *mem, struct pt_regs *regs) +{ + if (regno >= DBG_MAX_REG_NUM || regno < 0) + return -EINVAL; + + if (dbg_reg_def[regno].offset != -1) + memcpy((void *)regs + dbg_reg_def[regno].offset, mem, + dbg_reg_def[regno].size); + return 0; +} + +void +sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task) +{ + /* Initialize to zero */ + memset((char *)gdb_regs, 0, NUMREGBYTES); + + gdb_regs[2] = task->thread.sp; + gdb_regs[8] = task->thread.s[0]; + gdb_regs[9] = task->thread.s[1]; + gdb_regs[18] = task->thread.s[2]; + gdb_regs[19] = task->thread.s[3]; + gdb_regs[20] = task->thread.s[4]; + gdb_regs[21] = task->thread.s[5]; + gdb_regs[22] = task->thread.s[6]; + gdb_regs[23] = task->thread.s[7]; + gdb_regs[24] = task->thread.s[8]; + gdb_regs[25] = task->thread.s[10]; + gdb_regs[26] = task->thread.s[11]; + gdb_regs[32] = task->thread.ra; +} + +void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc) +{ + regs->epc = pc; +} + +static inline void kgdb_arch_update_addr(struct pt_regs *regs, + char *remcom_in_buffer) +{ + unsigned long addr; + char *ptr; + + ptr = &remcom_in_buffer[1]; + if (kgdb_hex2long(&ptr, &addr)) + regs->epc = addr; +} + +int kgdb_arch_handle_exception(int vector, int signo, int err_code, + char *remcom_in_buffer, char *remcom_out_buffer, + struct pt_regs *regs) +{ + int err = 0; + + switch (remcom_in_buffer[0]) { + case 'c': + case 'D': + case 'k': + if (remcom_in_buffer[0] == 'c') + kgdb_arch_update_addr(regs, remcom_in_buffer); + atomic_set(&kgdb_cpu_doing_single_step, -1); + kgdb_single_step = 0; + break; + default: + err = -1; + } + + return err; +} + +int kgdb_riscv_kgdbbreak(unsigned long addr) +{ + if (atomic_read(&kgdb_setting_breakpoint)) + if (addr == (unsigned long)&kgdb_compiled_break) + return KGDB_COMPILED_BREAK; + + return kgdb_has_hit_break(addr); +} + +static int kgdb_riscv_notify(struct notifier_block *self, unsigned long cmd, + void *ptr) +{ + struct die_args *args = (struct die_args *)ptr; + struct pt_regs *regs = args->regs; + unsigned long flags; + int type; + + if (user_mode(regs)) + return NOTIFY_DONE; + + type = kgdb_riscv_kgdbbreak(regs->epc); + if (type == NOT_KGDB_BREAK && cmd == DIE_TRAP) + return NOTIFY_DONE; + + local_irq_save(flags); + if (kgdb_handle_exception(1, args->signr, cmd, regs)) + return NOTIFY_DONE; + + if (type == KGDB_COMPILED_BREAK) + regs->epc += 4; + + local_irq_restore(flags); + + return NOTIFY_STOP; +} + +static struct notifier_block kgdb_notifier = { + .notifier_call = kgdb_riscv_notify, +}; + +int kgdb_arch_init(void) +{ + register_die_notifier(&kgdb_notifier); + + return 0; +} + +void kgdb_arch_exit(void) +{ + unregister_die_notifier(&kgdb_notifier); +} + +/* + * Global data + */ +#ifdef CONFIG_RISCV_ISA_C +const struct kgdb_arch arch_kgdb_ops = { + .gdb_bpt_instr = {0x02, 0x90}, /* c.ebreak */ +}; +#else +const struct kgdb_arch arch_kgdb_ops = { + .gdb_bpt_instr = {0x73, 0x00, 0x10, 0x00}, /* ebreak */ +}; +#endif diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index f4cad5163bf2..7e017c2131cf 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -125,6 +125,11 @@ asmlinkage __visible void do_trap_break(struct pt_regs *regs) { if (user_mode(regs)) force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc); +#ifdef CONFIG_KGDB + else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP) + == NOTIFY_STOP) + return; +#endif else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN) regs->epc += get_break_insn_length(regs->epc); else From patchwork Tue Mar 3 08:47:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 11417417 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CBF23924 for ; Tue, 3 Mar 2020 08:47:54 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A77B720873 for ; Tue, 3 Mar 2020 08:47:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="NpLdaJ/O"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="B3BkhLJY" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A77B720873 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=5f2ikCnnHJJs8RgHgk1kmK0wqdQGHiofgOLX/mam8vM=; b=NpLdaJ/OzP0hq3SMTW51eRzZDZ vUveU61gQZ+QWQJXboXodp1Ct2AgtFYqaOZxruVuWo/UhtDfllYqZUJCovame+oZQj6+05o6Gp+Sz JysmWW8T7QerJ/KOB8M3tBdej8TqC/FkM13qAzXqXCBForeF8n4IREaYgO3j+b55x+qF2RX52Vyif /r4Hay9u89WpvHvpfwv85lqf8rFG2CVrNeY4oJ4ykbleMJbLsOOS3wSp+gTQyQrQNwQ7CqKsOu3w6 rjEJREh/LdCCfgwLhlGpb40xOHYS+nP7YlWXgudY9tG+MISpJGpKgdxgCCT/58URR6CYiN2wQoirc b1XCrLjQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93DT-0000PY-UQ; Tue, 03 Mar 2020 08:47:51 +0000 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93DR-0000PC-76 for linux-riscv@lists.infradead.org; Tue, 03 Mar 2020 08:47:50 +0000 Received: by mail-pf1-x441.google.com with SMTP id i19so1128560pfa.2 for ; Tue, 03 Mar 2020 00:47:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id; bh=5f2ikCnnHJJs8RgHgk1kmK0wqdQGHiofgOLX/mam8vM=; b=B3BkhLJYazKwKoW9hmNFqT9XsWEOybf+G8am8aHdXpnOd78Txob0NZ40jjzX7hcvIZ AqeIr0k6ysCRVP2+XzaNovba4FiCHvvRMFmE/QMbAeGWwl3G52NzPXq355ZSBYCXX8l6 wqzdDJ50U8YVshXVkqoLiDj4uAWS3dxb/y7l0Efn+3pHlLV4JmilNoqI0LAOqMsgTWhm pog3ZoXshrxPnz79euSyncO2apbt/idJfss2G3voGr4iETl/6o/BCoCveMRiJezuvtn5 GhvYgvqVXSbi5494CLPOwGGPTUBtDtWPQPChXnd6STKgTlijNqakRDjOdGnDx1up5eDW TBOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=5f2ikCnnHJJs8RgHgk1kmK0wqdQGHiofgOLX/mam8vM=; b=GFSLiI07o86/+PuvUaYEmDduQMZ7dQzM0U9mrdau6ZEfMWXkcGu1qPxj0gpNTuLk1e cxD7ok04A0EkbTnEY+jEGmqB1iKw/s+47Xr2hORCPtdhBkbiCuZ61H7GAdzYIAPUl9+8 8SBxw2bW7AXbuW7HBtKTxZVMDx/p92JUkmP1eUoU14cACiW760ARa7chisWSHPXs5Pim AxVZ5nTgma2yEq9doNQUpbp2LUxail0kU9LXlPmoH5uxTPHdLIidDY6aTlXXcPLWT+Sy coTrr9biiXvTPv6xsX3GXTf3KJ0BwAvOqNbn3WUIkK24GGjHfqhpde1iK984IBaKbES7 084A== X-Gm-Message-State: ANhLgQ1XzC/QeQUg4usWGbHvCsL1Ko7cUTz7l4dPn4I8zY5E1n3KBGhF f3c+Djcfv8h3A2pPJUYXk9uFdg== X-Google-Smtp-Source: ADFU+vuUCgNBGlrIC6q0jXkCOeMDVk6v4zCvLLNF+fisxd/d/ux7pVwYJODkXoYYMQMCHN7zBoB2eg== X-Received: by 2002:a63:cb4a:: with SMTP id m10mr3053257pgi.259.1583225268322; Tue, 03 Mar 2020 00:47:48 -0800 (PST) Received: from VincentChen-ThinkPad-T480s.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id q13sm23825883pgh.30.2020.03.03.00.47.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 03 Mar 2020 00:47:47 -0800 (PST) From: Vincent Chen To: paul.walmsley@sifive.com, palmer@dabbelt.com, jason.wessel@windriver.com, daniel.thompson@linaro.org, dianders@chromium.org Subject: [PATCH 3/5] kgdb: enable arch to handle more query packets Date: Tue, 3 Mar 2020 16:47:43 +0800 Message-Id: <1583225263-26245-1-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200303_004749_259913_DBE6893F X-CRM114-Status: GOOD ( 10.60 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.3 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:441 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vincent Chen , linux-riscv@lists.infradead.org Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org KGDB only supports parts of GDB query packets. Add kgdb_arch_cmd_query() hook function to enable arch to handle more query packets such as the qSupported packet. Signed-off-by: Vincent Chen --- include/linux/kgdb.h | 10 ++++++++++ kernel/debug/gdbstub.c | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/include/linux/kgdb.h b/include/linux/kgdb.h index b072aeb1fd78..bbb35557f76d 100644 --- a/include/linux/kgdb.h +++ b/include/linux/kgdb.h @@ -177,6 +177,16 @@ kgdb_arch_handle_exception(int vector, int signo, int err_code, struct pt_regs *regs); /** + * kgdb_arch_handle_exception - Handle architecture specific query packet. + * @remcom_in_buffer: The buffer of the packet we have read. + * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into. + */ + +extern void +kgdb_arch_cmd_query(char *remcom_in_buffer, + char *remcom_out_buffer); + +/** * kgdb_call_nmi_hook - Call kgdb_nmicallback() on the current CPU * @ignored: This parameter is only here to match the prototype. * diff --git a/kernel/debug/gdbstub.c b/kernel/debug/gdbstub.c index 4b280fc7dd67..2b2e7b99edcc 100644 --- a/kernel/debug/gdbstub.c +++ b/kernel/debug/gdbstub.c @@ -694,6 +694,9 @@ static int gdb_cmd_reboot(struct kgdb_state *ks) return 0; } +void __weak kgdb_arch_cmd_query(char *remcom_in_buffer, + char *remcom_out_buffer); + /* Handle the 'q' query packets */ static void gdb_cmd_query(struct kgdb_state *ks) { @@ -792,6 +795,9 @@ static void gdb_cmd_query(struct kgdb_state *ks) } break; #endif + default: + kgdb_arch_cmd_query(remcom_in_buffer, remcom_out_buffer); + } } From patchwork Tue Mar 3 08:47:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 11417419 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 37740924 for ; Tue, 3 Mar 2020 08:48:06 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 156F7215A4 for ; Tue, 3 Mar 2020 08:48:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="WFSUdcGk"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="akKCBXxe" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 156F7215A4 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=B4ptpoEWJXXD28qu1l9ZNm7p+E+C4og8iM3qtgMmqGQ=; b=WFSUdcGkB/5UjldPQ6gaotRoCh LF4Gnh/nATBCmDkR5Zm6TeNCRuSIzVk+AOJ4v3m33UmdypoxtKrqI/1M9gJ23w/ezKz1QIwWi5Eww wm86VLQ95h8mjw1Sj39fC86LKbSKg4VJ5Yz92ZuftQxP6wijycV9oQLxqSD8bZFTmvZbY5H0XQKW+ 6KEhqw1HJIdRclFOkJlhEK/S6FQGdGMN8kYkcWlVkzmK1fInryMwj0oL3ozuJuaxqiUX+Ldi4k2ka DIUv7tZ85y9ANLG/k4sKRJgV1i+CYztDyIcU9MofqgTAejf5+XS5e4pabhmpJIz1Ko9gfkC9dGYSw Ez4HfBYA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93Df-0000VR-13; Tue, 03 Mar 2020 08:48:03 +0000 Received: from mail-pj1-x1044.google.com ([2607:f8b0:4864:20::1044]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93Dc-0000U4-J3 for linux-riscv@lists.infradead.org; Tue, 03 Mar 2020 08:48:02 +0000 Received: by mail-pj1-x1044.google.com with SMTP id cx7so1042729pjb.3 for ; Tue, 03 Mar 2020 00:48:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id; bh=B4ptpoEWJXXD28qu1l9ZNm7p+E+C4og8iM3qtgMmqGQ=; b=akKCBXxehJLgpoQdycp/W3Q2SvtoKRkAv0o5+3pL2hjimABRLEQuPI6giOYOOOYRxS +XXIDBNBVIdHl96umVeehwXeDNpGhZqOEbiFll1Kpfpuw/gjFAoM+wR2Vhv3wXf95gXs Ky1Av1LrlYUStpZ3qkXrWUMdZjSAyVmbnJnpaOJ3LXoprqyULyG7RQbVObXSeRY7r/uB kWYa+miV0tXElgI0gpu8Er5c46W1xNqxFdaPIZ87MRfKQcCr/MxLq8vHAxCQtRYwDv1J PzozRxpuJCLeShgIHWuizvacvg1CwZU0KHQpcvMXy6oPsoPHJLuyGlk5UBSFSImc016g EZjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=B4ptpoEWJXXD28qu1l9ZNm7p+E+C4og8iM3qtgMmqGQ=; b=hmLnIuwARAjCRWDISNeYnstkqwMabRX5PS6JUNrvvs6ZBybaMIOaTOzK/3i+qngNvy FfBuiFO5p/4ccVG2ZN/l0sy5m07Uw2R4KWbCPfEZpNKos1Tqd+XDfgmY2IdXuMF74yUQ 1lIx+DwVfrK2Rl7d98n9yU5wpgeNUgKST2NWBFwgxRRHs3pJrBTVxZbi2P239jkN5SUt DVMKs/oJDrGtOKdvypV0YQzw4WbRnReO7iTBp/ljLxjSJ8Gz/Svq6CKotoI9aJlF0cTD MjzEunX5pssMJcA7VPnUksyveFDjCoTuCycbItJRYwgw6a127ObxKdc07Fvw5YF3hFzA Tapg== X-Gm-Message-State: ANhLgQ3TjrkZPwJK0jPTK5JCWfRFxUp0Ryveek5KO5baJ2wIXakQqEvV vwfIcNxboJPtLWiqypCF2OxYcA== X-Google-Smtp-Source: ADFU+vvKskS9J2cyiuhkOK/aoDP53HRRMvr12ucHsrfd4V9fzNCWoxwRWAR8JygzXIRIRcMFxWPhOw== X-Received: by 2002:a17:902:b485:: with SMTP id y5mr3086150plr.4.1583225279559; Tue, 03 Mar 2020 00:47:59 -0800 (PST) Received: from VincentChen-ThinkPad-T480s.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id t11sm1806469pjo.21.2020.03.03.00.47.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 03 Mar 2020 00:47:59 -0800 (PST) From: Vincent Chen To: paul.walmsley@sifive.com, palmer@dabbelt.com, jason.wessel@windriver.com, daniel.thompson@linaro.org, dianders@chromium.org Subject: [PATCH 4/5] riscv: Use the XML target descriptions to support system registers report Date: Tue, 3 Mar 2020 16:47:54 +0800 Message-Id: <1583225274-26292-1-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200303_004800_634030_772B52E9 X-CRM114-Status: GOOD ( 13.29 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.3 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:1044 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vincent Chen , linux-riscv@lists.infradead.org Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org The sstatus, badaddr and scause registers belonged to the thread context, and KGDB can obtain their contents from pt_regs in each trap. However, the sequential number of these registers is far from the general purpose registers. It causes riscv to report many trivial middle registers in the reply packets. In order to solve this problem, the GDB query mechanism was introduced to enable KGDB to customize the reported register list through the XML target descriptions. Signed-off-by: Vincent Chen --- arch/riscv/include/asm/gdb_xml.h | 60 ++++++++++++++++++++++++++++++++++++++++ arch/riscv/include/asm/kgdb.h | 5 +++- arch/riscv/kernel/kgdb.c | 27 ++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/gdb_xml.h diff --git a/arch/riscv/include/asm/gdb_xml.h b/arch/riscv/include/asm/gdb_xml.h new file mode 100644 index 000000000000..42c31f11265c --- /dev/null +++ b/arch/riscv/include/asm/gdb_xml.h @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +static const char gdb_query_pkt[10] = "Supported:"; + +static const char gdb_reply_feature[64] = + "PacketSize=2048;qXfer:features:read+;"; + +static const char gdb_xfer_read_target[25] = "Xfer:features:read:target"; + +static const char gdb_xfer_read_cpuxml[38] = + "Xfer:features:read:riscv-64bit-cpu.xml"; + +static const char gdb_target_desc[256] = +"l" +"" +"" +"" +""; + +static const char gdb_cpuxml[2048] = +"l" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +""; diff --git a/arch/riscv/include/asm/kgdb.h b/arch/riscv/include/asm/kgdb.h index e3b455932506..67078aec867f 100644 --- a/arch/riscv/include/asm/kgdb.h +++ b/arch/riscv/include/asm/kgdb.h @@ -7,7 +7,7 @@ #define GDB_SIZEOF_REG sizeof(unsigned long) -#define DBG_MAX_REG_NUM (33) +#define DBG_MAX_REG_NUM (36) #define NUMREGBYTES ((DBG_MAX_REG_NUM) * GDB_SIZEOF_REG) #define CACHE_FLUSH_IS_SAFE 1 #define BUFMAX 2048 @@ -66,6 +66,9 @@ static inline void arch_kgdb_breakpoint(void) #define PT_REG_T5 "t5" #define PT_REG_T6 "t6" #define PT_REG_SEPC "pc" +#define PT_REG_SSTATUS "sstatus" +#define PT_REG_STVAL "stval" +#define PT_REG_SCAUSE "scause" #endif #endif diff --git a/arch/riscv/kernel/kgdb.c b/arch/riscv/kernel/kgdb.c index 0a3fe5d672f1..01af9fb43150 100644 --- a/arch/riscv/kernel/kgdb.c +++ b/arch/riscv/kernel/kgdb.c @@ -7,6 +7,7 @@ #include #include #include +#include enum { NOT_KGDB_BREAK = 0, @@ -48,6 +49,9 @@ struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = { {PT_REG_T5, GDB_SIZEOF_REG, offsetof(struct pt_regs, t5)}, {PT_REG_T6, GDB_SIZEOF_REG, offsetof(struct pt_regs, t6)}, {PT_REG_SEPC, GDB_SIZEOF_REG, offsetof(struct pt_regs, epc)}, + {PT_REG_SSTATUS, GDB_SIZEOF_REG, offsetof(struct pt_regs, status)}, + {PT_REG_STVAL, GDB_SIZEOF_REG, offsetof(struct pt_regs, badaddr)}, + {PT_REG_SCAUSE, GDB_SIZEOF_REG, offsetof(struct pt_regs, cause)}, }; char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs) @@ -100,6 +104,29 @@ void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc) regs->epc = pc; } +void kgdb_arch_cmd_query(char *remcom_in_buffer, char *remcom_out_buffer) +{ + switch (remcom_in_buffer[1]) { + case 'S': + if (!strncmp(remcom_in_buffer + 1, + gdb_query_pkt, sizeof(gdb_query_pkt))) + strcpy(remcom_out_buffer, gdb_reply_feature); + break; + case 'X': + if (!strncmp(remcom_in_buffer + 1, + gdb_xfer_read_target, + sizeof(gdb_xfer_read_target))) + strcpy(remcom_out_buffer, gdb_target_desc); + else if (!strncmp(remcom_in_buffer + 1, + gdb_xfer_read_cpuxml, + sizeof(gdb_xfer_read_cpuxml))) + strcpy(remcom_out_buffer, gdb_cpuxml); + break; + default: + break; + } +} + static inline void kgdb_arch_update_addr(struct pt_regs *regs, char *remcom_in_buffer) { From patchwork Tue Mar 3 08:48:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 11417421 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3676C138D for ; Tue, 3 Mar 2020 08:48:18 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 10A98215A4 for ; Tue, 3 Mar 2020 08:48:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="uL7DcLOZ"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="FNMt0uhC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 10A98215A4 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=4Hi0PP648pqz5WAIMtOxKekD1mZWnTv05En02OgpM58=; b=uL7DcLOZx5GDukKHRM/ciTdfdO JFCe93D1lf2fIqd0zXblzu1BcdVUT6al0+NuHHxr4gRknvsmLyIls4wEsmN8DW50WDSNirV8TZWhA lqIsGRDpC2PzY3Gv1P/1lRY4deSjizxc8buN71sZa6/fztqjhPIxSRjNYDALmeoHQm6nFSIGf0oAB /0FjeP+0lv6IKqXJOTOgg1m9ZmwqcY4Yxme+FKfiIRVZYE7zOIxcs1/oYzdusVzgCPvjkgE3dRKVk +zr/YwnBiZFdrLAY+2+xw6Vw1UHH9xAtVr/tVSyjQk0oEHrMN97FnIr+Ty91mxoTtFTTGN60K8JYO 6RnwTtcw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93Dr-0000az-25; Tue, 03 Mar 2020 08:48:15 +0000 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1j93Dn-0000a5-PW for linux-riscv@lists.infradead.org; Tue, 03 Mar 2020 08:48:13 +0000 Received: by mail-pg1-x544.google.com with SMTP id b1so1211437pgm.8 for ; Tue, 03 Mar 2020 00:48:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id; bh=4Hi0PP648pqz5WAIMtOxKekD1mZWnTv05En02OgpM58=; b=FNMt0uhCDA8lcTNPsJm1ysbqOdGAskcyuoqpcsomPlYrYaqWGiTRuu9sloj4doFAyN CUxKgubLV33TSYPFqYSTcupYwh6sD8Tql3guxbY0hHlwZ/ecnqIwtH2u4tjLKxwrDxgJ Qh+GcWVhPW8L2iVO47LJAS1IIzajfIKkMeLqxxi8RYuyWnKP33C0w8iTf0VMxZxcH/09 gCg8aWVnsdw0nMXrNwco0idy+JLN9AOe7XTg0lgm0KbGwb/4E140LZNLOjWVvG4B7WF0 f8xXa7veyqCB/3t8wlg90jS4u4F/9wiput9mTCwH7VtaEcmC6t3zKKA7ysNYq6+yCEzT +yvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=4Hi0PP648pqz5WAIMtOxKekD1mZWnTv05En02OgpM58=; b=E+sMLlHaDGfE5Q85itgYMlZc7AVWb4xLdUfMKRghhYuoL7N3zG4ULeuBeoYtrxnwjw lhiT8QMwlM7y2zjQYu8nDjQLJXKmQnrqx/zS4Yj4ymPchnQWAUMIykraWH170PZ9N8r4 COBvNpLGtxP8XI/jbNu9jvMvIMQ0vbDDlm+bNtWrZtoVjnWI23xc4zun5ZlbApYAzHX3 EUqVGOGwevGFFK75R0dvaAFix69e/KQNrkW60EKY+rHwmc3+JHHeZo1/zI++IvFsg1GR l5YslE0rtzQJ1Hg4dcf/n3aNucGM8JlTdWaxbSqjo/lP9tIYyOFIUPszeVUpdO9bmSTa n4GA== X-Gm-Message-State: ANhLgQ2qt7Q083KTj6CxNgWknJxUt71zqpMvciAyI9pH5allzsZ/Bhm6 c09cDroh6bvDCARk40em8ki3Zg== X-Google-Smtp-Source: ADFU+vslxajIO2sKe/uX1wb8utG9/S6d1GdIR7b9n8oyh3URR9eWt/qqjd95Xs59wcSEQ6n8ZTv2iQ== X-Received: by 2002:aa7:9606:: with SMTP id q6mr3131980pfg.247.1583225290883; Tue, 03 Mar 2020 00:48:10 -0800 (PST) Received: from VincentChen-ThinkPad-T480s.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id 196sm23828576pfy.86.2020.03.03.00.48.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 03 Mar 2020 00:48:10 -0800 (PST) From: Vincent Chen To: paul.walmsley@sifive.com, palmer@dabbelt.com, jason.wessel@windriver.com, daniel.thompson@linaro.org, dianders@chromium.org Subject: [PATCH 5/5] riscv: Add SW single-step support for KDB Date: Tue, 3 Mar 2020 16:48:06 +0800 Message-Id: <1583225286-26341-1-git-send-email-vincent.chen@sifive.com> X-Mailer: git-send-email 2.7.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200303_004811_827804_DA0A2C4B X-CRM114-Status: GOOD ( 16.45 ) X-Spam-Score: 3.3 (+++) X-Spam-Report: SpamAssassin version 3.4.3 on bombadil.infradead.org summary: Content analysis details: (3.3 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:544 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 2.2 DRUGS_ERECTILE Refers to an erectile drug 1.3 DRUGS_ERECTILE_OBFU Obfuscated reference to an erectile drug X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Vincent Chen , linux-riscv@lists.infradead.org Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org In KGDB, the GDB in the host is responsible for the single-step operation of the software. In other words, KGDB does not need to derive the next pc address when performing a software single-step operation. KGDB just inserts the break instruction at the indicated address according to the GDB instructions. This approach does not work in KDB because the GDB does not involve the KDB process. Therefore, this patch provides KDB a software single-step mechanism to use. Signed-off-by: Vincent Chen --- arch/riscv/kernel/kgdb.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 242 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kernel/kgdb.c b/arch/riscv/kernel/kgdb.c index 01af9fb43150..88080991fa85 100644 --- a/arch/riscv/kernel/kgdb.c +++ b/arch/riscv/kernel/kgdb.c @@ -13,8 +13,235 @@ enum { NOT_KGDB_BREAK = 0, KGDB_SW_BREAK, KGDB_COMPILED_BREAK, + KGDB_SW_SINGLE_STEP }; +static unsigned long stepped_address; +static unsigned int stepped_opcode; + +#define RV_IMM_SIGN(x) (-(((x) >> 31) & 1)) +#define RV_X(X, s, n) (((X) >> (s)) & ((1 << (n)) - 1)) +#define EXTRACT_UJTYPE_IMM(x) \ + ({typeof(x) x_ = (x); \ + (RV_X(x_, 21, 10) << 1) | (RV_X(x_, 20, 1) << 11) | \ + (RV_X(x_, 12, 8) << 12) | (RV_IMM_SIGN(x_) << 20); }) +#define EXTRACT_ITYPE_IMM(x) \ + ({typeof(x) x_ = (x); \ + RV_X(x_, 20, 12) | (RV_IMM_SIGN(x_) << 12); }) +#define EXTRACT_RVC_J_IMM(x) \ + ({typeof(x) x_ = (x); \ + (RV_X(x_, 3, 3) << 1) | (RV_X(x_, 11, 1) << 4) | \ + (RV_X(x_, 2, 1) << 5) | (RV_X(x_, 7, 1) << 6) | \ + (RV_X(x_, 6, 1) << 7) | (RV_X(x_, 9, 2) << 8) | \ + (RV_X(x_, 8, 1) << 10) | (-RV_X(x_, 12, 1) << 11); }) +#define EXTRACT_SBTYPE_IMM(x) \ + ({typeof(x) x_ = (x); \ + (RV_X(x_, 8, 4) << 1) | (RV_X(x_, 25, 6) << 5) | \ + (RV_X(x_, 7, 1) << 11) | (RV_IMM_SIGN(x_) << 12); }) +#define EXTRACT_RVC_B_IMM(x) \ + ({typeof(x) x_ = (x); \ + (RV_X(x_, 3, 2) << 1) | (RV_X(x_, 10, 2) << 3) | \ + (RV_X(x_, 2, 1) << 5) | (RV_X(x_, 5, 2) << 6) | \ + (-RV_X(x_, 12, 1) << 8); }) + +#define MATCH_JALR 0x67 +#define MASK_JALR 0x707f +#define MATCH_JAL 0x6f +#define MASK_JAL 0x7f +#define MATCH_C_JALR 0x9002 +#define MASK_C_JALR 0xf07f +#define MATCH_C_JR 0x8002 +#define MASK_C_JR 0xf07f +#define MATCH_C_JAL 0x2001 +#define MASK_C_JAL 0xe003 +#define MATCH_C_J 0xa001 +#define MASK_C_J 0xe003 +#define MATCH_BEQ 0x63 +#define MASK_BEQ 0x707f +#define MATCH_BNE 0x1063 +#define MASK_BNE 0x707f +#define MATCH_BLT 0x4063 +#define MASK_BLT 0x707f +#define MATCH_BGE 0x5063 +#define MASK_BGE 0x707f +#define MATCH_BLTU 0x6063 +#define MASK_BLTU 0x707f +#define MATCH_BGEU 0x7063 +#define MASK_BGEU 0x707f +#define MATCH_C_BEQZ 0xc001 +#define MASK_C_BEQZ 0xe003 +#define MATCH_C_BNEZ 0xe001 +#define MASK_C_BNEZ 0xe003 +#define MATCH_SRET 0x10200073 +#define MASK_SRET 0xffffffff + +#define OP_SH_RS1 15 +#define OP_SH_RS2 20 +#define OP_SH_CRS1S 7 + +#define __INSN_LENGTH_MASK _UL(0x3) +#define __INSN_LENGTH_GE_32 _UL(0x3) +#define __INSN_OPCODE_MASK _UL(0x7F) +#define __INSN_BRANCH_OPCODE _UL(0x63) +/* Define a series of is_XXX_insn functions to check if the value INSN + * is an instance of instruction XXX. + */ +#define DECLARE_INSN(INSN_NAME, INSN_MATCH, INSN_MASK) \ +static inline bool is_ ## INSN_NAME ## _insn(long insn) \ +{ \ + return (insn & (INSN_MASK)) == (INSN_MATCH); \ +} + +#if __riscv_xlen == 32 +/* C.JAL is an RV32C-only instruction */ +DECLARE_INSN(c_jal, MATCH_C_JAL, MASK_C_JAL) +#else +#define is_c_jal_insn(opcode) 0 +#endif +DECLARE_INSN(jalr, MATCH_JALR, MASK_JALR) +DECLARE_INSN(jal, MATCH_JAL, MASK_JAL) +DECLARE_INSN(c_jr, MATCH_C_JR, MASK_C_JR) +DECLARE_INSN(c_jalr, MATCH_C_JALR, MASK_C_JALR) +DECLARE_INSN(c_j, MATCH_C_J, MASK_C_J) +DECLARE_INSN(beq, MATCH_BEQ, MASK_BEQ) +DECLARE_INSN(bne, MATCH_BNE, MASK_BNE) +DECLARE_INSN(blt, MATCH_BLT, MASK_BLT) +DECLARE_INSN(bge, MATCH_BGE, MASK_BGE) +DECLARE_INSN(bltu, MATCH_BLTU, MASK_BLTU) +DECLARE_INSN(bgeu, MATCH_BGEU, MASK_BGEU) +DECLARE_INSN(c_beqz, MATCH_C_BEQZ, MASK_C_BEQZ) +DECLARE_INSN(c_bnez, MATCH_C_BNEZ, MASK_C_BNEZ) +DECLARE_INSN(sret, MATCH_SRET, MASK_SRET) + +int decode_register_index(unsigned long opcode, int offset) +{ + return (opcode >> offset) & 0x1F; +} + +int decode_register_index_short(unsigned long opcode, int offset) +{ + return ((opcode >> offset) & 0x7) + 8; +} + +/* Calculate the new address for after a step */ +int get_step_address(struct pt_regs *regs, unsigned long *next_addr) +{ + unsigned long pc = regs->epc; + unsigned long *regs_ptr = (unsigned long *)regs; + unsigned int rs1_num, rs2_num; + int op_code; + + if (probe_kernel_address((void *)pc, op_code)) + return -EINVAL; + + if ((op_code & __INSN_LENGTH_MASK) != __INSN_LENGTH_GE_32) { + if (is_c_jalr_insn(op_code) || is_c_jr_insn(op_code)) { + rs1_num = decode_register_index(op_code, OP_SH_CRS1S); + *next_addr = regs_ptr[rs1_num]; + } else if (is_c_j_insn(op_code) || is_c_jal_insn(op_code)) { + *next_addr = EXTRACT_RVC_J_IMM(op_code) + pc; + } else if (is_c_beqz_insn(op_code)) { + rs1_num = decode_register_index_short(op_code, + OP_SH_CRS1S); + if (!rs1_num || regs_ptr[rs1_num] == 0) + *next_addr = EXTRACT_RVC_B_IMM(op_code) + pc; + else + *next_addr = pc + 2; + } else if (is_c_bnez_insn(op_code)) { + rs1_num = + decode_register_index_short(op_code, OP_SH_CRS1S); + if (rs1_num && regs_ptr[rs1_num] != 0) + *next_addr = EXTRACT_RVC_B_IMM(op_code) + pc; + else + *next_addr = pc + 2; + } else + *next_addr = pc + 2; + } else { + if ((op_code & __INSN_OPCODE_MASK) == __INSN_BRANCH_OPCODE) { + bool result = false; + long imm = EXTRACT_SBTYPE_IMM(op_code); + unsigned long rs1_val = 0, rs2_val = 0; + + rs1_num = decode_register_index(op_code, OP_SH_RS1); + rs2_num = decode_register_index(op_code, OP_SH_RS2); + if (rs1_num) + rs1_val = regs_ptr[rs1_num]; + if (rs2_num) + rs2_val = regs_ptr[rs2_num]; + + if (is_beq_insn(op_code)) + result = (rs1_val == rs2_val) ? true : false; + else if (is_bne_insn(op_code)) + result = (rs1_val != rs2_val) ? true : false; + else if (is_blt_insn(op_code)) + result = + ((long)rs1_val < + (long)rs2_val) ? true : false; + else if (is_bge_insn(op_code)) + result = + ((long)rs1_val >= + (long)rs2_val) ? true : false; + else if (is_bltu_insn(op_code)) + result = (rs1_val < rs2_val) ? true : false; + else if (is_bgeu_insn(op_code)) + result = (rs1_val >= rs2_val) ? true : false; + if (result) + *next_addr = imm + pc; + else + *next_addr = pc + 4; + } else if (is_jal_insn(op_code)) { + *next_addr = EXTRACT_UJTYPE_IMM(op_code) + pc; + } else if (is_jalr_insn(op_code)) { + rs1_num = decode_register_index(op_code, OP_SH_RS1); + if (rs1_num) + *next_addr = ((unsigned long *)regs)[rs1_num]; + *next_addr += EXTRACT_ITYPE_IMM(op_code); + } else if (is_sret_insn(op_code)) { + *next_addr = pc; + } else + *next_addr = pc + 4; + } + + return 0; +} + +int do_single_step(struct pt_regs *regs) +{ + /* Determine where the target instruction will send us to */ + unsigned long addr = 0; + int error = get_step_address(regs, &addr); + + if (error) + return error; + + stepped_address = addr; + + /* Store the op code in the stepped address */ + probe_kernel_address((void *)addr, stepped_opcode); + /* Replace the op code with the break instruction */ + error = probe_kernel_write((void *)addr, + arch_kgdb_ops.gdb_bpt_instr, + BREAK_INSTR_SIZE); + /* Flush and return */ + if (!error) + flush_icache_range(addr, addr + BREAK_INSTR_SIZE); + + return error; +} + +/* Undo a single step */ +static void undo_single_step(struct pt_regs *regs) +{ + if (stepped_opcode != 0) { + probe_kernel_write((void *)stepped_address, + (void *)&stepped_opcode, BREAK_INSTR_SIZE); + flush_icache_range(stepped_address, + stepped_address + BREAK_INSTR_SIZE); + } + stepped_address = 0; + stepped_opcode = 0; +} + struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = { {PT_REG_ZERO, GDB_SIZEOF_REG, -1}, {PT_REG_RA, GDB_SIZEOF_REG, offsetof(struct pt_regs, ra)}, @@ -144,6 +371,8 @@ int kgdb_arch_handle_exception(int vector, int signo, int err_code, { int err = 0; + undo_single_step(regs); + switch (remcom_in_buffer[0]) { case 'c': case 'D': @@ -153,6 +382,15 @@ int kgdb_arch_handle_exception(int vector, int signo, int err_code, atomic_set(&kgdb_cpu_doing_single_step, -1); kgdb_single_step = 0; break; + case 's': + kgdb_arch_update_addr(regs, remcom_in_buffer); + err = do_single_step(regs); + if (!err) { + kgdb_single_step = 1; + atomic_set(&kgdb_cpu_doing_single_step, + raw_smp_processor_id()); + } + break; default: err = -1; } @@ -162,6 +400,8 @@ int kgdb_arch_handle_exception(int vector, int signo, int err_code, int kgdb_riscv_kgdbbreak(unsigned long addr) { + if (stepped_address == addr) + return KGDB_SW_SINGLE_STEP; if (atomic_read(&kgdb_setting_breakpoint)) if (addr == (unsigned long)&kgdb_compiled_break) return KGDB_COMPILED_BREAK; @@ -185,7 +425,8 @@ static int kgdb_riscv_notify(struct notifier_block *self, unsigned long cmd, return NOTIFY_DONE; local_irq_save(flags); - if (kgdb_handle_exception(1, args->signr, cmd, regs)) + if (kgdb_handle_exception(type == KGDB_SW_SINGLE_STEP ? 0 : 1, + args->signr, cmd, regs)) return NOTIFY_DONE; if (type == KGDB_COMPILED_BREAK)