From nobody Mon Jan 10 17:40:54 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 EB499194FE70; Mon, 10 Jan 2022 17:40:54 +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 4JXh1y4j42z4hsp; Mon, 10 Jan 2022 17:40:54 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 81B0A16AC; Mon, 10 Jan 2022 17:40:54 +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 20AHesTo033417; Mon, 10 Jan 2022 17:40:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20AHesOA033416; Mon, 10 Jan 2022 17:40:54 GMT (envelope-from git) Date: Mon, 10 Jan 2022 17:40:54 GMT Message-Id: <202201101740.20AHesOA033416@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: d72e944812f8 - main - 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/main X-Git-Reftype: branch X-Git-Commit: d72e944812f8774ab0b78fdff9b0204386dd6151 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1641836454; 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=ktBSRZy/sas8icA10bBGhvq0cRjI+LQfDI5cm79oklw=; b=nvOci5SEvrDnTcN/HyQzZQkkJ+i5in1qVUaGunL7/ekQNHxr1tlLknqZgvJrCiJ+E+Nm26 3QP4BIiOLgbFKWn9N6vqv+FSCFXEVg7HyqmkaFeOmQS8BA0aUKBd8Vo+sdFPCFfIiMn8P1 JrBPFXxWfe4z+IbdCaJw1Rg12RONfKDSDnZYr2p+udrOIEvQbDBdOVTwKhaGAgfm80Aw17 0lL36UXkdfnYEAFjZPgVdhjJPdAHpGO3Zkj1sK7jIihshIoc8GviLXX97IDvAgKsZI3v1G xXISQFBQI3RvVSNBfdlXd1fSn3tCa8ib7mzQU5kATsW3XyywA6lzOEXNKKLPWQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1641836454; a=rsa-sha256; cv=none; b=FJcVoB3cViBuPdxDryV4I7cbzQZ6yFPvYvU5hlxprw19o5lGchif1roFKaXLwuV3UjHeNT VNYFCp0tvYsQfbaiSpnD+UYCVn3cUMsjuwfnQRoYvXcGbYzq6KZ3SX/AIKlWl0QA1QbikM bcliFiZegkDm4Hd4+B+J8JK8KCEJtAMtS6K8OuooXY7ixGB4vtu3XHIeSD8ToNi9ZN0Vj7 /qiuAvIts4WlZGyVGe1G/PqE7wffBNxd4ycLYt5gYo/qkPrjna47143HvLaAKgoY8U8Pcs i8GQzmCw+FTB/4T3r4iWS9rWsHcX3rPY6jtbJrpQeHMdXjCuZLH06Ku+JooViA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=d72e944812f8774ab0b78fdff9b0204386dd6151 commit d72e944812f8774ab0b78fdff9b0204386dd6151 Author: Mitchell Horne AuthorDate: 2020-09-11 17:02:17 +0000 Commit: Mitchell Horne CommitDate: 2022-01-10 17:40:12 +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 --- sys/conf/files.riscv | 1 + sys/riscv/conf/GENERIC | 2 +- sys/riscv/conf/NOTES | 3 - sys/riscv/include/gdb_machdep.h | 88 ++++++++++++++++++++++++++ sys/riscv/riscv/gdb_machdep.c | 135 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 4 deletions(-) diff --git a/sys/conf/files.riscv b/sys/conf/files.riscv index e343b9b7f9b3..4a2d4efad137 100644 --- a/sys/conf/files.riscv +++ b/sys/conf/files.riscv @@ -46,6 +46,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/GENERIC b/sys/riscv/conf/GENERIC index 055574920bb5..2ea0ba5c3576 100644 --- a/sys/riscv/conf/GENERIC +++ b/sys/riscv/conf/GENERIC @@ -173,7 +173,7 @@ options KDB # Enable kernel debugger support. options KDB_TRACE # Print a stack trace for a panic. # For full debugger support use (turn off in stable branch): options DDB # Support DDB. -# options GDB # Support remote GDB. +options GDB # Support remote GDB. options DEADLKRES # Enable the deadlock resolver options INVARIANTS # Enable calls of extra sanity checking options INVARIANT_SUPPORT # Extra sanity checks of internal structures, required by INVARIANTS 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); +}