Home | History | Annotate | Line # | Download | only in luna68k
machdep.c revision 1.98
      1 /* $NetBSD: machdep.c,v 1.98 2014/03/24 19:46:24 christos 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.98 2014/03/24 19:46:24 christos 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 #define ELFSIZE 32
     72 #include <sys/exec_elf.h>
     73 #include <sys/cpu.h>
     74 
     75 #include <uvm/uvm_extern.h>
     76 
     77 #include <sys/sysctl.h>
     78 
     79 #include <machine/bootinfo.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 <dev/cons.h>
     88 #include <dev/mm.h>
     89 
     90 #if defined(DDB)
     91 #include <machine/db_machdep.h>
     92 #include <ddb/db_sym.h>
     93 #include <ddb/db_extern.h>
     94 #endif
     95 
     96 #include "ksyms.h"
     97 
     98 /*
     99  * Info for CTL_HW
    100  */
    101 char	machine[] = MACHINE;
    102 
    103 /* Our exported CPU info; we can have only one. */
    104 struct cpu_info cpu_info_store;
    105 
    106 struct vm_map *phys_map = NULL;
    107 
    108 int	maxmem;			/* max memory per process */
    109 
    110 extern	u_int lowram;
    111 
    112 void luna68k_init(void);
    113 void identifycpu(void);
    114 void dumpsys(void);
    115 
    116 void straytrap(int, u_short);
    117 void nmihand(struct frame);
    118 
    119 int  cpu_dumpsize(void);
    120 int  cpu_dump(int (*)(dev_t, daddr_t, void *, size_t), daddr_t *);
    121 void cpu_init_kcore_hdr(void);
    122 
    123 #if NKSYMS || defined(DDB) || defined(MODULAR)
    124 vsize_t symtab_size(vaddr_t);
    125 #endif
    126 extern char end[];
    127 extern void *esym;
    128 
    129 /*
    130  * Machine-independent crash dump header info.
    131  */
    132 cpu_kcore_hdr_t cpu_kcore_hdr;
    133 
    134 int	machtype;	/* model: 1 for LUNA-1, 2 for LUNA-2 */
    135 int	sysconsole;	/* console: 0 for ttya, 1 for video */
    136 
    137 extern struct consdev syscons;
    138 extern void omfb_cnattach(void);
    139 extern void ws_cnattach(void);
    140 extern void syscnattach(int);
    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 *)0x49000000;
    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 = &syscons;
    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 		syscnattach(0);
    252 	else {
    253 		omfb_cnattach();
    254 		ws_cnattach();
    255 	}
    256 
    257 #if NKSYMS || defined(DDB) || defined(MODULAR)
    258 	ksyms_addsyms_elf((esym != NULL) ? 1 : 0, (void *)&end, esym);
    259 #endif
    260 #ifdef DDB
    261 	if (boothowto & RB_KDB)
    262 		cpu_Debugger();
    263 #endif
    264 }
    265 
    266 #if NKSYMS || defined(DDB) || defined(MODULAR)
    267 
    268 /*
    269  * Check and compute size of DDB symbols and strings.
    270  *
    271  * Note this function could be called from locore.s before MMU is turned on
    272  * so we should avoid global variables and function calls.
    273  */
    274 vsize_t
    275 symtab_size(vaddr_t hdr)
    276 {
    277 	int i;
    278 	Elf_Ehdr *ehdr;
    279 	Elf_Shdr *shp;
    280 	vaddr_t maxsym;
    281 
    282 	/*
    283 	 * Check the ELF headers.
    284 	 */
    285 
    286 	ehdr = (void *)hdr;
    287 	if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
    288 	    ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
    289 	    ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
    290 	    ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
    291 	    ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
    292 		return 0;
    293 	}
    294 
    295 	/*
    296 	 * Find the end of the symbols and strings.
    297 	 */
    298 
    299 	maxsym = 0;
    300 	shp = (Elf_Shdr *)(hdr + ehdr->e_shoff);
    301 	for (i = 0; i < ehdr->e_shnum; i++) {
    302 		if (shp[i].sh_type != SHT_SYMTAB &&
    303 		    shp[i].sh_type != SHT_STRTAB) {
    304 			continue;
    305 		}
    306 		maxsym = max(maxsym, shp[i].sh_offset + shp[i].sh_size);
    307 	}
    308 
    309 	return maxsym;
    310 }
    311 #endif /* NKSYMS || defined(DDB) || defined(MODULAR) */
    312 
    313 /*
    314  * cpu_startup: allocate memory for variable-sized tables.
    315  */
    316 void
    317 cpu_startup(void)
    318 {
    319 	vaddr_t minaddr, maxaddr;
    320 	char pbuf[9];
    321 	extern void greeting(void);
    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(uvmexp.free));
    349 	printf("avail memory = %s\n", pbuf);
    350 
    351 	/*
    352 	 * Say "Hi" to the world
    353 	 */
    354 	greeting();
    355 }
    356 
    357 void
    358 identifycpu(void)
    359 {
    360 	extern int cputype;
    361 	const char *model, *fpu;
    362 
    363 	switch (cputype) {
    364 	case CPU_68030:
    365 		model ="LUNA-I";
    366 		switch (fputype) {
    367 		case FPU_68881:
    368 			fpu = "MC68881";
    369 			break;
    370 		case FPU_68882:
    371 			fpu = "MC68882";
    372 			break;
    373 		case FPU_NONE:
    374 			fpu = "no";
    375 			break;
    376 		default:
    377 			fpu = "unknown";
    378 			break;
    379 		}
    380 		cpu_setmodel("%s (MC68030 CPU+MMU, %s FPU)", model, fpu);
    381 		machtype = LUNA_I;
    382 		/* 20MHz 68030 */
    383 		cpuspeed = 20;
    384 		delay_divisor = 102;
    385 		hz = 60;
    386 		break;
    387 #if defined(M68040)
    388 	case CPU_68040:
    389 		model ="LUNA-II";
    390 		cpu_setmodel(
    391 		    "%s (MC68040 CPU+MMU+FPU, 4k on-chip physical I/D caches)",
    392 		    model);
    393 		machtype = LUNA_II;
    394 		/* 25MHz 68040 */
    395 		cpuspeed = 25;
    396 		delay_divisor = 30;
    397 		/* hz = 100 on LUNA-II */
    398 		break;
    399 #endif
    400 	default:
    401 		panic("unknown CPU type");
    402 	}
    403 	printf("%s\n", cpu_getmodel());
    404 }
    405 
    406 /*
    407  * machine dependent system variables.
    408  */
    409 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
    410 {
    411 
    412 	sysctl_createv(clog, 0, NULL, NULL,
    413 		       CTLFLAG_PERMANENT,
    414 		       CTLTYPE_NODE, "machdep", NULL,
    415 		       NULL, 0, NULL, 0,
    416 		       CTL_MACHDEP, CTL_EOL);
    417 
    418 	sysctl_createv(clog, 0, NULL, NULL,
    419 		       CTLFLAG_PERMANENT,
    420 		       CTLTYPE_STRUCT, "console_device", NULL,
    421 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    422 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
    423 }
    424 
    425 int	waittime = -1;
    426 
    427 void
    428 cpu_reboot(int howto, char *bootstr)
    429 {
    430 	struct pcb *pcb = lwp_getpcb(curlwp);
    431 	extern void doboot(void);
    432 
    433 	/* take a snap shot before clobbering any registers */
    434 	if (pcb != NULL)
    435 		savectx(pcb);
    436 
    437 	/* If system is hold, just halt. */
    438 	if (cold) {
    439 		howto |= RB_HALT;
    440 		goto haltsys;
    441 	}
    442 
    443 	boothowto = howto;
    444 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
    445 		waittime = 0;
    446 		vfs_shutdown();
    447 		/*
    448 		 * If we've been adjusting the clock, the todr
    449 		 * will be out of synch; adjust it now.
    450 		 */
    451 		resettodr();
    452 	}
    453 
    454 	/* Disable interrupts. */
    455 	splhigh();
    456 
    457 	/* If rebooting and a dump is requested, do it. */
    458 	if (howto & RB_DUMP)
    459 		dumpsys();
    460 
    461 haltsys:
    462 	/* Run any shutdown hooks. */
    463 	doshutdownhooks();
    464 
    465 	pmf_system_shutdown(boothowto);
    466 
    467 	/* Finally, halt/reboot the system. */
    468 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    469 		volatile uint8_t *pio = (void *)0x4d000000;
    470 
    471 		printf("power is going down.\n");
    472 		DELAY(100000);
    473 		pio[3] = 0x94;
    474 		pio[2] = 0 << 4;
    475 		for (;;)
    476 			/* NOP */;
    477 	}
    478 	if (howto & RB_HALT) {
    479 		printf("System halted.	Hit any key to reboot.\n\n");
    480 		(void)cngetc();
    481 	}
    482 
    483 	printf("rebooting...\n");
    484 	DELAY(100000);
    485 	doboot();
    486 	/*NOTREACHED*/
    487 	for (;;)
    488 		;
    489 }
    490 
    491 /*
    492  * Initialize the kernel crash dump header.
    493  */
    494 void
    495 cpu_init_kcore_hdr(void)
    496 {
    497 	cpu_kcore_hdr_t *h = &cpu_kcore_hdr;
    498 	struct m68k_kcore_hdr *m = &h->un._m68k;
    499 
    500 	memset(&cpu_kcore_hdr, 0, sizeof(cpu_kcore_hdr));
    501 
    502 	/*
    503 	 * Initialize the `dispatcher' portion of the header.
    504 	 */
    505 	strcpy(h->name, machine);
    506 	h->page_size = PAGE_SIZE;
    507 	h->kernbase = KERNBASE;
    508 
    509 	/*
    510 	 * Fill in information about our MMU configuration.
    511 	 */
    512 	m->mmutype	= mmutype;
    513 	m->sg_v		= SG_V;
    514 	m->sg_frame	= SG_FRAME;
    515 	m->sg_ishift	= SG_ISHIFT;
    516 	m->sg_pmask	= SG_PMASK;
    517 	m->sg40_shift1	= SG4_SHIFT1;
    518 	m->sg40_mask2	= SG4_MASK2;
    519 	m->sg40_shift2	= SG4_SHIFT2;
    520 	m->sg40_mask3	= SG4_MASK3;
    521 	m->sg40_shift3	= SG4_SHIFT3;
    522 	m->sg40_addr1	= SG4_ADDR1;
    523 	m->sg40_addr2	= SG4_ADDR2;
    524 	m->pg_v		= PG_V;
    525 	m->pg_frame	= PG_FRAME;
    526 
    527 	/*
    528 	 * Initialize pointer to kernel segment table.
    529 	 */
    530 	m->sysseg_pa = (uint32_t)(pmap_kernel()->pm_stpa);
    531 
    532 	/*
    533 	 * Initialize relocation value such that:
    534 	 *
    535 	 *	pa = (va - KERNBASE) + reloc
    536 	 *
    537 	 * Since we're linked and loaded at the same place,
    538 	 * and the kernel is mapped va == pa, this is 0.
    539 	 */
    540 	m->reloc = 0;
    541 
    542 	/*
    543 	 * Define the end of the relocatable range.
    544 	 */
    545 	m->relocend = (uint32_t)end;
    546 
    547 	/*
    548 	 * The luna68k has one contiguous memory segment.
    549 	 */
    550 	m->ram_segs[0].start = 0 /* lowram */;
    551 	m->ram_segs[0].size  = ctob(physmem);
    552 }
    553 
    554 /*
    555  * Compute the size of the machine-dependent crash dump header.
    556  * Returns size in disk blocks.
    557  */
    558 
    559 #define CHDRSIZE (ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)))
    560 #define MDHDRSIZE roundup(CHDRSIZE, dbtob(1))
    561 
    562 int
    563 cpu_dumpsize(void)
    564 {
    565 
    566 	return btodb(MDHDRSIZE);
    567 }
    568 
    569 /*
    570  * Called by dumpsys() to dump the machine-dependent header.
    571  */
    572 int
    573 cpu_dump(int (*dump)(dev_t, daddr_t, void *, size_t), daddr_t *blknop)
    574 {
    575 	int buf[MDHDRSIZE / sizeof(int)];
    576 	cpu_kcore_hdr_t *chdr;
    577 	kcore_seg_t *kseg;
    578 	int error;
    579 
    580 	kseg = (kcore_seg_t *)buf;
    581 	chdr = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(kcore_seg_t)) /
    582 	    sizeof(int)];
    583 
    584 	/* Create the segment header. */
    585 	CORE_SETMAGIC(*kseg, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
    586 	kseg->c_size = MDHDRSIZE - ALIGN(sizeof(kcore_seg_t));
    587 
    588 	memcpy(chdr, &cpu_kcore_hdr, sizeof(cpu_kcore_hdr_t));
    589 	error = (*dump)(dumpdev, *blknop, (void *)buf, sizeof(buf));
    590 	*blknop += btodb(sizeof(buf));
    591 	return error;
    592 }
    593 
    594 /*
    595  * These variables are needed by /sbin/savecore
    596  */
    597 uint32_t dumpmag = 0x8fca0101;	/* magic number */
    598 int	dumpsize = 0;		/* pages */
    599 long	dumplo = 0;		/* blocks */
    600 
    601 /*
    602  * This is called by main to set dumplo and dumpsize.
    603  * Dumps always skip the first PAGE_SIZE of disk space
    604  * in case there might be a disk label stored there.
    605  * If there is extra space, put dump at the end to
    606  * reduce the chance that swapping trashes it.
    607  */
    608 void
    609 cpu_dumpconf(void)
    610 {
    611 	int chdrsize;	/* size of dump header */
    612 	int nblks;	/* size of dump area */
    613 
    614 	if (dumpdev == NODEV)
    615 		return;
    616 	nblks = bdev_size(dumpdev);
    617 	chdrsize = cpu_dumpsize();
    618 
    619 	dumpsize = btoc(cpu_kcore_hdr.un._m68k.ram_segs[0].size);
    620 
    621 	/*
    622 	 * Check do see if we will fit.  Note we always skip the
    623 	 * first PAGE_SIZE in case there is a disk label there.
    624 	 */
    625 	if (nblks < (ctod(dumpsize) + chdrsize + ctod(1))) {
    626 		dumpsize = 0;
    627 		dumplo = -1;
    628 		return;
    629 	}
    630 
    631 	/*
    632 	 * Put dump at the end of the partition.
    633 	 */
    634 	dumplo = (nblks - 1) - ctod(dumpsize) - chdrsize;
    635 }
    636 
    637 /*
    638  * Dump physical memory onto the dump device.  Called by cpu_reboot().
    639  */
    640 void
    641 dumpsys(void)
    642 {
    643 	const struct bdevsw *bdev;
    644 	daddr_t blkno;		/* current block to write */
    645 				/* dump routine */
    646 	int (*dump)(dev_t, daddr_t, void *, size_t);
    647 	int pg;			/* page being dumped */
    648 	paddr_t maddr;		/* PA being dumped */
    649 	int error;		/* error code from (*dump)() */
    650 
    651 	/* XXX initialized here because of gcc lossage */
    652 	maddr = 0 /* lowram */;
    653 	pg = 0;
    654 
    655 	/* Make sure dump device is valid. */
    656 	if (dumpdev == NODEV)
    657 		return;
    658 	bdev = bdevsw_lookup(dumpdev);
    659 	if (bdev == NULL)
    660 		return;
    661 	if (dumpsize == 0) {
    662 		cpu_dumpconf();
    663 		if (dumpsize == 0)
    664 			return;
    665 	}
    666 	if (dumplo <= 0) {
    667 		printf("\ndump to dev %u,%u  not possible\n",
    668 		    major(dumpdev), minor(dumpdev));
    669 		return;
    670 	}
    671 	dump = bdev->d_dump;
    672 	blkno = dumplo;
    673 
    674 	printf("\ndumping to dev %u,%u offset %ld\n",
    675 	    major(dumpdev), minor(dumpdev), dumplo);
    676 
    677 	printf("dump ");
    678 
    679 	/* Write the dump header. */
    680 	error = cpu_dump(dump, &blkno);
    681 	if (error)
    682 		goto bad;
    683 
    684 	for (pg = 0; pg < dumpsize; pg++) {
    685 #define NPGMB	(1024*1024/PAGE_SIZE)
    686 		/* print out how many MBs we have dumped */
    687 		if (pg && (pg % NPGMB) == 0)
    688 			printf("%d ", pg / NPGMB);
    689 #undef NPGMB
    690 		pmap_enter(pmap_kernel(), (vaddr_t)vmmap, maddr,
    691 		    VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    692 
    693 		pmap_update(pmap_kernel());
    694 		error = (*dump)(dumpdev, blkno, vmmap, PAGE_SIZE);
    695  bad:
    696 		switch (error) {
    697 		case 0:
    698 			maddr += PAGE_SIZE;
    699 			blkno += btodb(PAGE_SIZE);
    700 			break;
    701 
    702 		case ENXIO:
    703 			printf("device bad\n");
    704 			return;
    705 
    706 		case EFAULT:
    707 			printf("device not ready\n");
    708 			return;
    709 
    710 		case EINVAL:
    711 			printf("area improper\n");
    712 			return;
    713 
    714 		case EIO:
    715 			printf("i/o error\n");
    716 			return;
    717 
    718 		case EINTR:
    719 			printf("aborted from console\n");
    720 			return;
    721 
    722 		default:
    723 			printf("error %d\n", error);
    724 			return;
    725 		}
    726 	}
    727 	printf("succeeded\n");
    728 }
    729 
    730 void
    731 straytrap(int pc, u_short evec)
    732 {
    733 
    734 	printf("unexpected trap (vector offset %x) from %x\n",
    735 	    evec & 0xFFF, pc);
    736 }
    737 
    738 int	*nofault;
    739 
    740 int
    741 badaddr(register void *addr, int nbytes)
    742 {
    743 	int i;
    744 	label_t faultbuf;
    745 
    746 	nofault = (int *)&faultbuf;
    747 	if (setjmp((label_t *)nofault)) {
    748 		nofault = (int *)0;
    749 		return 1;
    750 	}
    751 
    752 	switch (nbytes) {
    753 	case 1:
    754 		i = *(volatile int8_t *)addr;
    755 		break;
    756 
    757 	case 2:
    758 		i = *(volatile int16_t *)addr;
    759 		break;
    760 
    761 	case 4:
    762 		i = *(volatile int32_t *)addr;
    763 		break;
    764 
    765 	default:
    766 		panic("badaddr: bad request");
    767 	}
    768 	__USE(i);
    769 	nofault = (int *)0;
    770 	return 0;
    771 }
    772 
    773 void luna68k_abort(const char *);
    774 
    775 static int innmihand;	/* simple mutex */
    776 
    777 /*
    778  * Level 7 interrupts are caused by e.g. the ABORT switch.
    779  *
    780  * If we have DDB, then break into DDB on ABORT.  In a production
    781  * environment, bumping the ABORT switch would be bad, so we enable
    782  * panic'ing on ABORT with the kernel option "PANICBUTTON".
    783  */
    784 void
    785 nmihand(struct frame frame)
    786 {
    787 
    788 	/* Prevent unwanted recursion */
    789 	if (innmihand)
    790 		return;
    791 	innmihand = 1;
    792 
    793 	luna68k_abort("ABORT SWITCH");
    794 
    795 	innmihand = 0;
    796 }
    797 
    798 /*
    799  * Common code for handling ABORT signals from buttons, switches,
    800  * serial lines, etc.
    801  */
    802 void
    803 luna68k_abort(const char *cp)
    804 {
    805 
    806 #ifdef DDB
    807 	printf("%s\n", cp);
    808 	cpu_Debugger();
    809 #else
    810 #ifdef PANICBUTTON
    811 	panic(cp);
    812 #else
    813 	printf("%s ignored\n", cp);
    814 #endif /* PANICBUTTON */
    815 #endif /* DDB */
    816 }
    817 
    818 /*
    819  * cpu_exec_aout_makecmds():
    820  *	CPU-dependent a.out format hook for execve().
    821  *
    822  * Determine of the given exec package refers to something which we
    823  * understand and, if so, set up the vmcmds for it.
    824  */
    825 int
    826 cpu_exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
    827 {
    828 	int error = ENOEXEC;
    829 #ifdef COMPAT_SUNOS
    830 	extern sunos_exec_aout_makecmds(struct proc *, struct exec_package *);
    831 
    832 	if ((error = sunos_exec_aout_makecmds(l->l_proc, epp)) == 0)
    833 		return 0;
    834 #endif
    835 	return error;
    836 }
    837 
    838 #ifdef MODULAR
    839 /*
    840  * Push any modules loaded by the bootloader etc.
    841  */
    842 void
    843 module_init_md(void)
    844 {
    845 }
    846 #endif
    847 
    848 #ifdef notyet
    849 /*
    850  * romcons is useful until m68k TC register is initialized.
    851  */
    852 int  romcngetc(dev_t);
    853 void romcnputc(dev_t, int);
    854 
    855 struct consdev romcons = {
    856 	NULL,
    857 	NULL,
    858 	romcngetc,
    859 	romcnputc,
    860 	nullcnpollc,
    861 	makedev(7, 0), /* XXX */
    862 	CN_DEAD,
    863 };
    864 
    865 #define __		((int **)0x41000000)
    866 #define GETC()		(*(int (*)())__[6])()
    867 #define PUTC(x)		(*(void (*)())__[7])(x)
    868 
    869 #define ROMPUTC(x) \
    870 ({					\
    871 	register _r;			\
    872 	__asm volatile ("			\
    873 		movc	%%vbr,%0	; \
    874 		movel	%0,%%sp@-	; \
    875 		clrl	%0		; \
    876 		movc	%0,%%vbr"	\
    877 		: "=r" (_r));		\
    878 	PUTC(x);			\
    879 	__asm volatile ("			\
    880 		movel	%%sp@+,%0	; \
    881 		movc	%0,%%vbr"	\
    882 		: "=r" (_r));		\
    883 })
    884 
    885 #define ROMGETC() \
    886 ({					\
    887 	register _r, _c;		\
    888 	__asm volatile ("			\
    889 		movc	%%vbr,%0	; \
    890 		movel	%0,%%sp@-	; \
    891 		clrl	%0		; \
    892 		movc	%0,%%vbr"	\
    893 		: "=r" (_r));		\
    894 	_c = GETC();			\
    895 	__asm volatile ("			\
    896 		movel	%%sp@+,%0	; \
    897 		movc	%0,%%vbr"	\
    898 		: "=r" (_r));		\
    899 	_c;				\
    900 })
    901 
    902 void
    903 romcnputc(dev_t dev, int c)
    904 {
    905 	int s;
    906 
    907 	s = splhigh();
    908 	ROMPUTC(c);
    909 	splx(s);
    910 }
    911 
    912 int
    913 romcngetc(dev_t dev)
    914 {
    915 	int s, c;
    916 
    917 	do {
    918 		s = splhigh();
    919 		c = ROMGETC();
    920 		splx(s);
    921 	} while (c == -1);
    922 	return c;
    923 }
    924 #endif
    925 
    926 int
    927 mm_md_physacc(paddr_t pa, vm_prot_t prot)
    928 {
    929 
    930 	return (pa < lowram || pa >= 0xfffffffc) ? EFAULT : 0;
    931 }
    932