Home | History | Annotate | Line # | Download | only in sun3x
machdep.c revision 1.29
      1 /*	$NetBSD: machdep.c,v 1.29 1998/02/05 04:57:59 gwr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988 University of Utah.
      5  * Copyright (c) 1982, 1986, 1990, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * the Systems Programming Group of the University of Utah Computer
     10  * Science Department.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	from: Utah Hdr: machdep.c 1.74 92/12/20
     41  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/map.h>
     48 #include <sys/proc.h>
     49 #include <sys/buf.h>
     50 #include <sys/reboot.h>
     51 #include <sys/conf.h>
     52 #include <sys/file.h>
     53 #include <sys/clist.h>
     54 #include <sys/callout.h>
     55 #include <sys/malloc.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/msgbuf.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/tty.h>
     60 #include <sys/mount.h>
     61 #include <sys/user.h>
     62 #include <sys/exec.h>
     63 #include <sys/core.h>
     64 #include <sys/kcore.h>
     65 #include <sys/vnode.h>
     66 #include <sys/syscallargs.h>
     67 #ifdef SYSVMSG
     68 #include <sys/msg.h>
     69 #endif
     70 #ifdef SYSVSEM
     71 #include <sys/sem.h>
     72 #endif
     73 #ifdef SYSVSHM
     74 #include <sys/shm.h>
     75 #endif
     76 #ifdef	KGDB
     77 #include <sys/kgdb.h>
     78 #endif
     79 
     80 #include <vm/vm.h>
     81 #include <vm/vm_map.h>
     82 #include <vm/vm_kern.h>
     83 #include <vm/vm_page.h>
     84 
     85 #include <sys/sysctl.h>
     86 
     87 #include <dev/cons.h>
     88 
     89 #include <machine/cpu.h>
     90 #include <machine/dvma.h>
     91 #include <machine/idprom.h>
     92 #include <machine/kcore.h>
     93 #include <machine/reg.h>
     94 #include <machine/psl.h>
     95 #include <machine/pte.h>
     96 
     97 #include <machine/db_machdep.h>
     98 
     99 #include <sun3/sun3/machdep.h>
    100 
    101 /* Defined in locore.s */
    102 extern char kernel_text[];
    103 /* Defined by the linker */
    104 extern char etext[];
    105 
    106 int	physmem;
    107 int	fputype;
    108 caddr_t	msgbufaddr;
    109 
    110 /* Virtual page frame for /dev/mem (see mem.c) */
    111 vm_offset_t vmmap;
    112 
    113 /*
    114  * safepri is a safe priority for sleep to set for a spin-wait
    115  * during autoconfiguration or after a panic.
    116  */
    117 int	safepri = PSL_LOWIPL;
    118 
    119 /*
    120  * Declare these as initialized data so we can patch them.
    121  */
    122 int	nswbuf = 0;
    123 #ifdef	NBUF
    124 int	nbuf = NBUF;
    125 #else
    126 int	nbuf = 0;
    127 #endif
    128 #ifdef	BUFPAGES
    129 int	bufpages = BUFPAGES;
    130 #else
    131 int	bufpages = 0;
    132 #endif
    133 
    134 u_char cpu_machine_id = 0;
    135 char *cpu_string = NULL;
    136 int cpu_has_vme = 0;
    137 int has_iocache = 0;
    138 
    139 static void identifycpu __P((void));
    140 static void initcpu __P((void));
    141 
    142 /*
    143  * Console initialization: called early on from main,
    144  * before vm init or cpu_startup.  This system is able
    145  * to use the console for output immediately (via PROM)
    146  * but can not use it for input until after this point.
    147  */
    148 void
    149 consinit()
    150 {
    151 
    152 	/*
    153 	 * Switch from the PROM console (output only)
    154 	 * to our own console driver.
    155 	 */
    156 	cninit();
    157 
    158 #ifdef KGDB
    159 	/* XXX - Ask on console for kgdb_dev? */
    160 	/* Note: this will just return if kgdb_dev<0 */
    161 	if (boothowto & RB_KDB)
    162 		kgdb_connect(1);
    163 #endif
    164 #ifdef DDB
    165 	/* Now that we have a console, we can stop in DDB. */
    166 	db_machine_init();
    167 	ddb_init();
    168 	if (boothowto & RB_KDB)
    169 		Debugger();
    170 #endif DDB
    171 }
    172 
    173 /*
    174  * allocsys() - Private routine used by cpu_startup() below.
    175  *
    176  * Allocate space for system data structures.  We are given
    177  * a starting virtual address and we return a final virtual
    178  * address; along the way we set each data structure pointer.
    179  *
    180  * We call allocsys() with 0 to find out how much space we want,
    181  * allocate that much and fill it with zeroes, and then call
    182  * allocsys() again with the correct base virtual address.
    183  */
    184 #define	valloc(name, type, num) \
    185 	v = (caddr_t)(((name) = (type *)v) + (num))
    186 static caddr_t allocsys __P((caddr_t));
    187 static caddr_t
    188 allocsys(v)
    189 	register caddr_t v;
    190 {
    191 
    192 #ifdef REAL_CLISTS
    193 	valloc(cfree, struct cblock, nclist);
    194 #endif
    195 	valloc(callout, struct callout, ncallout);
    196 #ifdef SYSVSHM
    197 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
    198 #endif
    199 #ifdef SYSVSEM
    200 	valloc(sema, struct semid_ds, seminfo.semmni);
    201 	valloc(sem, struct sem, seminfo.semmns);
    202 	/* This is pretty disgusting! */
    203 	valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
    204 #endif
    205 #ifdef SYSVMSG
    206 	valloc(msgpool, char, msginfo.msgmax);
    207 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
    208 	valloc(msghdrs, struct msg, msginfo.msgtql);
    209 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
    210 #endif
    211 
    212 	/*
    213 	 * Determine how many buffers to allocate. We allocate
    214 	 * the BSD standard of use 10% of memory for the first 2 Meg,
    215 	 * 5% of remaining. Insure a minimum of 16 buffers.
    216 	 * Allocate 1/2 as many swap buffer headers as file i/o buffers.
    217 	 */
    218 	if (bufpages == 0) {
    219 		/* We always have more than 2MB of memory. */
    220 		bufpages = ((btoc(2 * 1024 * 1024) + physmem) /
    221 		            (20 * CLSIZE));
    222 	}
    223 	if (nbuf == 0) {
    224 		nbuf = bufpages;
    225 		if (nbuf < 16)
    226 			nbuf = 16;
    227 	}
    228 	if (nswbuf == 0) {
    229 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
    230 		if (nswbuf > 256)
    231 			nswbuf = 256;		/* sanity */
    232 	}
    233 	valloc(swbuf, struct buf, nswbuf);
    234 	valloc(buf, struct buf, nbuf);
    235 	return v;
    236 }
    237 #undef	valloc
    238 
    239 /*
    240  * cpu_startup: allocate memory for variable-sized tables,
    241  * initialize cpu, and do autoconfiguration.
    242  *
    243  * This is called early in init_main.c:main(), after the
    244  * kernel memory allocator is ready for use, but before
    245  * the creation of processes 1,2, and mountroot, etc.
    246  */
    247 void
    248 cpu_startup()
    249 {
    250 	caddr_t v;
    251 	int sz, i;
    252 	vm_size_t size;
    253 	int base, residual;
    254 	vm_offset_t minaddr, maxaddr;
    255 
    256 	/*
    257 	 * Initialize message buffer (for kernel printf).
    258 	 * This is put in physical page zero so it will
    259 	 * always be in the same place after a reboot.
    260 	 * Its mapping was prepared in pmap_bootstrap().
    261 	 * Also, offset some to avoid PROM scribbles.
    262 	 */
    263 	v = (caddr_t) KERNBASE;
    264 	msgbufaddr = (caddr_t)(v + MSGBUFOFF);
    265 	initmsgbuf(msgbufaddr, MSGBUFSIZE);
    266 
    267 	/*
    268 	 * Good {morning,afternoon,evening,night}.
    269 	 */
    270 	printf(version);
    271 	identifycpu();
    272 	initfpu();	/* also prints FPU type */
    273 
    274 	size = ptoa(physmem);
    275 	printf("real  mem = %dK (0x%lx)\n", (size >> 10), size);
    276 
    277 	/*
    278 	 * Find out how much space we need, allocate it,
    279 	 * and then give everything true virtual addresses.
    280 	 */
    281 	sz = (int)allocsys((caddr_t)0);
    282 	if ((v = (caddr_t)kmem_alloc(kernel_map, round_page(sz))) == 0)
    283 		panic("startup: no room for tables");
    284 	if (allocsys(v) - v != sz)
    285 		panic("startup: table size inconsistency");
    286 
    287 	/*
    288 	 * Now allocate buffers proper.  They are different than the above
    289 	 * in that they usually occupy more virtual memory than physical.
    290 	 */
    291 	size = MAXBSIZE * nbuf;
    292 	buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
    293 				   &maxaddr, size, TRUE);
    294 	minaddr = (vm_offset_t)buffers;
    295 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
    296 			&minaddr, size, FALSE) != KERN_SUCCESS)
    297 		panic("startup: cannot allocate buffers");
    298 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    299 		/* don't want to alloc more physical mem than needed */
    300 		bufpages = btoc(MAXBSIZE) * nbuf;
    301 	}
    302 	base = bufpages / nbuf;
    303 	residual = bufpages % nbuf;
    304 	for (i = 0; i < nbuf; i++) {
    305 		vm_size_t curbufsize;
    306 		vm_offset_t curbuf;
    307 
    308 		/*
    309 		 * First <residual> buffers get (base+1) physical pages
    310 		 * allocated for them.  The rest get (base) physical pages.
    311 		 *
    312 		 * The rest of each buffer occupies virtual space,
    313 		 * but has no physical memory allocated for it.
    314 		 */
    315 		curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
    316 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
    317 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
    318 		vm_map_simplify(buffer_map, curbuf);
    319 	}
    320 
    321 	/*
    322 	 * Allocate a submap for exec arguments.  This map effectively
    323 	 * limits the number of processes exec'ing at any time.
    324 	 */
    325 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    326 				 16*NCARGS, TRUE);
    327 
    328 	/*
    329 	 * We don't use a submap for physio, and use a separate map
    330 	 * for DVMA allocations.  Our vmapbuf just maps pages into
    331 	 * the kernel map (any kernel mapping is OK) and then the
    332 	 * device drivers clone the kernel mappings into DVMA space.
    333 	 */
    334 
    335 	/*
    336 	 * Finally, allocate mbuf cluster submap.
    337 	 */
    338 	mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
    339 			       VM_MBUF_SIZE, FALSE);
    340 
    341 	/*
    342 	 * Initialize callouts
    343 	 */
    344 	callfree = callout;
    345 	for (i = 1; i < ncallout; i++)
    346 		callout[i-1].c_next = &callout[i];
    347 	callout[i-1].c_next = NULL;
    348 
    349 	size = ptoa(cnt.v_free_count);
    350 	printf("avail mem = %dK (0x%lx)\n", (size >> 10), size);
    351 	printf("using %d buffers containing %d bytes of memory\n",
    352 		   nbuf, bufpages * CLBYTES);
    353 
    354 	/*
    355 	 * Tell the VM system that writing to kernel text isn't allowed.
    356 	 * If we don't, we might end up COW'ing the text segment!
    357 	 */
    358 	if (vm_map_protect(kernel_map, (vm_offset_t) kernel_text,
    359 					   trunc_page((vm_offset_t) etext),
    360 					   VM_PROT_READ|VM_PROT_EXECUTE, TRUE)
    361 		!= KERN_SUCCESS)
    362 		panic("can't protect kernel text");
    363 
    364 	/*
    365 	 * Allocate a virtual page (for use by /dev/mem)
    366 	 * This page is handed to pmap_enter() therefore
    367 	 * it has to be in the normal kernel VA range.
    368 	 */
    369 	vmmap = kmem_alloc_wait(kernel_map, NBPG);
    370 
    371 	/*
    372 	 * Create the DVMA maps.
    373 	 */
    374 	dvma_init();
    375 
    376 	/*
    377 	 * Set up CPU-specific registers, cache, etc.
    378 	 */
    379 	initcpu();
    380 
    381 	/*
    382 	 * Set up buffers, so they can be used to read disk labels.
    383 	 */
    384 	bufinit();
    385 
    386 	/*
    387 	 * Configure the system.
    388 	 */
    389 	configure();
    390 }
    391 
    392 /*
    393  * Set registers on exec.
    394  */
    395 void
    396 setregs(p, pack, stack)
    397 	register struct proc *p;
    398 	struct exec_package *pack;
    399 	u_long stack;
    400 {
    401 	struct trapframe *tf = (struct trapframe *)p->p_md.md_regs;
    402 
    403 	tf->tf_sr = PSL_USERSET;
    404 	tf->tf_pc = pack->ep_entry & ~1;
    405 	tf->tf_regs[D0] = 0;
    406 	tf->tf_regs[D1] = 0;
    407 	tf->tf_regs[D2] = 0;
    408 	tf->tf_regs[D3] = 0;
    409 	tf->tf_regs[D4] = 0;
    410 	tf->tf_regs[D5] = 0;
    411 	tf->tf_regs[D6] = 0;
    412 	tf->tf_regs[D7] = 0;
    413 	tf->tf_regs[A0] = 0;
    414 	tf->tf_regs[A1] = 0;
    415 	tf->tf_regs[A2] = (int)PS_STRINGS;
    416 	tf->tf_regs[A3] = 0;
    417 	tf->tf_regs[A4] = 0;
    418 	tf->tf_regs[A5] = 0;
    419 	tf->tf_regs[A6] = 0;
    420 	tf->tf_regs[SP] = stack;
    421 
    422 	/* restore a null state frame */
    423 	p->p_addr->u_pcb.pcb_fpregs.fpf_null = 0;
    424 	if (fputype)
    425 		m68881_restore(&p->p_addr->u_pcb.pcb_fpregs);
    426 
    427 	p->p_md.md_flags = 0;
    428 }
    429 
    430 /*
    431  * Info for CTL_HW
    432  */
    433 char	machine[16] = MACHINE;	/* from <machine/param.h> */
    434 char	cpu_model[120];
    435 
    436 /*
    437  * XXX - Should empirically estimate the divisor...
    438  * Note that the value of delay_divisor is roughly
    439  * 2048 / cpuclock	(where cpuclock is in MHz).
    440  */
    441 int delay_divisor = 62;		/* assume the fastest (33 MHz) */
    442 
    443 void
    444 identifycpu()
    445 {
    446 	u_char machtype;
    447 
    448 	machtype = identity_prom.idp_machtype;
    449 	if ((machtype & IDM_ARCH_MASK) != IDM_ARCH_SUN3X) {
    450 		printf("Bad IDPROM arch!\n");
    451 		sunmon_abort();
    452 	}
    453 
    454 	cpu_machine_id = machtype;
    455 	switch (cpu_machine_id) {
    456 
    457 	case SUN3X_MACH_80:
    458 		cpu_string = "80";  	/* Hydra */
    459 		delay_divisor = 102;	/* 20 MHz */
    460 		cpu_has_vme = FALSE;
    461 		break;
    462 
    463 	case SUN3X_MACH_470:
    464 		cpu_string = "470"; 	/* Pegasus */
    465 		delay_divisor = 62; 	/* 33 MHz */
    466 		cpu_has_vme = TRUE;
    467 		break;
    468 
    469 	default:
    470 		printf("unknown sun3x model\n");
    471 		sunmon_abort();
    472 	}
    473 
    474 	/* Other stuff? (VAC, mc6888x version, etc.) */
    475 	sprintf(cpu_model, "Sun-3X (3/%s)", cpu_string);
    476 
    477 	printf("Model: %s\n", cpu_model);
    478 }
    479 
    480 /*
    481  * machine dependent system variables.
    482  */
    483 int
    484 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    485 	int *name;
    486 	u_int namelen;
    487 	void *oldp;
    488 	size_t *oldlenp;
    489 	void *newp;
    490 	size_t newlen;
    491 	struct proc *p;
    492 {
    493 	int error;
    494 	dev_t consdev;
    495 
    496 	/* all sysctl names at this level are terminal */
    497 	if (namelen != 1)
    498 		return (ENOTDIR);		/* overloaded */
    499 
    500 	switch (name[0]) {
    501 	case CPU_CONSDEV:
    502 		if (cn_tab != NULL)
    503 			consdev = cn_tab->cn_dev;
    504 		else
    505 			consdev = NODEV;
    506 		error = sysctl_rdstruct(oldp, oldlenp, newp,
    507 		    &consdev, sizeof consdev);
    508 		break;
    509 
    510 #if 0	/* XXX - Not yet... */
    511 	case CPU_ROOT_DEVICE:
    512 		error = sysctl_rdstring(oldp, oldlenp, newp, root_device);
    513 		break;
    514 
    515 	case CPU_BOOTED_KERNEL:
    516 		error = sysctl_rdstring(oldp, oldlenp, newp, booted_kernel);
    517 		break;
    518 #endif
    519 
    520 	default:
    521 		error = EOPNOTSUPP;
    522 	}
    523 	return (error);
    524 }
    525 
    526 /* See: sig_machdep.c */
    527 
    528 /*
    529  * Do a sync in preparation for a reboot.
    530  * XXX - This could probably be common code.
    531  * XXX - And now, most of it is in vfs_shutdown()
    532  * XXX - Put waittime checks in there too?
    533  */
    534 int waittime = -1;	/* XXX - Who else looks at this? -gwr */
    535 static void
    536 reboot_sync __P((void))
    537 {
    538 
    539 	/* Check waittime here to localize its use to this function. */
    540 	if (waittime >= 0)
    541 		return;
    542 	waittime = 0;
    543 	vfs_shutdown();
    544 }
    545 
    546 /*
    547  * Common part of the BSD and SunOS reboot system calls.
    548  */
    549 __dead void
    550 cpu_reboot(howto, user_boot_string)
    551 	int howto;
    552 	char *user_boot_string;
    553 {
    554 	/* Note: this string MUST be static! */
    555 	static char bootstr[128];
    556 	char *p;
    557 
    558 	/* If system is cold, just halt. (early panic?) */
    559 	if (cold)
    560 		goto haltsys;
    561 
    562 	/* Un-blank the screen if appropriate. */
    563 	cnpollc(1);
    564 
    565 	if ((howto & RB_NOSYNC) == 0) {
    566 		reboot_sync();
    567 		/*
    568 		 * If we've been adjusting the clock, the todr
    569 		 * will be out of synch; adjust it now.
    570 		 *
    571 		 * XXX - However, if the kernel has been sitting in ddb,
    572 		 * the time will be way off, so don't set the HW clock!
    573 		 * XXX - Should do sanity check against HW clock. -gwr
    574 		 */
    575 		/* resettodr(); */
    576 	}
    577 
    578 	/* Disable interrupts. */
    579 	splhigh();
    580 
    581 	/* Write out a crash dump if asked. */
    582 	if (howto & RB_DUMP)
    583 		dumpsys();
    584 
    585 	/* run any shutdown hooks */
    586 	doshutdownhooks();
    587 
    588 	if (howto & RB_HALT) {
    589 	haltsys:
    590 		printf("Kernel halted.\n");
    591 #if 0
    592 		/*
    593 		 * This calls the PROM monitor "exit_to_mon" function
    594 		 * which appears to have problems...  SunOS uses the
    595 		 * "abort" function when you halt (bug work-around?)
    596 		 * so we might as well do the same.
    597 		 */
    598 		sunmon_halt(); /* provokes PROM monitor bug */
    599 #else
    600 		sunmon_abort();
    601 #endif
    602 	}
    603 
    604 	/*
    605 	 * Automatic reboot.
    606 	 */
    607 	if (user_boot_string)
    608 		strncpy(bootstr, user_boot_string, sizeof(bootstr));
    609 	else {
    610 		/*
    611 		 * Build our own boot string with an empty
    612 		 * boot device/file and (maybe) some flags.
    613 		 * The PROM will supply the device/file name.
    614 		 */
    615 		p = bootstr;
    616 		*p = '\0';
    617 		if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
    618 			/* Append the boot flags. */
    619 			*p++ = ' ';
    620 			*p++ = '-';
    621 			if (howto & RB_KDB)
    622 				*p++ = 'd';
    623 			if (howto & RB_ASKNAME)
    624 				*p++ = 'a';
    625 			if (howto & RB_SINGLE)
    626 				*p++ = 's';
    627 			*p = '\0';
    628 		}
    629 	}
    630 	printf("Kernel rebooting...\n");
    631 	sunmon_reboot(bootstr);
    632 	for (;;) ;
    633 	/*NOTREACHED*/
    634 }
    635 
    636 /*
    637  * These variables are needed by /sbin/savecore
    638  */
    639 u_long	dumpmag = 0x8fca0101;	/* magic number */
    640 int 	dumpsize = 0;		/* pages */
    641 long	dumplo = 0; 		/* blocks */
    642 
    643 /*
    644  * This is called by main to set dumplo, dumpsize.
    645  * Dumps always skip the first CLBYTES of disk space
    646  * in case there might be a disk label stored there.
    647  * If there is extra space, put dump at the end to
    648  * reduce the chance that swapping trashes it.
    649  */
    650 void
    651 cpu_dumpconf()
    652 {
    653 	int nblks;	/* size of dump area */
    654 	int maj;
    655 	int (*getsize)__P((dev_t));
    656 
    657 	/* Validate space in page zero for the kcore header. */
    658 	if (MSGBUFOFF < (sizeof(kcore_seg_t) + sizeof(cpu_kcore_hdr_t)))
    659 		panic("cpu_dumpconf: MSGBUFOFF too small");
    660 
    661 	if (dumpdev == NODEV)
    662 		return;
    663 
    664 	maj = major(dumpdev);
    665 	if (maj < 0 || maj >= nblkdev)
    666 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
    667 	getsize = bdevsw[maj].d_psize;
    668 	if (getsize == NULL)
    669 		return;
    670 	nblks = (*getsize)(dumpdev);
    671 	if (nblks <= ctod(1))
    672 		return;
    673 
    674 	/* Position dump image near end of space, page aligned. */
    675 	dumpsize = physmem; 	/* pages */
    676 	dumplo = nblks - ctod(dumpsize);
    677 	dumplo &= ~(ctod(1)-1);
    678 
    679 	/* If it does not fit, truncate it by moving dumplo. */
    680 	/* Note: Must force signed comparison. */
    681 	if (dumplo < ((long)ctod(1))) {
    682 		dumplo = ctod(1);
    683 		dumpsize = dtoc(nblks - dumplo);
    684 	}
    685 }
    686 
    687 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */
    688 struct pcb dumppcb;
    689 
    690 /*
    691  * Write a crash dump.  The format while in swap is:
    692  *   kcore_seg_t cpu_hdr;
    693  *   cpu_kcore_hdr_t cpu_data;
    694  *   padding (NBPG-sizeof(kcore_seg_t))
    695  *   pagemap (2*NBPG)
    696  *   physical memory...
    697  */
    698 void
    699 dumpsys()
    700 {
    701 	struct bdevsw *dsw;
    702 	kcore_seg_t	*kseg_p;
    703 	cpu_kcore_hdr_t *chdr_p;
    704 	struct sun3x_kcore_hdr *sh;
    705 	phys_ram_seg_t *crs_p;
    706 	char *vaddr;
    707 	vm_offset_t paddr;
    708 	int psize, todo, seg, segsz;
    709 	daddr_t blkno;
    710 	int error = 0;
    711 
    712 	msgbufmapped = 0;
    713 	if (dumpdev == NODEV)
    714 		return;
    715 
    716 	/*
    717 	 * For dumps during autoconfiguration,
    718 	 * if dump device has already configured...
    719 	 */
    720 	if (dumpsize == 0)
    721 		cpu_dumpconf();
    722 	if (dumplo <= 0) {
    723 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
    724 		    minor(dumpdev));
    725 		return;
    726 	}
    727 	savectx(&dumppcb);
    728 
    729 	dsw = &bdevsw[major(dumpdev)];
    730 	psize = (*(dsw->d_psize))(dumpdev);
    731 	if (psize == -1) {
    732 		printf("dump area unavailable\n");
    733 		return;
    734 	}
    735 
    736 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
    737 	    minor(dumpdev), dumplo);
    738 
    739 	/*
    740 	 * We put the dump header is in physical page zero,
    741 	 * so there is no extra work here to write it out.
    742 	 * All we do is initialize the header.
    743 	 */
    744 
    745 	/* Set pointers to all three parts. */
    746 	kseg_p = (kcore_seg_t *)KERNBASE;
    747 	chdr_p = (cpu_kcore_hdr_t *) (kseg_p + 1);
    748 	sh = &chdr_p->un._sun3x;
    749 
    750 	/* Fill in kcore_seg_t part. */
    751 	CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
    752 	kseg_p->c_size = sizeof(*chdr_p);
    753 
    754 	/* Fill in cpu_kcore_hdr_t part. */
    755 	/* Can NOT use machine[] as the name! */
    756 	strncpy(chdr_p->name, "sun3x", sizeof(chdr_p->name));
    757 	chdr_p->page_size = NBPG;
    758 	chdr_p->kernbase = KERNBASE;
    759 
    760 	/* Fill in the sun3x_kcore_hdr part. */
    761 	pmap_kcore_hdr(sh);
    762 
    763 	/*
    764 	 * Now dump physical memory.  Note that physical memory
    765 	 * might NOT be congiguous, so do it by segments.
    766 	 */
    767 
    768 	blkno = dumplo;
    769 	todo = dumpsize;	/* pages */
    770 	vaddr = (char*)vmmap;	/* Borrow /dev/mem VA */
    771 
    772 	for (seg = 0; seg < SUN3X_NPHYS_RAM_SEGS; seg++) {
    773 		crs_p = &sh->ram_segs[seg];
    774 		paddr = crs_p->start;
    775 		segsz = crs_p->size;
    776 		/*
    777 		 * Our header lives in the first little bit of
    778 		 * physical memory (not written separately), so
    779 		 * we have to adjust the first ram segment size
    780 		 * and start address to reflect the stolen RAM.
    781 		 * (Nothing interesing in that RAM anyway 8^).
    782 		 */
    783 		if (seg == 0) {
    784 			int adj = sizeof(*kseg_p) + sizeof(*chdr_p);
    785 			crs_p->start += adj;
    786 			crs_p->size  -= adj;
    787 		}
    788 
    789 		while (todo && (segsz > 0)) {
    790 
    791 			/* Print pages left after every 16. */
    792 			if ((todo & 0xf) == 0)
    793 				printf("\r%4d", todo);
    794 
    795 			/* Make a temporary mapping for the page. */
    796 			pmap_enter(pmap_kernel(), vmmap, paddr | PMAP_NC,
    797 					   VM_PROT_READ, FALSE);
    798 			error = (*dsw->d_dump)(dumpdev, blkno, vaddr, NBPG);
    799 			pmap_remove(pmap_kernel(), vmmap, vmmap + NBPG);
    800 			if (error)
    801 				goto fail;
    802 			paddr += NBPG;
    803 			segsz -= NBPG;
    804 			blkno += btodb(NBPG);
    805 			todo--;
    806 		}
    807 	}
    808 	printf("\rdump succeeded\n");
    809 	return;
    810 fail:
    811 	printf(" dump error=%d\n", error);
    812 }
    813 
    814 static void
    815 initcpu()
    816 {
    817 	/* XXX: Enable RAM parity/ECC checking? */
    818 	/* XXX: parityenable(); */
    819 
    820 #ifdef	HAVECACHE
    821 	cache_enable();
    822 #endif
    823 }
    824 
    825 /* straptrap() in trap.c */
    826 
    827 /* from hp300: badaddr() */
    828 /* peek_byte(), peek_word() moved to bus_subr.c */
    829 
    830 /* XXX: parityenable() ? */
    831 /* regdump() moved to regdump.c */
    832 
    833 /*
    834  * cpu_exec_aout_makecmds():
    835  *	cpu-dependent a.out format hook for execve().
    836  *
    837  * Determine if the given exec package refers to something which we
    838  * understand and, if so, set up the vmcmds for it.
    839  */
    840 int
    841 cpu_exec_aout_makecmds(p, epp)
    842 	struct proc *p;
    843 	struct exec_package *epp;
    844 {
    845 	return ENOEXEC;
    846 }
    847