Print this page
9210 remove KMDB branch debugging support
9211 ::crregs could do with cr2/cr3 support
9209 ::ttrace should be able to filter by thread
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mdb/intel/kmdb/kaif.c
+++ new/usr/src/cmd/mdb/intel/kmdb/kaif.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 + *
25 + * Copyright 2018 Joyent, Inc.
24 26 */
25 27
26 -#pragma ident "%Z%%M% %I% %E% SMI"
27 -
28 28 /*
29 29 * The debugger/"PROM" interface layer
30 30 *
31 31 * It makes more sense on SPARC. In reality, these interfaces deal with three
32 32 * things: setting break/watchpoints, stepping, and interfacing with the KDI to
33 33 * set up kmdb's IDT handlers.
34 34 */
35 35
36 36 #include <kmdb/kmdb_dpi_impl.h>
37 37 #include <kmdb/kmdb_kdi.h>
38 38 #include <kmdb/kmdb_umemglue.h>
39 39 #include <kmdb/kaif.h>
40 40 #include <kmdb/kmdb_io.h>
41 41 #include <kmdb/kaif_start.h>
42 42 #include <mdb/mdb_err.h>
43 43 #include <mdb/mdb_debug.h>
44 44 #include <mdb/mdb_isautil.h>
45 45 #include <mdb/mdb_io_impl.h>
46 46 #include <mdb/mdb_kreg_impl.h>
47 47 #include <mdb/mdb.h>
48 48
49 49 #include <sys/types.h>
50 50 #include <sys/bitmap.h>
51 51 #include <sys/termios.h>
52 52 #include <sys/kdi_impl.h>
53 53
54 54 /*
55 55 * This is the area containing the saved state when we enter
56 56 * via kmdb's IDT entries.
57 57 */
58 58 kdi_cpusave_t *kaif_cpusave;
59 59 int kaif_ncpusave;
60 60 kdi_drreg_t kaif_drreg;
61 61
62 62 uint32_t kaif_waptmap;
63 63
64 64 int kaif_trap_switch;
65 65
66 66 void (*kaif_modchg_cb)(struct modctl *, int);
67 67
68 68 enum {
69 69 M_SYSRET = 0x07, /* after M_ESC */
70 70 M_ESC = 0x0f,
71 71 M_SYSEXIT = 0x35, /* after M_ESC */
72 72 M_REX_LO = 0x40, /* first REX prefix */
73 73 M_REX_HI = 0x4f, /* last REX prefix */
74 74 M_PUSHF = 0x9c, /* pushfl and pushfq */
75 75 M_POPF = 0x9d, /* popfl and popfq */
76 76 M_INT3 = 0xcc,
77 77 M_INTX = 0xcd,
78 78 M_INTO = 0xce,
79 79 M_IRET = 0xcf,
80 80 M_CLI = 0xfa,
81 81 M_STI = 0xfb
82 82 };
83 83
84 84 #define KAIF_BREAKPOINT_INSTR M_INT3
85 85
86 86 #define KAIF_WPPRIV2ID(wp) (int)(uintptr_t)((wp)->wp_priv)
87 87
88 88 #ifdef __amd64
89 89 #define FLAGS_REG_NAME "rflags"
90 90 #else
91 91 #define FLAGS_REG_NAME "eflags"
92 92 #endif
93 93
94 94 /*
95 95 * Called during normal debugger operation and during debugger faults.
96 96 */
97 97 static void
98 98 kaif_enter_mon(void)
99 99 {
100 100 char c;
101 101
102 102 for (;;) {
103 103 mdb_iob_printf(mdb.m_out,
104 104 "%s: Do you really want to reboot? (y/n) ",
105 105 mdb.m_pname);
106 106 mdb_iob_flush(mdb.m_out);
107 107 mdb_iob_clearlines(mdb.m_out);
108 108
109 109 c = kmdb_getchar();
110 110
111 111 if (c == 'n' || c == 'N' || c == CTRL('c'))
112 112 return;
113 113 else if (c == 'y' || c == 'Y') {
114 114 mdb_iob_printf(mdb.m_out, "Rebooting...\n");
115 115
116 116 kmdb_dpi_reboot();
117 117 }
118 118 }
119 119 }
120 120
121 121 static kaif_cpusave_t *
122 122 kaif_cpuid2save(int cpuid)
123 123 {
124 124 kaif_cpusave_t *save;
125 125
126 126 if (cpuid == DPI_MASTER_CPUID)
127 127 return (&kaif_cpusave[kaif_master_cpuid]);
128 128
129 129 if (cpuid < 0 || cpuid >= kaif_ncpusave) {
130 130 (void) set_errno(EINVAL);
131 131 return (NULL);
132 132 }
133 133
134 134 save = &kaif_cpusave[cpuid];
135 135
136 136 if (save->krs_cpu_state != KAIF_CPU_STATE_MASTER &&
137 137 save->krs_cpu_state != KAIF_CPU_STATE_SLAVE) {
138 138 (void) set_errno(EINVAL);
139 139 return (NULL);
140 140 }
141 141
142 142 return (save);
143 143 }
144 144
145 145 static int
146 146 kaif_get_cpu_state(int cpuid)
147 147 {
148 148 kaif_cpusave_t *save;
149 149
150 150 if ((save = kaif_cpuid2save(cpuid)) == NULL)
151 151 return (-1); /* errno is set for us */
152 152
153 153 switch (save->krs_cpu_state) {
154 154 case KAIF_CPU_STATE_MASTER:
155 155 return (DPI_CPU_STATE_MASTER);
156 156 case KAIF_CPU_STATE_SLAVE:
157 157 return (DPI_CPU_STATE_SLAVE);
158 158 default:
159 159 return (set_errno(EINVAL));
160 160 }
161 161 }
162 162
163 163 static int
164 164 kaif_get_master_cpuid(void)
165 165 {
166 166 return (kaif_master_cpuid);
167 167 }
168 168
169 169 static mdb_tgt_gregset_t *
170 170 kaif_kdi_to_gregs(int cpuid)
171 171 {
172 172 kaif_cpusave_t *save;
173 173
174 174 if ((save = kaif_cpuid2save(cpuid)) == NULL)
175 175 return (NULL); /* errno is set for us */
176 176
177 177 /*
178 178 * The saved registers are actually identical to an mdb_tgt_gregset,
179 179 * so we can directly cast here.
180 180 */
181 181 return ((mdb_tgt_gregset_t *)save->krs_gregs);
182 182 }
183 183
184 184 static const mdb_tgt_gregset_t *
185 185 kaif_get_gregs(int cpuid)
186 186 {
187 187 return (kaif_kdi_to_gregs(cpuid));
188 188 }
189 189
190 190 typedef struct kaif_reg_synonyms {
191 191 const char *rs_syn;
192 192 const char *rs_name;
193 193 } kaif_reg_synonyms_t;
194 194
195 195 static kreg_t *
196 196 kaif_find_regp(const char *regname)
197 197 {
198 198 static const kaif_reg_synonyms_t synonyms[] = {
199 199 #ifdef __amd64
200 200 { "pc", "rip" },
201 201 { "sp", "rsp" },
202 202 { "fp", "rbp" },
203 203 #else
204 204 { "pc", "eip" },
205 205 { "sp", "esp" },
206 206 { "fp", "ebp" },
207 207 #endif
208 208 { "tt", "trapno" }
209 209 };
210 210 mdb_tgt_gregset_t *regs;
211 211 int i;
212 212
213 213 if ((regs = kaif_kdi_to_gregs(DPI_MASTER_CPUID)) == NULL)
214 214 return (NULL);
215 215
216 216 for (i = 0; i < sizeof (synonyms) / sizeof (synonyms[0]); i++) {
217 217 if (strcmp(synonyms[i].rs_syn, regname) == 0)
218 218 regname = synonyms[i].rs_name;
219 219 }
220 220
221 221 for (i = 0; mdb_isa_kregs[i].rd_name != NULL; i++) {
222 222 const mdb_tgt_regdesc_t *rd = &mdb_isa_kregs[i];
223 223
224 224 if (strcmp(rd->rd_name, regname) == 0)
225 225 return (®s->kregs[rd->rd_num]);
226 226 }
227 227
228 228 (void) set_errno(ENOENT);
229 229 return (NULL);
230 230 }
231 231
232 232 /*ARGSUSED*/
233 233 static int
234 234 kaif_get_register(const char *regname, kreg_t *valp)
235 235 {
236 236 kreg_t *regp;
237 237
238 238 if ((regp = kaif_find_regp(regname)) == NULL)
239 239 return (-1);
240 240
241 241 *valp = *regp;
242 242
243 243 return (0);
244 244 }
245 245
246 246 static int
247 247 kaif_set_register(const char *regname, kreg_t val)
248 248 {
249 249 kreg_t *regp;
250 250
251 251 if ((regp = kaif_find_regp(regname)) == NULL)
252 252 return (-1);
253 253
254 254 *regp = val;
255 255
256 256 return (0);
257 257 }
258 258
259 259 static int
260 260 kaif_brkpt_arm(uintptr_t addr, mdb_instr_t *instrp)
261 261 {
262 262 mdb_instr_t bkpt = KAIF_BREAKPOINT_INSTR;
263 263
264 264 if (mdb_tgt_vread(mdb.m_target, instrp, sizeof (mdb_instr_t), addr) !=
265 265 sizeof (mdb_instr_t))
266 266 return (-1); /* errno is set for us */
267 267
268 268 if (mdb_tgt_vwrite(mdb.m_target, &bkpt, sizeof (mdb_instr_t), addr) !=
269 269 sizeof (mdb_instr_t))
270 270 return (-1); /* errno is set for us */
271 271
272 272 return (0);
273 273 }
274 274
275 275 static int
276 276 kaif_brkpt_disarm(uintptr_t addr, mdb_instr_t instrp)
277 277 {
278 278 if (mdb_tgt_vwrite(mdb.m_target, &instrp, sizeof (mdb_instr_t), addr) !=
279 279 sizeof (mdb_instr_t))
280 280 return (-1); /* errno is set for us */
281 281
282 282 return (0);
283 283 }
284 284
285 285 /*
286 286 * Intel watchpoints are even more fun than SPARC ones. The Intel architecture
287 287 * manuals refer to watchpoints as breakpoints. For consistency with the
288 288 * terminology used in other portions of kmdb, we will, however, refer to them
289 289 * as watchpoints.
290 290 *
291 291 * Execute, data write, I/O read/write, and data read/write watchpoints are
292 292 * supported by the hardware. Execute watchpoints must be one byte in length,
293 293 * and must be placed on the first byte of the instruction to be watched.
294 294 * Lengths of other watchpoints are more varied.
295 295 *
296 296 * Given that we already have a breakpoint facility, and given the restrictions
297 297 * placed on execute watchpoints, we're going to disallow the creation of
298 298 * execute watchpoints. The others will be fully supported. See the Debugging
299 299 * chapter in both the IA32 and AMD64 System Programming books for more details.
300 300 */
301 301
302 302 #ifdef __amd64
303 303 #define WAPT_DATA_MAX_SIZE 8
304 304 #define WAPT_DATA_SIZES_MSG "1, 2, 4, or 8"
305 305 #else
306 306 #define WAPT_DATA_MAX_SIZE 4
307 307 #define WAPT_DATA_SIZES_MSG "1, 2, or 4"
308 308 #endif
309 309
310 310 static int
311 311 kaif_wapt_validate(kmdb_wapt_t *wp)
312 312 {
313 313 if (wp->wp_type == DPI_WAPT_TYPE_IO) {
314 314 if (wp->wp_wflags != (MDB_TGT_WA_R | MDB_TGT_WA_W)) {
315 315 warn("I/O port watchpoints must be read/write\n");
316 316 return (set_errno(EINVAL));
317 317 }
318 318
319 319 if (wp->wp_size != 1 && wp->wp_size != 2 && wp->wp_size != 4) {
320 320 warn("I/O watchpoint size must be 1, 2, or 4 bytes\n");
321 321 return (set_errno(EINVAL));
322 322 }
323 323
324 324 } else if (wp->wp_type == DPI_WAPT_TYPE_PHYS) {
325 325 warn("physical address watchpoints are not supported on this "
326 326 "platform\n");
327 327 return (set_errno(EMDB_TGTHWNOTSUP));
328 328
329 329 } else {
330 330 if (wp->wp_wflags != (MDB_TGT_WA_R | MDB_TGT_WA_W) &&
331 331 wp->wp_wflags != MDB_TGT_WA_W) {
332 332 warn("watchpoints must be read/write or write-only\n");
333 333 return (set_errno(EINVAL));
334 334 }
335 335
336 336 if ((wp->wp_size & -(wp->wp_size)) != wp->wp_size ||
337 337 wp->wp_size > WAPT_DATA_MAX_SIZE) {
338 338 warn("data watchpoint size must be " WAPT_DATA_SIZES_MSG
339 339 " bytes\n");
340 340 return (set_errno(EINVAL));
341 341 }
342 342
343 343 }
344 344
345 345 if (wp->wp_addr & (wp->wp_size - 1)) {
346 346 warn("%lu-byte watchpoints must be %lu-byte aligned\n",
347 347 (ulong_t)wp->wp_size, (ulong_t)wp->wp_size);
348 348 return (set_errno(EINVAL));
349 349 }
350 350
351 351 return (0);
352 352 }
353 353
354 354 static int
355 355 kaif_wapt_reserve(kmdb_wapt_t *wp)
356 356 {
357 357 int id;
358 358
359 359 for (id = 0; id <= KDI_MAXWPIDX; id++) {
360 360 if (!BT_TEST(&kaif_waptmap, id)) {
361 361 /* found one */
362 362 BT_SET(&kaif_waptmap, id);
363 363 wp->wp_priv = (void *)(uintptr_t)id;
364 364 return (0);
365 365 }
366 366 }
367 367
368 368 return (set_errno(EMDB_WPTOOMANY));
369 369 }
370 370
371 371 static void
372 372 kaif_wapt_release(kmdb_wapt_t *wp)
373 373 {
374 374 int id = KAIF_WPPRIV2ID(wp);
375 375
376 376 ASSERT(BT_TEST(&kaif_waptmap, id));
377 377 BT_CLEAR(&kaif_waptmap, id);
378 378 }
379 379
380 380 /*ARGSUSED*/
381 381 static void
382 382 kaif_wapt_arm(kmdb_wapt_t *wp)
383 383 {
384 384 uint_t rw;
385 385 int hwid = KAIF_WPPRIV2ID(wp);
386 386
387 387 ASSERT(BT_TEST(&kaif_waptmap, hwid));
388 388
389 389 if (wp->wp_type == DPI_WAPT_TYPE_IO)
390 390 rw = KREG_DRCTL_WP_IORW;
391 391 else if (wp->wp_wflags & MDB_TGT_WA_R)
392 392 rw = KREG_DRCTL_WP_RW;
393 393 else if (wp->wp_wflags & MDB_TGT_WA_X)
394 394 rw = KREG_DRCTL_WP_EXEC;
395 395 else
396 396 rw = KREG_DRCTL_WP_WONLY;
397 397
398 398 kaif_drreg.dr_addr[hwid] = wp->wp_addr;
399 399
400 400 kaif_drreg.dr_ctl &= ~KREG_DRCTL_WP_MASK(hwid);
401 401 kaif_drreg.dr_ctl |= KREG_DRCTL_WP_LENRW(hwid, wp->wp_size - 1, rw);
402 402 kaif_drreg.dr_ctl |= KREG_DRCTL_WPEN(hwid);
403 403 kmdb_kdi_update_drreg(&kaif_drreg);
404 404 }
405 405
406 406 /*ARGSUSED*/
407 407 static void
408 408 kaif_wapt_disarm(kmdb_wapt_t *wp)
409 409 {
410 410 int hwid = KAIF_WPPRIV2ID(wp);
411 411
412 412 ASSERT(BT_TEST(&kaif_waptmap, hwid));
413 413
414 414 kaif_drreg.dr_addr[hwid] = 0;
415 415 kaif_drreg.dr_ctl &= ~(KREG_DRCTL_WP_MASK(hwid) |
416 416 KREG_DRCTL_WPEN_MASK(hwid));
417 417 kmdb_kdi_update_drreg(&kaif_drreg);
418 418 }
419 419
420 420 /*ARGSUSED*/
421 421 static int
422 422 kaif_wapt_match(kmdb_wapt_t *wp)
423 423 {
424 424 int hwid = KAIF_WPPRIV2ID(wp);
425 425 uint32_t mask = KREG_DRSTAT_WP_MASK(hwid);
426 426 int n = 0;
427 427 int i;
428 428
429 429 ASSERT(BT_TEST(&kaif_waptmap, hwid));
430 430
431 431 for (i = 0; i < kaif_ncpusave; i++)
432 432 n += (kaif_cpusave[i].krs_dr.dr_stat & mask) != 0;
433 433
434 434 return (n);
435 435 }
436 436
437 437 static int
438 438 kaif_step(void)
439 439 {
440 440 kreg_t pc, fl, oldfl, newfl, sp;
441 441 mdb_tgt_addr_t npc;
442 442 mdb_instr_t instr;
443 443 int emulated = 0, rchk = 0;
444 444 size_t pcoff = 0;
445 445
446 446 (void) kmdb_dpi_get_register("pc", &pc);
447 447
448 448 if ((npc = mdb_dis_nextins(mdb.m_disasm, mdb.m_target,
449 449 MDB_TGT_AS_VIRT, pc)) == pc) {
450 450 warn("failed to decode instruction at %a for step\n", pc);
451 451 return (set_errno(EINVAL));
452 452 }
453 453
454 454 /*
455 455 * Stepping behavior depends on the type of instruction. It does not
456 456 * depend on the presence of a REX prefix, as the action we take for a
457 457 * given instruction doesn't currently vary for 32-bit instructions
458 458 * versus their 64-bit counterparts.
459 459 */
460 460 do {
461 461 if (mdb_tgt_vread(mdb.m_target, &instr, sizeof (mdb_instr_t),
462 462 pc + pcoff) != sizeof (mdb_instr_t)) {
463 463 warn("failed to read at %p for step",
464 464 (void *)(pc + pcoff));
465 465 return (-1);
466 466 }
467 467 } while (pcoff++, (instr >= M_REX_LO && instr <= M_REX_HI && !rchk++));
468 468
469 469 switch (instr) {
470 470 case M_IRET:
471 471 warn("iret cannot be stepped\n");
472 472 return (set_errno(EMDB_TGTNOTSUP));
473 473
474 474 case M_INT3:
475 475 case M_INTX:
476 476 case M_INTO:
477 477 warn("int cannot be stepped\n");
478 478 return (set_errno(EMDB_TGTNOTSUP));
479 479
480 480 case M_ESC:
481 481 if (mdb_tgt_vread(mdb.m_target, &instr, sizeof (mdb_instr_t),
482 482 pc + pcoff) != sizeof (mdb_instr_t)) {
483 483 warn("failed to read at %p for step",
484 484 (void *)(pc + pcoff));
485 485 return (-1);
486 486 }
487 487
488 488 switch (instr) {
489 489 case M_SYSRET:
490 490 warn("sysret cannot be stepped\n");
491 491 return (set_errno(EMDB_TGTNOTSUP));
492 492 case M_SYSEXIT:
493 493 warn("sysexit cannot be stepped\n");
494 494 return (set_errno(EMDB_TGTNOTSUP));
495 495 }
496 496 break;
497 497
498 498 /*
499 499 * Some instructions need to be emulated. We need to prevent direct
500 500 * manipulations of EFLAGS, so we'll emulate cli, sti. pushfl and
501 501 * popfl also receive special handling, as they manipulate both EFLAGS
502 502 * and %esp.
503 503 */
504 504 case M_CLI:
505 505 (void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
506 506 fl &= ~KREG_EFLAGS_IF_MASK;
507 507 (void) kmdb_dpi_set_register(FLAGS_REG_NAME, fl);
508 508
509 509 emulated = 1;
510 510 break;
511 511
512 512 case M_STI:
513 513 (void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
514 514 fl |= (1 << KREG_EFLAGS_IF_SHIFT);
515 515 (void) kmdb_dpi_set_register(FLAGS_REG_NAME, fl);
516 516
517 517 emulated = 1;
518 518 break;
519 519
520 520 case M_POPF:
521 521 /*
522 522 * popfl will restore a pushed EFLAGS from the stack, and could
523 523 * in so doing cause IF to be turned on, if only for a brief
524 524 * period. To avoid this, we'll secretly replace the stack's
525 525 * EFLAGS with our decaffeinated brand. We'll then manually
526 526 * load our EFLAGS copy with the real verion after the step.
527 527 */
528 528 (void) kmdb_dpi_get_register("sp", &sp);
529 529 (void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
530 530
531 531 if (mdb_tgt_vread(mdb.m_target, &newfl, sizeof (kreg_t),
532 532 sp) != sizeof (kreg_t)) {
533 533 warn("failed to read " FLAGS_REG_NAME
534 534 " at %p for popfl step\n", (void *)sp);
535 535 return (set_errno(EMDB_TGTNOTSUP)); /* XXX ? */
536 536 }
537 537
538 538 fl = (fl & ~KREG_EFLAGS_IF_MASK) | KREG_EFLAGS_TF_MASK;
539 539
540 540 if (mdb_tgt_vwrite(mdb.m_target, &fl, sizeof (kreg_t),
541 541 sp) != sizeof (kreg_t)) {
542 542 warn("failed to update " FLAGS_REG_NAME
543 543 " at %p for popfl step\n", (void *)sp);
544 544 return (set_errno(EMDB_TGTNOTSUP)); /* XXX ? */
545 545 }
546 546 break;
547 547 }
548 548
549 549 if (emulated) {
550 550 (void) kmdb_dpi_set_register("pc", npc);
551 551 return (0);
552 552 }
553 553
554 554 /* Do the step with IF off, and TF (step) on */
555 555 (void) kmdb_dpi_get_register(FLAGS_REG_NAME, &oldfl);
556 556 (void) kmdb_dpi_set_register(FLAGS_REG_NAME,
557 557 ((oldfl | (1 << KREG_EFLAGS_TF_SHIFT)) & ~KREG_EFLAGS_IF_MASK));
558 558
559 559 kmdb_dpi_resume_master(); /* ... there and back again ... */
560 560
561 561 /* EFLAGS has now changed, and may require tuning */
562 562
563 563 switch (instr) {
564 564 case M_POPF:
565 565 /*
566 566 * Use the EFLAGS we grabbed before the pop - see the pre-step
567 567 * M_POPFL comment.
568 568 */
569 569 (void) kmdb_dpi_set_register(FLAGS_REG_NAME, newfl);
570 570 return (0);
571 571
572 572 case M_PUSHF:
573 573 /*
574 574 * We pushed our modified EFLAGS (with IF and TF turned off)
575 575 * onto the stack. Replace the pushed version with our
576 576 * unmodified one.
577 577 */
578 578 (void) kmdb_dpi_get_register("sp", &sp);
579 579
580 580 if (mdb_tgt_vwrite(mdb.m_target, &oldfl, sizeof (kreg_t),
581 581 sp) != sizeof (kreg_t)) {
582 582 warn("failed to update pushed " FLAGS_REG_NAME
583 583 " at %p after pushfl step\n", (void *)sp);
584 584 return (set_errno(EMDB_TGTNOTSUP)); /* XXX ? */
585 585 }
586 586
587 587 /* Go back to using the EFLAGS we were using before the step */
588 588 (void) kmdb_dpi_set_register(FLAGS_REG_NAME, oldfl);
589 589 return (0);
590 590
591 591 default:
592 592 /*
593 593 * The stepped instruction may have altered EFLAGS. We only
594 594 * really care about the value of IF, and we know the stepped
595 595 * instruction didn't alter it, so we can simply copy the
↓ open down ↓ |
558 lines elided |
↑ open up ↑ |
596 596 * pre-step value. We'll also need to turn TF back off.
597 597 */
598 598 (void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
599 599 (void) kmdb_dpi_set_register(FLAGS_REG_NAME,
600 600 ((fl & ~(KREG_EFLAGS_TF_MASK|KREG_EFLAGS_IF_MASK)) |
601 601 (oldfl & KREG_EFLAGS_IF_MASK)));
602 602 return (0);
603 603 }
604 604 }
605 605
606 -/*
607 - * The target has already configured the chip for branch step, leaving us to
608 - * actually make the machine go. Due to a number of issues involving
609 - * the potential alteration of system state via instructions like sti, cli,
610 - * pushfl, and popfl, we're going to treat this like a normal system resume.
611 - * All CPUs will be released, on the kernel's IDT. Our primary concern is
612 - * the alteration/storage of our TF'd EFLAGS via pushfl and popfl. There's no
613 - * real workaround - we don't have opcode breakpoints - so the best we can do is
614 - * to ensure that the world won't end if someone does bad things to EFLAGS.
615 - *
616 - * Two things can happen:
617 - * 1. EFLAGS.TF may be cleared, either maliciously or via a popfl from saved
618 - * state. The CPU will continue execution beyond the branch, and will not
619 - * reenter the debugger unless brought/sent in by other means.
620 - * 2. Someone may pushlf the TF'd EFLAGS, and may stash a copy of it somewhere.
621 - * When the saved version is popfl'd back into place, the debugger will be
622 - * re-entered on a single-step trap.
623 - */
624 -static void
625 -kaif_step_branch(void)
626 -{
627 - kreg_t fl;
628 -
629 - (void) kmdb_dpi_get_register(FLAGS_REG_NAME, &fl);
630 - (void) kmdb_dpi_set_register(FLAGS_REG_NAME,
631 - (fl | (1 << KREG_EFLAGS_TF_SHIFT)));
632 -
633 - kmdb_dpi_resume_master();
634 -
635 - (void) kmdb_dpi_set_register(FLAGS_REG_NAME, fl);
636 -}
637 -
638 606 /*ARGSUSED*/
639 607 static uintptr_t
640 608 kaif_call(uintptr_t funcva, uint_t argc, const uintptr_t argv[])
641 609 {
642 610 return (kaif_invoke(funcva, argc, argv));
643 611 }
644 612
645 613 static void
646 614 dump_crumb(kdi_crumb_t *krmp)
647 615 {
648 616 kdi_crumb_t krm;
649 617
650 618 if (mdb_vread(&krm, sizeof (kdi_crumb_t), (uintptr_t)krmp) !=
651 619 sizeof (kdi_crumb_t)) {
652 620 warn("failed to read crumb at %p", krmp);
653 621 return;
654 622 }
655 623
656 624 mdb_printf("state: ");
657 625 switch (krm.krm_cpu_state) {
658 626 case KAIF_CPU_STATE_MASTER:
659 627 mdb_printf("M");
660 628 break;
661 629 case KAIF_CPU_STATE_SLAVE:
662 630 mdb_printf("S");
663 631 break;
664 632 default:
665 633 mdb_printf("%d", krm.krm_cpu_state);
666 634 }
667 635
668 636 mdb_printf(" trapno %3d sp %08x flag %d pc %p %A\n",
669 637 krm.krm_trapno, krm.krm_sp, krm.krm_flag, krm.krm_pc, krm.krm_pc);
670 638 }
671 639
672 640 static void
673 641 dump_crumbs(kaif_cpusave_t *save)
674 642 {
675 643 int i;
676 644
677 645 for (i = KDI_NCRUMBS; i > 0; i--) {
678 646 uint_t idx = (save->krs_curcrumbidx + i) % KDI_NCRUMBS;
679 647 dump_crumb(&save->krs_crumbs[idx]);
680 648 }
681 649 }
682 650
683 651 static void
684 652 kaif_dump_crumbs(uintptr_t addr, int cpuid)
685 653 {
686 654 int i;
687 655
688 656 if (addr != NULL) {
689 657 /* dump_crumb will protect us against bogus addresses */
690 658 dump_crumb((kdi_crumb_t *)addr);
691 659
692 660 } else if (cpuid != -1) {
693 661 if (cpuid < 0 || cpuid >= kaif_ncpusave)
694 662 return;
695 663
696 664 dump_crumbs(&kaif_cpusave[cpuid]);
697 665
698 666 } else {
699 667 for (i = 0; i < kaif_ncpusave; i++) {
700 668 kaif_cpusave_t *save = &kaif_cpusave[i];
701 669
702 670 if (save->krs_cpu_state == KAIF_CPU_STATE_NONE)
703 671 continue;
704 672
705 673 mdb_printf("%sCPU %d crumbs: (curidx %d)\n",
706 674 (i == 0 ? "" : "\n"), i, save->krs_curcrumbidx);
707 675
708 676 dump_crumbs(save);
709 677 }
710 678 }
711 679 }
712 680
713 681 static void
714 682 kaif_modchg_register(void (*func)(struct modctl *, int))
715 683 {
716 684 kaif_modchg_cb = func;
↓ open down ↓ |
69 lines elided |
↑ open up ↑ |
717 685 }
718 686
719 687 static void
720 688 kaif_modchg_cancel(void)
721 689 {
722 690 ASSERT(kaif_modchg_cb != NULL);
723 691
724 692 kaif_modchg_cb = NULL;
725 693 }
726 694
727 -static void
728 -kaif_msr_add(const kdi_msr_t *msrs)
729 -{
730 - kdi_msr_t *save;
731 - size_t nr_msrs = 0;
732 - size_t i;
733 -
734 - while (msrs[nr_msrs].msr_num != 0)
735 - nr_msrs++;
736 - /* we want to copy the terminating kdi_msr_t too */
737 - nr_msrs++;
738 -
739 - save = mdb_zalloc(sizeof (kdi_msr_t) * nr_msrs * kaif_ncpusave,
740 - UM_SLEEP);
741 -
742 - for (i = 0; i < kaif_ncpusave; i++)
743 - bcopy(msrs, &save[nr_msrs * i], sizeof (kdi_msr_t) * nr_msrs);
744 -
745 - kmdb_kdi_set_debug_msrs(save);
746 -}
747 -
748 -static uint64_t
749 -kaif_msr_get(int cpuid, uint_t num)
750 -{
751 - kdi_cpusave_t *save;
752 - kdi_msr_t *msr;
753 - int i;
754 -
755 - if ((save = kaif_cpuid2save(cpuid)) == NULL)
756 - return (-1); /* errno is set for us */
757 -
758 - msr = save->krs_msr;
759 -
760 - for (i = 0; msr[i].msr_num != 0; i++) {
761 - if (msr[i].msr_num == num && (msr[i].msr_type & KDI_MSR_READ))
762 - return (msr[i].kdi_msr_val);
763 - }
764 -
765 - return (0);
766 -}
767 -
768 695 void
769 696 kaif_trap_set_debugger(void)
770 697 {
771 698 kmdb_kdi_idt_switch(NULL);
772 699 }
773 700
774 701 void
775 702 kaif_trap_set_saved(kaif_cpusave_t *cpusave)
776 703 {
777 704 kmdb_kdi_idt_switch(cpusave);
778 705 }
779 706
780 707 static void
781 708 kaif_vmready(void)
782 709 {
783 710 }
784 711
785 712 void
786 713 kaif_memavail(caddr_t base, size_t len)
787 714 {
788 715 int ret;
789 716 /*
790 717 * In the unlikely event that someone is stepping through this routine,
791 718 * we need to make sure that the KDI knows about the new range before
792 719 * umem gets it. That way the entry code can recognize stacks
793 720 * allocated from the new region.
794 721 */
795 722 kmdb_kdi_memrange_add(base, len);
796 723 ret = mdb_umem_add(base, len);
797 724 ASSERT(ret == 0);
798 725 }
799 726
800 727 void
801 728 kaif_mod_loaded(struct modctl *modp)
802 729 {
803 730 if (kaif_modchg_cb != NULL)
804 731 kaif_modchg_cb(modp, 1);
805 732 }
806 733
807 734 void
808 735 kaif_mod_unloading(struct modctl *modp)
809 736 {
810 737 if (kaif_modchg_cb != NULL)
811 738 kaif_modchg_cb(modp, 0);
812 739 }
813 740
814 741 void
815 742 kaif_handle_fault(greg_t trapno, greg_t pc, greg_t sp, int cpuid)
816 743 {
817 744 kmdb_dpi_handle_fault((kreg_t)trapno, (kreg_t)pc,
818 745 (kreg_t)sp, cpuid);
819 746 }
820 747
821 748 static kdi_debugvec_t kaif_dvec = {
822 749 NULL, /* dv_kctl_vmready */
823 750 NULL, /* dv_kctl_memavail */
824 751 NULL, /* dv_kctl_modavail */
825 752 NULL, /* dv_kctl_thravail */
826 753 kaif_vmready,
827 754 kaif_memavail,
828 755 kaif_mod_loaded,
829 756 kaif_mod_unloading,
830 757 kaif_handle_fault
831 758 };
832 759
833 760 void
834 761 kaif_kdi_entry(kdi_cpusave_t *cpusave)
835 762 {
836 763 int ret = kaif_main_loop(cpusave);
837 764 ASSERT(ret == KAIF_CPU_CMD_RESUME ||
838 765 ret == KAIF_CPU_CMD_RESUME_MASTER);
839 766 }
840 767
841 768 /*ARGSUSED*/
842 769 void
843 770 kaif_activate(kdi_debugvec_t **dvecp, uint_t flags)
844 771 {
845 772 kmdb_kdi_activate(kaif_kdi_entry, kaif_cpusave, kaif_ncpusave);
846 773 *dvecp = &kaif_dvec;
847 774 }
848 775
849 776 static int
850 777 kaif_init(kmdb_auxv_t *kav)
851 778 {
852 779 /* Allocate the per-CPU save areas */
853 780 kaif_cpusave = mdb_zalloc(sizeof (kaif_cpusave_t) * kav->kav_ncpu,
854 781 UM_SLEEP);
855 782 kaif_ncpusave = kav->kav_ncpu;
856 783
857 784 kaif_modchg_cb = NULL;
858 785
859 786 kaif_waptmap = 0;
860 787
861 788 kaif_trap_switch = (kav->kav_flags & KMDB_AUXV_FL_NOTRPSWTCH) == 0;
862 789
863 790 return (0);
864 791 }
865 792
866 793 dpi_ops_t kmdb_dpi_ops = {
867 794 kaif_init,
868 795 kaif_activate,
869 796 kmdb_kdi_deactivate,
870 797 kaif_enter_mon,
871 798 kaif_modchg_register,
872 799 kaif_modchg_cancel,
873 800 kaif_get_cpu_state,
874 801 kaif_get_master_cpuid,
875 802 kaif_get_gregs,
876 803 kaif_get_register,
↓ open down ↓ |
99 lines elided |
↑ open up ↑ |
877 804 kaif_set_register,
878 805 kaif_brkpt_arm,
879 806 kaif_brkpt_disarm,
880 807 kaif_wapt_validate,
881 808 kaif_wapt_reserve,
882 809 kaif_wapt_release,
883 810 kaif_wapt_arm,
884 811 kaif_wapt_disarm,
885 812 kaif_wapt_match,
886 813 kaif_step,
887 - kaif_step_branch,
888 814 kaif_call,
889 815 kaif_dump_crumbs,
890 - kaif_msr_add,
891 - kaif_msr_get,
892 816 };
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX