Home | History | Annotate | Line # | Download | only in sh3
      1 /*	$NetBSD: db_interface.c,v 1.69 2026/11/10 03:43:26 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 2002 UCHIYAMA Yasushi.  All rights reserved.
      5  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.69 2026/11/10 03:43:26 thorpej Exp $");
     32 
     33 #include "opt_kgdb.h"
     34 #include "opt_kstack_debug.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 
     39 #include <uvm/uvm_extern.h>
     40 
     41 #include <dev/cons.h>
     42 
     43 #include <machine/db_machdep.h>
     44 #include <ddb/db_run.h>
     45 #include <ddb/db_sym.h>
     46 
     47 #include <sh3/ubcreg.h>
     48 
     49 db_regs_t ddb_regs;		/* register state */
     50 
     51 
     52 #ifndef KGDB
     53 #include <sh3/cache.h>
     54 #include <sh3/cache_sh3.h>
     55 #include <sh3/cache_sh4.h>
     56 #include <sh3/mmu.h>
     57 #include <sh3/mmu_sh3.h>
     58 #include <sh3/mmu_sh4.h>
     59 
     60 #include <ddb/db_active.h>
     61 #include <ddb/db_command.h>
     62 #include <ddb/db_extern.h>
     63 #include <ddb/db_output.h>
     64 #include <ddb/ddbvar.h>
     65 
     66 static void kdb_printtrap(u_int, int);
     67 
     68 static void db_tlbdump_cmd(db_expr_t, bool, db_expr_t, const char *);
     69 #ifdef SH4
     70 static __noinline void
     71 __db_get_itlb_sh4(uint32_t, uint32_t *, uint32_t *, uint32_t *);
     72 static __noinline void
     73 __db_get_utlb_sh4(uint32_t, uint32_t *, uint32_t *, uint32_t *);
     74 #endif
     75 
     76 static char *__db_procname_by_asid(int);
     77 static void __db_tlbdump_pfn(uint32_t);
     78 #ifdef SH4
     79 static void __db_tlbdump_page_size_sh4(uint32_t);
     80 #endif
     81 
     82 static void db_cachedump_cmd(db_expr_t, bool, db_expr_t, const char *);
     83 #ifdef SH3
     84 static __noinline void __db_cachedump_sh3(vaddr_t);
     85 #endif
     86 #ifdef SH4
     87 static __noinline void __db_cachedump_sh4(vaddr_t);
     88 #endif
     89 
     90 static void db_frame_cmd(db_expr_t, bool, db_expr_t, const char *);
     91 static void __db_print_symbol(db_expr_t);
     92 static void __db_print_tfstack(struct trapframe *, struct trapframe *);
     93 
     94 static void db_reset_cmd(db_expr_t, bool, db_expr_t, const char *);
     95 
     96 #ifdef KSTACK_DEBUG
     97 static void db_stackcheck_cmd(db_expr_t, bool, db_expr_t, const char *);
     98 #endif
     99 
    100 
    101 const struct db_command db_machine_command_table[] = {
    102 	{ DDB_ADD_CMD("cache", db_cachedump_cmd, 0,
    103 		"Dump contents of the cache address array.",
    104 		"[address]",
    105 		"   address: if specified, dump only matching entries" ) },
    106 
    107 	{ DDB_ADD_CMD("frame", db_frame_cmd, 0,
    108 		"Dump switch frame and trap frames of curlwp.",
    109 		NULL, NULL) },
    110 
    111 	{ DDB_ADD_CMD("reset", db_reset_cmd, 0,
    112 		"Reset machine (by taking a trap with exceptions disabled).",
    113 		NULL, NULL) },
    114 #ifdef KSTACK_DEBUG
    115 	{ DDB_ADD_CMD("stack", db_stackcheck_cmd, 0,
    116 		"Dump kernel stacks of all lwps.",
    117 		NULL, NULL) },
    118 #endif
    119 	{ DDB_ADD_CMD("tlb", db_tlbdump_cmd, 0,
    120 		"Dump TLB contents.",
    121 		NULL, NULL) },
    122 
    123 	{ DDB_END_CMD },
    124 };
    125 
    126 static void
    127 kdb_printtrap(u_int type, int code)
    128 {
    129 	int i;
    130 	i = type >> 5;
    131 
    132 	db_printf("%s mode trap: ", type & 1 ? "user" : "kernel");
    133 	if (i >= exp_types)
    134 		db_printf("type 0x%03x", type & ~1);
    135 	else
    136 		db_printf("%s", exp_type[i]);
    137 
    138 	db_printf(" code = 0x%x\n", code);
    139 }
    140 
    141 int
    142 kdb_trap(int type, int code, db_regs_t *regs)
    143 {
    144 	int s;
    145 
    146 	switch (type) {
    147 	case EXPEVT_TRAPA:	/* FALLTHROUGH */
    148 	case EXPEVT_BREAK:
    149 		break;
    150 
    151 	default:
    152 		if (!db_onpanic && db_recover == NULL)
    153 			return 0;
    154 
    155 		kdb_printtrap(type, code);
    156 		if (db_recover != NULL) {
    157 			db_printf("[pc %x, pr %x]: ",
    158 				  regs->tf_spc, regs->tf_pr);
    159 			db_error("Faulted in DDB; continuing...\n");
    160 			/*NOTREACHED*/
    161 		}
    162 	}
    163 
    164 	/* XXX: Should switch to ddb's own stack here. */
    165 
    166 	ddb_regs = *regs;
    167 
    168 	s = splhigh();
    169 	db_active++;
    170 	cnpollc(true);
    171 
    172 	db_trap(type, code);
    173 
    174 	cnpollc(false);
    175 	db_active--;
    176 	splx(s);
    177 
    178 	*regs = ddb_regs;
    179 
    180 	return 1;
    181 }
    182 
    183 void
    184 cpu_Debugger(void)
    185 {
    186 
    187 	__asm volatile("trapa %0" :: "i"(_SH_TRA_BREAK));
    188 }
    189 #endif /* !KGDB */
    190 
    191 #define	M_BSR	0xf000
    192 #define	I_BSR	0xb000
    193 #define	M_BSRF	0xf0ff
    194 #define	I_BSRF	0x0003
    195 #define	M_JSR	0xf0ff
    196 #define	I_JSR	0x400b
    197 #define	M_RTS	0xffff
    198 #define	I_RTS	0x000b
    199 #define	M_RTE	0xffff
    200 #define	I_RTE	0x002b
    201 
    202 bool
    203 inst_call(int inst)
    204 {
    205 #if _BYTE_ORDER == BIG_ENDIAN
    206 	inst >>= 16;
    207 #endif
    208 	return (inst & M_BSR) == I_BSR || (inst & M_BSRF) == I_BSRF ||
    209 	       (inst & M_JSR) == I_JSR;
    210 }
    211 
    212 bool
    213 inst_return(int inst)
    214 {
    215 #if _BYTE_ORDER == BIG_ENDIAN
    216 	inst >>= 16;
    217 #endif
    218 	return (inst & M_RTS) == I_RTS;
    219 }
    220 
    221 bool
    222 inst_trap_return(int inst)
    223 {
    224 #if _BYTE_ORDER == BIG_ENDIAN
    225 	inst >>= 16;
    226 #endif
    227 	return (inst & M_RTE) == I_RTE;
    228 }
    229 
    230 void
    231 db_set_single_step(db_regs_t *regs)
    232 {
    233 
    234 	/*
    235 	 * Channel A is set up for single stepping in sh_cpu_init().
    236 	 * Before RTE we write tf_ubc to BBRA and tf_spc to BARA.
    237 	 */
    238 #ifdef SH3
    239 	if (CPU_IS_SH3) {
    240 		regs->tf_ubc = UBC_CYCLE_INSN | UBC_CYCLE_READ
    241 			| SH3_UBC_CYCLE_CPU;
    242 	}
    243 #endif
    244 #ifdef SH4
    245 	if (CPU_IS_SH4) {
    246 		regs->tf_ubc = UBC_CYCLE_INSN | UBC_CYCLE_READ;
    247 	}
    248 #endif
    249 }
    250 
    251 void
    252 db_clear_single_step(db_regs_t *regs)
    253 {
    254 
    255 	regs->tf_ubc = 0;
    256 }
    257 
    258 #ifndef KGDB
    259 
    260 #define	ON(x, c)	((x) & (c) ? '|' : '.')
    261 
    262 /*
    263  * MMU
    264  */
    265 static void
    266 db_tlbdump_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    267     const char *modif)
    268 {
    269 	static const char *pr[] = { "_r", "_w", "rr", "ww" };
    270 	static const char title[] =
    271 	    "   VPN      ASID    PFN  AREA VDCGWtPR  SZ";
    272 	static const char title2[] =
    273 	    "          U/K                       U/K";
    274 	uint32_t r, e;
    275 	int i;
    276 #ifdef SH3
    277 	if (CPU_IS_SH3) {
    278 		/* MMU configuration. */
    279 		r = _reg_read_4(SH3_MMUCR);
    280 		db_printf("%s-mode, %s virtual storage mode\n",
    281 		    r & SH3_MMUCR_IX
    282 		    ? "ASID + VPN" : "VPN only",
    283 		    r & SH3_MMUCR_SV ? "single" : "multiple");
    284 		i = _reg_read_4(SH3_PTEH) & SH3_PTEH_ASID_MASK;
    285 		db_printf("ASID=%d (%s)", i, __db_procname_by_asid(i));
    286 
    287 		db_printf("---TLB DUMP---\n%s\n%s\n", title, title2);
    288 		for (i = 0; i < SH3_MMU_WAY; i++) {
    289 			db_printf(" [way %d]\n", i);
    290 			for (e = 0; e < SH3_MMU_ENTRY; e++) {
    291 				uint32_t a;
    292 				/* address/data array common offset. */
    293 				a = (e << SH3_MMU_VPN_SHIFT) |
    294 				    (i << SH3_MMU_WAY_SHIFT);
    295 
    296 				r = _reg_read_4(SH3_MMUAA | a);
    297 				if (r == 0) {
    298 					db_printf("---------- - --- ----------"
    299 					    " - ----x --  --\n");
    300 				} else {
    301 					vaddr_t va;
    302 					int asid;
    303 					asid = r & SH3_MMUAA_D_ASID_MASK;
    304 					r &= SH3_MMUAA_D_VPN_MASK_1K;
    305 					va = r | (e << SH3_MMU_VPN_SHIFT);
    306 					db_printf("0x%08lx %c %3d", va,
    307 					    (int)va < 0 ? 'K' : 'U', asid);
    308 
    309 					r = _reg_read_4(SH3_MMUDA | a);
    310 					__db_tlbdump_pfn(r);
    311 
    312 					db_printf(" %c%c%c%cx %s %2dK\n",
    313 					    ON(r, SH3_MMUDA_D_V),
    314 					    ON(r, SH3_MMUDA_D_D),
    315 					    ON(r, SH3_MMUDA_D_C),
    316 					    ON(r, SH3_MMUDA_D_SH),
    317 					    pr[(r & SH3_MMUDA_D_PR_MASK) >>
    318 						SH3_MMUDA_D_PR_SHIFT],
    319 					    r & SH3_MMUDA_D_SZ ? 4 : 1);
    320 				}
    321 			}
    322 		}
    323 	}
    324 #endif /* SH3 */
    325 #ifdef SH4
    326 	if (CPU_IS_SH4) {
    327 		void (*get_itlb_p2)(uint32_t, uint32_t *, uint32_t *, uint32_t *);
    328 		void (*get_utlb_p2)(uint32_t, uint32_t *, uint32_t *, uint32_t *);
    329 		uint32_t aa, da1, da2;
    330 
    331 		get_itlb_p2 = SH3_P2SEG_FUNC(__db_get_itlb_sh4);
    332 		get_utlb_p2 = SH3_P2SEG_FUNC(__db_get_utlb_sh4);
    333 
    334 		/* MMU configuration */
    335 		r = _reg_read_4(SH4_MMUCR);
    336 		db_printf("%s virtual storage mode, SQ access: (kernel%s)\n",
    337 		    r & SH3_MMUCR_SV ? "single" : "multiple",
    338 		    r & SH4_MMUCR_SQMD ? "" : "/user");
    339 		db_printf("random counter limit=%d\n",
    340 		    (r & SH4_MMUCR_URB_MASK) >> SH4_MMUCR_URB_SHIFT);
    341 
    342 		i = _reg_read_4(SH4_PTEH) & SH4_PTEH_ASID_MASK;
    343 		db_printf("ASID=%d (%s)", i, __db_procname_by_asid(i));
    344 
    345 		/* Dump ITLB */
    346 		db_printf("---ITLB DUMP ---\n%s TC SA\n%s\n", title, title2);
    347 		for (i = 0; i < 4; i++) {
    348 			e = i << SH4_ITLB_E_SHIFT;
    349 
    350 			(*get_itlb_p2)(e, &aa, &da1, &da2);
    351 
    352 			db_printf("0x%08x   %3d",
    353 			    aa & SH4_ITLB_AA_VPN_MASK,
    354 			    aa & SH4_ITLB_AA_ASID_MASK);
    355 
    356 			__db_tlbdump_pfn(da1);
    357 			db_printf(" %c_%c%c_ %s ",
    358 			    ON(da1, SH4_ITLB_DA1_V),
    359 			    ON(da1, SH4_ITLB_DA1_C),
    360 			    ON(da1, SH4_ITLB_DA1_SH),
    361 			    pr[(da1 & SH4_ITLB_DA1_PR) >>
    362 				SH4_UTLB_DA1_PR_SHIFT]);
    363 			__db_tlbdump_page_size_sh4(da1);
    364 
    365 			db_printf(" %c  %d\n",
    366 			    ON(da2, SH4_ITLB_DA2_TC),
    367 			    da2 & SH4_ITLB_DA2_SA_MASK);
    368 		}
    369 
    370 		/* Dump UTLB */
    371 		db_printf("---UTLB DUMP---\n%s TC SA\n%s\n", title, title2);
    372 		for (i = 0; i < 64; i++) {
    373 			e = i << SH4_UTLB_E_SHIFT;
    374 
    375 			(*get_utlb_p2)(e, &aa, &da1, &da2);
    376 
    377 			db_printf("0x%08x   %3d",
    378 			    aa & SH4_UTLB_AA_VPN_MASK,
    379 			    aa & SH4_UTLB_AA_ASID_MASK);
    380 
    381 			__db_tlbdump_pfn(da1);
    382 			db_printf(" %c%c%c%c%c %s ",
    383 			    ON(da1, SH4_UTLB_DA1_V),
    384 			    ON(da1, SH4_UTLB_DA1_D),
    385 			    ON(da1, SH4_UTLB_DA1_C),
    386 			    ON(da1, SH4_UTLB_DA1_SH),
    387 			    ON(da1, SH4_UTLB_DA1_WT),
    388 			    pr[(da1 & SH4_UTLB_DA1_PR_MASK) >>
    389 				SH4_UTLB_DA1_PR_SHIFT]
    390 			    );
    391 			__db_tlbdump_page_size_sh4(da1);
    392 
    393 			db_printf(" %c  %d\n",
    394 			    ON(da2, SH4_UTLB_DA2_TC),
    395 			    da2 & SH4_UTLB_DA2_SA_MASK);
    396 		}
    397 	}
    398 #endif /* SH4 */
    399 }
    400 
    401 
    402 #ifdef SH4
    403 static __noinline void
    404 __db_get_itlb_sh4(uint32_t e, uint32_t *paa, uint32_t *pda1, uint32_t *pda2)
    405 {
    406 
    407 	*paa = _reg_read_4(SH4_ITLB_AA | e);
    408 	*pda1 = _reg_read_4(SH4_ITLB_DA1 | e);
    409 	*pda2 = _reg_read_4(SH4_ITLB_DA2 | e);
    410 }
    411 
    412 
    413 static __noinline void
    414 __db_get_utlb_sh4(uint32_t e, uint32_t *paa, uint32_t *pda1, uint32_t *pda2)
    415 {
    416 
    417 	*paa = _reg_read_4(SH4_UTLB_AA | e);
    418 	*pda1 = _reg_read_4(SH4_UTLB_DA1 | e);
    419 	*pda2 = _reg_read_4(SH4_UTLB_DA2 | e);
    420 }
    421 #endif	/* SH4 */
    422 
    423 
    424 static void
    425 __db_tlbdump_pfn(uint32_t r)
    426 {
    427 	uint32_t pa = (r & SH3_MMUDA_D_PPN_MASK);
    428 
    429 	db_printf(" 0x%08x %d", pa, (pa >> 26) & 7);
    430 }
    431 
    432 static char *
    433 __db_procname_by_asid(int asid)
    434 {
    435 	static char notfound[] = "---";
    436 	struct proc *p;
    437 
    438 	LIST_FOREACH(p, &allproc, p_list) {
    439 		if (p->p_vmspace->vm_map.pmap->pm_asid == asid)
    440 			return (p->p_comm);
    441 	}
    442 
    443 	return (notfound);
    444 }
    445 
    446 #ifdef SH4
    447 static void
    448 __db_tlbdump_page_size_sh4(uint32_t r)
    449 {
    450 	switch (r & SH4_PTEL_SZ_MASK) {
    451 	case SH4_PTEL_SZ_1K:
    452 		db_printf(" 1K");
    453 		break;
    454 	case SH4_PTEL_SZ_4K:
    455 		db_printf(" 4K");
    456 		break;
    457 	case SH4_PTEL_SZ_64K:
    458 		db_printf("64K");
    459 		break;
    460 	case SH4_PTEL_SZ_1M:
    461 		db_printf(" 1M");
    462 		break;
    463 	}
    464 }
    465 #endif /* SH4 */
    466 
    467 /*
    468  * CACHE
    469  */
    470 static void
    471 db_cachedump_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    472     const char *modif)
    473 {
    474 	void (*cachedump_p2)(vaddr_t);
    475 
    476 #ifdef SH3
    477 	if (CPU_IS_SH3)
    478 		cachedump_p2 = SH3_P2SEG_FUNC(__db_cachedump_sh3);
    479 #endif
    480 #ifdef SH4
    481 	if (CPU_IS_SH4)
    482 		cachedump_p2 = SH3_P2SEG_FUNC(__db_cachedump_sh4);
    483 #endif
    484 	(*cachedump_p2)(have_addr ? addr : 0);
    485 }
    486 
    487 #ifdef SH3
    488 static __noinline void
    489 __db_cachedump_sh3(vaddr_t va_start)
    490 {
    491 	uint32_t r;
    492 	vaddr_t va, va_end, cca;
    493 	int entry, way;
    494 
    495 	/* disable cache */
    496 	_reg_write_4(SH3_CCR,
    497 	    _reg_read_4(SH3_CCR) & ~SH3_CCR_CE);
    498 
    499 	if (va_start) {
    500 		va = va_start & ~(sh_cache_line_size - 1);
    501 		va_end = va + sh_cache_line_size;
    502 	} else {
    503 		va = 0;
    504 		va_end = sh_cache_way_size;
    505 	}
    506 
    507 	db_printf("%d-way, way-size=%dB, way-shift=%d, entry-mask=%08x, "
    508 	    "line-size=%dB \n", sh_cache_ways, sh_cache_way_size,
    509 	    sh_cache_way_shift, sh_cache_entry_mask, sh_cache_line_size);
    510 	db_printf("Entry  Way 0  UV   Way 1  UV   Way 2  UV   Way 3  UV\n");
    511 	for (; va < va_end; va += sh_cache_line_size) {
    512 		entry = va & sh_cache_entry_mask;
    513 		cca = SH3_CCA | entry;
    514 		db_printf(" %3d ", entry >> CCA_ENTRY_SHIFT);
    515 		for (way = 0; way < sh_cache_ways; way++) {
    516 			r = _reg_read_4(cca | (way << sh_cache_way_shift));
    517 			db_printf("%08x %c%c ", r & CCA_TAGADDR_MASK,
    518 			    ON(r, CCA_U), ON(r, CCA_V));
    519 		}
    520 		db_printf("\n");
    521 	}
    522 
    523 	/* enable cache */
    524 	_reg_bset_4(SH3_CCR, SH3_CCR_CE);
    525 	sh_icache_sync_all();
    526 }
    527 #endif /* SH3 */
    528 
    529 #ifdef SH4
    530 static __noinline void
    531 __db_cachedump_sh4(vaddr_t va)
    532 {
    533 	uint32_t r, e;
    534 	int i, istart, iend;
    535 
    536 	/* disable I/D-cache */
    537 	_reg_write_4(SH4_CCR,
    538 	    _reg_read_4(SH4_CCR) & ~(SH4_CCR_ICE | SH4_CCR_OCE));
    539 
    540 	if (va) {
    541 		istart = ((va & CCIA_ENTRY_MASK) >> CCIA_ENTRY_SHIFT) & ~3;
    542 		iend = istart + 4;
    543 	} else {
    544 		istart = 0;
    545 		iend = SH4_ICACHE_SIZE / SH4_CACHE_LINESZ;
    546 	}
    547 
    548 	db_printf("[I-cache]\n");
    549 	db_printf("  Entry             V           V           V           V");
    550 	for (i = istart; i < iend; i++) {
    551 		if ((i & 3) == 0)
    552 			db_printf("\n[%3d-%3d] ", i, i + 3);
    553 		r = _reg_read_4(SH4_CCIA | (i << CCIA_ENTRY_SHIFT));
    554 		db_printf("%08x _%c ", r & CCIA_TAGADDR_MASK, ON(r, CCIA_V));
    555 	}
    556 
    557 	db_printf("\n\n[D-cache]\n");
    558 	db_printf("  Entry            UV          UV          UV          UV");
    559 	for (i = istart; i < iend; i++) {
    560 		if ((i & 3) == 0)
    561 			db_printf("\n[%3d-%3d] ", i, i + 3);
    562 		e = (i << CCDA_ENTRY_SHIFT);
    563 		r = _reg_read_4(SH4_CCDA | e);
    564 		db_printf("%08x %c%c ", r & CCDA_TAGADDR_MASK, ON(r, CCDA_U),
    565 		    ON(r, CCDA_V));
    566 
    567 	}
    568 	db_printf("\n");
    569 
    570 	_reg_write_4(SH4_CCR,
    571 	    _reg_read_4(SH4_CCR) | SH4_CCR_ICE | SH4_CCR_OCE);
    572 	sh_icache_sync_all();
    573 }
    574 #endif /* SH4 */
    575 
    576 #undef ON
    577 
    578 static void
    579 db_frame_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    580 {
    581 	struct switchframe *sf = &curpcb->pcb_sf;
    582 	struct trapframe *tf, *tfbot;
    583 
    584 	/* Print switch frame */
    585 	db_printf("[switch frame]\n");
    586 
    587 #define	SF(x)	db_printf("sf_" #x "\t\t0x%08x\t", sf->sf_ ## x);	\
    588 		__db_print_symbol(sf->sf_ ## x)
    589 
    590 	SF(sr);
    591 	SF(pr);
    592 	SF(gbr);
    593 	SF(r8);
    594 	SF(r9);
    595 	SF(r10);
    596 	SF(r11);
    597 	SF(r12);
    598 	SF(r13);
    599 	SF(r14);
    600 	SF(r15);
    601 	db_printf("sf_r6_bank\t0x%08x\n", sf->sf_r6_bank);
    602 	db_printf("sf_r7_bank\t0x%08x\n", sf->sf_r7_bank);
    603 #undef	SF
    604 
    605 
    606 	/* Print trap frame stack */
    607 	tfbot = (struct trapframe *)((vaddr_t)curpcb + PAGE_SIZE);
    608 
    609 	__asm("stc r6_bank, %0" : "=r"(tf));
    610 
    611 	db_printf("[trap frames]\n");
    612 	__db_print_tfstack(tf, tfbot);
    613 }
    614 
    615 
    616 static void
    617 __db_print_tfstack(struct trapframe *tf, struct trapframe *tfbot)
    618 {
    619 	db_printf("[[-- dumping frames from 0x%08x to 0x%08x --]]\n",
    620 		  (uint32_t)tf, (uint32_t)tfbot);
    621 
    622 	for (; tf != tfbot; tf++) {
    623 		db_printf("-- %p-%p --\n", tf, tf + 1);
    624 		db_printf("tf_expevt\t0x%08x\n", tf->tf_expevt);
    625 
    626 #define	TF(x)	db_printf("tf_" #x "\t\t0x%08x\t", tf->tf_ ## x);	\
    627 		__db_print_symbol(tf->tf_ ## x)
    628 
    629 		TF(ubc);
    630 		TF(ssr);
    631 		TF(spc);
    632 		TF(pr);
    633 		TF(gbr);
    634 		TF(macl);
    635 		TF(mach);
    636 		TF(r0);
    637 		TF(r1);
    638 		TF(r2);
    639 		TF(r3);
    640 		TF(r4);
    641 		TF(r5);
    642 		TF(r6);
    643 		TF(r7);
    644 		TF(r8);
    645 		TF(r9);
    646 		TF(r10);
    647 		TF(r11);
    648 		TF(r12);
    649 		TF(r13);
    650 		TF(r14);
    651 		TF(r15);
    652 #undef	TF
    653 	}
    654 }
    655 
    656 
    657 static void
    658 __db_print_symbol(db_expr_t value)
    659 {
    660 	const char *name;
    661 	db_expr_t offset;
    662 
    663 	db_find_sym_and_offset((db_addr_t)value, &name, &offset);
    664 	if (name != NULL && offset <= db_maxoff && offset != value)
    665 		db_printsym(value, DB_STGY_ANY, db_printf);
    666 
    667 	db_printf("\n");
    668 }
    669 
    670 #ifdef KSTACK_DEBUG
    671 /*
    672  * Stack overflow check
    673  */
    674 static void
    675 db_stackcheck_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    676 		  const char *modif)
    677 {
    678 	struct lwp *l;
    679 	struct pcb *pcb;
    680 	uint32_t *t32;
    681 	uint8_t *t8;
    682 	int i, j;
    683 
    684 #define	MAX_STACK	(USPACE - PAGE_SIZE)
    685 #define	MAX_FRAME	(PAGE_SIZE - sizeof(struct pcb))
    686 
    687 	db_printf("stack max: %d byte, frame max %d byte,"
    688 	    " sizeof(struct trapframe) %d byte\n", MAX_STACK, MAX_FRAME,
    689 	    sizeof(struct trapframe));
    690 	db_printf("   PID.LID    "
    691 		  "stack bot    max used    frame bot     max used"
    692 		  "  nest\n");
    693 
    694 	LIST_FOREACH(l, &alllwp, l_list) {
    695 		pcb = lwp_getpcb(l);
    696 		/* stack */
    697 		t32 = (uint32_t *)(pcb->pcb_sf.sf_r7_bank - MAX_STACK);
    698 		for (i = 0; *t32++ == 0xa5a5a5a5; i++)
    699 			continue;
    700 		i = MAX_STACK - i * sizeof(int);
    701 
    702 		/* frame */
    703 		t8 = (uint8_t *)((vaddr_t)pcb + PAGE_SIZE - MAX_FRAME);
    704 		for (j = 0; *t8++ == 0x5a; j++)
    705 			continue;
    706 		j = MAX_FRAME - j;
    707 
    708 		db_printf("%6d.%-6d 0x%08x %6d (%3d%%) 0x%08lx %6d (%3d%%) %d %s\n",
    709 		    l->l_proc->p_pid, l->l_lid,
    710 		    pcb->pcb_sf.sf_r7_bank, i, i * 100 / MAX_STACK,
    711 		    (vaddr_t)pcb + PAGE_SIZE, j, j * 100 / MAX_FRAME,
    712 		    j / sizeof(struct trapframe),
    713 		    l->l_proc->p_comm);
    714 	}
    715 #undef	MAX_STACK
    716 #undef	MAX_FRAME
    717 }
    718 #endif /* KSTACK_DEBUG */
    719 
    720 
    721 static void
    722 db_reset_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    723 	     const char *modif)
    724 {
    725     _cpu_exception_suspend();
    726     __asm volatile("trapa %0" :: "i"(_SH_TRA_BREAK));
    727 
    728     /* NOTREACHED, but just in case ... */
    729     printf("Reset failed\n");
    730 }
    731 
    732 #endif /* !KGDB */
    733