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