17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * SPARC-specific portions of the debugger fault routines
317c478bdstevel@tonic-gate */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include <sys/types.h>
347c478bdstevel@tonic-gate#include <sys/stack.h>
357c478bdstevel@tonic-gate#include <sys/frame.h>
367c478bdstevel@tonic-gate#include <sys/machtrap.h>
377c478bdstevel@tonic-gate#include <sys/machasi.h>
387c478bdstevel@tonic-gate#include <sys/sun4asi.h>
397c478bdstevel@tonic-gate#include <sys/intreg.h>
407c478bdstevel@tonic-gate#include <sys/mmu.h>
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#include <kmdb/kmdb_asmutil.h>
437c478bdstevel@tonic-gate#include <kmdb/kmdb_fault.h>
447c478bdstevel@tonic-gate#include <mdb/mdb_io.h>
457c478bdstevel@tonic-gate#include <mdb/mdb.h>
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate#define	MAX_STACK_FRAMES	30
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gatestatic void
507c478bdstevel@tonic-gateprint_frame(uintptr_t sp, uintptr_t pc, int fnum, int safe)
517c478bdstevel@tonic-gate{
527c478bdstevel@tonic-gate	if (safe) {
537c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, "    [%2d] %?p %?p()\n",
547c478bdstevel@tonic-gate		    fnum, sp, pc);
557c478bdstevel@tonic-gate	} else {
567c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, "    [%2d] %?p %a()\n",
577c478bdstevel@tonic-gate		    fnum, sp, pc);
587c478bdstevel@tonic-gate	}
597c478bdstevel@tonic-gate}
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gatestatic int
627c478bdstevel@tonic-gatevalid_frame(struct frame *fr)
637c478bdstevel@tonic-gate{
647c478bdstevel@tonic-gate	uintptr_t addr = (uintptr_t)fr;
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate	if (!(addr - (uintptr_t)mdb.m_dseg < mdb.m_dsegsz)) {
677c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, "    frame (%p) outside of "
687c478bdstevel@tonic-gate		    "debugger segment\n", addr);
697c478bdstevel@tonic-gate		return (0);
707c478bdstevel@tonic-gate	}
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate	if (addr & (STACK_ALIGN - 1)) {
737c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, "    mis-aligned frame (%p)\n", fr);
747c478bdstevel@tonic-gate		return (0);
757c478bdstevel@tonic-gate	}
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate	return (1);
787c478bdstevel@tonic-gate}
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gatestatic void
817c478bdstevel@tonic-gateprint_stack(kreg_t sp, int safe)
827c478bdstevel@tonic-gate{
837c478bdstevel@tonic-gate	struct frame *fr = (struct frame *)(sp + STACK_BIAS);
847c478bdstevel@tonic-gate	struct frame *nfr;
857c478bdstevel@tonic-gate	int frnum = 1;
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate	while (fr != NULL && valid_frame(fr) && fr->fr_savpc != 0 &&
887c478bdstevel@tonic-gate	    frnum <= MAX_STACK_FRAMES) {
897c478bdstevel@tonic-gate		print_frame((uintptr_t)fr - STACK_BIAS, fr->fr_savpc, frnum++,
907c478bdstevel@tonic-gate		    safe);
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate		nfr = (struct frame *)
937c478bdstevel@tonic-gate		    ((uintptr_t)fr->fr_savfp + STACK_BIAS);
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate		if ((uintptr_t)nfr == STACK_BIAS)
967c478bdstevel@tonic-gate			break;
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate		if ((uintptr_t)nfr < (uintptr_t)fr) {
997c478bdstevel@tonic-gate			mdb_iob_printf(mdb.m_err,
1007c478bdstevel@tonic-gate			    "    fp (%p) < sp (%p)\n", nfr, fr);
1017c478bdstevel@tonic-gate			break;
1027c478bdstevel@tonic-gate		}
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate		fr = nfr;
1057c478bdstevel@tonic-gate	}
1067c478bdstevel@tonic-gate}
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gatevoid
1097c478bdstevel@tonic-gatekmdb_print_stack(void)
1107c478bdstevel@tonic-gate{
1117c478bdstevel@tonic-gate	print_stack(get_fp(), FALSE); /* show sym names */
1127c478bdstevel@tonic-gate}
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gatevoid
1157c478bdstevel@tonic-gatekmdb_fault_display(kreg_t tt, kreg_t pc, kreg_t sp, int safe)
1167c478bdstevel@tonic-gate{
1177c478bdstevel@tonic-gate	mdb_iob_printf(mdb.m_err, "    tt: %p, sp: %p, pc: %p", tt, sp, pc);
1187c478bdstevel@tonic-gate	if (!safe)
1197c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, " %A", pc);
1207c478bdstevel@tonic-gate	mdb_iob_printf(mdb.m_err, "\n");
1217c478bdstevel@tonic-gate
1227c478bdstevel@tonic-gate	switch (tt) {
1237c478bdstevel@tonic-gate	case T_FAST_DATA_MMU_MISS: {
1247c478bdstevel@tonic-gate#ifdef sun4v
1257c478bdstevel@tonic-gate#else /* sun4v */
1267c478bdstevel@tonic-gate		uint64_t dsfar = rdasi(ASI_DMMU, MMU_SFAR);
1277c478bdstevel@tonic-gate		const char *fmt = safe ? "%s%p\n" : "%s%a\n";
1287c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err, fmt, "\tDSFAR now: ", dsfar);
1297c478bdstevel@tonic-gate#endif /* sun4v */
1307c478bdstevel@tonic-gate		break;
1317c478bdstevel@tonic-gate	}
1327c478bdstevel@tonic-gate	case T_VECTORED_INT:
1337c478bdstevel@tonic-gate#ifdef sun4v
1347c478bdstevel@tonic-gate#else /* sun4v */
1357c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err,
1367c478bdstevel@tonic-gate		    "\tIRDR now: 0: %lx, 1: %lx, 2: %lx\n",
1377c478bdstevel@tonic-gate		    (ulong_t)rdasi(ASI_INTR_RECEIVE, IRDR_0),
1387c478bdstevel@tonic-gate		    (ulong_t)rdasi(ASI_INTR_RECEIVE, IRDR_1),
1397c478bdstevel@tonic-gate		    (ulong_t)rdasi(ASI_INTR_RECEIVE, IRDR_2));
1407c478bdstevel@tonic-gate#endif /* sun4v */
1417c478bdstevel@tonic-gate		break;
1427c478bdstevel@tonic-gate	}
1437c478bdstevel@tonic-gate
1447c478bdstevel@tonic-gate	mdb_iob_printf(mdb.m_err, "\n");
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gate	if (mdb.m_dseg == NULL || mdb.m_dsegsz == 0) {
1477c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err,
1487c478bdstevel@tonic-gate		    "\t*** Stack trace omitted because debugger segment size\n"
1497c478bdstevel@tonic-gate		    "\t*** and/or length not set.\n");
1507c478bdstevel@tonic-gate		return;
1517c478bdstevel@tonic-gate	}
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	if (!(sp - (uintptr_t)mdb.m_dseg < mdb.m_dsegsz)) {
1547c478bdstevel@tonic-gate		mdb_iob_printf(mdb.m_err,
1557c478bdstevel@tonic-gate		    "\t*** Stack trace omitted because sp (%p) isn't in the\n"
1567c478bdstevel@tonic-gate		    "\t*** debugger segment.\n", sp);
1577c478bdstevel@tonic-gate		return;
1587c478bdstevel@tonic-gate	}
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate	flush_windows();
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gate	print_stack(sp, safe);
1637c478bdstevel@tonic-gate}
164