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