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