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