1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1994 John S. Dyson 5 * All rights reserved. 6 * Copyright (c) 1994 David Greenman 7 * All rights reserved. 8 * 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94 42 * 43 * 44 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 45 * All rights reserved. 46 * 47 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 48 * 49 * Permission to use, copy, modify and distribute this software and 50 * its documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 */ 69 70 /* 71 * Page fault handling module. 72 */ 73 74 #include <sys/cdefs.h> 75 __FBSDID("$FreeBSD$"); 76 77 #include "opt_ktrace.h" 78 #include "opt_vm.h" 79 80 #include <sys/param.h> 81 #include <sys/systm.h> 82 #include <sys/kernel.h> 83 #include <sys/lock.h> 84 #include <sys/mutex.h> 85 #include <sys/proc.h> 86 #include <sys/resourcevar.h> 87 #include <sys/sysctl.h> 88 #include <sys/vmmeter.h> 89 #include <sys/vnode.h> 90 #ifdef KTRACE 91 #include <sys/ktrace.h> 92 #endif 93 94 #include <vm/vm.h> 95 #include <vm/vm_param.h> 96 #include <vm/pmap.h> 97 #include <vm/vm_map.h> 98 #include <vm/vm_object.h> 99 #include <vm/vm_page.h> 100 #include <vm/vm_pageout.h> 101 #include <vm/vm_kern.h> 102 #include <vm/vm_pager.h> 103 #include <vm/vm_extern.h> 104 105 #include <sys/mount.h> /* XXX Temporary for VFS_LOCK_GIANT() */ 106 107 #define PFBAK 4 108 #define PFFOR 4 109 #define PAGEORDER_SIZE (PFBAK+PFFOR) 110 111 static int prefault_pageorder[] = { 112 -1 * PAGE_SIZE, 1 * PAGE_SIZE, 113 -2 * PAGE_SIZE, 2 * PAGE_SIZE, 114 -3 * PAGE_SIZE, 3 * PAGE_SIZE, 115 -4 * PAGE_SIZE, 4 * PAGE_SIZE 116 }; 117 118 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *); 119 static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t); 120 121 #define VM_FAULT_READ_AHEAD 8 122 #define VM_FAULT_READ_BEHIND 7 123 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1) 124 125 struct faultstate { 126 vm_page_t m; 127 vm_object_t object; 128 vm_pindex_t pindex; 129 vm_page_t first_m; 130 vm_object_t first_object; 131 vm_pindex_t first_pindex; 132 vm_map_t map; 133 vm_map_entry_t entry; 134 int lookup_still_valid; 135 struct vnode *vp; 136 int vfslocked; 137 }; 138 139 static inline void 140 release_page(struct faultstate *fs) 141 { 142 143 vm_page_wakeup(fs->m); 144 vm_page_lock(fs->m); 145 vm_page_deactivate(fs->m); 146 vm_page_unlock(fs->m); 147 fs->m = NULL; 148 } 149 150 static inline void 151 unlock_map(struct faultstate *fs) 152 { 153 154 if (fs->lookup_still_valid) { 155 vm_map_lookup_done(fs->map, fs->entry); 156 fs->lookup_still_valid = FALSE; 157 } 158 } 159 160 static void 161 unlock_and_deallocate(struct faultstate *fs) 162 { 163 164 vm_object_pip_wakeup(fs->object); 165 VM_OBJECT_UNLOCK(fs->object); 166 if (fs->object != fs->first_object) { 167 VM_OBJECT_LOCK(fs->first_object); 168 vm_page_lock(fs->first_m); 169 vm_page_free(fs->first_m); 170 vm_page_unlock(fs->first_m); 171 vm_object_pip_wakeup(fs->first_object); 172 VM_OBJECT_UNLOCK(fs->first_object); 173 fs->first_m = NULL; 174 } 175 vm_object_deallocate(fs->first_object); 176 unlock_map(fs); 177 if (fs->vp != NULL) { 178 vput(fs->vp); 179 fs->vp = NULL; 180 } 181 VFS_UNLOCK_GIANT(fs->vfslocked); 182 fs->vfslocked = 0; 183 } 184 185 /* 186 * TRYPAGER - used by vm_fault to calculate whether the pager for the 187 * current object *might* contain the page. 188 * 189 * default objects are zero-fill, there is no real pager. 190 */ 191 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \ 192 ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired)) 193 194 /* 195 * vm_fault: 196 * 197 * Handle a page fault occurring at the given address, 198 * requiring the given permissions, in the map specified. 199 * If successful, the page is inserted into the 200 * associated physical map. 201 * 202 * NOTE: the given address should be truncated to the 203 * proper page address. 204 * 205 * KERN_SUCCESS is returned if the page fault is handled; otherwise, 206 * a standard error specifying why the fault is fatal is returned. 207 * 208 * The map in question must be referenced, and remains so. 209 * Caller may hold no locks. 210 */ 211 int 212 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 213 int fault_flags) 214 { 215 struct thread *td; 216 int result; 217 218 td = curthread; 219 if ((td->td_pflags & TDP_NOFAULTING) != 0) 220 return (KERN_PROTECTION_FAILURE); 221 #ifdef KTRACE 222 if (map != kernel_map && KTRPOINT(td, KTR_FAULT)) 223 ktrfault(vaddr, fault_type); 224 #endif 225 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags, 226 NULL); 227 #ifdef KTRACE 228 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND)) 229 ktrfaultend(result); 230 #endif 231 return (result); 232 } 233 234 int 235 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 236 int fault_flags, vm_page_t *m_hold) 237 { 238 vm_prot_t prot; 239 int is_first_object_locked, result; 240 boolean_t growstack, wired; 241 int map_generation; 242 vm_object_t next_object; 243 vm_page_t marray[VM_FAULT_READ], mt, mt_prev; 244 int hardfault; 245 int faultcount, ahead, behind, alloc_req; 246 struct faultstate fs; 247 struct vnode *vp; 248 int locked, error; 249 250 hardfault = 0; 251 growstack = TRUE; 252 PCPU_INC(cnt.v_vm_faults); 253 fs.vp = NULL; 254 fs.vfslocked = 0; 255 faultcount = behind = 0; 256 257 RetryFault:; 258 259 /* 260 * Find the backing store object and offset into it to begin the 261 * search. 262 */ 263 fs.map = map; 264 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry, 265 &fs.first_object, &fs.first_pindex, &prot, &wired); 266 if (result != KERN_SUCCESS) { 267 if (growstack && result == KERN_INVALID_ADDRESS && 268 map != kernel_map) { 269 result = vm_map_growstack(curproc, vaddr); 270 if (result != KERN_SUCCESS) 271 return (KERN_FAILURE); 272 growstack = FALSE; 273 goto RetryFault; 274 } 275 return (result); 276 } 277 278 map_generation = fs.map->timestamp; 279 280 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 281 panic("vm_fault: fault on nofault entry, addr: %lx", 282 (u_long)vaddr); 283 } 284 285 /* 286 * Make a reference to this object to prevent its disposal while we 287 * are messing with it. Once we have the reference, the map is free 288 * to be diddled. Since objects reference their shadows (and copies), 289 * they will stay around as well. 290 * 291 * Bump the paging-in-progress count to prevent size changes (e.g. 292 * truncation operations) during I/O. This must be done after 293 * obtaining the vnode lock in order to avoid possible deadlocks. 294 */ 295 VM_OBJECT_LOCK(fs.first_object); 296 vm_object_reference_locked(fs.first_object); 297 vm_object_pip_add(fs.first_object, 1); 298 299 fs.lookup_still_valid = TRUE; 300 301 if (wired) 302 fault_type = prot | (fault_type & VM_PROT_COPY); 303 304 fs.first_m = NULL; 305 306 /* 307 * Search for the page at object/offset. 308 */ 309 fs.object = fs.first_object; 310 fs.pindex = fs.first_pindex; 311 while (TRUE) { 312 /* 313 * If the object is dead, we stop here 314 */ 315 if (fs.object->flags & OBJ_DEAD) { 316 unlock_and_deallocate(&fs); 317 return (KERN_PROTECTION_FAILURE); 318 } 319 320 /* 321 * See if page is resident 322 */ 323 fs.m = vm_page_lookup(fs.object, fs.pindex); 324 if (fs.m != NULL) { 325 /* 326 * check for page-based copy on write. 327 * We check fs.object == fs.first_object so 328 * as to ensure the legacy COW mechanism is 329 * used when the page in question is part of 330 * a shadow object. Otherwise, vm_page_cowfault() 331 * removes the page from the backing object, 332 * which is not what we want. 333 */ 334 vm_page_lock(fs.m); 335 if ((fs.m->cow) && 336 (fault_type & VM_PROT_WRITE) && 337 (fs.object == fs.first_object)) { 338 vm_page_cowfault(fs.m); 339 unlock_and_deallocate(&fs); 340 goto RetryFault; 341 } 342 343 /* 344 * Wait/Retry if the page is busy. We have to do this 345 * if the page is busy via either VPO_BUSY or 346 * vm_page_t->busy because the vm_pager may be using 347 * vm_page_t->busy for pageouts ( and even pageins if 348 * it is the vnode pager ), and we could end up trying 349 * to pagein and pageout the same page simultaneously. 350 * 351 * We can theoretically allow the busy case on a read 352 * fault if the page is marked valid, but since such 353 * pages are typically already pmap'd, putting that 354 * special case in might be more effort then it is 355 * worth. We cannot under any circumstances mess 356 * around with a vm_page_t->busy page except, perhaps, 357 * to pmap it. 358 */ 359 if ((fs.m->oflags & VPO_BUSY) || fs.m->busy) { 360 /* 361 * Reference the page before unlocking and 362 * sleeping so that the page daemon is less 363 * likely to reclaim it. 364 */ 365 vm_page_aflag_set(fs.m, PGA_REFERENCED); 366 vm_page_unlock(fs.m); 367 if (fs.object != fs.first_object) { 368 if (!VM_OBJECT_TRYLOCK( 369 fs.first_object)) { 370 VM_OBJECT_UNLOCK(fs.object); 371 VM_OBJECT_LOCK(fs.first_object); 372 VM_OBJECT_LOCK(fs.object); 373 } 374 vm_page_lock(fs.first_m); 375 vm_page_free(fs.first_m); 376 vm_page_unlock(fs.first_m); 377 vm_object_pip_wakeup(fs.first_object); 378 VM_OBJECT_UNLOCK(fs.first_object); 379 fs.first_m = NULL; 380 } 381 unlock_map(&fs); 382 if (fs.m == vm_page_lookup(fs.object, 383 fs.pindex)) { 384 vm_page_sleep_if_busy(fs.m, TRUE, 385 "vmpfw"); 386 } 387 vm_object_pip_wakeup(fs.object); 388 VM_OBJECT_UNLOCK(fs.object); 389 PCPU_INC(cnt.v_intrans); 390 vm_object_deallocate(fs.first_object); 391 goto RetryFault; 392 } 393 vm_pageq_remove(fs.m); 394 vm_page_unlock(fs.m); 395 396 /* 397 * Mark page busy for other processes, and the 398 * pagedaemon. If it still isn't completely valid 399 * (readable), jump to readrest, else break-out ( we 400 * found the page ). 401 */ 402 vm_page_busy(fs.m); 403 if (fs.m->valid != VM_PAGE_BITS_ALL) 404 goto readrest; 405 break; 406 } 407 408 /* 409 * Page is not resident, If this is the search termination 410 * or the pager might contain the page, allocate a new page. 411 */ 412 if (TRYPAGER || fs.object == fs.first_object) { 413 if (fs.pindex >= fs.object->size) { 414 unlock_and_deallocate(&fs); 415 return (KERN_PROTECTION_FAILURE); 416 } 417 418 /* 419 * Allocate a new page for this object/offset pair. 420 * 421 * Unlocked read of the p_flag is harmless. At 422 * worst, the P_KILLED might be not observed 423 * there, and allocation can fail, causing 424 * restart and new reading of the p_flag. 425 */ 426 fs.m = NULL; 427 if (!vm_page_count_severe() || P_KILLED(curproc)) { 428 #if VM_NRESERVLEVEL > 0 429 if ((fs.object->flags & OBJ_COLORED) == 0) { 430 fs.object->flags |= OBJ_COLORED; 431 fs.object->pg_color = atop(vaddr) - 432 fs.pindex; 433 } 434 #endif 435 alloc_req = P_KILLED(curproc) ? 436 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL; 437 if (fs.object->type != OBJT_VNODE && 438 fs.object->backing_object == NULL) 439 alloc_req |= VM_ALLOC_ZERO; 440 fs.m = vm_page_alloc(fs.object, fs.pindex, 441 alloc_req); 442 } 443 if (fs.m == NULL) { 444 unlock_and_deallocate(&fs); 445 VM_WAITPFAULT; 446 goto RetryFault; 447 } else if (fs.m->valid == VM_PAGE_BITS_ALL) 448 break; 449 } 450 451 readrest: 452 /* 453 * We have found a valid page or we have allocated a new page. 454 * The page thus may not be valid or may not be entirely 455 * valid. 456 * 457 * Attempt to fault-in the page if there is a chance that the 458 * pager has it, and potentially fault in additional pages 459 * at the same time. 460 */ 461 if (TRYPAGER) { 462 int rv; 463 int reqpage = 0; 464 u_char behavior = vm_map_entry_behavior(fs.entry); 465 466 if (behavior == MAP_ENTRY_BEHAV_RANDOM || 467 P_KILLED(curproc)) { 468 ahead = 0; 469 behind = 0; 470 } else { 471 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT; 472 if (behind > VM_FAULT_READ_BEHIND) 473 behind = VM_FAULT_READ_BEHIND; 474 475 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1; 476 if (ahead > VM_FAULT_READ_AHEAD) 477 ahead = VM_FAULT_READ_AHEAD; 478 } 479 is_first_object_locked = FALSE; 480 if ((behavior == MAP_ENTRY_BEHAV_SEQUENTIAL || 481 (behavior != MAP_ENTRY_BEHAV_RANDOM && 482 fs.pindex >= fs.entry->lastr && 483 fs.pindex < fs.entry->lastr + VM_FAULT_READ)) && 484 (fs.first_object == fs.object || 485 (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object))) && 486 fs.first_object->type != OBJT_DEVICE && 487 fs.first_object->type != OBJT_PHYS && 488 fs.first_object->type != OBJT_SG) { 489 vm_pindex_t firstpindex; 490 491 if (fs.first_pindex < 2 * VM_FAULT_READ) 492 firstpindex = 0; 493 else 494 firstpindex = fs.first_pindex - 2 * VM_FAULT_READ; 495 mt = fs.first_object != fs.object ? 496 fs.first_m : fs.m; 497 KASSERT(mt != NULL, ("vm_fault: missing mt")); 498 KASSERT((mt->oflags & VPO_BUSY) != 0, 499 ("vm_fault: mt %p not busy", mt)); 500 mt_prev = vm_page_prev(mt); 501 502 /* 503 * note: partially valid pages cannot be 504 * included in the lookahead - NFS piecemeal 505 * writes will barf on it badly. 506 */ 507 while ((mt = mt_prev) != NULL && 508 mt->pindex >= firstpindex && 509 mt->valid == VM_PAGE_BITS_ALL) { 510 mt_prev = vm_page_prev(mt); 511 if (mt->busy || 512 (mt->oflags & VPO_BUSY)) 513 continue; 514 vm_page_lock(mt); 515 if (mt->hold_count || 516 mt->wire_count) { 517 vm_page_unlock(mt); 518 continue; 519 } 520 pmap_remove_all(mt); 521 if (mt->dirty != 0) 522 vm_page_deactivate(mt); 523 else 524 vm_page_cache(mt); 525 vm_page_unlock(mt); 526 } 527 ahead += behind; 528 behind = 0; 529 } 530 if (is_first_object_locked) 531 VM_OBJECT_UNLOCK(fs.first_object); 532 533 /* 534 * Call the pager to retrieve the data, if any, after 535 * releasing the lock on the map. We hold a ref on 536 * fs.object and the pages are VPO_BUSY'd. 537 */ 538 unlock_map(&fs); 539 540 vnode_lock: 541 if (fs.object->type == OBJT_VNODE) { 542 vp = fs.object->handle; 543 if (vp == fs.vp) 544 goto vnode_locked; 545 else if (fs.vp != NULL) { 546 vput(fs.vp); 547 fs.vp = NULL; 548 } 549 locked = VOP_ISLOCKED(vp); 550 551 if (VFS_NEEDSGIANT(vp->v_mount) && !fs.vfslocked) { 552 fs.vfslocked = 1; 553 if (!mtx_trylock(&Giant)) { 554 VM_OBJECT_UNLOCK(fs.object); 555 mtx_lock(&Giant); 556 VM_OBJECT_LOCK(fs.object); 557 goto vnode_lock; 558 } 559 } 560 if (locked != LK_EXCLUSIVE) 561 locked = LK_SHARED; 562 /* Do not sleep for vnode lock while fs.m is busy */ 563 error = vget(vp, locked | LK_CANRECURSE | 564 LK_NOWAIT, curthread); 565 if (error != 0) { 566 int vfslocked; 567 568 vfslocked = fs.vfslocked; 569 fs.vfslocked = 0; /* Keep Giant */ 570 vhold(vp); 571 release_page(&fs); 572 unlock_and_deallocate(&fs); 573 error = vget(vp, locked | LK_RETRY | 574 LK_CANRECURSE, curthread); 575 vdrop(vp); 576 fs.vp = vp; 577 fs.vfslocked = vfslocked; 578 KASSERT(error == 0, 579 ("vm_fault: vget failed")); 580 goto RetryFault; 581 } 582 fs.vp = vp; 583 } 584 vnode_locked: 585 KASSERT(fs.vp == NULL || !fs.map->system_map, 586 ("vm_fault: vnode-backed object mapped by system map")); 587 588 /* 589 * now we find out if any other pages should be paged 590 * in at this time this routine checks to see if the 591 * pages surrounding this fault reside in the same 592 * object as the page for this fault. If they do, 593 * then they are faulted in also into the object. The 594 * array "marray" returned contains an array of 595 * vm_page_t structs where one of them is the 596 * vm_page_t passed to the routine. The reqpage 597 * return value is the index into the marray for the 598 * vm_page_t passed to the routine. 599 * 600 * fs.m plus the additional pages are VPO_BUSY'd. 601 */ 602 faultcount = vm_fault_additional_pages( 603 fs.m, behind, ahead, marray, &reqpage); 604 605 rv = faultcount ? 606 vm_pager_get_pages(fs.object, marray, faultcount, 607 reqpage) : VM_PAGER_FAIL; 608 609 if (rv == VM_PAGER_OK) { 610 /* 611 * Found the page. Leave it busy while we play 612 * with it. 613 */ 614 615 /* 616 * Relookup in case pager changed page. Pager 617 * is responsible for disposition of old page 618 * if moved. 619 */ 620 fs.m = vm_page_lookup(fs.object, fs.pindex); 621 if (!fs.m) { 622 unlock_and_deallocate(&fs); 623 goto RetryFault; 624 } 625 626 hardfault++; 627 break; /* break to PAGE HAS BEEN FOUND */ 628 } 629 /* 630 * Remove the bogus page (which does not exist at this 631 * object/offset); before doing so, we must get back 632 * our object lock to preserve our invariant. 633 * 634 * Also wake up any other process that may want to bring 635 * in this page. 636 * 637 * If this is the top-level object, we must leave the 638 * busy page to prevent another process from rushing 639 * past us, and inserting the page in that object at 640 * the same time that we are. 641 */ 642 if (rv == VM_PAGER_ERROR) 643 printf("vm_fault: pager read error, pid %d (%s)\n", 644 curproc->p_pid, curproc->p_comm); 645 /* 646 * Data outside the range of the pager or an I/O error 647 */ 648 /* 649 * XXX - the check for kernel_map is a kludge to work 650 * around having the machine panic on a kernel space 651 * fault w/ I/O error. 652 */ 653 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) || 654 (rv == VM_PAGER_BAD)) { 655 vm_page_lock(fs.m); 656 vm_page_free(fs.m); 657 vm_page_unlock(fs.m); 658 fs.m = NULL; 659 unlock_and_deallocate(&fs); 660 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE); 661 } 662 if (fs.object != fs.first_object) { 663 vm_page_lock(fs.m); 664 vm_page_free(fs.m); 665 vm_page_unlock(fs.m); 666 fs.m = NULL; 667 /* 668 * XXX - we cannot just fall out at this 669 * point, m has been freed and is invalid! 670 */ 671 } 672 } 673 674 /* 675 * We get here if the object has default pager (or unwiring) 676 * or the pager doesn't have the page. 677 */ 678 if (fs.object == fs.first_object) 679 fs.first_m = fs.m; 680 681 /* 682 * Move on to the next object. Lock the next object before 683 * unlocking the current one. 684 */ 685 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset); 686 next_object = fs.object->backing_object; 687 if (next_object == NULL) { 688 /* 689 * If there's no object left, fill the page in the top 690 * object with zeros. 691 */ 692 if (fs.object != fs.first_object) { 693 vm_object_pip_wakeup(fs.object); 694 VM_OBJECT_UNLOCK(fs.object); 695 696 fs.object = fs.first_object; 697 fs.pindex = fs.first_pindex; 698 fs.m = fs.first_m; 699 VM_OBJECT_LOCK(fs.object); 700 } 701 fs.first_m = NULL; 702 703 /* 704 * Zero the page if necessary and mark it valid. 705 */ 706 if ((fs.m->flags & PG_ZERO) == 0) { 707 pmap_zero_page(fs.m); 708 } else { 709 PCPU_INC(cnt.v_ozfod); 710 } 711 PCPU_INC(cnt.v_zfod); 712 fs.m->valid = VM_PAGE_BITS_ALL; 713 break; /* break to PAGE HAS BEEN FOUND */ 714 } else { 715 KASSERT(fs.object != next_object, 716 ("object loop %p", next_object)); 717 VM_OBJECT_LOCK(next_object); 718 vm_object_pip_add(next_object, 1); 719 if (fs.object != fs.first_object) 720 vm_object_pip_wakeup(fs.object); 721 VM_OBJECT_UNLOCK(fs.object); 722 fs.object = next_object; 723 } 724 } 725 726 KASSERT((fs.m->oflags & VPO_BUSY) != 0, 727 ("vm_fault: not busy after main loop")); 728 729 /* 730 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 731 * is held.] 732 */ 733 734 /* 735 * If the page is being written, but isn't already owned by the 736 * top-level object, we have to copy it into a new page owned by the 737 * top-level object. 738 */ 739 if (fs.object != fs.first_object) { 740 /* 741 * We only really need to copy if we want to write it. 742 */ 743 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) { 744 /* 745 * This allows pages to be virtually copied from a 746 * backing_object into the first_object, where the 747 * backing object has no other refs to it, and cannot 748 * gain any more refs. Instead of a bcopy, we just 749 * move the page from the backing object to the 750 * first object. Note that we must mark the page 751 * dirty in the first object so that it will go out 752 * to swap when needed. 753 */ 754 is_first_object_locked = FALSE; 755 if ( 756 /* 757 * Only one shadow object 758 */ 759 (fs.object->shadow_count == 1) && 760 /* 761 * No COW refs, except us 762 */ 763 (fs.object->ref_count == 1) && 764 /* 765 * No one else can look this object up 766 */ 767 (fs.object->handle == NULL) && 768 /* 769 * No other ways to look the object up 770 */ 771 ((fs.object->type == OBJT_DEFAULT) || 772 (fs.object->type == OBJT_SWAP)) && 773 (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) && 774 /* 775 * We don't chase down the shadow chain 776 */ 777 fs.object == fs.first_object->backing_object) { 778 /* 779 * get rid of the unnecessary page 780 */ 781 vm_page_lock(fs.first_m); 782 vm_page_free(fs.first_m); 783 vm_page_unlock(fs.first_m); 784 /* 785 * grab the page and put it into the 786 * process'es object. The page is 787 * automatically made dirty. 788 */ 789 vm_page_lock(fs.m); 790 vm_page_rename(fs.m, fs.first_object, fs.first_pindex); 791 vm_page_unlock(fs.m); 792 vm_page_busy(fs.m); 793 fs.first_m = fs.m; 794 fs.m = NULL; 795 PCPU_INC(cnt.v_cow_optim); 796 } else { 797 /* 798 * Oh, well, lets copy it. 799 */ 800 pmap_copy_page(fs.m, fs.first_m); 801 fs.first_m->valid = VM_PAGE_BITS_ALL; 802 if (wired && (fault_flags & 803 VM_FAULT_CHANGE_WIRING) == 0) { 804 vm_page_lock(fs.first_m); 805 vm_page_wire(fs.first_m); 806 vm_page_unlock(fs.first_m); 807 808 vm_page_lock(fs.m); 809 vm_page_unwire(fs.m, FALSE); 810 vm_page_unlock(fs.m); 811 } 812 /* 813 * We no longer need the old page or object. 814 */ 815 release_page(&fs); 816 } 817 /* 818 * fs.object != fs.first_object due to above 819 * conditional 820 */ 821 vm_object_pip_wakeup(fs.object); 822 VM_OBJECT_UNLOCK(fs.object); 823 /* 824 * Only use the new page below... 825 */ 826 fs.object = fs.first_object; 827 fs.pindex = fs.first_pindex; 828 fs.m = fs.first_m; 829 if (!is_first_object_locked) 830 VM_OBJECT_LOCK(fs.object); 831 PCPU_INC(cnt.v_cow_faults); 832 } else { 833 prot &= ~VM_PROT_WRITE; 834 } 835 } 836 837 /* 838 * We must verify that the maps have not changed since our last 839 * lookup. 840 */ 841 if (!fs.lookup_still_valid) { 842 vm_object_t retry_object; 843 vm_pindex_t retry_pindex; 844 vm_prot_t retry_prot; 845 846 if (!vm_map_trylock_read(fs.map)) { 847 release_page(&fs); 848 unlock_and_deallocate(&fs); 849 goto RetryFault; 850 } 851 fs.lookup_still_valid = TRUE; 852 if (fs.map->timestamp != map_generation) { 853 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type, 854 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 855 856 /* 857 * If we don't need the page any longer, put it on the inactive 858 * list (the easiest thing to do here). If no one needs it, 859 * pageout will grab it eventually. 860 */ 861 if (result != KERN_SUCCESS) { 862 release_page(&fs); 863 unlock_and_deallocate(&fs); 864 865 /* 866 * If retry of map lookup would have blocked then 867 * retry fault from start. 868 */ 869 if (result == KERN_FAILURE) 870 goto RetryFault; 871 return (result); 872 } 873 if ((retry_object != fs.first_object) || 874 (retry_pindex != fs.first_pindex)) { 875 release_page(&fs); 876 unlock_and_deallocate(&fs); 877 goto RetryFault; 878 } 879 880 /* 881 * Check whether the protection has changed or the object has 882 * been copied while we left the map unlocked. Changing from 883 * read to write permission is OK - we leave the page 884 * write-protected, and catch the write fault. Changing from 885 * write to read permission means that we can't mark the page 886 * write-enabled after all. 887 */ 888 prot &= retry_prot; 889 } 890 } 891 /* 892 * If the page was filled by a pager, update the map entry's 893 * last read offset. Since the pager does not return the 894 * actual set of pages that it read, this update is based on 895 * the requested set. Typically, the requested and actual 896 * sets are the same. 897 * 898 * XXX The following assignment modifies the map 899 * without holding a write lock on it. 900 */ 901 if (hardfault) 902 fs.entry->lastr = fs.pindex + faultcount - behind; 903 904 if ((prot & VM_PROT_WRITE) != 0 || 905 (fault_flags & VM_FAULT_DIRTY) != 0) { 906 vm_object_set_writeable_dirty(fs.object); 907 908 /* 909 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC 910 * if the page is already dirty to prevent data written with 911 * the expectation of being synced from not being synced. 912 * Likewise if this entry does not request NOSYNC then make 913 * sure the page isn't marked NOSYNC. Applications sharing 914 * data should use the same flags to avoid ping ponging. 915 */ 916 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) { 917 if (fs.m->dirty == 0) 918 fs.m->oflags |= VPO_NOSYNC; 919 } else { 920 fs.m->oflags &= ~VPO_NOSYNC; 921 } 922 923 /* 924 * If the fault is a write, we know that this page is being 925 * written NOW so dirty it explicitly to save on 926 * pmap_is_modified() calls later. 927 * 928 * Also tell the backing pager, if any, that it should remove 929 * any swap backing since the page is now dirty. 930 */ 931 if (((fault_type & VM_PROT_WRITE) != 0 && 932 (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) || 933 (fault_flags & VM_FAULT_DIRTY) != 0) { 934 vm_page_dirty(fs.m); 935 vm_pager_page_unswapped(fs.m); 936 } 937 } 938 939 /* 940 * Page had better still be busy 941 */ 942 KASSERT(fs.m->oflags & VPO_BUSY, 943 ("vm_fault: page %p not busy!", fs.m)); 944 /* 945 * Page must be completely valid or it is not fit to 946 * map into user space. vm_pager_get_pages() ensures this. 947 */ 948 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL, 949 ("vm_fault: page %p partially invalid", fs.m)); 950 VM_OBJECT_UNLOCK(fs.object); 951 952 /* 953 * Put this page into the physical map. We had to do the unlock above 954 * because pmap_enter() may sleep. We don't put the page 955 * back on the active queue until later so that the pageout daemon 956 * won't find it (yet). 957 */ 958 pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired); 959 if ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 && wired == 0) 960 vm_fault_prefault(fs.map->pmap, vaddr, fs.entry); 961 VM_OBJECT_LOCK(fs.object); 962 vm_page_lock(fs.m); 963 964 /* 965 * If the page is not wired down, then put it where the pageout daemon 966 * can find it. 967 */ 968 if (fault_flags & VM_FAULT_CHANGE_WIRING) { 969 if (wired) 970 vm_page_wire(fs.m); 971 else 972 vm_page_unwire(fs.m, 1); 973 } else 974 vm_page_activate(fs.m); 975 if (m_hold != NULL) { 976 *m_hold = fs.m; 977 vm_page_hold(fs.m); 978 } 979 vm_page_unlock(fs.m); 980 vm_page_wakeup(fs.m); 981 982 /* 983 * Unlock everything, and return 984 */ 985 unlock_and_deallocate(&fs); 986 if (hardfault) 987 curthread->td_ru.ru_majflt++; 988 else 989 curthread->td_ru.ru_minflt++; 990 991 return (KERN_SUCCESS); 992 } 993 994 /* 995 * vm_fault_prefault provides a quick way of clustering 996 * pagefaults into a processes address space. It is a "cousin" 997 * of vm_map_pmap_enter, except it runs at page fault time instead 998 * of mmap time. 999 */ 1000 static void 1001 vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry) 1002 { 1003 int i; 1004 vm_offset_t addr, starta; 1005 vm_pindex_t pindex; 1006 vm_page_t m; 1007 vm_object_t object; 1008 1009 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 1010 return; 1011 1012 object = entry->object.vm_object; 1013 1014 starta = addra - PFBAK * PAGE_SIZE; 1015 if (starta < entry->start) { 1016 starta = entry->start; 1017 } else if (starta > addra) { 1018 starta = 0; 1019 } 1020 1021 for (i = 0; i < PAGEORDER_SIZE; i++) { 1022 vm_object_t backing_object, lobject; 1023 1024 addr = addra + prefault_pageorder[i]; 1025 if (addr > addra + (PFFOR * PAGE_SIZE)) 1026 addr = 0; 1027 1028 if (addr < starta || addr >= entry->end) 1029 continue; 1030 1031 if (!pmap_is_prefaultable(pmap, addr)) 1032 continue; 1033 1034 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 1035 lobject = object; 1036 VM_OBJECT_LOCK(lobject); 1037 while ((m = vm_page_lookup(lobject, pindex)) == NULL && 1038 lobject->type == OBJT_DEFAULT && 1039 (backing_object = lobject->backing_object) != NULL) { 1040 KASSERT((lobject->backing_object_offset & PAGE_MASK) == 1041 0, ("vm_fault_prefault: unaligned object offset")); 1042 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 1043 VM_OBJECT_LOCK(backing_object); 1044 VM_OBJECT_UNLOCK(lobject); 1045 lobject = backing_object; 1046 } 1047 /* 1048 * give-up when a page is not in memory 1049 */ 1050 if (m == NULL) { 1051 VM_OBJECT_UNLOCK(lobject); 1052 break; 1053 } 1054 if (m->valid == VM_PAGE_BITS_ALL && 1055 (m->flags & PG_FICTITIOUS) == 0) 1056 pmap_enter_quick(pmap, addr, m, entry->protection); 1057 VM_OBJECT_UNLOCK(lobject); 1058 } 1059 } 1060 1061 /* 1062 * Hold each of the physical pages that are mapped by the specified range of 1063 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid 1064 * and allow the specified types of access, "prot". If all of the implied 1065 * pages are successfully held, then the number of held pages is returned 1066 * together with pointers to those pages in the array "ma". However, if any 1067 * of the pages cannot be held, -1 is returned. 1068 */ 1069 int 1070 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len, 1071 vm_prot_t prot, vm_page_t *ma, int max_count) 1072 { 1073 vm_offset_t end, va; 1074 vm_page_t *mp; 1075 int count; 1076 boolean_t pmap_failed; 1077 1078 if (len == 0) 1079 return (0); 1080 end = round_page(addr + len); 1081 addr = trunc_page(addr); 1082 1083 /* 1084 * Check for illegal addresses. 1085 */ 1086 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map)) 1087 return (-1); 1088 1089 count = howmany(end - addr, PAGE_SIZE); 1090 if (count > max_count) 1091 panic("vm_fault_quick_hold_pages: count > max_count"); 1092 1093 /* 1094 * Most likely, the physical pages are resident in the pmap, so it is 1095 * faster to try pmap_extract_and_hold() first. 1096 */ 1097 pmap_failed = FALSE; 1098 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) { 1099 *mp = pmap_extract_and_hold(map->pmap, va, prot); 1100 if (*mp == NULL) 1101 pmap_failed = TRUE; 1102 else if ((prot & VM_PROT_WRITE) != 0 && 1103 (*mp)->dirty != VM_PAGE_BITS_ALL) { 1104 /* 1105 * Explicitly dirty the physical page. Otherwise, the 1106 * caller's changes may go unnoticed because they are 1107 * performed through an unmanaged mapping or by a DMA 1108 * operation. 1109 * 1110 * The object lock is not held here. 1111 * See vm_page_clear_dirty_mask(). 1112 */ 1113 vm_page_dirty(*mp); 1114 } 1115 } 1116 if (pmap_failed) { 1117 /* 1118 * One or more pages could not be held by the pmap. Either no 1119 * page was mapped at the specified virtual address or that 1120 * mapping had insufficient permissions. Attempt to fault in 1121 * and hold these pages. 1122 */ 1123 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) 1124 if (*mp == NULL && vm_fault_hold(map, va, prot, 1125 VM_FAULT_NORMAL, mp) != KERN_SUCCESS) 1126 goto error; 1127 } 1128 return (count); 1129 error: 1130 for (mp = ma; mp < ma + count; mp++) 1131 if (*mp != NULL) { 1132 vm_page_lock(*mp); 1133 vm_page_unhold(*mp); 1134 vm_page_unlock(*mp); 1135 } 1136 return (-1); 1137 } 1138 1139 /* 1140 * vm_fault_wire: 1141 * 1142 * Wire down a range of virtual addresses in a map. 1143 */ 1144 int 1145 vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1146 boolean_t fictitious) 1147 { 1148 vm_offset_t va; 1149 int rv; 1150 1151 /* 1152 * We simulate a fault to get the page and enter it in the physical 1153 * map. For user wiring, we only ask for read access on currently 1154 * read-only sections. 1155 */ 1156 for (va = start; va < end; va += PAGE_SIZE) { 1157 rv = vm_fault(map, va, VM_PROT_NONE, VM_FAULT_CHANGE_WIRING); 1158 if (rv) { 1159 if (va != start) 1160 vm_fault_unwire(map, start, va, fictitious); 1161 return (rv); 1162 } 1163 } 1164 return (KERN_SUCCESS); 1165 } 1166 1167 /* 1168 * vm_fault_unwire: 1169 * 1170 * Unwire a range of virtual addresses in a map. 1171 */ 1172 void 1173 vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1174 boolean_t fictitious) 1175 { 1176 vm_paddr_t pa; 1177 vm_offset_t va; 1178 vm_page_t m; 1179 pmap_t pmap; 1180 1181 pmap = vm_map_pmap(map); 1182 1183 /* 1184 * Since the pages are wired down, we must be able to get their 1185 * mappings from the physical map system. 1186 */ 1187 for (va = start; va < end; va += PAGE_SIZE) { 1188 pa = pmap_extract(pmap, va); 1189 if (pa != 0) { 1190 pmap_change_wiring(pmap, va, FALSE); 1191 if (!fictitious) { 1192 m = PHYS_TO_VM_PAGE(pa); 1193 vm_page_lock(m); 1194 vm_page_unwire(m, TRUE); 1195 vm_page_unlock(m); 1196 } 1197 } 1198 } 1199 } 1200 1201 /* 1202 * Routine: 1203 * vm_fault_copy_entry 1204 * Function: 1205 * Create new shadow object backing dst_entry with private copy of 1206 * all underlying pages. When src_entry is equal to dst_entry, 1207 * function implements COW for wired-down map entry. Otherwise, 1208 * it forks wired entry into dst_map. 1209 * 1210 * In/out conditions: 1211 * The source and destination maps must be locked for write. 1212 * The source map entry must be wired down (or be a sharing map 1213 * entry corresponding to a main map entry that is wired down). 1214 */ 1215 void 1216 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map, 1217 vm_map_entry_t dst_entry, vm_map_entry_t src_entry, 1218 vm_ooffset_t *fork_charge) 1219 { 1220 vm_object_t backing_object, dst_object, object, src_object; 1221 vm_pindex_t dst_pindex, pindex, src_pindex; 1222 vm_prot_t access, prot; 1223 vm_offset_t vaddr; 1224 vm_page_t dst_m; 1225 vm_page_t src_m; 1226 boolean_t src_readonly, upgrade; 1227 1228 #ifdef lint 1229 src_map++; 1230 #endif /* lint */ 1231 1232 upgrade = src_entry == dst_entry; 1233 1234 src_object = src_entry->object.vm_object; 1235 src_pindex = OFF_TO_IDX(src_entry->offset); 1236 src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0; 1237 1238 /* 1239 * Create the top-level object for the destination entry. (Doesn't 1240 * actually shadow anything - we copy the pages directly.) 1241 */ 1242 dst_object = vm_object_allocate(OBJT_DEFAULT, 1243 OFF_TO_IDX(dst_entry->end - dst_entry->start)); 1244 #if VM_NRESERVLEVEL > 0 1245 dst_object->flags |= OBJ_COLORED; 1246 dst_object->pg_color = atop(dst_entry->start); 1247 #endif 1248 1249 VM_OBJECT_LOCK(dst_object); 1250 KASSERT(upgrade || dst_entry->object.vm_object == NULL, 1251 ("vm_fault_copy_entry: vm_object not NULL")); 1252 dst_entry->object.vm_object = dst_object; 1253 dst_entry->offset = 0; 1254 dst_object->charge = dst_entry->end - dst_entry->start; 1255 if (fork_charge != NULL) { 1256 KASSERT(dst_entry->cred == NULL, 1257 ("vm_fault_copy_entry: leaked swp charge")); 1258 dst_object->cred = curthread->td_ucred; 1259 crhold(dst_object->cred); 1260 *fork_charge += dst_object->charge; 1261 } else { 1262 dst_object->cred = dst_entry->cred; 1263 dst_entry->cred = NULL; 1264 } 1265 access = prot = dst_entry->protection; 1266 /* 1267 * If not an upgrade, then enter the mappings in the pmap as 1268 * read and/or execute accesses. Otherwise, enter them as 1269 * write accesses. 1270 * 1271 * A writeable large page mapping is only created if all of 1272 * the constituent small page mappings are modified. Marking 1273 * PTEs as modified on inception allows promotion to happen 1274 * without taking potentially large number of soft faults. 1275 */ 1276 if (!upgrade) 1277 access &= ~VM_PROT_WRITE; 1278 1279 /* 1280 * Loop through all of the pages in the entry's range, copying each 1281 * one from the source object (it should be there) to the destination 1282 * object. 1283 */ 1284 for (vaddr = dst_entry->start, dst_pindex = 0; 1285 vaddr < dst_entry->end; 1286 vaddr += PAGE_SIZE, dst_pindex++) { 1287 1288 /* 1289 * Allocate a page in the destination object. 1290 */ 1291 do { 1292 dst_m = vm_page_alloc(dst_object, dst_pindex, 1293 VM_ALLOC_NORMAL); 1294 if (dst_m == NULL) { 1295 VM_OBJECT_UNLOCK(dst_object); 1296 VM_WAIT; 1297 VM_OBJECT_LOCK(dst_object); 1298 } 1299 } while (dst_m == NULL); 1300 1301 /* 1302 * Find the page in the source object, and copy it in. 1303 * (Because the source is wired down, the page will be in 1304 * memory.) 1305 */ 1306 VM_OBJECT_LOCK(src_object); 1307 object = src_object; 1308 pindex = src_pindex + dst_pindex; 1309 while ((src_m = vm_page_lookup(object, pindex)) == NULL && 1310 src_readonly && 1311 (backing_object = object->backing_object) != NULL) { 1312 /* 1313 * Allow fallback to backing objects if we are reading. 1314 */ 1315 VM_OBJECT_LOCK(backing_object); 1316 pindex += OFF_TO_IDX(object->backing_object_offset); 1317 VM_OBJECT_UNLOCK(object); 1318 object = backing_object; 1319 } 1320 if (src_m == NULL) 1321 panic("vm_fault_copy_wired: page missing"); 1322 pmap_copy_page(src_m, dst_m); 1323 VM_OBJECT_UNLOCK(object); 1324 dst_m->valid = VM_PAGE_BITS_ALL; 1325 VM_OBJECT_UNLOCK(dst_object); 1326 1327 /* 1328 * Enter it in the pmap. If a wired, copy-on-write 1329 * mapping is being replaced by a write-enabled 1330 * mapping, then wire that new mapping. 1331 */ 1332 pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade); 1333 1334 /* 1335 * Mark it no longer busy, and put it on the active list. 1336 */ 1337 VM_OBJECT_LOCK(dst_object); 1338 1339 if (upgrade) { 1340 vm_page_lock(src_m); 1341 vm_page_unwire(src_m, 0); 1342 vm_page_unlock(src_m); 1343 1344 vm_page_lock(dst_m); 1345 vm_page_wire(dst_m); 1346 vm_page_unlock(dst_m); 1347 } else { 1348 vm_page_lock(dst_m); 1349 vm_page_activate(dst_m); 1350 vm_page_unlock(dst_m); 1351 } 1352 vm_page_wakeup(dst_m); 1353 } 1354 VM_OBJECT_UNLOCK(dst_object); 1355 if (upgrade) { 1356 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY); 1357 vm_object_deallocate(src_object); 1358 } 1359 } 1360 1361 1362 /* 1363 * This routine checks around the requested page for other pages that 1364 * might be able to be faulted in. This routine brackets the viable 1365 * pages for the pages to be paged in. 1366 * 1367 * Inputs: 1368 * m, rbehind, rahead 1369 * 1370 * Outputs: 1371 * marray (array of vm_page_t), reqpage (index of requested page) 1372 * 1373 * Return value: 1374 * number of pages in marray 1375 */ 1376 static int 1377 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage) 1378 vm_page_t m; 1379 int rbehind; 1380 int rahead; 1381 vm_page_t *marray; 1382 int *reqpage; 1383 { 1384 int i,j; 1385 vm_object_t object; 1386 vm_pindex_t pindex, startpindex, endpindex, tpindex; 1387 vm_page_t rtm; 1388 int cbehind, cahead; 1389 1390 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1391 1392 object = m->object; 1393 pindex = m->pindex; 1394 cbehind = cahead = 0; 1395 1396 /* 1397 * if the requested page is not available, then give up now 1398 */ 1399 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) { 1400 return 0; 1401 } 1402 1403 if ((cbehind == 0) && (cahead == 0)) { 1404 *reqpage = 0; 1405 marray[0] = m; 1406 return 1; 1407 } 1408 1409 if (rahead > cahead) { 1410 rahead = cahead; 1411 } 1412 1413 if (rbehind > cbehind) { 1414 rbehind = cbehind; 1415 } 1416 1417 /* 1418 * scan backward for the read behind pages -- in memory 1419 */ 1420 if (pindex > 0) { 1421 if (rbehind > pindex) { 1422 rbehind = pindex; 1423 startpindex = 0; 1424 } else { 1425 startpindex = pindex - rbehind; 1426 } 1427 1428 if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL && 1429 rtm->pindex >= startpindex) 1430 startpindex = rtm->pindex + 1; 1431 1432 /* tpindex is unsigned; beware of numeric underflow. */ 1433 for (i = 0, tpindex = pindex - 1; tpindex >= startpindex && 1434 tpindex < pindex; i++, tpindex--) { 1435 1436 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL | 1437 VM_ALLOC_IFNOTCACHED); 1438 if (rtm == NULL) { 1439 /* 1440 * Shift the allocated pages to the 1441 * beginning of the array. 1442 */ 1443 for (j = 0; j < i; j++) { 1444 marray[j] = marray[j + tpindex + 1 - 1445 startpindex]; 1446 } 1447 break; 1448 } 1449 1450 marray[tpindex - startpindex] = rtm; 1451 } 1452 } else { 1453 startpindex = 0; 1454 i = 0; 1455 } 1456 1457 marray[i] = m; 1458 /* page offset of the required page */ 1459 *reqpage = i; 1460 1461 tpindex = pindex + 1; 1462 i++; 1463 1464 /* 1465 * scan forward for the read ahead pages 1466 */ 1467 endpindex = tpindex + rahead; 1468 if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex) 1469 endpindex = rtm->pindex; 1470 if (endpindex > object->size) 1471 endpindex = object->size; 1472 1473 for (; tpindex < endpindex; i++, tpindex++) { 1474 1475 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL | 1476 VM_ALLOC_IFNOTCACHED); 1477 if (rtm == NULL) { 1478 break; 1479 } 1480 1481 marray[i] = rtm; 1482 } 1483 1484 /* return number of pages */ 1485 return i; 1486 } 1487 1488 /* 1489 * Block entry into the machine-independent layer's page fault handler by 1490 * the calling thread. Subsequent calls to vm_fault() by that thread will 1491 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of 1492 * spurious page faults. 1493 */ 1494 int 1495 vm_fault_disable_pagefaults(void) 1496 { 1497 1498 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR)); 1499 } 1500 1501 void 1502 vm_fault_enable_pagefaults(int save) 1503 { 1504 1505 curthread_pflags_restore(save); 1506 } 1507