Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.228
      1 /* $NetBSD: machdep.c,v 1.228 2000/12/22 22:58:52 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.228 2000/12/22 22:58:52 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 	u_long cpu_id = cpu_number();
   1037 	u_long wait_mask = (1UL << cpu_id) |
   1038 			   (1UL << hwrpb->rpb_primary_cpu_id);
   1039 	int i;
   1040 #endif
   1041 
   1042 	/* If "always halt" was specified as a boot flag, obey. */
   1043 	if ((boothowto & RB_HALT) != 0)
   1044 		howto |= RB_HALT;
   1045 
   1046 	boothowto = howto;
   1047 
   1048 	/* If system is cold, just halt. */
   1049 	if (cold) {
   1050 		boothowto |= RB_HALT;
   1051 		goto haltsys;
   1052 	}
   1053 
   1054 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
   1055 		waittime = 0;
   1056 		vfs_shutdown();
   1057 		/*
   1058 		 * If we've been adjusting the clock, the todr
   1059 		 * will be out of synch; adjust it now.
   1060 		 */
   1061 		resettodr();
   1062 	}
   1063 
   1064 	/* Disable interrupts. */
   1065 	splhigh();
   1066 
   1067 #if defined(MULTIPROCESSOR)
   1068 	/*
   1069 	 * Halt all other CPUs.  If we're not the primary, the
   1070 	 * primary will spin, waiting for us to halt.
   1071 	 */
   1072 	alpha_broadcast_ipi(ALPHA_IPI_HALT);
   1073 
   1074 	for (i = 0; i < 10000; i++) {
   1075 		alpha_mb();
   1076 		if (cpus_running == wait_mask)
   1077 			break;
   1078 		delay(1000);
   1079 	}
   1080 	alpha_mb();
   1081 	if (cpus_running != wait_mask)
   1082 		printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
   1083 		    cpus_running);
   1084 #endif /* MULTIPROCESSOR */
   1085 
   1086 	/* If rebooting and a dump is requested do it. */
   1087 #if 0
   1088 	if ((boothowto & (RB_DUMP | RB_HALT)) == RB_DUMP)
   1089 #else
   1090 	if (boothowto & RB_DUMP)
   1091 #endif
   1092 		dumpsys();
   1093 
   1094 haltsys:
   1095 
   1096 	/* run any shutdown hooks */
   1097 	doshutdownhooks();
   1098 
   1099 #ifdef BOOTKEY
   1100 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
   1101 	cnpollc(1);	/* for proper keyboard command handling */
   1102 	cngetc();
   1103 	cnpollc(0);
   1104 	printf("\n");
   1105 #endif
   1106 
   1107 	/* Finally, powerdown/halt/reboot the system. */
   1108 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN &&
   1109 	    platform.powerdown != NULL) {
   1110 		(*platform.powerdown)();
   1111 		printf("WARNING: powerdown failed!\n");
   1112 	}
   1113 	printf("%s\n\n", (boothowto & RB_HALT) ? "halted." : "rebooting...");
   1114 #if defined(MULTIPROCESSOR)
   1115 	if (cpu_id != hwrpb->rpb_primary_cpu_id)
   1116 		cpu_halt();
   1117 	else
   1118 #endif
   1119 		prom_halt(boothowto & RB_HALT);
   1120 	/*NOTREACHED*/
   1121 }
   1122 
   1123 /*
   1124  * These variables are needed by /sbin/savecore
   1125  */
   1126 u_long	dumpmag = 0x8fca0101;	/* magic number */
   1127 int 	dumpsize = 0;		/* pages */
   1128 long	dumplo = 0; 		/* blocks */
   1129 
   1130 /*
   1131  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
   1132  */
   1133 int
   1134 cpu_dumpsize()
   1135 {
   1136 	int size;
   1137 
   1138 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
   1139 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
   1140 	if (roundup(size, dbtob(1)) != dbtob(1))
   1141 		return -1;
   1142 
   1143 	return (1);
   1144 }
   1145 
   1146 /*
   1147  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
   1148  */
   1149 u_long
   1150 cpu_dump_mempagecnt()
   1151 {
   1152 	u_long i, n;
   1153 
   1154 	n = 0;
   1155 	for (i = 0; i < mem_cluster_cnt; i++)
   1156 		n += atop(mem_clusters[i].size);
   1157 	return (n);
   1158 }
   1159 
   1160 /*
   1161  * cpu_dump: dump machine-dependent kernel core dump headers.
   1162  */
   1163 int
   1164 cpu_dump()
   1165 {
   1166 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1167 	char buf[dbtob(1)];
   1168 	kcore_seg_t *segp;
   1169 	cpu_kcore_hdr_t *cpuhdrp;
   1170 	phys_ram_seg_t *memsegp;
   1171 	int i;
   1172 
   1173 	dump = bdevsw[major(dumpdev)].d_dump;
   1174 
   1175 	bzero(buf, sizeof buf);
   1176 	segp = (kcore_seg_t *)buf;
   1177 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
   1178 	memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
   1179 	    ALIGN(sizeof(*cpuhdrp))];
   1180 
   1181 	/*
   1182 	 * Generate a segment header.
   1183 	 */
   1184 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
   1185 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
   1186 
   1187 	/*
   1188 	 * Add the machine-dependent header info.
   1189 	 */
   1190 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
   1191 	cpuhdrp->page_size = PAGE_SIZE;
   1192 	cpuhdrp->nmemsegs = mem_cluster_cnt;
   1193 
   1194 	/*
   1195 	 * Fill in the memory segment descriptors.
   1196 	 */
   1197 	for (i = 0; i < mem_cluster_cnt; i++) {
   1198 		memsegp[i].start = mem_clusters[i].start;
   1199 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
   1200 	}
   1201 
   1202 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
   1203 }
   1204 
   1205 /*
   1206  * This is called by main to set dumplo and dumpsize.
   1207  * Dumps always skip the first NBPG of disk space
   1208  * in case there might be a disk label stored there.
   1209  * If there is extra space, put dump at the end to
   1210  * reduce the chance that swapping trashes it.
   1211  */
   1212 void
   1213 cpu_dumpconf()
   1214 {
   1215 	int nblks, dumpblks;	/* size of dump area */
   1216 	int maj;
   1217 
   1218 	if (dumpdev == NODEV)
   1219 		goto bad;
   1220 	maj = major(dumpdev);
   1221 	if (maj < 0 || maj >= nblkdev)
   1222 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
   1223 	if (bdevsw[maj].d_psize == NULL)
   1224 		goto bad;
   1225 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
   1226 	if (nblks <= ctod(1))
   1227 		goto bad;
   1228 
   1229 	dumpblks = cpu_dumpsize();
   1230 	if (dumpblks < 0)
   1231 		goto bad;
   1232 	dumpblks += ctod(cpu_dump_mempagecnt());
   1233 
   1234 	/* If dump won't fit (incl. room for possible label), punt. */
   1235 	if (dumpblks > (nblks - ctod(1)))
   1236 		goto bad;
   1237 
   1238 	/* Put dump at end of partition */
   1239 	dumplo = nblks - dumpblks;
   1240 
   1241 	/* dumpsize is in page units, and doesn't include headers. */
   1242 	dumpsize = cpu_dump_mempagecnt();
   1243 	return;
   1244 
   1245 bad:
   1246 	dumpsize = 0;
   1247 	return;
   1248 }
   1249 
   1250 /*
   1251  * Dump the kernel's image to the swap partition.
   1252  */
   1253 #define	BYTES_PER_DUMP	NBPG
   1254 
   1255 void
   1256 dumpsys()
   1257 {
   1258 	u_long totalbytesleft, bytes, i, n, memcl;
   1259 	u_long maddr;
   1260 	int psize;
   1261 	daddr_t blkno;
   1262 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1263 	int error;
   1264 
   1265 	/* Save registers. */
   1266 	savectx(&dumppcb);
   1267 
   1268 	if (dumpdev == NODEV)
   1269 		return;
   1270 
   1271 	/*
   1272 	 * For dumps during autoconfiguration,
   1273 	 * if dump device has already configured...
   1274 	 */
   1275 	if (dumpsize == 0)
   1276 		cpu_dumpconf();
   1277 	if (dumplo <= 0) {
   1278 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
   1279 		    minor(dumpdev));
   1280 		return;
   1281 	}
   1282 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
   1283 	    minor(dumpdev), dumplo);
   1284 
   1285 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
   1286 	printf("dump ");
   1287 	if (psize == -1) {
   1288 		printf("area unavailable\n");
   1289 		return;
   1290 	}
   1291 
   1292 	/* XXX should purge all outstanding keystrokes. */
   1293 
   1294 	if ((error = cpu_dump()) != 0)
   1295 		goto err;
   1296 
   1297 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
   1298 	blkno = dumplo + cpu_dumpsize();
   1299 	dump = bdevsw[major(dumpdev)].d_dump;
   1300 	error = 0;
   1301 
   1302 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
   1303 		maddr = mem_clusters[memcl].start;
   1304 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
   1305 
   1306 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
   1307 
   1308 			/* Print out how many MBs we to go. */
   1309 			if ((totalbytesleft % (1024*1024)) == 0)
   1310 				printf("%ld ", totalbytesleft / (1024 * 1024));
   1311 
   1312 			/* Limit size for next transfer. */
   1313 			n = bytes - i;
   1314 			if (n > BYTES_PER_DUMP)
   1315 				n =  BYTES_PER_DUMP;
   1316 
   1317 			error = (*dump)(dumpdev, blkno,
   1318 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
   1319 			if (error)
   1320 				goto err;
   1321 			maddr += n;
   1322 			blkno += btodb(n);			/* XXX? */
   1323 
   1324 			/* XXX should look for keystrokes, to cancel. */
   1325 		}
   1326 	}
   1327 
   1328 err:
   1329 	switch (error) {
   1330 
   1331 	case ENXIO:
   1332 		printf("device bad\n");
   1333 		break;
   1334 
   1335 	case EFAULT:
   1336 		printf("device not ready\n");
   1337 		break;
   1338 
   1339 	case EINVAL:
   1340 		printf("area improper\n");
   1341 		break;
   1342 
   1343 	case EIO:
   1344 		printf("i/o error\n");
   1345 		break;
   1346 
   1347 	case EINTR:
   1348 		printf("aborted from console\n");
   1349 		break;
   1350 
   1351 	case 0:
   1352 		printf("succeeded\n");
   1353 		break;
   1354 
   1355 	default:
   1356 		printf("error %d\n", error);
   1357 		break;
   1358 	}
   1359 	printf("\n\n");
   1360 	delay(1000);
   1361 }
   1362 
   1363 void
   1364 frametoreg(framep, regp)
   1365 	struct trapframe *framep;
   1366 	struct reg *regp;
   1367 {
   1368 
   1369 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1370 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1371 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1372 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1373 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1374 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1375 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1376 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1377 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1378 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1379 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1380 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1381 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1382 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1383 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1384 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1385 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1386 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1387 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1388 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1389 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1390 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1391 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1392 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1393 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1394 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1395 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1396 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1397 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1398 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1399 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1400 	regp->r_regs[R_ZERO] = 0;
   1401 }
   1402 
   1403 void
   1404 regtoframe(regp, framep)
   1405 	struct reg *regp;
   1406 	struct trapframe *framep;
   1407 {
   1408 
   1409 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1410 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1411 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1412 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1413 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1414 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1415 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1416 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1417 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1418 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1419 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1420 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1421 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1422 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1423 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1424 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1425 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1426 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1427 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1428 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1429 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1430 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1431 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1432 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1433 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1434 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1435 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1436 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1437 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1438 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1439 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1440 	/* ??? = regp->r_regs[R_ZERO]; */
   1441 }
   1442 
   1443 void
   1444 printregs(regp)
   1445 	struct reg *regp;
   1446 {
   1447 	int i;
   1448 
   1449 	for (i = 0; i < 32; i++)
   1450 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1451 		   i & 1 ? "\n" : "\t");
   1452 }
   1453 
   1454 void
   1455 regdump(framep)
   1456 	struct trapframe *framep;
   1457 {
   1458 	struct reg reg;
   1459 
   1460 	frametoreg(framep, &reg);
   1461 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1462 
   1463 	printf("REGISTERS:\n");
   1464 	printregs(&reg);
   1465 }
   1466 
   1467 #ifdef DEBUG
   1468 int sigdebug = 0;
   1469 int sigpid = 0;
   1470 #define	SDB_FOLLOW	0x01
   1471 #define	SDB_KSTACK	0x02
   1472 #endif
   1473 
   1474 /*
   1475  * Send an interrupt to process.
   1476  */
   1477 void
   1478 sendsig(catcher, sig, mask, code)
   1479 	sig_t catcher;
   1480 	int sig;
   1481 	sigset_t *mask;
   1482 	u_long code;
   1483 {
   1484 	struct proc *p = curproc;
   1485 	struct sigcontext *scp, ksc;
   1486 	struct trapframe *frame;
   1487 	int onstack, fsize, rndfsize;
   1488 
   1489 	frame = p->p_md.md_tf;
   1490 
   1491 	/* Do we need to jump onto the signal stack? */
   1492 	onstack =
   1493 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
   1494 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
   1495 
   1496 	/* Allocate space for the signal handler context. */
   1497 	fsize = sizeof(ksc);
   1498 	rndfsize = ((fsize + 15) / 16) * 16;
   1499 
   1500 	if (onstack)
   1501 		scp = (struct sigcontext *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
   1502 					p->p_sigctx.ps_sigstk.ss_size);
   1503 	else
   1504 		scp = (struct sigcontext *)(alpha_pal_rdusp());
   1505 	scp = (struct sigcontext *)((caddr_t)scp - rndfsize);
   1506 
   1507 #ifdef DEBUG
   1508 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1509 		printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1510 		    sig, &onstack, scp);
   1511 #endif
   1512 
   1513 	/* Build stack frame for signal trampoline. */
   1514 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
   1515 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
   1516 
   1517 	/* Save register context. */
   1518 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1519 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1520 	ksc.sc_regs[R_SP] = alpha_pal_rdusp();
   1521 
   1522 	/* save the floating-point state, if necessary, then copy it. */
   1523 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
   1524 		fpusave_proc(p, 1);
   1525 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
   1526 	bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1527 	    sizeof(struct fpreg));
   1528 	ksc.sc_fp_control = 0;					/* XXX ? */
   1529 	bzero(ksc.sc_reserved, sizeof ksc.sc_reserved);		/* XXX */
   1530 	bzero(ksc.sc_xxx, sizeof ksc.sc_xxx);			/* XXX */
   1531 
   1532 	/* Save signal stack. */
   1533 	ksc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
   1534 
   1535 	/* Save signal mask. */
   1536 	ksc.sc_mask = *mask;
   1537 
   1538 #ifdef COMPAT_13
   1539 	/*
   1540 	 * XXX We always have to save an old style signal mask because
   1541 	 * XXX we might be delivering a signal to a process which will
   1542 	 * XXX escape from the signal in a non-standard way and invoke
   1543 	 * XXX sigreturn() directly.
   1544 	 */
   1545 	{
   1546 		/* Note: it's a long in the stack frame. */
   1547 		sigset13_t mask13;
   1548 
   1549 		native_sigset_to_sigset13(mask, &mask13);
   1550 		ksc.__sc_mask13 = mask13;
   1551 	}
   1552 #endif
   1553 
   1554 #ifdef COMPAT_OSF1
   1555 	/*
   1556 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1557 	 */
   1558 #endif
   1559 
   1560 	if (copyout(&ksc, (caddr_t)scp, fsize) != 0) {
   1561 		/*
   1562 		 * Process has trashed its stack; give it an illegal
   1563 		 * instruction to halt it in its tracks.
   1564 		 */
   1565 #ifdef DEBUG
   1566 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1567 			printf("sendsig(%d): copyout failed on sig %d\n",
   1568 			    p->p_pid, sig);
   1569 #endif
   1570 		sigexit(p, SIGILL);
   1571 		/* NOTREACHED */
   1572 	}
   1573 #ifdef DEBUG
   1574 	if (sigdebug & SDB_FOLLOW)
   1575 		printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
   1576 		    scp, code);
   1577 #endif
   1578 
   1579 	/* Set up the registers to return to sigcode. */
   1580 	frame->tf_regs[FRAME_PC] = (u_int64_t)p->p_sigctx.ps_sigcode;
   1581 	frame->tf_regs[FRAME_A0] = sig;
   1582 	frame->tf_regs[FRAME_A1] = code;
   1583 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
   1584 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
   1585 	alpha_pal_wrusp((unsigned long)scp);
   1586 
   1587 	/* Remember that we're now on the signal stack. */
   1588 	if (onstack)
   1589 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
   1590 
   1591 #ifdef DEBUG
   1592 	if (sigdebug & SDB_FOLLOW)
   1593 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1594 		    frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
   1595 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1596 		printf("sendsig(%d): sig %d returns\n",
   1597 		    p->p_pid, sig);
   1598 #endif
   1599 }
   1600 
   1601 /*
   1602  * System call to cleanup state after a signal
   1603  * has been taken.  Reset signal mask and
   1604  * stack state from context left by sendsig (above).
   1605  * Return to previous pc and psl as specified by
   1606  * context left by sendsig. Check carefully to
   1607  * make sure that the user has not modified the
   1608  * psl to gain improper privileges or to cause
   1609  * a machine fault.
   1610  */
   1611 /* ARGSUSED */
   1612 int
   1613 sys___sigreturn14(p, v, retval)
   1614 	struct proc *p;
   1615 	void *v;
   1616 	register_t *retval;
   1617 {
   1618 	struct sys___sigreturn14_args /* {
   1619 		syscallarg(struct sigcontext *) sigcntxp;
   1620 	} */ *uap = v;
   1621 	struct sigcontext *scp, ksc;
   1622 
   1623 	/*
   1624 	 * The trampoline code hands us the context.
   1625 	 * It is unsafe to keep track of it ourselves, in the event that a
   1626 	 * program jumps out of a signal handler.
   1627 	 */
   1628 	scp = SCARG(uap, sigcntxp);
   1629 #ifdef DEBUG
   1630 	if (sigdebug & SDB_FOLLOW)
   1631 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
   1632 #endif
   1633 	if (ALIGN(scp) != (u_int64_t)scp)
   1634 		return (EINVAL);
   1635 
   1636 	if (copyin((caddr_t)scp, &ksc, sizeof(ksc)) != 0)
   1637 		return (EFAULT);
   1638 
   1639 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1640 		return (EINVAL);
   1641 
   1642 	/* Restore register context. */
   1643 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
   1644 	p->p_md.md_tf->tf_regs[FRAME_PS] =
   1645 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
   1646 
   1647 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
   1648 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
   1649 
   1650 	/* XXX ksc.sc_ownedfp ? */
   1651 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
   1652 		fpusave_proc(p, 0);
   1653 	bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
   1654 	    sizeof(struct fpreg));
   1655 	/* XXX ksc.sc_fp_control ? */
   1656 
   1657 	/* Restore signal stack. */
   1658 	if (ksc.sc_onstack & SS_ONSTACK)
   1659 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
   1660 	else
   1661 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1662 
   1663 	/* Restore signal mask. */
   1664 	(void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0);
   1665 
   1666 #ifdef DEBUG
   1667 	if (sigdebug & SDB_FOLLOW)
   1668 		printf("sigreturn(%d): returns\n", p->p_pid);
   1669 #endif
   1670 	return (EJUSTRETURN);
   1671 }
   1672 
   1673 /*
   1674  * machine dependent system variables.
   1675  */
   1676 int
   1677 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1678 	int *name;
   1679 	u_int namelen;
   1680 	void *oldp;
   1681 	size_t *oldlenp;
   1682 	void *newp;
   1683 	size_t newlen;
   1684 	struct proc *p;
   1685 {
   1686 	dev_t consdev;
   1687 
   1688 	/* all sysctl names at this level are terminal */
   1689 	if (namelen != 1)
   1690 		return (ENOTDIR);		/* overloaded */
   1691 
   1692 	switch (name[0]) {
   1693 	case CPU_CONSDEV:
   1694 		if (cn_tab != NULL)
   1695 			consdev = cn_tab->cn_dev;
   1696 		else
   1697 			consdev = NODEV;
   1698 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1699 			sizeof consdev));
   1700 
   1701 	case CPU_ROOT_DEVICE:
   1702 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1703 		    root_device->dv_xname));
   1704 
   1705 	case CPU_UNALIGNED_PRINT:
   1706 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1707 		    &alpha_unaligned_print));
   1708 
   1709 	case CPU_UNALIGNED_FIX:
   1710 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1711 		    &alpha_unaligned_fix));
   1712 
   1713 	case CPU_UNALIGNED_SIGBUS:
   1714 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1715 		    &alpha_unaligned_sigbus));
   1716 
   1717 	case CPU_BOOTED_KERNEL:
   1718 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1719 		    bootinfo.booted_kernel));
   1720 
   1721 	default:
   1722 		return (EOPNOTSUPP);
   1723 	}
   1724 	/* NOTREACHED */
   1725 }
   1726 
   1727 /*
   1728  * Set registers on exec.
   1729  */
   1730 void
   1731 setregs(p, pack, stack)
   1732 	register struct proc *p;
   1733 	struct exec_package *pack;
   1734 	u_long stack;
   1735 {
   1736 	struct trapframe *tfp = p->p_md.md_tf;
   1737 #ifdef DEBUG
   1738 	int i;
   1739 #endif
   1740 
   1741 #ifdef DEBUG
   1742 	/*
   1743 	 * Crash and dump, if the user requested it.
   1744 	 */
   1745 	if (boothowto & RB_DUMP)
   1746 		panic("crash requested by boot flags");
   1747 #endif
   1748 
   1749 #ifdef DEBUG
   1750 	for (i = 0; i < FRAME_SIZE; i++)
   1751 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1752 #else
   1753 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   1754 #endif
   1755 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
   1756 	p->p_addr->u_pcb.pcb_fp.fpr_cr =  FPCR_INED
   1757 					| FPCR_UNFD
   1758 					| FPCR_UNDZ
   1759 					| FPCR_DYN(FP_RN)
   1760 					| FPCR_OVFD
   1761 					| FPCR_DZED
   1762 					| FPCR_INVD
   1763 					| FPCR_DNZ;
   1764 	alpha_pal_wrusp(stack);
   1765 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   1766 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   1767 
   1768 	tfp->tf_regs[FRAME_A0] = stack;			/* a0 = sp */
   1769 	tfp->tf_regs[FRAME_A1] = 0;			/* a1 = rtld cleanup */
   1770 	tfp->tf_regs[FRAME_A2] = 0;			/* a2 = rtld object */
   1771 	tfp->tf_regs[FRAME_A3] = (u_int64_t)PS_STRINGS;	/* a3 = ps_strings */
   1772 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   1773 
   1774 	p->p_md.md_flags &= ~MDP_FPUSED;
   1775 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
   1776 		fpusave_proc(p, 0);
   1777 }
   1778 
   1779 /*
   1780  * Release the FPU.
   1781  */
   1782 void
   1783 fpusave_cpu(struct cpu_info *ci, int save)
   1784 {
   1785 	struct proc *p;
   1786 #if defined(MULTIPROCESSOR)
   1787 	int s;
   1788 #endif
   1789 
   1790 	KDASSERT(ci == curcpu());
   1791 
   1792 	p = ci->ci_fpcurproc;
   1793 	if (p == NULL)
   1794 		return;
   1795 
   1796 	if (save) {
   1797 		alpha_pal_wrfen(1);
   1798 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
   1799 	}
   1800 
   1801 	alpha_pal_wrfen(0);
   1802 
   1803 #if defined(MULTIPROCESSOR)
   1804 	s = splhigh();
   1805 #endif
   1806 	p->p_addr->u_pcb.pcb_fpcpu = NULL;
   1807 	ci->ci_fpcurproc = NULL;
   1808 #if defined(MULTIPROCESSOR)
   1809 	splx(s);
   1810 	alpha_mb();
   1811 #endif
   1812 }
   1813 
   1814 /*
   1815  * Synchronize FP state for this process.
   1816  */
   1817 void
   1818 fpusave_proc(struct proc *p, int save)
   1819 {
   1820 	struct cpu_info *ci = curcpu();
   1821 	struct cpu_info *oci;
   1822 
   1823 	KDASSERT(p->p_addr != NULL);
   1824 	KDASSERT(p->p_flag & P_INMEM);
   1825 
   1826 	oci = p->p_addr->u_pcb.pcb_fpcpu;
   1827 	if (oci == NULL)
   1828 		return;
   1829 
   1830 #if defined(MULTIPROCESSOR)
   1831 	if (oci == ci) {
   1832 		int s;
   1833 		KASSERT(ci->ci_fpcurproc == p);
   1834 		s = splhigh();
   1835 		fpusave_cpu(ci, save);
   1836 		splx(s);
   1837 	} else {
   1838 		u_long ipi = save ? ALPHA_IPI_SYNCH_FPU :
   1839 				    ALPHA_IPI_DISCARD_FPU;
   1840 
   1841 		KASSERT(oci->ci_fpcurproc == p);
   1842 		do {
   1843 			alpha_send_ipi(oci->ci_cpuid, ipi);
   1844 		} while (p->p_addr->u_pcb.pcb_fpcpu != NULL);
   1845 	}
   1846 #else
   1847 	KASSERT(ci->ci_fpcurproc == p);
   1848 	fpusave_cpu(ci, save);
   1849 #endif /* MULTIPROCESSOR */
   1850 }
   1851 
   1852 void
   1853 spl0()
   1854 {
   1855 
   1856 	if (ssir) {
   1857 		(void) alpha_pal_swpipl(ALPHA_PSL_IPL_SOFT);
   1858 		softintr_dispatch();
   1859 	}
   1860 
   1861 	(void) alpha_pal_swpipl(ALPHA_PSL_IPL_0);
   1862 }
   1863 
   1864 /*
   1865  * The following primitives manipulate the run queues.  _whichqs tells which
   1866  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
   1867  * into queues, Remrunqueue removes them from queues.  The running process is
   1868  * on no queue, other processes are on a queue related to p->p_priority,
   1869  * divided by 4 actually to shrink the 0-127 range of priorities into the 32
   1870  * available queues.
   1871  */
   1872 /*
   1873  * setrunqueue(p)
   1874  *	proc *p;
   1875  *
   1876  * Call should be made at splclock(), and p->p_stat should be SRUN.
   1877  */
   1878 
   1879 void
   1880 setrunqueue(p)
   1881 	struct proc *p;
   1882 {
   1883 	int bit;
   1884 
   1885 	/* firewall: p->p_back must be NULL */
   1886 	if (p->p_back != NULL)
   1887 		panic("setrunqueue");
   1888 
   1889 	bit = p->p_priority >> 2;
   1890 	sched_whichqs |= (1 << bit);
   1891 	p->p_forw = (struct proc *)&sched_qs[bit];
   1892 	p->p_back = sched_qs[bit].ph_rlink;
   1893 	p->p_back->p_forw = p;
   1894 	sched_qs[bit].ph_rlink = p;
   1895 }
   1896 
   1897 /*
   1898  * remrunqueue(p)
   1899  *
   1900  * Call should be made at splclock().
   1901  */
   1902 void
   1903 remrunqueue(p)
   1904 	struct proc *p;
   1905 {
   1906 	int bit;
   1907 
   1908 	bit = p->p_priority >> 2;
   1909 	if ((sched_whichqs & (1 << bit)) == 0)
   1910 		panic("remrunqueue");
   1911 
   1912 	p->p_back->p_forw = p->p_forw;
   1913 	p->p_forw->p_back = p->p_back;
   1914 	p->p_back = NULL;	/* for firewall checking. */
   1915 
   1916 	if ((struct proc *)&sched_qs[bit] == sched_qs[bit].ph_link)
   1917 		sched_whichqs &= ~(1 << bit);
   1918 }
   1919 
   1920 /*
   1921  * Return the best possible estimate of the time in the timeval
   1922  * to which tvp points.  Unfortunately, we can't read the hardware registers.
   1923  * We guarantee that the time will be greater than the value obtained by a
   1924  * previous call.
   1925  *
   1926  * XXX PLEASE REWRITE ME TO USE THE CYCLE COUNTER AND DEAL WITH
   1927  * XXX MULTIPLE CPUs IN A SANE WAY!
   1928  */
   1929 void
   1930 microtime(tvp)
   1931 	register struct timeval *tvp;
   1932 {
   1933 	static struct timeval lasttime;
   1934 	static struct simplelock microtime_slock = SIMPLELOCK_INITIALIZER;
   1935 	int s;
   1936 
   1937 	s = splclock();
   1938 	simple_lock(&microtime_slock);
   1939 
   1940 	*tvp = time;
   1941 #ifdef notdef
   1942 	tvp->tv_usec += clkread();
   1943 	while (tvp->tv_usec >= 1000000) {
   1944 		tvp->tv_sec++;
   1945 		tvp->tv_usec -= 1000000;
   1946 	}
   1947 #endif
   1948 	if (tvp->tv_sec == lasttime.tv_sec &&
   1949 	    tvp->tv_usec <= lasttime.tv_usec &&
   1950 	    (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
   1951 		tvp->tv_sec++;
   1952 		tvp->tv_usec -= 1000000;
   1953 	}
   1954 	lasttime = *tvp;
   1955 
   1956 	simple_unlock(&microtime_slock);
   1957 	splx(s);
   1958 }
   1959 
   1960 /*
   1961  * Wait "n" microseconds.
   1962  */
   1963 void
   1964 delay(n)
   1965 	unsigned long n;
   1966 {
   1967 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
   1968 
   1969 	if (n == 0)
   1970 		return;
   1971 
   1972 	pcc0 = alpha_rpcc() & 0xffffffffUL;
   1973 	cycles = 0;
   1974 	usec = 0;
   1975 
   1976 	while (usec <= n) {
   1977 		/*
   1978 		 * Get the next CPU cycle count- assumes that we cannot
   1979 		 * have had more than one 32 bit overflow.
   1980 		 */
   1981 		pcc1 = alpha_rpcc() & 0xffffffffUL;
   1982 		if (pcc1 < pcc0)
   1983 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
   1984 		else
   1985 			curcycle = pcc1 - pcc0;
   1986 
   1987 		/*
   1988 		 * We now have the number of processor cycles since we
   1989 		 * last checked. Add the current cycle count to the
   1990 		 * running total. If it's over cycles_per_usec, increment
   1991 		 * the usec counter.
   1992 		 */
   1993 		cycles += curcycle;
   1994 		while (cycles > cycles_per_usec) {
   1995 			usec++;
   1996 			cycles -= cycles_per_usec;
   1997 		}
   1998 		pcc0 = pcc1;
   1999 	}
   2000 }
   2001 
   2002 #if defined(COMPAT_OSF1) || 1		/* XXX */
   2003 void	cpu_exec_ecoff_setregs __P((struct proc *, struct exec_package *,
   2004 	    u_long));
   2005 #endif
   2006 
   2007 #if 1		/* XXX */
   2008 void
   2009 cpu_exec_ecoff_setregs(p, epp, stack)
   2010 	struct proc *p;
   2011 	struct exec_package *epp;
   2012 	u_long stack;
   2013 {
   2014 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   2015 
   2016 	setregs(p, epp, stack);
   2017 	p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   2018 }
   2019 
   2020 /*
   2021  * cpu_exec_ecoff_hook():
   2022  *	cpu-dependent ECOFF format hook for execve().
   2023  *
   2024  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   2025  *
   2026  */
   2027 int
   2028 cpu_exec_ecoff_probe(p, epp)
   2029 	struct proc *p;
   2030 	struct exec_package *epp;
   2031 {
   2032 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   2033 	int error;
   2034 
   2035 	if (execp->f.f_magic == ECOFF_MAGIC_NETBSD_ALPHA)
   2036 		error = 0;
   2037 	else
   2038 		error = ENOEXEC;
   2039 
   2040 	return (error);
   2041 }
   2042 #endif
   2043 
   2044 int
   2045 alpha_pa_access(pa)
   2046 	u_long pa;
   2047 {
   2048 	int i;
   2049 
   2050 	for (i = 0; i < mem_cluster_cnt; i++) {
   2051 		if (pa < mem_clusters[i].start)
   2052 			continue;
   2053 		if ((pa - mem_clusters[i].start) >=
   2054 		    (mem_clusters[i].size & ~PAGE_MASK))
   2055 			continue;
   2056 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
   2057 	}
   2058 
   2059 	/*
   2060 	 * Address is not a memory address.  If we're secure, disallow
   2061 	 * access.  Otherwise, grant read/write.
   2062 	 */
   2063 	if (securelevel > 0)
   2064 		return (PROT_NONE);
   2065 	else
   2066 		return (PROT_READ | PROT_WRITE);
   2067 }
   2068 
   2069 /* XXX XXX BEGIN XXX XXX */
   2070 paddr_t alpha_XXX_dmamap_or;					/* XXX */
   2071 								/* XXX */
   2072 paddr_t								/* XXX */
   2073 alpha_XXX_dmamap(v)						/* XXX */
   2074 	vaddr_t v;						/* XXX */
   2075 {								/* XXX */
   2076 								/* XXX */
   2077 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
   2078 }								/* XXX */
   2079 /* XXX XXX END XXX XXX */
   2080 
   2081 char *
   2082 dot_conv(x)
   2083 	unsigned long x;
   2084 {
   2085 	int i;
   2086 	char *xc;
   2087 	static int next;
   2088 	static char space[2][20];
   2089 
   2090 	xc = space[next ^= 1] + sizeof space[0];
   2091 	*--xc = '\0';
   2092 	for (i = 0;; ++i) {
   2093 		if (i && (i & 3) == 0)
   2094 			*--xc = '.';
   2095 		*--xc = "0123456789abcdef"[x & 0xf];
   2096 		x >>= 4;
   2097 		if (x == 0)
   2098 			break;
   2099 	}
   2100 	return xc;
   2101 }
   2102