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