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