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