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