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