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 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * User Process Target Intel 32-bit component
29 *
30 * This file provides the ISA-dependent portion of the user process target.
31 * For more details on the implementation refer to mdb_proc.c.
32 */
33
34 #include <mdb/mdb_proc.h>
35 #include <mdb/mdb_kreg.h>
36 #include <mdb/mdb_err.h>
37 #include <mdb/mdb_amd64util.h>
38 #include <mdb/mdb.h>
39
40 #include <sys/frame.h>
41 #include <libproc.h>
42 #include <sys/fp.h>
43 #include <ieeefp.h>
44
45 const mdb_tgt_regdesc_t pt_regdesc[] = {
46 { "r15", REG_R15, MDB_TGT_R_EXPORT },
47 { "r14", REG_R14, MDB_TGT_R_EXPORT },
48 { "r13", REG_R13, MDB_TGT_R_EXPORT },
49 { "r12", REG_R12, MDB_TGT_R_EXPORT },
50 { "r11", REG_R11, MDB_TGT_R_EXPORT },
51 { "r10", REG_R10, MDB_TGT_R_EXPORT },
52 { "r9", REG_R9, MDB_TGT_R_EXPORT },
53 { "r8", REG_R8, MDB_TGT_R_EXPORT },
54 { "rdi", REG_RDI, MDB_TGT_R_EXPORT },
55 { "rsi", REG_RSI, MDB_TGT_R_EXPORT },
56 { "rbp", REG_RBP, MDB_TGT_R_EXPORT },
57 { "rbx", REG_RBX, MDB_TGT_R_EXPORT },
58 { "rdx", REG_RDX, MDB_TGT_R_EXPORT },
59 { "rcx", REG_RCX, MDB_TGT_R_EXPORT },
60 { "rax", REG_RAX, MDB_TGT_R_EXPORT },
61 { "trapno", REG_TRAPNO, MDB_TGT_R_EXPORT },
62 { "err", REG_ERR, MDB_TGT_R_EXPORT },
63 { "rip", REG_RIP, MDB_TGT_R_EXPORT },
64 { "cs", REG_CS, MDB_TGT_R_EXPORT },
65 { "rflags", REG_RFL, MDB_TGT_R_EXPORT },
66 { "rsp", REG_RSP, MDB_TGT_R_EXPORT },
67 { "ss", REG_SS, MDB_TGT_R_EXPORT },
68 { "fs", REG_FS, MDB_TGT_R_EXPORT },
69 { "gs", REG_GS, MDB_TGT_R_EXPORT },
70 { "es", REG_ES, MDB_TGT_R_EXPORT },
71 { "ds", REG_DS, MDB_TGT_R_EXPORT },
72 { "fsbase", REG_FSBASE, MDB_TGT_R_EXPORT },
73 { "gsbase", REG_GSBASE, MDB_TGT_R_EXPORT },
74 { NULL, 0, 0 }
75 };
76
77 /*
78 * We cannot rely on pr_instr, because if we hit a breakpoint or the user has
79 * artifically modified memory, it will no longer be correct.
80 */
81 static uint8_t
82 pt_read_instr(mdb_tgt_t *t)
83 {
84 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
85 uint8_t ret = 0;
86
87 (void) mdb_tgt_vread(t, &ret, sizeof (ret), psp->pr_reg[REG_RIP]);
88
89 return (ret);
90 }
91
92 /*ARGSUSED*/
93 int
94 pt_regs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
95 {
96 mdb_tgt_t *t = mdb.m_target;
97 mdb_tgt_tid_t tid;
98 prgregset_t grs;
99 prgreg_t rflags;
100
101 if (argc != 0)
102 return (DCMD_USAGE);
103
104 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_UNDEAD) {
105 mdb_warn("no process active\n");
106 return (DCMD_ERR);
107 }
108
109 if (Pstate(t->t_pshandle) == PS_LOST) {
110 mdb_warn("debugger has lost control of process\n");
111 return (DCMD_ERR);
112 }
113
114 if (flags & DCMD_ADDRSPEC)
115 tid = (mdb_tgt_tid_t)addr;
116 else
117 tid = PTL_TID(t);
118
119 if (PTL_GETREGS(t, tid, grs) != 0) {
120 mdb_warn("failed to get current register set");
121 return (DCMD_ERR);
122 }
123
124 rflags = grs[REG_RFL];
125
126 mdb_printf("%%rax = 0x%0?p\t%%r8 = 0x%0?p\n",
127 grs[REG_RAX], grs[REG_R8]);
128 mdb_printf("%%rbx = 0x%0?p\t%%r9 = 0x%0?p\n",
129 grs[REG_RBX], grs[REG_R9]);
130 mdb_printf("%%rcx = 0x%0?p\t%%r10 = 0x%0?p\n",
131 grs[REG_RCX], grs[REG_R10]);
132 mdb_printf("%%rdx = 0x%0?p\t%%r11 = 0x%0?p\n",
133 grs[REG_RDX], grs[REG_R11]);
134 mdb_printf("%%rsi = 0x%0?p\t%%r12 = 0x%0?p\n",
135 grs[REG_RSI], grs[REG_R12]);
136 mdb_printf("%%rdi = 0x%0?p\t%%r13 = 0x%0?p\n",
137 grs[REG_RDI], grs[REG_R13]);
138 mdb_printf(" %?s\t%%r14 = 0x%0?p\n",
139 "", grs[REG_R14]);
140 mdb_printf(" %?s\t%%r15 = 0x%0?p\n",
141 "", grs[REG_R15]);
142
143 mdb_printf("\n");
144
145 mdb_printf("%%cs = 0x%04x\t%%fs = 0x%04x\t%%gs = 0x%04x\n",
146 grs[REG_CS], grs[REG_FS], grs[REG_GS]);
147 mdb_printf("%%ds = 0x%04x\t%%es = 0x%04x\t%%ss = 0x%04x\n",
148 grs[REG_DS], grs[REG_ES], grs[REG_SS]);
149
150 mdb_printf("\n");
151
152 mdb_printf("%%rip = 0x%0?p %A\n", grs[REG_RIP], grs[REG_RIP]);
153 mdb_printf("%%rbp = 0x%0?p\n", grs[REG_RBP], grs[REG_RBP]);
154 mdb_printf("%%rsp = 0x%0?p\n", grs[REG_RSP], grs[REG_RSP]);
155
156 mdb_printf("\n");
157
158 mdb_printf("%%rflags = 0x%08x\n", rflags);
159
160 mdb_printf(" id=%u vip=%u vif=%u ac=%u vm=%u rf=%u nt=%u iopl=0x%x\n",
161 (rflags & KREG_EFLAGS_ID_MASK) >> KREG_EFLAGS_ID_SHIFT,
162 (rflags & KREG_EFLAGS_VIP_MASK) >> KREG_EFLAGS_VIP_SHIFT,
163 (rflags & KREG_EFLAGS_VIF_MASK) >> KREG_EFLAGS_VIF_SHIFT,
164 (rflags & KREG_EFLAGS_AC_MASK) >> KREG_EFLAGS_AC_SHIFT,
165 (rflags & KREG_EFLAGS_VM_MASK) >> KREG_EFLAGS_VM_SHIFT,
166 (rflags & KREG_EFLAGS_RF_MASK) >> KREG_EFLAGS_RF_SHIFT,
167 (rflags & KREG_EFLAGS_NT_MASK) >> KREG_EFLAGS_NT_SHIFT,
168 (rflags & KREG_EFLAGS_IOPL_MASK) >> KREG_EFLAGS_IOPL_SHIFT);
169
170 mdb_printf(" status=<%s,%s,%s,%s,%s,%s,%s,%s,%s>\n",
171 (rflags & KREG_EFLAGS_OF_MASK) ? "OF" : "of",
172 (rflags & KREG_EFLAGS_DF_MASK) ? "DF" : "df",
173 (rflags & KREG_EFLAGS_IF_MASK) ? "IF" : "if",
174 (rflags & KREG_EFLAGS_TF_MASK) ? "TF" : "tf",
175 (rflags & KREG_EFLAGS_SF_MASK) ? "SF" : "sf",
176 (rflags & KREG_EFLAGS_ZF_MASK) ? "ZF" : "zf",
177 (rflags & KREG_EFLAGS_AF_MASK) ? "AF" : "af",
178 (rflags & KREG_EFLAGS_PF_MASK) ? "PF" : "pf",
179 (rflags & KREG_EFLAGS_CF_MASK) ? "CF" : "cf");
180
181 mdb_printf("\n");
182
183 mdb_printf("%%gsbase = 0x%0?p\n", grs[REG_GSBASE]);
184 mdb_printf("%%fsbase = 0x%0?p\n", grs[REG_FSBASE]);
185 mdb_printf("%%trapno = 0x%x\n", grs[REG_TRAPNO]);
186 mdb_printf(" %%err = 0x%x\n", grs[REG_ERR]);
187
188 return (DCMD_OK);
189 }
190
191 static const char *
192 fpcw2str(uint32_t cw, char *buf, size_t nbytes)
193 {
194 char *end = buf + nbytes;
195 char *p = buf;
196
197 buf[0] = '\0';
198
199 /*
200 * Decode all masks in the 80387 control word.
201 */
202 if (cw & FPIM)
203 p += mdb_snprintf(p, (size_t)(end - p), "|IM");
204 if (cw & FPDM)
205 p += mdb_snprintf(p, (size_t)(end - p), "|DM");
206 if (cw & FPZM)
207 p += mdb_snprintf(p, (size_t)(end - p), "|ZM");
208 if (cw & FPOM)
209 p += mdb_snprintf(p, (size_t)(end - p), "|OM");
210 if (cw & FPUM)
211 p += mdb_snprintf(p, (size_t)(end - p), "|UM");
212 if (cw & FPPM)
213 p += mdb_snprintf(p, (size_t)(end - p), "|PM");
214 if (cw & FPPC)
215 p += mdb_snprintf(p, (size_t)(end - p), "|PC");
216 if (cw & FPRC)
217 p += mdb_snprintf(p, (size_t)(end - p), "|RC");
218 if (cw & FPIC)
219 p += mdb_snprintf(p, (size_t)(end - p), "|IC");
220
221 /*
222 * Decode precision, rounding, and infinity options in control word.
223 */
224 if (cw & FPSIG24)
225 p += mdb_snprintf(p, (size_t)(end - p), "|SIG24");
226 if (cw & FPSIG53)
227 p += mdb_snprintf(p, (size_t)(end - p), "|SIG53");
228 if (cw & FPSIG64)
229 p += mdb_snprintf(p, (size_t)(end - p), "|SIG64");
230
231 if ((cw & FPRC) == (FPRD|FPRU))
232 p += mdb_snprintf(p, (size_t)(end - p), "|RTZ");
233 else if (cw & FPRD)
234 p += mdb_snprintf(p, (size_t)(end - p), "|RD");
235 else if (cw & FPRU)
236 p += mdb_snprintf(p, (size_t)(end - p), "|RU");
237 else
238 p += mdb_snprintf(p, (size_t)(end - p), "|RTN");
239
240 if (cw & FPA)
241 p += mdb_snprintf(p, (size_t)(end - p), "|A");
242 else
243 p += mdb_snprintf(p, (size_t)(end - p), "|P");
244 if (cw & WFPB17)
245 p += mdb_snprintf(p, (size_t)(end - p), "|WFPB17");
246 if (cw & WFPB24)
247 p += mdb_snprintf(p, (size_t)(end - p), "|WFPB24");
248
249 if (buf[0] == '|')
250 return (buf + 1);
251
252 return ("0");
253 }
254
255 static const char *
256 fpsw2str(uint32_t cw, char *buf, size_t nbytes)
257 {
258 char *end = buf + nbytes;
259 char *p = buf;
260
261 buf[0] = '\0';
262
263 /*
264 * Decode all masks in the 80387 status word.
265 */
266 if (cw & FPS_IE)
267 p += mdb_snprintf(p, (size_t)(end - p), "|IE");
268 if (cw & FPS_DE)
269 p += mdb_snprintf(p, (size_t)(end - p), "|DE");
270 if (cw & FPS_ZE)
271 p += mdb_snprintf(p, (size_t)(end - p), "|ZE");
272 if (cw & FPS_OE)
273 p += mdb_snprintf(p, (size_t)(end - p), "|OE");
274 if (cw & FPS_UE)
275 p += mdb_snprintf(p, (size_t)(end - p), "|UE");
276 if (cw & FPS_PE)
277 p += mdb_snprintf(p, (size_t)(end - p), "|PE");
278 if (cw & FPS_SF)
279 p += mdb_snprintf(p, (size_t)(end - p), "|SF");
280 if (cw & FPS_ES)
281 p += mdb_snprintf(p, (size_t)(end - p), "|ES");
282 if (cw & FPS_C0)
283 p += mdb_snprintf(p, (size_t)(end - p), "|C0");
284 if (cw & FPS_C1)
285 p += mdb_snprintf(p, (size_t)(end - p), "|C1");
286 if (cw & FPS_C2)
287 p += mdb_snprintf(p, (size_t)(end - p), "|C2");
288 if (cw & FPS_C3)
289 p += mdb_snprintf(p, (size_t)(end - p), "|C3");
290 if (cw & FPS_B)
291 p += mdb_snprintf(p, (size_t)(end - p), "|B");
292
293 if (buf[0] == '|')
294 return (buf + 1);
295
296 return ("0");
297 }
298
299 static const char *
300 fpmxcsr2str(uint32_t mxcsr, char *buf, size_t nbytes)
301 {
302 char *end = buf + nbytes;
303 char *p = buf;
304
305 buf[0] = '\0';
306
307 /*
308 * Decode the MXCSR word
309 */
310 if (mxcsr & SSE_IE)
311 p += mdb_snprintf(p, (size_t)(end - p), "|IE");
312 if (mxcsr & SSE_DE)
313 p += mdb_snprintf(p, (size_t)(end - p), "|DE");
314 if (mxcsr & SSE_ZE)
315 p += mdb_snprintf(p, (size_t)(end - p), "|ZE");
316 if (mxcsr & SSE_OE)
317 p += mdb_snprintf(p, (size_t)(end - p), "|OE");
318 if (mxcsr & SSE_UE)
319 p += mdb_snprintf(p, (size_t)(end - p), "|UE");
320 if (mxcsr & SSE_PE)
321 p += mdb_snprintf(p, (size_t)(end - p), "|PE");
322
323 if (mxcsr & SSE_DAZ)
324 p += mdb_snprintf(p, (size_t)(end - p), "|DAZ");
325
326 if (mxcsr & SSE_IM)
327 p += mdb_snprintf(p, (size_t)(end - p), "|IM");
328 if (mxcsr & SSE_DM)
329 p += mdb_snprintf(p, (size_t)(end - p), "|DM");
330 if (mxcsr & SSE_ZM)
331 p += mdb_snprintf(p, (size_t)(end - p), "|ZM");
332 if (mxcsr & SSE_OM)
333 p += mdb_snprintf(p, (size_t)(end - p), "|OM");
334 if (mxcsr & SSE_UM)
335 p += mdb_snprintf(p, (size_t)(end - p), "|UM");
336 if (mxcsr & SSE_PM)
337 p += mdb_snprintf(p, (size_t)(end - p), "|PM");
338
339 if ((mxcsr & SSE_RC) == (SSE_RD|SSE_RU))
340 p += mdb_snprintf(p, (size_t)(end - p), "|RTZ");
341 else if (mxcsr & SSE_RD)
342 p += mdb_snprintf(p, (size_t)(end - p), "|RD");
343 else if (mxcsr & SSE_RU)
344 p += mdb_snprintf(p, (size_t)(end - p), "|RU");
345 else
346 p += mdb_snprintf(p, (size_t)(end - p), "|RTN");
347
348 if (mxcsr & SSE_FZ)
349 p += mdb_snprintf(p, (size_t)(end - p), "|FZ");
350
351 if (buf[0] == '|')
352 return (buf + 1);
353 return ("0");
354 }
355
356 /*ARGSUSED*/
357 int
358 pt_fpregs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
359 {
360 mdb_tgt_t *t = mdb.m_target;
361 mdb_tgt_tid_t tid;
362 prfpregset_t fprs;
363 struct fpchip_state fps;
364 char buf[256];
365 uint_t top;
366 int i;
367
368 /*
369 * Union for overlaying _fpreg structure on to quad-precision
370 * floating-point value (long double).
371 */
372 union {
373 struct _fpreg reg;
374 long double ld;
375 } fpru;
376
377 /*
378 * Array of strings corresponding to FPU tag word values (see
379 * section 7.3.6 of the Intel Programmer's Reference Manual).
380 */
381 const char *tag_strings[] = { "valid", "zero", "special", "empty" };
382
383 if (argc != 0)
384 return (DCMD_USAGE);
385
386 if (t->t_pshandle == NULL || Pstate(t->t_pshandle) == PS_UNDEAD) {
387 mdb_warn("no process active\n");
388 return (DCMD_ERR);
389 }
390
391 if (Pstate(t->t_pshandle) == PS_LOST) {
392 mdb_warn("debugger has lost control of process\n");
393 return (DCMD_ERR);
394 }
395
396 if (flags & DCMD_ADDRSPEC)
397 tid = (mdb_tgt_tid_t)addr;
398 else
399 tid = PTL_TID(t);
400
401 mdb_printf("AMD64 (80486 chip with SSE)\n");
402
403 if (PTL_GETFPREGS(t, tid, &fprs) != 0) {
404 mdb_warn("failed to get floating point registers");
405 return (DCMD_ERR);
406 }
407
408 bcopy(&fprs.fp_reg_set.fpchip_state, &fps, sizeof (fps));
409
410 fps.status &= 0xffff; /* saved status word is really 16 bits */
411
412 mdb_printf("cw 0x%04x (%s)\n", fps.cw,
413 fpcw2str(fps.cw, buf, sizeof (buf)));
414
415 top = (fps.sw & FPS_TOP) >> 11;
416 mdb_printf("sw 0x%04x (TOP=0t%u) (%s)\n", fps.sw,
417 top, fpsw2str(fps.sw, buf, sizeof (buf)));
418
419 mdb_printf("xcp sw 0x%04x (%s)\n\n", fps.status,
420 fpsw2str(fps.status, buf, sizeof (buf)));
421
422 mdb_printf("fop 0x%x\n", fps.fop);
423 mdb_printf("rip 0x%x\n", fps.rip);
424 mdb_printf("rdp 0x%x\n\n", fps.rdp);
425
426 for (i = 0; i < 8; i++) {
427 /*
428 * Recall that we need to use the current TOP-of-stack value to
429 * associate the _st[] index back to a physical register number,
430 * since tag word indices are physical register numbers. Then
431 * to get the tag value, we shift over two bits for each tag
432 * index, and then grab the bottom two bits.
433 */
434 uint_t tag_index = (i + top) & 7;
435 uint_t tag_fctw = (fps.fctw >> tag_index) & 1;
436 uint_t tag_value;
437 uint_t exp;
438
439 /*
440 * AMD64 stores the tag in a compressed form. It is
441 * necessary to extract the original 2-bit tag value.
442 * See AMD64 Architecture Programmer's Manual Volume 2:
443 * System Programming, Chapter 11.
444 */
445
446 fpru.ld = fps.st[i].__fpr_pad._q;
447 exp = fpru.reg.exponent & 0x7fff;
448
449 if (tag_fctw == 0) {
450 tag_value = 3; /* empty */
451 } else if (exp == 0) {
452 if (fpru.reg.significand[0] == 0 &&
453 fpru.reg.significand[1] == 0 &&
454 fpru.reg.significand[2] == 0 &&
455 fpru.reg.significand[3] == 0)
456 tag_value = 1; /* zero */
457 else
458 tag_value = 2; /* special: denormal */
459 } else if (exp == 0x7fff) {
460 tag_value = 2; /* special: infinity or NaN */
461 } else if (fpru.reg.significand[3] & 0x8000) {
462 tag_value = 0; /* valid */
463 } else {
464 tag_value = 2; /* special: unnormal */
465 }
466
467 mdb_printf("%%st%d 0x%04x.%04x%04x%04x%04x = %lg %s\n",
468 i, fpru.reg.exponent,
469 fpru.reg.significand[3], fpru.reg.significand[2],
470 fpru.reg.significand[1], fpru.reg.significand[0],
471 fpru.ld, tag_strings[tag_value]);
472 }
473
474 mdb_printf("\nmxcsr 0x%04x (%s)\n", fps.mxcsr,
475 fpmxcsr2str(fps.mxcsr, buf, sizeof (buf)));
476 mdb_printf("xcp 0x%04x (%s)\n\n", fps.xstatus,
477 fpmxcsr2str(fps.xstatus, buf, sizeof (buf)));
478
479 for (i = 0; i < 8; i++)
480 mdb_printf("%%xmm%d 0x%08x%08x%08x%08x\n", i,
481 fps.xmm[i]._l[3], fps.xmm[i]._l[2],
482 fps.xmm[i]._l[1], fps.xmm[i]._l[0]);
483
484 return (DCMD_OK);
485 }
486
487 /*ARGSUSED*/
488 int
489 pt_getfpreg(mdb_tgt_t *t, mdb_tgt_tid_t tid, ushort_t rd_num,
490 ushort_t rd_flags, mdb_tgt_reg_t *rp)
491 {
492 return (set_errno(ENOTSUP));
493 }
494
495 /*ARGSUSED*/
496 int
497 pt_putfpreg(mdb_tgt_t *t, mdb_tgt_tid_t tid, ushort_t rd_num,
498 ushort_t rd_flags, mdb_tgt_reg_t rval)
499 {
500 return (set_errno(ENOTSUP));
501 }
502
503 /*ARGSUSED*/
504 void
505 pt_addfpregs(mdb_tgt_t *t)
506 {
507 /* not implemented */
508 }
509
510 /*ARGSUSED*/
511 int
512 pt_frameregs(void *arglim, uintptr_t pc, uint_t argc, const long *argv,
513 const mdb_tgt_gregset_t *gregs, boolean_t pc_faked)
514 {
515 return (set_errno(ENOTSUP));
516 }
517
518 /*ARGSUSED*/
519 const char *
520 pt_disasm(const GElf_Ehdr *ehp)
521 {
522 return ("amd64");
523 }
524
525 /*
526 * Determine the return address for the current frame.
527 */
528 int
529 pt_step_out(mdb_tgt_t *t, uintptr_t *p)
530 {
531 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
532
533 if (Pstate(t->t_pshandle) != PS_STOP)
534 return (set_errno(EMDB_TGTBUSY));
535
536 return (mdb_amd64_step_out(t, p, psp->pr_reg[EIP], psp->pr_reg[EBP],
537 psp->pr_reg[UESP], psp->pr_instr));
538 }
539
540 /*
541 * Return the address of the next instruction following a call, or return -1
542 * and set errno to EAGAIN if the target should just single-step.
543 */
544 int
545 pt_next(mdb_tgt_t *t, uintptr_t *p)
546 {
547 const lwpstatus_t *psp = &Pstatus(t->t_pshandle)->pr_lwp;
548
549 if (Pstate(t->t_pshandle) != PS_STOP)
550 return (set_errno(EMDB_TGTBUSY));
551
552 return (mdb_amd64_next(t, p, psp->pr_reg[REG_RIP], pt_read_instr(t)));
553 }