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