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