xref: /freebsd/sys/vm/vm_object.c (revision 2aebedc3ad9e722b272254e6dd3a12e399595e57)
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.134 1998/10/27 13:22:51 dg 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 static struct simplelock vm_object_list_lock;
123 static long vm_object_count;		/* count of all objects */
124 vm_object_t kernel_object;
125 vm_object_t kmem_object;
126 static struct vm_object kernel_object_store;
127 static struct vm_object kmem_object_store;
128 extern int vm_pageout_page_count;
129 
130 static long object_collapses;
131 static long object_bypasses;
132 static int next_index;
133 static vm_zone_t obj_zone;
134 static struct vm_zone obj_zone_store;
135 #define VM_OBJECTS_INIT 256
136 static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
137 static int objidnumber;
138 
139 void
140 _vm_object_allocate(type, size, object)
141 	objtype_t type;
142 	vm_size_t size;
143 	register vm_object_t object;
144 {
145 	int incr;
146 	TAILQ_INIT(&object->memq);
147 	TAILQ_INIT(&object->shadow_head);
148 
149 	object->type = type;
150 	object->size = size;
151 	object->ref_count = 1;
152 	object->flags = 0;
153 	object->id = ++objidnumber;
154 	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
155 		vm_object_set_flag(object, OBJ_ONEMAPPING);
156 	object->behavior = OBJ_NORMAL;
157 	object->paging_in_progress = 0;
158 	object->resident_page_count = 0;
159 	object->cache_count = 0;
160 	object->wire_count = 0;
161 	object->shadow_count = 0;
162 	object->pg_color = next_index;
163 	if ( size > (PQ_L2_SIZE / 3 + PQ_PRIME1))
164 		incr = PQ_L2_SIZE / 3 + PQ_PRIME1;
165 	else
166 		incr = size;
167 	next_index = (next_index + incr) & PQ_L2_MASK;
168 	object->handle = NULL;
169 	object->paging_offset = (vm_ooffset_t) 0;
170 	object->backing_object = NULL;
171 	object->backing_object_offset = (vm_ooffset_t) 0;
172 	object->page_hint = NULL;
173 
174 	object->last_read = 0;
175 	object->generation++;
176 
177 	TAILQ_INSERT_TAIL(&vm_object_list, object, object_list);
178 	vm_object_count++;
179 }
180 
181 /*
182  *	vm_object_init:
183  *
184  *	Initialize the VM objects module.
185  */
186 void
187 vm_object_init()
188 {
189 	TAILQ_INIT(&vm_object_list);
190 	simple_lock_init(&vm_object_list_lock);
191 	vm_object_count = 0;
192 
193 	kernel_object = &kernel_object_store;
194 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
195 	    kernel_object);
196 
197 	kmem_object = &kmem_object_store;
198 	_vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS),
199 	    kmem_object);
200 
201 	obj_zone = &obj_zone_store;
202 	zbootinit(obj_zone, "VM OBJECT", sizeof (struct vm_object),
203 		vm_objects_init, VM_OBJECTS_INIT);
204 }
205 
206 void
207 vm_object_init2() {
208 	zinitna(obj_zone, NULL, NULL, 0, 0, 0, 1);
209 }
210 
211 /*
212  *	vm_object_allocate:
213  *
214  *	Returns a new object with the given size.
215  */
216 
217 vm_object_t
218 vm_object_allocate(type, size)
219 	objtype_t type;
220 	vm_size_t size;
221 {
222 	register vm_object_t result;
223 	result = (vm_object_t) zalloc(obj_zone);
224 
225 	_vm_object_allocate(type, size, result);
226 
227 	return (result);
228 }
229 
230 
231 /*
232  *	vm_object_reference:
233  *
234  *	Gets another reference to the given object.
235  */
236 void
237 vm_object_reference(object)
238 	register vm_object_t object;
239 {
240 	if (object == NULL)
241 		return;
242 
243 #if defined(DIAGNOSTIC)
244 	if (object->flags & OBJ_DEAD)
245 		panic("vm_object_reference: attempting to reference dead obj");
246 #endif
247 
248 	object->ref_count++;
249 	if (object->type == OBJT_VNODE) {
250 		while (vget((struct vnode *) object->handle, LK_RETRY|LK_NOOBJ, curproc)) {
251 #if !defined(MAX_PERF)
252 			printf("vm_object_reference: delay in getting object\n");
253 #endif
254 		}
255 	}
256 }
257 
258 void
259 vm_object_vndeallocate(object)
260 	vm_object_t object;
261 {
262 	struct vnode *vp = (struct vnode *) object->handle;
263 #if defined(DIAGNOSTIC)
264 	if (object->type != OBJT_VNODE)
265 		panic("vm_object_vndeallocate: not a vnode object");
266 	if (vp == NULL)
267 		panic("vm_object_vndeallocate: missing vp");
268 	if (object->ref_count == 0) {
269 		vprint("vm_object_vndeallocate", vp);
270 		panic("vm_object_vndeallocate: bad object reference count");
271 	}
272 #endif
273 
274 	object->ref_count--;
275 	if (object->ref_count == 0) {
276 		vp->v_flag &= ~VTEXT;
277 		vm_object_clear_flag(object, OBJ_OPT);
278 	}
279 	vrele(vp);
280 }
281 
282 /*
283  *	vm_object_deallocate:
284  *
285  *	Release a reference to the specified object,
286  *	gained either through a vm_object_allocate
287  *	or a vm_object_reference call.  When all references
288  *	are gone, storage associated with this object
289  *	may be relinquished.
290  *
291  *	No object may be locked.
292  */
293 void
294 vm_object_deallocate(object)
295 	vm_object_t object;
296 {
297 	vm_object_t temp;
298 
299 	while (object != NULL) {
300 
301 		if (object->type == OBJT_VNODE) {
302 			vm_object_vndeallocate(object);
303 			return;
304 		}
305 
306 		if (object->ref_count == 0) {
307 			panic("vm_object_deallocate: object deallocated too many times: %d", object->type);
308 		} else if (object->ref_count > 2) {
309 			object->ref_count--;
310 			return;
311 		}
312 
313 		/*
314 		 * Here on ref_count of one or two, which are special cases for
315 		 * objects.
316 		 */
317 		if ((object->ref_count == 2) && (object->shadow_count == 0)) {
318 			vm_object_set_flag(object, OBJ_ONEMAPPING);
319 			object->ref_count--;
320 			return;
321 		} else if ((object->ref_count == 2) && (object->shadow_count == 1)) {
322 			object->ref_count--;
323 			if ((object->handle == NULL) &&
324 			    (object->type == OBJT_DEFAULT ||
325 			     object->type == OBJT_SWAP)) {
326 				vm_object_t robject;
327 
328 				robject = TAILQ_FIRST(&object->shadow_head);
329 #if defined(DIAGNOSTIC)
330 				if (robject == NULL)
331 					panic("vm_object_deallocate: ref_count: %d,"
332 						  " shadow_count: %d",
333 						  object->ref_count, object->shadow_count);
334 #endif
335 				if ((robject->handle == NULL) &&
336 				    (robject->type == OBJT_DEFAULT ||
337 				     robject->type == OBJT_SWAP)) {
338 
339 					robject->ref_count++;
340 
341 			retry:
342 					if (robject->paging_in_progress ||
343 							object->paging_in_progress) {
344 						vm_object_pip_sleep(robject, "objde1");
345 						if (robject->paging_in_progress &&
346 							robject->type == OBJT_SWAP) {
347 							swap_pager_sync();
348 							goto retry;
349 						}
350 
351 						vm_object_pip_sleep(object, "objde2");
352 						if (object->paging_in_progress &&
353 							object->type == OBJT_SWAP) {
354 							swap_pager_sync();
355 						}
356 						goto retry;
357 					}
358 
359 					if( robject->ref_count == 1) {
360 						robject->ref_count--;
361 						object = robject;
362 						goto doterm;
363 					}
364 
365 					object = robject;
366 					vm_object_collapse(object);
367 					continue;
368 				}
369 			}
370 
371 			return;
372 
373 		} else {
374 			object->ref_count--;
375 			if (object->ref_count != 0)
376 				return;
377 		}
378 
379 doterm:
380 
381 		temp = object->backing_object;
382 		if (temp) {
383 			TAILQ_REMOVE(&temp->shadow_head, object, shadow_list);
384 			temp->shadow_count--;
385 			if (temp->ref_count == 0)
386 				vm_object_clear_flag(temp, OBJ_OPT);
387 			temp->generation++;
388 			object->backing_object = NULL;
389 		}
390 		vm_object_terminate(object);
391 		/* unlocks and deallocates object */
392 		object = temp;
393 	}
394 }
395 
396 /*
397  *	vm_object_terminate actually destroys the specified object, freeing
398  *	up all previously used resources.
399  *
400  *	The object must be locked.
401  */
402 void
403 vm_object_terminate(object)
404 	register vm_object_t object;
405 {
406 	register vm_page_t p;
407 	int s;
408 
409 	/*
410 	 * Make sure no one uses us.
411 	 */
412 	vm_object_set_flag(object, OBJ_DEAD);
413 
414 	/*
415 	 * wait for the pageout daemon to be done with the object
416 	 */
417 	vm_object_pip_wait(object, "objtrm");
418 
419 #if defined(DIAGNOSTIC)
420 	if (object->paging_in_progress != 0)
421 		panic("vm_object_terminate: pageout in progress");
422 #endif
423 
424 	/*
425 	 * Clean and free the pages, as appropriate. All references to the
426 	 * object are gone, so we don't need to lock it.
427 	 */
428 	if (object->type == OBJT_VNODE) {
429 		struct vnode *vp;
430 
431 		/*
432 		 * Freeze optimized copies.
433 		 */
434 		vm_freeze_copyopts(object, 0, object->size);
435 
436 		/*
437 		 * Clean pages and flush buffers.
438 		 */
439 		vm_object_page_clean(object, 0, 0, OBJPC_SYNC);
440 
441 		vp = (struct vnode *) object->handle;
442 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
443 	}
444 
445 	if (object->ref_count != 0)
446 		panic("vm_object_terminate: object with references, ref_count=%d", object->ref_count);
447 
448 	/*
449 	 * Now free any remaining pages. For internal objects, this also
450 	 * removes them from paging queues. Don't free wired pages, just
451 	 * remove them from the object.
452 	 */
453 	s = splvm();
454 	while ((p = TAILQ_FIRST(&object->memq)) != NULL) {
455 #if !defined(MAX_PERF)
456 		if (p->busy || (p->flags & PG_BUSY))
457 			printf("vm_object_terminate: freeing busy page\n");
458 #endif
459 		if (p->wire_count == 0) {
460 			vm_page_busy(p);
461 			vm_page_free(p);
462 			cnt.v_pfree++;
463 		} else {
464 			vm_page_busy(p);
465 			vm_page_remove(p);
466 		}
467 	}
468 	splx(s);
469 
470 	/*
471 	 * Let the pager know object is dead.
472 	 */
473 	vm_pager_deallocate(object);
474 
475 	/*
476 	 * Remove the object from the global object list.
477 	 */
478 	simple_lock(&vm_object_list_lock);
479 	TAILQ_REMOVE(&vm_object_list, object, object_list);
480 	simple_unlock(&vm_object_list_lock);
481 
482 	wakeup(object);
483 
484 	/*
485 	 * Free the space for the object.
486 	 */
487 	zfree(obj_zone, object);
488 }
489 
490 /*
491  *	vm_object_page_clean
492  *
493  *	Clean all dirty pages in the specified range of object.
494  *	Leaves page on whatever queue it is currently on.
495  *
496  *	Odd semantics: if start == end, we clean everything.
497  *
498  *	The object must be locked.
499  */
500 
501 void
502 vm_object_page_clean(object, start, end, flags)
503 	vm_object_t object;
504 	vm_pindex_t start;
505 	vm_pindex_t end;
506 	int flags;
507 {
508 	register vm_page_t p, np, tp;
509 	register vm_offset_t tstart, tend;
510 	vm_pindex_t pi;
511 	int s;
512 	struct vnode *vp;
513 	int runlen;
514 	int maxf;
515 	int chkb;
516 	int maxb;
517 	int i;
518 	int pagerflags;
519 	vm_page_t maf[vm_pageout_page_count];
520 	vm_page_t mab[vm_pageout_page_count];
521 	vm_page_t ma[vm_pageout_page_count];
522 	int curgeneration;
523 
524 	if (object->type != OBJT_VNODE ||
525 		(object->flags & OBJ_MIGHTBEDIRTY) == 0)
526 		return;
527 
528 	pagerflags = (flags & (OBJPC_SYNC | OBJPC_INVAL)) ? VM_PAGER_PUT_SYNC : 0;
529 	pagerflags |= (flags & OBJPC_INVAL) ? VM_PAGER_PUT_INVAL : 0;
530 
531 	vp = object->handle;
532 
533 	vm_object_set_flag(object, OBJ_CLEANING);
534 
535 	tstart = start;
536 	if (end == 0) {
537 		tend = object->size;
538 	} else {
539 		tend = end;
540 	}
541 
542 	for(p = TAILQ_FIRST(&object->memq); p; p = TAILQ_NEXT(p, listq)) {
543 		vm_page_flag_set(p, PG_CLEANCHK);
544 		vm_page_protect(p, VM_PROT_READ);
545 	}
546 
547 	if ((tstart == 0) && (tend == object->size)) {
548 		vm_object_clear_flag(object, OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
549 	}
550 
551 rescan:
552 	curgeneration = object->generation;
553 
554 	for(p = TAILQ_FIRST(&object->memq); p; p = np) {
555 		np = TAILQ_NEXT(p, listq);
556 
557 		pi = p->pindex;
558 		if (((p->flags & PG_CLEANCHK) == 0) ||
559 			(pi < tstart) || (pi >= tend) ||
560 			(p->valid == 0) ||
561 			((p->queue - p->pc) == PQ_CACHE)) {
562 			vm_page_flag_clear(p, PG_CLEANCHK);
563 			continue;
564 		}
565 
566 		vm_page_test_dirty(p);
567 		if ((p->dirty & p->valid) == 0) {
568 			vm_page_flag_clear(p, PG_CLEANCHK);
569 			continue;
570 		}
571 
572 		s = splvm();
573 		while ((p->flags & PG_BUSY) || p->busy) {
574 			vm_page_flag_set(p, PG_WANTED | PG_REFERENCED);
575 			tsleep(p, PVM, "vpcwai", 0);
576 			if (object->generation != curgeneration) {
577 				splx(s);
578 				goto rescan;
579 			}
580 		}
581 
582 		maxf = 0;
583 		for(i=1;i<vm_pageout_page_count;i++) {
584 			if (tp = vm_page_lookup(object, pi + i)) {
585 				if ((tp->flags & PG_BUSY) ||
586 					(tp->flags & PG_CLEANCHK) == 0 ||
587 					(tp->busy != 0))
588 					break;
589 				if((tp->queue - tp->pc) == PQ_CACHE) {
590 					vm_page_flag_clear(tp, PG_CLEANCHK);
591 					break;
592 				}
593 				vm_page_test_dirty(tp);
594 				if ((tp->dirty & tp->valid) == 0) {
595 					vm_page_flag_clear(tp, PG_CLEANCHK);
596 					break;
597 				}
598 				maf[ i - 1 ] = tp;
599 				maxf++;
600 				continue;
601 			}
602 			break;
603 		}
604 
605 		maxb = 0;
606 		chkb = vm_pageout_page_count -  maxf;
607 		if (chkb) {
608 			for(i = 1; i < chkb;i++) {
609 				if (tp = vm_page_lookup(object, pi - i)) {
610 					if ((tp->flags & PG_BUSY) ||
611 						(tp->flags & PG_CLEANCHK) == 0 ||
612 						(tp->busy != 0))
613 						break;
614 					if((tp->queue - tp->pc) == PQ_CACHE) {
615 						vm_page_flag_clear(tp, PG_CLEANCHK);
616 						break;
617 					}
618 					vm_page_test_dirty(tp);
619 					if ((tp->dirty & tp->valid) == 0) {
620 						vm_page_flag_clear(tp, PG_CLEANCHK);
621 						break;
622 					}
623 					mab[ i - 1 ] = tp;
624 					maxb++;
625 					continue;
626 				}
627 				break;
628 			}
629 		}
630 
631 		for(i=0;i<maxb;i++) {
632 			int index = (maxb - i) - 1;
633 			ma[index] = mab[i];
634 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
635 		}
636 		vm_page_flag_clear(p, PG_CLEANCHK);
637 		ma[maxb] = p;
638 		for(i=0;i<maxf;i++) {
639 			int index = (maxb + i) + 1;
640 			ma[index] = maf[i];
641 			vm_page_flag_clear(ma[index], PG_CLEANCHK);
642 		}
643 		runlen = maxb + maxf + 1;
644 
645 		splx(s);
646 		vm_pageout_flush(ma, runlen, pagerflags);
647 		for (i = 0; i<runlen; i++) {
648 			if (ma[i]->valid & ma[i]->dirty) {
649 				vm_page_protect(ma[i], VM_PROT_READ);
650 				vm_page_flag_set(ma[i], PG_CLEANCHK);
651 			}
652 		}
653 		if (object->generation != curgeneration)
654 			goto rescan;
655 	}
656 
657 	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
658 
659 	vm_object_clear_flag(object, OBJ_CLEANING);
660 	return;
661 }
662 
663 #ifdef not_used
664 /* XXX I cannot tell if this should be an exported symbol */
665 /*
666  *	vm_object_deactivate_pages
667  *
668  *	Deactivate all pages in the specified object.  (Keep its pages
669  *	in memory even though it is no longer referenced.)
670  *
671  *	The object must be locked.
672  */
673 static void
674 vm_object_deactivate_pages(object)
675 	register vm_object_t object;
676 {
677 	register vm_page_t p, next;
678 
679 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
680 		next = TAILQ_NEXT(p, listq);
681 		vm_page_deactivate(p);
682 	}
683 }
684 #endif
685 
686 /*
687  *	vm_object_pmap_copy:
688  *
689  *	Makes all physical pages in the specified
690  *	object range copy-on-write.  No writeable
691  *	references to these pages should remain.
692  *
693  *	The object must *not* be locked.
694  */
695 void
696 vm_object_pmap_copy(object, start, end)
697 	register vm_object_t object;
698 	register vm_pindex_t start;
699 	register vm_pindex_t end;
700 {
701 	register vm_page_t p;
702 
703 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
704 		return;
705 
706 	for (p = TAILQ_FIRST(&object->memq);
707 		p != NULL;
708 		p = TAILQ_NEXT(p, listq)) {
709 		vm_page_protect(p, VM_PROT_READ);
710 	}
711 
712 	vm_object_clear_flag(object, OBJ_WRITEABLE);
713 }
714 
715 /*
716  * Same as vm_object_pmap_copy_1, except range checking really
717  * works, and is meant for small sections of an object.
718  */
719 void
720 vm_object_pmap_copy_1(object, start, end)
721 	register vm_object_t object;
722 	register vm_pindex_t start;
723 	register vm_pindex_t end;
724 {
725 	vm_pindex_t idx;
726 	register vm_page_t p;
727 
728 	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
729 		return;
730 
731 	for (idx = start; idx < end; idx++) {
732 		p = vm_page_lookup(object, idx);
733 		if (p == NULL)
734 			continue;
735 		vm_page_protect(p, VM_PROT_READ);
736 	}
737 }
738 
739 /*
740  *	vm_object_pmap_remove:
741  *
742  *	Removes all physical pages in the specified
743  *	object range from all physical maps.
744  *
745  *	The object must *not* be locked.
746  */
747 void
748 vm_object_pmap_remove(object, start, end)
749 	register vm_object_t object;
750 	register vm_pindex_t start;
751 	register vm_pindex_t end;
752 {
753 	register vm_page_t p;
754 	if (object == NULL)
755 		return;
756 	for (p = TAILQ_FIRST(&object->memq);
757 		p != NULL;
758 		p = TAILQ_NEXT(p, listq)) {
759 		if (p->pindex >= start && p->pindex < end)
760 			vm_page_protect(p, VM_PROT_NONE);
761 	}
762 	if ((start == 0) && (object->size == end))
763 		vm_object_clear_flag(object, OBJ_WRITEABLE);
764 }
765 
766 /*
767  *	vm_object_madvise:
768  *
769  *	Implements the madvise function at the object/page level.
770  */
771 void
772 vm_object_madvise(object, pindex, count, advise)
773 	vm_object_t object;
774 	vm_pindex_t pindex;
775 	int count;
776 	int advise;
777 {
778 	vm_pindex_t end, tpindex;
779 	vm_object_t tobject;
780 	vm_page_t m;
781 
782 	if (object == NULL)
783 		return;
784 
785 	end = pindex + count;
786 
787 	for (; pindex < end; pindex += 1) {
788 
789 relookup:
790 		tobject = object;
791 		tpindex = pindex;
792 shadowlookup:
793 		m = vm_page_lookup(tobject, tpindex);
794 		if (m == NULL) {
795 			if (tobject->type != OBJT_DEFAULT) {
796 				continue;
797 			}
798 
799 			tobject = tobject->backing_object;
800 			if ((tobject == NULL) || (tobject->ref_count != 1)) {
801 				continue;
802 			}
803 			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
804 			goto shadowlookup;
805 		}
806 
807 		/*
808 		 * If the page is busy or not in a normal active state,
809 		 * we skip it.  Things can break if we mess with pages
810 		 * in any of the below states.
811 		 */
812 		if (m->hold_count || m->wire_count ||
813 			m->valid != VM_PAGE_BITS_ALL) {
814 			continue;
815 		}
816 
817  		if (vm_page_sleep(m, "madvpo", &m->busy))
818   			goto relookup;
819 
820 		if (advise == MADV_WILLNEED) {
821 			vm_page_activate(m);
822 		} else if (advise == MADV_DONTNEED) {
823 			vm_page_deactivate(m);
824 		} else if (advise == MADV_FREE) {
825 			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
826 			m->dirty = 0;
827 			/*
828 			 * Force a demand zero if attempt to read from swap.
829 			 * We currently don't handle vnode files correctly,
830 			 * and will reread stale contents unnecessarily.
831 			 */
832 			if (object->type == OBJT_SWAP)
833 				swap_pager_dmzspace(tobject, m->pindex, 1);
834 		}
835 	}
836 }
837 
838 /*
839  *	vm_object_shadow:
840  *
841  *	Create a new object which is backed by the
842  *	specified existing object range.  The source
843  *	object reference is deallocated.
844  *
845  *	The new object and offset into that object
846  *	are returned in the source parameters.
847  */
848 
849 void
850 vm_object_shadow(object, offset, length)
851 	vm_object_t *object;	/* IN/OUT */
852 	vm_ooffset_t *offset;	/* IN/OUT */
853 	vm_size_t length;
854 {
855 	register vm_object_t source;
856 	register vm_object_t result;
857 
858 	source = *object;
859 
860 	/*
861 	 * Allocate a new object with the given length
862 	 */
863 
864 	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
865 		panic("vm_object_shadow: no object for shadowing");
866 
867 	/*
868 	 * The new object shadows the source object, adding a reference to it.
869 	 * Our caller changes his reference to point to the new object,
870 	 * removing a reference to the source object.  Net result: no change
871 	 * of reference count.
872 	 */
873 	result->backing_object = source;
874 	if (source) {
875 		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
876 		vm_object_clear_flag(source, OBJ_ONEMAPPING);
877 		source->shadow_count++;
878 		source->generation++;
879 	}
880 
881 	/*
882 	 * Store the offset into the source object, and fix up the offset into
883 	 * the new object.
884 	 */
885 
886 	result->backing_object_offset = *offset;
887 
888 	/*
889 	 * Return the new things
890 	 */
891 
892 	*offset = 0;
893 	*object = result;
894 }
895 
896 
897 /*
898  * this version of collapse allows the operation to occur earlier and
899  * when paging_in_progress is true for an object...  This is not a complete
900  * operation, but should plug 99.9% of the rest of the leaks.
901  */
902 static void
903 vm_object_qcollapse(object)
904 	register vm_object_t object;
905 {
906 	register vm_object_t backing_object;
907 	register vm_pindex_t backing_offset_index, paging_offset_index;
908 	vm_pindex_t backing_object_paging_offset_index;
909 	vm_pindex_t new_pindex;
910 	register vm_page_t p, pp;
911 	register vm_size_t size;
912 
913 	backing_object = object->backing_object;
914 	if (backing_object->ref_count != 1)
915 		return;
916 
917 	backing_object->ref_count += 2;
918 
919 	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
920 	backing_object_paging_offset_index = OFF_TO_IDX(backing_object->paging_offset);
921 	paging_offset_index = OFF_TO_IDX(object->paging_offset);
922 	size = object->size;
923 	p = TAILQ_FIRST(&backing_object->memq);
924 	while (p) {
925 		vm_page_t next;
926 
927 		next = TAILQ_NEXT(p, listq);
928 		if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
929 		    !p->valid || p->hold_count || p->wire_count || p->busy) {
930 			p = next;
931 			continue;
932 		}
933 		vm_page_busy(p);
934 
935 		new_pindex = p->pindex - backing_offset_index;
936 		if (p->pindex < backing_offset_index ||
937 		    new_pindex >= size) {
938 			if (backing_object->type == OBJT_SWAP)
939 				swap_pager_freespace(backing_object,
940 				    backing_object_paging_offset_index+p->pindex,
941 				    1);
942 			vm_page_protect(p, VM_PROT_NONE);
943 			vm_page_free(p);
944 		} else {
945 			pp = vm_page_lookup(object, new_pindex);
946 			if (pp != NULL ||
947 				(object->type == OBJT_SWAP && vm_pager_has_page(object,
948 				    paging_offset_index + new_pindex, NULL, NULL))) {
949 				if (backing_object->type == OBJT_SWAP)
950 					swap_pager_freespace(backing_object,
951 					    backing_object_paging_offset_index + p->pindex, 1);
952 				vm_page_protect(p, VM_PROT_NONE);
953 				vm_page_free(p);
954 			} else {
955 				if (backing_object->type == OBJT_SWAP)
956 					swap_pager_freespace(backing_object,
957 					    backing_object_paging_offset_index + p->pindex, 1);
958 
959 				if ((p->queue - p->pc) == PQ_CACHE)
960 					vm_page_deactivate(p);
961 				else
962 					vm_page_protect(p, VM_PROT_NONE);
963 
964 				vm_page_rename(p, object, new_pindex);
965 				p->dirty = VM_PAGE_BITS_ALL;
966 			}
967 		}
968 		p = next;
969 	}
970 	backing_object->ref_count -= 2;
971 }
972 
973 /*
974  *	vm_object_collapse:
975  *
976  *	Collapse an object with the object backing it.
977  *	Pages in the backing object are moved into the
978  *	parent, and the backing object is deallocated.
979  */
980 void
981 vm_object_collapse(object)
982 	vm_object_t object;
983 
984 {
985 	vm_object_t backing_object;
986 	vm_ooffset_t backing_offset;
987 	vm_size_t size;
988 	vm_pindex_t new_pindex, backing_offset_index;
989 	vm_page_t p, pp;
990 
991 	while (TRUE) {
992 		/*
993 		 * Verify that the conditions are right for collapse:
994 		 *
995 		 * The object exists and no pages in it are currently being paged
996 		 * out.
997 		 */
998 		if (object == NULL)
999 			return;
1000 
1001 		/*
1002 		 * Make sure there is a backing object.
1003 		 */
1004 		if ((backing_object = object->backing_object) == NULL)
1005 			return;
1006 
1007 		/*
1008 		 * we check the backing object first, because it is most likely
1009 		 * not collapsable.
1010 		 */
1011 		if (backing_object->handle != NULL ||
1012 		    (backing_object->type != OBJT_DEFAULT &&
1013 		     backing_object->type != OBJT_SWAP) ||
1014 		    (backing_object->flags & OBJ_DEAD) ||
1015 		    object->handle != NULL ||
1016 		    (object->type != OBJT_DEFAULT &&
1017 		     object->type != OBJT_SWAP) ||
1018 		    (object->flags & OBJ_DEAD)) {
1019 			return;
1020 		}
1021 
1022 		if (object->paging_in_progress != 0 ||
1023 		    backing_object->paging_in_progress != 0) {
1024 			vm_object_qcollapse(object);
1025 			return;
1026 		}
1027 
1028 		/*
1029 		 * We know that we can either collapse the backing object (if
1030 		 * the parent is the only reference to it) or (perhaps) remove
1031 		 * the parent's reference to it.
1032 		 */
1033 
1034 		backing_offset = object->backing_object_offset;
1035 		backing_offset_index = OFF_TO_IDX(backing_offset);
1036 		size = object->size;
1037 
1038 		/*
1039 		 * If there is exactly one reference to the backing object, we
1040 		 * can collapse it into the parent.
1041 		 */
1042 
1043 		if (backing_object->ref_count == 1) {
1044 
1045 			vm_object_set_flag(backing_object, OBJ_DEAD);
1046 			/*
1047 			 * We can collapse the backing object.
1048 			 *
1049 			 * Move all in-memory pages from backing_object to the
1050 			 * parent.  Pages that have been paged out will be
1051 			 * overwritten by any of the parent's pages that
1052 			 * shadow them.
1053 			 */
1054 
1055 			while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
1056 
1057 				new_pindex = p->pindex - backing_offset_index;
1058 				vm_page_busy(p);
1059 
1060 				/*
1061 				 * If the parent has a page here, or if this
1062 				 * page falls outside the parent, dispose of
1063 				 * it.
1064 				 *
1065 				 * Otherwise, move it as planned.
1066 				 */
1067 
1068 				if (p->pindex < backing_offset_index ||
1069 				    new_pindex >= size) {
1070 					vm_page_protect(p, VM_PROT_NONE);
1071 					vm_page_free(p);
1072 				} else {
1073 					pp = vm_page_lookup(object, new_pindex);
1074 					if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
1075 					    OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL))) {
1076 						vm_page_protect(p, VM_PROT_NONE);
1077 						vm_page_free(p);
1078 					} else {
1079 						if ((p->queue - p->pc) == PQ_CACHE)
1080 							vm_page_deactivate(p);
1081 						else
1082 							vm_page_protect(p, VM_PROT_NONE);
1083 						vm_page_rename(p, object, new_pindex);
1084 						p->dirty = VM_PAGE_BITS_ALL;
1085 					}
1086 				}
1087 			}
1088 
1089 			/*
1090 			 * Move the pager from backing_object to object.
1091 			 */
1092 
1093 			if (backing_object->type == OBJT_SWAP) {
1094 				vm_object_pip_add(backing_object, 1);
1095 				if (object->type == OBJT_SWAP) {
1096 					vm_object_pip_add(object, 1);
1097 					/*
1098 					 * copy shadow object pages into ours
1099 					 * and destroy unneeded pages in
1100 					 * shadow object.
1101 					 */
1102 					swap_pager_copy(
1103 					    backing_object,
1104 					    OFF_TO_IDX(backing_object->paging_offset),
1105 					    object,
1106 					    OFF_TO_IDX(object->paging_offset),
1107 					    OFF_TO_IDX(object->backing_object_offset), TRUE);
1108 					vm_object_pip_wakeup(object);
1109 				} else {
1110 					vm_object_pip_add(object, 1);
1111 					/*
1112 					 * move the shadow backing_object's pager data to
1113 					 * "object" and convert "object" type to OBJT_SWAP.
1114 					 */
1115 					object->type = OBJT_SWAP;
1116 					object->un_pager.swp.swp_nblocks =
1117 					    backing_object->un_pager.swp.swp_nblocks;
1118 					object->un_pager.swp.swp_allocsize =
1119 					    backing_object->un_pager.swp.swp_allocsize;
1120 					object->un_pager.swp.swp_blocks =
1121 					    backing_object->un_pager.swp.swp_blocks;
1122 					object->un_pager.swp.swp_poip =		/* XXX */
1123 					    backing_object->un_pager.swp.swp_poip;
1124 					object->paging_offset = backing_object->paging_offset + backing_offset;
1125 					TAILQ_INSERT_TAIL(&swap_pager_un_object_list, object, pager_object_list);
1126 
1127 					/*
1128 					 * Convert backing object from OBJT_SWAP to
1129 					 * OBJT_DEFAULT. XXX - only the TAILQ_REMOVE is
1130 					 * actually necessary.
1131 					 */
1132 					backing_object->type = OBJT_DEFAULT;
1133 					TAILQ_REMOVE(&swap_pager_un_object_list, backing_object, pager_object_list);
1134 					/*
1135 					 * free unnecessary blocks
1136 					 */
1137 					swap_pager_freespace(object, 0,
1138 						OFF_TO_IDX(object->paging_offset));
1139 					vm_object_pip_wakeup(object);
1140 				}
1141 
1142 				vm_object_pip_wakeup(backing_object);
1143 			}
1144 			/*
1145 			 * Object now shadows whatever backing_object did.
1146 			 * Note that the reference to backing_object->backing_object
1147 			 * moves from within backing_object to within object.
1148 			 */
1149 
1150 			TAILQ_REMOVE(&object->backing_object->shadow_head, object,
1151 			    shadow_list);
1152 			object->backing_object->shadow_count--;
1153 			object->backing_object->generation++;
1154 			if (backing_object->backing_object) {
1155 				TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
1156 				    backing_object, shadow_list);
1157 				backing_object->backing_object->shadow_count--;
1158 				backing_object->backing_object->generation++;
1159 			}
1160 			object->backing_object = backing_object->backing_object;
1161 			if (object->backing_object) {
1162 				TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
1163 				    object, shadow_list);
1164 				object->backing_object->shadow_count++;
1165 				object->backing_object->generation++;
1166 			}
1167 
1168 			object->backing_object_offset += backing_object->backing_object_offset;
1169 			/*
1170 			 * Discard backing_object.
1171 			 *
1172 			 * Since the backing object has no pages, no pager left,
1173 			 * and no object references within it, all that is
1174 			 * necessary is to dispose of it.
1175 			 */
1176 
1177 			TAILQ_REMOVE(&vm_object_list, backing_object,
1178 			    object_list);
1179 			vm_object_count--;
1180 
1181 			zfree(obj_zone, backing_object);
1182 
1183 			object_collapses++;
1184 		} else {
1185 			vm_object_t new_backing_object;
1186 			/*
1187 			 * If all of the pages in the backing object are
1188 			 * shadowed by the parent object, the parent object no
1189 			 * longer has to shadow the backing object; it can
1190 			 * shadow the next one in the chain.
1191 			 *
1192 			 * The backing object must not be paged out - we'd have
1193 			 * to check all of the paged-out pages, as well.
1194 			 */
1195 
1196 			if (backing_object->type != OBJT_DEFAULT) {
1197 				return;
1198 			}
1199 			/*
1200 			 * Should have a check for a 'small' number of pages
1201 			 * here.
1202 			 */
1203 
1204 			for (p = TAILQ_FIRST(&backing_object->memq); p;
1205 					p = TAILQ_NEXT(p, listq)) {
1206 
1207 				new_pindex = p->pindex - backing_offset_index;
1208 				vm_page_busy(p);
1209 
1210 				/*
1211 				 * If the parent has a page here, or if this
1212 				 * page falls outside the parent, keep going.
1213 				 *
1214 				 * Otherwise, the backing_object must be left in
1215 				 * the chain.
1216 				 */
1217 
1218 				if (p->pindex >= backing_offset_index &&
1219 					new_pindex <= size) {
1220 
1221 					pp = vm_page_lookup(object, new_pindex);
1222 
1223 					if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1224 						vm_page_wakeup(p);
1225 						return;
1226 					}
1227 
1228 					vm_page_busy(pp);
1229 					if ((pp->valid == 0) &&
1230 				   	    !vm_pager_has_page(object, OFF_TO_IDX(object->paging_offset) + new_pindex, NULL, NULL)) {
1231 						/*
1232 						 * Page still needed. Can't go any
1233 						 * further.
1234 						 */
1235 						vm_page_wakeup(pp);
1236 						vm_page_wakeup(p);
1237 						return;
1238 					}
1239 					vm_page_wakeup(pp);
1240 				}
1241 				vm_page_wakeup(p);
1242 			}
1243 
1244 			/*
1245 			 * Make the parent shadow the next object in the
1246 			 * chain.  Deallocating backing_object will not remove
1247 			 * it, since its reference count is at least 2.
1248 			 */
1249 
1250 			TAILQ_REMOVE(&backing_object->shadow_head,
1251 			    object, shadow_list);
1252 			backing_object->shadow_count--;
1253 			backing_object->generation++;
1254 
1255 			new_backing_object = backing_object->backing_object;
1256 			if (object->backing_object = new_backing_object) {
1257 				vm_object_reference(new_backing_object);
1258 				TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
1259 				    object, shadow_list);
1260 				new_backing_object->shadow_count++;
1261 				new_backing_object->generation++;
1262 				object->backing_object_offset +=
1263 					backing_object->backing_object_offset;
1264 			}
1265 
1266 			/*
1267 			 * Drop the reference count on backing_object. Since
1268 			 * its ref_count was at least 2, it will not vanish;
1269 			 * so we don't need to call vm_object_deallocate, but
1270 			 * we do anyway.
1271 			 */
1272 			vm_object_deallocate(backing_object);
1273 			object_bypasses++;
1274 		}
1275 
1276 		/*
1277 		 * Try again with this object's new backing object.
1278 		 */
1279 	}
1280 }
1281 
1282 /*
1283  *	vm_object_page_remove: [internal]
1284  *
1285  *	Removes all physical pages in the specified
1286  *	object range from the object's list of pages.
1287  *
1288  *	The object must be locked.
1289  */
1290 void
1291 vm_object_page_remove(object, start, end, clean_only)
1292 	register vm_object_t object;
1293 	register vm_pindex_t start;
1294 	register vm_pindex_t end;
1295 	boolean_t clean_only;
1296 {
1297 	register vm_page_t p, next;
1298 	unsigned int size;
1299 	int all;
1300 
1301 	if (object == NULL)
1302 		return;
1303 
1304 	all = ((end == 0) && (start == 0));
1305 
1306 	vm_object_pip_add(object, 1);
1307 again:
1308 	size = end - start;
1309 	if (all || size > 4 || size >= object->size / 4) {
1310 		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1311 			next = TAILQ_NEXT(p, listq);
1312 			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1313 				if (p->wire_count != 0) {
1314 					vm_page_protect(p, VM_PROT_NONE);
1315 					if (!clean_only)
1316 						p->valid = 0;
1317 					continue;
1318 				}
1319 
1320 				/*
1321 				 * The busy flags are only cleared at
1322 				 * interrupt -- minimize the spl transitions
1323 				 */
1324 
1325  				if (vm_page_sleep(p, "vmopar", &p->busy))
1326  					goto again;
1327 
1328 				if (clean_only && p->valid) {
1329 					vm_page_test_dirty(p);
1330 					if (p->valid & p->dirty)
1331 						continue;
1332 				}
1333 
1334 				vm_page_busy(p);
1335 				vm_page_protect(p, VM_PROT_NONE);
1336 				vm_page_free(p);
1337 			}
1338 		}
1339 	} else {
1340 		while (size > 0) {
1341 			if ((p = vm_page_lookup(object, start)) != 0) {
1342 
1343 				if (p->wire_count != 0) {
1344 					vm_page_protect(p, VM_PROT_NONE);
1345 					if (!clean_only)
1346 						p->valid = 0;
1347 					start += 1;
1348 					size -= 1;
1349 					continue;
1350 				}
1351 
1352 				/*
1353 				 * The busy flags are only cleared at
1354 				 * interrupt -- minimize the spl transitions
1355 				 */
1356  				if (vm_page_sleep(p, "vmopar", &p->busy))
1357 					goto again;
1358 
1359 				if (clean_only && p->valid) {
1360 					vm_page_test_dirty(p);
1361 					if (p->valid & p->dirty) {
1362 						start += 1;
1363 						size -= 1;
1364 						continue;
1365 					}
1366 				}
1367 
1368 				vm_page_busy(p);
1369 				vm_page_protect(p, VM_PROT_NONE);
1370 				vm_page_free(p);
1371 			}
1372 			start += 1;
1373 			size -= 1;
1374 		}
1375 	}
1376 	vm_object_pip_wakeup(object);
1377 }
1378 
1379 /*
1380  *	Routine:	vm_object_coalesce
1381  *	Function:	Coalesces two objects backing up adjoining
1382  *			regions of memory into a single object.
1383  *
1384  *	returns TRUE if objects were combined.
1385  *
1386  *	NOTE:	Only works at the moment if the second object is NULL -
1387  *		if it's not, which object do we lock first?
1388  *
1389  *	Parameters:
1390  *		prev_object	First object to coalesce
1391  *		prev_offset	Offset into prev_object
1392  *		next_object	Second object into coalesce
1393  *		next_offset	Offset into next_object
1394  *
1395  *		prev_size	Size of reference to prev_object
1396  *		next_size	Size of reference to next_object
1397  *
1398  *	Conditions:
1399  *	The object must *not* be locked.
1400  */
1401 boolean_t
1402 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1403 	register vm_object_t prev_object;
1404 	vm_pindex_t prev_pindex;
1405 	vm_size_t prev_size, next_size;
1406 {
1407 	vm_size_t newsize;
1408 
1409 	if (prev_object == NULL) {
1410 		return (TRUE);
1411 	}
1412 
1413 	if (prev_object->type != OBJT_DEFAULT) {
1414 		return (FALSE);
1415 	}
1416 
1417 	/*
1418 	 * Try to collapse the object first
1419 	 */
1420 	vm_object_collapse(prev_object);
1421 
1422 	/*
1423 	 * Can't coalesce if: . more than one reference . paged out . shadows
1424 	 * another object . has a copy elsewhere (any of which mean that the
1425 	 * pages not mapped to prev_entry may be in use anyway)
1426 	 */
1427 
1428 	if (prev_object->backing_object != NULL) {
1429 		return (FALSE);
1430 	}
1431 
1432 	prev_size >>= PAGE_SHIFT;
1433 	next_size >>= PAGE_SHIFT;
1434 
1435 	if ((prev_object->ref_count > 1) &&
1436 	    (prev_object->size != prev_pindex + prev_size)) {
1437 		return (FALSE);
1438 	}
1439 
1440 	/*
1441 	 * Remove any pages that may still be in the object from a previous
1442 	 * deallocation.
1443 	 */
1444 
1445 	vm_object_page_remove(prev_object,
1446 	    prev_pindex + prev_size,
1447 	    prev_pindex + prev_size + next_size, FALSE);
1448 
1449 	/*
1450 	 * Extend the object if necessary.
1451 	 */
1452 	newsize = prev_pindex + prev_size + next_size;
1453 	if (newsize > prev_object->size)
1454 		prev_object->size = newsize;
1455 
1456 	return (TRUE);
1457 }
1458 
1459 #include "opt_ddb.h"
1460 #ifdef DDB
1461 #include <sys/kernel.h>
1462 
1463 #include <machine/cons.h>
1464 
1465 #include <ddb/ddb.h>
1466 
1467 static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1468 				       vm_map_entry_t entry));
1469 static int	vm_object_in_map __P((vm_object_t object));
1470 
1471 static int
1472 _vm_object_in_map(map, object, entry)
1473 	vm_map_t map;
1474 	vm_object_t object;
1475 	vm_map_entry_t entry;
1476 {
1477 	vm_map_t tmpm;
1478 	vm_map_entry_t tmpe;
1479 	vm_object_t obj;
1480 	int entcount;
1481 
1482 	if (map == 0)
1483 		return 0;
1484 
1485 	if (entry == 0) {
1486 		tmpe = map->header.next;
1487 		entcount = map->nentries;
1488 		while (entcount-- && (tmpe != &map->header)) {
1489 			if( _vm_object_in_map(map, object, tmpe)) {
1490 				return 1;
1491 			}
1492 			tmpe = tmpe->next;
1493 		}
1494 	} else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1495 		tmpm = entry->object.share_map;
1496 		tmpe = tmpm->header.next;
1497 		entcount = tmpm->nentries;
1498 		while (entcount-- && tmpe != &tmpm->header) {
1499 			if( _vm_object_in_map(tmpm, object, tmpe)) {
1500 				return 1;
1501 			}
1502 			tmpe = tmpe->next;
1503 		}
1504 	} else if (obj = entry->object.vm_object) {
1505 		for(; obj; obj=obj->backing_object)
1506 			if( obj == object) {
1507 				return 1;
1508 			}
1509 	}
1510 	return 0;
1511 }
1512 
1513 static int
1514 vm_object_in_map( object)
1515 	vm_object_t object;
1516 {
1517 	struct proc *p;
1518 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1519 		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1520 			continue;
1521 		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1522 			return 1;
1523 	}
1524 	if( _vm_object_in_map( kernel_map, object, 0))
1525 		return 1;
1526 	if( _vm_object_in_map( kmem_map, object, 0))
1527 		return 1;
1528 	if( _vm_object_in_map( pager_map, object, 0))
1529 		return 1;
1530 	if( _vm_object_in_map( buffer_map, object, 0))
1531 		return 1;
1532 	if( _vm_object_in_map( io_map, object, 0))
1533 		return 1;
1534 	if( _vm_object_in_map( phys_map, object, 0))
1535 		return 1;
1536 	if( _vm_object_in_map( mb_map, object, 0))
1537 		return 1;
1538 	if( _vm_object_in_map( u_map, object, 0))
1539 		return 1;
1540 	return 0;
1541 }
1542 
1543 DB_SHOW_COMMAND(vmochk, vm_object_check)
1544 {
1545 	vm_object_t object;
1546 
1547 	/*
1548 	 * make sure that internal objs are in a map somewhere
1549 	 * and none have zero ref counts.
1550 	 */
1551 	for (object = TAILQ_FIRST(&vm_object_list);
1552 			object != NULL;
1553 			object = TAILQ_NEXT(object, object_list)) {
1554 		if (object->handle == NULL &&
1555 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1556 			if (object->ref_count == 0) {
1557 				db_printf("vmochk: internal obj has zero ref count: %d\n",
1558 					object->size);
1559 			}
1560 			if (!vm_object_in_map(object)) {
1561 				db_printf(
1562 			"vmochk: internal obj is not in a map: "
1563 			"ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1564 				    object->ref_count, (u_long)object->size,
1565 				    (u_long)object->size,
1566 				    (void *)object->backing_object);
1567 			}
1568 		}
1569 	}
1570 }
1571 
1572 /*
1573  *	vm_object_print:	[ debug ]
1574  */
1575 DB_SHOW_COMMAND(object, vm_object_print_static)
1576 {
1577 	/* XXX convert args. */
1578 	vm_object_t object = (vm_object_t)addr;
1579 	boolean_t full = have_addr;
1580 
1581 	register vm_page_t p;
1582 
1583 	/* XXX count is an (unused) arg.  Avoid shadowing it. */
1584 #define	count	was_count
1585 
1586 	register int count;
1587 
1588 	if (object == NULL)
1589 		return;
1590 
1591 	db_iprintf(
1592 	    "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1593 	    object, (int)object->type, (u_long)object->size,
1594 	    object->resident_page_count, object->ref_count, object->flags);
1595 	/*
1596 	 * XXX no %qd in kernel.  Truncate object->paging_offset and
1597 	 * object->backing_object_offset.
1598 	 */
1599 	db_iprintf(" sref=%d, offset=0x%lx, backing_object(%d)=(%p)+0x%lx\n",
1600 	    object->shadow_count, (long)object->paging_offset,
1601 	    object->backing_object ? object->backing_object->ref_count : 0,
1602 	    object->backing_object, (long)object->backing_object_offset);
1603 
1604 	if (!full)
1605 		return;
1606 
1607 	db_indent += 2;
1608 	count = 0;
1609 	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1610 		if (count == 0)
1611 			db_iprintf("memory:=");
1612 		else if (count == 6) {
1613 			db_printf("\n");
1614 			db_iprintf(" ...");
1615 			count = 0;
1616 		} else
1617 			db_printf(",");
1618 		count++;
1619 
1620 		db_printf("(off=0x%lx,page=0x%lx)",
1621 		    (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1622 	}
1623 	if (count != 0)
1624 		db_printf("\n");
1625 	db_indent -= 2;
1626 }
1627 
1628 /* XXX. */
1629 #undef count
1630 
1631 /* XXX need this non-static entry for calling from vm_map_print. */
1632 void
1633 vm_object_print(addr, have_addr, count, modif)
1634         /* db_expr_t */ long addr;
1635 	boolean_t have_addr;
1636 	/* db_expr_t */ long count;
1637 	char *modif;
1638 {
1639 	vm_object_print_static(addr, have_addr, count, modif);
1640 }
1641 
1642 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
1643 {
1644 	vm_object_t object;
1645 	int nl = 0;
1646 	int c;
1647 	for (object = TAILQ_FIRST(&vm_object_list);
1648 			object != NULL;
1649 			object = TAILQ_NEXT(object, object_list)) {
1650 		vm_pindex_t idx, fidx;
1651 		vm_pindex_t osize;
1652 		vm_offset_t pa = -1, padiff;
1653 		int rcount;
1654 		vm_page_t m;
1655 
1656 		db_printf("new object: %p\n", (void *)object);
1657 		if ( nl > 18) {
1658 			c = cngetc();
1659 			if (c != ' ')
1660 				return;
1661 			nl = 0;
1662 		}
1663 		nl++;
1664 		rcount = 0;
1665 		fidx = 0;
1666 		osize = object->size;
1667 		if (osize > 128)
1668 			osize = 128;
1669 		for(idx=0;idx<osize;idx++) {
1670 			m = vm_page_lookup(object, idx);
1671 			if (m == NULL) {
1672 				if (rcount) {
1673 					db_printf(" index(%d)run(%d)pa(0x%x)\n",
1674 						fidx, rcount, pa);
1675 					if ( nl > 18) {
1676 						c = cngetc();
1677 						if (c != ' ')
1678 							return;
1679 						nl = 0;
1680 					}
1681 					nl++;
1682 					rcount = 0;
1683 				}
1684 				continue;
1685 			}
1686 
1687 
1688 			if (rcount &&
1689 				(VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
1690 				++rcount;
1691 				continue;
1692 			}
1693 			if (rcount) {
1694 				padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
1695 				padiff >>= PAGE_SHIFT;
1696 				padiff &= PQ_L2_MASK;
1697 				if (padiff == 0) {
1698 					pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
1699 					++rcount;
1700 					continue;
1701 				}
1702 				db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
1703 				db_printf("pd(%d)\n", padiff);
1704 				if ( nl > 18) {
1705 					c = cngetc();
1706 					if (c != ' ')
1707 						return;
1708 					nl = 0;
1709 				}
1710 				nl++;
1711 			}
1712 			fidx = idx;
1713 			pa = VM_PAGE_TO_PHYS(m);
1714 			rcount = 1;
1715 		}
1716 		if (rcount) {
1717 			db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
1718 			if ( nl > 18) {
1719 				c = cngetc();
1720 				if (c != ' ')
1721 					return;
1722 				nl = 0;
1723 			}
1724 			nl++;
1725 		}
1726 	}
1727 }
1728 #endif /* DDB */
1729