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