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