1 /* $NetBSD: db_interface.c,v 1.103 2026/11/10 03:43:26 thorpej Exp $ */ 2 3 /* 4 * Mach Operating System 5 * Copyright (c) 1991,1990 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie Mellon 26 * the rights to redistribute these changes. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.103 2026/11/10 03:43:26 thorpej Exp $"); 31 32 #ifdef _KERNEL_OPT 33 #include "opt_multiprocessor.h" 34 #include "opt_cputype.h" /* which mips CPUs do we support? */ 35 #include "opt_ddb.h" 36 #include "opt_kgdb.h" 37 #endif 38 39 #define __PMAP_PRIVATE 40 41 #include <sys/types.h> 42 #include <sys/systm.h> 43 #include <sys/param.h> 44 #include <sys/proc.h> 45 #include <sys/reboot.h> 46 #include <sys/atomic.h> 47 #include <sys/cpu.h> 48 49 #include <uvm/uvm_extern.h> 50 51 #include <mips/regnum.h> 52 #include <mips/cache.h> 53 #include <mips/pcb.h> 54 #include <mips/pte.h> 55 #include <mips/locore.h> 56 #include <mips/mips_opcode.h> 57 #include <dev/cons.h> 58 59 #include <machine/int_fmtio.h> 60 #include <machine/db_machdep.h> 61 #include <ddb/db_access.h> 62 #include <ddb/db_active.h> 63 #include <ddb/db_user.h> 64 #ifndef KGDB 65 #include <ddb/db_command.h> 66 #include <ddb/db_output.h> 67 #include <ddb/db_sym.h> 68 #include <ddb/db_extern.h> 69 #include <ddb/db_interface.h> 70 #include <ddb/db_lex.h> 71 #include <ddb/db_run.h> /* for db_continue_cmd() proto */ 72 #endif 73 74 #define NOCPU ~0 75 volatile u_int ddb_cpu = NOCPU; 76 77 #ifdef _KERNEL 78 db_regs_t ddb_regs; 79 #endif 80 81 #if (MIPS32 + MIPS32R2 + MIPS64 + MIPS64R2) > 0 82 static void db_watch_cmd(db_expr_t, bool, db_expr_t, const char *); 83 static void db_unwatch_cmd(db_expr_t, bool, db_expr_t, const char *); 84 #endif /* (MIPS32 + MIPS32R2 + MIPS64 + MIPS64R2) > 0 */ 85 86 #ifdef MULTIPROCESSOR 87 static void db_mach_cpu_cmd(db_expr_t, bool, db_expr_t, const char *); 88 #endif 89 90 void db_cp0dump_cmd(db_expr_t, bool, db_expr_t, const char *); 91 void db_cpuinfo_cmd(db_expr_t, bool, db_expr_t, const char *); 92 void db_kvtophys_cmd(db_expr_t, bool, db_expr_t, const char *); 93 void db_tlbdump_cmd(db_expr_t, bool, db_expr_t, const char *); 94 95 #ifdef MIPS64_XLS 96 void db_mfcr_cmd(db_expr_t, bool, db_expr_t, const char *); 97 void db_mtcr_cmd(db_expr_t, bool, db_expr_t, const char *); 98 #endif 99 100 paddr_t kvtophys(vaddr_t); 101 102 #ifdef _KERNEL 103 CTASSERT(sizeof(ddb_regs) == sizeof(struct reg)); 104 105 #ifndef KGDB 106 int 107 kdb_trap(int type, struct reg *regs) 108 { 109 int s; 110 111 switch (type) { 112 case T_WATCH: /* watchpoint */ 113 case T_BREAK: /* breakpoint */ 114 printf("kernel: %s trap\n", trap_names[type & 0x1f]); 115 break; 116 case -1: /* keyboard interrupt */ 117 printf("kernel: kdbint trap\n"); 118 break; 119 default: 120 printf("kernel: %s trap\n", trap_names[type & 0x1f]); 121 if (db_recover != 0) { 122 db_error("Faulted in DDB; continuing...\n"); 123 /*NOTREACHED*/ 124 } 125 break; 126 } 127 128 s = splhigh(); 129 130 #if defined(MULTIPROCESSOR) 131 bool first_in_ddb = false; 132 const u_int cpu_me = cpu_number(); 133 const u_int old_ddb_cpu = atomic_cas_uint(&ddb_cpu, NOCPU, cpu_me); 134 if (old_ddb_cpu == NOCPU) { 135 first_in_ddb = true; 136 cpu_pause_others(); 137 } else { 138 if (old_ddb_cpu != cpu_me) { 139 KASSERT(cpu_is_paused(cpu_me)); 140 cpu_pause(regs); 141 splx(s); 142 return 1; 143 } 144 } 145 KASSERT(! cpu_is_paused(cpu_me)); 146 #endif 147 148 ddb_regs = *regs; 149 db_active++; 150 cnpollc(true); 151 db_trap(type & ~T_USER, 0 /*code*/); 152 cnpollc(false); 153 db_active--; 154 *regs = ddb_regs; 155 156 #if defined(MULTIPROCESSOR) 157 if (atomic_cas_uint(&ddb_cpu, cpu_me, NOCPU) == cpu_me) { 158 cpu_resume_others(); 159 } else { 160 cpu_resume(ddb_cpu); 161 if (first_in_ddb) 162 cpu_pause(regs); 163 } 164 #endif 165 166 splx(s); 167 return 1; 168 } 169 170 void 171 cpu_Debugger(void) 172 { 173 174 __asm("break"); 175 } 176 #endif /* !KGDB */ 177 178 /* 179 * Read bytes from kernel address space for debugger. 180 */ 181 void 182 db_read_bytes(vaddr_t addr, size_t size, char *data) 183 { 184 const char *src = (char *)addr; 185 int err; 186 187 /* If asked to fetch from userspace, do it safely */ 188 if (addr < VM_MAXUSER_ADDRESS) { 189 err = copyin(src, data, size); 190 if (err) { 191 #ifdef DDB 192 db_printf("address %p is invalid\n", src); 193 #endif 194 memset(data, 0, size); 195 } 196 return; 197 } 198 199 if (size <= 8 && (size & (size-1)) == 0 && (addr & (size-1)) == 0 200 && ((uintptr_t)data & (size-1)) == 0) { 201 if (size == sizeof(uint8_t)) 202 *(uint8_t *)data = *(const uint8_t *)src; 203 else if (size == sizeof(uint16_t)) 204 *(uint16_t *)data = *(const uint16_t *)src; 205 else if (size == sizeof(uint32_t)) 206 *(uint32_t *)data = *(const uint32_t *)src; 207 else 208 *(uint64_t *)data = *(const uint64_t *)src; 209 return; 210 } 211 while (size--) 212 *data++ = *src++; 213 } 214 215 /* 216 * Write bytes to kernel address space for debugger. 217 */ 218 void 219 db_write_bytes(vaddr_t addr, size_t size, const char *data) 220 { 221 char *p = (char *)addr; 222 size_t n = size; 223 int err; 224 225 /* If asked to store to userspace, do it safely */ 226 if (addr < VM_MAXUSER_ADDRESS) { 227 err = copyout(data, p, size); 228 if (err) { 229 #ifdef DDB 230 db_printf("address %p is invalid\n", p); 231 #endif 232 } 233 return; 234 } 235 236 if (size <= 8 && (size & (size-1)) == 0 && (addr & (size-1)) == 0 237 && ((uintptr_t)data & (size-1)) == 0) { 238 if (size == sizeof(uint8_t)) 239 *(uint8_t *)p = *(const uint8_t *)data; 240 else if (size == sizeof(uint16_t)) 241 *(uint16_t *)p = *(const uint16_t *)data; 242 else if (size == sizeof(uint32_t)) 243 *(uint32_t *)p = *(const uint32_t *)data; 244 else 245 *(uint64_t *)p = *(const uint64_t *)data; 246 return; 247 } 248 while (n--) 249 *p++ = *data++; 250 251 wbflush(); 252 mips_icache_sync_range(addr, size); 253 } 254 255 #ifndef KGDB 256 void 257 db_tlbdump_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 258 const char *modif) 259 { 260 struct tlbmask tlb; 261 bool valid_only = false; 262 263 if (modif[0] == 'v') 264 valid_only = true; 265 266 #ifdef MIPS1 267 if (!MIPS_HAS_R4K_MMU) { 268 int i; 269 270 for (i = 0; i < mips_options.mips_num_tlb_entries; i++) { 271 tlb_read_entry(i, &tlb); 272 if (valid_only && !(tlb.tlb_lo1 & MIPS1_PG_V)) 273 continue; /* skip invalid TLBs */ 274 db_printf("TLB%c%2d Hi 0x%08x Lo 0x%08x", 275 (tlb.tlb_lo1 & MIPS1_PG_V) ? ' ' : '*', 276 i, tlb.tlb_hi, 277 tlb.tlb_lo1 & MIPS1_PG_FRAME); 278 db_printf(" %c%c%c\n", 279 (tlb.tlb_lo1 & MIPS1_PG_D) ? 'D' : ' ', 280 (tlb.tlb_lo1 & MIPS1_PG_G) ? 'G' : ' ', 281 (tlb.tlb_lo1 & MIPS1_PG_N) ? 'N' : ' '); 282 } 283 } 284 #endif 285 #ifdef MIPS3_PLUS 286 if (MIPS_HAS_R4K_MMU) { 287 int i; 288 const int tlb_count_width = 289 mips_options.mips_num_tlb_entries > 100 ? 3 : 2; 290 291 for (i = 0; i < mips_options.mips_num_tlb_entries; i++) { 292 tlb_read_entry(i, &tlb); 293 if (valid_only && 294 !((tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V)) 295 continue; /* skip invalid TLBs */ 296 297 db_printf("TLB%c%*d Hi 0x%08"PRIxVADDR" ", 298 (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*', 299 tlb_count_width, i, tlb.tlb_hi); 300 db_printf("Lo0=0x%09" PRIx64 " %c%c attr %x ", 301 (uint64_t)mips_tlbpfn_to_paddr(tlb.tlb_lo0), 302 (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ', 303 (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ', 304 (int)(tlb.tlb_lo0 >> 3) & 7); 305 db_printf("Lo1=0x%09" PRIx64 " %c%c attr %x sz=%x\n", 306 (uint64_t)mips_tlbpfn_to_paddr(tlb.tlb_lo1), 307 (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ', 308 (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ', 309 (int)(tlb.tlb_lo1 >> 3) & 7, 310 tlb.tlb_mask); 311 } 312 } 313 #endif 314 } 315 316 void 317 db_kvtophys_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 318 const char *modif) 319 { 320 321 if (!have_addr) 322 return; 323 if (VM_MIN_KERNEL_ADDRESS <= addr && addr < VM_MAX_KERNEL_ADDRESS) { 324 /* 325 * Cast the physical address -- some platforms, while 326 * being ILP32, may be using 64-bit paddr_t's. 327 */ 328 db_printf("0x%" DDB_EXPR_FMT "x -> 0x%" PRIx64 "\n", addr, 329 (uint64_t) kvtophys(addr)); 330 } else 331 db_printf("not a kernel virtual address\n"); 332 } 333 334 #define FLDWIDTH 10 335 336 #define SHOW32(reg, name) SHOW32SELECT(reg, 0, name) 337 #define SHOW64(reg, name) SHOW64SELECT(reg, 0, name) 338 #define SHOW32SEL(reg, name) SHOW32SELECT(reg, name) 339 #define SHOW64SEL(reg, name) SHOW64SELECT(reg, name) 340 341 #define SHOW32SELECT(num, sel, name) \ 342 do { \ 343 uint32_t __val; \ 344 \ 345 __asm volatile( \ 346 ".set push \n\t" \ 347 ".set mips32 \n\t" \ 348 "mfc0 %0,$%1,%2 \n\t" \ 349 ".set pop \n\t" \ 350 : "=r"(__val) : "n"(num), "n"(sel)); \ 351 db_printf(" %s:%*s %#x\n", name, \ 352 FLDWIDTH - (int) strlen(name), "", __val); \ 353 } while (0) 354 355 /* XXX not 64-bit ABI safe! */ 356 #define SHOW64SELECT(num, sel, name) \ 357 do { \ 358 uint64_t __val; \ 359 \ 360 KASSERT (CPUIS64BITS); \ 361 __asm volatile( \ 362 ".set push \n\t" \ 363 ".set mips64 \n\t" \ 364 ".set noat \n\t" \ 365 "dmfc0 %0,$%1,%2 \n\t" \ 366 ".set pop" \ 367 : "=r"(__val) : "n"(num), "n"(sel)); \ 368 db_printf(" %s:%*s %#"PRIx64"\n", name, \ 369 FLDWIDTH - (int) strlen(name), "", __val); \ 370 } while (0) 371 372 #define SET32(reg, name, val) \ 373 do { \ 374 \ 375 __asm volatile("mtc0 %0,$" ___STRING(reg) :: "r"(val)); \ 376 if (name != NULL) \ 377 db_printf(" %s =%*s %#x\n", name, \ 378 FLDWIDTH - (int) strlen(name), "", val); \ 379 } while (0) 380 381 #define SET64(reg, name) MIPS64_SET64(reg, 0, name) 382 383 #define MIPS64_SET32(num, sel, name, val) \ 384 do { \ 385 \ 386 __asm volatile( \ 387 ".set push \n\t" \ 388 ".set mips32 \n\t" \ 389 "mtc0 %0,$%1,%2 \n\t" \ 390 ".set pop \n\t" \ 391 :: "r"(val), "n"(num), "n"(sel)); \ 392 if (name != NULL) \ 393 db_printf(" %s =%*s %#x\n", name, \ 394 FLDWIDTH - (int) strlen(name), "", val); \ 395 } while (0) 396 397 /* XXX not 64-bit ABI safe! */ 398 #define MIPS64_SET64(num, sel, name, val) \ 399 do { \ 400 \ 401 KASSERT (CPUIS64BITS); \ 402 __asm volatile( \ 403 ".set push \n\t" \ 404 ".set mips64 \n\t" \ 405 ".set noat \n\t" \ 406 "dmtc0 %0,$%1,%2 \n\t" \ 407 ".set pop" \ 408 :: "r"(val), "n"(num), "n"(sel)); \ 409 if (name != NULL) \ 410 db_printf(" %s =%*s %#"PRIx64"\n", name, \ 411 FLDWIDTH - (int) strlen(name), "", (uint64_t)val); \ 412 } while (0) 413 414 void 415 db_cp0dump_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 416 const char *modif) 417 { 418 u_int cp0flags = mips_options.mips_cpu->cpu_cp0flags; 419 420 SHOW32(MIPS_COP_0_TLB_INDEX, "index"); 421 SHOW32(MIPS_COP_0_TLB_RANDOM, "random"); 422 423 if (!MIPS_HAS_R4K_MMU) { 424 SHOW32(MIPS_COP_0_TLB_LOW, "entrylow"); 425 } else { 426 if (CPUIS64BITS) { 427 SHOW64(MIPS_COP_0_TLB_LO0, "entrylo0"); 428 SHOW64(MIPS_COP_0_TLB_LO1, "entrylo1"); 429 } else { 430 SHOW32(MIPS_COP_0_TLB_LO0, "entrylo0"); 431 SHOW32(MIPS_COP_0_TLB_LO1, "entrylo1"); 432 } 433 } 434 435 if (CPUIS64BITS) { 436 SHOW64(MIPS_COP_0_TLB_CONTEXT, "context"); 437 } else { 438 SHOW32(MIPS_COP_0_TLB_CONTEXT, "context"); 439 } 440 441 if (MIPS_HAS_R4K_MMU) { 442 SHOW32(MIPS_COP_0_TLB_PG_MASK, "pagemask"); 443 SHOW32(MIPS_COP_0_TLB_WIRED, "wired"); 444 } 445 446 if (CPUIS64BITS) { 447 SHOW64(MIPS_COP_0_BAD_VADDR, "badvaddr"); 448 } else { 449 SHOW32(MIPS_COP_0_BAD_VADDR, "badvaddr"); 450 } 451 452 if (mips_options.mips_cpu_arch >= CPU_ARCH_MIPS3) { 453 SHOW32(MIPS_COP_0_COUNT, "count"); 454 } 455 456 if ((cp0flags & MIPS_CP0FL_USE) != 0) { 457 if ((cp0flags & MIPS_CP0FL_EIRR) != 0) 458 SHOW64SEL(MIPS_COP_0_EIRR, "eirr"); 459 if ((cp0flags & MIPS_CP0FL_EIMR) != 0) 460 SHOW64SEL(MIPS_COP_0_EIMR, "eimr"); 461 } 462 463 if (CPUIS64BITS) { 464 SHOW64(MIPS_COP_0_TLB_HI, "entryhi"); 465 } else { 466 SHOW32(MIPS_COP_0_TLB_HI, "entryhi"); 467 } 468 469 if (mips_options.mips_cpu_arch >= CPU_ARCH_MIPS3) { 470 SHOW32(MIPS_COP_0_COMPARE, "compare"); 471 } 472 473 SHOW32(MIPS_COP_0_STATUS, "status"); 474 SHOW32(MIPS_COP_0_CAUSE, "cause"); 475 476 if (CPUIS64BITS) { 477 SHOW64(MIPS_COP_0_EXC_PC, "epc"); 478 } else { 479 SHOW32(MIPS_COP_0_EXC_PC, "epc"); 480 } 481 482 SHOW32(MIPS_COP_0_PRID, "prid"); 483 484 if ((cp0flags & MIPS_CP0FL_USE) != 0) { 485 if ((cp0flags & MIPS_CP0FL_EBASE) != 0) 486 SHOW32SEL(MIPS_COP_0_EBASE, "ebase"); 487 if ((cp0flags & MIPS_CP0FL_CONFIG) != 0) 488 SHOW32(MIPS_COP_0_CONFIG, "config"); 489 if ((cp0flags & MIPS_CP0FL_CONFIG1) != 0) 490 SHOW32SEL(MIPS_COP_0_CONFIG1, "config1"); 491 if ((cp0flags & MIPS_CP0FL_CONFIG2) != 0) 492 SHOW32SEL(MIPS_COP_0_CONFIG2, "config2"); 493 if ((cp0flags & MIPS_CP0FL_CONFIG3) != 0) 494 SHOW32SEL(MIPS_COP_0_CONFIG3, "config3"); 495 if ((cp0flags & MIPS_CP0FL_CONFIG4) != 0) 496 SHOW32SEL(MIPS_COP_0_CONFIG4, "config4"); 497 if ((cp0flags & MIPS_CP0FL_CONFIG5) != 0) 498 SHOW32SEL(MIPS_COP_0_CONFIG5, "config5"); 499 if ((cp0flags & MIPS_CP0FL_CONFIG6) != 0) 500 SHOW32SEL(MIPS_COP_0_CONFIG6, "config6"); 501 if ((cp0flags & MIPS_CP0FL_CONFIG7) != 0) 502 SHOW32SEL(MIPS_COP_0_CONFIG7, "config7"); 503 if (CPUISMIPSNNR2) 504 SHOW32(MIPS_COP_0_HWRENA, "hwrena"); 505 if (MIPS_HAS_USERLOCAL) 506 SHOW32SEL(MIPS_COP_0_USERLOCAL, "userlocal"); 507 } else { 508 SHOW32(MIPS_COP_0_CONFIG, "config"); 509 #if (MIPS32 + MIPS32R2 + MIPS64 + MIPS64R2) > 0 510 if (CPUISMIPSNN) { 511 uint32_t val; 512 513 val = mipsNN_cp0_config1_read(); 514 db_printf(" config1: %#x\n", val); 515 } 516 #endif 517 } 518 519 if (MIPS_HAS_LLSC) { 520 if (MIPS_HAS_LLADDR) { 521 if (CPUIS64BITS) 522 SHOW64(MIPS_COP_0_LLADDR, "lladdr"); 523 else 524 SHOW32(MIPS_COP_0_LLADDR, "lladdr"); 525 } 526 } 527 528 #if (MIPS32 + MIPS32R2 + MIPS64 + MIPS64R2) > 0 529 for (int i=0; i < curcpu()->ci_cpuwatch_count; i++) { 530 const intptr_t lo = mipsNN_cp0_watchlo_read(i); 531 const uint32_t hi = mipsNN_cp0_watchhi_read(i); 532 db_printf(" %s%d:%*s %#" PRIxPTR "\t", 533 "watchlo", i, FLDWIDTH - 8, "", lo); 534 db_printf(" %s%d:%*s %#" PRIx32 "\n", 535 "watchhi", i, FLDWIDTH - 8, "", hi); 536 } 537 #endif 538 539 if (CPUIS64BITS) { 540 SHOW64(MIPS_COP_0_TLB_XCONTEXT, "xcontext"); 541 } 542 543 if (CPUISMIPSNN) { 544 if (CPUIS64BITS) { 545 SHOW64(MIPS_COP_0_PERFCNT0_CTL, "perfcnt0ctl"); 546 SHOW64SEL(MIPS_COP_0_PERFCNT0_CNT, "perfcnt0cnt"); 547 } else { 548 SHOW32(MIPS_COP_0_PERFCNT0_CTL, "perfcnt0ctl"); 549 SHOW32SEL(MIPS_COP_0_PERFCNT0_CNT, "perfcnt0cnt"); 550 } 551 } 552 553 if (((cp0flags & MIPS_CP0FL_USE) == 0) || 554 ((cp0flags & MIPS_CP0FL_ECC) != 0)) 555 SHOW32(MIPS_COP_0_ECC, "ecc"); 556 557 if (((cp0flags & MIPS_CP0FL_USE) == 0) || 558 ((cp0flags & MIPS_CP0FL_CACHE_ERR) != 0)) 559 SHOW32(MIPS_COP_0_CACHE_ERR, "cacherr"); 560 561 SHOW32(MIPS_COP_0_TAG_LO, "cachelo"); 562 SHOW32(MIPS_COP_0_TAG_HI, "cachehi"); 563 564 if (CPUIS64BITS) { 565 SHOW64(MIPS_COP_0_ERROR_PC, "errorpc"); 566 } else { 567 SHOW32(MIPS_COP_0_ERROR_PC, "errorpc"); 568 } 569 } 570 571 572 static void 573 show_cpuinfo(struct cpu_info *kci) 574 { 575 struct cpu_info cpuinfobuf; 576 cpuid_t cpuid; 577 int i; 578 579 db_read_bytes((db_addr_t)kci, sizeof(cpuinfobuf), (char *)&cpuinfobuf); 580 581 struct cpu_info *ci = &cpuinfobuf; 582 cpuid = ci->ci_cpuid; 583 db_printf("cpu_info=%p, cpu_name=%s\n", kci, ci->ci_cpuname); 584 db_printf("%p cpu[%lu].ci_cpuid = %lu\n", 585 &ci->ci_cpuid, cpuid, ci->ci_cpuid); 586 db_printf("%p cpu[%lu].ci_curlwp = %p\n", 587 &ci->ci_curlwp, cpuid, ci->ci_curlwp); 588 for (i = 0; i < SOFTINT_COUNT; i++) { 589 db_printf("%p cpu[%lu].ci_softlwps[%d] = %p\n", 590 &ci->ci_softlwps[i], cpuid, i, ci->ci_softlwps[i]); 591 } 592 db_printf("%p cpu[%lu].ci_want_resched = %d\n", 593 &ci->ci_want_resched, cpuid, ci->ci_want_resched); 594 db_printf("%p cpu[%lu].ci_cpl = %d\n", 595 &ci->ci_cpl, cpuid, ci->ci_cpl); 596 db_printf("%p cpu[%lu].ci_softints = 0x%08x\n", 597 &ci->ci_softints, cpuid, ci->ci_softints); 598 db_printf("%p cpu[%lu].ci_idepth = %u\n", 599 &ci->ci_idepth, cpuid, ci->ci_idepth); 600 } 601 602 void 603 db_cpuinfo_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 604 const char *modif) 605 { 606 #ifdef MULTIPROCESSOR 607 CPU_INFO_ITERATOR cii; 608 struct cpu_info *ci; 609 bool showall = false; 610 611 if (modif != NULL) { 612 for (; *modif != '\0'; modif++) { 613 switch (*modif) { 614 case 'a': 615 showall = true; 616 break; 617 } 618 } 619 } 620 621 if (showall) { 622 for (CPU_INFO_FOREACH(cii, ci)) { 623 show_cpuinfo(ci); 624 } 625 } else 626 #endif /* MULTIPROCESSOR */ 627 show_cpuinfo(curcpu()); 628 } 629 630 631 #if (MIPS32 + MIPS32R2 + MIPS64 + MIPS64R2) > 0 632 static void 633 db_watch_cmd(db_expr_t address, bool have_addr, db_expr_t count, 634 const char *modif) 635 { 636 struct cpu_info * const ci = curcpu(); 637 cpu_watchpoint_t *cwp; 638 register_t mask=0; 639 uint32_t asid; 640 uint32_t mode; 641 db_expr_t value; 642 char str[6]; 643 644 if (!have_addr) { 645 db_printf("%-3s %-5s %-16s %4s %4s\n", 646 "#", "MODE", "ADDR", "MASK", "ASID"); 647 for (u_int i=0; i < ci->ci_cpuwatch_count; i++) { 648 cwp = &ci->ci_cpuwatch_tab[i]; 649 mode = cwp->cw_mode; 650 if ((mode & CPUWATCH_RWX) == 0) 651 continue; /* empty/disabled/invalid */ 652 str[0] = (mode & CPUWATCH_READ) ? 'r' : '-'; 653 str[1] = (mode & CPUWATCH_WRITE) ? 'w' : '-'; 654 str[2] = (mode & CPUWATCH_EXEC) ? 'x' : '-'; 655 str[3] = (mode & CPUWATCH_MASK) ? 'm' : '-'; 656 str[4] = (mode & CPUWATCH_ASID) ? 'a' : 'g'; 657 str[5] = '\0'; 658 db_printf("%2u: %s %16" PRIxREGISTER 659 " %4" PRIxREGISTER " %4x\n", 660 i, str, cwp->cw_addr, cwp->cw_mask, cwp->cw_asid); 661 } 662 db_flush_lex(); 663 return; 664 } 665 666 cwp = cpuwatch_alloc(); 667 if (cwp == NULL) { 668 db_printf("no watchpoint available\n"); 669 db_flush_lex(); 670 return; 671 } 672 673 /* 674 * parse modif to define mode 675 */ 676 KASSERT(modif != NULL); 677 mode = 0; 678 for (int i=0; modif[i] != '\0'; i++) { 679 switch(modif[i]) { 680 case 'w': 681 mode |= CPUWATCH_WRITE; 682 break; 683 case 'm': 684 mode |= CPUWATCH_MASK; 685 break; 686 case 'r': 687 mode |= CPUWATCH_READ; 688 break; 689 case 'x': 690 mode |= CPUWATCH_EXEC; 691 break; 692 case 'a': 693 mode |= CPUWATCH_ASID; 694 break; 695 } 696 } 697 if (mode == 0) { 698 db_printf("mode modifier(s) missing\n"); 699 db_flush_lex(); 700 return; 701 } 702 703 /* 704 * if mask mode is requested get the mask, 705 */ 706 if (mode & CPUWATCH_MASK) { 707 if (! db_expression(&value)) { 708 db_printf("mask missing\n"); 709 db_flush_lex(); 710 return; 711 } 712 mask = (register_t)(value & __BITS(11, 3)); 713 } 714 715 /* 716 * if asid mode is requested, get the asid; 717 * otherwise use global mode (and set asid=0) 718 */ 719 if (mode & CPUWATCH_ASID) { 720 if (! db_expression(&value)) { 721 db_printf("asid missing\n"); 722 db_flush_lex(); 723 return; 724 } 725 asid = (uint32_t)(value & __BITS(7,0)); 726 } else { 727 asid = 0; 728 } 729 730 if (mode & (CPUWATCH_MASK|CPUWATCH_ASID)) 731 db_skip_to_eol(); 732 else 733 db_flush_lex(); 734 735 /* 736 * store to the (volatile) table entry 737 * other CPUs can see this and load when resuming from pause 738 */ 739 cwp->cw_addr = (register_t)address; 740 cwp->cw_mask = (register_t)mask; 741 cwp->cw_asid = asid; 742 cwp->cw_mode = mode; 743 744 /* 745 * program the CPU watchpoint regs 746 */ 747 cpuwatch_set(cwp); 748 } 749 750 static void 751 db_unwatch_cmd(db_expr_t address, bool have_addr, db_expr_t count, 752 const char *modif) 753 { 754 struct cpu_info * const ci = curcpu(); 755 const bool unwatch_all = !have_addr; 756 int n; 757 758 n = 0; 759 for (u_int i=0; i < ci->ci_cpuwatch_count; i++) { 760 cpu_watchpoint_t * const cwp = &ci->ci_cpuwatch_tab[i]; 761 if (unwatch_all || (cwp->cw_addr == (register_t)address)) { 762 cpuwatch_free(cwp); 763 n++; 764 } 765 } 766 if (n == 0) 767 db_printf("no watch found on address %#" PRIxREGISTER "\n", 768 (register_t)address); 769 } 770 #endif /* (MIPS32 + MIPS32R2 + MIPS64 + MIPS64R2) > 0 */ 771 772 #ifdef MIPS64_XLS 773 void 774 db_mfcr_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 775 const char *modif) 776 { 777 uint64_t value; 778 779 if ((mips_options.mips_cpu->cpu_flags & CPU_MIPS_HAVE_MxCR) == 0) { 780 db_printf("mfcr not implemented on this CPU\n"); 781 return; 782 } 783 784 if (!have_addr) { 785 db_printf("Address missing\n"); 786 return; 787 } 788 789 /* value = CR[addr] */ 790 __asm volatile( \ 791 ".set push \n\t" \ 792 ".set arch=xlr \n\t" \ 793 ".set noat \n\t" \ 794 "mfcr %0,%1 \n\t" \ 795 ".set pop \n\t" \ 796 : "=r"(value) : "r"(addr)); 797 798 db_printf("control reg 0x%" DDB_EXPR_FMT "x = 0x%" PRIx64 "\n", 799 addr, value); 800 } 801 802 void 803 db_mtcr_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 804 const char *modif) 805 { 806 db_expr_t value; 807 808 if ((mips_options.mips_cpu->cpu_flags & CPU_MIPS_HAVE_MxCR) == 0) { 809 db_printf("mtcr not implemented on this CPU\n"); 810 return; 811 } 812 813 if ((!have_addr) || (! db_expression(&value))) { 814 db_printf("Address missing\n"); 815 db_flush_lex(); 816 return; 817 } 818 db_skip_to_eol(); 819 820 /* CR[addr] = value */ 821 __asm volatile( \ 822 ".set push \n\t" \ 823 ".set arch=xlr \n\t" \ 824 ".set noat \n\t" \ 825 "mtcr %0,%1 \n\t" \ 826 ".set pop \n\t" \ 827 :: "r"(value), "r"(addr)); 828 829 db_printf("control reg 0x%" DDB_EXPR_FMT "x = 0x%" DDB_EXPR_FMT "x\n", 830 addr, value); 831 } 832 #endif /* MIPS64_XLS */ 833 834 #ifdef MIPS64_OCTEON 835 #include <mips/cavium/dev/octeon_ciureg.h> 836 837 #ifdef MULTIPROCESSOR 838 static void 839 db_mach_nmi_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 840 const char *modif) 841 { 842 CPU_INFO_ITERATOR cii; 843 struct cpu_info *ci; 844 845 if (!have_addr) { 846 db_printf("CPU not specific\n"); 847 return; 848 } 849 for (CPU_INFO_FOREACH(cii, ci)) { 850 if (cpu_index(ci) == addr) 851 break; 852 } 853 if (ci == NULL) { 854 db_printf("CPU %ld not configured\n", (long)addr); 855 return; 856 } 857 if (ci == curcpu()) { 858 db_printf("CPU %ld is current cpu; request ignored\n", 859 (long)addr); 860 return; 861 } 862 mips3_sd(MIPS_PHYS_TO_XKPHYS_UNCACHED(CIU_NMI), __BIT(ci->ci_cpuid)); 863 } 864 #endif 865 #endif 866 867 static void 868 db_mach_reset_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 869 const char *modif) 870 { 871 872 if (cpu_reset_address == NULL) { 873 db_printf("cpu_reset_address is not set\n"); 874 return; 875 } 876 877 cpu_reset_address(); 878 } 879 880 const struct db_command db_machine_command_table[] = { 881 #ifdef MULTIPROCESSOR 882 { DDB_ADD_CMD("cpu", db_mach_cpu_cmd, 0, 883 "switch to another cpu", "cpu#", NULL) }, 884 #endif 885 { DDB_ADD_CMD("cp0", db_cp0dump_cmd, 0, 886 "Dump CP0 registers.", 887 NULL, NULL) }, 888 { DDB_ADD_CMD("cpuinfo", db_cpuinfo_cmd, 0, 889 "Displays the cpuinfo", 890 NULL, NULL) 891 }, 892 { DDB_ADD_CMD("kvtop", db_kvtophys_cmd, 0, 893 "Print the physical address for a given kernel virtual address", 894 "address", 895 " address:\tvirtual address to look up") }, 896 #ifdef MIPS64_XLS 897 { DDB_ADD_CMD("mfcr", db_mfcr_cmd, CS_NOREPEAT, 898 "Dump processor control register", 899 NULL, NULL) }, 900 { DDB_ADD_CMD("mtcr", db_mtcr_cmd, CS_NOREPEAT|CS_MORE, 901 "Set processor control register", 902 NULL, NULL) }, 903 #endif 904 #if defined(MIPS64_OCTEON) && defined(MULTIPROCESSOR) 905 { DDB_ADD_CMD("nmi", db_mach_nmi_cmd, CS_NOREPEAT, 906 "Send NMI to processor", 907 "cpu#", NULL) }, 908 #endif /* OCTEON + MP */ 909 { DDB_ADD_CMD("reset", db_mach_reset_cmd, CS_NOREPEAT, 910 "Initiate hardware reset", 911 NULL, NULL) }, 912 { DDB_ADD_CMD("tlb", db_tlbdump_cmd, 0, 913 "Print out TLB entries. (only works with options DEBUG)", 914 NULL, NULL) }, 915 #if (MIPS32 + MIPS32R2 + MIPS64 + MIPS64R2) > 0 916 { DDB_ADD_CMD("watch", db_watch_cmd, CS_MORE, 917 "set cp0 watchpoint", 918 "address <mask> <asid> </rwxma>", NULL) }, 919 { DDB_ADD_CMD("unwatch",db_unwatch_cmd, 0, 920 "delete cp0 watchpoint", 921 "address", NULL) }, 922 #endif /* (MIPS32 + MIPS32R2 + MIPS64 + MIPS64R2) > 0 */ 923 { DDB_END_CMD }, 924 }; 925 #endif /* !KGDB */ 926 927 /* 928 * Determine whether the instruction involves a delay slot. 929 */ 930 bool 931 inst_branch(int inst) 932 { 933 InstFmt i; 934 int delslt; 935 936 i.word = inst; 937 delslt = 0; 938 switch (i.JType.op) { 939 case OP_REGIMM: 940 case OP_J: 941 case OP_JAL: 942 #if MIPS64_OCTEON 943 case OP_CVM_BBIT0: 944 case OP_CVM_BBIT032: 945 case OP_CVM_BBIT1: 946 case OP_CVM_BBIT132: 947 #endif 948 case OP_BEQ: 949 case OP_BNE: 950 case OP_BLEZ: 951 case OP_BGTZ: 952 case OP_BEQL: 953 case OP_BNEL: 954 case OP_BLEZL: 955 case OP_BGTZL: 956 delslt = 1; 957 break; 958 959 case OP_COP0: 960 case OP_COP1: 961 switch (i.RType.rs) { 962 case OP_BCx: 963 case OP_BCy: 964 delslt = 1; 965 } 966 break; 967 968 case OP_SPECIAL: 969 if (i.RType.op == OP_JR || i.RType.op == OP_JALR) 970 delslt = 1; 971 break; 972 } 973 return delslt; 974 } 975 976 /* 977 * Determine whether the instruction calls a function. 978 */ 979 bool 980 inst_call(int inst) 981 { 982 bool call; 983 InstFmt i; 984 985 i.word = inst; 986 if (i.JType.op == OP_SPECIAL 987 && ((i.RType.func == OP_JR && i.RType.rs != 31) || 988 i.RType.func == OP_JALR)) 989 call = 1; 990 else if (i.JType.op == OP_JAL) 991 call = 1; 992 else 993 call = 0; 994 return call; 995 } 996 997 /* 998 * Determine whether the instruction returns from a function (j ra). The 999 * compiler can use this construct for other jumps, but usually will not. 1000 * This lets the ddb "next" command to work (also need inst_trap_return()). 1001 */ 1002 bool 1003 inst_return(int inst) 1004 { 1005 InstFmt i; 1006 1007 i.word = inst; 1008 1009 return (i.JType.op == OP_SPECIAL && i.RType.func == OP_JR && 1010 i.RType.rs == 31); 1011 } 1012 1013 /* 1014 * Determine whether the instruction makes a jump. 1015 */ 1016 bool 1017 inst_unconditional_flow_transfer(int inst) 1018 { 1019 InstFmt i; 1020 bool jump; 1021 1022 i.word = inst; 1023 jump = (i.JType.op == OP_J) || 1024 (i.JType.op == OP_SPECIAL && i.RType.func == OP_JR); 1025 return jump; 1026 } 1027 1028 /* 1029 * Determine whether the instruction is a load/store as appropriate. 1030 */ 1031 bool 1032 inst_load(int inst) 1033 { 1034 InstFmt i = { .word = inst, }; 1035 1036 /* 1037 * All loads are opcodes 04x or 06x. 1038 */ 1039 if ((i.JType.op & 050) != 040) 1040 return false; 1041 1042 /* 1043 * Except these this opcode is not a load. 1044 */ 1045 return i.JType.op != OP_PREF; 1046 } 1047 1048 bool 1049 inst_store(int inst) 1050 { 1051 InstFmt i = { .word = inst, }; 1052 1053 /* 1054 * All stores are opcodes 05x or 07x. 1055 */ 1056 if ((i.JType.op & 050) != 050) 1057 return false; 1058 1059 /* 1060 * Except these two opcodes are not stores. 1061 */ 1062 return i.JType.op != OP_RSVD073 && i.JType.op != OP_CACHE; 1063 } 1064 1065 /* 1066 * Return the next pc if the given branch is taken. 1067 * mips_emul_branch() runs analysis for branch delay slot. 1068 */ 1069 db_addr_t 1070 branch_taken(int inst, db_addr_t pc, db_regs_t *regs) 1071 { 1072 struct pcb * const pcb = lwp_getpcb(curlwp); 1073 const uint32_t fpucsr = PCB_FSR(pcb); 1074 vaddr_t ra; 1075 1076 ra = mips_emul_branch((struct trapframe *)regs, pc, fpucsr, false); 1077 return ra; 1078 } 1079 1080 /* 1081 * Return the next pc of an arbitrary instruction. 1082 */ 1083 db_addr_t 1084 next_instr_address(db_addr_t pc, bool bd) 1085 { 1086 uint32_t ins; 1087 1088 if (bd == false) 1089 return (pc + 4); 1090 1091 if (pc < MIPS_KSEG0_START) 1092 ins = mips_ufetch32((void *)pc); 1093 else 1094 ins = *(uint32_t *)pc; 1095 1096 if (inst_branch(ins) || inst_call(ins) || inst_return(ins)) 1097 return (pc + 4); 1098 1099 return (pc); 1100 } 1101 1102 #ifdef MULTIPROCESSOR 1103 1104 bool 1105 ddb_running_on_this_cpu_p(void) 1106 { 1107 return ddb_cpu == cpu_number(); 1108 } 1109 1110 bool 1111 ddb_running_on_any_cpu_p(void) 1112 { 1113 return ddb_cpu != NOCPU; 1114 } 1115 1116 void 1117 db_resume_others(void) 1118 { 1119 u_int cpu_me = cpu_number(); 1120 1121 if (atomic_cas_uint(&ddb_cpu, cpu_me, NOCPU) == cpu_me) 1122 cpu_resume_others(); 1123 } 1124 1125 static void 1126 db_mach_cpu_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 1127 { 1128 CPU_INFO_ITERATOR cii; 1129 struct cpu_info *ci; 1130 1131 if (!have_addr) { 1132 cpu_debug_dump(); 1133 return; 1134 } 1135 for (CPU_INFO_FOREACH(cii, ci)) { 1136 if (cpu_index(ci) == addr) 1137 break; 1138 } 1139 if (ci == NULL) { 1140 db_printf("CPU %ld not configured\n", (long)addr); 1141 return; 1142 } 1143 if (ci != curcpu()) { 1144 if (!cpu_is_paused(cpu_index(ci))) { 1145 db_printf("CPU %ld not paused\n", (long)addr); 1146 return; 1147 } 1148 (void)atomic_cas_uint(&ddb_cpu, cpu_number(), cpu_index(ci)); 1149 db_continue_cmd(0, false, 0, ""); 1150 } 1151 } 1152 #endif /* MULTIPROCESSOR */ 1153 1154 #endif /* _KERNEL */ 1155