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