xref: /netbsd-src/sys/uvm/uvm_glue.c (revision 37b34d511dea595d3ba03a661cf3b775038ea5f8)
1 /*	$NetBSD: uvm_glue.c,v 1.60 2002/09/22 07:20:32 chs Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * Copyright (c) 1991, 1993, The Regents of the University of California.
6  *
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * The Mach Operating System project at Carnegie-Mellon University.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by Charles D. Cranor,
23  *      Washington University, the University of California, Berkeley and
24  *      its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)vm_glue.c	8.6 (Berkeley) 1/5/94
42  * from: Id: uvm_glue.c,v 1.1.2.8 1998/02/07 01:16:54 chs Exp
43  *
44  *
45  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
46  * All rights reserved.
47  *
48  * Permission to use, copy, modify and distribute this software and
49  * its documentation is hereby granted, provided that both the copyright
50  * notice and this permission notice appear in all copies of the
51  * software, derivative works or modified versions, and any portions
52  * thereof, and that both notices appear in supporting documentation.
53  *
54  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
55  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
56  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
57  *
58  * Carnegie Mellon requests users of this software to return to
59  *
60  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
61  *  School of Computer Science
62  *  Carnegie Mellon University
63  *  Pittsburgh PA 15213-3890
64  *
65  * any improvements or extensions that they make and grant Carnegie the
66  * rights to redistribute these changes.
67  */
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.60 2002/09/22 07:20:32 chs Exp $");
71 
72 #include "opt_kgdb.h"
73 #include "opt_kstack.h"
74 #include "opt_sysv.h"
75 #include "opt_uvmhist.h"
76 
77 /*
78  * uvm_glue.c: glue functions
79  */
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/proc.h>
84 #include <sys/resourcevar.h>
85 #include <sys/buf.h>
86 #include <sys/user.h>
87 #ifdef SYSVSHM
88 #include <sys/shm.h>
89 #endif
90 
91 #include <uvm/uvm.h>
92 
93 #include <machine/cpu.h>
94 
95 /*
96  * local prototypes
97  */
98 
99 static void uvm_swapout __P((struct proc *));
100 
101 #define UVM_NUAREA_MAX 16
102 void *uvm_uareas;
103 int uvm_nuarea;
104 
105 /*
106  * XXXCDC: do these really belong here?
107  */
108 
109 int readbuffers = 0;		/* allow KGDB to read kern buffer pool */
110 				/* XXX: see uvm_kernacc */
111 
112 
113 /*
114  * uvm_kernacc: can the kernel access a region of memory
115  *
116  * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c)
117  */
118 
119 boolean_t
120 uvm_kernacc(addr, len, rw)
121 	caddr_t addr;
122 	size_t len;
123 	int rw;
124 {
125 	boolean_t rv;
126 	vaddr_t saddr, eaddr;
127 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
128 
129 	saddr = trunc_page((vaddr_t)addr);
130 	eaddr = round_page((vaddr_t)addr + len);
131 	vm_map_lock_read(kernel_map);
132 	rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
133 	vm_map_unlock_read(kernel_map);
134 
135 	/*
136 	 * XXX there are still some things (e.g. the buffer cache) that
137 	 * are managed behind the VM system's back so even though an
138 	 * address is accessible in the mind of the VM system, there may
139 	 * not be physical pages where the VM thinks there is.  This can
140 	 * lead to bogus allocation of pages in the kernel address space
141 	 * or worse, inconsistencies at the pmap level.  We only worry
142 	 * about the buffer cache for now.
143 	 */
144 	if (!readbuffers && rv && (eaddr > (vaddr_t)buffers &&
145 			     saddr < (vaddr_t)buffers + MAXBSIZE * nbuf))
146 		rv = FALSE;
147 	return(rv);
148 }
149 
150 /*
151  * uvm_useracc: can the user access it?
152  *
153  * - called from physio() and sys___sysctl().
154  */
155 
156 boolean_t
157 uvm_useracc(addr, len, rw)
158 	caddr_t addr;
159 	size_t len;
160 	int rw;
161 {
162 	struct vm_map *map;
163 	boolean_t rv;
164 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
165 
166 	/* XXX curproc */
167 	map = &curproc->p_vmspace->vm_map;
168 
169 	vm_map_lock_read(map);
170 	rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr),
171 	    round_page((vaddr_t)addr + len), prot);
172 	vm_map_unlock_read(map);
173 
174 	return(rv);
175 }
176 
177 #ifdef KGDB
178 /*
179  * Change protections on kernel pages from addr to addr+len
180  * (presumably so debugger can plant a breakpoint).
181  *
182  * We force the protection change at the pmap level.  If we were
183  * to use vm_map_protect a change to allow writing would be lazily-
184  * applied meaning we would still take a protection fault, something
185  * we really don't want to do.  It would also fragment the kernel
186  * map unnecessarily.  We cannot use pmap_protect since it also won't
187  * enforce a write-enable request.  Using pmap_enter is the only way
188  * we can ensure the change takes place properly.
189  */
190 void
191 uvm_chgkprot(addr, len, rw)
192 	caddr_t addr;
193 	size_t len;
194 	int rw;
195 {
196 	vm_prot_t prot;
197 	paddr_t pa;
198 	vaddr_t sva, eva;
199 
200 	prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
201 	eva = round_page((vaddr_t)addr + len);
202 	for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
203 		/*
204 		 * Extract physical address for the page.
205 		 */
206 		if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE)
207 			panic("chgkprot: invalid page");
208 		pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
209 	}
210 	pmap_update(pmap_kernel());
211 }
212 #endif
213 
214 /*
215  * uvm_vslock: wire user memory for I/O
216  *
217  * - called from physio and sys___sysctl
218  * - XXXCDC: consider nuking this (or making it a macro?)
219  */
220 
221 int
222 uvm_vslock(p, addr, len, access_type)
223 	struct proc *p;
224 	caddr_t	addr;
225 	size_t	len;
226 	vm_prot_t access_type;
227 {
228 	struct vm_map *map;
229 	vaddr_t start, end;
230 	int error;
231 
232 	map = &p->p_vmspace->vm_map;
233 	start = trunc_page((vaddr_t)addr);
234 	end = round_page((vaddr_t)addr + len);
235 	error = uvm_fault_wire(map, start, end, VM_FAULT_WIRE, access_type);
236 	return error;
237 }
238 
239 /*
240  * uvm_vsunlock: unwire user memory wired by uvm_vslock()
241  *
242  * - called from physio and sys___sysctl
243  * - XXXCDC: consider nuking this (or making it a macro?)
244  */
245 
246 void
247 uvm_vsunlock(p, addr, len)
248 	struct proc *p;
249 	caddr_t	addr;
250 	size_t	len;
251 {
252 	uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr),
253 		round_page((vaddr_t)addr + len));
254 }
255 
256 /*
257  * uvm_fork: fork a virtual address space
258  *
259  * - the address space is copied as per parent map's inherit values
260  * - a new "user" structure is allocated for the child process
261  *	[filled in by MD layer...]
262  * - if specified, the child gets a new user stack described by
263  *	stack and stacksize
264  * - NOTE: the kernel stack may be at a different location in the child
265  *	process, and thus addresses of automatic variables may be invalid
266  *	after cpu_fork returns in the child process.  We do nothing here
267  *	after cpu_fork returns.
268  * - XXXCDC: we need a way for this to return a failure value rather
269  *   than just hang
270  */
271 void
272 uvm_fork(p1, p2, shared, stack, stacksize, func, arg)
273 	struct proc *p1, *p2;
274 	boolean_t shared;
275 	void *stack;
276 	size_t stacksize;
277 	void (*func) __P((void *));
278 	void *arg;
279 {
280 	struct user *up = p2->p_addr;
281 	int error;
282 
283 	if (shared == TRUE) {
284 		p2->p_vmspace = NULL;
285 		uvmspace_share(p1, p2);
286 	} else
287 		p2->p_vmspace = uvmspace_fork(p1->p_vmspace);
288 
289 	/*
290 	 * Wire down the U-area for the process, which contains the PCB
291 	 * and the kernel stack.  Wired state is stored in p->p_flag's
292 	 * P_INMEM bit rather than in the vm_map_entry's wired count
293 	 * to prevent kernel_map fragmentation.
294 	 *
295 	 * Note the kernel stack gets read/write accesses right off
296 	 * the bat.
297 	 */
298 	error = uvm_fault_wire(kernel_map, (vaddr_t)up, (vaddr_t)up + USPACE,
299 	    VM_FAULT_WIRE, VM_PROT_READ | VM_PROT_WRITE);
300 	if (error)
301 		panic("uvm_fork: uvm_fault_wire failed: %d", error);
302 
303 #ifdef KSTACK_CHECK_MAGIC
304 	/*
305 	 * fill stack with magic number
306 	 */
307 	kstack_setup_magic(p2);
308 #endif
309 
310 	/*
311 	 * p_stats currently points at a field in the user struct.  Copy
312 	 * parts of p_stats, and zero out the rest.
313 	 */
314 	p2->p_stats = &up->u_stats;
315 	memset(&up->u_stats.pstat_startzero, 0,
316 	       ((caddr_t)&up->u_stats.pstat_endzero -
317 		(caddr_t)&up->u_stats.pstat_startzero));
318 	memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy,
319 	       ((caddr_t)&up->u_stats.pstat_endcopy -
320 		(caddr_t)&up->u_stats.pstat_startcopy));
321 
322 	/*
323 	 * cpu_fork() copy and update the pcb, and make the child ready
324 	 * to run.  If this is a normal user fork, the child will exit
325 	 * directly to user mode via child_return() on its first time
326 	 * slice and will not return here.  If this is a kernel thread,
327 	 * the specified entry point will be executed.
328 	 */
329 	cpu_fork(p1, p2, stack, stacksize, func, arg);
330 }
331 
332 /*
333  * uvm_exit: exit a virtual address space
334  *
335  * - the process passed to us is a dead (pre-zombie) process; we
336  *   are running on a different context now (the reaper).
337  * - we must run in a separate thread because freeing the vmspace
338  *   of the dead process may block.
339  */
340 
341 void
342 uvm_exit(p)
343 	struct proc *p;
344 {
345 	vaddr_t va = (vaddr_t)p->p_addr;
346 
347 	uvmspace_free(p->p_vmspace);
348 	p->p_flag &= ~P_INMEM;
349 	uvm_uarea_free(va);
350 	p->p_addr = NULL;
351 }
352 
353 /*
354  * uvm_uarea_alloc: allocate a u-area
355  */
356 
357 vaddr_t
358 uvm_uarea_alloc(void)
359 {
360 	vaddr_t uaddr;
361 
362 #ifndef USPACE_ALIGN
363 #define USPACE_ALIGN    0
364 #endif
365 
366 	uaddr = (vaddr_t)uvm_uareas;
367 	if (uaddr) {
368 		uvm_uareas = *(void **)uvm_uareas;
369 		uvm_nuarea--;
370 	} else {
371 		uaddr = uvm_km_valloc_align(kernel_map, USPACE, USPACE_ALIGN);
372 	}
373 	return uaddr;
374 }
375 
376 /*
377  * uvm_uarea_free: free a u-area
378  */
379 
380 void
381 uvm_uarea_free(vaddr_t uaddr)
382 {
383 
384 	if (uvm_nuarea < UVM_NUAREA_MAX) {
385 		*(void **)uaddr = uvm_uareas;
386 		uvm_uareas = (void *)uaddr;
387 		uvm_nuarea++;
388 	} else {
389 		uvm_km_free(kernel_map, uaddr, USPACE);
390 	}
391 }
392 
393 /*
394  * uvm_init_limit: init per-process VM limits
395  *
396  * - called for process 0 and then inherited by all others.
397  */
398 
399 void
400 uvm_init_limits(p)
401 	struct proc *p;
402 {
403 
404 	/*
405 	 * Set up the initial limits on process VM.  Set the maximum
406 	 * resident set size to be all of (reasonably) available memory.
407 	 * This causes any single, large process to start random page
408 	 * replacement once it fills memory.
409 	 */
410 
411 	p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
412 	p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
413 	p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
414 	p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
415 	p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
416 }
417 
418 #ifdef DEBUG
419 int	enableswap = 1;
420 int	swapdebug = 0;
421 #define	SDB_FOLLOW	1
422 #define SDB_SWAPIN	2
423 #define SDB_SWAPOUT	4
424 #endif
425 
426 /*
427  * uvm_swapin: swap in a process's u-area.
428  */
429 
430 void
431 uvm_swapin(p)
432 	struct proc *p;
433 {
434 	vaddr_t addr;
435 	int s, error;
436 
437 	addr = (vaddr_t)p->p_addr;
438 	/* make P_INMEM true */
439 	error = uvm_fault_wire(kernel_map, addr, addr + USPACE, VM_FAULT_WIRE,
440 	    VM_PROT_READ | VM_PROT_WRITE);
441 	if (error) {
442 		panic("uvm_swapin: rewiring stack failed: %d", error);
443 	}
444 
445 	/*
446 	 * Some architectures need to be notified when the user area has
447 	 * moved to new physical page(s) (e.g.  see mips/mips/vm_machdep.c).
448 	 */
449 	cpu_swapin(p);
450 	SCHED_LOCK(s);
451 	if (p->p_stat == SRUN)
452 		setrunqueue(p);
453 	p->p_flag |= P_INMEM;
454 	SCHED_UNLOCK(s);
455 	p->p_swtime = 0;
456 	++uvmexp.swapins;
457 }
458 
459 /*
460  * uvm_scheduler: process zero main loop
461  *
462  * - attempt to swapin every swaped-out, runnable process in order of
463  *	priority.
464  * - if not enough memory, wake the pagedaemon and let it clear space.
465  */
466 
467 void
468 uvm_scheduler()
469 {
470 	struct proc *p;
471 	int pri;
472 	struct proc *pp;
473 	int ppri;
474 
475 loop:
476 #ifdef DEBUG
477 	while (!enableswap)
478 		tsleep(&proc0, PVM, "noswap", 0);
479 #endif
480 	pp = NULL;		/* process to choose */
481 	ppri = INT_MIN;	/* its priority */
482 	proclist_lock_read();
483 	LIST_FOREACH(p, &allproc, p_list) {
484 
485 		/* is it a runnable swapped out process? */
486 		if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) {
487 			pri = p->p_swtime + p->p_slptime -
488 			    (p->p_nice - NZERO) * 8;
489 			if (pri > ppri) {   /* higher priority?  remember it. */
490 				pp = p;
491 				ppri = pri;
492 			}
493 		}
494 	}
495 	/*
496 	 * XXXSMP: possible unlock/sleep race between here and the
497 	 * "scheduler" tsleep below..
498 	 */
499 	proclist_unlock_read();
500 
501 #ifdef DEBUG
502 	if (swapdebug & SDB_FOLLOW)
503 		printf("scheduler: running, procp %p pri %d\n", pp, ppri);
504 #endif
505 	/*
506 	 * Nothing to do, back to sleep
507 	 */
508 	if ((p = pp) == NULL) {
509 		tsleep(&proc0, PVM, "scheduler", 0);
510 		goto loop;
511 	}
512 
513 	/*
514 	 * we have found swapped out process which we would like to bring
515 	 * back in.
516 	 *
517 	 * XXX: this part is really bogus cuz we could deadlock on memory
518 	 * despite our feeble check
519 	 */
520 	if (uvmexp.free > atop(USPACE)) {
521 #ifdef DEBUG
522 		if (swapdebug & SDB_SWAPIN)
523 			printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
524 	     p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free);
525 #endif
526 		uvm_swapin(p);
527 		goto loop;
528 	}
529 	/*
530 	 * not enough memory, jab the pageout daemon and wait til the coast
531 	 * is clear
532 	 */
533 #ifdef DEBUG
534 	if (swapdebug & SDB_FOLLOW)
535 		printf("scheduler: no room for pid %d(%s), free %d\n",
536 	   p->p_pid, p->p_comm, uvmexp.free);
537 #endif
538 	uvm_wait("schedpwait");
539 #ifdef DEBUG
540 	if (swapdebug & SDB_FOLLOW)
541 		printf("scheduler: room again, free %d\n", uvmexp.free);
542 #endif
543 	goto loop;
544 }
545 
546 /*
547  * swappable: is process "p" swappable?
548  */
549 
550 #define	swappable(p)							\
551 	(((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM &&	\
552 	 (p)->p_holdcnt == 0)
553 
554 /*
555  * swapout_threads: find threads that can be swapped and unwire their
556  *	u-areas.
557  *
558  * - called by the pagedaemon
559  * - try and swap at least one processs
560  * - processes that are sleeping or stopped for maxslp or more seconds
561  *   are swapped... otherwise the longest-sleeping or stopped process
562  *   is swapped, otherwise the longest resident process...
563  */
564 
565 void
566 uvm_swapout_threads()
567 {
568 	struct proc *p;
569 	struct proc *outp, *outp2;
570 	int outpri, outpri2;
571 	int didswap = 0;
572 	extern int maxslp;
573 	/* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
574 
575 #ifdef DEBUG
576 	if (!enableswap)
577 		return;
578 #endif
579 
580 	/*
581 	 * outp/outpri  : stop/sleep process with largest sleeptime < maxslp
582 	 * outp2/outpri2: the longest resident process (its swap time)
583 	 */
584 	outp = outp2 = NULL;
585 	outpri = outpri2 = 0;
586 	proclist_lock_read();
587 	LIST_FOREACH(p, &allproc, p_list) {
588 		if (!swappable(p))
589 			continue;
590 		switch (p->p_stat) {
591 		case SRUN:
592 		case SONPROC:
593 			if (p->p_swtime > outpri2) {
594 				outp2 = p;
595 				outpri2 = p->p_swtime;
596 			}
597 			continue;
598 
599 		case SSLEEP:
600 		case SSTOP:
601 			if (p->p_slptime >= maxslp) {
602 				uvm_swapout(p);
603 				didswap++;
604 			} else if (p->p_slptime > outpri) {
605 				outp = p;
606 				outpri = p->p_slptime;
607 			}
608 			continue;
609 		}
610 	}
611 	proclist_unlock_read();
612 
613 	/*
614 	 * If we didn't get rid of any real duds, toss out the next most
615 	 * likely sleeping/stopped or running candidate.  We only do this
616 	 * if we are real low on memory since we don't gain much by doing
617 	 * it (USPACE bytes).
618 	 */
619 	if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
620 		if ((p = outp) == NULL)
621 			p = outp2;
622 #ifdef DEBUG
623 		if (swapdebug & SDB_SWAPOUT)
624 			printf("swapout_threads: no duds, try procp %p\n", p);
625 #endif
626 		if (p)
627 			uvm_swapout(p);
628 	}
629 }
630 
631 /*
632  * uvm_swapout: swap out process "p"
633  *
634  * - currently "swapout" means "unwire U-area" and "pmap_collect()"
635  *   the pmap.
636  * - XXXCDC: should deactivate all process' private anonymous memory
637  */
638 
639 static void
640 uvm_swapout(p)
641 	struct proc *p;
642 {
643 	vaddr_t addr;
644 	int s;
645 
646 #ifdef DEBUG
647 	if (swapdebug & SDB_SWAPOUT)
648 		printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n",
649 	   p->p_pid, p->p_comm, p->p_addr, p->p_stat,
650 	   p->p_slptime, uvmexp.free);
651 #endif
652 
653 	/*
654 	 * Do any machine-specific actions necessary before swapout.
655 	 * This can include saving floating point state, etc.
656 	 */
657 	cpu_swapout(p);
658 
659 	/*
660 	 * Mark it as (potentially) swapped out.
661 	 */
662 	SCHED_LOCK(s);
663 	p->p_flag &= ~P_INMEM;
664 	if (p->p_stat == SRUN)
665 		remrunqueue(p);
666 	SCHED_UNLOCK(s);
667 	p->p_swtime = 0;
668 	p->p_stats->p_ru.ru_nswap++;
669 	++uvmexp.swapouts;
670 
671 	/*
672 	 * Unwire the to-be-swapped process's user struct and kernel stack.
673 	 */
674 	addr = (vaddr_t)p->p_addr;
675 	uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */
676 	pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
677 }
678 
679 /*
680  * uvm_coredump_walkmap: walk a process's map for the purpose of dumping
681  * a core file.
682  */
683 
684 int
685 uvm_coredump_walkmap(p, vp, cred, func, cookie)
686 	struct proc *p;
687 	struct vnode *vp;
688 	struct ucred *cred;
689 	int (*func)(struct proc *, struct vnode *, struct ucred *,
690 	    struct uvm_coredump_state *);
691 	void *cookie;
692 {
693 	struct uvm_coredump_state state;
694 	struct vmspace *vm = p->p_vmspace;
695 	struct vm_map *map = &vm->vm_map;
696 	struct vm_map_entry *entry;
697 	vaddr_t maxstack;
698 	int error;
699 
700 	maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
701 
702 	for (entry = map->header.next; entry != &map->header;
703 	     entry = entry->next) {
704 		/* Should never happen for a user process. */
705 		if (UVM_ET_ISSUBMAP(entry))
706 			panic("uvm_coredump_walkmap: user process with "
707 			    "submap?");
708 
709 		state.cookie = cookie;
710 		state.start = entry->start;
711 		state.end = entry->end;
712 		state.prot = entry->protection;
713 		state.flags = 0;
714 
715 		if (state.start >= VM_MAXUSER_ADDRESS)
716 			continue;
717 
718 		if (state.end > VM_MAXUSER_ADDRESS)
719 			state.end = VM_MAXUSER_ADDRESS;
720 
721 		if (state.start >= (vaddr_t)vm->vm_maxsaddr) {
722 			if (state.end <= maxstack)
723 				continue;
724 			if (state.start < maxstack)
725 				state.start = maxstack;
726 			state.flags |= UVM_COREDUMP_STACK;
727 		}
728 
729 		if ((entry->protection & VM_PROT_WRITE) == 0)
730 			state.flags |= UVM_COREDUMP_NODUMP;
731 
732 		if (entry->object.uvm_obj != NULL &&
733 		    entry->object.uvm_obj->pgops == &uvm_deviceops)
734 			state.flags |= UVM_COREDUMP_NODUMP;
735 
736 		error = (*func)(p, vp, cred, &state);
737 		if (error)
738 			return (error);
739 	}
740 
741 	return (0);
742 }
743