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