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