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