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