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