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