Home | History | Annotate | Line # | Download | only in sun2
      1 /*	$NetBSD: machdep.c,v 1.101 2026/11/15 15:16:24 thorpej 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  *
     98  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     99  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    100  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    101  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    102  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    103  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    104  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    105  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    106  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    107  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    108  * POSSIBILITY OF SUCH DAMAGE.
    109  */
    110 
    111 /*
    112  * Copyright (c) 1992, 1993
    113  *	The Regents of the University of California.  All rights reserved.
    114  *
    115  * This software was developed by the Computer Systems Engineering group
    116  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
    117  * contributed to Berkeley.
    118  *
    119  * All advertising materials mentioning features or use of this software
    120  * must display the following acknowledgement:
    121  *	This product includes software developed by the University of
    122  *	California, Lawrence Berkeley Laboratory.
    123  *
    124  * Redistribution and use in source and binary forms, with or without
    125  * modification, are permitted provided that the following conditions
    126  * are met:
    127  * 1. Redistributions of source code must retain the above copyright
    128  *    notice, this list of conditions and the following disclaimer.
    129  * 2. Redistributions in binary form must reproduce the above copyright
    130  *    notice, this list of conditions and the following disclaimer in the
    131  *    documentation and/or other materials provided with the distribution.
    132  * 3. Neither the name of the University nor the names of its contributors
    133  *    may be used to endorse or promote products derived from this software
    134  *    without specific prior written permission.
    135  *
    136  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    137  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    138  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    139  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    140  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    141  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    142  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    143  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    144  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    145  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    146  * SUCH DAMAGE.
    147  *
    148  *	@(#)machdep.c	8.6 (Berkeley) 1/14/94
    149  */
    150 
    151 #include <sys/cdefs.h>
    152 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.101 2026/11/15 15:16:24 thorpej Exp $");
    153 
    154 #include "opt_ddb.h"
    155 #include "opt_fpu_emulate.h"
    156 #include "opt_kgdb.h"
    157 #include "opt_modular.h"
    158 
    159 #include <sys/param.h>
    160 #include <sys/buf.h>
    161 #include <sys/conf.h>
    162 #include <sys/core.h>
    163 #include <sys/cpu.h>
    164 #include <sys/device.h>
    165 #include <sys/exec.h>
    166 #include <sys/vmem.h>
    167 #include <sys/file.h>
    168 #include <sys/ioctl.h>
    169 #include <sys/kcore.h>
    170 #include <sys/kernel.h>
    171 #include <sys/ksyms.h>
    172 #include <sys/mbuf.h>
    173 #include <sys/module.h>
    174 #include <sys/mount.h>
    175 #include <sys/msgbuf.h>
    176 #include <sys/proc.h>
    177 #include <sys/reboot.h>
    178 #include <sys/syscallargs.h>
    179 #include <sys/sysctl.h>
    180 #include <sys/systm.h>
    181 #include <sys/tty.h>
    182 #include <sys/vnode.h>
    183 
    184 #ifdef	KGDB
    185 #include <sys/kgdb.h>
    186 #endif
    187 
    188 #include <uvm/uvm.h> /* XXX: not _extern ... need vm_map_create */
    189 
    190 #include <dev/cons.h>
    191 #include <dev/mm.h>
    192 
    193 #define _SUN68K_BUS_DMA_PRIVATE
    194 #include <machine/autoconf.h>
    195 #include <machine/bus.h>
    196 #include <machine/cpu.h>
    197 #include <machine/dvma.h>
    198 #include <machine/idprom.h>
    199 #include <machine/intr.h>
    200 #include <machine/kcore.h>
    201 #include <machine/pcb.h>
    202 #include <machine/pmap.h>
    203 #include <machine/promlib.h>
    204 #include <machine/psl.h>
    205 #include <machine/pte.h>
    206 #include <machine/reg.h>
    207 
    208 #if defined(DDB)
    209 #include <machine/db_machdep.h>
    210 #include <ddb/db_extern.h>
    211 #include <ddb/db_sym.h>
    212 #endif
    213 
    214 #include <dev/vme/vmereg.h>
    215 #include <dev/vme/vmevar.h>
    216 
    217 #include <sun2/sun2/control.h>
    218 #include <sun2/sun2/enable.h>
    219 #include <sun2/sun2/machdep.h>
    220 
    221 #include <sun68k/sun68k/vme_sun68k.h>
    222 
    223 #include "ksyms.h"
    224 
    225 /* Defined in locore.s */
    226 extern char kernel_text[];
    227 /* Defined by the linker */
    228 extern char etext[];
    229 /* Defined in vfs_bio.c */
    230 extern u_int bufpages;
    231 
    232 extern void *msgbufaddr;
    233 
    234 /* Soft copy of the enable register. */
    235 volatile u_short enable_reg_soft = ENABLE_REG_SOFT_UNDEF;
    236 
    237 /*
    238  * dvmamap is used to manage DVMA memory.
    239  */
    240 vmem_t *dvma_arena;
    241 
    242 /* Our private scratch page for dumping the MMU. */
    243 static vaddr_t dumppage;
    244 
    245 char	kernel_arch[16] = "sun2";	/* XXX needs a sysctl node */
    246 
    247 /*
    248  * cpu_startup: allocate memory for variable-sized tables,
    249  * initialize CPU, and do autoconfiguration.
    250  *
    251  * This is called early in init_main.c:main(), after the
    252  * kernel memory allocator is ready for use, but before
    253  * the creation of processes 1,2, and mountroot, etc.
    254  */
    255 void
    256 cpu_startup(void)
    257 {
    258 #if NKSYMS || defined(DDB) || defined(MODULAR)
    259 	{
    260 		extern int nsym;
    261 		extern char *ssym, *esym;
    262 
    263 		ksyms_addsyms_elf(nsym, ssym, esym);
    264 	}
    265 #endif /* DDB */
    266 
    267 	/*
    268 	 * XXX fredette - we force a small number of buffers
    269 	 * to help me debug this on my low-memory machine.
    270 	 * this should go away at some point, allowing the
    271 	 * normal automatic buffer-sizing to happen.
    272 	 */
    273 	bufpages = 37;
    274 
    275 	/*
    276 	 * Get scratch page for dumpsys().
    277 	 */
    278 	if ((dumppage = uvm_km_alloc(kernel_map, PAGE_SIZE,0, UVM_KMF_WIRED))
    279 	    == 0)
    280 		panic("startup: alloc dumppage");
    281 
    282 	cpu_startup_common();
    283 
    284 	/*
    285 	 * Allocate a virtual page for temporary use.
    286 	 * This page is handed to pmap_enter() therefore
    287 	 * it has to be in the normal kernel VA range.
    288 	 */
    289 	cpu_info_primary.ci_tmpva_vmmap =
    290 	    uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
    291 			 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
    292 
    293 	/*
    294 	 * Allocate DMA map for devices on the bus.
    295 	 */
    296 	dvma_arena = vmem_create("dvmamap", DVMA_MAP_BASE, DVMA_MAP_AVAIL,
    297 				 PAGE_SIZE,		/* quantum */
    298 				 NULL,			/* importfn */
    299 				 NULL,			/* releasefn */
    300 				 NULL,			/* source */
    301 				 0,			/* qcache_max */
    302 				 VM_SLEEP,
    303 				 IPL_VM);
    304 	if (dvma_arena == NULL)
    305 		panic("unable to allocate DVMA map");
    306 }
    307 
    308 /*
    309  * machine dependent system variables.
    310  */
    311 #if 0	/* XXX - Not yet... */
    312 static int
    313 sysctl_machdep_root_device(SYSCTLFN_ARGS)
    314 {
    315 	struct sysctlnode node = *rnode;
    316 
    317 	node.sysctl_data = some permutation on root_device;
    318 	node.sysctl_size = strlen(root_device) + 1;
    319 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    320 }
    321 #endif
    322 
    323 static int
    324 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
    325 {
    326 	struct sysctlnode node = *rnode;
    327 	char *cp;
    328 
    329 	cp = prom_getbootfile();
    330 	if (cp == NULL || cp[0] == '\0')
    331 		return (ENOENT);
    332 
    333 	node.sysctl_data = cp;
    334 	node.sysctl_size = strlen(cp) + 1;
    335 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    336 }
    337 
    338 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
    339 {
    340 
    341 	sysctl_createv(clog, 0, NULL, NULL,
    342 		       CTLFLAG_PERMANENT,
    343 		       CTLTYPE_NODE, "machdep", NULL,
    344 		       NULL, 0, NULL, 0,
    345 		       CTL_MACHDEP, CTL_EOL);
    346 
    347 	sysctl_createv(clog, 0, NULL, NULL,
    348 		       CTLFLAG_PERMANENT,
    349 		       CTLTYPE_STRUCT, "console_device", NULL,
    350 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    351 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
    352 #if 0	/* XXX - Not yet... */
    353 	sysctl_createv(clog, 0, NULL, NULL,
    354 		       CTLFLAG_PERMANENT,
    355 		       CTLTYPE_STRING, "root_device", NULL,
    356 		       sysctl_machdep_root_device, 0, NULL, 0,
    357 		       CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
    358 #endif
    359 	sysctl_createv(clog, 0, NULL, NULL,
    360 		       CTLFLAG_PERMANENT,
    361 		       CTLTYPE_STRING, "booted_kernel", NULL,
    362 		       sysctl_machdep_booted_kernel, 0, NULL, 0,
    363 		       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
    364 }
    365 
    366 void
    367 machine_halt(void)
    368 {
    369 	prom_halt();
    370 }
    371 
    372 void
    373 machine_reboot(int howto, char *bootstr)
    374 {
    375 	char *p;
    376 	char default_boot_string[8];
    377 
    378 	if (bootstr == NULL) {
    379 		/*
    380 		 * Build our own boot string with an empty
    381 		 * boot device/file and (maybe) some flags.
    382 		 * The PROM will supply the device/file name.
    383 		 */
    384 		p = default_boot_string;
    385 		*p = '\0';
    386 
    387 		if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
    388 			/* Append the boot flags. */
    389 			*p++ = ' ';
    390 			*p++ = '-';
    391 			if (howto & RB_KDB)
    392 				*p++ = 'd';
    393 			if (howto & RB_ASKNAME)
    394 				*p++ = 'a';
    395 			if (howto & RB_SINGLE)
    396 				*p++ = 's';
    397 			*p = '\0';
    398 		}
    399 		bootstr = default_boot_string;
    400 	}
    401 	prom_boot(bootstr);
    402 }
    403 
    404 /*
    405  * These variables are needed by /sbin/savecore
    406  */
    407 uint32_t dumpmag = 0x8fca0101;	/* magic number */
    408 int 	dumpsize = 0;		/* pages */
    409 long	dumplo = 0; 		/* blocks */
    410 
    411 #define	DUMP_EXTRA 	3	/* CPU-dependent extra pages */
    412 
    413 /*
    414  * This is called by main to set dumplo, dumpsize.
    415  * Dumps always skip the first PAGE_SIZE of disk space
    416  * in case there might be a disk label stored there.
    417  * If there is extra space, put dump at the end to
    418  * reduce the chance that swapping trashes it.
    419  */
    420 void
    421 cpu_dumpconf(void)
    422 {
    423 	int devblks;	/* size of dump device in blocks */
    424 	int dumpblks;	/* size of dump image in blocks */
    425 
    426 	if (dumpdev == NODEV)
    427 		return;
    428 
    429 	devblks = bdev_size(dumpdev);
    430 	if (devblks <= ctod(1))
    431 		return;
    432 	devblks &= ~(ctod(1)-1);
    433 
    434 	/*
    435 	 * Note: savecore expects dumpsize to be the
    436 	 * number of pages AFTER the dump header.
    437 	 */
    438 	dumpsize = physmem;
    439 
    440 	/* Position dump image near end of space, page aligned. */
    441 	dumpblks = ctod(physmem + DUMP_EXTRA);
    442 	dumplo = devblks - dumpblks;
    443 
    444 	/* If it does not fit, truncate it by moving dumplo. */
    445 	/* Note: Must force signed comparison. */
    446 	if (dumplo < ((long)ctod(1))) {
    447 		dumplo = ctod(1);
    448 		dumpsize = dtoc(devblks - dumplo) - DUMP_EXTRA;
    449 	}
    450 }
    451 
    452 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */
    453 struct pcb dumppcb;
    454 extern paddr_t avail_start;
    455 
    456 /*
    457  * Write a crash dump.  The format while in swap is:
    458  *   kcore_seg_t cpu_hdr;
    459  *   cpu_kcore_hdr_t cpu_data;
    460  *   padding (PAGE_SIZE-sizeof(kcore_seg_t))
    461  *   pagemap (2*PAGE_SIZE)
    462  *   physical memory...
    463  */
    464 void
    465 dumpsys(void)
    466 {
    467 	const struct bdevsw *dsw;
    468 	kcore_seg_t	*kseg_p;
    469 	cpu_kcore_hdr_t *chdr_p;
    470 	struct sun2_kcore_hdr *sh;
    471 	char *vaddr;
    472 	paddr_t paddr;
    473 	int psize, todo, chunk;
    474 	daddr_t blkno;
    475 	int error = 0;
    476 
    477 	if (dumpdev == NODEV)
    478 		return;
    479 	dsw = bdevsw_lookup(dumpdev);
    480 	if (dsw == NULL || dsw->d_psize == NULL)
    481 		return;
    482 	if (dumppage == 0)
    483 		return;
    484 
    485 	/*
    486 	 * For dumps during autoconfiguration,
    487 	 * if dump device has already configured...
    488 	 */
    489 	if (dumpsize == 0)
    490 		cpu_dumpconf();
    491 	if (dumplo <= 0) {
    492 		printf("\ndump to dev %u,%u not possible\n",
    493 		    major(dumpdev), minor(dumpdev));
    494 		return;
    495 	}
    496 	savectx(&dumppcb);
    497 
    498 	psize = bdev_size(dumpdev);
    499 	if (psize == -1) {
    500 		printf("dump area unavailable\n");
    501 		return;
    502 	}
    503 
    504 	printf("\ndumping to dev %u,%u offset %ld\n",
    505 	    major(dumpdev), minor(dumpdev), dumplo);
    506 
    507 	/*
    508 	 * Prepare the dump header, including MMU state.
    509 	 */
    510 	blkno = dumplo;
    511 	todo = dumpsize;	/* pages */
    512 	vaddr = (char*)dumppage;
    513 	memset(vaddr, 0, PAGE_SIZE);
    514 
    515 	/* Set pointers to all three parts. */
    516 	kseg_p = (kcore_seg_t *)vaddr;
    517 	chdr_p = (cpu_kcore_hdr_t *) (kseg_p + 1);
    518 	sh = &chdr_p->un._sun2;
    519 
    520 	/* Fill in kcore_seg_t part. */
    521 	CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
    522 	kseg_p->c_size = (ctob(DUMP_EXTRA) - sizeof(*kseg_p));
    523 
    524 	/* Fill in cpu_kcore_hdr_t part. */
    525 	strncpy(chdr_p->name, kernel_arch, sizeof(chdr_p->name));
    526 	chdr_p->page_size = PAGE_SIZE;
    527 	chdr_p->kernbase = KERNBASE;
    528 
    529 	/* Fill in the sun2_kcore_hdr part (MMU state). */
    530 	pmap_kcore_hdr(sh);
    531 
    532 	/* Write out the dump header. */
    533 	error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
    534 	if (error)
    535 		goto fail;
    536 	blkno += btodb(PAGE_SIZE);
    537 
    538 	/* translation RAM (pages zero through seven) */
    539 	for(chunk = 0; chunk < (PAGE_SIZE * 8); chunk += PAGE_SIZE) {
    540 		pmap_get_pagemap((int*)vaddr, chunk);
    541 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
    542 		if (error)
    543 			goto fail;
    544 		blkno += btodb(PAGE_SIZE);
    545 	}
    546 
    547 	/*
    548 	 * Now dump physical memory.  Have to do it in two chunks.
    549 	 * The first chunk is "unmanaged" (by the VM code) and its
    550 	 * range of physical addresses is not allow in pmap_enter.
    551 	 * However, that segment is mapped linearly, so we can just
    552 	 * use the virtual mappings already in place.  The second
    553 	 * chunk is done the normal way, using pmap_enter.
    554 	 *
    555 	 * Note that vaddr==(paddr+KERNBASE) for paddr=0 through etext.
    556 	 */
    557 
    558 	/* Do the first chunk (0 <= PA < avail_start) */
    559 	paddr = 0;
    560 	chunk = btoc(avail_start);
    561 	if (chunk > todo)
    562 		chunk = todo;
    563 	do {
    564 		if ((todo & 0xf) == 0)
    565 			printf_nolog("\r%4d", todo);
    566 		vaddr = (char*)(paddr + KERNBASE);
    567 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
    568 		if (error)
    569 			goto fail;
    570 		paddr += PAGE_SIZE;
    571 		blkno += btodb(PAGE_SIZE);
    572 		--todo;
    573 	} while (--chunk > 0);
    574 
    575 	/* Do the second chunk (avail_start <= PA < dumpsize) */
    576 	vaddr_t vmmap = curcpu()->ci_tmpva_vmmap;
    577 	vaddr = (char*)vmmap;
    578 	do {
    579 		if ((todo & 0xf) == 0)
    580 			printf_nolog("\r%4d", todo);
    581 		pmap_kenter_pa(vmmap, paddr | PMAP_NC, VM_PROT_READ, 0);
    582 		pmap_update(pmap_kernel());
    583 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
    584 		pmap_kremove(vmmap, PAGE_SIZE);
    585 		pmap_update(pmap_kernel());
    586 		if (error)
    587 			goto fail;
    588 		paddr += PAGE_SIZE;
    589 		blkno += btodb(PAGE_SIZE);
    590 	} while (--todo > 0);
    591 
    592 	printf("\rdump succeeded\n");
    593 	return;
    594 fail:
    595 	printf(" dump error=%d\n", error);
    596 }
    597 
    598 /* straptrap() in trap.c */
    599 
    600 /* from hp300: badaddr() */
    601 
    602 /* XXX: parityenable() ? */
    603 /* regdump() moved to regdump.c */
    604 
    605 #if 0
    606 /*
    607  * Soft interrupt support.
    608  */
    609 void
    610 isr_soft_request(int level)
    611 {
    612 	u_char bit;
    613 
    614 	if ((level < _IPL_SOFT_LEVEL_MIN) || (level > _IPL_SOFT_LEVEL_MAX))
    615 		return;
    616 
    617 	bit = 1 << level;
    618 	enable_reg_or(bit);
    619 }
    620 
    621 void
    622 isr_soft_clear(int level)
    623 {
    624 	u_char bit;
    625 
    626 	if ((level < _IPL_SOFT_LEVEL_MIN) || (level > _IPL_SOFT_LEVEL_MAX))
    627 		return;
    628 
    629 	bit = 1 << level;
    630 	enable_reg_and(~bit);
    631 }
    632 #endif
    633 
    634 /*
    635  * Like _bus_dmamap_load(), but for raw memory allocated with
    636  * bus_dmamem_alloc().
    637  */
    638 int
    639 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs,
    640     int nsegs, bus_size_t size, int flags)
    641 {
    642 	struct vm_page *m;
    643 	paddr_t pa;
    644 	vmem_addr_t dva;
    645 	bus_size_t sgsize;
    646 	struct pglist *mlist;
    647 	int pagesz = PAGE_SIZE;
    648 	int error;
    649 
    650 	/*
    651 	 * Make sure that on error condition we return "no valid mappings".
    652 	 */
    653 	map->dm_nsegs = 0;
    654 	map->dm_mapsize = 0;
    655 
    656 	/* Allocate DVMA addresses */
    657 	sgsize = (size + pagesz - 1) & -pagesz;
    658 
    659 	/*
    660 	 * If the device can see our entire 24-bit address space,
    661 	 * we can use any properly aligned virtual addresses.
    662 	 */
    663 	if ((map->_dm_flags & BUS_DMA_24BIT) != 0) {
    664 		dva = _bus_dma_valloc_skewed(sgsize, map->_dm_boundary,
    665 					     pagesz, 0);
    666 		if (dva == 0)
    667 			return (ENOMEM);
    668 	}
    669 
    670 	/*
    671 	 * Otherwise, we need virtual addresses in DVMA space.
    672 	 */
    673 	else {
    674 		const vm_flag_t vmflags = VM_BESTFIT |
    675 		    ((flags & BUS_DMA_NOWAIT) ? VM_NOSLEEP : VM_SLEEP);
    676 
    677 		error = vmem_xalloc(dvma_arena, sgsize,
    678 				    0,			/* alignment */
    679 				    0,			/* phase */
    680 				    map->_dm_boundary,	/* nocross */
    681 				    VMEM_ADDR_MIN,	/* minaddr */
    682 				    VMEM_ADDR_MAX,	/* maxaddr */
    683 				    vmflags,
    684 				    &dva);
    685 		if (error)
    686 			return (error);
    687 	}
    688 
    689 	/* Fill in the segment. */
    690 	map->dm_segs[0].ds_addr = dva;
    691 	map->dm_segs[0].ds_len = size;
    692 	map->dm_segs[0]._ds_va = dva;
    693 	map->dm_segs[0]._ds_sgsize = sgsize;
    694 
    695 	/* Map physical pages into MMU */
    696 	mlist = segs[0]._ds_mlist;
    697 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq.queue)) {
    698 		if (sgsize == 0)
    699 			panic("_bus_dmamap_load_raw: size botch");
    700 		pa = VM_PAGE_TO_PHYS(m);
    701 		pmap_enter(pmap_kernel(), dva,
    702 			   (pa & -pagesz) | PMAP_NC,
    703 			   VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
    704 
    705 		dva += pagesz;
    706 		sgsize -= pagesz;
    707 	}
    708 	pmap_update(pmap_kernel());
    709 
    710 	/* Make the map truly valid. */
    711 	map->dm_nsegs = 1;
    712 	map->dm_mapsize = size;
    713 
    714 	return (0);
    715 }
    716 
    717 /*
    718  * load DMA map with a linear buffer.
    719  */
    720 int
    721 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    722     bus_size_t buflen, struct proc *p, int flags)
    723 {
    724 	bus_size_t sgsize;
    725 	vaddr_t va = (vaddr_t)buf;
    726 	int pagesz = PAGE_SIZE;
    727 	vmem_addr_t dva;
    728 	pmap_t pmap;
    729 	int rv __diagused;
    730 
    731 	/*
    732 	 * Make sure that on error condition we return "no valid mappings".
    733 	 */
    734 	map->dm_nsegs = 0;
    735 	map->dm_mapsize = 0;
    736 
    737 	if (buflen > map->_dm_size)
    738 		return (EINVAL);
    739 
    740 	/*
    741 	 * A 24-bit device can see all of our kernel address space, so
    742 	 * if we have KVAs, we can just load them as-is, no mapping
    743 	 * necessary.
    744 	 */
    745 	if ((map->_dm_flags & BUS_DMA_24BIT) != 0 && p == NULL) {
    746 		/*
    747 		 * XXX Need to implement "don't DMA across this boundary".
    748 		 */
    749 		if (map->_dm_boundary != 0)
    750 			panic("bus_dmamap_load: boundaries not implemented");
    751 		map->dm_mapsize = buflen;
    752 		map->dm_nsegs = 1;
    753 		map->dm_segs[0].ds_addr = (bus_addr_t)va;
    754 		map->dm_segs[0].ds_len = buflen;
    755 		map->_dm_flags |= _BUS_DMA_DIRECTMAP;
    756 		return (0);
    757 	}
    758 
    759 	/*
    760 	 * Allocate a region in DVMA space.
    761 	 */
    762 	sgsize = m68k_round_page(buflen + (va & (pagesz - 1)));
    763 
    764 	const vm_flag_t vmflags = VM_BESTFIT |
    765 	    ((flags & BUS_DMA_NOWAIT) ? VM_NOSLEEP : VM_SLEEP);
    766 
    767 	if (vmem_xalloc(dvma_arena, sgsize,
    768 			0,			/* alignment */
    769 			0,			/* phase */
    770 			map->_dm_boundary,	/* nocross */
    771 			VMEM_ADDR_MIN,		/* minaddr */
    772 			VMEM_ADDR_MAX,		/* maxaddr */
    773 			vmflags,
    774 			&dva) != 0) {
    775 		return (ENOMEM);
    776 	}
    777 
    778 	/* Fill in the segment. */
    779 	map->dm_segs[0].ds_addr = dva + (va & (pagesz - 1));
    780 	map->dm_segs[0].ds_len = buflen;
    781 	map->dm_segs[0]._ds_va = dva;
    782 	map->dm_segs[0]._ds_sgsize = sgsize;
    783 
    784 	/*
    785 	 * Now map the DVMA addresses we allocated to point to the
    786 	 * pages of the caller's buffer.
    787 	 */
    788 	if (p != NULL)
    789 		pmap = p->p_vmspace->vm_map.pmap;
    790 	else
    791 		pmap = pmap_kernel();
    792 
    793 	for (; buflen > 0; ) {
    794 		paddr_t pa;
    795 		/*
    796 		 * Get the physical address for this page.
    797 		 */
    798 		rv = pmap_extract(pmap, va, &pa);
    799 #ifdef	DIAGNOSTIC
    800 		if (!rv)
    801 			panic("_bus_dmamap_load: no page");
    802 #endif	/* DIAGNOSTIC */
    803 
    804 		/*
    805 		 * Compute the segment size, and adjust counts.
    806 		 */
    807 		sgsize = pagesz - (va & (pagesz - 1));
    808 		if (buflen < sgsize)
    809 			sgsize = buflen;
    810 
    811 		pmap_enter(pmap_kernel(), dva,
    812 			   (pa & -pagesz) | PMAP_NC,
    813 			   VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
    814 
    815 		dva += pagesz;
    816 		va += sgsize;
    817 		buflen -= sgsize;
    818 	}
    819 	pmap_update(pmap_kernel());
    820 
    821 	/* Make the map truly valid. */
    822 	map->dm_nsegs = 1;
    823 	map->dm_mapsize = map->dm_segs[0].ds_len;
    824 
    825 	return (0);
    826 }
    827 
    828 /*
    829  * unload a DMA map.
    830  */
    831 void
    832 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    833 {
    834 	bus_dma_segment_t *segs = map->dm_segs;
    835 	int nsegs = map->dm_nsegs;
    836 	int flags = map->_dm_flags;
    837 	bus_addr_t dva;
    838 	bus_size_t len;
    839 
    840 	if (nsegs != 1)
    841 		panic("_bus_dmamem_unload: nsegs = %d", nsegs);
    842 
    843 	/*
    844 	 * _BUS_DMA_DIRECTMAP is set iff this map was loaded using
    845 	 * _bus_dmamap_load for a 24-bit device.
    846 	 */
    847 	if ((flags & _BUS_DMA_DIRECTMAP) != 0) {
    848 		/* Nothing to release */
    849 		map->_dm_flags &= ~_BUS_DMA_DIRECTMAP;
    850 	}
    851 
    852 	/*
    853 	 * Otherwise, this map was loaded using _bus_dmamap_load for a
    854 	 * non-24-bit device, or using _bus_dmamap_load_raw.
    855 	 */
    856 	else {
    857 		dva = segs[0]._ds_va & -PAGE_SIZE;
    858 		len = segs[0]._ds_sgsize;
    859 
    860 		/*
    861 		 * Unmap the DVMA addresses.
    862 		 */
    863 		pmap_remove(pmap_kernel(), dva, dva + len);
    864 		pmap_update(pmap_kernel());
    865 
    866 		/*
    867 		 * Free the DVMA addresses.
    868 		 */
    869 		if ((flags & BUS_DMA_24BIT) != 0) {
    870 			/*
    871 			 * This map was loaded using _bus_dmamap_load_raw
    872 			 * for a 24-bit device.
    873 			 */
    874 			uvm_unmap(kernel_map, dva, dva + len);
    875 		} else {
    876 			/*
    877 			 * This map was loaded using _bus_dmamap_load or
    878 			 * _bus_dmamap_load_raw for a non-24-bit device.
    879 			 */
    880 			vmem_xfree(dvma_arena, dva, len);
    881 		}
    882 	}
    883 
    884 	/* Mark the mappings as invalid. */
    885 	map->dm_mapsize = 0;
    886 	map->dm_nsegs = 0;
    887 }
    888 
    889 /*
    890  * Translate a VME address and address modifier
    891  * into a CPU physical address and page type.
    892  */
    893 int
    894 vmebus_translate(vme_am_t mod, vme_addr_t addr, bus_type_t *btp,
    895     bus_addr_t *bap)
    896 {
    897 	bus_addr_t base;
    898 
    899 	switch(mod) {
    900 #define _DS (VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA)
    901 
    902 	case (VME_AM_A16|_DS):
    903 		base = 0x00ff0000;
    904 		break;
    905 
    906 	case (VME_AM_A24|_DS):
    907 		base = 0;
    908 		break;
    909 
    910 	default:
    911 		return (ENOENT);
    912 #undef _DS
    913 	}
    914 
    915 	*bap = base | addr;
    916 	*btp = (*bap & 0x800000 ? PMAP_VME8 : PMAP_VME0);
    917 	return (0);
    918 }
    919 
    920 /*
    921  * If we can find a mapping that was established by the PROM, use it.
    922  */
    923 int
    924 find_prom_map(paddr_t pa, bus_type_t iospace, int len, vaddr_t *vap)
    925 {
    926 	u_long	pf;
    927 	int	pgtype;
    928 	vaddr_t	va, eva;
    929 	int	sme;
    930 	u_long	pte;
    931 	int	saved_ctx;
    932 
    933 	/*
    934 	 * The mapping must fit entirely within one page.
    935 	 */
    936 	if ((((u_long)pa & PGOFSET) + len) > PAGE_SIZE)
    937 		return EINVAL;
    938 
    939 	pf = PA_PGNUM(pa);
    940 	pgtype = iospace << PG_MOD_SHIFT;
    941 	saved_ctx = kernel_context();
    942 
    943 	/*
    944 	 * Walk the PROM address space, looking for a page with the
    945 	 * mapping we want.
    946 	 */
    947 	for (va = SUN_MONSTART; va < SUN_MONEND; ) {
    948 
    949 		/*
    950 		 * Make sure this segment is mapped.
    951 		 */
    952 		sme = get_segmap(va);
    953 		if (sme == SEGINV) {
    954 			va += NBSG;
    955 			continue;			/* next segment */
    956 		}
    957 
    958 		/*
    959 		 * Walk the pages of this segment.
    960 		 */
    961 		for(eva = va + NBSG; va < eva; va += PAGE_SIZE) {
    962 			pte = get_pte(va);
    963 
    964 			if ((pte & (PG_VALID | PG_TYPE)) ==
    965 				(PG_VALID | pgtype) &&
    966 			    PG_PFNUM(pte) == pf)
    967 			{
    968 				/*
    969 				 * Found the PROM mapping.
    970 				 * note: preserve page offset
    971 				 */
    972 				*vap = (va | ((vaddr_t)pa & PGOFSET));
    973 				restore_context(saved_ctx);
    974 				return 0;
    975 			}
    976 		}
    977 	}
    978 	restore_context(saved_ctx);
    979 	return ENOENT;
    980 }
    981 
    982 bool
    983 mm_md_direct_mapped_phys(paddr_t paddr, vaddr_t *vaddr)
    984 {
    985 
    986 	if (paddr >= avail_start)
    987 		return false;
    988 	*vaddr = paddr;
    989 	return true;
    990 }
    991 
    992 /*
    993  * Allow access to the PROM mapping similar to uvm_kernacc().
    994  */
    995 int
    996 mm_md_kernacc(void *ptr, vm_prot_t prot, bool *handled)
    997 {
    998 
    999 	if ((vaddr_t)ptr < SUN2_PROM_BASE || (vaddr_t)ptr > SUN2_MONEND) {
   1000 		*handled = false;
   1001 		return 0;
   1002 	}
   1003 
   1004 	*handled = true;
   1005 	/* Read in the PROM itself is OK, write not. */
   1006 	if ((prot & VM_PROT_WRITE) == 0)
   1007 		return 0;
   1008 	return EFAULT;
   1009 }
   1010