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