xref: /openbsd/sys/arch/alpha/alpha/machdep.c (revision 275e6a2f)
1*275e6a2fSmpi /* $OpenBSD: machdep.c,v 1.181 2017/05/29 14:19:49 mpi 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
108df930be7Sderaadt 
10945e5a1a0Sart #include <dev/pci/pcivar.h>
11045e5a1a0Sart 
11112f8bbedSniklas #ifdef DDB
11212f8bbedSniklas #include <machine/db_machdep.h>
11312f8bbedSniklas #include <ddb/db_extern.h>
11421c23d01Smiod #include <ddb/db_interface.h>
11512f8bbedSniklas #endif
11612f8bbedSniklas 
117a072164aSmiod #include "ioasic.h"
118a072164aSmiod 
119a072164aSmiod #if NIOASIC > 0
120a072164aSmiod #include <machine/tc_machdep.h>
121a072164aSmiod #include <dev/tc/tcreg.h>
122a072164aSmiod #include <dev/tc/ioasicvar.h>
123a072164aSmiod #endif
124a072164aSmiod 
125c4071fd1Smillert int	cpu_dump(void);
126c4071fd1Smillert int	cpu_dumpsize(void);
127c4071fd1Smillert u_long	cpu_dump_mempagecnt(void);
128c4071fd1Smillert void	dumpsys(void);
129c4071fd1Smillert void	identifycpu(void);
130c4071fd1Smillert void	regdump(struct trapframe *framep);
131c4071fd1Smillert void	printregs(struct reg *);
132df930be7Sderaadt 
133b426ab7bSthib struct uvm_constraint_range  isa_constraint = { 0x0, 0x00ffffffUL };
134b426ab7bSthib struct uvm_constraint_range  dma_constraint = { 0x0, (paddr_t)-1 };
135b426ab7bSthib struct uvm_constraint_range *uvm_md_constraints[] = {
136b426ab7bSthib 	&isa_constraint,
137b426ab7bSthib 	NULL
138b426ab7bSthib };
139b426ab7bSthib 
140ab8e80c5Sart struct vm_map *exec_map = NULL;
141ab8e80c5Sart struct vm_map *phys_map = NULL;
142aed035abSart 
143c72644a3Sderaadt /*
144c72644a3Sderaadt  * safepri is a safe priority for sleep to set for a spin-wait
145c72644a3Sderaadt  * during autoconfiguration or after a panic.
146c72644a3Sderaadt  */
147c72644a3Sderaadt int   safepri = 0;
148c72644a3Sderaadt 
14927626149Smatthieu #ifdef APERTURE
15027626149Smatthieu int allowaperture = 0;
15127626149Smatthieu #endif
15227626149Smatthieu 
153df930be7Sderaadt int	totalphysmem;		/* total amount of physical memory in system */
15474652a67Sniklas int	physmem;		/* physical mem used by OpenBSD + some rsvd */
155df930be7Sderaadt int	resvmem;		/* amount of memory reserved for PROM */
156df930be7Sderaadt int	unusedmem;		/* amount of memory for OS that we don't use */
157df930be7Sderaadt int	unknownmem;		/* amount of memory with an unknown use */
158df930be7Sderaadt 
159df930be7Sderaadt int	cputype;		/* system type, from the RPB */
160df930be7Sderaadt 
1612a2685f2Sart int	bootdev_debug = 0;	/* patchable, or from DDB */
1622a2685f2Sart 
163df930be7Sderaadt /* the following is used externally (sysctl_hw) */
164aed035abSart char	machine[] = MACHINE;		/* from <machine/param.h> */
165417eba8cSderaadt char	cpu_model[128];
166df930be7Sderaadt 
167df930be7Sderaadt struct	user *proc0paddr;
168df930be7Sderaadt 
169df930be7Sderaadt /* Number of machine cycles per microsecond */
170df930be7Sderaadt u_int64_t	cycles_per_usec;
171df930be7Sderaadt 
172aed035abSart struct bootinfo_kernel bootinfo;
173aed035abSart 
174b1560ceaSmiod struct consdev *cn_tab;
175b1560ceaSmiod 
176aed035abSart /* For built-in TCDS */
177aed035abSart #if defined(DEC_3000_300) || defined(DEC_3000_500)
178aed035abSart u_int8_t	dec_3000_scsiid[2], dec_3000_scsifast[2];
179aed035abSart #endif
180aed035abSart 
181aed035abSart struct platform platform;
182417eba8cSderaadt 
183417eba8cSderaadt /* for cpu_sysctl() */
18450ce9ee0Sniklas int	alpha_unaligned_print = 1;	/* warn about unaligned accesses */
18550ce9ee0Sniklas int	alpha_unaligned_fix = 1;	/* fix up unaligned accesses */
186881c1eabSart int	alpha_unaligned_sigbus = 1;	/* SIGBUS on fixed-up accesses */
187433075b6Spvalchev #ifndef NO_IEEE
188433075b6Spvalchev int	alpha_fp_sync_complete = 0;	/* fp fixup if sync even without /s */
189433075b6Spvalchev #endif
190a072164aSmiod #if NIOASIC > 0
19191c0e687Sderaadt int	alpha_led_blink = 1;
192a072164aSmiod #endif
19350ce9ee0Sniklas 
194b3cee53eSmartin /* used by hw_sysctl */
195b3cee53eSmartin extern char *hw_serial;
196b3cee53eSmartin 
197aed035abSart /*
198aed035abSart  * XXX This should be dynamically sized, but we have the chicken-egg problem!
199aed035abSart  * XXX it should also be larger than it is, because not all of the mddt
200aed035abSart  * XXX clusters end up being used for VM.
201aed035abSart  */
202aed035abSart phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];	/* low size bits overloaded */
203aed035abSart int	mem_cluster_cnt;
204aed035abSart 
2053a630e3fSniklas void
206dd3e8537Smiod alpha_init(unused, ptb, bim, bip, biv)
207dd3e8537Smiod 	u_long unused;
208df930be7Sderaadt 	u_long ptb;		/* PFN of current level 1 page table */
209aed035abSart 	u_long bim;		/* bootinfo magic */
210aed035abSart 	u_long bip;		/* bootinfo pointer */
211aed035abSart 	u_long biv;		/* bootinfo version */
212df930be7Sderaadt {
213aed035abSart 	extern char kernel_text[], _end[];
214df930be7Sderaadt 	struct mddt *mddtp;
215aed035abSart 	struct mddt_cluster *memc;
216df930be7Sderaadt 	int i, mddtweird;
217aed035abSart 	struct vm_physseg *vps;
218aed035abSart 	vaddr_t kernstart, kernend;
219aed035abSart 	paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
220df930be7Sderaadt 	char *p;
2212a2685f2Sart 	const char *bootinfo_msg;
222aed035abSart 	const struct cpuinit *c;
223aed035abSart 	extern caddr_t esym;
224aed035abSart 	struct cpu_info *ci;
225aed035abSart 	cpuid_t cpu_id;
226df930be7Sderaadt 
227aed035abSart 	/* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
228f3914c62Sniklas 
229df930be7Sderaadt 	/*
230aed035abSart 	 * Turn off interrupts (not mchecks) and floating point.
231df930be7Sderaadt 	 * Make sure the instruction and data streams are consistent.
232df930be7Sderaadt 	 */
233aed035abSart 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
23450ce9ee0Sniklas 	alpha_pal_wrfen(0);
23550ce9ee0Sniklas 	ALPHA_TBIA();
23650ce9ee0Sniklas 	alpha_pal_imb();
237df930be7Sderaadt 
238c62181b1Sbrad 	/* Initialize the SCB. */
239c62181b1Sbrad 	scb_init();
240c62181b1Sbrad 
241aed035abSart 	cpu_id = cpu_number();
242aed035abSart 
243aed035abSart #if defined(MULTIPROCESSOR)
244df930be7Sderaadt 	/*
245aed035abSart 	 * Set our SysValue to the address of our cpu_info structure.
246aed035abSart 	 * Secondary processors do this in their spinup trampoline.
247df930be7Sderaadt 	 */
24821c23d01Smiod 	alpha_pal_wrval((u_long)&cpu_info_primary);
24921c23d01Smiod 	cpu_info[cpu_id] = &cpu_info_primary;
250aed035abSart #endif
251aed035abSart 
252aed035abSart 	ci = curcpu();
253aed035abSart 	ci->ci_cpuid = cpu_id;
254aed035abSart 
255aed035abSart 	/*
256aed035abSart 	 * Get critical system information (if possible, from the
257aed035abSart 	 * information provided by the boot program).
258aed035abSart 	 */
259aed035abSart 	bootinfo_msg = NULL;
260aed035abSart 	if (bim == BOOTINFO_MAGIC) {
261aed035abSart 		if (biv == 0) {		/* backward compat */
262aed035abSart 			biv = *(u_long *)bip;
263aed035abSart 			bip += 8;
264aed035abSart 		}
265aed035abSart 		switch (biv) {
266aed035abSart 		case 1: {
267aed035abSart 			struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
268aed035abSart 
269aed035abSart 			bootinfo.ssym = v1p->ssym;
270aed035abSart 			bootinfo.esym = v1p->esym;
271aed035abSart 			/* hwrpb may not be provided by boot block in v1 */
272aed035abSart 			if (v1p->hwrpb != NULL) {
273aed035abSart 				bootinfo.hwrpb_phys =
274aed035abSart 				    ((struct rpb *)v1p->hwrpb)->rpb_phys;
275aed035abSart 				bootinfo.hwrpb_size = v1p->hwrpbsize;
276aed035abSart 			} else {
277aed035abSart 				bootinfo.hwrpb_phys =
278aed035abSart 				    ((struct rpb *)HWRPB_ADDR)->rpb_phys;
279aed035abSart 				bootinfo.hwrpb_size =
280aed035abSart 				    ((struct rpb *)HWRPB_ADDR)->rpb_size;
281aed035abSart 			}
282aed035abSart 			bcopy(v1p->boot_flags, bootinfo.boot_flags,
283aed035abSart 			    min(sizeof v1p->boot_flags,
284aed035abSart 			      sizeof bootinfo.boot_flags));
285aed035abSart 			bcopy(v1p->booted_kernel, bootinfo.booted_kernel,
286aed035abSart 			    min(sizeof v1p->booted_kernel,
287aed035abSart 			      sizeof bootinfo.booted_kernel));
288aed035abSart 			/* booted dev not provided in bootinfo */
289aed035abSart 			init_prom_interface((struct rpb *)
290aed035abSart 			    ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
291aed035abSart                 	prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
292aed035abSart 			    sizeof bootinfo.booted_dev);
293aed035abSart 			break;
294aed035abSart 		}
295aed035abSart 		default:
296aed035abSart 			bootinfo_msg = "unknown bootinfo version";
297aed035abSart 			goto nobootinfo;
298aed035abSart 		}
299aed035abSart 	} else {
300aed035abSart 		bootinfo_msg = "boot program did not pass bootinfo";
301aed035abSart nobootinfo:
302aed035abSart 		bootinfo.ssym = (u_long)_end;
303aed035abSart 		bootinfo.esym = (u_long)_end;
304aed035abSart 		bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
305aed035abSart 		bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
306aed035abSart 		init_prom_interface((struct rpb *)HWRPB_ADDR);
307aed035abSart 		prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
308aed035abSart 		    sizeof bootinfo.boot_flags);
309aed035abSart 		prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
310aed035abSart 		    sizeof bootinfo.booted_kernel);
311aed035abSart 		prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
312aed035abSart 		    sizeof bootinfo.booted_dev);
313aed035abSart 	}
314aed035abSart 
315aed035abSart 	esym = (caddr_t)bootinfo.esym;
316aed035abSart 	/*
317aed035abSart 	 * Initialize the kernel's mapping of the RPB.  It's needed for
318aed035abSart 	 * lots of things.
319aed035abSart 	 */
320aed035abSart 	hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
321aed035abSart 
322aed035abSart #if defined(DEC_3000_300) || defined(DEC_3000_500)
323aed035abSart 	if (hwrpb->rpb_type == ST_DEC_3000_300 ||
324aed035abSart 	    hwrpb->rpb_type == ST_DEC_3000_500) {
325aed035abSart 		prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
326aed035abSart 		    sizeof(dec_3000_scsiid));
327aed035abSart 		prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
328aed035abSart 		    sizeof(dec_3000_scsifast));
329aed035abSart 	}
330aed035abSart #endif
331df930be7Sderaadt 
332df930be7Sderaadt 	/*
333df930be7Sderaadt 	 * Remember how many cycles there are per microsecond,
334df930be7Sderaadt 	 * so that we can use delay().  Round up, for safety.
335df930be7Sderaadt 	 */
336df930be7Sderaadt 	cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
337df930be7Sderaadt 
338df930be7Sderaadt 	/*
3399e71c994Saaron 	 * Initialize the (temporary) bootstrap console interface, so
340aed035abSart 	 * we can use printf until the VM system starts being setup.
341aed035abSart 	 * The real console is initialized before then.
342df930be7Sderaadt 	 */
343aed035abSart 	init_bootstrap_console();
344aed035abSart 
345aed035abSart 	/* OUTPUT NOW ALLOWED */
346aed035abSart 
347aed035abSart 	/* delayed from above */
348aed035abSart 	if (bootinfo_msg)
349aed035abSart 		printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
350aed035abSart 		    bootinfo_msg, bim, bip, biv);
351aed035abSart 
352aed035abSart 	/* Initialize the trap vectors on the primary processor. */
353aed035abSart 	trap_init();
354df930be7Sderaadt 
355df930be7Sderaadt 	/*
356aed035abSart 	 * Find out what hardware we're on, and do basic initialization.
357df930be7Sderaadt 	 */
358aed035abSart 	cputype = hwrpb->rpb_type;
359aed035abSart 	if (cputype < 0) {
360aed035abSart 		/*
361aed035abSart 		 * At least some white-box systems have SRM which
362aed035abSart 		 * reports a systype that's the negative of their
363aed035abSart 		 * blue-box counterpart.
364aed035abSart 		 */
365aed035abSart 		cputype = -cputype;
366aed035abSart 	}
367aed035abSart 	c = platform_lookup(cputype);
368aed035abSart 	if (c == NULL) {
369aed035abSart 		platform_not_supported();
370aed035abSart 		/* NOTREACHED */
371aed035abSart 	}
372aed035abSart 	(*c->init)();
373094fa01fSderaadt 	strlcpy(cpu_model, platform.model, sizeof cpu_model);
37450ce9ee0Sniklas 
37550ce9ee0Sniklas 	/*
3769e71c994Saaron 	 * Initialize the real console, so that the bootstrap console is
377aed035abSart 	 * no longer necessary.
37850ce9ee0Sniklas 	 */
379aed035abSart 	(*platform.cons_init)();
380aed035abSart 
381a55851f4Sderaadt #if 0
382aed035abSart 	/* Paranoid sanity checking */
383aed035abSart 
384aed035abSart 	assert(hwrpb->rpb_primary_cpu_id == alpha_pal_whami());
385aed035abSart 
386aed035abSart 	/*
387aed035abSart 	 * On single-CPU systypes, the primary should always be CPU 0,
388aed035abSart 	 * except on Alpha 8200 systems where the CPU id is related
389aed035abSart 	 * to the VID, which is related to the Turbo Laser node id.
390aed035abSart 	 */
391aed035abSart 	if (cputype != ST_DEC_21000)
392aed035abSart 		assert(hwrpb->rpb_primary_cpu_id == 0);
393aed035abSart #endif
394aed035abSart 
395aed035abSart 	/* NO MORE FIRMWARE ACCESS ALLOWED */
396aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
397aed035abSart 	/*
398aed035abSart 	 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
399aed035abSart 	 * XXX pmap_uses_prom_console() evaluates to non-zero.)
400aed035abSart 	 */
401aed035abSart #endif
402aed035abSart 
403cfcdef40Smiod #ifndef SMALL_KERNEL
404cfcdef40Smiod 	/*
405cfcdef40Smiod 	 * If we run on a BWX-capable processor, override cpu_switch
406cfcdef40Smiod 	 * with a faster version.
407cfcdef40Smiod 	 * We do this now because the kernel text might be mapped
408cfcdef40Smiod 	 * read-only eventually (although this is not the case at the moment).
409cfcdef40Smiod 	 */
410cfcdef40Smiod 	if (alpha_implver() >= ALPHA_IMPLVER_EV5) {
411cfcdef40Smiod 		if (~alpha_amask(ALPHA_AMASK_BWX) != 0) {
412cfcdef40Smiod 			extern vaddr_t __bwx_switch0, __bwx_switch1,
413cfcdef40Smiod 			    __bwx_switch2, __bwx_switch3;
414cfcdef40Smiod 			u_int32_t *dst, *src, *end;
415cfcdef40Smiod 
416cfcdef40Smiod 			src = (u_int32_t *)&__bwx_switch2;
417cfcdef40Smiod 			end = (u_int32_t *)&__bwx_switch3;
418cfcdef40Smiod 			dst = (u_int32_t *)&__bwx_switch0;
419cfcdef40Smiod 			while (src != end)
420cfcdef40Smiod 				*dst++ = *src++;
421cfcdef40Smiod 			src = (u_int32_t *)&__bwx_switch1;
422cfcdef40Smiod 			end = (u_int32_t *)&__bwx_switch2;
423cfcdef40Smiod 			while (src != end)
424cfcdef40Smiod 				*dst++ = *src++;
425cfcdef40Smiod 		}
426cfcdef40Smiod 	}
427cfcdef40Smiod #endif
428cfcdef40Smiod 
429aed035abSart 	/*
430aed035abSart 	 * find out this system's page size
431aed035abSart 	 */
43273b9fe7cSart 	if ((uvmexp.pagesize = hwrpb->rpb_page_size) != 8192)
43373b9fe7cSart 		panic("page size %d != 8192?!", uvmexp.pagesize);
434aed035abSart 
435aed035abSart 	uvm_setpagesize();
436aed035abSart 
437aed035abSart 	/*
438aed035abSart 	 * Find the beginning and end of the kernel (and leave a
439aed035abSart 	 * bit of space before the beginning for the bootstrap
440aed035abSart 	 * stack).
441aed035abSart 	 */
442aed035abSart 	kernstart = trunc_page((vaddr_t)kernel_text) - 2 * PAGE_SIZE;
443aed035abSart 	kernend = (vaddr_t)round_page((vaddr_t)bootinfo.esym);
444aed035abSart 
445aed035abSart 	kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
446aed035abSart 	kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
447df930be7Sderaadt 
448df930be7Sderaadt 	/*
449df930be7Sderaadt 	 * Find out how much memory is available, by looking at
450df930be7Sderaadt 	 * the memory cluster descriptors.  This also tries to do
451df930be7Sderaadt 	 * its best to detect things things that have never been seen
452df930be7Sderaadt 	 * before...
453df930be7Sderaadt 	 */
454df930be7Sderaadt 	mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
455df930be7Sderaadt 
456aed035abSart 	/* MDDT SANITY CHECKING */
457df930be7Sderaadt 	mddtweird = 0;
458aed035abSart 	if (mddtp->mddt_cluster_cnt < 2) {
459df930be7Sderaadt 		mddtweird = 1;
460aed035abSart 		printf("WARNING: weird number of mem clusters: %lu\n",
461e86d96d5Smiod 		    (unsigned long)mddtp->mddt_cluster_cnt);
462df930be7Sderaadt 	}
463df930be7Sderaadt 
464aed035abSart #if 0
465aed035abSart 	printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
466aed035abSart #endif
467df930be7Sderaadt 
468aed035abSart 	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
469aed035abSart 		memc = &mddtp->mddt_clusters[i];
470aed035abSart #if 0
471aed035abSart 		printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
472aed035abSart 		    memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
473aed035abSart #endif
474aed035abSart 		totalphysmem += memc->mddt_pg_cnt;
475aed035abSart 		if (mem_cluster_cnt < VM_PHYSSEG_MAX) {	/* XXX */
476aed035abSart 			mem_clusters[mem_cluster_cnt].start =
477aed035abSart 			    ptoa(memc->mddt_pfn);
478aed035abSart 			mem_clusters[mem_cluster_cnt].size =
479aed035abSart 			    ptoa(memc->mddt_pg_cnt);
480aed035abSart 			if (memc->mddt_usage & MDDT_mbz ||
481aed035abSart 			    memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
482aed035abSart 			    memc->mddt_usage & MDDT_PALCODE)
483aed035abSart 				mem_clusters[mem_cluster_cnt].size |=
4841e8cdc2eSderaadt 				    PROT_READ;
485aed035abSart 			else
486aed035abSart 				mem_clusters[mem_cluster_cnt].size |=
4871e8cdc2eSderaadt 				    PROT_READ | PROT_WRITE | PROT_EXEC;
488aed035abSart 			mem_cluster_cnt++;
489ee2d823aSmiod 		} /* XXX else print something! */
490aed035abSart 
491aed035abSart 		if (memc->mddt_usage & MDDT_mbz) {
492aed035abSart 			mddtweird = 1;
493aed035abSart 			printf("WARNING: mem cluster %d has weird "
494e86d96d5Smiod 			    "usage 0x%lx\n", i, (long)memc->mddt_usage);
495aed035abSart 			unknownmem += memc->mddt_pg_cnt;
496aed035abSart 			continue;
497aed035abSart 		}
498aed035abSart 		if (memc->mddt_usage & MDDT_NONVOLATILE) {
499aed035abSart 			/* XXX should handle these... */
500aed035abSart 			printf("WARNING: skipping non-volatile mem "
501aed035abSart 			    "cluster %d\n", i);
502aed035abSart 			unusedmem += memc->mddt_pg_cnt;
503aed035abSart 			continue;
504aed035abSart 		}
505aed035abSart 		if (memc->mddt_usage & MDDT_PALCODE) {
506aed035abSart 			resvmem += memc->mddt_pg_cnt;
507aed035abSart 			continue;
508aed035abSart 		}
509aed035abSart 
510aed035abSart 		/*
511aed035abSart 		 * We have a memory cluster available for system
512aed035abSart 		 * software use.  We must determine if this cluster
513aed035abSart 		 * holds the kernel.
514aed035abSart 		 */
515aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
516aed035abSart 		/*
517aed035abSart 		 * XXX If the kernel uses the PROM console, we only use the
518aed035abSart 		 * XXX memory after the kernel in the first system segment,
519aed035abSart 		 * XXX to avoid clobbering prom mapping, data, etc.
520aed035abSart 		 */
521aed035abSart 	    if (!pmap_uses_prom_console() || physmem == 0) {
522aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
523aed035abSart 		physmem += memc->mddt_pg_cnt;
524aed035abSart 		pfn0 = memc->mddt_pfn;
525aed035abSart 		pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
526aed035abSart 		if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
527aed035abSart 			/*
528aed035abSart 			 * Must compute the location of the kernel
529aed035abSart 			 * within the segment.
530aed035abSart 			 */
531aed035abSart #if 0
532aed035abSart 			printf("Cluster %d contains kernel\n", i);
533aed035abSart #endif
534aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
535aed035abSart 		    if (!pmap_uses_prom_console()) {
536aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
537aed035abSart 			if (pfn0 < kernstartpfn) {
538aed035abSart 				/*
539aed035abSart 				 * There is a chunk before the kernel.
540aed035abSart 				 */
541aed035abSart #if 0
542aed035abSart 				printf("Loading chunk before kernel: "
543aed035abSart 				    "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
544aed035abSart #endif
545aed035abSart 				uvm_page_physload(pfn0, kernstartpfn,
5462ce3b4a8Soga 				    pfn0, kernstartpfn, 0);
547aed035abSart 			}
548aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
549aed035abSart 		    }
550aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
551aed035abSart 			if (kernendpfn < pfn1) {
552aed035abSart 				/*
553aed035abSart 				 * There is a chunk after the kernel.
554aed035abSart 				 */
555aed035abSart #if 0
556aed035abSart 				printf("Loading chunk after kernel: "
557aed035abSart 				    "0x%lx / 0x%lx\n", kernendpfn, pfn1);
558aed035abSart #endif
559aed035abSart 				uvm_page_physload(kernendpfn, pfn1,
5602ce3b4a8Soga 				    kernendpfn, pfn1, 0);
561aed035abSart 			}
562aed035abSart 		} else {
563aed035abSart 			/*
564aed035abSart 			 * Just load this cluster as one chunk.
565aed035abSart 			 */
566aed035abSart #if 0
567aed035abSart 			printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
568aed035abSart 			    pfn0, pfn1);
569aed035abSart #endif
5702ce3b4a8Soga 			uvm_page_physload(pfn0, pfn1, pfn0, pfn1, 0);
571aed035abSart 		}
572aed035abSart #ifdef _PMAP_MAY_USE_PROM_CONSOLE
573aed035abSart 	    }
574aed035abSart #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
575aed035abSart 	}
576aed035abSart 
577a37778bcSderaadt #ifdef DEBUG
578aed035abSart 	/*
579aed035abSart 	 * Dump out the MDDT if it looks odd...
580aed035abSart 	 */
581df930be7Sderaadt 	if (mddtweird) {
582df930be7Sderaadt 		printf("\n");
583df930be7Sderaadt 		printf("complete memory cluster information:\n");
584df930be7Sderaadt 		for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
585df930be7Sderaadt 			printf("mddt %d:\n", i);
586df930be7Sderaadt 			printf("\tpfn %lx\n",
587df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pfn);
588df930be7Sderaadt 			printf("\tcnt %lx\n",
589df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pg_cnt);
590df930be7Sderaadt 			printf("\ttest %lx\n",
591df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_pg_test);
592df930be7Sderaadt 			printf("\tbva %lx\n",
593df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_v_bitaddr);
594df930be7Sderaadt 			printf("\tbpa %lx\n",
595df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_p_bitaddr);
596df930be7Sderaadt 			printf("\tbcksum %lx\n",
597df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_bit_cksum);
598df930be7Sderaadt 			printf("\tusage %lx\n",
599df930be7Sderaadt 			    mddtp->mddt_clusters[i].mddt_usage);
600df930be7Sderaadt 		}
601df930be7Sderaadt 		printf("\n");
602df930be7Sderaadt 	}
603a37778bcSderaadt #endif
604df930be7Sderaadt 
605df930be7Sderaadt 	if (totalphysmem == 0)
606df930be7Sderaadt 		panic("can't happen: system seems to have no memory!");
607df930be7Sderaadt #if 0
608f46637d1Sderaadt 	printf("totalphysmem = %u\n", totalphysmem);
609f46637d1Sderaadt 	printf("physmem = %u\n", physmem);
610df930be7Sderaadt 	printf("resvmem = %d\n", resvmem);
611df930be7Sderaadt 	printf("unusedmem = %d\n", unusedmem);
612df930be7Sderaadt 	printf("unknownmem = %d\n", unknownmem);
613df930be7Sderaadt #endif
614df930be7Sderaadt 
615df930be7Sderaadt 	/*
616aed035abSart 	 * Initialize error message buffer (at end of core).
617df930be7Sderaadt 	 */
618aed035abSart 	{
619aed035abSart 		vsize_t sz = (vsize_t)round_page(MSGBUFSIZE);
620aed035abSart 		vsize_t reqsz = sz;
621df930be7Sderaadt 
622aed035abSart 		vps = &vm_physmem[vm_nphysseg - 1];
623e1da84e1Salex 
624aed035abSart 		/* shrink so that it'll fit in the last segment */
625aed035abSart 		if ((vps->avail_end - vps->avail_start) < atop(sz))
626aed035abSart 			sz = ptoa(vps->avail_end - vps->avail_start);
627aed035abSart 
628aed035abSart 		vps->end -= atop(sz);
629aed035abSart 		vps->avail_end -= atop(sz);
630aed035abSart 		initmsgbuf((caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end)), sz);
631aed035abSart 
632aed035abSart 		/* Remove the last segment if it now has no pages. */
633aed035abSart 		if (vps->start == vps->end)
634aed035abSart 			vm_nphysseg--;
635aed035abSart 
636aed035abSart 		/* warn if the message buffer had to be shrunk */
637aed035abSart 		if (sz != reqsz)
638aed035abSart 			printf("WARNING: %ld bytes not available for msgbuf "
639aed035abSart 			    "in last cluster (%ld used)\n", reqsz, sz);
640aed035abSart 
641aed035abSart 	}
642aed035abSart 
643df930be7Sderaadt 	/*
644df930be7Sderaadt 	 * Init mapping for u page(s) for proc 0
645df930be7Sderaadt 	 */
646aed035abSart 	proc0.p_addr = proc0paddr =
647aed035abSart 	    (struct user *)pmap_steal_memory(UPAGES * PAGE_SIZE, NULL, NULL);
648df930be7Sderaadt 
649df930be7Sderaadt 	/*
650df930be7Sderaadt 	 * Initialize the virtual memory system, and set the
651df930be7Sderaadt 	 * page table base register in proc 0's PCB.
652df930be7Sderaadt 	 */
653aed035abSart 	pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
654aed035abSart 	    hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
655df930be7Sderaadt 
656df930be7Sderaadt 	/*
657df930be7Sderaadt 	 * Initialize the rest of proc 0's PCB, and cache its physical
658df930be7Sderaadt 	 * address.
659df930be7Sderaadt 	 */
660df930be7Sderaadt 	proc0.p_md.md_pcbpaddr =
661aed035abSart 	    (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
662df930be7Sderaadt 
663df930be7Sderaadt 	/*
664df930be7Sderaadt 	 * Set the kernel sp, reserving space for an (empty) trapframe,
665df930be7Sderaadt 	 * and make proc0's trapframe pointer point to it for sanity.
666df930be7Sderaadt 	 */
66750ce9ee0Sniklas 	proc0paddr->u_pcb.pcb_hw.apcb_ksp =
668df930be7Sderaadt 	    (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
66974652a67Sniklas 	proc0.p_md.md_tf =
67074652a67Sniklas 	    (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
67150ce9ee0Sniklas 
672aed035abSart 	/*
673aed035abSart 	 * Initialize the primary CPU's idle PCB to proc0's.  In a
674aed035abSart 	 * MULTIPROCESSOR configuration, each CPU will later get
675aed035abSart 	 * its own idle PCB when autoconfiguration runs.
676aed035abSart 	 */
677aed035abSart 	ci->ci_idle_pcb = &proc0paddr->u_pcb;
678aed035abSart 	ci->ci_idle_pcb_paddr = (u_long)proc0.p_md.md_pcbpaddr;
679df930be7Sderaadt 
680df930be7Sderaadt 	/*
681df930be7Sderaadt 	 * Look at arguments passed to us and compute boothowto.
682df930be7Sderaadt 	 */
683417eba8cSderaadt 
684aed035abSart 	for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
685417eba8cSderaadt 		/*
686417eba8cSderaadt 		 * Note that we'd really like to differentiate case here,
687417eba8cSderaadt 		 * but the Alpha AXP Architecture Reference Manual
688417eba8cSderaadt 		 * says that we shouldn't.
689417eba8cSderaadt 		 */
690df930be7Sderaadt 		switch (*p) {
691371c77f5Smartin 		case 'a': /* Ignore */
692417eba8cSderaadt 		case 'A':
693df930be7Sderaadt 			break;
694df930be7Sderaadt 
69512f8bbedSniklas 		case 'b': /* Enter DDB as soon as the console is initialised */
69612f8bbedSniklas 		case 'B':
69712f8bbedSniklas 			boothowto |= RB_KDB;
69812f8bbedSniklas 			break;
69912f8bbedSniklas 
70050ce9ee0Sniklas 		case 'c': /* enter user kernel configuration */
70150ce9ee0Sniklas 		case 'C':
70250ce9ee0Sniklas 			boothowto |= RB_CONFIG;
70350ce9ee0Sniklas 			break;
70450ce9ee0Sniklas 
70550ce9ee0Sniklas #ifdef DEBUG
70650ce9ee0Sniklas 		case 'd': /* crash dump immediately after autoconfig */
70750ce9ee0Sniklas 		case 'D':
70850ce9ee0Sniklas 			boothowto |= RB_DUMP;
70950ce9ee0Sniklas 			break;
71050ce9ee0Sniklas #endif
71150ce9ee0Sniklas 
71250ce9ee0Sniklas 		case 'h': /* always halt, never reboot */
71350ce9ee0Sniklas 		case 'H':
71450ce9ee0Sniklas 			boothowto |= RB_HALT;
715df930be7Sderaadt 			break;
716df930be7Sderaadt 
71750ce9ee0Sniklas 
71850ce9ee0Sniklas 		case 'n': /* askname */
71950ce9ee0Sniklas 		case 'N':
72050ce9ee0Sniklas 			boothowto |= RB_ASKNAME;
72150ce9ee0Sniklas 			break;
722aed035abSart 
723371c77f5Smartin 		case 's': /* single-user */
724aed035abSart 		case 'S':
725aed035abSart 			boothowto |= RB_SINGLE;
726aed035abSart 			break;
727aed035abSart 
728aed035abSart 		case '-':
729aed035abSart 			/*
730aed035abSart 			 * Just ignore this.  It's not required, but it's
731aed035abSart 			 * common for it to be passed regardless.
732aed035abSart 			 */
733aed035abSart 			break;
734aed035abSart 
735aed035abSart 		default:
736aed035abSart 			printf("Unrecognized boot flag '%c'.\n", *p);
737aed035abSart 			break;
738df930be7Sderaadt 		}
739df930be7Sderaadt 	}
740df930be7Sderaadt 
741aed035abSart 
742df930be7Sderaadt 	/*
743df930be7Sderaadt 	 * Figure out the number of cpus in the box, from RPB fields.
744df930be7Sderaadt 	 * Really.  We mean it.
745df930be7Sderaadt 	 */
74631326ec3Smiod 	for (ncpusfound = 0, i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
747df930be7Sderaadt 		struct pcs *pcsp;
748df930be7Sderaadt 
749aed035abSart 		pcsp = LOCATE_PCS(hwrpb, i);
750df930be7Sderaadt 		if ((pcsp->pcs_flags & PCS_PP) != 0)
75131326ec3Smiod 			ncpusfound++;
752df930be7Sderaadt 	}
753aed035abSart 
754aed035abSart 	/*
755aed035abSart 	 * Initialize debuggers, and break into them if appropriate.
756aed035abSart 	 */
757aed035abSart #ifdef DDB
75821c23d01Smiod 	db_machine_init();
759aed035abSart 	ddb_init();
760aed035abSart 
761aed035abSart 	if (boothowto & RB_KDB)
762e97088d6Smpi 		db_enter();
763aed035abSart #endif
764aed035abSart 	/*
765aed035abSart 	 * Figure out our clock frequency, from RPB fields.
766aed035abSart 	 */
767aed035abSart 	hz = hwrpb->rpb_intr_freq >> 12;
768aed035abSart 	if (!(60 <= hz && hz <= 10240)) {
769aed035abSart #ifdef DIAGNOSTIC
770e86d96d5Smiod 		printf("WARNING: unbelievable rpb_intr_freq: %lu (%d hz)\n",
771e86d96d5Smiod 			(unsigned long)hwrpb->rpb_intr_freq, hz);
772aed035abSart #endif
773dd3e8537Smiod 		hz = 1024;
774aed035abSart 	}
775aed035abSart }
776aed035abSart 
777417eba8cSderaadt void
778df930be7Sderaadt consinit()
779df930be7Sderaadt {
780aed035abSart 
781aed035abSart 	/*
782aed035abSart 	 * Everything related to console initialization is done
783aed035abSart 	 * in alpha_init().
784aed035abSart 	 */
785aed035abSart #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
786aed035abSart 	printf("consinit: %susing prom console\n",
787aed035abSart 	    pmap_uses_prom_console() ? "" : "not ");
78812f8bbedSniklas #endif
789df930be7Sderaadt }
790df930be7Sderaadt 
791417eba8cSderaadt void
792df930be7Sderaadt cpu_startup()
793df930be7Sderaadt {
794aed035abSart 	vaddr_t minaddr, maxaddr;
79550ce9ee0Sniklas #if defined(DEBUG)
796df930be7Sderaadt 	extern int pmapdebug;
797df930be7Sderaadt 	int opmapdebug = pmapdebug;
798df930be7Sderaadt 
799df930be7Sderaadt 	pmapdebug = 0;
800df930be7Sderaadt #endif
801df930be7Sderaadt 
802df930be7Sderaadt 	/*
803df930be7Sderaadt 	 * Good {morning,afternoon,evening,night}.
804df930be7Sderaadt 	 */
805df930be7Sderaadt 	printf(version);
806df930be7Sderaadt 	identifycpu();
807701cd583Smiod 	printf("real mem = %lu (%luMB)\n", ptoa((psize_t)totalphysmem),
808701cd583Smiod 	    ptoa((psize_t)totalphysmem) / 1024 / 1024);
809701cd583Smiod 	printf("rsvd mem = %lu (%luMB)\n", ptoa((psize_t)resvmem),
810701cd583Smiod 	    ptoa((psize_t)resvmem) / 1024 / 1024);
811aed035abSart 	if (unusedmem) {
812701cd583Smiod 		printf("WARNING: unused memory = %lu (%luMB)\n",
813701cd583Smiod 		    ptoa((psize_t)unusedmem),
814701cd583Smiod 		    ptoa((psize_t)unusedmem) / 1024 / 1024);
815aed035abSart 	}
816aed035abSart 	if (unknownmem) {
817701cd583Smiod 		printf("WARNING: %lu (%luMB) of memory with unknown purpose\n",
818701cd583Smiod 		    ptoa((psize_t)unknownmem),
819701cd583Smiod 		    ptoa((psize_t)unknownmem) / 1024 / 1024);
820aed035abSart 	}
821df930be7Sderaadt 
822df930be7Sderaadt 	/*
823df930be7Sderaadt 	 * Allocate a submap for exec arguments.  This map effectively
824df930be7Sderaadt 	 * limits the number of processes exec'ing at any time.
825df930be7Sderaadt 	 */
8267c10a71dSdrahn 	minaddr = vm_map_min(kernel_map);
827aed035abSart 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
828aed035abSart 	    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
829df930be7Sderaadt 
830df930be7Sderaadt 	/*
831df930be7Sderaadt 	 * Allocate a submap for physio
832df930be7Sderaadt 	 */
833aed035abSart 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
834aed035abSart 	    VM_PHYS_SIZE, 0, FALSE, NULL);
835df930be7Sderaadt 
83650ce9ee0Sniklas #if defined(DEBUG)
837df930be7Sderaadt 	pmapdebug = opmapdebug;
838df930be7Sderaadt #endif
839701cd583Smiod 	printf("avail mem = %lu (%luMB)\n", ptoa((psize_t)uvmexp.free),
840701cd583Smiod 	    ptoa((psize_t)uvmexp.free) / 1024 / 1024);
841aed035abSart #if 0
842aed035abSart 	{
843aed035abSart 		extern u_long pmap_pages_stolen;
844aed035abSart 
845aed035abSart 		printf("stolen memory for VM structures = %d\n", pmap_pages_stolen * PAGE_SIZE);
846aed035abSart 	}
847aed035abSart #endif
848df930be7Sderaadt 
849df930be7Sderaadt 	/*
850df930be7Sderaadt 	 * Set up buffers, so they can be used to read disk labels.
851df930be7Sderaadt 	 */
852df930be7Sderaadt 	bufinit();
853df930be7Sderaadt 
854df930be7Sderaadt 	/*
855df930be7Sderaadt 	 * Configure the system.
856df930be7Sderaadt 	 */
85741033391Sderaadt 	if (boothowto & RB_CONFIG) {
85841033391Sderaadt #ifdef BOOT_CONFIG
85941033391Sderaadt 		user_config();
86041033391Sderaadt #else
86141033391Sderaadt 		printf("kernel does not support -c; continuing..\n");
86241033391Sderaadt #endif
86341033391Sderaadt 	}
86450ce9ee0Sniklas 
86550ce9ee0Sniklas 	/*
866aed035abSart 	 * Set up the HWPCB so that it's safe to configure secondary
867aed035abSart 	 * CPUs.
86850ce9ee0Sniklas 	 */
869aed035abSart 	hwrpb_primary_init();
870aed035abSart }
871aed035abSart 
872aed035abSart /*
873aed035abSart  * Retrieve the platform name from the DSR.
874aed035abSart  */
875aed035abSart const char *
876aed035abSart alpha_dsr_sysname()
877aed035abSart {
878aed035abSart 	struct dsrdb *dsr;
879aed035abSart 	const char *sysname;
880aed035abSart 
881aed035abSart 	/*
882aed035abSart 	 * DSR does not exist on early HWRPB versions.
883aed035abSart 	 */
884aed035abSart 	if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
885aed035abSart 		return (NULL);
886aed035abSart 
887aed035abSart 	dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
888aed035abSart 	sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
889aed035abSart 	    sizeof(u_int64_t)));
890aed035abSart 	return (sysname);
891aed035abSart }
892aed035abSart 
893aed035abSart /*
894aed035abSart  * Lookup the system specified system variation in the provided table,
895aed035abSart  * returning the model string on match.
896aed035abSart  */
897aed035abSart const char *
898aed035abSart alpha_variation_name(variation, avtp)
899aed035abSart 	u_int64_t variation;
900aed035abSart 	const struct alpha_variation_table *avtp;
901aed035abSart {
902aed035abSart 	int i;
903aed035abSart 
904aed035abSart 	for (i = 0; avtp[i].avt_model != NULL; i++)
905aed035abSart 		if (avtp[i].avt_variation == variation)
906aed035abSart 			return (avtp[i].avt_model);
907aed035abSart 	return (NULL);
908aed035abSart }
909aed035abSart 
910aed035abSart /*
911aed035abSart  * Generate a default platform name based for unknown system variations.
912aed035abSart  */
913aed035abSart const char *
914aed035abSart alpha_unknown_sysname()
915aed035abSart {
916aed035abSart 	static char s[128];		/* safe size */
917aed035abSart 
918d5eb2d9aSderaadt 	snprintf(s, sizeof s, "%s family, unknown model variation 0x%lx",
919e86d96d5Smiod 	    platform.family, (unsigned long)hwrpb->rpb_variation & SV_ST_MASK);
920aed035abSart 	return ((const char *)s);
921df930be7Sderaadt }
922df930be7Sderaadt 
92350ce9ee0Sniklas void
924df930be7Sderaadt identifycpu()
925df930be7Sderaadt {
926aed035abSart 	char *s;
927b3cee53eSmartin 	int slen;
928df930be7Sderaadt 
929df930be7Sderaadt 	/*
930df930be7Sderaadt 	 * print out CPU identification information.
931df930be7Sderaadt 	 */
932aed035abSart 	printf("%s", cpu_model);
933aed035abSart 	for(s = cpu_model; *s; ++s)
934aed035abSart 		if(strncasecmp(s, "MHz", 3) == 0)
935aed035abSart 			goto skipMHz;
936e86d96d5Smiod 	printf(", %luMHz", (unsigned long)hwrpb->rpb_cc_freq / 1000000);
937aed035abSart skipMHz:
938b3cee53eSmartin 	/* fill in hw_serial if a serial number is known */
939b3cee53eSmartin 	slen = strlen(hwrpb->rpb_ssn) + 1;
940b3cee53eSmartin 	if (slen > 1) {
941b3cee53eSmartin 		hw_serial = malloc(slen, M_SYSCTL, M_NOWAIT);
942b3cee53eSmartin 		if (hw_serial)
943b3cee53eSmartin 			strlcpy(hw_serial, (char *)hwrpb->rpb_ssn, slen);
944b3cee53eSmartin 	}
945b3cee53eSmartin 
946aed035abSart 	printf("\n");
947e86d96d5Smiod 	printf("%lu byte page size, %d processor%s.\n",
948e86d96d5Smiod 	    (unsigned long)hwrpb->rpb_page_size, ncpusfound,
949e86d96d5Smiod 	    ncpusfound == 1 ? "" : "s");
950df930be7Sderaadt #if 0
951b3cee53eSmartin 	/* this is not particularly useful! */
952df930be7Sderaadt 	printf("variation: 0x%lx, revision 0x%lx\n",
953df930be7Sderaadt 	    hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
954df930be7Sderaadt #endif
955df930be7Sderaadt }
956df930be7Sderaadt 
957df930be7Sderaadt int	waittime = -1;
958df930be7Sderaadt struct pcb dumppcb;
959df930be7Sderaadt 
960ff261808Suebayasi __dead void
961ff261808Suebayasi boot(int howto)
962df930be7Sderaadt {
963aed035abSart #if defined(MULTIPROCESSOR)
96421c23d01Smiod 	u_long wait_mask;
96521c23d01Smiod 	int i;
966aed035abSart #endif
967aed035abSart 
968df930be7Sderaadt 	if (cold) {
969c9ad5066Stom 		if ((howto & RB_USERREQ) == 0)
970df930be7Sderaadt 			howto |= RB_HALT;
971df930be7Sderaadt 		goto haltsys;
972df930be7Sderaadt 	}
973df930be7Sderaadt 
97450ce9ee0Sniklas 	if ((boothowto & RB_HALT) != 0)
97550ce9ee0Sniklas 		howto |= RB_HALT;
97650ce9ee0Sniklas 
977df930be7Sderaadt 	boothowto = howto;
978df930be7Sderaadt 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
979df930be7Sderaadt 		waittime = 0;
980df930be7Sderaadt 		vfs_shutdown();
98115be954bSuebayasi 
9822417125dSmiod 		if ((howto & RB_TIMEBAD) == 0) {
983df930be7Sderaadt 			resettodr();
9842417125dSmiod 		} else {
9852417125dSmiod 			printf("WARNING: not updating battery clock\n");
9862417125dSmiod 		}
987df930be7Sderaadt 	}
9887d9ca166Sderaadt 	if_downall();
989df930be7Sderaadt 
990c98e8b29Sderaadt 	uvm_shutdown();
9919f43f03fSuebayasi 	splhigh();
99280ce5a38Smpi 	cold = 1;
993df930be7Sderaadt 
99421c23d01Smiod #if defined(MULTIPROCESSOR)
99521c23d01Smiod 	/*
99621c23d01Smiod 	 * Halt all other CPUs.
99721c23d01Smiod 	 */
99821c23d01Smiod 	wait_mask = (1UL << hwrpb->rpb_primary_cpu_id);
99921c23d01Smiod 	alpha_broadcast_ipi(ALPHA_IPI_HALT);
100021c23d01Smiod 
100121c23d01Smiod 	/* Ensure any CPUs paused by DDB resume execution so they can halt */
100221c23d01Smiod 	cpus_paused = 0;
100321c23d01Smiod 
100421c23d01Smiod 	for (i = 0; i < 10000; i++) {
100521c23d01Smiod 		alpha_mb();
100621c23d01Smiod 		if (cpus_running == wait_mask)
100721c23d01Smiod 			break;
100821c23d01Smiod 		delay(1000);
100921c23d01Smiod 	}
101021c23d01Smiod 	alpha_mb();
101121c23d01Smiod 	if (cpus_running != wait_mask)
101221c23d01Smiod 		printf("WARNING: Unable to halt secondary CPUs (0x%lx)\n",
101321c23d01Smiod 		    cpus_running);
101421c23d01Smiod #endif
101521c23d01Smiod 
1016b19c85f4Suebayasi 	if ((howto & RB_DUMP) != 0)
1017df930be7Sderaadt 		dumpsys();
1018df930be7Sderaadt 
101934fbf6deSderaadt haltsys:
102096f419e1Skettenis 	config_suspend_all(DVACT_POWERDOWN);
1021df930be7Sderaadt 
1022df930be7Sderaadt #ifdef BOOTKEY
1023b33b2f20Suebayasi 	printf("hit any key to %s...\n",
1024b33b2f20Suebayasi 	    (howto & RB_HALT) != 0 ? "halt" : "reboot");
1025aed035abSart 	cnpollc(1);	/* for proper keyboard command handling */
1026df930be7Sderaadt 	cngetc();
1027aed035abSart 	cnpollc(0);
1028df930be7Sderaadt 	printf("\n");
1029df930be7Sderaadt #endif
1030df930be7Sderaadt 
1031aed035abSart 	/* Finally, powerdown/halt/reboot the system. */
1032b33b2f20Suebayasi 	if ((howto & RB_POWERDOWN) != 0 &&
1033aed035abSart 	    platform.powerdown != NULL) {
1034aed035abSart 		(*platform.powerdown)();
1035aed035abSart 		printf("WARNING: powerdown failed!\n");
1036aed035abSart 	}
1037b33b2f20Suebayasi 	printf("%s\n\n",
1038b33b2f20Suebayasi 	    (howto & RB_HALT) != 0 ? "halted." : "rebooting...");
1039b33b2f20Suebayasi 	prom_halt((howto & RB_HALT) != 0);
1040de5ed823Stom 	for (;;)
1041de5ed823Stom 		continue;
1042df930be7Sderaadt 	/* NOTREACHED */
1043df930be7Sderaadt }
1044df930be7Sderaadt 
1045df930be7Sderaadt /*
1046df930be7Sderaadt  * These variables are needed by /sbin/savecore
1047df930be7Sderaadt  */
1048df930be7Sderaadt u_long	dumpmag = 0x8fca0101;	/* magic number */
1049df930be7Sderaadt int 	dumpsize = 0;		/* pages */
1050df930be7Sderaadt long	dumplo = 0; 		/* blocks */
1051df930be7Sderaadt 
1052df930be7Sderaadt /*
105350ce9ee0Sniklas  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
105450ce9ee0Sniklas  */
105550ce9ee0Sniklas int
105650ce9ee0Sniklas cpu_dumpsize()
105750ce9ee0Sniklas {
105850ce9ee0Sniklas 	int size;
105950ce9ee0Sniklas 
1060aed035abSart 	size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1061aed035abSart 	    ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
106250ce9ee0Sniklas 	if (roundup(size, dbtob(1)) != dbtob(1))
106350ce9ee0Sniklas 		return -1;
106450ce9ee0Sniklas 
106550ce9ee0Sniklas 	return (1);
106650ce9ee0Sniklas }
106750ce9ee0Sniklas 
106850ce9ee0Sniklas /*
1069aed035abSart  * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1070aed035abSart  */
1071aed035abSart u_long
1072aed035abSart cpu_dump_mempagecnt()
1073aed035abSart {
1074aed035abSart 	u_long i, n;
1075aed035abSart 
1076aed035abSart 	n = 0;
1077aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++)
1078aed035abSart 		n += atop(mem_clusters[i].size);
1079aed035abSart 	return (n);
1080aed035abSart }
1081aed035abSart 
1082aed035abSart /*
108350ce9ee0Sniklas  * cpu_dump: dump machine-dependent kernel core dump headers.
108450ce9ee0Sniklas  */
108550ce9ee0Sniklas int
108650ce9ee0Sniklas cpu_dump()
108750ce9ee0Sniklas {
10881abdbfdeSderaadt 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
1089aed035abSart 	char buf[dbtob(1)];
109050ce9ee0Sniklas 	kcore_seg_t *segp;
109150ce9ee0Sniklas 	cpu_kcore_hdr_t *cpuhdrp;
1092aed035abSart 	phys_ram_seg_t *memsegp;
1093aed035abSart 	int i;
109450ce9ee0Sniklas 
109550ce9ee0Sniklas 	dump = bdevsw[major(dumpdev)].d_dump;
109650ce9ee0Sniklas 
1097aed035abSart 	bzero(buf, sizeof buf);
109850ce9ee0Sniklas 	segp = (kcore_seg_t *)buf;
1099aed035abSart 	cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1100aed035abSart 	memsegp = (phys_ram_seg_t *)&buf[ALIGN(sizeof(*segp)) +
1101aed035abSart 	    ALIGN(sizeof(*cpuhdrp))];
110250ce9ee0Sniklas 
110350ce9ee0Sniklas 	/*
110450ce9ee0Sniklas 	 * Generate a segment header.
110550ce9ee0Sniklas 	 */
110650ce9ee0Sniklas 	CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
110750ce9ee0Sniklas 	segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
110850ce9ee0Sniklas 
110950ce9ee0Sniklas 	/*
1110aed035abSart 	 * Add the machine-dependent header info.
111150ce9ee0Sniklas 	 */
1112aed035abSart 	cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
111350ce9ee0Sniklas 	cpuhdrp->page_size = PAGE_SIZE;
1114aed035abSart 	cpuhdrp->nmemsegs = mem_cluster_cnt;
1115aed035abSart 
1116aed035abSart 	/*
1117aed035abSart 	 * Fill in the memory segment descriptors.
1118aed035abSart 	 */
1119aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++) {
1120aed035abSart 		memsegp[i].start = mem_clusters[i].start;
1121aed035abSart 		memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1122aed035abSart 	}
112350ce9ee0Sniklas 
112450ce9ee0Sniklas 	return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
112550ce9ee0Sniklas }
112650ce9ee0Sniklas 
112750ce9ee0Sniklas /*
1128aed035abSart  * This is called by main to set dumplo and dumpsize.
1129194dd68bSbrad  * Dumps always skip the first PAGE_SIZE of disk space
1130df930be7Sderaadt  * in case there might be a disk label stored there.
1131df930be7Sderaadt  * If there is extra space, put dump at the end to
1132df930be7Sderaadt  * reduce the chance that swapping trashes it.
1133df930be7Sderaadt  */
1134df930be7Sderaadt void
1135e17d3b39Sderaadt dumpconf(void)
1136df930be7Sderaadt {
113750ce9ee0Sniklas 	int nblks, dumpblks;	/* size of dump area */
1138df930be7Sderaadt 
1139e17d3b39Sderaadt 	if (dumpdev == NODEV ||
1140e17d3b39Sderaadt 	    (nblks = (bdevsw[major(dumpdev)].d_psize)(dumpdev)) == 0)
1141e17d3b39Sderaadt 		return;
1142df930be7Sderaadt 	if (nblks <= ctod(1))
1143e17d3b39Sderaadt 		return;
114450ce9ee0Sniklas 
114550ce9ee0Sniklas 	dumpblks = cpu_dumpsize();
114650ce9ee0Sniklas 	if (dumpblks < 0)
1147e17d3b39Sderaadt 		return;
1148aed035abSart 	dumpblks += ctod(cpu_dump_mempagecnt());
114950ce9ee0Sniklas 
115050ce9ee0Sniklas 	/* If dump won't fit (incl. room for possible label), punt. */
115150ce9ee0Sniklas 	if (dumpblks > (nblks - ctod(1)))
1152e17d3b39Sderaadt 		return;
115350ce9ee0Sniklas 
115450ce9ee0Sniklas 	/* Put dump at end of partition */
115550ce9ee0Sniklas 	dumplo = nblks - dumpblks;
115650ce9ee0Sniklas 
115750ce9ee0Sniklas 	/* dumpsize is in page units, and doesn't include headers. */
1158aed035abSart 	dumpsize = cpu_dump_mempagecnt();
1159df930be7Sderaadt }
1160df930be7Sderaadt 
1161df930be7Sderaadt /*
116250ce9ee0Sniklas  * Dump the kernel's image to the swap partition.
1163df930be7Sderaadt  */
1164194dd68bSbrad #define	BYTES_PER_DUMP	PAGE_SIZE
116550ce9ee0Sniklas 
1166df930be7Sderaadt void
1167df930be7Sderaadt dumpsys()
1168df930be7Sderaadt {
1169aed035abSart 	u_long totalbytesleft, bytes, i, n, memcl;
1170aed035abSart 	u_long maddr;
1171aed035abSart 	int psize;
11721abdbfdeSderaadt 	daddr_t blkno;
11731abdbfdeSderaadt 	int (*dump)(dev_t, daddr_t, caddr_t, size_t);
117450ce9ee0Sniklas 	int error;
1175067cbd75Sderaadt 	extern int msgbufmapped;
1176df930be7Sderaadt 
117750ce9ee0Sniklas 	/* Save registers. */
117850ce9ee0Sniklas 	savectx(&dumppcb);
117950ce9ee0Sniklas 
118050ce9ee0Sniklas 	msgbufmapped = 0;	/* don't record dump msgs in msgbuf */
1181df930be7Sderaadt 	if (dumpdev == NODEV)
1182df930be7Sderaadt 		return;
118350ce9ee0Sniklas 
118450ce9ee0Sniklas 	/*
118550ce9ee0Sniklas 	 * For dumps during autoconfiguration,
118650ce9ee0Sniklas 	 * if dump device has already configured...
118750ce9ee0Sniklas 	 */
1188df930be7Sderaadt 	if (dumpsize == 0)
118950ce9ee0Sniklas 		dumpconf();
119050ce9ee0Sniklas 	if (dumplo <= 0) {
1191aed035abSart 		printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
1192aed035abSart 		    minor(dumpdev));
1193df930be7Sderaadt 		return;
1194df930be7Sderaadt 	}
1195aed035abSart 	printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
1196aed035abSart 	    minor(dumpdev), dumplo);
1197df930be7Sderaadt 
119850ce9ee0Sniklas 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
1199df930be7Sderaadt 	printf("dump ");
120050ce9ee0Sniklas 	if (psize == -1) {
120150ce9ee0Sniklas 		printf("area unavailable\n");
120250ce9ee0Sniklas 		return;
120350ce9ee0Sniklas 	}
120450ce9ee0Sniklas 
120550ce9ee0Sniklas 	/* XXX should purge all outstanding keystrokes. */
120650ce9ee0Sniklas 
120750ce9ee0Sniklas 	if ((error = cpu_dump()) != 0)
120850ce9ee0Sniklas 		goto err;
120950ce9ee0Sniklas 
1210aed035abSart 	totalbytesleft = ptoa(cpu_dump_mempagecnt());
121150ce9ee0Sniklas 	blkno = dumplo + cpu_dumpsize();
121250ce9ee0Sniklas 	dump = bdevsw[major(dumpdev)].d_dump;
121350ce9ee0Sniklas 	error = 0;
1214aed035abSart 
1215aed035abSart 	for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1216aed035abSart 		maddr = mem_clusters[memcl].start;
1217aed035abSart 		bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1218aed035abSart 
1219aed035abSart 		for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
122050ce9ee0Sniklas 
122150ce9ee0Sniklas 			/* Print out how many MBs we to go. */
1222aed035abSart 			if ((totalbytesleft % (1024*1024)) == 0)
1223aed035abSart 				printf("%ld ", totalbytesleft / (1024 * 1024));
122450ce9ee0Sniklas 
122550ce9ee0Sniklas 			/* Limit size for next transfer. */
1226aed035abSart 			n = bytes - i;
122750ce9ee0Sniklas 			if (n > BYTES_PER_DUMP)
122850ce9ee0Sniklas 				n =  BYTES_PER_DUMP;
122950ce9ee0Sniklas 
123050ce9ee0Sniklas 			error = (*dump)(dumpdev, blkno,
123150ce9ee0Sniklas 			    (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
123250ce9ee0Sniklas 			if (error)
1233aed035abSart 				goto err;
123450ce9ee0Sniklas 			maddr += n;
123550ce9ee0Sniklas 			blkno += btodb(n);			/* XXX? */
123650ce9ee0Sniklas 
123750ce9ee0Sniklas 			/* XXX should look for keystrokes, to cancel. */
123850ce9ee0Sniklas 		}
1239aed035abSart 	}
124050ce9ee0Sniklas 
124150ce9ee0Sniklas err:
124250ce9ee0Sniklas 	switch (error) {
1243a37778bcSderaadt #ifdef DEBUG
1244df930be7Sderaadt 	case ENXIO:
1245df930be7Sderaadt 		printf("device bad\n");
1246df930be7Sderaadt 		break;
1247df930be7Sderaadt 
1248df930be7Sderaadt 	case EFAULT:
1249df930be7Sderaadt 		printf("device not ready\n");
1250df930be7Sderaadt 		break;
1251df930be7Sderaadt 
1252df930be7Sderaadt 	case EINVAL:
1253df930be7Sderaadt 		printf("area improper\n");
1254df930be7Sderaadt 		break;
1255df930be7Sderaadt 
1256df930be7Sderaadt 	case EIO:
1257df930be7Sderaadt 		printf("i/o error\n");
1258df930be7Sderaadt 		break;
1259df930be7Sderaadt 
1260df930be7Sderaadt 	case EINTR:
1261df930be7Sderaadt 		printf("aborted from console\n");
1262df930be7Sderaadt 		break;
1263a37778bcSderaadt #endif /* DEBUG */
126450ce9ee0Sniklas 	case 0:
1265df930be7Sderaadt 		printf("succeeded\n");
1266df930be7Sderaadt 		break;
126750ce9ee0Sniklas 
126850ce9ee0Sniklas 	default:
126950ce9ee0Sniklas 		printf("error %d\n", error);
127050ce9ee0Sniklas 		break;
1271df930be7Sderaadt 	}
1272df930be7Sderaadt 	printf("\n\n");
1273df930be7Sderaadt 	delay(1000);
1274df930be7Sderaadt }
1275df930be7Sderaadt 
1276df930be7Sderaadt void
1277df930be7Sderaadt frametoreg(framep, regp)
1278df930be7Sderaadt 	struct trapframe *framep;
1279df930be7Sderaadt 	struct reg *regp;
1280df930be7Sderaadt {
1281df930be7Sderaadt 
1282df930be7Sderaadt 	regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1283df930be7Sderaadt 	regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1284df930be7Sderaadt 	regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1285df930be7Sderaadt 	regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1286df930be7Sderaadt 	regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1287df930be7Sderaadt 	regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1288df930be7Sderaadt 	regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1289df930be7Sderaadt 	regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1290df930be7Sderaadt 	regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1291df930be7Sderaadt 	regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1292df930be7Sderaadt 	regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1293df930be7Sderaadt 	regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1294df930be7Sderaadt 	regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1295df930be7Sderaadt 	regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1296df930be7Sderaadt 	regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1297df930be7Sderaadt 	regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
129850ce9ee0Sniklas 	regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
129950ce9ee0Sniklas 	regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
130050ce9ee0Sniklas 	regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1301df930be7Sderaadt 	regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1302df930be7Sderaadt 	regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1303df930be7Sderaadt 	regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1304df930be7Sderaadt 	regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1305df930be7Sderaadt 	regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1306df930be7Sderaadt 	regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1307df930be7Sderaadt 	regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1308df930be7Sderaadt 	regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1309df930be7Sderaadt 	regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1310df930be7Sderaadt 	regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
131150ce9ee0Sniklas 	regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
131250ce9ee0Sniklas 	/* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1313df930be7Sderaadt 	regp->r_regs[R_ZERO] = 0;
1314df930be7Sderaadt }
1315df930be7Sderaadt 
1316df930be7Sderaadt void
1317df930be7Sderaadt regtoframe(regp, framep)
1318df930be7Sderaadt 	struct reg *regp;
1319df930be7Sderaadt 	struct trapframe *framep;
1320df930be7Sderaadt {
1321df930be7Sderaadt 
1322df930be7Sderaadt 	framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1323df930be7Sderaadt 	framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1324df930be7Sderaadt 	framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1325df930be7Sderaadt 	framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1326df930be7Sderaadt 	framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1327df930be7Sderaadt 	framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1328df930be7Sderaadt 	framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1329df930be7Sderaadt 	framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1330df930be7Sderaadt 	framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1331df930be7Sderaadt 	framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1332df930be7Sderaadt 	framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1333df930be7Sderaadt 	framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1334df930be7Sderaadt 	framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1335df930be7Sderaadt 	framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1336df930be7Sderaadt 	framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1337df930be7Sderaadt 	framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
133850ce9ee0Sniklas 	framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
133950ce9ee0Sniklas 	framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
134050ce9ee0Sniklas 	framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1341df930be7Sderaadt 	framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1342df930be7Sderaadt 	framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1343df930be7Sderaadt 	framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1344df930be7Sderaadt 	framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1345df930be7Sderaadt 	framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1346df930be7Sderaadt 	framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1347df930be7Sderaadt 	framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1348df930be7Sderaadt 	framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1349df930be7Sderaadt 	framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1350df930be7Sderaadt 	framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
135150ce9ee0Sniklas 	framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
135250ce9ee0Sniklas 	/* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1353df930be7Sderaadt 	/* ??? = regp->r_regs[R_ZERO]; */
1354df930be7Sderaadt }
1355df930be7Sderaadt 
1356df930be7Sderaadt void
1357df930be7Sderaadt printregs(regp)
1358df930be7Sderaadt 	struct reg *regp;
1359df930be7Sderaadt {
1360df930be7Sderaadt 	int i;
1361df930be7Sderaadt 
1362df930be7Sderaadt 	for (i = 0; i < 32; i++)
1363df930be7Sderaadt 		printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1364df930be7Sderaadt 		   i & 1 ? "\n" : "\t");
1365df930be7Sderaadt }
1366df930be7Sderaadt 
1367df930be7Sderaadt void
1368df930be7Sderaadt regdump(framep)
1369df930be7Sderaadt 	struct trapframe *framep;
1370df930be7Sderaadt {
1371df930be7Sderaadt 	struct reg reg;
1372df930be7Sderaadt 
1373df930be7Sderaadt 	frametoreg(framep, &reg);
137450ce9ee0Sniklas 	reg.r_regs[R_SP] = alpha_pal_rdusp();
137550ce9ee0Sniklas 
1376df930be7Sderaadt 	printf("REGISTERS:\n");
1377df930be7Sderaadt 	printregs(&reg);
1378df930be7Sderaadt }
1379df930be7Sderaadt 
1380df930be7Sderaadt #ifdef DEBUG
1381df930be7Sderaadt int sigdebug = 0;
138272830333Sokan pid_t sigpid = 0;
1383df930be7Sderaadt #define	SDB_FOLLOW	0x01
1384df930be7Sderaadt #define	SDB_KSTACK	0x02
1385df930be7Sderaadt #endif
1386df930be7Sderaadt 
1387df930be7Sderaadt /*
1388df930be7Sderaadt  * Send an interrupt to process.
1389df930be7Sderaadt  */
1390df930be7Sderaadt void
1391e99873aaSderaadt sendsig(sig_t catcher, int sig, int mask, u_long code, int type,
1392e99873aaSderaadt     union sigval val)
1393df930be7Sderaadt {
1394df930be7Sderaadt 	struct proc *p = curproc;
1395e99873aaSderaadt 	struct sigcontext ksc, *scp;
1396e7e08221Smiod 	struct fpreg *fpregs = (struct fpreg *)&ksc.sc_fpregs;
1397df930be7Sderaadt 	struct trapframe *frame;
1398533be81aSguenther 	struct sigacts *psp = p->p_p->ps_sigacts;
13991eaa59e7Sguenther 	unsigned long oldsp;
14001eaa59e7Sguenther 	int fsize, rndfsize, kscsize;
14012bf9c155Sderaadt 	siginfo_t *sip, ksi;
1402df930be7Sderaadt 
14031eaa59e7Sguenther 	oldsp = alpha_pal_rdusp();
1404df930be7Sderaadt 	frame = p->p_md.md_tf;
1405df930be7Sderaadt 	fsize = sizeof ksc;
1406df930be7Sderaadt 	rndfsize = ((fsize + 15) / 16) * 16;
14072bf9c155Sderaadt 	kscsize = rndfsize;
14082bf9c155Sderaadt 	if (psp->ps_siginfo & sigmask(sig)) {
14092bf9c155Sderaadt 		fsize += sizeof ksi;
14102bf9c155Sderaadt 		rndfsize = ((fsize + 15) / 16) * 16;
14112bf9c155Sderaadt 	}
141274652a67Sniklas 
1413df930be7Sderaadt 	/*
14146e870393Smiod 	 * Allocate space for the signal handler context.
1415df930be7Sderaadt 	 */
14161eaa59e7Sguenther 	if ((p->p_sigstk.ss_flags & SS_DISABLE) == 0 &&
14171eaa59e7Sguenther 	    !sigonstack(oldsp) && (psp->ps_sigonstack & sigmask(sig)))
14182725daddSguenther 		scp = (struct sigcontext *)(p->p_sigstk.ss_sp +
14192725daddSguenther 		    p->p_sigstk.ss_size - rndfsize);
14201eaa59e7Sguenther 	else
14211eaa59e7Sguenther 		scp = (struct sigcontext *)(oldsp - rndfsize);
1422df930be7Sderaadt 
1423df930be7Sderaadt 	/*
1424df930be7Sderaadt 	 * Build the signal context to be used by sigreturn.
1425df930be7Sderaadt 	 */
14261eaa59e7Sguenther 	bzero(&ksc, sizeof(ksc));
1427df930be7Sderaadt 	ksc.sc_mask = mask;
142850ce9ee0Sniklas 	ksc.sc_pc = frame->tf_regs[FRAME_PC];
142950ce9ee0Sniklas 	ksc.sc_ps = frame->tf_regs[FRAME_PS];
1430df930be7Sderaadt 
1431df930be7Sderaadt 	/* copy the registers. */
1432df930be7Sderaadt 	frametoreg(frame, (struct reg *)ksc.sc_regs);
14331eaa59e7Sguenther 	ksc.sc_regs[R_SP] = oldsp;
1434df930be7Sderaadt 
1435df930be7Sderaadt 	/* save the floating-point state, if necessary, then copy it. */
1436433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1437433075b6Spvalchev 		fpusave_proc(p, 1);
1438df930be7Sderaadt 	ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
1439e7e08221Smiod 	memcpy(/*ksc.sc_*/fpregs, &p->p_addr->u_pcb.pcb_fp,
1440df930be7Sderaadt 	    sizeof(struct fpreg));
1441433075b6Spvalchev #ifndef NO_IEEE
1442433075b6Spvalchev 	ksc.sc_fp_control = alpha_read_fp_c(p);
1443433075b6Spvalchev #else
1444433075b6Spvalchev 	ksc.sc_fp_control = 0;
1445433075b6Spvalchev #endif
1446433075b6Spvalchev 	memset(ksc.sc_reserved, 0, sizeof ksc.sc_reserved);	/* XXX */
1447433075b6Spvalchev 	memset(ksc.sc_xxx, 0, sizeof ksc.sc_xxx);		/* XXX */
1448df930be7Sderaadt 
14492bf9c155Sderaadt 	if (psp->ps_siginfo & sigmask(sig)) {
14502bf9c155Sderaadt 		initsiginfo(&ksi, sig, code, type, val);
14512bf9c155Sderaadt 		sip = (void *)scp + kscsize;
1452679ebc41Smiod 		if (copyout((caddr_t)&ksi, (caddr_t)sip, fsize - kscsize) != 0)
1453679ebc41Smiod 			goto trash;
1454aa540fb8Sart 	} else
1455aa540fb8Sart 		sip = NULL;
14562bf9c155Sderaadt 
14577730d1d9Sderaadt 	ksc.sc_cookie = (long)scp ^ p->p_p->ps_sigcookie;
1458679ebc41Smiod 	if (copyout((caddr_t)&ksc, (caddr_t)scp, kscsize) != 0) {
1459679ebc41Smiod trash:
1460679ebc41Smiod 		/*
1461679ebc41Smiod 		 * Process has trashed its stack; give it an illegal
1462679ebc41Smiod 		 * instruction to halt it in its tracks.
1463679ebc41Smiod 		 */
146486fd84b3Smiod 		sigexit(p, SIGILL);
146586fd84b3Smiod 		/* NOTREACHED */
1466679ebc41Smiod 	}
1467df930be7Sderaadt 
1468df930be7Sderaadt 	/*
1469df930be7Sderaadt 	 * Set up the registers to return to sigcode.
1470df930be7Sderaadt 	 */
14718f76f5adSguenther 	frame->tf_regs[FRAME_PC] = p->p_p->ps_sigcode;
147250ce9ee0Sniklas 	frame->tf_regs[FRAME_A0] = sig;
1473aa540fb8Sart 	frame->tf_regs[FRAME_A1] = (u_int64_t)sip;
147450ce9ee0Sniklas 	frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
1475df930be7Sderaadt 	frame->tf_regs[FRAME_T12] = (u_int64_t)catcher;		/* t12 is pv */
147650ce9ee0Sniklas 	alpha_pal_wrusp((unsigned long)scp);
1477df930be7Sderaadt }
1478df930be7Sderaadt 
1479df930be7Sderaadt /*
1480df930be7Sderaadt  * System call to cleanup state after a signal
1481df930be7Sderaadt  * has been taken.  Reset signal mask and
1482df930be7Sderaadt  * stack state from context left by sendsig (above).
1483df930be7Sderaadt  * Return to previous pc and psl as specified by
1484df930be7Sderaadt  * context left by sendsig. Check carefully to
1485df930be7Sderaadt  * make sure that the user has not modified the
1486125cd19fSderaadt  * psl to gain improper privileges or to cause
1487df930be7Sderaadt  * a machine fault.
1488df930be7Sderaadt  */
1489df930be7Sderaadt /* ARGSUSED */
1490df930be7Sderaadt int
1491e99873aaSderaadt sys_sigreturn(struct proc *p, void *v, register_t *retval)
1492df930be7Sderaadt {
1493df930be7Sderaadt 	struct sys_sigreturn_args /* {
1494df930be7Sderaadt 		syscallarg(struct sigcontext *) sigcntxp;
1495df930be7Sderaadt 	} */ *uap = v;
1496e99873aaSderaadt 	struct sigcontext ksc, *scp = SCARG(uap, sigcntxp);
1497e7e08221Smiod 	struct fpreg *fpregs = (struct fpreg *)&ksc.sc_fpregs;
1498aa540fb8Sart 	int error;
1499df930be7Sderaadt 
15007730d1d9Sderaadt 	if (PROC_PC(p) != p->p_p->ps_sigcoderet) {
15017730d1d9Sderaadt 		sigexit(p, SIGILL);
15027730d1d9Sderaadt 		return (EPERM);
15037730d1d9Sderaadt 	}
15047730d1d9Sderaadt 
15057730d1d9Sderaadt 	if ((error = copyin(scp, &ksc, sizeof(ksc))) != 0)
1506aa540fb8Sart 		return (error);
1507df930be7Sderaadt 
15087730d1d9Sderaadt 	if (ksc.sc_cookie != ((long)scp ^ p->p_p->ps_sigcookie)) {
15097730d1d9Sderaadt 		sigexit(p, SIGILL);
15107730d1d9Sderaadt 		return (EFAULT);
15117730d1d9Sderaadt 	}
15127730d1d9Sderaadt 
15134a064f11Sderaadt 	/* Prevent reuse of the sigcontext cookie */
15144a064f11Sderaadt 	ksc.sc_cookie = 0;
15154a064f11Sderaadt 	(void)copyout(&ksc.sc_cookie, (caddr_t)scp +
1516e99873aaSderaadt 	    offsetof(struct sigcontext, sc_cookie), sizeof (ksc.sc_cookie));
15174a064f11Sderaadt 
1518df930be7Sderaadt 	/*
1519df930be7Sderaadt 	 * Restore the user-supplied information
1520df930be7Sderaadt 	 */
1521df930be7Sderaadt 	p->p_sigmask = ksc.sc_mask &~ sigcantmask;
1522df930be7Sderaadt 
152350ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
152450ce9ee0Sniklas 	p->p_md.md_tf->tf_regs[FRAME_PS] =
152550ce9ee0Sniklas 	    (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
1526df930be7Sderaadt 
1527df930be7Sderaadt 	regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
152850ce9ee0Sniklas 	alpha_pal_wrusp(ksc.sc_regs[R_SP]);
1529df930be7Sderaadt 
1530df930be7Sderaadt 	/* XXX ksc.sc_ownedfp ? */
1531433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1532433075b6Spvalchev 		fpusave_proc(p, 0);
1533e7e08221Smiod 	memcpy(&p->p_addr->u_pcb.pcb_fp, /*ksc.sc_*/fpregs,
1534df930be7Sderaadt 	    sizeof(struct fpreg));
1535433075b6Spvalchev #ifndef NO_IEEE
1536433075b6Spvalchev 	p->p_addr->u_pcb.pcb_fp.fpr_cr = ksc.sc_fpcr;
1537433075b6Spvalchev 	p->p_md.md_flags = ksc.sc_fp_control & MDP_FP_C;
1538433075b6Spvalchev #endif
1539df930be7Sderaadt 	return (EJUSTRETURN);
1540df930be7Sderaadt }
1541df930be7Sderaadt 
1542df930be7Sderaadt /*
1543df930be7Sderaadt  * machine dependent system variables.
1544df930be7Sderaadt  */
154550ce9ee0Sniklas int
154669e3e61aSfcambus cpu_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
154769e3e61aSfcambus     size_t newlen, struct proc *p)
1548df930be7Sderaadt {
1549df930be7Sderaadt 	dev_t consdev;
1550a072164aSmiod #if NIOASIC > 0
1551a072164aSmiod 	int oldval, ret;
1552a072164aSmiod #endif
1553df930be7Sderaadt 
155445e5a1a0Sart 	if (name[0] != CPU_CHIPSET && namelen != 1)
1555df930be7Sderaadt 		return (ENOTDIR);		/* overloaded */
1556df930be7Sderaadt 
1557df930be7Sderaadt 	switch (name[0]) {
1558df930be7Sderaadt 	case CPU_CONSDEV:
1559df930be7Sderaadt 		if (cn_tab != NULL)
1560df930be7Sderaadt 			consdev = cn_tab->cn_dev;
1561df930be7Sderaadt 		else
1562df930be7Sderaadt 			consdev = NODEV;
1563df930be7Sderaadt 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
1564df930be7Sderaadt 			sizeof consdev));
1565417eba8cSderaadt 
1566a37778bcSderaadt #ifndef SMALL_KERNEL
156750ce9ee0Sniklas 	case CPU_UNALIGNED_PRINT:
156850ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
156950ce9ee0Sniklas 		    &alpha_unaligned_print));
157050ce9ee0Sniklas 
157150ce9ee0Sniklas 	case CPU_UNALIGNED_FIX:
157250ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
157350ce9ee0Sniklas 		    &alpha_unaligned_fix));
157450ce9ee0Sniklas 
157550ce9ee0Sniklas 	case CPU_UNALIGNED_SIGBUS:
157650ce9ee0Sniklas 		return (sysctl_int(oldp, oldlenp, newp, newlen,
157750ce9ee0Sniklas 		    &alpha_unaligned_sigbus));
157850ce9ee0Sniklas 
15793a630e3fSniklas 	case CPU_BOOTED_KERNEL:
1580aed035abSart 		return (sysctl_rdstring(oldp, oldlenp, newp,
1581aed035abSart 		    bootinfo.booted_kernel));
15823a630e3fSniklas 
158345e5a1a0Sart 	case CPU_CHIPSET:
158445e5a1a0Sart 		return (alpha_sysctl_chipset(name + 1, namelen - 1, oldp,
158545e5a1a0Sart 		    oldlenp));
1586a37778bcSderaadt #endif /* SMALL_KERNEL */
1587433075b6Spvalchev 
1588433075b6Spvalchev #ifndef NO_IEEE
1589433075b6Spvalchev 	case CPU_FP_SYNC_COMPLETE:
1590433075b6Spvalchev 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1591433075b6Spvalchev 		    &alpha_fp_sync_complete));
1592433075b6Spvalchev #endif
159327626149Smatthieu 	case CPU_ALLOWAPERTURE:
159427626149Smatthieu #ifdef APERTURE
159527626149Smatthieu 		if (securelevel > 0)
15961546ceefSderaadt 			return (sysctl_int_lower(oldp, oldlenp, newp, newlen,
15971546ceefSderaadt 			    &allowaperture));
159827626149Smatthieu                 else
159927626149Smatthieu                         return (sysctl_int(oldp, oldlenp, newp, newlen,
160027626149Smatthieu                             &allowaperture));
160127626149Smatthieu #else
160227626149Smatthieu 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
160327626149Smatthieu #endif
1604a072164aSmiod #if NIOASIC > 0
1605a072164aSmiod 	case CPU_LED_BLINK:
1606a072164aSmiod 		oldval = alpha_led_blink;
1607a072164aSmiod 		ret = sysctl_int(oldp, oldlenp, newp, newlen, &alpha_led_blink);
1608a072164aSmiod 		if (oldval != alpha_led_blink)
1609a072164aSmiod 			ioasic_led_blink(NULL);
1610a072164aSmiod 		return (ret);
1611a072164aSmiod #endif
1612df930be7Sderaadt 	default:
1613df930be7Sderaadt 		return (EOPNOTSUPP);
1614df930be7Sderaadt 	}
1615df930be7Sderaadt 	/* NOTREACHED */
1616df930be7Sderaadt }
1617df930be7Sderaadt 
1618df930be7Sderaadt /*
1619df930be7Sderaadt  * Set registers on exec.
1620df930be7Sderaadt  */
1621df930be7Sderaadt void
1622df930be7Sderaadt setregs(p, pack, stack, retval)
1623df930be7Sderaadt 	register struct proc *p;
1624df930be7Sderaadt 	struct exec_package *pack;
1625df930be7Sderaadt 	u_long stack;
1626df930be7Sderaadt 	register_t *retval;
1627df930be7Sderaadt {
1628df930be7Sderaadt 	struct trapframe *tfp = p->p_md.md_tf;
16293a630e3fSniklas #ifdef DEBUG
16303a630e3fSniklas 	int i;
16313a630e3fSniklas #endif
1632df930be7Sderaadt 
1633df930be7Sderaadt #ifdef DEBUG
163450ce9ee0Sniklas 	/*
163550ce9ee0Sniklas 	 * Crash and dump, if the user requested it.
163650ce9ee0Sniklas 	 */
163750ce9ee0Sniklas 	if (boothowto & RB_DUMP)
163850ce9ee0Sniklas 		panic("crash requested by boot flags");
163950ce9ee0Sniklas #endif
164050ce9ee0Sniklas 
164150ce9ee0Sniklas #ifdef DEBUG
164250ce9ee0Sniklas 	for (i = 0; i < FRAME_SIZE; i++)
1643df930be7Sderaadt 		tfp->tf_regs[i] = 0xbabefacedeadbeef;
16446334622bSguenther 	tfp->tf_regs[FRAME_A1] = 0;
1645df930be7Sderaadt #else
164650ce9ee0Sniklas 	bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1647df930be7Sderaadt #endif
1648df930be7Sderaadt 	bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
164950ce9ee0Sniklas 	alpha_pal_wrusp(stack);
165050ce9ee0Sniklas 	tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
165150ce9ee0Sniklas 	tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1652df930be7Sderaadt 
165350ce9ee0Sniklas 	tfp->tf_regs[FRAME_A0] = stack;
165450ce9ee0Sniklas 	/* a1 and a2 already zeroed */
165550ce9ee0Sniklas 	tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC];	/* a.k.a. PV */
165650ce9ee0Sniklas 
165750ce9ee0Sniklas 	p->p_md.md_flags &= ~MDP_FPUSED;
1658433075b6Spvalchev #ifndef NO_IEEE
1659433075b6Spvalchev 	if (__predict_true((p->p_md.md_flags & IEEE_INHERIT) == 0)) {
1660433075b6Spvalchev 		p->p_md.md_flags &= ~MDP_FP_C;
1661433075b6Spvalchev 		p->p_addr->u_pcb.pcb_fp.fpr_cr = FPCR_DYN(FP_RN);
1662433075b6Spvalchev 	}
1663433075b6Spvalchev #endif
1664433075b6Spvalchev 	if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1665433075b6Spvalchev 		fpusave_proc(p, 0);
1666ee2d823aSmiod 
1667ee2d823aSmiod 	retval[1] = 0;
1668433075b6Spvalchev }
1669df930be7Sderaadt 
1670433075b6Spvalchev /*
1671433075b6Spvalchev  * Release the FPU.
1672433075b6Spvalchev  */
1673433075b6Spvalchev void
1674433075b6Spvalchev fpusave_cpu(struct cpu_info *ci, int save)
1675433075b6Spvalchev {
1676433075b6Spvalchev 	struct proc *p;
167721c23d01Smiod #if defined(MULTIPROCESSOR)
167821c23d01Smiod 	int s;
167921c23d01Smiod #endif
1680433075b6Spvalchev 
1681433075b6Spvalchev 	KDASSERT(ci == curcpu());
1682433075b6Spvalchev 
1683433075b6Spvalchev #if defined(MULTIPROCESSOR)
168421c23d01Smiod 	/* Need to block IPIs */
16852d7472daSmiod 	s = splipi();
1686433075b6Spvalchev 	atomic_setbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
1687433075b6Spvalchev #endif
1688433075b6Spvalchev 
1689433075b6Spvalchev 	p = ci->ci_fpcurproc;
1690433075b6Spvalchev 	if (p == NULL)
1691433075b6Spvalchev 		goto out;
1692433075b6Spvalchev 
1693433075b6Spvalchev 	if (save) {
1694433075b6Spvalchev 		alpha_pal_wrfen(1);
1695433075b6Spvalchev 		savefpstate(&p->p_addr->u_pcb.pcb_fp);
1696433075b6Spvalchev 	}
1697433075b6Spvalchev 
1698433075b6Spvalchev 	alpha_pal_wrfen(0);
1699433075b6Spvalchev 
1700433075b6Spvalchev 	p->p_addr->u_pcb.pcb_fpcpu = NULL;
1701433075b6Spvalchev 	ci->ci_fpcurproc = NULL;
1702433075b6Spvalchev 
1703433075b6Spvalchev out:
1704433075b6Spvalchev #if defined(MULTIPROCESSOR)
1705433075b6Spvalchev 	atomic_clearbits_ulong(&ci->ci_flags, CPUF_FPUSAVE);
17064fa86cc0Smiod 	alpha_pal_swpipl(s);
1707433075b6Spvalchev #endif
1708433075b6Spvalchev 	return;
1709433075b6Spvalchev }
1710433075b6Spvalchev 
1711433075b6Spvalchev /*
1712433075b6Spvalchev  * Synchronize FP state for this process.
1713433075b6Spvalchev  */
1714433075b6Spvalchev void
1715433075b6Spvalchev fpusave_proc(struct proc *p, int save)
1716433075b6Spvalchev {
1717433075b6Spvalchev 	struct cpu_info *ci = curcpu();
1718433075b6Spvalchev 	struct cpu_info *oci;
1719433075b6Spvalchev #if defined(MULTIPROCESSOR)
1720433075b6Spvalchev 	u_long ipi = save ? ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU;
17212d7472daSmiod 	int s;
1722433075b6Spvalchev #endif
1723433075b6Spvalchev 
1724433075b6Spvalchev 	KDASSERT(p->p_addr != NULL);
1725433075b6Spvalchev 
17262d7472daSmiod 	for (;;) {
172721c23d01Smiod #if defined(MULTIPROCESSOR)
172821c23d01Smiod 		/* Need to block IPIs */
17292d7472daSmiod 		s = splipi();
173021c23d01Smiod #endif
173121c23d01Smiod 
1732433075b6Spvalchev 		oci = p->p_addr->u_pcb.pcb_fpcpu;
1733433075b6Spvalchev 		if (oci == NULL) {
173421c23d01Smiod #if defined(MULTIPROCESSOR)
17354fa86cc0Smiod 			alpha_pal_swpipl(s);
173621c23d01Smiod #endif
1737433075b6Spvalchev 			return;
1738433075b6Spvalchev 		}
1739433075b6Spvalchev 
1740433075b6Spvalchev #if defined(MULTIPROCESSOR)
1741433075b6Spvalchev 		if (oci == ci) {
1742433075b6Spvalchev 			KASSERT(ci->ci_fpcurproc == p);
17434fa86cc0Smiod 			alpha_pal_swpipl(s);
1744433075b6Spvalchev 			fpusave_cpu(ci, save);
1745433075b6Spvalchev 			return;
1746433075b6Spvalchev 		}
1747433075b6Spvalchev 
17482d7472daSmiod 		/*
17492d7472daSmiod 		 * The other cpu may still be running and could have
17502d7472daSmiod 		 * discarded the fpu context on its own.
17512d7472daSmiod 		 */
175289043825Smiod 		if (oci->ci_fpcurproc != p) {
175389043825Smiod 			alpha_pal_swpipl(s);
17542d7472daSmiod 			continue;
175589043825Smiod 		}
17562d7472daSmiod 
1757433075b6Spvalchev 		alpha_send_ipi(oci->ci_cpuid, ipi);
17584fa86cc0Smiod 		alpha_pal_swpipl(s);
1759433075b6Spvalchev 
17602d7472daSmiod 		while (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1761*275e6a2fSmpi 			CPU_BUSY_CYCLE();
1762433075b6Spvalchev #else
1763433075b6Spvalchev 		KASSERT(ci->ci_fpcurproc == p);
1764433075b6Spvalchev 		fpusave_cpu(ci, save);
1765433075b6Spvalchev #endif /* MULTIPROCESSOR */
17662d7472daSmiod 
17672d7472daSmiod 		break;
17682d7472daSmiod 	}
1769df930be7Sderaadt }
1770df930be7Sderaadt 
1771df930be7Sderaadt int
1772df930be7Sderaadt spl0()
1773df930be7Sderaadt {
1774df930be7Sderaadt 
1775aed035abSart 	if (ssir) {
1776aed035abSart 		(void) alpha_pal_swpipl(ALPHA_PSL_IPL_SOFT);
17772a2685f2Sart 		softintr_dispatch();
1778aed035abSart 	}
1779df930be7Sderaadt 
178050ce9ee0Sniklas 	return (alpha_pal_swpipl(ALPHA_PSL_IPL_0));
1781df930be7Sderaadt }
1782df930be7Sderaadt 
1783df930be7Sderaadt /*
1784417eba8cSderaadt  * Wait "n" microseconds.
1785417eba8cSderaadt  */
178650ce9ee0Sniklas void
1787417eba8cSderaadt delay(n)
178850ce9ee0Sniklas 	unsigned long n;
1789417eba8cSderaadt {
17905d097e9eSmiod 	unsigned long pcc0, pcc1, curcycle, cycles, usec;
17915d097e9eSmiod 
17925d097e9eSmiod 	if (n == 0)
17935d097e9eSmiod 		return;
17945d097e9eSmiod 
17955d097e9eSmiod 	pcc0 = alpha_rpcc() & 0xffffffffUL;
17965d097e9eSmiod 	cycles = 0;
17975d097e9eSmiod 	usec = 0;
17985d097e9eSmiod 
17995d097e9eSmiod 	while (usec <= n) {
18005d097e9eSmiod 		/*
18015d097e9eSmiod 		 * Get the next CPU cycle count - assumes that we can not
18025d097e9eSmiod 		 * have had more than one 32 bit overflow.
18035d097e9eSmiod 		 */
18045d097e9eSmiod 		pcc1 = alpha_rpcc() & 0xffffffffUL;
18055d097e9eSmiod 		if (pcc1 < pcc0)
18065d097e9eSmiod 			curcycle = (pcc1 + 0x100000000UL) - pcc0;
18075d097e9eSmiod 		else
18085d097e9eSmiod 			curcycle = pcc1 - pcc0;
1809417eba8cSderaadt 
1810aed035abSart 		/*
18115d097e9eSmiod 		 * We now have the number of processor cycles since we
18125d097e9eSmiod 		 * last checked. Add the current cycle count to the
18135d097e9eSmiod 		 * running total. If it's over cycles_per_usec, increment
18145d097e9eSmiod 		 * the usec counter.
1815aed035abSart 		 */
18165d097e9eSmiod 		cycles += curcycle;
181721c23d01Smiod 		while (cycles >= cycles_per_usec) {
18185d097e9eSmiod 			usec++;
18195d097e9eSmiod 			cycles -= cycles_per_usec;
18205d097e9eSmiod 		}
18215d097e9eSmiod 		pcc0 = pcc1;
18225d097e9eSmiod 	}
1823417eba8cSderaadt }
1824417eba8cSderaadt 
1825aed035abSart int
1826aed035abSart alpha_pa_access(pa)
1827aed035abSart 	u_long pa;
1828aed035abSart {
1829aed035abSart 	int i;
1830aed035abSart 
1831aed035abSart 	for (i = 0; i < mem_cluster_cnt; i++) {
1832aed035abSart 		if (pa < mem_clusters[i].start)
1833aed035abSart 			continue;
1834aed035abSart 		if ((pa - mem_clusters[i].start) >=
1835aed035abSart 		    (mem_clusters[i].size & ~PAGE_MASK))
1836aed035abSart 			continue;
1837aed035abSart 		return (mem_clusters[i].size & PAGE_MASK);	/* prot */
1838aed035abSart 	}
1839aed035abSart 
1840aed035abSart 	/*
1841aed035abSart 	 * Address is not a memory address.  If we're secure, disallow
1842aed035abSart 	 * access.  Otherwise, grant read/write.
1843aed035abSart 	 */
1844aed035abSart 	if (securelevel > 0)
18451e8cdc2eSderaadt 		return (PROT_NONE);
1846aed035abSart 	else
18471e8cdc2eSderaadt 		return (PROT_READ | PROT_WRITE);
1848aed035abSart }
1849aed035abSart 
1850e464495eSniklas /* XXX XXX BEGIN XXX XXX */
1851aed035abSart paddr_t alpha_XXX_dmamap_or;					/* XXX */
1852e464495eSniklas 								/* XXX */
1853aed035abSart paddr_t								/* XXX */
1854e464495eSniklas alpha_XXX_dmamap(v)						/* XXX */
1855aed035abSart 	vaddr_t v;						/* XXX */
1856e464495eSniklas {								/* XXX */
1857e464495eSniklas 								/* XXX */
1858e464495eSniklas 	return (vtophys(v) | alpha_XXX_dmamap_or);		/* XXX */
1859e464495eSniklas }								/* XXX */
1860e464495eSniklas /* XXX XXX END XXX XXX */
1861