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