Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.43
      1 /*	$NetBSD: machdep.c,v 1.43 1996/10/01 18:41:08 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 = (u_int64_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 	dumplo = -1;
    938 	return;
    939 }
    940 
    941 /*
    942  * Dump the kernel's image to the swap partition.
    943  */
    944 #define	BYTES_PER_DUMP	NBPG
    945 
    946 void
    947 dumpsys()
    948 {
    949 	unsigned bytes, i, n;
    950 	int maddr, psize;
    951 	daddr_t blkno;
    952 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
    953 	int error;
    954 
    955 	/* Save registers. */
    956 	savectx(&dumppcb);
    957 
    958 	msgbufmapped = 0;	/* don't record dump msgs in msgbuf */
    959 	if (dumpdev == NODEV)
    960 		return;
    961 
    962 	/*
    963 	 * For dumps during autoconfiguration,
    964 	 * if dump device has already configured...
    965 	 */
    966 	if (dumpsize == 0)
    967 		dumpconf();
    968 	if (dumplo < 0) {
    969 		printf("\ndump to dev %x not possible\n", dumpdev);
    970 		return;
    971 	}
    972 	printf("\ndumping to dev %x, offset %ld\n", dumpdev, dumplo);
    973 
    974 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
    975 	printf("dump ");
    976 	if (psize == -1) {
    977 		printf("area unavailable\n");
    978 		return;
    979 	}
    980 
    981 	/* XXX should purge all outstanding keystrokes. */
    982 
    983 	if ((error = cpu_dump()) != 0)
    984 		goto err;
    985 
    986 	bytes = ctob(physmem);
    987 	maddr = ctob(firstusablepage);
    988 	blkno = dumplo + cpu_dumpsize();
    989 	dump = bdevsw[major(dumpdev)].d_dump;
    990 	error = 0;
    991 	for (i = 0; i < bytes; i += n) {
    992 
    993 		/* Print out how many MBs we to go. */
    994 		n = bytes - i;
    995 		if (n && (n % (1024*1024)) == 0)
    996 			printf("%d ", n / (1024 * 1024));
    997 
    998 		/* Limit size for next transfer. */
    999 		if (n > BYTES_PER_DUMP)
   1000 			n =  BYTES_PER_DUMP;
   1001 
   1002 		error = (*dump)(dumpdev, blkno,
   1003 		    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
   1004 		if (error)
   1005 			break;
   1006 		maddr += n;
   1007 		blkno += btodb(n);			/* XXX? */
   1008 
   1009 		/* XXX should look for keystrokes, to cancel. */
   1010 	}
   1011 
   1012 err:
   1013 	switch (error) {
   1014 
   1015 	case ENXIO:
   1016 		printf("device bad\n");
   1017 		break;
   1018 
   1019 	case EFAULT:
   1020 		printf("device not ready\n");
   1021 		break;
   1022 
   1023 	case EINVAL:
   1024 		printf("area improper\n");
   1025 		break;
   1026 
   1027 	case EIO:
   1028 		printf("i/o error\n");
   1029 		break;
   1030 
   1031 	case EINTR:
   1032 		printf("aborted from console\n");
   1033 		break;
   1034 
   1035 	case 0:
   1036 		printf("succeeded\n");
   1037 		break;
   1038 
   1039 	default:
   1040 		printf("error %d\n", error);
   1041 		break;
   1042 	}
   1043 	printf("\n\n");
   1044 	delay(1000);
   1045 }
   1046 
   1047 void
   1048 frametoreg(framep, regp)
   1049 	struct trapframe *framep;
   1050 	struct reg *regp;
   1051 {
   1052 
   1053 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1054 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1055 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1056 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1057 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1058 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1059 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1060 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1061 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1062 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1063 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1064 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1065 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1066 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1067 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1068 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1069 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1070 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1071 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1072 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1073 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1074 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1075 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1076 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1077 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1078 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1079 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1080 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1081 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1082 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1083 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1084 	regp->r_regs[R_ZERO] = 0;
   1085 }
   1086 
   1087 void
   1088 regtoframe(regp, framep)
   1089 	struct reg *regp;
   1090 	struct trapframe *framep;
   1091 {
   1092 
   1093 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1094 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1095 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1096 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1097 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1098 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1099 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1100 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1101 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1102 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1103 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1104 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1105 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1106 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1107 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1108 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1109 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1110 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1111 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1112 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1113 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1114 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1115 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1116 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1117 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1118 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1119 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1120 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1121 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1122 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1123 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1124 	/* ??? = regp->r_regs[R_ZERO]; */
   1125 }
   1126 
   1127 void
   1128 printregs(regp)
   1129 	struct reg *regp;
   1130 {
   1131 	int i;
   1132 
   1133 	for (i = 0; i < 32; i++)
   1134 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1135 		   i & 1 ? "\n" : "\t");
   1136 }
   1137 
   1138 void
   1139 regdump(framep)
   1140 	struct trapframe *framep;
   1141 {
   1142 	struct reg reg;
   1143 
   1144 	frametoreg(framep, &reg);
   1145 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1146 
   1147 	printf("REGISTERS:\n");
   1148 	printregs(&reg);
   1149 }
   1150 
   1151 #ifdef DEBUG
   1152 int sigdebug = 0;
   1153 int sigpid = 0;
   1154 #define	SDB_FOLLOW	0x01
   1155 #define	SDB_KSTACK	0x02
   1156 #endif
   1157 
   1158 /*
   1159  * Send an interrupt to process.
   1160  */
   1161 void
   1162 sendsig(catcher, sig, mask, code)
   1163 	sig_t catcher;
   1164 	int sig, mask;
   1165 	u_long code;
   1166 {
   1167 	struct proc *p = curproc;
   1168 	struct sigcontext *scp, ksc;
   1169 	struct trapframe *frame;
   1170 	struct sigacts *psp = p->p_sigacts;
   1171 	int oonstack, fsize, rndfsize;
   1172 	extern char sigcode[], esigcode[];
   1173 	extern struct proc *fpcurproc;
   1174 
   1175 	frame = p->p_md.md_tf;
   1176 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
   1177 	fsize = sizeof ksc;
   1178 	rndfsize = ((fsize + 15) / 16) * 16;
   1179 	/*
   1180 	 * Allocate and validate space for the signal handler
   1181 	 * context. Note that if the stack is in P0 space, the
   1182 	 * call to grow() is a nop, and the useracc() check
   1183 	 * will fail if the process has not already allocated
   1184 	 * the space with a `brk'.
   1185 	 */
   1186 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
   1187 	    (psp->ps_sigonstack & sigmask(sig))) {
   1188 		scp = (struct sigcontext *)(psp->ps_sigstk.ss_sp +
   1189 		    psp->ps_sigstk.ss_size - rndfsize);
   1190 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
   1191 	} else
   1192 		scp = (struct sigcontext *)(alpha_pal_rdusp() - rndfsize);
   1193 	if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
   1194 		(void)grow(p, (u_long)scp);
   1195 #ifdef DEBUG
   1196 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1197 		printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1198 		    sig, &oonstack, scp);
   1199 #endif
   1200 	if (useracc((caddr_t)scp, fsize, B_WRITE) == 0) {
   1201 #ifdef DEBUG
   1202 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1203 			printf("sendsig(%d): useracc failed on sig %d\n",
   1204 			    p->p_pid, sig);
   1205 #endif
   1206 		/*
   1207 		 * Process has trashed its stack; give it an illegal
   1208 		 * instruction to halt it in its tracks.
   1209 		 */
   1210 		SIGACTION(p, SIGILL) = SIG_DFL;
   1211 		sig = sigmask(SIGILL);
   1212 		p->p_sigignore &= ~sig;
   1213 		p->p_sigcatch &= ~sig;
   1214 		p->p_sigmask &= ~sig;
   1215 		psignal(p, SIGILL);
   1216 		return;
   1217 	}
   1218 
   1219 	/*
   1220 	 * Build the signal context to be used by sigreturn.
   1221 	 */
   1222 	ksc.sc_onstack = oonstack;
   1223 	ksc.sc_mask = mask;
   1224 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
   1225 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
   1226 
   1227 	/* copy the registers. */
   1228 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1229 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1230 	ksc.sc_regs[R_SP] = alpha_pal_rdusp();
   1231 
   1232 	/* save the floating-point state, if necessary, then copy it. */
   1233 	if (p == fpcurproc) {
   1234 		alpha_pal_wrfen(1);
   1235 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
   1236 		alpha_pal_wrfen(0);
   1237 		fpcurproc = NULL;
   1238 	}
   1239 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
   1240 	bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1241 	    sizeof(struct fpreg));
   1242 	ksc.sc_fp_control = 0;					/* XXX ? */
   1243 	bzero(ksc.sc_reserved, sizeof ksc.sc_reserved);		/* XXX */
   1244 	bzero(ksc.sc_xxx, sizeof ksc.sc_xxx);			/* XXX */
   1245 
   1246 
   1247 #ifdef COMPAT_OSF1
   1248 	/*
   1249 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1250 	 */
   1251 #endif
   1252 
   1253 	/*
   1254 	 * copy the frame out to userland.
   1255 	 */
   1256 	(void) copyout((caddr_t)&ksc, (caddr_t)scp, fsize);
   1257 #ifdef DEBUG
   1258 	if (sigdebug & SDB_FOLLOW)
   1259 		printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
   1260 		    scp, code);
   1261 #endif
   1262 
   1263 	/*
   1264 	 * Set up the registers to return to sigcode.
   1265 	 */
   1266 	frame->tf_regs[FRAME_PC] =
   1267 	    (u_int64_t)PS_STRINGS - (esigcode - sigcode);
   1268 	frame->tf_regs[FRAME_A0] = sig;
   1269 	frame->tf_regs[FRAME_A1] = code;
   1270 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
   1271 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
   1272 	alpha_pal_wrusp((unsigned long)scp);
   1273 
   1274 #ifdef DEBUG
   1275 	if (sigdebug & SDB_FOLLOW)
   1276 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1277 		    frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
   1278 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1279 		printf("sendsig(%d): sig %d returns\n",
   1280 		    p->p_pid, sig);
   1281 #endif
   1282 }
   1283 
   1284 /*
   1285  * System call to cleanup state after a signal
   1286  * has been taken.  Reset signal mask and
   1287  * stack state from context left by sendsig (above).
   1288  * Return to previous pc and psl as specified by
   1289  * context left by sendsig. Check carefully to
   1290  * make sure that the user has not modified the
   1291  * psl to gain improper priviledges or to cause
   1292  * a machine fault.
   1293  */
   1294 /* ARGSUSED */
   1295 int
   1296 sys_sigreturn(p, v, retval)
   1297 	struct proc *p;
   1298 	void *v;
   1299 	register_t *retval;
   1300 {
   1301 	struct sys_sigreturn_args /* {
   1302 		syscallarg(struct sigcontext *) sigcntxp;
   1303 	} */ *uap = v;
   1304 	struct sigcontext *scp, ksc;
   1305 	extern struct proc *fpcurproc;
   1306 
   1307 	scp = SCARG(uap, sigcntxp);
   1308 #ifdef DEBUG
   1309 	if (sigdebug & SDB_FOLLOW)
   1310 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
   1311 #endif
   1312 
   1313 	if (ALIGN(scp) != (u_int64_t)scp)
   1314 		return (EINVAL);
   1315 
   1316 	/*
   1317 	 * Test and fetch the context structure.
   1318 	 * We grab it all at once for speed.
   1319 	 */
   1320 	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
   1321 	    copyin((caddr_t)scp, (caddr_t)&ksc, sizeof ksc))
   1322 		return (EINVAL);
   1323 
   1324 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1325 		return (EINVAL);
   1326 	/*
   1327 	 * Restore the user-supplied information
   1328 	 */
   1329 	if (ksc.sc_onstack)
   1330 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
   1331 	else
   1332 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1333 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
   1334 
   1335 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
   1336 	p->p_md.md_tf->tf_regs[FRAME_PS] =
   1337 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
   1338 
   1339 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
   1340 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
   1341 
   1342 	/* XXX ksc.sc_ownedfp ? */
   1343 	if (p == fpcurproc)
   1344 		fpcurproc = NULL;
   1345 	bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
   1346 	    sizeof(struct fpreg));
   1347 	/* XXX ksc.sc_fp_control ? */
   1348 
   1349 #ifdef DEBUG
   1350 	if (sigdebug & SDB_FOLLOW)
   1351 		printf("sigreturn(%d): returns\n", p->p_pid);
   1352 #endif
   1353 	return (EJUSTRETURN);
   1354 }
   1355 
   1356 /*
   1357  * machine dependent system variables.
   1358  */
   1359 int
   1360 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1361 	int *name;
   1362 	u_int namelen;
   1363 	void *oldp;
   1364 	size_t *oldlenp;
   1365 	void *newp;
   1366 	size_t newlen;
   1367 	struct proc *p;
   1368 {
   1369 	dev_t consdev;
   1370 
   1371 	/* all sysctl names at this level are terminal */
   1372 	if (namelen != 1)
   1373 		return (ENOTDIR);		/* overloaded */
   1374 
   1375 	switch (name[0]) {
   1376 	case CPU_CONSDEV:
   1377 		if (cn_tab != NULL)
   1378 			consdev = cn_tab->cn_dev;
   1379 		else
   1380 			consdev = NODEV;
   1381 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1382 			sizeof consdev));
   1383 
   1384 	case CPU_ROOT_DEVICE:
   1385 		return (sysctl_rdstring(oldp, oldlenp, newp, root_device));
   1386 
   1387 	case CPU_UNALIGNED_PRINT:
   1388 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1389 		    &alpha_unaligned_print));
   1390 
   1391 	case CPU_UNALIGNED_FIX:
   1392 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1393 		    &alpha_unaligned_fix));
   1394 
   1395 	case CPU_UNALIGNED_SIGBUS:
   1396 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1397 		    &alpha_unaligned_sigbus));
   1398 
   1399 	default:
   1400 		return (EOPNOTSUPP);
   1401 	}
   1402 	/* NOTREACHED */
   1403 }
   1404 
   1405 /*
   1406  * Set registers on exec.
   1407  */
   1408 void
   1409 setregs(p, pack, stack, retval)
   1410 	register struct proc *p;
   1411 	struct exec_package *pack;
   1412 	u_long stack;
   1413 	register_t *retval;
   1414 {
   1415 	struct trapframe *tfp = p->p_md.md_tf;
   1416 	int i;
   1417 	extern struct proc *fpcurproc;
   1418 
   1419 #ifdef DEBUG
   1420 	/*
   1421 	 * Crash and dump, if the user requested it.
   1422 	 */
   1423 	if (boothowto & RB_DUMP)
   1424 		panic("crash requested by boot flags");
   1425 #endif
   1426 
   1427 #ifdef DEBUG
   1428 	for (i = 0; i < FRAME_SIZE; i++)
   1429 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1430 #else
   1431 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   1432 #endif
   1433 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
   1434 #define FP_RN 2 /* XXX */
   1435 	p->p_addr->u_pcb.pcb_fp.fpr_cr = (long)FP_RN << 58;
   1436 	alpha_pal_wrusp(stack);
   1437 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   1438 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   1439 
   1440 	tfp->tf_regs[FRAME_A0] = stack;
   1441 	/* a1 and a2 already zeroed */
   1442 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   1443 
   1444 	p->p_md.md_flags &= ~MDP_FPUSED;
   1445 	if (fpcurproc == p)
   1446 		fpcurproc = NULL;
   1447 
   1448 	retval[0] = retval[1] = 0;
   1449 }
   1450 
   1451 void
   1452 netintr()
   1453 {
   1454 #ifdef INET
   1455 #if NETHER > 0
   1456 	if (netisr & (1 << NETISR_ARP)) {
   1457 		netisr &= ~(1 << NETISR_ARP);
   1458 		arpintr();
   1459 	}
   1460 #endif
   1461 	if (netisr & (1 << NETISR_IP)) {
   1462 		netisr &= ~(1 << NETISR_IP);
   1463 		ipintr();
   1464 	}
   1465 #endif
   1466 #ifdef NS
   1467 	if (netisr & (1 << NETISR_NS)) {
   1468 		netisr &= ~(1 << NETISR_NS);
   1469 		nsintr();
   1470 	}
   1471 #endif
   1472 #ifdef ISO
   1473 	if (netisr & (1 << NETISR_ISO)) {
   1474 		netisr &= ~(1 << NETISR_ISO);
   1475 		clnlintr();
   1476 	}
   1477 #endif
   1478 #ifdef CCITT
   1479 	if (netisr & (1 << NETISR_CCITT)) {
   1480 		netisr &= ~(1 << NETISR_CCITT);
   1481 		ccittintr();
   1482 	}
   1483 #endif
   1484 #ifdef PPP
   1485 	if (netisr & (1 << NETISR_PPP)) {
   1486 		netisr &= ~(1 << NETISR_PPP);
   1487 		pppintr();
   1488 	}
   1489 #endif
   1490 }
   1491 
   1492 void
   1493 do_sir()
   1494 {
   1495 
   1496 	if (ssir & SIR_NET) {
   1497 		siroff(SIR_NET);
   1498 		cnt.v_soft++;
   1499 		netintr();
   1500 	}
   1501 	if (ssir & SIR_CLOCK) {
   1502 		siroff(SIR_CLOCK);
   1503 		cnt.v_soft++;
   1504 		softclock();
   1505 	}
   1506 }
   1507 
   1508 int
   1509 spl0()
   1510 {
   1511 
   1512 	if (ssir) {
   1513 		splsoft();
   1514 		do_sir();
   1515 	}
   1516 
   1517 	return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
   1518 }
   1519 
   1520 /*
   1521  * The following primitives manipulate the run queues.  _whichqs tells which
   1522  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
   1523  * into queues, Remrq removes them from queues.  The running process is on
   1524  * no queue, other processes are on a queue related to p->p_priority, divided
   1525  * by 4 actually to shrink the 0-127 range of priorities into the 32 available
   1526  * queues.
   1527  */
   1528 /*
   1529  * setrunqueue(p)
   1530  *	proc *p;
   1531  *
   1532  * Call should be made at splclock(), and p->p_stat should be SRUN.
   1533  */
   1534 
   1535 void
   1536 setrunqueue(p)
   1537 	struct proc *p;
   1538 {
   1539 	int bit;
   1540 
   1541 	/* firewall: p->p_back must be NULL */
   1542 	if (p->p_back != NULL)
   1543 		panic("setrunqueue");
   1544 
   1545 	bit = p->p_priority >> 2;
   1546 	whichqs |= (1 << bit);
   1547 	p->p_forw = (struct proc *)&qs[bit];
   1548 	p->p_back = qs[bit].ph_rlink;
   1549 	p->p_back->p_forw = p;
   1550 	qs[bit].ph_rlink = p;
   1551 }
   1552 
   1553 /*
   1554  * Remrq(p)
   1555  *
   1556  * Call should be made at splclock().
   1557  */
   1558 void
   1559 remrq(p)
   1560 	struct proc *p;
   1561 {
   1562 	int bit;
   1563 
   1564 	bit = p->p_priority >> 2;
   1565 	if ((whichqs & (1 << bit)) == 0)
   1566 		panic("remrq");
   1567 
   1568 	p->p_back->p_forw = p->p_forw;
   1569 	p->p_forw->p_back = p->p_back;
   1570 	p->p_back = NULL;	/* for firewall checking. */
   1571 
   1572 	if ((struct proc *)&qs[bit] == qs[bit].ph_link)
   1573 		whichqs &= ~(1 << bit);
   1574 }
   1575 
   1576 /*
   1577  * Return the best possible estimate of the time in the timeval
   1578  * to which tvp points.  Unfortunately, we can't read the hardware registers.
   1579  * We guarantee that the time will be greater than the value obtained by a
   1580  * previous call.
   1581  */
   1582 void
   1583 microtime(tvp)
   1584 	register struct timeval *tvp;
   1585 {
   1586 	int s = splclock();
   1587 	static struct timeval lasttime;
   1588 
   1589 	*tvp = time;
   1590 #ifdef notdef
   1591 	tvp->tv_usec += clkread();
   1592 	while (tvp->tv_usec > 1000000) {
   1593 		tvp->tv_sec++;
   1594 		tvp->tv_usec -= 1000000;
   1595 	}
   1596 #endif
   1597 	if (tvp->tv_sec == lasttime.tv_sec &&
   1598 	    tvp->tv_usec <= lasttime.tv_usec &&
   1599 	    (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
   1600 		tvp->tv_sec++;
   1601 		tvp->tv_usec -= 1000000;
   1602 	}
   1603 	lasttime = *tvp;
   1604 	splx(s);
   1605 }
   1606 
   1607 /*
   1608  * Wait "n" microseconds.
   1609  */
   1610 void
   1611 delay(n)
   1612 	unsigned long n;
   1613 {
   1614 	long N = cycles_per_usec * (n);
   1615 
   1616 	while (N > 0)				/* XXX */
   1617 		N -= 3;				/* XXX */
   1618 }
   1619 
   1620 #if defined(COMPAT_OSF1) || 1		/* XXX */
   1621 void
   1622 cpu_exec_ecoff_setregs(p, epp, stack, retval)
   1623 	struct proc *p;
   1624 	struct exec_package *epp;
   1625 	u_long stack;
   1626 	register_t *retval;
   1627 {
   1628 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1629 
   1630 	setregs(p, epp, stack, retval);
   1631 	p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   1632 }
   1633 
   1634 /*
   1635  * cpu_exec_ecoff_hook():
   1636  *	cpu-dependent ECOFF format hook for execve().
   1637  *
   1638  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   1639  *
   1640  */
   1641 int
   1642 cpu_exec_ecoff_hook(p, epp)
   1643 	struct proc *p;
   1644 	struct exec_package *epp;
   1645 {
   1646 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1647 	extern struct emul emul_netbsd;
   1648 #ifdef COMPAT_OSF1
   1649 	extern struct emul emul_osf1;
   1650 #endif
   1651 
   1652 	switch (execp->f.f_magic) {
   1653 #ifdef COMPAT_OSF1
   1654 	case ECOFF_MAGIC_ALPHA:
   1655 		epp->ep_emul = &emul_osf1;
   1656 		break;
   1657 #endif
   1658 
   1659 	case ECOFF_MAGIC_NETBSD_ALPHA:
   1660 		epp->ep_emul = &emul_netbsd;
   1661 		break;
   1662 
   1663 	default:
   1664 		return ENOEXEC;
   1665 	}
   1666 	return 0;
   1667 }
   1668 #endif
   1669