kernel - Correct null pointer panic in debug code
[dragonfly.git] / sys / vm / vm_fault.c
1 /*
2  * Copyright (c) 2003-2014 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * ---
35  *
36  * Copyright (c) 1991, 1993
37  *      The Regents of the University of California.  All rights reserved.
38  * Copyright (c) 1994 John S. Dyson
39  * All rights reserved.
40  * Copyright (c) 1994 David Greenman
41  * All rights reserved.
42  *
43  *
44  * This code is derived from software contributed to Berkeley by
45  * The Mach Operating System project at Carnegie-Mellon University.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  * ---
72  *
73  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
74  * All rights reserved.
75  *
76  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
77  *
78  * Permission to use, copy, modify and distribute this software and
79  * its documentation is hereby granted, provided that both the copyright
80  * notice and this permission notice appear in all copies of the
81  * software, derivative works or modified versions, and any portions
82  * thereof, and that both notices appear in supporting documentation.
83  *
84  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
85  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
86  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
87  *
88  * Carnegie Mellon requests users of this software to return to
89  *
90  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
91  *  School of Computer Science
92  *  Carnegie Mellon University
93  *  Pittsburgh PA 15213-3890
94  *
95  * any improvements or extensions that they make and grant Carnegie the
96  * rights to redistribute these changes.
97  */
98
99 /*
100  *      Page fault handling module.
101  */
102
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/proc.h>
107 #include <sys/vnode.h>
108 #include <sys/resourcevar.h>
109 #include <sys/vmmeter.h>
110 #include <sys/vkernel.h>
111 #include <sys/lock.h>
112 #include <sys/sysctl.h>
113
114 #include <cpu/lwbuf.h>
115
116 #include <vm/vm.h>
117 #include <vm/vm_param.h>
118 #include <vm/pmap.h>
119 #include <vm/vm_map.h>
120 #include <vm/vm_object.h>
121 #include <vm/vm_page.h>
122 #include <vm/vm_pageout.h>
123 #include <vm/vm_kern.h>
124 #include <vm/vm_pager.h>
125 #include <vm/vnode_pager.h>
126 #include <vm/vm_extern.h>
127
128 #include <vm/vm_page2.h>
129
130 struct faultstate {
131         vm_page_t m;
132         vm_object_t object;
133         vm_pindex_t pindex;
134         vm_prot_t prot;
135         vm_page_t first_m;
136         vm_object_t first_object;
137         vm_prot_t first_prot;
138         vm_map_t map;
139         vm_map_entry_t entry;
140         int lookup_still_valid;
141         int hardfault;
142         int fault_flags;
143         int map_generation;
144         int shared;
145         int first_shared;
146         int wflags;
147         struct vnode *vp;
148 };
149
150 static int debug_fault = 0;
151 SYSCTL_INT(_vm, OID_AUTO, debug_fault, CTLFLAG_RW, &debug_fault, 0, "");
152 static int debug_cluster = 0;
153 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
154 static int virtual_copy_enable = 1;
155 SYSCTL_INT(_vm, OID_AUTO, virtual_copy_enable, CTLFLAG_RW,
156                 &virtual_copy_enable, 0, "");
157 int vm_shared_fault = 1;
158 TUNABLE_INT("vm.shared_fault", &vm_shared_fault);
159 SYSCTL_INT(_vm, OID_AUTO, shared_fault, CTLFLAG_RW,
160                 &vm_shared_fault, 0, "Allow shared token on vm_object");
161
162 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t, int);
163 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *,
164                         vpte_t, int, int);
165 #if 0
166 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
167 #endif
168 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry);
169 static void vm_prefault(pmap_t pmap, vm_offset_t addra,
170                         vm_map_entry_t entry, int prot, int fault_flags);
171 static void vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
172                         vm_map_entry_t entry, int prot, int fault_flags);
173
174 static __inline void
175 release_page(struct faultstate *fs)
176 {
177         vm_page_deactivate(fs->m);
178         vm_page_wakeup(fs->m);
179         fs->m = NULL;
180 }
181
182 /*
183  * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
184  *       requires relocking and then checking the timestamp.
185  *
186  * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
187  *       not have to update fs->map_generation here.
188  *
189  * NOTE: This function can fail due to a deadlock against the caller's
190  *       holding of a vm_page BUSY.
191  */
192 static __inline int
193 relock_map(struct faultstate *fs)
194 {
195         int error;
196
197         if (fs->lookup_still_valid == FALSE && fs->map) {
198                 error = vm_map_lock_read_to(fs->map);
199                 if (error == 0)
200                         fs->lookup_still_valid = TRUE;
201         } else {
202                 error = 0;
203         }
204         return error;
205 }
206
207 static __inline void
208 unlock_map(struct faultstate *fs)
209 {
210         if (fs->lookup_still_valid && fs->map) {
211                 vm_map_lookup_done(fs->map, fs->entry, 0);
212                 fs->lookup_still_valid = FALSE;
213         }
214 }
215
216 /*
217  * Clean up after a successful call to vm_fault_object() so another call
218  * to vm_fault_object() can be made.
219  */
220 static void
221 _cleanup_successful_fault(struct faultstate *fs, int relock)
222 {
223         /*
224          * We allocated a junk page for a COW operation that did
225          * not occur, the page must be freed.
226          */
227         if (fs->object != fs->first_object) {
228                 KKASSERT(fs->first_shared == 0);
229                 vm_page_free(fs->first_m);
230                 vm_object_pip_wakeup(fs->object);
231                 fs->first_m = NULL;
232         }
233
234         /*
235          * Reset fs->object.
236          */
237         fs->object = fs->first_object;
238         if (relock && fs->lookup_still_valid == FALSE) {
239                 if (fs->map)
240                         vm_map_lock_read(fs->map);
241                 fs->lookup_still_valid = TRUE;
242         }
243 }
244
245 static void
246 _unlock_things(struct faultstate *fs, int dealloc)
247 {
248         _cleanup_successful_fault(fs, 0);
249         if (dealloc) {
250                 /*vm_object_deallocate(fs->first_object);*/
251                 /*fs->first_object = NULL; drop used later on */
252         }
253         unlock_map(fs); 
254         if (fs->vp != NULL) { 
255                 vput(fs->vp);
256                 fs->vp = NULL;
257         }
258 }
259
260 #define unlock_things(fs) _unlock_things(fs, 0)
261 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
262 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
263
264 /*
265  * Virtual copy tests.   Used by the fault code to determine if a
266  * page can be moved from an orphan vm_object into its shadow
267  * instead of copying its contents.
268  */
269 static __inline int
270 virtual_copy_test(struct faultstate *fs)
271 {
272         /*
273          * Must be holding exclusive locks
274          */
275         if (fs->first_shared || fs->shared || virtual_copy_enable == 0)
276                 return 0;
277
278         /*
279          * Map, if present, has not changed
280          */
281         if (fs->map && fs->map_generation != fs->map->timestamp)
282                 return 0;
283
284         /*
285          * Only one shadow object
286          */
287         if (fs->object->shadow_count != 1)
288                 return 0;
289
290         /*
291          * No COW refs, except us
292          */
293         if (fs->object->ref_count != 1)
294                 return 0;
295
296         /*
297          * No one else can look this object up
298          */
299         if (fs->object->handle != NULL)
300                 return 0;
301
302         /*
303          * No other ways to look the object up
304          */
305         if (fs->object->type != OBJT_DEFAULT &&
306             fs->object->type != OBJT_SWAP)
307                 return 0;
308
309         /*
310          * We don't chase down the shadow chain
311          */
312         if (fs->object != fs->first_object->backing_object)
313                 return 0;
314
315         return 1;
316 }
317
318 static __inline int
319 virtual_copy_ok(struct faultstate *fs)
320 {
321         if (virtual_copy_test(fs)) {
322                 /*
323                  * Grab the lock and re-test changeable items.
324                  */
325                 if (fs->lookup_still_valid == FALSE && fs->map) {
326                         if (lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT))
327                                 return 0;
328                         fs->lookup_still_valid = TRUE;
329                         if (virtual_copy_test(fs)) {
330                                 fs->map_generation = ++fs->map->timestamp;
331                                 return 1;
332                         }
333                         fs->lookup_still_valid = FALSE;
334                         lockmgr(&fs->map->lock, LK_RELEASE);
335                 }
336         }
337         return 0;
338 }
339
340 /*
341  * TRYPAGER 
342  *
343  * Determine if the pager for the current object *might* contain the page.
344  *
345  * We only need to try the pager if this is not a default object (default
346  * objects are zero-fill and have no real pager), and if we are not taking
347  * a wiring fault or if the FS entry is wired.
348  */
349 #define TRYPAGER(fs)    \
350                 (fs->object->type != OBJT_DEFAULT &&                    \
351                 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) ||       \
352                  (fs->wflags & FW_WIRED)))
353
354 /*
355  * vm_fault:
356  *
357  * Handle a page fault occuring at the given address, requiring the given
358  * permissions, in the map specified.  If successful, the page is inserted
359  * into the associated physical map.
360  *
361  * NOTE: The given address should be truncated to the proper page address.
362  *
363  * KERN_SUCCESS is returned if the page fault is handled; otherwise,
364  * a standard error specifying why the fault is fatal is returned.
365  *
366  * The map in question must be referenced, and remains so.
367  * The caller may hold no locks.
368  * No other requirements.
369  */
370 int
371 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
372 {
373         int result;
374         vm_pindex_t first_pindex;
375         struct faultstate fs;
376         struct lwp *lp;
377         struct proc *p;
378         thread_t td;
379         struct vm_map_ilock ilock;
380         int didilock;
381         int growstack;
382         int retry = 0;
383         int inherit_prot;
384
385         inherit_prot = fault_type & VM_PROT_NOSYNC;
386         fs.hardfault = 0;
387         fs.fault_flags = fault_flags;
388         fs.vp = NULL;
389         fs.shared = vm_shared_fault;
390         fs.first_shared = vm_shared_fault;
391         growstack = 1;
392
393         /*
394          * vm_map interactions
395          */
396         td = curthread;
397         if ((lp = td->td_lwp) != NULL)
398                 lp->lwp_flags |= LWP_PAGING;
399
400 RetryFault:
401         /*
402          * Find the vm_map_entry representing the backing store and resolve
403          * the top level object and page index.  This may have the side
404          * effect of executing a copy-on-write on the map entry,
405          * creating a shadow object, or splitting an anonymous entry for
406          * performance, but will not COW any actual VM pages.
407          *
408          * On success fs.map is left read-locked and various other fields 
409          * are initialized but not otherwise referenced or locked.
410          *
411          * NOTE!  vm_map_lookup will try to upgrade the fault_type to
412          *        VM_FAULT_WRITE if the map entry is a virtual page table
413          *        and also writable, so we can set the 'A'accessed bit in
414          *        the virtual page table entry.
415          */
416         fs.map = map;
417         result = vm_map_lookup(&fs.map, vaddr, fault_type,
418                                &fs.entry, &fs.first_object,
419                                &first_pindex, &fs.first_prot, &fs.wflags);
420
421         /*
422          * If the lookup failed or the map protections are incompatible,
423          * the fault generally fails.
424          *
425          * The failure could be due to TDF_NOFAULT if vm_map_lookup()
426          * tried to do a COW fault.
427          *
428          * If the caller is trying to do a user wiring we have more work
429          * to do.
430          */
431         if (result != KERN_SUCCESS) {
432                 if (result == KERN_FAILURE_NOFAULT) {
433                         result = KERN_FAILURE;
434                         goto done;
435                 }
436                 if (result != KERN_PROTECTION_FAILURE ||
437                     (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
438                 {
439                         if (result == KERN_INVALID_ADDRESS && growstack &&
440                             map != &kernel_map && curproc != NULL) {
441                                 result = vm_map_growstack(map, vaddr);
442                                 if (result == KERN_SUCCESS) {
443                                         growstack = 0;
444                                         ++retry;
445                                         goto RetryFault;
446                                 }
447                                 result = KERN_FAILURE;
448                         }
449                         goto done;
450                 }
451
452                 /*
453                  * If we are user-wiring a r/w segment, and it is COW, then
454                  * we need to do the COW operation.  Note that we don't
455                  * currently COW RO sections now, because it is NOT desirable
456                  * to COW .text.  We simply keep .text from ever being COW'ed
457                  * and take the heat that one cannot debug wired .text sections.
458                  *
459                  * XXX Try to allow the above by specifying OVERRIDE_WRITE.
460                  */
461                 result = vm_map_lookup(&fs.map, vaddr,
462                                        VM_PROT_READ|VM_PROT_WRITE|
463                                         VM_PROT_OVERRIDE_WRITE,
464                                        &fs.entry, &fs.first_object,
465                                        &first_pindex, &fs.first_prot,
466                                        &fs.wflags);
467                 if (result != KERN_SUCCESS) {
468                         /* could also be KERN_FAILURE_NOFAULT */
469                         result = KERN_FAILURE;
470                         goto done;
471                 }
472
473                 /*
474                  * If we don't COW now, on a user wire, the user will never
475                  * be able to write to the mapping.  If we don't make this
476                  * restriction, the bookkeeping would be nearly impossible.
477                  *
478                  * XXX We have a shared lock, this will have a MP race but
479                  * I don't see how it can hurt anything.
480                  */
481                 if ((fs.entry->protection & VM_PROT_WRITE) == 0) {
482                         atomic_clear_char(&fs.entry->max_protection,
483                                           VM_PROT_WRITE);
484                 }
485         }
486
487         /*
488          * fs.map is read-locked
489          *
490          * Misc checks.  Save the map generation number to detect races.
491          */
492         fs.map_generation = fs.map->timestamp;
493         fs.lookup_still_valid = TRUE;
494         fs.first_m = NULL;
495         fs.object = fs.first_object;    /* so unlock_and_deallocate works */
496         fs.prot = fs.first_prot;        /* default (used by uksmap) */
497
498         if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
499                 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
500                         panic("vm_fault: fault on nofault entry, addr: %p",
501                               (void *)vaddr);
502                 }
503                 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
504                     vaddr >= fs.entry->start &&
505                     vaddr < fs.entry->start + PAGE_SIZE) {
506                         panic("vm_fault: fault on stack guard, addr: %p",
507                               (void *)vaddr);
508                 }
509         }
510
511         /*
512          * A user-kernel shared map has no VM object and bypasses
513          * everything.  We execute the uksmap function with a temporary
514          * fictitious vm_page.  The address is directly mapped with no
515          * management.
516          */
517         if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
518                 struct vm_page fakem;
519
520                 bzero(&fakem, sizeof(fakem));
521                 fakem.pindex = first_pindex;
522                 fakem.flags = PG_FICTITIOUS | PG_UNMANAGED;
523                 fakem.busy_count = PBUSY_LOCKED;
524                 fakem.valid = VM_PAGE_BITS_ALL;
525                 fakem.pat_mode = VM_MEMATTR_DEFAULT;
526                 if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
527                         result = KERN_FAILURE;
528                         unlock_things(&fs);
529                         goto done2;
530                 }
531                 pmap_enter(fs.map->pmap, vaddr, &fakem, fs.prot | inherit_prot,
532                            (fs.wflags & FW_WIRED), fs.entry);
533                 goto done_success;
534         }
535
536         /*
537          * A system map entry may return a NULL object.  No object means
538          * no pager means an unrecoverable kernel fault.
539          */
540         if (fs.first_object == NULL) {
541                 panic("vm_fault: unrecoverable fault at %p in entry %p",
542                         (void *)vaddr, fs.entry);
543         }
544
545         /*
546          * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
547          * is set.
548          *
549          * Unfortunately a deadlock can occur if we are forced to page-in
550          * from swap, but diving all the way into the vm_pager_get_page()
551          * function to find out is too much.  Just check the object type.
552          *
553          * The deadlock is a CAM deadlock on a busy VM page when trying
554          * to finish an I/O if another process gets stuck in
555          * vop_helper_read_shortcut() due to a swap fault.
556          */
557         if ((td->td_flags & TDF_NOFAULT) &&
558             (retry ||
559              fs.first_object->type == OBJT_VNODE ||
560              fs.first_object->type == OBJT_SWAP ||
561              fs.first_object->backing_object)) {
562                 result = KERN_FAILURE;
563                 unlock_things(&fs);
564                 goto done2;
565         }
566
567         /*
568          * If the entry is wired we cannot change the page protection.
569          */
570         if (fs.wflags & FW_WIRED)
571                 fault_type = fs.first_prot;
572
573         /*
574          * We generally want to avoid unnecessary exclusive modes on backing
575          * and terminal objects because this can seriously interfere with
576          * heavily fork()'d processes (particularly /bin/sh scripts).
577          *
578          * However, we also want to avoid unnecessary retries due to needed
579          * shared->exclusive promotion for common faults.  Exclusive mode is
580          * always needed if any page insertion, rename, or free occurs in an
581          * object (and also indirectly if any I/O is done).
582          *
583          * The main issue here is going to be fs.first_shared.  If the
584          * first_object has a backing object which isn't shadowed and the
585          * process is single-threaded we might as well use an exclusive
586          * lock/chain right off the bat.
587          */
588         if (fs.first_shared && fs.first_object->backing_object &&
589             LIST_EMPTY(&fs.first_object->shadow_head) &&
590             td->td_proc && td->td_proc->p_nthreads == 1) {
591                 fs.first_shared = 0;
592         }
593
594         /*
595          * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
596          * VM_FAULT_DIRTY  - may require swap_pager_unswapped() later, but
597          *                   we can try shared first.
598          */
599         if (fault_flags & VM_FAULT_UNSWAP) {
600                 fs.first_shared = 0;
601         }
602
603         /*
604          * Obtain a top-level object lock, shared or exclusive depending
605          * on fs.first_shared.  If a shared lock winds up being insufficient
606          * we will retry with an exclusive lock.
607          *
608          * The vnode pager lock is always shared.
609          */
610         if (fs.first_shared)
611                 vm_object_hold_shared(fs.first_object);
612         else
613                 vm_object_hold(fs.first_object);
614         if (fs.vp == NULL)
615                 fs.vp = vnode_pager_lock(fs.first_object);
616
617         /*
618          * The page we want is at (first_object, first_pindex), but if the
619          * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
620          * page table to figure out the actual pindex.
621          *
622          * NOTE!  DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
623          * ONLY
624          */
625         didilock = 0;
626         if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
627                 vm_map_interlock(fs.map, &ilock, vaddr, vaddr + PAGE_SIZE);
628                 didilock = 1;
629                 result = vm_fault_vpagetable(&fs, &first_pindex,
630                                              fs.entry->aux.master_pde,
631                                              fault_type, 1);
632                 if (result == KERN_TRY_AGAIN) {
633                         vm_map_deinterlock(fs.map, &ilock);
634                         vm_object_drop(fs.first_object);
635                         ++retry;
636                         goto RetryFault;
637                 }
638                 if (result != KERN_SUCCESS) {
639                         vm_map_deinterlock(fs.map, &ilock);
640                         goto done;
641                 }
642         }
643
644         /*
645          * Now we have the actual (object, pindex), fault in the page.  If
646          * vm_fault_object() fails it will unlock and deallocate the FS
647          * data.   If it succeeds everything remains locked and fs->object
648          * will have an additional PIP count if it is not equal to
649          * fs->first_object
650          *
651          * vm_fault_object will set fs->prot for the pmap operation.  It is
652          * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
653          * page can be safely written.  However, it will force a read-only
654          * mapping for a read fault if the memory is managed by a virtual
655          * page table.
656          *
657          * If the fault code uses the shared object lock shortcut
658          * we must not try to burst (we can't allocate VM pages).
659          */
660         result = vm_fault_object(&fs, first_pindex, fault_type, 1);
661
662         if (debug_fault > 0) {
663                 --debug_fault;
664                 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
665                         "fs.m=%p fs.prot=%02x fs.wflags=%02x fs.entry=%p\n",
666                         result, (intmax_t)vaddr, fault_type, fault_flags,
667                         fs.m, fs.prot, fs.wflags, fs.entry);
668         }
669
670         if (result == KERN_TRY_AGAIN) {
671                 if (didilock)
672                         vm_map_deinterlock(fs.map, &ilock);
673                 vm_object_drop(fs.first_object);
674                 ++retry;
675                 goto RetryFault;
676         }
677         if (result != KERN_SUCCESS) {
678                 if (didilock)
679                         vm_map_deinterlock(fs.map, &ilock);
680                 goto done;
681         }
682
683         /*
684          * On success vm_fault_object() does not unlock or deallocate, and fs.m
685          * will contain a busied page.
686          *
687          * Enter the page into the pmap and do pmap-related adjustments.
688          */
689         KKASSERT(fs.lookup_still_valid == TRUE);
690         vm_page_flag_set(fs.m, PG_REFERENCED);
691         pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot | inherit_prot,
692                    fs.wflags & FW_WIRED, fs.entry);
693
694         if (didilock)
695                 vm_map_deinterlock(fs.map, &ilock);
696
697         /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
698         KKASSERT(fs.m->busy_count & PBUSY_LOCKED);
699
700         /*
701          * If the page is not wired down, then put it where the pageout daemon
702          * can find it.
703          */
704         if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
705                 if (fs.wflags & FW_WIRED)
706                         vm_page_wire(fs.m);
707                 else
708                         vm_page_unwire(fs.m, 1);
709         } else {
710                 vm_page_activate(fs.m);
711         }
712         vm_page_wakeup(fs.m);
713
714         /*
715          * Burst in a few more pages if possible.  The fs.map should still
716          * be locked.  To avoid interlocking against a vnode->getblk
717          * operation we had to be sure to unbusy our primary vm_page above
718          * first.
719          *
720          * A normal burst can continue down backing store, only execute
721          * if we are holding an exclusive lock, otherwise the exclusive
722          * locks the burst code gets might cause excessive SMP collisions.
723          *
724          * A quick burst can be utilized when there is no backing object
725          * (i.e. a shared file mmap).
726          */
727         if ((fault_flags & VM_FAULT_BURST) &&
728             (fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
729             (fs.wflags & FW_WIRED) == 0) {
730                 if (fs.first_shared == 0 && fs.shared == 0) {
731                         vm_prefault(fs.map->pmap, vaddr,
732                                     fs.entry, fs.prot, fault_flags);
733                 } else {
734                         vm_prefault_quick(fs.map->pmap, vaddr,
735                                           fs.entry, fs.prot, fault_flags);
736                 }
737         }
738
739 done_success:
740         mycpu->gd_cnt.v_vm_faults++;
741         if (td->td_lwp)
742                 ++td->td_lwp->lwp_ru.ru_minflt;
743
744         /*
745          * Unlock everything, and return
746          */
747         unlock_things(&fs);
748
749         if (td->td_lwp) {
750                 if (fs.hardfault) {
751                         td->td_lwp->lwp_ru.ru_majflt++;
752                 } else {
753                         td->td_lwp->lwp_ru.ru_minflt++;
754                 }
755         }
756
757         /*vm_object_deallocate(fs.first_object);*/
758         /*fs.m = NULL; */
759         /*fs.first_object = NULL; must still drop later */
760
761         result = KERN_SUCCESS;
762 done:
763         if (fs.first_object)
764                 vm_object_drop(fs.first_object);
765 done2:
766         if (lp)
767                 lp->lwp_flags &= ~LWP_PAGING;
768
769 #if !defined(NO_SWAPPING)
770         /*
771          * Check the process RSS limit and force deactivation and
772          * (asynchronous) paging if necessary.  This is a complex operation,
773          * only do it for direct user-mode faults, for now.
774          *
775          * To reduce overhead implement approximately a ~16MB hysteresis.
776          */
777         p = td->td_proc;
778         if ((fault_flags & VM_FAULT_USERMODE) && lp &&
779             p->p_limit && map->pmap && vm_pageout_memuse_mode >= 1 &&
780             map != &kernel_map) {
781                 vm_pindex_t limit;
782                 vm_pindex_t size;
783
784                 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
785                                         p->p_rlimit[RLIMIT_RSS].rlim_max));
786                 size = pmap_resident_tlnw_count(map->pmap);
787                 if (limit >= 0 && size > 4096 && size - 4096 >= limit) {
788                         vm_pageout_map_deactivate_pages(map, limit);
789                 }
790         }
791 #endif
792
793         return (result);
794 }
795
796 /*
797  * Fault in the specified virtual address in the current process map, 
798  * returning a held VM page or NULL.  See vm_fault_page() for more 
799  * information.
800  *
801  * No requirements.
802  */
803 vm_page_t
804 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type,
805                     int *errorp, int *busyp)
806 {
807         struct lwp *lp = curthread->td_lwp;
808         vm_page_t m;
809
810         m = vm_fault_page(&lp->lwp_vmspace->vm_map, va, 
811                           fault_type, VM_FAULT_NORMAL,
812                           errorp, busyp);
813         return(m);
814 }
815
816 /*
817  * Fault in the specified virtual address in the specified map, doing all
818  * necessary manipulation of the object store and all necessary I/O.  Return
819  * a held VM page or NULL, and set *errorp.  The related pmap is not
820  * updated.
821  *
822  * If busyp is not NULL then *busyp will be set to TRUE if this routine
823  * decides to return a busied page (aka VM_PROT_WRITE), or FALSE if it
824  * does not (VM_PROT_WRITE not specified or busyp is NULL).  If busyp is
825  * NULL the returned page is only held.
826  *
827  * If the caller has no intention of writing to the page's contents, busyp
828  * can be passed as NULL along with VM_PROT_WRITE to force a COW operation
829  * without busying the page.
830  *
831  * The returned page will also be marked PG_REFERENCED.
832  *
833  * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
834  * error will be returned.
835  *
836  * No requirements.
837  */
838 vm_page_t
839 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
840               int fault_flags, int *errorp, int *busyp)
841 {
842         vm_pindex_t first_pindex;
843         struct faultstate fs;
844         int result;
845         int retry;
846         int growstack;
847         int didcow;
848         vm_prot_t orig_fault_type = fault_type;
849
850         retry = 0;
851         didcow = 0;
852         fs.hardfault = 0;
853         fs.fault_flags = fault_flags;
854         KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
855
856         /*
857          * Dive the pmap (concurrency possible).  If we find the
858          * appropriate page we can terminate early and quickly.
859          *
860          * This works great for normal programs but will always return
861          * NULL for host lookups of vkernel maps in VMM mode.
862          *
863          * NOTE: pmap_fault_page_quick() might not busy the page.  If
864          *       VM_PROT_WRITE is set in fault_type and pmap_fault_page_quick()
865          *       returns non-NULL, it will safely dirty the returned vm_page_t
866          *       for us.  We cannot safely dirty it here (it might not be
867          *       busy).
868          */
869         fs.m = pmap_fault_page_quick(map->pmap, vaddr, fault_type, busyp);
870         if (fs.m) {
871                 *errorp = 0;
872                 return(fs.m);
873         }
874
875         /*
876          * Otherwise take a concurrency hit and do a formal page
877          * fault.
878          */
879         fs.vp = NULL;
880         fs.shared = vm_shared_fault;
881         fs.first_shared = vm_shared_fault;
882         growstack = 1;
883
884         /*
885          * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
886          * VM_FAULT_DIRTY  - may require swap_pager_unswapped() later, but
887          *                   we can try shared first.
888          */
889         if (fault_flags & VM_FAULT_UNSWAP) {
890                 fs.first_shared = 0;
891         }
892
893 RetryFault:
894         /*
895          * Find the vm_map_entry representing the backing store and resolve
896          * the top level object and page index.  This may have the side
897          * effect of executing a copy-on-write on the map entry and/or
898          * creating a shadow object, but will not COW any actual VM pages.
899          *
900          * On success fs.map is left read-locked and various other fields 
901          * are initialized but not otherwise referenced or locked.
902          *
903          * NOTE!  vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
904          *        if the map entry is a virtual page table and also writable,
905          *        so we can set the 'A'accessed bit in the virtual page table
906          *        entry.
907          */
908         fs.map = map;
909         result = vm_map_lookup(&fs.map, vaddr, fault_type,
910                                &fs.entry, &fs.first_object,
911                                &first_pindex, &fs.first_prot, &fs.wflags);
912
913         if (result != KERN_SUCCESS) {
914                 if (result == KERN_FAILURE_NOFAULT) {
915                         *errorp = KERN_FAILURE;
916                         fs.m = NULL;
917                         goto done;
918                 }
919                 if (result != KERN_PROTECTION_FAILURE ||
920                     (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
921                 {
922                         if (result == KERN_INVALID_ADDRESS && growstack &&
923                             map != &kernel_map && curproc != NULL) {
924                                 result = vm_map_growstack(map, vaddr);
925                                 if (result == KERN_SUCCESS) {
926                                         growstack = 0;
927                                         ++retry;
928                                         goto RetryFault;
929                                 }
930                                 result = KERN_FAILURE;
931                         }
932                         fs.m = NULL;
933                         *errorp = result;
934                         goto done;
935                 }
936
937                 /*
938                  * If we are user-wiring a r/w segment, and it is COW, then
939                  * we need to do the COW operation.  Note that we don't
940                  * currently COW RO sections now, because it is NOT desirable
941                  * to COW .text.  We simply keep .text from ever being COW'ed
942                  * and take the heat that one cannot debug wired .text sections.
943                  */
944                 result = vm_map_lookup(&fs.map, vaddr,
945                                        VM_PROT_READ|VM_PROT_WRITE|
946                                         VM_PROT_OVERRIDE_WRITE,
947                                        &fs.entry, &fs.first_object,
948                                        &first_pindex, &fs.first_prot,
949                                        &fs.wflags);
950                 if (result != KERN_SUCCESS) {
951                         /* could also be KERN_FAILURE_NOFAULT */
952                         *errorp = KERN_FAILURE;
953                         fs.m = NULL;
954                         goto done;
955                 }
956
957                 /*
958                  * If we don't COW now, on a user wire, the user will never
959                  * be able to write to the mapping.  If we don't make this
960                  * restriction, the bookkeeping would be nearly impossible.
961                  *
962                  * XXX We have a shared lock, this will have a MP race but
963                  * I don't see how it can hurt anything.
964                  */
965                 if ((fs.entry->protection & VM_PROT_WRITE) == 0) {
966                         atomic_clear_char(&fs.entry->max_protection,
967                                           VM_PROT_WRITE);
968                 }
969         }
970
971         /*
972          * fs.map is read-locked
973          *
974          * Misc checks.  Save the map generation number to detect races.
975          */
976         fs.map_generation = fs.map->timestamp;
977         fs.lookup_still_valid = TRUE;
978         fs.first_m = NULL;
979         fs.object = fs.first_object;    /* so unlock_and_deallocate works */
980
981         if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
982                 panic("vm_fault: fault on nofault entry, addr: %lx",
983                     (u_long)vaddr);
984         }
985
986         /*
987          * A user-kernel shared map has no VM object and bypasses
988          * everything.  We execute the uksmap function with a temporary
989          * fictitious vm_page.  The address is directly mapped with no
990          * management.
991          */
992         if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
993                 struct vm_page fakem;
994
995                 bzero(&fakem, sizeof(fakem));
996                 fakem.pindex = first_pindex;
997                 fakem.flags = PG_FICTITIOUS | PG_UNMANAGED;
998                 fakem.busy_count = PBUSY_LOCKED;
999                 fakem.valid = VM_PAGE_BITS_ALL;
1000                 fakem.pat_mode = VM_MEMATTR_DEFAULT;
1001                 if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
1002                         *errorp = KERN_FAILURE;
1003                         fs.m = NULL;
1004                         unlock_things(&fs);
1005                         goto done2;
1006                 }
1007                 fs.m = PHYS_TO_VM_PAGE(fakem.phys_addr);
1008                 vm_page_hold(fs.m);
1009                 if (busyp)
1010                         *busyp = 0;     /* don't need to busy R or W */
1011                 unlock_things(&fs);
1012                 *errorp = 0;
1013                 goto done;
1014         }
1015
1016
1017         /*
1018          * A system map entry may return a NULL object.  No object means
1019          * no pager means an unrecoverable kernel fault.
1020          */
1021         if (fs.first_object == NULL) {
1022                 panic("vm_fault: unrecoverable fault at %p in entry %p",
1023                         (void *)vaddr, fs.entry);
1024         }
1025
1026         /*
1027          * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
1028          * is set.
1029          *
1030          * Unfortunately a deadlock can occur if we are forced to page-in
1031          * from swap, but diving all the way into the vm_pager_get_page()
1032          * function to find out is too much.  Just check the object type.
1033          */
1034         if ((curthread->td_flags & TDF_NOFAULT) &&
1035             (retry ||
1036              fs.first_object->type == OBJT_VNODE ||
1037              fs.first_object->type == OBJT_SWAP ||
1038              fs.first_object->backing_object)) {
1039                 *errorp = KERN_FAILURE;
1040                 unlock_things(&fs);
1041                 fs.m = NULL;
1042                 goto done2;
1043         }
1044
1045         /*
1046          * If the entry is wired we cannot change the page protection.
1047          */
1048         if (fs.wflags & FW_WIRED)
1049                 fault_type = fs.first_prot;
1050
1051         /*
1052          * Make a reference to this object to prevent its disposal while we
1053          * are messing with it.  Once we have the reference, the map is free
1054          * to be diddled.  Since objects reference their shadows (and copies),
1055          * they will stay around as well.
1056          *
1057          * The reference should also prevent an unexpected collapse of the
1058          * parent that might move pages from the current object into the
1059          * parent unexpectedly, resulting in corruption.
1060          *
1061          * Bump the paging-in-progress count to prevent size changes (e.g.
1062          * truncation operations) during I/O.  This must be done after
1063          * obtaining the vnode lock in order to avoid possible deadlocks.
1064          */
1065         if (fs.first_shared)
1066                 vm_object_hold_shared(fs.first_object);
1067         else
1068                 vm_object_hold(fs.first_object);
1069         if (fs.vp == NULL)
1070                 fs.vp = vnode_pager_lock(fs.first_object);      /* shared */
1071
1072         /*
1073          * The page we want is at (first_object, first_pindex), but if the
1074          * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
1075          * page table to figure out the actual pindex.
1076          *
1077          * NOTE!  DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
1078          * ONLY
1079          */
1080         if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1081                 result = vm_fault_vpagetable(&fs, &first_pindex,
1082                                              fs.entry->aux.master_pde,
1083                                              fault_type, 1);
1084                 if (result == KERN_TRY_AGAIN) {
1085                         vm_object_drop(fs.first_object);
1086                         ++retry;
1087                         goto RetryFault;
1088                 }
1089                 if (result != KERN_SUCCESS) {
1090                         *errorp = result;
1091                         fs.m = NULL;
1092                         goto done;
1093                 }
1094         }
1095
1096         /*
1097          * Now we have the actual (object, pindex), fault in the page.  If
1098          * vm_fault_object() fails it will unlock and deallocate the FS
1099          * data.   If it succeeds everything remains locked and fs->object
1100          * will have an additinal PIP count if it is not equal to
1101          * fs->first_object
1102          */
1103         fs.m = NULL;
1104         result = vm_fault_object(&fs, first_pindex, fault_type, 1);
1105
1106         if (result == KERN_TRY_AGAIN) {
1107                 vm_object_drop(fs.first_object);
1108                 ++retry;
1109                 didcow |= fs.wflags & FW_DIDCOW;
1110                 goto RetryFault;
1111         }
1112         if (result != KERN_SUCCESS) {
1113                 *errorp = result;
1114                 fs.m = NULL;
1115                 goto done;
1116         }
1117
1118         if ((orig_fault_type & VM_PROT_WRITE) &&
1119             (fs.prot & VM_PROT_WRITE) == 0) {
1120                 *errorp = KERN_PROTECTION_FAILURE;
1121                 unlock_and_deallocate(&fs);
1122                 fs.m = NULL;
1123                 goto done;
1124         }
1125
1126         /*
1127          * Generally speaking we don't want to update the pmap because
1128          * this routine can be called many times for situations that do
1129          * not require updating the pmap, not to mention the page might
1130          * already be in the pmap.
1131          *
1132          * However, if our vm_map_lookup() results in a COW, we need to
1133          * at least remove the pte from the pmap to guarantee proper
1134          * visibility of modifications made to the process.  For example,
1135          * modifications made by vkernel uiocopy/related routines and
1136          * modifications made by ptrace().
1137          */
1138         vm_page_flag_set(fs.m, PG_REFERENCED);
1139 #if 0
1140         pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot,
1141                    fs.wflags & FW_WIRED, NULL);
1142         mycpu->gd_cnt.v_vm_faults++;
1143         if (curthread->td_lwp)
1144                 ++curthread->td_lwp->lwp_ru.ru_minflt;
1145 #endif
1146         if ((fs.wflags | didcow) | FW_DIDCOW) {
1147                 pmap_remove(fs.map->pmap,
1148                             vaddr & ~PAGE_MASK,
1149                             (vaddr & ~PAGE_MASK) + PAGE_SIZE);
1150         }
1151
1152         /*
1153          * On success vm_fault_object() does not unlock or deallocate, and fs.m
1154          * will contain a busied page.  So we must unlock here after having
1155          * messed with the pmap.
1156          */
1157         unlock_things(&fs);
1158
1159         /*
1160          * Return a held page.  We are not doing any pmap manipulation so do
1161          * not set PG_MAPPED.  However, adjust the page flags according to
1162          * the fault type because the caller may not use a managed pmapping
1163          * (so we don't want to lose the fact that the page will be dirtied
1164          * if a write fault was specified).
1165          */
1166         if (fault_type & VM_PROT_WRITE)
1167                 vm_page_dirty(fs.m);
1168         vm_page_activate(fs.m);
1169
1170         if (curthread->td_lwp) {
1171                 if (fs.hardfault) {
1172                         curthread->td_lwp->lwp_ru.ru_majflt++;
1173                 } else {
1174                         curthread->td_lwp->lwp_ru.ru_minflt++;
1175                 }
1176         }
1177
1178         /*
1179          * Unlock everything, and return the held or busied page.
1180          */
1181         if (busyp) {
1182                 if (fault_type & VM_PROT_WRITE) {
1183                         vm_page_dirty(fs.m);
1184                         *busyp = 1;
1185                 } else {
1186                         *busyp = 0;
1187                         vm_page_hold(fs.m);
1188                         vm_page_wakeup(fs.m);
1189                 }
1190         } else {
1191                 vm_page_hold(fs.m);
1192                 vm_page_wakeup(fs.m);
1193         }
1194         /*vm_object_deallocate(fs.first_object);*/
1195         /*fs.first_object = NULL; */
1196         *errorp = 0;
1197
1198 done:
1199         if (fs.first_object)
1200                 vm_object_drop(fs.first_object);
1201 done2:
1202         return(fs.m);
1203 }
1204
1205 /*
1206  * Fault in the specified (object,offset), dirty the returned page as
1207  * needed.  If the requested fault_type cannot be done NULL and an
1208  * error is returned.
1209  *
1210  * A held (but not busied) page is returned.
1211  *
1212  * The passed in object must be held as specified by the shared
1213  * argument.
1214  */
1215 vm_page_t
1216 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
1217                      vm_prot_t fault_type, int fault_flags,
1218                      int *sharedp, int *errorp)
1219 {
1220         int result;
1221         vm_pindex_t first_pindex;
1222         struct faultstate fs;
1223         struct vm_map_entry entry;
1224
1225         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1226         bzero(&entry, sizeof(entry));
1227         entry.object.vm_object = object;
1228         entry.maptype = VM_MAPTYPE_NORMAL;
1229         entry.protection = entry.max_protection = fault_type;
1230
1231         fs.hardfault = 0;
1232         fs.fault_flags = fault_flags;
1233         fs.map = NULL;
1234         fs.shared = vm_shared_fault;
1235         fs.first_shared = *sharedp;
1236         fs.vp = NULL;
1237         KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
1238
1239         /*
1240          * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
1241          * VM_FAULT_DIRTY  - may require swap_pager_unswapped() later, but
1242          *                   we can try shared first.
1243          */
1244         if (fs.first_shared && (fault_flags & VM_FAULT_UNSWAP)) {
1245                 fs.first_shared = 0;
1246                 vm_object_upgrade(object);
1247         }
1248
1249         /*
1250          * Retry loop as needed (typically for shared->exclusive transitions)
1251          */
1252 RetryFault:
1253         *sharedp = fs.first_shared;
1254         first_pindex = OFF_TO_IDX(offset);
1255         fs.first_object = object;
1256         fs.entry = &entry;
1257         fs.first_prot = fault_type;
1258         fs.wflags = 0;
1259         /*fs.map_generation = 0; unused */
1260
1261         /*
1262          * Make a reference to this object to prevent its disposal while we
1263          * are messing with it.  Once we have the reference, the map is free
1264          * to be diddled.  Since objects reference their shadows (and copies),
1265          * they will stay around as well.
1266          *
1267          * The reference should also prevent an unexpected collapse of the
1268          * parent that might move pages from the current object into the
1269          * parent unexpectedly, resulting in corruption.
1270          *
1271          * Bump the paging-in-progress count to prevent size changes (e.g.
1272          * truncation operations) during I/O.  This must be done after
1273          * obtaining the vnode lock in order to avoid possible deadlocks.
1274          */
1275         if (fs.vp == NULL)
1276                 fs.vp = vnode_pager_lock(fs.first_object);
1277
1278         fs.lookup_still_valid = TRUE;
1279         fs.first_m = NULL;
1280         fs.object = fs.first_object;    /* so unlock_and_deallocate works */
1281
1282 #if 0
1283         /* XXX future - ability to operate on VM object using vpagetable */
1284         if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1285                 result = vm_fault_vpagetable(&fs, &first_pindex,
1286                                              fs.entry->aux.master_pde,
1287                                              fault_type, 0);
1288                 if (result == KERN_TRY_AGAIN) {
1289                         if (fs.first_shared == 0 && *sharedp)
1290                                 vm_object_upgrade(object);
1291                         goto RetryFault;
1292                 }
1293                 if (result != KERN_SUCCESS) {
1294                         *errorp = result;
1295                         return (NULL);
1296                 }
1297         }
1298 #endif
1299
1300         /*
1301          * Now we have the actual (object, pindex), fault in the page.  If
1302          * vm_fault_object() fails it will unlock and deallocate the FS
1303          * data.   If it succeeds everything remains locked and fs->object
1304          * will have an additinal PIP count if it is not equal to
1305          * fs->first_object
1306          *
1307          * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1308          * We may have to upgrade its lock to handle the requested fault.
1309          */
1310         result = vm_fault_object(&fs, first_pindex, fault_type, 0);
1311
1312         if (result == KERN_TRY_AGAIN) {
1313                 if (fs.first_shared == 0 && *sharedp)
1314                         vm_object_upgrade(object);
1315                 goto RetryFault;
1316         }
1317         if (result != KERN_SUCCESS) {
1318                 *errorp = result;
1319                 return(NULL);
1320         }
1321
1322         if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
1323                 *errorp = KERN_PROTECTION_FAILURE;
1324                 unlock_and_deallocate(&fs);
1325                 return(NULL);
1326         }
1327
1328         /*
1329          * On success vm_fault_object() does not unlock or deallocate, so we
1330          * do it here.  Note that the returned fs.m will be busied.
1331          */
1332         unlock_things(&fs);
1333
1334         /*
1335          * Return a held page.  We are not doing any pmap manipulation so do
1336          * not set PG_MAPPED.  However, adjust the page flags according to
1337          * the fault type because the caller may not use a managed pmapping
1338          * (so we don't want to lose the fact that the page will be dirtied
1339          * if a write fault was specified).
1340          */
1341         vm_page_hold(fs.m);
1342         vm_page_activate(fs.m);
1343         if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY))
1344                 vm_page_dirty(fs.m);
1345         if (fault_flags & VM_FAULT_UNSWAP)
1346                 swap_pager_unswapped(fs.m);
1347
1348         /*
1349          * Indicate that the page was accessed.
1350          */
1351         vm_page_flag_set(fs.m, PG_REFERENCED);
1352
1353         if (curthread->td_lwp) {
1354                 if (fs.hardfault) {
1355                         curthread->td_lwp->lwp_ru.ru_majflt++;
1356                 } else {
1357                         curthread->td_lwp->lwp_ru.ru_minflt++;
1358                 }
1359         }
1360
1361         /*
1362          * Unlock everything, and return the held page.
1363          */
1364         vm_page_wakeup(fs.m);
1365         /*vm_object_deallocate(fs.first_object);*/
1366         /*fs.first_object = NULL; */
1367
1368         *errorp = 0;
1369         return(fs.m);
1370 }
1371
1372 /*
1373  * Translate the virtual page number (first_pindex) that is relative
1374  * to the address space into a logical page number that is relative to the
1375  * backing object.  Use the virtual page table pointed to by (vpte).
1376  *
1377  * Possibly downgrade the protection based on the vpte bits.
1378  *
1379  * This implements an N-level page table.  Any level can terminate the
1380  * scan by setting VPTE_PS.   A linear mapping is accomplished by setting
1381  * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1382  */
1383 static
1384 int
1385 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
1386                     vpte_t vpte, int fault_type, int allow_nofault)
1387 {
1388         struct lwbuf *lwb;
1389         struct lwbuf lwb_cache;
1390         int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
1391         int result;
1392         vpte_t *ptep;
1393
1394         ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1395         for (;;) {
1396                 /*
1397                  * We cannot proceed if the vpte is not valid, not readable
1398                  * for a read fault, not writable for a write fault, or
1399                  * not executable for an instruction execution fault.
1400                  */
1401                 if ((vpte & VPTE_V) == 0) {
1402                         unlock_and_deallocate(fs);
1403                         return (KERN_FAILURE);
1404                 }
1405                 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW) == 0) {
1406                         unlock_and_deallocate(fs);
1407                         return (KERN_FAILURE);
1408                 }
1409                 if ((fault_type & VM_PROT_EXECUTE) && (vpte & VPTE_NX)) {
1410                         unlock_and_deallocate(fs);
1411                         return (KERN_FAILURE);
1412                 }
1413                 if ((vpte & VPTE_PS) || vshift == 0)
1414                         break;
1415
1416                 /*
1417                  * Get the page table page.  Nominally we only read the page
1418                  * table, but since we are actively setting VPTE_M and VPTE_A,
1419                  * tell vm_fault_object() that we are writing it. 
1420                  *
1421                  * There is currently no real need to optimize this.
1422                  */
1423                 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
1424                                          VM_PROT_READ|VM_PROT_WRITE,
1425                                          allow_nofault);
1426                 if (result != KERN_SUCCESS)
1427                         return (result);
1428
1429                 /*
1430                  * Process the returned fs.m and look up the page table
1431                  * entry in the page table page.
1432                  */
1433                 vshift -= VPTE_PAGE_BITS;
1434                 lwb = lwbuf_alloc(fs->m, &lwb_cache);
1435                 ptep = ((vpte_t *)lwbuf_kva(lwb) +
1436                         ((*pindex >> vshift) & VPTE_PAGE_MASK));
1437                 vm_page_activate(fs->m);
1438
1439                 /*
1440                  * Page table write-back - entire operation including
1441                  * validation of the pte must be atomic to avoid races
1442                  * against the vkernel changing the pte.
1443                  *
1444                  * If the vpte is valid for the* requested operation, do
1445                  * a write-back to the page table.
1446                  *
1447                  * XXX VPTE_M is not set properly for page directory pages.
1448                  * It doesn't get set in the page directory if the page table
1449                  * is modified during a read access.
1450                  */
1451                 for (;;) {
1452                         vpte_t nvpte;
1453
1454                         /*
1455                          * Reload for the cmpset, but make sure the pte is
1456                          * still valid.
1457                          */
1458                         vpte = *ptep;
1459                         cpu_ccfence();
1460                         nvpte = vpte;
1461
1462                         if ((vpte & VPTE_V) == 0)
1463                                 break;
1464
1465                         if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW))
1466                                 nvpte |= VPTE_M | VPTE_A;
1467                         if (fault_type & (VM_PROT_READ | VM_PROT_EXECUTE))
1468                                 nvpte |= VPTE_A;
1469                         if (vpte == nvpte)
1470                                 break;
1471                         if (atomic_cmpset_long(ptep, vpte, nvpte)) {
1472                                 vm_page_dirty(fs->m);
1473                                 break;
1474                         }
1475                 }
1476                 lwbuf_free(lwb);
1477                 vm_page_flag_set(fs->m, PG_REFERENCED);
1478                 vm_page_wakeup(fs->m);
1479                 fs->m = NULL;
1480                 cleanup_successful_fault(fs);
1481         }
1482
1483         /*
1484          * When the vkernel sets VPTE_RW it expects the real kernel to
1485          * reflect VPTE_M back when the page is modified via the mapping.
1486          * In order to accomplish this the real kernel must map the page
1487          * read-only for read faults and use write faults to reflect VPTE_M
1488          * back.
1489          *
1490          * Once VPTE_M has been set, the real kernel's pte allows writing.
1491          * If the vkernel clears VPTE_M the vkernel must be sure to
1492          * MADV_INVAL the real kernel's mappings to force the real kernel
1493          * to re-fault on the next write so oit can set VPTE_M again.
1494          */
1495         if ((fault_type & VM_PROT_WRITE) == 0 &&
1496             (vpte & (VPTE_RW | VPTE_M)) != (VPTE_RW | VPTE_M)) {
1497                 fs->first_prot &= ~VM_PROT_WRITE;
1498         }
1499
1500         /*
1501          * Disable EXECUTE perms if NX bit is set.
1502          */
1503         if (vpte & VPTE_NX)
1504                 fs->first_prot &= ~VM_PROT_EXECUTE;
1505
1506         /*
1507          * Combine remaining address bits with the vpte.
1508          */
1509         *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
1510                   (*pindex & ((1L << vshift) - 1));
1511         return (KERN_SUCCESS);
1512 }
1513
1514
1515 /*
1516  * This is the core of the vm_fault code.
1517  *
1518  * Do all operations required to fault-in (fs.first_object, pindex).  Run
1519  * through the shadow chain as necessary and do required COW or virtual
1520  * copy operations.  The caller has already fully resolved the vm_map_entry
1521  * and, if appropriate, has created a copy-on-write layer.  All we need to
1522  * do is iterate the object chain.
1523  *
1524  * On failure (fs) is unlocked and deallocated and the caller may return or
1525  * retry depending on the failure code.  On success (fs) is NOT unlocked or
1526  * deallocated, fs.m will contained a resolved, busied page, and fs.object
1527  * will have an additional PIP count if it is not equal to fs.first_object.
1528  *
1529  * If locks based on fs->first_shared or fs->shared are insufficient,
1530  * clear the appropriate field(s) and return RETRY.  COWs require that
1531  * first_shared be 0, while page allocations (or frees) require that
1532  * shared be 0.  Renames require that both be 0.
1533  *
1534  * NOTE! fs->[first_]shared might be set with VM_FAULT_DIRTY also set.
1535  *       we will have to retry with it exclusive if the vm_page is
1536  *       PG_SWAPPED.
1537  *
1538  * fs->first_object must be held on call.
1539  */
1540 static
1541 int
1542 vm_fault_object(struct faultstate *fs, vm_pindex_t first_pindex,
1543                 vm_prot_t fault_type, int allow_nofault)
1544 {
1545         vm_object_t next_object;
1546         vm_pindex_t pindex;
1547         int error;
1548
1549         ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1550         fs->prot = fs->first_prot;
1551         fs->object = fs->first_object;
1552         pindex = first_pindex;
1553
1554         vm_object_chain_acquire(fs->first_object, fs->shared);
1555         vm_object_pip_add(fs->first_object, 1);
1556
1557         /* 
1558          * If a read fault occurs we try to upgrade the page protection
1559          * and make it also writable if possible.  There are three cases
1560          * where we cannot make the page mapping writable:
1561          *
1562          * (1) The mapping is read-only or the VM object is read-only,
1563          *     fs->prot above will simply not have VM_PROT_WRITE set.
1564          *
1565          * (2) If the mapping is a virtual page table fs->first_prot will
1566          *     have already been properly adjusted by vm_fault_vpagetable().
1567          *     to detect writes so we can set VPTE_M in the virtual page
1568          *     table.  Used by vkernels.
1569          *
1570          * (3) If the VM page is read-only or copy-on-write, upgrading would
1571          *     just result in an unnecessary COW fault.
1572          *
1573          * (4) If the pmap specifically requests A/M bit emulation, downgrade
1574          *     here.
1575          */
1576 #if 0
1577         /* see vpagetable code */
1578         if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1579                 if ((fault_type & VM_PROT_WRITE) == 0)
1580                         fs->prot &= ~VM_PROT_WRITE;
1581         }
1582 #endif
1583
1584         if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace &&
1585             pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) {
1586                 if ((fault_type & VM_PROT_WRITE) == 0)
1587                         fs->prot &= ~VM_PROT_WRITE;
1588         }
1589
1590         /* vm_object_hold(fs->object); implied b/c object == first_object */
1591
1592         for (;;) {
1593                 /*
1594                  * The entire backing chain from first_object to object
1595                  * inclusive is chainlocked.
1596                  *
1597                  * If the object is dead, we stop here
1598                  */
1599                 if (fs->object->flags & OBJ_DEAD) {
1600                         vm_object_pip_wakeup(fs->first_object);
1601                         vm_object_chain_release_all(fs->first_object,
1602                                                     fs->object);
1603                         if (fs->object != fs->first_object)
1604                                 vm_object_drop(fs->object);
1605                         unlock_and_deallocate(fs);
1606                         return (KERN_PROTECTION_FAILURE);
1607                 }
1608
1609                 /*
1610                  * See if the page is resident.  Wait/Retry if the page is
1611                  * busy (lots of stuff may have changed so we can't continue
1612                  * in that case).
1613                  *
1614                  * We can theoretically allow the soft-busy case on a read
1615                  * fault if the page is marked valid, but since such
1616                  * pages are typically already pmap'd, putting that
1617                  * special case in might be more effort then it is
1618                  * worth.  We cannot under any circumstances mess
1619                  * around with a vm_page_t->busy page except, perhaps,
1620                  * to pmap it.
1621                  */
1622                 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1623                                                 TRUE, &error);
1624                 if (error) {
1625                         vm_object_pip_wakeup(fs->first_object);
1626                         vm_object_chain_release_all(fs->first_object,
1627                                                     fs->object);
1628                         if (fs->object != fs->first_object)
1629                                 vm_object_drop(fs->object);
1630                         unlock_things(fs);
1631                         vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1632                         mycpu->gd_cnt.v_intrans++;
1633                         /*vm_object_deallocate(fs->first_object);*/
1634                         /*fs->first_object = NULL;*/
1635                         fs->m = NULL;
1636                         return (KERN_TRY_AGAIN);
1637                 }
1638                 if (fs->m) {
1639                         /*
1640                          * The page is busied for us.
1641                          *
1642                          * If reactivating a page from PQ_CACHE we may have
1643                          * to rate-limit.
1644                          */
1645                         int queue = fs->m->queue;
1646                         vm_page_unqueue_nowakeup(fs->m);
1647
1648                         if ((queue - fs->m->pc) == PQ_CACHE && 
1649                             vm_page_count_severe()) {
1650                                 vm_page_activate(fs->m);
1651                                 vm_page_wakeup(fs->m);
1652                                 fs->m = NULL;
1653                                 vm_object_pip_wakeup(fs->first_object);
1654                                 vm_object_chain_release_all(fs->first_object,
1655                                                             fs->object);
1656                                 if (fs->object != fs->first_object)
1657                                         vm_object_drop(fs->object);
1658                                 unlock_and_deallocate(fs);
1659                                 if (allow_nofault == 0 ||
1660                                     (curthread->td_flags & TDF_NOFAULT) == 0) {
1661                                         thread_t td;
1662
1663                                         vm_wait_pfault();
1664                                         td = curthread;
1665                                         if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1666                                                 return (KERN_PROTECTION_FAILURE);
1667                                 }
1668                                 return (KERN_TRY_AGAIN);
1669                         }
1670
1671                         /*
1672                          * If it still isn't completely valid (readable),
1673                          * or if a read-ahead-mark is set on the VM page,
1674                          * jump to readrest, else we found the page and
1675                          * can return.
1676                          *
1677                          * We can release the spl once we have marked the
1678                          * page busy.
1679                          */
1680                         if (fs->m->object != &kernel_object) {
1681                                 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1682                                     VM_PAGE_BITS_ALL) {
1683                                         goto readrest;
1684                                 }
1685                                 if (fs->m->flags & PG_RAM) {
1686                                         if (debug_cluster)
1687                                                 kprintf("R");
1688                                         vm_page_flag_clear(fs->m, PG_RAM);
1689                                         goto readrest;
1690                                 }
1691                         }
1692                         break; /* break to PAGE HAS BEEN FOUND */
1693                 }
1694
1695                 /*
1696                  * Page is not resident, If this is the search termination
1697                  * or the pager might contain the page, allocate a new page.
1698                  */
1699                 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1700                         /*
1701                          * Allocating, must be exclusive.
1702                          */
1703                         if (fs->object == fs->first_object &&
1704                             fs->first_shared) {
1705                                 fs->first_shared = 0;
1706                                 vm_object_pip_wakeup(fs->first_object);
1707                                 vm_object_chain_release_all(fs->first_object,
1708                                                             fs->object);
1709                                 if (fs->object != fs->first_object)
1710                                         vm_object_drop(fs->object);
1711                                 unlock_and_deallocate(fs);
1712                                 return (KERN_TRY_AGAIN);
1713                         }
1714                         if (fs->object != fs->first_object &&
1715                             fs->shared) {
1716                                 fs->first_shared = 0;
1717                                 fs->shared = 0;
1718                                 vm_object_pip_wakeup(fs->first_object);
1719                                 vm_object_chain_release_all(fs->first_object,
1720                                                             fs->object);
1721                                 if (fs->object != fs->first_object)
1722                                         vm_object_drop(fs->object);
1723                                 unlock_and_deallocate(fs);
1724                                 return (KERN_TRY_AGAIN);
1725                         }
1726
1727                         /*
1728                          * If the page is beyond the object size we fail
1729                          */
1730                         if (pindex >= fs->object->size) {
1731                                 vm_object_pip_wakeup(fs->first_object);
1732                                 vm_object_chain_release_all(fs->first_object,
1733                                                             fs->object);
1734                                 if (fs->object != fs->first_object)
1735                                         vm_object_drop(fs->object);
1736                                 unlock_and_deallocate(fs);
1737                                 return (KERN_PROTECTION_FAILURE);
1738                         }
1739
1740                         /*
1741                          * Allocate a new page for this object/offset pair.
1742                          *
1743                          * It is possible for the allocation to race, so
1744                          * handle the case.
1745                          */
1746                         fs->m = NULL;
1747                         if (!vm_page_count_severe()) {
1748                                 fs->m = vm_page_alloc(fs->object, pindex,
1749                                     ((fs->vp || fs->object->backing_object) ?
1750                                         VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL :
1751                                         VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL |
1752                                         VM_ALLOC_USE_GD | VM_ALLOC_ZERO));
1753                         }
1754                         if (fs->m == NULL) {
1755                                 vm_object_pip_wakeup(fs->first_object);
1756                                 vm_object_chain_release_all(fs->first_object,
1757                                                             fs->object);
1758                                 if (fs->object != fs->first_object)
1759                                         vm_object_drop(fs->object);
1760                                 unlock_and_deallocate(fs);
1761                                 if (allow_nofault == 0 ||
1762                                     (curthread->td_flags & TDF_NOFAULT) == 0) {
1763                                         thread_t td;
1764
1765                                         vm_wait_pfault();
1766                                         td = curthread;
1767                                         if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1768                                                 return (KERN_PROTECTION_FAILURE);
1769                                 }
1770                                 return (KERN_TRY_AGAIN);
1771                         }
1772
1773                         /*
1774                          * Fall through to readrest.  We have a new page which
1775                          * will have to be paged (since m->valid will be 0).
1776                          */
1777                 }
1778
1779 readrest:
1780                 /*
1781                  * We have found an invalid or partially valid page, a
1782                  * page with a read-ahead mark which might be partially or
1783                  * fully valid (and maybe dirty too), or we have allocated
1784                  * a new page.
1785                  *
1786                  * Attempt to fault-in the page if there is a chance that the
1787                  * pager has it, and potentially fault in additional pages
1788                  * at the same time.
1789                  *
1790                  * If TRYPAGER is true then fs.m will be non-NULL and busied
1791                  * for us.
1792                  */
1793                 if (TRYPAGER(fs)) {
1794                         int rv;
1795                         int seqaccess;
1796                         u_char behavior = vm_map_entry_behavior(fs->entry);
1797
1798                         if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1799                                 seqaccess = 0;
1800                         else
1801                                 seqaccess = -1;
1802
1803                         /*
1804                          * Doing I/O may synchronously insert additional
1805                          * pages so we can't be shared at this point either.
1806                          *
1807                          * NOTE: We can't free fs->m here in the allocated
1808                          *       case (fs->object != fs->first_object) as
1809                          *       this would require an exclusively locked
1810                          *       VM object.
1811                          */
1812                         if (fs->object == fs->first_object &&
1813                             fs->first_shared) {
1814                                 vm_page_deactivate(fs->m);
1815                                 vm_page_wakeup(fs->m);
1816                                 fs->m = NULL;
1817                                 fs->first_shared = 0;
1818                                 vm_object_pip_wakeup(fs->first_object);
1819                                 vm_object_chain_release_all(fs->first_object,
1820                                                             fs->object);
1821                                 if (fs->object != fs->first_object)
1822                                         vm_object_drop(fs->object);
1823                                 unlock_and_deallocate(fs);
1824                                 return (KERN_TRY_AGAIN);
1825                         }
1826                         if (fs->object != fs->first_object &&
1827                             fs->shared) {
1828                                 vm_page_deactivate(fs->m);
1829                                 vm_page_wakeup(fs->m);
1830                                 fs->m = NULL;
1831                                 fs->first_shared = 0;
1832                                 fs->shared = 0;
1833                                 vm_object_pip_wakeup(fs->first_object);
1834                                 vm_object_chain_release_all(fs->first_object,
1835                                                             fs->object);
1836                                 if (fs->object != fs->first_object)
1837                                         vm_object_drop(fs->object);
1838                                 unlock_and_deallocate(fs);
1839                                 return (KERN_TRY_AGAIN);
1840                         }
1841
1842                         /*
1843                          * Avoid deadlocking against the map when doing I/O.
1844                          * fs.object and the page is BUSY'd.
1845                          *
1846                          * NOTE: Once unlocked, fs->entry can become stale
1847                          *       so this will NULL it out.
1848                          *
1849                          * NOTE: fs->entry is invalid until we relock the
1850                          *       map and verify that the timestamp has not
1851                          *       changed.
1852                          */
1853                         unlock_map(fs);
1854
1855                         /*
1856                          * Acquire the page data.  We still hold a ref on
1857                          * fs.object and the page has been BUSY's.
1858                          *
1859                          * The pager may replace the page (for example, in
1860                          * order to enter a fictitious page into the
1861                          * object).  If it does so it is responsible for
1862                          * cleaning up the passed page and properly setting
1863                          * the new page BUSY.
1864                          *
1865                          * If we got here through a PG_RAM read-ahead
1866                          * mark the page may be partially dirty and thus
1867                          * not freeable.  Don't bother checking to see
1868                          * if the pager has the page because we can't free
1869                          * it anyway.  We have to depend on the get_page
1870                          * operation filling in any gaps whether there is
1871                          * backing store or not.
1872                          */
1873                         rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1874
1875                         if (rv == VM_PAGER_OK) {
1876                                 /*
1877                                  * Relookup in case pager changed page. Pager
1878                                  * is responsible for disposition of old page
1879                                  * if moved.
1880                                  *
1881                                  * XXX other code segments do relookups too.
1882                                  * It's a bad abstraction that needs to be
1883                                  * fixed/removed.
1884                                  */
1885                                 fs->m = vm_page_lookup(fs->object, pindex);
1886                                 if (fs->m == NULL) {
1887                                         vm_object_pip_wakeup(fs->first_object);
1888                                         vm_object_chain_release_all(
1889                                                 fs->first_object, fs->object);
1890                                         if (fs->object != fs->first_object)
1891                                                 vm_object_drop(fs->object);
1892                                         unlock_and_deallocate(fs);
1893                                         return (KERN_TRY_AGAIN);
1894                                 }
1895                                 ++fs->hardfault;
1896                                 break; /* break to PAGE HAS BEEN FOUND */
1897                         }
1898
1899                         /*
1900                          * Remove the bogus page (which does not exist at this
1901                          * object/offset); before doing so, we must get back
1902                          * our object lock to preserve our invariant.
1903                          *
1904                          * Also wake up any other process that may want to bring
1905                          * in this page.
1906                          *
1907                          * If this is the top-level object, we must leave the
1908                          * busy page to prevent another process from rushing
1909                          * past us, and inserting the page in that object at
1910                          * the same time that we are.
1911                          */
1912                         if (rv == VM_PAGER_ERROR) {
1913                                 if (curproc) {
1914                                         kprintf("vm_fault: pager read error, "
1915                                                 "pid %d (%s)\n",
1916                                                 curproc->p_pid,
1917                                                 curproc->p_comm);
1918                                 } else {
1919                                         kprintf("vm_fault: pager read error, "
1920                                                 "thread %p (%s)\n",
1921                                                 curthread,
1922                                                 curthread->td_comm);
1923                                 }
1924                         }
1925
1926                         /*
1927                          * Data outside the range of the pager or an I/O error
1928                          *
1929                          * The page may have been wired during the pagein,
1930                          * e.g. by the buffer cache, and cannot simply be
1931                          * freed.  Call vnode_pager_freepage() to deal with it.
1932                          *
1933                          * Also note that we cannot free the page if we are
1934                          * holding the related object shared. XXX not sure
1935                          * what to do in that case.
1936                          */
1937                         if (fs->object != fs->first_object) {
1938                                 /*
1939                                  * Scrap the page.  Check to see if the
1940                                  * vm_pager_get_page() call has already
1941                                  * dealt with it.
1942                                  */
1943                                 if (fs->m) {
1944                                         vnode_pager_freepage(fs->m);
1945                                         fs->m = NULL;
1946                                 }
1947
1948                                 /*
1949                                  * XXX - we cannot just fall out at this
1950                                  * point, m has been freed and is invalid!
1951                                  */
1952                         }
1953                         /*
1954                          * XXX - the check for kernel_map is a kludge to work
1955                          * around having the machine panic on a kernel space
1956                          * fault w/ I/O error.
1957                          */
1958                         if (((fs->map != &kernel_map) &&
1959                             (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1960                                 if (fs->m) {
1961                                         if (fs->first_shared) {
1962                                                 vm_page_deactivate(fs->m);
1963                                                 vm_page_wakeup(fs->m);
1964                                         } else {
1965                                                 vnode_pager_freepage(fs->m);
1966                                         }
1967                                         fs->m = NULL;
1968                                 }
1969                                 vm_object_pip_wakeup(fs->first_object);
1970                                 vm_object_chain_release_all(fs->first_object,
1971                                                             fs->object);
1972                                 if (fs->object != fs->first_object)
1973                                         vm_object_drop(fs->object);
1974                                 unlock_and_deallocate(fs);
1975                                 if (rv == VM_PAGER_ERROR)
1976                                         return (KERN_FAILURE);
1977                                 else
1978                                         return (KERN_PROTECTION_FAILURE);
1979                                 /* NOT REACHED */
1980                         }
1981                 }
1982
1983                 /*
1984                  * We get here if the object has a default pager (or unwiring) 
1985                  * or the pager doesn't have the page.
1986                  *
1987                  * fs->first_m will be used for the COW unless we find a
1988                  * deeper page to be mapped read-only, in which case the
1989                  * unlock*(fs) will free first_m.
1990                  */
1991                 if (fs->object == fs->first_object)
1992                         fs->first_m = fs->m;
1993
1994                 /*
1995                  * Move on to the next object.  The chain lock should prevent
1996                  * the backing_object from getting ripped out from under us.
1997                  *
1998                  * The object lock for the next object is governed by
1999                  * fs->shared.
2000                  */
2001                 if ((next_object = fs->object->backing_object) != NULL) {
2002                         if (fs->shared)
2003                                 vm_object_hold_shared(next_object);
2004                         else
2005                                 vm_object_hold(next_object);
2006                         vm_object_chain_acquire(next_object, fs->shared);
2007                         KKASSERT(next_object == fs->object->backing_object);
2008                         pindex += OFF_TO_IDX(fs->object->backing_object_offset);
2009                 }
2010
2011                 if (next_object == NULL) {
2012                         /*
2013                          * If there's no object left, fill the page in the top
2014                          * object with zeros.
2015                          */
2016                         if (fs->object != fs->first_object) {
2017 #if 0
2018                                 if (fs->first_object->backing_object !=
2019                                     fs->object) {
2020                                         vm_object_hold(fs->first_object->backing_object);
2021                                 }
2022 #endif
2023                                 vm_object_chain_release_all(
2024                                         fs->first_object->backing_object,
2025                                         fs->object);
2026 #if 0
2027                                 if (fs->first_object->backing_object !=
2028                                     fs->object) {
2029                                         vm_object_drop(fs->first_object->backing_object);
2030                                 }
2031 #endif
2032                                 vm_object_pip_wakeup(fs->object);
2033                                 vm_object_drop(fs->object);
2034                                 fs->object = fs->first_object;
2035                                 pindex = first_pindex;
2036                                 fs->m = fs->first_m;
2037                         }
2038                         fs->first_m = NULL;
2039
2040                         /*
2041                          * Zero the page and mark it valid.
2042                          */
2043                         vm_page_zero_fill(fs->m);
2044                         mycpu->gd_cnt.v_zfod++;
2045                         fs->m->valid = VM_PAGE_BITS_ALL;
2046                         break;  /* break to PAGE HAS BEEN FOUND */
2047                 }
2048                 if (fs->object != fs->first_object) {
2049                         vm_object_pip_wakeup(fs->object);
2050                         vm_object_lock_swap();
2051                         vm_object_drop(fs->object);
2052                 }
2053                 KASSERT(fs->object != next_object,
2054                         ("object loop %p", next_object));
2055                 fs->object = next_object;
2056                 vm_object_pip_add(fs->object, 1);
2057         }
2058
2059         /*
2060          * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
2061          * is held.]
2062          *
2063          * object still held.
2064          * vm_map may not be locked (determined by fs->lookup_still_valid)
2065          *
2066          * local shared variable may be different from fs->shared.
2067          *
2068          * If the page is being written, but isn't already owned by the
2069          * top-level object, we have to copy it into a new page owned by the
2070          * top-level object.
2071          */
2072         KASSERT((fs->m->busy_count & PBUSY_LOCKED) != 0,
2073                 ("vm_fault: not busy after main loop"));
2074
2075         if (fs->object != fs->first_object) {
2076                 /*
2077                  * We only really need to copy if we want to write it.
2078                  */
2079                 if (fault_type & VM_PROT_WRITE) {
2080                         /*
2081                          * This allows pages to be virtually copied from a 
2082                          * backing_object into the first_object, where the 
2083                          * backing object has no other refs to it, and cannot
2084                          * gain any more refs.  Instead of a bcopy, we just 
2085                          * move the page from the backing object to the 
2086                          * first object.  Note that we must mark the page 
2087                          * dirty in the first object so that it will go out 
2088                          * to swap when needed.
2089                          */
2090                         if (virtual_copy_ok(fs)) {
2091                                 /*
2092                                  * (first_m) and (m) are both busied.  We have
2093                                  * move (m) into (first_m)'s object/pindex
2094                                  * in an atomic fashion, then free (first_m).
2095                                  *
2096                                  * first_object is held so second remove
2097                                  * followed by the rename should wind
2098                                  * up being atomic.  vm_page_free() might
2099                                  * block so we don't do it until after the
2100                                  * rename.
2101                                  */
2102                                 vm_page_protect(fs->first_m, VM_PROT_NONE);
2103                                 vm_page_remove(fs->first_m);
2104                                 vm_page_rename(fs->m, fs->first_object,
2105                                                first_pindex);
2106                                 vm_page_free(fs->first_m);
2107                                 fs->first_m = fs->m;
2108                                 fs->m = NULL;
2109                                 mycpu->gd_cnt.v_cow_optim++;
2110                         } else {
2111                                 /*
2112                                  * Oh, well, lets copy it.
2113                                  *
2114                                  * Why are we unmapping the original page
2115                                  * here?  Well, in short, not all accessors
2116                                  * of user memory go through the pmap.  The
2117                                  * procfs code doesn't have access user memory
2118                                  * via a local pmap, so vm_fault_page*()
2119                                  * can't call pmap_enter().  And the umtx*()
2120                                  * code may modify the COW'd page via a DMAP
2121                                  * or kernel mapping and not via the pmap,
2122                                  * leaving the original page still mapped
2123                                  * read-only into the pmap.
2124                                  *
2125                                  * So we have to remove the page from at
2126                                  * least the current pmap if it is in it.
2127                                  *
2128                                  * We used to just remove it from all pmaps
2129                                  * but that creates inefficiencies on SMP,
2130                                  * particularly for COW program & library
2131                                  * mappings that are concurrently exec'd.
2132                                  * Only remove the page from the current
2133                                  * pmap.
2134                                  */
2135                                 KKASSERT(fs->first_shared == 0);
2136                                 vm_page_copy(fs->m, fs->first_m);
2137                                 /*vm_page_protect(fs->m, VM_PROT_NONE);*/
2138                                 pmap_remove_specific(
2139                                     &curthread->td_lwp->lwp_vmspace->vm_pmap,
2140                                     fs->m);
2141                         }
2142
2143                         /*
2144                          * We no longer need the old page or object.
2145                          */
2146                         if (fs->m)
2147                                 release_page(fs);
2148
2149                         /*
2150                          * We intend to revert to first_object, undo the
2151                          * chain lock through to that.
2152                          */
2153 #if 0
2154                         if (fs->first_object->backing_object != fs->object)
2155                                 vm_object_hold(fs->first_object->backing_object);
2156 #endif
2157                         vm_object_chain_release_all(
2158                                         fs->first_object->backing_object,
2159                                         fs->object);
2160 #if 0
2161                         if (fs->first_object->backing_object != fs->object)
2162                                 vm_object_drop(fs->first_object->backing_object);
2163 #endif
2164
2165                         /*
2166                          * fs->object != fs->first_object due to above 
2167                          * conditional
2168                          */
2169                         vm_object_pip_wakeup(fs->object);
2170                         vm_object_drop(fs->object);
2171
2172                         /*
2173                          * Only use the new page below...
2174                          */
2175                         mycpu->gd_cnt.v_cow_faults++;
2176                         fs->m = fs->first_m;
2177                         fs->object = fs->first_object;
2178                         pindex = first_pindex;
2179                 } else {
2180                         /*
2181                          * If it wasn't a write fault avoid having to copy
2182                          * the page by mapping it read-only.
2183                          */
2184                         fs->prot &= ~VM_PROT_WRITE;
2185                 }
2186         }
2187
2188         /*
2189          * Relock the map if necessary, then check the generation count.
2190          * relock_map() will update fs->timestamp to account for the
2191          * relocking if necessary.
2192          *
2193          * If the count has changed after relocking then all sorts of
2194          * crap may have happened and we have to retry.
2195          *
2196          * NOTE: The relock_map() can fail due to a deadlock against
2197          *       the vm_page we are holding BUSY.
2198          */
2199         if (fs->lookup_still_valid == FALSE && fs->map) {
2200                 if (relock_map(fs) ||
2201                     fs->map->timestamp != fs->map_generation) {
2202                         release_page(fs);
2203                         vm_object_pip_wakeup(fs->first_object);
2204                         vm_object_chain_release_all(fs->first_object,
2205                                                     fs->object);
2206                         if (fs->object != fs->first_object)
2207                                 vm_object_drop(fs->object);
2208                         unlock_and_deallocate(fs);
2209                         return (KERN_TRY_AGAIN);
2210                 }
2211         }
2212
2213         /*
2214          * If the fault is a write, we know that this page is being
2215          * written NOW so dirty it explicitly to save on pmap_is_modified()
2216          * calls later.
2217          *
2218          * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
2219          * if the page is already dirty to prevent data written with
2220          * the expectation of being synced from not being synced.
2221          * Likewise if this entry does not request NOSYNC then make
2222          * sure the page isn't marked NOSYNC.  Applications sharing
2223          * data should use the same flags to avoid ping ponging.
2224          *
2225          * Also tell the backing pager, if any, that it should remove
2226          * any swap backing since the page is now dirty.
2227          */
2228         vm_page_activate(fs->m);
2229         if (fs->prot & VM_PROT_WRITE) {
2230                 vm_object_set_writeable_dirty(fs->m->object);
2231                 vm_set_nosync(fs->m, fs->entry);
2232                 if (fs->fault_flags & VM_FAULT_DIRTY) {
2233                         vm_page_dirty(fs->m);
2234                         if (fs->m->flags & PG_SWAPPED) {
2235                                 /*
2236                                  * If the page is swapped out we have to call
2237                                  * swap_pager_unswapped() which requires an
2238                                  * exclusive object lock.  If we are shared,
2239                                  * we must clear the shared flag and retry.
2240                                  */
2241                                 if ((fs->object == fs->first_object &&
2242                                      fs->first_shared) ||
2243                                     (fs->object != fs->first_object &&
2244                                      fs->shared)) {
2245                                         vm_page_wakeup(fs->m);
2246                                         fs->m = NULL;
2247                                         if (fs->object == fs->first_object)
2248                                                 fs->first_shared = 0;
2249                                         else
2250                                                 fs->shared = 0;
2251                                         vm_object_pip_wakeup(fs->first_object);
2252                                         vm_object_chain_release_all(
2253                                                 fs->first_object, fs->object);
2254                                         if (fs->object != fs->first_object)
2255                                                 vm_object_drop(fs->object);
2256                                         unlock_and_deallocate(fs);
2257                                         return (KERN_TRY_AGAIN);
2258                                 }
2259                                 swap_pager_unswapped(fs->m);
2260                         }
2261                 }
2262         }
2263
2264         vm_object_pip_wakeup(fs->first_object);
2265         vm_object_chain_release_all(fs->first_object, fs->object);
2266         if (fs->object != fs->first_object)
2267                 vm_object_drop(fs->object);
2268
2269         /*
2270          * Page had better still be busy.  We are still locked up and 
2271          * fs->object will have another PIP reference if it is not equal
2272          * to fs->first_object.
2273          */
2274         KASSERT(fs->m->busy_count & PBUSY_LOCKED,
2275                 ("vm_fault: page %p not busy!", fs->m));
2276
2277         /*
2278          * Sanity check: page must be completely valid or it is not fit to
2279          * map into user space.  vm_pager_get_pages() ensures this.
2280          */
2281         if (fs->m->valid != VM_PAGE_BITS_ALL) {
2282                 vm_page_zero_invalid(fs->m, TRUE);
2283                 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
2284         }
2285
2286         return (KERN_SUCCESS);
2287 }
2288
2289 /*
2290  * Wire down a range of virtual addresses in a map.  The entry in question
2291  * should be marked in-transition and the map must be locked.  We must
2292  * release the map temporarily while faulting-in the page to avoid a
2293  * deadlock.  Note that the entry may be clipped while we are blocked but
2294  * will never be freed.
2295  *
2296  * No requirements.
2297  */
2298 int
2299 vm_fault_wire(vm_map_t map, vm_map_entry_t entry,
2300               boolean_t user_wire, int kmflags)
2301 {
2302         boolean_t fictitious;
2303         vm_offset_t start;
2304         vm_offset_t end;
2305         vm_offset_t va;
2306         pmap_t pmap;
2307         int rv;
2308         int wire_prot;
2309         int fault_flags;
2310         vm_page_t m;
2311
2312         if (user_wire) {
2313                 wire_prot = VM_PROT_READ;
2314                 fault_flags = VM_FAULT_USER_WIRE;
2315         } else {
2316                 wire_prot = VM_PROT_READ | VM_PROT_WRITE;
2317                 fault_flags = VM_FAULT_CHANGE_WIRING;
2318         }
2319         if (kmflags & KM_NOTLBSYNC)
2320                 wire_prot |= VM_PROT_NOSYNC;
2321
2322         pmap = vm_map_pmap(map);
2323         start = entry->start;
2324         end = entry->end;
2325
2326         switch(entry->maptype) {
2327         case VM_MAPTYPE_NORMAL:
2328         case VM_MAPTYPE_VPAGETABLE:
2329                 fictitious = entry->object.vm_object &&
2330                             ((entry->object.vm_object->type == OBJT_DEVICE) ||
2331                              (entry->object.vm_object->type == OBJT_MGTDEVICE));
2332                 break;
2333         case VM_MAPTYPE_UKSMAP:
2334                 fictitious = TRUE;
2335                 break;
2336         default:
2337                 fictitious = FALSE;
2338                 break;
2339         }
2340
2341         if (entry->eflags & MAP_ENTRY_KSTACK)
2342                 start += PAGE_SIZE;
2343         map->timestamp++;
2344         vm_map_unlock(map);
2345
2346         /*
2347          * We simulate a fault to get the page and enter it in the physical
2348          * map.
2349          */
2350         for (va = start; va < end; va += PAGE_SIZE) {
2351                 rv = vm_fault(map, va, wire_prot, fault_flags);
2352                 if (rv) {
2353                         while (va > start) {
2354                                 va -= PAGE_SIZE;
2355                                 m = pmap_unwire(pmap, va);
2356                                 if (m && !fictitious) {
2357                                         vm_page_busy_wait(m, FALSE, "vmwrpg");
2358                                         vm_page_unwire(m, 1);
2359                                         vm_page_wakeup(m);
2360                                 }
2361                         }
2362                         goto done;
2363                 }
2364         }
2365         rv = KERN_SUCCESS;
2366 done:
2367         vm_map_lock(map);
2368
2369         return (rv);
2370 }
2371
2372 /*
2373  * Unwire a range of virtual addresses in a map.  The map should be
2374  * locked.
2375  */
2376 void
2377 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
2378 {
2379         boolean_t fictitious;
2380         vm_offset_t start;
2381         vm_offset_t end;
2382         vm_offset_t va;
2383         pmap_t pmap;
2384         vm_page_t m;
2385
2386         pmap = vm_map_pmap(map);
2387         start = entry->start;
2388         end = entry->end;
2389         fictitious = entry->object.vm_object &&
2390                         ((entry->object.vm_object->type == OBJT_DEVICE) ||
2391                          (entry->object.vm_object->type == OBJT_MGTDEVICE));
2392         if (entry->eflags & MAP_ENTRY_KSTACK)
2393                 start += PAGE_SIZE;
2394
2395         /*
2396          * Since the pages are wired down, we must be able to get their
2397          * mappings from the physical map system.
2398          */
2399         for (va = start; va < end; va += PAGE_SIZE) {
2400                 m = pmap_unwire(pmap, va);
2401                 if (m && !fictitious) {
2402                         vm_page_busy_wait(m, FALSE, "vmwrpg");
2403                         vm_page_unwire(m, 1);
2404                         vm_page_wakeup(m);
2405                 }
2406         }
2407 }
2408
2409 /*
2410  * Copy all of the pages from a wired-down map entry to another.
2411  *
2412  * The source and destination maps must be locked for write.
2413  * The source and destination maps token must be held
2414  * The source map entry must be wired down (or be a sharing map
2415  * entry corresponding to a main map entry that is wired down).
2416  *
2417  * No other requirements.
2418  *
2419  * XXX do segment optimization
2420  */
2421 void
2422 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
2423                     vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
2424 {
2425         vm_object_t dst_object;
2426         vm_object_t src_object;
2427         vm_ooffset_t dst_offset;
2428         vm_ooffset_t src_offset;
2429         vm_prot_t prot;
2430         vm_offset_t vaddr;
2431         vm_page_t dst_m;
2432         vm_page_t src_m;
2433
2434         src_object = src_entry->object.vm_object;
2435         src_offset = src_entry->offset;
2436
2437         /*
2438          * Create the top-level object for the destination entry. (Doesn't
2439          * actually shadow anything - we copy the pages directly.)
2440          */
2441         vm_map_entry_allocate_object(dst_entry);
2442         dst_object = dst_entry->object.vm_object;
2443
2444         prot = dst_entry->max_protection;
2445
2446         /*
2447          * Loop through all of the pages in the entry's range, copying each
2448          * one from the source object (it should be there) to the destination
2449          * object.
2450          */
2451         vm_object_hold(src_object);
2452         vm_object_hold(dst_object);
2453
2454         for (vaddr = dst_entry->start, dst_offset = 0;
2455              vaddr < dst_entry->end;
2456              vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
2457
2458                 /*
2459                  * Allocate a page in the destination object
2460                  */
2461                 do {
2462                         dst_m = vm_page_alloc(dst_object,
2463                                               OFF_TO_IDX(dst_offset),
2464                                               VM_ALLOC_NORMAL);
2465                         if (dst_m == NULL) {
2466                                 vm_wait(0);
2467                         }
2468                 } while (dst_m == NULL);
2469
2470                 /*
2471                  * Find the page in the source object, and copy it in.
2472                  * (Because the source is wired down, the page will be in
2473                  * memory.)
2474                  */
2475                 src_m = vm_page_lookup(src_object,
2476                                        OFF_TO_IDX(dst_offset + src_offset));
2477                 if (src_m == NULL)
2478                         panic("vm_fault_copy_wired: page missing");
2479
2480                 vm_page_copy(src_m, dst_m);
2481
2482                 /*
2483                  * Enter it in the pmap...
2484                  */
2485                 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE, dst_entry);
2486
2487                 /*
2488                  * Mark it no longer busy, and put it on the active list.
2489                  */
2490                 vm_page_activate(dst_m);
2491                 vm_page_wakeup(dst_m);
2492         }
2493         vm_object_drop(dst_object);
2494         vm_object_drop(src_object);
2495 }
2496
2497 #if 0
2498
2499 /*
2500  * This routine checks around the requested page for other pages that
2501  * might be able to be faulted in.  This routine brackets the viable
2502  * pages for the pages to be paged in.
2503  *
2504  * Inputs:
2505  *      m, rbehind, rahead
2506  *
2507  * Outputs:
2508  *  marray (array of vm_page_t), reqpage (index of requested page)
2509  *
2510  * Return value:
2511  *  number of pages in marray
2512  */
2513 static int
2514 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
2515                           vm_page_t *marray, int *reqpage)
2516 {
2517         int i,j;
2518         vm_object_t object;
2519         vm_pindex_t pindex, startpindex, endpindex, tpindex;
2520         vm_page_t rtm;
2521         int cbehind, cahead;
2522
2523         object = m->object;
2524         pindex = m->pindex;
2525
2526         /*
2527          * we don't fault-ahead for device pager
2528          */
2529         if ((object->type == OBJT_DEVICE) ||
2530             (object->type == OBJT_MGTDEVICE)) {
2531                 *reqpage = 0;
2532                 marray[0] = m;
2533                 return 1;
2534         }
2535
2536         /*
2537          * if the requested page is not available, then give up now
2538          */
2539         if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
2540                 *reqpage = 0;   /* not used by caller, fix compiler warn */
2541                 return 0;
2542         }
2543
2544         if ((cbehind == 0) && (cahead == 0)) {
2545                 *reqpage = 0;
2546                 marray[0] = m;
2547                 return 1;
2548         }
2549
2550         if (rahead > cahead) {
2551                 rahead = cahead;
2552         }
2553
2554         if (rbehind > cbehind) {
2555                 rbehind = cbehind;
2556         }
2557
2558         /*
2559          * Do not do any readahead if we have insufficient free memory.
2560          *
2561          * XXX code was broken disabled before and has instability
2562          * with this conditonal fixed, so shortcut for now.
2563          */
2564         if (burst_fault == 0 || vm_page_count_severe()) {
2565                 marray[0] = m;
2566                 *reqpage = 0;
2567                 return 1;
2568         }
2569
2570         /*
2571          * scan backward for the read behind pages -- in memory 
2572          *
2573          * Assume that if the page is not found an interrupt will not
2574          * create it.  Theoretically interrupts can only remove (busy)
2575          * pages, not create new associations.
2576          */
2577         if (pindex > 0) {
2578                 if (rbehind > pindex) {
2579                         rbehind = pindex;
2580                         startpindex = 0;
2581                 } else {
2582                         startpindex = pindex - rbehind;
2583                 }
2584
2585                 vm_object_hold(object);
2586                 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2587                         if (vm_page_lookup(object, tpindex - 1))
2588                                 break;
2589                 }
2590
2591                 i = 0;
2592                 while (tpindex < pindex) {
2593                         rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2594                                                              VM_ALLOC_NULL_OK);
2595                         if (rtm == NULL) {
2596                                 for (j = 0; j < i; j++) {
2597                                         vm_page_free(marray[j]);
2598                                 }
2599                                 vm_object_drop(object);
2600                                 marray[0] = m;
2601                                 *reqpage = 0;
2602                                 return 1;
2603                         }
2604                         marray[i] = rtm;
2605                         ++i;
2606                         ++tpindex;
2607                 }
2608                 vm_object_drop(object);
2609         } else {
2610                 i = 0;
2611         }
2612
2613         /*
2614          * Assign requested page
2615          */
2616         marray[i] = m;
2617         *reqpage = i;
2618         ++i;
2619
2620         /*
2621          * Scan forwards for read-ahead pages
2622          */
2623         tpindex = pindex + 1;
2624         endpindex = tpindex + rahead;
2625         if (endpindex > object->size)
2626                 endpindex = object->size;
2627
2628         vm_object_hold(object);
2629         while (tpindex < endpindex) {
2630                 if (vm_page_lookup(object, tpindex))
2631                         break;
2632                 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2633                                                      VM_ALLOC_NULL_OK);
2634                 if (rtm == NULL)
2635                         break;
2636                 marray[i] = rtm;
2637                 ++i;
2638                 ++tpindex;
2639         }
2640         vm_object_drop(object);
2641
2642         return (i);
2643 }
2644
2645 #endif
2646
2647 /*
2648  * vm_prefault() provides a quick way of clustering pagefaults into a
2649  * processes address space.  It is a "cousin" of pmap_object_init_pt,
2650  * except it runs at page fault time instead of mmap time.
2651  *
2652  * vm.fast_fault        Enables pre-faulting zero-fill pages
2653  *
2654  * vm.prefault_pages    Number of pages (1/2 negative, 1/2 positive) to
2655  *                      prefault.  Scan stops in either direction when
2656  *                      a page is found to already exist.
2657  *
2658  * This code used to be per-platform pmap_prefault().  It is now
2659  * machine-independent and enhanced to also pre-fault zero-fill pages
2660  * (see vm.fast_fault) as well as make them writable, which greatly
2661  * reduces the number of page faults programs incur.
2662  *
2663  * Application performance when pre-faulting zero-fill pages is heavily
2664  * dependent on the application.  Very tiny applications like /bin/echo
2665  * lose a little performance while applications of any appreciable size
2666  * gain performance.  Prefaulting multiple pages also reduces SMP
2667  * congestion and can improve SMP performance significantly.
2668  *
2669  * NOTE!  prot may allow writing but this only applies to the top level
2670  *        object.  If we wind up mapping a page extracted from a backing
2671  *        object we have to make sure it is read-only.
2672  *
2673  * NOTE!  The caller has already handled any COW operations on the
2674  *        vm_map_entry via the normal fault code.  Do NOT call this
2675  *        shortcut unless the normal fault code has run on this entry.
2676  *
2677  * The related map must be locked.
2678  * No other requirements.
2679  */
2680 static int vm_prefault_pages = 8;
2681 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0,
2682            "Maximum number of pages to pre-fault");
2683 static int vm_fast_fault = 1;
2684 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
2685            "Burst fault zero-fill regions");
2686
2687 /*
2688  * Set PG_NOSYNC if the map entry indicates so, but only if the page
2689  * is not already dirty by other means.  This will prevent passive
2690  * filesystem syncing as well as 'sync' from writing out the page.
2691  */
2692 static void
2693 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2694 {
2695         if (entry->eflags & MAP_ENTRY_NOSYNC) {
2696                 if (m->dirty == 0)
2697                         vm_page_flag_set(m, PG_NOSYNC);
2698         } else {
2699                 vm_page_flag_clear(m, PG_NOSYNC);
2700         }
2701 }
2702
2703 static void
2704 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2705             int fault_flags)
2706 {
2707         struct lwp *lp;
2708         vm_page_t m;
2709         vm_offset_t addr;
2710         vm_pindex_t index;
2711         vm_pindex_t pindex;
2712         vm_object_t object;
2713         int pprot;
2714         int i;
2715         int noneg;
2716         int nopos;
2717         int maxpages;
2718
2719         /*
2720          * Get stable max count value, disabled if set to 0
2721          */
2722         maxpages = vm_prefault_pages;
2723         cpu_ccfence();
2724         if (maxpages <= 0)
2725                 return;
2726
2727         /*
2728          * We do not currently prefault mappings that use virtual page
2729          * tables.  We do not prefault foreign pmaps.
2730          */
2731         if (entry->maptype != VM_MAPTYPE_NORMAL)
2732                 return;
2733         lp = curthread->td_lwp;
2734         if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2735                 return;
2736
2737         /*
2738          * Limit pre-fault count to 1024 pages.
2739          */
2740         if (maxpages > 1024)
2741                 maxpages = 1024;
2742
2743         object = entry->object.vm_object;
2744         KKASSERT(object != NULL);
2745         KKASSERT(object == entry->object.vm_object);
2746
2747         /*
2748          * NOTE: VM_FAULT_DIRTY allowed later so must hold object exclusively
2749          *       now (or do something more complex XXX).
2750          */
2751         vm_object_hold(object);
2752         vm_object_chain_acquire(object, 0);
2753
2754         noneg = 0;
2755         nopos = 0;
2756         for (i = 0; i < maxpages; ++i) {
2757                 vm_object_t lobject;
2758                 vm_object_t nobject;
2759                 int allocated = 0;
2760                 int error;
2761
2762                 /*
2763                  * This can eat a lot of time on a heavily contended
2764                  * machine so yield on the tick if needed.
2765                  */
2766                 if ((i & 7) == 7)
2767                         lwkt_yield();
2768
2769                 /*
2770                  * Calculate the page to pre-fault, stopping the scan in
2771                  * each direction separately if the limit is reached.
2772                  */
2773                 if (i & 1) {
2774                         if (noneg)
2775                                 continue;
2776                         addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2777                 } else {
2778                         if (nopos)
2779                                 continue;
2780                         addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2781                 }
2782                 if (addr < entry->start) {
2783                         noneg = 1;
2784                         if (noneg && nopos)
2785                                 break;
2786                         continue;
2787                 }
2788                 if (addr >= entry->end) {
2789                         nopos = 1;
2790                         if (noneg && nopos)
2791                                 break;
2792                         continue;
2793                 }
2794
2795                 /*
2796                  * Skip pages already mapped, and stop scanning in that
2797                  * direction.  When the scan terminates in both directions
2798                  * we are done.
2799                  */
2800                 if (pmap_prefault_ok(pmap, addr) == 0) {
2801                         if (i & 1)
2802                                 noneg = 1;
2803                         else
2804                                 nopos = 1;
2805                         if (noneg && nopos)
2806                                 break;
2807                         continue;
2808                 }
2809
2810                 /*
2811                  * Follow the VM object chain to obtain the page to be mapped
2812                  * into the pmap.
2813                  *
2814                  * If we reach the terminal object without finding a page
2815                  * and we determine it would be advantageous, then allocate
2816                  * a zero-fill page for the base object.  The base object
2817                  * is guaranteed to be OBJT_DEFAULT for this case.
2818                  *
2819                  * In order to not have to check the pager via *haspage*()
2820                  * we stop if any non-default object is encountered.  e.g.
2821                  * a vnode or swap object would stop the loop.
2822                  */
2823                 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2824                 lobject = object;
2825                 pindex = index;
2826                 pprot = prot;
2827
2828                 KKASSERT(lobject == entry->object.vm_object);
2829                 /*vm_object_hold(lobject); implied */
2830
2831                 while ((m = vm_page_lookup_busy_try(lobject, pindex,
2832                                                     TRUE, &error)) == NULL) {
2833                         if (lobject->type != OBJT_DEFAULT)
2834                                 break;
2835                         if (lobject->backing_object == NULL) {
2836                                 if (vm_fast_fault == 0)
2837                                         break;
2838                                 if ((prot & VM_PROT_WRITE) == 0 ||
2839                                     vm_page_count_min(0)) {
2840                                         break;
2841                                 }
2842
2843                                 /*
2844                                  * NOTE: Allocated from base object
2845                                  */
2846                                 m = vm_page_alloc(object, index,
2847                                                   VM_ALLOC_NORMAL |
2848                                                   VM_ALLOC_ZERO |
2849                                                   VM_ALLOC_USE_GD |
2850                                                   VM_ALLOC_NULL_OK);
2851                                 if (m == NULL)
2852                                         break;
2853                                 allocated = 1;
2854                                 pprot = prot;
2855                                 /* lobject = object .. not needed */
2856                                 break;
2857                         }
2858                         if (lobject->backing_object_offset & PAGE_MASK)
2859                                 break;
2860                         nobject = lobject->backing_object;
2861                         vm_object_hold(nobject);
2862                         KKASSERT(nobject == lobject->backing_object);
2863                         pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2864                         if (lobject != object) {
2865                                 vm_object_lock_swap();
2866                                 vm_object_drop(lobject);
2867                         }
2868                         lobject = nobject;
2869                         pprot &= ~VM_PROT_WRITE;
2870                         vm_object_chain_acquire(lobject, 0);
2871                 }
2872
2873                 /*
2874                  * NOTE: A non-NULL (m) will be associated with lobject if
2875                  *       it was found there, otherwise it is probably a
2876                  *       zero-fill page associated with the base object.
2877                  *
2878                  * Give-up if no page is available.
2879                  */
2880                 if (m == NULL) {
2881                         if (lobject != object) {
2882 #if 0
2883                                 if (object->backing_object != lobject)
2884                                         vm_object_hold(object->backing_object);
2885 #endif
2886                                 vm_object_chain_release_all(
2887                                         object->backing_object, lobject);
2888 #if 0
2889                                 if (object->backing_object != lobject)
2890                                         vm_object_drop(object->backing_object);
2891 #endif
2892                                 vm_object_drop(lobject);
2893                         }
2894                         break;
2895                 }
2896
2897                 /*
2898                  * The object must be marked dirty if we are mapping a
2899                  * writable page.  m->object is either lobject or object,
2900                  * both of which are still held.  Do this before we
2901                  * potentially drop the object.
2902                  */
2903                 if (pprot & VM_PROT_WRITE)
2904                         vm_object_set_writeable_dirty(m->object);
2905
2906                 /*
2907                  * Do not conditionalize on PG_RAM.  If pages are present in
2908                  * the VM system we assume optimal caching.  If caching is
2909                  * not optimal the I/O gravy train will be restarted when we
2910                  * hit an unavailable page.  We do not want to try to restart
2911                  * the gravy train now because we really don't know how much
2912                  * of the object has been cached.  The cost for restarting
2913                  * the gravy train should be low (since accesses will likely
2914                  * be I/O bound anyway).
2915                  */
2916                 if (lobject != object) {
2917 #if 0
2918                         if (object->backing_object != lobject)
2919                                 vm_object_hold(object->backing_object);
2920 #endif
2921                         vm_object_chain_release_all(object->backing_object,
2922                                                     lobject);
2923 #if 0
2924                         if (object->backing_object != lobject)
2925                                 vm_object_drop(object->backing_object);
2926 #endif
2927                         vm_object_drop(lobject);
2928                 }
2929
2930                 /*
2931                  * Enter the page into the pmap if appropriate.  If we had
2932                  * allocated the page we have to place it on a queue.  If not
2933                  * we just have to make sure it isn't on the cache queue
2934                  * (pages on the cache queue are not allowed to be mapped).
2935                  */
2936                 if (allocated) {
2937                         /*
2938                          * Page must be zerod.
2939                          */
2940                         vm_page_zero_fill(m);
2941                         mycpu->gd_cnt.v_zfod++;
2942                         m->valid = VM_PAGE_BITS_ALL;
2943
2944                         /*
2945                          * Handle dirty page case
2946                          */
2947                         if (pprot & VM_PROT_WRITE)
2948                                 vm_set_nosync(m, entry);
2949                         pmap_enter(pmap, addr, m, pprot, 0, entry);
2950                         mycpu->gd_cnt.v_vm_faults++;
2951                         if (curthread->td_lwp)
2952                                 ++curthread->td_lwp->lwp_ru.ru_minflt;
2953                         vm_page_deactivate(m);
2954                         if (pprot & VM_PROT_WRITE) {
2955                                 /*vm_object_set_writeable_dirty(m->object);*/
2956                                 vm_set_nosync(m, entry);
2957                                 if (fault_flags & VM_FAULT_DIRTY) {
2958                                         vm_page_dirty(m);
2959                                         /*XXX*/
2960                                         swap_pager_unswapped(m);
2961                                 }
2962                         }
2963                         vm_page_wakeup(m);
2964                 } else if (error) {
2965                         /* couldn't busy page, no wakeup */
2966                 } else if (
2967                     ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2968                     (m->flags & PG_FICTITIOUS) == 0) {
2969                         /*
2970                          * A fully valid page not undergoing soft I/O can
2971                          * be immediately entered into the pmap.
2972                          */
2973                         if ((m->queue - m->pc) == PQ_CACHE)
2974                                 vm_page_deactivate(m);
2975                         if (pprot & VM_PROT_WRITE) {
2976                                 /*vm_object_set_writeable_dirty(m->object);*/
2977                                 vm_set_nosync(m, entry);
2978                                 if (fault_flags & VM_FAULT_DIRTY) {
2979                                         vm_page_dirty(m);
2980                                         /*XXX*/
2981                                         swap_pager_unswapped(m);
2982                                 }
2983                         }
2984                         if (pprot & VM_PROT_WRITE)
2985                                 vm_set_nosync(m, entry);
2986                         pmap_enter(pmap, addr, m, pprot, 0, entry);
2987                         mycpu->gd_cnt.v_vm_faults++;
2988                         if (curthread->td_lwp)
2989                                 ++curthread->td_lwp->lwp_ru.ru_minflt;
2990                         vm_page_wakeup(m);
2991                 } else {
2992                         vm_page_wakeup(m);
2993                 }
2994         }
2995         vm_object_chain_release(object);
2996         vm_object_drop(object);
2997 }
2998
2999 /*
3000  * Object can be held shared
3001  */
3002 static void
3003 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
3004                   vm_map_entry_t entry, int prot, int fault_flags)
3005 {
3006         struct lwp *lp;
3007         vm_page_t m;
3008         vm_offset_t addr;
3009         vm_pindex_t pindex;
3010         vm_object_t object;
3011         int i;
3012         int noneg;
3013         int nopos;
3014         int maxpages;
3015
3016         /*
3017          * Get stable max count value, disabled if set to 0
3018          */
3019         maxpages = vm_prefault_pages;
3020         cpu_ccfence();
3021         if (maxpages <= 0)
3022                 return;
3023
3024         /*
3025          * We do not currently prefault mappings that use virtual page
3026          * tables.  We do not prefault foreign pmaps.
3027          */
3028         if (entry->maptype != VM_MAPTYPE_NORMAL)
3029                 return;
3030         lp = curthread->td_lwp;
3031         if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
3032                 return;
3033         object = entry->object.vm_object;
3034         if (object->backing_object != NULL)
3035                 return;
3036         ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
3037
3038         /*
3039          * Limit pre-fault count to 1024 pages.
3040          */
3041         if (maxpages > 1024)
3042                 maxpages = 1024;
3043
3044         noneg = 0;
3045         nopos = 0;
3046         for (i = 0; i < maxpages; ++i) {
3047                 int error;
3048
3049                 /*
3050                  * Calculate the page to pre-fault, stopping the scan in
3051                  * each direction separately if the limit is reached.
3052                  */
3053                 if (i & 1) {
3054                         if (noneg)
3055                                 continue;
3056                         addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
3057                 } else {
3058                         if (nopos)
3059                                 continue;
3060                         addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
3061                 }
3062                 if (addr < entry->start) {
3063                         noneg = 1;
3064                         if (noneg && nopos)
3065                                 break;
3066                         continue;
3067                 }
3068                 if (addr >= entry->end) {
3069                         nopos = 1;
3070                         if (noneg && nopos)
3071                                 break;
3072                         continue;
3073                 }
3074
3075                 /*
3076                  * Follow the VM object chain to obtain the page to be mapped
3077                  * into the pmap.  This version of the prefault code only
3078                  * works with terminal objects.
3079                  *
3080                  * The page must already exist.  If we encounter a problem
3081                  * we stop here.
3082                  *
3083                  * WARNING!  We cannot call swap_pager_unswapped() or insert
3084                  *           a new vm_page with a shared token.
3085                  */
3086                 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
3087
3088                 /*
3089                  * Skip pages already mapped, and stop scanning in that
3090                  * direction.  When the scan terminates in both directions
3091                  * we are done.
3092                  */
3093                 if (pmap_prefault_ok(pmap, addr) == 0) {
3094                         if (i & 1)
3095                                 noneg = 1;
3096                         else
3097                                 nopos = 1;
3098                         if (noneg && nopos)
3099                                 break;
3100                         continue;
3101                 }
3102
3103                 /*
3104                  * Shortcut the read-only mapping case using the far more
3105                  * efficient vm_page_lookup_sbusy_try() function.  This
3106                  * allows us to acquire the page soft-busied only which
3107                  * is especially nice for concurrent execs of the same
3108                  * program.
3109                  *
3110                  * The lookup function also validates page suitability
3111                  * (all valid bits set, and not fictitious).
3112                  *
3113                  * If the page is in PQ_CACHE we have to fall-through
3114                  * and hard-busy it so we can move it out of PQ_CACHE.
3115                  */
3116                 if ((prot & VM_PROT_WRITE) == 0) {
3117                         m = vm_page_lookup_sbusy_try(object, pindex,
3118                                                      0, PAGE_SIZE);
3119                         if (m == NULL)
3120                                 break;
3121                         if ((m->queue - m->pc) != PQ_CACHE) {
3122                                 pmap_enter(pmap, addr, m, prot, 0, entry);
3123                                 mycpu->gd_cnt.v_vm_faults++;
3124                                 if (curthread->td_lwp)
3125                                         ++curthread->td_lwp->lwp_ru.ru_minflt;
3126                                 vm_page_sbusy_drop(m);
3127                                 continue;
3128                         }
3129                         vm_page_sbusy_drop(m);
3130                 }
3131
3132                 /*
3133                  * Fallback to normal vm_page lookup code.  This code
3134                  * hard-busies the page.  Not only that, but the page
3135                  * can remain in that state for a significant period
3136                  * time due to pmap_enter()'s overhead.
3137                  */
3138                 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error);
3139                 if (m == NULL || error)
3140                         break;
3141
3142                 /*
3143                  * Stop if the page cannot be trivially entered into the
3144                  * pmap.
3145                  */
3146                 if (((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) ||
3147                     (m->flags & PG_FICTITIOUS) ||
3148                     ((m->flags & PG_SWAPPED) &&
3149                      (prot & VM_PROT_WRITE) &&
3150                      (fault_flags & VM_FAULT_DIRTY))) {
3151                         vm_page_wakeup(m);
3152                         break;
3153                 }
3154
3155                 /*
3156                  * Enter the page into the pmap.  The object might be held
3157                  * shared so we can't do any (serious) modifying operation
3158                  * on it.
3159                  */
3160                 if ((m->queue - m->pc) == PQ_CACHE)
3161                         vm_page_deactivate(m);
3162                 if (prot & VM_PROT_WRITE) {
3163                         vm_object_set_writeable_dirty(m->object);
3164                         vm_set_nosync(m, entry);
3165                         if (fault_flags & VM_FAULT_DIRTY) {
3166                                 vm_page_dirty(m);
3167                                 /* can't happeen due to conditional above */
3168                                 /* swap_pager_unswapped(m); */
3169                         }
3170                 }
3171                 pmap_enter(pmap, addr, m, prot, 0, entry);
3172                 mycpu->gd_cnt.v_vm_faults++;
3173                 if (curthread->td_lwp)
3174                         ++curthread->td_lwp->lwp_ru.ru_minflt;
3175                 vm_page_wakeup(m);
3176         }
3177 }