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