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