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