xref: /titanic_51/usr/src/cmd/mdb/sun4u/modules/unix/unix.c (revision ccbf80fa3b6bf6b986dca9037e5ad9d6c9f9fa65)
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/ivintr.h>
43 #include <sys/mutex_impl.h>
44 
45 #include <mdb/mdb_modapi.h>
46 #include <mdb/mdb_ctf.h>
47 #include "sfmmu.h"
48 
49 #ifndef SYSTRAP_TT
50 #define	SYSTRAP_TT	0x1300
51 #endif
52 
53 typedef struct trap_trace_fullrec {
54 	struct trap_trace_record ttf_rec;
55 	int ttf_cpu;
56 } trap_trace_fullrec_t;
57 
58 #ifdef sun4v
59 typedef struct htrap_trace_fullrec {
60 	struct htrap_trace_record ttf_rec;
61 	int ttf_cpu;
62 } htrap_trace_fullrec_t;
63 #endif
64 
65 /*
66  * These strings and accompanying macros allow our string table to look
67  * just like the real table in trap_table.s.
68  */
69 
70 static const char NOT[] = "reserved";	/* common reserved string */
71 static const char BAD[] = "unused";	/* common unused string */
72 
73 #define	NOT4	NOT, NOT, NOT, NOT
74 #define	BAD4	BAD, BAD, BAD, BAD
75 
76 static const char *const ttdescr[] = {
77 	NOT,				/* 000	reserved */
78 	"power-on",			/* 001	power on reset */
79 	"watchdog",			/* 002	watchdog reset */
80 	"xir",				/* 003	externally initiated reset */
81 	"sir",				/* 004	software initiated reset */
82 	"red",				/* 005	red mode exception */
83 	NOT, NOT,			/* 006 - 007 reserved */
84 	"immu-xcp",			/* 008	instruction access exception */
85 	"immu-miss",			/* 009	instruction access MMU miss */
86 	"immu-err",			/* 00A	instruction access error */
87 	NOT, NOT4,			/* 00B - 00F reserved */
88 	"ill-inst",			/* 010	illegal instruction */
89 	"priv-inst",			/* 011	privileged opcode */
90 	"unimp-ldd",			/* 012	unimplemented LDD */
91 	"unimp-std",			/* 013	unimplemented STD */
92 	NOT4, NOT4, NOT4,		/* 014 - 01F reserved */
93 	"fp-disable",			/* 020	fp disabled */
94 	"fp-ieee754",			/* 021	fp exception ieee 754 */
95 	"fp-xcp-other",			/* 022	fp exception other */
96 	"tag-oflow",			/* 023	tag overflow */
97 	"cleanwin",			/* 024	clean window */
98 	"cleanwin",			/* 025	clean window */
99 	"cleanwin",			/* 026	clean window */
100 	"cleanwin",			/* 027	clean window */
101 	"div-zero",			/* 028	division by zero */
102 	"internal-err",			/* 029	internal processor error */
103 	NOT, NOT, NOT4,			/* 02A - 02F reserved */
104 	"dmmu-xcp",			/* 030	data access exception */
105 	"dmmu-miss",			/* 031	data access MMU miss */
106 	"dmmu-err",			/* 032	data access error */
107 	"dmmu-prot",			/* 033	data access protection */
108 	"unalign",			/* 034	mem address not aligned */
109 	"lddf-unalign",			/* 035	LDDF mem address not aligned */
110 	"stdf-unalign",			/* 036	STDF mem address not aligned */
111 	"priv-act",			/* 037	privileged action */
112 	"ldqf-unalign",			/* 038	LDQF mem address not aligned */
113 	"stqf-unalign",			/* 039	STQF mem address not aligned */
114 	NOT, NOT, NOT4,			/* 03A - 03F reserved */
115 	"async-d-err",			/* 040	async data error */
116 	"level-1",			/* 041	interrupt level 1 */
117 	"level-2",			/* 042	interrupt level 2 */
118 	"level-3",			/* 043	interrupt level 3 */
119 	"level-4",			/* 044	interrupt level 4 */
120 	"level-5",			/* 045	interrupt level 5 */
121 	"level-6",			/* 046	interrupt level 6 */
122 	"level-7",			/* 047	interrupt level 7 */
123 	"level-8",			/* 048	interrupt level 8 */
124 	"level-9",			/* 049	interrupt level 9 */
125 	"level-10",			/* 04A	interrupt level 10 */
126 	"level-11",			/* 04B	interrupt level 11 */
127 	"level-12",			/* 04C	interrupt level 12 */
128 	"level-13",			/* 04D	interrupt level 13 */
129 	"level-14",			/* 04E	interrupt level 14 */
130 	"level-15",			/* 04F	interrupt level 15 */
131 	NOT4, NOT4, NOT4, NOT4,		/* 050 - 05F reserved */
132 	"int-vec",			/* 060	interrupt vector */
133 	"pa-watch",			/* 061	PA watchpoint */
134 	"va-watch",			/* 062	VA watchpoint */
135 	"ecc-err",			/* 063	corrected ECC error */
136 	"itlb-miss",			/* 064	instruction access MMU miss */
137 	"itlb-miss",			/* 065	instruction access MMU miss */
138 	"itlb-miss",			/* 066	instruction access MMU miss */
139 	"itlb-miss",			/* 067	instruction access MMU miss */
140 	"dtlb-miss",			/* 068	data access MMU miss */
141 	"dtlb-miss",			/* 069	data access MMU miss */
142 	"dtlb-miss",			/* 06A	data access MMU miss */
143 	"dtlb-miss",			/* 06B	data access MMU miss */
144 	"dtlb-prot",			/* 06C	data access protection */
145 	"dtlb-prot",			/* 06D	data access protection */
146 	"dtlb-prot",			/* 06E	data access protection */
147 	"dtlb-prot",			/* 06F	data access protection */
148 	"fast-ecc-err",			/* 070	fast ecache ECC error */
149 	"dp-err",			/* 071	data cache parity error */
150 	"ip-err",			/* 072	instr cache parity error */
151 	NOT, NOT4, NOT4,		/* 073 - 07B reserved */
152 #ifdef sun4v
153 	"cpu-mondo",			/* 07C  CPU mondo */
154 	"dev-mondo",			/* 07D  device mondo */
155 	"res.-err",			/* 07E  resumable error */
156 	"non-res.-err",			/* 07F  non-resumable error */
157 #else
158 	NOT4,				/* 07C - 07F reserved */
159 #endif
160 	"spill-0-norm",			/* 080	spill 0 normal */
161 	"spill-0-norm",			/* 081	spill 0 normal */
162 	"spill-0-norm",			/* 082	spill 0 normal */
163 	"spill-0-norm",			/* 083	spill 0 normal */
164 	"spill-1-norm",			/* 084	spill 1 normal */
165 	"spill-1-norm",			/* 085	spill 1 normal */
166 	"spill-1-norm",			/* 086	spill 1 normal */
167 	"spill-1-norm",			/* 087	spill 1 normal */
168 	"spill-2-norm",			/* 088	spill 2 normal */
169 	"spill-2-norm",			/* 089	spill 2 normal */
170 	"spill-2-norm",			/* 08A	spill 2 normal */
171 	"spill-2-norm",			/* 08B	spill 2 normal */
172 	"spill-3-norm",			/* 08C	spill 3 normal */
173 	"spill-3-norm",			/* 08D	spill 3 normal */
174 	"spill-3-norm",			/* 08E	spill 3 normal */
175 	"spill-3-norm",			/* 08F	spill 3 normal */
176 	"spill-4-norm",			/* 090	spill 4 normal */
177 	"spill-4-norm",			/* 091	spill 4 normal */
178 	"spill-4-norm",			/* 092	spill 4 normal */
179 	"spill-4-norm",			/* 093	spill 4 normal */
180 	"spill-5-norm",			/* 094	spill 5 normal */
181 	"spill-5-norm",			/* 095	spill 5 normal */
182 	"spill-5-norm",			/* 096	spill 5 normal */
183 	"spill-5-norm",			/* 097	spill 5 normal */
184 	"spill-6-norm",			/* 098	spill 6 normal */
185 	"spill-6-norm",			/* 099	spill 6 normal */
186 	"spill-6-norm",			/* 09A	spill 6 normal */
187 	"spill-6-norm",			/* 09B	spill 6 normal */
188 	"spill-7-norm",			/* 09C	spill 7 normal */
189 	"spill-7-norm",			/* 09D	spill 7 normal */
190 	"spill-7-norm",			/* 09E	spill 7 normal */
191 	"spill-7-norm",			/* 09F	spill 7 normal */
192 	"spill-0-oth",			/* 0A0	spill 0 other */
193 	"spill-0-oth",			/* 0A1	spill 0 other */
194 	"spill-0-oth",			/* 0A2	spill 0 other */
195 	"spill-0-oth",			/* 0A3	spill 0 other */
196 	"spill-1-oth",			/* 0A4	spill 1 other */
197 	"spill-1-oth",			/* 0A5	spill 1 other */
198 	"spill-1-oth",			/* 0A6	spill 1 other */
199 	"spill-1-oth",			/* 0A7	spill 1 other */
200 	"spill-2-oth",			/* 0A8	spill 2 other */
201 	"spill-2-oth",			/* 0A9	spill 2 other */
202 	"spill-2-oth",			/* 0AA	spill 2 other */
203 	"spill-2-oth",			/* 0AB	spill 2 other */
204 	"spill-3-oth",			/* 0AC	spill 3 other */
205 	"spill-3-oth",			/* 0AD	spill 3 other */
206 	"spill-3-oth",			/* 0AE	spill 3 other */
207 	"spill-3-oth",			/* 0AF	spill 3 other */
208 	"spill-4-oth",			/* 0B0	spill 4 other */
209 	"spill-4-oth",			/* 0B1	spill 4 other */
210 	"spill-4-oth",			/* 0B2	spill 4 other */
211 	"spill-4-oth",			/* 0B3	spill 4 other */
212 	"spill-5-oth",			/* 0B4	spill 5 other */
213 	"spill-5-oth",			/* 0B5	spill 5 other */
214 	"spill-5-oth",			/* 0B6	spill 5 other */
215 	"spill-5-oth",			/* 0B7	spill 5 other */
216 	"spill-6-oth",			/* 0B8	spill 6 other */
217 	"spill-6-oth",			/* 0B9	spill 6 other */
218 	"spill-6-oth",			/* 0BA	spill 6 other */
219 	"spill-6-oth",			/* 0BB	spill 6 other */
220 	"spill-7-oth",			/* 0BC	spill 7 other */
221 	"spill-7-oth",			/* 0BD	spill 7 other */
222 	"spill-7-oth",			/* 0BE	spill 7 other */
223 	"spill-7-oth",			/* 0BF	spill 7 other */
224 	"fill-0-norm",			/* 0C0	fill 0 normal */
225 	"fill-0-norm",			/* 0C1	fill 0 normal */
226 	"fill-0-norm",			/* 0C2	fill 0 normal */
227 	"fill-0-norm",			/* 0C3	fill 0 normal */
228 	"fill-1-norm",			/* 0C4	fill 1 normal */
229 	"fill-1-norm",			/* 0C5	fill 1 normal */
230 	"fill-1-norm",			/* 0C6	fill 1 normal */
231 	"fill-1-norm",			/* 0C7	fill 1 normal */
232 	"fill-2-norm",			/* 0C8	fill 2 normal */
233 	"fill-2-norm",			/* 0C9	fill 2 normal */
234 	"fill-2-norm",			/* 0CA	fill 2 normal */
235 	"fill-2-norm",			/* 0CB	fill 2 normal */
236 	"fill-3-norm",			/* 0CC	fill 3 normal */
237 	"fill-3-norm",			/* 0CD	fill 3 normal */
238 	"fill-3-norm",			/* 0CE	fill 3 normal */
239 	"fill-3-norm",			/* 0CF	fill 3 normal */
240 	"fill-4-norm",			/* 0D0	fill 4 normal */
241 	"fill-4-norm",			/* 0D1	fill 4 normal */
242 	"fill-4-norm",			/* 0D2	fill 4 normal */
243 	"fill-4-norm",			/* 0D3	fill 4 normal */
244 	"fill-5-norm",			/* 0D4	fill 5 normal */
245 	"fill-5-norm",			/* 0D5	fill 5 normal */
246 	"fill-5-norm",			/* 0D6	fill 5 normal */
247 	"fill-5-norm",			/* 0D7	fill 5 normal */
248 	"fill-6-norm",			/* 0D8	fill 6 normal */
249 	"fill-6-norm",			/* 0D9	fill 6 normal */
250 	"fill-6-norm",			/* 0DA	fill 6 normal */
251 	"fill-6-norm",			/* 0DB	fill 6 normal */
252 	"fill-7-norm",			/* 0DC	fill 7 normal */
253 	"fill-7-norm",			/* 0DD	fill 7 normal */
254 	"fill-7-norm",			/* 0DE	fill 7 normal */
255 	"fill-7-norm",			/* 0DF	fill 7 normal */
256 	"fill-0-oth",			/* 0E0	fill 0 other */
257 	"fill-0-oth",			/* 0E1	fill 0 other */
258 	"fill-0-oth",			/* 0E2	fill 0 other */
259 	"fill-0-oth",			/* 0E3	fill 0 other */
260 	"fill-1-oth",			/* 0E4	fill 1 other */
261 	"fill-1-oth",			/* 0E5	fill 1 other */
262 	"fill-1-oth",			/* 0E6	fill 1 other */
263 	"fill-1-oth",			/* 0E7	fill 1 other */
264 	"fill-2-oth",			/* 0E8	fill 2 other */
265 	"fill-2-oth",			/* 0E9	fill 2 other */
266 	"fill-2-oth",			/* 0EA	fill 2 other */
267 	"fill-2-oth",			/* 0EB	fill 2 other */
268 	"fill-3-oth",			/* 0EC	fill 3 other */
269 	"fill-3-oth",			/* 0ED	fill 3 other */
270 	"fill-3-oth",			/* 0EE	fill 3 other */
271 	"fill-3-oth",			/* 0EF	fill 3 other */
272 	"fill-4-oth",			/* 0F0	fill 4 other */
273 	"fill-4-oth",			/* 0F1	fill 4 other */
274 	"fill-4-oth",			/* 0F2	fill 4 other */
275 	"fill-4-oth",			/* 0F3	fill 4 other */
276 	"fill-5-oth",			/* 0F4	fill 5 other */
277 	"fill-5-oth",			/* 0F5	fill 5 other */
278 	"fill-5-oth",			/* 0F6	fill 5 other */
279 	"fill-5-oth",			/* 0F7	fill 5 other */
280 	"fill-6-oth",			/* 0F8	fill 6 other */
281 	"fill-6-oth",			/* 0F9	fill 6 other */
282 	"fill-6-oth",			/* 0FA	fill 6 other */
283 	"fill-6-oth",			/* 0FB	fill 6 other */
284 	"fill-7-oth",			/* 0FC	fill 7 other */
285 	"fill-7-oth",			/* 0FD	fill 7 other */
286 	"fill-7-oth",			/* 0FE	fill 7 other */
287 	"fill-7-oth",			/* 0FF	fill 7 other */
288 	"syscall-4x",			/* 100	old system call */
289 	"usr-brkpt",			/* 101	user breakpoint */
290 	"usr-div-zero",			/* 102	user divide by zero */
291 	"flush-wins",			/* 103	flush windows */
292 	"clean-wins",			/* 104	clean windows */
293 	"range-chk",			/* 105	range check ?? */
294 	"fix-align",			/* 106	do unaligned references */
295 	BAD,				/* 107	unused */
296 	"syscall-32",			/* 108	ILP32 system call on LP64 */
297 	"set-t0-addr",			/* 109	set trap0 address */
298 	BAD, BAD, BAD4,			/* 10A - 10F unused */
299 	BAD4, BAD4, BAD4, BAD4,		/* 110 - 11F unused (V9 user traps?) */
300 	"get-cc",			/* 120	get condition codes */
301 	"set-cc",			/* 121	set condition codes */
302 	"get-psr",			/* 122	get psr */
303 	"set-psr",			/* 123	set psr (some fields) */
304 	"getts",			/* 124	get timestamp */
305 	"gethrvtime",			/* 125	get lwp virtual time */
306 	"self-xcall",			/* 126	self xcall */
307 	"gethrtime",			/* 127	get hrestime */
308 	BAD,				/* 128  unused (ST_SETV9STACK) */
309 	"getlgrp",			/* 129	get lgrpid */
310 	BAD, BAD, BAD4,			/* 12A - 12F unused */
311 	BAD4, BAD4,			/* 130 - 137 unused */
312 	"dtrace-pid",			/* 138  DTrace pid provider */
313 	BAD,				/* 139  unused */
314 	"dtrace-return",		/* 13A  DTrace pid provider */
315 	BAD, BAD4,			/* 13B - 13F unused */
316 	"syscall-64",			/* 140  LP64 system call */
317 	BAD,				/* 141  unused */
318 	"tt-freeze",			/* 142  freeze traptrace */
319 	"tt-unfreeze",			/* 143  unfreeze traptrace */
320 	BAD4, BAD4, BAD4,		/* 144 - 14F unused */
321 	BAD4, BAD4, BAD4, BAD4,		/* 150 - 15F unused */
322 	BAD4, BAD4, BAD4, BAD4,		/* 160 - 16F unused */
323 	BAD4, BAD4, BAD4,		/* 170 - 17B unused */
324 	"ptl1-panic",			/* 17C	test ptl1_panic */
325 	"kmdb-enter",			/* 17D	kmdb enter (L1-A) */
326 	"kmdb-brkpt",			/* 17E	kmdb breakpoint */
327 	"obp-brkpt",			/* 17F	obp breakpoint */
328 #ifdef sun4v
329 	"fast_trap",			/* 180  hypervisor fast trap */
330 	"cpu_tick_npt",			/* 181  cpu_tick_npt() hcall */
331 	"cpu_stick_npt",		/* 182  cpu_stick_npt() hcall */
332 	"mmu_map_addr",			/* 183  mmu_map_addr() hcall */
333 	"mmu_unmap_addr",		/* 184  mmu_unmap_addr() hcall */
334 	"ttrace_addentry",		/* 185  ttrace_addentry() hcall */
335 	NOT, NOT, NOT4, NOT4,		/* 186 - 18F reserved */
336 #else
337 	NOT4, NOT4, NOT4, NOT4,		/* 180 - 18F reserved */
338 #endif
339 	NOT4, NOT4, NOT4, NOT4,		/* 190 - 19F reserved */
340 	NOT4, NOT4, NOT4, NOT4,		/* 1A0 - 1AF reserved */
341 	NOT4, NOT4, NOT4, NOT4,		/* 1B0 - 1BF reserved */
342 	NOT4, NOT4, NOT4, NOT4,		/* 1C0 - 1CF reserved */
343 	NOT4, NOT4, NOT4, NOT4,		/* 1D0 - 1DF reserved */
344 	NOT4, NOT4, NOT4, NOT4,		/* 1E0 - 1EF reserved */
345 	NOT4, NOT4, NOT4, NOT4		/* 1F0 - 1FF reserved */
346 };
347 static const size_t ttndescr = sizeof (ttdescr) / sizeof (ttdescr[0]);
348 
349 static GElf_Sym iv_sym;
350 static GElf_Sym iv_nohandler_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 softint_walk_data {
1340 	struct intr_vector *sw_table;
1341 	uintptr_t sw_base;
1342 	size_t sw_ndx;
1343 	size_t sw_size;
1344 } softint_walk_data_t;
1345 
1346 int
1347 softint_walk_init(mdb_walk_state_t *wsp)
1348 {
1349 	softint_walk_data_t *sw;
1350 
1351 	if (wsp->walk_addr != NULL) {
1352 		mdb_warn("softint walk only supports global walks\n");
1353 		return (WALK_ERR);
1354 	}
1355 
1356 	/*
1357 	 * If this fails, we'll just end up printing all the entries.
1358 	 */
1359 	if (mdb_lookup_by_name("nohandler", &iv_nohandler_sym) == -1)
1360 		mdb_warn("couldn't find iv nohandler");
1361 
1362 	sw = mdb_zalloc(sizeof (softint_walk_data_t), UM_SLEEP);
1363 
1364 	sw->sw_size = iv_sym.st_size;
1365 	sw->sw_base = (uintptr_t)iv_sym.st_value;
1366 	sw->sw_table = mdb_zalloc(sw->sw_size, UM_SLEEP);
1367 
1368 	if (mdb_vread(sw->sw_table, sw->sw_size, sw->sw_base) == -1) {
1369 		mdb_warn("couldn't read intr_vector table");
1370 		mdb_free(sw->sw_table, sw->sw_size);
1371 		mdb_free(sw, sizeof (softint_walk_data_t));
1372 		return (WALK_ERR);
1373 	}
1374 
1375 	wsp->walk_data = sw;
1376 	return (WALK_NEXT);
1377 }
1378 
1379 int
1380 softint_walk_step(mdb_walk_state_t *wsp)
1381 {
1382 	softint_walk_data_t *sw = (softint_walk_data_t *)wsp->walk_data;
1383 	size_t max = sw->sw_size / sizeof (struct intr_vector);
1384 
1385 	while (sw->sw_ndx < max) {
1386 		struct intr_vector *iv = &(sw->sw_table[sw->sw_ndx++]);
1387 
1388 		if (iv->iv_handler == (intrfunc)iv_nohandler_sym.st_value)
1389 			continue;
1390 
1391 		return (wsp->walk_callback((uintptr_t)iv -
1392 		    (uintptr_t)sw->sw_table + sw->sw_base, iv,
1393 		    wsp->walk_cbdata));
1394 	}
1395 
1396 	return (WALK_DONE);
1397 }
1398 
1399 void
1400 softint_walk_fini(mdb_walk_state_t *wsp)
1401 {
1402 	softint_walk_data_t *sw = wsp->walk_data;
1403 
1404 	mdb_free(sw->sw_table, sw->sw_size);
1405 	mdb_free(sw, sizeof (softint_walk_data_t));
1406 }
1407 
1408 int
1409 softint_dcmd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1410 {
1411 	int inum;
1412 	struct intr_vector iv;
1413 
1414 	if (!(flags & DCMD_ADDRSPEC)) {
1415 		if (mdb_walk_dcmd("softint", "softint", argc, argv) == -1) {
1416 			mdb_warn("can't walk softint");
1417 			return (DCMD_ERR);
1418 		}
1419 		return (DCMD_OK);
1420 	}
1421 
1422 	inum = (int)((addr - (uintptr_t)iv_sym.st_value) /
1423 	    sizeof (struct intr_vector));
1424 
1425 	if (DCMD_HDRSPEC(flags)) {
1426 		mdb_printf("%4s %?s %4s %4s %?s %s\n", "INUM", "ADDR", "PEND",
1427 		    "PIL", "ARG", "HANDLER");
1428 	}
1429 
1430 	if (mdb_vread(&iv, sizeof (iv), addr) == -1) {
1431 		mdb_warn("couldn't read intr_vector at %p", addr);
1432 		return (DCMD_ERR);
1433 	}
1434 
1435 	mdb_printf("%4d %0?p %4d %4d %?p %?p %a\n", inum, addr, iv.iv_pending,
1436 	    iv.iv_pil, iv.iv_arg, iv.iv_softint_arg2, iv.iv_handler);
1437 
1438 	return (DCMD_OK);
1439 }
1440 
1441 static int
1442 whatis_walk_tt(uintptr_t taddr, const trap_trace_fullrec_t *ttf, uintptr_t *ap)
1443 {
1444 	uintptr_t addr = *ap;
1445 
1446 	if (addr < taddr || addr > taddr + sizeof (struct trap_trace_record))
1447 		return (WALK_NEXT);
1448 
1449 	mdb_printf("%p is %p+%p, trap trace record for cpu %d\n",
1450 	    addr, taddr, addr - taddr, ttf->ttf_cpu);
1451 
1452 	mdb_set_dot(1);
1453 	return (WALK_DONE);
1454 }
1455 
1456 /*ARGSUSED*/
1457 int
1458 whatis(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1459 {
1460 	GElf_Sym sym;
1461 
1462 	if (!(flags & DCMD_ADDRSPEC))
1463 		return (DCMD_USAGE);
1464 
1465 	if (mdb_lookup_by_name("trap_trace_ctl", &sym) == -1)
1466 		return (DCMD_NEXT);
1467 
1468 	mdb_set_dot(0);
1469 
1470 	if (mdb_walk("ttrace", (mdb_walk_cb_t)whatis_walk_tt, &addr) == -1)
1471 		mdb_warn("failed to walk 'ttrace'");
1472 
1473 	return (DCMD_NEXT);
1474 }
1475 
1476 /*ARGSUSED*/
1477 int
1478 mutex_owner_init(mdb_walk_state_t *wsp)
1479 {
1480 	return (WALK_NEXT);
1481 }
1482 
1483 int
1484 mutex_owner_step(mdb_walk_state_t *wsp)
1485 {
1486 	uintptr_t addr = wsp->walk_addr;
1487 	mutex_impl_t mtx;
1488 	uintptr_t owner;
1489 	kthread_t thr;
1490 
1491 	if (mdb_vread(&mtx, sizeof (mtx), addr) == -1)
1492 		return (WALK_ERR);
1493 
1494 	if (!MUTEX_TYPE_ADAPTIVE(&mtx))
1495 		return (WALK_DONE);
1496 
1497 	if ((owner = (uintptr_t)MUTEX_OWNER(&mtx)) == NULL)
1498 		return (WALK_DONE);
1499 
1500 	if (mdb_vread(&thr, sizeof (thr), owner) != -1)
1501 		(void) wsp->walk_callback(owner, &thr, wsp->walk_cbdata);
1502 
1503 	return (WALK_DONE);
1504 }
1505 
1506 static const mdb_dcmd_t dcmds[] = {
1507 	{ "cpuset", ":[-l|-r]", "dump a cpuset_t", cmd_cpuset },
1508 	{ "ttctl", NULL, "dump trap trace ctl records", ttctl },
1509 	{ "ttrace", "[-x]", "dump trap trace buffer for a cpu", ttrace },
1510 #ifdef sun4v
1511 	{ "httctl", NULL, "dump hv trap trace ctl records", httctl },
1512 	{ "httrace", "[-x]", "dump hv trap trace buffer for a cpu", httrace },
1513 #endif
1514 	{ "xc_mbox", "?", "dump xcall mboxes", xc_mbox },
1515 	{ "xctrace", NULL, "dump xcall trace buffer", xctrace },
1516 	{ "softint", NULL, "dumps interrupt vector table", softint_dcmd },
1517 	{ "whatis", ":[-abv]", "given an address, return information", whatis },
1518 	{ "sfmmu_vtop", ":[[-v] -a as]", "print virtual to physical mapping",
1519 	    sfmmu_vtop },
1520 	{ "page_num2pp", ":", "page frame number to page structure",
1521 	    page_num2pp },
1522 	{ "memseg_list", ":", "show memseg list", memseg_list },
1523 	{ "tsbinfo", ":[-l [-a]]", "show tsbinfo", tsbinfo_list,
1524 	    tsbinfo_help },
1525 	{ NULL }
1526 };
1527 
1528 static const mdb_walker_t walkers[] = {
1529 	{ "mutex_owner", "walks the owner of a mutex",
1530 		mutex_owner_init, mutex_owner_step },
1531 	{ "ttrace", "walks the trap trace buffer for a CPU",
1532 		ttrace_walk_init, ttrace_walk_step, ttrace_walk_fini },
1533 #ifdef sun4v
1534 	{ "httrace", "walks the hv trap trace buffer for a CPU",
1535 		httrace_walk_init, httrace_walk_step, httrace_walk_fini },
1536 #endif
1537 	{ "xc_mbox", "walks the cross call mail boxes",
1538 		xc_mbox_walk_init, xc_mbox_walk_step, xc_mbox_walk_fini },
1539 	{ "softint", "walks the interrupt vector table",
1540 		softint_walk_init, softint_walk_step, softint_walk_fini },
1541 	{ "memseg", "walk the memseg structures",
1542 		memseg_walk_init, memseg_walk_step, memseg_walk_fini },
1543 	{ NULL }
1544 };
1545 
1546 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
1547 
1548 const mdb_modinfo_t *
1549 _mdb_init(void)
1550 {
1551 	if (mdb_lookup_by_name("intr_vector", &iv_sym) == -1) {
1552 		mdb_warn("couldn't find intr_vector");
1553 		return (NULL);
1554 	}
1555 
1556 	return (&modinfo);
1557 }
1558