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