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