xref: /illumos-gate/usr/src/cmd/mdb/intel/kmdb/kaif.c (revision 399ca3a7ff315244c51a7bbd1d3ce2709ef6c7c6)
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
5c7bf3205Sjohnlev  * Common Development and Distribution License (the "License").
6c7bf3205Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*399ca3a7SJohn Levon  *
25*399ca3a7SJohn Levon  * Copyright 2018 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * The debugger/"PROM" interface layer
307c478bd9Sstevel@tonic-gate  *
31ae115bc7Smrj  * It makes more sense on SPARC. In reality, these interfaces deal with three
32ae115bc7Smrj  * things: setting break/watchpoints, stepping, and interfacing with the KDI to
33ae115bc7Smrj  * set up kmdb's IDT handlers.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_dpi_impl.h>
377c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
387c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_umemglue.h>
397c478bd9Sstevel@tonic-gate #include <kmdb/kaif.h>
40c7bf3205Sjohnlev #include <kmdb/kmdb_io.h>
41ae115bc7Smrj #include <kmdb/kaif_start.h>
427c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
437c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
447c478bd9Sstevel@tonic-gate #include <mdb/mdb_isautil.h>
457c478bd9Sstevel@tonic-gate #include <mdb/mdb_io_impl.h>
46ae115bc7Smrj #include <mdb/mdb_kreg_impl.h>
477c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
51c7bf3205Sjohnlev #include <sys/termios.h>
52ae115bc7Smrj #include <sys/kdi_impl.h>
537c478bd9Sstevel@tonic-gate 
54ae115bc7Smrj /*
55ae115bc7Smrj  * This is the area containing the saved state when we enter
56ae115bc7Smrj  * via kmdb's IDT entries.
57ae115bc7Smrj  */
58ae115bc7Smrj kdi_cpusave_t	*kaif_cpusave;
597c478bd9Sstevel@tonic-gate int		kaif_ncpusave;
60ae115bc7Smrj kdi_drreg_t	kaif_drreg;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate uint32_t	kaif_waptmap;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate int		kaif_trap_switch;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate void (*kaif_modchg_cb)(struct modctl *, int);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate enum {
697c478bd9Sstevel@tonic-gate 	M_SYSRET	= 0x07, /* after M_ESC */
707c478bd9Sstevel@tonic-gate 	M_ESC		= 0x0f,
717c478bd9Sstevel@tonic-gate 	M_SYSEXIT	= 0x35, /* after M_ESC */
727c478bd9Sstevel@tonic-gate 	M_REX_LO	= 0x40, /* first REX prefix */
737c478bd9Sstevel@tonic-gate 	M_REX_HI	= 0x4f, /* last REX prefix */
747c478bd9Sstevel@tonic-gate 	M_PUSHF		= 0x9c,	/* pushfl and pushfq */
757c478bd9Sstevel@tonic-gate 	M_POPF		= 0x9d,	/* popfl and popfq */
767c478bd9Sstevel@tonic-gate 	M_INT3		= 0xcc,
777c478bd9Sstevel@tonic-gate 	M_INTX		= 0xcd,
787c478bd9Sstevel@tonic-gate 	M_INTO		= 0xce,
797c478bd9Sstevel@tonic-gate 	M_IRET		= 0xcf,
807c478bd9Sstevel@tonic-gate 	M_CLI		= 0xfa,
817c478bd9Sstevel@tonic-gate 	M_STI		= 0xfb
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	KAIF_BREAKPOINT_INSTR	M_INT3
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	KAIF_WPPRIV2ID(wp)	(int)(uintptr_t)((wp)->wp_priv)
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef __amd64
897c478bd9Sstevel@tonic-gate #define	FLAGS_REG_NAME		"rflags"
907c478bd9Sstevel@tonic-gate #else
917c478bd9Sstevel@tonic-gate #define	FLAGS_REG_NAME		"eflags"
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Called during normal debugger operation and during debugger faults.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate static void
987c478bd9Sstevel@tonic-gate kaif_enter_mon(void)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	char c;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	for (;;) {
1037c478bd9Sstevel@tonic-gate 		mdb_iob_printf(mdb.m_out,
1047c478bd9Sstevel@tonic-gate 		    "%s: Do you really want to reboot? (y/n) ",
1057c478bd9Sstevel@tonic-gate 		    mdb.m_pname);
1067c478bd9Sstevel@tonic-gate 		mdb_iob_flush(mdb.m_out);
107c7bf3205Sjohnlev 		mdb_iob_clearlines(mdb.m_out);
1087c478bd9Sstevel@tonic-gate 
109c7bf3205Sjohnlev 		c = kmdb_getchar();
1107c478bd9Sstevel@tonic-gate 
111c7bf3205Sjohnlev 		if (c == 'n' || c == 'N' || c == CTRL('c'))
1127c478bd9Sstevel@tonic-gate 			return;
1137c478bd9Sstevel@tonic-gate 		else if (c == 'y' || c == 'Y') {
1147c478bd9Sstevel@tonic-gate 			mdb_iob_printf(mdb.m_out, "Rebooting...\n");
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 			kmdb_dpi_reboot();
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
121acbc304dSjohnlev static kaif_cpusave_t *
122acbc304dSjohnlev kaif_cpuid2save(int cpuid)
123acbc304dSjohnlev {
124acbc304dSjohnlev 	kaif_cpusave_t *save;
125acbc304dSjohnlev 
126acbc304dSjohnlev 	if (cpuid == DPI_MASTER_CPUID)
127acbc304dSjohnlev 		return (&kaif_cpusave[kaif_master_cpuid]);
128acbc304dSjohnlev 
129acbc304dSjohnlev 	if (cpuid < 0 || cpuid >= kaif_ncpusave) {
130acbc304dSjohnlev 		(void) set_errno(EINVAL);
131acbc304dSjohnlev 		return (NULL);
132acbc304dSjohnlev 	}
133acbc304dSjohnlev 
134acbc304dSjohnlev 	save = &kaif_cpusave[cpuid];
135acbc304dSjohnlev 
136acbc304dSjohnlev 	if (save->krs_cpu_state != KAIF_CPU_STATE_MASTER &&
137acbc304dSjohnlev 	    save->krs_cpu_state != KAIF_CPU_STATE_SLAVE) {
138acbc304dSjohnlev 		(void) set_errno(EINVAL);
139acbc304dSjohnlev 		return (NULL);
140acbc304dSjohnlev 	}
141acbc304dSjohnlev 
142acbc304dSjohnlev 	return (save);
143acbc304dSjohnlev }
144acbc304dSjohnlev 
1457c478bd9Sstevel@tonic-gate static int
1467c478bd9Sstevel@tonic-gate kaif_get_cpu_state(int cpuid)
1477c478bd9Sstevel@tonic-gate {
148acbc304dSjohnlev 	kaif_cpusave_t *save;
1497c478bd9Sstevel@tonic-gate 
150acbc304dSjohnlev 	if ((save = kaif_cpuid2save(cpuid)) == NULL)
151acbc304dSjohnlev 		return (-1); /* errno is set for us */
1527c478bd9Sstevel@tonic-gate 
153acbc304dSjohnlev 	switch (save->krs_cpu_state) {
1547c478bd9Sstevel@tonic-gate 	case KAIF_CPU_STATE_MASTER:
1557c478bd9Sstevel@tonic-gate 		return (DPI_CPU_STATE_MASTER);
1567c478bd9Sstevel@tonic-gate 	case KAIF_CPU_STATE_SLAVE:
1577c478bd9Sstevel@tonic-gate 		return (DPI_CPU_STATE_SLAVE);
1587c478bd9Sstevel@tonic-gate 	default:
1597c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static int
1647c478bd9Sstevel@tonic-gate kaif_get_master_cpuid(void)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	return (kaif_master_cpuid);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
169ae115bc7Smrj static mdb_tgt_gregset_t *
170ae115bc7Smrj kaif_kdi_to_gregs(int cpuid)
1717c478bd9Sstevel@tonic-gate {
172acbc304dSjohnlev 	kaif_cpusave_t *save;
1737c478bd9Sstevel@tonic-gate 
174acbc304dSjohnlev 	if ((save = kaif_cpuid2save(cpuid)) == NULL)
175acbc304dSjohnlev 		return (NULL); /* errno is set for us */
1767c478bd9Sstevel@tonic-gate 
177ae115bc7Smrj 	/*
178ae115bc7Smrj 	 * The saved registers are actually identical to an mdb_tgt_gregset,
179ae115bc7Smrj 	 * so we can directly cast here.
180ae115bc7Smrj 	 */
181ae115bc7Smrj 	return ((mdb_tgt_gregset_t *)save->krs_gregs);
182ae115bc7Smrj }
183ae115bc7Smrj 
184ae115bc7Smrj static const mdb_tgt_gregset_t *
185ae115bc7Smrj kaif_get_gregs(int cpuid)
186ae115bc7Smrj {
187ae115bc7Smrj 	return (kaif_kdi_to_gregs(cpuid));
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate typedef struct kaif_reg_synonyms {
1917c478bd9Sstevel@tonic-gate 	const char *rs_syn;
1927c478bd9Sstevel@tonic-gate 	const char *rs_name;
1937c478bd9Sstevel@tonic-gate } kaif_reg_synonyms_t;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static kreg_t *
196acbc304dSjohnlev kaif_find_regp(const char *regname)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	static const kaif_reg_synonyms_t synonyms[] = {
1997c478bd9Sstevel@tonic-gate #ifdef __amd64
2007c478bd9Sstevel@tonic-gate 	    { "pc", "rip" },
2017c478bd9Sstevel@tonic-gate 	    { "sp", "rsp" },
2027c478bd9Sstevel@tonic-gate 	    { "fp", "rbp" },
2037c478bd9Sstevel@tonic-gate #else
2047c478bd9Sstevel@tonic-gate 	    { "pc", "eip" },
2057c478bd9Sstevel@tonic-gate 	    { "sp", "esp" },
2067c478bd9Sstevel@tonic-gate 	    { "fp", "ebp" },
2077c478bd9Sstevel@tonic-gate #endif
2087c478bd9Sstevel@tonic-gate 	    { "tt", "trapno" }
2097c478bd9Sstevel@tonic-gate 	};
210ae115bc7Smrj 	mdb_tgt_gregset_t *regs;
2117c478bd9Sstevel@tonic-gate 	int i;
2127c478bd9Sstevel@tonic-gate 
213ae115bc7Smrj 	if ((regs = kaif_kdi_to_gregs(DPI_MASTER_CPUID)) == NULL)
214ae115bc7Smrj 		return (NULL);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (synonyms) / sizeof (synonyms[0]); i++) {
2177c478bd9Sstevel@tonic-gate 		if (strcmp(synonyms[i].rs_syn, regname) == 0)
2187c478bd9Sstevel@tonic-gate 			regname = synonyms[i].rs_name;
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	for (i = 0; mdb_isa_kregs[i].rd_name != NULL; i++) {
2227c478bd9Sstevel@tonic-gate 		const mdb_tgt_regdesc_t *rd = &mdb_isa_kregs[i];
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		if (strcmp(rd->rd_name, regname) == 0)
225ae115bc7Smrj 			return (&regs->kregs[rd->rd_num]);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	(void) set_errno(ENOENT);
2297c478bd9Sstevel@tonic-gate 	return (NULL);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2337c478bd9Sstevel@tonic-gate static int
234acbc304dSjohnlev kaif_get_register(const char *regname, kreg_t *valp)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	kreg_t *regp;
2377c478bd9Sstevel@tonic-gate 
238acbc304dSjohnlev 	if ((regp = kaif_find_regp(regname)) == NULL)
2397c478bd9Sstevel@tonic-gate 		return (-1);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	*valp = *regp;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return (0);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static int
247acbc304dSjohnlev kaif_set_register(const char *regname, kreg_t val)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	kreg_t *regp;
2507c478bd9Sstevel@tonic-gate 
251acbc304dSjohnlev 	if ((regp = kaif_find_regp(regname)) == NULL)
2527c478bd9Sstevel@tonic-gate 		return (-1);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	*regp = val;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	return (0);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static int
2607c478bd9Sstevel@tonic-gate kaif_brkpt_arm(uintptr_t addr, mdb_instr_t *instrp)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	mdb_instr_t bkpt = KAIF_BREAKPOINT_INSTR;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (mdb_tgt_vread(mdb.m_target, instrp, sizeof (mdb_instr_t), addr) !=
2657c478bd9Sstevel@tonic-gate 	    sizeof (mdb_instr_t))
2667c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (mdb_tgt_vwrite(mdb.m_target, &bkpt, sizeof (mdb_instr_t), addr) !=
2697c478bd9Sstevel@tonic-gate 	    sizeof (mdb_instr_t))
2707c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	return (0);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate static int
2767c478bd9Sstevel@tonic-gate kaif_brkpt_disarm(uintptr_t addr, mdb_instr_t instrp)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	if (mdb_tgt_vwrite(mdb.m_target, &instrp, sizeof (mdb_instr_t), addr) !=
2797c478bd9Sstevel@tonic-gate 	    sizeof (mdb_instr_t))
2807c478bd9Sstevel@tonic-gate 		return (-1); /* errno is set for us */
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	return (0);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * Intel watchpoints are even more fun than SPARC ones.  The Intel architecture
2877c478bd9Sstevel@tonic-gate  * manuals refer to watchpoints as breakpoints.  For consistency  with the
2887c478bd9Sstevel@tonic-gate  * terminology used in other portions of kmdb, we will, however, refer to them
2897c478bd9Sstevel@tonic-gate  * as watchpoints.
2907c478bd9Sstevel@tonic-gate  *
2917c478bd9Sstevel@tonic-gate  * Execute, data write, I/O read/write, and data read/write watchpoints are
2927c478bd9Sstevel@tonic-gate  * supported by the hardware.  Execute watchpoints must be one byte in length,
2937c478bd9Sstevel@tonic-gate  * and must be placed on the first byte of the instruction to be watched.
2947c478bd9Sstevel@tonic-gate  * Lengths of other watchpoints are more varied.
2957c478bd9Sstevel@tonic-gate  *
2967c478bd9Sstevel@tonic-gate  * Given that we already have a breakpoint facility, and given the restrictions
2977c478bd9Sstevel@tonic-gate  * placed on execute watchpoints, we're going to disallow the creation of
2987c478bd9Sstevel@tonic-gate  * execute watchpoints.  The others will be fully supported.  See the Debugging
2997c478bd9Sstevel@tonic-gate  * chapter in both the IA32 and AMD64 System Programming books for more details.
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate #ifdef __amd64
3037c478bd9Sstevel@tonic-gate #define	WAPT_DATA_MAX_SIZE	8
3047c478bd9Sstevel@tonic-gate #define	WAPT_DATA_SIZES_MSG	"1, 2, 4, or 8"
3057c478bd9Sstevel@tonic-gate #else
3067c478bd9Sstevel@tonic-gate #define	WAPT_DATA_MAX_SIZE	4
3077c478bd9Sstevel@tonic-gate #define	WAPT_DATA_SIZES_MSG	"1, 2, or 4"
3087c478bd9Sstevel@tonic-gate #endif
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate static int
3117c478bd9Sstevel@tonic-gate kaif_wapt_validate(kmdb_wapt_t *wp)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	if (wp->wp_type == DPI_WAPT_TYPE_IO) {
3147c478bd9Sstevel@tonic-gate 		if (wp->wp_wflags != (MDB_TGT_WA_R | MDB_TGT_WA_W)) {
3157c478bd9Sstevel@tonic-gate 			warn("I/O port watchpoints must be read/write\n");
3167c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		if (wp->wp_size != 1 && wp->wp_size != 2 && wp->wp_size != 4) {
3207c478bd9Sstevel@tonic-gate 			warn("I/O watchpoint size must be 1, 2, or 4 bytes\n");
3217c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	} else if (wp->wp_type == DPI_WAPT_TYPE_PHYS) {
3257c478bd9Sstevel@tonic-gate 		warn("physical address watchpoints are not supported on this "
3267c478bd9Sstevel@tonic-gate 		    "platform\n");
3277c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_TGTHWNOTSUP));
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	} else {
3307c478bd9Sstevel@tonic-gate 		if (wp->wp_wflags != (MDB_TGT_WA_R | MDB_TGT_WA_W) &&
3317c478bd9Sstevel@tonic-gate 		    wp->wp_wflags != MDB_TGT_WA_W) {
3327c478bd9Sstevel@tonic-gate 			warn("watchpoints must be read/write or write-only\n");
3337c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		if ((wp->wp_size & -(wp->wp_size)) != wp->wp_size ||
3377c478bd9Sstevel@tonic-gate 		    wp->wp_size > WAPT_DATA_MAX_SIZE) {
3387c478bd9Sstevel@tonic-gate 			warn("data watchpoint size must be " WAPT_DATA_SIZES_MSG
3397c478bd9Sstevel@tonic-gate 			    " bytes\n");
3407c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (wp->wp_addr & (wp->wp_size - 1)) {
3467c478bd9Sstevel@tonic-gate 		warn("%lu-byte watchpoints must be %lu-byte aligned\n",
3477c478bd9Sstevel@tonic-gate 		    (ulong_t)wp->wp_size, (ulong_t)wp->wp_size);
3487c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	return (0);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate static int
3557c478bd9Sstevel@tonic-gate kaif_wapt_reserve(kmdb_wapt_t *wp)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	int id;
3587c478bd9Sstevel@tonic-gate 
359ae115bc7Smrj 	for (id = 0; id <= KDI_MAXWPIDX; id++) {
3607c478bd9Sstevel@tonic-gate 		if (!BT_TEST(&kaif_waptmap, id)) {
3617c478bd9Sstevel@tonic-gate 			/* found one */
3627c478bd9Sstevel@tonic-gate 			BT_SET(&kaif_waptmap, id);
3637c478bd9Sstevel@tonic-gate 			wp->wp_priv = (void *)(uintptr_t)id;
3647c478bd9Sstevel@tonic-gate 			return (0);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	return (set_errno(EMDB_WPTOOMANY));
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static void
3727c478bd9Sstevel@tonic-gate kaif_wapt_release(kmdb_wapt_t *wp)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	int id = KAIF_WPPRIV2ID(wp);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	ASSERT(BT_TEST(&kaif_waptmap, id));
3777c478bd9Sstevel@tonic-gate 	BT_CLEAR(&kaif_waptmap, id);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3817c478bd9Sstevel@tonic-gate static void
3827c478bd9Sstevel@tonic-gate kaif_wapt_arm(kmdb_wapt_t *wp)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	uint_t rw;
3857c478bd9Sstevel@tonic-gate 	int hwid = KAIF_WPPRIV2ID(wp);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	ASSERT(BT_TEST(&kaif_waptmap, hwid));
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (wp->wp_type == DPI_WAPT_TYPE_IO)
3907c478bd9Sstevel@tonic-gate 		rw = KREG_DRCTL_WP_IORW;
3917c478bd9Sstevel@tonic-gate 	else if (wp->wp_wflags & MDB_TGT_WA_R)
3927c478bd9Sstevel@tonic-gate 		rw = KREG_DRCTL_WP_RW;
3937c478bd9Sstevel@tonic-gate 	else if (wp->wp_wflags & MDB_TGT_WA_X)
3947c478bd9Sstevel@tonic-gate 		rw = KREG_DRCTL_WP_EXEC;
3957c478bd9Sstevel@tonic-gate 	else
3967c478bd9Sstevel@tonic-gate 		rw = KREG_DRCTL_WP_WONLY;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	kaif_drreg.dr_addr[hwid] = wp->wp_addr;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	kaif_drreg.dr_ctl &= ~KREG_DRCTL_WP_MASK(hwid);
4017c478bd9Sstevel@tonic-gate 	kaif_drreg.dr_ctl |= KREG_DRCTL_WP_LENRW(hwid, wp->wp_size - 1, rw);
4027c478bd9Sstevel@tonic-gate 	kaif_drreg.dr_ctl |= KREG_DRCTL_WPEN(hwid);
403ae115bc7Smrj 	kmdb_kdi_update_drreg(&kaif_drreg);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4077c478bd9Sstevel@tonic-gate static void
4087c478bd9Sstevel@tonic-gate kaif_wapt_disarm(kmdb_wapt_t *wp)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	int hwid = KAIF_WPPRIV2ID(wp);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	ASSERT(BT_TEST(&kaif_waptmap, hwid));
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	kaif_drreg.dr_addr[hwid] = 0;
4157c478bd9Sstevel@tonic-gate 	kaif_drreg.dr_ctl &= ~(KREG_DRCTL_WP_MASK(hwid) |
4167c478bd9Sstevel@tonic-gate 	    KREG_DRCTL_WPEN_MASK(hwid));
417ae115bc7Smrj 	kmdb_kdi_update_drreg(&kaif_drreg);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4217c478bd9Sstevel@tonic-gate static int
4227c478bd9Sstevel@tonic-gate kaif_wapt_match(kmdb_wapt_t *wp)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	int hwid = KAIF_WPPRIV2ID(wp);
4257c478bd9Sstevel@tonic-gate 	uint32_t mask = KREG_DRSTAT_WP_MASK(hwid);
4267c478bd9Sstevel@tonic-gate 	int n = 0;
4277c478bd9Sstevel@tonic-gate 	int i;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	ASSERT(BT_TEST(&kaif_waptmap, hwid));
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	for (i = 0; i < kaif_ncpusave; i++)
4327c478bd9Sstevel@tonic-gate 		n += (kaif_cpusave[i].krs_dr.dr_stat & mask) != 0;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	return (n);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate static int
4387c478bd9Sstevel@tonic-gate kaif_step(void)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	kreg_t pc, fl, oldfl, newfl, sp;
4417c478bd9Sstevel@tonic-gate 	mdb_tgt_addr_t npc;
4427c478bd9Sstevel@tonic-gate 	mdb_instr_t instr;
4437c478bd9Sstevel@tonic-gate 	int emulated = 0, rchk = 0;
4447c478bd9Sstevel@tonic-gate 	size_t pcoff = 0;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_get_register("pc", &pc);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if ((npc = mdb_dis_nextins(mdb.m_disasm, mdb.m_target,
4497c478bd9Sstevel@tonic-gate 	    MDB_TGT_AS_VIRT, pc)) == pc) {
4507c478bd9Sstevel@tonic-gate 		warn("failed to decode instruction at %a for step\n", pc);
4517c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * Stepping behavior depends on the type of instruction.  It does not
4567c478bd9Sstevel@tonic-gate 	 * depend on the presence of a REX prefix, as the action we take for a
4577c478bd9Sstevel@tonic-gate 	 * given instruction doesn't currently vary for 32-bit instructions
4587c478bd9Sstevel@tonic-gate 	 * versus their 64-bit counterparts.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	do {
4617c478bd9Sstevel@tonic-gate 		if (mdb_tgt_vread(mdb.m_target, &instr, sizeof (mdb_instr_t),
4627c478bd9Sstevel@tonic-gate 		    pc + pcoff) != sizeof (mdb_instr_t)) {
4637c478bd9Sstevel@tonic-gate 			warn("failed to read at %p for step",
4647c478bd9Sstevel@tonic-gate 			    (void *)(pc + pcoff));
4657c478bd9Sstevel@tonic-gate 			return (-1);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 	} while (pcoff++, (instr >= M_REX_LO && instr <= M_REX_HI && !rchk++));
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	switch (instr) {
4707c478bd9Sstevel@tonic-gate 	case M_IRET:
4717c478bd9Sstevel@tonic-gate 		warn("iret cannot be stepped\n");
4727c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_TGTNOTSUP));
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	case M_INT3:
4757c478bd9Sstevel@tonic-gate 	case M_INTX:
4767c478bd9Sstevel@tonic-gate 	case M_INTO:
4777c478bd9Sstevel@tonic-gate 		warn("int cannot be stepped\n");
4787c478bd9Sstevel@tonic-gate 		return (set_errno(EMDB_TGTNOTSUP));
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	case M_ESC:
4817c478bd9Sstevel@tonic-gate 		if (mdb_tgt_vread(mdb.m_target, &instr, sizeof (mdb_instr_t),
4827c478bd9Sstevel@tonic-gate 		    pc + pcoff) != sizeof (mdb_instr_t)) {
4837c478bd9Sstevel@tonic-gate 			warn("failed to read at %p for step",
4847c478bd9Sstevel@tonic-gate 			    (void *)(pc + pcoff));
4857c478bd9Sstevel@tonic-gate 			return (-1);
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		switch (instr) {
4897c478bd9Sstevel@tonic-gate 		case M_SYSRET:
4907c478bd9Sstevel@tonic-gate 			warn("sysret cannot be stepped\n");
4917c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_TGTNOTSUP));
4927c478bd9Sstevel@tonic-gate 		case M_SYSEXIT:
4937c478bd9Sstevel@tonic-gate 			warn("sysexit cannot be stepped\n");
4947c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_TGTNOTSUP));
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 		break;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * Some instructions need to be emulated.  We need to prevent direct
5007c478bd9Sstevel@tonic-gate 	 * manipulations of EFLAGS, so we'll emulate cli, sti.  pushfl and
5017c478bd9Sstevel@tonic-gate 	 * popfl also receive special handling, as they manipulate both EFLAGS
5027c478bd9Sstevel@tonic-gate 	 * and %esp.
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 	case M_CLI:
5057c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
5067c478bd9Sstevel@tonic-gate 		fl &= ~KREG_EFLAGS_IF_MASK;
5077c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_set_register(FLAGS_REG_NAME, fl);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		emulated = 1;
5107c478bd9Sstevel@tonic-gate 		break;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	case M_STI:
5137c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
5147c478bd9Sstevel@tonic-gate 		fl |= (1 << KREG_EFLAGS_IF_SHIFT);
5157c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_set_register(FLAGS_REG_NAME, fl);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		emulated = 1;
5187c478bd9Sstevel@tonic-gate 		break;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	case M_POPF:
5217c478bd9Sstevel@tonic-gate 		/*
5227c478bd9Sstevel@tonic-gate 		 * popfl will restore a pushed EFLAGS from the stack, and could
523ae115bc7Smrj 		 * in so doing cause IF to be turned on, if only for a brief
5247c478bd9Sstevel@tonic-gate 		 * period.  To avoid this, we'll secretly replace the stack's
5257c478bd9Sstevel@tonic-gate 		 * EFLAGS with our decaffeinated brand.  We'll then manually
5267c478bd9Sstevel@tonic-gate 		 * load our EFLAGS copy with the real verion after the step.
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register("sp", &sp);
5297c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		if (mdb_tgt_vread(mdb.m_target, &newfl, sizeof (kreg_t),
5327c478bd9Sstevel@tonic-gate 		    sp) != sizeof (kreg_t)) {
5337c478bd9Sstevel@tonic-gate 			warn("failed to read " FLAGS_REG_NAME
5347c478bd9Sstevel@tonic-gate 			    " at %p for popfl step\n", (void *)sp);
5357c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_TGTNOTSUP)); /* XXX ? */
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		fl = (fl & ~KREG_EFLAGS_IF_MASK) | KREG_EFLAGS_TF_MASK;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		if (mdb_tgt_vwrite(mdb.m_target, &fl, sizeof (kreg_t),
5417c478bd9Sstevel@tonic-gate 		    sp) != sizeof (kreg_t)) {
5427c478bd9Sstevel@tonic-gate 			warn("failed to update " FLAGS_REG_NAME
5437c478bd9Sstevel@tonic-gate 			    " at %p for popfl step\n", (void *)sp);
5447c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_TGTNOTSUP)); /* XXX ? */
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 		break;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (emulated) {
5507c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_set_register("pc", npc);
5517c478bd9Sstevel@tonic-gate 		return (0);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/* Do the step with IF off, and TF (step) on */
5557c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_get_register(FLAGS_REG_NAME, &oldfl);
5567c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_set_register(FLAGS_REG_NAME,
5577c478bd9Sstevel@tonic-gate 	    ((oldfl | (1 << KREG_EFLAGS_TF_SHIFT)) & ~KREG_EFLAGS_IF_MASK));
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	kmdb_dpi_resume_master(); /* ... there and back again ... */
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	/* EFLAGS has now changed, and may require tuning */
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	switch (instr) {
5647c478bd9Sstevel@tonic-gate 	case M_POPF:
5657c478bd9Sstevel@tonic-gate 		/*
5667c478bd9Sstevel@tonic-gate 		 * Use the EFLAGS we grabbed before the pop - see the pre-step
5677c478bd9Sstevel@tonic-gate 		 * M_POPFL comment.
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_set_register(FLAGS_REG_NAME, newfl);
5707c478bd9Sstevel@tonic-gate 		return (0);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	case M_PUSHF:
5737c478bd9Sstevel@tonic-gate 		/*
5747c478bd9Sstevel@tonic-gate 		 * We pushed our modified EFLAGS (with IF and TF turned off)
5757c478bd9Sstevel@tonic-gate 		 * onto the stack.  Replace the pushed version with our
5767c478bd9Sstevel@tonic-gate 		 * unmodified one.
5777c478bd9Sstevel@tonic-gate 		 */
5787c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register("sp", &sp);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		if (mdb_tgt_vwrite(mdb.m_target, &oldfl, sizeof (kreg_t),
5817c478bd9Sstevel@tonic-gate 		    sp) != sizeof (kreg_t)) {
5827c478bd9Sstevel@tonic-gate 			warn("failed to update pushed " FLAGS_REG_NAME
5837c478bd9Sstevel@tonic-gate 			    " at %p after pushfl step\n", (void *)sp);
5847c478bd9Sstevel@tonic-gate 			return (set_errno(EMDB_TGTNOTSUP)); /* XXX ? */
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		/* Go back to using the EFLAGS we were using before the step */
5887c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_set_register(FLAGS_REG_NAME, oldfl);
5897c478bd9Sstevel@tonic-gate 		return (0);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	default:
5927c478bd9Sstevel@tonic-gate 		/*
5937c478bd9Sstevel@tonic-gate 		 * The stepped instruction may have altered EFLAGS.  We only
5947c478bd9Sstevel@tonic-gate 		 * really care about the value of IF, and we know the stepped
5957c478bd9Sstevel@tonic-gate 		 * instruction didn't alter it, so we can simply copy the
5967c478bd9Sstevel@tonic-gate 		 * pre-step value.  We'll also need to turn TF back off.
5977c478bd9Sstevel@tonic-gate 		 */
5987c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
5997c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_set_register(FLAGS_REG_NAME,
6007c478bd9Sstevel@tonic-gate 		    ((fl & ~(KREG_EFLAGS_TF_MASK|KREG_EFLAGS_IF_MASK)) |
6017c478bd9Sstevel@tonic-gate 		    (oldfl & KREG_EFLAGS_IF_MASK)));
6027c478bd9Sstevel@tonic-gate 		return (0);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6077c478bd9Sstevel@tonic-gate static uintptr_t
6087c478bd9Sstevel@tonic-gate kaif_call(uintptr_t funcva, uint_t argc, const uintptr_t argv[])
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	return (kaif_invoke(funcva, argc, argv));
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate static void
614ae115bc7Smrj dump_crumb(kdi_crumb_t *krmp)
6157c478bd9Sstevel@tonic-gate {
616ae115bc7Smrj 	kdi_crumb_t krm;
617acbc304dSjohnlev 
618ae115bc7Smrj 	if (mdb_vread(&krm, sizeof (kdi_crumb_t), (uintptr_t)krmp) !=
619ae115bc7Smrj 	    sizeof (kdi_crumb_t)) {
620acbc304dSjohnlev 		warn("failed to read crumb at %p", krmp);
621acbc304dSjohnlev 		return;
622acbc304dSjohnlev 	}
623acbc304dSjohnlev 
6247c478bd9Sstevel@tonic-gate 	mdb_printf("state: ");
625acbc304dSjohnlev 	switch (krm.krm_cpu_state) {
6267c478bd9Sstevel@tonic-gate 	case KAIF_CPU_STATE_MASTER:
6277c478bd9Sstevel@tonic-gate 		mdb_printf("M");
6287c478bd9Sstevel@tonic-gate 		break;
6297c478bd9Sstevel@tonic-gate 	case KAIF_CPU_STATE_SLAVE:
6307c478bd9Sstevel@tonic-gate 		mdb_printf("S");
6317c478bd9Sstevel@tonic-gate 		break;
6327c478bd9Sstevel@tonic-gate 	default:
633acbc304dSjohnlev 		mdb_printf("%d", krm.krm_cpu_state);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	mdb_printf(" trapno %3d sp %08x flag %d pc %p %A\n",
637acbc304dSjohnlev 	    krm.krm_trapno, krm.krm_sp, krm.krm_flag, krm.krm_pc, krm.krm_pc);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate static void
6417c478bd9Sstevel@tonic-gate dump_crumbs(kaif_cpusave_t *save)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	int i;
6447c478bd9Sstevel@tonic-gate 
645ae115bc7Smrj 	for (i = KDI_NCRUMBS; i > 0; i--) {
646ae115bc7Smrj 		uint_t idx = (save->krs_curcrumbidx + i) % KDI_NCRUMBS;
6477c478bd9Sstevel@tonic-gate 		dump_crumb(&save->krs_crumbs[idx]);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate static void
6527c478bd9Sstevel@tonic-gate kaif_dump_crumbs(uintptr_t addr, int cpuid)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate 	int i;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	if (addr != NULL) {
657acbc304dSjohnlev 		/* dump_crumb will protect us against bogus addresses */
658ae115bc7Smrj 		dump_crumb((kdi_crumb_t *)addr);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	} else if (cpuid != -1) {
661acbc304dSjohnlev 		if (cpuid < 0 || cpuid >= kaif_ncpusave)
6627c478bd9Sstevel@tonic-gate 			return;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		dump_crumbs(&kaif_cpusave[cpuid]);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	} else {
6677c478bd9Sstevel@tonic-gate 		for (i = 0; i < kaif_ncpusave; i++) {
6687c478bd9Sstevel@tonic-gate 			kaif_cpusave_t *save = &kaif_cpusave[i];
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 			if (save->krs_cpu_state == KAIF_CPU_STATE_NONE)
6717c478bd9Sstevel@tonic-gate 				continue;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 			mdb_printf("%sCPU %d crumbs: (curidx %d)\n",
6747c478bd9Sstevel@tonic-gate 			    (i == 0 ? "" : "\n"), i, save->krs_curcrumbidx);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			dump_crumbs(save);
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate static void
6827c478bd9Sstevel@tonic-gate kaif_modchg_register(void (*func)(struct modctl *, int))
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	kaif_modchg_cb = func;
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate static void
6887c478bd9Sstevel@tonic-gate kaif_modchg_cancel(void)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	ASSERT(kaif_modchg_cb != NULL);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	kaif_modchg_cb = NULL;
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
695ae115bc7Smrj void
696ae115bc7Smrj kaif_trap_set_debugger(void)
697ae115bc7Smrj {
698ae115bc7Smrj 	kmdb_kdi_idt_switch(NULL);
699ae115bc7Smrj }
700ae115bc7Smrj 
701ae115bc7Smrj void
702ae115bc7Smrj kaif_trap_set_saved(kaif_cpusave_t *cpusave)
703ae115bc7Smrj {
704ae115bc7Smrj 	kmdb_kdi_idt_switch(cpusave);
705ae115bc7Smrj }
706ae115bc7Smrj 
707ae115bc7Smrj static void
708ae115bc7Smrj kaif_vmready(void)
709ae115bc7Smrj {
710ae115bc7Smrj }
711ae115bc7Smrj 
712ae115bc7Smrj void
713ae115bc7Smrj kaif_memavail(caddr_t base, size_t len)
714ae115bc7Smrj {
715ae115bc7Smrj 	int ret;
716ae115bc7Smrj 	/*
717ae115bc7Smrj 	 * In the unlikely event that someone is stepping through this routine,
718ae115bc7Smrj 	 * we need to make sure that the KDI knows about the new range before
719ae115bc7Smrj 	 * umem gets it.  That way the entry code can recognize stacks
720ae115bc7Smrj 	 * allocated from the new region.
721ae115bc7Smrj 	 */
722ae115bc7Smrj 	kmdb_kdi_memrange_add(base, len);
723ae115bc7Smrj 	ret = mdb_umem_add(base, len);
724ae115bc7Smrj 	ASSERT(ret == 0);
725ae115bc7Smrj }
726ae115bc7Smrj 
7277c478bd9Sstevel@tonic-gate void
7287c478bd9Sstevel@tonic-gate kaif_mod_loaded(struct modctl *modp)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	if (kaif_modchg_cb != NULL)
7317c478bd9Sstevel@tonic-gate 		kaif_modchg_cb(modp, 1);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate void
7357c478bd9Sstevel@tonic-gate kaif_mod_unloading(struct modctl *modp)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate 	if (kaif_modchg_cb != NULL)
7387c478bd9Sstevel@tonic-gate 		kaif_modchg_cb(modp, 0);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
741ae115bc7Smrj void
742ae115bc7Smrj kaif_handle_fault(greg_t trapno, greg_t pc, greg_t sp, int cpuid)
7437c478bd9Sstevel@tonic-gate {
744ae115bc7Smrj 	kmdb_dpi_handle_fault((kreg_t)trapno, (kreg_t)pc,
745ae115bc7Smrj 	    (kreg_t)sp, cpuid);
746ae115bc7Smrj }
747ae115bc7Smrj 
748ae115bc7Smrj static kdi_debugvec_t kaif_dvec = {
749ae115bc7Smrj 	NULL,			/* dv_kctl_vmready */
750ae115bc7Smrj 	NULL,			/* dv_kctl_memavail */
751ae115bc7Smrj 	NULL,			/* dv_kctl_modavail */
752ae115bc7Smrj 	NULL,			/* dv_kctl_thravail */
753ae115bc7Smrj 	kaif_vmready,
754ae115bc7Smrj 	kaif_memavail,
755ae115bc7Smrj 	kaif_mod_loaded,
756ae115bc7Smrj 	kaif_mod_unloading,
757ae115bc7Smrj 	kaif_handle_fault
7587c478bd9Sstevel@tonic-gate };
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate void
761ae115bc7Smrj kaif_kdi_entry(kdi_cpusave_t *cpusave)
7627c478bd9Sstevel@tonic-gate {
763ae115bc7Smrj 	int ret = kaif_main_loop(cpusave);
764ae115bc7Smrj 	ASSERT(ret == KAIF_CPU_CMD_RESUME ||
765ae115bc7Smrj 	    ret == KAIF_CPU_CMD_RESUME_MASTER);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
768ae115bc7Smrj /*ARGSUSED*/
7697c478bd9Sstevel@tonic-gate void
770ae115bc7Smrj kaif_activate(kdi_debugvec_t **dvecp, uint_t flags)
7717c478bd9Sstevel@tonic-gate {
772ae115bc7Smrj 	kmdb_kdi_activate(kaif_kdi_entry, kaif_cpusave, kaif_ncpusave);
773ae115bc7Smrj 	*dvecp = &kaif_dvec;
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate static int
7777c478bd9Sstevel@tonic-gate kaif_init(kmdb_auxv_t *kav)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	/* Allocate the per-CPU save areas */
7807c478bd9Sstevel@tonic-gate 	kaif_cpusave = mdb_zalloc(sizeof (kaif_cpusave_t) * kav->kav_ncpu,
7817c478bd9Sstevel@tonic-gate 	    UM_SLEEP);
7827c478bd9Sstevel@tonic-gate 	kaif_ncpusave = kav->kav_ncpu;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	kaif_modchg_cb = NULL;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	kaif_waptmap = 0;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	kaif_trap_switch = (kav->kav_flags & KMDB_AUXV_FL_NOTRPSWTCH) == 0;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	return (0);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate dpi_ops_t kmdb_dpi_ops = {
7947c478bd9Sstevel@tonic-gate 	kaif_init,
7957c478bd9Sstevel@tonic-gate 	kaif_activate,
796ae115bc7Smrj 	kmdb_kdi_deactivate,
7977c478bd9Sstevel@tonic-gate 	kaif_enter_mon,
7987c478bd9Sstevel@tonic-gate 	kaif_modchg_register,
7997c478bd9Sstevel@tonic-gate 	kaif_modchg_cancel,
8007c478bd9Sstevel@tonic-gate 	kaif_get_cpu_state,
8017c478bd9Sstevel@tonic-gate 	kaif_get_master_cpuid,
8027c478bd9Sstevel@tonic-gate 	kaif_get_gregs,
803acbc304dSjohnlev 	kaif_get_register,
804acbc304dSjohnlev 	kaif_set_register,
8057c478bd9Sstevel@tonic-gate 	kaif_brkpt_arm,
8067c478bd9Sstevel@tonic-gate 	kaif_brkpt_disarm,
8077c478bd9Sstevel@tonic-gate 	kaif_wapt_validate,
8087c478bd9Sstevel@tonic-gate 	kaif_wapt_reserve,
8097c478bd9Sstevel@tonic-gate 	kaif_wapt_release,
8107c478bd9Sstevel@tonic-gate 	kaif_wapt_arm,
8117c478bd9Sstevel@tonic-gate 	kaif_wapt_disarm,
8127c478bd9Sstevel@tonic-gate 	kaif_wapt_match,
8137c478bd9Sstevel@tonic-gate 	kaif_step,
8147c478bd9Sstevel@tonic-gate 	kaif_call,
8157c478bd9Sstevel@tonic-gate 	kaif_dump_crumbs,
8167c478bd9Sstevel@tonic-gate };
817