1 /* $NetBSD: db_machdep.c,v 1.64 2026/11/10 03:43:27 thorpej Exp $ */ 2 3 /* 4 * :set tabs=4 5 * 6 * Mach Operating System 7 * Copyright (c) 1991,1990 Carnegie Mellon University 8 * All Rights Reserved. 9 * 10 * Permission to use, copy, modify and distribute this software and its 11 * documentation is hereby granted, provided that both the copyright 12 * notice and this permission notice appear in all copies of the 13 * software, derivative works or modified versions, and any portions 14 * thereof, and that both notices appear in supporting documentation. 15 * 16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 17 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 18 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 19 * 20 * Carnegie Mellon requests users of this software to return to 21 * 22 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU 23 * School of Computer Science 24 * Carnegie Mellon University 25 * Pittsburgh PA 15213-3890 26 * 27 * any improvements or extensions that they make and grant Carnegie the 28 * rights to redistribute these changes. 29 * 30 * db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU) 31 * 32 * VAX enhancements by cmcmanis (at) mcmanis.com no rights reserved :-) 33 * 34 */ 35 36 /* 37 * Interface to new debugger. 38 * Taken from i386 port and modified for vax. 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: db_machdep.c,v 1.64 2026/11/10 03:43:27 thorpej Exp $"); 43 44 #include "opt_multiprocessor.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/cpu.h> 49 #include <sys/device.h> 50 #include <sys/intr.h> 51 #include <sys/proc.h> 52 #include <sys/reboot.h> 53 54 #include <dev/cons.h> 55 56 #include <machine/db_machdep.h> 57 #include <machine/trap.h> 58 #include <machine/frame.h> 59 #include <machine/rpb.h> 60 #include <vax/vax/gencons.h> 61 62 #include <ddb/db_sym.h> 63 #include <ddb/db_command.h> 64 #include <ddb/db_output.h> 65 #include <ddb/db_extern.h> 66 #include <ddb/db_access.h> 67 #include <ddb/db_active.h> 68 #include <ddb/db_interface.h> 69 #include <ddb/db_variables.h> 70 #include <ddb/db_proc.h> 71 72 #include "ioconf.h" 73 74 db_regs_t ddb_regs; 75 76 void kdbprinttrap(int, int); 77 78 static int splsave; /* IPL before entering debugger */ 79 80 #ifdef MULTIPROCESSOR 81 static struct cpu_info *stopcpu; 82 /* 83 * Only the master CPU is allowed to enter DDB, but the correct frames 84 * must still be there. Keep the state-machine here. 85 */ 86 static int 87 pause_cpus(void) 88 { 89 volatile struct cpu_info *ci = curcpu(); 90 91 if (stopcpu == NULL) { 92 stopcpu = curcpu(); 93 cpu_send_ipi(IPI_DEST_ALL, IPI_DDB); 94 } 95 if ((ci->ci_flags & CI_MASTERCPU) == 0) { 96 ci->ci_flags |= CI_STOPPED; 97 while (ci->ci_flags & CI_STOPPED) 98 ; 99 return 1; 100 } else 101 return 0; 102 } 103 104 static void 105 resume_cpus(void) 106 { 107 struct cpu_info *ci; 108 int i; 109 110 stopcpu = NULL; 111 for (i = 0; i < cpu_cd.cd_ndevs; i++) { 112 if ((ci = device_lookup_private(&cpu_cd, i)) == NULL) 113 continue; 114 ci->ci_flags &= ~CI_STOPPED; 115 } 116 } 117 #endif 118 /* 119 * VAX Call frame on the stack, this from 120 * "Computer Programming and Architecture, The VAX-11" 121 * Henry Levy & Richard Eckhouse Jr. 122 * ISBN 0-932376-07-X 123 */ 124 typedef struct __vax_frame { 125 u_int vax_cond; /* condition handler */ 126 u_int vax_psw:16; /* 16 bit processor status word */ 127 u_int vax_regs:12; /* Register save mask. */ 128 u_int vax_zero:1; /* Always zero */ 129 u_int vax_calls:1; /* True if CALLS, false if CALLG */ 130 u_int vax_spa:2; /* Stack pointer alignment */ 131 u_int *vax_ap; /* argument pointer */ 132 struct __vax_frame *vax_fp; /* frame pointer of previous frame */ 133 u_int vax_pc; /* program counter */ 134 u_int vax_args[1]; /* 0 or more arguments */ 135 } VAX_CALLFRAME; 136 137 /* 138 * DDB is called by either <ESC> - D on keyboard, via a TRACE or 139 * BPT trap or from kernel, normally as a result of a panic. 140 * If it is the result of a panic, set the ddb register frame to 141 * contain the registers when panic was called. (easy to debug). 142 */ 143 void 144 kdb_trap(struct trapframe *tf) 145 { 146 int s; 147 #ifdef MULTIPROCESSOR 148 struct cpu_info *ci = curcpu(); 149 #endif 150 151 switch (tf->tf_trap) { 152 case T_BPTFLT: /* breakpoint */ 153 case T_TRCTRAP: /* single_step */ 154 break; 155 156 /* XXX todo: should be migrated to use VAX_CALLFRAME at some point */ 157 case T_KDBTRAP: 158 #ifndef MULTIPROCESSOR /* No fancy panic stack conversion here */ 159 if (panicstr) { 160 struct callsframe *pf, *df; 161 162 df = (void *)tf->tf_fp; /* start of debug's calls */ 163 pf = (void *)df->ca_fp; /* start of panic's calls */ 164 memcpy(&ddb_regs.tf_r0, &pf->ca_argno, sizeof(int) * 12); 165 ddb_regs.tf_fp = pf->ca_fp; 166 ddb_regs.tf_pc = pf->ca_pc; 167 ddb_regs.tf_ap = pf->ca_ap; 168 ddb_regs.tf_sp = (unsigned)pf; 169 ddb_regs.tf_psl = tf->tf_psl & ~0x1fffe0; 170 ddb_regs.tf_psl |= pf->ca_maskpsw & 0xffe0; 171 ddb_regs.tf_psl |= (splsave << 16); 172 } 173 #endif 174 break; 175 176 default: 177 if ((boothowto & RB_KDB) == 0) 178 return; 179 180 kdbprinttrap(tf->tf_trap, tf->tf_code); 181 if (db_recover != 0) { 182 db_error("Faulted in DDB; continuing...\n"); 183 /*NOTREACHED*/ 184 } 185 } 186 187 #ifdef MULTIPROCESSOR 188 ci->ci_ddb_regs = tf; 189 if (pause_cpus()) 190 return; 191 #endif 192 #ifndef MULTIPROCESSOR 193 if (!panicstr) 194 memcpy(&ddb_regs, tf, sizeof(struct trapframe)); 195 #else 196 memcpy(&ddb_regs, stopcpu->ci_ddb_regs, sizeof(struct trapframe)); 197 printf("stopped on CPU %d\n", stopcpu->ci_cpuid); 198 #endif 199 200 /* XXX Should switch to interrupt stack here, if needed. */ 201 202 s = splhigh(); 203 db_active++; 204 cnpollc(true); 205 db_trap(tf->tf_trap, tf->tf_code); 206 cnpollc(false); 207 db_active--; 208 splx(s); 209 210 #ifndef MULTIPROCESSOR 211 if (!panicstr) 212 memcpy(tf, &ddb_regs, sizeof(struct trapframe)); 213 #else 214 memcpy(stopcpu->ci_ddb_regs, &ddb_regs, sizeof(struct trapframe)); 215 #endif 216 tf->tf_sp = mfpr(PR_USP); 217 #ifdef MULTIPROCESSOR 218 rpb.wait = 0; 219 resume_cpus(); 220 #endif 221 } 222 223 extern const char * const traptypes[]; 224 extern int no_traps; 225 226 /* 227 * Print trap reason. 228 */ 229 void 230 kdbprinttrap(int type, int code) 231 { 232 db_printf("kernel: "); 233 if (type >= no_traps || type < 0) 234 db_printf("type %d", type); 235 else 236 db_printf("%s", traptypes[type]); 237 db_printf(" trap, code=%x\n", code); 238 } 239 240 /* 241 * Check whether an address is accessible. 242 */ 243 bool 244 db_validate_address(vaddr_t addr) 245 { 246 struct proc *p = curproc; 247 struct pmap *pmap; 248 249 if (!p || !p->p_vmspace || !p->p_vmspace->vm_map.pmap || 250 addr >= VM_MIN_KERNEL_ADDRESS) 251 pmap = pmap_kernel(); 252 else 253 pmap = p->p_vmspace->vm_map.pmap; 254 255 return (pmap_extract(pmap, addr, NULL)); 256 } 257 258 /* 259 * Read bytes from kernel address space for debugger. 260 */ 261 void 262 db_read_bytes(vaddr_t addr, size_t size, char *data) 263 { 264 if (!db_validate_address(addr)) { 265 db_printf("address 0x%lx is inaccessible\n", addr); 266 db_error(NULL); 267 /*NOTREACHED*/ 268 } 269 270 memcpy(data, (void *)addr, size); 271 } 272 273 /* 274 * Write bytes to kernel address space for debugger. 275 */ 276 void 277 db_write_bytes(vaddr_t addr, size_t size, const char *data) 278 { 279 if (!db_validate_address(addr)) { 280 db_printf("address 0x%lx is inaccessible\n", addr); 281 db_error(NULL); 282 /*NOTREACHED*/ 283 } 284 285 memcpy((void *)addr, data, size); 286 } 287 288 void 289 Debugger(void) 290 { 291 splsave = splx(0xe); /* XXX WRONG (this can lower IPL) */ 292 setsoftddb(); /* beg for debugger */ 293 splx(splsave); 294 } 295 296 /* 297 * Machine register set. 298 */ 299 const struct db_variable db_regs[] = { 300 {"r0", &ddb_regs.tf_r0, FCN_NULL}, 301 {"r1", &ddb_regs.tf_r1, FCN_NULL}, 302 {"r2", &ddb_regs.tf_r2, FCN_NULL}, 303 {"r3", &ddb_regs.tf_r3, FCN_NULL}, 304 {"r4", &ddb_regs.tf_r4, FCN_NULL}, 305 {"r5", &ddb_regs.tf_r5, FCN_NULL}, 306 {"r6", &ddb_regs.tf_r6, FCN_NULL}, 307 {"r7", &ddb_regs.tf_r7, FCN_NULL}, 308 {"r8", &ddb_regs.tf_r8, FCN_NULL}, 309 {"r9", &ddb_regs.tf_r9, FCN_NULL}, 310 {"r10", &ddb_regs.tf_r10, FCN_NULL}, 311 {"r11", &ddb_regs.tf_r11, FCN_NULL}, 312 {"ap", &ddb_regs.tf_ap, FCN_NULL}, 313 {"fp", &ddb_regs.tf_fp, FCN_NULL}, 314 {"sp", &ddb_regs.tf_sp, FCN_NULL}, 315 {"pc", &ddb_regs.tf_pc, FCN_NULL}, 316 {"psl", &ddb_regs.tf_psl, FCN_NULL}, 317 }; 318 const struct db_variable * const db_eregs = db_regs + __arraycount(db_regs); 319 320 #define IN_USERLAND(x) (((u_int)(x) & 0x80000000) == 0) 321 322 /* 323 * Dump a stack traceback. Takes two arguments: 324 * fp - CALL FRAME pointer 325 * pr - printf-like function pointer for output 326 */ 327 static void 328 db_dump_stack(VAX_CALLFRAME *fp, void (*pr)(const char *, ...)) 329 { 330 u_int nargs, arg_base; 331 VAX_CALLFRAME *tmp_frame; 332 db_expr_t diff; 333 db_sym_t sym; 334 const char *symname; 335 extern int sret; 336 extern unsigned int etext; 337 338 (*pr)("Stack traceback:\n"); 339 if (IN_USERLAND(fp)) { 340 (*pr)("\t Process is executing in user space.\n"); 341 return; 342 } 343 344 if (!db_validate_address((vaddr_t)fp)) { 345 (*pr)("\t frame 0x%x inaccessible.\n", fp); 346 return; 347 } 348 349 while (!IN_USERLAND(fp->vax_fp) && 350 db_validate_address((vaddr_t)fp->vax_fp)) { 351 #define VAX_NUMARGS(x) ((x) & 0xff) 352 u_int pc = fp->vax_pc; 353 354 /* 355 * Figure out the arguments by using a bit of subtlety. 356 * As the argument pointer may have been used as a temporary 357 * by the callee ... recreate what it would have pointed to 358 * as follows: 359 * The vax_regs value is a 12 bit bitmask of the registers 360 * that were saved on the stack, corresponding to R0 to R11. 361 * By counting those bits, we get a value for arg_base so that 362 * args[arg_base] points to the longword that identifies the 363 * number of arguments. 364 * arg_base+1 - arg_base+n are the argument pointers/contents. 365 */ 366 367 /* 368 * If this was due to a trap/fault, pull the correct pc 369 * out of the trap frame. 370 */ 371 if (pc == (u_int) &sret && fp->vax_fp != 0) { 372 struct trapframe *tf; 373 374 /* Count saved register bits */ 375 arg_base = popcount(fp->vax_regs); 376 377 tf = (struct trapframe *) &fp->vax_args[arg_base + 2]; 378 if (!db_validate_address((vaddr_t)tf)) { 379 (*pr)("0x%lx: trap frame inaccessible.\n", tf); 380 return; 381 } 382 383 (*pr)("0x%lx: trap type=0x%lx code=0x%lx pc=0x%lx " 384 "psl=0x%lx\n", tf, tf->tf_trap, tf->tf_code, 385 tf->tf_pc, tf->tf_psl); 386 pc = tf->tf_pc; 387 } 388 389 diff = INT_MAX; 390 symname = NULL; 391 if (pc >= VM_MIN_KERNEL_ADDRESS && pc < (u_int) &etext) { 392 sym = db_search_symbol(pc, DB_STGY_ANY, &diff); 393 db_symbol_values(sym, &symname, 0); 394 } 395 if (symname != NULL) 396 (*pr)("0x%lx: %s+0x%lx(", fp, symname, diff); 397 else 398 (*pr)("0x%lx: %#x(", fp, pc); 399 400 /* First get the frame that called this function ... */ 401 tmp_frame = fp->vax_fp; 402 403 if (!tmp_frame->vax_calls) { 404 (*pr)("(Found CALLG, giving up)\n"); 405 return; 406 } 407 408 /* Count saved register bits */ 409 arg_base = popcount(tmp_frame->vax_regs); 410 411 /* number of arguments is then pointed to by vax_args[arg_base] */ 412 nargs = VAX_NUMARGS(tmp_frame->vax_args[arg_base]); 413 if (nargs) { 414 nargs--; /* reduce by one for formatting niceties */ 415 arg_base++; /* skip past the actual number of arguments */ 416 while (nargs--) 417 (*pr)("%#x,", tmp_frame->vax_args[arg_base++]); 418 419 /* now print out the last arg with closing brace and \n */ 420 (*pr)("%#x)\n", tmp_frame->vax_args[arg_base]); 421 } else 422 (*pr)("void)\n"); 423 /* move to the next frame */ 424 fp = fp->vax_fp; 425 } 426 } 427 428 static void 429 db_trace_process(struct lwp *l, struct proc *p, void (*pr)(const char *, ...)) 430 { 431 struct pcb *pcb; 432 433 KASSERT(l != NULL); 434 KASSERT(p != NULL); 435 436 if (!db_validate_address((vaddr_t)l)) { 437 (*pr)("LWP at %p inaccessible\n", l); 438 return; 439 } 440 441 if (!db_validate_address((vaddr_t)p)) { 442 (*pr)("Process at %p inaccessible\n", p); 443 return; 444 } 445 446 pcb = lwp_getpcb(l); 447 if (!db_validate_address((vaddr_t)pcb)) { 448 (*pr)("PCB at %p inaccessible\n", pcb); 449 return; 450 } 451 452 (*pr)("Process %d.%d\n", p->p_pid, l->l_lid); 453 (*pr)("\t PCB contents:\n"); 454 (*pr)(" KSP = 0x%x\n", (unsigned int)(pcb->KSP)); 455 (*pr)(" ESP = 0x%x\n", (unsigned int)(pcb->ESP)); 456 (*pr)(" SSP = 0x%x\n", (unsigned int)(pcb->SSP)); 457 (*pr)(" USP = 0x%x\n", (unsigned int)(pcb->USP)); 458 (*pr)(" R[00] = 0x%08x R[06] = 0x%08x\n", 459 (unsigned int)(pcb->R[0]), (unsigned int)(pcb->R[6])); 460 (*pr)(" R[01] = 0x%08x R[07] = 0x%08x\n", 461 (unsigned int)(pcb->R[1]), (unsigned int)(pcb->R[7])); 462 (*pr)(" R[02] = 0x%08x R[08] = 0x%08x\n", 463 (unsigned int)(pcb->R[2]), (unsigned int)(pcb->R[8])); 464 (*pr)(" R[03] = 0x%08x R[09] = 0x%08x\n", 465 (unsigned int)(pcb->R[3]), (unsigned int)(pcb->R[9])); 466 (*pr)(" R[04] = 0x%08x R[10] = 0x%08x\n", 467 (unsigned int)(pcb->R[4]), (unsigned int)(pcb->R[10])); 468 (*pr)(" R[05] = 0x%08x R[11] = 0x%08x\n", 469 (unsigned int)(pcb->R[5]), (unsigned int)(pcb->R[11])); 470 (*pr)(" AP = 0x%x\n", (unsigned int)(pcb->AP)); 471 (*pr)(" FP = 0x%x\n", (unsigned int)(pcb->FP)); 472 (*pr)(" PC = 0x%x\n", (unsigned int)(pcb->PC)); 473 (*pr)(" PSL = 0x%x\n", (unsigned int)(pcb->PSL)); 474 (*pr)(" Trap frame pointer: 0x%x\n", l->l_md.md_utf); 475 476 db_dump_stack((VAX_CALLFRAME *)(pcb->FP), pr); 477 478 return; 479 } 480 481 /* 482 * Implement the trace command which has the form: 483 * 484 * trace <-- Trace panic (same as before) 485 * trace 0x88888 <-- Trace frame whose address is 888888 486 * trace/t <-- Trace current process (0 if no current proc) 487 * trace/t 0tnn <-- Trace process nn (0t for decimal) 488 */ 489 void 490 db_stack_trace_print( 491 db_expr_t addr, /* Address parameter */ 492 bool have_addr, /* True if addr is valid */ 493 db_expr_t count, /* Optional count */ 494 const char *modif, /* pointer to flag modifier 't' */ 495 void (*pr)(const char *, ...)) /* Print function */ 496 { 497 struct lwp *l = curlwp; 498 struct proc *p = l->l_proc; 499 int trace_proc; 500 int trace_lwp; 501 const char *s; 502 503 /* Check to see if we're tracing a process or lwp */ 504 trace_proc = 0; 505 s = modif; 506 do { 507 switch (*s) { 508 case 't': 509 trace_proc++; 510 break; 511 512 case 'a': 513 trace_lwp++; 514 break; 515 } 516 } while (*s++ && !(trace_proc || trace_lwp)); 517 518 /* 519 * If we're here because of a panic, we'll always have an address of a 520 * frame. 521 * 522 * If the user typed an address, it can be one of the following: 523 * - the address of a struct lwp 524 * - the address of a struct proc 525 * - a PID, hex or decimal 526 * - the address of a frame 527 */ 528 if (have_addr) { 529 if (trace_lwp) { 530 l = (struct lwp *)addr; 531 p = l->l_proc; 532 db_trace_process(l, p, pr); 533 return; 534 } else if (trace_proc) { 535 p = db_proc_find((int)addr); 536 537 /* 538 * Try to be helpful by looking at it as if it were 539 * decimal. 540 */ 541 if (p == NULL) { 542 u_int tpid = 0; 543 u_int foo = addr; 544 545 while (foo != 0) { 546 int digit = (foo >> 28) & 0xf; 547 if (digit > 9) { 548 tpid = -1; 549 break; 550 } 551 tpid = tpid * 10 + digit; 552 foo = foo << 4; 553 } 554 p = db_proc_find(tpid); 555 } 556 557 /* Not a PID. If accessible, assume it's an address. */ 558 if (p == NULL) { 559 if (db_validate_address(addr)) { 560 p = (struct proc *)addr; 561 } else { 562 (*pr)("\t No such process.\n"); 563 return; 564 } 565 } 566 567 db_trace_process(p->p_lwps.lh_first, p, pr); 568 return; 569 } else { 570 db_dump_stack((VAX_CALLFRAME *)addr, pr); 571 return; 572 } 573 } else { 574 /* We got no address. Grab stuff from ddb_regs. */ 575 if (panicstr) 576 db_dump_stack((VAX_CALLFRAME *)ddb_regs.tf_fp, pr); 577 else 578 (*pr)("\t trace what?\n"); 579 } 580 } 581 582 static int ddbescape = 0; 583 584 int 585 kdbrint(int tkn) 586 { 587 if (ddbescape && ((tkn & 0x7f) == 'D')) { 588 setsoftddb(); 589 ddbescape = 0; 590 return 1; 591 } 592 593 if ((ddbescape == 0) && ((tkn & 0x7f) == 27)) { 594 ddbescape = 1; 595 return 1; 596 } 597 598 if (ddbescape) { 599 ddbescape = 0; 600 return 2; 601 } 602 603 ddbescape = 0; 604 return 0; 605 } 606 607 #ifdef MULTIPROCESSOR 608 609 static void 610 db_mach_cpu(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 611 { 612 struct cpu_info *ci; 613 614 if ((addr < 0) || (addr >= cpu_cd.cd_ndevs)) 615 return db_printf("%ld: CPU out of range\n", addr); 616 if ((ci = device_lookup_private(&cpu_cd, addr)) == NULL) 617 return db_printf("%ld: CPU not configured\n", addr); 618 619 if ((ci != curcpu()) && ((ci->ci_flags & CI_STOPPED) == 0)) 620 return db_printf("CPU %ld not stopped???\n", addr); 621 622 memcpy(stopcpu->ci_ddb_regs, &ddb_regs, sizeof(struct trapframe)); 623 stopcpu = ci; 624 memcpy(&ddb_regs, stopcpu->ci_ddb_regs, sizeof(struct trapframe)); 625 db_printf("using CPU %ld", addr); 626 if (ci->ci_curlwp) 627 db_printf(" in proc %d.%d (%s)\n", 628 ci->ci_curlwp->l_proc->p_pid, 629 ci->ci_curlwp->l_lid, 630 ci->ci_curlwp->l_proc->p_comm); 631 } 632 #endif 633 634 const struct db_command db_machine_command_table[] = { 635 #ifdef MULTIPROCESSOR 636 { DDB_ADD_CMD("cpu", db_mach_cpu, 0, 637 "switch to another cpu", "cpu-no", NULL) }, 638 #endif 639 { DDB_END_CMD }, 640 }; 641