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