1 /* $NetBSD: machdep.c,v 1.130 2011/06/14 15:23:19 tsutsui 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.130 2011/06/14 15:23:19 tsutsui 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/malloc.h> 57 #include <sys/mbuf.h> 58 #include <sys/msgbuf.h> 59 #include <sys/ioctl.h> 60 #include <sys/tty.h> 61 #include <sys/mount.h> 62 #include <sys/exec.h> 63 #include <sys/exec_aout.h> /* for MID_* */ 64 #include <sys/core.h> 65 #include <sys/kcore.h> 66 #include <sys/vnode.h> 67 #include <sys/syscallargs.h> 68 #include <sys/ksyms.h> 69 #include <sys/module.h> 70 #ifdef KGDB 71 #include <sys/kgdb.h> 72 #endif 73 74 #include <uvm/uvm_extern.h> 75 76 #include <sys/sysctl.h> 77 78 #include <dev/cons.h> 79 #include <dev/mm.h> 80 81 #include <machine/cpu.h> 82 #include <machine/dvma.h> 83 #include <machine/idprom.h> 84 #include <machine/kcore.h> 85 #include <machine/reg.h> 86 #include <machine/pcb.h> 87 #include <machine/psl.h> 88 #include <machine/pte.h> 89 #include <machine/mon.h> 90 91 #if defined(DDB) 92 #include <machine/db_machdep.h> 93 #include <ddb/db_sym.h> 94 #include <ddb/db_extern.h> 95 #endif 96 97 #include <sun3/sun3/machdep.h> 98 99 #include "ksyms.h" 100 101 /* Defined in locore.s */ 102 extern char kernel_text[]; 103 /* Defined by the linker */ 104 extern char etext[]; 105 106 /* kernel_arch specific values required by module(9) */ 107 const vaddr_t kernbase = KERNBASE3X; 108 const vaddr_t kern_end = KERN_END3X; 109 110 /* Our exported CPU info; we can have only one. */ 111 struct cpu_info cpu_info_store; 112 113 struct vm_map *phys_map = NULL; 114 115 int physmem; 116 int fputype; 117 void * msgbufaddr; 118 119 /* Virtual page frame for /dev/mem (see mem.c) */ 120 vaddr_t vmmap; 121 122 /* 123 * safepri is a safe priority for sleep to set for a spin-wait 124 * during autoconfiguration or after a panic. 125 */ 126 int safepri = PSL_LOWIPL; 127 128 u_char cpu_machine_id = 0; 129 const char *cpu_string = NULL; 130 int cpu_has_vme = 0; 131 int has_iocache = 0; 132 133 vaddr_t dumppage; 134 135 static void identifycpu(void); 136 static void initcpu(void); 137 138 /* 139 * Console initialization: called early on from main, 140 * before vm init or cpu_startup. This system is able 141 * to use the console for output immediately (via PROM) 142 * but can not use it for input until after this point. 143 */ 144 void 145 consinit(void) 146 { 147 148 /* 149 * Switch from the PROM console (output only) 150 * to our own console driver. 151 */ 152 cninit(); 153 154 #if NKSYMS || defined(DDB) || defined(MODULAR) 155 { 156 extern int nsym; 157 extern char *ssym, *esym; 158 159 ksyms_addsyms_elf(nsym, ssym, esym); 160 } 161 #endif /* DDB */ 162 163 /* 164 * Now that the console can do input as well as 165 * output, consider stopping for a debugger. 166 */ 167 if (boothowto & RB_KDB) { 168 #ifdef KGDB 169 /* XXX - Ask on console for kgdb_dev? */ 170 /* Note: this will just return if kgdb_dev==NODEV */ 171 kgdb_connect(1); 172 #else /* KGDB */ 173 /* Either DDB or no debugger (just PROM). */ 174 Debugger(); 175 #endif /* KGDB */ 176 } 177 } 178 179 /* 180 * cpu_startup: allocate memory for variable-sized tables, 181 * initialize CPU, and do autoconfiguration. 182 * 183 * This is called early in init_main.c:main(), after the 184 * kernel memory allocator is ready for use, but before 185 * the creation of processes 1,2, and mountroot, etc. 186 */ 187 void 188 cpu_startup(void) 189 { 190 char *v; 191 vaddr_t minaddr, maxaddr; 192 char pbuf[9]; 193 194 /* 195 * Initialize message buffer (for kernel printf). 196 * This is put in physical page zero so it will 197 * always be in the same place after a reboot. 198 * Its mapping was prepared in pmap_bootstrap(). 199 * Also, offset some to avoid PROM scribbles. 200 */ 201 v = (char *)KERNBASE3X; 202 msgbufaddr = v + MSGBUFOFF; 203 initmsgbuf(msgbufaddr, MSGBUFSIZE); 204 205 /* 206 * Good {morning,afternoon,evening,night}. 207 */ 208 printf("%s%s", copyright, version); 209 identifycpu(); 210 initfpu(); /* also prints FPU type */ 211 212 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 213 printf("total memory = %s\n", pbuf); 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 minaddr = 0; 223 224 /* 225 * Allocate a submap for physio 226 */ 227 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 228 VM_PHYS_SIZE, 0, false, NULL); 229 230 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 231 printf("avail memory = %s\n", pbuf); 232 233 /* 234 * Allocate a virtual page (for use by /dev/mem) 235 * This page is handed to pmap_enter() therefore 236 * it has to be in the normal kernel VA range. 237 */ 238 vmmap = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, 239 UVM_KMF_VAONLY | UVM_KMF_WAITVA); 240 241 /* 242 * Create the DVMA maps. 243 */ 244 dvma_init(); 245 246 /* 247 * Set up CPU-specific registers, cache, etc. 248 */ 249 initcpu(); 250 } 251 252 /* 253 * Info for CTL_HW 254 */ 255 char machine[16] = MACHINE; /* from <machine/param.h> */ 256 char kernel_arch[16] = "sun3x"; /* XXX needs a sysctl node */ 257 char cpu_model[120]; 258 259 /* 260 * XXX - Should empirically estimate the divisor... 261 * Note that the value of delay_divisor is roughly 262 * 2048 / cpuclock (where cpuclock is in MHz). 263 */ 264 int delay_divisor = 62; /* assume the fastest (33 MHz) */ 265 266 void 267 identifycpu(void) 268 { 269 u_char machtype; 270 271 machtype = identity_prom.idp_machtype; 272 if ((machtype & IDM_ARCH_MASK) != IDM_ARCH_SUN3X) { 273 printf("Bad IDPROM arch!\n"); 274 sunmon_abort(); 275 } 276 277 cpu_machine_id = machtype; 278 switch (cpu_machine_id) { 279 280 case ID_SUN3X_80: 281 cpu_string = "80"; /* Hydra */ 282 delay_divisor = 102; /* 20 MHz */ 283 cpu_has_vme = false; 284 break; 285 286 case ID_SUN3X_470: 287 cpu_string = "470"; /* Pegasus */ 288 delay_divisor = 62; /* 33 MHz */ 289 cpu_has_vme = true; 290 break; 291 292 default: 293 printf("unknown sun3x model\n"); 294 sunmon_abort(); 295 } 296 297 /* Other stuff? (VAC, mc6888x version, etc.) */ 298 /* Note: miniroot cares about the kernel_arch part. */ 299 sprintf(cpu_model, "%s %s", kernel_arch, cpu_string); 300 301 printf("Model: %s\n", cpu_model); 302 } 303 304 /* 305 * machine dependent system variables. 306 */ 307 #if 0 /* XXX - Not yet... */ 308 static int 309 sysctl_machdep_root_device(SYSCTLFN_ARGS) 310 { 311 struct sysctlnode node = *rnode; 312 313 node.sysctl_data = some permutation on root_device; 314 node.sysctl_size = strlen(root_device) + 1; 315 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 316 } 317 318 static int 319 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS) 320 { 321 struct sysctlnode node = *rnode; 322 323 node.sysctl_data = some permutation on booted_kernel; 324 node.sysctl_size = strlen(booted_kernel) + 1; 325 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 326 } 327 #endif 328 329 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup") 330 { 331 332 sysctl_createv(clog, 0, NULL, NULL, 333 CTLFLAG_PERMANENT, 334 CTLTYPE_NODE, "machdep", NULL, 335 NULL, 0, NULL, 0, 336 CTL_MACHDEP, CTL_EOL); 337 338 sysctl_createv(clog, 0, NULL, NULL, 339 CTLFLAG_PERMANENT, 340 CTLTYPE_STRUCT, "console_device", NULL, 341 sysctl_consdev, 0, NULL, sizeof(dev_t), 342 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL); 343 #if 0 /* XXX - Not yet... */ 344 sysctl_createv(clog, 0, NULL, NULL, 345 CTLFLAG_PERMANENT, 346 CTLTYPE_STRING, "root_device", NULL, 347 sysctl_machdep_root_device, 0, NULL, 0, 348 CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL); 349 sysctl_createv(clog, 0, NULL, NULL, 350 CTLFLAG_PERMANENT, 351 CTLTYPE_STRING, "booted_kernel", NULL, 352 sysctl_machdep_booted_kernel, 0, NULL, 0, 353 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL); 354 #endif 355 } 356 357 /* See: sig_machdep.c */ 358 359 /* 360 * Do a sync in preparation for a reboot. 361 * XXX - This could probably be common code. 362 * XXX - And now, most of it is in vfs_shutdown() 363 * XXX - Put waittime checks in there too? 364 */ 365 int waittime = -1; /* XXX - Who else looks at this? -gwr */ 366 static void 367 reboot_sync(void) 368 { 369 370 /* Check waittime here to localize its use to this function. */ 371 if (waittime >= 0) 372 return; 373 waittime = 0; 374 vfs_shutdown(); 375 } 376 377 /* 378 * Common part of the BSD and SunOS reboot system calls. 379 */ 380 __dead void 381 cpu_reboot(int howto, char *user_boot_string) 382 { 383 /* Note: this string MUST be static! */ 384 static char bootstr[128]; 385 char *p; 386 387 /* If system is cold, just halt. (early panic?) */ 388 if (cold) 389 goto haltsys; 390 391 /* Un-blank the screen if appropriate. */ 392 cnpollc(1); 393 394 if ((howto & RB_NOSYNC) == 0) { 395 reboot_sync(); 396 /* 397 * If we've been adjusting the clock, the todr 398 * will be out of synch; adjust it now. 399 * 400 * XXX - However, if the kernel has been sitting in ddb, 401 * the time will be way off, so don't set the HW clock! 402 * XXX - Should do sanity check against HW clock. -gwr 403 */ 404 /* resettodr(); */ 405 } 406 407 /* Disable interrupts. */ 408 splhigh(); 409 410 /* Write out a crash dump if asked. */ 411 if (howto & RB_DUMP) 412 dumpsys(); 413 414 /* run any shutdown hooks */ 415 doshutdownhooks(); 416 417 pmf_system_shutdown(boothowto); 418 419 if (howto & RB_HALT) { 420 haltsys: 421 printf("halted.\n"); 422 sunmon_halt(); 423 } 424 425 /* 426 * Automatic reboot. 427 */ 428 if (user_boot_string) 429 strncpy(bootstr, user_boot_string, sizeof(bootstr)); 430 else { 431 /* 432 * Build our own boot string with an empty 433 * boot device/file and (maybe) some flags. 434 * The PROM will supply the device/file name. 435 */ 436 p = bootstr; 437 *p = '\0'; 438 if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) { 439 /* Append the boot flags. */ 440 *p++ = ' '; 441 *p++ = '-'; 442 if (howto & RB_KDB) 443 *p++ = 'd'; 444 if (howto & RB_ASKNAME) 445 *p++ = 'a'; 446 if (howto & RB_SINGLE) 447 *p++ = 's'; 448 *p = '\0'; 449 } 450 } 451 printf("rebooting...\n"); 452 sunmon_reboot(bootstr); 453 for (;;) ; 454 /*NOTREACHED*/ 455 } 456 457 /* 458 * These variables are needed by /sbin/savecore 459 */ 460 uint32_t dumpmag = 0x8fca0101; /* magic number */ 461 int dumpsize = 0; /* pages */ 462 long dumplo = 0; /* blocks */ 463 464 #define DUMP_EXTRA 1 /* CPU-dependent extra pages */ 465 466 /* 467 * This is called by main to set dumplo, dumpsize. 468 * Dumps always skip the first PAGE_SIZE of disk space 469 * in case there might be a disk label stored there. 470 * If there is extra space, put dump at the end to 471 * reduce the chance that swapping trashes it. 472 */ 473 void 474 cpu_dumpconf(void) 475 { 476 const struct bdevsw *bdev; 477 int devblks; /* size of dump device in blocks */ 478 int dumpblks; /* size of dump image in blocks */ 479 int (*getsize)(dev_t); 480 481 if (dumpdev == NODEV) 482 return; 483 484 bdev = bdevsw_lookup(dumpdev); 485 if (bdev == NULL) { 486 dumpdev = NODEV; 487 return; 488 } 489 getsize = bdev->d_psize; 490 if (getsize == NULL) 491 return; 492 devblks = (*getsize)(dumpdev); 493 if (devblks <= ctod(1)) 494 return; 495 devblks &= ~(ctod(1) - 1); 496 497 /* 498 * Note: savecore expects dumpsize to be the 499 * number of pages AFTER the dump header. 500 */ 501 dumpsize = physmem; /* pages */ 502 503 /* Position dump image near end of space, page aligned. */ 504 dumpblks = ctod(physmem + DUMP_EXTRA); 505 dumplo = devblks - dumpblks; 506 507 /* If it does not fit, truncate it by moving dumplo. */ 508 /* Note: Must force signed comparison. */ 509 if (dumplo < ((long)ctod(1))) { 510 dumplo = ctod(1); 511 dumpsize = dtoc(devblks - dumplo) - DUMP_EXTRA; 512 } 513 } 514 515 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */ 516 struct pcb dumppcb; 517 518 /* 519 * Write a crash dump. The format while in swap is: 520 * kcore_seg_t cpu_hdr; 521 * cpu_kcore_hdr_t cpu_data; 522 * padding (PAGE_SIZE-sizeof(kcore_seg_t)) 523 * pagemap (2*PAGE_SIZE) 524 * physical memory... 525 */ 526 void 527 dumpsys(void) 528 { 529 const struct bdevsw *dsw; 530 kcore_seg_t *kseg_p; 531 cpu_kcore_hdr_t *chdr_p; 532 struct sun3x_kcore_hdr *sh; 533 phys_ram_seg_t *crs_p; 534 char *vaddr; 535 paddr_t paddr; 536 int psize, todo, seg, segsz; 537 daddr_t blkno; 538 int error = 0; 539 540 if (dumpdev == NODEV) 541 return; 542 dsw = bdevsw_lookup(dumpdev); 543 if (dsw == NULL || dsw->d_psize == NULL) 544 return; 545 546 /* 547 * For dumps during autoconfiguration, 548 * if dump device has already configured... 549 */ 550 if (dumpsize == 0) 551 cpu_dumpconf(); 552 if (dumplo <= 0) { 553 printf("\ndump to dev %u,%u not possible\n", 554 major(dumpdev), minor(dumpdev)); 555 return; 556 } 557 savectx(&dumppcb); 558 559 psize = (*(dsw->d_psize))(dumpdev); 560 if (psize == -1) { 561 printf("dump area unavailable\n"); 562 return; 563 } 564 565 printf("\ndumping to dev %u,%u offset %ld\n", 566 major(dumpdev), minor(dumpdev), dumplo); 567 568 /* 569 * Prepare the dump header 570 */ 571 blkno = dumplo; 572 todo = dumpsize; /* pages */ 573 vaddr = (char *)dumppage; 574 memset(vaddr, 0, PAGE_SIZE); 575 576 /* Set pointers to all three parts. */ 577 kseg_p = (kcore_seg_t *)vaddr; 578 chdr_p = (cpu_kcore_hdr_t *)(kseg_p + 1); 579 sh = &chdr_p->un._sun3x; 580 581 /* Fill in kcore_seg_t part. */ 582 CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU); 583 kseg_p->c_size = (ctob(DUMP_EXTRA) - sizeof(*kseg_p)); 584 585 /* Fill in cpu_kcore_hdr_t part. */ 586 strncpy(chdr_p->name, kernel_arch, sizeof(chdr_p->name)); 587 chdr_p->page_size = PAGE_SIZE; 588 chdr_p->kernbase = KERNBASE3X; 589 590 /* Fill in the sun3x_kcore_hdr part. */ 591 pmap_kcore_hdr(sh); 592 593 /* Write out the dump header. */ 594 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE); 595 if (error) 596 goto fail; 597 blkno += btodb(PAGE_SIZE); 598 599 /* 600 * Now dump physical memory. Note that physical memory 601 * might NOT be congiguous, so do it by segments. 602 */ 603 604 vaddr = (char *)vmmap; /* Borrow /dev/mem VA */ 605 606 for (seg = 0; seg < SUN3X_NPHYS_RAM_SEGS; seg++) { 607 crs_p = &sh->ram_segs[seg]; 608 paddr = crs_p->start; 609 segsz = crs_p->size; 610 611 while (todo && (segsz > 0)) { 612 613 /* Print pages left after every 16. */ 614 if ((todo & 0xf) == 0) 615 printf_nolog("\r%4d", todo); 616 617 /* Make a temporary mapping for the page. */ 618 pmap_kenter_pa(vmmap, paddr | PMAP_NC, VM_PROT_READ, 0); 619 pmap_update(pmap_kernel()); 620 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, 621 PAGE_SIZE); 622 pmap_kremove(vmmap, PAGE_SIZE); 623 pmap_update(pmap_kernel()); 624 if (error) 625 goto fail; 626 paddr += PAGE_SIZE; 627 segsz -= PAGE_SIZE; 628 blkno += btodb(PAGE_SIZE); 629 todo--; 630 } 631 } 632 printf("\rdump succeeded\n"); 633 return; 634 fail: 635 printf(" dump error=%d\n", error); 636 } 637 638 static void 639 initcpu(void) 640 { 641 /* XXX: Enable RAM parity/ECC checking? */ 642 /* XXX: parityenable(); */ 643 644 #ifdef HAVECACHE 645 cache_enable(); 646 #endif 647 } 648 649 /* straptrap() in trap.c */ 650 651 /* from hp300: badaddr() */ 652 /* peek_byte(), peek_word() moved to bus_subr.c */ 653 654 /* XXX: parityenable() ? */ 655 /* regdump() moved to regdump.c */ 656 657 /* 658 * cpu_exec_aout_makecmds(): 659 * CPU-dependent a.out format hook for execve(). 660 * 661 * Determine if the given exec package refers to something which we 662 * understand and, if so, set up the vmcmds for it. 663 */ 664 int 665 cpu_exec_aout_makecmds(struct lwp *l, struct exec_package *epp) 666 { 667 return ENOEXEC; 668 } 669 670 int 671 mm_md_physacc(paddr_t pa, vm_prot_t prot) 672 { 673 674 return pmap_pa_exists(pa) ? 0 : EFAULT; 675 } 676 677 bool 678 mm_md_direct_mapped_phys(paddr_t paddr, vaddr_t *vaddr) 679 { 680 extern paddr_t avail_start; 681 682 if (paddr >= avail_start) 683 return false; 684 *vaddr = KERNBASE3X + paddr; 685 return true; 686 } 687 688 /* 689 * Allow access to the PROM mapping similiar to uvm_kernacc(). 690 */ 691 int 692 mm_md_kernacc(void *ptr, vm_prot_t prot, bool *handled) 693 { 694 695 if ((vaddr_t)ptr < SUN3X_PROM_BASE || (vaddr_t)ptr > SUN3X_MONEND) { 696 *handled = false; 697 return 0; 698 } 699 700 *handled = true; 701 /* Read in the PROM itself is OK. */ 702 if ((prot & VM_PROT_WRITE) == 0) 703 return 0; 704 705 /* PROM data page is OK for read/write. */ 706 if ((vaddr_t)ptr >= SUN3X_MONDATA && 707 (vaddr_t)ptr < SUN3X_MONDATA + PAGE_SIZE) 708 return 0; 709 return EFAULT; 710 } 711 712 #ifdef MODULAR 713 /* 714 * Push any modules loaded by the bootloader etc. 715 */ 716 void 717 module_init_md(void) 718 { 719 } 720 #endif 721