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