1 /* $NetBSD: machdep.c,v 1.36 2005/01/22 15:36:09 chs 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) 2001 Matthew Fredette. 41 * Copyright (c) 1994, 1995 Gordon W. Ross 42 * Copyright (c) 1993 Adam Glass 43 * Copyright (c) 1988 University of Utah. 44 * 45 * This code is derived from software contributed to Berkeley by 46 * the Systems Programming Group of the University of Utah Computer 47 * Science Department. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions 51 * are met: 52 * 1. Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 2. Redistributions in binary form must reproduce the above copyright 55 * notice, this list of conditions and the following disclaimer in the 56 * documentation and/or other materials provided with the distribution. 57 * 3. All advertising materials mentioning features or use of this software 58 * must display the following acknowledgement: 59 * This product includes software developed by the University of 60 * California, Berkeley and its contributors. 61 * 4. Neither the name of the University nor the names of its contributors 62 * may be used to endorse or promote products derived from this software 63 * without specific prior written permission. 64 * 65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 75 * SUCH DAMAGE. 76 * 77 * from: Utah Hdr: machdep.c 1.74 92/12/20 78 * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94 79 */ 80 81 /*- 82 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc. 83 * All rights reserved. 84 * 85 * This code is derived from software contributed to The NetBSD Foundation 86 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 87 * NASA Ames Research Center. 88 * 89 * Redistribution and use in source and binary forms, with or without 90 * modification, are permitted provided that the following conditions 91 * are met: 92 * 1. Redistributions of source code must retain the above copyright 93 * notice, this list of conditions and the following disclaimer. 94 * 2. Redistributions in binary form must reproduce the above copyright 95 * notice, this list of conditions and the following disclaimer in the 96 * documentation and/or other materials provided with the distribution. 97 * 3. All advertising materials mentioning features or use of this software 98 * must display the following acknowledgement: 99 * This product includes software developed by the NetBSD 100 * Foundation, Inc. and its contributors. 101 * 4. Neither the name of The NetBSD Foundation nor the names of its 102 * contributors may be used to endorse or promote products derived 103 * from this software without specific prior written permission. 104 * 105 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 106 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 107 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 108 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 109 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 110 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 111 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 112 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 113 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 114 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 115 * POSSIBILITY OF SUCH DAMAGE. 116 */ 117 118 /* 119 * Copyright (c) 1992, 1993 120 * The Regents of the University of California. All rights reserved. 121 * 122 * This software was developed by the Computer Systems Engineering group 123 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 124 * contributed to Berkeley. 125 * 126 * All advertising materials mentioning features or use of this software 127 * must display the following acknowledgement: 128 * This product includes software developed by the University of 129 * California, Lawrence Berkeley Laboratory. 130 * 131 * Redistribution and use in source and binary forms, with or without 132 * modification, are permitted provided that the following conditions 133 * are met: 134 * 1. Redistributions of source code must retain the above copyright 135 * notice, this list of conditions and the following disclaimer. 136 * 2. Redistributions in binary form must reproduce the above copyright 137 * notice, this list of conditions and the following disclaimer in the 138 * documentation and/or other materials provided with the distribution. 139 * 3. All advertising materials mentioning features or use of this software 140 * must display the following acknowledgement: 141 * This product includes software developed by the University of 142 * California, Berkeley and its contributors. 143 * 4. Neither the name of the University nor the names of its contributors 144 * may be used to endorse or promote products derived from this software 145 * without specific prior written permission. 146 * 147 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 148 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 149 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 150 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 151 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 152 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 153 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 154 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 155 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 156 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 157 * SUCH DAMAGE. 158 * 159 * @(#)machdep.c 8.6 (Berkeley) 1/14/94 160 */ 161 162 #include <sys/cdefs.h> 163 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.36 2005/01/22 15:36:09 chs Exp $"); 164 165 #include "opt_ddb.h" 166 #include "opt_kgdb.h" 167 #include "opt_fpu_emulate.h" 168 169 #include <sys/param.h> 170 #include <sys/systm.h> 171 #include <sys/kernel.h> 172 #include <sys/proc.h> 173 #include <sys/buf.h> 174 #include <sys/reboot.h> 175 #include <sys/conf.h> 176 #include <sys/file.h> 177 #include <sys/device.h> 178 #include <sys/malloc.h> 179 #include <sys/extent.h> 180 #include <sys/mbuf.h> 181 #include <sys/msgbuf.h> 182 #include <sys/ioctl.h> 183 #include <sys/tty.h> 184 #include <sys/mount.h> 185 #include <sys/user.h> 186 #include <sys/exec.h> 187 #include <sys/core.h> 188 #include <sys/kcore.h> 189 #include <sys/vnode.h> 190 #include <sys/sa.h> 191 #include <sys/syscallargs.h> 192 #include <sys/ksyms.h> 193 #ifdef KGDB 194 #include <sys/kgdb.h> 195 #endif 196 197 #include <uvm/uvm.h> /* XXX: not _extern ... need vm_map_create */ 198 199 #include <sys/sysctl.h> 200 201 #include <dev/cons.h> 202 203 #include <machine/promlib.h> 204 #include <machine/cpu.h> 205 #include <machine/dvma.h> 206 #include <machine/idprom.h> 207 #include <machine/kcore.h> 208 #include <machine/reg.h> 209 #include <machine/psl.h> 210 #include <machine/pte.h> 211 #define _SUN68K_BUS_DMA_PRIVATE 212 #include <machine/autoconf.h> 213 #include <machine/bus.h> 214 #include <machine/intr.h> 215 #include <machine/pmap.h> 216 217 #if defined(DDB) 218 #include <machine/db_machdep.h> 219 #include <ddb/db_sym.h> 220 #include <ddb/db_extern.h> 221 #endif 222 223 #include <dev/vme/vmereg.h> 224 #include <dev/vme/vmevar.h> 225 226 #include <sun2/sun2/control.h> 227 #include <sun2/sun2/enable.h> 228 #include <sun2/sun2/machdep.h> 229 230 #include <sun68k/sun68k/vme_sun68k.h> 231 232 #include "ksyms.h" 233 234 /* Defined in locore.s */ 235 extern char kernel_text[]; 236 /* Defined by the linker */ 237 extern char etext[]; 238 239 /* Our exported CPU info; we can have only one. */ 240 struct cpu_info cpu_info_store; 241 242 struct vm_map *exec_map = NULL; 243 struct vm_map *mb_map = NULL; 244 struct vm_map *phys_map = NULL; 245 246 int physmem; 247 int fputype; 248 caddr_t msgbufaddr; 249 250 /* Virtual page frame for /dev/mem (see mem.c) */ 251 vaddr_t vmmap; 252 253 /* 254 * safepri is a safe priority for sleep to set for a spin-wait 255 * during autoconfiguration or after a panic. 256 */ 257 int safepri = PSL_LOWIPL; 258 259 /* Soft copy of the enable register. */ 260 __volatile u_short enable_reg_soft = ENABLE_REG_SOFT_UNDEF; 261 262 /* 263 * Our no-fault fault handler. 264 */ 265 label_t *nofault; 266 267 /* 268 * dvmamap is used to manage DVMA memory. 269 */ 270 static struct extent *dvmamap; 271 272 /* Our private scratch page for dumping the MMU. */ 273 static vaddr_t dumppage; 274 275 static void identifycpu(void); 276 static void initcpu(void); 277 278 /* 279 * cpu_startup: allocate memory for variable-sized tables, 280 * initialize CPU, and do autoconfiguration. 281 * 282 * This is called early in init_main.c:main(), after the 283 * kernel memory allocator is ready for use, but before 284 * the creation of processes 1,2, and mountroot, etc. 285 */ 286 void 287 cpu_startup(void) 288 { 289 caddr_t v; 290 vaddr_t minaddr, maxaddr; 291 char pbuf[9]; 292 293 /* 294 * Initialize message buffer (for kernel printf). 295 * This is put in physical pages four through seven 296 * so it will always be in the same place after a 297 * reboot. (physical pages 0-3 are reserved by the PROM 298 * for its vector table and other stuff.) 299 * Its mapping was prepared in pmap_bootstrap(). 300 * Also, offset some to avoid PROM scribbles. 301 */ 302 v = (caddr_t) (PAGE_SIZE * 4); 303 msgbufaddr = (caddr_t)(v + MSGBUFOFF); 304 initmsgbuf(msgbufaddr, MSGBUFSIZE); 305 306 #if NKSYMS || defined(DDB) || defined(LKM) 307 { 308 extern int end[]; 309 extern char *esym; 310 311 ksyms_init(end[0], end + 1, (int*)esym); 312 } 313 #endif /* DDB */ 314 315 /* 316 * Good {morning,afternoon,evening,night}. 317 */ 318 printf(version); 319 identifycpu(); 320 fputype = FPU_NONE; 321 #ifdef FPU_EMULATE 322 printf("fpu: emulator\n"); 323 #else 324 printf("fpu: no math support\n"); 325 #endif 326 327 format_bytes(pbuf, sizeof(pbuf), ctob(physmem)); 328 printf("total memory = %s\n", pbuf); 329 330 /* 331 * XXX fredette - we force a small number of buffers 332 * to help me debug this on my low-memory machine. 333 * this should go away at some point, allowing the 334 * normal automatic buffer-sizing to happen. 335 */ 336 bufpages = 37; 337 338 /* 339 * Get scratch page for dumpsys(). 340 */ 341 if ((dumppage = uvm_km_alloc(kernel_map, PAGE_SIZE)) == 0) 342 panic("startup: alloc dumppage"); 343 344 345 minaddr = 0; 346 /* 347 * Allocate a submap for exec arguments. This map effectively 348 * limits the number of processes exec'ing at any time. 349 */ 350 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 351 NCARGS, VM_MAP_PAGEABLE, FALSE, NULL); 352 353 /* 354 * Allocate a submap for physio 355 */ 356 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 357 VM_PHYS_SIZE, 0, FALSE, NULL); 358 359 /* 360 * Finally, allocate mbuf cluster submap. 361 */ 362 mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr, 363 nmbclusters * mclbytes, VM_MAP_INTRSAFE, 364 FALSE, NULL); 365 366 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free)); 367 printf("avail memory = %s\n", pbuf); 368 369 /* 370 * Allocate a virtual page (for use by /dev/mem) 371 * This page is handed to pmap_enter() therefore 372 * it has to be in the normal kernel VA range. 373 */ 374 vmmap = uvm_km_valloc_wait(kernel_map, PAGE_SIZE); 375 376 /* 377 * Allocate DMA map for devices on the bus. 378 */ 379 dvmamap = extent_create("dvmamap", 380 DVMA_MAP_BASE, DVMA_MAP_BASE + DVMA_MAP_AVAIL, 381 M_DEVBUF, 0, 0, EX_NOWAIT); 382 if (dvmamap == NULL) 383 panic("unable to allocate DVMA map"); 384 385 /* 386 * Set up CPU-specific registers, cache, etc. 387 */ 388 initcpu(); 389 } 390 391 /* 392 * Set registers on exec. 393 */ 394 void 395 setregs(struct lwp *l, struct exec_package *pack, u_long stack) 396 { 397 struct trapframe *tf = (struct trapframe *)l->l_md.md_regs; 398 399 tf->tf_sr = PSL_USERSET; 400 tf->tf_pc = pack->ep_entry & ~1; 401 tf->tf_regs[D0] = 0; 402 tf->tf_regs[D1] = 0; 403 tf->tf_regs[D2] = 0; 404 tf->tf_regs[D3] = 0; 405 tf->tf_regs[D4] = 0; 406 tf->tf_regs[D5] = 0; 407 tf->tf_regs[D6] = 0; 408 tf->tf_regs[D7] = 0; 409 tf->tf_regs[A0] = 0; 410 tf->tf_regs[A1] = 0; 411 tf->tf_regs[A2] = (int)l->l_proc->p_psstr; 412 tf->tf_regs[A3] = 0; 413 tf->tf_regs[A4] = 0; 414 tf->tf_regs[A5] = 0; 415 tf->tf_regs[A6] = 0; 416 tf->tf_regs[SP] = stack; 417 418 /* restore a null state frame */ 419 l->l_addr->u_pcb.pcb_fpregs.fpf_null = 0; 420 421 l->l_md.md_flags = 0; 422 } 423 424 /* 425 * Info for CTL_HW 426 */ 427 char machine[16] = MACHINE; /* from <machine/param.h> */ 428 char kernel_arch[16] = "sun2"; /* XXX needs a sysctl node */ 429 char cpu_model[120]; 430 431 /* 432 * Determine which Sun2 model we are running on. 433 */ 434 void 435 identifycpu(void) 436 { 437 extern char *cpu_string; /* XXX */ 438 439 /* Other stuff? (VAC, mc6888x version, etc.) */ 440 /* Note: miniroot cares about the kernel_arch part. */ 441 sprintf(cpu_model, "%s %s", kernel_arch, cpu_string); 442 443 printf("Model: %s\n", cpu_model); 444 } 445 446 /* 447 * machine dependent system variables. 448 */ 449 #if 0 /* XXX - Not yet... */ 450 static int 451 sysctl_machdep_root_device(SYSCTLFN_ARGS) 452 { 453 struct sysctlnode node = *rnode; 454 455 node.sysctl_data = some permutation on root_device; 456 node.sysctl_size = strlen(root_device) + 1; 457 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 458 } 459 #endif 460 461 static int 462 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS) 463 { 464 struct sysctlnode node = *rnode; 465 char *cp; 466 467 cp = prom_getbootfile(); 468 if (cp == NULL || cp[0] == '\0') 469 return (ENOENT); 470 471 node.sysctl_data = cp; 472 node.sysctl_size = strlen(cp) + 1; 473 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 474 } 475 476 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup") 477 { 478 479 sysctl_createv(clog, 0, NULL, NULL, 480 CTLFLAG_PERMANENT, 481 CTLTYPE_NODE, "machdep", NULL, 482 NULL, 0, NULL, 0, 483 CTL_MACHDEP, CTL_EOL); 484 485 sysctl_createv(clog, 0, NULL, NULL, 486 CTLFLAG_PERMANENT, 487 CTLTYPE_STRUCT, "console_device", NULL, 488 sysctl_consdev, 0, NULL, sizeof(dev_t), 489 CTL_MACHDEP, CPU_CONSDEV, CTL_EOL); 490 #if 0 /* XXX - Not yet... */ 491 sysctl_createv(clog, 0, NULL, NULL, 492 CTLFLAG_PERMANENT, 493 CTLTYPE_STRING, "root_device", NULL, 494 sysctl_machdep_root_device, 0, NULL, 0, 495 CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL); 496 #endif 497 sysctl_createv(clog, 0, NULL, NULL, 498 CTLFLAG_PERMANENT, 499 CTLTYPE_STRING, "booted_kernel", NULL, 500 sysctl_machdep_booted_kernel, 0, NULL, 0, 501 CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL); 502 } 503 504 /* See: sig_machdep.c */ 505 506 /* 507 * Do a sync in preparation for a reboot. 508 * XXX - This could probably be common code. 509 * XXX - And now, most of it is in vfs_shutdown() 510 * XXX - Put waittime checks in there too? 511 */ 512 int waittime = -1; /* XXX - Who else looks at this? -gwr */ 513 static void 514 reboot_sync(void) 515 { 516 517 /* Check waittime here to localize its use to this function. */ 518 if (waittime >= 0) 519 return; 520 waittime = 0; 521 vfs_shutdown(); 522 } 523 524 /* 525 * Common part of the BSD and SunOS reboot system calls. 526 */ 527 __dead void 528 cpu_reboot(int howto, char *user_boot_string) 529 { 530 char *bs, *p; 531 char default_boot_string[8]; 532 533 /* If system is cold, just halt. (early panic?) */ 534 if (cold) 535 goto haltsys; 536 537 /* Un-blank the screen if appropriate. */ 538 cnpollc(1); 539 540 if ((howto & RB_NOSYNC) == 0) { 541 reboot_sync(); 542 /* 543 * If we've been adjusting the clock, the todr 544 * will be out of synch; adjust it now. 545 * 546 * XXX - However, if the kernel has been sitting in ddb, 547 * the time will be way off, so don't set the HW clock! 548 * XXX - Should do sanity check against HW clock. -gwr 549 */ 550 /* resettodr(); */ 551 } 552 553 /* Disable interrupts. */ 554 splhigh(); 555 556 /* Write out a crash dump if asked. */ 557 if (howto & RB_DUMP) 558 dumpsys(); 559 560 /* run any shutdown hooks */ 561 doshutdownhooks(); 562 563 if (howto & RB_HALT) { 564 haltsys: 565 printf("halted.\n"); 566 prom_halt(); 567 } 568 569 /* 570 * Automatic reboot. 571 */ 572 bs = user_boot_string; 573 if (bs == NULL) { 574 /* 575 * Build our own boot string with an empty 576 * boot device/file and (maybe) some flags. 577 * The PROM will supply the device/file name. 578 */ 579 bs = default_boot_string; 580 *bs = '\0'; 581 if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) { 582 /* Append the boot flags. */ 583 p = bs; 584 *p++ = ' '; 585 *p++ = '-'; 586 if (howto & RB_KDB) 587 *p++ = 'd'; 588 if (howto & RB_ASKNAME) 589 *p++ = 'a'; 590 if (howto & RB_SINGLE) 591 *p++ = 's'; 592 *p = '\0'; 593 } 594 } 595 printf("rebooting...\n"); 596 prom_boot(bs); 597 for (;;) ; 598 /*NOTREACHED*/ 599 } 600 601 /* 602 * These variables are needed by /sbin/savecore 603 */ 604 uint32_t dumpmag = 0x8fca0101; /* magic number */ 605 int dumpsize = 0; /* pages */ 606 long dumplo = 0; /* blocks */ 607 608 #define DUMP_EXTRA 3 /* CPU-dependent extra pages */ 609 610 /* 611 * This is called by main to set dumplo, dumpsize. 612 * Dumps always skip the first PAGE_SIZE of disk space 613 * in case there might be a disk label stored there. 614 * If there is extra space, put dump at the end to 615 * reduce the chance that swapping trashes it. 616 */ 617 void 618 cpu_dumpconf(void) 619 { 620 const struct bdevsw *bdev; 621 int devblks; /* size of dump device in blocks */ 622 int dumpblks; /* size of dump image in blocks */ 623 int (*getsize)(dev_t); 624 625 if (dumpdev == NODEV) 626 return; 627 628 bdev = bdevsw_lookup(dumpdev); 629 if (bdev == NULL) 630 panic("dumpconf: bad dumpdev=0x%x", dumpdev); 631 getsize = bdev->d_psize; 632 if (getsize == NULL) 633 return; 634 devblks = (*getsize)(dumpdev); 635 if (devblks <= ctod(1)) 636 return; 637 devblks &= ~(ctod(1)-1); 638 639 /* 640 * Note: savecore expects dumpsize to be the 641 * number of pages AFTER the dump header. 642 */ 643 dumpsize = physmem; 644 645 /* Position dump image near end of space, page aligned. */ 646 dumpblks = ctod(physmem + DUMP_EXTRA); 647 dumplo = devblks - dumpblks; 648 649 /* If it does not fit, truncate it by moving dumplo. */ 650 /* Note: Must force signed comparison. */ 651 if (dumplo < ((long)ctod(1))) { 652 dumplo = ctod(1); 653 dumpsize = dtoc(devblks - dumplo) - DUMP_EXTRA; 654 } 655 } 656 657 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */ 658 struct pcb dumppcb; 659 extern paddr_t avail_start; 660 661 /* 662 * Write a crash dump. The format while in swap is: 663 * kcore_seg_t cpu_hdr; 664 * cpu_kcore_hdr_t cpu_data; 665 * padding (PAGE_SIZE-sizeof(kcore_seg_t)) 666 * pagemap (2*PAGE_SIZE) 667 * physical memory... 668 */ 669 void 670 dumpsys(void) 671 { 672 const struct bdevsw *dsw; 673 kcore_seg_t *kseg_p; 674 cpu_kcore_hdr_t *chdr_p; 675 struct sun2_kcore_hdr *sh; 676 char *vaddr; 677 paddr_t paddr; 678 int psize, todo, chunk; 679 daddr_t blkno; 680 int error = 0; 681 682 if (dumpdev == NODEV) 683 return; 684 dsw = bdevsw_lookup(dumpdev); 685 if (dsw == NULL || dsw->d_psize == NULL) 686 return; 687 if (dumppage == 0) 688 return; 689 690 /* 691 * For dumps during autoconfiguration, 692 * if dump device has already configured... 693 */ 694 if (dumpsize == 0) 695 cpu_dumpconf(); 696 if (dumplo <= 0) { 697 printf("\ndump to dev %u,%u not possible\n", major(dumpdev), 698 minor(dumpdev)); 699 return; 700 } 701 savectx(&dumppcb); 702 703 psize = (*(dsw->d_psize))(dumpdev); 704 if (psize == -1) { 705 printf("dump area unavailable\n"); 706 return; 707 } 708 709 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev), 710 minor(dumpdev), dumplo); 711 712 /* 713 * Prepare the dump header, including MMU state. 714 */ 715 blkno = dumplo; 716 todo = dumpsize; /* pages */ 717 vaddr = (char*)dumppage; 718 memset(vaddr, 0, PAGE_SIZE); 719 720 /* Set pointers to all three parts. */ 721 kseg_p = (kcore_seg_t *)vaddr; 722 chdr_p = (cpu_kcore_hdr_t *) (kseg_p + 1); 723 sh = &chdr_p->un._sun2; 724 725 /* Fill in kcore_seg_t part. */ 726 CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU); 727 kseg_p->c_size = (ctob(DUMP_EXTRA) - sizeof(*kseg_p)); 728 729 /* Fill in cpu_kcore_hdr_t part. */ 730 strncpy(chdr_p->name, kernel_arch, sizeof(chdr_p->name)); 731 chdr_p->page_size = PAGE_SIZE; 732 chdr_p->kernbase = KERNBASE; 733 734 /* Fill in the sun2_kcore_hdr part (MMU state). */ 735 pmap_kcore_hdr(sh); 736 737 /* Write out the dump header. */ 738 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE); 739 if (error) 740 goto fail; 741 blkno += btodb(PAGE_SIZE); 742 743 /* translation RAM (pages zero through seven) */ 744 for(chunk = 0; chunk < (PAGE_SIZE * 8); chunk += PAGE_SIZE) { 745 pmap_get_pagemap((int*)vaddr, chunk); 746 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE); 747 if (error) 748 goto fail; 749 blkno += btodb(PAGE_SIZE); 750 } 751 752 /* 753 * Now dump physical memory. Have to do it in two chunks. 754 * The first chunk is "unmanaged" (by the VM code) and its 755 * range of physical addresses is not allow in pmap_enter. 756 * However, that segment is mapped linearly, so we can just 757 * use the virtual mappings already in place. The second 758 * chunk is done the normal way, using pmap_enter. 759 * 760 * Note that vaddr==(paddr+KERNBASE) for paddr=0 through etext. 761 */ 762 763 /* Do the first chunk (0 <= PA < avail_start) */ 764 paddr = 0; 765 chunk = btoc(avail_start); 766 if (chunk > todo) 767 chunk = todo; 768 do { 769 if ((todo & 0xf) == 0) 770 printf("\r%4d", todo); 771 vaddr = (char*)(paddr + KERNBASE); 772 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE); 773 if (error) 774 goto fail; 775 paddr += PAGE_SIZE; 776 blkno += btodb(PAGE_SIZE); 777 --todo; 778 } while (--chunk > 0); 779 780 /* Do the second chunk (avail_start <= PA < dumpsize) */ 781 vaddr = (char*)vmmap; /* Borrow /dev/mem VA */ 782 do { 783 if ((todo & 0xf) == 0) 784 printf("\r%4d", todo); 785 pmap_kenter_pa(vmmap, paddr | PMAP_NC, VM_PROT_READ); 786 pmap_update(pmap_kernel()); 787 error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE); 788 pmap_kremove(vmmap, PAGE_SIZE); 789 pmap_update(pmap_kernel()); 790 if (error) 791 goto fail; 792 paddr += PAGE_SIZE; 793 blkno += btodb(PAGE_SIZE); 794 } while (--todo > 0); 795 796 printf("\rdump succeeded\n"); 797 return; 798 fail: 799 printf(" dump error=%d\n", error); 800 } 801 802 static void 803 initcpu(void) 804 { 805 /* XXX: Enable RAM parity/ECC checking? */ 806 /* XXX: parityenable(); */ 807 808 } 809 810 /* straptrap() in trap.c */ 811 812 /* from hp300: badaddr() */ 813 814 /* XXX: parityenable() ? */ 815 /* regdump() moved to regdump.c */ 816 817 /* 818 * cpu_exec_aout_makecmds(): 819 * CPU-dependent a.out format hook for execve(). 820 * 821 * Determine if the given exec package refers to something which we 822 * understand and, if so, set up the vmcmds for it. 823 */ 824 int 825 cpu_exec_aout_makecmds(struct proc *p, struct exec_package *epp) 826 { 827 return ENOEXEC; 828 } 829 830 /* 831 * Soft interrupt support. 832 */ 833 void 834 isr_soft_request(int level) 835 { 836 u_char bit; 837 838 if ((level < _IPL_SOFT_LEVEL_MIN) || (level > _IPL_SOFT_LEVEL_MAX)) 839 return; 840 841 bit = 1 << level; 842 enable_reg_or(bit); 843 } 844 845 void 846 isr_soft_clear(int level) 847 { 848 u_char bit; 849 850 if ((level < _IPL_SOFT_LEVEL_MIN) || (level > _IPL_SOFT_LEVEL_MAX)) 851 return; 852 853 bit = 1 << level; 854 enable_reg_and(~bit); 855 } 856 857 /* 858 * Like _bus_dmamap_load(), but for raw memory allocated with 859 * bus_dmamem_alloc(). 860 */ 861 int 862 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs, 863 int nsegs, bus_size_t size, int flags) 864 { 865 struct vm_page *m; 866 paddr_t pa; 867 bus_addr_t dva; 868 bus_size_t sgsize; 869 struct pglist *mlist; 870 int pagesz = PAGE_SIZE; 871 int error; 872 873 /* 874 * Make sure that on error condition we return "no valid mappings". 875 */ 876 map->dm_nsegs = 0; 877 map->dm_mapsize = 0; 878 879 /* Allocate DVMA addresses */ 880 sgsize = (size + pagesz - 1) & -pagesz; 881 882 /* 883 * If the device can see our entire 24-bit address space, 884 * we can use any properly aligned virtual addresses. 885 */ 886 if ((map->_dm_flags & BUS_DMA_24BIT) != 0) { 887 dva = _bus_dma_valloc_skewed(sgsize, map->_dm_boundary, 888 pagesz, 0); 889 if (dva == 0) 890 return (ENOMEM); 891 } 892 893 /* 894 * Otherwise, we need virtual addresses in DVMA space. 895 */ 896 else { 897 error = extent_alloc(dvmamap, sgsize, pagesz, 898 map->_dm_boundary, 899 (flags & BUS_DMA_NOWAIT) == 0 900 ? EX_WAITOK : EX_NOWAIT, 901 (u_long *)&dva); 902 if (error) 903 return (error); 904 } 905 906 /* Fill in the segment. */ 907 map->dm_segs[0].ds_addr = dva; 908 map->dm_segs[0].ds_len = size; 909 map->dm_segs[0]._ds_va = dva; 910 map->dm_segs[0]._ds_sgsize = sgsize; 911 912 /* Map physical pages into MMU */ 913 mlist = segs[0]._ds_mlist; 914 for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) { 915 if (sgsize == 0) 916 panic("_bus_dmamap_load_raw: size botch"); 917 pa = VM_PAGE_TO_PHYS(m); 918 pmap_enter(pmap_kernel(), dva, 919 (pa & -pagesz) | PMAP_NC, 920 VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED); 921 922 dva += pagesz; 923 sgsize -= pagesz; 924 } 925 pmap_update(pmap_kernel()); 926 927 /* Make the map truly valid. */ 928 map->dm_nsegs = 1; 929 map->dm_mapsize = size; 930 931 return (0); 932 } 933 934 /* 935 * load DMA map with a linear buffer. 936 */ 937 int 938 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 939 bus_size_t buflen, struct proc *p, int flags) 940 { 941 bus_size_t sgsize; 942 vaddr_t va = (vaddr_t)buf; 943 int pagesz = PAGE_SIZE; 944 bus_addr_t dva; 945 pmap_t pmap; 946 int rv; 947 948 /* 949 * Make sure that on error condition we return "no valid mappings". 950 */ 951 map->dm_nsegs = 0; 952 map->dm_mapsize = 0; 953 954 if (buflen > map->_dm_size) 955 return (EINVAL); 956 957 /* 958 * A 24-bit device can see all of our kernel address space, so 959 * if we have KVAs, we can just load them as-is, no mapping 960 * necessary. 961 */ 962 if ((map->_dm_flags & BUS_DMA_24BIT) != 0 && p == NULL) { 963 /* 964 * XXX Need to implement "don't DMA across this boundry". 965 */ 966 if (map->_dm_boundary != 0) 967 panic("bus_dmamap_load: boundaries not implemented"); 968 map->dm_mapsize = buflen; 969 map->dm_nsegs = 1; 970 map->dm_segs[0].ds_addr = (bus_addr_t)va; 971 map->dm_segs[0].ds_len = buflen; 972 map->_dm_flags |= _BUS_DMA_DIRECTMAP; 973 return (0); 974 } 975 976 /* 977 * Allocate a region in DVMA space. 978 */ 979 sgsize = m68k_round_page(buflen + (va & (pagesz - 1))); 980 981 if (extent_alloc(dvmamap, sgsize, pagesz, map->_dm_boundary, 982 (flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT, 983 (u_long *)&dva) != 0) { 984 return (ENOMEM); 985 } 986 987 /* Fill in the segment. */ 988 map->dm_segs[0].ds_addr = dva + (va & (pagesz - 1)); 989 map->dm_segs[0].ds_len = buflen; 990 map->dm_segs[0]._ds_va = dva; 991 map->dm_segs[0]._ds_sgsize = sgsize; 992 993 /* 994 * Now map the DVMA addresses we allocated to point to the 995 * pages of the caller's buffer. 996 */ 997 if (p != NULL) 998 pmap = p->p_vmspace->vm_map.pmap; 999 else 1000 pmap = pmap_kernel(); 1001 1002 for (; buflen > 0; ) { 1003 paddr_t pa; 1004 /* 1005 * Get the physical address for this page. 1006 */ 1007 rv = pmap_extract(pmap, va, &pa); 1008 #ifdef DIAGNOSTIC 1009 if (!rv) 1010 panic("_bus_dmamap_load: no page"); 1011 #endif /* DIAGNOSTIC */ 1012 1013 /* 1014 * Compute the segment size, and adjust counts. 1015 */ 1016 sgsize = pagesz - (va & (pagesz - 1)); 1017 if (buflen < sgsize) 1018 sgsize = buflen; 1019 1020 pmap_enter(pmap_kernel(), dva, 1021 (pa & -pagesz) | PMAP_NC, 1022 VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED); 1023 1024 dva += pagesz; 1025 va += sgsize; 1026 buflen -= sgsize; 1027 } 1028 pmap_update(pmap_kernel()); 1029 1030 /* Make the map truly valid. */ 1031 map->dm_nsegs = 1; 1032 map->dm_mapsize = map->dm_segs[0].ds_len; 1033 1034 return (0); 1035 } 1036 1037 /* 1038 * unload a DMA map. 1039 */ 1040 void 1041 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 1042 { 1043 bus_dma_segment_t *segs = map->dm_segs; 1044 int nsegs = map->dm_nsegs; 1045 int flags = map->_dm_flags; 1046 bus_addr_t dva; 1047 bus_size_t len; 1048 int s, error; 1049 1050 if (nsegs != 1) 1051 panic("_bus_dmamem_unload: nsegs = %d", nsegs); 1052 1053 /* 1054 * _BUS_DMA_DIRECTMAP is set iff this map was loaded using 1055 * _bus_dmamap_load for a 24-bit device. 1056 */ 1057 if ((flags & _BUS_DMA_DIRECTMAP) != 0) { 1058 /* Nothing to release */ 1059 map->_dm_flags &= ~_BUS_DMA_DIRECTMAP; 1060 } 1061 1062 /* 1063 * Otherwise, this map was loaded using _bus_dmamap_load for a 1064 * non-24-bit device, or using _bus_dmamap_load_raw. 1065 */ 1066 else { 1067 dva = segs[0]._ds_va & -PAGE_SIZE; 1068 len = segs[0]._ds_sgsize; 1069 1070 /* 1071 * Unmap the DVMA addresses. 1072 */ 1073 pmap_remove(pmap_kernel(), dva, dva + len); 1074 pmap_update(pmap_kernel()); 1075 1076 /* 1077 * Free the DVMA addresses. 1078 */ 1079 if ((flags & BUS_DMA_24BIT) != 0) { 1080 /* 1081 * This map was loaded using _bus_dmamap_load_raw 1082 * for a 24-bit device. 1083 */ 1084 uvm_unmap(kernel_map, dva, dva + len); 1085 } else { 1086 /* 1087 * This map was loaded using _bus_dmamap_load or 1088 * _bus_dmamap_load_raw for a non-24-bit device. 1089 */ 1090 s = splhigh(); 1091 error = extent_free(dvmamap, dva, len, EX_NOWAIT); 1092 splx(s); 1093 if (error != 0) 1094 printf("warning: %ld of DVMA space lost\n", len); 1095 } 1096 } 1097 1098 /* Mark the mappings as invalid. */ 1099 map->dm_mapsize = 0; 1100 map->dm_nsegs = 0; 1101 } 1102 1103 /* 1104 * Translate a VME address and address modifier 1105 * into a CPU physical address and page type. 1106 */ 1107 int 1108 vmebus_translate(vme_am_t mod, vme_addr_t addr, bus_type_t *btp, 1109 bus_addr_t *bap) 1110 { 1111 bus_addr_t base; 1112 1113 switch(mod) { 1114 #define _DS (VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA) 1115 1116 case (VME_AM_A16|_DS): 1117 base = 0x00ff0000; 1118 break; 1119 1120 case (VME_AM_A24|_DS): 1121 base = 0; 1122 break; 1123 1124 default: 1125 return (ENOENT); 1126 #undef _DS 1127 } 1128 1129 *bap = base | addr; 1130 *btp = (*bap & 0x800000 ? PMAP_VME8 : PMAP_VME0); 1131 return (0); 1132 } 1133