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