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