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