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