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