xref: /freebsd/sys/vm/vm_object.c (revision 8cc7e047a3057d571fc8130358fe1f72677586f3)
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  *
648cc7e047SJohn Dyson  * $Id: vm_object.c,v 1.82 1996/09/28 03:33:26 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;
167a2f4a846SJohn Dyson 	object->page_hint = NULL;
168a1f6d91cSDavid Greenman 
169a1f6d91cSDavid Greenman 	object->last_read = 0;
170df8bae1dSRodney W. Grimes 
171df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
172df8bae1dSRodney W. Grimes 	vm_object_count++;
173df8bae1dSRodney W. Grimes }
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes /*
17626f9a767SRodney W. Grimes  *	vm_object_init:
17726f9a767SRodney W. Grimes  *
17826f9a767SRodney W. Grimes  *	Initialize the VM objects module.
17926f9a767SRodney W. Grimes  */
18026f9a767SRodney W. Grimes void
181a316d390SJohn Dyson vm_object_init()
18226f9a767SRodney W. Grimes {
18326f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_cached_list);
18426f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
18526f9a767SRodney W. Grimes 	vm_object_count = 0;
1860217125fSDavid Greenman 
1870217125fSDavid Greenman 	vm_object_cache_max = 84;
1880217125fSDavid Greenman 	if (cnt.v_page_count > 1000)
18946268a60SDavid Greenman 		vm_object_cache_max += (cnt.v_page_count - 1000) / 4;
19026f9a767SRodney W. Grimes 
19126f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
192a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19326f9a767SRodney W. Grimes 	    kernel_object);
19426f9a767SRodney W. Grimes 
19526f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
196a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19726f9a767SRodney W. Grimes 	    kmem_object);
19826f9a767SRodney W. Grimes }
19926f9a767SRodney W. Grimes 
20026f9a767SRodney W. Grimes /*
20126f9a767SRodney W. Grimes  *	vm_object_allocate:
20226f9a767SRodney W. Grimes  *
20326f9a767SRodney W. Grimes  *	Returns a new object with the given size.
20426f9a767SRodney W. Grimes  */
20526f9a767SRodney W. Grimes 
20626f9a767SRodney W. Grimes vm_object_t
20724a1cce3SDavid Greenman vm_object_allocate(type, size)
20824a1cce3SDavid Greenman 	objtype_t type;
20926f9a767SRodney W. Grimes 	vm_size_t size;
21026f9a767SRodney W. Grimes {
21126f9a767SRodney W. Grimes 	register vm_object_t result;
21226f9a767SRodney W. Grimes 
21326f9a767SRodney W. Grimes 	result = (vm_object_t)
21426f9a767SRodney W. Grimes 	    malloc((u_long) sizeof *result, M_VMOBJ, M_WAITOK);
21526f9a767SRodney W. Grimes 
21667bf6868SJohn Dyson 
21724a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
21826f9a767SRodney W. Grimes 
21926f9a767SRodney W. Grimes 	return (result);
22026f9a767SRodney W. Grimes }
22126f9a767SRodney W. Grimes 
22226f9a767SRodney W. Grimes 
22326f9a767SRodney W. Grimes /*
224df8bae1dSRodney W. Grimes  *	vm_object_reference:
225df8bae1dSRodney W. Grimes  *
226df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
227df8bae1dSRodney W. Grimes  */
2286476c0d2SJohn Dyson void
22926f9a767SRodney W. Grimes vm_object_reference(object)
230df8bae1dSRodney W. Grimes 	register vm_object_t object;
231df8bae1dSRodney W. Grimes {
232df8bae1dSRodney W. Grimes 	if (object == NULL)
233df8bae1dSRodney W. Grimes 		return;
234df8bae1dSRodney W. Grimes 
23524a1cce3SDavid Greenman 	if (object->ref_count == 0) {
23624a1cce3SDavid Greenman 		if ((object->flags & OBJ_CANPERSIST) == 0)
23724a1cce3SDavid Greenman 			panic("vm_object_reference: non-persistent object with 0 ref_count");
23824a1cce3SDavid Greenman 		TAILQ_REMOVE(&vm_object_cached_list, object, cached_list);
23924a1cce3SDavid Greenman 		vm_object_cached--;
24024a1cce3SDavid Greenman 	}
241df8bae1dSRodney W. Grimes 	object->ref_count++;
242df8bae1dSRodney W. Grimes }
243df8bae1dSRodney W. Grimes 
244df8bae1dSRodney W. Grimes /*
245df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
246df8bae1dSRodney W. Grimes  *
247df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
248df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
249df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
250df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
251df8bae1dSRodney W. Grimes  *	may be relinquished.
252df8bae1dSRodney W. Grimes  *
253df8bae1dSRodney W. Grimes  *	No object may be locked.
254df8bae1dSRodney W. Grimes  */
25526f9a767SRodney W. Grimes void
25626f9a767SRodney W. Grimes vm_object_deallocate(object)
25726f9a767SRodney W. Grimes 	vm_object_t object;
258df8bae1dSRodney W. Grimes {
259df8bae1dSRodney W. Grimes 	vm_object_t temp;
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 	while (object != NULL) {
262df8bae1dSRodney W. Grimes 
263ba8da839SDavid Greenman 		if (object->ref_count == 0)
264ba8da839SDavid Greenman 			panic("vm_object_deallocate: object deallocated too many times");
265ba8da839SDavid Greenman 
266df8bae1dSRodney W. Grimes 		/*
267df8bae1dSRodney W. Grimes 		 * Lose the reference
268df8bae1dSRodney W. Grimes 		 */
269be6d5bfaSDavid Greenman 		object->ref_count--;
270be6d5bfaSDavid Greenman 		if (object->ref_count != 0) {
271be6d5bfaSDavid Greenman 			if ((object->ref_count == 1) &&
27224a1cce3SDavid Greenman 			    (object->handle == NULL) &&
27324a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
27424a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
275a1f6d91cSDavid Greenman 				vm_object_t robject;
276b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
277be6d5bfaSDavid Greenman 				if ((robject != NULL) &&
27824a1cce3SDavid Greenman 				    (robject->handle == NULL) &&
27924a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
28024a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
281a1f6d91cSDavid Greenman 					int s;
282a1f6d91cSDavid Greenman 					robject->ref_count += 2;
2839b4814bbSDavid Greenman 					object->ref_count += 2;
284a1f6d91cSDavid Greenman 
285a1f6d91cSDavid Greenman 					do {
286b18bfc3dSJohn Dyson 						s = splvm();
287a1f6d91cSDavid Greenman 						while (robject->paging_in_progress) {
288c0503609SDavid Greenman 							robject->flags |= OBJ_PIPWNT;
289a1f6d91cSDavid Greenman 							tsleep(robject, PVM, "objde1", 0);
2900d94caffSDavid Greenman 						}
291a1f6d91cSDavid Greenman 
292a1f6d91cSDavid Greenman 						while (object->paging_in_progress) {
293c0503609SDavid Greenman 							object->flags |= OBJ_PIPWNT;
294a1f6d91cSDavid Greenman 							tsleep(object, PVM, "objde2", 0);
295a1f6d91cSDavid Greenman 						}
296a1f6d91cSDavid Greenman 						splx(s);
297a1f6d91cSDavid Greenman 
298a1f6d91cSDavid Greenman 					} while( object->paging_in_progress || robject->paging_in_progress);
299a1f6d91cSDavid Greenman 
3009b4814bbSDavid Greenman 					object->ref_count -= 2;
301a1f6d91cSDavid Greenman 					robject->ref_count -= 2;
302a1f6d91cSDavid Greenman 					if( robject->ref_count == 0) {
303a1f6d91cSDavid Greenman 						robject->ref_count += 1;
304ba8da839SDavid Greenman 						object = robject;
305ba8da839SDavid Greenman 						continue;
306a1f6d91cSDavid Greenman 					}
307a1f6d91cSDavid Greenman 					vm_object_collapse(robject);
308a1f6d91cSDavid Greenman 					return;
309010cf3b9SDavid Greenman 				}
310010cf3b9SDavid Greenman 			}
311df8bae1dSRodney W. Grimes 			/*
3120d94caffSDavid Greenman 			 * If there are still references, then we are done.
313df8bae1dSRodney W. Grimes 			 */
314df8bae1dSRodney W. Grimes 			return;
315df8bae1dSRodney W. Grimes 		}
316df8bae1dSRodney W. Grimes 
31724a1cce3SDavid Greenman 		if (object->type == OBJT_VNODE) {
31824a1cce3SDavid Greenman 			struct vnode *vp = object->handle;
319f5cf85d4SDavid Greenman 
32024a1cce3SDavid Greenman 			vp->v_flag &= ~VTEXT;
321f5cf85d4SDavid Greenman 		}
322f5cf85d4SDavid Greenman 
323f919ebdeSDavid Greenman 		/*
324f919ebdeSDavid Greenman 		 * See if this object can persist and has some resident
325f919ebdeSDavid Greenman 		 * pages.  If so, enter it in the cache.
326f919ebdeSDavid Greenman 		 */
327f5cf85d4SDavid Greenman 		if (object->flags & OBJ_CANPERSIST) {
328f5cf85d4SDavid Greenman 			if (object->resident_page_count != 0) {
3296476c0d2SJohn Dyson #if 0
33024a1cce3SDavid Greenman 				vm_object_page_clean(object, 0, 0 ,TRUE, TRUE);
3316476c0d2SJohn Dyson #endif
332df8bae1dSRodney W. Grimes 				TAILQ_INSERT_TAIL(&vm_object_cached_list, object,
333df8bae1dSRodney W. Grimes 				    cached_list);
334df8bae1dSRodney W. Grimes 				vm_object_cached++;
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 				vm_object_cache_trim();
337df8bae1dSRodney W. Grimes 				return;
338f5cf85d4SDavid Greenman 			} else {
339f5cf85d4SDavid Greenman 				object->flags &= ~OBJ_CANPERSIST;
340f5cf85d4SDavid Greenman 			}
341df8bae1dSRodney W. Grimes 		}
342f919ebdeSDavid Greenman 
343df8bae1dSRodney W. Grimes 		/*
34424a1cce3SDavid Greenman 		 * Make sure no one uses us.
345df8bae1dSRodney W. Grimes 		 */
3460d94caffSDavid Greenman 		object->flags |= OBJ_DEAD;
347df8bae1dSRodney W. Grimes 
34824a1cce3SDavid Greenman 		temp = object->backing_object;
349de5f6a77SJohn Dyson 		if (temp) {
35024a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
351de5f6a77SJohn Dyson 			--temp->shadow_count;
352de5f6a77SJohn Dyson 		}
353df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
354df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
355df8bae1dSRodney W. Grimes 		object = temp;
356df8bae1dSRodney W. Grimes 	}
357df8bae1dSRodney W. Grimes }
358df8bae1dSRodney W. Grimes 
359df8bae1dSRodney W. Grimes /*
360df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
361df8bae1dSRodney W. Grimes  *	up all previously used resources.
362df8bae1dSRodney W. Grimes  *
363df8bae1dSRodney W. Grimes  *	The object must be locked.
364df8bae1dSRodney W. Grimes  */
365f708ef1bSPoul-Henning Kamp static void
36626f9a767SRodney W. Grimes vm_object_terminate(object)
367df8bae1dSRodney W. Grimes 	register vm_object_t object;
368df8bae1dSRodney W. Grimes {
3693af76890SPoul-Henning Kamp 	register vm_page_t p;
37026f9a767SRodney W. Grimes 	int s;
371df8bae1dSRodney W. Grimes 
372df8bae1dSRodney W. Grimes 	/*
373f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
374df8bae1dSRodney W. Grimes 	 */
375b18bfc3dSJohn Dyson 	s = splvm();
376df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
377c0503609SDavid Greenman 		object->flags |= OBJ_PIPWNT;
37824a1cce3SDavid Greenman 		tsleep(object, PVM, "objtrm", 0);
379df8bae1dSRodney W. Grimes 	}
3800d94caffSDavid Greenman 	splx(s);
381df8bae1dSRodney W. Grimes 
38226f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
38326f9a767SRodney W. Grimes 		panic("vm_object_deallocate: pageout in progress");
38426f9a767SRodney W. Grimes 
38526f9a767SRodney W. Grimes 	/*
3860d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
3870d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
38826f9a767SRodney W. Grimes 	 */
38924a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
39024a1cce3SDavid Greenman 		struct vnode *vp = object->handle;
3916476c0d2SJohn Dyson 		int waslocked;
392f6b04d2bSDavid Greenman 
3936476c0d2SJohn Dyson 		waslocked = VOP_ISLOCKED(vp);
3946476c0d2SJohn Dyson 		if (!waslocked)
3950426122fSDavid Greenman 			VOP_LOCK(vp);
39624a1cce3SDavid Greenman 		vm_object_page_clean(object, 0, 0, TRUE, FALSE);
397f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
3986476c0d2SJohn Dyson 		if (!waslocked)
3990426122fSDavid Greenman 			VOP_UNLOCK(vp);
400df8bae1dSRodney W. Grimes 	}
4010d94caffSDavid Greenman 	/*
4020d94caffSDavid Greenman 	 * Now free the pages. For internal objects, this also removes them
4030d94caffSDavid Greenman 	 * from paging queues.
404df8bae1dSRodney W. Grimes 	 */
405b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
406a2f4a846SJohn Dyson 		if (p->busy || (p->flags & PG_BUSY))
407be6d5bfaSDavid Greenman 			printf("vm_object_terminate: freeing busy page\n");
4080d94caffSDavid Greenman 		PAGE_WAKEUP(p);
409df8bae1dSRodney W. Grimes 		vm_page_free(p);
410df8bae1dSRodney W. Grimes 		cnt.v_pfree++;
411df8bae1dSRodney W. Grimes 	}
412df8bae1dSRodney W. Grimes 
413df8bae1dSRodney W. Grimes 	/*
414df8bae1dSRodney W. Grimes 	 * Let the pager know object is dead.
415df8bae1dSRodney W. Grimes 	 */
41624a1cce3SDavid Greenman 	vm_pager_deallocate(object);
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
419df8bae1dSRodney W. Grimes 	vm_object_count--;
420df8bae1dSRodney W. Grimes 
4210426122fSDavid Greenman 	wakeup(object);
4220426122fSDavid Greenman 
423df8bae1dSRodney W. Grimes 	/*
424df8bae1dSRodney W. Grimes 	 * Free the space for the object.
425df8bae1dSRodney W. Grimes 	 */
426df8bae1dSRodney W. Grimes 	free((caddr_t) object, M_VMOBJ);
427df8bae1dSRodney W. Grimes }
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes /*
430df8bae1dSRodney W. Grimes  *	vm_object_page_clean
431df8bae1dSRodney W. Grimes  *
432df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
43326f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
43426f9a767SRodney W. Grimes  *
43526f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
43626f9a767SRodney W. Grimes  *
43726f9a767SRodney W. Grimes  *	The object must be locked.
43826f9a767SRodney W. Grimes  */
439f6b04d2bSDavid Greenman 
440f6b04d2bSDavid Greenman void
44124a1cce3SDavid Greenman vm_object_page_clean(object, start, end, syncio, lockflag)
442f6b04d2bSDavid Greenman 	vm_object_t object;
443a316d390SJohn Dyson 	vm_pindex_t start;
444a316d390SJohn Dyson 	vm_pindex_t end;
445f6b04d2bSDavid Greenman 	boolean_t syncio;
44624a1cce3SDavid Greenman 	boolean_t lockflag;
447f6b04d2bSDavid Greenman {
448bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
449f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
450bd7e5f99SJohn Dyson 	vm_pindex_t pi;
451aef922f5SJohn Dyson 	int s;
45224a1cce3SDavid Greenman 	struct vnode *vp;
453aef922f5SJohn Dyson 	int runlen;
454bd7e5f99SJohn Dyson 	int maxf;
455bd7e5f99SJohn Dyson 	int chkb;
456bd7e5f99SJohn Dyson 	int maxb;
457bd7e5f99SJohn Dyson 	int i;
458bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
459bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
460aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
461f6b04d2bSDavid Greenman 
462aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
463aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
464f6b04d2bSDavid Greenman 		return;
465f6b04d2bSDavid Greenman 
46624a1cce3SDavid Greenman 	vp = object->handle;
46724a1cce3SDavid Greenman 
46824a1cce3SDavid Greenman 	if (lockflag)
46924a1cce3SDavid Greenman 		VOP_LOCK(vp);
470aef922f5SJohn Dyson 	object->flags |= OBJ_CLEANING;
47124a1cce3SDavid Greenman 
472f6b04d2bSDavid Greenman 	tstart = start;
473f6b04d2bSDavid Greenman 	if (end == 0) {
474f6b04d2bSDavid Greenman 		tend = object->size;
475f6b04d2bSDavid Greenman 	} else {
476f6b04d2bSDavid Greenman 		tend = end;
477f6b04d2bSDavid Greenman 	}
478a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
479aef922f5SJohn Dyson 		object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
480ec4f9fb0SDavid Greenman 	}
481b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq))
482bd7e5f99SJohn Dyson 		p->flags |= PG_CLEANCHK;
483f6b04d2bSDavid Greenman 
484bd7e5f99SJohn Dyson rescan:
485b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
486b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
487bd7e5f99SJohn Dyson 
488bd7e5f99SJohn Dyson 		pi = p->pindex;
489bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
490bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
4915070c7f8SJohn Dyson 			(p->valid == 0) ||
4925070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
493bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
494aef922f5SJohn Dyson 			continue;
495f6b04d2bSDavid Greenman 		}
496f6b04d2bSDavid Greenman 
497bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
498bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
499bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
500bd7e5f99SJohn Dyson 			continue;
501bd7e5f99SJohn Dyson 		}
502ec4f9fb0SDavid Greenman 
503b18bfc3dSJohn Dyson 		s = splvm();
504bd7e5f99SJohn Dyson 		if ((p->flags & PG_BUSY) || p->busy) {
505aef922f5SJohn Dyson 			p->flags |= PG_WANTED|PG_REFERENCED;
506aef922f5SJohn Dyson 			tsleep(p, PVM, "vpcwai", 0);
507f6b04d2bSDavid Greenman 			splx(s);
508bd7e5f99SJohn Dyson 			goto rescan;
509f6b04d2bSDavid Greenman 		}
510f6b04d2bSDavid Greenman 		splx(s);
511f6b04d2bSDavid Greenman 
512f35329acSJohn Dyson 		s = splvm();
513bd7e5f99SJohn Dyson 		maxf = 0;
514bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
515bd7e5f99SJohn Dyson 			if (tp = vm_page_lookup(object, pi + i)) {
516bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
517bd7e5f99SJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0)
518bd7e5f99SJohn Dyson 					break;
5195070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
5203077a9c2SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
5213077a9c2SJohn Dyson 					break;
5223077a9c2SJohn Dyson 				}
523bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
524bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
525bd7e5f99SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
526bd7e5f99SJohn Dyson 					break;
527bd7e5f99SJohn Dyson 				}
528bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
529bd7e5f99SJohn Dyson 				maxf++;
530bd7e5f99SJohn Dyson 				continue;
531bd7e5f99SJohn Dyson 			}
532bd7e5f99SJohn Dyson 			break;
533bd7e5f99SJohn Dyson 		}
534aef922f5SJohn Dyson 
535bd7e5f99SJohn Dyson 		maxb = 0;
536bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
537bd7e5f99SJohn Dyson 		if (chkb) {
538bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
539bd7e5f99SJohn Dyson 				if (tp = vm_page_lookup(object, pi - i)) {
540bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
541bd7e5f99SJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0)
542bd7e5f99SJohn Dyson 						break;
5435070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
5443077a9c2SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
5453077a9c2SJohn Dyson 						break;
5463077a9c2SJohn Dyson 					}
547bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
548bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
549bd7e5f99SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
550bd7e5f99SJohn Dyson 						break;
551bd7e5f99SJohn Dyson 					}
552bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
553bd7e5f99SJohn Dyson 					maxb++;
554bd7e5f99SJohn Dyson 					continue;
555bd7e5f99SJohn Dyson 				}
556bd7e5f99SJohn Dyson 				break;
557bd7e5f99SJohn Dyson 			}
558bd7e5f99SJohn Dyson 		}
559bd7e5f99SJohn Dyson 
560bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
561bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
562bd7e5f99SJohn Dyson 			ma[index] = mab[i];
563bd7e5f99SJohn Dyson 			ma[index]->flags |= PG_BUSY;
564bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
565bd7e5f99SJohn Dyson 			vm_page_protect(ma[index], VM_PROT_READ);
566bd7e5f99SJohn Dyson 		}
567bd7e5f99SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
568aef922f5SJohn Dyson 		p->flags |= PG_BUSY;
569bd7e5f99SJohn Dyson 		p->flags &= ~PG_CLEANCHK;
570bd7e5f99SJohn Dyson 		ma[maxb] = p;
571bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
572bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
573bd7e5f99SJohn Dyson 			ma[index] = maf[i];
574bd7e5f99SJohn Dyson 			ma[index]->flags |= PG_BUSY;
575bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
576bd7e5f99SJohn Dyson 			vm_page_protect(ma[index], VM_PROT_READ);
577f6b04d2bSDavid Greenman 		}
578bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
579f35329acSJohn Dyson 		splx(s);
580bd7e5f99SJohn Dyson 		vm_pageout_flush(ma, runlen, 0);
581bd7e5f99SJohn Dyson 		goto rescan;
582f6b04d2bSDavid Greenman 	}
583aef922f5SJohn Dyson 
584aef922f5SJohn Dyson 	VOP_FSYNC(vp, NULL, syncio, curproc);
585aef922f5SJohn Dyson 
58624a1cce3SDavid Greenman 	if (lockflag)
587d3628763SRodney W. Grimes 		VOP_UNLOCK(vp);
588aef922f5SJohn Dyson 	object->flags &= ~OBJ_CLEANING;
589f5cf85d4SDavid Greenman 	return;
59026f9a767SRodney W. Grimes }
591df8bae1dSRodney W. Grimes 
592f708ef1bSPoul-Henning Kamp #ifdef not_used
593f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
594df8bae1dSRodney W. Grimes /*
595df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
596df8bae1dSRodney W. Grimes  *
597df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
598df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
599df8bae1dSRodney W. Grimes  *
600df8bae1dSRodney W. Grimes  *	The object must be locked.
601df8bae1dSRodney W. Grimes  */
602f708ef1bSPoul-Henning Kamp static void
603df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
604df8bae1dSRodney W. Grimes 	register vm_object_t object;
605df8bae1dSRodney W. Grimes {
606df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
607df8bae1dSRodney W. Grimes 
608b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
609b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
610df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
611df8bae1dSRodney W. Grimes 	}
612df8bae1dSRodney W. Grimes }
613f708ef1bSPoul-Henning Kamp #endif
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes /*
616df8bae1dSRodney W. Grimes  *	Trim the object cache to size.
617df8bae1dSRodney W. Grimes  */
618f708ef1bSPoul-Henning Kamp static void
619df8bae1dSRodney W. Grimes vm_object_cache_trim()
620df8bae1dSRodney W. Grimes {
621df8bae1dSRodney W. Grimes 	register vm_object_t object;
622df8bae1dSRodney W. Grimes 
6230d94caffSDavid Greenman 	while (vm_object_cached > vm_object_cache_max) {
624b18bfc3dSJohn Dyson 		object = TAILQ_FIRST(&vm_object_cached_list);
625df8bae1dSRodney W. Grimes 
62624a1cce3SDavid Greenman 		vm_object_reference(object);
627df8bae1dSRodney W. Grimes 		pager_cache(object, FALSE);
628df8bae1dSRodney W. Grimes 	}
629df8bae1dSRodney W. Grimes }
630df8bae1dSRodney W. Grimes 
63126f9a767SRodney W. Grimes 
632df8bae1dSRodney W. Grimes /*
633df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
634df8bae1dSRodney W. Grimes  *
635df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
636df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
637df8bae1dSRodney W. Grimes  *	references to these pages should remain.
638df8bae1dSRodney W. Grimes  *
639df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
640df8bae1dSRodney W. Grimes  */
6410d94caffSDavid Greenman void
6420d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
643df8bae1dSRodney W. Grimes 	register vm_object_t object;
644a316d390SJohn Dyson 	register vm_pindex_t start;
645a316d390SJohn Dyson 	register vm_pindex_t end;
646df8bae1dSRodney W. Grimes {
647df8bae1dSRodney W. Grimes 	register vm_page_t p;
648df8bae1dSRodney W. Grimes 
649aef922f5SJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
650df8bae1dSRodney W. Grimes 		return;
651df8bae1dSRodney W. Grimes 
652b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
653b5b40fa6SJohn Dyson 		p != NULL;
654b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
655f919ebdeSDavid Greenman 		vm_page_protect(p, VM_PROT_READ);
656df8bae1dSRodney W. Grimes 	}
657aef922f5SJohn Dyson 
658aef922f5SJohn Dyson 	object->flags &= ~OBJ_WRITEABLE;
659df8bae1dSRodney W. Grimes }
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes /*
662df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
663df8bae1dSRodney W. Grimes  *
664df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
665df8bae1dSRodney W. Grimes  *	object range from all physical maps.
666df8bae1dSRodney W. Grimes  *
667df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
668df8bae1dSRodney W. Grimes  */
66926f9a767SRodney W. Grimes void
67026f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
671df8bae1dSRodney W. Grimes 	register vm_object_t object;
672a316d390SJohn Dyson 	register vm_pindex_t start;
673a316d390SJohn Dyson 	register vm_pindex_t end;
674df8bae1dSRodney W. Grimes {
675df8bae1dSRodney W. Grimes 	register vm_page_t p;
676df8bae1dSRodney W. Grimes 	if (object == NULL)
677df8bae1dSRodney W. Grimes 		return;
678b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
679b5b40fa6SJohn Dyson 		p != NULL;
680b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
681bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
682f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
68326f9a767SRodney W. Grimes 	}
68426f9a767SRodney W. Grimes }
685df8bae1dSRodney W. Grimes 
686df8bae1dSRodney W. Grimes /*
687867a482dSJohn Dyson  *	vm_object_madvise:
688867a482dSJohn Dyson  *
689867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
690867a482dSJohn Dyson  */
691867a482dSJohn Dyson void
692867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
693867a482dSJohn Dyson 	vm_object_t object;
694867a482dSJohn Dyson 	vm_pindex_t pindex;
695867a482dSJohn Dyson 	int count;
696867a482dSJohn Dyson 	int advise;
697867a482dSJohn Dyson {
698867a482dSJohn Dyson 	vm_pindex_t end;
699867a482dSJohn Dyson 	vm_page_t m;
700867a482dSJohn Dyson 
701867a482dSJohn Dyson 	if (object == NULL)
702867a482dSJohn Dyson 		return;
703867a482dSJohn Dyson 
704867a482dSJohn Dyson 	end = pindex + count;
705867a482dSJohn Dyson 
706867a482dSJohn Dyson 	for (; pindex < end; pindex += 1) {
707867a482dSJohn Dyson 		m = vm_page_lookup(object, pindex);
708867a482dSJohn Dyson 
709867a482dSJohn Dyson 		/*
710867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
711867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
712867a482dSJohn Dyson 		 * in any of the below states.
713867a482dSJohn Dyson 		 */
714867a482dSJohn Dyson 		if (m == NULL || m->busy || (m->flags & PG_BUSY) ||
715867a482dSJohn Dyson 			m->hold_count || m->wire_count ||
716867a482dSJohn Dyson 			m->valid != VM_PAGE_BITS_ALL)
717867a482dSJohn Dyson 			continue;
718867a482dSJohn Dyson 
719867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
720867a482dSJohn Dyson 			if (m->queue != PQ_ACTIVE)
721867a482dSJohn Dyson 				vm_page_activate(m);
7220a47b48bSJohn Dyson 		} else if ((advise == MADV_DONTNEED) ||
7230a47b48bSJohn Dyson 			((advise == MADV_FREE) &&
724b5b40fa6SJohn Dyson 				((object->type != OBJT_DEFAULT) &&
725b5b40fa6SJohn Dyson 					(object->type != OBJT_SWAP)))) {
726867a482dSJohn Dyson 			vm_page_deactivate(m);
7270a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
7280a47b48bSJohn Dyson 			/*
7290a47b48bSJohn Dyson 			 * Force a demand-zero on next ref
7300a47b48bSJohn Dyson 			 */
7310a47b48bSJohn Dyson 			if (object->type == OBJT_SWAP)
7320a47b48bSJohn Dyson 				swap_pager_dmzspace(object, m->pindex, 1);
7330a47b48bSJohn Dyson 			vm_page_protect(m, VM_PROT_NONE);
7340a47b48bSJohn Dyson 			vm_page_free(m);
735867a482dSJohn Dyson 		}
736867a482dSJohn Dyson 	}
737867a482dSJohn Dyson }
738867a482dSJohn Dyson 
739867a482dSJohn Dyson /*
740df8bae1dSRodney W. Grimes  *	vm_object_shadow:
741df8bae1dSRodney W. Grimes  *
742df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
743df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
744df8bae1dSRodney W. Grimes  *	object reference is deallocated.
745df8bae1dSRodney W. Grimes  *
746df8bae1dSRodney W. Grimes  *	The new object and offset into that object
747df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
748df8bae1dSRodney W. Grimes  */
749df8bae1dSRodney W. Grimes 
75026f9a767SRodney W. Grimes void
75126f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
752df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
753a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
754df8bae1dSRodney W. Grimes 	vm_size_t length;
755df8bae1dSRodney W. Grimes {
756df8bae1dSRodney W. Grimes 	register vm_object_t source;
757df8bae1dSRodney W. Grimes 	register vm_object_t result;
758df8bae1dSRodney W. Grimes 
759df8bae1dSRodney W. Grimes 	source = *object;
760df8bae1dSRodney W. Grimes 
761df8bae1dSRodney W. Grimes 	/*
762df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
763df8bae1dSRodney W. Grimes 	 */
764df8bae1dSRodney W. Grimes 
76524a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
766df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
767df8bae1dSRodney W. Grimes 
768df8bae1dSRodney W. Grimes 	/*
7690d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
7700d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
7710d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
7720d94caffSDavid Greenman 	 * of reference count.
773df8bae1dSRodney W. Grimes 	 */
77424a1cce3SDavid Greenman 	result->backing_object = source;
775de5f6a77SJohn Dyson 	if (source) {
776de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
777de5f6a77SJohn Dyson 		++source->shadow_count;
778de5f6a77SJohn Dyson 	}
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes 	/*
7810d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
7820d94caffSDavid Greenman 	 * the new object.
783df8bae1dSRodney W. Grimes 	 */
784df8bae1dSRodney W. Grimes 
78524a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
786df8bae1dSRodney W. Grimes 
787df8bae1dSRodney W. Grimes 	/*
788df8bae1dSRodney W. Grimes 	 * Return the new things
789df8bae1dSRodney W. Grimes 	 */
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 	*offset = 0;
792df8bae1dSRodney W. Grimes 	*object = result;
793df8bae1dSRodney W. Grimes }
794df8bae1dSRodney W. Grimes 
795df8bae1dSRodney W. Grimes 
796df8bae1dSRodney W. Grimes /*
7972fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
7982fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
7992fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
8002fe6e4d7SDavid Greenman  */
8012fe6e4d7SDavid Greenman static void
8022fe6e4d7SDavid Greenman vm_object_qcollapse(object)
8032fe6e4d7SDavid Greenman 	register vm_object_t object;
8042fe6e4d7SDavid Greenman {
8052fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
806a316d390SJohn Dyson 	register vm_pindex_t backing_offset_index, paging_offset_index;
807a316d390SJohn Dyson 	vm_pindex_t backing_object_paging_offset_index;
808a316d390SJohn Dyson 	vm_pindex_t new_pindex;
8092fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
8102fe6e4d7SDavid Greenman 	register vm_size_t size;
8112fe6e4d7SDavid Greenman 
81224a1cce3SDavid Greenman 	backing_object = object->backing_object;
8132fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
8142fe6e4d7SDavid Greenman 		return;
8152fe6e4d7SDavid Greenman 
816010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
817010cf3b9SDavid Greenman 
818a316d390SJohn Dyson 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
819a316d390SJohn Dyson 	backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset);
820a316d390SJohn Dyson 	paging_offset_index = OFF_TO_IDX(object->paging_offset);
8212fe6e4d7SDavid Greenman 	size = object->size;
822b18bfc3dSJohn Dyson 	p = TAILQ_FIRST(&backing_object->memq);
8232fe6e4d7SDavid Greenman 	while (p) {
8242fe6e4d7SDavid Greenman 		vm_page_t next;
8250d94caffSDavid Greenman 
826b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
827bd7e5f99SJohn Dyson 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
8285070c7f8SJohn Dyson 		    ((p->queue - p->pc) == PQ_CACHE) ||
8295070c7f8SJohn Dyson 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
8302fe6e4d7SDavid Greenman 			p = next;
8312fe6e4d7SDavid Greenman 			continue;
8322fe6e4d7SDavid Greenman 		}
833a316d390SJohn Dyson 		new_pindex = p->pindex - backing_offset_index;
834a316d390SJohn Dyson 		if (p->pindex < backing_offset_index ||
835a316d390SJohn Dyson 		    new_pindex >= size) {
83624a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP)
83724a1cce3SDavid Greenman 				swap_pager_freespace(backing_object,
838a316d390SJohn Dyson 				    backing_object_paging_offset_index+p->pindex,
839a316d390SJohn Dyson 				    1);
840b18bfc3dSJohn Dyson 			vm_page_protect(p, VM_PROT_NONE);
8412fe6e4d7SDavid Greenman 			vm_page_free(p);
8422fe6e4d7SDavid Greenman 		} else {
843a316d390SJohn Dyson 			pp = vm_page_lookup(object, new_pindex);
84424a1cce3SDavid Greenman 			if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
845a316d390SJohn Dyson 				    paging_offset_index + new_pindex, NULL, NULL))) {
84624a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
84724a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
848a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
849b18bfc3dSJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
8502fe6e4d7SDavid Greenman 				vm_page_free(p);
8512fe6e4d7SDavid Greenman 			} else {
85224a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
85324a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
854a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
855a316d390SJohn Dyson 				vm_page_rename(p, object, new_pindex);
8569b4814bbSDavid Greenman 				p->dirty = VM_PAGE_BITS_ALL;
8572fe6e4d7SDavid Greenman 			}
8582fe6e4d7SDavid Greenman 		}
8592fe6e4d7SDavid Greenman 		p = next;
8602fe6e4d7SDavid Greenman 	}
861010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
8622fe6e4d7SDavid Greenman }
8632fe6e4d7SDavid Greenman 
864df8bae1dSRodney W. Grimes /*
865df8bae1dSRodney W. Grimes  *	vm_object_collapse:
866df8bae1dSRodney W. Grimes  *
867df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
868df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
869df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
870df8bae1dSRodney W. Grimes  */
87126f9a767SRodney W. Grimes void
87226f9a767SRodney W. Grimes vm_object_collapse(object)
87324a1cce3SDavid Greenman 	vm_object_t object;
874df8bae1dSRodney W. Grimes 
875df8bae1dSRodney W. Grimes {
87624a1cce3SDavid Greenman 	vm_object_t backing_object;
877a316d390SJohn Dyson 	vm_ooffset_t backing_offset;
87824a1cce3SDavid Greenman 	vm_size_t size;
879a316d390SJohn Dyson 	vm_pindex_t new_pindex, backing_offset_index;
88024a1cce3SDavid Greenman 	vm_page_t p, pp;
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes 	while (TRUE) {
883df8bae1dSRodney W. Grimes 		/*
884df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
885df8bae1dSRodney W. Grimes 		 *
8860d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
8870d94caffSDavid Greenman 		 * out.
888df8bae1dSRodney W. Grimes 		 */
8892fe6e4d7SDavid Greenman 		if (object == NULL)
890df8bae1dSRodney W. Grimes 			return;
891df8bae1dSRodney W. Grimes 
892b9921222SDavid Greenman 		/*
893b9921222SDavid Greenman 		 * Make sure there is a backing object.
894b9921222SDavid Greenman 		 */
89524a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
896df8bae1dSRodney W. Grimes 			return;
897df8bae1dSRodney W. Grimes 
898f919ebdeSDavid Greenman 		/*
899f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
90024a1cce3SDavid Greenman 		 * not collapsable.
901f919ebdeSDavid Greenman 		 */
90224a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
90324a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
90424a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
905f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
90624a1cce3SDavid Greenman 		    object->handle != NULL ||
90724a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
90824a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
90924a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
9109b4814bbSDavid Greenman 			return;
91124a1cce3SDavid Greenman 		}
9129b4814bbSDavid Greenman 
913f919ebdeSDavid Greenman 		if (object->paging_in_progress != 0 ||
914f919ebdeSDavid Greenman 		    backing_object->paging_in_progress != 0) {
915b9921222SDavid Greenman 			vm_object_qcollapse(object);
916df8bae1dSRodney W. Grimes 			return;
917df8bae1dSRodney W. Grimes 		}
918f919ebdeSDavid Greenman 
91926f9a767SRodney W. Grimes 		/*
9200d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
9210d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
9220d94caffSDavid Greenman 		 * the parent's reference to it.
923df8bae1dSRodney W. Grimes 		 */
924df8bae1dSRodney W. Grimes 
92524a1cce3SDavid Greenman 		backing_offset = object->backing_object_offset;
926a316d390SJohn Dyson 		backing_offset_index = OFF_TO_IDX(backing_offset);
927df8bae1dSRodney W. Grimes 		size = object->size;
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes 		/*
9300d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
9310d94caffSDavid Greenman 		 * can collapse it into the parent.
932df8bae1dSRodney W. Grimes 		 */
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
935df8bae1dSRodney W. Grimes 
936a1f6d91cSDavid Greenman 			backing_object->flags |= OBJ_DEAD;
937df8bae1dSRodney W. Grimes 			/*
938df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
939df8bae1dSRodney W. Grimes 			 *
9400d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
9410d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
9420d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
9430d94caffSDavid Greenman 			 * shadow them.
944df8bae1dSRodney W. Grimes 			 */
945df8bae1dSRodney W. Grimes 
946b18bfc3dSJohn Dyson 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
94726f9a767SRodney W. Grimes 
948a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 				/*
9510d94caffSDavid Greenman 				 * If the parent has a page here, or if this
9520d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
9530d94caffSDavid Greenman 				 * it.
954df8bae1dSRodney W. Grimes 				 *
955df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
956df8bae1dSRodney W. Grimes 				 */
957df8bae1dSRodney W. Grimes 
958a316d390SJohn Dyson 				if (p->pindex < backing_offset_index ||
959a316d390SJohn Dyson 				    new_pindex >= size) {
960f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
9610d94caffSDavid Greenman 					PAGE_WAKEUP(p);
962df8bae1dSRodney W. Grimes 					vm_page_free(p);
963df8bae1dSRodney W. Grimes 				} else {
964a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
96524a1cce3SDavid Greenman 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
966a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) {
967f919ebdeSDavid Greenman 						vm_page_protect(p, VM_PROT_NONE);
9680d94caffSDavid Greenman 						PAGE_WAKEUP(p);
969df8bae1dSRodney W. Grimes 						vm_page_free(p);
97026f9a767SRodney W. Grimes 					} else {
971a316d390SJohn Dyson 						vm_page_rename(p, object, new_pindex);
972df8bae1dSRodney W. Grimes 					}
973df8bae1dSRodney W. Grimes 				}
974df8bae1dSRodney W. Grimes 			}
975df8bae1dSRodney W. Grimes 
976df8bae1dSRodney W. Grimes 			/*
977df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
978df8bae1dSRodney W. Grimes 			 */
979df8bae1dSRodney W. Grimes 
98024a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
98126f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
98224a1cce3SDavid Greenman 				if (object->type == OBJT_SWAP) {
98326f9a767SRodney W. Grimes 					object->paging_in_progress++;
98426f9a767SRodney W. Grimes 					/*
98526f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
9860d94caffSDavid Greenman 					 * and destroy unneeded pages in
9870d94caffSDavid Greenman 					 * shadow object.
98826f9a767SRodney W. Grimes 					 */
98926f9a767SRodney W. Grimes 					swap_pager_copy(
990a316d390SJohn Dyson 					    backing_object,
991a316d390SJohn Dyson 					    OFF_TO_IDX(backing_object->paging_offset),
992a316d390SJohn Dyson 					    object,
993a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset),
994a316d390SJohn Dyson 					    OFF_TO_IDX(object->backing_object_offset));
995f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
99626f9a767SRodney W. Grimes 				} else {
99726f9a767SRodney W. Grimes 					object->paging_in_progress++;
99826f9a767SRodney W. Grimes 					/*
99924a1cce3SDavid Greenman 					 * move the shadow backing_object's pager data to
100024a1cce3SDavid Greenman 					 * "object" and convert "object" type to OBJT_SWAP.
100126f9a767SRodney W. Grimes 					 */
100224a1cce3SDavid Greenman 					object->type = OBJT_SWAP;
10032a4895f4SDavid Greenman 					object->un_pager.swp.swp_nblocks =
10042a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_nblocks;
10052a4895f4SDavid Greenman 					object->un_pager.swp.swp_allocsize =
10062a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_allocsize;
10072a4895f4SDavid Greenman 					object->un_pager.swp.swp_blocks =
10082a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_blocks;
10092a4895f4SDavid Greenman 					object->un_pager.swp.swp_poip =		/* XXX */
10102a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_poip;
101126f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
101224a1cce3SDavid Greenman 					TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list);
101324a1cce3SDavid Greenman 
101424a1cce3SDavid Greenman 					/*
101524a1cce3SDavid Greenman 					 * Convert backing object from OBJT_SWAP to
101624a1cce3SDavid Greenman 					 * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is
101724a1cce3SDavid Greenman 					 * actually necessary.
101824a1cce3SDavid Greenman 					 */
101924a1cce3SDavid Greenman 					backing_object->type = OBJT_DEFAULT;
102024a1cce3SDavid Greenman 					TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list);
102126f9a767SRodney W. Grimes 					/*
102226f9a767SRodney W. Grimes 					 * free unnecessary blocks
102326f9a767SRodney W. Grimes 					 */
1024a316d390SJohn Dyson 					swap_pager_freespace(object, 0,
1025a316d390SJohn Dyson 						OFF_TO_IDX(object->paging_offset));
1026f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
1027c0503609SDavid Greenman 				}
1028c0503609SDavid Greenman 
1029f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1030c0503609SDavid Greenman 			}
1031df8bae1dSRodney W. Grimes 			/*
1032df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
103324a1cce3SDavid Greenman 			 * Note that the reference to backing_object->backing_object
1034df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1035df8bae1dSRodney W. Grimes 			 */
1036df8bae1dSRodney W. Grimes 
103724a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
103824a1cce3SDavid Greenman 			    shadow_list);
1039de5f6a77SJohn Dyson 			--object->backing_object->shadow_count;
1040de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
104124a1cce3SDavid Greenman 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
104224a1cce3SDavid Greenman 				    backing_object, shadow_list);
1043de5f6a77SJohn Dyson 				--backing_object->backing_object->shadow_count;
1044de5f6a77SJohn Dyson 			}
104524a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1046de5f6a77SJohn Dyson 			if (object->backing_object) {
104724a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
104824a1cce3SDavid Greenman 				    object, shadow_list);
1049de5f6a77SJohn Dyson 				++object->backing_object->shadow_count;
1050de5f6a77SJohn Dyson 			}
10512fe6e4d7SDavid Greenman 
105224a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1053df8bae1dSRodney W. Grimes 			/*
1054df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1055df8bae1dSRodney W. Grimes 			 *
10560d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
10570d94caffSDavid Greenman 			 * and no object references within it, all that is
10580d94caffSDavid Greenman 			 * necessary is to dispose of it.
1059df8bae1dSRodney W. Grimes 			 */
1060df8bae1dSRodney W. Grimes 
1061df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1062df8bae1dSRodney W. Grimes 			    object_list);
1063df8bae1dSRodney W. Grimes 			vm_object_count--;
1064df8bae1dSRodney W. Grimes 
1065df8bae1dSRodney W. Grimes 			free((caddr_t) backing_object, M_VMOBJ);
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes 			object_collapses++;
10680d94caffSDavid Greenman 		} else {
1069df8bae1dSRodney W. Grimes 			/*
1070df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
10710d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
10720d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
10730d94caffSDavid Greenman 			 * shadow the next one in the chain.
1074df8bae1dSRodney W. Grimes 			 *
10750d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
10760d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1077df8bae1dSRodney W. Grimes 			 */
1078df8bae1dSRodney W. Grimes 
107924a1cce3SDavid Greenman 			if (backing_object->type != OBJT_DEFAULT) {
1080df8bae1dSRodney W. Grimes 				return;
1081df8bae1dSRodney W. Grimes 			}
1082df8bae1dSRodney W. Grimes 			/*
10830d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
10840d94caffSDavid Greenman 			 * here.
1085df8bae1dSRodney W. Grimes 			 */
1086df8bae1dSRodney W. Grimes 
1087b18bfc3dSJohn Dyson 			for (p = TAILQ_FIRST(&backing_object->memq); p; p = TAILQ_NEXT(p, listq)) {
1088a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes 				/*
10910d94caffSDavid Greenman 				 * If the parent has a page here, or if this
10920d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1093df8bae1dSRodney W. Grimes 				 *
10940d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
10950d94caffSDavid Greenman 				 * the chain.
1096df8bae1dSRodney W. Grimes 				 */
1097df8bae1dSRodney W. Grimes 
1098a316d390SJohn Dyson 				if (p->pindex >= backing_offset_index &&
1099a316d390SJohn Dyson 					new_pindex <= size) {
110024a1cce3SDavid Greenman 
1101a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
110224a1cce3SDavid Greenman 
110324a1cce3SDavid Greenman 					if ((pp == NULL || pp->valid == 0) &&
1104a316d390SJohn Dyson 				   	    !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) {
1105df8bae1dSRodney W. Grimes 						/*
11060d94caffSDavid Greenman 						 * Page still needed. Can't go any
11070d94caffSDavid Greenman 						 * further.
1108df8bae1dSRodney W. Grimes 						 */
1109df8bae1dSRodney W. Grimes 						return;
1110df8bae1dSRodney W. Grimes 					}
1111df8bae1dSRodney W. Grimes 				}
111224a1cce3SDavid Greenman 			}
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes 			/*
11150d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
11160d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
11170d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1118df8bae1dSRodney W. Grimes 			 */
1119df8bae1dSRodney W. Grimes 
112024a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head,
112124a1cce3SDavid Greenman 			    object, shadow_list);
1122de5f6a77SJohn Dyson 			--object->backing_object->shadow_count;
112324a1cce3SDavid Greenman 			vm_object_reference(object->backing_object = backing_object->backing_object);
1124de5f6a77SJohn Dyson 			if (object->backing_object) {
112524a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
112624a1cce3SDavid Greenman 				    object, shadow_list);
1127de5f6a77SJohn Dyson 				++object->backing_object->shadow_count;
1128de5f6a77SJohn Dyson 			}
112924a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1130df8bae1dSRodney W. Grimes 
1131df8bae1dSRodney W. Grimes 			/*
11320d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
11330d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
11340d94caffSDavid Greenman 			 * so we don't need to call vm_object_deallocate.
1135df8bae1dSRodney W. Grimes 			 */
113626f9a767SRodney W. Grimes 			if (backing_object->ref_count == 1)
113726f9a767SRodney W. Grimes 				printf("should have called obj deallocate\n");
1138df8bae1dSRodney W. Grimes 			backing_object->ref_count--;
1139df8bae1dSRodney W. Grimes 
1140df8bae1dSRodney W. Grimes 			object_bypasses++;
1141df8bae1dSRodney W. Grimes 
1142df8bae1dSRodney W. Grimes 		}
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 		/*
1145df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1146df8bae1dSRodney W. Grimes 		 */
1147df8bae1dSRodney W. Grimes 	}
1148df8bae1dSRodney W. Grimes }
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes /*
1151df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1152df8bae1dSRodney W. Grimes  *
1153df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1154df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1155df8bae1dSRodney W. Grimes  *
1156df8bae1dSRodney W. Grimes  *	The object must be locked.
1157df8bae1dSRodney W. Grimes  */
115826f9a767SRodney W. Grimes void
11597c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1160df8bae1dSRodney W. Grimes 	register vm_object_t object;
1161a316d390SJohn Dyson 	register vm_pindex_t start;
1162a316d390SJohn Dyson 	register vm_pindex_t end;
11637c1f6cedSDavid Greenman 	boolean_t clean_only;
1164df8bae1dSRodney W. Grimes {
1165df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
1166a316d390SJohn Dyson 	unsigned int size;
116726f9a767SRodney W. Grimes 	int s;
1168df8bae1dSRodney W. Grimes 
1169df8bae1dSRodney W. Grimes 	if (object == NULL)
1170df8bae1dSRodney W. Grimes 		return;
1171df8bae1dSRodney W. Grimes 
11722fe6e4d7SDavid Greenman 	object->paging_in_progress++;
117326f9a767SRodney W. Grimes again:
117426f9a767SRodney W. Grimes 	size = end - start;
1175a316d390SJohn Dyson 	if (size > 4 || size >= object->size / 4) {
1176b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1177b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
11780891ef4cSJohn Dyson 			if ((start <= p->pindex) && (p->pindex < end)) {
1179bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1180bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1181bd7e5f99SJohn Dyson 					p->valid = 0;
11820d94caffSDavid Greenman 					continue;
11830d94caffSDavid Greenman 				}
11840891ef4cSJohn Dyson 
1185b18bfc3dSJohn Dyson 				/*
1186b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1187b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1188b18bfc3dSJohn Dyson 				 */
1189b18bfc3dSJohn Dyson 				if ((p->flags & PG_BUSY) || p->busy) {
1190b18bfc3dSJohn Dyson 					s = splvm();
11910d94caffSDavid Greenman 					if ((p->flags & PG_BUSY) || p->busy) {
119226f9a767SRodney W. Grimes 						p->flags |= PG_WANTED;
119324a1cce3SDavid Greenman 						tsleep(p, PVM, "vmopar", 0);
1194a481f200SDavid Greenman 						splx(s);
119526f9a767SRodney W. Grimes 						goto again;
119626f9a767SRodney W. Grimes 					}
1197a481f200SDavid Greenman 					splx(s);
1198b18bfc3dSJohn Dyson 				}
11990891ef4cSJohn Dyson 
12007c1f6cedSDavid Greenman 				if (clean_only) {
12017c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
12027c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
12037c1f6cedSDavid Greenman 						continue;
12047c1f6cedSDavid Greenman 				}
1205f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
12060d94caffSDavid Greenman 				PAGE_WAKEUP(p);
1207df8bae1dSRodney W. Grimes 				vm_page_free(p);
120826f9a767SRodney W. Grimes 			}
120926f9a767SRodney W. Grimes 		}
121026f9a767SRodney W. Grimes 	} else {
121126f9a767SRodney W. Grimes 		while (size > 0) {
1212bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1213bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1214bd7e5f99SJohn Dyson 					p->valid = 0;
1215bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1216bd7e5f99SJohn Dyson 					start += 1;
1217bd7e5f99SJohn Dyson 					size -= 1;
1218bd7e5f99SJohn Dyson 					continue;
12190d94caffSDavid Greenman 				}
1220b18bfc3dSJohn Dyson 				/*
1221b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1222b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1223b18bfc3dSJohn Dyson 				 */
1224b18bfc3dSJohn Dyson 				if ((p->flags & PG_BUSY) || p->busy) {
1225b18bfc3dSJohn Dyson 					s = splvm();
12260d94caffSDavid Greenman 					if ((p->flags & PG_BUSY) || p->busy) {
122726f9a767SRodney W. Grimes 						p->flags |= PG_WANTED;
122824a1cce3SDavid Greenman 						tsleep(p, PVM, "vmopar", 0);
1229a481f200SDavid Greenman 						splx(s);
123026f9a767SRodney W. Grimes 						goto again;
123126f9a767SRodney W. Grimes 					}
1232a481f200SDavid Greenman 					splx(s);
1233b18bfc3dSJohn Dyson 				}
12347c1f6cedSDavid Greenman 				if (clean_only) {
12357c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1236bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1237bd7e5f99SJohn Dyson 						start += 1;
1238bd7e5f99SJohn Dyson 						size -= 1;
12397c1f6cedSDavid Greenman 						continue;
12407c1f6cedSDavid Greenman 					}
1241bd7e5f99SJohn Dyson 				}
1242f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
12430d94caffSDavid Greenman 				PAGE_WAKEUP(p);
124426f9a767SRodney W. Grimes 				vm_page_free(p);
124526f9a767SRodney W. Grimes 			}
1246a316d390SJohn Dyson 			start += 1;
1247a316d390SJohn Dyson 			size -= 1;
1248df8bae1dSRodney W. Grimes 		}
1249df8bae1dSRodney W. Grimes 	}
1250f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1251c0503609SDavid Greenman }
1252df8bae1dSRodney W. Grimes 
1253df8bae1dSRodney W. Grimes /*
1254df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1255df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1256df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1257df8bae1dSRodney W. Grimes  *
1258df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1259df8bae1dSRodney W. Grimes  *
1260df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1261df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1262df8bae1dSRodney W. Grimes  *
1263df8bae1dSRodney W. Grimes  *	Parameters:
1264df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1265df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1266df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1267df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1268df8bae1dSRodney W. Grimes  *
1269df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1270df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1271df8bae1dSRodney W. Grimes  *
1272df8bae1dSRodney W. Grimes  *	Conditions:
1273df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1274df8bae1dSRodney W. Grimes  */
12750d94caffSDavid Greenman boolean_t
1276a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1277df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1278a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1279df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1280df8bae1dSRodney W. Grimes {
1281df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1282df8bae1dSRodney W. Grimes 
1283df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1284df8bae1dSRodney W. Grimes 		return (TRUE);
1285df8bae1dSRodney W. Grimes 	}
1286df8bae1dSRodney W. Grimes 
128730dcfc09SJohn Dyson 	if (prev_object->type != OBJT_DEFAULT) {
128830dcfc09SJohn Dyson 		return (FALSE);
128930dcfc09SJohn Dyson 	}
129030dcfc09SJohn Dyson 
1291df8bae1dSRodney W. Grimes 	/*
1292df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1293df8bae1dSRodney W. Grimes 	 */
1294df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1295df8bae1dSRodney W. Grimes 
1296df8bae1dSRodney W. Grimes 	/*
12970d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
12980d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
12990d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1300df8bae1dSRodney W. Grimes 	 */
1301df8bae1dSRodney W. Grimes 
13028cc7e047SJohn Dyson 	if (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;
13088cc7e047SJohn Dyson 
13098cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
13108cc7e047SJohn Dyson 	    (prev_object->size != prev_pindex + prev_size)) {
13118cc7e047SJohn Dyson 		return (FALSE);
13128cc7e047SJohn Dyson 	}
13138cc7e047SJohn Dyson 
1314df8bae1dSRodney W. Grimes 	/*
13150d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
13160d94caffSDavid Greenman 	 * deallocation.
1317df8bae1dSRodney W. Grimes 	 */
1318df8bae1dSRodney W. Grimes 
1319df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1320a316d390SJohn Dyson 	    prev_pindex + prev_size,
1321a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1322df8bae1dSRodney W. Grimes 
1323df8bae1dSRodney W. Grimes 	/*
1324df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1325df8bae1dSRodney W. Grimes 	 */
1326a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1327df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1328df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1329df8bae1dSRodney W. Grimes 
1330df8bae1dSRodney W. Grimes 	return (TRUE);
1331df8bae1dSRodney W. Grimes }
1332df8bae1dSRodney W. Grimes 
1333c7c34a24SBruce Evans #include "opt_ddb.h"
1334c3cb3e12SDavid Greenman #ifdef DDB
1335c7c34a24SBruce Evans #include <sys/kernel.h>
1336c7c34a24SBruce Evans 
1337c7c34a24SBruce Evans #include <machine/cons.h>
1338c7c34a24SBruce Evans 
1339c7c34a24SBruce Evans #include <ddb/ddb.h>
1340c7c34a24SBruce Evans 
1341c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1342c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1343c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1344c3cb3e12SDavid Greenman 
1345cac597e4SBruce Evans static int
1346a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1347a1f6d91cSDavid Greenman 	vm_map_t map;
1348a1f6d91cSDavid Greenman 	vm_object_t object;
1349a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1350a1f6d91cSDavid Greenman {
1351a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1352a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1353a1f6d91cSDavid Greenman 	vm_object_t obj;
1354a1f6d91cSDavid Greenman 	int entcount;
1355a1f6d91cSDavid Greenman 
1356a1f6d91cSDavid Greenman 	if (map == 0)
1357a1f6d91cSDavid Greenman 		return 0;
1358a1f6d91cSDavid Greenman 
1359a1f6d91cSDavid Greenman 	if (entry == 0) {
1360a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1361a1f6d91cSDavid Greenman 		entcount = map->nentries;
1362a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1363a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1364a1f6d91cSDavid Greenman 				return 1;
1365a1f6d91cSDavid Greenman 			}
1366a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1367a1f6d91cSDavid Greenman 		}
1368a1f6d91cSDavid Greenman 	} else if (entry->is_sub_map || entry->is_a_map) {
1369a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1370a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1371a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1372a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1373a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1374a1f6d91cSDavid Greenman 				return 1;
1375a1f6d91cSDavid Greenman 			}
1376a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1377a1f6d91cSDavid Greenman 		}
1378a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
137924a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1380a1f6d91cSDavid Greenman 			if( obj == object) {
1381a1f6d91cSDavid Greenman 				return 1;
1382a1f6d91cSDavid Greenman 			}
1383a1f6d91cSDavid Greenman 	}
1384a1f6d91cSDavid Greenman 	return 0;
1385a1f6d91cSDavid Greenman }
1386a1f6d91cSDavid Greenman 
1387cac597e4SBruce Evans static int
1388a1f6d91cSDavid Greenman vm_object_in_map( object)
1389a1f6d91cSDavid Greenman 	vm_object_t object;
1390a1f6d91cSDavid Greenman {
1391a1f6d91cSDavid Greenman 	struct proc *p;
13921b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1393a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1394a1f6d91cSDavid Greenman 			continue;
1395a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1396a1f6d91cSDavid Greenman 			return 1;
1397a1f6d91cSDavid Greenman 	}
1398a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1399a1f6d91cSDavid Greenman 		return 1;
1400a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1401a1f6d91cSDavid Greenman 		return 1;
1402a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1403a1f6d91cSDavid Greenman 		return 1;
1404a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1405a1f6d91cSDavid Greenman 		return 1;
1406a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1407a1f6d91cSDavid Greenman 		return 1;
1408a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1409a1f6d91cSDavid Greenman 		return 1;
1410a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1411a1f6d91cSDavid Greenman 		return 1;
1412a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1413a1f6d91cSDavid Greenman 		return 1;
1414a1f6d91cSDavid Greenman 	return 0;
1415a1f6d91cSDavid Greenman }
1416a1f6d91cSDavid Greenman 
1417c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1418f708ef1bSPoul-Henning Kamp {
1419a1f6d91cSDavid Greenman 	vm_object_t object;
1420a1f6d91cSDavid Greenman 
1421a1f6d91cSDavid Greenman 	/*
1422a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1423a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1424a1f6d91cSDavid Greenman 	 */
1425b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1426a1f6d91cSDavid Greenman 			object != NULL;
1427b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
142824a1cce3SDavid Greenman 		if (object->handle == NULL &&
142924a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1430a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1431c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1432a1f6d91cSDavid Greenman 					object->size);
1433a1f6d91cSDavid Greenman 			}
1434a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1435c7c34a24SBruce Evans 				db_printf("vmochk: internal obj is not in a map: "
14363af76890SPoul-Henning Kamp 		"ref: %d, size: %d: 0x%x, backing_object: 0x%x\n",
14373af76890SPoul-Henning Kamp 				    object->ref_count, object->size,
14383af76890SPoul-Henning Kamp 				    object->size, object->backing_object);
1439a1f6d91cSDavid Greenman 			}
1440a1f6d91cSDavid Greenman 		}
1441a1f6d91cSDavid Greenman 	}
1442a1f6d91cSDavid Greenman }
1443a1f6d91cSDavid Greenman 
144426f9a767SRodney W. Grimes /*
1445df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1446df8bae1dSRodney W. Grimes  */
1447c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1448df8bae1dSRodney W. Grimes {
1449c7c34a24SBruce Evans 	/* XXX convert args. */
1450c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1451c7c34a24SBruce Evans 	boolean_t full = have_addr;
1452c7c34a24SBruce Evans 
1453df8bae1dSRodney W. Grimes 	register vm_page_t p;
1454df8bae1dSRodney W. Grimes 
1455c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1456c7c34a24SBruce Evans #define	count	was_count
1457c7c34a24SBruce Evans 
1458df8bae1dSRodney W. Grimes 	register int count;
1459df8bae1dSRodney W. Grimes 
1460df8bae1dSRodney W. Grimes 	if (object == NULL)
1461df8bae1dSRodney W. Grimes 		return;
1462df8bae1dSRodney W. Grimes 
1463c7c34a24SBruce Evans 	db_iprintf("Object 0x%x: size=0x%x, res=%d, ref=%d, ",
1464df8bae1dSRodney W. Grimes 	    (int) object, (int) object->size,
1465df8bae1dSRodney W. Grimes 	    object->resident_page_count, object->ref_count);
1466c7c34a24SBruce Evans 	db_printf("offset=0x%x, backing_object=(0x%x)+0x%x\n",
14672a4895f4SDavid Greenman 	    (int) object->paging_offset,
146824a1cce3SDavid Greenman 	    (int) object->backing_object, (int) object->backing_object_offset);
1469c7c34a24SBruce Evans 	db_printf("cache: next=%p, prev=%p\n",
1470b18bfc3dSJohn Dyson 	    TAILQ_NEXT(object, cached_list), TAILQ_PREV(object, cached_list));
1471df8bae1dSRodney W. Grimes 
1472df8bae1dSRodney W. Grimes 	if (!full)
1473df8bae1dSRodney W. Grimes 		return;
1474df8bae1dSRodney W. Grimes 
1475c7c34a24SBruce Evans 	db_indent += 2;
1476df8bae1dSRodney W. Grimes 	count = 0;
1477b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1478df8bae1dSRodney W. Grimes 		if (count == 0)
1479c7c34a24SBruce Evans 			db_iprintf("memory:=");
1480df8bae1dSRodney W. Grimes 		else if (count == 6) {
1481c7c34a24SBruce Evans 			db_printf("\n");
1482c7c34a24SBruce Evans 			db_iprintf(" ...");
1483df8bae1dSRodney W. Grimes 			count = 0;
1484df8bae1dSRodney W. Grimes 		} else
1485c7c34a24SBruce Evans 			db_printf(",");
1486df8bae1dSRodney W. Grimes 		count++;
1487df8bae1dSRodney W. Grimes 
1488c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1489a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1490df8bae1dSRodney W. Grimes 	}
1491df8bae1dSRodney W. Grimes 	if (count != 0)
1492c7c34a24SBruce Evans 		db_printf("\n");
1493c7c34a24SBruce Evans 	db_indent -= 2;
1494df8bae1dSRodney W. Grimes }
14955070c7f8SJohn Dyson 
1496c7c34a24SBruce Evans /* XXX. */
1497c7c34a24SBruce Evans #undef count
1498c7c34a24SBruce Evans 
1499c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
15005070c7f8SJohn Dyson void
1501c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1502c7c34a24SBruce Evans 	db_expr_t addr;
1503c7c34a24SBruce Evans 	boolean_t have_addr;
1504c7c34a24SBruce Evans 	db_expr_t count;
1505c7c34a24SBruce Evans 	char *modif;
1506c7c34a24SBruce Evans {
1507c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1508c7c34a24SBruce Evans }
1509c7c34a24SBruce Evans 
1510c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
15115070c7f8SJohn Dyson {
15125070c7f8SJohn Dyson 	vm_object_t object;
15135070c7f8SJohn Dyson 	int nl = 0;
15145070c7f8SJohn Dyson 	int c;
15155070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
15165070c7f8SJohn Dyson 			object != NULL;
15175070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
15185070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
15195070c7f8SJohn Dyson 		vm_pindex_t osize;
15205070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
15215070c7f8SJohn Dyson 		int rcount;
15225070c7f8SJohn Dyson 		vm_page_t m;
15235070c7f8SJohn Dyson 
15245070c7f8SJohn Dyson 		db_printf("new object: 0x%x\n", object);
15255070c7f8SJohn Dyson 		if ( nl > 18) {
15265070c7f8SJohn Dyson 			c = cngetc();
15275070c7f8SJohn Dyson 			if (c != ' ')
15285070c7f8SJohn Dyson 				return;
15295070c7f8SJohn Dyson 			nl = 0;
15305070c7f8SJohn Dyson 		}
15315070c7f8SJohn Dyson 		nl++;
15325070c7f8SJohn Dyson 		rcount = 0;
15335070c7f8SJohn Dyson 		fidx = 0;
15345070c7f8SJohn Dyson 		osize = object->size;
15355070c7f8SJohn Dyson 		if (osize > 128)
15365070c7f8SJohn Dyson 			osize = 128;
15375070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
15385070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
15395070c7f8SJohn Dyson 			if (m == NULL) {
15405070c7f8SJohn Dyson 				if (rcount) {
15415070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
15425070c7f8SJohn Dyson 						fidx, rcount, pa);
15435070c7f8SJohn Dyson 					if ( nl > 18) {
15445070c7f8SJohn Dyson 						c = cngetc();
15455070c7f8SJohn Dyson 						if (c != ' ')
15465070c7f8SJohn Dyson 							return;
15475070c7f8SJohn Dyson 						nl = 0;
15485070c7f8SJohn Dyson 					}
15495070c7f8SJohn Dyson 					nl++;
15505070c7f8SJohn Dyson 					rcount = 0;
15515070c7f8SJohn Dyson 				}
15525070c7f8SJohn Dyson 				continue;
15535070c7f8SJohn Dyson 			}
15545070c7f8SJohn Dyson 
15555070c7f8SJohn Dyson 
15565070c7f8SJohn Dyson 			if (rcount &&
15575070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
15585070c7f8SJohn Dyson 				++rcount;
15595070c7f8SJohn Dyson 				continue;
15605070c7f8SJohn Dyson 			}
15615070c7f8SJohn Dyson 			if (rcount) {
15625070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
15635070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
15645070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
15655070c7f8SJohn Dyson 				if (padiff == 0) {
15665070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
15675070c7f8SJohn Dyson 					++rcount;
15685070c7f8SJohn Dyson 					continue;
15695070c7f8SJohn Dyson 				}
15705070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
15715070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
15725070c7f8SJohn Dyson 				if ( nl > 18) {
15735070c7f8SJohn Dyson 					c = cngetc();
15745070c7f8SJohn Dyson 					if (c != ' ')
15755070c7f8SJohn Dyson 						return;
15765070c7f8SJohn Dyson 					nl = 0;
15775070c7f8SJohn Dyson 				}
15785070c7f8SJohn Dyson 				nl++;
15795070c7f8SJohn Dyson 			}
15805070c7f8SJohn Dyson 			fidx = idx;
15815070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
15825070c7f8SJohn Dyson 			rcount = 1;
15835070c7f8SJohn Dyson 		}
15845070c7f8SJohn Dyson 		if (rcount) {
15855070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
15865070c7f8SJohn Dyson 			if ( nl > 18) {
15875070c7f8SJohn Dyson 				c = cngetc();
15885070c7f8SJohn Dyson 				if (c != ' ')
15895070c7f8SJohn Dyson 					return;
15905070c7f8SJohn Dyson 				nl = 0;
15915070c7f8SJohn Dyson 			}
15925070c7f8SJohn Dyson 			nl++;
15935070c7f8SJohn Dyson 		}
15945070c7f8SJohn Dyson 	}
15955070c7f8SJohn Dyson }
1596c3cb3e12SDavid Greenman #endif /* DDB */
1597