Home | History | Annotate | Line # | Download | only in luna68k
machdep.c revision 1.13
      1 /* $NetBSD: machdep.c,v 1.13 2000/12/12 04:07:35 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.13 2000/12/12 04:07:35 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/device.h>
     56 #include <sys/malloc.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/msgbuf.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/tty.h>
     61 #include <sys/mount.h>
     62 #include <sys/user.h>
     63 #include <sys/exec.h>
     64 #include <sys/core.h>
     65 #include <sys/kcore.h>
     66 #include <sys/vnode.h>
     67 #include <sys/syscallargs.h>
     68 #ifdef	KGDB
     69 #include <sys/kgdb.h>
     70 #endif
     71 #include <sys/boot_flag.h>
     72 
     73 #include <uvm/uvm_extern.h>
     74 
     75 #include <sys/sysctl.h>
     76 
     77 #include <machine/cpu.h>
     78 #include <machine/reg.h>
     79 #include <machine/psl.h>
     80 #include <machine/pte.h>
     81 #include <machine/kcore.h>	/* XXX should be pulled in by sys/kcore.h */
     82 
     83 #include <dev/cons.h>
     84 
     85 #if defined(DDB)
     86 #include <machine/db_machdep.h>
     87 #include <ddb/db_sym.h>
     88 #include <ddb/db_extern.h>
     89 #endif
     90 
     91 /*
     92  * Info for CTL_HW
     93  */
     94 char	machine[] = MACHINE;
     95 char	cpu_model[60];
     96 
     97 /* Our exported CPU info; we can have only one. */
     98 struct cpu_info cpu_info_store;
     99 
    100 extern char kernel_text[];
    101 extern char etext[];
    102 
    103 vm_map_t exec_map = NULL;
    104 vm_map_t mb_map = NULL;
    105 vm_map_t phys_map = NULL;
    106 
    107 caddr_t	msgbufaddr;
    108 int	maxmem;			/* max memory per process */
    109 int	physmem;		/* set by locore */
    110 /*
    111  * safepri is a safe priority for sleep to set for a spin-wait
    112  * during autoconfiguration or after a panic.
    113  */
    114 int	safepri = PSL_LOWIPL;
    115 
    116 void luna68k_init __P((void));
    117 void identifycpu __P((void));
    118 void dumpsys __P((void));
    119 
    120 void straytrap __P((int, u_short));
    121 void nmihand __P((struct frame));
    122 
    123 int  cpu_dumpsize __P((void));
    124 int  cpu_dump __P((int (*)(dev_t, daddr_t, caddr_t, size_t), daddr_t *));
    125 void cpu_init_kcore_hdr __P((void));
    126 
    127 /*
    128  * Machine-independent crash dump header info.
    129  */
    130 cpu_kcore_hdr_t cpu_kcore_hdr;
    131 
    132 int	machtype;	/* model: 1 for LUNA-1, 2 for LUNA-2 */
    133 int	sysconsole;	/* console: 0 for ttya, 1 for video */
    134 
    135 extern struct consdev syscons;
    136 extern void omfb_cnattach __P((void));
    137 extern void ws_cnattach __P((void));
    138 extern void syscnattach __P((int));
    139 
    140 /*
    141  * On the 68020/68030, the value of delay_divisor is roughly
    142  * 2048 / cpuspeed (where cpuspeed is in MHz).
    143  *
    144  * On the 68040/68060(?), the value of delay_divisor is roughly
    145  * 759 / cpuspeed (where cpuspeed is in MHz).
    146  * XXX -- is the above formula correct?
    147  */
    148 int	cpuspeed = 25;		/* only used for printing later */
    149 int	delay_divisor = 300;	/* for delay() loop count */
    150 
    151 /*
    152  * Early initialization, before main() is called.
    153  */
    154 void
    155 luna68k_init()
    156 {
    157 	volatile unsigned char *pio0 = (void *)0x49000000;
    158 	int sw1, i;
    159 	char *cp;
    160 	extern char bootarg[64];
    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 	pio0[3] = 0xb6;
    183 	pio0[2] = 1 << 6;		/* enable parity check */
    184 	pio0[3] = 0xb6;
    185 	sw1 = pio0[0];			/* dipssw1 value */
    186 	sw1 ^= 0xff;
    187 	sysconsole = !(sw1 & 0x2);	/* console selection */
    188 
    189 	boothowto = 0;
    190 	i = 0;
    191 	/*
    192 	 * 'bootarg' has;
    193 	 *   "<args of x command> ENADDR=<addr> HOST=<host> SERVER=<name>"
    194 	 * where <addr> is MAC address of which network loader used (not
    195 	 * necessarily same as one at 0x4101.FFE0), <host> and <name>
    196 	 * are the values of HOST and SERVER environment variables,
    197 	 *
    198 	 * NetBSD/luna68k cares only the first argment; any of "sda".
    199 	 */
    200 	for (cp = bootarg; *cp != ' '; cp++) {
    201 		BOOT_FLAG(*cp, boothowto);
    202 		if (i++ >= sizeof(bootarg))
    203 			break;
    204 	}
    205 #if 0 /* overload 1:sw1, which now means 'go ROM monitor' after poweron */
    206 	if (boothowto == 0)
    207 		boothowto = (sw1 & 0x1) ? RB_SINGLE : 0;
    208 #endif
    209 }
    210 
    211 /*
    212  * Console initialization: called early on from main,
    213  */
    214 void
    215 consinit()
    216 {
    217 	if (sysconsole == 0)
    218 		syscnattach(0);
    219 	else {
    220 		omfb_cnattach();
    221 		ws_cnattach();
    222 	}
    223 
    224 #ifdef DDB
    225 	{
    226 		extern int end;
    227 		extern int *esym;
    228 
    229 		ddb_init(*(int *)&end, ((int *)&end) + 1, esym);
    230 	}
    231 	if (boothowto & RB_KDB)
    232 		cpu_Debugger();
    233 #endif
    234 }
    235 
    236 /*
    237  * cpu_startup: allocate memory for variable-sized tables.
    238  */
    239 void
    240 cpu_startup()
    241 {
    242 	int i;
    243 	caddr_t v;
    244 	int base, residual;
    245 	vaddr_t minaddr, maxaddr;
    246 	vsize_t size;
    247 	char pbuf[9];
    248 	extern void greeting __P((void));
    249 
    250 	/*
    251 	 * Initialize the kernel crash dump header.
    252 	 */
    253 	cpu_init_kcore_hdr();
    254 
    255 	/*
    256 	 * Good {morning,afternoon,evening,night}.
    257 	 */
    258 	printf(version);
    259 	identifycpu();
    260 
    261 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    262 	printf("total memory = %s\n", pbuf);
    263 
    264 	/*
    265 	 * Find out how much space we need, allocate it,
    266 	 * and then give everything true virtual addresses.
    267 	 */
    268 	size = (int)allocsys(NULL, NULL);
    269 	if ((v = (caddr_t)uvm_km_alloc(kernel_map, round_page(size))) == 0)
    270 		panic("startup: no room for tables");
    271 	if (allocsys(v, NULL) - v != size)
    272 		panic("startup: table size inconsistency");
    273 
    274 	/*
    275 	 * Now allocate buffers proper.  They are different than the above
    276 	 * in that they usually occupy more virtual memory than physical.
    277 	 */
    278 	size = MAXBSIZE * nbuf;
    279 	if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
    280 		    NULL, UVM_UNKNOWN_OFFSET, 0,
    281 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    282 				UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
    283 		panic("startup: cannot allocate VM for buffers");
    284 	minaddr = (vaddr_t)buffers;
    285 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    286 		/* don't want to alloc more physical mem than needed */
    287 		bufpages = btoc(MAXBSIZE) * nbuf;
    288 	}
    289 	base = bufpages / nbuf;
    290 	residual = bufpages % nbuf;
    291 	for (i = 0; i < nbuf; i++) {
    292 		vsize_t curbufsize;
    293 		vaddr_t curbuf;
    294 		struct vm_page *pg;
    295 
    296 		/*
    297 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    298 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    299 		 * for the first "residual" buffers, and then we allocate
    300 		 * "base" pages for the rest.
    301 		 */
    302 		curbuf = (vsize_t) buffers + (i * MAXBSIZE);
    303 		curbufsize = NBPG * ((i < residual) ? (base+1) : base);
    304 
    305 		while (curbufsize) {
    306 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    307 			if (pg == NULL)
    308 				panic("cpu_startup: not enough memory for "
    309 				    "buffer cache");
    310 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    311 					VM_PROT_READ|VM_PROT_WRITE);
    312 			curbuf += PAGE_SIZE;
    313 			curbufsize -= PAGE_SIZE;
    314 		}
    315 	}
    316 
    317 	/*
    318 	 * Allocate a submap for exec arguments.  This map effectively
    319 	 * limits the number of processes exec'ing at any time.
    320 	 */
    321 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    322 				   16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    323 
    324 	/*
    325 	 * Allocate a submap for physio
    326 	 */
    327 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    328 				   VM_PHYS_SIZE, 0, FALSE, NULL);
    329 
    330 	/*
    331 	 * Finally, allocate mbuf cluster submap.
    332 	 */
    333 	mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    334 				 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
    335 				 FALSE, NULL);
    336 
    337 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    338 	printf("avail memory = %s\n", pbuf);
    339 	format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
    340 	printf("using %d buffers containing %s of memory\n", nbuf, pbuf);
    341 
    342 	/*
    343 	 * Tell the VM system that the area before the text segment
    344 	 * is invalid.
    345 	 *
    346 	 * XXX Should just change KERNBASE and VM_MIN_KERNEL_ADDRESS,
    347 	 * XXX but not right now.
    348 	 */
    349 	if (uvm_map_protect(kernel_map, 0, round_page((vaddr_t)&kernel_text),
    350 	    UVM_PROT_NONE, TRUE) != KERN_SUCCESS)
    351 		panic("can't mark pre-text pages off-limits");
    352 
    353 	/*
    354 	 * Tell the VM system that writing to kernel text isn't allowed.
    355 	 * If we don't, we might end up COW'ing the text segment!
    356 	 */
    357 	if (uvm_map_protect(kernel_map, trunc_page((vaddr_t)&kernel_text),
    358 	    trunc_page((vaddr_t)&etext), UVM_PROT_READ|UVM_PROT_EXEC, TRUE)
    359 	    != KERN_SUCCESS)
    360 		panic("can't protect kernel text");
    361 
    362 	/*
    363 	 * Set up buffers, so they can be used to read disk labels.
    364 	 */
    365 	bufinit();
    366 
    367 	/*
    368 	 * Say "Hi" to the world
    369 	 */
    370 	greeting();
    371 }
    372 
    373 /*
    374  * Set registers on exec.
    375  */
    376 void
    377 setregs(p, pack, stack)
    378 	register struct proc *p;
    379 	struct exec_package *pack;
    380 	u_long stack;
    381 {
    382 	struct frame *frame = (struct frame *)p->p_md.md_regs;
    383 	extern int fputype;
    384 
    385 	frame->f_sr = PSL_USERSET;
    386 	frame->f_pc = pack->ep_entry & ~1;
    387 	frame->f_regs[D0] = 0;
    388 	frame->f_regs[D1] = 0;
    389 	frame->f_regs[D2] = 0;
    390 	frame->f_regs[D3] = 0;
    391 	frame->f_regs[D4] = 0;
    392 	frame->f_regs[D5] = 0;
    393 	frame->f_regs[D6] = 0;
    394 	frame->f_regs[D7] = 0;
    395 	frame->f_regs[A0] = 0;
    396 	frame->f_regs[A1] = 0;
    397 	frame->f_regs[A2] = (int)PS_STRINGS;
    398 	frame->f_regs[A3] = 0;
    399 	frame->f_regs[A4] = 0;
    400 	frame->f_regs[A5] = 0;
    401 	frame->f_regs[A6] = 0;
    402 	frame->f_regs[SP] = stack;
    403 
    404 	/* restore a null state frame */
    405 	p->p_addr->u_pcb.pcb_fpregs.fpf_null = 0;
    406 	if (fputype)
    407 		m68881_restore(&p->p_addr->u_pcb.pcb_fpregs);
    408 }
    409 
    410 void
    411 identifycpu()
    412 {
    413 	extern int cputype;
    414 	char *cpu;
    415 
    416 	bzero(cpu_model, sizeof(cpu_model));
    417 	switch (cputype) {
    418 	case CPU_68030:
    419 		cpu = "MC68030 CPU+MMU, MC68882 FPU";
    420 		machtype = LUNA_I;
    421 		cpuspeed = 20; delay_divisor = 102;	/* 20MHz 68030 */
    422 		hz = 60;
    423 		break;
    424 #if defined(M68040)
    425 	case CPU_68040:
    426 		cpu = "MC68040 CPU+MMU+FPU, 4k on-chip physical I/D caches";
    427 		machtype = LUNA_II;
    428 		cpuspeed = 25; delay_divisor = 300;	/* 25MHz 68040 */
    429 		break;
    430 #endif
    431 	default:
    432 		panic("unknown CPU type");
    433 	}
    434 	strcpy(cpu_model, cpu);
    435 	printf("%s\n", cpu_model);
    436 }
    437 
    438 /*
    439  * machine dependent system variables.
    440  */
    441 int
    442 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    443 	int *name;
    444 	u_int namelen;
    445 	void *oldp;
    446 	size_t *oldlenp;
    447 	void *newp;
    448 	size_t newlen;
    449 	struct proc *p;
    450 {
    451 	dev_t consdev;
    452 
    453 	/* all sysctl names at this level are terminal */
    454 	if (namelen != 1)
    455 		return (ENOTDIR);		/* overloaded */
    456 
    457 	switch (name[0]) {
    458 	case CPU_CONSDEV:
    459 		if (cn_tab != NULL)
    460 			consdev = cn_tab->cn_dev;
    461 		else
    462 			consdev = NODEV;
    463 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
    464 		    sizeof consdev));
    465 	default:
    466 		return (EOPNOTSUPP);
    467 	}
    468 	/* NOTREACHED */
    469 }
    470 
    471 int	waittime = -1;
    472 
    473 void
    474 cpu_reboot(howto, bootstr)
    475 	volatile int howto; /* XXX to shutup GCC XXX */
    476 	char *bootstr;
    477 {
    478 	extern void doboot __P((void));
    479 
    480 	/* take a snap shot before clobbering any registers */
    481 	if (curproc && curproc->p_addr)
    482 		savectx(&curproc->p_addr->u_pcb);
    483 
    484 	/* If system is hold, just halt. */
    485 	if (cold) {
    486 		howto |= RB_HALT;
    487 		goto haltsys;
    488 	}
    489 
    490 	boothowto = howto;
    491 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
    492 		waittime = 0;
    493 		vfs_shutdown();
    494 		/*
    495 		 * If we've been adjusting the clock, the todr
    496 		 * will be out of synch; adjust it now.
    497 		 */
    498 		resettodr();
    499 	}
    500 
    501 	/* Disable interrupts. */
    502 	splhigh();
    503 
    504 	/* If rebooting and a dump is requested, do it. */
    505 	if (howto & RB_DUMP)
    506 		dumpsys();
    507 
    508 haltsys:
    509 	/* Run any shutdown hooks. */
    510 	doshutdownhooks();
    511 
    512 	/* Finally, halt/reboot the system. */
    513 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    514 		u_int8_t *pio = (void *)0x4d000000;
    515 
    516 		printf("power is going down.\n");
    517 		DELAY(100000);
    518 		pio[3] = 0x94;
    519 		pio[2] = 0 << 4;
    520 		for (;;) /* NOP */;
    521 	}
    522 	if (howto & RB_HALT) {
    523 		printf("System halted.	Hit any key to reboot.\n\n");
    524 		(void)cngetc();
    525 	}
    526 
    527 	printf("rebooting...\n");
    528 	DELAY(100000);
    529 	doboot();
    530 	/*NOTREACHED*/
    531 	while (1) ;
    532 }
    533 
    534 /*
    535  * Initialize the kernel crash dump header.
    536  */
    537 void
    538 cpu_init_kcore_hdr()
    539 {
    540 	cpu_kcore_hdr_t *h = &cpu_kcore_hdr;
    541 	struct m68k_kcore_hdr *m = &h->un._m68k;
    542 	extern char end[];
    543 
    544 	bzero(&cpu_kcore_hdr, sizeof(cpu_kcore_hdr));
    545 
    546 	/*
    547 	 * Initialize the `dispatcher' portion of the header.
    548 	 */
    549 	strcpy(h->name, machine);
    550 	h->page_size = NBPG;
    551 	h->kernbase = KERNBASE;
    552 
    553 	/*
    554 	 * Fill in information about our MMU configuration.
    555 	 */
    556 	m->mmutype	= mmutype;
    557 	m->sg_v		= SG_V;
    558 	m->sg_frame	= SG_FRAME;
    559 	m->sg_ishift	= SG_ISHIFT;
    560 	m->sg_pmask	= SG_PMASK;
    561 	m->sg40_shift1	= SG4_SHIFT1;
    562 	m->sg40_mask2	= SG4_MASK2;
    563 	m->sg40_shift2	= SG4_SHIFT2;
    564 	m->sg40_mask3	= SG4_MASK3;
    565 	m->sg40_shift3	= SG4_SHIFT3;
    566 	m->sg40_addr1	= SG4_ADDR1;
    567 	m->sg40_addr2	= SG4_ADDR2;
    568 	m->pg_v		= PG_V;
    569 	m->pg_frame	= PG_FRAME;
    570 
    571 	/*
    572 	 * Initialize pointer to kernel segment table.
    573 	 */
    574 	m->sysseg_pa = (u_int32_t)(pmap_kernel()->pm_stpa);
    575 
    576 	/*
    577 	 * Initialize relocation value such that:
    578 	 *
    579 	 *	pa = (va - KERNBASE) + reloc
    580 	 *
    581 	 * Since we're linked and loaded at the same place,
    582 	 * and the kernel is mapped va == pa, this is 0.
    583 	 */
    584 	m->reloc = 0;
    585 
    586 	/*
    587 	 * Define the end of the relocatable range.
    588 	 */
    589 	m->relocend = (u_int32_t)end;
    590 
    591 	/*
    592 	 * The luna68k has one contiguous memory segment.
    593 	 */
    594 	m->ram_segs[0].start = 0 /* lowram */;
    595 	m->ram_segs[0].size  = ctob(physmem);
    596 }
    597 
    598 /*
    599  * Compute the size of the machine-dependent crash dump header.
    600  * Returns size in disk blocks.
    601  */
    602 int
    603 cpu_dumpsize()
    604 {
    605 	int size;
    606 
    607 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t));
    608 	return (btodb(roundup(size, dbtob(1))));
    609 }
    610 
    611 /*
    612  * Called by dumpsys() to dump the machine-dependent header.
    613  */
    614 int
    615 cpu_dump(dump, blknop)
    616 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
    617 	daddr_t *blknop;
    618 {
    619 	int buf[dbtob(1) / sizeof(int)];
    620 	cpu_kcore_hdr_t *chdr;
    621 	kcore_seg_t *kseg;
    622 	int error;
    623 
    624 	kseg = (kcore_seg_t *)buf;
    625 	chdr = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(kcore_seg_t)) /
    626 	    sizeof(int)];
    627 
    628 	/* Create the segment header. */
    629 	CORE_SETMAGIC(*kseg, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
    630 	kseg->c_size = dbtob(1) - ALIGN(sizeof(kcore_seg_t));
    631 
    632 	bcopy(&cpu_kcore_hdr, chdr, sizeof(cpu_kcore_hdr_t));
    633 	error = (*dump)(dumpdev, *blknop, (caddr_t)buf, sizeof(buf));
    634 	*blknop += btodb(sizeof(buf));
    635 	return (error);
    636 }
    637 
    638 /*
    639  * These variables are needed by /sbin/savecore
    640  */
    641 u_long	dumpmag = 0x8fca0101;	/* magic number */
    642 int	dumpsize = 0;		/* pages */
    643 long	dumplo = 0;		/* blocks */
    644 
    645 /*
    646  * This is called by main to set dumplo and dumpsize.
    647  * Dumps always skip the first NBPG of disk space
    648  * in case there might be a disk label stored there.
    649  * If there is extra space, put dump at the end to
    650  * reduce the chance that swapping trashes it.
    651  */
    652 void
    653 cpu_dumpconf()
    654 {
    655 	int chdrsize;	/* size of dump header */
    656 	int nblks;	/* size of dump area */
    657 	int maj;
    658 
    659 	if (dumpdev == NODEV)
    660 		return;
    661 	maj = major(dumpdev);
    662 	if (maj < 0 || maj >= nblkdev)
    663 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
    664 	if (bdevsw[maj].d_psize == NULL)
    665 		return;
    666 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
    667 	chdrsize = cpu_dumpsize();
    668 
    669 	dumpsize = btoc(cpu_kcore_hdr.un._m68k.ram_segs[0].size);
    670 
    671 	/*
    672 	 * Check do see if we will fit.  Note we always skip the
    673 	 * first NBPG in case there is a disk label there.
    674 	 */
    675 	if (nblks < (ctod(dumpsize) + chdrsize + ctod(1))) {
    676 		dumpsize = 0;
    677 		dumplo = -1;
    678 		return;
    679 	}
    680 
    681 	/*
    682 	 * Put dump at the end of the partition.
    683 	 */
    684 	dumplo = (nblks - 1) - ctod(dumpsize) - chdrsize;
    685 }
    686 
    687 /*
    688  * Dump physical memory onto the dump device.  Called by cpu_reboot().
    689  */
    690 void
    691 dumpsys()
    692 {
    693 	daddr_t blkno;		/* current block to write */
    694 				/* dump routine */
    695 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
    696 	int pg;			/* page being dumped */
    697 	paddr_t maddr;		/* PA being dumped */
    698 	int error;		/* error code from (*dump)() */
    699 
    700 	/* XXX initialized here because of gcc lossage */
    701 	maddr = 0 /* lowram */;
    702 	pg = 0;
    703 
    704 	/* Make sure dump device is valid. */
    705 	if (dumpdev == NODEV)
    706 		return;
    707 	if (dumpsize == 0) {
    708 		cpu_dumpconf();
    709 		if (dumpsize == 0)
    710 			return;
    711 	}
    712 	if (dumplo <= 0) {
    713 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
    714 		    minor(dumpdev));
    715 		return;
    716 	}
    717 	dump = bdevsw[major(dumpdev)].d_dump;
    718 	blkno = dumplo;
    719 
    720 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
    721 	    minor(dumpdev), dumplo);
    722 
    723 	printf("dump ");
    724 
    725 	/* Write the dump header. */
    726 	error = cpu_dump(dump, &blkno);
    727 	if (error)
    728 		goto bad;
    729 
    730 	for (pg = 0; pg < dumpsize; pg++) {
    731 #define NPGMB	(1024*1024/NBPG)
    732 		/* print out how many MBs we have dumped */
    733 		if (pg && (pg % NPGMB) == 0)
    734 			printf("%d ", pg / NPGMB);
    735 #undef NPGMB
    736 		pmap_enter(pmap_kernel(), (vaddr_t)vmmap, maddr,
    737 		    VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
    738 
    739 		error = (*dump)(dumpdev, blkno, vmmap, NBPG);
    740  bad:
    741 		switch (error) {
    742 		case 0:
    743 			maddr += NBPG;
    744 			blkno += btodb(NBPG);
    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