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