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