Home | History | Annotate | Line # | Download | only in luna68k
machdep.c revision 1.35
      1 /* $NetBSD: machdep.c,v 1.35 2003/12/04 19:38:21 atatat 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.35 2003/12/04 19:38:21 atatat 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 *)(void *)&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 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
    453 {
    454 
    455 	sysctl_createv(SYSCTL_PERMANENT,
    456 		       CTLTYPE_NODE, "machdep", NULL,
    457 		       NULL, 0, NULL, 0,
    458 		       CTL_MACHDEP, CTL_EOL);
    459 
    460 	sysctl_createv(SYSCTL_PERMANENT,
    461 		       CTLTYPE_STRUCT, "console_device", NULL,
    462 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    463 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
    464 }
    465 
    466 int	waittime = -1;
    467 
    468 void
    469 cpu_reboot(howto, bootstr)
    470 	volatile int howto; /* XXX to shutup GCC XXX */
    471 	char *bootstr;
    472 {
    473 	extern void doboot __P((void));
    474 
    475 	/* take a snap shot before clobbering any registers */
    476 	if (curlwp && curlwp->l_addr)
    477 		savectx(&curlwp->l_addr->u_pcb);
    478 
    479 	/* If system is hold, just halt. */
    480 	if (cold) {
    481 		howto |= RB_HALT;
    482 		goto haltsys;
    483 	}
    484 
    485 	boothowto = howto;
    486 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
    487 		waittime = 0;
    488 		vfs_shutdown();
    489 		/*
    490 		 * If we've been adjusting the clock, the todr
    491 		 * will be out of synch; adjust it now.
    492 		 */
    493 		resettodr();
    494 	}
    495 
    496 	/* Disable interrupts. */
    497 	splhigh();
    498 
    499 	/* If rebooting and a dump is requested, do it. */
    500 	if (howto & RB_DUMP)
    501 		dumpsys();
    502 
    503 haltsys:
    504 	/* Run any shutdown hooks. */
    505 	doshutdownhooks();
    506 
    507 	/* Finally, halt/reboot the system. */
    508 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    509 		u_int8_t *pio = (void *)0x4d000000;
    510 
    511 		printf("power is going down.\n");
    512 		DELAY(100000);
    513 		pio[3] = 0x94;
    514 		pio[2] = 0 << 4;
    515 		for (;;) /* NOP */;
    516 	}
    517 	if (howto & RB_HALT) {
    518 		printf("System halted.	Hit any key to reboot.\n\n");
    519 		(void)cngetc();
    520 	}
    521 
    522 	printf("rebooting...\n");
    523 	DELAY(100000);
    524 	doboot();
    525 	/*NOTREACHED*/
    526 	while (1) ;
    527 }
    528 
    529 /*
    530  * Initialize the kernel crash dump header.
    531  */
    532 void
    533 cpu_init_kcore_hdr()
    534 {
    535 	cpu_kcore_hdr_t *h = &cpu_kcore_hdr;
    536 	struct m68k_kcore_hdr *m = &h->un._m68k;
    537 	extern char end[];
    538 
    539 	bzero(&cpu_kcore_hdr, sizeof(cpu_kcore_hdr));
    540 
    541 	/*
    542 	 * Initialize the `dispatcher' portion of the header.
    543 	 */
    544 	strcpy(h->name, machine);
    545 	h->page_size = PAGE_SIZE;
    546 	h->kernbase = KERNBASE;
    547 
    548 	/*
    549 	 * Fill in information about our MMU configuration.
    550 	 */
    551 	m->mmutype	= mmutype;
    552 	m->sg_v		= SG_V;
    553 	m->sg_frame	= SG_FRAME;
    554 	m->sg_ishift	= SG_ISHIFT;
    555 	m->sg_pmask	= SG_PMASK;
    556 	m->sg40_shift1	= SG4_SHIFT1;
    557 	m->sg40_mask2	= SG4_MASK2;
    558 	m->sg40_shift2	= SG4_SHIFT2;
    559 	m->sg40_mask3	= SG4_MASK3;
    560 	m->sg40_shift3	= SG4_SHIFT3;
    561 	m->sg40_addr1	= SG4_ADDR1;
    562 	m->sg40_addr2	= SG4_ADDR2;
    563 	m->pg_v		= PG_V;
    564 	m->pg_frame	= PG_FRAME;
    565 
    566 	/*
    567 	 * Initialize pointer to kernel segment table.
    568 	 */
    569 	m->sysseg_pa = (u_int32_t)(pmap_kernel()->pm_stpa);
    570 
    571 	/*
    572 	 * Initialize relocation value such that:
    573 	 *
    574 	 *	pa = (va - KERNBASE) + reloc
    575 	 *
    576 	 * Since we're linked and loaded at the same place,
    577 	 * and the kernel is mapped va == pa, this is 0.
    578 	 */
    579 	m->reloc = 0;
    580 
    581 	/*
    582 	 * Define the end of the relocatable range.
    583 	 */
    584 	m->relocend = (u_int32_t)end;
    585 
    586 	/*
    587 	 * The luna68k has one contiguous memory segment.
    588 	 */
    589 	m->ram_segs[0].start = 0 /* lowram */;
    590 	m->ram_segs[0].size  = ctob(physmem);
    591 }
    592 
    593 /*
    594  * Compute the size of the machine-dependent crash dump header.
    595  * Returns size in disk blocks.
    596  */
    597 int
    598 cpu_dumpsize()
    599 {
    600 	int size;
    601 
    602 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t));
    603 	return (btodb(roundup(size, dbtob(1))));
    604 }
    605 
    606 /*
    607  * Called by dumpsys() to dump the machine-dependent header.
    608  */
    609 int
    610 cpu_dump(dump, blknop)
    611 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
    612 	daddr_t *blknop;
    613 {
    614 	int buf[dbtob(1) / sizeof(int)];
    615 	cpu_kcore_hdr_t *chdr;
    616 	kcore_seg_t *kseg;
    617 	int error;
    618 
    619 	kseg = (kcore_seg_t *)buf;
    620 	chdr = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(kcore_seg_t)) /
    621 	    sizeof(int)];
    622 
    623 	/* Create the segment header. */
    624 	CORE_SETMAGIC(*kseg, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
    625 	kseg->c_size = dbtob(1) - ALIGN(sizeof(kcore_seg_t));
    626 
    627 	bcopy(&cpu_kcore_hdr, chdr, sizeof(cpu_kcore_hdr_t));
    628 	error = (*dump)(dumpdev, *blknop, (caddr_t)buf, sizeof(buf));
    629 	*blknop += btodb(sizeof(buf));
    630 	return (error);
    631 }
    632 
    633 /*
    634  * These variables are needed by /sbin/savecore
    635  */
    636 u_int32_t dumpmag = 0x8fca0101;	/* magic number */
    637 int	dumpsize = 0;		/* pages */
    638 long	dumplo = 0;		/* blocks */
    639 
    640 /*
    641  * This is called by main to set dumplo and dumpsize.
    642  * Dumps always skip the first PAGE_SIZE of disk space
    643  * in case there might be a disk label stored there.
    644  * If there is extra space, put dump at the end to
    645  * reduce the chance that swapping trashes it.
    646  */
    647 void
    648 cpu_dumpconf()
    649 {
    650 	const struct bdevsw *bdev;
    651 	int chdrsize;	/* size of dump header */
    652 	int nblks;	/* size of dump area */
    653 
    654 	if (dumpdev == NODEV)
    655 		return;
    656 	bdev = bdevsw_lookup(dumpdev);
    657 	if (bdev == NULL)
    658 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
    659 	if (bdev->d_psize == NULL)
    660 		return;
    661 	nblks = (*bdev->d_psize)(dumpdev);
    662 	chdrsize = cpu_dumpsize();
    663 
    664 	dumpsize = btoc(cpu_kcore_hdr.un._m68k.ram_segs[0].size);
    665 
    666 	/*
    667 	 * Check do see if we will fit.  Note we always skip the
    668 	 * first PAGE_SIZE in case there is a disk label there.
    669 	 */
    670 	if (nblks < (ctod(dumpsize) + chdrsize + ctod(1))) {
    671 		dumpsize = 0;
    672 		dumplo = -1;
    673 		return;
    674 	}
    675 
    676 	/*
    677 	 * Put dump at the end of the partition.
    678 	 */
    679 	dumplo = (nblks - 1) - ctod(dumpsize) - chdrsize;
    680 }
    681 
    682 /*
    683  * Dump physical memory onto the dump device.  Called by cpu_reboot().
    684  */
    685 void
    686 dumpsys()
    687 {
    688 	const struct bdevsw *bdev;
    689 	daddr_t blkno;		/* current block to write */
    690 				/* dump routine */
    691 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
    692 	int pg;			/* page being dumped */
    693 	paddr_t maddr;		/* PA being dumped */
    694 	int error;		/* error code from (*dump)() */
    695 
    696 	/* XXX initialized here because of gcc lossage */
    697 	maddr = 0 /* lowram */;
    698 	pg = 0;
    699 
    700 	/* Make sure dump device is valid. */
    701 	if (dumpdev == NODEV)
    702 		return;
    703 	bdev = bdevsw_lookup(dumpdev);
    704 	if (bdev == NULL)
    705 		return;
    706 	if (dumpsize == 0) {
    707 		cpu_dumpconf();
    708 		if (dumpsize == 0)
    709 			return;
    710 	}
    711 	if (dumplo <= 0) {
    712 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
    713 		    minor(dumpdev));
    714 		return;
    715 	}
    716 	dump = bdev->d_dump;
    717 	blkno = dumplo;
    718 
    719 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
    720 	    minor(dumpdev), dumplo);
    721 
    722 	printf("dump ");
    723 
    724 	/* Write the dump header. */
    725 	error = cpu_dump(dump, &blkno);
    726 	if (error)
    727 		goto bad;
    728 
    729 	for (pg = 0; pg < dumpsize; pg++) {
    730 #define NPGMB	(1024*1024/PAGE_SIZE)
    731 		/* print out how many MBs we have dumped */
    732 		if (pg && (pg % NPGMB) == 0)
    733 			printf("%d ", pg / NPGMB);
    734 #undef NPGMB
    735 		pmap_enter(pmap_kernel(), (vaddr_t)vmmap, maddr,
    736 		    VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    737 
    738 		pmap_update(pmap_kernel());
    739 		error = (*dump)(dumpdev, blkno, vmmap, PAGE_SIZE);
    740  bad:
    741 		switch (error) {
    742 		case 0:
    743 			maddr += PAGE_SIZE;
    744 			blkno += btodb(PAGE_SIZE);
    745 			break;
    746 
    747 		case ENXIO:
    748 			printf("device bad\n");
    749 			return;
    750 
    751 		case EFAULT:
    752 			printf("device not ready\n");
    753 			return;
    754 
    755 		case EINVAL:
    756 			printf("area improper\n");
    757 			return;
    758 
    759 		case EIO:
    760 			printf("i/o error\n");
    761 			return;
    762 
    763 		case EINTR:
    764 			printf("aborted from console\n");
    765 			return;
    766 
    767 		default:
    768 			printf("error %d\n", error);
    769 			return;
    770 		}
    771 	}
    772 	printf("succeeded\n");
    773 }
    774 
    775 void
    776 straytrap(pc, evec)
    777 	int pc;
    778 	u_short evec;
    779 {
    780 	printf("unexpected trap (vector offset %x) from %x\n",
    781 	       evec & 0xFFF, pc);
    782 }
    783 
    784 int	*nofault;
    785 
    786 int
    787 badaddr(addr, nbytes)
    788 	register caddr_t addr;
    789 	int nbytes;
    790 {
    791 	register int i;
    792 	label_t faultbuf;
    793 
    794 #ifdef lint
    795 	i = *addr; if (i) return (0);
    796 #endif
    797 
    798 	nofault = (int *) &faultbuf;
    799 	if (setjmp((label_t *)nofault)) {
    800 		nofault = (int *) 0;
    801 		return(1);
    802 	}
    803 
    804 	switch (nbytes) {
    805 	case 1:
    806 		i = *(volatile char *)addr;
    807 		break;
    808 
    809 	case 2:
    810 		i = *(volatile short *)addr;
    811 		break;
    812 
    813 	case 4:
    814 		i = *(volatile int *)addr;
    815 		break;
    816 
    817 	default:
    818 		panic("badaddr: bad request");
    819 	}
    820 	nofault = (int *) 0;
    821 	return (0);
    822 }
    823 
    824 void luna68k_abort __P((char *));
    825 
    826 static int innmihand;	/* simple mutex */
    827 
    828 /*
    829  * Level 7 interrupts are caused by e.g. the ABORT switch.
    830  *
    831  * If we have DDB, then break into DDB on ABORT.  In a production
    832  * environment, bumping the ABORT switch would be bad, so we enable
    833  * panic'ing on ABORT with the kernel option "PANICBUTTON".
    834  */
    835 void
    836 nmihand(frame)
    837 	struct frame frame;
    838 {
    839 	/* Prevent unwanted recursion */
    840 	if (innmihand)
    841 		return;
    842 	innmihand = 1;
    843 
    844 	luna68k_abort("ABORT SWITCH");
    845 }
    846 
    847 /*
    848  * Common code for handling ABORT signals from buttons, switches,
    849  * serial lines, etc.
    850  */
    851 void
    852 luna68k_abort(cp)
    853 	char *cp;
    854 {
    855 #ifdef DDB
    856 	printf("%s\n", cp);
    857 	cpu_Debugger();
    858 #else
    859 #ifdef PANICBUTTON
    860 	panic(cp);
    861 #else
    862 	printf("%s ignored\n", cp);
    863 #endif /* PANICBUTTON */
    864 #endif /* DDB */
    865 }
    866 
    867 /*
    868  * cpu_exec_aout_makecmds():
    869  *	cpu-dependent a.out format hook for execve().
    870  *
    871  * Determine of the given exec package refers to something which we
    872  * understand and, if so, set up the vmcmds for it.
    873  */
    874 int
    875 cpu_exec_aout_makecmds(p, epp)
    876 	struct proc *p;
    877 	struct exec_package *epp;
    878 {
    879 	int error = ENOEXEC;
    880 #ifdef COMPAT_SUNOS
    881 	extern sunos_exec_aout_makecmds
    882 	__P((struct proc *, struct exec_package *));
    883 	if ((error = sunos_exec_aout_makecmds(p, epp)) == 0)
    884 		return 0;
    885 #endif
    886 	return error;
    887 }
    888 
    889 /*
    890  * Return the best possible estimate of the time in the timeval
    891  * to which tvp points.	 Unfortunately, we can't read the hardware registers.
    892  * We guarantee that the time will be greater than the value obtained by a
    893  * previous call.
    894  */
    895 void
    896 microtime(tvp)
    897 	register struct timeval *tvp;
    898 {
    899 	int s = splclock();
    900 	static struct timeval lasttime;
    901 
    902 	*tvp = time;
    903 #ifdef notdef
    904 	tvp->tv_usec += clkread();
    905 	while (tvp->tv_usec >= 1000000) {
    906 		tvp->tv_sec++;
    907 		tvp->tv_usec -= 1000000;
    908 	}
    909 #endif
    910 	if (tvp->tv_sec == lasttime.tv_sec &&
    911 	    tvp->tv_usec <= lasttime.tv_usec &&
    912 	    (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
    913 		tvp->tv_sec++;
    914 		tvp->tv_usec -= 1000000;
    915 	}
    916 	lasttime = *tvp;
    917 	splx(s);
    918 }
    919 
    920 #if 1
    921 
    922 struct consdev *cn_tab = &syscons;
    923 
    924 #else
    925 
    926 /*
    927  * romcons is useful until m68k TC register is initialized.
    928  */
    929 int  romcngetc __P((dev_t));
    930 void romcnputc __P((dev_t, int));
    931 
    932 struct consdev romcons = {
    933 	NULL,
    934 	NULL,
    935 	romcngetc,
    936 	romcnputc,
    937 	nullcnpollc,
    938 	makedev(7, 0), /* XXX */
    939 	CN_DEAD,
    940 };
    941 struct consdev *cn_tab = &romcons;
    942 
    943 #define __		((int **)0x41000000)
    944 #define GETC()		(*(int (*)())__[6])()
    945 #define PUTC(x)		(*(void (*)())__[7])(x)
    946 
    947 #define ROMPUTC(x) \
    948 ({					\
    949 	register _r;			\
    950 	asm volatile ("			\
    951 		movc	%%vbr,%0	; \
    952 		movel	%0,%%sp@-	; \
    953 		clrl	%0		; \
    954 		movc	%0,%%vbr"	\
    955 		: "=r" (_r));		\
    956 	PUTC(x);			\
    957 	asm volatile ("			\
    958 		movel	%%sp@+,%0	; \
    959 		movc	%0,%%vbr"	\
    960 		: "=r" (_r));		\
    961 })
    962 
    963 #define ROMGETC() \
    964 ({					\
    965 	register _r, _c;		\
    966 	asm volatile ("			\
    967 		movc	%%vbr,%0	; \
    968 		movel	%0,%%sp@-	; \
    969 		clrl	%0		; \
    970 		movc	%0,%%vbr"	\
    971 		: "=r" (_r));		\
    972 	_c = GETC();			\
    973 	asm volatile ("			\
    974 		movel	%%sp@+,%0	; \
    975 		movc	%0,%%vbr"	\
    976 		: "=r" (_r));		\
    977 	_c;				\
    978 })
    979 
    980 void
    981 romcnputc(dev, c)
    982 	dev_t dev;
    983 	int c;
    984 {
    985 	int s;
    986 
    987 	s = splhigh();
    988 	ROMPUTC(c);
    989 	splx(s);
    990 }
    991 
    992 int
    993 romcngetc(dev)
    994 	dev_t dev;
    995 {
    996 	int s, c;
    997 
    998 	do {
    999 		s = splhigh();
   1000 		c = ROMGETC();
   1001 		splx(s);
   1002 	} while (c == -1);
   1003 	return c;
   1004 }
   1005 #endif
   1006