Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.65
      1 /*	$NetBSD: machdep.c,v 1.65 1997/02/03 20:02:02 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/signalvar.h>
     33 #include <sys/kernel.h>
     34 #include <sys/map.h>
     35 #include <sys/proc.h>
     36 #include <sys/buf.h>
     37 #include <sys/reboot.h>
     38 #include <sys/device.h>
     39 #include <sys/conf.h>
     40 #include <sys/file.h>
     41 #ifdef REAL_CLISTS
     42 #include <sys/clist.h>
     43 #endif
     44 #include <sys/callout.h>
     45 #include <sys/malloc.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/msgbuf.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/tty.h>
     50 #include <sys/user.h>
     51 #include <sys/exec.h>
     52 #include <sys/exec_ecoff.h>
     53 #include <sys/sysctl.h>
     54 #include <sys/core.h>
     55 #include <sys/kcore.h>
     56 #include <machine/kcore.h>
     57 #ifdef SYSVMSG
     58 #include <sys/msg.h>
     59 #endif
     60 #ifdef SYSVSEM
     61 #include <sys/sem.h>
     62 #endif
     63 #ifdef SYSVSHM
     64 #include <sys/shm.h>
     65 #endif
     66 
     67 #include <sys/mount.h>
     68 #include <sys/syscallargs.h>
     69 
     70 #include <vm/vm_kern.h>
     71 
     72 #include <dev/cons.h>
     73 
     74 #include <machine/cpu.h>
     75 #include <machine/reg.h>
     76 #include <machine/rpb.h>
     77 #include <machine/prom.h>
     78 #include <machine/cpuconf.h>
     79 
     80 #include <net/netisr.h>
     81 #include <net/if.h>
     82 
     83 #ifdef INET
     84 #include <netinet/in.h>
     85 #include <netinet/if_ether.h>
     86 #include <netinet/ip_var.h>
     87 #endif
     88 #ifdef NS
     89 #include <netns/ns_var.h>
     90 #endif
     91 #ifdef ISO
     92 #include <netiso/iso.h>
     93 #include <netiso/clnp.h>
     94 #endif
     95 #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 		case 's': /* single-user (default, supported for sanity) */
    570 		case 'S':
    571 			boothowto |= RB_SINGLE;
    572 			break;
    573 
    574 		default:
    575 			printf("Unrecognized boot flag '%c'.\n", *p);
    576 			break;
    577 		}
    578 	}
    579 
    580 	/*
    581 	 * Figure out the number of cpus in the box, from RPB fields.
    582 	 * Really.  We mean it.
    583 	 */
    584 	for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
    585 		struct pcs *pcsp;
    586 
    587 		pcsp = (struct pcs *)((char *)hwrpb + hwrpb->rpb_pcs_off +
    588 		    (i * hwrpb->rpb_pcs_size));
    589 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    590 			ncpus++;
    591 	}
    592 }
    593 
    594 void
    595 consinit()
    596 {
    597 
    598 	(*cpu_fn_switch->cons_init)();
    599 	pmap_unmap_prom();
    600 }
    601 
    602 void
    603 cpu_startup()
    604 {
    605 	register unsigned i;
    606 	int base, residual;
    607 	vm_offset_t minaddr, maxaddr;
    608 	vm_size_t size;
    609 #if defined(DEBUG)
    610 	extern int pmapdebug;
    611 	int opmapdebug = pmapdebug;
    612 
    613 	pmapdebug = 0;
    614 #endif
    615 
    616 	/*
    617 	 * Good {morning,afternoon,evening,night}.
    618 	 */
    619 	printf(version);
    620 	identifycpu();
    621 	printf("real mem = %d (%d reserved for PROM, %d used by NetBSD)\n",
    622 	    ctob(totalphysmem), ctob(resvmem), ctob(physmem));
    623 	if (unusedmem)
    624 		printf("WARNING: unused memory = %d bytes\n", ctob(unusedmem));
    625 	if (unknownmem)
    626 		printf("WARNING: %d bytes of memory with unknown purpose\n",
    627 		    ctob(unknownmem));
    628 
    629 	/*
    630 	 * Allocate virtual address space for file I/O buffers.
    631 	 * Note they are different than the array of headers, 'buf',
    632 	 * and usually occupy more virtual memory than physical.
    633 	 */
    634 	size = MAXBSIZE * nbuf;
    635 	buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
    636 	    &maxaddr, size, TRUE);
    637 	minaddr = (vm_offset_t)buffers;
    638 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
    639 			&minaddr, size, FALSE) != KERN_SUCCESS)
    640 		panic("startup: cannot allocate buffers");
    641 	base = bufpages / nbuf;
    642 	residual = bufpages % nbuf;
    643 	for (i = 0; i < nbuf; i++) {
    644 		vm_size_t curbufsize;
    645 		vm_offset_t curbuf;
    646 
    647 		/*
    648 		 * First <residual> buffers get (base+1) physical pages
    649 		 * allocated for them.  The rest get (base) physical pages.
    650 		 *
    651 		 * The rest of each buffer occupies virtual space,
    652 		 * but has no physical memory allocated for it.
    653 		 */
    654 		curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
    655 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
    656 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
    657 		vm_map_simplify(buffer_map, curbuf);
    658 	}
    659 	/*
    660 	 * Allocate a submap for exec arguments.  This map effectively
    661 	 * limits the number of processes exec'ing at any time.
    662 	 */
    663 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    664 				 16 * NCARGS, TRUE);
    665 
    666 	/*
    667 	 * Allocate a submap for physio
    668 	 */
    669 	phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    670 				 VM_PHYS_SIZE, TRUE);
    671 
    672 	/*
    673 	 * Finally, allocate mbuf pool.  Since mclrefcnt is an off-size
    674 	 * we use the more space efficient malloc in place of kmem_alloc.
    675 	 */
    676 	mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES,
    677 	    M_MBUF, M_NOWAIT);
    678 	bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES);
    679 	mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
    680 	    VM_MBUF_SIZE, FALSE);
    681 	/*
    682 	 * Initialize callouts
    683 	 */
    684 	callfree = callout;
    685 	for (i = 1; i < ncallout; i++)
    686 		callout[i-1].c_next = &callout[i];
    687 	callout[i-1].c_next = NULL;
    688 
    689 #if defined(DEBUG)
    690 	pmapdebug = opmapdebug;
    691 #endif
    692 	printf("avail mem = %ld\n", (long)ptoa(cnt.v_free_count));
    693 	printf("using %ld buffers containing %ld bytes of memory\n",
    694 		(long)nbuf, (long)(bufpages * CLBYTES));
    695 
    696 	/*
    697 	 * Set up buffers, so they can be used to read disk labels.
    698 	 */
    699 	bufinit();
    700 
    701 	/*
    702 	 * Configure the system.
    703 	 */
    704 	configure();
    705 
    706 	/*
    707 	 * Note that bootstrapping is finished, and set the HWRPB up
    708 	 * to do restarts.
    709 	 */
    710 	hwrpb_restart_setup();
    711 }
    712 
    713 void
    714 identifycpu()
    715 {
    716 
    717 	/*
    718 	 * print out CPU identification information.
    719 	 */
    720 	printf("%s, %ldMHz\n", cpu_model,
    721 	    hwrpb->rpb_cc_freq / 1000000);	/* XXX true for 21164? */
    722 	printf("%ld byte page size, %d processor%s.\n",
    723 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
    724 #if 0
    725 	/* this isn't defined for any systems that we run on? */
    726 	printf("serial number 0x%lx 0x%lx\n",
    727 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
    728 
    729 	/* and these aren't particularly useful! */
    730 	printf("variation: 0x%lx, revision 0x%lx\n",
    731 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
    732 #endif
    733 }
    734 
    735 int	waittime = -1;
    736 struct pcb dumppcb;
    737 
    738 void
    739 boot(howto, bootstr)
    740 	int howto;
    741 	char *bootstr;
    742 {
    743 	extern int cold;
    744 
    745 	/* If system is cold, just halt. */
    746 	if (cold) {
    747 		howto |= RB_HALT;
    748 		goto haltsys;
    749 	}
    750 
    751 	/* If "always halt" was specified as a boot flag, obey. */
    752 	if ((boothowto & RB_HALT) != 0)
    753 		howto |= RB_HALT;
    754 
    755 	boothowto = howto;
    756 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
    757 		waittime = 0;
    758 		vfs_shutdown();
    759 		/*
    760 		 * If we've been adjusting the clock, the todr
    761 		 * will be out of synch; adjust it now.
    762 		 */
    763 		resettodr();
    764 	}
    765 
    766 	/* Disable interrupts. */
    767 	splhigh();
    768 
    769 	/* If rebooting and a dump is requested do it. */
    770 #if 0
    771 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    772 #else
    773 	if (howto & RB_DUMP)
    774 #endif
    775 		dumpsys();
    776 
    777 haltsys:
    778 
    779 	/* run any shutdown hooks */
    780 	doshutdownhooks();
    781 
    782 #ifdef BOOTKEY
    783 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
    784 	cngetc();
    785 	printf("\n");
    786 #endif
    787 
    788 	/* Finally, halt/reboot the system. */
    789 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
    790 	prom_halt(howto & RB_HALT);
    791 	/*NOTREACHED*/
    792 }
    793 
    794 /*
    795  * These variables are needed by /sbin/savecore
    796  */
    797 u_long	dumpmag = 0x8fca0101;	/* magic number */
    798 int 	dumpsize = 0;		/* pages */
    799 long	dumplo = 0; 		/* blocks */
    800 
    801 /*
    802  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
    803  */
    804 int
    805 cpu_dumpsize()
    806 {
    807 	int size;
    808 
    809 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t));
    810 	if (roundup(size, dbtob(1)) != dbtob(1))
    811 		return -1;
    812 
    813 	return (1);
    814 }
    815 
    816 /*
    817  * cpu_dump: dump machine-dependent kernel core dump headers.
    818  */
    819 int
    820 cpu_dump()
    821 {
    822 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
    823 	long buf[dbtob(1) / sizeof (long)];
    824 	kcore_seg_t	*segp;
    825 	cpu_kcore_hdr_t	*cpuhdrp;
    826 
    827         dump = bdevsw[major(dumpdev)].d_dump;
    828 
    829 	segp = (kcore_seg_t *)buf;
    830 	cpuhdrp =
    831 	    (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp)) / sizeof (long)];
    832 
    833 	/*
    834 	 * Generate a segment header.
    835 	 */
    836 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
    837 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
    838 
    839 	/*
    840 	 * Add the machine-dependent header info
    841 	 */
    842 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vm_offset_t)Lev1map);
    843 	cpuhdrp->page_size = PAGE_SIZE;
    844 	cpuhdrp->core_seg.start = ctob(firstusablepage);
    845 	cpuhdrp->core_seg.size = ctob(physmem);
    846 
    847 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
    848 }
    849 
    850 /*
    851  * This is called by configure to set dumplo and dumpsize.
    852  * Dumps always skip the first CLBYTES of disk space
    853  * in case there might be a disk label stored there.
    854  * If there is extra space, put dump at the end to
    855  * reduce the chance that swapping trashes it.
    856  */
    857 void
    858 dumpconf()
    859 {
    860 	int nblks, dumpblks;	/* size of dump area */
    861 	int maj;
    862 
    863 	if (dumpdev == NODEV)
    864 		goto bad;
    865 	maj = major(dumpdev);
    866 	if (maj < 0 || maj >= nblkdev)
    867 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
    868 	if (bdevsw[maj].d_psize == NULL)
    869 		goto bad;
    870 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
    871 	if (nblks <= ctod(1))
    872 		goto bad;
    873 
    874 	dumpblks = cpu_dumpsize();
    875 	if (dumpblks < 0)
    876 		goto bad;
    877 	dumpblks += ctod(physmem);
    878 
    879 	/* If dump won't fit (incl. room for possible label), punt. */
    880 	if (dumpblks > (nblks - ctod(1)))
    881 		goto bad;
    882 
    883 	/* Put dump at end of partition */
    884 	dumplo = nblks - dumpblks;
    885 
    886 	/* dumpsize is in page units, and doesn't include headers. */
    887 	dumpsize = physmem;
    888 	return;
    889 
    890 bad:
    891 	dumpsize = 0;
    892 	return;
    893 }
    894 
    895 /*
    896  * Dump the kernel's image to the swap partition.
    897  */
    898 #define	BYTES_PER_DUMP	NBPG
    899 
    900 void
    901 dumpsys()
    902 {
    903 	unsigned bytes, i, n;
    904 	int maddr, psize;
    905 	daddr_t blkno;
    906 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
    907 	int error;
    908 
    909 	/* Save registers. */
    910 	savectx(&dumppcb);
    911 
    912 	msgbufmapped = 0;	/* don't record dump msgs in msgbuf */
    913 	if (dumpdev == NODEV)
    914 		return;
    915 
    916 	/*
    917 	 * For dumps during autoconfiguration,
    918 	 * if dump device has already configured...
    919 	 */
    920 	if (dumpsize == 0)
    921 		dumpconf();
    922 	if (dumplo <= 0) {
    923 		printf("\ndump to dev %x not possible\n", dumpdev);
    924 		return;
    925 	}
    926 	printf("\ndumping to dev %x, offset %ld\n", dumpdev, dumplo);
    927 
    928 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
    929 	printf("dump ");
    930 	if (psize == -1) {
    931 		printf("area unavailable\n");
    932 		return;
    933 	}
    934 
    935 	/* XXX should purge all outstanding keystrokes. */
    936 
    937 	if ((error = cpu_dump()) != 0)
    938 		goto err;
    939 
    940 	bytes = ctob(physmem);
    941 	maddr = ctob(firstusablepage);
    942 	blkno = dumplo + cpu_dumpsize();
    943 	dump = bdevsw[major(dumpdev)].d_dump;
    944 	error = 0;
    945 	for (i = 0; i < bytes; i += n) {
    946 
    947 		/* Print out how many MBs we to go. */
    948 		n = bytes - i;
    949 		if (n && (n % (1024*1024)) == 0)
    950 			printf("%d ", n / (1024 * 1024));
    951 
    952 		/* Limit size for next transfer. */
    953 		if (n > BYTES_PER_DUMP)
    954 			n =  BYTES_PER_DUMP;
    955 
    956 		error = (*dump)(dumpdev, blkno,
    957 		    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
    958 		if (error)
    959 			break;
    960 		maddr += n;
    961 		blkno += btodb(n);			/* XXX? */
    962 
    963 		/* XXX should look for keystrokes, to cancel. */
    964 	}
    965 
    966 err:
    967 	switch (error) {
    968 
    969 	case ENXIO:
    970 		printf("device bad\n");
    971 		break;
    972 
    973 	case EFAULT:
    974 		printf("device not ready\n");
    975 		break;
    976 
    977 	case EINVAL:
    978 		printf("area improper\n");
    979 		break;
    980 
    981 	case EIO:
    982 		printf("i/o error\n");
    983 		break;
    984 
    985 	case EINTR:
    986 		printf("aborted from console\n");
    987 		break;
    988 
    989 	case 0:
    990 		printf("succeeded\n");
    991 		break;
    992 
    993 	default:
    994 		printf("error %d\n", error);
    995 		break;
    996 	}
    997 	printf("\n\n");
    998 	delay(1000);
    999 }
   1000 
   1001 void
   1002 frametoreg(framep, regp)
   1003 	struct trapframe *framep;
   1004 	struct reg *regp;
   1005 {
   1006 
   1007 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1008 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1009 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1010 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1011 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1012 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1013 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1014 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1015 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1016 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1017 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1018 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1019 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1020 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1021 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1022 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1023 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1024 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1025 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1026 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1027 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1028 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1029 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1030 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1031 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1032 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1033 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1034 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1035 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1036 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1037 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1038 	regp->r_regs[R_ZERO] = 0;
   1039 }
   1040 
   1041 void
   1042 regtoframe(regp, framep)
   1043 	struct reg *regp;
   1044 	struct trapframe *framep;
   1045 {
   1046 
   1047 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1048 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1049 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1050 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1051 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1052 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1053 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1054 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1055 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1056 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1057 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1058 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1059 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1060 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1061 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1062 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1063 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1064 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1065 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1066 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1067 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1068 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1069 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1070 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1071 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1072 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1073 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1074 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1075 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1076 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1077 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1078 	/* ??? = regp->r_regs[R_ZERO]; */
   1079 }
   1080 
   1081 void
   1082 printregs(regp)
   1083 	struct reg *regp;
   1084 {
   1085 	int i;
   1086 
   1087 	for (i = 0; i < 32; i++)
   1088 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1089 		   i & 1 ? "\n" : "\t");
   1090 }
   1091 
   1092 void
   1093 regdump(framep)
   1094 	struct trapframe *framep;
   1095 {
   1096 	struct reg reg;
   1097 
   1098 	frametoreg(framep, &reg);
   1099 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1100 
   1101 	printf("REGISTERS:\n");
   1102 	printregs(&reg);
   1103 }
   1104 
   1105 #ifdef DEBUG
   1106 int sigdebug = 0;
   1107 int sigpid = 0;
   1108 #define	SDB_FOLLOW	0x01
   1109 #define	SDB_KSTACK	0x02
   1110 #endif
   1111 
   1112 /*
   1113  * Send an interrupt to process.
   1114  */
   1115 void
   1116 sendsig(catcher, sig, mask, code)
   1117 	sig_t catcher;
   1118 	int sig, mask;
   1119 	u_long code;
   1120 {
   1121 	struct proc *p = curproc;
   1122 	struct sigcontext *scp, ksc;
   1123 	struct trapframe *frame;
   1124 	struct sigacts *psp = p->p_sigacts;
   1125 	int oonstack, fsize, rndfsize;
   1126 	extern char sigcode[], esigcode[];
   1127 	extern struct proc *fpcurproc;
   1128 
   1129 	frame = p->p_md.md_tf;
   1130 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
   1131 	fsize = sizeof ksc;
   1132 	rndfsize = ((fsize + 15) / 16) * 16;
   1133 	/*
   1134 	 * Allocate and validate space for the signal handler
   1135 	 * context. Note that if the stack is in P0 space, the
   1136 	 * call to grow() is a nop, and the useracc() check
   1137 	 * will fail if the process has not already allocated
   1138 	 * the space with a `brk'.
   1139 	 */
   1140 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
   1141 	    (psp->ps_sigonstack & sigmask(sig))) {
   1142 		scp = (struct sigcontext *)(psp->ps_sigstk.ss_sp +
   1143 		    psp->ps_sigstk.ss_size - rndfsize);
   1144 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
   1145 	} else
   1146 		scp = (struct sigcontext *)(alpha_pal_rdusp() - rndfsize);
   1147 	if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
   1148 		(void)grow(p, (u_long)scp);
   1149 #ifdef DEBUG
   1150 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1151 		printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1152 		    sig, &oonstack, scp);
   1153 #endif
   1154 	if (useracc((caddr_t)scp, fsize, B_WRITE) == 0) {
   1155 #ifdef DEBUG
   1156 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1157 			printf("sendsig(%d): useracc failed on sig %d\n",
   1158 			    p->p_pid, sig);
   1159 #endif
   1160 		/*
   1161 		 * Process has trashed its stack; give it an illegal
   1162 		 * instruction to halt it in its tracks.
   1163 		 */
   1164 		SIGACTION(p, SIGILL) = SIG_DFL;
   1165 		sig = sigmask(SIGILL);
   1166 		p->p_sigignore &= ~sig;
   1167 		p->p_sigcatch &= ~sig;
   1168 		p->p_sigmask &= ~sig;
   1169 		psignal(p, SIGILL);
   1170 		return;
   1171 	}
   1172 
   1173 	/*
   1174 	 * Build the signal context to be used by sigreturn.
   1175 	 */
   1176 	ksc.sc_onstack = oonstack;
   1177 	ksc.sc_mask = mask;
   1178 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
   1179 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
   1180 
   1181 	/* copy the registers. */
   1182 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1183 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1184 	ksc.sc_regs[R_SP] = alpha_pal_rdusp();
   1185 
   1186 	/* save the floating-point state, if necessary, then copy it. */
   1187 	if (p == fpcurproc) {
   1188 		alpha_pal_wrfen(1);
   1189 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
   1190 		alpha_pal_wrfen(0);
   1191 		fpcurproc = NULL;
   1192 	}
   1193 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
   1194 	bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1195 	    sizeof(struct fpreg));
   1196 	ksc.sc_fp_control = 0;					/* XXX ? */
   1197 	bzero(ksc.sc_reserved, sizeof ksc.sc_reserved);		/* XXX */
   1198 	bzero(ksc.sc_xxx, sizeof ksc.sc_xxx);			/* XXX */
   1199 
   1200 
   1201 #ifdef COMPAT_OSF1
   1202 	/*
   1203 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1204 	 */
   1205 #endif
   1206 
   1207 	/*
   1208 	 * copy the frame out to userland.
   1209 	 */
   1210 	(void) copyout((caddr_t)&ksc, (caddr_t)scp, fsize);
   1211 #ifdef DEBUG
   1212 	if (sigdebug & SDB_FOLLOW)
   1213 		printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
   1214 		    scp, code);
   1215 #endif
   1216 
   1217 	/*
   1218 	 * Set up the registers to return to sigcode.
   1219 	 */
   1220 	frame->tf_regs[FRAME_PC] =
   1221 	    (u_int64_t)PS_STRINGS - (esigcode - sigcode);
   1222 	frame->tf_regs[FRAME_A0] = sig;
   1223 	frame->tf_regs[FRAME_A1] = code;
   1224 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
   1225 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
   1226 	alpha_pal_wrusp((unsigned long)scp);
   1227 
   1228 #ifdef DEBUG
   1229 	if (sigdebug & SDB_FOLLOW)
   1230 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1231 		    frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
   1232 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1233 		printf("sendsig(%d): sig %d returns\n",
   1234 		    p->p_pid, sig);
   1235 #endif
   1236 }
   1237 
   1238 /*
   1239  * System call to cleanup state after a signal
   1240  * has been taken.  Reset signal mask and
   1241  * stack state from context left by sendsig (above).
   1242  * Return to previous pc and psl as specified by
   1243  * context left by sendsig. Check carefully to
   1244  * make sure that the user has not modified the
   1245  * psl to gain improper priviledges or to cause
   1246  * a machine fault.
   1247  */
   1248 /* ARGSUSED */
   1249 int
   1250 sys_sigreturn(p, v, retval)
   1251 	struct proc *p;
   1252 	void *v;
   1253 	register_t *retval;
   1254 {
   1255 	struct sys_sigreturn_args /* {
   1256 		syscallarg(struct sigcontext *) sigcntxp;
   1257 	} */ *uap = v;
   1258 	struct sigcontext *scp, ksc;
   1259 	extern struct proc *fpcurproc;
   1260 
   1261 	scp = SCARG(uap, sigcntxp);
   1262 #ifdef DEBUG
   1263 	if (sigdebug & SDB_FOLLOW)
   1264 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
   1265 #endif
   1266 
   1267 	if (ALIGN(scp) != (u_int64_t)scp)
   1268 		return (EINVAL);
   1269 
   1270 	/*
   1271 	 * Test and fetch the context structure.
   1272 	 * We grab it all at once for speed.
   1273 	 */
   1274 	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
   1275 	    copyin((caddr_t)scp, (caddr_t)&ksc, sizeof ksc))
   1276 		return (EINVAL);
   1277 
   1278 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1279 		return (EINVAL);
   1280 	/*
   1281 	 * Restore the user-supplied information
   1282 	 */
   1283 	if (ksc.sc_onstack)
   1284 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
   1285 	else
   1286 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1287 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
   1288 
   1289 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
   1290 	p->p_md.md_tf->tf_regs[FRAME_PS] =
   1291 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
   1292 
   1293 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
   1294 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
   1295 
   1296 	/* XXX ksc.sc_ownedfp ? */
   1297 	if (p == fpcurproc)
   1298 		fpcurproc = NULL;
   1299 	bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
   1300 	    sizeof(struct fpreg));
   1301 	/* XXX ksc.sc_fp_control ? */
   1302 
   1303 #ifdef DEBUG
   1304 	if (sigdebug & SDB_FOLLOW)
   1305 		printf("sigreturn(%d): returns\n", p->p_pid);
   1306 #endif
   1307 	return (EJUSTRETURN);
   1308 }
   1309 
   1310 /*
   1311  * machine dependent system variables.
   1312  */
   1313 int
   1314 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1315 	int *name;
   1316 	u_int namelen;
   1317 	void *oldp;
   1318 	size_t *oldlenp;
   1319 	void *newp;
   1320 	size_t newlen;
   1321 	struct proc *p;
   1322 {
   1323 	dev_t consdev;
   1324 
   1325 	/* all sysctl names at this level are terminal */
   1326 	if (namelen != 1)
   1327 		return (ENOTDIR);		/* overloaded */
   1328 
   1329 	switch (name[0]) {
   1330 	case CPU_CONSDEV:
   1331 		if (cn_tab != NULL)
   1332 			consdev = cn_tab->cn_dev;
   1333 		else
   1334 			consdev = NODEV;
   1335 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1336 			sizeof consdev));
   1337 
   1338 	case CPU_ROOT_DEVICE:
   1339 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1340 		    root_device->dv_xname));
   1341 
   1342 	case CPU_UNALIGNED_PRINT:
   1343 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1344 		    &alpha_unaligned_print));
   1345 
   1346 	case CPU_UNALIGNED_FIX:
   1347 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1348 		    &alpha_unaligned_fix));
   1349 
   1350 	case CPU_UNALIGNED_SIGBUS:
   1351 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1352 		    &alpha_unaligned_sigbus));
   1353 
   1354 	case CPU_BOOTED_KERNEL:
   1355 		return (sysctl_rdstring(oldp, oldlenp, newp, booted_kernel));
   1356 
   1357 	default:
   1358 		return (EOPNOTSUPP);
   1359 	}
   1360 	/* NOTREACHED */
   1361 }
   1362 
   1363 /*
   1364  * Set registers on exec.
   1365  */
   1366 void
   1367 setregs(p, pack, stack, retval)
   1368 	register struct proc *p;
   1369 	struct exec_package *pack;
   1370 	u_long stack;
   1371 	register_t *retval;
   1372 {
   1373 	struct trapframe *tfp = p->p_md.md_tf;
   1374 	extern struct proc *fpcurproc;
   1375 #ifdef DEBUG
   1376 	int i;
   1377 #endif
   1378 
   1379 #ifdef DEBUG
   1380 	/*
   1381 	 * Crash and dump, if the user requested it.
   1382 	 */
   1383 	if (boothowto & RB_DUMP)
   1384 		panic("crash requested by boot flags");
   1385 #endif
   1386 
   1387 #ifdef DEBUG
   1388 	for (i = 0; i < FRAME_SIZE; i++)
   1389 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1390 #else
   1391 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   1392 #endif
   1393 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
   1394 #define FP_RN 2 /* XXX */
   1395 	p->p_addr->u_pcb.pcb_fp.fpr_cr = (long)FP_RN << 58;
   1396 	alpha_pal_wrusp(stack);
   1397 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   1398 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   1399 
   1400 	tfp->tf_regs[FRAME_A0] = stack;			/* a0 = sp */
   1401 	tfp->tf_regs[FRAME_A1] = 0;			/* a1 = rtld cleanup */
   1402 	tfp->tf_regs[FRAME_A2] = 0;			/* a2 = rtld object */
   1403 	tfp->tf_regs[FRAME_A3] = (u_int64_t)PS_STRINGS;	/* a3 = ps_strings */
   1404 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   1405 
   1406 	p->p_md.md_flags &= ~MDP_FPUSED;
   1407 	if (fpcurproc == p)
   1408 		fpcurproc = NULL;
   1409 
   1410 	retval[0] = retval[1] = 0;
   1411 }
   1412 
   1413 void
   1414 netintr()
   1415 {
   1416 	int n, s;
   1417 
   1418 	s = splhigh();
   1419 	n = netisr;
   1420 	netisr = 0;
   1421 	splx(s);
   1422 
   1423 #define	DONETISR(bit, fn)						\
   1424 	do {								\
   1425 		if (n & (1 << (bit)))					\
   1426 			fn;						\
   1427 	} while (0)
   1428 
   1429 #ifdef INET
   1430 	DONETISR(NETISR_ARP, arpintr());
   1431 	DONETISR(NETISR_IP, ipintr());
   1432 #endif
   1433 #ifdef NS
   1434 	DONETISR(NETISR_NS, nsintr());
   1435 #endif
   1436 #ifdef ISO
   1437 	DONETISR(NETISR_ISO, clnlintr());
   1438 #endif
   1439 #ifdef CCITT
   1440 	DONETISR(NETISR_CCITT, ccittintr());
   1441 #endif
   1442 #ifdef NATM
   1443 	DONETISR(NETISR_NATM, natmintr());
   1444 #endif
   1445 #if NPPP > 1
   1446 	DONETISR(NETISR_PPP, pppintr());
   1447 #endif
   1448 
   1449 #undef DONETISR
   1450 }
   1451 
   1452 void
   1453 do_sir()
   1454 {
   1455 	u_int64_t n;
   1456 
   1457 	do {
   1458 		(void)splhigh();
   1459 		n = ssir;
   1460 		ssir = 0;
   1461 		splsoft();		/* don't recurse through spl0() */
   1462 
   1463 #define	DO_SIR(bit, fn)							\
   1464 		do {							\
   1465 			if (n & (bit)) {				\
   1466 				cnt.v_soft++;				\
   1467 				fn;					\
   1468 			}						\
   1469 		} while (0)
   1470 
   1471 		DO_SIR(SIR_NET, netintr());
   1472 		DO_SIR(SIR_CLOCK, softclock());
   1473 
   1474 #undef DO_SIR
   1475 	} while (ssir != 0);
   1476 }
   1477 
   1478 int
   1479 spl0()
   1480 {
   1481 
   1482 	if (ssir)
   1483 		do_sir();		/* it lowers the IPL itself */
   1484 
   1485 	return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
   1486 }
   1487 
   1488 /*
   1489  * The following primitives manipulate the run queues.  _whichqs tells which
   1490  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
   1491  * into queues, Remrunqueue removes them from queues.  The running process is
   1492  * on no queue, other processes are on a queue related to p->p_priority,
   1493  * divided by 4 actually to shrink the 0-127 range of priorities into the 32
   1494  * available queues.
   1495  */
   1496 /*
   1497  * setrunqueue(p)
   1498  *	proc *p;
   1499  *
   1500  * Call should be made at splclock(), and p->p_stat should be SRUN.
   1501  */
   1502 
   1503 void
   1504 setrunqueue(p)
   1505 	struct proc *p;
   1506 {
   1507 	int bit;
   1508 
   1509 	/* firewall: p->p_back must be NULL */
   1510 	if (p->p_back != NULL)
   1511 		panic("setrunqueue");
   1512 
   1513 	bit = p->p_priority >> 2;
   1514 	whichqs |= (1 << bit);
   1515 	p->p_forw = (struct proc *)&qs[bit];
   1516 	p->p_back = qs[bit].ph_rlink;
   1517 	p->p_back->p_forw = p;
   1518 	qs[bit].ph_rlink = p;
   1519 }
   1520 
   1521 /*
   1522  * remrunqueue(p)
   1523  *
   1524  * Call should be made at splclock().
   1525  */
   1526 void
   1527 remrunqueue(p)
   1528 	struct proc *p;
   1529 {
   1530 	int bit;
   1531 
   1532 	bit = p->p_priority >> 2;
   1533 	if ((whichqs & (1 << bit)) == 0)
   1534 		panic("remrunqueue");
   1535 
   1536 	p->p_back->p_forw = p->p_forw;
   1537 	p->p_forw->p_back = p->p_back;
   1538 	p->p_back = NULL;	/* for firewall checking. */
   1539 
   1540 	if ((struct proc *)&qs[bit] == qs[bit].ph_link)
   1541 		whichqs &= ~(1 << bit);
   1542 }
   1543 
   1544 /*
   1545  * Return the best possible estimate of the time in the timeval
   1546  * to which tvp points.  Unfortunately, we can't read the hardware registers.
   1547  * We guarantee that the time will be greater than the value obtained by a
   1548  * previous call.
   1549  */
   1550 void
   1551 microtime(tvp)
   1552 	register struct timeval *tvp;
   1553 {
   1554 	int s = splclock();
   1555 	static struct timeval lasttime;
   1556 
   1557 	*tvp = time;
   1558 #ifdef notdef
   1559 	tvp->tv_usec += clkread();
   1560 	while (tvp->tv_usec > 1000000) {
   1561 		tvp->tv_sec++;
   1562 		tvp->tv_usec -= 1000000;
   1563 	}
   1564 #endif
   1565 	if (tvp->tv_sec == lasttime.tv_sec &&
   1566 	    tvp->tv_usec <= lasttime.tv_usec &&
   1567 	    (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
   1568 		tvp->tv_sec++;
   1569 		tvp->tv_usec -= 1000000;
   1570 	}
   1571 	lasttime = *tvp;
   1572 	splx(s);
   1573 }
   1574 
   1575 /*
   1576  * Wait "n" microseconds.
   1577  */
   1578 void
   1579 delay(n)
   1580 	unsigned long n;
   1581 {
   1582 	long N = cycles_per_usec * (n);
   1583 
   1584 	while (N > 0)				/* XXX */
   1585 		N -= 3;				/* XXX */
   1586 }
   1587 
   1588 #if defined(COMPAT_OSF1) || 1		/* XXX */
   1589 void	cpu_exec_ecoff_setregs __P((struct proc *, struct exec_package *,
   1590 	    u_long, register_t *));
   1591 
   1592 void
   1593 cpu_exec_ecoff_setregs(p, epp, stack, retval)
   1594 	struct proc *p;
   1595 	struct exec_package *epp;
   1596 	u_long stack;
   1597 	register_t *retval;
   1598 {
   1599 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1600 
   1601 	setregs(p, epp, stack, retval);
   1602 	p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   1603 }
   1604 
   1605 /*
   1606  * cpu_exec_ecoff_hook():
   1607  *	cpu-dependent ECOFF format hook for execve().
   1608  *
   1609  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   1610  *
   1611  */
   1612 int
   1613 cpu_exec_ecoff_hook(p, epp)
   1614 	struct proc *p;
   1615 	struct exec_package *epp;
   1616 {
   1617 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1618 	extern struct emul emul_netbsd;
   1619 #ifdef COMPAT_OSF1
   1620 	extern struct emul emul_osf1;
   1621 #endif
   1622 
   1623 	switch (execp->f.f_magic) {
   1624 #ifdef COMPAT_OSF1
   1625 	case ECOFF_MAGIC_ALPHA:
   1626 		epp->ep_emul = &emul_osf1;
   1627 		break;
   1628 #endif
   1629 
   1630 	case ECOFF_MAGIC_NETBSD_ALPHA:
   1631 		epp->ep_emul = &emul_netbsd;
   1632 		break;
   1633 
   1634 	default:
   1635 		return ENOEXEC;
   1636 	}
   1637 	return 0;
   1638 }
   1639 #endif
   1640 
   1641 /* XXX XXX BEGIN XXX XXX */
   1642 vm_offset_t alpha_XXX_dmamap_or;				/* XXX */
   1643 								/* XXX */
   1644 vm_offset_t							/* XXX */
   1645 alpha_XXX_dmamap(v)						/* XXX */
   1646 	vm_offset_t v;						/* XXX */
   1647 {								/* XXX */
   1648 								/* XXX */
   1649 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
   1650 }								/* XXX */
   1651 /* XXX XXX END XXX XXX */
   1652