Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.154
      1 /* $NetBSD: machdep.c,v 1.154 1998/11/02 04:43:23 ross Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center and by Chris G. Demetriou.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
     42  * All rights reserved.
     43  *
     44  * Author: Chris G. Demetriou
     45  *
     46  * Permission to use, copy, modify and distribute this software and
     47  * its documentation is hereby granted, provided that both the copyright
     48  * notice and this permission notice appear in all copies of the
     49  * software, derivative works or modified versions, and any portions
     50  * thereof, and that both notices appear in supporting documentation.
     51  *
     52  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     53  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     54  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     55  *
     56  * Carnegie Mellon requests users of this software to return to
     57  *
     58  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     59  *  School of Computer Science
     60  *  Carnegie Mellon University
     61  *  Pittsburgh PA 15213-3890
     62  *
     63  * any improvements or extensions that they make and grant Carnegie the
     64  * rights to redistribute these changes.
     65  */
     66 
     67 #include "opt_ddb.h"
     68 #include "opt_multiprocessor.h"
     69 #include "opt_uvm.h"
     70 #include "opt_pmap_new.h"
     71 #include "opt_dec_3000_300.h"
     72 #include "opt_dec_3000_500.h"
     73 #include "opt_compat_osf1.h"
     74 #include "opt_compat_netbsd.h"
     75 #include "opt_inet.h"
     76 #include "opt_atalk.h"
     77 #include "opt_ccitt.h"
     78 #include "opt_iso.h"
     79 #include "opt_ns.h"
     80 #include "opt_natm.h"
     81 #include "opt_sysv.h"
     82 
     83 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     84 
     85 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.154 1998/11/02 04:43:23 ross Exp $");
     86 
     87 #include <sys/param.h>
     88 #include <sys/systm.h>
     89 #include <sys/signalvar.h>
     90 #include <sys/kernel.h>
     91 #include <sys/map.h>
     92 #include <sys/proc.h>
     93 #include <sys/buf.h>
     94 #include <sys/reboot.h>
     95 #include <sys/device.h>
     96 #include <sys/file.h>
     97 #ifdef REAL_CLISTS
     98 #include <sys/clist.h>
     99 #endif
    100 #include <sys/callout.h>
    101 #include <sys/malloc.h>
    102 #include <sys/mbuf.h>
    103 #include <sys/mman.h>
    104 #include <sys/msgbuf.h>
    105 #include <sys/ioctl.h>
    106 #include <sys/tty.h>
    107 #include <sys/user.h>
    108 #include <sys/exec.h>
    109 #include <sys/exec_ecoff.h>
    110 #include <vm/vm.h>
    111 #include <sys/sysctl.h>
    112 #include <sys/core.h>
    113 #include <sys/kcore.h>
    114 #include <machine/kcore.h>
    115 #ifdef SYSVMSG
    116 #include <sys/msg.h>
    117 #endif
    118 #ifdef SYSVSEM
    119 #include <sys/sem.h>
    120 #endif
    121 #ifdef SYSVSHM
    122 #include <sys/shm.h>
    123 #endif
    124 
    125 #include <sys/mount.h>
    126 #include <sys/syscallargs.h>
    127 
    128 #include <vm/vm_kern.h>
    129 
    130 #if defined(UVM)
    131 #include <uvm/uvm_extern.h>
    132 #endif
    133 
    134 #include <dev/cons.h>
    135 
    136 #include <machine/autoconf.h>
    137 #include <machine/cpu.h>
    138 #include <machine/reg.h>
    139 #include <machine/rpb.h>
    140 #include <machine/prom.h>
    141 #include <machine/conf.h>
    142 
    143 #include <alpha/alpha/cpuvar.h>
    144 
    145 #include <net/netisr.h>
    146 #include <net/if.h>
    147 
    148 #ifdef INET
    149 #include <net/route.h>
    150 #include <netinet/in.h>
    151 #include <netinet/ip_var.h>
    152 #include "arp.h"
    153 #if NARP > 0
    154 #include <netinet/if_inarp.h>
    155 #endif
    156 #endif
    157 #ifdef NS
    158 #include <netns/ns_var.h>
    159 #endif
    160 #ifdef ISO
    161 #include <netiso/iso.h>
    162 #include <netiso/clnp.h>
    163 #endif
    164 #ifdef CCITT
    165 #include <netccitt/x25.h>
    166 #include <netccitt/pk.h>
    167 #include <netccitt/pk_extern.h>
    168 #endif
    169 #ifdef NATM
    170 #include <netnatm/natm.h>
    171 #endif
    172 #ifdef NETATALK
    173 #include <netatalk/at_extern.h>
    174 #endif
    175 #include "ppp.h"
    176 #if NPPP > 0
    177 #include <net/ppp_defs.h>
    178 #include <net/if_ppp.h>
    179 #endif
    180 
    181 #ifdef DDB
    182 #include <machine/db_machdep.h>
    183 #include <ddb/db_access.h>
    184 #include <ddb/db_sym.h>
    185 #include <ddb/db_extern.h>
    186 #include <ddb/db_interface.h>
    187 #endif
    188 
    189 #include "com.h"
    190 #if NCOM > 0
    191 extern void comsoft __P((void));
    192 #endif
    193 
    194 #if defined(UVM)
    195 vm_map_t exec_map = NULL;
    196 vm_map_t mb_map = NULL;
    197 vm_map_t phys_map = NULL;
    198 #else
    199 vm_map_t buffer_map;
    200 #endif
    201 
    202 /*
    203  * Declare these as initialized data so we can patch them.
    204  */
    205 int	nswbuf = 0;
    206 #ifdef	NBUF
    207 int	nbuf = NBUF;
    208 #else
    209 int	nbuf = 0;
    210 #endif
    211 
    212 #ifndef	BUFPAGES
    213 #define BUFPAGES 0
    214 #endif
    215 #ifndef BUFCACHE
    216 #define BUFCACHE 10
    217 #endif
    218 
    219 int	bufpages = BUFPAGES;	/* optional hardwired count */
    220 int	bufcache = BUFCACHE;	/* % of RAM to use for buffer cache */
    221 
    222 caddr_t msgbufaddr;
    223 
    224 int	maxmem;			/* max memory per process */
    225 
    226 int	totalphysmem;		/* total amount of physical memory in system */
    227 int	physmem;		/* physical memory used by NetBSD + some rsvd */
    228 int	resvmem;		/* amount of memory reserved for PROM */
    229 int	unusedmem;		/* amount of memory for OS that we don't use */
    230 int	unknownmem;		/* amount of memory with an unknown use */
    231 
    232 int	cputype;		/* system type, from the RPB */
    233 
    234 /*
    235  * XXX We need an address to which we can assign things so that they
    236  * won't be optimized away because we didn't use the value.
    237  */
    238 u_int32_t no_optimize;
    239 
    240 /* the following is used externally (sysctl_hw) */
    241 char	machine[] = MACHINE;		/* from <machine/param.h> */
    242 char	machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    243 char	cpu_model[128];
    244 
    245 struct	user *proc0paddr;
    246 
    247 /* Number of machine cycles per microsecond */
    248 u_int64_t	cycles_per_usec;
    249 
    250 /* number of cpus in the box.  really! */
    251 int		ncpus;
    252 
    253 /* machine check info array, valloc()'ed in allocsys() */
    254 
    255 static struct mchkinfo startup_info,
    256 			*mchkinfo_all_cpus;
    257 
    258 struct bootinfo_kernel bootinfo;
    259 
    260 /* For built-in TCDS */
    261 #if defined(DEC_3000_300) || defined(DEC_3000_500)
    262 u_int8_t	dec_3000_scsiid[2], dec_3000_scsifast[2];
    263 #endif
    264 
    265 struct platform platform;
    266 
    267 u_int32_t vm_mbuf_size = _VM_MBUF_SIZE;
    268 u_int32_t vm_kmem_size = _VM_KMEM_SIZE;
    269 u_int32_t vm_phys_size = _VM_PHYS_SIZE;
    270 
    271 #ifdef DDB
    272 /* start and end of kernel symbol table */
    273 void	*ksym_start, *ksym_end;
    274 #endif
    275 
    276 /* for cpu_sysctl() */
    277 int	alpha_unaligned_print = 1;	/* warn about unaligned accesses */
    278 int	alpha_unaligned_fix = 1;	/* fix up unaligned accesses */
    279 int	alpha_unaligned_sigbus = 0;	/* don't SIGBUS on fixed-up accesses */
    280 
    281 /*
    282  * XXX This should be dynamically sized, but we have the chicken-egg problem!
    283  * XXX it should also be larger than it is, because not all of the mddt
    284  * XXX clusters end up being used for VM.
    285  */
    286 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];	/* low size bits overloaded */
    287 int	mem_cluster_cnt;
    288 
    289 caddr_t	allocsys __P((caddr_t));
    290 int	cpu_dump __P((void));
    291 int	cpu_dumpsize __P((void));
    292 u_long	cpu_dump_mempagecnt __P((void));
    293 void	dumpsys __P((void));
    294 void	identifycpu __P((void));
    295 void	netintr __P((void));
    296 void	printregs __P((struct reg *));
    297 
    298 void
    299 alpha_init(pfn, ptb, bim, bip, biv)
    300 	u_long pfn;		/* first free PFN number */
    301 	u_long ptb;		/* PFN of current level 1 page table */
    302 	u_long bim;		/* bootinfo magic */
    303 	u_long bip;		/* bootinfo pointer */
    304 	u_long biv;		/* bootinfo version */
    305 {
    306 	extern char kernel_text[], _end[];
    307 	struct mddt *mddtp;
    308 	struct mddt_cluster *memc;
    309 	int i, mddtweird;
    310 	struct vm_physseg *vps;
    311 	vaddr_t kernstart, kernend;
    312 	paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
    313 	vsize_t size;
    314 	char *p;
    315 	caddr_t v;
    316 	char *bootinfo_msg;
    317 
    318 	/* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
    319 
    320 	/*
    321 	 * Turn off interrupts (not mchecks) and floating point.
    322 	 * Make sure the instruction and data streams are consistent.
    323 	 */
    324 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
    325 	alpha_pal_wrfen(0);
    326 	ALPHA_TBIA();
    327 	alpha_pal_imb();
    328 
    329 	/*
    330 	 * Get critical system information (if possible, from the
    331 	 * information provided by the boot program).
    332 	 */
    333 	bootinfo_msg = NULL;
    334 	if (bim == BOOTINFO_MAGIC) {
    335 		if (biv == 0) {		/* backward compat */
    336 			biv = *(u_long *)bip;
    337 			bip += 8;
    338 		}
    339 		switch (biv) {
    340 		case 1: {
    341 			struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
    342 
    343 			bootinfo.ssym = v1p->ssym;
    344 			bootinfo.esym = v1p->esym;
    345 			/* hwrpb may not be provided by boot block in v1 */
    346 			if (v1p->hwrpb != NULL) {
    347 				bootinfo.hwrpb_phys =
    348 				    ((struct rpb *)v1p->hwrpb)->rpb_phys;
    349 				bootinfo.hwrpb_size = v1p->hwrpbsize;
    350 			} else {
    351 				bootinfo.hwrpb_phys =
    352 				    ((struct rpb *)HWRPB_ADDR)->rpb_phys;
    353 				bootinfo.hwrpb_size =
    354 				    ((struct rpb *)HWRPB_ADDR)->rpb_size;
    355 			}
    356 			bcopy(v1p->boot_flags, bootinfo.boot_flags,
    357 			    min(sizeof v1p->boot_flags,
    358 			      sizeof bootinfo.boot_flags));
    359 			bcopy(v1p->booted_kernel, bootinfo.booted_kernel,
    360 			    min(sizeof v1p->booted_kernel,
    361 			      sizeof bootinfo.booted_kernel));
    362 			/* booted dev not provided in bootinfo */
    363 			init_prom_interface((struct rpb *)
    364 			    ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
    365                 	prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
    366 			    sizeof bootinfo.booted_dev);
    367 			break;
    368 		}
    369 		default:
    370 			bootinfo_msg = "unknown bootinfo version";
    371 			goto nobootinfo;
    372 		}
    373 	} else {
    374 		bootinfo_msg = "boot program did not pass bootinfo";
    375 nobootinfo:
    376 		bootinfo.ssym = (u_long)_end;
    377 		bootinfo.esym = (u_long)_end;
    378 		bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
    379 		bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
    380 		init_prom_interface((struct rpb *)HWRPB_ADDR);
    381 		prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
    382 		    sizeof bootinfo.boot_flags);
    383 		prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
    384 		    sizeof bootinfo.booted_kernel);
    385 		prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
    386 		    sizeof bootinfo.booted_dev);
    387 	}
    388 
    389 	/*
    390 	 * Initialize the kernel's mapping of the RPB.  It's needed for
    391 	 * lots of things.
    392 	 */
    393 	hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
    394 
    395 #if defined(DEC_3000_300) || defined(DEC_3000_500)
    396 	if (hwrpb->rpb_type == ST_DEC_3000_300 ||
    397 	    hwrpb->rpb_type == ST_DEC_3000_500) {
    398 		prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
    399 		    sizeof(dec_3000_scsiid));
    400 		prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
    401 		    sizeof(dec_3000_scsifast));
    402 	}
    403 #endif
    404 
    405 	/*
    406 	 * Remember how many cycles there are per microsecond,
    407 	 * so that we can use delay().  Round up, for safety.
    408 	 */
    409 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
    410 
    411 	/*
    412 	 * Initalize the (temporary) bootstrap console interface, so
    413 	 * we can use printf until the VM system starts being setup.
    414 	 * The real console is initialized before then.
    415 	 */
    416 	init_bootstrap_console();
    417 
    418 	/* OUTPUT NOW ALLOWED */
    419 
    420 	/* delayed from above */
    421 	if (bootinfo_msg)
    422 		printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
    423 		    bootinfo_msg, bim, bip, biv);
    424 
    425 	/* Initialize the trap vectors on the primary processor. */
    426 	trap_init();
    427 
    428 	/*
    429 	 * Find out what hardware we're on, and do basic initialization.
    430 	 */
    431 	cputype = hwrpb->rpb_type;
    432 	if (cputype >= ncpuinit) {
    433 		platform_not_supported();
    434 		/* NOTREACHED */
    435 	}
    436 	(*cpuinit[cputype].init)();
    437 	strcpy(cpu_model, platform.model);
    438 
    439 	/*
    440 	 * Initalize the real console, so the the bootstrap console is
    441 	 * no longer necessary.
    442 	 */
    443 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    444 	if (!pmap_uses_prom_console())
    445 #endif
    446 		(*platform.cons_init)();
    447 
    448 #ifdef DIAGNOSTIC
    449 	/* Paranoid sanity checking */
    450 
    451 	/* We should always be running on the the primary. */
    452 	assert(hwrpb->rpb_primary_cpu_id == alpha_pal_whami());
    453 
    454 	/*
    455 	 * On single-CPU systypes, the primary should always be CPU 0,
    456 	 * except on Alpha 8200 systems where the CPU id is related
    457 	 * to the VID, which is related to the Turbo Laser node id.
    458 	 */
    459 	if (cputype != ST_DEC_21000)
    460 		assert(hwrpb->rpb_primary_cpu_id == 0);
    461 #endif
    462 
    463 	/* NO MORE FIRMWARE ACCESS ALLOWED */
    464 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    465 	/*
    466 	 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
    467 	 * XXX pmap_uses_prom_console() evaluates to non-zero.)
    468 	 */
    469 #endif
    470 
    471 	/*
    472 	 * find out this system's page size
    473 	 */
    474 	PAGE_SIZE = hwrpb->rpb_page_size;
    475 	if (PAGE_SIZE != 8192)
    476 		panic("page size %d != 8192?!", PAGE_SIZE);
    477 
    478 	/*
    479 	 * Initialize PAGE_SIZE-dependent variables.
    480 	 */
    481 #if defined(UVM)
    482 	uvm_setpagesize();
    483 #else
    484 	vm_set_page_size();
    485 #endif
    486 
    487 	/*
    488 	 * Find the beginning and end of the kernel (and leave a
    489 	 * bit of space before the beginning for the bootstrap
    490 	 * stack).
    491 	 */
    492 	kernstart = trunc_page(kernel_text) - 2 * PAGE_SIZE;
    493 #ifdef DDB
    494 	ksym_start = (void *)bootinfo.ssym;
    495 	ksym_end   = (void *)bootinfo.esym;
    496 	kernend = (vaddr_t)round_page(ksym_end);
    497 #else
    498 	kernend = (vaddr_t)round_page(_end);
    499 #endif
    500 
    501 	kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
    502 	kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
    503 
    504 	/*
    505 	 * Find out how much memory is available, by looking at
    506 	 * the memory cluster descriptors.  This also tries to do
    507 	 * its best to detect things things that have never been seen
    508 	 * before...
    509 	 */
    510 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
    511 
    512 	/* MDDT SANITY CHECKING */
    513 	mddtweird = 0;
    514 	if (mddtp->mddt_cluster_cnt < 2) {
    515 		mddtweird = 1;
    516 		printf("WARNING: weird number of mem clusters: %d\n",
    517 		    mddtp->mddt_cluster_cnt);
    518 	}
    519 
    520 #if 0
    521 	printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
    522 #endif
    523 
    524 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    525 		memc = &mddtp->mddt_clusters[i];
    526 #if 0
    527 		printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
    528 		    memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
    529 #endif
    530 		totalphysmem += memc->mddt_pg_cnt;
    531 		if (mem_cluster_cnt < VM_PHYSSEG_MAX) {	/* XXX */
    532 			mem_clusters[mem_cluster_cnt].start =
    533 			    ptoa(memc->mddt_pfn);
    534 			mem_clusters[mem_cluster_cnt].size =
    535 			    ptoa(memc->mddt_pg_cnt);
    536 			if (memc->mddt_usage & MDDT_mbz ||
    537 			    memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
    538 			    memc->mddt_usage & MDDT_PALCODE)
    539 				mem_clusters[mem_cluster_cnt].size |=
    540 				    PROT_READ;
    541 			else
    542 				mem_clusters[mem_cluster_cnt].size |=
    543 				    PROT_READ | PROT_WRITE | PROT_EXEC;
    544 			mem_cluster_cnt++;
    545 		}
    546 
    547 		if (memc->mddt_usage & MDDT_mbz) {
    548 			mddtweird = 1;
    549 			printf("WARNING: mem cluster %d has weird "
    550 			    "usage 0x%lx\n", i, memc->mddt_usage);
    551 			unknownmem += memc->mddt_pg_cnt;
    552 			continue;
    553 		}
    554 		if (memc->mddt_usage & MDDT_NONVOLATILE) {
    555 			/* XXX should handle these... */
    556 			printf("WARNING: skipping non-volatile mem "
    557 			    "cluster %d\n", i);
    558 			unusedmem += memc->mddt_pg_cnt;
    559 			continue;
    560 		}
    561 		if (memc->mddt_usage & MDDT_PALCODE) {
    562 			resvmem += memc->mddt_pg_cnt;
    563 			continue;
    564 		}
    565 
    566 		/*
    567 		 * We have a memory cluster available for system
    568 		 * software use.  We must determine if this cluster
    569 		 * holds the kernel.
    570 		 */
    571 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    572 		/*
    573 		 * XXX If the kernel uses the PROM console, we only use the
    574 		 * XXX memory after the kernel in the first system segment,
    575 		 * XXX to avoid clobbering prom mapping, data, etc.
    576 		 */
    577 	    if (!pmap_uses_prom_console() || physmem == 0) {
    578 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    579 		physmem += memc->mddt_pg_cnt;
    580 		pfn0 = memc->mddt_pfn;
    581 		pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
    582 		if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
    583 			/*
    584 			 * Must compute the location of the kernel
    585 			 * within the segment.
    586 			 */
    587 #if 0
    588 			printf("Cluster %d contains kernel\n", i);
    589 #endif
    590 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    591 		    if (!pmap_uses_prom_console()) {
    592 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    593 			if (pfn0 < kernstartpfn) {
    594 				/*
    595 				 * There is a chunk before the kernel.
    596 				 */
    597 #if 0
    598 				printf("Loading chunk before kernel: "
    599 				    "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
    600 #endif
    601 #if defined(UVM)
    602 				uvm_page_physload(pfn0, kernstartpfn,
    603 				    pfn0, kernstartpfn, VM_FREELIST_DEFAULT);
    604 #else
    605 				vm_page_physload(pfn0, kernstartpfn,
    606 				    pfn0, kernstartpfn);
    607 #endif
    608 			}
    609 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    610 		    }
    611 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    612 			if (kernendpfn < pfn1) {
    613 				/*
    614 				 * There is a chunk after the kernel.
    615 				 */
    616 #if 0
    617 				printf("Loading chunk after kernel: "
    618 				    "0x%lx / 0x%lx\n", kernendpfn, pfn1);
    619 #endif
    620 #if defined(UVM)
    621 				uvm_page_physload(kernendpfn, pfn1,
    622 				    kernendpfn, pfn1, VM_FREELIST_DEFAULT);
    623 #else
    624 				vm_page_physload(kernendpfn, pfn1,
    625 				    kernendpfn, pfn1);
    626 #endif
    627 			}
    628 		} else {
    629 			/*
    630 			 * Just load this cluster as one chunk.
    631 			 */
    632 #if 0
    633 			printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
    634 			    pfn0, pfn1);
    635 #endif
    636 #if defined(UVM)
    637 			uvm_page_physload(pfn0, pfn1, pfn0, pfn1,
    638 			    VM_FREELIST_DEFAULT);
    639 #else
    640 			vm_page_physload(pfn0, pfn1, pfn0, pfn1);
    641 #endif
    642 		}
    643 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    644 	    }
    645 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    646 	}
    647 
    648 	/*
    649 	 * Dump out the MDDT if it looks odd...
    650 	 */
    651 	if (mddtweird) {
    652 		printf("\n");
    653 		printf("complete memory cluster information:\n");
    654 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    655 			printf("mddt %d:\n", i);
    656 			printf("\tpfn %lx\n",
    657 			    mddtp->mddt_clusters[i].mddt_pfn);
    658 			printf("\tcnt %lx\n",
    659 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
    660 			printf("\ttest %lx\n",
    661 			    mddtp->mddt_clusters[i].mddt_pg_test);
    662 			printf("\tbva %lx\n",
    663 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
    664 			printf("\tbpa %lx\n",
    665 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
    666 			printf("\tbcksum %lx\n",
    667 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
    668 			printf("\tusage %lx\n",
    669 			    mddtp->mddt_clusters[i].mddt_usage);
    670 		}
    671 		printf("\n");
    672 	}
    673 
    674 	if (totalphysmem == 0)
    675 		panic("can't happen: system seems to have no memory!");
    676 
    677 #ifdef LIMITMEM
    678 	/*
    679 	 * XXX Kludge so we can run on machines with memory larger
    680 	 * XXX than 1G until all device drivers are converted to
    681 	 * XXX use bus_dma.  (Relies on the fact that vm_physmem
    682 	 * XXX sorted in order of increasing addresses.)
    683 	 */
    684 	if (vm_physmem[vm_nphysseg - 1].end > atop(LIMITMEM * 1024 * 1024)) {
    685 
    686 		printf("******** LIMITING MEMORY TO %dMB **********\n",
    687 		    LIMITMEM);
    688 
    689 		do {
    690 			u_long ovf;
    691 
    692 			vps = &vm_physmem[vm_nphysseg - 1];
    693 
    694 			if (vps->start >= atop(LIMITMEM * 1024 * 1024)) {
    695 				/*
    696 				 * If the start is too high, just drop
    697 				 * the whole segment.
    698 				 *
    699 				 * XXX can start != avail_start in this
    700 				 * XXX case?  wouldn't that mean that
    701 				 * XXX some memory was stolen above the
    702 				 * XXX limit?  What to do?
    703 				 */
    704 				ovf = vps->end - vps->start;
    705 				vm_nphysseg--;
    706 			} else {
    707 				/*
    708 				 * If the start is OK, calculate how much
    709 				 * to drop and drop it.
    710 				 */
    711 				ovf = vps->end - atop(LIMITMEM * 1024 * 1024);
    712 				vps->end -= ovf;
    713 				vps->avail_end -= ovf;
    714 			}
    715 			physmem -= ovf;
    716 			unusedmem += ovf;
    717 		} while (vps->end > atop(LIMITMEM * 1024 * 1024));
    718 	}
    719 #endif /* LIMITMEM */
    720 
    721 	maxmem = physmem;
    722 
    723 #if 0
    724 	printf("totalphysmem = %d\n", totalphysmem);
    725 	printf("physmem = %d\n", physmem);
    726 	printf("resvmem = %d\n", resvmem);
    727 	printf("unusedmem = %d\n", unusedmem);
    728 	printf("unknownmem = %d\n", unknownmem);
    729 #endif
    730 
    731 	/*
    732 	 * Adjust some parameters if the amount of physmem
    733 	 * available would cause us to croak. This is completely
    734 	 * eyeballed and isn't meant to be the final answer.
    735 	 * vm_phys_size is probably the only one to really worry
    736 	 * about.
    737  	 *
    738 	 * It's for booting a GENERIC kernel on a large memory platform.
    739 	 */
    740 	if (physmem >= atop(128 * 1024 * 1024)) {
    741 		vm_mbuf_size <<= 1;
    742 		vm_kmem_size <<= 3;
    743 		vm_phys_size <<= 2;
    744 	}
    745 
    746 	/*
    747 	 * Initialize error message buffer (at end of core).
    748 	 */
    749 	{
    750 		size_t sz = round_page(MSGBUFSIZE);
    751 
    752 		vps = &vm_physmem[vm_nphysseg - 1];
    753 
    754 		/* shrink so that it'll fit in the last segment */
    755 		if ((vps->avail_end - vps->avail_start) < atop(sz))
    756 			sz = ptoa(vps->avail_end - vps->avail_start);
    757 
    758 		vps->end -= atop(sz);
    759 		vps->avail_end -= atop(sz);
    760 		msgbufaddr = (caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end));
    761 		initmsgbuf(msgbufaddr, sz);
    762 
    763 		/* Remove the last segment if it now has no pages. */
    764 		if (vps->start == vps->end)
    765 			vm_nphysseg--;
    766 
    767 		/* warn if the message buffer had to be shrunk */
    768 		if (sz != round_page(MSGBUFSIZE))
    769 			printf("WARNING: %d bytes not available for msgbuf in last cluster (%d used)\n",
    770 			    round_page(MSGBUFSIZE), sz);
    771 
    772 	}
    773 
    774 	/*
    775 	 * Init mapping for u page(s) for proc 0
    776 	 */
    777 	proc0.p_addr = proc0paddr =
    778 	    (struct user *)pmap_steal_memory(UPAGES * PAGE_SIZE, NULL, NULL);
    779 
    780 	/*
    781 	 * Allocate space for system data structures.  These data structures
    782 	 * are allocated here instead of cpu_startup() because physical
    783 	 * memory is directly addressable.  We don't have to map these into
    784 	 * virtual address space.
    785 	 */
    786 	size = (vsize_t)allocsys(0);
    787 	v = (caddr_t)pmap_steal_memory(size, NULL, NULL);
    788 	if ((allocsys(v) - v) != size)
    789 		panic("alpha_init: table size inconsistency");
    790 
    791 	/*
    792 	 * Initialize the virtual memory system, and set the
    793 	 * page table base register in proc 0's PCB.
    794 	 */
    795 	pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
    796 	    hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
    797 
    798 	/*
    799 	 * Initialize the rest of proc 0's PCB, and cache its physical
    800 	 * address.
    801 	 */
    802 	proc0.p_md.md_pcbpaddr =
    803 	    (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
    804 
    805 	/*
    806 	 * Set the kernel sp, reserving space for an (empty) trapframe,
    807 	 * and make proc0's trapframe pointer point to it for sanity.
    808 	 */
    809 	proc0paddr->u_pcb.pcb_hw.apcb_ksp =
    810 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
    811 	proc0.p_md.md_tf =
    812 	    (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
    813 
    814 	/*
    815 	 * Look at arguments passed to us and compute boothowto.
    816 	 */
    817 
    818 	boothowto = RB_SINGLE;
    819 #ifdef KADB
    820 	boothowto |= RB_KDB;
    821 #endif
    822 	for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
    823 		/*
    824 		 * Note that we'd really like to differentiate case here,
    825 		 * but the Alpha AXP Architecture Reference Manual
    826 		 * says that we shouldn't.
    827 		 */
    828 		switch (*p) {
    829 		case 'a': /* autoboot */
    830 		case 'A':
    831 			boothowto &= ~RB_SINGLE;
    832 			break;
    833 
    834 #ifdef DEBUG
    835 		case 'c': /* crash dump immediately after autoconfig */
    836 		case 'C':
    837 			boothowto |= RB_DUMP;
    838 			break;
    839 #endif
    840 
    841 #if defined(KGDB) || defined(DDB)
    842 		case 'd': /* break into the kernel debugger ASAP */
    843 		case 'D':
    844 			boothowto |= RB_KDB;
    845 			break;
    846 #endif
    847 
    848 		case 'h': /* always halt, never reboot */
    849 		case 'H':
    850 			boothowto |= RB_HALT;
    851 			break;
    852 
    853 #if 0
    854 		case 'm': /* mini root present in memory */
    855 		case 'M':
    856 			boothowto |= RB_MINIROOT;
    857 			break;
    858 #endif
    859 
    860 		case 'n': /* askname */
    861 		case 'N':
    862 			boothowto |= RB_ASKNAME;
    863 			break;
    864 
    865 		case 's': /* single-user (default, supported for sanity) */
    866 		case 'S':
    867 			boothowto |= RB_SINGLE;
    868 			break;
    869 
    870 		case '-':
    871 			/*
    872 			 * Just ignore this.  It's not required, but it's
    873 			 * common for it to be passed regardless.
    874 			 */
    875 			break;
    876 
    877 		default:
    878 			printf("Unrecognized boot flag '%c'.\n", *p);
    879 			break;
    880 		}
    881 	}
    882 
    883 
    884 	/*
    885 	 * Figure out the number of cpus in the box, from RPB fields.
    886 	 * Really.  We mean it.
    887 	 */
    888 	for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
    889 		struct pcs *pcsp;
    890 
    891 		pcsp = LOCATE_PCS(hwrpb, i);
    892 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    893 			ncpus++;
    894 	}
    895 
    896 	/*
    897 	 * Initialize debuggers, and break into them if appropriate.
    898 	 */
    899 #ifdef DDB
    900 	db_machine_init();
    901 	ddb_init(ksym_start, ksym_end);
    902 	if (boothowto & RB_KDB)
    903 		Debugger();
    904 #endif
    905 #ifdef KGDB
    906 	if (boothowto & RB_KDB)
    907 		kgdb_connect(0);
    908 #endif
    909 	/*
    910 	 * Figure out our clock frequency, from RPB fields.
    911 	 */
    912 	hz = hwrpb->rpb_intr_freq >> 12;
    913 	if (!(60 <= hz && hz <= 10240)) {
    914 		hz = 1024;
    915 #ifdef DIAGNOSTIC
    916 		printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
    917 			hwrpb->rpb_intr_freq, hz);
    918 #endif
    919 	}
    920 }
    921 
    922 /*
    923  * Allocate space for system data structures.  We are given
    924  * a starting virtual address and we return a final virtual
    925  * address; along the way we set each data structure pointer.
    926  *
    927  * We call allocsys() with 0 to find out how much space we want,
    928  * allocate that much and fill it with zeroes, and the call
    929  * allocsys() again with the correct base virtual address.
    930  */
    931 caddr_t
    932 allocsys(v)
    933 	caddr_t v;
    934 {
    935 
    936 #define valloc(name, type, num) \
    937 	    (name) = (type *)v; v = (caddr_t)ALIGN((name)+(num))
    938 
    939 #ifdef REAL_CLISTS
    940 	valloc(cfree, struct cblock, nclist);
    941 #endif
    942 	valloc(callout, struct callout, ncallout);
    943 #ifdef SYSVSHM
    944 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
    945 #endif
    946 #ifdef SYSVSEM
    947 	valloc(sema, struct semid_ds, seminfo.semmni);
    948 	valloc(sem, struct sem, seminfo.semmns);
    949 	/* This is pretty disgusting! */
    950 	valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
    951 #endif
    952 #ifdef SYSVMSG
    953 	valloc(msgpool, char, msginfo.msgmax);
    954 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
    955 	valloc(msghdrs, struct msg, msginfo.msgtql);
    956 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
    957 #endif
    958 
    959 	/*
    960 	 * Determine how many buffers to allocate.
    961 	 * We allocate bufcache % of memory for buffer space.  Insure a
    962 	 * minimum of 16 buffers.  We allocate 1/2 as many swap buffer
    963 	 * headers as file i/o buffers.
    964 	 */
    965 	if (bufpages == 0)
    966 		bufpages = physmem / CLSIZE * bufcache / 100;
    967 	if (nbuf == 0) {
    968 		nbuf = bufpages;
    969 		if (nbuf < 16)
    970 			nbuf = 16;
    971 	}
    972 	if (nswbuf == 0) {
    973 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
    974 		if (nswbuf > 256)
    975 			nswbuf = 256;		/* sanity */
    976 	}
    977 #if !defined(UVM)
    978 	valloc(swbuf, struct buf, nswbuf);
    979 #endif
    980 	valloc(buf, struct buf, nbuf);
    981 
    982 	/*
    983 	 * There appears to be a correlation between the number
    984 	 * of processor slots defined in the HWRPB and the whami
    985 	 * value that can be returned.
    986 	 */
    987 	valloc(mchkinfo_all_cpus, struct mchkinfo, hwrpb->rpb_pcs_cnt);
    988 
    989 	return (v);
    990 #undef valloc
    991 }
    992 
    993 void
    994 consinit()
    995 {
    996 
    997 	/*
    998 	 * Everything related to console initialization is done
    999 	 * in alpha_init().
   1000 	 */
   1001 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
   1002 	printf("consinit: %susing prom console\n",
   1003 	    pmap_uses_prom_console() ? "" : "not ");
   1004 #endif
   1005 }
   1006 
   1007 #include "pckbc.h"
   1008 #include "pckbd.h"
   1009 #if (NPCKBC > 0) && (NPCKBD == 0)
   1010 
   1011 #include <machine/bus.h>
   1012 #include <dev/isa/pckbcvar.h>
   1013 
   1014 /*
   1015  * This is called by the pbkbc driver if no pckbd is configured.
   1016  * On the i386, it is used to glue in the old, deprecated console
   1017  * code.  On the Alpha, it does nothing.
   1018  */
   1019 int
   1020 pckbc_machdep_cnattach(kbctag, kbcslot)
   1021 	pckbc_tag_t kbctag;
   1022 	pckbc_slot_t kbcslot;
   1023 {
   1024 
   1025 	return (ENXIO);
   1026 }
   1027 #endif /* NPCKBC > 0 && NPCKBD == 0 */
   1028 
   1029 void
   1030 cpu_startup()
   1031 {
   1032 	register unsigned i;
   1033 	int base, residual;
   1034 	vaddr_t minaddr, maxaddr;
   1035 	vsize_t size;
   1036 #if defined(DEBUG)
   1037 	extern int pmapdebug;
   1038 	int opmapdebug = pmapdebug;
   1039 
   1040 	pmapdebug = 0;
   1041 #endif
   1042 
   1043 	/*
   1044 	 * Good {morning,afternoon,evening,night}.
   1045 	 */
   1046 	printf(version);
   1047 	identifycpu();
   1048 	printf("real mem = %lu (%lu reserved for PROM, %lu used by NetBSD)\n",
   1049 	    ((psize_t) totalphysmem << (psize_t) PAGE_SHIFT),
   1050 	    ptoa(resvmem), ptoa(physmem));
   1051 	if (unusedmem)
   1052 		printf("WARNING: unused memory = %d bytes\n", ctob(unusedmem));
   1053 	if (unknownmem)
   1054 		printf("WARNING: %d bytes of memory with unknown purpose\n",
   1055 		    ctob(unknownmem));
   1056 
   1057 	/*
   1058 	 * Allocate virtual address space for file I/O buffers.
   1059 	 * Note they are different than the array of headers, 'buf',
   1060 	 * and usually occupy more virtual memory than physical.
   1061 	 */
   1062 	size = MAXBSIZE * nbuf;
   1063 #if defined(UVM)
   1064 	if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
   1065 		    NULL, UVM_UNKNOWN_OFFSET,
   1066 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
   1067 				UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
   1068 		panic("startup: cannot allocate VM for buffers");
   1069 #else
   1070 	buffer_map = kmem_suballoc(kernel_map, (vaddr_t *)&buffers,
   1071 	    &maxaddr, size, TRUE);
   1072 	minaddr = (vaddr_t)buffers;
   1073 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vaddr_t)0,
   1074 			&minaddr, size, FALSE) != KERN_SUCCESS)
   1075 		panic("startup: cannot allocate buffers");
   1076 #endif /* UVM */
   1077 	base = bufpages / nbuf;
   1078 	residual = bufpages % nbuf;
   1079 	for (i = 0; i < nbuf; i++) {
   1080 #if defined(UVM)
   1081 		vsize_t curbufsize;
   1082 		vaddr_t curbuf;
   1083 		struct vm_page *pg;
   1084 
   1085 		/*
   1086 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
   1087 		 * that MAXBSIZE space, we allocate and map (base+1) pages
   1088 		 * for the first "residual" buffers, and then we allocate
   1089 		 * "base" pages for the rest.
   1090 		 */
   1091 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
   1092 		curbufsize = CLBYTES * ((i < residual) ? (base+1) : base);
   1093 
   1094 		while (curbufsize) {
   1095 			pg = uvm_pagealloc(NULL, 0, NULL);
   1096 			if (pg == NULL)
   1097 				panic("cpu_startup: not enough memory for "
   1098 				    "buffer cache");
   1099 #if defined(PMAP_NEW)
   1100 			pmap_kenter_pgs(curbuf, &pg, 1);
   1101 #else
   1102 			pmap_enter(kernel_map->pmap, curbuf,
   1103 				   VM_PAGE_TO_PHYS(pg), VM_PROT_ALL, TRUE);
   1104 #endif
   1105 			curbuf += PAGE_SIZE;
   1106 			curbufsize -= PAGE_SIZE;
   1107 		}
   1108 #else /* ! UVM */
   1109 		vsize_t curbufsize;
   1110 		vaddr_t curbuf;
   1111 
   1112 		/*
   1113 		 * First <residual> buffers get (base+1) physical pages
   1114 		 * allocated for them.  The rest get (base) physical pages.
   1115 		 *
   1116 		 * The rest of each buffer occupies virtual space,
   1117 		 * but has no physical memory allocated for it.
   1118 		 */
   1119 		curbuf = (vaddr_t)buffers + i * MAXBSIZE;
   1120 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
   1121 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
   1122 		vm_map_simplify(buffer_map, curbuf);
   1123 #endif /* UVM */
   1124 	}
   1125 	/*
   1126 	 * Allocate a submap for exec arguments.  This map effectively
   1127 	 * limits the number of processes exec'ing at any time.
   1128 	 */
   1129 #if defined(UVM)
   1130 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
   1131 				   16 * NCARGS, TRUE, FALSE, NULL);
   1132 #else
   1133 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
   1134 				 16 * NCARGS, TRUE);
   1135 #endif
   1136 
   1137 	/*
   1138 	 * Allocate a submap for physio
   1139 	 */
   1140 #if defined(UVM)
   1141 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
   1142 				   VM_PHYS_SIZE, TRUE, FALSE, NULL);
   1143 #else
   1144 	phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
   1145 				 VM_PHYS_SIZE, TRUE);
   1146 #endif
   1147 
   1148 	/*
   1149 	 * Finally, allocate mbuf cluster submap.
   1150 	 */
   1151 #if defined(UVM)
   1152 	mb_map = uvm_km_suballoc(kernel_map, (vaddr_t *)&mbutl, &maxaddr,
   1153 				 VM_MBUF_SIZE, FALSE, FALSE, NULL);
   1154 #else
   1155 	mb_map = kmem_suballoc(kernel_map, (vaddr_t *)&mbutl, &maxaddr,
   1156 			       VM_MBUF_SIZE, FALSE);
   1157 #endif
   1158 	/*
   1159 	 * Initialize callouts
   1160 	 */
   1161 	callfree = callout;
   1162 	for (i = 1; i < ncallout; i++)
   1163 		callout[i-1].c_next = &callout[i];
   1164 	callout[i-1].c_next = NULL;
   1165 
   1166 #if defined(DEBUG)
   1167 	pmapdebug = opmapdebug;
   1168 #endif
   1169 #if defined(UVM)
   1170 	printf("avail mem = %ld\n", (long)ptoa(uvmexp.free));
   1171 #else
   1172 	printf("avail mem = %ld\n", (long)ptoa(cnt.v_free_count));
   1173 #endif
   1174 #if 0
   1175 	{
   1176 		extern u_long pmap_pages_stolen;
   1177 		printf("stolen memory for VM structures = %ld bytes\n",
   1178 		    pmap_pages_stolen * PAGE_SIZE);
   1179 	}
   1180 #endif
   1181 	printf("using %ld buffers containing %ld bytes of memory\n",
   1182 		(long)nbuf, (long)(bufpages * CLBYTES));
   1183 
   1184 	/*
   1185 	 * Set up buffers, so they can be used to read disk labels.
   1186 	 */
   1187 	bufinit();
   1188 
   1189 	/*
   1190 	 * Set up the HWPCB so that it's safe to configure secondary
   1191 	 * CPUs.
   1192 	 */
   1193 	hwrpb_primary_init();
   1194 
   1195 	/*
   1196 	 * Configure the system.
   1197 	 */
   1198 	configure();
   1199 }
   1200 
   1201 /*
   1202  * Retrieve the platform name from the DSR.
   1203  */
   1204 const char *
   1205 alpha_dsr_sysname()
   1206 {
   1207 	struct dsrdb *dsr;
   1208 	const char *sysname;
   1209 
   1210 	/*
   1211 	 * DSR does not exist on early HWRPB versions.
   1212 	 */
   1213 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
   1214 		return (NULL);
   1215 
   1216 	dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
   1217 	sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
   1218 	    sizeof(u_int64_t)));
   1219 	return (sysname);
   1220 }
   1221 
   1222 /*
   1223  * Lookup the system specified system variation in the provided table,
   1224  * returning the model string on match.
   1225  */
   1226 const char *
   1227 alpha_variation_name(variation, avtp)
   1228 	u_int64_t variation;
   1229 	const struct alpha_variation_table *avtp;
   1230 {
   1231 	int i;
   1232 
   1233 	for (i = 0; avtp[i].avt_model != NULL; i++)
   1234 		if (avtp[i].avt_variation == variation)
   1235 			return (avtp[i].avt_model);
   1236 	return (NULL);
   1237 }
   1238 
   1239 /*
   1240  * Generate a default platform name based for unknown system variations.
   1241  */
   1242 const char *
   1243 alpha_unknown_sysname()
   1244 {
   1245 	static char s[128];		/* safe size */
   1246 
   1247 	sprintf(s, "%s family, unknown model variation 0x%lx",
   1248 	    platform.family, hwrpb->rpb_variation & SV_ST_MASK);
   1249 	return ((const char *)s);
   1250 }
   1251 
   1252 void
   1253 identifycpu()
   1254 {
   1255 
   1256 	/*
   1257 	 * print out CPU identification information.
   1258 	 */
   1259 	printf("%s, %ldMHz\n", cpu_model,
   1260 	    hwrpb->rpb_cc_freq / 1000000);	/* XXX true for 21164? */
   1261 	printf("%ld byte page size, %d processor%s.\n",
   1262 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
   1263 #if 0
   1264 	/* this isn't defined for any systems that we run on? */
   1265 	printf("serial number 0x%lx 0x%lx\n",
   1266 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
   1267 
   1268 	/* and these aren't particularly useful! */
   1269 	printf("variation: 0x%lx, revision 0x%lx\n",
   1270 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
   1271 #endif
   1272 }
   1273 
   1274 int	waittime = -1;
   1275 struct pcb dumppcb;
   1276 
   1277 void
   1278 cpu_reboot(howto, bootstr)
   1279 	int howto;
   1280 	char *bootstr;
   1281 {
   1282 	extern int cold;
   1283 #if defined(MULTIPROCESSOR)
   1284 #if 0 /* XXX See below. */
   1285 	u_long cpu_id;
   1286 #endif
   1287 #endif
   1288 
   1289 #if defined(MULTIPROCESSOR)
   1290 	/* We must be running on the primary CPU. */
   1291 	if (alpha_pal_whami() != hwrpb->rpb_primary_cpu_id)
   1292 		panic("cpu_reboot: not on primary CPU!");
   1293 #endif
   1294 
   1295 	/* If system is cold, just halt. */
   1296 	if (cold) {
   1297 		howto |= RB_HALT;
   1298 		goto haltsys;
   1299 	}
   1300 
   1301 	/* If "always halt" was specified as a boot flag, obey. */
   1302 	if ((boothowto & RB_HALT) != 0)
   1303 		howto |= RB_HALT;
   1304 
   1305 	boothowto = howto;
   1306 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
   1307 		waittime = 0;
   1308 		vfs_shutdown();
   1309 		/*
   1310 		 * If we've been adjusting the clock, the todr
   1311 		 * will be out of synch; adjust it now.
   1312 		 */
   1313 		resettodr();
   1314 	}
   1315 
   1316 	/* Disable interrupts. */
   1317 	splhigh();
   1318 
   1319 	/* If rebooting and a dump is requested do it. */
   1320 #if 0
   1321 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
   1322 #else
   1323 	if (howto & RB_DUMP)
   1324 #endif
   1325 		dumpsys();
   1326 
   1327 haltsys:
   1328 
   1329 	/* run any shutdown hooks */
   1330 	doshutdownhooks();
   1331 
   1332 #if defined(MULTIPROCESSOR)
   1333 #if 0 /* XXX doesn't work when called from here?! */
   1334 	/* Kill off any secondary CPUs. */
   1335 	for (cpu_id = 0; cpu_id < hwrpb->rpb_pcs_cnt; cpu_id++) {
   1336 		if (cpu_id == hwrpb->rpb_primary_cpu_id ||
   1337 		    cpus[cpu_id] == NULL)
   1338 			continue;
   1339 		cpu_halt_secondary(cpu_id);
   1340 	}
   1341 #endif
   1342 #endif
   1343 
   1344 #ifdef BOOTKEY
   1345 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
   1346 	cnpollc(1);	/* for proper keyboard command handling */
   1347 	cngetc();
   1348 	cnpollc(0);
   1349 	printf("\n");
   1350 #endif
   1351 
   1352 	/* Finally, powerdown/halt/reboot the system. */
   1353 	if ((howto && RB_POWERDOWN) == RB_POWERDOWN &&
   1354 	    platform.powerdown != NULL) {
   1355 		(*platform.powerdown)();
   1356 		printf("WARNING: powerdown failed!\n");
   1357 	}
   1358 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
   1359 	prom_halt(howto & RB_HALT);
   1360 	/*NOTREACHED*/
   1361 }
   1362 
   1363 /*
   1364  * These variables are needed by /sbin/savecore
   1365  */
   1366 u_long	dumpmag = 0x8fca0101;	/* magic number */
   1367 int 	dumpsize = 0;		/* pages */
   1368 long	dumplo = 0; 		/* blocks */
   1369 
   1370 /*
   1371  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
   1372  */
   1373 int
   1374 cpu_dumpsize()
   1375 {
   1376 	int size;
   1377 
   1378 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
   1379 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
   1380 	if (roundup(size, dbtob(1)) != dbtob(1))
   1381 		return -1;
   1382 
   1383 	return (1);
   1384 }
   1385 
   1386 /*
   1387  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
   1388  */
   1389 u_long
   1390 cpu_dump_mempagecnt()
   1391 {
   1392 	u_long i, n;
   1393 
   1394 	n = 0;
   1395 	for (i = 0; i < mem_cluster_cnt; i++)
   1396 		n += atop(mem_clusters[i].size);
   1397 	return (n);
   1398 }
   1399 
   1400 /*
   1401  * cpu_dump: dump machine-dependent kernel core dump headers.
   1402  */
   1403 int
   1404 cpu_dump()
   1405 {
   1406 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1407 	char buf[dbtob(1)];
   1408 	kcore_seg_t *segp;
   1409 	cpu_kcore_hdr_t *cpuhdrp;
   1410 	phys_ram_seg_t *memsegp;
   1411 	int i;
   1412 
   1413 	dump = bdevsw[major(dumpdev)].d_dump;
   1414 
   1415 	bzero(buf, sizeof buf);
   1416 	segp = (kcore_seg_t *)buf;
   1417 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
   1418 	memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
   1419 	    ALIGN(sizeof(*cpuhdrp))];
   1420 
   1421 	/*
   1422 	 * Generate a segment header.
   1423 	 */
   1424 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
   1425 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
   1426 
   1427 	/*
   1428 	 * Add the machine-dependent header info.
   1429 	 */
   1430 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
   1431 	cpuhdrp->page_size = PAGE_SIZE;
   1432 	cpuhdrp->nmemsegs = mem_cluster_cnt;
   1433 
   1434 	/*
   1435 	 * Fill in the memory segment descriptors.
   1436 	 */
   1437 	for (i = 0; i < mem_cluster_cnt; i++) {
   1438 		memsegp[i].start = mem_clusters[i].start;
   1439 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
   1440 	}
   1441 
   1442 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
   1443 }
   1444 
   1445 /*
   1446  * This is called by main to set dumplo and dumpsize.
   1447  * Dumps always skip the first CLBYTES of disk space
   1448  * in case there might be a disk label stored there.
   1449  * If there is extra space, put dump at the end to
   1450  * reduce the chance that swapping trashes it.
   1451  */
   1452 void
   1453 cpu_dumpconf()
   1454 {
   1455 	int nblks, dumpblks;	/* size of dump area */
   1456 	int maj;
   1457 
   1458 	if (dumpdev == NODEV)
   1459 		goto bad;
   1460 	maj = major(dumpdev);
   1461 	if (maj < 0 || maj >= nblkdev)
   1462 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
   1463 	if (bdevsw[maj].d_psize == NULL)
   1464 		goto bad;
   1465 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
   1466 	if (nblks <= ctod(1))
   1467 		goto bad;
   1468 
   1469 	dumpblks = cpu_dumpsize();
   1470 	if (dumpblks < 0)
   1471 		goto bad;
   1472 	dumpblks += ctod(cpu_dump_mempagecnt());
   1473 
   1474 	/* If dump won't fit (incl. room for possible label), punt. */
   1475 	if (dumpblks > (nblks - ctod(1)))
   1476 		goto bad;
   1477 
   1478 	/* Put dump at end of partition */
   1479 	dumplo = nblks - dumpblks;
   1480 
   1481 	/* dumpsize is in page units, and doesn't include headers. */
   1482 	dumpsize = cpu_dump_mempagecnt();
   1483 	return;
   1484 
   1485 bad:
   1486 	dumpsize = 0;
   1487 	return;
   1488 }
   1489 
   1490 /*
   1491  * Dump the kernel's image to the swap partition.
   1492  */
   1493 #define	BYTES_PER_DUMP	NBPG
   1494 
   1495 void
   1496 dumpsys()
   1497 {
   1498 	u_long totalbytesleft, bytes, i, n, memcl;
   1499 	u_long maddr;
   1500 	int psize;
   1501 	daddr_t blkno;
   1502 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1503 	int error;
   1504 
   1505 	/* Save registers. */
   1506 	savectx(&dumppcb);
   1507 
   1508 	msgbufenabled = 0;	/* don't record dump msgs in msgbuf */
   1509 	if (dumpdev == NODEV)
   1510 		return;
   1511 
   1512 	/*
   1513 	 * For dumps during autoconfiguration,
   1514 	 * if dump device has already configured...
   1515 	 */
   1516 	if (dumpsize == 0)
   1517 		cpu_dumpconf();
   1518 	if (dumplo <= 0) {
   1519 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
   1520 		    minor(dumpdev));
   1521 		return;
   1522 	}
   1523 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
   1524 	    minor(dumpdev), dumplo);
   1525 
   1526 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
   1527 	printf("dump ");
   1528 	if (psize == -1) {
   1529 		printf("area unavailable\n");
   1530 		return;
   1531 	}
   1532 
   1533 	/* XXX should purge all outstanding keystrokes. */
   1534 
   1535 	if ((error = cpu_dump()) != 0)
   1536 		goto err;
   1537 
   1538 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
   1539 	blkno = dumplo + cpu_dumpsize();
   1540 	dump = bdevsw[major(dumpdev)].d_dump;
   1541 	error = 0;
   1542 
   1543 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
   1544 		maddr = mem_clusters[memcl].start;
   1545 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
   1546 
   1547 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
   1548 
   1549 			/* Print out how many MBs we to go. */
   1550 			if ((totalbytesleft % (1024*1024)) == 0)
   1551 				printf("%d ", totalbytesleft / (1024 * 1024));
   1552 
   1553 			/* Limit size for next transfer. */
   1554 			n = bytes - i;
   1555 			if (n > BYTES_PER_DUMP)
   1556 				n =  BYTES_PER_DUMP;
   1557 
   1558 			error = (*dump)(dumpdev, blkno,
   1559 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
   1560 			if (error)
   1561 				goto err;
   1562 			maddr += n;
   1563 			blkno += btodb(n);			/* XXX? */
   1564 
   1565 			/* XXX should look for keystrokes, to cancel. */
   1566 		}
   1567 	}
   1568 
   1569 err:
   1570 	switch (error) {
   1571 
   1572 	case ENXIO:
   1573 		printf("device bad\n");
   1574 		break;
   1575 
   1576 	case EFAULT:
   1577 		printf("device not ready\n");
   1578 		break;
   1579 
   1580 	case EINVAL:
   1581 		printf("area improper\n");
   1582 		break;
   1583 
   1584 	case EIO:
   1585 		printf("i/o error\n");
   1586 		break;
   1587 
   1588 	case EINTR:
   1589 		printf("aborted from console\n");
   1590 		break;
   1591 
   1592 	case 0:
   1593 		printf("succeeded\n");
   1594 		break;
   1595 
   1596 	default:
   1597 		printf("error %d\n", error);
   1598 		break;
   1599 	}
   1600 	printf("\n\n");
   1601 	delay(1000);
   1602 }
   1603 
   1604 void
   1605 frametoreg(framep, regp)
   1606 	struct trapframe *framep;
   1607 	struct reg *regp;
   1608 {
   1609 
   1610 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1611 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1612 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1613 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1614 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1615 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1616 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1617 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1618 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1619 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1620 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1621 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1622 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1623 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1624 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1625 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1626 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1627 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1628 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1629 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1630 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1631 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1632 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1633 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1634 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1635 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1636 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1637 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1638 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1639 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1640 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1641 	regp->r_regs[R_ZERO] = 0;
   1642 }
   1643 
   1644 void
   1645 regtoframe(regp, framep)
   1646 	struct reg *regp;
   1647 	struct trapframe *framep;
   1648 {
   1649 
   1650 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1651 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1652 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1653 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1654 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1655 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1656 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1657 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1658 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1659 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1660 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1661 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1662 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1663 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1664 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1665 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1666 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1667 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1668 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1669 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1670 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1671 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1672 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1673 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1674 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1675 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1676 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1677 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1678 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1679 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1680 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1681 	/* ??? = regp->r_regs[R_ZERO]; */
   1682 }
   1683 
   1684 void
   1685 printregs(regp)
   1686 	struct reg *regp;
   1687 {
   1688 	int i;
   1689 
   1690 	for (i = 0; i < 32; i++)
   1691 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1692 		   i & 1 ? "\n" : "\t");
   1693 }
   1694 
   1695 void
   1696 regdump(framep)
   1697 	struct trapframe *framep;
   1698 {
   1699 	struct reg reg;
   1700 
   1701 	frametoreg(framep, &reg);
   1702 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1703 
   1704 	printf("REGISTERS:\n");
   1705 	printregs(&reg);
   1706 }
   1707 
   1708 #ifdef DEBUG
   1709 int sigdebug = 0;
   1710 int sigpid = 0;
   1711 #define	SDB_FOLLOW	0x01
   1712 #define	SDB_KSTACK	0x02
   1713 #endif
   1714 
   1715 /*
   1716  * Send an interrupt to process.
   1717  */
   1718 void
   1719 sendsig(catcher, sig, mask, code)
   1720 	sig_t catcher;
   1721 	int sig;
   1722 	sigset_t *mask;
   1723 	u_long code;
   1724 {
   1725 	struct proc *p = curproc;
   1726 	struct sigcontext *scp, ksc;
   1727 	struct trapframe *frame;
   1728 	struct sigacts *psp = p->p_sigacts;
   1729 	int onstack, fsize, rndfsize;
   1730 	extern struct proc *fpcurproc;
   1731 
   1732 	frame = p->p_md.md_tf;
   1733 
   1734 	/* Do we need to jump onto the signal stack? */
   1735 	onstack =
   1736 	    (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
   1737 	    (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
   1738 
   1739 	/* Allocate space for the signal handler context. */
   1740 	fsize = sizeof(ksc);
   1741 	rndfsize = ((fsize + 15) / 16) * 16;
   1742 
   1743 	if (onstack)
   1744 		scp = (struct sigcontext *)((caddr_t)psp->ps_sigstk.ss_sp +
   1745 						     psp->ps_sigstk.ss_size);
   1746 	else
   1747 		scp = (struct sigcontext *)(alpha_pal_rdusp());
   1748 	scp = (struct sigcontext *)((caddr_t)scp - rndfsize);
   1749 
   1750 #ifdef DEBUG
   1751 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1752 		printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1753 		    sig, &onstack, scp);
   1754 #endif
   1755 
   1756 	/* Build stack frame for signal trampoline. */
   1757 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
   1758 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
   1759 
   1760 	/* Save register context. */
   1761 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1762 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1763 	ksc.sc_regs[R_SP] = alpha_pal_rdusp();
   1764 
   1765 	/* save the floating-point state, if necessary, then copy it. */
   1766 	if (p == fpcurproc) {
   1767 		alpha_pal_wrfen(1);
   1768 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
   1769 		alpha_pal_wrfen(0);
   1770 		fpcurproc = NULL;
   1771 	}
   1772 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
   1773 	bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1774 	    sizeof(struct fpreg));
   1775 	ksc.sc_fp_control = 0;					/* XXX ? */
   1776 	bzero(ksc.sc_reserved, sizeof ksc.sc_reserved);		/* XXX */
   1777 	bzero(ksc.sc_xxx, sizeof ksc.sc_xxx);			/* XXX */
   1778 
   1779 	/* Save signal stack. */
   1780 	ksc.sc_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
   1781 
   1782 	/* Save signal mask. */
   1783 	ksc.sc_mask = *mask;
   1784 
   1785 #ifdef COMPAT_13
   1786 	/*
   1787 	 * XXX We always have to save an old style signal mask because
   1788 	 * XXX we might be delivering a signal to a process which will
   1789 	 * XXX escape from the signal in a non-standard way and invoke
   1790 	 * XXX sigreturn() directly.
   1791 	 */
   1792 	{
   1793 		/* Note: it's a long in the stack frame. */
   1794 		sigset13_t mask13;
   1795 
   1796 		native_sigset_to_sigset13(mask, &mask13);
   1797 		ksc.__sc_mask13 = mask13;
   1798 	}
   1799 #endif
   1800 
   1801 #ifdef COMPAT_OSF1
   1802 	/*
   1803 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1804 	 */
   1805 #endif
   1806 
   1807 	if (copyout(&ksc, (caddr_t)scp, fsize) != 0) {
   1808 		/*
   1809 		 * Process has trashed its stack; give it an illegal
   1810 		 * instruction to halt it in its tracks.
   1811 		 */
   1812 #ifdef DEBUG
   1813 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1814 			printf("sendsig(%d): copyout failed on sig %d\n",
   1815 			    p->p_pid, sig);
   1816 #endif
   1817 		sigexit(p, SIGILL);
   1818 		/* NOTREACHED */
   1819 	}
   1820 #ifdef DEBUG
   1821 	if (sigdebug & SDB_FOLLOW)
   1822 		printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
   1823 		    scp, code);
   1824 #endif
   1825 
   1826 	/* Set up the registers to return to sigcode. */
   1827 	frame->tf_regs[FRAME_PC] = (u_int64_t)psp->ps_sigcode;
   1828 	frame->tf_regs[FRAME_A0] = sig;
   1829 	frame->tf_regs[FRAME_A1] = code;
   1830 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
   1831 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
   1832 	alpha_pal_wrusp((unsigned long)scp);
   1833 
   1834 	/* Remember that we're now on the signal stack. */
   1835 	if (onstack)
   1836 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
   1837 
   1838 #ifdef DEBUG
   1839 	if (sigdebug & SDB_FOLLOW)
   1840 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1841 		    frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
   1842 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1843 		printf("sendsig(%d): sig %d returns\n",
   1844 		    p->p_pid, sig);
   1845 #endif
   1846 }
   1847 
   1848 /*
   1849  * System call to cleanup state after a signal
   1850  * has been taken.  Reset signal mask and
   1851  * stack state from context left by sendsig (above).
   1852  * Return to previous pc and psl as specified by
   1853  * context left by sendsig. Check carefully to
   1854  * make sure that the user has not modified the
   1855  * psl to gain improper priviledges or to cause
   1856  * a machine fault.
   1857  */
   1858 /* ARGSUSED */
   1859 int
   1860 sys___sigreturn14(p, v, retval)
   1861 	struct proc *p;
   1862 	void *v;
   1863 	register_t *retval;
   1864 {
   1865 	struct sys___sigreturn14_args /* {
   1866 		syscallarg(struct sigcontext *) sigcntxp;
   1867 	} */ *uap = v;
   1868 	struct sigcontext *scp, ksc;
   1869 	extern struct proc *fpcurproc;
   1870 
   1871 	/*
   1872 	 * The trampoline code hands us the context.
   1873 	 * It is unsafe to keep track of it ourselves, in the event that a
   1874 	 * program jumps out of a signal handler.
   1875 	 */
   1876 	scp = SCARG(uap, sigcntxp);
   1877 #ifdef DEBUG
   1878 	if (sigdebug & SDB_FOLLOW)
   1879 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
   1880 #endif
   1881 	if (ALIGN(scp) != (u_int64_t)scp)
   1882 		return (EINVAL);
   1883 
   1884 	if (copyin((caddr_t)scp, &ksc, sizeof(ksc)) != 0)
   1885 		return (EFAULT);
   1886 
   1887 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1888 		return (EINVAL);
   1889 
   1890 	/* Restore register context. */
   1891 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
   1892 	p->p_md.md_tf->tf_regs[FRAME_PS] =
   1893 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
   1894 
   1895 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
   1896 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
   1897 
   1898 	/* XXX ksc.sc_ownedfp ? */
   1899 	if (p == fpcurproc)
   1900 		fpcurproc = NULL;
   1901 	bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
   1902 	    sizeof(struct fpreg));
   1903 	/* XXX ksc.sc_fp_control ? */
   1904 
   1905 	/* Restore signal stack. */
   1906 	if (ksc.sc_onstack & SS_ONSTACK)
   1907 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
   1908 	else
   1909 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1910 
   1911 	/* Restore signal mask. */
   1912 	(void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0);
   1913 
   1914 #ifdef DEBUG
   1915 	if (sigdebug & SDB_FOLLOW)
   1916 		printf("sigreturn(%d): returns\n", p->p_pid);
   1917 #endif
   1918 	return (EJUSTRETURN);
   1919 }
   1920 
   1921 /*
   1922  * machine dependent system variables.
   1923  */
   1924 int
   1925 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1926 	int *name;
   1927 	u_int namelen;
   1928 	void *oldp;
   1929 	size_t *oldlenp;
   1930 	void *newp;
   1931 	size_t newlen;
   1932 	struct proc *p;
   1933 {
   1934 	dev_t consdev;
   1935 
   1936 	/* all sysctl names at this level are terminal */
   1937 	if (namelen != 1)
   1938 		return (ENOTDIR);		/* overloaded */
   1939 
   1940 	switch (name[0]) {
   1941 	case CPU_CONSDEV:
   1942 		if (cn_tab != NULL)
   1943 			consdev = cn_tab->cn_dev;
   1944 		else
   1945 			consdev = NODEV;
   1946 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1947 			sizeof consdev));
   1948 
   1949 	case CPU_ROOT_DEVICE:
   1950 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1951 		    root_device->dv_xname));
   1952 
   1953 	case CPU_UNALIGNED_PRINT:
   1954 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1955 		    &alpha_unaligned_print));
   1956 
   1957 	case CPU_UNALIGNED_FIX:
   1958 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1959 		    &alpha_unaligned_fix));
   1960 
   1961 	case CPU_UNALIGNED_SIGBUS:
   1962 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1963 		    &alpha_unaligned_sigbus));
   1964 
   1965 	case CPU_BOOTED_KERNEL:
   1966 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1967 		    bootinfo.booted_kernel));
   1968 
   1969 	default:
   1970 		return (EOPNOTSUPP);
   1971 	}
   1972 	/* NOTREACHED */
   1973 }
   1974 
   1975 /*
   1976  * Set registers on exec.
   1977  */
   1978 void
   1979 setregs(p, pack, stack)
   1980 	register struct proc *p;
   1981 	struct exec_package *pack;
   1982 	u_long stack;
   1983 {
   1984 	struct trapframe *tfp = p->p_md.md_tf;
   1985 	extern struct proc *fpcurproc;
   1986 #ifdef DEBUG
   1987 	int i;
   1988 #endif
   1989 
   1990 #ifdef DEBUG
   1991 	/*
   1992 	 * Crash and dump, if the user requested it.
   1993 	 */
   1994 	if (boothowto & RB_DUMP)
   1995 		panic("crash requested by boot flags");
   1996 #endif
   1997 
   1998 #ifdef DEBUG
   1999 	for (i = 0; i < FRAME_SIZE; i++)
   2000 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   2001 #else
   2002 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   2003 #endif
   2004 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
   2005 #define FP_RN 2 /* XXX */
   2006 	p->p_addr->u_pcb.pcb_fp.fpr_cr = (long)FP_RN << 58;
   2007 	alpha_pal_wrusp(stack);
   2008 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   2009 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   2010 
   2011 	tfp->tf_regs[FRAME_A0] = stack;			/* a0 = sp */
   2012 	tfp->tf_regs[FRAME_A1] = 0;			/* a1 = rtld cleanup */
   2013 	tfp->tf_regs[FRAME_A2] = 0;			/* a2 = rtld object */
   2014 	tfp->tf_regs[FRAME_A3] = (u_int64_t)PS_STRINGS;	/* a3 = ps_strings */
   2015 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   2016 
   2017 	p->p_md.md_flags &= ~MDP_FPUSED;
   2018 	if (fpcurproc == p)
   2019 		fpcurproc = NULL;
   2020 }
   2021 
   2022 void
   2023 netintr()
   2024 {
   2025 	int n, s;
   2026 
   2027 	s = splhigh();
   2028 	n = netisr;
   2029 	netisr = 0;
   2030 	splx(s);
   2031 
   2032 #define	DONETISR(bit, fn)						\
   2033 	do {								\
   2034 		if (n & (1 << (bit)))					\
   2035 			fn;						\
   2036 	} while (0)
   2037 
   2038 #ifdef INET
   2039 #if NARP > 0
   2040 	DONETISR(NETISR_ARP, arpintr());
   2041 #endif
   2042 	DONETISR(NETISR_IP, ipintr());
   2043 #endif
   2044 #ifdef NETATALK
   2045 	DONETISR(NETISR_ATALK, atintr());
   2046 #endif
   2047 #ifdef NS
   2048 	DONETISR(NETISR_NS, nsintr());
   2049 #endif
   2050 #ifdef ISO
   2051 	DONETISR(NETISR_ISO, clnlintr());
   2052 #endif
   2053 #ifdef CCITT
   2054 	DONETISR(NETISR_CCITT, ccittintr());
   2055 #endif
   2056 #ifdef NATM
   2057 	DONETISR(NETISR_NATM, natmintr());
   2058 #endif
   2059 #if NPPP > 1
   2060 	DONETISR(NETISR_PPP, pppintr());
   2061 #endif
   2062 
   2063 #undef DONETISR
   2064 }
   2065 
   2066 void
   2067 do_sir()
   2068 {
   2069 	u_int64_t n;
   2070 
   2071 	do {
   2072 		(void)splhigh();
   2073 		n = ssir;
   2074 		ssir = 0;
   2075 		splsoft();		/* don't recurse through spl0() */
   2076 
   2077 #if defined(UVM)
   2078 #define	COUNT_SOFT	uvmexp.softs++
   2079 #else
   2080 #define	COUNT_SOFT	cnt.v_soft++
   2081 #endif
   2082 
   2083 #define	DO_SIR(bit, fn)							\
   2084 		do {							\
   2085 			if (n & (bit)) {				\
   2086 				COUNT_SOFT;				\
   2087 				fn;					\
   2088 			}						\
   2089 		} while (0)
   2090 
   2091 		DO_SIR(SIR_NET, netintr());
   2092 		DO_SIR(SIR_CLOCK, softclock());
   2093 #if NCOM > 0
   2094 		DO_SIR(SIR_SERIAL, comsoft());
   2095 #endif
   2096 
   2097 #undef COUNT_SOFT
   2098 #undef DO_SIR
   2099 	} while (ssir != 0);
   2100 }
   2101 
   2102 int
   2103 spl0()
   2104 {
   2105 
   2106 	if (ssir)
   2107 		do_sir();		/* it lowers the IPL itself */
   2108 
   2109 	return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
   2110 }
   2111 
   2112 /*
   2113  * The following primitives manipulate the run queues.  _whichqs tells which
   2114  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
   2115  * into queues, Remrunqueue removes them from queues.  The running process is
   2116  * on no queue, other processes are on a queue related to p->p_priority,
   2117  * divided by 4 actually to shrink the 0-127 range of priorities into the 32
   2118  * available queues.
   2119  */
   2120 /*
   2121  * setrunqueue(p)
   2122  *	proc *p;
   2123  *
   2124  * Call should be made at splclock(), and p->p_stat should be SRUN.
   2125  */
   2126 
   2127 void
   2128 setrunqueue(p)
   2129 	struct proc *p;
   2130 {
   2131 	int bit;
   2132 
   2133 	/* firewall: p->p_back must be NULL */
   2134 	if (p->p_back != NULL)
   2135 		panic("setrunqueue");
   2136 
   2137 	bit = p->p_priority >> 2;
   2138 	whichqs |= (1 << bit);
   2139 	p->p_forw = (struct proc *)&qs[bit];
   2140 	p->p_back = qs[bit].ph_rlink;
   2141 	p->p_back->p_forw = p;
   2142 	qs[bit].ph_rlink = p;
   2143 }
   2144 
   2145 /*
   2146  * remrunqueue(p)
   2147  *
   2148  * Call should be made at splclock().
   2149  */
   2150 void
   2151 remrunqueue(p)
   2152 	struct proc *p;
   2153 {
   2154 	int bit;
   2155 
   2156 	bit = p->p_priority >> 2;
   2157 	if ((whichqs & (1 << bit)) == 0)
   2158 		panic("remrunqueue");
   2159 
   2160 	p->p_back->p_forw = p->p_forw;
   2161 	p->p_forw->p_back = p->p_back;
   2162 	p->p_back = NULL;	/* for firewall checking. */
   2163 
   2164 	if ((struct proc *)&qs[bit] == qs[bit].ph_link)
   2165 		whichqs &= ~(1 << bit);
   2166 }
   2167 
   2168 /*
   2169  * Return the best possible estimate of the time in the timeval
   2170  * to which tvp points.  Unfortunately, we can't read the hardware registers.
   2171  * We guarantee that the time will be greater than the value obtained by a
   2172  * previous call.
   2173  */
   2174 void
   2175 microtime(tvp)
   2176 	register struct timeval *tvp;
   2177 {
   2178 	int s = splclock();
   2179 	static struct timeval lasttime;
   2180 
   2181 	*tvp = time;
   2182 #ifdef notdef
   2183 	tvp->tv_usec += clkread();
   2184 	while (tvp->tv_usec > 1000000) {
   2185 		tvp->tv_sec++;
   2186 		tvp->tv_usec -= 1000000;
   2187 	}
   2188 #endif
   2189 	if (tvp->tv_sec == lasttime.tv_sec &&
   2190 	    tvp->tv_usec <= lasttime.tv_usec &&
   2191 	    (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
   2192 		tvp->tv_sec++;
   2193 		tvp->tv_usec -= 1000000;
   2194 	}
   2195 	lasttime = *tvp;
   2196 	splx(s);
   2197 }
   2198 
   2199 /*
   2200  * Wait "n" microseconds.
   2201  */
   2202 void
   2203 delay(n)
   2204 	unsigned long n;
   2205 {
   2206 	long N = cycles_per_usec * (n);
   2207 
   2208 	while (N > 0)				/* XXX */
   2209 		N -= 3;				/* XXX */
   2210 }
   2211 
   2212 #if defined(COMPAT_OSF1) || 1		/* XXX */
   2213 void	cpu_exec_ecoff_setregs __P((struct proc *, struct exec_package *,
   2214 	    u_long));
   2215 
   2216 void
   2217 cpu_exec_ecoff_setregs(p, epp, stack)
   2218 	struct proc *p;
   2219 	struct exec_package *epp;
   2220 	u_long stack;
   2221 {
   2222 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   2223 
   2224 	setregs(p, epp, stack);
   2225 	p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   2226 }
   2227 
   2228 /*
   2229  * cpu_exec_ecoff_hook():
   2230  *	cpu-dependent ECOFF format hook for execve().
   2231  *
   2232  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   2233  *
   2234  */
   2235 int
   2236 cpu_exec_ecoff_hook(p, epp)
   2237 	struct proc *p;
   2238 	struct exec_package *epp;
   2239 {
   2240 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   2241 	extern struct emul emul_netbsd;
   2242 #ifdef COMPAT_OSF1
   2243 	extern struct emul emul_osf1;
   2244 #endif
   2245 
   2246 	switch (execp->f.f_magic) {
   2247 #ifdef COMPAT_OSF1
   2248 	case ECOFF_MAGIC_ALPHA:
   2249 		epp->ep_emul = &emul_osf1;
   2250 		break;
   2251 #endif
   2252 
   2253 	case ECOFF_MAGIC_NETBSD_ALPHA:
   2254 		epp->ep_emul = &emul_netbsd;
   2255 		break;
   2256 
   2257 	default:
   2258 		return ENOEXEC;
   2259 	}
   2260 	return 0;
   2261 }
   2262 #endif
   2263 
   2264 int
   2265 alpha_pa_access(pa)
   2266 	u_long pa;
   2267 {
   2268 	int i;
   2269 
   2270 	for (i = 0; i < mem_cluster_cnt; i++) {
   2271 		if (pa < mem_clusters[i].start)
   2272 			continue;
   2273 		if ((pa - mem_clusters[i].start) >=
   2274 		    (mem_clusters[i].size & ~PAGE_MASK))
   2275 			continue;
   2276 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
   2277 	}
   2278 	return (PROT_NONE);
   2279 }
   2280 
   2281 /* XXX XXX BEGIN XXX XXX */
   2282 paddr_t alpha_XXX_dmamap_or;					/* XXX */
   2283 								/* XXX */
   2284 paddr_t								/* XXX */
   2285 alpha_XXX_dmamap(v)						/* XXX */
   2286 	vaddr_t v;						/* XXX */
   2287 {								/* XXX */
   2288 								/* XXX */
   2289 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
   2290 }								/* XXX */
   2291 /* XXX XXX END XXX XXX */
   2292 
   2293 struct mchkinfo *
   2294 cpu_mchkinfo()
   2295 {
   2296 	if (mchkinfo_all_cpus == NULL)
   2297 		return &startup_info;
   2298 	return mchkinfo_all_cpus + alpha_pal_whami();
   2299 }
   2300