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
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * 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.
24399ca3a7SJohn Levon  *
25399ca3a7SJohn Levon  * Copyright 2018 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Intel-specific portions of the DPI
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/trap.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_dpi_impl.h>
367c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_fault.h>
377c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
387c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
397c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
407c478bd9Sstevel@tonic-gate #include <mdb/mdb_kreg.h>
417c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate void
kmdb_dpi_handle_fault(kreg_t trapno,kreg_t pc,kreg_t sp,int cpuid)447c478bd9Sstevel@tonic-gate kmdb_dpi_handle_fault(kreg_t trapno, kreg_t pc, kreg_t sp, int cpuid)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	kmdb_kdi_system_claim();
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_DPI, "\ndpi_handle_fault: trapno %u, pc 0x%0?p, "
497c478bd9Sstevel@tonic-gate 	    "sp 0x%0?p\n", (int)trapno, pc, sp);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	switch (trapno) {
527c478bd9Sstevel@tonic-gate 	case T_GPFLT:
537c478bd9Sstevel@tonic-gate 		errno = EACCES;
54*b1da084bSToomas Soome 		break;
557c478bd9Sstevel@tonic-gate 	default:
567c478bd9Sstevel@tonic-gate 		errno = EMDB_NOMAP;
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (kmdb_dpi_fault_pcb != NULL) {
607c478bd9Sstevel@tonic-gate 		longjmp(*kmdb_dpi_fault_pcb, 1);
617c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/* Debugger fault */
657c478bd9Sstevel@tonic-gate 	kmdb_fault(trapno, pc, sp, cpuid);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
697c478bd9Sstevel@tonic-gate int
kmdb_dpi_get_register(const char * regname,kreg_t * kregp)707c478bd9Sstevel@tonic-gate kmdb_dpi_get_register(const char *regname, kreg_t *kregp)
717c478bd9Sstevel@tonic-gate {
72acbc304dSjohnlev 	return (mdb.m_dpi->dpo_get_register(regname, kregp));
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
767c478bd9Sstevel@tonic-gate int
kmdb_dpi_set_register(const char * regname,kreg_t kreg)777c478bd9Sstevel@tonic-gate kmdb_dpi_set_register(const char *regname, kreg_t kreg)
787c478bd9Sstevel@tonic-gate {
79acbc304dSjohnlev 	return (mdb.m_dpi->dpo_set_register(regname, kreg));
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Continue/resume handling.  If the target calls kmdb_dpi_resume(), it
847c478bd9Sstevel@tonic-gate  * expects that the world will be resumed, and that the call will return
857c478bd9Sstevel@tonic-gate  * when the world has stopped again.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * For support, we have resume_return(), which is called from main() when
887c478bd9Sstevel@tonic-gate  * the continuation has completed (when the world has stopped again).
897c478bd9Sstevel@tonic-gate  * set_resume_exit() tells where to jump to actually restart the world.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * CAUTION: This routine may be called *after* mdb_destroy.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate void
kmdb_dpi_resume_common(int cmd)947c478bd9Sstevel@tonic-gate kmdb_dpi_resume_common(int cmd)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	kreg_t pc, trapno;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	ASSERT(kmdb_dpi_resume_requested == 0);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (setjmp(kmdb_dpi_resume_pcb) == 0) {
1017c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register("pc", &pc);
1027c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_PROC, "Resume requested, pc is %p\n",
1037c478bd9Sstevel@tonic-gate 		    (void *)pc);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		if (cmd != KMDB_DPI_CMD_RESUME_UNLOAD)
1067c478bd9Sstevel@tonic-gate 			kmdb_dpi_resume_requested = 1;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 		longjmp(kmdb_dpi_entry_pcb, cmd);
1097c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	} else {
1127c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register("pc", &pc);
1137c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_get_register("trapno", &trapno);
1147c478bd9Sstevel@tonic-gate 		mdb_dprintf(MDB_DBG_PROC, "Back from resume, pc: %p, "
1157c478bd9Sstevel@tonic-gate 		    "trapno: %u\n", (void *)pc, (int)trapno);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		kmdb_dpi_resume_requested = 0;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		switch (trapno) {
1207c478bd9Sstevel@tonic-gate 		case T_BPTFLT:
1217c478bd9Sstevel@tonic-gate 			kmdb_dpi_set_state(DPI_STATE_FAULTED,
1227c478bd9Sstevel@tonic-gate 			    DPI_STATE_WHY_BKPT);
1237c478bd9Sstevel@tonic-gate 			break;
1247c478bd9Sstevel@tonic-gate 		case T_DBGENTR:
1257c478bd9Sstevel@tonic-gate 			kmdb_dpi_set_state(DPI_STATE_STOPPED, 0);
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 		default:
1287c478bd9Sstevel@tonic-gate 			kmdb_dpi_set_state(DPI_STATE_FAULTED,
1297c478bd9Sstevel@tonic-gate 			    DPI_STATE_WHY_TRAP);
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_PROC, "returning from resume\n");
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate void
kmdb_dpi_reboot(void)1387c478bd9Sstevel@tonic-gate kmdb_dpi_reboot(void)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * We're going to skip all of the niceties we employ in resume_common,
1427c478bd9Sstevel@tonic-gate 	 * as we don't plan to ever return.
1437c478bd9Sstevel@tonic-gate 	 */
1447c478bd9Sstevel@tonic-gate 	longjmp(kmdb_dpi_entry_pcb, KMDB_DPI_CMD_REBOOT);
1457c478bd9Sstevel@tonic-gate }
146