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