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