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