xref: /titanic_41/usr/src/cmd/mdb/sparc/kmdb/kvm_isadep.c (revision acbc304de7414b7021bc969ccc9274ce478580b0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*acbc304dSjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * isa-dependent portions of the kmdb target
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <mdb/mdb_kreg_impl.h>
347c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
357c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
367c478bd9Sstevel@tonic-gate #include <mdb/mdb_v9util.h>
377c478bd9Sstevel@tonic-gate #include <mdb/mdb_target_impl.h>
387c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
397c478bd9Sstevel@tonic-gate #include <mdb/mdb_umem.h>
407c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
417c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_dpi.h>
427c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_promif.h>
437c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_asmutil.h>
447c478bd9Sstevel@tonic-gate #include <kmdb/kvm.h>
457c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/stack.h>
497c478bd9Sstevel@tonic-gate #include <sys/regset.h>
507c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
517c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
527c478bd9Sstevel@tonic-gate #include <sys/machtrap.h>
537c478bd9Sstevel@tonic-gate #include <sys/trap.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* Higher than the highest trap number for which we have a specific specifier */
567c478bd9Sstevel@tonic-gate #define	KMT_MAXTRAPNO	0x1ff
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	OP(x)		((x) >> 30)
597c478bd9Sstevel@tonic-gate #define	OP3(x)		(((x) >> 19) & 0x3f)
607c478bd9Sstevel@tonic-gate #define	RD(x)		(((x) >> 25) & 0x1f)
617c478bd9Sstevel@tonic-gate #define	RS1(x)		(((x) >> 14) & 0x1f)
627c478bd9Sstevel@tonic-gate #define	RS2(x)		((x) & 0x1f)
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	OP_ARITH	0x2
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define	OP3_OR		0x02
677c478bd9Sstevel@tonic-gate #define	OP3_SAVE	0x3c
687c478bd9Sstevel@tonic-gate #define	OP3_RESTORE	0x3d
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@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)717c478bd9Sstevel@tonic-gate kmt_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
72*acbc304dSjohnlev     mdb_tgt_stack_f *func, void *arg, int cpuid)
737c478bd9Sstevel@tonic-gate {
74*acbc304dSjohnlev 	const mdb_tgt_gregset_t *grp;
757c478bd9Sstevel@tonic-gate 	mdb_tgt_gregset_t gregs;
767c478bd9Sstevel@tonic-gate 	kreg_t *kregs = &gregs.kregs[0];
777c478bd9Sstevel@tonic-gate 	long nwin, stopwin, canrestore, wp, i, sp;
787c478bd9Sstevel@tonic-gate 	long argv[6];
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * If gsp isn't null, we were asked to dump a trace from a
827c478bd9Sstevel@tonic-gate 	 * specific location.  The normal iterator can handle that.
837c478bd9Sstevel@tonic-gate 	 */
847c478bd9Sstevel@tonic-gate 	if (gsp != NULL) {
85*acbc304dSjohnlev 		if (cpuid != DPI_MASTER_CPUID)
867c478bd9Sstevel@tonic-gate 			warn("register set provided - ignoring cpu argument\n");
877c478bd9Sstevel@tonic-gate 		return (mdb_kvm_v9stack_iter(t, gsp, func, arg));
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
90*acbc304dSjohnlev 	if (kmdb_dpi_get_cpu_state(cpuid) < 0) {
91*acbc304dSjohnlev 		warn("failed to iterate through stack for cpu %u", cpuid);
927c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/*
967c478bd9Sstevel@tonic-gate 	 * We're being asked to dump the trace for the current CPU.
977c478bd9Sstevel@tonic-gate 	 * To do that, we need to iterate first through the saved
987c478bd9Sstevel@tonic-gate 	 * register windors.  If there's more to the trace than that,
997c478bd9Sstevel@tonic-gate 	 * we'll hand off to the normal iterator.
1007c478bd9Sstevel@tonic-gate 	 */
101*acbc304dSjohnlev 	if ((grp = kmdb_dpi_get_gregs(cpuid)) == NULL) {
102*acbc304dSjohnlev 		warn("failed to retrieve registers for cpu %d", cpuid);
103*acbc304dSjohnlev 		return (DCMD_ERR);
104*acbc304dSjohnlev 	}
105*acbc304dSjohnlev 
106*acbc304dSjohnlev 	bcopy(grp, &gregs, sizeof (mdb_tgt_gregset_t));
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	wp = kregs[KREG_CWP];
1097c478bd9Sstevel@tonic-gate 	canrestore = kregs[KREG_CANRESTORE];
110*acbc304dSjohnlev 	nwin = kmdb_dpi_get_nwin(cpuid);
1117c478bd9Sstevel@tonic-gate 	stopwin = ((wp + nwin) - canrestore - 1) % nwin;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_KMOD, "dumping cwp = %lu, canrestore = %lu, "
1147c478bd9Sstevel@tonic-gate 	    "stopwin = %lu\n", wp, canrestore, stopwin);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	for (;;) {
1177c478bd9Sstevel@tonic-gate 		struct rwindow rwin;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		for (i = 0; i < 6; i++)
1207c478bd9Sstevel@tonic-gate 			argv[i] = kregs[KREG_I0 + i];
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		if (kregs[KREG_PC] != 0 &&
1237c478bd9Sstevel@tonic-gate 		    func(arg, kregs[KREG_PC], 6, argv, &gregs) != 0)
1247c478bd9Sstevel@tonic-gate 			return (0);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 		kregs[KREG_PC] = kregs[KREG_I7];
1277c478bd9Sstevel@tonic-gate 		kregs[KREG_NPC] = kregs[KREG_PC] + 4;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		if ((sp = kregs[KREG_FP] + STACK_BIAS) == STACK_BIAS || sp == 0)
1307c478bd9Sstevel@tonic-gate 			return (0); /* Stop if we're at the end of stack */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		if (sp & (STACK_ALIGN - 1))
1337c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_STKALIGN));
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		wp = (wp + nwin - 1) % nwin;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		if (wp == stopwin)
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 		bcopy(&kregs[KREG_I0], &kregs[KREG_O0], 8 * sizeof (kreg_t));
1417c478bd9Sstevel@tonic-gate 
142*acbc304dSjohnlev 		if (kmdb_dpi_get_rwin(cpuid, wp, &rwin) < 0) {
1437c478bd9Sstevel@tonic-gate 			warn("unable to get registers from window %ld\n", wp);
1447c478bd9Sstevel@tonic-gate 			return (-1);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		for (i = 0; i < 8; i++)
1487c478bd9Sstevel@tonic-gate 			kregs[KREG_L0 + i] = (uintptr_t)rwin.rw_local[i];
1497c478bd9Sstevel@tonic-gate 		for (i = 0; i < 8; i++)
1507c478bd9Sstevel@tonic-gate 			kregs[KREG_I0 + i] = (uintptr_t)rwin.rw_in[i];
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_KMOD, "dumping wp %ld and beyond normally\n", wp);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * hack - if we null out pc here, iterator won't print the frame
1577c478bd9Sstevel@tonic-gate 	 * that corresponds to the current set of registers.  That's what we
1587c478bd9Sstevel@tonic-gate 	 * want because we just printed them above.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	kregs[KREG_PC] = 0;
1617c478bd9Sstevel@tonic-gate 	return (mdb_kvm_v9stack_iter(t, &gregs, func, arg));
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate void
kmt_printregs(const mdb_tgt_gregset_t * gregs)1657c478bd9Sstevel@tonic-gate kmt_printregs(const mdb_tgt_gregset_t *gregs)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	mdb_v9printregs(gregs);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@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)1717c478bd9Sstevel@tonic-gate kmt_stack_common(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
1727c478bd9Sstevel@tonic-gate     int cpuid, mdb_tgt_stack_f *func, kreg_t saved_pc)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	mdb_tgt_gregset_t *grp = NULL;
1757c478bd9Sstevel@tonic-gate 	mdb_tgt_gregset_t gregs;
17689518a1cSdmick 	void *arg = (void *)(uintptr_t)mdb.m_nargs;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
1797c478bd9Sstevel@tonic-gate 		bzero(&gregs, sizeof (gregs));
1807c478bd9Sstevel@tonic-gate 		gregs.kregs[KREG_FP] = addr;
1817c478bd9Sstevel@tonic-gate 		gregs.kregs[KREG_I7] = saved_pc;
1827c478bd9Sstevel@tonic-gate 		grp = &gregs;
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (argc != 0) {
1867c478bd9Sstevel@tonic-gate 		if (argv->a_type == MDB_TYPE_CHAR || argc > 1)
1877c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (argv->a_type == MDB_TYPE_STRING)
19089518a1cSdmick 			arg = (void *)(uintptr_t)(uint_t)
19189518a1cSdmick 			    mdb_strtoull(argv->a_un.a_str);
1927c478bd9Sstevel@tonic-gate 		else
19389518a1cSdmick 			arg = (void *)(uintptr_t)(uint_t)argv->a_un.a_val;
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	(void) kmt_stack_iter(mdb.m_target, grp, func, arg, cpuid);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate int
kmt_cpustack(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv,int cpuid,int verbose)2027c478bd9Sstevel@tonic-gate kmt_cpustack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv,
2037c478bd9Sstevel@tonic-gate     int cpuid, int verbose)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	return (kmt_stack_common(addr, flags, argc, argv, cpuid,
2067c478bd9Sstevel@tonic-gate 	    (verbose ? mdb_kvm_v9framev : mdb_kvm_v9frame), 0));
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate int
kmt_stack(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2107c478bd9Sstevel@tonic-gate kmt_stack(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
2137c478bd9Sstevel@tonic-gate 	    mdb_kvm_v9frame, 0));
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate int
kmt_stackv(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2177c478bd9Sstevel@tonic-gate kmt_stackv(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
2207c478bd9Sstevel@tonic-gate 	    mdb_kvm_v9framev, 0));
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate int
kmt_stackr(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2247c478bd9Sstevel@tonic-gate kmt_stackr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Force printing of the first register window by setting the saved
2287c478bd9Sstevel@tonic-gate 	 * pc (%i7) to PC_FAKE.
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	return (kmt_stack_common(addr, flags, argc, argv, DPI_MASTER_CPUID,
2317c478bd9Sstevel@tonic-gate 	    mdb_kvm_v9framer, PC_FAKE));
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate ssize_t
kmt_write_page(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)2357c478bd9Sstevel@tonic-gate kmt_write_page(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	jmp_buf *oldpcb = NULL;
2387c478bd9Sstevel@tonic-gate 	jmp_buf pcb;
2397c478bd9Sstevel@tonic-gate 	physaddr_t pa;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * Can we write to this page?
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 	if (!(t->t_flags & MDB_TGT_F_ALLOWIO) &&
2457c478bd9Sstevel@tonic-gate 	    (nbytes = kmdb_kdi_range_is_nontoxic(addr, nbytes, 1)) == 0)
2467c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_NOMAP));
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*
2497c478bd9Sstevel@tonic-gate 	 * The OBP va>pa call returns a protection value that's right only some
2507c478bd9Sstevel@tonic-gate 	 * of the time.  We can, however, tell if we failed a write due to a
2517c478bd9Sstevel@tonic-gate 	 * protection violation.  If we get such an error, we'll retry the
2527c478bd9Sstevel@tonic-gate 	 * write using pwrite.
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate 	if (setjmp(pcb) != 0) {
2557c478bd9Sstevel@tonic-gate 		/* We failed the write */
2567c478bd9Sstevel@tonic-gate 		kmdb_dpi_restore_fault_hdlr(oldpcb);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		if (errno == EACCES && kmdb_prom_vtop(addr, &pa) == 0)
2597c478bd9Sstevel@tonic-gate 			return (kmt_pwrite(t, buf, nbytes, pa));
2607c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_KMOD, "copying %lu bytes from %p to %p\n", nbytes,
2647c478bd9Sstevel@tonic-gate 	    buf, (void *)addr);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	oldpcb = kmdb_dpi_set_fault_hdlr(&pcb);
2677c478bd9Sstevel@tonic-gate 	(void) kmt_writer((void *)buf, nbytes, addr);
2687c478bd9Sstevel@tonic-gate 	kmdb_dpi_restore_fault_hdlr(oldpcb);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	return (nbytes);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2747c478bd9Sstevel@tonic-gate ssize_t
kmt_write(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)2757c478bd9Sstevel@tonic-gate kmt_write(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	size_t ntowrite, nwritten, n;
2787c478bd9Sstevel@tonic-gate 	int rc;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	kmdb_prom_check_interrupt();
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (nbytes == 0)
2837c478bd9Sstevel@tonic-gate 		return (0);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Break the writes up into page-sized chunks.  First, the leading page
2877c478bd9Sstevel@tonic-gate 	 * fragment (if any), then the subsequent pages.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if ((n = (addr & (mdb.m_pagesize - 1))) != 0) {
2917c478bd9Sstevel@tonic-gate 		ntowrite = MIN(mdb.m_pagesize - n, nbytes);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		if ((rc = kmt_write_page(t, buf, ntowrite, addr)) != ntowrite)
2947c478bd9Sstevel@tonic-gate 			return (rc);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		addr = roundup(addr, mdb.m_pagesize);
2977c478bd9Sstevel@tonic-gate 		nbytes -= ntowrite;
2987c478bd9Sstevel@tonic-gate 		nwritten = ntowrite;
2997c478bd9Sstevel@tonic-gate 		buf = ((caddr_t)buf + ntowrite);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	while (nbytes > 0) {
3037c478bd9Sstevel@tonic-gate 		ntowrite = MIN(mdb.m_pagesize, nbytes);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		if ((rc = kmt_write_page(t, buf, ntowrite, addr)) != ntowrite)
3067c478bd9Sstevel@tonic-gate 			return (rc < 0 ? rc : rc + nwritten);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		addr += mdb.m_pagesize;
3097c478bd9Sstevel@tonic-gate 		nbytes -= ntowrite;
3107c478bd9Sstevel@tonic-gate 		nwritten += ntowrite;
3117c478bd9Sstevel@tonic-gate 		buf = ((caddr_t)buf + ntowrite);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	return (rc);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3187c478bd9Sstevel@tonic-gate ssize_t
kmt_ioread(mdb_tgt_t * t,void * buf,size_t nbytes,uintptr_t addr)3197c478bd9Sstevel@tonic-gate kmt_ioread(mdb_tgt_t *t, void *buf, size_t nbytes, uintptr_t addr)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_TGTHWNOTSUP));
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3257c478bd9Sstevel@tonic-gate ssize_t
kmt_iowrite(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)3267c478bd9Sstevel@tonic-gate kmt_iowrite(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_TGTHWNOTSUP));
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate const char *
kmt_def_dismode(void)3327c478bd9Sstevel@tonic-gate kmt_def_dismode(void)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate #ifdef __sparcv9
3357c478bd9Sstevel@tonic-gate 	return ("v9plus");
3367c478bd9Sstevel@tonic-gate #else
3377c478bd9Sstevel@tonic-gate 	return ("v8");
3387c478bd9Sstevel@tonic-gate #endif
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * If we are stopped on a save instruction or at the first instruction of a
3437c478bd9Sstevel@tonic-gate  * known function, return %o7 as the step-out address; otherwise return the
3447c478bd9Sstevel@tonic-gate  * current frame's return address (%i7).  Significantly better handling of
3457c478bd9Sstevel@tonic-gate  * step out in leaf routines could be accomplished by implementing more
3467c478bd9Sstevel@tonic-gate  * complex decoding of the current function and our current state.
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate int
kmt_step_out(mdb_tgt_t * t,uintptr_t * p)3497c478bd9Sstevel@tonic-gate kmt_step_out(mdb_tgt_t *t, uintptr_t *p)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	kreg_t pc, i7, o7;
3527c478bd9Sstevel@tonic-gate 	GElf_Sym func;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("pc", &pc);
3557c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("i7", &i7);
3567c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("o7", &o7);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY, NULL, 0,
3597c478bd9Sstevel@tonic-gate 	    &func, NULL) == 0 && func.st_value == pc)
3607c478bd9Sstevel@tonic-gate 		*p = o7 + 2 * sizeof (mdb_instr_t);
3617c478bd9Sstevel@tonic-gate 	else {
3627c478bd9Sstevel@tonic-gate 		mdb_instr_t instr;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		if (mdb_tgt_vread(t, &instr, sizeof (instr), pc) !=
3657c478bd9Sstevel@tonic-gate 		    sizeof (instr)) {
3667c478bd9Sstevel@tonic-gate 			warn("failed to read instruction at %p for step out",
3677c478bd9Sstevel@tonic-gate 			    (void *)pc);
3687c478bd9Sstevel@tonic-gate 			return (-1);
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		if (OP(instr) == OP_ARITH && OP3(instr) == OP3_SAVE)
3727c478bd9Sstevel@tonic-gate 			*p = o7 + 2 * sizeof (mdb_instr_t);
3737c478bd9Sstevel@tonic-gate 		else
3747c478bd9Sstevel@tonic-gate 			*p = i7 + 2 * sizeof (mdb_instr_t);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	return (0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3817c478bd9Sstevel@tonic-gate int
kmt_step_branch(mdb_tgt_t * t)3827c478bd9Sstevel@tonic-gate kmt_step_branch(mdb_tgt_t *t)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_TGTHWNOTSUP));
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static const char *
regno2name(int idx)3887c478bd9Sstevel@tonic-gate regno2name(int idx)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	const mdb_tgt_regdesc_t *rd;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	for (rd = mdb_sparcv9_kregs; rd->rd_name != NULL; rd++) {
3937c478bd9Sstevel@tonic-gate 		if (idx == rd->rd_num)
3947c478bd9Sstevel@tonic-gate 			return (rd->rd_name);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	ASSERT(rd->rd_name != NULL);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	return ("unknown");
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Step over call and jmpl by returning the address of the position where a
4047c478bd9Sstevel@tonic-gate  * temporary breakpoint can be set to catch return from the control transfer.
4057c478bd9Sstevel@tonic-gate  * This function does not currently provide advanced decoding of DCTI couples
4067c478bd9Sstevel@tonic-gate  * or any other complex special case; we just fall back to single-step.
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate int
kmt_next(mdb_tgt_t * t,uintptr_t * p)4097c478bd9Sstevel@tonic-gate kmt_next(mdb_tgt_t *t, uintptr_t *p)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	kreg_t pc, npc;
4127c478bd9Sstevel@tonic-gate 	GElf_Sym func;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("pc", &pc);
4157c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("npc", &npc);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (mdb_tgt_lookup_by_addr(t, pc, MDB_TGT_SYM_FUZZY, NULL, 0,
4187c478bd9Sstevel@tonic-gate 	    &func, NULL) != 0)
4197c478bd9Sstevel@tonic-gate 		return (-1);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if (npc < func.st_value || func.st_value + func.st_size <= npc) {
4227c478bd9Sstevel@tonic-gate 		mdb_instr_t instr;
4237c478bd9Sstevel@tonic-gate 		kreg_t reg;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 		/*
4267c478bd9Sstevel@tonic-gate 		 * We're about to transfer control outside this function, so we
4277c478bd9Sstevel@tonic-gate 		 * want to stop when control returns from the other function.
4287c478bd9Sstevel@tonic-gate 		 * Normally the return address will be in %o7, tail-calls being
4297c478bd9Sstevel@tonic-gate 		 * the exception.  We try to discover if this is a tail-call and
4307c478bd9Sstevel@tonic-gate 		 * compute the return address in that case.
4317c478bd9Sstevel@tonic-gate 		 */
4327c478bd9Sstevel@tonic-gate 		if (mdb_tgt_vread(t, &instr, sizeof (instr), pc) !=
4337c478bd9Sstevel@tonic-gate 		    sizeof (instr)) {
4347c478bd9Sstevel@tonic-gate 			warn("failed to read instruction at %p for next",
4357c478bd9Sstevel@tonic-gate 			    (void *)pc);
4367c478bd9Sstevel@tonic-gate 			return (-1);
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		if (OP(instr) == OP_ARITH && OP3(instr) == OP3_RESTORE) {
4407c478bd9Sstevel@tonic-gate 			(void) kmdb_dpi_get_register("i7", &reg);
4417c478bd9Sstevel@tonic-gate 		} else if (OP(instr) == OP_ARITH && OP3(instr) == OP3_OR &&
4427c478bd9Sstevel@tonic-gate 		    RD(instr) == KREG_O7) {
4437c478bd9Sstevel@tonic-gate 			if (RS1(instr) == KREG_G0)
4447c478bd9Sstevel@tonic-gate 				return (set_errno(EAGAIN));
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 			(void) kmdb_dpi_get_register(regno2name(RS2(instr)),
4477c478bd9Sstevel@tonic-gate 			    &reg);
4487c478bd9Sstevel@tonic-gate 		} else
4497c478bd9Sstevel@tonic-gate 			(void) kmdb_dpi_get_register("o7", &reg);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		*p = reg + 2 * sizeof (mdb_instr_t);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		return (0);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (set_errno(EAGAIN));
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate const char *
kmt_trapname(int trapnum)4607c478bd9Sstevel@tonic-gate kmt_trapname(int trapnum)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	static char trapname[11];
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	switch (trapnum) {
4657c478bd9Sstevel@tonic-gate 	case T_INSTR_EXCEPTION:
4667c478bd9Sstevel@tonic-gate 		return ("instruction access error trap");
4677c478bd9Sstevel@tonic-gate 	case T_ALIGNMENT:
4687c478bd9Sstevel@tonic-gate 		return ("improper alignment trap");
4697c478bd9Sstevel@tonic-gate 	case T_UNIMP_INSTR:
4707c478bd9Sstevel@tonic-gate 		return ("illegal instruction trap");
4717c478bd9Sstevel@tonic-gate 	case T_IDIV0:
4727c478bd9Sstevel@tonic-gate 		return ("division by zero trap");
4737c478bd9Sstevel@tonic-gate 	case T_FAST_INSTR_MMU_MISS:
4747c478bd9Sstevel@tonic-gate 		return ("instruction access MMU miss trap");
4757c478bd9Sstevel@tonic-gate 	case T_FAST_DATA_MMU_MISS:
4767c478bd9Sstevel@tonic-gate 		return ("data access MMU miss trap");
4777c478bd9Sstevel@tonic-gate 	case ST_KMDB_TRAP|T_SOFTWARE_TRAP:
4787c478bd9Sstevel@tonic-gate 		return ("debugger entry trap");
4797c478bd9Sstevel@tonic-gate 	case ST_KMDB_BREAKPOINT|T_SOFTWARE_TRAP:
4807c478bd9Sstevel@tonic-gate 		return ("breakpoint trap");
4817c478bd9Sstevel@tonic-gate 	default:
4827c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(trapname, sizeof (trapname), "trap %#x",
4837c478bd9Sstevel@tonic-gate 		    trapnum);
4847c478bd9Sstevel@tonic-gate 		return (trapname);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate void
kmt_init_isadep(mdb_tgt_t * t)4897c478bd9Sstevel@tonic-gate kmt_init_isadep(mdb_tgt_t *t)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	kmt_data_t *kmt = t->t_data;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	kmt->kmt_rds = mdb_sparcv9_kregs;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	kmt->kmt_trapmax = KMT_MAXTRAPNO;
4967c478bd9Sstevel@tonic-gate 	kmt->kmt_trapmap = mdb_zalloc(BT_SIZEOFMAP(kmt->kmt_trapmax), UM_SLEEP);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/* Traps for which we want to provide an explicit message */
4997c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_INSTR_EXCEPTION, MDB_TGT_SPEC_INTERNAL,
5007c478bd9Sstevel@tonic-gate 	    no_se_f, NULL);
5017c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_ALIGNMENT, MDB_TGT_SPEC_INTERNAL,
5027c478bd9Sstevel@tonic-gate 	    no_se_f, NULL);
5037c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_UNIMP_INSTR, MDB_TGT_SPEC_INTERNAL,
5047c478bd9Sstevel@tonic-gate 	    no_se_f, NULL);
5057c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_IDIV0, MDB_TGT_SPEC_INTERNAL,
5067c478bd9Sstevel@tonic-gate 	    no_se_f, NULL);
5077c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_FAST_INSTR_MMU_MISS,
5087c478bd9Sstevel@tonic-gate 	    MDB_TGT_SPEC_INTERNAL, no_se_f, NULL);
5097c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, T_FAST_DATA_MMU_MISS, MDB_TGT_SPEC_INTERNAL,
5107c478bd9Sstevel@tonic-gate 	    no_se_f, NULL);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Traps which will be handled elsewhere, and which therefore don't
5147c478bd9Sstevel@tonic-gate 	 * need the trap-based message.
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	BT_SET(kmt->kmt_trapmap, ST_KMDB_TRAP|T_SOFTWARE_TRAP);
5177c478bd9Sstevel@tonic-gate 	BT_SET(kmt->kmt_trapmap, ST_KMDB_BREAKPOINT|T_SOFTWARE_TRAP);
5187c478bd9Sstevel@tonic-gate 	BT_SET(kmt->kmt_trapmap, T_PA_WATCHPOINT);
5197c478bd9Sstevel@tonic-gate 	BT_SET(kmt->kmt_trapmap, T_VA_WATCHPOINT);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* Catch-all for traps not explicitly listed here */
5227c478bd9Sstevel@tonic-gate 	(void) mdb_tgt_add_fault(t, KMT_TRAP_NOTENUM, MDB_TGT_SPEC_INTERNAL,
5237c478bd9Sstevel@tonic-gate 	    no_se_f, NULL);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5277c478bd9Sstevel@tonic-gate void
kmt_startup_isadep(mdb_tgt_t * t)5287c478bd9Sstevel@tonic-gate kmt_startup_isadep(mdb_tgt_t *t)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate }
531