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