xref: /titanic_44/usr/src/cmd/mdb/sun4u/modules/unix/unix.c (revision 6185db853e024a486ff8837e6784dd290d866112)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 
30 #ifndef DEBUG
31 #define	DEBUG
32 #define	_SYS_DEBUG_H
33 #include <sys/xc_impl.h>
34 #undef	DEBUG
35 #else
36 #define	_SYS_DEBUG_H
37 #include <sys/xc_impl.h>
38 #endif
39 
40 #include <sys/traptrace.h>
41 #include <sys/machparam.h>
42 #include <sys/intreg.h>
43 #include <sys/ivintr.h>
44 #include <sys/mutex_impl.h>
45 
46 #include <mdb/mdb_modapi.h>
47 #include <mdb/mdb_ctf.h>
48 #include "sfmmu.h"
49 
50 #ifndef SYSTRAP_TT
51 #define	SYSTRAP_TT	0x1300
52 #endif
53 
54 typedef struct trap_trace_fullrec {
55 	struct trap_trace_record ttf_rec;
56 	int ttf_cpu;
57 } trap_trace_fullrec_t;
58 
59 #ifdef sun4v
60 typedef struct htrap_trace_fullrec {
61 	struct htrap_trace_record ttf_rec;
62 	int ttf_cpu;
63 } htrap_trace_fullrec_t;
64 #endif
65 
66 /*
67  * These strings and accompanying macros allow our string table to look
68  * just like the real table in trap_table.s.
69  */
70 
71 static const char NOT[] = "reserved";	/* common reserved string */
72 static const char BAD[] = "unused";	/* common unused string */
73 
74 #define	NOT4	NOT, NOT, NOT, NOT
75 #define	BAD4	BAD, BAD, BAD, BAD
76 
77 static const char *const ttdescr[] = {
78 	NOT,				/* 000	reserved */
79 	"power-on",			/* 001	power on reset */
80 	"watchdog",			/* 002	watchdog reset */
81 	"xir",				/* 003	externally initiated reset */
82 	"sir",				/* 004	software initiated reset */
83 	"red",				/* 005	red mode exception */
84 	NOT, NOT,			/* 006 - 007 reserved */
85 	"immu-xcp",			/* 008	instruction access exception */
86 	"immu-miss",			/* 009	instruction access MMU miss */
87 	"immu-err",			/* 00A	instruction access error */
88 	NOT, NOT4,			/* 00B - 00F reserved */
89 	"ill-inst",			/* 010	illegal instruction */
90 	"priv-inst",			/* 011	privileged opcode */
91 	"unimp-ldd",			/* 012	unimplemented LDD */
92 	"unimp-std",			/* 013	unimplemented STD */
93 	NOT4, NOT4, NOT4,		/* 014 - 01F reserved */
94 	"fp-disable",			/* 020	fp disabled */
95 	"fp-ieee754",			/* 021	fp exception ieee 754 */
96 	"fp-xcp-other",			/* 022	fp exception other */
97 	"tag-oflow",			/* 023	tag overflow */
98 	"cleanwin",			/* 024	clean window */
99 	"cleanwin",			/* 025	clean window */
100 	"cleanwin",			/* 026	clean window */
101 	"cleanwin",			/* 027	clean window */
102 	"div-zero",			/* 028	division by zero */
103 	"internal-err",			/* 029	internal processor error */
104 	NOT, NOT, NOT4,			/* 02A - 02F reserved */
105 	"dmmu-xcp",			/* 030	data access exception */
106 	"dmmu-miss",			/* 031	data access MMU miss */
107 	"dmmu-err",			/* 032	data access error */
108 	"dmmu-prot",			/* 033	data access protection */
109 	"unalign",			/* 034	mem address not aligned */
110 	"lddf-unalign",			/* 035	LDDF mem address not aligned */
111 	"stdf-unalign",			/* 036	STDF mem address not aligned */
112 	"priv-act",			/* 037	privileged action */
113 	"ldqf-unalign",			/* 038	LDQF mem address not aligned */
114 	"stqf-unalign",			/* 039	STQF mem address not aligned */
115 	NOT, NOT, NOT4,			/* 03A - 03F reserved */
116 	"async-d-err",			/* 040	async data error */
117 	"level-1",			/* 041	interrupt level 1 */
118 	"level-2",			/* 042	interrupt level 2 */
119 	"level-3",			/* 043	interrupt level 3 */
120 	"level-4",			/* 044	interrupt level 4 */
121 	"level-5",			/* 045	interrupt level 5 */
122 	"level-6",			/* 046	interrupt level 6 */
123 	"level-7",			/* 047	interrupt level 7 */
124 	"level-8",			/* 048	interrupt level 8 */
125 	"level-9",			/* 049	interrupt level 9 */
126 	"level-10",			/* 04A	interrupt level 10 */
127 	"level-11",			/* 04B	interrupt level 11 */
128 	"level-12",			/* 04C	interrupt level 12 */
129 	"level-13",			/* 04D	interrupt level 13 */
130 	"level-14",			/* 04E	interrupt level 14 */
131 	"level-15",			/* 04F	interrupt level 15 */
132 	NOT4, NOT4, NOT4, NOT4,		/* 050 - 05F reserved */
133 	"int-vec",			/* 060	interrupt vector */
134 	"pa-watch",			/* 061	PA watchpoint */
135 	"va-watch",			/* 062	VA watchpoint */
136 	"ecc-err",			/* 063	corrected ECC error */
137 	"itlb-miss",			/* 064	instruction access MMU miss */
138 	"itlb-miss",			/* 065	instruction access MMU miss */
139 	"itlb-miss",			/* 066	instruction access MMU miss */
140 	"itlb-miss",			/* 067	instruction access MMU miss */
141 	"dtlb-miss",			/* 068	data access MMU miss */
142 	"dtlb-miss",			/* 069	data access MMU miss */
143 	"dtlb-miss",			/* 06A	data access MMU miss */
144 	"dtlb-miss",			/* 06B	data access MMU miss */
145 	"dtlb-prot",			/* 06C	data access protection */
146 	"dtlb-prot",			/* 06D	data access protection */
147 	"dtlb-prot",			/* 06E	data access protection */
148 	"dtlb-prot",			/* 06F	data access protection */
149 	"fast-ecc-err",			/* 070	fast ecache ECC error */
150 	"dp-err",			/* 071	data cache parity error */
151 	"ip-err",			/* 072	instr cache parity error */
152 	NOT, NOT4, NOT4,		/* 073 - 07B reserved */
153 #ifdef sun4v
154 	"cpu-mondo",			/* 07C  CPU mondo */
155 	"dev-mondo",			/* 07D  device mondo */
156 	"res.-err",			/* 07E  resumable error */
157 	"non-res.-err",			/* 07F  non-resumable error */
158 #else
159 	NOT4,				/* 07C - 07F reserved */
160 #endif
161 	"spill-0-norm",			/* 080	spill 0 normal */
162 	"spill-0-norm",			/* 081	spill 0 normal */
163 	"spill-0-norm",			/* 082	spill 0 normal */
164 	"spill-0-norm",			/* 083	spill 0 normal */
165 	"spill-1-norm",			/* 084	spill 1 normal */
166 	"spill-1-norm",			/* 085	spill 1 normal */
167 	"spill-1-norm",			/* 086	spill 1 normal */
168 	"spill-1-norm",			/* 087	spill 1 normal */
169 	"spill-2-norm",			/* 088	spill 2 normal */
170 	"spill-2-norm",			/* 089	spill 2 normal */
171 	"spill-2-norm",			/* 08A	spill 2 normal */
172 	"spill-2-norm",			/* 08B	spill 2 normal */
173 	"spill-3-norm",			/* 08C	spill 3 normal */
174 	"spill-3-norm",			/* 08D	spill 3 normal */
175 	"spill-3-norm",			/* 08E	spill 3 normal */
176 	"spill-3-norm",			/* 08F	spill 3 normal */
177 	"spill-4-norm",			/* 090	spill 4 normal */
178 	"spill-4-norm",			/* 091	spill 4 normal */
179 	"spill-4-norm",			/* 092	spill 4 normal */
180 	"spill-4-norm",			/* 093	spill 4 normal */
181 	"spill-5-norm",			/* 094	spill 5 normal */
182 	"spill-5-norm",			/* 095	spill 5 normal */
183 	"spill-5-norm",			/* 096	spill 5 normal */
184 	"spill-5-norm",			/* 097	spill 5 normal */
185 	"spill-6-norm",			/* 098	spill 6 normal */
186 	"spill-6-norm",			/* 099	spill 6 normal */
187 	"spill-6-norm",			/* 09A	spill 6 normal */
188 	"spill-6-norm",			/* 09B	spill 6 normal */
189 	"spill-7-norm",			/* 09C	spill 7 normal */
190 	"spill-7-norm",			/* 09D	spill 7 normal */
191 	"spill-7-norm",			/* 09E	spill 7 normal */
192 	"spill-7-norm",			/* 09F	spill 7 normal */
193 	"spill-0-oth",			/* 0A0	spill 0 other */
194 	"spill-0-oth",			/* 0A1	spill 0 other */
195 	"spill-0-oth",			/* 0A2	spill 0 other */
196 	"spill-0-oth",			/* 0A3	spill 0 other */
197 	"spill-1-oth",			/* 0A4	spill 1 other */
198 	"spill-1-oth",			/* 0A5	spill 1 other */
199 	"spill-1-oth",			/* 0A6	spill 1 other */
200 	"spill-1-oth",			/* 0A7	spill 1 other */
201 	"spill-2-oth",			/* 0A8	spill 2 other */
202 	"spill-2-oth",			/* 0A9	spill 2 other */
203 	"spill-2-oth",			/* 0AA	spill 2 other */
204 	"spill-2-oth",			/* 0AB	spill 2 other */
205 	"spill-3-oth",			/* 0AC	spill 3 other */
206 	"spill-3-oth",			/* 0AD	spill 3 other */
207 	"spill-3-oth",			/* 0AE	spill 3 other */
208 	"spill-3-oth",			/* 0AF	spill 3 other */
209 	"spill-4-oth",			/* 0B0	spill 4 other */
210 	"spill-4-oth",			/* 0B1	spill 4 other */
211 	"spill-4-oth",			/* 0B2	spill 4 other */
212 	"spill-4-oth",			/* 0B3	spill 4 other */
213 	"spill-5-oth",			/* 0B4	spill 5 other */
214 	"spill-5-oth",			/* 0B5	spill 5 other */
215 	"spill-5-oth",			/* 0B6	spill 5 other */
216 	"spill-5-oth",			/* 0B7	spill 5 other */
217 	"spill-6-oth",			/* 0B8	spill 6 other */
218 	"spill-6-oth",			/* 0B9	spill 6 other */
219 	"spill-6-oth",			/* 0BA	spill 6 other */
220 	"spill-6-oth",			/* 0BB	spill 6 other */
221 	"spill-7-oth",			/* 0BC	spill 7 other */
222 	"spill-7-oth",			/* 0BD	spill 7 other */
223 	"spill-7-oth",			/* 0BE	spill 7 other */
224 	"spill-7-oth",			/* 0BF	spill 7 other */
225 	"fill-0-norm",			/* 0C0	fill 0 normal */
226 	"fill-0-norm",			/* 0C1	fill 0 normal */
227 	"fill-0-norm",			/* 0C2	fill 0 normal */
228 	"fill-0-norm",			/* 0C3	fill 0 normal */
229 	"fill-1-norm",			/* 0C4	fill 1 normal */
230 	"fill-1-norm",			/* 0C5	fill 1 normal */
231 	"fill-1-norm",			/* 0C6	fill 1 normal */
232 	"fill-1-norm",			/* 0C7	fill 1 normal */
233 	"fill-2-norm",			/* 0C8	fill 2 normal */
234 	"fill-2-norm",			/* 0C9	fill 2 normal */
235 	"fill-2-norm",			/* 0CA	fill 2 normal */
236 	"fill-2-norm",			/* 0CB	fill 2 normal */
237 	"fill-3-norm",			/* 0CC	fill 3 normal */
238 	"fill-3-norm",			/* 0CD	fill 3 normal */
239 	"fill-3-norm",			/* 0CE	fill 3 normal */
240 	"fill-3-norm",			/* 0CF	fill 3 normal */
241 	"fill-4-norm",			/* 0D0	fill 4 normal */
242 	"fill-4-norm",			/* 0D1	fill 4 normal */
243 	"fill-4-norm",			/* 0D2	fill 4 normal */
244 	"fill-4-norm",			/* 0D3	fill 4 normal */
245 	"fill-5-norm",			/* 0D4	fill 5 normal */
246 	"fill-5-norm",			/* 0D5	fill 5 normal */
247 	"fill-5-norm",			/* 0D6	fill 5 normal */
248 	"fill-5-norm",			/* 0D7	fill 5 normal */
249 	"fill-6-norm",			/* 0D8	fill 6 normal */
250 	"fill-6-norm",			/* 0D9	fill 6 normal */
251 	"fill-6-norm",			/* 0DA	fill 6 normal */
252 	"fill-6-norm",			/* 0DB	fill 6 normal */
253 	"fill-7-norm",			/* 0DC	fill 7 normal */
254 	"fill-7-norm",			/* 0DD	fill 7 normal */
255 	"fill-7-norm",			/* 0DE	fill 7 normal */
256 	"fill-7-norm",			/* 0DF	fill 7 normal */
257 	"fill-0-oth",			/* 0E0	fill 0 other */
258 	"fill-0-oth",			/* 0E1	fill 0 other */
259 	"fill-0-oth",			/* 0E2	fill 0 other */
260 	"fill-0-oth",			/* 0E3	fill 0 other */
261 	"fill-1-oth",			/* 0E4	fill 1 other */
262 	"fill-1-oth",			/* 0E5	fill 1 other */
263 	"fill-1-oth",			/* 0E6	fill 1 other */
264 	"fill-1-oth",			/* 0E7	fill 1 other */
265 	"fill-2-oth",			/* 0E8	fill 2 other */
266 	"fill-2-oth",			/* 0E9	fill 2 other */
267 	"fill-2-oth",			/* 0EA	fill 2 other */
268 	"fill-2-oth",			/* 0EB	fill 2 other */
269 	"fill-3-oth",			/* 0EC	fill 3 other */
270 	"fill-3-oth",			/* 0ED	fill 3 other */
271 	"fill-3-oth",			/* 0EE	fill 3 other */
272 	"fill-3-oth",			/* 0EF	fill 3 other */
273 	"fill-4-oth",			/* 0F0	fill 4 other */
274 	"fill-4-oth",			/* 0F1	fill 4 other */
275 	"fill-4-oth",			/* 0F2	fill 4 other */
276 	"fill-4-oth",			/* 0F3	fill 4 other */
277 	"fill-5-oth",			/* 0F4	fill 5 other */
278 	"fill-5-oth",			/* 0F5	fill 5 other */
279 	"fill-5-oth",			/* 0F6	fill 5 other */
280 	"fill-5-oth",			/* 0F7	fill 5 other */
281 	"fill-6-oth",			/* 0F8	fill 6 other */
282 	"fill-6-oth",			/* 0F9	fill 6 other */
283 	"fill-6-oth",			/* 0FA	fill 6 other */
284 	"fill-6-oth",			/* 0FB	fill 6 other */
285 	"fill-7-oth",			/* 0FC	fill 7 other */
286 	"fill-7-oth",			/* 0FD	fill 7 other */
287 	"fill-7-oth",			/* 0FE	fill 7 other */
288 	"fill-7-oth",			/* 0FF	fill 7 other */
289 	"syscall-4x",			/* 100	old system call */
290 	"usr-brkpt",			/* 101	user breakpoint */
291 	"usr-div-zero",			/* 102	user divide by zero */
292 	"flush-wins",			/* 103	flush windows */
293 	"clean-wins",			/* 104	clean windows */
294 	"range-chk",			/* 105	range check ?? */
295 	"fix-align",			/* 106	do unaligned references */
296 	BAD,				/* 107	unused */
297 	"syscall-32",			/* 108	ILP32 system call on LP64 */
298 	"set-t0-addr",			/* 109	set trap0 address */
299 	BAD, BAD, BAD4,			/* 10A - 10F unused */
300 	BAD4, BAD4, BAD4, BAD4,		/* 110 - 11F unused (V9 user traps?) */
301 	"get-cc",			/* 120	get condition codes */
302 	"set-cc",			/* 121	set condition codes */
303 	"get-psr",			/* 122	get psr */
304 	"set-psr",			/* 123	set psr (some fields) */
305 	"getts",			/* 124	get timestamp */
306 	"gethrvtime",			/* 125	get lwp virtual time */
307 	"self-xcall",			/* 126	self xcall */
308 	"gethrtime",			/* 127	get hrestime */
309 	BAD,				/* 128  unused (ST_SETV9STACK) */
310 	"getlgrp",			/* 129	get lgrpid */
311 	BAD, BAD, BAD4,			/* 12A - 12F unused */
312 	BAD4, BAD4,			/* 130 - 137 unused */
313 	"dtrace-pid",			/* 138  DTrace pid provider */
314 	BAD,				/* 139  unused */
315 	"dtrace-return",		/* 13A  DTrace pid provider */
316 	BAD, BAD4,			/* 13B - 13F unused */
317 	"syscall-64",			/* 140  LP64 system call */
318 	BAD,				/* 141  unused */
319 	"tt-freeze",			/* 142  freeze traptrace */
320 	"tt-unfreeze",			/* 143  unfreeze traptrace */
321 	BAD4, BAD4, BAD4,		/* 144 - 14F unused */
322 	BAD4, BAD4, BAD4, BAD4,		/* 150 - 15F unused */
323 	BAD4, BAD4, BAD4, BAD4,		/* 160 - 16F unused */
324 	BAD4, BAD4, BAD4,		/* 170 - 17B unused */
325 	"ptl1-panic",			/* 17C	test ptl1_panic */
326 	"kmdb-enter",			/* 17D	kmdb enter (L1-A) */
327 	"kmdb-brkpt",			/* 17E	kmdb breakpoint */
328 	"obp-brkpt",			/* 17F	obp breakpoint */
329 #ifdef sun4v
330 	"fast_trap",			/* 180  hypervisor fast trap */
331 	"cpu_tick_npt",			/* 181  cpu_tick_npt() hcall */
332 	"cpu_stick_npt",		/* 182  cpu_stick_npt() hcall */
333 	"mmu_map_addr",			/* 183  mmu_map_addr() hcall */
334 	"mmu_unmap_addr",		/* 184  mmu_unmap_addr() hcall */
335 	"ttrace_addentry",		/* 185  ttrace_addentry() hcall */
336 	NOT, NOT, NOT4, NOT4,		/* 186 - 18F reserved */
337 #else
338 	NOT4, NOT4, NOT4, NOT4,		/* 180 - 18F reserved */
339 #endif
340 	NOT4, NOT4, NOT4, NOT4,		/* 190 - 19F reserved */
341 	NOT4, NOT4, NOT4, NOT4,		/* 1A0 - 1AF reserved */
342 	NOT4, NOT4, NOT4, NOT4,		/* 1B0 - 1BF reserved */
343 	NOT4, NOT4, NOT4, NOT4,		/* 1C0 - 1CF reserved */
344 	NOT4, NOT4, NOT4, NOT4,		/* 1D0 - 1DF reserved */
345 	NOT4, NOT4, NOT4, NOT4,		/* 1E0 - 1EF reserved */
346 	NOT4, NOT4, NOT4, NOT4		/* 1F0 - 1FF reserved */
347 };
348 static const size_t ttndescr = sizeof (ttdescr) / sizeof (ttdescr[0]);
349 
350 static GElf_Sym iv_sym;
351 
352 /*
353  * Persistent data (shouldn't change).
354  */
355 static int ncpu;		/* _ncpu */
356 static ssize_t mbox_size;	/* size of xc_mbox */
357 static ulong_t mbox_stoff;	/* offset of xc_mbox.xc_state */
358 static mdb_ctf_id_t mbox_states; /* xc_state enumeration */
359 
360 static int
361 fetch_ncpu(void)
362 {
363 	if (ncpu == 0)
364 		if (mdb_readsym(&ncpu, sizeof (ncpu), "_ncpu") == -1) {
365 			mdb_warn("symbol '_ncpu' not found");
366 			return (1);
367 		}
368 	return (0);
369 }
370 
371 static int
372 fetch_mbox(void)
373 {
374 	if (mbox_size <= 0) {
375 		mdb_ctf_id_t id;
376 
377 		if (mdb_ctf_lookup_by_name("struct xc_mbox", &id) == -1) {
378 			mdb_warn("couldn't find type 'struct xc_mbox'");
379 			return (1);
380 		}
381 
382 		/*
383 		 * These two could be combined into a single call to
384 		 * mdb_ctf_member_info if xc_state was actually of type
385 		 * enum xc_states.
386 		 */
387 		if (mdb_ctf_lookup_by_name("enum xc_states",
388 		    &mbox_states) == -1) {
389 			mdb_warn("couldn't find type 'enum xc_states'");
390 			return (1);
391 		}
392 		if (mdb_ctf_offsetof(id, "xc_state", &mbox_stoff) == -1) {
393 			mdb_warn("couldn't find 'xc_mbox.xc_state'");
394 			return (1);
395 		}
396 		mbox_stoff /= NBBY;
397 
398 		if ((mbox_size = mdb_ctf_type_size(id)) == -1) {
399 			mdb_warn("couldn't size 'struct xc_mbox'");
400 			return (1);
401 		}
402 	}
403 	return (0);
404 }
405 
406 static int
407 print_range(int start, int end, int separator)
408 {
409 	int	count;
410 	char	tmp;
411 	char	*format;
412 
413 	if (start == end) {
414 		/* Unfortunately, mdb_printf returns void */
415 		format = separator ? ", %d" : "%d";
416 		mdb_printf(format, start);
417 		count = mdb_snprintf(&tmp, 1, format, start);
418 	} else {
419 		format = separator ? ", %d-%d" : "%d-%d";
420 		mdb_printf(format, start, end);
421 		count = mdb_snprintf(&tmp, 1, format, start, end);
422 	}
423 
424 	return (count);
425 }
426 
427 static void
428 print_cpuset_range(ulong_t *cs, int words, int width)
429 {
430 	int i, j;
431 	ulong_t m;
432 	int in = 0;
433 	int start;
434 	int end;
435 	int count = 0;
436 	int sep = 0;
437 
438 	for (i = 0; i < words; i++)
439 		for (j = 0, m = 1; j < BT_NBIPUL; j++, m <<= 1)
440 			if (cs[i] & m) {
441 				if (in == 0) {
442 					start = i * BT_NBIPUL + j;
443 					in = 1;
444 				}
445 			} else {
446 				if (in == 1) {
447 					end = i * BT_NBIPUL + j - 1;
448 					count += print_range(start, end, sep);
449 					sep = 1;
450 					in = 0;
451 				}
452 			}
453 	if (in == 1) {
454 		end = i * BT_NBIPUL - 1;
455 		count += print_range(start, end, sep);
456 	}
457 
458 	while (count++ < width)
459 		mdb_printf(" ");
460 }
461 
462 /*ARGSUSED*/
463 static int
464 cmd_cpuset(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
465 {
466 	uint_t rflag = 0, lflag = 0;
467 	int words;
468 	ulong_t *setp, set = 0;
469 
470 	if (mdb_getopts(argc, argv,
471 	    'l', MDB_OPT_SETBITS, TRUE, &lflag,
472 	    'r', MDB_OPT_SETBITS, TRUE, &rflag,  NULL) != argc)
473 		return (DCMD_USAGE);
474 
475 	if (lflag && rflag)
476 		return (DCMD_USAGE);
477 
478 	if (fetch_ncpu())
479 		return (DCMD_ERR);
480 
481 	if ((words = BT_BITOUL(ncpu)) == 1) {
482 		setp = &set;
483 		mdb_vread(setp, sizeof (ulong_t), addr);
484 	} else {
485 		setp = mdb_alloc(words * sizeof (ulong_t), UM_SLEEP | UM_GC);
486 		mdb_vread(setp, words * sizeof (ulong_t), addr);
487 	}
488 
489 	if (lflag) {
490 		int i, j;
491 		ulong_t m;
492 
493 		for (i = 0; i < words; i++)
494 			for (j = 0, m = 1; j < BT_NBIPUL; j++, m <<= 1)
495 				if (setp[i] & m)
496 					mdb_printf("%r\n", i * BT_NBIPUL + j);
497 	} else if (rflag) {
498 		int i;
499 		int sep = 0;
500 
501 		for (i = 0; i < words; i++) {
502 			mdb_printf(sep ? " %?0lx" : "%?0lx", setp[i]);
503 			sep = 1;
504 		}
505 	} else {
506 		print_cpuset_range(setp, words, 0);
507 	}
508 
509 	return (DCMD_OK);
510 }
511 
512 /*ARGSUSED*/
513 int
514 ttctl(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
515 {
516 	TRAP_TRACE_CTL *ctls, *ctl;
517 	int i, traptrace_buf_inuse = 0;
518 
519 	if (argc != 0)
520 		return (DCMD_USAGE);
521 
522 	if (fetch_ncpu())
523 		return (DCMD_ERR);
524 
525 	ctls = mdb_alloc(sizeof (TRAP_TRACE_CTL) * ncpu, UM_SLEEP | UM_GC);
526 	if (mdb_readsym(ctls, sizeof (TRAP_TRACE_CTL) * ncpu,
527 	    "trap_trace_ctl") == -1) {
528 		mdb_warn("symbol 'trap_trace_ctl' not found");
529 		return (DCMD_ERR);
530 	}
531 
532 	for (ctl = &ctls[0], i = 0; i < ncpu; i++, ctl++) {
533 		if (ctl->d.vaddr_base == 0)
534 			continue;
535 
536 		traptrace_buf_inuse = 1;
537 		mdb_printf("trap_trace_ctl[%d] = {\n", i);
538 		mdb_printf("  vaddr_base = 0x%lx\n", (long)ctl->d.vaddr_base);
539 		mdb_printf("  last_offset = 0x%x\n", ctl->d.last_offset);
540 		mdb_printf("  offset = 0x%x\n", ctl->d.offset);
541 		mdb_printf("  limit = 0x%x\n", ctl->d.limit);
542 		mdb_printf("  paddr_base = 0x%llx\n", ctl->d.paddr_base);
543 		mdb_printf("  asi = 0x%02x\n}\n", ctl->d.asi);
544 	}
545 	if (!traptrace_buf_inuse) {
546 		mdb_warn("traptrace not configured");
547 		return (DCMD_ERR);
548 	}
549 
550 	return (DCMD_OK);
551 }
552 
553 /*ARGSUSED*/
554 static int
555 ttprint_short(uintptr_t addr, const trap_trace_fullrec_t *full, int *cpu)
556 {
557 	const char *ttstr;
558 	const struct trap_trace_record *ttp = &full->ttf_rec;
559 
560 	if (*cpu == -1)
561 		mdb_printf("%3d ", full->ttf_cpu);
562 	else
563 		if (*cpu != full->ttf_cpu)
564 			return (0);
565 
566 	/*
567 	 * Decoding the traptype field is a bit messy.  First we check for
568 	 * several well-defined 16-bit values defined in <sys/traptrace.h>.
569 	 */
570 	switch (ttp->tt_tt) {
571 		case TT_SC_ENTR:
572 			ttstr = "sys-enter";
573 			break;
574 		case TT_SC_RET:
575 			ttstr = "sys-exit";
576 			break;
577 		case TT_SYS_RTT_PROM:
578 			ttstr = "prom_rtt";
579 			break;
580 		case TT_SYS_RTT_PRIV:
581 			ttstr = "priv_rtt";
582 			break;
583 		case TT_SYS_RTT_USER:
584 			ttstr = "user_rtt";
585 			break;
586 		case TT_INTR_EXIT:
587 			ttstr = "int-thr-exit";
588 			break;
589 		default:
590 			/*
591 			 * Next we consider several prefixes (which are
592 			 * typically OR'd with other information such as the
593 			 * %pil or %tt value at the time of the trace).
594 			 */
595 			switch (ttp->tt_tt & 0xff00) {
596 				case TT_SERVE_INTR:
597 					ttstr = "serve-intr";
598 					break;
599 				case TT_XCALL:
600 					ttstr = "xcall";
601 					break;
602 				case TT_XCALL_CONT:
603 					ttstr = "xcall-cont";
604 					break;
605 				case SYSTRAP_TT:
606 					ttstr = "sys_trap";
607 					break;
608 				default:
609 					/*
610 					 * Otherwise we try to convert the
611 					 * tt value to a string using our
612 					 * giant lookup table.
613 					 */
614 					ttstr = ttp->tt_tt < ttndescr ?
615 					    ttdescr[ttp->tt_tt] : "?";
616 			}
617 	}
618 
619 #ifdef sun4v
620 	mdb_printf("%016llx %04hx %-12s  %02x  %02x %0?p %A\n", ttp->tt_tick,
621 	    ttp->tt_tt, ttstr, ttp->tt_tl, ttp->tt_gl,
622 	    ttp->tt_tpc, ttp->tt_tpc);
623 #else
624 	mdb_printf("%016llx %04hx %-12s %04hx %0?p %A\n", ttp->tt_tick,
625 	    ttp->tt_tt, ttstr, ttp->tt_tl, ttp->tt_tpc, ttp->tt_tpc);
626 #endif
627 
628 	return (WALK_NEXT);
629 }
630 
631 /*ARGSUSED*/
632 static int
633 ttprint_long(uintptr_t addr, const trap_trace_fullrec_t *full, int *cpu)
634 {
635 	const struct trap_trace_record *ttp = &full->ttf_rec;
636 
637 	if (*cpu == -1)
638 		mdb_printf("%3d ", full->ttf_cpu);
639 	else if (*cpu != full->ttf_cpu)
640 		return (WALK_NEXT);
641 
642 #ifdef sun4v
643 	mdb_printf("%016llx %016llx %04hx  %02x  %02x %0?p %0?p %0?p "
644 	    "[%p,%p,%p,%p]\n",
645 	    ttp->tt_tick, ttp->tt_tstate, ttp->tt_tt, ttp->tt_tl, ttp->tt_gl,
646 	    ttp->tt_tpc, ttp->tt_sp, ttp->tt_tr,
647 	    ttp->tt_f1, ttp->tt_f2, ttp->tt_f3, ttp->tt_f4);
648 #else
649 	mdb_printf("%016llx %016llx %04hx %04hx %0?p %0?p %0?p [%p,%p,%p,%p]\n",
650 	    ttp->tt_tick, ttp->tt_tstate, ttp->tt_tt, ttp->tt_tl,
651 	    ttp->tt_tpc, ttp->tt_sp, ttp->tt_tr,
652 	    ttp->tt_f1, ttp->tt_f2, ttp->tt_f3, ttp->tt_f4);
653 #endif
654 
655 	return (WALK_NEXT);
656 }
657 
658 typedef struct ttrace_cpu_data {
659 	struct trap_trace_record *tc_buf;
660 	struct trap_trace_record *tc_rec;
661 	struct trap_trace_record *tc_stop;
662 	size_t tc_bufsiz;
663 	uintptr_t tc_base;
664 } ttrace_cpu_data_t;
665 
666 typedef struct ttrace_walk_data {
667 	int tw_ncpu;
668 	ttrace_cpu_data_t *tw_cpus;
669 } ttrace_walk_data_t;
670 
671 int
672 ttrace_walk_init(mdb_walk_state_t *wsp)
673 {
674 	TRAP_TRACE_CTL *ctls, *ctl;
675 	int i, traptrace_buf_inuse = 0;
676 	ttrace_walk_data_t *tw;
677 	ttrace_cpu_data_t *tc;
678 	struct trap_trace_record *buf;
679 
680 	if (wsp->walk_addr != NULL) {
681 		mdb_warn("ttrace only supports global walks\n");
682 		return (WALK_ERR);
683 	}
684 
685 	if (fetch_ncpu())
686 		return (WALK_ERR);
687 
688 	ctls = mdb_alloc(sizeof (TRAP_TRACE_CTL) * ncpu, UM_SLEEP);
689 	if (mdb_readsym(ctls, sizeof (TRAP_TRACE_CTL) * ncpu,
690 	    "trap_trace_ctl") == -1) {
691 		mdb_warn("symbol 'trap_trace_ctl' not found");
692 		mdb_free(ctls, sizeof (TRAP_TRACE_CTL) * ncpu);
693 		return (WALK_ERR);
694 	}
695 
696 	tw = mdb_zalloc(sizeof (ttrace_walk_data_t), UM_SLEEP);
697 	tw->tw_ncpu = ncpu;
698 	tw->tw_cpus = mdb_zalloc(sizeof (ttrace_cpu_data_t) * ncpu, UM_SLEEP);
699 
700 	for (i = 0; i < ncpu; i++) {
701 		ctl = &ctls[i];
702 
703 		if (ctl->d.vaddr_base == 0)
704 			continue;
705 
706 		traptrace_buf_inuse = 1;
707 		tc = &(tw->tw_cpus[i]);
708 		tc->tc_bufsiz = ctl->d.limit -
709 		    sizeof (struct trap_trace_record);
710 		tc->tc_buf = buf = mdb_alloc(tc->tc_bufsiz, UM_SLEEP);
711 		tc->tc_base = (uintptr_t)ctl->d.vaddr_base;
712 
713 		if (mdb_vread(buf, tc->tc_bufsiz, tc->tc_base) == -1) {
714 			mdb_warn("failed to read trap trace buffer at %p",
715 			    ctl->d.vaddr_base);
716 			mdb_free(buf, tc->tc_bufsiz);
717 			tc->tc_buf = NULL;
718 		} else {
719 			tc->tc_rec = (struct trap_trace_record *)
720 			    ((uintptr_t)buf + (uintptr_t)ctl->d.last_offset);
721 			tc->tc_stop = (struct trap_trace_record *)
722 			    ((uintptr_t)buf + (uintptr_t)ctl->d.offset);
723 		}
724 	}
725 	if (!traptrace_buf_inuse) {
726 		mdb_warn("traptrace not configured");
727 		mdb_free(ctls, sizeof (TRAP_TRACE_CTL) * ncpu);
728 		return (DCMD_ERR);
729 	}
730 
731 	mdb_free(ctls, sizeof (TRAP_TRACE_CTL) * ncpu);
732 	wsp->walk_data = tw;
733 	return (WALK_NEXT);
734 }
735 
736 int
737 ttrace_walk_step(mdb_walk_state_t *wsp)
738 {
739 	ttrace_walk_data_t *tw = wsp->walk_data;
740 	ttrace_cpu_data_t *tc;
741 	struct trap_trace_record *rec;
742 	int oldest, i, status;
743 	uint64_t oldest_tick = 0;
744 	int done = 1;
745 	trap_trace_fullrec_t fullrec;
746 
747 	for (i = 0; i < tw->tw_ncpu; i++) {
748 		tc = &(tw->tw_cpus[i]);
749 
750 		if (tc->tc_rec == NULL)
751 			continue;
752 		done = 0;
753 
754 		if (tc->tc_rec->tt_tick == 0)
755 			mdb_warn("Warning: tt_tick == 0\n");
756 
757 		if (tc->tc_rec->tt_tick > oldest_tick) {
758 			oldest_tick = tc->tc_rec->tt_tick;
759 			oldest = i;
760 		}
761 	}
762 
763 	if (done)
764 		return (-1);
765 
766 	tc = &(tw->tw_cpus[oldest]);
767 	rec = tc->tc_rec;
768 
769 	fullrec.ttf_rec = *rec;
770 	fullrec.ttf_cpu = oldest;
771 
772 	if (oldest_tick != 0)
773 		status = wsp->walk_callback((uintptr_t)rec -
774 		    (uintptr_t)tc->tc_buf + tc->tc_base, &fullrec,
775 		    wsp->walk_cbdata);
776 
777 	tc->tc_rec--;
778 
779 	if (tc->tc_rec < tc->tc_buf)
780 		tc->tc_rec = (struct trap_trace_record *)((uintptr_t)
781 		    tc->tc_buf + (uintptr_t)tc->tc_bufsiz -
782 		    sizeof (struct trap_trace_record));
783 
784 	if (tc->tc_rec == tc->tc_stop) {
785 		tc->tc_rec = NULL;
786 		mdb_free(tc->tc_buf, tc->tc_bufsiz);
787 	}
788 
789 	return (status);
790 }
791 
792 void
793 ttrace_walk_fini(mdb_walk_state_t *wsp)
794 {
795 	ttrace_walk_data_t *tw = wsp->walk_data;
796 
797 	mdb_free(tw->tw_cpus, sizeof (ttrace_cpu_data_t) * tw->tw_ncpu);
798 	mdb_free(tw, sizeof (ttrace_walk_data_t));
799 }
800 
801 int
802 ttrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
803 {
804 	uint_t opt_x = FALSE;
805 	int cpu = -1;
806 	mdb_walk_cb_t ttprint;
807 
808 	if (mdb_getopts(argc, argv,
809 	    'x', MDB_OPT_SETBITS, TRUE, &opt_x, NULL) != argc)
810 		return (DCMD_USAGE);
811 
812 	if (flags & DCMD_ADDRSPEC) {
813 		if (fetch_ncpu())
814 			return (DCMD_ERR);
815 		if (addr >= ncpu) {
816 			mdb_warn("expected cpu between 0 and %d\n", ncpu - 1);
817 			return (DCMD_ERR);
818 		}
819 		cpu = (int)addr;
820 	}
821 
822 	if (cpu == -1)
823 		mdb_printf("CPU ");
824 
825 	if (opt_x) {
826 #ifdef sun4v
827 		mdb_printf("%-16s %-16s %-4s %-3s %-3s %-?s %-?s %-?s "
828 		    "F1-4\n", "%tick", "%tstate", "%tt", "%tl", "%gl",
829 		    "%tpc", "%sp", "TR");
830 #else
831 		mdb_printf("%-16s %-16s %-4s %-4s %-?s %-?s %-?s "
832 		    "F1-4\n", "%tick", "%tstate", "%tt", "%tl",
833 		    "%tpc", "%sp", "TR");
834 #endif
835 
836 		ttprint = (mdb_walk_cb_t)ttprint_long;
837 	} else {
838 #ifdef sun4v
839 		mdb_printf("%-16s %-4s %-12s %-3s %-3s %s\n",
840 		    "%tick", "%tt", "", "%tl", "%gl", "%tpc");
841 #else
842 		mdb_printf("%-16s %-4s %-12s %-4s %s\n",
843 		    "%tick", "%tt", "", "%tl", "%tpc");
844 #endif
845 
846 		ttprint = (mdb_walk_cb_t)ttprint_short;
847 	}
848 
849 	if (mdb_walk("ttrace", ttprint, &cpu) == -1) {
850 		mdb_warn("couldn't walk ttrace");
851 		return (DCMD_ERR);
852 	}
853 
854 	return (DCMD_OK);
855 }
856 
857 #ifdef sun4v
858 /*ARGSUSED*/
859 int
860 httctl(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
861 {
862 	TRAP_TRACE_CTL *ctls, *ctl;
863 	int i, htraptrace_buf_inuse = 0;
864 	htrap_trace_hdr_t hdr;
865 
866 	if (argc != 0)
867 		return (DCMD_USAGE);
868 
869 	if (fetch_ncpu())
870 		return (DCMD_ERR);
871 
872 	ctls = mdb_alloc(sizeof (TRAP_TRACE_CTL) * ncpu, UM_SLEEP | UM_GC);
873 	if (mdb_readsym(ctls, sizeof (TRAP_TRACE_CTL) * ncpu,
874 	    "trap_trace_ctl") == -1) {
875 		mdb_warn("symbol 'trap_trace_ctl' not found");
876 		return (DCMD_ERR);
877 	}
878 
879 	for (ctl = &ctls[0], i = 0; i < ncpu; i++, ctl++) {
880 		if (ctl->d.hvaddr_base == 0)
881 			continue;
882 
883 		htraptrace_buf_inuse = 1;
884 		mdb_vread(&hdr, sizeof (htrap_trace_hdr_t),
885 			(uintptr_t)ctl->d.hvaddr_base);
886 		mdb_printf("htrap_trace_ctl[%d] = {\n", i);
887 		mdb_printf("  vaddr_base = 0x%lx\n", (long)ctl->d.hvaddr_base);
888 		mdb_printf("  last_offset = 0x%lx\n", hdr.last_offset);
889 		mdb_printf("  offset = 0x%lx\n", hdr.offset);
890 		mdb_printf("  limit = 0x%x\n", ctl->d.hlimit);
891 		mdb_printf("  paddr_base = 0x%llx\n}\n", ctl->d.hpaddr_base);
892 	}
893 	if (!htraptrace_buf_inuse) {
894 		mdb_warn("hv traptrace not configured");
895 		return (DCMD_ERR);
896 	}
897 
898 	return (DCMD_OK);
899 }
900 
901 /*ARGSUSED*/
902 static int
903 httprint_short(uintptr_t addr, const htrap_trace_fullrec_t *full, int *cpu)
904 {
905 	const char *ttstr;
906 	const struct htrap_trace_record *ttp = &full->ttf_rec;
907 
908 	if (*cpu == -1)
909 		mdb_printf("%3d ", full->ttf_cpu);
910 	else
911 		if (*cpu != full->ttf_cpu)
912 			return (0);
913 
914 	/*
915 	 * Convert the tt value to a string using our gaint lookuo table
916 	 */
917 	ttstr = ttp->tt_tt < ttndescr ? ttdescr[ttp->tt_tt] : "?";
918 
919 	mdb_printf("%016llx %02x  %04hx %04hx %-16s %02x  %02x  %0?p %A\n",
920 		ttp->tt_tick, ttp->tt_ty, ttp->tt_tag, ttp->tt_tt, ttstr,
921 		ttp->tt_tl, ttp->tt_gl, ttp->tt_tpc, ttp->tt_tpc);
922 
923 	return (WALK_NEXT);
924 }
925 
926 /*ARGSUSED*/
927 static int
928 httprint_long(uintptr_t addr, const htrap_trace_fullrec_t *full, int *cpu)
929 {
930 	const struct htrap_trace_record *ttp = &full->ttf_rec;
931 
932 	if (*cpu == -1)
933 		mdb_printf("%3d ", full->ttf_cpu);
934 	else if (*cpu != full->ttf_cpu)
935 		return (WALK_NEXT);
936 
937 	mdb_printf("%016llx %016llx %02x  %02x  %04hx %04hx %02x  %02x  %0?p "
938 		"[%p,%p,%p,%p]\n",
939 		ttp->tt_tick, ttp->tt_tstate, ttp->tt_hpstate, ttp->tt_ty,
940 		ttp->tt_tag, ttp->tt_tt, ttp->tt_tl, ttp->tt_gl, ttp->tt_tpc,
941 		ttp->tt_f1, ttp->tt_f2, ttp->tt_f3, ttp->tt_f4);
942 
943 	return (WALK_NEXT);
944 }
945 
946 typedef struct httrace_cpu_data {
947 	struct htrap_trace_record *tc_buf;
948 	struct htrap_trace_record *tc_rec;
949 	struct htrap_trace_record *tc_stop;
950 	size_t tc_bufsiz;
951 	uintptr_t tc_base;
952 } httrace_cpu_data_t;
953 
954 typedef struct httrace_walk_data {
955 	int tw_ncpu;
956 	httrace_cpu_data_t *tw_cpus;
957 } httrace_walk_data_t;
958 
959 int
960 httrace_walk_init(mdb_walk_state_t *wsp)
961 {
962 	TRAP_TRACE_CTL *ctls, *ctl;
963 	int i, htraptrace_buf_inuse = 0;
964 	httrace_walk_data_t *tw;
965 	httrace_cpu_data_t *tc;
966 	struct htrap_trace_record *buf;
967 	htrap_trace_hdr_t *hdr;
968 
969 	if (wsp->walk_addr != NULL) {
970 		mdb_warn("httrace only supports global walks\n");
971 		return (WALK_ERR);
972 	}
973 
974 	if (fetch_ncpu())
975 		return (WALK_ERR);
976 
977 	ctls = mdb_alloc(sizeof (TRAP_TRACE_CTL) * ncpu, UM_SLEEP);
978 	if (mdb_readsym(ctls, sizeof (TRAP_TRACE_CTL) * ncpu,
979 	    "trap_trace_ctl") == -1) {
980 		mdb_warn("symbol 'trap_trace_ctl' not found");
981 		mdb_free(ctls, sizeof (TRAP_TRACE_CTL) * ncpu);
982 		return (WALK_ERR);
983 	}
984 
985 	tw = mdb_zalloc(sizeof (httrace_walk_data_t), UM_SLEEP);
986 	tw->tw_ncpu = ncpu;
987 	tw->tw_cpus = mdb_zalloc(sizeof (httrace_cpu_data_t) * ncpu, UM_SLEEP);
988 
989 	for (i = 0; i < ncpu; i++) {
990 		ctl = &ctls[i];
991 
992 		if (ctl->d.hvaddr_base == 0)
993 			continue;
994 
995 		htraptrace_buf_inuse = 1;
996 		tc = &(tw->tw_cpus[i]);
997 		tc->tc_bufsiz = ctl->d.hlimit;
998 		tc->tc_buf = buf = mdb_alloc(tc->tc_bufsiz, UM_SLEEP);
999 		tc->tc_base = (uintptr_t)ctl->d.hvaddr_base;
1000 
1001 		if (mdb_vread(buf, tc->tc_bufsiz, tc->tc_base) == -1) {
1002 			mdb_warn("failed to read hv trap trace buffer at %p",
1003 			    ctl->d.hvaddr_base);
1004 			mdb_free(buf, tc->tc_bufsiz);
1005 			tc->tc_buf = NULL;
1006 		} else {
1007 			hdr = (htrap_trace_hdr_t *)buf;
1008 			tc->tc_rec = (struct htrap_trace_record *)
1009 				((uintptr_t)buf + (uintptr_t)hdr->last_offset);
1010 			tc->tc_stop = (struct htrap_trace_record *)
1011 				((uintptr_t)buf + (uintptr_t)hdr->offset);
1012 		}
1013 	}
1014 	if (!htraptrace_buf_inuse) {
1015 		mdb_warn("hv traptrace not configured");
1016 		mdb_free(ctls, sizeof (TRAP_TRACE_CTL) * ncpu);
1017 		return (DCMD_ERR);
1018 	}
1019 
1020 	mdb_free(ctls, sizeof (TRAP_TRACE_CTL) * ncpu);
1021 	wsp->walk_data = tw;
1022 	return (WALK_NEXT);
1023 }
1024 
1025 int
1026 httrace_walk_step(mdb_walk_state_t *wsp)
1027 {
1028 	httrace_walk_data_t *tw = wsp->walk_data;
1029 	httrace_cpu_data_t *tc;
1030 	struct htrap_trace_record *rec;
1031 	int oldest, i, status;
1032 	uint64_t oldest_tick = 0;
1033 	int done = 1;
1034 	htrap_trace_fullrec_t fullrec;
1035 
1036 	for (i = 0; i < tw->tw_ncpu; i++) {
1037 		tc = &(tw->tw_cpus[i]);
1038 
1039 		if (tc->tc_rec == NULL)
1040 			continue;
1041 		done = 0;
1042 
1043 		if (tc->tc_rec->tt_tick == 0)
1044 			mdb_warn("Warning: tt_tick == 0\n");
1045 
1046 		if (tc->tc_rec->tt_tick >= oldest_tick) {
1047 			oldest_tick = tc->tc_rec->tt_tick;
1048 			oldest = i;
1049 		}
1050 	}
1051 
1052 	if (done)
1053 		return (-1);
1054 
1055 	tc = &(tw->tw_cpus[oldest]);
1056 	rec = tc->tc_rec;
1057 
1058 	fullrec.ttf_rec = *rec;
1059 	fullrec.ttf_cpu = oldest;
1060 
1061 	if (oldest_tick != 0)
1062 		status = wsp->walk_callback((uintptr_t)rec -
1063 		    (uintptr_t)tc->tc_buf + tc->tc_base, &fullrec,
1064 		    wsp->walk_cbdata);
1065 
1066 	tc->tc_rec--;
1067 
1068 	/* first record of the trap trace buffer is trap trace header */
1069 	if (tc->tc_rec == tc->tc_buf)
1070 		tc->tc_rec = (struct htrap_trace_record *)((uintptr_t)
1071 		    tc->tc_buf + (uintptr_t)tc->tc_bufsiz -
1072 		    sizeof (struct htrap_trace_record));
1073 
1074 	if (tc->tc_rec == tc->tc_stop) {
1075 		tc->tc_rec = NULL;
1076 		mdb_free(tc->tc_buf, tc->tc_bufsiz);
1077 	}
1078 
1079 	return (status);
1080 }
1081 
1082 void
1083 httrace_walk_fini(mdb_walk_state_t *wsp)
1084 {
1085 	httrace_walk_data_t *tw = wsp->walk_data;
1086 
1087 	mdb_free(tw->tw_cpus, sizeof (httrace_cpu_data_t) * tw->tw_ncpu);
1088 	mdb_free(tw, sizeof (httrace_walk_data_t));
1089 }
1090 
1091 int
1092 httrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1093 {
1094 	uint_t opt_x = FALSE;
1095 	int cpu = -1;
1096 	mdb_walk_cb_t ttprint;
1097 
1098 	if (mdb_getopts(argc, argv,
1099 	    'x', MDB_OPT_SETBITS, TRUE, &opt_x, NULL) != argc)
1100 		return (DCMD_USAGE);
1101 
1102 	if (flags & DCMD_ADDRSPEC) {
1103 		if (fetch_ncpu())
1104 			return (DCMD_ERR);
1105 		if (addr >= ncpu) {
1106 			mdb_warn("expected cpu between 0 and %d\n", ncpu - 1);
1107 			return (DCMD_ERR);
1108 		}
1109 		cpu = (int)addr;
1110 	}
1111 
1112 	if (cpu == -1)
1113 		mdb_printf("CPU ");
1114 
1115 	if (opt_x) {
1116 		mdb_printf("%-16s %-16s %-3s %-3s %-4s %-4s %-3s %-3s %-?s "
1117 			"F1-4\n", "%tick", "%tstate", "%hp", "%ty", "%tag",
1118 			"%tt", "%tl", "%gl", "%tpc");
1119 		ttprint = (mdb_walk_cb_t)httprint_long;
1120 	} else {
1121 		mdb_printf("%-16s %-3s %-4s %-4s %-16s %-3s %-3s %s\n",
1122 			"%tick", "%ty", "%tag", "%tt", "", "%tl", "%gl",
1123 			"%tpc");
1124 		ttprint = (mdb_walk_cb_t)httprint_short;
1125 	}
1126 
1127 	if (mdb_walk("httrace", ttprint, &cpu) == -1) {
1128 		mdb_warn("couldn't walk httrace");
1129 		return (DCMD_ERR);
1130 	}
1131 
1132 	return (DCMD_OK);
1133 }
1134 #endif
1135 
1136 struct {
1137 	int xc_type;
1138 	const char *xc_str;
1139 } xc_data[] = {
1140 	{ XT_ONE_SELF,		"xt-one-self" },
1141 	{ XT_ONE_OTHER,		"xt-one-other" },
1142 	{ XT_SOME_SELF,		"xt-some-self" },
1143 	{ XT_SOME_OTHER,	"xt-some-other" },
1144 	{ XT_ALL_SELF,		"xt-all-self" },
1145 	{ XT_ALL_OTHER,		"xt-all-other" },
1146 	{ XC_ONE_SELF,		"xc-one-self" },
1147 	{ XC_ONE_OTHER,		"xc-one-other" },
1148 	{ XC_ONE_OTHER_H,	"xc-one-other-h" },
1149 	{ XC_SOME_SELF,		"xc-some-self" },
1150 	{ XC_SOME_OTHER,	"xc-some-other" },
1151 	{ XC_SOME_OTHER_H,	"xc-some-other-h" },
1152 	{ XC_ALL_SELF,		"xc-all-self" },
1153 	{ XC_ALL_OTHER,		"xc-all-other" },
1154 	{ XC_ALL_OTHER_H,	"xc-all-other-h" },
1155 	{ XC_ATTENTION,		"xc-attention" },
1156 	{ XC_DISMISSED,		"xc-dismissed" },
1157 	{ XC_LOOP_ENTER,	"xc-loop-enter" },
1158 	{ XC_LOOP_DOIT,		"xc-loop-doit" },
1159 	{ XC_LOOP_EXIT,		"xc-loop-exit" },
1160 	{ 0,			NULL }
1161 };
1162 
1163 /*ARGSUSED*/
1164 int
1165 xctrace_walk(uintptr_t addr, const trap_trace_fullrec_t *full, int *cpu)
1166 {
1167 	const struct trap_trace_record *ttp = &full->ttf_rec;
1168 	int i, type = ttp->tt_tt & 0xff;
1169 	const char *str = "???";
1170 
1171 	if ((ttp->tt_tt & 0xff00) == TT_XCALL) {
1172 		for (i = 0; xc_data[i].xc_str != NULL; i++) {
1173 			if (xc_data[i].xc_type == type) {
1174 				str = xc_data[i].xc_str;
1175 				break;
1176 			}
1177 		}
1178 	} else if ((ttp->tt_tt & 0xff00) == TT_XCALL_CONT) {
1179 		str = "xcall-cont";
1180 		mdb_printf("%3d %016llx %-16s %08x %08x %08x %08x\n",
1181 		    full->ttf_cpu, ttp->tt_tick, str, ttp->tt_f1, ttp->tt_f2,
1182 		    ttp->tt_f3, ttp->tt_f4);
1183 		return (WALK_NEXT);
1184 	} else if (ttp->tt_tt == 0x60) {
1185 		str = "int-vec";
1186 	} else {
1187 		return (WALK_NEXT);
1188 	}
1189 
1190 	mdb_printf("%3d %016llx %-16s %08x %a\n", full->ttf_cpu,
1191 	    ttp->tt_tick, str, ttp->tt_sp, ttp->tt_tr);
1192 
1193 	return (WALK_NEXT);
1194 }
1195 
1196 /*ARGSUSED*/
1197 int
1198 xctrace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1199 {
1200 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
1201 		return (DCMD_USAGE);
1202 
1203 	if (mdb_walk("ttrace", (mdb_walk_cb_t)xctrace_walk, NULL) == -1) {
1204 		mdb_warn("couldn't walk ttrace");
1205 		return (DCMD_ERR);
1206 	}
1207 
1208 	return (DCMD_OK);
1209 }
1210 
1211 /*
1212  * Grrr... xc_mbox isn't in an _impl header file; we define it here.
1213  */
1214 typedef struct xc_mbox {
1215 	xcfunc_t *xc_func;
1216 	uint64_t xc_arg1;
1217 	uint64_t xc_arg2;
1218 	cpuset_t xc_cpuset;
1219 	volatile uint_t xc_state;
1220 } xc_mbox_t;
1221 
1222 typedef struct xc_mbox_walk {
1223 	int xw_ndx;
1224 	uintptr_t xw_addr;
1225 	xc_mbox_t *xw_mbox;
1226 } xc_mbox_walk_t;
1227 
1228 static int
1229 xc_mbox_walk_init(mdb_walk_state_t *wsp)
1230 {
1231 	GElf_Sym sym;
1232 	xc_mbox_walk_t *xw;
1233 
1234 	if (mdb_lookup_by_name("xc_mbox", &sym) == -1) {
1235 		mdb_warn("couldn't find 'xc_mbox'");
1236 		return (WALK_ERR);
1237 	}
1238 
1239 	if (fetch_ncpu() || fetch_mbox())
1240 		return (WALK_ERR);
1241 
1242 	xw = mdb_zalloc(sizeof (xc_mbox_walk_t), UM_SLEEP);
1243 	xw->xw_mbox = mdb_zalloc(mbox_size * ncpu, UM_SLEEP);
1244 
1245 	if (mdb_readsym(xw->xw_mbox, mbox_size * ncpu, "xc_mbox") == -1) {
1246 		mdb_warn("couldn't read 'xc_mbox'");
1247 		mdb_free(xw->xw_mbox, mbox_size * ncpu);
1248 		mdb_free(xw, sizeof (xc_mbox_walk_t));
1249 		return (WALK_ERR);
1250 	}
1251 
1252 	xw->xw_addr = sym.st_value;
1253 	wsp->walk_data = xw;
1254 
1255 	return (WALK_NEXT);
1256 }
1257 
1258 static int
1259 xc_mbox_walk_step(mdb_walk_state_t *wsp)
1260 {
1261 	xc_mbox_walk_t *xw = wsp->walk_data;
1262 	int status;
1263 
1264 	if (xw->xw_ndx == ncpu)
1265 		return (WALK_DONE);
1266 
1267 	status = wsp->walk_callback(xw->xw_addr,
1268 	    &xw->xw_mbox[xw->xw_ndx++], wsp->walk_cbdata);
1269 
1270 	xw->xw_addr += mbox_size;
1271 	return (status);
1272 }
1273 
1274 static void
1275 xc_mbox_walk_fini(mdb_walk_state_t *wsp)
1276 {
1277 	xc_mbox_walk_t *xw = wsp->walk_data;
1278 
1279 	mdb_free(xw->xw_mbox, mbox_size * ncpu);
1280 	mdb_free(xw, sizeof (xc_mbox_walk_t));
1281 }
1282 
1283 static int
1284 xc_mbox(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1285 {
1286 	xc_mbox_t *mbox;
1287 	GElf_Sym sym;
1288 	const char *state;
1289 
1290 	if (argc != 0)
1291 		return (DCMD_USAGE);
1292 
1293 	if (!(flags & DCMD_ADDRSPEC)) {
1294 		if (mdb_walk_dcmd("xc_mbox", "xc_mbox", argc, argv) == -1) {
1295 			mdb_warn("can't walk 'xc_mbox'");
1296 			return (DCMD_ERR);
1297 		}
1298 		return (DCMD_OK);
1299 	}
1300 
1301 	if (fetch_ncpu() || fetch_mbox())
1302 		return (DCMD_ERR);
1303 
1304 	if (DCMD_HDRSPEC(flags)) {
1305 		mdb_printf("%3s %-8s %-8s %-9s %-16s %-16s %s\n",
1306 		    "CPU", "ADDR", "STATE", "CPUSET", "ARG1", "ARG2", "HNDLR");
1307 	}
1308 
1309 	mbox = mdb_alloc(mbox_size, UM_SLEEP | UM_GC);
1310 	if (mdb_vread(mbox, mbox_size, addr) == -1) {
1311 		mdb_warn("couldn't read xc_mbox at %p", addr);
1312 		return (DCMD_ERR);
1313 	}
1314 
1315 	if (mbox->xc_func == NULL)
1316 		return (DCMD_OK);
1317 
1318 	if (mdb_lookup_by_name("xc_mbox", &sym) == -1) {
1319 		mdb_warn("couldn't read 'xc_mbox'");
1320 		return (DCMD_ERR);
1321 	}
1322 
1323 	state = mdb_ctf_enum_name(mbox_states,
1324 	    /* LINTED - alignment */
1325 	    *(int *)((char *)mbox + mbox_stoff));
1326 
1327 	mdb_printf("%3d %08x %-8s [ ",
1328 	    (int)((addr - sym.st_value) / mbox_size), addr,
1329 	    state ? state : "XC_???");
1330 
1331 	print_cpuset_range((ulong_t *)&mbox->xc_cpuset, BT_BITOUL(ncpu), 5);
1332 
1333 	mdb_printf(" ] %-16a %-16a %a\n",
1334 	    mbox->xc_arg1, mbox->xc_arg2, mbox->xc_func);
1335 
1336 	return (DCMD_OK);
1337 }
1338 
1339 typedef struct vecint_walk_data {
1340 	intr_vec_t **vec_table;
1341 	uintptr_t vec_base;
1342 	size_t vec_idx;
1343 	size_t vec_size;
1344 } vecint_walk_data_t;
1345 
1346 int
1347 vecint_walk_init(mdb_walk_state_t *wsp)
1348 {
1349 	vecint_walk_data_t	*vecint;
1350 
1351 	if (wsp->walk_addr != NULL) {
1352 		mdb_warn("vecint walk only supports global walks\n");
1353 		return (WALK_ERR);
1354 	}
1355 
1356 	vecint = mdb_zalloc(sizeof (vecint_walk_data_t), UM_SLEEP);
1357 
1358 	vecint->vec_size = MAXIVNUM * sizeof (intr_vec_t *);
1359 	vecint->vec_base = (uintptr_t)iv_sym.st_value;
1360 	vecint->vec_table = mdb_zalloc(vecint->vec_size, UM_SLEEP);
1361 
1362 	if (mdb_vread(vecint->vec_table, vecint->vec_size,
1363 	    vecint->vec_base) == -1) {
1364 		mdb_warn("couldn't read intr_vec_table");
1365 		mdb_free(vecint->vec_table, vecint->vec_size);
1366 		mdb_free(vecint, sizeof (vecint_walk_data_t));
1367 		return (WALK_ERR);
1368 	}
1369 
1370 	wsp->walk_data = vecint;
1371 	return (WALK_NEXT);
1372 }
1373 
1374 int
1375 vecint_walk_step(mdb_walk_state_t *wsp)
1376 {
1377 	vecint_walk_data_t	*vecint = (vecint_walk_data_t *)wsp->walk_data;
1378 	size_t			max = vecint->vec_size / sizeof (intr_vec_t *);
1379 	intr_vec_t		iv;
1380 	int			status;
1381 
1382 	if (wsp->walk_addr == NULL) {
1383 		while ((vecint->vec_idx < max) && ((wsp->walk_addr =
1384 		    (uintptr_t)vecint->vec_table[vecint->vec_idx++]) == NULL))
1385 			continue;
1386 	}
1387 
1388 	if (wsp->walk_addr == NULL)
1389 		return (WALK_DONE);
1390 
1391 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
1392 	    wsp->walk_cbdata);
1393 
1394 	if (mdb_vread(&iv, sizeof (intr_vec_t),
1395 	    (uintptr_t)wsp->walk_addr) == -1) {
1396 		mdb_warn("failed to read iv_p %p\n", wsp->walk_addr);
1397 		return (WALK_ERR);
1398 	}
1399 
1400 	wsp->walk_addr = (uintptr_t)iv.iv_vec_next;
1401 	return (status);
1402 }
1403 
1404 void
1405 vecint_walk_fini(mdb_walk_state_t *wsp)
1406 {
1407 	vecint_walk_data_t	*vecint = wsp->walk_data;
1408 
1409 	mdb_free(vecint->vec_table, vecint->vec_size);
1410 	mdb_free(vecint, sizeof (vecint_walk_data_t));
1411 }
1412 
1413 int
1414 vecint_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1415 {
1416 	intr_vec_t	iv;
1417 
1418 	if (!(flags & DCMD_ADDRSPEC)) {
1419 		if (mdb_walk_dcmd("vecint", "vecint", argc, argv) == -1) {
1420 			mdb_warn("can't walk vecint");
1421 			return (DCMD_ERR);
1422 		}
1423 		return (DCMD_OK);
1424 	}
1425 
1426 	if (DCMD_HDRSPEC(flags)) {
1427 		mdb_printf("%4s %?s %4s %?s %?s %s\n", "INUM", "ADDR",
1428 		    "PIL", "ARG1", "ARG2", "HANDLER");
1429 	}
1430 
1431 	if (mdb_vread(&iv, sizeof (iv), addr) == -1) {
1432 		mdb_warn("couldn't read intr_vec_table at %p", addr);
1433 		return (DCMD_ERR);
1434 	}
1435 
1436 	mdb_printf("%4x %?p %4d %?p %?p %a\n", iv.iv_inum, addr,
1437 	    iv.iv_pil, iv.iv_arg1, iv.iv_arg2, iv.iv_handler);
1438 
1439 	return (DCMD_OK);
1440 }
1441 
1442 int
1443 softint_walk_init(mdb_walk_state_t *wsp)
1444 {
1445 	intr_vec_t	*list;
1446 
1447 	if (wsp->walk_addr != NULL) {
1448 		mdb_warn("softint walk only supports global walks\n");
1449 		return (WALK_ERR);
1450 	}
1451 
1452 	/* Read global softint linked list pointer */
1453 	if (mdb_readvar(&list, "softint_list") == -1) {
1454 		mdb_warn("failed to read the global softint_list pointer\n");
1455 		return (WALK_ERR);
1456 	}
1457 
1458 	wsp->walk_addr = (uintptr_t)list;
1459 	return (WALK_NEXT);
1460 }
1461 
1462 /*ARGSUSED*/
1463 void
1464 softint_walk_fini(mdb_walk_state_t *wsp)
1465 {
1466 	/* Nothing to do here */
1467 }
1468 
1469 int
1470 softint_walk_step(mdb_walk_state_t *wsp)
1471 {
1472 	intr_vec_t		iv;
1473 	int			status;
1474 
1475 	if (wsp->walk_addr == NULL)
1476 		return (WALK_DONE);
1477 
1478 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
1479 	    wsp->walk_cbdata);
1480 
1481 	if (mdb_vread(&iv, sizeof (intr_vec_t),
1482 	    (uintptr_t)wsp->walk_addr) == -1) {
1483 		mdb_warn("failed to read iv_p %p\n", wsp->walk_addr);
1484 		return (WALK_ERR);
1485 	}
1486 
1487 	wsp->walk_addr = (uintptr_t)iv.iv_vec_next;
1488 	return (status);
1489 }
1490 
1491 int
1492 softint_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1493 {
1494 	intr_vec_t	iv;
1495 
1496 	if (!(flags & DCMD_ADDRSPEC)) {
1497 		if (mdb_walk_dcmd("softint", "softint", argc, argv) == -1) {
1498 			mdb_warn("can't walk softint");
1499 			return (DCMD_ERR);
1500 		}
1501 		return (DCMD_OK);
1502 	}
1503 
1504 	if (DCMD_HDRSPEC(flags)) {
1505 		mdb_printf("%?s %4s %4s %4s %?s %?s %s\n", "ADDR", "TYPE",
1506 		    "PEND", "PIL", "ARG1", "ARG2", "HANDLER");
1507 	}
1508 
1509 	if (mdb_vread(&iv, sizeof (iv), addr) == -1) {
1510 		mdb_warn("couldn't read softint at %p", addr);
1511 		return (DCMD_ERR);
1512 	}
1513 
1514 	mdb_printf("%?p %4s %4d %4d %?p %?p %a\n", addr,
1515 	    (iv.iv_flags & IV_SOFTINT_MT) ? "M" : "S",
1516 	    iv.iv_flags & IV_SOFTINT_PEND, iv.iv_pil,
1517 	    iv.iv_arg1, iv.iv_arg2, iv.iv_handler);
1518 
1519 	return (DCMD_OK);
1520 }
1521 
1522 static int
1523 whatis_walk_tt(uintptr_t taddr, const trap_trace_fullrec_t *ttf, uintptr_t *ap)
1524 {
1525 	uintptr_t addr = *ap;
1526 
1527 	if (addr < taddr || addr > taddr + sizeof (struct trap_trace_record))
1528 		return (WALK_NEXT);
1529 
1530 	mdb_printf("%p is %p+%p, trap trace record for cpu %d\n",
1531 	    addr, taddr, addr - taddr, ttf->ttf_cpu);
1532 
1533 	mdb_set_dot(1);
1534 	return (WALK_DONE);
1535 }
1536 
1537 /*ARGSUSED*/
1538 int
1539 whatis(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1540 {
1541 	GElf_Sym sym;
1542 
1543 	if (!(flags & DCMD_ADDRSPEC))
1544 		return (DCMD_USAGE);
1545 
1546 	if (mdb_lookup_by_name("trap_trace_ctl", &sym) == -1)
1547 		return (DCMD_NEXT);
1548 
1549 	mdb_set_dot(0);
1550 
1551 	if (mdb_walk("ttrace", (mdb_walk_cb_t)whatis_walk_tt, &addr) == -1)
1552 		mdb_warn("failed to walk 'ttrace'");
1553 
1554 	return (DCMD_NEXT);
1555 }
1556 
1557 /*ARGSUSED*/
1558 int
1559 mutex_owner_init(mdb_walk_state_t *wsp)
1560 {
1561 	return (WALK_NEXT);
1562 }
1563 
1564 int
1565 mutex_owner_step(mdb_walk_state_t *wsp)
1566 {
1567 	uintptr_t addr = wsp->walk_addr;
1568 	mutex_impl_t mtx;
1569 	uintptr_t owner;
1570 	kthread_t thr;
1571 
1572 	if (mdb_vread(&mtx, sizeof (mtx), addr) == -1)
1573 		return (WALK_ERR);
1574 
1575 	if (!MUTEX_TYPE_ADAPTIVE(&mtx))
1576 		return (WALK_DONE);
1577 
1578 	if ((owner = (uintptr_t)MUTEX_OWNER(&mtx)) == NULL)
1579 		return (WALK_DONE);
1580 
1581 	if (mdb_vread(&thr, sizeof (thr), owner) != -1)
1582 		(void) wsp->walk_callback(owner, &thr, wsp->walk_cbdata);
1583 
1584 	return (WALK_DONE);
1585 }
1586 
1587 static const mdb_dcmd_t dcmds[] = {
1588 	{ "cpuset", ":[-l|-r]", "dump a cpuset_t", cmd_cpuset },
1589 	{ "ttctl", NULL, "dump trap trace ctl records", ttctl },
1590 	{ "ttrace", "[-x]", "dump trap trace buffer for a cpu", ttrace },
1591 #ifdef sun4v
1592 	{ "httctl", NULL, "dump hv trap trace ctl records", httctl },
1593 	{ "httrace", "[-x]", "dump hv trap trace buffer for a cpu", httrace },
1594 #endif
1595 	{ "xc_mbox", "?", "dump xcall mboxes", xc_mbox },
1596 	{ "xctrace", NULL, "dump xcall trace buffer", xctrace },
1597 	{ "vecint", NULL, "display a registered hardware interrupt",
1598 	    vecint_dcmd },
1599 	{ "softint", NULL, "display a registered software interrupt",
1600 	    softint_dcmd },
1601 	{ "whatis", ":[-abv]", "given an address, return information", whatis },
1602 	{ "sfmmu_vtop", ":[[-v] -a as]", "print virtual to physical mapping",
1603 	    sfmmu_vtop },
1604 	{ "page_num2pp", ":", "page frame number to page structure",
1605 	    page_num2pp },
1606 	{ "memseg_list", ":", "show memseg list", memseg_list },
1607 	{ "tsbinfo", ":[-l [-a]]", "show tsbinfo", tsbinfo_list,
1608 	    tsbinfo_help },
1609 	{ NULL }
1610 };
1611 
1612 static const mdb_walker_t walkers[] = {
1613 	{ "mutex_owner", "walks the owner of a mutex",
1614 		mutex_owner_init, mutex_owner_step },
1615 	{ "ttrace", "walks the trap trace buffer for a CPU",
1616 		ttrace_walk_init, ttrace_walk_step, ttrace_walk_fini },
1617 #ifdef sun4v
1618 	{ "httrace", "walks the hv trap trace buffer for a CPU",
1619 		httrace_walk_init, httrace_walk_step, httrace_walk_fini },
1620 #endif
1621 	{ "xc_mbox", "walks the cross call mail boxes",
1622 		xc_mbox_walk_init, xc_mbox_walk_step, xc_mbox_walk_fini },
1623 	{ "vecint", "walk the list of registered hardware interrupts",
1624 		vecint_walk_init, vecint_walk_step, vecint_walk_fini },
1625 	{ "softint", "walk the list of registered software interrupts",
1626 		softint_walk_init, softint_walk_step, softint_walk_fini },
1627 	{ "memseg", "walk the memseg structures",
1628 		memseg_walk_init, memseg_walk_step, memseg_walk_fini },
1629 	{ NULL }
1630 };
1631 
1632 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
1633 
1634 const mdb_modinfo_t *
1635 _mdb_init(void)
1636 {
1637 	if (mdb_lookup_by_name("intr_vec_table", &iv_sym) == -1) {
1638 		mdb_warn("couldn't find intr_vec_table");
1639 		return (NULL);
1640 	}
1641 
1642 	return (&modinfo);
1643 }
1644