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