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