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