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