Home | History | Annotate | Line # | Download | only in arm32
db_interface.c revision 1.21.2.1
      1  1.21.2.1  gehenna /*	$NetBSD: db_interface.c,v 1.21.2.1 2002/08/30 00:19:06 gehenna Exp $	*/
      2       1.1     matt 
      3       1.1     matt /*
      4       1.1     matt  * Copyright (c) 1996 Scott K. Stevens
      5       1.1     matt  *
      6       1.1     matt  * Mach Operating System
      7       1.1     matt  * Copyright (c) 1991,1990 Carnegie Mellon University
      8       1.1     matt  * All Rights Reserved.
      9       1.1     matt  *
     10       1.1     matt  * Permission to use, copy, modify and distribute this software and its
     11       1.1     matt  * documentation is hereby granted, provided that both the copyright
     12       1.1     matt  * notice and this permission notice appear in all copies of the
     13       1.1     matt  * software, derivative works or modified versions, and any portions
     14       1.1     matt  * thereof, and that both notices appear in supporting documentation.
     15       1.1     matt  *
     16       1.1     matt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     17       1.1     matt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     18       1.1     matt  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     19       1.1     matt  *
     20       1.1     matt  * Carnegie Mellon requests users of this software to return to
     21       1.1     matt  *
     22       1.1     matt  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     23       1.1     matt  *  School of Computer Science
     24       1.1     matt  *  Carnegie Mellon University
     25       1.1     matt  *  Pittsburgh PA 15213-3890
     26       1.1     matt  *
     27       1.1     matt  * any improvements or extensions that they make and grant Carnegie the
     28       1.1     matt  * rights to redistribute these changes.
     29       1.1     matt  *
     30       1.1     matt  *	From: db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU)
     31       1.1     matt  */
     32       1.1     matt 
     33       1.1     matt /*
     34       1.1     matt  * Interface to new debugger.
     35       1.1     matt  */
     36       1.1     matt #include "opt_ddb.h"
     37       1.1     matt 
     38       1.1     matt #include <sys/param.h>
     39       1.1     matt #include <sys/proc.h>
     40       1.1     matt #include <sys/reboot.h>
     41       1.1     matt #include <sys/systm.h>	/* just for boothowto */
     42       1.1     matt #include <sys/exec.h>
     43       1.1     matt 
     44       1.1     matt #include <uvm/uvm_extern.h>
     45       1.1     matt 
     46      1.13    chris #include <arm/arm32/db_machdep.h>
     47       1.9  thorpej #include <arm/arm32/katelib.h>
     48      1.11  thorpej #include <arm/undefined.h>
     49      1.13    chris #include <ddb/db_access.h>
     50       1.1     matt #include <ddb/db_command.h>
     51       1.1     matt #include <ddb/db_output.h>
     52       1.1     matt #include <ddb/db_variables.h>
     53       1.1     matt #include <ddb/db_sym.h>
     54       1.1     matt #include <ddb/db_extern.h>
     55       1.1     matt #include <ddb/db_interface.h>
     56       1.1     matt #include <dev/cons.h>
     57       1.1     matt 
     58       1.1     matt static int nil;
     59       1.1     matt 
     60       1.1     matt int db_access_und_sp __P((const struct db_variable *, db_expr_t *, int));
     61       1.1     matt int db_access_abt_sp __P((const struct db_variable *, db_expr_t *, int));
     62       1.1     matt int db_access_irq_sp __P((const struct db_variable *, db_expr_t *, int));
     63       1.1     matt u_int db_fetch_reg __P((int, db_regs_t *));
     64       1.1     matt 
     65      1.15  thorpej int db_trapper __P((u_int, u_int, trapframe_t *, int));
     66      1.13    chris 
     67       1.1     matt const struct db_variable db_regs[] = {
     68       1.1     matt 	{ "spsr", (long *)&DDB_REGS->tf_spsr, FCN_NULL, },
     69       1.1     matt 	{ "r0", (long *)&DDB_REGS->tf_r0, FCN_NULL, },
     70       1.1     matt 	{ "r1", (long *)&DDB_REGS->tf_r1, FCN_NULL, },
     71       1.1     matt 	{ "r2", (long *)&DDB_REGS->tf_r2, FCN_NULL, },
     72       1.1     matt 	{ "r3", (long *)&DDB_REGS->tf_r3, FCN_NULL, },
     73       1.1     matt 	{ "r4", (long *)&DDB_REGS->tf_r4, FCN_NULL, },
     74       1.1     matt 	{ "r5", (long *)&DDB_REGS->tf_r5, FCN_NULL, },
     75       1.1     matt 	{ "r6", (long *)&DDB_REGS->tf_r6, FCN_NULL, },
     76       1.1     matt 	{ "r7", (long *)&DDB_REGS->tf_r7, FCN_NULL, },
     77       1.1     matt 	{ "r8", (long *)&DDB_REGS->tf_r8, FCN_NULL, },
     78       1.1     matt 	{ "r9", (long *)&DDB_REGS->tf_r9, FCN_NULL, },
     79       1.1     matt 	{ "r10", (long *)&DDB_REGS->tf_r10, FCN_NULL, },
     80       1.1     matt 	{ "r11", (long *)&DDB_REGS->tf_r11, FCN_NULL, },
     81       1.1     matt 	{ "r12", (long *)&DDB_REGS->tf_r12, FCN_NULL, },
     82       1.1     matt 	{ "usr_sp", (long *)&DDB_REGS->tf_usr_sp, FCN_NULL, },
     83       1.1     matt 	{ "usr_lr", (long *)&DDB_REGS->tf_usr_lr, FCN_NULL, },
     84       1.1     matt 	{ "svc_sp", (long *)&DDB_REGS->tf_svc_sp, FCN_NULL, },
     85       1.1     matt 	{ "svc_lr", (long *)&DDB_REGS->tf_svc_lr, FCN_NULL, },
     86       1.1     matt 	{ "pc", (long *)&DDB_REGS->tf_pc, FCN_NULL, },
     87       1.1     matt 	{ "und_sp", (long *)&nil, db_access_und_sp, },
     88       1.1     matt 	{ "abt_sp", (long *)&nil, db_access_abt_sp, },
     89       1.1     matt 	{ "irq_sp", (long *)&nil, db_access_irq_sp, },
     90       1.1     matt };
     91       1.1     matt 
     92       1.1     matt const struct db_variable * const db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
     93       1.1     matt 
     94       1.1     matt int	db_active = 0;
     95       1.1     matt 
     96      1.15  thorpej int
     97      1.15  thorpej db_access_und_sp(const struct db_variable *vp, db_expr_t *valp, int rw)
     98       1.1     matt {
     99      1.15  thorpej 
    100       1.1     matt 	if (rw == DB_VAR_GET)
    101       1.1     matt 		*valp = get_stackptr(PSR_UND32_MODE);
    102       1.1     matt 	return(0);
    103       1.1     matt }
    104       1.1     matt 
    105      1.15  thorpej int
    106      1.15  thorpej db_access_abt_sp(const struct db_variable *vp, db_expr_t *valp, int rw)
    107       1.1     matt {
    108      1.15  thorpej 
    109       1.1     matt 	if (rw == DB_VAR_GET)
    110       1.1     matt 		*valp = get_stackptr(PSR_ABT32_MODE);
    111       1.1     matt 	return(0);
    112       1.1     matt }
    113       1.1     matt 
    114      1.15  thorpej int
    115      1.15  thorpej db_access_irq_sp(const struct db_variable *vp, db_expr_t *valp, int rw)
    116       1.1     matt {
    117      1.15  thorpej 
    118       1.1     matt 	if (rw == DB_VAR_GET)
    119       1.1     matt 		*valp = get_stackptr(PSR_IRQ32_MODE);
    120       1.1     matt 	return(0);
    121       1.1     matt }
    122       1.1     matt 
    123       1.1     matt /*
    124       1.1     matt  *  kdb_trap - field a TRACE or BPT trap
    125       1.1     matt  */
    126       1.1     matt int
    127      1.15  thorpej kdb_trap(int type, db_regs_t *regs)
    128       1.1     matt {
    129       1.1     matt 	int s;
    130       1.1     matt 
    131       1.1     matt 	switch (type) {
    132       1.1     matt 	case T_BREAKPOINT:	/* breakpoint */
    133       1.1     matt 	case -1:		/* keyboard interrupt */
    134       1.1     matt 		break;
    135       1.1     matt 	default:
    136       1.1     matt 		db_printf("kernel: trap");
    137       1.1     matt 		if (db_recover != 0) {
    138       1.1     matt 			db_error("Faulted in DDB; continuing...\n");
    139       1.1     matt 			/*NOTREACHED*/
    140       1.1     matt 		}
    141       1.1     matt 	}
    142       1.1     matt 
    143       1.1     matt 	/* Should switch to kdb`s own stack here. */
    144       1.1     matt 
    145       1.1     matt 	ddb_regs = *regs;
    146       1.1     matt 
    147       1.1     matt 	s = splhigh();
    148       1.1     matt 	db_active++;
    149       1.1     matt 	cnpollc(TRUE);
    150       1.1     matt 	db_trap(type, 0/*code*/);
    151       1.1     matt 	cnpollc(FALSE);
    152       1.1     matt 	db_active--;
    153       1.1     matt 	splx(s);
    154       1.1     matt 
    155       1.1     matt 	*regs = ddb_regs;
    156       1.1     matt 
    157       1.1     matt 	return (1);
    158       1.1     matt }
    159       1.1     matt 
    160       1.1     matt 
    161       1.1     matt static int
    162      1.15  thorpej db_validate_address(vaddr_t addr)
    163       1.1     matt {
    164       1.1     matt 	struct proc *p = curproc;
    165      1.14  thorpej 	struct pmap *pmap;
    166       1.1     matt 
    167       1.1     matt 	if (!p || !p->p_vmspace || !p->p_vmspace->vm_map.pmap)
    168      1.14  thorpej 		pmap = pmap_kernel();
    169       1.1     matt 	else
    170      1.14  thorpej 		pmap = p->p_vmspace->vm_map.pmap;
    171       1.1     matt 
    172      1.14  thorpej 	return (pmap_extract(pmap, addr, NULL) == FALSE);
    173       1.1     matt }
    174       1.1     matt 
    175       1.1     matt /*
    176       1.1     matt  * Read bytes from kernel address space for debugger.
    177       1.1     matt  */
    178       1.1     matt void
    179       1.1     matt db_read_bytes(addr, size, data)
    180      1.20  thorpej 	vaddr_t	addr;
    181      1.13    chris 	size_t	size;
    182       1.1     matt 	char	*data;
    183       1.1     matt {
    184       1.1     matt 	char	*src;
    185       1.1     matt 
    186       1.1     matt 	src = (char *)addr;
    187      1.14  thorpej 
    188      1.14  thorpej 	while (size-- > 0) {
    189       1.1     matt 		if (db_validate_address((u_int)src)) {
    190       1.1     matt 			db_printf("address %p is invalid\n", src);
    191       1.1     matt 			return;
    192       1.1     matt 		}
    193       1.1     matt 		*data++ = *src++;
    194       1.1     matt 	}
    195       1.1     matt }
    196       1.1     matt 
    197       1.1     matt static void
    198      1.15  thorpej db_write_text(vaddr_t addr, size_t size, char *data)
    199       1.1     matt {
    200      1.15  thorpej 	struct pmap *pmap = pmap_kernel();
    201      1.15  thorpej 	pd_entry_t *pde, oldpde, tmppde;
    202      1.15  thorpej 	pt_entry_t *pte, oldpte, tmppte;
    203      1.15  thorpej 	vaddr_t pgva;
    204      1.15  thorpej 	size_t limit, savesize;
    205      1.15  thorpej 	char *dst;
    206       1.1     matt 
    207      1.15  thorpej 	if ((savesize = size) == 0)
    208      1.15  thorpej 		return;
    209      1.15  thorpej 
    210      1.15  thorpej 	dst = (char *) addr;
    211       1.1     matt 
    212      1.15  thorpej 	do {
    213      1.15  thorpej 		/* Get the PDE of the current VA. */
    214      1.15  thorpej 		pde = pmap_pde(pmap, (vaddr_t) dst);
    215      1.18  thorpej 		switch ((oldpde = *pde) & L1_TYPE_MASK) {
    216      1.18  thorpej 		case L1_TYPE_S:
    217      1.18  thorpej 			pgva = (vaddr_t)dst & L1_S_FRAME;
    218      1.18  thorpej 			limit = L1_S_SIZE - ((vaddr_t)dst & L1_S_OFFSET);
    219      1.15  thorpej 
    220      1.19  thorpej 			tmppde = oldpde | L1_S_PROT_W;
    221      1.15  thorpej 			*pde = tmppde;
    222  1.21.2.1  gehenna 			PTE_SYNC(pde);
    223      1.15  thorpej 			break;
    224      1.15  thorpej 
    225      1.18  thorpej 		case L1_TYPE_C:
    226      1.18  thorpej 			pgva = (vaddr_t)dst & L2_S_FRAME;
    227      1.18  thorpej 			limit = L2_S_SIZE - ((vaddr_t)dst & L2_S_OFFSET);
    228      1.15  thorpej 
    229      1.15  thorpej 			pte = vtopte(pgva);
    230      1.15  thorpej 			oldpte = *pte;
    231      1.19  thorpej 			tmppte = oldpte | L2_S_PROT_W;
    232      1.15  thorpej 			*pte = tmppte;
    233  1.21.2.1  gehenna 			PTE_SYNC(pte);
    234      1.15  thorpej 			break;
    235      1.15  thorpej 
    236      1.15  thorpej 		default:
    237      1.15  thorpej 			printf(" address 0x%08lx not a valid page\n",
    238      1.15  thorpej 			    (vaddr_t) dst);
    239      1.15  thorpej 			return;
    240      1.15  thorpej 		}
    241      1.15  thorpej 		cpu_tlb_flushD_SE(pgva);
    242      1.16  thorpej 		cpu_cpwait();
    243       1.1     matt 
    244      1.15  thorpej 		if (limit > size)
    245      1.15  thorpej 			limit = size;
    246      1.15  thorpej 		size -= limit;
    247       1.1     matt 
    248      1.15  thorpej 		/*
    249      1.15  thorpej 		 * Page is now writable.  Do as much access as we
    250      1.15  thorpej 		 * can in this page.
    251      1.15  thorpej 		 */
    252      1.15  thorpej 		for (; limit > 0; limit--)
    253      1.15  thorpej 			*dst++ = *data++;
    254       1.1     matt 
    255      1.15  thorpej 		/*
    256      1.15  thorpej 		 * Restore old mapping permissions.
    257      1.15  thorpej 		 */
    258      1.18  thorpej 		switch (oldpde & L1_TYPE_MASK) {
    259      1.18  thorpej 		case L1_TYPE_S:
    260      1.15  thorpej 			*pde = oldpde;
    261  1.21.2.1  gehenna 			PTE_SYNC(pde);
    262      1.15  thorpej 			break;
    263      1.15  thorpej 
    264      1.18  thorpej 		case L1_TYPE_C:
    265      1.15  thorpej 			*pte = oldpte;
    266  1.21.2.1  gehenna 			PTE_SYNC(pte);
    267      1.15  thorpej 			break;
    268      1.15  thorpej 		}
    269      1.15  thorpej 		cpu_tlb_flushD_SE(pgva);
    270      1.16  thorpej 		cpu_cpwait();
    271      1.15  thorpej 	} while (size != 0);
    272       1.1     matt 
    273      1.15  thorpej 	/* Sync the I-cache. */
    274      1.17  thorpej 	cpu_icache_sync_range(addr, savesize);
    275       1.1     matt }
    276       1.1     matt 
    277       1.1     matt /*
    278       1.1     matt  * Write bytes to kernel address space for debugger.
    279       1.1     matt  */
    280       1.1     matt void
    281      1.15  thorpej db_write_bytes(vaddr_t addr, size_t size, char *data)
    282       1.1     matt {
    283      1.15  thorpej 	extern char etext[];
    284      1.15  thorpej 	char *dst;
    285      1.15  thorpej 	size_t loop;
    286      1.15  thorpej 
    287      1.15  thorpej 	/* If any part is in kernel text, use db_write_text() */
    288      1.15  thorpej 	if (addr >= KERNEL_TEXT_BASE && addr < (vaddr_t) etext) {
    289      1.15  thorpej 		db_write_text(addr, size, data);
    290      1.15  thorpej 		return;
    291      1.15  thorpej 	}
    292       1.1     matt 
    293       1.1     matt 	dst = (char *)addr;
    294       1.1     matt 	loop = size;
    295      1.14  thorpej 	while (loop-- > 0) {
    296      1.15  thorpej 		if (db_validate_address((u_int)dst)) {
    297      1.15  thorpej 			db_printf("address %p is invalid\n", dst);
    298      1.15  thorpej 			return;
    299       1.1     matt 		}
    300      1.15  thorpej 		*dst++ = *data++;
    301       1.1     matt 	}
    302       1.1     matt 	/* make sure the caches and memory are in sync */
    303      1.17  thorpej 	cpu_icache_sync_range(addr, size);
    304       1.1     matt 
    305       1.1     matt 	/* In case the current page tables have been modified ... */
    306       1.1     matt 	cpu_tlb_flushID();
    307      1.16  thorpej 	cpu_cpwait();
    308       1.1     matt }
    309       1.1     matt 
    310       1.1     matt void
    311      1.15  thorpej cpu_Debugger(void)
    312       1.1     matt {
    313       1.1     matt 	asm(".word	0xe7ffffff");
    314       1.1     matt }
    315       1.1     matt 
    316       1.1     matt const struct db_command db_machine_command_table[] = {
    317       1.1     matt 	{ "frame",	db_show_frame_cmd,	0, NULL },
    318       1.1     matt 	{ "panic",	db_show_panic_cmd,	0, NULL },
    319       1.2     matt #ifdef ARM32_DB_COMMANDS
    320       1.2     matt 	ARM32_DB_COMMANDS,
    321       1.2     matt #endif
    322       1.1     matt 	{ NULL, 	NULL, 			0, NULL }
    323       1.1     matt };
    324       1.1     matt 
    325       1.1     matt int
    326      1.15  thorpej db_trapper(u_int addr, u_int inst, trapframe_t *frame, int fault_code)
    327       1.1     matt {
    328      1.15  thorpej 
    329       1.1     matt 	if (fault_code == 0) {
    330       1.1     matt 		if ((inst & ~INSN_COND_MASK) == (BKPT_INST & ~INSN_COND_MASK))
    331       1.1     matt 			kdb_trap(T_BREAKPOINT, frame);
    332       1.1     matt 		else
    333       1.1     matt 			kdb_trap(-1, frame);
    334       1.1     matt 	} else
    335       1.1     matt 		return (1);
    336       1.1     matt 	return (0);
    337       1.1     matt }
    338       1.1     matt 
    339       1.1     matt extern u_int esym;
    340       1.1     matt extern u_int end;
    341       1.1     matt 
    342       1.3    bjh21 static struct undefined_handler db_uh;
    343       1.3    bjh21 
    344       1.1     matt void
    345      1.15  thorpej db_machine_init(void)
    346       1.1     matt {
    347       1.5    bjh21 #ifndef __ELF__
    348       1.1     matt 	struct exec *kernexec = (struct exec *)KERNEL_TEXT_BASE;
    349       1.1     matt 	int len;
    350       1.1     matt 
    351       1.1     matt 	/*
    352       1.1     matt 	 * The boot loader currently loads the kernel with the a.out
    353       1.1     matt 	 * header still attached.
    354       1.1     matt 	 */
    355       1.1     matt 
    356       1.1     matt 	if (kernexec->a_syms == 0) {
    357       1.8  thorpej 		printf("ddb: No symbol table\n");
    358       1.1     matt 	} else {
    359       1.2     matt 		/* cover the symbols themselves (what is the int for?? XXX) */
    360       1.1     matt 		esym = (int)&end + kernexec->a_syms + sizeof(int);
    361       1.2     matt 
    362       1.1     matt 		/*
    363       1.1     matt 		 * and the string table.  (int containing size of string
    364       1.1     matt 		 * table is included in string table size).
    365       1.1     matt 		 */
    366       1.1     matt 		len = *((u_int *)esym);
    367       1.1     matt 		esym += (len + (sizeof(u_int) - 1)) & ~(sizeof(u_int) - 1);
    368       1.1     matt 	}
    369       1.5    bjh21 #endif
    370       1.1     matt 
    371       1.3    bjh21 	/*
    372       1.3    bjh21 	 * We get called before malloc() is available, so supply a static
    373       1.3    bjh21 	 * struct undefined_handler.
    374       1.3    bjh21 	 */
    375       1.3    bjh21 	db_uh.uh_handler = db_trapper;
    376       1.3    bjh21 	install_coproc_handler_static(0, &db_uh);
    377       1.1     matt }
    378       1.1     matt 
    379       1.1     matt u_int
    380      1.15  thorpej db_fetch_reg(int reg, db_regs_t *db_regs)
    381       1.1     matt {
    382       1.1     matt 
    383       1.1     matt 	switch (reg) {
    384       1.1     matt 	case 0:
    385       1.1     matt 		return (db_regs->tf_r0);
    386       1.1     matt 	case 1:
    387       1.1     matt 		return (db_regs->tf_r1);
    388       1.1     matt 	case 2:
    389       1.1     matt 		return (db_regs->tf_r2);
    390       1.1     matt 	case 3:
    391       1.1     matt 		return (db_regs->tf_r3);
    392       1.1     matt 	case 4:
    393       1.1     matt 		return (db_regs->tf_r4);
    394       1.1     matt 	case 5:
    395       1.1     matt 		return (db_regs->tf_r5);
    396       1.1     matt 	case 6:
    397       1.1     matt 		return (db_regs->tf_r6);
    398       1.1     matt 	case 7:
    399       1.1     matt 		return (db_regs->tf_r7);
    400       1.1     matt 	case 8:
    401       1.1     matt 		return (db_regs->tf_r8);
    402       1.1     matt 	case 9:
    403       1.1     matt 		return (db_regs->tf_r9);
    404       1.1     matt 	case 10:
    405       1.1     matt 		return (db_regs->tf_r10);
    406       1.1     matt 	case 11:
    407       1.1     matt 		return (db_regs->tf_r11);
    408       1.1     matt 	case 12:
    409       1.1     matt 		return (db_regs->tf_r12);
    410       1.1     matt 	case 13:
    411       1.1     matt 		return (db_regs->tf_svc_sp);
    412       1.1     matt 	case 14:
    413       1.1     matt 		return (db_regs->tf_svc_lr);
    414       1.1     matt 	case 15:
    415       1.1     matt 		return (db_regs->tf_pc);
    416       1.1     matt 	default:
    417       1.1     matt 		panic("db_fetch_reg: botch");
    418       1.1     matt 	}
    419       1.1     matt }
    420       1.1     matt 
    421       1.1     matt u_int
    422      1.15  thorpej branch_taken(u_int insn, u_int pc, db_regs_t *db_regs)
    423       1.1     matt {
    424       1.1     matt 	u_int addr, nregs;
    425       1.1     matt 
    426       1.1     matt 	switch ((insn >> 24) & 0xf) {
    427       1.1     matt 	case 0xa:	/* b ... */
    428       1.1     matt 	case 0xb:	/* bl ... */
    429       1.1     matt 		addr = ((insn << 2) & 0x03ffffff);
    430       1.1     matt 		if (addr & 0x02000000)
    431       1.1     matt 			addr |= 0xfc000000;
    432       1.1     matt 		return (pc + 8 + addr);
    433       1.1     matt 	case 0x7:	/* ldr pc, [pc, reg, lsl #2] */
    434       1.1     matt 		addr = db_fetch_reg(insn & 0xf, db_regs);
    435       1.1     matt 		addr = pc + 8 + (addr << 2);
    436       1.1     matt 		db_read_bytes(addr, 4, (char *)&addr);
    437       1.1     matt 		return (addr);
    438       1.1     matt 	case 0x1:	/* mov pc, reg */
    439       1.1     matt 		addr = db_fetch_reg(insn & 0xf, db_regs);
    440       1.1     matt 		return (addr);
    441       1.1     matt 	case 0x8:	/* ldmxx reg, {..., pc} */
    442       1.1     matt 	case 0x9:
    443       1.1     matt 		addr = db_fetch_reg((insn >> 16) & 0xf, db_regs);
    444       1.1     matt 		nregs = (insn  & 0x5555) + ((insn  >> 1) & 0x5555);
    445       1.1     matt 		nregs = (nregs & 0x3333) + ((nregs >> 2) & 0x3333);
    446       1.1     matt 		nregs = (nregs + (nregs >> 4)) & 0x0f0f;
    447       1.1     matt 		nregs = (nregs + (nregs >> 8)) & 0x001f;
    448       1.1     matt 		switch ((insn >> 23) & 0x3) {
    449       1.1     matt 		case 0x0:	/* ldmda */
    450       1.1     matt 			addr = addr - 0;
    451       1.1     matt 			break;
    452       1.1     matt 		case 0x1:	/* ldmia */
    453       1.1     matt 			addr = addr + 0 + ((nregs - 1) << 2);
    454       1.1     matt 			break;
    455       1.1     matt 		case 0x2:	/* ldmdb */
    456       1.1     matt 			addr = addr - 4;
    457       1.1     matt 			break;
    458       1.1     matt 		case 0x3:	/* ldmib */
    459       1.1     matt 			addr = addr + 4 + ((nregs - 1) << 2);
    460       1.1     matt 			break;
    461       1.1     matt 		}
    462       1.1     matt 		db_read_bytes(addr, 4, (char *)&addr);
    463       1.1     matt 		return (addr);
    464       1.1     matt 	default:
    465       1.1     matt 		panic("branch_taken: botch");
    466       1.1     matt 	}
    467       1.1     matt }
    468