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