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