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