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