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