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