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