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