Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.31
      1 /*	$NetBSD: machdep.c,v 1.31 1996/06/15 03:55:17 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/signalvar.h>
     33 #include <sys/kernel.h>
     34 #include <sys/map.h>
     35 #include <sys/proc.h>
     36 #include <sys/buf.h>
     37 #include <sys/reboot.h>
     38 #include <sys/device.h>
     39 #include <sys/conf.h>
     40 #include <sys/file.h>
     41 #ifdef REAL_CLISTS
     42 #include <sys/clist.h>
     43 #endif
     44 #include <sys/callout.h>
     45 #include <sys/malloc.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/msgbuf.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/tty.h>
     50 #include <sys/user.h>
     51 #include <sys/exec.h>
     52 #include <sys/exec_ecoff.h>
     53 #include <sys/sysctl.h>
     54 #ifdef SYSVMSG
     55 #include <sys/msg.h>
     56 #endif
     57 #ifdef SYSVSEM
     58 #include <sys/sem.h>
     59 #endif
     60 #ifdef SYSVSHM
     61 #include <sys/shm.h>
     62 #endif
     63 
     64 #include <sys/mount.h>
     65 #include <sys/syscallargs.h>
     66 
     67 #include <vm/vm_kern.h>
     68 
     69 #include <dev/cons.h>
     70 
     71 #include <machine/cpu.h>
     72 #include <machine/reg.h>
     73 #include <machine/rpb.h>
     74 #include <machine/prom.h>
     75 
     76 #ifdef DEC_3000_500
     77 #include <alpha/alpha/dec_3000_500.h>
     78 #endif
     79 #ifdef DEC_3000_300
     80 #include <alpha/alpha/dec_3000_300.h>
     81 #endif
     82 #ifdef DEC_2100_A50
     83 #include <alpha/alpha/dec_2100_a50.h>
     84 #endif
     85 #ifdef DEC_KN20AA
     86 #include <alpha/alpha/dec_kn20aa.h>
     87 #endif
     88 #ifdef DEC_AXPPCI_33
     89 #include <alpha/alpha/dec_axppci_33.h>
     90 #endif
     91 #ifdef DEC_21000
     92 #include <alpha/alpha/dec_21000.h>
     93 #endif
     94 
     95 #include <net/netisr.h>
     96 #include "ether.h"
     97 
     98 #include "le_ioasic.h"			/* for le_iomem creation */
     99 
    100 vm_map_t buffer_map;
    101 
    102 void dumpsys __P((void));
    103 
    104 /*
    105  * Declare these as initialized data so we can patch them.
    106  */
    107 int	nswbuf = 0;
    108 #ifdef	NBUF
    109 int	nbuf = NBUF;
    110 #else
    111 int	nbuf = 0;
    112 #endif
    113 #ifdef	BUFPAGES
    114 int	bufpages = BUFPAGES;
    115 #else
    116 int	bufpages = 0;
    117 #endif
    118 int	msgbufmapped = 0;	/* set when safe to use msgbuf */
    119 int	maxmem;			/* max memory per process */
    120 
    121 int	totalphysmem;		/* total amount of physical memory in system */
    122 int	physmem;		/* physical memory used by NetBSD + some rsvd */
    123 int	firstusablepage;	/* first usable memory page */
    124 int	lastusablepage;		/* last usable memory page */
    125 int	resvmem;		/* amount of memory reserved for PROM */
    126 int	unusedmem;		/* amount of memory for OS that we don't use */
    127 int	unknownmem;		/* amount of memory with an unknown use */
    128 
    129 int	cputype;		/* system type, from the RPB */
    130 
    131 /*
    132  * XXX We need an address to which we can assign things so that they
    133  * won't be optimized away because we didn't use the value.
    134  */
    135 u_int32_t no_optimize;
    136 
    137 /* the following is used externally (sysctl_hw) */
    138 char	machine[] = "alpha";
    139 char	cpu_model[128];
    140 char	*model_names[] = {
    141 	"UNKNOWN (0)",
    142 	"Alpha Demonstration Unit",
    143 	"DEC 4000 (\"Cobra\")",
    144 	"DEC 7000 (\"Ruby\")",
    145 	"DEC 3000/500 (\"Flamingo\") family",
    146 	"UNKNOWN (5)",
    147 	"DEC 2000/300 (\"Jensen\")",
    148 	"DEC 3000/300 (\"Pelican\")",
    149 	"UNKNOWN (8)",
    150 	"DEC 2100/A500 (\"Sable\")",
    151 	"AXPvme 64",
    152 	"AXPpci 33 (\"NoName\")",
    153 	"DEC 21000 (\"TurboLaser\")",
    154 	"DEC 2100/A50 (\"Avanti\") family",
    155 	"Mustang",
    156 	"DEC KN20AA",
    157 	"UNKNOWN (16)",
    158 	"DEC 1000 (\"Mikasa\")",
    159 };
    160 int	nmodel_names = sizeof model_names/sizeof model_names[0];
    161 
    162 struct	user *proc0paddr;
    163 
    164 /* Number of machine cycles per microsecond */
    165 u_int64_t	cycles_per_usec;
    166 
    167 /* some memory areas for device DMA.  "ick." */
    168 caddr_t		le_iomem;		/* XXX iomem for LANCE DMA */
    169 
    170 /* Interrupt vectors (in locore) */
    171 extern int XentInt(), XentArith(), XentMM(), XentIF(), XentUna(), XentSys();
    172 
    173 /* number of cpus in the box.  really! */
    174 int		ncpus;
    175 
    176 /* various CPU-specific functions. */
    177 char		*(*cpu_modelname) __P((void));
    178 void		(*cpu_consinit) __P((void));
    179 void		(*cpu_device_register) __P((struct device *dev, void *aux));
    180 char		*cpu_iobus;
    181 
    182 char boot_flags[64];
    183 
    184 /* for cpu_sysctl() */
    185 char		root_device[17];
    186 
    187 int
    188 alpha_init(pfn, ptb)
    189 	u_long pfn;		/* first free PFN number */
    190 	u_long ptb;		/* PFN of current level 1 page table */
    191 {
    192 	extern char _end[];
    193 	caddr_t start, v;
    194 	struct mddt *mddtp;
    195 	int i, mddtweird;
    196 	char *p;
    197 
    198 	/*
    199 	 * Turn off interrupts and floating point.
    200 	 * Make sure the instruction and data streams are consistent.
    201 	 */
    202 	(void)splhigh();
    203 	pal_wrfen(0);
    204 	TBIA();
    205 	IMB();
    206 
    207 	/*
    208 	 * get address of the restart block, while we the bootstrap
    209 	 * mapping is still around.
    210 	 */
    211 	hwrpb = (struct rpb *) phystok0seg(*(struct rpb **)HWRPB_ADDR);
    212 
    213 	/*
    214 	 * Remember how many cycles there are per microsecond,
    215 	 * so that we can use delay().  Round up, for safety.
    216 	 */
    217 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
    218 
    219 	/*
    220 	 * Init the PROM interface, so we can use printf
    221 	 * until PROM mappings go away in consinit.
    222 	 */
    223 	init_prom_interface();
    224 
    225 	/*
    226 	 * Point interrupt/exception vectors to our own.
    227 	 */
    228 	pal_wrent(XentInt, 0);
    229 	pal_wrent(XentArith, 1);
    230 	pal_wrent(XentMM, 2);
    231 	pal_wrent(XentIF, 3);
    232 	pal_wrent(XentUna, 4);
    233 	pal_wrent(XentSys, 5);
    234 
    235 	/*
    236 	 * Find out how much memory is available, by looking at
    237 	 * the memory cluster descriptors.  This also tries to do
    238 	 * its best to detect things things that have never been seen
    239 	 * before...
    240 	 *
    241 	 * XXX Assumes that the first "system" cluster is the
    242 	 * only one we can use. Is the second (etc.) system cluster
    243 	 * (if one happens to exist) guaranteed to be contiguous?  or...?
    244 	 */
    245 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
    246 
    247 	/*
    248 	 * BEGIN MDDT WEIRDNESS CHECKING
    249 	 */
    250 	mddtweird = 0;
    251 
    252 #define cnt	 mddtp->mddt_cluster_cnt
    253 #define	usage(n) mddtp->mddt_clusters[(n)].mddt_usage
    254 	if (cnt != 2 && cnt != 3) {
    255 		printf("WARNING: weird number (%d) of mem clusters\n", cnt);
    256 		mddtweird = 1;
    257 	} else if (usage(0) != MDDT_PALCODE ||
    258 		   usage(1) != MDDT_SYSTEM ||
    259 	           (cnt == 3 && usage(2) != MDDT_PALCODE)) {
    260 		mddtweird = 1;
    261 		printf("WARNING: %d mem clusters, but weird config\n", cnt);
    262 	}
    263 
    264 	for (i = 0; i < cnt; i++) {
    265 		if ((usage(i) & MDDT_mbz) != 0) {
    266 			printf("WARNING: mem cluster %d has weird usage %lx\n",
    267 			    i, usage(i));
    268 			mddtweird = 1;
    269 		}
    270 		if (mddtp->mddt_clusters[i].mddt_pg_cnt == 0) {
    271 			printf("WARNING: mem cluster %d has pg cnt == 0\n", i);
    272 			mddtweird = 1;
    273 		}
    274 		/* XXX other things to check? */
    275 	}
    276 #undef cnt
    277 #undef usage
    278 
    279 	if (mddtweird) {
    280 		printf("\n");
    281 		printf("complete memory cluster information:\n");
    282 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    283 			printf("mddt %d:\n", i);
    284 			printf("\tpfn %lx\n",
    285 			    mddtp->mddt_clusters[i].mddt_pfn);
    286 			printf("\tcnt %lx\n",
    287 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
    288 			printf("\ttest %lx\n",
    289 			    mddtp->mddt_clusters[i].mddt_pg_test);
    290 			printf("\tbva %lx\n",
    291 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
    292 			printf("\tbpa %lx\n",
    293 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
    294 			printf("\tbcksum %lx\n",
    295 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
    296 			printf("\tusage %lx\n",
    297 			    mddtp->mddt_clusters[i].mddt_usage);
    298 		}
    299 		printf("\n");
    300 	}
    301 	/*
    302 	 * END MDDT WEIRDNESS CHECKING
    303 	 */
    304 
    305 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    306 		totalphysmem += mddtp->mddt_clusters[i].mddt_pg_cnt;
    307 #define	usage(n) mddtp->mddt_clusters[(n)].mddt_usage
    308 #define	pgcnt(n) mddtp->mddt_clusters[(n)].mddt_pg_cnt
    309 		if ((usage(i) & MDDT_mbz) != 0)
    310 			unknownmem += pgcnt(i);
    311 		else if ((usage(i) & ~MDDT_mbz) == MDDT_PALCODE)
    312 			resvmem += pgcnt(i);
    313 		else if ((usage(i) & ~MDDT_mbz) == MDDT_SYSTEM) {
    314 			/*
    315 			 * assumes that the system cluster listed is
    316 			 * one we're in...
    317 			 */
    318 			if (physmem != resvmem) {
    319 				physmem += pgcnt(i);
    320 				firstusablepage =
    321 				    mddtp->mddt_clusters[i].mddt_pfn;
    322 				lastusablepage = firstusablepage + pgcnt(i) - 1;
    323 			} else
    324 				unusedmem += pgcnt(i);
    325 		}
    326 #undef usage
    327 #undef pgcnt
    328 	}
    329 	if (totalphysmem == 0)
    330 		panic("can't happen: system seems to have no memory!");
    331 	maxmem = physmem;
    332 
    333 #if 0
    334 	printf("totalphysmem = %d\n", totalphysmem);
    335 	printf("physmem = %d\n", physmem);
    336 	printf("firstusablepage = %d\n", firstusablepage);
    337 	printf("lastusablepage = %d\n", lastusablepage);
    338 	printf("resvmem = %d\n", resvmem);
    339 	printf("unusedmem = %d\n", unusedmem);
    340 	printf("unknownmem = %d\n", unknownmem);
    341 #endif
    342 
    343 	/*
    344 	 * find out this CPU's page size
    345 	 */
    346 	PAGE_SIZE = hwrpb->rpb_page_size;
    347 	if (PAGE_SIZE != 8192)
    348 		panic("page size %d != 8192?!", PAGE_SIZE);
    349 
    350 	v = (caddr_t)alpha_round_page(_end);
    351 	/*
    352 	 * Init mapping for u page(s) for proc 0
    353 	 */
    354 	start = v;
    355 	curproc->p_addr = proc0paddr = (struct user *)v;
    356 	v += UPAGES * NBPG;
    357 
    358 	/*
    359 	 * Find out what hardware we're on, and remember its type name.
    360 	 */
    361 	cputype = hwrpb->rpb_type;
    362 	switch (cputype) {
    363 #ifdef DEC_3000_500				/* and 400, [6-9]00 */
    364 	case ST_DEC_3000_500:
    365 		cpu_modelname = dec_3000_500_modelname;
    366 		cpu_consinit = dec_3000_500_consinit;
    367 		cpu_device_register = dec_3000_500_device_register;
    368 		cpu_iobus = "tcasic";
    369 		break;
    370 #endif
    371 
    372 #ifdef DEC_3000_300
    373 	case ST_DEC_3000_300:
    374 		cpu_modelname = dec_3000_300_modelname;
    375 		cpu_consinit = dec_3000_300_consinit;
    376 		cpu_device_register = dec_3000_300_device_register;
    377 		cpu_iobus = "tcasic";
    378 		break;
    379 #endif
    380 
    381 #ifdef DEC_2100_A50
    382 	case ST_DEC_2100_A50:
    383 		cpu_modelname = dec_2100_a50_modelname;
    384 		cpu_consinit = dec_2100_a50_consinit;
    385 		cpu_device_register = dec_2100_a50_device_register;
    386 		cpu_iobus = "apecs";
    387 		break;
    388 #endif
    389 
    390 #ifdef DEC_KN20AA
    391 	case ST_DEC_KN20AA:
    392 		cpu_modelname = dec_kn20aa_modelname;
    393 		cpu_consinit = dec_kn20aa_consinit;
    394 		cpu_device_register = dec_kn20aa_device_register;
    395 		cpu_iobus = "cia";
    396 		break;
    397 #endif
    398 
    399 #ifdef DEC_AXPPCI_33
    400 	case ST_DEC_AXPPCI_33:
    401 		cpu_modelname = dec_axppci_33_modelname;
    402 		cpu_consinit = dec_axppci_33_consinit;
    403 		cpu_device_register = dec_axppci_33_device_register;
    404 		cpu_iobus = "lca";
    405 		break;
    406 #endif
    407 
    408 #ifdef DEC_2000_300
    409 	case ST_DEC_2000_300:
    410 		cpu_modelname = dec_2000_300_modelname;
    411 		cpu_consinit = dec_2000_300_consinit;
    412 		cpu_device_register = dec_2000_300_device_register;
    413 		cpu_iobus = "ibus";
    414 	XXX DEC 2000/300 NOT SUPPORTED
    415 		break;
    416 #endif
    417 
    418 #ifdef DEC_21000
    419 	case ST_DEC_21000:
    420 		cpu_modelname = dec_21000_modelname;
    421 		cpu_consinit = dec_21000_consinit;
    422 		cpu_device_register = dec_21000_device_register;
    423 		cpu_iobus = "tlsb";
    424 	XXX DEC 21000 NOT SUPPORTED
    425 		break;
    426 #endif
    427 
    428 	default:
    429 		if (cputype > nmodel_names)
    430 			panic("Unknown system type %d", cputype);
    431 		else
    432 			panic("Support for %s system type not in kernel.",
    433 			    model_names[cputype]);
    434 	}
    435 
    436 	if ((*cpu_modelname)() != NULL)
    437 		strncpy(cpu_model, (*cpu_modelname)(), sizeof cpu_model - 1);
    438 	else
    439 		strncpy(cpu_model, model_names[cputype], sizeof cpu_model - 1);
    440 	cpu_model[sizeof cpu_model - 1] = '\0';
    441 
    442 #if NLE_IOASIC > 0
    443 	/*
    444 	 * Grab 128K at the top of physical memory for the lance chip
    445 	 * on machines where it does dma through the I/O ASIC.
    446 	 * It must be physically contiguous and aligned on a 128K boundary.
    447 	 *
    448 	 * Note that since this is conditional on the presence of
    449 	 * IOASIC-attached 'le' units in the kernel config, the
    450 	 * message buffer may move on these systems.  This shouldn't
    451 	 * be a problem, because once people have a kernel config that
    452 	 * they use, they're going to stick with it.
    453 	 */
    454 	if (cputype == ST_DEC_3000_500 ||
    455 	    cputype == ST_DEC_3000_300) {	/* XXX possibly others? */
    456 		lastusablepage -= btoc(128 * 1024);
    457 		le_iomem = (caddr_t)phystok0seg(ctob(lastusablepage + 1));
    458 	}
    459 #endif /* NLE_IOASIC */
    460 
    461 	/*
    462 	 * Initialize error message buffer (at end of core).
    463 	 */
    464 	lastusablepage -= btoc(sizeof (struct msgbuf));
    465 	msgbufp = (struct msgbuf *)phystok0seg(ctob(lastusablepage + 1));
    466 	msgbufmapped = 1;
    467 
    468 	/*
    469 	 * Allocate space for system data structures.
    470 	 * The first available kernel virtual address is in "v".
    471 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
    472 	 *
    473 	 * These data structures are allocated here instead of cpu_startup()
    474 	 * because physical memory is directly addressable. We don't have
    475 	 * to map these into virtual address space.
    476 	 */
    477 #define valloc(name, type, num) \
    478 	    (name) = (type *)v; v = (caddr_t)ALIGN((name)+(num))
    479 #define valloclim(name, type, num, lim) \
    480 	    (name) = (type *)v; v = (caddr_t)ALIGN((lim) = ((name)+(num)))
    481 #ifdef REAL_CLISTS
    482 	valloc(cfree, struct cblock, nclist);
    483 #endif
    484 	valloc(callout, struct callout, ncallout);
    485 	valloc(swapmap, struct map, nswapmap = maxproc * 2);
    486 #ifdef SYSVSHM
    487 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
    488 #endif
    489 #ifdef SYSVSEM
    490 	valloc(sema, struct semid_ds, seminfo.semmni);
    491 	valloc(sem, struct sem, seminfo.semmns);
    492 	/* This is pretty disgusting! */
    493 	valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
    494 #endif
    495 #ifdef SYSVMSG
    496 	valloc(msgpool, char, msginfo.msgmax);
    497 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
    498 	valloc(msghdrs, struct msg, msginfo.msgtql);
    499 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
    500 #endif
    501 
    502 	/*
    503 	 * Determine how many buffers to allocate.
    504 	 * We allocate 10% of memory for buffer space.  Insure a
    505 	 * minimum of 16 buffers.  We allocate 1/2 as many swap buffer
    506 	 * headers as file i/o buffers.
    507 	 */
    508 	if (bufpages == 0)
    509 		bufpages = (physmem * 10) / (CLSIZE * 100);
    510 	if (nbuf == 0) {
    511 		nbuf = bufpages;
    512 		if (nbuf < 16)
    513 			nbuf = 16;
    514 	}
    515 	if (nswbuf == 0) {
    516 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
    517 		if (nswbuf > 256)
    518 			nswbuf = 256;		/* sanity */
    519 	}
    520 	valloc(swbuf, struct buf, nswbuf);
    521 	valloc(buf, struct buf, nbuf);
    522 
    523 	/*
    524 	 * Clear allocated memory.
    525 	 */
    526 	bzero(start, v - start);
    527 
    528 	/*
    529 	 * Initialize the virtual memory system, and set the
    530 	 * page table base register in proc 0's PCB.
    531 	 */
    532 	pmap_bootstrap((vm_offset_t)v, phystok0seg(ptb << PGSHIFT));
    533 
    534 	/*
    535 	 * Initialize the rest of proc 0's PCB, and cache its physical
    536 	 * address.
    537 	 */
    538 	proc0.p_md.md_pcbpaddr =
    539 	    (struct pcb *)k0segtophys(&proc0paddr->u_pcb);
    540 
    541 	/*
    542 	 * Set the kernel sp, reserving space for an (empty) trapframe,
    543 	 * and make proc0's trapframe pointer point to it for sanity.
    544 	 */
    545 	proc0paddr->u_pcb.pcb_ksp =
    546 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
    547 	proc0.p_md.md_tf = (struct trapframe *)proc0paddr->u_pcb.pcb_ksp;
    548 
    549 	/*
    550 	 * Look at arguments passed to us and compute boothowto.
    551 	 */
    552 	prom_getenv(PROM_E_BOOTED_OSFLAGS, boot_flags, sizeof(boot_flags));
    553 #if 0
    554 	printf("boot flags = \"%s\"\n", boot_flags);
    555 #endif
    556 
    557 	boothowto = RB_SINGLE;
    558 #ifdef KADB
    559 	boothowto |= RB_KDB;
    560 #endif
    561 	for (p = boot_flags; p && *p != '\0'; p++) {
    562 		/*
    563 		 * Note that we'd really like to differentiate case here,
    564 		 * but the Alpha AXP Architecture Reference Manual
    565 		 * says that we shouldn't.
    566 		 */
    567 		switch (*p) {
    568 		case 'a': /* autoboot */
    569 		case 'A':
    570 			boothowto &= ~RB_SINGLE;
    571 			break;
    572 
    573 		case 'n': /* askname */
    574 		case 'N':
    575 			boothowto |= RB_ASKNAME;
    576 			break;
    577 
    578 #if 0
    579 		case 'm': /* mini root present in memory */
    580 		case 'M':
    581 			boothowto |= RB_MINIROOT;
    582 			break;
    583 #endif
    584 		}
    585 	}
    586 
    587 	/*
    588 	 * Figure out the number of cpus in the box, from RPB fields.
    589 	 * Really.  We mean it.
    590 	 */
    591 	for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
    592 		struct pcs *pcsp;
    593 
    594 		pcsp = (struct pcs *)((char *)hwrpb + hwrpb->rpb_pcs_off +
    595 		    (i * hwrpb->rpb_pcs_size));
    596 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    597 			ncpus++;
    598 	}
    599 
    600 	return (0);
    601 }
    602 
    603 void
    604 consinit()
    605 {
    606 
    607 	(*cpu_consinit)();
    608 	pmap_unmap_prom();
    609 }
    610 
    611 void
    612 cpu_startup()
    613 {
    614 	register unsigned i;
    615 	register caddr_t v;
    616 	int base, residual;
    617 	vm_offset_t minaddr, maxaddr;
    618 	vm_size_t size;
    619 #ifdef DEBUG
    620 	extern int pmapdebug;
    621 	int opmapdebug = pmapdebug;
    622 
    623 	pmapdebug = 0;
    624 #endif
    625 
    626 	/*
    627 	 * Good {morning,afternoon,evening,night}.
    628 	 */
    629 	printf(version);
    630 	identifycpu();
    631 	printf("real mem = %d (%d reserved for PROM, %d used by NetBSD)\n",
    632 	    ctob(totalphysmem), ctob(resvmem), ctob(physmem));
    633 	if (unusedmem)
    634 		printf("WARNING: unused memory = %d bytes\n", ctob(unusedmem));
    635 	if (unknownmem)
    636 		printf("WARNING: %d bytes of memory with unknown purpose\n",
    637 		    ctob(unknownmem));
    638 
    639 	/*
    640 	 * Allocate virtual address space for file I/O buffers.
    641 	 * Note they are different than the array of headers, 'buf',
    642 	 * and usually occupy more virtual memory than physical.
    643 	 */
    644 	size = MAXBSIZE * nbuf;
    645 	buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
    646 	    &maxaddr, size, TRUE);
    647 	minaddr = (vm_offset_t)buffers;
    648 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
    649 			&minaddr, size, FALSE) != KERN_SUCCESS)
    650 		panic("startup: cannot allocate buffers");
    651 	base = bufpages / nbuf;
    652 	residual = bufpages % nbuf;
    653 	for (i = 0; i < nbuf; i++) {
    654 		vm_size_t curbufsize;
    655 		vm_offset_t curbuf;
    656 
    657 		/*
    658 		 * First <residual> buffers get (base+1) physical pages
    659 		 * allocated for them.  The rest get (base) physical pages.
    660 		 *
    661 		 * The rest of each buffer occupies virtual space,
    662 		 * but has no physical memory allocated for it.
    663 		 */
    664 		curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
    665 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
    666 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
    667 		vm_map_simplify(buffer_map, curbuf);
    668 	}
    669 	/*
    670 	 * Allocate a submap for exec arguments.  This map effectively
    671 	 * limits the number of processes exec'ing at any time.
    672 	 */
    673 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    674 				 16 * NCARGS, TRUE);
    675 
    676 	/*
    677 	 * Allocate a submap for physio
    678 	 */
    679 	phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    680 				 VM_PHYS_SIZE, TRUE);
    681 
    682 	/*
    683 	 * Finally, allocate mbuf pool.  Since mclrefcnt is an off-size
    684 	 * we use the more space efficient malloc in place of kmem_alloc.
    685 	 */
    686 	mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES,
    687 	    M_MBUF, M_NOWAIT);
    688 	bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES);
    689 	mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
    690 	    VM_MBUF_SIZE, FALSE);
    691 	/*
    692 	 * Initialize callouts
    693 	 */
    694 	callfree = callout;
    695 	for (i = 1; i < ncallout; i++)
    696 		callout[i-1].c_next = &callout[i];
    697 	callout[i-1].c_next = NULL;
    698 
    699 #ifdef DEBUG
    700 	pmapdebug = opmapdebug;
    701 #endif
    702 	printf("avail mem = %ld\n", (long)ptoa(cnt.v_free_count));
    703 	printf("using %ld buffers containing %ld bytes of memory\n",
    704 		(long)nbuf, (long)(bufpages * CLBYTES));
    705 
    706 	/*
    707 	 * Set up buffers, so they can be used to read disk labels.
    708 	 */
    709 	bufinit();
    710 
    711 	/*
    712 	 * Configure the system.
    713 	 */
    714 	configure();
    715 }
    716 
    717 identifycpu()
    718 {
    719 
    720 	/*
    721 	 * print out CPU identification information.
    722 	 */
    723 	printf("%s, %dMHz\n", cpu_model,
    724 	    hwrpb->rpb_cc_freq / 1000000);	/* XXX true for 21164? */
    725 	printf("%d byte page size, %d processor%s.\n",
    726 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
    727 #if 0
    728 	/* this isn't defined for any systems that we run on? */
    729 	printf("serial number 0x%lx 0x%lx\n",
    730 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
    731 
    732 	/* and these aren't particularly useful! */
    733 	printf("variation: 0x%lx, revision 0x%lx\n",
    734 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
    735 #endif
    736 }
    737 
    738 int	waittime = -1;
    739 struct pcb dumppcb;
    740 
    741 void
    742 boot(howto)
    743 	int howto;
    744 {
    745 	extern int cold;
    746 
    747 	/* If system is cold, just halt. */
    748 	if (cold) {
    749 		howto |= RB_HALT;
    750 		goto haltsys;
    751 	}
    752 
    753 	boothowto = howto;
    754 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
    755 		waittime = 0;
    756 		vfs_shutdown();
    757 		/*
    758 		 * If we've been adjusting the clock, the todr
    759 		 * will be out of synch; adjust it now.
    760 		 */
    761 		resettodr();
    762 	}
    763 
    764 	/* Disable interrupts. */
    765 	splhigh();
    766 
    767 	/* If rebooting and a dump is requested do it. */
    768 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) {
    769 		savectx(&dumppcb, 0);
    770 		dumpsys();
    771 	}
    772 
    773 haltsys:
    774 
    775 	/* run any shutdown hooks */
    776 	doshutdownhooks();
    777 
    778 #ifdef BOOTKEY
    779 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
    780 	cngetc();
    781 	printf("\n");
    782 #endif
    783 
    784 	/* Finally, halt/reboot the system. */
    785 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
    786 	prom_halt(howto & RB_HALT);
    787 	/*NOTREACHED*/
    788 }
    789 
    790 /*
    791  * These variables are needed by /sbin/savecore
    792  */
    793 u_long	dumpmag = 0x8fca0101;	/* magic number */
    794 int 	dumpsize = 0;		/* pages */
    795 long	dumplo = 0; 		/* blocks */
    796 
    797 /*
    798  * This is called by configure to set dumplo and dumpsize.
    799  * Dumps always skip the first CLBYTES of disk space
    800  * in case there might be a disk label stored there.
    801  * If there is extra space, put dump at the end to
    802  * reduce the chance that swapping trashes it.
    803  */
    804 void
    805 dumpconf()
    806 {
    807 	int nblks;	/* size of dump area */
    808 	int maj;
    809 
    810 	if (dumpdev == NODEV)
    811 		return;
    812 	maj = major(dumpdev);
    813 	if (maj < 0 || maj >= nblkdev)
    814 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
    815 	if (bdevsw[maj].d_psize == NULL)
    816 		return;
    817 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
    818 	if (nblks <= ctod(1))
    819 		return;
    820 
    821 	/* XXX XXX XXX STARTING MEMORY LOCATION */
    822 	dumpsize = physmem;
    823 
    824 	/* Always skip the first CLBYTES, in case there is a label there. */
    825 	if (dumplo < ctod(1))
    826 		dumplo = ctod(1);
    827 
    828 	/* Put dump at end of partition, and make it fit. */
    829 	if (dumpsize > dtoc(nblks - dumplo))
    830 		dumpsize = dtoc(nblks - dumplo);
    831 	if (dumplo < nblks - ctod(dumpsize))
    832 		dumplo = nblks - ctod(dumpsize);
    833 }
    834 
    835 /*
    836  * Doadump comes here after turning off memory management and
    837  * getting on the dump stack, either when called above, or by
    838  * the auto-restart code.
    839  */
    840 void
    841 dumpsys()
    842 {
    843 
    844 	msgbufmapped = 0;
    845 	if (dumpdev == NODEV)
    846 		return;
    847 	if (dumpsize == 0) {
    848 		dumpconf();
    849 		if (dumpsize == 0)
    850 			return;
    851 	}
    852 	printf("\ndumping to dev %x, offset %d\n", dumpdev, dumplo);
    853 
    854 	printf("dump ");
    855 	switch ((*bdevsw[major(dumpdev)].d_dump)(dumpdev)) {
    856 
    857 	case ENXIO:
    858 		printf("device bad\n");
    859 		break;
    860 
    861 	case EFAULT:
    862 		printf("device not ready\n");
    863 		break;
    864 
    865 	case EINVAL:
    866 		printf("area improper\n");
    867 		break;
    868 
    869 	case EIO:
    870 		printf("i/o error\n");
    871 		break;
    872 
    873 	case EINTR:
    874 		printf("aborted from console\n");
    875 		break;
    876 
    877 	default:
    878 		printf("succeeded\n");
    879 		break;
    880 	}
    881 	printf("\n\n");
    882 	delay(1000);
    883 }
    884 
    885 void
    886 frametoreg(framep, regp)
    887 	struct trapframe *framep;
    888 	struct reg *regp;
    889 {
    890 
    891 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
    892 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
    893 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
    894 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
    895 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
    896 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
    897 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
    898 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
    899 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
    900 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
    901 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
    902 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
    903 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
    904 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
    905 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
    906 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
    907 	regp->r_regs[R_A0] = framep->tf_a0;
    908 	regp->r_regs[R_A1] = framep->tf_a1;
    909 	regp->r_regs[R_A2] = framep->tf_a2;
    910 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
    911 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
    912 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
    913 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
    914 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
    915 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
    916 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
    917 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
    918 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
    919 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
    920 	regp->r_regs[R_GP] = framep->tf_gp;
    921 	regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP];
    922 	regp->r_regs[R_ZERO] = 0;
    923 }
    924 
    925 void
    926 regtoframe(regp, framep)
    927 	struct reg *regp;
    928 	struct trapframe *framep;
    929 {
    930 
    931 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
    932 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
    933 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
    934 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
    935 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
    936 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
    937 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
    938 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
    939 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
    940 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
    941 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
    942 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
    943 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
    944 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
    945 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
    946 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
    947 	framep->tf_a0 = regp->r_regs[R_A0];
    948 	framep->tf_a1 = regp->r_regs[R_A1];
    949 	framep->tf_a2 = regp->r_regs[R_A2];
    950 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
    951 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
    952 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
    953 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
    954 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
    955 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
    956 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
    957 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
    958 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
    959 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
    960 	framep->tf_gp = regp->r_regs[R_GP];
    961 	framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP];
    962 	/* ??? = regp->r_regs[R_ZERO]; */
    963 }
    964 
    965 void
    966 printregs(regp)
    967 	struct reg *regp;
    968 {
    969 	int i;
    970 
    971 	for (i = 0; i < 32; i++)
    972 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
    973 		   i & 1 ? "\n" : "\t");
    974 }
    975 
    976 void
    977 regdump(framep)
    978 	struct trapframe *framep;
    979 {
    980 	struct reg reg;
    981 
    982 	frametoreg(framep, &reg);
    983 	printf("REGISTERS:\n");
    984 	printregs(&reg);
    985 }
    986 
    987 #ifdef DEBUG
    988 int sigdebug = 0;
    989 int sigpid = 0;
    990 #define	SDB_FOLLOW	0x01
    991 #define	SDB_KSTACK	0x02
    992 #endif
    993 
    994 /*
    995  * Send an interrupt to process.
    996  */
    997 void
    998 sendsig(catcher, sig, mask, code)
    999 	sig_t catcher;
   1000 	int sig, mask;
   1001 	u_long code;
   1002 {
   1003 	struct proc *p = curproc;
   1004 	struct sigcontext *scp, ksc;
   1005 	struct trapframe *frame;
   1006 	struct sigacts *psp = p->p_sigacts;
   1007 	int oonstack, fsize, rndfsize;
   1008 	extern char sigcode[], esigcode[];
   1009 	extern struct proc *fpcurproc;
   1010 
   1011 	frame = p->p_md.md_tf;
   1012 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
   1013 	fsize = sizeof ksc;
   1014 	rndfsize = ((fsize + 15) / 16) * 16;
   1015 	/*
   1016 	 * Allocate and validate space for the signal handler
   1017 	 * context. Note that if the stack is in P0 space, the
   1018 	 * call to grow() is a nop, and the useracc() check
   1019 	 * will fail if the process has not already allocated
   1020 	 * the space with a `brk'.
   1021 	 */
   1022 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
   1023 	    (psp->ps_sigonstack & sigmask(sig))) {
   1024 		scp = (struct sigcontext *)(psp->ps_sigstk.ss_sp +
   1025 		    psp->ps_sigstk.ss_size - rndfsize);
   1026 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
   1027 	} else
   1028 		scp = (struct sigcontext *)(frame->tf_regs[FRAME_SP] -
   1029 		    rndfsize);
   1030 	if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
   1031 		(void)grow(p, (u_long)scp);
   1032 #ifdef DEBUG
   1033 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1034 		printf("sendsig(%d): sig %d ssp %lx usp %lx\n", p->p_pid,
   1035 		    sig, &oonstack, scp);
   1036 #endif
   1037 	if (useracc((caddr_t)scp, fsize, B_WRITE) == 0) {
   1038 #ifdef DEBUG
   1039 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1040 			printf("sendsig(%d): useracc failed on sig %d\n",
   1041 			    p->p_pid, sig);
   1042 #endif
   1043 		/*
   1044 		 * Process has trashed its stack; give it an illegal
   1045 		 * instruction to halt it in its tracks.
   1046 		 */
   1047 		SIGACTION(p, SIGILL) = SIG_DFL;
   1048 		sig = sigmask(SIGILL);
   1049 		p->p_sigignore &= ~sig;
   1050 		p->p_sigcatch &= ~sig;
   1051 		p->p_sigmask &= ~sig;
   1052 		psignal(p, SIGILL);
   1053 		return;
   1054 	}
   1055 
   1056 	/*
   1057 	 * Build the signal context to be used by sigreturn.
   1058 	 */
   1059 	ksc.sc_onstack = oonstack;
   1060 	ksc.sc_mask = mask;
   1061 	ksc.sc_pc = frame->tf_pc;
   1062 	ksc.sc_ps = frame->tf_ps;
   1063 
   1064 	/* copy the registers. */
   1065 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1066 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1067 
   1068 	/* save the floating-point state, if necessary, then copy it. */
   1069 	if (p == fpcurproc) {
   1070 		pal_wrfen(1);
   1071 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
   1072 		pal_wrfen(0);
   1073 		fpcurproc = NULL;
   1074 	}
   1075 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
   1076 	bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1077 	    sizeof(struct fpreg));
   1078 	ksc.sc_fp_control = 0;					/* XXX ? */
   1079 	bzero(ksc.sc_reserved, sizeof ksc.sc_reserved);		/* XXX */
   1080 	bzero(ksc.sc_xxx, sizeof ksc.sc_xxx);			/* XXX */
   1081 
   1082 
   1083 #ifdef COMPAT_OSF1
   1084 	/*
   1085 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1086 	 */
   1087 #endif
   1088 
   1089 	/*
   1090 	 * copy the frame out to userland.
   1091 	 */
   1092 	(void) copyout((caddr_t)&ksc, (caddr_t)scp, fsize);
   1093 #ifdef DEBUG
   1094 	if (sigdebug & SDB_FOLLOW)
   1095 		printf("sendsig(%d): sig %d scp %lx code %lx\n", p->p_pid, sig,
   1096 		    scp, code);
   1097 #endif
   1098 
   1099 	/*
   1100 	 * Set up the registers to return to sigcode.
   1101 	 */
   1102 	frame->tf_pc = (u_int64_t)PS_STRINGS - (esigcode - sigcode);
   1103 	frame->tf_regs[FRAME_SP] = (u_int64_t)scp;
   1104 	frame->tf_a0 = sig;
   1105 	frame->tf_a1 = code;
   1106 	frame->tf_a2 = (u_int64_t)scp;
   1107 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
   1108 
   1109 #ifdef DEBUG
   1110 	if (sigdebug & SDB_FOLLOW)
   1111 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1112 		    frame->tf_pc, frame->tf_regs[FRAME_A3]);
   1113 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1114 		printf("sendsig(%d): sig %d returns\n",
   1115 		    p->p_pid, sig);
   1116 #endif
   1117 }
   1118 
   1119 /*
   1120  * System call to cleanup state after a signal
   1121  * has been taken.  Reset signal mask and
   1122  * stack state from context left by sendsig (above).
   1123  * Return to previous pc and psl as specified by
   1124  * context left by sendsig. Check carefully to
   1125  * make sure that the user has not modified the
   1126  * psl to gain improper priviledges or to cause
   1127  * a machine fault.
   1128  */
   1129 /* ARGSUSED */
   1130 int
   1131 sys_sigreturn(p, v, retval)
   1132 	struct proc *p;
   1133 	void *v;
   1134 	register_t *retval;
   1135 {
   1136 	struct sys_sigreturn_args /* {
   1137 		syscallarg(struct sigcontext *) sigcntxp;
   1138 	} */ *uap = v;
   1139 	struct sigcontext *scp, ksc;
   1140 	extern struct proc *fpcurproc;
   1141 
   1142 	scp = SCARG(uap, sigcntxp);
   1143 #ifdef DEBUG
   1144 	if (sigdebug & SDB_FOLLOW)
   1145 	    printf("sigreturn: pid %d, scp %lx\n", p->p_pid, scp);
   1146 #endif
   1147 
   1148 	if (ALIGN(scp) != (u_int64_t)scp)
   1149 		return (EINVAL);
   1150 
   1151 	/*
   1152 	 * Test and fetch the context structure.
   1153 	 * We grab it all at once for speed.
   1154 	 */
   1155 	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
   1156 	    copyin((caddr_t)scp, (caddr_t)&ksc, sizeof ksc))
   1157 		return (EINVAL);
   1158 
   1159 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1160 		return (EINVAL);
   1161 	/*
   1162 	 * Restore the user-supplied information
   1163 	 */
   1164 	if (ksc.sc_onstack)
   1165 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
   1166 	else
   1167 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1168 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
   1169 
   1170 	p->p_md.md_tf->tf_pc = ksc.sc_pc;
   1171 	p->p_md.md_tf->tf_ps = (ksc.sc_ps | PSL_USERSET) & ~PSL_USERCLR;
   1172 
   1173 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
   1174 
   1175 	/* XXX ksc.sc_ownedfp ? */
   1176 	if (p == fpcurproc)
   1177 		fpcurproc = NULL;
   1178 	bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
   1179 	    sizeof(struct fpreg));
   1180 	/* XXX ksc.sc_fp_control ? */
   1181 
   1182 #ifdef DEBUG
   1183 	if (sigdebug & SDB_FOLLOW)
   1184 		printf("sigreturn(%d): returns\n", p->p_pid);
   1185 #endif
   1186 	return (EJUSTRETURN);
   1187 }
   1188 
   1189 /*
   1190  * machine dependent system variables.
   1191  */
   1192 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1193 	int *name;
   1194 	u_int namelen;
   1195 	void *oldp;
   1196 	size_t *oldlenp;
   1197 	void *newp;
   1198 	size_t newlen;
   1199 	struct proc *p;
   1200 {
   1201 	dev_t consdev;
   1202 
   1203 	/* all sysctl names at this level are terminal */
   1204 	if (namelen != 1)
   1205 		return (ENOTDIR);		/* overloaded */
   1206 
   1207 	switch (name[0]) {
   1208 	case CPU_CONSDEV:
   1209 		if (cn_tab != NULL)
   1210 			consdev = cn_tab->cn_dev;
   1211 		else
   1212 			consdev = NODEV;
   1213 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1214 			sizeof consdev));
   1215 
   1216 	case CPU_ROOT_DEVICE:
   1217 		return (sysctl_rdstring(oldp, oldlenp, newp, root_device));
   1218 
   1219 	default:
   1220 		return (EOPNOTSUPP);
   1221 	}
   1222 	/* NOTREACHED */
   1223 }
   1224 
   1225 /*
   1226  * Set registers on exec.
   1227  */
   1228 void
   1229 setregs(p, pack, stack, retval)
   1230 	register struct proc *p;
   1231 	struct exec_package *pack;
   1232 	u_long stack;
   1233 	register_t *retval;
   1234 {
   1235 	struct trapframe *tfp = p->p_md.md_tf;
   1236 	int i;
   1237 	extern struct proc *fpcurproc;
   1238 
   1239 #ifdef DEBUG
   1240 	for (i = 0; i < FRAME_NSAVEREGS; i++)
   1241 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1242 	tfp->tf_gp = 0xbabefacedeadbeef;
   1243 	tfp->tf_a0 = 0xbabefacedeadbeef;
   1244 	tfp->tf_a1 = 0xbabefacedeadbeef;
   1245 	tfp->tf_a2 = 0xbabefacedeadbeef;
   1246 #else
   1247 	bzero(tfp->tf_regs, FRAME_NSAVEREGS * sizeof tfp->tf_regs[0]);
   1248 	tfp->tf_gp = 0;
   1249 	tfp->tf_a0 = 0;
   1250 	tfp->tf_a1 = 0;
   1251 	tfp->tf_a2 = 0;
   1252 #endif
   1253 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
   1254 #define FP_RN 2 /* XXX */
   1255 	p->p_addr->u_pcb.pcb_fp.fpr_cr = (long)FP_RN << 58;
   1256 	tfp->tf_regs[FRAME_SP] = stack;	/* restored to usp in trap return */
   1257 	tfp->tf_ps = PSL_USERSET;
   1258 	tfp->tf_pc = pack->ep_entry & ~3;
   1259 
   1260 	p->p_md.md_flags & ~MDP_FPUSED;
   1261 	if (fpcurproc == p)
   1262 		fpcurproc = NULL;
   1263 
   1264 	retval[0] = retval[1] = 0;
   1265 }
   1266 
   1267 void
   1268 netintr()
   1269 {
   1270 #ifdef INET
   1271 #if NETHER > 0
   1272 	if (netisr & (1 << NETISR_ARP)) {
   1273 		netisr &= ~(1 << NETISR_ARP);
   1274 		arpintr();
   1275 	}
   1276 #endif
   1277 	if (netisr & (1 << NETISR_IP)) {
   1278 		netisr &= ~(1 << NETISR_IP);
   1279 		ipintr();
   1280 	}
   1281 #endif
   1282 #ifdef NS
   1283 	if (netisr & (1 << NETISR_NS)) {
   1284 		netisr &= ~(1 << NETISR_NS);
   1285 		nsintr();
   1286 	}
   1287 #endif
   1288 #ifdef ISO
   1289 	if (netisr & (1 << NETISR_ISO)) {
   1290 		netisr &= ~(1 << NETISR_ISO);
   1291 		clnlintr();
   1292 	}
   1293 #endif
   1294 #ifdef CCITT
   1295 	if (netisr & (1 << NETISR_CCITT)) {
   1296 		netisr &= ~(1 << NETISR_CCITT);
   1297 		ccittintr();
   1298 	}
   1299 #endif
   1300 #ifdef PPP
   1301 	if (netisr & (1 << NETISR_PPP)) {
   1302 		netisr &= ~(1 << NETISR_PPP);
   1303 		pppintr();
   1304 	}
   1305 #endif
   1306 }
   1307 
   1308 void
   1309 do_sir()
   1310 {
   1311 
   1312 	if (ssir & SIR_NET) {
   1313 		siroff(SIR_NET);
   1314 		cnt.v_soft++;
   1315 		netintr();
   1316 	}
   1317 	if (ssir & SIR_CLOCK) {
   1318 		siroff(SIR_CLOCK);
   1319 		cnt.v_soft++;
   1320 		softclock();
   1321 	}
   1322 }
   1323 
   1324 int
   1325 spl0()
   1326 {
   1327 
   1328 	if (ssir) {
   1329 		splsoft();
   1330 		do_sir();
   1331 	}
   1332 
   1333 	return (pal_swpipl(PSL_IPL_0));
   1334 }
   1335 
   1336 /*
   1337  * The following primitives manipulate the run queues.  _whichqs tells which
   1338  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
   1339  * into queues, Remrq removes them from queues.  The running process is on
   1340  * no queue, other processes are on a queue related to p->p_priority, divided
   1341  * by 4 actually to shrink the 0-127 range of priorities into the 32 available
   1342  * queues.
   1343  */
   1344 /*
   1345  * setrunqueue(p)
   1346  *	proc *p;
   1347  *
   1348  * Call should be made at splclock(), and p->p_stat should be SRUN.
   1349  */
   1350 
   1351 void
   1352 setrunqueue(p)
   1353 	struct proc *p;
   1354 {
   1355 	int bit;
   1356 
   1357 	/* firewall: p->p_back must be NULL */
   1358 	if (p->p_back != NULL)
   1359 		panic("setrunqueue");
   1360 
   1361 	bit = p->p_priority >> 2;
   1362 	whichqs |= (1 << bit);
   1363 	p->p_forw = (struct proc *)&qs[bit];
   1364 	p->p_back = qs[bit].ph_rlink;
   1365 	p->p_back->p_forw = p;
   1366 	qs[bit].ph_rlink = p;
   1367 }
   1368 
   1369 /*
   1370  * Remrq(p)
   1371  *
   1372  * Call should be made at splclock().
   1373  */
   1374 void
   1375 remrq(p)
   1376 	struct proc *p;
   1377 {
   1378 	int bit;
   1379 
   1380 	bit = p->p_priority >> 2;
   1381 	if ((whichqs & (1 << bit)) == 0)
   1382 		panic("remrq");
   1383 
   1384 	p->p_back->p_forw = p->p_forw;
   1385 	p->p_forw->p_back = p->p_back;
   1386 	p->p_back = NULL;	/* for firewall checking. */
   1387 
   1388 	if ((struct proc *)&qs[bit] == qs[bit].ph_link)
   1389 		whichqs &= ~(1 << bit);
   1390 }
   1391 
   1392 /*
   1393  * Return the best possible estimate of the time in the timeval
   1394  * to which tvp points.  Unfortunately, we can't read the hardware registers.
   1395  * We guarantee that the time will be greater than the value obtained by a
   1396  * previous call.
   1397  */
   1398 void
   1399 microtime(tvp)
   1400 	register struct timeval *tvp;
   1401 {
   1402 	int s = splclock();
   1403 	static struct timeval lasttime;
   1404 
   1405 	*tvp = time;
   1406 #ifdef notdef
   1407 	tvp->tv_usec += clkread();
   1408 	while (tvp->tv_usec > 1000000) {
   1409 		tvp->tv_sec++;
   1410 		tvp->tv_usec -= 1000000;
   1411 	}
   1412 #endif
   1413 	if (tvp->tv_sec == lasttime.tv_sec &&
   1414 	    tvp->tv_usec <= lasttime.tv_usec &&
   1415 	    (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
   1416 		tvp->tv_sec++;
   1417 		tvp->tv_usec -= 1000000;
   1418 	}
   1419 	lasttime = *tvp;
   1420 	splx(s);
   1421 }
   1422 
   1423 /*
   1424  * Wait "n" microseconds.
   1425  */
   1426 int
   1427 delay(n)
   1428 	int n;
   1429 {
   1430 	long N = cycles_per_usec * (n);
   1431 
   1432 	while (N > 0)				/* XXX */
   1433 		N -= 3;				/* XXX */
   1434 }
   1435 
   1436 #if defined(COMPAT_OSF1) || 1		/* XXX */
   1437 void
   1438 cpu_exec_ecoff_setregs(p, epp, stack, retval)
   1439 	struct proc *p;
   1440 	struct exec_package *epp;
   1441 	u_long stack;
   1442 	register_t *retval;
   1443 {
   1444 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1445 
   1446 	setregs(p, epp, stack, retval);
   1447 	p->p_md.md_tf->tf_gp = execp->a.gp_value;
   1448 }
   1449 
   1450 /*
   1451  * cpu_exec_ecoff_hook():
   1452  *	cpu-dependent ECOFF format hook for execve().
   1453  *
   1454  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   1455  *
   1456  */
   1457 int
   1458 cpu_exec_ecoff_hook(p, epp)
   1459 	struct proc *p;
   1460 	struct exec_package *epp;
   1461 {
   1462 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1463 	extern struct emul emul_netbsd;
   1464 #ifdef COMPAT_OSF1
   1465 	extern struct emul emul_osf1;
   1466 #endif
   1467 
   1468 	switch (execp->f.f_magic) {
   1469 #ifdef COMPAT_OSF1
   1470 	case ECOFF_MAGIC_ALPHA:
   1471 		epp->ep_emul = &emul_osf1;
   1472 		break;
   1473 #endif
   1474 
   1475 	case ECOFF_MAGIC_NETBSD_ALPHA:
   1476 		epp->ep_emul = &emul_netbsd;
   1477 		break;
   1478 
   1479 	default:
   1480 		return ENOEXEC;
   1481 	}
   1482 	return 0;
   1483 }
   1484 #endif
   1485 
   1486 vm_offset_t
   1487 vtophys(vaddr)
   1488 	vm_offset_t vaddr;
   1489 {
   1490 	vm_offset_t paddr;
   1491 
   1492 	if (vaddr < K0SEG_BEGIN) {
   1493 		printf("vtophys: invalid vaddr 0x%lx", vaddr);
   1494 		paddr = vaddr;
   1495 	} else if (vaddr < K0SEG_END)
   1496 		paddr = k0segtophys(vaddr);
   1497 	else
   1498 		paddr = vatopa(vaddr);
   1499 
   1500 #if 0
   1501 	printf("vtophys(0x%lx) -> %lx\n", vaddr, paddr);
   1502 #endif
   1503 
   1504 	return (paddr);
   1505 }
   1506