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