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