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