1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29/*
30 * SPARC-specific portions of the debugger fault routines
31 */
32
33#include <sys/types.h>
34#include <sys/stack.h>
35#include <sys/frame.h>
36#include <sys/machtrap.h>
37#include <sys/machasi.h>
38#include <sys/sun4asi.h>
39#include <sys/intreg.h>
40#include <sys/mmu.h>
41
42#include <kmdb/kmdb_asmutil.h>
43#include <kmdb/kmdb_fault.h>
44#include <mdb/mdb_io.h>
45#include <mdb/mdb.h>
46
47#define	MAX_STACK_FRAMES	30
48
49static void
50print_frame(uintptr_t sp, uintptr_t pc, int fnum, int safe)
51{
52	if (safe) {
53		mdb_iob_printf(mdb.m_err, "    [%2d] %?p %?p()\n",
54		    fnum, sp, pc);
55	} else {
56		mdb_iob_printf(mdb.m_err, "    [%2d] %?p %a()\n",
57		    fnum, sp, pc);
58	}
59}
60
61static int
62valid_frame(struct frame *fr)
63{
64	uintptr_t addr = (uintptr_t)fr;
65
66	if (!(addr - (uintptr_t)mdb.m_dseg < mdb.m_dsegsz)) {
67		mdb_iob_printf(mdb.m_err, "    frame (%p) outside of "
68		    "debugger segment\n", addr);
69		return (0);
70	}
71
72	if (addr & (STACK_ALIGN - 1)) {
73		mdb_iob_printf(mdb.m_err, "    mis-aligned frame (%p)\n", fr);
74		return (0);
75	}
76
77	return (1);
78}
79
80static void
81print_stack(kreg_t sp, int safe)
82{
83	struct frame *fr = (struct frame *)(sp + STACK_BIAS);
84	struct frame *nfr;
85	int frnum = 1;
86
87	while (fr != NULL && valid_frame(fr) && fr->fr_savpc != 0 &&
88	    frnum <= MAX_STACK_FRAMES) {
89		print_frame((uintptr_t)fr - STACK_BIAS, fr->fr_savpc, frnum++,
90		    safe);
91
92		nfr = (struct frame *)
93		    ((uintptr_t)fr->fr_savfp + STACK_BIAS);
94
95		if ((uintptr_t)nfr == STACK_BIAS)
96			break;
97
98		if ((uintptr_t)nfr < (uintptr_t)fr) {
99			mdb_iob_printf(mdb.m_err,
100			    "    fp (%p) < sp (%p)\n", nfr, fr);
101			break;
102		}
103
104		fr = nfr;
105	}
106}
107
108void
109kmdb_print_stack(void)
110{
111	print_stack(get_fp(), FALSE); /* show sym names */
112}
113
114void
115kmdb_fault_display(kreg_t tt, kreg_t pc, kreg_t sp, int safe)
116{
117	mdb_iob_printf(mdb.m_err, "    tt: %p, sp: %p, pc: %p", tt, sp, pc);
118	if (!safe)
119		mdb_iob_printf(mdb.m_err, " %A", pc);
120	mdb_iob_printf(mdb.m_err, "\n");
121
122	switch (tt) {
123	case T_FAST_DATA_MMU_MISS: {
124#ifdef sun4v
125#else /* sun4v */
126		uint64_t dsfar = rdasi(ASI_DMMU, MMU_SFAR);
127		const char *fmt = safe ? "%s%p\n" : "%s%a\n";
128		mdb_iob_printf(mdb.m_err, fmt, "\tDSFAR now: ", dsfar);
129#endif /* sun4v */
130		break;
131	}
132	case T_VECTORED_INT:
133#ifdef sun4v
134#else /* sun4v */
135		mdb_iob_printf(mdb.m_err,
136		    "\tIRDR now: 0: %lx, 1: %lx, 2: %lx\n",
137		    (ulong_t)rdasi(ASI_INTR_RECEIVE, IRDR_0),
138		    (ulong_t)rdasi(ASI_INTR_RECEIVE, IRDR_1),
139		    (ulong_t)rdasi(ASI_INTR_RECEIVE, IRDR_2));
140#endif /* sun4v */
141		break;
142	}
143
144	mdb_iob_printf(mdb.m_err, "\n");
145
146	if (mdb.m_dseg == NULL || mdb.m_dsegsz == 0) {
147		mdb_iob_printf(mdb.m_err,
148		    "\t*** Stack trace omitted because debugger segment size\n"
149		    "\t*** and/or length not set.\n");
150		return;
151	}
152
153	if (!(sp - (uintptr_t)mdb.m_dseg < mdb.m_dsegsz)) {
154		mdb_iob_printf(mdb.m_err,
155		    "\t*** Stack trace omitted because sp (%p) isn't in the\n"
156		    "\t*** debugger segment.\n", sp);
157		return;
158	}
159
160	flush_windows();
161
162	print_stack(sp, safe);
163}
164