Home | History | Annotate | Line # | Download | only in luna68k
machdep.c revision 1.22
      1 /* $NetBSD: machdep.c,v 1.22 2002/08/25 20:21:38 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.22 2002/08/25 20:21:38 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/map.h>
     50 #include <sys/proc.h>
     51 #include <sys/buf.h>
     52 #include <sys/reboot.h>
     53 #include <sys/conf.h>
     54 #include <sys/file.h>
     55 #include <sys/clist.h>
     56 #include <sys/device.h>
     57 #include <sys/malloc.h>
     58 #include <sys/mbuf.h>
     59 #include <sys/msgbuf.h>
     60 #include <sys/ioctl.h>
     61 #include <sys/tty.h>
     62 #include <sys/mount.h>
     63 #include <sys/user.h>
     64 #include <sys/exec.h>
     65 #include <sys/core.h>
     66 #include <sys/kcore.h>
     67 #include <sys/vnode.h>
     68 #include <sys/syscallargs.h>
     69 #ifdef	KGDB
     70 #include <sys/kgdb.h>
     71 #endif
     72 #include <sys/boot_flag.h>
     73 
     74 #include <uvm/uvm_extern.h>
     75 
     76 #include <sys/sysctl.h>
     77 
     78 #include <machine/cpu.h>
     79 #include <machine/reg.h>
     80 #include <machine/psl.h>
     81 #include <machine/pte.h>
     82 #include <machine/kcore.h>	/* XXX should be pulled in by sys/kcore.h */
     83 
     84 #include <dev/cons.h>
     85 
     86 #if defined(DDB)
     87 #include <machine/db_machdep.h>
     88 #include <ddb/db_sym.h>
     89 #include <ddb/db_extern.h>
     90 #endif
     91 
     92 /*
     93  * Info for CTL_HW
     94  */
     95 char	machine[] = MACHINE;
     96 char	cpu_model[60];
     97 
     98 /* Our exported CPU info; we can have only one. */
     99 struct cpu_info cpu_info_store;
    100 
    101 extern char kernel_text[];
    102 extern char etext[];
    103 
    104 struct vm_map *exec_map = NULL;
    105 struct vm_map *mb_map = NULL;
    106 struct vm_map *phys_map = NULL;
    107 
    108 caddr_t	msgbufaddr;
    109 int	maxmem;			/* max memory per process */
    110 int	physmem;		/* set by locore */
    111 /*
    112  * safepri is a safe priority for sleep to set for a spin-wait
    113  * during autoconfiguration or after a panic.
    114  */
    115 int	safepri = PSL_LOWIPL;
    116 
    117 void luna68k_init __P((void));
    118 void identifycpu __P((void));
    119 void dumpsys __P((void));
    120 
    121 void straytrap __P((int, u_short));
    122 void nmihand __P((struct frame));
    123 
    124 int  cpu_dumpsize __P((void));
    125 int  cpu_dump __P((int (*)(dev_t, daddr_t, caddr_t, size_t), daddr_t *));
    126 void cpu_init_kcore_hdr __P((void));
    127 
    128 /*
    129  * Machine-independent crash dump header info.
    130  */
    131 cpu_kcore_hdr_t cpu_kcore_hdr;
    132 
    133 int	machtype;	/* model: 1 for LUNA-1, 2 for LUNA-2 */
    134 int	sysconsole;	/* console: 0 for ttya, 1 for video */
    135 
    136 extern struct consdev syscons;
    137 extern void omfb_cnattach __P((void));
    138 extern void ws_cnattach __P((void));
    139 extern void syscnattach __P((int));
    140 
    141 /*
    142  * On the 68020/68030, the value of delay_divisor is roughly
    143  * 2048 / cpuspeed (where cpuspeed is in MHz).
    144  *
    145  * On the 68040/68060(?), the value of delay_divisor is roughly
    146  * 759 / cpuspeed (where cpuspeed is in MHz).
    147  * XXX -- is the above formula correct?
    148  */
    149 int	cpuspeed = 25;		/* only used for printing later */
    150 int	delay_divisor = 300;	/* for delay() loop count */
    151 
    152 /*
    153  * Early initialization, before main() is called.
    154  */
    155 void
    156 luna68k_init()
    157 {
    158 	volatile unsigned char *pio0 = (void *)0x49000000;
    159 	int sw1, i;
    160 	char *cp;
    161 	extern char bootarg[64];
    162 
    163 	extern paddr_t avail_start, avail_end;
    164 
    165 	/*
    166 	 * Tell the VM system about available physical memory.  The
    167 	 * luna68k only has one segment.
    168 	 */
    169 	uvm_page_physload(atop(avail_start), atop(avail_end),
    170 	    atop(avail_start), atop(avail_end), VM_FREELIST_DEFAULT);
    171 
    172 	/*
    173 	 * Initialize error message buffer (at end of core).
    174 	 * avail_end was pre-decremented in pmap_bootstrap to compensate.
    175 	 */
    176 	for (i = 0; i < btoc(MSGBUFSIZE); i++)
    177 		pmap_enter(pmap_kernel(), (vaddr_t)msgbufaddr + i * NBPG,
    178 		    avail_end + i * NBPG, VM_PROT_READ|VM_PROT_WRITE,
    179 		    VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
    180 	pmap_update(pmap_kernel());
    181 	initmsgbuf(msgbufaddr, m68k_round_page(MSGBUFSIZE));
    182 
    183 
    184 	pio0[3] = 0xb6;
    185 	pio0[2] = 1 << 6;		/* enable parity check */
    186 	pio0[3] = 0xb6;
    187 	sw1 = pio0[0];			/* dipssw1 value */
    188 	sw1 ^= 0xff;
    189 	sysconsole = !(sw1 & 0x2);	/* console selection */
    190 
    191 	boothowto = 0;
    192 	i = 0;
    193 	/*
    194 	 * 'bootarg' has;
    195 	 *   "<args of x command> ENADDR=<addr> HOST=<host> SERVER=<name>"
    196 	 * where <addr> is MAC address of which network loader used (not
    197 	 * necessarily same as one at 0x4101.FFE0), <host> and <name>
    198 	 * are the values of HOST and SERVER environment variables,
    199 	 *
    200 	 * NetBSD/luna68k cares only the first argment; any of "sda".
    201 	 */
    202 	for (cp = bootarg; *cp != ' '; cp++) {
    203 		BOOT_FLAG(*cp, boothowto);
    204 		if (i++ >= sizeof(bootarg))
    205 			break;
    206 	}
    207 #if 0 /* overload 1:sw1, which now means 'go ROM monitor' after poweron */
    208 	if (boothowto == 0)
    209 		boothowto = (sw1 & 0x1) ? RB_SINGLE : 0;
    210 #endif
    211 }
    212 
    213 /*
    214  * Console initialization: called early on from main,
    215  */
    216 void
    217 consinit()
    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 	caddr_t v;
    245 	u_int i, base, residual;
    246 	vaddr_t minaddr, maxaddr;
    247 	vsize_t size;
    248 	char pbuf[9];
    249 	extern void greeting __P((void));
    250 
    251 	/*
    252 	 * Initialize the kernel crash dump header.
    253 	 */
    254 	cpu_init_kcore_hdr();
    255 
    256 	/*
    257 	 * Good {morning,afternoon,evening,night}.
    258 	 */
    259 	printf(version);
    260 	identifycpu();
    261 
    262 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    263 	printf("total memory = %s\n", pbuf);
    264 
    265 	/*
    266 	 * Find out how much space we need, allocate it,
    267 	 * and then give everything true virtual addresses.
    268 	 */
    269 	size = (int)allocsys(NULL, NULL);
    270 	if ((v = (caddr_t)uvm_km_alloc(kernel_map, round_page(size))) == 0)
    271 		panic("startup: no room for tables");
    272 	if (allocsys(v, NULL) - v != size)
    273 		panic("startup: table size inconsistency");
    274 
    275 	/*
    276 	 * Now allocate buffers proper.  They are different than the above
    277 	 * in that they usually occupy more virtual memory than physical.
    278 	 */
    279 	size = MAXBSIZE * nbuf;
    280 	if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
    281 		    NULL, UVM_UNKNOWN_OFFSET, 0,
    282 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    283 				UVM_ADV_NORMAL, 0)) != 0)
    284 		panic("startup: cannot allocate VM for buffers");
    285 	minaddr = (vaddr_t)buffers;
    286 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    287 		/* don't want to alloc more physical mem than needed */
    288 		bufpages = btoc(MAXBSIZE) * nbuf;
    289 	}
    290 	base = bufpages / nbuf;
    291 	residual = bufpages % nbuf;
    292 	for (i = 0; i < nbuf; i++) {
    293 		vsize_t curbufsize;
    294 		vaddr_t curbuf;
    295 		struct vm_page *pg;
    296 
    297 		/*
    298 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    299 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    300 		 * for the first "residual" buffers, and then we allocate
    301 		 * "base" pages for the rest.
    302 		 */
    303 		curbuf = (vsize_t) buffers + (i * MAXBSIZE);
    304 		curbufsize = NBPG * ((i < residual) ? (base+1) : base);
    305 
    306 		while (curbufsize) {
    307 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    308 			if (pg == NULL)
    309 				panic("cpu_startup: not enough memory for "
    310 				    "buffer cache");
    311 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    312 					VM_PROT_READ|VM_PROT_WRITE);
    313 			curbuf += PAGE_SIZE;
    314 			curbufsize -= PAGE_SIZE;
    315 		}
    316 	}
    317 	pmap_update(pmap_kernel());
    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 %u 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) != 0)
    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 	    != 0)
    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)p->p_psstr;
    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 #if defined(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_int32_t 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 		pmap_update(pmap_kernel());
    742 		error = (*dump)(dumpdev, blkno, vmmap, NBPG);
    743  bad:
    744 		switch (error) {
    745 		case 0:
    746 			maddr += NBPG;
    747 			blkno += btodb(NBPG);
    748 			break;
    749 
    750 		case ENXIO:
    751 			printf("device bad\n");
    752 			return;
    753 
    754 		case EFAULT:
    755 			printf("device not ready\n");
    756 			return;
    757 
    758 		case EINVAL:
    759 			printf("area improper\n");
    760 			return;
    761 
    762 		case EIO:
    763 			printf("i/o error\n");
    764 			return;
    765 
    766 		case EINTR:
    767 			printf("aborted from console\n");
    768 			return;
    769 
    770 		default:
    771 			printf("error %d\n", error);
    772 			return;
    773 		}
    774 	}
    775 	printf("succeeded\n");
    776 }
    777 
    778 void
    779 straytrap(pc, evec)
    780 	int pc;
    781 	u_short evec;
    782 {
    783 	printf("unexpected trap (vector offset %x) from %x\n",
    784 	       evec & 0xFFF, pc);
    785 }
    786 
    787 int	*nofault;
    788 
    789 int
    790 badaddr(addr, nbytes)
    791 	register caddr_t addr;
    792 	int nbytes;
    793 {
    794 	register int i;
    795 	label_t faultbuf;
    796 
    797 #ifdef lint
    798 	i = *addr; if (i) return (0);
    799 #endif
    800 
    801 	nofault = (int *) &faultbuf;
    802 	if (setjmp((label_t *)nofault)) {
    803 		nofault = (int *) 0;
    804 		return(1);
    805 	}
    806 
    807 	switch (nbytes) {
    808 	case 1:
    809 		i = *(volatile char *)addr;
    810 		break;
    811 
    812 	case 2:
    813 		i = *(volatile short *)addr;
    814 		break;
    815 
    816 	case 4:
    817 		i = *(volatile int *)addr;
    818 		break;
    819 
    820 	default:
    821 		panic("badaddr: bad request");
    822 	}
    823 	nofault = (int *) 0;
    824 	return (0);
    825 }
    826 
    827 void luna68k_abort __P((char *));
    828 
    829 static int innmihand;	/* simple mutex */
    830 
    831 /*
    832  * Level 7 interrupts are caused by e.g. the ABORT switch.
    833  *
    834  * If we have DDB, then break into DDB on ABORT.  In a production
    835  * environment, bumping the ABORT switch would be bad, so we enable
    836  * panic'ing on ABORT with the kernel option "PANICBUTTON".
    837  */
    838 void
    839 nmihand(frame)
    840 	struct frame frame;
    841 {
    842 	/* Prevent unwanted recursion */
    843 	if (innmihand)
    844 		return;
    845 	innmihand = 1;
    846 
    847 	luna68k_abort("ABORT SWITCH");
    848 }
    849 
    850 /*
    851  * Common code for handling ABORT signals from buttons, switches,
    852  * serial lines, etc.
    853  */
    854 void
    855 luna68k_abort(cp)
    856 	char *cp;
    857 {
    858 #ifdef DDB
    859 	printf("%s\n", cp);
    860 	cpu_Debugger();
    861 #else
    862 #ifdef PANICBUTTON
    863 	panic(cp);
    864 #else
    865 	printf("%s ignored\n", cp);
    866 #endif /* PANICBUTTON */
    867 #endif /* DDB */
    868 }
    869 
    870 /*
    871  * cpu_exec_aout_makecmds():
    872  *	cpu-dependent a.out format hook for execve().
    873  *
    874  * Determine of the given exec package refers to something which we
    875  * understand and, if so, set up the vmcmds for it.
    876  */
    877 int
    878 cpu_exec_aout_makecmds(p, epp)
    879 	struct proc *p;
    880 	struct exec_package *epp;
    881 {
    882 	int error = ENOEXEC;
    883 #ifdef COMPAT_SUNOS
    884 	extern sunos_exec_aout_makecmds
    885 	__P((struct proc *, struct exec_package *));
    886 	if ((error = sunos_exec_aout_makecmds(p, epp)) == 0)
    887 		return 0;
    888 #endif
    889 	return error;
    890 }
    891 
    892 /*
    893  * Return the best possible estimate of the time in the timeval
    894  * to which tvp points.	 Unfortunately, we can't read the hardware registers.
    895  * We guarantee that the time will be greater than the value obtained by a
    896  * previous call.
    897  */
    898 void
    899 microtime(tvp)
    900 	register struct timeval *tvp;
    901 {
    902 	int s = splclock();
    903 	static struct timeval lasttime;
    904 
    905 	*tvp = time;
    906 #ifdef notdef
    907 	tvp->tv_usec += clkread();
    908 	while (tvp->tv_usec >= 1000000) {
    909 		tvp->tv_sec++;
    910 		tvp->tv_usec -= 1000000;
    911 	}
    912 #endif
    913 	if (tvp->tv_sec == lasttime.tv_sec &&
    914 	    tvp->tv_usec <= lasttime.tv_usec &&
    915 	    (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
    916 		tvp->tv_sec++;
    917 		tvp->tv_usec -= 1000000;
    918 	}
    919 	lasttime = *tvp;
    920 	splx(s);
    921 }
    922 
    923 #if 1
    924 
    925 struct consdev *cn_tab = &syscons;
    926 
    927 #else
    928 
    929 /*
    930  * romcons is useful until m68k TC register is initialized.
    931  */
    932 int  romcngetc __P((dev_t));
    933 void romcnputc __P((dev_t, int));
    934 
    935 struct consdev romcons = {
    936 	NULL,
    937 	NULL,
    938 	romcngetc,
    939 	romcnputc,
    940 	nullcnpollc,
    941 	makedev(7, 0), /* XXX */
    942 	CN_DEAD,
    943 };
    944 struct consdev *cn_tab = &romcons;
    945 
    946 #define __		((int **)0x41000000)
    947 #define GETC()		(*(int (*)())__[6])()
    948 #define PUTC(x)		(*(void (*)())__[7])(x)
    949 
    950 #define ROMPUTC(x) \
    951 ({					\
    952 	register _r;			\
    953 	asm volatile ("			\
    954 		movc	%%vbr,%0	; \
    955 		movel	%0,%%sp@-	; \
    956 		clrl	%0		; \
    957 		movc	%0,%%vbr"	\
    958 		: "=r" (_r));		\
    959 	PUTC(x);			\
    960 	asm volatile ("			\
    961 		movel	%%sp@+,%0	; \
    962 		movc	%0,%%vbr"	\
    963 		: "=r" (_r));		\
    964 })
    965 
    966 #define ROMGETC() \
    967 ({					\
    968 	register _r, _c;		\
    969 	asm volatile ("			\
    970 		movc	%%vbr,%0	; \
    971 		movel	%0,%%sp@-	; \
    972 		clrl	%0		; \
    973 		movc	%0,%%vbr"	\
    974 		: "=r" (_r));		\
    975 	_c = GETC();			\
    976 	asm volatile ("			\
    977 		movel	%%sp@+,%0	; \
    978 		movc	%0,%%vbr"	\
    979 		: "=r" (_r));		\
    980 	_c;				\
    981 })
    982 
    983 void
    984 romcnputc(dev, c)
    985 	dev_t dev;
    986 	int c;
    987 {
    988 	int s;
    989 
    990 	s = splhigh();
    991 	ROMPUTC(c);
    992 	splx(s);
    993 }
    994 
    995 int
    996 romcngetc(dev)
    997 	dev_t dev;
    998 {
    999 	int s, c;
   1000 
   1001 	do {
   1002 		s = splhigh();
   1003 		c = ROMGETC();
   1004 		splx(s);
   1005 	} while (c == -1);
   1006 	return c;
   1007 }
   1008 #endif
   1009