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