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