The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/vm_fault.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 46bd2cb372375fc112de2322af9130a8


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.