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