xref: /netbsd-src/sys/arch/sun2/sun2/machdep.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: machdep.c,v 1.65 2010/02/08 19:02:32 joerg Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the Systems Programming Group of the University of Utah Computer
9  * Science Department.
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. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	from: Utah Hdr: machdep.c 1.74 92/12/20
36  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
37  */
38 
39 /*
40  * Copyright (c) 2001 Matthew Fredette.
41  * Copyright (c) 1994, 1995 Gordon W. Ross
42  * Copyright (c) 1993 Adam Glass
43  * Copyright (c) 1988 University of Utah.
44  *
45  * This code is derived from software contributed to Berkeley by
46  * the Systems Programming Group of the University of Utah Computer
47  * Science Department.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  *    notice, this list of conditions and the following disclaimer in the
56  *    documentation and/or other materials provided with the distribution.
57  * 3. All advertising materials mentioning features or use of this software
58  *    must display the following acknowledgement:
59  *	This product includes software developed by the University of
60  *	California, Berkeley and its contributors.
61  * 4. Neither the name of the University nor the names of its contributors
62  *    may be used to endorse or promote products derived from this software
63  *    without specific prior written permission.
64  *
65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75  * SUCH DAMAGE.
76  *
77  *	from: Utah Hdr: machdep.c 1.74 92/12/20
78  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
79  */
80 
81 /*-
82  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
83  * All rights reserved.
84  *
85  * This code is derived from software contributed to The NetBSD Foundation
86  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
87  * NASA Ames Research Center.
88  *
89  * Redistribution and use in source and binary forms, with or without
90  * modification, are permitted provided that the following conditions
91  * are met:
92  * 1. Redistributions of source code must retain the above copyright
93  *    notice, this list of conditions and the following disclaimer.
94  * 2. Redistributions in binary form must reproduce the above copyright
95  *    notice, this list of conditions and the following disclaimer in the
96  *    documentation and/or other materials provided with the distribution.
97  *
98  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
99  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
100  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
101  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
102  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
103  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
104  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
105  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
106  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
107  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
108  * POSSIBILITY OF SUCH DAMAGE.
109  */
110 
111 /*
112  * Copyright (c) 1992, 1993
113  *	The Regents of the University of California.  All rights reserved.
114  *
115  * This software was developed by the Computer Systems Engineering group
116  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
117  * contributed to Berkeley.
118  *
119  * All advertising materials mentioning features or use of this software
120  * must display the following acknowledgement:
121  *	This product includes software developed by the University of
122  *	California, Lawrence Berkeley Laboratory.
123  *
124  * Redistribution and use in source and binary forms, with or without
125  * modification, are permitted provided that the following conditions
126  * are met:
127  * 1. Redistributions of source code must retain the above copyright
128  *    notice, this list of conditions and the following disclaimer.
129  * 2. Redistributions in binary form must reproduce the above copyright
130  *    notice, this list of conditions and the following disclaimer in the
131  *    documentation and/or other materials provided with the distribution.
132  * 3. All advertising materials mentioning features or use of this software
133  *    must display the following acknowledgement:
134  *	This product includes software developed by the University of
135  *	California, Berkeley and its contributors.
136  * 4. Neither the name of the University nor the names of its contributors
137  *    may be used to endorse or promote products derived from this software
138  *    without specific prior written permission.
139  *
140  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
141  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
142  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
143  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
144  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
145  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
146  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
147  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
148  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
149  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
150  * SUCH DAMAGE.
151  *
152  *	@(#)machdep.c	8.6 (Berkeley) 1/14/94
153  */
154 
155 #include <sys/cdefs.h>
156 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.65 2010/02/08 19:02:32 joerg Exp $");
157 
158 #include "opt_ddb.h"
159 #include "opt_kgdb.h"
160 #include "opt_fpu_emulate.h"
161 #include "opt_modular.h"
162 
163 #include <sys/param.h>
164 #include <sys/systm.h>
165 #include <sys/kernel.h>
166 #include <sys/proc.h>
167 #include <sys/buf.h>
168 #include <sys/reboot.h>
169 #include <sys/conf.h>
170 #include <sys/file.h>
171 #include <sys/device.h>
172 #include <sys/malloc.h>
173 #include <sys/extent.h>
174 #include <sys/mbuf.h>
175 #include <sys/msgbuf.h>
176 #include <sys/ioctl.h>
177 #include <sys/tty.h>
178 #include <sys/mount.h>
179 #include <sys/exec.h>
180 #include <sys/exec_aout.h>		/* for MID_* */
181 #include <sys/core.h>
182 #include <sys/kcore.h>
183 #include <sys/vnode.h>
184 #include <sys/syscallargs.h>
185 #include <sys/ksyms.h>
186 #ifdef	KGDB
187 #include <sys/kgdb.h>
188 #endif
189 
190 #include <uvm/uvm.h> /* XXX: not _extern ... need vm_map_create */
191 
192 #include <sys/sysctl.h>
193 
194 #include <dev/cons.h>
195 
196 #include <machine/promlib.h>
197 #include <machine/cpu.h>
198 #include <machine/dvma.h>
199 #include <machine/idprom.h>
200 #include <machine/kcore.h>
201 #include <machine/reg.h>
202 #include <machine/psl.h>
203 #include <machine/pte.h>
204 #define _SUN68K_BUS_DMA_PRIVATE
205 #include <machine/autoconf.h>
206 #include <machine/bus.h>
207 #include <machine/intr.h>
208 #include <machine/pmap.h>
209 
210 #if defined(DDB)
211 #include <machine/db_machdep.h>
212 #include <ddb/db_sym.h>
213 #include <ddb/db_extern.h>
214 #endif
215 
216 #include <dev/vme/vmereg.h>
217 #include <dev/vme/vmevar.h>
218 
219 #include <sun2/sun2/control.h>
220 #include <sun2/sun2/enable.h>
221 #include <sun2/sun2/machdep.h>
222 
223 #include <sun68k/sun68k/vme_sun68k.h>
224 
225 #include "ksyms.h"
226 
227 /* Defined in locore.s */
228 extern char kernel_text[];
229 /* Defined by the linker */
230 extern char etext[];
231 /* Defined in vfs_bio.c */
232 extern u_int bufpages;
233 
234 /* Our exported CPU info; we can have only one. */
235 struct cpu_info cpu_info_store;
236 
237 struct vm_map *phys_map = NULL;
238 
239 int	physmem;
240 int	fputype;
241 void *	msgbufaddr;
242 
243 /* Virtual page frame for /dev/mem (see mem.c) */
244 vaddr_t vmmap;
245 
246 /*
247  * safepri is a safe priority for sleep to set for a spin-wait
248  * during autoconfiguration or after a panic.
249  */
250 int	safepri = PSL_LOWIPL;
251 
252 /* Soft copy of the enable register. */
253 volatile u_short enable_reg_soft = ENABLE_REG_SOFT_UNDEF;
254 
255 /*
256  * Our no-fault fault handler.
257  */
258 label_t *nofault;
259 
260 /*
261  * dvmamap is used to manage DVMA memory.
262  */
263 static struct extent *dvmamap;
264 
265 /* Our private scratch page for dumping the MMU. */
266 static vaddr_t dumppage;
267 
268 static void identifycpu(void);
269 static void initcpu(void);
270 
271 /*
272  * cpu_startup: allocate memory for variable-sized tables,
273  * initialize CPU, and do autoconfiguration.
274  *
275  * This is called early in init_main.c:main(), after the
276  * kernel memory allocator is ready for use, but before
277  * the creation of processes 1,2, and mountroot, etc.
278  */
279 void
280 cpu_startup(void)
281 {
282 	void *v;
283 	vaddr_t minaddr, maxaddr;
284 	char pbuf[9];
285 
286 	/*
287 	 * Initialize message buffer (for kernel printf).
288 	 * This is put in physical pages four through seven
289 	 * so it will always be in the same place after a
290 	 * reboot. (physical pages 0-3 are reserved by the PROM
291 	 * for its vector table and other stuff.)
292 	 * Its mapping was prepared in pmap_bootstrap().
293 	 * Also, offset some to avoid PROM scribbles.
294 	 */
295 	v = (void *) (PAGE_SIZE * 4);
296 	msgbufaddr = (void *)((char *)v + MSGBUFOFF);
297 	initmsgbuf(msgbufaddr, MSGBUFSIZE);
298 
299 #if NKSYMS || defined(DDB) || defined(MODULAR)
300 	{
301 		extern int nsym;
302 		extern char *ssym, *esym;
303 
304 		ksyms_addsyms_elf(nsym, ssym, esym);
305 	}
306 #endif /* DDB */
307 
308 	/*
309 	 * Good {morning,afternoon,evening,night}.
310 	 */
311 	printf("%s%s", copyright, version);
312 	identifycpu();
313 	fputype = FPU_NONE;
314 #ifdef  FPU_EMULATE
315 	printf("fpu: emulator\n");
316 #else
317 	printf("fpu: no math support\n");
318 #endif
319 
320 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
321 	printf("total memory = %s\n", pbuf);
322 
323 	/*
324 	 * XXX fredette - we force a small number of buffers
325 	 * to help me debug this on my low-memory machine.
326 	 * this should go away at some point, allowing the
327 	 * normal automatic buffer-sizing to happen.
328 	 */
329 	bufpages = 37;
330 
331 	/*
332 	 * Get scratch page for dumpsys().
333 	 */
334 	if ((dumppage = uvm_km_alloc(kernel_map, PAGE_SIZE,0, UVM_KMF_WIRED))
335 	    == 0)
336 		panic("startup: alloc dumppage");
337 
338 
339 	minaddr = 0;
340 
341 	/*
342 	 * Allocate a submap for physio
343 	 */
344 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
345 				   VM_PHYS_SIZE, 0, false, NULL);
346 
347 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
348 	printf("avail memory = %s\n", pbuf);
349 
350 	/*
351 	 * Allocate a virtual page (for use by /dev/mem)
352 	 * This page is handed to pmap_enter() therefore
353 	 * it has to be in the normal kernel VA range.
354 	 */
355 	vmmap = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
356 	    UVM_KMF_VAONLY | UVM_KMF_WAITVA);
357 
358 	/*
359 	 * Allocate DMA map for devices on the bus.
360 	 */
361 	dvmamap = extent_create("dvmamap",
362 	    DVMA_MAP_BASE, DVMA_MAP_BASE + DVMA_MAP_AVAIL,
363 	    M_DEVBUF, 0, 0, EX_NOWAIT);
364 	if (dvmamap == NULL)
365 		panic("unable to allocate DVMA map");
366 
367 	/*
368 	 * Set up CPU-specific registers, cache, etc.
369 	 */
370 	initcpu();
371 }
372 
373 /*
374  * Set registers on exec.
375  */
376 void
377 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
378 {
379 	struct trapframe *tf = (struct trapframe *)l->l_md.md_regs;
380 	struct pcb *pcb = lwp_getpcb(l);
381 
382 	tf->tf_sr = PSL_USERSET;
383 	tf->tf_pc = pack->ep_entry & ~1;
384 	tf->tf_regs[D0] = 0;
385 	tf->tf_regs[D1] = 0;
386 	tf->tf_regs[D2] = 0;
387 	tf->tf_regs[D3] = 0;
388 	tf->tf_regs[D4] = 0;
389 	tf->tf_regs[D5] = 0;
390 	tf->tf_regs[D6] = 0;
391 	tf->tf_regs[D7] = 0;
392 	tf->tf_regs[A0] = 0;
393 	tf->tf_regs[A1] = 0;
394 	tf->tf_regs[A2] = (int)l->l_proc->p_psstr;
395 	tf->tf_regs[A3] = 0;
396 	tf->tf_regs[A4] = 0;
397 	tf->tf_regs[A5] = 0;
398 	tf->tf_regs[A6] = 0;
399 	tf->tf_regs[SP] = stack;
400 
401 	/* restore a null state frame */
402 	pcb->pcb_fpregs.fpf_null = 0;
403 
404 	l->l_md.md_flags = 0;
405 }
406 
407 /*
408  * Info for CTL_HW
409  */
410 char	machine[16] = MACHINE;		/* from <machine/param.h> */
411 char	kernel_arch[16] = "sun2";	/* XXX needs a sysctl node */
412 char	cpu_model[120];
413 
414 /*
415  * Determine which Sun2 model we are running on.
416  */
417 void
418 identifycpu(void)
419 {
420 	extern char *cpu_string;	/* XXX */
421 
422 	/* Other stuff? (VAC, mc6888x version, etc.) */
423 	/* Note: miniroot cares about the kernel_arch part. */
424 	sprintf(cpu_model, "%s %s", kernel_arch, cpu_string);
425 
426 	printf("Model: %s\n", cpu_model);
427 }
428 
429 /*
430  * machine dependent system variables.
431  */
432 #if 0	/* XXX - Not yet... */
433 static int
434 sysctl_machdep_root_device(SYSCTLFN_ARGS)
435 {
436 	struct sysctlnode node = *rnode;
437 
438 	node.sysctl_data = some permutation on root_device;
439 	node.sysctl_size = strlen(root_device) + 1;
440 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
441 }
442 #endif
443 
444 static int
445 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
446 {
447 	struct sysctlnode node = *rnode;
448 	char *cp;
449 
450 	cp = prom_getbootfile();
451 	if (cp == NULL || cp[0] == '\0')
452 		return (ENOENT);
453 
454 	node.sysctl_data = cp;
455 	node.sysctl_size = strlen(cp) + 1;
456 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
457 }
458 
459 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
460 {
461 
462 	sysctl_createv(clog, 0, NULL, NULL,
463 		       CTLFLAG_PERMANENT,
464 		       CTLTYPE_NODE, "machdep", NULL,
465 		       NULL, 0, NULL, 0,
466 		       CTL_MACHDEP, CTL_EOL);
467 
468 	sysctl_createv(clog, 0, NULL, NULL,
469 		       CTLFLAG_PERMANENT,
470 		       CTLTYPE_STRUCT, "console_device", NULL,
471 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
472 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
473 #if 0	/* XXX - Not yet... */
474 	sysctl_createv(clog, 0, NULL, NULL,
475 		       CTLFLAG_PERMANENT,
476 		       CTLTYPE_STRING, "root_device", NULL,
477 		       sysctl_machdep_root_device, 0, NULL, 0,
478 		       CTL_MACHDEP, CPU_ROOT_DEVICE, CTL_EOL);
479 #endif
480 	sysctl_createv(clog, 0, NULL, NULL,
481 		       CTLFLAG_PERMANENT,
482 		       CTLTYPE_STRING, "booted_kernel", NULL,
483 		       sysctl_machdep_booted_kernel, 0, NULL, 0,
484 		       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
485 }
486 
487 /* See: sig_machdep.c */
488 
489 /*
490  * Do a sync in preparation for a reboot.
491  * XXX - This could probably be common code.
492  * XXX - And now, most of it is in vfs_shutdown()
493  * XXX - Put waittime checks in there too?
494  */
495 int waittime = -1;	/* XXX - Who else looks at this? -gwr */
496 static void
497 reboot_sync(void)
498 {
499 
500 	/* Check waittime here to localize its use to this function. */
501 	if (waittime >= 0)
502 		return;
503 	waittime = 0;
504 	vfs_shutdown();
505 }
506 
507 /*
508  * Common part of the BSD and SunOS reboot system calls.
509  */
510 __dead void
511 cpu_reboot(int howto, char *user_boot_string)
512 {
513 	char *bs, *p;
514 	char default_boot_string[8];
515 
516 	/* If system is cold, just halt. (early panic?) */
517 	if (cold)
518 		goto haltsys;
519 
520 	/* Un-blank the screen if appropriate. */
521 	cnpollc(1);
522 
523 	if ((howto & RB_NOSYNC) == 0) {
524 		reboot_sync();
525 		/*
526 		 * If we've been adjusting the clock, the todr
527 		 * will be out of synch; adjust it now.
528 		 *
529 		 * XXX - However, if the kernel has been sitting in ddb,
530 		 * the time will be way off, so don't set the HW clock!
531 		 * XXX - Should do sanity check against HW clock. -gwr
532 		 */
533 		/* resettodr(); */
534 	}
535 
536 	/* Disable interrupts. */
537 	splhigh();
538 
539 	/* Write out a crash dump if asked. */
540 	if (howto & RB_DUMP)
541 		dumpsys();
542 
543 	/* run any shutdown hooks */
544 	doshutdownhooks();
545 
546 	pmf_system_shutdown(boothowto);
547 
548 	if (howto & RB_HALT) {
549 	haltsys:
550 		printf("halted.\n");
551 		prom_halt();
552 	}
553 
554 	/*
555 	 * Automatic reboot.
556 	 */
557 	bs = user_boot_string;
558 	if (bs == NULL) {
559 		/*
560 		 * Build our own boot string with an empty
561 		 * boot device/file and (maybe) some flags.
562 		 * The PROM will supply the device/file name.
563 		 */
564 		bs = default_boot_string;
565 		*bs = '\0';
566 		if (howto & (RB_KDB|RB_ASKNAME|RB_SINGLE)) {
567 			/* Append the boot flags. */
568 			p = bs;
569 			*p++ = ' ';
570 			*p++ = '-';
571 			if (howto & RB_KDB)
572 				*p++ = 'd';
573 			if (howto & RB_ASKNAME)
574 				*p++ = 'a';
575 			if (howto & RB_SINGLE)
576 				*p++ = 's';
577 			*p = '\0';
578 		}
579 	}
580 	printf("rebooting...\n");
581 	prom_boot(bs);
582 	for (;;) ;
583 	/*NOTREACHED*/
584 }
585 
586 /*
587  * These variables are needed by /sbin/savecore
588  */
589 uint32_t dumpmag = 0x8fca0101;	/* magic number */
590 int 	dumpsize = 0;		/* pages */
591 long	dumplo = 0; 		/* blocks */
592 
593 #define	DUMP_EXTRA 	3	/* CPU-dependent extra pages */
594 
595 /*
596  * This is called by main to set dumplo, dumpsize.
597  * Dumps always skip the first PAGE_SIZE of disk space
598  * in case there might be a disk label stored there.
599  * If there is extra space, put dump at the end to
600  * reduce the chance that swapping trashes it.
601  */
602 void
603 cpu_dumpconf(void)
604 {
605 	const struct bdevsw *bdev;
606 	int devblks;	/* size of dump device in blocks */
607 	int dumpblks;	/* size of dump image in blocks */
608 	int (*getsize)(dev_t);
609 
610 	if (dumpdev == NODEV)
611 		return;
612 
613 	bdev = bdevsw_lookup(dumpdev);
614 	if (bdev == NULL) {
615 		dumpdev = NODEV;
616 		return;
617 	}
618 	getsize = bdev->d_psize;
619 	if (getsize == NULL)
620 		return;
621 	devblks = (*getsize)(dumpdev);
622 	if (devblks <= ctod(1))
623 		return;
624 	devblks &= ~(ctod(1)-1);
625 
626 	/*
627 	 * Note: savecore expects dumpsize to be the
628 	 * number of pages AFTER the dump header.
629 	 */
630 	dumpsize = physmem;
631 
632 	/* Position dump image near end of space, page aligned. */
633 	dumpblks = ctod(physmem + DUMP_EXTRA);
634 	dumplo = devblks - dumpblks;
635 
636 	/* If it does not fit, truncate it by moving dumplo. */
637 	/* Note: Must force signed comparison. */
638 	if (dumplo < ((long)ctod(1))) {
639 		dumplo = ctod(1);
640 		dumpsize = dtoc(devblks - dumplo) - DUMP_EXTRA;
641 	}
642 }
643 
644 /* Note: gdb looks for "dumppcb" in a kernel crash dump. */
645 struct pcb dumppcb;
646 extern paddr_t avail_start;
647 
648 /*
649  * Write a crash dump.  The format while in swap is:
650  *   kcore_seg_t cpu_hdr;
651  *   cpu_kcore_hdr_t cpu_data;
652  *   padding (PAGE_SIZE-sizeof(kcore_seg_t))
653  *   pagemap (2*PAGE_SIZE)
654  *   physical memory...
655  */
656 void
657 dumpsys(void)
658 {
659 	const struct bdevsw *dsw;
660 	kcore_seg_t	*kseg_p;
661 	cpu_kcore_hdr_t *chdr_p;
662 	struct sun2_kcore_hdr *sh;
663 	char *vaddr;
664 	paddr_t paddr;
665 	int psize, todo, chunk;
666 	daddr_t blkno;
667 	int error = 0;
668 
669 	if (dumpdev == NODEV)
670 		return;
671 	dsw = bdevsw_lookup(dumpdev);
672 	if (dsw == NULL || dsw->d_psize == NULL)
673 		return;
674 	if (dumppage == 0)
675 		return;
676 
677 	/*
678 	 * For dumps during autoconfiguration,
679 	 * if dump device has already configured...
680 	 */
681 	if (dumpsize == 0)
682 		cpu_dumpconf();
683 	if (dumplo <= 0) {
684 		printf("\ndump to dev %u,%u not possible\n",
685 		    major(dumpdev), minor(dumpdev));
686 		return;
687 	}
688 	savectx(&dumppcb);
689 
690 	psize = (*(dsw->d_psize))(dumpdev);
691 	if (psize == -1) {
692 		printf("dump area unavailable\n");
693 		return;
694 	}
695 
696 	printf("\ndumping to dev %u,%u offset %ld\n",
697 	    major(dumpdev), minor(dumpdev), dumplo);
698 
699 	/*
700 	 * Prepare the dump header, including MMU state.
701 	 */
702 	blkno = dumplo;
703 	todo = dumpsize;	/* pages */
704 	vaddr = (char*)dumppage;
705 	memset(vaddr, 0, PAGE_SIZE);
706 
707 	/* Set pointers to all three parts. */
708 	kseg_p = (kcore_seg_t *)vaddr;
709 	chdr_p = (cpu_kcore_hdr_t *) (kseg_p + 1);
710 	sh = &chdr_p->un._sun2;
711 
712 	/* Fill in kcore_seg_t part. */
713 	CORE_SETMAGIC(*kseg_p, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
714 	kseg_p->c_size = (ctob(DUMP_EXTRA) - sizeof(*kseg_p));
715 
716 	/* Fill in cpu_kcore_hdr_t part. */
717 	strncpy(chdr_p->name, kernel_arch, sizeof(chdr_p->name));
718 	chdr_p->page_size = PAGE_SIZE;
719 	chdr_p->kernbase = KERNBASE;
720 
721 	/* Fill in the sun2_kcore_hdr part (MMU state). */
722 	pmap_kcore_hdr(sh);
723 
724 	/* Write out the dump header. */
725 	error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
726 	if (error)
727 		goto fail;
728 	blkno += btodb(PAGE_SIZE);
729 
730 	/* translation RAM (pages zero through seven) */
731 	for(chunk = 0; chunk < (PAGE_SIZE * 8); chunk += PAGE_SIZE) {
732 		pmap_get_pagemap((int*)vaddr, chunk);
733 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
734 		if (error)
735 			goto fail;
736 		blkno += btodb(PAGE_SIZE);
737 	}
738 
739 	/*
740 	 * Now dump physical memory.  Have to do it in two chunks.
741 	 * The first chunk is "unmanaged" (by the VM code) and its
742 	 * range of physical addresses is not allow in pmap_enter.
743 	 * However, that segment is mapped linearly, so we can just
744 	 * use the virtual mappings already in place.  The second
745 	 * chunk is done the normal way, using pmap_enter.
746 	 *
747 	 * Note that vaddr==(paddr+KERNBASE) for paddr=0 through etext.
748 	 */
749 
750 	/* Do the first chunk (0 <= PA < avail_start) */
751 	paddr = 0;
752 	chunk = btoc(avail_start);
753 	if (chunk > todo)
754 		chunk = todo;
755 	do {
756 		if ((todo & 0xf) == 0)
757 			printf_nolog("\r%4d", todo);
758 		vaddr = (char*)(paddr + KERNBASE);
759 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
760 		if (error)
761 			goto fail;
762 		paddr += PAGE_SIZE;
763 		blkno += btodb(PAGE_SIZE);
764 		--todo;
765 	} while (--chunk > 0);
766 
767 	/* Do the second chunk (avail_start <= PA < dumpsize) */
768 	vaddr = (char*)vmmap;	/* Borrow /dev/mem VA */
769 	do {
770 		if ((todo & 0xf) == 0)
771 			printf_nolog("\r%4d", todo);
772 		pmap_kenter_pa(vmmap, paddr | PMAP_NC, VM_PROT_READ, 0);
773 		pmap_update(pmap_kernel());
774 		error = (*dsw->d_dump)(dumpdev, blkno, vaddr, PAGE_SIZE);
775 		pmap_kremove(vmmap, PAGE_SIZE);
776 		pmap_update(pmap_kernel());
777 		if (error)
778 			goto fail;
779 		paddr += PAGE_SIZE;
780 		blkno += btodb(PAGE_SIZE);
781 	} while (--todo > 0);
782 
783 	printf("\rdump succeeded\n");
784 	return;
785 fail:
786 	printf(" dump error=%d\n", error);
787 }
788 
789 static void
790 initcpu(void)
791 {
792 	/* XXX: Enable RAM parity/ECC checking? */
793 	/* XXX: parityenable(); */
794 
795 }
796 
797 /* straptrap() in trap.c */
798 
799 /* from hp300: badaddr() */
800 
801 /* XXX: parityenable() ? */
802 /* regdump() moved to regdump.c */
803 
804 /*
805  * cpu_exec_aout_makecmds():
806  *	CPU-dependent a.out format hook for execve().
807  *
808  * Determine if the given exec package refers to something which we
809  * understand and, if so, set up the vmcmds for it.
810  */
811 int
812 cpu_exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
813 {
814 	return ENOEXEC;
815 }
816 
817 #if 0
818 /*
819  * Soft interrupt support.
820  */
821 void
822 isr_soft_request(int level)
823 {
824 	u_char bit;
825 
826 	if ((level < _IPL_SOFT_LEVEL_MIN) || (level > _IPL_SOFT_LEVEL_MAX))
827 		return;
828 
829 	bit = 1 << level;
830 	enable_reg_or(bit);
831 }
832 
833 void
834 isr_soft_clear(int level)
835 {
836 	u_char bit;
837 
838 	if ((level < _IPL_SOFT_LEVEL_MIN) || (level > _IPL_SOFT_LEVEL_MAX))
839 		return;
840 
841 	bit = 1 << level;
842 	enable_reg_and(~bit);
843 }
844 #endif
845 
846 /*
847  * Like _bus_dmamap_load(), but for raw memory allocated with
848  * bus_dmamem_alloc().
849  */
850 int
851 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs,
852     int nsegs, bus_size_t size, int flags)
853 {
854 	struct vm_page *m;
855 	paddr_t pa;
856 	bus_addr_t dva;
857 	bus_size_t sgsize;
858 	struct pglist *mlist;
859 	int pagesz = PAGE_SIZE;
860 	int error;
861 
862 	/*
863 	 * Make sure that on error condition we return "no valid mappings".
864 	 */
865 	map->dm_nsegs = 0;
866 	map->dm_mapsize = 0;
867 
868 	/* Allocate DVMA addresses */
869 	sgsize = (size + pagesz - 1) & -pagesz;
870 
871 	/*
872 	 * If the device can see our entire 24-bit address space,
873 	 * we can use any properly aligned virtual addresses.
874 	 */
875 	if ((map->_dm_flags & BUS_DMA_24BIT) != 0) {
876 		dva = _bus_dma_valloc_skewed(sgsize, map->_dm_boundary,
877 					     pagesz, 0);
878 		if (dva == 0)
879 			return (ENOMEM);
880 	}
881 
882 	/*
883 	 * Otherwise, we need virtual addresses in DVMA space.
884 	 */
885 	else {
886 		error = extent_alloc(dvmamap, sgsize, pagesz,
887 					map->_dm_boundary,
888 					(flags & BUS_DMA_NOWAIT) == 0
889 						? EX_WAITOK : EX_NOWAIT,
890 					(u_long *)&dva);
891 		if (error)
892 			return (error);
893 	}
894 
895 	/* Fill in the segment. */
896 	map->dm_segs[0].ds_addr = dva;
897 	map->dm_segs[0].ds_len = size;
898 	map->dm_segs[0]._ds_va = dva;
899 	map->dm_segs[0]._ds_sgsize = sgsize;
900 
901 	/* Map physical pages into MMU */
902 	mlist = segs[0]._ds_mlist;
903 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq.queue)) {
904 		if (sgsize == 0)
905 			panic("_bus_dmamap_load_raw: size botch");
906 		pa = VM_PAGE_TO_PHYS(m);
907 		pmap_enter(pmap_kernel(), dva,
908 			   (pa & -pagesz) | PMAP_NC,
909 			   VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
910 
911 		dva += pagesz;
912 		sgsize -= pagesz;
913 	}
914 	pmap_update(pmap_kernel());
915 
916 	/* Make the map truly valid. */
917 	map->dm_nsegs = 1;
918 	map->dm_mapsize = size;
919 
920 	return (0);
921 }
922 
923 /*
924  * load DMA map with a linear buffer.
925  */
926 int
927 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
928     bus_size_t buflen, struct proc *p, int flags)
929 {
930 	bus_size_t sgsize;
931 	vaddr_t va = (vaddr_t)buf;
932 	int pagesz = PAGE_SIZE;
933 	bus_addr_t dva;
934 	pmap_t pmap;
935 	int rv;
936 
937 	/*
938 	 * Make sure that on error condition we return "no valid mappings".
939 	 */
940 	map->dm_nsegs = 0;
941 	map->dm_mapsize = 0;
942 
943 	if (buflen > map->_dm_size)
944 		return (EINVAL);
945 
946 	/*
947 	 * A 24-bit device can see all of our kernel address space, so
948 	 * if we have KVAs, we can just load them as-is, no mapping
949 	 * necessary.
950 	 */
951 	if ((map->_dm_flags & BUS_DMA_24BIT) != 0 && p == NULL) {
952 		/*
953 		 * XXX Need to implement "don't DMA across this boundry".
954 		 */
955 		if (map->_dm_boundary != 0)
956 			panic("bus_dmamap_load: boundaries not implemented");
957 		map->dm_mapsize = buflen;
958 		map->dm_nsegs = 1;
959 		map->dm_segs[0].ds_addr = (bus_addr_t)va;
960 		map->dm_segs[0].ds_len = buflen;
961 		map->_dm_flags |= _BUS_DMA_DIRECTMAP;
962 		return (0);
963 	}
964 
965 	/*
966 	 * Allocate a region in DVMA space.
967 	 */
968 	sgsize = m68k_round_page(buflen + (va & (pagesz - 1)));
969 
970 	if (extent_alloc(dvmamap, sgsize, pagesz, map->_dm_boundary,
971 			 (flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT,
972 			 (u_long *)&dva) != 0) {
973 		return (ENOMEM);
974 	}
975 
976 	/* Fill in the segment. */
977 	map->dm_segs[0].ds_addr = dva + (va & (pagesz - 1));
978 	map->dm_segs[0].ds_len = buflen;
979 	map->dm_segs[0]._ds_va = dva;
980 	map->dm_segs[0]._ds_sgsize = sgsize;
981 
982 	/*
983 	 * Now map the DVMA addresses we allocated to point to the
984 	 * pages of the caller's buffer.
985 	 */
986 	if (p != NULL)
987 		pmap = p->p_vmspace->vm_map.pmap;
988 	else
989 		pmap = pmap_kernel();
990 
991 	for (; buflen > 0; ) {
992 		paddr_t pa;
993 		/*
994 		 * Get the physical address for this page.
995 		 */
996 		rv = pmap_extract(pmap, va, &pa);
997 #ifdef	DIAGNOSTIC
998 		if (!rv)
999 			panic("_bus_dmamap_load: no page");
1000 #endif	/* DIAGNOSTIC */
1001 
1002 		/*
1003 		 * Compute the segment size, and adjust counts.
1004 		 */
1005 		sgsize = pagesz - (va & (pagesz - 1));
1006 		if (buflen < sgsize)
1007 			sgsize = buflen;
1008 
1009 		pmap_enter(pmap_kernel(), dva,
1010 			   (pa & -pagesz) | PMAP_NC,
1011 			   VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
1012 
1013 		dva += pagesz;
1014 		va += sgsize;
1015 		buflen -= sgsize;
1016 	}
1017 	pmap_update(pmap_kernel());
1018 
1019 	/* Make the map truly valid. */
1020 	map->dm_nsegs = 1;
1021 	map->dm_mapsize = map->dm_segs[0].ds_len;
1022 
1023 	return (0);
1024 }
1025 
1026 /*
1027  * unload a DMA map.
1028  */
1029 void
1030 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
1031 {
1032 	bus_dma_segment_t *segs = map->dm_segs;
1033 	int nsegs = map->dm_nsegs;
1034 	int flags = map->_dm_flags;
1035 	bus_addr_t dva;
1036 	bus_size_t len;
1037 	int s, error;
1038 
1039 	if (nsegs != 1)
1040 		panic("_bus_dmamem_unload: nsegs = %d", nsegs);
1041 
1042 	/*
1043 	 * _BUS_DMA_DIRECTMAP is set iff this map was loaded using
1044 	 * _bus_dmamap_load for a 24-bit device.
1045 	 */
1046 	if ((flags & _BUS_DMA_DIRECTMAP) != 0) {
1047 		/* Nothing to release */
1048 		map->_dm_flags &= ~_BUS_DMA_DIRECTMAP;
1049 	}
1050 
1051 	/*
1052 	 * Otherwise, this map was loaded using _bus_dmamap_load for a
1053 	 * non-24-bit device, or using _bus_dmamap_load_raw.
1054 	 */
1055 	else {
1056 		dva = segs[0]._ds_va & -PAGE_SIZE;
1057 		len = segs[0]._ds_sgsize;
1058 
1059 		/*
1060 		 * Unmap the DVMA addresses.
1061 		 */
1062 		pmap_remove(pmap_kernel(), dva, dva + len);
1063 		pmap_update(pmap_kernel());
1064 
1065 		/*
1066 		 * Free the DVMA addresses.
1067 		 */
1068 		if ((flags & BUS_DMA_24BIT) != 0) {
1069 			/*
1070 			 * This map was loaded using _bus_dmamap_load_raw
1071 			 * for a 24-bit device.
1072 			 */
1073 			uvm_unmap(kernel_map, dva, dva + len);
1074 		} else {
1075 			/*
1076 			 * This map was loaded using _bus_dmamap_load or
1077 			 * _bus_dmamap_load_raw for a non-24-bit device.
1078 			 */
1079 			s = splhigh();
1080 			error = extent_free(dvmamap, dva, len, EX_NOWAIT);
1081 			splx(s);
1082 			if (error != 0)
1083 				printf("warning: %ld of DVMA space lost\n", len);
1084 		}
1085 	}
1086 
1087 	/* Mark the mappings as invalid. */
1088 	map->dm_mapsize = 0;
1089 	map->dm_nsegs = 0;
1090 }
1091 
1092 /*
1093  * Translate a VME address and address modifier
1094  * into a CPU physical address and page type.
1095  */
1096 int
1097 vmebus_translate(vme_am_t mod, vme_addr_t addr, bus_type_t *btp,
1098     bus_addr_t *bap)
1099 {
1100 	bus_addr_t base;
1101 
1102 	switch(mod) {
1103 #define _DS (VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA)
1104 
1105 	case (VME_AM_A16|_DS):
1106 		base = 0x00ff0000;
1107 		break;
1108 
1109 	case (VME_AM_A24|_DS):
1110 		base = 0;
1111 		break;
1112 
1113 	default:
1114 		return (ENOENT);
1115 #undef _DS
1116 	}
1117 
1118 	*bap = base | addr;
1119 	*btp = (*bap & 0x800000 ? PMAP_VME8 : PMAP_VME0);
1120 	return (0);
1121 }
1122 
1123 /*
1124  * If we can find a mapping that was established by the PROM, use it.
1125  */
1126 int
1127 find_prom_map(paddr_t pa, bus_type_t iospace, int len, vaddr_t *vap)
1128 {
1129 	u_long	pf;
1130 	int	pgtype;
1131 	vaddr_t	va, eva;
1132 	int	sme;
1133 	u_long	pte;
1134 	int	saved_ctx;
1135 
1136 	/*
1137 	 * The mapping must fit entirely within one page.
1138 	 */
1139 	if ((((u_long)pa & PGOFSET) + len) > PAGE_SIZE)
1140 		return EINVAL;
1141 
1142 	pf = PA_PGNUM(pa);
1143 	pgtype = iospace << PG_MOD_SHIFT;
1144 	saved_ctx = kernel_context();
1145 
1146 	/*
1147 	 * Walk the PROM address space, looking for a page with the
1148 	 * mapping we want.
1149 	 */
1150 	for (va = SUN_MONSTART; va < SUN_MONEND; ) {
1151 
1152 		/*
1153 		 * Make sure this segment is mapped.
1154 		 */
1155 		sme = get_segmap(va);
1156 		if (sme == SEGINV) {
1157 			va += NBSG;
1158 			continue;			/* next segment */
1159 		}
1160 
1161 		/*
1162 		 * Walk the pages of this segment.
1163 		 */
1164 		for(eva = va + NBSG; va < eva; va += PAGE_SIZE) {
1165 			pte = get_pte(va);
1166 
1167 			if ((pte & (PG_VALID | PG_TYPE)) ==
1168 				(PG_VALID | pgtype) &&
1169 			    PG_PFNUM(pte) == pf)
1170 			{
1171 				/*
1172 				 * Found the PROM mapping.
1173 				 * note: preserve page offset
1174 				 */
1175 				*vap = (va | ((vaddr_t)pa & PGOFSET));
1176 				restore_context(saved_ctx);
1177 				return 0;
1178 			}
1179 		}
1180 	}
1181 	restore_context(saved_ctx);
1182 	return ENOENT;
1183 }
1184