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