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