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