1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #pragma ident "%Z%%M% %I% %E% SMI"
28
29 /*
30 * Routines common to the kvm target and the kmdb target that manipulate
31 * registers. This includes register dumps, registers as named variables,
32 * and stack traces.
33 */
34
35 #include <sys/types.h>
36 #include <sys/stack.h>
37 #include <sys/regset.h>
38
39 #ifndef __sparcv9cpu
40 #define __sparcv9cpu
41 #endif
42
43 #include <mdb/mdb_debug.h>
44 #include <mdb/mdb_modapi.h>
45 #include <mdb/mdb_v9util.h>
46 #include <mdb/mdb_target_impl.h>
47 #include <mdb/mdb_err.h>
48 #include <mdb/mdb.h>
49
50 #include <mdb/mdb_kreg_impl.h>
51
52 /*
53 * We also define an array of register names and their corresponding
54 * array indices. This is used by the getareg and putareg entry points,
55 * and also by our register variable discipline.
56 */
57 const mdb_tgt_regdesc_t mdb_sparcv9_kregs[] = {
58 { "g0", KREG_G0, MDB_TGT_R_EXPORT },
59 { "g1", KREG_G1, MDB_TGT_R_EXPORT },
60 { "g2", KREG_G2, MDB_TGT_R_EXPORT },
61 { "g3", KREG_G3, MDB_TGT_R_EXPORT },
62 { "g4", KREG_G4, MDB_TGT_R_EXPORT },
63 { "g5", KREG_G5, MDB_TGT_R_EXPORT },
64 { "g6", KREG_G6, MDB_TGT_R_EXPORT },
65 { "g7", KREG_G7, MDB_TGT_R_EXPORT },
66 { "o0", KREG_O0, MDB_TGT_R_EXPORT },
67 { "o1", KREG_O1, MDB_TGT_R_EXPORT },
68 { "o2", KREG_O2, MDB_TGT_R_EXPORT },
69 { "o3", KREG_O3, MDB_TGT_R_EXPORT },
70 { "o4", KREG_O4, MDB_TGT_R_EXPORT },
71 { "o5", KREG_O5, MDB_TGT_R_EXPORT },
72 { "o6", KREG_O6, MDB_TGT_R_EXPORT },
73 { "o7", KREG_O7, MDB_TGT_R_EXPORT },
74 { "l0", KREG_L0, MDB_TGT_R_EXPORT },
75 { "l1", KREG_L1, MDB_TGT_R_EXPORT },
76 { "l2", KREG_L2, MDB_TGT_R_EXPORT },
77 { "l3", KREG_L3, MDB_TGT_R_EXPORT },
78 { "l4", KREG_L4, MDB_TGT_R_EXPORT },
79 { "l5", KREG_L5, MDB_TGT_R_EXPORT },
80 { "l6", KREG_L6, MDB_TGT_R_EXPORT },
81 { "l7", KREG_L7, MDB_TGT_R_EXPORT },
82 { "i0", KREG_I0, MDB_TGT_R_EXPORT },
83 { "i1", KREG_I1, MDB_TGT_R_EXPORT },
84 { "i2", KREG_I2, MDB_TGT_R_EXPORT },
85 { "i3", KREG_I3, MDB_TGT_R_EXPORT },
86 { "i4", KREG_I4, MDB_TGT_R_EXPORT },
87 { "i5", KREG_I5, MDB_TGT_R_EXPORT },
88 { "i6", KREG_I6, MDB_TGT_R_EXPORT },
89 { "i7", KREG_I7, MDB_TGT_R_EXPORT },
90 { "ccr", KREG_CCR, MDB_TGT_R_EXPORT },
91 { "pc", KREG_PC, MDB_TGT_R_EXPORT },
92 { "npc", KREG_NPC, MDB_TGT_R_EXPORT },
93 { "y", KREG_Y, 0 },
94 { "asi", KREG_ASI, MDB_TGT_R_EXPORT },
95 { "fprs", KREG_FPRS, MDB_TGT_R_EXPORT },
96 { "tick", KREG_TICK, MDB_TGT_R_EXPORT },
97 { "stick", KREG_STICK, MDB_TGT_R_EXPORT },
98 { "pstate", KREG_PSTATE, MDB_TGT_R_PRIV | MDB_TGT_R_EXPORT },
99 { "tl", KREG_TL, MDB_TGT_R_PRIV | MDB_TGT_R_EXPORT },
100 { "tt", KREG_TT, MDB_TGT_R_PRIV | MDB_TGT_R_EXPORT },
101 { "pil", KREG_PIL, MDB_TGT_R_PRIV | MDB_TGT_R_EXPORT },
102 { "tba", KREG_TBA, MDB_TGT_R_PRIV | MDB_TGT_R_EXPORT },
103 { "ver", KREG_VER, MDB_TGT_R_PRIV | MDB_TGT_R_EXPORT },
104 { "cwp", KREG_CWP, MDB_TGT_R_PRIV | MDB_TGT_R_EXPORT },
105 { "cansave", KREG_CANSAVE, MDB_TGT_R_PRIV },
106 { "canrestore", KREG_CANRESTORE, MDB_TGT_R_PRIV },
107 { "otherwin", KREG_OTHERWIN, MDB_TGT_R_PRIV },
108 { "wstate", KREG_WSTATE, MDB_TGT_R_PRIV },
109 { "cleanwin", KREG_CLEANWIN, MDB_TGT_R_PRIV },
110 { "sp", KREG_SP, MDB_TGT_R_EXPORT | MDB_TGT_R_ALIAS },
111 { "fp", KREG_FP, MDB_TGT_R_EXPORT | MDB_TGT_R_ALIAS },
112 { NULL, 0, 0 }
113 };
114
115 static const char *
pstate_mm_to_str(kreg_t pstate)116 pstate_mm_to_str(kreg_t pstate)
117 {
118 if (KREG_PSTATE_MM_TSO(pstate))
119 return ("TSO");
120
121 if (KREG_PSTATE_MM_PSO(pstate))
122 return ("PSO");
123
124 if (KREG_PSTATE_MM_RMO(pstate))
125 return ("RMO");
126
127 return ("???");
128 }
129
130 void
mdb_v9printregs(const mdb_tgt_gregset_t * gregs)131 mdb_v9printregs(const mdb_tgt_gregset_t *gregs)
132 {
133 const kreg_t *kregs = gregs->kregs;
134
135 #define GETREG2(x) ((uintptr_t)kregs[(x)]), ((uintptr_t)kregs[(x)])
136
137 mdb_printf("%%g0 = 0x%0?p %15A %%l0 = 0x%0?p %A\n",
138 GETREG2(KREG_G0), GETREG2(KREG_L0));
139
140 mdb_printf("%%g1 = 0x%0?p %15A %%l1 = 0x%0?p %A\n",
141 GETREG2(KREG_G1), GETREG2(KREG_L1));
142
143 mdb_printf("%%g2 = 0x%0?p %15A %%l2 = 0x%0?p %A\n",
144 GETREG2(KREG_G2), GETREG2(KREG_L2));
145
146 mdb_printf("%%g3 = 0x%0?p %15A %%l3 = 0x%0?p %A\n",
147 GETREG2(KREG_G3), GETREG2(KREG_L3));
148
149 mdb_printf("%%g4 = 0x%0?p %15A %%l4 = 0x%0?p %A\n",
150 GETREG2(KREG_G4), GETREG2(KREG_L4));
151
152 mdb_printf("%%g5 = 0x%0?p %15A %%l5 = 0x%0?p %A\n",
153 GETREG2(KREG_G5), GETREG2(KREG_L5));
154
155 mdb_printf("%%g6 = 0x%0?p %15A %%l6 = 0x%0?p %A\n",
156 GETREG2(KREG_G6), GETREG2(KREG_L6));
157
158 mdb_printf("%%g7 = 0x%0?p %15A %%l7 = 0x%0?p %A\n\n",
159 GETREG2(KREG_G7), GETREG2(KREG_L7));
160
161 mdb_printf("%%o0 = 0x%0?p %15A %%i0 = 0x%0?p %A\n",
162 GETREG2(KREG_O0), GETREG2(KREG_I0));
163
164 mdb_printf("%%o1 = 0x%0?p %15A %%i1 = 0x%0?p %A\n",
165 GETREG2(KREG_O1), GETREG2(KREG_I1));
166
167 mdb_printf("%%o2 = 0x%0?p %15A %%i2 = 0x%0?p %A\n",
168 GETREG2(KREG_O2), GETREG2(KREG_I2));
169
170 mdb_printf("%%o3 = 0x%0?p %15A %%i3 = 0x%0?p %A\n",
171 GETREG2(KREG_O3), GETREG2(KREG_I3));
172
173 mdb_printf("%%o4 = 0x%0?p %15A %%i4 = 0x%0?p %A\n",
174 GETREG2(KREG_O4), GETREG2(KREG_I4));
175
176 mdb_printf("%%o5 = 0x%0?p %15A %%i5 = 0x%0?p %A\n",
177 GETREG2(KREG_O5), GETREG2(KREG_I5));
178
179 mdb_printf("%%o6 = 0x%0?p %15A %%i6 = 0x%0?p %A\n",
180 GETREG2(KREG_O6), GETREG2(KREG_I6));
181
182 mdb_printf("%%o7 = 0x%0?p %15A %%i7 = 0x%0?p %A\n\n",
183 GETREG2(KREG_O7), GETREG2(KREG_I7));
184
185 mdb_printf(" %%ccr = 0x%02llx "
186 "xcc=%c%c%c%c icc=%c%c%c%c\n", kregs[KREG_CCR],
187 (kregs[KREG_CCR] & KREG_CCR_XCC_N_MASK) ? 'N' : 'n',
188 (kregs[KREG_CCR] & KREG_CCR_XCC_Z_MASK) ? 'Z' : 'z',
189 (kregs[KREG_CCR] & KREG_CCR_XCC_V_MASK) ? 'V' : 'v',
190 (kregs[KREG_CCR] & KREG_CCR_XCC_C_MASK) ? 'C' : 'c',
191 (kregs[KREG_CCR] & KREG_CCR_ICC_N_MASK) ? 'N' : 'n',
192 (kregs[KREG_CCR] & KREG_CCR_ICC_Z_MASK) ? 'Z' : 'z',
193 (kregs[KREG_CCR] & KREG_CCR_ICC_V_MASK) ? 'V' : 'v',
194 (kregs[KREG_CCR] & KREG_CCR_ICC_C_MASK) ? 'C' : 'c');
195
196 mdb_printf("%%fprs = 0x%02llx "
197 "fef=%llu du=%llu dl=%llu\n", kregs[KREG_FPRS],
198 (kregs[KREG_FPRS] & KREG_FPRS_FEF_MASK) >> KREG_FPRS_FEF_SHIFT,
199 (kregs[KREG_FPRS] & KREG_FPRS_DU_MASK) >> KREG_FPRS_DU_SHIFT,
200 (kregs[KREG_FPRS] & KREG_FPRS_DL_MASK) >> KREG_FPRS_DL_SHIFT);
201
202 mdb_printf(" %%asi = 0x%02llx\n", kregs[KREG_ASI]);
203 mdb_printf(" %%y = 0x%0?p\n", (uintptr_t)kregs[KREG_Y]);
204
205 mdb_printf(" %%pc = 0x%0?p %A\n", GETREG2(KREG_PC));
206 mdb_printf(" %%npc = 0x%0?p %A\n", GETREG2(KREG_NPC));
207
208 #if STACK_BIAS != 0
209 mdb_printf(" %%sp = 0x%0?p unbiased=0x%0?p\n",
210 (uintptr_t)kregs[KREG_SP], (uintptr_t)kregs[KREG_SP] + STACK_BIAS);
211 #else
212 mdb_printf(" %%sp = 0x%0?p\n", (uintptr_t)kregs[KREG_SP]);
213 #endif
214 mdb_printf(" %%fp = 0x%0?p\n\n", (uintptr_t)kregs[KREG_FP]);
215
216 mdb_printf(" %%tick = 0x%016llx\n", kregs[KREG_TICK]);
217 if (gregs->kreg_flags & MDB_V9GREG_F_STICK_VALID)
218 mdb_printf(" %%stick = 0x%016llx\n", kregs[KREG_STICK]);
219 mdb_printf(" %%tba = 0x%016llx\n", kregs[KREG_TBA]);
220 mdb_printf(" %%tt = 0x%01llx\n", kregs[KREG_TT]);
221 mdb_printf(" %%tl = 0x%01llx\n", kregs[KREG_TL]);
222 mdb_printf(" %%pil = 0x%01llx\n", kregs[KREG_PIL]);
223
224 mdb_printf("%%pstate = 0x%03llx cle=%llu tle=%llu mm=%s"
225 " red=%llu pef=%llu am=%llu priv=%llu ie=%llu ag=%llu\n\n",
226 kregs[KREG_PSTATE],
227 (kregs[KREG_PSTATE] & KREG_PSTATE_CLE_MASK) >>
228 KREG_PSTATE_CLE_SHIFT,
229 (kregs[KREG_PSTATE] & KREG_PSTATE_TLE_MASK) >>
230 KREG_PSTATE_TLE_SHIFT, pstate_mm_to_str(kregs[KREG_PSTATE]),
231 (kregs[KREG_PSTATE] & KREG_PSTATE_RED_MASK) >>
232 KREG_PSTATE_RED_SHIFT,
233 (kregs[KREG_PSTATE] & KREG_PSTATE_PEF_MASK) >>
234 KREG_PSTATE_PEF_SHIFT,
235 (kregs[KREG_PSTATE] & KREG_PSTATE_AM_MASK) >> KREG_PSTATE_AM_SHIFT,
236 (kregs[KREG_PSTATE] & KREG_PSTATE_PRIV_MASK) >>
237 KREG_PSTATE_PRIV_SHIFT,
238 (kregs[KREG_PSTATE] & KREG_PSTATE_IE_MASK) >> KREG_PSTATE_IE_SHIFT,
239 (kregs[KREG_PSTATE] & KREG_PSTATE_AG_MASK) >> KREG_PSTATE_AG_SHIFT);
240
241 mdb_printf(" %%cwp = 0x%02llx %%cansave = 0x%02llx\n",
242 kregs[KREG_CWP], kregs[KREG_CANSAVE]);
243
244 mdb_printf("%%canrestore = 0x%02llx %%otherwin = 0x%02llx\n",
245 kregs[KREG_CANRESTORE], kregs[KREG_OTHERWIN]);
246
247 mdb_printf(" %%wstate = 0x%02llx %%cleanwin = 0x%02llx\n",
248 kregs[KREG_WSTATE], kregs[KREG_CLEANWIN]);
249 }
250
251 int
mdb_kvm_v9stack_iter(mdb_tgt_t * t,const mdb_tgt_gregset_t * gsp,mdb_tgt_stack_f * func,void * arg)252 mdb_kvm_v9stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gsp,
253 mdb_tgt_stack_f *func, void *arg)
254 {
255 mdb_tgt_gregset_t gregs;
256 kreg_t *kregs = &gregs.kregs[0];
257 int got_pc = (gsp->kregs[KREG_PC] != 0);
258
259 struct rwindow rwin;
260 uintptr_t sp;
261 long argv[6];
262 int i;
263
264 /*
265 * - If we got a pc, invoke the call back function starting
266 * with gsp.
267 * - If we got a saved pc (%i7), invoke the call back function
268 * starting with the first register window.
269 * - If we got neither a pc nor a saved pc, invoke the call back
270 * function starting with the second register window.
271 */
272
273 bcopy(gsp, &gregs, sizeof (gregs));
274
275 for (;;) {
276 for (i = 0; i < 6; i++)
277 argv[i] = kregs[KREG_I0 + i];
278
279 if (got_pc && func(arg, kregs[KREG_PC], 6, argv, &gregs) != 0)
280 break;
281
282 kregs[KREG_PC] = kregs[KREG_I7];
283 kregs[KREG_NPC] = kregs[KREG_PC] + 4;
284
285 bcopy(&kregs[KREG_I0], &kregs[KREG_O0], 8 * sizeof (kreg_t));
286 got_pc |= (kregs[KREG_PC] != 0);
287
288 if ((sp = kregs[KREG_FP] + STACK_BIAS) == STACK_BIAS || sp == 0)
289 break; /* Stop if we're at the end of the stack */
290
291 if (sp & (STACK_ALIGN - 1))
292 return (set_errno(EMDB_STKALIGN));
293
294 if (mdb_tgt_vread(t, &rwin, sizeof (rwin), sp) != sizeof (rwin))
295 return (-1); /* Failed to read frame */
296
297 for (i = 0; i < 8; i++)
298 kregs[KREG_L0 + i] = (uintptr_t)rwin.rw_local[i];
299 for (i = 0; i < 8; i++)
300 kregs[KREG_I0 + i] = (uintptr_t)rwin.rw_in[i];
301 }
302
303 return (0);
304 }
305
306 /*ARGSUSED*/
307 int
mdb_kvm_v9frame(void * arglim,uintptr_t pc,uint_t argc,const long * argv,const mdb_tgt_gregset_t * gregs)308 mdb_kvm_v9frame(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
309 const mdb_tgt_gregset_t *gregs)
310 {
311 argc = MIN(argc, (uint_t)(uintptr_t)arglim);
312 mdb_printf("%a(", pc);
313
314 if (argc != 0) {
315 mdb_printf("%lr", *argv++);
316 for (argc--; argc != 0; argc--)
317 mdb_printf(", %lr", *argv++);
318 }
319
320 mdb_printf(")\n");
321 return (0);
322 }
323
324 int
mdb_kvm_v9framev(void * arglim,uintptr_t pc,uint_t argc,const long * argv,const mdb_tgt_gregset_t * gregs)325 mdb_kvm_v9framev(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
326 const mdb_tgt_gregset_t *gregs)
327 {
328 argc = MIN(argc, (uint_t)(uintptr_t)arglim);
329 mdb_printf("%0?llr %a(", gregs->kregs[KREG_SP], pc);
330
331 if (argc != 0) {
332 mdb_printf("%lr", *argv++);
333 for (argc--; argc != 0; argc--)
334 mdb_printf(", %lr", *argv++);
335 }
336
337 mdb_printf(")\n");
338 return (0);
339 }
340
341 int
mdb_kvm_v9framer(void * arglim,uintptr_t pc,uint_t argc,const long * argv,const mdb_tgt_gregset_t * gregs)342 mdb_kvm_v9framer(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
343 const mdb_tgt_gregset_t *gregs)
344 {
345 char buf[BUFSIZ];
346 const kreg_t *kregs = &gregs->kregs[0];
347
348 argc = MIN(argc, (uint_t)(uintptr_t)arglim);
349
350 if (pc == PC_FAKE)
351 mdb_printf("%<b>%0?llr% %s%</b>(", kregs[KREG_SP], "?");
352 else
353 mdb_printf("%<b>%0?llr% %a%</b>(", kregs[KREG_SP], pc);
354
355 if (argc != 0) {
356 mdb_printf("%lr", *argv++);
357 for (argc--; argc != 0; argc--)
358 mdb_printf(", %lr", *argv++);
359 }
360
361 mdb_printf(")\n");
362
363 (void) mdb_inc_indent(2);
364
365 mdb_printf("%%l0-%%l3: %?lr %?lr %?lr %?lr\n",
366 kregs[KREG_L0], kregs[KREG_L1], kregs[KREG_L2], kregs[KREG_L3]);
367
368 mdb_printf("%%l4-%%l7: %?lr %?lr %?lr %?lr\n",
369 kregs[KREG_L4], kregs[KREG_L5], kregs[KREG_L6], kregs[KREG_L7]);
370
371 if (kregs[KREG_FP] != 0 && (kregs[KREG_FP] + STACK_BIAS) != 0)
372 if (mdb_dis_ins2str(mdb.m_disasm, mdb.m_target, MDB_TGT_AS_VIRT,
373 buf, sizeof (buf), kregs[KREG_I7]) != kregs[KREG_I7])
374 mdb_printf("%-#25a%s\n", kregs[KREG_I7], buf);
375
376 (void) mdb_dec_indent(2);
377 mdb_printf("\n");
378
379 return (0);
380 }
381