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