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