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