xref: /freebsd/sys/vm/vm_object.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: @(#)vm_object.c	8.5 (Berkeley) 3/22/94
37  *
38  *
39  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40  * All rights reserved.
41  *
42  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43  *
44  * Permission to use, copy, modify and distribute this software and
45  * its documentation is hereby granted, provided that both the copyright
46  * notice and this permission notice appear in all copies of the
47  * software, derivative works or modified versions, and any portions
48  * thereof, and that both notices appear in supporting documentation.
49  *
50  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53  *
54  * Carnegie Mellon requests users of this software to return to
55  *
56  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57  *  School of Computer Science
58  *  Carnegie Mellon University
59  *  Pittsburgh PA 15213-3890
60  *
61  * any improvements or extensions that they make and grant Carnegie the
62  * rights to redistribute these changes.
63  *
64  * $Id: vm_object.c,v 1.156 1999/05/30 01:12:19 alc Exp $
65  */
66 
67 /*
68  *	Virtual memory object module.
69  */
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/proc.h>		/* for curproc, pageproc */
74 #include <sys/vnode.h>
75 #include <sys/vmmeter.h>
76 #include <sys/mman.h>
77 #include <sys/mount.h>
78 
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_prot.h>
82 #include <vm/pmap.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_pageout.h>
87 #include <vm/vm_pager.h>
88 #include <vm/swap_pager.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/vm_zone.h>
92 
93 static void	vm_object_qcollapse __P((vm_object_t object));
94 
95 /*
96  *	Virtual memory objects maintain the actual data
97  *	associated with allocated virtual memory.  A given
98  *	page of memory exists within exactly one object.
99  *
100  *	An object is only deallocated when all "references"
101  *	are given up.  Only one "reference" to a given
102  *	region of an object should be writeable.
103  *
104  *	Associated with each object is a list of all resident
105  *	memory pages belonging to that object; this list is
106  *	maintained by the "vm_page" module, and locked by the object's
107  *	lock.
108  *
109  *	Each object also records a "pager" routine which is
110  *	used to retrieve (and store) pages to the proper backing
111  *	storage.  In addition, objects may be backed by other
112  *	objects from which they were virtual-copied.
113  *
114  *	The only items within the object structure which are
115  *	modified after time of creation are:
116  *		reference count		locked by object's lock
117  *		pager routine		locked by object's lock
118  *
119  */
120 
121 struct object_q vm_object_list;
122 #ifndef NULL_SIMPLELOCKS
123 static struct simplelock vm_object_list_lock;
124 #endif
125 static long vm_object_count;		/* count of all objects */
126 vm_object_t kernel_object;
127 vm_object_t kmem_object;
128 static struct vm_object kernel_object_store;
129 static struct vm_object kmem_object_store;
130 extern int vm_pageout_page_count;
131 
132 static long object_collapses;
133 static long object_bypasses;
134 static int next_index;
135 static vm_zone_t obj_zone;
136 static struct vm_zone obj_zone_store;
137 static int object_hash_rand;
138 #define VM_OBJECTS_INIT 256
139 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
140 
141 void
142 _vm_object_allocate(type, size, object)
143 	objtype_t type;
144 	vm_size_t size;
145 	vm_object_t object;
146 {
147 	int incr;
148 	TAILQ_INIT(&object->memq);
149 	TAILQ_INIT(&object->shadow_head);
150 
151 	object->type = type;
152 	object->size = size;
153 	object->ref_count = 1;
154 	object->flags = 0;
155 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
156 		vm_object_set_flag(object, OBJ_ONEMAPPING);
157 	object->behavior = OBJ_NORMAL;
158 	object->paging_in_progress = 0;
159 	object->resident_page_count = 0;
160 	object->shadow_count = 0;
161 	object->pg_color = next_index;
162 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
163 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
164 	else
165 		incr = size;
166 	next_index = (next_index + incr) & PQ_L2_MASK;
167 	object->handle = NULL;
168 	object->backing_object = NULL;
169 	object->backing_object_offset = (vm_ooffset_t) 0;
170 	/*
171 	 * Try to generate a number that will spread objects out in the
172 	 * hash table.  We 'wipe' new objects across the hash in 128 page
173 	 * increments plus 1 more to offset it a little more by the time
174 	 * it wraps around.
175 	 */
176 	object->hash_rand = object_hash_rand - 129;
177 
178 	object->last_read = 0;
179 	object->generation++;
180 
181 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
182 	vm_object_count++;
183 	object_hash_rand = object->hash_rand;
184 }
185 
186 /*
187  *	vm_object_init:
188  *
189  *	Initialize the VM objects module.
190  */
191 void
192 vm_object_init()
193 {
194 	TAILQ_INIT(&vm_object_list);
195 	simple_lock_init(&vm_object_list_lock);
196 	vm_object_count = 0;
197 
198 	kernel_object = &kernel_object_store;
199 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
200 	    kernel_object);
201 
202 	kmem_object = &kmem_object_store;
203 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
204 	    kmem_object);
205 
206 	obj_zone = &obj_zone_store;
207 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
208 		vm_objects_init, VM_OBJECTS_INIT);
209 }
210 
211 void
212 vm_object_init2() {
213 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
214 }
215 
216 /*
217  *	vm_object_allocate:
218  *
219  *	Returns a new object with the given size.
220  */
221 
222 vm_object_t
223 vm_object_allocate(type, size)
224 	objtype_t type;
225 	vm_size_t size;
226 {
227 	vm_object_t result;
228 
229 	result = (vm_object_t) zalloc(obj_zone);
230 
231 	_vm_object_allocate(type, size, result);
232 
233 	return (result);
234 }
235 
236 
237 /*
238  *	vm_object_reference:
239  *
240  *	Gets another reference to the given object.
241  */
242 void
243 vm_object_reference(object)
244 	vm_object_t object;
245 {
246 	if (object == NULL)
247 		return;
248 
249 	KASSERT(!(object->flags & OBJ_DEAD),
250 	    ("vm_object_reference: attempting to reference dead obj"));
251 
252 	object->ref_count++;
253 	if (object->type == OBJT_VNODE) {
254 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
255 #if !defined(MAX_PERF)
256 			printf("vm_object_reference: delay in getting object\n");
257 #endif
258 		}
259 	}
260 }
261 
262 void
263 vm_object_vndeallocate(object)
264 	vm_object_t object;
265 {
266 	struct vnode *vp = (struct vnode *) object->handle;
267 
268 	KASSERT(object->type == OBJT_VNODE,
269 	    ("vm_object_vndeallocate: not a vnode object"));
270 	KASSERT(vp != NULL, ("vm_object_vndeallocate: missing vp"));
271 #ifdef INVARIANTS
272 	if (object->ref_count == 0) {
273 		vprint("vm_object_vndeallocate", vp);
274 		panic("vm_object_vndeallocate: bad object reference count");
275 	}
276 #endif
277 
278 	object->ref_count--;
279 	if (object->ref_count == 0) {
280 		vp->v_flag &= ~VTEXT;
281 		vm_object_clear_flag(object, OBJ_OPT);
282 	}
283 	vrele(vp);
284 }
285 
286 /*
287  *	vm_object_deallocate:
288  *
289  *	Release a reference to the specified object,
290  *	gained either through a vm_object_allocate
291  *	or a vm_object_reference call.  When all references
292  *	are gone, storage associated with this object
293  *	may be relinquished.
294  *
295  *	No object may be locked.
296  */
297 void
298 vm_object_deallocate(object)
299 	vm_object_t object;
300 {
301 	vm_object_t temp;
302 
303 	while (object != NULL) {
304 
305 		if (object->type == OBJT_VNODE) {
306 			vm_object_vndeallocate(object);
307 			return;
308 		}
309 
310 		if (object->ref_count == 0) {
311 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
312 		} else if (object->ref_count > 2) {
313 			object->ref_count--;
314 			return;
315 		}
316 
317 		/*
318 		 * Here on ref_count of one or two, which are special cases for
319 		 * objects.
320 		 */
321 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
322 			vm_object_set_flag(object, OBJ_ONEMAPPING);
323 			object->ref_count--;
324 			return;
325 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
326 			object->ref_count--;
327 			if ((object->handle == NULL) &&
328 			    (object->type == OBJT_DEFAULT ||
329 			     object->type == OBJT_SWAP)) {
330 				vm_object_t robject;
331 
332 				robject = TAILQ_FIRST(&object->shadow_head);
333 				KASSERT(robject != NULL,
334 				    ("vm_object_deallocate: ref_count: %d, shadow_count: %d",
335 					 object->ref_count,
336 					 object->shadow_count));
337 				if ((robject->handle == NULL) &&
338 				    (robject->type == OBJT_DEFAULT ||
339 				     robject->type == OBJT_SWAP)) {
340 
341 					robject->ref_count++;
342 
343 					while (
344 						robject->paging_in_progress ||
345 						object->paging_in_progress
346 					) {
347 						vm_object_pip_sleep(robject, "objde1");
348 						vm_object_pip_sleep(object, "objde2");
349 					}
350 
351 					if (robject->ref_count == 1) {
352 						robject->ref_count--;
353 						object = robject;
354 						goto doterm;
355 					}
356 
357 					object = robject;
358 					vm_object_collapse(object);
359 					continue;
360 				}
361 			}
362 
363 			return;
364 
365 		} else {
366 			object->ref_count--;
367 			if (object->ref_count != 0)
368 				return;
369 		}
370 
371 doterm:
372 
373 		temp = object->backing_object;
374 		if (temp) {
375 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
376 			temp->shadow_count--;
377 			if (temp->ref_count == 0)
378 				vm_object_clear_flag(temp, OBJ_OPT);
379 			temp->generation++;
380 			object->backing_object = NULL;
381 		}
382 		vm_object_terminate(object);
383 		/* unlocks and deallocates object */
384 		object = temp;
385 	}
386 }
387 
388 /*
389  *	vm_object_terminate actually destroys the specified object, freeing
390  *	up all previously used resources.
391  *
392  *	The object must be locked.
393  *	This routine may block.
394  */
395 void
396 vm_object_terminate(object)
397 	vm_object_t object;
398 {
399 	vm_page_t p;
400 	int s;
401 
402 	/*
403 	 * Make sure no one uses us.
404 	 */
405 	vm_object_set_flag(object, OBJ_DEAD);
406 
407 	/*
408 	 * wait for the pageout daemon to be done with the object
409 	 */
410 	vm_object_pip_wait(object, "objtrm");
411 
412 	KASSERT(!object->paging_in_progress,
413 		("vm_object_terminate: pageout in progress"));
414 
415 	/*
416 	 * Clean and free the pages, as appropriate. All references to the
417 	 * object are gone, so we don't need to lock it.
418 	 */
419 	if (object->type == OBJT_VNODE) {
420 		struct vnode *vp;
421 
422 		/*
423 		 * Freeze optimized copies.
424 		 */
425 		vm_freeze_copyopts(object, 0, object->size);
426 
427 		/*
428 		 * Clean pages and flush buffers.
429 		 */
430 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
431 
432 		vp = (struct vnode *) object->handle;
433 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
434 	}
435 
436 	if (object->ref_count != 0)
437 		panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
438 
439 	/*
440 	 * Now free any remaining pages. For internal objects, this also
441 	 * removes them from paging queues. Don't free wired pages, just
442 	 * remove them from the object.
443 	 */
444 	s = splvm();
445 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
446 #if !defined(MAX_PERF)
447 		if (p->busy || (p->flags & PG_BUSY))
448 			panic("vm_object_terminate: freeing busy page %p\n", p);
449 #endif
450 		if (p->wire_count == 0) {
451 			vm_page_busy(p);
452 			vm_page_free(p);
453 			cnt.v_pfree++;
454 		} else {
455 			vm_page_busy(p);
456 			vm_page_remove(p);
457 		}
458 	}
459 	splx(s);
460 
461 	/*
462 	 * Let the pager know object is dead.
463 	 */
464 	vm_pager_deallocate(object);
465 
466 	/*
467 	 * Remove the object from the global object list.
468 	 */
469 	simple_lock(&vm_object_list_lock);
470 	TAILQ_REMOVE(&vm_object_list, object, object_list);
471 	simple_unlock(&vm_object_list_lock);
472 
473 	wakeup(object);
474 
475 	/*
476 	 * Free the space for the object.
477 	 */
478 	zfree(obj_zone, object);
479 }
480 
481 /*
482  *	vm_object_page_clean
483  *
484  *	Clean all dirty pages in the specified range of object.
485  *	Leaves page on whatever queue it is currently on.
486  *
487  *	Odd semantics: if start == end, we clean everything.
488  *
489  *	The object must be locked.
490  */
491 
492 void
493 vm_object_page_clean(object, start, end, flags)
494 	vm_object_t object;
495 	vm_pindex_t start;
496 	vm_pindex_t end;
497 	int flags;
498 {
499 	vm_page_t p, np, tp;
500 	vm_offset_t tstart, tend;
501 	vm_pindex_t pi;
502 	int s;
503 	struct vnode *vp;
504 	int runlen;
505 	int maxf;
506 	int chkb;
507 	int maxb;
508 	int i;
509 	int pagerflags;
510 	vm_page_t maf[vm_pageout_page_count];
511 	vm_page_t mab[vm_pageout_page_count];
512 	vm_page_t ma[vm_pageout_page_count];
513 	int curgeneration;
514 
515 	if (object->type != OBJT_VNODE ||
516 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
517 		return;
518 
519 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
520 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
521 
522 	vp = object->handle;
523 
524 	vm_object_set_flag(object, OBJ_CLEANING);
525 
526 	tstart = start;
527 	if (end == 0) {
528 		tend = object->size;
529 	} else {
530 		tend = end;
531 	}
532 
533 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
534 		vm_page_flag_set(p, PG_CLEANCHK);
535 		vm_page_protect(p, VM_PROT_READ);
536 	}
537 
538 	if ((tstart == 0) && (tend == object->size)) {
539 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
540 	}
541 
542 rescan:
543 	curgeneration = object->generation;
544 
545 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
546 		np = TAILQ_NEXT(p, listq);
547 
548 		pi = p->pindex;
549 		if (((p->flags & PG_CLEANCHK) == 0) ||
550 			(pi < tstart) || (pi >= tend) ||
551 			(p->valid == 0) ||
552 			((p->queue - p->pc) == PQ_CACHE)) {
553 			vm_page_flag_clear(p, PG_CLEANCHK);
554 			continue;
555 		}
556 
557 		vm_page_test_dirty(p);
558 		if ((p->dirty & p->valid) == 0) {
559 			vm_page_flag_clear(p, PG_CLEANCHK);
560 			continue;
561 		}
562 
563 		s = splvm();
564 		while (vm_page_sleep_busy(p, TRUE, "vpcwai")) {
565 			if (object->generation != curgeneration) {
566 				splx(s);
567 				goto rescan;
568 			}
569 		}
570 
571 		maxf = 0;
572 		for(i=1;i<vm_pageout_page_count;i++) {
573 			if ((tp = vm_page_lookup(object, pi + i)) != NULL) {
574 				if ((tp->flags & PG_BUSY) ||
575 					(tp->flags & PG_CLEANCHK) == 0 ||
576 					(tp->busy != 0))
577 					break;
578 				if((tp->queue - tp->pc) == PQ_CACHE) {
579 					vm_page_flag_clear(tp, PG_CLEANCHK);
580 					break;
581 				}
582 				vm_page_test_dirty(tp);
583 				if ((tp->dirty & tp->valid) == 0) {
584 					vm_page_flag_clear(tp, PG_CLEANCHK);
585 					break;
586 				}
587 				maf[ i - 1 ] = tp;
588 				maxf++;
589 				continue;
590 			}
591 			break;
592 		}
593 
594 		maxb = 0;
595 		chkb = vm_pageout_page_count -  maxf;
596 		if (chkb) {
597 			for(i = 1; i < chkb;i++) {
598 				if ((tp = vm_page_lookup(object, pi - i)) != NULL) {
599 					if ((tp->flags & PG_BUSY) ||
600 						(tp->flags & PG_CLEANCHK) == 0 ||
601 						(tp->busy != 0))
602 						break;
603 					if((tp->queue - tp->pc) == PQ_CACHE) {
604 						vm_page_flag_clear(tp, PG_CLEANCHK);
605 						break;
606 					}
607 					vm_page_test_dirty(tp);
608 					if ((tp->dirty & tp->valid) == 0) {
609 						vm_page_flag_clear(tp, PG_CLEANCHK);
610 						break;
611 					}
612 					mab[ i - 1 ] = tp;
613 					maxb++;
614 					continue;
615 				}
616 				break;
617 			}
618 		}
619 
620 		for(i=0;i<maxb;i++) {
621 			int index = (maxb - i) - 1;
622 			ma[index] = mab[i];
623 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
624 		}
625 		vm_page_flag_clear(p, PG_CLEANCHK);
626 		ma[maxb] = p;
627 		for(i=0;i<maxf;i++) {
628 			int index = (maxb + i) + 1;
629 			ma[index] = maf[i];
630 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
631 		}
632 		runlen = maxb + maxf + 1;
633 
634 		splx(s);
635 		vm_pageout_flush(ma, runlen, pagerflags);
636 		for (i = 0; i<runlen; i++) {
637 			if (ma[i]->valid & ma[i]->dirty) {
638 				vm_page_protect(ma[i], VM_PROT_READ);
639 				vm_page_flag_set(ma[i], PG_CLEANCHK);
640 			}
641 		}
642 		if (object->generation != curgeneration)
643 			goto rescan;
644 	}
645 
646 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
647 
648 	vm_object_clear_flag(object, OBJ_CLEANING);
649 	return;
650 }
651 
652 #ifdef not_used
653 /* XXX I cannot tell if this should be an exported symbol */
654 /*
655  *	vm_object_deactivate_pages
656  *
657  *	Deactivate all pages in the specified object.  (Keep its pages
658  *	in memory even though it is no longer referenced.)
659  *
660  *	The object must be locked.
661  */
662 static void
663 vm_object_deactivate_pages(object)
664 	vm_object_t object;
665 {
666 	vm_page_t p, next;
667 
668 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
669 		next = TAILQ_NEXT(p, listq);
670 		vm_page_deactivate(p);
671 	}
672 }
673 #endif
674 
675 /*
676  * Same as vm_object_pmap_copy, except range checking really
677  * works, and is meant for small sections of an object.
678  *
679  * This code protects resident pages by making them read-only
680  * and is typically called on a fork or split when a page
681  * is converted to copy-on-write.
682  *
683  * NOTE: If the page is already at VM_PROT_NONE, calling
684  * vm_page_protect will have no effect.
685  */
686 
687 void
688 vm_object_pmap_copy_1(object, start, end)
689 	vm_object_t object;
690 	vm_pindex_t start;
691 	vm_pindex_t end;
692 {
693 	vm_pindex_t idx;
694 	vm_page_t p;
695 
696 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
697 		return;
698 
699 	for (idx = start; idx < end; idx++) {
700 		p = vm_page_lookup(object, idx);
701 		if (p == NULL)
702 			continue;
703 		vm_page_protect(p, VM_PROT_READ);
704 	}
705 }
706 
707 /*
708  *	vm_object_pmap_remove:
709  *
710  *	Removes all physical pages in the specified
711  *	object range from all physical maps.
712  *
713  *	The object must *not* be locked.
714  */
715 void
716 vm_object_pmap_remove(object, start, end)
717 	vm_object_t object;
718 	vm_pindex_t start;
719 	vm_pindex_t end;
720 {
721 	vm_page_t p;
722 
723 	if (object == NULL)
724 		return;
725 	for (p = TAILQ_FIRST(&object->memq);
726 		p != NULL;
727 		p = TAILQ_NEXT(p, listq)) {
728 		if (p->pindex >= start && p->pindex < end)
729 			vm_page_protect(p, VM_PROT_NONE);
730 	}
731 	if ((start == 0) && (object->size == end))
732 		vm_object_clear_flag(object, OBJ_WRITEABLE);
733 }
734 
735 /*
736  *	vm_object_madvise:
737  *
738  *	Implements the madvise function at the object/page level.
739  *
740  *	Currently, madvise() functions are limited to the default and
741  *	swap object types only, and also limited to only the unshared portions
742  *	of a process's address space.  MADV_FREE, certainly, could never be
743  *	run on anything else.  The others are more flexible and the code could
744  *	be adjusted in the future to handle expanded cases for them.
745  */
746 void
747 vm_object_madvise(object, pindex, count, advise)
748 	vm_object_t object;
749 	vm_pindex_t pindex;
750 	int count;
751 	int advise;
752 {
753 	vm_pindex_t end, tpindex;
754 	vm_object_t tobject;
755 	vm_page_t m;
756 
757 	if (object == NULL)
758 		return;
759 
760 	end = pindex + count;
761 
762 	/*
763 	 * Locate and adjust resident pages
764 	 */
765 
766 	for (; pindex < end; pindex += 1) {
767 relookup:
768 		tobject = object;
769 		tpindex = pindex;
770 shadowlookup:
771 
772 		if (tobject->type != OBJT_DEFAULT &&
773 		    tobject->type != OBJT_SWAP
774 		) {
775 			continue;
776 		}
777 
778 		if ((tobject->flags & OBJ_ONEMAPPING) == 0)
779 			continue;
780 
781 		m = vm_page_lookup(tobject, tpindex);
782 
783 		if (m == NULL) {
784 			/*
785 			 * There may be swap even if there is no backing page
786 			 */
787 			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
788 				swap_pager_freespace(tobject, tpindex, 1);
789 
790 			/*
791 			 * next object
792 			 */
793 			tobject = tobject->backing_object;
794 			if (tobject == NULL)
795 				continue;
796 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
797 			goto shadowlookup;
798 		}
799 
800 		/*
801 		 * If the page is busy or not in a normal active state,
802 		 * we skip it.  Things can break if we mess with pages
803 		 * in any of the below states.
804 		 */
805 		if (
806 		    m->hold_count ||
807 		    m->wire_count ||
808 		    m->valid != VM_PAGE_BITS_ALL
809 		) {
810 			continue;
811 		}
812 
813  		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
814   			goto relookup;
815 
816 		if (advise == MADV_WILLNEED) {
817 			vm_page_activate(m);
818 		} else if (advise == MADV_DONTNEED) {
819 			vm_page_deactivate(m);
820 		} else if (advise == MADV_FREE) {
821 			/*
822 			 * Mark the page clean.  This will allow the page
823 			 * to be freed up by the system.  However, such pages
824 			 * are often reused quickly by malloc()/free()
825 			 * so we do not do anything that would cause
826 			 * a page fault if we can help it.
827 			 *
828 			 * Specifically, we do not try to actually free
829 			 * the page now nor do we try to put it in the
830 			 * cache (which would cause a page fault on reuse).
831 			 *
832 			 * But we do make the page is freeable as we
833 			 * can without actually taking the step of unmapping
834 			 * it.
835 			 */
836 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
837 			m->dirty = 0;
838 			m->act_count = 0;
839 			vm_page_deactivate(m);
840 			if (tobject->type == OBJT_SWAP)
841 				swap_pager_freespace(tobject, tpindex, 1);
842 		}
843 	}
844 }
845 
846 /*
847  *	vm_object_shadow:
848  *
849  *	Create a new object which is backed by the
850  *	specified existing object range.  The source
851  *	object reference is deallocated.
852  *
853  *	The new object and offset into that object
854  *	are returned in the source parameters.
855  */
856 
857 void
858 vm_object_shadow(object, offset, length)
859 	vm_object_t *object;	/* IN/OUT */
860 	vm_ooffset_t *offset;	/* IN/OUT */
861 	vm_size_t length;
862 {
863 	vm_object_t source;
864 	vm_object_t result;
865 
866 	source = *object;
867 
868 	/*
869 	 * Don't create the new object if the old object isn't shared.
870 	 */
871 
872 	if (source != NULL &&
873 	    source->ref_count == 1 &&
874 	    source->handle == NULL &&
875 	    (source->type == OBJT_DEFAULT ||
876 	     source->type == OBJT_SWAP))
877 		return;
878 
879 	/*
880 	 * Allocate a new object with the given length
881 	 */
882 
883 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
884 		panic("vm_object_shadow: no object for shadowing");
885 
886 	/*
887 	 * The new object shadows the source object, adding a reference to it.
888 	 * Our caller changes his reference to point to the new object,
889 	 * removing a reference to the source object.  Net result: no change
890 	 * of reference count.
891 	 *
892 	 * Try to optimize the result object's page color when shadowing
893 	 * in order to maintain page coloring consistancy in the combined
894 	 * shadowed object.
895 	 */
896 	result->backing_object = source;
897 	if (source) {
898 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
899 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
900 		source->shadow_count++;
901 		source->generation++;
902 		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
903 	}
904 
905 	/*
906 	 * Store the offset into the source object, and fix up the offset into
907 	 * the new object.
908 	 */
909 
910 	result->backing_object_offset = *offset;
911 
912 	/*
913 	 * Return the new things
914 	 */
915 
916 	*offset = 0;
917 	*object = result;
918 }
919 
920 #define	OBSC_TEST_ALL_SHADOWED	0x0001
921 #define	OBSC_COLLAPSE_NOWAIT	0x0002
922 #define	OBSC_COLLAPSE_WAIT	0x0004
923 
924 static __inline int
925 vm_object_backing_scan(vm_object_t object, int op)
926 {
927 	int s;
928 	int r = 1;
929 	vm_page_t p;
930 	vm_object_t backing_object;
931 	vm_pindex_t backing_offset_index;
932 
933 	s = splvm();
934 
935 	backing_object = object->backing_object;
936 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
937 
938 	/*
939 	 * Initial conditions
940 	 */
941 
942 	if (op & OBSC_TEST_ALL_SHADOWED) {
943 		/*
944 		 * We do not want to have to test for the existance of
945 		 * swap pages in the backing object.  XXX but with the
946 		 * new swapper this would be pretty easy to do.
947 		 *
948 		 * XXX what about anonymous MAP_SHARED memory that hasn't
949 		 * been ZFOD faulted yet?  If we do not test for this, the
950 		 * shadow test may succeed! XXX
951 		 */
952 		if (backing_object->type != OBJT_DEFAULT) {
953 			splx(s);
954 			return(0);
955 		}
956 	}
957 	if (op & OBSC_COLLAPSE_WAIT) {
958 		vm_object_set_flag(backing_object, OBJ_DEAD);
959 	}
960 
961 	/*
962 	 * Our scan
963 	 */
964 
965 	p = TAILQ_FIRST(&backing_object->memq);
966 	while (p) {
967 		vm_page_t next = TAILQ_NEXT(p, listq);
968 		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
969 
970 		if (op & OBSC_TEST_ALL_SHADOWED) {
971 			vm_page_t pp;
972 
973 			/*
974 			 * Ignore pages outside the parent object's range
975 			 * and outside the parent object's mapping of the
976 			 * backing object.
977 			 *
978 			 * note that we do not busy the backing object's
979 			 * page.
980 			 */
981 
982 			if (
983 			    p->pindex < backing_offset_index ||
984 			    new_pindex >= object->size
985 			) {
986 				p = next;
987 				continue;
988 			}
989 
990 			/*
991 			 * See if the parent has the page or if the parent's
992 			 * object pager has the page.  If the parent has the
993 			 * page but the page is not valid, the parent's
994 			 * object pager must have the page.
995 			 *
996 			 * If this fails, the parent does not completely shadow
997 			 * the object and we might as well give up now.
998 			 */
999 
1000 			pp = vm_page_lookup(object, new_pindex);
1001 			if (
1002 			    (pp == NULL || pp->valid == 0) &&
1003 			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1004 			) {
1005 				r = 0;
1006 				break;
1007 			}
1008 		}
1009 
1010 		/*
1011 		 * Check for busy page
1012 		 */
1013 
1014 		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1015 			vm_page_t pp;
1016 
1017 			if (op & OBSC_COLLAPSE_NOWAIT) {
1018 				if (
1019 				    (p->flags & PG_BUSY) ||
1020 				    !p->valid ||
1021 				    p->hold_count ||
1022 				    p->wire_count ||
1023 				    p->busy
1024 				) {
1025 					p = next;
1026 					continue;
1027 				}
1028 			} else if (op & OBSC_COLLAPSE_WAIT) {
1029 				if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
1030 					/*
1031 					 * If we slept, anything could have
1032 					 * happened.  Since the object is
1033 					 * marked dead, the backing offset
1034 					 * should not have changed so we
1035 					 * just restart our scan.
1036 					 */
1037 					p = TAILQ_FIRST(&backing_object->memq);
1038 					continue;
1039 				}
1040 			}
1041 
1042 			/*
1043 			 * Busy the page
1044 			 */
1045 			vm_page_busy(p);
1046 
1047 			KASSERT(
1048 			    p->object == backing_object,
1049 			    ("vm_object_qcollapse(): object mismatch")
1050 			);
1051 
1052 			/*
1053 			 * Destroy any associated swap
1054 			 */
1055 			if (backing_object->type == OBJT_SWAP) {
1056 				swap_pager_freespace(
1057 				    backing_object,
1058 				    p->pindex,
1059 				    1
1060 				);
1061 			}
1062 
1063 			if (
1064 			    p->pindex < backing_offset_index ||
1065 			    new_pindex >= object->size
1066 			) {
1067 				/*
1068 				 * Page is out of the parent object's range, we
1069 				 * can simply destroy it.
1070 				 */
1071 				vm_page_protect(p, VM_PROT_NONE);
1072 				vm_page_free(p);
1073 				p = next;
1074 				continue;
1075 			}
1076 
1077 			pp = vm_page_lookup(object, new_pindex);
1078 			if (
1079 			    pp != NULL ||
1080 			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1081 			) {
1082 				/*
1083 				 * page already exists in parent OR swap exists
1084 				 * for this location in the parent.  Destroy
1085 				 * the original page from the backing object.
1086 				 *
1087 				 * Leave the parent's page alone
1088 				 */
1089 				vm_page_protect(p, VM_PROT_NONE);
1090 				vm_page_free(p);
1091 				p = next;
1092 				continue;
1093 			}
1094 
1095 			/*
1096 			 * Page does not exist in parent, rename the
1097 			 * page from the backing object to the main object.
1098 			 *
1099 			 * If the page was mapped to a process, it can remain
1100 			 * mapped through the rename.
1101 			 */
1102 			if ((p->queue - p->pc) == PQ_CACHE)
1103 				vm_page_deactivate(p);
1104 
1105 			vm_page_rename(p, object, new_pindex);
1106 			/* page automatically made dirty by rename */
1107 		}
1108 		p = next;
1109 	}
1110 	splx(s);
1111 	return(r);
1112 }
1113 
1114 
1115 /*
1116  * this version of collapse allows the operation to occur earlier and
1117  * when paging_in_progress is true for an object...  This is not a complete
1118  * operation, but should plug 99.9% of the rest of the leaks.
1119  */
1120 static void
1121 vm_object_qcollapse(object)
1122 	vm_object_t object;
1123 {
1124 	vm_object_t backing_object = object->backing_object;
1125 
1126 	if (backing_object->ref_count != 1)
1127 		return;
1128 
1129 	backing_object->ref_count += 2;
1130 
1131 	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1132 
1133 	backing_object->ref_count -= 2;
1134 }
1135 
1136 /*
1137  *	vm_object_collapse:
1138  *
1139  *	Collapse an object with the object backing it.
1140  *	Pages in the backing object are moved into the
1141  *	parent, and the backing object is deallocated.
1142  */
1143 void
1144 vm_object_collapse(object)
1145 	vm_object_t object;
1146 {
1147 	while (TRUE) {
1148 		vm_object_t backing_object;
1149 
1150 		/*
1151 		 * Verify that the conditions are right for collapse:
1152 		 *
1153 		 * The object exists and the backing object exists.
1154 		 */
1155 		if (object == NULL)
1156 			break;
1157 
1158 		if ((backing_object = object->backing_object) == NULL)
1159 			break;
1160 
1161 		/*
1162 		 * we check the backing object first, because it is most likely
1163 		 * not collapsable.
1164 		 */
1165 		if (backing_object->handle != NULL ||
1166 		    (backing_object->type != OBJT_DEFAULT &&
1167 		     backing_object->type != OBJT_SWAP) ||
1168 		    (backing_object->flags & OBJ_DEAD) ||
1169 		    object->handle != NULL ||
1170 		    (object->type != OBJT_DEFAULT &&
1171 		     object->type != OBJT_SWAP) ||
1172 		    (object->flags & OBJ_DEAD)) {
1173 			break;
1174 		}
1175 
1176 		if (
1177 		    object->paging_in_progress != 0 ||
1178 		    backing_object->paging_in_progress != 0
1179 		) {
1180 			vm_object_qcollapse(object);
1181 			break;
1182 		}
1183 
1184 		/*
1185 		 * We know that we can either collapse the backing object (if
1186 		 * the parent is the only reference to it) or (perhaps) have
1187 		 * the parent bypass the object if the parent happens to shadow
1188 		 * all the resident pages in the entire backing object.
1189 		 *
1190 		 * This is ignoring pager-backed pages such as swap pages.
1191 		 * vm_object_backing_scan fails the shadowing test in this
1192 		 * case.
1193 		 */
1194 
1195 		if (backing_object->ref_count == 1) {
1196 			/*
1197 			 * If there is exactly one reference to the backing
1198 			 * object, we can collapse it into the parent.
1199 			 */
1200 
1201 			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1202 
1203 			/*
1204 			 * Move the pager from backing_object to object.
1205 			 */
1206 
1207 			if (backing_object->type == OBJT_SWAP) {
1208 				vm_object_pip_add(backing_object, 1);
1209 
1210 				/*
1211 				 * scrap the paging_offset junk and do a
1212 				 * discrete copy.  This also removes major
1213 				 * assumptions about how the swap-pager
1214 				 * works from where it doesn't belong.  The
1215 				 * new swapper is able to optimize the
1216 				 * destroy-source case.
1217 				 */
1218 
1219 				vm_object_pip_add(object, 1);
1220 				swap_pager_copy(
1221 				    backing_object,
1222 				    object,
1223 				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1224 				vm_object_pip_wakeup(object);
1225 
1226 				vm_object_pip_wakeup(backing_object);
1227 			}
1228 			/*
1229 			 * Object now shadows whatever backing_object did.
1230 			 * Note that the reference to
1231 			 * backing_object->backing_object moves from within
1232 			 * backing_object to within object.
1233 			 */
1234 
1235 			TAILQ_REMOVE(
1236 			    &object->backing_object->shadow_head,
1237 			    object,
1238 			    shadow_list
1239 			);
1240 			object->backing_object->shadow_count--;
1241 			object->backing_object->generation++;
1242 			if (backing_object->backing_object) {
1243 				TAILQ_REMOVE(
1244 				    &backing_object->backing_object->shadow_head,
1245 				    backing_object,
1246 				    shadow_list
1247 				);
1248 				backing_object->backing_object->shadow_count--;
1249 				backing_object->backing_object->generation++;
1250 			}
1251 			object->backing_object = backing_object->backing_object;
1252 			if (object->backing_object) {
1253 				TAILQ_INSERT_TAIL(
1254 				    &object->backing_object->shadow_head,
1255 				    object,
1256 				    shadow_list
1257 				);
1258 				object->backing_object->shadow_count++;
1259 				object->backing_object->generation++;
1260 			}
1261 
1262 			object->backing_object_offset +=
1263 			    backing_object->backing_object_offset;
1264 
1265 			/*
1266 			 * Discard backing_object.
1267 			 *
1268 			 * Since the backing object has no pages, no pager left,
1269 			 * and no object references within it, all that is
1270 			 * necessary is to dispose of it.
1271 			 */
1272 
1273 			TAILQ_REMOVE(
1274 			    &vm_object_list,
1275 			    backing_object,
1276 			    object_list
1277 			);
1278 			vm_object_count--;
1279 
1280 			zfree(obj_zone, backing_object);
1281 
1282 			object_collapses++;
1283 		} else {
1284 			vm_object_t new_backing_object;
1285 
1286 			/*
1287 			 * If we do not entirely shadow the backing object,
1288 			 * there is nothing we can do so we give up.
1289 			 */
1290 
1291 			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1292 				break;
1293 			}
1294 
1295 			/*
1296 			 * Make the parent shadow the next object in the
1297 			 * chain.  Deallocating backing_object will not remove
1298 			 * it, since its reference count is at least 2.
1299 			 */
1300 
1301 			TAILQ_REMOVE(
1302 			    &backing_object->shadow_head,
1303 			    object,
1304 			    shadow_list
1305 			);
1306 			backing_object->shadow_count--;
1307 			backing_object->generation++;
1308 
1309 			new_backing_object = backing_object->backing_object;
1310 			if ((object->backing_object = new_backing_object) != NULL) {
1311 				vm_object_reference(new_backing_object);
1312 				TAILQ_INSERT_TAIL(
1313 				    &new_backing_object->shadow_head,
1314 				    object,
1315 				    shadow_list
1316 				);
1317 				new_backing_object->shadow_count++;
1318 				new_backing_object->generation++;
1319 				object->backing_object_offset +=
1320 					backing_object->backing_object_offset;
1321 			}
1322 
1323 			/*
1324 			 * Drop the reference count on backing_object. Since
1325 			 * its ref_count was at least 2, it will not vanish;
1326 			 * so we don't need to call vm_object_deallocate, but
1327 			 * we do anyway.
1328 			 */
1329 			vm_object_deallocate(backing_object);
1330 			object_bypasses++;
1331 		}
1332 
1333 		/*
1334 		 * Try again with this object's new backing object.
1335 		 */
1336 	}
1337 }
1338 
1339 /*
1340  *	vm_object_page_remove: [internal]
1341  *
1342  *	Removes all physical pages in the specified
1343  *	object range from the object's list of pages.
1344  *
1345  *	The object must be locked.
1346  */
1347 void
1348 vm_object_page_remove(object, start, end, clean_only)
1349 	vm_object_t object;
1350 	vm_pindex_t start;
1351 	vm_pindex_t end;
1352 	boolean_t clean_only;
1353 {
1354 	vm_page_t p, next;
1355 	unsigned int size;
1356 	int all;
1357 
1358 	if (object == NULL ||
1359 	    object->resident_page_count == 0)
1360 		return;
1361 
1362 	all = ((end == 0) && (start == 0));
1363 
1364 	vm_object_pip_add(object, 1);
1365 again:
1366 	size = end - start;
1367 	if (all || size > object->resident_page_count / 4) {
1368 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1369 			next = TAILQ_NEXT(p, listq);
1370 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1371 				if (p->wire_count != 0) {
1372 					vm_page_protect(p, VM_PROT_NONE);
1373 					if (!clean_only)
1374 						p->valid = 0;
1375 					continue;
1376 				}
1377 
1378 				/*
1379 				 * The busy flags are only cleared at
1380 				 * interrupt -- minimize the spl transitions
1381 				 */
1382 
1383  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1384  					goto again;
1385 
1386 				if (clean_only && p->valid) {
1387 					vm_page_test_dirty(p);
1388 					if (p->valid & p->dirty)
1389 						continue;
1390 				}
1391 
1392 				vm_page_busy(p);
1393 				vm_page_protect(p, VM_PROT_NONE);
1394 				vm_page_free(p);
1395 			}
1396 		}
1397 	} else {
1398 		while (size > 0) {
1399 			if ((p = vm_page_lookup(object, start)) != 0) {
1400 
1401 				if (p->wire_count != 0) {
1402 					vm_page_protect(p, VM_PROT_NONE);
1403 					if (!clean_only)
1404 						p->valid = 0;
1405 					start += 1;
1406 					size -= 1;
1407 					continue;
1408 				}
1409 
1410 				/*
1411 				 * The busy flags are only cleared at
1412 				 * interrupt -- minimize the spl transitions
1413 				 */
1414  				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1415 					goto again;
1416 
1417 				if (clean_only && p->valid) {
1418 					vm_page_test_dirty(p);
1419 					if (p->valid & p->dirty) {
1420 						start += 1;
1421 						size -= 1;
1422 						continue;
1423 					}
1424 				}
1425 
1426 				vm_page_busy(p);
1427 				vm_page_protect(p, VM_PROT_NONE);
1428 				vm_page_free(p);
1429 			}
1430 			start += 1;
1431 			size -= 1;
1432 		}
1433 	}
1434 	vm_object_pip_wakeup(object);
1435 }
1436 
1437 /*
1438  *	Routine:	vm_object_coalesce
1439  *	Function:	Coalesces two objects backing up adjoining
1440  *			regions of memory into a single object.
1441  *
1442  *	returns TRUE if objects were combined.
1443  *
1444  *	NOTE:	Only works at the moment if the second object is NULL -
1445  *		if it's not, which object do we lock first?
1446  *
1447  *	Parameters:
1448  *		prev_object	First object to coalesce
1449  *		prev_offset	Offset into prev_object
1450  *		next_object	Second object into coalesce
1451  *		next_offset	Offset into next_object
1452  *
1453  *		prev_size	Size of reference to prev_object
1454  *		next_size	Size of reference to next_object
1455  *
1456  *	Conditions:
1457  *	The object must *not* be locked.
1458  */
1459 boolean_t
1460 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1461 	vm_object_t prev_object;
1462 	vm_pindex_t prev_pindex;
1463 	vm_size_t prev_size, next_size;
1464 {
1465 	vm_pindex_t next_pindex;
1466 
1467 	if (prev_object == NULL) {
1468 		return (TRUE);
1469 	}
1470 
1471 	if (prev_object->type != OBJT_DEFAULT &&
1472 	    prev_object->type != OBJT_SWAP) {
1473 		return (FALSE);
1474 	}
1475 
1476 	/*
1477 	 * Try to collapse the object first
1478 	 */
1479 	vm_object_collapse(prev_object);
1480 
1481 	/*
1482 	 * Can't coalesce if: . more than one reference . paged out . shadows
1483 	 * another object . has a copy elsewhere (any of which mean that the
1484 	 * pages not mapped to prev_entry may be in use anyway)
1485 	 */
1486 
1487 	if (prev_object->backing_object != NULL) {
1488 		return (FALSE);
1489 	}
1490 
1491 	prev_size >>= PAGE_SHIFT;
1492 	next_size >>= PAGE_SHIFT;
1493 	next_pindex = prev_pindex + prev_size;
1494 
1495 	if ((prev_object->ref_count > 1) &&
1496 	    (prev_object->size != next_pindex)) {
1497 		return (FALSE);
1498 	}
1499 
1500 	/*
1501 	 * Remove any pages that may still be in the object from a previous
1502 	 * deallocation.
1503 	 */
1504 	if (next_pindex < prev_object->size) {
1505 		vm_object_page_remove(prev_object,
1506 				      next_pindex,
1507 				      next_pindex + next_size, FALSE);
1508 		if (prev_object->type == OBJT_SWAP)
1509 			swap_pager_freespace(prev_object,
1510 					     next_pindex, next_size);
1511 	}
1512 
1513 	/*
1514 	 * Extend the object if necessary.
1515 	 */
1516 	if (next_pindex + next_size > prev_object->size)
1517 		prev_object->size = next_pindex + next_size;
1518 
1519 	return (TRUE);
1520 }
1521 
1522 #include "opt_ddb.h"
1523 #ifdef DDB
1524 #include <sys/kernel.h>
1525 
1526 #include <machine/cons.h>
1527 
1528 #include <ddb/ddb.h>
1529 
1530 static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1531 				       vm_map_entry_t entry));
1532 static int	vm_object_in_map __P((vm_object_t object));
1533 
1534 static int
1535 _vm_object_in_map(map, object, entry)
1536 	vm_map_t map;
1537 	vm_object_t object;
1538 	vm_map_entry_t entry;
1539 {
1540 	vm_map_t tmpm;
1541 	vm_map_entry_t tmpe;
1542 	vm_object_t obj;
1543 	int entcount;
1544 
1545 	if (map == 0)
1546 		return 0;
1547 
1548 	if (entry == 0) {
1549 		tmpe = map->header.next;
1550 		entcount = map->nentries;
1551 		while (entcount-- && (tmpe != &map->header)) {
1552 			if( _vm_object_in_map(map, object, tmpe)) {
1553 				return 1;
1554 			}
1555 			tmpe = tmpe->next;
1556 		}
1557 	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1558 		tmpm = entry->object.sub_map;
1559 		tmpe = tmpm->header.next;
1560 		entcount = tmpm->nentries;
1561 		while (entcount-- && tmpe != &tmpm->header) {
1562 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1563 				return 1;
1564 			}
1565 			tmpe = tmpe->next;
1566 		}
1567 	} else if ((obj = entry->object.vm_object) != NULL) {
1568 		for(; obj; obj=obj->backing_object)
1569 			if( obj == object) {
1570 				return 1;
1571 			}
1572 	}
1573 	return 0;
1574 }
1575 
1576 static int
1577 vm_object_in_map( object)
1578 	vm_object_t object;
1579 {
1580 	struct proc *p;
1581 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1582 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1583 			continue;
1584 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1585 			return 1;
1586 	}
1587 	if( _vm_object_in_map( kernel_map, object, 0))
1588 		return 1;
1589 	if( _vm_object_in_map( kmem_map, object, 0))
1590 		return 1;
1591 	if( _vm_object_in_map( pager_map, object, 0))
1592 		return 1;
1593 	if( _vm_object_in_map( buffer_map, object, 0))
1594 		return 1;
1595 	if( _vm_object_in_map( io_map, object, 0))
1596 		return 1;
1597 	if( _vm_object_in_map( phys_map, object, 0))
1598 		return 1;
1599 	if( _vm_object_in_map( mb_map, object, 0))
1600 		return 1;
1601 	if( _vm_object_in_map( u_map, object, 0))
1602 		return 1;
1603 	return 0;
1604 }
1605 
1606 DB_SHOW_COMMAND(vmochk, vm_object_check)
1607 {
1608 	vm_object_t object;
1609 
1610 	/*
1611 	 * make sure that internal objs are in a map somewhere
1612 	 * and none have zero ref counts.
1613 	 */
1614 	for (object = TAILQ_FIRST(&vm_object_list);
1615 			object != NULL;
1616 			object = TAILQ_NEXT(object, object_list)) {
1617 		if (object->handle == NULL &&
1618 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1619 			if (object->ref_count == 0) {
1620 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1621 					object->size);
1622 			}
1623 			if (!vm_object_in_map(object)) {
1624 				db_printf(
1625 			"vmochk: internal obj is not in a map: "
1626 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1627 				    object->ref_count, (u_long)object->size,
1628 				    (u_long)object->size,
1629 				    (void *)object->backing_object);
1630 			}
1631 		}
1632 	}
1633 }
1634 
1635 /*
1636  *	vm_object_print:	[ debug ]
1637  */
1638 DB_SHOW_COMMAND(object, vm_object_print_static)
1639 {
1640 	/* XXX convert args. */
1641 	vm_object_t object = (vm_object_t)addr;
1642 	boolean_t full = have_addr;
1643 
1644 	vm_page_t p;
1645 
1646 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1647 #define	count	was_count
1648 
1649 	int count;
1650 
1651 	if (object == NULL)
1652 		return;
1653 
1654 	db_iprintf(
1655 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1656 	    object, (int)object->type, (u_long)object->size,
1657 	    object->resident_page_count, object->ref_count, object->flags);
1658 	/*
1659 	 * XXX no %qd in kernel.  Truncate object->backing_object_offset.
1660 	 */
1661 	db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
1662 	    object->shadow_count,
1663 	    object->backing_object ? object->backing_object->ref_count : 0,
1664 	    object->backing_object, (long)object->backing_object_offset);
1665 
1666 	if (!full)
1667 		return;
1668 
1669 	db_indent += 2;
1670 	count = 0;
1671 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1672 		if (count == 0)
1673 			db_iprintf("memory:=");
1674 		else if (count == 6) {
1675 			db_printf("\n");
1676 			db_iprintf(" ...");
1677 			count = 0;
1678 		} else
1679 			db_printf(",");
1680 		count++;
1681 
1682 		db_printf("(off=0x%lx,page=0x%lx)",
1683 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1684 	}
1685 	if (count != 0)
1686 		db_printf("\n");
1687 	db_indent -= 2;
1688 }
1689 
1690 /* XXX. */
1691 #undef count
1692 
1693 /* XXX need this non-static entry for calling from vm_map_print. */
1694 void
1695 vm_object_print(addr, have_addr, count, modif)
1696         /* db_expr_t */ long addr;
1697 	boolean_t have_addr;
1698 	/* db_expr_t */ long count;
1699 	char *modif;
1700 {
1701 	vm_object_print_static(addr, have_addr, count, modif);
1702 }
1703 
1704 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
1705 {
1706 	vm_object_t object;
1707 	int nl = 0;
1708 	int c;
1709 	for (object = TAILQ_FIRST(&vm_object_list);
1710 			object != NULL;
1711 			object = TAILQ_NEXT(object, object_list)) {
1712 		vm_pindex_t idx, fidx;
1713 		vm_pindex_t osize;
1714 		vm_offset_t pa = -1, padiff;
1715 		int rcount;
1716 		vm_page_t m;
1717 
1718 		db_printf("new object: %p\n", (void *)object);
1719 		if ( nl > 18) {
1720 			c = cngetc();
1721 			if (c != ' ')
1722 				return;
1723 			nl = 0;
1724 		}
1725 		nl++;
1726 		rcount = 0;
1727 		fidx = 0;
1728 		osize = object->size;
1729 		if (osize > 128)
1730 			osize = 128;
1731 		for(idx=0;idx<osize;idx++) {
1732 			m = vm_page_lookup(object, idx);
1733 			if (m == NULL) {
1734 				if (rcount) {
1735 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
1736 						fidx, rcount, pa);
1737 					if ( nl > 18) {
1738 						c = cngetc();
1739 						if (c != ' ')
1740 							return;
1741 						nl = 0;
1742 					}
1743 					nl++;
1744 					rcount = 0;
1745 				}
1746 				continue;
1747 			}
1748 
1749 
1750 			if (rcount &&
1751 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
1752 				++rcount;
1753 				continue;
1754 			}
1755 			if (rcount) {
1756 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
1757 				padiff >>= PAGE_SHIFT;
1758 				padiff &= PQ_L2_MASK;
1759 				if (padiff == 0) {
1760 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
1761 					++rcount;
1762 					continue;
1763 				}
1764 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
1765 				db_printf("pd(%d)\n", padiff);
1766 				if ( nl > 18) {
1767 					c = cngetc();
1768 					if (c != ' ')
1769 						return;
1770 					nl = 0;
1771 				}
1772 				nl++;
1773 			}
1774 			fidx = idx;
1775 			pa = VM_PAGE_TO_PHYS(m);
1776 			rcount = 1;
1777 		}
1778 		if (rcount) {
1779 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
1780 			if ( nl > 18) {
1781 				c = cngetc();
1782 				if (c != ' ')
1783 					return;
1784 				nl = 0;
1785 			}
1786 			nl++;
1787 		}
1788 	}
1789 }
1790 #endif /* DDB */
1791