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