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