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