Home | History | Annotate | Line # | Download | only in luna68k
machdep.c revision 1.108
      1 /* $NetBSD: machdep.c,v 1.108 2021/10/09 20:00:41 tsutsui Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33 
     34 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.108 2021/10/09 20:00:41 tsutsui Exp $");
     35 
     36 #include "opt_ddb.h"
     37 #include "opt_kgdb.h"
     38 #include "opt_compat_sunos.h"
     39 #include "opt_modular.h"
     40 #include "opt_panicbutton.h"
     41 #include "opt_m68k_arch.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/proc.h>
     47 #include <sys/buf.h>
     48 #include <sys/reboot.h>
     49 #include <sys/conf.h>
     50 #include <sys/file.h>
     51 #include <sys/device.h>
     52 #include <sys/malloc.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/msgbuf.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/tty.h>
     57 #include <sys/mount.h>
     58 #include <sys/exec.h>
     59 #include <sys/exec_aout.h>		/* for MID_* */
     60 #include <sys/core.h>
     61 #include <sys/kauth.h>
     62 #include <sys/kcore.h>
     63 #include <sys/vnode.h>
     64 #include <sys/syscallargs.h>
     65 #include <sys/ksyms.h>
     66 #include <sys/module.h>
     67 #ifdef	KGDB
     68 #include <sys/kgdb.h>
     69 #endif
     70 #include <sys/boot_flag.h>
     71 #include <sys/exec_elf.h>
     72 #include <sys/cpu.h>
     73 
     74 #include <uvm/uvm_extern.h>
     75 
     76 #include <sys/sysctl.h>
     77 
     78 #include <machine/bootinfo.h>
     79 #include <machine/board.h>
     80 #include <machine/cpu.h>
     81 #include <machine/reg.h>
     82 #include <machine/pcb.h>
     83 #include <machine/psl.h>
     84 #include <machine/pte.h>
     85 #include <machine/kcore.h>	/* XXX should be pulled in by sys/kcore.h */
     86 
     87 #include <luna68k/dev/siottyvar.h>
     88 
     89 #include <dev/cons.h>
     90 #include <dev/mm.h>
     91 
     92 #if defined(DDB)
     93 #include <machine/db_machdep.h>
     94 #include <ddb/db_sym.h>
     95 #include <ddb/db_extern.h>
     96 #endif
     97 
     98 #include "ksyms.h"
     99 
    100 /*
    101  * Info for CTL_HW
    102  */
    103 char	machine[] = MACHINE;
    104 
    105 /* Our exported CPU info; we can have only one. */
    106 struct cpu_info cpu_info_store;
    107 
    108 struct vm_map *phys_map = NULL;
    109 
    110 int	maxmem;			/* max memory per process */
    111 
    112 extern	u_int lowram;
    113 
    114 void luna68k_init(void);
    115 void identifycpu(void);
    116 void dumpsys(void);
    117 
    118 void straytrap(int, u_short);
    119 void nmihand(struct frame);
    120 
    121 int  cpu_dumpsize(void);
    122 int  cpu_dump(int (*)(dev_t, daddr_t, void *, size_t), daddr_t *);
    123 void cpu_init_kcore_hdr(void);
    124 
    125 #if NKSYMS || defined(DDB) || defined(MODULAR)
    126 vsize_t symtab_size(vaddr_t);
    127 #endif
    128 extern char end[];
    129 extern void *esym;
    130 
    131 /*
    132  * Machine-independent crash dump header info.
    133  */
    134 cpu_kcore_hdr_t cpu_kcore_hdr;
    135 
    136 int	machtype;	/* model: 1 for LUNA-1, 2 for LUNA-2 */
    137 int	sysconsole;	/* console: 0 for ttya, 1 for video */
    138 
    139 extern void omfb_cnattach(void);
    140 extern void ws_cnattach(void);
    141 
    142 /*
    143  * On the 68020/68030, the value of delay_divisor is roughly
    144  * 2048 / cpuspeed (where cpuspeed is in MHz).
    145  *
    146  * On the 68040/68060(?), the value of delay_divisor is roughly
    147  * 759 / cpuspeed (where cpuspeed is in MHz).
    148  * XXX -- is the above formula correct?
    149  */
    150 int	cpuspeed = 25;		/* only used for printing later */
    151 int	delay_divisor = 30;	/* for delay() loop count */
    152 
    153 /*
    154  * Early initialization, before main() is called.
    155  */
    156 void
    157 luna68k_init(void)
    158 {
    159 	volatile uint8_t *pio0 = (void *)OBIO_PIO0_BASE;
    160 	int sw1, i;
    161 	char *cp;
    162 	extern char bootarg[64];
    163 
    164 	extern paddr_t avail_start, avail_end;
    165 
    166 	/* initialize cn_tab for early console */
    167 #if 1
    168 	cn_tab = &siottycons;
    169 #else
    170 	cn_tab = &romcons;
    171 #endif
    172 
    173 	/*
    174 	 * Tell the VM system about available physical memory.  The
    175 	 * luna68k only has one segment.
    176 	 */
    177 	uvm_page_physload(atop(avail_start), atop(avail_end),
    178 	    atop(avail_start), atop(avail_end), VM_FREELIST_DEFAULT);
    179 
    180 	/*
    181 	 * Initialize error message buffer (at end of core).
    182 	 * avail_end was pre-decremented in pmap_bootstrap to compensate.
    183 	 */
    184 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
    185 		pmap_kenter_pa((vaddr_t)msgbufaddr + i * PAGE_SIZE,
    186 		    avail_end + i * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, 0);
    187 	pmap_update(pmap_kernel());
    188 	initmsgbuf(msgbufaddr, m68k_round_page(MSGBUFSIZE));
    189 
    190 
    191 	pio0[3] = 0xb6;
    192 	pio0[2] = 1 << 6;		/* enable parity check */
    193 	pio0[3] = 0xb6;
    194 	sw1 = pio0[0];			/* dip sw1 value */
    195 	sw1 ^= 0xff;
    196 	sysconsole = !(sw1 & 0x2);	/* console selection */
    197 
    198 	/*
    199 	 * Check if boothowto and bootdev values are passed by our bootloader.
    200 	 */
    201 	if ((bootdev & B_MAGICMASK) == B_DEVMAGIC) {
    202 		/* Valid value is set; no need to parse bootarg. */
    203 		return;
    204 	}
    205 
    206 	/*
    207 	 * No valid bootdev value is set.
    208 	 * Assume we are booted by ROM monitor directly using a.out kernel
    209 	 * and we have to parse bootarg passed from the monitor to set
    210 	 * proper boothowto and check netboot.
    211 	 */
    212 
    213 	/* set default to "sd0a" with no howto flags */
    214 	bootdev = MAKEBOOTDEV(0, LUNA68K_BOOTADPT_SPC, 0, 0, 0);
    215 	boothowto = 0;
    216 
    217 	/*
    218 	 * 'bootarg' on LUNA has:
    219 	 *   "<args of x command> ENADDR=<addr> HOST=<host> SERVER=<name>"
    220 	 * where <addr> is MAC address of which network loader used (not
    221 	 * necessarily same as one at 0x4101.FFE0), <host> and <name>
    222 	 * are the values of HOST and SERVER environment variables.
    223 	 *
    224 	 * 'bootarg' on LUNA-II has "<args of x command>" only.
    225 	 *
    226 	 * NetBSD/luna68k cares only the first argment; any of "sda".
    227 	 */
    228 	bootarg[63] = '\0';
    229 	for (cp = bootarg; *cp != '\0'; cp++) {
    230 		if (*cp == '-') {
    231 			char c;
    232 			while ((c = *cp) != '\0' && c != ' ') {
    233 				BOOT_FLAG(c, boothowto);
    234 				cp++;
    235 			}
    236 		} else if (*cp == 'E' && memcmp("ENADDR=", cp, 7) == 0) {
    237 			bootdev =
    238 			    MAKEBOOTDEV(0, LUNA68K_BOOTADPT_LANCE, 0, 0, 0);
    239 		}
    240 	}
    241 }
    242 
    243 /*
    244  * Console initialization: called early on from main,
    245  */
    246 void
    247 consinit(void)
    248 {
    249 
    250 	if (sysconsole == 0) {
    251 		cn_tab = &siottycons;
    252 		(*cn_tab->cn_init)(cn_tab);
    253 	} else {
    254 		omfb_cnattach();
    255 		ws_cnattach();
    256 	}
    257 
    258 #if NKSYMS || defined(DDB) || defined(MODULAR)
    259 	ksyms_addsyms_elf((esym != NULL) ? 1 : 0, (void *)&end, esym);
    260 #endif
    261 #ifdef DDB
    262 	if (boothowto & RB_KDB)
    263 		cpu_Debugger();
    264 #endif
    265 }
    266 
    267 #if NKSYMS || defined(DDB) || defined(MODULAR)
    268 
    269 /*
    270  * Check and compute size of DDB symbols and strings.
    271  *
    272  * Note this function could be called from locore.s before MMU is turned on
    273  * so we should avoid global variables and function calls.
    274  */
    275 vsize_t
    276 symtab_size(vaddr_t hdr)
    277 {
    278 	int i;
    279 	Elf_Ehdr *ehdr;
    280 	Elf_Shdr *shp;
    281 	vaddr_t maxsym;
    282 
    283 	/*
    284 	 * Check the ELF headers.
    285 	 */
    286 
    287 	ehdr = (void *)hdr;
    288 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
    289 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
    290 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
    291 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
    292 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
    293 		return 0;
    294 	}
    295 
    296 	/*
    297 	 * Find the end of the symbols and strings.
    298 	 */
    299 
    300 	maxsym = 0;
    301 	shp = (Elf_Shdr *)(hdr + ehdr->e_shoff);
    302 	for (i = 0; i < ehdr->e_shnum; i++) {
    303 		if (shp[i].sh_type != SHT_SYMTAB &&
    304 		    shp[i].sh_type != SHT_STRTAB) {
    305 			continue;
    306 		}
    307 		maxsym = uimax(maxsym, shp[i].sh_offset + shp[i].sh_size);
    308 	}
    309 
    310 	return maxsym;
    311 }
    312 #endif /* NKSYMS || defined(DDB) || defined(MODULAR) */
    313 
    314 /*
    315  * cpu_startup: allocate memory for variable-sized tables.
    316  */
    317 void
    318 cpu_startup(void)
    319 {
    320 	vaddr_t minaddr, maxaddr;
    321 	char pbuf[9];
    322 
    323 	if (fputype != FPU_NONE)
    324 		m68k_make_fpu_idle_frame();
    325 
    326 	/*
    327 	 * Initialize the kernel crash dump header.
    328 	 */
    329 	cpu_init_kcore_hdr();
    330 
    331 	/*
    332 	 * Good {morning,afternoon,evening,night}.
    333 	 */
    334 	printf("%s%s", copyright, version);
    335 	identifycpu();
    336 
    337 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    338 	printf("total memory = %s\n", pbuf);
    339 
    340 	minaddr = 0;
    341 
    342 	/*
    343 	 * Allocate a submap for physio
    344 	 */
    345 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    346 	    VM_PHYS_SIZE, 0, false, NULL);
    347 
    348 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
    349 	printf("avail memory = %s\n", pbuf);
    350 }
    351 
    352 void
    353 identifycpu(void)
    354 {
    355 	extern int cputype;
    356 	const char *model, *fpu;
    357 
    358 	switch (cputype) {
    359 	case CPU_68030:
    360 		model ="LUNA-I";
    361 		switch (fputype) {
    362 		case FPU_68881:
    363 			fpu = "MC68881";
    364 			break;
    365 		case FPU_68882:
    366 			fpu = "MC68882";
    367 			break;
    368 		case FPU_NONE:
    369 			fpu = "no";
    370 			break;
    371 		default:
    372 			fpu = "unknown";
    373 			break;
    374 		}
    375 		cpu_setmodel("%s (MC68030 CPU+MMU, %s FPU)", model, fpu);
    376 		machtype = LUNA_I;
    377 		/* 20MHz 68030 */
    378 		cpuspeed = 20;
    379 		delay_divisor = 102;
    380 		hz = 60;
    381 		break;
    382 #if defined(M68040)
    383 	case CPU_68040:
    384 		model ="LUNA-II";
    385 		cpu_setmodel(
    386 		    "%s (MC68040 CPU+MMU+FPU, 4k on-chip physical I/D caches)",
    387 		    model);
    388 		machtype = LUNA_II;
    389 		/* 25MHz 68040 */
    390 		cpuspeed = 25;
    391 		delay_divisor = 30;
    392 		/* hz = 100 on LUNA-II */
    393 		break;
    394 #endif
    395 	default:
    396 		panic("unknown CPU type");
    397 	}
    398 	printf("%s\n", cpu_getmodel());
    399 }
    400 
    401 /*
    402  * machine dependent system variables.
    403  */
    404 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
    405 {
    406 
    407 	sysctl_createv(clog, 0, NULL, NULL,
    408 		       CTLFLAG_PERMANENT,
    409 		       CTLTYPE_NODE, "machdep", NULL,
    410 		       NULL, 0, NULL, 0,
    411 		       CTL_MACHDEP, CTL_EOL);
    412 
    413 	sysctl_createv(clog, 0, NULL, NULL,
    414 		       CTLFLAG_PERMANENT,
    415 		       CTLTYPE_STRUCT, "console_device", NULL,
    416 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    417 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
    418 }
    419 
    420 int	waittime = -1;
    421 
    422 void
    423 cpu_reboot(int howto, char *bootstr)
    424 {
    425 	struct pcb *pcb = lwp_getpcb(curlwp);
    426 	extern void doboot(void);
    427 
    428 	/* take a snap shot before clobbering any registers */
    429 	if (pcb != NULL)
    430 		savectx(pcb);
    431 
    432 	/* If system is hold, just halt. */
    433 	if (cold) {
    434 		howto |= RB_HALT;
    435 		goto haltsys;
    436 	}
    437 
    438 	boothowto = howto;
    439 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
    440 		waittime = 0;
    441 		vfs_shutdown();
    442 		/*
    443 		 * If we've been adjusting the clock, the todr
    444 		 * will be out of synch; adjust it now.
    445 		 */
    446 		resettodr();
    447 	}
    448 
    449 	/* Disable interrupts. */
    450 	splhigh();
    451 
    452 	/* If rebooting and a dump is requested, do it. */
    453 	if (howto & RB_DUMP)
    454 		dumpsys();
    455 
    456 haltsys:
    457 	/* Run any shutdown hooks. */
    458 	doshutdownhooks();
    459 
    460 	pmf_system_shutdown(boothowto);
    461 
    462 	/* Finally, halt/reboot the system. */
    463 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    464 		volatile uint8_t *pio = (void *)OBIO_PIO1_BASE;
    465 
    466 		printf("power is going down.\n");
    467 		DELAY(100000);
    468 		pio[3] = 0x94;
    469 		pio[2] = 0 << 4;
    470 		for (;;)
    471 			/* NOP */;
    472 	}
    473 	if (howto & RB_HALT) {
    474 		printf("System halted.	Hit any key to reboot.\n\n");
    475 		cnpollc(1);
    476 		(void)cngetc();
    477 		cnpollc(0);
    478 	}
    479 
    480 	printf("rebooting...\n");
    481 	DELAY(100000);
    482 	doboot();
    483 	/*NOTREACHED*/
    484 	for (;;)
    485 		;
    486 }
    487 
    488 /*
    489  * Initialize the kernel crash dump header.
    490  */
    491 void
    492 cpu_init_kcore_hdr(void)
    493 {
    494 	cpu_kcore_hdr_t *h = &cpu_kcore_hdr;
    495 	struct m68k_kcore_hdr *m = &h->un._m68k;
    496 
    497 	memset(&cpu_kcore_hdr, 0, sizeof(cpu_kcore_hdr));
    498 
    499 	/*
    500 	 * Initialize the `dispatcher' portion of the header.
    501 	 */
    502 	strcpy(h->name, machine);
    503 	h->page_size = PAGE_SIZE;
    504 	h->kernbase = KERNBASE;
    505 
    506 	/*
    507 	 * Fill in information about our MMU configuration.
    508 	 */
    509 	m->mmutype	= mmutype;
    510 	m->sg_v		= SG_V;
    511 	m->sg_frame	= SG_FRAME;
    512 	m->sg_ishift	= SG_ISHIFT;
    513 	m->sg_pmask	= SG_PMASK;
    514 	m->sg40_shift1	= SG4_SHIFT1;
    515 	m->sg40_mask2	= SG4_MASK2;
    516 	m->sg40_shift2	= SG4_SHIFT2;
    517 	m->sg40_mask3	= SG4_MASK3;
    518 	m->sg40_shift3	= SG4_SHIFT3;
    519 	m->sg40_addr1	= SG4_ADDR1;
    520 	m->sg40_addr2	= SG4_ADDR2;
    521 	m->pg_v		= PG_V;
    522 	m->pg_frame	= PG_FRAME;
    523 
    524 	/*
    525 	 * Initialize pointer to kernel segment table.
    526 	 */
    527 	m->sysseg_pa = (uint32_t)(pmap_kernel()->pm_stpa);
    528 
    529 	/*
    530 	 * Initialize relocation value such that:
    531 	 *
    532 	 *	pa = (va - KERNBASE) + reloc
    533 	 *
    534 	 * Since we're linked and loaded at the same place,
    535 	 * and the kernel is mapped va == pa, this is 0.
    536 	 */
    537 	m->reloc = 0;
    538 
    539 	/*
    540 	 * Define the end of the relocatable range.
    541 	 */
    542 	m->relocend = (uint32_t)end;
    543 
    544 	/*
    545 	 * The luna68k has one contiguous memory segment.
    546 	 */
    547 	m->ram_segs[0].start = 0 /* lowram */;
    548 	m->ram_segs[0].size  = ctob(physmem);
    549 }
    550 
    551 /*
    552  * Compute the size of the machine-dependent crash dump header.
    553  * Returns size in disk blocks.
    554  */
    555 
    556 #define CHDRSIZE (ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)))
    557 #define MDHDRSIZE roundup(CHDRSIZE, dbtob(1))
    558 
    559 int
    560 cpu_dumpsize(void)
    561 {
    562 
    563 	return btodb(MDHDRSIZE);
    564 }
    565 
    566 /*
    567  * Called by dumpsys() to dump the machine-dependent header.
    568  */
    569 int
    570 cpu_dump(int (*dump)(dev_t, daddr_t, void *, size_t), daddr_t *blknop)
    571 {
    572 	int buf[MDHDRSIZE / sizeof(int)];
    573 	cpu_kcore_hdr_t *chdr;
    574 	kcore_seg_t *kseg;
    575 	int error;
    576 
    577 	kseg = (kcore_seg_t *)buf;
    578 	chdr = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(kcore_seg_t)) /
    579 	    sizeof(int)];
    580 
    581 	/* Create the segment header. */
    582 	CORE_SETMAGIC(*kseg, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
    583 	kseg->c_size = MDHDRSIZE - ALIGN(sizeof(kcore_seg_t));
    584 
    585 	memcpy(chdr, &cpu_kcore_hdr, sizeof(cpu_kcore_hdr_t));
    586 	error = (*dump)(dumpdev, *blknop, (void *)buf, sizeof(buf));
    587 	*blknop += btodb(sizeof(buf));
    588 	return error;
    589 }
    590 
    591 /*
    592  * These variables are needed by /sbin/savecore
    593  */
    594 uint32_t dumpmag = 0x8fca0101;	/* magic number */
    595 int	dumpsize = 0;		/* pages */
    596 long	dumplo = 0;		/* blocks */
    597 
    598 /*
    599  * This is called by main to set dumplo and dumpsize.
    600  * Dumps always skip the first PAGE_SIZE of disk space
    601  * in case there might be a disk label stored there.
    602  * If there is extra space, put dump at the end to
    603  * reduce the chance that swapping trashes it.
    604  */
    605 void
    606 cpu_dumpconf(void)
    607 {
    608 	int chdrsize;	/* size of dump header */
    609 	int nblks;	/* size of dump area */
    610 
    611 	if (dumpdev == NODEV)
    612 		return;
    613 	nblks = bdev_size(dumpdev);
    614 	chdrsize = cpu_dumpsize();
    615 
    616 	dumpsize = btoc(cpu_kcore_hdr.un._m68k.ram_segs[0].size);
    617 
    618 	/*
    619 	 * Check do see if we will fit.  Note we always skip the
    620 	 * first PAGE_SIZE in case there is a disk label there.
    621 	 */
    622 	if (nblks < (ctod(dumpsize) + chdrsize + ctod(1))) {
    623 		dumpsize = 0;
    624 		dumplo = -1;
    625 		return;
    626 	}
    627 
    628 	/*
    629 	 * Put dump at the end of the partition.
    630 	 */
    631 	dumplo = (nblks - 1) - ctod(dumpsize) - chdrsize;
    632 }
    633 
    634 /*
    635  * Dump physical memory onto the dump device.  Called by cpu_reboot().
    636  */
    637 void
    638 dumpsys(void)
    639 {
    640 	const struct bdevsw *bdev;
    641 	daddr_t blkno;		/* current block to write */
    642 				/* dump routine */
    643 	int (*dump)(dev_t, daddr_t, void *, size_t);
    644 	int pg;			/* page being dumped */
    645 	paddr_t maddr;		/* PA being dumped */
    646 	int error;		/* error code from (*dump)() */
    647 
    648 	/* XXX initialized here because of gcc lossage */
    649 	maddr = 0 /* lowram */;
    650 	pg = 0;
    651 
    652 	/* Make sure dump device is valid. */
    653 	if (dumpdev == NODEV)
    654 		return;
    655 	bdev = bdevsw_lookup(dumpdev);
    656 	if (bdev == NULL)
    657 		return;
    658 	if (dumpsize == 0) {
    659 		cpu_dumpconf();
    660 		if (dumpsize == 0)
    661 			return;
    662 	}
    663 	if (dumplo <= 0) {
    664 		printf("\ndump to dev %u,%u  not possible\n",
    665 		    major(dumpdev), minor(dumpdev));
    666 		return;
    667 	}
    668 	dump = bdev->d_dump;
    669 	blkno = dumplo;
    670 
    671 	printf("\ndumping to dev %u,%u offset %ld\n",
    672 	    major(dumpdev), minor(dumpdev), dumplo);
    673 
    674 	printf("dump ");
    675 
    676 	/* Write the dump header. */
    677 	error = cpu_dump(dump, &blkno);
    678 	if (error)
    679 		goto bad;
    680 
    681 	for (pg = 0; pg < dumpsize; pg++) {
    682 #define NPGMB	(1024*1024/PAGE_SIZE)
    683 		/* print out how many MBs we have dumped */
    684 		if (pg && (pg % NPGMB) == 0)
    685 			printf("%d ", pg / NPGMB);
    686 #undef NPGMB
    687 		pmap_enter(pmap_kernel(), (vaddr_t)vmmap, maddr,
    688 		    VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    689 
    690 		pmap_update(pmap_kernel());
    691 		error = (*dump)(dumpdev, blkno, vmmap, PAGE_SIZE);
    692  bad:
    693 		switch (error) {
    694 		case 0:
    695 			maddr += PAGE_SIZE;
    696 			blkno += btodb(PAGE_SIZE);
    697 			break;
    698 
    699 		case ENXIO:
    700 			printf("device bad\n");
    701 			return;
    702 
    703 		case EFAULT:
    704 			printf("device not ready\n");
    705 			return;
    706 
    707 		case EINVAL:
    708 			printf("area improper\n");
    709 			return;
    710 
    711 		case EIO:
    712 			printf("i/o error\n");
    713 			return;
    714 
    715 		case EINTR:
    716 			printf("aborted from console\n");
    717 			return;
    718 
    719 		default:
    720 			printf("error %d\n", error);
    721 			return;
    722 		}
    723 	}
    724 	printf("succeeded\n");
    725 }
    726 
    727 void
    728 straytrap(int pc, u_short evec)
    729 {
    730 
    731 	printf("unexpected trap (vector offset %x) from %x\n",
    732 	    evec & 0xFFF, pc);
    733 }
    734 
    735 int	*nofault;
    736 
    737 int
    738 badaddr(register void *addr, int nbytes)
    739 {
    740 	int i;
    741 	label_t faultbuf;
    742 
    743 	nofault = (int *)&faultbuf;
    744 	if (setjmp((label_t *)nofault)) {
    745 		nofault = (int *)0;
    746 		return 1;
    747 	}
    748 
    749 	switch (nbytes) {
    750 	case 1:
    751 		i = *(volatile int8_t *)addr;
    752 		break;
    753 
    754 	case 2:
    755 		i = *(volatile int16_t *)addr;
    756 		break;
    757 
    758 	case 4:
    759 		i = *(volatile int32_t *)addr;
    760 		break;
    761 
    762 	default:
    763 		panic("badaddr: bad request");
    764 	}
    765 	__USE(i);
    766 	nofault = (int *)0;
    767 	return 0;
    768 }
    769 
    770 void luna68k_abort(const char *);
    771 
    772 static int innmihand;	/* simple mutex */
    773 
    774 /*
    775  * Level 7 interrupts are caused by e.g. the ABORT switch.
    776  *
    777  * If we have DDB, then break into DDB on ABORT.  In a production
    778  * environment, bumping the ABORT switch would be bad, so we enable
    779  * panic'ing on ABORT with the kernel option "PANICBUTTON".
    780  */
    781 void
    782 nmihand(struct frame frame)
    783 {
    784 
    785 	/* Prevent unwanted recursion */
    786 	if (innmihand)
    787 		return;
    788 	innmihand = 1;
    789 
    790 	luna68k_abort("ABORT SWITCH");
    791 
    792 	innmihand = 0;
    793 }
    794 
    795 /*
    796  * Common code for handling ABORT signals from buttons, switches,
    797  * serial lines, etc.
    798  */
    799 void
    800 luna68k_abort(const char *cp)
    801 {
    802 
    803 #ifdef DDB
    804 	printf("%s\n", cp);
    805 	cpu_Debugger();
    806 #else
    807 #ifdef PANICBUTTON
    808 	panic(cp);
    809 #else
    810 	printf("%s ignored\n", cp);
    811 #endif /* PANICBUTTON */
    812 #endif /* DDB */
    813 }
    814 
    815 /*
    816  * cpu_exec_aout_makecmds():
    817  *	CPU-dependent a.out format hook for execve().
    818  *
    819  * Determine of the given exec package refers to something which we
    820  * understand and, if so, set up the vmcmds for it.
    821  */
    822 int
    823 cpu_exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
    824 {
    825 	int error = ENOEXEC;
    826 #ifdef COMPAT_SUNOS
    827 	extern sunos_exec_aout_makecmds(struct proc *, struct exec_package *);
    828 
    829 	if ((error = sunos_exec_aout_makecmds(l->l_proc, epp)) == 0)
    830 		return 0;
    831 #endif
    832 	return error;
    833 }
    834 
    835 #ifdef MODULAR
    836 /*
    837  * Push any modules loaded by the bootloader etc.
    838  */
    839 void
    840 module_init_md(void)
    841 {
    842 }
    843 #endif
    844 
    845 #ifdef notyet
    846 /*
    847  * romcons is useful until m68k TC register is initialized.
    848  */
    849 int  romcngetc(dev_t);
    850 void romcnputc(dev_t, int);
    851 
    852 struct consdev romcons = {
    853 	NULL,
    854 	NULL,
    855 	romcngetc,
    856 	romcnputc,
    857 	nullcnpollc,
    858 	makedev(7, 0), /* XXX */
    859 	CN_DEAD,
    860 };
    861 
    862 #define __		((int **)PROM_ADDR)
    863 #define GETC()		(*(int (*)())__[6])()
    864 #define PUTC(x)		(*(void (*)())__[7])(x)
    865 
    866 #define ROMPUTC(x) \
    867 ({					\
    868 	register _r;			\
    869 	__asm volatile ("			\
    870 		movc	%%vbr,%0	; \
    871 		movel	%0,%%sp@-	; \
    872 		clrl	%0		; \
    873 		movc	%0,%%vbr"	\
    874 		: "=r" (_r));		\
    875 	PUTC(x);			\
    876 	__asm volatile ("			\
    877 		movel	%%sp@+,%0	; \
    878 		movc	%0,%%vbr"	\
    879 		: "=r" (_r));		\
    880 })
    881 
    882 #define ROMGETC() \
    883 ({					\
    884 	register _r, _c;		\
    885 	__asm volatile ("			\
    886 		movc	%%vbr,%0	; \
    887 		movel	%0,%%sp@-	; \
    888 		clrl	%0		; \
    889 		movc	%0,%%vbr"	\
    890 		: "=r" (_r));		\
    891 	_c = GETC();			\
    892 	__asm volatile ("			\
    893 		movel	%%sp@+,%0	; \
    894 		movc	%0,%%vbr"	\
    895 		: "=r" (_r));		\
    896 	_c;				\
    897 })
    898 
    899 void
    900 romcnputc(dev_t dev, int c)
    901 {
    902 	int s;
    903 
    904 	s = splhigh();
    905 	ROMPUTC(c);
    906 	splx(s);
    907 }
    908 
    909 int
    910 romcngetc(dev_t dev)
    911 {
    912 	int s, c;
    913 
    914 	do {
    915 		s = splhigh();
    916 		c = ROMGETC();
    917 		splx(s);
    918 	} while (c == -1);
    919 	return c;
    920 }
    921 #endif
    922 
    923 int
    924 mm_md_physacc(paddr_t pa, vm_prot_t prot)
    925 {
    926 
    927 	return (pa < lowram || pa >= 0xfffffffc) ? EFAULT : 0;
    928 }
    929