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