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