Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/riscv Basic ddb and backtrace support.



details:   https://anonhg.NetBSD.org/src/rev/b6f53ace0dba
branches:  trunk
changeset: 370745:b6f53ace0dba
user:      skrll <skrll%NetBSD.org@localhost>
date:      Tue Sep 27 08:18:21 2022 +0000

description:
Basic ddb and backtrace support.

[   1.0000000] panic: kernel diagnostic assertion "msgbufaddr != 0" failed: file "/home/nick/netbsd/nbcvs/src/sys/arch/riscv/riscv/riscv_machdep.c", line 564
[   1.0000000] cpu0: Begin traceback...
[   1.0000000] trace fp ffffffc000801dd0
[   1.0000000] fp ffffffc000801e10 ?() at ffffffc0001eee98
[   1.0000000] fp ffffffc000801e30 ?() at ffffffc0002ad984
[   1.0000000] fp ffffffc000801ee0 ?() at ffffffc000005430
[   1.0000000] cpu0: End traceback...
[   1.0000000] Trapframe @ 0xffffffc000801cb0 (cause=3 (breakpoint), status=0x100, pc=0xffffffc0000007e4, va=0):
[   1.0000000] ra =0xffffffc0001eee9c, sp =0xffffffc000801dd0, gp =0xffffffc0006a8f40, tp =                 0
[   1.0000000] s0 =0xffffffc000600ac0, s1 =            0x1000, s2 =0xffffffc000438df0, s3 =0xffffffc000801d80
[   1.0000000] s4 =0xffffffc0001eed78, s5 =0xffffffc0006d55f8, s6 =0xffffffc000801d88, s7 =0xffffffc0006dfdb8
[   1.0000000] s8 =0xffffffc000428c18, s9 =0xffffffc000801dd0, s10=0xffffffc0000a3822, s11=0xffffffc0006d55f8
[   1.0000000] a0 =              0x17, a1 =               0xa, a2 =                 0, a3 =0xffffffc00041f658
[   1.0000000] a4 =                 0, a5 =               0x1, a5 =                 0, a7 =               0x1
[   1.0000000] t0 =0xffffffc0006dfe30, t1 =0xffffffc000801bb8, t2 =0xffffffc000803000, t3 =              0x75
[   1.0000000] t4 =                 0, t5 =              0x63, t6 =               0x1
[   1.0000000] kernel: breakpoint
Stopped in pid 0.0 (system) at  ffffffc0000007e4:       c.ebreak
db>

diffstat:

 sys/arch/riscv/conf/GENERIC         |    6 +-
 sys/arch/riscv/conf/files.riscv     |    3 +-
 sys/arch/riscv/include/db_machdep.h |    9 +-
 sys/arch/riscv/riscv/db_interface.c |  248 ++++++++++++++++++++++++++++++++++++
 sys/arch/riscv/riscv/db_machdep.c   |    6 +-
 sys/arch/riscv/riscv/db_trace.c     |  215 ++++++++++++++++++++++++++++++-
 sys/arch/riscv/riscv/locore.S       |    4 +-
 sys/arch/riscv/riscv/trap.c         |   21 ++-
 8 files changed, 489 insertions(+), 23 deletions(-)

diffs (truncated from 669 to 300 lines):

diff -r 96e6ab20ee41 -r b6f53ace0dba sys/arch/riscv/conf/GENERIC
--- a/sys/arch/riscv/conf/GENERIC       Tue Sep 27 06:36:41 2022 +0000
+++ b/sys/arch/riscv/conf/GENERIC       Tue Sep 27 08:18:21 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.14 2022/09/11 15:31:11 skrll Exp $
+# $NetBSD: GENERIC,v 1.15 2022/09/27 08:18:21 skrll Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@
 
 options        INCLUDE_CONFIG_FILE     # embed config file in kernel binary
 
-#ident                 "GENERIC-$Revision: 1.14 $"
+#ident                 "GENERIC-$Revision: 1.15 $"
 
 maxusers       64              # estimated number of users
 
@@ -74,7 +74,7 @@
 # Because gcc omits the frame pointer for any -O level, the line below
 # is needed to make backtraces in DDB work.
 #
-#makeoptions   COPTS="-O2 -fno-omit-frame-pointer"
+makeoptions    COPTS="-O2 -fno-omit-frame-pointer"
 makeoptions    COPY_SYMTAB=1
 options        DDB             # in-kernel debugger
 #options       DDB_COMMANDONENTER="bt" # execute command when ddb is entered
diff -r 96e6ab20ee41 -r b6f53ace0dba sys/arch/riscv/conf/files.riscv
--- a/sys/arch/riscv/conf/files.riscv   Tue Sep 27 06:36:41 2022 +0000
+++ b/sys/arch/riscv/conf/files.riscv   Tue Sep 27 08:18:21 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.riscv,v 1.9 2022/09/19 09:15:01 skrll Exp $
+#      $NetBSD: files.riscv,v 1.10 2022/09/27 08:18:21 skrll Exp $
 #
 
 maxpartitions  16
@@ -21,6 +21,7 @@
 file   arch/riscv/riscv/clock_machdep.c
 file   arch/riscv/riscv/core_machdep.c         coredump
 file   arch/riscv/riscv/cpu_subr.c
+file   arch/riscv/riscv/db_interface.c         ddb
 file   arch/riscv/riscv/db_disasm.c            ddb
 file   arch/riscv/riscv/db_machdep.c           ddb | kgdb
 file   arch/riscv/riscv/db_trace.c             ddb
diff -r 96e6ab20ee41 -r b6f53ace0dba sys/arch/riscv/include/db_machdep.h
--- a/sys/arch/riscv/include/db_machdep.h       Tue Sep 27 06:36:41 2022 +0000
+++ b/sys/arch/riscv/include/db_machdep.h       Tue Sep 27 08:18:21 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: db_machdep.h,v 1.5 2021/05/18 06:42:11 skrll Exp $ */
+/* $NetBSD: db_machdep.h,v 1.6 2022/09/27 08:18:21 skrll Exp $ */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -43,7 +43,8 @@
 typedef struct trapframe db_regs_t;
 
 extern const uint32_t __cpu_Debugger_insn[1];
-#define        DDB_REGS        (curcpu()->ci_ddb_regs)
+extern db_regs_t ddb_regs;
+#define        DDB_REGS        (&ddb_regs)
 
 #define        PC_REGS(tf)     ((tf)->tf_pc)
 
@@ -87,7 +88,7 @@
  * Entrypoints to DDB for kernel, keyboard drivers, init hook
  */
 void   kdb_kbd_trap(db_regs_t *);
-int    kdb_trap(int, struct trapframe *);
+int    kdb_trap(int, db_regs_t *);
 
 static inline void
 db_set_ddb_regs(int type, struct trapframe *tf)
@@ -130,4 +131,6 @@
 #define        DB_MACHINE_COMMANDS
 #endif
 
+void dump_trapframe(const struct trapframe *, void (*)(const char *, ...) __printflike(1, 2));
+
 #endif /* _RISCV_DB_MACHDEP_H_ */
diff -r 96e6ab20ee41 -r b6f53ace0dba sys/arch/riscv/riscv/db_interface.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/riscv/riscv/db_interface.c       Tue Sep 27 08:18:21 2022 +0000
@@ -0,0 +1,248 @@
+/*     $NetBSD: db_interface.c,v 1.1 2022/09/27 08:18:21 skrll Exp $   */
+
+/*
+ * Mach Operating System
+ * Copyright (c) 1991,1990 Carnegie Mellon University
+ * All Rights Reserved.
+ *
+ * Permission to use, copy, modify and distribute this software and its
+ * documentation is hereby granted, provided that both the copyright
+ * notice and this permission notice appear in all copies of the
+ * software, derivative works or modified versions, and any portions
+ * thereof, and that both notices appear in supporting documentation.
+ *
+ * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
+ * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
+ * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
+ *
+ * Carnegie Mellon requests users of this software to return to
+ *
+ *  Software Distribution Coordinator  or  Software.Distribution%CS.CMU.EDU@localhost
+ *  School of Computer Science
+ *  Carnegie Mellon University
+ *  Pittsburgh PA 15213-3890
+ *
+ * any improvements or extensions that they make and grant Carnegie Mellon
+ * the rights to redistribute these changes.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.1 2022/09/27 08:18:21 skrll Exp $");
+
+#ifdef _KERNEL_OPT
+#include "opt_multiprocessor.h"
+#include "opt_ddb.h"
+#include "opt_kgdb.h"
+#endif
+
+#define __PMAP_PRIVATE
+
+#include <sys/types.h>
+#include <sys/param.h>
+
+#include <sys/atomic.h>
+#include <sys/cpu.h>
+#include <sys/proc.h>
+#include <sys/reboot.h>
+#include <sys/systm.h>
+
+#include <uvm/uvm_extern.h>
+
+#include <dev/cons.h>
+
+#include <machine/db_machdep.h>
+
+#include <ddb/db_access.h>
+#include <ddb/db_user.h>
+#ifndef KGDB
+#include <ddb/db_command.h>
+#include <ddb/db_output.h>
+#include <ddb/db_sym.h>
+#include <ddb/db_extern.h>
+#include <ddb/db_interface.h>
+#include <ddb/db_lex.h>
+#include <ddb/db_run.h>        /* for db_continue_cmd() proto */
+#endif
+
+#define NOCPU   ~0
+volatile u_int ddb_cpu = NOCPU;
+
+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
+
+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);
+
+#ifdef _KERNEL
+
+#ifndef KGDB
+int
+kdb_trap(int type, db_regs_t *regs)
+{
+       int s;
+
+       switch (type) {
+       case CAUSE_BREAKPOINT:  /* breakpoint */
+               printf("kernel: breakpoint\n");
+               break;
+       case -1:                /* keyboard interrupt */
+               printf("kernel: kdbint trap\n");
+               break;
+       default:
+               printf("kernel: cause %d\n", type);
+               if (db_recover != 0) {
+                       db_error("Faulted in DDB; continuing...\n");
+                       /*NOTREACHED*/
+               }
+               break;
+       }
+
+       s = splhigh();
+
+#if defined(MULTIPROCESSOR)
+       bool first_in_ddb = false;
+       const u_int cpu_me = cpu_number();
+       const u_int old_ddb_cpu = atomic_cas_uint(&ddb_cpu, NOCPU, cpu_me);
+       if (old_ddb_cpu == NOCPU) {
+               first_in_ddb = true;
+               cpu_pause_others();
+       } else {
+               if (old_ddb_cpu != cpu_me) {
+                       KASSERT(cpu_is_paused(cpu_me));
+                       cpu_pause(regs);
+                       splx(s);
+                       return 1;
+               }
+       }
+       KASSERT(! cpu_is_paused(cpu_me));
+#endif
+
+       ddb_regs = *regs;
+       db_active++;
+       cnpollc(1);
+       db_trap(type, 0 /*code*/);
+       cnpollc(0);
+       db_active--;
+       *regs = ddb_regs;
+
+#if defined(MULTIPROCESSOR)
+       if (atomic_cas_uint(&ddb_cpu, cpu_me, NOCPU) == cpu_me) {
+               cpu_resume_others();
+       } else {
+               cpu_resume(ddb_cpu);
+               if (first_in_ddb)
+                       cpu_pause(regs);
+       }
+#endif
+
+       splx(s);
+       return 1;
+}
+#endif /* !KGDB */
+
+
+#ifndef KGDB
+void
+db_tlbdump_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
+              const char *modif)
+{
+}
+
+void
+db_kvtophys_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
+               const char *modif)
+{
+
+       if (!have_addr)
+               return;
+}
+
+
+static void
+db_mach_reset_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
+               const char *modif)
+{
+}
+
+
+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
+
+bool
+ddb_running_on_this_cpu_p(void)
+{
+       return ddb_cpu == cpu_number();
+}
+
+bool
+ddb_running_on_any_cpu_p(void)
+{
+       return ddb_cpu != NOCPU;
+}
+
+void
+db_resume_others(void)
+{
+       u_int cpu_me = cpu_number();
+
+       if (atomic_cas_uint(&ddb_cpu, cpu_me, NOCPU) == cpu_me)
+               cpu_resume_others();
+}



Home | Main Index | Thread Index | Old Index