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