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