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