Module Name:    src
Committed By:   skrll
Date:           Mon Nov 25 22:04:15 UTC 2024

Modified Files:
        src/sys/arch/riscv/include: db_machdep.h mutex.h
        src/sys/arch/riscv/riscv: db_interface.c db_machdep.c db_trace.c trap.c
        src/usr.sbin/crash: Makefile

Log Message:
risc-v: support crash(8)


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/riscv/include/db_machdep.h
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/riscv/include/mutex.h
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/riscv/riscv/db_interface.c
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/riscv/riscv/db_machdep.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/riscv/riscv/db_trace.c
cvs rdiff -u -r1.29 -r1.30 src/sys/arch/riscv/riscv/trap.c
cvs rdiff -u -r1.52 -r1.53 src/usr.sbin/crash/Makefile

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/riscv/include/db_machdep.h
diff -u src/sys/arch/riscv/include/db_machdep.h:1.12 src/sys/arch/riscv/include/db_machdep.h:1.13
--- src/sys/arch/riscv/include/db_machdep.h:1.12	Sat Nov 23 11:37:43 2024
+++ src/sys/arch/riscv/include/db_machdep.h	Mon Nov 25 22:04:14 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: db_machdep.h,v 1.12 2024/11/23 11:37:43 skrll Exp $ */
+/* $NetBSD: db_machdep.h,v 1.13 2024/11/25 22:04:14 skrll Exp $ */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -34,6 +34,10 @@
 
 #include <riscv/frame.h>
 
+#ifndef _KERNEL
+#include <stdbool.h>
+#endif /* _KERNEL */
+
 #define	DB_ELF_SYMBOLS
 
 typedef	vaddr_t		db_addr_t;	/* address - unsigned */

Index: src/sys/arch/riscv/include/mutex.h
diff -u src/sys/arch/riscv/include/mutex.h:1.6 src/sys/arch/riscv/include/mutex.h:1.7
--- src/sys/arch/riscv/include/mutex.h:1.6	Wed Jul 12 12:50:13 2023
+++ src/sys/arch/riscv/include/mutex.h	Mon Nov 25 22:04:14 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: mutex.h,v 1.6 2023/07/12 12:50:13 riastradh Exp $	*/
+/*	$NetBSD: mutex.h,v 1.7 2024/11/25 22:04:14 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
@@ -52,6 +52,8 @@ struct kmutex {
 	volatile uintptr_t	mtx_owner;
 };
 
+#ifdef _KERNEL
+
 #ifdef _LP64
 #define MTX_ASMOP_SFX ".d"		// doubleword atomic op
 #else
@@ -113,6 +115,8 @@ riscv_mutex_spinbit_lock_unlock(kmutex_t
 	   ::	"r"(~MTX_LOCK), "r"(__mtx));
 }
 
+#endif /* _KERNEL */
+
 #if 0
 #define	__HAVE_MUTEX_STUBS		1
 #define	__HAVE_SPIN_MUTEX_STUBS		1

Index: src/sys/arch/riscv/riscv/db_interface.c
diff -u src/sys/arch/riscv/riscv/db_interface.c:1.5 src/sys/arch/riscv/riscv/db_interface.c:1.6
--- src/sys/arch/riscv/riscv/db_interface.c:1.5	Fri Dec 22 08:41:59 2023
+++ src/sys/arch/riscv/riscv/db_interface.c	Mon Nov 25 22:04:14 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_interface.c,v 1.5 2023/12/22 08:41:59 skrll Exp $	*/
+/*	$NetBSD: db_interface.c,v 1.6 2024/11/25 22:04:14 skrll Exp $	*/
 
 /*
  * Mach Operating System
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.5 2023/12/22 08:41:59 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.6 2024/11/25 22:04:14 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_multiprocessor.h"
@@ -71,16 +71,38 @@ int		db_active = 0;
 
 #ifdef _KERNEL
 db_regs_t	ddb_regs;
-#endif
 
 #ifdef MULTIPROCESSOR
 static void db_mach_cpu_cmd(db_expr_t, bool, db_expr_t, const char *);
 #endif
+static void db_mach_reset_cmd(db_expr_t, bool, db_expr_t, const char *);
 
 void db_tlbdump_cmd(db_expr_t, bool, db_expr_t, const char *);
 void db_kvtophys_cmd(db_expr_t, bool, db_expr_t, const char *);
 
 paddr_t kvtophys(vaddr_t);
+#endif
+
+
+const struct db_command db_machine_command_table[] = {
+#ifdef _KERNEL
+#ifdef MULTIPROCESSOR
+	{ DDB_ADD_CMD("cpu",	db_mach_cpu_cmd,	0,
+	  "switch to another cpu", "cpu#", NULL) },
+#endif
+	{ DDB_ADD_CMD("kvtop",	db_kvtophys_cmd,	0,
+		"Print the physical address for a given kernel virtual address",
+		"address",
+		"   address:\tvirtual address to look up") },
+	{ DDB_ADD_CMD("reset",	db_mach_reset_cmd,	CS_NOREPEAT,
+		"Initiate hardware reset",
+		NULL, NULL) },
+	{ DDB_ADD_CMD("tlb",	db_tlbdump_cmd,		0,
+		"Print out TLB entries. (only works with options DEBUG)",
+		NULL, NULL) },
+#endif
+	{ DDB_END_CMD },
+};
 
 #ifdef _KERNEL
 
@@ -174,24 +196,6 @@ db_mach_reset_cmd(db_expr_t addr, bool h
 {
 }
 
-
-const struct db_command db_machine_command_table[] = {
-#ifdef MULTIPROCESSOR
-	{ DDB_ADD_CMD("cpu",	db_mach_cpu_cmd,	0,
-	  "switch to another cpu", "cpu#", NULL) },
-#endif
-	{ DDB_ADD_CMD("kvtop",	db_kvtophys_cmd,	0,
-		"Print the physical address for a given kernel virtual address",
-		"address",
-		"   address:\tvirtual address to look up") },
-	{ DDB_ADD_CMD("reset", 	db_mach_reset_cmd,	CS_NOREPEAT,
-		"Initiate hardware reset",
-		NULL, NULL) },
-	{ DDB_ADD_CMD("tlb",	db_tlbdump_cmd,		0,
-		"Print out TLB entries. (only works with options DEBUG)",
-		NULL, NULL) },
-	{ DDB_END_CMD },
-};
 #endif	/* !KGDB */
 
 #ifdef MULTIPROCESSOR

Index: src/sys/arch/riscv/riscv/db_machdep.c
diff -u src/sys/arch/riscv/riscv/db_machdep.c:1.13 src/sys/arch/riscv/riscv/db_machdep.c:1.14
--- src/sys/arch/riscv/riscv/db_machdep.c:1.13	Sat Nov 23 12:03:55 2024
+++ src/sys/arch/riscv/riscv/db_machdep.c	Mon Nov 25 22:04:14 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_machdep.c,v 1.13 2024/11/23 12:03:55 skrll Exp $	*/
+/*	$NetBSD: db_machdep.c,v 1.14 2024/11/25 22:04:14 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -31,21 +31,27 @@
 
 #include <sys/cdefs.h>
 
-__RCSID("$NetBSD: db_machdep.c,v 1.13 2024/11/23 12:03:55 skrll Exp $");
+__RCSID("$NetBSD: db_machdep.c,v 1.14 2024/11/25 22:04:14 skrll Exp $");
 
 #include <sys/param.h>
 
 #include <sys/cpu.h>
+#include <sys/systm.h>
 
 #include <riscv/insn.h>
 #include <riscv/db_machdep.h>
 
+#include <ddb/db_user.h>
 #include <ddb/db_access.h>
 #include <ddb/db_interface.h>
 #include <ddb/db_extern.h>
 #include <ddb/db_variables.h>
 #include <ddb/db_output.h>
 
+#ifndef _KERNEL
+#include <stddef.h>
+#endif
+
 static int db_rw_ddbreg(const struct db_variable *, db_expr_t *, int);
 
 const struct db_variable db_regs[] = {
@@ -90,7 +96,7 @@ const struct db_variable * const db_ereg
 int
 db_rw_ddbreg(const struct db_variable *vp, db_expr_t *valp, int rw)
 {
-	struct trapframe * const tf = curcpu()->ci_ddb_regs;
+	struct trapframe * const tf = &ddb_regs;
 	const uintptr_t addr = (uintptr_t)tf + (uintptr_t)vp->valuep;
 	if (vp->modif != NULL && vp->modif[0] == 'i') {
 		if (rw == DB_VAR_GET) {
@@ -179,7 +185,6 @@ branch_taken(uint32_t insn, db_addr_t pc
 		displacement |= i.type_j.j_imm11 << 11;
 		displacement |= i.type_j.j_imm10to1 << 1;
 	} else {
-		KASSERT(OPCODE_P(insn, BRANCH));
 		register_t rs1 = get_reg_value(tf, i.type_b.b_rs1);
 		register_t rs2 = get_reg_value(tf, i.type_b.b_rs2);
 		bool branch_p; // = false;

Index: src/sys/arch/riscv/riscv/db_trace.c
diff -u src/sys/arch/riscv/riscv/db_trace.c:1.6 src/sys/arch/riscv/riscv/db_trace.c:1.7
--- src/sys/arch/riscv/riscv/db_trace.c:1.6	Sat Nov 23 11:37:43 2024
+++ src/sys/arch/riscv/riscv/db_trace.c	Mon Nov 25 22:04:14 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_trace.c,v 1.6 2024/11/23 11:37:43 skrll Exp $	*/
+/*	$NetBSD: db_trace.c,v 1.7 2024/11/25 22:04:14 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -31,16 +31,19 @@
 
 #include <sys/cdefs.h>
 
-__RCSID("$NetBSD: db_trace.c,v 1.6 2024/11/23 11:37:43 skrll Exp $");
+__RCSID("$NetBSD: db_trace.c,v 1.7 2024/11/25 22:04:14 skrll Exp $");
 
 #include <sys/param.h>
 
+#include <sys/proc.h>
 #include <sys/systm.h>
+#include <sys/types.h>
 
 #include <riscv/db_machdep.h>
 
 #include <uvm/uvm_extern.h>
 
+#include <ddb/db_user.h>
 #include <ddb/db_access.h>
 #include <ddb/db_command.h>
 #include <ddb/db_output.h>
@@ -51,6 +54,10 @@ __RCSID("$NetBSD: db_trace.c,v 1.6 2024/
 #include <ddb/db_extern.h>
 #include <ddb/db_interface.h>
 
+#ifndef _KERNEL
+#include <stddef.h>
+#endif
+
 #define MAXBACKTRACE	128	/* against infinite loop */
 #define TRACEFLAG_LOOKUPLWP	0x00000001
 
@@ -122,7 +129,7 @@ void
 db_stack_trace_print(db_expr_t addr, bool have_addr, db_expr_t count,
     const char *modif, void (*pr)(const char *, ...) __printflike(1, 2))
 {
-	register_t ra, fp, lastra, lastfp;
+	vaddr_t ra, fp, lastra, lastfp;
 	struct trapframe *tf = NULL;
 	int flags = 0;
 	bool trace_user = false;
@@ -176,14 +183,14 @@ db_stack_trace_print(db_expr_t addr, boo
 #endif
 
 	if (trace_thread) {
-		proc_t *pp, p;
+		proc_t *pp;
 
 		if ((pp = db_proc_find((pid_t)addr)) == 0) {
 			(*pr)("trace: pid %d: not found\n", (int)addr);
 			return;
 		}
-		db_read_bytes((db_addr_t)pp, sizeof(p), (char *)&p);
-		addr = (db_addr_t)p.p_lwps.lh_first;
+		db_read_bytes((db_addr_t)pp + offsetof(proc_t, p_lwps.lh_first),
+		    sizeof(addr), (char *)&addr);
 		trace_thread = false;
 		trace_lwp = true;
 	}
@@ -217,7 +224,7 @@ db_stack_trace_print(db_expr_t addr, boo
 		}
 	} else if (tf == NULL) {
 		fp = addr;
-		pr("trace fp %016" PRIxREGISTER "\n", fp);
+		pr("trace fp %016" PRIxVADDR "\n", fp);
 	} else {
 		pr("trace tf %p\n", tf);
 	}

Index: src/sys/arch/riscv/riscv/trap.c
diff -u src/sys/arch/riscv/riscv/trap.c:1.29 src/sys/arch/riscv/riscv/trap.c:1.30
--- src/sys/arch/riscv/riscv/trap.c:1.29	Sat Nov 23 11:37:43 2024
+++ src/sys/arch/riscv/riscv/trap.c	Mon Nov 25 22:04:14 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.29 2024/11/23 11:37:43 skrll Exp $	*/
+/*	$NetBSD: trap.c,v 1.30 2024/11/25 22:04:14 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -34,11 +34,12 @@
 #define	__PMAP_PRIVATE
 #define	__UFETCHSTORE_PRIVATE
 
-__RCSID("$NetBSD: trap.c,v 1.29 2024/11/23 11:37:43 skrll Exp $");
+__RCSID("$NetBSD: trap.c,v 1.30 2024/11/25 22:04:14 skrll Exp $");
 
 #include <sys/param.h>
 
 #include <sys/atomic.h>
+#include <sys/cpu.h>
 #include <sys/kauth.h>
 #include <sys/signal.h>
 #include <sys/signalvar.h>

Index: src/usr.sbin/crash/Makefile
diff -u src/usr.sbin/crash/Makefile:1.52 src/usr.sbin/crash/Makefile:1.53
--- src/usr.sbin/crash/Makefile:1.52	Tue Nov 19 18:15:29 2024
+++ src/usr.sbin/crash/Makefile	Mon Nov 25 22:04:15 2024
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.52 2024/11/19 18:15:29 skrll Exp $
+#	$NetBSD: Makefile,v 1.53 2024/11/25 22:04:15 skrll Exp $
 
 PROG=		crash
 MAN=		crash.8
@@ -17,6 +17,7 @@ DPADD+=	${LIBUTIL} ${LIBKVM} ${LIBEDIT} 
        ${MACHINE_CPU} == "aarch64" \
     || ${MACHINE_CPU} == "arm" \
     || ${MACHINE_CPU} == "mips" \
+    || ${MACHINE_CPU} == "riscv" \
     || ${MACHINE_ARCH} == "alpha" \
     || ${MACHINE_ARCH} == "m68k" \
     || ${MACHINE_ARCH} == "powerpc" \
@@ -27,7 +28,9 @@ DPADD+=	${LIBUTIL} ${LIBKVM} ${LIBEDIT} 
     || ${MACHINE} == "sparc" \
     || ${MACHINE} == "sparc64"
 SRCS+=	db_trace.c
-.if ${MACHINE_ARCH} == "alpha"
+.if \
+       ${MACHINE_ARCH} == "alpha" \
+    || ${MACHINE_CPU} == "riscv"
 SRCS+=	db_interface.c
 .elif ${MACHINE_CPU} == "mips"
 SRCS+=	db_interface.c mips_stacktrace.c

Reply via email to