xref: /freebsd/sys/vm/vm_glue.c (revision 5856e12e6950cdbbf627ab23ddc0d10006d8dd75)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
17df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_glue.c	8.6 (Berkeley) 1/5/94
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
43df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
44df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
45df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
46df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55df8bae1dSRodney W. Grimes  *  School of Computer Science
56df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
57df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
60df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
613c4dd356SDavid Greenman  *
625856e12eSJohn Dyson  * $Id: vm_glue.c,v 1.62 1997/04/07 07:16:04 peter Exp $
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65e9822d92SJoerg Wunsch #include "opt_rlimit.h"
66e9822d92SJoerg Wunsch 
67df8bae1dSRodney W. Grimes #include <sys/param.h>
68df8bae1dSRodney W. Grimes #include <sys/systm.h>
69df8bae1dSRodney W. Grimes #include <sys/proc.h>
70df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
71df8bae1dSRodney W. Grimes #include <sys/buf.h>
723aa12267SBruce Evans #include <sys/shm.h>
73efeaf95aSDavid Greenman #include <sys/vmmeter.h>
74df8bae1dSRodney W. Grimes 
7526f9a767SRodney W. Grimes #include <sys/kernel.h>
7626f9a767SRodney W. Grimes #include <sys/dkstat.h>
77a2a1c95cSPeter Wemm #include <sys/unistd.h>
7826f9a767SRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <vm/vm.h>
80efeaf95aSDavid Greenman #include <vm/vm_param.h>
81efeaf95aSDavid Greenman #include <vm/vm_inherit.h>
82efeaf95aSDavid Greenman #include <vm/vm_prot.h>
83996c772fSJohn Dyson #include <sys/lock.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8726f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
88df8bae1dSRodney W. Grimes #include <vm/vm_kern.h>
89efeaf95aSDavid Greenman #include <vm/vm_extern.h>
90bd7e5f99SJohn Dyson #include <vm/vm_object.h>
91bd7e5f99SJohn Dyson #include <vm/vm_pager.h>
92efeaf95aSDavid Greenman 
93efeaf95aSDavid Greenman #include <sys/user.h>
94df8bae1dSRodney W. Grimes 
952b14f991SJulian Elischer /*
962b14f991SJulian Elischer  * System initialization
972b14f991SJulian Elischer  *
982b14f991SJulian Elischer  * Note: proc0 from proc.h
992b14f991SJulian Elischer  */
1002b14f991SJulian Elischer 
1014590fd3aSDavid Greenman static void vm_init_limits __P((void *));
1024590fd3aSDavid Greenman SYSINIT(vm_limits, SI_SUB_VM_CONF, SI_ORDER_FIRST, vm_init_limits, &proc0)
1032b14f991SJulian Elischer 
1042b14f991SJulian Elischer /*
1052b14f991SJulian Elischer  * THIS MUST BE THE LAST INITIALIZATION ITEM!!!
1062b14f991SJulian Elischer  *
1072b14f991SJulian Elischer  * Note: run scheduling should be divorced from the vm system.
1082b14f991SJulian Elischer  */
1094590fd3aSDavid Greenman static void scheduler __P((void *));
1102b14f991SJulian Elischer SYSINIT(scheduler, SI_SUB_RUN_SCHEDULER, SI_ORDER_FIRST, scheduler, NULL)
1112b14f991SJulian Elischer 
1122b14f991SJulian Elischer 
113f708ef1bSPoul-Henning Kamp static void swapout __P((struct proc *));
114f708ef1bSPoul-Henning Kamp 
11526f9a767SRodney W. Grimes extern char kstack[];
1160d94caffSDavid Greenman 
11726f9a767SRodney W. Grimes /* vm_map_t upages_map; */
118df8bae1dSRodney W. Grimes 
119df8bae1dSRodney W. Grimes int
120df8bae1dSRodney W. Grimes kernacc(addr, len, rw)
121df8bae1dSRodney W. Grimes 	caddr_t addr;
122df8bae1dSRodney W. Grimes 	int len, rw;
123df8bae1dSRodney W. Grimes {
124df8bae1dSRodney W. Grimes 	boolean_t rv;
125df8bae1dSRodney W. Grimes 	vm_offset_t saddr, eaddr;
126df8bae1dSRodney W. Grimes 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
127df8bae1dSRodney W. Grimes 
128df8bae1dSRodney W. Grimes 	saddr = trunc_page(addr);
129df8bae1dSRodney W. Grimes 	eaddr = round_page(addr + len);
130bc0d3334SJohn Dyson 	vm_map_lock_read(kernel_map);
131df8bae1dSRodney W. Grimes 	rv = vm_map_check_protection(kernel_map, saddr, eaddr, prot);
132bc0d3334SJohn Dyson 	vm_map_unlock_read(kernel_map);
133df8bae1dSRodney W. Grimes 	return (rv == TRUE);
134df8bae1dSRodney W. Grimes }
135df8bae1dSRodney W. Grimes 
136df8bae1dSRodney W. Grimes int
137df8bae1dSRodney W. Grimes useracc(addr, len, rw)
138df8bae1dSRodney W. Grimes 	caddr_t addr;
139df8bae1dSRodney W. Grimes 	int len, rw;
140df8bae1dSRodney W. Grimes {
141df8bae1dSRodney W. Grimes 	boolean_t rv;
142df8bae1dSRodney W. Grimes 	vm_prot_t prot = rw == B_READ ? VM_PROT_READ : VM_PROT_WRITE;
143bc0d3334SJohn Dyson 	vm_map_t map;
144bc0d3334SJohn Dyson 	vm_map_entry_t save_hint;
145df8bae1dSRodney W. Grimes 
14626f9a767SRodney W. Grimes 	/*
147bbc0ec52SDavid Greenman 	 * XXX - check separately to disallow access to user area and user
148bbc0ec52SDavid Greenman 	 * page tables - they are in the map.
14926f9a767SRodney W. Grimes 	 *
1500d94caffSDavid Greenman 	 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max.  It was once
1510d94caffSDavid Greenman 	 * only used (as an end address) in trap.c.  Use it as an end address
1520d94caffSDavid Greenman 	 * here too.  This bogusness has spread.  I just fixed where it was
1530d94caffSDavid Greenman 	 * used as a max in vm_mmap.c.
15426f9a767SRodney W. Grimes 	 */
155bbc0ec52SDavid Greenman 	if ((vm_offset_t) addr + len > /* XXX */ VM_MAXUSER_ADDRESS
156bbc0ec52SDavid Greenman 	    || (vm_offset_t) addr + len < (vm_offset_t) addr) {
15726f9a767SRodney W. Grimes 		return (FALSE);
15826f9a767SRodney W. Grimes 	}
159bc0d3334SJohn Dyson 	map = &curproc->p_vmspace->vm_map;
160bc0d3334SJohn Dyson 	vm_map_lock_read(map);
161bc0d3334SJohn Dyson 	/*
162bc0d3334SJohn Dyson 	 * We save the map hint, and restore it.  Useracc appears to distort
163bc0d3334SJohn Dyson 	 * the map hint unnecessarily.
164bc0d3334SJohn Dyson 	 */
165bc0d3334SJohn Dyson 	save_hint = map->hint;
166bc0d3334SJohn Dyson 	rv = vm_map_check_protection(map,
167df8bae1dSRodney W. Grimes 	    trunc_page(addr), round_page(addr + len), prot);
168bc0d3334SJohn Dyson 	map->hint = save_hint;
169bc0d3334SJohn Dyson 	vm_map_unlock_read(map);
170bc0d3334SJohn Dyson 
171df8bae1dSRodney W. Grimes 	return (rv == TRUE);
172df8bae1dSRodney W. Grimes }
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes void
175df8bae1dSRodney W. Grimes vslock(addr, len)
176df8bae1dSRodney W. Grimes 	caddr_t addr;
177df8bae1dSRodney W. Grimes 	u_int len;
178df8bae1dSRodney W. Grimes {
179df8bae1dSRodney W. Grimes 	vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr),
180df8bae1dSRodney W. Grimes 	    round_page(addr + len), FALSE);
181df8bae1dSRodney W. Grimes }
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes void
184df8bae1dSRodney W. Grimes vsunlock(addr, len, dirtied)
185df8bae1dSRodney W. Grimes 	caddr_t addr;
186df8bae1dSRodney W. Grimes 	u_int len;
187df8bae1dSRodney W. Grimes 	int dirtied;
188df8bae1dSRodney W. Grimes {
189df8bae1dSRodney W. Grimes #ifdef	lint
190df8bae1dSRodney W. Grimes 	dirtied++;
1910d94caffSDavid Greenman #endif	/* lint */
192df8bae1dSRodney W. Grimes 	vm_map_pageable(&curproc->p_vmspace->vm_map, trunc_page(addr),
193df8bae1dSRodney W. Grimes 	    round_page(addr + len), TRUE);
194df8bae1dSRodney W. Grimes }
195df8bae1dSRodney W. Grimes 
196df8bae1dSRodney W. Grimes /*
197df8bae1dSRodney W. Grimes  * Implement fork's actions on an address space.
198df8bae1dSRodney W. Grimes  * Here we arrange for the address space to be copied or referenced,
199df8bae1dSRodney W. Grimes  * allocate a user struct (pcb and kernel stack), then call the
200df8bae1dSRodney W. Grimes  * machine-dependent layer to fill those in and make the new process
201a2a1c95cSPeter Wemm  * ready to run.  The new process is set up so that it returns directly
202a2a1c95cSPeter Wemm  * to user mode to avoid stack copying and relocation problems.
203df8bae1dSRodney W. Grimes  */
204a2a1c95cSPeter Wemm void
205a2a1c95cSPeter Wemm vm_fork(p1, p2, flags)
206df8bae1dSRodney W. Grimes 	register struct proc *p1, *p2;
207a2a1c95cSPeter Wemm 	int flags;
208df8bae1dSRodney W. Grimes {
209df8bae1dSRodney W. Grimes 	register struct user *up;
210f35329acSJohn Dyson 	int i;
211bd7e5f99SJohn Dyson 	pmap_t pvp;
212b18bfc3dSJohn Dyson 	vm_object_t upobj;
213df8bae1dSRodney W. Grimes 
2145856e12eSJohn Dyson 	if (flags & RFMEM) {
2155856e12eSJohn Dyson 		p2->p_vmspace = p1->p_vmspace;
2165856e12eSJohn Dyson 		p1->p_vmspace->vm_refcnt++;
2175856e12eSJohn Dyson 	}
2185856e12eSJohn Dyson 
2190d94caffSDavid Greenman 	while ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) {
22026f9a767SRodney W. Grimes 		VM_WAIT;
2210d94caffSDavid Greenman 	}
22226f9a767SRodney W. Grimes 
2235856e12eSJohn Dyson 	if ((flags & RFMEM) == 0) {
224df8bae1dSRodney W. Grimes 		p2->p_vmspace = vmspace_fork(p1->p_vmspace);
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes 		if (p1->p_vmspace->vm_shm)
227dabee6feSPeter Wemm 			shmfork(p1, p2);
228a2a1c95cSPeter Wemm 	}
229df8bae1dSRodney W. Grimes 
230675878e7SJohn Dyson 	pmap_new_proc(p2);
23126f9a767SRodney W. Grimes 
232675878e7SJohn Dyson 	up = p2->p_addr;
233df8bae1dSRodney W. Grimes 
234df8bae1dSRodney W. Grimes 	/*
2350d94caffSDavid Greenman 	 * p_stats and p_sigacts currently point at fields in the user struct
2360d94caffSDavid Greenman 	 * but not at &u, instead at p_addr. Copy p_sigacts and parts of
2370d94caffSDavid Greenman 	 * p_stats; zero the rest of p_stats (statistics).
238df8bae1dSRodney W. Grimes 	 */
239df8bae1dSRodney W. Grimes 	p2->p_stats = &up->u_stats;
240df8bae1dSRodney W. Grimes 	p2->p_sigacts = &up->u_sigacts;
241df8bae1dSRodney W. Grimes 	up->u_sigacts = *p1->p_sigacts;
242df8bae1dSRodney W. Grimes 	bzero(&up->u_stats.pstat_startzero,
243df8bae1dSRodney W. Grimes 	    (unsigned) ((caddr_t) &up->u_stats.pstat_endzero -
244df8bae1dSRodney W. Grimes 		(caddr_t) &up->u_stats.pstat_startzero));
245df8bae1dSRodney W. Grimes 	bcopy(&p1->p_stats->pstat_startcopy, &up->u_stats.pstat_startcopy,
246df8bae1dSRodney W. Grimes 	    ((caddr_t) &up->u_stats.pstat_endcopy -
247df8bae1dSRodney W. Grimes 		(caddr_t) &up->u_stats.pstat_startcopy));
248df8bae1dSRodney W. Grimes 
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes 	/*
251a2a1c95cSPeter Wemm 	 * cpu_fork will copy and update the pcb, set up the kernel stack,
252a2a1c95cSPeter Wemm 	 * and make the child ready to run.
253df8bae1dSRodney W. Grimes 	 */
254a2a1c95cSPeter Wemm 	cpu_fork(p1, p2);
255df8bae1dSRodney W. Grimes }
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes /*
258df8bae1dSRodney W. Grimes  * Set default limits for VM system.
259df8bae1dSRodney W. Grimes  * Called for proc 0, and then inherited by all others.
2602b14f991SJulian Elischer  *
2612b14f991SJulian Elischer  * XXX should probably act directly on proc0.
262df8bae1dSRodney W. Grimes  */
2632b14f991SJulian Elischer static void
2642b14f991SJulian Elischer vm_init_limits(udata)
2654590fd3aSDavid Greenman 	void *udata;
266df8bae1dSRodney W. Grimes {
267d841aaa7SBruce Evans 	register struct proc *p = udata;
268bbc0ec52SDavid Greenman 	int rss_limit;
269df8bae1dSRodney W. Grimes 
270df8bae1dSRodney W. Grimes 	/*
2710d94caffSDavid Greenman 	 * Set up the initial limits on process VM. Set the maximum resident
2720d94caffSDavid Greenman 	 * set size to be half of (reasonably) available memory.  Since this
2730d94caffSDavid Greenman 	 * is a soft limit, it comes into effect only when the system is out
2740d94caffSDavid Greenman 	 * of memory - half of main memory helps to favor smaller processes,
275bbc0ec52SDavid Greenman 	 * and reduces thrashing of the object cache.
276df8bae1dSRodney W. Grimes 	 */
277df8bae1dSRodney W. Grimes 	p->p_rlimit[RLIMIT_STACK].rlim_cur = DFLSSIZ;
278df8bae1dSRodney W. Grimes 	p->p_rlimit[RLIMIT_STACK].rlim_max = MAXSSIZ;
279df8bae1dSRodney W. Grimes 	p->p_rlimit[RLIMIT_DATA].rlim_cur = DFLDSIZ;
280df8bae1dSRodney W. Grimes 	p->p_rlimit[RLIMIT_DATA].rlim_max = MAXDSIZ;
281dd0bd066SDavid Greenman 	/* limit the limit to no less than 2MB */
282f2daac0cSDavid Greenman 	rss_limit = max(cnt.v_free_count, 512);
283bbc0ec52SDavid Greenman 	p->p_rlimit[RLIMIT_RSS].rlim_cur = ptoa(rss_limit);
28426f9a767SRodney W. Grimes 	p->p_rlimit[RLIMIT_RSS].rlim_max = RLIM_INFINITY;
285df8bae1dSRodney W. Grimes }
286df8bae1dSRodney W. Grimes 
28726f9a767SRodney W. Grimes void
28826f9a767SRodney W. Grimes faultin(p)
28926f9a767SRodney W. Grimes 	struct proc *p;
29026f9a767SRodney W. Grimes {
29126f9a767SRodney W. Grimes 	vm_offset_t i;
29226f9a767SRodney W. Grimes 	int s;
29326f9a767SRodney W. Grimes 
29426f9a767SRodney W. Grimes 	if ((p->p_flag & P_INMEM) == 0) {
29526f9a767SRodney W. Grimes 
29626f9a767SRodney W. Grimes 		++p->p_lock;
29726f9a767SRodney W. Grimes 
298675878e7SJohn Dyson 		pmap_swapin_proc(p);
29926f9a767SRodney W. Grimes 
30026f9a767SRodney W. Grimes 		s = splhigh();
30126f9a767SRodney W. Grimes 
30226f9a767SRodney W. Grimes 		if (p->p_stat == SRUN)
30326f9a767SRodney W. Grimes 			setrunqueue(p);
30426f9a767SRodney W. Grimes 
30526f9a767SRodney W. Grimes 		p->p_flag |= P_INMEM;
30626f9a767SRodney W. Grimes 
30726f9a767SRodney W. Grimes 		/* undo the effect of setting SLOCK above */
30826f9a767SRodney W. Grimes 		--p->p_lock;
30926f9a767SRodney W. Grimes 		splx(s);
31026f9a767SRodney W. Grimes 
31126f9a767SRodney W. Grimes 	}
31226f9a767SRodney W. Grimes }
31326f9a767SRodney W. Grimes 
314df8bae1dSRodney W. Grimes /*
31526f9a767SRodney W. Grimes  * This swapin algorithm attempts to swap-in processes only if there
31626f9a767SRodney W. Grimes  * is enough space for them.  Of course, if a process waits for a long
31726f9a767SRodney W. Grimes  * time, it will be swapped in anyway.
318df8bae1dSRodney W. Grimes  */
3192b14f991SJulian Elischer /* ARGSUSED*/
3202b14f991SJulian Elischer static void
321d841aaa7SBruce Evans scheduler(dummy)
322d841aaa7SBruce Evans 	void *dummy;
323df8bae1dSRodney W. Grimes {
324df8bae1dSRodney W. Grimes 	register struct proc *p;
325df8bae1dSRodney W. Grimes 	register int pri;
326df8bae1dSRodney W. Grimes 	struct proc *pp;
327df8bae1dSRodney W. Grimes 	int ppri;
328df8bae1dSRodney W. Grimes 
329df8bae1dSRodney W. Grimes loop:
3309ea85708SJohn Dyson 	while ((cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) {
3310d94caffSDavid Greenman 		VM_WAIT;
3320d94caffSDavid Greenman 	}
33326f9a767SRodney W. Grimes 
334df8bae1dSRodney W. Grimes 	pp = NULL;
335df8bae1dSRodney W. Grimes 	ppri = INT_MIN;
3361b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
337bd7e5f99SJohn Dyson 		if (p->p_stat == SRUN &&
338bd7e5f99SJohn Dyson 			(p->p_flag & (P_INMEM | P_SWAPPING)) == 0) {
33926f9a767SRodney W. Grimes 			int mempri;
3400d94caffSDavid Greenman 
341a669a6e9SJohn Dyson 			pri = p->p_swtime + p->p_slptime;
342a669a6e9SJohn Dyson 			if ((p->p_flag & P_SWAPINREQ) == 0) {
343a669a6e9SJohn Dyson 				pri -= p->p_nice * 8;
344a669a6e9SJohn Dyson 			}
34526f9a767SRodney W. Grimes 			mempri = pri > 0 ? pri : 0;
34626f9a767SRodney W. Grimes 			/*
34726f9a767SRodney W. Grimes 			 * if this process is higher priority and there is
3480d94caffSDavid Greenman 			 * enough space, then select this process instead of
3490d94caffSDavid Greenman 			 * the previous selection.
35026f9a767SRodney W. Grimes 			 */
3510d94caffSDavid Greenman 			if (pri > ppri) {
352df8bae1dSRodney W. Grimes 				pp = p;
353df8bae1dSRodney W. Grimes 				ppri = pri;
354df8bae1dSRodney W. Grimes 			}
355df8bae1dSRodney W. Grimes 		}
356df8bae1dSRodney W. Grimes 	}
35726f9a767SRodney W. Grimes 
358df8bae1dSRodney W. Grimes 	/*
359a669a6e9SJohn Dyson 	 * Nothing to do, back to sleep.
360df8bae1dSRodney W. Grimes 	 */
361df8bae1dSRodney W. Grimes 	if ((p = pp) == NULL) {
36224a1cce3SDavid Greenman 		tsleep(&proc0, PVM, "sched", 0);
363df8bae1dSRodney W. Grimes 		goto loop;
364df8bae1dSRodney W. Grimes 	}
365a669a6e9SJohn Dyson 	p->p_flag &= ~P_SWAPINREQ;
366a669a6e9SJohn Dyson 
367df8bae1dSRodney W. Grimes 	/*
36826f9a767SRodney W. Grimes 	 * We would like to bring someone in. (only if there is space).
369df8bae1dSRodney W. Grimes 	 */
37026f9a767SRodney W. Grimes 	faultin(p);
371df8bae1dSRodney W. Grimes 	p->p_swtime = 0;
372df8bae1dSRodney W. Grimes 	goto loop;
373df8bae1dSRodney W. Grimes }
374df8bae1dSRodney W. Grimes 
3755afce282SDavid Greenman #ifndef NO_SWAPPING
3765afce282SDavid Greenman 
377df8bae1dSRodney W. Grimes #define	swappable(p) \
37826f9a767SRodney W. Grimes 	(((p)->p_lock == 0) && \
379bfbfac11SDavid Greenman 		((p)->p_flag & (P_TRACED|P_NOSWAP|P_SYSTEM|P_INMEM|P_WEXIT|P_PHYSIO|P_SWAPPING)) == P_INMEM)
380df8bae1dSRodney W. Grimes 
381df8bae1dSRodney W. Grimes /*
382df8bae1dSRodney W. Grimes  * Swapout is driven by the pageout daemon.  Very simple, we find eligible
383df8bae1dSRodney W. Grimes  * procs and unwire their u-areas.  We try to always "swap" at least one
384df8bae1dSRodney W. Grimes  * process in case we need the room for a swapin.
385df8bae1dSRodney W. Grimes  * If any procs have been sleeping/stopped for at least maxslp seconds,
386df8bae1dSRodney W. Grimes  * they are swapped.  Else, we swap the longest-sleeping or stopped process,
387df8bae1dSRodney W. Grimes  * if any, otherwise the longest-resident process.
388df8bae1dSRodney W. Grimes  */
389df8bae1dSRodney W. Grimes void
3906306c897SDavid Greenman swapout_procs()
391df8bae1dSRodney W. Grimes {
392df8bae1dSRodney W. Grimes 	register struct proc *p;
393df8bae1dSRodney W. Grimes 	struct proc *outp, *outp2;
394df8bae1dSRodney W. Grimes 	int outpri, outpri2;
395df8bae1dSRodney W. Grimes 	int didswap = 0;
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes 	outp = outp2 = NULL;
39826f9a767SRodney W. Grimes 	outpri = outpri2 = INT_MIN;
3990d94caffSDavid Greenman retry:
4001b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
401b18bfc3dSJohn Dyson 		struct vmspace *vm;
402df8bae1dSRodney W. Grimes 		if (!swappable(p))
403df8bae1dSRodney W. Grimes 			continue;
404b18bfc3dSJohn Dyson 
405b18bfc3dSJohn Dyson 		vm = p->p_vmspace;
406b18bfc3dSJohn Dyson 
407df8bae1dSRodney W. Grimes 		switch (p->p_stat) {
4080d94caffSDavid Greenman 		default:
409df8bae1dSRodney W. Grimes 			continue;
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 		case SSLEEP:
412df8bae1dSRodney W. Grimes 		case SSTOP:
41326f9a767SRodney W. Grimes 			/*
414bfbfac11SDavid Greenman 			 * do not swapout a realtime process
415bfbfac11SDavid Greenman 			 */
416bfbfac11SDavid Greenman 			if (p->p_rtprio.type == RTP_PRIO_REALTIME)
417bfbfac11SDavid Greenman 				continue;
418bfbfac11SDavid Greenman 
419bfbfac11SDavid Greenman 			/*
4200d94caffSDavid Greenman 			 * do not swapout a process waiting on a critical
4210d94caffSDavid Greenman 			 * event of some kind
4220d94caffSDavid Greenman 			 */
4230e27a03fSJohn Dyson 			if (((p->p_priority & 0x7f) < PSOCK) ||
424877329e0SJohn Dyson 				(p->p_slptime <= 10))
4250d94caffSDavid Greenman 				continue;
4260d94caffSDavid Greenman 
427b18bfc3dSJohn Dyson 			++vm->vm_refcnt;
428b18bfc3dSJohn Dyson 			vm_map_reference(&vm->vm_map);
429d3a34985SJohn Dyson 			/*
430d3a34985SJohn Dyson 			 * do not swapout a process that is waiting for VM
431d3a34985SJohn Dyson 			 * data structures there is a possible deadlock.
432d3a34985SJohn Dyson 			 */
433996c772fSJohn Dyson 			if (lockmgr(&vm->vm_map.lock,
434996c772fSJohn Dyson 					LK_EXCLUSIVE | LK_NOWAIT,
435996c772fSJohn Dyson 					(void *)0, curproc)) {
436b18bfc3dSJohn Dyson 				vm_map_deallocate(&vm->vm_map);
437b18bfc3dSJohn Dyson 				vmspace_free(vm);
438d3a34985SJohn Dyson 				continue;
439d3a34985SJohn Dyson 			}
440b18bfc3dSJohn Dyson 			vm_map_unlock(&vm->vm_map);
44111b224dcSDavid Greenman 			/*
4420d94caffSDavid Greenman 			 * If the process has been asleep for awhile and had
4430d94caffSDavid Greenman 			 * most of its pages taken away already, swap it out.
44411b224dcSDavid Greenman 			 */
445df8bae1dSRodney W. Grimes 			swapout(p);
446b18bfc3dSJohn Dyson 			vm_map_deallocate(&vm->vm_map);
447b18bfc3dSJohn Dyson 			vmspace_free(vm);
448df8bae1dSRodney W. Grimes 			didswap++;
4490d94caffSDavid Greenman 			goto retry;
450df8bae1dSRodney W. Grimes 		}
45126f9a767SRodney W. Grimes 	}
45226f9a767SRodney W. Grimes 	/*
45326f9a767SRodney W. Grimes 	 * If we swapped something out, and another process needed memory,
45426f9a767SRodney W. Grimes 	 * then wakeup the sched process.
45526f9a767SRodney W. Grimes 	 */
4560d94caffSDavid Greenman 	if (didswap)
45724a1cce3SDavid Greenman 		wakeup(&proc0);
458df8bae1dSRodney W. Grimes }
459df8bae1dSRodney W. Grimes 
460f708ef1bSPoul-Henning Kamp static void
461df8bae1dSRodney W. Grimes swapout(p)
462df8bae1dSRodney W. Grimes 	register struct proc *p;
463df8bae1dSRodney W. Grimes {
464bd7e5f99SJohn Dyson 	pmap_t pmap = &p->p_vmspace->vm_pmap;
465aef922f5SJohn Dyson 	int i;
466df8bae1dSRodney W. Grimes 
467d3a34985SJohn Dyson #if defined(SWAP_DEBUG)
468d3a34985SJohn Dyson 	printf("swapping out %d\n", p->p_pid);
469d3a34985SJohn Dyson #endif
47026f9a767SRodney W. Grimes 	++p->p_stats->p_ru.ru_nswap;
471df8bae1dSRodney W. Grimes 	/*
47226f9a767SRodney W. Grimes 	 * remember the process resident count
473df8bae1dSRodney W. Grimes 	 */
47426f9a767SRodney W. Grimes 	p->p_vmspace->vm_swrss =
47526f9a767SRodney W. Grimes 	    p->p_vmspace->vm_pmap.pm_stats.resident_count;
476df8bae1dSRodney W. Grimes 
477df8bae1dSRodney W. Grimes 	(void) splhigh();
478df8bae1dSRodney W. Grimes 	p->p_flag &= ~P_INMEM;
4790e27a03fSJohn Dyson 	p->p_flag |= P_SWAPPING;
480df8bae1dSRodney W. Grimes 	if (p->p_stat == SRUN)
481df8bae1dSRodney W. Grimes 		remrq(p);
482df8bae1dSRodney W. Grimes 	(void) spl0();
48326f9a767SRodney W. Grimes 
484675878e7SJohn Dyson 	pmap_swapout_proc(p);
4856d40c3d3SDavid Greenman 
486bfbfac11SDavid Greenman 	p->p_flag &= ~P_SWAPPING;
487df8bae1dSRodney W. Grimes 	p->p_swtime = 0;
488df8bae1dSRodney W. Grimes }
4895afce282SDavid Greenman #endif /* !NO_SWAPPING */
490