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