Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.299
      1 /* $NetBSD: machdep.c,v 1.299 2007/07/08 10:19:21 pooka 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.299 2007/07/08 10:19:21 pooka 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 	/*
    755 	 * Figure out the number of CPUs in the box, from RPB fields.
    756 	 * Really.  We mean it.
    757 	 */
    758 	for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
    759 		struct pcs *pcsp;
    760 
    761 		pcsp = LOCATE_PCS(hwrpb, i);
    762 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    763 			ncpus++;
    764 	}
    765 
    766 	/*
    767 	 * Initialize debuggers, and break into them if appropriate.
    768 	 */
    769 #if NKSYMS || defined(DDB) || defined(LKM)
    770 	ksyms_init((int)((u_int64_t)ksym_end - (u_int64_t)ksym_start),
    771 	    ksym_start, ksym_end);
    772 #endif
    773 
    774 	if (boothowto & RB_KDB) {
    775 #if defined(KGDB)
    776 		kgdb_debug_init = 1;
    777 		kgdb_connect(1);
    778 #elif defined(DDB)
    779 		Debugger();
    780 #endif
    781 	}
    782 
    783 #ifdef DIAGNOSTIC
    784 	/*
    785 	 * Check our clock frequency, from RPB fields.
    786 	 */
    787 	if ((hwrpb->rpb_intr_freq >> 12) != 1024)
    788 		printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
    789 			hwrpb->rpb_intr_freq, hz);
    790 #endif
    791 }
    792 
    793 void
    794 consinit()
    795 {
    796 
    797 	/*
    798 	 * Everything related to console initialization is done
    799 	 * in alpha_init().
    800 	 */
    801 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
    802 	printf("consinit: %susing prom console\n",
    803 	    pmap_uses_prom_console() ? "" : "not ");
    804 #endif
    805 }
    806 
    807 void
    808 cpu_startup()
    809 {
    810 	vaddr_t minaddr, maxaddr;
    811 	char pbuf[9];
    812 #if defined(DEBUG)
    813 	extern int pmapdebug;
    814 	int opmapdebug = pmapdebug;
    815 
    816 	pmapdebug = 0;
    817 #endif
    818 
    819 	/*
    820 	 * Good {morning,afternoon,evening,night}.
    821 	 */
    822 	printf("%s%s", copyright, version);
    823 	identifycpu();
    824 	format_bytes(pbuf, sizeof(pbuf), ptoa(totalphysmem));
    825 	printf("total memory = %s\n", pbuf);
    826 	format_bytes(pbuf, sizeof(pbuf), ptoa(resvmem));
    827 	printf("(%s reserved for PROM, ", pbuf);
    828 	format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
    829 	printf("%s used by NetBSD)\n", pbuf);
    830 	if (unusedmem) {
    831 		format_bytes(pbuf, sizeof(pbuf), ptoa(unusedmem));
    832 		printf("WARNING: unused memory = %s\n", pbuf);
    833 	}
    834 	if (unknownmem) {
    835 		format_bytes(pbuf, sizeof(pbuf), ptoa(unknownmem));
    836 		printf("WARNING: %s of memory with unknown purpose\n", pbuf);
    837 	}
    838 
    839 	minaddr = 0;
    840 
    841 	/*
    842 	 * Allocate a submap for exec arguments.  This map effectively
    843 	 * limits the number of processes exec'ing at any time.
    844 	 */
    845 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    846 				   16 * NCARGS, VM_MAP_PAGEABLE, false, NULL);
    847 
    848 	/*
    849 	 * Allocate a submap for physio
    850 	 */
    851 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    852 				   VM_PHYS_SIZE, 0, false, NULL);
    853 
    854 	/*
    855 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    856 	 * are allocated via the pool allocator, and we use K0SEG to
    857 	 * map those pages.
    858 	 */
    859 
    860 #if defined(DEBUG)
    861 	pmapdebug = opmapdebug;
    862 #endif
    863 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    864 	printf("avail memory = %s\n", pbuf);
    865 #if 0
    866 	{
    867 		extern u_long pmap_pages_stolen;
    868 
    869 		format_bytes(pbuf, sizeof(pbuf), pmap_pages_stolen * PAGE_SIZE);
    870 		printf("stolen memory for VM structures = %s\n", pbuf);
    871 	}
    872 #endif
    873 
    874 	/*
    875 	 * Set up the HWPCB so that it's safe to configure secondary
    876 	 * CPUs.
    877 	 */
    878 	hwrpb_primary_init();
    879 }
    880 
    881 /*
    882  * Retrieve the platform name from the DSR.
    883  */
    884 const char *
    885 alpha_dsr_sysname()
    886 {
    887 	struct dsrdb *dsr;
    888 	const char *sysname;
    889 
    890 	/*
    891 	 * DSR does not exist on early HWRPB versions.
    892 	 */
    893 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
    894 		return (NULL);
    895 
    896 	dsr = (struct dsrdb *)(((char *)hwrpb) + hwrpb->rpb_dsrdb_off);
    897 	sysname = (const char *)((char *)dsr + (dsr->dsr_sysname_off +
    898 	    sizeof(u_int64_t)));
    899 	return (sysname);
    900 }
    901 
    902 /*
    903  * Lookup the system specified system variation in the provided table,
    904  * returning the model string on match.
    905  */
    906 const char *
    907 alpha_variation_name(variation, avtp)
    908 	u_int64_t variation;
    909 	const struct alpha_variation_table *avtp;
    910 {
    911 	int i;
    912 
    913 	for (i = 0; avtp[i].avt_model != NULL; i++)
    914 		if (avtp[i].avt_variation == variation)
    915 			return (avtp[i].avt_model);
    916 	return (NULL);
    917 }
    918 
    919 /*
    920  * Generate a default platform name based for unknown system variations.
    921  */
    922 const char *
    923 alpha_unknown_sysname()
    924 {
    925 	static char s[128];		/* safe size */
    926 
    927 	sprintf(s, "%s family, unknown model variation 0x%lx",
    928 	    platform.family, hwrpb->rpb_variation & SV_ST_MASK);
    929 	return ((const char *)s);
    930 }
    931 
    932 void
    933 identifycpu()
    934 {
    935 	char *s;
    936 	int i;
    937 
    938 	/*
    939 	 * print out CPU identification information.
    940 	 */
    941 	printf("%s", cpu_model);
    942 	for(s = cpu_model; *s; ++s)
    943 		if(strncasecmp(s, "MHz", 3) == 0)
    944 			goto skipMHz;
    945 	printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
    946 skipMHz:
    947 	printf(", s/n ");
    948 	for (i = 0; i < 10; i++)
    949 		printf("%c", hwrpb->rpb_ssn[i]);
    950 	printf("\n");
    951 	printf("%ld byte page size, %d processor%s.\n",
    952 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
    953 #if 0
    954 	/* this isn't defined for any systems that we run on? */
    955 	printf("serial number 0x%lx 0x%lx\n",
    956 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
    957 
    958 	/* and these aren't particularly useful! */
    959 	printf("variation: 0x%lx, revision 0x%lx\n",
    960 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
    961 #endif
    962 }
    963 
    964 int	waittime = -1;
    965 struct pcb dumppcb;
    966 
    967 void
    968 cpu_reboot(howto, bootstr)
    969 	int howto;
    970 	char *bootstr;
    971 {
    972 #if defined(MULTIPROCESSOR)
    973 	u_long cpu_id = cpu_number();
    974 	u_long wait_mask = (1UL << cpu_id) |
    975 			   (1UL << hwrpb->rpb_primary_cpu_id);
    976 	int i;
    977 #endif
    978 
    979 	/* If "always halt" was specified as a boot flag, obey. */
    980 	if ((boothowto & RB_HALT) != 0)
    981 		howto |= RB_HALT;
    982 
    983 	boothowto = howto;
    984 
    985 	/* If system is cold, just halt. */
    986 	if (cold) {
    987 		boothowto |= RB_HALT;
    988 		goto haltsys;
    989 	}
    990 
    991 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    992 		waittime = 0;
    993 		vfs_shutdown();
    994 		/*
    995 		 * If we've been adjusting the clock, the todr
    996 		 * will be out of synch; adjust it now.
    997 		 */
    998 		resettodr();
    999 	}
   1000 
   1001 	/* Disable interrupts. */
   1002 	splhigh();
   1003 
   1004 #if defined(MULTIPROCESSOR)
   1005 	/*
   1006 	 * Halt all other CPUs.  If we're not the primary, the
   1007 	 * primary will spin, waiting for us to halt.
   1008 	 */
   1009 	alpha_broadcast_ipi(ALPHA_IPI_HALT);
   1010 
   1011 	/* Ensure any CPUs paused by DDB resume execution so they can halt */
   1012 	cpus_paused = 0;
   1013 
   1014 	for (i = 0; i < 10000; i++) {
   1015 		alpha_mb();
   1016 		if (cpus_running == wait_mask)
   1017 			break;
   1018 		delay(1000);
   1019 	}
   1020 	alpha_mb();
   1021 	if (cpus_running != wait_mask)
   1022 		printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
   1023 		    cpus_running);
   1024 #endif /* MULTIPROCESSOR */
   1025 
   1026 	/* If rebooting and a dump is requested do it. */
   1027 #if 0
   1028 	if ((boothowto & (RB_DUMP | RB_HALT)) == RB_DUMP)
   1029 #else
   1030 	if (boothowto & RB_DUMP)
   1031 #endif
   1032 		dumpsys();
   1033 
   1034 haltsys:
   1035 
   1036 	/* run any shutdown hooks */
   1037 	doshutdownhooks();
   1038 
   1039 #ifdef BOOTKEY
   1040 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
   1041 	cnpollc(1);	/* for proper keyboard command handling */
   1042 	cngetc();
   1043 	cnpollc(0);
   1044 	printf("\n");
   1045 #endif
   1046 
   1047 	/* Finally, powerdown/halt/reboot the system. */
   1048 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN &&
   1049 	    platform.powerdown != NULL) {
   1050 		(*platform.powerdown)();
   1051 		printf("WARNING: powerdown failed!\n");
   1052 	}
   1053 	printf("%s\n\n", (boothowto & RB_HALT) ? "halted." : "rebooting...");
   1054 #if defined(MULTIPROCESSOR)
   1055 	if (cpu_id != hwrpb->rpb_primary_cpu_id)
   1056 		cpu_halt();
   1057 	else
   1058 #endif
   1059 		prom_halt(boothowto & RB_HALT);
   1060 	/*NOTREACHED*/
   1061 }
   1062 
   1063 /*
   1064  * These variables are needed by /sbin/savecore
   1065  */
   1066 u_int32_t dumpmag = 0x8fca0101;	/* magic number */
   1067 int 	dumpsize = 0;		/* pages */
   1068 long	dumplo = 0; 		/* blocks */
   1069 
   1070 /*
   1071  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
   1072  */
   1073 int
   1074 cpu_dumpsize()
   1075 {
   1076 	int size;
   1077 
   1078 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
   1079 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
   1080 	if (roundup(size, dbtob(1)) != dbtob(1))
   1081 		return -1;
   1082 
   1083 	return (1);
   1084 }
   1085 
   1086 /*
   1087  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
   1088  */
   1089 u_long
   1090 cpu_dump_mempagecnt()
   1091 {
   1092 	u_long i, n;
   1093 
   1094 	n = 0;
   1095 	for (i = 0; i < mem_cluster_cnt; i++)
   1096 		n += atop(mem_clusters[i].size);
   1097 	return (n);
   1098 }
   1099 
   1100 /*
   1101  * cpu_dump: dump machine-dependent kernel core dump headers.
   1102  */
   1103 int
   1104 cpu_dump()
   1105 {
   1106 	int (*dump) __P((dev_t, daddr_t, void *, size_t));
   1107 	char buf[dbtob(1)];
   1108 	kcore_seg_t *segp;
   1109 	cpu_kcore_hdr_t *cpuhdrp;
   1110 	phys_ram_seg_t *memsegp;
   1111 	const struct bdevsw *bdev;
   1112 	int i;
   1113 
   1114 	bdev = bdevsw_lookup(dumpdev);
   1115 	if (bdev == NULL)
   1116 		return (ENXIO);
   1117 	dump = bdev->d_dump;
   1118 
   1119 	memset(buf, 0, sizeof buf);
   1120 	segp = (kcore_seg_t *)buf;
   1121 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
   1122 	memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
   1123 	    ALIGN(sizeof(*cpuhdrp))];
   1124 
   1125 	/*
   1126 	 * Generate a segment header.
   1127 	 */
   1128 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
   1129 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
   1130 
   1131 	/*
   1132 	 * Add the machine-dependent header info.
   1133 	 */
   1134 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
   1135 	cpuhdrp->page_size = PAGE_SIZE;
   1136 	cpuhdrp->nmemsegs = mem_cluster_cnt;
   1137 
   1138 	/*
   1139 	 * Fill in the memory segment descriptors.
   1140 	 */
   1141 	for (i = 0; i < mem_cluster_cnt; i++) {
   1142 		memsegp[i].start = mem_clusters[i].start;
   1143 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
   1144 	}
   1145 
   1146 	return (dump(dumpdev, dumplo, (void *)buf, dbtob(1)));
   1147 }
   1148 
   1149 /*
   1150  * This is called by main to set dumplo and dumpsize.
   1151  * Dumps always skip the first PAGE_SIZE of disk space
   1152  * in case there might be a disk label stored there.
   1153  * If there is extra space, put dump at the end to
   1154  * reduce the chance that swapping trashes it.
   1155  */
   1156 void
   1157 cpu_dumpconf()
   1158 {
   1159 	const struct bdevsw *bdev;
   1160 	int nblks, dumpblks;	/* size of dump area */
   1161 
   1162 	if (dumpdev == NODEV)
   1163 		goto bad;
   1164 	bdev = bdevsw_lookup(dumpdev);
   1165 	if (bdev == NULL) {
   1166 		dumpdev = NODEV;
   1167 		goto bad;
   1168 	}
   1169 	if (bdev->d_psize == NULL)
   1170 		goto bad;
   1171 	nblks = (*bdev->d_psize)(dumpdev);
   1172 	if (nblks <= ctod(1))
   1173 		goto bad;
   1174 
   1175 	dumpblks = cpu_dumpsize();
   1176 	if (dumpblks < 0)
   1177 		goto bad;
   1178 	dumpblks += ctod(cpu_dump_mempagecnt());
   1179 
   1180 	/* If dump won't fit (incl. room for possible label), punt. */
   1181 	if (dumpblks > (nblks - ctod(1)))
   1182 		goto bad;
   1183 
   1184 	/* Put dump at end of partition */
   1185 	dumplo = nblks - dumpblks;
   1186 
   1187 	/* dumpsize is in page units, and doesn't include headers. */
   1188 	dumpsize = cpu_dump_mempagecnt();
   1189 	return;
   1190 
   1191 bad:
   1192 	dumpsize = 0;
   1193 	return;
   1194 }
   1195 
   1196 /*
   1197  * Dump the kernel's image to the swap partition.
   1198  */
   1199 #define	BYTES_PER_DUMP	PAGE_SIZE
   1200 
   1201 void
   1202 dumpsys()
   1203 {
   1204 	const struct bdevsw *bdev;
   1205 	u_long totalbytesleft, bytes, i, n, memcl;
   1206 	u_long maddr;
   1207 	int psize;
   1208 	daddr_t blkno;
   1209 	int (*dump) __P((dev_t, daddr_t, void *, size_t));
   1210 	int error;
   1211 
   1212 	/* Save registers. */
   1213 	savectx(&dumppcb);
   1214 
   1215 	if (dumpdev == NODEV)
   1216 		return;
   1217 	bdev = bdevsw_lookup(dumpdev);
   1218 	if (bdev == NULL || bdev->d_psize == NULL)
   1219 		return;
   1220 
   1221 	/*
   1222 	 * For dumps during autoconfiguration,
   1223 	 * if dump device has already configured...
   1224 	 */
   1225 	if (dumpsize == 0)
   1226 		cpu_dumpconf();
   1227 	if (dumplo <= 0) {
   1228 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
   1229 		    minor(dumpdev));
   1230 		return;
   1231 	}
   1232 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
   1233 	    minor(dumpdev), dumplo);
   1234 
   1235 	psize = (*bdev->d_psize)(dumpdev);
   1236 	printf("dump ");
   1237 	if (psize == -1) {
   1238 		printf("area unavailable\n");
   1239 		return;
   1240 	}
   1241 
   1242 	/* XXX should purge all outstanding keystrokes. */
   1243 
   1244 	if ((error = cpu_dump()) != 0)
   1245 		goto err;
   1246 
   1247 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
   1248 	blkno = dumplo + cpu_dumpsize();
   1249 	dump = bdev->d_dump;
   1250 	error = 0;
   1251 
   1252 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
   1253 		maddr = mem_clusters[memcl].start;
   1254 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
   1255 
   1256 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
   1257 
   1258 			/* Print out how many MBs we to go. */
   1259 			if ((totalbytesleft % (1024*1024)) == 0)
   1260 				printf("%ld ", totalbytesleft / (1024 * 1024));
   1261 
   1262 			/* Limit size for next transfer. */
   1263 			n = bytes - i;
   1264 			if (n > BYTES_PER_DUMP)
   1265 				n =  BYTES_PER_DUMP;
   1266 
   1267 			error = (*dump)(dumpdev, blkno,
   1268 			    (void *)ALPHA_PHYS_TO_K0SEG(maddr), n);
   1269 			if (error)
   1270 				goto err;
   1271 			maddr += n;
   1272 			blkno += btodb(n);			/* XXX? */
   1273 
   1274 			/* XXX should look for keystrokes, to cancel. */
   1275 		}
   1276 	}
   1277 
   1278 err:
   1279 	switch (error) {
   1280 
   1281 	case ENXIO:
   1282 		printf("device bad\n");
   1283 		break;
   1284 
   1285 	case EFAULT:
   1286 		printf("device not ready\n");
   1287 		break;
   1288 
   1289 	case EINVAL:
   1290 		printf("area improper\n");
   1291 		break;
   1292 
   1293 	case EIO:
   1294 		printf("i/o error\n");
   1295 		break;
   1296 
   1297 	case EINTR:
   1298 		printf("aborted from console\n");
   1299 		break;
   1300 
   1301 	case 0:
   1302 		printf("succeeded\n");
   1303 		break;
   1304 
   1305 	default:
   1306 		printf("error %d\n", error);
   1307 		break;
   1308 	}
   1309 	printf("\n\n");
   1310 	delay(1000);
   1311 }
   1312 
   1313 void
   1314 frametoreg(framep, regp)
   1315 	const struct trapframe *framep;
   1316 	struct reg *regp;
   1317 {
   1318 
   1319 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1320 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1321 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1322 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1323 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1324 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1325 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1326 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1327 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1328 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1329 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1330 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1331 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1332 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1333 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1334 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1335 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1336 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1337 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1338 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1339 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1340 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1341 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1342 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1343 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1344 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1345 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1346 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1347 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1348 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1349 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1350 	regp->r_regs[R_ZERO] = 0;
   1351 }
   1352 
   1353 void
   1354 regtoframe(regp, framep)
   1355 	const struct reg *regp;
   1356 	struct trapframe *framep;
   1357 {
   1358 
   1359 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1360 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1361 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1362 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1363 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1364 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1365 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1366 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1367 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1368 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1369 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1370 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1371 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1372 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1373 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1374 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1375 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1376 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1377 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1378 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1379 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1380 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1381 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1382 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1383 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1384 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1385 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1386 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1387 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1388 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1389 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1390 	/* ??? = regp->r_regs[R_ZERO]; */
   1391 }
   1392 
   1393 void
   1394 printregs(regp)
   1395 	struct reg *regp;
   1396 {
   1397 	int i;
   1398 
   1399 	for (i = 0; i < 32; i++)
   1400 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1401 		   i & 1 ? "\n" : "\t");
   1402 }
   1403 
   1404 void
   1405 regdump(framep)
   1406 	struct trapframe *framep;
   1407 {
   1408 	struct reg reg;
   1409 
   1410 	frametoreg(framep, &reg);
   1411 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1412 
   1413 	printf("REGISTERS:\n");
   1414 	printregs(&reg);
   1415 }
   1416 
   1417 
   1418 
   1419 void *
   1420 getframe(const struct lwp *l, int sig, int *onstack)
   1421 {
   1422 	void *frame;
   1423 
   1424 	/* Do we need to jump onto the signal stack? */
   1425 	*onstack =
   1426 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
   1427 	    (SIGACTION(l->l_proc, sig).sa_flags & SA_ONSTACK) != 0;
   1428 
   1429 	if (*onstack)
   1430 		frame = (void *)((char *)l->l_sigstk.ss_sp +
   1431 					l->l_sigstk.ss_size);
   1432 	else
   1433 		frame = (void *)(alpha_pal_rdusp());
   1434 	return (frame);
   1435 }
   1436 
   1437 void
   1438 buildcontext(struct lwp *l, const void *catcher, const void *tramp, const void *fp)
   1439 {
   1440 	struct trapframe *tf = l->l_md.md_tf;
   1441 
   1442 	tf->tf_regs[FRAME_RA] = (u_int64_t)tramp;
   1443 	tf->tf_regs[FRAME_PC] = (u_int64_t)catcher;
   1444 	tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
   1445 	alpha_pal_wrusp((unsigned long)fp);
   1446 }
   1447 
   1448 
   1449 /*
   1450  * Send an interrupt to process, new style
   1451  */
   1452 void
   1453 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
   1454 {
   1455 	struct lwp *l = curlwp;
   1456 	struct proc *p = l->l_proc;
   1457 	struct sigacts *ps = p->p_sigacts;
   1458 	int onstack, sig = ksi->ksi_signo, error;
   1459 	struct sigframe_siginfo *fp, frame;
   1460 	struct trapframe *tf;
   1461 	sig_t catcher = SIGACTION(p, ksi->ksi_signo).sa_handler;
   1462 
   1463 	fp = (struct sigframe_siginfo *)getframe(l,ksi->ksi_signo,&onstack);
   1464 	tf = l->l_md.md_tf;
   1465 
   1466 	/* Allocate space for the signal handler context. */
   1467 	fp--;
   1468 
   1469 	/* Build stack frame for signal trampoline. */
   1470 	switch (ps->sa_sigdesc[sig].sd_vers) {
   1471 	case 0:		/* handled by sendsig_sigcontext */
   1472 	case 1:		/* handled by sendsig_sigcontext */
   1473 	default:	/* unknown version */
   1474 		printf("nsendsig: bad version %d\n",
   1475 		    ps->sa_sigdesc[sig].sd_vers);
   1476 		sigexit(l, SIGILL);
   1477 	case 2:
   1478 		break;
   1479 	}
   1480 
   1481 #ifdef DEBUG
   1482 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1483 		printf("sendsig_siginfo(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1484 		    sig, &onstack, fp);
   1485 #endif
   1486 
   1487 	/* Build stack frame for signal trampoline. */
   1488 
   1489 	frame.sf_si._info = ksi->ksi_info;
   1490 	frame.sf_uc.uc_flags = _UC_SIGMASK;
   1491 	frame.sf_uc.uc_sigmask = *mask;
   1492 	frame.sf_uc.uc_link = l->l_ctxlink;
   1493 	memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
   1494 	sendsig_reset(l, sig);
   1495 	mutex_exit(&p->p_smutex);
   1496 	cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
   1497 	error = copyout(&frame, fp, sizeof(frame));
   1498 	mutex_enter(&p->p_smutex);
   1499 
   1500 	if (error != 0) {
   1501 		/*
   1502 		 * Process has trashed its stack; give it an illegal
   1503 		 * instruction to halt it in its tracks.
   1504 		 */
   1505 #ifdef DEBUG
   1506 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1507 			printf("sendsig_siginfo(%d): copyout failed on sig %d\n",
   1508 			    p->p_pid, sig);
   1509 #endif
   1510 		sigexit(l, SIGILL);
   1511 		/* NOTREACHED */
   1512 	}
   1513 
   1514 #ifdef DEBUG
   1515 	if (sigdebug & SDB_FOLLOW)
   1516 		printf("sendsig_siginfo(%d): sig %d usp %p code %x\n",
   1517 		       p->p_pid, sig, fp, ksi->ksi_code);
   1518 #endif
   1519 
   1520 	/*
   1521 	 * Set up the registers to directly invoke the signal handler.  The
   1522 	 * signal trampoline is then used to return from the signal.  Note
   1523 	 * the trampoline version numbers are coordinated with machine-
   1524 	 * dependent code in libc.
   1525 	 */
   1526 
   1527 	tf->tf_regs[FRAME_A0] = sig;
   1528 	tf->tf_regs[FRAME_A1] = (u_int64_t)&fp->sf_si;
   1529 	tf->tf_regs[FRAME_A2] = (u_int64_t)&fp->sf_uc;
   1530 
   1531 	buildcontext(l,catcher,ps->sa_sigdesc[sig].sd_tramp,fp);
   1532 
   1533 	/* Remember that we're now on the signal stack. */
   1534 	if (onstack)
   1535 		l->l_sigstk.ss_flags |= SS_ONSTACK;
   1536 
   1537 #ifdef DEBUG
   1538 	if (sigdebug & SDB_FOLLOW)
   1539 		printf("sendsig_siginfo(%d): pc %lx, catcher %lx\n", p->p_pid,
   1540 		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
   1541 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1542 		printf("sendsig_siginfo(%d): sig %d returns\n",
   1543 		    p->p_pid, sig);
   1544 #endif
   1545 }
   1546 
   1547 
   1548 void
   1549 sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
   1550 {
   1551 #ifdef COMPAT_16
   1552 	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2) {
   1553 		sendsig_sigcontext(ksi, mask);
   1554 	} else {
   1555 #endif
   1556 #ifdef DEBUG
   1557 	if (sigdebug & SDB_FOLLOW)
   1558 		printf("sendsig: sendsig called: sig %d vers %d\n",
   1559 		       ksi->ksi_signo,
   1560 		       curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers);
   1561 #endif
   1562 		sendsig_siginfo(ksi, mask);
   1563 #ifdef COMPAT_16
   1564 	}
   1565 #endif
   1566 }
   1567 
   1568 /*
   1569  * machine dependent system variables.
   1570  */
   1571 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
   1572 {
   1573 
   1574 	sysctl_createv(clog, 0, NULL, NULL,
   1575 		       CTLFLAG_PERMANENT,
   1576 		       CTLTYPE_NODE, "machdep", NULL,
   1577 		       NULL, 0, NULL, 0,
   1578 		       CTL_MACHDEP, CTL_EOL);
   1579 
   1580 	sysctl_createv(clog, 0, NULL, NULL,
   1581 		       CTLFLAG_PERMANENT,
   1582 		       CTLTYPE_STRUCT, "console_device", NULL,
   1583 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
   1584 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
   1585 	sysctl_createv(clog, 0, NULL, NULL,
   1586 		       CTLFLAG_PERMANENT,
   1587 		       CTLTYPE_STRING, "root_device", NULL,
   1588 		       sysctl_root_device, 0, NULL, 0,
   1589 		       CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
   1590 	sysctl_createv(clog, 0, NULL, NULL,
   1591 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1592 		       CTLTYPE_INT, "unaligned_print", NULL,
   1593 		       NULL, 0, &alpha_unaligned_print, 0,
   1594 		       CTL_MACHDEP, CPU_UNALIGNED_PRINT, CTL_EOL);
   1595 	sysctl_createv(clog, 0, NULL, NULL,
   1596 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1597 		       CTLTYPE_INT, "unaligned_fix", NULL,
   1598 		       NULL, 0, &alpha_unaligned_fix, 0,
   1599 		       CTL_MACHDEP, CPU_UNALIGNED_FIX, CTL_EOL);
   1600 	sysctl_createv(clog, 0, NULL, NULL,
   1601 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1602 		       CTLTYPE_INT, "unaligned_sigbus", NULL,
   1603 		       NULL, 0, &alpha_unaligned_sigbus, 0,
   1604 		       CTL_MACHDEP, CPU_UNALIGNED_SIGBUS, CTL_EOL);
   1605 	sysctl_createv(clog, 0, NULL, NULL,
   1606 		       CTLFLAG_PERMANENT,
   1607 		       CTLTYPE_STRING, "booted_kernel", NULL,
   1608 		       NULL, 0, bootinfo.booted_kernel, 0,
   1609 		       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
   1610 	sysctl_createv(clog, 0, NULL, NULL,
   1611 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1612 		       CTLTYPE_INT, "fp_sync_complete", NULL,
   1613 		       NULL, 0, &alpha_fp_sync_complete, 0,
   1614 		       CTL_MACHDEP, CPU_FP_SYNC_COMPLETE, CTL_EOL);
   1615 }
   1616 
   1617 /*
   1618  * Set registers on exec.
   1619  */
   1620 void
   1621 setregs(l, pack, stack)
   1622 	register struct lwp *l;
   1623 	struct exec_package *pack;
   1624 	u_long stack;
   1625 {
   1626 	struct trapframe *tfp = l->l_md.md_tf;
   1627 #ifdef DEBUG
   1628 	int i;
   1629 #endif
   1630 
   1631 #ifdef DEBUG
   1632 	/*
   1633 	 * Crash and dump, if the user requested it.
   1634 	 */
   1635 	if (boothowto & RB_DUMP)
   1636 		panic("crash requested by boot flags");
   1637 #endif
   1638 
   1639 #ifdef DEBUG
   1640 	for (i = 0; i < FRAME_SIZE; i++)
   1641 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1642 #else
   1643 	memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   1644 #endif
   1645 	memset(&l->l_addr->u_pcb.pcb_fp, 0, sizeof l->l_addr->u_pcb.pcb_fp);
   1646 	alpha_pal_wrusp(stack);
   1647 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   1648 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   1649 
   1650 	tfp->tf_regs[FRAME_A0] = stack;			/* a0 = sp */
   1651 	tfp->tf_regs[FRAME_A1] = 0;			/* a1 = rtld cleanup */
   1652 	tfp->tf_regs[FRAME_A2] = 0;			/* a2 = rtld object */
   1653 	tfp->tf_regs[FRAME_A3] = (u_int64_t)l->l_proc->p_psstr;	/* a3 = ps_strings */
   1654 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   1655 
   1656 	l->l_md.md_flags &= ~MDP_FPUSED;
   1657 	if (__predict_true((l->l_md.md_flags & IEEE_INHERIT) == 0)) {
   1658 		l->l_md.md_flags &= ~MDP_FP_C;
   1659 		l->l_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
   1660 	}
   1661 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1662 		fpusave_proc(l, 0);
   1663 }
   1664 
   1665 /*
   1666  * Release the FPU.
   1667  */
   1668 void
   1669 fpusave_cpu(struct cpu_info *ci, int save)
   1670 {
   1671 	struct lwp *l;
   1672 #if defined(MULTIPROCESSOR)
   1673 	int s;
   1674 #endif
   1675 
   1676 	KDASSERT(ci == curcpu());
   1677 
   1678 #if defined(MULTIPROCESSOR)
   1679 	s = splhigh();		/* block IPIs for the duration */
   1680 	atomic_setbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
   1681 #endif
   1682 
   1683 	l = ci->ci_fpcurlwp;
   1684 	if (l == NULL)
   1685 		goto out;
   1686 
   1687 	if (save) {
   1688 		alpha_pal_wrfen(1);
   1689 		savefpstate(&l->l_addr->u_pcb.pcb_fp);
   1690 	}
   1691 
   1692 	alpha_pal_wrfen(0);
   1693 
   1694 	FPCPU_LOCK(&l->l_addr->u_pcb);
   1695 
   1696 	l->l_addr->u_pcb.pcb_fpcpu = NULL;
   1697 	ci->ci_fpcurlwp = NULL;
   1698 
   1699 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1700 
   1701  out:
   1702 #if defined(MULTIPROCESSOR)
   1703 	atomic_clearbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
   1704 	splx(s);
   1705 #endif
   1706 	return;
   1707 }
   1708 
   1709 /*
   1710  * Synchronize FP state for this process.
   1711  */
   1712 void
   1713 fpusave_proc(struct lwp *l, int save)
   1714 {
   1715 	struct cpu_info *ci = curcpu();
   1716 	struct cpu_info *oci;
   1717 #if defined(MULTIPROCESSOR)
   1718 	u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
   1719 	int s, spincount;
   1720 #endif
   1721 
   1722 	KDASSERT(l->l_addr != NULL);
   1723 
   1724 #if defined(MULTIPROCESSOR)
   1725 	s = splhigh();		/* block IPIs for the duration */
   1726 #endif
   1727 	FPCPU_LOCK(&l->l_addr->u_pcb);
   1728 
   1729 	oci = l->l_addr->u_pcb.pcb_fpcpu;
   1730 	if (oci == NULL) {
   1731 		FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1732 #if defined(MULTIPROCESSOR)
   1733 		splx(s);
   1734 #endif
   1735 		return;
   1736 	}
   1737 
   1738 #if defined(MULTIPROCESSOR)
   1739 	if (oci == ci) {
   1740 		KASSERT(ci->ci_fpcurlwp == l);
   1741 		FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1742 		splx(s);
   1743 		fpusave_cpu(ci, save);
   1744 		return;
   1745 	}
   1746 
   1747 	KASSERT(oci->ci_fpcurlwp == l);
   1748 	alpha_send_ipi(oci->ci_cpuid, ipi);
   1749 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1750 
   1751 	spincount = 0;
   1752 	while (l->l_addr->u_pcb.pcb_fpcpu != NULL) {
   1753 		spincount++;
   1754 		delay(1000);	/* XXX */
   1755 		if (spincount > 10000)
   1756 			panic("fpsave ipi didn't");
   1757 	}
   1758 #else
   1759 	KASSERT(ci->ci_fpcurlwp == l);
   1760 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1761 	fpusave_cpu(ci, save);
   1762 #endif /* MULTIPROCESSOR */
   1763 }
   1764 
   1765 /*
   1766  * Wait "n" microseconds.
   1767  */
   1768 void
   1769 delay(n)
   1770 	unsigned long n;
   1771 {
   1772 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
   1773 
   1774 	if (n == 0)
   1775 		return;
   1776 
   1777 	pcc0 = alpha_rpcc() & 0xffffffffUL;
   1778 	cycles = 0;
   1779 	usec = 0;
   1780 
   1781 	while (usec <= n) {
   1782 		/*
   1783 		 * Get the next CPU cycle count- assumes that we cannot
   1784 		 * have had more than one 32 bit overflow.
   1785 		 */
   1786 		pcc1 = alpha_rpcc() & 0xffffffffUL;
   1787 		if (pcc1 < pcc0)
   1788 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
   1789 		else
   1790 			curcycle = pcc1 - pcc0;
   1791 
   1792 		/*
   1793 		 * We now have the number of processor cycles since we
   1794 		 * last checked. Add the current cycle count to the
   1795 		 * running total. If it's over cycles_per_usec, increment
   1796 		 * the usec counter.
   1797 		 */
   1798 		cycles += curcycle;
   1799 		while (cycles > cycles_per_usec) {
   1800 			usec++;
   1801 			cycles -= cycles_per_usec;
   1802 		}
   1803 		pcc0 = pcc1;
   1804 	}
   1805 }
   1806 
   1807 #ifdef EXEC_ECOFF
   1808 void
   1809 cpu_exec_ecoff_setregs(l, epp, stack)
   1810 	struct lwp *l;
   1811 	struct exec_package *epp;
   1812 	u_long stack;
   1813 {
   1814 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1815 
   1816 	l->l_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   1817 }
   1818 
   1819 /*
   1820  * cpu_exec_ecoff_hook():
   1821  *	cpu-dependent ECOFF format hook for execve().
   1822  *
   1823  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   1824  *
   1825  */
   1826 int
   1827 cpu_exec_ecoff_probe(l, epp)
   1828 	struct lwp *l;
   1829 	struct exec_package *epp;
   1830 {
   1831 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1832 	int error;
   1833 
   1834 	if (execp->f.f_magic == ECOFF_MAGIC_NETBSD_ALPHA)
   1835 		error = 0;
   1836 	else
   1837 		error = ENOEXEC;
   1838 
   1839 	return (error);
   1840 }
   1841 #endif /* EXEC_ECOFF */
   1842 
   1843 int
   1844 alpha_pa_access(pa)
   1845 	u_long pa;
   1846 {
   1847 	int i;
   1848 
   1849 	for (i = 0; i < mem_cluster_cnt; i++) {
   1850 		if (pa < mem_clusters[i].start)
   1851 			continue;
   1852 		if ((pa - mem_clusters[i].start) >=
   1853 		    (mem_clusters[i].size & ~PAGE_MASK))
   1854 			continue;
   1855 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
   1856 	}
   1857 
   1858 	/*
   1859 	 * Address is not a memory address.  If we're secure, disallow
   1860 	 * access.  Otherwise, grant read/write.
   1861 	 */
   1862 	if (kauth_authorize_machdep(kauth_cred_get(),
   1863 	    KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0)
   1864 		return (PROT_NONE);
   1865 	else
   1866 		return (PROT_READ | PROT_WRITE);
   1867 }
   1868 
   1869 /* XXX XXX BEGIN XXX XXX */
   1870 paddr_t alpha_XXX_dmamap_or;					/* XXX */
   1871 								/* XXX */
   1872 paddr_t								/* XXX */
   1873 alpha_XXX_dmamap(v)						/* XXX */
   1874 	vaddr_t v;						/* XXX */
   1875 {								/* XXX */
   1876 								/* XXX */
   1877 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
   1878 }								/* XXX */
   1879 /* XXX XXX END XXX XXX */
   1880 
   1881 char *
   1882 dot_conv(x)
   1883 	unsigned long x;
   1884 {
   1885 	int i;
   1886 	char *xc;
   1887 	static int next;
   1888 	static char space[2][20];
   1889 
   1890 	xc = space[next ^= 1] + sizeof space[0];
   1891 	*--xc = '\0';
   1892 	for (i = 0;; ++i) {
   1893 		if (i && (i & 3) == 0)
   1894 			*--xc = '.';
   1895 		*--xc = hexdigits[x & 0xf];
   1896 		x >>= 4;
   1897 		if (x == 0)
   1898 			break;
   1899 	}
   1900 	return xc;
   1901 }
   1902 
   1903 void
   1904 cpu_getmcontext(l, mcp, flags)
   1905 	struct lwp *l;
   1906 	mcontext_t *mcp;
   1907 	unsigned int *flags;
   1908 {
   1909 	struct trapframe *frame = l->l_md.md_tf;
   1910 	__greg_t *gr = mcp->__gregs;
   1911 	__greg_t ras_pc;
   1912 
   1913 	/* Save register context. */
   1914 	frametoreg(frame, (struct reg *)gr);
   1915 	/* XXX if there's a better, general way to get the USP of
   1916 	 * an LWP that might or might not be curlwp, I'd like to know
   1917 	 * about it.
   1918 	 */
   1919 	if (l == curlwp) {
   1920 		gr[_REG_SP] = alpha_pal_rdusp();
   1921 		gr[_REG_UNIQUE] = alpha_pal_rdunique();
   1922 	} else {
   1923 		gr[_REG_SP] = l->l_addr->u_pcb.pcb_hw.apcb_usp;
   1924 		gr[_REG_UNIQUE] = l->l_addr->u_pcb.pcb_hw.apcb_unique;
   1925 	}
   1926 	gr[_REG_PC] = frame->tf_regs[FRAME_PC];
   1927 	gr[_REG_PS] = frame->tf_regs[FRAME_PS];
   1928 
   1929 	if ((ras_pc = (__greg_t)ras_lookup(l->l_proc,
   1930 	    (void *) gr[_REG_PC])) != -1)
   1931 		gr[_REG_PC] = ras_pc;
   1932 
   1933 	*flags |= _UC_CPU | _UC_UNIQUE;
   1934 
   1935 	/* Save floating point register context, if any, and copy it. */
   1936 	if (l->l_md.md_flags & MDP_FPUSED) {
   1937 		fpusave_proc(l, 1);
   1938 		(void)memcpy(&mcp->__fpregs, &l->l_addr->u_pcb.pcb_fp,
   1939 		    sizeof (mcp->__fpregs));
   1940 		mcp->__fpregs.__fp_fpcr = alpha_read_fp_c(l);
   1941 		*flags |= _UC_FPU;
   1942 	}
   1943 }
   1944 
   1945 
   1946 int
   1947 cpu_setmcontext(l, mcp, flags)
   1948 	struct lwp *l;
   1949 	const mcontext_t *mcp;
   1950 	unsigned int flags;
   1951 {
   1952 	struct trapframe *frame = l->l_md.md_tf;
   1953 	const __greg_t *gr = mcp->__gregs;
   1954 
   1955 	/* Restore register context, if any. */
   1956 	if (flags & _UC_CPU) {
   1957 		/* Check for security violations first. */
   1958 		if ((gr[_REG_PS] & ALPHA_PSL_USERSET) != ALPHA_PSL_USERSET ||
   1959 		    (gr[_REG_PS] & ALPHA_PSL_USERCLR) != 0)
   1960 			return (EINVAL);
   1961 
   1962 		regtoframe((const struct reg *)gr, l->l_md.md_tf);
   1963 		if (l == curlwp)
   1964 			alpha_pal_wrusp(gr[_REG_SP]);
   1965 		else
   1966 			l->l_addr->u_pcb.pcb_hw.apcb_usp = gr[_REG_SP];
   1967 		frame->tf_regs[FRAME_PC] = gr[_REG_PC];
   1968 		frame->tf_regs[FRAME_PS] = gr[_REG_PS];
   1969 	}
   1970 	if (flags & _UC_UNIQUE) {
   1971 		if (l == curlwp)
   1972 			alpha_pal_wrunique(gr[_REG_UNIQUE]);
   1973 		else
   1974 			l->l_addr->u_pcb.pcb_hw.apcb_unique = gr[_REG_UNIQUE];
   1975 	}
   1976 	/* Restore floating point register context, if any. */
   1977 	if (flags & _UC_FPU) {
   1978 		/* If we have an FP register context, get rid of it. */
   1979 		if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1980 			fpusave_proc(l, 0);
   1981 		(void)memcpy(&l->l_addr->u_pcb.pcb_fp, &mcp->__fpregs,
   1982 		    sizeof (l->l_addr->u_pcb.pcb_fp));
   1983 		l->l_md.md_flags = mcp->__fpregs.__fp_fpcr & MDP_FP_C;
   1984 		l->l_md.md_flags |= MDP_FPUSED;
   1985 	}
   1986 
   1987 	return (0);
   1988 }
   1989 
   1990 /*
   1991  * Preempt the current process if in interrupt from user mode,
   1992  * or after the current trap/syscall if in system mode.
   1993  */
   1994 void
   1995 cpu_need_resched(struct cpu_info *ci, int flags)
   1996 {
   1997 #if defined(MULTIPROCESSOR)
   1998 	bool immed = (flags & RESCHED_IMMED) != 0;
   1999 #endif /* defined(MULTIPROCESSOR) */
   2000 
   2001 	ci->ci_want_resched = 1;
   2002 	if (ci->ci_curlwp != ci->ci_data.cpu_idlelwp) {
   2003 		aston(ci->ci_curlwp);
   2004 #if defined(MULTIPROCESSOR)
   2005 		if (immed && ci != curcpu()) {
   2006 			alpha_send_ipi(ci->ci_cpuid, 0);
   2007 		}
   2008 #endif /* defined(MULTIPROCESSOR) */
   2009 	}
   2010 }
   2011