Home | History | Annotate | Line # | Download | only in powerpc
db_interface.c revision 1.46
      1  1.46      matt /*	$NetBSD: db_interface.c,v 1.46 2011/06/18 06:41:44 matt Exp $ */
      2   1.1  sakamoto /*	$OpenBSD: db_interface.c,v 1.2 1996/12/28 06:21:50 rahnds Exp $	*/
      3  1.29     lukem 
      4  1.29     lukem #include <sys/cdefs.h>
      5  1.46      matt __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.46 2011/06/18 06:41:44 matt Exp $");
      6   1.3  jonathan 
      7  1.10    simonb #define USERACC
      8  1.10    simonb 
      9   1.3  jonathan #include "opt_ddb.h"
     10  1.17       dbj #include "opt_kgdb.h"
     11  1.10    simonb #include "opt_ppcarch.h"
     12   1.1  sakamoto 
     13   1.1  sakamoto #include <sys/param.h>
     14   1.1  sakamoto #include <sys/proc.h>
     15   1.4    tsubai #include <sys/systm.h>
     16  1.46      matt #include <sys/cpu.h>
     17   1.6    kleink 
     18  1.11    simonb #include <dev/cons.h>
     19  1.11    simonb 
     20   1.7    tsubai #include <machine/db_machdep.h>
     21   1.7    tsubai #include <machine/frame.h>
     22  1.43       phx #include <powerpc/spr.h>
     23  1.43       phx #include <powerpc/pte.h>
     24  1.43       phx 
     25  1.44      matt #if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
     26  1.44      matt #include <powerpc/oea/spr.h>
     27  1.46      matt #include <powerpc/oea/bat.h>
     28  1.44      matt #endif
     29  1.44      matt 
     30  1.10    simonb #ifdef PPC_IBM4XX
     31  1.46      matt #include <powerpc/ibm4xx/cpu.h>
     32  1.44      matt #include <powerpc/ibm4xx/spr.h>
     33  1.10    simonb #include <machine/tlb.h>
     34  1.10    simonb #include <uvm/uvm_extern.h>
     35  1.10    simonb #endif
     36   1.7    tsubai 
     37  1.44      matt #ifdef PPC_BOOKE
     38  1.45      matt #include <powerpc/booke/cpuvar.h>
     39  1.44      matt #include <powerpc/booke/spr.h>
     40  1.44      matt #endif
     41  1.44      matt 
     42  1.17       dbj #ifdef DDB
     43   1.6    kleink #include <ddb/db_sym.h>
     44   1.6    kleink #include <ddb/db_command.h>
     45   1.6    kleink #include <ddb/db_extern.h>
     46   1.6    kleink #include <ddb/db_access.h>
     47  1.22       scw #include <ddb/db_lex.h>
     48   1.6    kleink #include <ddb/db_output.h>
     49   1.6    kleink #include <ddb/ddbvar.h>
     50  1.17       dbj #endif
     51  1.17       dbj 
     52  1.17       dbj #ifdef KGDB
     53  1.17       dbj #include <sys/kgdb.h>
     54  1.39   garbled #define db_printf printf
     55  1.17       dbj #endif
     56  1.17       dbj 
     57  1.17       dbj #include <dev/ofw/openfirm.h>
     58   1.1  sakamoto 
     59  1.11    simonb int	db_active = 0;
     60  1.11    simonb 
     61  1.19      matt db_regs_t ddb_regs;
     62   1.8    tsubai 
     63  1.10    simonb void ddb_trap(void);				/* Call into trap_subr.S */
     64  1.10    simonb int ddb_trap_glue(struct trapframe *);		/* Called from trap_subr.S */
     65  1.43       phx #if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
     66  1.43       phx static void db_show_bat(db_expr_t, bool, db_expr_t, const char *);
     67  1.43       phx static void db_show_mmu(db_expr_t, bool, db_expr_t, const char *);
     68  1.43       phx #endif /* PPC_OEA || PPC_OEA64 || PPC_OEA64_BRIDGE */
     69  1.10    simonb #ifdef PPC_IBM4XX
     70  1.37      matt static void db_ppc4xx_ctx(db_expr_t, bool, db_expr_t, const char *);
     71  1.37      matt static void db_ppc4xx_pv(db_expr_t, bool, db_expr_t, const char *);
     72  1.37      matt static void db_ppc4xx_reset(db_expr_t, bool, db_expr_t, const char *);
     73  1.37      matt static void db_ppc4xx_tf(db_expr_t, bool, db_expr_t, const char *);
     74  1.37      matt static void db_ppc4xx_dumptlb(db_expr_t, bool, db_expr_t, const char *);
     75  1.37      matt static void db_ppc4xx_dcr(db_expr_t, bool, db_expr_t, const char *);
     76  1.22       scw static db_expr_t db_ppc4xx_mfdcr(db_expr_t);
     77  1.22       scw static void db_ppc4xx_mtdcr(db_expr_t, db_expr_t);
     78  1.10    simonb #ifdef USERACC
     79  1.37      matt static void db_ppc4xx_useracc(db_expr_t, bool, db_expr_t, const char *);
     80  1.10    simonb #endif
     81  1.10    simonb #endif /* PPC_IBM4XX */
     82   1.9    briggs 
     83  1.45      matt #ifdef PPC_BOOKE
     84  1.45      matt static void db_ppcbooke_reset(db_expr_t, bool, db_expr_t, const char *);
     85  1.45      matt static void db_ppcbooke_tf(db_expr_t, bool, db_expr_t, const char *);
     86  1.45      matt static void db_ppcbooke_dumptlb(db_expr_t, bool, db_expr_t, const char *);
     87  1.45      matt #endif
     88  1.45      matt 
     89  1.17       dbj #ifdef DDB
     90  1.43       phx const struct db_command db_machine_command_table[] = {
     91  1.43       phx #if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
     92  1.43       phx 	{ DDB_ADD_CMD("bat",	db_show_bat,		0,
     93  1.43       phx 	  "Print BAT register translations", NULL,NULL) },
     94  1.43       phx 	{ DDB_ADD_CMD("mmu",	db_show_mmu,		0,
     95  1.43       phx 	  "Print MMU registers", NULL,NULL) },
     96  1.43       phx #endif /* PPC_OEA || PPC_OEA64 || PPC_OEA64_BRIDGE */
     97  1.43       phx #ifdef PPC_IBM4XX
     98  1.43       phx 	{ DDB_ADD_CMD("ctx",	db_ppc4xx_ctx,		0,
     99  1.43       phx 	  "Print process MMU context information", NULL,NULL) },
    100  1.43       phx 	{ DDB_ADD_CMD("pv",	db_ppc4xx_pv,		0,
    101  1.43       phx 	  "Print PA->VA mapping information",
    102  1.43       phx 	  "address",
    103  1.43       phx 	  "   address:\tphysical address to look up") },
    104  1.43       phx 	{ DDB_ADD_CMD("reset",	db_ppc4xx_reset,	0,
    105  1.43       phx 	  "Reset the system ", NULL,NULL) },
    106  1.43       phx 	{ DDB_ADD_CMD("tf",	db_ppc4xx_tf,		0,
    107  1.43       phx 	  "Display the contents of the trapframe",
    108  1.43       phx 	  "address",
    109  1.43       phx 	  "   address:\tthe struct trapframe to print") },
    110  1.43       phx 	{ DDB_ADD_CMD("tlb",	db_ppc4xx_dumptlb,	0,
    111  1.43       phx 	  "Display instruction translation storage buffer information.",
    112  1.43       phx 	  NULL,NULL) },
    113  1.43       phx 	{ DDB_ADD_CMD("dcr",	db_ppc4xx_dcr,		CS_MORE|CS_SET_DOT,
    114  1.43       phx 	  "Set the DCR register",
    115  1.43       phx 	  "dcr",
    116  1.43       phx 	  "   dcr:\tNew DCR value (between 0x0 and 0x3ff)") },
    117  1.43       phx #ifdef USERACC
    118  1.43       phx 	{ DDB_ADD_CMD("user",	db_ppc4xx_useracc,	0,
    119  1.43       phx 	   "Display user memory.", "[address][,count]",
    120  1.43       phx 	   "   address:\tuserspace address to start\n"
    121  1.43       phx 	   "   count:\tnumber of bytes to display") },
    122  1.43       phx #endif
    123  1.43       phx #endif /* PPC_IBM4XX */
    124  1.45      matt #ifdef PPC_BOOKE
    125  1.45      matt 	{ DDB_ADD_CMD("reset",	db_ppcbooke_reset,	0,
    126  1.45      matt 	  "Reset the system ", NULL,NULL) },
    127  1.45      matt 	{ DDB_ADD_CMD("tf",	db_ppcbooke_tf,		0,
    128  1.45      matt 	  "Display the contents of the trapframe",
    129  1.45      matt 	  "address",
    130  1.45      matt 	  "   address:\tthe struct trapframe to print") },
    131  1.45      matt 	{ DDB_ADD_CMD("tlb",	db_ppcbooke_dumptlb,	0,
    132  1.45      matt 	  "Display instruction translation storage buffer information.",
    133  1.45      matt 	  NULL,NULL) },
    134  1.45      matt #endif /* PPC_BOOKE */
    135  1.43       phx 	{ DDB_ADD_CMD(NULL,	NULL,			0,
    136  1.43       phx 	  NULL,NULL,NULL) }
    137  1.43       phx };
    138  1.43       phx 
    139   1.1  sakamoto void
    140  1.31      matt cpu_Debugger(void)
    141   1.1  sakamoto {
    142  1.45      matt #ifdef PPC_BOOKE
    143  1.45      matt 	const register_t msr = mfmsr();
    144  1.45      matt 	__asm volatile("wrteei 0\n\ttweq\t1,1");
    145  1.45      matt 	mtmsr(msr);
    146  1.45      matt 	__asm volatile("isync");
    147  1.45      matt #else
    148   1.1  sakamoto 	ddb_trap();
    149  1.45      matt #endif
    150   1.1  sakamoto }
    151  1.45      matt #endif /* DDB */
    152   1.1  sakamoto 
    153   1.1  sakamoto int
    154  1.45      matt ddb_trap_glue(struct trapframe *tf)
    155   1.1  sakamoto {
    156  1.45      matt #if defined(PPC_IBM4XX) || defined(PPC_BOOKE)
    157  1.45      matt 	if ((tf->tf_srr1 & PSL_PR) == 0)
    158  1.45      matt 		return kdb_trap(tf->tf_exc, tf);
    159  1.28      matt #else /* PPC_OEA */
    160  1.45      matt 	if ((tf->tf_srr1 & PSL_PR) == 0 &&
    161  1.45      matt 	    (tf->tf_exc == EXC_TRC ||
    162  1.45      matt 	     tf->tf_exc == EXC_RUNMODETRC ||
    163  1.45      matt 	     (tf->tf_exc == EXC_PGM && (tf->tf_srr1 & 0x20000)) ||
    164  1.45      matt 	     tf->tf_exc == EXC_BPT ||
    165  1.45      matt 	     tf->tf_exc == EXC_DSI)) {
    166  1.45      matt 		int type = tf->tf_exc;
    167  1.45      matt 		if (type == EXC_PGM && (tf->tf_srr1 & 0x20000)) {
    168  1.17       dbj 			type = T_BREAKPOINT;
    169  1.17       dbj 		}
    170  1.45      matt 		return kdb_trap(type, tf);
    171   1.1  sakamoto 	}
    172  1.25   thorpej #endif
    173   1.1  sakamoto 	return 0;
    174   1.8    tsubai }
    175   1.8    tsubai 
    176   1.8    tsubai int
    177  1.31      matt kdb_trap(int type, void *v)
    178   1.8    tsubai {
    179  1.45      matt 	struct trapframe *tf = v;
    180   1.8    tsubai 
    181  1.17       dbj #ifdef DDB
    182  1.30      matt 	if (db_recover != 0 && (type != -1 && type != T_BREAKPOINT)) {
    183  1.30      matt 		db_error("Faulted in DDB; continuing...\n");
    184  1.30      matt 		/* NOTREACHED */
    185   1.8    tsubai 	}
    186  1.17       dbj #endif
    187   1.8    tsubai 
    188   1.8    tsubai 	/* XXX Should switch to kdb's own stack here. */
    189   1.8    tsubai 
    190  1.45      matt 	memcpy(DDB_REGS->r, tf->tf_fixreg, 32 * sizeof(u_int32_t));
    191  1.45      matt 	DDB_REGS->iar = tf->tf_srr0;
    192  1.45      matt 	DDB_REGS->msr = tf->tf_srr1;
    193  1.45      matt 	DDB_REGS->lr = tf->tf_lr;
    194  1.45      matt 	DDB_REGS->ctr = tf->tf_ctr;
    195  1.45      matt 	DDB_REGS->cr = tf->tf_cr;
    196  1.45      matt 	DDB_REGS->xer = tf->tf_xer;
    197  1.28      matt #ifdef PPC_OEA
    198  1.45      matt 	DDB_REGS->mq = tf->tf_mq;
    199  1.45      matt #elif defined(PPC_IBM4XX) || defined(PPC_BOOKE)
    200  1.45      matt 	DDB_REGS->dear = tf->tf_dear;
    201  1.45      matt 	DDB_REGS->esr = tf->tf_esr;
    202  1.45      matt 	DDB_REGS->pid = tf->tf_pid;
    203  1.10    simonb #endif
    204   1.8    tsubai 
    205  1.17       dbj #ifdef DDB
    206  1.11    simonb 	db_active++;
    207  1.11    simonb 	cnpollc(1);
    208  1.15       dbj 	db_trap(type, 0);
    209  1.11    simonb 	cnpollc(0);
    210  1.11    simonb 	db_active--;
    211  1.17       dbj #elif defined(KGDB)
    212  1.17       dbj 	if (!kgdb_trap(type, DDB_REGS))
    213  1.17       dbj 		return 0;
    214  1.17       dbj #endif
    215  1.17       dbj 
    216  1.17       dbj 	/* KGDB isn't smart about advancing PC if we
    217  1.17       dbj 	 * take a breakpoint trap after kgdb_active is set.
    218  1.17       dbj 	 * Therefore, we help out here.
    219  1.17       dbj 	 */
    220  1.17       dbj 	if (IS_BREAKPOINT_TRAP(type, 0)) {
    221  1.17       dbj 		int bkpt;
    222  1.17       dbj 		db_read_bytes(PC_REGS(DDB_REGS),BKPT_SIZE,(void *)&bkpt);
    223  1.17       dbj 		if (bkpt== BKPT_INST) {
    224  1.17       dbj 			PC_REGS(DDB_REGS) += BKPT_SIZE;
    225  1.17       dbj 		}
    226  1.17       dbj 	}
    227   1.8    tsubai 
    228  1.45      matt 	memcpy(tf->tf_fixreg, DDB_REGS->r, 32 * sizeof(u_int32_t));
    229  1.45      matt 	tf->tf_srr0 = DDB_REGS->iar;
    230  1.45      matt 	tf->tf_srr1 = DDB_REGS->msr;
    231  1.45      matt 	tf->tf_lr = DDB_REGS->lr;
    232  1.45      matt 	tf->tf_ctr = DDB_REGS->ctr;
    233  1.45      matt 	tf->tf_cr = DDB_REGS->cr;
    234  1.45      matt 	tf->tf_xer = DDB_REGS->xer;
    235  1.28      matt #ifdef PPC_OEA
    236  1.45      matt 	tf->tf_mq = DDB_REGS->mq;
    237  1.27      matt #endif
    238  1.45      matt #if defined(PPC_IBM4XX) || defined(PPC_BOOKE)
    239  1.45      matt 	tf->tf_dear = DDB_REGS->dear;
    240  1.45      matt 	tf->tf_esr = DDB_REGS->esr;
    241  1.45      matt 	tf->tf_pid = DDB_REGS->pid;
    242  1.10    simonb #endif
    243   1.8    tsubai 
    244   1.8    tsubai 	return 1;
    245   1.1  sakamoto }
    246  1.10    simonb 
    247  1.43       phx #if defined (PPC_OEA) || defined(PPC_OEA64) || defined (PPC_OEA64_BRIDGE)
    248  1.43       phx static void
    249  1.43       phx print_battranslation(struct bat *bat, unsigned int blidx)
    250  1.43       phx {
    251  1.43       phx 	static const char *const batsizes[] = {
    252  1.43       phx 		"128kB",
    253  1.43       phx 		"256kB",
    254  1.43       phx 		"512kB",
    255  1.43       phx 		"1MB",
    256  1.43       phx 		"2MB",
    257  1.43       phx 		"4MB",
    258  1.43       phx 		"8MB",
    259  1.43       phx 		"16MB",
    260  1.43       phx 		"32MB",
    261  1.43       phx 		"64MB",
    262  1.43       phx 		"128MB",
    263  1.43       phx 		"256MB"
    264  1.43       phx 	};
    265  1.43       phx 	vsize_t len;
    266  1.43       phx 
    267  1.43       phx 	len = (0x20000L << blidx) - 1;
    268  1.43       phx 	db_printf("\t%08lx %08lx %5s: 0x%08lx..0x%08lx -> 0x%08lx physical\n",
    269  1.43       phx 	    bat->batu, bat->batl, batsizes[blidx], bat->batu & ~len,
    270  1.43       phx 	    (bat->batu & ~len) + len, bat->batl & ~len);
    271  1.43       phx }
    272  1.43       phx 
    273  1.43       phx static void
    274  1.43       phx print_batmodes(register_t super, register_t user, register_t pp)
    275  1.43       phx {
    276  1.43       phx 	static const char *const accessmodes[] = {
    277  1.43       phx 		"none",
    278  1.43       phx 		"ro soft",
    279  1.43       phx 		"read/write",
    280  1.43       phx 		"read only"
    281  1.43       phx 	};
    282  1.43       phx 
    283  1.43       phx 	db_printf("\tvalid: %c%c  access: %-10s  memory:",
    284  1.43       phx 	    super ? 'S' : '-', user ? 'U' : '-', accessmodes[pp]);
    285  1.43       phx }
    286  1.43       phx 
    287  1.43       phx static void
    288  1.43       phx print_wimg(register_t wimg)
    289  1.43       phx {
    290  1.43       phx 	if (wimg & BAT_W)
    291  1.43       phx 		db_printf(" wrthrough");
    292  1.43       phx 	if (wimg & BAT_I)
    293  1.43       phx 		db_printf(" nocache");
    294  1.43       phx 	if (wimg & BAT_M)
    295  1.43       phx 		db_printf(" coherent");
    296  1.43       phx 	if (wimg & BAT_G)
    297  1.43       phx 		db_printf(" guard");
    298  1.43       phx }
    299  1.43       phx 
    300  1.43       phx static void
    301  1.43       phx print_bat(struct bat *bat)
    302  1.43       phx {
    303  1.43       phx 	if ((bat->batu & BAT_V) == 0) {
    304  1.43       phx 		db_printf("\tdisabled\n\n");
    305  1.43       phx 		return;
    306  1.43       phx 	}
    307  1.43       phx 	print_battranslation(bat, 30 - __builtin_clz((bat->batu & BAT_BL)|2));
    308  1.43       phx 	print_batmodes(bat->batu & BAT_Vs, bat->batu & BAT_Vu,
    309  1.43       phx 	    bat->batl & BAT_PP);
    310  1.43       phx 	print_wimg(bat->batl & BAT_WIMG);
    311  1.43       phx 	db_printf("\n");
    312  1.43       phx }
    313  1.43       phx 
    314  1.43       phx #ifdef PPC_OEA601
    315  1.43       phx static void
    316  1.43       phx print_bat601(struct bat *bat)
    317  1.43       phx {
    318  1.43       phx 	if ((bat->batl & BAT601_V) == 0) {
    319  1.43       phx 		db_printf("\tdisabled\n\n");
    320  1.43       phx 		return;
    321  1.43       phx 	}
    322  1.43       phx 	print_battranslation(bat, 32 - __builtin_clz(bat->batl & BAT601_BSM));
    323  1.43       phx 	print_batmodes(bat->batu & BAT601_Ks, bat->batu & BAT601_Ku,
    324  1.43       phx 	    bat->batu & BAT601_PP);
    325  1.43       phx 	print_wimg(bat->batu & (BAT601_W | BAT601_I | BAT601_M));
    326  1.43       phx 	db_printf("\n");
    327  1.43       phx }
    328  1.43       phx #endif
    329  1.43       phx 
    330  1.43       phx static void
    331  1.43       phx db_show_bat(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    332  1.43       phx {
    333  1.43       phx 	struct bat ibat[4];
    334  1.43       phx 	struct bat dbat[4];
    335  1.43       phx 	unsigned int cpuvers;
    336  1.43       phx 	int i;
    337  1.43       phx 
    338  1.43       phx 	cpuvers = mfpvr() >> 16;
    339  1.43       phx 
    340  1.43       phx 	__asm volatile ("mfibatu %0,0" : "=r"(ibat[0].batu));
    341  1.43       phx 	__asm volatile ("mfibatl %0,0" : "=r"(ibat[0].batl));
    342  1.43       phx 	__asm volatile ("mfibatu %0,1" : "=r"(ibat[1].batu));
    343  1.43       phx 	__asm volatile ("mfibatl %0,1" : "=r"(ibat[1].batl));
    344  1.43       phx 	__asm volatile ("mfibatu %0,2" : "=r"(ibat[2].batu));
    345  1.43       phx 	__asm volatile ("mfibatl %0,2" : "=r"(ibat[2].batl));
    346  1.43       phx 	__asm volatile ("mfibatu %0,3" : "=r"(ibat[3].batu));
    347  1.43       phx 	__asm volatile ("mfibatl %0,3" : "=r"(ibat[3].batl));
    348  1.43       phx 
    349  1.43       phx 	if (cpuvers != MPC601) {
    350  1.43       phx 		/* The 601 has only four unified BATs */
    351  1.43       phx 		__asm volatile ("mfdbatu %0,0" : "=r"(dbat[0].batu));
    352  1.43       phx 		__asm volatile ("mfdbatl %0,0" : "=r"(dbat[0].batl));
    353  1.43       phx 		__asm volatile ("mfdbatu %0,1" : "=r"(dbat[1].batu));
    354  1.43       phx 		__asm volatile ("mfdbatl %0,1" : "=r"(dbat[1].batl));
    355  1.43       phx 		__asm volatile ("mfdbatu %0,2" : "=r"(dbat[2].batu));
    356  1.43       phx 		__asm volatile ("mfdbatl %0,2" : "=r"(dbat[2].batl));
    357  1.43       phx 		__asm volatile ("mfdbatu %0,3" : "=r"(dbat[3].batu));
    358  1.43       phx 		__asm volatile ("mfdbatl %0,3" : "=r"(dbat[3].batl));
    359  1.43       phx 	}
    360  1.43       phx 
    361  1.43       phx 	for (i = 0; i < 4; i++) {
    362  1.43       phx #ifdef PPC_OEA601
    363  1.43       phx 		if (cpuvers == MPC601) {
    364  1.43       phx 			db_printf("bat%d:", i);
    365  1.43       phx 			print_bat601(&ibat[i]);
    366  1.43       phx 		} else
    367  1.43       phx #endif
    368  1.43       phx 		{
    369  1.43       phx 			db_printf("ibat%d:", i);
    370  1.43       phx 			print_bat(&ibat[i]);
    371  1.43       phx 		}
    372  1.43       phx 	}
    373  1.43       phx 	if (cpuvers != MPC601) {
    374  1.43       phx 		for (i = 0; i < 4; i++) {
    375  1.43       phx 			db_printf("dbat%d:", i);
    376  1.43       phx 			print_bat(&dbat[i]);
    377  1.43       phx 		}
    378  1.43       phx 	}
    379  1.43       phx }
    380  1.43       phx 
    381  1.43       phx static void
    382  1.43       phx db_show_mmu(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    383  1.43       phx {
    384  1.43       phx 	paddr_t sdr1;
    385  1.43       phx #if !defined(PPC_OEA64) && !defined(PPC_OEA64_BRIDGE)
    386  1.43       phx 	register_t sr;
    387  1.43       phx 	vaddr_t saddr;
    388  1.43       phx 	int i;
    389  1.43       phx #endif
    390  1.43       phx 
    391  1.43       phx 	__asm volatile ("mfsdr1 %0" : "=r"(sdr1));
    392  1.43       phx 	db_printf("sdr1\t\t0x%08lx\n", sdr1);
    393  1.43       phx 
    394  1.43       phx #if defined(PPC_OEA64) || defined(PPC_OEA64_BRIDGE)
    395  1.43       phx 	__asm volatile ("mfasr %0" : "=r"(sdr1));
    396  1.43       phx 	db_printf("asr\t\t0x%08lx\n", sdr1);
    397  1.43       phx #else
    398  1.43       phx 	saddr = 0;
    399  1.43       phx 	for (i = 0; i<= 0xf; i++) {
    400  1.43       phx 		if ((i & 3) == 0)
    401  1.43       phx 			db_printf("sr%d-%d\t\t", i, i+3);
    402  1.43       phx 		__asm volatile ("mfsrin %0,%1" : "=r"(sr) : "r"(saddr));
    403  1.43       phx 		db_printf("0x%08lx   %c", sr, (i&3) == 3 ? '\n' : ' ');
    404  1.43       phx 		saddr += 1 << ADDR_SR_SHFT;
    405  1.43       phx 	}
    406  1.43       phx #endif
    407  1.43       phx }
    408  1.43       phx #endif /* PPC_OEA || PPC_OEA64 || PPC_OEA64_BRIDGE */
    409  1.43       phx 
    410  1.45      matt #if defined(PPC_IBM4XX) || defined(PPC_BOOKE)
    411  1.24       scw db_addr_t
    412  1.24       scw branch_taken(int inst, db_addr_t pc, db_regs_t *regs)
    413  1.24       scw {
    414  1.24       scw 
    415  1.24       scw 	if ((inst & M_B ) == I_B || (inst & M_B ) == I_BL) {
    416  1.24       scw 		db_expr_t off;
    417  1.24       scw 		off = ((db_expr_t)((inst & 0x03fffffc) << 6)) >> 6;
    418  1.24       scw 		return (((inst & 0x2) ? 0 : pc) + off);
    419  1.24       scw 	}
    420  1.24       scw 
    421  1.24       scw 	if ((inst & M_BC) == I_BC || (inst & M_BC) == I_BCL) {
    422  1.24       scw 		db_expr_t off;
    423  1.24       scw 		off = ((db_expr_t)((inst & 0x0000fffc) << 16)) >> 16;
    424  1.24       scw 		return (((inst & 0x2) ? 0 : pc) + off);
    425  1.24       scw 	}
    426  1.24       scw 
    427  1.24       scw 	if ((inst & M_RTS) == I_RTS || (inst & M_RTS) == I_BLRL)
    428  1.24       scw 		return (regs->lr);
    429  1.24       scw 
    430  1.24       scw 	if ((inst & M_BCTR) == I_BCTR || (inst & M_BCTR) == I_BCTRL)
    431  1.24       scw 		return (regs->ctr);
    432  1.24       scw 
    433  1.24       scw 	db_printf("branch_taken: can't figure out branch target for 0x%x!\n",
    434  1.24       scw 	    inst);
    435  1.24       scw 	return (0);
    436  1.24       scw }
    437  1.45      matt #endif /* PPC_IBM4XX || PPC_BOOKE */
    438  1.24       scw 
    439  1.43       phx 
    440  1.45      matt #ifdef PPC_IBM4XX
    441  1.42       phx #ifdef DDB
    442  1.10    simonb static void
    443  1.37      matt db_ppc4xx_ctx(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    444  1.10    simonb {
    445  1.10    simonb 	struct proc *p;
    446  1.10    simonb 
    447  1.10    simonb 	/* XXX LOCKING XXX */
    448  1.10    simonb 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    449  1.10    simonb 		if (p->p_stat) {
    450  1.10    simonb 			db_printf("process %p:", p);
    451  1.10    simonb 			db_printf("pid:%d pmap:%p ctx:%d %s\n",
    452  1.10    simonb 				p->p_pid, p->p_vmspace->vm_map.pmap,
    453  1.10    simonb 				p->p_vmspace->vm_map.pmap->pm_ctx,
    454  1.10    simonb 				p->p_comm);
    455  1.10    simonb 		}
    456  1.10    simonb 	}
    457  1.10    simonb 	return;
    458  1.10    simonb }
    459  1.10    simonb 
    460  1.10    simonb static void
    461  1.37      matt db_ppc4xx_pv(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    462  1.10    simonb {
    463  1.10    simonb 	struct pv_entry {
    464  1.10    simonb 		struct pv_entry *pv_next;	/* Linked list of mappings */
    465  1.10    simonb 		vaddr_t pv_va;			/* virtual address of mapping */
    466  1.10    simonb 		struct pmap *pv_pm;
    467  1.10    simonb 	};
    468  1.10    simonb 	struct pv_entry *pa_to_pv(paddr_t);
    469  1.10    simonb 	struct pv_entry *pv;
    470  1.10    simonb 
    471  1.10    simonb 	if (!have_addr) {
    472  1.10    simonb 		db_printf("pv: <pa>\n");
    473  1.10    simonb 		return;
    474  1.10    simonb 	}
    475  1.10    simonb 	pv = pa_to_pv(addr);
    476  1.10    simonb 	db_printf("pv at %p\n", pv);
    477  1.10    simonb 	while (pv && pv->pv_pm) {
    478  1.10    simonb 		db_printf("next %p va %p pmap %p\n", pv->pv_next,
    479  1.10    simonb 			(void *)pv->pv_va, pv->pv_pm);
    480  1.10    simonb 		pv = pv->pv_next;
    481  1.10    simonb 	}
    482  1.10    simonb }
    483  1.10    simonb 
    484  1.10    simonb static void
    485  1.37      matt db_ppc4xx_reset(db_expr_t addr, bool have_addr, db_expr_t count,
    486  1.34       scw     const char *modif)
    487  1.10    simonb {
    488  1.10    simonb 	printf("Reseting...\n");
    489  1.10    simonb 	ppc4xx_reset();
    490  1.10    simonb }
    491  1.10    simonb 
    492  1.10    simonb static void
    493  1.37      matt db_ppc4xx_tf(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    494  1.10    simonb {
    495  1.45      matt 	struct trapframe *tf;
    496  1.10    simonb 
    497  1.10    simonb 
    498  1.10    simonb 	if (have_addr) {
    499  1.45      matt 		tf = (struct trapframe *)addr;
    500  1.10    simonb 
    501  1.27      matt 		db_printf("r0-r3:  \t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    502  1.45      matt 			tf->tf_fixreg[0], tf->tf_fixreg[1],
    503  1.45      matt 			tf->tf_fixreg[2], tf->tf_fixreg[3]);
    504  1.27      matt 		db_printf("r4-r7:  \t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    505  1.45      matt 			tf->tf_fixreg[4], tf->tf_fixreg[5],
    506  1.45      matt 			tf->tf_fixreg[6], tf->tf_fixreg[7]);
    507  1.27      matt 		db_printf("r8-r11: \t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    508  1.45      matt 			tf->tf_fixreg[8], tf->tf_fixreg[9],
    509  1.45      matt 			tf->tf_fixreg[10], tf->tf_fixreg[11]);
    510  1.27      matt 		db_printf("r12-r15:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    511  1.45      matt 			tf->tf_fixreg[12], tf->tf_fixreg[13],
    512  1.45      matt 			tf->tf_fixreg[14], tf->tf_fixreg[15]);
    513  1.27      matt 		db_printf("r16-r19:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    514  1.45      matt 			tf->tf_fixreg[16], tf->tf_fixreg[17],
    515  1.45      matt 			tf->tf_fixreg[18], tf->tf_fixreg[19]);
    516  1.27      matt 		db_printf("r20-r23:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    517  1.45      matt 			tf->tf_fixreg[20], tf->tf_fixreg[21],
    518  1.45      matt 			tf->tf_fixreg[22], tf->tf_fixreg[23]);
    519  1.27      matt 		db_printf("r24-r27:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    520  1.45      matt 			tf->tf_fixreg[24], tf->tf_fixreg[25],
    521  1.45      matt 			tf->tf_fixreg[26], tf->tf_fixreg[27]);
    522  1.27      matt 		db_printf("r28-r31:\t%8.8lx %8.8lx %8.8lx %8.8lx\n",
    523  1.45      matt 			tf->tf_fixreg[28], tf->tf_fixreg[29],
    524  1.45      matt 			tf->tf_fixreg[30], tf->tf_fixreg[31]);
    525  1.10    simonb 
    526  1.27      matt 		db_printf("lr: %8.8lx cr: %8.8x xer: %8.8x ctr: %8.8lx\n",
    527  1.45      matt 			tf->tf_lr, tf->tf_cr, tf->tf_xer, tf->tf_ctr);
    528  1.27      matt 		db_printf("srr0(pc): %8.8lx srr1(msr): %8.8lx "
    529  1.27      matt 			"dear: %8.8lx esr: %8.8x\n",
    530  1.45      matt 			tf->tf_srr0, tf->tf_srr1, tf->tf_dear, tf->tf_esr);
    531  1.10    simonb 		db_printf("exc: %8.8x pid: %8.8x\n",
    532  1.45      matt 			tf->tf_exc, tf->tf_pid);
    533  1.10    simonb 	}
    534  1.10    simonb 	return;
    535  1.10    simonb }
    536  1.10    simonb 
    537  1.10    simonb static const char *const tlbsizes[] = {
    538  1.10    simonb 	  "1kB",
    539  1.10    simonb 	  "4kB",
    540  1.10    simonb 	 "16kB",
    541  1.10    simonb 	 "64kB",
    542  1.10    simonb 	"256kB",
    543  1.10    simonb 	  "1MB",
    544  1.10    simonb 	  "4MB",
    545  1.10    simonb 	 "16MB"
    546  1.10    simonb };
    547  1.10    simonb 
    548  1.10    simonb static void
    549  1.37      matt db_ppc4xx_dumptlb(db_expr_t addr, bool have_addr, db_expr_t count,
    550  1.34       scw     const char *modif)
    551  1.10    simonb {
    552  1.10    simonb 	int i, zone, tlbsize;
    553  1.10    simonb 	u_int zpr, pid, opid, msr;
    554  1.10    simonb 	u_long tlblo, tlbhi, tlbmask;
    555  1.10    simonb 
    556  1.10    simonb 	zpr = mfspr(SPR_ZPR);
    557  1.10    simonb 	for (i = 0; i < NTLB; i++) {
    558  1.36     perry 		__asm volatile("mfmsr %3;"
    559  1.10    simonb 			"mfpid %4;"
    560  1.10    simonb 			"li %0,0;"
    561  1.10    simonb 			"mtmsr %0;"
    562  1.10    simonb 			"sync; isync;"
    563  1.21   thorpej 			"tlbrelo %0,%5;"
    564  1.21   thorpej 			"tlbrehi %1,%5;"
    565  1.10    simonb 			"mfpid %2;"
    566  1.10    simonb 			"mtpid %4;"
    567  1.10    simonb 			"mtmsr %3;"
    568  1.10    simonb 			"sync; isync"
    569  1.10    simonb 			: "=&r" (tlblo), "=&r" (tlbhi), "=r" (pid),
    570  1.10    simonb 			"=&r" (msr), "=&r" (opid) : "r" (i));
    571  1.10    simonb 
    572  1.10    simonb 		if (strchr(modif, 'v') && !(tlbhi & TLB_VALID))
    573  1.10    simonb 			continue;
    574  1.10    simonb 
    575  1.10    simonb 		tlbsize = (tlbhi & TLB_SIZE_MASK) >> TLB_SIZE_SHFT;
    576  1.10    simonb 		/* map tlbsize 0 .. 7 to masks for 1kB .. 16MB */
    577  1.10    simonb 		tlbmask = ~(1 << (tlbsize * 2 + 10)) + 1;
    578  1.10    simonb 
    579  1.10    simonb 		if (have_addr && ((tlbhi & tlbmask) != (addr & tlbmask)))
    580  1.10    simonb 			continue;
    581  1.10    simonb 
    582  1.10    simonb 		zone = (tlblo & TLB_ZSEL_MASK) >> TLB_ZSEL_SHFT;
    583  1.10    simonb 		db_printf("tlb%c%2d", tlbhi & TLB_VALID ? ' ' : '*', i);
    584  1.10    simonb 		db_printf("  PID %3d EPN 0x%08lx %-5s",
    585  1.10    simonb 		    pid,
    586  1.10    simonb 		    tlbhi & tlbmask,
    587  1.10    simonb 		    tlbsizes[tlbsize]);
    588  1.10    simonb 		db_printf("  RPN 0x%08lx  ZONE %2d%c  %s %s %c%c%c%c%c %s",
    589  1.10    simonb 		    tlblo & tlbmask,
    590  1.10    simonb 		    zone,
    591  1.10    simonb 		    "NTTA"[(zpr >> ((15 - zone) * 2)) & 3],
    592  1.10    simonb 		    tlblo & TLB_EX ? "EX" : "  ",
    593  1.10    simonb 		    tlblo & TLB_WR ? "WR" : "  ",
    594  1.10    simonb 		    tlblo & TLB_W ? 'W' : ' ',
    595  1.10    simonb 		    tlblo & TLB_I ? 'I' : ' ',
    596  1.10    simonb 		    tlblo & TLB_M ? 'M' : ' ',
    597  1.10    simonb 		    tlblo & TLB_G ? 'G' : ' ',
    598  1.10    simonb 		    tlbhi & TLB_ENDIAN ? 'E' : ' ',
    599  1.10    simonb 		    tlbhi & TLB_U0 ? "U0" : "  ");
    600  1.10    simonb 		db_printf("\n");
    601  1.10    simonb 	}
    602  1.22       scw }
    603  1.22       scw 
    604  1.22       scw static void
    605  1.37      matt db_ppc4xx_dcr(db_expr_t address, bool have_addr, db_expr_t count,
    606  1.34       scw     const char *modif)
    607  1.22       scw {
    608  1.22       scw 	db_expr_t new_value;
    609  1.22       scw 	db_expr_t addr;
    610  1.23       scw 
    611  1.23       scw 	if (address < 0 || address > 0x3ff)
    612  1.23       scw 		db_error("Invalid DCR address (Valid range is 0x0 - 0x3ff)\n");
    613  1.22       scw 
    614  1.22       scw 	addr = address;
    615  1.22       scw 
    616  1.22       scw 	while (db_expression(&new_value)) {
    617  1.22       scw 		db_printf("dcr 0x%lx\t\t%s = ", addr,
    618  1.22       scw 		    db_num_to_str(db_ppc4xx_mfdcr(addr)));
    619  1.22       scw 		db_ppc4xx_mtdcr(addr, new_value);
    620  1.22       scw 		db_printf("%s\n", db_num_to_str(db_ppc4xx_mfdcr(addr)));
    621  1.22       scw 		addr += 1;
    622  1.22       scw 	}
    623  1.22       scw 
    624  1.22       scw 	if (addr == address) {
    625  1.22       scw 		db_next = (db_addr_t)addr + 1;
    626  1.22       scw 		db_prev = (db_addr_t)addr;
    627  1.22       scw 		db_printf("dcr 0x%lx\t\t%s\n", addr,
    628  1.22       scw 		    db_num_to_str(db_ppc4xx_mfdcr(addr)));
    629  1.22       scw 	} else {
    630  1.22       scw 		db_next = (db_addr_t)addr;
    631  1.22       scw 		db_prev = (db_addr_t)addr - 1;
    632  1.22       scw 	}
    633  1.22       scw 
    634  1.22       scw 	db_skip_to_eol();
    635  1.22       scw }
    636  1.22       scw 
    637  1.22       scw /*
    638  1.22       scw  * XXX Grossness Alert! XXX
    639  1.22       scw  *
    640  1.22       scw  * Please look away now if you don't like self-modifying code
    641  1.22       scw  */
    642  1.22       scw static u_int32_t db_ppc4xx_dcrfunc[4];
    643  1.22       scw 
    644  1.22       scw static db_expr_t
    645  1.22       scw db_ppc4xx_mfdcr(db_expr_t reg)
    646  1.22       scw {
    647  1.22       scw 	db_expr_t (*func)(void);
    648  1.22       scw 
    649  1.22       scw 	reg = (((reg & 0x1f) << 5) | ((reg >> 5) & 0x1f)) << 11;
    650  1.22       scw 	db_ppc4xx_dcrfunc[0] = 0x7c0004ac;		/* sync */
    651  1.22       scw 	db_ppc4xx_dcrfunc[1] = 0x4c00012c;		/* isync */
    652  1.22       scw 	db_ppc4xx_dcrfunc[2] = 0x7c600286 | reg;	/* mfdcr reg, r3 */
    653  1.22       scw 	db_ppc4xx_dcrfunc[3] = 0x4e800020;		/* blr */
    654  1.22       scw 
    655  1.22       scw 	__syncicache((void *)db_ppc4xx_dcrfunc, sizeof(db_ppc4xx_dcrfunc));
    656  1.22       scw 	func = (db_expr_t (*)(void))(void *)db_ppc4xx_dcrfunc;
    657  1.22       scw 
    658  1.22       scw 	return ((*func)());
    659  1.22       scw }
    660  1.22       scw 
    661  1.22       scw static void
    662  1.22       scw db_ppc4xx_mtdcr(db_expr_t reg, db_expr_t val)
    663  1.22       scw {
    664  1.22       scw 	db_expr_t (*func)(db_expr_t);
    665  1.22       scw 
    666  1.22       scw 	reg = (((reg & 0x1f) << 5) | ((reg >> 5) & 0x1f)) << 11;
    667  1.22       scw 	db_ppc4xx_dcrfunc[0] = 0x7c0004ac;		/* sync */
    668  1.22       scw 	db_ppc4xx_dcrfunc[1] = 0x4c00012c;		/* isync */
    669  1.22       scw 	db_ppc4xx_dcrfunc[2] = 0x7c600386 | reg;	/* mtdcr r3, reg */
    670  1.22       scw 	db_ppc4xx_dcrfunc[3] = 0x4e800020;		/* blr */
    671  1.22       scw 
    672  1.22       scw 	__syncicache((void *)db_ppc4xx_dcrfunc, sizeof(db_ppc4xx_dcrfunc));
    673  1.22       scw 	func = (db_expr_t (*)(db_expr_t))(void *)db_ppc4xx_dcrfunc;
    674  1.22       scw 
    675  1.22       scw 	(*func)(val);
    676  1.10    simonb }
    677  1.10    simonb 
    678  1.10    simonb #ifdef USERACC
    679  1.10    simonb static void
    680  1.37      matt db_ppc4xx_useracc(db_expr_t addr, bool have_addr, db_expr_t count,
    681  1.34       scw     const char *modif)
    682  1.10    simonb {
    683  1.10    simonb 	static paddr_t oldaddr = -1;
    684  1.10    simonb 	int instr = 0;
    685  1.10    simonb 	int data;
    686  1.10    simonb 	extern vaddr_t opc_disasm(vaddr_t loc, int);
    687  1.10    simonb 
    688  1.10    simonb 
    689  1.10    simonb 	if (!have_addr) {
    690  1.10    simonb 		addr = oldaddr;
    691  1.10    simonb 	}
    692  1.10    simonb 	if (addr == -1) {
    693  1.10    simonb 		db_printf("no address\n");
    694  1.10    simonb 		return;
    695  1.10    simonb 	}
    696  1.10    simonb 	addr &= ~0x3; /* align */
    697  1.10    simonb 	{
    698  1.34       scw 		const char *cp = modif;
    699  1.34       scw 		char c;
    700  1.10    simonb 		while ((c = *cp++) != 0)
    701  1.10    simonb 			if (c == 'i')
    702  1.10    simonb 				instr = 1;
    703  1.10    simonb 	}
    704  1.10    simonb 	while (count--) {
    705  1.10    simonb 		if (db_print_position() == 0) {
    706  1.10    simonb 			/* Always print the address. */
    707  1.10    simonb 			db_printf("%8.4lx:\t", addr);
    708  1.10    simonb 		}
    709  1.10    simonb 		oldaddr=addr;
    710  1.10    simonb 		copyin((void *)addr, &data, sizeof(data));
    711  1.10    simonb 		if (instr) {
    712  1.10    simonb 			opc_disasm(addr, data);
    713  1.10    simonb 		} else {
    714  1.10    simonb 			db_printf("%4.4x\n", data);
    715  1.10    simonb 		}
    716  1.10    simonb 		addr += 4;
    717  1.10    simonb 		db_end_line();
    718  1.10    simonb 	}
    719  1.10    simonb 
    720  1.10    simonb }
    721  1.10    simonb #endif
    722  1.10    simonb 
    723  1.39   garbled #endif /* DDB */
    724  1.39   garbled 
    725  1.10    simonb #endif /* PPC_IBM4XX */
    726  1.45      matt 
    727  1.45      matt #ifdef PPC_BOOKE
    728  1.45      matt static void
    729  1.45      matt db_ppcbooke_reset(db_expr_t addr, bool have_addr, db_expr_t count,
    730  1.45      matt     const char *modif)
    731  1.45      matt {
    732  1.45      matt 	printf("Reseting...\n");
    733  1.45      matt 	(*cpu_md_ops.md_cpu_reset)();
    734  1.45      matt }
    735  1.45      matt 
    736  1.45      matt static void
    737  1.45      matt db_ppcbooke_tf(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    738  1.45      matt {
    739  1.45      matt 	if (!have_addr)
    740  1.45      matt 		return;
    741  1.45      matt 
    742  1.45      matt 	const struct trapframe * const tf = (const struct trapframe *)addr;
    743  1.45      matt 
    744  1.45      matt 	db_printf("trapframe %p (exc=%x srr0/1=%#lx/%#lx esr/dear=%#x/%#lx)\n",
    745  1.45      matt 	    tf, tf->tf_exc, tf->tf_srr0, tf->tf_srr1, tf->tf_esr, tf->tf_dear);
    746  1.45      matt 	db_printf("lr =%08lx ctr=%08lx cr =%08x xer=%08x\n",
    747  1.45      matt 	    tf->tf_lr, tf->tf_ctr, tf->tf_cr, tf->tf_xer);
    748  1.45      matt 	for (u_int r = 0; r < 32; r += 4) {
    749  1.45      matt 		db_printf("r%02u=%08lx r%02u=%08lx r%02u=%08lx r%02u=%08lx\n",
    750  1.45      matt 		    r+0, tf->tf_fixreg[r+0], r+1, tf->tf_fixreg[r+1],
    751  1.45      matt 		    r+2, tf->tf_fixreg[r+2], r+3, tf->tf_fixreg[r+3]);
    752  1.45      matt 	}
    753  1.45      matt }
    754  1.45      matt 
    755  1.45      matt static void
    756  1.45      matt db_ppcbooke_dumptlb(db_expr_t addr, bool have_addr, db_expr_t count,
    757  1.45      matt     const char *modif)
    758  1.45      matt {
    759  1.45      matt 	tlb_dump(db_printf);
    760  1.45      matt }
    761  1.45      matt #endif /* PPC_BOOKE */
    762