xref: /onnv-gate/usr/src/cmd/mdb/sparc/kmdb/kvm_isadep.c (revision 1234:f13b951c0f14)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*1234Sjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * isa-dependent portions of the kmdb target
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <mdb/mdb_kreg_impl.h>
340Sstevel@tonic-gate #include <mdb/mdb_debug.h>
350Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
360Sstevel@tonic-gate #include <mdb/mdb_v9util.h>
370Sstevel@tonic-gate #include <mdb/mdb_target_impl.h>
380Sstevel@tonic-gate #include <mdb/mdb_err.h>
390Sstevel@tonic-gate #include <mdb/mdb_umem.h>
400Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
410Sstevel@tonic-gate #include <kmdb/kmdb_dpi.h>
420Sstevel@tonic-gate #include <kmdb/kmdb_promif.h>
430Sstevel@tonic-gate #include <kmdb/kmdb_asmutil.h>
440Sstevel@tonic-gate #include <kmdb/kvm.h>
450Sstevel@tonic-gate #include <mdb/mdb.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/stack.h>
490Sstevel@tonic-gate #include <sys/regset.h>
500Sstevel@tonic-gate #include <sys/sysmacros.h>
510Sstevel@tonic-gate #include <sys/bitmap.h>
520Sstevel@tonic-gate #include <sys/machtrap.h>
530Sstevel@tonic-gate #include <sys/trap.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* Higher than the highest trap number for which we have a specific specifier */
560Sstevel@tonic-gate #define	KMT_MAXTRAPNO	0x1ff
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	OP(x)		((x) >> 30)
590Sstevel@tonic-gate #define	OP3(x)		(((x) >> 19) & 0x3f)
600Sstevel@tonic-gate #define	RD(x)		(((x) >> 25) & 0x1f)
610Sstevel@tonic-gate #define	RS1(x)		(((x) >> 14) & 0x1f)
620Sstevel@tonic-gate #define	RS2(x)		((x) & 0x1f)
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #define	OP_ARITH	0x2
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	OP3_OR		0x02
670Sstevel@tonic-gate #define	OP3_SAVE	0x3c
680Sstevel@tonic-gate #define	OP3_RESTORE	0x3d
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static int
kmt_stack_iter(mdb_tgt_t * t,const mdb_tgt_gregset_t * gsp,mdb_tgt_stack_f * func,void * arg,int cpuid)710Sstevel@tonic-gate kmt_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
72*1234Sjohnlev     mdb_tgt_stack_f *func, void *arg, int cpuid)
730Sstevel@tonic-gate {
74*1234Sjohnlev 	const mdb_tgt_gregset_t *grp;
750Sstevel@tonic-gate 	mdb_tgt_gregset_t gregs;
760Sstevel@tonic-gate 	kreg_t *kregs = &gregs.kregs[0];
770Sstevel@tonic-gate 	long nwin, stopwin, canrestore, wp, i, sp;
780Sstevel@tonic-gate 	long argv[6];
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	 * If gsp isn't null, we were asked to dump a trace from a
820Sstevel@tonic-gate 	 * specific location.  The normal iterator can handle that.
830Sstevel@tonic-gate 	 */
840Sstevel@tonic-gate 	if (gsp != NULL) {
85*1234Sjohnlev 		if (cpuid != DPI_MASTER_CPUID)
860Sstevel@tonic-gate 			warn("register set provided - ignoring cpu argument\n");
870Sstevel@tonic-gate 		return (mdb_kvm_v9stack_iter(t, gsp, func, arg));
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
90*1234Sjohnlev 	if (kmdb_dpi_get_cpu_state(cpuid) < 0) {
91*1234Sjohnlev 		warn("failed to iterate through stack for cpu %u", cpuid);
920Sstevel@tonic-gate 		return (DCMD_ERR);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	/*
960Sstevel@tonic-gate 	 * We're being asked to dump the trace for the current CPU.
970Sstevel@tonic-gate 	 * To do that, we need to iterate first through the saved
980Sstevel@tonic-gate 	 * register windors.  If there's more to the trace than that,
990Sstevel@tonic-gate 	 * we'll hand off to the normal iterator.
1000Sstevel@tonic-gate 	 */
101*1234Sjohnlev 	if ((grp = kmdb_dpi_get_gregs(cpuid)) == NULL) {
102*1234Sjohnlev 		warn("failed to retrieve registers for cpu %d", cpuid);
103*1234Sjohnlev 		return (DCMD_ERR);
104*1234Sjohnlev 	}
105*1234Sjohnlev 
106*1234Sjohnlev 	bcopy(grp, &gregs, sizeof (mdb_tgt_gregset_t));
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	wp = kregs[KREG_CWP];
1090Sstevel@tonic-gate 	canrestore = kregs[KREG_CANRESTORE];
110*1234Sjohnlev 	nwin = kmdb_dpi_get_nwin(cpuid);
1110Sstevel@tonic-gate 	stopwin = ((wp + nwin) - canrestore - 1) % nwin;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_KMOD, "dumping cwp = %lu, canrestore = %lu, "
1140Sstevel@tonic-gate 	    "stopwin = %lu\n", wp, canrestore, stopwin);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	for (;;) {
1170Sstevel@tonic-gate 		struct rwindow rwin;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 		for (i = 0; i < 6; i++)
1200Sstevel@tonic-gate 			argv[i] = kregs[KREG_I0 + i];
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		if (kregs[KREG_PC] != 0 &&
1230Sstevel@tonic-gate 		    func(arg, kregs[KREG_PC], 6, argv, &gregs) != 0)
1240Sstevel@tonic-gate 			return (0);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 		kregs[KREG_PC] = kregs[KREG_I7];
1270Sstevel@tonic-gate 		kregs[KREG_NPC] = kregs[KREG_PC] + 4;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		if ((sp = kregs[KREG_FP] + STACK_BIAS) == STACK_BIAS || sp == 0)
1300Sstevel@tonic-gate 			return (0); /* Stop if we're at the end of stack */
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		if (sp & (STACK_ALIGN - 1))
1330Sstevel@tonic-gate 			return (set_errno(EMDB_STKALIGN));
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		wp = (wp + nwin - 1) % nwin;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		if (wp == stopwin)
1380Sstevel@tonic-gate 			break;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		bcopy(&kregs[KREG_I0], &kregs[KREG_O0], 8 * sizeof (kreg_t));
1410Sstevel@tonic-gate 
142*1234Sjohnlev 		if (kmdb_dpi_get_rwin(cpuid, wp, &rwin) < 0) {
1430Sstevel@tonic-gate 			warn("unable to get registers from window %ld\n", wp);
1440Sstevel@tonic-gate 			return (-1);
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		for (i = 0; i < 8; i++)
1480Sstevel@tonic-gate 			kregs[KREG_L0 + i] = (uintptr_t)rwin.rw_local[i];
1490Sstevel@tonic-gate 		for (i = 0; i < 8; i++)
1500Sstevel@tonic-gate 			kregs[KREG_I0 + i] = (uintptr_t)rwin.rw_in[i];
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_KMOD, "dumping wp %ld and beyond normally\n", wp);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/*
1560Sstevel@tonic-gate 	 * hack - if we null out pc here, iterator won't print the frame
1570Sstevel@tonic-gate 	 * that corresponds to the current set of registers.  That's what we
1580Sstevel@tonic-gate 	 * want because we just printed them above.
1590Sstevel@tonic-gate 	 */
1600Sstevel@tonic-gate 	kregs[KREG_PC] = 0;
1610Sstevel@tonic-gate 	return (mdb_kvm_v9stack_iter(t, &gregs, func, arg));
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate void
kmt_printregs(const mdb_tgt_gregset_t * gregs)1650Sstevel@tonic-gate kmt_printregs(const mdb_tgt_gregset_t *gregs)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	mdb_v9printregs(gregs);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static int
kmt_stack_common(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv,int cpuid,mdb_tgt_stack_f * func,kreg_t saved_pc)1710Sstevel@tonic-gate kmt_stack_common(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
1720Sstevel@tonic-gate     int cpuid, mdb_tgt_stack_f *func, kreg_t saved_pc)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	mdb_tgt_gregset_t *grp = NULL;
1750Sstevel@tonic-gate 	mdb_tgt_gregset_t gregs;
176436Sdmick 	void *arg = (void *)(uintptr_t)mdb.m_nargs;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
1790Sstevel@tonic-gate 		bzero(&gregs, sizeof (gregs));
1800Sstevel@tonic-gate 		gregs.kregs[KREG_FP] = addr;
1810Sstevel@tonic-gate 		gregs.kregs[KREG_I7] = saved_pc;
1820Sstevel@tonic-gate 		grp = &gregs;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (argc != 0) {
1860Sstevel@tonic-gate 		if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
1870Sstevel@tonic-gate 			return (DCMD_USAGE);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		if (argv->a_type == MDB_TYPE_STRING)
190436Sdmick 			arg = (void *)(uintptr_t)(uint_t)
191436Sdmick 			    mdb_strtoull(argv->a_un.a_str);
1920Sstevel@tonic-gate 		else
193436Sdmick 			arg = (void *)(uintptr_t)(uint_t)argv->a_un.a_val;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	(void) kmt_stack_iter(mdb.m_target, grp, func, arg, cpuid);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	return (DCMD_OK);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate int
kmt_cpustack(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv,int cpuid,int verbose)2020Sstevel@tonic-gate kmt_cpustack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
2030Sstevel@tonic-gate     int cpuid, int verbose)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	return (kmt_stack_common(addr, flags, argc, argv, cpuid,
2060Sstevel@tonic-gate 	    (verbose ? mdb_kvm_v9framev : mdb_kvm_v9frame), 0));
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate int
kmt_stack(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2100Sstevel@tonic-gate kmt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
2130Sstevel@tonic-gate 	    mdb_kvm_v9frame, 0));
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate int
kmt_stackv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2170Sstevel@tonic-gate kmt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
2200Sstevel@tonic-gate 	    mdb_kvm_v9framev, 0));
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate int
kmt_stackr(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2240Sstevel@tonic-gate kmt_stackr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	/*
2270Sstevel@tonic-gate 	 * Force printing of the first register window by setting the saved
2280Sstevel@tonic-gate 	 * pc (%i7) to PC_FAKE.
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
2310Sstevel@tonic-gate 	    mdb_kvm_v9framer, PC_FAKE));
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate ssize_t
kmt_write_page(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)2350Sstevel@tonic-gate kmt_write_page(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	jmp_buf *oldpcb = NULL;
2380Sstevel@tonic-gate 	jmp_buf pcb;
2390Sstevel@tonic-gate 	physaddr_t pa;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/*
2420Sstevel@tonic-gate 	 * Can we write to this page?
2430Sstevel@tonic-gate 	 */
2440Sstevel@tonic-gate 	if (!(t->t_flags & MDB_TGT_F_ALLOWIO) &&
2450Sstevel@tonic-gate 	    (nbytes = kmdb_kdi_range_is_nontoxic(addr, nbytes, 1)) == 0)
2460Sstevel@tonic-gate 		return (set_errno(EMDB_NOMAP));
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * The OBP va>pa call returns a protection value that's right only some
2500Sstevel@tonic-gate 	 * of the time.  We can, however, tell if we failed a write due to a
2510Sstevel@tonic-gate 	 * protection violation.  If we get such an error, we'll retry the
2520Sstevel@tonic-gate 	 * write using pwrite.
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	if (setjmp(pcb) != 0) {
2550Sstevel@tonic-gate 		/* We failed the write */
2560Sstevel@tonic-gate 		kmdb_dpi_restore_fault_hdlr(oldpcb);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		if (errno == EACCES && kmdb_prom_vtop(addr, &pa) == 0)
2590Sstevel@tonic-gate 			return (kmt_pwrite(t, buf, nbytes, pa));
2600Sstevel@tonic-gate 		return (-1); /* errno is set for us */
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_KMOD, "copying %lu bytes from %p to %p\n", nbytes,
2640Sstevel@tonic-gate 	    buf, (void *)addr);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	oldpcb = kmdb_dpi_set_fault_hdlr(&pcb);
2670Sstevel@tonic-gate 	(void) kmt_writer((void *)buf, nbytes, addr);
2680Sstevel@tonic-gate 	kmdb_dpi_restore_fault_hdlr(oldpcb);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	return (nbytes);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*ARGSUSED*/
2740Sstevel@tonic-gate ssize_t
kmt_write(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)2750Sstevel@tonic-gate kmt_write(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	size_t ntowrite, nwritten, n;
2780Sstevel@tonic-gate 	int rc;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	kmdb_prom_check_interrupt();
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if (nbytes == 0)
2830Sstevel@tonic-gate 		return (0);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	/*
2860Sstevel@tonic-gate 	 * Break the writes up into page-sized chunks.  First, the leading page
2870Sstevel@tonic-gate 	 * fragment (if any), then the subsequent pages.
2880Sstevel@tonic-gate 	 */
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	if ((n = (addr & (mdb.m_pagesize - 1))) != 0) {
2910Sstevel@tonic-gate 		ntowrite = MIN(mdb.m_pagesize - n, nbytes);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		if ((rc = kmt_write_page(t, buf, ntowrite, addr)) != ntowrite)
2940Sstevel@tonic-gate 			return (rc);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		addr = roundup(addr, mdb.m_pagesize);
2970Sstevel@tonic-gate 		nbytes -= ntowrite;
2980Sstevel@tonic-gate 		nwritten = ntowrite;
2990Sstevel@tonic-gate 		buf = ((caddr_t)buf + ntowrite);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	while (nbytes > 0) {
3030Sstevel@tonic-gate 		ntowrite = MIN(mdb.m_pagesize, nbytes);
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		if ((rc = kmt_write_page(t, buf, ntowrite, addr)) != ntowrite)
3060Sstevel@tonic-gate 			return (rc < 0 ? rc : rc + nwritten);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		addr += mdb.m_pagesize;
3090Sstevel@tonic-gate 		nbytes -= ntowrite;
3100Sstevel@tonic-gate 		nwritten += ntowrite;
3110Sstevel@tonic-gate 		buf = ((caddr_t)buf + ntowrite);
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	return (rc);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate /*ARGSUSED*/
3180Sstevel@tonic-gate ssize_t
kmt_ioread(mdb_tgt_t * t,void * buf,size_t nbytes,uintptr_t addr)3190Sstevel@tonic-gate kmt_ioread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	return (set_errno(EMDB_TGTHWNOTSUP));
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*ARGSUSED*/
3250Sstevel@tonic-gate ssize_t
kmt_iowrite(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)3260Sstevel@tonic-gate kmt_iowrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	return (set_errno(EMDB_TGTHWNOTSUP));
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate const char *
kmt_def_dismode(void)3320Sstevel@tonic-gate kmt_def_dismode(void)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate #ifdef __sparcv9
3350Sstevel@tonic-gate 	return ("v9plus");
3360Sstevel@tonic-gate #else
3370Sstevel@tonic-gate 	return ("v8");
3380Sstevel@tonic-gate #endif
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * If we are stopped on a save instruction or at the first instruction of a
3430Sstevel@tonic-gate  * known function, return %o7 as the step-out address; otherwise return the
3440Sstevel@tonic-gate  * current frame's return address (%i7).  Significantly better handling of
3450Sstevel@tonic-gate  * step out in leaf routines could be accomplished by implementing more
3460Sstevel@tonic-gate  * complex decoding of the current function and our current state.
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate int
kmt_step_out(mdb_tgt_t * t,uintptr_t * p)3490Sstevel@tonic-gate kmt_step_out(mdb_tgt_t *t, uintptr_t *p)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	kreg_t pc, i7, o7;
3520Sstevel@tonic-gate 	GElf_Sym func;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("pc", &pc);
3550Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("i7", &i7);
3560Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("o7", &o7);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY, NULL, 0,
3590Sstevel@tonic-gate 	    &func, NULL) == 0 && func.st_value == pc)
3600Sstevel@tonic-gate 		*p = o7 + 2 * sizeof (mdb_instr_t);
3610Sstevel@tonic-gate 	else {
3620Sstevel@tonic-gate 		mdb_instr_t instr;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		if (mdb_tgt_vread(t, &instr, sizeof (instr), pc) !=
3650Sstevel@tonic-gate 		    sizeof (instr)) {
3660Sstevel@tonic-gate 			warn("failed to read instruction at %p for step out",
3670Sstevel@tonic-gate 			    (void *)pc);
3680Sstevel@tonic-gate 			return (-1);
3690Sstevel@tonic-gate 		}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 		if (OP(instr) == OP_ARITH && OP3(instr) == OP3_SAVE)
3720Sstevel@tonic-gate 			*p = o7 + 2 * sizeof (mdb_instr_t);
3730Sstevel@tonic-gate 		else
3740Sstevel@tonic-gate 			*p = i7 + 2 * sizeof (mdb_instr_t);
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	return (0);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*ARGSUSED*/
3810Sstevel@tonic-gate int
kmt_step_branch(mdb_tgt_t * t)3820Sstevel@tonic-gate kmt_step_branch(mdb_tgt_t *t)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	return (set_errno(EMDB_TGTHWNOTSUP));
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate static const char *
regno2name(int idx)3880Sstevel@tonic-gate regno2name(int idx)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	const mdb_tgt_regdesc_t *rd;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	for (rd = mdb_sparcv9_kregs; rd->rd_name != NULL; rd++) {
3930Sstevel@tonic-gate 		if (idx == rd->rd_num)
3940Sstevel@tonic-gate 			return (rd->rd_name);
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	ASSERT(rd->rd_name != NULL);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	return ("unknown");
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate /*
4030Sstevel@tonic-gate  * Step over call and jmpl by returning the address of the position where a
4040Sstevel@tonic-gate  * temporary breakpoint can be set to catch return from the control transfer.
4050Sstevel@tonic-gate  * This function does not currently provide advanced decoding of DCTI couples
4060Sstevel@tonic-gate  * or any other complex special case; we just fall back to single-step.
4070Sstevel@tonic-gate  */
4080Sstevel@tonic-gate int
kmt_next(mdb_tgt_t * t,uintptr_t * p)4090Sstevel@tonic-gate kmt_next(mdb_tgt_t *t, uintptr_t *p)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	kreg_t pc, npc;
4120Sstevel@tonic-gate 	GElf_Sym func;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("pc", &pc);
4150Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("npc", &npc);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY, NULL, 0,
4180Sstevel@tonic-gate 	    &func, NULL) != 0)
4190Sstevel@tonic-gate 		return (-1);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (npc < func.st_value || func.st_value + func.st_size <= npc) {
4220Sstevel@tonic-gate 		mdb_instr_t instr;
4230Sstevel@tonic-gate 		kreg_t reg;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		/*
4260Sstevel@tonic-gate 		 * We're about to transfer control outside this function, so we
4270Sstevel@tonic-gate 		 * want to stop when control returns from the other function.
4280Sstevel@tonic-gate 		 * Normally the return address will be in %o7, tail-calls being
4290Sstevel@tonic-gate 		 * the exception.  We try to discover if this is a tail-call and
4300Sstevel@tonic-gate 		 * compute the return address in that case.
4310Sstevel@tonic-gate 		 */
4320Sstevel@tonic-gate 		if (mdb_tgt_vread(t, &instr, sizeof (instr), pc) !=
4330Sstevel@tonic-gate 		    sizeof (instr)) {
4340Sstevel@tonic-gate 			warn("failed to read instruction at %p for next",
4350Sstevel@tonic-gate 			    (void *)pc);
4360Sstevel@tonic-gate 			return (-1);
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		if (OP(instr) == OP_ARITH && OP3(instr) == OP3_RESTORE) {
4400Sstevel@tonic-gate 			(void) kmdb_dpi_get_register("i7", &reg);
4410Sstevel@tonic-gate 		} else if (OP(instr) == OP_ARITH && OP3(instr) == OP3_OR &&
4420Sstevel@tonic-gate 		    RD(instr) == KREG_O7) {
4430Sstevel@tonic-gate 			if (RS1(instr) == KREG_G0)
4440Sstevel@tonic-gate 				return (set_errno(EAGAIN));
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 			(void) kmdb_dpi_get_register(regno2name(RS2(instr)),
4470Sstevel@tonic-gate 			    &reg);
4480Sstevel@tonic-gate 		} else
4490Sstevel@tonic-gate 			(void) kmdb_dpi_get_register("o7", &reg);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 		*p = reg + 2 * sizeof (mdb_instr_t);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 		return (0);
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	return (set_errno(EAGAIN));
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate const char *
kmt_trapname(int trapnum)4600Sstevel@tonic-gate kmt_trapname(int trapnum)
4610Sstevel@tonic-gate {
4620Sstevel@tonic-gate 	static char trapname[11];
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	switch (trapnum) {
4650Sstevel@tonic-gate 	case T_INSTR_EXCEPTION:
4660Sstevel@tonic-gate 		return ("instruction access error trap");
4670Sstevel@tonic-gate 	case T_ALIGNMENT:
4680Sstevel@tonic-gate 		return ("improper alignment trap");
4690Sstevel@tonic-gate 	case T_UNIMP_INSTR:
4700Sstevel@tonic-gate 		return ("illegal instruction trap");
4710Sstevel@tonic-gate 	case T_IDIV0:
4720Sstevel@tonic-gate 		return ("division by zero trap");
4730Sstevel@tonic-gate 	case T_FAST_INSTR_MMU_MISS:
4740Sstevel@tonic-gate 		return ("instruction access MMU miss trap");
4750Sstevel@tonic-gate 	case T_FAST_DATA_MMU_MISS:
4760Sstevel@tonic-gate 		return ("data access MMU miss trap");
4770Sstevel@tonic-gate 	case ST_KMDB_TRAP|T_SOFTWARE_TRAP:
4780Sstevel@tonic-gate 		return ("debugger entry trap");
4790Sstevel@tonic-gate 	case ST_KMDB_BREAKPOINT|T_SOFTWARE_TRAP:
4800Sstevel@tonic-gate 		return ("breakpoint trap");
4810Sstevel@tonic-gate 	default:
4820Sstevel@tonic-gate 		(void) mdb_snprintf(trapname, sizeof (trapname), "trap %#x",
4830Sstevel@tonic-gate 		    trapnum);
4840Sstevel@tonic-gate 		return (trapname);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate void
kmt_init_isadep(mdb_tgt_t * t)4890Sstevel@tonic-gate kmt_init_isadep(mdb_tgt_t *t)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	kmt_data_t *kmt = t->t_data;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	kmt->kmt_rds = mdb_sparcv9_kregs;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	kmt->kmt_trapmax = KMT_MAXTRAPNO;
4960Sstevel@tonic-gate 	kmt->kmt_trapmap = mdb_zalloc(BT_SIZEOFMAP(kmt->kmt_trapmax), UM_SLEEP);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	/* Traps for which we want to provide an explicit message */
4990Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_INSTR_EXCEPTION, MDB_TGT_SPEC_INTERNAL,
5000Sstevel@tonic-gate 	    no_se_f, NULL);
5010Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_ALIGNMENT, MDB_TGT_SPEC_INTERNAL,
5020Sstevel@tonic-gate 	    no_se_f, NULL);
5030Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_UNIMP_INSTR, MDB_TGT_SPEC_INTERNAL,
5040Sstevel@tonic-gate 	    no_se_f, NULL);
5050Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_IDIV0, MDB_TGT_SPEC_INTERNAL,
5060Sstevel@tonic-gate 	    no_se_f, NULL);
5070Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_FAST_INSTR_MMU_MISS,
5080Sstevel@tonic-gate 	    MDB_TGT_SPEC_INTERNAL, no_se_f, NULL);
5090Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_FAST_DATA_MMU_MISS, MDB_TGT_SPEC_INTERNAL,
5100Sstevel@tonic-gate 	    no_se_f, NULL);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * Traps which will be handled elsewhere, and which therefore don't
5140Sstevel@tonic-gate 	 * need the trap-based message.
5150Sstevel@tonic-gate 	 */
5160Sstevel@tonic-gate 	BT_SET(kmt->kmt_trapmap, ST_KMDB_TRAP|T_SOFTWARE_TRAP);
5170Sstevel@tonic-gate 	BT_SET(kmt->kmt_trapmap, ST_KMDB_BREAKPOINT|T_SOFTWARE_TRAP);
5180Sstevel@tonic-gate 	BT_SET(kmt->kmt_trapmap, T_PA_WATCHPOINT);
5190Sstevel@tonic-gate 	BT_SET(kmt->kmt_trapmap, T_VA_WATCHPOINT);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/* Catch-all for traps not explicitly listed here */
5220Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, KMT_TRAP_NOTENUM, MDB_TGT_SPEC_INTERNAL,
5230Sstevel@tonic-gate 	    no_se_f, NULL);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*ARGSUSED*/
5270Sstevel@tonic-gate void
kmt_startup_isadep(mdb_tgt_t * t)5280Sstevel@tonic-gate kmt_startup_isadep(mdb_tgt_t *t)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate }
531