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