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