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