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