Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.293
      1 /* $NetBSD: machdep.c,v 1.293 2007/02/09 21:55:01 ad Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000 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_kgdb.h"
     69 #include "opt_multiprocessor.h"
     70 #include "opt_dec_3000_300.h"
     71 #include "opt_dec_3000_500.h"
     72 #include "opt_compat_osf1.h"
     73 #include "opt_compat_netbsd.h"
     74 #include "opt_execfmt.h"
     75 
     76 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     77 
     78 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.293 2007/02/09 21:55:01 ad Exp $");
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/signalvar.h>
     83 #include <sys/kernel.h>
     84 #include <sys/proc.h>
     85 #include <sys/ras.h>
     86 #include <sys/sched.h>
     87 #include <sys/buf.h>
     88 #include <sys/reboot.h>
     89 #include <sys/device.h>
     90 #include <sys/file.h>
     91 #include <sys/malloc.h>
     92 #include <sys/mbuf.h>
     93 #include <sys/mman.h>
     94 #include <sys/msgbuf.h>
     95 #include <sys/ioctl.h>
     96 #include <sys/tty.h>
     97 #include <sys/user.h>
     98 #include <sys/exec.h>
     99 #include <sys/exec_ecoff.h>
    100 #include <sys/core.h>
    101 #include <sys/kcore.h>
    102 #include <sys/ucontext.h>
    103 #include <sys/conf.h>
    104 #include <sys/ksyms.h>
    105 #include <sys/kauth.h>
    106 #include <machine/kcore.h>
    107 #include <machine/fpu.h>
    108 
    109 #include <sys/mount.h>
    110 #include <sys/syscallargs.h>
    111 
    112 #include <uvm/uvm_extern.h>
    113 #include <sys/sysctl.h>
    114 
    115 #include <dev/cons.h>
    116 
    117 #include <machine/autoconf.h>
    118 #include <machine/cpu.h>
    119 #include <machine/reg.h>
    120 #include <machine/rpb.h>
    121 #include <machine/prom.h>
    122 #include <machine/cpuconf.h>
    123 #include <machine/ieeefp.h>
    124 
    125 #ifdef DDB
    126 #include <machine/db_machdep.h>
    127 #include <ddb/db_access.h>
    128 #include <ddb/db_sym.h>
    129 #include <ddb/db_extern.h>
    130 #include <ddb/db_interface.h>
    131 #endif
    132 
    133 #ifdef KGDB
    134 #include <sys/kgdb.h>
    135 #endif
    136 
    137 #ifdef DEBUG
    138 #include <machine/sigdebug.h>
    139 #endif
    140 
    141 #include <machine/alpha.h>
    142 
    143 #include "ksyms.h"
    144 
    145 struct vm_map *exec_map = NULL;
    146 struct vm_map *mb_map = NULL;
    147 struct vm_map *phys_map = NULL;
    148 
    149 caddr_t msgbufaddr;
    150 
    151 int	maxmem;			/* max memory per process */
    152 
    153 int	totalphysmem;		/* total amount of physical memory in system */
    154 int	physmem;		/* physical memory used by NetBSD + some rsvd */
    155 int	resvmem;		/* amount of memory reserved for PROM */
    156 int	unusedmem;		/* amount of memory for OS that we don't use */
    157 int	unknownmem;		/* amount of memory with an unknown use */
    158 
    159 int	cputype;		/* system type, from the RPB */
    160 
    161 int	bootdev_debug = 0;	/* patchable, or from DDB */
    162 
    163 /*
    164  * XXX We need an address to which we can assign things so that they
    165  * won't be optimized away because we didn't use the value.
    166  */
    167 u_int32_t no_optimize;
    168 
    169 /* the following is used externally (sysctl_hw) */
    170 char	machine[] = MACHINE;		/* from <machine/param.h> */
    171 char	machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    172 char	cpu_model[128];
    173 
    174 struct	user *proc0paddr;
    175 
    176 /* Number of machine cycles per microsecond */
    177 u_int64_t	cycles_per_usec;
    178 
    179 /* number of CPUs in the box.  really! */
    180 int		ncpus;
    181 
    182 struct bootinfo_kernel bootinfo;
    183 
    184 /* For built-in TCDS */
    185 #if defined(DEC_3000_300) || defined(DEC_3000_500)
    186 u_int8_t	dec_3000_scsiid[2], dec_3000_scsifast[2];
    187 #endif
    188 
    189 struct platform platform;
    190 
    191 #if NKSYMS || defined(DDB) || defined(LKM)
    192 /* start and end of kernel symbol table */
    193 void	*ksym_start, *ksym_end;
    194 #endif
    195 
    196 /* for cpu_sysctl() */
    197 int	alpha_unaligned_print = 1;	/* warn about unaligned accesses */
    198 int	alpha_unaligned_fix = 1;	/* fix up unaligned accesses */
    199 int	alpha_unaligned_sigbus = 0;	/* don't SIGBUS on fixed-up accesses */
    200 int	alpha_fp_sync_complete = 0;	/* fp fixup if sync even without /s */
    201 
    202 /*
    203  * XXX This should be dynamically sized, but we have the chicken-egg problem!
    204  * XXX it should also be larger than it is, because not all of the mddt
    205  * XXX clusters end up being used for VM.
    206  */
    207 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];	/* low size bits overloaded */
    208 int	mem_cluster_cnt;
    209 
    210 int	cpu_dump __P((void));
    211 int	cpu_dumpsize __P((void));
    212 u_long	cpu_dump_mempagecnt __P((void));
    213 void	dumpsys __P((void));
    214 void	identifycpu __P((void));
    215 void	printregs __P((struct reg *));
    216 
    217 void
    218 alpha_init(pfn, ptb, bim, bip, biv)
    219 	u_long pfn;		/* first free PFN number */
    220 	u_long ptb;		/* PFN of current level 1 page table */
    221 	u_long bim;		/* bootinfo magic */
    222 	u_long bip;		/* bootinfo pointer */
    223 	u_long biv;		/* bootinfo version */
    224 {
    225 	extern char kernel_text[], _end[];
    226 	struct mddt *mddtp;
    227 	struct mddt_cluster *memc;
    228 	int i, mddtweird;
    229 	struct vm_physseg *vps;
    230 	vaddr_t kernstart, kernend;
    231 	paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
    232 	cpuid_t cpu_id;
    233 	struct cpu_info *ci;
    234 	char *p;
    235 	const char *bootinfo_msg;
    236 	const struct cpuinit *c;
    237 
    238 	/* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
    239 
    240 	/*
    241 	 * Turn off interrupts (not mchecks) and floating point.
    242 	 * Make sure the instruction and data streams are consistent.
    243 	 */
    244 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
    245 	alpha_pal_wrfen(0);
    246 	ALPHA_TBIA();
    247 	alpha_pal_imb();
    248 
    249 	/* Initialize the SCB. */
    250 	scb_init();
    251 
    252 	cpu_id = cpu_number();
    253 
    254 #if defined(MULTIPROCESSOR)
    255 	/*
    256 	 * Set our SysValue to the address of our cpu_info structure.
    257 	 * Secondary processors do this in their spinup trampoline.
    258 	 */
    259 	alpha_pal_wrval((u_long)&cpu_info_primary);
    260 	cpu_info[cpu_id] = &cpu_info_primary;
    261 #endif
    262 
    263 	ci = curcpu();
    264 	ci->ci_cpuid = cpu_id;
    265 
    266 	/*
    267 	 * Get critical system information (if possible, from the
    268 	 * information provided by the boot program).
    269 	 */
    270 	bootinfo_msg = NULL;
    271 	if (bim == BOOTINFO_MAGIC) {
    272 		if (biv == 0) {		/* backward compat */
    273 			biv = *(u_long *)bip;
    274 			bip += 8;
    275 		}
    276 		switch (biv) {
    277 		case 1: {
    278 			struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
    279 
    280 			bootinfo.ssym = v1p->ssym;
    281 			bootinfo.esym = v1p->esym;
    282 			/* hwrpb may not be provided by boot block in v1 */
    283 			if (v1p->hwrpb != NULL) {
    284 				bootinfo.hwrpb_phys =
    285 				    ((struct rpb *)v1p->hwrpb)->rpb_phys;
    286 				bootinfo.hwrpb_size = v1p->hwrpbsize;
    287 			} else {
    288 				bootinfo.hwrpb_phys =
    289 				    ((struct rpb *)HWRPB_ADDR)->rpb_phys;
    290 				bootinfo.hwrpb_size =
    291 				    ((struct rpb *)HWRPB_ADDR)->rpb_size;
    292 			}
    293 			memcpy(bootinfo.boot_flags, v1p->boot_flags,
    294 			    min(sizeof v1p->boot_flags,
    295 			      sizeof bootinfo.boot_flags));
    296 			memcpy(bootinfo.booted_kernel, v1p->booted_kernel,
    297 			    min(sizeof v1p->booted_kernel,
    298 			      sizeof bootinfo.booted_kernel));
    299 			/* booted dev not provided in bootinfo */
    300 			init_prom_interface((struct rpb *)
    301 			    ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
    302                 	prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
    303 			    sizeof bootinfo.booted_dev);
    304 			break;
    305 		}
    306 		default:
    307 			bootinfo_msg = "unknown bootinfo version";
    308 			goto nobootinfo;
    309 		}
    310 	} else {
    311 		bootinfo_msg = "boot program did not pass bootinfo";
    312 nobootinfo:
    313 		bootinfo.ssym = (u_long)_end;
    314 		bootinfo.esym = (u_long)_end;
    315 		bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
    316 		bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
    317 		init_prom_interface((struct rpb *)HWRPB_ADDR);
    318 		prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
    319 		    sizeof bootinfo.boot_flags);
    320 		prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
    321 		    sizeof bootinfo.booted_kernel);
    322 		prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
    323 		    sizeof bootinfo.booted_dev);
    324 	}
    325 
    326 	/*
    327 	 * Initialize the kernel's mapping of the RPB.  It's needed for
    328 	 * lots of things.
    329 	 */
    330 	hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
    331 
    332 #if defined(DEC_3000_300) || defined(DEC_3000_500)
    333 	if (hwrpb->rpb_type == ST_DEC_3000_300 ||
    334 	    hwrpb->rpb_type == ST_DEC_3000_500) {
    335 		prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
    336 		    sizeof(dec_3000_scsiid));
    337 		prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
    338 		    sizeof(dec_3000_scsifast));
    339 	}
    340 #endif
    341 
    342 	/*
    343 	 * Remember how many cycles there are per microsecond,
    344 	 * so that we can use delay().  Round up, for safety.
    345 	 */
    346 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
    347 
    348 	/*
    349 	 * Initialize the (temporary) bootstrap console interface, so
    350 	 * we can use printf until the VM system starts being setup.
    351 	 * The real console is initialized before then.
    352 	 */
    353 	init_bootstrap_console();
    354 
    355 	/* OUTPUT NOW ALLOWED */
    356 
    357 	/* delayed from above */
    358 	if (bootinfo_msg)
    359 		printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
    360 		    bootinfo_msg, bim, bip, biv);
    361 
    362 	/* Initialize the trap vectors on the primary processor. */
    363 	trap_init();
    364 
    365 	/*
    366 	 * Find out this system's page size, and initialize
    367 	 * PAGE_SIZE-dependent variables.
    368 	 */
    369 	if (hwrpb->rpb_page_size != ALPHA_PGBYTES)
    370 		panic("page size %lu != %d?!", hwrpb->rpb_page_size,
    371 		    ALPHA_PGBYTES);
    372 	uvmexp.pagesize = hwrpb->rpb_page_size;
    373 	uvm_setpagesize();
    374 
    375 	/*
    376 	 * Find out what hardware we're on, and do basic initialization.
    377 	 */
    378 	cputype = hwrpb->rpb_type;
    379 	if (cputype < 0) {
    380 		/*
    381 		 * At least some white-box systems have SRM which
    382 		 * reports a systype that's the negative of their
    383 		 * blue-box counterpart.
    384 		 */
    385 		cputype = -cputype;
    386 	}
    387 	c = platform_lookup(cputype);
    388 	if (c == NULL) {
    389 		platform_not_supported();
    390 		/* NOTREACHED */
    391 	}
    392 	(*c->init)();
    393 	strcpy(cpu_model, platform.model);
    394 
    395 	/*
    396 	 * Initialize the real console, so that the bootstrap console is
    397 	 * no longer necessary.
    398 	 */
    399 	(*platform.cons_init)();
    400 
    401 #ifdef DIAGNOSTIC
    402 	/* Paranoid sanity checking */
    403 
    404 	/* We should always be running on the primary. */
    405 	assert(hwrpb->rpb_primary_cpu_id == cpu_id);
    406 
    407 	/*
    408 	 * On single-CPU systypes, the primary should always be CPU 0,
    409 	 * except on Alpha 8200 systems where the CPU id is related
    410 	 * to the VID, which is related to the Turbo Laser node id.
    411 	 */
    412 	if (cputype != ST_DEC_21000)
    413 		assert(hwrpb->rpb_primary_cpu_id == 0);
    414 #endif
    415 
    416 	/* NO MORE FIRMWARE ACCESS ALLOWED */
    417 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    418 	/*
    419 	 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
    420 	 * XXX pmap_uses_prom_console() evaluates to non-zero.)
    421 	 */
    422 #endif
    423 
    424 	/*
    425 	 * Find the beginning and end of the kernel (and leave a
    426 	 * bit of space before the beginning for the bootstrap
    427 	 * stack).
    428 	 */
    429 	kernstart = trunc_page((vaddr_t)kernel_text) - 2 * PAGE_SIZE;
    430 #if NKSYMS || defined(DDB) || defined(LKM)
    431 	ksym_start = (void *)bootinfo.ssym;
    432 	ksym_end   = (void *)bootinfo.esym;
    433 	kernend = (vaddr_t)round_page((vaddr_t)ksym_end);
    434 #else
    435 	kernend = (vaddr_t)round_page((vaddr_t)_end);
    436 #endif
    437 
    438 	kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
    439 	kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
    440 
    441 	/*
    442 	 * Find out how much memory is available, by looking at
    443 	 * the memory cluster descriptors.  This also tries to do
    444 	 * its best to detect things things that have never been seen
    445 	 * before...
    446 	 */
    447 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
    448 
    449 	/* MDDT SANITY CHECKING */
    450 	mddtweird = 0;
    451 	if (mddtp->mddt_cluster_cnt < 2) {
    452 		mddtweird = 1;
    453 		printf("WARNING: weird number of mem clusters: %lu\n",
    454 		    mddtp->mddt_cluster_cnt);
    455 	}
    456 
    457 #if 0
    458 	printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
    459 #endif
    460 
    461 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    462 		memc = &mddtp->mddt_clusters[i];
    463 #if 0
    464 		printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
    465 		    memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
    466 #endif
    467 		totalphysmem += memc->mddt_pg_cnt;
    468 		if (mem_cluster_cnt < VM_PHYSSEG_MAX) {	/* XXX */
    469 			mem_clusters[mem_cluster_cnt].start =
    470 			    ptoa(memc->mddt_pfn);
    471 			mem_clusters[mem_cluster_cnt].size =
    472 			    ptoa(memc->mddt_pg_cnt);
    473 			if (memc->mddt_usage & MDDT_mbz ||
    474 			    memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
    475 			    memc->mddt_usage & MDDT_PALCODE)
    476 				mem_clusters[mem_cluster_cnt].size |=
    477 				    PROT_READ;
    478 			else
    479 				mem_clusters[mem_cluster_cnt].size |=
    480 				    PROT_READ | PROT_WRITE | PROT_EXEC;
    481 			mem_cluster_cnt++;
    482 		}
    483 
    484 		if (memc->mddt_usage & MDDT_mbz) {
    485 			mddtweird = 1;
    486 			printf("WARNING: mem cluster %d has weird "
    487 			    "usage 0x%lx\n", i, memc->mddt_usage);
    488 			unknownmem += memc->mddt_pg_cnt;
    489 			continue;
    490 		}
    491 		if (memc->mddt_usage & MDDT_NONVOLATILE) {
    492 			/* XXX should handle these... */
    493 			printf("WARNING: skipping non-volatile mem "
    494 			    "cluster %d\n", i);
    495 			unusedmem += memc->mddt_pg_cnt;
    496 			continue;
    497 		}
    498 		if (memc->mddt_usage & MDDT_PALCODE) {
    499 			resvmem += memc->mddt_pg_cnt;
    500 			continue;
    501 		}
    502 
    503 		/*
    504 		 * We have a memory cluster available for system
    505 		 * software use.  We must determine if this cluster
    506 		 * holds the kernel.
    507 		 */
    508 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    509 		/*
    510 		 * XXX If the kernel uses the PROM console, we only use the
    511 		 * XXX memory after the kernel in the first system segment,
    512 		 * XXX to avoid clobbering prom mapping, data, etc.
    513 		 */
    514 	    if (!pmap_uses_prom_console() || physmem == 0) {
    515 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    516 		physmem += memc->mddt_pg_cnt;
    517 		pfn0 = memc->mddt_pfn;
    518 		pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
    519 		if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
    520 			/*
    521 			 * Must compute the location of the kernel
    522 			 * within the segment.
    523 			 */
    524 #if 0
    525 			printf("Cluster %d contains kernel\n", i);
    526 #endif
    527 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    528 		    if (!pmap_uses_prom_console()) {
    529 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    530 			if (pfn0 < kernstartpfn) {
    531 				/*
    532 				 * There is a chunk before the kernel.
    533 				 */
    534 #if 0
    535 				printf("Loading chunk before kernel: "
    536 				    "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
    537 #endif
    538 				uvm_page_physload(pfn0, kernstartpfn,
    539 				    pfn0, kernstartpfn, VM_FREELIST_DEFAULT);
    540 			}
    541 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    542 		    }
    543 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    544 			if (kernendpfn < pfn1) {
    545 				/*
    546 				 * There is a chunk after the kernel.
    547 				 */
    548 #if 0
    549 				printf("Loading chunk after kernel: "
    550 				    "0x%lx / 0x%lx\n", kernendpfn, pfn1);
    551 #endif
    552 				uvm_page_physload(kernendpfn, pfn1,
    553 				    kernendpfn, pfn1, VM_FREELIST_DEFAULT);
    554 			}
    555 		} else {
    556 			/*
    557 			 * Just load this cluster as one chunk.
    558 			 */
    559 #if 0
    560 			printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
    561 			    pfn0, pfn1);
    562 #endif
    563 			uvm_page_physload(pfn0, pfn1, pfn0, pfn1,
    564 			    VM_FREELIST_DEFAULT);
    565 		}
    566 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    567 	    }
    568 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    569 	}
    570 
    571 	/*
    572 	 * Dump out the MDDT if it looks odd...
    573 	 */
    574 	if (mddtweird) {
    575 		printf("\n");
    576 		printf("complete memory cluster information:\n");
    577 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    578 			printf("mddt %d:\n", i);
    579 			printf("\tpfn %lx\n",
    580 			    mddtp->mddt_clusters[i].mddt_pfn);
    581 			printf("\tcnt %lx\n",
    582 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
    583 			printf("\ttest %lx\n",
    584 			    mddtp->mddt_clusters[i].mddt_pg_test);
    585 			printf("\tbva %lx\n",
    586 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
    587 			printf("\tbpa %lx\n",
    588 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
    589 			printf("\tbcksum %lx\n",
    590 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
    591 			printf("\tusage %lx\n",
    592 			    mddtp->mddt_clusters[i].mddt_usage);
    593 		}
    594 		printf("\n");
    595 	}
    596 
    597 	if (totalphysmem == 0)
    598 		panic("can't happen: system seems to have no memory!");
    599 	maxmem = physmem;
    600 #if 0
    601 	printf("totalphysmem = %d\n", totalphysmem);
    602 	printf("physmem = %d\n", physmem);
    603 	printf("resvmem = %d\n", resvmem);
    604 	printf("unusedmem = %d\n", unusedmem);
    605 	printf("unknownmem = %d\n", unknownmem);
    606 #endif
    607 
    608 	/*
    609 	 * Initialize error message buffer (at end of core).
    610 	 */
    611 	{
    612 		vsize_t sz = (vsize_t)round_page(MSGBUFSIZE);
    613 		vsize_t reqsz = sz;
    614 
    615 		vps = &vm_physmem[vm_nphysseg - 1];
    616 
    617 		/* shrink so that it'll fit in the last segment */
    618 		if ((vps->avail_end - vps->avail_start) < atop(sz))
    619 			sz = ptoa(vps->avail_end - vps->avail_start);
    620 
    621 		vps->end -= atop(sz);
    622 		vps->avail_end -= atop(sz);
    623 		msgbufaddr = (caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end));
    624 		initmsgbuf(msgbufaddr, sz);
    625 
    626 		/* Remove the last segment if it now has no pages. */
    627 		if (vps->start == vps->end)
    628 			vm_nphysseg--;
    629 
    630 		/* warn if the message buffer had to be shrunk */
    631 		if (sz != reqsz)
    632 			printf("WARNING: %ld bytes not available for msgbuf "
    633 			    "in last cluster (%ld used)\n", reqsz, sz);
    634 
    635 	}
    636 
    637 	/*
    638 	 * NOTE: It is safe to use uvm_pageboot_alloc() before
    639 	 * pmap_bootstrap() because our pmap_virtual_space()
    640 	 * returns compile-time constants.
    641 	 */
    642 
    643 	/*
    644 	 * Init mapping for u page(s) for proc 0
    645 	 */
    646 	lwp0.l_addr = proc0paddr =
    647 	    (struct user *)uvm_pageboot_alloc(UPAGES * PAGE_SIZE);
    648 
    649 	/*
    650 	 * Initialize the virtual memory system, and set the
    651 	 * page table base register in proc 0's PCB.
    652 	 */
    653 	pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
    654 	    hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
    655 
    656 	/*
    657 	 * Initialize the rest of proc 0's PCB, and cache its physical
    658 	 * address.
    659 	 */
    660 	lwp0.l_md.md_pcbpaddr =
    661 	    (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
    662 
    663 	/*
    664 	 * Set the kernel sp, reserving space for an (empty) trapframe,
    665 	 * and make proc0's trapframe pointer point to it for sanity.
    666 	 */
    667 	proc0paddr->u_pcb.pcb_hw.apcb_ksp =
    668 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
    669 	lwp0.l_md.md_tf =
    670 	    (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
    671 	simple_lock_init(&proc0paddr->u_pcb.pcb_fpcpu_slock);
    672 
    673 	/*
    674 	 * Initialize the primary CPU's idle PCB to proc0's.  In a
    675 	 * MULTIPROCESSOR configuration, each CPU will later get
    676 	 * its own idle PCB when autoconfiguration runs.
    677 	 */
    678 	ci->ci_idle_pcb = &proc0paddr->u_pcb;
    679 	ci->ci_idle_pcb_paddr = (u_long)lwp0.l_md.md_pcbpaddr;
    680 
    681 	/* Indicate that proc0 has a CPU. */
    682 	lwp0.l_cpu = ci;
    683 
    684 	/*
    685 	 * Look at arguments passed to us and compute boothowto.
    686 	 */
    687 
    688 	boothowto = RB_SINGLE;
    689 #ifdef KADB
    690 	boothowto |= RB_KDB;
    691 #endif
    692 	for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
    693 		/*
    694 		 * Note that we'd really like to differentiate case here,
    695 		 * but the Alpha AXP Architecture Reference Manual
    696 		 * says that we shouldn't.
    697 		 */
    698 		switch (*p) {
    699 		case 'a': /* autoboot */
    700 		case 'A':
    701 			boothowto &= ~RB_SINGLE;
    702 			break;
    703 
    704 #ifdef DEBUG
    705 		case 'c': /* crash dump immediately after autoconfig */
    706 		case 'C':
    707 			boothowto |= RB_DUMP;
    708 			break;
    709 #endif
    710 
    711 #if defined(KGDB) || defined(DDB)
    712 		case 'd': /* break into the kernel debugger ASAP */
    713 		case 'D':
    714 			boothowto |= RB_KDB;
    715 			break;
    716 #endif
    717 
    718 		case 'h': /* always halt, never reboot */
    719 		case 'H':
    720 			boothowto |= RB_HALT;
    721 			break;
    722 
    723 #if 0
    724 		case 'm': /* mini root present in memory */
    725 		case 'M':
    726 			boothowto |= RB_MINIROOT;
    727 			break;
    728 #endif
    729 
    730 		case 'n': /* askname */
    731 		case 'N':
    732 			boothowto |= RB_ASKNAME;
    733 			break;
    734 
    735 		case 's': /* single-user (default, supported for sanity) */
    736 		case 'S':
    737 			boothowto |= RB_SINGLE;
    738 			break;
    739 
    740 		case 'q': /* quiet boot */
    741 		case 'Q':
    742 			boothowto |= AB_QUIET;
    743 			break;
    744 
    745 		case 'v': /* verbose boot */
    746 		case 'V':
    747 			boothowto |= AB_VERBOSE;
    748 			break;
    749 
    750 		case '-':
    751 			/*
    752 			 * Just ignore this.  It's not required, but it's
    753 			 * common for it to be passed regardless.
    754 			 */
    755 			break;
    756 
    757 		default:
    758 			printf("Unrecognized boot flag '%c'.\n", *p);
    759 			break;
    760 		}
    761 	}
    762 
    763 
    764 	/*
    765 	 * Figure out the number of CPUs in the box, from RPB fields.
    766 	 * Really.  We mean it.
    767 	 */
    768 	for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
    769 		struct pcs *pcsp;
    770 
    771 		pcsp = LOCATE_PCS(hwrpb, i);
    772 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    773 			ncpus++;
    774 	}
    775 
    776 	/*
    777 	 * Initialize debuggers, and break into them if appropriate.
    778 	 */
    779 #if NKSYMS || defined(DDB) || defined(LKM)
    780 	ksyms_init((int)((u_int64_t)ksym_end - (u_int64_t)ksym_start),
    781 	    ksym_start, ksym_end);
    782 #endif
    783 
    784 	if (boothowto & RB_KDB) {
    785 #if defined(KGDB)
    786 		kgdb_debug_init = 1;
    787 		kgdb_connect(1);
    788 #elif defined(DDB)
    789 		Debugger();
    790 #endif
    791 	}
    792 
    793 	/*
    794 	 * Figure out our clock frequency, from RPB fields.
    795 	 */
    796 	hz = hwrpb->rpb_intr_freq >> 12;
    797 	if (!(60 <= hz && hz <= 10240)) {
    798 		hz = 1024;
    799 #ifdef DIAGNOSTIC
    800 		printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
    801 			hwrpb->rpb_intr_freq, hz);
    802 #endif
    803 	}
    804 }
    805 
    806 void
    807 consinit()
    808 {
    809 
    810 	/*
    811 	 * Everything related to console initialization is done
    812 	 * in alpha_init().
    813 	 */
    814 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
    815 	printf("consinit: %susing prom console\n",
    816 	    pmap_uses_prom_console() ? "" : "not ");
    817 #endif
    818 }
    819 
    820 void
    821 cpu_startup()
    822 {
    823 	vaddr_t minaddr, maxaddr;
    824 	char pbuf[9];
    825 #if defined(DEBUG)
    826 	extern int pmapdebug;
    827 	int opmapdebug = pmapdebug;
    828 
    829 	pmapdebug = 0;
    830 #endif
    831 
    832 	/*
    833 	 * Good {morning,afternoon,evening,night}.
    834 	 */
    835 	printf("%s%s", copyright, version);
    836 	identifycpu();
    837 	format_bytes(pbuf, sizeof(pbuf), ptoa(totalphysmem));
    838 	printf("total memory = %s\n", pbuf);
    839 	format_bytes(pbuf, sizeof(pbuf), ptoa(resvmem));
    840 	printf("(%s reserved for PROM, ", pbuf);
    841 	format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
    842 	printf("%s used by NetBSD)\n", pbuf);
    843 	if (unusedmem) {
    844 		format_bytes(pbuf, sizeof(pbuf), ptoa(unusedmem));
    845 		printf("WARNING: unused memory = %s\n", pbuf);
    846 	}
    847 	if (unknownmem) {
    848 		format_bytes(pbuf, sizeof(pbuf), ptoa(unknownmem));
    849 		printf("WARNING: %s of memory with unknown purpose\n", pbuf);
    850 	}
    851 
    852 	minaddr = 0;
    853 
    854 	/*
    855 	 * Allocate a submap for exec arguments.  This map effectively
    856 	 * limits the number of processes exec'ing at any time.
    857 	 */
    858 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    859 				   16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    860 
    861 	/*
    862 	 * Allocate a submap for physio
    863 	 */
    864 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    865 				   VM_PHYS_SIZE, 0, FALSE, NULL);
    866 
    867 	/*
    868 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    869 	 * are allocated via the pool allocator, and we use K0SEG to
    870 	 * map those pages.
    871 	 */
    872 
    873 #if defined(DEBUG)
    874 	pmapdebug = opmapdebug;
    875 #endif
    876 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    877 	printf("avail memory = %s\n", pbuf);
    878 #if 0
    879 	{
    880 		extern u_long pmap_pages_stolen;
    881 
    882 		format_bytes(pbuf, sizeof(pbuf), pmap_pages_stolen * PAGE_SIZE);
    883 		printf("stolen memory for VM structures = %s\n", pbuf);
    884 	}
    885 #endif
    886 
    887 	/*
    888 	 * Set up the HWPCB so that it's safe to configure secondary
    889 	 * CPUs.
    890 	 */
    891 	hwrpb_primary_init();
    892 }
    893 
    894 /*
    895  * Retrieve the platform name from the DSR.
    896  */
    897 const char *
    898 alpha_dsr_sysname()
    899 {
    900 	struct dsrdb *dsr;
    901 	const char *sysname;
    902 
    903 	/*
    904 	 * DSR does not exist on early HWRPB versions.
    905 	 */
    906 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
    907 		return (NULL);
    908 
    909 	dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
    910 	sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
    911 	    sizeof(u_int64_t)));
    912 	return (sysname);
    913 }
    914 
    915 /*
    916  * Lookup the system specified system variation in the provided table,
    917  * returning the model string on match.
    918  */
    919 const char *
    920 alpha_variation_name(variation, avtp)
    921 	u_int64_t variation;
    922 	const struct alpha_variation_table *avtp;
    923 {
    924 	int i;
    925 
    926 	for (i = 0; avtp[i].avt_model != NULL; i++)
    927 		if (avtp[i].avt_variation == variation)
    928 			return (avtp[i].avt_model);
    929 	return (NULL);
    930 }
    931 
    932 /*
    933  * Generate a default platform name based for unknown system variations.
    934  */
    935 const char *
    936 alpha_unknown_sysname()
    937 {
    938 	static char s[128];		/* safe size */
    939 
    940 	sprintf(s, "%s family, unknown model variation 0x%lx",
    941 	    platform.family, hwrpb->rpb_variation & SV_ST_MASK);
    942 	return ((const char *)s);
    943 }
    944 
    945 void
    946 identifycpu()
    947 {
    948 	char *s;
    949 	int i;
    950 
    951 	/*
    952 	 * print out CPU identification information.
    953 	 */
    954 	printf("%s", cpu_model);
    955 	for(s = cpu_model; *s; ++s)
    956 		if(strncasecmp(s, "MHz", 3) == 0)
    957 			goto skipMHz;
    958 	printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
    959 skipMHz:
    960 	printf(", s/n ");
    961 	for (i = 0; i < 10; i++)
    962 		printf("%c", hwrpb->rpb_ssn[i]);
    963 	printf("\n");
    964 	printf("%ld byte page size, %d processor%s.\n",
    965 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
    966 #if 0
    967 	/* this isn't defined for any systems that we run on? */
    968 	printf("serial number 0x%lx 0x%lx\n",
    969 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
    970 
    971 	/* and these aren't particularly useful! */
    972 	printf("variation: 0x%lx, revision 0x%lx\n",
    973 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
    974 #endif
    975 }
    976 
    977 int	waittime = -1;
    978 struct pcb dumppcb;
    979 
    980 void
    981 cpu_reboot(howto, bootstr)
    982 	int howto;
    983 	char *bootstr;
    984 {
    985 #if defined(MULTIPROCESSOR)
    986 	u_long cpu_id = cpu_number();
    987 	u_long wait_mask = (1UL << cpu_id) |
    988 			   (1UL << hwrpb->rpb_primary_cpu_id);
    989 	int i;
    990 #endif
    991 
    992 	/* If "always halt" was specified as a boot flag, obey. */
    993 	if ((boothowto & RB_HALT) != 0)
    994 		howto |= RB_HALT;
    995 
    996 	boothowto = howto;
    997 
    998 	/* If system is cold, just halt. */
    999 	if (cold) {
   1000 		boothowto |= RB_HALT;
   1001 		goto haltsys;
   1002 	}
   1003 
   1004 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
   1005 		waittime = 0;
   1006 		vfs_shutdown();
   1007 		/*
   1008 		 * If we've been adjusting the clock, the todr
   1009 		 * will be out of synch; adjust it now.
   1010 		 */
   1011 		resettodr();
   1012 	}
   1013 
   1014 	/* Disable interrupts. */
   1015 	splhigh();
   1016 
   1017 #if defined(MULTIPROCESSOR)
   1018 	/*
   1019 	 * Halt all other CPUs.  If we're not the primary, the
   1020 	 * primary will spin, waiting for us to halt.
   1021 	 */
   1022 	alpha_broadcast_ipi(ALPHA_IPI_HALT);
   1023 
   1024 	/* Ensure any CPUs paused by DDB resume execution so they can halt */
   1025 	cpus_paused = 0;
   1026 
   1027 	for (i = 0; i < 10000; i++) {
   1028 		alpha_mb();
   1029 		if (cpus_running == wait_mask)
   1030 			break;
   1031 		delay(1000);
   1032 	}
   1033 	alpha_mb();
   1034 	if (cpus_running != wait_mask)
   1035 		printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
   1036 		    cpus_running);
   1037 #endif /* MULTIPROCESSOR */
   1038 
   1039 	/* If rebooting and a dump is requested do it. */
   1040 #if 0
   1041 	if ((boothowto & (RB_DUMP | RB_HALT)) == RB_DUMP)
   1042 #else
   1043 	if (boothowto & RB_DUMP)
   1044 #endif
   1045 		dumpsys();
   1046 
   1047 haltsys:
   1048 
   1049 	/* run any shutdown hooks */
   1050 	doshutdownhooks();
   1051 
   1052 #ifdef BOOTKEY
   1053 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
   1054 	cnpollc(1);	/* for proper keyboard command handling */
   1055 	cngetc();
   1056 	cnpollc(0);
   1057 	printf("\n");
   1058 #endif
   1059 
   1060 	/* Finally, powerdown/halt/reboot the system. */
   1061 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN &&
   1062 	    platform.powerdown != NULL) {
   1063 		(*platform.powerdown)();
   1064 		printf("WARNING: powerdown failed!\n");
   1065 	}
   1066 	printf("%s\n\n", (boothowto & RB_HALT) ? "halted." : "rebooting...");
   1067 #if defined(MULTIPROCESSOR)
   1068 	if (cpu_id != hwrpb->rpb_primary_cpu_id)
   1069 		cpu_halt();
   1070 	else
   1071 #endif
   1072 		prom_halt(boothowto & RB_HALT);
   1073 	/*NOTREACHED*/
   1074 }
   1075 
   1076 /*
   1077  * These variables are needed by /sbin/savecore
   1078  */
   1079 u_int32_t dumpmag = 0x8fca0101;	/* magic number */
   1080 int 	dumpsize = 0;		/* pages */
   1081 long	dumplo = 0; 		/* blocks */
   1082 
   1083 /*
   1084  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
   1085  */
   1086 int
   1087 cpu_dumpsize()
   1088 {
   1089 	int size;
   1090 
   1091 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
   1092 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
   1093 	if (roundup(size, dbtob(1)) != dbtob(1))
   1094 		return -1;
   1095 
   1096 	return (1);
   1097 }
   1098 
   1099 /*
   1100  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
   1101  */
   1102 u_long
   1103 cpu_dump_mempagecnt()
   1104 {
   1105 	u_long i, n;
   1106 
   1107 	n = 0;
   1108 	for (i = 0; i < mem_cluster_cnt; i++)
   1109 		n += atop(mem_clusters[i].size);
   1110 	return (n);
   1111 }
   1112 
   1113 /*
   1114  * cpu_dump: dump machine-dependent kernel core dump headers.
   1115  */
   1116 int
   1117 cpu_dump()
   1118 {
   1119 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1120 	char buf[dbtob(1)];
   1121 	kcore_seg_t *segp;
   1122 	cpu_kcore_hdr_t *cpuhdrp;
   1123 	phys_ram_seg_t *memsegp;
   1124 	const struct bdevsw *bdev;
   1125 	int i;
   1126 
   1127 	bdev = bdevsw_lookup(dumpdev);
   1128 	if (bdev == NULL)
   1129 		return (ENXIO);
   1130 	dump = bdev->d_dump;
   1131 
   1132 	memset(buf, 0, sizeof buf);
   1133 	segp = (kcore_seg_t *)buf;
   1134 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
   1135 	memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
   1136 	    ALIGN(sizeof(*cpuhdrp))];
   1137 
   1138 	/*
   1139 	 * Generate a segment header.
   1140 	 */
   1141 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
   1142 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
   1143 
   1144 	/*
   1145 	 * Add the machine-dependent header info.
   1146 	 */
   1147 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
   1148 	cpuhdrp->page_size = PAGE_SIZE;
   1149 	cpuhdrp->nmemsegs = mem_cluster_cnt;
   1150 
   1151 	/*
   1152 	 * Fill in the memory segment descriptors.
   1153 	 */
   1154 	for (i = 0; i < mem_cluster_cnt; i++) {
   1155 		memsegp[i].start = mem_clusters[i].start;
   1156 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
   1157 	}
   1158 
   1159 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
   1160 }
   1161 
   1162 /*
   1163  * This is called by main to set dumplo and dumpsize.
   1164  * Dumps always skip the first PAGE_SIZE of disk space
   1165  * in case there might be a disk label stored there.
   1166  * If there is extra space, put dump at the end to
   1167  * reduce the chance that swapping trashes it.
   1168  */
   1169 void
   1170 cpu_dumpconf()
   1171 {
   1172 	const struct bdevsw *bdev;
   1173 	int nblks, dumpblks;	/* size of dump area */
   1174 
   1175 	if (dumpdev == NODEV)
   1176 		goto bad;
   1177 	bdev = bdevsw_lookup(dumpdev);
   1178 	if (bdev == NULL) {
   1179 		dumpdev = NODEV;
   1180 		goto bad;
   1181 	}
   1182 	if (bdev->d_psize == NULL)
   1183 		goto bad;
   1184 	nblks = (*bdev->d_psize)(dumpdev);
   1185 	if (nblks <= ctod(1))
   1186 		goto bad;
   1187 
   1188 	dumpblks = cpu_dumpsize();
   1189 	if (dumpblks < 0)
   1190 		goto bad;
   1191 	dumpblks += ctod(cpu_dump_mempagecnt());
   1192 
   1193 	/* If dump won't fit (incl. room for possible label), punt. */
   1194 	if (dumpblks > (nblks - ctod(1)))
   1195 		goto bad;
   1196 
   1197 	/* Put dump at end of partition */
   1198 	dumplo = nblks - dumpblks;
   1199 
   1200 	/* dumpsize is in page units, and doesn't include headers. */
   1201 	dumpsize = cpu_dump_mempagecnt();
   1202 	return;
   1203 
   1204 bad:
   1205 	dumpsize = 0;
   1206 	return;
   1207 }
   1208 
   1209 /*
   1210  * Dump the kernel's image to the swap partition.
   1211  */
   1212 #define	BYTES_PER_DUMP	PAGE_SIZE
   1213 
   1214 void
   1215 dumpsys()
   1216 {
   1217 	const struct bdevsw *bdev;
   1218 	u_long totalbytesleft, bytes, i, n, memcl;
   1219 	u_long maddr;
   1220 	int psize;
   1221 	daddr_t blkno;
   1222 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1223 	int error;
   1224 
   1225 	/* Save registers. */
   1226 	savectx(&dumppcb);
   1227 
   1228 	if (dumpdev == NODEV)
   1229 		return;
   1230 	bdev = bdevsw_lookup(dumpdev);
   1231 	if (bdev == NULL || bdev->d_psize == NULL)
   1232 		return;
   1233 
   1234 	/*
   1235 	 * For dumps during autoconfiguration,
   1236 	 * if dump device has already configured...
   1237 	 */
   1238 	if (dumpsize == 0)
   1239 		cpu_dumpconf();
   1240 	if (dumplo <= 0) {
   1241 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
   1242 		    minor(dumpdev));
   1243 		return;
   1244 	}
   1245 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
   1246 	    minor(dumpdev), dumplo);
   1247 
   1248 	psize = (*bdev->d_psize)(dumpdev);
   1249 	printf("dump ");
   1250 	if (psize == -1) {
   1251 		printf("area unavailable\n");
   1252 		return;
   1253 	}
   1254 
   1255 	/* XXX should purge all outstanding keystrokes. */
   1256 
   1257 	if ((error = cpu_dump()) != 0)
   1258 		goto err;
   1259 
   1260 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
   1261 	blkno = dumplo + cpu_dumpsize();
   1262 	dump = bdev->d_dump;
   1263 	error = 0;
   1264 
   1265 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
   1266 		maddr = mem_clusters[memcl].start;
   1267 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
   1268 
   1269 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
   1270 
   1271 			/* Print out how many MBs we to go. */
   1272 			if ((totalbytesleft % (1024*1024)) == 0)
   1273 				printf("%ld ", totalbytesleft / (1024 * 1024));
   1274 
   1275 			/* Limit size for next transfer. */
   1276 			n = bytes - i;
   1277 			if (n > BYTES_PER_DUMP)
   1278 				n =  BYTES_PER_DUMP;
   1279 
   1280 			error = (*dump)(dumpdev, blkno,
   1281 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
   1282 			if (error)
   1283 				goto err;
   1284 			maddr += n;
   1285 			blkno += btodb(n);			/* XXX? */
   1286 
   1287 			/* XXX should look for keystrokes, to cancel. */
   1288 		}
   1289 	}
   1290 
   1291 err:
   1292 	switch (error) {
   1293 
   1294 	case ENXIO:
   1295 		printf("device bad\n");
   1296 		break;
   1297 
   1298 	case EFAULT:
   1299 		printf("device not ready\n");
   1300 		break;
   1301 
   1302 	case EINVAL:
   1303 		printf("area improper\n");
   1304 		break;
   1305 
   1306 	case EIO:
   1307 		printf("i/o error\n");
   1308 		break;
   1309 
   1310 	case EINTR:
   1311 		printf("aborted from console\n");
   1312 		break;
   1313 
   1314 	case 0:
   1315 		printf("succeeded\n");
   1316 		break;
   1317 
   1318 	default:
   1319 		printf("error %d\n", error);
   1320 		break;
   1321 	}
   1322 	printf("\n\n");
   1323 	delay(1000);
   1324 }
   1325 
   1326 void
   1327 frametoreg(framep, regp)
   1328 	const struct trapframe *framep;
   1329 	struct reg *regp;
   1330 {
   1331 
   1332 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1333 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1334 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1335 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1336 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1337 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1338 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1339 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1340 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1341 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1342 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1343 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1344 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1345 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1346 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1347 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1348 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1349 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1350 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1351 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1352 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1353 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1354 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1355 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1356 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1357 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1358 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1359 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1360 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1361 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1362 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1363 	regp->r_regs[R_ZERO] = 0;
   1364 }
   1365 
   1366 void
   1367 regtoframe(regp, framep)
   1368 	const struct reg *regp;
   1369 	struct trapframe *framep;
   1370 {
   1371 
   1372 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1373 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1374 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1375 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1376 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1377 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1378 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1379 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1380 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1381 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1382 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1383 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1384 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1385 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1386 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1387 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1388 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1389 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1390 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1391 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1392 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1393 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1394 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1395 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1396 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1397 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1398 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1399 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1400 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1401 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1402 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1403 	/* ??? = regp->r_regs[R_ZERO]; */
   1404 }
   1405 
   1406 void
   1407 printregs(regp)
   1408 	struct reg *regp;
   1409 {
   1410 	int i;
   1411 
   1412 	for (i = 0; i < 32; i++)
   1413 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1414 		   i & 1 ? "\n" : "\t");
   1415 }
   1416 
   1417 void
   1418 regdump(framep)
   1419 	struct trapframe *framep;
   1420 {
   1421 	struct reg reg;
   1422 
   1423 	frametoreg(framep, &reg);
   1424 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1425 
   1426 	printf("REGISTERS:\n");
   1427 	printregs(&reg);
   1428 }
   1429 
   1430 
   1431 
   1432 void *
   1433 getframe(const struct lwp *l, int sig, int *onstack)
   1434 {
   1435 	void * frame;
   1436 
   1437 	/* Do we need to jump onto the signal stack? */
   1438 	*onstack =
   1439 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
   1440 	    (SIGACTION(l->l_proc, sig).sa_flags & SA_ONSTACK) != 0;
   1441 
   1442 	if (*onstack)
   1443 		frame = (void *)((caddr_t)l->l_sigstk.ss_sp +
   1444 					l->l_sigstk.ss_size);
   1445 	else
   1446 		frame = (void *)(alpha_pal_rdusp());
   1447 	return (frame);
   1448 }
   1449 
   1450 void
   1451 buildcontext(struct lwp *l, const void *catcher, const void *tramp, const void *fp)
   1452 {
   1453 	struct trapframe *tf = l->l_md.md_tf;
   1454 
   1455 	tf->tf_regs[FRAME_RA] = (u_int64_t)tramp;
   1456 	tf->tf_regs[FRAME_PC] = (u_int64_t)catcher;
   1457 	tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
   1458 	alpha_pal_wrusp((unsigned long)fp);
   1459 }
   1460 
   1461 
   1462 /*
   1463  * Send an interrupt to process, new style
   1464  */
   1465 void
   1466 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
   1467 {
   1468 	struct lwp *l = curlwp;
   1469 	struct proc *p = l->l_proc;
   1470 	struct sigacts *ps = p->p_sigacts;
   1471 	int onstack, sig = ksi->ksi_signo, error;
   1472 	struct sigframe_siginfo *fp, frame;
   1473 	struct trapframe *tf;
   1474 	sig_t catcher = SIGACTION(p, ksi->ksi_signo).sa_handler;
   1475 
   1476 	fp = (struct sigframe_siginfo *)getframe(l,ksi->ksi_signo,&onstack);
   1477 	tf = l->l_md.md_tf;
   1478 
   1479 	/* Allocate space for the signal handler context. */
   1480 	fp--;
   1481 
   1482 	/* Build stack frame for signal trampoline. */
   1483 	switch (ps->sa_sigdesc[sig].sd_vers) {
   1484 	case 0:		/* handled by sendsig_sigcontext */
   1485 	case 1:		/* handled by sendsig_sigcontext */
   1486 	default:	/* unknown version */
   1487 		printf("nsendsig: bad version %d\n",
   1488 		    ps->sa_sigdesc[sig].sd_vers);
   1489 		sigexit(l, SIGILL);
   1490 	case 2:
   1491 		break;
   1492 	}
   1493 
   1494 #ifdef DEBUG
   1495 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1496 		printf("sendsig_siginfo(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1497 		    sig, &onstack, fp);
   1498 #endif
   1499 
   1500 	/* Build stack frame for signal trampoline. */
   1501 
   1502 	frame.sf_si._info = ksi->ksi_info;
   1503 	frame.sf_uc.uc_flags = _UC_SIGMASK;
   1504 	frame.sf_uc.uc_sigmask = *mask;
   1505 	frame.sf_uc.uc_link = NULL;
   1506 	memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
   1507 	sendsig_reset(l, sig);
   1508 	mutex_exit(&p->p_smutex);
   1509 	cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
   1510 	error = copyout(&frame, fp, sizeof(frame));
   1511 	mutex_enter(&p->p_smutex);
   1512 
   1513 	if (error != 0) {
   1514 		/*
   1515 		 * Process has trashed its stack; give it an illegal
   1516 		 * instruction to halt it in its tracks.
   1517 		 */
   1518 #ifdef DEBUG
   1519 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1520 			printf("sendsig_siginfo(%d): copyout failed on sig %d\n",
   1521 			    p->p_pid, sig);
   1522 #endif
   1523 		sigexit(l, SIGILL);
   1524 		/* NOTREACHED */
   1525 	}
   1526 
   1527 #ifdef DEBUG
   1528 	if (sigdebug & SDB_FOLLOW)
   1529 		printf("sendsig_siginfo(%d): sig %d usp %p code %x\n",
   1530 		       p->p_pid, sig, fp, ksi->ksi_code);
   1531 #endif
   1532 
   1533 	/*
   1534 	 * Set up the registers to directly invoke the signal handler.  The
   1535 	 * signal trampoline is then used to return from the signal.  Note
   1536 	 * the trampoline version numbers are coordinated with machine-
   1537 	 * dependent code in libc.
   1538 	 */
   1539 
   1540 	tf->tf_regs[FRAME_A0] = sig;
   1541 	tf->tf_regs[FRAME_A1] = (u_int64_t)&fp->sf_si;
   1542 	tf->tf_regs[FRAME_A2] = (u_int64_t)&fp->sf_uc;
   1543 
   1544 	buildcontext(l,catcher,ps->sa_sigdesc[sig].sd_tramp,fp);
   1545 
   1546 	/* Remember that we're now on the signal stack. */
   1547 	if (onstack)
   1548 		l->l_sigstk.ss_flags |= SS_ONSTACK;
   1549 
   1550 #ifdef DEBUG
   1551 	if (sigdebug & SDB_FOLLOW)
   1552 		printf("sendsig_siginfo(%d): pc %lx, catcher %lx\n", p->p_pid,
   1553 		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
   1554 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1555 		printf("sendsig_siginfo(%d): sig %d returns\n",
   1556 		    p->p_pid, sig);
   1557 #endif
   1558 }
   1559 
   1560 
   1561 void
   1562 sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
   1563 {
   1564 #ifdef COMPAT_16
   1565 	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2) {
   1566 		sendsig_sigcontext(ksi, mask);
   1567 	} else {
   1568 #endif
   1569 #ifdef DEBUG
   1570 	if (sigdebug & SDB_FOLLOW)
   1571 		printf("sendsig: sendsig called: sig %d vers %d\n",
   1572 		       ksi->ksi_signo,
   1573 		       curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers);
   1574 #endif
   1575 		sendsig_siginfo(ksi, mask);
   1576 #ifdef COMPAT_16
   1577 	}
   1578 #endif
   1579 }
   1580 
   1581 /*
   1582  * machine dependent system variables.
   1583  */
   1584 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
   1585 {
   1586 
   1587 	sysctl_createv(clog, 0, NULL, NULL,
   1588 		       CTLFLAG_PERMANENT,
   1589 		       CTLTYPE_NODE, "machdep", NULL,
   1590 		       NULL, 0, NULL, 0,
   1591 		       CTL_MACHDEP, CTL_EOL);
   1592 
   1593 	sysctl_createv(clog, 0, NULL, NULL,
   1594 		       CTLFLAG_PERMANENT,
   1595 		       CTLTYPE_STRUCT, "console_device", NULL,
   1596 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
   1597 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
   1598 	sysctl_createv(clog, 0, NULL, NULL,
   1599 		       CTLFLAG_PERMANENT,
   1600 		       CTLTYPE_STRING, "root_device", NULL,
   1601 		       sysctl_root_device, 0, NULL, 0,
   1602 		       CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
   1603 	sysctl_createv(clog, 0, NULL, NULL,
   1604 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1605 		       CTLTYPE_INT, "unaligned_print", NULL,
   1606 		       NULL, 0, &alpha_unaligned_print, 0,
   1607 		       CTL_MACHDEP, CPU_UNALIGNED_PRINT, CTL_EOL);
   1608 	sysctl_createv(clog, 0, NULL, NULL,
   1609 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1610 		       CTLTYPE_INT, "unaligned_fix", NULL,
   1611 		       NULL, 0, &alpha_unaligned_fix, 0,
   1612 		       CTL_MACHDEP, CPU_UNALIGNED_FIX, CTL_EOL);
   1613 	sysctl_createv(clog, 0, NULL, NULL,
   1614 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1615 		       CTLTYPE_INT, "unaligned_sigbus", NULL,
   1616 		       NULL, 0, &alpha_unaligned_sigbus, 0,
   1617 		       CTL_MACHDEP, CPU_UNALIGNED_SIGBUS, CTL_EOL);
   1618 	sysctl_createv(clog, 0, NULL, NULL,
   1619 		       CTLFLAG_PERMANENT,
   1620 		       CTLTYPE_STRING, "booted_kernel", NULL,
   1621 		       NULL, 0, bootinfo.booted_kernel, 0,
   1622 		       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
   1623 	sysctl_createv(clog, 0, NULL, NULL,
   1624 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1625 		       CTLTYPE_INT, "fp_sync_complete", NULL,
   1626 		       NULL, 0, &alpha_fp_sync_complete, 0,
   1627 		       CTL_MACHDEP, CPU_FP_SYNC_COMPLETE, CTL_EOL);
   1628 }
   1629 
   1630 /*
   1631  * Set registers on exec.
   1632  */
   1633 void
   1634 setregs(l, pack, stack)
   1635 	register struct lwp *l;
   1636 	struct exec_package *pack;
   1637 	u_long stack;
   1638 {
   1639 	struct trapframe *tfp = l->l_md.md_tf;
   1640 #ifdef DEBUG
   1641 	int i;
   1642 #endif
   1643 
   1644 #ifdef DEBUG
   1645 	/*
   1646 	 * Crash and dump, if the user requested it.
   1647 	 */
   1648 	if (boothowto & RB_DUMP)
   1649 		panic("crash requested by boot flags");
   1650 #endif
   1651 
   1652 #ifdef DEBUG
   1653 	for (i = 0; i < FRAME_SIZE; i++)
   1654 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1655 #else
   1656 	memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   1657 #endif
   1658 	memset(&l->l_addr->u_pcb.pcb_fp, 0, sizeof l->l_addr->u_pcb.pcb_fp);
   1659 	alpha_pal_wrusp(stack);
   1660 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   1661 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   1662 
   1663 	tfp->tf_regs[FRAME_A0] = stack;			/* a0 = sp */
   1664 	tfp->tf_regs[FRAME_A1] = 0;			/* a1 = rtld cleanup */
   1665 	tfp->tf_regs[FRAME_A2] = 0;			/* a2 = rtld object */
   1666 	tfp->tf_regs[FRAME_A3] = (u_int64_t)l->l_proc->p_psstr;	/* a3 = ps_strings */
   1667 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   1668 
   1669 	l->l_md.md_flags &= ~MDP_FPUSED;
   1670 	if (__predict_true((l->l_md.md_flags & IEEE_INHERIT) == 0)) {
   1671 		l->l_md.md_flags &= ~MDP_FP_C;
   1672 		l->l_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
   1673 	}
   1674 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1675 		fpusave_proc(l, 0);
   1676 }
   1677 
   1678 /*
   1679  * Release the FPU.
   1680  */
   1681 void
   1682 fpusave_cpu(struct cpu_info *ci, int save)
   1683 {
   1684 	struct lwp *l;
   1685 #if defined(MULTIPROCESSOR)
   1686 	int s;
   1687 #endif
   1688 
   1689 	KDASSERT(ci == curcpu());
   1690 
   1691 #if defined(MULTIPROCESSOR)
   1692 	s = splhigh();		/* block IPIs for the duration */
   1693 	atomic_setbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
   1694 #endif
   1695 
   1696 	l = ci->ci_fpcurlwp;
   1697 	if (l == NULL)
   1698 		goto out;
   1699 
   1700 	if (save) {
   1701 		alpha_pal_wrfen(1);
   1702 		savefpstate(&l->l_addr->u_pcb.pcb_fp);
   1703 	}
   1704 
   1705 	alpha_pal_wrfen(0);
   1706 
   1707 	FPCPU_LOCK(&l->l_addr->u_pcb);
   1708 
   1709 	l->l_addr->u_pcb.pcb_fpcpu = NULL;
   1710 	ci->ci_fpcurlwp = NULL;
   1711 
   1712 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1713 
   1714  out:
   1715 #if defined(MULTIPROCESSOR)
   1716 	atomic_clearbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
   1717 	splx(s);
   1718 #endif
   1719 	return;
   1720 }
   1721 
   1722 /*
   1723  * Synchronize FP state for this process.
   1724  */
   1725 void
   1726 fpusave_proc(struct lwp *l, int save)
   1727 {
   1728 	struct cpu_info *ci = curcpu();
   1729 	struct cpu_info *oci;
   1730 #if defined(MULTIPROCESSOR)
   1731 	u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
   1732 	int s, spincount;
   1733 #endif
   1734 
   1735 	KDASSERT(l->l_addr != NULL);
   1736 
   1737 #if defined(MULTIPROCESSOR)
   1738 	s = splhigh();		/* block IPIs for the duration */
   1739 #endif
   1740 	FPCPU_LOCK(&l->l_addr->u_pcb);
   1741 
   1742 	oci = l->l_addr->u_pcb.pcb_fpcpu;
   1743 	if (oci == NULL) {
   1744 		FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1745 #if defined(MULTIPROCESSOR)
   1746 		splx(s);
   1747 #endif
   1748 		return;
   1749 	}
   1750 
   1751 #if defined(MULTIPROCESSOR)
   1752 	if (oci == ci) {
   1753 		KASSERT(ci->ci_fpcurlwp == l);
   1754 		FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1755 		splx(s);
   1756 		fpusave_cpu(ci, save);
   1757 		return;
   1758 	}
   1759 
   1760 	KASSERT(oci->ci_fpcurlwp == l);
   1761 	alpha_send_ipi(oci->ci_cpuid, ipi);
   1762 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1763 
   1764 	spincount = 0;
   1765 	while (l->l_addr->u_pcb.pcb_fpcpu != NULL) {
   1766 		spincount++;
   1767 		delay(1000);	/* XXX */
   1768 		if (spincount > 10000)
   1769 			panic("fpsave ipi didn't");
   1770 	}
   1771 #else
   1772 	KASSERT(ci->ci_fpcurlwp == l);
   1773 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1774 	fpusave_cpu(ci, save);
   1775 #endif /* MULTIPROCESSOR */
   1776 }
   1777 
   1778 /*
   1779  * Wait "n" microseconds.
   1780  */
   1781 void
   1782 delay(n)
   1783 	unsigned long n;
   1784 {
   1785 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
   1786 
   1787 	if (n == 0)
   1788 		return;
   1789 
   1790 	pcc0 = alpha_rpcc() & 0xffffffffUL;
   1791 	cycles = 0;
   1792 	usec = 0;
   1793 
   1794 	while (usec <= n) {
   1795 		/*
   1796 		 * Get the next CPU cycle count- assumes that we cannot
   1797 		 * have had more than one 32 bit overflow.
   1798 		 */
   1799 		pcc1 = alpha_rpcc() & 0xffffffffUL;
   1800 		if (pcc1 < pcc0)
   1801 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
   1802 		else
   1803 			curcycle = pcc1 - pcc0;
   1804 
   1805 		/*
   1806 		 * We now have the number of processor cycles since we
   1807 		 * last checked. Add the current cycle count to the
   1808 		 * running total. If it's over cycles_per_usec, increment
   1809 		 * the usec counter.
   1810 		 */
   1811 		cycles += curcycle;
   1812 		while (cycles > cycles_per_usec) {
   1813 			usec++;
   1814 			cycles -= cycles_per_usec;
   1815 		}
   1816 		pcc0 = pcc1;
   1817 	}
   1818 }
   1819 
   1820 #ifdef EXEC_ECOFF
   1821 void
   1822 cpu_exec_ecoff_setregs(l, epp, stack)
   1823 	struct lwp *l;
   1824 	struct exec_package *epp;
   1825 	u_long stack;
   1826 {
   1827 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1828 
   1829 	l->l_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   1830 }
   1831 
   1832 /*
   1833  * cpu_exec_ecoff_hook():
   1834  *	cpu-dependent ECOFF format hook for execve().
   1835  *
   1836  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   1837  *
   1838  */
   1839 int
   1840 cpu_exec_ecoff_probe(l, epp)
   1841 	struct lwp *l;
   1842 	struct exec_package *epp;
   1843 {
   1844 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1845 	int error;
   1846 
   1847 	if (execp->f.f_magic == ECOFF_MAGIC_NETBSD_ALPHA)
   1848 		error = 0;
   1849 	else
   1850 		error = ENOEXEC;
   1851 
   1852 	return (error);
   1853 }
   1854 #endif /* EXEC_ECOFF */
   1855 
   1856 int
   1857 alpha_pa_access(pa)
   1858 	u_long pa;
   1859 {
   1860 	int i;
   1861 
   1862 	for (i = 0; i < mem_cluster_cnt; i++) {
   1863 		if (pa < mem_clusters[i].start)
   1864 			continue;
   1865 		if ((pa - mem_clusters[i].start) >=
   1866 		    (mem_clusters[i].size & ~PAGE_MASK))
   1867 			continue;
   1868 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
   1869 	}
   1870 
   1871 	/*
   1872 	 * Address is not a memory address.  If we're secure, disallow
   1873 	 * access.  Otherwise, grant read/write.
   1874 	 */
   1875 	if (kauth_authorize_machdep(kauth_cred_get(),
   1876 	    KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0)
   1877 		return (PROT_NONE);
   1878 	else
   1879 		return (PROT_READ | PROT_WRITE);
   1880 }
   1881 
   1882 /* XXX XXX BEGIN XXX XXX */
   1883 paddr_t alpha_XXX_dmamap_or;					/* XXX */
   1884 								/* XXX */
   1885 paddr_t								/* XXX */
   1886 alpha_XXX_dmamap(v)						/* XXX */
   1887 	vaddr_t v;						/* XXX */
   1888 {								/* XXX */
   1889 								/* XXX */
   1890 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
   1891 }								/* XXX */
   1892 /* XXX XXX END XXX XXX */
   1893 
   1894 char *
   1895 dot_conv(x)
   1896 	unsigned long x;
   1897 {
   1898 	int i;
   1899 	char *xc;
   1900 	static int next;
   1901 	static char space[2][20];
   1902 
   1903 	xc = space[next ^= 1] + sizeof space[0];
   1904 	*--xc = '\0';
   1905 	for (i = 0;; ++i) {
   1906 		if (i && (i & 3) == 0)
   1907 			*--xc = '.';
   1908 		*--xc = hexdigits[x & 0xf];
   1909 		x >>= 4;
   1910 		if (x == 0)
   1911 			break;
   1912 	}
   1913 	return xc;
   1914 }
   1915 
   1916 void
   1917 cpu_getmcontext(l, mcp, flags)
   1918 	struct lwp *l;
   1919 	mcontext_t *mcp;
   1920 	unsigned int *flags;
   1921 {
   1922 	struct trapframe *frame = l->l_md.md_tf;
   1923 	__greg_t *gr = mcp->__gregs;
   1924 	__greg_t ras_pc;
   1925 
   1926 	/* Save register context. */
   1927 	frametoreg(frame, (struct reg *)gr);
   1928 	/* XXX if there's a better, general way to get the USP of
   1929 	 * an LWP that might or might not be curlwp, I'd like to know
   1930 	 * about it.
   1931 	 */
   1932 	if (l == curlwp) {
   1933 		gr[_REG_SP] = alpha_pal_rdusp();
   1934 		gr[_REG_UNIQUE] = alpha_pal_rdunique();
   1935 	} else {
   1936 		gr[_REG_SP] = l->l_addr->u_pcb.pcb_hw.apcb_usp;
   1937 		gr[_REG_UNIQUE] = l->l_addr->u_pcb.pcb_hw.apcb_unique;
   1938 	}
   1939 	gr[_REG_PC] = frame->tf_regs[FRAME_PC];
   1940 	gr[_REG_PS] = frame->tf_regs[FRAME_PS];
   1941 
   1942 	if ((ras_pc = (__greg_t)ras_lookup(l->l_proc,
   1943 	    (caddr_t) gr[_REG_PC])) != -1)
   1944 		gr[_REG_PC] = ras_pc;
   1945 
   1946 	*flags |= _UC_CPU | _UC_UNIQUE;
   1947 
   1948 	/* Save floating point register context, if any, and copy it. */
   1949 	if (l->l_md.md_flags & MDP_FPUSED) {
   1950 		fpusave_proc(l, 1);
   1951 		(void)memcpy(&mcp->__fpregs, &l->l_addr->u_pcb.pcb_fp,
   1952 		    sizeof (mcp->__fpregs));
   1953 		mcp->__fpregs.__fp_fpcr = alpha_read_fp_c(l);
   1954 		*flags |= _UC_FPU;
   1955 	}
   1956 }
   1957 
   1958 
   1959 int
   1960 cpu_setmcontext(l, mcp, flags)
   1961 	struct lwp *l;
   1962 	const mcontext_t *mcp;
   1963 	unsigned int flags;
   1964 {
   1965 	struct trapframe *frame = l->l_md.md_tf;
   1966 	const __greg_t *gr = mcp->__gregs;
   1967 
   1968 	/* Restore register context, if any. */
   1969 	if (flags & _UC_CPU) {
   1970 		/* Check for security violations first. */
   1971 		if ((gr[_REG_PS] & ALPHA_PSL_USERSET) != ALPHA_PSL_USERSET ||
   1972 		    (gr[_REG_PS] & ALPHA_PSL_USERCLR) != 0)
   1973 			return (EINVAL);
   1974 
   1975 		regtoframe((const struct reg *)gr, l->l_md.md_tf);
   1976 		if (l == curlwp)
   1977 			alpha_pal_wrusp(gr[_REG_SP]);
   1978 		else
   1979 			l->l_addr->u_pcb.pcb_hw.apcb_usp = gr[_REG_SP];
   1980 		frame->tf_regs[FRAME_PC] = gr[_REG_PC];
   1981 		frame->tf_regs[FRAME_PS] = gr[_REG_PS];
   1982 	}
   1983 	if (flags & _UC_UNIQUE) {
   1984 		if (l == curlwp)
   1985 			alpha_pal_wrunique(gr[_REG_UNIQUE]);
   1986 		else
   1987 			l->l_addr->u_pcb.pcb_hw.apcb_unique = gr[_REG_UNIQUE];
   1988 	}
   1989 	/* Restore floating point register context, if any. */
   1990 	if (flags & _UC_FPU) {
   1991 		/* If we have an FP register context, get rid of it. */
   1992 		if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1993 			fpusave_proc(l, 0);
   1994 		(void)memcpy(&l->l_addr->u_pcb.pcb_fp, &mcp->__fpregs,
   1995 		    sizeof (l->l_addr->u_pcb.pcb_fp));
   1996 		l->l_md.md_flags = mcp->__fpregs.__fp_fpcr & MDP_FP_C;
   1997 		l->l_md.md_flags |= MDP_FPUSED;
   1998 	}
   1999 
   2000 	return (0);
   2001 }
   2002