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