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