From nobody Wed Jan 19 18:31:26 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 707AC1954B64; Wed, 19 Jan 2022 18:31:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JfDk62lTJz4Vdp; Wed, 19 Jan 2022 18:31:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642617086; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=M7mFwfSyR/I1SCeZxZI9r1ef/J4odtSQWYn/8fTpfg0=; b=j+RbKLD1nPiYtHXOxFR5iI/w7KDlk+kkdJEot0JuqZCHR+W+3+wEZTpKNVtWuxFIv2n2c8 zoIMyYToS8wk4E9qF13up6eY3EzIR/WEADdZrce+PMukgw1qHmC364MRYsyhYId821LoBl P9l9lPdZE27eCkf181fDMhaWMD/o+n9DRROFGIeFEmFeU/CkL3/XSiVUmoB/9B260O7MSQ EiJp+Xh40tvza0snU+/qBEzquRLfMYB4dI4evx72jt5ZjNOOOIEk0E1SmMRSIY6CLjzLq/ rw9blCEagGshobe4USv/n+ZN9iFKS2PpcI/cAmppjqiLxAwnjzp6DQlRkaZW5w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3F9AA198FC; Wed, 19 Jan 2022 18:31:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 20JIVQsB043022; Wed, 19 Jan 2022 18:31:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20JIVQX8043021; Wed, 19 Jan 2022 18:31:26 GMT (envelope-from git) Date: Wed, 19 Jan 2022 18:31:26 GMT Message-Id: <202201191831.20JIVQX8043021@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mitchell Horne Subject: git: d25147c01a20 - stable/13 - riscv: gdb(4) support List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: d25147c01a20f4c4ea7194b6f1df94dcc9aad38c Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642617086; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=M7mFwfSyR/I1SCeZxZI9r1ef/J4odtSQWYn/8fTpfg0=; b=sVHrVaEf6P82Ir7WxocAsehlHu9nDos+pE1oLl0vREkJBx7ppsKNqS7lzkBrf+4MUw7Ohb +aUMW2XidG3FD2znMTY/5sH7mEPqVVOmu98RpvNm5FF6rRJYEYSC2Dp+IRYpQZBdLMbBwF fXGnZS/rhQbrexOSf3d08O7j0ot0R5HwXlph4oDyv1QpWh1GPEHWT4AqG3ppvU6ArdH44e 3TX/XWaqlI7+ujzB7hSLnrobk0gRTUiIfRAZcUAiiDEzZUXnp+abWE5cFEd9jMiUBU3E3k TXXo43YHLlcSxGmu1E59Lh9aB4k9fnKUCYySeiuTROnRt2ukMZkT/iZfPpQTKQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1642617086; a=rsa-sha256; cv=none; b=EZz+RUtgMtLAFn2dJvUJH+bbZ75NvyxbuDVZs0SrZ9A4Ii8wCmu92DhjkXCGsSs+mkS9ui k13Wzm6ZlmtwW2x05fh2aMfSVBLIXcIgnAa4/FRkDojLUGh0QyvamDt4we0Dj8tMP4qMQW bfnuvJYkoIXaLkmypVunEPbOzRqMIy0/GwSdyE1ze1REzDcIo3trcfBsKiatB4QBeJd9XL u1lwsSTJiXV0/OGJ9M0WjsWkwrol0NbZ8p6SttcN0wVJS/1gqlLTotxCqCyCMZ36SeKnPV 9FnorSJeocoN7rukTgQABg4CMCWmqKy2iHq69yAsiwWDIZl39xH8D5ydfAGMCg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=d25147c01a20f4c4ea7194b6f1df94dcc9aad38c commit d25147c01a20f4c4ea7194b6f1df94dcc9aad38c Author: Mitchell Horne AuthorDate: 2020-09-11 17:02:17 +0000 Commit: Mitchell Horne CommitDate: 2022-01-19 18:30:31 +0000 riscv: gdb(4) support Add the MD portion required for the gdb stub. Reviewed by: jhb (earlier version) Discussed with: jrtc27 MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D33734 (cherry picked from commit d72e944812f8774ab0b78fdff9b0204386dd6151) --- sys/conf/files.riscv | 1 + sys/riscv/conf/NOTES | 3 - sys/riscv/include/gdb_machdep.h | 88 ++++++++++++++++++++++++++ sys/riscv/riscv/gdb_machdep.c | 135 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 3 deletions(-) diff --git a/sys/conf/files.riscv b/sys/conf/files.riscv index 1b03a8840109..37f4f203ab21 100644 --- a/sys/conf/files.riscv +++ b/sys/conf/files.riscv @@ -47,6 +47,7 @@ riscv/riscv/dump_machdep.c standard riscv/riscv/elf_machdep.c standard riscv/riscv/exception.S standard riscv/riscv/exec_machdep.c standard +riscv/riscv/gdb_machdep.c optional gdb riscv/riscv/intr_machdep.c standard riscv/riscv/identcpu.c standard riscv/riscv/locore.S standard no-obj diff --git a/sys/riscv/conf/NOTES b/sys/riscv/conf/NOTES index 7dda89bfe1a8..1e4b8af1840e 100644 --- a/sys/riscv/conf/NOTES +++ b/sys/riscv/conf/NOTES @@ -90,9 +90,6 @@ nooptions COMPAT_FREEBSD9 nooptions COMPAT_FREEBSD10 nooptions COMPAT_FREEBSD11 -# No support for remote GDB -nooptions GDB - # riscv doesn't support inb/outb, so disable chipset probing which needs it nooptions PPC_PROBE_CHIPSET diff --git a/sys/riscv/include/gdb_machdep.h b/sys/riscv/include/gdb_machdep.h new file mode 100644 index 000000000000..422f57a5742b --- /dev/null +++ b/sys/riscv/include/gdb_machdep.h @@ -0,0 +1,88 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 Mitchell Horne + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _MACHINE_GDB_MACHDEP_H_ +#define _MACHINE_GDB_MACHDEP_H_ + +#define GDB_BUFSZ 4096 +#define GDB_NREGS 33 +#define GDB_REG_ZERO 0 +#define GDB_REG_RA 1 +#define GDB_REG_SP 2 +#define GDB_REG_GP 3 +#define GDB_REG_TP 4 +#define GDB_REG_T0 5 +#define GDB_REG_FP 8 +#define GDB_REG_S1 9 +#define GDB_REG_A0 10 +#define GDB_REG_S2 18 +#define GDB_REG_T3 28 +#define GDB_REG_PC 32 +#define GDB_REG_CSR_BASE 65 +#define GDB_REG_SSTATUS (GDB_REG_CSR_BASE + 0x100) +#define GDB_REG_SCAUSE (GDB_REG_CSR_BASE + 0x142) +#define GDB_REG_STVAL (GDB_REG_CSR_BASE + 0x143) +_Static_assert(GDB_BUFSZ >= (GDB_NREGS * 8), "buffer fits 'g' regs"); + +static __inline size_t +gdb_cpu_regsz(int regnum __unused) +{ + + return (8); +} + +static __inline int +gdb_cpu_query(void) +{ + return (0); +} + +static __inline void * +gdb_begin_write(void) +{ + + return (NULL); +} + +static __inline void +gdb_end_write(void *arg __unused) +{ + +} + +static __inline void +gdb_cpu_stop_reason(int type __unused, int code __unused) +{ + +} + +void *gdb_cpu_getreg(int, size_t *); +void gdb_cpu_setreg(int, void *); +int gdb_cpu_signal(int, int); + +#endif /* !_MACHINE_GDB_MACHDEP_H_ */ diff --git a/sys/riscv/riscv/gdb_machdep.c b/sys/riscv/riscv/gdb_machdep.c new file mode 100644 index 000000000000..06d4b8ba6e22 --- /dev/null +++ b/sys/riscv/riscv/gdb_machdep.c @@ -0,0 +1,135 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 Mitchell Horne + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +void * +gdb_cpu_getreg(int regnum, size_t *regsz) +{ + *regsz = gdb_cpu_regsz(regnum); + + if (kdb_thread == curthread) { + switch (regnum) { + case GDB_REG_RA: return (&kdb_frame->tf_ra); + case GDB_REG_PC: return (&kdb_frame->tf_sepc); + case GDB_REG_SSTATUS: return (&kdb_frame->tf_sstatus); + case GDB_REG_STVAL: return (&kdb_frame->tf_stval); + case GDB_REG_SCAUSE: return (&kdb_frame->tf_scause); + default: + if (regnum >= GDB_REG_A0 && regnum < GDB_REG_S2) + return (&kdb_frame->tf_a[regnum - GDB_REG_A0]); + if (regnum >= GDB_REG_T0 && regnum < GDB_REG_FP) + return (&kdb_frame->tf_t[regnum - GDB_REG_T0]); + if (regnum >= GDB_REG_T3 && regnum < GDB_REG_PC) + return (&kdb_frame->tf_t[regnum - GDB_REG_T3]); + break; + } + } + switch (regnum) { + case GDB_REG_PC: /* FALLTHROUGH */ + case GDB_REG_RA: return (&kdb_thrctx->pcb_ra); + case GDB_REG_SP: return (&kdb_thrctx->pcb_sp); + case GDB_REG_GP: return (&kdb_thrctx->pcb_gp); + case GDB_REG_TP: return (&kdb_thrctx->pcb_tp); + case GDB_REG_FP: return (&kdb_thrctx->pcb_s[0]); + case GDB_REG_S1: return (&kdb_thrctx->pcb_s[1]); + default: + if (regnum >= GDB_REG_S2 && regnum < GDB_REG_T3) + return (&kdb_thrctx->pcb_s[regnum - GDB_REG_S2]); + break; + } + + return (NULL); +} + +void +gdb_cpu_setreg(int regnum, void *val) +{ + register_t regval = *(register_t *)val; + + /* For curthread, keep the pcb and trapframe in sync. */ + if (kdb_thread == curthread) { + switch (regnum) { + case GDB_REG_PC: kdb_frame->tf_sepc = regval; break; + case GDB_REG_RA: kdb_frame->tf_ra = regval; break; + case GDB_REG_SP: kdb_frame->tf_sp = regval; break; + case GDB_REG_GP: kdb_frame->tf_gp = regval; break; + case GDB_REG_TP: kdb_frame->tf_tp = regval; break; + case GDB_REG_FP: kdb_frame->tf_s[0] = regval; break; + case GDB_REG_S1: kdb_frame->tf_s[1] = regval; break; + case GDB_REG_SSTATUS: kdb_frame->tf_sstatus = regval; break; + case GDB_REG_STVAL: kdb_frame->tf_stval = regval; break; + case GDB_REG_SCAUSE: kdb_frame->tf_scause = regval; break; + default: + if (regnum >= GDB_REG_A0 && regnum < GDB_REG_S2) + kdb_frame->tf_a[regnum - GDB_REG_A0] = regval; + if (regnum >= GDB_REG_S2 && regnum < GDB_REG_T3) + kdb_frame->tf_s[regnum - GDB_REG_S2] = regval; + if (regnum >= GDB_REG_T0 && regnum < GDB_REG_FP) + kdb_frame->tf_t[regnum - GDB_REG_T0] = regval; + if (regnum >= GDB_REG_T3 && regnum < GDB_REG_PC) + kdb_frame->tf_t[regnum - GDB_REG_T3] = regval; + break; + } + } + switch (regnum) { + case GDB_REG_PC: /* FALLTHROUGH */ + case GDB_REG_RA: kdb_thrctx->pcb_ra = regval; break; + case GDB_REG_SP: kdb_thrctx->pcb_sp = regval; break; + case GDB_REG_GP: kdb_thrctx->pcb_gp = regval; break; + case GDB_REG_TP: kdb_thrctx->pcb_tp = regval; break; + case GDB_REG_FP: kdb_thrctx->pcb_s[0] = regval; break; + case GDB_REG_S1: kdb_thrctx->pcb_s[1] = regval; break; + default: + if (regnum >= GDB_REG_S2 && regnum < GDB_REG_T3) + kdb_thrctx->pcb_s[regnum - GDB_REG_S2] = regval; + break; + } +} + +int +gdb_cpu_signal(int type, int code) +{ + + if (type == SCAUSE_BREAKPOINT) + return (SIGTRAP); + + return (SIGEMT); +}