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