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