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