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