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