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