Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.126
      1 /* $NetBSD: machdep.c,v 1.126 1998/06/24 01:09:45 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.126 1998/06/24 01:09:45 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 = LOCATE_PCS(hwrpb, i);
    889 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    890 			ncpus++;
    891 	}
    892 
    893 	/*
    894 	 * Figure out our clock frequency, from RPB fields.
    895 	 */
    896 	hz = hwrpb->rpb_intr_freq >> 12;
    897 	if (!(60 <= hz && hz <= 10240)) {
    898 		hz = 1024;
    899 #ifdef DIAGNOSTIC
    900 		printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
    901 			hwrpb->rpb_intr_freq, hz);
    902 #endif
    903 	}
    904 
    905 }
    906 
    907 /*
    908  * Allocate space for system data structures.  We are given
    909  * a starting virtual address and we return a final virtual
    910  * address; along the way we set each data structure pointer.
    911  *
    912  * We call allocsys() with 0 to find out how much space we want,
    913  * allocate that much and fill it with zeroes, and the call
    914  * allocsys() again with the correct base virtual address.
    915  */
    916 caddr_t
    917 allocsys(v)
    918 	caddr_t v;
    919 {
    920 
    921 #define valloc(name, type, num) \
    922 	    (name) = (type *)v; v = (caddr_t)ALIGN((name)+(num))
    923 #ifdef REAL_CLISTS
    924 	valloc(cfree, struct cblock, nclist);
    925 #endif
    926 	valloc(callout, struct callout, ncallout);
    927 #ifdef SYSVSHM
    928 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
    929 #endif
    930 #ifdef SYSVSEM
    931 	valloc(sema, struct semid_ds, seminfo.semmni);
    932 	valloc(sem, struct sem, seminfo.semmns);
    933 	/* This is pretty disgusting! */
    934 	valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
    935 #endif
    936 #ifdef SYSVMSG
    937 	valloc(msgpool, char, msginfo.msgmax);
    938 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
    939 	valloc(msghdrs, struct msg, msginfo.msgtql);
    940 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
    941 #endif
    942 
    943 	/*
    944 	 * Determine how many buffers to allocate.
    945 	 * We allocate 10% of memory for buffer space.  Insure a
    946 	 * minimum of 16 buffers.  We allocate 1/2 as many swap buffer
    947 	 * headers as file i/o buffers.
    948 	 */
    949 	if (bufpages == 0)
    950 		bufpages = (physmem * 10) / (CLSIZE * 100);
    951 	if (nbuf == 0) {
    952 		nbuf = bufpages;
    953 		if (nbuf < 16)
    954 			nbuf = 16;
    955 	}
    956 	if (nswbuf == 0) {
    957 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
    958 		if (nswbuf > 256)
    959 			nswbuf = 256;		/* sanity */
    960 	}
    961 #if !defined(UVM)
    962 	valloc(swbuf, struct buf, nswbuf);
    963 #endif
    964 	valloc(buf, struct buf, nbuf);
    965 	return (v);
    966 #undef valloc
    967 }
    968 
    969 void
    970 consinit()
    971 {
    972 
    973 	/*
    974 	 * Everything related to console initialization is done
    975 	 * in alpha_init().
    976 	 */
    977 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
    978 	printf("consinit: %susing prom console\n",
    979 	    pmap_uses_prom_console() ? "" : "not ");
    980 #endif
    981 }
    982 
    983 #include "pckbc.h"
    984 #include "pckbd.h"
    985 #if (NPCKBC > 0) && (NPCKBD == 0)
    986 
    987 #include <machine/bus.h>
    988 #include <dev/isa/pckbcvar.h>
    989 
    990 /*
    991  * This is called by the pbkbc driver if no pckbd is configured.
    992  * On the i386, it is used to glue in the old, deprecated console
    993  * code.  On the Alpha, it does nothing.
    994  */
    995 int
    996 pckbc_machdep_cnattach(kbctag, kbcslot)
    997 	pckbc_tag_t kbctag;
    998 	pckbc_slot_t kbcslot;
    999 {
   1000 
   1001 	return (ENXIO);
   1002 }
   1003 #endif /* NPCKBC > 0 && NPCKBD == 0 */
   1004 
   1005 void
   1006 cpu_startup()
   1007 {
   1008 	register unsigned i;
   1009 	int base, residual;
   1010 	vm_offset_t minaddr, maxaddr;
   1011 	vm_size_t size;
   1012 #if defined(DEBUG)
   1013 	extern int pmapdebug;
   1014 	int opmapdebug = pmapdebug;
   1015 
   1016 	pmapdebug = 0;
   1017 #endif
   1018 
   1019 	/*
   1020 	 * Good {morning,afternoon,evening,night}.
   1021 	 */
   1022 	printf(version);
   1023 	identifycpu();
   1024 	printf("real mem = %lu (%lu reserved for PROM, %lu used by NetBSD)\n",
   1025 	    ptoa(totalphysmem), ptoa(resvmem), ptoa(physmem));
   1026 	if (unusedmem)
   1027 		printf("WARNING: unused memory = %d bytes\n", ctob(unusedmem));
   1028 	if (unknownmem)
   1029 		printf("WARNING: %d bytes of memory with unknown purpose\n",
   1030 		    ctob(unknownmem));
   1031 
   1032 	/*
   1033 	 * Allocate virtual address space for file I/O buffers.
   1034 	 * Note they are different than the array of headers, 'buf',
   1035 	 * and usually occupy more virtual memory than physical.
   1036 	 */
   1037 	size = MAXBSIZE * nbuf;
   1038 #if defined(UVM)
   1039 	if (uvm_map(kernel_map, (vm_offset_t *) &buffers, round_page(size),
   1040 		    NULL, UVM_UNKNOWN_OFFSET,
   1041 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
   1042 				UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
   1043 		panic("startup: cannot allocate VM for buffers");
   1044 #else
   1045 	buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
   1046 	    &maxaddr, size, TRUE);
   1047 	minaddr = (vm_offset_t)buffers;
   1048 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
   1049 			&minaddr, size, FALSE) != KERN_SUCCESS)
   1050 		panic("startup: cannot allocate buffers");
   1051 #endif /* UVM */
   1052 	base = bufpages / nbuf;
   1053 	residual = bufpages % nbuf;
   1054 	for (i = 0; i < nbuf; i++) {
   1055 #if defined(UVM)
   1056 		vm_size_t curbufsize;
   1057 		vm_offset_t curbuf;
   1058 		struct vm_page *pg;
   1059 
   1060 		/*
   1061 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
   1062 		 * that MAXBSIZE space, we allocate and map (base+1) pages
   1063 		 * for the first "residual" buffers, and then we allocate
   1064 		 * "base" pages for the rest.
   1065 		 */
   1066 		curbuf = (vm_offset_t) buffers + (i * MAXBSIZE);
   1067 		curbufsize = CLBYTES * ((i < residual) ? (base+1) : base);
   1068 
   1069 		while (curbufsize) {
   1070 			pg = uvm_pagealloc(NULL, 0, NULL);
   1071 			if (pg == NULL)
   1072 				panic("cpu_startup: not enough memory for "
   1073 				    "buffer cache");
   1074 #if defined(PMAP_NEW)
   1075 			pmap_kenter_pgs(curbuf, &pg, 1);
   1076 #else
   1077 			pmap_enter(kernel_map->pmap, curbuf,
   1078 				   VM_PAGE_TO_PHYS(pg), VM_PROT_ALL, TRUE);
   1079 #endif
   1080 			curbuf += PAGE_SIZE;
   1081 			curbufsize -= PAGE_SIZE;
   1082 		}
   1083 #else /* ! UVM */
   1084 		vm_size_t curbufsize;
   1085 		vm_offset_t curbuf;
   1086 
   1087 		/*
   1088 		 * First <residual> buffers get (base+1) physical pages
   1089 		 * allocated for them.  The rest get (base) physical pages.
   1090 		 *
   1091 		 * The rest of each buffer occupies virtual space,
   1092 		 * but has no physical memory allocated for it.
   1093 		 */
   1094 		curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
   1095 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
   1096 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
   1097 		vm_map_simplify(buffer_map, curbuf);
   1098 #endif /* UVM */
   1099 	}
   1100 	/*
   1101 	 * Allocate a submap for exec arguments.  This map effectively
   1102 	 * limits the number of processes exec'ing at any time.
   1103 	 */
   1104 #if defined(UVM)
   1105 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
   1106 				   16 * NCARGS, TRUE, FALSE, NULL);
   1107 #else
   1108 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
   1109 				 16 * NCARGS, TRUE);
   1110 #endif
   1111 
   1112 	/*
   1113 	 * Allocate a submap for physio
   1114 	 */
   1115 #if defined(UVM)
   1116 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
   1117 				   VM_PHYS_SIZE, TRUE, FALSE, NULL);
   1118 #else
   1119 	phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
   1120 				 VM_PHYS_SIZE, TRUE);
   1121 #endif
   1122 
   1123 	/*
   1124 	 * Finally, allocate mbuf cluster submap.
   1125 	 */
   1126 #if defined(UVM)
   1127 	mb_map = uvm_km_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
   1128 				 VM_MBUF_SIZE, FALSE, FALSE, NULL);
   1129 #else
   1130 	mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
   1131 			       VM_MBUF_SIZE, FALSE);
   1132 #endif
   1133 	/*
   1134 	 * Initialize callouts
   1135 	 */
   1136 	callfree = callout;
   1137 	for (i = 1; i < ncallout; i++)
   1138 		callout[i-1].c_next = &callout[i];
   1139 	callout[i-1].c_next = NULL;
   1140 
   1141 #if defined(DEBUG)
   1142 	pmapdebug = opmapdebug;
   1143 #endif
   1144 #if defined(UVM)
   1145 	printf("avail mem = %ld\n", (long)ptoa(uvmexp.free));
   1146 #else
   1147 	printf("avail mem = %ld\n", (long)ptoa(cnt.v_free_count));
   1148 #endif
   1149 	printf("using %ld buffers containing %ld bytes of memory\n",
   1150 		(long)nbuf, (long)(bufpages * CLBYTES));
   1151 
   1152 	/*
   1153 	 * Set up buffers, so they can be used to read disk labels.
   1154 	 */
   1155 	bufinit();
   1156 
   1157 	/*
   1158 	 * Configure the system.
   1159 	 */
   1160 	configure();
   1161 
   1162 	/*
   1163 	 * Note that bootstrapping is finished, and set the HWRPB up
   1164 	 * to do restarts.
   1165 	 */
   1166 	hwrpb_restart_setup();
   1167 }
   1168 
   1169 /*
   1170  * Retrieve the platform name from the DSR.
   1171  */
   1172 const char *
   1173 alpha_dsr_sysname()
   1174 {
   1175 	struct dsrdb *dsr;
   1176 	const char *sysname;
   1177 
   1178 	/*
   1179 	 * DSR does not exist on early HWRPB versions.
   1180 	 */
   1181 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
   1182 		return (NULL);
   1183 
   1184 	dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
   1185 	sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
   1186 	    sizeof(u_int64_t)));
   1187 	return (sysname);
   1188 }
   1189 
   1190 /*
   1191  * Lookup the system specified system variation in the provided table,
   1192  * returning the model string on match.
   1193  */
   1194 const char *
   1195 alpha_variation_name(variation, avtp)
   1196 	u_int64_t variation;
   1197 	const struct alpha_variation_table *avtp;
   1198 {
   1199 	int i;
   1200 
   1201 	for (i = 0; avtp[i].avt_model != NULL; i++)
   1202 		if (avtp[i].avt_variation == variation)
   1203 			return (avtp[i].avt_model);
   1204 	return (NULL);
   1205 }
   1206 
   1207 /*
   1208  * Generate a default platform name based for unknown system variations.
   1209  */
   1210 const char *
   1211 alpha_unknown_sysname()
   1212 {
   1213 	static char s[128];		/* safe size */
   1214 
   1215 	sprintf(s, "%s family, unknown model variation 0x%lx",
   1216 	    platform.family, hwrpb->rpb_variation & SV_ST_MASK);
   1217 	return ((const char *)s);
   1218 }
   1219 
   1220 void
   1221 identifycpu()
   1222 {
   1223 
   1224 	/*
   1225 	 * print out CPU identification information.
   1226 	 */
   1227 	printf("%s, %ldMHz\n", cpu_model,
   1228 	    hwrpb->rpb_cc_freq / 1000000);	/* XXX true for 21164? */
   1229 	printf("%ld byte page size, %d processor%s.\n",
   1230 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
   1231 #if 0
   1232 	/* this isn't defined for any systems that we run on? */
   1233 	printf("serial number 0x%lx 0x%lx\n",
   1234 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
   1235 
   1236 	/* and these aren't particularly useful! */
   1237 	printf("variation: 0x%lx, revision 0x%lx\n",
   1238 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
   1239 #endif
   1240 }
   1241 
   1242 int	waittime = -1;
   1243 struct pcb dumppcb;
   1244 
   1245 void
   1246 cpu_reboot(howto, bootstr)
   1247 	int howto;
   1248 	char *bootstr;
   1249 {
   1250 	extern int cold;
   1251 
   1252 	/* If system is cold, just halt. */
   1253 	if (cold) {
   1254 		howto |= RB_HALT;
   1255 		goto haltsys;
   1256 	}
   1257 
   1258 	/* If "always halt" was specified as a boot flag, obey. */
   1259 	if ((boothowto & RB_HALT) != 0)
   1260 		howto |= RB_HALT;
   1261 
   1262 	boothowto = howto;
   1263 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
   1264 		waittime = 0;
   1265 		vfs_shutdown();
   1266 		/*
   1267 		 * If we've been adjusting the clock, the todr
   1268 		 * will be out of synch; adjust it now.
   1269 		 */
   1270 		resettodr();
   1271 	}
   1272 
   1273 	/* Disable interrupts. */
   1274 	splhigh();
   1275 
   1276 	/* If rebooting and a dump is requested do it. */
   1277 #if 0
   1278 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
   1279 #else
   1280 	if (howto & RB_DUMP)
   1281 #endif
   1282 		dumpsys();
   1283 
   1284 haltsys:
   1285 
   1286 	/* run any shutdown hooks */
   1287 	doshutdownhooks();
   1288 
   1289 #ifdef BOOTKEY
   1290 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
   1291 	cnpollc(1);	/* for proper keyboard command handling */
   1292 	cngetc();
   1293 	cnpollc(0);
   1294 	printf("\n");
   1295 #endif
   1296 
   1297 	/* Finally, powerdown/halt/reboot the system. */
   1298 	if ((howto && RB_POWERDOWN) == RB_POWERDOWN &&
   1299 	    platform.powerdown != NULL) {
   1300 		(*platform.powerdown)();
   1301 		printf("WARNING: powerdown failed!\n");
   1302 	}
   1303 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
   1304 	prom_halt(howto & RB_HALT);
   1305 	/*NOTREACHED*/
   1306 }
   1307 
   1308 /*
   1309  * These variables are needed by /sbin/savecore
   1310  */
   1311 u_long	dumpmag = 0x8fca0101;	/* magic number */
   1312 int 	dumpsize = 0;		/* pages */
   1313 long	dumplo = 0; 		/* blocks */
   1314 
   1315 /*
   1316  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
   1317  */
   1318 int
   1319 cpu_dumpsize()
   1320 {
   1321 	int size;
   1322 
   1323 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
   1324 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
   1325 	if (roundup(size, dbtob(1)) != dbtob(1))
   1326 		return -1;
   1327 
   1328 	return (1);
   1329 }
   1330 
   1331 /*
   1332  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
   1333  */
   1334 u_long
   1335 cpu_dump_mempagecnt()
   1336 {
   1337 	u_long i, n;
   1338 
   1339 	n = 0;
   1340 	for (i = 0; i < mem_cluster_cnt; i++)
   1341 		n += atop(mem_clusters[i].size);
   1342 	return (n);
   1343 }
   1344 
   1345 /*
   1346  * cpu_dump: dump machine-dependent kernel core dump headers.
   1347  */
   1348 int
   1349 cpu_dump()
   1350 {
   1351 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1352 	char buf[dbtob(1)];
   1353 	kcore_seg_t *segp;
   1354 	cpu_kcore_hdr_t *cpuhdrp;
   1355 	phys_ram_seg_t *memsegp;
   1356 	int i;
   1357 
   1358 	dump = bdevsw[major(dumpdev)].d_dump;
   1359 
   1360 	bzero(buf, sizeof buf);
   1361 	segp = (kcore_seg_t *)buf;
   1362 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
   1363 	memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
   1364 	    ALIGN(sizeof(*cpuhdrp))];
   1365 
   1366 	/*
   1367 	 * Generate a segment header.
   1368 	 */
   1369 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
   1370 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
   1371 
   1372 	/*
   1373 	 * Add the machine-dependent header info.
   1374 	 */
   1375 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vm_offset_t)kernel_lev1map);
   1376 	cpuhdrp->page_size = PAGE_SIZE;
   1377 	cpuhdrp->nmemsegs = mem_cluster_cnt;
   1378 
   1379 	/*
   1380 	 * Fill in the memory segment descriptors.
   1381 	 */
   1382 	for (i = 0; i < mem_cluster_cnt; i++) {
   1383 		memsegp[i].start = mem_clusters[i].start;
   1384 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
   1385 	}
   1386 
   1387 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
   1388 }
   1389 
   1390 /*
   1391  * This is called by main to set dumplo and dumpsize.
   1392  * Dumps always skip the first CLBYTES of disk space
   1393  * in case there might be a disk label stored there.
   1394  * If there is extra space, put dump at the end to
   1395  * reduce the chance that swapping trashes it.
   1396  */
   1397 void
   1398 cpu_dumpconf()
   1399 {
   1400 	int nblks, dumpblks;	/* size of dump area */
   1401 	int maj;
   1402 
   1403 	if (dumpdev == NODEV)
   1404 		goto bad;
   1405 	maj = major(dumpdev);
   1406 	if (maj < 0 || maj >= nblkdev)
   1407 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
   1408 	if (bdevsw[maj].d_psize == NULL)
   1409 		goto bad;
   1410 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
   1411 	if (nblks <= ctod(1))
   1412 		goto bad;
   1413 
   1414 	dumpblks = cpu_dumpsize();
   1415 	if (dumpblks < 0)
   1416 		goto bad;
   1417 	dumpblks += ctod(cpu_dump_mempagecnt());
   1418 
   1419 	/* If dump won't fit (incl. room for possible label), punt. */
   1420 	if (dumpblks > (nblks - ctod(1)))
   1421 		goto bad;
   1422 
   1423 	/* Put dump at end of partition */
   1424 	dumplo = nblks - dumpblks;
   1425 
   1426 	/* dumpsize is in page units, and doesn't include headers. */
   1427 	dumpsize = cpu_dump_mempagecnt();
   1428 	return;
   1429 
   1430 bad:
   1431 	dumpsize = 0;
   1432 	return;
   1433 }
   1434 
   1435 /*
   1436  * Dump the kernel's image to the swap partition.
   1437  */
   1438 #define	BYTES_PER_DUMP	NBPG
   1439 
   1440 void
   1441 dumpsys()
   1442 {
   1443 	u_long totalbytesleft, bytes, i, n, memcl;
   1444 	u_long maddr;
   1445 	int psize;
   1446 	daddr_t blkno;
   1447 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1448 	int error;
   1449 
   1450 	/* Save registers. */
   1451 	savectx(&dumppcb);
   1452 
   1453 	msgbufenabled = 0;	/* don't record dump msgs in msgbuf */
   1454 	if (dumpdev == NODEV)
   1455 		return;
   1456 
   1457 	/*
   1458 	 * For dumps during autoconfiguration,
   1459 	 * if dump device has already configured...
   1460 	 */
   1461 	if (dumpsize == 0)
   1462 		cpu_dumpconf();
   1463 	if (dumplo <= 0) {
   1464 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
   1465 		    minor(dumpdev));
   1466 		return;
   1467 	}
   1468 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
   1469 	    minor(dumpdev), dumplo);
   1470 
   1471 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
   1472 	printf("dump ");
   1473 	if (psize == -1) {
   1474 		printf("area unavailable\n");
   1475 		return;
   1476 	}
   1477 
   1478 	/* XXX should purge all outstanding keystrokes. */
   1479 
   1480 	if ((error = cpu_dump()) != 0)
   1481 		goto err;
   1482 
   1483 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
   1484 	blkno = dumplo + cpu_dumpsize();
   1485 	dump = bdevsw[major(dumpdev)].d_dump;
   1486 	error = 0;
   1487 
   1488 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
   1489 		maddr = mem_clusters[memcl].start;
   1490 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
   1491 
   1492 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
   1493 
   1494 			/* Print out how many MBs we to go. */
   1495 			if ((totalbytesleft % (1024*1024)) == 0)
   1496 				printf("%d ", totalbytesleft / (1024 * 1024));
   1497 
   1498 			/* Limit size for next transfer. */
   1499 			n = bytes - i;
   1500 			if (n > BYTES_PER_DUMP)
   1501 				n =  BYTES_PER_DUMP;
   1502 
   1503 			error = (*dump)(dumpdev, blkno,
   1504 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
   1505 			if (error)
   1506 				goto err;
   1507 			maddr += n;
   1508 			blkno += btodb(n);			/* XXX? */
   1509 
   1510 			/* XXX should look for keystrokes, to cancel. */
   1511 		}
   1512 	}
   1513 
   1514 err:
   1515 	switch (error) {
   1516 
   1517 	case ENXIO:
   1518 		printf("device bad\n");
   1519 		break;
   1520 
   1521 	case EFAULT:
   1522 		printf("device not ready\n");
   1523 		break;
   1524 
   1525 	case EINVAL:
   1526 		printf("area improper\n");
   1527 		break;
   1528 
   1529 	case EIO:
   1530 		printf("i/o error\n");
   1531 		break;
   1532 
   1533 	case EINTR:
   1534 		printf("aborted from console\n");
   1535 		break;
   1536 
   1537 	case 0:
   1538 		printf("succeeded\n");
   1539 		break;
   1540 
   1541 	default:
   1542 		printf("error %d\n", error);
   1543 		break;
   1544 	}
   1545 	printf("\n\n");
   1546 	delay(1000);
   1547 }
   1548 
   1549 void
   1550 frametoreg(framep, regp)
   1551 	struct trapframe *framep;
   1552 	struct reg *regp;
   1553 {
   1554 
   1555 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1556 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1557 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1558 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1559 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1560 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1561 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1562 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1563 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1564 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1565 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1566 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1567 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1568 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1569 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1570 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1571 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1572 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1573 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1574 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1575 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1576 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1577 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1578 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1579 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1580 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1581 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1582 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1583 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1584 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1585 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1586 	regp->r_regs[R_ZERO] = 0;
   1587 }
   1588 
   1589 void
   1590 regtoframe(regp, framep)
   1591 	struct reg *regp;
   1592 	struct trapframe *framep;
   1593 {
   1594 
   1595 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1596 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1597 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1598 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1599 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1600 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1601 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1602 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1603 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1604 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1605 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1606 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1607 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1608 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1609 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1610 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1611 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1612 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1613 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1614 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1615 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1616 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1617 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1618 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1619 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1620 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1621 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1622 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1623 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1624 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1625 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1626 	/* ??? = regp->r_regs[R_ZERO]; */
   1627 }
   1628 
   1629 void
   1630 printregs(regp)
   1631 	struct reg *regp;
   1632 {
   1633 	int i;
   1634 
   1635 	for (i = 0; i < 32; i++)
   1636 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1637 		   i & 1 ? "\n" : "\t");
   1638 }
   1639 
   1640 void
   1641 regdump(framep)
   1642 	struct trapframe *framep;
   1643 {
   1644 	struct reg reg;
   1645 
   1646 	frametoreg(framep, &reg);
   1647 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1648 
   1649 	printf("REGISTERS:\n");
   1650 	printregs(&reg);
   1651 }
   1652 
   1653 #ifdef DEBUG
   1654 int sigdebug = 0;
   1655 int sigpid = 0;
   1656 #define	SDB_FOLLOW	0x01
   1657 #define	SDB_KSTACK	0x02
   1658 #endif
   1659 
   1660 /*
   1661  * Send an interrupt to process.
   1662  */
   1663 void
   1664 sendsig(catcher, sig, mask, code)
   1665 	sig_t catcher;
   1666 	int sig, mask;
   1667 	u_long code;
   1668 {
   1669 	struct proc *p = curproc;
   1670 	struct sigcontext *scp, ksc;
   1671 	struct trapframe *frame;
   1672 	struct sigacts *psp = p->p_sigacts;
   1673 	int oonstack, fsize, rndfsize;
   1674 	extern char sigcode[], esigcode[];
   1675 	extern struct proc *fpcurproc;
   1676 
   1677 	frame = p->p_md.md_tf;
   1678 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
   1679 	fsize = sizeof ksc;
   1680 	rndfsize = ((fsize + 15) / 16) * 16;
   1681 	/*
   1682 	 * Allocate and validate space for the signal handler
   1683 	 * context. Note that if the stack is in P0 space, the
   1684 	 * call to grow() is a nop, and the useracc() check
   1685 	 * will fail if the process has not already allocated
   1686 	 * the space with a `brk'.
   1687 	 */
   1688 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
   1689 	    (psp->ps_sigonstack & sigmask(sig))) {
   1690 		scp = (struct sigcontext *)((caddr_t)psp->ps_sigstk.ss_sp +
   1691 		    psp->ps_sigstk.ss_size - rndfsize);
   1692 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
   1693 	} else
   1694 		scp = (struct sigcontext *)(alpha_pal_rdusp() - rndfsize);
   1695 	if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
   1696 #if defined(UVM)
   1697 		(void)uvm_grow(p, (u_long)scp);
   1698 #else
   1699 		(void)grow(p, (u_long)scp);
   1700 #endif
   1701 #ifdef DEBUG
   1702 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1703 		printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1704 		    sig, &oonstack, scp);
   1705 #endif
   1706 #if defined(UVM)
   1707 	if (uvm_useracc((caddr_t)scp, fsize, B_WRITE) == 0) {
   1708 #else
   1709 	if (useracc((caddr_t)scp, fsize, B_WRITE) == 0) {
   1710 #endif
   1711 #ifdef DEBUG
   1712 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1713 			printf("sendsig(%d): useracc failed on sig %d\n",
   1714 			    p->p_pid, sig);
   1715 #endif
   1716 		/*
   1717 		 * Process has trashed its stack; give it an illegal
   1718 		 * instruction to halt it in its tracks.
   1719 		 */
   1720 		SIGACTION(p, SIGILL) = SIG_DFL;
   1721 		sig = sigmask(SIGILL);
   1722 		p->p_sigignore &= ~sig;
   1723 		p->p_sigcatch &= ~sig;
   1724 		p->p_sigmask &= ~sig;
   1725 		psignal(p, SIGILL);
   1726 		return;
   1727 #if !defined(UVM)	/* this construct will balance braces for ctags(1) */
   1728 	}
   1729 #else
   1730 	}
   1731 #endif
   1732 
   1733 	/*
   1734 	 * Build the signal context to be used by sigreturn.
   1735 	 */
   1736 	ksc.sc_onstack = oonstack;
   1737 	ksc.sc_mask = mask;
   1738 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
   1739 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
   1740 
   1741 	/* copy the registers. */
   1742 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1743 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1744 	ksc.sc_regs[R_SP] = alpha_pal_rdusp();
   1745 
   1746 	/* save the floating-point state, if necessary, then copy it. */
   1747 	if (p == fpcurproc) {
   1748 		alpha_pal_wrfen(1);
   1749 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
   1750 		alpha_pal_wrfen(0);
   1751 		fpcurproc = NULL;
   1752 	}
   1753 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
   1754 	bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1755 	    sizeof(struct fpreg));
   1756 	ksc.sc_fp_control = 0;					/* XXX ? */
   1757 	bzero(ksc.sc_reserved, sizeof ksc.sc_reserved);		/* XXX */
   1758 	bzero(ksc.sc_xxx, sizeof ksc.sc_xxx);			/* XXX */
   1759 
   1760 
   1761 #ifdef COMPAT_OSF1
   1762 	/*
   1763 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1764 	 */
   1765 #endif
   1766 
   1767 	/*
   1768 	 * copy the frame out to userland.
   1769 	 */
   1770 	(void) copyout((caddr_t)&ksc, (caddr_t)scp, fsize);
   1771 #ifdef DEBUG
   1772 	if (sigdebug & SDB_FOLLOW)
   1773 		printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
   1774 		    scp, code);
   1775 #endif
   1776 
   1777 	/*
   1778 	 * Set up the registers to return to sigcode.
   1779 	 */
   1780 	frame->tf_regs[FRAME_PC] =
   1781 	    (u_int64_t)PS_STRINGS - (esigcode - sigcode);
   1782 	frame->tf_regs[FRAME_A0] = sig;
   1783 	frame->tf_regs[FRAME_A1] = code;
   1784 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
   1785 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
   1786 	alpha_pal_wrusp((unsigned long)scp);
   1787 
   1788 #ifdef DEBUG
   1789 	if (sigdebug & SDB_FOLLOW)
   1790 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1791 		    frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
   1792 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1793 		printf("sendsig(%d): sig %d returns\n",
   1794 		    p->p_pid, sig);
   1795 #endif
   1796 }
   1797 
   1798 /*
   1799  * System call to cleanup state after a signal
   1800  * has been taken.  Reset signal mask and
   1801  * stack state from context left by sendsig (above).
   1802  * Return to previous pc and psl as specified by
   1803  * context left by sendsig. Check carefully to
   1804  * make sure that the user has not modified the
   1805  * psl to gain improper priviledges or to cause
   1806  * a machine fault.
   1807  */
   1808 /* ARGSUSED */
   1809 int
   1810 sys_sigreturn(p, v, retval)
   1811 	struct proc *p;
   1812 	void *v;
   1813 	register_t *retval;
   1814 {
   1815 	struct sys_sigreturn_args /* {
   1816 		syscallarg(struct sigcontext *) sigcntxp;
   1817 	} */ *uap = v;
   1818 	struct sigcontext *scp, ksc;
   1819 	extern struct proc *fpcurproc;
   1820 
   1821 	scp = SCARG(uap, sigcntxp);
   1822 #ifdef DEBUG
   1823 	if (sigdebug & SDB_FOLLOW)
   1824 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
   1825 #endif
   1826 
   1827 	if (ALIGN(scp) != (u_int64_t)scp)
   1828 		return (EINVAL);
   1829 
   1830 	/*
   1831 	 * Test and fetch the context structure.
   1832 	 * We grab it all at once for speed.
   1833 	 */
   1834 #if defined(UVM)
   1835 	if (uvm_useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
   1836 	    copyin((caddr_t)scp, (caddr_t)&ksc, sizeof ksc))
   1837 		return (EINVAL);
   1838 #else
   1839 	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
   1840 	    copyin((caddr_t)scp, (caddr_t)&ksc, sizeof ksc))
   1841 		return (EINVAL);
   1842 #endif
   1843 
   1844 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1845 		return (EINVAL);
   1846 	/*
   1847 	 * Restore the user-supplied information
   1848 	 */
   1849 	if (ksc.sc_onstack)
   1850 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
   1851 	else
   1852 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1853 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
   1854 
   1855 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
   1856 	p->p_md.md_tf->tf_regs[FRAME_PS] =
   1857 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
   1858 
   1859 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
   1860 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
   1861 
   1862 	/* XXX ksc.sc_ownedfp ? */
   1863 	if (p == fpcurproc)
   1864 		fpcurproc = NULL;
   1865 	bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
   1866 	    sizeof(struct fpreg));
   1867 	/* XXX ksc.sc_fp_control ? */
   1868 
   1869 #ifdef DEBUG
   1870 	if (sigdebug & SDB_FOLLOW)
   1871 		printf("sigreturn(%d): returns\n", p->p_pid);
   1872 #endif
   1873 	return (EJUSTRETURN);
   1874 }
   1875 
   1876 /*
   1877  * machine dependent system variables.
   1878  */
   1879 int
   1880 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1881 	int *name;
   1882 	u_int namelen;
   1883 	void *oldp;
   1884 	size_t *oldlenp;
   1885 	void *newp;
   1886 	size_t newlen;
   1887 	struct proc *p;
   1888 {
   1889 	dev_t consdev;
   1890 
   1891 	/* all sysctl names at this level are terminal */
   1892 	if (namelen != 1)
   1893 		return (ENOTDIR);		/* overloaded */
   1894 
   1895 	switch (name[0]) {
   1896 	case CPU_CONSDEV:
   1897 		if (cn_tab != NULL)
   1898 			consdev = cn_tab->cn_dev;
   1899 		else
   1900 			consdev = NODEV;
   1901 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1902 			sizeof consdev));
   1903 
   1904 	case CPU_ROOT_DEVICE:
   1905 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1906 		    root_device->dv_xname));
   1907 
   1908 	case CPU_UNALIGNED_PRINT:
   1909 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1910 		    &alpha_unaligned_print));
   1911 
   1912 	case CPU_UNALIGNED_FIX:
   1913 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1914 		    &alpha_unaligned_fix));
   1915 
   1916 	case CPU_UNALIGNED_SIGBUS:
   1917 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1918 		    &alpha_unaligned_sigbus));
   1919 
   1920 	case CPU_BOOTED_KERNEL:
   1921 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1922 		    bootinfo.booted_kernel));
   1923 
   1924 	default:
   1925 		return (EOPNOTSUPP);
   1926 	}
   1927 	/* NOTREACHED */
   1928 }
   1929 
   1930 /*
   1931  * Set registers on exec.
   1932  */
   1933 void
   1934 setregs(p, pack, stack)
   1935 	register struct proc *p;
   1936 	struct exec_package *pack;
   1937 	u_long stack;
   1938 {
   1939 	struct trapframe *tfp = p->p_md.md_tf;
   1940 	extern struct proc *fpcurproc;
   1941 #ifdef DEBUG
   1942 	int i;
   1943 #endif
   1944 
   1945 #ifdef DEBUG
   1946 	/*
   1947 	 * Crash and dump, if the user requested it.
   1948 	 */
   1949 	if (boothowto & RB_DUMP)
   1950 		panic("crash requested by boot flags");
   1951 #endif
   1952 
   1953 #ifdef DEBUG
   1954 	for (i = 0; i < FRAME_SIZE; i++)
   1955 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1956 #else
   1957 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   1958 #endif
   1959 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
   1960 #define FP_RN 2 /* XXX */
   1961 	p->p_addr->u_pcb.pcb_fp.fpr_cr = (long)FP_RN << 58;
   1962 	alpha_pal_wrusp(stack);
   1963 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   1964 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   1965 
   1966 	tfp->tf_regs[FRAME_A0] = stack;			/* a0 = sp */
   1967 	tfp->tf_regs[FRAME_A1] = 0;			/* a1 = rtld cleanup */
   1968 	tfp->tf_regs[FRAME_A2] = 0;			/* a2 = rtld object */
   1969 	tfp->tf_regs[FRAME_A3] = (u_int64_t)PS_STRINGS;	/* a3 = ps_strings */
   1970 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   1971 
   1972 	p->p_md.md_flags &= ~MDP_FPUSED;
   1973 	if (fpcurproc == p)
   1974 		fpcurproc = NULL;
   1975 }
   1976 
   1977 void
   1978 netintr()
   1979 {
   1980 	int n, s;
   1981 
   1982 	s = splhigh();
   1983 	n = netisr;
   1984 	netisr = 0;
   1985 	splx(s);
   1986 
   1987 #define	DONETISR(bit, fn)						\
   1988 	do {								\
   1989 		if (n & (1 << (bit)))					\
   1990 			fn;						\
   1991 	} while (0)
   1992 
   1993 #ifdef INET
   1994 #if NARP > 0
   1995 	DONETISR(NETISR_ARP, arpintr());
   1996 #endif
   1997 	DONETISR(NETISR_IP, ipintr());
   1998 #endif
   1999 #ifdef NETATALK
   2000 	DONETISR(NETISR_ATALK, atintr());
   2001 #endif
   2002 #ifdef NS
   2003 	DONETISR(NETISR_NS, nsintr());
   2004 #endif
   2005 #ifdef ISO
   2006 	DONETISR(NETISR_ISO, clnlintr());
   2007 #endif
   2008 #ifdef CCITT
   2009 	DONETISR(NETISR_CCITT, ccittintr());
   2010 #endif
   2011 #ifdef NATM
   2012 	DONETISR(NETISR_NATM, natmintr());
   2013 #endif
   2014 #if NPPP > 1
   2015 	DONETISR(NETISR_PPP, pppintr());
   2016 #endif
   2017 
   2018 #undef DONETISR
   2019 }
   2020 
   2021 void
   2022 do_sir()
   2023 {
   2024 	u_int64_t n;
   2025 
   2026 	do {
   2027 		(void)splhigh();
   2028 		n = ssir;
   2029 		ssir = 0;
   2030 		splsoft();		/* don't recurse through spl0() */
   2031 
   2032 #if defined(UVM)
   2033 #define	COUNT_SOFT	uvmexp.softs++
   2034 #else
   2035 #define	COUNT_SOFT	cnt.v_soft++
   2036 #endif
   2037 
   2038 #define	DO_SIR(bit, fn)							\
   2039 		do {							\
   2040 			if (n & (bit)) {				\
   2041 				COUNT_SOFT;				\
   2042 				fn;					\
   2043 			}						\
   2044 		} while (0)
   2045 
   2046 		DO_SIR(SIR_NET, netintr());
   2047 		DO_SIR(SIR_CLOCK, softclock());
   2048 
   2049 #undef COUNT_SOFT
   2050 #undef DO_SIR
   2051 	} while (ssir != 0);
   2052 }
   2053 
   2054 int
   2055 spl0()
   2056 {
   2057 
   2058 	if (ssir)
   2059 		do_sir();		/* it lowers the IPL itself */
   2060 
   2061 	return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
   2062 }
   2063 
   2064 /*
   2065  * The following primitives manipulate the run queues.  _whichqs tells which
   2066  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
   2067  * into queues, Remrunqueue removes them from queues.  The running process is
   2068  * on no queue, other processes are on a queue related to p->p_priority,
   2069  * divided by 4 actually to shrink the 0-127 range of priorities into the 32
   2070  * available queues.
   2071  */
   2072 /*
   2073  * setrunqueue(p)
   2074  *	proc *p;
   2075  *
   2076  * Call should be made at splclock(), and p->p_stat should be SRUN.
   2077  */
   2078 
   2079 void
   2080 setrunqueue(p)
   2081 	struct proc *p;
   2082 {
   2083 	int bit;
   2084 
   2085 	/* firewall: p->p_back must be NULL */
   2086 	if (p->p_back != NULL)
   2087 		panic("setrunqueue");
   2088 
   2089 	bit = p->p_priority >> 2;
   2090 	whichqs |= (1 << bit);
   2091 	p->p_forw = (struct proc *)&qs[bit];
   2092 	p->p_back = qs[bit].ph_rlink;
   2093 	p->p_back->p_forw = p;
   2094 	qs[bit].ph_rlink = p;
   2095 }
   2096 
   2097 /*
   2098  * remrunqueue(p)
   2099  *
   2100  * Call should be made at splclock().
   2101  */
   2102 void
   2103 remrunqueue(p)
   2104 	struct proc *p;
   2105 {
   2106 	int bit;
   2107 
   2108 	bit = p->p_priority >> 2;
   2109 	if ((whichqs & (1 << bit)) == 0)
   2110 		panic("remrunqueue");
   2111 
   2112 	p->p_back->p_forw = p->p_forw;
   2113 	p->p_forw->p_back = p->p_back;
   2114 	p->p_back = NULL;	/* for firewall checking. */
   2115 
   2116 	if ((struct proc *)&qs[bit] == qs[bit].ph_link)
   2117 		whichqs &= ~(1 << bit);
   2118 }
   2119 
   2120 /*
   2121  * Return the best possible estimate of the time in the timeval
   2122  * to which tvp points.  Unfortunately, we can't read the hardware registers.
   2123  * We guarantee that the time will be greater than the value obtained by a
   2124  * previous call.
   2125  */
   2126 void
   2127 microtime(tvp)
   2128 	register struct timeval *tvp;
   2129 {
   2130 	int s = splclock();
   2131 	static struct timeval lasttime;
   2132 
   2133 	*tvp = time;
   2134 #ifdef notdef
   2135 	tvp->tv_usec += clkread();
   2136 	while (tvp->tv_usec > 1000000) {
   2137 		tvp->tv_sec++;
   2138 		tvp->tv_usec -= 1000000;
   2139 	}
   2140 #endif
   2141 	if (tvp->tv_sec == lasttime.tv_sec &&
   2142 	    tvp->tv_usec <= lasttime.tv_usec &&
   2143 	    (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
   2144 		tvp->tv_sec++;
   2145 		tvp->tv_usec -= 1000000;
   2146 	}
   2147 	lasttime = *tvp;
   2148 	splx(s);
   2149 }
   2150 
   2151 /*
   2152  * Wait "n" microseconds.
   2153  */
   2154 void
   2155 delay(n)
   2156 	unsigned long n;
   2157 {
   2158 	long N = cycles_per_usec * (n);
   2159 
   2160 	while (N > 0)				/* XXX */
   2161 		N -= 3;				/* XXX */
   2162 }
   2163 
   2164 #if defined(COMPAT_OSF1) || 1		/* XXX */
   2165 void	cpu_exec_ecoff_setregs __P((struct proc *, struct exec_package *,
   2166 	    u_long));
   2167 
   2168 void
   2169 cpu_exec_ecoff_setregs(p, epp, stack)
   2170 	struct proc *p;
   2171 	struct exec_package *epp;
   2172 	u_long stack;
   2173 {
   2174 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   2175 
   2176 	setregs(p, epp, stack);
   2177 	p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   2178 }
   2179 
   2180 /*
   2181  * cpu_exec_ecoff_hook():
   2182  *	cpu-dependent ECOFF format hook for execve().
   2183  *
   2184  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   2185  *
   2186  */
   2187 int
   2188 cpu_exec_ecoff_hook(p, epp)
   2189 	struct proc *p;
   2190 	struct exec_package *epp;
   2191 {
   2192 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   2193 	extern struct emul emul_netbsd;
   2194 #ifdef COMPAT_OSF1
   2195 	extern struct emul emul_osf1;
   2196 #endif
   2197 
   2198 	switch (execp->f.f_magic) {
   2199 #ifdef COMPAT_OSF1
   2200 	case ECOFF_MAGIC_ALPHA:
   2201 		epp->ep_emul = &emul_osf1;
   2202 		break;
   2203 #endif
   2204 
   2205 	case ECOFF_MAGIC_NETBSD_ALPHA:
   2206 		epp->ep_emul = &emul_netbsd;
   2207 		break;
   2208 
   2209 	default:
   2210 		return ENOEXEC;
   2211 	}
   2212 	return 0;
   2213 }
   2214 #endif
   2215 
   2216 int
   2217 alpha_pa_access(pa)
   2218 	u_long pa;
   2219 {
   2220 	int i;
   2221 
   2222 	for (i = 0; i < mem_cluster_cnt; i++) {
   2223 		if (pa < mem_clusters[i].start)
   2224 			continue;
   2225 		if ((pa - mem_clusters[i].start) >=
   2226 		    (mem_clusters[i].size & ~PAGE_MASK))
   2227 			continue;
   2228 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
   2229 	}
   2230 	return (PROT_NONE);
   2231 }
   2232 
   2233 /* XXX XXX BEGIN XXX XXX */
   2234 vm_offset_t alpha_XXX_dmamap_or;				/* XXX */
   2235 								/* XXX */
   2236 vm_offset_t							/* XXX */
   2237 alpha_XXX_dmamap(v)						/* XXX */
   2238 	vm_offset_t v;						/* XXX */
   2239 {								/* XXX */
   2240 								/* XXX */
   2241 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
   2242 }								/* XXX */
   2243 /* XXX XXX END XXX XXX */
   2244