Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.12
      1 /*	$NetBSD: machdep.c,v 1.12 1995/11/23 02:34:15 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 #ifdef DEC_3000_500
     76 #include <alpha/alpha/dec_3000_500.h>
     77 #endif
     78 #ifdef DEC_3000_300
     79 #include <alpha/alpha/dec_3000_300.h>
     80 #endif
     81 #ifdef DEC_2100_A50
     82 #include <alpha/alpha/dec_2100_a50.h>
     83 #endif
     84 #ifdef DEC_KN20AA
     85 #include <alpha/alpha/dec_kn20aa.h>
     86 #endif
     87 #ifdef DEC_AXPPCI_33
     88 #include <alpha/alpha/dec_axppci_33.h>
     89 #endif
     90 #ifdef DEC_21000
     91 #include <alpha/alpha/dec_21000.h>
     92 #endif
     93 
     94 #include <net/netisr.h>
     95 #include "ether.h"
     96 
     97 #include "le.h"			/* XXX for le_iomem creation */
     98 #include "esp.h"		/* XXX for esp_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;
    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 caddr_t		esp_iomem;		/* XXX iomem for SCSI DMA */
    170 
    171 /* Interrupt vectors (in locore) */
    172 extern int XentInt(), XentArith(), XentMM(), XentIF(), XentUna(), XentSys();
    173 
    174 /* number of cpus in the box.  really! */
    175 int		ncpus;
    176 
    177 /* various CPU-specific functions. */
    178 char		*(*cpu_modelname) __P((void));
    179 void		(*cpu_consinit) __P((char *));
    180 dev_t		(*cpu_bootdev) __P((char *));
    181 char		*cpu_iobus;
    182 
    183 char *boot_file, *boot_flags, *boot_console, *boot_dev;
    184 
    185 int
    186 alpha_init(pfn, ptb, argc, argv, envp)
    187 	u_long pfn;		/* first free PFN number */
    188 	u_long ptb;		/* PFN of current level 1 page table */
    189 	u_long argc;
    190 	char *argv[], *envp[];
    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_bootdev = dec_3000_500_bootdev;
    368 		cpu_iobus = "tc";
    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_bootdev = dec_3000_300_bootdev;
    377 		cpu_iobus = "tc";
    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_bootdev = dec_2100_a50_bootdev;
    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_bootdev = dec_kn20aa_bootdev;
    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_bootdev = dec_axppci_33_bootdev;
    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_bootdev = dec_2000_300_bootdev;
    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_bootdev = dec_21000_bootdev;
    423 		cpu_iobus = "tlsb";
    424 		break;
    425 #endif
    426 
    427 	default:
    428 		if (cputype > nmodel_names)
    429 			panic("Unknown system type %d", cputype);
    430 		else
    431 			panic("Support for %s system type not in kernel.",
    432 			    model_names[cputype]);
    433 	}
    434 
    435 	cpu_model = (*cpu_modelname)();
    436 	if (cpu_model == NULL)
    437 		cpu_model = model_names[cputype];
    438 
    439 #if NLE > 0
    440 	/*
    441 	 * Grab 128K at the top of physical memory for the lance chip
    442 	 * on machines where it does dma through the I/O ASIC.
    443 	 * It must be physically contiguous and aligned on a 128K boundary.
    444 	 */
    445 	if (cputype == ST_DEC_3000_500 ||
    446 	    cputype == ST_DEC_3000_300) {	/* XXX possibly others? */
    447 		lastusablepage -= btoc(128 * 1024);
    448 		le_iomem = (caddr_t)phystok0seg(ctob(lastusablepage + 1));
    449 	}
    450 #endif /* NLE */
    451 #if NESP > 0
    452 	/*
    453 	 * Ditto for the scsi chip. There is probably a way to make esp.c
    454 	 * do dma without these buffers, but it would require major
    455 	 * re-engineering of the esp driver.
    456 	 * They must be 8K in size and page aligned.
    457 	 */
    458 	if (cputype == ST_DEC_3000_500 ||
    459 	    cputype == ST_DEC_3000_300) {	/* XXX possibly others? */
    460 		lastusablepage -= btoc(NESP * 8192);
    461 		esp_iomem = (caddr_t)phystok0seg(ctob(lastusablepage + 1));
    462 	}
    463 #endif /* NESP */
    464 
    465 	/*
    466 	 * Initialize error message buffer (at end of core).
    467 	 */
    468 	lastusablepage -= btoc(sizeof (struct msgbuf));
    469 	msgbufp = (struct msgbuf *)phystok0seg(ctob(lastusablepage + 1));
    470 	msgbufmapped = 1;
    471 
    472 	/*
    473 	 * Allocate space for system data structures.
    474 	 * The first available kernel virtual address is in "v".
    475 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
    476 	 *
    477 	 * These data structures are allocated here instead of cpu_startup()
    478 	 * because physical memory is directly addressable. We don't have
    479 	 * to map these into virtual address space.
    480 	 */
    481 #define valloc(name, type, num) \
    482 	    (name) = (type *)v; v = (caddr_t)ALIGN((name)+(num))
    483 #define valloclim(name, type, num, lim) \
    484 	    (name) = (type *)v; v = (caddr_t)ALIGN((lim) = ((name)+(num)))
    485 #ifdef REAL_CLISTS
    486 	valloc(cfree, struct cblock, nclist);
    487 #endif
    488 	valloc(callout, struct callout, ncallout);
    489 	valloc(swapmap, struct map, nswapmap = maxproc * 2);
    490 #ifdef SYSVSHM
    491 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
    492 #endif
    493 #ifdef SYSVSEM
    494 	valloc(sema, struct semid_ds, seminfo.semmni);
    495 	valloc(sem, struct sem, seminfo.semmns);
    496 	/* This is pretty disgusting! */
    497 	valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
    498 #endif
    499 #ifdef SYSVMSG
    500 	valloc(msgpool, char, msginfo.msgmax);
    501 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
    502 	valloc(msghdrs, struct msg, msginfo.msgtql);
    503 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
    504 #endif
    505 
    506 	/*
    507 	 * Determine how many buffers to allocate.
    508 	 * We allocate the BSD standard of 10% of memory for the first
    509 	 * 2 Meg, and 5% of remaining memory for buffer space.  Insure a
    510 	 * minimum of 16 buffers.  We allocate 1/2 as many swap buffer
    511 	 * headers as file i/o buffers.
    512 	 */
    513 	if (bufpages == 0)
    514 		bufpages = (btoc(2 * 1024 * 1024) + physmem) /
    515 		    (20 * CLSIZE);
    516 	if (nbuf == 0) {
    517 		nbuf = bufpages;
    518 		if (nbuf < 16)
    519 			nbuf = 16;
    520 	}
    521 	if (nswbuf == 0) {
    522 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
    523 		if (nswbuf > 256)
    524 			nswbuf = 256;		/* sanity */
    525 	}
    526 	valloc(swbuf, struct buf, nswbuf);
    527 	valloc(buf, struct buf, nbuf);
    528 
    529 	/*
    530 	 * Clear allocated memory.
    531 	 */
    532 	bzero(start, v - start);
    533 
    534 	/*
    535 	 * Initialize the virtual memory system, and set the
    536 	 * page table base register in proc 0's PCB.
    537 	 */
    538 	pmap_bootstrap((vm_offset_t)v, phystok0seg(ptb << PGSHIFT));
    539 
    540 	/*
    541 	 * Initialize the rest of proc 0's PCB, and cache its physical
    542 	 * address.
    543 	 */
    544 	proc0.p_md.md_pcbpaddr =
    545 	    (struct pcb *)k0segtophys(&proc0paddr->u_pcb);
    546 
    547 	/*
    548 	 * Set the kernel sp, reserving space for an (empty) trapframe,
    549 	 * and make proc0's trapframe pointer point to it for sanity.
    550 	 */
    551 	proc0paddr->u_pcb.pcb_ksp =
    552 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
    553 	proc0.p_md.md_tf = (struct trapframe *)proc0paddr->u_pcb.pcb_ksp;
    554 
    555 	/*
    556 	 * figure out what arguments we have
    557 	 */
    558 	switch (argc) {
    559 	default:
    560 		printf("weird number of arguments from boot: %d\n", argc);
    561 		if (argc < 1)
    562 			break;
    563 		/* FALLTHRU */
    564 	case 4:
    565 		boot_dev = argv[3];
    566 		/* FALLTHRU */
    567 	case 3:
    568 		boot_console = argv[2];
    569 		/* FALLTHRU */
    570 	case 2:
    571 		boot_flags = argv[1];
    572 		/* FALLTHRU */
    573 	case 1:
    574 		boot_file = argv[0];
    575 		/* FALLTHRU */
    576 	}
    577 
    578 	/*
    579 	 * Look at arguments and compute bootdev.
    580 	 * XXX NOT HERE.
    581 	 */
    582 #if 0
    583 	{							/* XXX */
    584 		extern dev_t bootdev;				/* XXX */
    585 		bootdev = (*cpu_bootdev)(boot_dev);
    586 	}							/* XXX */
    587 #endif
    588 
    589 	/*
    590 	 * Look at arguments passed to us and compute boothowto.
    591 	 */
    592 	boothowto = RB_SINGLE;
    593 #ifdef GENERIC
    594 	boothowto |= RB_ASKNAME;
    595 #endif
    596 #ifdef KADB
    597 	boothowto |= RB_KDB;
    598 #endif
    599 	for (p = boot_flags; p && *p != '\0'; p++) {
    600 		switch (*p) {
    601 		case 'a': /* autoboot */
    602 		case 'A': /* DEC's notion of autoboot */
    603 			boothowto &= ~RB_SINGLE;
    604 			break;
    605 
    606 		case 'd': /* use compiled in default root */
    607 			boothowto |= RB_DFLTROOT;
    608 			break;
    609 
    610 		case 'm': /* mini root present in memory */
    611 			boothowto |= RB_MINIROOT;
    612 			break;
    613 
    614 		case 'n': /* ask for names */
    615 			boothowto |= RB_ASKNAME;
    616 			break;
    617 
    618 		case 'N': /* don't ask for names */
    619 			boothowto &= ~RB_ASKNAME;
    620 		}
    621 	}
    622 
    623 	/*
    624 	 * Figure out the number of cpus in the box, from RPB fields.
    625 	 * Really.  We mean it.
    626 	 */
    627 	for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
    628 		struct pcs *pcsp;
    629 
    630 		pcsp = (struct pcs *)((char *)hwrpb + hwrpb->rpb_pcs_off +
    631 		    (i * hwrpb->rpb_pcs_size));
    632 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    633 			ncpus++;
    634 	}
    635 
    636 	return (0);
    637 }
    638 
    639 consinit()
    640 {
    641 
    642 	(*cpu_consinit)(boot_console);
    643 	pmap_unmap_prom();
    644 }
    645 
    646 cpu_startup()
    647 {
    648 	register unsigned i;
    649 	register caddr_t v;
    650 	int base, residual;
    651 	vm_offset_t minaddr, maxaddr;
    652 	vm_size_t size;
    653 #ifdef DEBUG
    654 	extern int pmapdebug;
    655 	int opmapdebug = pmapdebug;
    656 
    657 	pmapdebug = 0;
    658 #endif
    659 
    660 	/*
    661 	 * Good {morning,afternoon,evening,night}.
    662 	 */
    663 	printf(version);
    664 	identifycpu();
    665 	printf("real mem = %d (%d reserved for PROM, %d used by NetBSD)\n",
    666 	    ctob(totalphysmem), ctob(resvmem), ctob(physmem));
    667 	if (unusedmem)
    668 		printf("WARNING: unused memory = %d bytes\n", ctob(unusedmem));
    669 	if (unknownmem)
    670 		printf("WARNING: %d bytes of memory with unknown purpose\n",
    671 		    ctob(unknownmem));
    672 
    673 	/*
    674 	 * Allocate virtual address space for file I/O buffers.
    675 	 * Note they are different than the array of headers, 'buf',
    676 	 * and usually occupy more virtual memory than physical.
    677 	 */
    678 	size = MAXBSIZE * nbuf;
    679 	buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
    680 	    &maxaddr, size, TRUE);
    681 	minaddr = (vm_offset_t)buffers;
    682 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
    683 			&minaddr, size, FALSE) != KERN_SUCCESS)
    684 		panic("startup: cannot allocate buffers");
    685 	base = bufpages / nbuf;
    686 	residual = bufpages % nbuf;
    687 	for (i = 0; i < nbuf; i++) {
    688 		vm_size_t curbufsize;
    689 		vm_offset_t curbuf;
    690 
    691 		/*
    692 		 * First <residual> buffers get (base+1) physical pages
    693 		 * allocated for them.  The rest get (base) physical pages.
    694 		 *
    695 		 * The rest of each buffer occupies virtual space,
    696 		 * but has no physical memory allocated for it.
    697 		 */
    698 		curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
    699 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
    700 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
    701 		vm_map_simplify(buffer_map, curbuf);
    702 	}
    703 	/*
    704 	 * Allocate a submap for exec arguments.  This map effectively
    705 	 * limits the number of processes exec'ing at any time.
    706 	 */
    707 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    708 				 16 * NCARGS, TRUE);
    709 
    710 	/*
    711 	 * Allocate a submap for physio
    712 	 */
    713 	phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    714 				 VM_PHYS_SIZE, TRUE);
    715 
    716 	/*
    717 	 * Finally, allocate mbuf pool.  Since mclrefcnt is an off-size
    718 	 * we use the more space efficient malloc in place of kmem_alloc.
    719 	 */
    720 	mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES,
    721 	    M_MBUF, M_NOWAIT);
    722 	bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES);
    723 	mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
    724 	    VM_MBUF_SIZE, FALSE);
    725 	/*
    726 	 * Initialize callouts
    727 	 */
    728 	callfree = callout;
    729 	for (i = 1; i < ncallout; i++)
    730 		callout[i-1].c_next = &callout[i];
    731 	callout[i-1].c_next = NULL;
    732 
    733 #ifdef DEBUG
    734 	pmapdebug = opmapdebug;
    735 #endif
    736 	printf("avail mem = %ld\n", (long)ptoa(cnt.v_free_count));
    737 	printf("using %ld buffers containing %ld bytes of memory\n",
    738 		(long)nbuf, (long)(bufpages * CLBYTES));
    739 
    740 	/*
    741 	 * Set up buffers, so they can be used to read disk labels.
    742 	 */
    743 	bufinit();
    744 
    745 	/*
    746 	 * Configure the system.
    747 	 */
    748 	configure();
    749 }
    750 
    751 identifycpu()
    752 {
    753 
    754 	/*
    755 	 * print out CPU identification information.
    756 	 */
    757 	printf("%s, %dMHz\n", cpu_model,
    758 	    hwrpb->rpb_cc_freq / 1000000);	/* XXX true for 21164? */
    759 	printf("%d byte page size, %d processor%s.\n",
    760 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
    761 #if 0
    762 	/* this isn't defined for any systems that we run on? */
    763 	printf("serial number 0x%lx 0x%lx\n",
    764 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
    765 
    766 	/* and these aren't particularly useful! */
    767 	printf("variation: 0x%lx, revision 0x%lx\n",
    768 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
    769 #endif
    770 }
    771 
    772 int	waittime = -1;
    773 struct pcb dumppcb;
    774 
    775 boot(howto)
    776 	int howto;
    777 {
    778 	extern int cold;
    779 
    780 	/* If system is cold, just halt. */
    781 	if (cold) {
    782 		howto |= RB_HALT;
    783 		goto haltsys;
    784 	}
    785 
    786 	boothowto = howto;
    787 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
    788 		waittime = 0;
    789 		vfs_shutdown();
    790 		/*
    791 		 * If we've been adjusting the clock, the todr
    792 		 * will be out of synch; adjust it now.
    793 		 */
    794 		resettodr();
    795 	}
    796 
    797 	/* Disable interrupts. */
    798 	splhigh();
    799 
    800 	/* If rebooting and a dump is requested do it. */
    801 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) {
    802 		savectx(&dumppcb, 0);
    803 		dumpsys();
    804 	}
    805 
    806 haltsys:
    807 
    808 	/* run any shutdown hooks */
    809 	doshutdownhooks();
    810 
    811 #ifdef BOOTKEY
    812 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
    813 	cngetc();
    814 	printf("\n");
    815 #endif
    816 
    817 	/* Finally, halt/reboot the system. */
    818 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
    819 	prom_halt(howto & RB_HALT);
    820 	/*NOTREACHED*/
    821 }
    822 
    823 /*
    824  * These variables are needed by /sbin/savecore
    825  */
    826 u_long	dumpmag = 0x8fca0101;	/* magic number */
    827 int 	dumpsize = 0;		/* pages */
    828 long	dumplo = 0; 		/* blocks */
    829 
    830 /*
    831  * This is called by configure to set dumplo and dumpsize.
    832  * Dumps always skip the first CLBYTES of disk space
    833  * in case there might be a disk label stored there.
    834  * If there is extra space, put dump at the end to
    835  * reduce the chance that swapping trashes it.
    836  */
    837 void
    838 dumpconf()
    839 {
    840 	int nblks;	/* size of dump area */
    841 	int maj;
    842 
    843 	if (dumpdev == NODEV)
    844 		return;
    845 	maj = major(dumpdev);
    846 	if (maj < 0 || maj >= nblkdev)
    847 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
    848 	if (bdevsw[maj].d_psize == NULL)
    849 		return;
    850 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
    851 	if (nblks <= ctod(1))
    852 		return;
    853 
    854 	/* XXX XXX XXX STARTING MEMORY LOCATION */
    855 	dumpsize = physmem;
    856 
    857 	/* Always skip the first CLBYTES, in case there is a label there. */
    858 	if (dumplo < ctod(1))
    859 		dumplo = ctod(1);
    860 
    861 	/* Put dump at end of partition, and make it fit. */
    862 	if (dumpsize > dtoc(nblks - dumplo))
    863 		dumpsize = dtoc(nblks - dumplo);
    864 	if (dumplo < nblks - ctod(dumpsize))
    865 		dumplo = nblks - ctod(dumpsize);
    866 }
    867 
    868 /*
    869  * Doadump comes here after turning off memory management and
    870  * getting on the dump stack, either when called above, or by
    871  * the auto-restart code.
    872  */
    873 void
    874 dumpsys()
    875 {
    876 
    877 	msgbufmapped = 0;
    878 	if (dumpdev == NODEV)
    879 		return;
    880 	if (dumpsize == 0) {
    881 		dumpconf();
    882 		if (dumpsize == 0)
    883 			return;
    884 	}
    885 	printf("\ndumping to dev %x, offset %d\n", dumpdev, dumplo);
    886 
    887 	printf("dump ");
    888 	switch ((*bdevsw[major(dumpdev)].d_dump)(dumpdev)) {
    889 
    890 	case ENXIO:
    891 		printf("device bad\n");
    892 		break;
    893 
    894 	case EFAULT:
    895 		printf("device not ready\n");
    896 		break;
    897 
    898 	case EINVAL:
    899 		printf("area improper\n");
    900 		break;
    901 
    902 	case EIO:
    903 		printf("i/o error\n");
    904 		break;
    905 
    906 	case EINTR:
    907 		printf("aborted from console\n");
    908 		break;
    909 
    910 	default:
    911 		printf("succeeded\n");
    912 		break;
    913 	}
    914 	printf("\n\n");
    915 	delay(1000);
    916 }
    917 
    918 void
    919 frametoreg(framep, regp)
    920 	struct trapframe *framep;
    921 	struct reg *regp;
    922 {
    923 
    924 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
    925 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
    926 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
    927 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
    928 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
    929 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
    930 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
    931 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
    932 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
    933 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
    934 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
    935 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
    936 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
    937 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
    938 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
    939 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
    940 	regp->r_regs[R_A0] = framep->tf_a0;
    941 	regp->r_regs[R_A1] = framep->tf_a1;
    942 	regp->r_regs[R_A2] = framep->tf_a2;
    943 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
    944 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
    945 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
    946 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
    947 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
    948 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
    949 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
    950 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
    951 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
    952 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
    953 	regp->r_regs[R_GP] = framep->tf_gp;
    954 	regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP];
    955 	regp->r_regs[R_ZERO] = 0;
    956 }
    957 
    958 void
    959 regtoframe(regp, framep)
    960 	struct reg *regp;
    961 	struct trapframe *framep;
    962 {
    963 
    964 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
    965 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
    966 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
    967 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
    968 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
    969 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
    970 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
    971 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
    972 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
    973 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
    974 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
    975 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
    976 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
    977 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
    978 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
    979 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
    980 	framep->tf_a0 = regp->r_regs[R_A0];
    981 	framep->tf_a1 = regp->r_regs[R_A1];
    982 	framep->tf_a2 = regp->r_regs[R_A2];
    983 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
    984 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
    985 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
    986 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
    987 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
    988 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
    989 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
    990 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
    991 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
    992 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
    993 	framep->tf_gp = regp->r_regs[R_GP];
    994 	framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP];
    995 	/* ??? = regp->r_regs[R_ZERO]; */
    996 }
    997 
    998 void
    999 printregs(regp)
   1000 	struct reg *regp;
   1001 {
   1002 	int i;
   1003 
   1004 	for (i = 0; i < 32; i++)
   1005 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1006 		   i & 1 ? "\n" : "\t");
   1007 }
   1008 
   1009 void
   1010 regdump(framep)
   1011 	struct trapframe *framep;
   1012 {
   1013 	struct reg reg;
   1014 
   1015 	frametoreg(framep, &reg);
   1016 	printf("REGISTERS:\n");
   1017 	printregs(&reg);
   1018 }
   1019 
   1020 #ifdef DEBUG
   1021 int sigdebug = 0;
   1022 int sigpid = 0;
   1023 #define	SDB_FOLLOW	0x01
   1024 #define	SDB_KSTACK	0x02
   1025 #endif
   1026 
   1027 /*
   1028  * Send an interrupt to process.
   1029  */
   1030 void
   1031 sendsig(catcher, sig, mask, code)
   1032 	sig_t catcher;
   1033 	int sig, mask;
   1034 	u_long code;
   1035 {
   1036 	struct proc *p = curproc;
   1037 	struct sigcontext *scp, ksc;
   1038 	struct trapframe *frame;
   1039 	struct sigacts *psp = p->p_sigacts;
   1040 	int oonstack, fsize, rndfsize;
   1041 	extern char sigcode[], esigcode[];
   1042 	extern struct proc *fpcurproc;
   1043 
   1044 	frame = p->p_md.md_tf;
   1045 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
   1046 	fsize = sizeof ksc;
   1047 	rndfsize = ((fsize + 15) / 16) * 16;
   1048 	/*
   1049 	 * Allocate and validate space for the signal handler
   1050 	 * context. Note that if the stack is in P0 space, the
   1051 	 * call to grow() is a nop, and the useracc() check
   1052 	 * will fail if the process has not already allocated
   1053 	 * the space with a `brk'.
   1054 	 */
   1055 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
   1056 	    (psp->ps_sigonstack & sigmask(sig))) {
   1057 		scp = (struct sigcontext *)(psp->ps_sigstk.ss_base +
   1058 		    psp->ps_sigstk.ss_size - rndfsize);
   1059 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
   1060 	} else
   1061 		scp = (struct sigcontext *)(frame->tf_regs[FRAME_SP] -
   1062 		    rndfsize);
   1063 	if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
   1064 		(void)grow(p, (u_long)scp);
   1065 #ifdef DEBUG
   1066 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1067 		printf("sendsig(%d): sig %d ssp %lx usp %lx\n", p->p_pid,
   1068 		    sig, &oonstack, scp);
   1069 #endif
   1070 	if (useracc((caddr_t)scp, fsize, B_WRITE) == 0) {
   1071 #ifdef DEBUG
   1072 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1073 			printf("sendsig(%d): useracc failed on sig %d\n",
   1074 			    p->p_pid, sig);
   1075 #endif
   1076 		/*
   1077 		 * Process has trashed its stack; give it an illegal
   1078 		 * instruction to halt it in its tracks.
   1079 		 */
   1080 		SIGACTION(p, SIGILL) = SIG_DFL;
   1081 		sig = sigmask(SIGILL);
   1082 		p->p_sigignore &= ~sig;
   1083 		p->p_sigcatch &= ~sig;
   1084 		p->p_sigmask &= ~sig;
   1085 		psignal(p, SIGILL);
   1086 		return;
   1087 	}
   1088 
   1089 	/*
   1090 	 * Build the signal context to be used by sigreturn.
   1091 	 */
   1092 	ksc.sc_onstack = oonstack;
   1093 	ksc.sc_mask = mask;
   1094 	ksc.sc_pc = frame->tf_pc;
   1095 	ksc.sc_ps = frame->tf_ps;
   1096 
   1097 	/* copy the registers. */
   1098 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1099 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1100 
   1101 	/* save the floating-point state, if necessary, then copy it. */
   1102 	if (p == fpcurproc) {
   1103 		pal_wrfen(1);
   1104 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
   1105 		pal_wrfen(0);
   1106 		fpcurproc = NULL;
   1107 	}
   1108 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
   1109 	bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1110 	    sizeof(struct fpreg));
   1111 	ksc.sc_fp_control = 0;					/* XXX ? */
   1112 	bzero(ksc.sc_reserved, sizeof ksc.sc_reserved);		/* XXX */
   1113 	bzero(ksc.sc_xxx, sizeof ksc.sc_xxx);			/* XXX */
   1114 
   1115 
   1116 #ifdef COMPAT_OSF1
   1117 	/*
   1118 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1119 	 */
   1120 #endif
   1121 
   1122 	/*
   1123 	 * copy the frame out to userland.
   1124 	 */
   1125 	(void) copyout((caddr_t)&ksc, (caddr_t)scp, fsize);
   1126 #ifdef DEBUG
   1127 	if (sigdebug & SDB_FOLLOW)
   1128 		printf("sendsig(%d): sig %d scp %lx code %lx\n", p->p_pid, sig,
   1129 		    scp, code);
   1130 #endif
   1131 
   1132 	/*
   1133 	 * Set up the registers to return to sigcode.
   1134 	 */
   1135 	frame->tf_pc = (u_int64_t)PS_STRINGS - (esigcode - sigcode);
   1136 	frame->tf_regs[FRAME_SP] = (u_int64_t)scp;
   1137 	frame->tf_a0 = sig;
   1138 	frame->tf_a1 = code;
   1139 	frame->tf_a2 = (u_int64_t)scp;
   1140 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
   1141 
   1142 #ifdef DEBUG
   1143 	if (sigdebug & SDB_FOLLOW)
   1144 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1145 		    frame->tf_pc, frame->tf_regs[FRAME_A3]);
   1146 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1147 		printf("sendsig(%d): sig %d returns\n",
   1148 		    p->p_pid, sig);
   1149 #endif
   1150 }
   1151 
   1152 /*
   1153  * System call to cleanup state after a signal
   1154  * has been taken.  Reset signal mask and
   1155  * stack state from context left by sendsig (above).
   1156  * Return to previous pc and psl as specified by
   1157  * context left by sendsig. Check carefully to
   1158  * make sure that the user has not modified the
   1159  * psl to gain improper priviledges or to cause
   1160  * a machine fault.
   1161  */
   1162 /* ARGSUSED */
   1163 int
   1164 sys_sigreturn(p, v, retval)
   1165 	struct proc *p;
   1166 	void *v;
   1167 	register_t *retval;
   1168 {
   1169 	struct sys_sigreturn_args /* {
   1170 		syscallarg(struct sigcontext *) sigcntxp;
   1171 	} */ *uap = v;
   1172 	struct sigcontext *scp, ksc;
   1173 	extern struct proc *fpcurproc;
   1174 
   1175 	scp = SCARG(uap, sigcntxp);
   1176 #ifdef DEBUG
   1177 	if (sigdebug & SDB_FOLLOW)
   1178 	    printf("sigreturn: pid %d, scp %lx\n", p->p_pid, scp);
   1179 #endif
   1180 
   1181 	if (ALIGN(scp) != (u_int64_t)scp)
   1182 		return (EINVAL);
   1183 
   1184 	/*
   1185 	 * Test and fetch the context structure.
   1186 	 * We grab it all at once for speed.
   1187 	 */
   1188 	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
   1189 	    copyin((caddr_t)scp, (caddr_t)&ksc, sizeof ksc))
   1190 		return (EINVAL);
   1191 
   1192 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1193 		return (EINVAL);
   1194 	/*
   1195 	 * Restore the user-supplied information
   1196 	 */
   1197 	if (ksc.sc_onstack)
   1198 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
   1199 	else
   1200 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1201 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
   1202 
   1203 	p->p_md.md_tf->tf_pc = ksc.sc_pc;
   1204 	p->p_md.md_tf->tf_ps = (ksc.sc_ps | PSL_USERSET) & ~PSL_USERCLR;
   1205 
   1206 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
   1207 
   1208 	/* XXX ksc.sc_ownedfp ? */
   1209 	if (p == fpcurproc)
   1210 		fpcurproc = NULL;
   1211 	bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
   1212 	    sizeof(struct fpreg));
   1213 	/* XXX ksc.sc_fp_control ? */
   1214 
   1215 #ifdef DEBUG
   1216 	if (sigdebug & SDB_FOLLOW)
   1217 		printf("sigreturn(%d): returns\n", p->p_pid);
   1218 #endif
   1219 	return (EJUSTRETURN);
   1220 }
   1221 
   1222 /*
   1223  * machine dependent system variables.
   1224  */
   1225 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1226 	int *name;
   1227 	u_int namelen;
   1228 	void *oldp;
   1229 	size_t *oldlenp;
   1230 	void *newp;
   1231 	size_t newlen;
   1232 	struct proc *p;
   1233 {
   1234 	dev_t consdev;
   1235 
   1236 	/* all sysctl names at this level are terminal */
   1237 	if (namelen != 1)
   1238 		return (ENOTDIR);		/* overloaded */
   1239 
   1240 	switch (name[0]) {
   1241 	case CPU_CONSDEV:
   1242 		if (cn_tab != NULL)
   1243 			consdev = cn_tab->cn_dev;
   1244 		else
   1245 			consdev = NODEV;
   1246 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1247 			sizeof consdev));
   1248 	default:
   1249 		return (EOPNOTSUPP);
   1250 	}
   1251 	/* NOTREACHED */
   1252 }
   1253 
   1254 /*
   1255  * Set registers on exec.
   1256  */
   1257 void
   1258 setregs(p, pack, stack, retval)
   1259 	register struct proc *p;
   1260 	struct exec_package *pack;
   1261 	u_long stack;
   1262 	register_t *retval;
   1263 {
   1264 	struct trapframe *tfp = p->p_md.md_tf;
   1265 	int i;
   1266 	extern struct proc *fpcurproc;
   1267 
   1268 #ifdef DEBUG
   1269 	for (i = 0; i < FRAME_NSAVEREGS; i++)
   1270 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1271 	tfp->tf_gp = 0xbabefacedeadbeef;
   1272 	tfp->tf_a0 = 0xbabefacedeadbeef;
   1273 	tfp->tf_a1 = 0xbabefacedeadbeef;
   1274 	tfp->tf_a2 = 0xbabefacedeadbeef;
   1275 #else
   1276 	bzero(tfp->tf_regs, FRAME_NSAVEREGS * sizeof tfp->tf_regs[0]);
   1277 	tfp->tf_gp = 0;
   1278 	tfp->tf_a0 = 0;
   1279 	tfp->tf_a1 = 0;
   1280 	tfp->tf_a2 = 0;
   1281 #endif
   1282 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
   1283 #define FP_RN 2 /* XXX */
   1284 	p->p_addr->u_pcb.pcb_fp.fpr_cr = (long)FP_RN << 58;
   1285 	tfp->tf_regs[FRAME_SP] = stack;	/* restored to usp in trap return */
   1286 	tfp->tf_ps = PSL_USERSET;
   1287 	tfp->tf_pc = pack->ep_entry & ~3;
   1288 
   1289 	p->p_md.md_flags & ~MDP_FPUSED;
   1290 	if (fpcurproc == p)
   1291 		fpcurproc = NULL;
   1292 
   1293 	retval[0] = retval[1] = 0;
   1294 }
   1295 
   1296 void
   1297 netintr()
   1298 {
   1299 #ifdef INET
   1300 #if NETHER > 0
   1301 	if (netisr & (1 << NETISR_ARP)) {
   1302 		netisr &= ~(1 << NETISR_ARP);
   1303 		arpintr();
   1304 	}
   1305 #endif
   1306 	if (netisr & (1 << NETISR_IP)) {
   1307 		netisr &= ~(1 << NETISR_IP);
   1308 		ipintr();
   1309 	}
   1310 #endif
   1311 #ifdef NS
   1312 	if (netisr & (1 << NETISR_NS)) {
   1313 		netisr &= ~(1 << NETISR_NS);
   1314 		nsintr();
   1315 	}
   1316 #endif
   1317 #ifdef ISO
   1318 	if (netisr & (1 << NETISR_ISO)) {
   1319 		netisr &= ~(1 << NETISR_ISO);
   1320 		clnlintr();
   1321 	}
   1322 #endif
   1323 #ifdef CCITT
   1324 	if (netisr & (1 << NETISR_CCITT)) {
   1325 		netisr &= ~(1 << NETISR_CCITT);
   1326 		ccittintr();
   1327 	}
   1328 #endif
   1329 #ifdef PPP
   1330 	if (netisr & (1 << NETISR_PPP)) {
   1331 		netisr &= ~(1 << NETISR_CCITT);
   1332 		pppintr();
   1333 	}
   1334 #endif
   1335 }
   1336 
   1337 void
   1338 do_sir()
   1339 {
   1340 
   1341 	if (ssir & SIR_NET) {
   1342 		siroff(SIR_NET);
   1343 		cnt.v_soft++;
   1344 		netintr();
   1345 	}
   1346 	if (ssir & SIR_CLOCK) {
   1347 		siroff(SIR_CLOCK);
   1348 		cnt.v_soft++;
   1349 		softclock();
   1350 	}
   1351 }
   1352 
   1353 int
   1354 spl0()
   1355 {
   1356 
   1357 	if (ssir) {
   1358 		splsoft();
   1359 		do_sir();
   1360 	}
   1361 
   1362 	return (pal_swpipl(PSL_IPL_0));
   1363 }
   1364 
   1365 /*
   1366  * The following primitives manipulate the run queues.  _whichqs tells which
   1367  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
   1368  * into queues, Remrq removes them from queues.  The running process is on
   1369  * no queue, other processes are on a queue related to p->p_priority, divided
   1370  * by 4 actually to shrink the 0-127 range of priorities into the 32 available
   1371  * queues.
   1372  */
   1373 /*
   1374  * setrunqueue(p)
   1375  *	proc *p;
   1376  *
   1377  * Call should be made at splclock(), and p->p_stat should be SRUN.
   1378  */
   1379 
   1380 void
   1381 setrunqueue(p)
   1382 	struct proc *p;
   1383 {
   1384 	int bit;
   1385 
   1386 	/* firewall: p->p_back must be NULL */
   1387 	if (p->p_back != NULL)
   1388 		panic("setrunqueue");
   1389 
   1390 	bit = p->p_priority >> 2;
   1391 	whichqs |= (1 << bit);
   1392 	p->p_forw = (struct proc *)&qs[bit];
   1393 	p->p_back = qs[bit].ph_rlink;
   1394 	p->p_back->p_forw = p;
   1395 	qs[bit].ph_rlink = p;
   1396 }
   1397 
   1398 /*
   1399  * Remrq(p)
   1400  *
   1401  * Call should be made at splclock().
   1402  */
   1403 void
   1404 remrq(p)
   1405 	struct proc *p;
   1406 {
   1407 	int bit;
   1408 
   1409 	bit = p->p_priority >> 2;
   1410 	if ((whichqs & (1 << bit)) == 0)
   1411 		panic("remrq");
   1412 
   1413 	p->p_back->p_forw = p->p_forw;
   1414 	p->p_forw->p_back = p->p_back;
   1415 	p->p_back = NULL;	/* for firewall checking. */
   1416 
   1417 	if ((struct proc *)&qs[bit] == qs[bit].ph_link)
   1418 		whichqs &= ~(1 << bit);
   1419 }
   1420 
   1421 /*
   1422  * Return the best possible estimate of the time in the timeval
   1423  * to which tvp points.  Unfortunately, we can't read the hardware registers.
   1424  * We guarantee that the time will be greater than the value obtained by a
   1425  * previous call.
   1426  */
   1427 void
   1428 microtime(tvp)
   1429 	register struct timeval *tvp;
   1430 {
   1431 	int s = splclock();
   1432 	static struct timeval lasttime;
   1433 
   1434 	*tvp = time;
   1435 #ifdef notdef
   1436 	tvp->tv_usec += clkread();
   1437 	while (tvp->tv_usec > 1000000) {
   1438 		tvp->tv_sec++;
   1439 		tvp->tv_usec -= 1000000;
   1440 	}
   1441 #endif
   1442 	if (tvp->tv_sec == lasttime.tv_sec &&
   1443 	    tvp->tv_usec <= lasttime.tv_usec &&
   1444 	    (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
   1445 		tvp->tv_sec++;
   1446 		tvp->tv_usec -= 1000000;
   1447 	}
   1448 	lasttime = *tvp;
   1449 	splx(s);
   1450 }
   1451 
   1452 #if defined(COMPAT_OSF1) || 1		/* XXX */
   1453 void
   1454 cpu_exec_ecoff_setregs(p, pack, stack, retval)
   1455 	struct proc *p;
   1456 	struct exec_package *pack;
   1457 	u_long stack;
   1458 	register_t *retval;
   1459 {
   1460 	struct ecoff_aouthdr *eap;
   1461 
   1462 	setregs(p, pack, stack, retval);
   1463 
   1464 	eap = (struct ecoff_aouthdr *)
   1465 	    ((caddr_t)pack->ep_hdr + sizeof(struct ecoff_filehdr));
   1466 	p->p_md.md_tf->tf_gp = eap->ea_gp_value;
   1467 }
   1468 
   1469 /*
   1470  * cpu_exec_ecoff_hook():
   1471  *	cpu-dependent ECOFF format hook for execve().
   1472  *
   1473  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   1474  *
   1475  */
   1476 int
   1477 cpu_exec_ecoff_hook(p, epp, eap)
   1478 	struct proc *p;
   1479 	struct exec_package *epp;
   1480 	struct ecoff_aouthdr *eap;
   1481 {
   1482 	struct ecoff_filehdr *efp = epp->ep_hdr;
   1483 	extern struct emul emul_netbsd;
   1484 #ifdef COMPAT_OSF1
   1485 	extern struct emul emul_osf1;
   1486 #endif
   1487 
   1488 	switch (efp->ef_magic) {
   1489 #ifdef COMPAT_OSF1
   1490 	case ECOFF_MAGIC_ALPHA:
   1491 		epp->ep_emul = &emul_osf1;
   1492 		break;
   1493 #endif
   1494 
   1495 	case ECOFF_MAGIC_NETBSD_ALPHA:
   1496 		epp->ep_emul = &emul_netbsd;
   1497 		break;
   1498 
   1499 	default:
   1500 		return ENOEXEC;
   1501 	}
   1502 	return 0;
   1503 }
   1504 #endif
   1505 
   1506 vm_offset_t
   1507 vtophys(vaddr)
   1508 	vm_offset_t vaddr;
   1509 {
   1510 	vm_offset_t paddr;
   1511 
   1512 	if (vaddr < K0SEG_BEGIN) {
   1513 		printf("vtophys: invalid vaddr 0x%lx", vaddr);
   1514 		paddr = vaddr;
   1515 	} else if (vaddr < K0SEG_END)
   1516 		paddr = k0segtophys(vaddr);
   1517 	else
   1518 		paddr = vatopa(vaddr);
   1519 
   1520 #if 0
   1521 	printf("vtophys(0x%lx) -> %lx\n", vaddr, paddr);
   1522 #endif
   1523 
   1524 	return (paddr);
   1525 }
   1526