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