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