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