]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/vm/vm_map.c
MFC 214144:
[FreeBSD/stable/8.git] / sys / vm / vm_map.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * The Mach Operating System project at Carnegie-Mellon University.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from: @(#)vm_map.c      8.3 (Berkeley) 1/12/94
33  *
34  *
35  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36  * All rights reserved.
37  *
38  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39  *
40  * Permission to use, copy, modify and distribute this software and
41  * its documentation is hereby granted, provided that both the copyright
42  * notice and this permission notice appear in all copies of the
43  * software, derivative works or modified versions, and any portions
44  * thereof, and that both notices appear in supporting documentation.
45  *
46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49  *
50  * Carnegie Mellon requests users of this software to return to
51  *
52  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53  *  School of Computer Science
54  *  Carnegie Mellon University
55  *  Pittsburgh PA 15213-3890
56  *
57  * any improvements or extensions that they make and grant Carnegie the
58  * rights to redistribute these changes.
59  */
60
61 /*
62  *      Virtual memory mapping module.
63  */
64
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/ktr.h>
72 #include <sys/lock.h>
73 #include <sys/mutex.h>
74 #include <sys/proc.h>
75 #include <sys/vmmeter.h>
76 #include <sys/mman.h>
77 #include <sys/vnode.h>
78 #include <sys/resourcevar.h>
79 #include <sys/file.h>
80 #include <sys/sysctl.h>
81 #include <sys/sysent.h>
82 #include <sys/shm.h>
83
84 #include <vm/vm.h>
85 #include <vm/vm_param.h>
86 #include <vm/pmap.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_pager.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
93 #include <vm/swap_pager.h>
94 #include <vm/uma.h>
95
96 /*
97  *      Virtual memory maps provide for the mapping, protection,
98  *      and sharing of virtual memory objects.  In addition,
99  *      this module provides for an efficient virtual copy of
100  *      memory from one map to another.
101  *
102  *      Synchronization is required prior to most operations.
103  *
104  *      Maps consist of an ordered doubly-linked list of simple
105  *      entries; a self-adjusting binary search tree of these
106  *      entries is used to speed up lookups.
107  *
108  *      Since portions of maps are specified by start/end addresses,
109  *      which may not align with existing map entries, all
110  *      routines merely "clip" entries to these start/end values.
111  *      [That is, an entry is split into two, bordering at a
112  *      start or end value.]  Note that these clippings may not
113  *      always be necessary (as the two resulting entries are then
114  *      not changed); however, the clipping is done for convenience.
115  *
116  *      As mentioned above, virtual copy operations are performed
117  *      by copying VM object references from one map to
118  *      another, and then marking both regions as copy-on-write.
119  */
120
121 static struct mtx map_sleep_mtx;
122 static uma_zone_t mapentzone;
123 static uma_zone_t kmapentzone;
124 static uma_zone_t mapzone;
125 static uma_zone_t vmspace_zone;
126 static struct vm_object kmapentobj;
127 static int vmspace_zinit(void *mem, int size, int flags);
128 static void vmspace_zfini(void *mem, int size);
129 static int vm_map_zinit(void *mem, int ize, int flags);
130 static void vm_map_zfini(void *mem, int size);
131 static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
132 static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
133 static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
134 #ifdef INVARIANTS
135 static void vm_map_zdtor(void *mem, int size, void *arg);
136 static void vmspace_zdtor(void *mem, int size, void *arg);
137 #endif
138
139 #define ENTRY_CHARGED(e) ((e)->uip != NULL || \
140     ((e)->object.vm_object != NULL && (e)->object.vm_object->uip != NULL && \
141      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
142
143 /* 
144  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
145  * stable.
146  */
147 #define PROC_VMSPACE_LOCK(p) do { } while (0)
148 #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
149
150 /*
151  *      VM_MAP_RANGE_CHECK:     [ internal use only ]
152  *
153  *      Asserts that the starting and ending region
154  *      addresses fall within the valid range of the map.
155  */
156 #define VM_MAP_RANGE_CHECK(map, start, end)             \
157                 {                                       \
158                 if (start < vm_map_min(map))            \
159                         start = vm_map_min(map);        \
160                 if (end > vm_map_max(map))              \
161                         end = vm_map_max(map);          \
162                 if (start > end)                        \
163                         start = end;                    \
164                 }
165
166 /*
167  *      vm_map_startup:
168  *
169  *      Initialize the vm_map module.  Must be called before
170  *      any other vm_map routines.
171  *
172  *      Map and entry structures are allocated from the general
173  *      purpose memory pool with some exceptions:
174  *
175  *      - The kernel map and kmem submap are allocated statically.
176  *      - Kernel map entries are allocated out of a static pool.
177  *
178  *      These restrictions are necessary since malloc() uses the
179  *      maps and requires map entries.
180  */
181
182 void
183 vm_map_startup(void)
184 {
185         mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
186         mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
187 #ifdef INVARIANTS
188             vm_map_zdtor,
189 #else
190             NULL,
191 #endif
192             vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
193         uma_prealloc(mapzone, MAX_KMAP);
194         kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
195             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
196             UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
197         uma_prealloc(kmapentzone, MAX_KMAPENT);
198         mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
199             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
200 }
201
202 static void
203 vmspace_zfini(void *mem, int size)
204 {
205         struct vmspace *vm;
206
207         vm = (struct vmspace *)mem;
208         vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
209 }
210
211 static int
212 vmspace_zinit(void *mem, int size, int flags)
213 {
214         struct vmspace *vm;
215
216         vm = (struct vmspace *)mem;
217
218         vm->vm_map.pmap = NULL;
219         (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
220         return (0);
221 }
222
223 static void
224 vm_map_zfini(void *mem, int size)
225 {
226         vm_map_t map;
227
228         map = (vm_map_t)mem;
229         mtx_destroy(&map->system_mtx);
230         sx_destroy(&map->lock);
231 }
232
233 static int
234 vm_map_zinit(void *mem, int size, int flags)
235 {
236         vm_map_t map;
237
238         map = (vm_map_t)mem;
239         map->nentries = 0;
240         map->size = 0;
241         mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
242         sx_init(&map->lock, "user map");
243         return (0);
244 }
245
246 #ifdef INVARIANTS
247 static void
248 vmspace_zdtor(void *mem, int size, void *arg)
249 {
250         struct vmspace *vm;
251
252         vm = (struct vmspace *)mem;
253
254         vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
255 }
256 static void
257 vm_map_zdtor(void *mem, int size, void *arg)
258 {
259         vm_map_t map;
260
261         map = (vm_map_t)mem;
262         KASSERT(map->nentries == 0,
263             ("map %p nentries == %d on free.",
264             map, map->nentries));
265         KASSERT(map->size == 0,
266             ("map %p size == %lu on free.",
267             map, (unsigned long)map->size));
268 }
269 #endif  /* INVARIANTS */
270
271 /*
272  * Allocate a vmspace structure, including a vm_map and pmap,
273  * and initialize those structures.  The refcnt is set to 1.
274  */
275 struct vmspace *
276 vmspace_alloc(min, max)
277         vm_offset_t min, max;
278 {
279         struct vmspace *vm;
280
281         vm = uma_zalloc(vmspace_zone, M_WAITOK);
282         if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
283                 uma_zfree(vmspace_zone, vm);
284                 return (NULL);
285         }
286         CTR1(KTR_VM, "vmspace_alloc: %p", vm);
287         _vm_map_init(&vm->vm_map, min, max);
288         vm->vm_map.pmap = vmspace_pmap(vm);             /* XXX */
289         vm->vm_refcnt = 1;
290         vm->vm_shm = NULL;
291         vm->vm_swrss = 0;
292         vm->vm_tsize = 0;
293         vm->vm_dsize = 0;
294         vm->vm_ssize = 0;
295         vm->vm_taddr = 0;
296         vm->vm_daddr = 0;
297         vm->vm_maxsaddr = 0;
298         return (vm);
299 }
300
301 void
302 vm_init2(void)
303 {
304         uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
305             (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
306              maxproc * 2 + maxfiles);
307         vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
308 #ifdef INVARIANTS
309             vmspace_zdtor,
310 #else
311             NULL,
312 #endif
313             vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
314 }
315
316 static inline void
317 vmspace_dofree(struct vmspace *vm)
318 {
319         CTR1(KTR_VM, "vmspace_free: %p", vm);
320
321         /*
322          * Make sure any SysV shm is freed, it might not have been in
323          * exit1().
324          */
325         shmexit(vm);
326
327         /*
328          * Lock the map, to wait out all other references to it.
329          * Delete all of the mappings and pages they hold, then call
330          * the pmap module to reclaim anything left.
331          */
332         (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
333             vm->vm_map.max_offset);
334
335         /*
336          * XXX Comment out the pmap_release call for now. The
337          * vmspace_zone is marked as UMA_ZONE_NOFREE, and bugs cause
338          * pmap.resident_count to be != 0 on exit sometimes.
339          */
340 /*      pmap_release(vmspace_pmap(vm)); */
341         uma_zfree(vmspace_zone, vm);
342 }
343
344 void
345 vmspace_free(struct vmspace *vm)
346 {
347
348         if (vm->vm_refcnt == 0)
349                 panic("vmspace_free: attempt to free already freed vmspace");
350
351         if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
352                 vmspace_dofree(vm);
353 }
354
355 void
356 vmspace_exitfree(struct proc *p)
357 {
358         struct vmspace *vm;
359
360         PROC_VMSPACE_LOCK(p);
361         vm = p->p_vmspace;
362         p->p_vmspace = NULL;
363         PROC_VMSPACE_UNLOCK(p);
364         KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
365         vmspace_free(vm);
366 }
367
368 void
369 vmspace_exit(struct thread *td)
370 {
371         int refcnt;
372         struct vmspace *vm;
373         struct proc *p;
374
375         /*
376          * Release user portion of address space.
377          * This releases references to vnodes,
378          * which could cause I/O if the file has been unlinked.
379          * Need to do this early enough that we can still sleep.
380          *
381          * The last exiting process to reach this point releases as
382          * much of the environment as it can. vmspace_dofree() is the
383          * slower fallback in case another process had a temporary
384          * reference to the vmspace.
385          */
386
387         p = td->td_proc;
388         vm = p->p_vmspace;
389         atomic_add_int(&vmspace0.vm_refcnt, 1);
390         do {
391                 refcnt = vm->vm_refcnt;
392                 if (refcnt > 1 && p->p_vmspace != &vmspace0) {
393                         /* Switch now since other proc might free vmspace */
394                         PROC_VMSPACE_LOCK(p);
395                         p->p_vmspace = &vmspace0;
396                         PROC_VMSPACE_UNLOCK(p);
397                         pmap_activate(td);
398                 }
399         } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
400         if (refcnt == 1) {
401                 if (p->p_vmspace != vm) {
402                         /* vmspace not yet freed, switch back */
403                         PROC_VMSPACE_LOCK(p);
404                         p->p_vmspace = vm;
405                         PROC_VMSPACE_UNLOCK(p);
406                         pmap_activate(td);
407                 }
408                 pmap_remove_pages(vmspace_pmap(vm));
409                 /* Switch now since this proc will free vmspace */
410                 PROC_VMSPACE_LOCK(p);
411                 p->p_vmspace = &vmspace0;
412                 PROC_VMSPACE_UNLOCK(p);
413                 pmap_activate(td);
414                 vmspace_dofree(vm);
415         }
416 }
417
418 /* Acquire reference to vmspace owned by another process. */
419
420 struct vmspace *
421 vmspace_acquire_ref(struct proc *p)
422 {
423         struct vmspace *vm;
424         int refcnt;
425
426         PROC_VMSPACE_LOCK(p);
427         vm = p->p_vmspace;
428         if (vm == NULL) {
429                 PROC_VMSPACE_UNLOCK(p);
430                 return (NULL);
431         }
432         do {
433                 refcnt = vm->vm_refcnt;
434                 if (refcnt <= 0) {      /* Avoid 0->1 transition */
435                         PROC_VMSPACE_UNLOCK(p);
436                         return (NULL);
437                 }
438         } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
439         if (vm != p->p_vmspace) {
440                 PROC_VMSPACE_UNLOCK(p);
441                 vmspace_free(vm);
442                 return (NULL);
443         }
444         PROC_VMSPACE_UNLOCK(p);
445         return (vm);
446 }
447
448 void
449 _vm_map_lock(vm_map_t map, const char *file, int line)
450 {
451
452         if (map->system_map)
453                 _mtx_lock_flags(&map->system_mtx, 0, file, line);
454         else
455                 (void)_sx_xlock(&map->lock, 0, file, line);
456         map->timestamp++;
457 }
458
459 static void
460 vm_map_process_deferred(void)
461 {
462         struct thread *td;
463         vm_map_entry_t entry;
464
465         td = curthread;
466
467         while ((entry = td->td_map_def_user) != NULL) {
468                 td->td_map_def_user = entry->next;
469                 vm_map_entry_deallocate(entry, FALSE);
470         }
471 }
472
473 void
474 _vm_map_unlock(vm_map_t map, const char *file, int line)
475 {
476
477         if (map->system_map)
478                 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
479         else {
480                 _sx_xunlock(&map->lock, file, line);
481                 vm_map_process_deferred();
482         }
483 }
484
485 void
486 _vm_map_lock_read(vm_map_t map, const char *file, int line)
487 {
488
489         if (map->system_map)
490                 _mtx_lock_flags(&map->system_mtx, 0, file, line);
491         else
492                 (void)_sx_slock(&map->lock, 0, file, line);
493 }
494
495 void
496 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
497 {
498
499         if (map->system_map)
500                 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
501         else {
502                 _sx_sunlock(&map->lock, file, line);
503                 vm_map_process_deferred();
504         }
505 }
506
507 int
508 _vm_map_trylock(vm_map_t map, const char *file, int line)
509 {
510         int error;
511
512         error = map->system_map ?
513             !_mtx_trylock(&map->system_mtx, 0, file, line) :
514             !_sx_try_xlock(&map->lock, file, line);
515         if (error == 0)
516                 map->timestamp++;
517         return (error == 0);
518 }
519
520 int
521 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
522 {
523         int error;
524
525         error = map->system_map ?
526             !_mtx_trylock(&map->system_mtx, 0, file, line) :
527             !_sx_try_slock(&map->lock, file, line);
528         return (error == 0);
529 }
530
531 /*
532  *      _vm_map_lock_upgrade:   [ internal use only ]
533  *
534  *      Tries to upgrade a read (shared) lock on the specified map to a write
535  *      (exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
536  *      non-zero value if the upgrade fails.  If the upgrade fails, the map is
537  *      returned without a read or write lock held.
538  *
539  *      Requires that the map be read locked.
540  */
541 int
542 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
543 {
544         unsigned int last_timestamp;
545
546         if (map->system_map) {
547 #ifdef INVARIANTS
548                 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
549 #endif
550         } else {
551                 if (!_sx_try_upgrade(&map->lock, file, line)) {
552                         last_timestamp = map->timestamp;
553                         _sx_sunlock(&map->lock, file, line);
554                         vm_map_process_deferred();
555                         /*
556                          * If the map's timestamp does not change while the
557                          * map is unlocked, then the upgrade succeeds.
558                          */
559                         (void)_sx_xlock(&map->lock, 0, file, line);
560                         if (last_timestamp != map->timestamp) {
561                                 _sx_xunlock(&map->lock, file, line);
562                                 return (1);
563                         }
564                 }
565         }
566         map->timestamp++;
567         return (0);
568 }
569
570 void
571 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
572 {
573
574         if (map->system_map) {
575 #ifdef INVARIANTS
576                 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
577 #endif
578         } else
579                 _sx_downgrade(&map->lock, file, line);
580 }
581
582 /*
583  *      vm_map_locked:
584  *
585  *      Returns a non-zero value if the caller holds a write (exclusive) lock
586  *      on the specified map and the value "0" otherwise.
587  */
588 int
589 vm_map_locked(vm_map_t map)
590 {
591
592         if (map->system_map)
593                 return (mtx_owned(&map->system_mtx));
594         else
595                 return (sx_xlocked(&map->lock));
596 }
597
598 #ifdef INVARIANTS
599 static void
600 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
601 {
602
603         if (map->system_map)
604                 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
605         else
606                 _sx_assert(&map->lock, SA_XLOCKED, file, line);
607 }
608
609 #if 0
610 static void
611 _vm_map_assert_locked_read(vm_map_t map, const char *file, int line)
612 {
613
614         if (map->system_map)
615                 _mtx_assert(&map->system_mtx, MA_OWNED, file, line);
616         else
617                 _sx_assert(&map->lock, SA_SLOCKED, file, line);
618 }
619 #endif
620
621 #define VM_MAP_ASSERT_LOCKED(map) \
622     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
623 #define VM_MAP_ASSERT_LOCKED_READ(map) \
624     _vm_map_assert_locked_read(map, LOCK_FILE, LOCK_LINE)
625 #else
626 #define VM_MAP_ASSERT_LOCKED(map)
627 #define VM_MAP_ASSERT_LOCKED_READ(map)
628 #endif
629
630 /*
631  *      _vm_map_unlock_and_wait:
632  *
633  *      Atomically releases the lock on the specified map and puts the calling
634  *      thread to sleep.  The calling thread will remain asleep until either
635  *      vm_map_wakeup() is performed on the map or the specified timeout is
636  *      exceeded.
637  *
638  *      WARNING!  This function does not perform deferred deallocations of
639  *      objects and map entries.  Therefore, the calling thread is expected to
640  *      reacquire the map lock after reawakening and later perform an ordinary
641  *      unlock operation, such as vm_map_unlock(), before completing its
642  *      operation on the map.
643  */
644 int
645 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
646 {
647
648         mtx_lock(&map_sleep_mtx);
649         if (map->system_map)
650                 _mtx_unlock_flags(&map->system_mtx, 0, file, line);
651         else
652                 _sx_xunlock(&map->lock, file, line);
653         return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
654             timo));
655 }
656
657 /*
658  *      vm_map_wakeup:
659  *
660  *      Awaken any threads that have slept on the map using
661  *      vm_map_unlock_and_wait().
662  */
663 void
664 vm_map_wakeup(vm_map_t map)
665 {
666
667         /*
668          * Acquire and release map_sleep_mtx to prevent a wakeup()
669          * from being performed (and lost) between the map unlock
670          * and the msleep() in _vm_map_unlock_and_wait().
671          */
672         mtx_lock(&map_sleep_mtx);
673         mtx_unlock(&map_sleep_mtx);
674         wakeup(&map->root);
675 }
676
677 void
678 vm_map_busy(vm_map_t map)
679 {
680
681         VM_MAP_ASSERT_LOCKED(map);
682         map->busy++;
683 }
684
685 void
686 vm_map_unbusy(vm_map_t map)
687 {
688
689         VM_MAP_ASSERT_LOCKED(map);
690         KASSERT(map->busy, ("vm_map_unbusy: not busy"));
691         if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
692                 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
693                 wakeup(&map->busy);
694         }
695 }
696
697 void 
698 vm_map_wait_busy(vm_map_t map)
699 {
700
701         VM_MAP_ASSERT_LOCKED(map);
702         while (map->busy) {
703                 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
704                 if (map->system_map)
705                         msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
706                 else
707                         sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
708         }
709         map->timestamp++;
710 }
711
712 long
713 vmspace_resident_count(struct vmspace *vmspace)
714 {
715         return pmap_resident_count(vmspace_pmap(vmspace));
716 }
717
718 long
719 vmspace_wired_count(struct vmspace *vmspace)
720 {
721         return pmap_wired_count(vmspace_pmap(vmspace));
722 }
723
724 /*
725  *      vm_map_create:
726  *
727  *      Creates and returns a new empty VM map with
728  *      the given physical map structure, and having
729  *      the given lower and upper address bounds.
730  */
731 vm_map_t
732 vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
733 {
734         vm_map_t result;
735
736         result = uma_zalloc(mapzone, M_WAITOK);
737         CTR1(KTR_VM, "vm_map_create: %p", result);
738         _vm_map_init(result, min, max);
739         result->pmap = pmap;
740         return (result);
741 }
742
743 /*
744  * Initialize an existing vm_map structure
745  * such as that in the vmspace structure.
746  * The pmap is set elsewhere.
747  */
748 static void
749 _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
750 {
751
752         map->header.next = map->header.prev = &map->header;
753         map->needs_wakeup = FALSE;
754         map->system_map = 0;
755         map->min_offset = min;
756         map->max_offset = max;
757         map->flags = 0;
758         map->root = NULL;
759         map->timestamp = 0;
760         map->busy = 0;
761 }
762
763 void
764 vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
765 {
766         _vm_map_init(map, min, max);
767         mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
768         sx_init(&map->lock, "user map");
769 }
770
771 /*
772  *      vm_map_entry_dispose:   [ internal use only ]
773  *
774  *      Inverse of vm_map_entry_create.
775  */
776 static void
777 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
778 {
779         uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
780 }
781
782 /*
783  *      vm_map_entry_create:    [ internal use only ]
784  *
785  *      Allocates a VM map entry for insertion.
786  *      No entry fields are filled in.
787  */
788 static vm_map_entry_t
789 vm_map_entry_create(vm_map_t map)
790 {
791         vm_map_entry_t new_entry;
792
793         if (map->system_map)
794                 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
795         else
796                 new_entry = uma_zalloc(mapentzone, M_WAITOK);
797         if (new_entry == NULL)
798                 panic("vm_map_entry_create: kernel resources exhausted");
799         return (new_entry);
800 }
801
802 /*
803  *      vm_map_entry_set_behavior:
804  *
805  *      Set the expected access behavior, either normal, random, or
806  *      sequential.
807  */
808 static inline void
809 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
810 {
811         entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
812             (behavior & MAP_ENTRY_BEHAV_MASK);
813 }
814
815 /*
816  *      vm_map_entry_set_max_free:
817  *
818  *      Set the max_free field in a vm_map_entry.
819  */
820 static inline void
821 vm_map_entry_set_max_free(vm_map_entry_t entry)
822 {
823
824         entry->max_free = entry->adj_free;
825         if (entry->left != NULL && entry->left->max_free > entry->max_free)
826                 entry->max_free = entry->left->max_free;
827         if (entry->right != NULL && entry->right->max_free > entry->max_free)
828                 entry->max_free = entry->right->max_free;
829 }
830
831 /*
832  *      vm_map_entry_splay:
833  *
834  *      The Sleator and Tarjan top-down splay algorithm with the
835  *      following variation.  Max_free must be computed bottom-up, so
836  *      on the downward pass, maintain the left and right spines in
837  *      reverse order.  Then, make a second pass up each side to fix
838  *      the pointers and compute max_free.  The time bound is O(log n)
839  *      amortized.
840  *
841  *      The new root is the vm_map_entry containing "addr", or else an
842  *      adjacent entry (lower or higher) if addr is not in the tree.
843  *
844  *      The map must be locked, and leaves it so.
845  *
846  *      Returns: the new root.
847  */
848 static vm_map_entry_t
849 vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
850 {
851         vm_map_entry_t llist, rlist;
852         vm_map_entry_t ltree, rtree;
853         vm_map_entry_t y;
854
855         /* Special case of empty tree. */
856         if (root == NULL)
857                 return (root);
858
859         /*
860          * Pass One: Splay down the tree until we find addr or a NULL
861          * pointer where addr would go.  llist and rlist are the two
862          * sides in reverse order (bottom-up), with llist linked by
863          * the right pointer and rlist linked by the left pointer in
864          * the vm_map_entry.  Wait until Pass Two to set max_free on
865          * the two spines.
866          */
867         llist = NULL;
868         rlist = NULL;
869         for (;;) {
870                 /* root is never NULL in here. */
871                 if (addr < root->start) {
872                         y = root->left;
873                         if (y == NULL)
874                                 break;
875                         if (addr < y->start && y->left != NULL) {
876                                 /* Rotate right and put y on rlist. */
877                                 root->left = y->right;
878                                 y->right = root;
879                                 vm_map_entry_set_max_free(root);
880                                 root = y->left;
881                                 y->left = rlist;
882                                 rlist = y;
883                         } else {
884                                 /* Put root on rlist. */
885                                 root->left = rlist;
886                                 rlist = root;
887                                 root = y;
888                         }
889                 } else if (addr >= root->end) {
890                         y = root->right;
891                         if (y == NULL)
892                                 break;
893                         if (addr >= y->end && y->right != NULL) {
894                                 /* Rotate left and put y on llist. */
895                                 root->right = y->left;
896                                 y->left = root;
897                                 vm_map_entry_set_max_free(root);
898                                 root = y->right;
899                                 y->right = llist;
900                                 llist = y;
901                         } else {
902                                 /* Put root on llist. */
903                                 root->right = llist;
904                                 llist = root;
905                                 root = y;
906                         }
907                 } else
908                         break;
909         }
910
911         /*
912          * Pass Two: Walk back up the two spines, flip the pointers
913          * and set max_free.  The subtrees of the root go at the
914          * bottom of llist and rlist.
915          */
916         ltree = root->left;
917         while (llist != NULL) {
918                 y = llist->right;
919                 llist->right = ltree;
920                 vm_map_entry_set_max_free(llist);
921                 ltree = llist;
922                 llist = y;
923         }
924         rtree = root->right;
925         while (rlist != NULL) {
926                 y = rlist->left;
927                 rlist->left = rtree;
928                 vm_map_entry_set_max_free(rlist);
929                 rtree = rlist;
930                 rlist = y;
931         }
932
933         /*
934          * Final assembly: add ltree and rtree as subtrees of root.
935          */
936         root->left = ltree;
937         root->right = rtree;
938         vm_map_entry_set_max_free(root);
939
940         return (root);
941 }
942
943 /*
944  *      vm_map_entry_{un,}link:
945  *
946  *      Insert/remove entries from maps.
947  */
948 static void
949 vm_map_entry_link(vm_map_t map,
950                   vm_map_entry_t after_where,
951                   vm_map_entry_t entry)
952 {
953
954         CTR4(KTR_VM,
955             "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
956             map->nentries, entry, after_where);
957         VM_MAP_ASSERT_LOCKED(map);
958         map->nentries++;
959         entry->prev = after_where;
960         entry->next = after_where->next;
961         entry->next->prev = entry;
962         after_where->next = entry;
963
964         if (after_where != &map->header) {
965                 if (after_where != map->root)
966                         vm_map_entry_splay(after_where->start, map->root);
967                 entry->right = after_where->right;
968                 entry->left = after_where;
969                 after_where->right = NULL;
970                 after_where->adj_free = entry->start - after_where->end;
971                 vm_map_entry_set_max_free(after_where);
972         } else {
973                 entry->right = map->root;
974                 entry->left = NULL;
975         }
976         entry->adj_free = (entry->next == &map->header ? map->max_offset :
977             entry->next->start) - entry->end;
978         vm_map_entry_set_max_free(entry);
979         map->root = entry;
980 }
981
982 static void
983 vm_map_entry_unlink(vm_map_t map,
984                     vm_map_entry_t entry)
985 {
986         vm_map_entry_t next, prev, root;
987
988         VM_MAP_ASSERT_LOCKED(map);
989         if (entry != map->root)
990                 vm_map_entry_splay(entry->start, map->root);
991         if (entry->left == NULL)
992                 root = entry->right;
993         else {
994                 root = vm_map_entry_splay(entry->start, entry->left);
995                 root->right = entry->right;
996                 root->adj_free = (entry->next == &map->header ? map->max_offset :
997                     entry->next->start) - root->end;
998                 vm_map_entry_set_max_free(root);
999         }
1000         map->root = root;
1001
1002         prev = entry->prev;
1003         next = entry->next;
1004         next->prev = prev;
1005         prev->next = next;
1006         map->nentries--;
1007         CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1008             map->nentries, entry);
1009 }
1010
1011 /*
1012  *      vm_map_entry_resize_free:
1013  *
1014  *      Recompute the amount of free space following a vm_map_entry
1015  *      and propagate that value up the tree.  Call this function after
1016  *      resizing a map entry in-place, that is, without a call to
1017  *      vm_map_entry_link() or _unlink().
1018  *
1019  *      The map must be locked, and leaves it so.
1020  */
1021 static void
1022 vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
1023 {
1024
1025         /*
1026          * Using splay trees without parent pointers, propagating
1027          * max_free up the tree is done by moving the entry to the
1028          * root and making the change there.
1029          */
1030         if (entry != map->root)
1031                 map->root = vm_map_entry_splay(entry->start, map->root);
1032
1033         entry->adj_free = (entry->next == &map->header ? map->max_offset :
1034             entry->next->start) - entry->end;
1035         vm_map_entry_set_max_free(entry);
1036 }
1037
1038 /*
1039  *      vm_map_lookup_entry:    [ internal use only ]
1040  *
1041  *      Finds the map entry containing (or
1042  *      immediately preceding) the specified address
1043  *      in the given map; the entry is returned
1044  *      in the "entry" parameter.  The boolean
1045  *      result indicates whether the address is
1046  *      actually contained in the map.
1047  */
1048 boolean_t
1049 vm_map_lookup_entry(
1050         vm_map_t map,
1051         vm_offset_t address,
1052         vm_map_entry_t *entry)  /* OUT */
1053 {
1054         vm_map_entry_t cur;
1055         boolean_t locked;
1056
1057         /*
1058          * If the map is empty, then the map entry immediately preceding
1059          * "address" is the map's header.
1060          */
1061         cur = map->root;
1062         if (cur == NULL)
1063                 *entry = &map->header;
1064         else if (address >= cur->start && cur->end > address) {
1065                 *entry = cur;
1066                 return (TRUE);
1067         } else if ((locked = vm_map_locked(map)) ||
1068             sx_try_upgrade(&map->lock)) {
1069                 /*
1070                  * Splay requires a write lock on the map.  However, it only
1071                  * restructures the binary search tree; it does not otherwise
1072                  * change the map.  Thus, the map's timestamp need not change
1073                  * on a temporary upgrade.
1074                  */
1075                 map->root = cur = vm_map_entry_splay(address, cur);
1076                 if (!locked)
1077                         sx_downgrade(&map->lock);
1078
1079                 /*
1080                  * If "address" is contained within a map entry, the new root
1081                  * is that map entry.  Otherwise, the new root is a map entry
1082                  * immediately before or after "address".
1083                  */
1084                 if (address >= cur->start) {
1085                         *entry = cur;
1086                         if (cur->end > address)
1087                                 return (TRUE);
1088                 } else
1089                         *entry = cur->prev;
1090         } else
1091                 /*
1092                  * Since the map is only locked for read access, perform a
1093                  * standard binary search tree lookup for "address".
1094                  */
1095                 for (;;) {
1096                         if (address < cur->start) {
1097                                 if (cur->left == NULL) {
1098                                         *entry = cur->prev;
1099                                         break;
1100                                 }
1101                                 cur = cur->left;
1102                         } else if (cur->end > address) {
1103                                 *entry = cur;
1104                                 return (TRUE);
1105                         } else {
1106                                 if (cur->right == NULL) {
1107                                         *entry = cur;
1108                                         break;
1109                                 }
1110                                 cur = cur->right;
1111                         }
1112                 }
1113         return (FALSE);
1114 }
1115
1116 /*
1117  *      vm_map_insert:
1118  *
1119  *      Inserts the given whole VM object into the target
1120  *      map at the specified address range.  The object's
1121  *      size should match that of the address range.
1122  *
1123  *      Requires that the map be locked, and leaves it so.
1124  *
1125  *      If object is non-NULL, ref count must be bumped by caller
1126  *      prior to making call to account for the new entry.
1127  */
1128 int
1129 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1130               vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1131               int cow)
1132 {
1133         vm_map_entry_t new_entry;
1134         vm_map_entry_t prev_entry;
1135         vm_map_entry_t temp_entry;
1136         vm_eflags_t protoeflags;
1137         struct uidinfo *uip;
1138         boolean_t charge_prev_obj;
1139
1140         VM_MAP_ASSERT_LOCKED(map);
1141
1142         /*
1143          * Check that the start and end points are not bogus.
1144          */
1145         if ((start < map->min_offset) || (end > map->max_offset) ||
1146             (start >= end))
1147                 return (KERN_INVALID_ADDRESS);
1148
1149         /*
1150          * Find the entry prior to the proposed starting address; if it's part
1151          * of an existing entry, this range is bogus.
1152          */
1153         if (vm_map_lookup_entry(map, start, &temp_entry))
1154                 return (KERN_NO_SPACE);
1155
1156         prev_entry = temp_entry;
1157
1158         /*
1159          * Assert that the next entry doesn't overlap the end point.
1160          */
1161         if ((prev_entry->next != &map->header) &&
1162             (prev_entry->next->start < end))
1163                 return (KERN_NO_SPACE);
1164
1165         protoeflags = 0;
1166         charge_prev_obj = FALSE;
1167
1168         if (cow & MAP_COPY_ON_WRITE)
1169                 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1170
1171         if (cow & MAP_NOFAULT) {
1172                 protoeflags |= MAP_ENTRY_NOFAULT;
1173
1174                 KASSERT(object == NULL,
1175                         ("vm_map_insert: paradoxical MAP_NOFAULT request"));
1176         }
1177         if (cow & MAP_DISABLE_SYNCER)
1178                 protoeflags |= MAP_ENTRY_NOSYNC;
1179         if (cow & MAP_DISABLE_COREDUMP)
1180                 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1181
1182         uip = NULL;
1183         KASSERT((object != kmem_object && object != kernel_object) ||
1184             ((object == kmem_object || object == kernel_object) &&
1185                 !(protoeflags & MAP_ENTRY_NEEDS_COPY)),
1186             ("kmem or kernel object and cow"));
1187         if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
1188                 goto charged;
1189         if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1190             ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1191                 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1192                         return (KERN_RESOURCE_SHORTAGE);
1193                 KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
1194                     object->uip == NULL,
1195                     ("OVERCOMMIT: vm_map_insert o %p", object));
1196                 uip = curthread->td_ucred->cr_ruidinfo;
1197                 uihold(uip);
1198                 if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
1199                         charge_prev_obj = TRUE;
1200         }
1201
1202 charged:
1203         /* Expand the kernel pmap, if necessary. */
1204         if (map == kernel_map && end > kernel_vm_end)
1205                 pmap_growkernel(end);
1206         if (object != NULL) {
1207                 /*
1208                  * OBJ_ONEMAPPING must be cleared unless this mapping
1209                  * is trivially proven to be the only mapping for any
1210                  * of the object's pages.  (Object granularity
1211                  * reference counting is insufficient to recognize
1212                  * aliases with precision.)
1213                  */
1214                 VM_OBJECT_LOCK(object);
1215                 if (object->ref_count > 1 || object->shadow_count != 0)
1216                         vm_object_clear_flag(object, OBJ_ONEMAPPING);
1217                 VM_OBJECT_UNLOCK(object);
1218         }
1219         else if ((prev_entry != &map->header) &&
1220                  (prev_entry->eflags == protoeflags) &&
1221                  (prev_entry->end == start) &&
1222                  (prev_entry->wired_count == 0) &&
1223                  (prev_entry->uip == uip ||
1224                   (prev_entry->object.vm_object != NULL &&
1225                    (prev_entry->object.vm_object->uip == uip))) &&
1226                    vm_object_coalesce(prev_entry->object.vm_object,
1227                        prev_entry->offset,
1228                        (vm_size_t)(prev_entry->end - prev_entry->start),
1229                        (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
1230                 /*
1231                  * We were able to extend the object.  Determine if we
1232                  * can extend the previous map entry to include the
1233                  * new range as well.
1234                  */
1235                 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
1236                     (prev_entry->protection == prot) &&
1237                     (prev_entry->max_protection == max)) {
1238                         map->size += (end - prev_entry->end);
1239                         prev_entry->end = end;
1240                         vm_map_entry_resize_free(map, prev_entry);
1241                         vm_map_simplify_entry(map, prev_entry);
1242                         if (uip != NULL)
1243                                 uifree(uip);
1244                         return (KERN_SUCCESS);
1245                 }
1246
1247                 /*
1248                  * If we can extend the object but cannot extend the
1249                  * map entry, we have to create a new map entry.  We
1250                  * must bump the ref count on the extended object to
1251                  * account for it.  object may be NULL.
1252                  */
1253                 object = prev_entry->object.vm_object;
1254                 offset = prev_entry->offset +
1255                         (prev_entry->end - prev_entry->start);
1256                 vm_object_reference(object);
1257                 if (uip != NULL && object != NULL && object->uip != NULL &&
1258                     !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1259                         /* Object already accounts for this uid. */
1260                         uifree(uip);
1261                         uip = NULL;
1262                 }
1263         }
1264
1265         /*
1266          * NOTE: if conditionals fail, object can be NULL here.  This occurs
1267          * in things like the buffer map where we manage kva but do not manage
1268          * backing objects.
1269          */
1270
1271         /*
1272          * Create a new entry
1273          */
1274         new_entry = vm_map_entry_create(map);
1275         new_entry->start = start;
1276         new_entry->end = end;
1277         new_entry->uip = NULL;
1278
1279         new_entry->eflags = protoeflags;
1280         new_entry->object.vm_object = object;
1281         new_entry->offset = offset;
1282         new_entry->avail_ssize = 0;
1283
1284         new_entry->inheritance = VM_INHERIT_DEFAULT;
1285         new_entry->protection = prot;
1286         new_entry->max_protection = max;
1287         new_entry->wired_count = 0;
1288
1289         KASSERT(uip == NULL || !ENTRY_CHARGED(new_entry),
1290             ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
1291         new_entry->uip = uip;
1292
1293         /*
1294          * Insert the new entry into the list
1295          */
1296         vm_map_entry_link(map, prev_entry, new_entry);
1297         map->size += new_entry->end - new_entry->start;
1298
1299 #if 0
1300         /*
1301          * Temporarily removed to avoid MAP_STACK panic, due to
1302          * MAP_STACK being a huge hack.  Will be added back in
1303          * when MAP_STACK (and the user stack mapping) is fixed.
1304          */
1305         /*
1306          * It may be possible to simplify the entry
1307          */
1308         vm_map_simplify_entry(map, new_entry);
1309 #endif
1310
1311         if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
1312                 vm_map_pmap_enter(map, start, prot,
1313                                     object, OFF_TO_IDX(offset), end - start,
1314                                     cow & MAP_PREFAULT_PARTIAL);
1315         }
1316
1317         return (KERN_SUCCESS);
1318 }
1319
1320 /*
1321  *      vm_map_findspace:
1322  *
1323  *      Find the first fit (lowest VM address) for "length" free bytes
1324  *      beginning at address >= start in the given map.
1325  *
1326  *      In a vm_map_entry, "adj_free" is the amount of free space
1327  *      adjacent (higher address) to this entry, and "max_free" is the
1328  *      maximum amount of contiguous free space in its subtree.  This
1329  *      allows finding a free region in one path down the tree, so
1330  *      O(log n) amortized with splay trees.
1331  *
1332  *      The map must be locked, and leaves it so.
1333  *
1334  *      Returns: 0 on success, and starting address in *addr,
1335  *               1 if insufficient space.
1336  */
1337 int
1338 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
1339     vm_offset_t *addr)  /* OUT */
1340 {
1341         vm_map_entry_t entry;
1342         vm_offset_t st;
1343
1344         /*
1345          * Request must fit within min/max VM address and must avoid
1346          * address wrap.
1347          */
1348         if (start < map->min_offset)
1349                 start = map->min_offset;
1350         if (start + length > map->max_offset || start + length < start)
1351                 return (1);
1352
1353         /* Empty tree means wide open address space. */
1354         if (map->root == NULL) {
1355                 *addr = start;
1356                 return (0);
1357         }
1358
1359         /*
1360          * After splay, if start comes before root node, then there
1361          * must be a gap from start to the root.
1362          */
1363         map->root = vm_map_entry_splay(start, map->root);
1364         if (start + length <= map->root->start) {
1365                 *addr = start;
1366                 return (0);
1367         }
1368
1369         /*
1370          * Root is the last node that might begin its gap before
1371          * start, and this is the last comparison where address
1372          * wrap might be a problem.
1373          */
1374         st = (start > map->root->end) ? start : map->root->end;
1375         if (length <= map->root->end + map->root->adj_free - st) {
1376                 *addr = st;
1377                 return (0);
1378         }
1379
1380         /* With max_free, can immediately tell if no solution. */
1381         entry = map->root->right;
1382         if (entry == NULL || length > entry->max_free)
1383                 return (1);
1384
1385         /*
1386          * Search the right subtree in the order: left subtree, root,
1387          * right subtree (first fit).  The previous splay implies that
1388          * all regions in the right subtree have addresses > start.
1389          */
1390         while (entry != NULL) {
1391                 if (entry->left != NULL && entry->left->max_free >= length)
1392                         entry = entry->left;
1393                 else if (entry->adj_free >= length) {
1394                         *addr = entry->end;
1395                         return (0);
1396                 } else
1397                         entry = entry->right;
1398         }
1399
1400         /* Can't get here, so panic if we do. */
1401         panic("vm_map_findspace: max_free corrupt");
1402 }
1403
1404 int
1405 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1406     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1407     vm_prot_t max, int cow)
1408 {
1409         vm_offset_t end;
1410         int result;
1411
1412         end = start + length;
1413         vm_map_lock(map);
1414         VM_MAP_RANGE_CHECK(map, start, end);
1415         (void) vm_map_delete(map, start, end);
1416         result = vm_map_insert(map, object, offset, start, end, prot,
1417             max, cow);
1418         vm_map_unlock(map);
1419         return (result);
1420 }
1421
1422 /*
1423  *      vm_map_find finds an unallocated region in the target address
1424  *      map with the given length.  The search is defined to be
1425  *      first-fit from the specified address; the region found is
1426  *      returned in the same parameter.
1427  *
1428  *      If object is non-NULL, ref count must be bumped by caller
1429  *      prior to making call to account for the new entry.
1430  */
1431 int
1432 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1433             vm_offset_t *addr,  /* IN/OUT */
1434             vm_size_t length, int find_space, vm_prot_t prot,
1435             vm_prot_t max, int cow)
1436 {
1437         vm_offset_t start;
1438         int result;
1439
1440         start = *addr;
1441         vm_map_lock(map);
1442         do {
1443                 if (find_space != VMFS_NO_SPACE) {
1444                         if (vm_map_findspace(map, start, length, addr)) {
1445                                 vm_map_unlock(map);
1446                                 return (KERN_NO_SPACE);
1447                         }
1448                         switch (find_space) {
1449                         case VMFS_ALIGNED_SPACE:
1450                                 pmap_align_superpage(object, offset, addr,
1451                                     length);
1452                                 break;
1453 #ifdef VMFS_TLB_ALIGNED_SPACE
1454                         case VMFS_TLB_ALIGNED_SPACE:
1455                                 pmap_align_tlb(addr);
1456                                 break;
1457 #endif
1458                         default:
1459                                 break;
1460                         }
1461
1462                         start = *addr;
1463                 }
1464                 result = vm_map_insert(map, object, offset, start, start +
1465                     length, prot, max, cow);
1466         } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE);
1467         vm_map_unlock(map);
1468         return (result);
1469 }
1470
1471 /*
1472  *      vm_map_simplify_entry:
1473  *
1474  *      Simplify the given map entry by merging with either neighbor.  This
1475  *      routine also has the ability to merge with both neighbors.
1476  *
1477  *      The map must be locked.
1478  *
1479  *      This routine guarentees that the passed entry remains valid (though
1480  *      possibly extended).  When merging, this routine may delete one or
1481  *      both neighbors.
1482  */
1483 void
1484 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1485 {
1486         vm_map_entry_t next, prev;
1487         vm_size_t prevsize, esize;
1488
1489         if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1490                 return;
1491
1492         prev = entry->prev;
1493         if (prev != &map->header) {
1494                 prevsize = prev->end - prev->start;
1495                 if ( (prev->end == entry->start) &&
1496                      (prev->object.vm_object == entry->object.vm_object) &&
1497                      (!prev->object.vm_object ||
1498                         (prev->offset + prevsize == entry->offset)) &&
1499                      (prev->eflags == entry->eflags) &&
1500                      (prev->protection == entry->protection) &&
1501                      (prev->max_protection == entry->max_protection) &&
1502                      (prev->inheritance == entry->inheritance) &&
1503                      (prev->wired_count == entry->wired_count) &&
1504                      (prev->uip == entry->uip)) {
1505                         vm_map_entry_unlink(map, prev);
1506                         entry->start = prev->start;
1507                         entry->offset = prev->offset;
1508                         if (entry->prev != &map->header)
1509                                 vm_map_entry_resize_free(map, entry->prev);
1510
1511                         /*
1512                          * If the backing object is a vnode object,
1513                          * vm_object_deallocate() calls vrele().
1514                          * However, vrele() does not lock the vnode
1515                          * because the vnode has additional
1516                          * references.  Thus, the map lock can be kept
1517                          * without causing a lock-order reversal with
1518                          * the vnode lock.
1519                          */
1520                         if (prev->object.vm_object)
1521                                 vm_object_deallocate(prev->object.vm_object);
1522                         if (prev->uip != NULL)
1523                                 uifree(prev->uip);
1524                         vm_map_entry_dispose(map, prev);
1525                 }
1526         }
1527
1528         next = entry->next;
1529         if (next != &map->header) {
1530                 esize = entry->end - entry->start;
1531                 if ((entry->end == next->start) &&
1532                     (next->object.vm_object == entry->object.vm_object) &&
1533                      (!entry->object.vm_object ||
1534                         (entry->offset + esize == next->offset)) &&
1535                     (next->eflags == entry->eflags) &&
1536                     (next->protection == entry->protection) &&
1537                     (next->max_protection == entry->max_protection) &&
1538                     (next->inheritance == entry->inheritance) &&
1539                     (next->wired_count == entry->wired_count) &&
1540                     (next->uip == entry->uip)) {
1541                         vm_map_entry_unlink(map, next);
1542                         entry->end = next->end;
1543                         vm_map_entry_resize_free(map, entry);
1544
1545                         /*
1546                          * See comment above.
1547                          */
1548                         if (next->object.vm_object)
1549                                 vm_object_deallocate(next->object.vm_object);
1550                         if (next->uip != NULL)
1551                                 uifree(next->uip);
1552                         vm_map_entry_dispose(map, next);
1553                 }
1554         }
1555 }
1556 /*
1557  *      vm_map_clip_start:      [ internal use only ]
1558  *
1559  *      Asserts that the given entry begins at or after
1560  *      the specified address; if necessary,
1561  *      it splits the entry into two.
1562  */
1563 #define vm_map_clip_start(map, entry, startaddr) \
1564 { \
1565         if (startaddr > entry->start) \
1566                 _vm_map_clip_start(map, entry, startaddr); \
1567 }
1568
1569 /*
1570  *      This routine is called only when it is known that
1571  *      the entry must be split.
1572  */
1573 static void
1574 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1575 {
1576         vm_map_entry_t new_entry;
1577
1578         VM_MAP_ASSERT_LOCKED(map);
1579
1580         /*
1581          * Split off the front portion -- note that we must insert the new
1582          * entry BEFORE this one, so that this entry has the specified
1583          * starting address.
1584          */
1585         vm_map_simplify_entry(map, entry);
1586
1587         /*
1588          * If there is no object backing this entry, we might as well create
1589          * one now.  If we defer it, an object can get created after the map
1590          * is clipped, and individual objects will be created for the split-up
1591          * map.  This is a bit of a hack, but is also about the best place to
1592          * put this improvement.
1593          */
1594         if (entry->object.vm_object == NULL && !map->system_map) {
1595                 vm_object_t object;
1596                 object = vm_object_allocate(OBJT_DEFAULT,
1597                                 atop(entry->end - entry->start));
1598                 entry->object.vm_object = object;
1599                 entry->offset = 0;
1600                 if (entry->uip != NULL) {
1601                         object->uip = entry->uip;
1602                         object->charge = entry->end - entry->start;
1603                         entry->uip = NULL;
1604                 }
1605         } else if (entry->object.vm_object != NULL &&
1606                    ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1607                    entry->uip != NULL) {
1608                 VM_OBJECT_LOCK(entry->object.vm_object);
1609                 KASSERT(entry->object.vm_object->uip == NULL,
1610                     ("OVERCOMMIT: vm_entry_clip_start: both uip e %p", entry));
1611                 entry->object.vm_object->uip = entry->uip;
1612                 entry->object.vm_object->charge = entry->end - entry->start;
1613                 VM_OBJECT_UNLOCK(entry->object.vm_object);
1614                 entry->uip = NULL;
1615         }
1616
1617         new_entry = vm_map_entry_create(map);
1618         *new_entry = *entry;
1619
1620         new_entry->end = start;
1621         entry->offset += (start - entry->start);
1622         entry->start = start;
1623         if (new_entry->uip != NULL)
1624                 uihold(entry->uip);
1625
1626         vm_map_entry_link(map, entry->prev, new_entry);
1627
1628         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1629                 vm_object_reference(new_entry->object.vm_object);
1630         }
1631 }
1632
1633 /*
1634  *      vm_map_clip_end:        [ internal use only ]
1635  *
1636  *      Asserts that the given entry ends at or before
1637  *      the specified address; if necessary,
1638  *      it splits the entry into two.
1639  */
1640 #define vm_map_clip_end(map, entry, endaddr) \
1641 { \
1642         if ((endaddr) < (entry->end)) \
1643                 _vm_map_clip_end((map), (entry), (endaddr)); \
1644 }
1645
1646 /*
1647  *      This routine is called only when it is known that
1648  *      the entry must be split.
1649  */
1650 static void
1651 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1652 {
1653         vm_map_entry_t new_entry;
1654
1655         VM_MAP_ASSERT_LOCKED(map);
1656
1657         /*
1658          * If there is no object backing this entry, we might as well create
1659          * one now.  If we defer it, an object can get created after the map
1660          * is clipped, and individual objects will be created for the split-up
1661          * map.  This is a bit of a hack, but is also about the best place to
1662          * put this improvement.
1663          */
1664         if (entry->object.vm_object == NULL && !map->system_map) {
1665                 vm_object_t object;
1666                 object = vm_object_allocate(OBJT_DEFAULT,
1667                                 atop(entry->end - entry->start));
1668                 entry->object.vm_object = object;
1669                 entry->offset = 0;
1670                 if (entry->uip != NULL) {
1671                         object->uip = entry->uip;
1672                         object->charge = entry->end - entry->start;
1673                         entry->uip = NULL;
1674                 }
1675         } else if (entry->object.vm_object != NULL &&
1676                    ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1677                    entry->uip != NULL) {
1678                 VM_OBJECT_LOCK(entry->object.vm_object);
1679                 KASSERT(entry->object.vm_object->uip == NULL,
1680                     ("OVERCOMMIT: vm_entry_clip_end: both uip e %p", entry));
1681                 entry->object.vm_object->uip = entry->uip;
1682                 entry->object.vm_object->charge = entry->end - entry->start;
1683                 VM_OBJECT_UNLOCK(entry->object.vm_object);
1684                 entry->uip = NULL;
1685         }
1686
1687         /*
1688          * Create a new entry and insert it AFTER the specified entry
1689          */
1690         new_entry = vm_map_entry_create(map);
1691         *new_entry = *entry;
1692
1693         new_entry->start = entry->end = end;
1694         new_entry->offset += (end - entry->start);
1695         if (new_entry->uip != NULL)
1696                 uihold(entry->uip);
1697
1698         vm_map_entry_link(map, entry, new_entry);
1699
1700         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1701                 vm_object_reference(new_entry->object.vm_object);
1702         }
1703 }
1704
1705 /*
1706  *      vm_map_submap:          [ kernel use only ]
1707  *
1708  *      Mark the given range as handled by a subordinate map.
1709  *
1710  *      This range must have been created with vm_map_find,
1711  *      and no other operations may have been performed on this
1712  *      range prior to calling vm_map_submap.
1713  *
1714  *      Only a limited number of operations can be performed
1715  *      within this rage after calling vm_map_submap:
1716  *              vm_fault
1717  *      [Don't try vm_map_copy!]
1718  *
1719  *      To remove a submapping, one must first remove the
1720  *      range from the superior map, and then destroy the
1721  *      submap (if desired).  [Better yet, don't try it.]
1722  */
1723 int
1724 vm_map_submap(
1725         vm_map_t map,
1726         vm_offset_t start,
1727         vm_offset_t end,
1728         vm_map_t submap)
1729 {
1730         vm_map_entry_t entry;
1731         int result = KERN_INVALID_ARGUMENT;
1732
1733         vm_map_lock(map);
1734
1735         VM_MAP_RANGE_CHECK(map, start, end);
1736
1737         if (vm_map_lookup_entry(map, start, &entry)) {
1738                 vm_map_clip_start(map, entry, start);
1739         } else
1740                 entry = entry->next;
1741
1742         vm_map_clip_end(map, entry, end);
1743
1744         if ((entry->start == start) && (entry->end == end) &&
1745             ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1746             (entry->object.vm_object == NULL)) {
1747                 entry->object.sub_map = submap;
1748                 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1749                 result = KERN_SUCCESS;
1750         }
1751         vm_map_unlock(map);
1752
1753         return (result);
1754 }
1755
1756 /*
1757  * The maximum number of pages to map
1758  */
1759 #define MAX_INIT_PT     96
1760
1761 /*
1762  *      vm_map_pmap_enter:
1763  *
1764  *      Preload read-only mappings for the given object's resident pages into
1765  *      the given map.  This eliminates the soft faults on process startup and
1766  *      immediately after an mmap(2).  Because these are speculative mappings,
1767  *      cached pages are not reactivated and mapped.
1768  */
1769 void
1770 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
1771     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
1772 {
1773         vm_offset_t start;
1774         vm_page_t p, p_start;
1775         vm_pindex_t psize, tmpidx;
1776         boolean_t are_queues_locked;
1777
1778         if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
1779                 return;
1780         VM_OBJECT_LOCK(object);
1781         if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
1782                 pmap_object_init_pt(map->pmap, addr, object, pindex, size);
1783                 goto unlock_return;
1784         }
1785
1786         psize = atop(size);
1787
1788         if ((flags & MAP_PREFAULT_PARTIAL) && psize > MAX_INIT_PT &&
1789             object->resident_page_count > MAX_INIT_PT)
1790                 goto unlock_return;
1791
1792         if (psize + pindex > object->size) {
1793                 if (object->size < pindex)
1794                         goto unlock_return;
1795                 psize = object->size - pindex;
1796         }
1797
1798         are_queues_locked = FALSE;
1799         start = 0;
1800         p_start = NULL;
1801
1802         p = vm_page_find_least(object, pindex);
1803         /*
1804          * Assert: the variable p is either (1) the page with the
1805          * least pindex greater than or equal to the parameter pindex
1806          * or (2) NULL.
1807          */
1808         for (;
1809              p != NULL && (tmpidx = p->pindex - pindex) < psize;
1810              p = TAILQ_NEXT(p, listq)) {
1811                 /*
1812                  * don't allow an madvise to blow away our really
1813                  * free pages allocating pv entries.
1814                  */
1815                 if ((flags & MAP_PREFAULT_MADVISE) &&
1816                     cnt.v_free_count < cnt.v_free_reserved) {
1817                         psize = tmpidx;
1818                         break;
1819                 }
1820                 if (p->valid == VM_PAGE_BITS_ALL) {
1821                         if (p_start == NULL) {
1822                                 start = addr + ptoa(tmpidx);
1823                                 p_start = p;
1824                         }
1825                 } else if (p_start != NULL) {
1826                         if (!are_queues_locked) {
1827                                 are_queues_locked = TRUE;
1828                                 vm_page_lock_queues();
1829                         }
1830                         pmap_enter_object(map->pmap, start, addr +
1831                             ptoa(tmpidx), p_start, prot);
1832                         p_start = NULL;
1833                 }
1834         }
1835         if (p_start != NULL) {
1836                 if (!are_queues_locked) {
1837                         are_queues_locked = TRUE;
1838                         vm_page_lock_queues();
1839                 }
1840                 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1841                     p_start, prot);
1842         }
1843         if (are_queues_locked)
1844                 vm_page_unlock_queues();
1845 unlock_return:
1846         VM_OBJECT_UNLOCK(object);
1847 }
1848
1849 /*
1850  *      vm_map_protect:
1851  *
1852  *      Sets the protection of the specified address
1853  *      region in the target map.  If "set_max" is
1854  *      specified, the maximum protection is to be set;
1855  *      otherwise, only the current protection is affected.
1856  */
1857 int
1858 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1859                vm_prot_t new_prot, boolean_t set_max)
1860 {
1861         vm_map_entry_t current, entry;
1862         vm_object_t obj;
1863         struct uidinfo *uip;
1864         vm_prot_t old_prot;
1865
1866         vm_map_lock(map);
1867
1868         VM_MAP_RANGE_CHECK(map, start, end);
1869
1870         if (vm_map_lookup_entry(map, start, &entry)) {
1871                 vm_map_clip_start(map, entry, start);
1872         } else {
1873                 entry = entry->next;
1874         }
1875
1876         /*
1877          * Make a first pass to check for protection violations.
1878          */
1879         current = entry;
1880         while ((current != &map->header) && (current->start < end)) {
1881                 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1882                         vm_map_unlock(map);
1883                         return (KERN_INVALID_ARGUMENT);
1884                 }
1885                 if ((new_prot & current->max_protection) != new_prot) {
1886                         vm_map_unlock(map);
1887                         return (KERN_PROTECTION_FAILURE);
1888                 }
1889                 current = current->next;
1890         }
1891
1892
1893         /*
1894          * Do an accounting pass for private read-only mappings that
1895          * now will do cow due to allowed write (e.g. debugger sets
1896          * breakpoint on text segment)
1897          */
1898         for (current = entry; (current != &map->header) &&
1899              (current->start < end); current = current->next) {
1900
1901                 vm_map_clip_end(map, current, end);
1902
1903                 if (set_max ||
1904                     ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
1905                     ENTRY_CHARGED(current)) {
1906                         continue;
1907                 }
1908
1909                 uip = curthread->td_ucred->cr_ruidinfo;
1910                 obj = current->object.vm_object;
1911
1912                 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
1913                         if (!swap_reserve(current->end - current->start)) {
1914                                 vm_map_unlock(map);
1915                                 return (KERN_RESOURCE_SHORTAGE);
1916                         }
1917                         uihold(uip);
1918                         current->uip = uip;
1919                         continue;
1920                 }
1921
1922                 VM_OBJECT_LOCK(obj);
1923                 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
1924                         VM_OBJECT_UNLOCK(obj);
1925                         continue;
1926                 }
1927
1928                 /*
1929                  * Charge for the whole object allocation now, since
1930                  * we cannot distinguish between non-charged and
1931                  * charged clipped mapping of the same object later.
1932                  */
1933                 KASSERT(obj->charge == 0,
1934                     ("vm_map_protect: object %p overcharged\n", obj));
1935                 if (!swap_reserve(ptoa(obj->size))) {
1936                         VM_OBJECT_UNLOCK(obj);
1937                         vm_map_unlock(map);
1938                         return (KERN_RESOURCE_SHORTAGE);
1939                 }
1940
1941                 uihold(uip);
1942                 obj->uip = uip;
1943                 obj->charge = ptoa(obj->size);
1944                 VM_OBJECT_UNLOCK(obj);
1945         }
1946
1947         /*
1948          * Go back and fix up protections. [Note that clipping is not
1949          * necessary the second time.]
1950          */
1951         current = entry;
1952         while ((current != &map->header) && (current->start < end)) {
1953                 old_prot = current->protection;
1954
1955                 if (set_max)
1956                         current->protection =
1957                             (current->max_protection = new_prot) &
1958                             old_prot;
1959                 else
1960                         current->protection = new_prot;
1961
1962                 if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1963                      == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1964                     (current->protection & VM_PROT_WRITE) != 0 &&
1965                     (old_prot & VM_PROT_WRITE) == 0) {
1966                         vm_fault_copy_entry(map, map, current, current, NULL);
1967                 }
1968
1969                 /*
1970                  * Update physical map if necessary. Worry about copy-on-write
1971                  * here.
1972                  */
1973                 if (current->protection != old_prot) {
1974 #define MASK(entry)     (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1975                                                         VM_PROT_ALL)
1976                         pmap_protect(map->pmap, current->start,
1977                             current->end,
1978                             current->protection & MASK(current));
1979 #undef  MASK
1980                 }
1981                 vm_map_simplify_entry(map, current);
1982                 current = current->next;
1983         }
1984         vm_map_unlock(map);
1985         return (KERN_SUCCESS);
1986 }
1987
1988 /*
1989  *      vm_map_madvise:
1990  *
1991  *      This routine traverses a processes map handling the madvise
1992  *      system call.  Advisories are classified as either those effecting
1993  *      the vm_map_entry structure, or those effecting the underlying
1994  *      objects.
1995  */
1996 int
1997 vm_map_madvise(
1998         vm_map_t map,
1999         vm_offset_t start,
2000         vm_offset_t end,
2001         int behav)
2002 {
2003         vm_map_entry_t current, entry;
2004         int modify_map = 0;
2005
2006         /*
2007          * Some madvise calls directly modify the vm_map_entry, in which case
2008          * we need to use an exclusive lock on the map and we need to perform
2009          * various clipping operations.  Otherwise we only need a read-lock
2010          * on the map.
2011          */
2012         switch(behav) {
2013         case MADV_NORMAL:
2014         case MADV_SEQUENTIAL:
2015         case MADV_RANDOM:
2016         case MADV_NOSYNC:
2017         case MADV_AUTOSYNC:
2018         case MADV_NOCORE:
2019         case MADV_CORE:
2020                 modify_map = 1;
2021                 vm_map_lock(map);
2022                 break;
2023         case MADV_WILLNEED:
2024         case MADV_DONTNEED:
2025         case MADV_FREE:
2026                 vm_map_lock_read(map);
2027                 break;
2028         default:
2029                 return (KERN_INVALID_ARGUMENT);
2030         }
2031
2032         /*
2033          * Locate starting entry and clip if necessary.
2034          */
2035         VM_MAP_RANGE_CHECK(map, start, end);
2036
2037         if (vm_map_lookup_entry(map, start, &entry)) {
2038                 if (modify_map)
2039                         vm_map_clip_start(map, entry, start);
2040         } else {
2041                 entry = entry->next;
2042         }
2043
2044         if (modify_map) {
2045                 /*
2046                  * madvise behaviors that are implemented in the vm_map_entry.
2047                  *
2048                  * We clip the vm_map_entry so that behavioral changes are
2049                  * limited to the specified address range.
2050                  */
2051                 for (current = entry;
2052                      (current != &map->header) && (current->start < end);
2053                      current = current->next
2054                 ) {
2055                         if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2056                                 continue;
2057
2058                         vm_map_clip_end(map, current, end);
2059
2060                         switch (behav) {
2061                         case MADV_NORMAL:
2062                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2063                                 break;
2064                         case MADV_SEQUENTIAL:
2065                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2066                                 break;
2067                         case MADV_RANDOM:
2068                                 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2069                                 break;
2070                         case MADV_NOSYNC:
2071                                 current->eflags |= MAP_ENTRY_NOSYNC;
2072                                 break;
2073                         case MADV_AUTOSYNC:
2074                                 current->eflags &= ~MAP_ENTRY_NOSYNC;
2075                                 break;
2076                         case MADV_NOCORE:
2077                                 current->eflags |= MAP_ENTRY_NOCOREDUMP;
2078                                 break;
2079                         case MADV_CORE:
2080                                 current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
2081                                 break;
2082                         default:
2083                                 break;
2084                         }
2085                         vm_map_simplify_entry(map, current);
2086                 }
2087                 vm_map_unlock(map);
2088         } else {
2089                 vm_pindex_t pindex;
2090                 int count;
2091
2092                 /*
2093                  * madvise behaviors that are implemented in the underlying
2094                  * vm_object.
2095                  *
2096                  * Since we don't clip the vm_map_entry, we have to clip
2097                  * the vm_object pindex and count.
2098                  */
2099                 for (current = entry;
2100                      (current != &map->header) && (current->start < end);
2101                      current = current->next
2102                 ) {
2103                         vm_offset_t useStart;
2104
2105                         if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2106                                 continue;
2107
2108                         pindex = OFF_TO_IDX(current->offset);
2109                         count = atop(current->end - current->start);
2110                         useStart = current->start;
2111
2112                         if (current->start < start) {
2113                                 pindex += atop(start - current->start);
2114                                 count -= atop(start - current->start);
2115                                 useStart = start;
2116                         }
2117                         if (current->end > end)
2118                                 count -= atop(current->end - end);
2119
2120                         if (count <= 0)
2121                                 continue;
2122
2123                         vm_object_madvise(current->object.vm_object,
2124                                           pindex, count, behav);
2125                         if (behav == MADV_WILLNEED) {
2126                                 vm_map_pmap_enter(map,
2127                                     useStart,
2128                                     current->protection,
2129                                     current->object.vm_object,
2130                                     pindex,
2131                                     (count << PAGE_SHIFT),
2132                                     MAP_PREFAULT_MADVISE
2133                                 );
2134                         }
2135                 }
2136                 vm_map_unlock_read(map);
2137         }
2138         return (0);
2139 }
2140
2141
2142 /*
2143  *      vm_map_inherit:
2144  *
2145  *      Sets the inheritance of the specified address
2146  *      range in the target map.  Inheritance
2147  *      affects how the map will be shared with
2148  *      child maps at the time of vmspace_fork.
2149  */
2150 int
2151 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2152                vm_inherit_t new_inheritance)
2153 {
2154         vm_map_entry_t entry;
2155         vm_map_entry_t temp_entry;
2156
2157         switch (new_inheritance) {
2158         case VM_INHERIT_NONE:
2159         case VM_INHERIT_COPY:
2160         case VM_INHERIT_SHARE:
2161                 break;
2162         default:
2163                 return (KERN_INVALID_ARGUMENT);
2164         }
2165         vm_map_lock(map);
2166         VM_MAP_RANGE_CHECK(map, start, end);
2167         if (vm_map_lookup_entry(map, start, &temp_entry)) {
2168                 entry = temp_entry;
2169                 vm_map_clip_start(map, entry, start);
2170         } else
2171                 entry = temp_entry->next;
2172         while ((entry != &map->header) && (entry->start < end)) {
2173                 vm_map_clip_end(map, entry, end);
2174                 entry->inheritance = new_inheritance;
2175                 vm_map_simplify_entry(map, entry);
2176                 entry = entry->next;
2177         }
2178         vm_map_unlock(map);
2179         return (KERN_SUCCESS);
2180 }
2181
2182 /*
2183  *      vm_map_unwire:
2184  *
2185  *      Implements both kernel and user unwiring.
2186  */
2187 int
2188 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2189     int flags)
2190 {
2191         vm_map_entry_t entry, first_entry, tmp_entry;
2192         vm_offset_t saved_start;
2193         unsigned int last_timestamp;
2194         int rv;
2195         boolean_t need_wakeup, result, user_unwire;
2196
2197         user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2198         vm_map_lock(map);
2199         VM_MAP_RANGE_CHECK(map, start, end);
2200         if (!vm_map_lookup_entry(map, start, &first_entry)) {
2201                 if (flags & VM_MAP_WIRE_HOLESOK)
2202                         first_entry = first_entry->next;
2203                 else {
2204                         vm_map_unlock(map);
2205                         return (KERN_INVALID_ADDRESS);
2206                 }
2207         }
2208         last_timestamp = map->timestamp;
2209         entry = first_entry;
2210         while (entry != &map->header && entry->start < end) {
2211                 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2212                         /*
2213                          * We have not yet clipped the entry.
2214                          */
2215                         saved_start = (start >= entry->start) ? start :
2216                             entry->start;
2217                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2218                         if (vm_map_unlock_and_wait(map, 0)) {
2219                                 /*
2220                                  * Allow interruption of user unwiring?
2221                                  */
2222                         }
2223                         vm_map_lock(map);
2224                         if (last_timestamp+1 != map->timestamp) {
2225                                 /*
2226                                  * Look again for the entry because the map was
2227                                  * modified while it was unlocked.
2228                                  * Specifically, the entry may have been
2229                                  * clipped, merged, or deleted.
2230                                  */
2231                                 if (!vm_map_lookup_entry(map, saved_start,
2232                                     &tmp_entry)) {
2233                                         if (flags & VM_MAP_WIRE_HOLESOK)
2234                                                 tmp_entry = tmp_entry->next;
2235                                         else {
2236                                                 if (saved_start == start) {
2237                                                         /*
2238                                                          * First_entry has been deleted.
2239                                                          */
2240                                                         vm_map_unlock(map);
2241                                                         return (KERN_INVALID_ADDRESS);
2242                                                 }
2243                                                 end = saved_start;
2244                                                 rv = KERN_INVALID_ADDRESS;
2245                                                 goto done;
2246                                         }
2247                                 }
2248                                 if (entry == first_entry)
2249                                         first_entry = tmp_entry;
2250                                 else
2251                                         first_entry = NULL;
2252                                 entry = tmp_entry;
2253                         }
2254                         last_timestamp = map->timestamp;
2255                         continue;
2256                 }
2257                 vm_map_clip_start(map, entry, start);
2258                 vm_map_clip_end(map, entry, end);
2259                 /*
2260                  * Mark the entry in case the map lock is released.  (See
2261                  * above.)
2262                  */
2263                 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2264                 /*
2265                  * Check the map for holes in the specified region.
2266                  * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2267                  */
2268                 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2269                     (entry->end < end && (entry->next == &map->header ||
2270                     entry->next->start > entry->end))) {
2271                         end = entry->end;
2272                         rv = KERN_INVALID_ADDRESS;
2273                         goto done;
2274                 }
2275                 /*
2276                  * If system unwiring, require that the entry is system wired.
2277                  */
2278                 if (!user_unwire &&
2279                     vm_map_entry_system_wired_count(entry) == 0) {
2280                         end = entry->end;
2281                         rv = KERN_INVALID_ARGUMENT;
2282                         goto done;
2283                 }
2284                 entry = entry->next;
2285         }
2286         rv = KERN_SUCCESS;
2287 done:
2288         need_wakeup = FALSE;
2289         if (first_entry == NULL) {
2290                 result = vm_map_lookup_entry(map, start, &first_entry);
2291                 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2292                         first_entry = first_entry->next;
2293                 else
2294                         KASSERT(result, ("vm_map_unwire: lookup failed"));
2295         }
2296         entry = first_entry;
2297         while (entry != &map->header && entry->start < end) {
2298                 if (rv == KERN_SUCCESS && (!user_unwire ||
2299                     (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2300                         if (user_unwire)
2301                                 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2302                         entry->wired_count--;
2303                         if (entry->wired_count == 0) {
2304                                 /*
2305                                  * Retain the map lock.
2306                                  */
2307                                 vm_fault_unwire(map, entry->start, entry->end,
2308                                     entry->object.vm_object != NULL &&
2309                                     (entry->object.vm_object->type == OBJT_DEVICE ||
2310                                     entry->object.vm_object->type == OBJT_SG));
2311                         }
2312                 }
2313                 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2314                         ("vm_map_unwire: in-transition flag missing"));
2315                 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2316                 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2317                         entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2318                         need_wakeup = TRUE;
2319                 }
2320                 vm_map_simplify_entry(map, entry);
2321                 entry = entry->next;
2322         }
2323         vm_map_unlock(map);
2324         if (need_wakeup)
2325                 vm_map_wakeup(map);
2326         return (rv);
2327 }
2328
2329 /*
2330  *      vm_map_wire:
2331  *
2332  *      Implements both kernel and user wiring.
2333  */
2334 int
2335 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2336     int flags)
2337 {
2338         vm_map_entry_t entry, first_entry, tmp_entry;
2339         vm_offset_t saved_end, saved_start;
2340         unsigned int last_timestamp;
2341         int rv;
2342         boolean_t fictitious, need_wakeup, result, user_wire;
2343
2344         user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2345         vm_map_lock(map);
2346         VM_MAP_RANGE_CHECK(map, start, end);
2347         if (!vm_map_lookup_entry(map, start, &first_entry)) {
2348                 if (flags & VM_MAP_WIRE_HOLESOK)
2349                         first_entry = first_entry->next;
2350                 else {
2351                         vm_map_unlock(map);
2352                         return (KERN_INVALID_ADDRESS);
2353                 }
2354         }
2355         last_timestamp = map->timestamp;
2356         entry = first_entry;
2357         while (entry != &map->header && entry->start < end) {
2358                 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2359                         /*
2360                          * We have not yet clipped the entry.
2361                          */
2362                         saved_start = (start >= entry->start) ? start :
2363                             entry->start;
2364                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2365                         if (vm_map_unlock_and_wait(map, 0)) {
2366                                 /*
2367                                  * Allow interruption of user wiring?
2368                                  */
2369                         }
2370                         vm_map_lock(map);
2371                         if (last_timestamp + 1 != map->timestamp) {
2372                                 /*
2373                                  * Look again for the entry because the map was
2374                                  * modified while it was unlocked.
2375                                  * Specifically, the entry may have been
2376                                  * clipped, merged, or deleted.
2377                                  */
2378                                 if (!vm_map_lookup_entry(map, saved_start,
2379                                     &tmp_entry)) {
2380                                         if (flags & VM_MAP_WIRE_HOLESOK)
2381                                                 tmp_entry = tmp_entry->next;
2382                                         else {
2383                                                 if (saved_start == start) {
2384                                                         /*
2385                                                          * first_entry has been deleted.
2386                                                          */
2387                                                         vm_map_unlock(map);
2388                                                         return (KERN_INVALID_ADDRESS);
2389                                                 }
2390                                                 end = saved_start;
2391                                                 rv = KERN_INVALID_ADDRESS;
2392                                                 goto done;
2393                                         }
2394                                 }
2395                                 if (entry == first_entry)
2396                                         first_entry = tmp_entry;
2397                                 else
2398                                         first_entry = NULL;
2399                                 entry = tmp_entry;
2400                         }
2401                         last_timestamp = map->timestamp;
2402                         continue;
2403                 }
2404                 vm_map_clip_start(map, entry, start);
2405                 vm_map_clip_end(map, entry, end);
2406                 /*
2407                  * Mark the entry in case the map lock is released.  (See
2408                  * above.)
2409                  */
2410                 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2411                 /*
2412                  *
2413                  */
2414                 if (entry->wired_count == 0) {
2415                         if ((entry->protection & (VM_PROT_READ|VM_PROT_EXECUTE))
2416                             == 0) {
2417                                 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
2418                                 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
2419                                         end = entry->end;
2420                                         rv = KERN_INVALID_ADDRESS;
2421                                         goto done;
2422                                 }
2423                                 goto next_entry;
2424                         }
2425                         entry->wired_count++;
2426                         saved_start = entry->start;
2427                         saved_end = entry->end;
2428                         fictitious = entry->object.vm_object != NULL &&
2429                             (entry->object.vm_object->type == OBJT_DEVICE ||
2430                             entry->object.vm_object->type == OBJT_SG);
2431                         /*
2432                          * Release the map lock, relying on the in-transition
2433                          * mark.  Mark the map busy for fork.
2434                          */
2435                         vm_map_busy(map);
2436                         vm_map_unlock(map);
2437                         rv = vm_fault_wire(map, saved_start, saved_end,
2438                             user_wire, fictitious);
2439                         vm_map_lock(map);
2440                         vm_map_unbusy(map);
2441                         if (last_timestamp + 1 != map->timestamp) {
2442                                 /*
2443                                  * Look again for the entry because the map was
2444                                  * modified while it was unlocked.  The entry
2445                                  * may have been clipped, but NOT merged or
2446                                  * deleted.
2447                                  */
2448                                 result = vm_map_lookup_entry(map, saved_start,
2449                                     &tmp_entry);
2450                                 KASSERT(result, ("vm_map_wire: lookup failed"));
2451                                 if (entry == first_entry)
2452                                         first_entry = tmp_entry;
2453                                 else
2454                                         first_entry = NULL;
2455                                 entry = tmp_entry;
2456                                 while (entry->end < saved_end) {
2457                                         if (rv != KERN_SUCCESS) {
2458                                                 KASSERT(entry->wired_count == 1,
2459                                                     ("vm_map_wire: bad count"));
2460                                                 entry->wired_count = -1;
2461                                         }
2462                                         entry = entry->next;
2463                                 }
2464                         }
2465                         last_timestamp = map->timestamp;
2466                         if (rv != KERN_SUCCESS) {
2467                                 KASSERT(entry->wired_count == 1,
2468                                     ("vm_map_wire: bad count"));
2469                                 /*
2470                                  * Assign an out-of-range value to represent
2471                                  * the failure to wire this entry.
2472                                  */
2473                                 entry->wired_count = -1;
2474                                 end = entry->end;
2475                                 goto done;
2476                         }
2477                 } else if (!user_wire ||
2478                            (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
2479                         entry->wired_count++;
2480                 }
2481                 /*
2482                  * Check the map for holes in the specified region.
2483                  * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2484                  */
2485         next_entry:
2486                 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2487                     (entry->end < end && (entry->next == &map->header ||
2488                     entry->next->start > entry->end))) {
2489                         end = entry->end;
2490                         rv = KERN_INVALID_ADDRESS;
2491                         goto done;
2492                 }
2493                 entry = entry->next;
2494         }
2495         rv = KERN_SUCCESS;
2496 done:
2497         need_wakeup = FALSE;
2498         if (first_entry == NULL) {
2499                 result = vm_map_lookup_entry(map, start, &first_entry);
2500                 if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2501                         first_entry = first_entry->next;
2502                 else
2503                         KASSERT(result, ("vm_map_wire: lookup failed"));
2504         }
2505         entry = first_entry;
2506         while (entry != &map->header && entry->start < end) {
2507                 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
2508                         goto next_entry_done;
2509                 if (rv == KERN_SUCCESS) {
2510                         if (user_wire)
2511                                 entry->eflags |= MAP_ENTRY_USER_WIRED;
2512                 } else if (entry->wired_count == -1) {
2513                         /*
2514                          * Wiring failed on this entry.  Thus, unwiring is
2515                          * unnecessary.
2516                          */
2517                         entry->wired_count = 0;
2518                 } else {
2519                         if (!user_wire ||
2520                             (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
2521                                 entry->wired_count--;
2522                         if (entry->wired_count == 0) {
2523                                 /*
2524                                  * Retain the map lock.
2525                                  */
2526                                 vm_fault_unwire(map, entry->start, entry->end,
2527                                     entry->object.vm_object != NULL &&
2528                                     (entry->object.vm_object->type == OBJT_DEVICE ||
2529                                     entry->object.vm_object->type == OBJT_SG));
2530                         }
2531                 }
2532         next_entry_done:
2533                 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2534                         ("vm_map_wire: in-transition flag missing"));
2535                 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
2536                 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2537                         entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2538                         need_wakeup = TRUE;
2539                 }
2540                 vm_map_simplify_entry(map, entry);
2541                 entry = entry->next;
2542         }
2543         vm_map_unlock(map);
2544         if (need_wakeup)
2545                 vm_map_wakeup(map);
2546         return (rv);
2547 }
2548
2549 /*
2550  * vm_map_sync
2551  *
2552  * Push any dirty cached pages in the address range to their pager.
2553  * If syncio is TRUE, dirty pages are written synchronously.
2554  * If invalidate is TRUE, any cached pages are freed as well.
2555  *
2556  * If the size of the region from start to end is zero, we are
2557  * supposed to flush all modified pages within the region containing
2558  * start.  Unfortunately, a region can be split or coalesced with
2559  * neighboring regions, making it difficult to determine what the
2560  * original region was.  Therefore, we approximate this requirement by
2561  * flushing the current region containing start.
2562  *
2563  * Returns an error if any part of the specified range is not mapped.
2564  */
2565 int
2566 vm_map_sync(
2567         vm_map_t map,
2568         vm_offset_t start,
2569         vm_offset_t end,
2570         boolean_t syncio,
2571         boolean_t invalidate)
2572 {
2573         vm_map_entry_t current;
2574         vm_map_entry_t entry;
2575         vm_size_t size;
2576         vm_object_t object;
2577         vm_ooffset_t offset;
2578         unsigned int last_timestamp;
2579
2580         vm_map_lock_read(map);
2581         VM_MAP_RANGE_CHECK(map, start, end);
2582         if (!vm_map_lookup_entry(map, start, &entry)) {
2583                 vm_map_unlock_read(map);
2584                 return (KERN_INVALID_ADDRESS);
2585         } else if (start == end) {
2586                 start = entry->start;
2587                 end = entry->end;
2588         }
2589         /*
2590          * Make a first pass to check for user-wired memory and holes.
2591          */
2592         for (current = entry; current != &map->header && current->start < end;
2593             current = current->next) {
2594                 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2595                         vm_map_unlock_read(map);
2596                         return (KERN_INVALID_ARGUMENT);
2597                 }
2598                 if (end > current->end &&
2599                     (current->next == &map->header ||
2600                         current->end != current->next->start)) {
2601                         vm_map_unlock_read(map);
2602                         return (KERN_INVALID_ADDRESS);
2603                 }
2604         }
2605
2606         if (invalidate)
2607                 pmap_remove(map->pmap, start, end);
2608
2609         /*
2610          * Make a second pass, cleaning/uncaching pages from the indicated
2611          * objects as we go.
2612          */
2613         for (current = entry; current != &map->header && current->start < end;) {
2614                 offset = current->offset + (start - current->start);
2615                 size = (end <= current->end ? end : current->end) - start;
2616                 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2617                         vm_map_t smap;
2618                         vm_map_entry_t tentry;
2619                         vm_size_t tsize;
2620
2621                         smap = current->object.sub_map;
2622                         vm_map_lock_read(smap);
2623                         (void) vm_map_lookup_entry(smap, offset, &tentry);
2624                         tsize = tentry->end - offset;
2625                         if (tsize < size)
2626                                 size = tsize;
2627                         object = tentry->object.vm_object;
2628                         offset = tentry->offset + (offset - tentry->start);
2629                         vm_map_unlock_read(smap);
2630                 } else {
2631                         object = current->object.vm_object;
2632                 }
2633                 vm_object_reference(object);
2634                 last_timestamp = map->timestamp;
2635                 vm_map_unlock_read(map);
2636                 vm_object_sync(object, offset, size, syncio, invalidate);
2637                 start += size;
2638                 vm_object_deallocate(object);
2639                 vm_map_lock_read(map);
2640                 if (last_timestamp == map->timestamp ||
2641                     !vm_map_lookup_entry(map, start, &current))
2642                         current = current->next;
2643         }
2644
2645         vm_map_unlock_read(map);
2646         return (KERN_SUCCESS);
2647 }
2648
2649 /*
2650  *      vm_map_entry_unwire:    [ internal use only ]
2651  *
2652  *      Make the region specified by this entry pageable.
2653  *
2654  *      The map in question should be locked.
2655  *      [This is the reason for this routine's existence.]
2656  */
2657 static void
2658 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2659 {
2660         vm_fault_unwire(map, entry->start, entry->end,
2661             entry->object.vm_object != NULL &&
2662             (entry->object.vm_object->type == OBJT_DEVICE ||
2663             entry->object.vm_object->type == OBJT_SG));
2664         entry->wired_count = 0;
2665 }
2666
2667 static void
2668 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
2669 {
2670
2671         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
2672                 vm_object_deallocate(entry->object.vm_object);
2673         uma_zfree(system_map ? kmapentzone : mapentzone, entry);
2674 }
2675
2676 /*
2677  *      vm_map_entry_delete:    [ internal use only ]
2678  *
2679  *      Deallocate the given entry from the target map.
2680  */
2681 static void
2682 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2683 {
2684         vm_object_t object;
2685         vm_pindex_t offidxstart, offidxend, count, size1;
2686         vm_ooffset_t size;
2687
2688         vm_map_entry_unlink(map, entry);
2689         object = entry->object.vm_object;
2690         size = entry->end - entry->start;
2691         map->size -= size;
2692
2693         if (entry->uip != NULL) {
2694                 swap_release_by_uid(size, entry->uip);
2695                 uifree(entry->uip);
2696         }
2697
2698         if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
2699             (object != NULL)) {
2700                 KASSERT(entry->uip == NULL || object->uip == NULL ||
2701                     (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2702                     ("OVERCOMMIT vm_map_entry_delete: both uip %p", entry));
2703                 count = OFF_TO_IDX(size);
2704                 offidxstart = OFF_TO_IDX(entry->offset);
2705                 offidxend = offidxstart + count;
2706                 VM_OBJECT_LOCK(object);
2707                 if (object->ref_count != 1 &&
2708                     ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
2709                     object == kernel_object || object == kmem_object)) {
2710                         vm_object_collapse(object);
2711                         vm_object_page_remove(object, offidxstart, offidxend, FALSE);
2712                         if (object->type == OBJT_SWAP)
2713                                 swap_pager_freespace(object, offidxstart, count);
2714                         if (offidxend >= object->size &&
2715                             offidxstart < object->size) {
2716                                 size1 = object->size;
2717                                 object->size = offidxstart;
2718                                 if (object->uip != NULL) {
2719                                         size1 -= object->size;
2720                                         KASSERT(object->charge >= ptoa(size1),
2721                                             ("vm_map_entry_delete: object->charge < 0"));
2722                                         swap_release_by_uid(ptoa(size1), object->uip);
2723                                         object->charge -= ptoa(size1);
2724                                 }
2725                         }
2726                 }
2727                 VM_OBJECT_UNLOCK(object);
2728         } else
2729                 entry->object.vm_object = NULL;
2730         if (map->system_map)
2731                 vm_map_entry_deallocate(entry, TRUE);
2732         else {
2733                 entry->next = curthread->td_map_def_user;
2734                 curthread->td_map_def_user = entry;
2735         }
2736 }
2737
2738 /*
2739  *      vm_map_delete:  [ internal use only ]
2740  *
2741  *      Deallocates the given address range from the target
2742  *      map.
2743  */
2744 int
2745 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2746 {
2747         vm_map_entry_t entry;
2748         vm_map_entry_t first_entry;
2749
2750         VM_MAP_ASSERT_LOCKED(map);
2751
2752         /*
2753          * Find the start of the region, and clip it
2754          */
2755         if (!vm_map_lookup_entry(map, start, &first_entry))
2756                 entry = first_entry->next;
2757         else {
2758                 entry = first_entry;
2759                 vm_map_clip_start(map, entry, start);
2760         }
2761
2762         /*
2763          * Step through all entries in this region
2764          */
2765         while ((entry != &map->header) && (entry->start < end)) {
2766                 vm_map_entry_t next;
2767
2768                 /*
2769                  * Wait for wiring or unwiring of an entry to complete.
2770                  * Also wait for any system wirings to disappear on
2771                  * user maps.
2772                  */
2773                 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
2774                     (vm_map_pmap(map) != kernel_pmap &&
2775                     vm_map_entry_system_wired_count(entry) != 0)) {
2776                         unsigned int last_timestamp;
2777                         vm_offset_t saved_start;
2778                         vm_map_entry_t tmp_entry;
2779
2780                         saved_start = entry->start;
2781                         entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2782                         last_timestamp = map->timestamp;
2783                         (void) vm_map_unlock_and_wait(map, 0);
2784                         vm_map_lock(map);
2785                         if (last_timestamp + 1 != map->timestamp) {
2786                                 /*
2787                                  * Look again for the entry because the map was
2788                                  * modified while it was unlocked.
2789                                  * Specifically, the entry may have been
2790                                  * clipped, merged, or deleted.
2791                                  */
2792                                 if (!vm_map_lookup_entry(map, saved_start,
2793                                                          &tmp_entry))
2794                                         entry = tmp_entry->next;
2795                                 else {
2796                                         entry = tmp_entry;
2797                                         vm_map_clip_start(map, entry,
2798                                                           saved_start);
2799                                 }
2800                         }
2801                         continue;
2802                 }
2803                 vm_map_clip_end(map, entry, end);
2804
2805                 next = entry->next;
2806
2807                 /*
2808                  * Unwire before removing addresses from the pmap; otherwise,
2809                  * unwiring will put the entries back in the pmap.
2810                  */
2811                 if (entry->wired_count != 0) {
2812                         vm_map_entry_unwire(map, entry);
2813                 }
2814
2815                 pmap_remove(map->pmap, entry->start, entry->end);
2816
2817                 /*
2818                  * Delete the entry only after removing all pmap
2819                  * entries pointing to its pages.  (Otherwise, its
2820                  * page frames may be reallocated, and any modify bits
2821                  * will be set in the wrong object!)
2822                  */
2823                 vm_map_entry_delete(map, entry);
2824                 entry = next;
2825         }
2826         return (KERN_SUCCESS);
2827 }
2828
2829 /*
2830  *      vm_map_remove:
2831  *
2832  *      Remove the given address range from the target map.
2833  *      This is the exported form of vm_map_delete.
2834  */
2835 int
2836 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2837 {
2838         int result;
2839
2840         vm_map_lock(map);
2841         VM_MAP_RANGE_CHECK(map, start, end);
2842         result = vm_map_delete(map, start, end);
2843         vm_map_unlock(map);
2844         return (result);
2845 }
2846
2847 /*
2848  *      vm_map_check_protection:
2849  *
2850  *      Assert that the target map allows the specified privilege on the
2851  *      entire address region given.  The entire region must be allocated.
2852  *
2853  *      WARNING!  This code does not and should not check whether the
2854  *      contents of the region is accessible.  For example a smaller file
2855  *      might be mapped into a larger address space.
2856  *
2857  *      NOTE!  This code is also called by munmap().
2858  *
2859  *      The map must be locked.  A read lock is sufficient.
2860  */
2861 boolean_t
2862 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2863                         vm_prot_t protection)
2864 {
2865         vm_map_entry_t entry;
2866         vm_map_entry_t tmp_entry;
2867
2868         if (!vm_map_lookup_entry(map, start, &tmp_entry))
2869                 return (FALSE);
2870         entry = tmp_entry;
2871
2872         while (start < end) {
2873                 if (entry == &map->header)
2874                         return (FALSE);
2875                 /*
2876                  * No holes allowed!
2877                  */
2878                 if (start < entry->start)
2879                         return (FALSE);
2880                 /*
2881                  * Check protection associated with entry.
2882                  */
2883                 if ((entry->protection & protection) != protection)
2884                         return (FALSE);
2885                 /* go to next entry */
2886                 start = entry->end;
2887                 entry = entry->next;
2888         }
2889         return (TRUE);
2890 }
2891
2892 /*
2893  *      vm_map_copy_entry:
2894  *
2895  *      Copies the contents of the source entry to the destination
2896  *      entry.  The entries *must* be aligned properly.
2897  */
2898 static void
2899 vm_map_copy_entry(
2900         vm_map_t src_map,
2901         vm_map_t dst_map,
2902         vm_map_entry_t src_entry,
2903         vm_map_entry_t dst_entry,
2904         vm_ooffset_t *fork_charge)
2905 {
2906         vm_object_t src_object;
2907         vm_offset_t size;
2908         struct uidinfo *uip;
2909         int charged;
2910
2911         VM_MAP_ASSERT_LOCKED(dst_map);
2912
2913         if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2914                 return;
2915
2916         if (src_entry->wired_count == 0) {
2917
2918                 /*
2919                  * If the source entry is marked needs_copy, it is already
2920                  * write-protected.
2921                  */
2922                 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2923                         pmap_protect(src_map->pmap,
2924                             src_entry->start,
2925                             src_entry->end,
2926                             src_entry->protection & ~VM_PROT_WRITE);
2927                 }
2928
2929                 /*
2930                  * Make a copy of the object.
2931                  */
2932                 size = src_entry->end - src_entry->start;
2933                 if ((src_object = src_entry->object.vm_object) != NULL) {
2934                         VM_OBJECT_LOCK(src_object);
2935                         charged = ENTRY_CHARGED(src_entry);
2936                         if ((src_object->handle == NULL) &&
2937                                 (src_object->type == OBJT_DEFAULT ||
2938                                  src_object->type == OBJT_SWAP)) {
2939                                 vm_object_collapse(src_object);
2940                                 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2941                                         vm_object_split(src_entry);
2942                                         src_object = src_entry->object.vm_object;
2943                                 }
2944                         }
2945                         vm_object_reference_locked(src_object);
2946                         vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2947                         if (src_entry->uip != NULL &&
2948                             !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
2949                                 KASSERT(src_object->uip == NULL,
2950                                     ("OVERCOMMIT: vm_map_copy_entry: uip %p",
2951                                      src_object));
2952                                 src_object->uip = src_entry->uip;
2953                                 src_object->charge = size;
2954                         }
2955                         VM_OBJECT_UNLOCK(src_object);
2956                         dst_entry->object.vm_object = src_object;
2957                         if (charged) {
2958                                 uip = curthread->td_ucred->cr_ruidinfo;
2959                                 uihold(uip);
2960                                 dst_entry->uip = uip;
2961                                 *fork_charge += size;
2962                                 if (!(src_entry->eflags &
2963                                       MAP_ENTRY_NEEDS_COPY)) {
2964                                         uihold(uip);
2965                                         src_entry->uip = uip;
2966                                         *fork_charge += size;
2967                                 }
2968                         }
2969                         src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2970                         dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2971                         dst_entry->offset = src_entry->offset;
2972                 } else {
2973                         dst_entry->object.vm_object = NULL;
2974                         dst_entry->offset = 0;
2975                         if (src_entry->uip != NULL) {
2976                                 dst_entry->uip = curthread->td_ucred->cr_ruidinfo;
2977                                 uihold(dst_entry->uip);
2978                                 *fork_charge += size;
2979                         }
2980                 }
2981
2982                 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2983                     dst_entry->end - dst_entry->start, src_entry->start);
2984         } else {
2985                 /*
2986                  * Of course, wired down pages can't be set copy-on-write.
2987                  * Cause wired pages to be copied into the new map by
2988                  * simulating faults (the new pages are pageable)
2989                  */
2990                 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
2991                     fork_charge);
2992         }
2993 }
2994
2995 /*
2996  * vmspace_map_entry_forked:
2997  * Update the newly-forked vmspace each time a map entry is inherited
2998  * or copied.  The values for vm_dsize and vm_tsize are approximate
2999  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
3000  */
3001 static void
3002 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
3003     vm_map_entry_t entry)
3004 {
3005         vm_size_t entrysize;
3006         vm_offset_t newend;
3007
3008         entrysize = entry->end - entry->start;
3009         vm2->vm_map.size += entrysize;
3010         if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
3011                 vm2->vm_ssize += btoc(entrysize);
3012         } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
3013             entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3014                 newend = MIN(entry->end,
3015                     (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
3016                 vm2->vm_dsize += btoc(newend - entry->start);
3017         } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
3018             entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3019                 newend = MIN(entry->end,
3020                     (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
3021                 vm2->vm_tsize += btoc(newend - entry->start);
3022         }
3023 }
3024
3025 /*
3026  * vmspace_fork:
3027  * Create a new process vmspace structure and vm_map
3028  * based on those of an existing process.  The new map
3029  * is based on the old map, according to the inheritance
3030  * values on the regions in that map.
3031  *
3032  * XXX It might be worth coalescing the entries added to the new vmspace.
3033  *
3034  * The source map must not be locked.
3035  */
3036 struct vmspace *
3037 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3038 {
3039         struct vmspace *vm2;
3040         vm_map_t old_map = &vm1->vm_map;
3041         vm_map_t new_map;
3042         vm_map_entry_t old_entry;
3043         vm_map_entry_t new_entry;
3044         vm_object_t object;
3045         int locked;
3046
3047         vm_map_lock(old_map);
3048         if (old_map->busy)
3049                 vm_map_wait_busy(old_map);
3050         vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
3051         if (vm2 == NULL)
3052                 goto unlock_and_return;
3053         vm2->vm_taddr = vm1->vm_taddr;
3054         vm2->vm_daddr = vm1->vm_daddr;
3055         vm2->vm_maxsaddr = vm1->vm_maxsaddr;
3056         new_map = &vm2->vm_map; /* XXX */
3057         locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
3058         KASSERT(locked, ("vmspace_fork: lock failed"));
3059         new_map->timestamp = 1;
3060
3061         old_entry = old_map->header.next;
3062
3063         while (old_entry != &old_map->header) {
3064                 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3065                         panic("vm_map_fork: encountered a submap");
3066
3067                 switch (old_entry->inheritance) {
3068                 case VM_INHERIT_NONE:
3069                         break;
3070
3071                 case VM_INHERIT_SHARE:
3072                         /*
3073                          * Clone the entry, creating the shared object if necessary.
3074                          */
3075                         object = old_entry->object.vm_object;
3076                         if (object == NULL) {
3077                                 object = vm_object_allocate(OBJT_DEFAULT,
3078                                         atop(old_entry->end - old_entry->start));
3079                                 old_entry->object.vm_object = object;
3080                                 old_entry->offset = 0;
3081                                 if (old_entry->uip != NULL) {
3082                                         object->uip = old_entry->uip;
3083                                         object->charge = old_entry->end -
3084                                             old_entry->start;
3085                                         old_entry->uip = NULL;
3086                                 }
3087                         }
3088
3089                         /*
3090                          * Add the reference before calling vm_object_shadow
3091                          * to insure that a shadow object is created.
3092                          */
3093                         vm_object_reference(object);
3094                         if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3095                                 vm_object_shadow(&old_entry->object.vm_object,
3096                                         &old_entry->offset,
3097                                         atop(old_entry->end - old_entry->start));
3098                                 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3099                                 /* Transfer the second reference too. */
3100                                 vm_object_reference(
3101                                     old_entry->object.vm_object);
3102
3103                                 /*
3104                                  * As in vm_map_simplify_entry(), the
3105                                  * vnode lock will not be acquired in
3106                                  * this call to vm_object_deallocate().
3107                                  */
3108                                 vm_object_deallocate(object);
3109                                 object = old_entry->object.vm_object;
3110                         }
3111                         VM_OBJECT_LOCK(object);
3112                         vm_object_clear_flag(object, OBJ_ONEMAPPING);
3113                         if (old_entry->uip != NULL) {
3114                                 KASSERT(object->uip == NULL, ("vmspace_fork both uip"));
3115                                 object->uip = old_entry->uip;
3116                                 object->charge = old_entry->end - old_entry->start;
3117                                 old_entry->uip = NULL;
3118                         }
3119                         VM_OBJECT_UNLOCK(object);
3120
3121                         /*
3122                          * Clone the entry, referencing the shared object.
3123                          */
3124                         new_entry = vm_map_entry_create(new_map);
3125                         *new_entry = *old_entry;
3126                         new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3127                             MAP_ENTRY_IN_TRANSITION);
3128                         new_entry->wired_count = 0;
3129
3130                         /*
3131                          * Insert the entry into the new map -- we know we're
3132                          * inserting at the end of the new map.
3133                          */
3134                         vm_map_entry_link(new_map, new_map->header.prev,
3135                             new_entry);
3136                         vmspace_map_entry_forked(vm1, vm2, new_entry);
3137
3138                         /*
3139                          * Update the physical map
3140                          */
3141                         pmap_copy(new_map->pmap, old_map->pmap,
3142                             new_entry->start,
3143                             (old_entry->end - old_entry->start),
3144                             old_entry->start);
3145                         break;
3146
3147                 case VM_INHERIT_COPY:
3148                         /*
3149                          * Clone the entry and link into the map.
3150                          */
3151                         new_entry = vm_map_entry_create(new_map);
3152                         *new_entry = *old_entry;
3153                         new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
3154                             MAP_ENTRY_IN_TRANSITION);
3155                         new_entry->wired_count = 0;
3156                         new_entry->object.vm_object = NULL;
3157                         new_entry->uip = NULL;
3158                         vm_map_entry_link(new_map, new_map->header.prev,
3159                             new_entry);
3160                         vmspace_map_entry_forked(vm1, vm2, new_entry);
3161                         vm_map_copy_entry(old_map, new_map, old_entry,
3162                             new_entry, fork_charge);
3163                         break;
3164                 }
3165                 old_entry = old_entry->next;
3166         }
3167 unlock_and_return:
3168         vm_map_unlock(old_map);
3169         if (vm2 != NULL)
3170                 vm_map_unlock(new_map);
3171
3172         return (vm2);
3173 }
3174
3175 int
3176 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3177     vm_prot_t prot, vm_prot_t max, int cow)
3178 {
3179         vm_map_entry_t new_entry, prev_entry;
3180         vm_offset_t bot, top;
3181         vm_size_t init_ssize;
3182         int orient, rv;
3183         rlim_t vmemlim;
3184
3185         /*
3186          * The stack orientation is piggybacked with the cow argument.
3187          * Extract it into orient and mask the cow argument so that we
3188          * don't pass it around further.
3189          * NOTE: We explicitly allow bi-directional stacks.
3190          */
3191         orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3192         cow &= ~orient;
3193         KASSERT(orient != 0, ("No stack grow direction"));
3194
3195         if (addrbos < vm_map_min(map) ||
3196             addrbos > vm_map_max(map) ||
3197             addrbos + max_ssize < addrbos)
3198                 return (KERN_NO_SPACE);
3199
3200         init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
3201
3202         PROC_LOCK(curthread->td_proc);
3203         vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
3204         PROC_UNLOCK(curthread->td_proc);
3205
3206         vm_map_lock(map);
3207
3208         /* If addr is already mapped, no go */
3209         if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
3210                 vm_map_unlock(map);
3211                 return (KERN_NO_SPACE);
3212         }
3213
3214         /* If we would blow our VMEM resource limit, no go */
3215         if (map->size + init_ssize > vmemlim) {
3216                 vm_map_unlock(map);
3217                 return (KERN_NO_SPACE);
3218         }
3219
3220         /*
3221          * If we can't accomodate max_ssize in the current mapping, no go.
3222          * However, we need to be aware that subsequent user mappings might
3223          * map into the space we have reserved for stack, and currently this
3224          * space is not protected.
3225          *
3226          * Hopefully we will at least detect this condition when we try to
3227          * grow the stack.
3228          */
3229         if ((prev_entry->next != &map->header) &&
3230             (prev_entry->next->start < addrbos + max_ssize)) {
3231                 vm_map_unlock(map);
3232                 return (KERN_NO_SPACE);
3233         }
3234
3235         /*
3236          * We initially map a stack of only init_ssize.  We will grow as
3237          * needed later.  Depending on the orientation of the stack (i.e.
3238          * the grow direction) we either map at the top of the range, the
3239          * bottom of the range or in the middle.
3240          *
3241          * Note: we would normally expect prot and max to be VM_PROT_ALL,
3242          * and cow to be 0.  Possibly we should eliminate these as input
3243          * parameters, and just pass these values here in the insert call.
3244          */
3245         if (orient == MAP_STACK_GROWS_DOWN)
3246                 bot = addrbos + max_ssize - init_ssize;
3247         else if (orient == MAP_STACK_GROWS_UP)
3248                 bot = addrbos;
3249         else
3250                 bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3251         top = bot + init_ssize;
3252         rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
3253
3254         /* Now set the avail_ssize amount. */
3255         if (rv == KERN_SUCCESS) {
3256                 if (prev_entry != &map->header)
3257                         vm_map_clip_end(map, prev_entry, bot);
3258                 new_entry = prev_entry->next;
3259                 if (new_entry->end != top || new_entry->start != bot)
3260                         panic("Bad entry start/end for new stack entry");
3261
3262                 new_entry->avail_ssize = max_ssize - init_ssize;
3263                 if (orient & MAP_STACK_GROWS_DOWN)
3264                         new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3265                 if (orient & MAP_STACK_GROWS_UP)
3266                         new_entry->eflags |= MAP_ENTRY_GROWS_UP;
3267         }
3268
3269         vm_map_unlock(map);
3270         return (rv);
3271 }
3272
3273 static int stack_guard_page = 0;
3274 TUNABLE_INT("security.bsd.stack_guard_page", &stack_guard_page);
3275 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RW,
3276     &stack_guard_page, 0,
3277     "Insert stack guard page ahead of the growable segments.");
3278
3279 /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
3280  * desired address is already mapped, or if we successfully grow
3281  * the stack.  Also returns KERN_SUCCESS if addr is outside the
3282  * stack range (this is strange, but preserves compatibility with
3283  * the grow function in vm_machdep.c).
3284  */
3285 int
3286 vm_map_growstack(struct proc *p, vm_offset_t addr)
3287 {
3288         vm_map_entry_t next_entry, prev_entry;
3289         vm_map_entry_t new_entry, stack_entry;
3290         struct vmspace *vm = p->p_vmspace;
3291         vm_map_t map = &vm->vm_map;
3292         vm_offset_t end;
3293         size_t grow_amount, max_grow;
3294         rlim_t stacklim, vmemlim;
3295         int is_procstack, rv;
3296         struct uidinfo *uip;
3297
3298 Retry:
3299         PROC_LOCK(p);
3300         stacklim = lim_cur(p, RLIMIT_STACK);
3301         vmemlim = lim_cur(p, RLIMIT_VMEM);
3302         PROC_UNLOCK(p);
3303
3304         vm_map_lock_read(map);
3305
3306         /* If addr is already in the entry range, no need to grow.*/
3307         if (vm_map_lookup_entry(map, addr, &prev_entry)) {
3308                 vm_map_unlock_read(map);
3309                 return (KERN_SUCCESS);
3310         }
3311
3312         next_entry = prev_entry->next;
3313         if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3314                 /*
3315                  * This entry does not grow upwards. Since the address lies
3316                  * beyond this entry, the next entry (if one exists) has to
3317                  * be a downward growable entry. The entry list header is
3318                  * never a growable entry, so it suffices to check the flags.
3319                  */
3320                 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
3321                         vm_map_unlock_read(map);
3322                         return (KERN_SUCCESS);
3323                 }
3324                 stack_entry = next_entry;
3325         } else {
3326                 /*
3327                  * This entry grows upward. If the next entry does not at
3328                  * least grow downwards, this is the entry we need to grow.
3329                  * otherwise we have two possible choices and we have to
3330                  * select one.
3331                  */
3332                 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3333                         /*
3334                          * We have two choices; grow the entry closest to
3335                          * the address to minimize the amount of growth.
3336                          */
3337                         if (addr - prev_entry->end <= next_entry->start - addr)
3338                                 stack_entry = prev_entry;
3339                         else
3340                                 stack_entry = next_entry;
3341                 } else
3342                         stack_entry = prev_entry;
3343         }
3344
3345         if (stack_entry == next_entry) {
3346                 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3347                 KASSERT(addr < stack_entry->start, ("foo"));
3348                 end = (prev_entry != &map->header) ? prev_entry->end :
3349                     stack_entry->start - stack_entry->avail_ssize;
3350                 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3351                 max_grow = stack_entry->start - end;
3352         } else {
3353                 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
3354                 KASSERT(addr >= stack_entry->end, ("foo"));
3355                 end = (next_entry != &map->header) ? next_entry->start :
3356                     stack_entry->end + stack_entry->avail_ssize;
3357                 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3358                 max_grow = end - stack_entry->end;
3359         }
3360
3361         if (grow_amount > stack_entry->avail_ssize) {
3362                 vm_map_unlock_read(map);
3363                 return (KERN_NO_SPACE);
3364         }
3365
3366         /*
3367          * If there is no longer enough space between the entries nogo, and
3368          * adjust the available space.  Note: this  should only happen if the
3369          * user has mapped into the stack area after the stack was created,
3370          * and is probably an error.
3371          *
3372          * This also effectively destroys any guard page the user might have
3373          * intended by limiting the stack size.
3374          */
3375         if (grow_amount + (stack_guard_page ? PAGE_SIZE : 0) > max_grow) {
3376                 if (vm_map_lock_upgrade(map))
3377                         goto Retry;
3378
3379                 stack_entry->avail_ssize = max_grow;
3380
3381                 vm_map_unlock(map);
3382                 return (KERN_NO_SPACE);
3383         }
3384
3385         is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
3386
3387         /*
3388          * If this is the main process stack, see if we're over the stack
3389          * limit.
3390          */
3391         if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3392                 vm_map_unlock_read(map);
3393                 return (KERN_NO_SPACE);
3394         }
3395
3396         /* Round up the grow amount modulo SGROWSIZ */
3397         grow_amount = roundup (grow_amount, sgrowsiz);
3398         if (grow_amount > stack_entry->avail_ssize)
3399                 grow_amount = stack_entry->avail_ssize;
3400         if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3401                 grow_amount = trunc_page((vm_size_t)stacklim) -
3402                     ctob(vm->vm_ssize);
3403         }
3404
3405         /* If we would blow our VMEM resource limit, no go */
3406         if (map->size + grow_amount > vmemlim) {
3407                 vm_map_unlock_read(map);
3408                 return (KERN_NO_SPACE);
3409         }
3410
3411         if (vm_map_lock_upgrade(map))
3412                 goto Retry;
3413
3414         if (stack_entry == next_entry) {
3415                 /*
3416                  * Growing downward.
3417                  */
3418                 /* Get the preliminary new entry start value */
3419                 addr = stack_entry->start - grow_amount;
3420
3421                 /*
3422                  * If this puts us into the previous entry, cut back our
3423                  * growth to the available space. Also, see the note above.
3424                  */
3425                 if (addr < end) {
3426                         stack_entry->avail_ssize = max_grow;
3427                         addr = end;
3428                         if (stack_guard_page)
3429                                 addr += PAGE_SIZE;
3430                 }
3431
3432                 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
3433                     p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
3434
3435                 /* Adjust the available stack space by the amount we grew. */
3436                 if (rv == KERN_SUCCESS) {
3437                         if (prev_entry != &map->header)
3438                                 vm_map_clip_end(map, prev_entry, addr);
3439                         new_entry = prev_entry->next;
3440                         KASSERT(new_entry == stack_entry->prev, ("foo"));
3441                         KASSERT(new_entry->end == stack_entry->start, ("foo"));
3442                         KASSERT(new_entry->start == addr, ("foo"));
3443                         grow_amount = new_entry->end - new_entry->start;
3444                         new_entry->avail_ssize = stack_entry->avail_ssize -
3445                             grow_amount;
3446                         stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3447                         new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3448                 }
3449         } else {
3450                 /*
3451                  * Growing upward.
3452                  */
3453                 addr = stack_entry->end + grow_amount;
3454
3455                 /*
3456                  * If this puts us into the next entry, cut back our growth
3457                  * to the available space. Also, see the note above.
3458                  */
3459                 if (addr > end) {
3460                         stack_entry->avail_ssize = end - stack_entry->end;
3461                         addr = end;
3462                         if (stack_guard_page)
3463                                 addr -= PAGE_SIZE;
3464                 }
3465
3466                 grow_amount = addr - stack_entry->end;
3467                 uip = stack_entry->uip;
3468                 if (uip == NULL && stack_entry->object.vm_object != NULL)
3469                         uip = stack_entry->object.vm_object->uip;
3470                 if (uip != NULL && !swap_reserve_by_uid(grow_amount, uip))
3471                         rv = KERN_NO_SPACE;
3472                 /* Grow the underlying object if applicable. */
3473                 else if (stack_entry->object.vm_object == NULL ||
3474                          vm_object_coalesce(stack_entry->object.vm_object,
3475                          stack_entry->offset,
3476                          (vm_size_t)(stack_entry->end - stack_entry->start),
3477                          (vm_size_t)grow_amount, uip != NULL)) {
3478                         map->size += (addr - stack_entry->end);
3479                         /* Update the current entry. */
3480                         stack_entry->end = addr;
3481                         stack_entry->avail_ssize -= grow_amount;
3482                         vm_map_entry_resize_free(map, stack_entry);
3483                         rv = KERN_SUCCESS;
3484
3485                         if (next_entry != &map->header)
3486                                 vm_map_clip_start(map, next_entry, addr);
3487                 } else
3488                         rv = KERN_FAILURE;
3489         }
3490
3491         if (rv == KERN_SUCCESS && is_procstack)
3492                 vm->vm_ssize += btoc(grow_amount);
3493
3494         vm_map_unlock(map);
3495
3496         /*
3497          * Heed the MAP_WIREFUTURE flag if it was set for this process.
3498          */
3499         if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3500                 vm_map_wire(map,
3501                     (stack_entry == next_entry) ? addr : addr - grow_amount,
3502                     (stack_entry == next_entry) ? stack_entry->start : addr,
3503                     (p->p_flag & P_SYSTEM)
3504                     ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3505                     : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3506         }
3507
3508         return (rv);
3509 }
3510
3511 /*
3512  * Unshare the specified VM space for exec.  If other processes are
3513  * mapped to it, then create a new one.  The new vmspace is null.
3514  */
3515 int
3516 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
3517 {
3518         struct vmspace *oldvmspace = p->p_vmspace;
3519         struct vmspace *newvmspace;
3520
3521         newvmspace = vmspace_alloc(minuser, maxuser);
3522         if (newvmspace == NULL)
3523                 return (ENOMEM);
3524         newvmspace->vm_swrss = oldvmspace->vm_swrss;
3525         /*
3526          * This code is written like this for prototype purposes.  The
3527          * goal is to avoid running down the vmspace here, but let the
3528          * other process's that are still using the vmspace to finally
3529          * run it down.  Even though there is little or no chance of blocking
3530          * here, it is a good idea to keep this form for future mods.
3531          */
3532         PROC_VMSPACE_LOCK(p);
3533         p->p_vmspace = newvmspace;
3534         PROC_VMSPACE_UNLOCK(p);
3535         if (p == curthread->td_proc)
3536                 pmap_activate(curthread);
3537         vmspace_free(oldvmspace);
3538         return (0);
3539 }
3540
3541 /*
3542  * Unshare the specified VM space for forcing COW.  This
3543  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
3544  */
3545 int
3546 vmspace_unshare(struct proc *p)
3547 {
3548         struct vmspace *oldvmspace = p->p_vmspace;
3549         struct vmspace *newvmspace;
3550         vm_ooffset_t fork_charge;
3551
3552         if (oldvmspace->vm_refcnt == 1)
3553                 return (0);
3554         fork_charge = 0;
3555         newvmspace = vmspace_fork(oldvmspace, &fork_charge);
3556         if (newvmspace == NULL)
3557                 return (ENOMEM);
3558         if (!swap_reserve_by_uid(fork_charge, p->p_ucred->cr_ruidinfo)) {
3559                 vmspace_free(newvmspace);
3560                 return (ENOMEM);
3561         }
3562         PROC_VMSPACE_LOCK(p);
3563         p->p_vmspace = newvmspace;
3564         PROC_VMSPACE_UNLOCK(p);
3565         if (p == curthread->td_proc)
3566                 pmap_activate(curthread);
3567         vmspace_free(oldvmspace);
3568         return (0);
3569 }
3570
3571 /*
3572  *      vm_map_lookup:
3573  *
3574  *      Finds the VM object, offset, and
3575  *      protection for a given virtual address in the
3576  *      specified map, assuming a page fault of the
3577  *      type specified.
3578  *
3579  *      Leaves the map in question locked for read; return
3580  *      values are guaranteed until a vm_map_lookup_done
3581  *      call is performed.  Note that the map argument
3582  *      is in/out; the returned map must be used in
3583  *      the call to vm_map_lookup_done.
3584  *
3585  *      A handle (out_entry) is returned for use in
3586  *      vm_map_lookup_done, to make that fast.
3587  *
3588  *      If a lookup is requested with "write protection"
3589  *      specified, the map may be changed to perform virtual
3590  *      copying operations, although the data referenced will
3591  *      remain the same.
3592  */
3593 int
3594 vm_map_lookup(vm_map_t *var_map,                /* IN/OUT */
3595               vm_offset_t vaddr,
3596               vm_prot_t fault_typea,
3597               vm_map_entry_t *out_entry,        /* OUT */
3598               vm_object_t *object,              /* OUT */
3599               vm_pindex_t *pindex,              /* OUT */
3600               vm_prot_t *out_prot,              /* OUT */
3601               boolean_t *wired)                 /* OUT */
3602 {
3603         vm_map_entry_t entry;
3604         vm_map_t map = *var_map;
3605         vm_prot_t prot;
3606         vm_prot_t fault_type = fault_typea;
3607         vm_object_t eobject;
3608         struct uidinfo *uip;
3609         vm_ooffset_t size;
3610
3611 RetryLookup:;
3612
3613         vm_map_lock_read(map);
3614
3615         /*
3616          * Lookup the faulting address.
3617          */
3618         if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3619                 vm_map_unlock_read(map);
3620                 return (KERN_INVALID_ADDRESS);
3621         }
3622
3623         entry = *out_entry;
3624
3625         /*
3626          * Handle submaps.
3627          */
3628         if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3629                 vm_map_t old_map = map;
3630
3631                 *var_map = map = entry->object.sub_map;
3632                 vm_map_unlock_read(old_map);
3633                 goto RetryLookup;
3634         }
3635
3636         /*
3637          * Check whether this task is allowed to have this page.
3638          * Note the special case for MAP_ENTRY_COW
3639          * pages with an override.  This is to implement a forced
3640          * COW for debuggers.
3641          */
3642         if (fault_type & VM_PROT_OVERRIDE_WRITE)
3643                 prot = entry->max_protection;
3644         else
3645                 prot = entry->protection;
3646         fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
3647         if ((fault_type & prot) != fault_type) {
3648                 vm_map_unlock_read(map);
3649                 return (KERN_PROTECTION_FAILURE);
3650         }
3651         if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3652             (entry->eflags & MAP_ENTRY_COW) &&
3653             (fault_type & VM_PROT_WRITE) &&
3654             (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3655                 vm_map_unlock_read(map);
3656                 return (KERN_PROTECTION_FAILURE);
3657         }
3658
3659         /*
3660          * If this page is not pageable, we have to get it for all possible
3661          * accesses.
3662          */
3663         *wired = (entry->wired_count != 0);
3664         if (*wired)
3665                 prot = fault_type = entry->protection;
3666         size = entry->end - entry->start;
3667         /*
3668          * If the entry was copy-on-write, we either ...
3669          */
3670         if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3671                 /*
3672                  * If we want to write the page, we may as well handle that
3673                  * now since we've got the map locked.
3674                  *
3675                  * If we don't need to write the page, we just demote the
3676                  * permissions allowed.
3677                  */
3678                 if (fault_type & VM_PROT_WRITE) {
3679                         /*
3680                          * Make a new object, and place it in the object
3681                          * chain.  Note that no new references have appeared
3682                          * -- one just moved from the map to the new
3683                          * object.
3684                          */
3685                         if (vm_map_lock_upgrade(map))
3686                                 goto RetryLookup;
3687
3688                         if (entry->uip == NULL) {
3689                                 /*
3690                                  * The debugger owner is charged for
3691                                  * the memory.
3692                                  */
3693                                 uip = curthread->td_ucred->cr_ruidinfo;
3694                                 uihold(uip);
3695                                 if (!swap_reserve_by_uid(size, uip)) {
3696                                         uifree(uip);
3697                                         vm_map_unlock(map);
3698                                         return (KERN_RESOURCE_SHORTAGE);
3699                                 }
3700                                 entry->uip = uip;
3701                         }
3702                         vm_object_shadow(
3703                             &entry->object.vm_object,
3704                             &entry->offset,
3705                             atop(size));
3706                         entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3707                         eobject = entry->object.vm_object;
3708                         if (eobject->uip != NULL) {
3709                                 /*
3710                                  * The object was not shadowed.
3711                                  */
3712                                 swap_release_by_uid(size, entry->uip);
3713                                 uifree(entry->uip);
3714                                 entry->uip = NULL;
3715                         } else if (entry->uip != NULL) {
3716                                 VM_OBJECT_LOCK(eobject);
3717                                 eobject->uip = entry->uip;
3718                                 eobject->charge = size;
3719                                 VM_OBJECT_UNLOCK(eobject);
3720                                 entry->uip = NULL;
3721                         }
3722
3723                         vm_map_lock_downgrade(map);
3724                 } else {
3725                         /*
3726                          * We're attempting to read a copy-on-write page --
3727                          * don't allow writes.
3728                          */
3729                         prot &= ~VM_PROT_WRITE;
3730                 }
3731         }
3732
3733         /*
3734          * Create an object if necessary.
3735          */
3736         if (entry->object.vm_object == NULL &&
3737             !map->system_map) {
3738                 if (vm_map_lock_upgrade(map))
3739                         goto RetryLookup;
3740                 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3741                     atop(size));
3742                 entry->offset = 0;
3743                 if (entry->uip != NULL) {
3744                         VM_OBJECT_LOCK(entry->object.vm_object);
3745                         entry->object.vm_object->uip = entry->uip;
3746                         entry->object.vm_object->charge = size;
3747                         VM_OBJECT_UNLOCK(entry->object.vm_object);
3748                         entry->uip = NULL;
3749                 }
3750                 vm_map_lock_downgrade(map);
3751         }
3752
3753         /*
3754          * Return the object/offset from this entry.  If the entry was
3755          * copy-on-write or empty, it has been fixed up.
3756          */
3757         *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3758         *object = entry->object.vm_object;
3759
3760         *out_prot = prot;
3761         return (KERN_SUCCESS);
3762 }
3763
3764 /*
3765  *      vm_map_lookup_locked:
3766  *
3767  *      Lookup the faulting address.  A version of vm_map_lookup that returns 
3768  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
3769  */
3770 int
3771 vm_map_lookup_locked(vm_map_t *var_map,         /* IN/OUT */
3772                      vm_offset_t vaddr,
3773                      vm_prot_t fault_typea,
3774                      vm_map_entry_t *out_entry, /* OUT */
3775                      vm_object_t *object,       /* OUT */
3776                      vm_pindex_t *pindex,       /* OUT */
3777                      vm_prot_t *out_prot,       /* OUT */
3778                      boolean_t *wired)          /* OUT */
3779 {
3780         vm_map_entry_t entry;
3781         vm_map_t map = *var_map;
3782         vm_prot_t prot;
3783         vm_prot_t fault_type = fault_typea;
3784
3785         /*
3786          * Lookup the faulting address.
3787          */
3788         if (!vm_map_lookup_entry(map, vaddr, out_entry))
3789                 return (KERN_INVALID_ADDRESS);
3790
3791         entry = *out_entry;
3792
3793         /*
3794          * Fail if the entry refers to a submap.
3795          */
3796         if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3797                 return (KERN_FAILURE);
3798
3799         /*
3800          * Check whether this task is allowed to have this page.
3801          * Note the special case for MAP_ENTRY_COW
3802          * pages with an override.  This is to implement a forced
3803          * COW for debuggers.
3804          */
3805         if (fault_type & VM_PROT_OVERRIDE_WRITE)
3806                 prot = entry->max_protection;
3807         else
3808                 prot = entry->protection;
3809         fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
3810         if ((fault_type & prot) != fault_type)
3811                 return (KERN_PROTECTION_FAILURE);
3812         if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
3813             (entry->eflags & MAP_ENTRY_COW) &&
3814             (fault_type & VM_PROT_WRITE) &&
3815             (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
3816                 return (KERN_PROTECTION_FAILURE);
3817
3818         /*
3819          * If this page is not pageable, we have to get it for all possible
3820          * accesses.
3821          */
3822         *wired = (entry->wired_count != 0);
3823         if (*wired)
3824                 prot = fault_type = entry->protection;
3825
3826         if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3827                 /*
3828                  * Fail if the entry was copy-on-write for a write fault.
3829                  */
3830                 if (fault_type & VM_PROT_WRITE)
3831                         return (KERN_FAILURE);
3832                 /*
3833                  * We're attempting to read a copy-on-write page --
3834                  * don't allow writes.
3835                  */
3836                 prot &= ~VM_PROT_WRITE;
3837         }
3838
3839         /*
3840          * Fail if an object should be created.
3841          */
3842         if (entry->object.vm_object == NULL && !map->system_map)
3843                 return (KERN_FAILURE);
3844
3845         /*
3846          * Return the object/offset from this entry.  If the entry was
3847          * copy-on-write or empty, it has been fixed up.
3848          */
3849         *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3850         *object = entry->object.vm_object;
3851
3852         *out_prot = prot;
3853         return (KERN_SUCCESS);
3854 }
3855
3856 /*
3857  *      vm_map_lookup_done:
3858  *
3859  *      Releases locks acquired by a vm_map_lookup
3860  *      (according to the handle returned by that lookup).
3861  */
3862 void
3863 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3864 {
3865         /*
3866          * Unlock the main-level map
3867          */
3868         vm_map_unlock_read(map);
3869 }
3870
3871 #include "opt_ddb.h"
3872 #ifdef DDB
3873 #include <sys/kernel.h>
3874
3875 #include <ddb/ddb.h>
3876
3877 /*
3878  *      vm_map_print:   [ debug ]
3879  */
3880 DB_SHOW_COMMAND(map, vm_map_print)
3881 {
3882         static int nlines;
3883         /* XXX convert args. */
3884         vm_map_t map = (vm_map_t)addr;
3885         boolean_t full = have_addr;
3886
3887         vm_map_entry_t entry;
3888
3889         db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3890             (void *)map,
3891             (void *)map->pmap, map->nentries, map->timestamp);
3892         nlines++;
3893
3894         if (!full && db_indent)
3895                 return;
3896
3897         db_indent += 2;
3898         for (entry = map->header.next; entry != &map->header;
3899             entry = entry->next) {
3900                 db_iprintf("map entry %p: start=%p, end=%p\n",
3901                     (void *)entry, (void *)entry->start, (void *)entry->end);
3902                 nlines++;
3903                 {
3904                         static char *inheritance_name[4] =
3905                         {"share", "copy", "none", "donate_copy"};
3906
3907                         db_iprintf(" prot=%x/%x/%s",
3908                             entry->protection,
3909                             entry->max_protection,
3910                             inheritance_name[(int)(unsigned char)entry->inheritance]);
3911                         if (entry->wired_count != 0)
3912                                 db_printf(", wired");
3913                 }
3914                 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3915                         db_printf(", share=%p, offset=0x%jx\n",
3916                             (void *)entry->object.sub_map,
3917                             (uintmax_t)entry->offset);
3918                         nlines++;
3919                         if ((entry->prev == &map->header) ||
3920                             (entry->prev->object.sub_map !=
3921                                 entry->object.sub_map)) {
3922                                 db_indent += 2;
3923                                 vm_map_print((db_expr_t)(intptr_t)
3924                                              entry->object.sub_map,
3925                                              full, 0, (char *)0);
3926                                 db_indent -= 2;
3927                         }
3928                 } else {
3929                         if (entry->uip != NULL)
3930                                 db_printf(", uip %d", entry->uip->ui_uid);
3931                         db_printf(", object=%p, offset=0x%jx",
3932                             (void *)entry->object.vm_object,
3933                             (uintmax_t)entry->offset);
3934                         if (entry->object.vm_object && entry->object.vm_object->uip)
3935                                 db_printf(", obj uip %d charge %jx",
3936                                     entry->object.vm_object->uip->ui_uid,
3937                                     (uintmax_t)entry->object.vm_object->charge);
3938                         if (entry->eflags & MAP_ENTRY_COW)
3939                                 db_printf(", copy (%s)",
3940                                     (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3941                         db_printf("\n");
3942                         nlines++;
3943
3944                         if ((entry->prev == &map->header) ||
3945                             (entry->prev->object.vm_object !=
3946                                 entry->object.vm_object)) {
3947                                 db_indent += 2;
3948                                 vm_object_print((db_expr_t)(intptr_t)
3949                                                 entry->object.vm_object,
3950                                                 full, 0, (char *)0);
3951                                 nlines += 4;
3952                                 db_indent -= 2;
3953                         }
3954                 }
3955         }
3956         db_indent -= 2;
3957         if (db_indent == 0)
3958                 nlines = 0;
3959 }
3960
3961
3962 DB_SHOW_COMMAND(procvm, procvm)
3963 {
3964         struct proc *p;
3965
3966         if (have_addr) {
3967                 p = (struct proc *) addr;
3968         } else {
3969                 p = curproc;
3970         }
3971
3972         db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3973             (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3974             (void *)vmspace_pmap(p->p_vmspace));
3975
3976         vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
3977 }
3978
3979 #endif /* DDB */