Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.309
      1 /* $NetBSD: machdep.c,v 1.309 2008/11/12 12:35:55 ad 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_multiprocessor.h"
     63 #include "opt_dec_3000_300.h"
     64 #include "opt_dec_3000_500.h"
     65 #include "opt_compat_osf1.h"
     66 #include "opt_compat_netbsd.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.309 2008/11/12 12:35:55 ad 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 __P((void));
    205 int	cpu_dumpsize __P((void));
    206 u_long	cpu_dump_mempagecnt __P((void));
    207 void	dumpsys __P((void));
    208 void	identifycpu __P((void));
    209 void	printregs __P((struct reg *));
    210 
    211 void
    212 alpha_init(pfn, ptb, bim, bip, biv)
    213 	u_long pfn;		/* first free PFN number */
    214 	u_long ptb;		/* PFN of current level 1 page table */
    215 	u_long bim;		/* bootinfo magic */
    216 	u_long bip;		/* bootinfo pointer */
    217 	u_long 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_init((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(variation, avtp)
    902 	u_int64_t variation;
    903 	const struct alpha_variation_table *avtp;
    904 {
    905 	int i;
    906 
    907 	for (i = 0; avtp[i].avt_model != NULL; i++)
    908 		if (avtp[i].avt_variation == variation)
    909 			return (avtp[i].avt_model);
    910 	return (NULL);
    911 }
    912 
    913 /*
    914  * Generate a default platform name based for unknown system variations.
    915  */
    916 const char *
    917 alpha_unknown_sysname()
    918 {
    919 	static char s[128];		/* safe size */
    920 
    921 	sprintf(s, "%s family, unknown model variation 0x%lx",
    922 	    platform.family, hwrpb->rpb_variation & SV_ST_MASK);
    923 	return ((const char *)s);
    924 }
    925 
    926 void
    927 identifycpu()
    928 {
    929 	char *s;
    930 	int i;
    931 
    932 	/*
    933 	 * print out CPU identification information.
    934 	 */
    935 	printf("%s", cpu_model);
    936 	for(s = cpu_model; *s; ++s)
    937 		if(strncasecmp(s, "MHz", 3) == 0)
    938 			goto skipMHz;
    939 	printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
    940 skipMHz:
    941 	printf(", s/n ");
    942 	for (i = 0; i < 10; i++)
    943 		printf("%c", hwrpb->rpb_ssn[i]);
    944 	printf("\n");
    945 	printf("%ld byte page size, %d processor%s.\n",
    946 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
    947 #if 0
    948 	/* this isn't defined for any systems that we run on? */
    949 	printf("serial number 0x%lx 0x%lx\n",
    950 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
    951 
    952 	/* and these aren't particularly useful! */
    953 	printf("variation: 0x%lx, revision 0x%lx\n",
    954 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
    955 #endif
    956 }
    957 
    958 int	waittime = -1;
    959 struct pcb dumppcb;
    960 
    961 void
    962 cpu_reboot(howto, bootstr)
    963 	int howto;
    964 	char *bootstr;
    965 {
    966 #if defined(MULTIPROCESSOR)
    967 	u_long cpu_id = cpu_number();
    968 	u_long wait_mask = (1UL << cpu_id) |
    969 			   (1UL << hwrpb->rpb_primary_cpu_id);
    970 	int i;
    971 #endif
    972 
    973 	/* If "always halt" was specified as a boot flag, obey. */
    974 	if ((boothowto & RB_HALT) != 0)
    975 		howto |= RB_HALT;
    976 
    977 	boothowto = howto;
    978 
    979 	/* If system is cold, just halt. */
    980 	if (cold) {
    981 		boothowto |= RB_HALT;
    982 		goto haltsys;
    983 	}
    984 
    985 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    986 		waittime = 0;
    987 		vfs_shutdown();
    988 		/*
    989 		 * If we've been adjusting the clock, the todr
    990 		 * will be out of synch; adjust it now.
    991 		 */
    992 		resettodr();
    993 	}
    994 
    995 	/* Disable interrupts. */
    996 	splhigh();
    997 
    998 #if defined(MULTIPROCESSOR)
    999 	/*
   1000 	 * Halt all other CPUs.  If we're not the primary, the
   1001 	 * primary will spin, waiting for us to halt.
   1002 	 */
   1003 	alpha_broadcast_ipi(ALPHA_IPI_HALT);
   1004 
   1005 	/* Ensure any CPUs paused by DDB resume execution so they can halt */
   1006 	cpus_paused = 0;
   1007 
   1008 	for (i = 0; i < 10000; i++) {
   1009 		alpha_mb();
   1010 		if (cpus_running == wait_mask)
   1011 			break;
   1012 		delay(1000);
   1013 	}
   1014 	alpha_mb();
   1015 	if (cpus_running != wait_mask)
   1016 		printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
   1017 		    cpus_running);
   1018 #endif /* MULTIPROCESSOR */
   1019 
   1020 	/* If rebooting and a dump is requested do it. */
   1021 #if 0
   1022 	if ((boothowto & (RB_DUMP | RB_HALT)) == RB_DUMP)
   1023 #else
   1024 	if (boothowto & RB_DUMP)
   1025 #endif
   1026 		dumpsys();
   1027 
   1028 haltsys:
   1029 
   1030 	/* run any shutdown hooks */
   1031 	doshutdownhooks();
   1032 
   1033 	pmf_system_shutdown(boothowto);
   1034 
   1035 #ifdef BOOTKEY
   1036 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
   1037 	cnpollc(1);	/* for proper keyboard command handling */
   1038 	cngetc();
   1039 	cnpollc(0);
   1040 	printf("\n");
   1041 #endif
   1042 
   1043 	/* Finally, powerdown/halt/reboot the system. */
   1044 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN &&
   1045 	    platform.powerdown != NULL) {
   1046 		(*platform.powerdown)();
   1047 		printf("WARNING: powerdown failed!\n");
   1048 	}
   1049 	printf("%s\n\n", (boothowto & RB_HALT) ? "halted." : "rebooting...");
   1050 #if defined(MULTIPROCESSOR)
   1051 	if (cpu_id != hwrpb->rpb_primary_cpu_id)
   1052 		cpu_halt();
   1053 	else
   1054 #endif
   1055 		prom_halt(boothowto & RB_HALT);
   1056 	/*NOTREACHED*/
   1057 }
   1058 
   1059 /*
   1060  * These variables are needed by /sbin/savecore
   1061  */
   1062 u_int32_t dumpmag = 0x8fca0101;	/* magic number */
   1063 int 	dumpsize = 0;		/* pages */
   1064 long	dumplo = 0; 		/* blocks */
   1065 
   1066 /*
   1067  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
   1068  */
   1069 int
   1070 cpu_dumpsize()
   1071 {
   1072 	int size;
   1073 
   1074 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
   1075 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
   1076 	if (roundup(size, dbtob(1)) != dbtob(1))
   1077 		return -1;
   1078 
   1079 	return (1);
   1080 }
   1081 
   1082 /*
   1083  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
   1084  */
   1085 u_long
   1086 cpu_dump_mempagecnt()
   1087 {
   1088 	u_long i, n;
   1089 
   1090 	n = 0;
   1091 	for (i = 0; i < mem_cluster_cnt; i++)
   1092 		n += atop(mem_clusters[i].size);
   1093 	return (n);
   1094 }
   1095 
   1096 /*
   1097  * cpu_dump: dump machine-dependent kernel core dump headers.
   1098  */
   1099 int
   1100 cpu_dump()
   1101 {
   1102 	int (*dump) __P((dev_t, daddr_t, void *, size_t));
   1103 	char buf[dbtob(1)];
   1104 	kcore_seg_t *segp;
   1105 	cpu_kcore_hdr_t *cpuhdrp;
   1106 	phys_ram_seg_t *memsegp;
   1107 	const struct bdevsw *bdev;
   1108 	int i;
   1109 
   1110 	bdev = bdevsw_lookup(dumpdev);
   1111 	if (bdev == NULL)
   1112 		return (ENXIO);
   1113 	dump = bdev->d_dump;
   1114 
   1115 	memset(buf, 0, sizeof buf);
   1116 	segp = (kcore_seg_t *)buf;
   1117 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
   1118 	memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
   1119 	    ALIGN(sizeof(*cpuhdrp))];
   1120 
   1121 	/*
   1122 	 * Generate a segment header.
   1123 	 */
   1124 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
   1125 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
   1126 
   1127 	/*
   1128 	 * Add the machine-dependent header info.
   1129 	 */
   1130 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
   1131 	cpuhdrp->page_size = PAGE_SIZE;
   1132 	cpuhdrp->nmemsegs = mem_cluster_cnt;
   1133 
   1134 	/*
   1135 	 * Fill in the memory segment descriptors.
   1136 	 */
   1137 	for (i = 0; i < mem_cluster_cnt; i++) {
   1138 		memsegp[i].start = mem_clusters[i].start;
   1139 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
   1140 	}
   1141 
   1142 	return (dump(dumpdev, dumplo, (void *)buf, dbtob(1)));
   1143 }
   1144 
   1145 /*
   1146  * This is called by main to set dumplo and dumpsize.
   1147  * Dumps always skip the first PAGE_SIZE of disk space
   1148  * in case there might be a disk label stored there.
   1149  * If there is extra space, put dump at the end to
   1150  * reduce the chance that swapping trashes it.
   1151  */
   1152 void
   1153 cpu_dumpconf()
   1154 {
   1155 	const struct bdevsw *bdev;
   1156 	int nblks, dumpblks;	/* size of dump area */
   1157 
   1158 	if (dumpdev == NODEV)
   1159 		goto bad;
   1160 	bdev = bdevsw_lookup(dumpdev);
   1161 	if (bdev == NULL) {
   1162 		dumpdev = NODEV;
   1163 		goto bad;
   1164 	}
   1165 	if (bdev->d_psize == NULL)
   1166 		goto bad;
   1167 	nblks = (*bdev->d_psize)(dumpdev);
   1168 	if (nblks <= ctod(1))
   1169 		goto bad;
   1170 
   1171 	dumpblks = cpu_dumpsize();
   1172 	if (dumpblks < 0)
   1173 		goto bad;
   1174 	dumpblks += ctod(cpu_dump_mempagecnt());
   1175 
   1176 	/* If dump won't fit (incl. room for possible label), punt. */
   1177 	if (dumpblks > (nblks - ctod(1)))
   1178 		goto bad;
   1179 
   1180 	/* Put dump at end of partition */
   1181 	dumplo = nblks - dumpblks;
   1182 
   1183 	/* dumpsize is in page units, and doesn't include headers. */
   1184 	dumpsize = cpu_dump_mempagecnt();
   1185 	return;
   1186 
   1187 bad:
   1188 	dumpsize = 0;
   1189 	return;
   1190 }
   1191 
   1192 /*
   1193  * Dump the kernel's image to the swap partition.
   1194  */
   1195 #define	BYTES_PER_DUMP	PAGE_SIZE
   1196 
   1197 void
   1198 dumpsys()
   1199 {
   1200 	const struct bdevsw *bdev;
   1201 	u_long totalbytesleft, bytes, i, n, memcl;
   1202 	u_long maddr;
   1203 	int psize;
   1204 	daddr_t blkno;
   1205 	int (*dump) __P((dev_t, daddr_t, void *, size_t));
   1206 	int error;
   1207 
   1208 	/* Save registers. */
   1209 	savectx(&dumppcb);
   1210 
   1211 	if (dumpdev == NODEV)
   1212 		return;
   1213 	bdev = bdevsw_lookup(dumpdev);
   1214 	if (bdev == NULL || bdev->d_psize == NULL)
   1215 		return;
   1216 
   1217 	/*
   1218 	 * For dumps during autoconfiguration,
   1219 	 * if dump device has already configured...
   1220 	 */
   1221 	if (dumpsize == 0)
   1222 		cpu_dumpconf();
   1223 	if (dumplo <= 0) {
   1224 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
   1225 		    minor(dumpdev));
   1226 		return;
   1227 	}
   1228 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
   1229 	    minor(dumpdev), dumplo);
   1230 
   1231 	psize = (*bdev->d_psize)(dumpdev);
   1232 	printf("dump ");
   1233 	if (psize == -1) {
   1234 		printf("area unavailable\n");
   1235 		return;
   1236 	}
   1237 
   1238 	/* XXX should purge all outstanding keystrokes. */
   1239 
   1240 	if ((error = cpu_dump()) != 0)
   1241 		goto err;
   1242 
   1243 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
   1244 	blkno = dumplo + cpu_dumpsize();
   1245 	dump = bdev->d_dump;
   1246 	error = 0;
   1247 
   1248 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
   1249 		maddr = mem_clusters[memcl].start;
   1250 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
   1251 
   1252 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
   1253 
   1254 			/* Print out how many MBs we to go. */
   1255 			if ((totalbytesleft % (1024*1024)) == 0)
   1256 				printf("%ld ", totalbytesleft / (1024 * 1024));
   1257 
   1258 			/* Limit size for next transfer. */
   1259 			n = bytes - i;
   1260 			if (n > BYTES_PER_DUMP)
   1261 				n =  BYTES_PER_DUMP;
   1262 
   1263 			error = (*dump)(dumpdev, blkno,
   1264 			    (void *)ALPHA_PHYS_TO_K0SEG(maddr), n);
   1265 			if (error)
   1266 				goto err;
   1267 			maddr += n;
   1268 			blkno += btodb(n);			/* XXX? */
   1269 
   1270 			/* XXX should look for keystrokes, to cancel. */
   1271 		}
   1272 	}
   1273 
   1274 err:
   1275 	switch (error) {
   1276 
   1277 	case ENXIO:
   1278 		printf("device bad\n");
   1279 		break;
   1280 
   1281 	case EFAULT:
   1282 		printf("device not ready\n");
   1283 		break;
   1284 
   1285 	case EINVAL:
   1286 		printf("area improper\n");
   1287 		break;
   1288 
   1289 	case EIO:
   1290 		printf("i/o error\n");
   1291 		break;
   1292 
   1293 	case EINTR:
   1294 		printf("aborted from console\n");
   1295 		break;
   1296 
   1297 	case 0:
   1298 		printf("succeeded\n");
   1299 		break;
   1300 
   1301 	default:
   1302 		printf("error %d\n", error);
   1303 		break;
   1304 	}
   1305 	printf("\n\n");
   1306 	delay(1000);
   1307 }
   1308 
   1309 void
   1310 frametoreg(framep, regp)
   1311 	const struct trapframe *framep;
   1312 	struct reg *regp;
   1313 {
   1314 
   1315 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1316 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1317 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1318 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1319 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1320 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1321 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1322 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1323 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1324 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1325 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1326 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1327 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1328 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1329 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1330 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1331 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1332 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1333 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1334 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1335 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1336 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1337 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1338 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1339 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1340 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1341 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1342 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1343 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1344 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1345 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1346 	regp->r_regs[R_ZERO] = 0;
   1347 }
   1348 
   1349 void
   1350 regtoframe(regp, framep)
   1351 	const struct reg *regp;
   1352 	struct trapframe *framep;
   1353 {
   1354 
   1355 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1356 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1357 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1358 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1359 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1360 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1361 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1362 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1363 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1364 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1365 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1366 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1367 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1368 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1369 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1370 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1371 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1372 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1373 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1374 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1375 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1376 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1377 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1378 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1379 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1380 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1381 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1382 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1383 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1384 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1385 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1386 	/* ??? = regp->r_regs[R_ZERO]; */
   1387 }
   1388 
   1389 void
   1390 printregs(regp)
   1391 	struct reg *regp;
   1392 {
   1393 	int i;
   1394 
   1395 	for (i = 0; i < 32; i++)
   1396 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1397 		   i & 1 ? "\n" : "\t");
   1398 }
   1399 
   1400 void
   1401 regdump(framep)
   1402 	struct trapframe *framep;
   1403 {
   1404 	struct reg reg;
   1405 
   1406 	frametoreg(framep, &reg);
   1407 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1408 
   1409 	printf("REGISTERS:\n");
   1410 	printregs(&reg);
   1411 }
   1412 
   1413 
   1414 
   1415 void *
   1416 getframe(const struct lwp *l, int sig, int *onstack)
   1417 {
   1418 	void *frame;
   1419 
   1420 	/* Do we need to jump onto the signal stack? */
   1421 	*onstack =
   1422 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
   1423 	    (SIGACTION(l->l_proc, sig).sa_flags & SA_ONSTACK) != 0;
   1424 
   1425 	if (*onstack)
   1426 		frame = (void *)((char *)l->l_sigstk.ss_sp +
   1427 					l->l_sigstk.ss_size);
   1428 	else
   1429 		frame = (void *)(alpha_pal_rdusp());
   1430 	return (frame);
   1431 }
   1432 
   1433 void
   1434 buildcontext(struct lwp *l, const void *catcher, const void *tramp, const void *fp)
   1435 {
   1436 	struct trapframe *tf = l->l_md.md_tf;
   1437 
   1438 	tf->tf_regs[FRAME_RA] = (u_int64_t)tramp;
   1439 	tf->tf_regs[FRAME_PC] = (u_int64_t)catcher;
   1440 	tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
   1441 	alpha_pal_wrusp((unsigned long)fp);
   1442 }
   1443 
   1444 
   1445 /*
   1446  * Send an interrupt to process, new style
   1447  */
   1448 void
   1449 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
   1450 {
   1451 	struct lwp *l = curlwp;
   1452 	struct proc *p = l->l_proc;
   1453 	struct sigacts *ps = p->p_sigacts;
   1454 	int onstack, sig = ksi->ksi_signo, error;
   1455 	struct sigframe_siginfo *fp, frame;
   1456 	struct trapframe *tf;
   1457 	sig_t catcher = SIGACTION(p, ksi->ksi_signo).sa_handler;
   1458 
   1459 	fp = (struct sigframe_siginfo *)getframe(l,ksi->ksi_signo,&onstack);
   1460 	tf = l->l_md.md_tf;
   1461 
   1462 	/* Allocate space for the signal handler context. */
   1463 	fp--;
   1464 
   1465 	/* Build stack frame for signal trampoline. */
   1466 	switch (ps->sa_sigdesc[sig].sd_vers) {
   1467 	case 0:		/* handled by sendsig_sigcontext */
   1468 	case 1:		/* handled by sendsig_sigcontext */
   1469 	default:	/* unknown version */
   1470 		printf("nsendsig: bad version %d\n",
   1471 		    ps->sa_sigdesc[sig].sd_vers);
   1472 		sigexit(l, SIGILL);
   1473 	case 2:
   1474 		break;
   1475 	}
   1476 
   1477 #ifdef DEBUG
   1478 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1479 		printf("sendsig_siginfo(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1480 		    sig, &onstack, fp);
   1481 #endif
   1482 
   1483 	/* Build stack frame for signal trampoline. */
   1484 
   1485 	frame.sf_si._info = ksi->ksi_info;
   1486 	frame.sf_uc.uc_flags = _UC_SIGMASK;
   1487 	frame.sf_uc.uc_sigmask = *mask;
   1488 	frame.sf_uc.uc_link = l->l_ctxlink;
   1489 	memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
   1490 	sendsig_reset(l, sig);
   1491 	mutex_exit(p->p_lock);
   1492 	cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
   1493 	error = copyout(&frame, fp, sizeof(frame));
   1494 	mutex_enter(p->p_lock);
   1495 
   1496 	if (error != 0) {
   1497 		/*
   1498 		 * Process has trashed its stack; give it an illegal
   1499 		 * instruction to halt it in its tracks.
   1500 		 */
   1501 #ifdef DEBUG
   1502 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1503 			printf("sendsig_siginfo(%d): copyout failed on sig %d\n",
   1504 			    p->p_pid, sig);
   1505 #endif
   1506 		sigexit(l, SIGILL);
   1507 		/* NOTREACHED */
   1508 	}
   1509 
   1510 #ifdef DEBUG
   1511 	if (sigdebug & SDB_FOLLOW)
   1512 		printf("sendsig_siginfo(%d): sig %d usp %p code %x\n",
   1513 		       p->p_pid, sig, fp, ksi->ksi_code);
   1514 #endif
   1515 
   1516 	/*
   1517 	 * Set up the registers to directly invoke the signal handler.  The
   1518 	 * signal trampoline is then used to return from the signal.  Note
   1519 	 * the trampoline version numbers are coordinated with machine-
   1520 	 * dependent code in libc.
   1521 	 */
   1522 
   1523 	tf->tf_regs[FRAME_A0] = sig;
   1524 	tf->tf_regs[FRAME_A1] = (u_int64_t)&fp->sf_si;
   1525 	tf->tf_regs[FRAME_A2] = (u_int64_t)&fp->sf_uc;
   1526 
   1527 	buildcontext(l,catcher,ps->sa_sigdesc[sig].sd_tramp,fp);
   1528 
   1529 	/* Remember that we're now on the signal stack. */
   1530 	if (onstack)
   1531 		l->l_sigstk.ss_flags |= SS_ONSTACK;
   1532 
   1533 #ifdef DEBUG
   1534 	if (sigdebug & SDB_FOLLOW)
   1535 		printf("sendsig_siginfo(%d): pc %lx, catcher %lx\n", p->p_pid,
   1536 		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
   1537 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1538 		printf("sendsig_siginfo(%d): sig %d returns\n",
   1539 		    p->p_pid, sig);
   1540 #endif
   1541 }
   1542 
   1543 
   1544 void
   1545 sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
   1546 {
   1547 #ifdef COMPAT_16
   1548 	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2) {
   1549 		sendsig_sigcontext(ksi, mask);
   1550 	} else {
   1551 #endif
   1552 #ifdef DEBUG
   1553 	if (sigdebug & SDB_FOLLOW)
   1554 		printf("sendsig: sendsig called: sig %d vers %d\n",
   1555 		       ksi->ksi_signo,
   1556 		       curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers);
   1557 #endif
   1558 		sendsig_siginfo(ksi, mask);
   1559 #ifdef COMPAT_16
   1560 	}
   1561 #endif
   1562 }
   1563 
   1564 void
   1565 cpu_upcall(struct lwp *l, int type, int nevents, int ninterrupted, void *sas, void *ap, void *sp, sa_upcall_t upcall)
   1566 {
   1567        	struct trapframe *tf;
   1568 
   1569 	tf = l->l_md.md_tf;
   1570 
   1571 	tf->tf_regs[FRAME_PC] = (u_int64_t)upcall;
   1572 	tf->tf_regs[FRAME_RA] = 0;
   1573 	tf->tf_regs[FRAME_A0] = type;
   1574 	tf->tf_regs[FRAME_A1] = (u_int64_t)sas;
   1575 	tf->tf_regs[FRAME_A2] = nevents;
   1576 	tf->tf_regs[FRAME_A3] = ninterrupted;
   1577 	tf->tf_regs[FRAME_A4] = (u_int64_t)ap;
   1578 	tf->tf_regs[FRAME_T12] = (u_int64_t)upcall;  /* t12 is pv */
   1579 	alpha_pal_wrusp((unsigned long)sp);
   1580 }
   1581 
   1582 /*
   1583  * machine dependent system variables.
   1584  */
   1585 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
   1586 {
   1587 
   1588 	sysctl_createv(clog, 0, NULL, NULL,
   1589 		       CTLFLAG_PERMANENT,
   1590 		       CTLTYPE_NODE, "machdep", NULL,
   1591 		       NULL, 0, NULL, 0,
   1592 		       CTL_MACHDEP, CTL_EOL);
   1593 
   1594 	sysctl_createv(clog, 0, NULL, NULL,
   1595 		       CTLFLAG_PERMANENT,
   1596 		       CTLTYPE_STRUCT, "console_device", NULL,
   1597 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
   1598 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
   1599 	sysctl_createv(clog, 0, NULL, NULL,
   1600 		       CTLFLAG_PERMANENT,
   1601 		       CTLTYPE_STRING, "root_device", NULL,
   1602 		       sysctl_root_device, 0, NULL, 0,
   1603 		       CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
   1604 	sysctl_createv(clog, 0, NULL, NULL,
   1605 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1606 		       CTLTYPE_INT, "unaligned_print", NULL,
   1607 		       NULL, 0, &alpha_unaligned_print, 0,
   1608 		       CTL_MACHDEP, CPU_UNALIGNED_PRINT, CTL_EOL);
   1609 	sysctl_createv(clog, 0, NULL, NULL,
   1610 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1611 		       CTLTYPE_INT, "unaligned_fix", NULL,
   1612 		       NULL, 0, &alpha_unaligned_fix, 0,
   1613 		       CTL_MACHDEP, CPU_UNALIGNED_FIX, CTL_EOL);
   1614 	sysctl_createv(clog, 0, NULL, NULL,
   1615 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1616 		       CTLTYPE_INT, "unaligned_sigbus", NULL,
   1617 		       NULL, 0, &alpha_unaligned_sigbus, 0,
   1618 		       CTL_MACHDEP, CPU_UNALIGNED_SIGBUS, CTL_EOL);
   1619 	sysctl_createv(clog, 0, NULL, NULL,
   1620 		       CTLFLAG_PERMANENT,
   1621 		       CTLTYPE_STRING, "booted_kernel", NULL,
   1622 		       NULL, 0, bootinfo.booted_kernel, 0,
   1623 		       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
   1624 	sysctl_createv(clog, 0, NULL, NULL,
   1625 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1626 		       CTLTYPE_INT, "fp_sync_complete", NULL,
   1627 		       NULL, 0, &alpha_fp_sync_complete, 0,
   1628 		       CTL_MACHDEP, CPU_FP_SYNC_COMPLETE, CTL_EOL);
   1629 }
   1630 
   1631 /*
   1632  * Set registers on exec.
   1633  */
   1634 void
   1635 setregs(l, pack, stack)
   1636 	register struct lwp *l;
   1637 	struct exec_package *pack;
   1638 	u_long stack;
   1639 {
   1640 	struct trapframe *tfp = l->l_md.md_tf;
   1641 #ifdef DEBUG
   1642 	int i;
   1643 #endif
   1644 
   1645 #ifdef DEBUG
   1646 	/*
   1647 	 * Crash and dump, if the user requested it.
   1648 	 */
   1649 	if (boothowto & RB_DUMP)
   1650 		panic("crash requested by boot flags");
   1651 #endif
   1652 
   1653 #ifdef DEBUG
   1654 	for (i = 0; i < FRAME_SIZE; i++)
   1655 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1656 #else
   1657 	memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   1658 #endif
   1659 	memset(&l->l_addr->u_pcb.pcb_fp, 0, sizeof l->l_addr->u_pcb.pcb_fp);
   1660 	alpha_pal_wrusp(stack);
   1661 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   1662 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   1663 
   1664 	tfp->tf_regs[FRAME_A0] = stack;			/* a0 = sp */
   1665 	tfp->tf_regs[FRAME_A1] = 0;			/* a1 = rtld cleanup */
   1666 	tfp->tf_regs[FRAME_A2] = 0;			/* a2 = rtld object */
   1667 	tfp->tf_regs[FRAME_A3] = (u_int64_t)l->l_proc->p_psstr;	/* a3 = ps_strings */
   1668 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   1669 
   1670 	l->l_md.md_flags &= ~MDP_FPUSED;
   1671 	if (__predict_true((l->l_md.md_flags & IEEE_INHERIT) == 0)) {
   1672 		l->l_md.md_flags &= ~MDP_FP_C;
   1673 		l->l_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
   1674 	}
   1675 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1676 		fpusave_proc(l, 0);
   1677 }
   1678 
   1679 /*
   1680  * Release the FPU.
   1681  */
   1682 void
   1683 fpusave_cpu(struct cpu_info *ci, int save)
   1684 {
   1685 	struct lwp *l;
   1686 #if defined(MULTIPROCESSOR)
   1687 	int s;
   1688 #endif
   1689 
   1690 	KDASSERT(ci == curcpu());
   1691 
   1692 #if defined(MULTIPROCESSOR)
   1693 	s = splhigh();		/* block IPIs for the duration */
   1694 	atomic_or_ulong(&ci->ci_flags, CPUF_FPUSAVE);
   1695 #endif
   1696 
   1697 	l = ci->ci_fpcurlwp;
   1698 	if (l == NULL)
   1699 		goto out;
   1700 
   1701 	if (save) {
   1702 		alpha_pal_wrfen(1);
   1703 		savefpstate(&l->l_addr->u_pcb.pcb_fp);
   1704 	}
   1705 
   1706 	alpha_pal_wrfen(0);
   1707 
   1708 	FPCPU_LOCK(&l->l_addr->u_pcb);
   1709 
   1710 	l->l_addr->u_pcb.pcb_fpcpu = NULL;
   1711 	ci->ci_fpcurlwp = NULL;
   1712 
   1713 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1714 
   1715  out:
   1716 #if defined(MULTIPROCESSOR)
   1717 	atomic_and_ulong(&ci->ci_flags, ~CPUF_FPUSAVE);
   1718 	splx(s);
   1719 #endif
   1720 	return;
   1721 }
   1722 
   1723 /*
   1724  * Synchronize FP state for this process.
   1725  */
   1726 void
   1727 fpusave_proc(struct lwp *l, int save)
   1728 {
   1729 	struct cpu_info *ci = curcpu();
   1730 	struct cpu_info *oci;
   1731 #if defined(MULTIPROCESSOR)
   1732 	u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
   1733 	int s, spincount;
   1734 #endif
   1735 
   1736 	KDASSERT(l->l_addr != NULL);
   1737 
   1738 #if defined(MULTIPROCESSOR)
   1739 	s = splhigh();		/* block IPIs for the duration */
   1740 #endif
   1741 	FPCPU_LOCK(&l->l_addr->u_pcb);
   1742 
   1743 	oci = l->l_addr->u_pcb.pcb_fpcpu;
   1744 	if (oci == NULL) {
   1745 		FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1746 #if defined(MULTIPROCESSOR)
   1747 		splx(s);
   1748 #endif
   1749 		return;
   1750 	}
   1751 
   1752 #if defined(MULTIPROCESSOR)
   1753 	if (oci == ci) {
   1754 		KASSERT(ci->ci_fpcurlwp == l);
   1755 		FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1756 		splx(s);
   1757 		fpusave_cpu(ci, save);
   1758 		return;
   1759 	}
   1760 
   1761 	KASSERT(oci->ci_fpcurlwp == l);
   1762 	alpha_send_ipi(oci->ci_cpuid, ipi);
   1763 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1764 
   1765 	spincount = 0;
   1766 	while (l->l_addr->u_pcb.pcb_fpcpu != NULL) {
   1767 		spincount++;
   1768 		delay(1000);	/* XXX */
   1769 		if (spincount > 10000)
   1770 			panic("fpsave ipi didn't");
   1771 	}
   1772 #else
   1773 	KASSERT(ci->ci_fpcurlwp == l);
   1774 	FPCPU_UNLOCK(&l->l_addr->u_pcb);
   1775 	fpusave_cpu(ci, save);
   1776 #endif /* MULTIPROCESSOR */
   1777 }
   1778 
   1779 /*
   1780  * Wait "n" microseconds.
   1781  */
   1782 void
   1783 delay(n)
   1784 	unsigned long n;
   1785 {
   1786 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
   1787 
   1788 	if (n == 0)
   1789 		return;
   1790 
   1791 	pcc0 = alpha_rpcc() & 0xffffffffUL;
   1792 	cycles = 0;
   1793 	usec = 0;
   1794 
   1795 	while (usec <= n) {
   1796 		/*
   1797 		 * Get the next CPU cycle count- assumes that we cannot
   1798 		 * have had more than one 32 bit overflow.
   1799 		 */
   1800 		pcc1 = alpha_rpcc() & 0xffffffffUL;
   1801 		if (pcc1 < pcc0)
   1802 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
   1803 		else
   1804 			curcycle = pcc1 - pcc0;
   1805 
   1806 		/*
   1807 		 * We now have the number of processor cycles since we
   1808 		 * last checked. Add the current cycle count to the
   1809 		 * running total. If it's over cycles_per_usec, increment
   1810 		 * the usec counter.
   1811 		 */
   1812 		cycles += curcycle;
   1813 		while (cycles > cycles_per_usec) {
   1814 			usec++;
   1815 			cycles -= cycles_per_usec;
   1816 		}
   1817 		pcc0 = pcc1;
   1818 	}
   1819 }
   1820 
   1821 #ifdef EXEC_ECOFF
   1822 void
   1823 cpu_exec_ecoff_setregs(l, epp, stack)
   1824 	struct lwp *l;
   1825 	struct exec_package *epp;
   1826 	u_long stack;
   1827 {
   1828 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1829 
   1830 	l->l_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   1831 }
   1832 
   1833 /*
   1834  * cpu_exec_ecoff_hook():
   1835  *	cpu-dependent ECOFF format hook for execve().
   1836  *
   1837  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   1838  *
   1839  */
   1840 int
   1841 cpu_exec_ecoff_probe(l, epp)
   1842 	struct lwp *l;
   1843 	struct exec_package *epp;
   1844 {
   1845 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1846 	int error;
   1847 
   1848 	if (execp->f.f_magic == ECOFF_MAGIC_NETBSD_ALPHA)
   1849 		error = 0;
   1850 	else
   1851 		error = ENOEXEC;
   1852 
   1853 	return (error);
   1854 }
   1855 #endif /* EXEC_ECOFF */
   1856 
   1857 int
   1858 alpha_pa_access(pa)
   1859 	u_long pa;
   1860 {
   1861 	int i;
   1862 
   1863 	for (i = 0; i < mem_cluster_cnt; i++) {
   1864 		if (pa < mem_clusters[i].start)
   1865 			continue;
   1866 		if ((pa - mem_clusters[i].start) >=
   1867 		    (mem_clusters[i].size & ~PAGE_MASK))
   1868 			continue;
   1869 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
   1870 	}
   1871 
   1872 	/*
   1873 	 * Address is not a memory address.  If we're secure, disallow
   1874 	 * access.  Otherwise, grant read/write.
   1875 	 */
   1876 	if (kauth_authorize_machdep(kauth_cred_get(),
   1877 	    KAUTH_MACHDEP_UNMANAGEDMEM, NULL, NULL, NULL, NULL) != 0)
   1878 		return (PROT_NONE);
   1879 	else
   1880 		return (PROT_READ | PROT_WRITE);
   1881 }
   1882 
   1883 /* XXX XXX BEGIN XXX XXX */
   1884 paddr_t alpha_XXX_dmamap_or;					/* XXX */
   1885 								/* XXX */
   1886 paddr_t								/* XXX */
   1887 alpha_XXX_dmamap(v)						/* XXX */
   1888 	vaddr_t v;						/* XXX */
   1889 {								/* XXX */
   1890 								/* XXX */
   1891 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
   1892 }								/* XXX */
   1893 /* XXX XXX END XXX XXX */
   1894 
   1895 char *
   1896 dot_conv(x)
   1897 	unsigned long x;
   1898 {
   1899 	int i;
   1900 	char *xc;
   1901 	static int next;
   1902 	static char space[2][20];
   1903 
   1904 	xc = space[next ^= 1] + sizeof space[0];
   1905 	*--xc = '\0';
   1906 	for (i = 0;; ++i) {
   1907 		if (i && (i & 3) == 0)
   1908 			*--xc = '.';
   1909 		*--xc = hexdigits[x & 0xf];
   1910 		x >>= 4;
   1911 		if (x == 0)
   1912 			break;
   1913 	}
   1914 	return xc;
   1915 }
   1916 
   1917 void
   1918 cpu_getmcontext(l, mcp, flags)
   1919 	struct lwp *l;
   1920 	mcontext_t *mcp;
   1921 	unsigned int *flags;
   1922 {
   1923 	struct trapframe *frame = l->l_md.md_tf;
   1924 	__greg_t *gr = mcp->__gregs;
   1925 	__greg_t ras_pc;
   1926 
   1927 	/* Save register context. */
   1928 	frametoreg(frame, (struct reg *)gr);
   1929 	/* XXX if there's a better, general way to get the USP of
   1930 	 * an LWP that might or might not be curlwp, I'd like to know
   1931 	 * about it.
   1932 	 */
   1933 	if (l == curlwp) {
   1934 		gr[_REG_SP] = alpha_pal_rdusp();
   1935 		gr[_REG_UNIQUE] = alpha_pal_rdunique();
   1936 	} else {
   1937 		gr[_REG_SP] = l->l_addr->u_pcb.pcb_hw.apcb_usp;
   1938 		gr[_REG_UNIQUE] = l->l_addr->u_pcb.pcb_hw.apcb_unique;
   1939 	}
   1940 	gr[_REG_PC] = frame->tf_regs[FRAME_PC];
   1941 	gr[_REG_PS] = frame->tf_regs[FRAME_PS];
   1942 
   1943 	if ((ras_pc = (__greg_t)ras_lookup(l->l_proc,
   1944 	    (void *) gr[_REG_PC])) != -1)
   1945 		gr[_REG_PC] = ras_pc;
   1946 
   1947 	*flags |= _UC_CPU | _UC_UNIQUE;
   1948 
   1949 	/* Save floating point register context, if any, and copy it. */
   1950 	if (l->l_md.md_flags & MDP_FPUSED) {
   1951 		fpusave_proc(l, 1);
   1952 		(void)memcpy(&mcp->__fpregs, &l->l_addr->u_pcb.pcb_fp,
   1953 		    sizeof (mcp->__fpregs));
   1954 		mcp->__fpregs.__fp_fpcr = alpha_read_fp_c(l);
   1955 		*flags |= _UC_FPU;
   1956 	}
   1957 }
   1958 
   1959 
   1960 int
   1961 cpu_setmcontext(l, mcp, flags)
   1962 	struct lwp *l;
   1963 	const mcontext_t *mcp;
   1964 	unsigned int flags;
   1965 {
   1966 	struct trapframe *frame = l->l_md.md_tf;
   1967 	const __greg_t *gr = mcp->__gregs;
   1968 
   1969 	/* Restore register context, if any. */
   1970 	if (flags & _UC_CPU) {
   1971 		/* Check for security violations first. */
   1972 		if ((gr[_REG_PS] & ALPHA_PSL_USERSET) != ALPHA_PSL_USERSET ||
   1973 		    (gr[_REG_PS] & ALPHA_PSL_USERCLR) != 0)
   1974 			return (EINVAL);
   1975 
   1976 		regtoframe((const struct reg *)gr, l->l_md.md_tf);
   1977 		if (l == curlwp)
   1978 			alpha_pal_wrusp(gr[_REG_SP]);
   1979 		else
   1980 			l->l_addr->u_pcb.pcb_hw.apcb_usp = gr[_REG_SP];
   1981 		frame->tf_regs[FRAME_PC] = gr[_REG_PC];
   1982 		frame->tf_regs[FRAME_PS] = gr[_REG_PS];
   1983 	}
   1984 	if (flags & _UC_UNIQUE) {
   1985 		if (l == curlwp)
   1986 			alpha_pal_wrunique(gr[_REG_UNIQUE]);
   1987 		else
   1988 			l->l_addr->u_pcb.pcb_hw.apcb_unique = gr[_REG_UNIQUE];
   1989 	}
   1990 	/* Restore floating point register context, if any. */
   1991 	if (flags & _UC_FPU) {
   1992 		/* If we have an FP register context, get rid of it. */
   1993 		if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1994 			fpusave_proc(l, 0);
   1995 		(void)memcpy(&l->l_addr->u_pcb.pcb_fp, &mcp->__fpregs,
   1996 		    sizeof (l->l_addr->u_pcb.pcb_fp));
   1997 		l->l_md.md_flags = mcp->__fpregs.__fp_fpcr & MDP_FP_C;
   1998 		l->l_md.md_flags |= MDP_FPUSED;
   1999 	}
   2000 
   2001 	return (0);
   2002 }
   2003 
   2004 /*
   2005  * Preempt the current process if in interrupt from user mode,
   2006  * or after the current trap/syscall if in system mode.
   2007  */
   2008 void
   2009 cpu_need_resched(struct cpu_info *ci, int flags)
   2010 {
   2011 #if defined(MULTIPROCESSOR)
   2012 	bool immed = (flags & RESCHED_IMMED) != 0;
   2013 #endif /* defined(MULTIPROCESSOR) */
   2014 
   2015 	aston(ci->ci_data.cpu_onproc);
   2016 	ci->ci_want_resched = 1;
   2017 	if (ci->ci_data.cpu_onproc != ci->ci_data.cpu_idlelwp) {
   2018 #if defined(MULTIPROCESSOR)
   2019 		if (immed && ci != curcpu()) {
   2020 			alpha_send_ipi(ci->ci_cpuid, 0);
   2021 		}
   2022 #endif /* defined(MULTIPROCESSOR) */
   2023 	}
   2024 }
   2025