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