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