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