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