Home | History | Annotate | Line # | Download | only in luna68k
machdep.c revision 1.23
      1 /* $NetBSD: machdep.c,v 1.23 2002/09/06 13:18:43 gehenna 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.23 2002/09/06 13:18:43 gehenna 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 	const struct bdevsw *bdev;
    658 	int chdrsize;	/* size of dump header */
    659 	int nblks;	/* size of dump area */
    660 
    661 	if (dumpdev == NODEV)
    662 		return;
    663 	bdev = bdevsw_lookup(dumpdev);
    664 	if (bdev == NULL)
    665 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
    666 	if (bdev->d_psize == NULL)
    667 		return;
    668 	nblks = (*bdev->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 	const struct bdevsw *bdev;
    696 	daddr_t blkno;		/* current block to write */
    697 				/* dump routine */
    698 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
    699 	int pg;			/* page being dumped */
    700 	paddr_t maddr;		/* PA being dumped */
    701 	int error;		/* error code from (*dump)() */
    702 
    703 	/* XXX initialized here because of gcc lossage */
    704 	maddr = 0 /* lowram */;
    705 	pg = 0;
    706 
    707 	/* Make sure dump device is valid. */
    708 	if (dumpdev == NODEV)
    709 		return;
    710 	bdev = bdevsw_lookup(dumpdev);
    711 	if (bdev == NULL)
    712 		return;
    713 	if (dumpsize == 0) {
    714 		cpu_dumpconf();
    715 		if (dumpsize == 0)
    716 			return;
    717 	}
    718 	if (dumplo <= 0) {
    719 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
    720 		    minor(dumpdev));
    721 		return;
    722 	}
    723 	dump = bdev->d_dump;
    724 	blkno = dumplo;
    725 
    726 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
    727 	    minor(dumpdev), dumplo);
    728 
    729 	printf("dump ");
    730 
    731 	/* Write the dump header. */
    732 	error = cpu_dump(dump, &blkno);
    733 	if (error)
    734 		goto bad;
    735 
    736 	for (pg = 0; pg < dumpsize; pg++) {
    737 #define NPGMB	(1024*1024/NBPG)
    738 		/* print out how many MBs we have dumped */
    739 		if (pg && (pg % NPGMB) == 0)
    740 			printf("%d ", pg / NPGMB);
    741 #undef NPGMB
    742 		pmap_enter(pmap_kernel(), (vaddr_t)vmmap, maddr,
    743 		    VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    744 
    745 		pmap_update(pmap_kernel());
    746 		error = (*dump)(dumpdev, blkno, vmmap, NBPG);
    747  bad:
    748 		switch (error) {
    749 		case 0:
    750 			maddr += NBPG;
    751 			blkno += btodb(NBPG);
    752 			break;
    753 
    754 		case ENXIO:
    755 			printf("device bad\n");
    756 			return;
    757 
    758 		case EFAULT:
    759 			printf("device not ready\n");
    760 			return;
    761 
    762 		case EINVAL:
    763 			printf("area improper\n");
    764 			return;
    765 
    766 		case EIO:
    767 			printf("i/o error\n");
    768 			return;
    769 
    770 		case EINTR:
    771 			printf("aborted from console\n");
    772 			return;
    773 
    774 		default:
    775 			printf("error %d\n", error);
    776 			return;
    777 		}
    778 	}
    779 	printf("succeeded\n");
    780 }
    781 
    782 void
    783 straytrap(pc, evec)
    784 	int pc;
    785 	u_short evec;
    786 {
    787 	printf("unexpected trap (vector offset %x) from %x\n",
    788 	       evec & 0xFFF, pc);
    789 }
    790 
    791 int	*nofault;
    792 
    793 int
    794 badaddr(addr, nbytes)
    795 	register caddr_t addr;
    796 	int nbytes;
    797 {
    798 	register int i;
    799 	label_t faultbuf;
    800 
    801 #ifdef lint
    802 	i = *addr; if (i) return (0);
    803 #endif
    804 
    805 	nofault = (int *) &faultbuf;
    806 	if (setjmp((label_t *)nofault)) {
    807 		nofault = (int *) 0;
    808 		return(1);
    809 	}
    810 
    811 	switch (nbytes) {
    812 	case 1:
    813 		i = *(volatile char *)addr;
    814 		break;
    815 
    816 	case 2:
    817 		i = *(volatile short *)addr;
    818 		break;
    819 
    820 	case 4:
    821 		i = *(volatile int *)addr;
    822 		break;
    823 
    824 	default:
    825 		panic("badaddr: bad request");
    826 	}
    827 	nofault = (int *) 0;
    828 	return (0);
    829 }
    830 
    831 void luna68k_abort __P((char *));
    832 
    833 static int innmihand;	/* simple mutex */
    834 
    835 /*
    836  * Level 7 interrupts are caused by e.g. the ABORT switch.
    837  *
    838  * If we have DDB, then break into DDB on ABORT.  In a production
    839  * environment, bumping the ABORT switch would be bad, so we enable
    840  * panic'ing on ABORT with the kernel option "PANICBUTTON".
    841  */
    842 void
    843 nmihand(frame)
    844 	struct frame frame;
    845 {
    846 	/* Prevent unwanted recursion */
    847 	if (innmihand)
    848 		return;
    849 	innmihand = 1;
    850 
    851 	luna68k_abort("ABORT SWITCH");
    852 }
    853 
    854 /*
    855  * Common code for handling ABORT signals from buttons, switches,
    856  * serial lines, etc.
    857  */
    858 void
    859 luna68k_abort(cp)
    860 	char *cp;
    861 {
    862 #ifdef DDB
    863 	printf("%s\n", cp);
    864 	cpu_Debugger();
    865 #else
    866 #ifdef PANICBUTTON
    867 	panic(cp);
    868 #else
    869 	printf("%s ignored\n", cp);
    870 #endif /* PANICBUTTON */
    871 #endif /* DDB */
    872 }
    873 
    874 /*
    875  * cpu_exec_aout_makecmds():
    876  *	cpu-dependent a.out format hook for execve().
    877  *
    878  * Determine of the given exec package refers to something which we
    879  * understand and, if so, set up the vmcmds for it.
    880  */
    881 int
    882 cpu_exec_aout_makecmds(p, epp)
    883 	struct proc *p;
    884 	struct exec_package *epp;
    885 {
    886 	int error = ENOEXEC;
    887 #ifdef COMPAT_SUNOS
    888 	extern sunos_exec_aout_makecmds
    889 	__P((struct proc *, struct exec_package *));
    890 	if ((error = sunos_exec_aout_makecmds(p, epp)) == 0)
    891 		return 0;
    892 #endif
    893 	return error;
    894 }
    895 
    896 /*
    897  * Return the best possible estimate of the time in the timeval
    898  * to which tvp points.	 Unfortunately, we can't read the hardware registers.
    899  * We guarantee that the time will be greater than the value obtained by a
    900  * previous call.
    901  */
    902 void
    903 microtime(tvp)
    904 	register struct timeval *tvp;
    905 {
    906 	int s = splclock();
    907 	static struct timeval lasttime;
    908 
    909 	*tvp = time;
    910 #ifdef notdef
    911 	tvp->tv_usec += clkread();
    912 	while (tvp->tv_usec >= 1000000) {
    913 		tvp->tv_sec++;
    914 		tvp->tv_usec -= 1000000;
    915 	}
    916 #endif
    917 	if (tvp->tv_sec == lasttime.tv_sec &&
    918 	    tvp->tv_usec <= lasttime.tv_usec &&
    919 	    (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
    920 		tvp->tv_sec++;
    921 		tvp->tv_usec -= 1000000;
    922 	}
    923 	lasttime = *tvp;
    924 	splx(s);
    925 }
    926 
    927 #if 1
    928 
    929 struct consdev *cn_tab = &syscons;
    930 
    931 #else
    932 
    933 /*
    934  * romcons is useful until m68k TC register is initialized.
    935  */
    936 int  romcngetc __P((dev_t));
    937 void romcnputc __P((dev_t, int));
    938 
    939 struct consdev romcons = {
    940 	NULL,
    941 	NULL,
    942 	romcngetc,
    943 	romcnputc,
    944 	nullcnpollc,
    945 	makedev(7, 0), /* XXX */
    946 	CN_DEAD,
    947 };
    948 struct consdev *cn_tab = &romcons;
    949 
    950 #define __		((int **)0x41000000)
    951 #define GETC()		(*(int (*)())__[6])()
    952 #define PUTC(x)		(*(void (*)())__[7])(x)
    953 
    954 #define ROMPUTC(x) \
    955 ({					\
    956 	register _r;			\
    957 	asm volatile ("			\
    958 		movc	%%vbr,%0	; \
    959 		movel	%0,%%sp@-	; \
    960 		clrl	%0		; \
    961 		movc	%0,%%vbr"	\
    962 		: "=r" (_r));		\
    963 	PUTC(x);			\
    964 	asm volatile ("			\
    965 		movel	%%sp@+,%0	; \
    966 		movc	%0,%%vbr"	\
    967 		: "=r" (_r));		\
    968 })
    969 
    970 #define ROMGETC() \
    971 ({					\
    972 	register _r, _c;		\
    973 	asm volatile ("			\
    974 		movc	%%vbr,%0	; \
    975 		movel	%0,%%sp@-	; \
    976 		clrl	%0		; \
    977 		movc	%0,%%vbr"	\
    978 		: "=r" (_r));		\
    979 	_c = GETC();			\
    980 	asm volatile ("			\
    981 		movel	%%sp@+,%0	; \
    982 		movc	%0,%%vbr"	\
    983 		: "=r" (_r));		\
    984 	_c;				\
    985 })
    986 
    987 void
    988 romcnputc(dev, c)
    989 	dev_t dev;
    990 	int c;
    991 {
    992 	int s;
    993 
    994 	s = splhigh();
    995 	ROMPUTC(c);
    996 	splx(s);
    997 }
    998 
    999 int
   1000 romcngetc(dev)
   1001 	dev_t dev;
   1002 {
   1003 	int s, c;
   1004 
   1005 	do {
   1006 		s = splhigh();
   1007 		c = ROMGETC();
   1008 		splx(s);
   1009 	} while (c == -1);
   1010 	return c;
   1011 }
   1012 #endif
   1013