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