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