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