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