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