xref: /openbsd/sys/arch/alpha/alpha/machdep.c (revision c62181b1)
1*c62181b1Sbrad /* $OpenBSD: machdep.c,v 1.101 2006/06/15 20:08:29 brad 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 
103aed035abSart #include <uvm/uvm_extern.h>
104df930be7Sderaadt 
105df930be7Sderaadt #include <dev/cons.h>
106df930be7Sderaadt 
10750ce9ee0Sniklas #include <machine/autoconf.h>
108df930be7Sderaadt #include <machine/cpu.h>
109df930be7Sderaadt #include <machine/reg.h>
110df930be7Sderaadt #include <machine/rpb.h>
111df930be7Sderaadt #include <machine/prom.h>
1123a630e3fSniklas #include <machine/cpuconf.h>
113433075b6Spvalchev #ifndef NO_IEEE
114433075b6Spvalchev #include <machine/ieeefp.h>
115433075b6Spvalchev #endif
116df930be7Sderaadt 
11745e5a1a0Sart #include <dev/pci/pcivar.h>
11845e5a1a0Sart 
11912f8bbedSniklas #ifdef DDB
12012f8bbedSniklas #include <machine/db_machdep.h>
12112f8bbedSniklas #include <ddb/db_access.h>
12212f8bbedSniklas #include <ddb/db_sym.h>
12312f8bbedSniklas #include <ddb/db_extern.h>
12412f8bbedSniklas #endif
12512f8bbedSniklas 
126c4071fd1Smillert int	cpu_dump(void);
127c4071fd1Smillert int	cpu_dumpsize(void);
128c4071fd1Smillert u_long	cpu_dump_mempagecnt(void);
129c4071fd1Smillert void	dumpsys(void);
130c4071fd1Smillert caddr_t allocsys(caddr_t);
131c4071fd1Smillert void	identifycpu(void);
132c4071fd1Smillert void	regdump(struct trapframe *framep);
133c4071fd1Smillert void	printregs(struct reg *);
134df930be7Sderaadt 
135df930be7Sderaadt /*
136df930be7Sderaadt  * Declare these as initialized data so we can patch them.
137df930be7Sderaadt  */
138df930be7Sderaadt #ifdef	NBUF
139df930be7Sderaadt int	nbuf = NBUF;
140df930be7Sderaadt #else
141df930be7Sderaadt int	nbuf = 0;
142df930be7Sderaadt #endif
14360535ec9Smaja 
14460535ec9Smaja #ifndef BUFCACHEPERCENT
14560535ec9Smaja #define BUFCACHEPERCENT 10
14660535ec9Smaja #endif
14760535ec9Smaja 
148df930be7Sderaadt #ifdef	BUFPAGES
149df930be7Sderaadt int	bufpages = BUFPAGES;
150df930be7Sderaadt #else
151df930be7Sderaadt int	bufpages = 0;
152df930be7Sderaadt #endif
15360535ec9Smaja int	bufcachepercent = BUFCACHEPERCENT;
154aed035abSart 
155ab8e80c5Sart struct vm_map *exec_map = NULL;
156ab8e80c5Sart struct vm_map *phys_map = NULL;
157aed035abSart 
15827626149Smatthieu #ifdef APERTURE
15927626149Smatthieu #ifdef INSECURE
16027626149Smatthieu int allowaperture = 1;
16127626149Smatthieu #else
16227626149Smatthieu int allowaperture = 0;
16327626149Smatthieu #endif
16427626149Smatthieu #endif
16527626149Smatthieu 
166df930be7Sderaadt int	totalphysmem;		/* total amount of physical memory in system */
16774652a67Sniklas int	physmem;		/* physical mem used by OpenBSD + some rsvd */
168df930be7Sderaadt int	resvmem;		/* amount of memory reserved for PROM */
169df930be7Sderaadt int	unusedmem;		/* amount of memory for OS that we don't use */
170df930be7Sderaadt int	unknownmem;		/* amount of memory with an unknown use */
171df930be7Sderaadt 
172df930be7Sderaadt int	cputype;		/* system type, from the RPB */
1732586fa93Smiod int	alpha_cpus;
174df930be7Sderaadt 
1752a2685f2Sart int	bootdev_debug = 0;	/* patchable, or from DDB */
1762a2685f2Sart 
177df930be7Sderaadt /*
178df930be7Sderaadt  * XXX We need an address to which we can assign things so that they
179df930be7Sderaadt  * won't be optimized away because we didn't use the value.
180df930be7Sderaadt  */
181df930be7Sderaadt u_int32_t no_optimize;
182df930be7Sderaadt 
183df930be7Sderaadt /* the following is used externally (sysctl_hw) */
184aed035abSart char	machine[] = MACHINE;		/* from <machine/param.h> */
185417eba8cSderaadt char	cpu_model[128];
186aed035abSart char	root_device[17];
187df930be7Sderaadt 
188df930be7Sderaadt struct	user *proc0paddr;
189df930be7Sderaadt 
190df930be7Sderaadt /* Number of machine cycles per microsecond */
191df930be7Sderaadt u_int64_t	cycles_per_usec;
192df930be7Sderaadt 
193aed035abSart struct bootinfo_kernel bootinfo;
194aed035abSart 
195aed035abSart /* For built-in TCDS */
196aed035abSart #if defined(DEC_3000_300) || defined(DEC_3000_500)
197aed035abSart u_int8_t	dec_3000_scsiid[2], dec_3000_scsifast[2];
198aed035abSart #endif
199aed035abSart 
200aed035abSart struct platform platform;
201417eba8cSderaadt 
202417eba8cSderaadt /* for cpu_sysctl() */
20350ce9ee0Sniklas int	alpha_unaligned_print = 1;	/* warn about unaligned accesses */
20450ce9ee0Sniklas int	alpha_unaligned_fix = 1;	/* fix up unaligned accesses */
205881c1eabSart int	alpha_unaligned_sigbus = 1;	/* SIGBUS on fixed-up accesses */
206433075b6Spvalchev #ifndef NO_IEEE
207433075b6Spvalchev int	alpha_fp_sync_complete = 0;	/* fp fixup if sync even without /s */
208433075b6Spvalchev #endif
20950ce9ee0Sniklas 
210aed035abSart /*
211aed035abSart  * XXX This should be dynamically sized, but we have the chicken-egg problem!
212aed035abSart  * XXX it should also be larger than it is, because not all of the mddt
213aed035abSart  * XXX clusters end up being used for VM.
214aed035abSart  */
215aed035abSart phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];	/* low size bits overloaded */
216aed035abSart int	mem_cluster_cnt;
217aed035abSart 
2183a630e3fSniklas void
219aed035abSart alpha_init(pfn, ptb, bim, bip, biv)
220df930be7Sderaadt 	u_long pfn;		/* first free PFN number */
221df930be7Sderaadt 	u_long ptb;		/* PFN of current level 1 page table */
222aed035abSart 	u_long bim;		/* bootinfo magic */
223aed035abSart 	u_long bip;		/* bootinfo pointer */
224aed035abSart 	u_long biv;		/* bootinfo version */
225df930be7Sderaadt {
226aed035abSart 	extern char kernel_text[], _end[];
227df930be7Sderaadt 	struct mddt *mddtp;
228aed035abSart 	struct mddt_cluster *memc;
229df930be7Sderaadt 	int i, mddtweird;
230aed035abSart 	struct vm_physseg *vps;
231aed035abSart 	vaddr_t kernstart, kernend;
232aed035abSart 	paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
233aed035abSart 	vsize_t size;
234df930be7Sderaadt 	char *p;
235aed035abSart 	caddr_t v;
2362a2685f2Sart 	const char *bootinfo_msg;
237aed035abSart 	const struct cpuinit *c;
238aed035abSart 	extern caddr_t esym;
239aed035abSart 	struct cpu_info *ci;
240aed035abSart 	cpuid_t cpu_id;
241df930be7Sderaadt 
242aed035abSart 	/* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
243f3914c62Sniklas 
244df930be7Sderaadt 	/*
245aed035abSart 	 * Turn off interrupts (not mchecks) and floating point.
246df930be7Sderaadt 	 * Make sure the instruction and data streams are consistent.
247df930be7Sderaadt 	 */
248aed035abSart 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
24950ce9ee0Sniklas 	alpha_pal_wrfen(0);
25050ce9ee0Sniklas 	ALPHA_TBIA();
25150ce9ee0Sniklas 	alpha_pal_imb();
252df930be7Sderaadt 
253*c62181b1Sbrad 	/* Initialize the SCB. */
254*c62181b1Sbrad 	scb_init();
255*c62181b1Sbrad 
256aed035abSart 	cpu_id = cpu_number();
257aed035abSart 
258aed035abSart #if defined(MULTIPROCESSOR)
259df930be7Sderaadt 	/*
260aed035abSart 	 * Set our SysValue to the address of our cpu_info structure.
261aed035abSart 	 * Secondary processors do this in their spinup trampoline.
262df930be7Sderaadt 	 */
263aed035abSart 	alpha_pal_wrval((u_long)&cpu_info[cpu_id]);
264aed035abSart #endif
265aed035abSart 
266aed035abSart 	ci = curcpu();
267aed035abSart 	ci->ci_cpuid = cpu_id;
268aed035abSart 
269aed035abSart 	/*
270aed035abSart 	 * Get critical system information (if possible, from the
271aed035abSart 	 * information provided by the boot program).
272aed035abSart 	 */
273aed035abSart 	bootinfo_msg = NULL;
274aed035abSart 	if (bim == BOOTINFO_MAGIC) {
275aed035abSart 		if (biv == 0) {		/* backward compat */
276aed035abSart 			biv = *(u_long *)bip;
277aed035abSart 			bip += 8;
278aed035abSart 		}
279aed035abSart 		switch (biv) {
280aed035abSart 		case 1: {
281aed035abSart 			struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
282aed035abSart 
283aed035abSart 			bootinfo.ssym = v1p->ssym;
284aed035abSart 			bootinfo.esym = v1p->esym;
285aed035abSart 			/* hwrpb may not be provided by boot block in v1 */
286aed035abSart 			if (v1p->hwrpb != NULL) {
287aed035abSart 				bootinfo.hwrpb_phys =
288aed035abSart 				    ((struct rpb *)v1p->hwrpb)->rpb_phys;
289aed035abSart 				bootinfo.hwrpb_size = v1p->hwrpbsize;
290aed035abSart 			} else {
291aed035abSart 				bootinfo.hwrpb_phys =
292aed035abSart 				    ((struct rpb *)HWRPB_ADDR)->rpb_phys;
293aed035abSart 				bootinfo.hwrpb_size =
294aed035abSart 				    ((struct rpb *)HWRPB_ADDR)->rpb_size;
295aed035abSart 			}
296aed035abSart 			bcopy(v1p->boot_flags, bootinfo.boot_flags,
297aed035abSart 			    min(sizeof v1p->boot_flags,
298aed035abSart 			      sizeof bootinfo.boot_flags));
299aed035abSart 			bcopy(v1p->booted_kernel, bootinfo.booted_kernel,
300aed035abSart 			    min(sizeof v1p->booted_kernel,
301aed035abSart 			      sizeof bootinfo.booted_kernel));
302aed035abSart 			/* booted dev not provided in bootinfo */
303aed035abSart 			init_prom_interface((struct rpb *)
304aed035abSart 			    ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
305aed035abSart                 	prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
306aed035abSart 			    sizeof bootinfo.booted_dev);
307aed035abSart 			break;
308aed035abSart 		}
309aed035abSart 		default:
310aed035abSart 			bootinfo_msg = "unknown bootinfo version";
311aed035abSart 			goto nobootinfo;
312aed035abSart 		}
313aed035abSart 	} else {
314aed035abSart 		bootinfo_msg = "boot program did not pass bootinfo";
315aed035abSart nobootinfo:
316aed035abSart 		bootinfo.ssym = (u_long)_end;
317aed035abSart 		bootinfo.esym = (u_long)_end;
318aed035abSart 		bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
319aed035abSart 		bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
320aed035abSart 		init_prom_interface((struct rpb *)HWRPB_ADDR);
321aed035abSart 		prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
322aed035abSart 		    sizeof bootinfo.boot_flags);
323aed035abSart 		prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
324aed035abSart 		    sizeof bootinfo.booted_kernel);
325aed035abSart 		prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
326aed035abSart 		    sizeof bootinfo.booted_dev);
327aed035abSart 	}
328aed035abSart 
329aed035abSart 	esym = (caddr_t)bootinfo.esym;
330aed035abSart 	/*
331aed035abSart 	 * Initialize the kernel's mapping of the RPB.  It's needed for
332aed035abSart 	 * lots of things.
333aed035abSart 	 */
334aed035abSart 	hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
335aed035abSart 
336aed035abSart #if defined(DEC_3000_300) || defined(DEC_3000_500)
337aed035abSart 	if (hwrpb->rpb_type == ST_DEC_3000_300 ||
338aed035abSart 	    hwrpb->rpb_type == ST_DEC_3000_500) {
339aed035abSart 		prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
340aed035abSart 		    sizeof(dec_3000_scsiid));
341aed035abSart 		prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
342aed035abSart 		    sizeof(dec_3000_scsifast));
343aed035abSart 	}
344aed035abSart #endif
345df930be7Sderaadt 
346df930be7Sderaadt 	/*
347df930be7Sderaadt 	 * Remember how many cycles there are per microsecond,
348df930be7Sderaadt 	 * so that we can use delay().  Round up, for safety.
349df930be7Sderaadt 	 */
350df930be7Sderaadt 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
351df930be7Sderaadt 
352df930be7Sderaadt 	/*
3539e71c994Saaron 	 * Initialize the (temporary) bootstrap console interface, so
354aed035abSart 	 * we can use printf until the VM system starts being setup.
355aed035abSart 	 * The real console is initialized before then.
356df930be7Sderaadt 	 */
357aed035abSart 	init_bootstrap_console();
358aed035abSart 
359aed035abSart 	/* OUTPUT NOW ALLOWED */
360aed035abSart 
361aed035abSart 	/* delayed from above */
362aed035abSart 	if (bootinfo_msg)
363aed035abSart 		printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
364aed035abSart 		    bootinfo_msg, bim, bip, biv);
365aed035abSart 
366aed035abSart 	/* Initialize the trap vectors on the primary processor. */
367aed035abSart 	trap_init();
368df930be7Sderaadt 
369df930be7Sderaadt 	/*
370aed035abSart 	 * Find out what hardware we're on, and do basic initialization.
371df930be7Sderaadt 	 */
372aed035abSart 	cputype = hwrpb->rpb_type;
373aed035abSart 	if (cputype < 0) {
374aed035abSart 		/*
375aed035abSart 		 * At least some white-box systems have SRM which
376aed035abSart 		 * reports a systype that's the negative of their
377aed035abSart 		 * blue-box counterpart.
378aed035abSart 		 */
379aed035abSart 		cputype = -cputype;
380aed035abSart 	}
381aed035abSart 	c = platform_lookup(cputype);
382aed035abSart 	if (c == NULL) {
383aed035abSart 		platform_not_supported();
384aed035abSart 		/* NOTREACHED */
385aed035abSart 	}
386aed035abSart 	(*c->init)();
387094fa01fSderaadt 	strlcpy(cpu_model, platform.model, sizeof cpu_model);
38850ce9ee0Sniklas 
38950ce9ee0Sniklas 	/*
3909e71c994Saaron 	 * Initialize the real console, so that the bootstrap console is
391aed035abSart 	 * no longer necessary.
39250ce9ee0Sniklas 	 */
393aed035abSart 	(*platform.cons_init)();
394aed035abSart 
395a55851f4Sderaadt #if 0
396aed035abSart 	/* Paranoid sanity checking */
397aed035abSart 
398aed035abSart 	assert(hwrpb->rpb_primary_cpu_id == alpha_pal_whami());
399aed035abSart 
400aed035abSart 	/*
401aed035abSart 	 * On single-CPU systypes, the primary should always be CPU 0,
402aed035abSart 	 * except on Alpha 8200 systems where the CPU id is related
403aed035abSart 	 * to the VID, which is related to the Turbo Laser node id.
404aed035abSart 	 */
405aed035abSart 	if (cputype != ST_DEC_21000)
406aed035abSart 		assert(hwrpb->rpb_primary_cpu_id == 0);
407aed035abSart #endif
408aed035abSart 
409aed035abSart 	/* NO MORE FIRMWARE ACCESS ALLOWED */
410aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
411aed035abSart 	/*
412aed035abSart 	 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
413aed035abSart 	 * XXX pmap_uses_prom_console() evaluates to non-zero.)
414aed035abSart 	 */
415aed035abSart #endif
416aed035abSart 
417cfcdef40Smiod #ifndef SMALL_KERNEL
418cfcdef40Smiod 	/*
419cfcdef40Smiod 	 * If we run on a BWX-capable processor, override cpu_switch
420cfcdef40Smiod 	 * with a faster version.
421cfcdef40Smiod 	 * We do this now because the kernel text might be mapped
422cfcdef40Smiod 	 * read-only eventually (although this is not the case at the moment).
423cfcdef40Smiod 	 */
424cfcdef40Smiod 	if (alpha_implver() >= ALPHA_IMPLVER_EV5) {
425cfcdef40Smiod 		if (~alpha_amask(ALPHA_AMASK_BWX) != 0) {
426cfcdef40Smiod 			extern vaddr_t __bwx_switch0, __bwx_switch1,
427cfcdef40Smiod 			    __bwx_switch2, __bwx_switch3;
428cfcdef40Smiod 			u_int32_t *dst, *src, *end;
429cfcdef40Smiod 
430cfcdef40Smiod 			src = (u_int32_t *)&__bwx_switch2;
431cfcdef40Smiod 			end = (u_int32_t *)&__bwx_switch3;
432cfcdef40Smiod 			dst = (u_int32_t *)&__bwx_switch0;
433cfcdef40Smiod 			while (src != end)
434cfcdef40Smiod 				*dst++ = *src++;
435cfcdef40Smiod 			src = (u_int32_t *)&__bwx_switch1;
436cfcdef40Smiod 			end = (u_int32_t *)&__bwx_switch2;
437cfcdef40Smiod 			while (src != end)
438cfcdef40Smiod 				*dst++ = *src++;
439cfcdef40Smiod 		}
440cfcdef40Smiod 	}
441cfcdef40Smiod #endif
442cfcdef40Smiod 
443aed035abSart 	/*
444aed035abSart 	 * find out this system's page size
445aed035abSart 	 */
44673b9fe7cSart 	if ((uvmexp.pagesize = hwrpb->rpb_page_size) != 8192)
44773b9fe7cSart 		panic("page size %d != 8192?!", uvmexp.pagesize);
448aed035abSart 
449aed035abSart 	uvm_setpagesize();
450aed035abSart 
451aed035abSart 	/*
452aed035abSart 	 * Find the beginning and end of the kernel (and leave a
453aed035abSart 	 * bit of space before the beginning for the bootstrap
454aed035abSart 	 * stack).
455aed035abSart 	 */
456aed035abSart 	kernstart = trunc_page((vaddr_t)kernel_text) - 2 * PAGE_SIZE;
457aed035abSart 	kernend = (vaddr_t)round_page((vaddr_t)bootinfo.esym);
458aed035abSart 
459aed035abSart 	kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
460aed035abSart 	kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
461df930be7Sderaadt 
462df930be7Sderaadt 	/*
463df930be7Sderaadt 	 * Find out how much memory is available, by looking at
464df930be7Sderaadt 	 * the memory cluster descriptors.  This also tries to do
465df930be7Sderaadt 	 * its best to detect things things that have never been seen
466df930be7Sderaadt 	 * before...
467df930be7Sderaadt 	 */
468df930be7Sderaadt 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
469df930be7Sderaadt 
470aed035abSart 	/* MDDT SANITY CHECKING */
471df930be7Sderaadt 	mddtweird = 0;
472aed035abSart 	if (mddtp->mddt_cluster_cnt < 2) {
473df930be7Sderaadt 		mddtweird = 1;
474aed035abSart 		printf("WARNING: weird number of mem clusters: %lu\n",
475aed035abSart 		    mddtp->mddt_cluster_cnt);
476df930be7Sderaadt 	}
477df930be7Sderaadt 
478aed035abSart #if 0
479aed035abSart 	printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
480aed035abSart #endif
481df930be7Sderaadt 
482aed035abSart 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
483aed035abSart 		memc = &mddtp->mddt_clusters[i];
484aed035abSart #if 0
485aed035abSart 		printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
486aed035abSart 		    memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
487aed035abSart #endif
488aed035abSart 		totalphysmem += memc->mddt_pg_cnt;
489aed035abSart 		if (mem_cluster_cnt < VM_PHYSSEG_MAX) {	/* XXX */
490aed035abSart 			mem_clusters[mem_cluster_cnt].start =
491aed035abSart 			    ptoa(memc->mddt_pfn);
492aed035abSart 			mem_clusters[mem_cluster_cnt].size =
493aed035abSart 			    ptoa(memc->mddt_pg_cnt);
494aed035abSart 			if (memc->mddt_usage & MDDT_mbz ||
495aed035abSart 			    memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
496aed035abSart 			    memc->mddt_usage & MDDT_PALCODE)
497aed035abSart 				mem_clusters[mem_cluster_cnt].size |=
498aed035abSart 				    VM_PROT_READ;
499aed035abSart 			else
500aed035abSart 				mem_clusters[mem_cluster_cnt].size |=
501aed035abSart 				    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
502aed035abSart 			mem_cluster_cnt++;
503aed035abSart 		}
504aed035abSart 
505aed035abSart 		if (memc->mddt_usage & MDDT_mbz) {
506aed035abSart 			mddtweird = 1;
507aed035abSart 			printf("WARNING: mem cluster %d has weird "
508aed035abSart 			    "usage 0x%lx\n", i, memc->mddt_usage);
509aed035abSart 			unknownmem += memc->mddt_pg_cnt;
510aed035abSart 			continue;
511aed035abSart 		}
512aed035abSart 		if (memc->mddt_usage & MDDT_NONVOLATILE) {
513aed035abSart 			/* XXX should handle these... */
514aed035abSart 			printf("WARNING: skipping non-volatile mem "
515aed035abSart 			    "cluster %d\n", i);
516aed035abSart 			unusedmem += memc->mddt_pg_cnt;
517aed035abSart 			continue;
518aed035abSart 		}
519aed035abSart 		if (memc->mddt_usage & MDDT_PALCODE) {
520aed035abSart 			resvmem += memc->mddt_pg_cnt;
521aed035abSart 			continue;
522aed035abSart 		}
523aed035abSart 
524aed035abSart 		/*
525aed035abSart 		 * We have a memory cluster available for system
526aed035abSart 		 * software use.  We must determine if this cluster
527aed035abSart 		 * holds the kernel.
528aed035abSart 		 */
529aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
530aed035abSart 		/*
531aed035abSart 		 * XXX If the kernel uses the PROM console, we only use the
532aed035abSart 		 * XXX memory after the kernel in the first system segment,
533aed035abSart 		 * XXX to avoid clobbering prom mapping, data, etc.
534aed035abSart 		 */
535aed035abSart 	    if (!pmap_uses_prom_console() || physmem == 0) {
536aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
537aed035abSart 		physmem += memc->mddt_pg_cnt;
538aed035abSart 		pfn0 = memc->mddt_pfn;
539aed035abSart 		pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
540aed035abSart 		if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
541aed035abSart 			/*
542aed035abSart 			 * Must compute the location of the kernel
543aed035abSart 			 * within the segment.
544aed035abSart 			 */
545aed035abSart #if 0
546aed035abSart 			printf("Cluster %d contains kernel\n", i);
547aed035abSart #endif
548aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
549aed035abSart 		    if (!pmap_uses_prom_console()) {
550aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
551aed035abSart 			if (pfn0 < kernstartpfn) {
552aed035abSart 				/*
553aed035abSart 				 * There is a chunk before the kernel.
554aed035abSart 				 */
555aed035abSart #if 0
556aed035abSart 				printf("Loading chunk before kernel: "
557aed035abSart 				    "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
558aed035abSart #endif
559aed035abSart 				uvm_page_physload(pfn0, kernstartpfn,
560aed035abSart 				    pfn0, kernstartpfn, VM_FREELIST_DEFAULT);
561aed035abSart 			}
562aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
563aed035abSart 		    }
564aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
565aed035abSart 			if (kernendpfn < pfn1) {
566aed035abSart 				/*
567aed035abSart 				 * There is a chunk after the kernel.
568aed035abSart 				 */
569aed035abSart #if 0
570aed035abSart 				printf("Loading chunk after kernel: "
571aed035abSart 				    "0x%lx / 0x%lx\n", kernendpfn, pfn1);
572aed035abSart #endif
573aed035abSart 				uvm_page_physload(kernendpfn, pfn1,
574aed035abSart 				    kernendpfn, pfn1, VM_FREELIST_DEFAULT);
575aed035abSart 			}
576aed035abSart 		} else {
577aed035abSart 			/*
578aed035abSart 			 * Just load this cluster as one chunk.
579aed035abSart 			 */
580aed035abSart #if 0
581aed035abSart 			printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
582aed035abSart 			    pfn0, pfn1);
583aed035abSart #endif
584aed035abSart 			uvm_page_physload(pfn0, pfn1, pfn0, pfn1,
585aed035abSart 			    VM_FREELIST_DEFAULT);
586aed035abSart 		}
587aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
588aed035abSart 	    }
589aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
590aed035abSart 	}
591aed035abSart 
592a37778bcSderaadt #ifdef DEBUG
593aed035abSart 	/*
594aed035abSart 	 * Dump out the MDDT if it looks odd...
595aed035abSart 	 */
596df930be7Sderaadt 	if (mddtweird) {
597df930be7Sderaadt 		printf("\n");
598df930be7Sderaadt 		printf("complete memory cluster information:\n");
599df930be7Sderaadt 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
600df930be7Sderaadt 			printf("mddt %d:\n", i);
601df930be7Sderaadt 			printf("\tpfn %lx\n",
602df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pfn);
603df930be7Sderaadt 			printf("\tcnt %lx\n",
604df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
605df930be7Sderaadt 			printf("\ttest %lx\n",
606df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pg_test);
607df930be7Sderaadt 			printf("\tbva %lx\n",
608df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
609df930be7Sderaadt 			printf("\tbpa %lx\n",
610df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
611df930be7Sderaadt 			printf("\tbcksum %lx\n",
612df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
613df930be7Sderaadt 			printf("\tusage %lx\n",
614df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_usage);
615df930be7Sderaadt 		}
616df930be7Sderaadt 		printf("\n");
617df930be7Sderaadt 	}
618a37778bcSderaadt #endif
619df930be7Sderaadt 
620df930be7Sderaadt 	if (totalphysmem == 0)
621df930be7Sderaadt 		panic("can't happen: system seems to have no memory!");
622df930be7Sderaadt #if 0
623df930be7Sderaadt 	printf("totalphysmem = %d\n", totalphysmem);
624df930be7Sderaadt 	printf("physmem = %d\n", physmem);
625df930be7Sderaadt 	printf("resvmem = %d\n", resvmem);
626df930be7Sderaadt 	printf("unusedmem = %d\n", unusedmem);
627df930be7Sderaadt 	printf("unknownmem = %d\n", unknownmem);
628df930be7Sderaadt #endif
629df930be7Sderaadt 
630df930be7Sderaadt 	/*
631aed035abSart 	 * Initialize error message buffer (at end of core).
632df930be7Sderaadt 	 */
633aed035abSart 	{
634aed035abSart 		vsize_t sz = (vsize_t)round_page(MSGBUFSIZE);
635aed035abSart 		vsize_t reqsz = sz;
636df930be7Sderaadt 
637aed035abSart 		vps = &vm_physmem[vm_nphysseg - 1];
638e1da84e1Salex 
639aed035abSart 		/* shrink so that it'll fit in the last segment */
640aed035abSart 		if ((vps->avail_end - vps->avail_start) < atop(sz))
641aed035abSart 			sz = ptoa(vps->avail_end - vps->avail_start);
642aed035abSart 
643aed035abSart 		vps->end -= atop(sz);
644aed035abSart 		vps->avail_end -= atop(sz);
645aed035abSart 		initmsgbuf((caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end)), sz);
646aed035abSart 
647aed035abSart 		/* Remove the last segment if it now has no pages. */
648aed035abSart 		if (vps->start == vps->end)
649aed035abSart 			vm_nphysseg--;
650aed035abSart 
651aed035abSart 		/* warn if the message buffer had to be shrunk */
652aed035abSart 		if (sz != reqsz)
653aed035abSart 			printf("WARNING: %ld bytes not available for msgbuf "
654aed035abSart 			    "in last cluster (%ld used)\n", reqsz, sz);
655aed035abSart 
656aed035abSart 	}
657aed035abSart 
658df930be7Sderaadt 	/*
659df930be7Sderaadt 	 * Init mapping for u page(s) for proc 0
660df930be7Sderaadt 	 */
661aed035abSart 	proc0.p_addr = proc0paddr =
662aed035abSart 	    (struct user *)pmap_steal_memory(UPAGES * PAGE_SIZE, NULL, NULL);
663df930be7Sderaadt 
664df930be7Sderaadt 	/*
665aed035abSart 	 * Allocate space for system data structures.  These data structures
666aed035abSart 	 * are allocated here instead of cpu_startup() because physical
667aed035abSart 	 * memory is directly addressable.  We don't have to map these into
668aed035abSart 	 * virtual address space.
669df930be7Sderaadt 	 */
670aed035abSart 	size = (vsize_t)allocsys(NULL);
671aed035abSart 	v = (caddr_t)pmap_steal_memory(size, NULL, NULL);
672aed035abSart 	if ((allocsys(v) - v) != size)
673aed035abSart 		panic("alpha_init: table size inconsistency");
674df930be7Sderaadt 
675df930be7Sderaadt 	/*
676df930be7Sderaadt 	 * Clear allocated memory.
677df930be7Sderaadt 	 */
678aed035abSart 	bzero(v, size);
679df930be7Sderaadt 
680df930be7Sderaadt 	/*
681df930be7Sderaadt 	 * Initialize the virtual memory system, and set the
682df930be7Sderaadt 	 * page table base register in proc 0's PCB.
683df930be7Sderaadt 	 */
684aed035abSart 	pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
685aed035abSart 	    hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
686df930be7Sderaadt 
687df930be7Sderaadt 	/*
688df930be7Sderaadt 	 * Initialize the rest of proc 0's PCB, and cache its physical
689df930be7Sderaadt 	 * address.
690df930be7Sderaadt 	 */
691df930be7Sderaadt 	proc0.p_md.md_pcbpaddr =
692aed035abSart 	    (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
693df930be7Sderaadt 
694df930be7Sderaadt 	/*
695df930be7Sderaadt 	 * Set the kernel sp, reserving space for an (empty) trapframe,
696df930be7Sderaadt 	 * and make proc0's trapframe pointer point to it for sanity.
697df930be7Sderaadt 	 */
69850ce9ee0Sniklas 	proc0paddr->u_pcb.pcb_hw.apcb_ksp =
699df930be7Sderaadt 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
70074652a67Sniklas 	proc0.p_md.md_tf =
70174652a67Sniklas 	    (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
70250ce9ee0Sniklas 
703aed035abSart 	/*
704aed035abSart 	 * Initialize the primary CPU's idle PCB to proc0's.  In a
705aed035abSart 	 * MULTIPROCESSOR configuration, each CPU will later get
706aed035abSart 	 * its own idle PCB when autoconfiguration runs.
707aed035abSart 	 */
708aed035abSart 	ci->ci_idle_pcb = &proc0paddr->u_pcb;
709aed035abSart 	ci->ci_idle_pcb_paddr = (u_long)proc0.p_md.md_pcbpaddr;
710df930be7Sderaadt 
711df930be7Sderaadt 	/*
712df930be7Sderaadt 	 * Look at arguments passed to us and compute boothowto.
713df930be7Sderaadt 	 */
714417eba8cSderaadt 
715df930be7Sderaadt #ifdef KADB
716df930be7Sderaadt 	boothowto |= RB_KDB;
717df930be7Sderaadt #endif
718aed035abSart 	for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
719417eba8cSderaadt 		/*
720417eba8cSderaadt 		 * Note that we'd really like to differentiate case here,
721417eba8cSderaadt 		 * but the Alpha AXP Architecture Reference Manual
722417eba8cSderaadt 		 * says that we shouldn't.
723417eba8cSderaadt 		 */
724df930be7Sderaadt 		switch (*p) {
725371c77f5Smartin 		case 'a': /* Ignore */
726417eba8cSderaadt 		case 'A':
727df930be7Sderaadt 			break;
728df930be7Sderaadt 
72912f8bbedSniklas 		case 'b': /* Enter DDB as soon as the console is initialised */
73012f8bbedSniklas 		case 'B':
73112f8bbedSniklas 			boothowto |= RB_KDB;
73212f8bbedSniklas 			break;
73312f8bbedSniklas 
73450ce9ee0Sniklas 		case 'c': /* enter user kernel configuration */
73550ce9ee0Sniklas 		case 'C':
73650ce9ee0Sniklas 			boothowto |= RB_CONFIG;
73750ce9ee0Sniklas 			break;
73850ce9ee0Sniklas 
73950ce9ee0Sniklas #ifdef DEBUG
74050ce9ee0Sniklas 		case 'd': /* crash dump immediately after autoconfig */
74150ce9ee0Sniklas 		case 'D':
74250ce9ee0Sniklas 			boothowto |= RB_DUMP;
74350ce9ee0Sniklas 			break;
74450ce9ee0Sniklas #endif
74550ce9ee0Sniklas 
74650ce9ee0Sniklas 		case 'h': /* always halt, never reboot */
74750ce9ee0Sniklas 		case 'H':
74850ce9ee0Sniklas 			boothowto |= RB_HALT;
749df930be7Sderaadt 			break;
750df930be7Sderaadt 
751417eba8cSderaadt #if 0
752417eba8cSderaadt 		case 'm': /* mini root present in memory */
753417eba8cSderaadt 		case 'M':
754417eba8cSderaadt 			boothowto |= RB_MINIROOT;
755417eba8cSderaadt 			break;
756417eba8cSderaadt #endif
75750ce9ee0Sniklas 
75850ce9ee0Sniklas 		case 'n': /* askname */
75950ce9ee0Sniklas 		case 'N':
76050ce9ee0Sniklas 			boothowto |= RB_ASKNAME;
76150ce9ee0Sniklas 			break;
762aed035abSart 
763371c77f5Smartin 		case 's': /* single-user */
764aed035abSart 		case 'S':
765aed035abSart 			boothowto |= RB_SINGLE;
766aed035abSart 			break;
767aed035abSart 
768aed035abSart 		case '-':
769aed035abSart 			/*
770aed035abSart 			 * Just ignore this.  It's not required, but it's
771aed035abSart 			 * common for it to be passed regardless.
772aed035abSart 			 */
773aed035abSart 			break;
774aed035abSart 
775aed035abSart 		default:
776aed035abSart 			printf("Unrecognized boot flag '%c'.\n", *p);
777aed035abSart 			break;
778df930be7Sderaadt 		}
779df930be7Sderaadt 	}
780df930be7Sderaadt 
781aed035abSart 
782df930be7Sderaadt 	/*
783df930be7Sderaadt 	 * Figure out the number of cpus in the box, from RPB fields.
784df930be7Sderaadt 	 * Really.  We mean it.
785df930be7Sderaadt 	 */
7862586fa93Smiod 	for (alpha_cpus = 0, i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
787df930be7Sderaadt 		struct pcs *pcsp;
788df930be7Sderaadt 
789aed035abSart 		pcsp = LOCATE_PCS(hwrpb, i);
790df930be7Sderaadt 		if ((pcsp->pcs_flags & PCS_PP) != 0)
7912586fa93Smiod 			alpha_cpus++;
792df930be7Sderaadt 	}
793aed035abSart 
794aed035abSart 	/*
795aed035abSart 	 * Initialize debuggers, and break into them if appropriate.
796aed035abSart 	 */
797aed035abSart #ifdef DDB
798aed035abSart 	ddb_init();
799aed035abSart 
800aed035abSart 	if (boothowto & RB_KDB)
801aed035abSart 		Debugger();
802aed035abSart #endif
803aed035abSart #ifdef KGDB
804aed035abSart 	if (boothowto & RB_KDB)
805aed035abSart 		kgdb_connect(0);
806aed035abSart #endif
807aed035abSart 	/*
808aed035abSart 	 * Figure out our clock frequency, from RPB fields.
809aed035abSart 	 */
810aed035abSart 	hz = hwrpb->rpb_intr_freq >> 12;
811aed035abSart 	if (!(60 <= hz && hz <= 10240)) {
812aed035abSart 		hz = 1024;
813aed035abSart #ifdef DIAGNOSTIC
814aed035abSart 		printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
815aed035abSart 			hwrpb->rpb_intr_freq, hz);
816aed035abSart #endif
817aed035abSart 	}
818aed035abSart }
819aed035abSart 
820aed035abSart caddr_t
821aed035abSart allocsys(v)
822aed035abSart 	caddr_t v;
823aed035abSart {
824aed035abSart 	/*
825aed035abSart 	 * Allocate space for system data structures.
826aed035abSart 	 * The first available kernel virtual address is in "v".
827aed035abSart 	 * As pages of kernel virtual memory are allocated, "v" is incremented.
828aed035abSart 	 *
829aed035abSart 	 * These data structures are allocated here instead of cpu_startup()
830aed035abSart 	 * because physical memory is directly addressable. We don't have
831aed035abSart 	 * to map these into virtual address space.
832aed035abSart 	 */
833aed035abSart #define valloc(name, type, num) \
834aed035abSart 	    (name) = (type *)v; v = (caddr_t)ALIGN((name)+(num))
835aed035abSart 
836aed035abSart #ifdef SYSVMSG
837aed035abSart 	valloc(msgpool, char, msginfo.msgmax);
838aed035abSart 	valloc(msgmaps, struct msgmap, msginfo.msgseg);
839aed035abSart 	valloc(msghdrs, struct msg, msginfo.msgtql);
840aed035abSart 	valloc(msqids, struct msqid_ds, msginfo.msgmni);
841aed035abSart #endif
842aed035abSart 
843aed035abSart 	/*
844aed035abSart 	 * Determine how many buffers to allocate.
845aed035abSart 	 * We allocate 10% of memory for buffer space.  Insure a
846aed035abSart 	 * minimum of 16 buffers.
847aed035abSart 	 */
848aed035abSart 	if (bufpages == 0)
84960535ec9Smaja 		bufpages = (physmem / (100/bufcachepercent));
850aed035abSart 	if (nbuf == 0) {
851aed035abSart 		nbuf = bufpages;
852aed035abSart 		if (nbuf < 16)
853aed035abSart 			nbuf = 16;
854aed035abSart 	}
855aed035abSart 	valloc(buf, struct buf, nbuf);
856aed035abSart 
857aed035abSart #undef valloc
858aed035abSart 
859aed035abSart 	return v;
860df930be7Sderaadt }
861df930be7Sderaadt 
862417eba8cSderaadt void
863df930be7Sderaadt consinit()
864df930be7Sderaadt {
865aed035abSart 
866aed035abSart 	/*
867aed035abSart 	 * Everything related to console initialization is done
868aed035abSart 	 * in alpha_init().
869aed035abSart 	 */
870aed035abSart #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
871aed035abSart 	printf("consinit: %susing prom console\n",
872aed035abSart 	    pmap_uses_prom_console() ? "" : "not ");
87312f8bbedSniklas #endif
874df930be7Sderaadt }
875df930be7Sderaadt 
876417eba8cSderaadt void
877df930be7Sderaadt cpu_startup()
878df930be7Sderaadt {
879df930be7Sderaadt 	register unsigned i;
880df930be7Sderaadt 	int base, residual;
881aed035abSart 	vaddr_t minaddr, maxaddr;
882aed035abSart 	vsize_t size;
88350ce9ee0Sniklas #if defined(DEBUG)
884df930be7Sderaadt 	extern int pmapdebug;
885df930be7Sderaadt 	int opmapdebug = pmapdebug;
886df930be7Sderaadt 
887df930be7Sderaadt 	pmapdebug = 0;
888df930be7Sderaadt #endif
889df930be7Sderaadt 
890df930be7Sderaadt 	/*
891df930be7Sderaadt 	 * Good {morning,afternoon,evening,night}.
892df930be7Sderaadt 	 */
893df930be7Sderaadt 	printf(version);
894df930be7Sderaadt 	identifycpu();
8953f4ce3b7Smiod 	printf("total memory = %ld (%ldK)\n", (long)ptoa(totalphysmem),
8963f4ce3b7Smiod 	    (long)ptoa(totalphysmem) / 1024);
8973f4ce3b7Smiod 	printf("(%ld reserved for PROM, ", (long)ptoa(resvmem));
8983f4ce3b7Smiod 	printf("%ld used by OpenBSD)\n", (long)ptoa(physmem));
899aed035abSart 	if (unusedmem) {
9003f4ce3b7Smiod 		printf("WARNING: unused memory = %ld (%ldK)\n",
9013f4ce3b7Smiod 		    (long)ptoa(unusedmem), (long)ptoa(unusedmem) / 1024);
902aed035abSart 	}
903aed035abSart 	if (unknownmem) {
9043f4ce3b7Smiod 		printf("WARNING: %ld (%ldK) of memory with unknown purpose\n",
9053f4ce3b7Smiod 		    (long)ptoa(unknownmem), (long)ptoa(unknownmem) / 1024);
906aed035abSart 	}
907df930be7Sderaadt 
908df930be7Sderaadt 	/*
909df930be7Sderaadt 	 * Allocate virtual address space for file I/O buffers.
910df930be7Sderaadt 	 * Note they are different than the array of headers, 'buf',
911df930be7Sderaadt 	 * and usually occupy more virtual memory than physical.
912df930be7Sderaadt 	 */
913df930be7Sderaadt 	size = MAXBSIZE * nbuf;
914aed035abSart 	if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
915198a4b3fSart 		    NULL, UVM_UNKNOWN_OFFSET, 0,
916aed035abSart 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
917738a5b4dSart 				UVM_ADV_NORMAL, 0)))
918aed035abSart 		panic("startup: cannot allocate VM for buffers");
919df930be7Sderaadt 	base = bufpages / nbuf;
920df930be7Sderaadt 	residual = bufpages % nbuf;
921df930be7Sderaadt 	for (i = 0; i < nbuf; i++) {
922aed035abSart 		vsize_t curbufsize;
923aed035abSart 		vaddr_t curbuf;
924aed035abSart 		struct vm_page *pg;
925df930be7Sderaadt 
926df930be7Sderaadt 		/*
927aed035abSart 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
928aed035abSart 		 * that MAXBSIZE space, we allocate and map (base+1) pages
929aed035abSart 		 * for the first "residual" buffers, and then we allocate
930aed035abSart 		 * "base" pages for the rest.
931df930be7Sderaadt 		 */
932aed035abSart 		curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
933194dd68bSbrad 		curbufsize = PAGE_SIZE * ((i < residual) ? (base+1) : base);
934aed035abSart 
935aed035abSart 		while (curbufsize) {
936aed035abSart 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
937aed035abSart 			if (pg == NULL)
938aed035abSart 				panic("cpu_startup: not enough memory for "
939aed035abSart 				    "buffer cache");
940aed035abSart 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
941aed035abSart 					VM_PROT_READ|VM_PROT_WRITE);
942aed035abSart 			curbuf += PAGE_SIZE;
943aed035abSart 			curbufsize -= PAGE_SIZE;
944aed035abSart 		}
9450e5798cfSart 		pmap_update(pmap_kernel());
946df930be7Sderaadt 	}
947df930be7Sderaadt 	/*
948df930be7Sderaadt 	 * Allocate a submap for exec arguments.  This map effectively
949df930be7Sderaadt 	 * limits the number of processes exec'ing at any time.
950df930be7Sderaadt 	 */
9517c10a71dSdrahn 	minaddr = vm_map_min(kernel_map);
952aed035abSart 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
953aed035abSart 	    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
954df930be7Sderaadt 
955df930be7Sderaadt 	/*
956df930be7Sderaadt 	 * Allocate a submap for physio
957df930be7Sderaadt 	 */
958aed035abSart 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
959aed035abSart 	    VM_PHYS_SIZE, 0, FALSE, NULL);
960df930be7Sderaadt 
96150ce9ee0Sniklas #if defined(DEBUG)
962df930be7Sderaadt 	pmapdebug = opmapdebug;
963df930be7Sderaadt #endif
9643f4ce3b7Smiod 	printf("avail memory = %ld (%ldK)\n", (long)ptoa(uvmexp.free),
9653f4ce3b7Smiod 	    (long)ptoa(uvmexp.free) / 1024);
966aed035abSart #if 0
967aed035abSart 	{
968aed035abSart 		extern u_long pmap_pages_stolen;
969aed035abSart 
970aed035abSart 		printf("stolen memory for VM structures = %d\n", pmap_pages_stolen * PAGE_SIZE);
971aed035abSart 	}
972aed035abSart #endif
9733f4ce3b7Smiod 	printf("using %ld buffers containing %ld bytes (%ldK) of memory\n",
974194dd68bSbrad 	    (long)nbuf, (long)bufpages * PAGE_SIZE, (long)bufpages * (PAGE_SIZE / 1024));
975df930be7Sderaadt 
976df930be7Sderaadt 	/*
977df930be7Sderaadt 	 * Set up buffers, so they can be used to read disk labels.
978df930be7Sderaadt 	 */
979df930be7Sderaadt 	bufinit();
980df930be7Sderaadt 
981df930be7Sderaadt 	/*
982df930be7Sderaadt 	 * Configure the system.
983df930be7Sderaadt 	 */
98441033391Sderaadt 	if (boothowto & RB_CONFIG) {
98541033391Sderaadt #ifdef BOOT_CONFIG
98641033391Sderaadt 		user_config();
98741033391Sderaadt #else
98841033391Sderaadt 		printf("kernel does not support -c; continuing..\n");
98941033391Sderaadt #endif
99041033391Sderaadt 	}
99150ce9ee0Sniklas 
99250ce9ee0Sniklas 	/*
993aed035abSart 	 * Set up the HWPCB so that it's safe to configure secondary
994aed035abSart 	 * CPUs.
99550ce9ee0Sniklas 	 */
996aed035abSart 	hwrpb_primary_init();
997aed035abSart }
998aed035abSart 
999aed035abSart /*
1000aed035abSart  * Retrieve the platform name from the DSR.
1001aed035abSart  */
1002aed035abSart const char *
1003aed035abSart alpha_dsr_sysname()
1004aed035abSart {
1005aed035abSart 	struct dsrdb *dsr;
1006aed035abSart 	const char *sysname;
1007aed035abSart 
1008aed035abSart 	/*
1009aed035abSart 	 * DSR does not exist on early HWRPB versions.
1010aed035abSart 	 */
1011aed035abSart 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
1012aed035abSart 		return (NULL);
1013aed035abSart 
1014aed035abSart 	dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
1015aed035abSart 	sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
1016aed035abSart 	    sizeof(u_int64_t)));
1017aed035abSart 	return (sysname);
1018aed035abSart }
1019aed035abSart 
1020aed035abSart /*
1021aed035abSart  * Lookup the system specified system variation in the provided table,
1022aed035abSart  * returning the model string on match.
1023aed035abSart  */
1024aed035abSart const char *
1025aed035abSart alpha_variation_name(variation, avtp)
1026aed035abSart 	u_int64_t variation;
1027aed035abSart 	const struct alpha_variation_table *avtp;
1028aed035abSart {
1029aed035abSart 	int i;
1030aed035abSart 
1031aed035abSart 	for (i = 0; avtp[i].avt_model != NULL; i++)
1032aed035abSart 		if (avtp[i].avt_variation == variation)
1033aed035abSart 			return (avtp[i].avt_model);
1034aed035abSart 	return (NULL);
1035aed035abSart }
1036aed035abSart 
1037aed035abSart /*
1038aed035abSart  * Generate a default platform name based for unknown system variations.
1039aed035abSart  */
1040aed035abSart const char *
1041aed035abSart alpha_unknown_sysname()
1042aed035abSart {
1043aed035abSart 	static char s[128];		/* safe size */
1044aed035abSart 
1045d5eb2d9aSderaadt 	snprintf(s, sizeof s, "%s family, unknown model variation 0x%lx",
1046aed035abSart 	    platform.family, hwrpb->rpb_variation & SV_ST_MASK);
1047aed035abSart 	return ((const char *)s);
1048df930be7Sderaadt }
1049df930be7Sderaadt 
105050ce9ee0Sniklas void
1051df930be7Sderaadt identifycpu()
1052df930be7Sderaadt {
1053aed035abSart 	char *s;
1054df930be7Sderaadt 
1055df930be7Sderaadt 	/*
1056df930be7Sderaadt 	 * print out CPU identification information.
1057df930be7Sderaadt 	 */
1058aed035abSart 	printf("%s", cpu_model);
1059aed035abSart 	for(s = cpu_model; *s; ++s)
1060aed035abSart 		if(strncasecmp(s, "MHz", 3) == 0)
1061aed035abSart 			goto skipMHz;
1062aed035abSart 	printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
1063aed035abSart skipMHz:
1064aed035abSart 	printf("\n");
106550ce9ee0Sniklas 	printf("%ld byte page size, %d processor%s.\n",
10662586fa93Smiod 	    hwrpb->rpb_page_size, alpha_cpus, alpha_cpus == 1 ? "" : "s");
1067df930be7Sderaadt #if 0
1068df930be7Sderaadt 	/* this isn't defined for any systems that we run on? */
1069df930be7Sderaadt 	printf("serial number 0x%lx 0x%lx\n",
1070df930be7Sderaadt 	    ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
1071df930be7Sderaadt 
1072df930be7Sderaadt 	/* and these aren't particularly useful! */
1073df930be7Sderaadt 	printf("variation: 0x%lx, revision 0x%lx\n",
1074df930be7Sderaadt 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
1075df930be7Sderaadt #endif
1076df930be7Sderaadt }
1077df930be7Sderaadt 
1078df930be7Sderaadt int	waittime = -1;
1079df930be7Sderaadt struct pcb dumppcb;
1080df930be7Sderaadt 
1081417eba8cSderaadt void
1082aed035abSart boot(howto)
1083df930be7Sderaadt 	int howto;
1084df930be7Sderaadt {
1085aed035abSart #if defined(MULTIPROCESSOR)
1086aed035abSart #if 0 /* XXX See below. */
1087aed035abSart 	u_long cpu_id;
1088aed035abSart #endif
1089aed035abSart #endif
1090aed035abSart 
1091aed035abSart #if defined(MULTIPROCESSOR)
1092aed035abSart 	/* We must be running on the primary CPU. */
1093aed035abSart 	if (alpha_pal_whami() != hwrpb->rpb_primary_cpu_id)
1094aed035abSart 		panic("cpu_reboot: not on primary CPU!");
1095aed035abSart #endif
1096aed035abSart 
1097df930be7Sderaadt 	/* If system is cold, just halt. */
1098df930be7Sderaadt 	if (cold) {
1099c9ad5066Stom 		/* (Unless the user explicitly asked for reboot.) */
1100c9ad5066Stom 		if ((howto & RB_USERREQ) == 0)
1101df930be7Sderaadt 			howto |= RB_HALT;
1102df930be7Sderaadt 		goto haltsys;
1103df930be7Sderaadt 	}
1104df930be7Sderaadt 
110550ce9ee0Sniklas 	/* If "always halt" was specified as a boot flag, obey. */
110650ce9ee0Sniklas 	if ((boothowto & RB_HALT) != 0)
110750ce9ee0Sniklas 		howto |= RB_HALT;
110850ce9ee0Sniklas 
1109df930be7Sderaadt 	boothowto = howto;
1110df930be7Sderaadt 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
1111df930be7Sderaadt 		waittime = 0;
1112df930be7Sderaadt 		vfs_shutdown();
1113df930be7Sderaadt 		/*
1114df930be7Sderaadt 		 * If we've been adjusting the clock, the todr
11152417125dSmiod 		 * will be out of synch; adjust it now unless
11162417125dSmiod 		 * the system has been sitting in ddb.
1117df930be7Sderaadt 		 */
11182417125dSmiod 		if ((howto & RB_TIMEBAD) == 0) {
1119df930be7Sderaadt 			resettodr();
11202417125dSmiod 		} else {
11212417125dSmiod 			printf("WARNING: not updating battery clock\n");
11222417125dSmiod 		}
1123df930be7Sderaadt 	}
1124df930be7Sderaadt 
1125df930be7Sderaadt 	/* Disable interrupts. */
1126df930be7Sderaadt 	splhigh();
1127df930be7Sderaadt 
1128df930be7Sderaadt 	/* If rebooting and a dump is requested do it. */
112950ce9ee0Sniklas 	if (howto & RB_DUMP)
1130df930be7Sderaadt 		dumpsys();
1131df930be7Sderaadt 
113234fbf6deSderaadt haltsys:
113334fbf6deSderaadt 
1134df930be7Sderaadt 	/* run any shutdown hooks */
1135df930be7Sderaadt 	doshutdownhooks();
1136df930be7Sderaadt 
1137aed035abSart #if defined(MULTIPROCESSOR)
1138aed035abSart #if 0 /* XXX doesn't work when called from here?! */
1139aed035abSart 	/* Kill off any secondary CPUs. */
1140aed035abSart 	for (cpu_id = 0; cpu_id < hwrpb->rpb_pcs_cnt; cpu_id++) {
1141aed035abSart 		if (cpu_id == hwrpb->rpb_primary_cpu_id ||
1142aed035abSart 		    cpu_info[cpu_id].ci_softc == NULL)
1143aed035abSart 			continue;
1144aed035abSart 		cpu_halt_secondary(cpu_id);
1145aed035abSart 	}
1146aed035abSart #endif
1147aed035abSart #endif
1148aed035abSart 
1149df930be7Sderaadt #ifdef BOOTKEY
1150df930be7Sderaadt 	printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
1151aed035abSart 	cnpollc(1);	/* for proper keyboard command handling */
1152df930be7Sderaadt 	cngetc();
1153aed035abSart 	cnpollc(0);
1154df930be7Sderaadt 	printf("\n");
1155df930be7Sderaadt #endif
1156df930be7Sderaadt 
1157aed035abSart 	/* Finally, powerdown/halt/reboot the system. */
1158aed035abSart 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN &&
1159aed035abSart 	    platform.powerdown != NULL) {
1160aed035abSart 		(*platform.powerdown)();
1161aed035abSart 		printf("WARNING: powerdown failed!\n");
1162aed035abSart 	}
1163df930be7Sderaadt 	printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
1164df930be7Sderaadt 	prom_halt(howto & RB_HALT);
1165df930be7Sderaadt 	/*NOTREACHED*/
1166df930be7Sderaadt }
1167df930be7Sderaadt 
1168df930be7Sderaadt /*
1169df930be7Sderaadt  * These variables are needed by /sbin/savecore
1170df930be7Sderaadt  */
1171df930be7Sderaadt u_long	dumpmag = 0x8fca0101;	/* magic number */
1172df930be7Sderaadt int 	dumpsize = 0;		/* pages */
1173df930be7Sderaadt long	dumplo = 0; 		/* blocks */
1174df930be7Sderaadt 
1175df930be7Sderaadt /*
117650ce9ee0Sniklas  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
117750ce9ee0Sniklas  */
117850ce9ee0Sniklas int
117950ce9ee0Sniklas cpu_dumpsize()
118050ce9ee0Sniklas {
118150ce9ee0Sniklas 	int size;
118250ce9ee0Sniklas 
1183aed035abSart 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1184aed035abSart 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
118550ce9ee0Sniklas 	if (roundup(size, dbtob(1)) != dbtob(1))
118650ce9ee0Sniklas 		return -1;
118750ce9ee0Sniklas 
118850ce9ee0Sniklas 	return (1);
118950ce9ee0Sniklas }
119050ce9ee0Sniklas 
119150ce9ee0Sniklas /*
1192aed035abSart  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1193aed035abSart  */
1194aed035abSart u_long
1195aed035abSart cpu_dump_mempagecnt()
1196aed035abSart {
1197aed035abSart 	u_long i, n;
1198aed035abSart 
1199aed035abSart 	n = 0;
1200aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++)
1201aed035abSart 		n += atop(mem_clusters[i].size);
1202aed035abSart 	return (n);
1203aed035abSart }
1204aed035abSart 
1205aed035abSart /*
120650ce9ee0Sniklas  * cpu_dump: dump machine-dependent kernel core dump headers.
120750ce9ee0Sniklas  */
120850ce9ee0Sniklas int
120950ce9ee0Sniklas cpu_dump()
121050ce9ee0Sniklas {
1211c4071fd1Smillert 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
1212aed035abSart 	char buf[dbtob(1)];
121350ce9ee0Sniklas 	kcore_seg_t *segp;
121450ce9ee0Sniklas 	cpu_kcore_hdr_t *cpuhdrp;
1215aed035abSart 	phys_ram_seg_t *memsegp;
1216aed035abSart 	int i;
121750ce9ee0Sniklas 
121850ce9ee0Sniklas 	dump = bdevsw[major(dumpdev)].d_dump;
121950ce9ee0Sniklas 
1220aed035abSart 	bzero(buf, sizeof buf);
122150ce9ee0Sniklas 	segp = (kcore_seg_t *)buf;
1222aed035abSart 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1223aed035abSart 	memsegp = (phys_ram_seg_t *)&buf[ALIGN(sizeof(*segp)) +
1224aed035abSart 	    ALIGN(sizeof(*cpuhdrp))];
122550ce9ee0Sniklas 
122650ce9ee0Sniklas 	/*
122750ce9ee0Sniklas 	 * Generate a segment header.
122850ce9ee0Sniklas 	 */
122950ce9ee0Sniklas 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
123050ce9ee0Sniklas 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
123150ce9ee0Sniklas 
123250ce9ee0Sniklas 	/*
1233aed035abSart 	 * Add the machine-dependent header info.
123450ce9ee0Sniklas 	 */
1235aed035abSart 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
123650ce9ee0Sniklas 	cpuhdrp->page_size = PAGE_SIZE;
1237aed035abSart 	cpuhdrp->nmemsegs = mem_cluster_cnt;
1238aed035abSart 
1239aed035abSart 	/*
1240aed035abSart 	 * Fill in the memory segment descriptors.
1241aed035abSart 	 */
1242aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++) {
1243aed035abSart 		memsegp[i].start = mem_clusters[i].start;
1244aed035abSart 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1245aed035abSart 	}
124650ce9ee0Sniklas 
124750ce9ee0Sniklas 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
124850ce9ee0Sniklas }
124950ce9ee0Sniklas 
125050ce9ee0Sniklas /*
1251aed035abSart  * This is called by main to set dumplo and dumpsize.
1252194dd68bSbrad  * Dumps always skip the first PAGE_SIZE of disk space
1253df930be7Sderaadt  * in case there might be a disk label stored there.
1254df930be7Sderaadt  * If there is extra space, put dump at the end to
1255df930be7Sderaadt  * reduce the chance that swapping trashes it.
1256df930be7Sderaadt  */
1257df930be7Sderaadt void
1258df930be7Sderaadt dumpconf()
1259df930be7Sderaadt {
126050ce9ee0Sniklas 	int nblks, dumpblks;	/* size of dump area */
1261df930be7Sderaadt 	int maj;
1262df930be7Sderaadt 
1263df930be7Sderaadt 	if (dumpdev == NODEV)
126450ce9ee0Sniklas 		goto bad;
1265df930be7Sderaadt 	maj = major(dumpdev);
1266df930be7Sderaadt 	if (maj < 0 || maj >= nblkdev)
1267df930be7Sderaadt 		panic("dumpconf: bad dumpdev=0x%x", dumpdev);
1268df930be7Sderaadt 	if (bdevsw[maj].d_psize == NULL)
126950ce9ee0Sniklas 		goto bad;
1270df930be7Sderaadt 	nblks = (*bdevsw[maj].d_psize)(dumpdev);
1271df930be7Sderaadt 	if (nblks <= ctod(1))
127250ce9ee0Sniklas 		goto bad;
127350ce9ee0Sniklas 
127450ce9ee0Sniklas 	dumpblks = cpu_dumpsize();
127550ce9ee0Sniklas 	if (dumpblks < 0)
127650ce9ee0Sniklas 		goto bad;
1277aed035abSart 	dumpblks += ctod(cpu_dump_mempagecnt());
127850ce9ee0Sniklas 
127950ce9ee0Sniklas 	/* If dump won't fit (incl. room for possible label), punt. */
128050ce9ee0Sniklas 	if (dumpblks > (nblks - ctod(1)))
128150ce9ee0Sniklas 		goto bad;
128250ce9ee0Sniklas 
128350ce9ee0Sniklas 	/* Put dump at end of partition */
128450ce9ee0Sniklas 	dumplo = nblks - dumpblks;
128550ce9ee0Sniklas 
128650ce9ee0Sniklas 	/* dumpsize is in page units, and doesn't include headers. */
1287aed035abSart 	dumpsize = cpu_dump_mempagecnt();
1288df930be7Sderaadt 	return;
1289df930be7Sderaadt 
129050ce9ee0Sniklas bad:
129150ce9ee0Sniklas 	dumpsize = 0;
129250ce9ee0Sniklas 	return;
1293df930be7Sderaadt }
1294df930be7Sderaadt 
1295df930be7Sderaadt /*
129650ce9ee0Sniklas  * Dump the kernel's image to the swap partition.
1297df930be7Sderaadt  */
1298194dd68bSbrad #define	BYTES_PER_DUMP	PAGE_SIZE
129950ce9ee0Sniklas 
1300df930be7Sderaadt void
1301df930be7Sderaadt dumpsys()
1302df930be7Sderaadt {
1303aed035abSart 	u_long totalbytesleft, bytes, i, n, memcl;
1304aed035abSart 	u_long maddr;
1305aed035abSart 	int psize;
130650ce9ee0Sniklas 	daddr_t blkno;
1307c4071fd1Smillert 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
130850ce9ee0Sniklas 	int error;
1309067cbd75Sderaadt 	extern int msgbufmapped;
1310df930be7Sderaadt 
131150ce9ee0Sniklas 	/* Save registers. */
131250ce9ee0Sniklas 	savectx(&dumppcb);
131350ce9ee0Sniklas 
131450ce9ee0Sniklas 	msgbufmapped = 0;	/* don't record dump msgs in msgbuf */
1315df930be7Sderaadt 	if (dumpdev == NODEV)
1316df930be7Sderaadt 		return;
131750ce9ee0Sniklas 
131850ce9ee0Sniklas 	/*
131950ce9ee0Sniklas 	 * For dumps during autoconfiguration,
132050ce9ee0Sniklas 	 * if dump device has already configured...
132150ce9ee0Sniklas 	 */
1322df930be7Sderaadt 	if (dumpsize == 0)
132350ce9ee0Sniklas 		dumpconf();
132450ce9ee0Sniklas 	if (dumplo <= 0) {
1325aed035abSart 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
1326aed035abSart 		    minor(dumpdev));
1327df930be7Sderaadt 		return;
1328df930be7Sderaadt 	}
1329aed035abSart 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
1330aed035abSart 	    minor(dumpdev), dumplo);
1331df930be7Sderaadt 
133250ce9ee0Sniklas 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
1333df930be7Sderaadt 	printf("dump ");
133450ce9ee0Sniklas 	if (psize == -1) {
133550ce9ee0Sniklas 		printf("area unavailable\n");
133650ce9ee0Sniklas 		return;
133750ce9ee0Sniklas 	}
133850ce9ee0Sniklas 
133950ce9ee0Sniklas 	/* XXX should purge all outstanding keystrokes. */
134050ce9ee0Sniklas 
134150ce9ee0Sniklas 	if ((error = cpu_dump()) != 0)
134250ce9ee0Sniklas 		goto err;
134350ce9ee0Sniklas 
1344aed035abSart 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
134550ce9ee0Sniklas 	blkno = dumplo + cpu_dumpsize();
134650ce9ee0Sniklas 	dump = bdevsw[major(dumpdev)].d_dump;
134750ce9ee0Sniklas 	error = 0;
1348aed035abSart 
1349aed035abSart 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1350aed035abSart 		maddr = mem_clusters[memcl].start;
1351aed035abSart 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1352aed035abSart 
1353aed035abSart 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
135450ce9ee0Sniklas 
135550ce9ee0Sniklas 			/* Print out how many MBs we to go. */
1356aed035abSart 			if ((totalbytesleft % (1024*1024)) == 0)
1357aed035abSart 				printf("%ld ", totalbytesleft / (1024 * 1024));
135850ce9ee0Sniklas 
135950ce9ee0Sniklas 			/* Limit size for next transfer. */
1360aed035abSart 			n = bytes - i;
136150ce9ee0Sniklas 			if (n > BYTES_PER_DUMP)
136250ce9ee0Sniklas 				n =  BYTES_PER_DUMP;
136350ce9ee0Sniklas 
136450ce9ee0Sniklas 			error = (*dump)(dumpdev, blkno,
136550ce9ee0Sniklas 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
136650ce9ee0Sniklas 			if (error)
1367aed035abSart 				goto err;
136850ce9ee0Sniklas 			maddr += n;
136950ce9ee0Sniklas 			blkno += btodb(n);			/* XXX? */
137050ce9ee0Sniklas 
137150ce9ee0Sniklas 			/* XXX should look for keystrokes, to cancel. */
137250ce9ee0Sniklas 		}
1373aed035abSart 	}
137450ce9ee0Sniklas 
137550ce9ee0Sniklas err:
137650ce9ee0Sniklas 	switch (error) {
1377a37778bcSderaadt #ifdef DEBUG
1378df930be7Sderaadt 	case ENXIO:
1379df930be7Sderaadt 		printf("device bad\n");
1380df930be7Sderaadt 		break;
1381df930be7Sderaadt 
1382df930be7Sderaadt 	case EFAULT:
1383df930be7Sderaadt 		printf("device not ready\n");
1384df930be7Sderaadt 		break;
1385df930be7Sderaadt 
1386df930be7Sderaadt 	case EINVAL:
1387df930be7Sderaadt 		printf("area improper\n");
1388df930be7Sderaadt 		break;
1389df930be7Sderaadt 
1390df930be7Sderaadt 	case EIO:
1391df930be7Sderaadt 		printf("i/o error\n");
1392df930be7Sderaadt 		break;
1393df930be7Sderaadt 
1394df930be7Sderaadt 	case EINTR:
1395df930be7Sderaadt 		printf("aborted from console\n");
1396df930be7Sderaadt 		break;
1397a37778bcSderaadt #endif /* DEBUG */
139850ce9ee0Sniklas 	case 0:
1399df930be7Sderaadt 		printf("succeeded\n");
1400df930be7Sderaadt 		break;
140150ce9ee0Sniklas 
140250ce9ee0Sniklas 	default:
140350ce9ee0Sniklas 		printf("error %d\n", error);
140450ce9ee0Sniklas 		break;
1405df930be7Sderaadt 	}
1406df930be7Sderaadt 	printf("\n\n");
1407df930be7Sderaadt 	delay(1000);
1408df930be7Sderaadt }
1409df930be7Sderaadt 
1410df930be7Sderaadt void
1411df930be7Sderaadt frametoreg(framep, regp)
1412df930be7Sderaadt 	struct trapframe *framep;
1413df930be7Sderaadt 	struct reg *regp;
1414df930be7Sderaadt {
1415df930be7Sderaadt 
1416df930be7Sderaadt 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1417df930be7Sderaadt 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1418df930be7Sderaadt 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1419df930be7Sderaadt 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1420df930be7Sderaadt 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1421df930be7Sderaadt 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1422df930be7Sderaadt 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1423df930be7Sderaadt 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1424df930be7Sderaadt 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1425df930be7Sderaadt 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1426df930be7Sderaadt 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1427df930be7Sderaadt 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1428df930be7Sderaadt 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1429df930be7Sderaadt 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1430df930be7Sderaadt 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1431df930be7Sderaadt 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
143250ce9ee0Sniklas 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
143350ce9ee0Sniklas 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
143450ce9ee0Sniklas 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1435df930be7Sderaadt 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1436df930be7Sderaadt 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1437df930be7Sderaadt 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1438df930be7Sderaadt 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1439df930be7Sderaadt 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1440df930be7Sderaadt 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1441df930be7Sderaadt 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1442df930be7Sderaadt 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1443df930be7Sderaadt 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1444df930be7Sderaadt 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
144550ce9ee0Sniklas 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
144650ce9ee0Sniklas 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1447df930be7Sderaadt 	regp->r_regs[R_ZERO] = 0;
1448df930be7Sderaadt }
1449df930be7Sderaadt 
1450df930be7Sderaadt void
1451df930be7Sderaadt regtoframe(regp, framep)
1452df930be7Sderaadt 	struct reg *regp;
1453df930be7Sderaadt 	struct trapframe *framep;
1454df930be7Sderaadt {
1455df930be7Sderaadt 
1456df930be7Sderaadt 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1457df930be7Sderaadt 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1458df930be7Sderaadt 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1459df930be7Sderaadt 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1460df930be7Sderaadt 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1461df930be7Sderaadt 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1462df930be7Sderaadt 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1463df930be7Sderaadt 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1464df930be7Sderaadt 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1465df930be7Sderaadt 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1466df930be7Sderaadt 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1467df930be7Sderaadt 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1468df930be7Sderaadt 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1469df930be7Sderaadt 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1470df930be7Sderaadt 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1471df930be7Sderaadt 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
147250ce9ee0Sniklas 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
147350ce9ee0Sniklas 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
147450ce9ee0Sniklas 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1475df930be7Sderaadt 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1476df930be7Sderaadt 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1477df930be7Sderaadt 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1478df930be7Sderaadt 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1479df930be7Sderaadt 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1480df930be7Sderaadt 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1481df930be7Sderaadt 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1482df930be7Sderaadt 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1483df930be7Sderaadt 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1484df930be7Sderaadt 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
148550ce9ee0Sniklas 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
148650ce9ee0Sniklas 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1487df930be7Sderaadt 	/* ??? = regp->r_regs[R_ZERO]; */
1488df930be7Sderaadt }
1489df930be7Sderaadt 
1490df930be7Sderaadt void
1491df930be7Sderaadt printregs(regp)
1492df930be7Sderaadt 	struct reg *regp;
1493df930be7Sderaadt {
1494df930be7Sderaadt 	int i;
1495df930be7Sderaadt 
1496df930be7Sderaadt 	for (i = 0; i < 32; i++)
1497df930be7Sderaadt 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1498df930be7Sderaadt 		   i & 1 ? "\n" : "\t");
1499df930be7Sderaadt }
1500df930be7Sderaadt 
1501df930be7Sderaadt void
1502df930be7Sderaadt regdump(framep)
1503df930be7Sderaadt 	struct trapframe *framep;
1504df930be7Sderaadt {
1505df930be7Sderaadt 	struct reg reg;
1506df930be7Sderaadt 
1507df930be7Sderaadt 	frametoreg(framep, &reg);
150850ce9ee0Sniklas 	reg.r_regs[R_SP] = alpha_pal_rdusp();
150950ce9ee0Sniklas 
1510df930be7Sderaadt 	printf("REGISTERS:\n");
1511df930be7Sderaadt 	printregs(&reg);
1512df930be7Sderaadt }
1513df930be7Sderaadt 
1514df930be7Sderaadt #ifdef DEBUG
1515df930be7Sderaadt int sigdebug = 0;
1516df930be7Sderaadt int sigpid = 0;
1517df930be7Sderaadt #define	SDB_FOLLOW	0x01
1518df930be7Sderaadt #define	SDB_KSTACK	0x02
1519df930be7Sderaadt #endif
1520df930be7Sderaadt 
1521df930be7Sderaadt /*
1522df930be7Sderaadt  * Send an interrupt to process.
1523df930be7Sderaadt  */
1524df930be7Sderaadt void
15255e1760a6Sderaadt sendsig(catcher, sig, mask, code, type, val)
1526df930be7Sderaadt 	sig_t catcher;
1527df930be7Sderaadt 	int sig, mask;
1528df930be7Sderaadt 	u_long code;
15295e1760a6Sderaadt 	int type;
15305e1760a6Sderaadt 	union sigval val;
1531df930be7Sderaadt {
1532df930be7Sderaadt 	struct proc *p = curproc;
1533df930be7Sderaadt 	struct sigcontext *scp, ksc;
1534df930be7Sderaadt 	struct trapframe *frame;
1535df930be7Sderaadt 	struct sigacts *psp = p->p_sigacts;
15362bf9c155Sderaadt 	int oonstack, fsize, rndfsize, kscsize;
15372bf9c155Sderaadt 	siginfo_t *sip, ksi;
1538df930be7Sderaadt 
1539df930be7Sderaadt 	frame = p->p_md.md_tf;
1540df930be7Sderaadt 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
1541df930be7Sderaadt 	fsize = sizeof ksc;
1542df930be7Sderaadt 	rndfsize = ((fsize + 15) / 16) * 16;
15432bf9c155Sderaadt 	kscsize = rndfsize;
15442bf9c155Sderaadt 	if (psp->ps_siginfo & sigmask(sig)) {
15452bf9c155Sderaadt 		fsize += sizeof ksi;
15462bf9c155Sderaadt 		rndfsize = ((fsize + 15) / 16) * 16;
15472bf9c155Sderaadt 	}
154874652a67Sniklas 
1549df930be7Sderaadt 	/*
1550df930be7Sderaadt 	 * Allocate and validate space for the signal handler
1551df930be7Sderaadt 	 * context. Note that if the stack is in P0 space, the
1552aed035abSart 	 * call to uvm_grow() is a nop, and the useracc() check
1553df930be7Sderaadt 	 * will fail if the process has not already allocated
1554df930be7Sderaadt 	 * the space with a `brk'.
1555df930be7Sderaadt 	 */
1556df930be7Sderaadt 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
1557df930be7Sderaadt 	    (psp->ps_sigonstack & sigmask(sig))) {
15588bc2093aSderaadt 		scp = (struct sigcontext *)(psp->ps_sigstk.ss_sp +
1559df930be7Sderaadt 		    psp->ps_sigstk.ss_size - rndfsize);
1560df930be7Sderaadt 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
1561df930be7Sderaadt 	} else
156250ce9ee0Sniklas 		scp = (struct sigcontext *)(alpha_pal_rdusp() - rndfsize);
1563df930be7Sderaadt 	if ((u_long)scp <= USRSTACK - ctob(p->p_vmspace->vm_ssize))
1564aed035abSart 		(void)uvm_grow(p, (u_long)scp);
1565df930be7Sderaadt #ifdef DEBUG
1566df930be7Sderaadt 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
156750ce9ee0Sniklas 		printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
1568df930be7Sderaadt 		    sig, &oonstack, scp);
1569df930be7Sderaadt #endif
1570df930be7Sderaadt 
1571df930be7Sderaadt 	/*
1572df930be7Sderaadt 	 * Build the signal context to be used by sigreturn.
1573df930be7Sderaadt 	 */
1574df930be7Sderaadt 	ksc.sc_onstack = oonstack;
1575df930be7Sderaadt 	ksc.sc_mask = mask;
157650ce9ee0Sniklas 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
157750ce9ee0Sniklas 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
1578df930be7Sderaadt 
1579df930be7Sderaadt 	/* copy the registers. */
1580df930be7Sderaadt 	frametoreg(frame, (struct reg *)ksc.sc_regs);
1581df930be7Sderaadt 	ksc.sc_regs[R_ZERO] = 0xACEDBADE;		/* magic number */
158250ce9ee0Sniklas 	ksc.sc_regs[R_SP] = alpha_pal_rdusp();
1583df930be7Sderaadt 
1584df930be7Sderaadt 	/* save the floating-point state, if necessary, then copy it. */
1585433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1586433075b6Spvalchev 		fpusave_proc(p, 1);
1587df930be7Sderaadt 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
1588433075b6Spvalchev 	memcpy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
1589df930be7Sderaadt 	    sizeof(struct fpreg));
1590433075b6Spvalchev #ifndef NO_IEEE
1591433075b6Spvalchev 	ksc.sc_fp_control = alpha_read_fp_c(p);
1592433075b6Spvalchev #else
1593433075b6Spvalchev 	ksc.sc_fp_control = 0;
1594433075b6Spvalchev #endif
1595433075b6Spvalchev 	memset(ksc.sc_reserved, 0, sizeof ksc.sc_reserved);	/* XXX */
1596433075b6Spvalchev 	memset(ksc.sc_xxx, 0, sizeof ksc.sc_xxx);		/* XXX */
1597df930be7Sderaadt 
1598df930be7Sderaadt #ifdef COMPAT_OSF1
1599df930be7Sderaadt 	/*
1600df930be7Sderaadt 	 * XXX Create an OSF/1-style sigcontext and associated goo.
1601df930be7Sderaadt 	 */
1602df930be7Sderaadt #endif
1603df930be7Sderaadt 
16042bf9c155Sderaadt 	if (psp->ps_siginfo & sigmask(sig)) {
16052bf9c155Sderaadt 		initsiginfo(&ksi, sig, code, type, val);
16062bf9c155Sderaadt 		sip = (void *)scp + kscsize;
1607679ebc41Smiod 		if (copyout((caddr_t)&ksi, (caddr_t)sip, fsize - kscsize) != 0)
1608679ebc41Smiod 			goto trash;
1609aa540fb8Sart 	} else
1610aa540fb8Sart 		sip = NULL;
16112bf9c155Sderaadt 
1612df930be7Sderaadt 	/*
1613df930be7Sderaadt 	 * copy the frame out to userland.
1614df930be7Sderaadt 	 */
1615679ebc41Smiod 	if (copyout((caddr_t)&ksc, (caddr_t)scp, kscsize) != 0) {
1616679ebc41Smiod trash:
1617679ebc41Smiod #ifdef DEBUG
1618679ebc41Smiod 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1619679ebc41Smiod 			printf("sendsig(%d): copyout failed on sig %d\n",
1620679ebc41Smiod 			    p->p_pid, sig);
1621679ebc41Smiod #endif
1622679ebc41Smiod 		/*
1623679ebc41Smiod 		 * Process has trashed its stack; give it an illegal
1624679ebc41Smiod 		 * instruction to halt it in its tracks.
1625679ebc41Smiod 		 */
162686fd84b3Smiod 		sigexit(p, SIGILL);
162786fd84b3Smiod 		/* NOTREACHED */
1628679ebc41Smiod 	}
1629df930be7Sderaadt #ifdef DEBUG
1630df930be7Sderaadt 	if (sigdebug & SDB_FOLLOW)
163150ce9ee0Sniklas 		printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
1632df930be7Sderaadt 		    scp, code);
1633df930be7Sderaadt #endif
1634df930be7Sderaadt 
1635df930be7Sderaadt 	/*
1636df930be7Sderaadt 	 * Set up the registers to return to sigcode.
1637df930be7Sderaadt 	 */
16384a5480feSart 	frame->tf_regs[FRAME_PC] = p->p_sigcode;
163950ce9ee0Sniklas 	frame->tf_regs[FRAME_A0] = sig;
1640aa540fb8Sart 	frame->tf_regs[FRAME_A1] = (u_int64_t)sip;
164150ce9ee0Sniklas 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
1642df930be7Sderaadt 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
164350ce9ee0Sniklas 	alpha_pal_wrusp((unsigned long)scp);
1644df930be7Sderaadt 
1645df930be7Sderaadt #ifdef DEBUG
1646df930be7Sderaadt 	if (sigdebug & SDB_FOLLOW)
1647df930be7Sderaadt 		printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
164850ce9ee0Sniklas 		    frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
1649df930be7Sderaadt 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1650df930be7Sderaadt 		printf("sendsig(%d): sig %d returns\n",
1651df930be7Sderaadt 		    p->p_pid, sig);
1652df930be7Sderaadt #endif
1653df930be7Sderaadt }
1654df930be7Sderaadt 
1655df930be7Sderaadt /*
1656df930be7Sderaadt  * System call to cleanup state after a signal
1657df930be7Sderaadt  * has been taken.  Reset signal mask and
1658df930be7Sderaadt  * stack state from context left by sendsig (above).
1659df930be7Sderaadt  * Return to previous pc and psl as specified by
1660df930be7Sderaadt  * context left by sendsig. Check carefully to
1661df930be7Sderaadt  * make sure that the user has not modified the
1662125cd19fSderaadt  * psl to gain improper privileges or to cause
1663df930be7Sderaadt  * a machine fault.
1664df930be7Sderaadt  */
1665df930be7Sderaadt /* ARGSUSED */
1666df930be7Sderaadt int
1667df930be7Sderaadt sys_sigreturn(p, v, retval)
1668df930be7Sderaadt 	struct proc *p;
1669df930be7Sderaadt 	void *v;
1670df930be7Sderaadt 	register_t *retval;
1671df930be7Sderaadt {
1672df930be7Sderaadt 	struct sys_sigreturn_args /* {
1673df930be7Sderaadt 		syscallarg(struct sigcontext *) sigcntxp;
1674df930be7Sderaadt 	} */ *uap = v;
1675aa540fb8Sart 	struct sigcontext ksc;
167660959295Smartin #ifdef DEBUG
167760959295Smartin 	struct sigcontext *scp;
167860959295Smartin #endif
1679aa540fb8Sart 	int error;
1680df930be7Sderaadt 
1681df930be7Sderaadt #ifdef DEBUG
1682df930be7Sderaadt 	if (sigdebug & SDB_FOLLOW)
168350ce9ee0Sniklas 	    printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
1684df930be7Sderaadt #endif
1685df930be7Sderaadt 
1686df930be7Sderaadt 	/*
1687df930be7Sderaadt 	 * Test and fetch the context structure.
1688df930be7Sderaadt 	 * We grab it all at once for speed.
1689df930be7Sderaadt 	 */
1690aa540fb8Sart 	if ((error = copyin(SCARG(uap, sigcntxp), &ksc, sizeof(ksc))) != 0)
1691aa540fb8Sart 		return (error);
1692df930be7Sderaadt 
1693df930be7Sderaadt 	if (ksc.sc_regs[R_ZERO] != 0xACEDBADE)		/* magic number */
1694df930be7Sderaadt 		return (EINVAL);
1695df930be7Sderaadt 	/*
1696df930be7Sderaadt 	 * Restore the user-supplied information
1697df930be7Sderaadt 	 */
1698df930be7Sderaadt 	if (ksc.sc_onstack)
1699df930be7Sderaadt 		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
1700df930be7Sderaadt 	else
1701df930be7Sderaadt 		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
1702df930be7Sderaadt 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
1703df930be7Sderaadt 
170450ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
170550ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_PS] =
170650ce9ee0Sniklas 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
1707df930be7Sderaadt 
1708df930be7Sderaadt 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
170950ce9ee0Sniklas 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
1710df930be7Sderaadt 
1711df930be7Sderaadt 	/* XXX ksc.sc_ownedfp ? */
1712433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1713433075b6Spvalchev 		fpusave_proc(p, 0);
1714433075b6Spvalchev 	memcpy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
1715df930be7Sderaadt 	    sizeof(struct fpreg));
1716433075b6Spvalchev #ifndef NO_IEEE
1717433075b6Spvalchev 	p->p_addr->u_pcb.pcb_fp.fpr_cr = ksc.sc_fpcr;
1718433075b6Spvalchev 	p->p_md.md_flags = ksc.sc_fp_control & MDP_FP_C;
1719433075b6Spvalchev #endif
1720df930be7Sderaadt 
1721df930be7Sderaadt #ifdef DEBUG
1722df930be7Sderaadt 	if (sigdebug & SDB_FOLLOW)
1723df930be7Sderaadt 		printf("sigreturn(%d): returns\n", p->p_pid);
1724df930be7Sderaadt #endif
1725df930be7Sderaadt 	return (EJUSTRETURN);
1726df930be7Sderaadt }
1727df930be7Sderaadt 
1728df930be7Sderaadt /*
1729df930be7Sderaadt  * machine dependent system variables.
1730df930be7Sderaadt  */
173150ce9ee0Sniklas int
1732df930be7Sderaadt cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1733df930be7Sderaadt 	int *name;
1734df930be7Sderaadt 	u_int namelen;
1735df930be7Sderaadt 	void *oldp;
1736df930be7Sderaadt 	size_t *oldlenp;
1737df930be7Sderaadt 	void *newp;
1738df930be7Sderaadt 	size_t newlen;
1739df930be7Sderaadt 	struct proc *p;
1740df930be7Sderaadt {
1741df930be7Sderaadt 	dev_t consdev;
1742df930be7Sderaadt 
174345e5a1a0Sart 	if (name[0] != CPU_CHIPSET && namelen != 1)
1744df930be7Sderaadt 		return (ENOTDIR);		/* overloaded */
1745df930be7Sderaadt 
1746df930be7Sderaadt 	switch (name[0]) {
1747df930be7Sderaadt 	case CPU_CONSDEV:
1748df930be7Sderaadt 		if (cn_tab != NULL)
1749df930be7Sderaadt 			consdev = cn_tab->cn_dev;
1750df930be7Sderaadt 		else
1751df930be7Sderaadt 			consdev = NODEV;
1752df930be7Sderaadt 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
1753df930be7Sderaadt 			sizeof consdev));
1754417eba8cSderaadt 
1755417eba8cSderaadt 	case CPU_ROOT_DEVICE:
1756aed035abSart 		return (sysctl_rdstring(oldp, oldlenp, newp,
1757aed035abSart 		    root_device));
1758a37778bcSderaadt #ifndef SMALL_KERNEL
175950ce9ee0Sniklas 	case CPU_UNALIGNED_PRINT:
176050ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
176150ce9ee0Sniklas 		    &alpha_unaligned_print));
176250ce9ee0Sniklas 
176350ce9ee0Sniklas 	case CPU_UNALIGNED_FIX:
176450ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
176550ce9ee0Sniklas 		    &alpha_unaligned_fix));
176650ce9ee0Sniklas 
176750ce9ee0Sniklas 	case CPU_UNALIGNED_SIGBUS:
176850ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
176950ce9ee0Sniklas 		    &alpha_unaligned_sigbus));
177050ce9ee0Sniklas 
17713a630e3fSniklas 	case CPU_BOOTED_KERNEL:
1772aed035abSart 		return (sysctl_rdstring(oldp, oldlenp, newp,
1773aed035abSart 		    bootinfo.booted_kernel));
17743a630e3fSniklas 
177545e5a1a0Sart 	case CPU_CHIPSET:
177645e5a1a0Sart 		return (alpha_sysctl_chipset(name + 1, namelen - 1, oldp,
177745e5a1a0Sart 		    oldlenp));
1778a37778bcSderaadt #endif /* SMALL_KERNEL */
1779433075b6Spvalchev 
1780433075b6Spvalchev #ifndef NO_IEEE
1781433075b6Spvalchev 	case CPU_FP_SYNC_COMPLETE:
1782433075b6Spvalchev 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1783433075b6Spvalchev 		    &alpha_fp_sync_complete));
1784433075b6Spvalchev #endif
178527626149Smatthieu 	case CPU_ALLOWAPERTURE:
178627626149Smatthieu #ifdef APERTURE
178727626149Smatthieu 		if (securelevel > 0)
17881546ceefSderaadt 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
17891546ceefSderaadt 			    &allowaperture));
179027626149Smatthieu                 else
179127626149Smatthieu                         return (sysctl_int(oldp, oldlenp, newp, newlen,
179227626149Smatthieu                             &allowaperture));
179327626149Smatthieu #else
179427626149Smatthieu 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
179527626149Smatthieu #endif
1796df930be7Sderaadt 	default:
1797df930be7Sderaadt 		return (EOPNOTSUPP);
1798df930be7Sderaadt 	}
1799df930be7Sderaadt 	/* NOTREACHED */
1800df930be7Sderaadt }
1801df930be7Sderaadt 
1802df930be7Sderaadt /*
1803df930be7Sderaadt  * Set registers on exec.
1804df930be7Sderaadt  */
1805df930be7Sderaadt void
1806df930be7Sderaadt setregs(p, pack, stack, retval)
1807df930be7Sderaadt 	register struct proc *p;
1808df930be7Sderaadt 	struct exec_package *pack;
1809df930be7Sderaadt 	u_long stack;
1810df930be7Sderaadt 	register_t *retval;
1811df930be7Sderaadt {
1812df930be7Sderaadt 	struct trapframe *tfp = p->p_md.md_tf;
18133a630e3fSniklas #ifdef DEBUG
18143a630e3fSniklas 	int i;
18153a630e3fSniklas #endif
1816df930be7Sderaadt 
1817df930be7Sderaadt #ifdef DEBUG
181850ce9ee0Sniklas 	/*
181950ce9ee0Sniklas 	 * Crash and dump, if the user requested it.
182050ce9ee0Sniklas 	 */
182150ce9ee0Sniklas 	if (boothowto & RB_DUMP)
182250ce9ee0Sniklas 		panic("crash requested by boot flags");
182350ce9ee0Sniklas #endif
182450ce9ee0Sniklas 
182550ce9ee0Sniklas #ifdef DEBUG
182650ce9ee0Sniklas 	for (i = 0; i < FRAME_SIZE; i++)
1827df930be7Sderaadt 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
1828df930be7Sderaadt #else
182950ce9ee0Sniklas 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1830df930be7Sderaadt #endif
1831df930be7Sderaadt 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
183250ce9ee0Sniklas 	alpha_pal_wrusp(stack);
183350ce9ee0Sniklas 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
183450ce9ee0Sniklas 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1835df930be7Sderaadt 
183650ce9ee0Sniklas 	tfp->tf_regs[FRAME_A0] = stack;
183750ce9ee0Sniklas 	/* a1 and a2 already zeroed */
183850ce9ee0Sniklas 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
183950ce9ee0Sniklas 
184050ce9ee0Sniklas 	p->p_md.md_flags &= ~MDP_FPUSED;
1841433075b6Spvalchev #ifndef NO_IEEE
1842433075b6Spvalchev 	if (__predict_true((p->p_md.md_flags & IEEE_INHERIT) == 0)) {
1843433075b6Spvalchev 		p->p_md.md_flags &= ~MDP_FP_C;
1844433075b6Spvalchev 		p->p_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
1845433075b6Spvalchev 	}
1846433075b6Spvalchev #endif
1847433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1848433075b6Spvalchev 		fpusave_proc(p, 0);
1849433075b6Spvalchev }
1850df930be7Sderaadt 
1851433075b6Spvalchev /*
1852433075b6Spvalchev  * Release the FPU.
1853433075b6Spvalchev  */
1854433075b6Spvalchev void
1855433075b6Spvalchev fpusave_cpu(struct cpu_info *ci, int save)
1856433075b6Spvalchev {
1857433075b6Spvalchev 	struct proc *p;
1858433075b6Spvalchev #if defined(MULTIPROCESSOR)
1859433075b6Spvalchev 	int s;
1860433075b6Spvalchev #endif
1861433075b6Spvalchev 
1862433075b6Spvalchev 	KDASSERT(ci == curcpu());
1863433075b6Spvalchev 
1864433075b6Spvalchev #if defined(MULTIPROCESSOR)
1865433075b6Spvalchev 	atomic_setbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1866433075b6Spvalchev #endif
1867433075b6Spvalchev 
1868433075b6Spvalchev 	p = ci->ci_fpcurproc;
1869433075b6Spvalchev 	if (p == NULL)
1870433075b6Spvalchev 		goto out;
1871433075b6Spvalchev 
1872433075b6Spvalchev 	if (save) {
1873433075b6Spvalchev 		alpha_pal_wrfen(1);
1874433075b6Spvalchev 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
1875433075b6Spvalchev 	}
1876433075b6Spvalchev 
1877433075b6Spvalchev 	alpha_pal_wrfen(0);
1878433075b6Spvalchev 
1879433075b6Spvalchev 	p->p_addr->u_pcb.pcb_fpcpu = NULL;
1880433075b6Spvalchev 	ci->ci_fpcurproc = NULL;
1881433075b6Spvalchev 
1882433075b6Spvalchev out:
1883433075b6Spvalchev #if defined(MULTIPROCESSOR)
1884433075b6Spvalchev 	atomic_clearbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1885433075b6Spvalchev #endif
1886433075b6Spvalchev 	return;
1887433075b6Spvalchev }
1888433075b6Spvalchev 
1889433075b6Spvalchev /*
1890433075b6Spvalchev  * Synchronize FP state for this process.
1891433075b6Spvalchev  */
1892433075b6Spvalchev void
1893433075b6Spvalchev fpusave_proc(struct proc *p, int save)
1894433075b6Spvalchev {
1895433075b6Spvalchev 	struct cpu_info *ci = curcpu();
1896433075b6Spvalchev 	struct cpu_info *oci;
1897433075b6Spvalchev #if defined(MULTIPROCESSOR)
1898433075b6Spvalchev 	u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
1899433075b6Spvalchev 	int s, spincount;
1900433075b6Spvalchev #endif
1901433075b6Spvalchev 
1902433075b6Spvalchev 	KDASSERT(p->p_addr != NULL);
1903433075b6Spvalchev 
1904433075b6Spvalchev 	oci = p->p_addr->u_pcb.pcb_fpcpu;
1905433075b6Spvalchev 	if (oci == NULL) {
1906433075b6Spvalchev 		return;
1907433075b6Spvalchev 	}
1908433075b6Spvalchev 
1909433075b6Spvalchev #if defined(MULTIPROCESSOR)
1910433075b6Spvalchev 	if (oci == ci) {
1911433075b6Spvalchev 		KASSERT(ci->ci_fpcurproc == p);
1912433075b6Spvalchev 		fpusave_cpu(ci, save);
1913433075b6Spvalchev 		return;
1914433075b6Spvalchev 	}
1915433075b6Spvalchev 
1916433075b6Spvalchev 	KASSERT(oci->ci_fpcurproc == p);
1917433075b6Spvalchev 	alpha_send_ipi(oci->ci_cpuid, ipi);
1918433075b6Spvalchev 
1919433075b6Spvalchev 	spincount = 0;
1920433075b6Spvalchev 	while (p->p_addr->u_pcb.pcb_fpcpu != NULL) {
1921433075b6Spvalchev 		spincount++;
1922433075b6Spvalchev 		delay(1000);    /* XXX */
1923433075b6Spvalchev 		if (spincount > 10000)
1924433075b6Spvalchev 			panic("fpsave ipi didn't");
1925433075b6Spvalchev 	}
1926433075b6Spvalchev #else
1927433075b6Spvalchev 	KASSERT(ci->ci_fpcurproc == p);
1928433075b6Spvalchev 	fpusave_cpu(ci, save);
1929433075b6Spvalchev #endif /* MULTIPROCESSOR */
1930df930be7Sderaadt }
1931df930be7Sderaadt 
1932df930be7Sderaadt int
1933df930be7Sderaadt spl0()
1934df930be7Sderaadt {
1935df930be7Sderaadt 
1936aed035abSart 	if (ssir) {
1937aed035abSart 		(void) alpha_pal_swpipl(ALPHA_PSL_IPL_SOFT);
19382a2685f2Sart 		softintr_dispatch();
1939aed035abSart 	}
1940df930be7Sderaadt 
194150ce9ee0Sniklas 	return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
1942df930be7Sderaadt }
1943df930be7Sderaadt 
1944df930be7Sderaadt /*
1945df930be7Sderaadt  * The following primitives manipulate the run queues.  _whichqs tells which
1946df930be7Sderaadt  * of the 32 queues _qs have processes in them.  Setrunqueue puts processes
1947e464495eSniklas  * into queues, Remrunqueue removes them from queues.  The running process is
1948e464495eSniklas  * on no queue, other processes are on a queue related to p->p_priority,
1949e464495eSniklas  * divided by 4 actually to shrink the 0-127 range of priorities into the 32
1950e464495eSniklas  * available queues.
1951df930be7Sderaadt  */
1952df930be7Sderaadt /*
1953df930be7Sderaadt  * setrunqueue(p)
1954df930be7Sderaadt  *	proc *p;
1955df930be7Sderaadt  *
1956df930be7Sderaadt  * Call should be made at splclock(), and p->p_stat should be SRUN.
1957df930be7Sderaadt  */
1958df930be7Sderaadt 
19592a2685f2Sart /* XXXART - grmble */
19602a2685f2Sart #define sched_qs qs
19612a2685f2Sart #define sched_whichqs whichqs
19622a2685f2Sart 
1963df930be7Sderaadt void
1964df930be7Sderaadt setrunqueue(p)
1965df930be7Sderaadt 	struct proc *p;
1966df930be7Sderaadt {
1967df930be7Sderaadt 	int bit;
1968df930be7Sderaadt 
1969df930be7Sderaadt 	/* firewall: p->p_back must be NULL */
1970df930be7Sderaadt 	if (p->p_back != NULL)
1971df930be7Sderaadt 		panic("setrunqueue");
1972df930be7Sderaadt 
1973df930be7Sderaadt 	bit = p->p_priority >> 2;
19742a2685f2Sart 	sched_whichqs |= (1 << bit);
19752a2685f2Sart 	p->p_forw = (struct proc *)&sched_qs[bit];
19762a2685f2Sart 	p->p_back = sched_qs[bit].ph_rlink;
1977df930be7Sderaadt 	p->p_back->p_forw = p;
19782a2685f2Sart 	sched_qs[bit].ph_rlink = p;
1979df930be7Sderaadt }
1980df930be7Sderaadt 
1981df930be7Sderaadt /*
1982e464495eSniklas  * remrunqueue(p)
1983df930be7Sderaadt  *
1984df930be7Sderaadt  * Call should be made at splclock().
1985df930be7Sderaadt  */
1986df930be7Sderaadt void
1987d3cbbad5Skstailey remrunqueue(p)
1988df930be7Sderaadt 	struct proc *p;
1989df930be7Sderaadt {
1990df930be7Sderaadt 	int bit;
1991df930be7Sderaadt 
1992df930be7Sderaadt 	bit = p->p_priority >> 2;
19932a2685f2Sart 	if ((sched_whichqs & (1 << bit)) == 0)
1994d3cbbad5Skstailey 		panic("remrunqueue");
1995df930be7Sderaadt 
1996df930be7Sderaadt 	p->p_back->p_forw = p->p_forw;
1997df930be7Sderaadt 	p->p_forw->p_back = p->p_back;
1998df930be7Sderaadt 	p->p_back = NULL;	/* for firewall checking. */
1999df930be7Sderaadt 
20002a2685f2Sart 	if ((struct proc *)&sched_qs[bit] == sched_qs[bit].ph_link)
20012a2685f2Sart 		sched_whichqs &= ~(1 << bit);
2002df930be7Sderaadt }
2003df930be7Sderaadt 
2004df930be7Sderaadt /*
2005df930be7Sderaadt  * Return the best possible estimate of the time in the timeval
2006df930be7Sderaadt  * to which tvp points.  Unfortunately, we can't read the hardware registers.
2007df930be7Sderaadt  * We guarantee that the time will be greater than the value obtained by a
2008df930be7Sderaadt  * previous call.
2009df930be7Sderaadt  */
2010df930be7Sderaadt void
2011df930be7Sderaadt microtime(tvp)
2012df930be7Sderaadt 	register struct timeval *tvp;
2013df930be7Sderaadt {
2014df930be7Sderaadt 	int s = splclock();
2015df930be7Sderaadt 	static struct timeval lasttime;
2016df930be7Sderaadt 
2017df930be7Sderaadt 	*tvp = time;
2018df930be7Sderaadt #ifdef notdef
2019df930be7Sderaadt 	tvp->tv_usec += clkread();
20204c891e15Spjanzen 	while (tvp->tv_usec >= 1000000) {
2021df930be7Sderaadt 		tvp->tv_sec++;
2022df930be7Sderaadt 		tvp->tv_usec -= 1000000;
2023df930be7Sderaadt 	}
2024df930be7Sderaadt #endif
2025df930be7Sderaadt 	if (tvp->tv_sec == lasttime.tv_sec &&
2026df930be7Sderaadt 	    tvp->tv_usec <= lasttime.tv_usec &&
20274c891e15Spjanzen 	    (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
2028df930be7Sderaadt 		tvp->tv_sec++;
2029df930be7Sderaadt 		tvp->tv_usec -= 1000000;
2030df930be7Sderaadt 	}
2031df930be7Sderaadt 	lasttime = *tvp;
2032df930be7Sderaadt 	splx(s);
2033df930be7Sderaadt }
2034df930be7Sderaadt 
2035417eba8cSderaadt /*
2036417eba8cSderaadt  * Wait "n" microseconds.
2037417eba8cSderaadt  */
203850ce9ee0Sniklas void
2039417eba8cSderaadt delay(n)
204050ce9ee0Sniklas 	unsigned long n;
2041417eba8cSderaadt {
20425d097e9eSmiod 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
20435d097e9eSmiod 
20445d097e9eSmiod 	if (n == 0)
20455d097e9eSmiod 		return;
20465d097e9eSmiod 
20475d097e9eSmiod 	pcc0 = alpha_rpcc() & 0xffffffffUL;
20485d097e9eSmiod 	cycles = 0;
20495d097e9eSmiod 	usec = 0;
20505d097e9eSmiod 
20515d097e9eSmiod 	while (usec <= n) {
20525d097e9eSmiod 		/*
20535d097e9eSmiod 		 * Get the next CPU cycle count - assumes that we can not
20545d097e9eSmiod 		 * have had more than one 32 bit overflow.
20555d097e9eSmiod 		 */
20565d097e9eSmiod 		pcc1 = alpha_rpcc() & 0xffffffffUL;
20575d097e9eSmiod 		if (pcc1 < pcc0)
20585d097e9eSmiod 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
20595d097e9eSmiod 		else
20605d097e9eSmiod 			curcycle = pcc1 - pcc0;
2061417eba8cSderaadt 
2062aed035abSart 		/*
20635d097e9eSmiod 		 * We now have the number of processor cycles since we
20645d097e9eSmiod 		 * last checked. Add the current cycle count to the
20655d097e9eSmiod 		 * running total. If it's over cycles_per_usec, increment
20665d097e9eSmiod 		 * the usec counter.
2067aed035abSart 		 */
20685d097e9eSmiod 		cycles += curcycle;
20695d097e9eSmiod 		while (cycles > cycles_per_usec) {
20705d097e9eSmiod 			usec++;
20715d097e9eSmiod 			cycles -= cycles_per_usec;
20725d097e9eSmiod 		}
20735d097e9eSmiod 		pcc0 = pcc1;
20745d097e9eSmiod 	}
2075417eba8cSderaadt }
2076417eba8cSderaadt 
20779da89091Sderaadt #if defined(COMPAT_OSF1)
2078c4071fd1Smillert void	cpu_exec_ecoff_setregs(struct proc *, struct exec_package *,
2079c4071fd1Smillert 	    u_long, register_t *);
20803a630e3fSniklas 
2081df930be7Sderaadt void
2082417eba8cSderaadt cpu_exec_ecoff_setregs(p, epp, stack, retval)
2083df930be7Sderaadt 	struct proc *p;
2084417eba8cSderaadt 	struct exec_package *epp;
2085df930be7Sderaadt 	u_long stack;
2086df930be7Sderaadt 	register_t *retval;
2087df930be7Sderaadt {
2088417eba8cSderaadt 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
2089df930be7Sderaadt 
2090417eba8cSderaadt 	setregs(p, epp, stack, retval);
209150ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
2092df930be7Sderaadt }
2093df930be7Sderaadt 
2094df930be7Sderaadt /*
2095df930be7Sderaadt  * cpu_exec_ecoff_hook():
2096df930be7Sderaadt  *	cpu-dependent ECOFF format hook for execve().
2097df930be7Sderaadt  *
2098df930be7Sderaadt  * Do any machine-dependent diddling of the exec package when doing ECOFF.
2099df930be7Sderaadt  *
2100df930be7Sderaadt  */
2101df930be7Sderaadt int
2102417eba8cSderaadt cpu_exec_ecoff_hook(p, epp)
2103df930be7Sderaadt 	struct proc *p;
2104df930be7Sderaadt 	struct exec_package *epp;
2105df930be7Sderaadt {
2106417eba8cSderaadt 	struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
2107c3114d5bSericj 	extern struct emul emul_native;
2108aed035abSart 	int error;
21094e8700e2Sericj 	extern int osf1_exec_ecoff_hook(struct proc *, struct exec_package *);
2110df930be7Sderaadt 
2111417eba8cSderaadt 	switch (execp->f.f_magic) {
2112df930be7Sderaadt #ifdef COMPAT_OSF1
2113df930be7Sderaadt 	case ECOFF_MAGIC_ALPHA:
21144e8700e2Sericj 		error = osf1_exec_ecoff_hook(p, epp);
2115df930be7Sderaadt 		break;
2116df930be7Sderaadt #endif
2117df930be7Sderaadt 
211850ce9ee0Sniklas 	case ECOFF_MAGIC_NATIVE_ALPHA:
2119a2f8ce8dSderaadt 		epp->ep_emul = &emul_native;
2120aed035abSart 		error = 0;
2121df930be7Sderaadt 		break;
2122df930be7Sderaadt 
2123df930be7Sderaadt 	default:
2124aed035abSart 		error = ENOEXEC;
2125df930be7Sderaadt 	}
2126aed035abSart 	return (error);
2127df930be7Sderaadt }
2128df930be7Sderaadt #endif
2129e464495eSniklas 
2130aed035abSart int
2131aed035abSart alpha_pa_access(pa)
2132aed035abSart 	u_long pa;
2133aed035abSart {
2134aed035abSart 	int i;
2135aed035abSart 
2136aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++) {
2137aed035abSart 		if (pa < mem_clusters[i].start)
2138aed035abSart 			continue;
2139aed035abSart 		if ((pa - mem_clusters[i].start) >=
2140aed035abSart 		    (mem_clusters[i].size & ~PAGE_MASK))
2141aed035abSart 			continue;
2142aed035abSart 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
2143aed035abSart 	}
2144aed035abSart 
2145aed035abSart 	/*
2146aed035abSart 	 * Address is not a memory address.  If we're secure, disallow
2147aed035abSart 	 * access.  Otherwise, grant read/write.
2148aed035abSart 	 */
2149aed035abSart 	if (securelevel > 0)
2150aed035abSart 		return (VM_PROT_NONE);
2151aed035abSart 	else
2152aed035abSart 		return (VM_PROT_READ | VM_PROT_WRITE);
2153aed035abSart }
2154aed035abSart 
2155e464495eSniklas /* XXX XXX BEGIN XXX XXX */
2156aed035abSart paddr_t alpha_XXX_dmamap_or;					/* XXX */
2157e464495eSniklas 								/* XXX */
2158aed035abSart paddr_t								/* XXX */
2159e464495eSniklas alpha_XXX_dmamap(v)						/* XXX */
2160aed035abSart 	vaddr_t v;						/* XXX */
2161e464495eSniklas {								/* XXX */
2162e464495eSniklas 								/* XXX */
2163e464495eSniklas 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
2164e464495eSniklas }								/* XXX */
2165e464495eSniklas /* XXX XXX END XXX XXX */
2166