Home | History | Annotate | Line # | Download | only in powerpc
      1 /*	$NetBSD: db_interface.c,v 1.66 2026/11/10 03:43:26 thorpej Exp $ */
      2 /*	$OpenBSD: db_interface.c,v 1.2 1996/12/28 06:21:50 rahnds Exp $	*/
      3 
      4 #include <sys/cdefs.h>
      5 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.66 2026/11/10 03:43:26 thorpej Exp $");
      6 
      7 #define USERACC
      8 
      9 #ifdef _KERNEL_OPT
     10 #include "opt_ddb.h"
     11 #include "opt_kgdb.h"
     12 #include "opt_multiprocessor.h"
     13 #include "opt_ppcarch.h"
     14 #endif
     15 
     16 #include <sys/param.h>
     17 #include <sys/proc.h>
     18 #include <sys/systm.h>
     19 #include <sys/cpu.h>
     20 #include <sys/atomic.h>
     21 
     22 #include <dev/cons.h>
     23 
     24 #include <powerpc/db_machdep.h>
     25 #include <powerpc/frame.h>
     26 #include <powerpc/spr.h>
     27 #include <powerpc/pte.h>
     28 #include <powerpc/psl.h>
     29 
     30 #if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
     31 #include <powerpc/oea/spr.h>
     32 #include <powerpc/oea/bat.h>
     33 #include <powerpc/oea/cpufeat.h>
     34 #endif
     35 
     36 #ifdef PPC_IBM4XX
     37 #include <powerpc/ibm4xx/cpu.h>
     38 #include <powerpc/ibm4xx/spr.h>
     39 #include <powerpc/ibm4xx/tlb.h>
     40 #include <uvm/uvm_extern.h>
     41 #endif
     42 
     43 #ifdef PPC_BOOKE
     44 #include <powerpc/booke/cpuvar.h>
     45 #include <powerpc/booke/spr.h>
     46 #endif
     47 
     48 #ifdef DDB
     49 #include <ddb/db_sym.h>
     50 #include <ddb/db_command.h>
     51 #include <ddb/db_extern.h>
     52 #include <ddb/db_access.h>
     53 #include <ddb/db_active.h>
     54 #include <ddb/db_lex.h>
     55 #include <ddb/db_output.h>
     56 #include <ddb/db_run.h>	/* for db_continue_cmd() proto */
     57 #include <ddb/ddbvar.h>
     58 #endif
     59 
     60 #ifdef KGDB
     61 #include <sys/kgdb.h>
     62 #define db_printf printf
     63 #endif
     64 
     65 #include <dev/ofw/openfirm.h>
     66 
     67 #define NOCPU   ~0
     68 volatile u_int ddb_cpu = NOCPU;
     69 
     70 db_regs_t ddb_regs;
     71 
     72 void ddb_trap(void);				/* Call into trap_subr.S */
     73 int ddb_trap_glue(struct trapframe *);		/* Called from trap_subr.S */
     74 #ifdef DDB
     75 #if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
     76 static void db_show_bat(db_expr_t, bool, db_expr_t, const char *);
     77 static void db_show_mmu(db_expr_t, bool, db_expr_t, const char *);
     78 #endif /* PPC_OEA || PPC_OEA64 || PPC_OEA64_BRIDGE */
     79 #ifdef PPC_IBM4XX
     80 static void db_ppc4xx_ctx(db_expr_t, bool, db_expr_t, const char *);
     81 static void db_ppc4xx_pv(db_expr_t, bool, db_expr_t, const char *);
     82 static void db_ppc4xx_reset(db_expr_t, bool, db_expr_t, const char *);
     83 static void db_ppc4xx_tf(db_expr_t, bool, db_expr_t, const char *);
     84 static void db_ppc4xx_dumptlb(db_expr_t, bool, db_expr_t, const char *);
     85 static void db_ppc4xx_dcr(db_expr_t, bool, db_expr_t, const char *);
     86 static db_expr_t db_ppc4xx_mfdcr(db_expr_t);
     87 static void db_ppc4xx_mtdcr(db_expr_t, db_expr_t);
     88 #ifdef USERACC
     89 static void db_ppc4xx_useracc(db_expr_t, bool, db_expr_t, const char *);
     90 #endif
     91 #endif /* PPC_IBM4XX */
     92 
     93 #ifdef PPC_BOOKE
     94 static void db_ppcbooke_reset(db_expr_t, bool, db_expr_t, const char *);
     95 static void db_ppcbooke_splhist(db_expr_t, bool, db_expr_t, const char *);
     96 static void db_ppcbooke_tf(db_expr_t, bool, db_expr_t, const char *);
     97 static void db_ppcbooke_dumptlb(db_expr_t, bool, db_expr_t, const char *);
     98 #endif
     99 
    100 #ifdef MULTIPROCESSOR
    101 static void db_mach_cpu(db_expr_t, bool, db_expr_t, const char *);
    102 #endif	/* MULTIPROCESSOR */
    103 
    104 const struct db_command db_machine_command_table[] = {
    105 #if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    106 	{ DDB_ADD_CMD("bat",	db_show_bat,		0,
    107 	  "Print BAT register translations", NULL,NULL) },
    108 	{ DDB_ADD_CMD("mmu",	db_show_mmu,		0,
    109 	  "Print MMU registers", NULL,NULL) },
    110 #endif /* PPC_OEA || PPC_OEA64 || PPC_OEA64_BRIDGE */
    111 #ifdef PPC_IBM4XX
    112 	{ DDB_ADD_CMD("ctx",	db_ppc4xx_ctx,		0,
    113 	  "Print process MMU context information", NULL,NULL) },
    114 	{ DDB_ADD_CMD("pv",	db_ppc4xx_pv,		0,
    115 	  "Print PA->VA mapping information",
    116 	  "address",
    117 	  "   address:\tphysical address to look up") },
    118 	{ DDB_ADD_CMD("reset",	db_ppc4xx_reset,	0,
    119 	  "Reset the system ", NULL,NULL) },
    120 	{ DDB_ADD_CMD("tf",	db_ppc4xx_tf,		0,
    121 	  "Display the contents of the trapframe",
    122 	  "address",
    123 	  "   address:\tthe struct trapframe to print") },
    124 	{ DDB_ADD_CMD("tlb",	db_ppc4xx_dumptlb,	0,
    125 	  "Display instruction translation storage buffer information.",
    126 	  NULL,NULL) },
    127 	{ DDB_ADD_CMD("dcr",	db_ppc4xx_dcr,		CS_MORE|CS_SET_DOT,
    128 	  "Set the DCR register",
    129 	  "dcr",
    130 	  "   dcr:\tNew DCR value (between 0x0 and 0x3ff)") },
    131 #ifdef USERACC
    132 	{ DDB_ADD_CMD("user",	db_ppc4xx_useracc,	0,
    133 	   "Display user memory.", "[address][,count]",
    134 	   "   address:\tuserspace address to start\n"
    135 	   "   count:\tnumber of bytes to display") },
    136 #endif
    137 #endif /* PPC_IBM4XX */
    138 #ifdef PPC_BOOKE
    139 	{ DDB_ADD_CMD("reset",	db_ppcbooke_reset,	0,
    140 	  "Reset the system ", NULL,NULL) },
    141 	{ DDB_ADD_CMD("tf",	db_ppcbooke_tf,		0,
    142 	  "Display the contents of the trapframe",
    143 	  "address",
    144 	  "   address:\tthe struct trapframe to print") },
    145 	{ DDB_ADD_CMD("splhist", db_ppcbooke_splhist,	0,
    146 	  "Display the splraise/splx splx",
    147 	  NULL, NULL) },
    148 	{ DDB_ADD_CMD("tlb",	db_ppcbooke_dumptlb,	0,
    149 	  "Display instruction translation storage buffer information.",
    150 	  NULL,NULL) },
    151 #endif /* PPC_BOOKE */
    152 
    153 #ifdef MULTIPROCESSOR
    154 	{ DDB_ADD_CMD("cpu",	db_mach_cpu,	0,
    155 	  "switch to another cpu", "cpu-no", NULL) },
    156 #endif	/* MULTIPROCESSOR */
    157 
    158 	{ DDB_END_CMD },
    159 };
    160 
    161 void
    162 cpu_Debugger(void)
    163 {
    164 #ifdef PPC_BOOKE
    165 	const register_t msr = mfmsr();
    166 	__asm volatile("wrteei 0\n\ttweq\t1,1");
    167 	mtmsr(msr);
    168 	__asm volatile("isync");
    169 #else
    170 	ddb_trap();
    171 #endif
    172 }
    173 #endif /* DDB */
    174 
    175 int
    176 ddb_trap_glue(struct trapframe *tf)
    177 {
    178 #if defined(PPC_IBM4XX) || defined(PPC_BOOKE)
    179 	if ((tf->tf_srr1 & PSL_PR) == 0)
    180 		return kdb_trap(tf->tf_exc, tf);
    181 #else /* PPC_OEA */
    182 	if ((tf->tf_srr1 & PSL_PR) == 0 &&
    183 	    (tf->tf_exc == EXC_TRC ||
    184 	     tf->tf_exc == EXC_RUNMODETRC ||
    185 	     (tf->tf_exc == EXC_PGM && (tf->tf_srr1 & 0x20000)) ||
    186 	     tf->tf_exc == EXC_BPT ||
    187 	     tf->tf_exc == EXC_DSI)) {
    188 		int type = tf->tf_exc;
    189 		if (type == EXC_PGM && (tf->tf_srr1 & 0x20000)) {
    190 			type = T_BREAKPOINT;
    191 		}
    192 		return kdb_trap(type, tf);
    193 	}
    194 #endif
    195 	return 0;
    196 }
    197 
    198 int
    199 kdb_trap(int type, void *v)
    200 {
    201 	struct trapframe *tf = v;
    202 	int rv = 1;
    203 	int s;
    204 
    205 #ifdef DDB
    206 	if (db_recover != 0 && (type != -1 && type != T_BREAKPOINT)) {
    207 		db_error("Faulted in DDB; continuing...\n");
    208 		/* NOTREACHED */
    209 	}
    210 #endif
    211 
    212 	/* XXX Should switch to kdb's own stack here. */
    213 
    214 #ifdef MULTIPROCESSOR
    215 	bool first_in_ddb = false;
    216 	const u_int cpu_me = cpu_number();
    217 	const u_int old_ddb_cpu = atomic_cas_uint(&ddb_cpu, NOCPU, cpu_me);
    218 	if (old_ddb_cpu == NOCPU) {
    219 		first_in_ddb = true;
    220 		cpu_pause_others();
    221 	} else {
    222 		if (old_ddb_cpu != cpu_me) {
    223 			KASSERT(cpu_is_paused(cpu_me));
    224 			cpu_pause(tf);
    225 			return 1;
    226 		}
    227 	}
    228 	KASSERT(!cpu_is_paused(cpu_me));
    229 #endif	/* MULTIPROCESSOR */
    230 
    231 	s = splhigh();
    232 	memcpy(DDB_REGS->r, tf->tf_fixreg, 32 * sizeof(u_int32_t));
    233 	DDB_REGS->iar = tf->tf_srr0;
    234 	DDB_REGS->msr = tf->tf_srr1;
    235 	DDB_REGS->lr = tf->tf_lr;
    236 	DDB_REGS->ctr = tf->tf_ctr;
    237 	DDB_REGS->cr = tf->tf_cr;
    238 	DDB_REGS->xer = tf->tf_xer;
    239 #ifdef PPC_OEA
    240 	DDB_REGS->mq = tf->tf_mq;
    241 #elif defined(PPC_IBM4XX) || defined(PPC_BOOKE)
    242 	DDB_REGS->dear = tf->tf_dear;
    243 	DDB_REGS->esr = tf->tf_esr;
    244 	DDB_REGS->pid = tf->tf_pid;
    245 #endif
    246 
    247 #ifdef DDB
    248 	db_active++;
    249 	cnpollc(true);
    250 	db_trap(type, 0);
    251 	cnpollc(false);
    252 	db_active--;
    253 #endif
    254 #ifdef KGDB
    255 	if (!kgdb_trap(type, DDB_REGS)) {
    256 		rv = 0;
    257 		goto out;
    258 	}
    259 #endif
    260 
    261 	/* KGDB isn't smart about advancing PC if we
    262 	 * take a breakpoint trap after kgdb_active is set.
    263 	 * Therefore, we help out here.
    264 	 */
    265 	if (IS_BREAKPOINT_TRAP(type, 0)) {
    266 		int bkpt;
    267 		db_read_bytes(PC_REGS(DDB_REGS),BKPT_SIZE,(void *)&bkpt);
    268 		if (bkpt== BKPT_INST) {
    269 			PC_REGS(DDB_REGS) += BKPT_SIZE;
    270 		}
    271 	}
    272 
    273 	memcpy(tf->tf_fixreg, DDB_REGS->r, 32 * sizeof(u_int32_t));
    274 	tf->tf_srr0 = DDB_REGS->iar;
    275 	tf->tf_srr1 = DDB_REGS->msr;
    276 	tf->tf_lr = DDB_REGS->lr;
    277 	tf->tf_ctr = DDB_REGS->ctr;
    278 	tf->tf_cr = DDB_REGS->cr;
    279 	tf->tf_xer = DDB_REGS->xer;
    280 #ifdef PPC_OEA
    281 	tf->tf_mq = DDB_REGS->mq;
    282 #endif
    283 #if defined(PPC_IBM4XX) || defined(PPC_BOOKE)
    284 	tf->tf_dear = DDB_REGS->dear;
    285 	tf->tf_esr = DDB_REGS->esr;
    286 	tf->tf_pid = DDB_REGS->pid;
    287 #endif
    288 #ifdef KGDB
    289  out:
    290 #endif	/* KGDB */
    291 	splx(s);
    292 
    293 #ifdef MULTIPROCESSOR
    294 	if (atomic_cas_uint(&ddb_cpu, cpu_me, NOCPU) == cpu_me) {
    295 		cpu_resume_others();
    296 	} else {
    297 		cpu_resume(ddb_cpu);
    298 		if (first_in_ddb)
    299 			cpu_pause(tf);
    300 	}
    301 #endif	/* MULTIPROCESSOR */
    302 
    303 	return rv;
    304 }
    305 
    306 #ifdef DDB
    307 #if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    308 static void
    309 print_battranslation(struct bat *bat, unsigned int blidx)
    310 {
    311 	static const char batsizes[][6] = {
    312 		"128KB",
    313 		"256KB",
    314 		"512KB",
    315 		"1MB",
    316 		"2MB",
    317 		"4MB",
    318 		"8MB",
    319 		"16MB",
    320 		"32MB",
    321 		"64MB",
    322 		"128MB",
    323 		"256MB",
    324 		"512MB",
    325 		"1GB",
    326 		"2GB",
    327 		"4GB",
    328 	};
    329 	vsize_t len;
    330 
    331 	len = (0x20000L << blidx) - 1;
    332 	db_printf("\t%08lx %08lx %5s: 0x%08lx..0x%08lx -> 0x%08lx physical\n",
    333 	    bat->batu, bat->batl, batsizes[blidx], bat->batu & ~len,
    334 	    (bat->batu & ~len) + len, bat->batl & ~len);
    335 }
    336 
    337 static void
    338 print_batmodes(register_t super, register_t user, register_t pp)
    339 {
    340 	static const char *const accessmodes[] = {
    341 		"none",
    342 		"ro soft",
    343 		"read/write",
    344 		"read only"
    345 	};
    346 
    347 	db_printf("\tvalid: %c%c  access: %-10s  memory:",
    348 	    super ? 'S' : '-', user ? 'U' : '-', accessmodes[pp]);
    349 }
    350 
    351 static void
    352 print_wimg(register_t wimg)
    353 {
    354 	if (wimg & BAT_W)
    355 		db_printf(" wrthrough");
    356 	if (wimg & BAT_I)
    357 		db_printf(" nocache");
    358 	if (wimg & BAT_M)
    359 		db_printf(" coherent");
    360 	if (wimg & BAT_G)
    361 		db_printf(" guard");
    362 }
    363 
    364 static void
    365 print_bat(struct bat *bat)
    366 {
    367 	if ((bat->batu & BAT_V) == 0) {
    368 		db_printf("\tdisabled\n\n");
    369 		return;
    370 	}
    371 	print_battranslation(bat,
    372 	    30 - __builtin_clz((bat->batu & (BAT_XBL|BAT_BL))|2));
    373 	print_batmodes(bat->batu & BAT_Vs, bat->batu & BAT_Vu,
    374 	    bat->batl & BAT_PP);
    375 	print_wimg(bat->batl & BAT_WIMG);
    376 	db_printf("\n");
    377 }
    378 
    379 #ifdef PPC_OEA601
    380 static void
    381 print_bat601(struct bat *bat)
    382 {
    383 	if ((bat->batl & BAT601_V) == 0) {
    384 		db_printf("\tdisabled\n\n");
    385 		return;
    386 	}
    387 	print_battranslation(bat, 32 - __builtin_clz(bat->batl & BAT601_BSM));
    388 	print_batmodes(bat->batu & BAT601_Ks, bat->batu & BAT601_Ku,
    389 	    bat->batu & BAT601_PP);
    390 	print_wimg(bat->batu & (BAT601_W | BAT601_I | BAT601_M));
    391 	db_printf("\n");
    392 }
    393 #endif
    394 
    395 static void
    396 db_show_bat(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    397 {
    398 	struct bat ibat[8];
    399 	struct bat dbat[8];
    400 	unsigned int cpuvers;
    401 	u_int i;
    402 	u_int maxbat = (oeacpufeat & OEACPU_HIGHBAT) ? 8 : 4;
    403 
    404 	if (oeacpufeat & OEACPU_NOBAT)
    405 		return;
    406 
    407 	cpuvers = mfpvr() >> 16;
    408 
    409 	ibat[0].batu = mfspr(SPR_IBAT0U);
    410 	ibat[0].batl = mfspr(SPR_IBAT0L);
    411 	ibat[1].batu = mfspr(SPR_IBAT1U);
    412 	ibat[1].batl = mfspr(SPR_IBAT1L);
    413 	ibat[2].batu = mfspr(SPR_IBAT2U);
    414 	ibat[2].batl = mfspr(SPR_IBAT2L);
    415 	ibat[3].batu = mfspr(SPR_IBAT3U);
    416 	ibat[3].batl = mfspr(SPR_IBAT3L);
    417 	if (maxbat == 8) {
    418 		ibat[4].batu = mfspr(SPR_IBAT4U);
    419 		ibat[4].batl = mfspr(SPR_IBAT4L);
    420 		ibat[5].batu = mfspr(SPR_IBAT5U);
    421 		ibat[5].batl = mfspr(SPR_IBAT5L);
    422 		ibat[6].batu = mfspr(SPR_IBAT6U);
    423 		ibat[6].batl = mfspr(SPR_IBAT6L);
    424 		ibat[7].batu = mfspr(SPR_IBAT7U);
    425 		ibat[7].batl = mfspr(SPR_IBAT7L);
    426 	}
    427 
    428 	if (cpuvers != MPC601) {
    429 		/* The 601 has only four unified BATs */
    430 		dbat[0].batu = mfspr(SPR_DBAT0U);
    431 		dbat[0].batl = mfspr(SPR_DBAT0L);
    432 		dbat[1].batu = mfspr(SPR_DBAT1U);
    433 		dbat[1].batl = mfspr(SPR_DBAT1L);
    434 		dbat[2].batu = mfspr(SPR_DBAT2U);
    435 		dbat[2].batl = mfspr(SPR_DBAT2L);
    436 		dbat[3].batu = mfspr(SPR_DBAT3U);
    437 		dbat[3].batl = mfspr(SPR_DBAT3L);
    438 		if (maxbat == 8) {
    439 			dbat[4].batu = mfspr(SPR_DBAT4U);
    440 			dbat[4].batl = mfspr(SPR_DBAT4L);
    441 			dbat[5].batu = mfspr(SPR_DBAT5U);
    442 			dbat[5].batl = mfspr(SPR_DBAT5L);
    443 			dbat[6].batu = mfspr(SPR_DBAT6U);
    444 			dbat[6].batl = mfspr(SPR_DBAT6L);
    445 			dbat[7].batu = mfspr(SPR_DBAT7U);
    446 			dbat[7].batl = mfspr(SPR_DBAT7L);
    447 		}
    448 	}
    449 
    450 	for (i = 0; i < maxbat; i++) {
    451 #ifdef PPC_OEA601
    452 		if (cpuvers == MPC601) {
    453 			db_printf("bat[%u]:\n", i);
    454 			print_bat601(&ibat[i]);
    455 		} else
    456 #endif
    457 		{
    458 			db_printf("ibat[%u]:\n", i);
    459 			print_bat(&ibat[i]);
    460 			db_printf("dbat[%u]:\n", i);
    461 			print_bat(&dbat[i]);
    462 		}
    463 	}
    464 }
    465 
    466 static void
    467 db_show_mmu(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    468 {
    469 	paddr_t sdr1;
    470 
    471 	__asm volatile ("mfsdr1 %0" : "=r"(sdr1));
    472 	db_printf("sdr1\t\t0x%08lx\n", sdr1);
    473 
    474 #if defined(PPC_OEA64) || defined(PPC_OEA64_BRIDGE)
    475 	if (oeacpufeat & (OEACPU_64|OEACPU_64_BRIDGE)) {
    476 		__asm volatile ("mfasr %0" : "=r"(sdr1));
    477 		db_printf("asr\t\t0x%08lx\n", sdr1);
    478 	}
    479 #endif
    480 #if defined(PPC_OEA) || defined(PPC_OEA64_BRIDGE)
    481 	if ((oeacpufeat & OEACPU_64) == 0) {
    482 		vaddr_t saddr = 0;
    483 		for (u_int i = 0; i <= 0xf; i++) {
    484 			register_t sr;
    485 			if ((i & 3) == 0)
    486 				db_printf("sr%d-%d\t\t", i, i+3);
    487 			__asm volatile ("mfsrin %0,%1" : "=r"(sr) : "r"(saddr));
    488 			db_printf("0x%08lx   %c", sr, (i&3) == 3 ? '\n' : ' ');
    489 			saddr += 1 << ADDR_SR_SHFT;
    490 		}
    491 	}
    492 #endif
    493 }
    494 #endif /* PPC_OEA || PPC_OEA64 || PPC_OEA64_BRIDGE */
    495 #endif /* DDB */
    496 
    497 #if defined(PPC_IBM4XX) || defined(PPC_BOOKE)
    498 db_addr_t
    499 branch_taken(int inst, db_addr_t pc, db_regs_t *regs)
    500 {
    501 
    502 	if ((inst & M_B ) == I_B || (inst & M_B ) == I_BL) {
    503 		db_expr_t off;
    504 		off = ((db_expr_t)((inst & 0x03fffffc) << 6)) >> 6;
    505 		return (((inst & 0x2) ? 0 : pc) + off);
    506 	}
    507 
    508 	if ((inst & M_BC) == I_BC || (inst & M_BC) == I_BCL) {
    509 		db_expr_t off;
    510 		off = ((db_expr_t)((inst & 0x0000fffc) << 16)) >> 16;
    511 		return (((inst & 0x2) ? 0 : pc) + off);
    512 	}
    513 
    514 	if ((inst & M_RTS) == I_RTS || (inst & M_RTS) == I_BLRL)
    515 		return (regs->lr);
    516 
    517 	if ((inst & M_BCTR) == I_BCTR || (inst & M_BCTR) == I_BCTRL)
    518 		return (regs->ctr);
    519 
    520 	db_printf("branch_taken: can't figure out branch target for 0x%x!\n",
    521 	    inst);
    522 	return (0);
    523 }
    524 #endif /* PPC_IBM4XX || PPC_BOOKE */
    525 
    526 #ifdef DDB
    527 #ifdef PPC_IBM4XX
    528 static void
    529 db_ppc4xx_ctx(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    530 {
    531 	struct proc *p;
    532 
    533 	/* XXX LOCKING XXX */
    534 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    535 		if (p->p_stat) {
    536 			db_printf("process %p:", p);
    537 			db_printf("pid:%d pmap:%p ctx:%d %s\n",
    538 				p->p_pid, p->p_vmspace->vm_map.pmap,
    539 				p->p_vmspace->vm_map.pmap->pm_ctx,
    540 				p->p_comm);
    541 		}
    542 	}
    543 	return;
    544 }
    545 
    546 static void
    547 db_ppc4xx_pv(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    548 {
    549 	struct pv_entry {
    550 		struct pv_entry *pv_next;	/* Linked list of mappings */
    551 		vaddr_t pv_va;			/* virtual address of mapping */
    552 		struct pmap *pv_pm;
    553 	};
    554 	struct pv_entry *pa_to_pv(paddr_t);
    555 	struct pv_entry *pv;
    556 
    557 	if (!have_addr) {
    558 		db_printf("pv: <pa>\n");
    559 		return;
    560 	}
    561 	pv = pa_to_pv(addr);
    562 	db_printf("pv at %p\n", pv);
    563 	while (pv && pv->pv_pm) {
    564 		db_printf("next %p va %p pmap %p\n", pv->pv_next,
    565 			(void *)pv->pv_va, pv->pv_pm);
    566 		pv = pv->pv_next;
    567 	}
    568 }
    569 
    570 static void
    571 db_ppc4xx_reset(db_expr_t addr, bool have_addr, db_expr_t count,
    572     const char *modif)
    573 {
    574 	printf("Resetting...\n");
    575 	ppc4xx_reset();
    576 }
    577 
    578 static void
    579 db_ppc4xx_tf(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    580 {
    581 	struct trapframe *tf;
    582 
    583 
    584 	if (have_addr) {
    585 		tf = (struct trapframe *)addr;
    586 
    587 		db_printf("r0-r3:  \t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    588 			tf->tf_fixreg[0], tf->tf_fixreg[1],
    589 			tf->tf_fixreg[2], tf->tf_fixreg[3]);
    590 		db_printf("r4-r7:  \t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    591 			tf->tf_fixreg[4], tf->tf_fixreg[5],
    592 			tf->tf_fixreg[6], tf->tf_fixreg[7]);
    593 		db_printf("r8-r11: \t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    594 			tf->tf_fixreg[8], tf->tf_fixreg[9],
    595 			tf->tf_fixreg[10], tf->tf_fixreg[11]);
    596 		db_printf("r12-r15:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    597 			tf->tf_fixreg[12], tf->tf_fixreg[13],
    598 			tf->tf_fixreg[14], tf->tf_fixreg[15]);
    599 		db_printf("r16-r19:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    600 			tf->tf_fixreg[16], tf->tf_fixreg[17],
    601 			tf->tf_fixreg[18], tf->tf_fixreg[19]);
    602 		db_printf("r20-r23:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    603 			tf->tf_fixreg[20], tf->tf_fixreg[21],
    604 			tf->tf_fixreg[22], tf->tf_fixreg[23]);
    605 		db_printf("r24-r27:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    606 			tf->tf_fixreg[24], tf->tf_fixreg[25],
    607 			tf->tf_fixreg[26], tf->tf_fixreg[27]);
    608 		db_printf("r28-r31:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    609 			tf->tf_fixreg[28], tf->tf_fixreg[29],
    610 			tf->tf_fixreg[30], tf->tf_fixreg[31]);
    611 
    612 		db_printf("lr: %8.8lx cr: %8.8x xer: %8.8x ctr: %8.8lx\n",
    613 			tf->tf_lr, tf->tf_cr, tf->tf_xer, tf->tf_ctr);
    614 		db_printf("srr0(pc): %8.8lx srr1(msr): %8.8lx "
    615 			"dear: %8.8lx esr: %8.8x\n",
    616 			tf->tf_srr0, tf->tf_srr1, tf->tf_dear, tf->tf_esr);
    617 		db_printf("exc: %8.8x pid: %8.8x\n",
    618 			tf->tf_exc, tf->tf_pid);
    619 	}
    620 	return;
    621 }
    622 
    623 static const char *const tlbsizes[] = {
    624 	  "1kB",
    625 	  "4kB",
    626 	 "16kB",
    627 	 "64kB",
    628 	"256kB",
    629 	  "1MB",
    630 	  "4MB",
    631 	 "16MB"
    632 };
    633 
    634 static void
    635 db_ppc4xx_dumptlb(db_expr_t addr, bool have_addr, db_expr_t count,
    636     const char *modif)
    637 {
    638 	int i, zone, tlbsize;
    639 	u_int zpr, pid, opid, msr;
    640 	u_long tlblo, tlbhi, tlbmask;
    641 
    642 #ifdef PPC_IBM440
    643 	for (i = 0; i < NTLB; i++) {
    644 		u_long w0, w1, w2, tid;
    645 
    646 		ppc44x_tlb_read(i, &w0, &w1, &w2, &tid);
    647 
    648 		if (strchr(modif, 'v') && !(w0 & TLB44_V))
    649 			continue;
    650 
    651 		tlbsize = (w0 & TLB44_SIZE_MASK) >> TLB44_SIZE_SHFT;
    652 		tlbmask = tlbsize <= TLB44_SIZE_1G ?
    653 		    ~((1UL << (tlbsize * 2 + 10)) - 1) : ~0UL;
    654 
    655 		if (have_addr && ((w0 & tlbmask) != ((u_long)addr & tlbmask)))
    656 			continue;
    657 
    658 		ppc44x_tlb_print(i, w0, w1, w2, tid, db_printf);
    659 	}
    660 	return;
    661 #endif /* PPC_IBM440 */
    662 
    663 	zpr = mfspr(SPR_ZPR);
    664 	for (i = 0; i < NTLB; i++) {
    665 		__asm volatile("mfmsr %3;"
    666 			MFPID(%4)
    667 			"li %0,0;"
    668 			"mtmsr %0;"
    669 			"sync; isync;"
    670 			"tlbrelo %0,%5;"
    671 			"tlbrehi %1,%5;"
    672 			MFPID(%2)
    673 			MTPID(%4)
    674 			"mtmsr %3;"
    675 			"sync; isync"
    676 			: "=&r" (tlblo), "=&r" (tlbhi), "=r" (pid),
    677 			"=&r" (msr), "=&r" (opid) : "r" (i));
    678 
    679 		if (strchr(modif, 'v') && !(tlbhi & TLB_VALID))
    680 			continue;
    681 
    682 		tlbsize = (tlbhi & TLB_SIZE_MASK) >> TLB_SIZE_SHFT;
    683 		/* map tlbsize 0 .. 7 to masks for 1kB .. 16MB */
    684 		tlbmask = ~(1 << (tlbsize * 2 + 10)) + 1;
    685 
    686 		if (have_addr && ((tlbhi & tlbmask) != (addr & tlbmask)))
    687 			continue;
    688 
    689 		zone = (tlblo & TLB_ZSEL_MASK) >> TLB_ZSEL_SHFT;
    690 		db_printf("tlb%c%2d", tlbhi & TLB_VALID ? ' ' : '*', i);
    691 		db_printf("  PID %3d EPN 0x%08lx %-5s",
    692 		    pid,
    693 		    tlbhi & tlbmask,
    694 		    tlbsizes[tlbsize]);
    695 		db_printf("  RPN 0x%08lx  ZONE %2d%c  %s %s %c%c%c%c%c %s",
    696 		    tlblo & tlbmask,
    697 		    zone,
    698 		    "NTTA"[(zpr >> ((15 - zone) * 2)) & 3],
    699 		    tlblo & TLB_EX ? "EX" : "  ",
    700 		    tlblo & TLB_WR ? "WR" : "  ",
    701 		    tlblo & TLB_W ? 'W' : ' ',
    702 		    tlblo & TLB_I ? 'I' : ' ',
    703 		    tlblo & TLB_M ? 'M' : ' ',
    704 		    tlblo & TLB_G ? 'G' : ' ',
    705 		    tlbhi & TLB_ENDIAN ? 'E' : ' ',
    706 		    tlbhi & TLB_U0 ? "U0" : "  ");
    707 		db_printf("\n");
    708 	}
    709 }
    710 
    711 static void
    712 db_ppc4xx_dcr(db_expr_t address, bool have_addr, db_expr_t count,
    713     const char *modif)
    714 {
    715 	db_expr_t new_value;
    716 	db_expr_t addr;
    717 
    718 	if (address < 0 || address > 0x3ff)
    719 		db_error("Invalid DCR address (Valid range is 0x0 - 0x3ff)\n");
    720 
    721 	addr = address;
    722 
    723 	while (db_expression(&new_value)) {
    724 		db_printf("dcr 0x%lx\t\t%s = ", addr,
    725 		    db_num_to_str(db_ppc4xx_mfdcr(addr)));
    726 		db_ppc4xx_mtdcr(addr, new_value);
    727 		db_printf("%s\n", db_num_to_str(db_ppc4xx_mfdcr(addr)));
    728 		addr += 1;
    729 	}
    730 
    731 	if (addr == address) {
    732 		db_next = (db_addr_t)addr + 1;
    733 		db_prev = (db_addr_t)addr;
    734 		db_printf("dcr 0x%lx\t\t%s\n", addr,
    735 		    db_num_to_str(db_ppc4xx_mfdcr(addr)));
    736 	} else {
    737 		db_next = (db_addr_t)addr;
    738 		db_prev = (db_addr_t)addr - 1;
    739 	}
    740 
    741 	db_skip_to_eol();
    742 }
    743 
    744 /*
    745  * XXX Grossness Alert! XXX
    746  *
    747  * Please look away now if you don't like self-modifying code
    748  */
    749 static u_int32_t db_ppc4xx_dcrfunc[4];
    750 
    751 static db_expr_t
    752 db_ppc4xx_mfdcr(db_expr_t reg)
    753 {
    754 	db_expr_t (*func)(void);
    755 
    756 	reg = (((reg & 0x1f) << 5) | ((reg >> 5) & 0x1f)) << 11;
    757 	db_ppc4xx_dcrfunc[0] = 0x7c0004ac;		/* sync */
    758 	db_ppc4xx_dcrfunc[1] = 0x4c00012c;		/* isync */
    759 	db_ppc4xx_dcrfunc[2] = 0x7c600286 | reg;	/* mfdcr reg, r3 */
    760 	db_ppc4xx_dcrfunc[3] = 0x4e800020;		/* blr */
    761 
    762 	__syncicache((void *)db_ppc4xx_dcrfunc, sizeof(db_ppc4xx_dcrfunc));
    763 	func = (db_expr_t (*)(void))(void *)db_ppc4xx_dcrfunc;
    764 
    765 	return ((*func)());
    766 }
    767 
    768 static void
    769 db_ppc4xx_mtdcr(db_expr_t reg, db_expr_t val)
    770 {
    771 	db_expr_t (*func)(db_expr_t);
    772 
    773 	reg = (((reg & 0x1f) << 5) | ((reg >> 5) & 0x1f)) << 11;
    774 	db_ppc4xx_dcrfunc[0] = 0x7c0004ac;		/* sync */
    775 	db_ppc4xx_dcrfunc[1] = 0x4c00012c;		/* isync */
    776 	db_ppc4xx_dcrfunc[2] = 0x7c600386 | reg;	/* mtdcr r3, reg */
    777 	db_ppc4xx_dcrfunc[3] = 0x4e800020;		/* blr */
    778 
    779 	__syncicache((void *)db_ppc4xx_dcrfunc, sizeof(db_ppc4xx_dcrfunc));
    780 	func = (db_expr_t (*)(db_expr_t))(void *)db_ppc4xx_dcrfunc;
    781 
    782 	(*func)(val);
    783 }
    784 
    785 #ifdef USERACC
    786 static void
    787 db_ppc4xx_useracc(db_expr_t addr, bool have_addr, db_expr_t count,
    788     const char *modif)
    789 {
    790 	static paddr_t oldaddr = -1;
    791 	int instr = 0;
    792 	int data;
    793 	extern vaddr_t opc_disasm(vaddr_t loc, int);
    794 
    795 
    796 	if (!have_addr) {
    797 		addr = oldaddr;
    798 	}
    799 	if (addr == -1) {
    800 		db_printf("no address\n");
    801 		return;
    802 	}
    803 	addr &= ~0x3; /* align */
    804 	{
    805 		const char *cp = modif;
    806 		char c;
    807 		while ((c = *cp++) != 0)
    808 			if (c == 'i')
    809 				instr = 1;
    810 	}
    811 	while (count--) {
    812 		if (db_print_position() == 0) {
    813 			/* Always print the address. */
    814 			db_printf("%8.4lx:\t", addr);
    815 		}
    816 		oldaddr=addr;
    817 		copyin((void *)addr, &data, sizeof(data));
    818 		if (instr) {
    819 			opc_disasm(addr, data);
    820 		} else {
    821 			db_printf("%4.4x\n", data);
    822 		}
    823 		addr += 4;
    824 		db_end_line();
    825 	}
    826 
    827 }
    828 #endif
    829 
    830 #endif /* PPC_IBM4XX */
    831 
    832 #ifdef PPC_BOOKE
    833 static void
    834 db_ppcbooke_reset(db_expr_t addr, bool have_addr, db_expr_t count,
    835     const char *modif)
    836 {
    837 	printf("Resetting...\n");
    838 	(*cpu_md_ops.md_cpu_reset)();
    839 }
    840 
    841 static void
    842 db_ppcbooke_splhist(db_expr_t addr, bool have_addr, db_expr_t count,
    843     const char *modif)
    844 {
    845 	dump_splhist(curcpu(), db_printf);
    846 }
    847 
    848 static void
    849 db_ppcbooke_tf(db_expr_t addr, bool have_addr, db_expr_t count,
    850     const char *modif)
    851 {
    852 	if (!have_addr)
    853 		return;
    854 
    855 	dump_trapframe((const struct trapframe *)addr, db_printf);
    856 }
    857 
    858 static void
    859 db_ppcbooke_dumptlb(db_expr_t addr, bool have_addr, db_expr_t count,
    860     const char *modif)
    861 {
    862 	tlb_dump(db_printf);
    863 }
    864 #endif /* PPC_BOOKE */
    865 
    866 #ifdef MULTIPROCESSOR
    867 bool
    868 ddb_running_on_this_cpu_p(void)
    869 {
    870 
    871 	return ddb_cpu == cpu_number();
    872 }
    873 
    874 bool
    875 ddb_running_on_any_cpu_p(void)
    876 {
    877 
    878 	return ddb_cpu != NOCPU;
    879 }
    880 
    881 void
    882 db_resume_others(void)
    883 {
    884 	u_int cpu_me = cpu_number();
    885 
    886 	if (atomic_cas_uint(&ddb_cpu, cpu_me, NOCPU) == cpu_me)
    887 		cpu_resume_others();
    888 }
    889 
    890 static void
    891 db_mach_cpu(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    892 {
    893 	CPU_INFO_ITERATOR cii;
    894 	struct cpu_info *ci;
    895 	bool found = false;
    896 
    897 	if (!have_addr) {
    898 		cpu_debug_dump();
    899 		return;
    900 	}
    901 
    902 	if (addr < 0) {
    903 		db_printf("%ld: CPU out of range\n", addr);
    904 		return;
    905 	}
    906 	for (CPU_INFO_FOREACH(cii, ci)) {
    907 		if (cpu_index(ci) == addr) {
    908 			found = true;
    909 			break;
    910 		}
    911 	}
    912 	if (!found) {
    913 		db_printf("CPU %ld not configured\n", addr);
    914 		return;
    915 	}
    916 	if (ci != curcpu()) {
    917 		if (!cpu_is_paused(cpu_index(ci))) {
    918 			db_printf("CPU %ld not paused\n", (long)addr);
    919 			return;
    920 		}
    921 		(void)atomic_cas_uint(&ddb_cpu, cpu_number(), cpu_index(ci));
    922 		db_continue_cmd(0, false, 0, "");
    923 	}
    924 }
    925 #endif	/* MULTIPROCESSOR */
    926 #endif /* DDB */
    927