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