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