Home | History | Annotate | Line # | Download | only in uvm
uvm_glue.c revision 1.67
      1 /*	$NetBSD: uvm_glue.c,v 1.67 2003/10/13 20:43:03 scw 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.67 2003/10/13 20:43:03 scw 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 lwp *));
    100 
    101 #define UVM_NUAREA_MAX 16
    102 void *uvm_uareas;
    103 int uvm_nuarea;
    104 struct simplelock uvm_uareas_slock = SIMPLELOCK_INITIALIZER;
    105 
    106 /*
    107  * XXXCDC: do these really belong here?
    108  */
    109 
    110 int readbuffers = 0;		/* allow KGDB to read kern buffer pool */
    111 				/* XXX: see uvm_kernacc */
    112 
    113 
    114 /*
    115  * uvm_kernacc: can the kernel access a region of memory
    116  *
    117  * - called from malloc [DIAGNOSTIC], and /dev/kmem driver (mem.c)
    118  */
    119 
    120 boolean_t
    121 uvm_kernacc(addr, len, rw)
    122 	caddr_t addr;
    123 	size_t len;
    124 	int rw;
    125 {
    126 	boolean_t rv;
    127 	vaddr_t saddr, eaddr;
    128 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
    129 
    130 	saddr = trunc_page((vaddr_t)addr);
    131 	eaddr = round_page((vaddr_t)addr + len);
    132 	vm_map_lock_read(kernel_map);
    133 	rv = uvm_map_checkprot(kernel_map, saddr, eaddr, prot);
    134 	vm_map_unlock_read(kernel_map);
    135 
    136 	/*
    137 	 * XXX there are still some things (e.g. the buffer cache) that
    138 	 * are managed behind the VM system's back so even though an
    139 	 * address is accessible in the mind of the VM system, there may
    140 	 * not be physical pages where the VM thinks there is.  This can
    141 	 * lead to bogus allocation of pages in the kernel address space
    142 	 * or worse, inconsistencies at the pmap level.  We only worry
    143 	 * about the buffer cache for now.
    144 	 */
    145 	if (!readbuffers && rv && (eaddr > (vaddr_t)buffers &&
    146 			     saddr < (vaddr_t)buffers + MAXBSIZE * nbuf))
    147 		rv = FALSE;
    148 	return(rv);
    149 }
    150 
    151 /*
    152  * uvm_useracc: can the user access it?
    153  *
    154  * - called from physio() and sys___sysctl().
    155  */
    156 
    157 boolean_t
    158 uvm_useracc(addr, len, rw)
    159 	caddr_t addr;
    160 	size_t len;
    161 	int rw;
    162 {
    163 	struct vm_map *map;
    164 	boolean_t rv;
    165 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
    166 
    167 	/* XXX curproc */
    168 	map = &curproc->p_vmspace->vm_map;
    169 
    170 	vm_map_lock_read(map);
    171 	rv = uvm_map_checkprot(map, trunc_page((vaddr_t)addr),
    172 	    round_page((vaddr_t)addr + len), prot);
    173 	vm_map_unlock_read(map);
    174 
    175 	return(rv);
    176 }
    177 
    178 #ifdef KGDB
    179 /*
    180  * Change protections on kernel pages from addr to addr+len
    181  * (presumably so debugger can plant a breakpoint).
    182  *
    183  * We force the protection change at the pmap level.  If we were
    184  * to use vm_map_protect a change to allow writing would be lazily-
    185  * applied meaning we would still take a protection fault, something
    186  * we really don't want to do.  It would also fragment the kernel
    187  * map unnecessarily.  We cannot use pmap_protect since it also won't
    188  * enforce a write-enable request.  Using pmap_enter is the only way
    189  * we can ensure the change takes place properly.
    190  */
    191 void
    192 uvm_chgkprot(addr, len, rw)
    193 	caddr_t addr;
    194 	size_t len;
    195 	int rw;
    196 {
    197 	vm_prot_t prot;
    198 	paddr_t pa;
    199 	vaddr_t sva, eva;
    200 
    201 	prot = rw == B_READ ? VM_PROT_READ : VM_PROT_READ|VM_PROT_WRITE;
    202 	eva = round_page((vaddr_t)addr + len);
    203 	for (sva = trunc_page((vaddr_t)addr); sva < eva; sva += PAGE_SIZE) {
    204 		/*
    205 		 * Extract physical address for the page.
    206 		 */
    207 		if (pmap_extract(pmap_kernel(), sva, &pa) == FALSE)
    208 			panic("chgkprot: invalid page");
    209 		pmap_enter(pmap_kernel(), sva, pa, prot, PMAP_WIRED);
    210 	}
    211 	pmap_update(pmap_kernel());
    212 }
    213 #endif
    214 
    215 /*
    216  * uvm_vslock: wire user memory for I/O
    217  *
    218  * - called from physio and sys___sysctl
    219  * - XXXCDC: consider nuking this (or making it a macro?)
    220  */
    221 
    222 int
    223 uvm_vslock(p, addr, len, access_type)
    224 	struct proc *p;
    225 	caddr_t	addr;
    226 	size_t	len;
    227 	vm_prot_t access_type;
    228 {
    229 	struct vm_map *map;
    230 	vaddr_t start, end;
    231 	int error;
    232 
    233 	map = &p->p_vmspace->vm_map;
    234 	start = trunc_page((vaddr_t)addr);
    235 	end = round_page((vaddr_t)addr + len);
    236 	error = uvm_fault_wire(map, start, end, VM_FAULT_WIRE, access_type);
    237 	return error;
    238 }
    239 
    240 /*
    241  * uvm_vsunlock: unwire user memory wired by uvm_vslock()
    242  *
    243  * - called from physio and sys___sysctl
    244  * - XXXCDC: consider nuking this (or making it a macro?)
    245  */
    246 
    247 void
    248 uvm_vsunlock(p, addr, len)
    249 	struct proc *p;
    250 	caddr_t	addr;
    251 	size_t	len;
    252 {
    253 	uvm_fault_unwire(&p->p_vmspace->vm_map, trunc_page((vaddr_t)addr),
    254 		round_page((vaddr_t)addr + len));
    255 }
    256 
    257 /*
    258  * uvm_proc_fork: fork a virtual address space
    259  *
    260  * - the address space is copied as per parent map's inherit values
    261  */
    262 void
    263 uvm_proc_fork(p1, p2, shared)
    264 	struct proc *p1, *p2;
    265 	boolean_t shared;
    266 {
    267 
    268 	if (shared == TRUE) {
    269 		p2->p_vmspace = NULL;
    270 		uvmspace_share(p1, p2);
    271 	} else {
    272 		p2->p_vmspace = uvmspace_fork(p1->p_vmspace);
    273 	}
    274 
    275 	cpu_proc_fork(p1, p2);
    276 }
    277 
    278 
    279 /*
    280  * uvm_lwp_fork: fork a thread
    281  *
    282  * - a new "user" structure is allocated for the child process
    283  *	[filled in by MD layer...]
    284  * - if specified, the child gets a new user stack described by
    285  *	stack and stacksize
    286  * - NOTE: the kernel stack may be at a different location in the child
    287  *	process, and thus addresses of automatic variables may be invalid
    288  *	after cpu_lwp_fork returns in the child process.  We do nothing here
    289  *	after cpu_lwp_fork returns.
    290  * - XXXCDC: we need a way for this to return a failure value rather
    291  *   than just hang
    292  */
    293 void
    294 uvm_lwp_fork(l1, l2, stack, stacksize, func, arg)
    295 	struct lwp *l1, *l2;
    296 	void *stack;
    297 	size_t stacksize;
    298 	void (*func) __P((void *));
    299 	void *arg;
    300 {
    301 	struct user *up = l2->l_addr;
    302 	int error;
    303 
    304 	/*
    305 	 * Wire down the U-area for the process, which contains the PCB
    306 	 * and the kernel stack.  Wired state is stored in l->l_flag's
    307 	 * L_INMEM bit rather than in the vm_map_entry's wired count
    308 	 * to prevent kernel_map fragmentation.  If we reused a cached U-area,
    309 	 * L_INMEM will already be set and we don't need to do anything.
    310 	 *
    311 	 * Note the kernel stack gets read/write accesses right off the bat.
    312 	 */
    313 
    314 	if ((l2->l_flag & L_INMEM) == 0) {
    315 		error = uvm_fault_wire(kernel_map, (vaddr_t)up,
    316 		    (vaddr_t)up + USPACE, VM_FAULT_WIRE,
    317 		    VM_PROT_READ | VM_PROT_WRITE);
    318 		if (error)
    319 			panic("uvm_lwp_fork: uvm_fault_wire failed: %d", error);
    320 #ifdef PMAP_UAREA
    321 		/* Tell the pmap this is a u-area mapping */
    322 		PMAP_UAREA((vaddr_t)up);
    323 #endif
    324 		l2->l_flag |= L_INMEM;
    325 	}
    326 
    327 #ifdef KSTACK_CHECK_MAGIC
    328 	/*
    329 	 * fill stack with magic number
    330 	 */
    331 	kstack_setup_magic(l2);
    332 #endif
    333 
    334 	/*
    335 	 * cpu_lwp_fork() copy and update the pcb, and make the child ready
    336  	 * to run.  If this is a normal user fork, the child will exit
    337 	 * directly to user mode via child_return() on its first time
    338 	 * slice and will not return here.  If this is a kernel thread,
    339 	 * the specified entry point will be executed.
    340 	 */
    341 	cpu_lwp_fork(l1, l2, stack, stacksize, func, arg);
    342 }
    343 
    344 /*
    345  * uvm_exit: exit a virtual address space
    346  *
    347  * - the process passed to us is a dead (pre-zombie) process; we
    348  *   are running on a different context now (the reaper).
    349  * - we must run in a separate thread because freeing the vmspace
    350  *   of the dead process may block.
    351  */
    352 
    353 void
    354 uvm_proc_exit(p)
    355 	struct proc *p;
    356 {
    357 	uvmspace_free(p->p_vmspace);
    358 }
    359 
    360 void
    361 uvm_lwp_exit(l)
    362 	struct lwp *l;
    363 {
    364 	vaddr_t va = (vaddr_t)l->l_addr;
    365 
    366 	l->l_flag &= ~L_INMEM;
    367 	uvm_uarea_free(va);
    368 	l->l_addr = NULL;
    369 }
    370 
    371 /*
    372  * uvm_uarea_alloc: allocate a u-area
    373  */
    374 
    375 boolean_t
    376 uvm_uarea_alloc(vaddr_t *uaddrp)
    377 {
    378 	vaddr_t uaddr;
    379 
    380 #ifndef USPACE_ALIGN
    381 #define USPACE_ALIGN    0
    382 #endif
    383 
    384 	simple_lock(&uvm_uareas_slock);
    385 	uaddr = (vaddr_t)uvm_uareas;
    386 	if (uaddr) {
    387 		uvm_uareas = *(void **)uvm_uareas;
    388 		uvm_nuarea--;
    389 		simple_unlock(&uvm_uareas_slock);
    390 		*uaddrp = uaddr;
    391 		return TRUE;
    392 	} else {
    393 		simple_unlock(&uvm_uareas_slock);
    394 		*uaddrp = uvm_km_valloc_align(kernel_map, USPACE, USPACE_ALIGN);
    395 		return FALSE;
    396 	}
    397 }
    398 
    399 /*
    400  * uvm_uarea_free: free a u-area
    401  */
    402 
    403 void
    404 uvm_uarea_free(vaddr_t uaddr)
    405 {
    406 
    407 	simple_lock(&uvm_uareas_slock);
    408 	if (uvm_nuarea < UVM_NUAREA_MAX) {
    409 		*(void **)uaddr = uvm_uareas;
    410 		uvm_uareas = (void *)uaddr;
    411 		uvm_nuarea++;
    412 		simple_unlock(&uvm_uareas_slock);
    413 	} else {
    414 		simple_unlock(&uvm_uareas_slock);
    415 		uvm_km_free(kernel_map, uaddr, USPACE);
    416 	}
    417 }
    418 
    419 /*
    420  * uvm_init_limit: init per-process VM limits
    421  *
    422  * - called for process 0 and then inherited by all others.
    423  */
    424 
    425 void
    426 uvm_init_limits(p)
    427 	struct proc *p;
    428 {
    429 
    430 	/*
    431 	 * Set up the initial limits on process VM.  Set the maximum
    432 	 * resident set size to be all of (reasonably) available memory.
    433 	 * This causes any single, large process to start random page
    434 	 * replacement once it fills memory.
    435 	 */
    436 
    437 	p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
    438 	p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
    439 	p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
    440 	p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
    441 	p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(uvmexp.free);
    442 }
    443 
    444 #ifdef DEBUG
    445 int	enableswap = 1;
    446 int	swapdebug = 0;
    447 #define	SDB_FOLLOW	1
    448 #define SDB_SWAPIN	2
    449 #define SDB_SWAPOUT	4
    450 #endif
    451 
    452 /*
    453  * uvm_swapin: swap in a process's u-area.
    454  */
    455 
    456 void
    457 uvm_swapin(l)
    458 	struct lwp *l;
    459 {
    460 	vaddr_t addr;
    461 	int s, error;
    462 
    463 	addr = (vaddr_t)l->l_addr;
    464 	/* make L_INMEM true */
    465 	error = uvm_fault_wire(kernel_map, addr, addr + USPACE, VM_FAULT_WIRE,
    466 	    VM_PROT_READ | VM_PROT_WRITE);
    467 	if (error) {
    468 		panic("uvm_swapin: rewiring stack failed: %d", error);
    469 	}
    470 
    471 	/*
    472 	 * Some architectures need to be notified when the user area has
    473 	 * moved to new physical page(s) (e.g.  see mips/mips/vm_machdep.c).
    474 	 */
    475 	cpu_swapin(l);
    476 	SCHED_LOCK(s);
    477 	if (l->l_stat == LSRUN)
    478 		setrunqueue(l);
    479 	l->l_flag |= L_INMEM;
    480 	SCHED_UNLOCK(s);
    481 	l->l_swtime = 0;
    482 	++uvmexp.swapins;
    483 }
    484 
    485 /*
    486  * uvm_scheduler: process zero main loop
    487  *
    488  * - attempt to swapin every swaped-out, runnable process in order of
    489  *	priority.
    490  * - if not enough memory, wake the pagedaemon and let it clear space.
    491  */
    492 
    493 void
    494 uvm_scheduler()
    495 {
    496 	struct lwp *l, *ll;
    497 	int pri;
    498 	int ppri;
    499 
    500 loop:
    501 #ifdef DEBUG
    502 	while (!enableswap)
    503 		tsleep(&proc0, PVM, "noswap", 0);
    504 #endif
    505 	ll = NULL;		/* process to choose */
    506 	ppri = INT_MIN;	/* its priority */
    507 	proclist_lock_read();
    508 
    509 	LIST_FOREACH(l, &alllwp, l_list) {
    510 		/* is it a runnable swapped out process? */
    511 		if (l->l_stat == LSRUN && (l->l_flag & L_INMEM) == 0) {
    512 			pri = l->l_swtime + l->l_slptime -
    513 			    (l->l_proc->p_nice - NZERO) * 8;
    514 			if (pri > ppri) {   /* higher priority?  remember it. */
    515 				ll = l;
    516 				ppri = pri;
    517 			}
    518 		}
    519 	}
    520 	/*
    521 	 * XXXSMP: possible unlock/sleep race between here and the
    522 	 * "scheduler" tsleep below..
    523 	 */
    524 	proclist_unlock_read();
    525 
    526 #ifdef DEBUG
    527 	if (swapdebug & SDB_FOLLOW)
    528 		printf("scheduler: running, procp %p pri %d\n", ll, ppri);
    529 #endif
    530 	/*
    531 	 * Nothing to do, back to sleep
    532 	 */
    533 	if ((l = ll) == NULL) {
    534 		tsleep(&proc0, PVM, "scheduler", 0);
    535 		goto loop;
    536 	}
    537 
    538 	/*
    539 	 * we have found swapped out process which we would like to bring
    540 	 * back in.
    541 	 *
    542 	 * XXX: this part is really bogus cuz we could deadlock on memory
    543 	 * despite our feeble check
    544 	 */
    545 	if (uvmexp.free > atop(USPACE)) {
    546 #ifdef DEBUG
    547 		if (swapdebug & SDB_SWAPIN)
    548 			printf("swapin: pid %d(%s)@%p, pri %d free %d\n",
    549 	     l->l_proc->p_pid, l->l_proc->p_comm, l->l_addr, ppri, uvmexp.free);
    550 #endif
    551 		uvm_swapin(l);
    552 		goto loop;
    553 	}
    554 	/*
    555 	 * not enough memory, jab the pageout daemon and wait til the coast
    556 	 * is clear
    557 	 */
    558 #ifdef DEBUG
    559 	if (swapdebug & SDB_FOLLOW)
    560 		printf("scheduler: no room for pid %d(%s), free %d\n",
    561 	   l->l_proc->p_pid, l->l_proc->p_comm, uvmexp.free);
    562 #endif
    563 	uvm_wait("schedpwait");
    564 #ifdef DEBUG
    565 	if (swapdebug & SDB_FOLLOW)
    566 		printf("scheduler: room again, free %d\n", uvmexp.free);
    567 #endif
    568 	goto loop;
    569 }
    570 
    571 /*
    572  * swappable: is LWP "l" swappable?
    573  */
    574 
    575 #define	swappable(l)							\
    576 	(((l)->l_flag & (L_INMEM)) &&					\
    577 	 ((((l)->l_proc->p_flag) & (P_SYSTEM | P_WEXIT)) == 0) &&	\
    578 	 (l)->l_holdcnt == 0)
    579 
    580 /*
    581  * swapout_threads: find threads that can be swapped and unwire their
    582  *	u-areas.
    583  *
    584  * - called by the pagedaemon
    585  * - try and swap at least one processs
    586  * - processes that are sleeping or stopped for maxslp or more seconds
    587  *   are swapped... otherwise the longest-sleeping or stopped process
    588  *   is swapped, otherwise the longest resident process...
    589  */
    590 
    591 void
    592 uvm_swapout_threads()
    593 {
    594 	struct lwp *l;
    595 	struct lwp *outl, *outl2;
    596 	int outpri, outpri2;
    597 	int didswap = 0;
    598 	extern int maxslp;
    599 	/* XXXCDC: should move off to uvmexp. or uvm., also in uvm_meter */
    600 
    601 #ifdef DEBUG
    602 	if (!enableswap)
    603 		return;
    604 #endif
    605 
    606 	/*
    607 	 * outl/outpri  : stop/sleep thread with largest sleeptime < maxslp
    608 	 * outl2/outpri2: the longest resident thread (its swap time)
    609 	 */
    610 	outl = outl2 = NULL;
    611 	outpri = outpri2 = 0;
    612 	proclist_lock_read();
    613 	LIST_FOREACH(l, &alllwp, l_list) {
    614 		if (!swappable(l))
    615 			continue;
    616 		switch (l->l_stat) {
    617 		case LSRUN:
    618 		case LSONPROC:
    619 			if (l->l_swtime > outpri2) {
    620 				outl2 = l;
    621 				outpri2 = l->l_swtime;
    622 			}
    623 			continue;
    624 
    625 		case LSSLEEP:
    626 		case LSSTOP:
    627 			if (l->l_slptime >= maxslp) {
    628 				uvm_swapout(l);
    629 				didswap++;
    630 			} else if (l->l_slptime > outpri) {
    631 				outl = l;
    632 				outpri = l->l_slptime;
    633 			}
    634 			continue;
    635 		}
    636 	}
    637 	proclist_unlock_read();
    638 
    639 	/*
    640 	 * If we didn't get rid of any real duds, toss out the next most
    641 	 * likely sleeping/stopped or running candidate.  We only do this
    642 	 * if we are real low on memory since we don't gain much by doing
    643 	 * it (USPACE bytes).
    644 	 */
    645 	if (didswap == 0 && uvmexp.free <= atop(round_page(USPACE))) {
    646 		if ((l = outl) == NULL)
    647 			l = outl2;
    648 #ifdef DEBUG
    649 		if (swapdebug & SDB_SWAPOUT)
    650 			printf("swapout_threads: no duds, try procp %p\n", l);
    651 #endif
    652 		if (l)
    653 			uvm_swapout(l);
    654 	}
    655 }
    656 
    657 /*
    658  * uvm_swapout: swap out lwp "l"
    659  *
    660  * - currently "swapout" means "unwire U-area" and "pmap_collect()"
    661  *   the pmap.
    662  * - XXXCDC: should deactivate all process' private anonymous memory
    663  */
    664 
    665 static void
    666 uvm_swapout(l)
    667 	struct lwp *l;
    668 {
    669 	vaddr_t addr;
    670 	int s;
    671 	struct proc *p = l->l_proc;
    672 
    673 #ifdef DEBUG
    674 	if (swapdebug & SDB_SWAPOUT)
    675 		printf("swapout: lid %d.%d(%s)@%p, stat %x pri %d free %d\n",
    676 	   p->p_pid, l->l_lid, p->p_comm, l->l_addr, l->l_stat,
    677 	   l->l_slptime, uvmexp.free);
    678 #endif
    679 
    680 	/*
    681 	 * Do any machine-specific actions necessary before swapout.
    682 	 * This can include saving floating point state, etc.
    683 	 */
    684 	cpu_swapout(l);
    685 
    686 	/*
    687 	 * Mark it as (potentially) swapped out.
    688 	 */
    689 	SCHED_LOCK(s);
    690 	l->l_flag &= ~L_INMEM;
    691 	if (l->l_stat == LSRUN)
    692 		remrunqueue(l);
    693 	SCHED_UNLOCK(s);
    694 	l->l_swtime = 0;
    695 	p->p_stats->p_ru.ru_nswap++;
    696 	++uvmexp.swapouts;
    697 
    698 	/*
    699 	 * Unwire the to-be-swapped process's user struct and kernel stack.
    700 	 */
    701 	addr = (vaddr_t)l->l_addr;
    702 	uvm_fault_unwire(kernel_map, addr, addr + USPACE); /* !L_INMEM */
    703 	pmap_collect(vm_map_pmap(&p->p_vmspace->vm_map));
    704 }
    705 
    706 /*
    707  * uvm_coredump_walkmap: walk a process's map for the purpose of dumping
    708  * a core file.
    709  */
    710 
    711 int
    712 uvm_coredump_walkmap(p, vp, cred, func, cookie)
    713 	struct proc *p;
    714 	struct vnode *vp;
    715 	struct ucred *cred;
    716 	int (*func)(struct proc *, struct vnode *, struct ucred *,
    717 	    struct uvm_coredump_state *);
    718 	void *cookie;
    719 {
    720 	struct uvm_coredump_state state;
    721 	struct vmspace *vm = p->p_vmspace;
    722 	struct vm_map *map = &vm->vm_map;
    723 	struct vm_map_entry *entry;
    724 	vaddr_t maxstack;
    725 	int error;
    726 
    727 	maxstack = trunc_page(USRSTACK - ctob(vm->vm_ssize));
    728 
    729 	entry = NULL;
    730 	vm_map_lock_read(map);
    731 	for (;;) {
    732 		if (entry == NULL)
    733 			entry = map->header.next;
    734 		else if (!uvm_map_lookup_entry(map, state.end, &entry))
    735 			entry = entry->next;
    736 		if (entry == &map->header)
    737 			break;
    738 
    739 		/* Should never happen for a user process. */
    740 		if (UVM_ET_ISSUBMAP(entry))
    741 			panic("uvm_coredump_walkmap: user process with "
    742 			    "submap?");
    743 
    744 		state.cookie = cookie;
    745 		state.start = entry->start;
    746 		state.end = entry->end;
    747 		state.prot = entry->protection;
    748 		state.flags = 0;
    749 
    750 		if (state.start >= VM_MAXUSER_ADDRESS)
    751 			continue;
    752 
    753 		if (state.end > VM_MAXUSER_ADDRESS)
    754 			state.end = VM_MAXUSER_ADDRESS;
    755 
    756 		if (state.start >= (vaddr_t)vm->vm_maxsaddr) {
    757 			if (state.end <= maxstack)
    758 				continue;
    759 			if (state.start < maxstack)
    760 				state.start = maxstack;
    761 			state.flags |= UVM_COREDUMP_STACK;
    762 		}
    763 
    764 		if ((entry->protection & VM_PROT_WRITE) == 0)
    765 			state.flags |= UVM_COREDUMP_NODUMP;
    766 
    767 		if (entry->object.uvm_obj != NULL &&
    768 		    entry->object.uvm_obj->pgops == &uvm_deviceops)
    769 			state.flags |= UVM_COREDUMP_NODUMP;
    770 
    771 		vm_map_unlock_read(map);
    772 		error = (*func)(p, vp, cred, &state);
    773 		if (error)
    774 			return (error);
    775 		vm_map_lock_read(map);
    776 	}
    777 	vm_map_unlock_read(map);
    778 
    779 	return (0);
    780 }
    781