vm_object.c revision 49858
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.165 1999/08/15 21:55:20 alc Exp $
65 */
66
67/*
68 *	Virtual memory object module.
69 */
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/proc.h>		/* for curproc, pageproc */
74#include <sys/vnode.h>
75#include <sys/vmmeter.h>
76#include <sys/mman.h>
77#include <sys/mount.h>
78
79#include <vm/vm.h>
80#include <vm/vm_param.h>
81#include <vm/vm_prot.h>
82#include <vm/pmap.h>
83#include <vm/vm_map.h>
84#include <vm/vm_object.h>
85#include <vm/vm_page.h>
86#include <vm/vm_pageout.h>
87#include <vm/vm_pager.h>
88#include <vm/swap_pager.h>
89#include <vm/vm_kern.h>
90#include <vm/vm_extern.h>
91#include <vm/vm_zone.h>
92
93static void	vm_object_qcollapse __P((vm_object_t object));
94
95/*
96 *	Virtual memory objects maintain the actual data
97 *	associated with allocated virtual memory.  A given
98 *	page of memory exists within exactly one object.
99 *
100 *	An object is only deallocated when all "references"
101 *	are given up.  Only one "reference" to a given
102 *	region of an object should be writeable.
103 *
104 *	Associated with each object is a list of all resident
105 *	memory pages belonging to that object; this list is
106 *	maintained by the "vm_page" module, and locked by the object's
107 *	lock.
108 *
109 *	Each object also records a "pager" routine which is
110 *	used to retrieve (and store) pages to the proper backing
111 *	storage.  In addition, objects may be backed by other
112 *	objects from which they were virtual-copied.
113 *
114 *	The only items within the object structure which are
115 *	modified after time of creation are:
116 *		reference count		locked by object's lock
117 *		pager routine		locked by object's lock
118 *
119 */
120
121struct object_q vm_object_list;
122#ifndef NULL_SIMPLELOCKS
123static struct simplelock vm_object_list_lock;
124#endif
125static long vm_object_count;		/* count of all objects */
126vm_object_t kernel_object;
127vm_object_t kmem_object;
128static struct vm_object kernel_object_store;
129static struct vm_object kmem_object_store;
130extern int vm_pageout_page_count;
131
132static long object_collapses;
133static long object_bypasses;
134static int next_index;
135static vm_zone_t obj_zone;
136static struct vm_zone obj_zone_store;
137static int object_hash_rand;
138#define VM_OBJECTS_INIT 256
139static struct vm_object vm_objects_init[VM_OBJECTS_INIT];
140
141void
142_vm_object_allocate(type, size, object)
143	objtype_t type;
144	vm_size_t size;
145	vm_object_t object;
146{
147	int incr;
148	TAILQ_INIT(&object->memq);
149	TAILQ_INIT(&object->shadow_head);
150
151	object->type = type;
152	object->size = size;
153	object->ref_count = 1;
154	object->flags = 0;
155	if ((object->type == OBJT_DEFAULT) || (object->type == OBJT_SWAP))
156		vm_object_set_flag(object, OBJ_ONEMAPPING);
157	object->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	VOP_FSYNC(vp, NULL, (pagerflags & VM_PAGER_PUT_SYNC)?MNT_WAIT:0, curproc);
645
646	vm_object_clear_flag(object, OBJ_CLEANING);
647	return;
648}
649
650#ifdef not_used
651/* XXX I cannot tell if this should be an exported symbol */
652/*
653 *	vm_object_deactivate_pages
654 *
655 *	Deactivate all pages in the specified object.  (Keep its pages
656 *	in memory even though it is no longer referenced.)
657 *
658 *	The object must be locked.
659 */
660static void
661vm_object_deactivate_pages(object)
662	vm_object_t object;
663{
664	vm_page_t p, next;
665
666	for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
667		next = TAILQ_NEXT(p, listq);
668		vm_page_deactivate(p);
669	}
670}
671#endif
672
673/*
674 * Same as vm_object_pmap_copy, except range checking really
675 * works, and is meant for small sections of an object.
676 *
677 * This code protects resident pages by making them read-only
678 * and is typically called on a fork or split when a page
679 * is converted to copy-on-write.
680 *
681 * NOTE: If the page is already at VM_PROT_NONE, calling
682 * vm_page_protect will have no effect.
683 */
684
685void
686vm_object_pmap_copy_1(object, start, end)
687	vm_object_t object;
688	vm_pindex_t start;
689	vm_pindex_t end;
690{
691	vm_pindex_t idx;
692	vm_page_t p;
693
694	if (object == NULL || (object->flags & OBJ_WRITEABLE) == 0)
695		return;
696
697	for (idx = start; idx < end; idx++) {
698		p = vm_page_lookup(object, idx);
699		if (p == NULL)
700			continue;
701		vm_page_protect(p, VM_PROT_READ);
702	}
703}
704
705/*
706 *	vm_object_pmap_remove:
707 *
708 *	Removes all physical pages in the specified
709 *	object range from all physical maps.
710 *
711 *	The object must *not* be locked.
712 */
713void
714vm_object_pmap_remove(object, start, end)
715	vm_object_t object;
716	vm_pindex_t start;
717	vm_pindex_t end;
718{
719	vm_page_t p;
720
721	if (object == NULL)
722		return;
723	for (p = TAILQ_FIRST(&object->memq);
724		p != NULL;
725		p = TAILQ_NEXT(p, listq)) {
726		if (p->pindex >= start && p->pindex < end)
727			vm_page_protect(p, VM_PROT_NONE);
728	}
729	if ((start == 0) && (object->size == end))
730		vm_object_clear_flag(object, OBJ_WRITEABLE);
731}
732
733/*
734 *	vm_object_madvise:
735 *
736 *	Implements the madvise function at the object/page level.
737 *
738 *	MADV_WILLNEED	(any object)
739 *
740 *	    Activate the specified pages if they are resident.
741 *
742 *	MADV_DONTNEED	(any object)
743 *
744 *	    Deactivate the specified pages if they are resident.
745 *
746 *	MADV_FREE	(OBJT_DEFAULT/OBJT_SWAP objects,
747 *			 OBJ_ONEMAPPING only)
748 *
749 *	    Deactivate and clean the specified pages if they are
750 *	    resident.  This permits the process to reuse the pages
751 *	    without faulting or the kernel to reclaim the pages
752 *	    without I/O.
753 */
754void
755vm_object_madvise(object, pindex, count, advise)
756	vm_object_t object;
757	vm_pindex_t pindex;
758	int count;
759	int advise;
760{
761	vm_pindex_t end, tpindex;
762	vm_object_t tobject;
763	vm_page_t m;
764
765	if (object == NULL)
766		return;
767
768	end = pindex + count;
769
770	/*
771	 * Locate and adjust resident pages
772	 */
773
774	for (; pindex < end; pindex += 1) {
775relookup:
776		tobject = object;
777		tpindex = pindex;
778shadowlookup:
779		/*
780		 * MADV_FREE only operates on OBJT_DEFAULT or OBJT_SWAP pages
781		 * and those pages must be OBJ_ONEMAPPING.
782		 */
783		if (advise == MADV_FREE) {
784			if ((tobject->type != OBJT_DEFAULT &&
785			     tobject->type != OBJT_SWAP) ||
786			    (tobject->flags & OBJ_ONEMAPPING) == 0) {
787				continue;
788			}
789		}
790
791		m = vm_page_lookup(tobject, tpindex);
792
793		if (m == NULL) {
794			/*
795			 * There may be swap even if there is no backing page
796			 */
797			if (advise == MADV_FREE && tobject->type == OBJT_SWAP)
798				swap_pager_freespace(tobject, tpindex, 1);
799
800			/*
801			 * next object
802			 */
803			tobject = tobject->backing_object;
804			if (tobject == NULL)
805				continue;
806			tpindex += OFF_TO_IDX(tobject->backing_object_offset);
807			goto shadowlookup;
808		}
809
810		/*
811		 * If the page is busy or not in a normal active state,
812		 * we skip it.  Things can break if we mess with pages
813		 * in any of the below states.
814		 */
815		if (
816		    m->hold_count ||
817		    m->wire_count ||
818		    m->valid != VM_PAGE_BITS_ALL
819		) {
820			continue;
821		}
822
823 		if (vm_page_sleep_busy(m, TRUE, "madvpo"))
824  			goto relookup;
825
826		if (advise == MADV_WILLNEED) {
827			vm_page_activate(m);
828		} else if (advise == MADV_DONTNEED) {
829			vm_page_deactivate(m);
830		} else if (advise == MADV_FREE) {
831			/*
832			 * Mark the page clean.  This will allow the page
833			 * to be freed up by the system.  However, such pages
834			 * are often reused quickly by malloc()/free()
835			 * so we do not do anything that would cause
836			 * a page fault if we can help it.
837			 *
838			 * Specifically, we do not try to actually free
839			 * the page now nor do we try to put it in the
840			 * cache (which would cause a page fault on reuse).
841			 *
842			 * But we do make the page is freeable as we
843			 * can without actually taking the step of unmapping
844			 * it.
845			 */
846			pmap_clear_modify(VM_PAGE_TO_PHYS(m));
847			m->dirty = 0;
848			m->act_count = 0;
849			vm_page_deactivate(m);
850			if (tobject->type == OBJT_SWAP)
851				swap_pager_freespace(tobject, tpindex, 1);
852		}
853	}
854}
855
856/*
857 *	vm_object_shadow:
858 *
859 *	Create a new object which is backed by the
860 *	specified existing object range.  The source
861 *	object reference is deallocated.
862 *
863 *	The new object and offset into that object
864 *	are returned in the source parameters.
865 */
866
867void
868vm_object_shadow(object, offset, length)
869	vm_object_t *object;	/* IN/OUT */
870	vm_ooffset_t *offset;	/* IN/OUT */
871	vm_size_t length;
872{
873	vm_object_t source;
874	vm_object_t result;
875
876	source = *object;
877
878	/*
879	 * Don't create the new object if the old object isn't shared.
880	 */
881
882	if (source != NULL &&
883	    source->ref_count == 1 &&
884	    source->handle == NULL &&
885	    (source->type == OBJT_DEFAULT ||
886	     source->type == OBJT_SWAP))
887		return;
888
889	KASSERT((source->flags & OBJ_ONEMAPPING) == 0,
890		("vm_object_shadow: source object has OBJ_ONEMAPPING set.\n"));
891
892	/*
893	 * Allocate a new object with the given length
894	 */
895
896	if ((result = vm_object_allocate(OBJT_DEFAULT, length)) == NULL)
897		panic("vm_object_shadow: no object for shadowing");
898
899	/*
900	 * The new object shadows the source object, adding a reference to it.
901	 * Our caller changes his reference to point to the new object,
902	 * removing a reference to the source object.  Net result: no change
903	 * of reference count.
904	 *
905	 * Try to optimize the result object's page color when shadowing
906	 * in order to maintain page coloring consistancy in the combined
907	 * shadowed object.
908	 */
909	result->backing_object = source;
910	if (source) {
911		TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
912		source->shadow_count++;
913		source->generation++;
914		result->pg_color = (source->pg_color + OFF_TO_IDX(*offset)) & PQ_L2_MASK;
915	}
916
917	/*
918	 * Store the offset into the source object, and fix up the offset into
919	 * the new object.
920	 */
921
922	result->backing_object_offset = *offset;
923
924	/*
925	 * Return the new things
926	 */
927
928	*offset = 0;
929	*object = result;
930}
931
932#define	OBSC_TEST_ALL_SHADOWED	0x0001
933#define	OBSC_COLLAPSE_NOWAIT	0x0002
934#define	OBSC_COLLAPSE_WAIT	0x0004
935
936static __inline int
937vm_object_backing_scan(vm_object_t object, int op)
938{
939	int s;
940	int r = 1;
941	vm_page_t p;
942	vm_object_t backing_object;
943	vm_pindex_t backing_offset_index;
944
945	s = splvm();
946
947	backing_object = object->backing_object;
948	backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
949
950	/*
951	 * Initial conditions
952	 */
953
954	if (op & OBSC_TEST_ALL_SHADOWED) {
955		/*
956		 * We do not want to have to test for the existance of
957		 * swap pages in the backing object.  XXX but with the
958		 * new swapper this would be pretty easy to do.
959		 *
960		 * XXX what about anonymous MAP_SHARED memory that hasn't
961		 * been ZFOD faulted yet?  If we do not test for this, the
962		 * shadow test may succeed! XXX
963		 */
964		if (backing_object->type != OBJT_DEFAULT) {
965			splx(s);
966			return(0);
967		}
968	}
969	if (op & OBSC_COLLAPSE_WAIT) {
970		vm_object_set_flag(backing_object, OBJ_DEAD);
971	}
972
973	/*
974	 * Our scan
975	 */
976
977	p = TAILQ_FIRST(&backing_object->memq);
978	while (p) {
979		vm_page_t next = TAILQ_NEXT(p, listq);
980		vm_pindex_t new_pindex = p->pindex - backing_offset_index;
981
982		if (op & OBSC_TEST_ALL_SHADOWED) {
983			vm_page_t pp;
984
985			/*
986			 * Ignore pages outside the parent object's range
987			 * and outside the parent object's mapping of the
988			 * backing object.
989			 *
990			 * note that we do not busy the backing object's
991			 * page.
992			 */
993
994			if (
995			    p->pindex < backing_offset_index ||
996			    new_pindex >= object->size
997			) {
998				p = next;
999				continue;
1000			}
1001
1002			/*
1003			 * See if the parent has the page or if the parent's
1004			 * object pager has the page.  If the parent has the
1005			 * page but the page is not valid, the parent's
1006			 * object pager must have the page.
1007			 *
1008			 * If this fails, the parent does not completely shadow
1009			 * the object and we might as well give up now.
1010			 */
1011
1012			pp = vm_page_lookup(object, new_pindex);
1013			if (
1014			    (pp == NULL || pp->valid == 0) &&
1015			    !vm_pager_has_page(object, new_pindex, NULL, NULL)
1016			) {
1017				r = 0;
1018				break;
1019			}
1020		}
1021
1022		/*
1023		 * Check for busy page
1024		 */
1025
1026		if (op & (OBSC_COLLAPSE_WAIT | OBSC_COLLAPSE_NOWAIT)) {
1027			vm_page_t pp;
1028
1029			if (op & OBSC_COLLAPSE_NOWAIT) {
1030				if (
1031				    (p->flags & PG_BUSY) ||
1032				    !p->valid ||
1033				    p->hold_count ||
1034				    p->wire_count ||
1035				    p->busy
1036				) {
1037					p = next;
1038					continue;
1039				}
1040			} else if (op & OBSC_COLLAPSE_WAIT) {
1041				if (vm_page_sleep_busy(p, TRUE, "vmocol")) {
1042					/*
1043					 * If we slept, anything could have
1044					 * happened.  Since the object is
1045					 * marked dead, the backing offset
1046					 * should not have changed so we
1047					 * just restart our scan.
1048					 */
1049					p = TAILQ_FIRST(&backing_object->memq);
1050					continue;
1051				}
1052			}
1053
1054			/*
1055			 * Busy the page
1056			 */
1057			vm_page_busy(p);
1058
1059			KASSERT(
1060			    p->object == backing_object,
1061			    ("vm_object_qcollapse(): object mismatch")
1062			);
1063
1064			/*
1065			 * Destroy any associated swap
1066			 */
1067			if (backing_object->type == OBJT_SWAP) {
1068				swap_pager_freespace(
1069				    backing_object,
1070				    p->pindex,
1071				    1
1072				);
1073			}
1074
1075			if (
1076			    p->pindex < backing_offset_index ||
1077			    new_pindex >= object->size
1078			) {
1079				/*
1080				 * Page is out of the parent object's range, we
1081				 * can simply destroy it.
1082				 */
1083				vm_page_protect(p, VM_PROT_NONE);
1084				vm_page_free(p);
1085				p = next;
1086				continue;
1087			}
1088
1089			pp = vm_page_lookup(object, new_pindex);
1090			if (
1091			    pp != NULL ||
1092			    vm_pager_has_page(object, new_pindex, NULL, NULL)
1093			) {
1094				/*
1095				 * page already exists in parent OR swap exists
1096				 * for this location in the parent.  Destroy
1097				 * the original page from the backing object.
1098				 *
1099				 * Leave the parent's page alone
1100				 */
1101				vm_page_protect(p, VM_PROT_NONE);
1102				vm_page_free(p);
1103				p = next;
1104				continue;
1105			}
1106
1107			/*
1108			 * Page does not exist in parent, rename the
1109			 * page from the backing object to the main object.
1110			 *
1111			 * If the page was mapped to a process, it can remain
1112			 * mapped through the rename.
1113			 */
1114			if ((p->queue - p->pc) == PQ_CACHE)
1115				vm_page_deactivate(p);
1116
1117			vm_page_rename(p, object, new_pindex);
1118			/* page automatically made dirty by rename */
1119		}
1120		p = next;
1121	}
1122	splx(s);
1123	return(r);
1124}
1125
1126
1127/*
1128 * this version of collapse allows the operation to occur earlier and
1129 * when paging_in_progress is true for an object...  This is not a complete
1130 * operation, but should plug 99.9% of the rest of the leaks.
1131 */
1132static void
1133vm_object_qcollapse(object)
1134	vm_object_t object;
1135{
1136	vm_object_t backing_object = object->backing_object;
1137
1138	if (backing_object->ref_count != 1)
1139		return;
1140
1141	backing_object->ref_count += 2;
1142
1143	vm_object_backing_scan(object, OBSC_COLLAPSE_NOWAIT);
1144
1145	backing_object->ref_count -= 2;
1146}
1147
1148/*
1149 *	vm_object_collapse:
1150 *
1151 *	Collapse an object with the object backing it.
1152 *	Pages in the backing object are moved into the
1153 *	parent, and the backing object is deallocated.
1154 */
1155void
1156vm_object_collapse(object)
1157	vm_object_t object;
1158{
1159	while (TRUE) {
1160		vm_object_t backing_object;
1161
1162		/*
1163		 * Verify that the conditions are right for collapse:
1164		 *
1165		 * The object exists and the backing object exists.
1166		 */
1167		if (object == NULL)
1168			break;
1169
1170		if ((backing_object = object->backing_object) == NULL)
1171			break;
1172
1173		/*
1174		 * we check the backing object first, because it is most likely
1175		 * not collapsable.
1176		 */
1177		if (backing_object->handle != NULL ||
1178		    (backing_object->type != OBJT_DEFAULT &&
1179		     backing_object->type != OBJT_SWAP) ||
1180		    (backing_object->flags & OBJ_DEAD) ||
1181		    object->handle != NULL ||
1182		    (object->type != OBJT_DEFAULT &&
1183		     object->type != OBJT_SWAP) ||
1184		    (object->flags & OBJ_DEAD)) {
1185			break;
1186		}
1187
1188		if (
1189		    object->paging_in_progress != 0 ||
1190		    backing_object->paging_in_progress != 0
1191		) {
1192			vm_object_qcollapse(object);
1193			break;
1194		}
1195
1196		/*
1197		 * We know that we can either collapse the backing object (if
1198		 * the parent is the only reference to it) or (perhaps) have
1199		 * the parent bypass the object if the parent happens to shadow
1200		 * all the resident pages in the entire backing object.
1201		 *
1202		 * This is ignoring pager-backed pages such as swap pages.
1203		 * vm_object_backing_scan fails the shadowing test in this
1204		 * case.
1205		 */
1206
1207		if (backing_object->ref_count == 1) {
1208			/*
1209			 * If there is exactly one reference to the backing
1210			 * object, we can collapse it into the parent.
1211			 */
1212
1213			vm_object_backing_scan(object, OBSC_COLLAPSE_WAIT);
1214
1215			/*
1216			 * Move the pager from backing_object to object.
1217			 */
1218
1219			if (backing_object->type == OBJT_SWAP) {
1220				vm_object_pip_add(backing_object, 1);
1221
1222				/*
1223				 * scrap the paging_offset junk and do a
1224				 * discrete copy.  This also removes major
1225				 * assumptions about how the swap-pager
1226				 * works from where it doesn't belong.  The
1227				 * new swapper is able to optimize the
1228				 * destroy-source case.
1229				 */
1230
1231				vm_object_pip_add(object, 1);
1232				swap_pager_copy(
1233				    backing_object,
1234				    object,
1235				    OFF_TO_IDX(object->backing_object_offset), TRUE);
1236				vm_object_pip_wakeup(object);
1237
1238				vm_object_pip_wakeup(backing_object);
1239			}
1240			/*
1241			 * Object now shadows whatever backing_object did.
1242			 * Note that the reference to
1243			 * backing_object->backing_object moves from within
1244			 * backing_object to within object.
1245			 */
1246
1247			TAILQ_REMOVE(
1248			    &object->backing_object->shadow_head,
1249			    object,
1250			    shadow_list
1251			);
1252			object->backing_object->shadow_count--;
1253			object->backing_object->generation++;
1254			if (backing_object->backing_object) {
1255				TAILQ_REMOVE(
1256				    &backing_object->backing_object->shadow_head,
1257				    backing_object,
1258				    shadow_list
1259				);
1260				backing_object->backing_object->shadow_count--;
1261				backing_object->backing_object->generation++;
1262			}
1263			object->backing_object = backing_object->backing_object;
1264			if (object->backing_object) {
1265				TAILQ_INSERT_TAIL(
1266				    &object->backing_object->shadow_head,
1267				    object,
1268				    shadow_list
1269				);
1270				object->backing_object->shadow_count++;
1271				object->backing_object->generation++;
1272			}
1273
1274			object->backing_object_offset +=
1275			    backing_object->backing_object_offset;
1276
1277			/*
1278			 * Discard backing_object.
1279			 *
1280			 * Since the backing object has no pages, no pager left,
1281			 * and no object references within it, all that is
1282			 * necessary is to dispose of it.
1283			 */
1284
1285			TAILQ_REMOVE(
1286			    &vm_object_list,
1287			    backing_object,
1288			    object_list
1289			);
1290			vm_object_count--;
1291
1292			zfree(obj_zone, backing_object);
1293
1294			object_collapses++;
1295		} else {
1296			vm_object_t new_backing_object;
1297
1298			/*
1299			 * If we do not entirely shadow the backing object,
1300			 * there is nothing we can do so we give up.
1301			 */
1302
1303			if (vm_object_backing_scan(object, OBSC_TEST_ALL_SHADOWED) == 0) {
1304				break;
1305			}
1306
1307			/*
1308			 * Make the parent shadow the next object in the
1309			 * chain.  Deallocating backing_object will not remove
1310			 * it, since its reference count is at least 2.
1311			 */
1312
1313			TAILQ_REMOVE(
1314			    &backing_object->shadow_head,
1315			    object,
1316			    shadow_list
1317			);
1318			backing_object->shadow_count--;
1319			backing_object->generation++;
1320
1321			new_backing_object = backing_object->backing_object;
1322			if ((object->backing_object = new_backing_object) != NULL) {
1323				vm_object_reference(new_backing_object);
1324				TAILQ_INSERT_TAIL(
1325				    &new_backing_object->shadow_head,
1326				    object,
1327				    shadow_list
1328				);
1329				new_backing_object->shadow_count++;
1330				new_backing_object->generation++;
1331				object->backing_object_offset +=
1332					backing_object->backing_object_offset;
1333			}
1334
1335			/*
1336			 * Drop the reference count on backing_object. Since
1337			 * its ref_count was at least 2, it will not vanish;
1338			 * so we don't need to call vm_object_deallocate, but
1339			 * we do anyway.
1340			 */
1341			vm_object_deallocate(backing_object);
1342			object_bypasses++;
1343		}
1344
1345		/*
1346		 * Try again with this object's new backing object.
1347		 */
1348	}
1349}
1350
1351/*
1352 *	vm_object_page_remove: [internal]
1353 *
1354 *	Removes all physical pages in the specified
1355 *	object range from the object's list of pages.
1356 *
1357 *	The object must be locked.
1358 */
1359void
1360vm_object_page_remove(object, start, end, clean_only)
1361	vm_object_t object;
1362	vm_pindex_t start;
1363	vm_pindex_t end;
1364	boolean_t clean_only;
1365{
1366	vm_page_t p, next;
1367	unsigned int size;
1368	int all;
1369
1370	if (object == NULL ||
1371	    object->resident_page_count == 0)
1372		return;
1373
1374	all = ((end == 0) && (start == 0));
1375
1376	vm_object_pip_add(object, 1);
1377again:
1378	size = end - start;
1379	if (all || size > object->resident_page_count / 4) {
1380		for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1381			next = TAILQ_NEXT(p, listq);
1382			if (all || ((start <= p->pindex) && (p->pindex < end))) {
1383				if (p->wire_count != 0) {
1384					vm_page_protect(p, VM_PROT_NONE);
1385					if (!clean_only)
1386						p->valid = 0;
1387					continue;
1388				}
1389
1390				/*
1391				 * The busy flags are only cleared at
1392				 * interrupt -- minimize the spl transitions
1393				 */
1394
1395 				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1396 					goto again;
1397
1398				if (clean_only && p->valid) {
1399					vm_page_test_dirty(p);
1400					if (p->valid & p->dirty)
1401						continue;
1402				}
1403
1404				vm_page_busy(p);
1405				vm_page_protect(p, VM_PROT_NONE);
1406				vm_page_free(p);
1407			}
1408		}
1409	} else {
1410		while (size > 0) {
1411			if ((p = vm_page_lookup(object, start)) != 0) {
1412
1413				if (p->wire_count != 0) {
1414					vm_page_protect(p, VM_PROT_NONE);
1415					if (!clean_only)
1416						p->valid = 0;
1417					start += 1;
1418					size -= 1;
1419					continue;
1420				}
1421
1422				/*
1423				 * The busy flags are only cleared at
1424				 * interrupt -- minimize the spl transitions
1425				 */
1426 				if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1427					goto again;
1428
1429				if (clean_only && p->valid) {
1430					vm_page_test_dirty(p);
1431					if (p->valid & p->dirty) {
1432						start += 1;
1433						size -= 1;
1434						continue;
1435					}
1436				}
1437
1438				vm_page_busy(p);
1439				vm_page_protect(p, VM_PROT_NONE);
1440				vm_page_free(p);
1441			}
1442			start += 1;
1443			size -= 1;
1444		}
1445	}
1446	vm_object_pip_wakeup(object);
1447}
1448
1449/*
1450 *	Routine:	vm_object_coalesce
1451 *	Function:	Coalesces two objects backing up adjoining
1452 *			regions of memory into a single object.
1453 *
1454 *	returns TRUE if objects were combined.
1455 *
1456 *	NOTE:	Only works at the moment if the second object is NULL -
1457 *		if it's not, which object do we lock first?
1458 *
1459 *	Parameters:
1460 *		prev_object	First object to coalesce
1461 *		prev_offset	Offset into prev_object
1462 *		next_object	Second object into coalesce
1463 *		next_offset	Offset into next_object
1464 *
1465 *		prev_size	Size of reference to prev_object
1466 *		next_size	Size of reference to next_object
1467 *
1468 *	Conditions:
1469 *	The object must *not* be locked.
1470 */
1471boolean_t
1472vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1473	vm_object_t prev_object;
1474	vm_pindex_t prev_pindex;
1475	vm_size_t prev_size, next_size;
1476{
1477	vm_pindex_t next_pindex;
1478
1479	if (prev_object == NULL) {
1480		return (TRUE);
1481	}
1482
1483	if (prev_object->type != OBJT_DEFAULT &&
1484	    prev_object->type != OBJT_SWAP) {
1485		return (FALSE);
1486	}
1487
1488	/*
1489	 * Try to collapse the object first
1490	 */
1491	vm_object_collapse(prev_object);
1492
1493	/*
1494	 * Can't coalesce if: . more than one reference . paged out . shadows
1495	 * another object . has a copy elsewhere (any of which mean that the
1496	 * pages not mapped to prev_entry may be in use anyway)
1497	 */
1498
1499	if (prev_object->backing_object != NULL) {
1500		return (FALSE);
1501	}
1502
1503	prev_size >>= PAGE_SHIFT;
1504	next_size >>= PAGE_SHIFT;
1505	next_pindex = prev_pindex + prev_size;
1506
1507	if ((prev_object->ref_count > 1) &&
1508	    (prev_object->size != next_pindex)) {
1509		return (FALSE);
1510	}
1511
1512	/*
1513	 * Remove any pages that may still be in the object from a previous
1514	 * deallocation.
1515	 */
1516	if (next_pindex < prev_object->size) {
1517		vm_object_page_remove(prev_object,
1518				      next_pindex,
1519				      next_pindex + next_size, FALSE);
1520		if (prev_object->type == OBJT_SWAP)
1521			swap_pager_freespace(prev_object,
1522					     next_pindex, next_size);
1523	}
1524
1525	/*
1526	 * Extend the object if necessary.
1527	 */
1528	if (next_pindex + next_size > prev_object->size)
1529		prev_object->size = next_pindex + next_size;
1530
1531	return (TRUE);
1532}
1533
1534#include "opt_ddb.h"
1535#ifdef DDB
1536#include <sys/kernel.h>
1537
1538#include <sys/cons.h>
1539
1540#include <ddb/ddb.h>
1541
1542static int	_vm_object_in_map __P((vm_map_t map, vm_object_t object,
1543				       vm_map_entry_t entry));
1544static int	vm_object_in_map __P((vm_object_t object));
1545
1546static int
1547_vm_object_in_map(map, object, entry)
1548	vm_map_t map;
1549	vm_object_t object;
1550	vm_map_entry_t entry;
1551{
1552	vm_map_t tmpm;
1553	vm_map_entry_t tmpe;
1554	vm_object_t obj;
1555	int entcount;
1556
1557	if (map == 0)
1558		return 0;
1559
1560	if (entry == 0) {
1561		tmpe = map->header.next;
1562		entcount = map->nentries;
1563		while (entcount-- && (tmpe != &map->header)) {
1564			if( _vm_object_in_map(map, object, tmpe)) {
1565				return 1;
1566			}
1567			tmpe = tmpe->next;
1568		}
1569	} else if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
1570		tmpm = entry->object.sub_map;
1571		tmpe = tmpm->header.next;
1572		entcount = tmpm->nentries;
1573		while (entcount-- && tmpe != &tmpm->header) {
1574			if( _vm_object_in_map(tmpm, object, tmpe)) {
1575				return 1;
1576			}
1577			tmpe = tmpe->next;
1578		}
1579	} else if ((obj = entry->object.vm_object) != NULL) {
1580		for(; obj; obj=obj->backing_object)
1581			if( obj == object) {
1582				return 1;
1583			}
1584	}
1585	return 0;
1586}
1587
1588static int
1589vm_object_in_map( object)
1590	vm_object_t object;
1591{
1592	struct proc *p;
1593	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1594		if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1595			continue;
1596		if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1597			return 1;
1598	}
1599	if( _vm_object_in_map( kernel_map, object, 0))
1600		return 1;
1601	if( _vm_object_in_map( kmem_map, object, 0))
1602		return 1;
1603	if( _vm_object_in_map( pager_map, object, 0))
1604		return 1;
1605	if( _vm_object_in_map( buffer_map, object, 0))
1606		return 1;
1607	if( _vm_object_in_map( phys_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