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