xref: /openbsd/sys/arch/alpha/alpha/machdep.c (revision b3cee53e)
1*b3cee53eSmartin /* $OpenBSD: machdep.c,v 1.106 2007/04/18 16:53:19 martin Exp $ */
22a2685f2Sart /* $NetBSD: machdep.c,v 1.210 2000/06/01 17:12:38 thorpej Exp $ */
3aed035abSart 
4aed035abSart /*-
5aed035abSart  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6aed035abSart  * All rights reserved.
7aed035abSart  *
8aed035abSart  * This code is derived from software contributed to The NetBSD Foundation
9aed035abSart  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10aed035abSart  * NASA Ames Research Center and by Chris G. Demetriou.
11aed035abSart  *
12aed035abSart  * Redistribution and use in source and binary forms, with or without
13aed035abSart  * modification, are permitted provided that the following conditions
14aed035abSart  * are met:
15aed035abSart  * 1. Redistributions of source code must retain the above copyright
16aed035abSart  *    notice, this list of conditions and the following disclaimer.
17aed035abSart  * 2. Redistributions in binary form must reproduce the above copyright
18aed035abSart  *    notice, this list of conditions and the following disclaimer in the
19aed035abSart  *    documentation and/or other materials provided with the distribution.
20aed035abSart  * 3. All advertising materials mentioning features or use of this software
21aed035abSart  *    must display the following acknowledgement:
22aed035abSart  *	This product includes software developed by the NetBSD
23aed035abSart  *	Foundation, Inc. and its contributors.
24aed035abSart  * 4. Neither the name of The NetBSD Foundation nor the names of its
25aed035abSart  *    contributors may be used to endorse or promote products derived
26aed035abSart  *    from this software without specific prior written permission.
27aed035abSart  *
28aed035abSart  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29aed035abSart  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30aed035abSart  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31aed035abSart  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32aed035abSart  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33aed035abSart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34aed035abSart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35aed035abSart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36aed035abSart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37aed035abSart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38aed035abSart  * POSSIBILITY OF SUCH DAMAGE.
39aed035abSart  */
40df930be7Sderaadt 
41df930be7Sderaadt /*
42417eba8cSderaadt  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
43df930be7Sderaadt  * All rights reserved.
44df930be7Sderaadt  *
45df930be7Sderaadt  * Author: Chris G. Demetriou
46df930be7Sderaadt  *
47df930be7Sderaadt  * Permission to use, copy, modify and distribute this software and
48df930be7Sderaadt  * its documentation is hereby granted, provided that both the copyright
49df930be7Sderaadt  * notice and this permission notice appear in all copies of the
50df930be7Sderaadt  * software, derivative works or modified versions, and any portions
51df930be7Sderaadt  * thereof, and that both notices appear in supporting documentation.
52df930be7Sderaadt  *
53df930be7Sderaadt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
54df930be7Sderaadt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
55df930be7Sderaadt  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
56df930be7Sderaadt  *
57df930be7Sderaadt  * Carnegie Mellon requests users of this software to return to
58df930be7Sderaadt  *
59df930be7Sderaadt  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
60df930be7Sderaadt  *  School of Computer Science
61df930be7Sderaadt  *  Carnegie Mellon University
62df930be7Sderaadt  *  Pittsburgh PA 15213-3890
63df930be7Sderaadt  *
64df930be7Sderaadt  * any improvements or extensions that they make and grant Carnegie the
65df930be7Sderaadt  * rights to redistribute these changes.
66df930be7Sderaadt  */
67df930be7Sderaadt 
68df930be7Sderaadt #include <sys/param.h>
69df930be7Sderaadt #include <sys/systm.h>
70df930be7Sderaadt #include <sys/signalvar.h>
71df930be7Sderaadt #include <sys/kernel.h>
72df930be7Sderaadt #include <sys/proc.h>
732a2685f2Sart #include <sys/sched.h>
74df930be7Sderaadt #include <sys/buf.h>
75df930be7Sderaadt #include <sys/reboot.h>
76417eba8cSderaadt #include <sys/device.h>
77df930be7Sderaadt #include <sys/conf.h>
78df930be7Sderaadt #include <sys/file.h>
79d66eba84Sart #include <sys/timeout.h>
80df930be7Sderaadt #include <sys/malloc.h>
81df930be7Sderaadt #include <sys/mbuf.h>
82df930be7Sderaadt #include <sys/msgbuf.h>
83df930be7Sderaadt #include <sys/ioctl.h>
84df930be7Sderaadt #include <sys/tty.h>
85df930be7Sderaadt #include <sys/user.h>
86df930be7Sderaadt #include <sys/exec.h>
87df930be7Sderaadt #include <sys/exec_ecoff.h>
88489e49f9Smiod #include <uvm/uvm_extern.h>
89df930be7Sderaadt #include <sys/sysctl.h>
9050ce9ee0Sniklas #include <sys/core.h>
9150ce9ee0Sniklas #include <sys/kcore.h>
9250ce9ee0Sniklas #include <machine/kcore.h>
93433075b6Spvalchev #ifndef NO_IEEE
94433075b6Spvalchev #include <machine/fpu.h>
95433075b6Spvalchev #endif
96df930be7Sderaadt #ifdef SYSVMSG
97df930be7Sderaadt #include <sys/msg.h>
98df930be7Sderaadt #endif
99df930be7Sderaadt 
100df930be7Sderaadt #include <sys/mount.h>
101df930be7Sderaadt #include <sys/syscallargs.h>
102df930be7Sderaadt 
103df930be7Sderaadt #include <dev/cons.h>
104df930be7Sderaadt 
10550ce9ee0Sniklas #include <machine/autoconf.h>
106df930be7Sderaadt #include <machine/cpu.h>
107df930be7Sderaadt #include <machine/reg.h>
108df930be7Sderaadt #include <machine/rpb.h>
109df930be7Sderaadt #include <machine/prom.h>
1103a630e3fSniklas #include <machine/cpuconf.h>
111433075b6Spvalchev #ifndef NO_IEEE
112433075b6Spvalchev #include <machine/ieeefp.h>
113433075b6Spvalchev #endif
114df930be7Sderaadt 
11545e5a1a0Sart #include <dev/pci/pcivar.h>
11645e5a1a0Sart 
11712f8bbedSniklas #ifdef DDB
11812f8bbedSniklas #include <machine/db_machdep.h>
11912f8bbedSniklas #include <ddb/db_access.h>
12012f8bbedSniklas #include <ddb/db_sym.h>
12112f8bbedSniklas #include <ddb/db_extern.h>
12212f8bbedSniklas #endif
12312f8bbedSniklas 
124c4071fd1Smillert int	cpu_dump(void);
125c4071fd1Smillert int	cpu_dumpsize(void);
126c4071fd1Smillert u_long	cpu_dump_mempagecnt(void);
127c4071fd1Smillert void	dumpsys(void);
128c4071fd1Smillert caddr_t allocsys(caddr_t);
129c4071fd1Smillert void	identifycpu(void);
130c4071fd1Smillert void	regdump(struct trapframe *framep);
131c4071fd1Smillert void	printregs(struct reg *);
132df930be7Sderaadt 
133df930be7Sderaadt /*
134df930be7Sderaadt  * Declare these as initialized data so we can patch them.
135df930be7Sderaadt  */
136df930be7Sderaadt #ifdef	NBUF
137df930be7Sderaadt int	nbuf = NBUF;
138df930be7Sderaadt #else
139df930be7Sderaadt int	nbuf = 0;
140df930be7Sderaadt #endif
14160535ec9Smaja 
14260535ec9Smaja #ifndef BUFCACHEPERCENT
14360535ec9Smaja #define BUFCACHEPERCENT 10
14460535ec9Smaja #endif
14560535ec9Smaja 
146df930be7Sderaadt #ifdef	BUFPAGES
147df930be7Sderaadt int	bufpages = BUFPAGES;
148df930be7Sderaadt #else
149df930be7Sderaadt int	bufpages = 0;
150df930be7Sderaadt #endif
15160535ec9Smaja int	bufcachepercent = BUFCACHEPERCENT;
152aed035abSart 
153ab8e80c5Sart struct vm_map *exec_map = NULL;
154ab8e80c5Sart struct vm_map *phys_map = NULL;
155aed035abSart 
15627626149Smatthieu #ifdef APERTURE
15727626149Smatthieu #ifdef INSECURE
15827626149Smatthieu int allowaperture = 1;
15927626149Smatthieu #else
16027626149Smatthieu int allowaperture = 0;
16127626149Smatthieu #endif
16227626149Smatthieu #endif
16327626149Smatthieu 
164df930be7Sderaadt int	totalphysmem;		/* total amount of physical memory in system */
16574652a67Sniklas int	physmem;		/* physical mem used by OpenBSD + some rsvd */
166df930be7Sderaadt int	resvmem;		/* amount of memory reserved for PROM */
167df930be7Sderaadt int	unusedmem;		/* amount of memory for OS that we don't use */
168df930be7Sderaadt int	unknownmem;		/* amount of memory with an unknown use */
169df930be7Sderaadt 
170df930be7Sderaadt int	cputype;		/* system type, from the RPB */
1712586fa93Smiod int	alpha_cpus;
172df930be7Sderaadt 
1732a2685f2Sart int	bootdev_debug = 0;	/* patchable, or from DDB */
1742a2685f2Sart 
175df930be7Sderaadt /*
176df930be7Sderaadt  * XXX We need an address to which we can assign things so that they
177df930be7Sderaadt  * won't be optimized away because we didn't use the value.
178df930be7Sderaadt  */
179df930be7Sderaadt u_int32_t no_optimize;
180df930be7Sderaadt 
181df930be7Sderaadt /* the following is used externally (sysctl_hw) */
182aed035abSart char	machine[] = MACHINE;		/* from <machine/param.h> */
183417eba8cSderaadt char	cpu_model[128];
184aed035abSart char	root_device[17];
185df930be7Sderaadt 
186df930be7Sderaadt struct	user *proc0paddr;
187df930be7Sderaadt 
188df930be7Sderaadt /* Number of machine cycles per microsecond */
189df930be7Sderaadt u_int64_t	cycles_per_usec;
190df930be7Sderaadt 
191aed035abSart struct bootinfo_kernel bootinfo;
192aed035abSart 
193aed035abSart /* For built-in TCDS */
194aed035abSart #if defined(DEC_3000_300) || defined(DEC_3000_500)
195aed035abSart u_int8_t	dec_3000_scsiid[2], dec_3000_scsifast[2];
196aed035abSart #endif
197aed035abSart 
198aed035abSart struct platform platform;
199417eba8cSderaadt 
200417eba8cSderaadt /* for cpu_sysctl() */
20150ce9ee0Sniklas int	alpha_unaligned_print = 1;	/* warn about unaligned accesses */
20250ce9ee0Sniklas int	alpha_unaligned_fix = 1;	/* fix up unaligned accesses */
203881c1eabSart int	alpha_unaligned_sigbus = 1;	/* SIGBUS on fixed-up accesses */
204433075b6Spvalchev #ifndef NO_IEEE
205433075b6Spvalchev int	alpha_fp_sync_complete = 0;	/* fp fixup if sync even without /s */
206433075b6Spvalchev #endif
20750ce9ee0Sniklas 
208*b3cee53eSmartin /* used by hw_sysctl */
209*b3cee53eSmartin extern char *hw_serial;
210*b3cee53eSmartin 
211aed035abSart /*
212aed035abSart  * XXX This should be dynamically sized, but we have the chicken-egg problem!
213aed035abSart  * XXX it should also be larger than it is, because not all of the mddt
214aed035abSart  * XXX clusters end up being used for VM.
215aed035abSart  */
216aed035abSart phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];	/* low size bits overloaded */
217aed035abSart int	mem_cluster_cnt;
218aed035abSart 
2193a630e3fSniklas void
220aed035abSart alpha_init(pfn, ptb, bim, bip, biv)
221df930be7Sderaadt 	u_long pfn;		/* first free PFN number */
222df930be7Sderaadt 	u_long ptb;		/* PFN of current level 1 page table */
223aed035abSart 	u_long bim;		/* bootinfo magic */
224aed035abSart 	u_long bip;		/* bootinfo pointer */
225aed035abSart 	u_long biv;		/* bootinfo version */
226df930be7Sderaadt {
227aed035abSart 	extern char kernel_text[], _end[];
228df930be7Sderaadt 	struct mddt *mddtp;
229aed035abSart 	struct mddt_cluster *memc;
230df930be7Sderaadt 	int i, mddtweird;
231aed035abSart 	struct vm_physseg *vps;
232aed035abSart 	vaddr_t kernstart, kernend;
233aed035abSart 	paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
234aed035abSart 	vsize_t size;
235df930be7Sderaadt 	char *p;
236aed035abSart 	caddr_t v;
2372a2685f2Sart 	const char *bootinfo_msg;
238aed035abSart 	const struct cpuinit *c;
239aed035abSart 	extern caddr_t esym;
240aed035abSart 	struct cpu_info *ci;
241aed035abSart 	cpuid_t cpu_id;
242df930be7Sderaadt 
243aed035abSart 	/* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
244f3914c62Sniklas 
245df930be7Sderaadt 	/*
246aed035abSart 	 * Turn off interrupts (not mchecks) and floating point.
247df930be7Sderaadt 	 * Make sure the instruction and data streams are consistent.
248df930be7Sderaadt 	 */
249aed035abSart 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
25050ce9ee0Sniklas 	alpha_pal_wrfen(0);
25150ce9ee0Sniklas 	ALPHA_TBIA();
25250ce9ee0Sniklas 	alpha_pal_imb();
253df930be7Sderaadt 
254c62181b1Sbrad 	/* Initialize the SCB. */
255c62181b1Sbrad 	scb_init();
256c62181b1Sbrad 
257aed035abSart 	cpu_id = cpu_number();
258aed035abSart 
259aed035abSart #if defined(MULTIPROCESSOR)
260df930be7Sderaadt 	/*
261aed035abSart 	 * Set our SysValue to the address of our cpu_info structure.
262aed035abSart 	 * Secondary processors do this in their spinup trampoline.
263df930be7Sderaadt 	 */
264aed035abSart 	alpha_pal_wrval((u_long)&cpu_info[cpu_id]);
265aed035abSart #endif
266aed035abSart 
267aed035abSart 	ci = curcpu();
268aed035abSart 	ci->ci_cpuid = cpu_id;
269aed035abSart 
270aed035abSart 	/*
271aed035abSart 	 * Get critical system information (if possible, from the
272aed035abSart 	 * information provided by the boot program).
273aed035abSart 	 */
274aed035abSart 	bootinfo_msg = NULL;
275aed035abSart 	if (bim == BOOTINFO_MAGIC) {
276aed035abSart 		if (biv == 0) {		/* backward compat */
277aed035abSart 			biv = *(u_long *)bip;
278aed035abSart 			bip += 8;
279aed035abSart 		}
280aed035abSart 		switch (biv) {
281aed035abSart 		case 1: {
282aed035abSart 			struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
283aed035abSart 
284aed035abSart 			bootinfo.ssym = v1p->ssym;
285aed035abSart 			bootinfo.esym = v1p->esym;
286aed035abSart 			/* hwrpb may not be provided by boot block in v1 */
287aed035abSart 			if (v1p->hwrpb != NULL) {
288aed035abSart 				bootinfo.hwrpb_phys =
289aed035abSart 				    ((struct rpb *)v1p->hwrpb)->rpb_phys;
290aed035abSart 				bootinfo.hwrpb_size = v1p->hwrpbsize;
291aed035abSart 			} else {
292aed035abSart 				bootinfo.hwrpb_phys =
293aed035abSart 				    ((struct rpb *)HWRPB_ADDR)->rpb_phys;
294aed035abSart 				bootinfo.hwrpb_size =
295aed035abSart 				    ((struct rpb *)HWRPB_ADDR)->rpb_size;
296aed035abSart 			}
297aed035abSart 			bcopy(v1p->boot_flags, bootinfo.boot_flags,
298aed035abSart 			    min(sizeof v1p->boot_flags,
299aed035abSart 			      sizeof bootinfo.boot_flags));
300aed035abSart 			bcopy(v1p->booted_kernel, bootinfo.booted_kernel,
301aed035abSart 			    min(sizeof v1p->booted_kernel,
302aed035abSart 			      sizeof bootinfo.booted_kernel));
303aed035abSart 			/* booted dev not provided in bootinfo */
304aed035abSart 			init_prom_interface((struct rpb *)
305aed035abSart 			    ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
306aed035abSart                 	prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
307aed035abSart 			    sizeof bootinfo.booted_dev);
308aed035abSart 			break;
309aed035abSart 		}
310aed035abSart 		default:
311aed035abSart 			bootinfo_msg = "unknown bootinfo version";
312aed035abSart 			goto nobootinfo;
313aed035abSart 		}
314aed035abSart 	} else {
315aed035abSart 		bootinfo_msg = "boot program did not pass bootinfo";
316aed035abSart nobootinfo:
317aed035abSart 		bootinfo.ssym = (u_long)_end;
318aed035abSart 		bootinfo.esym = (u_long)_end;
319aed035abSart 		bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
320aed035abSart 		bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
321aed035abSart 		init_prom_interface((struct rpb *)HWRPB_ADDR);
322aed035abSart 		prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
323aed035abSart 		    sizeof bootinfo.boot_flags);
324aed035abSart 		prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
325aed035abSart 		    sizeof bootinfo.booted_kernel);
326aed035abSart 		prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
327aed035abSart 		    sizeof bootinfo.booted_dev);
328aed035abSart 	}
329aed035abSart 
330aed035abSart 	esym = (caddr_t)bootinfo.esym;
331aed035abSart 	/*
332aed035abSart 	 * Initialize the kernel's mapping of the RPB.  It's needed for
333aed035abSart 	 * lots of things.
334aed035abSart 	 */
335aed035abSart 	hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
336aed035abSart 
337aed035abSart #if defined(DEC_3000_300) || defined(DEC_3000_500)
338aed035abSart 	if (hwrpb->rpb_type == ST_DEC_3000_300 ||
339aed035abSart 	    hwrpb->rpb_type == ST_DEC_3000_500) {
340aed035abSart 		prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
341aed035abSart 		    sizeof(dec_3000_scsiid));
342aed035abSart 		prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
343aed035abSart 		    sizeof(dec_3000_scsifast));
344aed035abSart 	}
345aed035abSart #endif
346df930be7Sderaadt 
347df930be7Sderaadt 	/*
348df930be7Sderaadt 	 * Remember how many cycles there are per microsecond,
349df930be7Sderaadt 	 * so that we can use delay().  Round up, for safety.
350df930be7Sderaadt 	 */
351df930be7Sderaadt 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
352df930be7Sderaadt 
353df930be7Sderaadt 	/*
3549e71c994Saaron 	 * Initialize the (temporary) bootstrap console interface, so
355aed035abSart 	 * we can use printf until the VM system starts being setup.
356aed035abSart 	 * The real console is initialized before then.
357df930be7Sderaadt 	 */
358aed035abSart 	init_bootstrap_console();
359aed035abSart 
360aed035abSart 	/* OUTPUT NOW ALLOWED */
361aed035abSart 
362aed035abSart 	/* delayed from above */
363aed035abSart 	if (bootinfo_msg)
364aed035abSart 		printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
365aed035abSart 		    bootinfo_msg, bim, bip, biv);
366aed035abSart 
367aed035abSart 	/* Initialize the trap vectors on the primary processor. */
368aed035abSart 	trap_init();
369df930be7Sderaadt 
370df930be7Sderaadt 	/*
371aed035abSart 	 * Find out what hardware we're on, and do basic initialization.
372df930be7Sderaadt 	 */
373aed035abSart 	cputype = hwrpb->rpb_type;
374aed035abSart 	if (cputype < 0) {
375aed035abSart 		/*
376aed035abSart 		 * At least some white-box systems have SRM which
377aed035abSart 		 * reports a systype that's the negative of their
378aed035abSart 		 * blue-box counterpart.
379aed035abSart 		 */
380aed035abSart 		cputype = -cputype;
381aed035abSart 	}
382aed035abSart 	c = platform_lookup(cputype);
383aed035abSart 	if (c == NULL) {
384aed035abSart 		platform_not_supported();
385aed035abSart 		/* NOTREACHED */
386aed035abSart 	}
387aed035abSart 	(*c->init)();
388094fa01fSderaadt 	strlcpy(cpu_model, platform.model, sizeof cpu_model);
38950ce9ee0Sniklas 
39050ce9ee0Sniklas 	/*
3919e71c994Saaron 	 * Initialize the real console, so that the bootstrap console is
392aed035abSart 	 * no longer necessary.
39350ce9ee0Sniklas 	 */
394aed035abSart 	(*platform.cons_init)();
395aed035abSart 
396a55851f4Sderaadt #if 0
397aed035abSart 	/* Paranoid sanity checking */
398aed035abSart 
399aed035abSart 	assert(hwrpb->rpb_primary_cpu_id == alpha_pal_whami());
400aed035abSart 
401aed035abSart 	/*
402aed035abSart 	 * On single-CPU systypes, the primary should always be CPU 0,
403aed035abSart 	 * except on Alpha 8200 systems where the CPU id is related
404aed035abSart 	 * to the VID, which is related to the Turbo Laser node id.
405aed035abSart 	 */
406aed035abSart 	if (cputype != ST_DEC_21000)
407aed035abSart 		assert(hwrpb->rpb_primary_cpu_id == 0);
408aed035abSart #endif
409aed035abSart 
410aed035abSart 	/* NO MORE FIRMWARE ACCESS ALLOWED */
411aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
412aed035abSart 	/*
413aed035abSart 	 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
414aed035abSart 	 * XXX pmap_uses_prom_console() evaluates to non-zero.)
415aed035abSart 	 */
416aed035abSart #endif
417aed035abSart 
418cfcdef40Smiod #ifndef SMALL_KERNEL
419cfcdef40Smiod 	/*
420cfcdef40Smiod 	 * If we run on a BWX-capable processor, override cpu_switch
421cfcdef40Smiod 	 * with a faster version.
422cfcdef40Smiod 	 * We do this now because the kernel text might be mapped
423cfcdef40Smiod 	 * read-only eventually (although this is not the case at the moment).
424cfcdef40Smiod 	 */
425cfcdef40Smiod 	if (alpha_implver() >= ALPHA_IMPLVER_EV5) {
426cfcdef40Smiod 		if (~alpha_amask(ALPHA_AMASK_BWX) != 0) {
427cfcdef40Smiod 			extern vaddr_t __bwx_switch0, __bwx_switch1,
428cfcdef40Smiod 			    __bwx_switch2, __bwx_switch3;
429cfcdef40Smiod 			u_int32_t *dst, *src, *end;
430cfcdef40Smiod 
431cfcdef40Smiod 			src = (u_int32_t *)&__bwx_switch2;
432cfcdef40Smiod 			end = (u_int32_t *)&__bwx_switch3;
433cfcdef40Smiod 			dst = (u_int32_t *)&__bwx_switch0;
434cfcdef40Smiod 			while (src != end)
435cfcdef40Smiod 				*dst++ = *src++;
436cfcdef40Smiod 			src = (u_int32_t *)&__bwx_switch1;
437cfcdef40Smiod 			end = (u_int32_t *)&__bwx_switch2;
438cfcdef40Smiod 			while (src != end)
439cfcdef40Smiod 				*dst++ = *src++;
440cfcdef40Smiod 		}
441cfcdef40Smiod 	}
442cfcdef40Smiod #endif
443cfcdef40Smiod 
444aed035abSart 	/*
445aed035abSart 	 * find out this system's page size
446aed035abSart 	 */
44773b9fe7cSart 	if ((uvmexp.pagesize = hwrpb->rpb_page_size) != 8192)
44873b9fe7cSart 		panic("page size %d != 8192?!", uvmexp.pagesize);
449aed035abSart 
450aed035abSart 	uvm_setpagesize();
451aed035abSart 
452aed035abSart 	/*
453aed035abSart 	 * Find the beginning and end of the kernel (and leave a
454aed035abSart 	 * bit of space before the beginning for the bootstrap
455aed035abSart 	 * stack).
456aed035abSart 	 */
457aed035abSart 	kernstart = trunc_page((vaddr_t)kernel_text) - 2 * PAGE_SIZE;
458aed035abSart 	kernend = (vaddr_t)round_page((vaddr_t)bootinfo.esym);
459aed035abSart 
460aed035abSart 	kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
461aed035abSart 	kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
462df930be7Sderaadt 
463df930be7Sderaadt 	/*
464df930be7Sderaadt 	 * Find out how much memory is available, by looking at
465df930be7Sderaadt 	 * the memory cluster descriptors.  This also tries to do
466df930be7Sderaadt 	 * its best to detect things things that have never been seen
467df930be7Sderaadt 	 * before...
468df930be7Sderaadt 	 */
469df930be7Sderaadt 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
470df930be7Sderaadt 
471aed035abSart 	/* MDDT SANITY CHECKING */
472df930be7Sderaadt 	mddtweird = 0;
473aed035abSart 	if (mddtp->mddt_cluster_cnt < 2) {
474df930be7Sderaadt 		mddtweird = 1;
475aed035abSart 		printf("WARNING: weird number of mem clusters: %lu\n",
476aed035abSart 		    mddtp->mddt_cluster_cnt);
477df930be7Sderaadt 	}
478df930be7Sderaadt 
479aed035abSart #if 0
480aed035abSart 	printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
481aed035abSart #endif
482df930be7Sderaadt 
483aed035abSart 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
484aed035abSart 		memc = &mddtp->mddt_clusters[i];
485aed035abSart #if 0
486aed035abSart 		printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
487aed035abSart 		    memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
488aed035abSart #endif
489aed035abSart 		totalphysmem += memc->mddt_pg_cnt;
490aed035abSart 		if (mem_cluster_cnt < VM_PHYSSEG_MAX) {	/* XXX */
491aed035abSart 			mem_clusters[mem_cluster_cnt].start =
492aed035abSart 			    ptoa(memc->mddt_pfn);
493aed035abSart 			mem_clusters[mem_cluster_cnt].size =
494aed035abSart 			    ptoa(memc->mddt_pg_cnt);
495aed035abSart 			if (memc->mddt_usage & MDDT_mbz ||
496aed035abSart 			    memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
497aed035abSart 			    memc->mddt_usage & MDDT_PALCODE)
498aed035abSart 				mem_clusters[mem_cluster_cnt].size |=
499aed035abSart 				    VM_PROT_READ;
500aed035abSart 			else
501aed035abSart 				mem_clusters[mem_cluster_cnt].size |=
502aed035abSart 				    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
503aed035abSart 			mem_cluster_cnt++;
504ee2d823aSmiod 		} /* XXX else print something! */
505aed035abSart 
506aed035abSart 		if (memc->mddt_usage & MDDT_mbz) {
507aed035abSart 			mddtweird = 1;
508aed035abSart 			printf("WARNING: mem cluster %d has weird "
509aed035abSart 			    "usage 0x%lx\n", i, memc->mddt_usage);
510aed035abSart 			unknownmem += memc->mddt_pg_cnt;
511aed035abSart 			continue;
512aed035abSart 		}
513aed035abSart 		if (memc->mddt_usage & MDDT_NONVOLATILE) {
514aed035abSart 			/* XXX should handle these... */
515aed035abSart 			printf("WARNING: skipping non-volatile mem "
516aed035abSart 			    "cluster %d\n", i);
517aed035abSart 			unusedmem += memc->mddt_pg_cnt;
518aed035abSart 			continue;
519aed035abSart 		}
520aed035abSart 		if (memc->mddt_usage & MDDT_PALCODE) {
521aed035abSart 			resvmem += memc->mddt_pg_cnt;
522aed035abSart 			continue;
523aed035abSart 		}
524aed035abSart 
525aed035abSart 		/*
526aed035abSart 		 * We have a memory cluster available for system
527aed035abSart 		 * software use.  We must determine if this cluster
528aed035abSart 		 * holds the kernel.
529aed035abSart 		 */
530aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
531aed035abSart 		/*
532aed035abSart 		 * XXX If the kernel uses the PROM console, we only use the
533aed035abSart 		 * XXX memory after the kernel in the first system segment,
534aed035abSart 		 * XXX to avoid clobbering prom mapping, data, etc.
535aed035abSart 		 */
536aed035abSart 	    if (!pmap_uses_prom_console() || physmem == 0) {
537aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
538aed035abSart 		physmem += memc->mddt_pg_cnt;
539aed035abSart 		pfn0 = memc->mddt_pfn;
540aed035abSart 		pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
541aed035abSart 		if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
542aed035abSart 			/*
543aed035abSart 			 * Must compute the location of the kernel
544aed035abSart 			 * within the segment.
545aed035abSart 			 */
546aed035abSart #if 0
547aed035abSart 			printf("Cluster %d contains kernel\n", i);
548aed035abSart #endif
549aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
550aed035abSart 		    if (!pmap_uses_prom_console()) {
551aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
552aed035abSart 			if (pfn0 < kernstartpfn) {
553aed035abSart 				/*
554aed035abSart 				 * There is a chunk before the kernel.
555aed035abSart 				 */
556aed035abSart #if 0
557aed035abSart 				printf("Loading chunk before kernel: "
558aed035abSart 				    "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
559aed035abSart #endif
560aed035abSart 				uvm_page_physload(pfn0, kernstartpfn,
561aed035abSart 				    pfn0, kernstartpfn, VM_FREELIST_DEFAULT);
562aed035abSart 			}
563aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
564aed035abSart 		    }
565aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
566aed035abSart 			if (kernendpfn < pfn1) {
567aed035abSart 				/*
568aed035abSart 				 * There is a chunk after the kernel.
569aed035abSart 				 */
570aed035abSart #if 0
571aed035abSart 				printf("Loading chunk after kernel: "
572aed035abSart 				    "0x%lx / 0x%lx\n", kernendpfn, pfn1);
573aed035abSart #endif
574aed035abSart 				uvm_page_physload(kernendpfn, pfn1,
575aed035abSart 				    kernendpfn, pfn1, VM_FREELIST_DEFAULT);
576aed035abSart 			}
577aed035abSart 		} else {
578aed035abSart 			/*
579aed035abSart 			 * Just load this cluster as one chunk.
580aed035abSart 			 */
581aed035abSart #if 0
582aed035abSart 			printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
583aed035abSart 			    pfn0, pfn1);
584aed035abSart #endif
585aed035abSart 			uvm_page_physload(pfn0, pfn1, pfn0, pfn1,
586aed035abSart 			    VM_FREELIST_DEFAULT);
587aed035abSart 		}
588aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
589aed035abSart 	    }
590aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
591aed035abSart 	}
592aed035abSart 
593a37778bcSderaadt #ifdef DEBUG
594aed035abSart 	/*
595aed035abSart 	 * Dump out the MDDT if it looks odd...
596aed035abSart 	 */
597df930be7Sderaadt 	if (mddtweird) {
598df930be7Sderaadt 		printf("\n");
599df930be7Sderaadt 		printf("complete memory cluster information:\n");
600df930be7Sderaadt 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
601df930be7Sderaadt 			printf("mddt %d:\n", i);
602df930be7Sderaadt 			printf("\tpfn %lx\n",
603df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pfn);
604df930be7Sderaadt 			printf("\tcnt %lx\n",
605df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
606df930be7Sderaadt 			printf("\ttest %lx\n",
607df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pg_test);
608df930be7Sderaadt 			printf("\tbva %lx\n",
609df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
610df930be7Sderaadt 			printf("\tbpa %lx\n",
611df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
612df930be7Sderaadt 			printf("\tbcksum %lx\n",
613df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
614df930be7Sderaadt 			printf("\tusage %lx\n",
615df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_usage);
616df930be7Sderaadt 		}
617df930be7Sderaadt 		printf("\n");
618df930be7Sderaadt 	}
619a37778bcSderaadt #endif
620df930be7Sderaadt 
621df930be7Sderaadt 	if (totalphysmem == 0)
622df930be7Sderaadt 		panic("can't happen: system seems to have no memory!");
623df930be7Sderaadt #if 0
624f46637d1Sderaadt 	printf("totalphysmem = %u\n", totalphysmem);
625f46637d1Sderaadt 	printf("physmem = %u\n", physmem);
626df930be7Sderaadt 	printf("resvmem = %d\n", resvmem);
627df930be7Sderaadt 	printf("unusedmem = %d\n", unusedmem);
628df930be7Sderaadt 	printf("unknownmem = %d\n", unknownmem);
629df930be7Sderaadt #endif
630df930be7Sderaadt 
631df930be7Sderaadt 	/*
632aed035abSart 	 * Initialize error message buffer (at end of core).
633df930be7Sderaadt 	 */
634aed035abSart 	{
635aed035abSart 		vsize_t sz = (vsize_t)round_page(MSGBUFSIZE);
636aed035abSart 		vsize_t reqsz = sz;
637df930be7Sderaadt 
638aed035abSart 		vps = &vm_physmem[vm_nphysseg - 1];
639e1da84e1Salex 
640aed035abSart 		/* shrink so that it'll fit in the last segment */
641aed035abSart 		if ((vps->avail_end - vps->avail_start) < atop(sz))
642aed035abSart 			sz = ptoa(vps->avail_end - vps->avail_start);
643aed035abSart 
644aed035abSart 		vps->end -= atop(sz);
645aed035abSart 		vps->avail_end -= atop(sz);
646aed035abSart 		initmsgbuf((caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end)), sz);
647aed035abSart 
648aed035abSart 		/* Remove the last segment if it now has no pages. */
649aed035abSart 		if (vps->start == vps->end)
650aed035abSart 			vm_nphysseg--;
651aed035abSart 
652aed035abSart 		/* warn if the message buffer had to be shrunk */
653aed035abSart 		if (sz != reqsz)
654aed035abSart 			printf("WARNING: %ld bytes not available for msgbuf "
655aed035abSart 			    "in last cluster (%ld used)\n", reqsz, sz);
656aed035abSart 
657aed035abSart 	}
658aed035abSart 
659df930be7Sderaadt 	/*
660df930be7Sderaadt 	 * Init mapping for u page(s) for proc 0
661df930be7Sderaadt 	 */
662aed035abSart 	proc0.p_addr = proc0paddr =
663aed035abSart 	    (struct user *)pmap_steal_memory(UPAGES * PAGE_SIZE, NULL, NULL);
664df930be7Sderaadt 
665df930be7Sderaadt 	/*
666aed035abSart 	 * Allocate space for system data structures.  These data structures
667aed035abSart 	 * are allocated here instead of cpu_startup() because physical
668aed035abSart 	 * memory is directly addressable.  We don't have to map these into
669aed035abSart 	 * virtual address space.
670df930be7Sderaadt 	 */
671aed035abSart 	size = (vsize_t)allocsys(NULL);
672aed035abSart 	v = (caddr_t)pmap_steal_memory(size, NULL, NULL);
673aed035abSart 	if ((allocsys(v) - v) != size)
674aed035abSart 		panic("alpha_init: table size inconsistency");
675df930be7Sderaadt 
676df930be7Sderaadt 	/*
677df930be7Sderaadt 	 * Clear allocated memory.
678df930be7Sderaadt 	 */
679aed035abSart 	bzero(v, size);
680df930be7Sderaadt 
681df930be7Sderaadt 	/*
682df930be7Sderaadt 	 * Initialize the virtual memory system, and set the
683df930be7Sderaadt 	 * page table base register in proc 0's PCB.
684df930be7Sderaadt 	 */
685aed035abSart 	pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
686aed035abSart 	    hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
687df930be7Sderaadt 
688df930be7Sderaadt 	/*
689df930be7Sderaadt 	 * Initialize the rest of proc 0's PCB, and cache its physical
690df930be7Sderaadt 	 * address.
691df930be7Sderaadt 	 */
692df930be7Sderaadt 	proc0.p_md.md_pcbpaddr =
693aed035abSart 	    (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
694df930be7Sderaadt 
695df930be7Sderaadt 	/*
696df930be7Sderaadt 	 * Set the kernel sp, reserving space for an (empty) trapframe,
697df930be7Sderaadt 	 * and make proc0's trapframe pointer point to it for sanity.
698df930be7Sderaadt 	 */
69950ce9ee0Sniklas 	proc0paddr->u_pcb.pcb_hw.apcb_ksp =
700df930be7Sderaadt 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
70174652a67Sniklas 	proc0.p_md.md_tf =
70274652a67Sniklas 	    (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
70350ce9ee0Sniklas 
704aed035abSart 	/*
705aed035abSart 	 * Initialize the primary CPU's idle PCB to proc0's.  In a
706aed035abSart 	 * MULTIPROCESSOR configuration, each CPU will later get
707aed035abSart 	 * its own idle PCB when autoconfiguration runs.
708aed035abSart 	 */
709aed035abSart 	ci->ci_idle_pcb = &proc0paddr->u_pcb;
710aed035abSart 	ci->ci_idle_pcb_paddr = (u_long)proc0.p_md.md_pcbpaddr;
711df930be7Sderaadt 
712df930be7Sderaadt 	/*
713df930be7Sderaadt 	 * Look at arguments passed to us and compute boothowto.
714df930be7Sderaadt 	 */
715417eba8cSderaadt 
716df930be7Sderaadt #ifdef KADB
717df930be7Sderaadt 	boothowto |= RB_KDB;
718df930be7Sderaadt #endif
719aed035abSart 	for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
720417eba8cSderaadt 		/*
721417eba8cSderaadt 		 * Note that we'd really like to differentiate case here,
722417eba8cSderaadt 		 * but the Alpha AXP Architecture Reference Manual
723417eba8cSderaadt 		 * says that we shouldn't.
724417eba8cSderaadt 		 */
725df930be7Sderaadt 		switch (*p) {
726371c77f5Smartin 		case 'a': /* Ignore */
727417eba8cSderaadt 		case 'A':
728df930be7Sderaadt 			break;
729df930be7Sderaadt 
73012f8bbedSniklas 		case 'b': /* Enter DDB as soon as the console is initialised */
73112f8bbedSniklas 		case 'B':
73212f8bbedSniklas 			boothowto |= RB_KDB;
73312f8bbedSniklas 			break;
73412f8bbedSniklas 
73550ce9ee0Sniklas 		case 'c': /* enter user kernel configuration */
73650ce9ee0Sniklas 		case 'C':
73750ce9ee0Sniklas 			boothowto |= RB_CONFIG;
73850ce9ee0Sniklas 			break;
73950ce9ee0Sniklas 
74050ce9ee0Sniklas #ifdef DEBUG
74150ce9ee0Sniklas 		case 'd': /* crash dump immediately after autoconfig */
74250ce9ee0Sniklas 		case 'D':
74350ce9ee0Sniklas 			boothowto |= RB_DUMP;
74450ce9ee0Sniklas 			break;
74550ce9ee0Sniklas #endif
74650ce9ee0Sniklas 
74750ce9ee0Sniklas 		case 'h': /* always halt, never reboot */
74850ce9ee0Sniklas 		case 'H':
74950ce9ee0Sniklas 			boothowto |= RB_HALT;
750df930be7Sderaadt 			break;
751df930be7Sderaadt 
752417eba8cSderaadt #if 0
753417eba8cSderaadt 		case 'm': /* mini root present in memory */
754417eba8cSderaadt 		case 'M':
755417eba8cSderaadt 			boothowto |= RB_MINIROOT;
756417eba8cSderaadt 			break;
757417eba8cSderaadt #endif
75850ce9ee0Sniklas 
75950ce9ee0Sniklas 		case 'n': /* askname */
76050ce9ee0Sniklas 		case 'N':
76150ce9ee0Sniklas 			boothowto |= RB_ASKNAME;
76250ce9ee0Sniklas 			break;
763aed035abSart 
764371c77f5Smartin 		case 's': /* single-user */
765aed035abSart 		case 'S':
766aed035abSart 			boothowto |= RB_SINGLE;
767aed035abSart 			break;
768aed035abSart 
769aed035abSart 		case '-':
770aed035abSart 			/*
771aed035abSart 			 * Just ignore this.  It's not required, but it's
772aed035abSart 			 * common for it to be passed regardless.
773aed035abSart 			 */
774aed035abSart 			break;
775aed035abSart 
776aed035abSart 		default:
777aed035abSart 			printf("Unrecognized boot flag '%c'.\n", *p);
778aed035abSart 			break;
779df930be7Sderaadt 		}
780df930be7Sderaadt 	}
781df930be7Sderaadt 
782aed035abSart 
783df930be7Sderaadt 	/*
784df930be7Sderaadt 	 * Figure out the number of cpus in the box, from RPB fields.
785df930be7Sderaadt 	 * Really.  We mean it.
786df930be7Sderaadt 	 */
7872586fa93Smiod 	for (alpha_cpus = 0, i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
788df930be7Sderaadt 		struct pcs *pcsp;
789df930be7Sderaadt 
790aed035abSart 		pcsp = LOCATE_PCS(hwrpb, i);
791df930be7Sderaadt 		if ((pcsp->pcs_flags & PCS_PP) != 0)
7922586fa93Smiod 			alpha_cpus++;
793df930be7Sderaadt 	}
794aed035abSart 
795aed035abSart 	/*
796aed035abSart 	 * Initialize debuggers, and break into them if appropriate.
797aed035abSart 	 */
798aed035abSart #ifdef DDB
799aed035abSart 	ddb_init();
800aed035abSart 
801aed035abSart 	if (boothowto & RB_KDB)
802aed035abSart 		Debugger();
803aed035abSart #endif
804aed035abSart #ifdef KGDB
805aed035abSart 	if (boothowto & RB_KDB)
806aed035abSart 		kgdb_connect(0);
807aed035abSart #endif
808aed035abSart 	/*
809aed035abSart 	 * Figure out our clock frequency, from RPB fields.
810aed035abSart 	 */
811aed035abSart 	hz = hwrpb->rpb_intr_freq >> 12;
812aed035abSart 	if (!(60 <= hz && hz <= 10240)) {
813aed035abSart 		hz = 1024;
814aed035abSart #ifdef DIAGNOSTIC
815aed035abSart 		printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
816aed035abSart 			hwrpb->rpb_intr_freq, hz);
817aed035abSart #endif
818aed035abSart 	}
819aed035abSart }
820aed035abSart 
821aed035abSart caddr_t
822aed035abSart allocsys(v)
823aed035abSart 	caddr_t v;
824aed035abSart {
825aed035abSart 	/*
826aed035abSart 	 * Allocate space for system data structures.
827aed035abSart 	 * The first available kernel virtual address is in "v".
828aed035abSart 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
829aed035abSart 	 *
830aed035abSart 	 * These data structures are allocated here instead of cpu_startup()
831aed035abSart 	 * because physical memory is directly addressable. We don't have
832aed035abSart 	 * to map these into virtual address space.
833aed035abSart 	 */
834aed035abSart #define valloc(name, type, num) \
835aed035abSart 	    (name) = (type *)v; v = (caddr_t)ALIGN((name)+(num))
836aed035abSart 
837aed035abSart #ifdef SYSVMSG
838aed035abSart 	valloc(msgpool, char, msginfo.msgmax);
839aed035abSart 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
840aed035abSart 	valloc(msghdrs, struct msg, msginfo.msgtql);
841aed035abSart 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
842aed035abSart #endif
843aed035abSart 
844aed035abSart 	/*
845aed035abSart 	 * Determine how many buffers to allocate.
846aed035abSart 	 * We allocate 10% of memory for buffer space.  Insure a
847aed035abSart 	 * minimum of 16 buffers.
848aed035abSart 	 */
849aed035abSart 	if (bufpages == 0)
85060535ec9Smaja 		bufpages = (physmem / (100/bufcachepercent));
851aed035abSart 	if (nbuf == 0) {
852aed035abSart 		nbuf = bufpages;
853aed035abSart 		if (nbuf < 16)
854aed035abSart 			nbuf = 16;
855aed035abSart 	}
856aed035abSart 	valloc(buf, struct buf, nbuf);
857aed035abSart 
858aed035abSart #undef valloc
859aed035abSart 
860aed035abSart 	return v;
861df930be7Sderaadt }
862df930be7Sderaadt 
863417eba8cSderaadt void
864df930be7Sderaadt consinit()
865df930be7Sderaadt {
866aed035abSart 
867aed035abSart 	/*
868aed035abSart 	 * Everything related to console initialization is done
869aed035abSart 	 * in alpha_init().
870aed035abSart 	 */
871aed035abSart #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
872aed035abSart 	printf("consinit: %susing prom console\n",
873aed035abSart 	    pmap_uses_prom_console() ? "" : "not ");
87412f8bbedSniklas #endif
875df930be7Sderaadt }
876df930be7Sderaadt 
877417eba8cSderaadt void
878df930be7Sderaadt cpu_startup()
879df930be7Sderaadt {
880df930be7Sderaadt 	register unsigned i;
881df930be7Sderaadt 	int base, residual;
882aed035abSart 	vaddr_t minaddr, maxaddr;
883aed035abSart 	vsize_t size;
88450ce9ee0Sniklas #if defined(DEBUG)
885df930be7Sderaadt 	extern int pmapdebug;
886df930be7Sderaadt 	int opmapdebug = pmapdebug;
887df930be7Sderaadt 
888df930be7Sderaadt 	pmapdebug = 0;
889df930be7Sderaadt #endif
890df930be7Sderaadt 
891df930be7Sderaadt 	/*
892df930be7Sderaadt 	 * Good {morning,afternoon,evening,night}.
893df930be7Sderaadt 	 */
894df930be7Sderaadt 	printf(version);
895df930be7Sderaadt 	identifycpu();
896f46637d1Sderaadt 	printf("total memory = %ld (%ldK)\n", ptoa((u_long)totalphysmem),
897f46637d1Sderaadt 	    ptoa((u_long)totalphysmem) / 1024);
898f46637d1Sderaadt 	printf("(%ld reserved for PROM, ", ptoa((u_long)resvmem));
899f46637d1Sderaadt 	printf("%ld used by OpenBSD)\n", ptoa((u_long)physmem));
900aed035abSart 	if (unusedmem) {
9013f4ce3b7Smiod 		printf("WARNING: unused memory = %ld (%ldK)\n",
902f46637d1Sderaadt 		    ptoa((u_long)unusedmem), ptoa((u_long)unusedmem) / 1024);
903aed035abSart 	}
904aed035abSart 	if (unknownmem) {
9053f4ce3b7Smiod 		printf("WARNING: %ld (%ldK) of memory with unknown purpose\n",
906f46637d1Sderaadt 		    ptoa((u_long)unknownmem), ptoa((u_long)unknownmem) / 1024);
907aed035abSart 	}
908df930be7Sderaadt 
909df930be7Sderaadt 	/*
910df930be7Sderaadt 	 * Allocate virtual address space for file I/O buffers.
911df930be7Sderaadt 	 * Note they are different than the array of headers, 'buf',
912df930be7Sderaadt 	 * and usually occupy more virtual memory than physical.
913df930be7Sderaadt 	 */
914df930be7Sderaadt 	size = MAXBSIZE * nbuf;
915aed035abSart 	if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
916198a4b3fSart 		    NULL, UVM_UNKNOWN_OFFSET, 0,
917aed035abSart 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
918738a5b4dSart 				UVM_ADV_NORMAL, 0)))
919aed035abSart 		panic("startup: cannot allocate VM for buffers");
920df930be7Sderaadt 	base = bufpages / nbuf;
921df930be7Sderaadt 	residual = bufpages % nbuf;
922df930be7Sderaadt 	for (i = 0; i < nbuf; i++) {
923aed035abSart 		vsize_t curbufsize;
924aed035abSart 		vaddr_t curbuf;
925aed035abSart 		struct vm_page *pg;
926df930be7Sderaadt 
927df930be7Sderaadt 		/*
928aed035abSart 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
929aed035abSart 		 * that MAXBSIZE space, we allocate and map (base+1) pages
930aed035abSart 		 * for the first "residual" buffers, and then we allocate
931aed035abSart 		 * "base" pages for the rest.
932df930be7Sderaadt 		 */
933aed035abSart 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
934194dd68bSbrad 		curbufsize = PAGE_SIZE * ((i < residual) ? (base+1) : base);
935aed035abSart 
936aed035abSart 		while (curbufsize) {
937aed035abSart 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
938aed035abSart 			if (pg == NULL)
939aed035abSart 				panic("cpu_startup: not enough memory for "
940aed035abSart 				    "buffer cache");
941aed035abSart 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
942aed035abSart 					VM_PROT_READ|VM_PROT_WRITE);
943aed035abSart 			curbuf += PAGE_SIZE;
944aed035abSart 			curbufsize -= PAGE_SIZE;
945aed035abSart 		}
9460e5798cfSart 		pmap_update(pmap_kernel());
947df930be7Sderaadt 	}
948df930be7Sderaadt 	/*
949df930be7Sderaadt 	 * Allocate a submap for exec arguments.  This map effectively
950df930be7Sderaadt 	 * limits the number of processes exec'ing at any time.
951df930be7Sderaadt 	 */
9527c10a71dSdrahn 	minaddr = vm_map_min(kernel_map);
953aed035abSart 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
954aed035abSart 	    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
955df930be7Sderaadt 
956df930be7Sderaadt 	/*
957df930be7Sderaadt 	 * Allocate a submap for physio
958df930be7Sderaadt 	 */
959aed035abSart 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
960aed035abSart 	    VM_PHYS_SIZE, 0, FALSE, NULL);
961df930be7Sderaadt 
96250ce9ee0Sniklas #if defined(DEBUG)
963df930be7Sderaadt 	pmapdebug = opmapdebug;
964df930be7Sderaadt #endif
9653f4ce3b7Smiod 	printf("avail memory = %ld (%ldK)\n", (long)ptoa(uvmexp.free),
9663f4ce3b7Smiod 	    (long)ptoa(uvmexp.free) / 1024);
967aed035abSart #if 0
968aed035abSart 	{
969aed035abSart 		extern u_long pmap_pages_stolen;
970aed035abSart 
971aed035abSart 		printf("stolen memory for VM structures = %d\n", pmap_pages_stolen * PAGE_SIZE);
972aed035abSart 	}
973aed035abSart #endif
9743f4ce3b7Smiod 	printf("using %ld buffers containing %ld bytes (%ldK) of memory\n",
975194dd68bSbrad 	    (long)nbuf, (long)bufpages * PAGE_SIZE, (long)bufpages * (PAGE_SIZE / 1024));
976df930be7Sderaadt 
977df930be7Sderaadt 	/*
978df930be7Sderaadt 	 * Set up buffers, so they can be used to read disk labels.
979df930be7Sderaadt 	 */
980df930be7Sderaadt 	bufinit();
981df930be7Sderaadt 
982df930be7Sderaadt 	/*
983df930be7Sderaadt 	 * Configure the system.
984df930be7Sderaadt 	 */
98541033391Sderaadt 	if (boothowto & RB_CONFIG) {
98641033391Sderaadt #ifdef BOOT_CONFIG
98741033391Sderaadt 		user_config();
98841033391Sderaadt #else
98941033391Sderaadt 		printf("kernel does not support -c; continuing..\n");
99041033391Sderaadt #endif
99141033391Sderaadt 	}
99250ce9ee0Sniklas 
99350ce9ee0Sniklas 	/*
994aed035abSart 	 * Set up the HWPCB so that it's safe to configure secondary
995aed035abSart 	 * CPUs.
99650ce9ee0Sniklas 	 */
997aed035abSart 	hwrpb_primary_init();
998aed035abSart }
999aed035abSart 
1000aed035abSart /*
1001aed035abSart  * Retrieve the platform name from the DSR.
1002aed035abSart  */
1003aed035abSart const char *
1004aed035abSart alpha_dsr_sysname()
1005aed035abSart {
1006aed035abSart 	struct dsrdb *dsr;
1007aed035abSart 	const char *sysname;
1008aed035abSart 
1009aed035abSart 	/*
1010aed035abSart 	 * DSR does not exist on early HWRPB versions.
1011aed035abSart 	 */
1012aed035abSart 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
1013aed035abSart 		return (NULL);
1014aed035abSart 
1015aed035abSart 	dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
1016aed035abSart 	sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
1017aed035abSart 	    sizeof(u_int64_t)));
1018aed035abSart 	return (sysname);
1019aed035abSart }
1020aed035abSart 
1021aed035abSart /*
1022aed035abSart  * Lookup the system specified system variation in the provided table,
1023aed035abSart  * returning the model string on match.
1024aed035abSart  */
1025aed035abSart const char *
1026aed035abSart alpha_variation_name(variation, avtp)
1027aed035abSart 	u_int64_t variation;
1028aed035abSart 	const struct alpha_variation_table *avtp;
1029aed035abSart {
1030aed035abSart 	int i;
1031aed035abSart 
1032aed035abSart 	for (i = 0; avtp[i].avt_model != NULL; i++)
1033aed035abSart 		if (avtp[i].avt_variation == variation)
1034aed035abSart 			return (avtp[i].avt_model);
1035aed035abSart 	return (NULL);
1036aed035abSart }
1037aed035abSart 
1038aed035abSart /*
1039aed035abSart  * Generate a default platform name based for unknown system variations.
1040aed035abSart  */
1041aed035abSart const char *
1042aed035abSart alpha_unknown_sysname()
1043aed035abSart {
1044aed035abSart 	static char s[128];		/* safe size */
1045aed035abSart 
1046d5eb2d9aSderaadt 	snprintf(s, sizeof s, "%s family, unknown model variation 0x%lx",
1047aed035abSart 	    platform.family, hwrpb->rpb_variation & SV_ST_MASK);
1048aed035abSart 	return ((const char *)s);
1049df930be7Sderaadt }
1050df930be7Sderaadt 
105150ce9ee0Sniklas void
1052df930be7Sderaadt identifycpu()
1053df930be7Sderaadt {
1054aed035abSart 	char *s;
1055*b3cee53eSmartin 	int slen;
1056df930be7Sderaadt 
1057df930be7Sderaadt 	/*
1058df930be7Sderaadt 	 * print out CPU identification information.
1059df930be7Sderaadt 	 */
1060aed035abSart 	printf("%s", cpu_model);
1061aed035abSart 	for(s = cpu_model; *s; ++s)
1062aed035abSart 		if(strncasecmp(s, "MHz", 3) == 0)
1063aed035abSart 			goto skipMHz;
1064aed035abSart 	printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
1065aed035abSart skipMHz:
1066*b3cee53eSmartin 	/* fill in hw_serial if a serial number is known */
1067*b3cee53eSmartin 	slen = strlen(hwrpb->rpb_ssn) + 1;
1068*b3cee53eSmartin 	if (slen > 1) {
1069*b3cee53eSmartin 		hw_serial = malloc(slen, M_SYSCTL, M_NOWAIT);
1070*b3cee53eSmartin 		if (hw_serial)
1071*b3cee53eSmartin 			strlcpy(hw_serial, (char *)hwrpb->rpb_ssn, slen);
1072*b3cee53eSmartin 	}
1073*b3cee53eSmartin 
1074aed035abSart 	printf("\n");
107550ce9ee0Sniklas 	printf("%ld byte page size, %d processor%s.\n",
10762586fa93Smiod 	    hwrpb->rpb_page_size, alpha_cpus, alpha_cpus == 1 ? "" : "s");
1077df930be7Sderaadt #if 0
1078*b3cee53eSmartin 	/* this is not particularly useful! */
1079df930be7Sderaadt 	printf("variation: 0x%lx, revision 0x%lx\n",
1080df930be7Sderaadt 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
1081df930be7Sderaadt #endif
1082df930be7Sderaadt }
1083df930be7Sderaadt 
1084df930be7Sderaadt int	waittime = -1;
1085df930be7Sderaadt struct pcb dumppcb;
1086df930be7Sderaadt 
1087417eba8cSderaadt void
1088aed035abSart boot(howto)
1089df930be7Sderaadt 	int howto;
1090df930be7Sderaadt {
1091aed035abSart #if defined(MULTIPROCESSOR)
1092aed035abSart #if 0 /* XXX See below. */
1093aed035abSart 	u_long cpu_id;
1094aed035abSart #endif
1095aed035abSart #endif
1096aed035abSart 
1097aed035abSart #if defined(MULTIPROCESSOR)
1098aed035abSart 	/* We must be running on the primary CPU. */
1099aed035abSart 	if (alpha_pal_whami() != hwrpb->rpb_primary_cpu_id)
1100aed035abSart 		panic("cpu_reboot: not on primary CPU!");
1101aed035abSart #endif
1102aed035abSart 
1103df930be7Sderaadt 	/* If system is cold, just halt. */
1104df930be7Sderaadt 	if (cold) {
1105c9ad5066Stom 		/* (Unless the user explicitly asked for reboot.) */
1106c9ad5066Stom 		if ((howto & RB_USERREQ) == 0)
1107df930be7Sderaadt 			howto |= RB_HALT;
1108df930be7Sderaadt 		goto haltsys;
1109df930be7Sderaadt 	}
1110df930be7Sderaadt 
111150ce9ee0Sniklas 	/* If "always halt" was specified as a boot flag, obey. */
111250ce9ee0Sniklas 	if ((boothowto & RB_HALT) != 0)
111350ce9ee0Sniklas 		howto |= RB_HALT;
111450ce9ee0Sniklas 
1115df930be7Sderaadt 	boothowto = howto;
1116df930be7Sderaadt 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
1117df930be7Sderaadt 		waittime = 0;
1118df930be7Sderaadt 		vfs_shutdown();
1119df930be7Sderaadt 		/*
1120df930be7Sderaadt 		 * If we've been adjusting the clock, the todr
11212417125dSmiod 		 * will be out of synch; adjust it now unless
11222417125dSmiod 		 * the system has been sitting in ddb.
1123df930be7Sderaadt 		 */
11242417125dSmiod 		if ((howto & RB_TIMEBAD) == 0) {
1125df930be7Sderaadt 			resettodr();
11262417125dSmiod 		} else {
11272417125dSmiod 			printf("WARNING: not updating battery clock\n");
11282417125dSmiod 		}
1129df930be7Sderaadt 	}
1130df930be7Sderaadt 
1131df930be7Sderaadt 	/* Disable interrupts. */
1132df930be7Sderaadt 	splhigh();
1133df930be7Sderaadt 
1134df930be7Sderaadt 	/* If rebooting and a dump is requested do it. */
113550ce9ee0Sniklas 	if (howto & RB_DUMP)
1136df930be7Sderaadt 		dumpsys();
1137df930be7Sderaadt 
113834fbf6deSderaadt haltsys:
113934fbf6deSderaadt 
1140df930be7Sderaadt 	/* run any shutdown hooks */
1141df930be7Sderaadt 	doshutdownhooks();
1142df930be7Sderaadt 
1143aed035abSart #if defined(MULTIPROCESSOR)
1144aed035abSart #if 0 /* XXX doesn't work when called from here?! */
1145aed035abSart 	/* Kill off any secondary CPUs. */
1146aed035abSart 	for (cpu_id = 0; cpu_id < hwrpb->rpb_pcs_cnt; cpu_id++) {
1147aed035abSart 		if (cpu_id == hwrpb->rpb_primary_cpu_id ||
1148aed035abSart 		    cpu_info[cpu_id].ci_softc == NULL)
1149aed035abSart 			continue;
1150aed035abSart 		cpu_halt_secondary(cpu_id);
1151aed035abSart 	}
1152aed035abSart #endif
1153aed035abSart #endif
1154aed035abSart 
1155df930be7Sderaadt #ifdef BOOTKEY
1156df930be7Sderaadt 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
1157aed035abSart 	cnpollc(1);	/* for proper keyboard command handling */
1158df930be7Sderaadt 	cngetc();
1159aed035abSart 	cnpollc(0);
1160df930be7Sderaadt 	printf("\n");
1161df930be7Sderaadt #endif
1162df930be7Sderaadt 
1163aed035abSart 	/* Finally, powerdown/halt/reboot the system. */
1164aed035abSart 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN &&
1165aed035abSart 	    platform.powerdown != NULL) {
1166aed035abSart 		(*platform.powerdown)();
1167aed035abSart 		printf("WARNING: powerdown failed!\n");
1168aed035abSart 	}
1169df930be7Sderaadt 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
1170df930be7Sderaadt 	prom_halt(howto & RB_HALT);
1171df930be7Sderaadt 	/*NOTREACHED*/
1172df930be7Sderaadt }
1173df930be7Sderaadt 
1174df930be7Sderaadt /*
1175df930be7Sderaadt  * These variables are needed by /sbin/savecore
1176df930be7Sderaadt  */
1177df930be7Sderaadt u_long	dumpmag = 0x8fca0101;	/* magic number */
1178df930be7Sderaadt int 	dumpsize = 0;		/* pages */
1179df930be7Sderaadt long	dumplo = 0; 		/* blocks */
1180df930be7Sderaadt 
1181df930be7Sderaadt /*
118250ce9ee0Sniklas  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
118350ce9ee0Sniklas  */
118450ce9ee0Sniklas int
118550ce9ee0Sniklas cpu_dumpsize()
118650ce9ee0Sniklas {
118750ce9ee0Sniklas 	int size;
118850ce9ee0Sniklas 
1189aed035abSart 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1190aed035abSart 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
119150ce9ee0Sniklas 	if (roundup(size, dbtob(1)) != dbtob(1))
119250ce9ee0Sniklas 		return -1;
119350ce9ee0Sniklas 
119450ce9ee0Sniklas 	return (1);
119550ce9ee0Sniklas }
119650ce9ee0Sniklas 
119750ce9ee0Sniklas /*
1198aed035abSart  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1199aed035abSart  */
1200aed035abSart u_long
1201aed035abSart cpu_dump_mempagecnt()
1202aed035abSart {
1203aed035abSart 	u_long i, n;
1204aed035abSart 
1205aed035abSart 	n = 0;
1206aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++)
1207aed035abSart 		n += atop(mem_clusters[i].size);
1208aed035abSart 	return (n);
1209aed035abSart }
1210aed035abSart 
1211aed035abSart /*
121250ce9ee0Sniklas  * cpu_dump: dump machine-dependent kernel core dump headers.
121350ce9ee0Sniklas  */
121450ce9ee0Sniklas int
121550ce9ee0Sniklas cpu_dump()
121650ce9ee0Sniklas {
1217c4071fd1Smillert 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
1218aed035abSart 	char buf[dbtob(1)];
121950ce9ee0Sniklas 	kcore_seg_t *segp;
122050ce9ee0Sniklas 	cpu_kcore_hdr_t *cpuhdrp;
1221aed035abSart 	phys_ram_seg_t *memsegp;
1222aed035abSart 	int i;
122350ce9ee0Sniklas 
122450ce9ee0Sniklas 	dump = bdevsw[major(dumpdev)].d_dump;
122550ce9ee0Sniklas 
1226aed035abSart 	bzero(buf, sizeof buf);
122750ce9ee0Sniklas 	segp = (kcore_seg_t *)buf;
1228aed035abSart 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1229aed035abSart 	memsegp = (phys_ram_seg_t *)&buf[ALIGN(sizeof(*segp)) +
1230aed035abSart 	    ALIGN(sizeof(*cpuhdrp))];
123150ce9ee0Sniklas 
123250ce9ee0Sniklas 	/*
123350ce9ee0Sniklas 	 * Generate a segment header.
123450ce9ee0Sniklas 	 */
123550ce9ee0Sniklas 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
123650ce9ee0Sniklas 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
123750ce9ee0Sniklas 
123850ce9ee0Sniklas 	/*
1239aed035abSart 	 * Add the machine-dependent header info.
124050ce9ee0Sniklas 	 */
1241aed035abSart 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
124250ce9ee0Sniklas 	cpuhdrp->page_size = PAGE_SIZE;
1243aed035abSart 	cpuhdrp->nmemsegs = mem_cluster_cnt;
1244aed035abSart 
1245aed035abSart 	/*
1246aed035abSart 	 * Fill in the memory segment descriptors.
1247aed035abSart 	 */
1248aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++) {
1249aed035abSart 		memsegp[i].start = mem_clusters[i].start;
1250aed035abSart 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1251aed035abSart 	}
125250ce9ee0Sniklas 
125350ce9ee0Sniklas 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
125450ce9ee0Sniklas }
125550ce9ee0Sniklas 
125650ce9ee0Sniklas /*
1257aed035abSart  * This is called by main to set dumplo and dumpsize.
1258194dd68bSbrad  * Dumps always skip the first PAGE_SIZE of disk space
1259df930be7Sderaadt  * in case there might be a disk label stored there.
1260df930be7Sderaadt  * If there is extra space, put dump at the end to
1261df930be7Sderaadt  * reduce the chance that swapping trashes it.
1262df930be7Sderaadt  */
1263df930be7Sderaadt void
1264df930be7Sderaadt dumpconf()
1265df930be7Sderaadt {
126650ce9ee0Sniklas 	int nblks, dumpblks;	/* size of dump area */
1267df930be7Sderaadt 	int maj;
1268df930be7Sderaadt 
1269df930be7Sderaadt 	if (dumpdev == NODEV)
127050ce9ee0Sniklas 		goto bad;
1271df930be7Sderaadt 	maj = major(dumpdev);
1272df930be7Sderaadt 	if (maj < 0 || maj >= nblkdev)
1273df930be7Sderaadt 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
1274df930be7Sderaadt 	if (bdevsw[maj].d_psize == NULL)
127550ce9ee0Sniklas 		goto bad;
1276df930be7Sderaadt 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
1277df930be7Sderaadt 	if (nblks <= ctod(1))
127850ce9ee0Sniklas 		goto bad;
127950ce9ee0Sniklas 
128050ce9ee0Sniklas 	dumpblks = cpu_dumpsize();
128150ce9ee0Sniklas 	if (dumpblks < 0)
128250ce9ee0Sniklas 		goto bad;
1283aed035abSart 	dumpblks += ctod(cpu_dump_mempagecnt());
128450ce9ee0Sniklas 
128550ce9ee0Sniklas 	/* If dump won't fit (incl. room for possible label), punt. */
128650ce9ee0Sniklas 	if (dumpblks > (nblks - ctod(1)))
128750ce9ee0Sniklas 		goto bad;
128850ce9ee0Sniklas 
128950ce9ee0Sniklas 	/* Put dump at end of partition */
129050ce9ee0Sniklas 	dumplo = nblks - dumpblks;
129150ce9ee0Sniklas 
129250ce9ee0Sniklas 	/* dumpsize is in page units, and doesn't include headers. */
1293aed035abSart 	dumpsize = cpu_dump_mempagecnt();
1294df930be7Sderaadt 	return;
1295df930be7Sderaadt 
129650ce9ee0Sniklas bad:
129750ce9ee0Sniklas 	dumpsize = 0;
129850ce9ee0Sniklas 	return;
1299df930be7Sderaadt }
1300df930be7Sderaadt 
1301df930be7Sderaadt /*
130250ce9ee0Sniklas  * Dump the kernel's image to the swap partition.
1303df930be7Sderaadt  */
1304194dd68bSbrad #define	BYTES_PER_DUMP	PAGE_SIZE
130550ce9ee0Sniklas 
1306df930be7Sderaadt void
1307df930be7Sderaadt dumpsys()
1308df930be7Sderaadt {
1309aed035abSart 	u_long totalbytesleft, bytes, i, n, memcl;
1310aed035abSart 	u_long maddr;
1311aed035abSart 	int psize;
131250ce9ee0Sniklas 	daddr_t blkno;
1313c4071fd1Smillert 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
131450ce9ee0Sniklas 	int error;
1315067cbd75Sderaadt 	extern int msgbufmapped;
1316df930be7Sderaadt 
131750ce9ee0Sniklas 	/* Save registers. */
131850ce9ee0Sniklas 	savectx(&dumppcb);
131950ce9ee0Sniklas 
132050ce9ee0Sniklas 	msgbufmapped = 0;	/* don't record dump msgs in msgbuf */
1321df930be7Sderaadt 	if (dumpdev == NODEV)
1322df930be7Sderaadt 		return;
132350ce9ee0Sniklas 
132450ce9ee0Sniklas 	/*
132550ce9ee0Sniklas 	 * For dumps during autoconfiguration,
132650ce9ee0Sniklas 	 * if dump device has already configured...
132750ce9ee0Sniklas 	 */
1328df930be7Sderaadt 	if (dumpsize == 0)
132950ce9ee0Sniklas 		dumpconf();
133050ce9ee0Sniklas 	if (dumplo <= 0) {
1331aed035abSart 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
1332aed035abSart 		    minor(dumpdev));
1333df930be7Sderaadt 		return;
1334df930be7Sderaadt 	}
1335aed035abSart 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
1336aed035abSart 	    minor(dumpdev), dumplo);
1337df930be7Sderaadt 
133850ce9ee0Sniklas 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
1339df930be7Sderaadt 	printf("dump ");
134050ce9ee0Sniklas 	if (psize == -1) {
134150ce9ee0Sniklas 		printf("area unavailable\n");
134250ce9ee0Sniklas 		return;
134350ce9ee0Sniklas 	}
134450ce9ee0Sniklas 
134550ce9ee0Sniklas 	/* XXX should purge all outstanding keystrokes. */
134650ce9ee0Sniklas 
134750ce9ee0Sniklas 	if ((error = cpu_dump()) != 0)
134850ce9ee0Sniklas 		goto err;
134950ce9ee0Sniklas 
1350aed035abSart 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
135150ce9ee0Sniklas 	blkno = dumplo + cpu_dumpsize();
135250ce9ee0Sniklas 	dump = bdevsw[major(dumpdev)].d_dump;
135350ce9ee0Sniklas 	error = 0;
1354aed035abSart 
1355aed035abSart 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1356aed035abSart 		maddr = mem_clusters[memcl].start;
1357aed035abSart 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1358aed035abSart 
1359aed035abSart 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
136050ce9ee0Sniklas 
136150ce9ee0Sniklas 			/* Print out how many MBs we to go. */
1362aed035abSart 			if ((totalbytesleft % (1024*1024)) == 0)
1363aed035abSart 				printf("%ld ", totalbytesleft / (1024 * 1024));
136450ce9ee0Sniklas 
136550ce9ee0Sniklas 			/* Limit size for next transfer. */
1366aed035abSart 			n = bytes - i;
136750ce9ee0Sniklas 			if (n > BYTES_PER_DUMP)
136850ce9ee0Sniklas 				n =  BYTES_PER_DUMP;
136950ce9ee0Sniklas 
137050ce9ee0Sniklas 			error = (*dump)(dumpdev, blkno,
137150ce9ee0Sniklas 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
137250ce9ee0Sniklas 			if (error)
1373aed035abSart 				goto err;
137450ce9ee0Sniklas 			maddr += n;
137550ce9ee0Sniklas 			blkno += btodb(n);			/* XXX? */
137650ce9ee0Sniklas 
137750ce9ee0Sniklas 			/* XXX should look for keystrokes, to cancel. */
137850ce9ee0Sniklas 		}
1379aed035abSart 	}
138050ce9ee0Sniklas 
138150ce9ee0Sniklas err:
138250ce9ee0Sniklas 	switch (error) {
1383a37778bcSderaadt #ifdef DEBUG
1384df930be7Sderaadt 	case ENXIO:
1385df930be7Sderaadt 		printf("device bad\n");
1386df930be7Sderaadt 		break;
1387df930be7Sderaadt 
1388df930be7Sderaadt 	case EFAULT:
1389df930be7Sderaadt 		printf("device not ready\n");
1390df930be7Sderaadt 		break;
1391df930be7Sderaadt 
1392df930be7Sderaadt 	case EINVAL:
1393df930be7Sderaadt 		printf("area improper\n");
1394df930be7Sderaadt 		break;
1395df930be7Sderaadt 
1396df930be7Sderaadt 	case EIO:
1397df930be7Sderaadt 		printf("i/o error\n");
1398df930be7Sderaadt 		break;
1399df930be7Sderaadt 
1400df930be7Sderaadt 	case EINTR:
1401df930be7Sderaadt 		printf("aborted from console\n");
1402df930be7Sderaadt 		break;
1403a37778bcSderaadt #endif /* DEBUG */
140450ce9ee0Sniklas 	case 0:
1405df930be7Sderaadt 		printf("succeeded\n");
1406df930be7Sderaadt 		break;
140750ce9ee0Sniklas 
140850ce9ee0Sniklas 	default:
140950ce9ee0Sniklas 		printf("error %d\n", error);
141050ce9ee0Sniklas 		break;
1411df930be7Sderaadt 	}
1412df930be7Sderaadt 	printf("\n\n");
1413df930be7Sderaadt 	delay(1000);
1414df930be7Sderaadt }
1415df930be7Sderaadt 
1416df930be7Sderaadt void
1417df930be7Sderaadt frametoreg(framep, regp)
1418df930be7Sderaadt 	struct trapframe *framep;
1419df930be7Sderaadt 	struct reg *regp;
1420df930be7Sderaadt {
1421df930be7Sderaadt 
1422df930be7Sderaadt 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1423df930be7Sderaadt 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1424df930be7Sderaadt 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1425df930be7Sderaadt 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1426df930be7Sderaadt 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1427df930be7Sderaadt 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1428df930be7Sderaadt 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1429df930be7Sderaadt 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1430df930be7Sderaadt 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1431df930be7Sderaadt 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1432df930be7Sderaadt 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1433df930be7Sderaadt 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1434df930be7Sderaadt 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1435df930be7Sderaadt 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1436df930be7Sderaadt 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1437df930be7Sderaadt 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
143850ce9ee0Sniklas 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
143950ce9ee0Sniklas 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
144050ce9ee0Sniklas 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1441df930be7Sderaadt 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1442df930be7Sderaadt 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1443df930be7Sderaadt 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1444df930be7Sderaadt 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1445df930be7Sderaadt 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1446df930be7Sderaadt 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1447df930be7Sderaadt 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1448df930be7Sderaadt 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1449df930be7Sderaadt 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1450df930be7Sderaadt 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
145150ce9ee0Sniklas 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
145250ce9ee0Sniklas 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1453df930be7Sderaadt 	regp->r_regs[R_ZERO] = 0;
1454df930be7Sderaadt }
1455df930be7Sderaadt 
1456df930be7Sderaadt void
1457df930be7Sderaadt regtoframe(regp, framep)
1458df930be7Sderaadt 	struct reg *regp;
1459df930be7Sderaadt 	struct trapframe *framep;
1460df930be7Sderaadt {
1461df930be7Sderaadt 
1462df930be7Sderaadt 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1463df930be7Sderaadt 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1464df930be7Sderaadt 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1465df930be7Sderaadt 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1466df930be7Sderaadt 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1467df930be7Sderaadt 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1468df930be7Sderaadt 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1469df930be7Sderaadt 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1470df930be7Sderaadt 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1471df930be7Sderaadt 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1472df930be7Sderaadt 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1473df930be7Sderaadt 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1474df930be7Sderaadt 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1475df930be7Sderaadt 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1476df930be7Sderaadt 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1477df930be7Sderaadt 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
147850ce9ee0Sniklas 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
147950ce9ee0Sniklas 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
148050ce9ee0Sniklas 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1481df930be7Sderaadt 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1482df930be7Sderaadt 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1483df930be7Sderaadt 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1484df930be7Sderaadt 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1485df930be7Sderaadt 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1486df930be7Sderaadt 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1487df930be7Sderaadt 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1488df930be7Sderaadt 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1489df930be7Sderaadt 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1490df930be7Sderaadt 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
149150ce9ee0Sniklas 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
149250ce9ee0Sniklas 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1493df930be7Sderaadt 	/* ??? = regp->r_regs[R_ZERO]; */
1494df930be7Sderaadt }
1495df930be7Sderaadt 
1496df930be7Sderaadt void
1497df930be7Sderaadt printregs(regp)
1498df930be7Sderaadt 	struct reg *regp;
1499df930be7Sderaadt {
1500df930be7Sderaadt 	int i;
1501df930be7Sderaadt 
1502df930be7Sderaadt 	for (i = 0; i < 32; i++)
1503df930be7Sderaadt 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1504df930be7Sderaadt 		   i & 1 ? "\n" : "\t");
1505df930be7Sderaadt }
1506df930be7Sderaadt 
1507df930be7Sderaadt void
1508df930be7Sderaadt regdump(framep)
1509df930be7Sderaadt 	struct trapframe *framep;
1510df930be7Sderaadt {
1511df930be7Sderaadt 	struct reg reg;
1512df930be7Sderaadt 
1513df930be7Sderaadt 	frametoreg(framep, &reg);
151450ce9ee0Sniklas 	reg.r_regs[R_SP] = alpha_pal_rdusp();
151550ce9ee0Sniklas 
1516df930be7Sderaadt 	printf("REGISTERS:\n");
1517df930be7Sderaadt 	printregs(&reg);
1518df930be7Sderaadt }
1519df930be7Sderaadt 
1520df930be7Sderaadt #ifdef DEBUG
1521df930be7Sderaadt int sigdebug = 0;
1522df930be7Sderaadt int sigpid = 0;
1523df930be7Sderaadt #define	SDB_FOLLOW	0x01
1524df930be7Sderaadt #define	SDB_KSTACK	0x02
1525df930be7Sderaadt #endif
1526df930be7Sderaadt 
1527df930be7Sderaadt /*
1528df930be7Sderaadt  * Send an interrupt to process.
1529df930be7Sderaadt  */
1530df930be7Sderaadt void
15315e1760a6Sderaadt sendsig(catcher, sig, mask, code, type, val)
1532df930be7Sderaadt 	sig_t catcher;
1533df930be7Sderaadt 	int sig, mask;
1534df930be7Sderaadt 	u_long code;
15355e1760a6Sderaadt 	int type;
15365e1760a6Sderaadt 	union sigval val;
1537df930be7Sderaadt {
1538df930be7Sderaadt 	struct proc *p = curproc;
1539df930be7Sderaadt 	struct sigcontext *scp, ksc;
1540df930be7Sderaadt 	struct trapframe *frame;
1541df930be7Sderaadt 	struct sigacts *psp = p->p_sigacts;
15422bf9c155Sderaadt 	int oonstack, fsize, rndfsize, kscsize;
15432bf9c155Sderaadt 	siginfo_t *sip, ksi;
1544df930be7Sderaadt 
1545df930be7Sderaadt 	frame = p->p_md.md_tf;
1546df930be7Sderaadt 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
1547df930be7Sderaadt 	fsize = sizeof ksc;
1548df930be7Sderaadt 	rndfsize = ((fsize + 15) / 16) * 16;
15492bf9c155Sderaadt 	kscsize = rndfsize;
15502bf9c155Sderaadt 	if (psp->ps_siginfo & sigmask(sig)) {
15512bf9c155Sderaadt 		fsize += sizeof ksi;
15522bf9c155Sderaadt 		rndfsize = ((fsize + 15) / 16) * 16;
15532bf9c155Sderaadt 	}
155474652a67Sniklas 
1555df930be7Sderaadt 	/*
1556df930be7Sderaadt 	 * Allocate and validate space for the signal handler
1557df930be7Sderaadt 	 * context. Note that if the stack is in P0 space, the
1558aed035abSart 	 * call to uvm_grow() is a nop, and the useracc() check
1559df930be7Sderaadt 	 * will fail if the process has not already allocated
1560df930be7Sderaadt 	 * the space with a `brk'.
1561df930be7Sderaadt 	 */
1562df930be7Sderaadt 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
1563df930be7Sderaadt 	    (psp->ps_sigonstack & sigmask(sig))) {
15648bc2093aSderaadt 		scp = (struct sigcontext *)(psp->ps_sigstk.ss_sp +
1565df930be7Sderaadt 		    psp->ps_sigstk.ss_size - rndfsize);
1566df930be7Sderaadt 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
1567df930be7Sderaadt 	} else
156850ce9ee0Sniklas 		scp = (struct sigcontext *)(alpha_pal_rdusp() - rndfsize);
1569df930be7Sderaadt 	if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
1570aed035abSart 		(void)uvm_grow(p, (u_long)scp);
1571df930be7Sderaadt #ifdef DEBUG
1572df930be7Sderaadt 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
157350ce9ee0Sniklas 		printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
1574df930be7Sderaadt 		    sig, &oonstack, scp);
1575df930be7Sderaadt #endif
1576df930be7Sderaadt 
1577df930be7Sderaadt 	/*
1578df930be7Sderaadt 	 * Build the signal context to be used by sigreturn.
1579df930be7Sderaadt 	 */
1580df930be7Sderaadt 	ksc.sc_onstack = oonstack;
1581df930be7Sderaadt 	ksc.sc_mask = mask;
158250ce9ee0Sniklas 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
158350ce9ee0Sniklas 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
1584df930be7Sderaadt 
1585df930be7Sderaadt 	/* copy the registers. */
1586df930be7Sderaadt 	frametoreg(frame, (struct reg *)ksc.sc_regs);
1587df930be7Sderaadt 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
158850ce9ee0Sniklas 	ksc.sc_regs[R_SP] = alpha_pal_rdusp();
1589df930be7Sderaadt 
1590df930be7Sderaadt 	/* save the floating-point state, if necessary, then copy it. */
1591433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1592433075b6Spvalchev 		fpusave_proc(p, 1);
1593df930be7Sderaadt 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
1594433075b6Spvalchev 	memcpy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
1595df930be7Sderaadt 	    sizeof(struct fpreg));
1596433075b6Spvalchev #ifndef NO_IEEE
1597433075b6Spvalchev 	ksc.sc_fp_control = alpha_read_fp_c(p);
1598433075b6Spvalchev #else
1599433075b6Spvalchev 	ksc.sc_fp_control = 0;
1600433075b6Spvalchev #endif
1601433075b6Spvalchev 	memset(ksc.sc_reserved, 0, sizeof ksc.sc_reserved);	/* XXX */
1602433075b6Spvalchev 	memset(ksc.sc_xxx, 0, sizeof ksc.sc_xxx);		/* XXX */
1603df930be7Sderaadt 
1604df930be7Sderaadt #ifdef COMPAT_OSF1
1605df930be7Sderaadt 	/*
1606df930be7Sderaadt 	 * XXX Create an OSF/1-style sigcontext and associated goo.
1607df930be7Sderaadt 	 */
1608df930be7Sderaadt #endif
1609df930be7Sderaadt 
16102bf9c155Sderaadt 	if (psp->ps_siginfo & sigmask(sig)) {
16112bf9c155Sderaadt 		initsiginfo(&ksi, sig, code, type, val);
16122bf9c155Sderaadt 		sip = (void *)scp + kscsize;
1613679ebc41Smiod 		if (copyout((caddr_t)&ksi, (caddr_t)sip, fsize - kscsize) != 0)
1614679ebc41Smiod 			goto trash;
1615aa540fb8Sart 	} else
1616aa540fb8Sart 		sip = NULL;
16172bf9c155Sderaadt 
1618df930be7Sderaadt 	/*
1619df930be7Sderaadt 	 * copy the frame out to userland.
1620df930be7Sderaadt 	 */
1621679ebc41Smiod 	if (copyout((caddr_t)&ksc, (caddr_t)scp, kscsize) != 0) {
1622679ebc41Smiod trash:
1623679ebc41Smiod #ifdef DEBUG
1624679ebc41Smiod 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1625679ebc41Smiod 			printf("sendsig(%d): copyout failed on sig %d\n",
1626679ebc41Smiod 			    p->p_pid, sig);
1627679ebc41Smiod #endif
1628679ebc41Smiod 		/*
1629679ebc41Smiod 		 * Process has trashed its stack; give it an illegal
1630679ebc41Smiod 		 * instruction to halt it in its tracks.
1631679ebc41Smiod 		 */
163286fd84b3Smiod 		sigexit(p, SIGILL);
163386fd84b3Smiod 		/* NOTREACHED */
1634679ebc41Smiod 	}
1635df930be7Sderaadt #ifdef DEBUG
1636df930be7Sderaadt 	if (sigdebug & SDB_FOLLOW)
163750ce9ee0Sniklas 		printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
1638df930be7Sderaadt 		    scp, code);
1639df930be7Sderaadt #endif
1640df930be7Sderaadt 
1641df930be7Sderaadt 	/*
1642df930be7Sderaadt 	 * Set up the registers to return to sigcode.
1643df930be7Sderaadt 	 */
16444a5480feSart 	frame->tf_regs[FRAME_PC] = p->p_sigcode;
164550ce9ee0Sniklas 	frame->tf_regs[FRAME_A0] = sig;
1646aa540fb8Sart 	frame->tf_regs[FRAME_A1] = (u_int64_t)sip;
164750ce9ee0Sniklas 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
1648df930be7Sderaadt 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
164950ce9ee0Sniklas 	alpha_pal_wrusp((unsigned long)scp);
1650df930be7Sderaadt 
1651df930be7Sderaadt #ifdef DEBUG
1652df930be7Sderaadt 	if (sigdebug & SDB_FOLLOW)
1653df930be7Sderaadt 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
165450ce9ee0Sniklas 		    frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
1655df930be7Sderaadt 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1656df930be7Sderaadt 		printf("sendsig(%d): sig %d returns\n",
1657df930be7Sderaadt 		    p->p_pid, sig);
1658df930be7Sderaadt #endif
1659df930be7Sderaadt }
1660df930be7Sderaadt 
1661df930be7Sderaadt /*
1662df930be7Sderaadt  * System call to cleanup state after a signal
1663df930be7Sderaadt  * has been taken.  Reset signal mask and
1664df930be7Sderaadt  * stack state from context left by sendsig (above).
1665df930be7Sderaadt  * Return to previous pc and psl as specified by
1666df930be7Sderaadt  * context left by sendsig. Check carefully to
1667df930be7Sderaadt  * make sure that the user has not modified the
1668125cd19fSderaadt  * psl to gain improper privileges or to cause
1669df930be7Sderaadt  * a machine fault.
1670df930be7Sderaadt  */
1671df930be7Sderaadt /* ARGSUSED */
1672df930be7Sderaadt int
1673df930be7Sderaadt sys_sigreturn(p, v, retval)
1674df930be7Sderaadt 	struct proc *p;
1675df930be7Sderaadt 	void *v;
1676df930be7Sderaadt 	register_t *retval;
1677df930be7Sderaadt {
1678df930be7Sderaadt 	struct sys_sigreturn_args /* {
1679df930be7Sderaadt 		syscallarg(struct sigcontext *) sigcntxp;
1680df930be7Sderaadt 	} */ *uap = v;
1681aa540fb8Sart 	struct sigcontext ksc;
168260959295Smartin #ifdef DEBUG
168360959295Smartin 	struct sigcontext *scp;
168460959295Smartin #endif
1685aa540fb8Sart 	int error;
1686df930be7Sderaadt 
1687df930be7Sderaadt #ifdef DEBUG
1688df930be7Sderaadt 	if (sigdebug & SDB_FOLLOW)
168950ce9ee0Sniklas 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
1690df930be7Sderaadt #endif
1691df930be7Sderaadt 
1692df930be7Sderaadt 	/*
1693df930be7Sderaadt 	 * Test and fetch the context structure.
1694df930be7Sderaadt 	 * We grab it all at once for speed.
1695df930be7Sderaadt 	 */
1696aa540fb8Sart 	if ((error = copyin(SCARG(uap, sigcntxp), &ksc, sizeof(ksc))) != 0)
1697aa540fb8Sart 		return (error);
1698df930be7Sderaadt 
1699df930be7Sderaadt 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
1700df930be7Sderaadt 		return (EINVAL);
1701df930be7Sderaadt 	/*
1702df930be7Sderaadt 	 * Restore the user-supplied information
1703df930be7Sderaadt 	 */
1704df930be7Sderaadt 	if (ksc.sc_onstack)
1705df930be7Sderaadt 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
1706df930be7Sderaadt 	else
1707df930be7Sderaadt 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
1708df930be7Sderaadt 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
1709df930be7Sderaadt 
171050ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
171150ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_PS] =
171250ce9ee0Sniklas 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
1713df930be7Sderaadt 
1714df930be7Sderaadt 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
171550ce9ee0Sniklas 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
1716df930be7Sderaadt 
1717df930be7Sderaadt 	/* XXX ksc.sc_ownedfp ? */
1718433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1719433075b6Spvalchev 		fpusave_proc(p, 0);
1720433075b6Spvalchev 	memcpy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
1721df930be7Sderaadt 	    sizeof(struct fpreg));
1722433075b6Spvalchev #ifndef NO_IEEE
1723433075b6Spvalchev 	p->p_addr->u_pcb.pcb_fp.fpr_cr = ksc.sc_fpcr;
1724433075b6Spvalchev 	p->p_md.md_flags = ksc.sc_fp_control & MDP_FP_C;
1725433075b6Spvalchev #endif
1726df930be7Sderaadt 
1727df930be7Sderaadt #ifdef DEBUG
1728df930be7Sderaadt 	if (sigdebug & SDB_FOLLOW)
1729df930be7Sderaadt 		printf("sigreturn(%d): returns\n", p->p_pid);
1730df930be7Sderaadt #endif
1731df930be7Sderaadt 	return (EJUSTRETURN);
1732df930be7Sderaadt }
1733df930be7Sderaadt 
1734df930be7Sderaadt /*
1735df930be7Sderaadt  * machine dependent system variables.
1736df930be7Sderaadt  */
173750ce9ee0Sniklas int
1738df930be7Sderaadt cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1739df930be7Sderaadt 	int *name;
1740df930be7Sderaadt 	u_int namelen;
1741df930be7Sderaadt 	void *oldp;
1742df930be7Sderaadt 	size_t *oldlenp;
1743df930be7Sderaadt 	void *newp;
1744df930be7Sderaadt 	size_t newlen;
1745df930be7Sderaadt 	struct proc *p;
1746df930be7Sderaadt {
1747df930be7Sderaadt 	dev_t consdev;
1748df930be7Sderaadt 
174945e5a1a0Sart 	if (name[0] != CPU_CHIPSET && namelen != 1)
1750df930be7Sderaadt 		return (ENOTDIR);		/* overloaded */
1751df930be7Sderaadt 
1752df930be7Sderaadt 	switch (name[0]) {
1753df930be7Sderaadt 	case CPU_CONSDEV:
1754df930be7Sderaadt 		if (cn_tab != NULL)
1755df930be7Sderaadt 			consdev = cn_tab->cn_dev;
1756df930be7Sderaadt 		else
1757df930be7Sderaadt 			consdev = NODEV;
1758df930be7Sderaadt 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
1759df930be7Sderaadt 			sizeof consdev));
1760417eba8cSderaadt 
1761417eba8cSderaadt 	case CPU_ROOT_DEVICE:
1762aed035abSart 		return (sysctl_rdstring(oldp, oldlenp, newp,
1763aed035abSart 		    root_device));
1764a37778bcSderaadt #ifndef SMALL_KERNEL
176550ce9ee0Sniklas 	case CPU_UNALIGNED_PRINT:
176650ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
176750ce9ee0Sniklas 		    &alpha_unaligned_print));
176850ce9ee0Sniklas 
176950ce9ee0Sniklas 	case CPU_UNALIGNED_FIX:
177050ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
177150ce9ee0Sniklas 		    &alpha_unaligned_fix));
177250ce9ee0Sniklas 
177350ce9ee0Sniklas 	case CPU_UNALIGNED_SIGBUS:
177450ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
177550ce9ee0Sniklas 		    &alpha_unaligned_sigbus));
177650ce9ee0Sniklas 
17773a630e3fSniklas 	case CPU_BOOTED_KERNEL:
1778aed035abSart 		return (sysctl_rdstring(oldp, oldlenp, newp,
1779aed035abSart 		    bootinfo.booted_kernel));
17803a630e3fSniklas 
178145e5a1a0Sart 	case CPU_CHIPSET:
178245e5a1a0Sart 		return (alpha_sysctl_chipset(name + 1, namelen - 1, oldp,
178345e5a1a0Sart 		    oldlenp));
1784a37778bcSderaadt #endif /* SMALL_KERNEL */
1785433075b6Spvalchev 
1786433075b6Spvalchev #ifndef NO_IEEE
1787433075b6Spvalchev 	case CPU_FP_SYNC_COMPLETE:
1788433075b6Spvalchev 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1789433075b6Spvalchev 		    &alpha_fp_sync_complete));
1790433075b6Spvalchev #endif
179127626149Smatthieu 	case CPU_ALLOWAPERTURE:
179227626149Smatthieu #ifdef APERTURE
179327626149Smatthieu 		if (securelevel > 0)
17941546ceefSderaadt 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
17951546ceefSderaadt 			    &allowaperture));
179627626149Smatthieu                 else
179727626149Smatthieu                         return (sysctl_int(oldp, oldlenp, newp, newlen,
179827626149Smatthieu                             &allowaperture));
179927626149Smatthieu #else
180027626149Smatthieu 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
180127626149Smatthieu #endif
1802df930be7Sderaadt 	default:
1803df930be7Sderaadt 		return (EOPNOTSUPP);
1804df930be7Sderaadt 	}
1805df930be7Sderaadt 	/* NOTREACHED */
1806df930be7Sderaadt }
1807df930be7Sderaadt 
1808df930be7Sderaadt /*
1809df930be7Sderaadt  * Set registers on exec.
1810df930be7Sderaadt  */
1811df930be7Sderaadt void
1812df930be7Sderaadt setregs(p, pack, stack, retval)
1813df930be7Sderaadt 	register struct proc *p;
1814df930be7Sderaadt 	struct exec_package *pack;
1815df930be7Sderaadt 	u_long stack;
1816df930be7Sderaadt 	register_t *retval;
1817df930be7Sderaadt {
1818df930be7Sderaadt 	struct trapframe *tfp = p->p_md.md_tf;
18193a630e3fSniklas #ifdef DEBUG
18203a630e3fSniklas 	int i;
18213a630e3fSniklas #endif
1822df930be7Sderaadt 
1823df930be7Sderaadt #ifdef DEBUG
182450ce9ee0Sniklas 	/*
182550ce9ee0Sniklas 	 * Crash and dump, if the user requested it.
182650ce9ee0Sniklas 	 */
182750ce9ee0Sniklas 	if (boothowto & RB_DUMP)
182850ce9ee0Sniklas 		panic("crash requested by boot flags");
182950ce9ee0Sniklas #endif
183050ce9ee0Sniklas 
183150ce9ee0Sniklas #ifdef DEBUG
183250ce9ee0Sniklas 	for (i = 0; i < FRAME_SIZE; i++)
1833df930be7Sderaadt 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
1834df930be7Sderaadt #else
183550ce9ee0Sniklas 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1836df930be7Sderaadt #endif
1837df930be7Sderaadt 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
183850ce9ee0Sniklas 	alpha_pal_wrusp(stack);
183950ce9ee0Sniklas 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
184050ce9ee0Sniklas 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1841df930be7Sderaadt 
184250ce9ee0Sniklas 	tfp->tf_regs[FRAME_A0] = stack;
184350ce9ee0Sniklas 	/* a1 and a2 already zeroed */
184450ce9ee0Sniklas 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
184550ce9ee0Sniklas 
184650ce9ee0Sniklas 	p->p_md.md_flags &= ~MDP_FPUSED;
1847433075b6Spvalchev #ifndef NO_IEEE
1848433075b6Spvalchev 	if (__predict_true((p->p_md.md_flags & IEEE_INHERIT) == 0)) {
1849433075b6Spvalchev 		p->p_md.md_flags &= ~MDP_FP_C;
1850433075b6Spvalchev 		p->p_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
1851433075b6Spvalchev 	}
1852433075b6Spvalchev #endif
1853433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1854433075b6Spvalchev 		fpusave_proc(p, 0);
1855ee2d823aSmiod 
1856ee2d823aSmiod 	retval[1] = 0;
1857433075b6Spvalchev }
1858df930be7Sderaadt 
1859433075b6Spvalchev /*
1860433075b6Spvalchev  * Release the FPU.
1861433075b6Spvalchev  */
1862433075b6Spvalchev void
1863433075b6Spvalchev fpusave_cpu(struct cpu_info *ci, int save)
1864433075b6Spvalchev {
1865433075b6Spvalchev 	struct proc *p;
1866433075b6Spvalchev 
1867433075b6Spvalchev 	KDASSERT(ci == curcpu());
1868433075b6Spvalchev 
1869433075b6Spvalchev #if defined(MULTIPROCESSOR)
1870433075b6Spvalchev 	atomic_setbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1871433075b6Spvalchev #endif
1872433075b6Spvalchev 
1873433075b6Spvalchev 	p = ci->ci_fpcurproc;
1874433075b6Spvalchev 	if (p == NULL)
1875433075b6Spvalchev 		goto out;
1876433075b6Spvalchev 
1877433075b6Spvalchev 	if (save) {
1878433075b6Spvalchev 		alpha_pal_wrfen(1);
1879433075b6Spvalchev 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
1880433075b6Spvalchev 	}
1881433075b6Spvalchev 
1882433075b6Spvalchev 	alpha_pal_wrfen(0);
1883433075b6Spvalchev 
1884433075b6Spvalchev 	p->p_addr->u_pcb.pcb_fpcpu = NULL;
1885433075b6Spvalchev 	ci->ci_fpcurproc = NULL;
1886433075b6Spvalchev 
1887433075b6Spvalchev out:
1888433075b6Spvalchev #if defined(MULTIPROCESSOR)
1889433075b6Spvalchev 	atomic_clearbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1890433075b6Spvalchev #endif
1891433075b6Spvalchev 	return;
1892433075b6Spvalchev }
1893433075b6Spvalchev 
1894433075b6Spvalchev /*
1895433075b6Spvalchev  * Synchronize FP state for this process.
1896433075b6Spvalchev  */
1897433075b6Spvalchev void
1898433075b6Spvalchev fpusave_proc(struct proc *p, int save)
1899433075b6Spvalchev {
1900433075b6Spvalchev 	struct cpu_info *ci = curcpu();
1901433075b6Spvalchev 	struct cpu_info *oci;
1902433075b6Spvalchev #if defined(MULTIPROCESSOR)
1903433075b6Spvalchev 	u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
19042199c730Smartin 	int spincount;
1905433075b6Spvalchev #endif
1906433075b6Spvalchev 
1907433075b6Spvalchev 	KDASSERT(p->p_addr != NULL);
1908433075b6Spvalchev 
1909433075b6Spvalchev 	oci = p->p_addr->u_pcb.pcb_fpcpu;
1910433075b6Spvalchev 	if (oci == NULL) {
1911433075b6Spvalchev 		return;
1912433075b6Spvalchev 	}
1913433075b6Spvalchev 
1914433075b6Spvalchev #if defined(MULTIPROCESSOR)
1915433075b6Spvalchev 	if (oci == ci) {
1916433075b6Spvalchev 		KASSERT(ci->ci_fpcurproc == p);
1917433075b6Spvalchev 		fpusave_cpu(ci, save);
1918433075b6Spvalchev 		return;
1919433075b6Spvalchev 	}
1920433075b6Spvalchev 
1921433075b6Spvalchev 	KASSERT(oci->ci_fpcurproc == p);
1922433075b6Spvalchev 	alpha_send_ipi(oci->ci_cpuid, ipi);
1923433075b6Spvalchev 
1924433075b6Spvalchev 	spincount = 0;
1925433075b6Spvalchev 	while (p->p_addr->u_pcb.pcb_fpcpu != NULL) {
1926433075b6Spvalchev 		spincount++;
1927433075b6Spvalchev 		delay(1000);    /* XXX */
1928433075b6Spvalchev 		if (spincount > 10000)
1929433075b6Spvalchev 			panic("fpsave ipi didn't");
1930433075b6Spvalchev 	}
1931433075b6Spvalchev #else
1932433075b6Spvalchev 	KASSERT(ci->ci_fpcurproc == p);
1933433075b6Spvalchev 	fpusave_cpu(ci, save);
1934433075b6Spvalchev #endif /* MULTIPROCESSOR */
1935df930be7Sderaadt }
1936df930be7Sderaadt 
1937df930be7Sderaadt int
1938df930be7Sderaadt spl0()
1939df930be7Sderaadt {
1940df930be7Sderaadt 
1941aed035abSart 	if (ssir) {
1942aed035abSart 		(void) alpha_pal_swpipl(ALPHA_PSL_IPL_SOFT);
19432a2685f2Sart 		softintr_dispatch();
1944aed035abSart 	}
1945df930be7Sderaadt 
194650ce9ee0Sniklas 	return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
1947df930be7Sderaadt }
1948df930be7Sderaadt 
1949df930be7Sderaadt /*
1950df930be7Sderaadt  * The following primitives manipulate the run queues.  _whichqs tells which
1951df930be7Sderaadt  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
1952e464495eSniklas  * into queues, Remrunqueue removes them from queues.  The running process is
1953e464495eSniklas  * on no queue, other processes are on a queue related to p->p_priority,
1954e464495eSniklas  * divided by 4 actually to shrink the 0-127 range of priorities into the 32
1955e464495eSniklas  * available queues.
1956df930be7Sderaadt  */
1957df930be7Sderaadt /*
1958df930be7Sderaadt  * setrunqueue(p)
1959df930be7Sderaadt  *	proc *p;
1960df930be7Sderaadt  *
1961df930be7Sderaadt  * Call should be made at splclock(), and p->p_stat should be SRUN.
1962df930be7Sderaadt  */
1963df930be7Sderaadt 
19642a2685f2Sart /* XXXART - grmble */
19652a2685f2Sart #define sched_qs qs
19662a2685f2Sart #define sched_whichqs whichqs
19672a2685f2Sart 
1968df930be7Sderaadt void
1969df930be7Sderaadt setrunqueue(p)
1970df930be7Sderaadt 	struct proc *p;
1971df930be7Sderaadt {
1972df930be7Sderaadt 	int bit;
1973df930be7Sderaadt 
1974df930be7Sderaadt 	/* firewall: p->p_back must be NULL */
1975df930be7Sderaadt 	if (p->p_back != NULL)
1976df930be7Sderaadt 		panic("setrunqueue");
1977df930be7Sderaadt 
1978df930be7Sderaadt 	bit = p->p_priority >> 2;
19792a2685f2Sart 	sched_whichqs |= (1 << bit);
19802a2685f2Sart 	p->p_forw = (struct proc *)&sched_qs[bit];
19812a2685f2Sart 	p->p_back = sched_qs[bit].ph_rlink;
1982df930be7Sderaadt 	p->p_back->p_forw = p;
19832a2685f2Sart 	sched_qs[bit].ph_rlink = p;
1984df930be7Sderaadt }
1985df930be7Sderaadt 
1986df930be7Sderaadt /*
1987e464495eSniklas  * remrunqueue(p)
1988df930be7Sderaadt  *
1989df930be7Sderaadt  * Call should be made at splclock().
1990df930be7Sderaadt  */
1991df930be7Sderaadt void
1992d3cbbad5Skstailey remrunqueue(p)
1993df930be7Sderaadt 	struct proc *p;
1994df930be7Sderaadt {
1995df930be7Sderaadt 	int bit;
1996df930be7Sderaadt 
1997df930be7Sderaadt 	bit = p->p_priority >> 2;
19982a2685f2Sart 	if ((sched_whichqs & (1 << bit)) == 0)
1999d3cbbad5Skstailey 		panic("remrunqueue");
2000df930be7Sderaadt 
2001df930be7Sderaadt 	p->p_back->p_forw = p->p_forw;
2002df930be7Sderaadt 	p->p_forw->p_back = p->p_back;
2003df930be7Sderaadt 	p->p_back = NULL;	/* for firewall checking. */
2004df930be7Sderaadt 
20052a2685f2Sart 	if ((struct proc *)&sched_qs[bit] == sched_qs[bit].ph_link)
20062a2685f2Sart 		sched_whichqs &= ~(1 << bit);
2007df930be7Sderaadt }
2008df930be7Sderaadt 
2009df930be7Sderaadt /*
2010df930be7Sderaadt  * Return the best possible estimate of the time in the timeval
2011df930be7Sderaadt  * to which tvp points.  Unfortunately, we can't read the hardware registers.
2012df930be7Sderaadt  * We guarantee that the time will be greater than the value obtained by a
2013df930be7Sderaadt  * previous call.
2014df930be7Sderaadt  */
2015df930be7Sderaadt void
2016df930be7Sderaadt microtime(tvp)
2017df930be7Sderaadt 	register struct timeval *tvp;
2018df930be7Sderaadt {
2019df930be7Sderaadt 	int s = splclock();
2020df930be7Sderaadt 	static struct timeval lasttime;
2021df930be7Sderaadt 
2022df930be7Sderaadt 	*tvp = time;
2023df930be7Sderaadt #ifdef notdef
2024df930be7Sderaadt 	tvp->tv_usec += clkread();
20254c891e15Spjanzen 	while (tvp->tv_usec >= 1000000) {
2026df930be7Sderaadt 		tvp->tv_sec++;
2027df930be7Sderaadt 		tvp->tv_usec -= 1000000;
2028df930be7Sderaadt 	}
2029df930be7Sderaadt #endif
2030df930be7Sderaadt 	if (tvp->tv_sec == lasttime.tv_sec &&
2031df930be7Sderaadt 	    tvp->tv_usec <= lasttime.tv_usec &&
20324c891e15Spjanzen 	    (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
2033df930be7Sderaadt 		tvp->tv_sec++;
2034df930be7Sderaadt 		tvp->tv_usec -= 1000000;
2035df930be7Sderaadt 	}
2036df930be7Sderaadt 	lasttime = *tvp;
2037df930be7Sderaadt 	splx(s);
2038df930be7Sderaadt }
2039df930be7Sderaadt 
2040417eba8cSderaadt /*
2041417eba8cSderaadt  * Wait "n" microseconds.
2042417eba8cSderaadt  */
204350ce9ee0Sniklas void
2044417eba8cSderaadt delay(n)
204550ce9ee0Sniklas 	unsigned long n;
2046417eba8cSderaadt {
20475d097e9eSmiod 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
20485d097e9eSmiod 
20495d097e9eSmiod 	if (n == 0)
20505d097e9eSmiod 		return;
20515d097e9eSmiod 
20525d097e9eSmiod 	pcc0 = alpha_rpcc() & 0xffffffffUL;
20535d097e9eSmiod 	cycles = 0;
20545d097e9eSmiod 	usec = 0;
20555d097e9eSmiod 
20565d097e9eSmiod 	while (usec <= n) {
20575d097e9eSmiod 		/*
20585d097e9eSmiod 		 * Get the next CPU cycle count - assumes that we can not
20595d097e9eSmiod 		 * have had more than one 32 bit overflow.
20605d097e9eSmiod 		 */
20615d097e9eSmiod 		pcc1 = alpha_rpcc() & 0xffffffffUL;
20625d097e9eSmiod 		if (pcc1 < pcc0)
20635d097e9eSmiod 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
20645d097e9eSmiod 		else
20655d097e9eSmiod 			curcycle = pcc1 - pcc0;
2066417eba8cSderaadt 
2067aed035abSart 		/*
20685d097e9eSmiod 		 * We now have the number of processor cycles since we
20695d097e9eSmiod 		 * last checked. Add the current cycle count to the
20705d097e9eSmiod 		 * running total. If it's over cycles_per_usec, increment
20715d097e9eSmiod 		 * the usec counter.
2072aed035abSart 		 */
20735d097e9eSmiod 		cycles += curcycle;
20745d097e9eSmiod 		while (cycles > cycles_per_usec) {
20755d097e9eSmiod 			usec++;
20765d097e9eSmiod 			cycles -= cycles_per_usec;
20775d097e9eSmiod 		}
20785d097e9eSmiod 		pcc0 = pcc1;
20795d097e9eSmiod 	}
2080417eba8cSderaadt }
2081417eba8cSderaadt 
20829da89091Sderaadt #if defined(COMPAT_OSF1)
2083c4071fd1Smillert void	cpu_exec_ecoff_setregs(struct proc *, struct exec_package *,
2084c4071fd1Smillert 	    u_long, register_t *);
20853a630e3fSniklas 
2086df930be7Sderaadt void
2087417eba8cSderaadt cpu_exec_ecoff_setregs(p, epp, stack, retval)
2088df930be7Sderaadt 	struct proc *p;
2089417eba8cSderaadt 	struct exec_package *epp;
2090df930be7Sderaadt 	u_long stack;
2091df930be7Sderaadt 	register_t *retval;
2092df930be7Sderaadt {
2093417eba8cSderaadt 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
2094df930be7Sderaadt 
2095417eba8cSderaadt 	setregs(p, epp, stack, retval);
209650ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
2097df930be7Sderaadt }
2098df930be7Sderaadt 
2099df930be7Sderaadt /*
2100df930be7Sderaadt  * cpu_exec_ecoff_hook():
2101df930be7Sderaadt  *	cpu-dependent ECOFF format hook for execve().
2102df930be7Sderaadt  *
2103df930be7Sderaadt  * Do any machine-dependent diddling of the exec package when doing ECOFF.
2104df930be7Sderaadt  *
2105df930be7Sderaadt  */
2106df930be7Sderaadt int
2107417eba8cSderaadt cpu_exec_ecoff_hook(p, epp)
2108df930be7Sderaadt 	struct proc *p;
2109df930be7Sderaadt 	struct exec_package *epp;
2110df930be7Sderaadt {
2111417eba8cSderaadt 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
2112c3114d5bSericj 	extern struct emul emul_native;
2113aed035abSart 	int error;
21144e8700e2Sericj 	extern int osf1_exec_ecoff_hook(struct proc *, struct exec_package *);
2115df930be7Sderaadt 
2116417eba8cSderaadt 	switch (execp->f.f_magic) {
2117df930be7Sderaadt #ifdef COMPAT_OSF1
2118df930be7Sderaadt 	case ECOFF_MAGIC_ALPHA:
21194e8700e2Sericj 		error = osf1_exec_ecoff_hook(p, epp);
2120df930be7Sderaadt 		break;
2121df930be7Sderaadt #endif
2122df930be7Sderaadt 
212350ce9ee0Sniklas 	case ECOFF_MAGIC_NATIVE_ALPHA:
2124a2f8ce8dSderaadt 		epp->ep_emul = &emul_native;
2125aed035abSart 		error = 0;
2126df930be7Sderaadt 		break;
2127df930be7Sderaadt 
2128df930be7Sderaadt 	default:
2129aed035abSart 		error = ENOEXEC;
2130df930be7Sderaadt 	}
2131aed035abSart 	return (error);
2132df930be7Sderaadt }
2133df930be7Sderaadt #endif
2134e464495eSniklas 
2135aed035abSart int
2136aed035abSart alpha_pa_access(pa)
2137aed035abSart 	u_long pa;
2138aed035abSart {
2139aed035abSart 	int i;
2140aed035abSart 
2141aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++) {
2142aed035abSart 		if (pa < mem_clusters[i].start)
2143aed035abSart 			continue;
2144aed035abSart 		if ((pa - mem_clusters[i].start) >=
2145aed035abSart 		    (mem_clusters[i].size & ~PAGE_MASK))
2146aed035abSart 			continue;
2147aed035abSart 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
2148aed035abSart 	}
2149aed035abSart 
2150aed035abSart 	/*
2151aed035abSart 	 * Address is not a memory address.  If we're secure, disallow
2152aed035abSart 	 * access.  Otherwise, grant read/write.
2153aed035abSart 	 */
2154aed035abSart 	if (securelevel > 0)
2155aed035abSart 		return (VM_PROT_NONE);
2156aed035abSart 	else
2157aed035abSart 		return (VM_PROT_READ | VM_PROT_WRITE);
2158aed035abSart }
2159aed035abSart 
2160e464495eSniklas /* XXX XXX BEGIN XXX XXX */
2161aed035abSart paddr_t alpha_XXX_dmamap_or;					/* XXX */
2162e464495eSniklas 								/* XXX */
2163aed035abSart paddr_t								/* XXX */
2164e464495eSniklas alpha_XXX_dmamap(v)						/* XXX */
2165aed035abSart 	vaddr_t v;						/* XXX */
2166e464495eSniklas {								/* XXX */
2167e464495eSniklas 								/* XXX */
2168e464495eSniklas 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
2169e464495eSniklas }								/* XXX */
2170e464495eSniklas /* XXX XXX END XXX XXX */
2171