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