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