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