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