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