]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/vm_object.c
Fix warnings in preparation for adding -Wall -Wcast-qual to the
[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.141 1999/01/24 01:01:38 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         result->backing_object = source;
941         if (source) {
942                 TAILQ_INSERT_TAIL(&source->shadow_head, result, shadow_list);
943                 vm_object_clear_flag(source, OBJ_ONEMAPPING);
944                 source->shadow_count++;
945                 source->generation++;
946         }
947
948         /*
949          * Store the offset into the source object, and fix up the offset into
950          * the new object.
951          */
952
953         result->backing_object_offset = *offset;
954
955         /*
956          * Return the new things
957          */
958
959         *offset = 0;
960         *object = result;
961 }
962
963
964 /*
965  * this version of collapse allows the operation to occur earlier and
966  * when paging_in_progress is true for an object...  This is not a complete
967  * operation, but should plug 99.9% of the rest of the leaks.
968  */
969 static void
970 vm_object_qcollapse(object)
971         register vm_object_t object;
972 {
973         register vm_object_t backing_object;
974         register vm_pindex_t backing_offset_index;
975         vm_pindex_t new_pindex;
976         register vm_page_t p, pp;
977         register vm_size_t size;
978
979         backing_object = object->backing_object;
980         if (backing_object->ref_count != 1)
981                 return;
982
983         backing_object->ref_count += 2;
984
985         backing_offset_index = OFF_TO_IDX(object->backing_object_offset);
986         size = object->size;
987
988         p = TAILQ_FIRST(&backing_object->memq);
989         while (p) {
990                 vm_page_t next;
991
992                 /*
993                  * setup for loop.
994                  * loop if the page isn't trivial.
995                  */
996
997                 next = TAILQ_NEXT(p, listq);
998                 if ((p->flags & (PG_BUSY | PG_FICTITIOUS)) ||
999                     !p->valid || p->hold_count || p->wire_count || p->busy) {
1000                         p = next;
1001                         continue;
1002                 }
1003
1004                 /*
1005                  * busy the page and move it from the backing store to the
1006                  * parent object.
1007                  */
1008
1009                 vm_page_busy(p);
1010
1011                 KASSERT(p->object == object, ("vm_object_qcollapse(): object mismatch"));
1012
1013                 new_pindex = p->pindex - backing_offset_index;
1014                 if (p->pindex < backing_offset_index ||
1015                     new_pindex >= size) {
1016                         if (backing_object->type == OBJT_SWAP)
1017                                 swap_pager_freespace(backing_object,
1018                                     p->pindex,
1019                                     1);
1020                         vm_page_protect(p, VM_PROT_NONE);
1021                         vm_page_free(p);
1022                 } else {
1023                         pp = vm_page_lookup(object, new_pindex);
1024                         if (pp != NULL ||
1025                                 (object->type == OBJT_SWAP && vm_pager_has_page(object,
1026                                     new_pindex, NULL, NULL))) {
1027                                 if (backing_object->type == OBJT_SWAP)
1028                                         swap_pager_freespace(backing_object,
1029                                             p->pindex, 1);
1030                                 vm_page_protect(p, VM_PROT_NONE);
1031                                 vm_page_free(p);
1032                         } else {
1033                                 if (backing_object->type == OBJT_SWAP)
1034                                         swap_pager_freespace(backing_object,
1035                                             p->pindex, 1);
1036
1037                                 if ((p->queue - p->pc) == PQ_CACHE)
1038                                         vm_page_deactivate(p);
1039                                 else
1040                                         vm_page_protect(p, VM_PROT_NONE);
1041
1042                                 vm_page_rename(p, object, new_pindex);
1043                                 /* page automatically made dirty by rename */
1044                         }
1045                 }
1046                 p = next;
1047         }
1048         backing_object->ref_count -= 2;
1049 }
1050
1051 /*
1052  *      vm_object_collapse:
1053  *
1054  *      Collapse an object with the object backing it.
1055  *      Pages in the backing object are moved into the
1056  *      parent, and the backing object is deallocated.
1057  */
1058 void
1059 vm_object_collapse(object)
1060         vm_object_t object;
1061
1062 {
1063         vm_object_t backing_object;
1064         vm_ooffset_t backing_offset;
1065         vm_size_t size;
1066         vm_pindex_t new_pindex, backing_offset_index;
1067         vm_page_t p, pp;
1068
1069         while (TRUE) {
1070                 /*
1071                  * Verify that the conditions are right for collapse:
1072                  *
1073                  * The object exists and no pages in it are currently being paged
1074                  * out.
1075                  */
1076                 if (object == NULL)
1077                         return;
1078
1079                 /*
1080                  * Make sure there is a backing object.
1081                  */
1082                 if ((backing_object = object->backing_object) == NULL)
1083                         return;
1084
1085                 /*
1086                  * we check the backing object first, because it is most likely
1087                  * not collapsable.
1088                  */
1089                 if (backing_object->handle != NULL ||
1090                     (backing_object->type != OBJT_DEFAULT &&
1091                      backing_object->type != OBJT_SWAP) ||
1092                     (backing_object->flags & OBJ_DEAD) ||
1093                     object->handle != NULL ||
1094                     (object->type != OBJT_DEFAULT &&
1095                      object->type != OBJT_SWAP) ||
1096                     (object->flags & OBJ_DEAD)) {
1097                         return;
1098                 }
1099
1100                 if (object->paging_in_progress != 0 ||
1101                     backing_object->paging_in_progress != 0) {
1102                         vm_object_qcollapse(object);
1103                         return;
1104                 }
1105
1106                 /*
1107                  * We know that we can either collapse the backing object (if
1108                  * the parent is the only reference to it) or (perhaps) remove
1109                  * the parent's reference to it.
1110                  */
1111
1112                 backing_offset = object->backing_object_offset;
1113                 backing_offset_index = OFF_TO_IDX(backing_offset);
1114                 size = object->size;
1115
1116                 /*
1117                  * If there is exactly one reference to the backing object, we
1118                  * can collapse it into the parent.
1119                  */
1120
1121                 if (backing_object->ref_count == 1) {
1122
1123                         vm_object_set_flag(backing_object, OBJ_DEAD);
1124                         /*
1125                          * We can collapse the backing object.
1126                          *
1127                          * Move all in-memory pages from backing_object to the
1128                          * parent.  Pages that have been paged out will be
1129                          * overwritten by any of the parent's pages that
1130                          * shadow them.
1131                          */
1132
1133                         while ((p = TAILQ_FIRST(&backing_object->memq)) != 0) {
1134                                 if (vm_page_sleep_busy(p, TRUE, "vmocol"))
1135                                         continue;
1136                                 vm_page_busy(p);
1137                                 new_pindex = p->pindex - backing_offset_index;
1138
1139                                 /*
1140                                  * If the parent has a page here, or if this
1141                                  * page falls outside the parent, dispose of
1142                                  * it.
1143                                  *
1144                                  * Otherwise, move it as planned.
1145                                  */
1146
1147                                 if (p->pindex < backing_offset_index ||
1148                                     new_pindex >= size) {
1149                                         vm_page_protect(p, VM_PROT_NONE);
1150                                         vm_page_free(p);
1151                                 } else {
1152                                         pp = vm_page_lookup(object, new_pindex);
1153                                         if (pp != NULL || (object->type == OBJT_SWAP && vm_pager_has_page(object,
1154                                             new_pindex, NULL, NULL))) {
1155                                                 vm_page_protect(p, VM_PROT_NONE);
1156                                                 vm_page_free(p);
1157                                         } else {
1158                                                 if ((p->queue - p->pc) == PQ_CACHE)
1159                                                         vm_page_deactivate(p);
1160                                                 else
1161                                                         vm_page_protect(p, VM_PROT_NONE);
1162                                                 vm_page_rename(p, object, new_pindex);
1163                                                 /* page automatically made dirty by rename */
1164                                         }
1165                                 }
1166                         }
1167
1168                         /*
1169                          * Move the pager from backing_object to object.
1170                          */
1171
1172                         if (backing_object->type == OBJT_SWAP) {
1173                                 vm_object_pip_add(backing_object, 1);
1174
1175                                 /*
1176                                  * scrap the paging_offset junk and do a 
1177                                  * discrete copy.  This also removes major 
1178                                  * assumptions about how the swap-pager 
1179                                  * works from where it doesn't belong.  The
1180                                  * new swapper is able to optimize the
1181                                  * destroy-source case.
1182                                  */
1183
1184                                 vm_object_pip_add(object, 1);
1185                                 swap_pager_copy(
1186                                     backing_object,
1187                                     object,
1188                                     OFF_TO_IDX(object->backing_object_offset), TRUE);
1189                                 vm_object_pip_wakeup(object);
1190
1191                                 vm_object_pip_wakeup(backing_object);
1192                         }
1193                         /*
1194                          * Object now shadows whatever backing_object did.
1195                          * Note that the reference to backing_object->backing_object
1196                          * moves from within backing_object to within object.
1197                          */
1198
1199                         TAILQ_REMOVE(&object->backing_object->shadow_head, object,
1200                             shadow_list);
1201                         object->backing_object->shadow_count--;
1202                         object->backing_object->generation++;
1203                         if (backing_object->backing_object) {
1204                                 TAILQ_REMOVE(&backing_object->backing_object->shadow_head,
1205                                     backing_object, shadow_list);
1206                                 backing_object->backing_object->shadow_count--;
1207                                 backing_object->backing_object->generation++;
1208                         }
1209                         object->backing_object = backing_object->backing_object;
1210                         if (object->backing_object) {
1211                                 TAILQ_INSERT_TAIL(&object->backing_object->shadow_head,
1212                                     object, shadow_list);
1213                                 object->backing_object->shadow_count++;
1214                                 object->backing_object->generation++;
1215                         }
1216
1217                         object->backing_object_offset += backing_object->backing_object_offset;
1218                         /*
1219                          * Discard backing_object.
1220                          *
1221                          * Since the backing object has no pages, no pager left,
1222                          * and no object references within it, all that is
1223                          * necessary is to dispose of it.
1224                          */
1225
1226                         TAILQ_REMOVE(&vm_object_list, backing_object,
1227                             object_list);
1228                         vm_object_count--;
1229
1230                         zfree(obj_zone, backing_object);
1231
1232                         object_collapses++;
1233                 } else {
1234                         vm_object_t new_backing_object;
1235                         /*
1236                          * If all of the pages in the backing object are
1237                          * shadowed by the parent object, the parent object no
1238                          * longer has to shadow the backing object; it can
1239                          * shadow the next one in the chain.
1240                          *
1241                          * The backing object must not be paged out - we'd have
1242                          * to check all of the paged-out pages, as well.
1243                          */
1244
1245                         if (backing_object->type != OBJT_DEFAULT) {
1246                                 return;
1247                         }
1248                         /*
1249                          * Should have a check for a 'small' number of pages
1250                          * here.
1251                          */
1252
1253                         for (p = TAILQ_FIRST(&backing_object->memq); p;
1254                                         p = TAILQ_NEXT(p, listq)) {
1255
1256                                 new_pindex = p->pindex - backing_offset_index;
1257                                 vm_page_busy(p);
1258
1259                                 /*
1260                                  * If the parent has a page here, or if this
1261                                  * page falls outside the parent, keep going.
1262                                  *
1263                                  * Otherwise, the backing_object must be left in
1264                                  * the chain.
1265                                  */
1266
1267                                 if (p->pindex >= backing_offset_index &&
1268                                         new_pindex <= size) {
1269
1270                                         pp = vm_page_lookup(object, new_pindex);
1271
1272                                         if ((pp == NULL) || (pp->flags & PG_BUSY) || pp->busy) {
1273                                                 vm_page_wakeup(p);
1274                                                 return;
1275                                         }
1276
1277                                         vm_page_busy(pp);
1278                                         if ((pp->valid == 0) &&
1279                                             !vm_pager_has_page(object, new_pindex, NULL, NULL)) {
1280                                                 /*
1281                                                  * Page still needed. Can't go any
1282                                                  * further.
1283                                                  */
1284                                                 vm_page_wakeup(pp);
1285                                                 vm_page_wakeup(p);
1286                                                 return;
1287                                         }
1288                                         vm_page_wakeup(pp);
1289                                 }
1290                                 vm_page_wakeup(p);
1291                         }
1292
1293                         /*
1294                          * Make the parent shadow the next object in the
1295                          * chain.  Deallocating backing_object will not remove
1296                          * it, since its reference count is at least 2.
1297                          */
1298
1299                         TAILQ_REMOVE(&backing_object->shadow_head,
1300                             object, shadow_list);
1301                         backing_object->shadow_count--;
1302                         backing_object->generation++;
1303
1304                         new_backing_object = backing_object->backing_object;
1305                         if ((object->backing_object = new_backing_object) != NULL) {
1306                                 vm_object_reference(new_backing_object);
1307                                 TAILQ_INSERT_TAIL(&new_backing_object->shadow_head,
1308                                     object, shadow_list);
1309                                 new_backing_object->shadow_count++;
1310                                 new_backing_object->generation++;
1311                                 object->backing_object_offset +=
1312                                         backing_object->backing_object_offset;
1313                         }
1314
1315                         /*
1316                          * Drop the reference count on backing_object. Since
1317                          * its ref_count was at least 2, it will not vanish;
1318                          * so we don't need to call vm_object_deallocate, but
1319                          * we do anyway.
1320                          */
1321                         vm_object_deallocate(backing_object);
1322                         object_bypasses++;
1323                 }
1324
1325                 /*
1326                  * Try again with this object's new backing object.
1327                  */
1328         }
1329 }
1330
1331 /*
1332  *      vm_object_page_remove: [internal]
1333  *
1334  *      Removes all physical pages in the specified
1335  *      object range from the object's list of pages.
1336  *
1337  *      The object must be locked.
1338  */
1339 void
1340 vm_object_page_remove(object, start, end, clean_only)
1341         register vm_object_t object;
1342         register vm_pindex_t start;
1343         register vm_pindex_t end;
1344         boolean_t clean_only;
1345 {
1346         register vm_page_t p, next;
1347         unsigned int size;
1348         int all;
1349
1350         if (object == NULL)
1351                 return;
1352
1353         all = ((end == 0) && (start == 0));
1354
1355         vm_object_pip_add(object, 1);
1356 again:
1357         size = end - start;
1358         if (all || size > 4 || size >= object->size / 4) {
1359                 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = next) {
1360                         next = TAILQ_NEXT(p, listq);
1361                         if (all || ((start <= p->pindex) && (p->pindex < end))) {
1362                                 if (p->wire_count != 0) {
1363                                         vm_page_protect(p, VM_PROT_NONE);
1364                                         if (!clean_only)
1365                                                 p->valid = 0;
1366                                         continue;
1367                                 }
1368
1369                                 /*
1370                                  * The busy flags are only cleared at
1371                                  * interrupt -- minimize the spl transitions
1372                                  */
1373
1374                                 if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1375                                         goto again;
1376
1377                                 if (clean_only && p->valid) {
1378                                         vm_page_test_dirty(p);
1379                                         if (p->valid & p->dirty)
1380                                                 continue;
1381                                 }
1382
1383                                 vm_page_busy(p);
1384                                 vm_page_protect(p, VM_PROT_NONE);
1385                                 vm_page_free(p);
1386                         }
1387                 }
1388         } else {
1389                 while (size > 0) {
1390                         if ((p = vm_page_lookup(object, start)) != 0) {
1391
1392                                 if (p->wire_count != 0) {
1393                                         vm_page_protect(p, VM_PROT_NONE);
1394                                         if (!clean_only)
1395                                                 p->valid = 0;
1396                                         start += 1;
1397                                         size -= 1;
1398                                         continue;
1399                                 }
1400
1401                                 /*
1402                                  * The busy flags are only cleared at
1403                                  * interrupt -- minimize the spl transitions
1404                                  */
1405                                 if (vm_page_sleep_busy(p, TRUE, "vmopar"))
1406                                         goto again;
1407
1408                                 if (clean_only && p->valid) {
1409                                         vm_page_test_dirty(p);
1410                                         if (p->valid & p->dirty) {
1411                                                 start += 1;
1412                                                 size -= 1;
1413                                                 continue;
1414                                         }
1415                                 }
1416
1417                                 vm_page_busy(p);
1418                                 vm_page_protect(p, VM_PROT_NONE);
1419                                 vm_page_free(p);
1420                         }
1421                         start += 1;
1422                         size -= 1;
1423                 }
1424         }
1425         vm_object_pip_wakeup(object);
1426 }
1427
1428 /*
1429  *      Routine:        vm_object_coalesce
1430  *      Function:       Coalesces two objects backing up adjoining
1431  *                      regions of memory into a single object.
1432  *
1433  *      returns TRUE if objects were combined.
1434  *
1435  *      NOTE:   Only works at the moment if the second object is NULL -
1436  *              if it's not, which object do we lock first?
1437  *
1438  *      Parameters:
1439  *              prev_object     First object to coalesce
1440  *              prev_offset     Offset into prev_object
1441  *              next_object     Second object into coalesce
1442  *              next_offset     Offset into next_object
1443  *
1444  *              prev_size       Size of reference to prev_object
1445  *              next_size       Size of reference to next_object
1446  *
1447  *      Conditions:
1448  *      The object must *not* be locked.
1449  */
1450 boolean_t
1451 vm_object_coalesce(prev_object, prev_pindex, prev_size, next_size)
1452         register vm_object_t prev_object;
1453         vm_pindex_t prev_pindex;
1454         vm_size_t prev_size, next_size;
1455 {
1456         vm_size_t newsize;
1457
1458         if (prev_object == NULL) {
1459                 return (TRUE);
1460         }
1461
1462         if (prev_object->type != OBJT_DEFAULT) {
1463                 return (FALSE);
1464         }
1465
1466         /*
1467          * Try to collapse the object first
1468          */
1469         vm_object_collapse(prev_object);
1470
1471         /*
1472          * Can't coalesce if: . more than one reference . paged out . shadows
1473          * another object . has a copy elsewhere (any of which mean that the
1474          * pages not mapped to prev_entry may be in use anyway)
1475          */
1476
1477         if (prev_object->backing_object != NULL) {
1478                 return (FALSE);
1479         }
1480
1481         prev_size >>= PAGE_SHIFT;
1482         next_size >>= PAGE_SHIFT;
1483
1484         if ((prev_object->ref_count > 1) &&
1485             (prev_object->size != prev_pindex + prev_size)) {
1486                 return (FALSE);
1487         }
1488
1489         /*
1490          * Remove any pages that may still be in the object from a previous
1491          * deallocation.
1492          */
1493
1494         vm_object_page_remove(prev_object,
1495             prev_pindex + prev_size,
1496             prev_pindex + prev_size + next_size, FALSE);
1497
1498         /*
1499          * Extend the object if necessary.
1500          */
1501         newsize = prev_pindex + prev_size + next_size;
1502         if (newsize > prev_object->size)
1503                 prev_object->size = newsize;
1504
1505         return (TRUE);
1506 }
1507
1508 #include "opt_ddb.h"
1509 #ifdef DDB
1510 #include <sys/kernel.h>
1511
1512 #include <machine/cons.h>
1513
1514 #include <ddb/ddb.h>
1515
1516 static int      _vm_object_in_map __P((vm_map_t map, vm_object_t object,
1517                                        vm_map_entry_t entry));
1518 static int      vm_object_in_map __P((vm_object_t object));
1519
1520 static int
1521 _vm_object_in_map(map, object, entry)
1522         vm_map_t map;
1523         vm_object_t object;
1524         vm_map_entry_t entry;
1525 {
1526         vm_map_t tmpm;
1527         vm_map_entry_t tmpe;
1528         vm_object_t obj;
1529         int entcount;
1530
1531         if (map == 0)
1532                 return 0;
1533
1534         if (entry == 0) {
1535                 tmpe = map->header.next;
1536                 entcount = map->nentries;
1537                 while (entcount-- && (tmpe != &map->header)) {
1538                         if( _vm_object_in_map(map, object, tmpe)) {
1539                                 return 1;
1540                         }
1541                         tmpe = tmpe->next;
1542                 }
1543         } else if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) {
1544                 tmpm = entry->object.share_map;
1545                 tmpe = tmpm->header.next;
1546                 entcount = tmpm->nentries;
1547                 while (entcount-- && tmpe != &tmpm->header) {
1548                         if( _vm_object_in_map(tmpm, object, tmpe)) {
1549                                 return 1;
1550                         }
1551                         tmpe = tmpe->next;
1552                 }
1553         } else if ((obj = entry->object.vm_object) != NULL) {
1554                 for(; obj; obj=obj->backing_object)
1555                         if( obj == object) {
1556                                 return 1;
1557                         }
1558         }
1559         return 0;
1560 }
1561
1562 static int
1563 vm_object_in_map( object)
1564         vm_object_t object;
1565 {
1566         struct proc *p;
1567         for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
1568                 if( !p->p_vmspace /* || (p->p_flag & (P_SYSTEM|P_WEXIT)) */)
1569                         continue;
1570                 if( _vm_object_in_map(&p->p_vmspace->vm_map, object, 0))
1571                         return 1;
1572         }
1573         if( _vm_object_in_map( kernel_map, object, 0))
1574                 return 1;
1575         if( _vm_object_in_map( kmem_map, object, 0))
1576                 return 1;
1577         if( _vm_object_in_map( pager_map, object, 0))
1578                 return 1;
1579         if( _vm_object_in_map( buffer_map, object, 0))
1580                 return 1;
1581         if( _vm_object_in_map( io_map, object, 0))
1582                 return 1;
1583         if( _vm_object_in_map( phys_map, object, 0))
1584                 return 1;
1585         if( _vm_object_in_map( mb_map, object, 0))
1586                 return 1;
1587         if( _vm_object_in_map( u_map, object, 0))
1588                 return 1;
1589         return 0;
1590 }
1591
1592 DB_SHOW_COMMAND(vmochk, vm_object_check)
1593 {
1594         vm_object_t object;
1595
1596         /*
1597          * make sure that internal objs are in a map somewhere
1598          * and none have zero ref counts.
1599          */
1600         for (object = TAILQ_FIRST(&vm_object_list);
1601                         object != NULL;
1602                         object = TAILQ_NEXT(object, object_list)) {
1603                 if (object->handle == NULL &&
1604                     (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
1605                         if (object->ref_count == 0) {
1606                                 db_printf("vmochk: internal obj has zero ref count: %d\n",
1607                                         object->size);
1608                         }
1609                         if (!vm_object_in_map(object)) {
1610                                 db_printf(
1611                         "vmochk: internal obj is not in a map: "
1612                         "ref: %d, size: %lu: 0x%lx, backing_object: %p\n",
1613                                     object->ref_count, (u_long)object->size, 
1614                                     (u_long)object->size,
1615                                     (void *)object->backing_object);
1616                         }
1617                 }
1618         }
1619 }
1620
1621 /*
1622  *      vm_object_print:        [ debug ]
1623  */
1624 DB_SHOW_COMMAND(object, vm_object_print_static)
1625 {
1626         /* XXX convert args. */
1627         vm_object_t object = (vm_object_t)addr;
1628         boolean_t full = have_addr;
1629
1630         register vm_page_t p;
1631
1632         /* XXX count is an (unused) arg.  Avoid shadowing it. */
1633 #define count   was_count
1634
1635         register int count;
1636
1637         if (object == NULL)
1638                 return;
1639
1640         db_iprintf(
1641             "Object %p: type=%d, size=0x%lx, res=%d, ref=%d, flags=0x%x\n",
1642             object, (int)object->type, (u_long)object->size,
1643             object->resident_page_count, object->ref_count, object->flags);
1644         /*
1645          * XXX no %qd in kernel.  Truncate object->backing_object_offset.
1646          */
1647         db_iprintf(" sref=%d, backing_object(%d)=(%p)+0x%lx\n",
1648             object->shadow_count, 
1649             object->backing_object ? object->backing_object->ref_count : 0,
1650             object->backing_object, (long)object->backing_object_offset);
1651
1652         if (!full)
1653                 return;
1654
1655         db_indent += 2;
1656         count = 0;
1657         for (p = TAILQ_FIRST(&object->memq); p != NULL; p = TAILQ_NEXT(p, listq)) {
1658                 if (count == 0)
1659                         db_iprintf("memory:=");
1660                 else if (count == 6) {
1661                         db_printf("\n");
1662                         db_iprintf(" ...");
1663                         count = 0;
1664                 } else
1665                         db_printf(",");
1666                 count++;
1667
1668                 db_printf("(off=0x%lx,page=0x%lx)",
1669                     (u_long) p->pindex, (u_long) VM_PAGE_TO_PHYS(p));
1670         }
1671         if (count != 0)
1672                 db_printf("\n");
1673         db_indent -= 2;
1674 }
1675
1676 /* XXX. */
1677 #undef count
1678
1679 /* XXX need this non-static entry for calling from vm_map_print. */
1680 void
1681 vm_object_print(addr, have_addr, count, modif)
1682         /* db_expr_t */ long addr;
1683         boolean_t have_addr;
1684         /* db_expr_t */ long count;
1685         char *modif;
1686 {
1687         vm_object_print_static(addr, have_addr, count, modif);
1688 }
1689
1690 DB_SHOW_COMMAND(vmopag, vm_object_print_pages)
1691 {
1692         vm_object_t object;
1693         int nl = 0;
1694         int c;
1695         for (object = TAILQ_FIRST(&vm_object_list);
1696                         object != NULL;
1697                         object = TAILQ_NEXT(object, object_list)) {
1698                 vm_pindex_t idx, fidx;
1699                 vm_pindex_t osize;
1700                 vm_offset_t pa = -1, padiff;
1701                 int rcount;
1702                 vm_page_t m;
1703
1704                 db_printf("new object: %p\n", (void *)object);
1705                 if ( nl > 18) {
1706                         c = cngetc();
1707                         if (c != ' ')
1708                                 return;
1709                         nl = 0;
1710                 }
1711                 nl++;
1712                 rcount = 0;
1713                 fidx = 0;
1714                 osize = object->size;
1715                 if (osize > 128)
1716                         osize = 128;
1717                 for(idx=0;idx<osize;idx++) {
1718                         m = vm_page_lookup(object, idx);
1719                         if (m == NULL) {
1720                                 if (rcount) {
1721                                         db_printf(" index(%d)run(%d)pa(0x%x)\n",
1722                                                 fidx, rcount, pa);
1723                                         if ( nl > 18) {
1724                                                 c = cngetc();
1725                                                 if (c != ' ')
1726                                                         return;
1727                                                 nl = 0;
1728                                         }
1729                                         nl++;
1730                                         rcount = 0;
1731                                 }
1732                                 continue;
1733                         }
1734
1735                                 
1736                         if (rcount &&
1737                                 (VM_PAGE_TO_PHYS(m) == pa + rcount * PAGE_SIZE)) {
1738                                 ++rcount;
1739                                 continue;
1740                         }
1741                         if (rcount) {
1742                                 padiff = pa + rcount * PAGE_SIZE - VM_PAGE_TO_PHYS(m);
1743                                 padiff >>= PAGE_SHIFT;
1744                                 padiff &= PQ_L2_MASK;
1745                                 if (padiff == 0) {
1746                                         pa = VM_PAGE_TO_PHYS(m) - rcount * PAGE_SIZE;
1747                                         ++rcount;
1748                                         continue;
1749                                 }
1750                                 db_printf(" index(%d)run(%d)pa(0x%x)", fidx, rcount, pa);
1751                                 db_printf("pd(%d)\n", padiff);
1752                                 if ( nl > 18) {
1753                                         c = cngetc();
1754                                         if (c != ' ')
1755                                                 return;
1756                                         nl = 0;
1757                                 }
1758                                 nl++;
1759                         }
1760                         fidx = idx;
1761                         pa = VM_PAGE_TO_PHYS(m);
1762                         rcount = 1;
1763                 }
1764                 if (rcount) {
1765                         db_printf(" index(%d)run(%d)pa(0x%x)\n", fidx, rcount, pa);
1766                         if ( nl > 18) {
1767                                 c = cngetc();
1768                                 if (c != ' ')
1769                                         return;
1770                                 nl = 0;
1771                         }
1772                         nl++;
1773                 }
1774         }
1775 }
1776 #endif /* DDB */