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