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