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