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