xref: /original-bsd/sys/sparc/sparc/machdep.c (revision 6c394c2f)
1 /*
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * All advertising materials mentioning features or use of this software
10  * must display the following acknowledgement:
11  *	This product includes software developed by the University of
12  *	California, Lawrence Berkeley Laboratories.
13  *
14  * %sccs.include.redist.c%
15  *
16  *	@(#)machdep.c	7.2 (Berkeley) 07/21/92
17  *
18  * from: $Header: machdep.c,v 1.32 92/07/13 01:41:14 torek Exp $
19  */
20 
21 #include "param.h"
22 #include "proc.h"
23 #include "user.h"
24 #include "map.h"
25 #include "buf.h"
26 #include "device.h"
27 #include "reboot.h"
28 #include "systm.h"
29 #include "conf.h"
30 #include "file.h"
31 #include "clist.h"
32 #include "callout.h"
33 #include "malloc.h"
34 #include "mbuf.h"
35 #include "mount.h"
36 #include "msgbuf.h"
37 #ifdef SYSVSHM
38 #include "shm.h"
39 #endif
40 #include "exec.h"
41 
42 #include "machine/autoconf.h"
43 #include "machine/frame.h"
44 #include "machine/cpu.h"
45 
46 #include "vm/vm_kern.h"
47 #include "vm/vm_page.h"
48 
49 #include "asm.h"
50 #include "cache.h"
51 #include "vaddrs.h"
52 
53 vm_map_t buffer_map;
54 extern vm_offset_t avail_end;
55 
56 /*
57  * Declare these as initialized data so we can patch them.
58  */
59 int	nswbuf = 0;
60 #ifdef	NBUF
61 int	nbuf = NBUF;
62 #else
63 int	nbuf = 0;
64 #endif
65 #ifdef	BUFPAGES
66 int	bufpages = BUFPAGES;
67 #else
68 int	bufpages = 0;
69 #endif
70 
71 int	physmem;
72 
73 extern struct msgbuf msgbuf;
74 struct	msgbuf *msgbufp = &msgbuf;
75 int	msgbufmapped = 1;	/* message buffer is always mapped */
76 
77 /*
78  * safepri is a safe priority for sleep to set for a spin-wait
79  * during autoconfiguration or after a panic.
80  */
81 int   safepri = 0;
82 
83 caddr_t allocsys();
84 
85 /*
86  * Machine-dependent startup code
87  */
88 cpu_startup()
89 {
90 	register unsigned i;
91 	register caddr_t v;
92 	register int sz;
93 	int base, residual;
94 #ifdef DEBUG
95 	extern int pmapdebug;
96 	int opmapdebug = pmapdebug;
97 #endif
98 	vm_offset_t minaddr, maxaddr;
99 	vm_size_t size;
100 
101 #ifdef DEBUG
102 	pmapdebug = 0;
103 #endif
104 
105 	/*
106 	 * Good {morning,afternoon,evening,night}.
107 	 */
108 	printf(version);
109 	/*identifycpu();*/
110 	physmem = btoc(avail_end);
111 	printf("real mem = %d\n", avail_end);
112 
113 	/*
114 	 * Find out how much space we need, allocate it,
115 	 * and then give everything true virtual addresses.
116 	 */
117 	sz = (int)allocsys((caddr_t)0);
118 	if ((v = (caddr_t)kmem_alloc(kernel_map, round_page(sz))) == 0)
119 		panic("startup: no room for tables");
120 	if (allocsys(v) - v != sz)
121 		panic("startup: table size inconsistency");
122 
123 	/*
124 	 * Now allocate buffers proper.  They are different than the above
125 	 * in that they usually occupy more virtual memory than physical.
126 	 */
127 	size = MAXBSIZE * nbuf;
128 	buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
129 	    &maxaddr, size, FALSE);
130 	minaddr = (vm_offset_t)buffers;
131 	if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
132 			&minaddr, size, FALSE) != KERN_SUCCESS)
133 		panic("startup: cannot allocate buffers");
134 	base = bufpages / nbuf;
135 	residual = bufpages % nbuf;
136 	for (i = 0; i < nbuf; i++) {
137 		vm_size_t curbufsize;
138 		vm_offset_t curbuf;
139 
140 		/*
141 		 * First <residual> buffers get (base+1) physical pages
142 		 * allocated for them.  The rest get (base) physical pages.
143 		 *
144 		 * The rest of each buffer occupies virtual space,
145 		 * but has no physical memory allocated for it.
146 		 */
147 		curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
148 		curbufsize = CLBYTES * (i < residual ? base+1 : base);
149 		vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
150 		vm_map_simplify(buffer_map, curbuf);
151 	}
152 	/*
153 	 * Allocate a submap for exec arguments.  This map effectively
154 	 * limits the number of processes exec'ing at any time.
155 	 */
156 	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
157 	    16*NCARGS, TRUE);
158 	/*
159 	 * Allocate a map for physio.  Others use a submap of the kernel
160 	 * map, but we want one completely separate, even though it uses
161 	 * the same pmap.
162 	 */
163 	phys_map = vm_map_create(kernel_pmap, DVMA_BASE, DVMA_END, 1);
164 	if (phys_map == NULL)
165 		panic("unable to create DVMA map");
166 
167 	/*
168 	 * Finally, allocate mbuf pool.  Since mclrefcnt is an off-size
169 	 * we use the more space efficient malloc in place of kmem_alloc.
170 	 */
171 	mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES,
172 				   M_MBUF, M_NOWAIT);
173 	bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES);
174 	mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
175 			       VM_MBUF_SIZE, FALSE);
176 	/*
177 	 * Initialize callouts
178 	 */
179 	callfree = callout;
180 	for (i = 1; i < ncallout; i++)
181 		callout[i-1].c_next = &callout[i];
182 	callout[i-1].c_next = NULL;
183 
184 #ifdef DEBUG
185 	pmapdebug = opmapdebug;
186 #endif
187 	printf("avail mem = %d\n", ptoa(cnt.v_free_count));
188 	printf("using %d buffers containing %d bytes of memory\n",
189 		nbuf, bufpages * CLBYTES);
190 
191 	/*
192 	 * Set up buffers, so they can be used to read disk labels.
193 	 */
194 	bufinit();
195 
196 	/*
197 	 * Configure the system.
198 	 */
199 	configure();
200 
201 	/*
202 	 * Turn on the cache (do after configuration due to a bug in
203 	 * some versions of the SPARC chips -- this info from Gilmore).
204 	 */
205 	cache_enable();
206 }
207 
208 /*
209  * Allocate space for system data structures.  We are given
210  * a starting virtual address and we return a final virtual
211  * address; along the way we set each data structure pointer.
212  *
213  * You call allocsys() with 0 to find out how much space we want,
214  * allocate that much and fill it with zeroes, and then call
215  * allocsys() again with the correct base virtual address.
216  */
217 caddr_t
218 allocsys(v)
219 	register caddr_t v;
220 {
221 
222 #define	valloc(name, type, num) \
223 	    v = (caddr_t)(((name) = (type *)v) + (num))
224 	valloc(cfree, struct cblock, nclist);
225 	valloc(callout, struct callout, ncallout);
226 	valloc(swapmap, struct map, nswapmap = maxproc * 2);
227 #ifdef SYSVSHM
228 	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
229 #endif
230 
231 	/*
232 	 * Determine how many buffers to allocate (enough to
233 	 * hold 5% of total physical memory, but at least 16).
234 	 * Allocate 1/2 as many swap buffer headers as file i/o buffers.
235 	 */
236 	if (bufpages == 0)
237 		bufpages = (physmem / 20) / CLSIZE;
238 	if (nbuf == 0) {
239 		nbuf = bufpages;
240 		if (nbuf < 16)
241 			nbuf = 16;
242 	}
243 	if (nswbuf == 0) {
244 		nswbuf = (nbuf / 2) &~ 1;	/* force even */
245 		if (nswbuf > 256)
246 			nswbuf = 256;		/* sanity */
247 	}
248 	valloc(swbuf, struct buf, nswbuf);
249 	valloc(buf, struct buf, nbuf);
250 	return (v);
251 }
252 
253 /*
254  * Set up registers on exec.
255  *
256  * XXX this entire mess must be fixed
257  */
258 /* ARGSUSED */
259 setregs(p, entry, retval)
260 	register struct proc *p;
261 	u_long entry;
262 	int retval[2];
263 {
264 	register struct trapframe *tf = p->p_md.md_tf;
265 	register struct fpstate *fs;
266 	register int psr, sp;
267 
268 	/*
269 	 * The syscall will ``return'' to npc or %g7 or %g2; set them all.
270 	 * Set the rest of the registers to 0 except for %o6 (stack pointer,
271 	 * built in exec()) and psr (retain CWP and PSR_S bits).
272 	 */
273 	psr = tf->tf_psr & (PSR_S | PSR_CWP);
274 	sp = tf->tf_out[6];
275 	if ((fs = p->p_md.md_fpstate) != NULL) {
276 		/*
277 		 * We hold an FPU state.  If we own *the* FPU chip state
278 		 * we must get rid of it, and the only way to do that is
279 		 * to save it.  In any case, get rid of our FPU state.
280 		 */
281 		if (p == fpproc) {
282 			savefpstate(fs);
283 			fpproc = NULL;
284 		}
285 		free((void *)fs, M_SUBPROC);
286 		p->p_md.md_fpstate = NULL;
287 	}
288 	bzero((caddr_t)tf, sizeof *tf);
289 	tf->tf_psr = psr;
290 	tf->tf_global[2] = tf->tf_global[7] = tf->tf_npc = entry & ~3;
291 	tf->tf_out[6] = sp;
292 	retval[1] = 0;
293 }
294 
295 #ifdef DEBUG
296 int sigdebug = 0;
297 int sigpid = 0;
298 #define SDB_FOLLOW	0x01
299 #define SDB_KSTACK	0x02
300 #define SDB_FPSTATE	0x04
301 #endif
302 
303 struct sigframe {
304 	int	sf_signo;		/* signal number */
305 	int	sf_code;		/* code */
306 #ifdef COMPAT_SUNOS
307 	struct	sigcontext *sf_scp;	/* points to user addr of sigcontext */
308 #else
309 	int	sf_xxx;			/* placeholder */
310 #endif
311 	int	sf_addr;		/* SunOS compat, always 0 for now */
312 	struct	sigcontext sf_sc;	/* actual sigcontext */
313 };
314 
315 /*
316  * Send an interrupt to process.
317  */
318 void
319 sendsig(catcher, sig, mask, code)
320 	sig_t catcher;
321 	int sig, mask;
322 	unsigned code;
323 {
324 	register struct proc *p = curproc;
325 	register struct sigacts *psp = p->p_sigacts;
326 	register struct sigframe *fp;
327 	register struct trapframe *tf;
328 	register int addr, oonstack;
329 	struct sigframe sf;
330 	extern char sigcode[], esigcode[];
331 #define	szsigcode	(esigcode - sigcode)
332 
333 	tf = p->p_md.md_tf;
334 	oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
335 	/*
336 	 * Compute new user stack addresses, subtract off
337 	 * one signal frame, and align.
338 	 */
339 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
340 	    (psp->ps_sigonstack & sigmask(sig))) {
341 		fp = (struct sigframe *)(psp->ps_sigstk.ss_base +
342 					 psp->ps_sigstk.ss_size);
343 		psp->ps_sigstk.ss_flags |= SA_ONSTACK;
344 	} else
345 		fp = (struct sigframe *)tf->tf_out[6];
346 	fp = (struct sigframe *)((int)(fp - 1) & ~7);
347 
348 #ifdef DEBUG
349 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
350 		printf("sendsig: %s[%d] sig %d newusp %x scp %x\n",
351 		    p->p_comm, p->p_pid, sig, fp, &fp->sf_sc);
352 #endif
353 	/*
354 	 * Now set up the signal frame.  We build it in kernel space
355 	 * and then copy it out.  We probably ought to just build it
356 	 * directly in user space....
357 	 */
358 	sf.sf_signo = sig;
359 	sf.sf_code = code;
360 #ifdef COMPAT_SUNOS
361 	sf.sf_scp = &fp->sf_sc;
362 #endif
363 	sf.sf_addr = 0;			/* XXX */
364 
365 	/*
366 	 * Build the signal context to be used by sigreturn.
367 	 */
368 	sf.sf_sc.sc_onstack = oonstack;
369 	sf.sf_sc.sc_mask = mask;
370 	sf.sf_sc.sc_sp = tf->tf_out[6];
371 	sf.sf_sc.sc_pc = tf->tf_pc;
372 	sf.sf_sc.sc_npc = tf->tf_npc;
373 	sf.sf_sc.sc_psr = tf->tf_psr;
374 	sf.sf_sc.sc_g1 = tf->tf_global[1];
375 	sf.sf_sc.sc_o0 = tf->tf_out[0];
376 
377 	/*
378 	 * Put the stack in a consistent state before we whack away
379 	 * at it.  Note that write_user_windows may just dump the
380 	 * registers into the pcb; we need them in the process's memory.
381 	 */
382 	write_user_windows();
383 	if (rwindow_save(p) || copyout((caddr_t)&sf, (caddr_t)fp, sizeof sf)) {
384 		/*
385 		 * Process has trashed its stack; give it an illegal
386 		 * instruction to halt it in its tracks.
387 		 */
388 #ifdef DEBUG
389 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
390 			printf("sendsig: window save or copyout error\n");
391 #endif
392 		sigexit(p, SIGILL);
393 		/* NOTREACHED */
394 	}
395 #ifdef DEBUG
396 	if (sigdebug & SDB_FOLLOW)
397 		printf("sendsig: %s[%d] sig %d scp %x\n",
398 		       p->p_comm, p->p_pid, sig, &fp->sf_sc);
399 #endif
400 	/*
401 	 * Arrange to continue execution at the code copied out in exec().
402 	 * It needs the function to call in %g1, and a new stack pointer.
403 	 */
404 #ifdef COMPAT_SUNOS
405 	if (psp->ps_usertramp & sigmask(sig)) {
406 		addr = (int)catcher;	/* user does his own trampolining */
407 	} else
408 #endif
409 	{
410 		addr = USRSTACK - sizeof(struct ps_strings) - szsigcode;
411 		tf->tf_global[1] = (int)catcher;
412 	}
413 	tf->tf_pc = addr;
414 	tf->tf_npc = addr + 4;
415 	tf->tf_out[6] = (int)fp - sizeof(struct rwindow);
416 #ifdef DEBUG
417 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
418 		printf("sendsig: about to return to catcher\n");
419 #endif
420 }
421 
422 /*
423  * System call to cleanup state after a signal
424  * has been taken.  Reset signal mask and
425  * stack state from context left by sendsig (above),
426  * and return to the given trap frame (if there is one).
427  * Check carefully to make sure that the user has not
428  * modified the state to gain improper privileges or to cause
429  * a machine fault.
430  */
431 /* ARGSUSED */
432 struct sigreturn_args {
433 	struct sigcontext *scp;
434 };
435 sigreturn(p, uap, retval)
436 	register struct proc *p;
437 	struct sigreturn_args *uap;
438 	int *retval;
439 {
440 	register struct sigcontext *scp;
441 	register struct trapframe *tf;
442 
443 	/* First ensure consistent stack state (see sendsig). */
444 	write_user_windows();
445 	if (rwindow_save(p))
446 		sigexit(p, SIGILL);
447 #ifdef DEBUG
448 	if (sigdebug & SDB_FOLLOW)
449 		printf("sigreturn: %s[%d], scp %x\n",
450 		    p->p_comm, p->p_pid, uap->scp);
451 #endif
452 	scp = uap->scp;
453 	if ((int)scp & 3 || useracc((caddr_t)scp, sizeof *scp, B_WRITE) == 0)
454 		return (EINVAL);
455 	tf = p->p_md.md_tf;
456 	/*
457 	 * Only the icc bits in the psr are used, so it need not be
458 	 * verified.  pc and npc must be multiples of 4.  This is all
459 	 * that is required; if it holds, just do it.
460 	 */
461 	if (((scp->sc_pc | scp->sc_npc) & 3) != 0)
462 		return (EINVAL);
463 	/* take only psr ICC field */
464 	tf->tf_psr = (tf->tf_psr & ~PSR_ICC) | (scp->sc_psr & PSR_ICC);
465 	tf->tf_pc = scp->sc_pc;
466 	tf->tf_npc = scp->sc_npc;
467 	tf->tf_global[1] = scp->sc_g1;
468 	tf->tf_out[0] = scp->sc_o0;
469 	tf->tf_out[6] = scp->sc_sp;
470 	if (scp->sc_onstack & 1)
471 		p->p_sigacts->ps_sigstk.ss_flags |= SA_ONSTACK;
472 	else
473 		p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
474 	p->p_sigmask = scp->sc_mask & ~sigcantmask;
475 	return (EJUSTRETURN);
476 }
477 
478 int	waittime = -1;
479 
480 boot(howto)
481 	register int howto;
482 {
483 	int i;
484 	static char str[4];	/* room for "-sd\0" */
485 	extern volatile void romhalt(void);
486 	extern volatile void romboot(char *);
487 
488 	fb_unblank();
489 	boothowto = howto;
490 	if ((howto & RB_NOSYNC) == 0 && waittime < 0 && rootfs) {
491 		register struct buf *bp;
492 		int iter, nbusy;
493 #if 1
494 		extern struct proc proc0;
495 
496 		/* protect against curproc->p_stats.foo refs in sync()   XXX */
497 		if (curproc == NULL)
498 			curproc = &proc0;
499 #endif
500 		waittime = 0;
501 		(void) spl0();
502 		printf("syncing disks... ");
503 		/*
504 		 * Release vnodes held by texts before sync.
505 		 */
506 		if (panicstr == 0)
507 			vnode_pager_umount((struct mount *)NULL);
508 #include "fd.h"
509 #if NFD > 0
510 		fdshutdown();
511 #endif
512 		sync((struct proc *)NULL, (void *)NULL, (int *)NULL);
513 
514 		for (iter = 0; iter < 20; iter++) {
515 			nbusy = 0;
516 			for (bp = &buf[nbuf]; --bp >= buf; )
517 				if ((bp->b_flags & (B_BUSY|B_INVAL)) == B_BUSY)
518 					nbusy++;
519 			if (nbusy == 0)
520 				break;
521 			printf("%d ", nbusy);
522 			DELAY(40000 * iter);
523 		}
524 		if (nbusy)
525 			printf("giving up\n");
526 		else
527 			printf("done\n");
528 		/*
529 		 * If we've been adjusting the clock, the todr
530 		 * will be out of synch; adjust it now.
531 		 */
532 		resettodr();
533 	}
534 	(void) splhigh();		/* ??? */
535 	if (howto & RB_HALT) {
536 		printf("halted\n\n");
537 		romhalt();
538 	}
539 	if (howto & RB_DUMP)
540 		dumpsys();
541 	printf("rebooting\n\n");
542 	i = 1;
543 	if (howto & RB_SINGLE)
544 		str[i++] = 's';
545 	if (howto & RB_KDB)
546 		str[i++] = 'd';
547 	if (i > 1) {
548 		str[0] = '-';
549 		str[i] = 0;
550 	} else
551 		str[0] = 0;
552 	romboot(str);
553 	/*NOTREACHED*/
554 }
555 
556 int	dumpmag = 0x8fca0101;	/* magic number for savecore */
557 int	dumpsize = 0;		/* also for savecore */
558 long	dumplo = 0;
559 
560 dumpconf()
561 {
562 	int nblks;
563 
564 	dumpsize = physmem;
565 #define DUMPMMU
566 #ifdef DUMPMMU
567 #define NPMEG 128
568 	/*
569 	 * savecore views the image in units of pages (i.e., dumpsize is in
570 	 * pages) so we round the two mmu entities into page-sized chunks.
571 	 * The PMEGs (32kB) and the segment table (512 bytes plus padding)
572 	 * are appending to the end of the crash dump.
573 	 */
574 	dumpsize += btoc(sizeof(((struct kpmap *)0)->pm_rsegmap)) +
575 		btoc(NPMEG * NPTESG * sizeof(int));
576 #endif
577 	if (dumpdev != NODEV && bdevsw[major(dumpdev)].d_psize) {
578 		nblks = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
579 		/*
580 		 * Don't dump on the first CLBYTES (why CLBYTES?)
581 		 * in case the dump device includes a disk label.
582 		 */
583 		if (dumplo < btodb(CLBYTES))
584 			dumplo = btodb(CLBYTES);
585 
586 		/*
587 		 * If dumpsize is too big for the partition, truncate it.
588 		 * Otherwise, put the dump at the end of the partition
589 		 * by making dumplo as large as possible.
590 		 */
591 		if (dumpsize > btoc(dbtob(nblks - dumplo)))
592 			dumpsize = btoc(dbtob(nblks - dumplo));
593 		else if (dumplo + ctod(dumpsize) > nblks)
594 			dumplo = nblks - ctod(dumpsize);
595 	}
596 }
597 
598 #ifdef DUMPMMU
599 /* XXX */
600 #include "ctlreg.h"
601 #define	getpte(va)		lda(va, ASI_PTE)
602 #define	setsegmap(va, pmeg)	stba(va, ASI_SEGMAP, pmeg)
603 
604 /*
605  * Write the mmu contents to the dump device.
606  * This gets appended to the end of a crash dump since
607  * there is no in-core copy of kernel memory mappings.
608  */
609 int
610 dumpmmu(blkno)
611 	register daddr_t blkno;
612 {
613 	register int (*dump)(/*dev_t, daddr_t, caddr_t, int*/);
614 	register int pmeg;
615 	register int addr;	/* unused kernel virtual address */
616 	register int i;
617 	register int *pte, *ptend;
618 	register int error;
619 	register struct kpmap *kpmap = &kernel_pmap_store;
620 	int buffer[dbtob(1) / sizeof(int)];
621 	extern int seginval;	/* from pmap.c */
622 
623 
624 	dump = bdevsw[major(dumpdev)].d_dump;
625 
626 	/*
627 	 * dump page table entries
628 	 *
629 	 * We dump each pmeg in order (by segment number).  Since the MMU
630 	 * automatically maps the given virtual segment to a pmeg we must
631 	 * iterate over the segments by incrementing an unused segment slot
632 	 * in the MMU.  This fixed segment number is used in the virtual
633 	 * address argument to getpte().
634 	 */
635 
636 	/* First find an unused virtual segment. */
637 	i = NKSEG;
638 	while (kpmap->pm_rsegmap[--i] != seginval)
639 		if (i <= 0)
640 			return (-1);
641 	/*
642 	 * Compute the base address corresponding to the unused segment.
643 	 * Note that the kernel segments start after all the user segments
644 	 * so we must account for this offset.
645 	 */
646 	addr = VSTOVA(i + NUSEG);
647 	/*
648 	 * Go through the pmegs and dump each one.
649 	 */
650 	pte = buffer;
651 	ptend = &buffer[sizeof(buffer) / sizeof(buffer[0])];
652 	for (pmeg = 0; pmeg < NPMEG; ++pmeg) {
653 		register int va = addr;
654 
655 		setsegmap(addr, pmeg);
656 		i = NPTESG;
657 		do {
658 			*pte++ = getpte(va);
659 			if (pte >= ptend) {
660 				/*
661 				 * Note that we'll dump the last block
662 				 * the last time through the loops because
663 				 * all the PMEGs occupy 32KB which is
664 				 * a multiple of the block size.
665 				 */
666 				error = (*dump)(dumpdev, blkno,
667 						(caddr_t)buffer,
668 						dbtob(1));
669 				if (error != 0)
670 					return (error);
671 				++blkno;
672 				pte = buffer;
673 			}
674 			va += NBPG;
675 		} while (--i > 0);
676 	}
677 	setsegmap(addr, seginval);
678 
679 	/*
680 	 * dump (512 byte) segment map
681 	 * XXX assume it's a multiple of the block size
682 	 */
683 	error = (*dump)(dumpdev, blkno, (caddr_t)kpmap->pm_rsegmap,
684 			sizeof(kpmap->pm_rsegmap), 0);
685 	return (error);
686 }
687 #endif
688 
689 #define	BYTES_PER_DUMP	(32 * 1024)	/* must be a multiple of pagesize */
690 static vm_offset_t dumpspace;
691 
692 caddr_t
693 reserve_dumppages(p)
694 	caddr_t p;
695 {
696 
697 	dumpspace = (vm_offset_t)p;
698 	return (p + BYTES_PER_DUMP);
699 }
700 
701 /*
702  * Write a crash dump.
703  */
704 dumpsys()
705 {
706 	register unsigned bytes, i, n;
707 	register int maddr, psize;
708 	register daddr_t blkno;
709 	register int (*dump)(/*dev_t, daddr_t, caddr_t, int, int*/);
710 	int error = 0;
711 
712 	if (dumpdev == NODEV)
713 		return;
714 	/* copy registers to memory */
715 	snapshot(cpcb);
716 	/*
717 	 * For dumps during autoconfiguration,
718 	 * if dump device has already configured...
719 	 */
720 	if (dumpsize == 0)
721 		dumpconf();
722 	if (dumplo < 0)
723 		return;
724 	printf("\ndumping to dev %x, offset %d\n", dumpdev, dumplo);
725 
726 	psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
727 	printf("dump ");
728 	if (psize == -1) {
729 		printf("area unavailable\n");
730 		return;
731 	}
732 	bytes = physmem << PGSHIFT;
733 	maddr = 0;
734 	blkno = dumplo;
735 	dump = bdevsw[major(dumpdev)].d_dump;
736 	for (i = 0; i < bytes; i += n) {
737 		n = bytes - i;
738 		if (n > BYTES_PER_DUMP)
739 			 n = BYTES_PER_DUMP;
740 #ifdef DEBUG
741 		/* print out how many MBs we have dumped */
742 		if (i && (i % (1024*1024)) == 0)
743 			printf("%d ", i / (1024*1024));
744 #endif
745 		(void) pmap_map(dumpspace, maddr, maddr + n, VM_PROT_READ);
746 		error = (*dump)(dumpdev, blkno, (caddr_t)dumpspace, (int)n);
747 		if (error)
748 			break;
749 		maddr += n;
750 		blkno += btodb(n);
751 	}
752 #ifdef DUMPMMU
753 	if (!error)
754 		error = dumpmmu(blkno);
755 #endif
756 	switch (error) {
757 
758 	case ENXIO:
759 		printf("device bad\n");
760 		break;
761 
762 	case EFAULT:
763 		printf("device not ready\n");
764 		break;
765 
766 	case EINVAL:
767 		printf("area improper\n");
768 		break;
769 
770 	case EIO:
771 		printf("i/o error\n");
772 		break;
773 
774 	case 0:
775 		printf("succeeded\n");
776 		break;
777 
778 	default:
779 		printf("error %d\n", error);
780 		break;
781 	}
782 }
783 
784 /*
785  * Map an I/O device given physical address and size in bytes, e.g.,
786  *
787  *	mydev = (struct mydev *)mapdev(myioaddr, 0, sizeof(struct mydev));
788  *
789  * See also machine/autoconf.h.
790  */
791 void *
792 mapdev(phys, virt, size)
793 	register void *phys;
794 	register int virt, size;
795 {
796 	register vm_offset_t v;
797 	register void *ret;
798 	static vm_offset_t iobase = IODEV_BASE;
799 
800 	size = round_page(size);
801 	if (virt)
802 		v = virt;
803 	else {
804 		v = iobase;
805 		iobase += size;
806 		if (iobase > IODEV_END)	/* unlikely */
807 			panic("mapiodev");
808 	}
809 	ret = (void *)v;
810 	do {
811 		pmap_enter(kernel_pmap, v,
812 		    (vm_offset_t)phys | PMAP_OBIO | PMAP_NC,
813 		    VM_PROT_READ | VM_PROT_WRITE, 1);
814 		v += PAGE_SIZE;
815 		phys += PAGE_SIZE;
816 	} while ((size -= PAGE_SIZE) > 0);
817 	return (ret);
818 }
819