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