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