xref: /openbsd/sys/arch/alpha/alpha/machdep.c (revision e99873aa)
1*e99873aaSderaadt /* $OpenBSD: machdep.c,v 1.174 2016/05/21 00:56:41 deraadt 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  *
21aed035abSart  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22aed035abSart  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23aed035abSart  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24aed035abSart  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25aed035abSart  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26aed035abSart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27aed035abSart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28aed035abSart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29aed035abSart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30aed035abSart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31aed035abSart  * POSSIBILITY OF SUCH DAMAGE.
32aed035abSart  */
33df930be7Sderaadt 
34df930be7Sderaadt /*
35417eba8cSderaadt  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
36df930be7Sderaadt  * All rights reserved.
37df930be7Sderaadt  *
38df930be7Sderaadt  * Author: Chris G. Demetriou
39df930be7Sderaadt  *
40df930be7Sderaadt  * Permission to use, copy, modify and distribute this software and
41df930be7Sderaadt  * its documentation is hereby granted, provided that both the copyright
42df930be7Sderaadt  * notice and this permission notice appear in all copies of the
43df930be7Sderaadt  * software, derivative works or modified versions, and any portions
44df930be7Sderaadt  * thereof, and that both notices appear in supporting documentation.
45df930be7Sderaadt  *
46df930be7Sderaadt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df930be7Sderaadt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df930be7Sderaadt  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df930be7Sderaadt  *
50df930be7Sderaadt  * Carnegie Mellon requests users of this software to return to
51df930be7Sderaadt  *
52df930be7Sderaadt  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df930be7Sderaadt  *  School of Computer Science
54df930be7Sderaadt  *  Carnegie Mellon University
55df930be7Sderaadt  *  Pittsburgh PA 15213-3890
56df930be7Sderaadt  *
57df930be7Sderaadt  * any improvements or extensions that they make and grant Carnegie the
58df930be7Sderaadt  * rights to redistribute these changes.
59df930be7Sderaadt  */
60df930be7Sderaadt 
61df930be7Sderaadt #include <sys/param.h>
62df930be7Sderaadt #include <sys/systm.h>
63df930be7Sderaadt #include <sys/signalvar.h>
64df930be7Sderaadt #include <sys/kernel.h>
65df930be7Sderaadt #include <sys/proc.h>
667d9ca166Sderaadt #include <sys/socket.h>
672a2685f2Sart #include <sys/sched.h>
68df930be7Sderaadt #include <sys/buf.h>
69df930be7Sderaadt #include <sys/reboot.h>
70417eba8cSderaadt #include <sys/device.h>
71df930be7Sderaadt #include <sys/conf.h>
72df930be7Sderaadt #include <sys/file.h>
73d66eba84Sart #include <sys/timeout.h>
74df930be7Sderaadt #include <sys/malloc.h>
75df930be7Sderaadt #include <sys/mbuf.h>
76df930be7Sderaadt #include <sys/msgbuf.h>
77df930be7Sderaadt #include <sys/ioctl.h>
78df930be7Sderaadt #include <sys/tty.h>
79df930be7Sderaadt #include <sys/user.h>
80df930be7Sderaadt #include <sys/exec.h>
81df930be7Sderaadt #include <sys/sysctl.h>
8250ce9ee0Sniklas #include <sys/core.h>
8350ce9ee0Sniklas #include <sys/kcore.h>
847d9ca166Sderaadt 
857d9ca166Sderaadt #include <net/if.h>
867d9ca166Sderaadt #include <uvm/uvm.h>
877d9ca166Sderaadt 
8850ce9ee0Sniklas #include <machine/kcore.h>
89433075b6Spvalchev #ifndef NO_IEEE
90433075b6Spvalchev #include <machine/fpu.h>
91433075b6Spvalchev #endif
921f87e7b7Sart #include <sys/timetc.h>
93df930be7Sderaadt 
94df930be7Sderaadt #include <sys/mount.h>
95df930be7Sderaadt #include <sys/syscallargs.h>
96df930be7Sderaadt 
97df930be7Sderaadt #include <dev/cons.h>
98df930be7Sderaadt 
9950ce9ee0Sniklas #include <machine/autoconf.h>
100df930be7Sderaadt #include <machine/cpu.h>
101df930be7Sderaadt #include <machine/reg.h>
102df930be7Sderaadt #include <machine/rpb.h>
103df930be7Sderaadt #include <machine/prom.h>
1043a630e3fSniklas #include <machine/cpuconf.h>
105433075b6Spvalchev #ifndef NO_IEEE
106433075b6Spvalchev #include <machine/ieeefp.h>
107433075b6Spvalchev #endif
108247cdf31Sdlg #ifdef MULTIPROCESSOR
109247cdf31Sdlg #include <machine/lock.h>
110247cdf31Sdlg #endif
111df930be7Sderaadt 
11245e5a1a0Sart #include <dev/pci/pcivar.h>
11345e5a1a0Sart 
11412f8bbedSniklas #ifdef DDB
11512f8bbedSniklas #include <machine/db_machdep.h>
11612f8bbedSniklas #include <ddb/db_extern.h>
11721c23d01Smiod #include <ddb/db_interface.h>
11812f8bbedSniklas #endif
11912f8bbedSniklas 
120a072164aSmiod #include "ioasic.h"
121a072164aSmiod 
122a072164aSmiod #if NIOASIC > 0
123a072164aSmiod #include <machine/tc_machdep.h>
124a072164aSmiod #include <dev/tc/tcreg.h>
125a072164aSmiod #include <dev/tc/ioasicvar.h>
126a072164aSmiod #endif
127a072164aSmiod 
128c4071fd1Smillert int	cpu_dump(void);
129c4071fd1Smillert int	cpu_dumpsize(void);
130c4071fd1Smillert u_long	cpu_dump_mempagecnt(void);
131c4071fd1Smillert void	dumpsys(void);
132c4071fd1Smillert void	identifycpu(void);
133c4071fd1Smillert void	regdump(struct trapframe *framep);
134c4071fd1Smillert void	printregs(struct reg *);
135df930be7Sderaadt 
136b426ab7bSthib struct uvm_constraint_range  isa_constraint = { 0x0, 0x00ffffffUL };
137b426ab7bSthib struct uvm_constraint_range  dma_constraint = { 0x0, (paddr_t)-1 };
138b426ab7bSthib struct uvm_constraint_range *uvm_md_constraints[] = {
139b426ab7bSthib 	&isa_constraint,
140b426ab7bSthib 	NULL
141b426ab7bSthib };
142b426ab7bSthib 
143ab8e80c5Sart struct vm_map *exec_map = NULL;
144ab8e80c5Sart struct vm_map *phys_map = NULL;
145aed035abSart 
146c72644a3Sderaadt /*
147c72644a3Sderaadt  * safepri is a safe priority for sleep to set for a spin-wait
148c72644a3Sderaadt  * during autoconfiguration or after a panic.
149c72644a3Sderaadt  */
150c72644a3Sderaadt int   safepri = 0;
151c72644a3Sderaadt 
15227626149Smatthieu #ifdef APERTURE
15327626149Smatthieu #ifdef INSECURE
15427626149Smatthieu int allowaperture = 1;
15527626149Smatthieu #else
15627626149Smatthieu int allowaperture = 0;
15727626149Smatthieu #endif
15827626149Smatthieu #endif
15927626149Smatthieu 
160df930be7Sderaadt int	totalphysmem;		/* total amount of physical memory in system */
16174652a67Sniklas int	physmem;		/* physical mem used by OpenBSD + some rsvd */
162df930be7Sderaadt int	resvmem;		/* amount of memory reserved for PROM */
163df930be7Sderaadt int	unusedmem;		/* amount of memory for OS that we don't use */
164df930be7Sderaadt int	unknownmem;		/* amount of memory with an unknown use */
165df930be7Sderaadt 
166df930be7Sderaadt int	cputype;		/* system type, from the RPB */
167df930be7Sderaadt 
1682a2685f2Sart int	bootdev_debug = 0;	/* patchable, or from DDB */
1692a2685f2Sart 
170df930be7Sderaadt /* the following is used externally (sysctl_hw) */
171aed035abSart char	machine[] = MACHINE;		/* from <machine/param.h> */
172417eba8cSderaadt char	cpu_model[128];
173df930be7Sderaadt 
174df930be7Sderaadt struct	user *proc0paddr;
175df930be7Sderaadt 
176df930be7Sderaadt /* Number of machine cycles per microsecond */
177df930be7Sderaadt u_int64_t	cycles_per_usec;
178df930be7Sderaadt 
179aed035abSart struct bootinfo_kernel bootinfo;
180aed035abSart 
181b1560ceaSmiod struct consdev *cn_tab;
182b1560ceaSmiod 
183aed035abSart /* For built-in TCDS */
184aed035abSart #if defined(DEC_3000_300) || defined(DEC_3000_500)
185aed035abSart u_int8_t	dec_3000_scsiid[2], dec_3000_scsifast[2];
186aed035abSart #endif
187aed035abSart 
188aed035abSart struct platform platform;
189417eba8cSderaadt 
190417eba8cSderaadt /* for cpu_sysctl() */
19150ce9ee0Sniklas int	alpha_unaligned_print = 1;	/* warn about unaligned accesses */
19250ce9ee0Sniklas int	alpha_unaligned_fix = 1;	/* fix up unaligned accesses */
193881c1eabSart int	alpha_unaligned_sigbus = 1;	/* SIGBUS on fixed-up accesses */
194433075b6Spvalchev #ifndef NO_IEEE
195433075b6Spvalchev int	alpha_fp_sync_complete = 0;	/* fp fixup if sync even without /s */
196433075b6Spvalchev #endif
197a072164aSmiod #if NIOASIC > 0
19891c0e687Sderaadt int	alpha_led_blink = 1;
199a072164aSmiod #endif
20050ce9ee0Sniklas 
201b3cee53eSmartin /* used by hw_sysctl */
202b3cee53eSmartin extern char *hw_serial;
203b3cee53eSmartin 
204aed035abSart /*
205aed035abSart  * XXX This should be dynamically sized, but we have the chicken-egg problem!
206aed035abSart  * XXX it should also be larger than it is, because not all of the mddt
207aed035abSart  * XXX clusters end up being used for VM.
208aed035abSart  */
209aed035abSart phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];	/* low size bits overloaded */
210aed035abSart int	mem_cluster_cnt;
211aed035abSart 
2123a630e3fSniklas void
213dd3e8537Smiod alpha_init(unused, ptb, bim, bip, biv)
214dd3e8537Smiod 	u_long unused;
215df930be7Sderaadt 	u_long ptb;		/* PFN of current level 1 page table */
216aed035abSart 	u_long bim;		/* bootinfo magic */
217aed035abSart 	u_long bip;		/* bootinfo pointer */
218aed035abSart 	u_long biv;		/* bootinfo version */
219df930be7Sderaadt {
220aed035abSart 	extern char kernel_text[], _end[];
221df930be7Sderaadt 	struct mddt *mddtp;
222aed035abSart 	struct mddt_cluster *memc;
223df930be7Sderaadt 	int i, mddtweird;
224aed035abSart 	struct vm_physseg *vps;
225aed035abSart 	vaddr_t kernstart, kernend;
226aed035abSart 	paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
227df930be7Sderaadt 	char *p;
2282a2685f2Sart 	const char *bootinfo_msg;
229aed035abSart 	const struct cpuinit *c;
230aed035abSart 	extern caddr_t esym;
231aed035abSart 	struct cpu_info *ci;
232aed035abSart 	cpuid_t cpu_id;
233df930be7Sderaadt 
234aed035abSart 	/* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
235f3914c62Sniklas 
236df930be7Sderaadt 	/*
237aed035abSart 	 * Turn off interrupts (not mchecks) and floating point.
238df930be7Sderaadt 	 * Make sure the instruction and data streams are consistent.
239df930be7Sderaadt 	 */
240aed035abSart 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
24150ce9ee0Sniklas 	alpha_pal_wrfen(0);
24250ce9ee0Sniklas 	ALPHA_TBIA();
24350ce9ee0Sniklas 	alpha_pal_imb();
244df930be7Sderaadt 
245c62181b1Sbrad 	/* Initialize the SCB. */
246c62181b1Sbrad 	scb_init();
247c62181b1Sbrad 
248aed035abSart 	cpu_id = cpu_number();
249aed035abSart 
250aed035abSart #if defined(MULTIPROCESSOR)
251df930be7Sderaadt 	/*
252aed035abSart 	 * Set our SysValue to the address of our cpu_info structure.
253aed035abSart 	 * Secondary processors do this in their spinup trampoline.
254df930be7Sderaadt 	 */
25521c23d01Smiod 	alpha_pal_wrval((u_long)&cpu_info_primary);
25621c23d01Smiod 	cpu_info[cpu_id] = &cpu_info_primary;
257aed035abSart #endif
258aed035abSart 
259aed035abSart 	ci = curcpu();
260aed035abSart 	ci->ci_cpuid = cpu_id;
261aed035abSart 
262aed035abSart 	/*
263aed035abSart 	 * Get critical system information (if possible, from the
264aed035abSart 	 * information provided by the boot program).
265aed035abSart 	 */
266aed035abSart 	bootinfo_msg = NULL;
267aed035abSart 	if (bim == BOOTINFO_MAGIC) {
268aed035abSart 		if (biv == 0) {		/* backward compat */
269aed035abSart 			biv = *(u_long *)bip;
270aed035abSart 			bip += 8;
271aed035abSart 		}
272aed035abSart 		switch (biv) {
273aed035abSart 		case 1: {
274aed035abSart 			struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
275aed035abSart 
276aed035abSart 			bootinfo.ssym = v1p->ssym;
277aed035abSart 			bootinfo.esym = v1p->esym;
278aed035abSart 			/* hwrpb may not be provided by boot block in v1 */
279aed035abSart 			if (v1p->hwrpb != NULL) {
280aed035abSart 				bootinfo.hwrpb_phys =
281aed035abSart 				    ((struct rpb *)v1p->hwrpb)->rpb_phys;
282aed035abSart 				bootinfo.hwrpb_size = v1p->hwrpbsize;
283aed035abSart 			} else {
284aed035abSart 				bootinfo.hwrpb_phys =
285aed035abSart 				    ((struct rpb *)HWRPB_ADDR)->rpb_phys;
286aed035abSart 				bootinfo.hwrpb_size =
287aed035abSart 				    ((struct rpb *)HWRPB_ADDR)->rpb_size;
288aed035abSart 			}
289aed035abSart 			bcopy(v1p->boot_flags, bootinfo.boot_flags,
290aed035abSart 			    min(sizeof v1p->boot_flags,
291aed035abSart 			      sizeof bootinfo.boot_flags));
292aed035abSart 			bcopy(v1p->booted_kernel, bootinfo.booted_kernel,
293aed035abSart 			    min(sizeof v1p->booted_kernel,
294aed035abSart 			      sizeof bootinfo.booted_kernel));
295aed035abSart 			/* booted dev not provided in bootinfo */
296aed035abSart 			init_prom_interface((struct rpb *)
297aed035abSart 			    ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
298aed035abSart                 	prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
299aed035abSart 			    sizeof bootinfo.booted_dev);
300aed035abSart 			break;
301aed035abSart 		}
302aed035abSart 		default:
303aed035abSart 			bootinfo_msg = "unknown bootinfo version";
304aed035abSart 			goto nobootinfo;
305aed035abSart 		}
306aed035abSart 	} else {
307aed035abSart 		bootinfo_msg = "boot program did not pass bootinfo";
308aed035abSart nobootinfo:
309aed035abSart 		bootinfo.ssym = (u_long)_end;
310aed035abSart 		bootinfo.esym = (u_long)_end;
311aed035abSart 		bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
312aed035abSart 		bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
313aed035abSart 		init_prom_interface((struct rpb *)HWRPB_ADDR);
314aed035abSart 		prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
315aed035abSart 		    sizeof bootinfo.boot_flags);
316aed035abSart 		prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
317aed035abSart 		    sizeof bootinfo.booted_kernel);
318aed035abSart 		prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
319aed035abSart 		    sizeof bootinfo.booted_dev);
320aed035abSart 	}
321aed035abSart 
322aed035abSart 	esym = (caddr_t)bootinfo.esym;
323aed035abSart 	/*
324aed035abSart 	 * Initialize the kernel's mapping of the RPB.  It's needed for
325aed035abSart 	 * lots of things.
326aed035abSart 	 */
327aed035abSart 	hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
328aed035abSart 
329aed035abSart #if defined(DEC_3000_300) || defined(DEC_3000_500)
330aed035abSart 	if (hwrpb->rpb_type == ST_DEC_3000_300 ||
331aed035abSart 	    hwrpb->rpb_type == ST_DEC_3000_500) {
332aed035abSart 		prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
333aed035abSart 		    sizeof(dec_3000_scsiid));
334aed035abSart 		prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
335aed035abSart 		    sizeof(dec_3000_scsifast));
336aed035abSart 	}
337aed035abSart #endif
338df930be7Sderaadt 
339df930be7Sderaadt 	/*
340df930be7Sderaadt 	 * Remember how many cycles there are per microsecond,
341df930be7Sderaadt 	 * so that we can use delay().  Round up, for safety.
342df930be7Sderaadt 	 */
343df930be7Sderaadt 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
344df930be7Sderaadt 
345df930be7Sderaadt 	/*
3469e71c994Saaron 	 * Initialize the (temporary) bootstrap console interface, so
347aed035abSart 	 * we can use printf until the VM system starts being setup.
348aed035abSart 	 * The real console is initialized before then.
349df930be7Sderaadt 	 */
350aed035abSart 	init_bootstrap_console();
351aed035abSart 
352aed035abSart 	/* OUTPUT NOW ALLOWED */
353aed035abSart 
354aed035abSart 	/* delayed from above */
355aed035abSart 	if (bootinfo_msg)
356aed035abSart 		printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
357aed035abSart 		    bootinfo_msg, bim, bip, biv);
358aed035abSart 
359aed035abSart 	/* Initialize the trap vectors on the primary processor. */
360aed035abSart 	trap_init();
361df930be7Sderaadt 
362df930be7Sderaadt 	/*
363aed035abSart 	 * Find out what hardware we're on, and do basic initialization.
364df930be7Sderaadt 	 */
365aed035abSart 	cputype = hwrpb->rpb_type;
366aed035abSart 	if (cputype < 0) {
367aed035abSart 		/*
368aed035abSart 		 * At least some white-box systems have SRM which
369aed035abSart 		 * reports a systype that's the negative of their
370aed035abSart 		 * blue-box counterpart.
371aed035abSart 		 */
372aed035abSart 		cputype = -cputype;
373aed035abSart 	}
374aed035abSart 	c = platform_lookup(cputype);
375aed035abSart 	if (c == NULL) {
376aed035abSart 		platform_not_supported();
377aed035abSart 		/* NOTREACHED */
378aed035abSart 	}
379aed035abSart 	(*c->init)();
380094fa01fSderaadt 	strlcpy(cpu_model, platform.model, sizeof cpu_model);
38150ce9ee0Sniklas 
38250ce9ee0Sniklas 	/*
3839e71c994Saaron 	 * Initialize the real console, so that the bootstrap console is
384aed035abSart 	 * no longer necessary.
38550ce9ee0Sniklas 	 */
386aed035abSart 	(*platform.cons_init)();
387aed035abSart 
388a55851f4Sderaadt #if 0
389aed035abSart 	/* Paranoid sanity checking */
390aed035abSart 
391aed035abSart 	assert(hwrpb->rpb_primary_cpu_id == alpha_pal_whami());
392aed035abSart 
393aed035abSart 	/*
394aed035abSart 	 * On single-CPU systypes, the primary should always be CPU 0,
395aed035abSart 	 * except on Alpha 8200 systems where the CPU id is related
396aed035abSart 	 * to the VID, which is related to the Turbo Laser node id.
397aed035abSart 	 */
398aed035abSart 	if (cputype != ST_DEC_21000)
399aed035abSart 		assert(hwrpb->rpb_primary_cpu_id == 0);
400aed035abSart #endif
401aed035abSart 
402aed035abSart 	/* NO MORE FIRMWARE ACCESS ALLOWED */
403aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
404aed035abSart 	/*
405aed035abSart 	 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
406aed035abSart 	 * XXX pmap_uses_prom_console() evaluates to non-zero.)
407aed035abSart 	 */
408aed035abSart #endif
409aed035abSart 
410cfcdef40Smiod #ifndef SMALL_KERNEL
411cfcdef40Smiod 	/*
412cfcdef40Smiod 	 * If we run on a BWX-capable processor, override cpu_switch
413cfcdef40Smiod 	 * with a faster version.
414cfcdef40Smiod 	 * We do this now because the kernel text might be mapped
415cfcdef40Smiod 	 * read-only eventually (although this is not the case at the moment).
416cfcdef40Smiod 	 */
417cfcdef40Smiod 	if (alpha_implver() >= ALPHA_IMPLVER_EV5) {
418cfcdef40Smiod 		if (~alpha_amask(ALPHA_AMASK_BWX) != 0) {
419cfcdef40Smiod 			extern vaddr_t __bwx_switch0, __bwx_switch1,
420cfcdef40Smiod 			    __bwx_switch2, __bwx_switch3;
421cfcdef40Smiod 			u_int32_t *dst, *src, *end;
422cfcdef40Smiod 
423cfcdef40Smiod 			src = (u_int32_t *)&__bwx_switch2;
424cfcdef40Smiod 			end = (u_int32_t *)&__bwx_switch3;
425cfcdef40Smiod 			dst = (u_int32_t *)&__bwx_switch0;
426cfcdef40Smiod 			while (src != end)
427cfcdef40Smiod 				*dst++ = *src++;
428cfcdef40Smiod 			src = (u_int32_t *)&__bwx_switch1;
429cfcdef40Smiod 			end = (u_int32_t *)&__bwx_switch2;
430cfcdef40Smiod 			while (src != end)
431cfcdef40Smiod 				*dst++ = *src++;
432cfcdef40Smiod 		}
433cfcdef40Smiod 	}
434cfcdef40Smiod #endif
435cfcdef40Smiod 
436aed035abSart 	/*
437aed035abSart 	 * find out this system's page size
438aed035abSart 	 */
43973b9fe7cSart 	if ((uvmexp.pagesize = hwrpb->rpb_page_size) != 8192)
44073b9fe7cSart 		panic("page size %d != 8192?!", uvmexp.pagesize);
441aed035abSart 
442aed035abSart 	uvm_setpagesize();
443aed035abSart 
444aed035abSart 	/*
445aed035abSart 	 * Find the beginning and end of the kernel (and leave a
446aed035abSart 	 * bit of space before the beginning for the bootstrap
447aed035abSart 	 * stack).
448aed035abSart 	 */
449aed035abSart 	kernstart = trunc_page((vaddr_t)kernel_text) - 2 * PAGE_SIZE;
450aed035abSart 	kernend = (vaddr_t)round_page((vaddr_t)bootinfo.esym);
451aed035abSart 
452aed035abSart 	kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
453aed035abSart 	kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
454df930be7Sderaadt 
455df930be7Sderaadt 	/*
456df930be7Sderaadt 	 * Find out how much memory is available, by looking at
457df930be7Sderaadt 	 * the memory cluster descriptors.  This also tries to do
458df930be7Sderaadt 	 * its best to detect things things that have never been seen
459df930be7Sderaadt 	 * before...
460df930be7Sderaadt 	 */
461df930be7Sderaadt 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
462df930be7Sderaadt 
463aed035abSart 	/* MDDT SANITY CHECKING */
464df930be7Sderaadt 	mddtweird = 0;
465aed035abSart 	if (mddtp->mddt_cluster_cnt < 2) {
466df930be7Sderaadt 		mddtweird = 1;
467aed035abSart 		printf("WARNING: weird number of mem clusters: %lu\n",
468e86d96d5Smiod 		    (unsigned long)mddtp->mddt_cluster_cnt);
469df930be7Sderaadt 	}
470df930be7Sderaadt 
471aed035abSart #if 0
472aed035abSart 	printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
473aed035abSart #endif
474df930be7Sderaadt 
475aed035abSart 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
476aed035abSart 		memc = &mddtp->mddt_clusters[i];
477aed035abSart #if 0
478aed035abSart 		printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
479aed035abSart 		    memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
480aed035abSart #endif
481aed035abSart 		totalphysmem += memc->mddt_pg_cnt;
482aed035abSart 		if (mem_cluster_cnt < VM_PHYSSEG_MAX) {	/* XXX */
483aed035abSart 			mem_clusters[mem_cluster_cnt].start =
484aed035abSart 			    ptoa(memc->mddt_pfn);
485aed035abSart 			mem_clusters[mem_cluster_cnt].size =
486aed035abSart 			    ptoa(memc->mddt_pg_cnt);
487aed035abSart 			if (memc->mddt_usage & MDDT_mbz ||
488aed035abSart 			    memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
489aed035abSart 			    memc->mddt_usage & MDDT_PALCODE)
490aed035abSart 				mem_clusters[mem_cluster_cnt].size |=
4911e8cdc2eSderaadt 				    PROT_READ;
492aed035abSart 			else
493aed035abSart 				mem_clusters[mem_cluster_cnt].size |=
4941e8cdc2eSderaadt 				    PROT_READ | PROT_WRITE | PROT_EXEC;
495aed035abSart 			mem_cluster_cnt++;
496ee2d823aSmiod 		} /* XXX else print something! */
497aed035abSart 
498aed035abSart 		if (memc->mddt_usage & MDDT_mbz) {
499aed035abSart 			mddtweird = 1;
500aed035abSart 			printf("WARNING: mem cluster %d has weird "
501e86d96d5Smiod 			    "usage 0x%lx\n", i, (long)memc->mddt_usage);
502aed035abSart 			unknownmem += memc->mddt_pg_cnt;
503aed035abSart 			continue;
504aed035abSart 		}
505aed035abSart 		if (memc->mddt_usage & MDDT_NONVOLATILE) {
506aed035abSart 			/* XXX should handle these... */
507aed035abSart 			printf("WARNING: skipping non-volatile mem "
508aed035abSart 			    "cluster %d\n", i);
509aed035abSart 			unusedmem += memc->mddt_pg_cnt;
510aed035abSart 			continue;
511aed035abSart 		}
512aed035abSart 		if (memc->mddt_usage & MDDT_PALCODE) {
513aed035abSart 			resvmem += memc->mddt_pg_cnt;
514aed035abSart 			continue;
515aed035abSart 		}
516aed035abSart 
517aed035abSart 		/*
518aed035abSart 		 * We have a memory cluster available for system
519aed035abSart 		 * software use.  We must determine if this cluster
520aed035abSart 		 * holds the kernel.
521aed035abSart 		 */
522aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
523aed035abSart 		/*
524aed035abSart 		 * XXX If the kernel uses the PROM console, we only use the
525aed035abSart 		 * XXX memory after the kernel in the first system segment,
526aed035abSart 		 * XXX to avoid clobbering prom mapping, data, etc.
527aed035abSart 		 */
528aed035abSart 	    if (!pmap_uses_prom_console() || physmem == 0) {
529aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
530aed035abSart 		physmem += memc->mddt_pg_cnt;
531aed035abSart 		pfn0 = memc->mddt_pfn;
532aed035abSart 		pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
533aed035abSart 		if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
534aed035abSart 			/*
535aed035abSart 			 * Must compute the location of the kernel
536aed035abSart 			 * within the segment.
537aed035abSart 			 */
538aed035abSart #if 0
539aed035abSart 			printf("Cluster %d contains kernel\n", i);
540aed035abSart #endif
541aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
542aed035abSart 		    if (!pmap_uses_prom_console()) {
543aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
544aed035abSart 			if (pfn0 < kernstartpfn) {
545aed035abSart 				/*
546aed035abSart 				 * There is a chunk before the kernel.
547aed035abSart 				 */
548aed035abSart #if 0
549aed035abSart 				printf("Loading chunk before kernel: "
550aed035abSart 				    "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
551aed035abSart #endif
552aed035abSart 				uvm_page_physload(pfn0, kernstartpfn,
5532ce3b4a8Soga 				    pfn0, kernstartpfn, 0);
554aed035abSart 			}
555aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
556aed035abSart 		    }
557aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
558aed035abSart 			if (kernendpfn < pfn1) {
559aed035abSart 				/*
560aed035abSart 				 * There is a chunk after the kernel.
561aed035abSart 				 */
562aed035abSart #if 0
563aed035abSart 				printf("Loading chunk after kernel: "
564aed035abSart 				    "0x%lx / 0x%lx\n", kernendpfn, pfn1);
565aed035abSart #endif
566aed035abSart 				uvm_page_physload(kernendpfn, pfn1,
5672ce3b4a8Soga 				    kernendpfn, pfn1, 0);
568aed035abSart 			}
569aed035abSart 		} else {
570aed035abSart 			/*
571aed035abSart 			 * Just load this cluster as one chunk.
572aed035abSart 			 */
573aed035abSart #if 0
574aed035abSart 			printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
575aed035abSart 			    pfn0, pfn1);
576aed035abSart #endif
5772ce3b4a8Soga 			uvm_page_physload(pfn0, pfn1, pfn0, pfn1, 0);
578aed035abSart 		}
579aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
580aed035abSart 	    }
581aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
582aed035abSart 	}
583aed035abSart 
584a37778bcSderaadt #ifdef DEBUG
585aed035abSart 	/*
586aed035abSart 	 * Dump out the MDDT if it looks odd...
587aed035abSart 	 */
588df930be7Sderaadt 	if (mddtweird) {
589df930be7Sderaadt 		printf("\n");
590df930be7Sderaadt 		printf("complete memory cluster information:\n");
591df930be7Sderaadt 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
592df930be7Sderaadt 			printf("mddt %d:\n", i);
593df930be7Sderaadt 			printf("\tpfn %lx\n",
594df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pfn);
595df930be7Sderaadt 			printf("\tcnt %lx\n",
596df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
597df930be7Sderaadt 			printf("\ttest %lx\n",
598df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pg_test);
599df930be7Sderaadt 			printf("\tbva %lx\n",
600df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
601df930be7Sderaadt 			printf("\tbpa %lx\n",
602df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
603df930be7Sderaadt 			printf("\tbcksum %lx\n",
604df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
605df930be7Sderaadt 			printf("\tusage %lx\n",
606df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_usage);
607df930be7Sderaadt 		}
608df930be7Sderaadt 		printf("\n");
609df930be7Sderaadt 	}
610a37778bcSderaadt #endif
611df930be7Sderaadt 
612df930be7Sderaadt 	if (totalphysmem == 0)
613df930be7Sderaadt 		panic("can't happen: system seems to have no memory!");
614df930be7Sderaadt #if 0
615f46637d1Sderaadt 	printf("totalphysmem = %u\n", totalphysmem);
616f46637d1Sderaadt 	printf("physmem = %u\n", physmem);
617df930be7Sderaadt 	printf("resvmem = %d\n", resvmem);
618df930be7Sderaadt 	printf("unusedmem = %d\n", unusedmem);
619df930be7Sderaadt 	printf("unknownmem = %d\n", unknownmem);
620df930be7Sderaadt #endif
621df930be7Sderaadt 
622df930be7Sderaadt 	/*
623aed035abSart 	 * Initialize error message buffer (at end of core).
624df930be7Sderaadt 	 */
625aed035abSart 	{
626aed035abSart 		vsize_t sz = (vsize_t)round_page(MSGBUFSIZE);
627aed035abSart 		vsize_t reqsz = sz;
628df930be7Sderaadt 
629aed035abSart 		vps = &vm_physmem[vm_nphysseg - 1];
630e1da84e1Salex 
631aed035abSart 		/* shrink so that it'll fit in the last segment */
632aed035abSart 		if ((vps->avail_end - vps->avail_start) < atop(sz))
633aed035abSart 			sz = ptoa(vps->avail_end - vps->avail_start);
634aed035abSart 
635aed035abSart 		vps->end -= atop(sz);
636aed035abSart 		vps->avail_end -= atop(sz);
637aed035abSart 		initmsgbuf((caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end)), sz);
638aed035abSart 
639aed035abSart 		/* Remove the last segment if it now has no pages. */
640aed035abSart 		if (vps->start == vps->end)
641aed035abSart 			vm_nphysseg--;
642aed035abSart 
643aed035abSart 		/* warn if the message buffer had to be shrunk */
644aed035abSart 		if (sz != reqsz)
645aed035abSart 			printf("WARNING: %ld bytes not available for msgbuf "
646aed035abSart 			    "in last cluster (%ld used)\n", reqsz, sz);
647aed035abSart 
648aed035abSart 	}
649aed035abSart 
650df930be7Sderaadt 	/*
651df930be7Sderaadt 	 * Init mapping for u page(s) for proc 0
652df930be7Sderaadt 	 */
653aed035abSart 	proc0.p_addr = proc0paddr =
654aed035abSart 	    (struct user *)pmap_steal_memory(UPAGES * PAGE_SIZE, NULL, NULL);
655df930be7Sderaadt 
656df930be7Sderaadt 	/*
657df930be7Sderaadt 	 * Initialize the virtual memory system, and set the
658df930be7Sderaadt 	 * page table base register in proc 0's PCB.
659df930be7Sderaadt 	 */
660aed035abSart 	pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
661aed035abSart 	    hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
662df930be7Sderaadt 
663df930be7Sderaadt 	/*
664df930be7Sderaadt 	 * Initialize the rest of proc 0's PCB, and cache its physical
665df930be7Sderaadt 	 * address.
666df930be7Sderaadt 	 */
667df930be7Sderaadt 	proc0.p_md.md_pcbpaddr =
668aed035abSart 	    (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
669df930be7Sderaadt 
670df930be7Sderaadt 	/*
671df930be7Sderaadt 	 * Set the kernel sp, reserving space for an (empty) trapframe,
672df930be7Sderaadt 	 * and make proc0's trapframe pointer point to it for sanity.
673df930be7Sderaadt 	 */
67450ce9ee0Sniklas 	proc0paddr->u_pcb.pcb_hw.apcb_ksp =
675df930be7Sderaadt 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
67674652a67Sniklas 	proc0.p_md.md_tf =
67774652a67Sniklas 	    (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
67850ce9ee0Sniklas 
679aed035abSart 	/*
680aed035abSart 	 * Initialize the primary CPU's idle PCB to proc0's.  In a
681aed035abSart 	 * MULTIPROCESSOR configuration, each CPU will later get
682aed035abSart 	 * its own idle PCB when autoconfiguration runs.
683aed035abSart 	 */
684aed035abSart 	ci->ci_idle_pcb = &proc0paddr->u_pcb;
685aed035abSart 	ci->ci_idle_pcb_paddr = (u_long)proc0.p_md.md_pcbpaddr;
686df930be7Sderaadt 
687df930be7Sderaadt 	/*
688df930be7Sderaadt 	 * Look at arguments passed to us and compute boothowto.
689df930be7Sderaadt 	 */
690417eba8cSderaadt 
691df930be7Sderaadt #ifdef KADB
692df930be7Sderaadt 	boothowto |= RB_KDB;
693df930be7Sderaadt #endif
694aed035abSart 	for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
695417eba8cSderaadt 		/*
696417eba8cSderaadt 		 * Note that we'd really like to differentiate case here,
697417eba8cSderaadt 		 * but the Alpha AXP Architecture Reference Manual
698417eba8cSderaadt 		 * says that we shouldn't.
699417eba8cSderaadt 		 */
700df930be7Sderaadt 		switch (*p) {
701371c77f5Smartin 		case 'a': /* Ignore */
702417eba8cSderaadt 		case 'A':
703df930be7Sderaadt 			break;
704df930be7Sderaadt 
70512f8bbedSniklas 		case 'b': /* Enter DDB as soon as the console is initialised */
70612f8bbedSniklas 		case 'B':
70712f8bbedSniklas 			boothowto |= RB_KDB;
70812f8bbedSniklas 			break;
70912f8bbedSniklas 
71050ce9ee0Sniklas 		case 'c': /* enter user kernel configuration */
71150ce9ee0Sniklas 		case 'C':
71250ce9ee0Sniklas 			boothowto |= RB_CONFIG;
71350ce9ee0Sniklas 			break;
71450ce9ee0Sniklas 
71550ce9ee0Sniklas #ifdef DEBUG
71650ce9ee0Sniklas 		case 'd': /* crash dump immediately after autoconfig */
71750ce9ee0Sniklas 		case 'D':
71850ce9ee0Sniklas 			boothowto |= RB_DUMP;
71950ce9ee0Sniklas 			break;
72050ce9ee0Sniklas #endif
72150ce9ee0Sniklas 
72250ce9ee0Sniklas 		case 'h': /* always halt, never reboot */
72350ce9ee0Sniklas 		case 'H':
72450ce9ee0Sniklas 			boothowto |= RB_HALT;
725df930be7Sderaadt 			break;
726df930be7Sderaadt 
72750ce9ee0Sniklas 
72850ce9ee0Sniklas 		case 'n': /* askname */
72950ce9ee0Sniklas 		case 'N':
73050ce9ee0Sniklas 			boothowto |= RB_ASKNAME;
73150ce9ee0Sniklas 			break;
732aed035abSart 
733371c77f5Smartin 		case 's': /* single-user */
734aed035abSart 		case 'S':
735aed035abSart 			boothowto |= RB_SINGLE;
736aed035abSart 			break;
737aed035abSart 
738aed035abSart 		case '-':
739aed035abSart 			/*
740aed035abSart 			 * Just ignore this.  It's not required, but it's
741aed035abSart 			 * common for it to be passed regardless.
742aed035abSart 			 */
743aed035abSart 			break;
744aed035abSart 
745aed035abSart 		default:
746aed035abSart 			printf("Unrecognized boot flag '%c'.\n", *p);
747aed035abSart 			break;
748df930be7Sderaadt 		}
749df930be7Sderaadt 	}
750df930be7Sderaadt 
751aed035abSart 
752df930be7Sderaadt 	/*
753df930be7Sderaadt 	 * Figure out the number of cpus in the box, from RPB fields.
754df930be7Sderaadt 	 * Really.  We mean it.
755df930be7Sderaadt 	 */
75631326ec3Smiod 	for (ncpusfound = 0, i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
757df930be7Sderaadt 		struct pcs *pcsp;
758df930be7Sderaadt 
759aed035abSart 		pcsp = LOCATE_PCS(hwrpb, i);
760df930be7Sderaadt 		if ((pcsp->pcs_flags & PCS_PP) != 0)
76131326ec3Smiod 			ncpusfound++;
762df930be7Sderaadt 	}
763aed035abSart 
764aed035abSart 	/*
765aed035abSart 	 * Initialize debuggers, and break into them if appropriate.
766aed035abSart 	 */
767aed035abSart #ifdef DDB
76821c23d01Smiod 	db_machine_init();
769aed035abSart 	ddb_init();
770aed035abSart 
771aed035abSart 	if (boothowto & RB_KDB)
772aed035abSart 		Debugger();
773aed035abSart #endif
774aed035abSart #ifdef KGDB
775aed035abSart 	if (boothowto & RB_KDB)
776aed035abSart 		kgdb_connect(0);
777aed035abSart #endif
778aed035abSart 	/*
779aed035abSart 	 * Figure out our clock frequency, from RPB fields.
780aed035abSart 	 */
781aed035abSart 	hz = hwrpb->rpb_intr_freq >> 12;
782aed035abSart 	if (!(60 <= hz && hz <= 10240)) {
783aed035abSart #ifdef DIAGNOSTIC
784e86d96d5Smiod 		printf("WARNING: unbelievable rpb_intr_freq: %lu (%d hz)\n",
785e86d96d5Smiod 			(unsigned long)hwrpb->rpb_intr_freq, hz);
786aed035abSart #endif
787dd3e8537Smiod 		hz = 1024;
788aed035abSart 	}
789aed035abSart }
790aed035abSart 
791417eba8cSderaadt void
792df930be7Sderaadt consinit()
793df930be7Sderaadt {
794aed035abSart 
795aed035abSart 	/*
796aed035abSart 	 * Everything related to console initialization is done
797aed035abSart 	 * in alpha_init().
798aed035abSart 	 */
799aed035abSart #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
800aed035abSart 	printf("consinit: %susing prom console\n",
801aed035abSart 	    pmap_uses_prom_console() ? "" : "not ");
80212f8bbedSniklas #endif
803df930be7Sderaadt }
804df930be7Sderaadt 
805417eba8cSderaadt void
806df930be7Sderaadt cpu_startup()
807df930be7Sderaadt {
808aed035abSart 	vaddr_t minaddr, maxaddr;
80950ce9ee0Sniklas #if defined(DEBUG)
810df930be7Sderaadt 	extern int pmapdebug;
811df930be7Sderaadt 	int opmapdebug = pmapdebug;
812df930be7Sderaadt 
813df930be7Sderaadt 	pmapdebug = 0;
814df930be7Sderaadt #endif
815df930be7Sderaadt 
816df930be7Sderaadt 	/*
817df930be7Sderaadt 	 * Good {morning,afternoon,evening,night}.
818df930be7Sderaadt 	 */
819df930be7Sderaadt 	printf(version);
820df930be7Sderaadt 	identifycpu();
821701cd583Smiod 	printf("real mem = %lu (%luMB)\n", ptoa((psize_t)totalphysmem),
822701cd583Smiod 	    ptoa((psize_t)totalphysmem) / 1024 / 1024);
823701cd583Smiod 	printf("rsvd mem = %lu (%luMB)\n", ptoa((psize_t)resvmem),
824701cd583Smiod 	    ptoa((psize_t)resvmem) / 1024 / 1024);
825aed035abSart 	if (unusedmem) {
826701cd583Smiod 		printf("WARNING: unused memory = %lu (%luMB)\n",
827701cd583Smiod 		    ptoa((psize_t)unusedmem),
828701cd583Smiod 		    ptoa((psize_t)unusedmem) / 1024 / 1024);
829aed035abSart 	}
830aed035abSart 	if (unknownmem) {
831701cd583Smiod 		printf("WARNING: %lu (%luMB) of memory with unknown purpose\n",
832701cd583Smiod 		    ptoa((psize_t)unknownmem),
833701cd583Smiod 		    ptoa((psize_t)unknownmem) / 1024 / 1024);
834aed035abSart 	}
835df930be7Sderaadt 
836df930be7Sderaadt 	/*
837df930be7Sderaadt 	 * Allocate a submap for exec arguments.  This map effectively
838df930be7Sderaadt 	 * limits the number of processes exec'ing at any time.
839df930be7Sderaadt 	 */
8407c10a71dSdrahn 	minaddr = vm_map_min(kernel_map);
841aed035abSart 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
842aed035abSart 	    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
843df930be7Sderaadt 
844df930be7Sderaadt 	/*
845df930be7Sderaadt 	 * Allocate a submap for physio
846df930be7Sderaadt 	 */
847aed035abSart 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
848aed035abSart 	    VM_PHYS_SIZE, 0, FALSE, NULL);
849df930be7Sderaadt 
85050ce9ee0Sniklas #if defined(DEBUG)
851df930be7Sderaadt 	pmapdebug = opmapdebug;
852df930be7Sderaadt #endif
853701cd583Smiod 	printf("avail mem = %lu (%luMB)\n", ptoa((psize_t)uvmexp.free),
854701cd583Smiod 	    ptoa((psize_t)uvmexp.free) / 1024 / 1024);
855aed035abSart #if 0
856aed035abSart 	{
857aed035abSart 		extern u_long pmap_pages_stolen;
858aed035abSart 
859aed035abSart 		printf("stolen memory for VM structures = %d\n", pmap_pages_stolen * PAGE_SIZE);
860aed035abSart 	}
861aed035abSart #endif
862df930be7Sderaadt 
863df930be7Sderaadt 	/*
864df930be7Sderaadt 	 * Set up buffers, so they can be used to read disk labels.
865df930be7Sderaadt 	 */
866df930be7Sderaadt 	bufinit();
867df930be7Sderaadt 
868df930be7Sderaadt 	/*
869df930be7Sderaadt 	 * Configure the system.
870df930be7Sderaadt 	 */
87141033391Sderaadt 	if (boothowto & RB_CONFIG) {
87241033391Sderaadt #ifdef BOOT_CONFIG
87341033391Sderaadt 		user_config();
87441033391Sderaadt #else
87541033391Sderaadt 		printf("kernel does not support -c; continuing..\n");
87641033391Sderaadt #endif
87741033391Sderaadt 	}
87850ce9ee0Sniklas 
87950ce9ee0Sniklas 	/*
880aed035abSart 	 * Set up the HWPCB so that it's safe to configure secondary
881aed035abSart 	 * CPUs.
88250ce9ee0Sniklas 	 */
883aed035abSart 	hwrpb_primary_init();
884aed035abSart }
885aed035abSart 
886aed035abSart /*
887aed035abSart  * Retrieve the platform name from the DSR.
888aed035abSart  */
889aed035abSart const char *
890aed035abSart alpha_dsr_sysname()
891aed035abSart {
892aed035abSart 	struct dsrdb *dsr;
893aed035abSart 	const char *sysname;
894aed035abSart 
895aed035abSart 	/*
896aed035abSart 	 * DSR does not exist on early HWRPB versions.
897aed035abSart 	 */
898aed035abSart 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
899aed035abSart 		return (NULL);
900aed035abSart 
901aed035abSart 	dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
902aed035abSart 	sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
903aed035abSart 	    sizeof(u_int64_t)));
904aed035abSart 	return (sysname);
905aed035abSart }
906aed035abSart 
907aed035abSart /*
908aed035abSart  * Lookup the system specified system variation in the provided table,
909aed035abSart  * returning the model string on match.
910aed035abSart  */
911aed035abSart const char *
912aed035abSart alpha_variation_name(variation, avtp)
913aed035abSart 	u_int64_t variation;
914aed035abSart 	const struct alpha_variation_table *avtp;
915aed035abSart {
916aed035abSart 	int i;
917aed035abSart 
918aed035abSart 	for (i = 0; avtp[i].avt_model != NULL; i++)
919aed035abSart 		if (avtp[i].avt_variation == variation)
920aed035abSart 			return (avtp[i].avt_model);
921aed035abSart 	return (NULL);
922aed035abSart }
923aed035abSart 
924aed035abSart /*
925aed035abSart  * Generate a default platform name based for unknown system variations.
926aed035abSart  */
927aed035abSart const char *
928aed035abSart alpha_unknown_sysname()
929aed035abSart {
930aed035abSart 	static char s[128];		/* safe size */
931aed035abSart 
932d5eb2d9aSderaadt 	snprintf(s, sizeof s, "%s family, unknown model variation 0x%lx",
933e86d96d5Smiod 	    platform.family, (unsigned long)hwrpb->rpb_variation & SV_ST_MASK);
934aed035abSart 	return ((const char *)s);
935df930be7Sderaadt }
936df930be7Sderaadt 
93750ce9ee0Sniklas void
938df930be7Sderaadt identifycpu()
939df930be7Sderaadt {
940aed035abSart 	char *s;
941b3cee53eSmartin 	int slen;
942df930be7Sderaadt 
943df930be7Sderaadt 	/*
944df930be7Sderaadt 	 * print out CPU identification information.
945df930be7Sderaadt 	 */
946aed035abSart 	printf("%s", cpu_model);
947aed035abSart 	for(s = cpu_model; *s; ++s)
948aed035abSart 		if(strncasecmp(s, "MHz", 3) == 0)
949aed035abSart 			goto skipMHz;
950e86d96d5Smiod 	printf(", %luMHz", (unsigned long)hwrpb->rpb_cc_freq / 1000000);
951aed035abSart skipMHz:
952b3cee53eSmartin 	/* fill in hw_serial if a serial number is known */
953b3cee53eSmartin 	slen = strlen(hwrpb->rpb_ssn) + 1;
954b3cee53eSmartin 	if (slen > 1) {
955b3cee53eSmartin 		hw_serial = malloc(slen, M_SYSCTL, M_NOWAIT);
956b3cee53eSmartin 		if (hw_serial)
957b3cee53eSmartin 			strlcpy(hw_serial, (char *)hwrpb->rpb_ssn, slen);
958b3cee53eSmartin 	}
959b3cee53eSmartin 
960aed035abSart 	printf("\n");
961e86d96d5Smiod 	printf("%lu byte page size, %d processor%s.\n",
962e86d96d5Smiod 	    (unsigned long)hwrpb->rpb_page_size, ncpusfound,
963e86d96d5Smiod 	    ncpusfound == 1 ? "" : "s");
964df930be7Sderaadt #if 0
965b3cee53eSmartin 	/* this is not particularly useful! */
966df930be7Sderaadt 	printf("variation: 0x%lx, revision 0x%lx\n",
967df930be7Sderaadt 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
968df930be7Sderaadt #endif
969df930be7Sderaadt }
970df930be7Sderaadt 
971df930be7Sderaadt int	waittime = -1;
972df930be7Sderaadt struct pcb dumppcb;
973df930be7Sderaadt 
974ff261808Suebayasi __dead void
975ff261808Suebayasi boot(int howto)
976df930be7Sderaadt {
977aed035abSart #if defined(MULTIPROCESSOR)
97821c23d01Smiod 	u_long wait_mask;
97921c23d01Smiod 	int i;
980aed035abSart #endif
981aed035abSart 
982df930be7Sderaadt 	if (cold) {
983c9ad5066Stom 		if ((howto & RB_USERREQ) == 0)
984df930be7Sderaadt 			howto |= RB_HALT;
985df930be7Sderaadt 		goto haltsys;
986df930be7Sderaadt 	}
987df930be7Sderaadt 
98850ce9ee0Sniklas 	if ((boothowto & RB_HALT) != 0)
98950ce9ee0Sniklas 		howto |= RB_HALT;
99050ce9ee0Sniklas 
991df930be7Sderaadt 	boothowto = howto;
992df930be7Sderaadt 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
993df930be7Sderaadt 		waittime = 0;
994df930be7Sderaadt 		vfs_shutdown();
99515be954bSuebayasi 
9962417125dSmiod 		if ((howto & RB_TIMEBAD) == 0) {
997df930be7Sderaadt 			resettodr();
9982417125dSmiod 		} else {
9992417125dSmiod 			printf("WARNING: not updating battery clock\n");
10002417125dSmiod 		}
1001df930be7Sderaadt 	}
10027d9ca166Sderaadt 	if_downall();
1003df930be7Sderaadt 
1004c98e8b29Sderaadt 	uvm_shutdown();
10059f43f03fSuebayasi 	splhigh();
100680ce5a38Smpi 	cold = 1;
1007df930be7Sderaadt 
100821c23d01Smiod #if defined(MULTIPROCESSOR)
100921c23d01Smiod 	/*
101021c23d01Smiod 	 * Halt all other CPUs.
101121c23d01Smiod 	 */
101221c23d01Smiod 	wait_mask = (1UL << hwrpb->rpb_primary_cpu_id);
101321c23d01Smiod 	alpha_broadcast_ipi(ALPHA_IPI_HALT);
101421c23d01Smiod 
101521c23d01Smiod 	/* Ensure any CPUs paused by DDB resume execution so they can halt */
101621c23d01Smiod 	cpus_paused = 0;
101721c23d01Smiod 
101821c23d01Smiod 	for (i = 0; i < 10000; i++) {
101921c23d01Smiod 		alpha_mb();
102021c23d01Smiod 		if (cpus_running == wait_mask)
102121c23d01Smiod 			break;
102221c23d01Smiod 		delay(1000);
102321c23d01Smiod 	}
102421c23d01Smiod 	alpha_mb();
102521c23d01Smiod 	if (cpus_running != wait_mask)
102621c23d01Smiod 		printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
102721c23d01Smiod 		    cpus_running);
102821c23d01Smiod #endif
102921c23d01Smiod 
1030b19c85f4Suebayasi 	if ((howto & RB_DUMP) != 0)
1031df930be7Sderaadt 		dumpsys();
1032df930be7Sderaadt 
103334fbf6deSderaadt haltsys:
103496f419e1Skettenis 	config_suspend_all(DVACT_POWERDOWN);
1035df930be7Sderaadt 
1036df930be7Sderaadt #ifdef BOOTKEY
1037b33b2f20Suebayasi 	printf("hit any key to %s...\n",
1038b33b2f20Suebayasi 	    (howto & RB_HALT) != 0 ? "halt" : "reboot");
1039aed035abSart 	cnpollc(1);	/* for proper keyboard command handling */
1040df930be7Sderaadt 	cngetc();
1041aed035abSart 	cnpollc(0);
1042df930be7Sderaadt 	printf("\n");
1043df930be7Sderaadt #endif
1044df930be7Sderaadt 
1045aed035abSart 	/* Finally, powerdown/halt/reboot the system. */
1046b33b2f20Suebayasi 	if ((howto & RB_POWERDOWN) != 0 &&
1047aed035abSart 	    platform.powerdown != NULL) {
1048aed035abSart 		(*platform.powerdown)();
1049aed035abSart 		printf("WARNING: powerdown failed!\n");
1050aed035abSart 	}
1051b33b2f20Suebayasi 	printf("%s\n\n",
1052b33b2f20Suebayasi 	    (howto & RB_HALT) != 0 ? "halted." : "rebooting...");
1053b33b2f20Suebayasi 	prom_halt((howto & RB_HALT) != 0);
1054b19c85f4Suebayasi 	for (;;) ;
1055df930be7Sderaadt 	/* NOTREACHED */
1056df930be7Sderaadt }
1057df930be7Sderaadt 
1058df930be7Sderaadt /*
1059df930be7Sderaadt  * These variables are needed by /sbin/savecore
1060df930be7Sderaadt  */
1061df930be7Sderaadt u_long	dumpmag = 0x8fca0101;	/* magic number */
1062df930be7Sderaadt int 	dumpsize = 0;		/* pages */
1063df930be7Sderaadt long	dumplo = 0; 		/* blocks */
1064df930be7Sderaadt 
1065df930be7Sderaadt /*
106650ce9ee0Sniklas  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
106750ce9ee0Sniklas  */
106850ce9ee0Sniklas int
106950ce9ee0Sniklas cpu_dumpsize()
107050ce9ee0Sniklas {
107150ce9ee0Sniklas 	int size;
107250ce9ee0Sniklas 
1073aed035abSart 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1074aed035abSart 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
107550ce9ee0Sniklas 	if (roundup(size, dbtob(1)) != dbtob(1))
107650ce9ee0Sniklas 		return -1;
107750ce9ee0Sniklas 
107850ce9ee0Sniklas 	return (1);
107950ce9ee0Sniklas }
108050ce9ee0Sniklas 
108150ce9ee0Sniklas /*
1082aed035abSart  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1083aed035abSart  */
1084aed035abSart u_long
1085aed035abSart cpu_dump_mempagecnt()
1086aed035abSart {
1087aed035abSart 	u_long i, n;
1088aed035abSart 
1089aed035abSart 	n = 0;
1090aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++)
1091aed035abSart 		n += atop(mem_clusters[i].size);
1092aed035abSart 	return (n);
1093aed035abSart }
1094aed035abSart 
1095aed035abSart /*
109650ce9ee0Sniklas  * cpu_dump: dump machine-dependent kernel core dump headers.
109750ce9ee0Sniklas  */
109850ce9ee0Sniklas int
109950ce9ee0Sniklas cpu_dump()
110050ce9ee0Sniklas {
11011abdbfdeSderaadt 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
1102aed035abSart 	char buf[dbtob(1)];
110350ce9ee0Sniklas 	kcore_seg_t *segp;
110450ce9ee0Sniklas 	cpu_kcore_hdr_t *cpuhdrp;
1105aed035abSart 	phys_ram_seg_t *memsegp;
1106aed035abSart 	int i;
110750ce9ee0Sniklas 
110850ce9ee0Sniklas 	dump = bdevsw[major(dumpdev)].d_dump;
110950ce9ee0Sniklas 
1110aed035abSart 	bzero(buf, sizeof buf);
111150ce9ee0Sniklas 	segp = (kcore_seg_t *)buf;
1112aed035abSart 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1113aed035abSart 	memsegp = (phys_ram_seg_t *)&buf[ALIGN(sizeof(*segp)) +
1114aed035abSart 	    ALIGN(sizeof(*cpuhdrp))];
111550ce9ee0Sniklas 
111650ce9ee0Sniklas 	/*
111750ce9ee0Sniklas 	 * Generate a segment header.
111850ce9ee0Sniklas 	 */
111950ce9ee0Sniklas 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
112050ce9ee0Sniklas 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
112150ce9ee0Sniklas 
112250ce9ee0Sniklas 	/*
1123aed035abSart 	 * Add the machine-dependent header info.
112450ce9ee0Sniklas 	 */
1125aed035abSart 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
112650ce9ee0Sniklas 	cpuhdrp->page_size = PAGE_SIZE;
1127aed035abSart 	cpuhdrp->nmemsegs = mem_cluster_cnt;
1128aed035abSart 
1129aed035abSart 	/*
1130aed035abSart 	 * Fill in the memory segment descriptors.
1131aed035abSart 	 */
1132aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++) {
1133aed035abSart 		memsegp[i].start = mem_clusters[i].start;
1134aed035abSart 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1135aed035abSart 	}
113650ce9ee0Sniklas 
113750ce9ee0Sniklas 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
113850ce9ee0Sniklas }
113950ce9ee0Sniklas 
114050ce9ee0Sniklas /*
1141aed035abSart  * This is called by main to set dumplo and dumpsize.
1142194dd68bSbrad  * Dumps always skip the first PAGE_SIZE of disk space
1143df930be7Sderaadt  * in case there might be a disk label stored there.
1144df930be7Sderaadt  * If there is extra space, put dump at the end to
1145df930be7Sderaadt  * reduce the chance that swapping trashes it.
1146df930be7Sderaadt  */
1147df930be7Sderaadt void
1148e17d3b39Sderaadt dumpconf(void)
1149df930be7Sderaadt {
115050ce9ee0Sniklas 	int nblks, dumpblks;	/* size of dump area */
1151df930be7Sderaadt 
1152e17d3b39Sderaadt 	if (dumpdev == NODEV ||
1153e17d3b39Sderaadt 	    (nblks = (bdevsw[major(dumpdev)].d_psize)(dumpdev)) == 0)
1154e17d3b39Sderaadt 		return;
1155df930be7Sderaadt 	if (nblks <= ctod(1))
1156e17d3b39Sderaadt 		return;
115750ce9ee0Sniklas 
115850ce9ee0Sniklas 	dumpblks = cpu_dumpsize();
115950ce9ee0Sniklas 	if (dumpblks < 0)
1160e17d3b39Sderaadt 		return;
1161aed035abSart 	dumpblks += ctod(cpu_dump_mempagecnt());
116250ce9ee0Sniklas 
116350ce9ee0Sniklas 	/* If dump won't fit (incl. room for possible label), punt. */
116450ce9ee0Sniklas 	if (dumpblks > (nblks - ctod(1)))
1165e17d3b39Sderaadt 		return;
116650ce9ee0Sniklas 
116750ce9ee0Sniklas 	/* Put dump at end of partition */
116850ce9ee0Sniklas 	dumplo = nblks - dumpblks;
116950ce9ee0Sniklas 
117050ce9ee0Sniklas 	/* dumpsize is in page units, and doesn't include headers. */
1171aed035abSart 	dumpsize = cpu_dump_mempagecnt();
1172df930be7Sderaadt }
1173df930be7Sderaadt 
1174df930be7Sderaadt /*
117550ce9ee0Sniklas  * Dump the kernel's image to the swap partition.
1176df930be7Sderaadt  */
1177194dd68bSbrad #define	BYTES_PER_DUMP	PAGE_SIZE
117850ce9ee0Sniklas 
1179df930be7Sderaadt void
1180df930be7Sderaadt dumpsys()
1181df930be7Sderaadt {
1182aed035abSart 	u_long totalbytesleft, bytes, i, n, memcl;
1183aed035abSart 	u_long maddr;
1184aed035abSart 	int psize;
11851abdbfdeSderaadt 	daddr_t blkno;
11861abdbfdeSderaadt 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
118750ce9ee0Sniklas 	int error;
1188067cbd75Sderaadt 	extern int msgbufmapped;
1189df930be7Sderaadt 
119050ce9ee0Sniklas 	/* Save registers. */
119150ce9ee0Sniklas 	savectx(&dumppcb);
119250ce9ee0Sniklas 
119350ce9ee0Sniklas 	msgbufmapped = 0;	/* don't record dump msgs in msgbuf */
1194df930be7Sderaadt 	if (dumpdev == NODEV)
1195df930be7Sderaadt 		return;
119650ce9ee0Sniklas 
119750ce9ee0Sniklas 	/*
119850ce9ee0Sniklas 	 * For dumps during autoconfiguration,
119950ce9ee0Sniklas 	 * if dump device has already configured...
120050ce9ee0Sniklas 	 */
1201df930be7Sderaadt 	if (dumpsize == 0)
120250ce9ee0Sniklas 		dumpconf();
120350ce9ee0Sniklas 	if (dumplo <= 0) {
1204aed035abSart 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
1205aed035abSart 		    minor(dumpdev));
1206df930be7Sderaadt 		return;
1207df930be7Sderaadt 	}
1208aed035abSart 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
1209aed035abSart 	    minor(dumpdev), dumplo);
1210df930be7Sderaadt 
121150ce9ee0Sniklas 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
1212df930be7Sderaadt 	printf("dump ");
121350ce9ee0Sniklas 	if (psize == -1) {
121450ce9ee0Sniklas 		printf("area unavailable\n");
121550ce9ee0Sniklas 		return;
121650ce9ee0Sniklas 	}
121750ce9ee0Sniklas 
121850ce9ee0Sniklas 	/* XXX should purge all outstanding keystrokes. */
121950ce9ee0Sniklas 
122050ce9ee0Sniklas 	if ((error = cpu_dump()) != 0)
122150ce9ee0Sniklas 		goto err;
122250ce9ee0Sniklas 
1223aed035abSart 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
122450ce9ee0Sniklas 	blkno = dumplo + cpu_dumpsize();
122550ce9ee0Sniklas 	dump = bdevsw[major(dumpdev)].d_dump;
122650ce9ee0Sniklas 	error = 0;
1227aed035abSart 
1228aed035abSart 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1229aed035abSart 		maddr = mem_clusters[memcl].start;
1230aed035abSart 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1231aed035abSart 
1232aed035abSart 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
123350ce9ee0Sniklas 
123450ce9ee0Sniklas 			/* Print out how many MBs we to go. */
1235aed035abSart 			if ((totalbytesleft % (1024*1024)) == 0)
1236aed035abSart 				printf("%ld ", totalbytesleft / (1024 * 1024));
123750ce9ee0Sniklas 
123850ce9ee0Sniklas 			/* Limit size for next transfer. */
1239aed035abSart 			n = bytes - i;
124050ce9ee0Sniklas 			if (n > BYTES_PER_DUMP)
124150ce9ee0Sniklas 				n =  BYTES_PER_DUMP;
124250ce9ee0Sniklas 
124350ce9ee0Sniklas 			error = (*dump)(dumpdev, blkno,
124450ce9ee0Sniklas 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
124550ce9ee0Sniklas 			if (error)
1246aed035abSart 				goto err;
124750ce9ee0Sniklas 			maddr += n;
124850ce9ee0Sniklas 			blkno += btodb(n);			/* XXX? */
124950ce9ee0Sniklas 
125050ce9ee0Sniklas 			/* XXX should look for keystrokes, to cancel. */
125150ce9ee0Sniklas 		}
1252aed035abSart 	}
125350ce9ee0Sniklas 
125450ce9ee0Sniklas err:
125550ce9ee0Sniklas 	switch (error) {
1256a37778bcSderaadt #ifdef DEBUG
1257df930be7Sderaadt 	case ENXIO:
1258df930be7Sderaadt 		printf("device bad\n");
1259df930be7Sderaadt 		break;
1260df930be7Sderaadt 
1261df930be7Sderaadt 	case EFAULT:
1262df930be7Sderaadt 		printf("device not ready\n");
1263df930be7Sderaadt 		break;
1264df930be7Sderaadt 
1265df930be7Sderaadt 	case EINVAL:
1266df930be7Sderaadt 		printf("area improper\n");
1267df930be7Sderaadt 		break;
1268df930be7Sderaadt 
1269df930be7Sderaadt 	case EIO:
1270df930be7Sderaadt 		printf("i/o error\n");
1271df930be7Sderaadt 		break;
1272df930be7Sderaadt 
1273df930be7Sderaadt 	case EINTR:
1274df930be7Sderaadt 		printf("aborted from console\n");
1275df930be7Sderaadt 		break;
1276a37778bcSderaadt #endif /* DEBUG */
127750ce9ee0Sniklas 	case 0:
1278df930be7Sderaadt 		printf("succeeded\n");
1279df930be7Sderaadt 		break;
128050ce9ee0Sniklas 
128150ce9ee0Sniklas 	default:
128250ce9ee0Sniklas 		printf("error %d\n", error);
128350ce9ee0Sniklas 		break;
1284df930be7Sderaadt 	}
1285df930be7Sderaadt 	printf("\n\n");
1286df930be7Sderaadt 	delay(1000);
1287df930be7Sderaadt }
1288df930be7Sderaadt 
1289df930be7Sderaadt void
1290df930be7Sderaadt frametoreg(framep, regp)
1291df930be7Sderaadt 	struct trapframe *framep;
1292df930be7Sderaadt 	struct reg *regp;
1293df930be7Sderaadt {
1294df930be7Sderaadt 
1295df930be7Sderaadt 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1296df930be7Sderaadt 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1297df930be7Sderaadt 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1298df930be7Sderaadt 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1299df930be7Sderaadt 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1300df930be7Sderaadt 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1301df930be7Sderaadt 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1302df930be7Sderaadt 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1303df930be7Sderaadt 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1304df930be7Sderaadt 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1305df930be7Sderaadt 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1306df930be7Sderaadt 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1307df930be7Sderaadt 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1308df930be7Sderaadt 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1309df930be7Sderaadt 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1310df930be7Sderaadt 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
131150ce9ee0Sniklas 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
131250ce9ee0Sniklas 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
131350ce9ee0Sniklas 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1314df930be7Sderaadt 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1315df930be7Sderaadt 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1316df930be7Sderaadt 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1317df930be7Sderaadt 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1318df930be7Sderaadt 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1319df930be7Sderaadt 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1320df930be7Sderaadt 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1321df930be7Sderaadt 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1322df930be7Sderaadt 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1323df930be7Sderaadt 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
132450ce9ee0Sniklas 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
132550ce9ee0Sniklas 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1326df930be7Sderaadt 	regp->r_regs[R_ZERO] = 0;
1327df930be7Sderaadt }
1328df930be7Sderaadt 
1329df930be7Sderaadt void
1330df930be7Sderaadt regtoframe(regp, framep)
1331df930be7Sderaadt 	struct reg *regp;
1332df930be7Sderaadt 	struct trapframe *framep;
1333df930be7Sderaadt {
1334df930be7Sderaadt 
1335df930be7Sderaadt 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1336df930be7Sderaadt 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1337df930be7Sderaadt 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1338df930be7Sderaadt 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1339df930be7Sderaadt 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1340df930be7Sderaadt 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1341df930be7Sderaadt 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1342df930be7Sderaadt 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1343df930be7Sderaadt 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1344df930be7Sderaadt 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1345df930be7Sderaadt 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1346df930be7Sderaadt 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1347df930be7Sderaadt 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1348df930be7Sderaadt 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1349df930be7Sderaadt 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1350df930be7Sderaadt 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
135150ce9ee0Sniklas 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
135250ce9ee0Sniklas 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
135350ce9ee0Sniklas 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1354df930be7Sderaadt 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1355df930be7Sderaadt 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1356df930be7Sderaadt 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1357df930be7Sderaadt 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1358df930be7Sderaadt 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1359df930be7Sderaadt 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1360df930be7Sderaadt 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1361df930be7Sderaadt 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1362df930be7Sderaadt 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1363df930be7Sderaadt 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
136450ce9ee0Sniklas 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
136550ce9ee0Sniklas 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1366df930be7Sderaadt 	/* ??? = regp->r_regs[R_ZERO]; */
1367df930be7Sderaadt }
1368df930be7Sderaadt 
1369df930be7Sderaadt void
1370df930be7Sderaadt printregs(regp)
1371df930be7Sderaadt 	struct reg *regp;
1372df930be7Sderaadt {
1373df930be7Sderaadt 	int i;
1374df930be7Sderaadt 
1375df930be7Sderaadt 	for (i = 0; i < 32; i++)
1376df930be7Sderaadt 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1377df930be7Sderaadt 		   i & 1 ? "\n" : "\t");
1378df930be7Sderaadt }
1379df930be7Sderaadt 
1380df930be7Sderaadt void
1381df930be7Sderaadt regdump(framep)
1382df930be7Sderaadt 	struct trapframe *framep;
1383df930be7Sderaadt {
1384df930be7Sderaadt 	struct reg reg;
1385df930be7Sderaadt 
1386df930be7Sderaadt 	frametoreg(framep, &reg);
138750ce9ee0Sniklas 	reg.r_regs[R_SP] = alpha_pal_rdusp();
138850ce9ee0Sniklas 
1389df930be7Sderaadt 	printf("REGISTERS:\n");
1390df930be7Sderaadt 	printregs(&reg);
1391df930be7Sderaadt }
1392df930be7Sderaadt 
1393df930be7Sderaadt #ifdef DEBUG
1394df930be7Sderaadt int sigdebug = 0;
139572830333Sokan pid_t sigpid = 0;
1396df930be7Sderaadt #define	SDB_FOLLOW	0x01
1397df930be7Sderaadt #define	SDB_KSTACK	0x02
1398df930be7Sderaadt #endif
1399df930be7Sderaadt 
1400df930be7Sderaadt /*
1401df930be7Sderaadt  * Send an interrupt to process.
1402df930be7Sderaadt  */
1403df930be7Sderaadt void
1404*e99873aaSderaadt sendsig(sig_t catcher, int sig, int mask, u_long code, int type,
1405*e99873aaSderaadt     union sigval val)
1406df930be7Sderaadt {
1407df930be7Sderaadt 	struct proc *p = curproc;
1408*e99873aaSderaadt 	struct sigcontext ksc, *scp;
1409e7e08221Smiod 	struct fpreg *fpregs = (struct fpreg *)&ksc.sc_fpregs;
1410df930be7Sderaadt 	struct trapframe *frame;
1411533be81aSguenther 	struct sigacts *psp = p->p_p->ps_sigacts;
14121eaa59e7Sguenther 	unsigned long oldsp;
14131eaa59e7Sguenther 	int fsize, rndfsize, kscsize;
14142bf9c155Sderaadt 	siginfo_t *sip, ksi;
1415df930be7Sderaadt 
14161eaa59e7Sguenther 	oldsp = alpha_pal_rdusp();
1417df930be7Sderaadt 	frame = p->p_md.md_tf;
1418df930be7Sderaadt 	fsize = sizeof ksc;
1419df930be7Sderaadt 	rndfsize = ((fsize + 15) / 16) * 16;
14202bf9c155Sderaadt 	kscsize = rndfsize;
14212bf9c155Sderaadt 	if (psp->ps_siginfo & sigmask(sig)) {
14222bf9c155Sderaadt 		fsize += sizeof ksi;
14232bf9c155Sderaadt 		rndfsize = ((fsize + 15) / 16) * 16;
14242bf9c155Sderaadt 	}
142574652a67Sniklas 
1426df930be7Sderaadt 	/*
14276e870393Smiod 	 * Allocate space for the signal handler context.
1428df930be7Sderaadt 	 */
14291eaa59e7Sguenther 	if ((p->p_sigstk.ss_flags & SS_DISABLE) == 0 &&
14301eaa59e7Sguenther 	    !sigonstack(oldsp) && (psp->ps_sigonstack & sigmask(sig)))
14312725daddSguenther 		scp = (struct sigcontext *)(p->p_sigstk.ss_sp +
14322725daddSguenther 		    p->p_sigstk.ss_size - rndfsize);
14331eaa59e7Sguenther 	else
14341eaa59e7Sguenther 		scp = (struct sigcontext *)(oldsp - rndfsize);
1435df930be7Sderaadt 
1436df930be7Sderaadt 	/*
1437df930be7Sderaadt 	 * Build the signal context to be used by sigreturn.
1438df930be7Sderaadt 	 */
14391eaa59e7Sguenther 	bzero(&ksc, sizeof(ksc));
1440df930be7Sderaadt 	ksc.sc_mask = mask;
144150ce9ee0Sniklas 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
144250ce9ee0Sniklas 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
1443df930be7Sderaadt 
1444df930be7Sderaadt 	/* copy the registers. */
1445df930be7Sderaadt 	frametoreg(frame, (struct reg *)ksc.sc_regs);
14461eaa59e7Sguenther 	ksc.sc_regs[R_SP] = oldsp;
1447df930be7Sderaadt 
1448df930be7Sderaadt 	/* save the floating-point state, if necessary, then copy it. */
1449433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1450433075b6Spvalchev 		fpusave_proc(p, 1);
1451df930be7Sderaadt 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
1452e7e08221Smiod 	memcpy(/*ksc.sc_*/fpregs, &p->p_addr->u_pcb.pcb_fp,
1453df930be7Sderaadt 	    sizeof(struct fpreg));
1454433075b6Spvalchev #ifndef NO_IEEE
1455433075b6Spvalchev 	ksc.sc_fp_control = alpha_read_fp_c(p);
1456433075b6Spvalchev #else
1457433075b6Spvalchev 	ksc.sc_fp_control = 0;
1458433075b6Spvalchev #endif
1459433075b6Spvalchev 	memset(ksc.sc_reserved, 0, sizeof ksc.sc_reserved);	/* XXX */
1460433075b6Spvalchev 	memset(ksc.sc_xxx, 0, sizeof ksc.sc_xxx);		/* XXX */
1461df930be7Sderaadt 
14622bf9c155Sderaadt 	if (psp->ps_siginfo & sigmask(sig)) {
14632bf9c155Sderaadt 		initsiginfo(&ksi, sig, code, type, val);
14642bf9c155Sderaadt 		sip = (void *)scp + kscsize;
1465679ebc41Smiod 		if (copyout((caddr_t)&ksi, (caddr_t)sip, fsize - kscsize) != 0)
1466679ebc41Smiod 			goto trash;
1467aa540fb8Sart 	} else
1468aa540fb8Sart 		sip = NULL;
14692bf9c155Sderaadt 
14707730d1d9Sderaadt 	ksc.sc_cookie = (long)scp ^ p->p_p->ps_sigcookie;
1471679ebc41Smiod 	if (copyout((caddr_t)&ksc, (caddr_t)scp, kscsize) != 0) {
1472679ebc41Smiod trash:
1473679ebc41Smiod 		/*
1474679ebc41Smiod 		 * Process has trashed its stack; give it an illegal
1475679ebc41Smiod 		 * instruction to halt it in its tracks.
1476679ebc41Smiod 		 */
147786fd84b3Smiod 		sigexit(p, SIGILL);
147886fd84b3Smiod 		/* NOTREACHED */
1479679ebc41Smiod 	}
1480df930be7Sderaadt 
1481df930be7Sderaadt 	/*
1482df930be7Sderaadt 	 * Set up the registers to return to sigcode.
1483df930be7Sderaadt 	 */
14848f76f5adSguenther 	frame->tf_regs[FRAME_PC] = p->p_p->ps_sigcode;
148550ce9ee0Sniklas 	frame->tf_regs[FRAME_A0] = sig;
1486aa540fb8Sart 	frame->tf_regs[FRAME_A1] = (u_int64_t)sip;
148750ce9ee0Sniklas 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
1488df930be7Sderaadt 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
148950ce9ee0Sniklas 	alpha_pal_wrusp((unsigned long)scp);
1490df930be7Sderaadt }
1491df930be7Sderaadt 
1492df930be7Sderaadt /*
1493df930be7Sderaadt  * System call to cleanup state after a signal
1494df930be7Sderaadt  * has been taken.  Reset signal mask and
1495df930be7Sderaadt  * stack state from context left by sendsig (above).
1496df930be7Sderaadt  * Return to previous pc and psl as specified by
1497df930be7Sderaadt  * context left by sendsig. Check carefully to
1498df930be7Sderaadt  * make sure that the user has not modified the
1499125cd19fSderaadt  * psl to gain improper privileges or to cause
1500df930be7Sderaadt  * a machine fault.
1501df930be7Sderaadt  */
1502df930be7Sderaadt /* ARGSUSED */
1503df930be7Sderaadt int
1504*e99873aaSderaadt sys_sigreturn(struct proc *p, void *v, register_t *retval)
1505df930be7Sderaadt {
1506df930be7Sderaadt 	struct sys_sigreturn_args /* {
1507df930be7Sderaadt 		syscallarg(struct sigcontext *) sigcntxp;
1508df930be7Sderaadt 	} */ *uap = v;
1509*e99873aaSderaadt 	struct sigcontext ksc, *scp = SCARG(uap, sigcntxp);
1510e7e08221Smiod 	struct fpreg *fpregs = (struct fpreg *)&ksc.sc_fpregs;
1511aa540fb8Sart 	int error;
1512df930be7Sderaadt 
15137730d1d9Sderaadt 	if (PROC_PC(p) != p->p_p->ps_sigcoderet) {
15147730d1d9Sderaadt 		sigexit(p, SIGILL);
15157730d1d9Sderaadt 		return (EPERM);
15167730d1d9Sderaadt 	}
15177730d1d9Sderaadt 
15187730d1d9Sderaadt 	if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0)
1519aa540fb8Sart 		return (error);
1520df930be7Sderaadt 
15217730d1d9Sderaadt 	if (ksc.sc_cookie != ((long)scp ^ p->p_p->ps_sigcookie)) {
15227730d1d9Sderaadt 		sigexit(p, SIGILL);
15237730d1d9Sderaadt 		return (EFAULT);
15247730d1d9Sderaadt 	}
15257730d1d9Sderaadt 
15264a064f11Sderaadt 	/* Prevent reuse of the sigcontext cookie */
15274a064f11Sderaadt 	ksc.sc_cookie = 0;
15284a064f11Sderaadt 	(void)copyout(&ksc.sc_cookie, (caddr_t)scp +
1529*e99873aaSderaadt 	    offsetof(struct sigcontext, sc_cookie), sizeof (ksc.sc_cookie));
15304a064f11Sderaadt 
1531df930be7Sderaadt 	/*
1532df930be7Sderaadt 	 * Restore the user-supplied information
1533df930be7Sderaadt 	 */
1534df930be7Sderaadt 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
1535df930be7Sderaadt 
153650ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
153750ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_PS] =
153850ce9ee0Sniklas 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
1539df930be7Sderaadt 
1540df930be7Sderaadt 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
154150ce9ee0Sniklas 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
1542df930be7Sderaadt 
1543df930be7Sderaadt 	/* XXX ksc.sc_ownedfp ? */
1544433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1545433075b6Spvalchev 		fpusave_proc(p, 0);
1546e7e08221Smiod 	memcpy(&p->p_addr->u_pcb.pcb_fp, /*ksc.sc_*/fpregs,
1547df930be7Sderaadt 	    sizeof(struct fpreg));
1548433075b6Spvalchev #ifndef NO_IEEE
1549433075b6Spvalchev 	p->p_addr->u_pcb.pcb_fp.fpr_cr = ksc.sc_fpcr;
1550433075b6Spvalchev 	p->p_md.md_flags = ksc.sc_fp_control & MDP_FP_C;
1551433075b6Spvalchev #endif
1552df930be7Sderaadt 	return (EJUSTRETURN);
1553df930be7Sderaadt }
1554df930be7Sderaadt 
1555df930be7Sderaadt /*
1556df930be7Sderaadt  * machine dependent system variables.
1557df930be7Sderaadt  */
155850ce9ee0Sniklas int
1559df930be7Sderaadt cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1560df930be7Sderaadt 	int *name;
1561df930be7Sderaadt 	u_int namelen;
1562df930be7Sderaadt 	void *oldp;
1563df930be7Sderaadt 	size_t *oldlenp;
1564df930be7Sderaadt 	void *newp;
1565df930be7Sderaadt 	size_t newlen;
1566df930be7Sderaadt 	struct proc *p;
1567df930be7Sderaadt {
1568df930be7Sderaadt 	dev_t consdev;
1569a072164aSmiod #if NIOASIC > 0
1570a072164aSmiod 	int oldval, ret;
1571a072164aSmiod #endif
1572df930be7Sderaadt 
157345e5a1a0Sart 	if (name[0] != CPU_CHIPSET && namelen != 1)
1574df930be7Sderaadt 		return (ENOTDIR);		/* overloaded */
1575df930be7Sderaadt 
1576df930be7Sderaadt 	switch (name[0]) {
1577df930be7Sderaadt 	case CPU_CONSDEV:
1578df930be7Sderaadt 		if (cn_tab != NULL)
1579df930be7Sderaadt 			consdev = cn_tab->cn_dev;
1580df930be7Sderaadt 		else
1581df930be7Sderaadt 			consdev = NODEV;
1582df930be7Sderaadt 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
1583df930be7Sderaadt 			sizeof consdev));
1584417eba8cSderaadt 
1585a37778bcSderaadt #ifndef SMALL_KERNEL
158650ce9ee0Sniklas 	case CPU_UNALIGNED_PRINT:
158750ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
158850ce9ee0Sniklas 		    &alpha_unaligned_print));
158950ce9ee0Sniklas 
159050ce9ee0Sniklas 	case CPU_UNALIGNED_FIX:
159150ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
159250ce9ee0Sniklas 		    &alpha_unaligned_fix));
159350ce9ee0Sniklas 
159450ce9ee0Sniklas 	case CPU_UNALIGNED_SIGBUS:
159550ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
159650ce9ee0Sniklas 		    &alpha_unaligned_sigbus));
159750ce9ee0Sniklas 
15983a630e3fSniklas 	case CPU_BOOTED_KERNEL:
1599aed035abSart 		return (sysctl_rdstring(oldp, oldlenp, newp,
1600aed035abSart 		    bootinfo.booted_kernel));
16013a630e3fSniklas 
160245e5a1a0Sart 	case CPU_CHIPSET:
160345e5a1a0Sart 		return (alpha_sysctl_chipset(name + 1, namelen - 1, oldp,
160445e5a1a0Sart 		    oldlenp));
1605a37778bcSderaadt #endif /* SMALL_KERNEL */
1606433075b6Spvalchev 
1607433075b6Spvalchev #ifndef NO_IEEE
1608433075b6Spvalchev 	case CPU_FP_SYNC_COMPLETE:
1609433075b6Spvalchev 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1610433075b6Spvalchev 		    &alpha_fp_sync_complete));
1611433075b6Spvalchev #endif
161227626149Smatthieu 	case CPU_ALLOWAPERTURE:
161327626149Smatthieu #ifdef APERTURE
161427626149Smatthieu 		if (securelevel > 0)
16151546ceefSderaadt 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
16161546ceefSderaadt 			    &allowaperture));
161727626149Smatthieu                 else
161827626149Smatthieu                         return (sysctl_int(oldp, oldlenp, newp, newlen,
161927626149Smatthieu                             &allowaperture));
162027626149Smatthieu #else
162127626149Smatthieu 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
162227626149Smatthieu #endif
1623a072164aSmiod #if NIOASIC > 0
1624a072164aSmiod 	case CPU_LED_BLINK:
1625a072164aSmiod 		oldval = alpha_led_blink;
1626a072164aSmiod 		ret = sysctl_int(oldp, oldlenp, newp, newlen, &alpha_led_blink);
1627a072164aSmiod 		if (oldval != alpha_led_blink)
1628a072164aSmiod 			ioasic_led_blink(NULL);
1629a072164aSmiod 		return (ret);
1630a072164aSmiod #endif
1631df930be7Sderaadt 	default:
1632df930be7Sderaadt 		return (EOPNOTSUPP);
1633df930be7Sderaadt 	}
1634df930be7Sderaadt 	/* NOTREACHED */
1635df930be7Sderaadt }
1636df930be7Sderaadt 
1637df930be7Sderaadt /*
1638df930be7Sderaadt  * Set registers on exec.
1639df930be7Sderaadt  */
1640df930be7Sderaadt void
1641df930be7Sderaadt setregs(p, pack, stack, retval)
1642df930be7Sderaadt 	register struct proc *p;
1643df930be7Sderaadt 	struct exec_package *pack;
1644df930be7Sderaadt 	u_long stack;
1645df930be7Sderaadt 	register_t *retval;
1646df930be7Sderaadt {
1647df930be7Sderaadt 	struct trapframe *tfp = p->p_md.md_tf;
16483a630e3fSniklas #ifdef DEBUG
16493a630e3fSniklas 	int i;
16503a630e3fSniklas #endif
1651df930be7Sderaadt 
1652df930be7Sderaadt #ifdef DEBUG
165350ce9ee0Sniklas 	/*
165450ce9ee0Sniklas 	 * Crash and dump, if the user requested it.
165550ce9ee0Sniklas 	 */
165650ce9ee0Sniklas 	if (boothowto & RB_DUMP)
165750ce9ee0Sniklas 		panic("crash requested by boot flags");
165850ce9ee0Sniklas #endif
165950ce9ee0Sniklas 
166050ce9ee0Sniklas #ifdef DEBUG
166150ce9ee0Sniklas 	for (i = 0; i < FRAME_SIZE; i++)
1662df930be7Sderaadt 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
16636334622bSguenther 	tfp->tf_regs[FRAME_A1] = 0;
1664df930be7Sderaadt #else
166550ce9ee0Sniklas 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1666df930be7Sderaadt #endif
1667df930be7Sderaadt 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
166850ce9ee0Sniklas 	alpha_pal_wrusp(stack);
166950ce9ee0Sniklas 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
167050ce9ee0Sniklas 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1671df930be7Sderaadt 
167250ce9ee0Sniklas 	tfp->tf_regs[FRAME_A0] = stack;
167350ce9ee0Sniklas 	/* a1 and a2 already zeroed */
167450ce9ee0Sniklas 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
167550ce9ee0Sniklas 
167650ce9ee0Sniklas 	p->p_md.md_flags &= ~MDP_FPUSED;
1677433075b6Spvalchev #ifndef NO_IEEE
1678433075b6Spvalchev 	if (__predict_true((p->p_md.md_flags & IEEE_INHERIT) == 0)) {
1679433075b6Spvalchev 		p->p_md.md_flags &= ~MDP_FP_C;
1680433075b6Spvalchev 		p->p_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
1681433075b6Spvalchev 	}
1682433075b6Spvalchev #endif
1683433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1684433075b6Spvalchev 		fpusave_proc(p, 0);
1685ee2d823aSmiod 
1686ee2d823aSmiod 	retval[1] = 0;
1687433075b6Spvalchev }
1688df930be7Sderaadt 
1689433075b6Spvalchev /*
1690433075b6Spvalchev  * Release the FPU.
1691433075b6Spvalchev  */
1692433075b6Spvalchev void
1693433075b6Spvalchev fpusave_cpu(struct cpu_info *ci, int save)
1694433075b6Spvalchev {
1695433075b6Spvalchev 	struct proc *p;
169621c23d01Smiod #if defined(MULTIPROCESSOR)
169721c23d01Smiod 	int s;
169821c23d01Smiod #endif
1699433075b6Spvalchev 
1700433075b6Spvalchev 	KDASSERT(ci == curcpu());
1701433075b6Spvalchev 
1702433075b6Spvalchev #if defined(MULTIPROCESSOR)
170321c23d01Smiod 	/* Need to block IPIs */
17042d7472daSmiod 	s = splipi();
1705433075b6Spvalchev 	atomic_setbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1706433075b6Spvalchev #endif
1707433075b6Spvalchev 
1708433075b6Spvalchev 	p = ci->ci_fpcurproc;
1709433075b6Spvalchev 	if (p == NULL)
1710433075b6Spvalchev 		goto out;
1711433075b6Spvalchev 
1712433075b6Spvalchev 	if (save) {
1713433075b6Spvalchev 		alpha_pal_wrfen(1);
1714433075b6Spvalchev 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
1715433075b6Spvalchev 	}
1716433075b6Spvalchev 
1717433075b6Spvalchev 	alpha_pal_wrfen(0);
1718433075b6Spvalchev 
1719433075b6Spvalchev 	p->p_addr->u_pcb.pcb_fpcpu = NULL;
1720433075b6Spvalchev 	ci->ci_fpcurproc = NULL;
1721433075b6Spvalchev 
1722433075b6Spvalchev out:
1723433075b6Spvalchev #if defined(MULTIPROCESSOR)
1724433075b6Spvalchev 	atomic_clearbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
17254fa86cc0Smiod 	alpha_pal_swpipl(s);
1726433075b6Spvalchev #endif
1727433075b6Spvalchev 	return;
1728433075b6Spvalchev }
1729433075b6Spvalchev 
1730433075b6Spvalchev /*
1731433075b6Spvalchev  * Synchronize FP state for this process.
1732433075b6Spvalchev  */
1733433075b6Spvalchev void
1734433075b6Spvalchev fpusave_proc(struct proc *p, int save)
1735433075b6Spvalchev {
1736433075b6Spvalchev 	struct cpu_info *ci = curcpu();
1737433075b6Spvalchev 	struct cpu_info *oci;
1738433075b6Spvalchev #if defined(MULTIPROCESSOR)
1739433075b6Spvalchev 	u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
17402d7472daSmiod 	int s;
1741433075b6Spvalchev #endif
1742433075b6Spvalchev 
1743433075b6Spvalchev 	KDASSERT(p->p_addr != NULL);
1744433075b6Spvalchev 
17452d7472daSmiod 	for (;;) {
174621c23d01Smiod #if defined(MULTIPROCESSOR)
174721c23d01Smiod 		/* Need to block IPIs */
17482d7472daSmiod 		s = splipi();
174921c23d01Smiod #endif
175021c23d01Smiod 
1751433075b6Spvalchev 		oci = p->p_addr->u_pcb.pcb_fpcpu;
1752433075b6Spvalchev 		if (oci == NULL) {
175321c23d01Smiod #if defined(MULTIPROCESSOR)
17544fa86cc0Smiod 			alpha_pal_swpipl(s);
175521c23d01Smiod #endif
1756433075b6Spvalchev 			return;
1757433075b6Spvalchev 		}
1758433075b6Spvalchev 
1759433075b6Spvalchev #if defined(MULTIPROCESSOR)
1760433075b6Spvalchev 		if (oci == ci) {
1761433075b6Spvalchev 			KASSERT(ci->ci_fpcurproc == p);
17624fa86cc0Smiod 			alpha_pal_swpipl(s);
1763433075b6Spvalchev 			fpusave_cpu(ci, save);
1764433075b6Spvalchev 			return;
1765433075b6Spvalchev 		}
1766433075b6Spvalchev 
17672d7472daSmiod 		/*
17682d7472daSmiod 		 * The other cpu may still be running and could have
17692d7472daSmiod 		 * discarded the fpu context on its own.
17702d7472daSmiod 		 */
177189043825Smiod 		if (oci->ci_fpcurproc != p) {
177289043825Smiod 			alpha_pal_swpipl(s);
17732d7472daSmiod 			continue;
177489043825Smiod 		}
17752d7472daSmiod 
1776433075b6Spvalchev 		alpha_send_ipi(oci->ci_cpuid, ipi);
17774fa86cc0Smiod 		alpha_pal_swpipl(s);
1778433075b6Spvalchev 
17792d7472daSmiod 		while (p->p_addr->u_pcb.pcb_fpcpu != NULL)
17802d7472daSmiod 			SPINLOCK_SPIN_HOOK;
1781433075b6Spvalchev #else
1782433075b6Spvalchev 		KASSERT(ci->ci_fpcurproc == p);
1783433075b6Spvalchev 		fpusave_cpu(ci, save);
1784433075b6Spvalchev #endif /* MULTIPROCESSOR */
17852d7472daSmiod 
17862d7472daSmiod 		break;
17872d7472daSmiod 	}
1788df930be7Sderaadt }
1789df930be7Sderaadt 
1790df930be7Sderaadt int
1791df930be7Sderaadt spl0()
1792df930be7Sderaadt {
1793df930be7Sderaadt 
1794aed035abSart 	if (ssir) {
1795aed035abSart 		(void) alpha_pal_swpipl(ALPHA_PSL_IPL_SOFT);
17962a2685f2Sart 		softintr_dispatch();
1797aed035abSart 	}
1798df930be7Sderaadt 
179950ce9ee0Sniklas 	return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
1800df930be7Sderaadt }
1801df930be7Sderaadt 
1802df930be7Sderaadt /*
1803417eba8cSderaadt  * Wait "n" microseconds.
1804417eba8cSderaadt  */
180550ce9ee0Sniklas void
1806417eba8cSderaadt delay(n)
180750ce9ee0Sniklas 	unsigned long n;
1808417eba8cSderaadt {
18095d097e9eSmiod 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
18105d097e9eSmiod 
18115d097e9eSmiod 	if (n == 0)
18125d097e9eSmiod 		return;
18135d097e9eSmiod 
18145d097e9eSmiod 	pcc0 = alpha_rpcc() & 0xffffffffUL;
18155d097e9eSmiod 	cycles = 0;
18165d097e9eSmiod 	usec = 0;
18175d097e9eSmiod 
18185d097e9eSmiod 	while (usec <= n) {
18195d097e9eSmiod 		/*
18205d097e9eSmiod 		 * Get the next CPU cycle count - assumes that we can not
18215d097e9eSmiod 		 * have had more than one 32 bit overflow.
18225d097e9eSmiod 		 */
18235d097e9eSmiod 		pcc1 = alpha_rpcc() & 0xffffffffUL;
18245d097e9eSmiod 		if (pcc1 < pcc0)
18255d097e9eSmiod 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
18265d097e9eSmiod 		else
18275d097e9eSmiod 			curcycle = pcc1 - pcc0;
1828417eba8cSderaadt 
1829aed035abSart 		/*
18305d097e9eSmiod 		 * We now have the number of processor cycles since we
18315d097e9eSmiod 		 * last checked. Add the current cycle count to the
18325d097e9eSmiod 		 * running total. If it's over cycles_per_usec, increment
18335d097e9eSmiod 		 * the usec counter.
1834aed035abSart 		 */
18355d097e9eSmiod 		cycles += curcycle;
183621c23d01Smiod 		while (cycles >= cycles_per_usec) {
18375d097e9eSmiod 			usec++;
18385d097e9eSmiod 			cycles -= cycles_per_usec;
18395d097e9eSmiod 		}
18405d097e9eSmiod 		pcc0 = pcc1;
18415d097e9eSmiod 	}
1842417eba8cSderaadt }
1843417eba8cSderaadt 
1844aed035abSart int
1845aed035abSart alpha_pa_access(pa)
1846aed035abSart 	u_long pa;
1847aed035abSart {
1848aed035abSart 	int i;
1849aed035abSart 
1850aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++) {
1851aed035abSart 		if (pa < mem_clusters[i].start)
1852aed035abSart 			continue;
1853aed035abSart 		if ((pa - mem_clusters[i].start) >=
1854aed035abSart 		    (mem_clusters[i].size & ~PAGE_MASK))
1855aed035abSart 			continue;
1856aed035abSart 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
1857aed035abSart 	}
1858aed035abSart 
1859aed035abSart 	/*
1860aed035abSart 	 * Address is not a memory address.  If we're secure, disallow
1861aed035abSart 	 * access.  Otherwise, grant read/write.
1862aed035abSart 	 */
1863aed035abSart 	if (securelevel > 0)
18641e8cdc2eSderaadt 		return (PROT_NONE);
1865aed035abSart 	else
18661e8cdc2eSderaadt 		return (PROT_READ | PROT_WRITE);
1867aed035abSart }
1868aed035abSart 
1869e464495eSniklas /* XXX XXX BEGIN XXX XXX */
1870aed035abSart paddr_t alpha_XXX_dmamap_or;					/* XXX */
1871e464495eSniklas 								/* XXX */
1872aed035abSart paddr_t								/* XXX */
1873e464495eSniklas alpha_XXX_dmamap(v)						/* XXX */
1874aed035abSart 	vaddr_t v;						/* XXX */
1875e464495eSniklas {								/* XXX */
1876e464495eSniklas 								/* XXX */
1877e464495eSniklas 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
1878e464495eSniklas }								/* XXX */
1879e464495eSniklas /* XXX XXX END XXX XXX */
1880