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