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