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