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