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