xref: /freebsd/sys/vm/vm_object.c (revision fc2ffbe6040d6630a06229c5c9be77601fb635eb)
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
175929bcfaSPhilippe Charnier  *    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  *
64c3aac50fSPeter Wemm  * $FreeBSD$
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>
77cf2819ccSJohn Dyson #include <sys/mount.h>
781b367556SJason Evans #include <sys/mutex.h>
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes #include <vm/vm.h>
81efeaf95aSDavid Greenman #include <vm/vm_param.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>
8821cd6e62SSeigo Tanimura #include <vm/vm_zone.h>
8905f0fdd2SPoul-Henning Kamp #include <vm/swap_pager.h>
90a1f6d91cSDavid Greenman #include <vm/vm_kern.h>
91efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9226f9a767SRodney W. Grimes 
93cac597e4SBruce Evans static void	vm_object_qcollapse __P((vm_object_t object));
94f6b04d2bSDavid Greenman 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  *	Virtual memory objects maintain the actual data
97df8bae1dSRodney W. Grimes  *	associated with allocated virtual memory.  A given
98df8bae1dSRodney W. Grimes  *	page of memory exists within exactly one object.
99df8bae1dSRodney W. Grimes  *
100df8bae1dSRodney W. Grimes  *	An object is only deallocated when all "references"
101df8bae1dSRodney W. Grimes  *	are given up.  Only one "reference" to a given
102df8bae1dSRodney W. Grimes  *	region of an object should be writeable.
103df8bae1dSRodney W. Grimes  *
104df8bae1dSRodney W. Grimes  *	Associated with each object is a list of all resident
105df8bae1dSRodney W. Grimes  *	memory pages belonging to that object; this list is
106df8bae1dSRodney W. Grimes  *	maintained by the "vm_page" module, and locked by the object's
107df8bae1dSRodney W. Grimes  *	lock.
108df8bae1dSRodney W. Grimes  *
109df8bae1dSRodney W. Grimes  *	Each object also records a "pager" routine which is
110df8bae1dSRodney W. Grimes  *	used to retrieve (and store) pages to the proper backing
111df8bae1dSRodney W. Grimes  *	storage.  In addition, objects may be backed by other
112df8bae1dSRodney W. Grimes  *	objects from which they were virtual-copied.
113df8bae1dSRodney W. Grimes  *
114df8bae1dSRodney W. Grimes  *	The only items within the object structure which are
115df8bae1dSRodney W. Grimes  *	modified after time of creation are:
116df8bae1dSRodney W. Grimes  *		reference count		locked by object's lock
117df8bae1dSRodney W. Grimes  *		pager routine		locked by object's lock
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  */
120df8bae1dSRodney W. Grimes 
12128f8db14SBruce Evans struct object_q vm_object_list;
1221b367556SJason Evans static struct mtx vm_object_list_mtx;
1234de628deSBruce Evans static long vm_object_count;		/* count of all objects */
12428f8db14SBruce Evans vm_object_t kernel_object;
12528f8db14SBruce Evans vm_object_t kmem_object;
126f708ef1bSPoul-Henning Kamp static struct vm_object kernel_object_store;
127f708ef1bSPoul-Henning Kamp static struct vm_object kmem_object_store;
128aef922f5SJohn Dyson extern int vm_pageout_page_count;
129df8bae1dSRodney W. Grimes 
130f708ef1bSPoul-Henning Kamp static long object_collapses;
131f708ef1bSPoul-Henning Kamp static long object_bypasses;
1325070c7f8SJohn Dyson static int next_index;
13399448ed1SJohn Dyson static vm_zone_t obj_zone;
13499448ed1SJohn Dyson static struct vm_zone obj_zone_store;
1351c7c3c6aSMatthew Dillon static int object_hash_rand;
13699448ed1SJohn Dyson #define VM_OBJECTS_INIT 256
137303b270bSEivind Eklund static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
138df8bae1dSRodney W. Grimes 
1393075778bSJohn Dyson void
14024a1cce3SDavid Greenman _vm_object_allocate(type, size, object)
14124a1cce3SDavid Greenman 	objtype_t type;
142df8bae1dSRodney W. Grimes 	vm_size_t size;
143d031cff1SMatthew Dillon 	vm_object_t object;
144df8bae1dSRodney W. Grimes {
14599448ed1SJohn Dyson 	int incr;
146df8bae1dSRodney W. Grimes 	TAILQ_INIT(&object->memq);
14724a1cce3SDavid Greenman 	TAILQ_INIT(&object->shadow_head);
148a1f6d91cSDavid Greenman 
14924a1cce3SDavid Greenman 	object->type = type;
150df8bae1dSRodney W. Grimes 	object->size = size;
151a1f6d91cSDavid Greenman 	object->ref_count = 1;
15224a1cce3SDavid Greenman 	object->flags = 0;
153c0877f10SJohn Dyson 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
154069e9bc1SDoug Rabson 		vm_object_set_flag(object, OBJ_ONEMAPPING);
155df8bae1dSRodney W. Grimes 	object->paging_in_progress = 0;
156a1f6d91cSDavid Greenman 	object->resident_page_count = 0;
157de5f6a77SJohn Dyson 	object->shadow_count = 0;
1585070c7f8SJohn Dyson 	object->pg_color = next_index;
15999448ed1SJohn Dyson 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
16099448ed1SJohn Dyson 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
16199448ed1SJohn Dyson 	else
16299448ed1SJohn Dyson 		incr = size;
16399448ed1SJohn Dyson 	next_index = (next_index + incr) & PQ_L2_MASK;
16424a1cce3SDavid Greenman 	object->handle = NULL;
16524a1cce3SDavid Greenman 	object->backing_object = NULL;
166a316d390SJohn Dyson 	object->backing_object_offset = (vm_ooffset_t) 0;
1671c7c3c6aSMatthew Dillon 	/*
1681c7c3c6aSMatthew Dillon 	 * Try to generate a number that will spread objects out in the
1691c7c3c6aSMatthew Dillon 	 * hash table.  We 'wipe' new objects across the hash in 128 page
1701c7c3c6aSMatthew Dillon 	 * increments plus 1 more to offset it a little more by the time
1711c7c3c6aSMatthew Dillon 	 * it wraps around.
1721c7c3c6aSMatthew Dillon 	 */
1731c7c3c6aSMatthew Dillon 	object->hash_rand = object_hash_rand - 129;
174a1f6d91cSDavid Greenman 
1752d8acc0fSJohn Dyson 	object->generation++;
176df8bae1dSRodney W. Grimes 
177df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
178df8bae1dSRodney W. Grimes 	vm_object_count++;
1791c7c3c6aSMatthew Dillon 	object_hash_rand = object->hash_rand;
180df8bae1dSRodney W. Grimes }
181df8bae1dSRodney W. Grimes 
182df8bae1dSRodney W. Grimes /*
18326f9a767SRodney W. Grimes  *	vm_object_init:
18426f9a767SRodney W. Grimes  *
18526f9a767SRodney W. Grimes  *	Initialize the VM objects module.
18626f9a767SRodney W. Grimes  */
18726f9a767SRodney W. Grimes void
188a316d390SJohn Dyson vm_object_init()
18926f9a767SRodney W. Grimes {
19026f9a767SRodney W. Grimes 	TAILQ_INIT(&vm_object_list);
1911b367556SJason Evans 	mtx_init(&vm_object_list_mtx, "vm object_list", MTX_DEF);
19226f9a767SRodney W. Grimes 	vm_object_count = 0;
1930217125fSDavid Greenman 
19426f9a767SRodney W. Grimes 	kernel_object = &kernel_object_store;
195a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
19626f9a767SRodney W. Grimes 	    kernel_object);
19726f9a767SRodney W. Grimes 
19826f9a767SRodney W. Grimes 	kmem_object = &kmem_object_store;
199a316d390SJohn Dyson 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
20026f9a767SRodney W. Grimes 	    kmem_object);
20199448ed1SJohn Dyson 
20299448ed1SJohn Dyson 	obj_zone = &obj_zone_store;
20399448ed1SJohn Dyson 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
20499448ed1SJohn Dyson 		vm_objects_init, VM_OBJECTS_INIT);
20599448ed1SJohn Dyson }
20699448ed1SJohn Dyson 
20799448ed1SJohn Dyson void
20899448ed1SJohn Dyson vm_object_init2() {
2090a80f406SJohn Dyson 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
21026f9a767SRodney W. Grimes }
21126f9a767SRodney W. Grimes 
21226f9a767SRodney W. Grimes /*
21326f9a767SRodney W. Grimes  *	vm_object_allocate:
21426f9a767SRodney W. Grimes  *
21526f9a767SRodney W. Grimes  *	Returns a new object with the given size.
21626f9a767SRodney W. Grimes  */
21726f9a767SRodney W. Grimes 
21826f9a767SRodney W. Grimes vm_object_t
21924a1cce3SDavid Greenman vm_object_allocate(type, size)
22024a1cce3SDavid Greenman 	objtype_t type;
22126f9a767SRodney W. Grimes 	vm_size_t size;
22226f9a767SRodney W. Grimes {
223d031cff1SMatthew Dillon 	vm_object_t result;
224d031cff1SMatthew Dillon 
22599448ed1SJohn Dyson 	result = (vm_object_t) zalloc(obj_zone);
22667bf6868SJohn Dyson 
22724a1cce3SDavid Greenman 	_vm_object_allocate(type, size, result);
22826f9a767SRodney W. Grimes 
22926f9a767SRodney W. Grimes 	return (result);
23026f9a767SRodney W. Grimes }
23126f9a767SRodney W. Grimes 
23226f9a767SRodney W. Grimes 
23326f9a767SRodney W. Grimes /*
234df8bae1dSRodney W. Grimes  *	vm_object_reference:
235df8bae1dSRodney W. Grimes  *
236df8bae1dSRodney W. Grimes  *	Gets another reference to the given object.
237df8bae1dSRodney W. Grimes  */
2386476c0d2SJohn Dyson void
23926f9a767SRodney W. Grimes vm_object_reference(object)
240d031cff1SMatthew Dillon 	vm_object_t object;
241df8bae1dSRodney W. Grimes {
242df8bae1dSRodney W. Grimes 	if (object == NULL)
243df8bae1dSRodney W. Grimes 		return;
24495e5e988SJohn Dyson 
2455526d2d9SEivind Eklund 	KASSERT(!(object->flags & OBJ_DEAD),
2465526d2d9SEivind Eklund 	    ("vm_object_reference: attempting to reference dead obj"));
24795e5e988SJohn Dyson 
248df8bae1dSRodney W. Grimes 	object->ref_count++;
24947221757SJohn Dyson 	if (object->type == OBJT_VNODE) {
25047221757SJohn Dyson 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
25147221757SJohn Dyson 			printf("vm_object_reference: delay in getting object\n");
25247221757SJohn Dyson 		}
25347221757SJohn Dyson 	}
25495e5e988SJohn Dyson }
25595e5e988SJohn Dyson 
256bf27292bSJohn Dyson void
25795e5e988SJohn Dyson vm_object_vndeallocate(object)
25895e5e988SJohn Dyson 	vm_object_t object;
25995e5e988SJohn Dyson {
26095e5e988SJohn Dyson 	struct vnode *vp = (struct vnode *) object->handle;
261219cbf59SEivind Eklund 
2625526d2d9SEivind Eklund 	KASSERT(object->type == OBJT_VNODE,
2635526d2d9SEivind Eklund 	    ("vm_object_vndeallocate: not a vnode object"));
264219cbf59SEivind Eklund 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
265219cbf59SEivind Eklund #ifdef INVARIANTS
26695e5e988SJohn Dyson 	if (object->ref_count == 0) {
26795e5e988SJohn Dyson 		vprint("vm_object_vndeallocate", vp);
26895e5e988SJohn Dyson 		panic("vm_object_vndeallocate: bad object reference count");
26995e5e988SJohn Dyson 	}
27095e5e988SJohn Dyson #endif
27195e5e988SJohn Dyson 
27295e5e988SJohn Dyson 	object->ref_count--;
27347221757SJohn Dyson 	if (object->ref_count == 0) {
274bf27292bSJohn Dyson 		vp->v_flag &= ~VTEXT;
275069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_OPT);
2762be70f79SJohn Dyson 	}
27747221757SJohn Dyson 	vrele(vp);
278df8bae1dSRodney W. Grimes }
279df8bae1dSRodney W. Grimes 
280df8bae1dSRodney W. Grimes /*
281df8bae1dSRodney W. Grimes  *	vm_object_deallocate:
282df8bae1dSRodney W. Grimes  *
283df8bae1dSRodney W. Grimes  *	Release a reference to the specified object,
284df8bae1dSRodney W. Grimes  *	gained either through a vm_object_allocate
285df8bae1dSRodney W. Grimes  *	or a vm_object_reference call.  When all references
286df8bae1dSRodney W. Grimes  *	are gone, storage associated with this object
287df8bae1dSRodney W. Grimes  *	may be relinquished.
288df8bae1dSRodney W. Grimes  *
289df8bae1dSRodney W. Grimes  *	No object may be locked.
290df8bae1dSRodney W. Grimes  */
29126f9a767SRodney W. Grimes void
29226f9a767SRodney W. Grimes vm_object_deallocate(object)
29326f9a767SRodney W. Grimes 	vm_object_t object;
294df8bae1dSRodney W. Grimes {
295df8bae1dSRodney W. Grimes 	vm_object_t temp;
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 	while (object != NULL) {
298df8bae1dSRodney W. Grimes 
29995e5e988SJohn Dyson 		if (object->type == OBJT_VNODE) {
30095e5e988SJohn Dyson 			vm_object_vndeallocate(object);
30195e5e988SJohn Dyson 			return;
30295e5e988SJohn Dyson 		}
30395e5e988SJohn Dyson 
3042be70f79SJohn Dyson 		if (object->ref_count == 0) {
30547221757SJohn Dyson 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
3062be70f79SJohn Dyson 		} else if (object->ref_count > 2) {
3072be70f79SJohn Dyson 			object->ref_count--;
3082be70f79SJohn Dyson 			return;
3092be70f79SJohn Dyson 		}
3102be70f79SJohn Dyson 
3112be70f79SJohn Dyson 		/*
3122be70f79SJohn Dyson 		 * Here on ref_count of one or two, which are special cases for
3132be70f79SJohn Dyson 		 * objects.
3142be70f79SJohn Dyson 		 */
315c0877f10SJohn Dyson 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
316069e9bc1SDoug Rabson 			vm_object_set_flag(object, OBJ_ONEMAPPING);
317c0877f10SJohn Dyson 			object->ref_count--;
318c0877f10SJohn Dyson 			return;
319c0877f10SJohn Dyson 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
320be6d5bfaSDavid Greenman 			object->ref_count--;
3212be70f79SJohn Dyson 			if ((object->handle == NULL) &&
32224a1cce3SDavid Greenman 			    (object->type == OBJT_DEFAULT ||
32324a1cce3SDavid Greenman 			     object->type == OBJT_SWAP)) {
324a1f6d91cSDavid Greenman 				vm_object_t robject;
32595e5e988SJohn Dyson 
326b18bfc3dSJohn Dyson 				robject = TAILQ_FIRST(&object->shadow_head);
3275526d2d9SEivind Eklund 				KASSERT(robject != NULL,
328219cbf59SEivind Eklund 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
3295526d2d9SEivind Eklund 					 object->ref_count,
3305526d2d9SEivind Eklund 					 object->shadow_count));
33195e5e988SJohn Dyson 				if ((robject->handle == NULL) &&
33224a1cce3SDavid Greenman 				    (robject->type == OBJT_DEFAULT ||
33324a1cce3SDavid Greenman 				     robject->type == OBJT_SWAP)) {
334a1f6d91cSDavid Greenman 
33595e5e988SJohn Dyson 					robject->ref_count++;
33695e5e988SJohn Dyson 
3371c7c3c6aSMatthew Dillon 					while (
3381c7c3c6aSMatthew Dillon 						robject->paging_in_progress ||
3391c7c3c6aSMatthew Dillon 						object->paging_in_progress
3401c7c3c6aSMatthew Dillon 					) {
34166095752SJohn Dyson 						vm_object_pip_sleep(robject, "objde1");
34266095752SJohn Dyson 						vm_object_pip_sleep(object, "objde2");
343a1f6d91cSDavid Greenman 					}
344a1f6d91cSDavid Greenman 
34595e5e988SJohn Dyson 					if (robject->ref_count == 1) {
34695e5e988SJohn Dyson 						robject->ref_count--;
347ba8da839SDavid Greenman 						object = robject;
34895e5e988SJohn Dyson 						goto doterm;
34995e5e988SJohn Dyson 					}
35095e5e988SJohn Dyson 
35195e5e988SJohn Dyson 					object = robject;
35295e5e988SJohn Dyson 					vm_object_collapse(object);
353ba8da839SDavid Greenman 					continue;
354a1f6d91cSDavid Greenman 				}
35595e5e988SJohn Dyson 			}
35695e5e988SJohn Dyson 
357a1f6d91cSDavid Greenman 			return;
358df8bae1dSRodney W. Grimes 
35995e5e988SJohn Dyson 		} else {
3601efb74fbSJohn Dyson 			object->ref_count--;
36195e5e988SJohn Dyson 			if (object->ref_count != 0)
36295e5e988SJohn Dyson 				return;
36395e5e988SJohn Dyson 		}
36495e5e988SJohn Dyson 
36595e5e988SJohn Dyson doterm:
3661efb74fbSJohn Dyson 
36724a1cce3SDavid Greenman 		temp = object->backing_object;
368de5f6a77SJohn Dyson 		if (temp) {
36924a1cce3SDavid Greenman 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
37095e5e988SJohn Dyson 			temp->shadow_count--;
3712d8acc0fSJohn Dyson 			if (temp->ref_count == 0)
372069e9bc1SDoug Rabson 				vm_object_clear_flag(temp, OBJ_OPT);
373eaf13dd7SJohn Dyson 			temp->generation++;
37495461b45SJohn Dyson 			object->backing_object = NULL;
375de5f6a77SJohn Dyson 		}
376df8bae1dSRodney W. Grimes 		vm_object_terminate(object);
377df8bae1dSRodney W. Grimes 		/* unlocks and deallocates object */
378df8bae1dSRodney W. Grimes 		object = temp;
379df8bae1dSRodney W. Grimes 	}
380df8bae1dSRodney W. Grimes }
381df8bae1dSRodney W. Grimes 
382df8bae1dSRodney W. Grimes /*
383df8bae1dSRodney W. Grimes  *	vm_object_terminate actually destroys the specified object, freeing
384df8bae1dSRodney W. Grimes  *	up all previously used resources.
385df8bae1dSRodney W. Grimes  *
386df8bae1dSRodney W. Grimes  *	The object must be locked.
3871c7c3c6aSMatthew Dillon  *	This routine may block.
388df8bae1dSRodney W. Grimes  */
38995e5e988SJohn Dyson void
39026f9a767SRodney W. Grimes vm_object_terminate(object)
391d031cff1SMatthew Dillon 	vm_object_t object;
392df8bae1dSRodney W. Grimes {
393d031cff1SMatthew Dillon 	vm_page_t p;
394e4b7635dSDavid Greenman 	int s;
395df8bae1dSRodney W. Grimes 
39695e5e988SJohn Dyson 	/*
39795e5e988SJohn Dyson 	 * Make sure no one uses us.
39895e5e988SJohn Dyson 	 */
399069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_DEAD);
4003c631446SJohn Dyson 
401df8bae1dSRodney W. Grimes 	/*
402f6b04d2bSDavid Greenman 	 * wait for the pageout daemon to be done with the object
403df8bae1dSRodney W. Grimes 	 */
40466095752SJohn Dyson 	vm_object_pip_wait(object, "objtrm");
405df8bae1dSRodney W. Grimes 
4065526d2d9SEivind Eklund 	KASSERT(!object->paging_in_progress,
4075526d2d9SEivind Eklund 		("vm_object_terminate: pageout in progress"));
40826f9a767SRodney W. Grimes 
40926f9a767SRodney W. Grimes 	/*
4100d94caffSDavid Greenman 	 * Clean and free the pages, as appropriate. All references to the
4110d94caffSDavid Greenman 	 * object are gone, so we don't need to lock it.
41226f9a767SRodney W. Grimes 	 */
41324a1cce3SDavid Greenman 	if (object->type == OBJT_VNODE) {
41495e5e988SJohn Dyson 		struct vnode *vp;
41595e5e988SJohn Dyson 
41695e5e988SJohn Dyson 		/*
41795e5e988SJohn Dyson 		 * Freeze optimized copies.
41895e5e988SJohn Dyson 		 */
41995e5e988SJohn Dyson 		vm_freeze_copyopts(object, 0, object->size);
42095e5e988SJohn Dyson 
42195e5e988SJohn Dyson 		/*
42295e5e988SJohn Dyson 		 * Clean pages and flush buffers.
42395e5e988SJohn Dyson 		 */
4248f9110f6SJohn Dyson 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
42595e5e988SJohn Dyson 
42695e5e988SJohn Dyson 		vp = (struct vnode *) object->handle;
427f6b04d2bSDavid Greenman 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
428bef608bdSJohn Dyson 	}
429bef608bdSJohn Dyson 
4300b10ba98SDavid Greenman 	if (object->ref_count != 0)
4310b10ba98SDavid Greenman 		panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
432996c772fSJohn Dyson 
4330d94caffSDavid Greenman 	/*
434356863ebSDavid Greenman 	 * Now free any remaining pages. For internal objects, this also
435356863ebSDavid Greenman 	 * removes them from paging queues. Don't free wired pages, just
436356863ebSDavid Greenman 	 * remove them from the object.
437df8bae1dSRodney W. Grimes 	 */
438e4b7635dSDavid Greenman 	s = splvm();
439b18bfc3dSJohn Dyson 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
440a2f4a846SJohn Dyson 		if (p->busy || (p->flags & PG_BUSY))
4411c7c3c6aSMatthew Dillon 			panic("vm_object_terminate: freeing busy page %p\n", p);
4420b10ba98SDavid Greenman 		if (p->wire_count == 0) {
443e69763a3SDoug Rabson 			vm_page_busy(p);
444df8bae1dSRodney W. Grimes 			vm_page_free(p);
445df8bae1dSRodney W. Grimes 			cnt.v_pfree++;
4460b10ba98SDavid Greenman 		} else {
447356863ebSDavid Greenman 			vm_page_busy(p);
4480b10ba98SDavid Greenman 			vm_page_remove(p);
4490b10ba98SDavid Greenman 		}
450df8bae1dSRodney W. Grimes 	}
451e4b7635dSDavid Greenman 	splx(s);
452bef608bdSJohn Dyson 
4532d8acc0fSJohn Dyson 	/*
4549fcfb650SDavid Greenman 	 * Let the pager know object is dead.
4559fcfb650SDavid Greenman 	 */
4569fcfb650SDavid Greenman 	vm_pager_deallocate(object);
4579fcfb650SDavid Greenman 
4589fcfb650SDavid Greenman 	/*
4590b10ba98SDavid Greenman 	 * Remove the object from the global object list.
4602d8acc0fSJohn Dyson 	 */
4611b367556SJason Evans 	mtx_enter(&vm_object_list_mtx, MTX_DEF);
462df8bae1dSRodney W. Grimes 	TAILQ_REMOVE(&vm_object_list, object, object_list);
4631b367556SJason Evans 	mtx_exit(&vm_object_list_mtx, MTX_DEF);
4640b10ba98SDavid Greenman 
4650b10ba98SDavid Greenman 	wakeup(object);
4660b10ba98SDavid Greenman 
467df8bae1dSRodney W. Grimes 	/*
468df8bae1dSRodney W. Grimes 	 * Free the space for the object.
469df8bae1dSRodney W. Grimes 	 */
47099448ed1SJohn Dyson 	zfree(obj_zone, object);
47147221757SJohn Dyson }
472df8bae1dSRodney W. Grimes 
473df8bae1dSRodney W. Grimes /*
474df8bae1dSRodney W. Grimes  *	vm_object_page_clean
475df8bae1dSRodney W. Grimes  *
4764f79d873SMatthew Dillon  *	Clean all dirty pages in the specified range of object.  Leaves page
4774f79d873SMatthew Dillon  * 	on whatever queue it is currently on.   If NOSYNC is set then do not
4784f79d873SMatthew Dillon  *	write out pages with PG_NOSYNC set (originally comes from MAP_NOSYNC),
4794f79d873SMatthew Dillon  *	leaving the object dirty.
48026f9a767SRodney W. Grimes  *
48126f9a767SRodney W. Grimes  *	Odd semantics: if start == end, we clean everything.
48226f9a767SRodney W. Grimes  *
48326f9a767SRodney W. Grimes  *	The object must be locked.
48426f9a767SRodney W. Grimes  */
485f6b04d2bSDavid Greenman 
486f6b04d2bSDavid Greenman void
4878f9110f6SJohn Dyson vm_object_page_clean(object, start, end, flags)
488f6b04d2bSDavid Greenman 	vm_object_t object;
489a316d390SJohn Dyson 	vm_pindex_t start;
490a316d390SJohn Dyson 	vm_pindex_t end;
4918f9110f6SJohn Dyson 	int flags;
492f6b04d2bSDavid Greenman {
493d031cff1SMatthew Dillon 	vm_page_t p, np, tp;
494d031cff1SMatthew Dillon 	vm_offset_t tstart, tend;
495bd7e5f99SJohn Dyson 	vm_pindex_t pi;
496aef922f5SJohn Dyson 	int s;
49724a1cce3SDavid Greenman 	struct vnode *vp;
498aef922f5SJohn Dyson 	int runlen;
499bd7e5f99SJohn Dyson 	int maxf;
500bd7e5f99SJohn Dyson 	int chkb;
501bd7e5f99SJohn Dyson 	int maxb;
502bd7e5f99SJohn Dyson 	int i;
5034f79d873SMatthew Dillon 	int clearobjflags;
5048f9110f6SJohn Dyson 	int pagerflags;
505bd7e5f99SJohn Dyson 	vm_page_t maf[vm_pageout_page_count];
506bd7e5f99SJohn Dyson 	vm_page_t mab[vm_pageout_page_count];
507aef922f5SJohn Dyson 	vm_page_t ma[vm_pageout_page_count];
5082d8acc0fSJohn Dyson 	int curgeneration;
509f6b04d2bSDavid Greenman 
510aef922f5SJohn Dyson 	if (object->type != OBJT_VNODE ||
511aef922f5SJohn Dyson 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
512f6b04d2bSDavid Greenman 		return;
513f6b04d2bSDavid Greenman 
5148f9110f6SJohn Dyson 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
5158f9110f6SJohn Dyson 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
5168f9110f6SJohn Dyson 
51724a1cce3SDavid Greenman 	vp = object->handle;
51824a1cce3SDavid Greenman 
519069e9bc1SDoug Rabson 	vm_object_set_flag(object, OBJ_CLEANING);
52024a1cce3SDavid Greenman 
521f6b04d2bSDavid Greenman 	tstart = start;
522f6b04d2bSDavid Greenman 	if (end == 0) {
523f6b04d2bSDavid Greenman 		tend = object->size;
524f6b04d2bSDavid Greenman 	} else {
525f6b04d2bSDavid Greenman 		tend = end;
526f6b04d2bSDavid Greenman 	}
527eaf13dd7SJohn Dyson 
5284f79d873SMatthew Dillon 	/*
5294f79d873SMatthew Dillon 	 * Generally set CLEANCHK interlock and make the page read-only so
5304f79d873SMatthew Dillon 	 * we can then clear the object flags.
5314f79d873SMatthew Dillon 	 *
5324f79d873SMatthew Dillon 	 * However, if this is a nosync mmap then the object is likely to
5334f79d873SMatthew Dillon 	 * stay dirty so do not mess with the page and do not clear the
5344f79d873SMatthew Dillon 	 * object flags.
5354f79d873SMatthew Dillon 	 */
5364f79d873SMatthew Dillon 
5374f79d873SMatthew Dillon 	clearobjflags = 1;
5384f79d873SMatthew Dillon 
539fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
540e69763a3SDoug Rabson 		vm_page_flag_set(p, PG_CLEANCHK);
5414f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC))
5424f79d873SMatthew Dillon 			clearobjflags = 0;
5434f79d873SMatthew Dillon 		else
544eaf13dd7SJohn Dyson 			vm_page_protect(p, VM_PROT_READ);
545eaf13dd7SJohn Dyson 	}
546eaf13dd7SJohn Dyson 
5474f79d873SMatthew Dillon 	if (clearobjflags && (tstart == 0) && (tend == object->size)) {
548069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
549ec4f9fb0SDavid Greenman 	}
550f6b04d2bSDavid Greenman 
551bd7e5f99SJohn Dyson rescan:
5522d8acc0fSJohn Dyson 	curgeneration = object->generation;
5532d8acc0fSJohn Dyson 
554b18bfc3dSJohn Dyson 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
555b18bfc3dSJohn Dyson 		np = TAILQ_NEXT(p, listq);
556bd7e5f99SJohn Dyson 
557bd7e5f99SJohn Dyson 		pi = p->pindex;
558bd7e5f99SJohn Dyson 		if (((p->flags & PG_CLEANCHK) == 0) ||
559bd7e5f99SJohn Dyson 			(pi < tstart) || (pi >= tend) ||
5605070c7f8SJohn Dyson 			(p->valid == 0) ||
5615070c7f8SJohn Dyson 			((p->queue - p->pc) == PQ_CACHE)) {
562e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
563aef922f5SJohn Dyson 			continue;
564f6b04d2bSDavid Greenman 		}
565f6b04d2bSDavid Greenman 
566bd7e5f99SJohn Dyson 		vm_page_test_dirty(p);
567bd7e5f99SJohn Dyson 		if ((p->dirty & p->valid) == 0) {
568e69763a3SDoug Rabson 			vm_page_flag_clear(p, PG_CLEANCHK);
569bd7e5f99SJohn Dyson 			continue;
570bd7e5f99SJohn Dyson 		}
571ec4f9fb0SDavid Greenman 
5724f79d873SMatthew Dillon 		/*
5734f79d873SMatthew Dillon 		 * If we have been asked to skip nosync pages and this is a
5744f79d873SMatthew Dillon 		 * nosync page, skip it.  Note that the object flags were
5754f79d873SMatthew Dillon 		 * not cleared in this case so we do not have to set them.
5764f79d873SMatthew Dillon 		 */
5774f79d873SMatthew Dillon 		if ((flags & OBJPC_NOSYNC) && (p->flags & PG_NOSYNC)) {
5784f79d873SMatthew Dillon 			vm_page_flag_clear(p, PG_CLEANCHK);
5794f79d873SMatthew Dillon 			continue;
5804f79d873SMatthew Dillon 		}
5814f79d873SMatthew Dillon 
582b18bfc3dSJohn Dyson 		s = splvm();
5831c7c3c6aSMatthew Dillon 		while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
5842d8acc0fSJohn Dyson 			if (object->generation != curgeneration) {
585f6b04d2bSDavid Greenman 				splx(s);
586bd7e5f99SJohn Dyson 				goto rescan;
587f6b04d2bSDavid Greenman 			}
5882d8acc0fSJohn Dyson 		}
589f6b04d2bSDavid Greenman 
590bd7e5f99SJohn Dyson 		maxf = 0;
591bd7e5f99SJohn Dyson 		for(i=1;i<vm_pageout_page_count;i++) {
5928aef1712SMatthew Dillon 			if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
593bd7e5f99SJohn Dyson 				if ((tp->flags & PG_BUSY) ||
594ffc82b0aSJohn Dyson 					(tp->flags & PG_CLEANCHK) == 0 ||
595ffc82b0aSJohn Dyson 					(tp->busy != 0))
596bd7e5f99SJohn Dyson 					break;
5975070c7f8SJohn Dyson 				if((tp->queue - tp->pc) == PQ_CACHE) {
598e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
5993077a9c2SJohn Dyson 					break;
6003077a9c2SJohn Dyson 				}
601bd7e5f99SJohn Dyson 				vm_page_test_dirty(tp);
602bd7e5f99SJohn Dyson 				if ((tp->dirty & tp->valid) == 0) {
603e69763a3SDoug Rabson 					vm_page_flag_clear(tp, PG_CLEANCHK);
604bd7e5f99SJohn Dyson 					break;
605bd7e5f99SJohn Dyson 				}
606bd7e5f99SJohn Dyson 				maf[ i - 1 ] = tp;
607bd7e5f99SJohn Dyson 				maxf++;
608bd7e5f99SJohn Dyson 				continue;
609bd7e5f99SJohn Dyson 			}
610bd7e5f99SJohn Dyson 			break;
611bd7e5f99SJohn Dyson 		}
612aef922f5SJohn Dyson 
613bd7e5f99SJohn Dyson 		maxb = 0;
614bd7e5f99SJohn Dyson 		chkb = vm_pageout_page_count -  maxf;
615bd7e5f99SJohn Dyson 		if (chkb) {
616bd7e5f99SJohn Dyson 			for(i = 1; i < chkb;i++) {
6178aef1712SMatthew Dillon 				if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
618bd7e5f99SJohn Dyson 					if ((tp->flags & PG_BUSY) ||
619ffc82b0aSJohn Dyson 						(tp->flags & PG_CLEANCHK) == 0 ||
620ffc82b0aSJohn Dyson 						(tp->busy != 0))
621bd7e5f99SJohn Dyson 						break;
6225070c7f8SJohn Dyson 					if((tp->queue - tp->pc) == PQ_CACHE) {
623e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
6243077a9c2SJohn Dyson 						break;
6253077a9c2SJohn Dyson 					}
626bd7e5f99SJohn Dyson 					vm_page_test_dirty(tp);
627bd7e5f99SJohn Dyson 					if ((tp->dirty & tp->valid) == 0) {
628e69763a3SDoug Rabson 						vm_page_flag_clear(tp, PG_CLEANCHK);
629bd7e5f99SJohn Dyson 						break;
630bd7e5f99SJohn Dyson 					}
631bd7e5f99SJohn Dyson 					mab[ i - 1 ] = tp;
632bd7e5f99SJohn Dyson 					maxb++;
633bd7e5f99SJohn Dyson 					continue;
634bd7e5f99SJohn Dyson 				}
635bd7e5f99SJohn Dyson 				break;
636bd7e5f99SJohn Dyson 			}
637bd7e5f99SJohn Dyson 		}
638bd7e5f99SJohn Dyson 
639bd7e5f99SJohn Dyson 		for(i=0;i<maxb;i++) {
640bd7e5f99SJohn Dyson 			int index = (maxb - i) - 1;
641bd7e5f99SJohn Dyson 			ma[index] = mab[i];
642e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
643bd7e5f99SJohn Dyson 		}
644e69763a3SDoug Rabson 		vm_page_flag_clear(p, PG_CLEANCHK);
645bd7e5f99SJohn Dyson 		ma[maxb] = p;
646bd7e5f99SJohn Dyson 		for(i=0;i<maxf;i++) {
647bd7e5f99SJohn Dyson 			int index = (maxb + i) + 1;
648bd7e5f99SJohn Dyson 			ma[index] = maf[i];
649e69763a3SDoug Rabson 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
650f6b04d2bSDavid Greenman 		}
651bd7e5f99SJohn Dyson 		runlen = maxb + maxf + 1;
652cf2819ccSJohn Dyson 
653f35329acSJohn Dyson 		splx(s);
6548f9110f6SJohn Dyson 		vm_pageout_flush(ma, runlen, pagerflags);
655cf2819ccSJohn Dyson 		for (i = 0; i<runlen; i++) {
656cf2819ccSJohn Dyson 			if (ma[i]->valid & ma[i]->dirty) {
657cf2819ccSJohn Dyson 				vm_page_protect(ma[i], VM_PROT_READ);
658e69763a3SDoug Rabson 				vm_page_flag_set(ma[i], PG_CLEANCHK);
659cf2819ccSJohn Dyson 			}
660cf2819ccSJohn Dyson 		}
6612d8acc0fSJohn Dyson 		if (object->generation != curgeneration)
662bd7e5f99SJohn Dyson 			goto rescan;
663f6b04d2bSDavid Greenman 	}
664aef922f5SJohn Dyson 
665479112dfSMatthew Dillon #if 0
666cf2819ccSJohn Dyson 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
667479112dfSMatthew Dillon #endif
668aef922f5SJohn Dyson 
669069e9bc1SDoug Rabson 	vm_object_clear_flag(object, OBJ_CLEANING);
670f5cf85d4SDavid Greenman 	return;
67126f9a767SRodney W. Grimes }
672df8bae1dSRodney W. Grimes 
673f708ef1bSPoul-Henning Kamp #ifdef not_used
674f708ef1bSPoul-Henning Kamp /* XXX I cannot tell if this should be an exported symbol */
675df8bae1dSRodney W. Grimes /*
676df8bae1dSRodney W. Grimes  *	vm_object_deactivate_pages
677df8bae1dSRodney W. Grimes  *
678df8bae1dSRodney W. Grimes  *	Deactivate all pages in the specified object.  (Keep its pages
679df8bae1dSRodney W. Grimes  *	in memory even though it is no longer referenced.)
680df8bae1dSRodney W. Grimes  *
681df8bae1dSRodney W. Grimes  *	The object must be locked.
682df8bae1dSRodney W. Grimes  */
683f708ef1bSPoul-Henning Kamp static void
684df8bae1dSRodney W. Grimes vm_object_deactivate_pages(object)
685d031cff1SMatthew Dillon 	vm_object_t object;
686df8bae1dSRodney W. Grimes {
687d031cff1SMatthew Dillon 	vm_page_t p, next;
688df8bae1dSRodney W. Grimes 
689b18bfc3dSJohn Dyson 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
690b18bfc3dSJohn Dyson 		next = TAILQ_NEXT(p, listq);
691df8bae1dSRodney W. Grimes 		vm_page_deactivate(p);
692df8bae1dSRodney W. Grimes 	}
693df8bae1dSRodney W. Grimes }
694f708ef1bSPoul-Henning Kamp #endif
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes /*
6978e3ad7c9SMatthew Dillon  * Same as vm_object_pmap_copy, except range checking really
6981efb74fbSJohn Dyson  * works, and is meant for small sections of an object.
6998e3ad7c9SMatthew Dillon  *
7008e3ad7c9SMatthew Dillon  * This code protects resident pages by making them read-only
7018e3ad7c9SMatthew Dillon  * and is typically called on a fork or split when a page
7028e3ad7c9SMatthew Dillon  * is converted to copy-on-write.
7038e3ad7c9SMatthew Dillon  *
7048e3ad7c9SMatthew Dillon  * NOTE: If the page is already at VM_PROT_NONE, calling
7058e3ad7c9SMatthew Dillon  * vm_page_protect will have no effect.
7061efb74fbSJohn Dyson  */
7078e3ad7c9SMatthew Dillon 
7081efb74fbSJohn Dyson void
7091efb74fbSJohn Dyson vm_object_pmap_copy_1(object, start, end)
710d031cff1SMatthew Dillon 	vm_object_t object;
711d031cff1SMatthew Dillon 	vm_pindex_t start;
712d031cff1SMatthew Dillon 	vm_pindex_t end;
7131efb74fbSJohn Dyson {
7141efb74fbSJohn Dyson 	vm_pindex_t idx;
715d031cff1SMatthew Dillon 	vm_page_t p;
7161efb74fbSJohn Dyson 
7171efb74fbSJohn Dyson 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
7181efb74fbSJohn Dyson 		return;
7191efb74fbSJohn Dyson 
7201efb74fbSJohn Dyson 	for (idx = start; idx < end; idx++) {
7211efb74fbSJohn Dyson 		p = vm_page_lookup(object, idx);
7221efb74fbSJohn Dyson 		if (p == NULL)
7231efb74fbSJohn Dyson 			continue;
7241efb74fbSJohn Dyson 		vm_page_protect(p, VM_PROT_READ);
7251efb74fbSJohn Dyson 	}
7261efb74fbSJohn Dyson }
7271efb74fbSJohn Dyson 
7281efb74fbSJohn Dyson /*
729df8bae1dSRodney W. Grimes  *	vm_object_pmap_remove:
730df8bae1dSRodney W. Grimes  *
731df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
732df8bae1dSRodney W. Grimes  *	object range from all physical maps.
733df8bae1dSRodney W. Grimes  *
734df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
735df8bae1dSRodney W. Grimes  */
73626f9a767SRodney W. Grimes void
73726f9a767SRodney W. Grimes vm_object_pmap_remove(object, start, end)
738d031cff1SMatthew Dillon 	vm_object_t object;
739d031cff1SMatthew Dillon 	vm_pindex_t start;
740d031cff1SMatthew Dillon 	vm_pindex_t end;
741df8bae1dSRodney W. Grimes {
742d031cff1SMatthew Dillon 	vm_page_t p;
743d031cff1SMatthew Dillon 
744df8bae1dSRodney W. Grimes 	if (object == NULL)
745df8bae1dSRodney W. Grimes 		return;
746b5b40fa6SJohn Dyson 	for (p = TAILQ_FIRST(&object->memq);
747b5b40fa6SJohn Dyson 		p != NULL;
748b5b40fa6SJohn Dyson 		p = TAILQ_NEXT(p, listq)) {
749bd7e5f99SJohn Dyson 		if (p->pindex >= start && p->pindex < end)
750f919ebdeSDavid Greenman 			vm_page_protect(p, VM_PROT_NONE);
75126f9a767SRodney W. Grimes 	}
7526e20a165SJohn Dyson 	if ((start == 0) && (object->size == end))
753069e9bc1SDoug Rabson 		vm_object_clear_flag(object, OBJ_WRITEABLE);
75426f9a767SRodney W. Grimes }
755df8bae1dSRodney W. Grimes 
756df8bae1dSRodney W. Grimes /*
757867a482dSJohn Dyson  *	vm_object_madvise:
758867a482dSJohn Dyson  *
759867a482dSJohn Dyson  *	Implements the madvise function at the object/page level.
7601c7c3c6aSMatthew Dillon  *
761193b9358SAlan Cox  *	MADV_WILLNEED	(any object)
762193b9358SAlan Cox  *
763193b9358SAlan Cox  *	    Activate the specified pages if they are resident.
764193b9358SAlan Cox  *
765193b9358SAlan Cox  *	MADV_DONTNEED	(any object)
766193b9358SAlan Cox  *
767193b9358SAlan Cox  *	    Deactivate the specified pages if they are resident.
768193b9358SAlan Cox  *
769193b9358SAlan Cox  *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
770193b9358SAlan Cox  *			 OBJ_ONEMAPPING only)
771193b9358SAlan Cox  *
772193b9358SAlan Cox  *	    Deactivate and clean the specified pages if they are
773193b9358SAlan Cox  *	    resident.  This permits the process to reuse the pages
774193b9358SAlan Cox  *	    without faulting or the kernel to reclaim the pages
775193b9358SAlan Cox  *	    without I/O.
776867a482dSJohn Dyson  */
777867a482dSJohn Dyson void
778867a482dSJohn Dyson vm_object_madvise(object, pindex, count, advise)
779867a482dSJohn Dyson 	vm_object_t object;
780867a482dSJohn Dyson 	vm_pindex_t pindex;
781867a482dSJohn Dyson 	int count;
782867a482dSJohn Dyson 	int advise;
783867a482dSJohn Dyson {
7846e20a165SJohn Dyson 	vm_pindex_t end, tpindex;
7856e20a165SJohn Dyson 	vm_object_t tobject;
786867a482dSJohn Dyson 	vm_page_t m;
787867a482dSJohn Dyson 
788867a482dSJohn Dyson 	if (object == NULL)
789867a482dSJohn Dyson 		return;
790867a482dSJohn Dyson 
791867a482dSJohn Dyson 	end = pindex + count;
792867a482dSJohn Dyson 
7931c7c3c6aSMatthew Dillon 	/*
7941c7c3c6aSMatthew Dillon 	 * Locate and adjust resident pages
7951c7c3c6aSMatthew Dillon 	 */
7961c7c3c6aSMatthew Dillon 
7971c7c3c6aSMatthew Dillon 	for (; pindex < end; pindex += 1) {
7986e20a165SJohn Dyson relookup:
7996e20a165SJohn Dyson 		tobject = object;
8006e20a165SJohn Dyson 		tpindex = pindex;
8016e20a165SJohn Dyson shadowlookup:
80258b4e6ccSAlan Cox 		/*
80358b4e6ccSAlan Cox 		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
80458b4e6ccSAlan Cox 		 * and those pages must be OBJ_ONEMAPPING.
80558b4e6ccSAlan Cox 		 */
80658b4e6ccSAlan Cox 		if (advise == MADV_FREE) {
80758b4e6ccSAlan Cox 			if ((tobject->type != OBJT_DEFAULT &&
80858b4e6ccSAlan Cox 			     tobject->type != OBJT_SWAP) ||
80958b4e6ccSAlan Cox 			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
8106e20a165SJohn Dyson 				continue;
8116e20a165SJohn Dyson 			}
81258b4e6ccSAlan Cox 		}
8131c7c3c6aSMatthew Dillon 
8141c7c3c6aSMatthew Dillon 		m = vm_page_lookup(tobject, tpindex);
8151c7c3c6aSMatthew Dillon 
8161c7c3c6aSMatthew Dillon 		if (m == NULL) {
8171ce137beSMatthew Dillon 			/*
8181ce137beSMatthew Dillon 			 * There may be swap even if there is no backing page
8191ce137beSMatthew Dillon 			 */
8201ce137beSMatthew Dillon 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
8211ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
8221ce137beSMatthew Dillon 
8231ce137beSMatthew Dillon 			/*
8241ce137beSMatthew Dillon 			 * next object
8251ce137beSMatthew Dillon 			 */
8266e20a165SJohn Dyson 			tobject = tobject->backing_object;
8271c7c3c6aSMatthew Dillon 			if (tobject == NULL)
8281c7c3c6aSMatthew Dillon 				continue;
8296e20a165SJohn Dyson 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
8306e20a165SJohn Dyson 			goto shadowlookup;
8316e20a165SJohn Dyson 		}
832867a482dSJohn Dyson 
833867a482dSJohn Dyson 		/*
834867a482dSJohn Dyson 		 * If the page is busy or not in a normal active state,
8358b03c8edSMatthew Dillon 		 * we skip it.  If the page is not managed there are no
8368b03c8edSMatthew Dillon 		 * page queues to mess with.  Things can break if we mess
8378b03c8edSMatthew Dillon 		 * with pages in any of the below states.
838867a482dSJohn Dyson 		 */
8391c7c3c6aSMatthew Dillon 		if (
8401c7c3c6aSMatthew Dillon 		    m->hold_count ||
8411c7c3c6aSMatthew Dillon 		    m->wire_count ||
8428b03c8edSMatthew Dillon 		    (m->flags & PG_UNMANAGED) ||
8431c7c3c6aSMatthew Dillon 		    m->valid != VM_PAGE_BITS_ALL
8441c7c3c6aSMatthew Dillon 		) {
845867a482dSJohn Dyson 			continue;
8466e20a165SJohn Dyson 		}
8476e20a165SJohn Dyson 
8481c7c3c6aSMatthew Dillon  		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
8496e20a165SJohn Dyson   			goto relookup;
850867a482dSJohn Dyson 
851867a482dSJohn Dyson 		if (advise == MADV_WILLNEED) {
852867a482dSJohn Dyson 			vm_page_activate(m);
8536e20a165SJohn Dyson 		} else if (advise == MADV_DONTNEED) {
854479112dfSMatthew Dillon 			vm_page_dontneed(m);
8550a47b48bSJohn Dyson 		} else if (advise == MADV_FREE) {
8561c7c3c6aSMatthew Dillon 			/*
8572aaeadf8SMatthew Dillon 			 * Mark the page clean.  This will allow the page
8582aaeadf8SMatthew Dillon 			 * to be freed up by the system.  However, such pages
8592aaeadf8SMatthew Dillon 			 * are often reused quickly by malloc()/free()
8602aaeadf8SMatthew Dillon 			 * so we do not do anything that would cause
8612aaeadf8SMatthew Dillon 			 * a page fault if we can help it.
8622aaeadf8SMatthew Dillon 			 *
8632aaeadf8SMatthew Dillon 			 * Specifically, we do not try to actually free
8642aaeadf8SMatthew Dillon 			 * the page now nor do we try to put it in the
8652aaeadf8SMatthew Dillon 			 * cache (which would cause a page fault on reuse).
86641c67e12SMatthew Dillon 			 *
86741c67e12SMatthew Dillon 			 * But we do make the page is freeable as we
86841c67e12SMatthew Dillon 			 * can without actually taking the step of unmapping
86941c67e12SMatthew Dillon 			 * it.
8701c7c3c6aSMatthew Dillon 			 */
8710385347cSPeter Wemm 			pmap_clear_modify(m);
8726e20a165SJohn Dyson 			m->dirty = 0;
87341c67e12SMatthew Dillon 			m->act_count = 0;
874479112dfSMatthew Dillon 			vm_page_dontneed(m);
8751ce137beSMatthew Dillon 			if (tobject->type == OBJT_SWAP)
8761ce137beSMatthew Dillon 				swap_pager_freespace(tobject, tpindex, 1);
877867a482dSJohn Dyson 		}
878867a482dSJohn Dyson 	}
879867a482dSJohn Dyson }
880867a482dSJohn Dyson 
881867a482dSJohn Dyson /*
882df8bae1dSRodney W. Grimes  *	vm_object_shadow:
883df8bae1dSRodney W. Grimes  *
884df8bae1dSRodney W. Grimes  *	Create a new object which is backed by the
885df8bae1dSRodney W. Grimes  *	specified existing object range.  The source
886df8bae1dSRodney W. Grimes  *	object reference is deallocated.
887df8bae1dSRodney W. Grimes  *
888df8bae1dSRodney W. Grimes  *	The new object and offset into that object
889df8bae1dSRodney W. Grimes  *	are returned in the source parameters.
890df8bae1dSRodney W. Grimes  */
891df8bae1dSRodney W. Grimes 
89226f9a767SRodney W. Grimes void
89326f9a767SRodney W. Grimes vm_object_shadow(object, offset, length)
894df8bae1dSRodney W. Grimes 	vm_object_t *object;	/* IN/OUT */
895a316d390SJohn Dyson 	vm_ooffset_t *offset;	/* IN/OUT */
896df8bae1dSRodney W. Grimes 	vm_size_t length;
897df8bae1dSRodney W. Grimes {
898d031cff1SMatthew Dillon 	vm_object_t source;
899d031cff1SMatthew Dillon 	vm_object_t result;
900df8bae1dSRodney W. Grimes 
901df8bae1dSRodney W. Grimes 	source = *object;
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes 	/*
9049a2f6362SAlan Cox 	 * Don't create the new object if the old object isn't shared.
9059a2f6362SAlan Cox 	 */
9069a2f6362SAlan Cox 
907c7997d57SAlan Cox 	if (source != NULL &&
908c7997d57SAlan Cox 	    source->ref_count == 1 &&
9099a2f6362SAlan Cox 	    source->handle == NULL &&
9109a2f6362SAlan Cox 	    (source->type == OBJT_DEFAULT ||
9119a2f6362SAlan Cox 	     source->type == OBJT_SWAP))
9129a2f6362SAlan Cox 		return;
9139a2f6362SAlan Cox 
9149a2f6362SAlan Cox 	/*
915df8bae1dSRodney W. Grimes 	 * Allocate a new object with the given length
916df8bae1dSRodney W. Grimes 	 */
917df8bae1dSRodney W. Grimes 
91824a1cce3SDavid Greenman 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
919df8bae1dSRodney W. Grimes 		panic("vm_object_shadow: no object for shadowing");
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes 	/*
9220d94caffSDavid Greenman 	 * The new object shadows the source object, adding a reference to it.
9230d94caffSDavid Greenman 	 * Our caller changes his reference to point to the new object,
9240d94caffSDavid Greenman 	 * removing a reference to the source object.  Net result: no change
9250d94caffSDavid Greenman 	 * of reference count.
9269b09fe24SMatthew Dillon 	 *
9279b09fe24SMatthew Dillon 	 * Try to optimize the result object's page color when shadowing
928956f3135SPhilippe Charnier 	 * in order to maintain page coloring consistency in the combined
9299b09fe24SMatthew Dillon 	 * shadowed object.
930df8bae1dSRodney W. Grimes 	 */
93124a1cce3SDavid Greenman 	result->backing_object = source;
932de5f6a77SJohn Dyson 	if (source) {
933de5f6a77SJohn Dyson 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
934eaf13dd7SJohn Dyson 		source->shadow_count++;
935eaf13dd7SJohn Dyson 		source->generation++;
9369b09fe24SMatthew Dillon 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
937de5f6a77SJohn Dyson 	}
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes 	/*
9400d94caffSDavid Greenman 	 * Store the offset into the source object, and fix up the offset into
9410d94caffSDavid Greenman 	 * the new object.
942df8bae1dSRodney W. Grimes 	 */
943df8bae1dSRodney W. Grimes 
94424a1cce3SDavid Greenman 	result->backing_object_offset = *offset;
945df8bae1dSRodney W. Grimes 
946df8bae1dSRodney W. Grimes 	/*
947df8bae1dSRodney W. Grimes 	 * Return the new things
948df8bae1dSRodney W. Grimes 	 */
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 	*offset = 0;
951df8bae1dSRodney W. Grimes 	*object = result;
952df8bae1dSRodney W. Grimes }
953df8bae1dSRodney W. Grimes 
9542ad1a3f7SMatthew Dillon #define	OBSC_TEST_ALL_SHADOWED	0x0001
9552ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_NOWAIT	0x0002
9562ad1a3f7SMatthew Dillon #define	OBSC_COLLAPSE_WAIT	0x0004
9572ad1a3f7SMatthew Dillon 
9582ad1a3f7SMatthew Dillon static __inline int
9592ad1a3f7SMatthew Dillon vm_object_backing_scan(vm_object_t object, int op)
9602ad1a3f7SMatthew Dillon {
9612ad1a3f7SMatthew Dillon 	int s;
9622ad1a3f7SMatthew Dillon 	int r = 1;
9632ad1a3f7SMatthew Dillon 	vm_page_t p;
9642ad1a3f7SMatthew Dillon 	vm_object_t backing_object;
9652ad1a3f7SMatthew Dillon 	vm_pindex_t backing_offset_index;
9662ad1a3f7SMatthew Dillon 
9672ad1a3f7SMatthew Dillon 	s = splvm();
9682ad1a3f7SMatthew Dillon 
9692ad1a3f7SMatthew Dillon 	backing_object = object->backing_object;
9702ad1a3f7SMatthew Dillon 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
9712ad1a3f7SMatthew Dillon 
9722ad1a3f7SMatthew Dillon 	/*
9732ad1a3f7SMatthew Dillon 	 * Initial conditions
9742ad1a3f7SMatthew Dillon 	 */
9752ad1a3f7SMatthew Dillon 
9762ad1a3f7SMatthew Dillon 	if (op & OBSC_TEST_ALL_SHADOWED) {
9772ad1a3f7SMatthew Dillon 		/*
978956f3135SPhilippe Charnier 		 * We do not want to have to test for the existence of
9792ad1a3f7SMatthew Dillon 		 * swap pages in the backing object.  XXX but with the
9802ad1a3f7SMatthew Dillon 		 * new swapper this would be pretty easy to do.
9812ad1a3f7SMatthew Dillon 		 *
9822ad1a3f7SMatthew Dillon 		 * XXX what about anonymous MAP_SHARED memory that hasn't
9832ad1a3f7SMatthew Dillon 		 * been ZFOD faulted yet?  If we do not test for this, the
9842ad1a3f7SMatthew Dillon 		 * shadow test may succeed! XXX
9852ad1a3f7SMatthew Dillon 		 */
9862ad1a3f7SMatthew Dillon 		if (backing_object->type != OBJT_DEFAULT) {
9872ad1a3f7SMatthew Dillon 			splx(s);
9882ad1a3f7SMatthew Dillon 			return(0);
9892ad1a3f7SMatthew Dillon 		}
9902ad1a3f7SMatthew Dillon 	}
9912ad1a3f7SMatthew Dillon 	if (op & OBSC_COLLAPSE_WAIT) {
9922ad1a3f7SMatthew Dillon 		vm_object_set_flag(backing_object, OBJ_DEAD);
9932ad1a3f7SMatthew Dillon 	}
9942ad1a3f7SMatthew Dillon 
9952ad1a3f7SMatthew Dillon 	/*
9962ad1a3f7SMatthew Dillon 	 * Our scan
9972ad1a3f7SMatthew Dillon 	 */
9982ad1a3f7SMatthew Dillon 
9992ad1a3f7SMatthew Dillon 	p = TAILQ_FIRST(&backing_object->memq);
10002ad1a3f7SMatthew Dillon 	while (p) {
10012ad1a3f7SMatthew Dillon 		vm_page_t next = TAILQ_NEXT(p, listq);
10022ad1a3f7SMatthew Dillon 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
10032ad1a3f7SMatthew Dillon 
10042ad1a3f7SMatthew Dillon 		if (op & OBSC_TEST_ALL_SHADOWED) {
10052ad1a3f7SMatthew Dillon 			vm_page_t pp;
10062ad1a3f7SMatthew Dillon 
10072ad1a3f7SMatthew Dillon 			/*
10082ad1a3f7SMatthew Dillon 			 * Ignore pages outside the parent object's range
10092ad1a3f7SMatthew Dillon 			 * and outside the parent object's mapping of the
10102ad1a3f7SMatthew Dillon 			 * backing object.
10112ad1a3f7SMatthew Dillon 			 *
10122ad1a3f7SMatthew Dillon 			 * note that we do not busy the backing object's
10132ad1a3f7SMatthew Dillon 			 * page.
10142ad1a3f7SMatthew Dillon 			 */
10152ad1a3f7SMatthew Dillon 
10162ad1a3f7SMatthew Dillon 			if (
10172ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
10182ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
10192ad1a3f7SMatthew Dillon 			) {
10202ad1a3f7SMatthew Dillon 				p = next;
10212ad1a3f7SMatthew Dillon 				continue;
10222ad1a3f7SMatthew Dillon 			}
10232ad1a3f7SMatthew Dillon 
10242ad1a3f7SMatthew Dillon 			/*
10252ad1a3f7SMatthew Dillon 			 * See if the parent has the page or if the parent's
10262ad1a3f7SMatthew Dillon 			 * object pager has the page.  If the parent has the
10272ad1a3f7SMatthew Dillon 			 * page but the page is not valid, the parent's
10282ad1a3f7SMatthew Dillon 			 * object pager must have the page.
10292ad1a3f7SMatthew Dillon 			 *
10302ad1a3f7SMatthew Dillon 			 * If this fails, the parent does not completely shadow
10312ad1a3f7SMatthew Dillon 			 * the object and we might as well give up now.
10322ad1a3f7SMatthew Dillon 			 */
10332ad1a3f7SMatthew Dillon 
10342ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
10352ad1a3f7SMatthew Dillon 			if (
10362ad1a3f7SMatthew Dillon 			    (pp == NULL || pp->valid == 0) &&
10372ad1a3f7SMatthew Dillon 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
10382ad1a3f7SMatthew Dillon 			) {
10392ad1a3f7SMatthew Dillon 				r = 0;
10402ad1a3f7SMatthew Dillon 				break;
10412ad1a3f7SMatthew Dillon 			}
10422ad1a3f7SMatthew Dillon 		}
10432ad1a3f7SMatthew Dillon 
10442ad1a3f7SMatthew Dillon 		/*
10452ad1a3f7SMatthew Dillon 		 * Check for busy page
10462ad1a3f7SMatthew Dillon 		 */
10472ad1a3f7SMatthew Dillon 
10482ad1a3f7SMatthew Dillon 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
10492ad1a3f7SMatthew Dillon 			vm_page_t pp;
10502ad1a3f7SMatthew Dillon 
10512ad1a3f7SMatthew Dillon 			if (op & OBSC_COLLAPSE_NOWAIT) {
10522ad1a3f7SMatthew Dillon 				if (
10532ad1a3f7SMatthew Dillon 				    (p->flags & PG_BUSY) ||
10542ad1a3f7SMatthew Dillon 				    !p->valid ||
10552ad1a3f7SMatthew Dillon 				    p->hold_count ||
10562ad1a3f7SMatthew Dillon 				    p->wire_count ||
10572ad1a3f7SMatthew Dillon 				    p->busy
10582ad1a3f7SMatthew Dillon 				) {
10592ad1a3f7SMatthew Dillon 					p = next;
10602ad1a3f7SMatthew Dillon 					continue;
10612ad1a3f7SMatthew Dillon 				}
10622ad1a3f7SMatthew Dillon 			} else if (op & OBSC_COLLAPSE_WAIT) {
10632ad1a3f7SMatthew Dillon 				if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
10642ad1a3f7SMatthew Dillon 					/*
10652ad1a3f7SMatthew Dillon 					 * If we slept, anything could have
10662ad1a3f7SMatthew Dillon 					 * happened.  Since the object is
10672ad1a3f7SMatthew Dillon 					 * marked dead, the backing offset
10682ad1a3f7SMatthew Dillon 					 * should not have changed so we
10692ad1a3f7SMatthew Dillon 					 * just restart our scan.
10702ad1a3f7SMatthew Dillon 					 */
10712ad1a3f7SMatthew Dillon 					p = TAILQ_FIRST(&backing_object->memq);
10722ad1a3f7SMatthew Dillon 					continue;
10732ad1a3f7SMatthew Dillon 				}
10742ad1a3f7SMatthew Dillon 			}
10752ad1a3f7SMatthew Dillon 
10762ad1a3f7SMatthew Dillon 			/*
10772ad1a3f7SMatthew Dillon 			 * Busy the page
10782ad1a3f7SMatthew Dillon 			 */
10792ad1a3f7SMatthew Dillon 			vm_page_busy(p);
10802ad1a3f7SMatthew Dillon 
10812ad1a3f7SMatthew Dillon 			KASSERT(
10822ad1a3f7SMatthew Dillon 			    p->object == backing_object,
10832ad1a3f7SMatthew Dillon 			    ("vm_object_qcollapse(): object mismatch")
10842ad1a3f7SMatthew Dillon 			);
10852ad1a3f7SMatthew Dillon 
10862ad1a3f7SMatthew Dillon 			/*
10872ad1a3f7SMatthew Dillon 			 * Destroy any associated swap
10882ad1a3f7SMatthew Dillon 			 */
10892ad1a3f7SMatthew Dillon 			if (backing_object->type == OBJT_SWAP) {
10902ad1a3f7SMatthew Dillon 				swap_pager_freespace(
10912ad1a3f7SMatthew Dillon 				    backing_object,
10922ad1a3f7SMatthew Dillon 				    p->pindex,
10932ad1a3f7SMatthew Dillon 				    1
10942ad1a3f7SMatthew Dillon 				);
10952ad1a3f7SMatthew Dillon 			}
10962ad1a3f7SMatthew Dillon 
10972ad1a3f7SMatthew Dillon 			if (
10982ad1a3f7SMatthew Dillon 			    p->pindex < backing_offset_index ||
10992ad1a3f7SMatthew Dillon 			    new_pindex >= object->size
11002ad1a3f7SMatthew Dillon 			) {
11012ad1a3f7SMatthew Dillon 				/*
11022ad1a3f7SMatthew Dillon 				 * Page is out of the parent object's range, we
11032ad1a3f7SMatthew Dillon 				 * can simply destroy it.
11042ad1a3f7SMatthew Dillon 				 */
11052ad1a3f7SMatthew Dillon 				vm_page_protect(p, VM_PROT_NONE);
11062ad1a3f7SMatthew Dillon 				vm_page_free(p);
11072ad1a3f7SMatthew Dillon 				p = next;
11082ad1a3f7SMatthew Dillon 				continue;
11092ad1a3f7SMatthew Dillon 			}
11102ad1a3f7SMatthew Dillon 
11112ad1a3f7SMatthew Dillon 			pp = vm_page_lookup(object, new_pindex);
11122ad1a3f7SMatthew Dillon 			if (
11132ad1a3f7SMatthew Dillon 			    pp != NULL ||
11142ad1a3f7SMatthew Dillon 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
11152ad1a3f7SMatthew Dillon 			) {
11162ad1a3f7SMatthew Dillon 				/*
11172ad1a3f7SMatthew Dillon 				 * page already exists in parent OR swap exists
11182ad1a3f7SMatthew Dillon 				 * for this location in the parent.  Destroy
11192ad1a3f7SMatthew Dillon 				 * the original page from the backing object.
11202ad1a3f7SMatthew Dillon 				 *
11212ad1a3f7SMatthew Dillon 				 * Leave the parent's page alone
11222ad1a3f7SMatthew Dillon 				 */
11232ad1a3f7SMatthew Dillon 				vm_page_protect(p, VM_PROT_NONE);
11242ad1a3f7SMatthew Dillon 				vm_page_free(p);
11252ad1a3f7SMatthew Dillon 				p = next;
11262ad1a3f7SMatthew Dillon 				continue;
11272ad1a3f7SMatthew Dillon 			}
11282ad1a3f7SMatthew Dillon 
11292ad1a3f7SMatthew Dillon 			/*
11302ad1a3f7SMatthew Dillon 			 * Page does not exist in parent, rename the
11312ad1a3f7SMatthew Dillon 			 * page from the backing object to the main object.
1132d1bf5d56SMatthew Dillon 			 *
1133d1bf5d56SMatthew Dillon 			 * If the page was mapped to a process, it can remain
1134d1bf5d56SMatthew Dillon 			 * mapped through the rename.
11352ad1a3f7SMatthew Dillon 			 */
11362ad1a3f7SMatthew Dillon 			if ((p->queue - p->pc) == PQ_CACHE)
11372ad1a3f7SMatthew Dillon 				vm_page_deactivate(p);
11382ad1a3f7SMatthew Dillon 
11392ad1a3f7SMatthew Dillon 			vm_page_rename(p, object, new_pindex);
11402ad1a3f7SMatthew Dillon 			/* page automatically made dirty by rename */
11412ad1a3f7SMatthew Dillon 		}
11422ad1a3f7SMatthew Dillon 		p = next;
11432ad1a3f7SMatthew Dillon 	}
11442ad1a3f7SMatthew Dillon 	splx(s);
11452ad1a3f7SMatthew Dillon 	return(r);
11462ad1a3f7SMatthew Dillon }
11472ad1a3f7SMatthew Dillon 
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes /*
11502fe6e4d7SDavid Greenman  * this version of collapse allows the operation to occur earlier and
11512fe6e4d7SDavid Greenman  * when paging_in_progress is true for an object...  This is not a complete
11522fe6e4d7SDavid Greenman  * operation, but should plug 99.9% of the rest of the leaks.
11532fe6e4d7SDavid Greenman  */
11542fe6e4d7SDavid Greenman static void
11552fe6e4d7SDavid Greenman vm_object_qcollapse(object)
1156d031cff1SMatthew Dillon 	vm_object_t object;
11572fe6e4d7SDavid Greenman {
11582ad1a3f7SMatthew Dillon 	vm_object_t backing_object = object->backing_object;
11592fe6e4d7SDavid Greenman 
11602fe6e4d7SDavid Greenman 	if (backing_object->ref_count != 1)
11612fe6e4d7SDavid Greenman 		return;
11622fe6e4d7SDavid Greenman 
1163010cf3b9SDavid Greenman 	backing_object->ref_count += 2;
1164010cf3b9SDavid Greenman 
11652ad1a3f7SMatthew Dillon 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
11661c7c3c6aSMatthew Dillon 
1167010cf3b9SDavid Greenman 	backing_object->ref_count -= 2;
11682fe6e4d7SDavid Greenman }
11692fe6e4d7SDavid Greenman 
1170df8bae1dSRodney W. Grimes /*
1171df8bae1dSRodney W. Grimes  *	vm_object_collapse:
1172df8bae1dSRodney W. Grimes  *
1173df8bae1dSRodney W. Grimes  *	Collapse an object with the object backing it.
1174df8bae1dSRodney W. Grimes  *	Pages in the backing object are moved into the
1175df8bae1dSRodney W. Grimes  *	parent, and the backing object is deallocated.
1176df8bae1dSRodney W. Grimes  */
117726f9a767SRodney W. Grimes void
117826f9a767SRodney W. Grimes vm_object_collapse(object)
117924a1cce3SDavid Greenman 	vm_object_t object;
1180df8bae1dSRodney W. Grimes {
1181df8bae1dSRodney W. Grimes 	while (TRUE) {
11822ad1a3f7SMatthew Dillon 		vm_object_t backing_object;
11832ad1a3f7SMatthew Dillon 
1184df8bae1dSRodney W. Grimes 		/*
1185df8bae1dSRodney W. Grimes 		 * Verify that the conditions are right for collapse:
1186df8bae1dSRodney W. Grimes 		 *
11872ad1a3f7SMatthew Dillon 		 * The object exists and the backing object exists.
1188df8bae1dSRodney W. Grimes 		 */
11892fe6e4d7SDavid Greenman 		if (object == NULL)
11902ad1a3f7SMatthew Dillon 			break;
1191df8bae1dSRodney W. Grimes 
119224a1cce3SDavid Greenman 		if ((backing_object = object->backing_object) == NULL)
11932ad1a3f7SMatthew Dillon 			break;
1194df8bae1dSRodney W. Grimes 
1195f919ebdeSDavid Greenman 		/*
1196f919ebdeSDavid Greenman 		 * we check the backing object first, because it is most likely
119724a1cce3SDavid Greenman 		 * not collapsable.
1198f919ebdeSDavid Greenman 		 */
119924a1cce3SDavid Greenman 		if (backing_object->handle != NULL ||
120024a1cce3SDavid Greenman 		    (backing_object->type != OBJT_DEFAULT &&
120124a1cce3SDavid Greenman 		     backing_object->type != OBJT_SWAP) ||
1202f919ebdeSDavid Greenman 		    (backing_object->flags & OBJ_DEAD) ||
120324a1cce3SDavid Greenman 		    object->handle != NULL ||
120424a1cce3SDavid Greenman 		    (object->type != OBJT_DEFAULT &&
120524a1cce3SDavid Greenman 		     object->type != OBJT_SWAP) ||
120624a1cce3SDavid Greenman 		    (object->flags & OBJ_DEAD)) {
12072ad1a3f7SMatthew Dillon 			break;
120824a1cce3SDavid Greenman 		}
12099b4814bbSDavid Greenman 
12102ad1a3f7SMatthew Dillon 		if (
12112ad1a3f7SMatthew Dillon 		    object->paging_in_progress != 0 ||
12122ad1a3f7SMatthew Dillon 		    backing_object->paging_in_progress != 0
12132ad1a3f7SMatthew Dillon 		) {
1214b9921222SDavid Greenman 			vm_object_qcollapse(object);
12152ad1a3f7SMatthew Dillon 			break;
1216df8bae1dSRodney W. Grimes 		}
1217f919ebdeSDavid Greenman 
121826f9a767SRodney W. Grimes 		/*
12190d94caffSDavid Greenman 		 * We know that we can either collapse the backing object (if
12202ad1a3f7SMatthew Dillon 		 * the parent is the only reference to it) or (perhaps) have
12212ad1a3f7SMatthew Dillon 		 * the parent bypass the object if the parent happens to shadow
12222ad1a3f7SMatthew Dillon 		 * all the resident pages in the entire backing object.
12232ad1a3f7SMatthew Dillon 		 *
12242ad1a3f7SMatthew Dillon 		 * This is ignoring pager-backed pages such as swap pages.
12252ad1a3f7SMatthew Dillon 		 * vm_object_backing_scan fails the shadowing test in this
12262ad1a3f7SMatthew Dillon 		 * case.
1227df8bae1dSRodney W. Grimes 		 */
1228df8bae1dSRodney W. Grimes 
1229df8bae1dSRodney W. Grimes 		if (backing_object->ref_count == 1) {
1230df8bae1dSRodney W. Grimes 			/*
12312ad1a3f7SMatthew Dillon 			 * If there is exactly one reference to the backing
12322ad1a3f7SMatthew Dillon 			 * object, we can collapse it into the parent.
1233df8bae1dSRodney W. Grimes 			 */
1234df8bae1dSRodney W. Grimes 
12352ad1a3f7SMatthew Dillon 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1236df8bae1dSRodney W. Grimes 
1237df8bae1dSRodney W. Grimes 			/*
1238df8bae1dSRodney W. Grimes 			 * Move the pager from backing_object to object.
1239df8bae1dSRodney W. Grimes 			 */
1240df8bae1dSRodney W. Grimes 
124124a1cce3SDavid Greenman 			if (backing_object->type == OBJT_SWAP) {
1242d474eaaaSDoug Rabson 				vm_object_pip_add(backing_object, 1);
124324a1cce3SDavid Greenman 
124424a1cce3SDavid Greenman 				/*
12451c7c3c6aSMatthew Dillon 				 * scrap the paging_offset junk and do a
12461c7c3c6aSMatthew Dillon 				 * discrete copy.  This also removes major
12471c7c3c6aSMatthew Dillon 				 * assumptions about how the swap-pager
12481c7c3c6aSMatthew Dillon 				 * works from where it doesn't belong.  The
12491c7c3c6aSMatthew Dillon 				 * new swapper is able to optimize the
12501c7c3c6aSMatthew Dillon 				 * destroy-source case.
125124a1cce3SDavid Greenman 				 */
12521c7c3c6aSMatthew Dillon 
12531c7c3c6aSMatthew Dillon 				vm_object_pip_add(object, 1);
12541c7c3c6aSMatthew Dillon 				swap_pager_copy(
12551c7c3c6aSMatthew Dillon 				    backing_object,
12561c7c3c6aSMatthew Dillon 				    object,
12571c7c3c6aSMatthew Dillon 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1258f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
1259c0503609SDavid Greenman 
1260f919ebdeSDavid Greenman 				vm_object_pip_wakeup(backing_object);
1261c0503609SDavid Greenman 			}
1262df8bae1dSRodney W. Grimes 			/*
1263df8bae1dSRodney W. Grimes 			 * Object now shadows whatever backing_object did.
12642ad1a3f7SMatthew Dillon 			 * Note that the reference to
12652ad1a3f7SMatthew Dillon 			 * backing_object->backing_object moves from within
12662ad1a3f7SMatthew Dillon 			 * backing_object to within object.
1267df8bae1dSRodney W. Grimes 			 */
1268df8bae1dSRodney W. Grimes 
12692ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
12702ad1a3f7SMatthew Dillon 			    &object->backing_object->shadow_head,
12712ad1a3f7SMatthew Dillon 			    object,
12722ad1a3f7SMatthew Dillon 			    shadow_list
12732ad1a3f7SMatthew Dillon 			);
1274eaf13dd7SJohn Dyson 			object->backing_object->shadow_count--;
1275eaf13dd7SJohn Dyson 			object->backing_object->generation++;
1276de5f6a77SJohn Dyson 			if (backing_object->backing_object) {
12772ad1a3f7SMatthew Dillon 				TAILQ_REMOVE(
12782ad1a3f7SMatthew Dillon 				    &backing_object->backing_object->shadow_head,
12792ad1a3f7SMatthew Dillon 				    backing_object,
12802ad1a3f7SMatthew Dillon 				    shadow_list
12812ad1a3f7SMatthew Dillon 				);
1282eaf13dd7SJohn Dyson 				backing_object->backing_object->shadow_count--;
1283eaf13dd7SJohn Dyson 				backing_object->backing_object->generation++;
1284de5f6a77SJohn Dyson 			}
128524a1cce3SDavid Greenman 			object->backing_object = backing_object->backing_object;
1286de5f6a77SJohn Dyson 			if (object->backing_object) {
12872ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
12882ad1a3f7SMatthew Dillon 				    &object->backing_object->shadow_head,
12892ad1a3f7SMatthew Dillon 				    object,
12902ad1a3f7SMatthew Dillon 				    shadow_list
12912ad1a3f7SMatthew Dillon 				);
1292eaf13dd7SJohn Dyson 				object->backing_object->shadow_count++;
1293eaf13dd7SJohn Dyson 				object->backing_object->generation++;
1294de5f6a77SJohn Dyson 			}
12952fe6e4d7SDavid Greenman 
12962ad1a3f7SMatthew Dillon 			object->backing_object_offset +=
12972ad1a3f7SMatthew Dillon 			    backing_object->backing_object_offset;
12982ad1a3f7SMatthew Dillon 
1299df8bae1dSRodney W. Grimes 			/*
1300df8bae1dSRodney W. Grimes 			 * Discard backing_object.
1301df8bae1dSRodney W. Grimes 			 *
13020d94caffSDavid Greenman 			 * Since the backing object has no pages, no pager left,
13030d94caffSDavid Greenman 			 * and no object references within it, all that is
13040d94caffSDavid Greenman 			 * necessary is to dispose of it.
1305df8bae1dSRodney W. Grimes 			 */
1306df8bae1dSRodney W. Grimes 
13072ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
13082ad1a3f7SMatthew Dillon 			    &vm_object_list,
13092ad1a3f7SMatthew Dillon 			    backing_object,
13102ad1a3f7SMatthew Dillon 			    object_list
13112ad1a3f7SMatthew Dillon 			);
1312df8bae1dSRodney W. Grimes 			vm_object_count--;
1313df8bae1dSRodney W. Grimes 
131499448ed1SJohn Dyson 			zfree(obj_zone, backing_object);
1315df8bae1dSRodney W. Grimes 
1316df8bae1dSRodney W. Grimes 			object_collapses++;
13170d94caffSDavid Greenman 		} else {
131895e5e988SJohn Dyson 			vm_object_t new_backing_object;
1319df8bae1dSRodney W. Grimes 
1320df8bae1dSRodney W. Grimes 			/*
13212ad1a3f7SMatthew Dillon 			 * If we do not entirely shadow the backing object,
13222ad1a3f7SMatthew Dillon 			 * there is nothing we can do so we give up.
1323df8bae1dSRodney W. Grimes 			 */
1324df8bae1dSRodney W. Grimes 
13252ad1a3f7SMatthew Dillon 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
13262ad1a3f7SMatthew Dillon 				break;
132724a1cce3SDavid Greenman 			}
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes 			/*
13300d94caffSDavid Greenman 			 * Make the parent shadow the next object in the
13310d94caffSDavid Greenman 			 * chain.  Deallocating backing_object will not remove
13320d94caffSDavid Greenman 			 * it, since its reference count is at least 2.
1333df8bae1dSRodney W. Grimes 			 */
1334df8bae1dSRodney W. Grimes 
13352ad1a3f7SMatthew Dillon 			TAILQ_REMOVE(
13362ad1a3f7SMatthew Dillon 			    &backing_object->shadow_head,
13372ad1a3f7SMatthew Dillon 			    object,
13382ad1a3f7SMatthew Dillon 			    shadow_list
13392ad1a3f7SMatthew Dillon 			);
1340eaf13dd7SJohn Dyson 			backing_object->shadow_count--;
1341eaf13dd7SJohn Dyson 			backing_object->generation++;
134295e5e988SJohn Dyson 
134395e5e988SJohn Dyson 			new_backing_object = backing_object->backing_object;
13448aef1712SMatthew Dillon 			if ((object->backing_object = new_backing_object) != NULL) {
134595e5e988SJohn Dyson 				vm_object_reference(new_backing_object);
13462ad1a3f7SMatthew Dillon 				TAILQ_INSERT_TAIL(
13472ad1a3f7SMatthew Dillon 				    &new_backing_object->shadow_head,
13482ad1a3f7SMatthew Dillon 				    object,
13492ad1a3f7SMatthew Dillon 				    shadow_list
13502ad1a3f7SMatthew Dillon 				);
1351eaf13dd7SJohn Dyson 				new_backing_object->shadow_count++;
1352eaf13dd7SJohn Dyson 				new_backing_object->generation++;
135395e5e988SJohn Dyson 				object->backing_object_offset +=
135495e5e988SJohn Dyson 					backing_object->backing_object_offset;
1355de5f6a77SJohn Dyson 			}
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes 			/*
13580d94caffSDavid Greenman 			 * Drop the reference count on backing_object. Since
13590d94caffSDavid Greenman 			 * its ref_count was at least 2, it will not vanish;
1360eaf13dd7SJohn Dyson 			 * so we don't need to call vm_object_deallocate, but
1361eaf13dd7SJohn Dyson 			 * we do anyway.
1362df8bae1dSRodney W. Grimes 			 */
136395e5e988SJohn Dyson 			vm_object_deallocate(backing_object);
1364df8bae1dSRodney W. Grimes 			object_bypasses++;
1365df8bae1dSRodney W. Grimes 		}
1366df8bae1dSRodney W. Grimes 
1367df8bae1dSRodney W. Grimes 		/*
1368df8bae1dSRodney W. Grimes 		 * Try again with this object's new backing object.
1369df8bae1dSRodney W. Grimes 		 */
1370df8bae1dSRodney W. Grimes 	}
1371df8bae1dSRodney W. Grimes }
1372df8bae1dSRodney W. Grimes 
1373df8bae1dSRodney W. Grimes /*
1374df8bae1dSRodney W. Grimes  *	vm_object_page_remove: [internal]
1375df8bae1dSRodney W. Grimes  *
1376df8bae1dSRodney W. Grimes  *	Removes all physical pages in the specified
1377df8bae1dSRodney W. Grimes  *	object range from the object's list of pages.
1378df8bae1dSRodney W. Grimes  *
1379df8bae1dSRodney W. Grimes  *	The object must be locked.
1380df8bae1dSRodney W. Grimes  */
138126f9a767SRodney W. Grimes void
13827c1f6cedSDavid Greenman vm_object_page_remove(object, start, end, clean_only)
1383d031cff1SMatthew Dillon 	vm_object_t object;
1384d031cff1SMatthew Dillon 	vm_pindex_t start;
1385d031cff1SMatthew Dillon 	vm_pindex_t end;
13867c1f6cedSDavid Greenman 	boolean_t clean_only;
1387df8bae1dSRodney W. Grimes {
1388d031cff1SMatthew Dillon 	vm_page_t p, next;
1389a316d390SJohn Dyson 	unsigned int size;
1390f5ef029eSPoul-Henning Kamp 	int all;
1391df8bae1dSRodney W. Grimes 
1392a1a54e9fSAlan Cox 	if (object == NULL ||
1393a1a54e9fSAlan Cox 	    object->resident_page_count == 0)
1394df8bae1dSRodney W. Grimes 		return;
1395df8bae1dSRodney W. Grimes 
139695e5e988SJohn Dyson 	all = ((end == 0) && (start == 0));
139795e5e988SJohn Dyson 
13988b03c8edSMatthew Dillon 	/*
13998b03c8edSMatthew Dillon 	 * Since physically-backed objects do not use managed pages, we can't
14008b03c8edSMatthew Dillon 	 * remove pages from the object (we must instead remove the page
14018b03c8edSMatthew Dillon 	 * references, and then destroy the object).
14028b03c8edSMatthew Dillon 	 */
14038b03c8edSMatthew Dillon 	KASSERT(object->type != OBJT_PHYS, ("attempt to remove pages from a physical object"));
14048b03c8edSMatthew Dillon 
1405d474eaaaSDoug Rabson 	vm_object_pip_add(object, 1);
140626f9a767SRodney W. Grimes again:
140726f9a767SRodney W. Grimes 	size = end - start;
1408a1a54e9fSAlan Cox 	if (all || size > object->resident_page_count / 4) {
1409b18bfc3dSJohn Dyson 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1410b18bfc3dSJohn Dyson 			next = TAILQ_NEXT(p, listq);
141195e5e988SJohn Dyson 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1412bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1413bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1414ce65e68cSDavid Greenman 					if (!clean_only)
1415bd7e5f99SJohn Dyson 						p->valid = 0;
14160d94caffSDavid Greenman 					continue;
14170d94caffSDavid Greenman 				}
14180891ef4cSJohn Dyson 
1419b18bfc3dSJohn Dyson 				/*
1420b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1421b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1422b18bfc3dSJohn Dyson 				 */
1423ffc82b0aSJohn Dyson 
14241c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
142526f9a767SRodney W. Grimes  					goto again;
14260891ef4cSJohn Dyson 
14278f9110f6SJohn Dyson 				if (clean_only && p->valid) {
14287c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
14297c1f6cedSDavid Greenman 					if (p->valid & p->dirty)
14307c1f6cedSDavid Greenman 						continue;
14317c1f6cedSDavid Greenman 				}
1432eaf13dd7SJohn Dyson 
1433e69763a3SDoug Rabson 				vm_page_busy(p);
1434f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
1435df8bae1dSRodney W. Grimes 				vm_page_free(p);
143626f9a767SRodney W. Grimes 			}
143726f9a767SRodney W. Grimes 		}
143826f9a767SRodney W. Grimes 	} else {
143926f9a767SRodney W. Grimes 		while (size > 0) {
1440bd7e5f99SJohn Dyson 			if ((p = vm_page_lookup(object, start)) != 0) {
1441eaf13dd7SJohn Dyson 
1442bd7e5f99SJohn Dyson 				if (p->wire_count != 0) {
1443bd7e5f99SJohn Dyson 					vm_page_protect(p, VM_PROT_NONE);
1444ce65e68cSDavid Greenman 					if (!clean_only)
1445ce65e68cSDavid Greenman 						p->valid = 0;
1446bd7e5f99SJohn Dyson 					start += 1;
1447bd7e5f99SJohn Dyson 					size -= 1;
1448bd7e5f99SJohn Dyson 					continue;
14490d94caffSDavid Greenman 				}
1450eaf13dd7SJohn Dyson 
1451b18bfc3dSJohn Dyson 				/*
1452b18bfc3dSJohn Dyson 				 * The busy flags are only cleared at
1453b18bfc3dSJohn Dyson 				 * interrupt -- minimize the spl transitions
1454b18bfc3dSJohn Dyson 				 */
14551c7c3c6aSMatthew Dillon  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
145626f9a767SRodney W. Grimes 					goto again;
1457eaf13dd7SJohn Dyson 
14588f9110f6SJohn Dyson 				if (clean_only && p->valid) {
14597c1f6cedSDavid Greenman 					vm_page_test_dirty(p);
1460bd7e5f99SJohn Dyson 					if (p->valid & p->dirty) {
1461bd7e5f99SJohn Dyson 						start += 1;
1462bd7e5f99SJohn Dyson 						size -= 1;
14637c1f6cedSDavid Greenman 						continue;
14647c1f6cedSDavid Greenman 					}
1465bd7e5f99SJohn Dyson 				}
1466eaf13dd7SJohn Dyson 
1467e69763a3SDoug Rabson 				vm_page_busy(p);
1468f919ebdeSDavid Greenman 				vm_page_protect(p, VM_PROT_NONE);
146926f9a767SRodney W. Grimes 				vm_page_free(p);
147026f9a767SRodney W. Grimes 			}
1471a316d390SJohn Dyson 			start += 1;
1472a316d390SJohn Dyson 			size -= 1;
1473df8bae1dSRodney W. Grimes 		}
1474df8bae1dSRodney W. Grimes 	}
1475f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object);
1476c0503609SDavid Greenman }
1477df8bae1dSRodney W. Grimes 
1478df8bae1dSRodney W. Grimes /*
1479df8bae1dSRodney W. Grimes  *	Routine:	vm_object_coalesce
1480df8bae1dSRodney W. Grimes  *	Function:	Coalesces two objects backing up adjoining
1481df8bae1dSRodney W. Grimes  *			regions of memory into a single object.
1482df8bae1dSRodney W. Grimes  *
1483df8bae1dSRodney W. Grimes  *	returns TRUE if objects were combined.
1484df8bae1dSRodney W. Grimes  *
1485df8bae1dSRodney W. Grimes  *	NOTE:	Only works at the moment if the second object is NULL -
1486df8bae1dSRodney W. Grimes  *		if it's not, which object do we lock first?
1487df8bae1dSRodney W. Grimes  *
1488df8bae1dSRodney W. Grimes  *	Parameters:
1489df8bae1dSRodney W. Grimes  *		prev_object	First object to coalesce
1490df8bae1dSRodney W. Grimes  *		prev_offset	Offset into prev_object
1491df8bae1dSRodney W. Grimes  *		next_object	Second object into coalesce
1492df8bae1dSRodney W. Grimes  *		next_offset	Offset into next_object
1493df8bae1dSRodney W. Grimes  *
1494df8bae1dSRodney W. Grimes  *		prev_size	Size of reference to prev_object
1495df8bae1dSRodney W. Grimes  *		next_size	Size of reference to next_object
1496df8bae1dSRodney W. Grimes  *
1497df8bae1dSRodney W. Grimes  *	Conditions:
1498df8bae1dSRodney W. Grimes  *	The object must *not* be locked.
1499df8bae1dSRodney W. Grimes  */
15000d94caffSDavid Greenman boolean_t
1501a316d390SJohn Dyson vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1502d031cff1SMatthew Dillon 	vm_object_t prev_object;
1503a316d390SJohn Dyson 	vm_pindex_t prev_pindex;
1504df8bae1dSRodney W. Grimes 	vm_size_t prev_size, next_size;
1505df8bae1dSRodney W. Grimes {
1506ea41812fSAlan Cox 	vm_pindex_t next_pindex;
1507df8bae1dSRodney W. Grimes 
1508df8bae1dSRodney W. Grimes 	if (prev_object == NULL) {
1509df8bae1dSRodney W. Grimes 		return (TRUE);
1510df8bae1dSRodney W. Grimes 	}
1511df8bae1dSRodney W. Grimes 
15124112823fSMatthew Dillon 	if (prev_object->type != OBJT_DEFAULT &&
15134112823fSMatthew Dillon 	    prev_object->type != OBJT_SWAP) {
151430dcfc09SJohn Dyson 		return (FALSE);
151530dcfc09SJohn Dyson 	}
151630dcfc09SJohn Dyson 
1517df8bae1dSRodney W. Grimes 	/*
1518df8bae1dSRodney W. Grimes 	 * Try to collapse the object first
1519df8bae1dSRodney W. Grimes 	 */
1520df8bae1dSRodney W. Grimes 	vm_object_collapse(prev_object);
1521df8bae1dSRodney W. Grimes 
1522df8bae1dSRodney W. Grimes 	/*
15230d94caffSDavid Greenman 	 * Can't coalesce if: . more than one reference . paged out . shadows
15240d94caffSDavid Greenman 	 * another object . has a copy elsewhere (any of which mean that the
15250d94caffSDavid Greenman 	 * pages not mapped to prev_entry may be in use anyway)
1526df8bae1dSRodney W. Grimes 	 */
1527df8bae1dSRodney W. Grimes 
15288cc7e047SJohn Dyson 	if (prev_object->backing_object != NULL) {
1529df8bae1dSRodney W. Grimes 		return (FALSE);
1530df8bae1dSRodney W. Grimes 	}
1531a316d390SJohn Dyson 
1532a316d390SJohn Dyson 	prev_size >>= PAGE_SHIFT;
1533a316d390SJohn Dyson 	next_size >>= PAGE_SHIFT;
1534ea41812fSAlan Cox 	next_pindex = prev_pindex + prev_size;
15358cc7e047SJohn Dyson 
15368cc7e047SJohn Dyson 	if ((prev_object->ref_count > 1) &&
1537ea41812fSAlan Cox 	    (prev_object->size != next_pindex)) {
15388cc7e047SJohn Dyson 		return (FALSE);
15398cc7e047SJohn Dyson 	}
15408cc7e047SJohn Dyson 
1541df8bae1dSRodney W. Grimes 	/*
15420d94caffSDavid Greenman 	 * Remove any pages that may still be in the object from a previous
15430d94caffSDavid Greenman 	 * deallocation.
1544df8bae1dSRodney W. Grimes 	 */
1545ea41812fSAlan Cox 	if (next_pindex < prev_object->size) {
1546df8bae1dSRodney W. Grimes 		vm_object_page_remove(prev_object,
1547ea41812fSAlan Cox 				      next_pindex,
1548ea41812fSAlan Cox 				      next_pindex + next_size, FALSE);
1549ea41812fSAlan Cox 		if (prev_object->type == OBJT_SWAP)
1550ea41812fSAlan Cox 			swap_pager_freespace(prev_object,
1551ea41812fSAlan Cox 					     next_pindex, next_size);
1552ea41812fSAlan Cox 	}
1553df8bae1dSRodney W. Grimes 
1554df8bae1dSRodney W. Grimes 	/*
1555df8bae1dSRodney W. Grimes 	 * Extend the object if necessary.
1556df8bae1dSRodney W. Grimes 	 */
1557ea41812fSAlan Cox 	if (next_pindex + next_size > prev_object->size)
1558ea41812fSAlan Cox 		prev_object->size = next_pindex + next_size;
1559df8bae1dSRodney W. Grimes 
1560df8bae1dSRodney W. Grimes 	return (TRUE);
1561df8bae1dSRodney W. Grimes }
1562df8bae1dSRodney W. Grimes 
1563c7c34a24SBruce Evans #include "opt_ddb.h"
1564c3cb3e12SDavid Greenman #ifdef DDB
1565c7c34a24SBruce Evans #include <sys/kernel.h>
1566c7c34a24SBruce Evans 
1567ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
1568c7c34a24SBruce Evans 
1569c7c34a24SBruce Evans #include <ddb/ddb.h>
1570c7c34a24SBruce Evans 
1571c7c34a24SBruce Evans static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1572c7c34a24SBruce Evans 				       vm_map_entry_t entry));
1573c7c34a24SBruce Evans static int	vm_object_in_map __P((vm_object_t object));
1574c3cb3e12SDavid Greenman 
1575cac597e4SBruce Evans static int
1576a1f6d91cSDavid Greenman _vm_object_in_map(map, object, entry)
1577a1f6d91cSDavid Greenman 	vm_map_t map;
1578a1f6d91cSDavid Greenman 	vm_object_t object;
1579a1f6d91cSDavid Greenman 	vm_map_entry_t entry;
1580a1f6d91cSDavid Greenman {
1581a1f6d91cSDavid Greenman 	vm_map_t tmpm;
1582a1f6d91cSDavid Greenman 	vm_map_entry_t tmpe;
1583a1f6d91cSDavid Greenman 	vm_object_t obj;
1584a1f6d91cSDavid Greenman 	int entcount;
1585a1f6d91cSDavid Greenman 
1586a1f6d91cSDavid Greenman 	if (map == 0)
1587a1f6d91cSDavid Greenman 		return 0;
1588a1f6d91cSDavid Greenman 
1589a1f6d91cSDavid Greenman 	if (entry == 0) {
1590a1f6d91cSDavid Greenman 		tmpe = map->header.next;
1591a1f6d91cSDavid Greenman 		entcount = map->nentries;
1592a1f6d91cSDavid Greenman 		while (entcount-- && (tmpe != &map->header)) {
1593a1f6d91cSDavid Greenman 			if( _vm_object_in_map(map, object, tmpe)) {
1594a1f6d91cSDavid Greenman 				return 1;
1595a1f6d91cSDavid Greenman 			}
1596a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1597a1f6d91cSDavid Greenman 		}
15989fdfe602SMatthew Dillon 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
15999fdfe602SMatthew Dillon 		tmpm = entry->object.sub_map;
1600a1f6d91cSDavid Greenman 		tmpe = tmpm->header.next;
1601a1f6d91cSDavid Greenman 		entcount = tmpm->nentries;
1602a1f6d91cSDavid Greenman 		while (entcount-- && tmpe != &tmpm->header) {
1603a1f6d91cSDavid Greenman 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1604a1f6d91cSDavid Greenman 				return 1;
1605a1f6d91cSDavid Greenman 			}
1606a1f6d91cSDavid Greenman 			tmpe = tmpe->next;
1607a1f6d91cSDavid Greenman 		}
16088aef1712SMatthew Dillon 	} else if ((obj = entry->object.vm_object) != NULL) {
160924a1cce3SDavid Greenman 		for(; obj; obj=obj->backing_object)
1610a1f6d91cSDavid Greenman 			if( obj == object) {
1611a1f6d91cSDavid Greenman 				return 1;
1612a1f6d91cSDavid Greenman 			}
1613a1f6d91cSDavid Greenman 	}
1614a1f6d91cSDavid Greenman 	return 0;
1615a1f6d91cSDavid Greenman }
1616a1f6d91cSDavid Greenman 
1617cac597e4SBruce Evans static int
1618a1f6d91cSDavid Greenman vm_object_in_map( object)
1619a1f6d91cSDavid Greenman 	vm_object_t object;
1620a1f6d91cSDavid Greenman {
1621a1f6d91cSDavid Greenman 	struct proc *p;
1622c0c25570SJake Burkholder 	ALLPROC_LOCK(AP_SHARED);
1623fc2ffbe6SPoul-Henning Kamp 	LIST_FOREACH(p, &allproc, p_list) {
1624a1f6d91cSDavid Greenman 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1625a1f6d91cSDavid Greenman 			continue;
1626553629ebSJake Burkholder 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0)) {
1627c0c25570SJake Burkholder 			ALLPROC_LOCK(AP_RELEASE);
1628a1f6d91cSDavid Greenman 			return 1;
1629a1f6d91cSDavid Greenman 		}
1630553629ebSJake Burkholder 	}
1631c0c25570SJake Burkholder 	ALLPROC_LOCK(AP_RELEASE);
1632a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kernel_map, object, 0))
1633a1f6d91cSDavid Greenman 		return 1;
1634a1f6d91cSDavid Greenman 	if( _vm_object_in_map( kmem_map, object, 0))
1635a1f6d91cSDavid Greenman 		return 1;
1636a1f6d91cSDavid Greenman 	if( _vm_object_in_map( pager_map, object, 0))
1637a1f6d91cSDavid Greenman 		return 1;
1638a1f6d91cSDavid Greenman 	if( _vm_object_in_map( buffer_map, object, 0))
1639a1f6d91cSDavid Greenman 		return 1;
1640a1f6d91cSDavid Greenman 	if( _vm_object_in_map( mb_map, object, 0))
1641a1f6d91cSDavid Greenman 		return 1;
1642a1f6d91cSDavid Greenman 	return 0;
1643a1f6d91cSDavid Greenman }
1644a1f6d91cSDavid Greenman 
1645c7c34a24SBruce Evans DB_SHOW_COMMAND(vmochk, vm_object_check)
1646f708ef1bSPoul-Henning Kamp {
1647a1f6d91cSDavid Greenman 	vm_object_t object;
1648a1f6d91cSDavid Greenman 
1649a1f6d91cSDavid Greenman 	/*
1650a1f6d91cSDavid Greenman 	 * make sure that internal objs are in a map somewhere
1651a1f6d91cSDavid Greenman 	 * and none have zero ref counts.
1652a1f6d91cSDavid Greenman 	 */
1653b18bfc3dSJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
1654a1f6d91cSDavid Greenman 			object != NULL;
1655b18bfc3dSJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
165624a1cce3SDavid Greenman 		if (object->handle == NULL &&
165724a1cce3SDavid Greenman 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1658a1f6d91cSDavid Greenman 			if (object->ref_count == 0) {
16593efc015bSPeter Wemm 				db_printf("vmochk: internal obj has zero ref count: %ld\n",
16603efc015bSPeter Wemm 					(long)object->size);
1661a1f6d91cSDavid Greenman 			}
1662a1f6d91cSDavid Greenman 			if (!vm_object_in_map(object)) {
1663fc62ef1fSBruce Evans 				db_printf(
1664fc62ef1fSBruce Evans 			"vmochk: internal obj is not in a map: "
1665fc62ef1fSBruce Evans 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1666fc62ef1fSBruce Evans 				    object->ref_count, (u_long)object->size,
1667fc62ef1fSBruce Evans 				    (u_long)object->size,
1668fc62ef1fSBruce Evans 				    (void *)object->backing_object);
1669a1f6d91cSDavid Greenman 			}
1670a1f6d91cSDavid Greenman 		}
1671a1f6d91cSDavid Greenman 	}
1672a1f6d91cSDavid Greenman }
1673a1f6d91cSDavid Greenman 
167426f9a767SRodney W. Grimes /*
1675df8bae1dSRodney W. Grimes  *	vm_object_print:	[ debug ]
1676df8bae1dSRodney W. Grimes  */
1677c7c34a24SBruce Evans DB_SHOW_COMMAND(object, vm_object_print_static)
1678df8bae1dSRodney W. Grimes {
1679c7c34a24SBruce Evans 	/* XXX convert args. */
1680c7c34a24SBruce Evans 	vm_object_t object = (vm_object_t)addr;
1681c7c34a24SBruce Evans 	boolean_t full = have_addr;
1682c7c34a24SBruce Evans 
1683d031cff1SMatthew Dillon 	vm_page_t p;
1684df8bae1dSRodney W. Grimes 
1685c7c34a24SBruce Evans 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1686c7c34a24SBruce Evans #define	count	was_count
1687c7c34a24SBruce Evans 
1688d031cff1SMatthew Dillon 	int count;
1689df8bae1dSRodney W. Grimes 
1690df8bae1dSRodney W. Grimes 	if (object == NULL)
1691df8bae1dSRodney W. Grimes 		return;
1692df8bae1dSRodney W. Grimes 
1693eb95adefSBruce Evans 	db_iprintf(
1694eb95adefSBruce Evans 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1695eb95adefSBruce Evans 	    object, (int)object->type, (u_long)object->size,
1696eb95adefSBruce Evans 	    object->resident_page_count, object->ref_count, object->flags);
1697eb95adefSBruce Evans 	/*
16981c7c3c6aSMatthew Dillon 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
1699eb95adefSBruce Evans 	 */
17001c7c3c6aSMatthew Dillon 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
17011c7c3c6aSMatthew Dillon 	    object->shadow_count,
1702eb95adefSBruce Evans 	    object->backing_object ? object->backing_object->ref_count : 0,
1703eb95adefSBruce Evans 	    object->backing_object, (long)object->backing_object_offset);
1704df8bae1dSRodney W. Grimes 
1705df8bae1dSRodney W. Grimes 	if (!full)
1706df8bae1dSRodney W. Grimes 		return;
1707df8bae1dSRodney W. Grimes 
1708c7c34a24SBruce Evans 	db_indent += 2;
1709df8bae1dSRodney W. Grimes 	count = 0;
1710fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(p, &object->memq, listq) {
1711df8bae1dSRodney W. Grimes 		if (count == 0)
1712c7c34a24SBruce Evans 			db_iprintf("memory:=");
1713df8bae1dSRodney W. Grimes 		else if (count == 6) {
1714c7c34a24SBruce Evans 			db_printf("\n");
1715c7c34a24SBruce Evans 			db_iprintf(" ...");
1716df8bae1dSRodney W. Grimes 			count = 0;
1717df8bae1dSRodney W. Grimes 		} else
1718c7c34a24SBruce Evans 			db_printf(",");
1719df8bae1dSRodney W. Grimes 		count++;
1720df8bae1dSRodney W. Grimes 
1721c7c34a24SBruce Evans 		db_printf("(off=0x%lx,page=0x%lx)",
1722a316d390SJohn Dyson 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1723df8bae1dSRodney W. Grimes 	}
1724df8bae1dSRodney W. Grimes 	if (count != 0)
1725c7c34a24SBruce Evans 		db_printf("\n");
1726c7c34a24SBruce Evans 	db_indent -= 2;
1727df8bae1dSRodney W. Grimes }
17285070c7f8SJohn Dyson 
1729c7c34a24SBruce Evans /* XXX. */
1730c7c34a24SBruce Evans #undef count
1731c7c34a24SBruce Evans 
1732c7c34a24SBruce Evans /* XXX need this non-static entry for calling from vm_map_print. */
17335070c7f8SJohn Dyson void
1734c7c34a24SBruce Evans vm_object_print(addr, have_addr, count, modif)
1735ecbb00a2SDoug Rabson         /* db_expr_t */ long addr;
1736c7c34a24SBruce Evans 	boolean_t have_addr;
1737ecbb00a2SDoug Rabson 	/* db_expr_t */ long count;
1738c7c34a24SBruce Evans 	char *modif;
1739c7c34a24SBruce Evans {
1740c7c34a24SBruce Evans 	vm_object_print_static(addr, have_addr, count, modif);
1741c7c34a24SBruce Evans }
1742c7c34a24SBruce Evans 
1743c7c34a24SBruce Evans DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
17445070c7f8SJohn Dyson {
17455070c7f8SJohn Dyson 	vm_object_t object;
17465070c7f8SJohn Dyson 	int nl = 0;
17475070c7f8SJohn Dyson 	int c;
17485070c7f8SJohn Dyson 	for (object = TAILQ_FIRST(&vm_object_list);
17495070c7f8SJohn Dyson 			object != NULL;
17505070c7f8SJohn Dyson 			object = TAILQ_NEXT(object, object_list)) {
17515070c7f8SJohn Dyson 		vm_pindex_t idx, fidx;
17525070c7f8SJohn Dyson 		vm_pindex_t osize;
17535070c7f8SJohn Dyson 		vm_offset_t pa = -1, padiff;
17545070c7f8SJohn Dyson 		int rcount;
17555070c7f8SJohn Dyson 		vm_page_t m;
17565070c7f8SJohn Dyson 
1757fc62ef1fSBruce Evans 		db_printf("new object: %p\n", (void *)object);
17585070c7f8SJohn Dyson 		if ( nl > 18) {
17595070c7f8SJohn Dyson 			c = cngetc();
17605070c7f8SJohn Dyson 			if (c != ' ')
17615070c7f8SJohn Dyson 				return;
17625070c7f8SJohn Dyson 			nl = 0;
17635070c7f8SJohn Dyson 		}
17645070c7f8SJohn Dyson 		nl++;
17655070c7f8SJohn Dyson 		rcount = 0;
17665070c7f8SJohn Dyson 		fidx = 0;
17675070c7f8SJohn Dyson 		osize = object->size;
17685070c7f8SJohn Dyson 		if (osize > 128)
17695070c7f8SJohn Dyson 			osize = 128;
17705070c7f8SJohn Dyson 		for(idx=0;idx<osize;idx++) {
17715070c7f8SJohn Dyson 			m = vm_page_lookup(object, idx);
17725070c7f8SJohn Dyson 			if (m == NULL) {
17735070c7f8SJohn Dyson 				if (rcount) {
17743efc015bSPeter Wemm 					db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
17753efc015bSPeter Wemm 						(long)fidx, rcount, (long)pa);
17765070c7f8SJohn Dyson 					if ( nl > 18) {
17775070c7f8SJohn Dyson 						c = cngetc();
17785070c7f8SJohn Dyson 						if (c != ' ')
17795070c7f8SJohn Dyson 							return;
17805070c7f8SJohn Dyson 						nl = 0;
17815070c7f8SJohn Dyson 					}
17825070c7f8SJohn Dyson 					nl++;
17835070c7f8SJohn Dyson 					rcount = 0;
17845070c7f8SJohn Dyson 				}
17855070c7f8SJohn Dyson 				continue;
17865070c7f8SJohn Dyson 			}
17875070c7f8SJohn Dyson 
17885070c7f8SJohn Dyson 
17895070c7f8SJohn Dyson 			if (rcount &&
17905070c7f8SJohn Dyson 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
17915070c7f8SJohn Dyson 				++rcount;
17925070c7f8SJohn Dyson 				continue;
17935070c7f8SJohn Dyson 			}
17945070c7f8SJohn Dyson 			if (rcount) {
17955070c7f8SJohn Dyson 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
17965070c7f8SJohn Dyson 				padiff >>= PAGE_SHIFT;
17975070c7f8SJohn Dyson 				padiff &= PQ_L2_MASK;
17985070c7f8SJohn Dyson 				if (padiff == 0) {
17995070c7f8SJohn Dyson 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
18005070c7f8SJohn Dyson 					++rcount;
18015070c7f8SJohn Dyson 					continue;
18025070c7f8SJohn Dyson 				}
18033efc015bSPeter Wemm 				db_printf(" index(%ld)run(%d)pa(0x%lx)",
18043efc015bSPeter Wemm 					(long)fidx, rcount, (long)pa);
18053efc015bSPeter Wemm 				db_printf("pd(%ld)\n", (long)padiff);
18065070c7f8SJohn Dyson 				if ( nl > 18) {
18075070c7f8SJohn Dyson 					c = cngetc();
18085070c7f8SJohn Dyson 					if (c != ' ')
18095070c7f8SJohn Dyson 						return;
18105070c7f8SJohn Dyson 					nl = 0;
18115070c7f8SJohn Dyson 				}
18125070c7f8SJohn Dyson 				nl++;
18135070c7f8SJohn Dyson 			}
18145070c7f8SJohn Dyson 			fidx = idx;
18155070c7f8SJohn Dyson 			pa = VM_PAGE_TO_PHYS(m);
18165070c7f8SJohn Dyson 			rcount = 1;
18175070c7f8SJohn Dyson 		}
18185070c7f8SJohn Dyson 		if (rcount) {
18193efc015bSPeter Wemm 			db_printf(" index(%ld)run(%d)pa(0x%lx)\n",
18203efc015bSPeter Wemm 				(long)fidx, rcount, (long)pa);
18215070c7f8SJohn Dyson 			if ( nl > 18) {
18225070c7f8SJohn Dyson 				c = cngetc();
18235070c7f8SJohn Dyson 				if (c != ' ')
18245070c7f8SJohn Dyson 					return;
18255070c7f8SJohn Dyson 				nl = 0;
18265070c7f8SJohn Dyson 			}
18275070c7f8SJohn Dyson 			nl++;
18285070c7f8SJohn Dyson 		}
18295070c7f8SJohn Dyson 	}
18305070c7f8SJohn Dyson }
1831c3cb3e12SDavid Greenman #endif /* DDB */
1832