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