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