1 /* $NetBSD: machdep.c,v 1.156 2026/11/15 15:16:25 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: Utah Hdr: machdep.c 1.74 92/12/20 37 * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.156 2026/11/15 15:16:25 thorpej Exp $"); 42 43 #include "opt_ddb.h" 44 #include "opt_kgdb.h" 45 #include "opt_modular.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/proc.h> 51 #include <sys/buf.h> 52 #include <sys/reboot.h> 53 #include <sys/conf.h> 54 #include <sys/file.h> 55 #include <sys/device.h> 56 #include <sys/mbuf.h> 57 #include <sys/msgbuf.h> 58 #include <sys/ioctl.h> 59 #include <sys/tty.h> 60 #include <sys/mount.h> 61 #include <sys/exec.h> 62 #include <sys/core.h> 63 #include <sys/kcore.h> 64 #include <sys/vnode.h> 65 #include <sys/syscallargs.h> 66 #include <sys/ksyms.h> 67 #include <sys/module.h> 68 #include <sys/cpu.h> 69 #ifdef KGDB 70 #include <sys/kgdb.h> 71 #endif 72 73 #include <uvm/uvm_extern.h> 74 75 #include <sys/sysctl.h> 76 77 #include <dev/cons.h> 78 #include <dev/mm.h> 79 80 #include <machine/cpu.h> 81 #include <machine/dvma.h> 82 #include <machine/idprom.h> 83 #include <machine/kcore.h> 84 #include <machine/reg.h> 85 #include <machine/pcb.h> 86 #include <machine/psl.h> 87 #include <machine/pte.h> 88 #include <machine/mon.h> 89 90 #if defined(DDB) 91 #include <machine/db_machdep.h> 92 #include <ddb/db_sym.h> 93 #include <ddb/db_extern.h> 94 #endif 95 96 #include <sun3/sun3/machdep.h> 97 98 #include "ksyms.h" 99 100 /* Defined in locore.s */ 101 extern char kernel_text[]; 102 /* Defined by the linker */ 103 extern char etext[]; 104 105 /* kernel_arch specific values required by module(9) */ 106 const vaddr_t kernbase = KERNBASE3X; 107 const vaddr_t kern_end = KERN_END3X; 108 109 extern void *msgbufaddr; 110 111 u_char cpu_machine_id = 0; 112 const char *cpu_string = NULL; 113 int cpu_has_vme = 0; 114 int has_iocache = 0; 115 116 vaddr_t dumppage; 117 118 int delay_divisor = delay_divisor_est(33); /* assume fastest 33Hz */ 119 char kernel_arch[16] = "sun3x"; /* XXX needs a sysctl node */ 120 121 static void initcpu(void); 122 123 /* 124 * Console initialization: called early on from main, 125 * before vm init or cpu_startup. This system is able 126 * to use the console for output immediately (via PROM) 127 * but can not use it for input until after this point. 128 */ 129 void 130 consinit(void) 131 { 132 133 /* 134 * Switch from the PROM console (output only) 135 * to our own console driver. 136 */ 137 cninit(); 138 139 #if NKSYMS || defined(DDB) || defined(MODULAR) 140 { 141 extern int nsym; 142 extern char *ssym, *esym; 143 144 ksyms_addsyms_elf(nsym, ssym, esym); 145 } 146 #endif /* DDB */ 147 148 /* 149 * Now that the console can do input as well as 150 * output, consider stopping for a debugger. 151 */ 152 if (boothowto & RB_KDB) { 153 #ifdef KGDB 154 /* XXX - Ask on console for kgdb_dev? */ 155 /* Note: this will just return if kgdb_dev==NODEV */ 156 kgdb_connect(1); 157 #else /* KGDB */ 158 /* Either DDB or no debugger (just PROM). */ 159 cpu_Debugger(); 160 #endif /* KGDB */ 161 } 162 } 163 164 /* 165 * cpu_startup: allocate memory for variable-sized tables, 166 * initialize CPU, and do autoconfiguration. 167 * 168 * This is called early in init_main.c:main(), after the 169 * kernel memory allocator is ready for use, but before 170 * the creation of processes 1,2, and mountroot, etc. 171 */ 172 void 173 cpu_startup(void) 174 { 175 /* Set the FPU bit in the "system enable register" */ 176 enable_fpu(1); 177 178 /* 179 * Get scratch page for dumpsys(). 180 */ 181 dumppage = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, UVM_KMF_WIRED); 182 if (dumppage == 0) 183 panic("startup: alloc dumppage"); 184 185 cpu_startup_common(); 186 187 /* 188 * Allocate a virtual page (for use by /dev/mem) 189 * This page is handed to pmap_enter() therefore 190 * it has to be in the normal kernel VA range. 191 */ 192 cpu_info_primary.ci_tmpva_vmmap = 193 uvm_km_alloc(kernel_map, PAGE_SIZE, 0, 194 UVM_KMF_VAONLY | UVM_KMF_WAITVA); 195 196 /* 197 * Create the DVMA maps. 198 */ 199 dvma_init(); 200 201 /* 202 * Set up CPU-specific registers, cache, etc. 203 */ 204 initcpu(); 205 206 if (fputype == FPU_NONE) { 207 /* Might as well turn the enable bit back off. */ 208 enable_fpu(0); 209 } 210 } 211 212 void 213 machine_set_model(void) 214 { 215 u_char machtype = identity_prom.idp_machtype; 216 if ((machtype & IDM_ARCH_MASK) != IDM_ARCH_SUN3X) { 217 printf("Bad IDPROM arch!\n"); 218 sunmon_abort(); 219 } 220 221 cpu_machine_id = machtype; 222 switch (cpu_machine_id) { 223 224 case ID_SUN3X_80: 225 cpu_string = "80"; /* Hydra */ 226 cpuspeed_khz = 20000; 227 delay_divisor = delay_divisor_est(20); 228 cpu_has_vme = false; 229 break; 230 231 case ID_SUN3X_470: 232 cpu_string = "470"; /* Pegasus */ 233 cpuspeed_khz = 33000; 234 delay_divisor = delay_divisor_est(33); 235 cpu_has_vme = true; 236 break; 237 238 default: 239 printf("unknown sun3x model\n"); 240 sunmon_abort(); 241 } 242 243 sun68k_set_model(); 244 } 245 246 /* 247 * machine dependent system variables. 248 */ 249 #if 0 /* XXX - Not yet... */ 250 static int 251 sysctl_machdep_root_device(SYSCTLFN_ARGS) 252 { 253 struct sysctlnode node = *rnode; 254 255 node.sysctl_data = some permutation on root_device; 256 node.sysctl_size = strlen(root_device) + 1; 257 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 258 } 259 260 static int 261 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS) 262 { 263 struct sysctlnode node = *rnode; 264 265 node.sysctl_data = some permutation on booted_kernel; 266 node.sysctl_size = strlen(booted_kernel) + 1; 267 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 268 } 269 #endif 270 271 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup") 272 { 273 274 sysctl_createv(clog, 0, NULL, NULL, 275 CTLFLAG_PERMANENT, 276 CTLTYPE_NODE, "machdep", NULL, 277 NULL, 0, NULL, 0, 278 CTL_MACHDEP, CTL_EOL); 279 280 sysctl_createv(clog, 0, NULL, NULL, 281 CTLFLAG_PERMANENT, 282 CTLTYPE_STRUCT, "console_device", NULL, 283 sysctl_consdev, 0, NULL, sizeof(dev_t), 284 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL); 285 #if 0 /* XXX - Not yet... */ 286 sysctl_createv(clog, 0, NULL, NULL, 287 CTLFLAG_PERMANENT, 288 CTLTYPE_STRING, "root_device", NULL, 289 sysctl_machdep_root_device, 0, NULL, 0, 290 CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL); 291 sysctl_createv(clog, 0, NULL, NULL, 292 CTLFLAG_PERMANENT, 293 CTLTYPE_STRING, "booted_kernel", NULL, 294 sysctl_machdep_booted_kernel, 0, NULL, 0, 295 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL); 296 #endif 297 } 298 299 void 300 machine_halt(void) 301 { 302 sunmon_halt(); 303 } 304 305 void 306 machine_reboot(int howto, char *user_bootstr) 307 { 308 static char bootstr[128]; /* N.B. MUST BE STATIC */ 309 char *p; 310 311 if (user_bootstr != NULL) { 312 strlcpy(bootstr, user_bootstr, sizeof(bootstr)); 313 } else { 314 /* 315 * Build our own boot string with an empty 316 * boot device/file and (maybe) some flags. 317 * The PROM will supply the device/file name. 318 */ 319 p = bootstr; 320 *p = '\0'; 321 322 if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) { 323 /* Append the boot flags. */ 324 *p++ = ' '; 325 *p++ = '-'; 326 if (howto & RB_KDB) 327 *p++ = 'd'; 328 if (howto & RB_ASKNAME) 329 *p++ = 'a'; 330 if (howto & RB_SINGLE) 331 *p++ = 's'; 332 *p = '\0'; 333 } 334 } 335 sunmon_reboot(bootstr); 336 } 337 338 /* 339 * These variables are needed by /sbin/savecore 340 */ 341 uint32_t dumpmag = 0x8fca0101; /* magic number */ 342 int dumpsize = 0; /* pages */ 343 long dumplo = 0; /* blocks */ 344 345 #define DUMP_EXTRA 1 /* CPU-dependent extra pages */ 346 347 /* 348 * This is called by main to set dumplo, dumpsize. 349 * Dumps always skip the first PAGE_SIZE of disk space 350 * in case there might be a disk label stored there. 351 * If there is extra space, put dump at the end to 352 * reduce the chance that swapping trashes it. 353 */ 354 void 355 cpu_dumpconf(void) 356 { 357 int devblks; /* size of dump device in blocks */ 358 int dumpblks; /* size of dump image in blocks */ 359 360 if (dumpdev == NODEV) 361 return; 362 363 devblks = bdev_size(dumpdev); 364 if (devblks <= ctod(1)) 365 return; 366 devblks &= ~(ctod(1) - 1); 367 368 /* 369 * Note: savecore expects dumpsize to be the 370 * number of pages AFTER the dump header. 371 */ 372 dumpsize = physmem; /* pages */ 373 374 /* Position dump image near end of space, page aligned. */ 375 dumpblks = ctod(physmem + DUMP_EXTRA); 376 dumplo = devblks - dumpblks; 377 378 /* If it does not fit, truncate it by moving dumplo. */ 379 /* Note: Must force signed comparison. */ 380 if (dumplo < ((long)ctod(1))) { 381 dumplo = ctod(1); 382 dumpsize = dtoc(devblks - dumplo) - DUMP_EXTRA; 383 } 384 } 385 386 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */ 387 struct pcb dumppcb; 388 389 /* 390 * Write a crash dump. The format while in swap is: 391 * kcore_seg_t cpu_hdr; 392 * cpu_kcore_hdr_t cpu_data; 393 * padding (PAGE_SIZE-sizeof(kcore_seg_t)) 394 * pagemap (2*PAGE_SIZE) 395 * physical memory... 396 */ 397 void 398 dumpsys(void) 399 { 400 const struct bdevsw *dsw; 401 kcore_seg_t *kseg_p; 402 cpu_kcore_hdr_t *chdr_p; 403 struct sun3x_kcore_hdr *sh; 404 phys_ram_seg_t *crs_p; 405 char *vaddr; 406 paddr_t paddr; 407 int psize, todo, seg, segsz; 408 daddr_t blkno; 409 int error = 0; 410 411 if (dumpdev == NODEV) 412 return; 413 dsw = bdevsw_lookup(dumpdev); 414 if (dsw == NULL || dsw->d_psize == NULL) 415 return; 416 417 /* 418 * For dumps during autoconfiguration, 419 * if dump device has already configured... 420 */ 421 if (dumpsize == 0) 422 cpu_dumpconf(); 423 if (dumplo <= 0) { 424 printf("\ndump to dev %u,%u not possible\n", 425 major(dumpdev), minor(dumpdev)); 426 return; 427 } 428 savectx(&dumppcb); 429 430 psize = bdev_size(dumpdev); 431 if (psize == -1) { 432 printf("dump area unavailable\n"); 433 return; 434 } 435 436 printf("\ndumping to dev %u,%u offset %ld\n", 437 major(dumpdev), minor(dumpdev), dumplo); 438 439 /* 440 * Prepare the dump header 441 */ 442 blkno = dumplo; 443 todo = dumpsize; /* pages */ 444 vaddr = (char *)dumppage; 445 memset(vaddr, 0, PAGE_SIZE); 446 447 /* Set pointers to all three parts. */ 448 kseg_p = (kcore_seg_t *)vaddr; 449 chdr_p = (cpu_kcore_hdr_t *)(kseg_p + 1); 450 sh = &chdr_p->un._sun3x; 451 452 /* Fill in kcore_seg_t part. */ 453 CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU); 454 kseg_p->c_size = (ctob(DUMP_EXTRA) - sizeof(*kseg_p)); 455 456 /* Fill in cpu_kcore_hdr_t part. */ 457 strncpy(chdr_p->name, kernel_arch, sizeof(chdr_p->name)); 458 chdr_p->page_size = PAGE_SIZE; 459 chdr_p->kernbase = KERNBASE3X; 460 461 /* Fill in the sun3x_kcore_hdr part. */ 462 pmap_kcore_hdr(sh); 463 464 /* Write out the dump header. */ 465 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE); 466 if (error) 467 goto fail; 468 blkno += btodb(PAGE_SIZE); 469 470 /* 471 * Now dump physical memory. Note that physical memory 472 * might NOT be contiguous, so do it by segments. 473 */ 474 475 vaddr_t vmmap = curcpu()->ci_tmpva_vmmap; 476 vaddr = (char *)vmmap; 477 478 for (seg = 0; seg < SUN3X_NPHYS_RAM_SEGS; seg++) { 479 crs_p = &sh->ram_segs[seg]; 480 paddr = crs_p->start; 481 segsz = crs_p->size; 482 483 while (todo && (segsz > 0)) { 484 485 /* Print pages left after every 16. */ 486 if ((todo & 0xf) == 0) 487 printf_nolog("\r%4d", todo); 488 489 /* Make a temporary mapping for the page. */ 490 pmap_kenter_pa(vmmap, paddr | PMAP_NC, VM_PROT_READ, 0); 491 pmap_update(pmap_kernel()); 492 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, 493 PAGE_SIZE); 494 pmap_kremove(vmmap, PAGE_SIZE); 495 pmap_update(pmap_kernel()); 496 if (error) 497 goto fail; 498 paddr += PAGE_SIZE; 499 segsz -= PAGE_SIZE; 500 blkno += btodb(PAGE_SIZE); 501 todo--; 502 } 503 } 504 printf("\rdump succeeded\n"); 505 return; 506 fail: 507 printf(" dump error=%d\n", error); 508 } 509 510 static void 511 initcpu(void) 512 { 513 /* XXX: Enable RAM parity/ECC checking? */ 514 /* XXX: parityenable(); */ 515 516 #ifdef HAVECACHE 517 cache_enable(); 518 #endif 519 } 520 521 /* straptrap() in trap.c */ 522 523 /* from hp300: badaddr() */ 524 /* peek_byte(), peek_word() moved to bus_subr.c */ 525 526 /* XXX: parityenable() ? */ 527 /* regdump() moved to regdump.c */ 528 529 bool 530 mm_md_direct_mapped_phys(paddr_t paddr, vaddr_t *vaddr) 531 { 532 extern paddr_t avail_start; 533 534 if (paddr >= avail_start) 535 return false; 536 *vaddr = KERNBASE3X + paddr; 537 return true; 538 } 539 540 /* 541 * Allow access to the PROM mapping similar to uvm_kernacc(). 542 */ 543 int 544 mm_md_kernacc(void *ptr, vm_prot_t prot, bool *handled) 545 { 546 547 if ((vaddr_t)ptr < SUN3X_PROM_BASE || (vaddr_t)ptr > SUN3X_MONEND) { 548 *handled = false; 549 return 0; 550 } 551 552 *handled = true; 553 /* Read in the PROM itself is OK. */ 554 if ((prot & VM_PROT_WRITE) == 0) 555 return 0; 556 557 /* PROM data page is OK for read/write. */ 558 if ((vaddr_t)ptr >= SUN3X_MONDATA && 559 (vaddr_t)ptr < SUN3X_MONDATA + PAGE_SIZE) 560 return 0; 561 return EFAULT; 562 } 563