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