xref: /freebsd/sys/vm/vm_object.c (revision c7c34a24a3f07aa94c633605632dc562e8ad01cb)
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_object.c	8.5 (Berkeley) 3/22/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  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
633c4dd356SDavid Greenman  *
64c7c34a24SBruce Evans  * $Id: vm_object.c,v 1.80 1996/09/08 20:44:41 dyson Exp $
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes /*
68df8bae1dSRodney W. Grimes  *	Virtual memory object module.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <sys/param.h>
72df8bae1dSRodney W. Grimes #include <sys/systm.h>
73f23b4c91SGarrett Wollman #include <sys/kernel.h>
74f23b4c91SGarrett Wollman #include <sys/proc.h>		/* for curproc, pageproc */
75df8bae1dSRodney W. Grimes #include <sys/malloc.h>
760d94caffSDavid Greenman #include <sys/vnode.h>
770d94caffSDavid Greenman #include <sys/mount.h>
78efeaf95aSDavid Greenman #include <sys/vmmeter.h>
79867a482dSJohn Dyson #include <sys/mman.h>
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes #include <vm/vm.h>
82efeaf95aSDavid Greenman #include <vm/vm_param.h>
83efeaf95aSDavid Greenman #include <vm/vm_prot.h>
84efeaf95aSDavid Greenman #include <vm/lock.h>
85efeaf95aSDavid Greenman #include <vm/pmap.h>
86efeaf95aSDavid Greenman #include <vm/vm_map.h>
87efeaf95aSDavid Greenman #include <vm/vm_object.h>
88df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8926f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
900d94caffSDavid Greenman #include <vm/vm_pager.h>
9105f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
92a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
93efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9426f9a767SRodney W. Grimes 
95cac597e4SBruce Evans static void	_vm_object_allocate __P((objtype_t, vm_size_t, vm_object_t));
96cac597e4SBruce Evans static void	vm_object_qcollapse __P((vm_object_t object));
97f708ef1bSPoul-Henning Kamp #ifdef not_used
98f708ef1bSPoul-Henning Kamp static void	vm_object_deactivate_pages __P((vm_object_t));
99f708ef1bSPoul-Henning Kamp #endif
100f708ef1bSPoul-Henning Kamp static void	vm_object_terminate __P((vm_object_t));
101f708ef1bSPoul-Henning Kamp static void	vm_object_cache_trim __P((void));
102f6b04d2bSDavid Greenman 
103df8bae1dSRodney W. Grimes /*
104df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
105df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
106df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
107df8bae1dSRodney W. Grimes  *
108df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
109df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
110df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
111df8bae1dSRodney W. Grimes  *
112df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
113df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
114df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
115df8bae1dSRodney W. Grimes  *	lock.
116df8bae1dSRodney W. Grimes  *
117df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
118df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
119df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
120df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
121df8bae1dSRodney W. Grimes  *
122df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
123df8bae1dSRodney W. Grimes  *	modified after time of creation are:
124df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
125df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
126df8bae1dSRodney W. Grimes  *
127df8bae1dSRodney W. Grimes  */
128df8bae1dSRodney W. Grimes 
12928f8db14SBruce Evans int vm_object_cache_max;
13028f8db14SBruce Evans struct object_q vm_object_cached_list;
131f708ef1bSPoul-Henning Kamp static int vm_object_cached;
13228f8db14SBruce Evans struct object_q vm_object_list;
133f708ef1bSPoul-Henning Kamp static long vm_object_count;
13428f8db14SBruce Evans vm_object_t kernel_object;
13528f8db14SBruce Evans vm_object_t kmem_object;
136f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
137f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
138aef922f5SJohn Dyson extern int vm_pageout_page_count;
139df8bae1dSRodney W. Grimes 
140f708ef1bSPoul-Henning Kamp static long object_collapses;
141f708ef1bSPoul-Henning Kamp static long object_bypasses;
1425070c7f8SJohn Dyson static int next_index;
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes static void
14524a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14624a1cce3SDavid Greenman 	objtype_t type;
147df8bae1dSRodney W. Grimes 	vm_size_t size;
148df8bae1dSRodney W. Grimes 	register vm_object_t object;
149df8bae1dSRodney W. Grimes {
150df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
15124a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
152a1f6d91cSDavid Greenman 
15324a1cce3SDavid Greenman 	object->type = type;
154df8bae1dSRodney W. Grimes 	object->size = size;
155a1f6d91cSDavid Greenman 	object->ref_count = 1;
15624a1cce3SDavid Greenman 	object->flags = 0;
157867a482dSJohn Dyson 	object->behavior = OBJ_NORMAL;
158df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
159a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
160de5f6a77SJohn Dyson 	object->shadow_count = 0;
1615070c7f8SJohn Dyson 	object->pg_color = next_index;
1625070c7f8SJohn Dyson 	next_index = (next_index + PQ_PRIME1) & PQ_L2_MASK;
16324a1cce3SDavid Greenman 	object->handle = NULL;
164a316d390SJohn Dyson 	object->paging_offset = (vm_ooffset_t) 0;
16524a1cce3SDavid Greenman 	object->backing_object = NULL;
166a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
167a1f6d91cSDavid Greenman 
168a1f6d91cSDavid Greenman 	object->last_read = 0;
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
171df8bae1dSRodney W. Grimes 	vm_object_count++;
172df8bae1dSRodney W. Grimes }
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes /*
17526f9a767SRodney W. Grimes  *	vm_object_init:
17626f9a767SRodney W. Grimes  *
17726f9a767SRodney W. Grimes  *	Initialize the VM objects module.
17826f9a767SRodney W. Grimes  */
17926f9a767SRodney W. Grimes void
180a316d390SJohn Dyson vm_object_init()
18126f9a767SRodney W. Grimes {
18226f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_cached_list);
18326f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
18426f9a767SRodney W. Grimes 	vm_object_count = 0;
1850217125fSDavid Greenman 
1860217125fSDavid Greenman 	vm_object_cache_max = 84;
1870217125fSDavid Greenman 	if (cnt.v_page_count > 1000)
18846268a60SDavid Greenman 		vm_object_cache_max += (cnt.v_page_count - 1000) / 4;
18926f9a767SRodney W. Grimes 
19026f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
191a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19226f9a767SRodney W. Grimes 	    kernel_object);
19326f9a767SRodney W. Grimes 
19426f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
195a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19626f9a767SRodney W. Grimes 	    kmem_object);
19726f9a767SRodney W. Grimes }
19826f9a767SRodney W. Grimes 
19926f9a767SRodney W. Grimes /*
20026f9a767SRodney W. Grimes  *	vm_object_allocate:
20126f9a767SRodney W. Grimes  *
20226f9a767SRodney W. Grimes  *	Returns a new object with the given size.
20326f9a767SRodney W. Grimes  */
20426f9a767SRodney W. Grimes 
20526f9a767SRodney W. Grimes vm_object_t
20624a1cce3SDavid Greenman vm_object_allocate(type, size)
20724a1cce3SDavid Greenman 	objtype_t type;
20826f9a767SRodney W. Grimes 	vm_size_t size;
20926f9a767SRodney W. Grimes {
21026f9a767SRodney W. Grimes 	register vm_object_t result;
21126f9a767SRodney W. Grimes 
21226f9a767SRodney W. Grimes 	result = (vm_object_t)
21326f9a767SRodney W. Grimes 	    malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK);
21426f9a767SRodney W. Grimes 
21567bf6868SJohn Dyson 
21624a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
21726f9a767SRodney W. Grimes 
21826f9a767SRodney W. Grimes 	return (result);
21926f9a767SRodney W. Grimes }
22026f9a767SRodney W. Grimes 
22126f9a767SRodney W. Grimes 
22226f9a767SRodney W. Grimes /*
223df8bae1dSRodney W. Grimes  *	vm_object_reference:
224df8bae1dSRodney W. Grimes  *
225df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
226df8bae1dSRodney W. Grimes  */
2276476c0d2SJohn Dyson void
22826f9a767SRodney W. Grimes vm_object_reference(object)
229df8bae1dSRodney W. Grimes 	register vm_object_t object;
230df8bae1dSRodney W. Grimes {
231df8bae1dSRodney W. Grimes 	if (object == NULL)
232df8bae1dSRodney W. Grimes 		return;
233df8bae1dSRodney W. Grimes 
23424a1cce3SDavid Greenman 	if (object->ref_count == 0) {
23524a1cce3SDavid Greenman 		if ((object->flags & OBJ_CANPERSIST) == 0)
23624a1cce3SDavid Greenman 			panic("vm_object_reference: non-persistent object with 0 ref_count");
23724a1cce3SDavid Greenman 		TAILQ_REMOVE(&vm_object_cached_list, object, cached_list);
23824a1cce3SDavid Greenman 		vm_object_cached--;
23924a1cce3SDavid Greenman 	}
240df8bae1dSRodney W. Grimes 	object->ref_count++;
241df8bae1dSRodney W. Grimes }
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes /*
244df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
245df8bae1dSRodney W. Grimes  *
246df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
247df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
248df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
249df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
250df8bae1dSRodney W. Grimes  *	may be relinquished.
251df8bae1dSRodney W. Grimes  *
252df8bae1dSRodney W. Grimes  *	No object may be locked.
253df8bae1dSRodney W. Grimes  */
25426f9a767SRodney W. Grimes void
25526f9a767SRodney W. Grimes vm_object_deallocate(object)
25626f9a767SRodney W. Grimes 	vm_object_t object;
257df8bae1dSRodney W. Grimes {
258df8bae1dSRodney W. Grimes 	vm_object_t temp;
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes 	while (object != NULL) {
261df8bae1dSRodney W. Grimes 
262ba8da839SDavid Greenman 		if (object->ref_count == 0)
263ba8da839SDavid Greenman 			panic("vm_object_deallocate: object deallocated too many times");
264ba8da839SDavid Greenman 
265df8bae1dSRodney W. Grimes 		/*
266df8bae1dSRodney W. Grimes 		 * Lose the reference
267df8bae1dSRodney W. Grimes 		 */
268be6d5bfaSDavid Greenman 		object->ref_count--;
269be6d5bfaSDavid Greenman 		if (object->ref_count != 0) {
270be6d5bfaSDavid Greenman 			if ((object->ref_count == 1) &&
27124a1cce3SDavid Greenman 			    (object->handle == NULL) &&
27224a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
27324a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
274a1f6d91cSDavid Greenman 				vm_object_t robject;
275b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
276be6d5bfaSDavid Greenman 				if ((robject != NULL) &&
27724a1cce3SDavid Greenman 				    (robject->handle == NULL) &&
27824a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
27924a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
280a1f6d91cSDavid Greenman 					int s;
281a1f6d91cSDavid Greenman 					robject->ref_count += 2;
2829b4814bbSDavid Greenman 					object->ref_count += 2;
283a1f6d91cSDavid Greenman 
284a1f6d91cSDavid Greenman 					do {
285b18bfc3dSJohn Dyson 						s = splvm();
286a1f6d91cSDavid Greenman 						while (robject->paging_in_progress) {
287c0503609SDavid Greenman 							robject->flags |= OBJ_PIPWNT;
288a1f6d91cSDavid Greenman 							tsleep(robject, PVM, "objde1", 0);
2890d94caffSDavid Greenman 						}
290a1f6d91cSDavid Greenman 
291a1f6d91cSDavid Greenman 						while (object->paging_in_progress) {
292c0503609SDavid Greenman 							object->flags |= OBJ_PIPWNT;
293a1f6d91cSDavid Greenman 							tsleep(object, PVM, "objde2", 0);
294a1f6d91cSDavid Greenman 						}
295a1f6d91cSDavid Greenman 						splx(s);
296a1f6d91cSDavid Greenman 
297a1f6d91cSDavid Greenman 					} while( object->paging_in_progress || robject->paging_in_progress);
298a1f6d91cSDavid Greenman 
2999b4814bbSDavid Greenman 					object->ref_count -= 2;
300a1f6d91cSDavid Greenman 					robject->ref_count -= 2;
301a1f6d91cSDavid Greenman 					if( robject->ref_count == 0) {
302a1f6d91cSDavid Greenman 						robject->ref_count += 1;
303ba8da839SDavid Greenman 						object = robject;
304ba8da839SDavid Greenman 						continue;
305a1f6d91cSDavid Greenman 					}
306a1f6d91cSDavid Greenman 					vm_object_collapse(robject);
307a1f6d91cSDavid Greenman 					return;
308010cf3b9SDavid Greenman 				}
309010cf3b9SDavid Greenman 			}
310df8bae1dSRodney W. Grimes 			/*
3110d94caffSDavid Greenman 			 * If there are still references, then we are done.
312df8bae1dSRodney W. Grimes 			 */
313df8bae1dSRodney W. Grimes 			return;
314df8bae1dSRodney W. Grimes 		}
315df8bae1dSRodney W. Grimes 
31624a1cce3SDavid Greenman 		if (object->type == OBJT_VNODE) {
31724a1cce3SDavid Greenman 			struct vnode *vp = object->handle;
318f5cf85d4SDavid Greenman 
31924a1cce3SDavid Greenman 			vp->v_flag &= ~VTEXT;
320f5cf85d4SDavid Greenman 		}
321f5cf85d4SDavid Greenman 
322f919ebdeSDavid Greenman 		/*
323f919ebdeSDavid Greenman 		 * See if this object can persist and has some resident
324f919ebdeSDavid Greenman 		 * pages.  If so, enter it in the cache.
325f919ebdeSDavid Greenman 		 */
326f5cf85d4SDavid Greenman 		if (object->flags & OBJ_CANPERSIST) {
327f5cf85d4SDavid Greenman 			if (object->resident_page_count != 0) {
3286476c0d2SJohn Dyson #if 0
32924a1cce3SDavid Greenman 				vm_object_page_clean(object, 0, 0 ,TRUE, TRUE);
3306476c0d2SJohn Dyson #endif
331df8bae1dSRodney W. Grimes 				TAILQ_INSERT_TAIL(&vm_object_cached_list, object,
332df8bae1dSRodney W. Grimes 				    cached_list);
333df8bae1dSRodney W. Grimes 				vm_object_cached++;
334df8bae1dSRodney W. Grimes 
335df8bae1dSRodney W. Grimes 				vm_object_cache_trim();
336df8bae1dSRodney W. Grimes 				return;
337f5cf85d4SDavid Greenman 			} else {
338f5cf85d4SDavid Greenman 				object->flags &= ~OBJ_CANPERSIST;
339f5cf85d4SDavid Greenman 			}
340df8bae1dSRodney W. Grimes 		}
341f919ebdeSDavid Greenman 
342df8bae1dSRodney W. Grimes 		/*
34324a1cce3SDavid Greenman 		 * Make sure no one uses us.
344df8bae1dSRodney W. Grimes 		 */
3450d94caffSDavid Greenman 		object->flags |= OBJ_DEAD;
346df8bae1dSRodney W. Grimes 
34724a1cce3SDavid Greenman 		temp = object->backing_object;
348de5f6a77SJohn Dyson 		if (temp) {
34924a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
350de5f6a77SJohn Dyson 			--temp->shadow_count;
351de5f6a77SJohn Dyson 		}
352df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
353df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
354df8bae1dSRodney W. Grimes 		object = temp;
355df8bae1dSRodney W. Grimes 	}
356df8bae1dSRodney W. Grimes }
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes /*
359df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
360df8bae1dSRodney W. Grimes  *	up all previously used resources.
361df8bae1dSRodney W. Grimes  *
362df8bae1dSRodney W. Grimes  *	The object must be locked.
363df8bae1dSRodney W. Grimes  */
364f708ef1bSPoul-Henning Kamp static void
36526f9a767SRodney W. Grimes vm_object_terminate(object)
366df8bae1dSRodney W. Grimes 	register vm_object_t object;
367df8bae1dSRodney W. Grimes {
3683af76890SPoul-Henning Kamp 	register vm_page_t p;
36926f9a767SRodney W. Grimes 	int s;
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes 	/*
372f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
373df8bae1dSRodney W. Grimes 	 */
374b18bfc3dSJohn Dyson 	s = splvm();
375df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
376c0503609SDavid Greenman 		object->flags |= OBJ_PIPWNT;
37724a1cce3SDavid Greenman 		tsleep(object, PVM, "objtrm", 0);
378df8bae1dSRodney W. Grimes 	}
3790d94caffSDavid Greenman 	splx(s);
380df8bae1dSRodney W. Grimes 
38126f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
38226f9a767SRodney W. Grimes 		panic("vm_object_deallocate: pageout in progress");
38326f9a767SRodney W. Grimes 
38426f9a767SRodney W. Grimes 	/*
3850d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
3860d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
38726f9a767SRodney W. Grimes 	 */
38824a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
38924a1cce3SDavid Greenman 		struct vnode *vp = object->handle;
3906476c0d2SJohn Dyson 		int waslocked;
391f6b04d2bSDavid Greenman 
3926476c0d2SJohn Dyson 		waslocked = VOP_ISLOCKED(vp);
3936476c0d2SJohn Dyson 		if (!waslocked)
3940426122fSDavid Greenman 			VOP_LOCK(vp);
39524a1cce3SDavid Greenman 		vm_object_page_clean(object, 0, 0, TRUE, FALSE);
396f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
3976476c0d2SJohn Dyson 		if (!waslocked)
3980426122fSDavid Greenman 			VOP_UNLOCK(vp);
399df8bae1dSRodney W. Grimes 	}
4000d94caffSDavid Greenman 	/*
4010d94caffSDavid Greenman 	 * Now free the pages. For internal objects, this also removes them
4020d94caffSDavid Greenman 	 * from paging queues.
403df8bae1dSRodney W. Grimes 	 */
404b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
405be6d5bfaSDavid Greenman 		if (p->flags & PG_BUSY)
406be6d5bfaSDavid Greenman 			printf("vm_object_terminate: freeing busy page\n");
4070d94caffSDavid Greenman 		PAGE_WAKEUP(p);
408df8bae1dSRodney W. Grimes 		vm_page_free(p);
409df8bae1dSRodney W. Grimes 		cnt.v_pfree++;
410df8bae1dSRodney W. Grimes 	}
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	/*
413df8bae1dSRodney W. Grimes 	 * Let the pager know object is dead.
414df8bae1dSRodney W. Grimes 	 */
41524a1cce3SDavid Greenman 	vm_pager_deallocate(object);
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
418df8bae1dSRodney W. Grimes 	vm_object_count--;
419df8bae1dSRodney W. Grimes 
4200426122fSDavid Greenman 	wakeup(object);
4210426122fSDavid Greenman 
422df8bae1dSRodney W. Grimes 	/*
423df8bae1dSRodney W. Grimes 	 * Free the space for the object.
424df8bae1dSRodney W. Grimes 	 */
425df8bae1dSRodney W. Grimes 	free((caddr_t) object, M_VMOBJ);
426df8bae1dSRodney W. Grimes }
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes /*
429df8bae1dSRodney W. Grimes  *	vm_object_page_clean
430df8bae1dSRodney W. Grimes  *
431df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
43226f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
43326f9a767SRodney W. Grimes  *
43426f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
43526f9a767SRodney W. Grimes  *
43626f9a767SRodney W. Grimes  *	The object must be locked.
43726f9a767SRodney W. Grimes  */
438f6b04d2bSDavid Greenman 
439f6b04d2bSDavid Greenman void
44024a1cce3SDavid Greenman vm_object_page_clean(object, start, end, syncio, lockflag)
441f6b04d2bSDavid Greenman 	vm_object_t object;
442a316d390SJohn Dyson 	vm_pindex_t start;
443a316d390SJohn Dyson 	vm_pindex_t end;
444f6b04d2bSDavid Greenman 	boolean_t syncio;
44524a1cce3SDavid Greenman 	boolean_t lockflag;
446f6b04d2bSDavid Greenman {
447bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
448f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
449bd7e5f99SJohn Dyson 	vm_pindex_t pi;
450aef922f5SJohn Dyson 	int s;
45124a1cce3SDavid Greenman 	struct vnode *vp;
452aef922f5SJohn Dyson 	int runlen;
453bd7e5f99SJohn Dyson 	int maxf;
454bd7e5f99SJohn Dyson 	int chkb;
455bd7e5f99SJohn Dyson 	int maxb;
456bd7e5f99SJohn Dyson 	int i;
457bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
458bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
459aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
460f6b04d2bSDavid Greenman 
461aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
462aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
463f6b04d2bSDavid Greenman 		return;
464f6b04d2bSDavid Greenman 
46524a1cce3SDavid Greenman 	vp = object->handle;
46624a1cce3SDavid Greenman 
46724a1cce3SDavid Greenman 	if (lockflag)
46824a1cce3SDavid Greenman 		VOP_LOCK(vp);
469aef922f5SJohn Dyson 	object->flags |= OBJ_CLEANING;
47024a1cce3SDavid Greenman 
471f6b04d2bSDavid Greenman 	tstart = start;
472f6b04d2bSDavid Greenman 	if (end == 0) {
473f6b04d2bSDavid Greenman 		tend = object->size;
474f6b04d2bSDavid Greenman 	} else {
475f6b04d2bSDavid Greenman 		tend = end;
476f6b04d2bSDavid Greenman 	}
477a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
478aef922f5SJohn Dyson 		object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
479ec4f9fb0SDavid Greenman 	}
480b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq))
481bd7e5f99SJohn Dyson 		p->flags |= PG_CLEANCHK;
482f6b04d2bSDavid Greenman 
483bd7e5f99SJohn Dyson rescan:
484b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
485b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
486bd7e5f99SJohn Dyson 
487bd7e5f99SJohn Dyson 		pi = p->pindex;
488bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
489bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
4905070c7f8SJohn Dyson 			(p->valid == 0) ||
4915070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
492bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
493aef922f5SJohn Dyson 			continue;
494f6b04d2bSDavid Greenman 		}
495f6b04d2bSDavid Greenman 
496bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
497bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
498bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
499bd7e5f99SJohn Dyson 			continue;
500bd7e5f99SJohn Dyson 		}
501ec4f9fb0SDavid Greenman 
502b18bfc3dSJohn Dyson 		s = splvm();
503bd7e5f99SJohn Dyson 		if ((p->flags & PG_BUSY) || p->busy) {
504aef922f5SJohn Dyson 			p->flags |= PG_WANTED|PG_REFERENCED;
505aef922f5SJohn Dyson 			tsleep(p, PVM, "vpcwai", 0);
506f6b04d2bSDavid Greenman 			splx(s);
507bd7e5f99SJohn Dyson 			goto rescan;
508f6b04d2bSDavid Greenman 		}
509f6b04d2bSDavid Greenman 		splx(s);
510f6b04d2bSDavid Greenman 
511f35329acSJohn Dyson 		s = splvm();
512bd7e5f99SJohn Dyson 		maxf = 0;
513bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
514bd7e5f99SJohn Dyson 			if (tp = vm_page_lookup(object, pi + i)) {
515bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
516bd7e5f99SJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0)
517bd7e5f99SJohn Dyson 					break;
5185070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
5193077a9c2SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
5203077a9c2SJohn Dyson 					break;
5213077a9c2SJohn Dyson 				}
522bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
523bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
524bd7e5f99SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
525bd7e5f99SJohn Dyson 					break;
526bd7e5f99SJohn Dyson 				}
527bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
528bd7e5f99SJohn Dyson 				maxf++;
529bd7e5f99SJohn Dyson 				continue;
530bd7e5f99SJohn Dyson 			}
531bd7e5f99SJohn Dyson 			break;
532bd7e5f99SJohn Dyson 		}
533aef922f5SJohn Dyson 
534bd7e5f99SJohn Dyson 		maxb = 0;
535bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
536bd7e5f99SJohn Dyson 		if (chkb) {
537bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
538bd7e5f99SJohn Dyson 				if (tp = vm_page_lookup(object, pi - i)) {
539bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
540bd7e5f99SJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0)
541bd7e5f99SJohn Dyson 						break;
5425070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
5433077a9c2SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
5443077a9c2SJohn Dyson 						break;
5453077a9c2SJohn Dyson 					}
546bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
547bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
548bd7e5f99SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
549bd7e5f99SJohn Dyson 						break;
550bd7e5f99SJohn Dyson 					}
551bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
552bd7e5f99SJohn Dyson 					maxb++;
553bd7e5f99SJohn Dyson 					continue;
554bd7e5f99SJohn Dyson 				}
555bd7e5f99SJohn Dyson 				break;
556bd7e5f99SJohn Dyson 			}
557bd7e5f99SJohn Dyson 		}
558bd7e5f99SJohn Dyson 
559bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
560bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
561bd7e5f99SJohn Dyson 			ma[index] = mab[i];
562bd7e5f99SJohn Dyson 			ma[index]->flags |= PG_BUSY;
563bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
564bd7e5f99SJohn Dyson 			vm_page_protect(ma[index], VM_PROT_READ);
565bd7e5f99SJohn Dyson 		}
566bd7e5f99SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
567aef922f5SJohn Dyson 		p->flags |= PG_BUSY;
568bd7e5f99SJohn Dyson 		p->flags &= ~PG_CLEANCHK;
569bd7e5f99SJohn Dyson 		ma[maxb] = p;
570bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
571bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
572bd7e5f99SJohn Dyson 			ma[index] = maf[i];
573bd7e5f99SJohn Dyson 			ma[index]->flags |= PG_BUSY;
574bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
575bd7e5f99SJohn Dyson 			vm_page_protect(ma[index], VM_PROT_READ);
576f6b04d2bSDavid Greenman 		}
577bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
578f35329acSJohn Dyson 		splx(s);
579bd7e5f99SJohn Dyson 		vm_pageout_flush(ma, runlen, 0);
580bd7e5f99SJohn Dyson 		goto rescan;
581f6b04d2bSDavid Greenman 	}
582aef922f5SJohn Dyson 
583aef922f5SJohn Dyson 	VOP_FSYNC(vp, NULL, syncio, curproc);
584aef922f5SJohn Dyson 
58524a1cce3SDavid Greenman 	if (lockflag)
586d3628763SRodney W. Grimes 		VOP_UNLOCK(vp);
587aef922f5SJohn Dyson 	object->flags &= ~OBJ_CLEANING;
588f5cf85d4SDavid Greenman 	return;
58926f9a767SRodney W. Grimes }
590df8bae1dSRodney W. Grimes 
591f708ef1bSPoul-Henning Kamp #ifdef not_used
592f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
593df8bae1dSRodney W. Grimes /*
594df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
595df8bae1dSRodney W. Grimes  *
596df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
597df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
598df8bae1dSRodney W. Grimes  *
599df8bae1dSRodney W. Grimes  *	The object must be locked.
600df8bae1dSRodney W. Grimes  */
601f708ef1bSPoul-Henning Kamp static void
602df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
603df8bae1dSRodney W. Grimes 	register vm_object_t object;
604df8bae1dSRodney W. Grimes {
605df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
606df8bae1dSRodney W. Grimes 
607b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
608b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
609df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
610df8bae1dSRodney W. Grimes 	}
611df8bae1dSRodney W. Grimes }
612f708ef1bSPoul-Henning Kamp #endif
613df8bae1dSRodney W. Grimes 
614df8bae1dSRodney W. Grimes /*
615df8bae1dSRodney W. Grimes  *	Trim the object cache to size.
616df8bae1dSRodney W. Grimes  */
617f708ef1bSPoul-Henning Kamp static void
618df8bae1dSRodney W. Grimes vm_object_cache_trim()
619df8bae1dSRodney W. Grimes {
620df8bae1dSRodney W. Grimes 	register vm_object_t object;
621df8bae1dSRodney W. Grimes 
6220d94caffSDavid Greenman 	while (vm_object_cached > vm_object_cache_max) {
623b18bfc3dSJohn Dyson 		object = TAILQ_FIRST(&vm_object_cached_list);
624df8bae1dSRodney W. Grimes 
62524a1cce3SDavid Greenman 		vm_object_reference(object);
626df8bae1dSRodney W. Grimes 		pager_cache(object, FALSE);
627df8bae1dSRodney W. Grimes 	}
628df8bae1dSRodney W. Grimes }
629df8bae1dSRodney W. Grimes 
63026f9a767SRodney W. Grimes 
631df8bae1dSRodney W. Grimes /*
632df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
633df8bae1dSRodney W. Grimes  *
634df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
635df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
636df8bae1dSRodney W. Grimes  *	references to these pages should remain.
637df8bae1dSRodney W. Grimes  *
638df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
639df8bae1dSRodney W. Grimes  */
6400d94caffSDavid Greenman void
6410d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
642df8bae1dSRodney W. Grimes 	register vm_object_t object;
643a316d390SJohn Dyson 	register vm_pindex_t start;
644a316d390SJohn Dyson 	register vm_pindex_t end;
645df8bae1dSRodney W. Grimes {
646df8bae1dSRodney W. Grimes 	register vm_page_t p;
647df8bae1dSRodney W. Grimes 
648aef922f5SJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
649df8bae1dSRodney W. Grimes 		return;
650df8bae1dSRodney W. Grimes 
651b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
652b5b40fa6SJohn Dyson 		p != NULL;
653b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
654f919ebdeSDavid Greenman 		vm_page_protect(p, VM_PROT_READ);
655df8bae1dSRodney W. Grimes 	}
656aef922f5SJohn Dyson 
657aef922f5SJohn Dyson 	object->flags &= ~OBJ_WRITEABLE;
658df8bae1dSRodney W. Grimes }
659df8bae1dSRodney W. Grimes 
660df8bae1dSRodney W. Grimes /*
661df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
662df8bae1dSRodney W. Grimes  *
663df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
664df8bae1dSRodney W. Grimes  *	object range from all physical maps.
665df8bae1dSRodney W. Grimes  *
666df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
667df8bae1dSRodney W. Grimes  */
66826f9a767SRodney W. Grimes void
66926f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
670df8bae1dSRodney W. Grimes 	register vm_object_t object;
671a316d390SJohn Dyson 	register vm_pindex_t start;
672a316d390SJohn Dyson 	register vm_pindex_t end;
673df8bae1dSRodney W. Grimes {
674df8bae1dSRodney W. Grimes 	register vm_page_t p;
675df8bae1dSRodney W. Grimes 	if (object == NULL)
676df8bae1dSRodney W. Grimes 		return;
677b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
678b5b40fa6SJohn Dyson 		p != NULL;
679b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
680bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
681f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
68226f9a767SRodney W. Grimes 	}
68326f9a767SRodney W. Grimes }
684df8bae1dSRodney W. Grimes 
685df8bae1dSRodney W. Grimes /*
686867a482dSJohn Dyson  *	vm_object_madvise:
687867a482dSJohn Dyson  *
688867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
689867a482dSJohn Dyson  */
690867a482dSJohn Dyson void
691867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
692867a482dSJohn Dyson 	vm_object_t object;
693867a482dSJohn Dyson 	vm_pindex_t pindex;
694867a482dSJohn Dyson 	int count;
695867a482dSJohn Dyson 	int advise;
696867a482dSJohn Dyson {
697867a482dSJohn Dyson 	vm_pindex_t end;
698867a482dSJohn Dyson 	vm_page_t m;
699867a482dSJohn Dyson 
700867a482dSJohn Dyson 	if (object == NULL)
701867a482dSJohn Dyson 		return;
702867a482dSJohn Dyson 
703867a482dSJohn Dyson 	end = pindex + count;
704867a482dSJohn Dyson 
705867a482dSJohn Dyson 	for (; pindex < end; pindex += 1) {
706867a482dSJohn Dyson 		m = vm_page_lookup(object, pindex);
707867a482dSJohn Dyson 
708867a482dSJohn Dyson 		/*
709867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
710867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
711867a482dSJohn Dyson 		 * in any of the below states.
712867a482dSJohn Dyson 		 */
713867a482dSJohn Dyson 		if (m == NULL || m->busy || (m->flags & PG_BUSY) ||
714867a482dSJohn Dyson 			m->hold_count || m->wire_count ||
715867a482dSJohn Dyson 			m->valid != VM_PAGE_BITS_ALL)
716867a482dSJohn Dyson 			continue;
717867a482dSJohn Dyson 
718867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
719867a482dSJohn Dyson 			if (m->queue != PQ_ACTIVE)
720867a482dSJohn Dyson 				vm_page_activate(m);
7210a47b48bSJohn Dyson 		} else if ((advise == MADV_DONTNEED) ||
7220a47b48bSJohn Dyson 			((advise == MADV_FREE) &&
723b5b40fa6SJohn Dyson 				((object->type != OBJT_DEFAULT) &&
724b5b40fa6SJohn Dyson 					(object->type != OBJT_SWAP)))) {
725867a482dSJohn Dyson 			vm_page_deactivate(m);
7260a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
7270a47b48bSJohn Dyson 			/*
7280a47b48bSJohn Dyson 			 * Force a demand-zero on next ref
7290a47b48bSJohn Dyson 			 */
7300a47b48bSJohn Dyson 			if (object->type == OBJT_SWAP)
7310a47b48bSJohn Dyson 				swap_pager_dmzspace(object, m->pindex, 1);
7320a47b48bSJohn Dyson 			vm_page_protect(m, VM_PROT_NONE);
7330a47b48bSJohn Dyson 			vm_page_free(m);
734867a482dSJohn Dyson 		}
735867a482dSJohn Dyson 	}
736867a482dSJohn Dyson }
737867a482dSJohn Dyson 
738867a482dSJohn Dyson /*
739df8bae1dSRodney W. Grimes  *	vm_object_shadow:
740df8bae1dSRodney W. Grimes  *
741df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
742df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
743df8bae1dSRodney W. Grimes  *	object reference is deallocated.
744df8bae1dSRodney W. Grimes  *
745df8bae1dSRodney W. Grimes  *	The new object and offset into that object
746df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
747df8bae1dSRodney W. Grimes  */
748df8bae1dSRodney W. Grimes 
74926f9a767SRodney W. Grimes void
75026f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
751df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
752a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
753df8bae1dSRodney W. Grimes 	vm_size_t length;
754df8bae1dSRodney W. Grimes {
755df8bae1dSRodney W. Grimes 	register vm_object_t source;
756df8bae1dSRodney W. Grimes 	register vm_object_t result;
757df8bae1dSRodney W. Grimes 
758df8bae1dSRodney W. Grimes 	source = *object;
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes 	/*
761df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
762df8bae1dSRodney W. Grimes 	 */
763df8bae1dSRodney W. Grimes 
76424a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
765df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
766df8bae1dSRodney W. Grimes 
767df8bae1dSRodney W. Grimes 	/*
7680d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
7690d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
7700d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
7710d94caffSDavid Greenman 	 * of reference count.
772df8bae1dSRodney W. Grimes 	 */
77324a1cce3SDavid Greenman 	result->backing_object = source;
774de5f6a77SJohn Dyson 	if (source) {
775de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
776de5f6a77SJohn Dyson 		++source->shadow_count;
777de5f6a77SJohn Dyson 	}
778df8bae1dSRodney W. Grimes 
779df8bae1dSRodney W. Grimes 	/*
7800d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
7810d94caffSDavid Greenman 	 * the new object.
782df8bae1dSRodney W. Grimes 	 */
783df8bae1dSRodney W. Grimes 
78424a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes 	/*
787df8bae1dSRodney W. Grimes 	 * Return the new things
788df8bae1dSRodney W. Grimes 	 */
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes 	*offset = 0;
791df8bae1dSRodney W. Grimes 	*object = result;
792df8bae1dSRodney W. Grimes }
793df8bae1dSRodney W. Grimes 
794df8bae1dSRodney W. Grimes 
795df8bae1dSRodney W. Grimes /*
7962fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
7972fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
7982fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
7992fe6e4d7SDavid Greenman  */
8002fe6e4d7SDavid Greenman static void
8012fe6e4d7SDavid Greenman vm_object_qcollapse(object)
8022fe6e4d7SDavid Greenman 	register vm_object_t object;
8032fe6e4d7SDavid Greenman {
8042fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
805a316d390SJohn Dyson 	register vm_pindex_t backing_offset_index, paging_offset_index;
806a316d390SJohn Dyson 	vm_pindex_t backing_object_paging_offset_index;
807a316d390SJohn Dyson 	vm_pindex_t new_pindex;
8082fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
8092fe6e4d7SDavid Greenman 	register vm_size_t size;
8102fe6e4d7SDavid Greenman 
81124a1cce3SDavid Greenman 	backing_object = object->backing_object;
8122fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
8132fe6e4d7SDavid Greenman 		return;
8142fe6e4d7SDavid Greenman 
815010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
816010cf3b9SDavid Greenman 
817a316d390SJohn Dyson 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
818a316d390SJohn Dyson 	backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset);
819a316d390SJohn Dyson 	paging_offset_index = OFF_TO_IDX(object->paging_offset);
8202fe6e4d7SDavid Greenman 	size = object->size;
821b18bfc3dSJohn Dyson 	p = TAILQ_FIRST(&backing_object->memq);
8222fe6e4d7SDavid Greenman 	while (p) {
8232fe6e4d7SDavid Greenman 		vm_page_t next;
8240d94caffSDavid Greenman 
825b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
826bd7e5f99SJohn Dyson 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
8275070c7f8SJohn Dyson 		    ((p->queue - p->pc) == PQ_CACHE) ||
8285070c7f8SJohn Dyson 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
8292fe6e4d7SDavid Greenman 			p = next;
8302fe6e4d7SDavid Greenman 			continue;
8312fe6e4d7SDavid Greenman 		}
832a316d390SJohn Dyson 		new_pindex = p->pindex - backing_offset_index;
833a316d390SJohn Dyson 		if (p->pindex < backing_offset_index ||
834a316d390SJohn Dyson 		    new_pindex >= size) {
83524a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP)
83624a1cce3SDavid Greenman 				swap_pager_freespace(backing_object,
837a316d390SJohn Dyson 				    backing_object_paging_offset_index+p->pindex,
838a316d390SJohn Dyson 				    1);
839b18bfc3dSJohn Dyson 			vm_page_protect(p, VM_PROT_NONE);
8402fe6e4d7SDavid Greenman 			vm_page_free(p);
8412fe6e4d7SDavid Greenman 		} else {
842a316d390SJohn Dyson 			pp = vm_page_lookup(object, new_pindex);
84324a1cce3SDavid Greenman 			if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
844a316d390SJohn Dyson 				    paging_offset_index + new_pindex, NULL, NULL))) {
84524a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
84624a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
847a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
848b18bfc3dSJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
8492fe6e4d7SDavid Greenman 				vm_page_free(p);
8502fe6e4d7SDavid Greenman 			} else {
85124a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
85224a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
853a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
854a316d390SJohn Dyson 				vm_page_rename(p, object, new_pindex);
8559b4814bbSDavid Greenman 				p->dirty = VM_PAGE_BITS_ALL;
8562fe6e4d7SDavid Greenman 			}
8572fe6e4d7SDavid Greenman 		}
8582fe6e4d7SDavid Greenman 		p = next;
8592fe6e4d7SDavid Greenman 	}
860010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
8612fe6e4d7SDavid Greenman }
8622fe6e4d7SDavid Greenman 
863df8bae1dSRodney W. Grimes /*
864df8bae1dSRodney W. Grimes  *	vm_object_collapse:
865df8bae1dSRodney W. Grimes  *
866df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
867df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
868df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
869df8bae1dSRodney W. Grimes  */
87026f9a767SRodney W. Grimes void
87126f9a767SRodney W. Grimes vm_object_collapse(object)
87224a1cce3SDavid Greenman 	vm_object_t object;
873df8bae1dSRodney W. Grimes 
874df8bae1dSRodney W. Grimes {
87524a1cce3SDavid Greenman 	vm_object_t backing_object;
876a316d390SJohn Dyson 	vm_ooffset_t backing_offset;
87724a1cce3SDavid Greenman 	vm_size_t size;
878a316d390SJohn Dyson 	vm_pindex_t new_pindex, backing_offset_index;
87924a1cce3SDavid Greenman 	vm_page_t p, pp;
880df8bae1dSRodney W. Grimes 
881df8bae1dSRodney W. Grimes 	while (TRUE) {
882df8bae1dSRodney W. Grimes 		/*
883df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
884df8bae1dSRodney W. Grimes 		 *
8850d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
8860d94caffSDavid Greenman 		 * out.
887df8bae1dSRodney W. Grimes 		 */
8882fe6e4d7SDavid Greenman 		if (object == NULL)
889df8bae1dSRodney W. Grimes 			return;
890df8bae1dSRodney W. Grimes 
891b9921222SDavid Greenman 		/*
892b9921222SDavid Greenman 		 * Make sure there is a backing object.
893b9921222SDavid Greenman 		 */
89424a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
895df8bae1dSRodney W. Grimes 			return;
896df8bae1dSRodney W. Grimes 
897f919ebdeSDavid Greenman 		/*
898f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
89924a1cce3SDavid Greenman 		 * not collapsable.
900f919ebdeSDavid Greenman 		 */
90124a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
90224a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
90324a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
904f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
90524a1cce3SDavid Greenman 		    object->handle != NULL ||
90624a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
90724a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
90824a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
9099b4814bbSDavid Greenman 			return;
91024a1cce3SDavid Greenman 		}
9119b4814bbSDavid Greenman 
912f919ebdeSDavid Greenman 		if (object->paging_in_progress != 0 ||
913f919ebdeSDavid Greenman 		    backing_object->paging_in_progress != 0) {
914b9921222SDavid Greenman 			vm_object_qcollapse(object);
915df8bae1dSRodney W. Grimes 			return;
916df8bae1dSRodney W. Grimes 		}
917f919ebdeSDavid Greenman 
91826f9a767SRodney W. Grimes 		/*
9190d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
9200d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
9210d94caffSDavid Greenman 		 * the parent's reference to it.
922df8bae1dSRodney W. Grimes 		 */
923df8bae1dSRodney W. Grimes 
92424a1cce3SDavid Greenman 		backing_offset = object->backing_object_offset;
925a316d390SJohn Dyson 		backing_offset_index = OFF_TO_IDX(backing_offset);
926df8bae1dSRodney W. Grimes 		size = object->size;
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes 		/*
9290d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
9300d94caffSDavid Greenman 		 * can collapse it into the parent.
931df8bae1dSRodney W. Grimes 		 */
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
934df8bae1dSRodney W. Grimes 
935a1f6d91cSDavid Greenman 			backing_object->flags |= OBJ_DEAD;
936df8bae1dSRodney W. Grimes 			/*
937df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
938df8bae1dSRodney W. Grimes 			 *
9390d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
9400d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
9410d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
9420d94caffSDavid Greenman 			 * shadow them.
943df8bae1dSRodney W. Grimes 			 */
944df8bae1dSRodney W. Grimes 
945b18bfc3dSJohn Dyson 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
94626f9a767SRodney W. Grimes 
947a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
948df8bae1dSRodney W. Grimes 
949df8bae1dSRodney W. Grimes 				/*
9500d94caffSDavid Greenman 				 * If the parent has a page here, or if this
9510d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
9520d94caffSDavid Greenman 				 * it.
953df8bae1dSRodney W. Grimes 				 *
954df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
955df8bae1dSRodney W. Grimes 				 */
956df8bae1dSRodney W. Grimes 
957a316d390SJohn Dyson 				if (p->pindex < backing_offset_index ||
958a316d390SJohn Dyson 				    new_pindex >= size) {
959f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
9600d94caffSDavid Greenman 					PAGE_WAKEUP(p);
961df8bae1dSRodney W. Grimes 					vm_page_free(p);
962df8bae1dSRodney W. Grimes 				} else {
963a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
96424a1cce3SDavid Greenman 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
965a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) {
966f919ebdeSDavid Greenman 						vm_page_protect(p, VM_PROT_NONE);
9670d94caffSDavid Greenman 						PAGE_WAKEUP(p);
968df8bae1dSRodney W. Grimes 						vm_page_free(p);
96926f9a767SRodney W. Grimes 					} else {
970a316d390SJohn Dyson 						vm_page_rename(p, object, new_pindex);
971df8bae1dSRodney W. Grimes 					}
972df8bae1dSRodney W. Grimes 				}
973df8bae1dSRodney W. Grimes 			}
974df8bae1dSRodney W. Grimes 
975df8bae1dSRodney W. Grimes 			/*
976df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
977df8bae1dSRodney W. Grimes 			 */
978df8bae1dSRodney W. Grimes 
97924a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
98026f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
98124a1cce3SDavid Greenman 				if (object->type == OBJT_SWAP) {
98226f9a767SRodney W. Grimes 					object->paging_in_progress++;
98326f9a767SRodney W. Grimes 					/*
98426f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
9850d94caffSDavid Greenman 					 * and destroy unneeded pages in
9860d94caffSDavid Greenman 					 * shadow object.
98726f9a767SRodney W. Grimes 					 */
98826f9a767SRodney W. Grimes 					swap_pager_copy(
989a316d390SJohn Dyson 					    backing_object,
990a316d390SJohn Dyson 					    OFF_TO_IDX(backing_object->paging_offset),
991a316d390SJohn Dyson 					    object,
992a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset),
993a316d390SJohn Dyson 					    OFF_TO_IDX(object->backing_object_offset));
994f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
99526f9a767SRodney W. Grimes 				} else {
99626f9a767SRodney W. Grimes 					object->paging_in_progress++;
99726f9a767SRodney W. Grimes 					/*
99824a1cce3SDavid Greenman 					 * move the shadow backing_object's pager data to
99924a1cce3SDavid Greenman 					 * "object" and convert "object" type to OBJT_SWAP.
100026f9a767SRodney W. Grimes 					 */
100124a1cce3SDavid Greenman 					object->type = OBJT_SWAP;
10022a4895f4SDavid Greenman 					object->un_pager.swp.swp_nblocks =
10032a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_nblocks;
10042a4895f4SDavid Greenman 					object->un_pager.swp.swp_allocsize =
10052a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_allocsize;
10062a4895f4SDavid Greenman 					object->un_pager.swp.swp_blocks =
10072a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_blocks;
10082a4895f4SDavid Greenman 					object->un_pager.swp.swp_poip =		/* XXX */
10092a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_poip;
101026f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
101124a1cce3SDavid Greenman 					TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list);
101224a1cce3SDavid Greenman 
101324a1cce3SDavid Greenman 					/*
101424a1cce3SDavid Greenman 					 * Convert backing object from OBJT_SWAP to
101524a1cce3SDavid Greenman 					 * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is
101624a1cce3SDavid Greenman 					 * actually necessary.
101724a1cce3SDavid Greenman 					 */
101824a1cce3SDavid Greenman 					backing_object->type = OBJT_DEFAULT;
101924a1cce3SDavid Greenman 					TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list);
102026f9a767SRodney W. Grimes 					/*
102126f9a767SRodney W. Grimes 					 * free unnecessary blocks
102226f9a767SRodney W. Grimes 					 */
1023a316d390SJohn Dyson 					swap_pager_freespace(object, 0,
1024a316d390SJohn Dyson 						OFF_TO_IDX(object->paging_offset));
1025f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
1026c0503609SDavid Greenman 				}
1027c0503609SDavid Greenman 
1028f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1029c0503609SDavid Greenman 			}
1030df8bae1dSRodney W. Grimes 			/*
1031df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
103224a1cce3SDavid Greenman 			 * Note that the reference to backing_object->backing_object
1033df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1034df8bae1dSRodney W. Grimes 			 */
1035df8bae1dSRodney W. Grimes 
103624a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
103724a1cce3SDavid Greenman 			    shadow_list);
1038de5f6a77SJohn Dyson 			--object->backing_object->shadow_count;
1039de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
104024a1cce3SDavid Greenman 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
104124a1cce3SDavid Greenman 				    backing_object, shadow_list);
1042de5f6a77SJohn Dyson 				--backing_object->backing_object->shadow_count;
1043de5f6a77SJohn Dyson 			}
104424a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1045de5f6a77SJohn Dyson 			if (object->backing_object) {
104624a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
104724a1cce3SDavid Greenman 				    object, shadow_list);
1048de5f6a77SJohn Dyson 				++object->backing_object->shadow_count;
1049de5f6a77SJohn Dyson 			}
10502fe6e4d7SDavid Greenman 
105124a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1052df8bae1dSRodney W. Grimes 			/*
1053df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1054df8bae1dSRodney W. Grimes 			 *
10550d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
10560d94caffSDavid Greenman 			 * and no object references within it, all that is
10570d94caffSDavid Greenman 			 * necessary is to dispose of it.
1058df8bae1dSRodney W. Grimes 			 */
1059df8bae1dSRodney W. Grimes 
1060df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1061df8bae1dSRodney W. Grimes 			    object_list);
1062df8bae1dSRodney W. Grimes 			vm_object_count--;
1063df8bae1dSRodney W. Grimes 
1064df8bae1dSRodney W. Grimes 			free((caddr_t) backing_object, M_VMOBJ);
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes 			object_collapses++;
10670d94caffSDavid Greenman 		} else {
1068df8bae1dSRodney W. Grimes 			/*
1069df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
10700d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
10710d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
10720d94caffSDavid Greenman 			 * shadow the next one in the chain.
1073df8bae1dSRodney W. Grimes 			 *
10740d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
10750d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1076df8bae1dSRodney W. Grimes 			 */
1077df8bae1dSRodney W. Grimes 
107824a1cce3SDavid Greenman 			if (backing_object->type != OBJT_DEFAULT) {
1079df8bae1dSRodney W. Grimes 				return;
1080df8bae1dSRodney W. Grimes 			}
1081df8bae1dSRodney W. Grimes 			/*
10820d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
10830d94caffSDavid Greenman 			 * here.
1084df8bae1dSRodney W. Grimes 			 */
1085df8bae1dSRodney W. Grimes 
1086b18bfc3dSJohn Dyson 			for (p = TAILQ_FIRST(&backing_object->memq); p; p = TAILQ_NEXT(p, listq)) {
1087a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1088df8bae1dSRodney W. Grimes 
1089df8bae1dSRodney W. Grimes 				/*
10900d94caffSDavid Greenman 				 * If the parent has a page here, or if this
10910d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1092df8bae1dSRodney W. Grimes 				 *
10930d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
10940d94caffSDavid Greenman 				 * the chain.
1095df8bae1dSRodney W. Grimes 				 */
1096df8bae1dSRodney W. Grimes 
1097a316d390SJohn Dyson 				if (p->pindex >= backing_offset_index &&
1098a316d390SJohn Dyson 					new_pindex <= size) {
109924a1cce3SDavid Greenman 
1100a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
110124a1cce3SDavid Greenman 
110224a1cce3SDavid Greenman 					if ((pp == NULL || pp->valid == 0) &&
1103a316d390SJohn Dyson 				   	    !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) {
1104df8bae1dSRodney W. Grimes 						/*
11050d94caffSDavid Greenman 						 * Page still needed. Can't go any
11060d94caffSDavid Greenman 						 * further.
1107df8bae1dSRodney W. Grimes 						 */
1108df8bae1dSRodney W. Grimes 						return;
1109df8bae1dSRodney W. Grimes 					}
1110df8bae1dSRodney W. Grimes 				}
111124a1cce3SDavid Greenman 			}
1112df8bae1dSRodney W. Grimes 
1113df8bae1dSRodney W. Grimes 			/*
11140d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
11150d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
11160d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1117df8bae1dSRodney W. Grimes 			 */
1118df8bae1dSRodney W. Grimes 
111924a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head,
112024a1cce3SDavid Greenman 			    object, shadow_list);
1121de5f6a77SJohn Dyson 			--object->backing_object->shadow_count;
112224a1cce3SDavid Greenman 			vm_object_reference(object->backing_object = backing_object->backing_object);
1123de5f6a77SJohn Dyson 			if (object->backing_object) {
112424a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
112524a1cce3SDavid Greenman 				    object, shadow_list);
1126de5f6a77SJohn Dyson 				++object->backing_object->shadow_count;
1127de5f6a77SJohn Dyson 			}
112824a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes 			/*
11310d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
11320d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
11330d94caffSDavid Greenman 			 * so we don't need to call vm_object_deallocate.
1134df8bae1dSRodney W. Grimes 			 */
113526f9a767SRodney W. Grimes 			if (backing_object->ref_count == 1)
113626f9a767SRodney W. Grimes 				printf("should have called obj deallocate\n");
1137df8bae1dSRodney W. Grimes 			backing_object->ref_count--;
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 			object_bypasses++;
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes 		}
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 		/*
1144df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1145df8bae1dSRodney W. Grimes 		 */
1146df8bae1dSRodney W. Grimes 	}
1147df8bae1dSRodney W. Grimes }
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes /*
1150df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1151df8bae1dSRodney W. Grimes  *
1152df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1153df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1154df8bae1dSRodney W. Grimes  *
1155df8bae1dSRodney W. Grimes  *	The object must be locked.
1156df8bae1dSRodney W. Grimes  */
115726f9a767SRodney W. Grimes void
11587c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1159df8bae1dSRodney W. Grimes 	register vm_object_t object;
1160a316d390SJohn Dyson 	register vm_pindex_t start;
1161a316d390SJohn Dyson 	register vm_pindex_t end;
11627c1f6cedSDavid Greenman 	boolean_t clean_only;
1163df8bae1dSRodney W. Grimes {
1164df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
1165a316d390SJohn Dyson 	unsigned int size;
116626f9a767SRodney W. Grimes 	int s;
1167df8bae1dSRodney W. Grimes 
1168df8bae1dSRodney W. Grimes 	if (object == NULL)
1169df8bae1dSRodney W. Grimes 		return;
1170df8bae1dSRodney W. Grimes 
11712fe6e4d7SDavid Greenman 	object->paging_in_progress++;
117226f9a767SRodney W. Grimes again:
117326f9a767SRodney W. Grimes 	size = end - start;
1174a316d390SJohn Dyson 	if (size > 4 || size >= object->size / 4) {
1175b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1176b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
11770891ef4cSJohn Dyson 			if ((start <= p->pindex) && (p->pindex < end)) {
1178bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1179bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1180bd7e5f99SJohn Dyson 					p->valid = 0;
11810d94caffSDavid Greenman 					continue;
11820d94caffSDavid Greenman 				}
11830891ef4cSJohn Dyson 
1184b18bfc3dSJohn Dyson 				/*
1185b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1186b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1187b18bfc3dSJohn Dyson 				 */
1188b18bfc3dSJohn Dyson 				if ((p->flags & PG_BUSY) || p->busy) {
1189b18bfc3dSJohn Dyson 					s = splvm();
11900d94caffSDavid Greenman 					if ((p->flags & PG_BUSY) || p->busy) {
119126f9a767SRodney W. Grimes 						p->flags |= PG_WANTED;
119224a1cce3SDavid Greenman 						tsleep(p, PVM, "vmopar", 0);
1193a481f200SDavid Greenman 						splx(s);
119426f9a767SRodney W. Grimes 						goto again;
119526f9a767SRodney W. Grimes 					}
1196a481f200SDavid Greenman 					splx(s);
1197b18bfc3dSJohn Dyson 				}
11980891ef4cSJohn Dyson 
11997c1f6cedSDavid Greenman 				if (clean_only) {
12007c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
12017c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
12027c1f6cedSDavid Greenman 						continue;
12037c1f6cedSDavid Greenman 				}
1204f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
12050d94caffSDavid Greenman 				PAGE_WAKEUP(p);
1206df8bae1dSRodney W. Grimes 				vm_page_free(p);
120726f9a767SRodney W. Grimes 			}
120826f9a767SRodney W. Grimes 		}
120926f9a767SRodney W. Grimes 	} else {
121026f9a767SRodney W. Grimes 		while (size > 0) {
1211bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1212bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1213bd7e5f99SJohn Dyson 					p->valid = 0;
1214bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1215bd7e5f99SJohn Dyson 					start += 1;
1216bd7e5f99SJohn Dyson 					size -= 1;
1217bd7e5f99SJohn Dyson 					continue;
12180d94caffSDavid Greenman 				}
1219b18bfc3dSJohn Dyson 				/*
1220b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1221b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1222b18bfc3dSJohn Dyson 				 */
1223b18bfc3dSJohn Dyson 				if ((p->flags & PG_BUSY) || p->busy) {
1224b18bfc3dSJohn Dyson 					s = splvm();
12250d94caffSDavid Greenman 					if ((p->flags & PG_BUSY) || p->busy) {
122626f9a767SRodney W. Grimes 						p->flags |= PG_WANTED;
122724a1cce3SDavid Greenman 						tsleep(p, PVM, "vmopar", 0);
1228a481f200SDavid Greenman 						splx(s);
122926f9a767SRodney W. Grimes 						goto again;
123026f9a767SRodney W. Grimes 					}
1231a481f200SDavid Greenman 					splx(s);
1232b18bfc3dSJohn Dyson 				}
12337c1f6cedSDavid Greenman 				if (clean_only) {
12347c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1235bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1236bd7e5f99SJohn Dyson 						start += 1;
1237bd7e5f99SJohn Dyson 						size -= 1;
12387c1f6cedSDavid Greenman 						continue;
12397c1f6cedSDavid Greenman 					}
1240bd7e5f99SJohn Dyson 				}
1241f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
12420d94caffSDavid Greenman 				PAGE_WAKEUP(p);
124326f9a767SRodney W. Grimes 				vm_page_free(p);
124426f9a767SRodney W. Grimes 			}
1245a316d390SJohn Dyson 			start += 1;
1246a316d390SJohn Dyson 			size -= 1;
1247df8bae1dSRodney W. Grimes 		}
1248df8bae1dSRodney W. Grimes 	}
1249f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1250c0503609SDavid Greenman }
1251df8bae1dSRodney W. Grimes 
1252df8bae1dSRodney W. Grimes /*
1253df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1254df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1255df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1256df8bae1dSRodney W. Grimes  *
1257df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1258df8bae1dSRodney W. Grimes  *
1259df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1260df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1261df8bae1dSRodney W. Grimes  *
1262df8bae1dSRodney W. Grimes  *	Parameters:
1263df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1264df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1265df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1266df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1267df8bae1dSRodney W. Grimes  *
1268df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1269df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1270df8bae1dSRodney W. Grimes  *
1271df8bae1dSRodney W. Grimes  *	Conditions:
1272df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1273df8bae1dSRodney W. Grimes  */
12740d94caffSDavid Greenman boolean_t
1275a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1276df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1277a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1278df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1279df8bae1dSRodney W. Grimes {
1280df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1281df8bae1dSRodney W. Grimes 
1282df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1283df8bae1dSRodney W. Grimes 		return (TRUE);
1284df8bae1dSRodney W. Grimes 	}
1285df8bae1dSRodney W. Grimes 
128630dcfc09SJohn Dyson 	if (prev_object->type != OBJT_DEFAULT) {
128730dcfc09SJohn Dyson 		return (FALSE);
128830dcfc09SJohn Dyson 	}
128930dcfc09SJohn Dyson 
1290df8bae1dSRodney W. Grimes 	/*
1291df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1292df8bae1dSRodney W. Grimes 	 */
1293df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1294df8bae1dSRodney W. Grimes 
1295df8bae1dSRodney W. Grimes 	/*
12960d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
12970d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
12980d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1299df8bae1dSRodney W. Grimes 	 */
1300df8bae1dSRodney W. Grimes 
1301df8bae1dSRodney W. Grimes 	if (prev_object->ref_count > 1 ||
130224a1cce3SDavid Greenman 	    prev_object->backing_object != NULL) {
1303df8bae1dSRodney W. Grimes 		return (FALSE);
1304df8bae1dSRodney W. Grimes 	}
1305a316d390SJohn Dyson 
1306a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1307a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
1308df8bae1dSRodney W. Grimes 	/*
13090d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
13100d94caffSDavid Greenman 	 * deallocation.
1311df8bae1dSRodney W. Grimes 	 */
1312df8bae1dSRodney W. Grimes 
1313df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1314a316d390SJohn Dyson 	    prev_pindex + prev_size,
1315a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1316df8bae1dSRodney W. Grimes 
1317df8bae1dSRodney W. Grimes 	/*
1318df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1319df8bae1dSRodney W. Grimes 	 */
1320a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1321df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1322df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1323df8bae1dSRodney W. Grimes 
1324df8bae1dSRodney W. Grimes 	return (TRUE);
1325df8bae1dSRodney W. Grimes }
1326df8bae1dSRodney W. Grimes 
1327c7c34a24SBruce Evans #include "opt_ddb.h"
1328c3cb3e12SDavid Greenman #ifdef DDB
1329c7c34a24SBruce Evans #include <sys/kernel.h>
1330c7c34a24SBruce Evans 
1331c7c34a24SBruce Evans #include <machine/cons.h>
1332c7c34a24SBruce Evans 
1333c7c34a24SBruce Evans #include <ddb/ddb.h>
1334c7c34a24SBruce Evans 
1335c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1336c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1337c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1338c3cb3e12SDavid Greenman 
1339cac597e4SBruce Evans static int
1340a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1341a1f6d91cSDavid Greenman 	vm_map_t map;
1342a1f6d91cSDavid Greenman 	vm_object_t object;
1343a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1344a1f6d91cSDavid Greenman {
1345a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1346a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1347a1f6d91cSDavid Greenman 	vm_object_t obj;
1348a1f6d91cSDavid Greenman 	int entcount;
1349a1f6d91cSDavid Greenman 
1350a1f6d91cSDavid Greenman 	if (map == 0)
1351a1f6d91cSDavid Greenman 		return 0;
1352a1f6d91cSDavid Greenman 
1353a1f6d91cSDavid Greenman 	if (entry == 0) {
1354a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1355a1f6d91cSDavid Greenman 		entcount = map->nentries;
1356a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1357a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1358a1f6d91cSDavid Greenman 				return 1;
1359a1f6d91cSDavid Greenman 			}
1360a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1361a1f6d91cSDavid Greenman 		}
1362a1f6d91cSDavid Greenman 	} else if (entry->is_sub_map || entry->is_a_map) {
1363a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1364a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1365a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1366a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1367a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1368a1f6d91cSDavid Greenman 				return 1;
1369a1f6d91cSDavid Greenman 			}
1370a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1371a1f6d91cSDavid Greenman 		}
1372a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
137324a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1374a1f6d91cSDavid Greenman 			if( obj == object) {
1375a1f6d91cSDavid Greenman 				return 1;
1376a1f6d91cSDavid Greenman 			}
1377a1f6d91cSDavid Greenman 	}
1378a1f6d91cSDavid Greenman 	return 0;
1379a1f6d91cSDavid Greenman }
1380a1f6d91cSDavid Greenman 
1381cac597e4SBruce Evans static int
1382a1f6d91cSDavid Greenman vm_object_in_map( object)
1383a1f6d91cSDavid Greenman 	vm_object_t object;
1384a1f6d91cSDavid Greenman {
1385a1f6d91cSDavid Greenman 	struct proc *p;
13861b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1387a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1388a1f6d91cSDavid Greenman 			continue;
1389a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1390a1f6d91cSDavid Greenman 			return 1;
1391a1f6d91cSDavid Greenman 	}
1392a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1393a1f6d91cSDavid Greenman 		return 1;
1394a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1395a1f6d91cSDavid Greenman 		return 1;
1396a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1397a1f6d91cSDavid Greenman 		return 1;
1398a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1399a1f6d91cSDavid Greenman 		return 1;
1400a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1401a1f6d91cSDavid Greenman 		return 1;
1402a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1403a1f6d91cSDavid Greenman 		return 1;
1404a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1405a1f6d91cSDavid Greenman 		return 1;
1406a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1407a1f6d91cSDavid Greenman 		return 1;
1408a1f6d91cSDavid Greenman 	return 0;
1409a1f6d91cSDavid Greenman }
1410a1f6d91cSDavid Greenman 
1411c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1412f708ef1bSPoul-Henning Kamp {
1413a1f6d91cSDavid Greenman 	vm_object_t object;
1414a1f6d91cSDavid Greenman 
1415a1f6d91cSDavid Greenman 	/*
1416a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1417a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1418a1f6d91cSDavid Greenman 	 */
1419b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1420a1f6d91cSDavid Greenman 			object != NULL;
1421b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
142224a1cce3SDavid Greenman 		if (object->handle == NULL &&
142324a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1424a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1425c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1426a1f6d91cSDavid Greenman 					object->size);
1427a1f6d91cSDavid Greenman 			}
1428a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1429c7c34a24SBruce Evans 				db_printf("vmochk: internal obj is not in a map: "
14303af76890SPoul-Henning Kamp 		"ref: %d, size: %d: 0x%x, backing_object: 0x%x\n",
14313af76890SPoul-Henning Kamp 				    object->ref_count, object->size,
14323af76890SPoul-Henning Kamp 				    object->size, object->backing_object);
1433a1f6d91cSDavid Greenman 			}
1434a1f6d91cSDavid Greenman 		}
1435a1f6d91cSDavid Greenman 	}
1436a1f6d91cSDavid Greenman }
1437a1f6d91cSDavid Greenman 
143826f9a767SRodney W. Grimes /*
1439df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1440df8bae1dSRodney W. Grimes  */
1441c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1442df8bae1dSRodney W. Grimes {
1443c7c34a24SBruce Evans 	/* XXX convert args. */
1444c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1445c7c34a24SBruce Evans 	boolean_t full = have_addr;
1446c7c34a24SBruce Evans 
1447df8bae1dSRodney W. Grimes 	register vm_page_t p;
1448df8bae1dSRodney W. Grimes 
1449c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1450c7c34a24SBruce Evans #define	count	was_count
1451c7c34a24SBruce Evans 
1452df8bae1dSRodney W. Grimes 	register int count;
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes 	if (object == NULL)
1455df8bae1dSRodney W. Grimes 		return;
1456df8bae1dSRodney W. Grimes 
1457c7c34a24SBruce Evans 	db_iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ",
1458df8bae1dSRodney W. Grimes 	    (int) object, (int) object->size,
1459df8bae1dSRodney W. Grimes 	    object->resident_page_count, object->ref_count);
1460c7c34a24SBruce Evans 	db_printf("offset=0x%x, backing_object=(0x%x)+0x%x\n",
14612a4895f4SDavid Greenman 	    (int) object->paging_offset,
146224a1cce3SDavid Greenman 	    (int) object->backing_object, (int) object->backing_object_offset);
1463c7c34a24SBruce Evans 	db_printf("cache: next=%p, prev=%p\n",
1464b18bfc3dSJohn Dyson 	    TAILQ_NEXT(object, cached_list), TAILQ_PREV(object, cached_list));
1465df8bae1dSRodney W. Grimes 
1466df8bae1dSRodney W. Grimes 	if (!full)
1467df8bae1dSRodney W. Grimes 		return;
1468df8bae1dSRodney W. Grimes 
1469c7c34a24SBruce Evans 	db_indent += 2;
1470df8bae1dSRodney W. Grimes 	count = 0;
1471b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1472df8bae1dSRodney W. Grimes 		if (count == 0)
1473c7c34a24SBruce Evans 			db_iprintf("memory:=");
1474df8bae1dSRodney W. Grimes 		else if (count == 6) {
1475c7c34a24SBruce Evans 			db_printf("\n");
1476c7c34a24SBruce Evans 			db_iprintf(" ...");
1477df8bae1dSRodney W. Grimes 			count = 0;
1478df8bae1dSRodney W. Grimes 		} else
1479c7c34a24SBruce Evans 			db_printf(",");
1480df8bae1dSRodney W. Grimes 		count++;
1481df8bae1dSRodney W. Grimes 
1482c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1483a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1484df8bae1dSRodney W. Grimes 	}
1485df8bae1dSRodney W. Grimes 	if (count != 0)
1486c7c34a24SBruce Evans 		db_printf("\n");
1487c7c34a24SBruce Evans 	db_indent -= 2;
1488df8bae1dSRodney W. Grimes }
14895070c7f8SJohn Dyson 
1490c7c34a24SBruce Evans /* XXX. */
1491c7c34a24SBruce Evans #undef count
1492c7c34a24SBruce Evans 
1493c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
14945070c7f8SJohn Dyson void
1495c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1496c7c34a24SBruce Evans 	db_expr_t addr;
1497c7c34a24SBruce Evans 	boolean_t have_addr;
1498c7c34a24SBruce Evans 	db_expr_t count;
1499c7c34a24SBruce Evans 	char *modif;
1500c7c34a24SBruce Evans {
1501c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1502c7c34a24SBruce Evans }
1503c7c34a24SBruce Evans 
1504c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
15055070c7f8SJohn Dyson {
15065070c7f8SJohn Dyson 	vm_object_t object;
15075070c7f8SJohn Dyson 	int nl = 0;
15085070c7f8SJohn Dyson 	int c;
15095070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
15105070c7f8SJohn Dyson 			object != NULL;
15115070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
15125070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
15135070c7f8SJohn Dyson 		vm_pindex_t osize;
15145070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
15155070c7f8SJohn Dyson 		int rcount;
15165070c7f8SJohn Dyson 		vm_page_t m;
15175070c7f8SJohn Dyson 
15185070c7f8SJohn Dyson 		db_printf("new object: 0x%x\n", object);
15195070c7f8SJohn Dyson 		if ( nl > 18) {
15205070c7f8SJohn Dyson 			c = cngetc();
15215070c7f8SJohn Dyson 			if (c != ' ')
15225070c7f8SJohn Dyson 				return;
15235070c7f8SJohn Dyson 			nl = 0;
15245070c7f8SJohn Dyson 		}
15255070c7f8SJohn Dyson 		nl++;
15265070c7f8SJohn Dyson 		rcount = 0;
15275070c7f8SJohn Dyson 		fidx = 0;
15285070c7f8SJohn Dyson 		osize = object->size;
15295070c7f8SJohn Dyson 		if (osize > 128)
15305070c7f8SJohn Dyson 			osize = 128;
15315070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
15325070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
15335070c7f8SJohn Dyson 			if (m == NULL) {
15345070c7f8SJohn Dyson 				if (rcount) {
15355070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
15365070c7f8SJohn Dyson 						fidx, rcount, pa);
15375070c7f8SJohn Dyson 					if ( nl > 18) {
15385070c7f8SJohn Dyson 						c = cngetc();
15395070c7f8SJohn Dyson 						if (c != ' ')
15405070c7f8SJohn Dyson 							return;
15415070c7f8SJohn Dyson 						nl = 0;
15425070c7f8SJohn Dyson 					}
15435070c7f8SJohn Dyson 					nl++;
15445070c7f8SJohn Dyson 					rcount = 0;
15455070c7f8SJohn Dyson 				}
15465070c7f8SJohn Dyson 				continue;
15475070c7f8SJohn Dyson 			}
15485070c7f8SJohn Dyson 
15495070c7f8SJohn Dyson 
15505070c7f8SJohn Dyson 			if (rcount &&
15515070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
15525070c7f8SJohn Dyson 				++rcount;
15535070c7f8SJohn Dyson 				continue;
15545070c7f8SJohn Dyson 			}
15555070c7f8SJohn Dyson 			if (rcount) {
15565070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
15575070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
15585070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
15595070c7f8SJohn Dyson 				if (padiff == 0) {
15605070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
15615070c7f8SJohn Dyson 					++rcount;
15625070c7f8SJohn Dyson 					continue;
15635070c7f8SJohn Dyson 				}
15645070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
15655070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
15665070c7f8SJohn Dyson 				if ( nl > 18) {
15675070c7f8SJohn Dyson 					c = cngetc();
15685070c7f8SJohn Dyson 					if (c != ' ')
15695070c7f8SJohn Dyson 						return;
15705070c7f8SJohn Dyson 					nl = 0;
15715070c7f8SJohn Dyson 				}
15725070c7f8SJohn Dyson 				nl++;
15735070c7f8SJohn Dyson 			}
15745070c7f8SJohn Dyson 			fidx = idx;
15755070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
15765070c7f8SJohn Dyson 			rcount = 1;
15775070c7f8SJohn Dyson 		}
15785070c7f8SJohn Dyson 		if (rcount) {
15795070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
15805070c7f8SJohn Dyson 			if ( nl > 18) {
15815070c7f8SJohn Dyson 				c = cngetc();
15825070c7f8SJohn Dyson 				if (c != ' ')
15835070c7f8SJohn Dyson 					return;
15845070c7f8SJohn Dyson 				nl = 0;
15855070c7f8SJohn Dyson 			}
15865070c7f8SJohn Dyson 			nl++;
15875070c7f8SJohn Dyson 		}
15885070c7f8SJohn Dyson 	}
15895070c7f8SJohn Dyson }
1590c3cb3e12SDavid Greenman #endif /* DDB */
1591