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