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
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * 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  */
211ae08745Sheppo 
227c478bd9Sstevel@tonic-gate /*
231ae08745Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*5799076eSPatrick Mooney  * Copyright 2016 Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Handling of unintentional faults (i.e. bugs) in the debugger.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_fault.h>
357c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_promif.h>
367c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
377c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_dpi.h>
387c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
397c478bd9Sstevel@tonic-gate #include <mdb/mdb_kreg.h>
407c478bd9Sstevel@tonic-gate #include <mdb/mdb_io_impl.h>
417c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate void
kmdb_fault(kreg_t tt,kreg_t pc,kreg_t sp,int cpuid)447c478bd9Sstevel@tonic-gate kmdb_fault(kreg_t tt, kreg_t pc, kreg_t sp, int cpuid)
457c478bd9Sstevel@tonic-gate {
46*5799076eSPatrick Mooney 	int debug_self_confirm = 0;
47*5799076eSPatrick Mooney 	volatile int try;
487c478bd9Sstevel@tonic-gate 	jmp_buf pcb, *old;
497c478bd9Sstevel@tonic-gate 	char c;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	/* Make absolutely sure */
527c478bd9Sstevel@tonic-gate 	kmdb_kdi_system_claim();
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	try = 1;
557c478bd9Sstevel@tonic-gate 	if (setjmp(pcb) != 0) {
567c478bd9Sstevel@tonic-gate 		if (++try == 2) {
577c478bd9Sstevel@tonic-gate 			mdb_iob_printf(mdb.m_err,
587c478bd9Sstevel@tonic-gate 			    "\n*** First stack trace attempt failed.  "
597c478bd9Sstevel@tonic-gate 			    "Trying safe mode.\n\n");
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 			kmdb_fault_display(tt, pc, sp, 1);
627c478bd9Sstevel@tonic-gate 		} else {
637c478bd9Sstevel@tonic-gate 			mdb_iob_printf(mdb.m_err,
647c478bd9Sstevel@tonic-gate 			    "\n*** Unable to print stack trace.\n");
657c478bd9Sstevel@tonic-gate 		}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	} else {
687c478bd9Sstevel@tonic-gate 		old = kmdb_dpi_set_fault_hdlr(&pcb);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 		mdb_iob_printf(mdb.m_err, "\n*** Debugger Fault (CPU %d)\n\n",
717c478bd9Sstevel@tonic-gate 		    cpuid);
727c478bd9Sstevel@tonic-gate 		kmdb_fault_display(tt, pc, sp, 0);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	kmdb_dpi_restore_fault_hdlr(old);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (mdb.m_term != NULL) {
787c478bd9Sstevel@tonic-gate 		for (;;) {
797c478bd9Sstevel@tonic-gate 			mdb_iob_printf(mdb.m_err, "\n%s: "
807c478bd9Sstevel@tonic-gate #if defined(__sparc)
811ae08745Sheppo #ifndef sun4v
821ae08745Sheppo 			    "(o)bp, "
831ae08745Sheppo #endif /* sun4v */
841ae08745Sheppo 			    "(p)anic"
857c478bd9Sstevel@tonic-gate #else
867c478bd9Sstevel@tonic-gate 			    "reboo(t)"
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate 			    ", or (d)ebug with self? ", mdb.m_pname);
897c478bd9Sstevel@tonic-gate 			mdb_iob_flush(mdb.m_err);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 			if (IOP_READ(mdb.m_term, &c, sizeof (c)) != sizeof (c))
927c478bd9Sstevel@tonic-gate 				goto fault_obp;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 			mdb_iob_printf(mdb.m_err, "\n");
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 			switch (c) {
977c478bd9Sstevel@tonic-gate #ifdef __sparc
987c478bd9Sstevel@tonic-gate 			case 'p':
997c478bd9Sstevel@tonic-gate 				kmdb_dpi_kernpanic(cpuid);
1007c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
1017c478bd9Sstevel@tonic-gate 				continue;
1027c478bd9Sstevel@tonic-gate #endif
1037c478bd9Sstevel@tonic-gate 
1041ae08745Sheppo #ifndef sun4v
1057c478bd9Sstevel@tonic-gate 			case 'o':
1067c478bd9Sstevel@tonic-gate 			case 'O':
1071ae08745Sheppo #endif /* sun4v */
1087c478bd9Sstevel@tonic-gate 			case 't':
1097c478bd9Sstevel@tonic-gate 			case 'T':
1107c478bd9Sstevel@tonic-gate 				kmdb_dpi_enter_mon();
1117c478bd9Sstevel@tonic-gate 				continue;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 			case 'd':
1147c478bd9Sstevel@tonic-gate 			case 'D':
1157c478bd9Sstevel@tonic-gate 				/*
1167c478bd9Sstevel@tonic-gate 				 * Debug self - get confirmation, because they
1177c478bd9Sstevel@tonic-gate 				 * can't go back to their running system if
1187c478bd9Sstevel@tonic-gate 				 * they choose this one.
1197c478bd9Sstevel@tonic-gate 				 */
1207c478bd9Sstevel@tonic-gate 				if (debug_self_confirm == 0) {
1217c478bd9Sstevel@tonic-gate 					mdb_iob_printf(mdb.m_err,
1227c478bd9Sstevel@tonic-gate 					    "NOTE: You will not be able to "
1237c478bd9Sstevel@tonic-gate 					    "resume your system if you "
1247c478bd9Sstevel@tonic-gate 					    "choose this option.\nPlease "
1257c478bd9Sstevel@tonic-gate 					    "select 'd' again to confirm.\n");
1267c478bd9Sstevel@tonic-gate 					debug_self_confirm = 1;
1277c478bd9Sstevel@tonic-gate 					continue;
1287c478bd9Sstevel@tonic-gate 				}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 				kmdb_dpi_set_state(DPI_STATE_LOST, 0);
1317c478bd9Sstevel@tonic-gate 				return;
1327c478bd9Sstevel@tonic-gate 			}
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate fault_obp:
1377c478bd9Sstevel@tonic-gate 	exit(1);
1387c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1397c478bd9Sstevel@tonic-gate }
140