1 /* 2 * Copyright (c) 1991 Regents of the University of California. 3 * 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_page.c 7.4 (Berkeley) 5/7/91 37 * $FreeBSD$ 38 */ 39 40 /* 41 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 42 * All rights reserved. 43 * 44 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 45 * 46 * Permission to use, copy, modify and distribute this software and 47 * its documentation is hereby granted, provided that both the copyright 48 * notice and this permission notice appear in all copies of the 49 * software, derivative works or modified versions, and any portions 50 * thereof, and that both notices appear in supporting documentation. 51 * 52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 55 * 56 * Carnegie Mellon requests users of this software to return to 57 * 58 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 59 * School of Computer Science 60 * Carnegie Mellon University 61 * Pittsburgh PA 15213-3890 62 * 63 * any improvements or extensions that they make and grant Carnegie the 64 * rights to redistribute these changes. 65 */ 66 67 /* 68 * GENERAL RULES ON VM_PAGE MANIPULATION 69 * 70 * - a pageq mutex is required when adding or removing a page from a 71 * page queue (vm_page_queue[]), regardless of other mutexes or the 72 * busy state of a page. 73 * 74 * - a hash chain mutex is required when associating or disassociating 75 * a page from the VM PAGE CACHE hash table (vm_page_buckets), 76 * regardless of other mutexes or the busy state of a page. 77 * 78 * - either a hash chain mutex OR a busied page is required in order 79 * to modify the page flags. A hash chain mutex must be obtained in 80 * order to busy a page. A page's flags cannot be modified by a 81 * hash chain mutex if the page is marked busy. 82 * 83 * - The object memq mutex is held when inserting or removing 84 * pages from an object (vm_page_insert() or vm_page_remove()). This 85 * is different from the object's main mutex. 86 * 87 * Generally speaking, you have to be aware of side effects when running 88 * vm_page ops. A vm_page_lookup() will return with the hash chain 89 * locked, whether it was able to lookup the page or not. vm_page_free(), 90 * vm_page_cache(), vm_page_activate(), and a number of other routines 91 * will release the hash chain mutex for you. Intermediate manipulation 92 * routines such as vm_page_flag_set() expect the hash chain to be held 93 * on entry and the hash chain will remain held on return. 94 * 95 * pageq scanning can only occur with the pageq in question locked. 96 * We have a known bottleneck with the active queue, but the cache 97 * and free queues are actually arrays already. 98 */ 99 100 /* 101 * Resident memory management module. 102 */ 103 104 #include <sys/param.h> 105 #include <sys/systm.h> 106 #include <sys/lock.h> 107 #include <sys/malloc.h> 108 #include <sys/mutex.h> 109 #include <sys/proc.h> 110 #include <sys/vmmeter.h> 111 #include <sys/vnode.h> 112 113 #include <vm/vm.h> 114 #include <vm/vm_param.h> 115 #include <vm/vm_kern.h> 116 #include <vm/vm_object.h> 117 #include <vm/vm_page.h> 118 #include <vm/vm_pageout.h> 119 #include <vm/vm_pager.h> 120 #include <vm/vm_extern.h> 121 122 /* 123 * Associated with page of user-allocatable memory is a 124 * page structure. 125 */ 126 127 static struct vm_page **vm_page_buckets; /* Array of buckets */ 128 static int vm_page_bucket_count; /* How big is array? */ 129 static int vm_page_hash_mask; /* Mask for hash function */ 130 static volatile int vm_page_bucket_generation; 131 static struct mtx vm_buckets_mtx[BUCKET_HASH_SIZE]; 132 133 vm_page_t vm_page_array = 0; 134 int vm_page_array_size = 0; 135 long first_page = 0; 136 int vm_page_zero_count = 0; 137 138 /* 139 * vm_set_page_size: 140 * 141 * Sets the page size, perhaps based upon the memory 142 * size. Must be called before any use of page-size 143 * dependent functions. 144 */ 145 void 146 vm_set_page_size(void) 147 { 148 if (cnt.v_page_size == 0) 149 cnt.v_page_size = PAGE_SIZE; 150 if (((cnt.v_page_size - 1) & cnt.v_page_size) != 0) 151 panic("vm_set_page_size: page size not a power of two"); 152 } 153 154 /* 155 * vm_page_startup: 156 * 157 * Initializes the resident memory module. 158 * 159 * Allocates memory for the page cells, and 160 * for the object/offset-to-page hash table headers. 161 * Each page cell is initialized and placed on the free list. 162 */ 163 164 vm_offset_t 165 vm_page_startup(vm_offset_t starta, vm_offset_t enda, vm_offset_t vaddr) 166 { 167 vm_offset_t mapped; 168 struct vm_page **bucket; 169 vm_size_t npages, page_range; 170 vm_offset_t new_end; 171 int i; 172 vm_offset_t pa; 173 int nblocks; 174 vm_offset_t last_pa; 175 176 /* the biggest memory array is the second group of pages */ 177 vm_offset_t end; 178 vm_offset_t biggestone, biggestsize; 179 180 vm_offset_t total; 181 182 total = 0; 183 biggestsize = 0; 184 biggestone = 0; 185 nblocks = 0; 186 vaddr = round_page(vaddr); 187 188 for (i = 0; phys_avail[i + 1]; i += 2) { 189 phys_avail[i] = round_page(phys_avail[i]); 190 phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 191 } 192 193 for (i = 0; phys_avail[i + 1]; i += 2) { 194 int size = phys_avail[i + 1] - phys_avail[i]; 195 196 if (size > biggestsize) { 197 biggestone = i; 198 biggestsize = size; 199 } 200 ++nblocks; 201 total += size; 202 } 203 204 end = phys_avail[biggestone+1]; 205 206 /* 207 * Initialize the queue headers for the free queue, the active queue 208 * and the inactive queue. 209 */ 210 211 vm_pageq_init(); 212 213 /* 214 * Allocate (and initialize) the hash table buckets. 215 * 216 * The number of buckets MUST BE a power of 2, and the actual value is 217 * the next power of 2 greater than the number of physical pages in 218 * the system. 219 * 220 * We make the hash table approximately 2x the number of pages to 221 * reduce the chain length. This is about the same size using the 222 * singly-linked list as the 1x hash table we were using before 223 * using TAILQ but the chain length will be smaller. 224 * 225 * Note: This computation can be tweaked if desired. 226 */ 227 if (vm_page_bucket_count == 0) { 228 vm_page_bucket_count = 1; 229 while (vm_page_bucket_count < atop(total)) 230 vm_page_bucket_count <<= 1; 231 } 232 vm_page_bucket_count <<= 1; 233 vm_page_hash_mask = vm_page_bucket_count - 1; 234 235 /* 236 * Validate these addresses. 237 */ 238 new_end = end - vm_page_bucket_count * sizeof(struct vm_page *); 239 new_end = trunc_page(new_end); 240 mapped = pmap_map(&vaddr, new_end, end, 241 VM_PROT_READ | VM_PROT_WRITE); 242 bzero((caddr_t) mapped, end - new_end); 243 244 vm_page_buckets = (struct vm_page **)mapped; 245 bucket = vm_page_buckets; 246 for (i = 0; i < vm_page_bucket_count; i++) { 247 *bucket = NULL; 248 bucket++; 249 } 250 for (i = 0; i < BUCKET_HASH_SIZE; ++i) 251 mtx_init(&vm_buckets_mtx[i], "vm buckets hash mutexes", MTX_DEF); 252 253 /* 254 * Compute the number of pages of memory that will be available for 255 * use (taking into account the overhead of a page structure per 256 * page). 257 */ 258 259 first_page = phys_avail[0] / PAGE_SIZE; 260 261 page_range = phys_avail[(nblocks - 1) * 2 + 1] / PAGE_SIZE - first_page; 262 npages = (total - (page_range * sizeof(struct vm_page)) - 263 (end - new_end)) / PAGE_SIZE; 264 265 end = new_end; 266 267 /* 268 * Initialize the mem entry structures now, and put them in the free 269 * queue. 270 */ 271 new_end = trunc_page(end - page_range * sizeof(struct vm_page)); 272 mapped = pmap_map(&vaddr, new_end, end, 273 VM_PROT_READ | VM_PROT_WRITE); 274 vm_page_array = (vm_page_t) mapped; 275 276 /* 277 * Clear all of the page structures 278 */ 279 bzero((caddr_t) vm_page_array, page_range * sizeof(struct vm_page)); 280 vm_page_array_size = page_range; 281 282 /* 283 * Construct the free queue(s) in descending order (by physical 284 * address) so that the first 16MB of physical memory is allocated 285 * last rather than first. On large-memory machines, this avoids 286 * the exhaustion of low physical memory before isa_dmainit has run. 287 */ 288 cnt.v_page_count = 0; 289 cnt.v_free_count = 0; 290 for (i = 0; phys_avail[i + 1] && npages > 0; i += 2) { 291 pa = phys_avail[i]; 292 if (i == biggestone) 293 last_pa = new_end; 294 else 295 last_pa = phys_avail[i + 1]; 296 while (pa < last_pa && npages-- > 0) { 297 vm_pageq_add_new_page(pa); 298 pa += PAGE_SIZE; 299 } 300 } 301 return (vaddr); 302 } 303 304 /* 305 * vm_page_hash: 306 * 307 * Distributes the object/offset key pair among hash buckets. 308 * 309 * NOTE: This macro depends on vm_page_bucket_count being a power of 2. 310 * This routine may not block. 311 * 312 * We try to randomize the hash based on the object to spread the pages 313 * out in the hash table without it costing us too much. 314 */ 315 static __inline int 316 vm_page_hash(vm_object_t object, vm_pindex_t pindex) 317 { 318 int i = ((uintptr_t)object + pindex) ^ object->hash_rand; 319 320 return(i & vm_page_hash_mask); 321 } 322 323 void 324 vm_page_flag_set(vm_page_t m, unsigned short bits) 325 { 326 GIANT_REQUIRED; 327 m->flags |= bits; 328 } 329 330 void 331 vm_page_flag_clear(vm_page_t m, unsigned short bits) 332 { 333 GIANT_REQUIRED; 334 m->flags &= ~bits; 335 } 336 337 void 338 vm_page_busy(vm_page_t m) 339 { 340 KASSERT((m->flags & PG_BUSY) == 0, 341 ("vm_page_busy: page already busy!!!")); 342 vm_page_flag_set(m, PG_BUSY); 343 } 344 345 /* 346 * vm_page_flash: 347 * 348 * wakeup anyone waiting for the page. 349 */ 350 351 void 352 vm_page_flash(vm_page_t m) 353 { 354 if (m->flags & PG_WANTED) { 355 vm_page_flag_clear(m, PG_WANTED); 356 wakeup(m); 357 } 358 } 359 360 /* 361 * vm_page_wakeup: 362 * 363 * clear the PG_BUSY flag and wakeup anyone waiting for the 364 * page. 365 * 366 */ 367 368 void 369 vm_page_wakeup(vm_page_t m) 370 { 371 KASSERT(m->flags & PG_BUSY, ("vm_page_wakeup: page not busy!!!")); 372 vm_page_flag_clear(m, PG_BUSY); 373 vm_page_flash(m); 374 } 375 376 /* 377 * 378 * 379 */ 380 381 void 382 vm_page_io_start(vm_page_t m) 383 { 384 GIANT_REQUIRED; 385 m->busy++; 386 } 387 388 void 389 vm_page_io_finish(vm_page_t m) 390 { 391 GIANT_REQUIRED; 392 m->busy--; 393 if (m->busy == 0) 394 vm_page_flash(m); 395 } 396 397 /* 398 * Keep page from being freed by the page daemon 399 * much of the same effect as wiring, except much lower 400 * overhead and should be used only for *very* temporary 401 * holding ("wiring"). 402 */ 403 void 404 vm_page_hold(vm_page_t mem) 405 { 406 GIANT_REQUIRED; 407 mem->hold_count++; 408 } 409 410 void 411 vm_page_unhold(vm_page_t mem) 412 { 413 GIANT_REQUIRED; 414 --mem->hold_count; 415 KASSERT(mem->hold_count >= 0, ("vm_page_unhold: hold count < 0!!!")); 416 } 417 418 /* 419 * vm_page_protect: 420 * 421 * Reduce the protection of a page. This routine never raises the 422 * protection and therefore can be safely called if the page is already 423 * at VM_PROT_NONE (it will be a NOP effectively ). 424 */ 425 426 void 427 vm_page_protect(vm_page_t mem, int prot) 428 { 429 if (prot == VM_PROT_NONE) { 430 if (mem->flags & (PG_WRITEABLE|PG_MAPPED)) { 431 pmap_page_protect(mem, VM_PROT_NONE); 432 vm_page_flag_clear(mem, PG_WRITEABLE|PG_MAPPED); 433 } 434 } else if ((prot == VM_PROT_READ) && (mem->flags & PG_WRITEABLE)) { 435 pmap_page_protect(mem, VM_PROT_READ); 436 vm_page_flag_clear(mem, PG_WRITEABLE); 437 } 438 } 439 /* 440 * vm_page_zero_fill: 441 * 442 * Zero-fill the specified page. 443 * Written as a standard pagein routine, to 444 * be used by the zero-fill object. 445 */ 446 boolean_t 447 vm_page_zero_fill(vm_page_t m) 448 { 449 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 450 return (TRUE); 451 } 452 453 /* 454 * vm_page_copy: 455 * 456 * Copy one page to another 457 */ 458 void 459 vm_page_copy(vm_page_t src_m, vm_page_t dest_m) 460 { 461 pmap_copy_page(VM_PAGE_TO_PHYS(src_m), VM_PAGE_TO_PHYS(dest_m)); 462 dest_m->valid = VM_PAGE_BITS_ALL; 463 } 464 465 /* 466 * vm_page_free: 467 * 468 * Free a page 469 * 470 * The clearing of PG_ZERO is a temporary safety until the code can be 471 * reviewed to determine that PG_ZERO is being properly cleared on 472 * write faults or maps. PG_ZERO was previously cleared in 473 * vm_page_alloc(). 474 */ 475 void 476 vm_page_free(vm_page_t m) 477 { 478 vm_page_flag_clear(m, PG_ZERO); 479 vm_page_free_toq(m); 480 vm_page_zero_idle_wakeup(); 481 } 482 483 /* 484 * vm_page_free_zero: 485 * 486 * Free a page to the zerod-pages queue 487 */ 488 void 489 vm_page_free_zero(vm_page_t m) 490 { 491 vm_page_flag_set(m, PG_ZERO); 492 vm_page_free_toq(m); 493 } 494 495 /* 496 * vm_page_sleep_busy: 497 * 498 * Wait until page is no longer PG_BUSY or (if also_m_busy is TRUE) 499 * m->busy is zero. Returns TRUE if it had to sleep ( including if 500 * it almost had to sleep and made temporary spl*() mods), FALSE 501 * otherwise. 502 * 503 * This routine assumes that interrupts can only remove the busy 504 * status from a page, not set the busy status or change it from 505 * PG_BUSY to m->busy or vise versa (which would create a timing 506 * window). 507 */ 508 509 int 510 vm_page_sleep_busy(vm_page_t m, int also_m_busy, const char *msg) 511 { 512 GIANT_REQUIRED; 513 if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) { 514 int s = splvm(); 515 if ((m->flags & PG_BUSY) || (also_m_busy && m->busy)) { 516 /* 517 * Page is busy. Wait and retry. 518 */ 519 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 520 tsleep(m, PVM, msg, 0); 521 } 522 splx(s); 523 return(TRUE); 524 /* not reached */ 525 } 526 return(FALSE); 527 } 528 /* 529 * vm_page_dirty: 530 * 531 * make page all dirty 532 */ 533 534 void 535 vm_page_dirty(vm_page_t m) 536 { 537 KASSERT(m->queue - m->pc != PQ_CACHE, 538 ("vm_page_dirty: page in cache!")); 539 m->dirty = VM_PAGE_BITS_ALL; 540 } 541 542 /* 543 * vm_page_undirty: 544 * 545 * Set page to not be dirty. Note: does not clear pmap modify bits 546 */ 547 548 void 549 vm_page_undirty(vm_page_t m) 550 { 551 m->dirty = 0; 552 } 553 554 /* 555 * vm_page_insert: [ internal use only ] 556 * 557 * Inserts the given mem entry into the object and object list. 558 * 559 * The pagetables are not updated but will presumably fault the page 560 * in if necessary, or if a kernel page the caller will at some point 561 * enter the page into the kernel's pmap. We are not allowed to block 562 * here so we *can't* do this anyway. 563 * 564 * The object and page must be locked, and must be splhigh. 565 * This routine may not block. 566 */ 567 568 void 569 vm_page_insert(vm_page_t m, vm_object_t object, vm_pindex_t pindex) 570 { 571 struct vm_page **bucket; 572 573 GIANT_REQUIRED; 574 575 if (m->object != NULL) 576 panic("vm_page_insert: already inserted"); 577 578 /* 579 * Record the object/offset pair in this page 580 */ 581 582 m->object = object; 583 m->pindex = pindex; 584 585 /* 586 * Insert it into the object_object/offset hash table 587 */ 588 589 bucket = &vm_page_buckets[vm_page_hash(object, pindex)]; 590 m->hnext = *bucket; 591 *bucket = m; 592 vm_page_bucket_generation++; 593 594 /* 595 * Now link into the object's list of backed pages. 596 */ 597 598 TAILQ_INSERT_TAIL(&object->memq, m, listq); 599 object->generation++; 600 601 /* 602 * show that the object has one more resident page. 603 */ 604 605 object->resident_page_count++; 606 607 /* 608 * Since we are inserting a new and possibly dirty page, 609 * update the object's OBJ_WRITEABLE and OBJ_MIGHTBEDIRTY flags. 610 */ 611 if (m->flags & PG_WRITEABLE) 612 vm_object_set_writeable_dirty(object); 613 } 614 615 /* 616 * vm_page_remove: 617 * NOTE: used by device pager as well -wfj 618 * 619 * Removes the given mem entry from the object/offset-page 620 * table and the object page list, but do not invalidate/terminate 621 * the backing store. 622 * 623 * The object and page must be locked, and at splhigh. 624 * The underlying pmap entry (if any) is NOT removed here. 625 * This routine may not block. 626 */ 627 628 void 629 vm_page_remove(vm_page_t m) 630 { 631 vm_object_t object; 632 633 GIANT_REQUIRED; 634 635 if (m->object == NULL) 636 return; 637 638 if ((m->flags & PG_BUSY) == 0) { 639 panic("vm_page_remove: page not busy"); 640 } 641 642 /* 643 * Basically destroy the page. 644 */ 645 646 vm_page_wakeup(m); 647 648 object = m->object; 649 650 /* 651 * Remove from the object_object/offset hash table. The object 652 * must be on the hash queue, we will panic if it isn't 653 * 654 * Note: we must NULL-out m->hnext to prevent loops in detached 655 * buffers with vm_page_lookup(). 656 */ 657 658 { 659 struct vm_page **bucket; 660 661 bucket = &vm_page_buckets[vm_page_hash(m->object, m->pindex)]; 662 while (*bucket != m) { 663 if (*bucket == NULL) 664 panic("vm_page_remove(): page not found in hash"); 665 bucket = &(*bucket)->hnext; 666 } 667 *bucket = m->hnext; 668 m->hnext = NULL; 669 vm_page_bucket_generation++; 670 } 671 672 /* 673 * Now remove from the object's list of backed pages. 674 */ 675 676 TAILQ_REMOVE(&object->memq, m, listq); 677 678 /* 679 * And show that the object has one fewer resident page. 680 */ 681 682 object->resident_page_count--; 683 object->generation++; 684 685 m->object = NULL; 686 } 687 688 /* 689 * vm_page_lookup: 690 * 691 * Returns the page associated with the object/offset 692 * pair specified; if none is found, NULL is returned. 693 * 694 * NOTE: the code below does not lock. It will operate properly if 695 * an interrupt makes a change, but the generation algorithm will not 696 * operate properly in an SMP environment where both cpu's are able to run 697 * kernel code simultaneously. 698 * 699 * The object must be locked. No side effects. 700 * This routine may not block. 701 * This is a critical path routine 702 */ 703 704 vm_page_t 705 vm_page_lookup(vm_object_t object, vm_pindex_t pindex) 706 { 707 vm_page_t m; 708 struct vm_page **bucket; 709 int generation; 710 711 /* 712 * Search the hash table for this object/offset pair 713 */ 714 715 retry: 716 generation = vm_page_bucket_generation; 717 bucket = &vm_page_buckets[vm_page_hash(object, pindex)]; 718 for (m = *bucket; m != NULL; m = m->hnext) { 719 if ((m->object == object) && (m->pindex == pindex)) { 720 if (vm_page_bucket_generation != generation) 721 goto retry; 722 return (m); 723 } 724 } 725 if (vm_page_bucket_generation != generation) 726 goto retry; 727 return (NULL); 728 } 729 730 /* 731 * vm_page_rename: 732 * 733 * Move the given memory entry from its 734 * current object to the specified target object/offset. 735 * 736 * The object must be locked. 737 * This routine may not block. 738 * 739 * Note: this routine will raise itself to splvm(), the caller need not. 740 * 741 * Note: swap associated with the page must be invalidated by the move. We 742 * have to do this for several reasons: (1) we aren't freeing the 743 * page, (2) we are dirtying the page, (3) the VM system is probably 744 * moving the page from object A to B, and will then later move 745 * the backing store from A to B and we can't have a conflict. 746 * 747 * Note: we *always* dirty the page. It is necessary both for the 748 * fact that we moved it, and because we may be invalidating 749 * swap. If the page is on the cache, we have to deactivate it 750 * or vm_page_dirty() will panic. Dirty pages are not allowed 751 * on the cache. 752 */ 753 754 void 755 vm_page_rename(vm_page_t m, vm_object_t new_object, vm_pindex_t new_pindex) 756 { 757 int s; 758 759 s = splvm(); 760 vm_page_remove(m); 761 vm_page_insert(m, new_object, new_pindex); 762 if (m->queue - m->pc == PQ_CACHE) 763 vm_page_deactivate(m); 764 vm_page_dirty(m); 765 splx(s); 766 } 767 768 /* 769 * vm_page_select_cache: 770 * 771 * Find a page on the cache queue with color optimization. As pages 772 * might be found, but not applicable, they are deactivated. This 773 * keeps us from using potentially busy cached pages. 774 * 775 * This routine must be called at splvm(). 776 * This routine may not block. 777 */ 778 static vm_page_t 779 vm_page_select_cache(vm_object_t object, vm_pindex_t pindex) 780 { 781 vm_page_t m; 782 783 GIANT_REQUIRED; 784 while (TRUE) { 785 m = vm_pageq_find( 786 PQ_CACHE, 787 (pindex + object->pg_color) & PQ_L2_MASK, 788 FALSE 789 ); 790 if (m && ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || 791 m->hold_count || m->wire_count)) { 792 vm_page_deactivate(m); 793 continue; 794 } 795 return m; 796 } 797 } 798 799 /* 800 * vm_page_select_free: 801 * 802 * Find a free or zero page, with specified preference. 803 * 804 * This routine must be called at splvm(). 805 * This routine may not block. 806 */ 807 808 static __inline vm_page_t 809 vm_page_select_free(vm_object_t object, vm_pindex_t pindex, boolean_t prefer_zero) 810 { 811 vm_page_t m; 812 813 m = vm_pageq_find( 814 PQ_FREE, 815 (pindex + object->pg_color) & PQ_L2_MASK, 816 prefer_zero 817 ); 818 return(m); 819 } 820 821 /* 822 * vm_page_alloc: 823 * 824 * Allocate and return a memory cell associated 825 * with this VM object/offset pair. 826 * 827 * page_req classes: 828 * VM_ALLOC_NORMAL normal process request 829 * VM_ALLOC_SYSTEM system *really* needs a page 830 * VM_ALLOC_INTERRUPT interrupt time request 831 * VM_ALLOC_ZERO zero page 832 * 833 * This routine may not block. 834 * 835 * Additional special handling is required when called from an 836 * interrupt (VM_ALLOC_INTERRUPT). We are not allowed to mess with 837 * the page cache in this case. 838 */ 839 840 vm_page_t 841 vm_page_alloc(vm_object_t object, vm_pindex_t pindex, int page_req) 842 { 843 vm_page_t m = NULL; 844 int s; 845 846 GIANT_REQUIRED; 847 848 KASSERT(!vm_page_lookup(object, pindex), 849 ("vm_page_alloc: page already allocated")); 850 851 /* 852 * The pager is allowed to eat deeper into the free page list. 853 */ 854 855 if ((curproc == pageproc) && (page_req != VM_ALLOC_INTERRUPT)) { 856 page_req = VM_ALLOC_SYSTEM; 857 }; 858 859 s = splvm(); 860 861 loop: 862 if (cnt.v_free_count > cnt.v_free_reserved) { 863 /* 864 * Allocate from the free queue if there are plenty of pages 865 * in it. 866 */ 867 if (page_req == VM_ALLOC_ZERO) 868 m = vm_page_select_free(object, pindex, TRUE); 869 else 870 m = vm_page_select_free(object, pindex, FALSE); 871 } else if ( 872 (page_req == VM_ALLOC_SYSTEM && 873 cnt.v_cache_count == 0 && 874 cnt.v_free_count > cnt.v_interrupt_free_min) || 875 (page_req == VM_ALLOC_INTERRUPT && cnt.v_free_count > 0) 876 ) { 877 /* 878 * Interrupt or system, dig deeper into the free list. 879 */ 880 m = vm_page_select_free(object, pindex, FALSE); 881 } else if (page_req != VM_ALLOC_INTERRUPT) { 882 /* 883 * Allocatable from cache (non-interrupt only). On success, 884 * we must free the page and try again, thus ensuring that 885 * cnt.v_*_free_min counters are replenished. 886 */ 887 m = vm_page_select_cache(object, pindex); 888 if (m == NULL) { 889 splx(s); 890 #if defined(DIAGNOSTIC) 891 if (cnt.v_cache_count > 0) 892 printf("vm_page_alloc(NORMAL): missing pages on cache queue: %d\n", cnt.v_cache_count); 893 #endif 894 vm_pageout_deficit++; 895 pagedaemon_wakeup(); 896 return (NULL); 897 } 898 KASSERT(m->dirty == 0, ("Found dirty cache page %p", m)); 899 vm_page_busy(m); 900 vm_page_protect(m, VM_PROT_NONE); 901 vm_page_free(m); 902 goto loop; 903 } else { 904 /* 905 * Not allocatable from cache from interrupt, give up. 906 */ 907 splx(s); 908 vm_pageout_deficit++; 909 pagedaemon_wakeup(); 910 return (NULL); 911 } 912 913 /* 914 * At this point we had better have found a good page. 915 */ 916 917 KASSERT( 918 m != NULL, 919 ("vm_page_alloc(): missing page on free queue\n") 920 ); 921 922 /* 923 * Remove from free queue 924 */ 925 926 vm_pageq_remove_nowakeup(m); 927 928 /* 929 * Initialize structure. Only the PG_ZERO flag is inherited. 930 */ 931 932 if (m->flags & PG_ZERO) { 933 vm_page_zero_count--; 934 m->flags = PG_ZERO | PG_BUSY; 935 } else { 936 m->flags = PG_BUSY; 937 } 938 m->wire_count = 0; 939 m->hold_count = 0; 940 m->act_count = 0; 941 m->busy = 0; 942 m->valid = 0; 943 KASSERT(m->dirty == 0, ("vm_page_alloc: free/cache page %p was dirty", m)); 944 945 /* 946 * vm_page_insert() is safe prior to the splx(). Note also that 947 * inserting a page here does not insert it into the pmap (which 948 * could cause us to block allocating memory). We cannot block 949 * anywhere. 950 */ 951 952 vm_page_insert(m, object, pindex); 953 954 /* 955 * Don't wakeup too often - wakeup the pageout daemon when 956 * we would be nearly out of memory. 957 */ 958 if (vm_paging_needed()) 959 pagedaemon_wakeup(); 960 961 splx(s); 962 963 return (m); 964 } 965 966 /* 967 * vm_wait: (also see VM_WAIT macro) 968 * 969 * Block until free pages are available for allocation 970 */ 971 972 void 973 vm_wait(void) 974 { 975 int s; 976 977 s = splvm(); 978 if (curproc == pageproc) { 979 vm_pageout_pages_needed = 1; 980 tsleep(&vm_pageout_pages_needed, PSWP, "VMWait", 0); 981 } else { 982 if (!vm_pages_needed) { 983 vm_pages_needed = 1; 984 wakeup(&vm_pages_needed); 985 } 986 tsleep(&cnt.v_free_count, PVM, "vmwait", 0); 987 } 988 splx(s); 989 } 990 991 /* 992 * vm_page_activate: 993 * 994 * Put the specified page on the active list (if appropriate). 995 * Ensure that act_count is at least ACT_INIT but do not otherwise 996 * mess with it. 997 * 998 * The page queues must be locked. 999 * This routine may not block. 1000 */ 1001 void 1002 vm_page_activate(vm_page_t m) 1003 { 1004 int s; 1005 1006 GIANT_REQUIRED; 1007 s = splvm(); 1008 1009 if (m->queue != PQ_ACTIVE) { 1010 if ((m->queue - m->pc) == PQ_CACHE) 1011 cnt.v_reactivated++; 1012 1013 vm_pageq_remove(m); 1014 1015 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1016 m->queue = PQ_ACTIVE; 1017 vm_page_queues[PQ_ACTIVE].lcnt++; 1018 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1019 if (m->act_count < ACT_INIT) 1020 m->act_count = ACT_INIT; 1021 cnt.v_active_count++; 1022 } 1023 } else { 1024 if (m->act_count < ACT_INIT) 1025 m->act_count = ACT_INIT; 1026 } 1027 1028 splx(s); 1029 } 1030 1031 /* 1032 * vm_page_free_wakeup: 1033 * 1034 * Helper routine for vm_page_free_toq() and vm_page_cache(). This 1035 * routine is called when a page has been added to the cache or free 1036 * queues. 1037 * 1038 * This routine may not block. 1039 * This routine must be called at splvm() 1040 */ 1041 static __inline void 1042 vm_page_free_wakeup(void) 1043 { 1044 /* 1045 * if pageout daemon needs pages, then tell it that there are 1046 * some free. 1047 */ 1048 if (vm_pageout_pages_needed && 1049 cnt.v_cache_count + cnt.v_free_count >= cnt.v_pageout_free_min) { 1050 wakeup(&vm_pageout_pages_needed); 1051 vm_pageout_pages_needed = 0; 1052 } 1053 /* 1054 * wakeup processes that are waiting on memory if we hit a 1055 * high water mark. And wakeup scheduler process if we have 1056 * lots of memory. this process will swapin processes. 1057 */ 1058 if (vm_pages_needed && !vm_page_count_min()) { 1059 vm_pages_needed = 0; 1060 wakeup(&cnt.v_free_count); 1061 } 1062 } 1063 1064 /* 1065 * vm_page_free_toq: 1066 * 1067 * Returns the given page to the PQ_FREE list, 1068 * disassociating it with any VM object. 1069 * 1070 * Object and page must be locked prior to entry. 1071 * This routine may not block. 1072 */ 1073 1074 void 1075 vm_page_free_toq(vm_page_t m) 1076 { 1077 int s; 1078 struct vpgqueues *pq; 1079 vm_object_t object = m->object; 1080 1081 GIANT_REQUIRED; 1082 s = splvm(); 1083 cnt.v_tfree++; 1084 1085 if (m->busy || ((m->queue - m->pc) == PQ_FREE) || 1086 (m->hold_count != 0)) { 1087 printf( 1088 "vm_page_free: pindex(%lu), busy(%d), PG_BUSY(%d), hold(%d)\n", 1089 (u_long)m->pindex, m->busy, (m->flags & PG_BUSY) ? 1 : 0, 1090 m->hold_count); 1091 if ((m->queue - m->pc) == PQ_FREE) 1092 panic("vm_page_free: freeing free page"); 1093 else 1094 panic("vm_page_free: freeing busy page"); 1095 } 1096 1097 /* 1098 * unqueue, then remove page. Note that we cannot destroy 1099 * the page here because we do not want to call the pager's 1100 * callback routine until after we've put the page on the 1101 * appropriate free queue. 1102 */ 1103 1104 vm_pageq_remove_nowakeup(m); 1105 vm_page_remove(m); 1106 1107 /* 1108 * If fictitious remove object association and 1109 * return, otherwise delay object association removal. 1110 */ 1111 1112 if ((m->flags & PG_FICTITIOUS) != 0) { 1113 splx(s); 1114 return; 1115 } 1116 1117 m->valid = 0; 1118 vm_page_undirty(m); 1119 1120 if (m->wire_count != 0) { 1121 if (m->wire_count > 1) { 1122 panic("vm_page_free: invalid wire count (%d), pindex: 0x%lx", 1123 m->wire_count, (long)m->pindex); 1124 } 1125 panic("vm_page_free: freeing wired page\n"); 1126 } 1127 1128 /* 1129 * If we've exhausted the object's resident pages we want to free 1130 * it up. 1131 */ 1132 1133 if (object && 1134 (object->type == OBJT_VNODE) && 1135 ((object->flags & OBJ_DEAD) == 0) 1136 ) { 1137 struct vnode *vp = (struct vnode *)object->handle; 1138 1139 if (vp && VSHOULDFREE(vp)) 1140 vfree(vp); 1141 } 1142 1143 /* 1144 * Clear the UNMANAGED flag when freeing an unmanaged page. 1145 */ 1146 1147 if (m->flags & PG_UNMANAGED) { 1148 m->flags &= ~PG_UNMANAGED; 1149 } else { 1150 #ifdef __alpha__ 1151 pmap_page_is_free(m); 1152 #endif 1153 } 1154 1155 m->queue = PQ_FREE + m->pc; 1156 pq = &vm_page_queues[m->queue]; 1157 pq->lcnt++; 1158 ++(*pq->cnt); 1159 1160 /* 1161 * Put zero'd pages on the end ( where we look for zero'd pages 1162 * first ) and non-zerod pages at the head. 1163 */ 1164 1165 if (m->flags & PG_ZERO) { 1166 TAILQ_INSERT_TAIL(&pq->pl, m, pageq); 1167 ++vm_page_zero_count; 1168 } else { 1169 TAILQ_INSERT_HEAD(&pq->pl, m, pageq); 1170 } 1171 1172 vm_page_free_wakeup(); 1173 1174 splx(s); 1175 } 1176 1177 /* 1178 * vm_page_unmanage: 1179 * 1180 * Prevent PV management from being done on the page. The page is 1181 * removed from the paging queues as if it were wired, and as a 1182 * consequence of no longer being managed the pageout daemon will not 1183 * touch it (since there is no way to locate the pte mappings for the 1184 * page). madvise() calls that mess with the pmap will also no longer 1185 * operate on the page. 1186 * 1187 * Beyond that the page is still reasonably 'normal'. Freeing the page 1188 * will clear the flag. 1189 * 1190 * This routine is used by OBJT_PHYS objects - objects using unswappable 1191 * physical memory as backing store rather then swap-backed memory and 1192 * will eventually be extended to support 4MB unmanaged physical 1193 * mappings. 1194 */ 1195 1196 void 1197 vm_page_unmanage(vm_page_t m) 1198 { 1199 int s; 1200 1201 s = splvm(); 1202 if ((m->flags & PG_UNMANAGED) == 0) { 1203 if (m->wire_count == 0) 1204 vm_pageq_remove(m); 1205 } 1206 vm_page_flag_set(m, PG_UNMANAGED); 1207 splx(s); 1208 } 1209 1210 /* 1211 * vm_page_wire: 1212 * 1213 * Mark this page as wired down by yet 1214 * another map, removing it from paging queues 1215 * as necessary. 1216 * 1217 * The page queues must be locked. 1218 * This routine may not block. 1219 */ 1220 void 1221 vm_page_wire(vm_page_t m) 1222 { 1223 int s; 1224 1225 /* 1226 * Only bump the wire statistics if the page is not already wired, 1227 * and only unqueue the page if it is on some queue (if it is unmanaged 1228 * it is already off the queues). 1229 */ 1230 s = splvm(); 1231 if (m->wire_count == 0) { 1232 if ((m->flags & PG_UNMANAGED) == 0) 1233 vm_pageq_remove(m); 1234 cnt.v_wire_count++; 1235 } 1236 m->wire_count++; 1237 KASSERT(m->wire_count != 0, ("vm_page_wire: wire_count overflow m=%p", m)); 1238 splx(s); 1239 vm_page_flag_set(m, PG_MAPPED); 1240 } 1241 1242 /* 1243 * vm_page_unwire: 1244 * 1245 * Release one wiring of this page, potentially 1246 * enabling it to be paged again. 1247 * 1248 * Many pages placed on the inactive queue should actually go 1249 * into the cache, but it is difficult to figure out which. What 1250 * we do instead, if the inactive target is well met, is to put 1251 * clean pages at the head of the inactive queue instead of the tail. 1252 * This will cause them to be moved to the cache more quickly and 1253 * if not actively re-referenced, freed more quickly. If we just 1254 * stick these pages at the end of the inactive queue, heavy filesystem 1255 * meta-data accesses can cause an unnecessary paging load on memory bound 1256 * processes. This optimization causes one-time-use metadata to be 1257 * reused more quickly. 1258 * 1259 * BUT, if we are in a low-memory situation we have no choice but to 1260 * put clean pages on the cache queue. 1261 * 1262 * A number of routines use vm_page_unwire() to guarantee that the page 1263 * will go into either the inactive or active queues, and will NEVER 1264 * be placed in the cache - for example, just after dirtying a page. 1265 * dirty pages in the cache are not allowed. 1266 * 1267 * The page queues must be locked. 1268 * This routine may not block. 1269 */ 1270 void 1271 vm_page_unwire(vm_page_t m, int activate) 1272 { 1273 int s; 1274 1275 s = splvm(); 1276 1277 if (m->wire_count > 0) { 1278 m->wire_count--; 1279 if (m->wire_count == 0) { 1280 cnt.v_wire_count--; 1281 if (m->flags & PG_UNMANAGED) { 1282 ; 1283 } else if (activate) { 1284 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_ACTIVE].pl, m, pageq); 1285 m->queue = PQ_ACTIVE; 1286 vm_page_queues[PQ_ACTIVE].lcnt++; 1287 cnt.v_active_count++; 1288 } else { 1289 vm_page_flag_clear(m, PG_WINATCFLS); 1290 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1291 m->queue = PQ_INACTIVE; 1292 vm_page_queues[PQ_INACTIVE].lcnt++; 1293 cnt.v_inactive_count++; 1294 } 1295 } 1296 } else { 1297 panic("vm_page_unwire: invalid wire count: %d\n", m->wire_count); 1298 } 1299 splx(s); 1300 } 1301 1302 1303 /* 1304 * Move the specified page to the inactive queue. If the page has 1305 * any associated swap, the swap is deallocated. 1306 * 1307 * Normally athead is 0 resulting in LRU operation. athead is set 1308 * to 1 if we want this page to be 'as if it were placed in the cache', 1309 * except without unmapping it from the process address space. 1310 * 1311 * This routine may not block. 1312 */ 1313 static __inline void 1314 _vm_page_deactivate(vm_page_t m, int athead) 1315 { 1316 int s; 1317 1318 GIANT_REQUIRED; 1319 /* 1320 * Ignore if already inactive. 1321 */ 1322 if (m->queue == PQ_INACTIVE) 1323 return; 1324 1325 s = splvm(); 1326 if (m->wire_count == 0 && (m->flags & PG_UNMANAGED) == 0) { 1327 if ((m->queue - m->pc) == PQ_CACHE) 1328 cnt.v_reactivated++; 1329 vm_page_flag_clear(m, PG_WINATCFLS); 1330 vm_pageq_remove(m); 1331 if (athead) 1332 TAILQ_INSERT_HEAD(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1333 else 1334 TAILQ_INSERT_TAIL(&vm_page_queues[PQ_INACTIVE].pl, m, pageq); 1335 m->queue = PQ_INACTIVE; 1336 vm_page_queues[PQ_INACTIVE].lcnt++; 1337 cnt.v_inactive_count++; 1338 } 1339 splx(s); 1340 } 1341 1342 void 1343 vm_page_deactivate(vm_page_t m) 1344 { 1345 _vm_page_deactivate(m, 0); 1346 } 1347 1348 /* 1349 * vm_page_try_to_cache: 1350 * 1351 * Returns 0 on failure, 1 on success 1352 */ 1353 int 1354 vm_page_try_to_cache(vm_page_t m) 1355 { 1356 GIANT_REQUIRED; 1357 1358 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1359 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1360 return(0); 1361 } 1362 vm_page_test_dirty(m); 1363 if (m->dirty) 1364 return(0); 1365 vm_page_cache(m); 1366 return(1); 1367 } 1368 1369 /* 1370 * vm_page_try_to_free() 1371 * 1372 * Attempt to free the page. If we cannot free it, we do nothing. 1373 * 1 is returned on success, 0 on failure. 1374 */ 1375 int 1376 vm_page_try_to_free(vm_page_t m) 1377 { 1378 if (m->dirty || m->hold_count || m->busy || m->wire_count || 1379 (m->flags & (PG_BUSY|PG_UNMANAGED))) { 1380 return(0); 1381 } 1382 vm_page_test_dirty(m); 1383 if (m->dirty) 1384 return(0); 1385 vm_page_busy(m); 1386 vm_page_protect(m, VM_PROT_NONE); 1387 vm_page_free(m); 1388 return(1); 1389 } 1390 1391 /* 1392 * vm_page_cache 1393 * 1394 * Put the specified page onto the page cache queue (if appropriate). 1395 * 1396 * This routine may not block. 1397 */ 1398 void 1399 vm_page_cache(vm_page_t m) 1400 { 1401 int s; 1402 1403 GIANT_REQUIRED; 1404 if ((m->flags & (PG_BUSY|PG_UNMANAGED)) || m->busy || m->wire_count) { 1405 printf("vm_page_cache: attempting to cache busy page\n"); 1406 return; 1407 } 1408 if ((m->queue - m->pc) == PQ_CACHE) 1409 return; 1410 1411 /* 1412 * Remove all pmaps and indicate that the page is not 1413 * writeable or mapped. 1414 */ 1415 1416 vm_page_protect(m, VM_PROT_NONE); 1417 if (m->dirty != 0) { 1418 panic("vm_page_cache: caching a dirty page, pindex: %ld", 1419 (long)m->pindex); 1420 } 1421 s = splvm(); 1422 vm_pageq_remove_nowakeup(m); 1423 m->queue = PQ_CACHE + m->pc; 1424 vm_page_queues[m->queue].lcnt++; 1425 TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m, pageq); 1426 cnt.v_cache_count++; 1427 vm_page_free_wakeup(); 1428 splx(s); 1429 } 1430 1431 /* 1432 * vm_page_dontneed 1433 * 1434 * Cache, deactivate, or do nothing as appropriate. This routine 1435 * is typically used by madvise() MADV_DONTNEED. 1436 * 1437 * Generally speaking we want to move the page into the cache so 1438 * it gets reused quickly. However, this can result in a silly syndrome 1439 * due to the page recycling too quickly. Small objects will not be 1440 * fully cached. On the otherhand, if we move the page to the inactive 1441 * queue we wind up with a problem whereby very large objects 1442 * unnecessarily blow away our inactive and cache queues. 1443 * 1444 * The solution is to move the pages based on a fixed weighting. We 1445 * either leave them alone, deactivate them, or move them to the cache, 1446 * where moving them to the cache has the highest weighting. 1447 * By forcing some pages into other queues we eventually force the 1448 * system to balance the queues, potentially recovering other unrelated 1449 * space from active. The idea is to not force this to happen too 1450 * often. 1451 */ 1452 1453 void 1454 vm_page_dontneed(vm_page_t m) 1455 { 1456 static int dnweight; 1457 int dnw; 1458 int head; 1459 1460 GIANT_REQUIRED; 1461 dnw = ++dnweight; 1462 1463 /* 1464 * occassionally leave the page alone 1465 */ 1466 1467 if ((dnw & 0x01F0) == 0 || 1468 m->queue == PQ_INACTIVE || 1469 m->queue - m->pc == PQ_CACHE 1470 ) { 1471 if (m->act_count >= ACT_INIT) 1472 --m->act_count; 1473 return; 1474 } 1475 1476 if (m->dirty == 0) 1477 vm_page_test_dirty(m); 1478 1479 if (m->dirty || (dnw & 0x0070) == 0) { 1480 /* 1481 * Deactivate the page 3 times out of 32. 1482 */ 1483 head = 0; 1484 } else { 1485 /* 1486 * Cache the page 28 times out of every 32. Note that 1487 * the page is deactivated instead of cached, but placed 1488 * at the head of the queue instead of the tail. 1489 */ 1490 head = 1; 1491 } 1492 _vm_page_deactivate(m, head); 1493 } 1494 1495 /* 1496 * Grab a page, waiting until we are waken up due to the page 1497 * changing state. We keep on waiting, if the page continues 1498 * to be in the object. If the page doesn't exist, allocate it. 1499 * 1500 * This routine may block. 1501 */ 1502 vm_page_t 1503 vm_page_grab(vm_object_t object, vm_pindex_t pindex, int allocflags) 1504 { 1505 vm_page_t m; 1506 int s, generation; 1507 1508 GIANT_REQUIRED; 1509 retrylookup: 1510 if ((m = vm_page_lookup(object, pindex)) != NULL) { 1511 if (m->busy || (m->flags & PG_BUSY)) { 1512 generation = object->generation; 1513 1514 s = splvm(); 1515 while ((object->generation == generation) && 1516 (m->busy || (m->flags & PG_BUSY))) { 1517 vm_page_flag_set(m, PG_WANTED | PG_REFERENCED); 1518 tsleep(m, PVM, "pgrbwt", 0); 1519 if ((allocflags & VM_ALLOC_RETRY) == 0) { 1520 splx(s); 1521 return NULL; 1522 } 1523 } 1524 splx(s); 1525 goto retrylookup; 1526 } else { 1527 vm_page_busy(m); 1528 return m; 1529 } 1530 } 1531 1532 m = vm_page_alloc(object, pindex, allocflags & ~VM_ALLOC_RETRY); 1533 if (m == NULL) { 1534 VM_WAIT; 1535 if ((allocflags & VM_ALLOC_RETRY) == 0) 1536 return NULL; 1537 goto retrylookup; 1538 } 1539 1540 return m; 1541 } 1542 1543 /* 1544 * Mapping function for valid bits or for dirty bits in 1545 * a page. May not block. 1546 * 1547 * Inputs are required to range within a page. 1548 */ 1549 1550 __inline int 1551 vm_page_bits(int base, int size) 1552 { 1553 int first_bit; 1554 int last_bit; 1555 1556 KASSERT( 1557 base + size <= PAGE_SIZE, 1558 ("vm_page_bits: illegal base/size %d/%d", base, size) 1559 ); 1560 1561 if (size == 0) /* handle degenerate case */ 1562 return(0); 1563 1564 first_bit = base >> DEV_BSHIFT; 1565 last_bit = (base + size - 1) >> DEV_BSHIFT; 1566 1567 return ((2 << last_bit) - (1 << first_bit)); 1568 } 1569 1570 /* 1571 * vm_page_set_validclean: 1572 * 1573 * Sets portions of a page valid and clean. The arguments are expected 1574 * to be DEV_BSIZE aligned but if they aren't the bitmap is inclusive 1575 * of any partial chunks touched by the range. The invalid portion of 1576 * such chunks will be zero'd. 1577 * 1578 * This routine may not block. 1579 * 1580 * (base + size) must be less then or equal to PAGE_SIZE. 1581 */ 1582 void 1583 vm_page_set_validclean(vm_page_t m, int base, int size) 1584 { 1585 int pagebits; 1586 int frag; 1587 int endoff; 1588 1589 GIANT_REQUIRED; 1590 if (size == 0) /* handle degenerate case */ 1591 return; 1592 1593 /* 1594 * If the base is not DEV_BSIZE aligned and the valid 1595 * bit is clear, we have to zero out a portion of the 1596 * first block. 1597 */ 1598 1599 if ((frag = base & ~(DEV_BSIZE - 1)) != base && 1600 (m->valid & (1 << (base >> DEV_BSHIFT))) == 0 1601 ) { 1602 pmap_zero_page_area( 1603 VM_PAGE_TO_PHYS(m), 1604 frag, 1605 base - frag 1606 ); 1607 } 1608 1609 /* 1610 * If the ending offset is not DEV_BSIZE aligned and the 1611 * valid bit is clear, we have to zero out a portion of 1612 * the last block. 1613 */ 1614 1615 endoff = base + size; 1616 1617 if ((frag = endoff & ~(DEV_BSIZE - 1)) != endoff && 1618 (m->valid & (1 << (endoff >> DEV_BSHIFT))) == 0 1619 ) { 1620 pmap_zero_page_area( 1621 VM_PAGE_TO_PHYS(m), 1622 endoff, 1623 DEV_BSIZE - (endoff & (DEV_BSIZE - 1)) 1624 ); 1625 } 1626 1627 /* 1628 * Set valid, clear dirty bits. If validating the entire 1629 * page we can safely clear the pmap modify bit. We also 1630 * use this opportunity to clear the PG_NOSYNC flag. If a process 1631 * takes a write fault on a MAP_NOSYNC memory area the flag will 1632 * be set again. 1633 * 1634 * We set valid bits inclusive of any overlap, but we can only 1635 * clear dirty bits for DEV_BSIZE chunks that are fully within 1636 * the range. 1637 */ 1638 1639 pagebits = vm_page_bits(base, size); 1640 m->valid |= pagebits; 1641 #if 0 /* NOT YET */ 1642 if ((frag = base & (DEV_BSIZE - 1)) != 0) { 1643 frag = DEV_BSIZE - frag; 1644 base += frag; 1645 size -= frag; 1646 if (size < 0) 1647 size = 0; 1648 } 1649 pagebits = vm_page_bits(base, size & (DEV_BSIZE - 1)); 1650 #endif 1651 m->dirty &= ~pagebits; 1652 if (base == 0 && size == PAGE_SIZE) { 1653 pmap_clear_modify(m); 1654 vm_page_flag_clear(m, PG_NOSYNC); 1655 } 1656 } 1657 1658 #if 0 1659 1660 void 1661 vm_page_set_dirty(vm_page_t m, int base, int size) 1662 { 1663 m->dirty |= vm_page_bits(base, size); 1664 } 1665 1666 #endif 1667 1668 void 1669 vm_page_clear_dirty(vm_page_t m, int base, int size) 1670 { 1671 GIANT_REQUIRED; 1672 m->dirty &= ~vm_page_bits(base, size); 1673 } 1674 1675 /* 1676 * vm_page_set_invalid: 1677 * 1678 * Invalidates DEV_BSIZE'd chunks within a page. Both the 1679 * valid and dirty bits for the effected areas are cleared. 1680 * 1681 * May not block. 1682 */ 1683 void 1684 vm_page_set_invalid(vm_page_t m, int base, int size) 1685 { 1686 int bits; 1687 1688 GIANT_REQUIRED; 1689 bits = vm_page_bits(base, size); 1690 m->valid &= ~bits; 1691 m->dirty &= ~bits; 1692 m->object->generation++; 1693 } 1694 1695 /* 1696 * vm_page_zero_invalid() 1697 * 1698 * The kernel assumes that the invalid portions of a page contain 1699 * garbage, but such pages can be mapped into memory by user code. 1700 * When this occurs, we must zero out the non-valid portions of the 1701 * page so user code sees what it expects. 1702 * 1703 * Pages are most often semi-valid when the end of a file is mapped 1704 * into memory and the file's size is not page aligned. 1705 */ 1706 1707 void 1708 vm_page_zero_invalid(vm_page_t m, boolean_t setvalid) 1709 { 1710 int b; 1711 int i; 1712 1713 /* 1714 * Scan the valid bits looking for invalid sections that 1715 * must be zerod. Invalid sub-DEV_BSIZE'd areas ( where the 1716 * valid bit may be set ) have already been zerod by 1717 * vm_page_set_validclean(). 1718 */ 1719 1720 for (b = i = 0; i <= PAGE_SIZE / DEV_BSIZE; ++i) { 1721 if (i == (PAGE_SIZE / DEV_BSIZE) || 1722 (m->valid & (1 << i)) 1723 ) { 1724 if (i > b) { 1725 pmap_zero_page_area( 1726 VM_PAGE_TO_PHYS(m), 1727 b << DEV_BSHIFT, 1728 (i - b) << DEV_BSHIFT 1729 ); 1730 } 1731 b = i + 1; 1732 } 1733 } 1734 1735 /* 1736 * setvalid is TRUE when we can safely set the zero'd areas 1737 * as being valid. We can do this if there are no cache consistancy 1738 * issues. e.g. it is ok to do with UFS, but not ok to do with NFS. 1739 */ 1740 1741 if (setvalid) 1742 m->valid = VM_PAGE_BITS_ALL; 1743 } 1744 1745 /* 1746 * vm_page_is_valid: 1747 * 1748 * Is (partial) page valid? Note that the case where size == 0 1749 * will return FALSE in the degenerate case where the page is 1750 * entirely invalid, and TRUE otherwise. 1751 * 1752 * May not block. 1753 */ 1754 1755 int 1756 vm_page_is_valid(vm_page_t m, int base, int size) 1757 { 1758 int bits = vm_page_bits(base, size); 1759 1760 if (m->valid && ((m->valid & bits) == bits)) 1761 return 1; 1762 else 1763 return 0; 1764 } 1765 1766 /* 1767 * update dirty bits from pmap/mmu. May not block. 1768 */ 1769 1770 void 1771 vm_page_test_dirty(vm_page_t m) 1772 { 1773 if ((m->dirty != VM_PAGE_BITS_ALL) && pmap_is_modified(m)) { 1774 vm_page_dirty(m); 1775 } 1776 } 1777 1778 #include "opt_ddb.h" 1779 #ifdef DDB 1780 #include <sys/kernel.h> 1781 1782 #include <ddb/ddb.h> 1783 1784 DB_SHOW_COMMAND(page, vm_page_print_page_info) 1785 { 1786 db_printf("cnt.v_free_count: %d\n", cnt.v_free_count); 1787 db_printf("cnt.v_cache_count: %d\n", cnt.v_cache_count); 1788 db_printf("cnt.v_inactive_count: %d\n", cnt.v_inactive_count); 1789 db_printf("cnt.v_active_count: %d\n", cnt.v_active_count); 1790 db_printf("cnt.v_wire_count: %d\n", cnt.v_wire_count); 1791 db_printf("cnt.v_free_reserved: %d\n", cnt.v_free_reserved); 1792 db_printf("cnt.v_free_min: %d\n", cnt.v_free_min); 1793 db_printf("cnt.v_free_target: %d\n", cnt.v_free_target); 1794 db_printf("cnt.v_cache_min: %d\n", cnt.v_cache_min); 1795 db_printf("cnt.v_inactive_target: %d\n", cnt.v_inactive_target); 1796 } 1797 1798 DB_SHOW_COMMAND(pageq, vm_page_print_pageq_info) 1799 { 1800 int i; 1801 db_printf("PQ_FREE:"); 1802 for (i = 0; i < PQ_L2_SIZE; i++) { 1803 db_printf(" %d", vm_page_queues[PQ_FREE + i].lcnt); 1804 } 1805 db_printf("\n"); 1806 1807 db_printf("PQ_CACHE:"); 1808 for (i = 0; i < PQ_L2_SIZE; i++) { 1809 db_printf(" %d", vm_page_queues[PQ_CACHE + i].lcnt); 1810 } 1811 db_printf("\n"); 1812 1813 db_printf("PQ_ACTIVE: %d, PQ_INACTIVE: %d\n", 1814 vm_page_queues[PQ_ACTIVE].lcnt, 1815 vm_page_queues[PQ_INACTIVE].lcnt); 1816 } 1817 #endif /* DDB */ 1818