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