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