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