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