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