Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.26
      1 /*	$NetBSD: machdep.c,v 1.26 1996/06/13 00:24:52 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/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/signalvar.h>
     33 #include <sys/kernel.h>
     34 #include <sys/map.h>
     35 #include <sys/proc.h>
     36 #include <sys/buf.h>
     37 #include <sys/reboot.h>
     38 #include <sys/conf.h>
     39 #include <sys/file.h>
     40 #ifdef REAL_CLISTS
     41 #include <sys/clist.h>
     42 #endif
     43 #include <sys/callout.h>
     44 #include <sys/malloc.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/msgbuf.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/tty.h>
     49 #include <sys/user.h>
     50 #include <sys/exec.h>
     51 #include <sys/exec_ecoff.h>
     52 #include <sys/sysctl.h>
     53 #ifdef SYSVMSG
     54 #include <sys/msg.h>
     55 #endif
     56 #ifdef SYSVSEM
     57 #include <sys/sem.h>
     58 #endif
     59 #ifdef SYSVSHM
     60 #include <sys/shm.h>
     61 #endif
     62 
     63 #include <sys/mount.h>
     64 #include <sys/syscallargs.h>
     65 
     66 #include <vm/vm_kern.h>
     67 
     68 #include <dev/cons.h>
     69 
     70 #include <machine/cpu.h>
     71 #include <machine/reg.h>
     72 #include <machine/rpb.h>
     73 #include <machine/prom.h>
     74 
     75 #ifdef DEC_3000_500
     76 #include <alpha/alpha/dec_3000_500.h>
     77 #endif
     78 #ifdef DEC_3000_300
     79 #include <alpha/alpha/dec_3000_300.h>
     80 #endif
     81 #ifdef DEC_2100_A50
     82 #include <alpha/alpha/dec_2100_a50.h>
     83 #endif
     84 #ifdef DEC_KN20AA
     85 #include <alpha/alpha/dec_kn20aa.h>
     86 #endif
     87 #ifdef DEC_AXPPCI_33
     88 #include <alpha/alpha/dec_axppci_33.h>
     89 #endif
     90 #ifdef DEC_21000
     91 #include <alpha/alpha/dec_21000.h>
     92 #endif
     93 
     94 #include <net/netisr.h>
     95 #include "ether.h"
     96 
     97 #include "le_ioasic.h"			/* for le_iomem creation */
     98 
     99 vm_map_t buffer_map;
    100 
    101 void dumpsys __P((void));
    102 
    103 /*
    104  * Declare these as initialized data so we can patch them.
    105  */
    106 int	nswbuf = 0;
    107 #ifdef	NBUF
    108 int	nbuf = NBUF;
    109 #else
    110 int	nbuf = 0;
    111 #endif
    112 #ifdef	BUFPAGES
    113 int	bufpages = BUFPAGES;
    114 #else
    115 int	bufpages = 0;
    116 #endif
    117 int	msgbufmapped = 0;	/* set when safe to use msgbuf */
    118 int	maxmem;			/* max memory per process */
    119 
    120 int	totalphysmem;		/* total amount of physical memory in system */
    121 int	physmem;		/* physical memory used by NetBSD + some rsvd */
    122 int	firstusablepage;	/* first usable memory page */
    123 int	lastusablepage;		/* last usable memory page */
    124 int	resvmem;		/* amount of memory reserved for PROM */
    125 int	unusedmem;		/* amount of memory for OS that we don't use */
    126 int	unknownmem;		/* amount of memory with an unknown use */
    127 
    128 int	cputype;		/* system type, from the RPB */
    129 
    130 /*
    131  * XXX We need an address to which we can assign things so that they
    132  * won't be optimized away because we didn't use the value.
    133  */
    134 u_int32_t no_optimize;
    135 
    136 /* the following is used externally (sysctl_hw) */
    137 char	machine[] = "alpha";
    138 char	*cpu_model;
    139 char	*model_names[] = {
    140 	"UNKNOWN (0)",
    141 	"Alpha Demonstration Unit",
    142 	"DEC 4000 (\"Cobra\")",
    143 	"DEC 7000 (\"Ruby\")",
    144 	"DEC 3000/500 (\"Flamingo\") family",
    145 	"UNKNOWN (5)",
    146 	"DEC 2000/300 (\"Jensen\")",
    147 	"DEC 3000/300 (\"Pelican\")",
    148 	"UNKNOWN (8)",
    149 	"DEC 2100/A500 (\"Sable\")",
    150 	"AXPvme 64",
    151 	"AXPpci 33 (\"NoName\")",
    152 	"DEC 21000 (\"TurboLaser\")",
    153 	"DEC 2100/A50 (\"Avanti\") family",
    154 	"Mustang",
    155 	"DEC KN20AA",
    156 	"UNKNOWN (16)",
    157 	"DEC 1000 (\"Mikasa\")",
    158 };
    159 int	nmodel_names = sizeof model_names/sizeof model_names[0];
    160 
    161 struct	user *proc0paddr;
    162 
    163 /* Number of machine cycles per microsecond */
    164 u_int64_t	cycles_per_usec;
    165 
    166 /* some memory areas for device DMA.  "ick." */
    167 caddr_t		le_iomem;		/* XXX iomem for LANCE DMA */
    168 
    169 /* Interrupt vectors (in locore) */
    170 extern int XentInt(), XentArith(), XentMM(), XentIF(), XentUna(), XentSys();
    171 
    172 /* number of cpus in the box.  really! */
    173 int		ncpus;
    174 
    175 /* various CPU-specific functions. */
    176 char		*(*cpu_modelname) __P((void));
    177 void		(*cpu_consinit) __P((void));
    178 char		*cpu_iobus;
    179 
    180 char boot_flags[64];
    181 
    182 int
    183 alpha_init(pfn, ptb)
    184 	u_long pfn;		/* first free PFN number */
    185 	u_long ptb;		/* PFN of current level 1 page table */
    186 {
    187 	extern char _end[];
    188 	caddr_t start, v;
    189 	struct mddt *mddtp;
    190 	int i, mddtweird;
    191 	char *p;
    192 
    193 	/*
    194 	 * Turn off interrupts and floating point.
    195 	 * Make sure the instruction and data streams are consistent.
    196 	 */
    197 	(void)splhigh();
    198 	pal_wrfen(0);
    199 	TBIA();
    200 	IMB();
    201 
    202 	/*
    203 	 * get address of the restart block, while we the bootstrap
    204 	 * mapping is still around.
    205 	 */
    206 	hwrpb = (struct rpb *) phystok0seg(*(struct rpb **)HWRPB_ADDR);
    207 
    208 	/*
    209 	 * Remember how many cycles there are per microsecond,
    210 	 * so that we can use delay().  Round up, for safety.
    211 	 */
    212 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
    213 
    214 	/*
    215 	 * Init the PROM interface, so we can use printf
    216 	 * until PROM mappings go away in consinit.
    217 	 */
    218 	init_prom_interface();
    219 
    220 	/*
    221 	 * Point interrupt/exception vectors to our own.
    222 	 */
    223 	pal_wrent(XentInt, 0);
    224 	pal_wrent(XentArith, 1);
    225 	pal_wrent(XentMM, 2);
    226 	pal_wrent(XentIF, 3);
    227 	pal_wrent(XentUna, 4);
    228 	pal_wrent(XentSys, 5);
    229 
    230 	/*
    231 	 * Find out how much memory is available, by looking at
    232 	 * the memory cluster descriptors.  This also tries to do
    233 	 * its best to detect things things that have never been seen
    234 	 * before...
    235 	 *
    236 	 * XXX Assumes that the first "system" cluster is the
    237 	 * only one we can use. Is the second (etc.) system cluster
    238 	 * (if one happens to exist) guaranteed to be contiguous?  or...?
    239 	 */
    240 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
    241 
    242 	/*
    243 	 * BEGIN MDDT WEIRDNESS CHECKING
    244 	 */
    245 	mddtweird = 0;
    246 
    247 #define cnt	 mddtp->mddt_cluster_cnt
    248 #define	usage(n) mddtp->mddt_clusters[(n)].mddt_usage
    249 	if (cnt != 2 && cnt != 3) {
    250 		printf("WARNING: weird number (%d) of mem clusters\n", cnt);
    251 		mddtweird = 1;
    252 	} else if (usage(0) != MDDT_PALCODE ||
    253 		   usage(1) != MDDT_SYSTEM ||
    254 	           (cnt == 3 && usage(2) != MDDT_PALCODE)) {
    255 		mddtweird = 1;
    256 		printf("WARNING: %d mem clusters, but weird config\n", cnt);
    257 	}
    258 
    259 	for (i = 0; i < cnt; i++) {
    260 		if ((usage(i) & MDDT_mbz) != 0) {
    261 			printf("WARNING: mem cluster %d has weird usage %lx\n",
    262 			    i, usage(i));
    263 			mddtweird = 1;
    264 		}
    265 		if (mddtp->mddt_clusters[i].mddt_pg_cnt == 0) {
    266 			printf("WARNING: mem cluster %d has pg cnt == 0\n", i);
    267 			mddtweird = 1;
    268 		}
    269 		/* XXX other things to check? */
    270 	}
    271 #undef cnt
    272 #undef usage
    273 
    274 	if (mddtweird) {
    275 		printf("\n");
    276 		printf("complete memory cluster information:\n");
    277 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    278 			printf("mddt %d:\n", i);
    279 			printf("\tpfn %lx\n",
    280 			    mddtp->mddt_clusters[i].mddt_pfn);
    281 			printf("\tcnt %lx\n",
    282 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
    283 			printf("\ttest %lx\n",
    284 			    mddtp->mddt_clusters[i].mddt_pg_test);
    285 			printf("\tbva %lx\n",
    286 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
    287 			printf("\tbpa %lx\n",
    288 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
    289 			printf("\tbcksum %lx\n",
    290 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
    291 			printf("\tusage %lx\n",
    292 			    mddtp->mddt_clusters[i].mddt_usage);
    293 		}
    294 		printf("\n");
    295 	}
    296 	/*
    297 	 * END MDDT WEIRDNESS CHECKING
    298 	 */
    299 
    300 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    301 		totalphysmem += mddtp->mddt_clusters[i].mddt_pg_cnt;
    302 #define	usage(n) mddtp->mddt_clusters[(n)].mddt_usage
    303 #define	pgcnt(n) mddtp->mddt_clusters[(n)].mddt_pg_cnt
    304 		if ((usage(i) & MDDT_mbz) != 0)
    305 			unknownmem += pgcnt(i);
    306 		else if ((usage(i) & ~MDDT_mbz) == MDDT_PALCODE)
    307 			resvmem += pgcnt(i);
    308 		else if ((usage(i) & ~MDDT_mbz) == MDDT_SYSTEM) {
    309 			/*
    310 			 * assumes that the system cluster listed is
    311 			 * one we're in...
    312 			 */
    313 			if (physmem != resvmem) {
    314 				physmem += pgcnt(i);
    315 				firstusablepage =
    316 				    mddtp->mddt_clusters[i].mddt_pfn;
    317 				lastusablepage = firstusablepage + pgcnt(i) - 1;
    318 			} else
    319 				unusedmem += pgcnt(i);
    320 		}
    321 #undef usage
    322 #undef pgcnt
    323 	}
    324 	if (totalphysmem == 0)
    325 		panic("can't happen: system seems to have no memory!");
    326 	maxmem = physmem;
    327 
    328 #if 0
    329 	printf("totalphysmem = %d\n", totalphysmem);
    330 	printf("physmem = %d\n", physmem);
    331 	printf("firstusablepage = %d\n", firstusablepage);
    332 	printf("lastusablepage = %d\n", lastusablepage);
    333 	printf("resvmem = %d\n", resvmem);
    334 	printf("unusedmem = %d\n", unusedmem);
    335 	printf("unknownmem = %d\n", unknownmem);
    336 #endif
    337 
    338 	/*
    339 	 * find out this CPU's page size
    340 	 */
    341 	PAGE_SIZE = hwrpb->rpb_page_size;
    342 	if (PAGE_SIZE != 8192)
    343 		panic("page size %d != 8192?!", PAGE_SIZE);
    344 
    345 	v = (caddr_t)alpha_round_page(_end);
    346 	/*
    347 	 * Init mapping for u page(s) for proc 0
    348 	 */
    349 	start = v;
    350 	curproc->p_addr = proc0paddr = (struct user *)v;
    351 	v += UPAGES * NBPG;
    352 
    353 	/*
    354 	 * Find out what hardware we're on, and remember its type name.
    355 	 */
    356 	cputype = hwrpb->rpb_type;
    357 	switch (cputype) {
    358 #ifdef DEC_3000_500				/* and 400, [6-9]00 */
    359 	case ST_DEC_3000_500:
    360 		cpu_modelname = dec_3000_500_modelname;
    361 		cpu_consinit = dec_3000_500_consinit;
    362 		cpu_iobus = "tcasic";
    363 		break;
    364 #endif
    365 
    366 #ifdef DEC_3000_300
    367 	case ST_DEC_3000_300:
    368 		cpu_modelname = dec_3000_300_modelname;
    369 		cpu_consinit = dec_3000_300_consinit;
    370 		cpu_iobus = "tcasic";
    371 		break;
    372 #endif
    373 
    374 #ifdef DEC_2100_A50
    375 	case ST_DEC_2100_A50:
    376 		cpu_modelname = dec_2100_a50_modelname;
    377 		cpu_consinit = dec_2100_a50_consinit;
    378 		cpu_iobus = "apecs";
    379 		break;
    380 #endif
    381 
    382 #ifdef DEC_KN20AA
    383 	case ST_DEC_KN20AA:
    384 		cpu_modelname = dec_kn20aa_modelname;
    385 		cpu_consinit = dec_kn20aa_consinit;
    386 		cpu_iobus = "cia";
    387 		break;
    388 #endif
    389 
    390 #ifdef DEC_AXPPCI_33
    391 	case ST_DEC_AXPPCI_33:
    392 		cpu_modelname = dec_axppci_33_modelname;
    393 		cpu_consinit = dec_axppci_33_consinit;
    394 		cpu_iobus = "lca";
    395 		break;
    396 #endif
    397 
    398 #ifdef DEC_2000_300
    399 	case ST_DEC_2000_300:
    400 		cpu_modelname = dec_2000_300_modelname;
    401 		cpu_consinit = dec_2000_300_consinit;
    402 		cpu_iobus = "ibus";
    403 	XXX DEC 2000/300 NOT SUPPORTED
    404 		break;
    405 #endif
    406 
    407 #ifdef DEC_21000
    408 	case ST_DEC_21000:
    409 		cpu_modelname = dec_21000_modelname;
    410 		cpu_consinit = dec_21000_consinit;
    411 		cpu_iobus = "tlsb";
    412 		break;
    413 #endif
    414 
    415 	default:
    416 		if (cputype > nmodel_names)
    417 			panic("Unknown system type %d", cputype);
    418 		else
    419 			panic("Support for %s system type not in kernel.",
    420 			    model_names[cputype]);
    421 	}
    422 
    423 	cpu_model = (*cpu_modelname)();
    424 	if (cpu_model == NULL)
    425 		cpu_model = model_names[cputype];
    426 
    427 #if NLE_IOASIC > 0
    428 	/*
    429 	 * Grab 128K at the top of physical memory for the lance chip
    430 	 * on machines where it does dma through the I/O ASIC.
    431 	 * It must be physically contiguous and aligned on a 128K boundary.
    432 	 *
    433 	 * Note that since this is conditional on the presence of
    434 	 * IOASIC-attached 'le' units in the kernel config, the
    435 	 * message buffer may move on these systems.  This shouldn't
    436 	 * be a problem, because once people have a kernel config that
    437 	 * they use, they're going to stick with it.
    438 	 */
    439 	if (cputype == ST_DEC_3000_500 ||
    440 	    cputype == ST_DEC_3000_300) {	/* XXX possibly others? */
    441 		lastusablepage -= btoc(128 * 1024);
    442 		le_iomem = (caddr_t)phystok0seg(ctob(lastusablepage + 1));
    443 	}
    444 #endif /* NLE_IOASIC */
    445 
    446 	/*
    447 	 * Initialize error message buffer (at end of core).
    448 	 */
    449 	lastusablepage -= btoc(sizeof (struct msgbuf));
    450 	msgbufp = (struct msgbuf *)phystok0seg(ctob(lastusablepage + 1));
    451 	msgbufmapped = 1;
    452 
    453 	/*
    454 	 * Allocate space for system data structures.
    455 	 * The first available kernel virtual address is in "v".
    456 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
    457 	 *
    458 	 * These data structures are allocated here instead of cpu_startup()
    459 	 * because physical memory is directly addressable. We don't have
    460 	 * to map these into virtual address space.
    461 	 */
    462 #define valloc(name, type, num) \
    463 	    (name) = (type *)v; v = (caddr_t)ALIGN((name)+(num))
    464 #define valloclim(name, type, num, lim) \
    465 	    (name) = (type *)v; v = (caddr_t)ALIGN((lim) = ((name)+(num)))
    466 #ifdef REAL_CLISTS
    467 	valloc(cfree, struct cblock, nclist);
    468 #endif
    469 	valloc(callout, struct callout, ncallout);
    470 	valloc(swapmap, struct map, nswapmap = maxproc * 2);
    471 #ifdef SYSVSHM
    472 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
    473 #endif
    474 #ifdef SYSVSEM
    475 	valloc(sema, struct semid_ds, seminfo.semmni);
    476 	valloc(sem, struct sem, seminfo.semmns);
    477 	/* This is pretty disgusting! */
    478 	valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
    479 #endif
    480 #ifdef SYSVMSG
    481 	valloc(msgpool, char, msginfo.msgmax);
    482 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
    483 	valloc(msghdrs, struct msg, msginfo.msgtql);
    484 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
    485 #endif
    486 
    487 	/*
    488 	 * Determine how many buffers to allocate.
    489 	 * We allocate the BSD standard of 10% of memory for the first
    490 	 * 2 Meg, and 5% of remaining memory for buffer space.  Insure a
    491 	 * minimum of 16 buffers.  We allocate 1/2 as many swap buffer
    492 	 * headers as file i/o buffers.
    493 	 */
    494 	if (bufpages == 0)
    495 		bufpages = (btoc(2 * 1024 * 1024) + physmem) /
    496 		    (20 * CLSIZE);
    497 	if (nbuf == 0) {
    498 		nbuf = bufpages;
    499 		if (nbuf < 16)
    500 			nbuf = 16;
    501 	}
    502 	if (nswbuf == 0) {
    503 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
    504 		if (nswbuf > 256)
    505 			nswbuf = 256;		/* sanity */
    506 	}
    507 	valloc(swbuf, struct buf, nswbuf);
    508 	valloc(buf, struct buf, nbuf);
    509 
    510 	/*
    511 	 * Clear allocated memory.
    512 	 */
    513 	bzero(start, v - start);
    514 
    515 	/*
    516 	 * Initialize the virtual memory system, and set the
    517 	 * page table base register in proc 0's PCB.
    518 	 */
    519 	pmap_bootstrap((vm_offset_t)v, phystok0seg(ptb << PGSHIFT));
    520 
    521 	/*
    522 	 * Initialize the rest of proc 0's PCB, and cache its physical
    523 	 * address.
    524 	 */
    525 	proc0.p_md.md_pcbpaddr =
    526 	    (struct pcb *)k0segtophys(&proc0paddr->u_pcb);
    527 
    528 	/*
    529 	 * Set the kernel sp, reserving space for an (empty) trapframe,
    530 	 * and make proc0's trapframe pointer point to it for sanity.
    531 	 */
    532 	proc0paddr->u_pcb.pcb_ksp =
    533 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
    534 	proc0.p_md.md_tf = (struct trapframe *)proc0paddr->u_pcb.pcb_ksp;
    535 
    536 	/*
    537 	 * Look at arguments passed to us and compute boothowto.
    538 	 */
    539 	prom_getenv(PROM_E_BOOTED_OSFLAGS, boot_flags, sizeof(boot_flags));
    540 #if 0
    541 	printf("boot flags = \"%s\"\n", boot_flags);
    542 #endif
    543 
    544 	boothowto = RB_SINGLE;
    545 #ifdef KADB
    546 	boothowto |= RB_KDB;
    547 #endif
    548 	for (p = boot_flags; p && *p != '\0'; p++) {
    549 		/*
    550 		 * Note that we'd really like to differentiate case here,
    551 		 * but the Alpha AXP Architecture Reference Manual
    552 		 * says that we shouldn't.
    553 		 */
    554 		switch (*p) {
    555 		case 'a': /* autoboot */
    556 		case 'A':
    557 			boothowto &= ~RB_SINGLE;
    558 			break;
    559 
    560 		case 'n': /* askname */
    561 		case 'N':
    562 			boothowto |= RB_ASKNAME;
    563 			break;
    564 
    565 #if 0
    566 		case 'm': /* mini root present in memory */
    567 		case 'M':
    568 			boothowto |= RB_MINIROOT;
    569 			break;
    570 #endif
    571 		}
    572 	}
    573 
    574 	/*
    575 	 * Figure out the number of cpus in the box, from RPB fields.
    576 	 * Really.  We mean it.
    577 	 */
    578 	for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
    579 		struct pcs *pcsp;
    580 
    581 		pcsp = (struct pcs *)((char *)hwrpb + hwrpb->rpb_pcs_off +
    582 		    (i * hwrpb->rpb_pcs_size));
    583 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    584 			ncpus++;
    585 	}
    586 
    587 	return (0);
    588 }
    589 
    590 void
    591 consinit()
    592 {
    593 
    594 	(*cpu_consinit)();
    595 	pmap_unmap_prom();
    596 }
    597 
    598 void
    599 cpu_startup()
    600 {
    601 	register unsigned i;
    602 	register caddr_t v;
    603 	int base, residual;
    604 	vm_offset_t minaddr, maxaddr;
    605 	vm_size_t size;
    606 #ifdef DEBUG
    607 	extern int pmapdebug;
    608 	int opmapdebug = pmapdebug;
    609 
    610 	pmapdebug = 0;
    611 #endif
    612 
    613 	/*
    614 	 * Good {morning,afternoon,evening,night}.
    615 	 */
    616 	printf(version);
    617 	identifycpu();
    618 	printf("real mem = %d (%d reserved for PROM, %d used by NetBSD)\n",
    619 	    ctob(totalphysmem), ctob(resvmem), ctob(physmem));
    620 	if (unusedmem)
    621 		printf("WARNING: unused memory = %d bytes\n", ctob(unusedmem));
    622 	if (unknownmem)
    623 		printf("WARNING: %d bytes of memory with unknown purpose\n",
    624 		    ctob(unknownmem));
    625 
    626 	/*
    627 	 * Allocate virtual address space for file I/O buffers.
    628 	 * Note they are different than the array of headers, 'buf',
    629 	 * and usually occupy more virtual memory than physical.
    630 	 */
    631 	size = MAXBSIZE * nbuf;
    632 	buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
    633 	    &maxaddr, size, TRUE);
    634 	minaddr = (vm_offset_t)buffers;
    635 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
    636 			&minaddr, size, FALSE) != KERN_SUCCESS)
    637 		panic("startup: cannot allocate buffers");
    638 	base = bufpages / nbuf;
    639 	residual = bufpages % nbuf;
    640 	for (i = 0; i < nbuf; i++) {
    641 		vm_size_t curbufsize;
    642 		vm_offset_t curbuf;
    643 
    644 		/*
    645 		 * First <residual> buffers get (base+1) physical pages
    646 		 * allocated for them.  The rest get (base) physical pages.
    647 		 *
    648 		 * The rest of each buffer occupies virtual space,
    649 		 * but has no physical memory allocated for it.
    650 		 */
    651 		curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
    652 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
    653 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
    654 		vm_map_simplify(buffer_map, curbuf);
    655 	}
    656 	/*
    657 	 * Allocate a submap for exec arguments.  This map effectively
    658 	 * limits the number of processes exec'ing at any time.
    659 	 */
    660 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    661 				 16 * NCARGS, TRUE);
    662 
    663 	/*
    664 	 * Allocate a submap for physio
    665 	 */
    666 	phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
    667 				 VM_PHYS_SIZE, TRUE);
    668 
    669 	/*
    670 	 * Finally, allocate mbuf pool.  Since mclrefcnt is an off-size
    671 	 * we use the more space efficient malloc in place of kmem_alloc.
    672 	 */
    673 	mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES,
    674 	    M_MBUF, M_NOWAIT);
    675 	bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES);
    676 	mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
    677 	    VM_MBUF_SIZE, FALSE);
    678 	/*
    679 	 * Initialize callouts
    680 	 */
    681 	callfree = callout;
    682 	for (i = 1; i < ncallout; i++)
    683 		callout[i-1].c_next = &callout[i];
    684 	callout[i-1].c_next = NULL;
    685 
    686 #ifdef DEBUG
    687 	pmapdebug = opmapdebug;
    688 #endif
    689 	printf("avail mem = %ld\n", (long)ptoa(cnt.v_free_count));
    690 	printf("using %ld buffers containing %ld bytes of memory\n",
    691 		(long)nbuf, (long)(bufpages * CLBYTES));
    692 
    693 	/*
    694 	 * Set up buffers, so they can be used to read disk labels.
    695 	 */
    696 	bufinit();
    697 
    698 	/*
    699 	 * Configure the system.
    700 	 */
    701 	configure();
    702 }
    703 
    704 identifycpu()
    705 {
    706 
    707 	/*
    708 	 * print out CPU identification information.
    709 	 */
    710 	printf("%s, %dMHz\n", cpu_model,
    711 	    hwrpb->rpb_cc_freq / 1000000);	/* XXX true for 21164? */
    712 	printf("%d byte page size, %d processor%s.\n",
    713 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
    714 #if 0
    715 	/* this isn't defined for any systems that we run on? */
    716 	printf("serial number 0x%lx 0x%lx\n",
    717 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
    718 
    719 	/* and these aren't particularly useful! */
    720 	printf("variation: 0x%lx, revision 0x%lx\n",
    721 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
    722 #endif
    723 }
    724 
    725 int	waittime = -1;
    726 struct pcb dumppcb;
    727 
    728 void
    729 boot(howto)
    730 	int howto;
    731 {
    732 	extern int cold;
    733 
    734 	/* If system is cold, just halt. */
    735 	if (cold) {
    736 		howto |= RB_HALT;
    737 		goto haltsys;
    738 	}
    739 
    740 	boothowto = howto;
    741 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
    742 		waittime = 0;
    743 		vfs_shutdown();
    744 		/*
    745 		 * If we've been adjusting the clock, the todr
    746 		 * will be out of synch; adjust it now.
    747 		 */
    748 		resettodr();
    749 	}
    750 
    751 	/* Disable interrupts. */
    752 	splhigh();
    753 
    754 	/* If rebooting and a dump is requested do it. */
    755 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) {
    756 		savectx(&dumppcb, 0);
    757 		dumpsys();
    758 	}
    759 
    760 haltsys:
    761 
    762 	/* run any shutdown hooks */
    763 	doshutdownhooks();
    764 
    765 #ifdef BOOTKEY
    766 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
    767 	cngetc();
    768 	printf("\n");
    769 #endif
    770 
    771 	/* Finally, halt/reboot the system. */
    772 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
    773 	prom_halt(howto & RB_HALT);
    774 	/*NOTREACHED*/
    775 }
    776 
    777 /*
    778  * These variables are needed by /sbin/savecore
    779  */
    780 u_long	dumpmag = 0x8fca0101;	/* magic number */
    781 int 	dumpsize = 0;		/* pages */
    782 long	dumplo = 0; 		/* blocks */
    783 
    784 /*
    785  * This is called by configure to set dumplo and dumpsize.
    786  * Dumps always skip the first CLBYTES of disk space
    787  * in case there might be a disk label stored there.
    788  * If there is extra space, put dump at the end to
    789  * reduce the chance that swapping trashes it.
    790  */
    791 void
    792 dumpconf()
    793 {
    794 	int nblks;	/* size of dump area */
    795 	int maj;
    796 
    797 	if (dumpdev == NODEV)
    798 		return;
    799 	maj = major(dumpdev);
    800 	if (maj < 0 || maj >= nblkdev)
    801 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
    802 	if (bdevsw[maj].d_psize == NULL)
    803 		return;
    804 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
    805 	if (nblks <= ctod(1))
    806 		return;
    807 
    808 	/* XXX XXX XXX STARTING MEMORY LOCATION */
    809 	dumpsize = physmem;
    810 
    811 	/* Always skip the first CLBYTES, in case there is a label there. */
    812 	if (dumplo < ctod(1))
    813 		dumplo = ctod(1);
    814 
    815 	/* Put dump at end of partition, and make it fit. */
    816 	if (dumpsize > dtoc(nblks - dumplo))
    817 		dumpsize = dtoc(nblks - dumplo);
    818 	if (dumplo < nblks - ctod(dumpsize))
    819 		dumplo = nblks - ctod(dumpsize);
    820 }
    821 
    822 /*
    823  * Doadump comes here after turning off memory management and
    824  * getting on the dump stack, either when called above, or by
    825  * the auto-restart code.
    826  */
    827 void
    828 dumpsys()
    829 {
    830 
    831 	msgbufmapped = 0;
    832 	if (dumpdev == NODEV)
    833 		return;
    834 	if (dumpsize == 0) {
    835 		dumpconf();
    836 		if (dumpsize == 0)
    837 			return;
    838 	}
    839 	printf("\ndumping to dev %x, offset %d\n", dumpdev, dumplo);
    840 
    841 	printf("dump ");
    842 	switch ((*bdevsw[major(dumpdev)].d_dump)(dumpdev)) {
    843 
    844 	case ENXIO:
    845 		printf("device bad\n");
    846 		break;
    847 
    848 	case EFAULT:
    849 		printf("device not ready\n");
    850 		break;
    851 
    852 	case EINVAL:
    853 		printf("area improper\n");
    854 		break;
    855 
    856 	case EIO:
    857 		printf("i/o error\n");
    858 		break;
    859 
    860 	case EINTR:
    861 		printf("aborted from console\n");
    862 		break;
    863 
    864 	default:
    865 		printf("succeeded\n");
    866 		break;
    867 	}
    868 	printf("\n\n");
    869 	delay(1000);
    870 }
    871 
    872 void
    873 frametoreg(framep, regp)
    874 	struct trapframe *framep;
    875 	struct reg *regp;
    876 {
    877 
    878 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
    879 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
    880 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
    881 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
    882 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
    883 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
    884 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
    885 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
    886 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
    887 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
    888 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
    889 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
    890 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
    891 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
    892 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
    893 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
    894 	regp->r_regs[R_A0] = framep->tf_a0;
    895 	regp->r_regs[R_A1] = framep->tf_a1;
    896 	regp->r_regs[R_A2] = framep->tf_a2;
    897 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
    898 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
    899 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
    900 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
    901 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
    902 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
    903 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
    904 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
    905 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
    906 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
    907 	regp->r_regs[R_GP] = framep->tf_gp;
    908 	regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP];
    909 	regp->r_regs[R_ZERO] = 0;
    910 }
    911 
    912 void
    913 regtoframe(regp, framep)
    914 	struct reg *regp;
    915 	struct trapframe *framep;
    916 {
    917 
    918 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
    919 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
    920 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
    921 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
    922 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
    923 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
    924 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
    925 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
    926 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
    927 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
    928 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
    929 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
    930 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
    931 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
    932 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
    933 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
    934 	framep->tf_a0 = regp->r_regs[R_A0];
    935 	framep->tf_a1 = regp->r_regs[R_A1];
    936 	framep->tf_a2 = regp->r_regs[R_A2];
    937 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
    938 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
    939 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
    940 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
    941 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
    942 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
    943 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
    944 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
    945 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
    946 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
    947 	framep->tf_gp = regp->r_regs[R_GP];
    948 	framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP];
    949 	/* ??? = regp->r_regs[R_ZERO]; */
    950 }
    951 
    952 void
    953 printregs(regp)
    954 	struct reg *regp;
    955 {
    956 	int i;
    957 
    958 	for (i = 0; i < 32; i++)
    959 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
    960 		   i & 1 ? "\n" : "\t");
    961 }
    962 
    963 void
    964 regdump(framep)
    965 	struct trapframe *framep;
    966 {
    967 	struct reg reg;
    968 
    969 	frametoreg(framep, &reg);
    970 	printf("REGISTERS:\n");
    971 	printregs(&reg);
    972 }
    973 
    974 #ifdef DEBUG
    975 int sigdebug = 0;
    976 int sigpid = 0;
    977 #define	SDB_FOLLOW	0x01
    978 #define	SDB_KSTACK	0x02
    979 #endif
    980 
    981 /*
    982  * Send an interrupt to process.
    983  */
    984 void
    985 sendsig(catcher, sig, mask, code)
    986 	sig_t catcher;
    987 	int sig, mask;
    988 	u_long code;
    989 {
    990 	struct proc *p = curproc;
    991 	struct sigcontext *scp, ksc;
    992 	struct trapframe *frame;
    993 	struct sigacts *psp = p->p_sigacts;
    994 	int oonstack, fsize, rndfsize;
    995 	extern char sigcode[], esigcode[];
    996 	extern struct proc *fpcurproc;
    997 
    998 	frame = p->p_md.md_tf;
    999 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
   1000 	fsize = sizeof ksc;
   1001 	rndfsize = ((fsize + 15) / 16) * 16;
   1002 	/*
   1003 	 * Allocate and validate space for the signal handler
   1004 	 * context. Note that if the stack is in P0 space, the
   1005 	 * call to grow() is a nop, and the useracc() check
   1006 	 * will fail if the process has not already allocated
   1007 	 * the space with a `brk'.
   1008 	 */
   1009 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
   1010 	    (psp->ps_sigonstack & sigmask(sig))) {
   1011 		scp = (struct sigcontext *)(psp->ps_sigstk.ss_sp +
   1012 		    psp->ps_sigstk.ss_size - rndfsize);
   1013 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
   1014 	} else
   1015 		scp = (struct sigcontext *)(frame->tf_regs[FRAME_SP] -
   1016 		    rndfsize);
   1017 	if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
   1018 		(void)grow(p, (u_long)scp);
   1019 #ifdef DEBUG
   1020 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1021 		printf("sendsig(%d): sig %d ssp %lx usp %lx\n", p->p_pid,
   1022 		    sig, &oonstack, scp);
   1023 #endif
   1024 	if (useracc((caddr_t)scp, fsize, B_WRITE) == 0) {
   1025 #ifdef DEBUG
   1026 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1027 			printf("sendsig(%d): useracc failed on sig %d\n",
   1028 			    p->p_pid, sig);
   1029 #endif
   1030 		/*
   1031 		 * Process has trashed its stack; give it an illegal
   1032 		 * instruction to halt it in its tracks.
   1033 		 */
   1034 		SIGACTION(p, SIGILL) = SIG_DFL;
   1035 		sig = sigmask(SIGILL);
   1036 		p->p_sigignore &= ~sig;
   1037 		p->p_sigcatch &= ~sig;
   1038 		p->p_sigmask &= ~sig;
   1039 		psignal(p, SIGILL);
   1040 		return;
   1041 	}
   1042 
   1043 	/*
   1044 	 * Build the signal context to be used by sigreturn.
   1045 	 */
   1046 	ksc.sc_onstack = oonstack;
   1047 	ksc.sc_mask = mask;
   1048 	ksc.sc_pc = frame->tf_pc;
   1049 	ksc.sc_ps = frame->tf_ps;
   1050 
   1051 	/* copy the registers. */
   1052 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1053 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1054 
   1055 	/* save the floating-point state, if necessary, then copy it. */
   1056 	if (p == fpcurproc) {
   1057 		pal_wrfen(1);
   1058 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
   1059 		pal_wrfen(0);
   1060 		fpcurproc = NULL;
   1061 	}
   1062 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
   1063 	bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1064 	    sizeof(struct fpreg));
   1065 	ksc.sc_fp_control = 0;					/* XXX ? */
   1066 	bzero(ksc.sc_reserved, sizeof ksc.sc_reserved);		/* XXX */
   1067 	bzero(ksc.sc_xxx, sizeof ksc.sc_xxx);			/* XXX */
   1068 
   1069 
   1070 #ifdef COMPAT_OSF1
   1071 	/*
   1072 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1073 	 */
   1074 #endif
   1075 
   1076 	/*
   1077 	 * copy the frame out to userland.
   1078 	 */
   1079 	(void) copyout((caddr_t)&ksc, (caddr_t)scp, fsize);
   1080 #ifdef DEBUG
   1081 	if (sigdebug & SDB_FOLLOW)
   1082 		printf("sendsig(%d): sig %d scp %lx code %lx\n", p->p_pid, sig,
   1083 		    scp, code);
   1084 #endif
   1085 
   1086 	/*
   1087 	 * Set up the registers to return to sigcode.
   1088 	 */
   1089 	frame->tf_pc = (u_int64_t)PS_STRINGS - (esigcode - sigcode);
   1090 	frame->tf_regs[FRAME_SP] = (u_int64_t)scp;
   1091 	frame->tf_a0 = sig;
   1092 	frame->tf_a1 = code;
   1093 	frame->tf_a2 = (u_int64_t)scp;
   1094 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
   1095 
   1096 #ifdef DEBUG
   1097 	if (sigdebug & SDB_FOLLOW)
   1098 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1099 		    frame->tf_pc, frame->tf_regs[FRAME_A3]);
   1100 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1101 		printf("sendsig(%d): sig %d returns\n",
   1102 		    p->p_pid, sig);
   1103 #endif
   1104 }
   1105 
   1106 /*
   1107  * System call to cleanup state after a signal
   1108  * has been taken.  Reset signal mask and
   1109  * stack state from context left by sendsig (above).
   1110  * Return to previous pc and psl as specified by
   1111  * context left by sendsig. Check carefully to
   1112  * make sure that the user has not modified the
   1113  * psl to gain improper priviledges or to cause
   1114  * a machine fault.
   1115  */
   1116 /* ARGSUSED */
   1117 int
   1118 sys_sigreturn(p, v, retval)
   1119 	struct proc *p;
   1120 	void *v;
   1121 	register_t *retval;
   1122 {
   1123 	struct sys_sigreturn_args /* {
   1124 		syscallarg(struct sigcontext *) sigcntxp;
   1125 	} */ *uap = v;
   1126 	struct sigcontext *scp, ksc;
   1127 	extern struct proc *fpcurproc;
   1128 
   1129 	scp = SCARG(uap, sigcntxp);
   1130 #ifdef DEBUG
   1131 	if (sigdebug & SDB_FOLLOW)
   1132 	    printf("sigreturn: pid %d, scp %lx\n", p->p_pid, scp);
   1133 #endif
   1134 
   1135 	if (ALIGN(scp) != (u_int64_t)scp)
   1136 		return (EINVAL);
   1137 
   1138 	/*
   1139 	 * Test and fetch the context structure.
   1140 	 * We grab it all at once for speed.
   1141 	 */
   1142 	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0 ||
   1143 	    copyin((caddr_t)scp, (caddr_t)&ksc, sizeof ksc))
   1144 		return (EINVAL);
   1145 
   1146 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1147 		return (EINVAL);
   1148 	/*
   1149 	 * Restore the user-supplied information
   1150 	 */
   1151 	if (ksc.sc_onstack)
   1152 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
   1153 	else
   1154 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1155 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
   1156 
   1157 	p->p_md.md_tf->tf_pc = ksc.sc_pc;
   1158 	p->p_md.md_tf->tf_ps = (ksc.sc_ps | PSL_USERSET) & ~PSL_USERCLR;
   1159 
   1160 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
   1161 
   1162 	/* XXX ksc.sc_ownedfp ? */
   1163 	if (p == fpcurproc)
   1164 		fpcurproc = NULL;
   1165 	bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
   1166 	    sizeof(struct fpreg));
   1167 	/* XXX ksc.sc_fp_control ? */
   1168 
   1169 #ifdef DEBUG
   1170 	if (sigdebug & SDB_FOLLOW)
   1171 		printf("sigreturn(%d): returns\n", p->p_pid);
   1172 #endif
   1173 	return (EJUSTRETURN);
   1174 }
   1175 
   1176 /*
   1177  * machine dependent system variables.
   1178  */
   1179 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1180 	int *name;
   1181 	u_int namelen;
   1182 	void *oldp;
   1183 	size_t *oldlenp;
   1184 	void *newp;
   1185 	size_t newlen;
   1186 	struct proc *p;
   1187 {
   1188 	dev_t consdev;
   1189 
   1190 	/* all sysctl names at this level are terminal */
   1191 	if (namelen != 1)
   1192 		return (ENOTDIR);		/* overloaded */
   1193 
   1194 	switch (name[0]) {
   1195 	case CPU_CONSDEV:
   1196 		if (cn_tab != NULL)
   1197 			consdev = cn_tab->cn_dev;
   1198 		else
   1199 			consdev = NODEV;
   1200 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1201 			sizeof consdev));
   1202 	default:
   1203 		return (EOPNOTSUPP);
   1204 	}
   1205 	/* NOTREACHED */
   1206 }
   1207 
   1208 /*
   1209  * Set registers on exec.
   1210  */
   1211 void
   1212 setregs(p, pack, stack, retval)
   1213 	register struct proc *p;
   1214 	struct exec_package *pack;
   1215 	u_long stack;
   1216 	register_t *retval;
   1217 {
   1218 	struct trapframe *tfp = p->p_md.md_tf;
   1219 	int i;
   1220 	extern struct proc *fpcurproc;
   1221 
   1222 #ifdef DEBUG
   1223 	for (i = 0; i < FRAME_NSAVEREGS; i++)
   1224 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1225 	tfp->tf_gp = 0xbabefacedeadbeef;
   1226 	tfp->tf_a0 = 0xbabefacedeadbeef;
   1227 	tfp->tf_a1 = 0xbabefacedeadbeef;
   1228 	tfp->tf_a2 = 0xbabefacedeadbeef;
   1229 #else
   1230 	bzero(tfp->tf_regs, FRAME_NSAVEREGS * sizeof tfp->tf_regs[0]);
   1231 	tfp->tf_gp = 0;
   1232 	tfp->tf_a0 = 0;
   1233 	tfp->tf_a1 = 0;
   1234 	tfp->tf_a2 = 0;
   1235 #endif
   1236 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
   1237 #define FP_RN 2 /* XXX */
   1238 	p->p_addr->u_pcb.pcb_fp.fpr_cr = (long)FP_RN << 58;
   1239 	tfp->tf_regs[FRAME_SP] = stack;	/* restored to usp in trap return */
   1240 	tfp->tf_ps = PSL_USERSET;
   1241 	tfp->tf_pc = pack->ep_entry & ~3;
   1242 
   1243 	p->p_md.md_flags & ~MDP_FPUSED;
   1244 	if (fpcurproc == p)
   1245 		fpcurproc = NULL;
   1246 
   1247 	retval[0] = retval[1] = 0;
   1248 }
   1249 
   1250 void
   1251 netintr()
   1252 {
   1253 #ifdef INET
   1254 #if NETHER > 0
   1255 	if (netisr & (1 << NETISR_ARP)) {
   1256 		netisr &= ~(1 << NETISR_ARP);
   1257 		arpintr();
   1258 	}
   1259 #endif
   1260 	if (netisr & (1 << NETISR_IP)) {
   1261 		netisr &= ~(1 << NETISR_IP);
   1262 		ipintr();
   1263 	}
   1264 #endif
   1265 #ifdef NS
   1266 	if (netisr & (1 << NETISR_NS)) {
   1267 		netisr &= ~(1 << NETISR_NS);
   1268 		nsintr();
   1269 	}
   1270 #endif
   1271 #ifdef ISO
   1272 	if (netisr & (1 << NETISR_ISO)) {
   1273 		netisr &= ~(1 << NETISR_ISO);
   1274 		clnlintr();
   1275 	}
   1276 #endif
   1277 #ifdef CCITT
   1278 	if (netisr & (1 << NETISR_CCITT)) {
   1279 		netisr &= ~(1 << NETISR_CCITT);
   1280 		ccittintr();
   1281 	}
   1282 #endif
   1283 #ifdef PPP
   1284 	if (netisr & (1 << NETISR_PPP)) {
   1285 		netisr &= ~(1 << NETISR_PPP);
   1286 		pppintr();
   1287 	}
   1288 #endif
   1289 }
   1290 
   1291 void
   1292 do_sir()
   1293 {
   1294 
   1295 	if (ssir & SIR_NET) {
   1296 		siroff(SIR_NET);
   1297 		cnt.v_soft++;
   1298 		netintr();
   1299 	}
   1300 	if (ssir & SIR_CLOCK) {
   1301 		siroff(SIR_CLOCK);
   1302 		cnt.v_soft++;
   1303 		softclock();
   1304 	}
   1305 }
   1306 
   1307 int
   1308 spl0()
   1309 {
   1310 
   1311 	if (ssir) {
   1312 		splsoft();
   1313 		do_sir();
   1314 	}
   1315 
   1316 	return (pal_swpipl(PSL_IPL_0));
   1317 }
   1318 
   1319 /*
   1320  * The following primitives manipulate the run queues.  _whichqs tells which
   1321  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
   1322  * into queues, Remrq removes them from queues.  The running process is on
   1323  * no queue, other processes are on a queue related to p->p_priority, divided
   1324  * by 4 actually to shrink the 0-127 range of priorities into the 32 available
   1325  * queues.
   1326  */
   1327 /*
   1328  * setrunqueue(p)
   1329  *	proc *p;
   1330  *
   1331  * Call should be made at splclock(), and p->p_stat should be SRUN.
   1332  */
   1333 
   1334 void
   1335 setrunqueue(p)
   1336 	struct proc *p;
   1337 {
   1338 	int bit;
   1339 
   1340 	/* firewall: p->p_back must be NULL */
   1341 	if (p->p_back != NULL)
   1342 		panic("setrunqueue");
   1343 
   1344 	bit = p->p_priority >> 2;
   1345 	whichqs |= (1 << bit);
   1346 	p->p_forw = (struct proc *)&qs[bit];
   1347 	p->p_back = qs[bit].ph_rlink;
   1348 	p->p_back->p_forw = p;
   1349 	qs[bit].ph_rlink = p;
   1350 }
   1351 
   1352 /*
   1353  * Remrq(p)
   1354  *
   1355  * Call should be made at splclock().
   1356  */
   1357 void
   1358 remrq(p)
   1359 	struct proc *p;
   1360 {
   1361 	int bit;
   1362 
   1363 	bit = p->p_priority >> 2;
   1364 	if ((whichqs & (1 << bit)) == 0)
   1365 		panic("remrq");
   1366 
   1367 	p->p_back->p_forw = p->p_forw;
   1368 	p->p_forw->p_back = p->p_back;
   1369 	p->p_back = NULL;	/* for firewall checking. */
   1370 
   1371 	if ((struct proc *)&qs[bit] == qs[bit].ph_link)
   1372 		whichqs &= ~(1 << bit);
   1373 }
   1374 
   1375 /*
   1376  * Return the best possible estimate of the time in the timeval
   1377  * to which tvp points.  Unfortunately, we can't read the hardware registers.
   1378  * We guarantee that the time will be greater than the value obtained by a
   1379  * previous call.
   1380  */
   1381 void
   1382 microtime(tvp)
   1383 	register struct timeval *tvp;
   1384 {
   1385 	int s = splclock();
   1386 	static struct timeval lasttime;
   1387 
   1388 	*tvp = time;
   1389 #ifdef notdef
   1390 	tvp->tv_usec += clkread();
   1391 	while (tvp->tv_usec > 1000000) {
   1392 		tvp->tv_sec++;
   1393 		tvp->tv_usec -= 1000000;
   1394 	}
   1395 #endif
   1396 	if (tvp->tv_sec == lasttime.tv_sec &&
   1397 	    tvp->tv_usec <= lasttime.tv_usec &&
   1398 	    (tvp->tv_usec = lasttime.tv_usec + 1) > 1000000) {
   1399 		tvp->tv_sec++;
   1400 		tvp->tv_usec -= 1000000;
   1401 	}
   1402 	lasttime = *tvp;
   1403 	splx(s);
   1404 }
   1405 
   1406 /*
   1407  * Wait "n" microseconds.
   1408  */
   1409 int
   1410 delay(n)
   1411 	int n;
   1412 {
   1413 	long N = cycles_per_usec * (n);
   1414 
   1415 	while (N > 0)				/* XXX */
   1416 		N -= 3;				/* XXX */
   1417 }
   1418 
   1419 #if defined(COMPAT_OSF1) || 1		/* XXX */
   1420 void
   1421 cpu_exec_ecoff_setregs(p, epp, stack, retval)
   1422 	struct proc *p;
   1423 	struct exec_package *epp;
   1424 	u_long stack;
   1425 	register_t *retval;
   1426 {
   1427 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1428 
   1429 	setregs(p, epp, stack, retval);
   1430 	p->p_md.md_tf->tf_gp = execp->a.gp_value;
   1431 }
   1432 
   1433 /*
   1434  * cpu_exec_ecoff_hook():
   1435  *	cpu-dependent ECOFF format hook for execve().
   1436  *
   1437  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   1438  *
   1439  */
   1440 int
   1441 cpu_exec_ecoff_hook(p, epp)
   1442 	struct proc *p;
   1443 	struct exec_package *epp;
   1444 {
   1445 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   1446 	extern struct emul emul_netbsd;
   1447 #ifdef COMPAT_OSF1
   1448 	extern struct emul emul_osf1;
   1449 #endif
   1450 
   1451 	switch (execp->f.f_magic) {
   1452 #ifdef COMPAT_OSF1
   1453 	case ECOFF_MAGIC_ALPHA:
   1454 		epp->ep_emul = &emul_osf1;
   1455 		break;
   1456 #endif
   1457 
   1458 	case ECOFF_MAGIC_NETBSD_ALPHA:
   1459 		epp->ep_emul = &emul_netbsd;
   1460 		break;
   1461 
   1462 	default:
   1463 		return ENOEXEC;
   1464 	}
   1465 	return 0;
   1466 }
   1467 #endif
   1468 
   1469 vm_offset_t
   1470 vtophys(vaddr)
   1471 	vm_offset_t vaddr;
   1472 {
   1473 	vm_offset_t paddr;
   1474 
   1475 	if (vaddr < K0SEG_BEGIN) {
   1476 		printf("vtophys: invalid vaddr 0x%lx", vaddr);
   1477 		paddr = vaddr;
   1478 	} else if (vaddr < K0SEG_END)
   1479 		paddr = k0segtophys(vaddr);
   1480 	else
   1481 		paddr = vatopa(vaddr);
   1482 
   1483 #if 0
   1484 	printf("vtophys(0x%lx) -> %lx\n", vaddr, paddr);
   1485 #endif
   1486 
   1487 	return (paddr);
   1488 }
   1489