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