Home | History | Annotate | Line # | Download | only in alpha
machdep.c revision 1.268
      1  1.267     enami /* $NetBSD: machdep.c,v 1.268 2003/05/10 21:10:25 thorpej Exp $ */
      2  1.110   thorpej 
      3  1.110   thorpej /*-
      4  1.211   thorpej  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      5  1.110   thorpej  * All rights reserved.
      6  1.110   thorpej  *
      7  1.110   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.110   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.110   thorpej  * NASA Ames Research Center and by Chris G. Demetriou.
     10  1.110   thorpej  *
     11  1.110   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.110   thorpej  * modification, are permitted provided that the following conditions
     13  1.110   thorpej  * are met:
     14  1.110   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.110   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.110   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.110   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.110   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.110   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.110   thorpej  *    must display the following acknowledgement:
     21  1.110   thorpej  *	This product includes software developed by the NetBSD
     22  1.110   thorpej  *	Foundation, Inc. and its contributors.
     23  1.110   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.110   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.110   thorpej  *    from this software without specific prior written permission.
     26  1.110   thorpej  *
     27  1.110   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.110   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.110   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.110   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.110   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.110   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.110   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.110   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.110   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.110   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.110   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.110   thorpej  */
     39    1.1       cgd 
     40    1.1       cgd /*
     41   1.16       cgd  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
     42    1.1       cgd  * All rights reserved.
     43    1.1       cgd  *
     44    1.1       cgd  * Author: Chris G. Demetriou
     45    1.1       cgd  *
     46    1.1       cgd  * Permission to use, copy, modify and distribute this software and
     47    1.1       cgd  * its documentation is hereby granted, provided that both the copyright
     48    1.1       cgd  * notice and this permission notice appear in all copies of the
     49    1.1       cgd  * software, derivative works or modified versions, and any portions
     50    1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     51    1.1       cgd  *
     52    1.1       cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     53    1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     54    1.1       cgd  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     55    1.1       cgd  *
     56    1.1       cgd  * Carnegie Mellon requests users of this software to return to
     57    1.1       cgd  *
     58    1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     59    1.1       cgd  *  School of Computer Science
     60    1.1       cgd  *  Carnegie Mellon University
     61    1.1       cgd  *  Pittsburgh PA 15213-3890
     62    1.1       cgd  *
     63    1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     64    1.1       cgd  * rights to redistribute these changes.
     65    1.1       cgd  */
     66   1.74       cgd 
     67  1.129  jonathan #include "opt_ddb.h"
     68  1.244     lukem #include "opt_kgdb.h"
     69  1.147   thorpej #include "opt_multiprocessor.h"
     70  1.123   thorpej #include "opt_dec_3000_300.h"
     71  1.123   thorpej #include "opt_dec_3000_500.h"
     72  1.127   thorpej #include "opt_compat_osf1.h"
     73  1.141   thorpej #include "opt_compat_netbsd.h"
     74  1.250  jdolecek #include "opt_execfmt.h"
     75  1.112   thorpej 
     76   1.75       cgd #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     77   1.75       cgd 
     78  1.267     enami __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.268 2003/05/10 21:10:25 thorpej Exp $");
     79    1.1       cgd 
     80    1.1       cgd #include <sys/param.h>
     81    1.1       cgd #include <sys/systm.h>
     82    1.1       cgd #include <sys/signalvar.h>
     83    1.1       cgd #include <sys/kernel.h>
     84    1.1       cgd #include <sys/proc.h>
     85  1.264   nathanw #include <sys/ras.h>
     86  1.261   thorpej #include <sys/sa.h>
     87  1.261   thorpej #include <sys/savar.h>
     88  1.207   thorpej #include <sys/sched.h>
     89    1.1       cgd #include <sys/buf.h>
     90    1.1       cgd #include <sys/reboot.h>
     91   1.28       cgd #include <sys/device.h>
     92    1.1       cgd #include <sys/file.h>
     93    1.1       cgd #include <sys/malloc.h>
     94    1.1       cgd #include <sys/mbuf.h>
     95  1.110   thorpej #include <sys/mman.h>
     96    1.1       cgd #include <sys/msgbuf.h>
     97    1.1       cgd #include <sys/ioctl.h>
     98    1.1       cgd #include <sys/tty.h>
     99    1.1       cgd #include <sys/user.h>
    100    1.1       cgd #include <sys/exec.h>
    101    1.1       cgd #include <sys/exec_ecoff.h>
    102   1.43       cgd #include <sys/core.h>
    103   1.43       cgd #include <sys/kcore.h>
    104  1.261   thorpej #include <sys/ucontext.h>
    105  1.258   gehenna #include <sys/conf.h>
    106  1.266     ragge #include <sys/ksyms.h>
    107   1.43       cgd #include <machine/kcore.h>
    108  1.241      ross #include <machine/fpu.h>
    109    1.1       cgd 
    110    1.1       cgd #include <sys/mount.h>
    111  1.261   thorpej #include <sys/sa.h>
    112    1.1       cgd #include <sys/syscallargs.h>
    113    1.1       cgd 
    114  1.112   thorpej #include <uvm/uvm_extern.h>
    115  1.217       mrg #include <sys/sysctl.h>
    116  1.112   thorpej 
    117    1.1       cgd #include <dev/cons.h>
    118    1.1       cgd 
    119   1.81   thorpej #include <machine/autoconf.h>
    120    1.1       cgd #include <machine/cpu.h>
    121    1.1       cgd #include <machine/reg.h>
    122    1.1       cgd #include <machine/rpb.h>
    123    1.1       cgd #include <machine/prom.h>
    124  1.258   gehenna #include <machine/cpuconf.h>
    125  1.172      ross #include <machine/ieeefp.h>
    126  1.148   thorpej 
    127   1.81   thorpej #ifdef DDB
    128   1.81   thorpej #include <machine/db_machdep.h>
    129   1.81   thorpej #include <ddb/db_access.h>
    130   1.81   thorpej #include <ddb/db_sym.h>
    131   1.81   thorpej #include <ddb/db_extern.h>
    132   1.81   thorpej #include <ddb/db_interface.h>
    133  1.233   thorpej #endif
    134  1.233   thorpej 
    135  1.233   thorpej #ifdef KGDB
    136  1.233   thorpej #include <sys/kgdb.h>
    137   1.81   thorpej #endif
    138   1.81   thorpej 
    139  1.229  sommerfe #ifdef DEBUG
    140  1.229  sommerfe #include <machine/sigdebug.h>
    141  1.229  sommerfe #endif
    142  1.229  sommerfe 
    143  1.155      ross #include <machine/alpha.h>
    144  1.143      matt 
    145  1.266     ragge #include "ksyms.h"
    146  1.266     ragge 
    147  1.245       chs struct vm_map *exec_map = NULL;
    148  1.245       chs struct vm_map *mb_map = NULL;
    149  1.245       chs struct vm_map *phys_map = NULL;
    150    1.1       cgd 
    151   1.86       leo caddr_t msgbufaddr;
    152   1.86       leo 
    153    1.1       cgd int	maxmem;			/* max memory per process */
    154    1.7       cgd 
    155    1.7       cgd int	totalphysmem;		/* total amount of physical memory in system */
    156    1.7       cgd int	physmem;		/* physical memory used by NetBSD + some rsvd */
    157    1.1       cgd int	resvmem;		/* amount of memory reserved for PROM */
    158    1.7       cgd int	unusedmem;		/* amount of memory for OS that we don't use */
    159    1.7       cgd int	unknownmem;		/* amount of memory with an unknown use */
    160    1.1       cgd 
    161    1.1       cgd int	cputype;		/* system type, from the RPB */
    162  1.210   thorpej 
    163  1.210   thorpej int	bootdev_debug = 0;	/* patchable, or from DDB */
    164    1.1       cgd 
    165    1.1       cgd /*
    166    1.1       cgd  * XXX We need an address to which we can assign things so that they
    167    1.1       cgd  * won't be optimized away because we didn't use the value.
    168    1.1       cgd  */
    169    1.1       cgd u_int32_t no_optimize;
    170    1.1       cgd 
    171    1.1       cgd /* the following is used externally (sysctl_hw) */
    172   1.79     veego char	machine[] = MACHINE;		/* from <machine/param.h> */
    173   1.79     veego char	machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
    174   1.29       cgd char	cpu_model[128];
    175    1.1       cgd 
    176    1.1       cgd struct	user *proc0paddr;
    177    1.1       cgd 
    178    1.1       cgd /* Number of machine cycles per microsecond */
    179    1.1       cgd u_int64_t	cycles_per_usec;
    180    1.1       cgd 
    181    1.7       cgd /* number of cpus in the box.  really! */
    182    1.7       cgd int		ncpus;
    183    1.7       cgd 
    184  1.102       cgd struct bootinfo_kernel bootinfo;
    185   1.81   thorpej 
    186  1.123   thorpej /* For built-in TCDS */
    187  1.123   thorpej #if defined(DEC_3000_300) || defined(DEC_3000_500)
    188  1.123   thorpej u_int8_t	dec_3000_scsiid[2], dec_3000_scsifast[2];
    189  1.123   thorpej #endif
    190  1.123   thorpej 
    191   1.89    mjacob struct platform platform;
    192   1.89    mjacob 
    193  1.266     ragge #if NKSYMS || defined(DDB) || defined(LKM)
    194   1.81   thorpej /* start and end of kernel symbol table */
    195   1.81   thorpej void	*ksym_start, *ksym_end;
    196   1.81   thorpej #endif
    197   1.81   thorpej 
    198   1.30       cgd /* for cpu_sysctl() */
    199   1.36       cgd int	alpha_unaligned_print = 1;	/* warn about unaligned accesses */
    200   1.36       cgd int	alpha_unaligned_fix = 1;	/* fix up unaligned accesses */
    201   1.36       cgd int	alpha_unaligned_sigbus = 0;	/* don't SIGBUS on fixed-up accesses */
    202  1.241      ross int	alpha_fp_sync_complete = 0;	/* fp fixup if sync even without /s */
    203   1.30       cgd 
    204  1.110   thorpej /*
    205  1.110   thorpej  * XXX This should be dynamically sized, but we have the chicken-egg problem!
    206  1.110   thorpej  * XXX it should also be larger than it is, because not all of the mddt
    207  1.110   thorpej  * XXX clusters end up being used for VM.
    208  1.110   thorpej  */
    209  1.110   thorpej phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];	/* low size bits overloaded */
    210  1.110   thorpej int	mem_cluster_cnt;
    211  1.110   thorpej 
    212   1.55       cgd int	cpu_dump __P((void));
    213   1.55       cgd int	cpu_dumpsize __P((void));
    214  1.110   thorpej u_long	cpu_dump_mempagecnt __P((void));
    215   1.55       cgd void	dumpsys __P((void));
    216   1.55       cgd void	identifycpu __P((void));
    217   1.55       cgd void	printregs __P((struct reg *));
    218   1.33       cgd 
    219   1.55       cgd void
    220  1.102       cgd alpha_init(pfn, ptb, bim, bip, biv)
    221    1.1       cgd 	u_long pfn;		/* first free PFN number */
    222    1.1       cgd 	u_long ptb;		/* PFN of current level 1 page table */
    223   1.81   thorpej 	u_long bim;		/* bootinfo magic */
    224   1.81   thorpej 	u_long bip;		/* bootinfo pointer */
    225  1.102       cgd 	u_long biv;		/* bootinfo version */
    226    1.1       cgd {
    227   1.95   thorpej 	extern char kernel_text[], _end[];
    228    1.1       cgd 	struct mddt *mddtp;
    229  1.110   thorpej 	struct mddt_cluster *memc;
    230    1.7       cgd 	int i, mddtweird;
    231  1.110   thorpej 	struct vm_physseg *vps;
    232  1.140   thorpej 	vaddr_t kernstart, kernend;
    233  1.140   thorpej 	paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
    234  1.140   thorpej 	vsize_t size;
    235  1.211   thorpej 	cpuid_t cpu_id;
    236  1.211   thorpej 	struct cpu_info *ci;
    237    1.1       cgd 	char *p;
    238   1.95   thorpej 	caddr_t v;
    239  1.209   thorpej 	const char *bootinfo_msg;
    240  1.209   thorpej 	const struct cpuinit *c;
    241  1.106       cgd 
    242  1.106       cgd 	/* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
    243    1.1       cgd 
    244    1.1       cgd 	/*
    245   1.77       cgd 	 * Turn off interrupts (not mchecks) and floating point.
    246    1.1       cgd 	 * Make sure the instruction and data streams are consistent.
    247    1.1       cgd 	 */
    248   1.77       cgd 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
    249   1.32       cgd 	alpha_pal_wrfen(0);
    250   1.37       cgd 	ALPHA_TBIA();
    251   1.32       cgd 	alpha_pal_imb();
    252  1.248   thorpej 
    253  1.248   thorpej 	/* Initialize the SCB. */
    254  1.248   thorpej 	scb_init();
    255    1.1       cgd 
    256  1.211   thorpej 	cpu_id = cpu_number();
    257  1.211   thorpej 
    258  1.189   thorpej #if defined(MULTIPROCESSOR)
    259  1.189   thorpej 	/*
    260  1.189   thorpej 	 * Set our SysValue to the address of our cpu_info structure.
    261  1.189   thorpej 	 * Secondary processors do this in their spinup trampoline.
    262  1.189   thorpej 	 */
    263  1.237   thorpej 	alpha_pal_wrval((u_long)&cpu_info_primary);
    264  1.237   thorpej 	cpu_info[cpu_id] = &cpu_info_primary;
    265  1.189   thorpej #endif
    266  1.189   thorpej 
    267  1.211   thorpej 	ci = curcpu();
    268  1.211   thorpej 	ci->ci_cpuid = cpu_id;
    269  1.211   thorpej 
    270    1.1       cgd 	/*
    271  1.106       cgd 	 * Get critical system information (if possible, from the
    272  1.106       cgd 	 * information provided by the boot program).
    273   1.81   thorpej 	 */
    274  1.106       cgd 	bootinfo_msg = NULL;
    275   1.81   thorpej 	if (bim == BOOTINFO_MAGIC) {
    276  1.102       cgd 		if (biv == 0) {		/* backward compat */
    277  1.102       cgd 			biv = *(u_long *)bip;
    278  1.102       cgd 			bip += 8;
    279  1.102       cgd 		}
    280  1.102       cgd 		switch (biv) {
    281  1.102       cgd 		case 1: {
    282  1.102       cgd 			struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
    283  1.102       cgd 
    284  1.102       cgd 			bootinfo.ssym = v1p->ssym;
    285  1.102       cgd 			bootinfo.esym = v1p->esym;
    286  1.106       cgd 			/* hwrpb may not be provided by boot block in v1 */
    287  1.106       cgd 			if (v1p->hwrpb != NULL) {
    288  1.106       cgd 				bootinfo.hwrpb_phys =
    289  1.106       cgd 				    ((struct rpb *)v1p->hwrpb)->rpb_phys;
    290  1.106       cgd 				bootinfo.hwrpb_size = v1p->hwrpbsize;
    291  1.106       cgd 			} else {
    292  1.106       cgd 				bootinfo.hwrpb_phys =
    293  1.106       cgd 				    ((struct rpb *)HWRPB_ADDR)->rpb_phys;
    294  1.106       cgd 				bootinfo.hwrpb_size =
    295  1.106       cgd 				    ((struct rpb *)HWRPB_ADDR)->rpb_size;
    296  1.106       cgd 			}
    297  1.247   thorpej 			memcpy(bootinfo.boot_flags, v1p->boot_flags,
    298  1.102       cgd 			    min(sizeof v1p->boot_flags,
    299  1.102       cgd 			      sizeof bootinfo.boot_flags));
    300  1.247   thorpej 			memcpy(bootinfo.booted_kernel, v1p->booted_kernel,
    301  1.102       cgd 			    min(sizeof v1p->booted_kernel,
    302  1.102       cgd 			      sizeof bootinfo.booted_kernel));
    303  1.106       cgd 			/* booted dev not provided in bootinfo */
    304  1.106       cgd 			init_prom_interface((struct rpb *)
    305  1.106       cgd 			    ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
    306  1.102       cgd                 	prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
    307  1.102       cgd 			    sizeof bootinfo.booted_dev);
    308   1.81   thorpej 			break;
    309  1.102       cgd 		}
    310   1.81   thorpej 		default:
    311  1.106       cgd 			bootinfo_msg = "unknown bootinfo version";
    312  1.102       cgd 			goto nobootinfo;
    313   1.81   thorpej 		}
    314  1.102       cgd 	} else {
    315  1.106       cgd 		bootinfo_msg = "boot program did not pass bootinfo";
    316  1.102       cgd nobootinfo:
    317  1.102       cgd 		bootinfo.ssym = (u_long)_end;
    318  1.102       cgd 		bootinfo.esym = (u_long)_end;
    319  1.106       cgd 		bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
    320  1.106       cgd 		bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
    321  1.106       cgd 		init_prom_interface((struct rpb *)HWRPB_ADDR);
    322  1.102       cgd 		prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
    323  1.102       cgd 		    sizeof bootinfo.boot_flags);
    324  1.102       cgd 		prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
    325  1.102       cgd 		    sizeof bootinfo.booted_kernel);
    326  1.102       cgd 		prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
    327  1.102       cgd 		    sizeof bootinfo.booted_dev);
    328  1.102       cgd 	}
    329  1.102       cgd 
    330   1.81   thorpej 	/*
    331  1.106       cgd 	 * Initialize the kernel's mapping of the RPB.  It's needed for
    332  1.106       cgd 	 * lots of things.
    333  1.106       cgd 	 */
    334  1.106       cgd 	hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
    335  1.123   thorpej 
    336  1.123   thorpej #if defined(DEC_3000_300) || defined(DEC_3000_500)
    337  1.123   thorpej 	if (hwrpb->rpb_type == ST_DEC_3000_300 ||
    338  1.123   thorpej 	    hwrpb->rpb_type == ST_DEC_3000_500) {
    339  1.123   thorpej 		prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
    340  1.123   thorpej 		    sizeof(dec_3000_scsiid));
    341  1.123   thorpej 		prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
    342  1.123   thorpej 		    sizeof(dec_3000_scsifast));
    343  1.123   thorpej 	}
    344  1.123   thorpej #endif
    345  1.106       cgd 
    346  1.106       cgd 	/*
    347  1.106       cgd 	 * Remember how many cycles there are per microsecond,
    348  1.106       cgd 	 * so that we can use delay().  Round up, for safety.
    349  1.106       cgd 	 */
    350  1.106       cgd 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
    351  1.106       cgd 
    352  1.106       cgd 	/*
    353  1.251       wiz 	 * Initialize the (temporary) bootstrap console interface, so
    354  1.106       cgd 	 * we can use printf until the VM system starts being setup.
    355  1.106       cgd 	 * The real console is initialized before then.
    356  1.106       cgd 	 */
    357  1.106       cgd 	init_bootstrap_console();
    358  1.106       cgd 
    359  1.106       cgd 	/* OUTPUT NOW ALLOWED */
    360  1.106       cgd 
    361  1.106       cgd 	/* delayed from above */
    362  1.106       cgd 	if (bootinfo_msg)
    363  1.106       cgd 		printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
    364  1.106       cgd 		    bootinfo_msg, bim, bip, biv);
    365  1.106       cgd 
    366  1.147   thorpej 	/* Initialize the trap vectors on the primary processor. */
    367  1.147   thorpej 	trap_init();
    368    1.1       cgd 
    369    1.1       cgd 	/*
    370  1.263   thorpej 	 * Find out this system's page size, and initialize
    371  1.263   thorpej 	 * PAGE_SIZE-dependent variables.
    372  1.243   thorpej 	 */
    373  1.263   thorpej 	if (hwrpb->rpb_page_size != ALPHA_PGBYTES)
    374  1.263   thorpej 		panic("page size %lu != %d?!", hwrpb->rpb_page_size,
    375  1.263   thorpej 		    ALPHA_PGBYTES);
    376  1.263   thorpej 	uvmexp.pagesize = hwrpb->rpb_page_size;
    377  1.243   thorpej 	uvm_setpagesize();
    378  1.243   thorpej 
    379  1.243   thorpej 	/*
    380  1.106       cgd 	 * Find out what hardware we're on, and do basic initialization.
    381  1.106       cgd 	 */
    382  1.106       cgd 	cputype = hwrpb->rpb_type;
    383  1.167       cgd 	if (cputype < 0) {
    384  1.167       cgd 		/*
    385  1.167       cgd 		 * At least some white-box systems have SRM which
    386  1.167       cgd 		 * reports a systype that's the negative of their
    387  1.167       cgd 		 * blue-box counterpart.
    388  1.167       cgd 		 */
    389  1.167       cgd 		cputype = -cputype;
    390  1.167       cgd 	}
    391  1.209   thorpej 	c = platform_lookup(cputype);
    392  1.209   thorpej 	if (c == NULL) {
    393  1.106       cgd 		platform_not_supported();
    394  1.106       cgd 		/* NOTREACHED */
    395  1.106       cgd 	}
    396  1.209   thorpej 	(*c->init)();
    397  1.106       cgd 	strcpy(cpu_model, platform.model);
    398  1.106       cgd 
    399  1.106       cgd 	/*
    400  1.251       wiz 	 * Initialize the real console, so that the bootstrap console is
    401  1.106       cgd 	 * no longer necessary.
    402  1.106       cgd 	 */
    403  1.169   thorpej 	(*platform.cons_init)();
    404  1.106       cgd 
    405  1.106       cgd #ifdef DIAGNOSTIC
    406  1.106       cgd 	/* Paranoid sanity checking */
    407  1.106       cgd 
    408  1.199     soren 	/* We should always be running on the primary. */
    409  1.211   thorpej 	assert(hwrpb->rpb_primary_cpu_id == cpu_id);
    410  1.106       cgd 
    411  1.116    mjacob 	/*
    412  1.116    mjacob 	 * On single-CPU systypes, the primary should always be CPU 0,
    413  1.116    mjacob 	 * except on Alpha 8200 systems where the CPU id is related
    414  1.116    mjacob 	 * to the VID, which is related to the Turbo Laser node id.
    415  1.116    mjacob 	 */
    416  1.106       cgd 	if (cputype != ST_DEC_21000)
    417  1.106       cgd 		assert(hwrpb->rpb_primary_cpu_id == 0);
    418  1.106       cgd #endif
    419  1.106       cgd 
    420  1.106       cgd 	/* NO MORE FIRMWARE ACCESS ALLOWED */
    421  1.106       cgd #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    422  1.106       cgd 	/*
    423  1.106       cgd 	 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
    424  1.106       cgd 	 * XXX pmap_uses_prom_console() evaluates to non-zero.)
    425  1.106       cgd 	 */
    426  1.106       cgd #endif
    427   1.95   thorpej 
    428   1.95   thorpej 	/*
    429  1.101       cgd 	 * Find the beginning and end of the kernel (and leave a
    430  1.101       cgd 	 * bit of space before the beginning for the bootstrap
    431  1.101       cgd 	 * stack).
    432   1.95   thorpej 	 */
    433  1.201    kleink 	kernstart = trunc_page((vaddr_t)kernel_text) - 2 * PAGE_SIZE;
    434  1.266     ragge #if NKSYMS || defined(DDB) || defined(LKM)
    435  1.102       cgd 	ksym_start = (void *)bootinfo.ssym;
    436  1.102       cgd 	ksym_end   = (void *)bootinfo.esym;
    437  1.201    kleink 	kernend = (vaddr_t)round_page((vaddr_t)ksym_end);
    438  1.102       cgd #else
    439  1.201    kleink 	kernend = (vaddr_t)round_page((vaddr_t)_end);
    440   1.95   thorpej #endif
    441   1.95   thorpej 
    442  1.110   thorpej 	kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
    443  1.110   thorpej 	kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
    444  1.110   thorpej 
    445   1.95   thorpej 	/*
    446    1.1       cgd 	 * Find out how much memory is available, by looking at
    447    1.7       cgd 	 * the memory cluster descriptors.  This also tries to do
    448    1.7       cgd 	 * its best to detect things things that have never been seen
    449    1.7       cgd 	 * before...
    450    1.1       cgd 	 */
    451    1.1       cgd 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
    452    1.7       cgd 
    453  1.110   thorpej 	/* MDDT SANITY CHECKING */
    454    1.7       cgd 	mddtweird = 0;
    455  1.110   thorpej 	if (mddtp->mddt_cluster_cnt < 2) {
    456    1.7       cgd 		mddtweird = 1;
    457  1.160   thorpej 		printf("WARNING: weird number of mem clusters: %lu\n",
    458  1.110   thorpej 		    mddtp->mddt_cluster_cnt);
    459    1.7       cgd 	}
    460    1.7       cgd 
    461  1.110   thorpej #if 0
    462  1.110   thorpej 	printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
    463  1.110   thorpej #endif
    464  1.110   thorpej 
    465  1.110   thorpej 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    466  1.110   thorpej 		memc = &mddtp->mddt_clusters[i];
    467  1.110   thorpej #if 0
    468  1.110   thorpej 		printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
    469  1.110   thorpej 		    memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
    470  1.110   thorpej #endif
    471  1.110   thorpej 		totalphysmem += memc->mddt_pg_cnt;
    472  1.110   thorpej 		if (mem_cluster_cnt < VM_PHYSSEG_MAX) {	/* XXX */
    473  1.110   thorpej 			mem_clusters[mem_cluster_cnt].start =
    474  1.110   thorpej 			    ptoa(memc->mddt_pfn);
    475  1.110   thorpej 			mem_clusters[mem_cluster_cnt].size =
    476  1.110   thorpej 			    ptoa(memc->mddt_pg_cnt);
    477  1.110   thorpej 			if (memc->mddt_usage & MDDT_mbz ||
    478  1.110   thorpej 			    memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
    479  1.110   thorpej 			    memc->mddt_usage & MDDT_PALCODE)
    480  1.110   thorpej 				mem_clusters[mem_cluster_cnt].size |=
    481  1.110   thorpej 				    PROT_READ;
    482  1.110   thorpej 			else
    483  1.110   thorpej 				mem_clusters[mem_cluster_cnt].size |=
    484  1.110   thorpej 				    PROT_READ | PROT_WRITE | PROT_EXEC;
    485  1.110   thorpej 			mem_cluster_cnt++;
    486  1.110   thorpej 		}
    487  1.110   thorpej 
    488  1.110   thorpej 		if (memc->mddt_usage & MDDT_mbz) {
    489    1.7       cgd 			mddtweird = 1;
    490  1.110   thorpej 			printf("WARNING: mem cluster %d has weird "
    491  1.110   thorpej 			    "usage 0x%lx\n", i, memc->mddt_usage);
    492  1.110   thorpej 			unknownmem += memc->mddt_pg_cnt;
    493  1.110   thorpej 			continue;
    494    1.7       cgd 		}
    495  1.110   thorpej 		if (memc->mddt_usage & MDDT_NONVOLATILE) {
    496  1.110   thorpej 			/* XXX should handle these... */
    497  1.110   thorpej 			printf("WARNING: skipping non-volatile mem "
    498  1.110   thorpej 			    "cluster %d\n", i);
    499  1.110   thorpej 			unusedmem += memc->mddt_pg_cnt;
    500  1.110   thorpej 			continue;
    501  1.110   thorpej 		}
    502  1.110   thorpej 		if (memc->mddt_usage & MDDT_PALCODE) {
    503  1.110   thorpej 			resvmem += memc->mddt_pg_cnt;
    504  1.110   thorpej 			continue;
    505  1.110   thorpej 		}
    506  1.110   thorpej 
    507  1.110   thorpej 		/*
    508  1.110   thorpej 		 * We have a memory cluster available for system
    509  1.110   thorpej 		 * software use.  We must determine if this cluster
    510  1.110   thorpej 		 * holds the kernel.
    511  1.110   thorpej 		 */
    512  1.110   thorpej #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    513  1.110   thorpej 		/*
    514  1.110   thorpej 		 * XXX If the kernel uses the PROM console, we only use the
    515  1.110   thorpej 		 * XXX memory after the kernel in the first system segment,
    516  1.110   thorpej 		 * XXX to avoid clobbering prom mapping, data, etc.
    517  1.110   thorpej 		 */
    518  1.110   thorpej 	    if (!pmap_uses_prom_console() || physmem == 0) {
    519  1.110   thorpej #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    520  1.110   thorpej 		physmem += memc->mddt_pg_cnt;
    521  1.110   thorpej 		pfn0 = memc->mddt_pfn;
    522  1.110   thorpej 		pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
    523  1.110   thorpej 		if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
    524  1.110   thorpej 			/*
    525  1.110   thorpej 			 * Must compute the location of the kernel
    526  1.110   thorpej 			 * within the segment.
    527  1.110   thorpej 			 */
    528  1.110   thorpej #if 0
    529  1.110   thorpej 			printf("Cluster %d contains kernel\n", i);
    530  1.110   thorpej #endif
    531  1.110   thorpej #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    532  1.110   thorpej 		    if (!pmap_uses_prom_console()) {
    533  1.110   thorpej #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    534  1.110   thorpej 			if (pfn0 < kernstartpfn) {
    535  1.110   thorpej 				/*
    536  1.110   thorpej 				 * There is a chunk before the kernel.
    537  1.110   thorpej 				 */
    538  1.110   thorpej #if 0
    539  1.110   thorpej 				printf("Loading chunk before kernel: "
    540  1.110   thorpej 				    "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
    541  1.110   thorpej #endif
    542  1.112   thorpej 				uvm_page_physload(pfn0, kernstartpfn,
    543  1.135   thorpej 				    pfn0, kernstartpfn, VM_FREELIST_DEFAULT);
    544  1.110   thorpej 			}
    545  1.110   thorpej #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    546  1.110   thorpej 		    }
    547  1.110   thorpej #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    548  1.110   thorpej 			if (kernendpfn < pfn1) {
    549  1.110   thorpej 				/*
    550  1.110   thorpej 				 * There is a chunk after the kernel.
    551  1.110   thorpej 				 */
    552  1.110   thorpej #if 0
    553  1.110   thorpej 				printf("Loading chunk after kernel: "
    554  1.110   thorpej 				    "0x%lx / 0x%lx\n", kernendpfn, pfn1);
    555  1.110   thorpej #endif
    556  1.112   thorpej 				uvm_page_physload(kernendpfn, pfn1,
    557  1.135   thorpej 				    kernendpfn, pfn1, VM_FREELIST_DEFAULT);
    558  1.110   thorpej 			}
    559  1.110   thorpej 		} else {
    560  1.110   thorpej 			/*
    561  1.110   thorpej 			 * Just load this cluster as one chunk.
    562  1.110   thorpej 			 */
    563  1.110   thorpej #if 0
    564  1.110   thorpej 			printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
    565  1.110   thorpej 			    pfn0, pfn1);
    566  1.110   thorpej #endif
    567  1.135   thorpej 			uvm_page_physload(pfn0, pfn1, pfn0, pfn1,
    568  1.135   thorpej 			    VM_FREELIST_DEFAULT);
    569    1.7       cgd 		}
    570  1.110   thorpej #ifdef _PMAP_MAY_USE_PROM_CONSOLE
    571  1.110   thorpej 	    }
    572  1.110   thorpej #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
    573    1.7       cgd 	}
    574    1.7       cgd 
    575  1.110   thorpej 	/*
    576  1.110   thorpej 	 * Dump out the MDDT if it looks odd...
    577  1.110   thorpej 	 */
    578    1.7       cgd 	if (mddtweird) {
    579   1.46  christos 		printf("\n");
    580   1.46  christos 		printf("complete memory cluster information:\n");
    581    1.2       cgd 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
    582   1.46  christos 			printf("mddt %d:\n", i);
    583   1.46  christos 			printf("\tpfn %lx\n",
    584    1.2       cgd 			    mddtp->mddt_clusters[i].mddt_pfn);
    585   1.46  christos 			printf("\tcnt %lx\n",
    586    1.2       cgd 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
    587   1.46  christos 			printf("\ttest %lx\n",
    588    1.2       cgd 			    mddtp->mddt_clusters[i].mddt_pg_test);
    589   1.46  christos 			printf("\tbva %lx\n",
    590    1.2       cgd 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
    591   1.46  christos 			printf("\tbpa %lx\n",
    592    1.2       cgd 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
    593   1.46  christos 			printf("\tbcksum %lx\n",
    594    1.2       cgd 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
    595   1.46  christos 			printf("\tusage %lx\n",
    596    1.2       cgd 			    mddtp->mddt_clusters[i].mddt_usage);
    597    1.2       cgd 		}
    598   1.46  christos 		printf("\n");
    599    1.2       cgd 	}
    600    1.2       cgd 
    601    1.7       cgd 	if (totalphysmem == 0)
    602    1.1       cgd 		panic("can't happen: system seems to have no memory!");
    603    1.1       cgd 	maxmem = physmem;
    604    1.7       cgd #if 0
    605   1.46  christos 	printf("totalphysmem = %d\n", totalphysmem);
    606   1.46  christos 	printf("physmem = %d\n", physmem);
    607   1.46  christos 	printf("resvmem = %d\n", resvmem);
    608   1.46  christos 	printf("unusedmem = %d\n", unusedmem);
    609   1.46  christos 	printf("unknownmem = %d\n", unknownmem);
    610    1.7       cgd #endif
    611    1.7       cgd 
    612    1.1       cgd 	/*
    613    1.1       cgd 	 * Initialize error message buffer (at end of core).
    614    1.1       cgd 	 */
    615  1.110   thorpej 	{
    616  1.204     enami 		vsize_t sz = (vsize_t)round_page(MSGBUFSIZE);
    617  1.203     enami 		vsize_t reqsz = sz;
    618  1.110   thorpej 
    619  1.110   thorpej 		vps = &vm_physmem[vm_nphysseg - 1];
    620  1.110   thorpej 
    621  1.110   thorpej 		/* shrink so that it'll fit in the last segment */
    622  1.110   thorpej 		if ((vps->avail_end - vps->avail_start) < atop(sz))
    623  1.110   thorpej 			sz = ptoa(vps->avail_end - vps->avail_start);
    624  1.110   thorpej 
    625  1.110   thorpej 		vps->end -= atop(sz);
    626  1.110   thorpej 		vps->avail_end -= atop(sz);
    627  1.110   thorpej 		msgbufaddr = (caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end));
    628  1.110   thorpej 		initmsgbuf(msgbufaddr, sz);
    629  1.110   thorpej 
    630  1.110   thorpej 		/* Remove the last segment if it now has no pages. */
    631  1.110   thorpej 		if (vps->start == vps->end)
    632  1.110   thorpej 			vm_nphysseg--;
    633  1.110   thorpej 
    634  1.110   thorpej 		/* warn if the message buffer had to be shrunk */
    635  1.203     enami 		if (sz != reqsz)
    636  1.203     enami 			printf("WARNING: %ld bytes not available for msgbuf "
    637  1.203     enami 			    "in last cluster (%ld used)\n", reqsz, sz);
    638  1.268   thorpej 
    639  1.110   thorpej 	}
    640  1.239   thorpej 
    641  1.239   thorpej 	/*
    642  1.268   thorpej 	 * NOTE: It is safe to use uvm_pageboot_alloc() before
    643  1.268   thorpej 	 * pmap_bootstrap() because our pmap_virtual_space()
    644  1.268   thorpej 	 * returns compile-time constants.
    645  1.268   thorpej 	 */
    646  1.268   thorpej 
    647  1.268   thorpej 	/*
    648   1.95   thorpej 	 * Init mapping for u page(s) for proc 0
    649    1.1       cgd 	 */
    650  1.261   thorpej 	lwp0.l_addr = proc0paddr =
    651  1.268   thorpej 	    (struct user *)uvm_pageboot_alloc(UPAGES * PAGE_SIZE);
    652    1.1       cgd 
    653    1.1       cgd 	/*
    654   1.95   thorpej 	 * Allocate space for system data structures.  These data structures
    655   1.95   thorpej 	 * are allocated here instead of cpu_startup() because physical
    656   1.95   thorpej 	 * memory is directly addressable.  We don't have to map these into
    657   1.95   thorpej 	 * virtual address space.
    658   1.95   thorpej 	 */
    659  1.198   thorpej 	size = (vsize_t)allocsys(NULL, NULL);
    660  1.268   thorpej 	v = (caddr_t)uvm_pageboot_alloc(size);
    661  1.198   thorpej 	if ((allocsys(v, NULL) - v) != size)
    662   1.95   thorpej 		panic("alpha_init: table size inconsistency");
    663    1.1       cgd 
    664    1.1       cgd 	/*
    665    1.1       cgd 	 * Initialize the virtual memory system, and set the
    666    1.1       cgd 	 * page table base register in proc 0's PCB.
    667    1.1       cgd 	 */
    668  1.110   thorpej 	pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
    669  1.144   thorpej 	    hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
    670    1.1       cgd 
    671    1.1       cgd 	/*
    672    1.3       cgd 	 * Initialize the rest of proc 0's PCB, and cache its physical
    673    1.3       cgd 	 * address.
    674    1.3       cgd 	 */
    675  1.261   thorpej 	lwp0.l_md.md_pcbpaddr =
    676  1.140   thorpej 	    (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
    677    1.3       cgd 
    678    1.3       cgd 	/*
    679    1.3       cgd 	 * Set the kernel sp, reserving space for an (empty) trapframe,
    680    1.3       cgd 	 * and make proc0's trapframe pointer point to it for sanity.
    681    1.3       cgd 	 */
    682   1.33       cgd 	proc0paddr->u_pcb.pcb_hw.apcb_ksp =
    683    1.3       cgd 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
    684  1.261   thorpej 	lwp0.l_md.md_tf =
    685   1.81   thorpej 	    (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
    686  1.235   thorpej 	simple_lock_init(&proc0paddr->u_pcb.pcb_fpcpu_slock);
    687  1.189   thorpej 
    688  1.189   thorpej 	/*
    689  1.208   thorpej 	 * Initialize the primary CPU's idle PCB to proc0's.  In a
    690  1.208   thorpej 	 * MULTIPROCESSOR configuration, each CPU will later get
    691  1.208   thorpej 	 * its own idle PCB when autoconfiguration runs.
    692  1.189   thorpej 	 */
    693  1.211   thorpej 	ci->ci_idle_pcb = &proc0paddr->u_pcb;
    694  1.261   thorpej 	ci->ci_idle_pcb_paddr = (u_long)lwp0.l_md.md_pcbpaddr;
    695  1.208   thorpej 
    696  1.208   thorpej 	/* Indicate that proc0 has a CPU. */
    697  1.261   thorpej 	lwp0.l_cpu = ci;
    698    1.1       cgd 
    699    1.1       cgd 	/*
    700   1.25       cgd 	 * Look at arguments passed to us and compute boothowto.
    701    1.8       cgd 	 */
    702    1.1       cgd 
    703    1.8       cgd 	boothowto = RB_SINGLE;
    704    1.1       cgd #ifdef KADB
    705    1.1       cgd 	boothowto |= RB_KDB;
    706    1.1       cgd #endif
    707  1.102       cgd 	for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
    708   1.26       cgd 		/*
    709   1.26       cgd 		 * Note that we'd really like to differentiate case here,
    710   1.26       cgd 		 * but the Alpha AXP Architecture Reference Manual
    711   1.26       cgd 		 * says that we shouldn't.
    712   1.26       cgd 		 */
    713    1.8       cgd 		switch (*p) {
    714   1.26       cgd 		case 'a': /* autoboot */
    715   1.26       cgd 		case 'A':
    716   1.26       cgd 			boothowto &= ~RB_SINGLE;
    717   1.21       cgd 			break;
    718   1.21       cgd 
    719   1.43       cgd #ifdef DEBUG
    720   1.43       cgd 		case 'c': /* crash dump immediately after autoconfig */
    721   1.43       cgd 		case 'C':
    722   1.43       cgd 			boothowto |= RB_DUMP;
    723   1.43       cgd 			break;
    724   1.43       cgd #endif
    725   1.43       cgd 
    726   1.81   thorpej #if defined(KGDB) || defined(DDB)
    727   1.81   thorpej 		case 'd': /* break into the kernel debugger ASAP */
    728   1.81   thorpej 		case 'D':
    729   1.81   thorpej 			boothowto |= RB_KDB;
    730   1.81   thorpej 			break;
    731   1.81   thorpej #endif
    732   1.81   thorpej 
    733   1.36       cgd 		case 'h': /* always halt, never reboot */
    734   1.36       cgd 		case 'H':
    735   1.36       cgd 			boothowto |= RB_HALT;
    736    1.8       cgd 			break;
    737    1.8       cgd 
    738   1.21       cgd #if 0
    739    1.8       cgd 		case 'm': /* mini root present in memory */
    740   1.26       cgd 		case 'M':
    741    1.8       cgd 			boothowto |= RB_MINIROOT;
    742    1.8       cgd 			break;
    743   1.21       cgd #endif
    744   1.36       cgd 
    745   1.36       cgd 		case 'n': /* askname */
    746   1.36       cgd 		case 'N':
    747   1.36       cgd 			boothowto |= RB_ASKNAME;
    748   1.65       cgd 			break;
    749   1.65       cgd 
    750   1.65       cgd 		case 's': /* single-user (default, supported for sanity) */
    751   1.65       cgd 		case 'S':
    752   1.65       cgd 			boothowto |= RB_SINGLE;
    753  1.221  jdolecek 			break;
    754  1.221  jdolecek 
    755  1.221  jdolecek 		case 'q': /* quiet boot */
    756  1.221  jdolecek 		case 'Q':
    757  1.221  jdolecek 			boothowto |= AB_QUIET;
    758  1.221  jdolecek 			break;
    759  1.221  jdolecek 
    760  1.221  jdolecek 		case 'v': /* verbose boot */
    761  1.221  jdolecek 		case 'V':
    762  1.221  jdolecek 			boothowto |= AB_VERBOSE;
    763  1.119   thorpej 			break;
    764  1.119   thorpej 
    765  1.119   thorpej 		case '-':
    766  1.119   thorpej 			/*
    767  1.119   thorpej 			 * Just ignore this.  It's not required, but it's
    768  1.119   thorpej 			 * common for it to be passed regardless.
    769  1.119   thorpej 			 */
    770   1.65       cgd 			break;
    771   1.65       cgd 
    772   1.65       cgd 		default:
    773   1.65       cgd 			printf("Unrecognized boot flag '%c'.\n", *p);
    774   1.36       cgd 			break;
    775    1.1       cgd 		}
    776    1.1       cgd 	}
    777    1.1       cgd 
    778  1.136    mjacob 
    779  1.136    mjacob 	/*
    780  1.136    mjacob 	 * Figure out the number of cpus in the box, from RPB fields.
    781  1.136    mjacob 	 * Really.  We mean it.
    782  1.136    mjacob 	 */
    783  1.136    mjacob 	for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
    784  1.136    mjacob 		struct pcs *pcsp;
    785  1.136    mjacob 
    786  1.144   thorpej 		pcsp = LOCATE_PCS(hwrpb, i);
    787  1.136    mjacob 		if ((pcsp->pcs_flags & PCS_PP) != 0)
    788  1.136    mjacob 			ncpus++;
    789  1.136    mjacob 	}
    790  1.136    mjacob 
    791    1.7       cgd 	/*
    792  1.106       cgd 	 * Initialize debuggers, and break into them if appropriate.
    793  1.106       cgd 	 */
    794  1.266     ragge #if NKSYMS || defined(DDB) || defined(LKM)
    795  1.266     ragge 	ksyms_init((int)((u_int64_t)ksym_end - (u_int64_t)ksym_start),
    796  1.159    mjacob 	    ksym_start, ksym_end);
    797  1.234   thorpej #endif
    798  1.234   thorpej 
    799  1.234   thorpej 	if (boothowto & RB_KDB) {
    800  1.234   thorpej #if defined(KGDB)
    801  1.234   thorpej 		kgdb_debug_init = 1;
    802  1.234   thorpej 		kgdb_connect(1);
    803  1.234   thorpej #elif defined(DDB)
    804  1.106       cgd 		Debugger();
    805  1.106       cgd #endif
    806  1.234   thorpej 	}
    807  1.234   thorpej 
    808  1.106       cgd 	/*
    809  1.106       cgd 	 * Figure out our clock frequency, from RPB fields.
    810  1.106       cgd 	 */
    811  1.106       cgd 	hz = hwrpb->rpb_intr_freq >> 12;
    812  1.106       cgd 	if (!(60 <= hz && hz <= 10240)) {
    813  1.106       cgd 		hz = 1024;
    814  1.106       cgd #ifdef DIAGNOSTIC
    815  1.106       cgd 		printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
    816  1.106       cgd 			hwrpb->rpb_intr_freq, hz);
    817  1.106       cgd #endif
    818  1.106       cgd 	}
    819   1.95   thorpej }
    820   1.95   thorpej 
    821   1.18       cgd void
    822    1.1       cgd consinit()
    823    1.1       cgd {
    824   1.81   thorpej 
    825  1.106       cgd 	/*
    826  1.106       cgd 	 * Everything related to console initialization is done
    827  1.106       cgd 	 * in alpha_init().
    828  1.106       cgd 	 */
    829  1.106       cgd #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
    830  1.106       cgd 	printf("consinit: %susing prom console\n",
    831  1.106       cgd 	    pmap_uses_prom_console() ? "" : "not ");
    832   1.81   thorpej #endif
    833    1.1       cgd }
    834  1.118   thorpej 
    835  1.118   thorpej #include "pckbc.h"
    836  1.118   thorpej #include "pckbd.h"
    837  1.118   thorpej #if (NPCKBC > 0) && (NPCKBD == 0)
    838  1.118   thorpej 
    839  1.187   thorpej #include <dev/ic/pckbcvar.h>
    840  1.118   thorpej 
    841  1.118   thorpej /*
    842  1.118   thorpej  * This is called by the pbkbc driver if no pckbd is configured.
    843  1.118   thorpej  * On the i386, it is used to glue in the old, deprecated console
    844  1.118   thorpej  * code.  On the Alpha, it does nothing.
    845  1.118   thorpej  */
    846  1.118   thorpej int
    847  1.118   thorpej pckbc_machdep_cnattach(kbctag, kbcslot)
    848  1.118   thorpej 	pckbc_tag_t kbctag;
    849  1.118   thorpej 	pckbc_slot_t kbcslot;
    850  1.118   thorpej {
    851  1.118   thorpej 
    852  1.118   thorpej 	return (ENXIO);
    853  1.118   thorpej }
    854  1.118   thorpej #endif /* NPCKBC > 0 && NPCKBD == 0 */
    855    1.1       cgd 
    856   1.18       cgd void
    857    1.1       cgd cpu_startup()
    858    1.1       cgd {
    859  1.257   thorpej 	u_int i, base, residual;
    860  1.140   thorpej 	vaddr_t minaddr, maxaddr;
    861  1.140   thorpej 	vsize_t size;
    862  1.173     lukem 	char pbuf[9];
    863   1.40       cgd #if defined(DEBUG)
    864    1.1       cgd 	extern int pmapdebug;
    865    1.1       cgd 	int opmapdebug = pmapdebug;
    866    1.1       cgd 
    867    1.1       cgd 	pmapdebug = 0;
    868    1.1       cgd #endif
    869    1.1       cgd 
    870    1.1       cgd 	/*
    871    1.1       cgd 	 * Good {morning,afternoon,evening,night}.
    872    1.1       cgd 	 */
    873   1.46  christos 	printf(version);
    874    1.1       cgd 	identifycpu();
    875  1.185   thorpej 	format_bytes(pbuf, sizeof(pbuf), ptoa(totalphysmem));
    876  1.173     lukem 	printf("total memory = %s\n", pbuf);
    877  1.173     lukem 	format_bytes(pbuf, sizeof(pbuf), ptoa(resvmem));
    878  1.173     lukem 	printf("(%s reserved for PROM, ", pbuf);
    879  1.173     lukem 	format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
    880  1.173     lukem 	printf("%s used by NetBSD)\n", pbuf);
    881  1.173     lukem 	if (unusedmem) {
    882  1.185   thorpej 		format_bytes(pbuf, sizeof(pbuf), ptoa(unusedmem));
    883  1.173     lukem 		printf("WARNING: unused memory = %s\n", pbuf);
    884  1.173     lukem 	}
    885  1.173     lukem 	if (unknownmem) {
    886  1.185   thorpej 		format_bytes(pbuf, sizeof(pbuf), ptoa(unknownmem));
    887  1.173     lukem 		printf("WARNING: %s of memory with unknown purpose\n", pbuf);
    888  1.173     lukem 	}
    889    1.1       cgd 
    890    1.1       cgd 	/*
    891    1.1       cgd 	 * Allocate virtual address space for file I/O buffers.
    892    1.1       cgd 	 * Note they are different than the array of headers, 'buf',
    893    1.1       cgd 	 * and usually occupy more virtual memory than physical.
    894    1.1       cgd 	 */
    895    1.1       cgd 	size = MAXBSIZE * nbuf;
    896  1.140   thorpej 	if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
    897  1.220   thorpej 		    NULL, UVM_UNKNOWN_OFFSET, 0,
    898  1.112   thorpej 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    899  1.231       chs 				UVM_ADV_NORMAL, 0)) != 0)
    900  1.112   thorpej 		panic("startup: cannot allocate VM for buffers");
    901    1.1       cgd 	base = bufpages / nbuf;
    902    1.1       cgd 	residual = bufpages % nbuf;
    903    1.1       cgd 	for (i = 0; i < nbuf; i++) {
    904  1.140   thorpej 		vsize_t curbufsize;
    905  1.140   thorpej 		vaddr_t curbuf;
    906  1.112   thorpej 		struct vm_page *pg;
    907  1.112   thorpej 
    908  1.112   thorpej 		/*
    909  1.112   thorpej 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    910  1.112   thorpej 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    911  1.112   thorpej 		 * for the first "residual" buffers, and then we allocate
    912  1.112   thorpej 		 * "base" pages for the rest.
    913  1.112   thorpej 		 */
    914  1.140   thorpej 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
    915  1.262   thorpej 		curbufsize = PAGE_SIZE * ((i < residual) ? (base+1) : base);
    916  1.112   thorpej 
    917  1.112   thorpej 		while (curbufsize) {
    918  1.168       chs 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    919  1.112   thorpej 			if (pg == NULL)
    920  1.112   thorpej 				panic("cpu_startup: not enough memory for "
    921  1.112   thorpej 				    "buffer cache");
    922  1.182       chs 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    923  1.182       chs 					VM_PROT_READ|VM_PROT_WRITE);
    924  1.112   thorpej 			curbuf += PAGE_SIZE;
    925  1.112   thorpej 			curbufsize -= PAGE_SIZE;
    926  1.112   thorpej 		}
    927    1.1       cgd 	}
    928  1.249     chris 	pmap_update(pmap_kernel());
    929  1.240   thorpej 
    930    1.1       cgd 	/*
    931    1.1       cgd 	 * Allocate a submap for exec arguments.  This map effectively
    932    1.1       cgd 	 * limits the number of processes exec'ing at any time.
    933    1.1       cgd 	 */
    934  1.112   thorpej 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    935  1.175   thorpej 				   16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    936    1.1       cgd 
    937    1.1       cgd 	/*
    938    1.1       cgd 	 * Allocate a submap for physio
    939    1.1       cgd 	 */
    940  1.112   thorpej 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    941  1.175   thorpej 				   VM_PHYS_SIZE, 0, FALSE, NULL);
    942    1.1       cgd 
    943    1.1       cgd 	/*
    944  1.164   thorpej 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    945  1.164   thorpej 	 * are allocated via the pool allocator, and we use K0SEG to
    946  1.164   thorpej 	 * map those pages.
    947    1.1       cgd 	 */
    948    1.1       cgd 
    949   1.40       cgd #if defined(DEBUG)
    950    1.1       cgd 	pmapdebug = opmapdebug;
    951    1.1       cgd #endif
    952  1.173     lukem 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    953  1.173     lukem 	printf("avail memory = %s\n", pbuf);
    954  1.139   thorpej #if 0
    955  1.139   thorpej 	{
    956  1.139   thorpej 		extern u_long pmap_pages_stolen;
    957  1.173     lukem 
    958  1.173     lukem 		format_bytes(pbuf, sizeof(pbuf), pmap_pages_stolen * PAGE_SIZE);
    959  1.173     lukem 		printf("stolen memory for VM structures = %s\n", pbuf);
    960  1.139   thorpej 	}
    961  1.112   thorpej #endif
    962  1.262   thorpej 	format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
    963  1.257   thorpej 	printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
    964    1.1       cgd 
    965    1.1       cgd 	/*
    966    1.1       cgd 	 * Set up buffers, so they can be used to read disk labels.
    967    1.1       cgd 	 */
    968    1.1       cgd 	bufinit();
    969  1.151   thorpej 
    970  1.151   thorpej 	/*
    971  1.151   thorpej 	 * Set up the HWPCB so that it's safe to configure secondary
    972  1.151   thorpej 	 * CPUs.
    973  1.151   thorpej 	 */
    974  1.151   thorpej 	hwrpb_primary_init();
    975  1.104   thorpej }
    976  1.104   thorpej 
    977  1.104   thorpej /*
    978  1.104   thorpej  * Retrieve the platform name from the DSR.
    979  1.104   thorpej  */
    980  1.104   thorpej const char *
    981  1.104   thorpej alpha_dsr_sysname()
    982  1.104   thorpej {
    983  1.104   thorpej 	struct dsrdb *dsr;
    984  1.104   thorpej 	const char *sysname;
    985  1.104   thorpej 
    986  1.104   thorpej 	/*
    987  1.104   thorpej 	 * DSR does not exist on early HWRPB versions.
    988  1.104   thorpej 	 */
    989  1.104   thorpej 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
    990  1.104   thorpej 		return (NULL);
    991  1.104   thorpej 
    992  1.104   thorpej 	dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
    993  1.104   thorpej 	sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
    994  1.104   thorpej 	    sizeof(u_int64_t)));
    995  1.104   thorpej 	return (sysname);
    996  1.104   thorpej }
    997  1.104   thorpej 
    998  1.104   thorpej /*
    999  1.104   thorpej  * Lookup the system specified system variation in the provided table,
   1000  1.104   thorpej  * returning the model string on match.
   1001  1.104   thorpej  */
   1002  1.104   thorpej const char *
   1003  1.104   thorpej alpha_variation_name(variation, avtp)
   1004  1.104   thorpej 	u_int64_t variation;
   1005  1.104   thorpej 	const struct alpha_variation_table *avtp;
   1006  1.104   thorpej {
   1007  1.104   thorpej 	int i;
   1008  1.104   thorpej 
   1009  1.104   thorpej 	for (i = 0; avtp[i].avt_model != NULL; i++)
   1010  1.104   thorpej 		if (avtp[i].avt_variation == variation)
   1011  1.104   thorpej 			return (avtp[i].avt_model);
   1012  1.104   thorpej 	return (NULL);
   1013  1.104   thorpej }
   1014  1.104   thorpej 
   1015  1.104   thorpej /*
   1016  1.104   thorpej  * Generate a default platform name based for unknown system variations.
   1017  1.104   thorpej  */
   1018  1.104   thorpej const char *
   1019  1.104   thorpej alpha_unknown_sysname()
   1020  1.104   thorpej {
   1021  1.105   thorpej 	static char s[128];		/* safe size */
   1022  1.104   thorpej 
   1023  1.105   thorpej 	sprintf(s, "%s family, unknown model variation 0x%lx",
   1024  1.105   thorpej 	    platform.family, hwrpb->rpb_variation & SV_ST_MASK);
   1025  1.104   thorpej 	return ((const char *)s);
   1026    1.1       cgd }
   1027    1.1       cgd 
   1028   1.33       cgd void
   1029    1.1       cgd identifycpu()
   1030    1.1       cgd {
   1031  1.177      ross 	char *s;
   1032  1.218   thorpej 	int i;
   1033    1.1       cgd 
   1034    1.7       cgd 	/*
   1035    1.7       cgd 	 * print out CPU identification information.
   1036    1.7       cgd 	 */
   1037  1.177      ross 	printf("%s", cpu_model);
   1038  1.177      ross 	for(s = cpu_model; *s; ++s)
   1039  1.177      ross 		if(strncasecmp(s, "MHz", 3) == 0)
   1040  1.177      ross 			goto skipMHz;
   1041  1.177      ross 	printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
   1042  1.177      ross skipMHz:
   1043  1.218   thorpej 	printf(", s/n ");
   1044  1.218   thorpej 	for (i = 0; i < 10; i++)
   1045  1.218   thorpej 		printf("%c", hwrpb->rpb_ssn[i]);
   1046  1.177      ross 	printf("\n");
   1047   1.46  christos 	printf("%ld byte page size, %d processor%s.\n",
   1048    1.7       cgd 	    hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
   1049    1.7       cgd #if 0
   1050    1.7       cgd 	/* this isn't defined for any systems that we run on? */
   1051   1.46  christos 	printf("serial number 0x%lx 0x%lx\n",
   1052    1.1       cgd 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
   1053    1.7       cgd 
   1054    1.7       cgd 	/* and these aren't particularly useful! */
   1055   1.46  christos 	printf("variation: 0x%lx, revision 0x%lx\n",
   1056    1.1       cgd 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
   1057    1.7       cgd #endif
   1058    1.1       cgd }
   1059    1.1       cgd 
   1060    1.1       cgd int	waittime = -1;
   1061    1.7       cgd struct pcb dumppcb;
   1062    1.1       cgd 
   1063   1.18       cgd void
   1064   1.68       gwr cpu_reboot(howto, bootstr)
   1065    1.1       cgd 	int howto;
   1066   1.39       mrg 	char *bootstr;
   1067    1.1       cgd {
   1068  1.148   thorpej #if defined(MULTIPROCESSOR)
   1069  1.225   thorpej 	u_long cpu_id = cpu_number();
   1070  1.225   thorpej 	u_long wait_mask = (1UL << cpu_id) |
   1071  1.225   thorpej 			   (1UL << hwrpb->rpb_primary_cpu_id);
   1072  1.225   thorpej 	int i;
   1073  1.148   thorpej #endif
   1074  1.148   thorpej 
   1075  1.225   thorpej 	/* If "always halt" was specified as a boot flag, obey. */
   1076  1.225   thorpej 	if ((boothowto & RB_HALT) != 0)
   1077  1.225   thorpej 		howto |= RB_HALT;
   1078  1.225   thorpej 
   1079  1.225   thorpej 	boothowto = howto;
   1080    1.1       cgd 
   1081    1.1       cgd 	/* If system is cold, just halt. */
   1082    1.1       cgd 	if (cold) {
   1083  1.225   thorpej 		boothowto |= RB_HALT;
   1084    1.1       cgd 		goto haltsys;
   1085    1.1       cgd 	}
   1086    1.1       cgd 
   1087  1.225   thorpej 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
   1088    1.1       cgd 		waittime = 0;
   1089    1.7       cgd 		vfs_shutdown();
   1090    1.1       cgd 		/*
   1091    1.1       cgd 		 * If we've been adjusting the clock, the todr
   1092    1.1       cgd 		 * will be out of synch; adjust it now.
   1093    1.1       cgd 		 */
   1094    1.1       cgd 		resettodr();
   1095    1.1       cgd 	}
   1096    1.1       cgd 
   1097    1.1       cgd 	/* Disable interrupts. */
   1098    1.1       cgd 	splhigh();
   1099    1.1       cgd 
   1100  1.225   thorpej #if defined(MULTIPROCESSOR)
   1101  1.225   thorpej 	/*
   1102  1.225   thorpej 	 * Halt all other CPUs.  If we're not the primary, the
   1103  1.225   thorpej 	 * primary will spin, waiting for us to halt.
   1104  1.225   thorpej 	 */
   1105  1.225   thorpej 	alpha_broadcast_ipi(ALPHA_IPI_HALT);
   1106  1.225   thorpej 
   1107  1.225   thorpej 	for (i = 0; i < 10000; i++) {
   1108  1.225   thorpej 		alpha_mb();
   1109  1.225   thorpej 		if (cpus_running == wait_mask)
   1110  1.225   thorpej 			break;
   1111  1.225   thorpej 		delay(1000);
   1112  1.225   thorpej 	}
   1113  1.225   thorpej 	alpha_mb();
   1114  1.225   thorpej 	if (cpus_running != wait_mask)
   1115  1.225   thorpej 		printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
   1116  1.225   thorpej 		    cpus_running);
   1117  1.225   thorpej #endif /* MULTIPROCESSOR */
   1118  1.225   thorpej 
   1119    1.7       cgd 	/* If rebooting and a dump is requested do it. */
   1120   1.42       cgd #if 0
   1121  1.225   thorpej 	if ((boothowto & (RB_DUMP | RB_HALT)) == RB_DUMP)
   1122   1.42       cgd #else
   1123  1.225   thorpej 	if (boothowto & RB_DUMP)
   1124   1.42       cgd #endif
   1125    1.1       cgd 		dumpsys();
   1126    1.6       cgd 
   1127   1.12       cgd haltsys:
   1128   1.12       cgd 
   1129    1.6       cgd 	/* run any shutdown hooks */
   1130    1.6       cgd 	doshutdownhooks();
   1131  1.148   thorpej 
   1132    1.7       cgd #ifdef BOOTKEY
   1133   1.46  christos 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
   1134  1.117  drochner 	cnpollc(1);	/* for proper keyboard command handling */
   1135    1.7       cgd 	cngetc();
   1136  1.117  drochner 	cnpollc(0);
   1137   1.46  christos 	printf("\n");
   1138    1.7       cgd #endif
   1139    1.7       cgd 
   1140  1.124   thorpej 	/* Finally, powerdown/halt/reboot the system. */
   1141  1.225   thorpej 	if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN &&
   1142  1.124   thorpej 	    platform.powerdown != NULL) {
   1143  1.124   thorpej 		(*platform.powerdown)();
   1144  1.124   thorpej 		printf("WARNING: powerdown failed!\n");
   1145  1.124   thorpej 	}
   1146  1.225   thorpej 	printf("%s\n\n", (boothowto & RB_HALT) ? "halted." : "rebooting...");
   1147  1.225   thorpej #if defined(MULTIPROCESSOR)
   1148  1.225   thorpej 	if (cpu_id != hwrpb->rpb_primary_cpu_id)
   1149  1.225   thorpej 		cpu_halt();
   1150  1.225   thorpej 	else
   1151  1.225   thorpej #endif
   1152  1.225   thorpej 		prom_halt(boothowto & RB_HALT);
   1153    1.1       cgd 	/*NOTREACHED*/
   1154    1.1       cgd }
   1155    1.1       cgd 
   1156    1.7       cgd /*
   1157    1.7       cgd  * These variables are needed by /sbin/savecore
   1158    1.7       cgd  */
   1159  1.253   tsutsui u_int32_t dumpmag = 0x8fca0101;	/* magic number */
   1160    1.7       cgd int 	dumpsize = 0;		/* pages */
   1161    1.7       cgd long	dumplo = 0; 		/* blocks */
   1162    1.7       cgd 
   1163    1.7       cgd /*
   1164   1.43       cgd  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
   1165   1.43       cgd  */
   1166   1.43       cgd int
   1167   1.43       cgd cpu_dumpsize()
   1168   1.43       cgd {
   1169   1.43       cgd 	int size;
   1170   1.43       cgd 
   1171  1.108       cgd 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
   1172  1.110   thorpej 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
   1173   1.43       cgd 	if (roundup(size, dbtob(1)) != dbtob(1))
   1174   1.43       cgd 		return -1;
   1175   1.43       cgd 
   1176   1.43       cgd 	return (1);
   1177   1.43       cgd }
   1178   1.43       cgd 
   1179   1.43       cgd /*
   1180  1.110   thorpej  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
   1181  1.110   thorpej  */
   1182  1.110   thorpej u_long
   1183  1.110   thorpej cpu_dump_mempagecnt()
   1184  1.110   thorpej {
   1185  1.110   thorpej 	u_long i, n;
   1186  1.110   thorpej 
   1187  1.110   thorpej 	n = 0;
   1188  1.110   thorpej 	for (i = 0; i < mem_cluster_cnt; i++)
   1189  1.110   thorpej 		n += atop(mem_clusters[i].size);
   1190  1.110   thorpej 	return (n);
   1191  1.110   thorpej }
   1192  1.110   thorpej 
   1193  1.110   thorpej /*
   1194   1.43       cgd  * cpu_dump: dump machine-dependent kernel core dump headers.
   1195   1.43       cgd  */
   1196   1.43       cgd int
   1197   1.43       cgd cpu_dump()
   1198   1.43       cgd {
   1199   1.43       cgd 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1200  1.107       cgd 	char buf[dbtob(1)];
   1201  1.107       cgd 	kcore_seg_t *segp;
   1202  1.107       cgd 	cpu_kcore_hdr_t *cpuhdrp;
   1203  1.107       cgd 	phys_ram_seg_t *memsegp;
   1204  1.258   gehenna 	const struct bdevsw *bdev;
   1205  1.110   thorpej 	int i;
   1206   1.43       cgd 
   1207  1.258   gehenna 	bdev = bdevsw_lookup(dumpdev);
   1208  1.258   gehenna 	if (bdev == NULL)
   1209  1.258   gehenna 		return (ENXIO);
   1210  1.258   gehenna 	dump = bdev->d_dump;
   1211   1.43       cgd 
   1212  1.246   thorpej 	memset(buf, 0, sizeof buf);
   1213   1.43       cgd 	segp = (kcore_seg_t *)buf;
   1214  1.107       cgd 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
   1215  1.107       cgd 	memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
   1216  1.107       cgd 	    ALIGN(sizeof(*cpuhdrp))];
   1217   1.43       cgd 
   1218   1.43       cgd 	/*
   1219   1.43       cgd 	 * Generate a segment header.
   1220   1.43       cgd 	 */
   1221   1.43       cgd 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
   1222   1.43       cgd 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
   1223   1.43       cgd 
   1224   1.43       cgd 	/*
   1225  1.107       cgd 	 * Add the machine-dependent header info.
   1226   1.43       cgd 	 */
   1227  1.140   thorpej 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
   1228   1.43       cgd 	cpuhdrp->page_size = PAGE_SIZE;
   1229  1.110   thorpej 	cpuhdrp->nmemsegs = mem_cluster_cnt;
   1230  1.107       cgd 
   1231  1.107       cgd 	/*
   1232  1.107       cgd 	 * Fill in the memory segment descriptors.
   1233  1.107       cgd 	 */
   1234  1.110   thorpej 	for (i = 0; i < mem_cluster_cnt; i++) {
   1235  1.110   thorpej 		memsegp[i].start = mem_clusters[i].start;
   1236  1.110   thorpej 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
   1237  1.110   thorpej 	}
   1238   1.43       cgd 
   1239   1.43       cgd 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
   1240   1.43       cgd }
   1241   1.43       cgd 
   1242   1.43       cgd /*
   1243   1.68       gwr  * This is called by main to set dumplo and dumpsize.
   1244  1.262   thorpej  * Dumps always skip the first PAGE_SIZE of disk space
   1245    1.7       cgd  * in case there might be a disk label stored there.
   1246    1.7       cgd  * If there is extra space, put dump at the end to
   1247    1.7       cgd  * reduce the chance that swapping trashes it.
   1248    1.7       cgd  */
   1249    1.7       cgd void
   1250   1.68       gwr cpu_dumpconf()
   1251    1.7       cgd {
   1252  1.258   gehenna 	const struct bdevsw *bdev;
   1253   1.43       cgd 	int nblks, dumpblks;	/* size of dump area */
   1254    1.7       cgd 
   1255    1.7       cgd 	if (dumpdev == NODEV)
   1256   1.43       cgd 		goto bad;
   1257  1.258   gehenna 	bdev = bdevsw_lookup(dumpdev);
   1258  1.258   gehenna 	if (bdev == NULL)
   1259    1.7       cgd 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
   1260  1.258   gehenna 	if (bdev->d_psize == NULL)
   1261   1.43       cgd 		goto bad;
   1262  1.258   gehenna 	nblks = (*bdev->d_psize)(dumpdev);
   1263    1.7       cgd 	if (nblks <= ctod(1))
   1264   1.43       cgd 		goto bad;
   1265   1.43       cgd 
   1266   1.43       cgd 	dumpblks = cpu_dumpsize();
   1267   1.43       cgd 	if (dumpblks < 0)
   1268   1.43       cgd 		goto bad;
   1269  1.110   thorpej 	dumpblks += ctod(cpu_dump_mempagecnt());
   1270   1.43       cgd 
   1271   1.43       cgd 	/* If dump won't fit (incl. room for possible label), punt. */
   1272   1.43       cgd 	if (dumpblks > (nblks - ctod(1)))
   1273   1.43       cgd 		goto bad;
   1274   1.43       cgd 
   1275   1.43       cgd 	/* Put dump at end of partition */
   1276   1.43       cgd 	dumplo = nblks - dumpblks;
   1277    1.7       cgd 
   1278   1.43       cgd 	/* dumpsize is in page units, and doesn't include headers. */
   1279  1.110   thorpej 	dumpsize = cpu_dump_mempagecnt();
   1280   1.43       cgd 	return;
   1281    1.7       cgd 
   1282   1.43       cgd bad:
   1283   1.43       cgd 	dumpsize = 0;
   1284   1.43       cgd 	return;
   1285    1.7       cgd }
   1286    1.7       cgd 
   1287    1.7       cgd /*
   1288   1.42       cgd  * Dump the kernel's image to the swap partition.
   1289    1.7       cgd  */
   1290  1.262   thorpej #define	BYTES_PER_DUMP	PAGE_SIZE
   1291   1.42       cgd 
   1292    1.7       cgd void
   1293    1.7       cgd dumpsys()
   1294    1.7       cgd {
   1295  1.258   gehenna 	const struct bdevsw *bdev;
   1296  1.110   thorpej 	u_long totalbytesleft, bytes, i, n, memcl;
   1297  1.110   thorpej 	u_long maddr;
   1298  1.110   thorpej 	int psize;
   1299   1.42       cgd 	daddr_t blkno;
   1300   1.42       cgd 	int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
   1301   1.42       cgd 	int error;
   1302   1.42       cgd 
   1303   1.42       cgd 	/* Save registers. */
   1304   1.42       cgd 	savectx(&dumppcb);
   1305    1.7       cgd 
   1306    1.7       cgd 	if (dumpdev == NODEV)
   1307    1.7       cgd 		return;
   1308  1.258   gehenna 	bdev = bdevsw_lookup(dumpdev);
   1309  1.258   gehenna 	if (bdev == NULL || bdev->d_psize == NULL)
   1310  1.258   gehenna 		return;
   1311   1.42       cgd 
   1312   1.42       cgd 	/*
   1313   1.42       cgd 	 * For dumps during autoconfiguration,
   1314   1.42       cgd 	 * if dump device has already configured...
   1315   1.42       cgd 	 */
   1316   1.42       cgd 	if (dumpsize == 0)
   1317   1.68       gwr 		cpu_dumpconf();
   1318   1.47       cgd 	if (dumplo <= 0) {
   1319   1.97   mycroft 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
   1320   1.97   mycroft 		    minor(dumpdev));
   1321   1.42       cgd 		return;
   1322   1.43       cgd 	}
   1323   1.97   mycroft 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
   1324   1.97   mycroft 	    minor(dumpdev), dumplo);
   1325    1.7       cgd 
   1326  1.258   gehenna 	psize = (*bdev->d_psize)(dumpdev);
   1327   1.46  christos 	printf("dump ");
   1328   1.42       cgd 	if (psize == -1) {
   1329   1.46  christos 		printf("area unavailable\n");
   1330   1.42       cgd 		return;
   1331   1.42       cgd 	}
   1332   1.42       cgd 
   1333   1.42       cgd 	/* XXX should purge all outstanding keystrokes. */
   1334   1.42       cgd 
   1335   1.43       cgd 	if ((error = cpu_dump()) != 0)
   1336   1.43       cgd 		goto err;
   1337   1.43       cgd 
   1338  1.110   thorpej 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
   1339   1.43       cgd 	blkno = dumplo + cpu_dumpsize();
   1340  1.258   gehenna 	dump = bdev->d_dump;
   1341   1.42       cgd 	error = 0;
   1342   1.42       cgd 
   1343  1.110   thorpej 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
   1344  1.110   thorpej 		maddr = mem_clusters[memcl].start;
   1345  1.110   thorpej 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
   1346  1.110   thorpej 
   1347  1.110   thorpej 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
   1348  1.110   thorpej 
   1349  1.110   thorpej 			/* Print out how many MBs we to go. */
   1350  1.110   thorpej 			if ((totalbytesleft % (1024*1024)) == 0)
   1351  1.160   thorpej 				printf("%ld ", totalbytesleft / (1024 * 1024));
   1352  1.110   thorpej 
   1353  1.110   thorpej 			/* Limit size for next transfer. */
   1354  1.110   thorpej 			n = bytes - i;
   1355  1.110   thorpej 			if (n > BYTES_PER_DUMP)
   1356  1.110   thorpej 				n =  BYTES_PER_DUMP;
   1357  1.110   thorpej 
   1358  1.110   thorpej 			error = (*dump)(dumpdev, blkno,
   1359  1.110   thorpej 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
   1360  1.110   thorpej 			if (error)
   1361  1.110   thorpej 				goto err;
   1362  1.110   thorpej 			maddr += n;
   1363  1.110   thorpej 			blkno += btodb(n);			/* XXX? */
   1364   1.42       cgd 
   1365  1.110   thorpej 			/* XXX should look for keystrokes, to cancel. */
   1366  1.110   thorpej 		}
   1367   1.42       cgd 	}
   1368   1.42       cgd 
   1369   1.43       cgd err:
   1370   1.42       cgd 	switch (error) {
   1371    1.7       cgd 
   1372    1.7       cgd 	case ENXIO:
   1373   1.46  christos 		printf("device bad\n");
   1374    1.7       cgd 		break;
   1375    1.7       cgd 
   1376    1.7       cgd 	case EFAULT:
   1377   1.46  christos 		printf("device not ready\n");
   1378    1.7       cgd 		break;
   1379    1.7       cgd 
   1380    1.7       cgd 	case EINVAL:
   1381   1.46  christos 		printf("area improper\n");
   1382    1.7       cgd 		break;
   1383    1.7       cgd 
   1384    1.7       cgd 	case EIO:
   1385   1.46  christos 		printf("i/o error\n");
   1386    1.7       cgd 		break;
   1387    1.7       cgd 
   1388    1.7       cgd 	case EINTR:
   1389   1.46  christos 		printf("aborted from console\n");
   1390    1.7       cgd 		break;
   1391    1.7       cgd 
   1392   1.42       cgd 	case 0:
   1393   1.46  christos 		printf("succeeded\n");
   1394   1.42       cgd 		break;
   1395   1.42       cgd 
   1396    1.7       cgd 	default:
   1397   1.46  christos 		printf("error %d\n", error);
   1398    1.7       cgd 		break;
   1399    1.7       cgd 	}
   1400   1.46  christos 	printf("\n\n");
   1401    1.7       cgd 	delay(1000);
   1402    1.7       cgd }
   1403    1.7       cgd 
   1404    1.1       cgd void
   1405    1.1       cgd frametoreg(framep, regp)
   1406  1.261   thorpej 	const struct trapframe *framep;
   1407    1.1       cgd 	struct reg *regp;
   1408    1.1       cgd {
   1409    1.1       cgd 
   1410    1.1       cgd 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
   1411    1.1       cgd 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
   1412    1.1       cgd 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
   1413    1.1       cgd 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
   1414    1.1       cgd 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
   1415    1.1       cgd 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
   1416    1.1       cgd 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
   1417    1.1       cgd 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
   1418    1.1       cgd 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
   1419    1.1       cgd 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
   1420    1.1       cgd 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
   1421    1.1       cgd 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
   1422    1.1       cgd 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
   1423    1.1       cgd 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
   1424    1.1       cgd 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
   1425    1.1       cgd 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
   1426   1.34       cgd 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
   1427   1.34       cgd 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
   1428   1.34       cgd 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
   1429    1.1       cgd 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
   1430    1.1       cgd 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
   1431    1.1       cgd 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
   1432    1.1       cgd 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
   1433    1.1       cgd 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
   1434    1.1       cgd 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
   1435    1.1       cgd 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
   1436    1.1       cgd 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
   1437    1.1       cgd 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
   1438    1.1       cgd 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
   1439   1.34       cgd 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
   1440   1.35       cgd 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
   1441    1.1       cgd 	regp->r_regs[R_ZERO] = 0;
   1442    1.1       cgd }
   1443    1.1       cgd 
   1444    1.1       cgd void
   1445    1.1       cgd regtoframe(regp, framep)
   1446  1.261   thorpej 	const struct reg *regp;
   1447    1.1       cgd 	struct trapframe *framep;
   1448    1.1       cgd {
   1449    1.1       cgd 
   1450    1.1       cgd 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
   1451    1.1       cgd 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
   1452    1.1       cgd 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
   1453    1.1       cgd 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
   1454    1.1       cgd 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
   1455    1.1       cgd 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
   1456    1.1       cgd 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
   1457    1.1       cgd 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
   1458    1.1       cgd 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
   1459    1.1       cgd 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
   1460    1.1       cgd 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
   1461    1.1       cgd 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
   1462    1.1       cgd 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
   1463    1.1       cgd 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
   1464    1.1       cgd 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
   1465    1.1       cgd 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
   1466   1.34       cgd 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
   1467   1.34       cgd 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
   1468   1.34       cgd 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
   1469    1.1       cgd 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
   1470    1.1       cgd 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
   1471    1.1       cgd 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
   1472    1.1       cgd 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
   1473    1.1       cgd 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
   1474    1.1       cgd 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
   1475    1.1       cgd 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
   1476    1.1       cgd 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
   1477    1.1       cgd 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
   1478    1.1       cgd 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
   1479   1.34       cgd 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
   1480   1.35       cgd 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
   1481    1.1       cgd 	/* ??? = regp->r_regs[R_ZERO]; */
   1482    1.1       cgd }
   1483    1.1       cgd 
   1484    1.1       cgd void
   1485    1.1       cgd printregs(regp)
   1486    1.1       cgd 	struct reg *regp;
   1487    1.1       cgd {
   1488    1.1       cgd 	int i;
   1489    1.1       cgd 
   1490    1.1       cgd 	for (i = 0; i < 32; i++)
   1491   1.46  christos 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
   1492    1.1       cgd 		   i & 1 ? "\n" : "\t");
   1493    1.1       cgd }
   1494    1.1       cgd 
   1495    1.1       cgd void
   1496    1.1       cgd regdump(framep)
   1497    1.1       cgd 	struct trapframe *framep;
   1498    1.1       cgd {
   1499    1.1       cgd 	struct reg reg;
   1500    1.1       cgd 
   1501    1.1       cgd 	frametoreg(framep, &reg);
   1502   1.35       cgd 	reg.r_regs[R_SP] = alpha_pal_rdusp();
   1503   1.35       cgd 
   1504   1.46  christos 	printf("REGISTERS:\n");
   1505    1.1       cgd 	printregs(&reg);
   1506    1.1       cgd }
   1507    1.1       cgd 
   1508    1.1       cgd 
   1509    1.1       cgd /*
   1510    1.1       cgd  * Send an interrupt to process.
   1511    1.1       cgd  */
   1512    1.1       cgd void
   1513  1.256   thorpej sendsig(sig, mask, code)
   1514  1.141   thorpej 	int sig;
   1515  1.141   thorpej 	sigset_t *mask;
   1516    1.1       cgd 	u_long code;
   1517    1.1       cgd {
   1518  1.261   thorpej 	struct lwp *l = curlwp;
   1519  1.261   thorpej 	struct proc *p = l->l_proc;
   1520  1.256   thorpej 	struct sigacts *ps = p->p_sigacts;
   1521    1.1       cgd 	struct sigcontext *scp, ksc;
   1522    1.1       cgd 	struct trapframe *frame;
   1523  1.141   thorpej 	int onstack, fsize, rndfsize;
   1524  1.256   thorpej 	sig_t catcher = SIGACTION(p, sig).sa_handler;
   1525    1.1       cgd 
   1526  1.261   thorpej 	frame = l->l_md.md_tf;
   1527  1.141   thorpej 
   1528  1.141   thorpej 	/* Do we need to jump onto the signal stack? */
   1529  1.141   thorpej 	onstack =
   1530  1.228  jdolecek 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
   1531  1.228  jdolecek 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
   1532  1.141   thorpej 
   1533  1.141   thorpej 	/* Allocate space for the signal handler context. */
   1534  1.141   thorpej 	fsize = sizeof(ksc);
   1535    1.1       cgd 	rndfsize = ((fsize + 15) / 16) * 16;
   1536  1.141   thorpej 
   1537  1.141   thorpej 	if (onstack)
   1538  1.228  jdolecek 		scp = (struct sigcontext *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
   1539  1.228  jdolecek 					p->p_sigctx.ps_sigstk.ss_size);
   1540  1.141   thorpej 	else
   1541  1.142   mycroft 		scp = (struct sigcontext *)(alpha_pal_rdusp());
   1542  1.142   mycroft 	scp = (struct sigcontext *)((caddr_t)scp - rndfsize);
   1543  1.141   thorpej 
   1544    1.1       cgd #ifdef DEBUG
   1545    1.1       cgd 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1546   1.46  christos 		printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
   1547  1.141   thorpej 		    sig, &onstack, scp);
   1548  1.125      ross #endif
   1549    1.1       cgd 
   1550  1.141   thorpej 	/* Build stack frame for signal trampoline. */
   1551   1.34       cgd 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
   1552   1.34       cgd 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
   1553    1.1       cgd 
   1554  1.141   thorpej 	/* Save register context. */
   1555    1.1       cgd 	frametoreg(frame, (struct reg *)ksc.sc_regs);
   1556    1.1       cgd 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
   1557   1.35       cgd 	ksc.sc_regs[R_SP] = alpha_pal_rdusp();
   1558    1.1       cgd 
   1559  1.261   thorpej  	/* save the floating-point state, if necessary, then copy it. */
   1560  1.261   thorpej 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1561  1.261   thorpej 		fpusave_proc(l, 1);
   1562  1.261   thorpej 	ksc.sc_ownedfp = l->l_md.md_flags & MDP_FPUSED;
   1563  1.261   thorpej 	memcpy((struct fpreg *)ksc.sc_fpregs, &l->l_addr->u_pcb.pcb_fp,
   1564    1.1       cgd 	    sizeof(struct fpreg));
   1565  1.261   thorpej 	ksc.sc_fp_control = alpha_read_fp_c(l);
   1566  1.246   thorpej 	memset(ksc.sc_reserved, 0, sizeof ksc.sc_reserved);	/* XXX */
   1567  1.246   thorpej 	memset(ksc.sc_xxx, 0, sizeof ksc.sc_xxx);		/* XXX */
   1568    1.1       cgd 
   1569  1.141   thorpej 	/* Save signal stack. */
   1570  1.228  jdolecek 	ksc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
   1571  1.141   thorpej 
   1572  1.141   thorpej 	/* Save signal mask. */
   1573  1.141   thorpej 	ksc.sc_mask = *mask;
   1574  1.141   thorpej 
   1575  1.141   thorpej #ifdef COMPAT_13
   1576  1.141   thorpej 	/*
   1577  1.141   thorpej 	 * XXX We always have to save an old style signal mask because
   1578  1.141   thorpej 	 * XXX we might be delivering a signal to a process which will
   1579  1.141   thorpej 	 * XXX escape from the signal in a non-standard way and invoke
   1580  1.141   thorpej 	 * XXX sigreturn() directly.
   1581  1.141   thorpej 	 */
   1582  1.141   thorpej 	{
   1583  1.141   thorpej 		/* Note: it's a long in the stack frame. */
   1584  1.141   thorpej 		sigset13_t mask13;
   1585  1.141   thorpej 
   1586  1.141   thorpej 		native_sigset_to_sigset13(mask, &mask13);
   1587  1.141   thorpej 		ksc.__sc_mask13 = mask13;
   1588  1.141   thorpej 	}
   1589  1.141   thorpej #endif
   1590    1.1       cgd 
   1591    1.1       cgd #ifdef COMPAT_OSF1
   1592    1.1       cgd 	/*
   1593    1.1       cgd 	 * XXX Create an OSF/1-style sigcontext and associated goo.
   1594    1.1       cgd 	 */
   1595    1.1       cgd #endif
   1596    1.1       cgd 
   1597  1.141   thorpej 	if (copyout(&ksc, (caddr_t)scp, fsize) != 0) {
   1598  1.141   thorpej 		/*
   1599  1.141   thorpej 		 * Process has trashed its stack; give it an illegal
   1600  1.141   thorpej 		 * instruction to halt it in its tracks.
   1601  1.141   thorpej 		 */
   1602  1.141   thorpej #ifdef DEBUG
   1603  1.141   thorpej 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1604  1.141   thorpej 			printf("sendsig(%d): copyout failed on sig %d\n",
   1605  1.141   thorpej 			    p->p_pid, sig);
   1606  1.141   thorpej #endif
   1607  1.261   thorpej 		sigexit(l, SIGILL);
   1608  1.141   thorpej 		/* NOTREACHED */
   1609  1.141   thorpej 	}
   1610    1.1       cgd #ifdef DEBUG
   1611    1.1       cgd 	if (sigdebug & SDB_FOLLOW)
   1612   1.46  christos 		printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
   1613    1.1       cgd 		    scp, code);
   1614    1.1       cgd #endif
   1615    1.1       cgd 
   1616  1.256   thorpej 	/*
   1617  1.256   thorpej 	 * Set up the registers to directly invoke the signal handler.  The
   1618  1.256   thorpej 	 * signal trampoline is then used to return from the signal.  Note
   1619  1.256   thorpej 	 * the trampoline version numbers are coordinated with machine-
   1620  1.256   thorpej 	 * dependent code in libc.
   1621  1.256   thorpej 	 */
   1622  1.256   thorpej 	switch (ps->sa_sigdesc[sig].sd_vers) {
   1623  1.256   thorpej #if 1 /* COMPAT_16 */
   1624  1.256   thorpej 	case 0:		/* legacy on-stack sigtramp */
   1625  1.256   thorpej 		frame->tf_regs[FRAME_RA] = (u_int64_t)p->p_sigctx.ps_sigcode;
   1626  1.256   thorpej 		break;
   1627  1.256   thorpej #endif /* COMPAT_16 */
   1628  1.256   thorpej 
   1629  1.256   thorpej 	case 1:
   1630  1.256   thorpej 		frame->tf_regs[FRAME_RA] =
   1631  1.256   thorpej 		    (u_int64_t)ps->sa_sigdesc[sig].sd_tramp;
   1632  1.256   thorpej 		break;
   1633  1.256   thorpej 
   1634  1.256   thorpej 	default:
   1635  1.256   thorpej 		/* Don't know what trampoline version; kill it. */
   1636  1.261   thorpej 		sigexit(l, SIGILL);
   1637  1.256   thorpej 	}
   1638  1.255   thorpej 	frame->tf_regs[FRAME_PC] = (u_int64_t)catcher;
   1639   1.34       cgd 	frame->tf_regs[FRAME_A0] = sig;
   1640   1.34       cgd 	frame->tf_regs[FRAME_A1] = code;
   1641   1.34       cgd 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
   1642  1.256   thorpej 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;
   1643   1.35       cgd 	alpha_pal_wrusp((unsigned long)scp);
   1644  1.142   mycroft 
   1645  1.142   mycroft 	/* Remember that we're now on the signal stack. */
   1646  1.142   mycroft 	if (onstack)
   1647  1.228  jdolecek 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
   1648    1.1       cgd 
   1649    1.1       cgd #ifdef DEBUG
   1650    1.1       cgd 	if (sigdebug & SDB_FOLLOW)
   1651   1.46  christos 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
   1652   1.34       cgd 		    frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
   1653    1.1       cgd 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
   1654   1.46  christos 		printf("sendsig(%d): sig %d returns\n",
   1655    1.1       cgd 		    p->p_pid, sig);
   1656    1.1       cgd #endif
   1657    1.1       cgd }
   1658    1.1       cgd 
   1659  1.261   thorpej 
   1660  1.261   thorpej void
   1661  1.261   thorpej cpu_upcall(struct lwp *l, int type, int nevents, int ninterrupted, void *sas, void *ap, void *sp, sa_upcall_t upcall)
   1662  1.261   thorpej {
   1663  1.261   thorpej        	struct trapframe *tf;
   1664  1.261   thorpej 
   1665  1.261   thorpej 	tf = l->l_md.md_tf;
   1666  1.261   thorpej 
   1667  1.261   thorpej 	tf->tf_regs[FRAME_PC] = (u_int64_t)upcall;
   1668  1.261   thorpej 	tf->tf_regs[FRAME_RA] = 0;
   1669  1.261   thorpej 	tf->tf_regs[FRAME_A0] = type;
   1670  1.261   thorpej 	tf->tf_regs[FRAME_A1] = (u_int64_t)sas;
   1671  1.261   thorpej 	tf->tf_regs[FRAME_A2] = nevents;
   1672  1.261   thorpej 	tf->tf_regs[FRAME_A3] = ninterrupted;
   1673  1.261   thorpej 	tf->tf_regs[FRAME_A4] = (u_int64_t)ap;
   1674  1.261   thorpej 	tf->tf_regs[FRAME_T12] = (u_int64_t)upcall;  /* t12 is pv */
   1675  1.261   thorpej 	alpha_pal_wrusp((unsigned long)sp);
   1676  1.261   thorpej }
   1677  1.261   thorpej 
   1678    1.1       cgd /*
   1679    1.1       cgd  * System call to cleanup state after a signal
   1680    1.1       cgd  * has been taken.  Reset signal mask and
   1681    1.1       cgd  * stack state from context left by sendsig (above).
   1682    1.1       cgd  * Return to previous pc and psl as specified by
   1683    1.1       cgd  * context left by sendsig. Check carefully to
   1684    1.1       cgd  * make sure that the user has not modified the
   1685  1.180    simonb  * psl to gain improper privileges or to cause
   1686    1.1       cgd  * a machine fault.
   1687    1.1       cgd  */
   1688    1.1       cgd /* ARGSUSED */
   1689   1.11   mycroft int
   1690  1.261   thorpej sys___sigreturn14(l, v, retval)
   1691  1.261   thorpej 	struct lwp *l;
   1692   1.10   thorpej 	void *v;
   1693   1.10   thorpej 	register_t *retval;
   1694   1.10   thorpej {
   1695  1.141   thorpej 	struct sys___sigreturn14_args /* {
   1696    1.1       cgd 		syscallarg(struct sigcontext *) sigcntxp;
   1697   1.10   thorpej 	} */ *uap = v;
   1698    1.1       cgd 	struct sigcontext *scp, ksc;
   1699  1.261   thorpej 	struct proc *p = l->l_proc;
   1700    1.1       cgd 
   1701  1.141   thorpej 	/*
   1702  1.141   thorpej 	 * The trampoline code hands us the context.
   1703  1.141   thorpej 	 * It is unsafe to keep track of it ourselves, in the event that a
   1704  1.141   thorpej 	 * program jumps out of a signal handler.
   1705  1.141   thorpej 	 */
   1706    1.1       cgd 	scp = SCARG(uap, sigcntxp);
   1707    1.1       cgd #ifdef DEBUG
   1708    1.1       cgd 	if (sigdebug & SDB_FOLLOW)
   1709   1.46  christos 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
   1710    1.1       cgd #endif
   1711    1.1       cgd 	if (ALIGN(scp) != (u_int64_t)scp)
   1712    1.1       cgd 		return (EINVAL);
   1713    1.1       cgd 
   1714  1.141   thorpej 	if (copyin((caddr_t)scp, &ksc, sizeof(ksc)) != 0)
   1715  1.141   thorpej 		return (EFAULT);
   1716    1.1       cgd 
   1717    1.1       cgd 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
   1718    1.1       cgd 		return (EINVAL);
   1719    1.1       cgd 
   1720  1.141   thorpej 	/* Restore register context. */
   1721  1.261   thorpej 	l->l_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
   1722  1.261   thorpej 	l->l_md.md_tf->tf_regs[FRAME_PS] =
   1723   1.32       cgd 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
   1724    1.1       cgd 
   1725  1.261   thorpej 	regtoframe((struct reg *)ksc.sc_regs, l->l_md.md_tf);
   1726   1.35       cgd 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
   1727    1.1       cgd 
   1728    1.1       cgd 	/* XXX ksc.sc_ownedfp ? */
   1729  1.261   thorpej 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1730  1.261   thorpej 		fpusave_proc(l, 0);
   1731  1.261   thorpej 	memcpy(&l->l_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
   1732    1.1       cgd 	    sizeof(struct fpreg));
   1733  1.261   thorpej 	l->l_addr->u_pcb.pcb_fp.fpr_cr = ksc.sc_fpcr;
   1734  1.261   thorpej 	l->l_md.md_flags = ksc.sc_fp_control & MDP_FP_C;
   1735  1.141   thorpej 
   1736  1.141   thorpej 	/* Restore signal stack. */
   1737  1.141   thorpej 	if (ksc.sc_onstack & SS_ONSTACK)
   1738  1.228  jdolecek 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
   1739  1.141   thorpej 	else
   1740  1.228  jdolecek 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
   1741  1.141   thorpej 
   1742  1.141   thorpej 	/* Restore signal mask. */
   1743  1.141   thorpej 	(void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0);
   1744    1.1       cgd 
   1745    1.1       cgd #ifdef DEBUG
   1746    1.1       cgd 	if (sigdebug & SDB_FOLLOW)
   1747   1.46  christos 		printf("sigreturn(%d): returns\n", p->p_pid);
   1748    1.1       cgd #endif
   1749    1.1       cgd 	return (EJUSTRETURN);
   1750    1.1       cgd }
   1751    1.1       cgd 
   1752    1.1       cgd /*
   1753    1.1       cgd  * machine dependent system variables.
   1754    1.1       cgd  */
   1755   1.33       cgd int
   1756    1.1       cgd cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
   1757    1.1       cgd 	int *name;
   1758    1.1       cgd 	u_int namelen;
   1759    1.1       cgd 	void *oldp;
   1760    1.1       cgd 	size_t *oldlenp;
   1761    1.1       cgd 	void *newp;
   1762    1.1       cgd 	size_t newlen;
   1763    1.1       cgd 	struct proc *p;
   1764    1.1       cgd {
   1765    1.1       cgd 	dev_t consdev;
   1766    1.1       cgd 
   1767    1.1       cgd 	/* all sysctl names at this level are terminal */
   1768    1.1       cgd 	if (namelen != 1)
   1769    1.1       cgd 		return (ENOTDIR);		/* overloaded */
   1770    1.1       cgd 
   1771    1.1       cgd 	switch (name[0]) {
   1772    1.1       cgd 	case CPU_CONSDEV:
   1773    1.1       cgd 		if (cn_tab != NULL)
   1774    1.1       cgd 			consdev = cn_tab->cn_dev;
   1775    1.1       cgd 		else
   1776    1.1       cgd 			consdev = NODEV;
   1777    1.1       cgd 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
   1778    1.1       cgd 			sizeof consdev));
   1779   1.30       cgd 
   1780   1.30       cgd 	case CPU_ROOT_DEVICE:
   1781   1.64   thorpej 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1782   1.64   thorpej 		    root_device->dv_xname));
   1783   1.36       cgd 
   1784   1.36       cgd 	case CPU_UNALIGNED_PRINT:
   1785   1.36       cgd 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1786   1.36       cgd 		    &alpha_unaligned_print));
   1787   1.36       cgd 
   1788   1.36       cgd 	case CPU_UNALIGNED_FIX:
   1789   1.36       cgd 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1790   1.36       cgd 		    &alpha_unaligned_fix));
   1791   1.36       cgd 
   1792   1.36       cgd 	case CPU_UNALIGNED_SIGBUS:
   1793   1.36       cgd 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1794   1.36       cgd 		    &alpha_unaligned_sigbus));
   1795   1.61       cgd 
   1796   1.61       cgd 	case CPU_BOOTED_KERNEL:
   1797  1.102       cgd 		return (sysctl_rdstring(oldp, oldlenp, newp,
   1798  1.102       cgd 		    bootinfo.booted_kernel));
   1799   1.30       cgd 
   1800  1.241      ross 	case CPU_FP_SYNC_COMPLETE:
   1801  1.241      ross 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1802  1.241      ross 		    &alpha_fp_sync_complete));
   1803  1.241      ross 
   1804    1.1       cgd 	default:
   1805    1.1       cgd 		return (EOPNOTSUPP);
   1806    1.1       cgd 	}
   1807    1.1       cgd 	/* NOTREACHED */
   1808    1.1       cgd }
   1809    1.1       cgd 
   1810    1.1       cgd /*
   1811    1.1       cgd  * Set registers on exec.
   1812    1.1       cgd  */
   1813    1.1       cgd void
   1814  1.261   thorpej setregs(l, pack, stack)
   1815  1.261   thorpej 	register struct lwp *l;
   1816    1.5  christos 	struct exec_package *pack;
   1817    1.1       cgd 	u_long stack;
   1818    1.1       cgd {
   1819  1.261   thorpej 	struct trapframe *tfp = l->l_md.md_tf;
   1820   1.56       cgd #ifdef DEBUG
   1821    1.1       cgd 	int i;
   1822   1.56       cgd #endif
   1823   1.43       cgd 
   1824   1.43       cgd #ifdef DEBUG
   1825   1.43       cgd 	/*
   1826   1.43       cgd 	 * Crash and dump, if the user requested it.
   1827   1.43       cgd 	 */
   1828   1.43       cgd 	if (boothowto & RB_DUMP)
   1829   1.43       cgd 		panic("crash requested by boot flags");
   1830   1.43       cgd #endif
   1831    1.1       cgd 
   1832    1.1       cgd #ifdef DEBUG
   1833   1.34       cgd 	for (i = 0; i < FRAME_SIZE; i++)
   1834    1.1       cgd 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
   1835    1.1       cgd #else
   1836  1.246   thorpej 	memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
   1837    1.1       cgd #endif
   1838  1.261   thorpej 	memset(&l->l_addr->u_pcb.pcb_fp, 0, sizeof l->l_addr->u_pcb.pcb_fp);
   1839   1.35       cgd 	alpha_pal_wrusp(stack);
   1840   1.34       cgd 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
   1841   1.34       cgd 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
   1842   1.41       cgd 
   1843   1.62       cgd 	tfp->tf_regs[FRAME_A0] = stack;			/* a0 = sp */
   1844   1.62       cgd 	tfp->tf_regs[FRAME_A1] = 0;			/* a1 = rtld cleanup */
   1845   1.62       cgd 	tfp->tf_regs[FRAME_A2] = 0;			/* a2 = rtld object */
   1846  1.261   thorpej 	tfp->tf_regs[FRAME_A3] = (u_int64_t)l->l_proc->p_psstr;	/* a3 = ps_strings */
   1847   1.41       cgd 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
   1848    1.1       cgd 
   1849  1.261   thorpej 	l->l_md.md_flags &= ~MDP_FPUSED;
   1850  1.261   thorpej 	if (__predict_true((l->l_md.md_flags & IEEE_INHERIT) == 0)) {
   1851  1.261   thorpej 		l->l_md.md_flags &= ~MDP_FP_C;
   1852  1.261   thorpej 		l->l_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
   1853  1.241      ross 	}
   1854  1.261   thorpej 	if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   1855  1.261   thorpej 		fpusave_proc(l, 0);
   1856  1.219   thorpej }
   1857  1.219   thorpej 
   1858  1.219   thorpej /*
   1859  1.219   thorpej  * Release the FPU.
   1860  1.219   thorpej  */
   1861  1.219   thorpej void
   1862  1.225   thorpej fpusave_cpu(struct cpu_info *ci, int save)
   1863  1.219   thorpej {
   1864  1.261   thorpej 	struct lwp *l;
   1865  1.225   thorpej #if defined(MULTIPROCESSOR)
   1866  1.219   thorpej 	int s;
   1867  1.225   thorpej #endif
   1868  1.219   thorpej 
   1869  1.225   thorpej 	KDASSERT(ci == curcpu());
   1870  1.225   thorpej 
   1871  1.235   thorpej #if defined(MULTIPROCESSOR)
   1872  1.235   thorpej 	atomic_setbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
   1873  1.235   thorpej #endif
   1874  1.235   thorpej 
   1875  1.261   thorpej 	l = ci->ci_fpcurlwp;
   1876  1.261   thorpej 	if (l == NULL)
   1877  1.235   thorpej 		goto out;
   1878  1.219   thorpej 
   1879  1.219   thorpej 	if (save) {
   1880  1.219   thorpej 		alpha_pal_wrfen(1);
   1881  1.261   thorpej 		savefpstate(&l->l_addr->u_pcb.pcb_fp);
   1882  1.225   thorpej 	}
   1883  1.225   thorpej 
   1884  1.225   thorpej 	alpha_pal_wrfen(0);
   1885  1.225   thorpej 
   1886  1.261   thorpej 	FPCPU_LOCK(&l->l_addr->u_pcb, s);
   1887  1.235   thorpej 
   1888  1.261   thorpej 	l->l_addr->u_pcb.pcb_fpcpu = NULL;
   1889  1.261   thorpej 	ci->ci_fpcurlwp = NULL;
   1890  1.235   thorpej 
   1891  1.261   thorpej 	FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
   1892  1.235   thorpej 
   1893  1.235   thorpej  out:
   1894  1.219   thorpej #if defined(MULTIPROCESSOR)
   1895  1.235   thorpej 	atomic_clearbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
   1896  1.219   thorpej #endif
   1897  1.235   thorpej 	return;
   1898  1.219   thorpej }
   1899  1.219   thorpej 
   1900  1.219   thorpej /*
   1901  1.219   thorpej  * Synchronize FP state for this process.
   1902  1.219   thorpej  */
   1903  1.219   thorpej void
   1904  1.261   thorpej fpusave_proc(struct lwp *l, int save)
   1905  1.219   thorpej {
   1906  1.225   thorpej 	struct cpu_info *ci = curcpu();
   1907  1.225   thorpej 	struct cpu_info *oci;
   1908  1.235   thorpej #if defined(MULTIPROCESSOR)
   1909  1.235   thorpej 	u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
   1910  1.236   thorpej 	int s, spincount;
   1911  1.235   thorpej #endif
   1912  1.219   thorpej 
   1913  1.261   thorpej 	KDASSERT(l->l_addr != NULL);
   1914  1.261   thorpej 	KDASSERT(l->l_flag & L_INMEM);
   1915  1.225   thorpej 
   1916  1.261   thorpej 	FPCPU_LOCK(&l->l_addr->u_pcb, s);
   1917  1.235   thorpej 
   1918  1.261   thorpej 	oci = l->l_addr->u_pcb.pcb_fpcpu;
   1919  1.235   thorpej 	if (oci == NULL) {
   1920  1.261   thorpej 		FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
   1921  1.219   thorpej 		return;
   1922  1.235   thorpej 	}
   1923  1.219   thorpej 
   1924  1.219   thorpej #if defined(MULTIPROCESSOR)
   1925  1.225   thorpej 	if (oci == ci) {
   1926  1.261   thorpej 		KASSERT(ci->ci_fpcurlwp == l);
   1927  1.261   thorpej 		FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
   1928  1.225   thorpej 		fpusave_cpu(ci, save);
   1929  1.235   thorpej 		return;
   1930  1.235   thorpej 	}
   1931  1.235   thorpej 
   1932  1.261   thorpej 	KASSERT(oci->ci_fpcurlwp == l);
   1933  1.235   thorpej 	alpha_send_ipi(oci->ci_cpuid, ipi);
   1934  1.261   thorpej 	FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
   1935  1.235   thorpej 
   1936  1.235   thorpej 	spincount = 0;
   1937  1.261   thorpej 	while (l->l_addr->u_pcb.pcb_fpcpu != NULL) {
   1938  1.235   thorpej 		spincount++;
   1939  1.235   thorpej 		delay(1000);	/* XXX */
   1940  1.235   thorpej 		if (spincount > 10000)
   1941  1.235   thorpej 			panic("fpsave ipi didn't");
   1942  1.219   thorpej 	}
   1943  1.219   thorpej #else
   1944  1.261   thorpej 	KASSERT(ci->ci_fpcurlwp == l);
   1945  1.261   thorpej 	FPCPU_UNLOCK(&l->l_addr->u_pcb, s);
   1946  1.225   thorpej 	fpusave_cpu(ci, save);
   1947  1.219   thorpej #endif /* MULTIPROCESSOR */
   1948   1.15       cgd }
   1949   1.15       cgd 
   1950   1.15       cgd /*
   1951   1.15       cgd  * Wait "n" microseconds.
   1952   1.15       cgd  */
   1953   1.32       cgd void
   1954   1.15       cgd delay(n)
   1955   1.32       cgd 	unsigned long n;
   1956   1.15       cgd {
   1957  1.216   thorpej 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
   1958   1.15       cgd 
   1959  1.216   thorpej 	if (n == 0)
   1960  1.216   thorpej 		return;
   1961  1.216   thorpej 
   1962  1.216   thorpej 	pcc0 = alpha_rpcc() & 0xffffffffUL;
   1963  1.216   thorpej 	cycles = 0;
   1964  1.216   thorpej 	usec = 0;
   1965  1.216   thorpej 
   1966  1.216   thorpej 	while (usec <= n) {
   1967  1.216   thorpej 		/*
   1968  1.216   thorpej 		 * Get the next CPU cycle count- assumes that we cannot
   1969  1.216   thorpej 		 * have had more than one 32 bit overflow.
   1970  1.216   thorpej 		 */
   1971  1.216   thorpej 		pcc1 = alpha_rpcc() & 0xffffffffUL;
   1972  1.216   thorpej 		if (pcc1 < pcc0)
   1973  1.216   thorpej 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
   1974  1.216   thorpej 		else
   1975  1.216   thorpej 			curcycle = pcc1 - pcc0;
   1976  1.186   thorpej 
   1977  1.216   thorpej 		/*
   1978  1.216   thorpej 		 * We now have the number of processor cycles since we
   1979  1.216   thorpej 		 * last checked. Add the current cycle count to the
   1980  1.216   thorpej 		 * running total. If it's over cycles_per_usec, increment
   1981  1.216   thorpej 		 * the usec counter.
   1982  1.216   thorpej 		 */
   1983  1.216   thorpej 		cycles += curcycle;
   1984  1.216   thorpej 		while (cycles > cycles_per_usec) {
   1985  1.216   thorpej 			usec++;
   1986  1.216   thorpej 			cycles -= cycles_per_usec;
   1987  1.216   thorpej 		}
   1988  1.216   thorpej 		pcc0 = pcc1;
   1989  1.216   thorpej 	}
   1990    1.1       cgd }
   1991  1.225   thorpej 
   1992  1.250  jdolecek #ifdef EXEC_ECOFF
   1993    1.1       cgd void
   1994  1.261   thorpej cpu_exec_ecoff_setregs(l, epp, stack)
   1995  1.261   thorpej 	struct lwp *l;
   1996   1.19       cgd 	struct exec_package *epp;
   1997    1.5  christos 	u_long stack;
   1998    1.1       cgd {
   1999   1.19       cgd 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   2000    1.1       cgd 
   2001  1.261   thorpej 	l->l_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
   2002    1.1       cgd }
   2003    1.1       cgd 
   2004    1.1       cgd /*
   2005    1.1       cgd  * cpu_exec_ecoff_hook():
   2006    1.1       cgd  *	cpu-dependent ECOFF format hook for execve().
   2007    1.1       cgd  *
   2008    1.1       cgd  * Do any machine-dependent diddling of the exec package when doing ECOFF.
   2009    1.1       cgd  *
   2010    1.1       cgd  */
   2011    1.1       cgd int
   2012  1.224  jdolecek cpu_exec_ecoff_probe(p, epp)
   2013    1.1       cgd 	struct proc *p;
   2014    1.1       cgd 	struct exec_package *epp;
   2015    1.1       cgd {
   2016   1.19       cgd 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
   2017  1.171       cgd 	int error;
   2018    1.1       cgd 
   2019  1.224  jdolecek 	if (execp->f.f_magic == ECOFF_MAGIC_NETBSD_ALPHA)
   2020  1.171       cgd 		error = 0;
   2021  1.224  jdolecek 	else
   2022  1.224  jdolecek 		error = ENOEXEC;
   2023    1.1       cgd 
   2024  1.171       cgd 	return (error);
   2025    1.1       cgd }
   2026  1.250  jdolecek #endif /* EXEC_ECOFF */
   2027  1.110   thorpej 
   2028  1.110   thorpej int
   2029  1.110   thorpej alpha_pa_access(pa)
   2030  1.110   thorpej 	u_long pa;
   2031  1.110   thorpej {
   2032  1.110   thorpej 	int i;
   2033  1.110   thorpej 
   2034  1.110   thorpej 	for (i = 0; i < mem_cluster_cnt; i++) {
   2035  1.110   thorpej 		if (pa < mem_clusters[i].start)
   2036  1.110   thorpej 			continue;
   2037  1.110   thorpej 		if ((pa - mem_clusters[i].start) >=
   2038  1.110   thorpej 		    (mem_clusters[i].size & ~PAGE_MASK))
   2039  1.110   thorpej 			continue;
   2040  1.110   thorpej 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
   2041  1.110   thorpej 	}
   2042  1.197   thorpej 
   2043  1.197   thorpej 	/*
   2044  1.197   thorpej 	 * Address is not a memory address.  If we're secure, disallow
   2045  1.197   thorpej 	 * access.  Otherwise, grant read/write.
   2046  1.197   thorpej 	 */
   2047  1.197   thorpej 	if (securelevel > 0)
   2048  1.197   thorpej 		return (PROT_NONE);
   2049  1.197   thorpej 	else
   2050  1.197   thorpej 		return (PROT_READ | PROT_WRITE);
   2051  1.110   thorpej }
   2052   1.50       cgd 
   2053   1.50       cgd /* XXX XXX BEGIN XXX XXX */
   2054  1.140   thorpej paddr_t alpha_XXX_dmamap_or;					/* XXX */
   2055   1.50       cgd 								/* XXX */
   2056  1.140   thorpej paddr_t								/* XXX */
   2057   1.50       cgd alpha_XXX_dmamap(v)						/* XXX */
   2058  1.140   thorpej 	vaddr_t v;						/* XXX */
   2059   1.50       cgd {								/* XXX */
   2060   1.50       cgd 								/* XXX */
   2061   1.51       cgd 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
   2062   1.50       cgd }								/* XXX */
   2063   1.50       cgd /* XXX XXX END XXX XXX */
   2064  1.177      ross 
   2065  1.177      ross char *
   2066  1.177      ross dot_conv(x)
   2067  1.177      ross 	unsigned long x;
   2068  1.177      ross {
   2069  1.177      ross 	int i;
   2070  1.177      ross 	char *xc;
   2071  1.177      ross 	static int next;
   2072  1.177      ross 	static char space[2][20];
   2073  1.177      ross 
   2074  1.177      ross 	xc = space[next ^= 1] + sizeof space[0];
   2075  1.177      ross 	*--xc = '\0';
   2076  1.177      ross 	for (i = 0;; ++i) {
   2077  1.177      ross 		if (i && (i & 3) == 0)
   2078  1.177      ross 			*--xc = '.';
   2079  1.177      ross 		*--xc = "0123456789abcdef"[x & 0xf];
   2080  1.177      ross 		x >>= 4;
   2081  1.177      ross 		if (x == 0)
   2082  1.177      ross 			break;
   2083  1.177      ross 	}
   2084  1.177      ross 	return xc;
   2085  1.261   thorpej }
   2086  1.261   thorpej 
   2087  1.261   thorpej void
   2088  1.261   thorpej cpu_getmcontext(l, mcp, flags)
   2089  1.261   thorpej 	struct lwp *l;
   2090  1.261   thorpej 	mcontext_t *mcp;
   2091  1.261   thorpej 	unsigned int *flags;
   2092  1.261   thorpej {
   2093  1.261   thorpej 	struct trapframe *frame = l->l_md.md_tf;
   2094  1.261   thorpej 	__greg_t *gr = mcp->__gregs;
   2095  1.264   nathanw 	__greg_t ras_pc;
   2096  1.261   thorpej 
   2097  1.261   thorpej 	/* Save register context. */
   2098  1.261   thorpej 	frametoreg(frame, (struct reg *)gr);
   2099  1.261   thorpej 	/* XXX if there's a better, general way to get the USP of
   2100  1.261   thorpej 	 * an LWP that might or might not be curlwp, I'd like to know
   2101  1.261   thorpej 	 * about it.
   2102  1.261   thorpej 	 */
   2103  1.261   thorpej 	if (l == curlwp) {
   2104  1.261   thorpej 		gr[_REG_SP] = alpha_pal_rdusp();
   2105  1.261   thorpej 		gr[_REG_UNIQUE] = alpha_pal_rdunique();
   2106  1.261   thorpej 	} else {
   2107  1.261   thorpej 		gr[_REG_SP] = l->l_addr->u_pcb.pcb_hw.apcb_usp;
   2108  1.261   thorpej 		gr[_REG_UNIQUE] = l->l_addr->u_pcb.pcb_hw.apcb_unique;
   2109  1.261   thorpej 	}
   2110  1.261   thorpej 	gr[_REG_PC] = frame->tf_regs[FRAME_PC];
   2111  1.261   thorpej 	gr[_REG_PS] = frame->tf_regs[FRAME_PS];
   2112  1.264   nathanw 
   2113  1.264   nathanw 	if ((ras_pc = (__greg_t)ras_lookup(l->l_proc,
   2114  1.264   nathanw 	    (caddr_t) gr[_REG_PC])) != -1)
   2115  1.264   nathanw 		gr[_REG_PC] = ras_pc;
   2116  1.264   nathanw 
   2117  1.261   thorpej 	*flags |= _UC_CPU | _UC_UNIQUE;
   2118  1.261   thorpej 
   2119  1.261   thorpej 	/* Save floating point register context, if any, and copy it. */
   2120  1.265   nathanw 	if (l->l_md.md_flags & MDP_FPUSED) {
   2121  1.261   thorpej 		fpusave_proc(l, 1);
   2122  1.261   thorpej 		(void)memcpy(&mcp->__fpregs, &l->l_addr->u_pcb.pcb_fp,
   2123  1.261   thorpej 		    sizeof (mcp->__fpregs));
   2124  1.261   thorpej 		mcp->__fpregs.__fp_fpcr = alpha_read_fp_c(l);
   2125  1.261   thorpej 		*flags |= _UC_FPU;
   2126  1.261   thorpej 	}
   2127  1.261   thorpej }
   2128  1.261   thorpej 
   2129  1.261   thorpej 
   2130  1.261   thorpej int
   2131  1.261   thorpej cpu_setmcontext(l, mcp, flags)
   2132  1.261   thorpej 	struct lwp *l;
   2133  1.261   thorpej 	const mcontext_t *mcp;
   2134  1.261   thorpej 	unsigned int flags;
   2135  1.261   thorpej {
   2136  1.261   thorpej 	struct trapframe *frame = l->l_md.md_tf;
   2137  1.261   thorpej 	const __greg_t *gr = mcp->__gregs;
   2138  1.261   thorpej 
   2139  1.261   thorpej 	/* Restore register context, if any. */
   2140  1.261   thorpej 	if (flags & _UC_CPU) {
   2141  1.261   thorpej 		/* Check for security violations first. */
   2142  1.261   thorpej 		if ((gr[_REG_PS] & ALPHA_PSL_USERSET) != ALPHA_PSL_USERSET ||
   2143  1.261   thorpej 		    (gr[_REG_PS] & ALPHA_PSL_USERCLR) != 0)
   2144  1.261   thorpej 			return (EINVAL);
   2145  1.261   thorpej 
   2146  1.261   thorpej 		regtoframe((struct reg *)gr, l->l_md.md_tf);
   2147  1.261   thorpej 		if (l == curlwp)
   2148  1.261   thorpej 			alpha_pal_wrusp(gr[_REG_SP]);
   2149  1.261   thorpej 		else
   2150  1.261   thorpej 			l->l_addr->u_pcb.pcb_hw.apcb_usp = gr[_REG_SP];
   2151  1.261   thorpej 		frame->tf_regs[FRAME_PC] = gr[_REG_PC];
   2152  1.261   thorpej 		frame->tf_regs[FRAME_PS] = gr[_REG_PS];
   2153  1.261   thorpej 	}
   2154  1.261   thorpej 	if (flags & _UC_UNIQUE) {
   2155  1.261   thorpej 		if (l == curlwp)
   2156  1.261   thorpej 			alpha_pal_wrunique(gr[_REG_UNIQUE]);
   2157  1.261   thorpej 		else
   2158  1.261   thorpej 			l->l_addr->u_pcb.pcb_hw.apcb_unique = gr[_REG_UNIQUE];
   2159  1.261   thorpej 	}
   2160  1.261   thorpej 	/* Restore floating point register context, if any. */
   2161  1.261   thorpej 	if (flags & _UC_FPU) {
   2162  1.261   thorpej 		/* If we have an FP register context, get rid of it. */
   2163  1.261   thorpej 		if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
   2164  1.261   thorpej 			fpusave_proc(l, 0);
   2165  1.261   thorpej 		(void)memcpy(&l->l_addr->u_pcb.pcb_fp, &mcp->__fpregs,
   2166  1.261   thorpej 		    sizeof (l->l_addr->u_pcb.pcb_fp));
   2167  1.261   thorpej 		l->l_md.md_flags = mcp->__fpregs.__fp_fpcr & MDP_FP_C;
   2168  1.261   thorpej 	}
   2169  1.261   thorpej 
   2170  1.261   thorpej 	return (0);
   2171  1.138      ross }
   2172