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