vm_glue.c revision 193593
133965Sjdp/*-
260484Sobrien * Copyright (c) 1991, 1993
360484Sobrien *	The Regents of the University of California.  All rights reserved.
433965Sjdp *
533965Sjdp * This code is derived from software contributed to Berkeley by
633965Sjdp * The Mach Operating System project at Carnegie-Mellon University.
733965Sjdp *
833965Sjdp * Redistribution and use in source and binary forms, with or without
933965Sjdp * modification, are permitted provided that the following conditions
1033965Sjdp * are met:
1133965Sjdp * 1. Redistributions of source code must retain the above copyright
1233965Sjdp *    notice, this list of conditions and the following disclaimer.
1333965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1433965Sjdp *    notice, this list of conditions and the following disclaimer in the
1533965Sjdp *    documentation and/or other materials provided with the distribution.
1633965Sjdp * 4. Neither the name of the University nor the names of its contributors
1733965Sjdp *    may be used to endorse or promote products derived from this software
1833965Sjdp *    without specific prior written permission.
1933965Sjdp *
2033965Sjdp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2133965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2233965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2333965Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2433965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2533965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2633965Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2733965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2833965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2933965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3033965Sjdp * SUCH DAMAGE.
3133965Sjdp *
3233965Sjdp *	from: @(#)vm_glue.c	8.6 (Berkeley) 1/5/94
3333965Sjdp *
3477298Sobrien *
3533965Sjdp * Copyright (c) 1987, 1990 Carnegie-Mellon University.
3633965Sjdp * All rights reserved.
3733965Sjdp *
3833965Sjdp * Permission to use, copy, modify and distribute this software and
3933965Sjdp * its documentation is hereby granted, provided that both the copyright
4033965Sjdp * notice and this permission notice appear in all copies of the
4133965Sjdp * software, derivative works or modified versions, and any portions
4233965Sjdp * thereof, and that both notices appear in supporting documentation.
4333965Sjdp *
4477298Sobrien * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
4533965Sjdp * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
4633965Sjdp * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
4733965Sjdp *
4833965Sjdp * Carnegie Mellon requests users of this software to return to
4933965Sjdp *
5077298Sobrien *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
5133965Sjdp *  School of Computer Science
5233965Sjdp *  Carnegie Mellon University
5333965Sjdp *  Pittsburgh PA 15213-3890
5433965Sjdp *
5533965Sjdp * any improvements or extensions that they make and grant Carnegie the
5633965Sjdp * rights to redistribute these changes.
5733965Sjdp */
5833965Sjdp
5933965Sjdp#include <sys/cdefs.h>
6033965Sjdp__FBSDID("$FreeBSD: head/sys/vm/vm_glue.c 193593 2009-06-06 20:13:14Z alc $");
6133965Sjdp
6233965Sjdp#include "opt_vm.h"
6333965Sjdp#include "opt_kstack_pages.h"
6433965Sjdp#include "opt_kstack_max_pages.h"
6533965Sjdp
6633965Sjdp#include <sys/param.h>
6777298Sobrien#include <sys/systm.h>
6833965Sjdp#include <sys/limits.h>
6977298Sobrien#include <sys/lock.h>
7033965Sjdp#include <sys/mutex.h>
7133965Sjdp#include <sys/proc.h>
7233965Sjdp#include <sys/resourcevar.h>
7333965Sjdp#include <sys/sched.h>
7438889Sjdp#include <sys/sf_buf.h>
7538889Sjdp#include <sys/shm.h>
7638889Sjdp#include <sys/vmmeter.h>
7738889Sjdp#include <sys/sx.h>
7838889Sjdp#include <sys/sysctl.h>
7938889Sjdp
8038889Sjdp#include <sys/kernel.h>
8160484Sobrien#include <sys/ktr.h>
8277298Sobrien#include <sys/unistd.h>
8333965Sjdp
8433965Sjdp#include <vm/vm.h>
8577298Sobrien#include <vm/vm_param.h>
8633965Sjdp#include <vm/pmap.h>
8733965Sjdp#include <vm/vm_map.h>
8833965Sjdp#include <vm/vm_page.h>
8977298Sobrien#include <vm/vm_pageout.h>
9033965Sjdp#include <vm/vm_object.h>
9133965Sjdp#include <vm/vm_kern.h>
9233965Sjdp#include <vm/vm_extern.h>
9333965Sjdp#include <vm/vm_pager.h>
9433965Sjdp#include <vm/swap_pager.h>
9533965Sjdp
9633965Sjdpextern int maxslp;
9733965Sjdp
9833965Sjdp/*
9933965Sjdp * System initialization
10033965Sjdp *
10133965Sjdp * Note: proc0 from proc.h
10233965Sjdp */
10333965Sjdpstatic void vm_init_limits(void *);
10433965SjdpSYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0);
10533965Sjdp
10633965Sjdp/*
10733965Sjdp * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
10833965Sjdp *
10960484Sobrien * Note: run scheduling should be divorced from the vm system.
11060484Sobrien */
11160484Sobrienstatic void scheduler(void *);
11277298SobrienSYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_ANY, scheduler, NULL);
11377298Sobrien
11433965Sjdp#ifndef NO_SWAPPING
11533965Sjdpstatic int swapout(struct proc *);
11677298Sobrienstatic void swapclear(struct proc *);
11777298Sobrien#endif
11833965Sjdp
11938889Sjdp/*
12038889Sjdp * MPSAFE
12138889Sjdp *
12238889Sjdp * WARNING!  This code calls vm_map_check_protection() which only checks
12338889Sjdp * the associated vm_map_entry range.  It does not determine whether the
12460484Sobrien * contents of the memory is actually readable or writable.  In most cases
12538889Sjdp * just checking the vm_map_entry is sufficient within the kernel's address
12638889Sjdp * space.
12738889Sjdp */
12838889Sjdpint
12938889Sjdpkernacc(addr, len, rw)
13038889Sjdp	void *addr;
13138889Sjdp	int len, rw;
13238889Sjdp{
13338889Sjdp	boolean_t rv;
13477298Sobrien	vm_offset_t saddr, eaddr;
13577298Sobrien	vm_prot_t prot;
13677298Sobrien
13777298Sobrien	KASSERT((rw & ~VM_PROT_ALL) == 0,
13877298Sobrien	    ("illegal ``rw'' argument to kernacc (%x)\n", rw));
13933965Sjdp
14033965Sjdp	if ((vm_offset_t)addr + len > kernel_map->max_offset ||
14133965Sjdp	    (vm_offset_t)addr + len < (vm_offset_t)addr)
14260484Sobrien		return (FALSE);
14377298Sobrien
14477298Sobrien	prot = rw;
14533965Sjdp	saddr = trunc_page((vm_offset_t)addr);
14633965Sjdp	eaddr = round_page((vm_offset_t)addr + len);
14733965Sjdp	vm_map_lock_read(kernel_map);
14860484Sobrien	rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot);
14933965Sjdp	vm_map_unlock_read(kernel_map);
15033965Sjdp	return (rv == TRUE);
15133965Sjdp}
15233965Sjdp
15333965Sjdp/*
15433965Sjdp * MPSAFE
15533965Sjdp *
15633965Sjdp * WARNING!  This code calls vm_map_check_protection() which only checks
15733965Sjdp * the associated vm_map_entry range.  It does not determine whether the
15833965Sjdp * contents of the memory is actually readable or writable.  vmapbuf(),
15977298Sobrien * vm_fault_quick(), or copyin()/copout()/su*()/fu*() functions should be
16077298Sobrien * used in conjuction with this call.
16177298Sobrien */
16277298Sobrienint
16377298Sobrienuseracc(addr, len, rw)
16477298Sobrien	void *addr;
16577298Sobrien	int len, rw;
16677298Sobrien{
16777298Sobrien	boolean_t rv;
16877298Sobrien	vm_prot_t prot;
16977298Sobrien	vm_map_t map;
17077298Sobrien
17177298Sobrien	KASSERT((rw & ~VM_PROT_ALL) == 0,
17277298Sobrien	    ("illegal ``rw'' argument to useracc (%x)\n", rw));
17377298Sobrien	prot = rw;
17477298Sobrien	map = &curproc->p_vmspace->vm_map;
17577298Sobrien	if ((vm_offset_t)addr + len > vm_map_max(map) ||
17633965Sjdp	    (vm_offset_t)addr + len < (vm_offset_t)addr) {
17777298Sobrien		return (FALSE);
17877298Sobrien	}
17977298Sobrien	vm_map_lock_read(map);
18077298Sobrien	rv = vm_map_check_protection(map, trunc_page((vm_offset_t)addr),
18133965Sjdp	    round_page((vm_offset_t)addr + len), prot);
18233965Sjdp	vm_map_unlock_read(map);
18360484Sobrien	return (rv == TRUE);
18460484Sobrien}
18560484Sobrien
18660484Sobrienint
18777298Sobrienvslock(void *addr, size_t len)
18877298Sobrien{
18933965Sjdp	vm_offset_t end, last, start;
19033965Sjdp	vm_size_t npages;
19133965Sjdp	int error;
19233965Sjdp
19333965Sjdp	last = (vm_offset_t)addr + len;
19438889Sjdp	start = trunc_page((vm_offset_t)addr);
19560484Sobrien	end = round_page(last);
19660484Sobrien	if (last < (vm_offset_t)addr || end < (vm_offset_t)addr)
19738889Sjdp		return (EINVAL);
19860484Sobrien	npages = atop(end - start);
19938889Sjdp	if (npages > vm_page_max_wired)
20038889Sjdp		return (ENOMEM);
20133965Sjdp	PROC_LOCK(curproc);
20260484Sobrien	if (ptoa(npages +
20360484Sobrien	    pmap_wired_count(vm_map_pmap(&curproc->p_vmspace->vm_map))) >
20460484Sobrien	    lim_cur(curproc, RLIMIT_MEMLOCK)) {
20533965Sjdp		PROC_UNLOCK(curproc);
20638889Sjdp		return (ENOMEM);
20760484Sobrien	}
20838889Sjdp	PROC_UNLOCK(curproc);
20938889Sjdp#if 0
21060484Sobrien	/*
21138889Sjdp	 * XXX - not yet
21260484Sobrien	 *
21360484Sobrien	 * The limit for transient usage of wired pages should be
21477298Sobrien	 * larger than for "permanent" wired pages (mlock()).
21577298Sobrien	 *
21633965Sjdp	 * Also, the sysctl code, which is the only present user
21733965Sjdp	 * of vslock(), does a hard loop on EAGAIN.
21838889Sjdp	 */
21977298Sobrien	if (npages + cnt.v_wire_count > vm_page_max_wired)
22060484Sobrien		return (EAGAIN);
22138889Sjdp#endif
22238889Sjdp	error = vm_map_wire(&curproc->p_vmspace->vm_map, start, end,
22360484Sobrien	    VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
22460484Sobrien	/*
22577298Sobrien	 * Return EFAULT on error to match copy{in,out}() behaviour
22677298Sobrien	 * rather than returning ENOMEM like mlock() would.
22777298Sobrien	 */
22877298Sobrien	return (error == KERN_SUCCESS ? 0 : EFAULT);
22977298Sobrien}
23033965Sjdp
23138889Sjdpvoid
23260484Sobrienvsunlock(void *addr, size_t len)
23333965Sjdp{
23433965Sjdp
23560484Sobrien	/* Rely on the parameter sanity checks performed by vslock(). */
23660484Sobrien	(void)vm_map_unwire(&curproc->p_vmspace->vm_map,
23760484Sobrien	    trunc_page((vm_offset_t)addr), round_page((vm_offset_t)addr + len),
23860484Sobrien	    VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
23960484Sobrien}
24077298Sobrien
24177298Sobrien/*
24277298Sobrien * Pin the page contained within the given object at the given offset.  If the
24360484Sobrien * page is not resident, allocate and load it using the given object's pager.
24460484Sobrien * Return the pinned page if successful; otherwise, return NULL.
24560484Sobrien */
24660484Sobrienstatic vm_page_t
24760484Sobrienvm_imgact_hold_page(vm_object_t object, vm_ooffset_t offset)
24860484Sobrien{
24977298Sobrien	vm_page_t m, ma[1];
25077298Sobrien	vm_pindex_t pindex;
25133965Sjdp	int rv;
25233965Sjdp
25333965Sjdp	VM_OBJECT_LOCK(object);
25433965Sjdp	pindex = OFF_TO_IDX(offset);
25533965Sjdp	m = vm_page_grab(object, pindex, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
25633965Sjdp	if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
25733965Sjdp		ma[0] = m;
25833965Sjdp		rv = vm_pager_get_pages(object, ma, 1, 0);
25933965Sjdp		m = vm_page_lookup(object, pindex);
26033965Sjdp		if (m == NULL)
26133965Sjdp			goto out;
26233965Sjdp		if (rv != VM_PAGER_OK) {
26333965Sjdp			vm_page_lock_queues();
26477298Sobrien			vm_page_free(m);
26533965Sjdp			vm_page_unlock_queues();
26633965Sjdp			m = NULL;
26733965Sjdp			goto out;
26833965Sjdp		}
26933965Sjdp	}
27033965Sjdp	vm_page_lock_queues();
27133965Sjdp	vm_page_hold(m);
27233965Sjdp	vm_page_unlock_queues();
27333965Sjdp	vm_page_wakeup(m);
27477298Sobrienout:
27577298Sobrien	VM_OBJECT_UNLOCK(object);
27633965Sjdp	return (m);
27733965Sjdp}
27833965Sjdp
27933965Sjdp/*
28033965Sjdp * Return a CPU private mapping to the page at the given offset within the
28133965Sjdp * given object.  The page is pinned before it is mapped.
28233965Sjdp */
28333965Sjdpstruct sf_buf *
28433965Sjdpvm_imgact_map_page(vm_object_t object, vm_ooffset_t offset)
28533965Sjdp{
28633965Sjdp	vm_page_t m;
28733965Sjdp
28838889Sjdp	m = vm_imgact_hold_page(object, offset);
28933965Sjdp	if (m == NULL)
29077298Sobrien		return (NULL);
29133965Sjdp	sched_pin();
29260484Sobrien	return (sf_buf_alloc(m, SFB_CPUPRIVATE));
29333965Sjdp}
29433965Sjdp
29533965Sjdp/*
29660484Sobrien * Destroy the given CPU private mapping and unpin the page that it mapped.
29733965Sjdp */
29833965Sjdpvoid
29933965Sjdpvm_imgact_unmap_page(struct sf_buf *sf)
30033965Sjdp{
30177298Sobrien	vm_page_t m;
30277298Sobrien
30333965Sjdp	m = sf_buf_page(sf);
30433965Sjdp	sf_buf_free(sf);
30533965Sjdp	sched_unpin();
30633965Sjdp	vm_page_lock_queues();
30733965Sjdp	vm_page_unhold(m);
30833965Sjdp	vm_page_unlock_queues();
30933965Sjdp}
31060484Sobrien
31133965Sjdp#ifndef KSTACK_MAX_PAGES
31233965Sjdp#define KSTACK_MAX_PAGES 32
31338889Sjdp#endif
31477298Sobrien
31560484Sobrien/*
31633965Sjdp * Create the kernel stack (including pcb for i386) for a new thread.
31733965Sjdp * This routine directly affects the fork perf for a process and
31833965Sjdp * create performance for a thread.
31933965Sjdp */
32033965Sjdpint
32138889Sjdpvm_thread_new(struct thread *td, int pages)
32260484Sobrien{
32360484Sobrien	vm_object_t ksobj;
32460484Sobrien	vm_offset_t ks;
32577298Sobrien	vm_page_t m, ma[KSTACK_MAX_PAGES];
32633965Sjdp	int i;
32777298Sobrien
32833965Sjdp	/* Bounds check */
32933965Sjdp	if (pages <= 1)
33033965Sjdp		pages = KSTACK_PAGES;
33133965Sjdp	else if (pages > KSTACK_MAX_PAGES)
33233965Sjdp		pages = KSTACK_MAX_PAGES;
33338889Sjdp	/*
33433965Sjdp	 * Allocate an object for the kstack.
33577298Sobrien	 */
33633965Sjdp	ksobj = vm_object_allocate(OBJT_DEFAULT, pages);
33760484Sobrien
33833965Sjdp	/*
33933965Sjdp	 * Get a kernel virtual address for this thread's kstack.
34033965Sjdp	 */
34160484Sobrien	ks = kmem_alloc_nofault(kernel_map,
34233965Sjdp	   (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE);
34333965Sjdp	if (ks == 0) {
34433965Sjdp		printf("vm_thread_new: kstack allocation failed\n");
34533965Sjdp		vm_object_deallocate(ksobj);
34677298Sobrien		return (0);
34777298Sobrien	}
34833965Sjdp
34933965Sjdp	if (KSTACK_GUARD_PAGES != 0) {
35033965Sjdp		pmap_qremove(ks, KSTACK_GUARD_PAGES);
35133965Sjdp		ks += KSTACK_GUARD_PAGES * PAGE_SIZE;
35233965Sjdp	}
35333965Sjdp	td->td_kstack_obj = ksobj;
35433965Sjdp	td->td_kstack = ks;
35533965Sjdp	/*
35633965Sjdp	 * Knowing the number of pages allocated is useful when you
35738889Sjdp	 * want to deallocate them.
35877298Sobrien	 */
35960484Sobrien	td->td_kstack_pages = pages;
36033965Sjdp	/*
36133965Sjdp	 * For the length of the stack, link in a real page of ram for each
36233965Sjdp	 * page of stack.
36333965Sjdp	 */
36433965Sjdp	VM_OBJECT_LOCK(ksobj);
36538889Sjdp	for (i = 0; i < pages; i++) {
36660484Sobrien		/*
36760484Sobrien		 * Get a kernel stack page.
36860484Sobrien		 */
36977298Sobrien		m = vm_page_grab(ksobj, i, VM_ALLOC_NOBUSY |
37033965Sjdp		    VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED);
37133965Sjdp		ma[i] = m;
37233965Sjdp		m->valid = VM_PAGE_BITS_ALL;
37333965Sjdp	}
37433965Sjdp	VM_OBJECT_UNLOCK(ksobj);
37533965Sjdp	pmap_qenter(ks, ma, pages);
37633965Sjdp	return (1);
37733965Sjdp}
37833965Sjdp
37933965Sjdp/*
38033965Sjdp * Dispose of a thread's kernel stack.
38133965Sjdp */
38233965Sjdpvoid
38333965Sjdpvm_thread_dispose(struct thread *td)
38433965Sjdp{
38533965Sjdp	vm_object_t ksobj;
38633965Sjdp	vm_offset_t ks;
38733965Sjdp	vm_page_t m;
38833965Sjdp	int i, pages;
38933965Sjdp
39033965Sjdp	pages = td->td_kstack_pages;
39133965Sjdp	ksobj = td->td_kstack_obj;
39233965Sjdp	ks = td->td_kstack;
39333965Sjdp	pmap_qremove(ks, pages);
39433965Sjdp	VM_OBJECT_LOCK(ksobj);
39533965Sjdp	for (i = 0; i < pages; i++) {
39633965Sjdp		m = vm_page_lookup(ksobj, i);
39733965Sjdp		if (m == NULL)
39833965Sjdp			panic("vm_thread_dispose: kstack already missing?");
39933965Sjdp		vm_page_lock_queues();
40033965Sjdp		vm_page_unwire(m, 0);
40133965Sjdp		vm_page_free(m);
40233965Sjdp		vm_page_unlock_queues();
40333965Sjdp	}
40433965Sjdp	VM_OBJECT_UNLOCK(ksobj);
40533965Sjdp	vm_object_deallocate(ksobj);
40633965Sjdp	kmem_free(kernel_map, ks - (KSTACK_GUARD_PAGES * PAGE_SIZE),
40733965Sjdp	    (pages + KSTACK_GUARD_PAGES) * PAGE_SIZE);
40833965Sjdp	td->td_kstack = 0;
40933965Sjdp}
41033965Sjdp
41133965Sjdp/*
41233965Sjdp * Allow a thread's kernel stack to be paged out.
41377298Sobrien */
41433965Sjdpvoid
41533965Sjdpvm_thread_swapout(struct thread *td)
41633965Sjdp{
41733965Sjdp	vm_object_t ksobj;
41833965Sjdp	vm_page_t m;
41933965Sjdp	int i, pages;
42033965Sjdp
42133965Sjdp	cpu_thread_swapout(td);
42233965Sjdp	pages = td->td_kstack_pages;
42333965Sjdp	ksobj = td->td_kstack_obj;
42433965Sjdp	pmap_qremove(td->td_kstack, pages);
42533965Sjdp	VM_OBJECT_LOCK(ksobj);
42633965Sjdp	for (i = 0; i < pages; i++) {
42733965Sjdp		m = vm_page_lookup(ksobj, i);
42838889Sjdp		if (m == NULL)
42938889Sjdp			panic("vm_thread_swapout: kstack already missing?");
43038889Sjdp		vm_page_lock_queues();
43138889Sjdp		vm_page_dirty(m);
43238889Sjdp		vm_page_unwire(m, 0);
43338889Sjdp		vm_page_unlock_queues();
43438889Sjdp	}
43538889Sjdp	VM_OBJECT_UNLOCK(ksobj);
43638889Sjdp}
43738889Sjdp
43838889Sjdp/*
43938889Sjdp * Bring the kernel stack for a specified thread back in.
44038889Sjdp */
44138889Sjdpvoid
44238889Sjdpvm_thread_swapin(struct thread *td)
44338889Sjdp{
44438889Sjdp	vm_object_t ksobj;
44538889Sjdp	vm_page_t m, ma[KSTACK_MAX_PAGES];
44677298Sobrien	int i, pages, rv;
44738889Sjdp
44838889Sjdp	pages = td->td_kstack_pages;
44938889Sjdp	ksobj = td->td_kstack_obj;
45038889Sjdp	VM_OBJECT_LOCK(ksobj);
45138889Sjdp	for (i = 0; i < pages; i++) {
45238889Sjdp		m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
45338889Sjdp		if (m->valid != VM_PAGE_BITS_ALL) {
45438889Sjdp			rv = vm_pager_get_pages(ksobj, &m, 1, 0);
45538889Sjdp			if (rv != VM_PAGER_OK)
45638889Sjdp				panic("vm_thread_swapin: cannot get kstack for proc: %d", td->td_proc->p_pid);
45777298Sobrien			m = vm_page_lookup(ksobj, i);
45838889Sjdp		}
45938889Sjdp		ma[i] = m;
46038889Sjdp		vm_page_lock_queues();
46138889Sjdp		vm_page_wire(m);
46277298Sobrien		vm_page_unlock_queues();
46338889Sjdp		vm_page_wakeup(m);
46438889Sjdp	}
46538889Sjdp	VM_OBJECT_UNLOCK(ksobj);
46638889Sjdp	pmap_qenter(td->td_kstack, ma, pages);
46738889Sjdp	cpu_thread_swapin(td);
46838889Sjdp}
46938889Sjdp
47038889Sjdp/*
47138889Sjdp * Set up a variable-sized alternate kstack.
47238889Sjdp */
47338889Sjdpint
47438889Sjdpvm_thread_new_altkstack(struct thread *td, int pages)
47538889Sjdp{
47638889Sjdp
47738889Sjdp	td->td_altkstack = td->td_kstack;
47838889Sjdp	td->td_altkstack_obj = td->td_kstack_obj;
47938889Sjdp	td->td_altkstack_pages = td->td_kstack_pages;
48033965Sjdp
48133965Sjdp	return (vm_thread_new(td, pages));
48233965Sjdp}
48333965Sjdp
48433965Sjdp/*
48533965Sjdp * Restore the original kstack.
48633965Sjdp */
48733965Sjdpvoid
48833965Sjdpvm_thread_dispose_altkstack(struct thread *td)
48933965Sjdp{
49033965Sjdp
49133965Sjdp	vm_thread_dispose(td);
49233965Sjdp
49333965Sjdp	td->td_kstack = td->td_altkstack;
49433965Sjdp	td->td_kstack_obj = td->td_altkstack_obj;
49533965Sjdp	td->td_kstack_pages = td->td_altkstack_pages;
49633965Sjdp	td->td_altkstack = 0;
49733965Sjdp	td->td_altkstack_obj = NULL;
49833965Sjdp	td->td_altkstack_pages = 0;
49933965Sjdp}
50033965Sjdp
50133965Sjdp/*
50233965Sjdp * Implement fork's actions on an address space.
50377298Sobrien * Here we arrange for the address space to be copied or referenced,
50433965Sjdp * allocate a user struct (pcb and kernel stack), then call the
50577298Sobrien * machine-dependent layer to fill those in and make the new process
50633965Sjdp * ready to run.  The new process is set up so that it returns directly
50733965Sjdp * to user mode to avoid stack copying and relocation problems.
50833965Sjdp */
50933965Sjdpint
51033965Sjdpvm_forkproc(td, p2, td2, vm2, flags)
51133965Sjdp	struct thread *td;
51233965Sjdp	struct proc *p2;
51333965Sjdp	struct thread *td2;
51433965Sjdp	struct vmspace *vm2;
51533965Sjdp	int flags;
51633965Sjdp{
51733965Sjdp	struct proc *p1 = td->td_proc;
51833965Sjdp	int error;
51933965Sjdp
52033965Sjdp	if ((flags & RFPROC) == 0) {
52133965Sjdp		/*
52233965Sjdp		 * Divorce the memory, if it is shared, essentially
52333965Sjdp		 * this changes shared memory amongst threads, into
52433965Sjdp		 * COW locally.
52577298Sobrien		 */
52677298Sobrien		if ((flags & RFMEM) == 0) {
52777298Sobrien			if (p1->p_vmspace->vm_refcnt > 1) {
52877298Sobrien				error = vmspace_unshare(p1);
52977298Sobrien				if (error)
53033965Sjdp					return (error);
53133965Sjdp			}
53233965Sjdp		}
53333965Sjdp		cpu_fork(td, p2, td2, flags);
53433965Sjdp		return (0);
53533965Sjdp	}
53633965Sjdp
53733965Sjdp	if (flags & RFMEM) {
53833965Sjdp		p2->p_vmspace = p1->p_vmspace;
53933965Sjdp		atomic_add_int(&p1->p_vmspace->vm_refcnt, 1);
54033965Sjdp	}
54133965Sjdp
54233965Sjdp	while (vm_page_count_severe()) {
54333965Sjdp		VM_WAIT;
54433965Sjdp	}
54533965Sjdp
54633965Sjdp	if ((flags & RFMEM) == 0) {
54733965Sjdp		p2->p_vmspace = vm2;
54833965Sjdp		if (p1->p_vmspace->vm_shm)
54933965Sjdp			shmfork(p1, p2);
55033965Sjdp	}
55133965Sjdp
55233965Sjdp	/*
55333965Sjdp	 * cpu_fork will copy and update the pcb, set up the kernel stack,
55433965Sjdp	 * and make the child ready to run.
55533965Sjdp	 */
55633965Sjdp	cpu_fork(td, p2, td2, flags);
55733965Sjdp	return (0);
55833965Sjdp}
55933965Sjdp
56033965Sjdp/*
56133965Sjdp * Called after process has been wait(2)'ed apon and is being reaped.
56233965Sjdp * The idea is to reclaim resources that we could not reclaim while
56333965Sjdp * the process was still executing.
56477298Sobrien */
56533965Sjdpvoid
56633965Sjdpvm_waitproc(p)
56733965Sjdp	struct proc *p;
56833965Sjdp{
56933965Sjdp
57033965Sjdp	vmspace_exitfree(p);		/* and clean-out the vmspace */
57133965Sjdp}
57233965Sjdp
57333965Sjdp/*
57433965Sjdp * Set default limits for VM system.
57533965Sjdp * Called for proc 0, and then inherited by all others.
57633965Sjdp *
57733965Sjdp * XXX should probably act directly on proc0.
57833965Sjdp */
57933965Sjdpstatic void
58033965Sjdpvm_init_limits(udata)
58133965Sjdp	void *udata;
58233965Sjdp{
58333965Sjdp	struct proc *p = udata;
58433965Sjdp	struct plimit *limp;
58533965Sjdp	int rss_limit;
58633965Sjdp
58733965Sjdp	/*
58833965Sjdp	 * Set up the initial limits on process VM. Set the maximum resident
58933965Sjdp	 * set size to be half of (reasonably) available memory.  Since this
59033965Sjdp	 * is a soft limit, it comes into effect only when the system is out
59133965Sjdp	 * of memory - half of main memory helps to favor smaller processes,
59233965Sjdp	 * and reduces thrashing of the object cache.
59333965Sjdp	 */
59433965Sjdp	limp = p->p_limit;
59533965Sjdp	limp->pl_rlimit[RLIMIT_STACK].rlim_cur = dflssiz;
59633965Sjdp	limp->pl_rlimit[RLIMIT_STACK].rlim_max = maxssiz;
59733965Sjdp	limp->pl_rlimit[RLIMIT_DATA].rlim_cur = dfldsiz;
59833965Sjdp	limp->pl_rlimit[RLIMIT_DATA].rlim_max = maxdsiz;
59933965Sjdp	/* limit the limit to no less than 2MB */
60033965Sjdp	rss_limit = max(cnt.v_free_count, 512);
60133965Sjdp	limp->pl_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
60233965Sjdp	limp->pl_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
60333965Sjdp}
60433965Sjdp
60533965Sjdpvoid
60633965Sjdpfaultin(p)
60733965Sjdp	struct proc *p;
60833965Sjdp{
60933965Sjdp#ifdef NO_SWAPPING
61077298Sobrien
61133965Sjdp	PROC_LOCK_ASSERT(p, MA_OWNED);
61233965Sjdp	if ((p->p_flag & P_INMEM) == 0)
61333965Sjdp		panic("faultin: proc swapped out with NO_SWAPPING!");
61433965Sjdp#else /* !NO_SWAPPING */
61533965Sjdp	struct thread *td;
61633965Sjdp
61733965Sjdp	PROC_LOCK_ASSERT(p, MA_OWNED);
61833965Sjdp	/*
61933965Sjdp	 * If another process is swapping in this process,
62033965Sjdp	 * just wait until it finishes.
62133965Sjdp	 */
62233965Sjdp	if (p->p_flag & P_SWAPPINGIN) {
62333965Sjdp		while (p->p_flag & P_SWAPPINGIN)
62433965Sjdp			msleep(&p->p_flag, &p->p_mtx, PVM, "faultin", 0);
62577298Sobrien		return;
62633965Sjdp	}
62733965Sjdp	if ((p->p_flag & P_INMEM) == 0) {
62833965Sjdp		/*
62977298Sobrien		 * Don't let another thread swap process p out while we are
63033965Sjdp		 * busy swapping it in.
63133965Sjdp		 */
63233965Sjdp		++p->p_lock;
63333965Sjdp		p->p_flag |= P_SWAPPINGIN;
63433965Sjdp		PROC_UNLOCK(p);
63533965Sjdp
63633965Sjdp		/*
63733965Sjdp		 * We hold no lock here because the list of threads
63833965Sjdp		 * can not change while all threads in the process are
63933965Sjdp		 * swapped out.
64033965Sjdp		 */
64133965Sjdp		FOREACH_THREAD_IN_PROC(p, td)
64233965Sjdp			vm_thread_swapin(td);
64333965Sjdp		PROC_LOCK(p);
64433965Sjdp		swapclear(p);
64533965Sjdp		p->p_swtick = ticks;
64633965Sjdp
64733965Sjdp		wakeup(&p->p_flag);
64833965Sjdp
64933965Sjdp		/* Allow other threads to swap p out now. */
65033965Sjdp		--p->p_lock;
65133965Sjdp	}
65233965Sjdp#endif /* NO_SWAPPING */
65333965Sjdp}
65433965Sjdp
65533965Sjdp/*
65633965Sjdp * This swapin algorithm attempts to swap-in processes only if there
65733965Sjdp * is enough space for them.  Of course, if a process waits for a long
65833965Sjdp * time, it will be swapped in anyway.
65933965Sjdp *
66033965Sjdp * Giant is held on entry.
66133965Sjdp */
66233965Sjdp/* ARGSUSED*/
66333965Sjdpstatic void
66433965Sjdpscheduler(dummy)
66533965Sjdp	void *dummy;
66633965Sjdp{
66733965Sjdp	struct proc *p;
66833965Sjdp	struct thread *td;
66933965Sjdp	struct proc *pp;
67033965Sjdp	int slptime;
67177298Sobrien	int swtime;
67233965Sjdp	int ppri;
67333965Sjdp	int pri;
67433965Sjdp
67533965Sjdp	mtx_assert(&Giant, MA_OWNED | MA_NOTRECURSED);
67633965Sjdp	mtx_unlock(&Giant);
67733965Sjdp
67833965Sjdploop:
67933965Sjdp	if (vm_page_count_min()) {
68033965Sjdp		VM_WAIT;
68133965Sjdp		goto loop;
68233965Sjdp	}
68333965Sjdp
68477298Sobrien	pp = NULL;
68533965Sjdp	ppri = INT_MIN;
68633965Sjdp	sx_slock(&allproc_lock);
68733965Sjdp	FOREACH_PROC_IN_SYSTEM(p) {
68833965Sjdp		PROC_LOCK(p);
68933965Sjdp		if (p->p_flag & (P_SWAPPINGOUT | P_SWAPPINGIN | P_INMEM)) {
69033965Sjdp			PROC_UNLOCK(p);
69133965Sjdp			continue;
69233965Sjdp		}
69333965Sjdp		swtime = (ticks - p->p_swtick) / hz;
69433965Sjdp		FOREACH_THREAD_IN_PROC(p, td) {
69533965Sjdp			/*
69633965Sjdp			 * An otherwise runnable thread of a process
69733965Sjdp			 * swapped out has only the TDI_SWAPPED bit set.
69833965Sjdp			 *
69933965Sjdp			 */
70033965Sjdp			thread_lock(td);
70133965Sjdp			if (td->td_inhibitors == TDI_SWAPPED) {
70233965Sjdp				slptime = (ticks - td->td_slptick) / hz;
70333965Sjdp				pri = swtime + slptime;
70433965Sjdp				if ((td->td_flags & TDF_SWAPINREQ) == 0)
70533965Sjdp					pri -= p->p_nice * 8;
70633965Sjdp				/*
70733965Sjdp				 * if this thread is higher priority
70833965Sjdp				 * and there is enough space, then select
70933965Sjdp				 * this process instead of the previous
71033965Sjdp				 * selection.
71133965Sjdp				 */
71277298Sobrien				if (pri > ppri) {
71333965Sjdp					pp = p;
71433965Sjdp					ppri = pri;
71533965Sjdp				}
71633965Sjdp			}
71733965Sjdp			thread_unlock(td);
71833965Sjdp		}
71933965Sjdp		PROC_UNLOCK(p);
72033965Sjdp	}
72138889Sjdp	sx_sunlock(&allproc_lock);
72233965Sjdp
72338889Sjdp	/*
72477298Sobrien	 * Nothing to do, back to sleep.
72538889Sjdp	 */
72638889Sjdp	if ((p = pp) == NULL) {
72733965Sjdp		tsleep(&proc0, PVM, "sched", maxslp * hz / 2);
72833965Sjdp		goto loop;
72977298Sobrien	}
73038889Sjdp	PROC_LOCK(p);
73138889Sjdp
73277298Sobrien	/*
73338889Sjdp	 * Another process may be bringing or may have already
73477298Sobrien	 * brought this process in while we traverse all threads.
73538889Sjdp	 * Or, this process may even be being swapped out again.
73638889Sjdp	 */
73738889Sjdp	if (p->p_flag & (P_INMEM | P_SWAPPINGOUT | P_SWAPPINGIN)) {
73838889Sjdp		PROC_UNLOCK(p);
73938889Sjdp		goto loop;
74038889Sjdp	}
74138889Sjdp
74238889Sjdp	/*
74338889Sjdp	 * We would like to bring someone in. (only if there is space).
74438889Sjdp	 * [What checks the space? ]
74538889Sjdp	 */
74638889Sjdp	faultin(p);
74738889Sjdp	PROC_UNLOCK(p);
74838889Sjdp	goto loop;
74938889Sjdp}
75038889Sjdp
75138889Sjdpvoid
75238889Sjdpkick_proc0(void)
75338889Sjdp{
75438889Sjdp
75538889Sjdp	wakeup(&proc0);
75638889Sjdp}
75777298Sobrien
75838889Sjdp#ifndef NO_SWAPPING
75938889Sjdp
76038889Sjdp/*
76138889Sjdp * Swap_idle_threshold1 is the guaranteed swapped in time for a process
76238889Sjdp */
76338889Sjdpstatic int swap_idle_threshold1 = 2;
76438889SjdpSYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold1, CTLFLAG_RW,
76538889Sjdp    &swap_idle_threshold1, 0, "Guaranteed swapped in time for a process");
76638889Sjdp
76738889Sjdp/*
76838889Sjdp * Swap_idle_threshold2 is the time that a process can be idle before
76977298Sobrien * it will be swapped out, if idle swapping is enabled.
77038889Sjdp */
77177298Sobrienstatic int swap_idle_threshold2 = 10;
77277298SobrienSYSCTL_INT(_vm, OID_AUTO, swap_idle_threshold2, CTLFLAG_RW,
77338889Sjdp    &swap_idle_threshold2, 0, "Time before a process will be swapped out");
77433965Sjdp
77533965Sjdp/*
77677298Sobrien * Swapout is driven by the pageout daemon.  Very simple, we find eligible
77733965Sjdp * procs and swap out their stacks.  We try to always "swap" at least one
77877298Sobrien * process in case we need the room for a swapin.
77933965Sjdp * If any procs have been sleeping/stopped for at least maxslp seconds,
78077298Sobrien * they are swapped.  Else, we swap the longest-sleeping or stopped process,
78133965Sjdp * if any, otherwise the longest-resident process.
78277298Sobrien */
78377298Sobrienvoid
78433965Sjdpswapout_procs(action)
78533965Sjdpint action;
78633965Sjdp{
78777298Sobrien	struct proc *p;
78833965Sjdp	struct thread *td;
78933965Sjdp	int didswap = 0;
79077298Sobrien
79133965Sjdpretry:
79233965Sjdp	sx_slock(&allproc_lock);
79377298Sobrien	FOREACH_PROC_IN_SYSTEM(p) {
79477298Sobrien		struct vmspace *vm;
79533965Sjdp		int minslptime = 100000;
79633965Sjdp		int slptime;
79733965Sjdp
79833965Sjdp		/*
79938889Sjdp		 * Watch out for a process in
80033965Sjdp		 * creation.  It may have no
80177298Sobrien		 * address space or lock yet.
80233965Sjdp		 */
80333965Sjdp		if (p->p_state == PRS_NEW)
80433965Sjdp			continue;
80538889Sjdp		/*
80677298Sobrien		 * An aio daemon switches its
80738889Sjdp		 * address space while running.
80877298Sobrien		 * Perform a quick check whether
80933965Sjdp		 * a process has P_SYSTEM.
81038889Sjdp		 */
81177298Sobrien		if ((p->p_flag & P_SYSTEM) != 0)
81277298Sobrien			continue;
81377298Sobrien		/*
81477298Sobrien		 * Do not swapout a process that
81577298Sobrien		 * is waiting for VM data
81677298Sobrien		 * structures as there is a possible
81777298Sobrien		 * deadlock.  Test this first as
81877298Sobrien		 * this may block.
81977298Sobrien		 *
82077298Sobrien		 * Lock the map until swapout
82138889Sjdp		 * finishes, or a thread of this
82277298Sobrien		 * process may attempt to alter
82377298Sobrien		 * the map.
82477298Sobrien		 */
82538889Sjdp		vm = vmspace_acquire_ref(p);
82633965Sjdp		if (vm == NULL)
82738889Sjdp			continue;
82838889Sjdp		if (!vm_map_trylock(&vm->vm_map))
82938889Sjdp			goto nextproc1;
83033965Sjdp
83138889Sjdp		PROC_LOCK(p);
83238889Sjdp		if (p->p_lock != 0 ||
83338889Sjdp		    (p->p_flag & (P_STOPPED_SINGLE|P_TRACED|P_SYSTEM|P_WEXIT)
83433965Sjdp		    ) != 0) {
83533965Sjdp			goto nextproc;
83638889Sjdp		}
83738889Sjdp		/*
83838889Sjdp		 * only aiod changes vmspace, however it will be
83933965Sjdp		 * skipped because of the if statement above checking
84038889Sjdp		 * for P_SYSTEM
84138889Sjdp		 */
84238889Sjdp		if ((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) != P_INMEM)
84360484Sobrien			goto nextproc;
84460484Sobrien
84560484Sobrien		switch (p->p_state) {
84660484Sobrien		default:
84733965Sjdp			/* Don't swap out processes in any sort
84838889Sjdp			 * of 'special' state. */
84960484Sobrien			break;
85033965Sjdp
85133965Sjdp		case PRS_NORMAL:
85233965Sjdp			/*
85333965Sjdp			 * do not swapout a realtime process
85433965Sjdp			 * Check all the thread groups..
85533965Sjdp			 */
85638889Sjdp			FOREACH_THREAD_IN_PROC(p, td) {
85738889Sjdp				thread_lock(td);
85838889Sjdp				if (PRI_IS_REALTIME(td->td_pri_class)) {
85933965Sjdp					thread_unlock(td);
86033965Sjdp					goto nextproc;
86133965Sjdp				}
86233965Sjdp				slptime = (ticks - td->td_slptick) / hz;
86338889Sjdp				/*
86433965Sjdp				 * Guarantee swap_idle_threshold1
86533965Sjdp				 * time in memory.
86633965Sjdp				 */
86733965Sjdp				if (slptime < swap_idle_threshold1) {
86833965Sjdp					thread_unlock(td);
86933965Sjdp					goto nextproc;
87060484Sobrien				}
87160484Sobrien
87260484Sobrien				/*
87360484Sobrien				 * Do not swapout a process if it is
87460484Sobrien				 * waiting on a critical event of some
87560484Sobrien				 * kind or there is a thread whose
87660484Sobrien				 * pageable memory may be accessed.
87760484Sobrien				 *
87860484Sobrien				 * This could be refined to support
87960484Sobrien				 * swapping out a thread.
88060484Sobrien				 */
88160484Sobrien				if (!thread_safetoswapout(td)) {
88260484Sobrien					thread_unlock(td);
88360484Sobrien					goto nextproc;
88460484Sobrien				}
88560484Sobrien				/*
88660484Sobrien				 * If the system is under memory stress,
88760484Sobrien				 * or if we are swapping
88860484Sobrien				 * idle processes >= swap_idle_threshold2,
88960484Sobrien				 * then swap the process out.
89077298Sobrien				 */
89133965Sjdp				if (((action & VM_SWAP_NORMAL) == 0) &&
89233965Sjdp				    (((action & VM_SWAP_IDLE) == 0) ||
89333965Sjdp				    (slptime < swap_idle_threshold2))) {
89477298Sobrien					thread_unlock(td);
89533965Sjdp					goto nextproc;
89633965Sjdp				}
89733965Sjdp
89833965Sjdp				if (minslptime > slptime)
89933965Sjdp					minslptime = slptime;
90033965Sjdp				thread_unlock(td);
90133965Sjdp			}
90233965Sjdp
90333965Sjdp			/*
90433965Sjdp			 * If the pageout daemon didn't free enough pages,
90533965Sjdp			 * or if this process is idle and the system is
90633965Sjdp			 * configured to swap proactively, swap it out.
90733965Sjdp			 */
90833965Sjdp			if ((action & VM_SWAP_NORMAL) ||
90933965Sjdp				((action & VM_SWAP_IDLE) &&
91033965Sjdp				 (minslptime > swap_idle_threshold2))) {
91133965Sjdp				if (swapout(p) == 0)
91233965Sjdp					didswap++;
91333965Sjdp				PROC_UNLOCK(p);
91433965Sjdp				vm_map_unlock(&vm->vm_map);
91533965Sjdp				vmspace_free(vm);
91633965Sjdp				sx_sunlock(&allproc_lock);
91733965Sjdp				goto retry;
91833965Sjdp			}
91933965Sjdp		}
92033965Sjdpnextproc:
92133965Sjdp		PROC_UNLOCK(p);
92233965Sjdp		vm_map_unlock(&vm->vm_map);
92333965Sjdpnextproc1:
92433965Sjdp		vmspace_free(vm);
92533965Sjdp		continue;
92633965Sjdp	}
92733965Sjdp	sx_sunlock(&allproc_lock);
92833965Sjdp	/*
92933965Sjdp	 * If we swapped something out, and another process needed memory,
93033965Sjdp	 * then wakeup the sched process.
93133965Sjdp	 */
93233965Sjdp	if (didswap)
93333965Sjdp		wakeup(&proc0);
93433965Sjdp}
93533965Sjdp
93633965Sjdpstatic void
93733965Sjdpswapclear(p)
93877298Sobrien	struct proc *p;
93933965Sjdp{
94033965Sjdp	struct thread *td;
94133965Sjdp
94233965Sjdp	PROC_LOCK_ASSERT(p, MA_OWNED);
94333965Sjdp
94433965Sjdp	FOREACH_THREAD_IN_PROC(p, td) {
94533965Sjdp		thread_lock(td);
94633965Sjdp		td->td_flags |= TDF_INMEM;
94733965Sjdp		td->td_flags &= ~TDF_SWAPINREQ;
94833965Sjdp		TD_CLR_SWAPPED(td);
94933965Sjdp		if (TD_CAN_RUN(td))
95033965Sjdp			if (setrunnable(td)) {
95133965Sjdp#ifdef INVARIANTS
95233965Sjdp				/*
95333965Sjdp				 * XXX: We just cleared TDI_SWAPPED
95433965Sjdp				 * above and set TDF_INMEM, so this
95533965Sjdp				 * should never happen.
95633965Sjdp				 */
95733965Sjdp				panic("not waking up swapper");
95833965Sjdp#endif
95933965Sjdp			}
96033965Sjdp		thread_unlock(td);
96133965Sjdp	}
96233965Sjdp	p->p_flag &= ~(P_SWAPPINGIN|P_SWAPPINGOUT);
96333965Sjdp	p->p_flag |= P_INMEM;
96433965Sjdp}
96533965Sjdp
96633965Sjdpstatic int
96733965Sjdpswapout(p)
96833965Sjdp	struct proc *p;
96977298Sobrien{
97033965Sjdp	struct thread *td;
97133965Sjdp
97233965Sjdp	PROC_LOCK_ASSERT(p, MA_OWNED);
97333965Sjdp#if defined(SWAP_DEBUG)
97433965Sjdp	printf("swapping out %d\n", p->p_pid);
97533965Sjdp#endif
97633965Sjdp
97733965Sjdp	/*
97833965Sjdp	 * The states of this process and its threads may have changed
97977298Sobrien	 * by now.  Assuming that there is only one pageout daemon thread,
98033965Sjdp	 * this process should still be in memory.
98177298Sobrien	 */
98277298Sobrien	KASSERT((p->p_flag & (P_INMEM|P_SWAPPINGOUT|P_SWAPPINGIN)) == P_INMEM,
98377298Sobrien		("swapout: lost a swapout race?"));
98433965Sjdp
98560484Sobrien	/*
98660484Sobrien	 * remember the process resident count
98760484Sobrien	 */
98860484Sobrien	p->p_vmspace->vm_swrss = vmspace_resident_count(p->p_vmspace);
98960484Sobrien	/*
99060484Sobrien	 * Check and mark all threads before we proceed.
99160484Sobrien	 */
99260484Sobrien	p->p_flag &= ~P_INMEM;
99360484Sobrien	p->p_flag |= P_SWAPPINGOUT;
99460484Sobrien	FOREACH_THREAD_IN_PROC(p, td) {
99560484Sobrien		thread_lock(td);
99660484Sobrien		if (!thread_safetoswapout(td)) {
99760484Sobrien			thread_unlock(td);
99860484Sobrien			swapclear(p);
99960484Sobrien			return (EBUSY);
100060484Sobrien		}
100177298Sobrien		td->td_flags &= ~TDF_INMEM;
100260484Sobrien		TD_SET_SWAPPED(td);
100377298Sobrien		thread_unlock(td);
100477298Sobrien	}
100560484Sobrien	td = FIRST_THREAD_IN_PROC(p);
100660484Sobrien	++td->td_ru.ru_nswap;
100760484Sobrien	PROC_UNLOCK(p);
100860484Sobrien
100960484Sobrien	/*
101060484Sobrien	 * This list is stable because all threads are now prevented from
101160484Sobrien	 * running.  The list is only modified in the context of a running
101260484Sobrien	 * thread in this process.
101360484Sobrien	 */
101460484Sobrien	FOREACH_THREAD_IN_PROC(p, td)
101560484Sobrien		vm_thread_swapout(td);
101660484Sobrien
101777298Sobrien	PROC_LOCK(p);
101860484Sobrien	p->p_flag &= ~P_SWAPPINGOUT;
101960484Sobrien	p->p_swtick = ticks;
102060484Sobrien	return (0);
102160484Sobrien}
102260484Sobrien#endif /* !NO_SWAPPING */
102360484Sobrien