160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
351369649SPedro F. Giffuni *
4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved.
6df8bae1dSRodney W. Grimes *
7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University.
9df8bae1dSRodney W. Grimes *
10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes * are met:
13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes * without specific prior written permission.
21df8bae1dSRodney W. Grimes *
22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes *
34df8bae1dSRodney W. Grimes *
35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes * All rights reserved.
37df8bae1dSRodney W. Grimes *
38df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes *
40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes *
46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes *
50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes *
52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes * School of Computer Science
54df8bae1dSRodney W. Grimes * Carnegie Mellon University
55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes *
57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes */
60df8bae1dSRodney W. Grimes
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes * Virtual memory mapping module.
63df8bae1dSRodney W. Grimes */
64df8bae1dSRodney W. Grimes
65df8bae1dSRodney W. Grimes #include <sys/param.h>
66df8bae1dSRodney W. Grimes #include <sys/systm.h>
67156e8654SKonstantin Belousov #include <sys/elf.h>
689a6d144fSKonstantin Belousov #include <sys/kernel.h>
6961d80e90SJohn Baldwin #include <sys/ktr.h>
70fb919e4dSMark Murray #include <sys/lock.h>
71fb919e4dSMark Murray #include <sys/mutex.h>
72b5e8ce9fSBruce Evans #include <sys/proc.h>
73efeaf95aSDavid Greenman #include <sys/vmmeter.h>
74867a482dSJohn Dyson #include <sys/mman.h>
751efb74fbSJohn Dyson #include <sys/vnode.h>
761ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
772267af78SJulian Elischer #include <sys/resourcevar.h>
7889f6b863SAttilio Rao #include <sys/rwlock.h>
793fde38dfSMike Silbersack #include <sys/file.h>
809a6d144fSKonstantin Belousov #include <sys/sysctl.h>
8105ba50f5SJake Burkholder #include <sys/sysent.h>
823db161e0SMatthew Dillon #include <sys/shm.h>
83df8bae1dSRodney W. Grimes
84df8bae1dSRodney W. Grimes #include <vm/vm.h>
85efeaf95aSDavid Greenman #include <vm/vm_param.h>
86efeaf95aSDavid Greenman #include <vm/pmap.h>
87efeaf95aSDavid Greenman #include <vm/vm_map.h>
88df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
8954a3a114SMark Johnston #include <vm/vm_pageout.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
9147221757SJohn Dyson #include <vm/vm_pager.h>
9226f9a767SRodney W. Grimes #include <vm/vm_kern.h>
93efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9484110e7eSKonstantin Belousov #include <vm/vnode_pager.h>
9521cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
96670d17b5SJeff Roberson #include <vm/uma.h>
97df8bae1dSRodney W. Grimes
98df8bae1dSRodney W. Grimes /*
99df8bae1dSRodney W. Grimes * Virtual memory maps provide for the mapping, protection,
100df8bae1dSRodney W. Grimes * and sharing of virtual memory objects. In addition,
101df8bae1dSRodney W. Grimes * this module provides for an efficient virtual copy of
102df8bae1dSRodney W. Grimes * memory from one map to another.
103df8bae1dSRodney W. Grimes *
104df8bae1dSRodney W. Grimes * Synchronization is required prior to most operations.
105df8bae1dSRodney W. Grimes *
106df8bae1dSRodney W. Grimes * Maps consist of an ordered doubly-linked list of simple
107e2abaaaaSAlan Cox * entries; a self-adjusting binary search tree of these
108e2abaaaaSAlan Cox * entries is used to speed up lookups.
109df8bae1dSRodney W. Grimes *
110956f3135SPhilippe Charnier * Since portions of maps are specified by start/end addresses,
111df8bae1dSRodney W. Grimes * which may not align with existing map entries, all
112df8bae1dSRodney W. Grimes * routines merely "clip" entries to these start/end values.
113df8bae1dSRodney W. Grimes * [That is, an entry is split into two, bordering at a
114df8bae1dSRodney W. Grimes * start or end value.] Note that these clippings may not
115df8bae1dSRodney W. Grimes * always be necessary (as the two resulting entries are then
116df8bae1dSRodney W. Grimes * not changed); however, the clipping is done for convenience.
117df8bae1dSRodney W. Grimes *
118df8bae1dSRodney W. Grimes * As mentioned above, virtual copy operations are performed
119ad5fca3bSAlan Cox * by copying VM object references from one map to
120df8bae1dSRodney W. Grimes * another, and then marking both regions as copy-on-write.
121df8bae1dSRodney W. Grimes */
122df8bae1dSRodney W. Grimes
1233a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1248355f576SJeff Roberson static uma_zone_t mapentzone;
1258355f576SJeff Roberson static uma_zone_t kmapentzone;
1268355f576SJeff Roberson static uma_zone_t vmspace_zone;
127b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
12892351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
12992351f16SAlan Cox vm_offset_t max);
1300b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
131655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
13203462509SAlan Cox static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
13319bd0d9cSKonstantin Belousov static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
13419bd0d9cSKonstantin Belousov vm_map_entry_t gap_entry);
135077ec27cSAlan Cox static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
136077ec27cSAlan Cox vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
1378355f576SJeff Roberson #ifdef INVARIANTS
1388355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1398355f576SJeff Roberson #endif
1404648ba0aSKonstantin Belousov static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
1414648ba0aSKonstantin Belousov vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
1424648ba0aSKonstantin Belousov int cow);
14366cd575bSAlan Cox static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
14466cd575bSAlan Cox vm_offset_t failed_addr);
145b18bfc3dSJohn Dyson
1460fb6aae7SKonstantin Belousov #define CONTAINS_BITS(set, bits) ((~(set) & (bits)) == 0)
1470fb6aae7SKonstantin Belousov
148ef694c1aSEdward Tomasz Napierala #define ENTRY_CHARGED(e) ((e)->cred != NULL || \
149ef694c1aSEdward Tomasz Napierala ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
1503364c323SKonstantin Belousov !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1513364c323SKonstantin Belousov
15257051fdcSTor Egge /*
15357051fdcSTor Egge * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
15457051fdcSTor Egge * stable.
15557051fdcSTor Egge */
15657051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
15757051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
15857051fdcSTor Egge
159d239bd3cSKonstantin Belousov /*
160d239bd3cSKonstantin Belousov * VM_MAP_RANGE_CHECK: [ internal use only ]
161d239bd3cSKonstantin Belousov *
162d239bd3cSKonstantin Belousov * Asserts that the starting and ending region
163d239bd3cSKonstantin Belousov * addresses fall within the valid range of the map.
164d239bd3cSKonstantin Belousov */
165d239bd3cSKonstantin Belousov #define VM_MAP_RANGE_CHECK(map, start, end) \
166d239bd3cSKonstantin Belousov { \
167d239bd3cSKonstantin Belousov if (start < vm_map_min(map)) \
168d239bd3cSKonstantin Belousov start = vm_map_min(map); \
169d239bd3cSKonstantin Belousov if (end > vm_map_max(map)) \
170d239bd3cSKonstantin Belousov end = vm_map_max(map); \
171d239bd3cSKonstantin Belousov if (start > end) \
172d239bd3cSKonstantin Belousov start = end; \
173d239bd3cSKonstantin Belousov }
174d239bd3cSKonstantin Belousov
175da76d349SBojan Novković #ifndef UMA_USE_DMAP
17620f02659SMark Johnston
17720f02659SMark Johnston /*
17820f02659SMark Johnston * Allocate a new slab for kernel map entries. The kernel map may be locked or
17920f02659SMark Johnston * unlocked, depending on whether the request is coming from the kernel map or a
18020f02659SMark Johnston * submap. This function allocates a virtual address range directly from the
18120f02659SMark Johnston * kernel map instead of the kmem_* layer to avoid recursion on the kernel map
18220f02659SMark Johnston * lock and also to avoid triggering allocator recursion in the vmem boundary
18320f02659SMark Johnston * tag allocator.
18420f02659SMark Johnston */
18520f02659SMark Johnston static void *
kmapent_alloc(uma_zone_t zone,vm_size_t bytes,int domain,uint8_t * pflag,int wait)18620f02659SMark Johnston kmapent_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
18720f02659SMark Johnston int wait)
18820f02659SMark Johnston {
18920f02659SMark Johnston vm_offset_t addr;
19020f02659SMark Johnston int error, locked;
19120f02659SMark Johnston
19220f02659SMark Johnston *pflag = UMA_SLAB_PRIV;
19320f02659SMark Johnston
19420f02659SMark Johnston if (!(locked = vm_map_locked(kernel_map)))
19520f02659SMark Johnston vm_map_lock(kernel_map);
19620f02659SMark Johnston addr = vm_map_findspace(kernel_map, vm_map_min(kernel_map), bytes);
19720f02659SMark Johnston if (addr + bytes < addr || addr + bytes > vm_map_max(kernel_map))
19820f02659SMark Johnston panic("%s: kernel map is exhausted", __func__);
19920f02659SMark Johnston error = vm_map_insert(kernel_map, NULL, 0, addr, addr + bytes,
20020f02659SMark Johnston VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
20120f02659SMark Johnston if (error != KERN_SUCCESS)
20220f02659SMark Johnston panic("%s: vm_map_insert() failed: %d", __func__, error);
20320f02659SMark Johnston if (!locked)
20420f02659SMark Johnston vm_map_unlock(kernel_map);
20520f02659SMark Johnston error = kmem_back_domain(domain, kernel_object, addr, bytes, M_NOWAIT |
20620f02659SMark Johnston M_USE_RESERVE | (wait & M_ZERO));
20720f02659SMark Johnston if (error == KERN_SUCCESS) {
20820f02659SMark Johnston return ((void *)addr);
20920f02659SMark Johnston } else {
21020f02659SMark Johnston if (!locked)
21120f02659SMark Johnston vm_map_lock(kernel_map);
21220f02659SMark Johnston vm_map_delete(kernel_map, addr, bytes);
21320f02659SMark Johnston if (!locked)
21420f02659SMark Johnston vm_map_unlock(kernel_map);
21520f02659SMark Johnston return (NULL);
21620f02659SMark Johnston }
21720f02659SMark Johnston }
21820f02659SMark Johnston
21920f02659SMark Johnston static void
kmapent_free(void * item,vm_size_t size,uint8_t pflag)22020f02659SMark Johnston kmapent_free(void *item, vm_size_t size, uint8_t pflag)
22120f02659SMark Johnston {
22220f02659SMark Johnston vm_offset_t addr;
223b8ebd99aSJohn Baldwin int error __diagused;
22420f02659SMark Johnston
22520f02659SMark Johnston if ((pflag & UMA_SLAB_PRIV) == 0)
22620f02659SMark Johnston /* XXX leaked */
22720f02659SMark Johnston return;
22820f02659SMark Johnston
22920f02659SMark Johnston addr = (vm_offset_t)item;
23020f02659SMark Johnston kmem_unback(kernel_object, addr, size);
23120f02659SMark Johnston error = vm_map_remove(kernel_map, addr, addr + size);
23220f02659SMark Johnston KASSERT(error == KERN_SUCCESS,
23320f02659SMark Johnston ("%s: vm_map_remove failed: %d", __func__, error));
23420f02659SMark Johnston }
23520f02659SMark Johnston
23620f02659SMark Johnston /*
23720f02659SMark Johnston * The worst-case upper bound on the number of kernel map entries that may be
23820f02659SMark Johnston * created before the zone must be replenished in _vm_map_unlock().
23920f02659SMark Johnston */
24020f02659SMark Johnston #define KMAPENT_RESERVE 1
24120f02659SMark Johnston
24220f02659SMark Johnston #endif /* !UMD_MD_SMALL_ALLOC */
24320f02659SMark Johnston
2446fecb26bSKonstantin Belousov /*
2456fecb26bSKonstantin Belousov * vm_map_startup:
2466fecb26bSKonstantin Belousov *
24720f02659SMark Johnston * Initialize the vm_map module. Must be called before any other vm_map
24820f02659SMark Johnston * routines.
2496fecb26bSKonstantin Belousov *
25020f02659SMark Johnston * User map and entry structures are allocated from the general purpose
25120f02659SMark Johnston * memory pool. Kernel maps are statically defined. Kernel map entries
25220f02659SMark Johnston * require special handling to avoid recursion; see the comments above
25320f02659SMark Johnston * kmapent_alloc() and in vm_map_entry_create().
2546fecb26bSKonstantin Belousov */
2550d94caffSDavid Greenman void
vm_map_startup(void)2561b40f8c0SMatthew Dillon vm_map_startup(void)
257df8bae1dSRodney W. Grimes {
2583a92e5d5SAlan Cox mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
25920f02659SMark Johnston
26020f02659SMark Johnston /*
26120f02659SMark Johnston * Disable the use of per-CPU buckets: map entry allocation is
26220f02659SMark Johnston * serialized by the kernel map lock.
26320f02659SMark Johnston */
264670d17b5SJeff Roberson kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
26518aa2de5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
26620f02659SMark Johnston UMA_ZONE_VM | UMA_ZONE_NOBUCKET);
267da76d349SBojan Novković #ifndef UMA_USE_DMAP
26820f02659SMark Johnston /* Reserve an extra map entry for use when replenishing the reserve. */
26920f02659SMark Johnston uma_zone_reserve(kmapentzone, KMAPENT_RESERVE + 1);
27020f02659SMark Johnston uma_prealloc(kmapentzone, KMAPENT_RESERVE + 1);
27120f02659SMark Johnston uma_zone_set_allocf(kmapentzone, kmapent_alloc);
27220f02659SMark Johnston uma_zone_set_freef(kmapentzone, kmapent_free);
27320f02659SMark Johnston #endif
27420f02659SMark Johnston
275670d17b5SJeff Roberson mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
276670d17b5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2775df87b21SJeff Roberson vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2785df87b21SJeff Roberson #ifdef INVARIANTS
2795df87b21SJeff Roberson vmspace_zdtor,
2805df87b21SJeff Roberson #else
2815df87b21SJeff Roberson NULL,
2825df87b21SJeff Roberson #endif
283f872f6eaSAlan Cox vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2848355f576SJeff Roberson }
2858355f576SJeff Roberson
286b23f72e9SBrian Feldman static int
vmspace_zinit(void * mem,int size,int flags)287b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2888355f576SJeff Roberson {
2898355f576SJeff Roberson struct vmspace *vm;
2908355f576SJeff Roberson vm_map_t map;
2918355f576SJeff Roberson
2927dd979dfSMark Johnston vm = (struct vmspace *)mem;
2937dd979dfSMark Johnston map = &vm->vm_map;
2947dd979dfSMark Johnston
295763d9566STim Kientzle memset(map, 0, sizeof(*map));
2967dd979dfSMark Johnston mtx_init(&map->system_mtx, "vm map (system)", NULL,
2977dd979dfSMark Johnston MTX_DEF | MTX_DUPOK);
298e30df26eSAlan Cox sx_init(&map->lock, "vm map (user)");
2997dd979dfSMark Johnston PMAP_LOCK_INIT(vmspace_pmap(vm));
300b23f72e9SBrian Feldman return (0);
3018355f576SJeff Roberson }
3028355f576SJeff Roberson
3038355f576SJeff Roberson #ifdef INVARIANTS
3048355f576SJeff Roberson static void
vmspace_zdtor(void * mem,int size,void * arg)3058355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
3068355f576SJeff Roberson {
3078355f576SJeff Roberson struct vmspace *vm;
3088355f576SJeff Roberson
3098355f576SJeff Roberson vm = (struct vmspace *)mem;
3107dd979dfSMark Johnston KASSERT(vm->vm_map.nentries == 0,
3117dd979dfSMark Johnston ("vmspace %p nentries == %d on free", vm, vm->vm_map.nentries));
3127dd979dfSMark Johnston KASSERT(vm->vm_map.size == 0,
3137dd979dfSMark Johnston ("vmspace %p size == %ju on free", vm, (uintmax_t)vm->vm_map.size));
3148355f576SJeff Roberson }
3158355f576SJeff Roberson #endif /* INVARIANTS */
3168355f576SJeff Roberson
317df8bae1dSRodney W. Grimes /*
318df8bae1dSRodney W. Grimes * Allocate a vmspace structure, including a vm_map and pmap,
319df8bae1dSRodney W. Grimes * and initialize those structures. The refcnt is set to 1.
320df8bae1dSRodney W. Grimes */
321df8bae1dSRodney W. Grimes struct vmspace *
vmspace_alloc(vm_offset_t min,vm_offset_t max,pmap_pinit_t pinit)32274d1d2b7SNeel Natu vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
323df8bae1dSRodney W. Grimes {
324c0877f10SJohn Dyson struct vmspace *vm;
3250d94caffSDavid Greenman
326a163d034SWarner Losh vm = uma_zalloc(vmspace_zone, M_WAITOK);
32774d1d2b7SNeel Natu KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
32874d1d2b7SNeel Natu if (!pinit(vmspace_pmap(vm))) {
32989b57fcfSKonstantin Belousov uma_zfree(vmspace_zone, vm);
33089b57fcfSKonstantin Belousov return (NULL);
33189b57fcfSKonstantin Belousov }
33221c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_alloc: %p", vm);
33392351f16SAlan Cox _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
334f7db0c95SMark Johnston refcount_init(&vm->vm_refcnt, 1);
3352d8acc0fSJohn Dyson vm->vm_shm = NULL;
33651ab6c28SAlan Cox vm->vm_swrss = 0;
33751ab6c28SAlan Cox vm->vm_tsize = 0;
33851ab6c28SAlan Cox vm->vm_dsize = 0;
33951ab6c28SAlan Cox vm->vm_ssize = 0;
34051ab6c28SAlan Cox vm->vm_taddr = 0;
34151ab6c28SAlan Cox vm->vm_daddr = 0;
34251ab6c28SAlan Cox vm->vm_maxsaddr = 0;
343df8bae1dSRodney W. Grimes return (vm);
344df8bae1dSRodney W. Grimes }
345df8bae1dSRodney W. Grimes
3464b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
3471ba5ad42SEdward Tomasz Napierala static void
vmspace_container_reset(struct proc * p)3481ba5ad42SEdward Tomasz Napierala vmspace_container_reset(struct proc *p)
3491ba5ad42SEdward Tomasz Napierala {
3501ba5ad42SEdward Tomasz Napierala
3511ba5ad42SEdward Tomasz Napierala PROC_LOCK(p);
3521ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_DATA, 0);
3531ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_STACK, 0);
3541ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_RSS, 0);
3551ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_MEMLOCK, 0);
3561ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_VMEM, 0);
3571ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p);
3581ba5ad42SEdward Tomasz Napierala }
3594b5c9cf6SEdward Tomasz Napierala #endif
3601ba5ad42SEdward Tomasz Napierala
36162a59e8fSWarner Losh static inline void
vmspace_dofree(struct vmspace * vm)362582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
363df8bae1dSRodney W. Grimes {
3640ef12795SAlan Cox
36521c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_free: %p", vm);
3663db161e0SMatthew Dillon
3673db161e0SMatthew Dillon /*
3683db161e0SMatthew Dillon * Make sure any SysV shm is freed, it might not have been in
3693db161e0SMatthew Dillon * exit1().
3703db161e0SMatthew Dillon */
3713db161e0SMatthew Dillon shmexit(vm);
3723db161e0SMatthew Dillon
37330dcfc09SJohn Dyson /*
374df8bae1dSRodney W. Grimes * Lock the map, to wait out all other references to it.
3750d94caffSDavid Greenman * Delete all of the mappings and pages they hold, then call
3760d94caffSDavid Greenman * the pmap module to reclaim anything left.
377df8bae1dSRodney W. Grimes */
378f0165b1cSKonstantin Belousov (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
379f0165b1cSKonstantin Belousov vm_map_max(&vm->vm_map));
3808355f576SJeff Roberson
3810ef12795SAlan Cox pmap_release(vmspace_pmap(vm));
3820ef12795SAlan Cox vm->vm_map.pmap = NULL;
3838355f576SJeff Roberson uma_zfree(vmspace_zone, vm);
384df8bae1dSRodney W. Grimes }
385582ec34cSAlfred Perlstein
386582ec34cSAlfred Perlstein void
vmspace_free(struct vmspace * vm)387582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
388582ec34cSAlfred Perlstein {
389582ec34cSAlfred Perlstein
390423521aaSRyan Stone WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
391164a37a5SJohn Baldwin "vmspace_free() called");
392423521aaSRyan Stone
393f7db0c95SMark Johnston if (refcount_release(&vm->vm_refcnt))
394582ec34cSAlfred Perlstein vmspace_dofree(vm);
395582ec34cSAlfred Perlstein }
396582ec34cSAlfred Perlstein
397582ec34cSAlfred Perlstein void
vmspace_exitfree(struct proc * p)398582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
399582ec34cSAlfred Perlstein {
400334f7061SPeter Wemm struct vmspace *vm;
401582ec34cSAlfred Perlstein
40257051fdcSTor Egge PROC_VMSPACE_LOCK(p);
403334f7061SPeter Wemm vm = p->p_vmspace;
404334f7061SPeter Wemm p->p_vmspace = NULL;
40557051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
40657051fdcSTor Egge KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
40757051fdcSTor Egge vmspace_free(vm);
40857051fdcSTor Egge }
40957051fdcSTor Egge
41057051fdcSTor Egge void
vmspace_exit(struct thread * td)41157051fdcSTor Egge vmspace_exit(struct thread *td)
41257051fdcSTor Egge {
41357051fdcSTor Egge struct vmspace *vm;
41457051fdcSTor Egge struct proc *p;
415f7db0c95SMark Johnston bool released;
41657051fdcSTor Egge
41757051fdcSTor Egge p = td->td_proc;
41857051fdcSTor Egge vm = p->p_vmspace;
419f7db0c95SMark Johnston
420f7db0c95SMark Johnston /*
421f7db0c95SMark Johnston * Prepare to release the vmspace reference. The thread that releases
422f7db0c95SMark Johnston * the last reference is responsible for tearing down the vmspace.
423f7db0c95SMark Johnston * However, threads not releasing the final reference must switch to the
424f7db0c95SMark Johnston * kernel's vmspace0 before the decrement so that the subsequent pmap
425f7db0c95SMark Johnston * deactivation does not modify a freed vmspace.
426f7db0c95SMark Johnston */
427f7db0c95SMark Johnston refcount_acquire(&vmspace0.vm_refcnt);
428f7db0c95SMark Johnston if (!(released = refcount_release_if_last(&vm->vm_refcnt))) {
429f7db0c95SMark Johnston if (p->p_vmspace != &vmspace0) {
43057051fdcSTor Egge PROC_VMSPACE_LOCK(p);
43157051fdcSTor Egge p->p_vmspace = &vmspace0;
43257051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
43357051fdcSTor Egge pmap_activate(td);
43457051fdcSTor Egge }
435f7db0c95SMark Johnston released = refcount_release(&vm->vm_refcnt);
436f7db0c95SMark Johnston }
437f7db0c95SMark Johnston if (released) {
438f7db0c95SMark Johnston /*
439f7db0c95SMark Johnston * pmap_remove_pages() expects the pmap to be active, so switch
440f7db0c95SMark Johnston * back first if necessary.
441f7db0c95SMark Johnston */
44257051fdcSTor Egge if (p->p_vmspace != vm) {
44357051fdcSTor Egge PROC_VMSPACE_LOCK(p);
44457051fdcSTor Egge p->p_vmspace = vm;
44557051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
44657051fdcSTor Egge pmap_activate(td);
44757051fdcSTor Egge }
44857051fdcSTor Egge pmap_remove_pages(vmspace_pmap(vm));
44957051fdcSTor Egge PROC_VMSPACE_LOCK(p);
45057051fdcSTor Egge p->p_vmspace = &vmspace0;
45157051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
45257051fdcSTor Egge pmap_activate(td);
453334f7061SPeter Wemm vmspace_dofree(vm);
454334f7061SPeter Wemm }
4554b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4564b5c9cf6SEdward Tomasz Napierala if (racct_enable)
4571ba5ad42SEdward Tomasz Napierala vmspace_container_reset(p);
4584b5c9cf6SEdward Tomasz Napierala #endif
45957051fdcSTor Egge }
46057051fdcSTor Egge
46157051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
46257051fdcSTor Egge
46357051fdcSTor Egge struct vmspace *
vmspace_acquire_ref(struct proc * p)46457051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
46557051fdcSTor Egge {
46657051fdcSTor Egge struct vmspace *vm;
46757051fdcSTor Egge
46857051fdcSTor Egge PROC_VMSPACE_LOCK(p);
46957051fdcSTor Egge vm = p->p_vmspace;
470f7db0c95SMark Johnston if (vm == NULL || !refcount_acquire_if_not_zero(&vm->vm_refcnt)) {
47157051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
47257051fdcSTor Egge return (NULL);
47357051fdcSTor Egge }
47457051fdcSTor Egge if (vm != p->p_vmspace) {
47557051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
47657051fdcSTor Egge vmspace_free(vm);
47757051fdcSTor Egge return (NULL);
47857051fdcSTor Egge }
47957051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
48057051fdcSTor Egge return (vm);
48157051fdcSTor Egge }
482df8bae1dSRodney W. Grimes
4838a4dc40fSJohn Baldwin /*
4848a4dc40fSJohn Baldwin * Switch between vmspaces in an AIO kernel process.
4858a4dc40fSJohn Baldwin *
4860b96ca33SJohn Baldwin * The new vmspace is either the vmspace of a user process obtained
4870b96ca33SJohn Baldwin * from an active AIO request or the initial vmspace of the AIO kernel
4880b96ca33SJohn Baldwin * process (when it is idling). Because user processes will block to
4890b96ca33SJohn Baldwin * drain any active AIO requests before proceeding in exit() or
4900b96ca33SJohn Baldwin * execve(), the reference count for vmspaces from AIO requests can
4910b96ca33SJohn Baldwin * never be 0. Similarly, AIO kernel processes hold an extra
4920b96ca33SJohn Baldwin * reference on their initial vmspace for the life of the process. As
4930b96ca33SJohn Baldwin * a result, the 'newvm' vmspace always has a non-zero reference
4940b96ca33SJohn Baldwin * count. This permits an additional reference on 'newvm' to be
4950b96ca33SJohn Baldwin * acquired via a simple atomic increment rather than the loop in
4960b96ca33SJohn Baldwin * vmspace_acquire_ref() above.
4978a4dc40fSJohn Baldwin */
4988a4dc40fSJohn Baldwin void
vmspace_switch_aio(struct vmspace * newvm)4998a4dc40fSJohn Baldwin vmspace_switch_aio(struct vmspace *newvm)
5008a4dc40fSJohn Baldwin {
5018a4dc40fSJohn Baldwin struct vmspace *oldvm;
5028a4dc40fSJohn Baldwin
5038a4dc40fSJohn Baldwin /* XXX: Need some way to assert that this is an aio daemon. */
5048a4dc40fSJohn Baldwin
505f7db0c95SMark Johnston KASSERT(refcount_load(&newvm->vm_refcnt) > 0,
5068a4dc40fSJohn Baldwin ("vmspace_switch_aio: newvm unreferenced"));
5078a4dc40fSJohn Baldwin
5088a4dc40fSJohn Baldwin oldvm = curproc->p_vmspace;
5098a4dc40fSJohn Baldwin if (oldvm == newvm)
5108a4dc40fSJohn Baldwin return;
5118a4dc40fSJohn Baldwin
5128a4dc40fSJohn Baldwin /*
5138a4dc40fSJohn Baldwin * Point to the new address space and refer to it.
5148a4dc40fSJohn Baldwin */
5158a4dc40fSJohn Baldwin curproc->p_vmspace = newvm;
516f7db0c95SMark Johnston refcount_acquire(&newvm->vm_refcnt);
5178a4dc40fSJohn Baldwin
5188a4dc40fSJohn Baldwin /* Activate the new mapping. */
5198a4dc40fSJohn Baldwin pmap_activate(curthread);
5208a4dc40fSJohn Baldwin
5218a4dc40fSJohn Baldwin vmspace_free(oldvm);
5228a4dc40fSJohn Baldwin }
5238a4dc40fSJohn Baldwin
5241b40f8c0SMatthew Dillon void
_vm_map_lock(vm_map_t map,const char * file,int line)525780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
5261b40f8c0SMatthew Dillon {
527bc91c510SAlan Cox
52893bc4879SAlan Cox if (map->system_map)
529ccdf2333SAttilio Rao mtx_lock_flags_(&map->system_mtx, 0, file, line);
53012c64974SMaxime Henrion else
5319fde98bbSAttilio Rao sx_xlock_(&map->lock, file, line);
5321b40f8c0SMatthew Dillon map->timestamp++;
5331b40f8c0SMatthew Dillon }
5341b40f8c0SMatthew Dillon
53578022527SKonstantin Belousov void
vm_map_entry_set_vnode_text(vm_map_entry_t entry,bool add)53678022527SKonstantin Belousov vm_map_entry_set_vnode_text(vm_map_entry_t entry, bool add)
53778022527SKonstantin Belousov {
53867388836SKonstantin Belousov vm_object_t object;
53978022527SKonstantin Belousov struct vnode *vp;
54067388836SKonstantin Belousov bool vp_held;
54178022527SKonstantin Belousov
54278022527SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_VN_EXEC) == 0)
54378022527SKonstantin Belousov return;
54478022527SKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
54578022527SKonstantin Belousov ("Submap with execs"));
54678022527SKonstantin Belousov object = entry->object.vm_object;
54778022527SKonstantin Belousov KASSERT(object != NULL, ("No object for text, entry %p", entry));
54867388836SKonstantin Belousov if ((object->flags & OBJ_ANON) != 0)
54967388836SKonstantin Belousov object = object->handle;
55067388836SKonstantin Belousov else
55167388836SKonstantin Belousov KASSERT(object->backing_object == NULL,
55267388836SKonstantin Belousov ("non-anon object %p shadows", object));
55367388836SKonstantin Belousov KASSERT(object != NULL, ("No content object for text, entry %p obj %p",
55467388836SKonstantin Belousov entry, entry->object.vm_object));
55578022527SKonstantin Belousov
55667388836SKonstantin Belousov /*
55767388836SKonstantin Belousov * Mostly, we do not lock the backing object. It is
55867388836SKonstantin Belousov * referenced by the entry we are processing, so it cannot go
55967388836SKonstantin Belousov * away.
56067388836SKonstantin Belousov */
561192112b7SKonstantin Belousov vm_pager_getvp(object, &vp, &vp_held);
56232d2014dSKonstantin Belousov if (vp != NULL) {
563bb9e2184SKonstantin Belousov if (add) {
56478022527SKonstantin Belousov VOP_SET_TEXT_CHECKED(vp);
565bb9e2184SKonstantin Belousov } else {
566bb9e2184SKonstantin Belousov vn_lock(vp, LK_SHARED | LK_RETRY);
567bb9e2184SKonstantin Belousov VOP_UNSET_TEXT_CHECKED(vp);
568b249ce48SMateusz Guzik VOP_UNLOCK(vp);
569bb9e2184SKonstantin Belousov }
57067388836SKonstantin Belousov if (vp_held)
57167388836SKonstantin Belousov vdrop(vp);
572bb9e2184SKonstantin Belousov }
57378022527SKonstantin Belousov }
57478022527SKonstantin Belousov
5757cdcf863SDoug Moore /*
5767cdcf863SDoug Moore * Use a different name for this vm_map_entry field when it's use
5777cdcf863SDoug Moore * is not consistent with its use as part of an ordered search tree.
5787cdcf863SDoug Moore */
5797cdcf863SDoug Moore #define defer_next right
5807cdcf863SDoug Moore
5810b367bd8SKonstantin Belousov static void
vm_map_process_deferred(void)5820b367bd8SKonstantin Belousov vm_map_process_deferred(void)
5830e0af8ecSBrian Feldman {
5840b367bd8SKonstantin Belousov struct thread *td;
5856fbe60faSJohn Baldwin vm_map_entry_t entry, next;
58684110e7eSKonstantin Belousov vm_object_t object;
587655c3490SKonstantin Belousov
5880b367bd8SKonstantin Belousov td = curthread;
5896fbe60faSJohn Baldwin entry = td->td_map_def_user;
5906fbe60faSJohn Baldwin td->td_map_def_user = NULL;
5916fbe60faSJohn Baldwin while (entry != NULL) {
5927cdcf863SDoug Moore next = entry->defer_next;
593fe7bcbafSKyle Evans MPASS((entry->eflags & (MAP_ENTRY_WRITECNT |
594fe7bcbafSKyle Evans MAP_ENTRY_VN_EXEC)) != (MAP_ENTRY_WRITECNT |
59578022527SKonstantin Belousov MAP_ENTRY_VN_EXEC));
596fe7bcbafSKyle Evans if ((entry->eflags & MAP_ENTRY_WRITECNT) != 0) {
59784110e7eSKonstantin Belousov /*
59884110e7eSKonstantin Belousov * Decrement the object's writemappings and
59984110e7eSKonstantin Belousov * possibly the vnode's v_writecount.
60084110e7eSKonstantin Belousov */
60184110e7eSKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
60284110e7eSKonstantin Belousov ("Submap with writecount"));
60384110e7eSKonstantin Belousov object = entry->object.vm_object;
60484110e7eSKonstantin Belousov KASSERT(object != NULL, ("No object for writecount"));
605fe7bcbafSKyle Evans vm_pager_release_writecount(object, entry->start,
60684110e7eSKonstantin Belousov entry->end);
60784110e7eSKonstantin Belousov }
60878022527SKonstantin Belousov vm_map_entry_set_vnode_text(entry, false);
6090b367bd8SKonstantin Belousov vm_map_entry_deallocate(entry, FALSE);
6106fbe60faSJohn Baldwin entry = next;
6110b367bd8SKonstantin Belousov }
6120b367bd8SKonstantin Belousov }
6130b367bd8SKonstantin Belousov
614461587dcSDoug Moore #ifdef INVARIANTS
615461587dcSDoug Moore static void
_vm_map_assert_locked(vm_map_t map,const char * file,int line)616461587dcSDoug Moore _vm_map_assert_locked(vm_map_t map, const char *file, int line)
617461587dcSDoug Moore {
618461587dcSDoug Moore
619461587dcSDoug Moore if (map->system_map)
620461587dcSDoug Moore mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
621461587dcSDoug Moore else
622461587dcSDoug Moore sx_assert_(&map->lock, SA_XLOCKED, file, line);
623461587dcSDoug Moore }
624461587dcSDoug Moore
625461587dcSDoug Moore #define VM_MAP_ASSERT_LOCKED(map) \
626461587dcSDoug Moore _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
627461587dcSDoug Moore
628461587dcSDoug Moore enum { VMMAP_CHECK_NONE, VMMAP_CHECK_UNLOCK, VMMAP_CHECK_ALL };
629461587dcSDoug Moore #ifdef DIAGNOSTIC
630461587dcSDoug Moore static int enable_vmmap_check = VMMAP_CHECK_UNLOCK;
631461587dcSDoug Moore #else
632461587dcSDoug Moore static int enable_vmmap_check = VMMAP_CHECK_NONE;
633461587dcSDoug Moore #endif
634461587dcSDoug Moore SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
635461587dcSDoug Moore &enable_vmmap_check, 0, "Enable vm map consistency checking");
636461587dcSDoug Moore
637461587dcSDoug Moore static void _vm_map_assert_consistent(vm_map_t map, int check);
638461587dcSDoug Moore
639461587dcSDoug Moore #define VM_MAP_ASSERT_CONSISTENT(map) \
640461587dcSDoug Moore _vm_map_assert_consistent(map, VMMAP_CHECK_ALL)
641461587dcSDoug Moore #ifdef DIAGNOSTIC
642461587dcSDoug Moore #define VM_MAP_UNLOCK_CONSISTENT(map) do { \
643461587dcSDoug Moore if (map->nupdates > map->nentries) { \
644461587dcSDoug Moore _vm_map_assert_consistent(map, VMMAP_CHECK_UNLOCK); \
645461587dcSDoug Moore map->nupdates = 0; \
646461587dcSDoug Moore } \
647461587dcSDoug Moore } while (0)
648461587dcSDoug Moore #else
649461587dcSDoug Moore #define VM_MAP_UNLOCK_CONSISTENT(map)
650461587dcSDoug Moore #endif
651461587dcSDoug Moore #else
652461587dcSDoug Moore #define VM_MAP_ASSERT_LOCKED(map)
653461587dcSDoug Moore #define VM_MAP_ASSERT_CONSISTENT(map)
654461587dcSDoug Moore #define VM_MAP_UNLOCK_CONSISTENT(map)
655461587dcSDoug Moore #endif /* INVARIANTS */
656461587dcSDoug Moore
6570b367bd8SKonstantin Belousov void
_vm_map_unlock(vm_map_t map,const char * file,int line)6580b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line)
6590b367bd8SKonstantin Belousov {
6600b367bd8SKonstantin Belousov
661461587dcSDoug Moore VM_MAP_UNLOCK_CONSISTENT(map);
66220f02659SMark Johnston if (map->system_map) {
663da76d349SBojan Novković #ifndef UMA_USE_DMAP
66420f02659SMark Johnston if (map == kernel_map && (map->flags & MAP_REPLENISH) != 0) {
66520f02659SMark Johnston uma_prealloc(kmapentzone, 1);
66620f02659SMark Johnston map->flags &= ~MAP_REPLENISH;
66720f02659SMark Johnston }
66820f02659SMark Johnston #endif
669ccdf2333SAttilio Rao mtx_unlock_flags_(&map->system_mtx, 0, file, line);
67020f02659SMark Johnston } else {
6719fde98bbSAttilio Rao sx_xunlock_(&map->lock, file, line);
6720b367bd8SKonstantin Belousov vm_map_process_deferred();
673655c3490SKonstantin Belousov }
6740e0af8ecSBrian Feldman }
6750e0af8ecSBrian Feldman
6760e0af8ecSBrian Feldman void
_vm_map_lock_read(vm_map_t map,const char * file,int line)677780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
6780e0af8ecSBrian Feldman {
679bc91c510SAlan Cox
68093bc4879SAlan Cox if (map->system_map)
681ccdf2333SAttilio Rao mtx_lock_flags_(&map->system_mtx, 0, file, line);
68212c64974SMaxime Henrion else
6839fde98bbSAttilio Rao sx_slock_(&map->lock, file, line);
68436daaecdSAlan Cox }
6850e0af8ecSBrian Feldman
6860e0af8ecSBrian Feldman void
_vm_map_unlock_read(vm_map_t map,const char * file,int line)687780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
6880e0af8ecSBrian Feldman {
689bc91c510SAlan Cox
69020f02659SMark Johnston if (map->system_map) {
69120f02659SMark Johnston KASSERT((map->flags & MAP_REPLENISH) == 0,
69220f02659SMark Johnston ("%s: MAP_REPLENISH leaked", __func__));
693ccdf2333SAttilio Rao mtx_unlock_flags_(&map->system_mtx, 0, file, line);
69420f02659SMark Johnston } else {
6959fde98bbSAttilio Rao sx_sunlock_(&map->lock, file, line);
6960b367bd8SKonstantin Belousov vm_map_process_deferred();
6970b367bd8SKonstantin Belousov }
69825adb370SBrian Feldman }
69925adb370SBrian Feldman
700d974f03cSAlan Cox int
_vm_map_trylock(vm_map_t map,const char * file,int line)701780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
702d974f03cSAlan Cox {
70325adb370SBrian Feldman int error;
70425adb370SBrian Feldman
70536daaecdSAlan Cox error = map->system_map ?
706ccdf2333SAttilio Rao !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
7079fde98bbSAttilio Rao !sx_try_xlock_(&map->lock, file, line);
7083a92e5d5SAlan Cox if (error == 0)
7093a92e5d5SAlan Cox map->timestamp++;
710bc91c510SAlan Cox return (error == 0);
7110e0af8ecSBrian Feldman }
7120e0af8ecSBrian Feldman
7130e0af8ecSBrian Feldman int
_vm_map_trylock_read(vm_map_t map,const char * file,int line)71472d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
71572d97679SDavid Schultz {
71672d97679SDavid Schultz int error;
71772d97679SDavid Schultz
71872d97679SDavid Schultz error = map->system_map ?
719ccdf2333SAttilio Rao !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
7209fde98bbSAttilio Rao !sx_try_slock_(&map->lock, file, line);
72172d97679SDavid Schultz return (error == 0);
72272d97679SDavid Schultz }
72372d97679SDavid Schultz
72405a8c414SAlan Cox /*
72505a8c414SAlan Cox * _vm_map_lock_upgrade: [ internal use only ]
72605a8c414SAlan Cox *
72705a8c414SAlan Cox * Tries to upgrade a read (shared) lock on the specified map to a write
72805a8c414SAlan Cox * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
72905a8c414SAlan Cox * non-zero value if the upgrade fails. If the upgrade fails, the map is
73005a8c414SAlan Cox * returned without a read or write lock held.
73105a8c414SAlan Cox *
73205a8c414SAlan Cox * Requires that the map be read locked.
73305a8c414SAlan Cox */
73472d97679SDavid Schultz int
_vm_map_lock_upgrade(vm_map_t map,const char * file,int line)735780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
7360e0af8ecSBrian Feldman {
73705a8c414SAlan Cox unsigned int last_timestamp;
738bc91c510SAlan Cox
73912c64974SMaxime Henrion if (map->system_map) {
740ccdf2333SAttilio Rao mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
74105a8c414SAlan Cox } else {
7429fde98bbSAttilio Rao if (!sx_try_upgrade_(&map->lock, file, line)) {
74305a8c414SAlan Cox last_timestamp = map->timestamp;
7449fde98bbSAttilio Rao sx_sunlock_(&map->lock, file, line);
7450b367bd8SKonstantin Belousov vm_map_process_deferred();
74605a8c414SAlan Cox /*
74705a8c414SAlan Cox * If the map's timestamp does not change while the
74805a8c414SAlan Cox * map is unlocked, then the upgrade succeeds.
74905a8c414SAlan Cox */
7509fde98bbSAttilio Rao sx_xlock_(&map->lock, file, line);
75105a8c414SAlan Cox if (last_timestamp != map->timestamp) {
7529fde98bbSAttilio Rao sx_xunlock_(&map->lock, file, line);
75305a8c414SAlan Cox return (1);
75405a8c414SAlan Cox }
75505a8c414SAlan Cox }
75605a8c414SAlan Cox }
757bc91c510SAlan Cox map->timestamp++;
758bc91c510SAlan Cox return (0);
7590e0af8ecSBrian Feldman }
7600e0af8ecSBrian Feldman
7610e0af8ecSBrian Feldman void
_vm_map_lock_downgrade(vm_map_t map,const char * file,int line)762780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
7631b40f8c0SMatthew Dillon {
764bc91c510SAlan Cox
76512c64974SMaxime Henrion if (map->system_map) {
76620f02659SMark Johnston KASSERT((map->flags & MAP_REPLENISH) == 0,
76720f02659SMark Johnston ("%s: MAP_REPLENISH leaked", __func__));
768ccdf2333SAttilio Rao mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
769461587dcSDoug Moore } else {
770461587dcSDoug Moore VM_MAP_UNLOCK_CONSISTENT(map);
7719fde98bbSAttilio Rao sx_downgrade_(&map->lock, file, line);
77205a8c414SAlan Cox }
773461587dcSDoug Moore }
77405a8c414SAlan Cox
77505a8c414SAlan Cox /*
77605a8c414SAlan Cox * vm_map_locked:
77705a8c414SAlan Cox *
77805a8c414SAlan Cox * Returns a non-zero value if the caller holds a write (exclusive) lock
77905a8c414SAlan Cox * on the specified map and the value "0" otherwise.
78005a8c414SAlan Cox */
78105a8c414SAlan Cox int
vm_map_locked(vm_map_t map)78205a8c414SAlan Cox vm_map_locked(vm_map_t map)
78305a8c414SAlan Cox {
78405a8c414SAlan Cox
78505a8c414SAlan Cox if (map->system_map)
78605a8c414SAlan Cox return (mtx_owned(&map->system_mtx));
78705a8c414SAlan Cox else
78805a8c414SAlan Cox return (sx_xlocked(&map->lock));
78925adb370SBrian Feldman }
79025adb370SBrian Feldman
791acd9a301SAlan Cox /*
7928304adaaSAlan Cox * _vm_map_unlock_and_wait:
7938304adaaSAlan Cox *
7948304adaaSAlan Cox * Atomically releases the lock on the specified map and puts the calling
7958304adaaSAlan Cox * thread to sleep. The calling thread will remain asleep until either
7968304adaaSAlan Cox * vm_map_wakeup() is performed on the map or the specified timeout is
7978304adaaSAlan Cox * exceeded.
7988304adaaSAlan Cox *
7998304adaaSAlan Cox * WARNING! This function does not perform deferred deallocations of
8008304adaaSAlan Cox * objects and map entries. Therefore, the calling thread is expected to
8018304adaaSAlan Cox * reacquire the map lock after reawakening and later perform an ordinary
8028304adaaSAlan Cox * unlock operation, such as vm_map_unlock(), before completing its
8038304adaaSAlan Cox * operation on the map.
804acd9a301SAlan Cox */
8059688f931SAlan Cox int
_vm_map_unlock_and_wait(vm_map_t map,int timo,const char * file,int line)8068304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
807acd9a301SAlan Cox {
808acd9a301SAlan Cox
809461587dcSDoug Moore VM_MAP_UNLOCK_CONSISTENT(map);
8103a92e5d5SAlan Cox mtx_lock(&map_sleep_mtx);
81120f02659SMark Johnston if (map->system_map) {
81220f02659SMark Johnston KASSERT((map->flags & MAP_REPLENISH) == 0,
81320f02659SMark Johnston ("%s: MAP_REPLENISH leaked", __func__));
814ccdf2333SAttilio Rao mtx_unlock_flags_(&map->system_mtx, 0, file, line);
81520f02659SMark Johnston } else {
8169fde98bbSAttilio Rao sx_xunlock_(&map->lock, file, line);
81720f02659SMark Johnston }
8188304adaaSAlan Cox return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
8198304adaaSAlan Cox timo));
820acd9a301SAlan Cox }
821acd9a301SAlan Cox
822acd9a301SAlan Cox /*
823acd9a301SAlan Cox * vm_map_wakeup:
8248304adaaSAlan Cox *
8258304adaaSAlan Cox * Awaken any threads that have slept on the map using
8268304adaaSAlan Cox * vm_map_unlock_and_wait().
827acd9a301SAlan Cox */
8289688f931SAlan Cox void
vm_map_wakeup(vm_map_t map)829acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
830acd9a301SAlan Cox {
831acd9a301SAlan Cox
832b49ecb86SAlan Cox /*
8333a92e5d5SAlan Cox * Acquire and release map_sleep_mtx to prevent a wakeup()
8348304adaaSAlan Cox * from being performed (and lost) between the map unlock
8358304adaaSAlan Cox * and the msleep() in _vm_map_unlock_and_wait().
836b49ecb86SAlan Cox */
8373a92e5d5SAlan Cox mtx_lock(&map_sleep_mtx);
8383a92e5d5SAlan Cox mtx_unlock(&map_sleep_mtx);
839acd9a301SAlan Cox wakeup(&map->root);
840acd9a301SAlan Cox }
841acd9a301SAlan Cox
842a5db445dSMax Laier void
vm_map_busy(vm_map_t map)843a5db445dSMax Laier vm_map_busy(vm_map_t map)
844a5db445dSMax Laier {
845a5db445dSMax Laier
846a5db445dSMax Laier VM_MAP_ASSERT_LOCKED(map);
847a5db445dSMax Laier map->busy++;
848a5db445dSMax Laier }
849a5db445dSMax Laier
850a5db445dSMax Laier void
vm_map_unbusy(vm_map_t map)851a5db445dSMax Laier vm_map_unbusy(vm_map_t map)
852a5db445dSMax Laier {
853a5db445dSMax Laier
854a5db445dSMax Laier VM_MAP_ASSERT_LOCKED(map);
855a5db445dSMax Laier KASSERT(map->busy, ("vm_map_unbusy: not busy"));
856a5db445dSMax Laier if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
857a5db445dSMax Laier vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
858a5db445dSMax Laier wakeup(&map->busy);
859a5db445dSMax Laier }
860a5db445dSMax Laier }
861a5db445dSMax Laier
862a5db445dSMax Laier void
vm_map_wait_busy(vm_map_t map)863a5db445dSMax Laier vm_map_wait_busy(vm_map_t map)
864a5db445dSMax Laier {
865a5db445dSMax Laier
866a5db445dSMax Laier VM_MAP_ASSERT_LOCKED(map);
867a5db445dSMax Laier while (map->busy) {
868a5db445dSMax Laier vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
869a5db445dSMax Laier if (map->system_map)
870a5db445dSMax Laier msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
871a5db445dSMax Laier else
872a5db445dSMax Laier sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
873a5db445dSMax Laier }
874a5db445dSMax Laier map->timestamp++;
875a5db445dSMax Laier }
876a5db445dSMax Laier
8771b40f8c0SMatthew Dillon long
vmspace_resident_count(struct vmspace * vmspace)8781b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
8791b40f8c0SMatthew Dillon {
8801b40f8c0SMatthew Dillon return pmap_resident_count(vmspace_pmap(vmspace));
8811b40f8c0SMatthew Dillon }
8821b40f8c0SMatthew Dillon
883ff2b5645SMatthew Dillon /*
884df8bae1dSRodney W. Grimes * Initialize an existing vm_map structure
885df8bae1dSRodney W. Grimes * such as that in the vmspace structure.
886df8bae1dSRodney W. Grimes */
8878355f576SJeff Roberson static void
_vm_map_init(vm_map_t map,pmap_t pmap,vm_offset_t min,vm_offset_t max)88892351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
889df8bae1dSRodney W. Grimes {
89021c641b2SJohn Baldwin
8912203c46dSMark Johnston map->header.eflags = MAP_ENTRY_HEADER;
8929688f931SAlan Cox map->needs_wakeup = FALSE;
8933075778bSJohn Dyson map->system_map = 0;
89492351f16SAlan Cox map->pmap = pmap;
895f0165b1cSKonstantin Belousov map->header.end = min;
896f0165b1cSKonstantin Belousov map->header.start = max;
897af7cd0c5SBrian Feldman map->flags = 0;
898c1ad5342SDoug Moore map->header.left = map->header.right = &map->header;
8994e94f402SAlan Cox map->root = NULL;
900df8bae1dSRodney W. Grimes map->timestamp = 0;
901a5db445dSMax Laier map->busy = 0;
902fa50a355SKonstantin Belousov map->anon_loc = 0;
903461587dcSDoug Moore #ifdef DIAGNOSTIC
904461587dcSDoug Moore map->nupdates = 0;
905461587dcSDoug Moore #endif
906df8bae1dSRodney W. Grimes }
907df8bae1dSRodney W. Grimes
908a18b1f1dSJason Evans void
vm_map_init(vm_map_t map,pmap_t pmap,vm_offset_t min,vm_offset_t max)90992351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
910a18b1f1dSJason Evans {
91192351f16SAlan Cox
91292351f16SAlan Cox _vm_map_init(map, pmap, min, max);
9137dd979dfSMark Johnston mtx_init(&map->system_mtx, "vm map (system)", NULL,
9147dd979dfSMark Johnston MTX_DEF | MTX_DUPOK);
9157dd979dfSMark Johnston sx_init(&map->lock, "vm map (user)");
916a18b1f1dSJason Evans }
917a18b1f1dSJason Evans
918df8bae1dSRodney W. Grimes /*
919b18bfc3dSJohn Dyson * vm_map_entry_dispose: [ internal use only ]
920b18bfc3dSJohn Dyson *
921b18bfc3dSJohn Dyson * Inverse of vm_map_entry_create.
922b18bfc3dSJohn Dyson */
92362487bb4SJohn Dyson static void
vm_map_entry_dispose(vm_map_t map,vm_map_entry_t entry)9241b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
925b18bfc3dSJohn Dyson {
9262b4a2c27SAlan Cox uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
927b18bfc3dSJohn Dyson }
928b18bfc3dSJohn Dyson
929b18bfc3dSJohn Dyson /*
930df8bae1dSRodney W. Grimes * vm_map_entry_create: [ internal use only ]
931df8bae1dSRodney W. Grimes *
932df8bae1dSRodney W. Grimes * Allocates a VM map entry for insertion.
933b28cb1caSAlfred Perlstein * No entry fields are filled in.
934df8bae1dSRodney W. Grimes */
935f708ef1bSPoul-Henning Kamp static vm_map_entry_t
vm_map_entry_create(vm_map_t map)9361b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
937df8bae1dSRodney W. Grimes {
9381f6889a1SMatthew Dillon vm_map_entry_t new_entry;
9391f6889a1SMatthew Dillon
940da76d349SBojan Novković #ifndef UMA_USE_DMAP
94120f02659SMark Johnston if (map == kernel_map) {
94220f02659SMark Johnston VM_MAP_ASSERT_LOCKED(map);
94320f02659SMark Johnston
94420f02659SMark Johnston /*
94520f02659SMark Johnston * A new slab of kernel map entries cannot be allocated at this
94620f02659SMark Johnston * point because the kernel map has not yet been updated to
94720f02659SMark Johnston * reflect the caller's request. Therefore, we allocate a new
94820f02659SMark Johnston * map entry, dipping into the reserve if necessary, and set a
94920f02659SMark Johnston * flag indicating that the reserve must be replenished before
95020f02659SMark Johnston * the map is unlocked.
95120f02659SMark Johnston */
95220f02659SMark Johnston new_entry = uma_zalloc(kmapentzone, M_NOWAIT | M_NOVM);
95320f02659SMark Johnston if (new_entry == NULL) {
95420f02659SMark Johnston new_entry = uma_zalloc(kmapentzone,
95520f02659SMark Johnston M_NOWAIT | M_NOVM | M_USE_RESERVE);
95620f02659SMark Johnston kernel_map->flags |= MAP_REPLENISH;
95720f02659SMark Johnston }
95820f02659SMark Johnston } else
95920f02659SMark Johnston #endif
96020f02659SMark Johnston if (map->system_map) {
9612b4a2c27SAlan Cox new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
96220f02659SMark Johnston } else {
963a163d034SWarner Losh new_entry = uma_zalloc(mapentzone, M_WAITOK);
96420f02659SMark Johnston }
96520f02659SMark Johnston KASSERT(new_entry != NULL,
96620f02659SMark Johnston ("vm_map_entry_create: kernel resources exhausted"));
9671f6889a1SMatthew Dillon return (new_entry);
968df8bae1dSRodney W. Grimes }
969df8bae1dSRodney W. Grimes
970df8bae1dSRodney W. Grimes /*
971794316a8SAlan Cox * vm_map_entry_set_behavior:
972794316a8SAlan Cox *
973794316a8SAlan Cox * Set the expected access behavior, either normal, random, or
974794316a8SAlan Cox * sequential.
975794316a8SAlan Cox */
97662a59e8fSWarner Losh static inline void
vm_map_entry_set_behavior(vm_map_entry_t entry,u_char behavior)977794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
978794316a8SAlan Cox {
979794316a8SAlan Cox entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
980794316a8SAlan Cox (behavior & MAP_ENTRY_BEHAV_MASK);
981794316a8SAlan Cox }
982794316a8SAlan Cox
983794316a8SAlan Cox /*
9845a0879daSDoug Moore * vm_map_entry_max_free_{left,right}:
9850164e057SAlan Cox *
9865a0879daSDoug Moore * Compute the size of the largest free gap between two entries,
9875a0879daSDoug Moore * one the root of a tree and the other the ancestor of that root
9885a0879daSDoug Moore * that is the least or greatest ancestor found on the search path.
9890164e057SAlan Cox */
9905a0879daSDoug Moore static inline vm_size_t
vm_map_entry_max_free_left(vm_map_entry_t root,vm_map_entry_t left_ancestor)9915a0879daSDoug Moore vm_map_entry_max_free_left(vm_map_entry_t root, vm_map_entry_t left_ancestor)
9920164e057SAlan Cox {
9930164e057SAlan Cox
994c1ad5342SDoug Moore return (root->left != left_ancestor ?
9955a0879daSDoug Moore root->left->max_free : root->start - left_ancestor->end);
9965a0879daSDoug Moore }
9975a0879daSDoug Moore
9985a0879daSDoug Moore static inline vm_size_t
vm_map_entry_max_free_right(vm_map_entry_t root,vm_map_entry_t right_ancestor)9995a0879daSDoug Moore vm_map_entry_max_free_right(vm_map_entry_t root, vm_map_entry_t right_ancestor)
10005a0879daSDoug Moore {
10015a0879daSDoug Moore
1002c1ad5342SDoug Moore return (root->right != right_ancestor ?
10035a0879daSDoug Moore root->right->max_free : right_ancestor->start - root->end);
10040164e057SAlan Cox }
10050164e057SAlan Cox
100683704cc2SDoug Moore /*
100783704cc2SDoug Moore * vm_map_entry_{pred,succ}:
100883704cc2SDoug Moore *
100983704cc2SDoug Moore * Find the {predecessor, successor} of the entry by taking one step
101083704cc2SDoug Moore * in the appropriate direction and backtracking as much as necessary.
1011c1ad5342SDoug Moore * vm_map_entry_succ is defined in vm_map.h.
101283704cc2SDoug Moore */
101383704cc2SDoug Moore static inline vm_map_entry_t
vm_map_entry_pred(vm_map_entry_t entry)101483704cc2SDoug Moore vm_map_entry_pred(vm_map_entry_t entry)
101583704cc2SDoug Moore {
1016c1ad5342SDoug Moore vm_map_entry_t prior;
101783704cc2SDoug Moore
1018c1ad5342SDoug Moore prior = entry->left;
1019c1ad5342SDoug Moore if (prior->right->start < entry->start) {
1020c1ad5342SDoug Moore do
1021c1ad5342SDoug Moore prior = prior->right;
1022c1ad5342SDoug Moore while (prior->right != entry);
102383704cc2SDoug Moore }
1024c1ad5342SDoug Moore return (prior);
1025c1ad5342SDoug Moore }
102683704cc2SDoug Moore
102785b7bedbSDoug Moore static inline vm_size_t
vm_size_max(vm_size_t a,vm_size_t b)102885b7bedbSDoug Moore vm_size_max(vm_size_t a, vm_size_t b)
102985b7bedbSDoug Moore {
103085b7bedbSDoug Moore
103185b7bedbSDoug Moore return (a > b ? a : b);
103285b7bedbSDoug Moore }
103385b7bedbSDoug Moore
1034c1ad5342SDoug Moore #define SPLAY_LEFT_STEP(root, y, llist, rlist, test) do { \
1035c1ad5342SDoug Moore vm_map_entry_t z; \
10365a0879daSDoug Moore vm_size_t max_free; \
10375a0879daSDoug Moore \
10385a0879daSDoug Moore /* \
10395a0879daSDoug Moore * Infer root->right->max_free == root->max_free when \
10405a0879daSDoug Moore * y->max_free < root->max_free || root->max_free == 0. \
10415a0879daSDoug Moore * Otherwise, look right to find it. \
10425a0879daSDoug Moore */ \
10439f701172SKonstantin Belousov y = root->left; \
10445a0879daSDoug Moore max_free = root->max_free; \
1045668a8aa8SDoug Moore KASSERT(max_free == vm_size_max( \
1046668a8aa8SDoug Moore vm_map_entry_max_free_left(root, llist), \
1047668a8aa8SDoug Moore vm_map_entry_max_free_right(root, rlist)), \
10485a0879daSDoug Moore ("%s: max_free invariant fails", __func__)); \
1049668a8aa8SDoug Moore if (max_free - 1 < vm_map_entry_max_free_left(root, llist)) \
10505a0879daSDoug Moore max_free = vm_map_entry_max_free_right(root, rlist); \
1051c1ad5342SDoug Moore if (y != llist && (test)) { \
10529f701172SKonstantin Belousov /* Rotate right and make y root. */ \
1053c1ad5342SDoug Moore z = y->right; \
1054c1ad5342SDoug Moore if (z != root) { \
1055c1ad5342SDoug Moore root->left = z; \
10569f701172SKonstantin Belousov y->right = root; \
10575a0879daSDoug Moore if (max_free < y->max_free) \
105885b7bedbSDoug Moore root->max_free = max_free = \
1059c1ad5342SDoug Moore vm_size_max(max_free, z->max_free); \
1060c1ad5342SDoug Moore } else if (max_free < y->max_free) \
1061c1ad5342SDoug Moore root->max_free = max_free = \
1062c1ad5342SDoug Moore vm_size_max(max_free, root->start - y->end);\
10639f701172SKonstantin Belousov root = y; \
10649f701172SKonstantin Belousov y = root->left; \
10659f701172SKonstantin Belousov } \
10665a0879daSDoug Moore /* Copy right->max_free. Put root on rlist. */ \
10675a0879daSDoug Moore root->max_free = max_free; \
10685a0879daSDoug Moore KASSERT(max_free == vm_map_entry_max_free_right(root, rlist), \
10695a0879daSDoug Moore ("%s: max_free not copied from right", __func__)); \
10709f701172SKonstantin Belousov root->left = rlist; \
10719f701172SKonstantin Belousov rlist = root; \
1072c1ad5342SDoug Moore root = y != llist ? y : NULL; \
10739f701172SKonstantin Belousov } while (0)
10749f701172SKonstantin Belousov
1075c1ad5342SDoug Moore #define SPLAY_RIGHT_STEP(root, y, llist, rlist, test) do { \
1076c1ad5342SDoug Moore vm_map_entry_t z; \
10775a0879daSDoug Moore vm_size_t max_free; \
10785a0879daSDoug Moore \
10795a0879daSDoug Moore /* \
10805a0879daSDoug Moore * Infer root->left->max_free == root->max_free when \
10815a0879daSDoug Moore * y->max_free < root->max_free || root->max_free == 0. \
10825a0879daSDoug Moore * Otherwise, look left to find it. \
10835a0879daSDoug Moore */ \
10849f701172SKonstantin Belousov y = root->right; \
10855a0879daSDoug Moore max_free = root->max_free; \
1086668a8aa8SDoug Moore KASSERT(max_free == vm_size_max( \
1087668a8aa8SDoug Moore vm_map_entry_max_free_left(root, llist), \
1088668a8aa8SDoug Moore vm_map_entry_max_free_right(root, rlist)), \
10895a0879daSDoug Moore ("%s: max_free invariant fails", __func__)); \
1090668a8aa8SDoug Moore if (max_free - 1 < vm_map_entry_max_free_right(root, rlist)) \
10915a0879daSDoug Moore max_free = vm_map_entry_max_free_left(root, llist); \
1092c1ad5342SDoug Moore if (y != rlist && (test)) { \
10939f701172SKonstantin Belousov /* Rotate left and make y root. */ \
1094c1ad5342SDoug Moore z = y->left; \
1095c1ad5342SDoug Moore if (z != root) { \
1096c1ad5342SDoug Moore root->right = z; \
10979f701172SKonstantin Belousov y->left = root; \
10985a0879daSDoug Moore if (max_free < y->max_free) \
109985b7bedbSDoug Moore root->max_free = max_free = \
1100c1ad5342SDoug Moore vm_size_max(max_free, z->max_free); \
1101c1ad5342SDoug Moore } else if (max_free < y->max_free) \
1102c1ad5342SDoug Moore root->max_free = max_free = \
1103c1ad5342SDoug Moore vm_size_max(max_free, y->start - root->end);\
11049f701172SKonstantin Belousov root = y; \
11059f701172SKonstantin Belousov y = root->right; \
11069f701172SKonstantin Belousov } \
11075a0879daSDoug Moore /* Copy left->max_free. Put root on llist. */ \
11085a0879daSDoug Moore root->max_free = max_free; \
11095a0879daSDoug Moore KASSERT(max_free == vm_map_entry_max_free_left(root, llist), \
11105a0879daSDoug Moore ("%s: max_free not copied from left", __func__)); \
11119f701172SKonstantin Belousov root->right = llist; \
11129f701172SKonstantin Belousov llist = root; \
1113c1ad5342SDoug Moore root = y != rlist ? y : NULL; \
11149f701172SKonstantin Belousov } while (0)
11159f701172SKonstantin Belousov
11160164e057SAlan Cox /*
1117c1ad5342SDoug Moore * Walk down the tree until we find addr or a gap where addr would go, breaking
1118c1ad5342SDoug Moore * off left and right subtrees of nodes less than, or greater than addr. Treat
1119c1ad5342SDoug Moore * subtrees with root->max_free < length as empty trees. llist and rlist are
1120c1ad5342SDoug Moore * the two sides in reverse order (bottom-up), with llist linked by the right
1121c1ad5342SDoug Moore * pointer and rlist linked by the left pointer in the vm_map_entry, and both
1122c1ad5342SDoug Moore * lists terminated by &map->header. This function, and the subsequent call to
1123c1ad5342SDoug Moore * vm_map_splay_merge_{left,right,pred,succ}, rely on the start and end address
11245a0879daSDoug Moore * values in &map->header.
11254e94f402SAlan Cox */
11261867d2f2SDoug Moore static __always_inline vm_map_entry_t
vm_map_splay_split(vm_map_t map,vm_offset_t addr,vm_size_t length,vm_map_entry_t * llist,vm_map_entry_t * rlist)11275a0879daSDoug Moore vm_map_splay_split(vm_map_t map, vm_offset_t addr, vm_size_t length,
11281867d2f2SDoug Moore vm_map_entry_t *llist, vm_map_entry_t *rlist)
11294e94f402SAlan Cox {
1130c1ad5342SDoug Moore vm_map_entry_t left, right, root, y;
11314e94f402SAlan Cox
1132c1ad5342SDoug Moore left = right = &map->header;
11335a0879daSDoug Moore root = map->root;
11349f701172SKonstantin Belousov while (root != NULL && root->max_free >= length) {
1135c1ad5342SDoug Moore KASSERT(left->end <= root->start &&
1136c1ad5342SDoug Moore root->end <= right->start,
11375a0879daSDoug Moore ("%s: root not within tree bounds", __func__));
11380164e057SAlan Cox if (addr < root->start) {
1139c1ad5342SDoug Moore SPLAY_LEFT_STEP(root, y, left, right,
11409f701172SKonstantin Belousov y->max_free >= length && addr < y->start);
11417438d60bSAlan Cox } else if (addr >= root->end) {
1142c1ad5342SDoug Moore SPLAY_RIGHT_STEP(root, y, left, right,
11439f701172SKonstantin Belousov y->max_free >= length && addr >= y->end);
11447438d60bSAlan Cox } else
11457438d60bSAlan Cox break;
11460164e057SAlan Cox }
1147c1ad5342SDoug Moore *llist = left;
1148c1ad5342SDoug Moore *rlist = right;
11499f701172SKonstantin Belousov return (root);
11509f701172SKonstantin Belousov }
11519f701172SKonstantin Belousov
11521867d2f2SDoug Moore static __always_inline void
vm_map_splay_findnext(vm_map_entry_t root,vm_map_entry_t * rlist)11531867d2f2SDoug Moore vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *rlist)
11549f701172SKonstantin Belousov {
1155c1ad5342SDoug Moore vm_map_entry_t hi, right, y;
11569f701172SKonstantin Belousov
1157c1ad5342SDoug Moore right = *rlist;
1158c1ad5342SDoug Moore hi = root->right == right ? NULL : root->right;
1159c1ad5342SDoug Moore if (hi == NULL)
1160c1ad5342SDoug Moore return;
1161c1ad5342SDoug Moore do
1162c1ad5342SDoug Moore SPLAY_LEFT_STEP(hi, y, root, right, true);
1163c1ad5342SDoug Moore while (hi != NULL);
1164c1ad5342SDoug Moore *rlist = right;
11659f701172SKonstantin Belousov }
11669f701172SKonstantin Belousov
11671867d2f2SDoug Moore static __always_inline void
vm_map_splay_findprev(vm_map_entry_t root,vm_map_entry_t * llist)11681867d2f2SDoug Moore vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *llist)
11699f701172SKonstantin Belousov {
1170c1ad5342SDoug Moore vm_map_entry_t left, lo, y;
11719f701172SKonstantin Belousov
1172c1ad5342SDoug Moore left = *llist;
1173c1ad5342SDoug Moore lo = root->left == left ? NULL : root->left;
1174c1ad5342SDoug Moore if (lo == NULL)
1175c1ad5342SDoug Moore return;
1176c1ad5342SDoug Moore do
1177c1ad5342SDoug Moore SPLAY_RIGHT_STEP(lo, y, left, root, true);
1178c1ad5342SDoug Moore while (lo != NULL);
1179c1ad5342SDoug Moore *llist = left;
11809f701172SKonstantin Belousov }
11810164e057SAlan Cox
11825a0879daSDoug Moore static inline void
vm_map_entry_swap(vm_map_entry_t * a,vm_map_entry_t * b)11835a0879daSDoug Moore vm_map_entry_swap(vm_map_entry_t *a, vm_map_entry_t *b)
11845a0879daSDoug Moore {
11855a0879daSDoug Moore vm_map_entry_t tmp;
11865a0879daSDoug Moore
11875a0879daSDoug Moore tmp = *b;
11885a0879daSDoug Moore *b = *a;
11895a0879daSDoug Moore *a = tmp;
11905a0879daSDoug Moore }
11915a0879daSDoug Moore
11920164e057SAlan Cox /*
11939f701172SKonstantin Belousov * Walk back up the two spines, flip the pointers and set max_free. The
11949f701172SKonstantin Belousov * subtrees of the root go at the bottom of llist and rlist.
11950164e057SAlan Cox */
119685b7bedbSDoug Moore static vm_size_t
vm_map_splay_merge_left_walk(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t tail,vm_size_t max_free,vm_map_entry_t llist)119785b7bedbSDoug Moore vm_map_splay_merge_left_walk(vm_map_entry_t header, vm_map_entry_t root,
119885b7bedbSDoug Moore vm_map_entry_t tail, vm_size_t max_free, vm_map_entry_t llist)
11999f701172SKonstantin Belousov {
12005a0879daSDoug Moore do {
12010164e057SAlan Cox /*
12025a0879daSDoug Moore * The max_free values of the children of llist are in
120385b7bedbSDoug Moore * llist->max_free and max_free. Update with the
12045a0879daSDoug Moore * max value.
12050164e057SAlan Cox */
120685b7bedbSDoug Moore llist->max_free = max_free =
120785b7bedbSDoug Moore vm_size_max(llist->max_free, max_free);
120885b7bedbSDoug Moore vm_map_entry_swap(&llist->right, &tail);
120985b7bedbSDoug Moore vm_map_entry_swap(&tail, &llist);
121085b7bedbSDoug Moore } while (llist != header);
121185b7bedbSDoug Moore root->left = tail;
121285b7bedbSDoug Moore return (max_free);
12135a0879daSDoug Moore }
121485b7bedbSDoug Moore
121585b7bedbSDoug Moore /*
121685b7bedbSDoug Moore * When llist is known to be the predecessor of root.
121785b7bedbSDoug Moore */
121885b7bedbSDoug Moore static inline vm_size_t
vm_map_splay_merge_pred(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t llist)121985b7bedbSDoug Moore vm_map_splay_merge_pred(vm_map_entry_t header, vm_map_entry_t root,
122085b7bedbSDoug Moore vm_map_entry_t llist)
122185b7bedbSDoug Moore {
122285b7bedbSDoug Moore vm_size_t max_free;
122385b7bedbSDoug Moore
122485b7bedbSDoug Moore max_free = root->start - llist->end;
122585b7bedbSDoug Moore if (llist != header) {
122685b7bedbSDoug Moore max_free = vm_map_splay_merge_left_walk(header, root,
1227c1ad5342SDoug Moore root, max_free, llist);
122885b7bedbSDoug Moore } else {
1229c1ad5342SDoug Moore root->left = header;
1230c1ad5342SDoug Moore header->right = root;
123185b7bedbSDoug Moore }
123285b7bedbSDoug Moore return (max_free);
123385b7bedbSDoug Moore }
123485b7bedbSDoug Moore
123585b7bedbSDoug Moore /*
123685b7bedbSDoug Moore * When llist may or may not be the predecessor of root.
123785b7bedbSDoug Moore */
123885b7bedbSDoug Moore static inline vm_size_t
vm_map_splay_merge_left(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t llist)123985b7bedbSDoug Moore vm_map_splay_merge_left(vm_map_entry_t header, vm_map_entry_t root,
124085b7bedbSDoug Moore vm_map_entry_t llist)
124185b7bedbSDoug Moore {
124285b7bedbSDoug Moore vm_size_t max_free;
124385b7bedbSDoug Moore
124485b7bedbSDoug Moore max_free = vm_map_entry_max_free_left(root, llist);
124585b7bedbSDoug Moore if (llist != header) {
124685b7bedbSDoug Moore max_free = vm_map_splay_merge_left_walk(header, root,
1247c1ad5342SDoug Moore root->left == llist ? root : root->left,
1248c1ad5342SDoug Moore max_free, llist);
124985b7bedbSDoug Moore }
125085b7bedbSDoug Moore return (max_free);
125185b7bedbSDoug Moore }
125285b7bedbSDoug Moore
125385b7bedbSDoug Moore static vm_size_t
vm_map_splay_merge_right_walk(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t tail,vm_size_t max_free,vm_map_entry_t rlist)125485b7bedbSDoug Moore vm_map_splay_merge_right_walk(vm_map_entry_t header, vm_map_entry_t root,
125585b7bedbSDoug Moore vm_map_entry_t tail, vm_size_t max_free, vm_map_entry_t rlist)
125685b7bedbSDoug Moore {
12575a0879daSDoug Moore do {
12585a0879daSDoug Moore /*
12595a0879daSDoug Moore * The max_free values of the children of rlist are in
126085b7bedbSDoug Moore * rlist->max_free and max_free. Update with the
12615a0879daSDoug Moore * max value.
12625a0879daSDoug Moore */
126385b7bedbSDoug Moore rlist->max_free = max_free =
126485b7bedbSDoug Moore vm_size_max(rlist->max_free, max_free);
126585b7bedbSDoug Moore vm_map_entry_swap(&rlist->left, &tail);
126685b7bedbSDoug Moore vm_map_entry_swap(&tail, &rlist);
126785b7bedbSDoug Moore } while (rlist != header);
126885b7bedbSDoug Moore root->right = tail;
126985b7bedbSDoug Moore return (max_free);
12705a0879daSDoug Moore }
127185b7bedbSDoug Moore
127285b7bedbSDoug Moore /*
127385b7bedbSDoug Moore * When rlist is known to be the succecessor of root.
127485b7bedbSDoug Moore */
127585b7bedbSDoug Moore static inline vm_size_t
vm_map_splay_merge_succ(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t rlist)127685b7bedbSDoug Moore vm_map_splay_merge_succ(vm_map_entry_t header, vm_map_entry_t root,
127785b7bedbSDoug Moore vm_map_entry_t rlist)
127885b7bedbSDoug Moore {
127985b7bedbSDoug Moore vm_size_t max_free;
128085b7bedbSDoug Moore
128185b7bedbSDoug Moore max_free = rlist->start - root->end;
128285b7bedbSDoug Moore if (rlist != header) {
128385b7bedbSDoug Moore max_free = vm_map_splay_merge_right_walk(header, root,
1284c1ad5342SDoug Moore root, max_free, rlist);
128585b7bedbSDoug Moore } else {
1286c1ad5342SDoug Moore root->right = header;
1287c1ad5342SDoug Moore header->left = root;
128885b7bedbSDoug Moore }
128985b7bedbSDoug Moore return (max_free);
129085b7bedbSDoug Moore }
129185b7bedbSDoug Moore
129285b7bedbSDoug Moore /*
129385b7bedbSDoug Moore * When rlist may or may not be the succecessor of root.
129485b7bedbSDoug Moore */
129585b7bedbSDoug Moore static inline vm_size_t
vm_map_splay_merge_right(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t rlist)129685b7bedbSDoug Moore vm_map_splay_merge_right(vm_map_entry_t header, vm_map_entry_t root,
129785b7bedbSDoug Moore vm_map_entry_t rlist)
129885b7bedbSDoug Moore {
129985b7bedbSDoug Moore vm_size_t max_free;
130085b7bedbSDoug Moore
130185b7bedbSDoug Moore max_free = vm_map_entry_max_free_right(root, rlist);
130285b7bedbSDoug Moore if (rlist != header) {
130385b7bedbSDoug Moore max_free = vm_map_splay_merge_right_walk(header, root,
1304c1ad5342SDoug Moore root->right == rlist ? root : root->right,
1305c1ad5342SDoug Moore max_free, rlist);
130685b7bedbSDoug Moore }
130785b7bedbSDoug Moore return (max_free);
13084e94f402SAlan Cox }
13094e94f402SAlan Cox
13104e94f402SAlan Cox /*
1311d1d3f7e1SDoug Moore * vm_map_splay:
1312d1d3f7e1SDoug Moore *
1313d1d3f7e1SDoug Moore * The Sleator and Tarjan top-down splay algorithm with the
1314d1d3f7e1SDoug Moore * following variation. Max_free must be computed bottom-up, so
1315d1d3f7e1SDoug Moore * on the downward pass, maintain the left and right spines in
1316d1d3f7e1SDoug Moore * reverse order. Then, make a second pass up each side to fix
1317d1d3f7e1SDoug Moore * the pointers and compute max_free. The time bound is O(log n)
1318d1d3f7e1SDoug Moore * amortized.
1319d1d3f7e1SDoug Moore *
1320c1ad5342SDoug Moore * The tree is threaded, which means that there are no null pointers.
1321c1ad5342SDoug Moore * When a node has no left child, its left pointer points to its
1322c1ad5342SDoug Moore * predecessor, which the last ancestor on the search path from the root
1323c1ad5342SDoug Moore * where the search branched right. Likewise, when a node has no right
1324c1ad5342SDoug Moore * child, its right pointer points to its successor. The map header node
1325c1ad5342SDoug Moore * is the predecessor of the first map entry, and the successor of the
1326c1ad5342SDoug Moore * last.
1327c1ad5342SDoug Moore *
1328d1d3f7e1SDoug Moore * The new root is the vm_map_entry containing "addr", or else an
1329d1d3f7e1SDoug Moore * adjacent entry (lower if possible) if addr is not in the tree.
1330d1d3f7e1SDoug Moore *
1331d1d3f7e1SDoug Moore * The map must be locked, and leaves it so.
1332d1d3f7e1SDoug Moore *
1333d1d3f7e1SDoug Moore * Returns: the new root.
1334d1d3f7e1SDoug Moore */
1335d1d3f7e1SDoug Moore static vm_map_entry_t
vm_map_splay(vm_map_t map,vm_offset_t addr)1336d1d3f7e1SDoug Moore vm_map_splay(vm_map_t map, vm_offset_t addr)
1337d1d3f7e1SDoug Moore {
133885b7bedbSDoug Moore vm_map_entry_t header, llist, rlist, root;
133985b7bedbSDoug Moore vm_size_t max_free_left, max_free_right;
1340d1d3f7e1SDoug Moore
134185b7bedbSDoug Moore header = &map->header;
1342d1d3f7e1SDoug Moore root = vm_map_splay_split(map, addr, 0, &llist, &rlist);
1343d1d3f7e1SDoug Moore if (root != NULL) {
134485b7bedbSDoug Moore max_free_left = vm_map_splay_merge_left(header, root, llist);
134585b7bedbSDoug Moore max_free_right = vm_map_splay_merge_right(header, root, rlist);
134685b7bedbSDoug Moore } else if (llist != header) {
1347d1d3f7e1SDoug Moore /*
1348d1d3f7e1SDoug Moore * Recover the greatest node in the left
1349d1d3f7e1SDoug Moore * subtree and make it the root.
1350d1d3f7e1SDoug Moore */
1351d1d3f7e1SDoug Moore root = llist;
1352d1d3f7e1SDoug Moore llist = root->right;
135385b7bedbSDoug Moore max_free_left = vm_map_splay_merge_left(header, root, llist);
135485b7bedbSDoug Moore max_free_right = vm_map_splay_merge_succ(header, root, rlist);
135585b7bedbSDoug Moore } else if (rlist != header) {
1356d1d3f7e1SDoug Moore /*
1357d1d3f7e1SDoug Moore * Recover the least node in the right
1358d1d3f7e1SDoug Moore * subtree and make it the root.
1359d1d3f7e1SDoug Moore */
1360d1d3f7e1SDoug Moore root = rlist;
1361d1d3f7e1SDoug Moore rlist = root->left;
136285b7bedbSDoug Moore max_free_left = vm_map_splay_merge_pred(header, root, llist);
136385b7bedbSDoug Moore max_free_right = vm_map_splay_merge_right(header, root, rlist);
1364d1d3f7e1SDoug Moore } else {
1365d1d3f7e1SDoug Moore /* There is no root. */
1366d1d3f7e1SDoug Moore return (NULL);
1367d1d3f7e1SDoug Moore }
136885b7bedbSDoug Moore root->max_free = vm_size_max(max_free_left, max_free_right);
136985b7bedbSDoug Moore map->root = root;
1370d1d3f7e1SDoug Moore VM_MAP_ASSERT_CONSISTENT(map);
1371d1d3f7e1SDoug Moore return (root);
1372d1d3f7e1SDoug Moore }
1373d1d3f7e1SDoug Moore
1374d1d3f7e1SDoug Moore /*
1375df8bae1dSRodney W. Grimes * vm_map_entry_{un,}link:
1376df8bae1dSRodney W. Grimes *
1377668a8aa8SDoug Moore * Insert/remove entries from maps. On linking, if new entry clips
1378668a8aa8SDoug Moore * existing entry, trim existing entry to avoid overlap, and manage
1379668a8aa8SDoug Moore * offsets. On unlinking, merge disappearing entry with neighbor, if
1380668a8aa8SDoug Moore * called for, and manage offsets. Callers should not modify fields in
1381668a8aa8SDoug Moore * entries already mapped.
1382df8bae1dSRodney W. Grimes */
13834e94f402SAlan Cox static void
vm_map_entry_link(vm_map_t map,vm_map_entry_t entry)13845a0879daSDoug Moore vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
138599c81ca9SAlan Cox {
138685b7bedbSDoug Moore vm_map_entry_t header, llist, rlist, root;
1387668a8aa8SDoug Moore vm_size_t max_free_left, max_free_right;
138821c641b2SJohn Baldwin
13899f701172SKonstantin Belousov CTR3(KTR_VM,
13909f701172SKonstantin Belousov "vm_map_entry_link: map %p, nentries %d, entry %p", map,
13919f701172SKonstantin Belousov map->nentries, entry);
13923a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map);
139399c81ca9SAlan Cox map->nentries++;
139485b7bedbSDoug Moore header = &map->header;
13955a0879daSDoug Moore root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1396668a8aa8SDoug Moore if (root == NULL) {
1397668a8aa8SDoug Moore /*
1398668a8aa8SDoug Moore * The new entry does not overlap any existing entry in the
1399668a8aa8SDoug Moore * map, so it becomes the new root of the map tree.
1400668a8aa8SDoug Moore */
1401668a8aa8SDoug Moore max_free_left = vm_map_splay_merge_pred(header, entry, llist);
1402668a8aa8SDoug Moore max_free_right = vm_map_splay_merge_succ(header, entry, rlist);
1403668a8aa8SDoug Moore } else if (entry->start == root->start) {
1404668a8aa8SDoug Moore /*
1405668a8aa8SDoug Moore * The new entry is a clone of root, with only the end field
1406668a8aa8SDoug Moore * changed. The root entry will be shrunk to abut the new
1407668a8aa8SDoug Moore * entry, and will be the right child of the new root entry in
1408668a8aa8SDoug Moore * the modified map.
1409668a8aa8SDoug Moore */
1410668a8aa8SDoug Moore KASSERT(entry->end < root->end,
1411668a8aa8SDoug Moore ("%s: clip_start not within entry", __func__));
1412668a8aa8SDoug Moore vm_map_splay_findprev(root, &llist);
1413c7180098SKonstantin Belousov if ((root->eflags & (MAP_ENTRY_STACK_GAP_DN |
1414c7180098SKonstantin Belousov MAP_ENTRY_STACK_GAP_UP)) == 0)
1415668a8aa8SDoug Moore root->offset += entry->end - root->start;
1416668a8aa8SDoug Moore root->start = entry->end;
1417668a8aa8SDoug Moore max_free_left = vm_map_splay_merge_pred(header, entry, llist);
1418668a8aa8SDoug Moore max_free_right = root->max_free = vm_size_max(
1419668a8aa8SDoug Moore vm_map_splay_merge_pred(entry, root, entry),
1420668a8aa8SDoug Moore vm_map_splay_merge_right(header, root, rlist));
1421668a8aa8SDoug Moore } else {
1422668a8aa8SDoug Moore /*
1423668a8aa8SDoug Moore * The new entry is a clone of root, with only the start field
1424668a8aa8SDoug Moore * changed. The root entry will be shrunk to abut the new
1425668a8aa8SDoug Moore * entry, and will be the left child of the new root entry in
1426668a8aa8SDoug Moore * the modified map.
1427668a8aa8SDoug Moore */
1428668a8aa8SDoug Moore KASSERT(entry->end == root->end,
1429668a8aa8SDoug Moore ("%s: clip_start not within entry", __func__));
1430668a8aa8SDoug Moore vm_map_splay_findnext(root, &rlist);
1431c7180098SKonstantin Belousov if ((entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
1432c7180098SKonstantin Belousov MAP_ENTRY_STACK_GAP_UP)) == 0)
1433668a8aa8SDoug Moore entry->offset += entry->start - root->start;
1434668a8aa8SDoug Moore root->end = entry->start;
1435668a8aa8SDoug Moore max_free_left = root->max_free = vm_size_max(
1436668a8aa8SDoug Moore vm_map_splay_merge_left(header, root, llist),
1437668a8aa8SDoug Moore vm_map_splay_merge_succ(entry, root, entry));
1438668a8aa8SDoug Moore max_free_right = vm_map_splay_merge_succ(header, entry, rlist);
1439668a8aa8SDoug Moore }
1440668a8aa8SDoug Moore entry->max_free = vm_size_max(max_free_left, max_free_right);
1441668a8aa8SDoug Moore map->root = entry;
14429f701172SKonstantin Belousov VM_MAP_ASSERT_CONSISTENT(map);
1443df8bae1dSRodney W. Grimes }
144499c81ca9SAlan Cox
14459f701172SKonstantin Belousov enum unlink_merge_type {
14469f701172SKonstantin Belousov UNLINK_MERGE_NONE,
14479f701172SKonstantin Belousov UNLINK_MERGE_NEXT
14489f701172SKonstantin Belousov };
14499f701172SKonstantin Belousov
14504e94f402SAlan Cox static void
vm_map_entry_unlink(vm_map_t map,vm_map_entry_t entry,enum unlink_merge_type op)14515a0879daSDoug Moore vm_map_entry_unlink(vm_map_t map, vm_map_entry_t entry,
14529f701172SKonstantin Belousov enum unlink_merge_type op)
145399c81ca9SAlan Cox {
1454c1ad5342SDoug Moore vm_map_entry_t header, llist, rlist, root;
145585b7bedbSDoug Moore vm_size_t max_free_left, max_free_right;
145699c81ca9SAlan Cox
14573a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map);
145885b7bedbSDoug Moore header = &map->header;
14595a0879daSDoug Moore root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
14609f701172SKonstantin Belousov KASSERT(root != NULL,
14619f701172SKonstantin Belousov ("vm_map_entry_unlink: unlink object not mapped"));
14624e94f402SAlan Cox
14631867d2f2SDoug Moore vm_map_splay_findprev(root, &llist);
14649f701172SKonstantin Belousov vm_map_splay_findnext(root, &rlist);
14651867d2f2SDoug Moore if (op == UNLINK_MERGE_NEXT) {
14669f701172SKonstantin Belousov rlist->start = root->start;
1467c7180098SKonstantin Belousov MPASS((rlist->eflags & (MAP_ENTRY_STACK_GAP_DN |
14685f452214SKonstantin Belousov MAP_ENTRY_STACK_GAP_UP)) == 0);
14699f701172SKonstantin Belousov rlist->offset = root->offset;
14701867d2f2SDoug Moore }
147185b7bedbSDoug Moore if (llist != header) {
14729f701172SKonstantin Belousov root = llist;
14739f701172SKonstantin Belousov llist = root->right;
147485b7bedbSDoug Moore max_free_left = vm_map_splay_merge_left(header, root, llist);
147585b7bedbSDoug Moore max_free_right = vm_map_splay_merge_succ(header, root, rlist);
147685b7bedbSDoug Moore } else if (rlist != header) {
14779f701172SKonstantin Belousov root = rlist;
14789f701172SKonstantin Belousov rlist = root->left;
147985b7bedbSDoug Moore max_free_left = vm_map_splay_merge_pred(header, root, llist);
148085b7bedbSDoug Moore max_free_right = vm_map_splay_merge_right(header, root, rlist);
1481c1ad5342SDoug Moore } else {
1482c1ad5342SDoug Moore header->left = header->right = header;
14839f701172SKonstantin Belousov root = NULL;
1484c1ad5342SDoug Moore }
14859f701172SKonstantin Belousov if (root != NULL)
148685b7bedbSDoug Moore root->max_free = vm_size_max(max_free_left, max_free_right);
148785b7bedbSDoug Moore map->root = root;
14889f701172SKonstantin Belousov VM_MAP_ASSERT_CONSISTENT(map);
148999c81ca9SAlan Cox map->nentries--;
149021c641b2SJohn Baldwin CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
149121c641b2SJohn Baldwin map->nentries, entry);
1492df8bae1dSRodney W. Grimes }
1493df8bae1dSRodney W. Grimes
1494df8bae1dSRodney W. Grimes /*
1495fa581662SDoug Moore * vm_map_entry_resize:
14960164e057SAlan Cox *
1497fa581662SDoug Moore * Resize a vm_map_entry, recompute the amount of free space that
1498fa581662SDoug Moore * follows it and propagate that value up the tree.
14990164e057SAlan Cox *
15000164e057SAlan Cox * The map must be locked, and leaves it so.
15010164e057SAlan Cox */
15020164e057SAlan Cox static void
vm_map_entry_resize(vm_map_t map,vm_map_entry_t entry,vm_size_t grow_amount)1503fa581662SDoug Moore vm_map_entry_resize(vm_map_t map, vm_map_entry_t entry, vm_size_t grow_amount)
15040164e057SAlan Cox {
150585b7bedbSDoug Moore vm_map_entry_t header, llist, rlist, root;
15060164e057SAlan Cox
15079f701172SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map);
150885b7bedbSDoug Moore header = &map->header;
15095a0879daSDoug Moore root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
15101867d2f2SDoug Moore KASSERT(root != NULL, ("%s: resize object not mapped", __func__));
15119f701172SKonstantin Belousov vm_map_splay_findnext(root, &rlist);
15121895f520SDoug Moore entry->end += grow_amount;
151385b7bedbSDoug Moore root->max_free = vm_size_max(
151485b7bedbSDoug Moore vm_map_splay_merge_left(header, root, llist),
151585b7bedbSDoug Moore vm_map_splay_merge_succ(header, root, rlist));
151685b7bedbSDoug Moore map->root = root;
15179f701172SKonstantin Belousov VM_MAP_ASSERT_CONSISTENT(map);
1518fa581662SDoug Moore CTR4(KTR_VM, "%s: map %p, nentries %d, entry %p",
151973f11451SDoug Moore __func__, map, map->nentries, entry);
15200164e057SAlan Cox }
15210164e057SAlan Cox
15220164e057SAlan Cox /*
1523d1d3f7e1SDoug Moore * vm_map_lookup_entry: [ internal use only ]
1524df8bae1dSRodney W. Grimes *
1525d1d3f7e1SDoug Moore * Finds the map entry containing (or
1526d1d3f7e1SDoug Moore * immediately preceding) the specified address
1527d1d3f7e1SDoug Moore * in the given map; the entry is returned
1528d1d3f7e1SDoug Moore * in the "entry" parameter. The boolean
1529d1d3f7e1SDoug Moore * result indicates whether the address is
1530d1d3f7e1SDoug Moore * actually contained in the map.
1531df8bae1dSRodney W. Grimes */
1532d1d3f7e1SDoug Moore boolean_t
vm_map_lookup_entry(vm_map_t map,vm_offset_t address,vm_map_entry_t * entry)1533d1d3f7e1SDoug Moore vm_map_lookup_entry(
1534d1d3f7e1SDoug Moore vm_map_t map,
1535d1d3f7e1SDoug Moore vm_offset_t address,
1536d1d3f7e1SDoug Moore vm_map_entry_t *entry) /* OUT */
1537df8bae1dSRodney W. Grimes {
1538c1ad5342SDoug Moore vm_map_entry_t cur, header, lbound, ubound;
1539d1d3f7e1SDoug Moore boolean_t locked;
1540df8bae1dSRodney W. Grimes
15414c3ef59eSAlan Cox /*
15424c3ef59eSAlan Cox * If the map is empty, then the map entry immediately preceding
1543d1d3f7e1SDoug Moore * "address" is the map's header.
15444c3ef59eSAlan Cox */
154585b7bedbSDoug Moore header = &map->header;
1546d1d3f7e1SDoug Moore cur = map->root;
1547d1d3f7e1SDoug Moore if (cur == NULL) {
154885b7bedbSDoug Moore *entry = header;
1549d1d3f7e1SDoug Moore return (FALSE);
1550d1d3f7e1SDoug Moore }
1551d1d3f7e1SDoug Moore if (address >= cur->start && cur->end > address) {
1552d1d3f7e1SDoug Moore *entry = cur;
1553d1d3f7e1SDoug Moore return (TRUE);
15549f701172SKonstantin Belousov }
15559f701172SKonstantin Belousov if ((locked = vm_map_locked(map)) ||
155605a8c414SAlan Cox sx_try_upgrade(&map->lock)) {
155705a8c414SAlan Cox /*
155805a8c414SAlan Cox * Splay requires a write lock on the map. However, it only
155905a8c414SAlan Cox * restructures the binary search tree; it does not otherwise
156005a8c414SAlan Cox * change the map. Thus, the map's timestamp need not change
156105a8c414SAlan Cox * on a temporary upgrade.
156205a8c414SAlan Cox */
1563d1d3f7e1SDoug Moore cur = vm_map_splay(map, address);
1564461587dcSDoug Moore if (!locked) {
1565461587dcSDoug Moore VM_MAP_UNLOCK_CONSISTENT(map);
156605a8c414SAlan Cox sx_downgrade(&map->lock);
1567461587dcSDoug Moore }
1568d1d3f7e1SDoug Moore
1569d1d3f7e1SDoug Moore /*
1570d1d3f7e1SDoug Moore * If "address" is contained within a map entry, the new root
1571d1d3f7e1SDoug Moore * is that map entry. Otherwise, the new root is a map entry
1572d1d3f7e1SDoug Moore * immediately before or after "address".
1573d1d3f7e1SDoug Moore */
1574d1d3f7e1SDoug Moore if (address < cur->start) {
157585b7bedbSDoug Moore *entry = header;
1576d1d3f7e1SDoug Moore return (FALSE);
1577d1d3f7e1SDoug Moore }
1578d1d3f7e1SDoug Moore *entry = cur;
1579d1d3f7e1SDoug Moore return (address < cur->end);
15809f701172SKonstantin Belousov }
158105a8c414SAlan Cox /*
158205a8c414SAlan Cox * Since the map is only locked for read access, perform a
1583d1d3f7e1SDoug Moore * standard binary search tree lookup for "address".
158405a8c414SAlan Cox */
1585c1ad5342SDoug Moore lbound = ubound = header;
1586c1ad5342SDoug Moore for (;;) {
1587d1d3f7e1SDoug Moore if (address < cur->start) {
1588c1ad5342SDoug Moore ubound = cur;
1589d1d3f7e1SDoug Moore cur = cur->left;
1590c1ad5342SDoug Moore if (cur == lbound)
1591c1ad5342SDoug Moore break;
1592d1d3f7e1SDoug Moore } else if (cur->end <= address) {
1593d1d3f7e1SDoug Moore lbound = cur;
1594d1d3f7e1SDoug Moore cur = cur->right;
1595c1ad5342SDoug Moore if (cur == ubound)
1596c1ad5342SDoug Moore break;
15979f701172SKonstantin Belousov } else {
1598d1d3f7e1SDoug Moore *entry = cur;
1599d1d3f7e1SDoug Moore return (TRUE);
160005a8c414SAlan Cox }
1601c1ad5342SDoug Moore }
1602d1d3f7e1SDoug Moore *entry = lbound;
1603d1d3f7e1SDoug Moore return (FALSE);
1604df8bae1dSRodney W. Grimes }
1605df8bae1dSRodney W. Grimes
1606df8bae1dSRodney W. Grimes /*
1607ba41b0deSKonstantin Belousov * vm_map_insert1() is identical to vm_map_insert() except that it
1608ba41b0deSKonstantin Belousov * returns the newly inserted map entry in '*res'. In case the new
1609ba41b0deSKonstantin Belousov * entry is coalesced with a neighbor or an existing entry was
1610ba41b0deSKonstantin Belousov * resized, that entry is returned. In any case, the returned entry
1611ba41b0deSKonstantin Belousov * covers the specified address range.
161230dcfc09SJohn Dyson */
1613ba41b0deSKonstantin Belousov static int
vm_map_insert1(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t start,vm_offset_t end,vm_prot_t prot,vm_prot_t max,int cow,vm_map_entry_t * res)1614ba41b0deSKonstantin Belousov vm_map_insert1(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1615ba41b0deSKonstantin Belousov vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow,
1616ba41b0deSKonstantin Belousov vm_map_entry_t *res)
161730dcfc09SJohn Dyson {
161883704cc2SDoug Moore vm_map_entry_t new_entry, next_entry, prev_entry;
1619ef694c1aSEdward Tomasz Napierala struct ucred *cred;
16201569205fSKonstantin Belousov vm_eflags_t protoeflags;
16218211bd45SKonstantin Belousov vm_inherit_t inheritance;
1622e2e80fb3SKonstantin Belousov u_long bdry;
1623e2e80fb3SKonstantin Belousov u_int bidx;
162430dcfc09SJohn Dyson
16253a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map);
16262e47807cSJeff Roberson KASSERT(object != kernel_object ||
162733314db0SAlan Cox (cow & MAP_COPY_ON_WRITE) == 0,
16282e47807cSJeff Roberson ("vm_map_insert: kernel object and COW"));
1629e2e80fb3SKonstantin Belousov KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0 ||
1630e2e80fb3SKonstantin Belousov (cow & MAP_SPLIT_BOUNDARY_MASK) != 0,
1631e2e80fb3SKonstantin Belousov ("vm_map_insert: paradoxical MAP_NOFAULT request, obj %p cow %#x",
1632e2e80fb3SKonstantin Belousov object, cow));
163300de6773SKonstantin Belousov KASSERT((prot & ~max) == 0,
163400de6773SKonstantin Belousov ("prot %#x is not subset of max_prot %#x", prot, max));
16353a0916b8SKonstantin Belousov
163630dcfc09SJohn Dyson /*
163730dcfc09SJohn Dyson * Check that the start and end points are not bogus.
163830dcfc09SJohn Dyson */
1639f0340740SMark Johnston if (start == end || !vm_map_range_valid(map, start, end))
164030dcfc09SJohn Dyson return (KERN_INVALID_ADDRESS);
164130dcfc09SJohn Dyson
16422e1c94aaSKonstantin Belousov if ((map->flags & MAP_WXORX) != 0 && (prot & (VM_PROT_WRITE |
16432e1c94aaSKonstantin Belousov VM_PROT_EXECUTE)) == (VM_PROT_WRITE | VM_PROT_EXECUTE))
16442e1c94aaSKonstantin Belousov return (KERN_PROTECTION_FAILURE);
16452e1c94aaSKonstantin Belousov
164630dcfc09SJohn Dyson /*
164730dcfc09SJohn Dyson * Find the entry prior to the proposed starting address; if it's part
164830dcfc09SJohn Dyson * of an existing entry, this range is bogus.
164930dcfc09SJohn Dyson */
1650723413beSDoug Moore if (vm_map_lookup_entry(map, start, &prev_entry))
165130dcfc09SJohn Dyson return (KERN_NO_SPACE);
165230dcfc09SJohn Dyson
165330dcfc09SJohn Dyson /*
165430dcfc09SJohn Dyson * Assert that the next entry doesn't overlap the end point.
165530dcfc09SJohn Dyson */
165683704cc2SDoug Moore next_entry = vm_map_entry_succ(prev_entry);
165783704cc2SDoug Moore if (next_entry->start < end)
165830dcfc09SJohn Dyson return (KERN_NO_SPACE);
165930dcfc09SJohn Dyson
166019bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
166119bd0d9cSKonstantin Belousov max != VM_PROT_NONE))
166219bd0d9cSKonstantin Belousov return (KERN_INVALID_ARGUMENT);
166319bd0d9cSKonstantin Belousov
1664afa07f7eSJohn Dyson protoeflags = 0;
1665afa07f7eSJohn Dyson if (cow & MAP_COPY_ON_WRITE)
1666e5f13bddSAlan Cox protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
166733314db0SAlan Cox if (cow & MAP_NOFAULT)
1668afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NOFAULT;
16694f79d873SMatthew Dillon if (cow & MAP_DISABLE_SYNCER)
16704f79d873SMatthew Dillon protoeflags |= MAP_ENTRY_NOSYNC;
16719730a5daSPaul Saab if (cow & MAP_DISABLE_COREDUMP)
16729730a5daSPaul Saab protoeflags |= MAP_ENTRY_NOCOREDUMP;
1673712efe66SAlan Cox if (cow & MAP_STACK_GROWS_DOWN)
1674712efe66SAlan Cox protoeflags |= MAP_ENTRY_GROWS_DOWN;
1675712efe66SAlan Cox if (cow & MAP_STACK_GROWS_UP)
1676712efe66SAlan Cox protoeflags |= MAP_ENTRY_GROWS_UP;
1677fe7bcbafSKyle Evans if (cow & MAP_WRITECOUNT)
1678fe7bcbafSKyle Evans protoeflags |= MAP_ENTRY_WRITECNT;
167978022527SKonstantin Belousov if (cow & MAP_VN_EXEC)
168078022527SKonstantin Belousov protoeflags |= MAP_ENTRY_VN_EXEC;
168119bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_GUARD) != 0)
168219bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_GUARD;
168319bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
168419bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_STACK_GAP_DN;
168519bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
168619bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_STACK_GAP_UP;
16878211bd45SKonstantin Belousov if (cow & MAP_INHERIT_SHARE)
16888211bd45SKonstantin Belousov inheritance = VM_INHERIT_SHARE;
16898211bd45SKonstantin Belousov else
16908211bd45SKonstantin Belousov inheritance = VM_INHERIT_DEFAULT;
1691e2e80fb3SKonstantin Belousov if ((cow & MAP_SPLIT_BOUNDARY_MASK) != 0) {
1692e2e80fb3SKonstantin Belousov /* This magically ignores index 0, for usual page size. */
1693e2e80fb3SKonstantin Belousov bidx = (cow & MAP_SPLIT_BOUNDARY_MASK) >>
1694e2e80fb3SKonstantin Belousov MAP_SPLIT_BOUNDARY_SHIFT;
1695e2e80fb3SKonstantin Belousov if (bidx >= MAXPAGESIZES)
1696e2e80fb3SKonstantin Belousov return (KERN_INVALID_ARGUMENT);
1697e2e80fb3SKonstantin Belousov bdry = pagesizes[bidx] - 1;
1698e2e80fb3SKonstantin Belousov if ((start & bdry) != 0 || (end & bdry) != 0)
1699e2e80fb3SKonstantin Belousov return (KERN_INVALID_ARGUMENT);
1700e2e80fb3SKonstantin Belousov protoeflags |= bidx << MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
1701e2e80fb3SKonstantin Belousov }
17024f79d873SMatthew Dillon
1703ef694c1aSEdward Tomasz Napierala cred = NULL;
170419bd0d9cSKonstantin Belousov if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
17053364c323SKonstantin Belousov goto charged;
17063364c323SKonstantin Belousov if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
17073364c323SKonstantin Belousov ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
17083364c323SKonstantin Belousov if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
17093364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE);
17101569205fSKonstantin Belousov KASSERT(object == NULL ||
17111569205fSKonstantin Belousov (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1712ef694c1aSEdward Tomasz Napierala object->cred == NULL,
17131569205fSKonstantin Belousov ("overcommit: vm_map_insert o %p", object));
1714ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred;
17153364c323SKonstantin Belousov }
17163364c323SKonstantin Belousov
17173364c323SKonstantin Belousov charged:
1718f8616ebfSAlan Cox /* Expand the kernel pmap, if necessary. */
1719f8616ebfSAlan Cox if (map == kernel_map && end > kernel_vm_end)
1720f8616ebfSAlan Cox pmap_growkernel(end);
17211d284e00SAlan Cox if (object != NULL) {
172230dcfc09SJohn Dyson /*
17231d284e00SAlan Cox * OBJ_ONEMAPPING must be cleared unless this mapping
17241d284e00SAlan Cox * is trivially proven to be the only mapping for any
17251d284e00SAlan Cox * of the object's pages. (Object granularity
17261d284e00SAlan Cox * reference counting is insufficient to recognize
17271d284e00SAlan Cox * aliases with precision.)
172830dcfc09SJohn Dyson */
172963967687SJeff Roberson if ((object->flags & OBJ_ANON) != 0) {
173089f6b863SAttilio Rao VM_OBJECT_WLOCK(object);
17311d284e00SAlan Cox if (object->ref_count > 1 || object->shadow_count != 0)
17322aaeadf8SMatthew Dillon vm_object_clear_flag(object, OBJ_ONEMAPPING);
173389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object);
173463967687SJeff Roberson }
17352203c46dSMark Johnston } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
17362203c46dSMark Johnston protoeflags &&
173778022527SKonstantin Belousov (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP |
173878022527SKonstantin Belousov MAP_VN_EXEC)) == 0 &&
1739737e25f7SAlan Cox prev_entry->end == start && (prev_entry->cred == cred ||
17403364c323SKonstantin Belousov (prev_entry->object.vm_object != NULL &&
17411569205fSKonstantin Belousov prev_entry->object.vm_object->cred == cred)) &&
17428cc7e047SJohn Dyson vm_object_coalesce(prev_entry->object.vm_object,
174357a21abaSAlan Cox prev_entry->offset,
17448cc7e047SJohn Dyson (vm_size_t)(prev_entry->end - prev_entry->start),
174560169c88SAlan Cox (vm_size_t)(end - prev_entry->end), cred != NULL &&
174660169c88SAlan Cox (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
174730dcfc09SJohn Dyson /*
17482aaeadf8SMatthew Dillon * We were able to extend the object. Determine if we
17492aaeadf8SMatthew Dillon * can extend the previous map entry to include the
17502aaeadf8SMatthew Dillon * new range as well.
175130dcfc09SJohn Dyson */
17521569205fSKonstantin Belousov if (prev_entry->inheritance == inheritance &&
17531569205fSKonstantin Belousov prev_entry->protection == prot &&
1754737e25f7SAlan Cox prev_entry->max_protection == max &&
1755737e25f7SAlan Cox prev_entry->wired_count == 0) {
1756737e25f7SAlan Cox KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1757737e25f7SAlan Cox 0, ("prev_entry %p has incoherent wiring",
1758737e25f7SAlan Cox prev_entry));
175919bd0d9cSKonstantin Belousov if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
17601569205fSKonstantin Belousov map->size += end - prev_entry->end;
1761fa581662SDoug Moore vm_map_entry_resize(map, prev_entry,
17621895f520SDoug Moore end - prev_entry->end);
1763ba41b0deSKonstantin Belousov *res = vm_map_try_merge_entries(map, prev_entry,
1764ba41b0deSKonstantin Belousov next_entry);
176530dcfc09SJohn Dyson return (KERN_SUCCESS);
176630dcfc09SJohn Dyson }
17678cc7e047SJohn Dyson
17682aaeadf8SMatthew Dillon /*
17692aaeadf8SMatthew Dillon * If we can extend the object but cannot extend the
17702aaeadf8SMatthew Dillon * map entry, we have to create a new map entry. We
17712aaeadf8SMatthew Dillon * must bump the ref count on the extended object to
17724e71e795SMatthew Dillon * account for it. object may be NULL.
17732aaeadf8SMatthew Dillon */
17742aaeadf8SMatthew Dillon object = prev_entry->object.vm_object;
17752aaeadf8SMatthew Dillon offset = prev_entry->offset +
17762aaeadf8SMatthew Dillon (prev_entry->end - prev_entry->start);
17778cc7e047SJohn Dyson vm_object_reference(object);
1778ef694c1aSEdward Tomasz Napierala if (cred != NULL && object != NULL && object->cred != NULL &&
17793364c323SKonstantin Belousov !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
17803364c323SKonstantin Belousov /* Object already accounts for this uid. */
1781ef694c1aSEdward Tomasz Napierala cred = NULL;
17823364c323SKonstantin Belousov }
1783b18bfc3dSJohn Dyson }
178460169c88SAlan Cox if (cred != NULL)
178560169c88SAlan Cox crhold(cred);
17862aaeadf8SMatthew Dillon
17872aaeadf8SMatthew Dillon /*
178830dcfc09SJohn Dyson * Create a new entry
178930dcfc09SJohn Dyson */
179030dcfc09SJohn Dyson new_entry = vm_map_entry_create(map);
179130dcfc09SJohn Dyson new_entry->start = start;
179230dcfc09SJohn Dyson new_entry->end = end;
1793ef694c1aSEdward Tomasz Napierala new_entry->cred = NULL;
179430dcfc09SJohn Dyson
1795afa07f7eSJohn Dyson new_entry->eflags = protoeflags;
179630dcfc09SJohn Dyson new_entry->object.vm_object = object;
179730dcfc09SJohn Dyson new_entry->offset = offset;
17982267af78SJulian Elischer
17998211bd45SKonstantin Belousov new_entry->inheritance = inheritance;
180030dcfc09SJohn Dyson new_entry->protection = prot;
180130dcfc09SJohn Dyson new_entry->max_protection = max;
180230dcfc09SJohn Dyson new_entry->wired_count = 0;
1803997ac690SKonstantin Belousov new_entry->wiring_thread = NULL;
180413458803SAlan Cox new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1805381b7242SAlan Cox new_entry->next_read = start;
1806e5f251d2SAlan Cox
1807ef694c1aSEdward Tomasz Napierala KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
18081569205fSKonstantin Belousov ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1809ef694c1aSEdward Tomasz Napierala new_entry->cred = cred;
18103364c323SKonstantin Belousov
181130dcfc09SJohn Dyson /*
181230dcfc09SJohn Dyson * Insert the new entry into the list
181330dcfc09SJohn Dyson */
18149f701172SKonstantin Belousov vm_map_entry_link(map, new_entry);
181519bd0d9cSKonstantin Belousov if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
181630dcfc09SJohn Dyson map->size += new_entry->end - new_entry->start;
181730dcfc09SJohn Dyson
18181a484d28SMatthew Dillon /*
1819eaaf9f7fSAlan Cox * Try to coalesce the new entry with both the previous and next
1820eaaf9f7fSAlan Cox * entries in the list. Previously, we only attempted to coalesce
1821eaaf9f7fSAlan Cox * with the previous entry when object is NULL. Here, we handle the
1822eaaf9f7fSAlan Cox * other cases, which are less common.
18231a484d28SMatthew Dillon */
182483ea714fSDoug Moore vm_map_try_merge_entries(map, prev_entry, new_entry);
1825ba41b0deSKonstantin Belousov *res = vm_map_try_merge_entries(map, new_entry, next_entry);
18264e71e795SMatthew Dillon
18271569205fSKonstantin Belousov if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
18281569205fSKonstantin Belousov vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
18291569205fSKonstantin Belousov end - start, cow & MAP_PREFAULT_PARTIAL);
18304f79d873SMatthew Dillon }
1831e972780aSAlan Cox
183230dcfc09SJohn Dyson return (KERN_SUCCESS);
183330dcfc09SJohn Dyson }
183430dcfc09SJohn Dyson
183530dcfc09SJohn Dyson /*
1836ba41b0deSKonstantin Belousov * vm_map_insert:
1837ba41b0deSKonstantin Belousov *
1838ba41b0deSKonstantin Belousov * Inserts the given VM object into the target map at the
1839ba41b0deSKonstantin Belousov * specified address range.
1840ba41b0deSKonstantin Belousov *
1841ba41b0deSKonstantin Belousov * Requires that the map be locked, and leaves it so.
1842ba41b0deSKonstantin Belousov *
1843ba41b0deSKonstantin Belousov * If object is non-NULL, ref count must be bumped by caller
1844ba41b0deSKonstantin Belousov * prior to making call to account for the new entry.
1845ba41b0deSKonstantin Belousov */
1846ba41b0deSKonstantin Belousov int
vm_map_insert(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t start,vm_offset_t end,vm_prot_t prot,vm_prot_t max,int cow)1847ba41b0deSKonstantin Belousov vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1848ba41b0deSKonstantin Belousov vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1849ba41b0deSKonstantin Belousov {
1850ba41b0deSKonstantin Belousov vm_map_entry_t res;
1851ba41b0deSKonstantin Belousov
1852ba41b0deSKonstantin Belousov return (vm_map_insert1(map, object, offset, start, end, prot, max,
1853ba41b0deSKonstantin Belousov cow, &res));
1854ba41b0deSKonstantin Belousov }
1855ba41b0deSKonstantin Belousov
1856ba41b0deSKonstantin Belousov /*
18570164e057SAlan Cox * vm_map_findspace:
18580164e057SAlan Cox *
18590164e057SAlan Cox * Find the first fit (lowest VM address) for "length" free bytes
18600164e057SAlan Cox * beginning at address >= start in the given map.
18610164e057SAlan Cox *
18629f701172SKonstantin Belousov * In a vm_map_entry, "max_free" is the maximum amount of
18639f701172SKonstantin Belousov * contiguous free space between an entry in its subtree and a
18649f701172SKonstantin Belousov * neighbor of that entry. This allows finding a free region in
18659f701172SKonstantin Belousov * one path down the tree, so O(log n) amortized with splay
18669f701172SKonstantin Belousov * trees.
18670164e057SAlan Cox *
18680164e057SAlan Cox * The map must be locked, and leaves it so.
18690164e057SAlan Cox *
18709f701172SKonstantin Belousov * Returns: starting address if sufficient space,
18719f701172SKonstantin Belousov * vm_map_max(map)-length+1 if insufficient space.
1872df8bae1dSRodney W. Grimes */
18739f701172SKonstantin Belousov vm_offset_t
vm_map_findspace(vm_map_t map,vm_offset_t start,vm_size_t length)18749f701172SKonstantin Belousov vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1875df8bae1dSRodney W. Grimes {
187685b7bedbSDoug Moore vm_map_entry_t header, llist, rlist, root, y;
187785b7bedbSDoug Moore vm_size_t left_length, max_free_left, max_free_right;
1878e65d58a0SDoug Moore vm_offset_t gap_end;
1879df8bae1dSRodney W. Grimes
188020f02659SMark Johnston VM_MAP_ASSERT_LOCKED(map);
188120f02659SMark Johnston
1882986b43f8SAlan Cox /*
1883986b43f8SAlan Cox * Request must fit within min/max VM address and must avoid
1884986b43f8SAlan Cox * address wrap.
1885986b43f8SAlan Cox */
1886f0165b1cSKonstantin Belousov start = MAX(start, vm_map_min(map));
1887e65d58a0SDoug Moore if (start >= vm_map_max(map) || length > vm_map_max(map) - start)
18889f701172SKonstantin Belousov return (vm_map_max(map) - length + 1);
1889df8bae1dSRodney W. Grimes
18900164e057SAlan Cox /* Empty tree means wide open address space. */
18919f701172SKonstantin Belousov if (map->root == NULL)
18929f701172SKonstantin Belousov return (start);
18930164e057SAlan Cox
18940164e057SAlan Cox /*
1895e65d58a0SDoug Moore * After splay_split, if start is within an entry, push it to the start
1896e65d58a0SDoug Moore * of the following gap. If rlist is at the end of the gap containing
1897e65d58a0SDoug Moore * start, save the end of that gap in gap_end to see if the gap is big
1898e65d58a0SDoug Moore * enough; otherwise set gap_end to start skip gap-checking and move
1899e65d58a0SDoug Moore * directly to a search of the right subtree.
19000164e057SAlan Cox */
190185b7bedbSDoug Moore header = &map->header;
19025a0879daSDoug Moore root = vm_map_splay_split(map, start, length, &llist, &rlist);
1903e65d58a0SDoug Moore gap_end = rlist->start;
1904e65d58a0SDoug Moore if (root != NULL) {
19059f701172SKonstantin Belousov start = root->end;
1906c1ad5342SDoug Moore if (root->right != rlist)
1907e65d58a0SDoug Moore gap_end = start;
190885b7bedbSDoug Moore max_free_left = vm_map_splay_merge_left(header, root, llist);
190985b7bedbSDoug Moore max_free_right = vm_map_splay_merge_right(header, root, rlist);
191085b7bedbSDoug Moore } else if (rlist != header) {
19119f701172SKonstantin Belousov root = rlist;
19129f701172SKonstantin Belousov rlist = root->left;
191385b7bedbSDoug Moore max_free_left = vm_map_splay_merge_pred(header, root, llist);
191485b7bedbSDoug Moore max_free_right = vm_map_splay_merge_right(header, root, rlist);
19159f701172SKonstantin Belousov } else {
19169f701172SKonstantin Belousov root = llist;
19179f701172SKonstantin Belousov llist = root->right;
191885b7bedbSDoug Moore max_free_left = vm_map_splay_merge_left(header, root, llist);
191985b7bedbSDoug Moore max_free_right = vm_map_splay_merge_succ(header, root, rlist);
19200164e057SAlan Cox }
192185b7bedbSDoug Moore root->max_free = vm_size_max(max_free_left, max_free_right);
192285b7bedbSDoug Moore map->root = root;
19239f701172SKonstantin Belousov VM_MAP_ASSERT_CONSISTENT(map);
1924e65d58a0SDoug Moore if (length <= gap_end - start)
19259f701172SKonstantin Belousov return (start);
19260164e057SAlan Cox
19270164e057SAlan Cox /* With max_free, can immediately tell if no solution. */
1928c1ad5342SDoug Moore if (root->right == header || length > root->right->max_free)
19299f701172SKonstantin Belousov return (vm_map_max(map) - length + 1);
19300164e057SAlan Cox
19310164e057SAlan Cox /*
19329f701172SKonstantin Belousov * Splay for the least large-enough gap in the right subtree.
19330164e057SAlan Cox */
193485b7bedbSDoug Moore llist = rlist = header;
19359f701172SKonstantin Belousov for (left_length = 0;;
19365a0879daSDoug Moore left_length = vm_map_entry_max_free_left(root, llist)) {
19379f701172SKonstantin Belousov if (length <= left_length)
1938c1ad5342SDoug Moore SPLAY_LEFT_STEP(root, y, llist, rlist,
19395a0879daSDoug Moore length <= vm_map_entry_max_free_left(y, llist));
19409f701172SKonstantin Belousov else
1941c1ad5342SDoug Moore SPLAY_RIGHT_STEP(root, y, llist, rlist,
19425a0879daSDoug Moore length > vm_map_entry_max_free_left(y, root));
19439f701172SKonstantin Belousov if (root == NULL)
19449f701172SKonstantin Belousov break;
19450164e057SAlan Cox }
19469f701172SKonstantin Belousov root = llist;
19479f701172SKonstantin Belousov llist = root->right;
194885b7bedbSDoug Moore max_free_left = vm_map_splay_merge_left(header, root, llist);
194985b7bedbSDoug Moore if (rlist == header) {
195085b7bedbSDoug Moore root->max_free = vm_size_max(max_free_left,
195185b7bedbSDoug Moore vm_map_splay_merge_succ(header, root, rlist));
195285b7bedbSDoug Moore } else {
19535a0879daSDoug Moore y = rlist;
19549f701172SKonstantin Belousov rlist = y->left;
195585b7bedbSDoug Moore y->max_free = vm_size_max(
195685b7bedbSDoug Moore vm_map_splay_merge_pred(root, y, root),
195785b7bedbSDoug Moore vm_map_splay_merge_right(header, y, rlist));
195885b7bedbSDoug Moore root->max_free = vm_size_max(max_free_left, y->max_free);
19599f701172SKonstantin Belousov }
196085b7bedbSDoug Moore map->root = root;
19619f701172SKonstantin Belousov VM_MAP_ASSERT_CONSISTENT(map);
19629f701172SKonstantin Belousov return (root->end);
1963df8bae1dSRodney W. Grimes }
1964df8bae1dSRodney W. Grimes
1965d239bd3cSKonstantin Belousov int
vm_map_fixed(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t start,vm_size_t length,vm_prot_t prot,vm_prot_t max,int cow)1966d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1967b8ca4ef2SAlan Cox vm_offset_t start, vm_size_t length, vm_prot_t prot,
1968d239bd3cSKonstantin Belousov vm_prot_t max, int cow)
1969d239bd3cSKonstantin Belousov {
1970b8ca4ef2SAlan Cox vm_offset_t end;
1971d239bd3cSKonstantin Belousov int result;
1972d239bd3cSKonstantin Belousov
1973d239bd3cSKonstantin Belousov end = start + length;
19744648ba0aSKonstantin Belousov KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
19754648ba0aSKonstantin Belousov object == NULL,
19764648ba0aSKonstantin Belousov ("vm_map_fixed: non-NULL backing object for stack"));
1977897d81a0SKonstantin Belousov vm_map_lock(map);
1978d239bd3cSKonstantin Belousov VM_MAP_RANGE_CHECK(map, start, end);
1979e8f77c20SKonstantin Belousov if ((cow & MAP_CHECK_EXCL) == 0) {
1980e8f77c20SKonstantin Belousov result = vm_map_delete(map, start, end);
1981e8f77c20SKonstantin Belousov if (result != KERN_SUCCESS)
1982e8f77c20SKonstantin Belousov goto out;
1983e8f77c20SKonstantin Belousov }
19844648ba0aSKonstantin Belousov if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
19854648ba0aSKonstantin Belousov result = vm_map_stack_locked(map, start, length, sgrowsiz,
19864648ba0aSKonstantin Belousov prot, max, cow);
19874648ba0aSKonstantin Belousov } else {
19884648ba0aSKonstantin Belousov result = vm_map_insert(map, object, offset, start, end,
19894648ba0aSKonstantin Belousov prot, max, cow);
19904648ba0aSKonstantin Belousov }
1991e8f77c20SKonstantin Belousov out:
1992d239bd3cSKonstantin Belousov vm_map_unlock(map);
1993d239bd3cSKonstantin Belousov return (result);
1994d239bd3cSKonstantin Belousov }
1995d239bd3cSKonstantin Belousov
1996772ae9edSAlan Cox #if VM_NRESERVLEVEL <= 1
1997fa50a355SKonstantin Belousov static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
1998fa50a355SKonstantin Belousov static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
19993e00c11aSAlan Cox #elif VM_NRESERVLEVEL == 2
20003e00c11aSAlan Cox static const int aslr_pages_rnd_64[3] = {0x1000, 0x1000, 0x10};
20013e00c11aSAlan Cox static const int aslr_pages_rnd_32[3] = {0x100, 0x100, 0x4};
20023e00c11aSAlan Cox #else
20033e00c11aSAlan Cox #error "Unsupported VM_NRESERVLEVEL"
20043e00c11aSAlan Cox #endif
2005fa50a355SKonstantin Belousov
2006fa50a355SKonstantin Belousov static int cluster_anon = 1;
2007fa50a355SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
2008fa50a355SKonstantin Belousov &cluster_anon, 0,
2009484e9d03SKonstantin Belousov "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
2010484e9d03SKonstantin Belousov
2011484e9d03SKonstantin Belousov static bool
clustering_anon_allowed(vm_offset_t addr,int cow)2012d8e6f494SAlan Cox clustering_anon_allowed(vm_offset_t addr, int cow)
2013484e9d03SKonstantin Belousov {
2014484e9d03SKonstantin Belousov
2015484e9d03SKonstantin Belousov switch (cluster_anon) {
2016484e9d03SKonstantin Belousov case 0:
2017484e9d03SKonstantin Belousov return (false);
2018484e9d03SKonstantin Belousov case 1:
2019d8e6f494SAlan Cox return (addr == 0 || (cow & MAP_NO_HINT) != 0);
2020484e9d03SKonstantin Belousov case 2:
2021484e9d03SKonstantin Belousov default:
2022484e9d03SKonstantin Belousov return (true);
2023484e9d03SKonstantin Belousov }
2024484e9d03SKonstantin Belousov }
2025fa50a355SKonstantin Belousov
2026fa50a355SKonstantin Belousov static long aslr_restarts;
2027fa50a355SKonstantin Belousov SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
2028fa50a355SKonstantin Belousov &aslr_restarts, 0,
2029fa50a355SKonstantin Belousov "Number of aslr failures");
2030fa50a355SKonstantin Belousov
2031df8bae1dSRodney W. Grimes /*
2032fec29688SAlan Cox * Searches for the specified amount of free space in the given map with the
2033fec29688SAlan Cox * specified alignment. Performs an address-ordered, first-fit search from
2034fec29688SAlan Cox * the given address "*addr", with an optional upper bound "max_addr". If the
2035fec29688SAlan Cox * parameter "alignment" is zero, then the alignment is computed from the
2036fec29688SAlan Cox * given (object, offset) pair so as to enable the greatest possible use of
2037fec29688SAlan Cox * superpage mappings. Returns KERN_SUCCESS and the address of the free space
2038fec29688SAlan Cox * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE.
2039fec29688SAlan Cox *
2040fec29688SAlan Cox * The map must be locked. Initially, there must be at least "length" bytes
2041fec29688SAlan Cox * of free space at the given address.
2042fec29688SAlan Cox */
2043fec29688SAlan Cox static int
vm_map_alignspace(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t * addr,vm_size_t length,vm_offset_t max_addr,vm_offset_t alignment)2044fec29688SAlan Cox vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2045fec29688SAlan Cox vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
2046fec29688SAlan Cox vm_offset_t alignment)
2047fec29688SAlan Cox {
2048fec29688SAlan Cox vm_offset_t aligned_addr, free_addr;
2049fec29688SAlan Cox
2050fec29688SAlan Cox VM_MAP_ASSERT_LOCKED(map);
2051fec29688SAlan Cox free_addr = *addr;
20529f701172SKonstantin Belousov KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
2053e65d58a0SDoug Moore ("caller failed to provide space %#jx at address %p",
2054e65d58a0SDoug Moore (uintmax_t)length, (void *)free_addr));
2055fec29688SAlan Cox for (;;) {
2056fec29688SAlan Cox /*
2057fec29688SAlan Cox * At the start of every iteration, the free space at address
2058fec29688SAlan Cox * "*addr" is at least "length" bytes.
2059fec29688SAlan Cox */
2060fec29688SAlan Cox if (alignment == 0)
2061fec29688SAlan Cox pmap_align_superpage(object, offset, addr, length);
2062c606ab59SDoug Moore else
2063c606ab59SDoug Moore *addr = roundup2(*addr, alignment);
2064fec29688SAlan Cox aligned_addr = *addr;
2065fec29688SAlan Cox if (aligned_addr == free_addr) {
2066fec29688SAlan Cox /*
2067fec29688SAlan Cox * Alignment did not change "*addr", so "*addr" must
2068fec29688SAlan Cox * still provide sufficient free space.
2069fec29688SAlan Cox */
2070fec29688SAlan Cox return (KERN_SUCCESS);
2071fec29688SAlan Cox }
2072fec29688SAlan Cox
2073fec29688SAlan Cox /*
2074fec29688SAlan Cox * Test for address wrap on "*addr". A wrapped "*addr" could
2075fec29688SAlan Cox * be a valid address, in which case vm_map_findspace() cannot
2076fec29688SAlan Cox * be relied upon to fail.
2077fec29688SAlan Cox */
20789f701172SKonstantin Belousov if (aligned_addr < free_addr)
20799f701172SKonstantin Belousov return (KERN_NO_SPACE);
20809f701172SKonstantin Belousov *addr = vm_map_findspace(map, aligned_addr, length);
20819f701172SKonstantin Belousov if (*addr + length > vm_map_max(map) ||
2082fec29688SAlan Cox (max_addr != 0 && *addr + length > max_addr))
2083fec29688SAlan Cox return (KERN_NO_SPACE);
2084fec29688SAlan Cox free_addr = *addr;
2085fec29688SAlan Cox if (free_addr == aligned_addr) {
2086fec29688SAlan Cox /*
2087fec29688SAlan Cox * If a successful call to vm_map_findspace() did not
2088fec29688SAlan Cox * change "*addr", then "*addr" must still be aligned
2089fec29688SAlan Cox * and provide sufficient free space.
2090fec29688SAlan Cox */
2091fec29688SAlan Cox return (KERN_SUCCESS);
2092fec29688SAlan Cox }
2093fec29688SAlan Cox }
2094fec29688SAlan Cox }
2095fec29688SAlan Cox
20967a9f2da3SKonstantin Belousov int
vm_map_find_aligned(vm_map_t map,vm_offset_t * addr,vm_size_t length,vm_offset_t max_addr,vm_offset_t alignment)20977a9f2da3SKonstantin Belousov vm_map_find_aligned(vm_map_t map, vm_offset_t *addr, vm_size_t length,
20987a9f2da3SKonstantin Belousov vm_offset_t max_addr, vm_offset_t alignment)
20997a9f2da3SKonstantin Belousov {
21007a9f2da3SKonstantin Belousov /* XXXKIB ASLR eh ? */
21017a9f2da3SKonstantin Belousov *addr = vm_map_findspace(map, *addr, length);
21027a9f2da3SKonstantin Belousov if (*addr + length > vm_map_max(map) ||
21037a9f2da3SKonstantin Belousov (max_addr != 0 && *addr + length > max_addr))
21047a9f2da3SKonstantin Belousov return (KERN_NO_SPACE);
21057a9f2da3SKonstantin Belousov return (vm_map_alignspace(map, NULL, 0, addr, length, max_addr,
21067a9f2da3SKonstantin Belousov alignment));
21077a9f2da3SKonstantin Belousov }
21087a9f2da3SKonstantin Belousov
2109fec29688SAlan Cox /*
2110df8bae1dSRodney W. Grimes * vm_map_find finds an unallocated region in the target address
2111df8bae1dSRodney W. Grimes * map with the given length. The search is defined to be
2112df8bae1dSRodney W. Grimes * first-fit from the specified address; the region found is
2113df8bae1dSRodney W. Grimes * returned in the same parameter.
2114df8bae1dSRodney W. Grimes *
21152aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller
21162aaeadf8SMatthew Dillon * prior to making call to account for the new entry.
2117df8bae1dSRodney W. Grimes */
2118df8bae1dSRodney W. Grimes int
vm_map_find(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t * addr,vm_size_t length,vm_offset_t max_addr,int find_space,vm_prot_t prot,vm_prot_t max,int cow)2119b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2120b9dcd593SBruce Evans vm_offset_t *addr, /* IN/OUT */
2121edb572a3SJohn Baldwin vm_size_t length, vm_offset_t max_addr, int find_space,
2122edb572a3SJohn Baldwin vm_prot_t prot, vm_prot_t max, int cow)
2123df8bae1dSRodney W. Grimes {
2124*0ecbb28cSKonstantin Belousov int rv;
2125*0ecbb28cSKonstantin Belousov
2126*0ecbb28cSKonstantin Belousov vm_map_lock(map);
2127*0ecbb28cSKonstantin Belousov rv = vm_map_find_locked(map, object, offset, addr, length, max_addr,
2128*0ecbb28cSKonstantin Belousov find_space, prot, max, cow);
2129*0ecbb28cSKonstantin Belousov vm_map_unlock(map);
2130*0ecbb28cSKonstantin Belousov return (rv);
2131*0ecbb28cSKonstantin Belousov }
2132*0ecbb28cSKonstantin Belousov
2133*0ecbb28cSKonstantin Belousov int
vm_map_find_locked(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t * addr,vm_size_t length,vm_offset_t max_addr,int find_space,vm_prot_t prot,vm_prot_t max,int cow)2134*0ecbb28cSKonstantin Belousov vm_map_find_locked(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2135*0ecbb28cSKonstantin Belousov vm_offset_t *addr, /* IN/OUT */
2136*0ecbb28cSKonstantin Belousov vm_size_t length, vm_offset_t max_addr, int find_space,
2137*0ecbb28cSKonstantin Belousov vm_prot_t prot, vm_prot_t max, int cow)
2138*0ecbb28cSKonstantin Belousov {
2139fa50a355SKonstantin Belousov vm_offset_t alignment, curr_min_addr, min_addr;
2140fa50a355SKonstantin Belousov int gap, pidx, rv, try;
2141fa50a355SKonstantin Belousov bool cluster, en_aslr, update_anon;
2142df8bae1dSRodney W. Grimes
21434648ba0aSKonstantin Belousov KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
21444648ba0aSKonstantin Belousov object == NULL,
2145*0ecbb28cSKonstantin Belousov ("non-NULL backing object for stack"));
2146ea7e7006SKonstantin Belousov MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
2147ea7e7006SKonstantin Belousov (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
2148ff74a3faSJohn Baldwin if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
2149ff74a3faSJohn Baldwin (object->flags & OBJ_COLORED) == 0))
2150ff74a3faSJohn Baldwin find_space = VMFS_ANY_SPACE;
21515aa60b6fSJohn Baldwin if (find_space >> 8 != 0) {
21525aa60b6fSJohn Baldwin KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
21535aa60b6fSJohn Baldwin alignment = (vm_offset_t)1 << (find_space >> 8);
21545aa60b6fSJohn Baldwin } else
21555aa60b6fSJohn Baldwin alignment = 0;
2156fa50a355SKonstantin Belousov en_aslr = (map->flags & MAP_ASLR) != 0;
2157d8e6f494SAlan Cox update_anon = cluster = clustering_anon_allowed(*addr, cow) &&
2158fa50a355SKonstantin Belousov (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
2159fa50a355SKonstantin Belousov find_space != VMFS_NO_SPACE && object == NULL &&
2160fa50a355SKonstantin Belousov (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
2161fa50a355SKonstantin Belousov MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
2162fa50a355SKonstantin Belousov curr_min_addr = min_addr = *addr;
2163fa50a355SKonstantin Belousov if (en_aslr && min_addr == 0 && !cluster &&
2164fa50a355SKonstantin Belousov find_space != VMFS_NO_SPACE &&
2165fa50a355SKonstantin Belousov (map->flags & MAP_ASLR_IGNSTART) != 0)
2166fa50a355SKonstantin Belousov curr_min_addr = min_addr = vm_map_min(map);
2167fa50a355SKonstantin Belousov try = 0;
2168fa50a355SKonstantin Belousov if (cluster) {
2169fa50a355SKonstantin Belousov curr_min_addr = map->anon_loc;
2170fa50a355SKonstantin Belousov if (curr_min_addr == 0)
2171fa50a355SKonstantin Belousov cluster = false;
2172fa50a355SKonstantin Belousov }
217326c538ffSAlan Cox if (find_space != VMFS_NO_SPACE) {
2174fec29688SAlan Cox KASSERT(find_space == VMFS_ANY_SPACE ||
2175fec29688SAlan Cox find_space == VMFS_OPTIMAL_SPACE ||
2176fec29688SAlan Cox find_space == VMFS_SUPER_SPACE ||
2177fec29688SAlan Cox alignment != 0, ("unexpected VMFS flag"));
2178fec29688SAlan Cox again:
2179fa50a355SKonstantin Belousov /*
2180fa50a355SKonstantin Belousov * When creating an anonymous mapping, try clustering
2181fa50a355SKonstantin Belousov * with an existing anonymous mapping first.
2182fa50a355SKonstantin Belousov *
2183fa50a355SKonstantin Belousov * We make up to two attempts to find address space
2184fa50a355SKonstantin Belousov * for a given find_space value. The first attempt may
2185fa50a355SKonstantin Belousov * apply randomization or may cluster with an existing
2186fa50a355SKonstantin Belousov * anonymous mapping. If this first attempt fails,
2187fa50a355SKonstantin Belousov * perform a first-fit search of the available address
2188fa50a355SKonstantin Belousov * space.
2189fa50a355SKonstantin Belousov *
2190fa50a355SKonstantin Belousov * If all tries failed, and find_space is
2191fa50a355SKonstantin Belousov * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
2192fa50a355SKonstantin Belousov * Again enable clustering and randomization.
2193fa50a355SKonstantin Belousov */
2194fa50a355SKonstantin Belousov try++;
2195fa50a355SKonstantin Belousov MPASS(try <= 2);
2196fa50a355SKonstantin Belousov
2197fa50a355SKonstantin Belousov if (try == 2) {
2198fa50a355SKonstantin Belousov /*
2199fa50a355SKonstantin Belousov * Second try: we failed either to find a
2200fa50a355SKonstantin Belousov * suitable region for randomizing the
2201fa50a355SKonstantin Belousov * allocation, or to cluster with an existing
2202fa50a355SKonstantin Belousov * mapping. Retry with free run.
2203fa50a355SKonstantin Belousov */
2204fa50a355SKonstantin Belousov curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
2205fa50a355SKonstantin Belousov vm_map_min(map) : min_addr;
2206fa50a355SKonstantin Belousov atomic_add_long(&aslr_restarts, 1);
2207fa50a355SKonstantin Belousov }
2208fa50a355SKonstantin Belousov
2209fa50a355SKonstantin Belousov if (try == 1 && en_aslr && !cluster) {
2210fa50a355SKonstantin Belousov /*
2211fa50a355SKonstantin Belousov * Find space for allocation, including
2212fa50a355SKonstantin Belousov * gap needed for later randomization.
2213fa50a355SKonstantin Belousov */
22143e00c11aSAlan Cox pidx = 0;
22153e00c11aSAlan Cox #if VM_NRESERVLEVEL > 0
22163e00c11aSAlan Cox if ((find_space == VMFS_SUPER_SPACE ||
22173e00c11aSAlan Cox find_space == VMFS_OPTIMAL_SPACE) &&
22183e00c11aSAlan Cox pagesizes[VM_NRESERVLEVEL] != 0) {
22193e00c11aSAlan Cox /*
22203e00c11aSAlan Cox * Do not pointlessly increase the space that
22213e00c11aSAlan Cox * is requested from vm_map_findspace().
22223e00c11aSAlan Cox * pmap_align_superpage() will only change a
22233e00c11aSAlan Cox * mapping's alignment if that mapping is at
22243e00c11aSAlan Cox * least a superpage in size.
22253e00c11aSAlan Cox */
22263e00c11aSAlan Cox pidx = VM_NRESERVLEVEL;
22273e00c11aSAlan Cox while (pidx > 0 && length < pagesizes[pidx])
22283e00c11aSAlan Cox pidx--;
22293e00c11aSAlan Cox }
22303e00c11aSAlan Cox #endif
2231fa50a355SKonstantin Belousov gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
2232fa50a355SKonstantin Belousov (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
2233fa50a355SKonstantin Belousov aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
22349f701172SKonstantin Belousov *addr = vm_map_findspace(map, curr_min_addr,
22359f701172SKonstantin Belousov length + gap * pagesizes[pidx]);
22369f701172SKonstantin Belousov if (*addr + length + gap * pagesizes[pidx] >
2237a5a02ef4SKonstantin Belousov vm_map_max(map))
2238fa50a355SKonstantin Belousov goto again;
2239fa50a355SKonstantin Belousov /* And randomize the start address. */
2240fa50a355SKonstantin Belousov *addr += (arc4random() % gap) * pagesizes[pidx];
22415019dac9SKonstantin Belousov if (max_addr != 0 && *addr + length > max_addr)
22425019dac9SKonstantin Belousov goto again;
22439f701172SKonstantin Belousov } else {
22449f701172SKonstantin Belousov *addr = vm_map_findspace(map, curr_min_addr, length);
22459f701172SKonstantin Belousov if (*addr + length > vm_map_max(map) ||
2246edb572a3SJohn Baldwin (max_addr != 0 && *addr + length > max_addr)) {
2247fa50a355SKonstantin Belousov if (cluster) {
2248fa50a355SKonstantin Belousov cluster = false;
2249fa50a355SKonstantin Belousov MPASS(try == 1);
2250fa50a355SKonstantin Belousov goto again;
2251fa50a355SKonstantin Belousov }
2252*0ecbb28cSKonstantin Belousov return (KERN_NO_SPACE);
2253fec29688SAlan Cox }
22549f701172SKonstantin Belousov }
2255fa50a355SKonstantin Belousov
2256fec29688SAlan Cox if (find_space != VMFS_ANY_SPACE &&
2257fec29688SAlan Cox (rv = vm_map_alignspace(map, object, offset, addr, length,
2258fec29688SAlan Cox max_addr, alignment)) != KERN_SUCCESS) {
2259ff74a3faSJohn Baldwin if (find_space == VMFS_OPTIMAL_SPACE) {
2260ff74a3faSJohn Baldwin find_space = VMFS_ANY_SPACE;
2261fa50a355SKonstantin Belousov curr_min_addr = min_addr;
2262fa50a355SKonstantin Belousov cluster = update_anon;
2263fa50a355SKonstantin Belousov try = 0;
2264ff74a3faSJohn Baldwin goto again;
2265ff74a3faSJohn Baldwin }
2266*0ecbb28cSKonstantin Belousov return (rv);
2267df8bae1dSRodney W. Grimes }
2268ea7e7006SKonstantin Belousov } else if ((cow & MAP_REMAP) != 0) {
2269*0ecbb28cSKonstantin Belousov if (!vm_map_range_valid(map, *addr, *addr + length))
2270*0ecbb28cSKonstantin Belousov return (KERN_INVALID_ADDRESS);
2271e8f77c20SKonstantin Belousov rv = vm_map_delete(map, *addr, *addr + length);
2272e8f77c20SKonstantin Belousov if (rv != KERN_SUCCESS)
2273*0ecbb28cSKonstantin Belousov return (rv);
2274df8bae1dSRodney W. Grimes }
22754648ba0aSKonstantin Belousov if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
2276fec29688SAlan Cox rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
2277fec29688SAlan Cox max, cow);
22784648ba0aSKonstantin Belousov } else {
2279fec29688SAlan Cox rv = vm_map_insert(map, object, offset, *addr, *addr + length,
2280fec29688SAlan Cox prot, max, cow);
22814648ba0aSKonstantin Belousov }
2282268f19aaSAlan Cox
2283268f19aaSAlan Cox /*
2284268f19aaSAlan Cox * Update the starting address for clustered anonymous memory mappings
2285268f19aaSAlan Cox * if a starting address was not previously defined or an ASLR restart
2286268f19aaSAlan Cox * placed an anonymous memory mapping at a lower address.
2287268f19aaSAlan Cox */
2288268f19aaSAlan Cox if (update_anon && rv == KERN_SUCCESS && (map->anon_loc == 0 ||
2289268f19aaSAlan Cox *addr < map->anon_loc))
2290268f19aaSAlan Cox map->anon_loc = *addr;
2291fec29688SAlan Cox return (rv);
2292df8bae1dSRodney W. Grimes }
2293df8bae1dSRodney W. Grimes
2294e8502826SKonstantin Belousov /*
2295e8502826SKonstantin Belousov * vm_map_find_min() is a variant of vm_map_find() that takes an
229637e5d49eSAlan Cox * additional parameter ("default_addr") and treats the given address
229737e5d49eSAlan Cox * ("*addr") differently. Specifically, it treats "*addr" as a hint
2298e8502826SKonstantin Belousov * and not as the minimum address where the mapping is created.
2299e8502826SKonstantin Belousov *
2300e8502826SKonstantin Belousov * This function works in two phases. First, it tries to
2301e8502826SKonstantin Belousov * allocate above the hint. If that fails and the hint is
230237e5d49eSAlan Cox * greater than "default_addr", it performs a second pass, replacing
230337e5d49eSAlan Cox * the hint with "default_addr" as the minimum address for the
2304e8502826SKonstantin Belousov * allocation.
2305e8502826SKonstantin Belousov */
23066a97a3f7SKonstantin Belousov int
vm_map_find_min(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t * addr,vm_size_t length,vm_offset_t default_addr,vm_offset_t max_addr,int find_space,vm_prot_t prot,vm_prot_t max,int cow)23076a97a3f7SKonstantin Belousov vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
230837e5d49eSAlan Cox vm_offset_t *addr, vm_size_t length, vm_offset_t default_addr,
23096a97a3f7SKonstantin Belousov vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
23106a97a3f7SKonstantin Belousov int cow)
23116a97a3f7SKonstantin Belousov {
23126a97a3f7SKonstantin Belousov vm_offset_t hint;
23136a97a3f7SKonstantin Belousov int rv;
23146a97a3f7SKonstantin Belousov
23156a97a3f7SKonstantin Belousov hint = *addr;
231650d663b1SAlan Cox if (hint == 0) {
2317d8e6f494SAlan Cox cow |= MAP_NO_HINT;
231837e5d49eSAlan Cox *addr = hint = default_addr;
231950d663b1SAlan Cox }
23206a97a3f7SKonstantin Belousov for (;;) {
23216a97a3f7SKonstantin Belousov rv = vm_map_find(map, object, offset, addr, length, max_addr,
23226a97a3f7SKonstantin Belousov find_space, prot, max, cow);
232337e5d49eSAlan Cox if (rv == KERN_SUCCESS || default_addr >= hint)
23246a97a3f7SKonstantin Belousov return (rv);
232537e5d49eSAlan Cox *addr = hint = default_addr;
23266a97a3f7SKonstantin Belousov }
23276a97a3f7SKonstantin Belousov }
23286a97a3f7SKonstantin Belousov
232992e78c10SAlan Cox /*
233092e78c10SAlan Cox * A map entry with any of the following flags set must not be merged with
233192e78c10SAlan Cox * another entry.
233292e78c10SAlan Cox */
233392e78c10SAlan Cox #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
23349d7ea6cfSKonstantin Belousov MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_VN_EXEC | \
23359d7ea6cfSKonstantin Belousov MAP_ENTRY_STACK_GAP_UP | MAP_ENTRY_STACK_GAP_DN)
233692e78c10SAlan Cox
233707424462SKonstantin Belousov static bool
vm_map_mergeable_neighbors(vm_map_entry_t prev,vm_map_entry_t entry)233807424462SKonstantin Belousov vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
233907424462SKonstantin Belousov {
234007424462SKonstantin Belousov
234192e78c10SAlan Cox KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
234292e78c10SAlan Cox (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
234392e78c10SAlan Cox ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
234492e78c10SAlan Cox prev, entry));
234507424462SKonstantin Belousov return (prev->end == entry->start &&
234607424462SKonstantin Belousov prev->object.vm_object == entry->object.vm_object &&
234707424462SKonstantin Belousov (prev->object.vm_object == NULL ||
234892e78c10SAlan Cox prev->offset + (prev->end - prev->start) == entry->offset) &&
234907424462SKonstantin Belousov prev->eflags == entry->eflags &&
235007424462SKonstantin Belousov prev->protection == entry->protection &&
235107424462SKonstantin Belousov prev->max_protection == entry->max_protection &&
235207424462SKonstantin Belousov prev->inheritance == entry->inheritance &&
235307424462SKonstantin Belousov prev->wired_count == entry->wired_count &&
235407424462SKonstantin Belousov prev->cred == entry->cred);
235507424462SKonstantin Belousov }
235607424462SKonstantin Belousov
235707424462SKonstantin Belousov static void
vm_map_merged_neighbor_dispose(vm_map_t map,vm_map_entry_t entry)235807424462SKonstantin Belousov vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
235907424462SKonstantin Belousov {
236007424462SKonstantin Belousov
236107424462SKonstantin Belousov /*
236292e78c10SAlan Cox * If the backing object is a vnode object, vm_object_deallocate()
236392e78c10SAlan Cox * calls vrele(). However, vrele() does not lock the vnode because
236492e78c10SAlan Cox * the vnode has additional references. Thus, the map lock can be
236592e78c10SAlan Cox * kept without causing a lock-order reversal with the vnode lock.
236607424462SKonstantin Belousov *
236792e78c10SAlan Cox * Since we count the number of virtual page mappings in
236892e78c10SAlan Cox * object->un_pager.vnp.writemappings, the writemappings value
236992e78c10SAlan Cox * should not be adjusted when the entry is disposed of.
237007424462SKonstantin Belousov */
237107424462SKonstantin Belousov if (entry->object.vm_object != NULL)
237207424462SKonstantin Belousov vm_object_deallocate(entry->object.vm_object);
237307424462SKonstantin Belousov if (entry->cred != NULL)
237407424462SKonstantin Belousov crfree(entry->cred);
237507424462SKonstantin Belousov vm_map_entry_dispose(map, entry);
237607424462SKonstantin Belousov }
237707424462SKonstantin Belousov
2378df8bae1dSRodney W. Grimes /*
237983ea714fSDoug Moore * vm_map_try_merge_entries:
238067bf6868SJohn Dyson *
2381ba41b0deSKonstantin Belousov * Compare two map entries that represent consecutive ranges. If
2382ba41b0deSKonstantin Belousov * the entries can be merged, expand the range of the second to
2383ba41b0deSKonstantin Belousov * cover the range of the first and delete the first. Then return
2384ba41b0deSKonstantin Belousov * the map entry that includes the first range.
23854e71e795SMatthew Dillon *
23864e71e795SMatthew Dillon * The map must be locked.
2387df8bae1dSRodney W. Grimes */
2388ba41b0deSKonstantin Belousov vm_map_entry_t
vm_map_try_merge_entries(vm_map_t map,vm_map_entry_t prev_entry,vm_map_entry_t entry)23892767c9f3SDoug Moore vm_map_try_merge_entries(vm_map_t map, vm_map_entry_t prev_entry,
23902767c9f3SDoug Moore vm_map_entry_t entry)
2391df8bae1dSRodney W. Grimes {
2392df8bae1dSRodney W. Grimes
239383ea714fSDoug Moore VM_MAP_ASSERT_LOCKED(map);
239483ea714fSDoug Moore if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 &&
23952767c9f3SDoug Moore vm_map_mergeable_neighbors(prev_entry, entry)) {
23962767c9f3SDoug Moore vm_map_entry_unlink(map, prev_entry, UNLINK_MERGE_NEXT);
23972767c9f3SDoug Moore vm_map_merged_neighbor_dispose(map, prev_entry);
2398ba41b0deSKonstantin Belousov return (entry);
2399308c24baSJohn Dyson }
2400ba41b0deSKonstantin Belousov return (prev_entry);
2401df8bae1dSRodney W. Grimes }
240292e78c10SAlan Cox
2403df8bae1dSRodney W. Grimes /*
2404af1d6d6aSDoug Moore * vm_map_entry_back:
2405af1d6d6aSDoug Moore *
2406af1d6d6aSDoug Moore * Allocate an object to back a map entry.
2407af1d6d6aSDoug Moore */
2408af1d6d6aSDoug Moore static inline void
vm_map_entry_back(vm_map_entry_t entry)2409af1d6d6aSDoug Moore vm_map_entry_back(vm_map_entry_t entry)
2410af1d6d6aSDoug Moore {
2411af1d6d6aSDoug Moore vm_object_t object;
2412af1d6d6aSDoug Moore
2413af1d6d6aSDoug Moore KASSERT(entry->object.vm_object == NULL,
2414af1d6d6aSDoug Moore ("map entry %p has backing object", entry));
2415af1d6d6aSDoug Moore KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2416af1d6d6aSDoug Moore ("map entry %p is a submap", entry));
241767388836SKonstantin Belousov object = vm_object_allocate_anon(atop(entry->end - entry->start), NULL,
241867388836SKonstantin Belousov entry->cred, entry->end - entry->start);
2419af1d6d6aSDoug Moore entry->object.vm_object = object;
2420af1d6d6aSDoug Moore entry->offset = 0;
2421af1d6d6aSDoug Moore entry->cred = NULL;
2422af1d6d6aSDoug Moore }
2423af1d6d6aSDoug Moore
2424af1d6d6aSDoug Moore /*
2425af1d6d6aSDoug Moore * vm_map_entry_charge_object
2426af1d6d6aSDoug Moore *
2427af1d6d6aSDoug Moore * If there is no object backing this entry, create one. Otherwise, if
2428af1d6d6aSDoug Moore * the entry has cred, give it to the backing object.
2429af1d6d6aSDoug Moore */
2430af1d6d6aSDoug Moore static inline void
vm_map_entry_charge_object(vm_map_t map,vm_map_entry_t entry)2431af1d6d6aSDoug Moore vm_map_entry_charge_object(vm_map_t map, vm_map_entry_t entry)
2432af1d6d6aSDoug Moore {
2433af1d6d6aSDoug Moore
2434af1d6d6aSDoug Moore VM_MAP_ASSERT_LOCKED(map);
2435af1d6d6aSDoug Moore KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2436af1d6d6aSDoug Moore ("map entry %p is a submap", entry));
2437af1d6d6aSDoug Moore if (entry->object.vm_object == NULL && !map->system_map &&
2438af1d6d6aSDoug Moore (entry->eflags & MAP_ENTRY_GUARD) == 0)
2439af1d6d6aSDoug Moore vm_map_entry_back(entry);
2440af1d6d6aSDoug Moore else if (entry->object.vm_object != NULL &&
2441af1d6d6aSDoug Moore ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2442af1d6d6aSDoug Moore entry->cred != NULL) {
2443af1d6d6aSDoug Moore VM_OBJECT_WLOCK(entry->object.vm_object);
2444af1d6d6aSDoug Moore KASSERT(entry->object.vm_object->cred == NULL,
2445af1d6d6aSDoug Moore ("OVERCOMMIT: %s: both cred e %p", __func__, entry));
2446af1d6d6aSDoug Moore entry->object.vm_object->cred = entry->cred;
2447af1d6d6aSDoug Moore entry->object.vm_object->charge = entry->end - entry->start;
2448af1d6d6aSDoug Moore VM_OBJECT_WUNLOCK(entry->object.vm_object);
2449af1d6d6aSDoug Moore entry->cred = NULL;
2450af1d6d6aSDoug Moore }
2451af1d6d6aSDoug Moore }
2452af1d6d6aSDoug Moore
2453af1d6d6aSDoug Moore /*
2454037c0994SDoug Moore * vm_map_entry_clone
2455037c0994SDoug Moore *
2456037c0994SDoug Moore * Create a duplicate map entry for clipping.
2457037c0994SDoug Moore */
2458037c0994SDoug Moore static vm_map_entry_t
vm_map_entry_clone(vm_map_t map,vm_map_entry_t entry)2459037c0994SDoug Moore vm_map_entry_clone(vm_map_t map, vm_map_entry_t entry)
2460037c0994SDoug Moore {
2461037c0994SDoug Moore vm_map_entry_t new_entry;
2462037c0994SDoug Moore
2463037c0994SDoug Moore VM_MAP_ASSERT_LOCKED(map);
2464037c0994SDoug Moore
2465037c0994SDoug Moore /*
2466037c0994SDoug Moore * Create a backing object now, if none exists, so that more individual
2467037c0994SDoug Moore * objects won't be created after the map entry is split.
2468037c0994SDoug Moore */
2469037c0994SDoug Moore vm_map_entry_charge_object(map, entry);
2470037c0994SDoug Moore
2471037c0994SDoug Moore /* Clone the entry. */
2472037c0994SDoug Moore new_entry = vm_map_entry_create(map);
2473037c0994SDoug Moore *new_entry = *entry;
2474037c0994SDoug Moore if (new_entry->cred != NULL)
2475037c0994SDoug Moore crhold(entry->cred);
2476037c0994SDoug Moore if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2477037c0994SDoug Moore vm_object_reference(new_entry->object.vm_object);
2478037c0994SDoug Moore vm_map_entry_set_vnode_text(new_entry, true);
2479037c0994SDoug Moore /*
2480037c0994SDoug Moore * The object->un_pager.vnp.writemappings for the object of
2481037c0994SDoug Moore * MAP_ENTRY_WRITECNT type entry shall be kept as is here. The
2482037c0994SDoug Moore * virtual pages are re-distributed among the clipped entries,
2483037c0994SDoug Moore * so the sum is left the same.
2484037c0994SDoug Moore */
2485037c0994SDoug Moore }
2486037c0994SDoug Moore return (new_entry);
2487037c0994SDoug Moore }
2488037c0994SDoug Moore
2489037c0994SDoug Moore /*
2490df8bae1dSRodney W. Grimes * vm_map_clip_start: [ internal use only ]
2491df8bae1dSRodney W. Grimes *
2492df8bae1dSRodney W. Grimes * Asserts that the given entry begins at or after
2493df8bae1dSRodney W. Grimes * the specified address; if necessary,
2494df8bae1dSRodney W. Grimes * it splits the entry into two.
2495df8bae1dSRodney W. Grimes */
2496e2e80fb3SKonstantin Belousov static int
vm_map_clip_start(vm_map_t map,vm_map_entry_t entry,vm_offset_t startaddr)2497e2e80fb3SKonstantin Belousov vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t startaddr)
2498df8bae1dSRodney W. Grimes {
2499c0877f10SJohn Dyson vm_map_entry_t new_entry;
2500e2e80fb3SKonstantin Belousov int bdry_idx;
2501df8bae1dSRodney W. Grimes
25028a64110eSConrad Meyer if (!map->system_map)
25038a64110eSConrad Meyer WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
25048a64110eSConrad Meyer "%s: map %p entry %p start 0x%jx", __func__, map, entry,
2505e2e80fb3SKonstantin Belousov (uintmax_t)startaddr);
25068a64110eSConrad Meyer
2507e2e80fb3SKonstantin Belousov if (startaddr <= entry->start)
2508e2e80fb3SKonstantin Belousov return (KERN_SUCCESS);
2509a116b5d3SConrad Meyer
25103a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map);
2511e2e80fb3SKonstantin Belousov KASSERT(entry->end > startaddr && entry->start < startaddr,
2512a116b5d3SConrad Meyer ("%s: invalid clip of entry %p", __func__, entry));
25133a0916b8SKonstantin Belousov
2514d0e4e53eSMark Johnston bdry_idx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(entry);
2515e2e80fb3SKonstantin Belousov if (bdry_idx != 0) {
2516e2e80fb3SKonstantin Belousov if ((startaddr & (pagesizes[bdry_idx] - 1)) != 0)
2517e2e80fb3SKonstantin Belousov return (KERN_INVALID_ARGUMENT);
2518e2e80fb3SKonstantin Belousov }
2519e2e80fb3SKonstantin Belousov
2520037c0994SDoug Moore new_entry = vm_map_entry_clone(map, entry);
2521df8bae1dSRodney W. Grimes
25224766eba1SDoug Moore /*
25234766eba1SDoug Moore * Split off the front portion. Insert the new entry BEFORE this one,
25244766eba1SDoug Moore * so that this entry has the specified starting address.
25254766eba1SDoug Moore */
2526e2e80fb3SKonstantin Belousov new_entry->end = startaddr;
25279f701172SKonstantin Belousov vm_map_entry_link(map, new_entry);
2528e2e80fb3SKonstantin Belousov return (KERN_SUCCESS);
2529c0877f10SJohn Dyson }
2530df8bae1dSRodney W. Grimes
2531df8bae1dSRodney W. Grimes /*
2532c7b23459SDoug Moore * vm_map_lookup_clip_start:
2533c7b23459SDoug Moore *
2534c7b23459SDoug Moore * Find the entry at or just after 'start', and clip it if 'start' is in
2535c7b23459SDoug Moore * the interior of the entry. Return entry after 'start', and in
2536c7b23459SDoug Moore * prev_entry set the entry before 'start'.
2537c7b23459SDoug Moore */
2538e2e80fb3SKonstantin Belousov static int
vm_map_lookup_clip_start(vm_map_t map,vm_offset_t start,vm_map_entry_t * res_entry,vm_map_entry_t * prev_entry)2539c7b23459SDoug Moore vm_map_lookup_clip_start(vm_map_t map, vm_offset_t start,
2540e2e80fb3SKonstantin Belousov vm_map_entry_t *res_entry, vm_map_entry_t *prev_entry)
2541c7b23459SDoug Moore {
2542c7b23459SDoug Moore vm_map_entry_t entry;
2543e2e80fb3SKonstantin Belousov int rv;
2544c7b23459SDoug Moore
25458a64110eSConrad Meyer if (!map->system_map)
25468a64110eSConrad Meyer WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
25478a64110eSConrad Meyer "%s: map %p start 0x%jx prev %p", __func__, map,
25488a64110eSConrad Meyer (uintmax_t)start, prev_entry);
25498a64110eSConrad Meyer
2550c7b23459SDoug Moore if (vm_map_lookup_entry(map, start, prev_entry)) {
2551c7b23459SDoug Moore entry = *prev_entry;
2552e2e80fb3SKonstantin Belousov rv = vm_map_clip_start(map, entry, start);
2553e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
2554e2e80fb3SKonstantin Belousov return (rv);
2555c7b23459SDoug Moore *prev_entry = vm_map_entry_pred(entry);
2556c7b23459SDoug Moore } else
2557c7b23459SDoug Moore entry = vm_map_entry_succ(*prev_entry);
2558e2e80fb3SKonstantin Belousov *res_entry = entry;
2559e2e80fb3SKonstantin Belousov return (KERN_SUCCESS);
2560c7b23459SDoug Moore }
2561c7b23459SDoug Moore
2562c7b23459SDoug Moore /*
2563df8bae1dSRodney W. Grimes * vm_map_clip_end: [ internal use only ]
2564df8bae1dSRodney W. Grimes *
2565df8bae1dSRodney W. Grimes * Asserts that the given entry ends at or before
2566df8bae1dSRodney W. Grimes * the specified address; if necessary,
2567df8bae1dSRodney W. Grimes * it splits the entry into two.
2568df8bae1dSRodney W. Grimes */
2569e2e80fb3SKonstantin Belousov static int
vm_map_clip_end(vm_map_t map,vm_map_entry_t entry,vm_offset_t endaddr)2570e2e80fb3SKonstantin Belousov vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t endaddr)
2571df8bae1dSRodney W. Grimes {
2572c0877f10SJohn Dyson vm_map_entry_t new_entry;
2573e2e80fb3SKonstantin Belousov int bdry_idx;
2574df8bae1dSRodney W. Grimes
25758a64110eSConrad Meyer if (!map->system_map)
25768a64110eSConrad Meyer WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
25778a64110eSConrad Meyer "%s: map %p entry %p end 0x%jx", __func__, map, entry,
2578e2e80fb3SKonstantin Belousov (uintmax_t)endaddr);
25798a64110eSConrad Meyer
2580e2e80fb3SKonstantin Belousov if (endaddr >= entry->end)
2581e2e80fb3SKonstantin Belousov return (KERN_SUCCESS);
2582a116b5d3SConrad Meyer
25833a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map);
2584e2e80fb3SKonstantin Belousov KASSERT(entry->start < endaddr && entry->end > endaddr,
2585a116b5d3SConrad Meyer ("%s: invalid clip of entry %p", __func__, entry));
25863a0916b8SKonstantin Belousov
2587d0e4e53eSMark Johnston bdry_idx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(entry);
2588e2e80fb3SKonstantin Belousov if (bdry_idx != 0) {
2589e2e80fb3SKonstantin Belousov if ((endaddr & (pagesizes[bdry_idx] - 1)) != 0)
2590e2e80fb3SKonstantin Belousov return (KERN_INVALID_ARGUMENT);
2591e2e80fb3SKonstantin Belousov }
2592e2e80fb3SKonstantin Belousov
2593037c0994SDoug Moore new_entry = vm_map_entry_clone(map, entry);
2594df8bae1dSRodney W. Grimes
25954766eba1SDoug Moore /*
25964766eba1SDoug Moore * Split off the back portion. Insert the new entry AFTER this one,
25974766eba1SDoug Moore * so that this entry has the specified ending address.
25984766eba1SDoug Moore */
2599e2e80fb3SKonstantin Belousov new_entry->start = endaddr;
26009f701172SKonstantin Belousov vm_map_entry_link(map, new_entry);
2601e2e80fb3SKonstantin Belousov
2602e2e80fb3SKonstantin Belousov return (KERN_SUCCESS);
2603c0877f10SJohn Dyson }
2604df8bae1dSRodney W. Grimes
2605df8bae1dSRodney W. Grimes /*
2606df8bae1dSRodney W. Grimes * vm_map_submap: [ kernel use only ]
2607df8bae1dSRodney W. Grimes *
2608df8bae1dSRodney W. Grimes * Mark the given range as handled by a subordinate map.
2609df8bae1dSRodney W. Grimes *
2610df8bae1dSRodney W. Grimes * This range must have been created with vm_map_find,
2611df8bae1dSRodney W. Grimes * and no other operations may have been performed on this
2612df8bae1dSRodney W. Grimes * range prior to calling vm_map_submap.
2613df8bae1dSRodney W. Grimes *
2614df8bae1dSRodney W. Grimes * Only a limited number of operations can be performed
2615df8bae1dSRodney W. Grimes * within this rage after calling vm_map_submap:
2616df8bae1dSRodney W. Grimes * vm_fault
2617df8bae1dSRodney W. Grimes * [Don't try vm_map_copy!]
2618df8bae1dSRodney W. Grimes *
2619df8bae1dSRodney W. Grimes * To remove a submapping, one must first remove the
2620df8bae1dSRodney W. Grimes * range from the superior map, and then destroy the
2621df8bae1dSRodney W. Grimes * submap (if desired). [Better yet, don't try it.]
2622df8bae1dSRodney W. Grimes */
2623df8bae1dSRodney W. Grimes int
vm_map_submap(vm_map_t map,vm_offset_t start,vm_offset_t end,vm_map_t submap)26241b40f8c0SMatthew Dillon vm_map_submap(
26251b40f8c0SMatthew Dillon vm_map_t map,
26261b40f8c0SMatthew Dillon vm_offset_t start,
26271b40f8c0SMatthew Dillon vm_offset_t end,
26281b40f8c0SMatthew Dillon vm_map_t submap)
2629df8bae1dSRodney W. Grimes {
2630df8bae1dSRodney W. Grimes vm_map_entry_t entry;
2631fa50a355SKonstantin Belousov int result;
2632fa50a355SKonstantin Belousov
2633fa50a355SKonstantin Belousov result = KERN_INVALID_ARGUMENT;
2634fa50a355SKonstantin Belousov
2635fa50a355SKonstantin Belousov vm_map_lock(submap);
2636fa50a355SKonstantin Belousov submap->flags |= MAP_IS_SUB_MAP;
2637fa50a355SKonstantin Belousov vm_map_unlock(submap);
2638df8bae1dSRodney W. Grimes
2639df8bae1dSRodney W. Grimes vm_map_lock(map);
2640df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end);
2641e6bd3a81SMark Johnston if (vm_map_lookup_entry(map, start, &entry) && entry->end >= end &&
2642e6bd3a81SMark Johnston (entry->eflags & MAP_ENTRY_COW) == 0 &&
2643e6bd3a81SMark Johnston entry->object.vm_object == NULL) {
2644e2e80fb3SKonstantin Belousov result = vm_map_clip_start(map, entry, start);
2645e2e80fb3SKonstantin Belousov if (result != KERN_SUCCESS)
2646e2e80fb3SKonstantin Belousov goto unlock;
2647e2e80fb3SKonstantin Belousov result = vm_map_clip_end(map, entry, end);
2648e2e80fb3SKonstantin Belousov if (result != KERN_SUCCESS)
2649e2e80fb3SKonstantin Belousov goto unlock;
26502d8acc0fSJohn Dyson entry->object.sub_map = submap;
2651afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2652df8bae1dSRodney W. Grimes result = KERN_SUCCESS;
2653df8bae1dSRodney W. Grimes }
2654e2e80fb3SKonstantin Belousov unlock:
2655df8bae1dSRodney W. Grimes vm_map_unlock(map);
2656df8bae1dSRodney W. Grimes
2657fa50a355SKonstantin Belousov if (result != KERN_SUCCESS) {
2658fa50a355SKonstantin Belousov vm_map_lock(submap);
2659fa50a355SKonstantin Belousov submap->flags &= ~MAP_IS_SUB_MAP;
2660fa50a355SKonstantin Belousov vm_map_unlock(submap);
2661fa50a355SKonstantin Belousov }
2662df8bae1dSRodney W. Grimes return (result);
2663df8bae1dSRodney W. Grimes }
2664df8bae1dSRodney W. Grimes
2665df8bae1dSRodney W. Grimes /*
2666dd05fa19SAlan Cox * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
26671f78f902SAlan Cox */
26681f78f902SAlan Cox #define MAX_INIT_PT 96
26691f78f902SAlan Cox
26701f78f902SAlan Cox /*
26710551c08dSAlan Cox * vm_map_pmap_enter:
26720551c08dSAlan Cox *
2673dd05fa19SAlan Cox * Preload the specified map's pmap with mappings to the specified
2674dd05fa19SAlan Cox * object's memory-resident pages. No further physical pages are
2675dd05fa19SAlan Cox * allocated, and no further virtual pages are retrieved from secondary
2676dd05fa19SAlan Cox * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a
2677dd05fa19SAlan Cox * limited number of page mappings are created at the low-end of the
2678dd05fa19SAlan Cox * specified address range. (For this purpose, a superpage mapping
2679dd05fa19SAlan Cox * counts as one page mapping.) Otherwise, all resident pages within
26803453bca8SAlan Cox * the specified address range are mapped.
26810551c08dSAlan Cox */
2682077ec27cSAlan Cox static void
vm_map_pmap_enter(vm_map_t map,vm_offset_t addr,vm_prot_t prot,vm_object_t object,vm_pindex_t pindex,vm_size_t size,int flags)26834da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
26840551c08dSAlan Cox vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
26850551c08dSAlan Cox {
26868fece8c3SAlan Cox vm_offset_t start;
2687ce142d9eSAlan Cox vm_page_t p, p_start;
2688dd05fa19SAlan Cox vm_pindex_t mask, psize, threshold, tmpidx;
26893e00c11aSAlan Cox int psind;
26900551c08dSAlan Cox
2691ba8bca61SAlan Cox if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
26921f78f902SAlan Cox return;
26939af6d512SAttilio Rao if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
269489f6b863SAttilio Rao VM_OBJECT_WLOCK(object);
269501381811SJohn Baldwin if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
26969af6d512SAttilio Rao pmap_object_init_pt(map->pmap, addr, object, pindex,
26979af6d512SAttilio Rao size);
26989af6d512SAttilio Rao VM_OBJECT_WUNLOCK(object);
26999af6d512SAttilio Rao return;
27009af6d512SAttilio Rao }
27019af6d512SAttilio Rao VM_OBJECT_LOCK_DOWNGRADE(object);
2702886b9021SJeff Roberson } else
2703886b9021SJeff Roberson VM_OBJECT_RLOCK(object);
27041f78f902SAlan Cox
27051f78f902SAlan Cox psize = atop(size);
27061f78f902SAlan Cox if (psize + pindex > object->size) {
2707ed2f945aSMark Johnston if (pindex >= object->size) {
27089af6d512SAttilio Rao VM_OBJECT_RUNLOCK(object);
27099af6d512SAttilio Rao return;
27109af6d512SAttilio Rao }
27111f78f902SAlan Cox psize = object->size - pindex;
27121f78f902SAlan Cox }
27131f78f902SAlan Cox
2714ce142d9eSAlan Cox start = 0;
2715ce142d9eSAlan Cox p_start = NULL;
2716dd05fa19SAlan Cox threshold = MAX_INIT_PT;
27171f78f902SAlan Cox
2718b382c10aSKonstantin Belousov p = vm_page_find_least(object, pindex);
27191f78f902SAlan Cox /*
27201f78f902SAlan Cox * Assert: the variable p is either (1) the page with the
27211f78f902SAlan Cox * least pindex greater than or equal to the parameter pindex
27221f78f902SAlan Cox * or (2) NULL.
27231f78f902SAlan Cox */
27241f78f902SAlan Cox for (;
27251f78f902SAlan Cox p != NULL && (tmpidx = p->pindex - pindex) < psize;
27261f78f902SAlan Cox p = TAILQ_NEXT(p, listq)) {
27271f78f902SAlan Cox /*
27281f78f902SAlan Cox * don't allow an madvise to blow away our really
27291f78f902SAlan Cox * free pages allocating pv entries.
27301f78f902SAlan Cox */
2731dd05fa19SAlan Cox if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2732e2068d0bSJeff Roberson vm_page_count_severe()) ||
2733dd05fa19SAlan Cox ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2734dd05fa19SAlan Cox tmpidx >= threshold)) {
2735379fb642SAlan Cox psize = tmpidx;
27361f78f902SAlan Cox break;
27371f78f902SAlan Cox }
27380012f373SJeff Roberson if (vm_page_all_valid(p)) {
2739ce142d9eSAlan Cox if (p_start == NULL) {
2740ce142d9eSAlan Cox start = addr + ptoa(tmpidx);
2741ce142d9eSAlan Cox p_start = p;
2742ce142d9eSAlan Cox }
2743dd05fa19SAlan Cox /* Jump ahead if a superpage mapping is possible. */
27443e00c11aSAlan Cox for (psind = p->psind; psind > 0; psind--) {
27453e00c11aSAlan Cox if (((addr + ptoa(tmpidx)) &
27463e00c11aSAlan Cox (pagesizes[psind] - 1)) == 0) {
27473e00c11aSAlan Cox mask = atop(pagesizes[psind]) - 1;
2748dd05fa19SAlan Cox if (tmpidx + mask < psize &&
27493e00c11aSAlan Cox vm_page_ps_test(p, psind,
27503e00c11aSAlan Cox PS_ALL_VALID, NULL)) {
2751dd05fa19SAlan Cox p += mask;
2752dd05fa19SAlan Cox threshold += mask;
27533e00c11aSAlan Cox break;
27543e00c11aSAlan Cox }
2755dd05fa19SAlan Cox }
2756dd05fa19SAlan Cox }
27577bfda801SAlan Cox } else if (p_start != NULL) {
2758cf4682aeSAlan Cox pmap_enter_object(map->pmap, start, addr +
2759cf4682aeSAlan Cox ptoa(tmpidx), p_start, prot);
2760cf4682aeSAlan Cox p_start = NULL;
2761cf4682aeSAlan Cox }
2762cf4682aeSAlan Cox }
2763c46b90e9SAlan Cox if (p_start != NULL)
2764379fb642SAlan Cox pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2765379fb642SAlan Cox p_start, prot);
27669af6d512SAttilio Rao VM_OBJECT_RUNLOCK(object);
27670551c08dSAlan Cox }
27680551c08dSAlan Cox
276979169929SKonstantin Belousov static void
vm_map_protect_guard(vm_map_entry_t entry,vm_prot_t new_prot,vm_prot_t new_maxprot,int flags)277079169929SKonstantin Belousov vm_map_protect_guard(vm_map_entry_t entry, vm_prot_t new_prot,
277179169929SKonstantin Belousov vm_prot_t new_maxprot, int flags)
277279169929SKonstantin Belousov {
277355be6be1SKonstantin Belousov vm_prot_t old_prot;
277455be6be1SKonstantin Belousov
277579169929SKonstantin Belousov MPASS((entry->eflags & MAP_ENTRY_GUARD) != 0);
277655be6be1SKonstantin Belousov if ((entry->eflags & (MAP_ENTRY_STACK_GAP_UP |
277755be6be1SKonstantin Belousov MAP_ENTRY_STACK_GAP_DN)) == 0)
277855be6be1SKonstantin Belousov return;
277955be6be1SKonstantin Belousov
278055be6be1SKonstantin Belousov old_prot = PROT_EXTRACT(entry->offset);
278155be6be1SKonstantin Belousov if ((flags & VM_MAP_PROTECT_SET_MAXPROT) != 0) {
278255be6be1SKonstantin Belousov entry->offset = PROT_MAX(new_maxprot) |
278355be6be1SKonstantin Belousov (new_maxprot & old_prot);
278455be6be1SKonstantin Belousov }
278555be6be1SKonstantin Belousov if ((flags & VM_MAP_PROTECT_SET_PROT) != 0) {
278655be6be1SKonstantin Belousov entry->offset = new_prot | PROT_MAX(
278755be6be1SKonstantin Belousov PROT_MAX_EXTRACT(entry->offset));
278855be6be1SKonstantin Belousov }
278979169929SKonstantin Belousov }
279079169929SKonstantin Belousov
27910551c08dSAlan Cox /*
2792df8bae1dSRodney W. Grimes * vm_map_protect:
2793df8bae1dSRodney W. Grimes *
27940659df6fSKonstantin Belousov * Sets the protection and/or the maximum protection of the
27950659df6fSKonstantin Belousov * specified address region in the target map.
2796df8bae1dSRodney W. Grimes */
2797df8bae1dSRodney W. Grimes int
vm_map_protect(vm_map_t map,vm_offset_t start,vm_offset_t end,vm_prot_t new_prot,vm_prot_t new_maxprot,int flags)2798b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
27990659df6fSKonstantin Belousov vm_prot_t new_prot, vm_prot_t new_maxprot, int flags)
2800df8bae1dSRodney W. Grimes {
28012767c9f3SDoug Moore vm_map_entry_t entry, first_entry, in_tran, prev_entry;
28023364c323SKonstantin Belousov vm_object_t obj;
2803ef694c1aSEdward Tomasz Napierala struct ucred *cred;
280490049eabSKonstantin Belousov vm_offset_t orig_start;
280555be6be1SKonstantin Belousov vm_prot_t check_prot, max_prot, old_prot;
2806a72dce34SDoug Moore int rv;
2807df8bae1dSRodney W. Grimes
280879e9451fSKonstantin Belousov if (start == end)
280979e9451fSKonstantin Belousov return (KERN_SUCCESS);
281079e9451fSKonstantin Belousov
28110fb6aae7SKonstantin Belousov if (CONTAINS_BITS(flags, VM_MAP_PROTECT_SET_PROT |
28120fb6aae7SKonstantin Belousov VM_MAP_PROTECT_SET_MAXPROT) &&
28130fb6aae7SKonstantin Belousov !CONTAINS_BITS(new_maxprot, new_prot))
28140659df6fSKonstantin Belousov return (KERN_OUT_OF_BOUNDS);
28150659df6fSKonstantin Belousov
281690049eabSKonstantin Belousov orig_start = start;
281719f5d9f2SKonstantin Belousov again:
281819f5d9f2SKonstantin Belousov in_tran = NULL;
281990049eabSKonstantin Belousov start = orig_start;
2820df8bae1dSRodney W. Grimes vm_map_lock(map);
2821df8bae1dSRodney W. Grimes
28220659df6fSKonstantin Belousov if ((map->flags & MAP_WXORX) != 0 &&
28230659df6fSKonstantin Belousov (flags & VM_MAP_PROTECT_SET_PROT) != 0 &&
28240fb6aae7SKonstantin Belousov CONTAINS_BITS(new_prot, VM_PROT_WRITE | VM_PROT_EXECUTE)) {
28252e1c94aaSKonstantin Belousov vm_map_unlock(map);
28262e1c94aaSKonstantin Belousov return (KERN_PROTECTION_FAILURE);
28272e1c94aaSKonstantin Belousov }
28282e1c94aaSKonstantin Belousov
2829e1cb9d37SMark Johnston /*
2830e1cb9d37SMark Johnston * Ensure that we are not concurrently wiring pages. vm_map_wire() may
2831e1cb9d37SMark Johnston * need to fault pages into the map and will drop the map lock while
2832e1cb9d37SMark Johnston * doing so, and the VM object may end up in an inconsistent state if we
2833e1cb9d37SMark Johnston * update the protection on the map entry in between faults.
2834e1cb9d37SMark Johnston */
2835e1cb9d37SMark Johnston vm_map_wait_busy(map);
2836e1cb9d37SMark Johnston
2837df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end);
2838df8bae1dSRodney W. Grimes
28392767c9f3SDoug Moore if (!vm_map_lookup_entry(map, start, &first_entry))
28402767c9f3SDoug Moore first_entry = vm_map_entry_succ(first_entry);
2841df8bae1dSRodney W. Grimes
284290049eabSKonstantin Belousov if ((flags & VM_MAP_PROTECT_GROWSDOWN) != 0 &&
284390049eabSKonstantin Belousov (first_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0) {
284490049eabSKonstantin Belousov /*
284590049eabSKonstantin Belousov * Handle Linux's PROT_GROWSDOWN flag.
284690049eabSKonstantin Belousov * It means that protection is applied down to the
284790049eabSKonstantin Belousov * whole stack, including the specified range of the
284890049eabSKonstantin Belousov * mapped region, and the grow down region (AKA
284990049eabSKonstantin Belousov * guard).
285090049eabSKonstantin Belousov */
285190049eabSKonstantin Belousov while (!CONTAINS_BITS(first_entry->eflags,
285290049eabSKonstantin Belousov MAP_ENTRY_GUARD | MAP_ENTRY_STACK_GAP_DN) &&
285390049eabSKonstantin Belousov first_entry != vm_map_entry_first(map))
285490049eabSKonstantin Belousov first_entry = vm_map_entry_pred(first_entry);
285590049eabSKonstantin Belousov start = first_entry->start;
285690049eabSKonstantin Belousov }
285790049eabSKonstantin Belousov
2858df8bae1dSRodney W. Grimes /*
28590d94caffSDavid Greenman * Make a first pass to check for protection violations.
2860df8bae1dSRodney W. Grimes */
28610fb6aae7SKonstantin Belousov check_prot = 0;
28620fb6aae7SKonstantin Belousov if ((flags & VM_MAP_PROTECT_SET_PROT) != 0)
28630fb6aae7SKonstantin Belousov check_prot |= new_prot;
28640fb6aae7SKonstantin Belousov if ((flags & VM_MAP_PROTECT_SET_MAXPROT) != 0)
28650fb6aae7SKonstantin Belousov check_prot |= new_maxprot;
28662767c9f3SDoug Moore for (entry = first_entry; entry->start < end;
28672767c9f3SDoug Moore entry = vm_map_entry_succ(entry)) {
28682767c9f3SDoug Moore if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
2869a1f6d91cSDavid Greenman vm_map_unlock(map);
2870df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT);
2871a1f6d91cSDavid Greenman }
287255be6be1SKonstantin Belousov if ((entry->eflags & (MAP_ENTRY_GUARD |
287355be6be1SKonstantin Belousov MAP_ENTRY_STACK_GAP_DN | MAP_ENTRY_STACK_GAP_UP)) ==
287455be6be1SKonstantin Belousov MAP_ENTRY_GUARD)
287579169929SKonstantin Belousov continue;
287655be6be1SKonstantin Belousov max_prot = (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
287755be6be1SKonstantin Belousov MAP_ENTRY_STACK_GAP_UP)) != 0 ?
287855be6be1SKonstantin Belousov PROT_MAX_EXTRACT(entry->offset) : entry->max_protection;
287955be6be1SKonstantin Belousov if (!CONTAINS_BITS(max_prot, check_prot)) {
2880df8bae1dSRodney W. Grimes vm_map_unlock(map);
2881df8bae1dSRodney W. Grimes return (KERN_PROTECTION_FAILURE);
2882df8bae1dSRodney W. Grimes }
28832767c9f3SDoug Moore if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0)
28842767c9f3SDoug Moore in_tran = entry;
288519f5d9f2SKonstantin Belousov }
288619f5d9f2SKonstantin Belousov
288719f5d9f2SKonstantin Belousov /*
2888bdb90e76SDoug Moore * Postpone the operation until all in-transition map entries have
2889bdb90e76SDoug Moore * stabilized. An in-transition entry might already have its pages
2890bdb90e76SDoug Moore * wired and wired_count incremented, but not yet have its
2891bdb90e76SDoug Moore * MAP_ENTRY_USER_WIRED flag set. In which case, we would fail to call
2892bdb90e76SDoug Moore * vm_fault_copy_entry() in the final loop below.
289319f5d9f2SKonstantin Belousov */
289419f5d9f2SKonstantin Belousov if (in_tran != NULL) {
289519f5d9f2SKonstantin Belousov in_tran->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
289619f5d9f2SKonstantin Belousov vm_map_unlock_and_wait(map, 0);
289719f5d9f2SKonstantin Belousov goto again;
2898df8bae1dSRodney W. Grimes }
2899df8bae1dSRodney W. Grimes
29003364c323SKonstantin Belousov /*
2901a72dce34SDoug Moore * Before changing the protections, try to reserve swap space for any
2902a72dce34SDoug Moore * private (i.e., copy-on-write) mappings that are transitioning from
2903a72dce34SDoug Moore * read-only to read/write access. If a reservation fails, break out
2904a72dce34SDoug Moore * of this loop early and let the next loop simplify the entries, since
2905a72dce34SDoug Moore * some may now be mergeable.
29063364c323SKonstantin Belousov */
2907e2e80fb3SKonstantin Belousov rv = vm_map_clip_start(map, first_entry, start);
2908e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS) {
2909e2e80fb3SKonstantin Belousov vm_map_unlock(map);
2910e2e80fb3SKonstantin Belousov return (rv);
2911e2e80fb3SKonstantin Belousov }
29122767c9f3SDoug Moore for (entry = first_entry; entry->start < end;
29132767c9f3SDoug Moore entry = vm_map_entry_succ(entry)) {
2914e2e80fb3SKonstantin Belousov rv = vm_map_clip_end(map, entry, end);
2915e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS) {
2916e2e80fb3SKonstantin Belousov vm_map_unlock(map);
2917e2e80fb3SKonstantin Belousov return (rv);
2918e2e80fb3SKonstantin Belousov }
29193364c323SKonstantin Belousov
29200659df6fSKonstantin Belousov if ((flags & VM_MAP_PROTECT_SET_PROT) == 0 ||
29212767c9f3SDoug Moore ((new_prot & ~entry->protection) & VM_PROT_WRITE) == 0 ||
29222767c9f3SDoug Moore ENTRY_CHARGED(entry) ||
29230659df6fSKonstantin Belousov (entry->eflags & MAP_ENTRY_GUARD) != 0)
29243364c323SKonstantin Belousov continue;
29253364c323SKonstantin Belousov
2926ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred;
29272767c9f3SDoug Moore obj = entry->object.vm_object;
29283364c323SKonstantin Belousov
29292767c9f3SDoug Moore if (obj == NULL ||
29302767c9f3SDoug Moore (entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0) {
29312767c9f3SDoug Moore if (!swap_reserve(entry->end - entry->start)) {
2932a72dce34SDoug Moore rv = KERN_RESOURCE_SHORTAGE;
29332767c9f3SDoug Moore end = entry->end;
2934a72dce34SDoug Moore break;
29353364c323SKonstantin Belousov }
2936ef694c1aSEdward Tomasz Napierala crhold(cred);
29372767c9f3SDoug Moore entry->cred = cred;
29383364c323SKonstantin Belousov continue;
29393364c323SKonstantin Belousov }
29403364c323SKonstantin Belousov
294189f6b863SAttilio Rao VM_OBJECT_WLOCK(obj);
29420cb2610eSMark Johnston if ((obj->flags & OBJ_SWAP) == 0) {
294389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj);
29443364c323SKonstantin Belousov continue;
29453364c323SKonstantin Belousov }
29463364c323SKonstantin Belousov
29473364c323SKonstantin Belousov /*
29483364c323SKonstantin Belousov * Charge for the whole object allocation now, since
29493364c323SKonstantin Belousov * we cannot distinguish between non-charged and
29503364c323SKonstantin Belousov * charged clipped mapping of the same object later.
29513364c323SKonstantin Belousov */
29523364c323SKonstantin Belousov KASSERT(obj->charge == 0,
29533d95614fSKonstantin Belousov ("vm_map_protect: object %p overcharged (entry %p)",
29542767c9f3SDoug Moore obj, entry));
29553364c323SKonstantin Belousov if (!swap_reserve(ptoa(obj->size))) {
295689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj);
2957a72dce34SDoug Moore rv = KERN_RESOURCE_SHORTAGE;
29582767c9f3SDoug Moore end = entry->end;
2959a72dce34SDoug Moore break;
29603364c323SKonstantin Belousov }
29613364c323SKonstantin Belousov
2962ef694c1aSEdward Tomasz Napierala crhold(cred);
2963ef694c1aSEdward Tomasz Napierala obj->cred = cred;
29643364c323SKonstantin Belousov obj->charge = ptoa(obj->size);
296589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj);
29663364c323SKonstantin Belousov }
29673364c323SKonstantin Belousov
2968df8bae1dSRodney W. Grimes /*
2969a72dce34SDoug Moore * If enough swap space was available, go back and fix up protections.
2970a72dce34SDoug Moore * Otherwise, just simplify entries, since some may have been modified.
2971a72dce34SDoug Moore * [Note that clipping is not necessary the second time.]
2972df8bae1dSRodney W. Grimes */
29732767c9f3SDoug Moore for (prev_entry = vm_map_entry_pred(first_entry), entry = first_entry;
29742767c9f3SDoug Moore entry->start < end;
29752767c9f3SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry),
29762767c9f3SDoug Moore prev_entry = entry, entry = vm_map_entry_succ(entry)) {
297779169929SKonstantin Belousov if (rv != KERN_SUCCESS)
297819bd0d9cSKonstantin Belousov continue;
297919bd0d9cSKonstantin Belousov
298079169929SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
298179169929SKonstantin Belousov vm_map_protect_guard(entry, new_prot, new_maxprot,
298279169929SKonstantin Belousov flags);
298379169929SKonstantin Belousov continue;
298479169929SKonstantin Belousov }
298579169929SKonstantin Belousov
29862767c9f3SDoug Moore old_prot = entry->protection;
2987210a6886SKonstantin Belousov
29880659df6fSKonstantin Belousov if ((flags & VM_MAP_PROTECT_SET_MAXPROT) != 0) {
29890659df6fSKonstantin Belousov entry->max_protection = new_maxprot;
29900659df6fSKonstantin Belousov entry->protection = new_maxprot & old_prot;
29910659df6fSKonstantin Belousov }
29920659df6fSKonstantin Belousov if ((flags & VM_MAP_PROTECT_SET_PROT) != 0)
29932767c9f3SDoug Moore entry->protection = new_prot;
2994df8bae1dSRodney W. Grimes
2995dd006a1bSAlan Cox /*
2996dd006a1bSAlan Cox * For user wired map entries, the normal lazy evaluation of
2997dd006a1bSAlan Cox * write access upgrades through soft page faults is
2998dd006a1bSAlan Cox * undesirable. Instead, immediately copy any pages that are
2999dd006a1bSAlan Cox * copy-on-write and enable write access in the physical map.
3000dd006a1bSAlan Cox */
30012767c9f3SDoug Moore if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
30022767c9f3SDoug Moore (entry->protection & VM_PROT_WRITE) != 0 &&
30035930251aSKonstantin Belousov (old_prot & VM_PROT_WRITE) == 0)
30042767c9f3SDoug Moore vm_fault_copy_entry(map, map, entry, entry, NULL);
3005210a6886SKonstantin Belousov
3006df8bae1dSRodney W. Grimes /*
30072fafce9eSAlan Cox * When restricting access, update the physical map. Worry
30082fafce9eSAlan Cox * about copy-on-write here.
3009df8bae1dSRodney W. Grimes */
30102767c9f3SDoug Moore if ((old_prot & ~entry->protection) != 0) {
3011afa07f7eSJohn Dyson #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
3012df8bae1dSRodney W. Grimes VM_PROT_ALL)
30132767c9f3SDoug Moore pmap_protect(map->pmap, entry->start,
30142767c9f3SDoug Moore entry->end,
30152767c9f3SDoug Moore entry->protection & MASK(entry));
3016df8bae1dSRodney W. Grimes #undef MASK
3017df8bae1dSRodney W. Grimes }
3018df8bae1dSRodney W. Grimes }
30192767c9f3SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
3020df8bae1dSRodney W. Grimes vm_map_unlock(map);
3021a72dce34SDoug Moore return (rv);
3022df8bae1dSRodney W. Grimes }
3023df8bae1dSRodney W. Grimes
3024df8bae1dSRodney W. Grimes /*
3025867a482dSJohn Dyson * vm_map_madvise:
3026867a482dSJohn Dyson *
3027867a482dSJohn Dyson * This routine traverses a processes map handling the madvise
3028f7fc307aSAlan Cox * system call. Advisories are classified as either those effecting
3029f7fc307aSAlan Cox * the vm_map_entry structure, or those effecting the underlying
3030f7fc307aSAlan Cox * objects.
3031867a482dSJohn Dyson */
3032b4309055SMatthew Dillon int
vm_map_madvise(vm_map_t map,vm_offset_t start,vm_offset_t end,int behav)30331b40f8c0SMatthew Dillon vm_map_madvise(
30341b40f8c0SMatthew Dillon vm_map_t map,
30351b40f8c0SMatthew Dillon vm_offset_t start,
30361b40f8c0SMatthew Dillon vm_offset_t end,
30371b40f8c0SMatthew Dillon int behav)
3038867a482dSJohn Dyson {
30392767c9f3SDoug Moore vm_map_entry_t entry, prev_entry;
3040e2e80fb3SKonstantin Belousov int rv;
30413e7cb27cSAlan Cox bool modify_map;
3042867a482dSJohn Dyson
3043b4309055SMatthew Dillon /*
3044b4309055SMatthew Dillon * Some madvise calls directly modify the vm_map_entry, in which case
3045b4309055SMatthew Dillon * we need to use an exclusive lock on the map and we need to perform
3046b4309055SMatthew Dillon * various clipping operations. Otherwise we only need a read-lock
3047b4309055SMatthew Dillon * on the map.
3048b4309055SMatthew Dillon */
3049b4309055SMatthew Dillon switch(behav) {
3050b4309055SMatthew Dillon case MADV_NORMAL:
3051b4309055SMatthew Dillon case MADV_SEQUENTIAL:
3052b4309055SMatthew Dillon case MADV_RANDOM:
30534f79d873SMatthew Dillon case MADV_NOSYNC:
30544f79d873SMatthew Dillon case MADV_AUTOSYNC:
30559730a5daSPaul Saab case MADV_NOCORE:
30569730a5daSPaul Saab case MADV_CORE:
305779e9451fSKonstantin Belousov if (start == end)
30583e7cb27cSAlan Cox return (0);
30593e7cb27cSAlan Cox modify_map = true;
3060867a482dSJohn Dyson vm_map_lock(map);
3061b4309055SMatthew Dillon break;
3062b4309055SMatthew Dillon case MADV_WILLNEED:
3063b4309055SMatthew Dillon case MADV_DONTNEED:
3064b4309055SMatthew Dillon case MADV_FREE:
306579e9451fSKonstantin Belousov if (start == end)
30663e7cb27cSAlan Cox return (0);
30673e7cb27cSAlan Cox modify_map = false;
3068f7fc307aSAlan Cox vm_map_lock_read(map);
3069b4309055SMatthew Dillon break;
3070b4309055SMatthew Dillon default:
30713e7cb27cSAlan Cox return (EINVAL);
3072b4309055SMatthew Dillon }
3073b4309055SMatthew Dillon
3074b4309055SMatthew Dillon /*
3075b4309055SMatthew Dillon * Locate starting entry and clip if necessary.
3076b4309055SMatthew Dillon */
3077867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end);
3078867a482dSJohn Dyson
3079f7fc307aSAlan Cox if (modify_map) {
3080f7fc307aSAlan Cox /*
3081f7fc307aSAlan Cox * madvise behaviors that are implemented in the vm_map_entry.
3082f7fc307aSAlan Cox *
3083f7fc307aSAlan Cox * We clip the vm_map_entry so that behavioral changes are
3084f7fc307aSAlan Cox * limited to the specified address range.
3085f7fc307aSAlan Cox */
3086e2e80fb3SKonstantin Belousov rv = vm_map_lookup_clip_start(map, start, &entry, &prev_entry);
3087e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS) {
3088e2e80fb3SKonstantin Belousov vm_map_unlock(map);
3089e2e80fb3SKonstantin Belousov return (vm_mmap_to_errno(rv));
3090e2e80fb3SKonstantin Belousov }
3091e2e80fb3SKonstantin Belousov
3092e2e80fb3SKonstantin Belousov for (; entry->start < end; prev_entry = entry,
3093e2e80fb3SKonstantin Belousov entry = vm_map_entry_succ(entry)) {
30942767c9f3SDoug Moore if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
3095867a482dSJohn Dyson continue;
3096fed9a903SJohn Dyson
3097e2e80fb3SKonstantin Belousov rv = vm_map_clip_end(map, entry, end);
3098e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS) {
3099e2e80fb3SKonstantin Belousov vm_map_unlock(map);
3100e2e80fb3SKonstantin Belousov return (vm_mmap_to_errno(rv));
3101e2e80fb3SKonstantin Belousov }
3102fed9a903SJohn Dyson
3103f7fc307aSAlan Cox switch (behav) {
3104867a482dSJohn Dyson case MADV_NORMAL:
31052767c9f3SDoug Moore vm_map_entry_set_behavior(entry,
31062767c9f3SDoug Moore MAP_ENTRY_BEHAV_NORMAL);
3107867a482dSJohn Dyson break;
3108867a482dSJohn Dyson case MADV_SEQUENTIAL:
31092767c9f3SDoug Moore vm_map_entry_set_behavior(entry,
31102767c9f3SDoug Moore MAP_ENTRY_BEHAV_SEQUENTIAL);
3111867a482dSJohn Dyson break;
3112867a482dSJohn Dyson case MADV_RANDOM:
31132767c9f3SDoug Moore vm_map_entry_set_behavior(entry,
31142767c9f3SDoug Moore MAP_ENTRY_BEHAV_RANDOM);
3115867a482dSJohn Dyson break;
31164f79d873SMatthew Dillon case MADV_NOSYNC:
31172767c9f3SDoug Moore entry->eflags |= MAP_ENTRY_NOSYNC;
31184f79d873SMatthew Dillon break;
31194f79d873SMatthew Dillon case MADV_AUTOSYNC:
31202767c9f3SDoug Moore entry->eflags &= ~MAP_ENTRY_NOSYNC;
31214f79d873SMatthew Dillon break;
31229730a5daSPaul Saab case MADV_NOCORE:
31232767c9f3SDoug Moore entry->eflags |= MAP_ENTRY_NOCOREDUMP;
31249730a5daSPaul Saab break;
31259730a5daSPaul Saab case MADV_CORE:
31262767c9f3SDoug Moore entry->eflags &= ~MAP_ENTRY_NOCOREDUMP;
31279730a5daSPaul Saab break;
3128867a482dSJohn Dyson default:
3129867a482dSJohn Dyson break;
3130867a482dSJohn Dyson }
31312767c9f3SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
3132867a482dSJohn Dyson }
31332767c9f3SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
3134867a482dSJohn Dyson vm_map_unlock(map);
3135b4309055SMatthew Dillon } else {
313692a59946SJohn Baldwin vm_pindex_t pstart, pend;
3137f7fc307aSAlan Cox
3138f7fc307aSAlan Cox /*
3139f7fc307aSAlan Cox * madvise behaviors that are implemented in the underlying
3140f7fc307aSAlan Cox * vm_object.
3141f7fc307aSAlan Cox *
3142f7fc307aSAlan Cox * Since we don't clip the vm_map_entry, we have to clip
3143f7fc307aSAlan Cox * the vm_object pindex and count.
3144f7fc307aSAlan Cox */
3145c7b23459SDoug Moore if (!vm_map_lookup_entry(map, start, &entry))
3146c7b23459SDoug Moore entry = vm_map_entry_succ(entry);
31472767c9f3SDoug Moore for (; entry->start < end;
31482767c9f3SDoug Moore entry = vm_map_entry_succ(entry)) {
314951321f7cSAlan Cox vm_offset_t useEnd, useStart;
31505f99b57cSMatthew Dillon
3151c7180098SKonstantin Belousov if ((entry->eflags & (MAP_ENTRY_IS_SUB_MAP |
3152c7180098SKonstantin Belousov MAP_ENTRY_GUARD)) != 0)
3153f7fc307aSAlan Cox continue;
3154f7fc307aSAlan Cox
3155bf5661f4SKonstantin Belousov /*
3156bf5661f4SKonstantin Belousov * MADV_FREE would otherwise rewind time to
3157bf5661f4SKonstantin Belousov * the creation of the shadow object. Because
3158bf5661f4SKonstantin Belousov * we hold the VM map read-locked, neither the
3159bf5661f4SKonstantin Belousov * entry's object nor the presence of a
3160bf5661f4SKonstantin Belousov * backing object can change.
3161bf5661f4SKonstantin Belousov */
3162bf5661f4SKonstantin Belousov if (behav == MADV_FREE &&
31632767c9f3SDoug Moore entry->object.vm_object != NULL &&
31642767c9f3SDoug Moore entry->object.vm_object->backing_object != NULL)
3165bf5661f4SKonstantin Belousov continue;
3166bf5661f4SKonstantin Belousov
31672767c9f3SDoug Moore pstart = OFF_TO_IDX(entry->offset);
31682767c9f3SDoug Moore pend = pstart + atop(entry->end - entry->start);
31692767c9f3SDoug Moore useStart = entry->start;
31702767c9f3SDoug Moore useEnd = entry->end;
3171f7fc307aSAlan Cox
31722767c9f3SDoug Moore if (entry->start < start) {
31732767c9f3SDoug Moore pstart += atop(start - entry->start);
31745f99b57cSMatthew Dillon useStart = start;
3175f7fc307aSAlan Cox }
31762767c9f3SDoug Moore if (entry->end > end) {
31772767c9f3SDoug Moore pend -= atop(entry->end - end);
317851321f7cSAlan Cox useEnd = end;
317951321f7cSAlan Cox }
3180f7fc307aSAlan Cox
318192a59946SJohn Baldwin if (pstart >= pend)
3182f7fc307aSAlan Cox continue;
3183f7fc307aSAlan Cox
318451321f7cSAlan Cox /*
318551321f7cSAlan Cox * Perform the pmap_advise() before clearing
318651321f7cSAlan Cox * PGA_REFERENCED in vm_page_advise(). Otherwise, a
318751321f7cSAlan Cox * concurrent pmap operation, such as pmap_remove(),
318851321f7cSAlan Cox * could clear a reference in the pmap and set
318951321f7cSAlan Cox * PGA_REFERENCED on the page before the pmap_advise()
319051321f7cSAlan Cox * had completed. Consequently, the page would appear
319151321f7cSAlan Cox * referenced based upon an old reference that
319251321f7cSAlan Cox * occurred before this pmap_advise() ran.
319351321f7cSAlan Cox */
319451321f7cSAlan Cox if (behav == MADV_DONTNEED || behav == MADV_FREE)
319551321f7cSAlan Cox pmap_advise(map->pmap, useStart, useEnd,
319651321f7cSAlan Cox behav);
319751321f7cSAlan Cox
31982767c9f3SDoug Moore vm_object_madvise(entry->object.vm_object, pstart,
319992a59946SJohn Baldwin pend, behav);
320054432196SKonstantin Belousov
320154432196SKonstantin Belousov /*
320254432196SKonstantin Belousov * Pre-populate paging structures in the
320354432196SKonstantin Belousov * WILLNEED case. For wired entries, the
320454432196SKonstantin Belousov * paging structures are already populated.
320554432196SKonstantin Belousov */
320654432196SKonstantin Belousov if (behav == MADV_WILLNEED &&
32072767c9f3SDoug Moore entry->wired_count == 0) {
32080551c08dSAlan Cox vm_map_pmap_enter(map,
32095f99b57cSMatthew Dillon useStart,
32102767c9f3SDoug Moore entry->protection,
32112767c9f3SDoug Moore entry->object.vm_object,
321292a59946SJohn Baldwin pstart,
321392a59946SJohn Baldwin ptoa(pend - pstart),
3214e3026983SMatthew Dillon MAP_PREFAULT_MADVISE
3215b4309055SMatthew Dillon );
3216f7fc307aSAlan Cox }
3217f7fc307aSAlan Cox }
3218f7fc307aSAlan Cox vm_map_unlock_read(map);
3219f7fc307aSAlan Cox }
3220b4309055SMatthew Dillon return (0);
3221867a482dSJohn Dyson }
3222867a482dSJohn Dyson
3223867a482dSJohn Dyson /*
3224df8bae1dSRodney W. Grimes * vm_map_inherit:
3225df8bae1dSRodney W. Grimes *
3226df8bae1dSRodney W. Grimes * Sets the inheritance of the specified address
3227df8bae1dSRodney W. Grimes * range in the target map. Inheritance
3228df8bae1dSRodney W. Grimes * affects how the map will be shared with
3229e2abaaaaSAlan Cox * child maps at the time of vmspace_fork.
3230df8bae1dSRodney W. Grimes */
3231df8bae1dSRodney W. Grimes int
vm_map_inherit(vm_map_t map,vm_offset_t start,vm_offset_t end,vm_inherit_t new_inheritance)3232b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
3233b9dcd593SBruce Evans vm_inherit_t new_inheritance)
3234df8bae1dSRodney W. Grimes {
3235e2e80fb3SKonstantin Belousov vm_map_entry_t entry, lentry, prev_entry, start_entry;
3236e2e80fb3SKonstantin Belousov int rv;
3237df8bae1dSRodney W. Grimes
3238df8bae1dSRodney W. Grimes switch (new_inheritance) {
3239df8bae1dSRodney W. Grimes case VM_INHERIT_NONE:
3240df8bae1dSRodney W. Grimes case VM_INHERIT_COPY:
3241df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE:
324278d7964bSXin LI case VM_INHERIT_ZERO:
3243df8bae1dSRodney W. Grimes break;
3244df8bae1dSRodney W. Grimes default:
3245df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT);
3246df8bae1dSRodney W. Grimes }
324779e9451fSKonstantin Belousov if (start == end)
324879e9451fSKonstantin Belousov return (KERN_SUCCESS);
3249df8bae1dSRodney W. Grimes vm_map_lock(map);
3250df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end);
3251e2e80fb3SKonstantin Belousov rv = vm_map_lookup_clip_start(map, start, &start_entry, &prev_entry);
3252e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
3253e2e80fb3SKonstantin Belousov goto unlock;
3254e2e80fb3SKonstantin Belousov if (vm_map_lookup_entry(map, end - 1, &lentry)) {
3255e2e80fb3SKonstantin Belousov rv = vm_map_clip_end(map, lentry, end);
3256e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
3257e2e80fb3SKonstantin Belousov goto unlock;
3258e2e80fb3SKonstantin Belousov }
3259e2e80fb3SKonstantin Belousov if (new_inheritance == VM_INHERIT_COPY) {
3260e2e80fb3SKonstantin Belousov for (entry = start_entry; entry->start < end;
326183704cc2SDoug Moore prev_entry = entry, entry = vm_map_entry_succ(entry)) {
3262e2e80fb3SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK)
3263e2e80fb3SKonstantin Belousov != 0) {
3264e2e80fb3SKonstantin Belousov rv = KERN_INVALID_ARGUMENT;
3265e2e80fb3SKonstantin Belousov goto unlock;
3266e2e80fb3SKonstantin Belousov }
3267e2e80fb3SKonstantin Belousov }
3268e2e80fb3SKonstantin Belousov }
3269e2e80fb3SKonstantin Belousov for (entry = start_entry; entry->start < end; prev_entry = entry,
3270e2e80fb3SKonstantin Belousov entry = vm_map_entry_succ(entry)) {
3271e2e80fb3SKonstantin Belousov KASSERT(entry->end <= end, ("non-clipped entry %p end %jx %jx",
3272e2e80fb3SKonstantin Belousov entry, (uintmax_t)entry->end, (uintmax_t)end));
327319bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
327419bd0d9cSKonstantin Belousov new_inheritance != VM_INHERIT_ZERO)
3275df8bae1dSRodney W. Grimes entry->inheritance = new_inheritance;
327683704cc2SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
3277df8bae1dSRodney W. Grimes }
327883704cc2SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
3279e2e80fb3SKonstantin Belousov unlock:
3280df8bae1dSRodney W. Grimes vm_map_unlock(map);
3281e2e80fb3SKonstantin Belousov return (rv);
3282df8bae1dSRodney W. Grimes }
3283df8bae1dSRodney W. Grimes
3284df8bae1dSRodney W. Grimes /*
3285312df2c1SDoug Moore * vm_map_entry_in_transition:
3286312df2c1SDoug Moore *
3287312df2c1SDoug Moore * Release the map lock, and sleep until the entry is no longer in
3288312df2c1SDoug Moore * transition. Awake and acquire the map lock. If the map changed while
3289312df2c1SDoug Moore * another held the lock, lookup a possibly-changed entry at or after the
3290312df2c1SDoug Moore * 'start' position of the old entry.
3291312df2c1SDoug Moore */
3292312df2c1SDoug Moore static vm_map_entry_t
vm_map_entry_in_transition(vm_map_t map,vm_offset_t in_start,vm_offset_t * io_end,bool holes_ok,vm_map_entry_t in_entry)3293312df2c1SDoug Moore vm_map_entry_in_transition(vm_map_t map, vm_offset_t in_start,
3294312df2c1SDoug Moore vm_offset_t *io_end, bool holes_ok, vm_map_entry_t in_entry)
3295312df2c1SDoug Moore {
3296312df2c1SDoug Moore vm_map_entry_t entry;
3297312df2c1SDoug Moore vm_offset_t start;
3298312df2c1SDoug Moore u_int last_timestamp;
3299312df2c1SDoug Moore
3300312df2c1SDoug Moore VM_MAP_ASSERT_LOCKED(map);
3301312df2c1SDoug Moore KASSERT((in_entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3302312df2c1SDoug Moore ("not in-tranition map entry %p", in_entry));
3303312df2c1SDoug Moore /*
3304312df2c1SDoug Moore * We have not yet clipped the entry.
3305312df2c1SDoug Moore */
3306312df2c1SDoug Moore start = MAX(in_start, in_entry->start);
3307312df2c1SDoug Moore in_entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3308312df2c1SDoug Moore last_timestamp = map->timestamp;
3309312df2c1SDoug Moore if (vm_map_unlock_and_wait(map, 0)) {
3310312df2c1SDoug Moore /*
3311312df2c1SDoug Moore * Allow interruption of user wiring/unwiring?
3312312df2c1SDoug Moore */
3313312df2c1SDoug Moore }
3314312df2c1SDoug Moore vm_map_lock(map);
3315312df2c1SDoug Moore if (last_timestamp + 1 == map->timestamp)
3316312df2c1SDoug Moore return (in_entry);
3317312df2c1SDoug Moore
3318312df2c1SDoug Moore /*
3319312df2c1SDoug Moore * Look again for the entry because the map was modified while it was
3320312df2c1SDoug Moore * unlocked. Specifically, the entry may have been clipped, merged, or
3321312df2c1SDoug Moore * deleted.
3322312df2c1SDoug Moore */
3323312df2c1SDoug Moore if (!vm_map_lookup_entry(map, start, &entry)) {
3324312df2c1SDoug Moore if (!holes_ok) {
3325312df2c1SDoug Moore *io_end = start;
3326312df2c1SDoug Moore return (NULL);
3327312df2c1SDoug Moore }
33287cdcf863SDoug Moore entry = vm_map_entry_succ(entry);
3329312df2c1SDoug Moore }
3330312df2c1SDoug Moore return (entry);
3331312df2c1SDoug Moore }
3332312df2c1SDoug Moore
3333312df2c1SDoug Moore /*
3334acd9a301SAlan Cox * vm_map_unwire:
3335acd9a301SAlan Cox *
3336e27e17b7SAlan Cox * Implements both kernel and user unwiring.
3337acd9a301SAlan Cox */
3338acd9a301SAlan Cox int
vm_map_unwire(vm_map_t map,vm_offset_t start,vm_offset_t end,int flags)3339acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
3340abd498aaSBruce M Simpson int flags)
3341acd9a301SAlan Cox {
334283704cc2SDoug Moore vm_map_entry_t entry, first_entry, next_entry, prev_entry;
3343acd9a301SAlan Cox int rv;
334483704cc2SDoug Moore bool holes_ok, need_wakeup, user_unwire;
3345acd9a301SAlan Cox
334679e9451fSKonstantin Belousov if (start == end)
334779e9451fSKonstantin Belousov return (KERN_SUCCESS);
33489a0cdf94SDoug Moore holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
33499a0cdf94SDoug Moore user_unwire = (flags & VM_MAP_WIRE_USER) != 0;
3350acd9a301SAlan Cox vm_map_lock(map);
3351acd9a301SAlan Cox VM_MAP_RANGE_CHECK(map, start, end);
3352d1d3f7e1SDoug Moore if (!vm_map_lookup_entry(map, start, &first_entry)) {
33539a0cdf94SDoug Moore if (holes_ok)
33547cdcf863SDoug Moore first_entry = vm_map_entry_succ(first_entry);
3355d1d3f7e1SDoug Moore else {
3356acd9a301SAlan Cox vm_map_unlock(map);
3357acd9a301SAlan Cox return (KERN_INVALID_ADDRESS);
3358acd9a301SAlan Cox }
3359abd498aaSBruce M Simpson }
3360d2860f22SDoug Moore rv = KERN_SUCCESS;
336183704cc2SDoug Moore for (entry = first_entry; entry->start < end; entry = next_entry) {
3362acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3363acd9a301SAlan Cox /*
3364acd9a301SAlan Cox * We have not yet clipped the entry.
3365acd9a301SAlan Cox */
336683704cc2SDoug Moore next_entry = vm_map_entry_in_transition(map, start,
336783704cc2SDoug Moore &end, holes_ok, entry);
336883704cc2SDoug Moore if (next_entry == NULL) {
336983704cc2SDoug Moore if (entry == first_entry) {
3370acd9a301SAlan Cox vm_map_unlock(map);
3371acd9a301SAlan Cox return (KERN_INVALID_ADDRESS);
3372acd9a301SAlan Cox }
3373acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS;
3374d2860f22SDoug Moore break;
3375acd9a301SAlan Cox }
337683704cc2SDoug Moore first_entry = (entry == first_entry) ?
337783704cc2SDoug Moore next_entry : NULL;
3378acd9a301SAlan Cox continue;
3379acd9a301SAlan Cox }
3380e2e80fb3SKonstantin Belousov rv = vm_map_clip_start(map, entry, start);
3381e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
3382e2e80fb3SKonstantin Belousov break;
3383e2e80fb3SKonstantin Belousov rv = vm_map_clip_end(map, entry, end);
3384e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
3385e2e80fb3SKonstantin Belousov break;
3386e2e80fb3SKonstantin Belousov
3387acd9a301SAlan Cox /*
3388acd9a301SAlan Cox * Mark the entry in case the map lock is released. (See
3389acd9a301SAlan Cox * above.)
3390acd9a301SAlan Cox */
3391ff3ae454SKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3392ff3ae454SKonstantin Belousov entry->wiring_thread == NULL,
3393ff3ae454SKonstantin Belousov ("owned map entry %p", entry));
3394acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION;
33950acea7dfSKonstantin Belousov entry->wiring_thread = curthread;
339683704cc2SDoug Moore next_entry = vm_map_entry_succ(entry);
3397acd9a301SAlan Cox /*
3398acd9a301SAlan Cox * Check the map for holes in the specified region.
33999a0cdf94SDoug Moore * If holes_ok, skip this check.
3400acd9a301SAlan Cox */
34019a0cdf94SDoug Moore if (!holes_ok &&
340283704cc2SDoug Moore entry->end < end && next_entry->start > entry->end) {
3403acd9a301SAlan Cox end = entry->end;
3404acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS;
3405d2860f22SDoug Moore break;
3406acd9a301SAlan Cox }
3407acd9a301SAlan Cox /*
34083ffbc0cdSAlan Cox * If system unwiring, require that the entry is system wired.
3409acd9a301SAlan Cox */
34100ada205eSBrian Feldman if (!user_unwire &&
34110ada205eSBrian Feldman vm_map_entry_system_wired_count(entry) == 0) {
3412acd9a301SAlan Cox end = entry->end;
3413acd9a301SAlan Cox rv = KERN_INVALID_ARGUMENT;
3414d2860f22SDoug Moore break;
3415acd9a301SAlan Cox }
3416acd9a301SAlan Cox }
34179a0cdf94SDoug Moore need_wakeup = false;
34189a0cdf94SDoug Moore if (first_entry == NULL &&
34199a0cdf94SDoug Moore !vm_map_lookup_entry(map, start, &first_entry)) {
34209a0cdf94SDoug Moore KASSERT(holes_ok, ("vm_map_unwire: lookup failed"));
342183704cc2SDoug Moore prev_entry = first_entry;
342283704cc2SDoug Moore entry = vm_map_entry_succ(first_entry);
342383704cc2SDoug Moore } else {
342483704cc2SDoug Moore prev_entry = vm_map_entry_pred(first_entry);
342583704cc2SDoug Moore entry = first_entry;
3426acd9a301SAlan Cox }
342783704cc2SDoug Moore for (; entry->start < end;
342883704cc2SDoug Moore prev_entry = entry, entry = vm_map_entry_succ(entry)) {
34290acea7dfSKonstantin Belousov /*
34309a0cdf94SDoug Moore * If holes_ok was specified, an empty
34310acea7dfSKonstantin Belousov * space in the unwired region could have been mapped
34320acea7dfSKonstantin Belousov * while the map lock was dropped for draining
34330acea7dfSKonstantin Belousov * MAP_ENTRY_IN_TRANSITION. Moreover, another thread
34340acea7dfSKonstantin Belousov * could be simultaneously wiring this new mapping
34350acea7dfSKonstantin Belousov * entry. Detect these cases and skip any entries
34360acea7dfSKonstantin Belousov * marked as in transition by us.
34370acea7dfSKonstantin Belousov */
34380acea7dfSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
34390acea7dfSKonstantin Belousov entry->wiring_thread != curthread) {
34409a0cdf94SDoug Moore KASSERT(holes_ok,
34410acea7dfSKonstantin Belousov ("vm_map_unwire: !HOLESOK and new/changed entry"));
34420acea7dfSKonstantin Belousov continue;
34430acea7dfSKonstantin Belousov }
34440acea7dfSKonstantin Belousov
34453ffbc0cdSAlan Cox if (rv == KERN_SUCCESS && (!user_unwire ||
34463ffbc0cdSAlan Cox (entry->eflags & MAP_ENTRY_USER_WIRED))) {
344703462509SAlan Cox if (entry->wired_count == 1)
344803462509SAlan Cox vm_map_entry_unwire(map, entry);
344903462509SAlan Cox else
3450b2f3846aSAlan Cox entry->wired_count--;
345154a3a114SMark Johnston if (user_unwire)
345254a3a114SMark Johnston entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3453b2f3846aSAlan Cox }
34540acea7dfSKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3455ff3ae454SKonstantin Belousov ("vm_map_unwire: in-transition flag missing %p", entry));
3456ff3ae454SKonstantin Belousov KASSERT(entry->wiring_thread == curthread,
3457ff3ae454SKonstantin Belousov ("vm_map_unwire: alien wire %p", entry));
3458acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
34590acea7dfSKonstantin Belousov entry->wiring_thread = NULL;
3460acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3461acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
34629a0cdf94SDoug Moore need_wakeup = true;
3463acd9a301SAlan Cox }
346483704cc2SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
3465acd9a301SAlan Cox }
346683704cc2SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
3467acd9a301SAlan Cox vm_map_unlock(map);
3468acd9a301SAlan Cox if (need_wakeup)
3469acd9a301SAlan Cox vm_map_wakeup(map);
3470acd9a301SAlan Cox return (rv);
3471acd9a301SAlan Cox }
3472acd9a301SAlan Cox
347354a3a114SMark Johnston static void
vm_map_wire_user_count_sub(u_long npages)347454a3a114SMark Johnston vm_map_wire_user_count_sub(u_long npages)
347554a3a114SMark Johnston {
347654a3a114SMark Johnston
347754a3a114SMark Johnston atomic_subtract_long(&vm_user_wire_count, npages);
347854a3a114SMark Johnston }
347954a3a114SMark Johnston
348054a3a114SMark Johnston static bool
vm_map_wire_user_count_add(u_long npages)348154a3a114SMark Johnston vm_map_wire_user_count_add(u_long npages)
348254a3a114SMark Johnston {
348354a3a114SMark Johnston u_long wired;
348454a3a114SMark Johnston
348554a3a114SMark Johnston wired = vm_user_wire_count;
348654a3a114SMark Johnston do {
348754a3a114SMark Johnston if (npages + wired > vm_page_max_user_wired)
348854a3a114SMark Johnston return (false);
348954a3a114SMark Johnston } while (!atomic_fcmpset_long(&vm_user_wire_count, &wired,
349054a3a114SMark Johnston npages + wired));
349154a3a114SMark Johnston
349254a3a114SMark Johnston return (true);
349354a3a114SMark Johnston }
349454a3a114SMark Johnston
3495acd9a301SAlan Cox /*
349666cd575bSAlan Cox * vm_map_wire_entry_failure:
349766cd575bSAlan Cox *
349866cd575bSAlan Cox * Handle a wiring failure on the given entry.
349966cd575bSAlan Cox *
350066cd575bSAlan Cox * The map should be locked.
350166cd575bSAlan Cox */
350266cd575bSAlan Cox static void
vm_map_wire_entry_failure(vm_map_t map,vm_map_entry_t entry,vm_offset_t failed_addr)350366cd575bSAlan Cox vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
350466cd575bSAlan Cox vm_offset_t failed_addr)
350566cd575bSAlan Cox {
350666cd575bSAlan Cox
350766cd575bSAlan Cox VM_MAP_ASSERT_LOCKED(map);
350866cd575bSAlan Cox KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
350966cd575bSAlan Cox entry->wired_count == 1,
351066cd575bSAlan Cox ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
351166cd575bSAlan Cox KASSERT(failed_addr < entry->end,
351266cd575bSAlan Cox ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
351366cd575bSAlan Cox
351466cd575bSAlan Cox /*
351566cd575bSAlan Cox * If any pages at the start of this entry were successfully wired,
351666cd575bSAlan Cox * then unwire them.
351766cd575bSAlan Cox */
351866cd575bSAlan Cox if (failed_addr > entry->start) {
351966cd575bSAlan Cox pmap_unwire(map->pmap, entry->start, failed_addr);
352066cd575bSAlan Cox vm_object_unwire(entry->object.vm_object, entry->offset,
352166cd575bSAlan Cox failed_addr - entry->start, PQ_ACTIVE);
352266cd575bSAlan Cox }
352366cd575bSAlan Cox
352466cd575bSAlan Cox /*
352566cd575bSAlan Cox * Assign an out-of-range value to represent the failure to wire this
352666cd575bSAlan Cox * entry.
352766cd575bSAlan Cox */
352866cd575bSAlan Cox entry->wired_count = -1;
352966cd575bSAlan Cox }
353066cd575bSAlan Cox
353154a3a114SMark Johnston int
vm_map_wire(vm_map_t map,vm_offset_t start,vm_offset_t end,int flags)353254a3a114SMark Johnston vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
353354a3a114SMark Johnston {
353454a3a114SMark Johnston int rv;
353554a3a114SMark Johnston
353654a3a114SMark Johnston vm_map_lock(map);
353754a3a114SMark Johnston rv = vm_map_wire_locked(map, start, end, flags);
353854a3a114SMark Johnston vm_map_unlock(map);
353954a3a114SMark Johnston return (rv);
354054a3a114SMark Johnston }
354154a3a114SMark Johnston
354266cd575bSAlan Cox /*
354354a3a114SMark Johnston * vm_map_wire_locked:
3544e27e17b7SAlan Cox *
354554a3a114SMark Johnston * Implements both kernel and user wiring. Returns with the map locked,
354654a3a114SMark Johnston * the map lock may be dropped.
3547e27e17b7SAlan Cox */
3548e27e17b7SAlan Cox int
vm_map_wire_locked(vm_map_t map,vm_offset_t start,vm_offset_t end,int flags)354954a3a114SMark Johnston vm_map_wire_locked(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3550e27e17b7SAlan Cox {
355183704cc2SDoug Moore vm_map_entry_t entry, first_entry, next_entry, prev_entry;
355266cd575bSAlan Cox vm_offset_t faddr, saved_end, saved_start;
3553e2e80fb3SKonstantin Belousov u_long incr, npages;
3554e2e80fb3SKonstantin Belousov u_int bidx, last_timestamp;
355512d7cc84SAlan Cox int rv;
355683704cc2SDoug Moore bool holes_ok, need_wakeup, user_wire;
3557e4cd31ddSJeff Roberson vm_prot_t prot;
3558e27e17b7SAlan Cox
355954a3a114SMark Johnston VM_MAP_ASSERT_LOCKED(map);
356054a3a114SMark Johnston
356179e9451fSKonstantin Belousov if (start == end)
356279e9451fSKonstantin Belousov return (KERN_SUCCESS);
3563e4cd31ddSJeff Roberson prot = 0;
3564e4cd31ddSJeff Roberson if (flags & VM_MAP_WIRE_WRITE)
3565e4cd31ddSJeff Roberson prot |= VM_PROT_WRITE;
35669a0cdf94SDoug Moore holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
35679a0cdf94SDoug Moore user_wire = (flags & VM_MAP_WIRE_USER) != 0;
356812d7cc84SAlan Cox VM_MAP_RANGE_CHECK(map, start, end);
3569d1d3f7e1SDoug Moore if (!vm_map_lookup_entry(map, start, &first_entry)) {
35709a0cdf94SDoug Moore if (holes_ok)
35717cdcf863SDoug Moore first_entry = vm_map_entry_succ(first_entry);
3572d1d3f7e1SDoug Moore else
357312d7cc84SAlan Cox return (KERN_INVALID_ADDRESS);
357412d7cc84SAlan Cox }
357583704cc2SDoug Moore for (entry = first_entry; entry->start < end; entry = next_entry) {
357612d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
357712d7cc84SAlan Cox /*
357812d7cc84SAlan Cox * We have not yet clipped the entry.
357912d7cc84SAlan Cox */
358083704cc2SDoug Moore next_entry = vm_map_entry_in_transition(map, start,
358183704cc2SDoug Moore &end, holes_ok, entry);
358283704cc2SDoug Moore if (next_entry == NULL) {
358383704cc2SDoug Moore if (entry == first_entry)
358412d7cc84SAlan Cox return (KERN_INVALID_ADDRESS);
358512d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS;
358612d7cc84SAlan Cox goto done;
358712d7cc84SAlan Cox }
358883704cc2SDoug Moore first_entry = (entry == first_entry) ?
358983704cc2SDoug Moore next_entry : NULL;
359012d7cc84SAlan Cox continue;
359112d7cc84SAlan Cox }
3592e2e80fb3SKonstantin Belousov rv = vm_map_clip_start(map, entry, start);
3593e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
3594e2e80fb3SKonstantin Belousov goto done;
3595e2e80fb3SKonstantin Belousov rv = vm_map_clip_end(map, entry, end);
3596e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
3597e2e80fb3SKonstantin Belousov goto done;
3598e2e80fb3SKonstantin Belousov
359912d7cc84SAlan Cox /*
360012d7cc84SAlan Cox * Mark the entry in case the map lock is released. (See
360112d7cc84SAlan Cox * above.)
360212d7cc84SAlan Cox */
3603ff3ae454SKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3604ff3ae454SKonstantin Belousov entry->wiring_thread == NULL,
3605ff3ae454SKonstantin Belousov ("owned map entry %p", entry));
360612d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION;
36070acea7dfSKonstantin Belousov entry->wiring_thread = curthread;
3608e4cd31ddSJeff Roberson if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3609e4cd31ddSJeff Roberson || (entry->protection & prot) != prot) {
3610529ab57bSKonstantin Belousov entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
36119a0cdf94SDoug Moore if (!holes_ok) {
36126d7e8091SKonstantin Belousov end = entry->end;
36136d7e8091SKonstantin Belousov rv = KERN_INVALID_ADDRESS;
36146d7e8091SKonstantin Belousov goto done;
36156d7e8091SKonstantin Belousov }
361638e220e8SDoug Moore } else if (entry->wired_count == 0) {
36170ada205eSBrian Feldman entry->wired_count++;
361854a3a114SMark Johnston
361954a3a114SMark Johnston npages = atop(entry->end - entry->start);
362054a3a114SMark Johnston if (user_wire && !vm_map_wire_user_count_add(npages)) {
362154a3a114SMark Johnston vm_map_wire_entry_failure(map, entry,
362254a3a114SMark Johnston entry->start);
362354a3a114SMark Johnston end = entry->end;
362454a3a114SMark Johnston rv = KERN_RESOURCE_SHORTAGE;
362554a3a114SMark Johnston goto done;
362654a3a114SMark Johnston }
362766cd575bSAlan Cox
362812d7cc84SAlan Cox /*
362912d7cc84SAlan Cox * Release the map lock, relying on the in-transition
3630a5db445dSMax Laier * mark. Mark the map busy for fork.
363112d7cc84SAlan Cox */
363254a3a114SMark Johnston saved_start = entry->start;
363354a3a114SMark Johnston saved_end = entry->end;
3634312df2c1SDoug Moore last_timestamp = map->timestamp;
3635d0e4e53eSMark Johnston bidx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(entry);
3636e2e80fb3SKonstantin Belousov incr = pagesizes[bidx];
3637a5db445dSMax Laier vm_map_busy(map);
363812d7cc84SAlan Cox vm_map_unlock(map);
363966cd575bSAlan Cox
3640e2e80fb3SKonstantin Belousov for (faddr = saved_start; faddr < saved_end;
3641e2e80fb3SKonstantin Belousov faddr += incr) {
364266cd575bSAlan Cox /*
364366cd575bSAlan Cox * Simulate a fault to get the page and enter
364466cd575bSAlan Cox * it into the physical map.
364566cd575bSAlan Cox */
3646e2e80fb3SKonstantin Belousov rv = vm_fault(map, faddr, VM_PROT_NONE,
3647e2e80fb3SKonstantin Belousov VM_FAULT_WIRE, NULL);
3648e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
364966cd575bSAlan Cox break;
3650e2e80fb3SKonstantin Belousov }
365112d7cc84SAlan Cox vm_map_lock(map);
3652a5db445dSMax Laier vm_map_unbusy(map);
365312d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) {
365412d7cc84SAlan Cox /*
365512d7cc84SAlan Cox * Look again for the entry because the map was
365612d7cc84SAlan Cox * modified while it was unlocked. The entry
365712d7cc84SAlan Cox * may have been clipped, but NOT merged or
365812d7cc84SAlan Cox * deleted.
365912d7cc84SAlan Cox */
36609a0cdf94SDoug Moore if (!vm_map_lookup_entry(map, saved_start,
366183704cc2SDoug Moore &next_entry))
36629a0cdf94SDoug Moore KASSERT(false,
36639a0cdf94SDoug Moore ("vm_map_wire: lookup failed"));
366483704cc2SDoug Moore first_entry = (entry == first_entry) ?
366583704cc2SDoug Moore next_entry : NULL;
366683704cc2SDoug Moore for (entry = next_entry; entry->end < saved_end;
366783704cc2SDoug Moore entry = vm_map_entry_succ(entry)) {
366866cd575bSAlan Cox /*
366966cd575bSAlan Cox * In case of failure, handle entries
367066cd575bSAlan Cox * that were not fully wired here;
367166cd575bSAlan Cox * fully wired entries are handled
367266cd575bSAlan Cox * later.
367366cd575bSAlan Cox */
367466cd575bSAlan Cox if (rv != KERN_SUCCESS &&
367566cd575bSAlan Cox faddr < entry->end)
367666cd575bSAlan Cox vm_map_wire_entry_failure(map,
367766cd575bSAlan Cox entry, faddr);
367812d7cc84SAlan Cox }
367928c58286SAlan Cox }
368012d7cc84SAlan Cox if (rv != KERN_SUCCESS) {
368166cd575bSAlan Cox vm_map_wire_entry_failure(map, entry, faddr);
368254a3a114SMark Johnston if (user_wire)
368354a3a114SMark Johnston vm_map_wire_user_count_sub(npages);
368412d7cc84SAlan Cox end = entry->end;
368512d7cc84SAlan Cox goto done;
368612d7cc84SAlan Cox }
36870ada205eSBrian Feldman } else if (!user_wire ||
36880ada205eSBrian Feldman (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
36890ada205eSBrian Feldman entry->wired_count++;
369012d7cc84SAlan Cox }
369112d7cc84SAlan Cox /*
369212d7cc84SAlan Cox * Check the map for holes in the specified region.
36939a0cdf94SDoug Moore * If holes_ok was specified, skip this check.
369412d7cc84SAlan Cox */
369583704cc2SDoug Moore next_entry = vm_map_entry_succ(entry);
36969a0cdf94SDoug Moore if (!holes_ok &&
369783704cc2SDoug Moore entry->end < end && next_entry->start > entry->end) {
369812d7cc84SAlan Cox end = entry->end;
369912d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS;
370012d7cc84SAlan Cox goto done;
370112d7cc84SAlan Cox }
370212d7cc84SAlan Cox }
370312d7cc84SAlan Cox rv = KERN_SUCCESS;
370412d7cc84SAlan Cox done:
37059a0cdf94SDoug Moore need_wakeup = false;
37069a0cdf94SDoug Moore if (first_entry == NULL &&
37079a0cdf94SDoug Moore !vm_map_lookup_entry(map, start, &first_entry)) {
37089a0cdf94SDoug Moore KASSERT(holes_ok, ("vm_map_wire: lookup failed"));
370983704cc2SDoug Moore prev_entry = first_entry;
371083704cc2SDoug Moore entry = vm_map_entry_succ(first_entry);
371183704cc2SDoug Moore } else {
371283704cc2SDoug Moore prev_entry = vm_map_entry_pred(first_entry);
371383704cc2SDoug Moore entry = first_entry;
371412d7cc84SAlan Cox }
371583704cc2SDoug Moore for (; entry->start < end;
371683704cc2SDoug Moore prev_entry = entry, entry = vm_map_entry_succ(entry)) {
37170acea7dfSKonstantin Belousov /*
37189a0cdf94SDoug Moore * If holes_ok was specified, an empty
37190acea7dfSKonstantin Belousov * space in the unwired region could have been mapped
37200acea7dfSKonstantin Belousov * while the map lock was dropped for faulting in the
37210acea7dfSKonstantin Belousov * pages or draining MAP_ENTRY_IN_TRANSITION.
37220acea7dfSKonstantin Belousov * Moreover, another thread could be simultaneously
37230acea7dfSKonstantin Belousov * wiring this new mapping entry. Detect these cases
3724546bb2d7SKonstantin Belousov * and skip any entries marked as in transition not by us.
3725e2e80fb3SKonstantin Belousov *
3726e2e80fb3SKonstantin Belousov * Another way to get an entry not marked with
3727e2e80fb3SKonstantin Belousov * MAP_ENTRY_IN_TRANSITION is after failed clipping,
3728e2e80fb3SKonstantin Belousov * which set rv to KERN_INVALID_ARGUMENT.
37290acea7dfSKonstantin Belousov */
37300acea7dfSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
37310acea7dfSKonstantin Belousov entry->wiring_thread != curthread) {
3732e2e80fb3SKonstantin Belousov KASSERT(holes_ok || rv == KERN_INVALID_ARGUMENT,
37330acea7dfSKonstantin Belousov ("vm_map_wire: !HOLESOK and new/changed entry"));
37340acea7dfSKonstantin Belousov continue;
37350acea7dfSKonstantin Belousov }
37360acea7dfSKonstantin Belousov
3737b71f9b0dSDoug Moore if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0) {
3738b71f9b0dSDoug Moore /* do nothing */
3739b71f9b0dSDoug Moore } else if (rv == KERN_SUCCESS) {
374012d7cc84SAlan Cox if (user_wire)
374112d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_USER_WIRED;
374228c58286SAlan Cox } else if (entry->wired_count == -1) {
374328c58286SAlan Cox /*
374428c58286SAlan Cox * Wiring failed on this entry. Thus, unwiring is
374528c58286SAlan Cox * unnecessary.
374628c58286SAlan Cox */
374728c58286SAlan Cox entry->wired_count = 0;
374803462509SAlan Cox } else if (!user_wire ||
374903462509SAlan Cox (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
375066cd575bSAlan Cox /*
375166cd575bSAlan Cox * Undo the wiring. Wiring succeeded on this entry
375266cd575bSAlan Cox * but failed on a later entry.
375366cd575bSAlan Cox */
375454a3a114SMark Johnston if (entry->wired_count == 1) {
375503462509SAlan Cox vm_map_entry_unwire(map, entry);
375654a3a114SMark Johnston if (user_wire)
375754a3a114SMark Johnston vm_map_wire_user_count_sub(
375854a3a114SMark Johnston atop(entry->end - entry->start));
375954a3a114SMark Johnston } else
376012d7cc84SAlan Cox entry->wired_count--;
376112d7cc84SAlan Cox }
37620acea7dfSKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
37630acea7dfSKonstantin Belousov ("vm_map_wire: in-transition flag missing %p", entry));
37640acea7dfSKonstantin Belousov KASSERT(entry->wiring_thread == curthread,
37650acea7dfSKonstantin Belousov ("vm_map_wire: alien wire %p", entry));
37660acea7dfSKonstantin Belousov entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
37670acea7dfSKonstantin Belousov MAP_ENTRY_WIRE_SKIPPED);
37680acea7dfSKonstantin Belousov entry->wiring_thread = NULL;
376912d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
377012d7cc84SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
37719a0cdf94SDoug Moore need_wakeup = true;
377212d7cc84SAlan Cox }
377383704cc2SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
377412d7cc84SAlan Cox }
377583704cc2SDoug Moore vm_map_try_merge_entries(map, prev_entry, entry);
377612d7cc84SAlan Cox if (need_wakeup)
377712d7cc84SAlan Cox vm_map_wakeup(map);
377812d7cc84SAlan Cox return (rv);
3779e27e17b7SAlan Cox }
3780e27e17b7SAlan Cox
3781e27e17b7SAlan Cox /*
3782950f8459SAlan Cox * vm_map_sync
3783df8bae1dSRodney W. Grimes *
3784df8bae1dSRodney W. Grimes * Push any dirty cached pages in the address range to their pager.
3785df8bae1dSRodney W. Grimes * If syncio is TRUE, dirty pages are written synchronously.
3786df8bae1dSRodney W. Grimes * If invalidate is TRUE, any cached pages are freed as well.
3787df8bae1dSRodney W. Grimes *
3788637315edSAlan Cox * If the size of the region from start to end is zero, we are
3789637315edSAlan Cox * supposed to flush all modified pages within the region containing
3790637315edSAlan Cox * start. Unfortunately, a region can be split or coalesced with
3791637315edSAlan Cox * neighboring regions, making it difficult to determine what the
3792637315edSAlan Cox * original region was. Therefore, we approximate this requirement by
3793637315edSAlan Cox * flushing the current region containing start.
3794637315edSAlan Cox *
3795df8bae1dSRodney W. Grimes * Returns an error if any part of the specified range is not mapped.
3796df8bae1dSRodney W. Grimes */
3797df8bae1dSRodney W. Grimes int
vm_map_sync(vm_map_t map,vm_offset_t start,vm_offset_t end,boolean_t syncio,boolean_t invalidate)3798950f8459SAlan Cox vm_map_sync(
37991b40f8c0SMatthew Dillon vm_map_t map,
38001b40f8c0SMatthew Dillon vm_offset_t start,
38011b40f8c0SMatthew Dillon vm_offset_t end,
38021b40f8c0SMatthew Dillon boolean_t syncio,
38031b40f8c0SMatthew Dillon boolean_t invalidate)
3804df8bae1dSRodney W. Grimes {
38052767c9f3SDoug Moore vm_map_entry_t entry, first_entry, next_entry;
3806df8bae1dSRodney W. Grimes vm_size_t size;
3807df8bae1dSRodney W. Grimes vm_object_t object;
3808a316d390SJohn Dyson vm_ooffset_t offset;
3809e53fa61bSKonstantin Belousov unsigned int last_timestamp;
3810e2e80fb3SKonstantin Belousov int bdry_idx;
3811126d6082SKonstantin Belousov boolean_t failed;
3812df8bae1dSRodney W. Grimes
3813df8bae1dSRodney W. Grimes vm_map_lock_read(map);
3814df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end);
38152767c9f3SDoug Moore if (!vm_map_lookup_entry(map, start, &first_entry)) {
3816df8bae1dSRodney W. Grimes vm_map_unlock_read(map);
3817df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS);
3818d1d3f7e1SDoug Moore } else if (start == end) {
38192767c9f3SDoug Moore start = first_entry->start;
38202767c9f3SDoug Moore end = first_entry->end;
3821df8bae1dSRodney W. Grimes }
3822e2e80fb3SKonstantin Belousov
3823df8bae1dSRodney W. Grimes /*
3824e2e80fb3SKonstantin Belousov * Make a first pass to check for user-wired memory, holes,
3825e2e80fb3SKonstantin Belousov * and partial invalidation of largepage mappings.
3826df8bae1dSRodney W. Grimes */
38272767c9f3SDoug Moore for (entry = first_entry; entry->start < end; entry = next_entry) {
3828e2e80fb3SKonstantin Belousov if (invalidate) {
3829e2e80fb3SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0) {
3830df8bae1dSRodney W. Grimes vm_map_unlock_read(map);
3831df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT);
3832df8bae1dSRodney W. Grimes }
3833d0e4e53eSMark Johnston bdry_idx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(entry);
3834e2e80fb3SKonstantin Belousov if (bdry_idx != 0 &&
3835e2e80fb3SKonstantin Belousov ((start & (pagesizes[bdry_idx] - 1)) != 0 ||
3836e2e80fb3SKonstantin Belousov (end & (pagesizes[bdry_idx] - 1)) != 0)) {
3837e2e80fb3SKonstantin Belousov vm_map_unlock_read(map);
3838e2e80fb3SKonstantin Belousov return (KERN_INVALID_ARGUMENT);
3839e2e80fb3SKonstantin Belousov }
3840e2e80fb3SKonstantin Belousov }
38412767c9f3SDoug Moore next_entry = vm_map_entry_succ(entry);
38422767c9f3SDoug Moore if (end > entry->end &&
38432767c9f3SDoug Moore entry->end != next_entry->start) {
3844df8bae1dSRodney W. Grimes vm_map_unlock_read(map);
3845df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS);
3846df8bae1dSRodney W. Grimes }
3847df8bae1dSRodney W. Grimes }
3848df8bae1dSRodney W. Grimes
38492cf13952SAlan Cox if (invalidate)
3850bc105a67SAlan Cox pmap_remove(map->pmap, start, end);
3851126d6082SKonstantin Belousov failed = FALSE;
38522cf13952SAlan Cox
3853df8bae1dSRodney W. Grimes /*
3854df8bae1dSRodney W. Grimes * Make a second pass, cleaning/uncaching pages from the indicated
3855df8bae1dSRodney W. Grimes * objects as we go.
3856df8bae1dSRodney W. Grimes */
38572767c9f3SDoug Moore for (entry = first_entry; entry->start < end;) {
38582767c9f3SDoug Moore offset = entry->offset + (start - entry->start);
38592767c9f3SDoug Moore size = (end <= entry->end ? end : entry->end) - start;
38602767c9f3SDoug Moore if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
3861c0877f10SJohn Dyson vm_map_t smap;
3862df8bae1dSRodney W. Grimes vm_map_entry_t tentry;
3863df8bae1dSRodney W. Grimes vm_size_t tsize;
3864df8bae1dSRodney W. Grimes
38652767c9f3SDoug Moore smap = entry->object.sub_map;
3866df8bae1dSRodney W. Grimes vm_map_lock_read(smap);
3867df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry(smap, offset, &tentry);
3868df8bae1dSRodney W. Grimes tsize = tentry->end - offset;
3869df8bae1dSRodney W. Grimes if (tsize < size)
3870df8bae1dSRodney W. Grimes size = tsize;
3871df8bae1dSRodney W. Grimes object = tentry->object.vm_object;
3872df8bae1dSRodney W. Grimes offset = tentry->offset + (offset - tentry->start);
3873df8bae1dSRodney W. Grimes vm_map_unlock_read(smap);
3874df8bae1dSRodney W. Grimes } else {
38752767c9f3SDoug Moore object = entry->object.vm_object;
3876df8bae1dSRodney W. Grimes }
3877e53fa61bSKonstantin Belousov vm_object_reference(object);
3878e53fa61bSKonstantin Belousov last_timestamp = map->timestamp;
3879e53fa61bSKonstantin Belousov vm_map_unlock_read(map);
3880126d6082SKonstantin Belousov if (!vm_object_sync(object, offset, size, syncio, invalidate))
3881126d6082SKonstantin Belousov failed = TRUE;
3882df8bae1dSRodney W. Grimes start += size;
3883e53fa61bSKonstantin Belousov vm_object_deallocate(object);
3884e53fa61bSKonstantin Belousov vm_map_lock_read(map);
3885d1d3f7e1SDoug Moore if (last_timestamp == map->timestamp ||
38862767c9f3SDoug Moore !vm_map_lookup_entry(map, start, &entry))
38872767c9f3SDoug Moore entry = vm_map_entry_succ(entry);
3888df8bae1dSRodney W. Grimes }
3889df8bae1dSRodney W. Grimes
3890df8bae1dSRodney W. Grimes vm_map_unlock_read(map);
3891126d6082SKonstantin Belousov return (failed ? KERN_FAILURE : KERN_SUCCESS);
3892df8bae1dSRodney W. Grimes }
3893df8bae1dSRodney W. Grimes
3894df8bae1dSRodney W. Grimes /*
3895df8bae1dSRodney W. Grimes * vm_map_entry_unwire: [ internal use only ]
3896df8bae1dSRodney W. Grimes *
3897df8bae1dSRodney W. Grimes * Make the region specified by this entry pageable.
3898df8bae1dSRodney W. Grimes *
3899df8bae1dSRodney W. Grimes * The map in question should be locked.
3900df8bae1dSRodney W. Grimes * [This is the reason for this routine's existence.]
3901df8bae1dSRodney W. Grimes */
39020362d7d7SJohn Dyson static void
vm_map_entry_unwire(vm_map_t map,vm_map_entry_t entry)39031b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3904df8bae1dSRodney W. Grimes {
390554a3a114SMark Johnston vm_size_t size;
390603462509SAlan Cox
390703462509SAlan Cox VM_MAP_ASSERT_LOCKED(map);
390803462509SAlan Cox KASSERT(entry->wired_count > 0,
390903462509SAlan Cox ("vm_map_entry_unwire: entry %p isn't wired", entry));
391054a3a114SMark Johnston
391154a3a114SMark Johnston size = entry->end - entry->start;
391254a3a114SMark Johnston if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0)
391354a3a114SMark Johnston vm_map_wire_user_count_sub(atop(size));
391403462509SAlan Cox pmap_unwire(map->pmap, entry->start, entry->end);
391554a3a114SMark Johnston vm_object_unwire(entry->object.vm_object, entry->offset, size,
391654a3a114SMark Johnston PQ_ACTIVE);
3917df8bae1dSRodney W. Grimes entry->wired_count = 0;
3918df8bae1dSRodney W. Grimes }
3919df8bae1dSRodney W. Grimes
39200b367bd8SKonstantin Belousov static void
vm_map_entry_deallocate(vm_map_entry_t entry,boolean_t system_map)39210b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
39220b367bd8SKonstantin Belousov {
39230b367bd8SKonstantin Belousov
39240b367bd8SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
39250b367bd8SKonstantin Belousov vm_object_deallocate(entry->object.vm_object);
39260b367bd8SKonstantin Belousov uma_zfree(system_map ? kmapentzone : mapentzone, entry);
39270b367bd8SKonstantin Belousov }
39280b367bd8SKonstantin Belousov
3929df8bae1dSRodney W. Grimes /*
3930df8bae1dSRodney W. Grimes * vm_map_entry_delete: [ internal use only ]
3931df8bae1dSRodney W. Grimes *
3932df8bae1dSRodney W. Grimes * Deallocate the given entry from the target map.
3933df8bae1dSRodney W. Grimes */
39340362d7d7SJohn Dyson static void
vm_map_entry_delete(vm_map_t map,vm_map_entry_t entry)39351b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3936df8bae1dSRodney W. Grimes {
393732a89c32SAlan Cox vm_object_t object;
393884242cf6SMark Johnston vm_pindex_t offidxstart, offidxend, size1;
3939d1780e8dSKonstantin Belousov vm_size_t size;
394032a89c32SAlan Cox
39419f701172SKonstantin Belousov vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
39423364c323SKonstantin Belousov object = entry->object.vm_object;
394319bd0d9cSKonstantin Belousov
394419bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
394519bd0d9cSKonstantin Belousov MPASS(entry->cred == NULL);
394619bd0d9cSKonstantin Belousov MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
394719bd0d9cSKonstantin Belousov MPASS(object == NULL);
394819bd0d9cSKonstantin Belousov vm_map_entry_deallocate(entry, map->system_map);
394919bd0d9cSKonstantin Belousov return;
395019bd0d9cSKonstantin Belousov }
395119bd0d9cSKonstantin Belousov
39523364c323SKonstantin Belousov size = entry->end - entry->start;
39533364c323SKonstantin Belousov map->size -= size;
39543364c323SKonstantin Belousov
3955ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) {
3956ef694c1aSEdward Tomasz Napierala swap_release_by_cred(size, entry->cred);
3957ef694c1aSEdward Tomasz Napierala crfree(entry->cred);
39583364c323SKonstantin Belousov }
3959df8bae1dSRodney W. Grimes
396063967687SJeff Roberson if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 || object == NULL) {
396163967687SJeff Roberson entry->object.vm_object = NULL;
396263967687SJeff Roberson } else if ((object->flags & OBJ_ANON) != 0 ||
396363967687SJeff Roberson object == kernel_object) {
3964ef694c1aSEdward Tomasz Napierala KASSERT(entry->cred == NULL || object->cred == NULL ||
39653364c323SKonstantin Belousov (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3966ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
396732a89c32SAlan Cox offidxstart = OFF_TO_IDX(entry->offset);
396884242cf6SMark Johnston offidxend = offidxstart + atop(size);
396989f6b863SAttilio Rao VM_OBJECT_WLOCK(object);
397063967687SJeff Roberson if (object->ref_count != 1 &&
397163967687SJeff Roberson ((object->flags & OBJ_ONEMAPPING) != 0 ||
39722e47807cSJeff Roberson object == kernel_object)) {
397332a89c32SAlan Cox vm_object_collapse(object);
39746bbee8e2SAlan Cox
39756bbee8e2SAlan Cox /*
39766bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED can be passed here
39776bbee8e2SAlan Cox * because vm_map_delete() already performed
39786bbee8e2SAlan Cox * pmap_remove() on the only mapping to this range
39796bbee8e2SAlan Cox * of pages.
39806bbee8e2SAlan Cox */
39816bbee8e2SAlan Cox vm_object_page_remove(object, offidxstart, offidxend,
39826bbee8e2SAlan Cox OBJPR_NOTMAPPED);
398332a89c32SAlan Cox if (offidxend >= object->size &&
39843364c323SKonstantin Belousov offidxstart < object->size) {
39853364c323SKonstantin Belousov size1 = object->size;
398632a89c32SAlan Cox object->size = offidxstart;
3987ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) {
39883364c323SKonstantin Belousov size1 -= object->size;
39893364c323SKonstantin Belousov KASSERT(object->charge >= ptoa(size1),
39909a4ee196SKonstantin Belousov ("object %p charge < 0", object));
39919a4ee196SKonstantin Belousov swap_release_by_cred(ptoa(size1),
39929a4ee196SKonstantin Belousov object->cred);
39933364c323SKonstantin Belousov object->charge -= ptoa(size1);
39943364c323SKonstantin Belousov }
39953364c323SKonstantin Belousov }
399632a89c32SAlan Cox }
399789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object);
399863967687SJeff Roberson }
39990b367bd8SKonstantin Belousov if (map->system_map)
40000b367bd8SKonstantin Belousov vm_map_entry_deallocate(entry, TRUE);
40010b367bd8SKonstantin Belousov else {
40027cdcf863SDoug Moore entry->defer_next = curthread->td_map_def_user;
40030b367bd8SKonstantin Belousov curthread->td_map_def_user = entry;
40040b367bd8SKonstantin Belousov }
4005df8bae1dSRodney W. Grimes }
4006df8bae1dSRodney W. Grimes
4007df8bae1dSRodney W. Grimes /*
4008df8bae1dSRodney W. Grimes * vm_map_delete: [ internal use only ]
4009df8bae1dSRodney W. Grimes *
4010df8bae1dSRodney W. Grimes * Deallocates the given address range from the target
4011df8bae1dSRodney W. Grimes * map.
4012df8bae1dSRodney W. Grimes */
4013df8bae1dSRodney W. Grimes int
vm_map_delete(vm_map_t map,vm_offset_t start,vm_offset_t end)4014655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
4015df8bae1dSRodney W. Grimes {
4016e2e80fb3SKonstantin Belousov vm_map_entry_t entry, next_entry, scratch_entry;
4017e2e80fb3SKonstantin Belousov int rv;
4018df8bae1dSRodney W. Grimes
40193a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map);
40208a64110eSConrad Meyer
402179e9451fSKonstantin Belousov if (start == end)
402279e9451fSKonstantin Belousov return (KERN_SUCCESS);
40233a0916b8SKonstantin Belousov
4024df8bae1dSRodney W. Grimes /*
4025c7b23459SDoug Moore * Find the start of the region, and clip it.
4026c7b23459SDoug Moore * Step through all entries in this region.
4027df8bae1dSRodney W. Grimes */
4028e2e80fb3SKonstantin Belousov rv = vm_map_lookup_clip_start(map, start, &entry, &scratch_entry);
4029e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
4030e2e80fb3SKonstantin Belousov return (rv);
4031e2e80fb3SKonstantin Belousov for (; entry->start < end; entry = next_entry) {
403273b2baceSAlan Cox /*
403373b2baceSAlan Cox * Wait for wiring or unwiring of an entry to complete.
40347c938963SBrian Feldman * Also wait for any system wirings to disappear on
40357c938963SBrian Feldman * user maps.
403673b2baceSAlan Cox */
40377c938963SBrian Feldman if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
40387c938963SBrian Feldman (vm_map_pmap(map) != kernel_pmap &&
40397c938963SBrian Feldman vm_map_entry_system_wired_count(entry) != 0)) {
404073b2baceSAlan Cox unsigned int last_timestamp;
404173b2baceSAlan Cox vm_offset_t saved_start;
404273b2baceSAlan Cox
404373b2baceSAlan Cox saved_start = entry->start;
404473b2baceSAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
404573b2baceSAlan Cox last_timestamp = map->timestamp;
40468ce2d00aSPawel Jakub Dawidek (void) vm_map_unlock_and_wait(map, 0);
404773b2baceSAlan Cox vm_map_lock(map);
4048d1d3f7e1SDoug Moore if (last_timestamp + 1 != map->timestamp) {
404973b2baceSAlan Cox /*
405073b2baceSAlan Cox * Look again for the entry because the map was
4051d1d3f7e1SDoug Moore * modified while it was unlocked.
4052d1d3f7e1SDoug Moore * Specifically, the entry may have been
4053d1d3f7e1SDoug Moore * clipped, merged, or deleted.
405473b2baceSAlan Cox */
4055e2e80fb3SKonstantin Belousov rv = vm_map_lookup_clip_start(map, saved_start,
4056e2e80fb3SKonstantin Belousov &next_entry, &scratch_entry);
4057e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
4058e2e80fb3SKonstantin Belousov break;
4059c7b23459SDoug Moore } else
4060c7b23459SDoug Moore next_entry = entry;
406173b2baceSAlan Cox continue;
406273b2baceSAlan Cox }
4063e2e80fb3SKonstantin Belousov
4064e2e80fb3SKonstantin Belousov /* XXXKIB or delete to the upper superpage boundary ? */
4065e2e80fb3SKonstantin Belousov rv = vm_map_clip_end(map, entry, end);
4066e2e80fb3SKonstantin Belousov if (rv != KERN_SUCCESS)
4067e2e80fb3SKonstantin Belousov break;
4068c7b23459SDoug Moore next_entry = vm_map_entry_succ(entry);
4069df8bae1dSRodney W. Grimes
4070df8bae1dSRodney W. Grimes /*
40710d94caffSDavid Greenman * Unwire before removing addresses from the pmap; otherwise,
40720d94caffSDavid Greenman * unwiring will put the entries back in the pmap.
4073df8bae1dSRodney W. Grimes */
4074be7be412SKonstantin Belousov if (entry->wired_count != 0)
4075df8bae1dSRodney W. Grimes vm_map_entry_unwire(map, entry);
4076df8bae1dSRodney W. Grimes
407732f0fefcSKonstantin Belousov /*
407832f0fefcSKonstantin Belousov * Remove mappings for the pages, but only if the
407932f0fefcSKonstantin Belousov * mappings could exist. For instance, it does not
408032f0fefcSKonstantin Belousov * make sense to call pmap_remove() for guard entries.
408132f0fefcSKonstantin Belousov */
408232f0fefcSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
408332f0fefcSKonstantin Belousov entry->object.vm_object != NULL)
40841e0e335bSKonstantin Belousov pmap_map_delete(map->pmap, entry->start, entry->end);
4085df8bae1dSRodney W. Grimes
4086df8bae1dSRodney W. Grimes /*
4087e608cc3cSKonstantin Belousov * Delete the entry only after removing all pmap
4088e608cc3cSKonstantin Belousov * entries pointing to its pages. (Otherwise, its
4089e608cc3cSKonstantin Belousov * page frames may be reallocated, and any modify bits
4090e608cc3cSKonstantin Belousov * will be set in the wrong object!)
4091df8bae1dSRodney W. Grimes */
4092df8bae1dSRodney W. Grimes vm_map_entry_delete(map, entry);
4093df8bae1dSRodney W. Grimes }
4094e2e80fb3SKonstantin Belousov return (rv);
4095df8bae1dSRodney W. Grimes }
4096df8bae1dSRodney W. Grimes
4097df8bae1dSRodney W. Grimes /*
4098df8bae1dSRodney W. Grimes * vm_map_remove:
4099df8bae1dSRodney W. Grimes *
4100df8bae1dSRodney W. Grimes * Remove the given address range from the target map.
4101df8bae1dSRodney W. Grimes * This is the exported form of vm_map_delete.
4102df8bae1dSRodney W. Grimes */
4103df8bae1dSRodney W. Grimes int
vm_map_remove(vm_map_t map,vm_offset_t start,vm_offset_t end)41041b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
4105df8bae1dSRodney W. Grimes {
41066eaee3feSAlan Cox int result;
4107df8bae1dSRodney W. Grimes
4108df8bae1dSRodney W. Grimes vm_map_lock(map);
4109df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end);
4110655c3490SKonstantin Belousov result = vm_map_delete(map, start, end);
4111df8bae1dSRodney W. Grimes vm_map_unlock(map);
4112df8bae1dSRodney W. Grimes return (result);
4113df8bae1dSRodney W. Grimes }
4114df8bae1dSRodney W. Grimes
4115df8bae1dSRodney W. Grimes /*
4116df8bae1dSRodney W. Grimes * vm_map_check_protection:
4117df8bae1dSRodney W. Grimes *
41182d5c7e45SMatthew Dillon * Assert that the target map allows the specified privilege on the
41192d5c7e45SMatthew Dillon * entire address region given. The entire region must be allocated.
41202d5c7e45SMatthew Dillon *
41212d5c7e45SMatthew Dillon * WARNING! This code does not and should not check whether the
41222d5c7e45SMatthew Dillon * contents of the region is accessible. For example a smaller file
41232d5c7e45SMatthew Dillon * might be mapped into a larger address space.
41242d5c7e45SMatthew Dillon *
41252d5c7e45SMatthew Dillon * NOTE! This code is also called by munmap().
4126d8834602SAlan Cox *
4127d8834602SAlan Cox * The map must be locked. A read lock is sufficient.
4128df8bae1dSRodney W. Grimes */
41290d94caffSDavid Greenman boolean_t
vm_map_check_protection(vm_map_t map,vm_offset_t start,vm_offset_t end,vm_prot_t protection)4130b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
4131b9dcd593SBruce Evans vm_prot_t protection)
4132df8bae1dSRodney W. Grimes {
4133c0877f10SJohn Dyson vm_map_entry_t entry;
4134d1d3f7e1SDoug Moore vm_map_entry_t tmp_entry;
4135df8bae1dSRodney W. Grimes
4136d1d3f7e1SDoug Moore if (!vm_map_lookup_entry(map, start, &tmp_entry))
4137df8bae1dSRodney W. Grimes return (FALSE);
4138d1d3f7e1SDoug Moore entry = tmp_entry;
4139df8bae1dSRodney W. Grimes
4140df8bae1dSRodney W. Grimes while (start < end) {
4141df8bae1dSRodney W. Grimes /*
4142df8bae1dSRodney W. Grimes * No holes allowed!
4143df8bae1dSRodney W. Grimes */
4144d8834602SAlan Cox if (start < entry->start)
4145df8bae1dSRodney W. Grimes return (FALSE);
4146df8bae1dSRodney W. Grimes /*
4147df8bae1dSRodney W. Grimes * Check protection associated with entry.
4148df8bae1dSRodney W. Grimes */
4149d8834602SAlan Cox if ((entry->protection & protection) != protection)
4150df8bae1dSRodney W. Grimes return (FALSE);
4151df8bae1dSRodney W. Grimes /* go to next entry */
4152df8bae1dSRodney W. Grimes start = entry->end;
41537cdcf863SDoug Moore entry = vm_map_entry_succ(entry);
4154df8bae1dSRodney W. Grimes }
4155df8bae1dSRodney W. Grimes return (TRUE);
4156df8bae1dSRodney W. Grimes }
4157df8bae1dSRodney W. Grimes
41584d987866SJeff Roberson /*
41594d987866SJeff Roberson *
4160886b9021SJeff Roberson * vm_map_copy_swap_object:
41614d987866SJeff Roberson *
4162886b9021SJeff Roberson * Copies a swap-backed object from an existing map entry to a
41634d987866SJeff Roberson * new one. Carries forward the swap charge. May change the
41644d987866SJeff Roberson * src object on return.
41654d987866SJeff Roberson */
41664d987866SJeff Roberson static void
vm_map_copy_swap_object(vm_map_entry_t src_entry,vm_map_entry_t dst_entry,vm_offset_t size,vm_ooffset_t * fork_charge)4167886b9021SJeff Roberson vm_map_copy_swap_object(vm_map_entry_t src_entry, vm_map_entry_t dst_entry,
41684d987866SJeff Roberson vm_offset_t size, vm_ooffset_t *fork_charge)
41694d987866SJeff Roberson {
41704d987866SJeff Roberson vm_object_t src_object;
41714d987866SJeff Roberson struct ucred *cred;
41724d987866SJeff Roberson int charged;
41734d987866SJeff Roberson
41744d987866SJeff Roberson src_object = src_entry->object.vm_object;
41754d987866SJeff Roberson charged = ENTRY_CHARGED(src_entry);
4176d966c761SJeff Roberson if ((src_object->flags & OBJ_ANON) != 0) {
4177d966c761SJeff Roberson VM_OBJECT_WLOCK(src_object);
41784d987866SJeff Roberson vm_object_collapse(src_object);
41794d987866SJeff Roberson if ((src_object->flags & OBJ_ONEMAPPING) != 0) {
41804d987866SJeff Roberson vm_object_split(src_entry);
41814d987866SJeff Roberson src_object = src_entry->object.vm_object;
41824d987866SJeff Roberson }
41834d987866SJeff Roberson vm_object_reference_locked(src_object);
41844d987866SJeff Roberson vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
4185d966c761SJeff Roberson VM_OBJECT_WUNLOCK(src_object);
4186d966c761SJeff Roberson } else
4187d966c761SJeff Roberson vm_object_reference(src_object);
41884d987866SJeff Roberson if (src_entry->cred != NULL &&
41894d987866SJeff Roberson !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
41904d987866SJeff Roberson KASSERT(src_object->cred == NULL,
41914d987866SJeff Roberson ("OVERCOMMIT: vm_map_copy_anon_entry: cred %p",
41924d987866SJeff Roberson src_object));
41934d987866SJeff Roberson src_object->cred = src_entry->cred;
41944d987866SJeff Roberson src_object->charge = size;
41954d987866SJeff Roberson }
41964d987866SJeff Roberson dst_entry->object.vm_object = src_object;
41974d987866SJeff Roberson if (charged) {
41984d987866SJeff Roberson cred = curthread->td_ucred;
41994d987866SJeff Roberson crhold(cred);
42004d987866SJeff Roberson dst_entry->cred = cred;
42014d987866SJeff Roberson *fork_charge += size;
42024d987866SJeff Roberson if (!(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
42034d987866SJeff Roberson crhold(cred);
42044d987866SJeff Roberson src_entry->cred = cred;
42054d987866SJeff Roberson *fork_charge += size;
42064d987866SJeff Roberson }
42074d987866SJeff Roberson }
42084d987866SJeff Roberson }
42094d987866SJeff Roberson
421086524867SJohn Dyson /*
4211df8bae1dSRodney W. Grimes * vm_map_copy_entry:
4212df8bae1dSRodney W. Grimes *
4213df8bae1dSRodney W. Grimes * Copies the contents of the source entry to the destination
4214df8bae1dSRodney W. Grimes * entry. The entries *must* be aligned properly.
4215df8bae1dSRodney W. Grimes */
4216f708ef1bSPoul-Henning Kamp static void
vm_map_copy_entry(vm_map_t src_map,vm_map_t dst_map,vm_map_entry_t src_entry,vm_map_entry_t dst_entry,vm_ooffset_t * fork_charge)42171b40f8c0SMatthew Dillon vm_map_copy_entry(
42181b40f8c0SMatthew Dillon vm_map_t src_map,
42191b40f8c0SMatthew Dillon vm_map_t dst_map,
42201b40f8c0SMatthew Dillon vm_map_entry_t src_entry,
42213364c323SKonstantin Belousov vm_map_entry_t dst_entry,
42223364c323SKonstantin Belousov vm_ooffset_t *fork_charge)
4223df8bae1dSRodney W. Grimes {
4224c0877f10SJohn Dyson vm_object_t src_object;
422584110e7eSKonstantin Belousov vm_map_entry_t fake_entry;
42263364c323SKonstantin Belousov vm_offset_t size;
4227c0877f10SJohn Dyson
42283a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(dst_map);
42293a0916b8SKonstantin Belousov
42309fdfe602SMatthew Dillon if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
4231df8bae1dSRodney W. Grimes return;
4232df8bae1dSRodney W. Grimes
4233afaa41f6SAlan Cox if (src_entry->wired_count == 0 ||
4234afaa41f6SAlan Cox (src_entry->protection & VM_PROT_WRITE) == 0) {
4235df8bae1dSRodney W. Grimes /*
42360d94caffSDavid Greenman * If the source entry is marked needs_copy, it is already
42370d94caffSDavid Greenman * write-protected.
4238df8bae1dSRodney W. Grimes */
4239d9a9209aSAlan Cox if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
4240d9a9209aSAlan Cox (src_entry->protection & VM_PROT_WRITE) != 0) {
4241df8bae1dSRodney W. Grimes pmap_protect(src_map->pmap,
4242df8bae1dSRodney W. Grimes src_entry->start,
4243df8bae1dSRodney W. Grimes src_entry->end,
4244df8bae1dSRodney W. Grimes src_entry->protection & ~VM_PROT_WRITE);
4245df8bae1dSRodney W. Grimes }
4246b18bfc3dSJohn Dyson
4247df8bae1dSRodney W. Grimes /*
4248df8bae1dSRodney W. Grimes * Make a copy of the object.
4249df8bae1dSRodney W. Grimes */
42503364c323SKonstantin Belousov size = src_entry->end - src_entry->start;
42518aef1712SMatthew Dillon if ((src_object = src_entry->object.vm_object) != NULL) {
42520cb2610eSMark Johnston if ((src_object->flags & OBJ_SWAP) != 0) {
4253886b9021SJeff Roberson vm_map_copy_swap_object(src_entry, dst_entry,
42544d987866SJeff Roberson size, fork_charge);
42554d987866SJeff Roberson /* May have split/collapsed, reload obj. */
42564d987866SJeff Roberson src_object = src_entry->object.vm_object;
42574d987866SJeff Roberson } else {
42584d987866SJeff Roberson vm_object_reference(src_object);
4259c0877f10SJohn Dyson dst_entry->object.vm_object = src_object;
42603364c323SKonstantin Belousov }
42619a4ee196SKonstantin Belousov src_entry->eflags |= MAP_ENTRY_COW |
42629a4ee196SKonstantin Belousov MAP_ENTRY_NEEDS_COPY;
42639a4ee196SKonstantin Belousov dst_entry->eflags |= MAP_ENTRY_COW |
42649a4ee196SKonstantin Belousov MAP_ENTRY_NEEDS_COPY;
4265b18bfc3dSJohn Dyson dst_entry->offset = src_entry->offset;
4266fe7bcbafSKyle Evans if (src_entry->eflags & MAP_ENTRY_WRITECNT) {
426784110e7eSKonstantin Belousov /*
4268fe7bcbafSKyle Evans * MAP_ENTRY_WRITECNT cannot
426984110e7eSKonstantin Belousov * indicate write reference from
427084110e7eSKonstantin Belousov * src_entry, since the entry is
427184110e7eSKonstantin Belousov * marked as needs copy. Allocate a
427284110e7eSKonstantin Belousov * fake entry that is used to
4273fe7bcbafSKyle Evans * decrement object->un_pager writecount
427484110e7eSKonstantin Belousov * at the appropriate time. Attach
427584110e7eSKonstantin Belousov * fake_entry to the deferred list.
427684110e7eSKonstantin Belousov */
427784110e7eSKonstantin Belousov fake_entry = vm_map_entry_create(dst_map);
4278fe7bcbafSKyle Evans fake_entry->eflags = MAP_ENTRY_WRITECNT;
4279fe7bcbafSKyle Evans src_entry->eflags &= ~MAP_ENTRY_WRITECNT;
428084110e7eSKonstantin Belousov vm_object_reference(src_object);
428184110e7eSKonstantin Belousov fake_entry->object.vm_object = src_object;
428284110e7eSKonstantin Belousov fake_entry->start = src_entry->start;
428384110e7eSKonstantin Belousov fake_entry->end = src_entry->end;
42847cdcf863SDoug Moore fake_entry->defer_next =
42857cdcf863SDoug Moore curthread->td_map_def_user;
428684110e7eSKonstantin Belousov curthread->td_map_def_user = fake_entry;
428784110e7eSKonstantin Belousov }
42880ec97ffcSKonstantin Belousov
42890ec97ffcSKonstantin Belousov pmap_copy(dst_map->pmap, src_map->pmap,
42900ec97ffcSKonstantin Belousov dst_entry->start, dst_entry->end - dst_entry->start,
42910ec97ffcSKonstantin Belousov src_entry->start);
4292b18bfc3dSJohn Dyson } else {
4293b18bfc3dSJohn Dyson dst_entry->object.vm_object = NULL;
4294db6c7c7fSKonstantin Belousov if ((dst_entry->eflags & MAP_ENTRY_GUARD) == 0)
4295b18bfc3dSJohn Dyson dst_entry->offset = 0;
4296ef694c1aSEdward Tomasz Napierala if (src_entry->cred != NULL) {
4297ef694c1aSEdward Tomasz Napierala dst_entry->cred = curthread->td_ucred;
4298ef694c1aSEdward Tomasz Napierala crhold(dst_entry->cred);
42993364c323SKonstantin Belousov *fork_charge += size;
43003364c323SKonstantin Belousov }
4301b18bfc3dSJohn Dyson }
43020d94caffSDavid Greenman } else {
4303df8bae1dSRodney W. Grimes /*
4304afaa41f6SAlan Cox * We don't want to make writeable wired pages copy-on-write.
4305afaa41f6SAlan Cox * Immediately copy these pages into the new map by simulating
4306afaa41f6SAlan Cox * page faults. The new pages are pageable.
4307df8bae1dSRodney W. Grimes */
4308121fd461SKonstantin Belousov vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
4309121fd461SKonstantin Belousov fork_charge);
4310df8bae1dSRodney W. Grimes }
4311df8bae1dSRodney W. Grimes }
4312df8bae1dSRodney W. Grimes
4313df8bae1dSRodney W. Grimes /*
43142a7be1b6SBrian Feldman * vmspace_map_entry_forked:
43152a7be1b6SBrian Feldman * Update the newly-forked vmspace each time a map entry is inherited
43162a7be1b6SBrian Feldman * or copied. The values for vm_dsize and vm_tsize are approximate
43172a7be1b6SBrian Feldman * (and mostly-obsolete ideas in the face of mmap(2) et al.)
43182a7be1b6SBrian Feldman */
43192a7be1b6SBrian Feldman static void
vmspace_map_entry_forked(const struct vmspace * vm1,struct vmspace * vm2,vm_map_entry_t entry)43202a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
43212a7be1b6SBrian Feldman vm_map_entry_t entry)
43222a7be1b6SBrian Feldman {
43232a7be1b6SBrian Feldman vm_size_t entrysize;
43242a7be1b6SBrian Feldman vm_offset_t newend;
43252a7be1b6SBrian Feldman
432619bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
432719bd0d9cSKonstantin Belousov return;
43282a7be1b6SBrian Feldman entrysize = entry->end - entry->start;
43292a7be1b6SBrian Feldman vm2->vm_map.size += entrysize;
43302a7be1b6SBrian Feldman if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
43312a7be1b6SBrian Feldman vm2->vm_ssize += btoc(entrysize);
43322a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
43332a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
4334b351299cSAndrew Gallatin newend = MIN(entry->end,
43352a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
43362a7be1b6SBrian Feldman vm2->vm_dsize += btoc(newend - entry->start);
43372a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
43382a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
4339b351299cSAndrew Gallatin newend = MIN(entry->end,
43402a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
43412a7be1b6SBrian Feldman vm2->vm_tsize += btoc(newend - entry->start);
43422a7be1b6SBrian Feldman }
43432a7be1b6SBrian Feldman }
43442a7be1b6SBrian Feldman
43452a7be1b6SBrian Feldman /*
4346df8bae1dSRodney W. Grimes * vmspace_fork:
4347df8bae1dSRodney W. Grimes * Create a new process vmspace structure and vm_map
4348df8bae1dSRodney W. Grimes * based on those of an existing process. The new map
4349df8bae1dSRodney W. Grimes * is based on the old map, according to the inheritance
4350df8bae1dSRodney W. Grimes * values on the regions in that map.
4351df8bae1dSRodney W. Grimes *
43522a7be1b6SBrian Feldman * XXX It might be worth coalescing the entries added to the new vmspace.
43532a7be1b6SBrian Feldman *
4354df8bae1dSRodney W. Grimes * The source map must not be locked.
4355df8bae1dSRodney W. Grimes */
4356df8bae1dSRodney W. Grimes struct vmspace *
vmspace_fork(struct vmspace * vm1,vm_ooffset_t * fork_charge)43573364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
4358df8bae1dSRodney W. Grimes {
4359c0877f10SJohn Dyson struct vmspace *vm2;
436079e53838SAlan Cox vm_map_t new_map, old_map;
436179e53838SAlan Cox vm_map_entry_t new_entry, old_entry;
4362de5f6a77SJohn Dyson vm_object_t object;
4363b8ebd99aSJohn Baldwin int error, locked __diagused;
436419bd0d9cSKonstantin Belousov vm_inherit_t inh;
4365df8bae1dSRodney W. Grimes
436679e53838SAlan Cox old_map = &vm1->vm_map;
436779e53838SAlan Cox /* Copy immutable fields of vm1 to vm2. */
43686e00f3a3SKonstantin Belousov vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
43696e00f3a3SKonstantin Belousov pmap_pinit);
437089b57fcfSKonstantin Belousov if (vm2 == NULL)
437179e53838SAlan Cox return (NULL);
4372e7a9df16SKonstantin Belousov
43732a7be1b6SBrian Feldman vm2->vm_taddr = vm1->vm_taddr;
43742a7be1b6SBrian Feldman vm2->vm_daddr = vm1->vm_daddr;
43752a7be1b6SBrian Feldman vm2->vm_maxsaddr = vm1->vm_maxsaddr;
437646d35d41SMark Johnston vm2->vm_stacktop = vm1->vm_stacktop;
4377361971fbSKornel Dulęba vm2->vm_shp_base = vm1->vm_shp_base;
437879e53838SAlan Cox vm_map_lock(old_map);
437979e53838SAlan Cox if (old_map->busy)
438079e53838SAlan Cox vm_map_wait_busy(old_map);
438179e53838SAlan Cox new_map = &vm2->vm_map;
43821fac7d7fSKonstantin Belousov locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
43831fac7d7fSKonstantin Belousov KASSERT(locked, ("vmspace_fork: lock failed"));
4384df8bae1dSRodney W. Grimes
4385e7a9df16SKonstantin Belousov error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
4386e7a9df16SKonstantin Belousov if (error != 0) {
4387e7a9df16SKonstantin Belousov sx_xunlock(&old_map->lock);
4388e7a9df16SKonstantin Belousov sx_xunlock(&new_map->lock);
4389e7a9df16SKonstantin Belousov vm_map_process_deferred();
4390e7a9df16SKonstantin Belousov vmspace_free(vm2);
4391e7a9df16SKonstantin Belousov return (NULL);
4392e7a9df16SKonstantin Belousov }
4393e7a9df16SKonstantin Belousov
4394fa50a355SKonstantin Belousov new_map->anon_loc = old_map->anon_loc;
43959402bb44SKonstantin Belousov new_map->flags |= old_map->flags & (MAP_ASLR | MAP_ASLR_IGNSTART |
43961811c1e9SMark Johnston MAP_ASLR_STACK | MAP_WXORX);
4397e7a9df16SKonstantin Belousov
43982767c9f3SDoug Moore VM_MAP_ENTRY_FOREACH(old_entry, old_map) {
43992767c9f3SDoug Moore if ((old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
4400df8bae1dSRodney W. Grimes panic("vm_map_fork: encountered a submap");
4401df8bae1dSRodney W. Grimes
440219bd0d9cSKonstantin Belousov inh = old_entry->inheritance;
440319bd0d9cSKonstantin Belousov if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
440419bd0d9cSKonstantin Belousov inh != VM_INHERIT_NONE)
440519bd0d9cSKonstantin Belousov inh = VM_INHERIT_COPY;
440619bd0d9cSKonstantin Belousov
440719bd0d9cSKonstantin Belousov switch (inh) {
4408df8bae1dSRodney W. Grimes case VM_INHERIT_NONE:
4409df8bae1dSRodney W. Grimes break;
4410df8bae1dSRodney W. Grimes
4411df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE:
4412df8bae1dSRodney W. Grimes /*
44132767c9f3SDoug Moore * Clone the entry, creating the shared object if
44142767c9f3SDoug Moore * necessary.
4415fed9a903SJohn Dyson */
4416fed9a903SJohn Dyson object = old_entry->object.vm_object;
4417fed9a903SJohn Dyson if (object == NULL) {
4418af1d6d6aSDoug Moore vm_map_entry_back(old_entry);
4419af1d6d6aSDoug Moore object = old_entry->object.vm_object;
44209a2f6362SAlan Cox }
44219a2f6362SAlan Cox
44229a2f6362SAlan Cox /*
44239a2f6362SAlan Cox * Add the reference before calling vm_object_shadow
44249a2f6362SAlan Cox * to insure that a shadow object is created.
44259a2f6362SAlan Cox */
44269a2f6362SAlan Cox vm_object_reference(object);
44279a2f6362SAlan Cox if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
44285069bf57SJohn Dyson vm_object_shadow(&old_entry->object.vm_object,
44295069bf57SJohn Dyson &old_entry->offset,
443067388836SKonstantin Belousov old_entry->end - old_entry->start,
443167388836SKonstantin Belousov old_entry->cred,
4432d30344bdSIan Dowse /* Transfer the second reference too. */
443367388836SKonstantin Belousov true);
443467388836SKonstantin Belousov old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
443567388836SKonstantin Belousov old_entry->cred = NULL;
44367fd10fb3SKonstantin Belousov
44377fd10fb3SKonstantin Belousov /*
443883ea714fSDoug Moore * As in vm_map_merged_neighbor_dispose(),
443983ea714fSDoug Moore * the vnode lock will not be acquired in
44407fd10fb3SKonstantin Belousov * this call to vm_object_deallocate().
44417fd10fb3SKonstantin Belousov */
4442d30344bdSIan Dowse vm_object_deallocate(object);
44435069bf57SJohn Dyson object = old_entry->object.vm_object;
444467388836SKonstantin Belousov } else {
444589f6b863SAttilio Rao VM_OBJECT_WLOCK(object);
4446069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_ONEMAPPING);
4447ef694c1aSEdward Tomasz Napierala if (old_entry->cred != NULL) {
444867388836SKonstantin Belousov KASSERT(object->cred == NULL,
444967388836SKonstantin Belousov ("vmspace_fork both cred"));
4450ef694c1aSEdward Tomasz Napierala object->cred = old_entry->cred;
445167388836SKonstantin Belousov object->charge = old_entry->end -
445267388836SKonstantin Belousov old_entry->start;
4453ef694c1aSEdward Tomasz Napierala old_entry->cred = NULL;
44543364c323SKonstantin Belousov }
4455b9781cf6SKonstantin Belousov
4456b9781cf6SKonstantin Belousov /*
4457b9781cf6SKonstantin Belousov * Assert the correct state of the vnode
4458b9781cf6SKonstantin Belousov * v_writecount while the object is locked, to
4459b9781cf6SKonstantin Belousov * not relock it later for the assertion
4460b9781cf6SKonstantin Belousov * correctness.
4461b9781cf6SKonstantin Belousov */
4462fe7bcbafSKyle Evans if (old_entry->eflags & MAP_ENTRY_WRITECNT &&
4463b9781cf6SKonstantin Belousov object->type == OBJT_VNODE) {
446467388836SKonstantin Belousov KASSERT(((struct vnode *)object->
446567388836SKonstantin Belousov handle)->v_writecount > 0,
446667388836SKonstantin Belousov ("vmspace_fork: v_writecount %p",
446767388836SKonstantin Belousov object));
446867388836SKonstantin Belousov KASSERT(object->un_pager.vnp.
446967388836SKonstantin Belousov writemappings > 0,
4470b9781cf6SKonstantin Belousov ("vmspace_fork: vnp.writecount %p",
4471b9781cf6SKonstantin Belousov object));
4472b9781cf6SKonstantin Belousov }
447389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object);
447467388836SKonstantin Belousov }
4475fed9a903SJohn Dyson
4476fed9a903SJohn Dyson /*
4477ad5fca3bSAlan Cox * Clone the entry, referencing the shared object.
4478df8bae1dSRodney W. Grimes */
4479df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map);
4480df8bae1dSRodney W. Grimes *new_entry = *old_entry;
44819f6acfd1SKonstantin Belousov new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
44829f6acfd1SKonstantin Belousov MAP_ENTRY_IN_TRANSITION);
44830acea7dfSKonstantin Belousov new_entry->wiring_thread = NULL;
4484df8bae1dSRodney W. Grimes new_entry->wired_count = 0;
4485fe7bcbafSKyle Evans if (new_entry->eflags & MAP_ENTRY_WRITECNT) {
4486fe7bcbafSKyle Evans vm_pager_update_writecount(object,
448784110e7eSKonstantin Belousov new_entry->start, new_entry->end);
448884110e7eSKonstantin Belousov }
448978022527SKonstantin Belousov vm_map_entry_set_vnode_text(new_entry, true);
4490df8bae1dSRodney W. Grimes
4491df8bae1dSRodney W. Grimes /*
44920d94caffSDavid Greenman * Insert the entry into the new map -- we know we're
44930d94caffSDavid Greenman * inserting at the end of the new map.
4494df8bae1dSRodney W. Grimes */
44959f701172SKonstantin Belousov vm_map_entry_link(new_map, new_entry);
44962a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry);
4497df8bae1dSRodney W. Grimes
4498df8bae1dSRodney W. Grimes /*
4499df8bae1dSRodney W. Grimes * Update the physical map
4500df8bae1dSRodney W. Grimes */
4501df8bae1dSRodney W. Grimes pmap_copy(new_map->pmap, old_map->pmap,
4502df8bae1dSRodney W. Grimes new_entry->start,
4503df8bae1dSRodney W. Grimes (old_entry->end - old_entry->start),
4504df8bae1dSRodney W. Grimes old_entry->start);
4505df8bae1dSRodney W. Grimes break;
4506df8bae1dSRodney W. Grimes
4507df8bae1dSRodney W. Grimes case VM_INHERIT_COPY:
4508df8bae1dSRodney W. Grimes /*
4509df8bae1dSRodney W. Grimes * Clone the entry and link into the map.
4510df8bae1dSRodney W. Grimes */
4511df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map);
4512df8bae1dSRodney W. Grimes *new_entry = *old_entry;
451384110e7eSKonstantin Belousov /*
451484110e7eSKonstantin Belousov * Copied entry is COW over the old object.
451584110e7eSKonstantin Belousov */
45169f6acfd1SKonstantin Belousov new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4517fe7bcbafSKyle Evans MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_WRITECNT);
45180acea7dfSKonstantin Belousov new_entry->wiring_thread = NULL;
4519df8bae1dSRodney W. Grimes new_entry->wired_count = 0;
4520df8bae1dSRodney W. Grimes new_entry->object.vm_object = NULL;
4521ef694c1aSEdward Tomasz Napierala new_entry->cred = NULL;
45229f701172SKonstantin Belousov vm_map_entry_link(new_map, new_entry);
45232a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry);
4524bd7e5f99SJohn Dyson vm_map_copy_entry(old_map, new_map, old_entry,
45253364c323SKonstantin Belousov new_entry, fork_charge);
452678022527SKonstantin Belousov vm_map_entry_set_vnode_text(new_entry, true);
4527df8bae1dSRodney W. Grimes break;
452878d7964bSXin LI
452978d7964bSXin LI case VM_INHERIT_ZERO:
453078d7964bSXin LI /*
453178d7964bSXin LI * Create a new anonymous mapping entry modelled from
453278d7964bSXin LI * the old one.
453378d7964bSXin LI */
453478d7964bSXin LI new_entry = vm_map_entry_create(new_map);
453578d7964bSXin LI memset(new_entry, 0, sizeof(*new_entry));
453678d7964bSXin LI
453778d7964bSXin LI new_entry->start = old_entry->start;
453878d7964bSXin LI new_entry->end = old_entry->end;
453978d7964bSXin LI new_entry->eflags = old_entry->eflags &
454078d7964bSXin LI ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
4541e2e80fb3SKonstantin Belousov MAP_ENTRY_WRITECNT | MAP_ENTRY_VN_EXEC |
4542e2e80fb3SKonstantin Belousov MAP_ENTRY_SPLIT_BOUNDARY_MASK);
454378d7964bSXin LI new_entry->protection = old_entry->protection;
454478d7964bSXin LI new_entry->max_protection = old_entry->max_protection;
454578d7964bSXin LI new_entry->inheritance = VM_INHERIT_ZERO;
454678d7964bSXin LI
45479f701172SKonstantin Belousov vm_map_entry_link(new_map, new_entry);
454878d7964bSXin LI vmspace_map_entry_forked(vm1, vm2, new_entry);
454978d7964bSXin LI
455078d7964bSXin LI new_entry->cred = curthread->td_ucred;
455178d7964bSXin LI crhold(new_entry->cred);
455278d7964bSXin LI *fork_charge += (new_entry->end - new_entry->start);
455378d7964bSXin LI
455478d7964bSXin LI break;
4555df8bae1dSRodney W. Grimes }
4556df8bae1dSRodney W. Grimes }
455784110e7eSKonstantin Belousov /*
455884110e7eSKonstantin Belousov * Use inlined vm_map_unlock() to postpone handling the deferred
455984110e7eSKonstantin Belousov * map entries, which cannot be done until both old_map and
456084110e7eSKonstantin Belousov * new_map locks are released.
456184110e7eSKonstantin Belousov */
456284110e7eSKonstantin Belousov sx_xunlock(&old_map->lock);
456384110e7eSKonstantin Belousov sx_xunlock(&new_map->lock);
456484110e7eSKonstantin Belousov vm_map_process_deferred();
4565df8bae1dSRodney W. Grimes
4566df8bae1dSRodney W. Grimes return (vm2);
4567df8bae1dSRodney W. Grimes }
4568df8bae1dSRodney W. Grimes
45698056df6eSAlan Cox /*
45708056df6eSAlan Cox * Create a process's stack for exec_new_vmspace(). This function is never
45718056df6eSAlan Cox * asked to wire the newly created stack.
45728056df6eSAlan Cox */
457394f7e29aSAlan Cox int
vm_map_stack(vm_map_t map,vm_offset_t addrbos,vm_size_t max_ssize,vm_prot_t prot,vm_prot_t max,int cow)457494f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
457594f7e29aSAlan Cox vm_prot_t prot, vm_prot_t max, int cow)
457694f7e29aSAlan Cox {
45774648ba0aSKonstantin Belousov vm_size_t growsize, init_ssize;
45788056df6eSAlan Cox rlim_t vmemlim;
45794648ba0aSKonstantin Belousov int rv;
45804648ba0aSKonstantin Belousov
45818056df6eSAlan Cox MPASS((map->flags & MAP_WIREFUTURE) == 0);
45824648ba0aSKonstantin Belousov growsize = sgrowsiz;
45834648ba0aSKonstantin Belousov init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
45844648ba0aSKonstantin Belousov vm_map_lock(map);
4585f6f6d240SMateusz Guzik vmemlim = lim_cur(curthread, RLIMIT_VMEM);
45864648ba0aSKonstantin Belousov /* If we would blow our VMEM resource limit, no go */
45874648ba0aSKonstantin Belousov if (map->size + init_ssize > vmemlim) {
45884648ba0aSKonstantin Belousov rv = KERN_NO_SPACE;
45894648ba0aSKonstantin Belousov goto out;
45904648ba0aSKonstantin Belousov }
4591e1f92cccSAlan Cox rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
45924648ba0aSKonstantin Belousov max, cow);
45934648ba0aSKonstantin Belousov out:
45944648ba0aSKonstantin Belousov vm_map_unlock(map);
45954648ba0aSKonstantin Belousov return (rv);
45964648ba0aSKonstantin Belousov }
45974648ba0aSKonstantin Belousov
459819f49ad3SKonstantin Belousov static int stack_guard_page = 1;
459919f49ad3SKonstantin Belousov SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
460019f49ad3SKonstantin Belousov &stack_guard_page, 0,
460119f49ad3SKonstantin Belousov "Specifies the number of guard pages for a stack that grows");
460219f49ad3SKonstantin Belousov
46034648ba0aSKonstantin Belousov static int
vm_map_stack_locked(vm_map_t map,vm_offset_t addrbos,vm_size_t max_ssize,vm_size_t growsize,vm_prot_t prot,vm_prot_t max,int cow)46044648ba0aSKonstantin Belousov vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
46054648ba0aSKonstantin Belousov vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
46064648ba0aSKonstantin Belousov {
460721e45c30SKonstantin Belousov vm_map_entry_t gap_entry, new_entry, prev_entry;
460819bd0d9cSKonstantin Belousov vm_offset_t bot, gap_bot, gap_top, top;
460919f49ad3SKonstantin Belousov vm_size_t init_ssize, sgp;
4610fd75d710SMarcel Moolenaar int orient, rv;
461194f7e29aSAlan Cox
4612fd75d710SMarcel Moolenaar /*
4613fd75d710SMarcel Moolenaar * The stack orientation is piggybacked with the cow argument.
4614fd75d710SMarcel Moolenaar * Extract it into orient and mask the cow argument so that we
4615fd75d710SMarcel Moolenaar * don't pass it around further.
4616fd75d710SMarcel Moolenaar */
4617fd75d710SMarcel Moolenaar orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
4618fd75d710SMarcel Moolenaar KASSERT(orient != 0, ("No stack grow direction"));
461919bd0d9cSKonstantin Belousov KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
462019bd0d9cSKonstantin Belousov ("bi-dir stack"));
4621fd75d710SMarcel Moolenaar
46220f1e6ec5SMark Johnston if (max_ssize == 0 ||
46230f1e6ec5SMark Johnston !vm_map_range_valid(map, addrbos, addrbos + max_ssize))
46249410cd7dSKonstantin Belousov return (KERN_INVALID_ADDRESS);
4625156e8654SKonstantin Belousov sgp = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4626156e8654SKonstantin Belousov (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4627fe69291fSKonstantin Belousov (vm_size_t)stack_guard_page * PAGE_SIZE;
46289410cd7dSKonstantin Belousov if (sgp >= max_ssize)
46299410cd7dSKonstantin Belousov return (KERN_INVALID_ARGUMENT);
4630fd75d710SMarcel Moolenaar
463119f49ad3SKonstantin Belousov init_ssize = growsize;
463219f49ad3SKonstantin Belousov if (max_ssize < init_ssize + sgp)
463319f49ad3SKonstantin Belousov init_ssize = max_ssize - sgp;
463494f7e29aSAlan Cox
463594f7e29aSAlan Cox /* If addr is already mapped, no go */
4636d1d3f7e1SDoug Moore if (vm_map_lookup_entry(map, addrbos, &prev_entry))
463794f7e29aSAlan Cox return (KERN_NO_SPACE);
4638a69ac174SMatthew Dillon
4639fd75d710SMarcel Moolenaar /*
4640763df3ecSPedro F. Giffuni * If we can't accommodate max_ssize in the current mapping, no go.
464194f7e29aSAlan Cox */
46427cdcf863SDoug Moore if (vm_map_entry_succ(prev_entry)->start < addrbos + max_ssize)
464394f7e29aSAlan Cox return (KERN_NO_SPACE);
464494f7e29aSAlan Cox
4645fd75d710SMarcel Moolenaar /*
4646fd75d710SMarcel Moolenaar * We initially map a stack of only init_ssize. We will grow as
4647fd75d710SMarcel Moolenaar * needed later. Depending on the orientation of the stack (i.e.
4648fd75d710SMarcel Moolenaar * the grow direction) we either map at the top of the range, the
4649fd75d710SMarcel Moolenaar * bottom of the range or in the middle.
465094f7e29aSAlan Cox *
4651fd75d710SMarcel Moolenaar * Note: we would normally expect prot and max to be VM_PROT_ALL,
4652fd75d710SMarcel Moolenaar * and cow to be 0. Possibly we should eliminate these as input
4653fd75d710SMarcel Moolenaar * parameters, and just pass these values here in the insert call.
465494f7e29aSAlan Cox */
465519bd0d9cSKonstantin Belousov if (orient == MAP_STACK_GROWS_DOWN) {
4656fd75d710SMarcel Moolenaar bot = addrbos + max_ssize - init_ssize;
4657fd75d710SMarcel Moolenaar top = bot + init_ssize;
465819bd0d9cSKonstantin Belousov gap_bot = addrbos;
465919bd0d9cSKonstantin Belousov gap_top = bot;
466019bd0d9cSKonstantin Belousov } else /* if (orient == MAP_STACK_GROWS_UP) */ {
466119bd0d9cSKonstantin Belousov bot = addrbos;
466219bd0d9cSKonstantin Belousov top = bot + init_ssize;
466319bd0d9cSKonstantin Belousov gap_bot = top;
466419bd0d9cSKonstantin Belousov gap_top = addrbos + max_ssize;
466519bd0d9cSKonstantin Belousov }
4666ba41b0deSKonstantin Belousov rv = vm_map_insert1(map, NULL, 0, bot, top, prot, max, cow,
4667ba41b0deSKonstantin Belousov &new_entry);
466819bd0d9cSKonstantin Belousov if (rv != KERN_SUCCESS)
466919bd0d9cSKonstantin Belousov return (rv);
467019bd0d9cSKonstantin Belousov KASSERT(new_entry->end == top || new_entry->start == bot,
467119bd0d9cSKonstantin Belousov ("Bad entry start/end for new stack entry"));
4672712efe66SAlan Cox KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4673712efe66SAlan Cox (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4674712efe66SAlan Cox ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4675712efe66SAlan Cox KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4676712efe66SAlan Cox (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4677712efe66SAlan Cox ("new entry lacks MAP_ENTRY_GROWS_UP"));
4678fe69291fSKonstantin Belousov if (gap_bot == gap_top)
4679fe69291fSKonstantin Belousov return (KERN_SUCCESS);
4680ba41b0deSKonstantin Belousov rv = vm_map_insert1(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
468119bd0d9cSKonstantin Belousov VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
4682ba41b0deSKonstantin Belousov MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP), &gap_entry);
4683a7751d32SKonstantin Belousov if (rv == KERN_SUCCESS) {
4684ba41b0deSKonstantin Belousov KASSERT((gap_entry->eflags & MAP_ENTRY_GUARD) != 0,
4685ba41b0deSKonstantin Belousov ("entry %p not gap %#x", gap_entry, gap_entry->eflags));
4686ba41b0deSKonstantin Belousov KASSERT((gap_entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4687ba41b0deSKonstantin Belousov MAP_ENTRY_STACK_GAP_UP)) != 0,
4688ba41b0deSKonstantin Belousov ("entry %p not stack gap %#x", gap_entry,
4689ba41b0deSKonstantin Belousov gap_entry->eflags));
4690ba41b0deSKonstantin Belousov
4691a7751d32SKonstantin Belousov /*
4692a7751d32SKonstantin Belousov * Gap can never successfully handle a fault, so
4693a7751d32SKonstantin Belousov * read-ahead logic is never used for it. Re-use
4694a7751d32SKonstantin Belousov * next_read of the gap entry to store
4695a7751d32SKonstantin Belousov * stack_guard_page for vm_map_growstack().
469621e45c30SKonstantin Belousov * Similarly, since a gap cannot have a backing object,
469721e45c30SKonstantin Belousov * store the original stack protections in the
469821e45c30SKonstantin Belousov * object offset.
4699a7751d32SKonstantin Belousov */
470021e45c30SKonstantin Belousov gap_entry->next_read = sgp;
4701aa928a52SKonstantin Belousov gap_entry->offset = prot | PROT_MAX(max);
4702a7751d32SKonstantin Belousov } else {
470319bd0d9cSKonstantin Belousov (void)vm_map_delete(map, bot, top);
4704a7751d32SKonstantin Belousov }
470594f7e29aSAlan Cox return (rv);
470694f7e29aSAlan Cox }
470794f7e29aSAlan Cox
470819bd0d9cSKonstantin Belousov /*
470919bd0d9cSKonstantin Belousov * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we
471019bd0d9cSKonstantin Belousov * successfully grow the stack.
471194f7e29aSAlan Cox */
471219bd0d9cSKonstantin Belousov static int
vm_map_growstack(vm_map_t map,vm_offset_t addr,vm_map_entry_t gap_entry)471319bd0d9cSKonstantin Belousov vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
471494f7e29aSAlan Cox {
471519bd0d9cSKonstantin Belousov vm_map_entry_t stack_entry;
471619bd0d9cSKonstantin Belousov struct proc *p;
471719bd0d9cSKonstantin Belousov struct vmspace *vm;
471819bd0d9cSKonstantin Belousov struct ucred *cred;
471919bd0d9cSKonstantin Belousov vm_offset_t gap_end, gap_start, grow_start;
4720b6037edbSKonstantin Belousov vm_size_t grow_amount, guard, max_grow, sgp;
4721aa928a52SKonstantin Belousov vm_prot_t prot, max;
47227e19eda4SAndrey Zonov rlim_t lmemlim, stacklim, vmemlim;
4723b8ebd99aSJohn Baldwin int rv, rv1 __diagused;
472419bd0d9cSKonstantin Belousov bool gap_deleted, grow_down, is_procstack;
47251ba5ad42SEdward Tomasz Napierala #ifdef notyet
47261ba5ad42SEdward Tomasz Napierala uint64_t limit;
47271ba5ad42SEdward Tomasz Napierala #endif
4728afcc55f3SEdward Tomasz Napierala #ifdef RACCT
4729b8ebd99aSJohn Baldwin int error __diagused;
4730afcc55f3SEdward Tomasz Napierala #endif
473123955314SAlfred Perlstein
473219bd0d9cSKonstantin Belousov p = curproc;
473319bd0d9cSKonstantin Belousov vm = p->p_vmspace;
4734eb5ea878SKonstantin Belousov
4735eb5ea878SKonstantin Belousov /*
4736eb5ea878SKonstantin Belousov * Disallow stack growth when the access is performed by a
4737eb5ea878SKonstantin Belousov * debugger or AIO daemon. The reason is that the wrong
4738eb5ea878SKonstantin Belousov * resource limits are applied.
4739eb5ea878SKonstantin Belousov */
474010ae16c7SKonstantin Belousov if (p != initproc && (map != &p->p_vmspace->vm_map ||
474110ae16c7SKonstantin Belousov p->p_textvp == NULL))
4742f758aaddSKonstantin Belousov return (KERN_FAILURE);
4743eb5ea878SKonstantin Belousov
474419bd0d9cSKonstantin Belousov MPASS(!map->system_map);
474519bd0d9cSKonstantin Belousov
4746f6f6d240SMateusz Guzik lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4747f6f6d240SMateusz Guzik stacklim = lim_cur(curthread, RLIMIT_STACK);
4748f6f6d240SMateusz Guzik vmemlim = lim_cur(curthread, RLIMIT_VMEM);
474919bd0d9cSKonstantin Belousov retry:
475019bd0d9cSKonstantin Belousov /* If addr is not in a hole for a stack grow area, no need to grow. */
4751d1d3f7e1SDoug Moore if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
475219bd0d9cSKonstantin Belousov return (KERN_FAILURE);
475319bd0d9cSKonstantin Belousov if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
47540cddd8f0SMatthew Dillon return (KERN_SUCCESS);
475519bd0d9cSKonstantin Belousov if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
47567cdcf863SDoug Moore stack_entry = vm_map_entry_succ(gap_entry);
475719bd0d9cSKonstantin Belousov if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
475819bd0d9cSKonstantin Belousov stack_entry->start != gap_entry->end)
475919bd0d9cSKonstantin Belousov return (KERN_FAILURE);
476019bd0d9cSKonstantin Belousov grow_amount = round_page(stack_entry->start - addr);
476119bd0d9cSKonstantin Belousov grow_down = true;
476219bd0d9cSKonstantin Belousov } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
47637cdcf863SDoug Moore stack_entry = vm_map_entry_pred(gap_entry);
476419bd0d9cSKonstantin Belousov if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
476519bd0d9cSKonstantin Belousov stack_entry->end != gap_entry->start)
476619bd0d9cSKonstantin Belousov return (KERN_FAILURE);
476719bd0d9cSKonstantin Belousov grow_amount = round_page(addr + 1 - stack_entry->end);
476819bd0d9cSKonstantin Belousov grow_down = false;
4769b21a0008SMarcel Moolenaar } else {
477019bd0d9cSKonstantin Belousov return (KERN_FAILURE);
4771b21a0008SMarcel Moolenaar }
4772156e8654SKonstantin Belousov guard = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4773156e8654SKonstantin Belousov (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4774fe69291fSKonstantin Belousov gap_entry->next_read;
4775201f03b8SAlan Cox max_grow = gap_entry->end - gap_entry->start;
4776201f03b8SAlan Cox if (guard > max_grow)
4777201f03b8SAlan Cox return (KERN_NO_SPACE);
4778201f03b8SAlan Cox max_grow -= guard;
477919bd0d9cSKonstantin Belousov if (grow_amount > max_grow)
47800cddd8f0SMatthew Dillon return (KERN_NO_SPACE);
478194f7e29aSAlan Cox
4782b21a0008SMarcel Moolenaar /*
4783b21a0008SMarcel Moolenaar * If this is the main process stack, see if we're over the stack
4784b21a0008SMarcel Moolenaar * limit.
478594f7e29aSAlan Cox */
478619bd0d9cSKonstantin Belousov is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
4787becaf643SJohn Baldwin addr < (vm_offset_t)vm->vm_stacktop;
478819bd0d9cSKonstantin Belousov if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
47890cddd8f0SMatthew Dillon return (KERN_NO_SPACE);
479019bd0d9cSKonstantin Belousov
4791afcc55f3SEdward Tomasz Napierala #ifdef RACCT
47924b5c9cf6SEdward Tomasz Napierala if (racct_enable) {
47931ba5ad42SEdward Tomasz Napierala PROC_LOCK(p);
47944b5c9cf6SEdward Tomasz Napierala if (is_procstack && racct_set(p, RACCT_STACK,
47954b5c9cf6SEdward Tomasz Napierala ctob(vm->vm_ssize) + grow_amount)) {
47961ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p);
47971ba5ad42SEdward Tomasz Napierala return (KERN_NO_SPACE);
47981ba5ad42SEdward Tomasz Napierala }
47991ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p);
48004b5c9cf6SEdward Tomasz Napierala }
4801afcc55f3SEdward Tomasz Napierala #endif
480294f7e29aSAlan Cox
480319bd0d9cSKonstantin Belousov grow_amount = roundup(grow_amount, sgrowsiz);
480419bd0d9cSKonstantin Belousov if (grow_amount > max_grow)
480519bd0d9cSKonstantin Belousov grow_amount = max_grow;
480691d5354aSJohn Baldwin if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4807e4826248SAlan Cox grow_amount = trunc_page((vm_size_t)stacklim) -
4808e4826248SAlan Cox ctob(vm->vm_ssize);
480994f7e29aSAlan Cox }
481019bd0d9cSKonstantin Belousov
48111ba5ad42SEdward Tomasz Napierala #ifdef notyet
48121ba5ad42SEdward Tomasz Napierala PROC_LOCK(p);
48131ba5ad42SEdward Tomasz Napierala limit = racct_get_available(p, RACCT_STACK);
48141ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p);
48151ba5ad42SEdward Tomasz Napierala if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
48161ba5ad42SEdward Tomasz Napierala grow_amount = limit - ctob(vm->vm_ssize);
48171ba5ad42SEdward Tomasz Napierala #endif
481819bd0d9cSKonstantin Belousov
481919bd0d9cSKonstantin Belousov if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
48203ac7d297SAndrey Zonov if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
48217e19eda4SAndrey Zonov rv = KERN_NO_SPACE;
48227e19eda4SAndrey Zonov goto out;
48237e19eda4SAndrey Zonov }
48247e19eda4SAndrey Zonov #ifdef RACCT
48254b5c9cf6SEdward Tomasz Napierala if (racct_enable) {
48267e19eda4SAndrey Zonov PROC_LOCK(p);
48277e19eda4SAndrey Zonov if (racct_set(p, RACCT_MEMLOCK,
48283ac7d297SAndrey Zonov ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
48297e19eda4SAndrey Zonov PROC_UNLOCK(p);
48307e19eda4SAndrey Zonov rv = KERN_NO_SPACE;
48317e19eda4SAndrey Zonov goto out;
48327e19eda4SAndrey Zonov }
48337e19eda4SAndrey Zonov PROC_UNLOCK(p);
48344b5c9cf6SEdward Tomasz Napierala }
48357e19eda4SAndrey Zonov #endif
48367e19eda4SAndrey Zonov }
483719bd0d9cSKonstantin Belousov
4838a69ac174SMatthew Dillon /* If we would blow our VMEM resource limit, no go */
483991d5354aSJohn Baldwin if (map->size + grow_amount > vmemlim) {
48401ba5ad42SEdward Tomasz Napierala rv = KERN_NO_SPACE;
48411ba5ad42SEdward Tomasz Napierala goto out;
4842a69ac174SMatthew Dillon }
4843afcc55f3SEdward Tomasz Napierala #ifdef RACCT
48444b5c9cf6SEdward Tomasz Napierala if (racct_enable) {
48451ba5ad42SEdward Tomasz Napierala PROC_LOCK(p);
48461ba5ad42SEdward Tomasz Napierala if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
48471ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p);
48481ba5ad42SEdward Tomasz Napierala rv = KERN_NO_SPACE;
48491ba5ad42SEdward Tomasz Napierala goto out;
48501ba5ad42SEdward Tomasz Napierala }
48511ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p);
48524b5c9cf6SEdward Tomasz Napierala }
4853afcc55f3SEdward Tomasz Napierala #endif
4854a69ac174SMatthew Dillon
485519bd0d9cSKonstantin Belousov if (vm_map_lock_upgrade(map)) {
485619bd0d9cSKonstantin Belousov gap_entry = NULL;
485719bd0d9cSKonstantin Belousov vm_map_lock_read(map);
485819bd0d9cSKonstantin Belousov goto retry;
485994f7e29aSAlan Cox }
486094f7e29aSAlan Cox
486119bd0d9cSKonstantin Belousov if (grow_down) {
486221e45c30SKonstantin Belousov /*
486321e45c30SKonstantin Belousov * The gap_entry "offset" field is overloaded. See
486421e45c30SKonstantin Belousov * vm_map_stack_locked().
486521e45c30SKonstantin Belousov */
4866aa928a52SKonstantin Belousov prot = PROT_EXTRACT(gap_entry->offset);
4867aa928a52SKonstantin Belousov max = PROT_MAX_EXTRACT(gap_entry->offset);
4868b6037edbSKonstantin Belousov sgp = gap_entry->next_read;
486921e45c30SKonstantin Belousov
487019bd0d9cSKonstantin Belousov grow_start = gap_entry->end - grow_amount;
487119bd0d9cSKonstantin Belousov if (gap_entry->start + grow_amount == gap_entry->end) {
487219bd0d9cSKonstantin Belousov gap_start = gap_entry->start;
487319bd0d9cSKonstantin Belousov gap_end = gap_entry->end;
487419bd0d9cSKonstantin Belousov vm_map_entry_delete(map, gap_entry);
487519bd0d9cSKonstantin Belousov gap_deleted = true;
487619bd0d9cSKonstantin Belousov } else {
487719bd0d9cSKonstantin Belousov MPASS(gap_entry->start < gap_entry->end - grow_amount);
4878fa581662SDoug Moore vm_map_entry_resize(map, gap_entry, -grow_amount);
487919bd0d9cSKonstantin Belousov gap_deleted = false;
488019bd0d9cSKonstantin Belousov }
488119bd0d9cSKonstantin Belousov rv = vm_map_insert(map, NULL, 0, grow_start,
4882aa928a52SKonstantin Belousov grow_start + grow_amount, prot, max, MAP_STACK_GROWS_DOWN);
488319bd0d9cSKonstantin Belousov if (rv != KERN_SUCCESS) {
488419bd0d9cSKonstantin Belousov if (gap_deleted) {
4885b6037edbSKonstantin Belousov rv1 = vm_map_insert1(map, NULL, 0, gap_start,
488619bd0d9cSKonstantin Belousov gap_end, VM_PROT_NONE, VM_PROT_NONE,
4887b6037edbSKonstantin Belousov MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN,
4888b6037edbSKonstantin Belousov &gap_entry);
488919bd0d9cSKonstantin Belousov MPASS(rv1 == KERN_SUCCESS);
4890b6037edbSKonstantin Belousov gap_entry->next_read = sgp;
4891b6037edbSKonstantin Belousov gap_entry->offset = prot | PROT_MAX(max);
48921895f520SDoug Moore } else
4893fa581662SDoug Moore vm_map_entry_resize(map, gap_entry,
48941895f520SDoug Moore grow_amount);
489594f7e29aSAlan Cox }
4896b21a0008SMarcel Moolenaar } else {
489719bd0d9cSKonstantin Belousov grow_start = stack_entry->end;
4898ef694c1aSEdward Tomasz Napierala cred = stack_entry->cred;
4899ef694c1aSEdward Tomasz Napierala if (cred == NULL && stack_entry->object.vm_object != NULL)
4900ef694c1aSEdward Tomasz Napierala cred = stack_entry->object.vm_object->cred;
4901ef694c1aSEdward Tomasz Napierala if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
49023364c323SKonstantin Belousov rv = KERN_NO_SPACE;
4903b21a0008SMarcel Moolenaar /* Grow the underlying object if applicable. */
49043364c323SKonstantin Belousov else if (stack_entry->object.vm_object == NULL ||
4905b21a0008SMarcel Moolenaar vm_object_coalesce(stack_entry->object.vm_object,
490657a21abaSAlan Cox stack_entry->offset,
4907b21a0008SMarcel Moolenaar (vm_size_t)(stack_entry->end - stack_entry->start),
4908fa581662SDoug Moore grow_amount, cred != NULL)) {
4909fa581662SDoug Moore if (gap_entry->start + grow_amount == gap_entry->end) {
491019bd0d9cSKonstantin Belousov vm_map_entry_delete(map, gap_entry);
4911fa581662SDoug Moore vm_map_entry_resize(map, stack_entry,
4912fa581662SDoug Moore grow_amount);
4913fa581662SDoug Moore } else {
491419bd0d9cSKonstantin Belousov gap_entry->start += grow_amount;
4915fa581662SDoug Moore stack_entry->end += grow_amount;
4916fa581662SDoug Moore }
491719bd0d9cSKonstantin Belousov map->size += grow_amount;
4918b21a0008SMarcel Moolenaar rv = KERN_SUCCESS;
4919b21a0008SMarcel Moolenaar } else
4920b21a0008SMarcel Moolenaar rv = KERN_FAILURE;
4921b21a0008SMarcel Moolenaar }
4922b21a0008SMarcel Moolenaar if (rv == KERN_SUCCESS && is_procstack)
4923b21a0008SMarcel Moolenaar vm->vm_ssize += btoc(grow_amount);
4924b21a0008SMarcel Moolenaar
4925abd498aaSBruce M Simpson /*
4926abd498aaSBruce M Simpson * Heed the MAP_WIREFUTURE flag if it was set for this process.
4927abd498aaSBruce M Simpson */
492819bd0d9cSKonstantin Belousov if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
492954a3a114SMark Johnston rv = vm_map_wire_locked(map, grow_start,
493054a3a114SMark Johnston grow_start + grow_amount,
4931212e02c8SKonstantin Belousov VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
493254a3a114SMark Johnston }
493319bd0d9cSKonstantin Belousov vm_map_lock_downgrade(map);
4934abd498aaSBruce M Simpson
49351ba5ad42SEdward Tomasz Napierala out:
4936afcc55f3SEdward Tomasz Napierala #ifdef RACCT
49374b5c9cf6SEdward Tomasz Napierala if (racct_enable && rv != KERN_SUCCESS) {
49381ba5ad42SEdward Tomasz Napierala PROC_LOCK(p);
49391ba5ad42SEdward Tomasz Napierala error = racct_set(p, RACCT_VMEM, map->size);
49401ba5ad42SEdward Tomasz Napierala KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
49417e19eda4SAndrey Zonov if (!old_mlock) {
49427e19eda4SAndrey Zonov error = racct_set(p, RACCT_MEMLOCK,
49433ac7d297SAndrey Zonov ptoa(pmap_wired_count(map->pmap)));
49447e19eda4SAndrey Zonov KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
49457e19eda4SAndrey Zonov }
49461ba5ad42SEdward Tomasz Napierala error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
49471ba5ad42SEdward Tomasz Napierala KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
49481ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p);
49491ba5ad42SEdward Tomasz Napierala }
4950afcc55f3SEdward Tomasz Napierala #endif
49511ba5ad42SEdward Tomasz Napierala
49520cddd8f0SMatthew Dillon return (rv);
495394f7e29aSAlan Cox }
495494f7e29aSAlan Cox
4955df8bae1dSRodney W. Grimes /*
49565856e12eSJohn Dyson * Unshare the specified VM space for exec. If other processes are
49575856e12eSJohn Dyson * mapped to it, then create a new one. The new vmspace is null.
49585856e12eSJohn Dyson */
495989b57fcfSKonstantin Belousov int
vmspace_exec(struct proc * p,vm_offset_t minuser,vm_offset_t maxuser)49603ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
49611b40f8c0SMatthew Dillon {
49625856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace;
49635856e12eSJohn Dyson struct vmspace *newvmspace;
49645856e12eSJohn Dyson
49657032434eSKonstantin Belousov KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
49667032434eSKonstantin Belousov ("vmspace_exec recursed"));
49676e00f3a3SKonstantin Belousov newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
496889b57fcfSKonstantin Belousov if (newvmspace == NULL)
496989b57fcfSKonstantin Belousov return (ENOMEM);
497051ab6c28SAlan Cox newvmspace->vm_swrss = oldvmspace->vm_swrss;
49715856e12eSJohn Dyson /*
49725856e12eSJohn Dyson * This code is written like this for prototype purposes. The
49735856e12eSJohn Dyson * goal is to avoid running down the vmspace here, but let the
49745856e12eSJohn Dyson * other process's that are still using the vmspace to finally
49755856e12eSJohn Dyson * run it down. Even though there is little or no chance of blocking
49765856e12eSJohn Dyson * here, it is a good idea to keep this form for future mods.
49775856e12eSJohn Dyson */
497857051fdcSTor Egge PROC_VMSPACE_LOCK(p);
49795856e12eSJohn Dyson p->p_vmspace = newvmspace;
498057051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
49816617724cSJeff Roberson if (p == curthread->td_proc)
4982b40ce416SJulian Elischer pmap_activate(curthread);
49837032434eSKonstantin Belousov curthread->td_pflags |= TDP_EXECVMSPC;
498489b57fcfSKonstantin Belousov return (0);
49855856e12eSJohn Dyson }
49865856e12eSJohn Dyson
49875856e12eSJohn Dyson /*
49885856e12eSJohn Dyson * Unshare the specified VM space for forcing COW. This
49895856e12eSJohn Dyson * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
49905856e12eSJohn Dyson */
499189b57fcfSKonstantin Belousov int
vmspace_unshare(struct proc * p)49921b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
49931b40f8c0SMatthew Dillon {
49945856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace;
49955856e12eSJohn Dyson struct vmspace *newvmspace;
49963364c323SKonstantin Belousov vm_ooffset_t fork_charge;
49975856e12eSJohn Dyson
49989246b309SMark Johnston /*
49999246b309SMark Johnston * The caller is responsible for ensuring that the reference count
50009246b309SMark Johnston * cannot concurrently transition 1 -> 2.
50019246b309SMark Johnston */
5002f7db0c95SMark Johnston if (refcount_load(&oldvmspace->vm_refcnt) == 1)
500389b57fcfSKonstantin Belousov return (0);
50043364c323SKonstantin Belousov fork_charge = 0;
50053364c323SKonstantin Belousov newvmspace = vmspace_fork(oldvmspace, &fork_charge);
500689b57fcfSKonstantin Belousov if (newvmspace == NULL)
500789b57fcfSKonstantin Belousov return (ENOMEM);
5008ef694c1aSEdward Tomasz Napierala if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
50093364c323SKonstantin Belousov vmspace_free(newvmspace);
50103364c323SKonstantin Belousov return (ENOMEM);
50113364c323SKonstantin Belousov }
501257051fdcSTor Egge PROC_VMSPACE_LOCK(p);
50135856e12eSJohn Dyson p->p_vmspace = newvmspace;
501457051fdcSTor Egge PROC_VMSPACE_UNLOCK(p);
50156617724cSJeff Roberson if (p == curthread->td_proc)
5016b40ce416SJulian Elischer pmap_activate(curthread);
5017b56ef1c1SJohn Baldwin vmspace_free(oldvmspace);
501889b57fcfSKonstantin Belousov return (0);
50195856e12eSJohn Dyson }
50205856e12eSJohn Dyson
50215856e12eSJohn Dyson /*
5022df8bae1dSRodney W. Grimes * vm_map_lookup:
5023df8bae1dSRodney W. Grimes *
5024df8bae1dSRodney W. Grimes * Finds the VM object, offset, and
5025df8bae1dSRodney W. Grimes * protection for a given virtual address in the
5026df8bae1dSRodney W. Grimes * specified map, assuming a page fault of the
5027df8bae1dSRodney W. Grimes * type specified.
5028df8bae1dSRodney W. Grimes *
5029df8bae1dSRodney W. Grimes * Leaves the map in question locked for read; return
5030df8bae1dSRodney W. Grimes * values are guaranteed until a vm_map_lookup_done
5031df8bae1dSRodney W. Grimes * call is performed. Note that the map argument
5032df8bae1dSRodney W. Grimes * is in/out; the returned map must be used in
5033df8bae1dSRodney W. Grimes * the call to vm_map_lookup_done.
5034df8bae1dSRodney W. Grimes *
5035df8bae1dSRodney W. Grimes * A handle (out_entry) is returned for use in
5036df8bae1dSRodney W. Grimes * vm_map_lookup_done, to make that fast.
5037df8bae1dSRodney W. Grimes *
5038df8bae1dSRodney W. Grimes * If a lookup is requested with "write protection"
5039df8bae1dSRodney W. Grimes * specified, the map may be changed to perform virtual
5040df8bae1dSRodney W. Grimes * copying operations, although the data referenced will
5041df8bae1dSRodney W. Grimes * remain the same.
5042df8bae1dSRodney W. Grimes */
5043df8bae1dSRodney W. Grimes int
vm_map_lookup(vm_map_t * var_map,vm_offset_t vaddr,vm_prot_t fault_typea,vm_map_entry_t * out_entry,vm_object_t * object,vm_pindex_t * pindex,vm_prot_t * out_prot,boolean_t * wired)5044b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
5045b9dcd593SBruce Evans vm_offset_t vaddr,
504647221757SJohn Dyson vm_prot_t fault_typea,
5047b9dcd593SBruce Evans vm_map_entry_t *out_entry, /* OUT */
5048b9dcd593SBruce Evans vm_object_t *object, /* OUT */
5049b9dcd593SBruce Evans vm_pindex_t *pindex, /* OUT */
5050b9dcd593SBruce Evans vm_prot_t *out_prot, /* OUT */
50512d8acc0fSJohn Dyson boolean_t *wired) /* OUT */
5052df8bae1dSRodney W. Grimes {
5053c0877f10SJohn Dyson vm_map_entry_t entry;
5054c0877f10SJohn Dyson vm_map_t map = *var_map;
5055c0877f10SJohn Dyson vm_prot_t prot;
5056a6f21d15SMark Johnston vm_prot_t fault_type;
50573364c323SKonstantin Belousov vm_object_t eobject;
50580cc74f14SAlan Cox vm_size_t size;
5059ef694c1aSEdward Tomasz Napierala struct ucred *cred;
5060df8bae1dSRodney W. Grimes
506119bd0d9cSKonstantin Belousov RetryLookup:
5062df8bae1dSRodney W. Grimes
5063df8bae1dSRodney W. Grimes vm_map_lock_read(map);
5064df8bae1dSRodney W. Grimes
506519bd0d9cSKonstantin Belousov RetryLookupLocked:
5066df8bae1dSRodney W. Grimes /*
50674c3ef59eSAlan Cox * Lookup the faulting address.
5068df8bae1dSRodney W. Grimes */
5069095104acSAlan Cox if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
5070095104acSAlan Cox vm_map_unlock_read(map);
5071095104acSAlan Cox return (KERN_INVALID_ADDRESS);
5072095104acSAlan Cox }
5073df8bae1dSRodney W. Grimes
50744e94f402SAlan Cox entry = *out_entry;
5075b7b2aac2SJohn Dyson
5076df8bae1dSRodney W. Grimes /*
5077df8bae1dSRodney W. Grimes * Handle submaps.
5078df8bae1dSRodney W. Grimes */
5079afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
5080df8bae1dSRodney W. Grimes vm_map_t old_map = map;
5081df8bae1dSRodney W. Grimes
5082df8bae1dSRodney W. Grimes *var_map = map = entry->object.sub_map;
5083df8bae1dSRodney W. Grimes vm_map_unlock_read(old_map);
5084df8bae1dSRodney W. Grimes goto RetryLookup;
5085df8bae1dSRodney W. Grimes }
5086a04c970aSJohn Dyson
5087df8bae1dSRodney W. Grimes /*
50880d94caffSDavid Greenman * Check whether this task is allowed to have this page.
5089df8bae1dSRodney W. Grimes */
5090df8bae1dSRodney W. Grimes prot = entry->protection;
509119bd0d9cSKonstantin Belousov if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
509219bd0d9cSKonstantin Belousov fault_typea &= ~VM_PROT_FAULT_LOOKUP;
509319bd0d9cSKonstantin Belousov if (prot == VM_PROT_NONE && map != kernel_map &&
509419bd0d9cSKonstantin Belousov (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
509519bd0d9cSKonstantin Belousov (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
509619bd0d9cSKonstantin Belousov MAP_ENTRY_STACK_GAP_UP)) != 0 &&
509719bd0d9cSKonstantin Belousov vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
509819bd0d9cSKonstantin Belousov goto RetryLookupLocked;
509919bd0d9cSKonstantin Belousov }
5100a6f21d15SMark Johnston fault_type = fault_typea & VM_PROT_ALL;
51012db65ab4SAlan Cox if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
5102095104acSAlan Cox vm_map_unlock_read(map);
5103095104acSAlan Cox return (KERN_PROTECTION_FAILURE);
510447221757SJohn Dyson }
5105b8db9776SKonstantin Belousov KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
5106b8db9776SKonstantin Belousov (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
5107b8db9776SKonstantin Belousov (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
5108b8db9776SKonstantin Belousov ("entry %p flags %x", entry, entry->eflags));
51095b3e0257SDag-Erling Smørgrav if ((fault_typea & VM_PROT_COPY) != 0 &&
51105b3e0257SDag-Erling Smørgrav (entry->max_protection & VM_PROT_WRITE) == 0 &&
51115b3e0257SDag-Erling Smørgrav (entry->eflags & MAP_ENTRY_COW) == 0) {
51125b3e0257SDag-Erling Smørgrav vm_map_unlock_read(map);
51135b3e0257SDag-Erling Smørgrav return (KERN_PROTECTION_FAILURE);
51145b3e0257SDag-Erling Smørgrav }
5115df8bae1dSRodney W. Grimes
5116df8bae1dSRodney W. Grimes /*
51170d94caffSDavid Greenman * If this page is not pageable, we have to get it for all possible
51180d94caffSDavid Greenman * accesses.
5119df8bae1dSRodney W. Grimes */
512005f0fdd2SPoul-Henning Kamp *wired = (entry->wired_count != 0);
512105f0fdd2SPoul-Henning Kamp if (*wired)
5122a6d42a0dSAlan Cox fault_type = entry->protection;
51233364c323SKonstantin Belousov size = entry->end - entry->start;
512467388836SKonstantin Belousov
5125df8bae1dSRodney W. Grimes /*
5126df8bae1dSRodney W. Grimes * If the entry was copy-on-write, we either ...
5127df8bae1dSRodney W. Grimes */
5128afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
5129df8bae1dSRodney W. Grimes /*
51300d94caffSDavid Greenman * If we want to write the page, we may as well handle that
5131ad5fca3bSAlan Cox * now since we've got the map locked.
5132df8bae1dSRodney W. Grimes *
51330d94caffSDavid Greenman * If we don't need to write the page, we just demote the
51340d94caffSDavid Greenman * permissions allowed.
5135df8bae1dSRodney W. Grimes */
5136a6d42a0dSAlan Cox if ((fault_type & VM_PROT_WRITE) != 0 ||
5137a6d42a0dSAlan Cox (fault_typea & VM_PROT_COPY) != 0) {
5138df8bae1dSRodney W. Grimes /*
51390d94caffSDavid Greenman * Make a new object, and place it in the object
51400d94caffSDavid Greenman * chain. Note that no new references have appeared
5141ad5fca3bSAlan Cox * -- one just moved from the map to the new
51420d94caffSDavid Greenman * object.
5143df8bae1dSRodney W. Grimes */
514425adb370SBrian Feldman if (vm_map_lock_upgrade(map))
5145df8bae1dSRodney W. Grimes goto RetryLookup;
51469917e010SAlan Cox
5147ef694c1aSEdward Tomasz Napierala if (entry->cred == NULL) {
51483364c323SKonstantin Belousov /*
51493364c323SKonstantin Belousov * The debugger owner is charged for
51503364c323SKonstantin Belousov * the memory.
51513364c323SKonstantin Belousov */
5152ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred;
5153ef694c1aSEdward Tomasz Napierala crhold(cred);
5154ef694c1aSEdward Tomasz Napierala if (!swap_reserve_by_cred(size, cred)) {
5155ef694c1aSEdward Tomasz Napierala crfree(cred);
51563364c323SKonstantin Belousov vm_map_unlock(map);
51573364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE);
51583364c323SKonstantin Belousov }
5159ef694c1aSEdward Tomasz Napierala entry->cred = cred;
51603364c323SKonstantin Belousov }
51613364c323SKonstantin Belousov eobject = entry->object.vm_object;
516267388836SKonstantin Belousov vm_object_shadow(&entry->object.vm_object,
516367388836SKonstantin Belousov &entry->offset, size, entry->cred, false);
516467388836SKonstantin Belousov if (eobject == entry->object.vm_object) {
51653364c323SKonstantin Belousov /*
51663364c323SKonstantin Belousov * The object was not shadowed.
51673364c323SKonstantin Belousov */
5168ef694c1aSEdward Tomasz Napierala swap_release_by_cred(size, entry->cred);
5169ef694c1aSEdward Tomasz Napierala crfree(entry->cred);
51703364c323SKonstantin Belousov }
517167388836SKonstantin Belousov entry->cred = NULL;
517267388836SKonstantin Belousov entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
51739917e010SAlan Cox
51749b09b6c7SMatthew Dillon vm_map_lock_downgrade(map);
51750d94caffSDavid Greenman } else {
5176df8bae1dSRodney W. Grimes /*
51770d94caffSDavid Greenman * We're attempting to read a copy-on-write page --
51780d94caffSDavid Greenman * don't allow writes.
5179df8bae1dSRodney W. Grimes */
51802d8acc0fSJohn Dyson prot &= ~VM_PROT_WRITE;
5181df8bae1dSRodney W. Grimes }
5182df8bae1dSRodney W. Grimes }
51832d8acc0fSJohn Dyson
5184df8bae1dSRodney W. Grimes /*
5185df8bae1dSRodney W. Grimes * Create an object if necessary.
5186df8bae1dSRodney W. Grimes */
518767388836SKonstantin Belousov if (entry->object.vm_object == NULL && !map->system_map) {
518825adb370SBrian Feldman if (vm_map_lock_upgrade(map))
5189df8bae1dSRodney W. Grimes goto RetryLookup;
519067388836SKonstantin Belousov entry->object.vm_object = vm_object_allocate_anon(atop(size),
519170b29961SMark Johnston NULL, entry->cred, size);
5192df8bae1dSRodney W. Grimes entry->offset = 0;
5193ef694c1aSEdward Tomasz Napierala entry->cred = NULL;
51949b09b6c7SMatthew Dillon vm_map_lock_downgrade(map);
5195df8bae1dSRodney W. Grimes }
5196b5b40fa6SJohn Dyson
5197df8bae1dSRodney W. Grimes /*
51980d94caffSDavid Greenman * Return the object/offset from this entry. If the entry was
51990d94caffSDavid Greenman * copy-on-write or empty, it has been fixed up.
5200df8bae1dSRodney W. Grimes */
520110d9120cSKonstantin Belousov *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
5202df8bae1dSRodney W. Grimes *object = entry->object.vm_object;
5203df8bae1dSRodney W. Grimes
5204df8bae1dSRodney W. Grimes *out_prot = prot;
5205df8bae1dSRodney W. Grimes return (KERN_SUCCESS);
5206df8bae1dSRodney W. Grimes }
5207df8bae1dSRodney W. Grimes
5208df8bae1dSRodney W. Grimes /*
520919dc5607STor Egge * vm_map_lookup_locked:
521019dc5607STor Egge *
521119dc5607STor Egge * Lookup the faulting address. A version of vm_map_lookup that returns
521219dc5607STor Egge * KERN_FAILURE instead of blocking on map lock or memory allocation.
521319dc5607STor Egge */
521419dc5607STor Egge int
vm_map_lookup_locked(vm_map_t * var_map,vm_offset_t vaddr,vm_prot_t fault_typea,vm_map_entry_t * out_entry,vm_object_t * object,vm_pindex_t * pindex,vm_prot_t * out_prot,boolean_t * wired)521519dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
521619dc5607STor Egge vm_offset_t vaddr,
521719dc5607STor Egge vm_prot_t fault_typea,
521819dc5607STor Egge vm_map_entry_t *out_entry, /* OUT */
521919dc5607STor Egge vm_object_t *object, /* OUT */
522019dc5607STor Egge vm_pindex_t *pindex, /* OUT */
522119dc5607STor Egge vm_prot_t *out_prot, /* OUT */
522219dc5607STor Egge boolean_t *wired) /* OUT */
522319dc5607STor Egge {
522419dc5607STor Egge vm_map_entry_t entry;
522519dc5607STor Egge vm_map_t map = *var_map;
522619dc5607STor Egge vm_prot_t prot;
522719dc5607STor Egge vm_prot_t fault_type = fault_typea;
522819dc5607STor Egge
522919dc5607STor Egge /*
52304c3ef59eSAlan Cox * Lookup the faulting address.
523119dc5607STor Egge */
523219dc5607STor Egge if (!vm_map_lookup_entry(map, vaddr, out_entry))
523319dc5607STor Egge return (KERN_INVALID_ADDRESS);
523419dc5607STor Egge
523519dc5607STor Egge entry = *out_entry;
523619dc5607STor Egge
523719dc5607STor Egge /*
523819dc5607STor Egge * Fail if the entry refers to a submap.
523919dc5607STor Egge */
524019dc5607STor Egge if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
524119dc5607STor Egge return (KERN_FAILURE);
524219dc5607STor Egge
524319dc5607STor Egge /*
524419dc5607STor Egge * Check whether this task is allowed to have this page.
524519dc5607STor Egge */
524619dc5607STor Egge prot = entry->protection;
524719dc5607STor Egge fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
524819dc5607STor Egge if ((fault_type & prot) != fault_type)
524919dc5607STor Egge return (KERN_PROTECTION_FAILURE);
525019dc5607STor Egge
525119dc5607STor Egge /*
525219dc5607STor Egge * If this page is not pageable, we have to get it for all possible
525319dc5607STor Egge * accesses.
525419dc5607STor Egge */
525519dc5607STor Egge *wired = (entry->wired_count != 0);
525619dc5607STor Egge if (*wired)
5257a6d42a0dSAlan Cox fault_type = entry->protection;
525819dc5607STor Egge
525919dc5607STor Egge if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
526019dc5607STor Egge /*
526119dc5607STor Egge * Fail if the entry was copy-on-write for a write fault.
526219dc5607STor Egge */
526319dc5607STor Egge if (fault_type & VM_PROT_WRITE)
526419dc5607STor Egge return (KERN_FAILURE);
526519dc5607STor Egge /*
526619dc5607STor Egge * We're attempting to read a copy-on-write page --
526719dc5607STor Egge * don't allow writes.
526819dc5607STor Egge */
526919dc5607STor Egge prot &= ~VM_PROT_WRITE;
527019dc5607STor Egge }
527119dc5607STor Egge
527219dc5607STor Egge /*
527319dc5607STor Egge * Fail if an object should be created.
527419dc5607STor Egge */
527519dc5607STor Egge if (entry->object.vm_object == NULL && !map->system_map)
527619dc5607STor Egge return (KERN_FAILURE);
527719dc5607STor Egge
527819dc5607STor Egge /*
527919dc5607STor Egge * Return the object/offset from this entry. If the entry was
528019dc5607STor Egge * copy-on-write or empty, it has been fixed up.
528119dc5607STor Egge */
528210d9120cSKonstantin Belousov *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
528319dc5607STor Egge *object = entry->object.vm_object;
528419dc5607STor Egge
528519dc5607STor Egge *out_prot = prot;
528619dc5607STor Egge return (KERN_SUCCESS);
528719dc5607STor Egge }
528819dc5607STor Egge
528919dc5607STor Egge /*
5290df8bae1dSRodney W. Grimes * vm_map_lookup_done:
5291df8bae1dSRodney W. Grimes *
5292df8bae1dSRodney W. Grimes * Releases locks acquired by a vm_map_lookup
5293df8bae1dSRodney W. Grimes * (according to the handle returned by that lookup).
5294df8bae1dSRodney W. Grimes */
52950d94caffSDavid Greenman void
vm_map_lookup_done(vm_map_t map,vm_map_entry_t entry)52961b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
5297df8bae1dSRodney W. Grimes {
5298df8bae1dSRodney W. Grimes /*
5299df8bae1dSRodney W. Grimes * Unlock the main-level map
5300df8bae1dSRodney W. Grimes */
5301df8bae1dSRodney W. Grimes vm_map_unlock_read(map);
5302df8bae1dSRodney W. Grimes }
5303df8bae1dSRodney W. Grimes
530419ea042eSKonstantin Belousov vm_offset_t
vm_map_max_KBI(const struct vm_map * map)530519ea042eSKonstantin Belousov vm_map_max_KBI(const struct vm_map *map)
530619ea042eSKonstantin Belousov {
530719ea042eSKonstantin Belousov
5308f0165b1cSKonstantin Belousov return (vm_map_max(map));
530919ea042eSKonstantin Belousov }
531019ea042eSKonstantin Belousov
531119ea042eSKonstantin Belousov vm_offset_t
vm_map_min_KBI(const struct vm_map * map)531219ea042eSKonstantin Belousov vm_map_min_KBI(const struct vm_map *map)
531319ea042eSKonstantin Belousov {
531419ea042eSKonstantin Belousov
5315f0165b1cSKonstantin Belousov return (vm_map_min(map));
531619ea042eSKonstantin Belousov }
531719ea042eSKonstantin Belousov
531819ea042eSKonstantin Belousov pmap_t
vm_map_pmap_KBI(vm_map_t map)531919ea042eSKonstantin Belousov vm_map_pmap_KBI(vm_map_t map)
532019ea042eSKonstantin Belousov {
532119ea042eSKonstantin Belousov
532219ea042eSKonstantin Belousov return (map->pmap);
532319ea042eSKonstantin Belousov }
532419ea042eSKonstantin Belousov
5325a7752896SMark Johnston bool
vm_map_range_valid_KBI(vm_map_t map,vm_offset_t start,vm_offset_t end)5326a7752896SMark Johnston vm_map_range_valid_KBI(vm_map_t map, vm_offset_t start, vm_offset_t end)
5327a7752896SMark Johnston {
5328a7752896SMark Johnston
5329a7752896SMark Johnston return (vm_map_range_valid(map, start, end));
5330a7752896SMark Johnston }
5331a7752896SMark Johnston
5332721899b1SDoug Moore #ifdef INVARIANTS
5333721899b1SDoug Moore static void
_vm_map_assert_consistent(vm_map_t map,int check)5334461587dcSDoug Moore _vm_map_assert_consistent(vm_map_t map, int check)
5335721899b1SDoug Moore {
5336721899b1SDoug Moore vm_map_entry_t entry, prev;
5337c1ad5342SDoug Moore vm_map_entry_t cur, header, lbound, ubound;
5338721899b1SDoug Moore vm_size_t max_left, max_right;
5339721899b1SDoug Moore
534085b7bedbSDoug Moore #ifdef DIAGNOSTIC
534185b7bedbSDoug Moore ++map->nupdates;
534285b7bedbSDoug Moore #endif
5343461587dcSDoug Moore if (enable_vmmap_check != check)
5344721899b1SDoug Moore return;
5345721899b1SDoug Moore
5346c1ad5342SDoug Moore header = prev = &map->header;
5347721899b1SDoug Moore VM_MAP_ENTRY_FOREACH(entry, map) {
5348721899b1SDoug Moore KASSERT(prev->end <= entry->start,
5349721899b1SDoug Moore ("map %p prev->end = %jx, start = %jx", map,
5350721899b1SDoug Moore (uintmax_t)prev->end, (uintmax_t)entry->start));
5351721899b1SDoug Moore KASSERT(entry->start < entry->end,
5352721899b1SDoug Moore ("map %p start = %jx, end = %jx", map,
5353721899b1SDoug Moore (uintmax_t)entry->start, (uintmax_t)entry->end));
5354c1ad5342SDoug Moore KASSERT(entry->left == header ||
5355721899b1SDoug Moore entry->left->start < entry->start,
5356721899b1SDoug Moore ("map %p left->start = %jx, start = %jx", map,
5357721899b1SDoug Moore (uintmax_t)entry->left->start, (uintmax_t)entry->start));
5358c1ad5342SDoug Moore KASSERT(entry->right == header ||
5359721899b1SDoug Moore entry->start < entry->right->start,
5360721899b1SDoug Moore ("map %p start = %jx, right->start = %jx", map,
5361721899b1SDoug Moore (uintmax_t)entry->start, (uintmax_t)entry->right->start));
5362c1ad5342SDoug Moore cur = map->root;
5363c1ad5342SDoug Moore lbound = ubound = header;
5364c1ad5342SDoug Moore for (;;) {
5365c1ad5342SDoug Moore if (entry->start < cur->start) {
5366c1ad5342SDoug Moore ubound = cur;
5367c1ad5342SDoug Moore cur = cur->left;
5368c1ad5342SDoug Moore KASSERT(cur != lbound,
5369c1ad5342SDoug Moore ("map %p cannot find %jx",
5370c0829bb1SMark Johnston map, (uintmax_t)entry->start));
5371c1ad5342SDoug Moore } else if (cur->end <= entry->start) {
5372c1ad5342SDoug Moore lbound = cur;
5373c1ad5342SDoug Moore cur = cur->right;
5374c1ad5342SDoug Moore KASSERT(cur != ubound,
5375c1ad5342SDoug Moore ("map %p cannot find %jx",
5376c0829bb1SMark Johnston map, (uintmax_t)entry->start));
5377c1ad5342SDoug Moore } else {
5378c1ad5342SDoug Moore KASSERT(cur == entry,
5379c1ad5342SDoug Moore ("map %p cannot find %jx",
5380c0829bb1SMark Johnston map, (uintmax_t)entry->start));
5381c1ad5342SDoug Moore break;
5382c1ad5342SDoug Moore }
5383c1ad5342SDoug Moore }
5384c1ad5342SDoug Moore max_left = vm_map_entry_max_free_left(entry, lbound);
5385c1ad5342SDoug Moore max_right = vm_map_entry_max_free_right(entry, ubound);
5386c1ad5342SDoug Moore KASSERT(entry->max_free == vm_size_max(max_left, max_right),
5387721899b1SDoug Moore ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
5388721899b1SDoug Moore (uintmax_t)entry->max_free,
5389721899b1SDoug Moore (uintmax_t)max_left, (uintmax_t)max_right));
5390721899b1SDoug Moore prev = entry;
5391721899b1SDoug Moore }
5392721899b1SDoug Moore KASSERT(prev->end <= entry->start,
5393721899b1SDoug Moore ("map %p prev->end = %jx, start = %jx", map,
5394721899b1SDoug Moore (uintmax_t)prev->end, (uintmax_t)entry->start));
5395721899b1SDoug Moore }
5396721899b1SDoug Moore #endif
5397721899b1SDoug Moore
5398c7c34a24SBruce Evans #include "opt_ddb.h"
5399c3cb3e12SDavid Greenman #ifdef DDB
5400c7c34a24SBruce Evans #include <sys/kernel.h>
5401c7c34a24SBruce Evans
5402c7c34a24SBruce Evans #include <ddb/ddb.h>
5403c7c34a24SBruce Evans
54042ebcd458SAttilio Rao static void
vm_map_print(vm_map_t map)54052ebcd458SAttilio Rao vm_map_print(vm_map_t map)
5406df8bae1dSRodney W. Grimes {
540777131528SDoug Moore vm_map_entry_t entry, prev;
5408c7c34a24SBruce Evans
5409e5f251d2SAlan Cox db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
5410e5f251d2SAlan Cox (void *)map,
5411101eeb7fSBruce Evans (void *)map->pmap, map->nentries, map->timestamp);
5412df8bae1dSRodney W. Grimes
5413c7c34a24SBruce Evans db_indent += 2;
5414721899b1SDoug Moore prev = &map->header;
5415721899b1SDoug Moore VM_MAP_ENTRY_FOREACH(entry, map) {
541619bd0d9cSKonstantin Belousov db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
541719bd0d9cSKonstantin Belousov (void *)entry, (void *)entry->start, (void *)entry->end,
541819bd0d9cSKonstantin Belousov entry->eflags);
5419e5f251d2SAlan Cox {
5420eaa17d42SRyan Libby static const char * const inheritance_name[4] =
5421df8bae1dSRodney W. Grimes {"share", "copy", "none", "donate_copy"};
54220d94caffSDavid Greenman
542395e5e988SJohn Dyson db_iprintf(" prot=%x/%x/%s",
5424df8bae1dSRodney W. Grimes entry->protection,
5425df8bae1dSRodney W. Grimes entry->max_protection,
542677131528SDoug Moore inheritance_name[(int)(unsigned char)
542777131528SDoug Moore entry->inheritance]);
5428df8bae1dSRodney W. Grimes if (entry->wired_count != 0)
542995e5e988SJohn Dyson db_printf(", wired");
5430df8bae1dSRodney W. Grimes }
54319fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
5432cd034a5bSMaxime Henrion db_printf(", share=%p, offset=0x%jx\n",
54339fdfe602SMatthew Dillon (void *)entry->object.sub_map,
5434cd034a5bSMaxime Henrion (uintmax_t)entry->offset);
543577131528SDoug Moore if (prev == &map->header ||
543677131528SDoug Moore prev->object.sub_map !=
543777131528SDoug Moore entry->object.sub_map) {
5438c7c34a24SBruce Evans db_indent += 2;
54392ebcd458SAttilio Rao vm_map_print((vm_map_t)entry->object.sub_map);
5440c7c34a24SBruce Evans db_indent -= 2;
5441df8bae1dSRodney W. Grimes }
54420d94caffSDavid Greenman } else {
5443ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL)
5444ef694c1aSEdward Tomasz Napierala db_printf(", ruid %d", entry->cred->cr_ruid);
5445cd034a5bSMaxime Henrion db_printf(", object=%p, offset=0x%jx",
5446101eeb7fSBruce Evans (void *)entry->object.vm_object,
5447cd034a5bSMaxime Henrion (uintmax_t)entry->offset);
5448ef694c1aSEdward Tomasz Napierala if (entry->object.vm_object && entry->object.vm_object->cred)
5449ef694c1aSEdward Tomasz Napierala db_printf(", obj ruid %d charge %jx",
5450ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred->cr_ruid,
54513364c323SKonstantin Belousov (uintmax_t)entry->object.vm_object->charge);
5452afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_COW)
5453c7c34a24SBruce Evans db_printf(", copy (%s)",
5454afa07f7eSJohn Dyson (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
5455c7c34a24SBruce Evans db_printf("\n");
5456df8bae1dSRodney W. Grimes
545777131528SDoug Moore if (prev == &map->header ||
545877131528SDoug Moore prev->object.vm_object !=
545977131528SDoug Moore entry->object.vm_object) {
5460c7c34a24SBruce Evans db_indent += 2;
5461101eeb7fSBruce Evans vm_object_print((db_expr_t)(intptr_t)
5462101eeb7fSBruce Evans entry->object.vm_object,
546344bbc3b7SKonstantin Belousov 0, 0, (char *)0);
5464c7c34a24SBruce Evans db_indent -= 2;
5465df8bae1dSRodney W. Grimes }
5466df8bae1dSRodney W. Grimes }
5467721899b1SDoug Moore prev = entry;
5468df8bae1dSRodney W. Grimes }
5469c7c34a24SBruce Evans db_indent -= 2;
5470df8bae1dSRodney W. Grimes }
547195e5e988SJohn Dyson
DB_SHOW_COMMAND(map,map)54722ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map)
54732ebcd458SAttilio Rao {
54742ebcd458SAttilio Rao
54752ebcd458SAttilio Rao if (!have_addr) {
54762ebcd458SAttilio Rao db_printf("usage: show map <addr>\n");
54772ebcd458SAttilio Rao return;
54782ebcd458SAttilio Rao }
54792ebcd458SAttilio Rao vm_map_print((vm_map_t)addr);
54802ebcd458SAttilio Rao }
548195e5e988SJohn Dyson
DB_SHOW_COMMAND(procvm,procvm)548295e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
548395e5e988SJohn Dyson {
548495e5e988SJohn Dyson struct proc *p;
548595e5e988SJohn Dyson
548695e5e988SJohn Dyson if (have_addr) {
5487a9546a6bSJohn Baldwin p = db_lookup_proc(addr);
548895e5e988SJohn Dyson } else {
548995e5e988SJohn Dyson p = curproc;
549095e5e988SJohn Dyson }
549195e5e988SJohn Dyson
5492ac1e407bSBruce Evans db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
5493ac1e407bSBruce Evans (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
5494b1028ad1SLuoqi Chen (void *)vmspace_pmap(p->p_vmspace));
549595e5e988SJohn Dyson
54962ebcd458SAttilio Rao vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
549795e5e988SJohn Dyson }
549895e5e988SJohn Dyson
5499c7c34a24SBruce Evans #endif /* DDB */
5500