Home | History | Annotate | Line # | Download | only in uvm
uvm_glue.c revision 1.61
      1 /*	$NetBSD: uvm_glue.c,v 1.61 2002/11/17 08:32:45 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 (at) 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.61 2002/11/17 08:32:45 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.  If we reused a cached U-area,
    294 	 * P_INMEM will already be set and we don't need to do anything.
    295 	 *
    296 	 * Note the kernel stack gets read/write accesses right off the bat.
    297 	 */
    298 
    299 	if ((p2->p_flag & P_INMEM) == 0) {
    300 		error = uvm_fault_wire(kernel_map, (vaddr_t)up,
    301 		    (vaddr_t)up + USPACE, VM_FAULT_WIRE,
    302 		    VM_PROT_READ | VM_PROT_WRITE);
    303 		if (error)
    304 			panic("uvm_fork: uvm_fault_wire failed: %d", error);
    305 		p2->p_flag |= P_INMEM;
    306 	}
    307 
    308 #ifdef KSTACK_CHECK_MAGIC
    309 	/*
    310 	 * fill stack with magic number
    311 	 */
    312 	kstack_setup_magic(p2);
    313 #endif
    314 
    315 	/*
    316 	 * p_stats currently points at a field in the user struct.  Copy
    317 	 * parts of p_stats, and zero out the rest.
    318 	 */
    319 	p2->p_stats = &up->u_stats;
    320 	memset(&up->u_stats.pstat_startzero, 0,
    321 	       ((caddr_t)&up->u_stats.pstat_endzero -
    322 		(caddr_t)&up->u_stats.pstat_startzero));
    323 	memcpy(&up->u_stats.pstat_startcopy, &p1->p_stats->pstat_startcopy,
    324 	       ((caddr_t)&up->u_stats.pstat_endcopy -
    325 		(caddr_t)&up->u_stats.pstat_startcopy));
    326 
    327 	/*
    328 	 * cpu_fork() copy and update the pcb, and make the child ready
    329 	 * to run.  If this is a normal user fork, the child will exit
    330 	 * directly to user mode via child_return() on its first time
    331 	 * slice and will not return here.  If this is a kernel thread,
    332 	 * the specified entry point will be executed.
    333 	 */
    334 	cpu_fork(p1, p2, stack, stacksize, func, arg);
    335 }
    336 
    337 /*
    338  * uvm_exit: exit a virtual address space
    339  *
    340  * - the process passed to us is a dead (pre-zombie) process; we
    341  *   are running on a different context now (the reaper).
    342  * - we must run in a separate thread because freeing the vmspace
    343  *   of the dead process may block.
    344  */
    345 
    346 void
    347 uvm_exit(p)
    348 	struct proc *p;
    349 {
    350 	vaddr_t va = (vaddr_t)p->p_addr;
    351 
    352 	uvmspace_free(p->p_vmspace);
    353 	p->p_flag &= ~P_INMEM;
    354 	uvm_uarea_free(va);
    355 	p->p_addr = NULL;
    356 }
    357 
    358 /*
    359  * uvm_uarea_alloc: allocate a u-area
    360  */
    361 
    362 boolean_t
    363 uvm_uarea_alloc(vaddr_t *uaddrp)
    364 {
    365 	vaddr_t uaddr;
    366 
    367 #ifndef USPACE_ALIGN
    368 #define USPACE_ALIGN    0
    369 #endif
    370 
    371 	uaddr = (vaddr_t)uvm_uareas;
    372 	if (uaddr) {
    373 		uvm_uareas = *(void **)uvm_uareas;
    374 		uvm_nuarea--;
    375 		*uaddrp = uaddr;
    376 		return TRUE;
    377 	} else {
    378 		*uaddrp = uvm_km_valloc_align(kernel_map, USPACE, USPACE_ALIGN);
    379 		return FALSE;
    380 	}
    381 }
    382 
    383 /*
    384  * uvm_uarea_free: free a u-area
    385  */
    386 
    387 void
    388 uvm_uarea_free(vaddr_t uaddr)
    389 {
    390 
    391 	if (uvm_nuarea < UVM_NUAREA_MAX) {
    392 		*(void **)uaddr = uvm_uareas;
    393 		uvm_uareas = (void *)uaddr;
    394 		uvm_nuarea++;
    395 	} else {
    396 		uvm_km_free(kernel_map, uaddr, USPACE);
    397 	}
    398 }
    399 
    400 /*
    401  * uvm_init_limit: init per-process VM limits
    402  *
    403  * - called for process 0 and then inherited by all others.
    404  */
    405 
    406 void
    407 uvm_init_limits(p)
    408 	struct proc *p;
    409 {
    410 
    411 	/*
    412 	 * Set up the initial limits on process VM.  Set the maximum
    413 	 * resident set size to be all of (reasonably) available memory.
    414 	 * This causes any single, large process to start random page
    415 	 * replacement once it fills memory.
    416 	 */
    417 
    418 	p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
    419 	p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
    420 	p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
    421 	p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
    422 	p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
    423 }
    424 
    425 #ifdef DEBUG
    426 int	enableswap = 1;
    427 int	swapdebug = 0;
    428 #define	SDB_FOLLOW	1
    429 #define SDB_SWAPIN	2
    430 #define SDB_SWAPOUT	4
    431 #endif
    432 
    433 /*
    434  * uvm_swapin: swap in a process's u-area.
    435  */
    436 
    437 void
    438 uvm_swapin(p)
    439 	struct proc *p;
    440 {
    441 	vaddr_t addr;
    442 	int s, error;
    443 
    444 	addr = (vaddr_t)p->p_addr;
    445 	/* make P_INMEM true */
    446 	error = uvm_fault_wire(kernel_map, addr, addr + USPACE, VM_FAULT_WIRE,
    447 	    VM_PROT_READ | VM_PROT_WRITE);
    448 	if (error) {
    449 		panic("uvm_swapin: rewiring stack failed: %d", error);
    450 	}
    451 
    452 	/*
    453 	 * Some architectures need to be notified when the user area has
    454 	 * moved to new physical page(s) (e.g.  see mips/mips/vm_machdep.c).
    455 	 */
    456 	cpu_swapin(p);
    457 	SCHED_LOCK(s);
    458 	if (p->p_stat == SRUN)
    459 		setrunqueue(p);
    460 	p->p_flag |= P_INMEM;
    461 	SCHED_UNLOCK(s);
    462 	p->p_swtime = 0;
    463 	++uvmexp.swapins;
    464 }
    465 
    466 /*
    467  * uvm_scheduler: process zero main loop
    468  *
    469  * - attempt to swapin every swaped-out, runnable process in order of
    470  *	priority.
    471  * - if not enough memory, wake the pagedaemon and let it clear space.
    472  */
    473 
    474 void
    475 uvm_scheduler()
    476 {
    477 	struct proc *p;
    478 	int pri;
    479 	struct proc *pp;
    480 	int ppri;
    481 
    482 loop:
    483 #ifdef DEBUG
    484 	while (!enableswap)
    485 		tsleep(&proc0, PVM, "noswap", 0);
    486 #endif
    487 	pp = NULL;		/* process to choose */
    488 	ppri = INT_MIN;	/* its priority */
    489 	proclist_lock_read();
    490 	LIST_FOREACH(p, &allproc, p_list) {
    491 
    492 		/* is it a runnable swapped out process? */
    493 		if (p->p_stat == SRUN && (p->p_flag & P_INMEM) == 0) {
    494 			pri = p->p_swtime + p->p_slptime -
    495 			    (p->p_nice - NZERO) * 8;
    496 			if (pri > ppri) {   /* higher priority?  remember it. */
    497 				pp = p;
    498 				ppri = pri;
    499 			}
    500 		}
    501 	}
    502 	/*
    503 	 * XXXSMP: possible unlock/sleep race between here and the
    504 	 * "scheduler" tsleep below..
    505 	 */
    506 	proclist_unlock_read();
    507 
    508 #ifdef DEBUG
    509 	if (swapdebug & SDB_FOLLOW)
    510 		printf("scheduler: running, procp %p pri %d\n", pp, ppri);
    511 #endif
    512 	/*
    513 	 * Nothing to do, back to sleep
    514 	 */
    515 	if ((p = pp) == NULL) {
    516 		tsleep(&proc0, PVM, "scheduler", 0);
    517 		goto loop;
    518 	}
    519 
    520 	/*
    521 	 * we have found swapped out process which we would like to bring
    522 	 * back in.
    523 	 *
    524 	 * XXX: this part is really bogus cuz we could deadlock on memory
    525 	 * despite our feeble check
    526 	 */
    527 	if (uvmexp.free > atop(USPACE)) {
    528 #ifdef DEBUG
    529 		if (swapdebug & SDB_SWAPIN)
    530 			printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
    531 	     p->p_pid, p->p_comm, p->p_addr, ppri, uvmexp.free);
    532 #endif
    533 		uvm_swapin(p);
    534 		goto loop;
    535 	}
    536 	/*
    537 	 * not enough memory, jab the pageout daemon and wait til the coast
    538 	 * is clear
    539 	 */
    540 #ifdef DEBUG
    541 	if (swapdebug & SDB_FOLLOW)
    542 		printf("scheduler: no room for pid %d(%s), free %d\n",
    543 	   p->p_pid, p->p_comm, uvmexp.free);
    544 #endif
    545 	uvm_wait("schedpwait");
    546 #ifdef DEBUG
    547 	if (swapdebug & SDB_FOLLOW)
    548 		printf("scheduler: room again, free %d\n", uvmexp.free);
    549 #endif
    550 	goto loop;
    551 }
    552 
    553 /*
    554  * swappable: is process "p" swappable?
    555  */
    556 
    557 #define	swappable(p)							\
    558 	(((p)->p_flag & (P_SYSTEM | P_INMEM | P_WEXIT)) == P_INMEM &&	\
    559 	 (p)->p_holdcnt == 0)
    560 
    561 /*
    562  * swapout_threads: find threads that can be swapped and unwire their
    563  *	u-areas.
    564  *
    565  * - called by the pagedaemon
    566  * - try and swap at least one processs
    567  * - processes that are sleeping or stopped for maxslp or more seconds
    568  *   are swapped... otherwise the longest-sleeping or stopped process
    569  *   is swapped, otherwise the longest resident process...
    570  */
    571 
    572 void
    573 uvm_swapout_threads()
    574 {
    575 	struct proc *p;
    576 	struct proc *outp, *outp2;
    577 	int outpri, outpri2;
    578 	int didswap = 0;
    579 	extern int maxslp;
    580 	/* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
    581 
    582 #ifdef DEBUG
    583 	if (!enableswap)
    584 		return;
    585 #endif
    586 
    587 	/*
    588 	 * outp/outpri  : stop/sleep process with largest sleeptime < maxslp
    589 	 * outp2/outpri2: the longest resident process (its swap time)
    590 	 */
    591 	outp = outp2 = NULL;
    592 	outpri = outpri2 = 0;
    593 	proclist_lock_read();
    594 	LIST_FOREACH(p, &allproc, p_list) {
    595 		if (!swappable(p))
    596 			continue;
    597 		switch (p->p_stat) {
    598 		case SRUN:
    599 		case SONPROC:
    600 			if (p->p_swtime > outpri2) {
    601 				outp2 = p;
    602 				outpri2 = p->p_swtime;
    603 			}
    604 			continue;
    605 
    606 		case SSLEEP:
    607 		case SSTOP:
    608 			if (p->p_slptime >= maxslp) {
    609 				uvm_swapout(p);
    610 				didswap++;
    611 			} else if (p->p_slptime > outpri) {
    612 				outp = p;
    613 				outpri = p->p_slptime;
    614 			}
    615 			continue;
    616 		}
    617 	}
    618 	proclist_unlock_read();
    619 
    620 	/*
    621 	 * If we didn't get rid of any real duds, toss out the next most
    622 	 * likely sleeping/stopped or running candidate.  We only do this
    623 	 * if we are real low on memory since we don't gain much by doing
    624 	 * it (USPACE bytes).
    625 	 */
    626 	if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
    627 		if ((p = outp) == NULL)
    628 			p = outp2;
    629 #ifdef DEBUG
    630 		if (swapdebug & SDB_SWAPOUT)
    631 			printf("swapout_threads: no duds, try procp %p\n", p);
    632 #endif
    633 		if (p)
    634 			uvm_swapout(p);
    635 	}
    636 }
    637 
    638 /*
    639  * uvm_swapout: swap out process "p"
    640  *
    641  * - currently "swapout" means "unwire U-area" and "pmap_collect()"
    642  *   the pmap.
    643  * - XXXCDC: should deactivate all process' private anonymous memory
    644  */
    645 
    646 static void
    647 uvm_swapout(p)
    648 	struct proc *p;
    649 {
    650 	vaddr_t addr;
    651 	int s;
    652 
    653 #ifdef DEBUG
    654 	if (swapdebug & SDB_SWAPOUT)
    655 		printf("swapout: pid %d(%s)@%p, stat %x pri %d free %d\n",
    656 	   p->p_pid, p->p_comm, p->p_addr, p->p_stat,
    657 	   p->p_slptime, uvmexp.free);
    658 #endif
    659 
    660 	/*
    661 	 * Do any machine-specific actions necessary before swapout.
    662 	 * This can include saving floating point state, etc.
    663 	 */
    664 	cpu_swapout(p);
    665 
    666 	/*
    667 	 * Mark it as (potentially) swapped out.
    668 	 */
    669 	SCHED_LOCK(s);
    670 	p->p_flag &= ~P_INMEM;
    671 	if (p->p_stat == SRUN)
    672 		remrunqueue(p);
    673 	SCHED_UNLOCK(s);
    674 	p->p_swtime = 0;
    675 	p->p_stats->p_ru.ru_nswap++;
    676 	++uvmexp.swapouts;
    677 
    678 	/*
    679 	 * Unwire the to-be-swapped process's user struct and kernel stack.
    680 	 */
    681 	addr = (vaddr_t)p->p_addr;
    682 	uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !P_INMEM */
    683 	pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
    684 }
    685 
    686 /*
    687  * uvm_coredump_walkmap: walk a process's map for the purpose of dumping
    688  * a core file.
    689  */
    690 
    691 int
    692 uvm_coredump_walkmap(p, vp, cred, func, cookie)
    693 	struct proc *p;
    694 	struct vnode *vp;
    695 	struct ucred *cred;
    696 	int (*func)(struct proc *, struct vnode *, struct ucred *,
    697 	    struct uvm_coredump_state *);
    698 	void *cookie;
    699 {
    700 	struct uvm_coredump_state state;
    701 	struct vmspace *vm = p->p_vmspace;
    702 	struct vm_map *map = &vm->vm_map;
    703 	struct vm_map_entry *entry;
    704 	vaddr_t maxstack;
    705 	int error;
    706 
    707 	maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
    708 
    709 	for (entry = map->header.next; entry != &map->header;
    710 	     entry = entry->next) {
    711 		/* Should never happen for a user process. */
    712 		if (UVM_ET_ISSUBMAP(entry))
    713 			panic("uvm_coredump_walkmap: user process with "
    714 			    "submap?");
    715 
    716 		state.cookie = cookie;
    717 		state.start = entry->start;
    718 		state.end = entry->end;
    719 		state.prot = entry->protection;
    720 		state.flags = 0;
    721 
    722 		if (state.start >= VM_MAXUSER_ADDRESS)
    723 			continue;
    724 
    725 		if (state.end > VM_MAXUSER_ADDRESS)
    726 			state.end = VM_MAXUSER_ADDRESS;
    727 
    728 		if (state.start >= (vaddr_t)vm->vm_maxsaddr) {
    729 			if (state.end <= maxstack)
    730 				continue;
    731 			if (state.start < maxstack)
    732 				state.start = maxstack;
    733 			state.flags |= UVM_COREDUMP_STACK;
    734 		}
    735 
    736 		if ((entry->protection & VM_PROT_WRITE) == 0)
    737 			state.flags |= UVM_COREDUMP_NODUMP;
    738 
    739 		if (entry->object.uvm_obj != NULL &&
    740 		    entry->object.uvm_obj->pgops == &uvm_deviceops)
    741 			state.flags |= UVM_COREDUMP_NODUMP;
    742 
    743 		error = (*func)(p, vp, cred, &state);
    744 		if (error)
    745 			return (error);
    746 	}
    747 
    748 	return (0);
    749 }
    750