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