xref: /freebsd/sys/vm/vm_object.c (revision 2d8acc0f4aee0ac81e6a065fad89c82c6237faf5)
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  *
642d8acc0fSJohn Dyson  * $Id: vm_object.c,v 1.107 1998/01/17 09:16:55 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/proc.h>		/* for curproc, pageproc */
740d94caffSDavid Greenman #include <sys/vnode.h>
75efeaf95aSDavid Greenman #include <sys/vmmeter.h>
76867a482dSJohn Dyson #include <sys/mman.h>
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes #include <vm/vm.h>
79efeaf95aSDavid Greenman #include <vm/vm_param.h>
80efeaf95aSDavid Greenman #include <vm/vm_prot.h>
81996c772fSJohn Dyson #include <sys/lock.h>
82efeaf95aSDavid Greenman #include <vm/pmap.h>
83efeaf95aSDavid Greenman #include <vm/vm_map.h>
84efeaf95aSDavid Greenman #include <vm/vm_object.h>
85df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8626f9a767SRodney W. Grimes #include <vm/vm_pageout.h>
870d94caffSDavid Greenman #include <vm/vm_pager.h>
8805f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
89a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
90efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9199448ed1SJohn Dyson #include <vm/vm_zone.h>
9226f9a767SRodney W. Grimes 
93cac597e4SBruce Evans static void	vm_object_qcollapse __P((vm_object_t object));
94f708ef1bSPoul-Henning Kamp #ifdef not_used
95f708ef1bSPoul-Henning Kamp static void	vm_object_deactivate_pages __P((vm_object_t));
96f708ef1bSPoul-Henning Kamp #endif
97f6b04d2bSDavid Greenman 
98df8bae1dSRodney W. Grimes /*
99df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
100df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
101df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
102df8bae1dSRodney W. Grimes  *
103df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
104df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
105df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
106df8bae1dSRodney W. Grimes  *
107df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
108df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
109df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
110df8bae1dSRodney W. Grimes  *	lock.
111df8bae1dSRodney W. Grimes  *
112df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
113df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
114df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
115df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
116df8bae1dSRodney W. Grimes  *
117df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
118df8bae1dSRodney W. Grimes  *	modified after time of creation are:
119df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
120df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
121df8bae1dSRodney W. Grimes  *
122df8bae1dSRodney W. Grimes  */
123df8bae1dSRodney W. Grimes 
12428f8db14SBruce Evans struct object_q vm_object_list;
125996c772fSJohn Dyson struct simplelock vm_object_list_lock;
1264de628deSBruce Evans static long vm_object_count;		/* count of all objects */
12728f8db14SBruce Evans vm_object_t kernel_object;
12828f8db14SBruce Evans vm_object_t kmem_object;
129f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
130f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
131aef922f5SJohn Dyson extern int vm_pageout_page_count;
132df8bae1dSRodney W. Grimes 
133f708ef1bSPoul-Henning Kamp static long object_collapses;
134f708ef1bSPoul-Henning Kamp static long object_bypasses;
1355070c7f8SJohn Dyson static int next_index;
13699448ed1SJohn Dyson static vm_zone_t obj_zone;
13799448ed1SJohn Dyson static struct vm_zone obj_zone_store;
13899448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
13999448ed1SJohn Dyson struct vm_object vm_objects_init[VM_OBJECTS_INIT];
140df8bae1dSRodney W. Grimes 
1413075778bSJohn Dyson void
14224a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14324a1cce3SDavid Greenman 	objtype_t type;
144df8bae1dSRodney W. Grimes 	vm_size_t size;
145df8bae1dSRodney W. Grimes 	register vm_object_t object;
146df8bae1dSRodney W. Grimes {
14799448ed1SJohn Dyson 	int incr;
148df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
14924a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
150a1f6d91cSDavid Greenman 
15124a1cce3SDavid Greenman 	object->type = type;
152df8bae1dSRodney W. Grimes 	object->size = size;
153a1f6d91cSDavid Greenman 	object->ref_count = 1;
15424a1cce3SDavid Greenman 	object->flags = 0;
155867a482dSJohn Dyson 	object->behavior = OBJ_NORMAL;
156df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
157a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
158de5f6a77SJohn Dyson 	object->shadow_count = 0;
1595070c7f8SJohn Dyson 	object->pg_color = next_index;
16099448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
16199448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
16299448ed1SJohn Dyson 	else
16399448ed1SJohn Dyson 		incr = size;
16499448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
16524a1cce3SDavid Greenman 	object->handle = NULL;
166a316d390SJohn Dyson 	object->paging_offset = (vm_ooffset_t) 0;
16724a1cce3SDavid Greenman 	object->backing_object = NULL;
168a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
169a2f4a846SJohn Dyson 	object->page_hint = NULL;
170a1f6d91cSDavid Greenman 
171a1f6d91cSDavid Greenman 	object->last_read = 0;
1722d8acc0fSJohn Dyson 	object->generation++;
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
175df8bae1dSRodney W. Grimes 	vm_object_count++;
176df8bae1dSRodney W. Grimes }
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes /*
17926f9a767SRodney W. Grimes  *	vm_object_init:
18026f9a767SRodney W. Grimes  *
18126f9a767SRodney W. Grimes  *	Initialize the VM objects module.
18226f9a767SRodney W. Grimes  */
18326f9a767SRodney W. Grimes void
184a316d390SJohn Dyson vm_object_init()
18526f9a767SRodney W. Grimes {
18626f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
187996c772fSJohn Dyson 	simple_lock_init(&vm_object_list_lock);
18826f9a767SRodney W. Grimes 	vm_object_count = 0;
1890217125fSDavid Greenman 
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);
19799448ed1SJohn Dyson 
19899448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
19999448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
20099448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
20199448ed1SJohn Dyson }
20299448ed1SJohn Dyson 
20399448ed1SJohn Dyson void
20499448ed1SJohn Dyson vm_object_init2() {
2050a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
20626f9a767SRodney W. Grimes }
20726f9a767SRodney W. Grimes 
20826f9a767SRodney W. Grimes /*
20926f9a767SRodney W. Grimes  *	vm_object_allocate:
21026f9a767SRodney W. Grimes  *
21126f9a767SRodney W. Grimes  *	Returns a new object with the given size.
21226f9a767SRodney W. Grimes  */
21326f9a767SRodney W. Grimes 
21426f9a767SRodney W. Grimes vm_object_t
21524a1cce3SDavid Greenman vm_object_allocate(type, size)
21624a1cce3SDavid Greenman 	objtype_t type;
21726f9a767SRodney W. Grimes 	vm_size_t size;
21826f9a767SRodney W. Grimes {
21926f9a767SRodney W. Grimes 	register vm_object_t result;
22099448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
22167bf6868SJohn Dyson 
22224a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
22326f9a767SRodney W. Grimes 
22426f9a767SRodney W. Grimes 	return (result);
22526f9a767SRodney W. Grimes }
22626f9a767SRodney W. Grimes 
22726f9a767SRodney W. Grimes 
22826f9a767SRodney W. Grimes /*
229df8bae1dSRodney W. Grimes  *	vm_object_reference:
230df8bae1dSRodney W. Grimes  *
231df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
232df8bae1dSRodney W. Grimes  */
2336476c0d2SJohn Dyson void
23426f9a767SRodney W. Grimes vm_object_reference(object)
235df8bae1dSRodney W. Grimes 	register vm_object_t object;
236df8bae1dSRodney W. Grimes {
237df8bae1dSRodney W. Grimes 	if (object == NULL)
238df8bae1dSRodney W. Grimes 		return;
23995e5e988SJohn Dyson 
24095e5e988SJohn Dyson #if defined(DIAGNOSTIC)
24195e5e988SJohn Dyson 	if (object->flags & OBJ_DEAD)
24295e5e988SJohn Dyson 		panic("vm_object_reference: attempting to reference dead obj");
24395e5e988SJohn Dyson #endif
24495e5e988SJohn Dyson 
245df8bae1dSRodney W. Grimes 	object->ref_count++;
24647221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
24747221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
24847221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
24947221757SJohn Dyson 		}
25047221757SJohn Dyson 	}
25195e5e988SJohn Dyson }
25295e5e988SJohn Dyson 
253bf27292bSJohn Dyson void
25495e5e988SJohn Dyson vm_object_vndeallocate(object)
25595e5e988SJohn Dyson 	vm_object_t object;
25695e5e988SJohn Dyson {
25795e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
25895e5e988SJohn Dyson #if defined(DIAGNOSTIC)
25995e5e988SJohn Dyson 	if (object->type != OBJT_VNODE)
26095e5e988SJohn Dyson 		panic("vm_object_vndeallocate: not a vnode object");
26195e5e988SJohn Dyson 	if (vp == NULL)
26295e5e988SJohn Dyson 		panic("vm_object_vndeallocate: missing vp");
26395e5e988SJohn Dyson 	if (object->ref_count == 0) {
26495e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
26595e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
26695e5e988SJohn Dyson 	}
26795e5e988SJohn Dyson #endif
26895e5e988SJohn Dyson 
26995e5e988SJohn Dyson 	object->ref_count--;
27047221757SJohn Dyson 	if (object->ref_count == 0) {
271bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
2722d8acc0fSJohn Dyson 		object->flags &= ~OBJ_OPT;
2732be70f79SJohn Dyson 	}
27447221757SJohn Dyson 	vrele(vp);
275df8bae1dSRodney W. Grimes }
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes /*
278df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
279df8bae1dSRodney W. Grimes  *
280df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
281df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
282df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
283df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
284df8bae1dSRodney W. Grimes  *	may be relinquished.
285df8bae1dSRodney W. Grimes  *
286df8bae1dSRodney W. Grimes  *	No object may be locked.
287df8bae1dSRodney W. Grimes  */
28826f9a767SRodney W. Grimes void
28926f9a767SRodney W. Grimes vm_object_deallocate(object)
29026f9a767SRodney W. Grimes 	vm_object_t object;
291df8bae1dSRodney W. Grimes {
29295e5e988SJohn Dyson 	int s;
293df8bae1dSRodney W. Grimes 	vm_object_t temp;
294df8bae1dSRodney W. Grimes 
295df8bae1dSRodney W. Grimes 	while (object != NULL) {
296df8bae1dSRodney W. Grimes 
29795e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
29895e5e988SJohn Dyson 			vm_object_vndeallocate(object);
29995e5e988SJohn Dyson 			return;
30095e5e988SJohn Dyson 		}
30195e5e988SJohn Dyson 
3022be70f79SJohn Dyson 		if (object->ref_count == 0) {
30347221757SJohn Dyson 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
3042be70f79SJohn Dyson 		} else if (object->ref_count > 2) {
3052be70f79SJohn Dyson 			object->ref_count--;
3062be70f79SJohn Dyson 			return;
3072be70f79SJohn Dyson 		}
3082be70f79SJohn Dyson 
3092be70f79SJohn Dyson 		/*
3102be70f79SJohn Dyson 		 * Here on ref_count of one or two, which are special cases for
3112be70f79SJohn Dyson 		 * objects.
3122be70f79SJohn Dyson 		 */
31395e5e988SJohn Dyson 		if ((object->ref_count == 2) && (object->shadow_count == 1)) {
3142be70f79SJohn Dyson 
315be6d5bfaSDavid Greenman 			object->ref_count--;
3162be70f79SJohn Dyson 			if ((object->handle == NULL) &&
31724a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
31824a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
319a1f6d91cSDavid Greenman 				vm_object_t robject;
32095e5e988SJohn Dyson 
321b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
32295e5e988SJohn Dyson #if defined(DIAGNOSTIC)
32395e5e988SJohn Dyson 				if (robject == NULL)
32495e5e988SJohn Dyson 					panic("vm_object_deallocate: ref_count: %d,"
32595e5e988SJohn Dyson 						  " shadow_count: %d",
32695e5e988SJohn Dyson 						  object->ref_count, object->shadow_count);
32795e5e988SJohn Dyson #endif
32895e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
32924a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
33024a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
331a1f6d91cSDavid Greenman 
33295e5e988SJohn Dyson 					robject->ref_count++;
33395e5e988SJohn Dyson 
33495e5e988SJohn Dyson 			retry:
335b18bfc3dSJohn Dyson 					s = splvm();
33695e5e988SJohn Dyson 					if (robject->paging_in_progress) {
337c0503609SDavid Greenman 						robject->flags |= OBJ_PIPWNT;
338a1f6d91cSDavid Greenman 						tsleep(robject, PVM, "objde1", 0);
339925a3a41SJohn Dyson 						splx(s);
34095e5e988SJohn Dyson 						goto retry;
3410d94caffSDavid Greenman 					}
342a1f6d91cSDavid Greenman 
34395e5e988SJohn Dyson 					if (object->paging_in_progress) {
344c0503609SDavid Greenman 						object->flags |= OBJ_PIPWNT;
345a1f6d91cSDavid Greenman 						tsleep(object, PVM, "objde2", 0);
346925a3a41SJohn Dyson 						splx(s);
34795e5e988SJohn Dyson 						goto retry;
348a1f6d91cSDavid Greenman 					}
349a1f6d91cSDavid Greenman 					splx(s);
350a1f6d91cSDavid Greenman 
35195e5e988SJohn Dyson 					if( robject->ref_count == 1) {
35295e5e988SJohn Dyson 						robject->ref_count--;
353ba8da839SDavid Greenman 						object = robject;
35495e5e988SJohn Dyson 						goto doterm;
35595e5e988SJohn Dyson 					}
35695e5e988SJohn Dyson 
35795e5e988SJohn Dyson 					object = robject;
35895e5e988SJohn Dyson 					vm_object_collapse(object);
359ba8da839SDavid Greenman 					continue;
360a1f6d91cSDavid Greenman 				}
36195e5e988SJohn Dyson 			}
36295e5e988SJohn Dyson 
363a1f6d91cSDavid Greenman 			return;
364df8bae1dSRodney W. Grimes 
36595e5e988SJohn Dyson 		} else {
3661efb74fbSJohn Dyson 			object->ref_count--;
36795e5e988SJohn Dyson 			if (object->ref_count != 0)
36895e5e988SJohn Dyson 				return;
36995e5e988SJohn Dyson 		}
37095e5e988SJohn Dyson 
37195e5e988SJohn Dyson doterm:
3721efb74fbSJohn Dyson 
37324a1cce3SDavid Greenman 		temp = object->backing_object;
374de5f6a77SJohn Dyson 		if (temp) {
37524a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
37695e5e988SJohn Dyson 			temp->shadow_count--;
3772d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
378bf27292bSJohn Dyson 				temp->flags &= ~OBJ_OPT;
379de5f6a77SJohn Dyson 		}
380df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
381df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
382df8bae1dSRodney W. Grimes 		object = temp;
383df8bae1dSRodney W. Grimes 	}
384df8bae1dSRodney W. Grimes }
385df8bae1dSRodney W. Grimes 
386df8bae1dSRodney W. Grimes /*
387df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
388df8bae1dSRodney W. Grimes  *	up all previously used resources.
389df8bae1dSRodney W. Grimes  *
390df8bae1dSRodney W. Grimes  *	The object must be locked.
391df8bae1dSRodney W. Grimes  */
39295e5e988SJohn Dyson void
39326f9a767SRodney W. Grimes vm_object_terminate(object)
394df8bae1dSRodney W. Grimes 	register vm_object_t object;
395df8bae1dSRodney W. Grimes {
3963af76890SPoul-Henning Kamp 	register vm_page_t p;
39726f9a767SRodney W. Grimes 	int s;
398df8bae1dSRodney W. Grimes 
39995e5e988SJohn Dyson 	/*
40095e5e988SJohn Dyson 	 * Make sure no one uses us.
40195e5e988SJohn Dyson 	 */
40295e5e988SJohn Dyson 	object->flags |= OBJ_DEAD;
4033c631446SJohn Dyson 
404df8bae1dSRodney W. Grimes 	/*
405f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
406df8bae1dSRodney W. Grimes 	 */
407b18bfc3dSJohn Dyson 	s = splvm();
408df8bae1dSRodney W. Grimes 	while (object->paging_in_progress) {
409c0503609SDavid Greenman 		object->flags |= OBJ_PIPWNT;
41024a1cce3SDavid Greenman 		tsleep(object, PVM, "objtrm", 0);
411df8bae1dSRodney W. Grimes 	}
4120d94caffSDavid Greenman 	splx(s);
413df8bae1dSRodney W. Grimes 
41495e5e988SJohn Dyson #if defined(DIAGNOSTIC)
41526f9a767SRodney W. Grimes 	if (object->paging_in_progress != 0)
41626f9a767SRodney W. Grimes 		panic("vm_object_deallocate: pageout in progress");
41795e5e988SJohn Dyson #endif
41826f9a767SRodney W. Grimes 
41926f9a767SRodney W. Grimes 	/*
4200d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4210d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
42226f9a767SRodney W. Grimes 	 */
42324a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
42495e5e988SJohn Dyson 		struct vnode *vp;
42595e5e988SJohn Dyson 
42695e5e988SJohn Dyson 		/*
42795e5e988SJohn Dyson 		 * Freeze optimized copies.
42895e5e988SJohn Dyson 		 */
42995e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
43095e5e988SJohn Dyson 
43195e5e988SJohn Dyson 		/*
43295e5e988SJohn Dyson 		 * Clean pages and flush buffers.
43395e5e988SJohn Dyson 		 */
4342be70f79SJohn Dyson 		vm_object_page_clean(object, 0, 0, TRUE);
43595e5e988SJohn Dyson 
43695e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
437f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
43895e5e988SJohn Dyson 
43995e5e988SJohn Dyson 	} else {
440996c772fSJohn Dyson 
4410d94caffSDavid Greenman 		/*
4420d94caffSDavid Greenman 		 * Now free the pages. For internal objects, this also removes them
4430d94caffSDavid Greenman 		 * from paging queues.
444df8bae1dSRodney W. Grimes 		 */
445b18bfc3dSJohn Dyson 		while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
446a2f4a846SJohn Dyson 			if (p->busy || (p->flags & PG_BUSY))
447be6d5bfaSDavid Greenman 				printf("vm_object_terminate: freeing busy page\n");
4480d94caffSDavid Greenman 			PAGE_WAKEUP(p);
449df8bae1dSRodney W. Grimes 			vm_page_free(p);
450df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
451df8bae1dSRodney W. Grimes 		}
45295e5e988SJohn Dyson 	}
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes 	/*
455df8bae1dSRodney W. Grimes 	 * Let the pager know object is dead.
456df8bae1dSRodney W. Grimes 	 */
45724a1cce3SDavid Greenman 	vm_pager_deallocate(object);
458df8bae1dSRodney W. Grimes 
45947221757SJohn Dyson 	if (object->ref_count == 0) {
4602d8acc0fSJohn Dyson 		vm_object_dispose(object);
4612d8acc0fSJohn Dyson 	}
4622d8acc0fSJohn Dyson }
4632d8acc0fSJohn Dyson 
4642d8acc0fSJohn Dyson /*
4652d8acc0fSJohn Dyson  * vm_object_dispose
4662d8acc0fSJohn Dyson  *
4672d8acc0fSJohn Dyson  * Dispose the object.
4682d8acc0fSJohn Dyson  */
4692d8acc0fSJohn Dyson void
4702d8acc0fSJohn Dyson vm_object_dispose(object)
4712d8acc0fSJohn Dyson 	vm_object_t object;
4722d8acc0fSJohn Dyson {
473996c772fSJohn Dyson 		simple_lock(&vm_object_list_lock);
474df8bae1dSRodney W. Grimes 		TAILQ_REMOVE(&vm_object_list, object, object_list);
475df8bae1dSRodney W. Grimes 		vm_object_count--;
476996c772fSJohn Dyson 		simple_unlock(&vm_object_list_lock);
477df8bae1dSRodney W. Grimes 		/*
478df8bae1dSRodney W. Grimes    		* Free the space for the object.
479df8bae1dSRodney W. Grimes    		*/
48099448ed1SJohn Dyson 		zfree(obj_zone, object);
48147221757SJohn Dyson 		wakeup(object);
48247221757SJohn Dyson }
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes /*
485df8bae1dSRodney W. Grimes  *	vm_object_page_clean
486df8bae1dSRodney W. Grimes  *
487df8bae1dSRodney W. Grimes  *	Clean all dirty pages in the specified range of object.
48826f9a767SRodney W. Grimes  *	Leaves page on whatever queue it is currently on.
48926f9a767SRodney W. Grimes  *
49026f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
49126f9a767SRodney W. Grimes  *
49226f9a767SRodney W. Grimes  *	The object must be locked.
49326f9a767SRodney W. Grimes  */
494f6b04d2bSDavid Greenman 
495f6b04d2bSDavid Greenman void
4962be70f79SJohn Dyson vm_object_page_clean(object, start, end, syncio)
497f6b04d2bSDavid Greenman 	vm_object_t object;
498a316d390SJohn Dyson 	vm_pindex_t start;
499a316d390SJohn Dyson 	vm_pindex_t end;
500f6b04d2bSDavid Greenman 	boolean_t syncio;
501f6b04d2bSDavid Greenman {
502bd7e5f99SJohn Dyson 	register vm_page_t p, np, tp;
503f6b04d2bSDavid Greenman 	register vm_offset_t tstart, tend;
504bd7e5f99SJohn Dyson 	vm_pindex_t pi;
505aef922f5SJohn Dyson 	int s;
50624a1cce3SDavid Greenman 	struct vnode *vp;
507aef922f5SJohn Dyson 	int runlen;
508bd7e5f99SJohn Dyson 	int maxf;
509bd7e5f99SJohn Dyson 	int chkb;
510bd7e5f99SJohn Dyson 	int maxb;
511bd7e5f99SJohn Dyson 	int i;
512bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
513bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
514aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5152d8acc0fSJohn Dyson 	int curgeneration;
516996c772fSJohn Dyson 	struct proc *pproc = curproc;	/* XXX */
517f6b04d2bSDavid Greenman 
518aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
519aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
520f6b04d2bSDavid Greenman 		return;
521f6b04d2bSDavid Greenman 
52224a1cce3SDavid Greenman 	vp = object->handle;
52324a1cce3SDavid Greenman 
524aef922f5SJohn Dyson 	object->flags |= OBJ_CLEANING;
52524a1cce3SDavid Greenman 
526f6b04d2bSDavid Greenman 	tstart = start;
527f6b04d2bSDavid Greenman 	if (end == 0) {
528f6b04d2bSDavid Greenman 		tend = object->size;
529f6b04d2bSDavid Greenman 	} else {
530f6b04d2bSDavid Greenman 		tend = end;
531f6b04d2bSDavid Greenman 	}
532a316d390SJohn Dyson 	if ((tstart == 0) && (tend == object->size)) {
533aef922f5SJohn Dyson 		object->flags &= ~(OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
534ec4f9fb0SDavid Greenman 	}
535b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq))
536bd7e5f99SJohn Dyson 		p->flags |= PG_CLEANCHK;
537f6b04d2bSDavid Greenman 
538bd7e5f99SJohn Dyson rescan:
5392d8acc0fSJohn Dyson 	curgeneration = object->generation;
5402d8acc0fSJohn Dyson 
541b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
542b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
543bd7e5f99SJohn Dyson 
544bd7e5f99SJohn Dyson 		pi = p->pindex;
545bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
546bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5475070c7f8SJohn Dyson 			(p->valid == 0) ||
5485070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
549bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
550aef922f5SJohn Dyson 			continue;
551f6b04d2bSDavid Greenman 		}
552f6b04d2bSDavid Greenman 
553bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
554bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
555bd7e5f99SJohn Dyson 			p->flags &= ~PG_CLEANCHK;
556bd7e5f99SJohn Dyson 			continue;
557bd7e5f99SJohn Dyson 		}
558ec4f9fb0SDavid Greenman 
559b18bfc3dSJohn Dyson 		s = splvm();
5602d8acc0fSJohn Dyson 		while ((p->flags & PG_BUSY) || p->busy) {
561aef922f5SJohn Dyson 			p->flags |= PG_WANTED|PG_REFERENCED;
562aef922f5SJohn Dyson 			tsleep(p, PVM, "vpcwai", 0);
5632d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
564f6b04d2bSDavid Greenman 				splx(s);
565bd7e5f99SJohn Dyson 				goto rescan;
566f6b04d2bSDavid Greenman 			}
5672d8acc0fSJohn Dyson 		}
568f6b04d2bSDavid Greenman 		splx(s);
569f6b04d2bSDavid Greenman 
570f35329acSJohn Dyson 		s = splvm();
571bd7e5f99SJohn Dyson 		maxf = 0;
572bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
573bd7e5f99SJohn Dyson 			if (tp = vm_page_lookup(object, pi + i)) {
574bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
575bd7e5f99SJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0)
576bd7e5f99SJohn Dyson 					break;
5775070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
5783077a9c2SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
5793077a9c2SJohn Dyson 					break;
5803077a9c2SJohn Dyson 				}
581bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
582bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
583bd7e5f99SJohn Dyson 					tp->flags &= ~PG_CLEANCHK;
584bd7e5f99SJohn Dyson 					break;
585bd7e5f99SJohn Dyson 				}
586bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
587bd7e5f99SJohn Dyson 				maxf++;
588bd7e5f99SJohn Dyson 				continue;
589bd7e5f99SJohn Dyson 			}
590bd7e5f99SJohn Dyson 			break;
591bd7e5f99SJohn Dyson 		}
592aef922f5SJohn Dyson 
593bd7e5f99SJohn Dyson 		maxb = 0;
594bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
595bd7e5f99SJohn Dyson 		if (chkb) {
596bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
597bd7e5f99SJohn Dyson 				if (tp = vm_page_lookup(object, pi - i)) {
598bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
599bd7e5f99SJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0)
600bd7e5f99SJohn Dyson 						break;
6015070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
6023077a9c2SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
6033077a9c2SJohn Dyson 						break;
6043077a9c2SJohn Dyson 					}
605bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
606bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
607bd7e5f99SJohn Dyson 						tp->flags &= ~PG_CLEANCHK;
608bd7e5f99SJohn Dyson 						break;
609bd7e5f99SJohn Dyson 					}
610bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
611bd7e5f99SJohn Dyson 					maxb++;
612bd7e5f99SJohn Dyson 					continue;
613bd7e5f99SJohn Dyson 				}
614bd7e5f99SJohn Dyson 				break;
615bd7e5f99SJohn Dyson 			}
616bd7e5f99SJohn Dyson 		}
617bd7e5f99SJohn Dyson 
618bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
619bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
620bd7e5f99SJohn Dyson 			ma[index] = mab[i];
621bd7e5f99SJohn Dyson 			ma[index]->flags |= PG_BUSY;
622bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
623bd7e5f99SJohn Dyson 			vm_page_protect(ma[index], VM_PROT_READ);
624bd7e5f99SJohn Dyson 		}
625bd7e5f99SJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
626aef922f5SJohn Dyson 		p->flags |= PG_BUSY;
627bd7e5f99SJohn Dyson 		p->flags &= ~PG_CLEANCHK;
628bd7e5f99SJohn Dyson 		ma[maxb] = p;
629bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
630bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
631bd7e5f99SJohn Dyson 			ma[index] = maf[i];
632bd7e5f99SJohn Dyson 			ma[index]->flags |= PG_BUSY;
633bd7e5f99SJohn Dyson 			ma[index]->flags &= ~PG_CLEANCHK;
634bd7e5f99SJohn Dyson 			vm_page_protect(ma[index], VM_PROT_READ);
635f6b04d2bSDavid Greenman 		}
636bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
637f35329acSJohn Dyson 		splx(s);
638bd7e5f99SJohn Dyson 		vm_pageout_flush(ma, runlen, 0);
6392d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
640bd7e5f99SJohn Dyson 			goto rescan;
641f6b04d2bSDavid Greenman 	}
642aef922f5SJohn Dyson 
643aef922f5SJohn Dyson 	VOP_FSYNC(vp, NULL, syncio, curproc);
644aef922f5SJohn Dyson 
645aef922f5SJohn Dyson 	object->flags &= ~OBJ_CLEANING;
646f5cf85d4SDavid Greenman 	return;
64726f9a767SRodney W. Grimes }
648df8bae1dSRodney W. Grimes 
649f708ef1bSPoul-Henning Kamp #ifdef not_used
650f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
651df8bae1dSRodney W. Grimes /*
652df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
653df8bae1dSRodney W. Grimes  *
654df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
655df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
656df8bae1dSRodney W. Grimes  *
657df8bae1dSRodney W. Grimes  *	The object must be locked.
658df8bae1dSRodney W. Grimes  */
659f708ef1bSPoul-Henning Kamp static void
660df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
661df8bae1dSRodney W. Grimes 	register vm_object_t object;
662df8bae1dSRodney W. Grimes {
663df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
664df8bae1dSRodney W. Grimes 
665b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
666b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
667df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
668df8bae1dSRodney W. Grimes 	}
669df8bae1dSRodney W. Grimes }
670f708ef1bSPoul-Henning Kamp #endif
671df8bae1dSRodney W. Grimes 
672df8bae1dSRodney W. Grimes /*
673df8bae1dSRodney W. Grimes  *	vm_object_pmap_copy:
674df8bae1dSRodney W. Grimes  *
675df8bae1dSRodney W. Grimes  *	Makes all physical pages in the specified
676df8bae1dSRodney W. Grimes  *	object range copy-on-write.  No writeable
677df8bae1dSRodney W. Grimes  *	references to these pages should remain.
678df8bae1dSRodney W. Grimes  *
679df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
680df8bae1dSRodney W. Grimes  */
6810d94caffSDavid Greenman void
6820d94caffSDavid Greenman vm_object_pmap_copy(object, start, end)
683df8bae1dSRodney W. Grimes 	register vm_object_t object;
684a316d390SJohn Dyson 	register vm_pindex_t start;
685a316d390SJohn Dyson 	register vm_pindex_t end;
686df8bae1dSRodney W. Grimes {
687df8bae1dSRodney W. Grimes 	register vm_page_t p;
688df8bae1dSRodney W. Grimes 
689aef922f5SJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
690df8bae1dSRodney W. Grimes 		return;
691df8bae1dSRodney W. Grimes 
692b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
693b5b40fa6SJohn Dyson 		p != NULL;
694b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
695f919ebdeSDavid Greenman 		vm_page_protect(p, VM_PROT_READ);
696df8bae1dSRodney W. Grimes 	}
697aef922f5SJohn Dyson 
698aef922f5SJohn Dyson 	object->flags &= ~OBJ_WRITEABLE;
699df8bae1dSRodney W. Grimes }
700df8bae1dSRodney W. Grimes 
701df8bae1dSRodney W. Grimes /*
7021efb74fbSJohn Dyson  * Same as vm_object_pmap_copy_1, except range checking really
7031efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
7041efb74fbSJohn Dyson  */
7051efb74fbSJohn Dyson void
7061efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end)
7071efb74fbSJohn Dyson 	register vm_object_t object;
7081efb74fbSJohn Dyson 	register vm_pindex_t start;
7091efb74fbSJohn Dyson 	register vm_pindex_t end;
7101efb74fbSJohn Dyson {
7111efb74fbSJohn Dyson 	vm_pindex_t idx;
7121efb74fbSJohn Dyson 	register vm_page_t p;
7131efb74fbSJohn Dyson 
7141efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
7151efb74fbSJohn Dyson 		return;
7161efb74fbSJohn Dyson 
7171efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
7181efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
7191efb74fbSJohn Dyson 		if (p == NULL)
7201efb74fbSJohn Dyson 			continue;
7211efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
7221efb74fbSJohn Dyson 	}
7231efb74fbSJohn Dyson }
7241efb74fbSJohn Dyson 
7251efb74fbSJohn Dyson /*
726df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
727df8bae1dSRodney W. Grimes  *
728df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
729df8bae1dSRodney W. Grimes  *	object range from all physical maps.
730df8bae1dSRodney W. Grimes  *
731df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
732df8bae1dSRodney W. Grimes  */
73326f9a767SRodney W. Grimes void
73426f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
735df8bae1dSRodney W. Grimes 	register vm_object_t object;
736a316d390SJohn Dyson 	register vm_pindex_t start;
737a316d390SJohn Dyson 	register vm_pindex_t end;
738df8bae1dSRodney W. Grimes {
739df8bae1dSRodney W. Grimes 	register vm_page_t p;
740df8bae1dSRodney W. Grimes 	if (object == NULL)
741df8bae1dSRodney W. Grimes 		return;
742b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
743b5b40fa6SJohn Dyson 		p != NULL;
744b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
745bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
746f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
74726f9a767SRodney W. Grimes 	}
7486e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
7496e20a165SJohn Dyson 		object->flags &= ~OBJ_WRITEABLE;
75026f9a767SRodney W. Grimes }
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes /*
753867a482dSJohn Dyson  *	vm_object_madvise:
754867a482dSJohn Dyson  *
755867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
756867a482dSJohn Dyson  */
757867a482dSJohn Dyson void
758867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
759867a482dSJohn Dyson 	vm_object_t object;
760867a482dSJohn Dyson 	vm_pindex_t pindex;
761867a482dSJohn Dyson 	int count;
762867a482dSJohn Dyson 	int advise;
763867a482dSJohn Dyson {
7646e20a165SJohn Dyson 	int s;
7656e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
7666e20a165SJohn Dyson 	vm_object_t tobject;
767867a482dSJohn Dyson 	vm_page_t m;
768867a482dSJohn Dyson 
769867a482dSJohn Dyson 	if (object == NULL)
770867a482dSJohn Dyson 		return;
771867a482dSJohn Dyson 
772867a482dSJohn Dyson 	end = pindex + count;
773867a482dSJohn Dyson 
774867a482dSJohn Dyson 	for (; pindex < end; pindex += 1) {
7756e20a165SJohn Dyson 
7766e20a165SJohn Dyson relookup:
7776e20a165SJohn Dyson 		tobject = object;
7786e20a165SJohn Dyson 		tpindex = pindex;
7796e20a165SJohn Dyson shadowlookup:
7806e20a165SJohn Dyson 		m = vm_page_lookup(tobject, tpindex);
7816e20a165SJohn Dyson 		if (m == NULL) {
7826e20a165SJohn Dyson 			if (tobject->type != OBJT_DEFAULT) {
7836e20a165SJohn Dyson 				continue;
7846e20a165SJohn Dyson 			}
7856e20a165SJohn Dyson 
7866e20a165SJohn Dyson 			tobject = tobject->backing_object;
7876e20a165SJohn Dyson 			if ((tobject == NULL) || (tobject->ref_count != 1)) {
7886e20a165SJohn Dyson 				continue;
7896e20a165SJohn Dyson 			}
7906e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
7916e20a165SJohn Dyson 			goto shadowlookup;
7926e20a165SJohn Dyson 		}
793867a482dSJohn Dyson 
794867a482dSJohn Dyson 		/*
795867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
796867a482dSJohn Dyson 		 * we skip it.  Things can break if we mess with pages
797867a482dSJohn Dyson 		 * in any of the below states.
798867a482dSJohn Dyson 		 */
7996e20a165SJohn Dyson 		if (m->hold_count || m->wire_count ||
8006e20a165SJohn Dyson 			m->valid != VM_PAGE_BITS_ALL) {
801867a482dSJohn Dyson 			continue;
8026e20a165SJohn Dyson 		}
8036e20a165SJohn Dyson 
8046e20a165SJohn Dyson 		if (m->busy || (m->flags & PG_BUSY)) {
8056e20a165SJohn Dyson 			s = splvm();
8066e20a165SJohn Dyson 			if (m->busy || (m->flags & PG_BUSY)) {
8076e20a165SJohn Dyson 				m->flags |= PG_WANTED;
8086e20a165SJohn Dyson 				tsleep(m, PVM, "madvpw", 0);
8096e20a165SJohn Dyson 			}
8106e20a165SJohn Dyson 			splx(s);
8116e20a165SJohn Dyson 			goto relookup;
8126e20a165SJohn Dyson 		}
813867a482dSJohn Dyson 
814867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
815867a482dSJohn Dyson 			if (m->queue != PQ_ACTIVE)
816867a482dSJohn Dyson 				vm_page_activate(m);
8176e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
818867a482dSJohn Dyson 			vm_page_deactivate(m);
8190a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8206e20a165SJohn Dyson 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
8216e20a165SJohn Dyson 			m->dirty = 0;
8220a47b48bSJohn Dyson 			/*
8236e20a165SJohn Dyson 			 * Force a demand zero if attempt to read from swap.
8246e20a165SJohn Dyson 			 * We currently don't handle vnode files correctly,
8256e20a165SJohn Dyson 			 * and will reread stale contents unnecessarily.
8260a47b48bSJohn Dyson 			 */
8270a47b48bSJohn Dyson 			if (object->type == OBJT_SWAP)
8286e20a165SJohn Dyson 				swap_pager_dmzspace(tobject, m->pindex, 1);
829867a482dSJohn Dyson 		}
830867a482dSJohn Dyson 	}
831867a482dSJohn Dyson }
832867a482dSJohn Dyson 
833867a482dSJohn Dyson /*
834df8bae1dSRodney W. Grimes  *	vm_object_shadow:
835df8bae1dSRodney W. Grimes  *
836df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
837df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
838df8bae1dSRodney W. Grimes  *	object reference is deallocated.
839df8bae1dSRodney W. Grimes  *
840df8bae1dSRodney W. Grimes  *	The new object and offset into that object
841df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
842df8bae1dSRodney W. Grimes  */
843df8bae1dSRodney W. Grimes 
84426f9a767SRodney W. Grimes void
84526f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
846df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
847a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
848df8bae1dSRodney W. Grimes 	vm_size_t length;
849df8bae1dSRodney W. Grimes {
850df8bae1dSRodney W. Grimes 	register vm_object_t source;
851df8bae1dSRodney W. Grimes 	register vm_object_t result;
852df8bae1dSRodney W. Grimes 
853df8bae1dSRodney W. Grimes 	source = *object;
854df8bae1dSRodney W. Grimes 
855df8bae1dSRodney W. Grimes 	/*
856df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
857df8bae1dSRodney W. Grimes 	 */
858df8bae1dSRodney W. Grimes 
85924a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
860df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
861df8bae1dSRodney W. Grimes 
862df8bae1dSRodney W. Grimes 	/*
8630d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
8640d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
8650d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
8660d94caffSDavid Greenman 	 * of reference count.
867df8bae1dSRodney W. Grimes 	 */
86824a1cce3SDavid Greenman 	result->backing_object = source;
869de5f6a77SJohn Dyson 	if (source) {
870de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
871de5f6a77SJohn Dyson 		++source->shadow_count;
872de5f6a77SJohn Dyson 	}
873df8bae1dSRodney W. Grimes 
874df8bae1dSRodney W. Grimes 	/*
8750d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
8760d94caffSDavid Greenman 	 * the new object.
877df8bae1dSRodney W. Grimes 	 */
878df8bae1dSRodney W. Grimes 
87924a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
880df8bae1dSRodney W. Grimes 
881df8bae1dSRodney W. Grimes 	/*
882df8bae1dSRodney W. Grimes 	 * Return the new things
883df8bae1dSRodney W. Grimes 	 */
884df8bae1dSRodney W. Grimes 
885df8bae1dSRodney W. Grimes 	*offset = 0;
886df8bae1dSRodney W. Grimes 	*object = result;
887df8bae1dSRodney W. Grimes }
888df8bae1dSRodney W. Grimes 
889df8bae1dSRodney W. Grimes 
890df8bae1dSRodney W. Grimes /*
8912fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
8922fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
8932fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
8942fe6e4d7SDavid Greenman  */
8952fe6e4d7SDavid Greenman static void
8962fe6e4d7SDavid Greenman vm_object_qcollapse(object)
8972fe6e4d7SDavid Greenman 	register vm_object_t object;
8982fe6e4d7SDavid Greenman {
8992fe6e4d7SDavid Greenman 	register vm_object_t backing_object;
900a316d390SJohn Dyson 	register vm_pindex_t backing_offset_index, paging_offset_index;
901a316d390SJohn Dyson 	vm_pindex_t backing_object_paging_offset_index;
902a316d390SJohn Dyson 	vm_pindex_t new_pindex;
9032fe6e4d7SDavid Greenman 	register vm_page_t p, pp;
9042fe6e4d7SDavid Greenman 	register vm_size_t size;
9052fe6e4d7SDavid Greenman 
90624a1cce3SDavid Greenman 	backing_object = object->backing_object;
9072fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
9082fe6e4d7SDavid Greenman 		return;
9092fe6e4d7SDavid Greenman 
910010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
911010cf3b9SDavid Greenman 
912a316d390SJohn Dyson 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
913a316d390SJohn Dyson 	backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset);
914a316d390SJohn Dyson 	paging_offset_index = OFF_TO_IDX(object->paging_offset);
9152fe6e4d7SDavid Greenman 	size = object->size;
916b18bfc3dSJohn Dyson 	p = TAILQ_FIRST(&backing_object->memq);
9172fe6e4d7SDavid Greenman 	while (p) {
9182fe6e4d7SDavid Greenman 		vm_page_t next;
9190d94caffSDavid Greenman 
920b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
921bd7e5f99SJohn Dyson 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
9225070c7f8SJohn Dyson 		    ((p->queue - p->pc) == PQ_CACHE) ||
9235070c7f8SJohn Dyson 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
9242fe6e4d7SDavid Greenman 			p = next;
9252fe6e4d7SDavid Greenman 			continue;
9262fe6e4d7SDavid Greenman 		}
927a316d390SJohn Dyson 		new_pindex = p->pindex - backing_offset_index;
928a316d390SJohn Dyson 		if (p->pindex < backing_offset_index ||
929a316d390SJohn Dyson 		    new_pindex >= size) {
93024a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP)
93124a1cce3SDavid Greenman 				swap_pager_freespace(backing_object,
932a316d390SJohn Dyson 				    backing_object_paging_offset_index+p->pindex,
933a316d390SJohn Dyson 				    1);
934b18bfc3dSJohn Dyson 			vm_page_protect(p, VM_PROT_NONE);
9352fe6e4d7SDavid Greenman 			vm_page_free(p);
9362fe6e4d7SDavid Greenman 		} else {
937a316d390SJohn Dyson 			pp = vm_page_lookup(object, new_pindex);
93824a1cce3SDavid Greenman 			if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
939a316d390SJohn Dyson 				    paging_offset_index + new_pindex, NULL, NULL))) {
94024a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
94124a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
942a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
943b18bfc3dSJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
9442fe6e4d7SDavid Greenman 				vm_page_free(p);
9452fe6e4d7SDavid Greenman 			} else {
94624a1cce3SDavid Greenman 				if (backing_object->type == OBJT_SWAP)
94724a1cce3SDavid Greenman 					swap_pager_freespace(backing_object,
948a316d390SJohn Dyson 					    backing_object_paging_offset_index + p->pindex, 1);
949a316d390SJohn Dyson 				vm_page_rename(p, object, new_pindex);
9506e20a165SJohn Dyson 				vm_page_protect(p, VM_PROT_NONE);
9519b4814bbSDavid Greenman 				p->dirty = VM_PAGE_BITS_ALL;
9522fe6e4d7SDavid Greenman 			}
9532fe6e4d7SDavid Greenman 		}
9542fe6e4d7SDavid Greenman 		p = next;
9552fe6e4d7SDavid Greenman 	}
956010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
9572fe6e4d7SDavid Greenman }
9582fe6e4d7SDavid Greenman 
959df8bae1dSRodney W. Grimes /*
960df8bae1dSRodney W. Grimes  *	vm_object_collapse:
961df8bae1dSRodney W. Grimes  *
962df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
963df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
964df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
965df8bae1dSRodney W. Grimes  */
96626f9a767SRodney W. Grimes void
96726f9a767SRodney W. Grimes vm_object_collapse(object)
96824a1cce3SDavid Greenman 	vm_object_t object;
969df8bae1dSRodney W. Grimes 
970df8bae1dSRodney W. Grimes {
97124a1cce3SDavid Greenman 	vm_object_t backing_object;
972a316d390SJohn Dyson 	vm_ooffset_t backing_offset;
97324a1cce3SDavid Greenman 	vm_size_t size;
974a316d390SJohn Dyson 	vm_pindex_t new_pindex, backing_offset_index;
97524a1cce3SDavid Greenman 	vm_page_t p, pp;
976df8bae1dSRodney W. Grimes 
977df8bae1dSRodney W. Grimes 	while (TRUE) {
978df8bae1dSRodney W. Grimes 		/*
979df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
980df8bae1dSRodney W. Grimes 		 *
9810d94caffSDavid Greenman 		 * The object exists and no pages in it are currently being paged
9820d94caffSDavid Greenman 		 * out.
983df8bae1dSRodney W. Grimes 		 */
9842fe6e4d7SDavid Greenman 		if (object == NULL)
985df8bae1dSRodney W. Grimes 			return;
986df8bae1dSRodney W. Grimes 
987b9921222SDavid Greenman 		/*
988b9921222SDavid Greenman 		 * Make sure there is a backing object.
989b9921222SDavid Greenman 		 */
99024a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
991df8bae1dSRodney W. Grimes 			return;
992df8bae1dSRodney W. Grimes 
993f919ebdeSDavid Greenman 		/*
994f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
99524a1cce3SDavid Greenman 		 * not collapsable.
996f919ebdeSDavid Greenman 		 */
99724a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
99824a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
99924a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1000f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
100124a1cce3SDavid Greenman 		    object->handle != NULL ||
100224a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
100324a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
100424a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
10059b4814bbSDavid Greenman 			return;
100624a1cce3SDavid Greenman 		}
10079b4814bbSDavid Greenman 
1008f919ebdeSDavid Greenman 		if (object->paging_in_progress != 0 ||
1009f919ebdeSDavid Greenman 		    backing_object->paging_in_progress != 0) {
1010b9921222SDavid Greenman 			vm_object_qcollapse(object);
1011df8bae1dSRodney W. Grimes 			return;
1012df8bae1dSRodney W. Grimes 		}
1013f919ebdeSDavid Greenman 
101426f9a767SRodney W. Grimes 		/*
10150d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
10160d94caffSDavid Greenman 		 * the parent is the only reference to it) or (perhaps) remove
10170d94caffSDavid Greenman 		 * the parent's reference to it.
1018df8bae1dSRodney W. Grimes 		 */
1019df8bae1dSRodney W. Grimes 
102024a1cce3SDavid Greenman 		backing_offset = object->backing_object_offset;
1021a316d390SJohn Dyson 		backing_offset_index = OFF_TO_IDX(backing_offset);
1022df8bae1dSRodney W. Grimes 		size = object->size;
1023df8bae1dSRodney W. Grimes 
1024df8bae1dSRodney W. Grimes 		/*
10250d94caffSDavid Greenman 		 * If there is exactly one reference to the backing object, we
10260d94caffSDavid Greenman 		 * can collapse it into the parent.
1027df8bae1dSRodney W. Grimes 		 */
1028df8bae1dSRodney W. Grimes 
1029df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1030df8bae1dSRodney W. Grimes 
1031a1f6d91cSDavid Greenman 			backing_object->flags |= OBJ_DEAD;
1032df8bae1dSRodney W. Grimes 			/*
1033df8bae1dSRodney W. Grimes 			 * We can collapse the backing object.
1034df8bae1dSRodney W. Grimes 			 *
10350d94caffSDavid Greenman 			 * Move all in-memory pages from backing_object to the
10360d94caffSDavid Greenman 			 * parent.  Pages that have been paged out will be
10370d94caffSDavid Greenman 			 * overwritten by any of the parent's pages that
10380d94caffSDavid Greenman 			 * shadow them.
1039df8bae1dSRodney W. Grimes 			 */
1040df8bae1dSRodney W. Grimes 
1041b18bfc3dSJohn Dyson 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
104226f9a767SRodney W. Grimes 
1043a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1044df8bae1dSRodney W. Grimes 
1045df8bae1dSRodney W. Grimes 				/*
10460d94caffSDavid Greenman 				 * If the parent has a page here, or if this
10470d94caffSDavid Greenman 				 * page falls outside the parent, dispose of
10480d94caffSDavid Greenman 				 * it.
1049df8bae1dSRodney W. Grimes 				 *
1050df8bae1dSRodney W. Grimes 				 * Otherwise, move it as planned.
1051df8bae1dSRodney W. Grimes 				 */
1052df8bae1dSRodney W. Grimes 
1053a316d390SJohn Dyson 				if (p->pindex < backing_offset_index ||
1054a316d390SJohn Dyson 				    new_pindex >= size) {
1055f919ebdeSDavid Greenman 					vm_page_protect(p, VM_PROT_NONE);
10560d94caffSDavid Greenman 					PAGE_WAKEUP(p);
1057df8bae1dSRodney W. Grimes 					vm_page_free(p);
1058df8bae1dSRodney W. Grimes 				} else {
1059a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
106024a1cce3SDavid Greenman 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
1061a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) {
1062f919ebdeSDavid Greenman 						vm_page_protect(p, VM_PROT_NONE);
10630d94caffSDavid Greenman 						PAGE_WAKEUP(p);
1064df8bae1dSRodney W. Grimes 						vm_page_free(p);
106526f9a767SRodney W. Grimes 					} else {
10666e20a165SJohn Dyson 						vm_page_protect(p, VM_PROT_NONE);
1067a316d390SJohn Dyson 						vm_page_rename(p, object, new_pindex);
10686e20a165SJohn Dyson 						p->dirty = VM_PAGE_BITS_ALL;
1069df8bae1dSRodney W. Grimes 					}
1070df8bae1dSRodney W. Grimes 				}
1071df8bae1dSRodney W. Grimes 			}
1072df8bae1dSRodney W. Grimes 
1073df8bae1dSRodney W. Grimes 			/*
1074df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1075df8bae1dSRodney W. Grimes 			 */
1076df8bae1dSRodney W. Grimes 
107724a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
107826f9a767SRodney W. Grimes 				backing_object->paging_in_progress++;
107924a1cce3SDavid Greenman 				if (object->type == OBJT_SWAP) {
108026f9a767SRodney W. Grimes 					object->paging_in_progress++;
108126f9a767SRodney W. Grimes 					/*
108226f9a767SRodney W. Grimes 					 * copy shadow object pages into ours
10830d94caffSDavid Greenman 					 * and destroy unneeded pages in
10840d94caffSDavid Greenman 					 * shadow object.
108526f9a767SRodney W. Grimes 					 */
108626f9a767SRodney W. Grimes 					swap_pager_copy(
1087a316d390SJohn Dyson 					    backing_object,
1088a316d390SJohn Dyson 					    OFF_TO_IDX(backing_object->paging_offset),
1089a316d390SJohn Dyson 					    object,
1090a316d390SJohn Dyson 					    OFF_TO_IDX(object->paging_offset),
1091a316d390SJohn Dyson 					    OFF_TO_IDX(object->backing_object_offset));
1092f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
109326f9a767SRodney W. Grimes 				} else {
109426f9a767SRodney W. Grimes 					object->paging_in_progress++;
109526f9a767SRodney W. Grimes 					/*
109624a1cce3SDavid Greenman 					 * move the shadow backing_object's pager data to
109724a1cce3SDavid Greenman 					 * "object" and convert "object" type to OBJT_SWAP.
109826f9a767SRodney W. Grimes 					 */
109924a1cce3SDavid Greenman 					object->type = OBJT_SWAP;
11002a4895f4SDavid Greenman 					object->un_pager.swp.swp_nblocks =
11012a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_nblocks;
11022a4895f4SDavid Greenman 					object->un_pager.swp.swp_allocsize =
11032a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_allocsize;
11042a4895f4SDavid Greenman 					object->un_pager.swp.swp_blocks =
11052a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_blocks;
11062a4895f4SDavid Greenman 					object->un_pager.swp.swp_poip =		/* XXX */
11072a4895f4SDavid Greenman 					    backing_object->un_pager.swp.swp_poip;
110826f9a767SRodney W. Grimes 					object->paging_offset = backing_object->paging_offset + backing_offset;
110924a1cce3SDavid Greenman 					TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list);
111024a1cce3SDavid Greenman 
111124a1cce3SDavid Greenman 					/*
111224a1cce3SDavid Greenman 					 * Convert backing object from OBJT_SWAP to
111324a1cce3SDavid Greenman 					 * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is
111424a1cce3SDavid Greenman 					 * actually necessary.
111524a1cce3SDavid Greenman 					 */
111624a1cce3SDavid Greenman 					backing_object->type = OBJT_DEFAULT;
111724a1cce3SDavid Greenman 					TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list);
111826f9a767SRodney W. Grimes 					/*
111926f9a767SRodney W. Grimes 					 * free unnecessary blocks
112026f9a767SRodney W. Grimes 					 */
1121a316d390SJohn Dyson 					swap_pager_freespace(object, 0,
1122a316d390SJohn Dyson 						OFF_TO_IDX(object->paging_offset));
1123f919ebdeSDavid Greenman 					vm_object_pip_wakeup(object);
1124c0503609SDavid Greenman 				}
1125c0503609SDavid Greenman 
1126f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1127c0503609SDavid Greenman 			}
1128df8bae1dSRodney W. Grimes 			/*
1129df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
113024a1cce3SDavid Greenman 			 * Note that the reference to backing_object->backing_object
1131df8bae1dSRodney W. Grimes 			 * moves from within backing_object to within object.
1132df8bae1dSRodney W. Grimes 			 */
1133df8bae1dSRodney W. Grimes 
113424a1cce3SDavid Greenman 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
113524a1cce3SDavid Greenman 			    shadow_list);
1136de5f6a77SJohn Dyson 			--object->backing_object->shadow_count;
1137de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
113824a1cce3SDavid Greenman 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
113924a1cce3SDavid Greenman 				    backing_object, shadow_list);
1140de5f6a77SJohn Dyson 				--backing_object->backing_object->shadow_count;
1141de5f6a77SJohn Dyson 			}
114224a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1143de5f6a77SJohn Dyson 			if (object->backing_object) {
114424a1cce3SDavid Greenman 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
114524a1cce3SDavid Greenman 				    object, shadow_list);
1146de5f6a77SJohn Dyson 				++object->backing_object->shadow_count;
1147de5f6a77SJohn Dyson 			}
11482fe6e4d7SDavid Greenman 
114924a1cce3SDavid Greenman 			object->backing_object_offset += backing_object->backing_object_offset;
1150df8bae1dSRodney W. Grimes 			/*
1151df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1152df8bae1dSRodney W. Grimes 			 *
11530d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
11540d94caffSDavid Greenman 			 * and no object references within it, all that is
11550d94caffSDavid Greenman 			 * necessary is to dispose of it.
1156df8bae1dSRodney W. Grimes 			 */
1157df8bae1dSRodney W. Grimes 
1158df8bae1dSRodney W. Grimes 			TAILQ_REMOVE(&vm_object_list, backing_object,
1159df8bae1dSRodney W. Grimes 			    object_list);
1160df8bae1dSRodney W. Grimes 			vm_object_count--;
1161df8bae1dSRodney W. Grimes 
116299448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes 			object_collapses++;
11650d94caffSDavid Greenman 		} else {
116695e5e988SJohn Dyson 			vm_object_t new_backing_object;
1167df8bae1dSRodney W. Grimes 			/*
1168df8bae1dSRodney W. Grimes 			 * If all of the pages in the backing object are
11690d94caffSDavid Greenman 			 * shadowed by the parent object, the parent object no
11700d94caffSDavid Greenman 			 * longer has to shadow the backing object; it can
11710d94caffSDavid Greenman 			 * shadow the next one in the chain.
1172df8bae1dSRodney W. Grimes 			 *
11730d94caffSDavid Greenman 			 * The backing object must not be paged out - we'd have
11740d94caffSDavid Greenman 			 * to check all of the paged-out pages, as well.
1175df8bae1dSRodney W. Grimes 			 */
1176df8bae1dSRodney W. Grimes 
117724a1cce3SDavid Greenman 			if (backing_object->type != OBJT_DEFAULT) {
1178df8bae1dSRodney W. Grimes 				return;
1179df8bae1dSRodney W. Grimes 			}
1180df8bae1dSRodney W. Grimes 			/*
11810d94caffSDavid Greenman 			 * Should have a check for a 'small' number of pages
11820d94caffSDavid Greenman 			 * here.
1183df8bae1dSRodney W. Grimes 			 */
1184df8bae1dSRodney W. Grimes 
1185b18bfc3dSJohn Dyson 			for (p = TAILQ_FIRST(&backing_object->memq); p; p = TAILQ_NEXT(p, listq)) {
1186a316d390SJohn Dyson 				new_pindex = p->pindex - backing_offset_index;
1187df8bae1dSRodney W. Grimes 
1188df8bae1dSRodney W. Grimes 				/*
11890d94caffSDavid Greenman 				 * If the parent has a page here, or if this
11900d94caffSDavid Greenman 				 * page falls outside the parent, keep going.
1191df8bae1dSRodney W. Grimes 				 *
11920d94caffSDavid Greenman 				 * Otherwise, the backing_object must be left in
11930d94caffSDavid Greenman 				 * the chain.
1194df8bae1dSRodney W. Grimes 				 */
1195df8bae1dSRodney W. Grimes 
1196a316d390SJohn Dyson 				if (p->pindex >= backing_offset_index &&
1197a316d390SJohn Dyson 					new_pindex <= size) {
119824a1cce3SDavid Greenman 
1199a316d390SJohn Dyson 					pp = vm_page_lookup(object, new_pindex);
120024a1cce3SDavid Greenman 
120124a1cce3SDavid Greenman 					if ((pp == NULL || pp->valid == 0) &&
1202a316d390SJohn Dyson 				   	    !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) {
1203df8bae1dSRodney W. Grimes 						/*
12040d94caffSDavid Greenman 						 * Page still needed. Can't go any
12050d94caffSDavid Greenman 						 * further.
1206df8bae1dSRodney W. Grimes 						 */
1207df8bae1dSRodney W. Grimes 						return;
1208df8bae1dSRodney W. Grimes 					}
1209df8bae1dSRodney W. Grimes 				}
121024a1cce3SDavid Greenman 			}
1211df8bae1dSRodney W. Grimes 
1212df8bae1dSRodney W. Grimes 			/*
12130d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
12140d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
12150d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1216df8bae1dSRodney W. Grimes 			 */
1217df8bae1dSRodney W. Grimes 
121895e5e988SJohn Dyson 			TAILQ_REMOVE(&backing_object->shadow_head,
121924a1cce3SDavid Greenman 			    object, shadow_list);
122095e5e988SJohn Dyson 			--backing_object->shadow_count;
122195e5e988SJohn Dyson 
122295e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
122395e5e988SJohn Dyson 			if (object->backing_object = new_backing_object) {
122495e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
122595e5e988SJohn Dyson 				TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
122624a1cce3SDavid Greenman 				    object, shadow_list);
122795e5e988SJohn Dyson 				++new_backing_object->shadow_count;
122895e5e988SJohn Dyson 				object->backing_object_offset +=
122995e5e988SJohn Dyson 					backing_object->backing_object_offset;
1230de5f6a77SJohn Dyson 			}
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes 			/*
12330d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
12340d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
12350d94caffSDavid Greenman 			 * so we don't need to call vm_object_deallocate.
1236df8bae1dSRodney W. Grimes 			 */
123795e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 			object_bypasses++;
1240df8bae1dSRodney W. Grimes 
1241df8bae1dSRodney W. Grimes 		}
1242df8bae1dSRodney W. Grimes 
1243df8bae1dSRodney W. Grimes 		/*
1244df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1245df8bae1dSRodney W. Grimes 		 */
1246df8bae1dSRodney W. Grimes 	}
1247df8bae1dSRodney W. Grimes }
1248df8bae1dSRodney W. Grimes 
1249df8bae1dSRodney W. Grimes /*
1250df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1251df8bae1dSRodney W. Grimes  *
1252df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1253df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1254df8bae1dSRodney W. Grimes  *
1255df8bae1dSRodney W. Grimes  *	The object must be locked.
1256df8bae1dSRodney W. Grimes  */
125726f9a767SRodney W. Grimes void
12587c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1259df8bae1dSRodney W. Grimes 	register vm_object_t object;
1260a316d390SJohn Dyson 	register vm_pindex_t start;
1261a316d390SJohn Dyson 	register vm_pindex_t end;
12627c1f6cedSDavid Greenman 	boolean_t clean_only;
1263df8bae1dSRodney W. Grimes {
1264df8bae1dSRodney W. Grimes 	register vm_page_t p, next;
1265a316d390SJohn Dyson 	unsigned int size;
126695e5e988SJohn Dyson 	int s, all;
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes 	if (object == NULL)
1269df8bae1dSRodney W. Grimes 		return;
1270df8bae1dSRodney W. Grimes 
127195e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
127295e5e988SJohn Dyson 
12732fe6e4d7SDavid Greenman 	object->paging_in_progress++;
127426f9a767SRodney W. Grimes again:
127526f9a767SRodney W. Grimes 	size = end - start;
127695e5e988SJohn Dyson 	if (all || size > 4 || size >= object->size / 4) {
1277b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1278b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
127995e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1280bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1281bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1282bd7e5f99SJohn Dyson 					p->valid = 0;
12830d94caffSDavid Greenman 					continue;
12840d94caffSDavid Greenman 				}
12850891ef4cSJohn Dyson 
1286b18bfc3dSJohn Dyson 				/*
1287b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1288b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1289b18bfc3dSJohn Dyson 				 */
1290b18bfc3dSJohn Dyson 				if ((p->flags & PG_BUSY) || p->busy) {
1291b18bfc3dSJohn Dyson 					s = splvm();
12920d94caffSDavid Greenman 					if ((p->flags & PG_BUSY) || p->busy) {
129326f9a767SRodney W. Grimes 						p->flags |= PG_WANTED;
129424a1cce3SDavid Greenman 						tsleep(p, PVM, "vmopar", 0);
1295a481f200SDavid Greenman 						splx(s);
129626f9a767SRodney W. Grimes 						goto again;
129726f9a767SRodney W. Grimes 					}
1298a481f200SDavid Greenman 					splx(s);
1299b18bfc3dSJohn Dyson 				}
13000891ef4cSJohn Dyson 
13017c1f6cedSDavid Greenman 				if (clean_only) {
13027c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
13037c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
13047c1f6cedSDavid Greenman 						continue;
13057c1f6cedSDavid Greenman 				}
1306f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
13070d94caffSDavid Greenman 				PAGE_WAKEUP(p);
1308df8bae1dSRodney W. Grimes 				vm_page_free(p);
130926f9a767SRodney W. Grimes 			}
131026f9a767SRodney W. Grimes 		}
131126f9a767SRodney W. Grimes 	} else {
131226f9a767SRodney W. Grimes 		while (size > 0) {
1313bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1314bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1315bd7e5f99SJohn Dyson 					p->valid = 0;
1316bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1317bd7e5f99SJohn Dyson 					start += 1;
1318bd7e5f99SJohn Dyson 					size -= 1;
1319bd7e5f99SJohn Dyson 					continue;
13200d94caffSDavid Greenman 				}
1321b18bfc3dSJohn Dyson 				/*
1322b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1323b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1324b18bfc3dSJohn Dyson 				 */
1325b18bfc3dSJohn Dyson 				if ((p->flags & PG_BUSY) || p->busy) {
1326b18bfc3dSJohn Dyson 					s = splvm();
13270d94caffSDavid Greenman 					if ((p->flags & PG_BUSY) || p->busy) {
132826f9a767SRodney W. Grimes 						p->flags |= PG_WANTED;
132924a1cce3SDavid Greenman 						tsleep(p, PVM, "vmopar", 0);
1330a481f200SDavid Greenman 						splx(s);
133126f9a767SRodney W. Grimes 						goto again;
133226f9a767SRodney W. Grimes 					}
1333a481f200SDavid Greenman 					splx(s);
1334b18bfc3dSJohn Dyson 				}
13357c1f6cedSDavid Greenman 				if (clean_only) {
13367c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1337bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1338bd7e5f99SJohn Dyson 						start += 1;
1339bd7e5f99SJohn Dyson 						size -= 1;
13407c1f6cedSDavid Greenman 						continue;
13417c1f6cedSDavid Greenman 					}
1342bd7e5f99SJohn Dyson 				}
1343f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
13440d94caffSDavid Greenman 				PAGE_WAKEUP(p);
134526f9a767SRodney W. Grimes 				vm_page_free(p);
134626f9a767SRodney W. Grimes 			}
1347a316d390SJohn Dyson 			start += 1;
1348a316d390SJohn Dyson 			size -= 1;
1349df8bae1dSRodney W. Grimes 		}
1350df8bae1dSRodney W. Grimes 	}
1351f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1352c0503609SDavid Greenman }
1353df8bae1dSRodney W. Grimes 
1354df8bae1dSRodney W. Grimes /*
1355df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1356df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1357df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1358df8bae1dSRodney W. Grimes  *
1359df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1360df8bae1dSRodney W. Grimes  *
1361df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1362df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1363df8bae1dSRodney W. Grimes  *
1364df8bae1dSRodney W. Grimes  *	Parameters:
1365df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1366df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1367df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1368df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1369df8bae1dSRodney W. Grimes  *
1370df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1371df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1372df8bae1dSRodney W. Grimes  *
1373df8bae1dSRodney W. Grimes  *	Conditions:
1374df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1375df8bae1dSRodney W. Grimes  */
13760d94caffSDavid Greenman boolean_t
1377a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1378df8bae1dSRodney W. Grimes 	register vm_object_t prev_object;
1379a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1380df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1381df8bae1dSRodney W. Grimes {
1382df8bae1dSRodney W. Grimes 	vm_size_t newsize;
1383df8bae1dSRodney W. Grimes 
1384df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1385df8bae1dSRodney W. Grimes 		return (TRUE);
1386df8bae1dSRodney W. Grimes 	}
1387df8bae1dSRodney W. Grimes 
138830dcfc09SJohn Dyson 	if (prev_object->type != OBJT_DEFAULT) {
138930dcfc09SJohn Dyson 		return (FALSE);
139030dcfc09SJohn Dyson 	}
139130dcfc09SJohn Dyson 
1392df8bae1dSRodney W. Grimes 	/*
1393df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1394df8bae1dSRodney W. Grimes 	 */
1395df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1396df8bae1dSRodney W. Grimes 
1397df8bae1dSRodney W. Grimes 	/*
13980d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
13990d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
14000d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1401df8bae1dSRodney W. Grimes 	 */
1402df8bae1dSRodney W. Grimes 
14038cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1404df8bae1dSRodney W. Grimes 		return (FALSE);
1405df8bae1dSRodney W. Grimes 	}
1406a316d390SJohn Dyson 
1407a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1408a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
14098cc7e047SJohn Dyson 
14108cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
14118cc7e047SJohn Dyson 	    (prev_object->size != prev_pindex + prev_size)) {
14128cc7e047SJohn Dyson 		return (FALSE);
14138cc7e047SJohn Dyson 	}
14148cc7e047SJohn Dyson 
1415df8bae1dSRodney W. Grimes 	/*
14160d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
14170d94caffSDavid Greenman 	 * deallocation.
1418df8bae1dSRodney W. Grimes 	 */
1419df8bae1dSRodney W. Grimes 
1420df8bae1dSRodney W. Grimes 	vm_object_page_remove(prev_object,
1421a316d390SJohn Dyson 	    prev_pindex + prev_size,
1422a316d390SJohn Dyson 	    prev_pindex + prev_size + next_size, FALSE);
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes 	/*
1425df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1426df8bae1dSRodney W. Grimes 	 */
1427a316d390SJohn Dyson 	newsize = prev_pindex + prev_size + next_size;
1428df8bae1dSRodney W. Grimes 	if (newsize > prev_object->size)
1429df8bae1dSRodney W. Grimes 		prev_object->size = newsize;
1430df8bae1dSRodney W. Grimes 
1431df8bae1dSRodney W. Grimes 	return (TRUE);
1432df8bae1dSRodney W. Grimes }
1433df8bae1dSRodney W. Grimes 
1434c7c34a24SBruce Evans #include "opt_ddb.h"
1435c3cb3e12SDavid Greenman #ifdef DDB
1436c7c34a24SBruce Evans #include <sys/kernel.h>
1437c7c34a24SBruce Evans 
1438c7c34a24SBruce Evans #include <machine/cons.h>
1439c7c34a24SBruce Evans 
1440c7c34a24SBruce Evans #include <ddb/ddb.h>
1441c7c34a24SBruce Evans 
1442c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1443c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1444c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1445c3cb3e12SDavid Greenman 
1446cac597e4SBruce Evans static int
1447a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1448a1f6d91cSDavid Greenman 	vm_map_t map;
1449a1f6d91cSDavid Greenman 	vm_object_t object;
1450a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1451a1f6d91cSDavid Greenman {
1452a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1453a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1454a1f6d91cSDavid Greenman 	vm_object_t obj;
1455a1f6d91cSDavid Greenman 	int entcount;
1456a1f6d91cSDavid Greenman 
1457a1f6d91cSDavid Greenman 	if (map == 0)
1458a1f6d91cSDavid Greenman 		return 0;
1459a1f6d91cSDavid Greenman 
1460a1f6d91cSDavid Greenman 	if (entry == 0) {
1461a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1462a1f6d91cSDavid Greenman 		entcount = map->nentries;
1463a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1464a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1465a1f6d91cSDavid Greenman 				return 1;
1466a1f6d91cSDavid Greenman 			}
1467a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1468a1f6d91cSDavid Greenman 		}
1469afa07f7eSJohn Dyson 	} else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1470a1f6d91cSDavid Greenman 		tmpm = entry->object.share_map;
1471a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1472a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1473a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1474a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1475a1f6d91cSDavid Greenman 				return 1;
1476a1f6d91cSDavid Greenman 			}
1477a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1478a1f6d91cSDavid Greenman 		}
1479a1f6d91cSDavid Greenman 	} else if (obj = entry->object.vm_object) {
148024a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1481a1f6d91cSDavid Greenman 			if( obj == object) {
1482a1f6d91cSDavid Greenman 				return 1;
1483a1f6d91cSDavid Greenman 			}
1484a1f6d91cSDavid Greenman 	}
1485a1f6d91cSDavid Greenman 	return 0;
1486a1f6d91cSDavid Greenman }
1487a1f6d91cSDavid Greenman 
1488cac597e4SBruce Evans static int
1489a1f6d91cSDavid Greenman vm_object_in_map( object)
1490a1f6d91cSDavid Greenman 	vm_object_t object;
1491a1f6d91cSDavid Greenman {
1492a1f6d91cSDavid Greenman 	struct proc *p;
14931b67ec6dSJeffrey Hsu 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1494a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1495a1f6d91cSDavid Greenman 			continue;
1496a1f6d91cSDavid Greenman 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1497a1f6d91cSDavid Greenman 			return 1;
1498a1f6d91cSDavid Greenman 	}
1499a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1500a1f6d91cSDavid Greenman 		return 1;
1501a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1502a1f6d91cSDavid Greenman 		return 1;
1503a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1504a1f6d91cSDavid Greenman 		return 1;
1505a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1506a1f6d91cSDavid Greenman 		return 1;
1507a1f6d91cSDavid Greenman 	if( _vm_object_in_map( io_map, object, 0))
1508a1f6d91cSDavid Greenman 		return 1;
1509a1f6d91cSDavid Greenman 	if( _vm_object_in_map( phys_map, object, 0))
1510a1f6d91cSDavid Greenman 		return 1;
1511a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1512a1f6d91cSDavid Greenman 		return 1;
1513a1f6d91cSDavid Greenman 	if( _vm_object_in_map( u_map, object, 0))
1514a1f6d91cSDavid Greenman 		return 1;
1515a1f6d91cSDavid Greenman 	return 0;
1516a1f6d91cSDavid Greenman }
1517a1f6d91cSDavid Greenman 
1518c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1519f708ef1bSPoul-Henning Kamp {
1520a1f6d91cSDavid Greenman 	vm_object_t object;
1521a1f6d91cSDavid Greenman 
1522a1f6d91cSDavid Greenman 	/*
1523a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1524a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1525a1f6d91cSDavid Greenman 	 */
1526b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1527a1f6d91cSDavid Greenman 			object != NULL;
1528b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
152924a1cce3SDavid Greenman 		if (object->handle == NULL &&
153024a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1531a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
1532c7c34a24SBruce Evans 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1533a1f6d91cSDavid Greenman 					object->size);
1534a1f6d91cSDavid Greenman 			}
1535a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1536c7c34a24SBruce Evans 				db_printf("vmochk: internal obj is not in a map: "
15373af76890SPoul-Henning Kamp 		"ref: %d, size: %d: 0x%x, backing_object: 0x%x\n",
15383af76890SPoul-Henning Kamp 				    object->ref_count, object->size,
15393af76890SPoul-Henning Kamp 				    object->size, object->backing_object);
1540a1f6d91cSDavid Greenman 			}
1541a1f6d91cSDavid Greenman 		}
1542a1f6d91cSDavid Greenman 	}
1543a1f6d91cSDavid Greenman }
1544a1f6d91cSDavid Greenman 
154526f9a767SRodney W. Grimes /*
1546df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1547df8bae1dSRodney W. Grimes  */
1548c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1549df8bae1dSRodney W. Grimes {
1550c7c34a24SBruce Evans 	/* XXX convert args. */
1551c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1552c7c34a24SBruce Evans 	boolean_t full = have_addr;
1553c7c34a24SBruce Evans 
1554df8bae1dSRodney W. Grimes 	register vm_page_t p;
1555df8bae1dSRodney W. Grimes 
1556c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1557c7c34a24SBruce Evans #define	count	was_count
1558c7c34a24SBruce Evans 
1559df8bae1dSRodney W. Grimes 	register int count;
1560df8bae1dSRodney W. Grimes 
1561df8bae1dSRodney W. Grimes 	if (object == NULL)
1562df8bae1dSRodney W. Grimes 		return;
1563df8bae1dSRodney W. Grimes 
156495e5e988SJohn Dyson 	db_iprintf("Object 0x%x: type=%d, size=0x%x, res=%d, ref=%d, flags=0x%x\n",
156595e5e988SJohn Dyson 	    (int) object, (int) object->type, (int) object->size,
156695e5e988SJohn Dyson 	    object->resident_page_count,
156795e5e988SJohn Dyson 		object->ref_count,
156895e5e988SJohn Dyson 		object->flags);
156995e5e988SJohn Dyson 	db_iprintf(" sref=%d, offset=0x%x, backing_object(%d)=(0x%x)+0x%x\n",
157095e5e988SJohn Dyson 		object->shadow_count,
15712a4895f4SDavid Greenman 	    (int) object->paging_offset,
157295e5e988SJohn Dyson 		(((int)object->backing_object)?object->backing_object->ref_count:0),
157395e5e988SJohn Dyson 	    (int) object->backing_object,
157495e5e988SJohn Dyson 		(int) object->backing_object_offset);
1575df8bae1dSRodney W. Grimes 
1576df8bae1dSRodney W. Grimes 	if (!full)
1577df8bae1dSRodney W. Grimes 		return;
1578df8bae1dSRodney W. Grimes 
1579c7c34a24SBruce Evans 	db_indent += 2;
1580df8bae1dSRodney W. Grimes 	count = 0;
1581b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1582df8bae1dSRodney W. Grimes 		if (count == 0)
1583c7c34a24SBruce Evans 			db_iprintf("memory:=");
1584df8bae1dSRodney W. Grimes 		else if (count == 6) {
1585c7c34a24SBruce Evans 			db_printf("\n");
1586c7c34a24SBruce Evans 			db_iprintf(" ...");
1587df8bae1dSRodney W. Grimes 			count = 0;
1588df8bae1dSRodney W. Grimes 		} else
1589c7c34a24SBruce Evans 			db_printf(",");
1590df8bae1dSRodney W. Grimes 		count++;
1591df8bae1dSRodney W. Grimes 
1592c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1593a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1594df8bae1dSRodney W. Grimes 	}
1595df8bae1dSRodney W. Grimes 	if (count != 0)
1596c7c34a24SBruce Evans 		db_printf("\n");
1597c7c34a24SBruce Evans 	db_indent -= 2;
1598df8bae1dSRodney W. Grimes }
15995070c7f8SJohn Dyson 
1600c7c34a24SBruce Evans /* XXX. */
1601c7c34a24SBruce Evans #undef count
1602c7c34a24SBruce Evans 
1603c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
16045070c7f8SJohn Dyson void
1605c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1606c7c34a24SBruce Evans 	db_expr_t addr;
1607c7c34a24SBruce Evans 	boolean_t have_addr;
1608c7c34a24SBruce Evans 	db_expr_t count;
1609c7c34a24SBruce Evans 	char *modif;
1610c7c34a24SBruce Evans {
1611c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1612c7c34a24SBruce Evans }
1613c7c34a24SBruce Evans 
1614c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
16155070c7f8SJohn Dyson {
16165070c7f8SJohn Dyson 	vm_object_t object;
16175070c7f8SJohn Dyson 	int nl = 0;
16185070c7f8SJohn Dyson 	int c;
16195070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
16205070c7f8SJohn Dyson 			object != NULL;
16215070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
16225070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
16235070c7f8SJohn Dyson 		vm_pindex_t osize;
16245070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
16255070c7f8SJohn Dyson 		int rcount;
16265070c7f8SJohn Dyson 		vm_page_t m;
16275070c7f8SJohn Dyson 
16285070c7f8SJohn Dyson 		db_printf("new object: 0x%x\n", object);
16295070c7f8SJohn Dyson 		if ( nl > 18) {
16305070c7f8SJohn Dyson 			c = cngetc();
16315070c7f8SJohn Dyson 			if (c != ' ')
16325070c7f8SJohn Dyson 				return;
16335070c7f8SJohn Dyson 			nl = 0;
16345070c7f8SJohn Dyson 		}
16355070c7f8SJohn Dyson 		nl++;
16365070c7f8SJohn Dyson 		rcount = 0;
16375070c7f8SJohn Dyson 		fidx = 0;
16385070c7f8SJohn Dyson 		osize = object->size;
16395070c7f8SJohn Dyson 		if (osize > 128)
16405070c7f8SJohn Dyson 			osize = 128;
16415070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
16425070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
16435070c7f8SJohn Dyson 			if (m == NULL) {
16445070c7f8SJohn Dyson 				if (rcount) {
16455070c7f8SJohn Dyson 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
16465070c7f8SJohn Dyson 						fidx, rcount, pa);
16475070c7f8SJohn Dyson 					if ( nl > 18) {
16485070c7f8SJohn Dyson 						c = cngetc();
16495070c7f8SJohn Dyson 						if (c != ' ')
16505070c7f8SJohn Dyson 							return;
16515070c7f8SJohn Dyson 						nl = 0;
16525070c7f8SJohn Dyson 					}
16535070c7f8SJohn Dyson 					nl++;
16545070c7f8SJohn Dyson 					rcount = 0;
16555070c7f8SJohn Dyson 				}
16565070c7f8SJohn Dyson 				continue;
16575070c7f8SJohn Dyson 			}
16585070c7f8SJohn Dyson 
16595070c7f8SJohn Dyson 
16605070c7f8SJohn Dyson 			if (rcount &&
16615070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
16625070c7f8SJohn Dyson 				++rcount;
16635070c7f8SJohn Dyson 				continue;
16645070c7f8SJohn Dyson 			}
16655070c7f8SJohn Dyson 			if (rcount) {
16665070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
16675070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
16685070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
16695070c7f8SJohn Dyson 				if (padiff == 0) {
16705070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
16715070c7f8SJohn Dyson 					++rcount;
16725070c7f8SJohn Dyson 					continue;
16735070c7f8SJohn Dyson 				}
16745070c7f8SJohn Dyson 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
16755070c7f8SJohn Dyson 				db_printf("pd(%d)\n", padiff);
16765070c7f8SJohn Dyson 				if ( nl > 18) {
16775070c7f8SJohn Dyson 					c = cngetc();
16785070c7f8SJohn Dyson 					if (c != ' ')
16795070c7f8SJohn Dyson 						return;
16805070c7f8SJohn Dyson 					nl = 0;
16815070c7f8SJohn Dyson 				}
16825070c7f8SJohn Dyson 				nl++;
16835070c7f8SJohn Dyson 			}
16845070c7f8SJohn Dyson 			fidx = idx;
16855070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
16865070c7f8SJohn Dyson 			rcount = 1;
16875070c7f8SJohn Dyson 		}
16885070c7f8SJohn Dyson 		if (rcount) {
16895070c7f8SJohn Dyson 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
16905070c7f8SJohn Dyson 			if ( nl > 18) {
16915070c7f8SJohn Dyson 				c = cngetc();
16925070c7f8SJohn Dyson 				if (c != ' ')
16935070c7f8SJohn Dyson 					return;
16945070c7f8SJohn Dyson 				nl = 0;
16955070c7f8SJohn Dyson 			}
16965070c7f8SJohn Dyson 			nl++;
16975070c7f8SJohn Dyson 		}
16985070c7f8SJohn Dyson 	}
16995070c7f8SJohn Dyson }
1700c3cb3e12SDavid Greenman #endif /* DDB */
1701