1 /*-
2 * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
3 *
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94
35 *
36 *
37 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38 * All rights reserved.
39 *
40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41 *
42 * Permission to use, copy, modify and distribute this software and
43 * its documentation is hereby granted, provided that both the copyright
44 * notice and this permission notice appear in all copies of the
45 * software, derivative works or modified versions, and any portions
46 * thereof, and that both notices appear in supporting documentation.
47 *
48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51 *
52 * Carnegie Mellon requests users of this software to return to
53 *
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
58 *
59 * any improvements or extensions that they make and grant Carnegie the
60 * rights to redistribute these changes.
61 */
62
63 /*
64 * Virtual memory mapping module.
65 */
66
67 #include <sys/cdefs.h>
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/elf.h>
71 #include <sys/kernel.h>
72 #include <sys/ktr.h>
73 #include <sys/lock.h>
74 #include <sys/mutex.h>
75 #include <sys/proc.h>
76 #include <sys/vmmeter.h>
77 #include <sys/mman.h>
78 #include <sys/vnode.h>
79 #include <sys/racct.h>
80 #include <sys/resourcevar.h>
81 #include <sys/rwlock.h>
82 #include <sys/file.h>
83 #include <sys/sysctl.h>
84 #include <sys/sysent.h>
85 #include <sys/shm.h>
86
87 #include <vm/vm.h>
88 #include <vm/vm_param.h>
89 #include <vm/pmap.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_pageout.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_kern.h>
96 #include <vm/vm_extern.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/swap_pager.h>
99 #include <vm/uma.h>
100
101 /*
102 * Virtual memory maps provide for the mapping, protection,
103 * and sharing of virtual memory objects. In addition,
104 * this module provides for an efficient virtual copy of
105 * memory from one map to another.
106 *
107 * Synchronization is required prior to most operations.
108 *
109 * Maps consist of an ordered doubly-linked list of simple
110 * entries; a self-adjusting binary search tree of these
111 * entries is used to speed up lookups.
112 *
113 * Since portions of maps are specified by start/end addresses,
114 * which may not align with existing map entries, all
115 * routines merely "clip" entries to these start/end values.
116 * [That is, an entry is split into two, bordering at a
117 * start or end value.] Note that these clippings may not
118 * always be necessary (as the two resulting entries are then
119 * not changed); however, the clipping is done for convenience.
120 *
121 * As mentioned above, virtual copy operations are performed
122 * by copying VM object references from one map to
123 * another, and then marking both regions as copy-on-write.
124 */
125
126 static struct mtx map_sleep_mtx;
127 static uma_zone_t mapentzone;
128 static uma_zone_t kmapentzone;
129 static uma_zone_t vmspace_zone;
130 static int vmspace_zinit(void *mem, int size, 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 int vm_map_growstack(vm_map_t map, vm_offset_t addr,
137 vm_map_entry_t gap_entry);
138 static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
139 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
140 #ifdef INVARIANTS
141 static void vmspace_zdtor(void *mem, int size, void *arg);
142 #endif
143 static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
144 vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
145 int cow);
146 static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
147 vm_offset_t failed_addr);
148
149 #define CONTAINS_BITS(set, bits) ((~(set) & (bits)) == 0)
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 #ifndef UMA_MD_SMALL_ALLOC
179
180 /*
181 * Allocate a new slab for kernel map entries. The kernel map may be locked or
182 * unlocked, depending on whether the request is coming from the kernel map or a
183 * submap. This function allocates a virtual address range directly from the
184 * kernel map instead of the kmem_* layer to avoid recursion on the kernel map
185 * lock and also to avoid triggering allocator recursion in the vmem boundary
186 * tag allocator.
187 */
188 static void *
kmapent_alloc(uma_zone_t zone,vm_size_t bytes,int domain,uint8_t * pflag,int wait)189 kmapent_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
190 int wait)
191 {
192 vm_offset_t addr;
193 int error, locked;
194
195 *pflag = UMA_SLAB_PRIV;
196
197 if (!(locked = vm_map_locked(kernel_map)))
198 vm_map_lock(kernel_map);
199 addr = vm_map_findspace(kernel_map, vm_map_min(kernel_map), bytes);
200 if (addr + bytes < addr || addr + bytes > vm_map_max(kernel_map))
201 panic("%s: kernel map is exhausted", __func__);
202 error = vm_map_insert(kernel_map, NULL, 0, addr, addr + bytes,
203 VM_PROT_RW, VM_PROT_RW, MAP_NOFAULT);
204 if (error != KERN_SUCCESS)
205 panic("%s: vm_map_insert() failed: %d", __func__, error);
206 if (!locked)
207 vm_map_unlock(kernel_map);
208 error = kmem_back_domain(domain, kernel_object, addr, bytes, M_NOWAIT |
209 M_USE_RESERVE | (wait & M_ZERO));
210 if (error == KERN_SUCCESS) {
211 return ((void *)addr);
212 } else {
213 if (!locked)
214 vm_map_lock(kernel_map);
215 vm_map_delete(kernel_map, addr, bytes);
216 if (!locked)
217 vm_map_unlock(kernel_map);
218 return (NULL);
219 }
220 }
221
222 static void
kmapent_free(void * item,vm_size_t size,uint8_t pflag)223 kmapent_free(void *item, vm_size_t size, uint8_t pflag)
224 {
225 vm_offset_t addr;
226 int error;
227
228 if ((pflag & UMA_SLAB_PRIV) == 0)
229 /* XXX leaked */
230 return;
231
232 addr = (vm_offset_t)item;
233 kmem_unback(kernel_object, addr, size);
234 error = vm_map_remove(kernel_map, addr, addr + size);
235 KASSERT(error == KERN_SUCCESS,
236 ("%s: vm_map_remove failed: %d", __func__, error));
237 }
238
239 /*
240 * The worst-case upper bound on the number of kernel map entries that may be
241 * created before the zone must be replenished in _vm_map_unlock().
242 */
243 #define KMAPENT_RESERVE 1
244
245 #endif /* !UMD_MD_SMALL_ALLOC */
246
247 /*
248 * vm_map_startup:
249 *
250 * Initialize the vm_map module. Must be called before any other vm_map
251 * routines.
252 *
253 * User map and entry structures are allocated from the general purpose
254 * memory pool. Kernel maps are statically defined. Kernel map entries
255 * require special handling to avoid recursion; see the comments above
256 * kmapent_alloc() and in vm_map_entry_create().
257 */
258 void
vm_map_startup(void)259 vm_map_startup(void)
260 {
261 mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
262
263 /*
264 * Disable the use of per-CPU buckets: map entry allocation is
265 * serialized by the kernel map lock.
266 */
267 kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
268 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
269 UMA_ZONE_VM | UMA_ZONE_NOBUCKET);
270 #ifndef UMA_MD_SMALL_ALLOC
271 /* Reserve an extra map entry for use when replenishing the reserve. */
272 uma_zone_reserve(kmapentzone, KMAPENT_RESERVE + 1);
273 uma_prealloc(kmapentzone, KMAPENT_RESERVE + 1);
274 uma_zone_set_allocf(kmapentzone, kmapent_alloc);
275 uma_zone_set_freef(kmapentzone, kmapent_free);
276 #endif
277
278 mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
279 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
280 vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
281 #ifdef INVARIANTS
282 vmspace_zdtor,
283 #else
284 NULL,
285 #endif
286 vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
287 }
288
289 static int
vmspace_zinit(void * mem,int size,int flags)290 vmspace_zinit(void *mem, int size, int flags)
291 {
292 struct vmspace *vm;
293 vm_map_t map;
294
295 vm = (struct vmspace *)mem;
296 map = &vm->vm_map;
297
298 memset(map, 0, sizeof(*map));
299 mtx_init(&map->system_mtx, "vm map (system)", NULL,
300 MTX_DEF | MTX_DUPOK);
301 sx_init(&map->lock, "vm map (user)");
302 PMAP_LOCK_INIT(vmspace_pmap(vm));
303 return (0);
304 }
305
306 #ifdef INVARIANTS
307 static void
vmspace_zdtor(void * mem,int size,void * arg)308 vmspace_zdtor(void *mem, int size, void *arg)
309 {
310 struct vmspace *vm;
311
312 vm = (struct vmspace *)mem;
313 KASSERT(vm->vm_map.nentries == 0,
314 ("vmspace %p nentries == %d on free", vm, vm->vm_map.nentries));
315 KASSERT(vm->vm_map.size == 0,
316 ("vmspace %p size == %ju on free", vm, (uintmax_t)vm->vm_map.size));
317 }
318 #endif /* INVARIANTS */
319
320 /*
321 * Allocate a vmspace structure, including a vm_map and pmap,
322 * and initialize those structures. The refcnt is set to 1.
323 */
324 struct vmspace *
vmspace_alloc(vm_offset_t min,vm_offset_t max,pmap_pinit_t pinit)325 vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
326 {
327 struct vmspace *vm;
328
329 vm = uma_zalloc(vmspace_zone, M_WAITOK);
330 KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
331 if (!pinit(vmspace_pmap(vm))) {
332 uma_zfree(vmspace_zone, vm);
333 return (NULL);
334 }
335 CTR1(KTR_VM, "vmspace_alloc: %p", vm);
336 _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
337 refcount_init(&vm->vm_refcnt, 1);
338 vm->vm_shm = NULL;
339 vm->vm_swrss = 0;
340 vm->vm_tsize = 0;
341 vm->vm_dsize = 0;
342 vm->vm_ssize = 0;
343 vm->vm_taddr = 0;
344 vm->vm_daddr = 0;
345 vm->vm_maxsaddr = 0;
346 return (vm);
347 }
348
349 #ifdef RACCT
350 static void
vmspace_container_reset(struct proc * p)351 vmspace_container_reset(struct proc *p)
352 {
353
354 PROC_LOCK(p);
355 racct_set(p, RACCT_DATA, 0);
356 racct_set(p, RACCT_STACK, 0);
357 racct_set(p, RACCT_RSS, 0);
358 racct_set(p, RACCT_MEMLOCK, 0);
359 racct_set(p, RACCT_VMEM, 0);
360 PROC_UNLOCK(p);
361 }
362 #endif
363
364 static inline void
vmspace_dofree(struct vmspace * vm)365 vmspace_dofree(struct vmspace *vm)
366 {
367
368 CTR1(KTR_VM, "vmspace_free: %p", vm);
369
370 /*
371 * Make sure any SysV shm is freed, it might not have been in
372 * exit1().
373 */
374 shmexit(vm);
375
376 /*
377 * Lock the map, to wait out all other references to it.
378 * Delete all of the mappings and pages they hold, then call
379 * the pmap module to reclaim anything left.
380 */
381 (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map),
382 vm_map_max(&vm->vm_map));
383
384 pmap_release(vmspace_pmap(vm));
385 vm->vm_map.pmap = NULL;
386 uma_zfree(vmspace_zone, vm);
387 }
388
389 void
vmspace_free(struct vmspace * vm)390 vmspace_free(struct vmspace *vm)
391 {
392
393 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
394 "vmspace_free() called");
395
396 if (refcount_release(&vm->vm_refcnt))
397 vmspace_dofree(vm);
398 }
399
400 void
vmspace_exitfree(struct proc * p)401 vmspace_exitfree(struct proc *p)
402 {
403 struct vmspace *vm;
404
405 PROC_VMSPACE_LOCK(p);
406 vm = p->p_vmspace;
407 p->p_vmspace = NULL;
408 PROC_VMSPACE_UNLOCK(p);
409 KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
410 vmspace_free(vm);
411 }
412
413 void
vmspace_exit(struct thread * td)414 vmspace_exit(struct thread *td)
415 {
416 struct vmspace *vm;
417 struct proc *p;
418 bool released;
419
420 p = td->td_proc;
421 vm = p->p_vmspace;
422
423 /*
424 * Prepare to release the vmspace reference. The thread that releases
425 * the last reference is responsible for tearing down the vmspace.
426 * However, threads not releasing the final reference must switch to the
427 * kernel's vmspace0 before the decrement so that the subsequent pmap
428 * deactivation does not modify a freed vmspace.
429 */
430 refcount_acquire(&vmspace0.vm_refcnt);
431 if (!(released = refcount_release_if_last(&vm->vm_refcnt))) {
432 if (p->p_vmspace != &vmspace0) {
433 PROC_VMSPACE_LOCK(p);
434 p->p_vmspace = &vmspace0;
435 PROC_VMSPACE_UNLOCK(p);
436 pmap_activate(td);
437 }
438 released = refcount_release(&vm->vm_refcnt);
439 }
440 if (released) {
441 /*
442 * pmap_remove_pages() expects the pmap to be active, so switch
443 * back first if necessary.
444 */
445 if (p->p_vmspace != vm) {
446 PROC_VMSPACE_LOCK(p);
447 p->p_vmspace = vm;
448 PROC_VMSPACE_UNLOCK(p);
449 pmap_activate(td);
450 }
451 pmap_remove_pages(vmspace_pmap(vm));
452 PROC_VMSPACE_LOCK(p);
453 p->p_vmspace = &vmspace0;
454 PROC_VMSPACE_UNLOCK(p);
455 pmap_activate(td);
456 vmspace_dofree(vm);
457 }
458 #ifdef RACCT
459 if (racct_enable)
460 vmspace_container_reset(p);
461 #endif
462 }
463
464 /* Acquire reference to vmspace owned by another process. */
465
466 struct vmspace *
vmspace_acquire_ref(struct proc * p)467 vmspace_acquire_ref(struct proc *p)
468 {
469 struct vmspace *vm;
470
471 PROC_VMSPACE_LOCK(p);
472 vm = p->p_vmspace;
473 if (vm == NULL || !refcount_acquire_if_not_zero(&vm->vm_refcnt)) {
474 PROC_VMSPACE_UNLOCK(p);
475 return (NULL);
476 }
477 if (vm != p->p_vmspace) {
478 PROC_VMSPACE_UNLOCK(p);
479 vmspace_free(vm);
480 return (NULL);
481 }
482 PROC_VMSPACE_UNLOCK(p);
483 return (vm);
484 }
485
486 /*
487 * Switch between vmspaces in an AIO kernel process.
488 *
489 * The new vmspace is either the vmspace of a user process obtained
490 * from an active AIO request or the initial vmspace of the AIO kernel
491 * process (when it is idling). Because user processes will block to
492 * drain any active AIO requests before proceeding in exit() or
493 * execve(), the reference count for vmspaces from AIO requests can
494 * never be 0. Similarly, AIO kernel processes hold an extra
495 * reference on their initial vmspace for the life of the process. As
496 * a result, the 'newvm' vmspace always has a non-zero reference
497 * count. This permits an additional reference on 'newvm' to be
498 * acquired via a simple atomic increment rather than the loop in
499 * vmspace_acquire_ref() above.
500 */
501 void
vmspace_switch_aio(struct vmspace * newvm)502 vmspace_switch_aio(struct vmspace *newvm)
503 {
504 struct vmspace *oldvm;
505
506 /* XXX: Need some way to assert that this is an aio daemon. */
507
508 KASSERT(refcount_load(&newvm->vm_refcnt) > 0,
509 ("vmspace_switch_aio: newvm unreferenced"));
510
511 oldvm = curproc->p_vmspace;
512 if (oldvm == newvm)
513 return;
514
515 /*
516 * Point to the new address space and refer to it.
517 */
518 curproc->p_vmspace = newvm;
519 refcount_acquire(&newvm->vm_refcnt);
520
521 /* Activate the new mapping. */
522 pmap_activate(curthread);
523
524 vmspace_free(oldvm);
525 }
526
527 void
_vm_map_lock(vm_map_t map,const char * file,int line)528 _vm_map_lock(vm_map_t map, const char *file, int line)
529 {
530
531 if (map->system_map)
532 mtx_lock_flags_(&map->system_mtx, 0, file, line);
533 else
534 sx_xlock_(&map->lock, file, line);
535 map->timestamp++;
536 }
537
538 void
vm_map_entry_set_vnode_text(vm_map_entry_t entry,bool add)539 vm_map_entry_set_vnode_text(vm_map_entry_t entry, bool add)
540 {
541 vm_object_t object;
542 struct vnode *vp;
543 bool vp_held;
544
545 if ((entry->eflags & MAP_ENTRY_VN_EXEC) == 0)
546 return;
547 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
548 ("Submap with execs"));
549 object = entry->object.vm_object;
550 KASSERT(object != NULL, ("No object for text, entry %p", entry));
551 if ((object->flags & OBJ_ANON) != 0)
552 object = object->handle;
553 else
554 KASSERT(object->backing_object == NULL,
555 ("non-anon object %p shadows", object));
556 KASSERT(object != NULL, ("No content object for text, entry %p obj %p",
557 entry, entry->object.vm_object));
558
559 /*
560 * Mostly, we do not lock the backing object. It is
561 * referenced by the entry we are processing, so it cannot go
562 * away.
563 */
564 vm_pager_getvp(object, &vp, &vp_held);
565 if (vp != NULL) {
566 if (add) {
567 VOP_SET_TEXT_CHECKED(vp);
568 } else {
569 vn_lock(vp, LK_SHARED | LK_RETRY);
570 VOP_UNSET_TEXT_CHECKED(vp);
571 VOP_UNLOCK(vp);
572 }
573 if (vp_held)
574 vdrop(vp);
575 }
576 }
577
578 /*
579 * Use a different name for this vm_map_entry field when it's use
580 * is not consistent with its use as part of an ordered search tree.
581 */
582 #define defer_next right
583
584 static void
vm_map_process_deferred(void)585 vm_map_process_deferred(void)
586 {
587 struct thread *td;
588 vm_map_entry_t entry, next;
589 vm_object_t object;
590
591 td = curthread;
592 entry = td->td_map_def_user;
593 td->td_map_def_user = NULL;
594 while (entry != NULL) {
595 next = entry->defer_next;
596 MPASS((entry->eflags & (MAP_ENTRY_WRITECNT |
597 MAP_ENTRY_VN_EXEC)) != (MAP_ENTRY_WRITECNT |
598 MAP_ENTRY_VN_EXEC));
599 if ((entry->eflags & MAP_ENTRY_WRITECNT) != 0) {
600 /*
601 * Decrement the object's writemappings and
602 * possibly the vnode's v_writecount.
603 */
604 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
605 ("Submap with writecount"));
606 object = entry->object.vm_object;
607 KASSERT(object != NULL, ("No object for writecount"));
608 vm_pager_release_writecount(object, entry->start,
609 entry->end);
610 }
611 vm_map_entry_set_vnode_text(entry, false);
612 vm_map_entry_deallocate(entry, FALSE);
613 entry = next;
614 }
615 }
616
617 #ifdef INVARIANTS
618 static void
_vm_map_assert_locked(vm_map_t map,const char * file,int line)619 _vm_map_assert_locked(vm_map_t map, const char *file, int line)
620 {
621
622 if (map->system_map)
623 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
624 else
625 sx_assert_(&map->lock, SA_XLOCKED, file, line);
626 }
627
628 #define VM_MAP_ASSERT_LOCKED(map) \
629 _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
630
631 enum { VMMAP_CHECK_NONE, VMMAP_CHECK_UNLOCK, VMMAP_CHECK_ALL };
632 #ifdef DIAGNOSTIC
633 static int enable_vmmap_check = VMMAP_CHECK_UNLOCK;
634 #else
635 static int enable_vmmap_check = VMMAP_CHECK_NONE;
636 #endif
637 SYSCTL_INT(_debug, OID_AUTO, vmmap_check, CTLFLAG_RWTUN,
638 &enable_vmmap_check, 0, "Enable vm map consistency checking");
639
640 static void _vm_map_assert_consistent(vm_map_t map, int check);
641
642 #define VM_MAP_ASSERT_CONSISTENT(map) \
643 _vm_map_assert_consistent(map, VMMAP_CHECK_ALL)
644 #ifdef DIAGNOSTIC
645 #define VM_MAP_UNLOCK_CONSISTENT(map) do { \
646 if (map->nupdates > map->nentries) { \
647 _vm_map_assert_consistent(map, VMMAP_CHECK_UNLOCK); \
648 map->nupdates = 0; \
649 } \
650 } while (0)
651 #else
652 #define VM_MAP_UNLOCK_CONSISTENT(map)
653 #endif
654 #else
655 #define VM_MAP_ASSERT_LOCKED(map)
656 #define VM_MAP_ASSERT_CONSISTENT(map)
657 #define VM_MAP_UNLOCK_CONSISTENT(map)
658 #endif /* INVARIANTS */
659
660 void
_vm_map_unlock(vm_map_t map,const char * file,int line)661 _vm_map_unlock(vm_map_t map, const char *file, int line)
662 {
663
664 VM_MAP_UNLOCK_CONSISTENT(map);
665 if (map->system_map) {
666 #ifndef UMA_MD_SMALL_ALLOC
667 if (map == kernel_map && (map->flags & MAP_REPLENISH) != 0) {
668 uma_prealloc(kmapentzone, 1);
669 map->flags &= ~MAP_REPLENISH;
670 }
671 #endif
672 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
673 } else {
674 sx_xunlock_(&map->lock, file, line);
675 vm_map_process_deferred();
676 }
677 }
678
679 void
_vm_map_lock_read(vm_map_t map,const char * file,int line)680 _vm_map_lock_read(vm_map_t map, const char *file, int line)
681 {
682
683 if (map->system_map)
684 mtx_lock_flags_(&map->system_mtx, 0, file, line);
685 else
686 sx_slock_(&map->lock, file, line);
687 }
688
689 void
_vm_map_unlock_read(vm_map_t map,const char * file,int line)690 _vm_map_unlock_read(vm_map_t map, const char *file, int line)
691 {
692
693 if (map->system_map) {
694 KASSERT((map->flags & MAP_REPLENISH) == 0,
695 ("%s: MAP_REPLENISH leaked", __func__));
696 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
697 } else {
698 sx_sunlock_(&map->lock, file, line);
699 vm_map_process_deferred();
700 }
701 }
702
703 int
_vm_map_trylock(vm_map_t map,const char * file,int line)704 _vm_map_trylock(vm_map_t map, const char *file, int line)
705 {
706 int error;
707
708 error = map->system_map ?
709 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
710 !sx_try_xlock_(&map->lock, file, line);
711 if (error == 0)
712 map->timestamp++;
713 return (error == 0);
714 }
715
716 int
_vm_map_trylock_read(vm_map_t map,const char * file,int line)717 _vm_map_trylock_read(vm_map_t map, const char *file, int line)
718 {
719 int error;
720
721 error = map->system_map ?
722 !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
723 !sx_try_slock_(&map->lock, file, line);
724 return (error == 0);
725 }
726
727 /*
728 * _vm_map_lock_upgrade: [ internal use only ]
729 *
730 * Tries to upgrade a read (shared) lock on the specified map to a write
731 * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a
732 * non-zero value if the upgrade fails. If the upgrade fails, the map is
733 * returned without a read or write lock held.
734 *
735 * Requires that the map be read locked.
736 */
737 int
_vm_map_lock_upgrade(vm_map_t map,const char * file,int line)738 _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
739 {
740 unsigned int last_timestamp;
741
742 if (map->system_map) {
743 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
744 } else {
745 if (!sx_try_upgrade_(&map->lock, file, line)) {
746 last_timestamp = map->timestamp;
747 sx_sunlock_(&map->lock, file, line);
748 vm_map_process_deferred();
749 /*
750 * If the map's timestamp does not change while the
751 * map is unlocked, then the upgrade succeeds.
752 */
753 sx_xlock_(&map->lock, file, line);
754 if (last_timestamp != map->timestamp) {
755 sx_xunlock_(&map->lock, file, line);
756 return (1);
757 }
758 }
759 }
760 map->timestamp++;
761 return (0);
762 }
763
764 void
_vm_map_lock_downgrade(vm_map_t map,const char * file,int line)765 _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
766 {
767
768 if (map->system_map) {
769 KASSERT((map->flags & MAP_REPLENISH) == 0,
770 ("%s: MAP_REPLENISH leaked", __func__));
771 mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
772 } else {
773 VM_MAP_UNLOCK_CONSISTENT(map);
774 sx_downgrade_(&map->lock, file, line);
775 }
776 }
777
778 /*
779 * vm_map_locked:
780 *
781 * Returns a non-zero value if the caller holds a write (exclusive) lock
782 * on the specified map and the value "0" otherwise.
783 */
784 int
vm_map_locked(vm_map_t map)785 vm_map_locked(vm_map_t map)
786 {
787
788 if (map->system_map)
789 return (mtx_owned(&map->system_mtx));
790 else
791 return (sx_xlocked(&map->lock));
792 }
793
794 /*
795 * _vm_map_unlock_and_wait:
796 *
797 * Atomically releases the lock on the specified map and puts the calling
798 * thread to sleep. The calling thread will remain asleep until either
799 * vm_map_wakeup() is performed on the map or the specified timeout is
800 * exceeded.
801 *
802 * WARNING! This function does not perform deferred deallocations of
803 * objects and map entries. Therefore, the calling thread is expected to
804 * reacquire the map lock after reawakening and later perform an ordinary
805 * unlock operation, such as vm_map_unlock(), before completing its
806 * operation on the map.
807 */
808 int
_vm_map_unlock_and_wait(vm_map_t map,int timo,const char * file,int line)809 _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
810 {
811
812 VM_MAP_UNLOCK_CONSISTENT(map);
813 mtx_lock(&map_sleep_mtx);
814 if (map->system_map) {
815 KASSERT((map->flags & MAP_REPLENISH) == 0,
816 ("%s: MAP_REPLENISH leaked", __func__));
817 mtx_unlock_flags_(&map->system_mtx, 0, file, line);
818 } else {
819 sx_xunlock_(&map->lock, file, line);
820 }
821 return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
822 timo));
823 }
824
825 /*
826 * vm_map_wakeup:
827 *
828 * Awaken any threads that have slept on the map using
829 * vm_map_unlock_and_wait().
830 */
831 void
vm_map_wakeup(vm_map_t map)832 vm_map_wakeup(vm_map_t map)
833 {
834
835 /*
836 * Acquire and release map_sleep_mtx to prevent a wakeup()
837 * from being performed (and lost) between the map unlock
838 * and the msleep() in _vm_map_unlock_and_wait().
839 */
840 mtx_lock(&map_sleep_mtx);
841 mtx_unlock(&map_sleep_mtx);
842 wakeup(&map->root);
843 }
844
845 void
vm_map_busy(vm_map_t map)846 vm_map_busy(vm_map_t map)
847 {
848
849 VM_MAP_ASSERT_LOCKED(map);
850 map->busy++;
851 }
852
853 void
vm_map_unbusy(vm_map_t map)854 vm_map_unbusy(vm_map_t map)
855 {
856
857 VM_MAP_ASSERT_LOCKED(map);
858 KASSERT(map->busy, ("vm_map_unbusy: not busy"));
859 if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
860 vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
861 wakeup(&map->busy);
862 }
863 }
864
865 void
vm_map_wait_busy(vm_map_t map)866 vm_map_wait_busy(vm_map_t map)
867 {
868
869 VM_MAP_ASSERT_LOCKED(map);
870 while (map->busy) {
871 vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
872 if (map->system_map)
873 msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
874 else
875 sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
876 }
877 map->timestamp++;
878 }
879
880 long
vmspace_resident_count(struct vmspace * vmspace)881 vmspace_resident_count(struct vmspace *vmspace)
882 {
883 return pmap_resident_count(vmspace_pmap(vmspace));
884 }
885
886 /*
887 * Initialize an existing vm_map structure
888 * such as that in the vmspace structure.
889 */
890 static void
_vm_map_init(vm_map_t map,pmap_t pmap,vm_offset_t min,vm_offset_t max)891 _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
892 {
893
894 map->header.eflags = MAP_ENTRY_HEADER;
895 map->needs_wakeup = FALSE;
896 map->system_map = 0;
897 map->pmap = pmap;
898 map->header.end = min;
899 map->header.start = max;
900 map->flags = 0;
901 map->header.left = map->header.right = &map->header;
902 map->root = NULL;
903 map->timestamp = 0;
904 map->busy = 0;
905 map->anon_loc = 0;
906 #ifdef DIAGNOSTIC
907 map->nupdates = 0;
908 #endif
909 }
910
911 void
vm_map_init(vm_map_t map,pmap_t pmap,vm_offset_t min,vm_offset_t max)912 vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
913 {
914
915 _vm_map_init(map, pmap, min, max);
916 mtx_init(&map->system_mtx, "vm map (system)", NULL,
917 MTX_DEF | MTX_DUPOK);
918 sx_init(&map->lock, "vm map (user)");
919 }
920
921 /*
922 * vm_map_entry_dispose: [ internal use only ]
923 *
924 * Inverse of vm_map_entry_create.
925 */
926 static void
vm_map_entry_dispose(vm_map_t map,vm_map_entry_t entry)927 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
928 {
929 uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
930 }
931
932 /*
933 * vm_map_entry_create: [ internal use only ]
934 *
935 * Allocates a VM map entry for insertion.
936 * No entry fields are filled in.
937 */
938 static vm_map_entry_t
vm_map_entry_create(vm_map_t map)939 vm_map_entry_create(vm_map_t map)
940 {
941 vm_map_entry_t new_entry;
942
943 #ifndef UMA_MD_SMALL_ALLOC
944 if (map == kernel_map) {
945 VM_MAP_ASSERT_LOCKED(map);
946
947 /*
948 * A new slab of kernel map entries cannot be allocated at this
949 * point because the kernel map has not yet been updated to
950 * reflect the caller's request. Therefore, we allocate a new
951 * map entry, dipping into the reserve if necessary, and set a
952 * flag indicating that the reserve must be replenished before
953 * the map is unlocked.
954 */
955 new_entry = uma_zalloc(kmapentzone, M_NOWAIT | M_NOVM);
956 if (new_entry == NULL) {
957 new_entry = uma_zalloc(kmapentzone,
958 M_NOWAIT | M_NOVM | M_USE_RESERVE);
959 kernel_map->flags |= MAP_REPLENISH;
960 }
961 } else
962 #endif
963 if (map->system_map) {
964 new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
965 } else {
966 new_entry = uma_zalloc(mapentzone, M_WAITOK);
967 }
968 KASSERT(new_entry != NULL,
969 ("vm_map_entry_create: kernel resources exhausted"));
970 return (new_entry);
971 }
972
973 /*
974 * vm_map_entry_set_behavior:
975 *
976 * Set the expected access behavior, either normal, random, or
977 * sequential.
978 */
979 static inline void
vm_map_entry_set_behavior(vm_map_entry_t entry,u_char behavior)980 vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
981 {
982 entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
983 (behavior & MAP_ENTRY_BEHAV_MASK);
984 }
985
986 /*
987 * vm_map_entry_max_free_{left,right}:
988 *
989 * Compute the size of the largest free gap between two entries,
990 * one the root of a tree and the other the ancestor of that root
991 * that is the least or greatest ancestor found on the search path.
992 */
993 static inline vm_size_t
vm_map_entry_max_free_left(vm_map_entry_t root,vm_map_entry_t left_ancestor)994 vm_map_entry_max_free_left(vm_map_entry_t root, vm_map_entry_t left_ancestor)
995 {
996
997 return (root->left != left_ancestor ?
998 root->left->max_free : root->start - left_ancestor->end);
999 }
1000
1001 static inline vm_size_t
vm_map_entry_max_free_right(vm_map_entry_t root,vm_map_entry_t right_ancestor)1002 vm_map_entry_max_free_right(vm_map_entry_t root, vm_map_entry_t right_ancestor)
1003 {
1004
1005 return (root->right != right_ancestor ?
1006 root->right->max_free : right_ancestor->start - root->end);
1007 }
1008
1009 /*
1010 * vm_map_entry_{pred,succ}:
1011 *
1012 * Find the {predecessor, successor} of the entry by taking one step
1013 * in the appropriate direction and backtracking as much as necessary.
1014 * vm_map_entry_succ is defined in vm_map.h.
1015 */
1016 static inline vm_map_entry_t
vm_map_entry_pred(vm_map_entry_t entry)1017 vm_map_entry_pred(vm_map_entry_t entry)
1018 {
1019 vm_map_entry_t prior;
1020
1021 prior = entry->left;
1022 if (prior->right->start < entry->start) {
1023 do
1024 prior = prior->right;
1025 while (prior->right != entry);
1026 }
1027 return (prior);
1028 }
1029
1030 static inline vm_size_t
vm_size_max(vm_size_t a,vm_size_t b)1031 vm_size_max(vm_size_t a, vm_size_t b)
1032 {
1033
1034 return (a > b ? a : b);
1035 }
1036
1037 #define SPLAY_LEFT_STEP(root, y, llist, rlist, test) do { \
1038 vm_map_entry_t z; \
1039 vm_size_t max_free; \
1040 \
1041 /* \
1042 * Infer root->right->max_free == root->max_free when \
1043 * y->max_free < root->max_free || root->max_free == 0. \
1044 * Otherwise, look right to find it. \
1045 */ \
1046 y = root->left; \
1047 max_free = root->max_free; \
1048 KASSERT(max_free == vm_size_max( \
1049 vm_map_entry_max_free_left(root, llist), \
1050 vm_map_entry_max_free_right(root, rlist)), \
1051 ("%s: max_free invariant fails", __func__)); \
1052 if (max_free - 1 < vm_map_entry_max_free_left(root, llist)) \
1053 max_free = vm_map_entry_max_free_right(root, rlist); \
1054 if (y != llist && (test)) { \
1055 /* Rotate right and make y root. */ \
1056 z = y->right; \
1057 if (z != root) { \
1058 root->left = z; \
1059 y->right = root; \
1060 if (max_free < y->max_free) \
1061 root->max_free = max_free = \
1062 vm_size_max(max_free, z->max_free); \
1063 } else if (max_free < y->max_free) \
1064 root->max_free = max_free = \
1065 vm_size_max(max_free, root->start - y->end);\
1066 root = y; \
1067 y = root->left; \
1068 } \
1069 /* Copy right->max_free. Put root on rlist. */ \
1070 root->max_free = max_free; \
1071 KASSERT(max_free == vm_map_entry_max_free_right(root, rlist), \
1072 ("%s: max_free not copied from right", __func__)); \
1073 root->left = rlist; \
1074 rlist = root; \
1075 root = y != llist ? y : NULL; \
1076 } while (0)
1077
1078 #define SPLAY_RIGHT_STEP(root, y, llist, rlist, test) do { \
1079 vm_map_entry_t z; \
1080 vm_size_t max_free; \
1081 \
1082 /* \
1083 * Infer root->left->max_free == root->max_free when \
1084 * y->max_free < root->max_free || root->max_free == 0. \
1085 * Otherwise, look left to find it. \
1086 */ \
1087 y = root->right; \
1088 max_free = root->max_free; \
1089 KASSERT(max_free == vm_size_max( \
1090 vm_map_entry_max_free_left(root, llist), \
1091 vm_map_entry_max_free_right(root, rlist)), \
1092 ("%s: max_free invariant fails", __func__)); \
1093 if (max_free - 1 < vm_map_entry_max_free_right(root, rlist)) \
1094 max_free = vm_map_entry_max_free_left(root, llist); \
1095 if (y != rlist && (test)) { \
1096 /* Rotate left and make y root. */ \
1097 z = y->left; \
1098 if (z != root) { \
1099 root->right = z; \
1100 y->left = root; \
1101 if (max_free < y->max_free) \
1102 root->max_free = max_free = \
1103 vm_size_max(max_free, z->max_free); \
1104 } else if (max_free < y->max_free) \
1105 root->max_free = max_free = \
1106 vm_size_max(max_free, y->start - root->end);\
1107 root = y; \
1108 y = root->right; \
1109 } \
1110 /* Copy left->max_free. Put root on llist. */ \
1111 root->max_free = max_free; \
1112 KASSERT(max_free == vm_map_entry_max_free_left(root, llist), \
1113 ("%s: max_free not copied from left", __func__)); \
1114 root->right = llist; \
1115 llist = root; \
1116 root = y != rlist ? y : NULL; \
1117 } while (0)
1118
1119 /*
1120 * Walk down the tree until we find addr or a gap where addr would go, breaking
1121 * off left and right subtrees of nodes less than, or greater than addr. Treat
1122 * subtrees with root->max_free < length as empty trees. llist and rlist are
1123 * the two sides in reverse order (bottom-up), with llist linked by the right
1124 * pointer and rlist linked by the left pointer in the vm_map_entry, and both
1125 * lists terminated by &map->header. This function, and the subsequent call to
1126 * vm_map_splay_merge_{left,right,pred,succ}, rely on the start and end address
1127 * values in &map->header.
1128 */
1129 static __always_inline vm_map_entry_t
vm_map_splay_split(vm_map_t map,vm_offset_t addr,vm_size_t length,vm_map_entry_t * llist,vm_map_entry_t * rlist)1130 vm_map_splay_split(vm_map_t map, vm_offset_t addr, vm_size_t length,
1131 vm_map_entry_t *llist, vm_map_entry_t *rlist)
1132 {
1133 vm_map_entry_t left, right, root, y;
1134
1135 left = right = &map->header;
1136 root = map->root;
1137 while (root != NULL && root->max_free >= length) {
1138 KASSERT(left->end <= root->start &&
1139 root->end <= right->start,
1140 ("%s: root not within tree bounds", __func__));
1141 if (addr < root->start) {
1142 SPLAY_LEFT_STEP(root, y, left, right,
1143 y->max_free >= length && addr < y->start);
1144 } else if (addr >= root->end) {
1145 SPLAY_RIGHT_STEP(root, y, left, right,
1146 y->max_free >= length && addr >= y->end);
1147 } else
1148 break;
1149 }
1150 *llist = left;
1151 *rlist = right;
1152 return (root);
1153 }
1154
1155 static __always_inline void
vm_map_splay_findnext(vm_map_entry_t root,vm_map_entry_t * rlist)1156 vm_map_splay_findnext(vm_map_entry_t root, vm_map_entry_t *rlist)
1157 {
1158 vm_map_entry_t hi, right, y;
1159
1160 right = *rlist;
1161 hi = root->right == right ? NULL : root->right;
1162 if (hi == NULL)
1163 return;
1164 do
1165 SPLAY_LEFT_STEP(hi, y, root, right, true);
1166 while (hi != NULL);
1167 *rlist = right;
1168 }
1169
1170 static __always_inline void
vm_map_splay_findprev(vm_map_entry_t root,vm_map_entry_t * llist)1171 vm_map_splay_findprev(vm_map_entry_t root, vm_map_entry_t *llist)
1172 {
1173 vm_map_entry_t left, lo, y;
1174
1175 left = *llist;
1176 lo = root->left == left ? NULL : root->left;
1177 if (lo == NULL)
1178 return;
1179 do
1180 SPLAY_RIGHT_STEP(lo, y, left, root, true);
1181 while (lo != NULL);
1182 *llist = left;
1183 }
1184
1185 static inline void
vm_map_entry_swap(vm_map_entry_t * a,vm_map_entry_t * b)1186 vm_map_entry_swap(vm_map_entry_t *a, vm_map_entry_t *b)
1187 {
1188 vm_map_entry_t tmp;
1189
1190 tmp = *b;
1191 *b = *a;
1192 *a = tmp;
1193 }
1194
1195 /*
1196 * Walk back up the two spines, flip the pointers and set max_free. The
1197 * subtrees of the root go at the bottom of llist and rlist.
1198 */
1199 static vm_size_t
vm_map_splay_merge_left_walk(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t tail,vm_size_t max_free,vm_map_entry_t llist)1200 vm_map_splay_merge_left_walk(vm_map_entry_t header, vm_map_entry_t root,
1201 vm_map_entry_t tail, vm_size_t max_free, vm_map_entry_t llist)
1202 {
1203 do {
1204 /*
1205 * The max_free values of the children of llist are in
1206 * llist->max_free and max_free. Update with the
1207 * max value.
1208 */
1209 llist->max_free = max_free =
1210 vm_size_max(llist->max_free, max_free);
1211 vm_map_entry_swap(&llist->right, &tail);
1212 vm_map_entry_swap(&tail, &llist);
1213 } while (llist != header);
1214 root->left = tail;
1215 return (max_free);
1216 }
1217
1218 /*
1219 * When llist is known to be the predecessor of root.
1220 */
1221 static inline vm_size_t
vm_map_splay_merge_pred(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t llist)1222 vm_map_splay_merge_pred(vm_map_entry_t header, vm_map_entry_t root,
1223 vm_map_entry_t llist)
1224 {
1225 vm_size_t max_free;
1226
1227 max_free = root->start - llist->end;
1228 if (llist != header) {
1229 max_free = vm_map_splay_merge_left_walk(header, root,
1230 root, max_free, llist);
1231 } else {
1232 root->left = header;
1233 header->right = root;
1234 }
1235 return (max_free);
1236 }
1237
1238 /*
1239 * When llist may or may not be the predecessor of root.
1240 */
1241 static inline vm_size_t
vm_map_splay_merge_left(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t llist)1242 vm_map_splay_merge_left(vm_map_entry_t header, vm_map_entry_t root,
1243 vm_map_entry_t llist)
1244 {
1245 vm_size_t max_free;
1246
1247 max_free = vm_map_entry_max_free_left(root, llist);
1248 if (llist != header) {
1249 max_free = vm_map_splay_merge_left_walk(header, root,
1250 root->left == llist ? root : root->left,
1251 max_free, llist);
1252 }
1253 return (max_free);
1254 }
1255
1256 static vm_size_t
vm_map_splay_merge_right_walk(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t tail,vm_size_t max_free,vm_map_entry_t rlist)1257 vm_map_splay_merge_right_walk(vm_map_entry_t header, vm_map_entry_t root,
1258 vm_map_entry_t tail, vm_size_t max_free, vm_map_entry_t rlist)
1259 {
1260 do {
1261 /*
1262 * The max_free values of the children of rlist are in
1263 * rlist->max_free and max_free. Update with the
1264 * max value.
1265 */
1266 rlist->max_free = max_free =
1267 vm_size_max(rlist->max_free, max_free);
1268 vm_map_entry_swap(&rlist->left, &tail);
1269 vm_map_entry_swap(&tail, &rlist);
1270 } while (rlist != header);
1271 root->right = tail;
1272 return (max_free);
1273 }
1274
1275 /*
1276 * When rlist is known to be the succecessor of root.
1277 */
1278 static inline vm_size_t
vm_map_splay_merge_succ(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t rlist)1279 vm_map_splay_merge_succ(vm_map_entry_t header, vm_map_entry_t root,
1280 vm_map_entry_t rlist)
1281 {
1282 vm_size_t max_free;
1283
1284 max_free = rlist->start - root->end;
1285 if (rlist != header) {
1286 max_free = vm_map_splay_merge_right_walk(header, root,
1287 root, max_free, rlist);
1288 } else {
1289 root->right = header;
1290 header->left = root;
1291 }
1292 return (max_free);
1293 }
1294
1295 /*
1296 * When rlist may or may not be the succecessor of root.
1297 */
1298 static inline vm_size_t
vm_map_splay_merge_right(vm_map_entry_t header,vm_map_entry_t root,vm_map_entry_t rlist)1299 vm_map_splay_merge_right(vm_map_entry_t header, vm_map_entry_t root,
1300 vm_map_entry_t rlist)
1301 {
1302 vm_size_t max_free;
1303
1304 max_free = vm_map_entry_max_free_right(root, rlist);
1305 if (rlist != header) {
1306 max_free = vm_map_splay_merge_right_walk(header, root,
1307 root->right == rlist ? root : root->right,
1308 max_free, rlist);
1309 }
1310 return (max_free);
1311 }
1312
1313 /*
1314 * vm_map_splay:
1315 *
1316 * The Sleator and Tarjan top-down splay algorithm with the
1317 * following variation. Max_free must be computed bottom-up, so
1318 * on the downward pass, maintain the left and right spines in
1319 * reverse order. Then, make a second pass up each side to fix
1320 * the pointers and compute max_free. The time bound is O(log n)
1321 * amortized.
1322 *
1323 * The tree is threaded, which means that there are no null pointers.
1324 * When a node has no left child, its left pointer points to its
1325 * predecessor, which the last ancestor on the search path from the root
1326 * where the search branched right. Likewise, when a node has no right
1327 * child, its right pointer points to its successor. The map header node
1328 * is the predecessor of the first map entry, and the successor of the
1329 * last.
1330 *
1331 * The new root is the vm_map_entry containing "addr", or else an
1332 * adjacent entry (lower if possible) if addr is not in the tree.
1333 *
1334 * The map must be locked, and leaves it so.
1335 *
1336 * Returns: the new root.
1337 */
1338 static vm_map_entry_t
vm_map_splay(vm_map_t map,vm_offset_t addr)1339 vm_map_splay(vm_map_t map, vm_offset_t addr)
1340 {
1341 vm_map_entry_t header, llist, rlist, root;
1342 vm_size_t max_free_left, max_free_right;
1343
1344 header = &map->header;
1345 root = vm_map_splay_split(map, addr, 0, &llist, &rlist);
1346 if (root != NULL) {
1347 max_free_left = vm_map_splay_merge_left(header, root, llist);
1348 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1349 } else if (llist != header) {
1350 /*
1351 * Recover the greatest node in the left
1352 * subtree and make it the root.
1353 */
1354 root = llist;
1355 llist = root->right;
1356 max_free_left = vm_map_splay_merge_left(header, root, llist);
1357 max_free_right = vm_map_splay_merge_succ(header, root, rlist);
1358 } else if (rlist != header) {
1359 /*
1360 * Recover the least node in the right
1361 * subtree and make it the root.
1362 */
1363 root = rlist;
1364 rlist = root->left;
1365 max_free_left = vm_map_splay_merge_pred(header, root, llist);
1366 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1367 } else {
1368 /* There is no root. */
1369 return (NULL);
1370 }
1371 root->max_free = vm_size_max(max_free_left, max_free_right);
1372 map->root = root;
1373 VM_MAP_ASSERT_CONSISTENT(map);
1374 return (root);
1375 }
1376
1377 /*
1378 * vm_map_entry_{un,}link:
1379 *
1380 * Insert/remove entries from maps. On linking, if new entry clips
1381 * existing entry, trim existing entry to avoid overlap, and manage
1382 * offsets. On unlinking, merge disappearing entry with neighbor, if
1383 * called for, and manage offsets. Callers should not modify fields in
1384 * entries already mapped.
1385 */
1386 static void
vm_map_entry_link(vm_map_t map,vm_map_entry_t entry)1387 vm_map_entry_link(vm_map_t map, vm_map_entry_t entry)
1388 {
1389 vm_map_entry_t header, llist, rlist, root;
1390 vm_size_t max_free_left, max_free_right;
1391
1392 CTR3(KTR_VM,
1393 "vm_map_entry_link: map %p, nentries %d, entry %p", map,
1394 map->nentries, entry);
1395 VM_MAP_ASSERT_LOCKED(map);
1396 map->nentries++;
1397 header = &map->header;
1398 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1399 if (root == NULL) {
1400 /*
1401 * The new entry does not overlap any existing entry in the
1402 * map, so it becomes the new root of the map tree.
1403 */
1404 max_free_left = vm_map_splay_merge_pred(header, entry, llist);
1405 max_free_right = vm_map_splay_merge_succ(header, entry, rlist);
1406 } else if (entry->start == root->start) {
1407 /*
1408 * The new entry is a clone of root, with only the end field
1409 * changed. The root entry will be shrunk to abut the new
1410 * entry, and will be the right child of the new root entry in
1411 * the modified map.
1412 */
1413 KASSERT(entry->end < root->end,
1414 ("%s: clip_start not within entry", __func__));
1415 vm_map_splay_findprev(root, &llist);
1416 if ((root->eflags & (MAP_ENTRY_STACK_GAP_DN |
1417 MAP_ENTRY_STACK_GAP_UP)) == 0)
1418 root->offset += entry->end - root->start;
1419 root->start = entry->end;
1420 max_free_left = vm_map_splay_merge_pred(header, entry, llist);
1421 max_free_right = root->max_free = vm_size_max(
1422 vm_map_splay_merge_pred(entry, root, entry),
1423 vm_map_splay_merge_right(header, root, rlist));
1424 } else {
1425 /*
1426 * The new entry is a clone of root, with only the start field
1427 * changed. The root entry will be shrunk to abut the new
1428 * entry, and will be the left child of the new root entry in
1429 * the modified map.
1430 */
1431 KASSERT(entry->end == root->end,
1432 ("%s: clip_start not within entry", __func__));
1433 vm_map_splay_findnext(root, &rlist);
1434 if ((entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
1435 MAP_ENTRY_STACK_GAP_UP)) == 0)
1436 entry->offset += entry->start - root->start;
1437 root->end = entry->start;
1438 max_free_left = root->max_free = vm_size_max(
1439 vm_map_splay_merge_left(header, root, llist),
1440 vm_map_splay_merge_succ(entry, root, entry));
1441 max_free_right = vm_map_splay_merge_succ(header, entry, rlist);
1442 }
1443 entry->max_free = vm_size_max(max_free_left, max_free_right);
1444 map->root = entry;
1445 VM_MAP_ASSERT_CONSISTENT(map);
1446 }
1447
1448 enum unlink_merge_type {
1449 UNLINK_MERGE_NONE,
1450 UNLINK_MERGE_NEXT
1451 };
1452
1453 static void
vm_map_entry_unlink(vm_map_t map,vm_map_entry_t entry,enum unlink_merge_type op)1454 vm_map_entry_unlink(vm_map_t map, vm_map_entry_t entry,
1455 enum unlink_merge_type op)
1456 {
1457 vm_map_entry_t header, llist, rlist, root;
1458 vm_size_t max_free_left, max_free_right;
1459
1460 VM_MAP_ASSERT_LOCKED(map);
1461 header = &map->header;
1462 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1463 KASSERT(root != NULL,
1464 ("vm_map_entry_unlink: unlink object not mapped"));
1465
1466 vm_map_splay_findprev(root, &llist);
1467 vm_map_splay_findnext(root, &rlist);
1468 if (op == UNLINK_MERGE_NEXT) {
1469 rlist->start = root->start;
1470 MPASS((rlist->eflags & (MAP_ENTRY_STACK_GAP_DN |
1471 MAP_ENTRY_STACK_GAP_UP)) == 0);
1472 rlist->offset = root->offset;
1473 }
1474 if (llist != header) {
1475 root = llist;
1476 llist = root->right;
1477 max_free_left = vm_map_splay_merge_left(header, root, llist);
1478 max_free_right = vm_map_splay_merge_succ(header, root, rlist);
1479 } else if (rlist != header) {
1480 root = rlist;
1481 rlist = root->left;
1482 max_free_left = vm_map_splay_merge_pred(header, root, llist);
1483 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1484 } else {
1485 header->left = header->right = header;
1486 root = NULL;
1487 }
1488 if (root != NULL)
1489 root->max_free = vm_size_max(max_free_left, max_free_right);
1490 map->root = root;
1491 VM_MAP_ASSERT_CONSISTENT(map);
1492 map->nentries--;
1493 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
1494 map->nentries, entry);
1495 }
1496
1497 /*
1498 * vm_map_entry_resize:
1499 *
1500 * Resize a vm_map_entry, recompute the amount of free space that
1501 * follows it and propagate that value up the tree.
1502 *
1503 * The map must be locked, and leaves it so.
1504 */
1505 static void
vm_map_entry_resize(vm_map_t map,vm_map_entry_t entry,vm_size_t grow_amount)1506 vm_map_entry_resize(vm_map_t map, vm_map_entry_t entry, vm_size_t grow_amount)
1507 {
1508 vm_map_entry_t header, llist, rlist, root;
1509
1510 VM_MAP_ASSERT_LOCKED(map);
1511 header = &map->header;
1512 root = vm_map_splay_split(map, entry->start, 0, &llist, &rlist);
1513 KASSERT(root != NULL, ("%s: resize object not mapped", __func__));
1514 vm_map_splay_findnext(root, &rlist);
1515 entry->end += grow_amount;
1516 root->max_free = vm_size_max(
1517 vm_map_splay_merge_left(header, root, llist),
1518 vm_map_splay_merge_succ(header, root, rlist));
1519 map->root = root;
1520 VM_MAP_ASSERT_CONSISTENT(map);
1521 CTR4(KTR_VM, "%s: map %p, nentries %d, entry %p",
1522 __func__, map, map->nentries, entry);
1523 }
1524
1525 /*
1526 * vm_map_lookup_entry: [ internal use only ]
1527 *
1528 * Finds the map entry containing (or
1529 * immediately preceding) the specified address
1530 * in the given map; the entry is returned
1531 * in the "entry" parameter. The boolean
1532 * result indicates whether the address is
1533 * actually contained in the map.
1534 */
1535 boolean_t
vm_map_lookup_entry(vm_map_t map,vm_offset_t address,vm_map_entry_t * entry)1536 vm_map_lookup_entry(
1537 vm_map_t map,
1538 vm_offset_t address,
1539 vm_map_entry_t *entry) /* OUT */
1540 {
1541 vm_map_entry_t cur, header, lbound, ubound;
1542 boolean_t locked;
1543
1544 /*
1545 * If the map is empty, then the map entry immediately preceding
1546 * "address" is the map's header.
1547 */
1548 header = &map->header;
1549 cur = map->root;
1550 if (cur == NULL) {
1551 *entry = header;
1552 return (FALSE);
1553 }
1554 if (address >= cur->start && cur->end > address) {
1555 *entry = cur;
1556 return (TRUE);
1557 }
1558 if ((locked = vm_map_locked(map)) ||
1559 sx_try_upgrade(&map->lock)) {
1560 /*
1561 * Splay requires a write lock on the map. However, it only
1562 * restructures the binary search tree; it does not otherwise
1563 * change the map. Thus, the map's timestamp need not change
1564 * on a temporary upgrade.
1565 */
1566 cur = vm_map_splay(map, address);
1567 if (!locked) {
1568 VM_MAP_UNLOCK_CONSISTENT(map);
1569 sx_downgrade(&map->lock);
1570 }
1571
1572 /*
1573 * If "address" is contained within a map entry, the new root
1574 * is that map entry. Otherwise, the new root is a map entry
1575 * immediately before or after "address".
1576 */
1577 if (address < cur->start) {
1578 *entry = header;
1579 return (FALSE);
1580 }
1581 *entry = cur;
1582 return (address < cur->end);
1583 }
1584 /*
1585 * Since the map is only locked for read access, perform a
1586 * standard binary search tree lookup for "address".
1587 */
1588 lbound = ubound = header;
1589 for (;;) {
1590 if (address < cur->start) {
1591 ubound = cur;
1592 cur = cur->left;
1593 if (cur == lbound)
1594 break;
1595 } else if (cur->end <= address) {
1596 lbound = cur;
1597 cur = cur->right;
1598 if (cur == ubound)
1599 break;
1600 } else {
1601 *entry = cur;
1602 return (TRUE);
1603 }
1604 }
1605 *entry = lbound;
1606 return (FALSE);
1607 }
1608
1609 /*
1610 * vm_map_insert1() is identical to vm_map_insert() except that it
1611 * returns the newly inserted map entry in '*res'. In case the new
1612 * entry is coalesced with a neighbor or an existing entry was
1613 * resized, that entry is returned. In any case, the returned entry
1614 * covers the specified address range.
1615 */
1616 static int
vm_map_insert1(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t start,vm_offset_t end,vm_prot_t prot,vm_prot_t max,int cow,vm_map_entry_t * res)1617 vm_map_insert1(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1618 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow,
1619 vm_map_entry_t *res)
1620 {
1621 vm_map_entry_t new_entry, next_entry, prev_entry;
1622 struct ucred *cred;
1623 vm_eflags_t protoeflags;
1624 vm_inherit_t inheritance;
1625 u_long bdry;
1626 u_int bidx;
1627
1628 VM_MAP_ASSERT_LOCKED(map);
1629 KASSERT(object != kernel_object ||
1630 (cow & MAP_COPY_ON_WRITE) == 0,
1631 ("vm_map_insert: kernel object and COW"));
1632 KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0 ||
1633 (cow & MAP_SPLIT_BOUNDARY_MASK) != 0,
1634 ("vm_map_insert: paradoxical MAP_NOFAULT request, obj %p cow %#x",
1635 object, cow));
1636 KASSERT((prot & ~max) == 0,
1637 ("prot %#x is not subset of max_prot %#x", prot, max));
1638
1639 /*
1640 * Check that the start and end points are not bogus.
1641 */
1642 if (start == end || !vm_map_range_valid(map, start, end))
1643 return (KERN_INVALID_ADDRESS);
1644
1645 if ((map->flags & MAP_WXORX) != 0 && (prot & (VM_PROT_WRITE |
1646 VM_PROT_EXECUTE)) == (VM_PROT_WRITE | VM_PROT_EXECUTE))
1647 return (KERN_PROTECTION_FAILURE);
1648
1649 /*
1650 * Find the entry prior to the proposed starting address; if it's part
1651 * of an existing entry, this range is bogus.
1652 */
1653 if (vm_map_lookup_entry(map, start, &prev_entry))
1654 return (KERN_NO_SPACE);
1655
1656 /*
1657 * Assert that the next entry doesn't overlap the end point.
1658 */
1659 next_entry = vm_map_entry_succ(prev_entry);
1660 if (next_entry->start < end)
1661 return (KERN_NO_SPACE);
1662
1663 if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
1664 max != VM_PROT_NONE))
1665 return (KERN_INVALID_ARGUMENT);
1666
1667 protoeflags = 0;
1668 if (cow & MAP_COPY_ON_WRITE)
1669 protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
1670 if (cow & MAP_NOFAULT)
1671 protoeflags |= MAP_ENTRY_NOFAULT;
1672 if (cow & MAP_DISABLE_SYNCER)
1673 protoeflags |= MAP_ENTRY_NOSYNC;
1674 if (cow & MAP_DISABLE_COREDUMP)
1675 protoeflags |= MAP_ENTRY_NOCOREDUMP;
1676 if (cow & MAP_STACK_GROWS_DOWN)
1677 protoeflags |= MAP_ENTRY_GROWS_DOWN;
1678 if (cow & MAP_STACK_GROWS_UP)
1679 protoeflags |= MAP_ENTRY_GROWS_UP;
1680 if (cow & MAP_WRITECOUNT)
1681 protoeflags |= MAP_ENTRY_WRITECNT;
1682 if (cow & MAP_VN_EXEC)
1683 protoeflags |= MAP_ENTRY_VN_EXEC;
1684 if ((cow & MAP_CREATE_GUARD) != 0)
1685 protoeflags |= MAP_ENTRY_GUARD;
1686 if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
1687 protoeflags |= MAP_ENTRY_STACK_GAP_DN;
1688 if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
1689 protoeflags |= MAP_ENTRY_STACK_GAP_UP;
1690 if (cow & MAP_INHERIT_SHARE)
1691 inheritance = VM_INHERIT_SHARE;
1692 else
1693 inheritance = VM_INHERIT_DEFAULT;
1694 if ((cow & MAP_SPLIT_BOUNDARY_MASK) != 0) {
1695 /* This magically ignores index 0, for usual page size. */
1696 bidx = (cow & MAP_SPLIT_BOUNDARY_MASK) >>
1697 MAP_SPLIT_BOUNDARY_SHIFT;
1698 if (bidx >= MAXPAGESIZES)
1699 return (KERN_INVALID_ARGUMENT);
1700 bdry = pagesizes[bidx] - 1;
1701 if ((start & bdry) != 0 || (end & bdry) != 0)
1702 return (KERN_INVALID_ARGUMENT);
1703 protoeflags |= bidx << MAP_ENTRY_SPLIT_BOUNDARY_SHIFT;
1704 }
1705
1706 cred = NULL;
1707 if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
1708 goto charged;
1709 if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
1710 ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
1711 if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
1712 return (KERN_RESOURCE_SHORTAGE);
1713 KASSERT(object == NULL ||
1714 (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1715 object->cred == NULL,
1716 ("overcommit: vm_map_insert o %p", object));
1717 cred = curthread->td_ucred;
1718 }
1719
1720 charged:
1721 /* Expand the kernel pmap, if necessary. */
1722 if (map == kernel_map && end > kernel_vm_end)
1723 pmap_growkernel(end);
1724 if (object != NULL) {
1725 /*
1726 * OBJ_ONEMAPPING must be cleared unless this mapping
1727 * is trivially proven to be the only mapping for any
1728 * of the object's pages. (Object granularity
1729 * reference counting is insufficient to recognize
1730 * aliases with precision.)
1731 */
1732 if ((object->flags & OBJ_ANON) != 0) {
1733 VM_OBJECT_WLOCK(object);
1734 if (object->ref_count > 1 || object->shadow_count != 0)
1735 vm_object_clear_flag(object, OBJ_ONEMAPPING);
1736 VM_OBJECT_WUNLOCK(object);
1737 }
1738 } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) ==
1739 protoeflags &&
1740 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP |
1741 MAP_VN_EXEC)) == 0 &&
1742 prev_entry->end == start && (prev_entry->cred == cred ||
1743 (prev_entry->object.vm_object != NULL &&
1744 prev_entry->object.vm_object->cred == cred)) &&
1745 vm_object_coalesce(prev_entry->object.vm_object,
1746 prev_entry->offset,
1747 (vm_size_t)(prev_entry->end - prev_entry->start),
1748 (vm_size_t)(end - prev_entry->end), cred != NULL &&
1749 (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
1750 /*
1751 * We were able to extend the object. Determine if we
1752 * can extend the previous map entry to include the
1753 * new range as well.
1754 */
1755 if (prev_entry->inheritance == inheritance &&
1756 prev_entry->protection == prot &&
1757 prev_entry->max_protection == max &&
1758 prev_entry->wired_count == 0) {
1759 KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) ==
1760 0, ("prev_entry %p has incoherent wiring",
1761 prev_entry));
1762 if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
1763 map->size += end - prev_entry->end;
1764 vm_map_entry_resize(map, prev_entry,
1765 end - prev_entry->end);
1766 *res = vm_map_try_merge_entries(map, prev_entry,
1767 next_entry);
1768 return (KERN_SUCCESS);
1769 }
1770
1771 /*
1772 * If we can extend the object but cannot extend the
1773 * map entry, we have to create a new map entry. We
1774 * must bump the ref count on the extended object to
1775 * account for it. object may be NULL.
1776 */
1777 object = prev_entry->object.vm_object;
1778 offset = prev_entry->offset +
1779 (prev_entry->end - prev_entry->start);
1780 vm_object_reference(object);
1781 if (cred != NULL && object != NULL && object->cred != NULL &&
1782 !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
1783 /* Object already accounts for this uid. */
1784 cred = NULL;
1785 }
1786 }
1787 if (cred != NULL)
1788 crhold(cred);
1789
1790 /*
1791 * Create a new entry
1792 */
1793 new_entry = vm_map_entry_create(map);
1794 new_entry->start = start;
1795 new_entry->end = end;
1796 new_entry->cred = NULL;
1797
1798 new_entry->eflags = protoeflags;
1799 new_entry->object.vm_object = object;
1800 new_entry->offset = offset;
1801
1802 new_entry->inheritance = inheritance;
1803 new_entry->protection = prot;
1804 new_entry->max_protection = max;
1805 new_entry->wired_count = 0;
1806 new_entry->wiring_thread = NULL;
1807 new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1808 new_entry->next_read = start;
1809
1810 KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
1811 ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1812 new_entry->cred = cred;
1813
1814 /*
1815 * Insert the new entry into the list
1816 */
1817 vm_map_entry_link(map, new_entry);
1818 if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
1819 map->size += new_entry->end - new_entry->start;
1820
1821 /*
1822 * Try to coalesce the new entry with both the previous and next
1823 * entries in the list. Previously, we only attempted to coalesce
1824 * with the previous entry when object is NULL. Here, we handle the
1825 * other cases, which are less common.
1826 */
1827 vm_map_try_merge_entries(map, prev_entry, new_entry);
1828 *res = vm_map_try_merge_entries(map, new_entry, next_entry);
1829
1830 if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
1831 vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
1832 end - start, cow & MAP_PREFAULT_PARTIAL);
1833 }
1834
1835 return (KERN_SUCCESS);
1836 }
1837
1838 /*
1839 * vm_map_insert:
1840 *
1841 * Inserts the given VM object into the target map at the
1842 * specified address range.
1843 *
1844 * Requires that the map be locked, and leaves it so.
1845 *
1846 * If object is non-NULL, ref count must be bumped by caller
1847 * prior to making call to account for the new entry.
1848 */
1849 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)1850 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1851 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
1852 {
1853 vm_map_entry_t res;
1854
1855 return (vm_map_insert1(map, object, offset, start, end, prot, max,
1856 cow, &res));
1857 }
1858
1859 /*
1860 * vm_map_findspace:
1861 *
1862 * Find the first fit (lowest VM address) for "length" free bytes
1863 * beginning at address >= start in the given map.
1864 *
1865 * In a vm_map_entry, "max_free" is the maximum amount of
1866 * contiguous free space between an entry in its subtree and a
1867 * neighbor of that entry. This allows finding a free region in
1868 * one path down the tree, so O(log n) amortized with splay
1869 * trees.
1870 *
1871 * The map must be locked, and leaves it so.
1872 *
1873 * Returns: starting address if sufficient space,
1874 * vm_map_max(map)-length+1 if insufficient space.
1875 */
1876 vm_offset_t
vm_map_findspace(vm_map_t map,vm_offset_t start,vm_size_t length)1877 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length)
1878 {
1879 vm_map_entry_t header, llist, rlist, root, y;
1880 vm_size_t left_length, max_free_left, max_free_right;
1881 vm_offset_t gap_end;
1882
1883 VM_MAP_ASSERT_LOCKED(map);
1884
1885 /*
1886 * Request must fit within min/max VM address and must avoid
1887 * address wrap.
1888 */
1889 start = MAX(start, vm_map_min(map));
1890 if (start >= vm_map_max(map) || length > vm_map_max(map) - start)
1891 return (vm_map_max(map) - length + 1);
1892
1893 /* Empty tree means wide open address space. */
1894 if (map->root == NULL)
1895 return (start);
1896
1897 /*
1898 * After splay_split, if start is within an entry, push it to the start
1899 * of the following gap. If rlist is at the end of the gap containing
1900 * start, save the end of that gap in gap_end to see if the gap is big
1901 * enough; otherwise set gap_end to start skip gap-checking and move
1902 * directly to a search of the right subtree.
1903 */
1904 header = &map->header;
1905 root = vm_map_splay_split(map, start, length, &llist, &rlist);
1906 gap_end = rlist->start;
1907 if (root != NULL) {
1908 start = root->end;
1909 if (root->right != rlist)
1910 gap_end = start;
1911 max_free_left = vm_map_splay_merge_left(header, root, llist);
1912 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1913 } else if (rlist != header) {
1914 root = rlist;
1915 rlist = root->left;
1916 max_free_left = vm_map_splay_merge_pred(header, root, llist);
1917 max_free_right = vm_map_splay_merge_right(header, root, rlist);
1918 } else {
1919 root = llist;
1920 llist = root->right;
1921 max_free_left = vm_map_splay_merge_left(header, root, llist);
1922 max_free_right = vm_map_splay_merge_succ(header, root, rlist);
1923 }
1924 root->max_free = vm_size_max(max_free_left, max_free_right);
1925 map->root = root;
1926 VM_MAP_ASSERT_CONSISTENT(map);
1927 if (length <= gap_end - start)
1928 return (start);
1929
1930 /* With max_free, can immediately tell if no solution. */
1931 if (root->right == header || length > root->right->max_free)
1932 return (vm_map_max(map) - length + 1);
1933
1934 /*
1935 * Splay for the least large-enough gap in the right subtree.
1936 */
1937 llist = rlist = header;
1938 for (left_length = 0;;
1939 left_length = vm_map_entry_max_free_left(root, llist)) {
1940 if (length <= left_length)
1941 SPLAY_LEFT_STEP(root, y, llist, rlist,
1942 length <= vm_map_entry_max_free_left(y, llist));
1943 else
1944 SPLAY_RIGHT_STEP(root, y, llist, rlist,
1945 length > vm_map_entry_max_free_left(y, root));
1946 if (root == NULL)
1947 break;
1948 }
1949 root = llist;
1950 llist = root->right;
1951 max_free_left = vm_map_splay_merge_left(header, root, llist);
1952 if (rlist == header) {
1953 root->max_free = vm_size_max(max_free_left,
1954 vm_map_splay_merge_succ(header, root, rlist));
1955 } else {
1956 y = rlist;
1957 rlist = y->left;
1958 y->max_free = vm_size_max(
1959 vm_map_splay_merge_pred(root, y, root),
1960 vm_map_splay_merge_right(header, y, rlist));
1961 root->max_free = vm_size_max(max_free_left, y->max_free);
1962 }
1963 map->root = root;
1964 VM_MAP_ASSERT_CONSISTENT(map);
1965 return (root->end);
1966 }
1967
1968 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)1969 vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1970 vm_offset_t start, vm_size_t length, vm_prot_t prot,
1971 vm_prot_t max, int cow)
1972 {
1973 vm_offset_t end;
1974 int result;
1975
1976 end = start + length;
1977 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1978 object == NULL,
1979 ("vm_map_fixed: non-NULL backing object for stack"));
1980 vm_map_lock(map);
1981 VM_MAP_RANGE_CHECK(map, start, end);
1982 if ((cow & MAP_CHECK_EXCL) == 0) {
1983 result = vm_map_delete(map, start, end);
1984 if (result != KERN_SUCCESS)
1985 goto out;
1986 }
1987 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1988 result = vm_map_stack_locked(map, start, length, sgrowsiz,
1989 prot, max, cow);
1990 } else {
1991 result = vm_map_insert(map, object, offset, start, end,
1992 prot, max, cow);
1993 }
1994 out:
1995 vm_map_unlock(map);
1996 return (result);
1997 }
1998
1999 static const int aslr_pages_rnd_64[2] = {0x1000, 0x10};
2000 static const int aslr_pages_rnd_32[2] = {0x100, 0x4};
2001
2002 static int cluster_anon = 1;
2003 SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW,
2004 &cluster_anon, 0,
2005 "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always");
2006
2007 static bool
clustering_anon_allowed(vm_offset_t addr)2008 clustering_anon_allowed(vm_offset_t addr)
2009 {
2010
2011 switch (cluster_anon) {
2012 case 0:
2013 return (false);
2014 case 1:
2015 return (addr == 0);
2016 case 2:
2017 default:
2018 return (true);
2019 }
2020 }
2021
2022 static long aslr_restarts;
2023 SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD,
2024 &aslr_restarts, 0,
2025 "Number of aslr failures");
2026
2027 /*
2028 * Searches for the specified amount of free space in the given map with the
2029 * specified alignment. Performs an address-ordered, first-fit search from
2030 * the given address "*addr", with an optional upper bound "max_addr". If the
2031 * parameter "alignment" is zero, then the alignment is computed from the
2032 * given (object, offset) pair so as to enable the greatest possible use of
2033 * superpage mappings. Returns KERN_SUCCESS and the address of the free space
2034 * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE.
2035 *
2036 * The map must be locked. Initially, there must be at least "length" bytes
2037 * of free space at the given address.
2038 */
2039 static int
vm_map_alignspace(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t * addr,vm_size_t length,vm_offset_t max_addr,vm_offset_t alignment)2040 vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2041 vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
2042 vm_offset_t alignment)
2043 {
2044 vm_offset_t aligned_addr, free_addr;
2045
2046 VM_MAP_ASSERT_LOCKED(map);
2047 free_addr = *addr;
2048 KASSERT(free_addr == vm_map_findspace(map, free_addr, length),
2049 ("caller failed to provide space %#jx at address %p",
2050 (uintmax_t)length, (void *)free_addr));
2051 for (;;) {
2052 /*
2053 * At the start of every iteration, the free space at address
2054 * "*addr" is at least "length" bytes.
2055 */
2056 if (alignment == 0)
2057 pmap_align_superpage(object, offset, addr, length);
2058 else
2059 *addr = roundup2(*addr, alignment);
2060 aligned_addr = *addr;
2061 if (aligned_addr == free_addr) {
2062 /*
2063 * Alignment did not change "*addr", so "*addr" must
2064 * still provide sufficient free space.
2065 */
2066 return (KERN_SUCCESS);
2067 }
2068
2069 /*
2070 * Test for address wrap on "*addr". A wrapped "*addr" could
2071 * be a valid address, in which case vm_map_findspace() cannot
2072 * be relied upon to fail.
2073 */
2074 if (aligned_addr < free_addr)
2075 return (KERN_NO_SPACE);
2076 *addr = vm_map_findspace(map, aligned_addr, length);
2077 if (*addr + length > vm_map_max(map) ||
2078 (max_addr != 0 && *addr + length > max_addr))
2079 return (KERN_NO_SPACE);
2080 free_addr = *addr;
2081 if (free_addr == aligned_addr) {
2082 /*
2083 * If a successful call to vm_map_findspace() did not
2084 * change "*addr", then "*addr" must still be aligned
2085 * and provide sufficient free space.
2086 */
2087 return (KERN_SUCCESS);
2088 }
2089 }
2090 }
2091
2092 int
vm_map_find_aligned(vm_map_t map,vm_offset_t * addr,vm_size_t length,vm_offset_t max_addr,vm_offset_t alignment)2093 vm_map_find_aligned(vm_map_t map, vm_offset_t *addr, vm_size_t length,
2094 vm_offset_t max_addr, vm_offset_t alignment)
2095 {
2096 /* XXXKIB ASLR eh ? */
2097 *addr = vm_map_findspace(map, *addr, length);
2098 if (*addr + length > vm_map_max(map) ||
2099 (max_addr != 0 && *addr + length > max_addr))
2100 return (KERN_NO_SPACE);
2101 return (vm_map_alignspace(map, NULL, 0, addr, length, max_addr,
2102 alignment));
2103 }
2104
2105 /*
2106 * vm_map_find finds an unallocated region in the target address
2107 * map with the given length. The search is defined to be
2108 * first-fit from the specified address; the region found is
2109 * returned in the same parameter.
2110 *
2111 * If object is non-NULL, ref count must be bumped by caller
2112 * prior to making call to account for the new entry.
2113 */
2114 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)2115 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2116 vm_offset_t *addr, /* IN/OUT */
2117 vm_size_t length, vm_offset_t max_addr, int find_space,
2118 vm_prot_t prot, vm_prot_t max, int cow)
2119 {
2120 vm_offset_t alignment, curr_min_addr, min_addr;
2121 int gap, pidx, rv, try;
2122 bool cluster, en_aslr, update_anon;
2123
2124 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
2125 object == NULL,
2126 ("vm_map_find: non-NULL backing object for stack"));
2127 MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE &&
2128 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0));
2129 if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
2130 (object->flags & OBJ_COLORED) == 0))
2131 find_space = VMFS_ANY_SPACE;
2132 if (find_space >> 8 != 0) {
2133 KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
2134 alignment = (vm_offset_t)1 << (find_space >> 8);
2135 } else
2136 alignment = 0;
2137 en_aslr = (map->flags & MAP_ASLR) != 0;
2138 update_anon = cluster = clustering_anon_allowed(*addr) &&
2139 (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 &&
2140 find_space != VMFS_NO_SPACE && object == NULL &&
2141 (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP |
2142 MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE;
2143 curr_min_addr = min_addr = *addr;
2144 if (en_aslr && min_addr == 0 && !cluster &&
2145 find_space != VMFS_NO_SPACE &&
2146 (map->flags & MAP_ASLR_IGNSTART) != 0)
2147 curr_min_addr = min_addr = vm_map_min(map);
2148 try = 0;
2149 vm_map_lock(map);
2150 if (cluster) {
2151 curr_min_addr = map->anon_loc;
2152 if (curr_min_addr == 0)
2153 cluster = false;
2154 }
2155 if (find_space != VMFS_NO_SPACE) {
2156 KASSERT(find_space == VMFS_ANY_SPACE ||
2157 find_space == VMFS_OPTIMAL_SPACE ||
2158 find_space == VMFS_SUPER_SPACE ||
2159 alignment != 0, ("unexpected VMFS flag"));
2160 again:
2161 /*
2162 * When creating an anonymous mapping, try clustering
2163 * with an existing anonymous mapping first.
2164 *
2165 * We make up to two attempts to find address space
2166 * for a given find_space value. The first attempt may
2167 * apply randomization or may cluster with an existing
2168 * anonymous mapping. If this first attempt fails,
2169 * perform a first-fit search of the available address
2170 * space.
2171 *
2172 * If all tries failed, and find_space is
2173 * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE.
2174 * Again enable clustering and randomization.
2175 */
2176 try++;
2177 MPASS(try <= 2);
2178
2179 if (try == 2) {
2180 /*
2181 * Second try: we failed either to find a
2182 * suitable region for randomizing the
2183 * allocation, or to cluster with an existing
2184 * mapping. Retry with free run.
2185 */
2186 curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ?
2187 vm_map_min(map) : min_addr;
2188 atomic_add_long(&aslr_restarts, 1);
2189 }
2190
2191 if (try == 1 && en_aslr && !cluster) {
2192 /*
2193 * Find space for allocation, including
2194 * gap needed for later randomization.
2195 */
2196 pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 &&
2197 (find_space == VMFS_SUPER_SPACE || find_space ==
2198 VMFS_OPTIMAL_SPACE) ? 1 : 0;
2199 gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR &&
2200 (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ?
2201 aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx];
2202 *addr = vm_map_findspace(map, curr_min_addr,
2203 length + gap * pagesizes[pidx]);
2204 if (*addr + length + gap * pagesizes[pidx] >
2205 vm_map_max(map))
2206 goto again;
2207 /* And randomize the start address. */
2208 *addr += (arc4random() % gap) * pagesizes[pidx];
2209 if (max_addr != 0 && *addr + length > max_addr)
2210 goto again;
2211 } else {
2212 *addr = vm_map_findspace(map, curr_min_addr, length);
2213 if (*addr + length > vm_map_max(map) ||
2214 (max_addr != 0 && *addr + length > max_addr)) {
2215 if (cluster) {
2216 cluster = false;
2217 MPASS(try == 1);
2218 goto again;
2219 }
2220 rv = KERN_NO_SPACE;
2221 goto done;
2222 }
2223 }
2224
2225 if (find_space != VMFS_ANY_SPACE &&
2226 (rv = vm_map_alignspace(map, object, offset, addr, length,
2227 max_addr, alignment)) != KERN_SUCCESS) {
2228 if (find_space == VMFS_OPTIMAL_SPACE) {
2229 find_space = VMFS_ANY_SPACE;
2230 curr_min_addr = min_addr;
2231 cluster = update_anon;
2232 try = 0;
2233 goto again;
2234 }
2235 goto done;
2236 }
2237 } else if ((cow & MAP_REMAP) != 0) {
2238 if (!vm_map_range_valid(map, *addr, *addr + length)) {
2239 rv = KERN_INVALID_ADDRESS;
2240 goto done;
2241 }
2242 rv = vm_map_delete(map, *addr, *addr + length);
2243 if (rv != KERN_SUCCESS)
2244 goto done;
2245 }
2246 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
2247 rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
2248 max, cow);
2249 } else {
2250 rv = vm_map_insert(map, object, offset, *addr, *addr + length,
2251 prot, max, cow);
2252 }
2253 if (rv == KERN_SUCCESS && update_anon)
2254 map->anon_loc = *addr + length;
2255 done:
2256 vm_map_unlock(map);
2257 return (rv);
2258 }
2259
2260 /*
2261 * vm_map_find_min() is a variant of vm_map_find() that takes an
2262 * additional parameter (min_addr) and treats the given address
2263 * (*addr) differently. Specifically, it treats *addr as a hint
2264 * and not as the minimum address where the mapping is created.
2265 *
2266 * This function works in two phases. First, it tries to
2267 * allocate above the hint. If that fails and the hint is
2268 * greater than min_addr, it performs a second pass, replacing
2269 * the hint with min_addr as the minimum address for the
2270 * allocation.
2271 */
2272 int
vm_map_find_min(vm_map_t map,vm_object_t object,vm_ooffset_t offset,vm_offset_t * addr,vm_size_t length,vm_offset_t min_addr,vm_offset_t max_addr,int find_space,vm_prot_t prot,vm_prot_t max,int cow)2273 vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
2274 vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
2275 vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
2276 int cow)
2277 {
2278 vm_offset_t hint;
2279 int rv;
2280
2281 hint = *addr;
2282 for (;;) {
2283 rv = vm_map_find(map, object, offset, addr, length, max_addr,
2284 find_space, prot, max, cow);
2285 if (rv == KERN_SUCCESS || min_addr >= hint)
2286 return (rv);
2287 *addr = hint = min_addr;
2288 }
2289 }
2290
2291 /*
2292 * A map entry with any of the following flags set must not be merged with
2293 * another entry.
2294 */
2295 #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \
2296 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP | MAP_ENTRY_VN_EXEC | \
2297 MAP_ENTRY_STACK_GAP_UP | MAP_ENTRY_STACK_GAP_DN)
2298
2299 static bool
vm_map_mergeable_neighbors(vm_map_entry_t prev,vm_map_entry_t entry)2300 vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry)
2301 {
2302
2303 KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 ||
2304 (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0,
2305 ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable",
2306 prev, entry));
2307 return (prev->end == entry->start &&
2308 prev->object.vm_object == entry->object.vm_object &&
2309 (prev->object.vm_object == NULL ||
2310 prev->offset + (prev->end - prev->start) == entry->offset) &&
2311 prev->eflags == entry->eflags &&
2312 prev->protection == entry->protection &&
2313 prev->max_protection == entry->max_protection &&
2314 prev->inheritance == entry->inheritance &&
2315 prev->wired_count == entry->wired_count &&
2316 prev->cred == entry->cred);
2317 }
2318
2319 static void
vm_map_merged_neighbor_dispose(vm_map_t map,vm_map_entry_t entry)2320 vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry)
2321 {
2322
2323 /*
2324 * If the backing object is a vnode object, vm_object_deallocate()
2325 * calls vrele(). However, vrele() does not lock the vnode because
2326 * the vnode has additional references. Thus, the map lock can be
2327 * kept without causing a lock-order reversal with the vnode lock.
2328 *
2329 * Since we count the number of virtual page mappings in
2330 * object->un_pager.vnp.writemappings, the writemappings value
2331 * should not be adjusted when the entry is disposed of.
2332 */
2333 if (entry->object.vm_object != NULL)
2334 vm_object_deallocate(entry->object.vm_object);
2335 if (entry->cred != NULL)
2336 crfree(entry->cred);
2337 vm_map_entry_dispose(map, entry);
2338 }
2339
2340 /*
2341 * vm_map_try_merge_entries:
2342 *
2343 * Compare two map entries that represent consecutive ranges. If
2344 * the entries can be merged, expand the range of the second to
2345 * cover the range of the first and delete the first. Then return
2346 * the map entry that includes the first range.
2347 *
2348 * The map must be locked.
2349 */
2350 vm_map_entry_t
vm_map_try_merge_entries(vm_map_t map,vm_map_entry_t prev_entry,vm_map_entry_t entry)2351 vm_map_try_merge_entries(vm_map_t map, vm_map_entry_t prev_entry,
2352 vm_map_entry_t entry)
2353 {
2354
2355 VM_MAP_ASSERT_LOCKED(map);
2356 if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 &&
2357 vm_map_mergeable_neighbors(prev_entry, entry)) {
2358 vm_map_entry_unlink(map, prev_entry, UNLINK_MERGE_NEXT);
2359 vm_map_merged_neighbor_dispose(map, prev_entry);
2360 return (entry);
2361 }
2362 return (prev_entry);
2363 }
2364
2365 /*
2366 * vm_map_entry_back:
2367 *
2368 * Allocate an object to back a map entry.
2369 */
2370 static inline void
vm_map_entry_back(vm_map_entry_t entry)2371 vm_map_entry_back(vm_map_entry_t entry)
2372 {
2373 vm_object_t object;
2374
2375 KASSERT(entry->object.vm_object == NULL,
2376 ("map entry %p has backing object", entry));
2377 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2378 ("map entry %p is a submap", entry));
2379 object = vm_object_allocate_anon(atop(entry->end - entry->start), NULL,
2380 entry->cred, entry->end - entry->start);
2381 entry->object.vm_object = object;
2382 entry->offset = 0;
2383 entry->cred = NULL;
2384 }
2385
2386 /*
2387 * vm_map_entry_charge_object
2388 *
2389 * If there is no object backing this entry, create one. Otherwise, if
2390 * the entry has cred, give it to the backing object.
2391 */
2392 static inline void
vm_map_entry_charge_object(vm_map_t map,vm_map_entry_t entry)2393 vm_map_entry_charge_object(vm_map_t map, vm_map_entry_t entry)
2394 {
2395
2396 VM_MAP_ASSERT_LOCKED(map);
2397 KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
2398 ("map entry %p is a submap", entry));
2399 if (entry->object.vm_object == NULL && !map->system_map &&
2400 (entry->eflags & MAP_ENTRY_GUARD) == 0)
2401 vm_map_entry_back(entry);
2402 else if (entry->object.vm_object != NULL &&
2403 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
2404 entry->cred != NULL) {
2405 VM_OBJECT_WLOCK(entry->object.vm_object);
2406 KASSERT(entry->object.vm_object->cred == NULL,
2407 ("OVERCOMMIT: %s: both cred e %p", __func__, entry));
2408 entry->object.vm_object->cred = entry->cred;
2409 entry->object.vm_object->charge = entry->end - entry->start;
2410 VM_OBJECT_WUNLOCK(entry->object.vm_object);
2411 entry->cred = NULL;
2412 }
2413 }
2414
2415 /*
2416 * vm_map_entry_clone
2417 *
2418 * Create a duplicate map entry for clipping.
2419 */
2420 static vm_map_entry_t
vm_map_entry_clone(vm_map_t map,vm_map_entry_t entry)2421 vm_map_entry_clone(vm_map_t map, vm_map_entry_t entry)
2422 {
2423 vm_map_entry_t new_entry;
2424
2425 VM_MAP_ASSERT_LOCKED(map);
2426
2427 /*
2428 * Create a backing object now, if none exists, so that more individual
2429 * objects won't be created after the map entry is split.
2430 */
2431 vm_map_entry_charge_object(map, entry);
2432
2433 /* Clone the entry. */
2434 new_entry = vm_map_entry_create(map);
2435 *new_entry = *entry;
2436 if (new_entry->cred != NULL)
2437 crhold(entry->cred);
2438 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2439 vm_object_reference(new_entry->object.vm_object);
2440 vm_map_entry_set_vnode_text(new_entry, true);
2441 /*
2442 * The object->un_pager.vnp.writemappings for the object of
2443 * MAP_ENTRY_WRITECNT type entry shall be kept as is here. The
2444 * virtual pages are re-distributed among the clipped entries,
2445 * so the sum is left the same.
2446 */
2447 }
2448 return (new_entry);
2449 }
2450
2451 /*
2452 * vm_map_clip_start: [ internal use only ]
2453 *
2454 * Asserts that the given entry begins at or after
2455 * the specified address; if necessary,
2456 * it splits the entry into two.
2457 */
2458 static int
vm_map_clip_start(vm_map_t map,vm_map_entry_t entry,vm_offset_t startaddr)2459 vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t startaddr)
2460 {
2461 vm_map_entry_t new_entry;
2462 int bdry_idx;
2463
2464 if (!map->system_map)
2465 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2466 "%s: map %p entry %p start 0x%jx", __func__, map, entry,
2467 (uintmax_t)startaddr);
2468
2469 if (startaddr <= entry->start)
2470 return (KERN_SUCCESS);
2471
2472 VM_MAP_ASSERT_LOCKED(map);
2473 KASSERT(entry->end > startaddr && entry->start < startaddr,
2474 ("%s: invalid clip of entry %p", __func__, entry));
2475
2476 bdry_idx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(entry);
2477 if (bdry_idx != 0) {
2478 if ((startaddr & (pagesizes[bdry_idx] - 1)) != 0)
2479 return (KERN_INVALID_ARGUMENT);
2480 }
2481
2482 new_entry = vm_map_entry_clone(map, entry);
2483
2484 /*
2485 * Split off the front portion. Insert the new entry BEFORE this one,
2486 * so that this entry has the specified starting address.
2487 */
2488 new_entry->end = startaddr;
2489 vm_map_entry_link(map, new_entry);
2490 return (KERN_SUCCESS);
2491 }
2492
2493 /*
2494 * vm_map_lookup_clip_start:
2495 *
2496 * Find the entry at or just after 'start', and clip it if 'start' is in
2497 * the interior of the entry. Return entry after 'start', and in
2498 * prev_entry set the entry before 'start'.
2499 */
2500 static int
vm_map_lookup_clip_start(vm_map_t map,vm_offset_t start,vm_map_entry_t * res_entry,vm_map_entry_t * prev_entry)2501 vm_map_lookup_clip_start(vm_map_t map, vm_offset_t start,
2502 vm_map_entry_t *res_entry, vm_map_entry_t *prev_entry)
2503 {
2504 vm_map_entry_t entry;
2505 int rv;
2506
2507 if (!map->system_map)
2508 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2509 "%s: map %p start 0x%jx prev %p", __func__, map,
2510 (uintmax_t)start, prev_entry);
2511
2512 if (vm_map_lookup_entry(map, start, prev_entry)) {
2513 entry = *prev_entry;
2514 rv = vm_map_clip_start(map, entry, start);
2515 if (rv != KERN_SUCCESS)
2516 return (rv);
2517 *prev_entry = vm_map_entry_pred(entry);
2518 } else
2519 entry = vm_map_entry_succ(*prev_entry);
2520 *res_entry = entry;
2521 return (KERN_SUCCESS);
2522 }
2523
2524 /*
2525 * vm_map_clip_end: [ internal use only ]
2526 *
2527 * Asserts that the given entry ends at or before
2528 * the specified address; if necessary,
2529 * it splits the entry into two.
2530 */
2531 static int
vm_map_clip_end(vm_map_t map,vm_map_entry_t entry,vm_offset_t endaddr)2532 vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t endaddr)
2533 {
2534 vm_map_entry_t new_entry;
2535 int bdry_idx;
2536
2537 if (!map->system_map)
2538 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2539 "%s: map %p entry %p end 0x%jx", __func__, map, entry,
2540 (uintmax_t)endaddr);
2541
2542 if (endaddr >= entry->end)
2543 return (KERN_SUCCESS);
2544
2545 VM_MAP_ASSERT_LOCKED(map);
2546 KASSERT(entry->start < endaddr && entry->end > endaddr,
2547 ("%s: invalid clip of entry %p", __func__, entry));
2548
2549 bdry_idx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(entry);
2550 if (bdry_idx != 0) {
2551 if ((endaddr & (pagesizes[bdry_idx] - 1)) != 0)
2552 return (KERN_INVALID_ARGUMENT);
2553 }
2554
2555 new_entry = vm_map_entry_clone(map, entry);
2556
2557 /*
2558 * Split off the back portion. Insert the new entry AFTER this one,
2559 * so that this entry has the specified ending address.
2560 */
2561 new_entry->start = endaddr;
2562 vm_map_entry_link(map, new_entry);
2563
2564 return (KERN_SUCCESS);
2565 }
2566
2567 /*
2568 * vm_map_submap: [ kernel use only ]
2569 *
2570 * Mark the given range as handled by a subordinate map.
2571 *
2572 * This range must have been created with vm_map_find,
2573 * and no other operations may have been performed on this
2574 * range prior to calling vm_map_submap.
2575 *
2576 * Only a limited number of operations can be performed
2577 * within this rage after calling vm_map_submap:
2578 * vm_fault
2579 * [Don't try vm_map_copy!]
2580 *
2581 * To remove a submapping, one must first remove the
2582 * range from the superior map, and then destroy the
2583 * submap (if desired). [Better yet, don't try it.]
2584 */
2585 int
vm_map_submap(vm_map_t map,vm_offset_t start,vm_offset_t end,vm_map_t submap)2586 vm_map_submap(
2587 vm_map_t map,
2588 vm_offset_t start,
2589 vm_offset_t end,
2590 vm_map_t submap)
2591 {
2592 vm_map_entry_t entry;
2593 int result;
2594
2595 result = KERN_INVALID_ARGUMENT;
2596
2597 vm_map_lock(submap);
2598 submap->flags |= MAP_IS_SUB_MAP;
2599 vm_map_unlock(submap);
2600
2601 vm_map_lock(map);
2602 VM_MAP_RANGE_CHECK(map, start, end);
2603 if (vm_map_lookup_entry(map, start, &entry) && entry->end >= end &&
2604 (entry->eflags & MAP_ENTRY_COW) == 0 &&
2605 entry->object.vm_object == NULL) {
2606 result = vm_map_clip_start(map, entry, start);
2607 if (result != KERN_SUCCESS)
2608 goto unlock;
2609 result = vm_map_clip_end(map, entry, end);
2610 if (result != KERN_SUCCESS)
2611 goto unlock;
2612 entry->object.sub_map = submap;
2613 entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
2614 result = KERN_SUCCESS;
2615 }
2616 unlock:
2617 vm_map_unlock(map);
2618
2619 if (result != KERN_SUCCESS) {
2620 vm_map_lock(submap);
2621 submap->flags &= ~MAP_IS_SUB_MAP;
2622 vm_map_unlock(submap);
2623 }
2624 return (result);
2625 }
2626
2627 /*
2628 * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
2629 */
2630 #define MAX_INIT_PT 96
2631
2632 /*
2633 * vm_map_pmap_enter:
2634 *
2635 * Preload the specified map's pmap with mappings to the specified
2636 * object's memory-resident pages. No further physical pages are
2637 * allocated, and no further virtual pages are retrieved from secondary
2638 * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a
2639 * limited number of page mappings are created at the low-end of the
2640 * specified address range. (For this purpose, a superpage mapping
2641 * counts as one page mapping.) Otherwise, all resident pages within
2642 * the specified address range are mapped.
2643 */
2644 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)2645 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
2646 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
2647 {
2648 vm_offset_t start;
2649 vm_page_t p, p_start;
2650 vm_pindex_t mask, psize, threshold, tmpidx;
2651
2652 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
2653 return;
2654 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2655 VM_OBJECT_WLOCK(object);
2656 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
2657 pmap_object_init_pt(map->pmap, addr, object, pindex,
2658 size);
2659 VM_OBJECT_WUNLOCK(object);
2660 return;
2661 }
2662 VM_OBJECT_LOCK_DOWNGRADE(object);
2663 } else
2664 VM_OBJECT_RLOCK(object);
2665
2666 psize = atop(size);
2667 if (psize + pindex > object->size) {
2668 if (pindex >= object->size) {
2669 VM_OBJECT_RUNLOCK(object);
2670 return;
2671 }
2672 psize = object->size - pindex;
2673 }
2674
2675 start = 0;
2676 p_start = NULL;
2677 threshold = MAX_INIT_PT;
2678
2679 p = vm_page_find_least(object, pindex);
2680 /*
2681 * Assert: the variable p is either (1) the page with the
2682 * least pindex greater than or equal to the parameter pindex
2683 * or (2) NULL.
2684 */
2685 for (;
2686 p != NULL && (tmpidx = p->pindex - pindex) < psize;
2687 p = TAILQ_NEXT(p, listq)) {
2688 /*
2689 * don't allow an madvise to blow away our really
2690 * free pages allocating pv entries.
2691 */
2692 if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2693 vm_page_count_severe()) ||
2694 ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2695 tmpidx >= threshold)) {
2696 psize = tmpidx;
2697 break;
2698 }
2699 if (vm_page_all_valid(p)) {
2700 if (p_start == NULL) {
2701 start = addr + ptoa(tmpidx);
2702 p_start = p;
2703 }
2704 /* Jump ahead if a superpage mapping is possible. */
2705 if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2706 (pagesizes[p->psind] - 1)) == 0) {
2707 mask = atop(pagesizes[p->psind]) - 1;
2708 if (tmpidx + mask < psize &&
2709 vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2710 p += mask;
2711 threshold += mask;
2712 }
2713 }
2714 } else if (p_start != NULL) {
2715 pmap_enter_object(map->pmap, start, addr +
2716 ptoa(tmpidx), p_start, prot);
2717 p_start = NULL;
2718 }
2719 }
2720 if (p_start != NULL)
2721 pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2722 p_start, prot);
2723 VM_OBJECT_RUNLOCK(object);
2724 }
2725
2726 static void
vm_map_protect_guard(vm_map_entry_t entry,vm_prot_t new_prot,vm_prot_t new_maxprot,int flags)2727 vm_map_protect_guard(vm_map_entry_t entry, vm_prot_t new_prot,
2728 vm_prot_t new_maxprot, int flags)
2729 {
2730 vm_prot_t old_prot;
2731
2732 MPASS((entry->eflags & MAP_ENTRY_GUARD) != 0);
2733 if ((entry->eflags & (MAP_ENTRY_STACK_GAP_UP |
2734 MAP_ENTRY_STACK_GAP_DN)) == 0)
2735 return;
2736
2737 old_prot = PROT_EXTRACT(entry->offset);
2738 if ((flags & VM_MAP_PROTECT_SET_MAXPROT) != 0) {
2739 entry->offset = PROT_MAX(new_maxprot) |
2740 (new_maxprot & old_prot);
2741 }
2742 if ((flags & VM_MAP_PROTECT_SET_PROT) != 0) {
2743 entry->offset = new_prot | PROT_MAX(
2744 PROT_MAX_EXTRACT(entry->offset));
2745 }
2746 }
2747
2748 /*
2749 * vm_map_protect:
2750 *
2751 * Sets the protection and/or the maximum protection of the
2752 * specified address region in the target map.
2753 */
2754 int
vm_map_protect(vm_map_t map,vm_offset_t start,vm_offset_t end,vm_prot_t new_prot,vm_prot_t new_maxprot,int flags)2755 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2756 vm_prot_t new_prot, vm_prot_t new_maxprot, int flags)
2757 {
2758 vm_map_entry_t entry, first_entry, in_tran, prev_entry;
2759 vm_object_t obj;
2760 struct ucred *cred;
2761 vm_offset_t orig_start;
2762 vm_prot_t check_prot, max_prot, old_prot;
2763 int rv;
2764
2765 if (start == end)
2766 return (KERN_SUCCESS);
2767
2768 if (CONTAINS_BITS(flags, VM_MAP_PROTECT_SET_PROT |
2769 VM_MAP_PROTECT_SET_MAXPROT) &&
2770 !CONTAINS_BITS(new_maxprot, new_prot))
2771 return (KERN_OUT_OF_BOUNDS);
2772
2773 orig_start = start;
2774 again:
2775 in_tran = NULL;
2776 start = orig_start;
2777 vm_map_lock(map);
2778
2779 if ((map->flags & MAP_WXORX) != 0 &&
2780 (flags & VM_MAP_PROTECT_SET_PROT) != 0 &&
2781 CONTAINS_BITS(new_prot, VM_PROT_WRITE | VM_PROT_EXECUTE)) {
2782 vm_map_unlock(map);
2783 return (KERN_PROTECTION_FAILURE);
2784 }
2785
2786 /*
2787 * Ensure that we are not concurrently wiring pages. vm_map_wire() may
2788 * need to fault pages into the map and will drop the map lock while
2789 * doing so, and the VM object may end up in an inconsistent state if we
2790 * update the protection on the map entry in between faults.
2791 */
2792 vm_map_wait_busy(map);
2793
2794 VM_MAP_RANGE_CHECK(map, start, end);
2795
2796 if (!vm_map_lookup_entry(map, start, &first_entry))
2797 first_entry = vm_map_entry_succ(first_entry);
2798
2799 if ((flags & VM_MAP_PROTECT_GROWSDOWN) != 0 &&
2800 (first_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0) {
2801 /*
2802 * Handle Linux's PROT_GROWSDOWN flag.
2803 * It means that protection is applied down to the
2804 * whole stack, including the specified range of the
2805 * mapped region, and the grow down region (AKA
2806 * guard).
2807 */
2808 while (!CONTAINS_BITS(first_entry->eflags,
2809 MAP_ENTRY_GUARD | MAP_ENTRY_STACK_GAP_DN) &&
2810 first_entry != vm_map_entry_first(map))
2811 first_entry = vm_map_entry_pred(first_entry);
2812 start = first_entry->start;
2813 }
2814
2815 /*
2816 * Make a first pass to check for protection violations.
2817 */
2818 check_prot = 0;
2819 if ((flags & VM_MAP_PROTECT_SET_PROT) != 0)
2820 check_prot |= new_prot;
2821 if ((flags & VM_MAP_PROTECT_SET_MAXPROT) != 0)
2822 check_prot |= new_maxprot;
2823 for (entry = first_entry; entry->start < end;
2824 entry = vm_map_entry_succ(entry)) {
2825 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
2826 vm_map_unlock(map);
2827 return (KERN_INVALID_ARGUMENT);
2828 }
2829 if ((entry->eflags & (MAP_ENTRY_GUARD |
2830 MAP_ENTRY_STACK_GAP_DN | MAP_ENTRY_STACK_GAP_UP)) ==
2831 MAP_ENTRY_GUARD)
2832 continue;
2833 max_prot = (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
2834 MAP_ENTRY_STACK_GAP_UP)) != 0 ?
2835 PROT_MAX_EXTRACT(entry->offset) : entry->max_protection;
2836 if (!CONTAINS_BITS(max_prot, check_prot)) {
2837 vm_map_unlock(map);
2838 return (KERN_PROTECTION_FAILURE);
2839 }
2840 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0)
2841 in_tran = entry;
2842 }
2843
2844 /*
2845 * Postpone the operation until all in-transition map entries have
2846 * stabilized. An in-transition entry might already have its pages
2847 * wired and wired_count incremented, but not yet have its
2848 * MAP_ENTRY_USER_WIRED flag set. In which case, we would fail to call
2849 * vm_fault_copy_entry() in the final loop below.
2850 */
2851 if (in_tran != NULL) {
2852 in_tran->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2853 vm_map_unlock_and_wait(map, 0);
2854 goto again;
2855 }
2856
2857 /*
2858 * Before changing the protections, try to reserve swap space for any
2859 * private (i.e., copy-on-write) mappings that are transitioning from
2860 * read-only to read/write access. If a reservation fails, break out
2861 * of this loop early and let the next loop simplify the entries, since
2862 * some may now be mergeable.
2863 */
2864 rv = vm_map_clip_start(map, first_entry, start);
2865 if (rv != KERN_SUCCESS) {
2866 vm_map_unlock(map);
2867 return (rv);
2868 }
2869 for (entry = first_entry; entry->start < end;
2870 entry = vm_map_entry_succ(entry)) {
2871 rv = vm_map_clip_end(map, entry, end);
2872 if (rv != KERN_SUCCESS) {
2873 vm_map_unlock(map);
2874 return (rv);
2875 }
2876
2877 if ((flags & VM_MAP_PROTECT_SET_PROT) == 0 ||
2878 ((new_prot & ~entry->protection) & VM_PROT_WRITE) == 0 ||
2879 ENTRY_CHARGED(entry) ||
2880 (entry->eflags & MAP_ENTRY_GUARD) != 0)
2881 continue;
2882
2883 cred = curthread->td_ucred;
2884 obj = entry->object.vm_object;
2885
2886 if (obj == NULL ||
2887 (entry->eflags & MAP_ENTRY_NEEDS_COPY) != 0) {
2888 if (!swap_reserve(entry->end - entry->start)) {
2889 rv = KERN_RESOURCE_SHORTAGE;
2890 end = entry->end;
2891 break;
2892 }
2893 crhold(cred);
2894 entry->cred = cred;
2895 continue;
2896 }
2897
2898 VM_OBJECT_WLOCK(obj);
2899 if (obj->type != OBJT_DEFAULT &&
2900 (obj->flags & OBJ_SWAP) == 0) {
2901 VM_OBJECT_WUNLOCK(obj);
2902 continue;
2903 }
2904
2905 /*
2906 * Charge for the whole object allocation now, since
2907 * we cannot distinguish between non-charged and
2908 * charged clipped mapping of the same object later.
2909 */
2910 KASSERT(obj->charge == 0,
2911 ("vm_map_protect: object %p overcharged (entry %p)",
2912 obj, entry));
2913 if (!swap_reserve(ptoa(obj->size))) {
2914 VM_OBJECT_WUNLOCK(obj);
2915 rv = KERN_RESOURCE_SHORTAGE;
2916 end = entry->end;
2917 break;
2918 }
2919
2920 crhold(cred);
2921 obj->cred = cred;
2922 obj->charge = ptoa(obj->size);
2923 VM_OBJECT_WUNLOCK(obj);
2924 }
2925
2926 /*
2927 * If enough swap space was available, go back and fix up protections.
2928 * Otherwise, just simplify entries, since some may have been modified.
2929 * [Note that clipping is not necessary the second time.]
2930 */
2931 for (prev_entry = vm_map_entry_pred(first_entry), entry = first_entry;
2932 entry->start < end;
2933 vm_map_try_merge_entries(map, prev_entry, entry),
2934 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
2935 if (rv != KERN_SUCCESS)
2936 continue;
2937
2938 if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
2939 vm_map_protect_guard(entry, new_prot, new_maxprot,
2940 flags);
2941 continue;
2942 }
2943
2944 old_prot = entry->protection;
2945
2946 if ((flags & VM_MAP_PROTECT_SET_MAXPROT) != 0) {
2947 entry->max_protection = new_maxprot;
2948 entry->protection = new_maxprot & old_prot;
2949 }
2950 if ((flags & VM_MAP_PROTECT_SET_PROT) != 0)
2951 entry->protection = new_prot;
2952
2953 /*
2954 * For user wired map entries, the normal lazy evaluation of
2955 * write access upgrades through soft page faults is
2956 * undesirable. Instead, immediately copy any pages that are
2957 * copy-on-write and enable write access in the physical map.
2958 */
2959 if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2960 (entry->protection & VM_PROT_WRITE) != 0 &&
2961 (old_prot & VM_PROT_WRITE) == 0)
2962 vm_fault_copy_entry(map, map, entry, entry, NULL);
2963
2964 /*
2965 * When restricting access, update the physical map. Worry
2966 * about copy-on-write here.
2967 */
2968 if ((old_prot & ~entry->protection) != 0) {
2969 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2970 VM_PROT_ALL)
2971 pmap_protect(map->pmap, entry->start,
2972 entry->end,
2973 entry->protection & MASK(entry));
2974 #undef MASK
2975 }
2976 }
2977 vm_map_try_merge_entries(map, prev_entry, entry);
2978 vm_map_unlock(map);
2979 return (rv);
2980 }
2981
2982 /*
2983 * vm_map_madvise:
2984 *
2985 * This routine traverses a processes map handling the madvise
2986 * system call. Advisories are classified as either those effecting
2987 * the vm_map_entry structure, or those effecting the underlying
2988 * objects.
2989 */
2990 int
vm_map_madvise(vm_map_t map,vm_offset_t start,vm_offset_t end,int behav)2991 vm_map_madvise(
2992 vm_map_t map,
2993 vm_offset_t start,
2994 vm_offset_t end,
2995 int behav)
2996 {
2997 vm_map_entry_t entry, prev_entry;
2998 int rv;
2999 bool modify_map;
3000
3001 /*
3002 * Some madvise calls directly modify the vm_map_entry, in which case
3003 * we need to use an exclusive lock on the map and we need to perform
3004 * various clipping operations. Otherwise we only need a read-lock
3005 * on the map.
3006 */
3007 switch(behav) {
3008 case MADV_NORMAL:
3009 case MADV_SEQUENTIAL:
3010 case MADV_RANDOM:
3011 case MADV_NOSYNC:
3012 case MADV_AUTOSYNC:
3013 case MADV_NOCORE:
3014 case MADV_CORE:
3015 if (start == end)
3016 return (0);
3017 modify_map = true;
3018 vm_map_lock(map);
3019 break;
3020 case MADV_WILLNEED:
3021 case MADV_DONTNEED:
3022 case MADV_FREE:
3023 if (start == end)
3024 return (0);
3025 modify_map = false;
3026 vm_map_lock_read(map);
3027 break;
3028 default:
3029 return (EINVAL);
3030 }
3031
3032 /*
3033 * Locate starting entry and clip if necessary.
3034 */
3035 VM_MAP_RANGE_CHECK(map, start, end);
3036
3037 if (modify_map) {
3038 /*
3039 * madvise behaviors that are implemented in the vm_map_entry.
3040 *
3041 * We clip the vm_map_entry so that behavioral changes are
3042 * limited to the specified address range.
3043 */
3044 rv = vm_map_lookup_clip_start(map, start, &entry, &prev_entry);
3045 if (rv != KERN_SUCCESS) {
3046 vm_map_unlock(map);
3047 return (vm_mmap_to_errno(rv));
3048 }
3049
3050 for (; entry->start < end; prev_entry = entry,
3051 entry = vm_map_entry_succ(entry)) {
3052 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
3053 continue;
3054
3055 rv = vm_map_clip_end(map, entry, end);
3056 if (rv != KERN_SUCCESS) {
3057 vm_map_unlock(map);
3058 return (vm_mmap_to_errno(rv));
3059 }
3060
3061 switch (behav) {
3062 case MADV_NORMAL:
3063 vm_map_entry_set_behavior(entry,
3064 MAP_ENTRY_BEHAV_NORMAL);
3065 break;
3066 case MADV_SEQUENTIAL:
3067 vm_map_entry_set_behavior(entry,
3068 MAP_ENTRY_BEHAV_SEQUENTIAL);
3069 break;
3070 case MADV_RANDOM:
3071 vm_map_entry_set_behavior(entry,
3072 MAP_ENTRY_BEHAV_RANDOM);
3073 break;
3074 case MADV_NOSYNC:
3075 entry->eflags |= MAP_ENTRY_NOSYNC;
3076 break;
3077 case MADV_AUTOSYNC:
3078 entry->eflags &= ~MAP_ENTRY_NOSYNC;
3079 break;
3080 case MADV_NOCORE:
3081 entry->eflags |= MAP_ENTRY_NOCOREDUMP;
3082 break;
3083 case MADV_CORE:
3084 entry->eflags &= ~MAP_ENTRY_NOCOREDUMP;
3085 break;
3086 default:
3087 break;
3088 }
3089 vm_map_try_merge_entries(map, prev_entry, entry);
3090 }
3091 vm_map_try_merge_entries(map, prev_entry, entry);
3092 vm_map_unlock(map);
3093 } else {
3094 vm_pindex_t pstart, pend;
3095
3096 /*
3097 * madvise behaviors that are implemented in the underlying
3098 * vm_object.
3099 *
3100 * Since we don't clip the vm_map_entry, we have to clip
3101 * the vm_object pindex and count.
3102 */
3103 if (!vm_map_lookup_entry(map, start, &entry))
3104 entry = vm_map_entry_succ(entry);
3105 for (; entry->start < end;
3106 entry = vm_map_entry_succ(entry)) {
3107 vm_offset_t useEnd, useStart;
3108
3109 if ((entry->eflags & (MAP_ENTRY_IS_SUB_MAP |
3110 MAP_ENTRY_GUARD)) != 0)
3111 continue;
3112
3113 /*
3114 * MADV_FREE would otherwise rewind time to
3115 * the creation of the shadow object. Because
3116 * we hold the VM map read-locked, neither the
3117 * entry's object nor the presence of a
3118 * backing object can change.
3119 */
3120 if (behav == MADV_FREE &&
3121 entry->object.vm_object != NULL &&
3122 entry->object.vm_object->backing_object != NULL)
3123 continue;
3124
3125 pstart = OFF_TO_IDX(entry->offset);
3126 pend = pstart + atop(entry->end - entry->start);
3127 useStart = entry->start;
3128 useEnd = entry->end;
3129
3130 if (entry->start < start) {
3131 pstart += atop(start - entry->start);
3132 useStart = start;
3133 }
3134 if (entry->end > end) {
3135 pend -= atop(entry->end - end);
3136 useEnd = end;
3137 }
3138
3139 if (pstart >= pend)
3140 continue;
3141
3142 /*
3143 * Perform the pmap_advise() before clearing
3144 * PGA_REFERENCED in vm_page_advise(). Otherwise, a
3145 * concurrent pmap operation, such as pmap_remove(),
3146 * could clear a reference in the pmap and set
3147 * PGA_REFERENCED on the page before the pmap_advise()
3148 * had completed. Consequently, the page would appear
3149 * referenced based upon an old reference that
3150 * occurred before this pmap_advise() ran.
3151 */
3152 if (behav == MADV_DONTNEED || behav == MADV_FREE)
3153 pmap_advise(map->pmap, useStart, useEnd,
3154 behav);
3155
3156 vm_object_madvise(entry->object.vm_object, pstart,
3157 pend, behav);
3158
3159 /*
3160 * Pre-populate paging structures in the
3161 * WILLNEED case. For wired entries, the
3162 * paging structures are already populated.
3163 */
3164 if (behav == MADV_WILLNEED &&
3165 entry->wired_count == 0) {
3166 vm_map_pmap_enter(map,
3167 useStart,
3168 entry->protection,
3169 entry->object.vm_object,
3170 pstart,
3171 ptoa(pend - pstart),
3172 MAP_PREFAULT_MADVISE
3173 );
3174 }
3175 }
3176 vm_map_unlock_read(map);
3177 }
3178 return (0);
3179 }
3180
3181 /*
3182 * vm_map_inherit:
3183 *
3184 * Sets the inheritance of the specified address
3185 * range in the target map. Inheritance
3186 * affects how the map will be shared with
3187 * child maps at the time of vmspace_fork.
3188 */
3189 int
vm_map_inherit(vm_map_t map,vm_offset_t start,vm_offset_t end,vm_inherit_t new_inheritance)3190 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
3191 vm_inherit_t new_inheritance)
3192 {
3193 vm_map_entry_t entry, lentry, prev_entry, start_entry;
3194 int rv;
3195
3196 switch (new_inheritance) {
3197 case VM_INHERIT_NONE:
3198 case VM_INHERIT_COPY:
3199 case VM_INHERIT_SHARE:
3200 case VM_INHERIT_ZERO:
3201 break;
3202 default:
3203 return (KERN_INVALID_ARGUMENT);
3204 }
3205 if (start == end)
3206 return (KERN_SUCCESS);
3207 vm_map_lock(map);
3208 VM_MAP_RANGE_CHECK(map, start, end);
3209 rv = vm_map_lookup_clip_start(map, start, &start_entry, &prev_entry);
3210 if (rv != KERN_SUCCESS)
3211 goto unlock;
3212 if (vm_map_lookup_entry(map, end - 1, &lentry)) {
3213 rv = vm_map_clip_end(map, lentry, end);
3214 if (rv != KERN_SUCCESS)
3215 goto unlock;
3216 }
3217 if (new_inheritance == VM_INHERIT_COPY) {
3218 for (entry = start_entry; entry->start < end;
3219 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
3220 if ((entry->eflags & MAP_ENTRY_SPLIT_BOUNDARY_MASK)
3221 != 0) {
3222 rv = KERN_INVALID_ARGUMENT;
3223 goto unlock;
3224 }
3225 }
3226 }
3227 for (entry = start_entry; entry->start < end; prev_entry = entry,
3228 entry = vm_map_entry_succ(entry)) {
3229 KASSERT(entry->end <= end, ("non-clipped entry %p end %jx %jx",
3230 entry, (uintmax_t)entry->end, (uintmax_t)end));
3231 if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
3232 new_inheritance != VM_INHERIT_ZERO)
3233 entry->inheritance = new_inheritance;
3234 vm_map_try_merge_entries(map, prev_entry, entry);
3235 }
3236 vm_map_try_merge_entries(map, prev_entry, entry);
3237 unlock:
3238 vm_map_unlock(map);
3239 return (rv);
3240 }
3241
3242 /*
3243 * vm_map_entry_in_transition:
3244 *
3245 * Release the map lock, and sleep until the entry is no longer in
3246 * transition. Awake and acquire the map lock. If the map changed while
3247 * another held the lock, lookup a possibly-changed entry at or after the
3248 * 'start' position of the old entry.
3249 */
3250 static vm_map_entry_t
vm_map_entry_in_transition(vm_map_t map,vm_offset_t in_start,vm_offset_t * io_end,bool holes_ok,vm_map_entry_t in_entry)3251 vm_map_entry_in_transition(vm_map_t map, vm_offset_t in_start,
3252 vm_offset_t *io_end, bool holes_ok, vm_map_entry_t in_entry)
3253 {
3254 vm_map_entry_t entry;
3255 vm_offset_t start;
3256 u_int last_timestamp;
3257
3258 VM_MAP_ASSERT_LOCKED(map);
3259 KASSERT((in_entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3260 ("not in-tranition map entry %p", in_entry));
3261 /*
3262 * We have not yet clipped the entry.
3263 */
3264 start = MAX(in_start, in_entry->start);
3265 in_entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
3266 last_timestamp = map->timestamp;
3267 if (vm_map_unlock_and_wait(map, 0)) {
3268 /*
3269 * Allow interruption of user wiring/unwiring?
3270 */
3271 }
3272 vm_map_lock(map);
3273 if (last_timestamp + 1 == map->timestamp)
3274 return (in_entry);
3275
3276 /*
3277 * Look again for the entry because the map was modified while it was
3278 * unlocked. Specifically, the entry may have been clipped, merged, or
3279 * deleted.
3280 */
3281 if (!vm_map_lookup_entry(map, start, &entry)) {
3282 if (!holes_ok) {
3283 *io_end = start;
3284 return (NULL);
3285 }
3286 entry = vm_map_entry_succ(entry);
3287 }
3288 return (entry);
3289 }
3290
3291 /*
3292 * vm_map_unwire:
3293 *
3294 * Implements both kernel and user unwiring.
3295 */
3296 int
vm_map_unwire(vm_map_t map,vm_offset_t start,vm_offset_t end,int flags)3297 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
3298 int flags)
3299 {
3300 vm_map_entry_t entry, first_entry, next_entry, prev_entry;
3301 int rv;
3302 bool holes_ok, need_wakeup, user_unwire;
3303
3304 if (start == end)
3305 return (KERN_SUCCESS);
3306 holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
3307 user_unwire = (flags & VM_MAP_WIRE_USER) != 0;
3308 vm_map_lock(map);
3309 VM_MAP_RANGE_CHECK(map, start, end);
3310 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3311 if (holes_ok)
3312 first_entry = vm_map_entry_succ(first_entry);
3313 else {
3314 vm_map_unlock(map);
3315 return (KERN_INVALID_ADDRESS);
3316 }
3317 }
3318 rv = KERN_SUCCESS;
3319 for (entry = first_entry; entry->start < end; entry = next_entry) {
3320 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3321 /*
3322 * We have not yet clipped the entry.
3323 */
3324 next_entry = vm_map_entry_in_transition(map, start,
3325 &end, holes_ok, entry);
3326 if (next_entry == NULL) {
3327 if (entry == first_entry) {
3328 vm_map_unlock(map);
3329 return (KERN_INVALID_ADDRESS);
3330 }
3331 rv = KERN_INVALID_ADDRESS;
3332 break;
3333 }
3334 first_entry = (entry == first_entry) ?
3335 next_entry : NULL;
3336 continue;
3337 }
3338 rv = vm_map_clip_start(map, entry, start);
3339 if (rv != KERN_SUCCESS)
3340 break;
3341 rv = vm_map_clip_end(map, entry, end);
3342 if (rv != KERN_SUCCESS)
3343 break;
3344
3345 /*
3346 * Mark the entry in case the map lock is released. (See
3347 * above.)
3348 */
3349 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3350 entry->wiring_thread == NULL,
3351 ("owned map entry %p", entry));
3352 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3353 entry->wiring_thread = curthread;
3354 next_entry = vm_map_entry_succ(entry);
3355 /*
3356 * Check the map for holes in the specified region.
3357 * If holes_ok, skip this check.
3358 */
3359 if (!holes_ok &&
3360 entry->end < end && next_entry->start > entry->end) {
3361 end = entry->end;
3362 rv = KERN_INVALID_ADDRESS;
3363 break;
3364 }
3365 /*
3366 * If system unwiring, require that the entry is system wired.
3367 */
3368 if (!user_unwire &&
3369 vm_map_entry_system_wired_count(entry) == 0) {
3370 end = entry->end;
3371 rv = KERN_INVALID_ARGUMENT;
3372 break;
3373 }
3374 }
3375 need_wakeup = false;
3376 if (first_entry == NULL &&
3377 !vm_map_lookup_entry(map, start, &first_entry)) {
3378 KASSERT(holes_ok, ("vm_map_unwire: lookup failed"));
3379 prev_entry = first_entry;
3380 entry = vm_map_entry_succ(first_entry);
3381 } else {
3382 prev_entry = vm_map_entry_pred(first_entry);
3383 entry = first_entry;
3384 }
3385 for (; entry->start < end;
3386 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
3387 /*
3388 * If holes_ok was specified, an empty
3389 * space in the unwired region could have been mapped
3390 * while the map lock was dropped for draining
3391 * MAP_ENTRY_IN_TRANSITION. Moreover, another thread
3392 * could be simultaneously wiring this new mapping
3393 * entry. Detect these cases and skip any entries
3394 * marked as in transition by us.
3395 */
3396 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3397 entry->wiring_thread != curthread) {
3398 KASSERT(holes_ok,
3399 ("vm_map_unwire: !HOLESOK and new/changed entry"));
3400 continue;
3401 }
3402
3403 if (rv == KERN_SUCCESS && (!user_unwire ||
3404 (entry->eflags & MAP_ENTRY_USER_WIRED))) {
3405 if (entry->wired_count == 1)
3406 vm_map_entry_unwire(map, entry);
3407 else
3408 entry->wired_count--;
3409 if (user_unwire)
3410 entry->eflags &= ~MAP_ENTRY_USER_WIRED;
3411 }
3412 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3413 ("vm_map_unwire: in-transition flag missing %p", entry));
3414 KASSERT(entry->wiring_thread == curthread,
3415 ("vm_map_unwire: alien wire %p", entry));
3416 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
3417 entry->wiring_thread = NULL;
3418 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3419 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3420 need_wakeup = true;
3421 }
3422 vm_map_try_merge_entries(map, prev_entry, entry);
3423 }
3424 vm_map_try_merge_entries(map, prev_entry, entry);
3425 vm_map_unlock(map);
3426 if (need_wakeup)
3427 vm_map_wakeup(map);
3428 return (rv);
3429 }
3430
3431 static void
vm_map_wire_user_count_sub(u_long npages)3432 vm_map_wire_user_count_sub(u_long npages)
3433 {
3434
3435 atomic_subtract_long(&vm_user_wire_count, npages);
3436 }
3437
3438 static bool
vm_map_wire_user_count_add(u_long npages)3439 vm_map_wire_user_count_add(u_long npages)
3440 {
3441 u_long wired;
3442
3443 wired = vm_user_wire_count;
3444 do {
3445 if (npages + wired > vm_page_max_user_wired)
3446 return (false);
3447 } while (!atomic_fcmpset_long(&vm_user_wire_count, &wired,
3448 npages + wired));
3449
3450 return (true);
3451 }
3452
3453 /*
3454 * vm_map_wire_entry_failure:
3455 *
3456 * Handle a wiring failure on the given entry.
3457 *
3458 * The map should be locked.
3459 */
3460 static void
vm_map_wire_entry_failure(vm_map_t map,vm_map_entry_t entry,vm_offset_t failed_addr)3461 vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
3462 vm_offset_t failed_addr)
3463 {
3464
3465 VM_MAP_ASSERT_LOCKED(map);
3466 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
3467 entry->wired_count == 1,
3468 ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
3469 KASSERT(failed_addr < entry->end,
3470 ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
3471
3472 /*
3473 * If any pages at the start of this entry were successfully wired,
3474 * then unwire them.
3475 */
3476 if (failed_addr > entry->start) {
3477 pmap_unwire(map->pmap, entry->start, failed_addr);
3478 vm_object_unwire(entry->object.vm_object, entry->offset,
3479 failed_addr - entry->start, PQ_ACTIVE);
3480 }
3481
3482 /*
3483 * Assign an out-of-range value to represent the failure to wire this
3484 * entry.
3485 */
3486 entry->wired_count = -1;
3487 }
3488
3489 int
vm_map_wire(vm_map_t map,vm_offset_t start,vm_offset_t end,int flags)3490 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3491 {
3492 int rv;
3493
3494 vm_map_lock(map);
3495 rv = vm_map_wire_locked(map, start, end, flags);
3496 vm_map_unlock(map);
3497 return (rv);
3498 }
3499
3500 /*
3501 * vm_map_wire_locked:
3502 *
3503 * Implements both kernel and user wiring. Returns with the map locked,
3504 * the map lock may be dropped.
3505 */
3506 int
vm_map_wire_locked(vm_map_t map,vm_offset_t start,vm_offset_t end,int flags)3507 vm_map_wire_locked(vm_map_t map, vm_offset_t start, vm_offset_t end, int flags)
3508 {
3509 vm_map_entry_t entry, first_entry, next_entry, prev_entry;
3510 vm_offset_t faddr, saved_end, saved_start;
3511 u_long incr, npages;
3512 u_int bidx, last_timestamp;
3513 int rv;
3514 bool holes_ok, need_wakeup, user_wire;
3515 vm_prot_t prot;
3516
3517 VM_MAP_ASSERT_LOCKED(map);
3518
3519 if (start == end)
3520 return (KERN_SUCCESS);
3521 prot = 0;
3522 if (flags & VM_MAP_WIRE_WRITE)
3523 prot |= VM_PROT_WRITE;
3524 holes_ok = (flags & VM_MAP_WIRE_HOLESOK) != 0;
3525 user_wire = (flags & VM_MAP_WIRE_USER) != 0;
3526 VM_MAP_RANGE_CHECK(map, start, end);
3527 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3528 if (holes_ok)
3529 first_entry = vm_map_entry_succ(first_entry);
3530 else
3531 return (KERN_INVALID_ADDRESS);
3532 }
3533 for (entry = first_entry; entry->start < end; entry = next_entry) {
3534 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
3535 /*
3536 * We have not yet clipped the entry.
3537 */
3538 next_entry = vm_map_entry_in_transition(map, start,
3539 &end, holes_ok, entry);
3540 if (next_entry == NULL) {
3541 if (entry == first_entry)
3542 return (KERN_INVALID_ADDRESS);
3543 rv = KERN_INVALID_ADDRESS;
3544 goto done;
3545 }
3546 first_entry = (entry == first_entry) ?
3547 next_entry : NULL;
3548 continue;
3549 }
3550 rv = vm_map_clip_start(map, entry, start);
3551 if (rv != KERN_SUCCESS)
3552 goto done;
3553 rv = vm_map_clip_end(map, entry, end);
3554 if (rv != KERN_SUCCESS)
3555 goto done;
3556
3557 /*
3558 * Mark the entry in case the map lock is released. (See
3559 * above.)
3560 */
3561 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
3562 entry->wiring_thread == NULL,
3563 ("owned map entry %p", entry));
3564 entry->eflags |= MAP_ENTRY_IN_TRANSITION;
3565 entry->wiring_thread = curthread;
3566 if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
3567 || (entry->protection & prot) != prot) {
3568 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
3569 if (!holes_ok) {
3570 end = entry->end;
3571 rv = KERN_INVALID_ADDRESS;
3572 goto done;
3573 }
3574 } else if (entry->wired_count == 0) {
3575 entry->wired_count++;
3576
3577 npages = atop(entry->end - entry->start);
3578 if (user_wire && !vm_map_wire_user_count_add(npages)) {
3579 vm_map_wire_entry_failure(map, entry,
3580 entry->start);
3581 end = entry->end;
3582 rv = KERN_RESOURCE_SHORTAGE;
3583 goto done;
3584 }
3585
3586 /*
3587 * Release the map lock, relying on the in-transition
3588 * mark. Mark the map busy for fork.
3589 */
3590 saved_start = entry->start;
3591 saved_end = entry->end;
3592 last_timestamp = map->timestamp;
3593 bidx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(entry);
3594 incr = pagesizes[bidx];
3595 vm_map_busy(map);
3596 vm_map_unlock(map);
3597
3598 for (faddr = saved_start; faddr < saved_end;
3599 faddr += incr) {
3600 /*
3601 * Simulate a fault to get the page and enter
3602 * it into the physical map.
3603 */
3604 rv = vm_fault(map, faddr, VM_PROT_NONE,
3605 VM_FAULT_WIRE, NULL);
3606 if (rv != KERN_SUCCESS)
3607 break;
3608 }
3609 vm_map_lock(map);
3610 vm_map_unbusy(map);
3611 if (last_timestamp + 1 != map->timestamp) {
3612 /*
3613 * Look again for the entry because the map was
3614 * modified while it was unlocked. The entry
3615 * may have been clipped, but NOT merged or
3616 * deleted.
3617 */
3618 if (!vm_map_lookup_entry(map, saved_start,
3619 &next_entry))
3620 KASSERT(false,
3621 ("vm_map_wire: lookup failed"));
3622 first_entry = (entry == first_entry) ?
3623 next_entry : NULL;
3624 for (entry = next_entry; entry->end < saved_end;
3625 entry = vm_map_entry_succ(entry)) {
3626 /*
3627 * In case of failure, handle entries
3628 * that were not fully wired here;
3629 * fully wired entries are handled
3630 * later.
3631 */
3632 if (rv != KERN_SUCCESS &&
3633 faddr < entry->end)
3634 vm_map_wire_entry_failure(map,
3635 entry, faddr);
3636 }
3637 }
3638 if (rv != KERN_SUCCESS) {
3639 vm_map_wire_entry_failure(map, entry, faddr);
3640 if (user_wire)
3641 vm_map_wire_user_count_sub(npages);
3642 end = entry->end;
3643 goto done;
3644 }
3645 } else if (!user_wire ||
3646 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3647 entry->wired_count++;
3648 }
3649 /*
3650 * Check the map for holes in the specified region.
3651 * If holes_ok was specified, skip this check.
3652 */
3653 next_entry = vm_map_entry_succ(entry);
3654 if (!holes_ok &&
3655 entry->end < end && next_entry->start > entry->end) {
3656 end = entry->end;
3657 rv = KERN_INVALID_ADDRESS;
3658 goto done;
3659 }
3660 }
3661 rv = KERN_SUCCESS;
3662 done:
3663 need_wakeup = false;
3664 if (first_entry == NULL &&
3665 !vm_map_lookup_entry(map, start, &first_entry)) {
3666 KASSERT(holes_ok, ("vm_map_wire: lookup failed"));
3667 prev_entry = first_entry;
3668 entry = vm_map_entry_succ(first_entry);
3669 } else {
3670 prev_entry = vm_map_entry_pred(first_entry);
3671 entry = first_entry;
3672 }
3673 for (; entry->start < end;
3674 prev_entry = entry, entry = vm_map_entry_succ(entry)) {
3675 /*
3676 * If holes_ok was specified, an empty
3677 * space in the unwired region could have been mapped
3678 * while the map lock was dropped for faulting in the
3679 * pages or draining MAP_ENTRY_IN_TRANSITION.
3680 * Moreover, another thread could be simultaneously
3681 * wiring this new mapping entry. Detect these cases
3682 * and skip any entries marked as in transition not by us.
3683 *
3684 * Another way to get an entry not marked with
3685 * MAP_ENTRY_IN_TRANSITION is after failed clipping,
3686 * which set rv to KERN_INVALID_ARGUMENT.
3687 */
3688 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
3689 entry->wiring_thread != curthread) {
3690 KASSERT(holes_ok || rv == KERN_INVALID_ARGUMENT,
3691 ("vm_map_wire: !HOLESOK and new/changed entry"));
3692 continue;
3693 }
3694
3695 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0) {
3696 /* do nothing */
3697 } else if (rv == KERN_SUCCESS) {
3698 if (user_wire)
3699 entry->eflags |= MAP_ENTRY_USER_WIRED;
3700 } else if (entry->wired_count == -1) {
3701 /*
3702 * Wiring failed on this entry. Thus, unwiring is
3703 * unnecessary.
3704 */
3705 entry->wired_count = 0;
3706 } else if (!user_wire ||
3707 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
3708 /*
3709 * Undo the wiring. Wiring succeeded on this entry
3710 * but failed on a later entry.
3711 */
3712 if (entry->wired_count == 1) {
3713 vm_map_entry_unwire(map, entry);
3714 if (user_wire)
3715 vm_map_wire_user_count_sub(
3716 atop(entry->end - entry->start));
3717 } else
3718 entry->wired_count--;
3719 }
3720 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
3721 ("vm_map_wire: in-transition flag missing %p", entry));
3722 KASSERT(entry->wiring_thread == curthread,
3723 ("vm_map_wire: alien wire %p", entry));
3724 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
3725 MAP_ENTRY_WIRE_SKIPPED);
3726 entry->wiring_thread = NULL;
3727 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
3728 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
3729 need_wakeup = true;
3730 }
3731 vm_map_try_merge_entries(map, prev_entry, entry);
3732 }
3733 vm_map_try_merge_entries(map, prev_entry, entry);
3734 if (need_wakeup)
3735 vm_map_wakeup(map);
3736 return (rv);
3737 }
3738
3739 /*
3740 * vm_map_sync
3741 *
3742 * Push any dirty cached pages in the address range to their pager.
3743 * If syncio is TRUE, dirty pages are written synchronously.
3744 * If invalidate is TRUE, any cached pages are freed as well.
3745 *
3746 * If the size of the region from start to end is zero, we are
3747 * supposed to flush all modified pages within the region containing
3748 * start. Unfortunately, a region can be split or coalesced with
3749 * neighboring regions, making it difficult to determine what the
3750 * original region was. Therefore, we approximate this requirement by
3751 * flushing the current region containing start.
3752 *
3753 * Returns an error if any part of the specified range is not mapped.
3754 */
3755 int
vm_map_sync(vm_map_t map,vm_offset_t start,vm_offset_t end,boolean_t syncio,boolean_t invalidate)3756 vm_map_sync(
3757 vm_map_t map,
3758 vm_offset_t start,
3759 vm_offset_t end,
3760 boolean_t syncio,
3761 boolean_t invalidate)
3762 {
3763 vm_map_entry_t entry, first_entry, next_entry;
3764 vm_size_t size;
3765 vm_object_t object;
3766 vm_ooffset_t offset;
3767 unsigned int last_timestamp;
3768 int bdry_idx;
3769 boolean_t failed;
3770
3771 vm_map_lock_read(map);
3772 VM_MAP_RANGE_CHECK(map, start, end);
3773 if (!vm_map_lookup_entry(map, start, &first_entry)) {
3774 vm_map_unlock_read(map);
3775 return (KERN_INVALID_ADDRESS);
3776 } else if (start == end) {
3777 start = first_entry->start;
3778 end = first_entry->end;
3779 }
3780
3781 /*
3782 * Make a first pass to check for user-wired memory, holes,
3783 * and partial invalidation of largepage mappings.
3784 */
3785 for (entry = first_entry; entry->start < end; entry = next_entry) {
3786 if (invalidate) {
3787 if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0) {
3788 vm_map_unlock_read(map);
3789 return (KERN_INVALID_ARGUMENT);
3790 }
3791 bdry_idx = MAP_ENTRY_SPLIT_BOUNDARY_INDEX(entry);
3792 if (bdry_idx != 0 &&
3793 ((start & (pagesizes[bdry_idx] - 1)) != 0 ||
3794 (end & (pagesizes[bdry_idx] - 1)) != 0)) {
3795 vm_map_unlock_read(map);
3796 return (KERN_INVALID_ARGUMENT);
3797 }
3798 }
3799 next_entry = vm_map_entry_succ(entry);
3800 if (end > entry->end &&
3801 entry->end != next_entry->start) {
3802 vm_map_unlock_read(map);
3803 return (KERN_INVALID_ADDRESS);
3804 }
3805 }
3806
3807 if (invalidate)
3808 pmap_remove(map->pmap, start, end);
3809 failed = FALSE;
3810
3811 /*
3812 * Make a second pass, cleaning/uncaching pages from the indicated
3813 * objects as we go.
3814 */
3815 for (entry = first_entry; entry->start < end;) {
3816 offset = entry->offset + (start - entry->start);
3817 size = (end <= entry->end ? end : entry->end) - start;
3818 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
3819 vm_map_t smap;
3820 vm_map_entry_t tentry;
3821 vm_size_t tsize;
3822
3823 smap = entry->object.sub_map;
3824 vm_map_lock_read(smap);
3825 (void) vm_map_lookup_entry(smap, offset, &tentry);
3826 tsize = tentry->end - offset;
3827 if (tsize < size)
3828 size = tsize;
3829 object = tentry->object.vm_object;
3830 offset = tentry->offset + (offset - tentry->start);
3831 vm_map_unlock_read(smap);
3832 } else {
3833 object = entry->object.vm_object;
3834 }
3835 vm_object_reference(object);
3836 last_timestamp = map->timestamp;
3837 vm_map_unlock_read(map);
3838 if (!vm_object_sync(object, offset, size, syncio, invalidate))
3839 failed = TRUE;
3840 start += size;
3841 vm_object_deallocate(object);
3842 vm_map_lock_read(map);
3843 if (last_timestamp == map->timestamp ||
3844 !vm_map_lookup_entry(map, start, &entry))
3845 entry = vm_map_entry_succ(entry);
3846 }
3847
3848 vm_map_unlock_read(map);
3849 return (failed ? KERN_FAILURE : KERN_SUCCESS);
3850 }
3851
3852 /*
3853 * vm_map_entry_unwire: [ internal use only ]
3854 *
3855 * Make the region specified by this entry pageable.
3856 *
3857 * The map in question should be locked.
3858 * [This is the reason for this routine's existence.]
3859 */
3860 static void
vm_map_entry_unwire(vm_map_t map,vm_map_entry_t entry)3861 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
3862 {
3863 vm_size_t size;
3864
3865 VM_MAP_ASSERT_LOCKED(map);
3866 KASSERT(entry->wired_count > 0,
3867 ("vm_map_entry_unwire: entry %p isn't wired", entry));
3868
3869 size = entry->end - entry->start;
3870 if ((entry->eflags & MAP_ENTRY_USER_WIRED) != 0)
3871 vm_map_wire_user_count_sub(atop(size));
3872 pmap_unwire(map->pmap, entry->start, entry->end);
3873 vm_object_unwire(entry->object.vm_object, entry->offset, size,
3874 PQ_ACTIVE);
3875 entry->wired_count = 0;
3876 }
3877
3878 static void
vm_map_entry_deallocate(vm_map_entry_t entry,boolean_t system_map)3879 vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
3880 {
3881
3882 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
3883 vm_object_deallocate(entry->object.vm_object);
3884 uma_zfree(system_map ? kmapentzone : mapentzone, entry);
3885 }
3886
3887 /*
3888 * vm_map_entry_delete: [ internal use only ]
3889 *
3890 * Deallocate the given entry from the target map.
3891 */
3892 static void
vm_map_entry_delete(vm_map_t map,vm_map_entry_t entry)3893 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3894 {
3895 vm_object_t object;
3896 vm_pindex_t offidxstart, offidxend, size1;
3897 vm_size_t size;
3898
3899 vm_map_entry_unlink(map, entry, UNLINK_MERGE_NONE);
3900 object = entry->object.vm_object;
3901
3902 if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
3903 MPASS(entry->cred == NULL);
3904 MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
3905 MPASS(object == NULL);
3906 vm_map_entry_deallocate(entry, map->system_map);
3907 return;
3908 }
3909
3910 size = entry->end - entry->start;
3911 map->size -= size;
3912
3913 if (entry->cred != NULL) {
3914 swap_release_by_cred(size, entry->cred);
3915 crfree(entry->cred);
3916 }
3917
3918 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 || object == NULL) {
3919 entry->object.vm_object = NULL;
3920 } else if ((object->flags & OBJ_ANON) != 0 ||
3921 object == kernel_object) {
3922 KASSERT(entry->cred == NULL || object->cred == NULL ||
3923 (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3924 ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3925 offidxstart = OFF_TO_IDX(entry->offset);
3926 offidxend = offidxstart + atop(size);
3927 VM_OBJECT_WLOCK(object);
3928 if (object->ref_count != 1 &&
3929 ((object->flags & OBJ_ONEMAPPING) != 0 ||
3930 object == kernel_object)) {
3931 vm_object_collapse(object);
3932
3933 /*
3934 * The option OBJPR_NOTMAPPED can be passed here
3935 * because vm_map_delete() already performed
3936 * pmap_remove() on the only mapping to this range
3937 * of pages.
3938 */
3939 vm_object_page_remove(object, offidxstart, offidxend,
3940 OBJPR_NOTMAPPED);
3941 if (offidxend >= object->size &&
3942 offidxstart < object->size) {
3943 size1 = object->size;
3944 object->size = offidxstart;
3945 if (object->cred != NULL) {
3946 size1 -= object->size;
3947 KASSERT(object->charge >= ptoa(size1),
3948 ("object %p charge < 0", object));
3949 swap_release_by_cred(ptoa(size1),
3950 object->cred);
3951 object->charge -= ptoa(size1);
3952 }
3953 }
3954 }
3955 VM_OBJECT_WUNLOCK(object);
3956 }
3957 if (map->system_map)
3958 vm_map_entry_deallocate(entry, TRUE);
3959 else {
3960 entry->defer_next = curthread->td_map_def_user;
3961 curthread->td_map_def_user = entry;
3962 }
3963 }
3964
3965 /*
3966 * vm_map_delete: [ internal use only ]
3967 *
3968 * Deallocates the given address range from the target
3969 * map.
3970 */
3971 int
vm_map_delete(vm_map_t map,vm_offset_t start,vm_offset_t end)3972 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3973 {
3974 vm_map_entry_t entry, next_entry, scratch_entry;
3975 int rv;
3976
3977 VM_MAP_ASSERT_LOCKED(map);
3978
3979 if (start == end)
3980 return (KERN_SUCCESS);
3981
3982 /*
3983 * Find the start of the region, and clip it.
3984 * Step through all entries in this region.
3985 */
3986 rv = vm_map_lookup_clip_start(map, start, &entry, &scratch_entry);
3987 if (rv != KERN_SUCCESS)
3988 return (rv);
3989 for (; entry->start < end; entry = next_entry) {
3990 /*
3991 * Wait for wiring or unwiring of an entry to complete.
3992 * Also wait for any system wirings to disappear on
3993 * user maps.
3994 */
3995 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
3996 (vm_map_pmap(map) != kernel_pmap &&
3997 vm_map_entry_system_wired_count(entry) != 0)) {
3998 unsigned int last_timestamp;
3999 vm_offset_t saved_start;
4000
4001 saved_start = entry->start;
4002 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
4003 last_timestamp = map->timestamp;
4004 (void) vm_map_unlock_and_wait(map, 0);
4005 vm_map_lock(map);
4006 if (last_timestamp + 1 != map->timestamp) {
4007 /*
4008 * Look again for the entry because the map was
4009 * modified while it was unlocked.
4010 * Specifically, the entry may have been
4011 * clipped, merged, or deleted.
4012 */
4013 rv = vm_map_lookup_clip_start(map, saved_start,
4014 &next_entry, &scratch_entry);
4015 if (rv != KERN_SUCCESS)
4016 break;
4017 } else
4018 next_entry = entry;
4019 continue;
4020 }
4021
4022 /* XXXKIB or delete to the upper superpage boundary ? */
4023 rv = vm_map_clip_end(map, entry, end);
4024 if (rv != KERN_SUCCESS)
4025 break;
4026 next_entry = vm_map_entry_succ(entry);
4027
4028 /*
4029 * Unwire before removing addresses from the pmap; otherwise,
4030 * unwiring will put the entries back in the pmap.
4031 */
4032 if (entry->wired_count != 0)
4033 vm_map_entry_unwire(map, entry);
4034
4035 /*
4036 * Remove mappings for the pages, but only if the
4037 * mappings could exist. For instance, it does not
4038 * make sense to call pmap_remove() for guard entries.
4039 */
4040 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 ||
4041 entry->object.vm_object != NULL)
4042 pmap_map_delete(map->pmap, entry->start, entry->end);
4043
4044 if (entry->end == map->anon_loc)
4045 map->anon_loc = entry->start;
4046
4047 /*
4048 * Delete the entry only after removing all pmap
4049 * entries pointing to its pages. (Otherwise, its
4050 * page frames may be reallocated, and any modify bits
4051 * will be set in the wrong object!)
4052 */
4053 vm_map_entry_delete(map, entry);
4054 }
4055 return (rv);
4056 }
4057
4058 /*
4059 * vm_map_remove:
4060 *
4061 * Remove the given address range from the target map.
4062 * This is the exported form of vm_map_delete.
4063 */
4064 int
vm_map_remove(vm_map_t map,vm_offset_t start,vm_offset_t end)4065 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
4066 {
4067 int result;
4068
4069 vm_map_lock(map);
4070 VM_MAP_RANGE_CHECK(map, start, end);
4071 result = vm_map_delete(map, start, end);
4072 vm_map_unlock(map);
4073 return (result);
4074 }
4075
4076 /*
4077 * vm_map_check_protection:
4078 *
4079 * Assert that the target map allows the specified privilege on the
4080 * entire address region given. The entire region must be allocated.
4081 *
4082 * WARNING! This code does not and should not check whether the
4083 * contents of the region is accessible. For example a smaller file
4084 * might be mapped into a larger address space.
4085 *
4086 * NOTE! This code is also called by munmap().
4087 *
4088 * The map must be locked. A read lock is sufficient.
4089 */
4090 boolean_t
vm_map_check_protection(vm_map_t map,vm_offset_t start,vm_offset_t end,vm_prot_t protection)4091 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
4092 vm_prot_t protection)
4093 {
4094 vm_map_entry_t entry;
4095 vm_map_entry_t tmp_entry;
4096
4097 if (!vm_map_lookup_entry(map, start, &tmp_entry))
4098 return (FALSE);
4099 entry = tmp_entry;
4100
4101 while (start < end) {
4102 /*
4103 * No holes allowed!
4104 */
4105 if (start < entry->start)
4106 return (FALSE);
4107 /*
4108 * Check protection associated with entry.
4109 */
4110 if ((entry->protection & protection) != protection)
4111 return (FALSE);
4112 /* go to next entry */
4113 start = entry->end;
4114 entry = vm_map_entry_succ(entry);
4115 }
4116 return (TRUE);
4117 }
4118
4119 /*
4120 *
4121 * vm_map_copy_swap_object:
4122 *
4123 * Copies a swap-backed object from an existing map entry to a
4124 * new one. Carries forward the swap charge. May change the
4125 * src object on return.
4126 */
4127 static void
vm_map_copy_swap_object(vm_map_entry_t src_entry,vm_map_entry_t dst_entry,vm_offset_t size,vm_ooffset_t * fork_charge)4128 vm_map_copy_swap_object(vm_map_entry_t src_entry, vm_map_entry_t dst_entry,
4129 vm_offset_t size, vm_ooffset_t *fork_charge)
4130 {
4131 vm_object_t src_object;
4132 struct ucred *cred;
4133 int charged;
4134
4135 src_object = src_entry->object.vm_object;
4136 charged = ENTRY_CHARGED(src_entry);
4137 if ((src_object->flags & OBJ_ANON) != 0) {
4138 VM_OBJECT_WLOCK(src_object);
4139 vm_object_collapse(src_object);
4140 if ((src_object->flags & OBJ_ONEMAPPING) != 0) {
4141 vm_object_split(src_entry);
4142 src_object = src_entry->object.vm_object;
4143 }
4144 vm_object_reference_locked(src_object);
4145 vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
4146 VM_OBJECT_WUNLOCK(src_object);
4147 } else
4148 vm_object_reference(src_object);
4149 if (src_entry->cred != NULL &&
4150 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
4151 KASSERT(src_object->cred == NULL,
4152 ("OVERCOMMIT: vm_map_copy_anon_entry: cred %p",
4153 src_object));
4154 src_object->cred = src_entry->cred;
4155 src_object->charge = size;
4156 }
4157 dst_entry->object.vm_object = src_object;
4158 if (charged) {
4159 cred = curthread->td_ucred;
4160 crhold(cred);
4161 dst_entry->cred = cred;
4162 *fork_charge += size;
4163 if (!(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
4164 crhold(cred);
4165 src_entry->cred = cred;
4166 *fork_charge += size;
4167 }
4168 }
4169 }
4170
4171 /*
4172 * vm_map_copy_entry:
4173 *
4174 * Copies the contents of the source entry to the destination
4175 * entry. The entries *must* be aligned properly.
4176 */
4177 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)4178 vm_map_copy_entry(
4179 vm_map_t src_map,
4180 vm_map_t dst_map,
4181 vm_map_entry_t src_entry,
4182 vm_map_entry_t dst_entry,
4183 vm_ooffset_t *fork_charge)
4184 {
4185 vm_object_t src_object;
4186 vm_map_entry_t fake_entry;
4187 vm_offset_t size;
4188
4189 VM_MAP_ASSERT_LOCKED(dst_map);
4190
4191 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
4192 return;
4193
4194 if (src_entry->wired_count == 0 ||
4195 (src_entry->protection & VM_PROT_WRITE) == 0) {
4196 /*
4197 * If the source entry is marked needs_copy, it is already
4198 * write-protected.
4199 */
4200 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
4201 (src_entry->protection & VM_PROT_WRITE) != 0) {
4202 pmap_protect(src_map->pmap,
4203 src_entry->start,
4204 src_entry->end,
4205 src_entry->protection & ~VM_PROT_WRITE);
4206 }
4207
4208 /*
4209 * Make a copy of the object.
4210 */
4211 size = src_entry->end - src_entry->start;
4212 if ((src_object = src_entry->object.vm_object) != NULL) {
4213 /*
4214 * Swap-backed objects need special handling. Note that
4215 * this is an unlocked check, so it is possible to race
4216 * with an OBJT_DEFAULT -> OBJT_SWAP conversion.
4217 */
4218 if (src_object->type == OBJT_DEFAULT ||
4219 src_object->type == OBJT_SWAP ||
4220 (src_object->flags & OBJ_SWAP) != 0) {
4221 vm_map_copy_swap_object(src_entry, dst_entry,
4222 size, fork_charge);
4223 /* May have split/collapsed, reload obj. */
4224 src_object = src_entry->object.vm_object;
4225 } else {
4226 vm_object_reference(src_object);
4227 dst_entry->object.vm_object = src_object;
4228 }
4229 src_entry->eflags |= MAP_ENTRY_COW |
4230 MAP_ENTRY_NEEDS_COPY;
4231 dst_entry->eflags |= MAP_ENTRY_COW |
4232 MAP_ENTRY_NEEDS_COPY;
4233 dst_entry->offset = src_entry->offset;
4234 if (src_entry->eflags & MAP_ENTRY_WRITECNT) {
4235 /*
4236 * MAP_ENTRY_WRITECNT cannot
4237 * indicate write reference from
4238 * src_entry, since the entry is
4239 * marked as needs copy. Allocate a
4240 * fake entry that is used to
4241 * decrement object->un_pager writecount
4242 * at the appropriate time. Attach
4243 * fake_entry to the deferred list.
4244 */
4245 fake_entry = vm_map_entry_create(dst_map);
4246 fake_entry->eflags = MAP_ENTRY_WRITECNT;
4247 src_entry->eflags &= ~MAP_ENTRY_WRITECNT;
4248 vm_object_reference(src_object);
4249 fake_entry->object.vm_object = src_object;
4250 fake_entry->start = src_entry->start;
4251 fake_entry->end = src_entry->end;
4252 fake_entry->defer_next =
4253 curthread->td_map_def_user;
4254 curthread->td_map_def_user = fake_entry;
4255 }
4256
4257 pmap_copy(dst_map->pmap, src_map->pmap,
4258 dst_entry->start, dst_entry->end - dst_entry->start,
4259 src_entry->start);
4260 } else {
4261 dst_entry->object.vm_object = NULL;
4262 if ((dst_entry->eflags & MAP_ENTRY_GUARD) == 0)
4263 dst_entry->offset = 0;
4264 if (src_entry->cred != NULL) {
4265 dst_entry->cred = curthread->td_ucred;
4266 crhold(dst_entry->cred);
4267 *fork_charge += size;
4268 }
4269 }
4270 } else {
4271 /*
4272 * We don't want to make writeable wired pages copy-on-write.
4273 * Immediately copy these pages into the new map by simulating
4274 * page faults. The new pages are pageable.
4275 */
4276 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
4277 fork_charge);
4278 }
4279 }
4280
4281 /*
4282 * vmspace_map_entry_forked:
4283 * Update the newly-forked vmspace each time a map entry is inherited
4284 * or copied. The values for vm_dsize and vm_tsize are approximate
4285 * (and mostly-obsolete ideas in the face of mmap(2) et al.)
4286 */
4287 static void
vmspace_map_entry_forked(const struct vmspace * vm1,struct vmspace * vm2,vm_map_entry_t entry)4288 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
4289 vm_map_entry_t entry)
4290 {
4291 vm_size_t entrysize;
4292 vm_offset_t newend;
4293
4294 if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
4295 return;
4296 entrysize = entry->end - entry->start;
4297 vm2->vm_map.size += entrysize;
4298 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
4299 vm2->vm_ssize += btoc(entrysize);
4300 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
4301 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
4302 newend = MIN(entry->end,
4303 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
4304 vm2->vm_dsize += btoc(newend - entry->start);
4305 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
4306 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
4307 newend = MIN(entry->end,
4308 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
4309 vm2->vm_tsize += btoc(newend - entry->start);
4310 }
4311 }
4312
4313 /*
4314 * vmspace_fork:
4315 * Create a new process vmspace structure and vm_map
4316 * based on those of an existing process. The new map
4317 * is based on the old map, according to the inheritance
4318 * values on the regions in that map.
4319 *
4320 * XXX It might be worth coalescing the entries added to the new vmspace.
4321 *
4322 * The source map must not be locked.
4323 */
4324 struct vmspace *
vmspace_fork(struct vmspace * vm1,vm_ooffset_t * fork_charge)4325 vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
4326 {
4327 struct vmspace *vm2;
4328 vm_map_t new_map, old_map;
4329 vm_map_entry_t new_entry, old_entry;
4330 vm_object_t object;
4331 int error, locked;
4332 vm_inherit_t inh;
4333
4334 old_map = &vm1->vm_map;
4335 /* Copy immutable fields of vm1 to vm2. */
4336 vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map),
4337 pmap_pinit);
4338 if (vm2 == NULL)
4339 return (NULL);
4340
4341 vm2->vm_taddr = vm1->vm_taddr;
4342 vm2->vm_daddr = vm1->vm_daddr;
4343 vm2->vm_maxsaddr = vm1->vm_maxsaddr;
4344 vm2->vm_stacktop = vm1->vm_stacktop;
4345 vm_map_lock(old_map);
4346 if (old_map->busy)
4347 vm_map_wait_busy(old_map);
4348 new_map = &vm2->vm_map;
4349 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
4350 KASSERT(locked, ("vmspace_fork: lock failed"));
4351
4352 error = pmap_vmspace_copy(new_map->pmap, old_map->pmap);
4353 if (error != 0) {
4354 sx_xunlock(&old_map->lock);
4355 sx_xunlock(&new_map->lock);
4356 vm_map_process_deferred();
4357 vmspace_free(vm2);
4358 return (NULL);
4359 }
4360
4361 new_map->anon_loc = old_map->anon_loc;
4362 new_map->flags |= old_map->flags & (MAP_ASLR | MAP_ASLR_IGNSTART |
4363 MAP_ASLR_STACK | MAP_WXORX);
4364
4365 VM_MAP_ENTRY_FOREACH(old_entry, old_map) {
4366 if ((old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
4367 panic("vm_map_fork: encountered a submap");
4368
4369 inh = old_entry->inheritance;
4370 if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
4371 inh != VM_INHERIT_NONE)
4372 inh = VM_INHERIT_COPY;
4373
4374 switch (inh) {
4375 case VM_INHERIT_NONE:
4376 break;
4377
4378 case VM_INHERIT_SHARE:
4379 /*
4380 * Clone the entry, creating the shared object if
4381 * necessary.
4382 */
4383 object = old_entry->object.vm_object;
4384 if (object == NULL) {
4385 vm_map_entry_back(old_entry);
4386 object = old_entry->object.vm_object;
4387 }
4388
4389 /*
4390 * Add the reference before calling vm_object_shadow
4391 * to insure that a shadow object is created.
4392 */
4393 vm_object_reference(object);
4394 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4395 vm_object_shadow(&old_entry->object.vm_object,
4396 &old_entry->offset,
4397 old_entry->end - old_entry->start,
4398 old_entry->cred,
4399 /* Transfer the second reference too. */
4400 true);
4401 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
4402 old_entry->cred = NULL;
4403
4404 /*
4405 * As in vm_map_merged_neighbor_dispose(),
4406 * the vnode lock will not be acquired in
4407 * this call to vm_object_deallocate().
4408 */
4409 vm_object_deallocate(object);
4410 object = old_entry->object.vm_object;
4411 } else {
4412 VM_OBJECT_WLOCK(object);
4413 vm_object_clear_flag(object, OBJ_ONEMAPPING);
4414 if (old_entry->cred != NULL) {
4415 KASSERT(object->cred == NULL,
4416 ("vmspace_fork both cred"));
4417 object->cred = old_entry->cred;
4418 object->charge = old_entry->end -
4419 old_entry->start;
4420 old_entry->cred = NULL;
4421 }
4422
4423 /*
4424 * Assert the correct state of the vnode
4425 * v_writecount while the object is locked, to
4426 * not relock it later for the assertion
4427 * correctness.
4428 */
4429 if (old_entry->eflags & MAP_ENTRY_WRITECNT &&
4430 object->type == OBJT_VNODE) {
4431 KASSERT(((struct vnode *)object->
4432 handle)->v_writecount > 0,
4433 ("vmspace_fork: v_writecount %p",
4434 object));
4435 KASSERT(object->un_pager.vnp.
4436 writemappings > 0,
4437 ("vmspace_fork: vnp.writecount %p",
4438 object));
4439 }
4440 VM_OBJECT_WUNLOCK(object);
4441 }
4442
4443 /*
4444 * Clone the entry, referencing the shared object.
4445 */
4446 new_entry = vm_map_entry_create(new_map);
4447 *new_entry = *old_entry;
4448 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4449 MAP_ENTRY_IN_TRANSITION);
4450 new_entry->wiring_thread = NULL;
4451 new_entry->wired_count = 0;
4452 if (new_entry->eflags & MAP_ENTRY_WRITECNT) {
4453 vm_pager_update_writecount(object,
4454 new_entry->start, new_entry->end);
4455 }
4456 vm_map_entry_set_vnode_text(new_entry, true);
4457
4458 /*
4459 * Insert the entry into the new map -- we know we're
4460 * inserting at the end of the new map.
4461 */
4462 vm_map_entry_link(new_map, new_entry);
4463 vmspace_map_entry_forked(vm1, vm2, new_entry);
4464
4465 /*
4466 * Update the physical map
4467 */
4468 pmap_copy(new_map->pmap, old_map->pmap,
4469 new_entry->start,
4470 (old_entry->end - old_entry->start),
4471 old_entry->start);
4472 break;
4473
4474 case VM_INHERIT_COPY:
4475 /*
4476 * Clone the entry and link into the map.
4477 */
4478 new_entry = vm_map_entry_create(new_map);
4479 *new_entry = *old_entry;
4480 /*
4481 * Copied entry is COW over the old object.
4482 */
4483 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
4484 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_WRITECNT);
4485 new_entry->wiring_thread = NULL;
4486 new_entry->wired_count = 0;
4487 new_entry->object.vm_object = NULL;
4488 new_entry->cred = NULL;
4489 vm_map_entry_link(new_map, new_entry);
4490 vmspace_map_entry_forked(vm1, vm2, new_entry);
4491 vm_map_copy_entry(old_map, new_map, old_entry,
4492 new_entry, fork_charge);
4493 vm_map_entry_set_vnode_text(new_entry, true);
4494 break;
4495
4496 case VM_INHERIT_ZERO:
4497 /*
4498 * Create a new anonymous mapping entry modelled from
4499 * the old one.
4500 */
4501 new_entry = vm_map_entry_create(new_map);
4502 memset(new_entry, 0, sizeof(*new_entry));
4503
4504 new_entry->start = old_entry->start;
4505 new_entry->end = old_entry->end;
4506 new_entry->eflags = old_entry->eflags &
4507 ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
4508 MAP_ENTRY_WRITECNT | MAP_ENTRY_VN_EXEC |
4509 MAP_ENTRY_SPLIT_BOUNDARY_MASK);
4510 new_entry->protection = old_entry->protection;
4511 new_entry->max_protection = old_entry->max_protection;
4512 new_entry->inheritance = VM_INHERIT_ZERO;
4513
4514 vm_map_entry_link(new_map, new_entry);
4515 vmspace_map_entry_forked(vm1, vm2, new_entry);
4516
4517 new_entry->cred = curthread->td_ucred;
4518 crhold(new_entry->cred);
4519 *fork_charge += (new_entry->end - new_entry->start);
4520
4521 break;
4522 }
4523 }
4524 /*
4525 * Use inlined vm_map_unlock() to postpone handling the deferred
4526 * map entries, which cannot be done until both old_map and
4527 * new_map locks are released.
4528 */
4529 sx_xunlock(&old_map->lock);
4530 sx_xunlock(&new_map->lock);
4531 vm_map_process_deferred();
4532
4533 return (vm2);
4534 }
4535
4536 /*
4537 * Create a process's stack for exec_new_vmspace(). This function is never
4538 * asked to wire the newly created stack.
4539 */
4540 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)4541 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4542 vm_prot_t prot, vm_prot_t max, int cow)
4543 {
4544 vm_size_t growsize, init_ssize;
4545 rlim_t vmemlim;
4546 int rv;
4547
4548 MPASS((map->flags & MAP_WIREFUTURE) == 0);
4549 growsize = sgrowsiz;
4550 init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
4551 vm_map_lock(map);
4552 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4553 /* If we would blow our VMEM resource limit, no go */
4554 if (map->size + init_ssize > vmemlim) {
4555 rv = KERN_NO_SPACE;
4556 goto out;
4557 }
4558 rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
4559 max, cow);
4560 out:
4561 vm_map_unlock(map);
4562 return (rv);
4563 }
4564
4565 static int stack_guard_page = 1;
4566 SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
4567 &stack_guard_page, 0,
4568 "Specifies the number of guard pages for a stack that grows");
4569
4570 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)4571 vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
4572 vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
4573 {
4574 vm_map_entry_t gap_entry, new_entry, prev_entry;
4575 vm_offset_t bot, gap_bot, gap_top, top;
4576 vm_size_t init_ssize, sgp;
4577 int orient, rv;
4578
4579 /*
4580 * The stack orientation is piggybacked with the cow argument.
4581 * Extract it into orient and mask the cow argument so that we
4582 * don't pass it around further.
4583 */
4584 orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
4585 KASSERT(orient != 0, ("No stack grow direction"));
4586 KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
4587 ("bi-dir stack"));
4588
4589 if (max_ssize == 0 ||
4590 !vm_map_range_valid(map, addrbos, addrbos + max_ssize))
4591 return (KERN_INVALID_ADDRESS);
4592 sgp = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4593 (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4594 (vm_size_t)stack_guard_page * PAGE_SIZE;
4595 if (sgp >= max_ssize)
4596 return (KERN_INVALID_ARGUMENT);
4597
4598 init_ssize = growsize;
4599 if (max_ssize < init_ssize + sgp)
4600 init_ssize = max_ssize - sgp;
4601
4602 /* If addr is already mapped, no go */
4603 if (vm_map_lookup_entry(map, addrbos, &prev_entry))
4604 return (KERN_NO_SPACE);
4605
4606 /*
4607 * If we can't accommodate max_ssize in the current mapping, no go.
4608 */
4609 if (vm_map_entry_succ(prev_entry)->start < addrbos + max_ssize)
4610 return (KERN_NO_SPACE);
4611
4612 /*
4613 * We initially map a stack of only init_ssize. We will grow as
4614 * needed later. Depending on the orientation of the stack (i.e.
4615 * the grow direction) we either map at the top of the range, the
4616 * bottom of the range or in the middle.
4617 *
4618 * Note: we would normally expect prot and max to be VM_PROT_ALL,
4619 * and cow to be 0. Possibly we should eliminate these as input
4620 * parameters, and just pass these values here in the insert call.
4621 */
4622 if (orient == MAP_STACK_GROWS_DOWN) {
4623 bot = addrbos + max_ssize - init_ssize;
4624 top = bot + init_ssize;
4625 gap_bot = addrbos;
4626 gap_top = bot;
4627 } else /* if (orient == MAP_STACK_GROWS_UP) */ {
4628 bot = addrbos;
4629 top = bot + init_ssize;
4630 gap_bot = top;
4631 gap_top = addrbos + max_ssize;
4632 }
4633 rv = vm_map_insert1(map, NULL, 0, bot, top, prot, max, cow,
4634 &new_entry);
4635 if (rv != KERN_SUCCESS)
4636 return (rv);
4637 KASSERT(new_entry->end == top || new_entry->start == bot,
4638 ("Bad entry start/end for new stack entry"));
4639 KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
4640 (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
4641 ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
4642 KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
4643 (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
4644 ("new entry lacks MAP_ENTRY_GROWS_UP"));
4645 if (gap_bot == gap_top)
4646 return (KERN_SUCCESS);
4647 rv = vm_map_insert1(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
4648 VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
4649 MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP), &gap_entry);
4650 if (rv == KERN_SUCCESS) {
4651 KASSERT((gap_entry->eflags & MAP_ENTRY_GUARD) != 0,
4652 ("entry %p not gap %#x", gap_entry, gap_entry->eflags));
4653 KASSERT((gap_entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
4654 MAP_ENTRY_STACK_GAP_UP)) != 0,
4655 ("entry %p not stack gap %#x", gap_entry,
4656 gap_entry->eflags));
4657
4658 /*
4659 * Gap can never successfully handle a fault, so
4660 * read-ahead logic is never used for it. Re-use
4661 * next_read of the gap entry to store
4662 * stack_guard_page for vm_map_growstack().
4663 * Similarly, since a gap cannot have a backing object,
4664 * store the original stack protections in the
4665 * object offset.
4666 */
4667 gap_entry->next_read = sgp;
4668 gap_entry->offset = prot | PROT_MAX(max);
4669 } else {
4670 (void)vm_map_delete(map, bot, top);
4671 }
4672 return (rv);
4673 }
4674
4675 /*
4676 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we
4677 * successfully grow the stack.
4678 */
4679 static int
vm_map_growstack(vm_map_t map,vm_offset_t addr,vm_map_entry_t gap_entry)4680 vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
4681 {
4682 vm_map_entry_t stack_entry;
4683 struct proc *p;
4684 struct vmspace *vm;
4685 struct ucred *cred;
4686 vm_offset_t gap_end, gap_start, grow_start;
4687 vm_size_t grow_amount, guard, max_grow, sgp;
4688 vm_prot_t prot, max;
4689 rlim_t lmemlim, stacklim, vmemlim;
4690 int rv, rv1;
4691 bool gap_deleted, grow_down, is_procstack;
4692 #ifdef notyet
4693 uint64_t limit;
4694 #endif
4695 #ifdef RACCT
4696 int error;
4697 #endif
4698
4699 p = curproc;
4700 vm = p->p_vmspace;
4701
4702 /*
4703 * Disallow stack growth when the access is performed by a
4704 * debugger or AIO daemon. The reason is that the wrong
4705 * resource limits are applied.
4706 */
4707 if (p != initproc && (map != &p->p_vmspace->vm_map ||
4708 p->p_textvp == NULL))
4709 return (KERN_FAILURE);
4710
4711 MPASS(!map->system_map);
4712
4713 lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
4714 stacklim = lim_cur(curthread, RLIMIT_STACK);
4715 vmemlim = lim_cur(curthread, RLIMIT_VMEM);
4716 retry:
4717 /* If addr is not in a hole for a stack grow area, no need to grow. */
4718 if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
4719 return (KERN_FAILURE);
4720 if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
4721 return (KERN_SUCCESS);
4722 if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
4723 stack_entry = vm_map_entry_succ(gap_entry);
4724 if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
4725 stack_entry->start != gap_entry->end)
4726 return (KERN_FAILURE);
4727 grow_amount = round_page(stack_entry->start - addr);
4728 grow_down = true;
4729 } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
4730 stack_entry = vm_map_entry_pred(gap_entry);
4731 if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
4732 stack_entry->end != gap_entry->start)
4733 return (KERN_FAILURE);
4734 grow_amount = round_page(addr + 1 - stack_entry->end);
4735 grow_down = false;
4736 } else {
4737 return (KERN_FAILURE);
4738 }
4739 guard = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ||
4740 (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 :
4741 gap_entry->next_read;
4742 max_grow = gap_entry->end - gap_entry->start;
4743 if (guard > max_grow)
4744 return (KERN_NO_SPACE);
4745 max_grow -= guard;
4746 if (grow_amount > max_grow)
4747 return (KERN_NO_SPACE);
4748
4749 /*
4750 * If this is the main process stack, see if we're over the stack
4751 * limit.
4752 */
4753 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
4754 addr < (vm_offset_t)vm->vm_stacktop;
4755 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
4756 return (KERN_NO_SPACE);
4757
4758 #ifdef RACCT
4759 if (racct_enable) {
4760 PROC_LOCK(p);
4761 if (is_procstack && racct_set(p, RACCT_STACK,
4762 ctob(vm->vm_ssize) + grow_amount)) {
4763 PROC_UNLOCK(p);
4764 return (KERN_NO_SPACE);
4765 }
4766 PROC_UNLOCK(p);
4767 }
4768 #endif
4769
4770 grow_amount = roundup(grow_amount, sgrowsiz);
4771 if (grow_amount > max_grow)
4772 grow_amount = max_grow;
4773 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
4774 grow_amount = trunc_page((vm_size_t)stacklim) -
4775 ctob(vm->vm_ssize);
4776 }
4777
4778 #ifdef notyet
4779 PROC_LOCK(p);
4780 limit = racct_get_available(p, RACCT_STACK);
4781 PROC_UNLOCK(p);
4782 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
4783 grow_amount = limit - ctob(vm->vm_ssize);
4784 #endif
4785
4786 if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
4787 if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
4788 rv = KERN_NO_SPACE;
4789 goto out;
4790 }
4791 #ifdef RACCT
4792 if (racct_enable) {
4793 PROC_LOCK(p);
4794 if (racct_set(p, RACCT_MEMLOCK,
4795 ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
4796 PROC_UNLOCK(p);
4797 rv = KERN_NO_SPACE;
4798 goto out;
4799 }
4800 PROC_UNLOCK(p);
4801 }
4802 #endif
4803 }
4804
4805 /* If we would blow our VMEM resource limit, no go */
4806 if (map->size + grow_amount > vmemlim) {
4807 rv = KERN_NO_SPACE;
4808 goto out;
4809 }
4810 #ifdef RACCT
4811 if (racct_enable) {
4812 PROC_LOCK(p);
4813 if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
4814 PROC_UNLOCK(p);
4815 rv = KERN_NO_SPACE;
4816 goto out;
4817 }
4818 PROC_UNLOCK(p);
4819 }
4820 #endif
4821
4822 if (vm_map_lock_upgrade(map)) {
4823 gap_entry = NULL;
4824 vm_map_lock_read(map);
4825 goto retry;
4826 }
4827
4828 if (grow_down) {
4829 /*
4830 * The gap_entry "offset" field is overloaded. See
4831 * vm_map_stack_locked().
4832 */
4833 prot = PROT_EXTRACT(gap_entry->offset);
4834 max = PROT_MAX_EXTRACT(gap_entry->offset);
4835 sgp = gap_entry->next_read;
4836
4837 grow_start = gap_entry->end - grow_amount;
4838 if (gap_entry->start + grow_amount == gap_entry->end) {
4839 gap_start = gap_entry->start;
4840 gap_end = gap_entry->end;
4841 vm_map_entry_delete(map, gap_entry);
4842 gap_deleted = true;
4843 } else {
4844 MPASS(gap_entry->start < gap_entry->end - grow_amount);
4845 vm_map_entry_resize(map, gap_entry, -grow_amount);
4846 gap_deleted = false;
4847 }
4848 rv = vm_map_insert(map, NULL, 0, grow_start,
4849 grow_start + grow_amount, prot, max, MAP_STACK_GROWS_DOWN);
4850 if (rv != KERN_SUCCESS) {
4851 if (gap_deleted) {
4852 rv1 = vm_map_insert1(map, NULL, 0, gap_start,
4853 gap_end, VM_PROT_NONE, VM_PROT_NONE,
4854 MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN,
4855 &gap_entry);
4856 MPASS(rv1 == KERN_SUCCESS);
4857 gap_entry->next_read = sgp;
4858 gap_entry->offset = prot | PROT_MAX(max);
4859 } else
4860 vm_map_entry_resize(map, gap_entry,
4861 grow_amount);
4862 }
4863 } else {
4864 grow_start = stack_entry->end;
4865 cred = stack_entry->cred;
4866 if (cred == NULL && stack_entry->object.vm_object != NULL)
4867 cred = stack_entry->object.vm_object->cred;
4868 if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
4869 rv = KERN_NO_SPACE;
4870 /* Grow the underlying object if applicable. */
4871 else if (stack_entry->object.vm_object == NULL ||
4872 vm_object_coalesce(stack_entry->object.vm_object,
4873 stack_entry->offset,
4874 (vm_size_t)(stack_entry->end - stack_entry->start),
4875 grow_amount, cred != NULL)) {
4876 if (gap_entry->start + grow_amount == gap_entry->end) {
4877 vm_map_entry_delete(map, gap_entry);
4878 vm_map_entry_resize(map, stack_entry,
4879 grow_amount);
4880 } else {
4881 gap_entry->start += grow_amount;
4882 stack_entry->end += grow_amount;
4883 }
4884 map->size += grow_amount;
4885 rv = KERN_SUCCESS;
4886 } else
4887 rv = KERN_FAILURE;
4888 }
4889 if (rv == KERN_SUCCESS && is_procstack)
4890 vm->vm_ssize += btoc(grow_amount);
4891
4892 /*
4893 * Heed the MAP_WIREFUTURE flag if it was set for this process.
4894 */
4895 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
4896 rv = vm_map_wire_locked(map, grow_start,
4897 grow_start + grow_amount,
4898 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
4899 }
4900 vm_map_lock_downgrade(map);
4901
4902 out:
4903 #ifdef RACCT
4904 if (racct_enable && rv != KERN_SUCCESS) {
4905 PROC_LOCK(p);
4906 error = racct_set(p, RACCT_VMEM, map->size);
4907 KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
4908 if (!old_mlock) {
4909 error = racct_set(p, RACCT_MEMLOCK,
4910 ptoa(pmap_wired_count(map->pmap)));
4911 KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
4912 }
4913 error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
4914 KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
4915 PROC_UNLOCK(p);
4916 }
4917 #endif
4918
4919 return (rv);
4920 }
4921
4922 /*
4923 * Unshare the specified VM space for exec. If other processes are
4924 * mapped to it, then create a new one. The new vmspace is null.
4925 */
4926 int
vmspace_exec(struct proc * p,vm_offset_t minuser,vm_offset_t maxuser)4927 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
4928 {
4929 struct vmspace *oldvmspace = p->p_vmspace;
4930 struct vmspace *newvmspace;
4931
4932 KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
4933 ("vmspace_exec recursed"));
4934 newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit);
4935 if (newvmspace == NULL)
4936 return (ENOMEM);
4937 newvmspace->vm_swrss = oldvmspace->vm_swrss;
4938 /*
4939 * This code is written like this for prototype purposes. The
4940 * goal is to avoid running down the vmspace here, but let the
4941 * other process's that are still using the vmspace to finally
4942 * run it down. Even though there is little or no chance of blocking
4943 * here, it is a good idea to keep this form for future mods.
4944 */
4945 PROC_VMSPACE_LOCK(p);
4946 p->p_vmspace = newvmspace;
4947 PROC_VMSPACE_UNLOCK(p);
4948 if (p == curthread->td_proc)
4949 pmap_activate(curthread);
4950 curthread->td_pflags |= TDP_EXECVMSPC;
4951 return (0);
4952 }
4953
4954 /*
4955 * Unshare the specified VM space for forcing COW. This
4956 * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
4957 */
4958 int
vmspace_unshare(struct proc * p)4959 vmspace_unshare(struct proc *p)
4960 {
4961 struct vmspace *oldvmspace = p->p_vmspace;
4962 struct vmspace *newvmspace;
4963 vm_ooffset_t fork_charge;
4964
4965 /*
4966 * The caller is responsible for ensuring that the reference count
4967 * cannot concurrently transition 1 -> 2.
4968 */
4969 if (refcount_load(&oldvmspace->vm_refcnt) == 1)
4970 return (0);
4971 fork_charge = 0;
4972 newvmspace = vmspace_fork(oldvmspace, &fork_charge);
4973 if (newvmspace == NULL)
4974 return (ENOMEM);
4975 if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
4976 vmspace_free(newvmspace);
4977 return (ENOMEM);
4978 }
4979 PROC_VMSPACE_LOCK(p);
4980 p->p_vmspace = newvmspace;
4981 PROC_VMSPACE_UNLOCK(p);
4982 if (p == curthread->td_proc)
4983 pmap_activate(curthread);
4984 vmspace_free(oldvmspace);
4985 return (0);
4986 }
4987
4988 /*
4989 * vm_map_lookup:
4990 *
4991 * Finds the VM object, offset, and
4992 * protection for a given virtual address in the
4993 * specified map, assuming a page fault of the
4994 * type specified.
4995 *
4996 * Leaves the map in question locked for read; return
4997 * values are guaranteed until a vm_map_lookup_done
4998 * call is performed. Note that the map argument
4999 * is in/out; the returned map must be used in
5000 * the call to vm_map_lookup_done.
5001 *
5002 * A handle (out_entry) is returned for use in
5003 * vm_map_lookup_done, to make that fast.
5004 *
5005 * If a lookup is requested with "write protection"
5006 * specified, the map may be changed to perform virtual
5007 * copying operations, although the data referenced will
5008 * remain the same.
5009 */
5010 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)5011 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
5012 vm_offset_t vaddr,
5013 vm_prot_t fault_typea,
5014 vm_map_entry_t *out_entry, /* OUT */
5015 vm_object_t *object, /* OUT */
5016 vm_pindex_t *pindex, /* OUT */
5017 vm_prot_t *out_prot, /* OUT */
5018 boolean_t *wired) /* OUT */
5019 {
5020 vm_map_entry_t entry;
5021 vm_map_t map = *var_map;
5022 vm_prot_t prot;
5023 vm_prot_t fault_type;
5024 vm_object_t eobject;
5025 vm_size_t size;
5026 struct ucred *cred;
5027
5028 RetryLookup:
5029
5030 vm_map_lock_read(map);
5031
5032 RetryLookupLocked:
5033 /*
5034 * Lookup the faulting address.
5035 */
5036 if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
5037 vm_map_unlock_read(map);
5038 return (KERN_INVALID_ADDRESS);
5039 }
5040
5041 entry = *out_entry;
5042
5043 /*
5044 * Handle submaps.
5045 */
5046 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
5047 vm_map_t old_map = map;
5048
5049 *var_map = map = entry->object.sub_map;
5050 vm_map_unlock_read(old_map);
5051 goto RetryLookup;
5052 }
5053
5054 /*
5055 * Check whether this task is allowed to have this page.
5056 */
5057 prot = entry->protection;
5058 if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
5059 fault_typea &= ~VM_PROT_FAULT_LOOKUP;
5060 if (prot == VM_PROT_NONE && map != kernel_map &&
5061 (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
5062 (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
5063 MAP_ENTRY_STACK_GAP_UP)) != 0 &&
5064 vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
5065 goto RetryLookupLocked;
5066 }
5067 fault_type = fault_typea & VM_PROT_ALL;
5068 if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
5069 vm_map_unlock_read(map);
5070 return (KERN_PROTECTION_FAILURE);
5071 }
5072 KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
5073 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
5074 (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
5075 ("entry %p flags %x", entry, entry->eflags));
5076 if ((fault_typea & VM_PROT_COPY) != 0 &&
5077 (entry->max_protection & VM_PROT_WRITE) == 0 &&
5078 (entry->eflags & MAP_ENTRY_COW) == 0) {
5079 vm_map_unlock_read(map);
5080 return (KERN_PROTECTION_FAILURE);
5081 }
5082
5083 /*
5084 * If this page is not pageable, we have to get it for all possible
5085 * accesses.
5086 */
5087 *wired = (entry->wired_count != 0);
5088 if (*wired)
5089 fault_type = entry->protection;
5090 size = entry->end - entry->start;
5091
5092 /*
5093 * If the entry was copy-on-write, we either ...
5094 */
5095 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
5096 /*
5097 * If we want to write the page, we may as well handle that
5098 * now since we've got the map locked.
5099 *
5100 * If we don't need to write the page, we just demote the
5101 * permissions allowed.
5102 */
5103 if ((fault_type & VM_PROT_WRITE) != 0 ||
5104 (fault_typea & VM_PROT_COPY) != 0) {
5105 /*
5106 * Make a new object, and place it in the object
5107 * chain. Note that no new references have appeared
5108 * -- one just moved from the map to the new
5109 * object.
5110 */
5111 if (vm_map_lock_upgrade(map))
5112 goto RetryLookup;
5113
5114 if (entry->cred == NULL) {
5115 /*
5116 * The debugger owner is charged for
5117 * the memory.
5118 */
5119 cred = curthread->td_ucred;
5120 crhold(cred);
5121 if (!swap_reserve_by_cred(size, cred)) {
5122 crfree(cred);
5123 vm_map_unlock(map);
5124 return (KERN_RESOURCE_SHORTAGE);
5125 }
5126 entry->cred = cred;
5127 }
5128 eobject = entry->object.vm_object;
5129 vm_object_shadow(&entry->object.vm_object,
5130 &entry->offset, size, entry->cred, false);
5131 if (eobject == entry->object.vm_object) {
5132 /*
5133 * The object was not shadowed.
5134 */
5135 swap_release_by_cred(size, entry->cred);
5136 crfree(entry->cred);
5137 }
5138 entry->cred = NULL;
5139 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
5140
5141 vm_map_lock_downgrade(map);
5142 } else {
5143 /*
5144 * We're attempting to read a copy-on-write page --
5145 * don't allow writes.
5146 */
5147 prot &= ~VM_PROT_WRITE;
5148 }
5149 }
5150
5151 /*
5152 * Create an object if necessary.
5153 */
5154 if (entry->object.vm_object == NULL && !map->system_map) {
5155 if (vm_map_lock_upgrade(map))
5156 goto RetryLookup;
5157 entry->object.vm_object = vm_object_allocate_anon(atop(size),
5158 NULL, entry->cred, size);
5159 entry->offset = 0;
5160 entry->cred = NULL;
5161 vm_map_lock_downgrade(map);
5162 }
5163
5164 /*
5165 * Return the object/offset from this entry. If the entry was
5166 * copy-on-write or empty, it has been fixed up.
5167 */
5168 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
5169 *object = entry->object.vm_object;
5170
5171 *out_prot = prot;
5172 return (KERN_SUCCESS);
5173 }
5174
5175 /*
5176 * vm_map_lookup_locked:
5177 *
5178 * Lookup the faulting address. A version of vm_map_lookup that returns
5179 * KERN_FAILURE instead of blocking on map lock or memory allocation.
5180 */
5181 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)5182 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
5183 vm_offset_t vaddr,
5184 vm_prot_t fault_typea,
5185 vm_map_entry_t *out_entry, /* OUT */
5186 vm_object_t *object, /* OUT */
5187 vm_pindex_t *pindex, /* OUT */
5188 vm_prot_t *out_prot, /* OUT */
5189 boolean_t *wired) /* OUT */
5190 {
5191 vm_map_entry_t entry;
5192 vm_map_t map = *var_map;
5193 vm_prot_t prot;
5194 vm_prot_t fault_type = fault_typea;
5195
5196 /*
5197 * Lookup the faulting address.
5198 */
5199 if (!vm_map_lookup_entry(map, vaddr, out_entry))
5200 return (KERN_INVALID_ADDRESS);
5201
5202 entry = *out_entry;
5203
5204 /*
5205 * Fail if the entry refers to a submap.
5206 */
5207 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
5208 return (KERN_FAILURE);
5209
5210 /*
5211 * Check whether this task is allowed to have this page.
5212 */
5213 prot = entry->protection;
5214 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
5215 if ((fault_type & prot) != fault_type)
5216 return (KERN_PROTECTION_FAILURE);
5217
5218 /*
5219 * If this page is not pageable, we have to get it for all possible
5220 * accesses.
5221 */
5222 *wired = (entry->wired_count != 0);
5223 if (*wired)
5224 fault_type = entry->protection;
5225
5226 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
5227 /*
5228 * Fail if the entry was copy-on-write for a write fault.
5229 */
5230 if (fault_type & VM_PROT_WRITE)
5231 return (KERN_FAILURE);
5232 /*
5233 * We're attempting to read a copy-on-write page --
5234 * don't allow writes.
5235 */
5236 prot &= ~VM_PROT_WRITE;
5237 }
5238
5239 /*
5240 * Fail if an object should be created.
5241 */
5242 if (entry->object.vm_object == NULL && !map->system_map)
5243 return (KERN_FAILURE);
5244
5245 /*
5246 * Return the object/offset from this entry. If the entry was
5247 * copy-on-write or empty, it has been fixed up.
5248 */
5249 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
5250 *object = entry->object.vm_object;
5251
5252 *out_prot = prot;
5253 return (KERN_SUCCESS);
5254 }
5255
5256 /*
5257 * vm_map_lookup_done:
5258 *
5259 * Releases locks acquired by a vm_map_lookup
5260 * (according to the handle returned by that lookup).
5261 */
5262 void
vm_map_lookup_done(vm_map_t map,vm_map_entry_t entry)5263 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
5264 {
5265 /*
5266 * Unlock the main-level map
5267 */
5268 vm_map_unlock_read(map);
5269 }
5270
5271 vm_offset_t
vm_map_max_KBI(const struct vm_map * map)5272 vm_map_max_KBI(const struct vm_map *map)
5273 {
5274
5275 return (vm_map_max(map));
5276 }
5277
5278 vm_offset_t
vm_map_min_KBI(const struct vm_map * map)5279 vm_map_min_KBI(const struct vm_map *map)
5280 {
5281
5282 return (vm_map_min(map));
5283 }
5284
5285 pmap_t
vm_map_pmap_KBI(vm_map_t map)5286 vm_map_pmap_KBI(vm_map_t map)
5287 {
5288
5289 return (map->pmap);
5290 }
5291
5292 bool
vm_map_range_valid_KBI(vm_map_t map,vm_offset_t start,vm_offset_t end)5293 vm_map_range_valid_KBI(vm_map_t map, vm_offset_t start, vm_offset_t end)
5294 {
5295
5296 return (vm_map_range_valid(map, start, end));
5297 }
5298
5299 #ifdef INVARIANTS
5300 static void
_vm_map_assert_consistent(vm_map_t map,int check)5301 _vm_map_assert_consistent(vm_map_t map, int check)
5302 {
5303 vm_map_entry_t entry, prev;
5304 vm_map_entry_t cur, header, lbound, ubound;
5305 vm_size_t max_left, max_right;
5306
5307 #ifdef DIAGNOSTIC
5308 ++map->nupdates;
5309 #endif
5310 if (enable_vmmap_check != check)
5311 return;
5312
5313 header = prev = &map->header;
5314 VM_MAP_ENTRY_FOREACH(entry, map) {
5315 KASSERT(prev->end <= entry->start,
5316 ("map %p prev->end = %jx, start = %jx", map,
5317 (uintmax_t)prev->end, (uintmax_t)entry->start));
5318 KASSERT(entry->start < entry->end,
5319 ("map %p start = %jx, end = %jx", map,
5320 (uintmax_t)entry->start, (uintmax_t)entry->end));
5321 KASSERT(entry->left == header ||
5322 entry->left->start < entry->start,
5323 ("map %p left->start = %jx, start = %jx", map,
5324 (uintmax_t)entry->left->start, (uintmax_t)entry->start));
5325 KASSERT(entry->right == header ||
5326 entry->start < entry->right->start,
5327 ("map %p start = %jx, right->start = %jx", map,
5328 (uintmax_t)entry->start, (uintmax_t)entry->right->start));
5329 cur = map->root;
5330 lbound = ubound = header;
5331 for (;;) {
5332 if (entry->start < cur->start) {
5333 ubound = cur;
5334 cur = cur->left;
5335 KASSERT(cur != lbound,
5336 ("map %p cannot find %jx",
5337 map, (uintmax_t)entry->start));
5338 } else if (cur->end <= entry->start) {
5339 lbound = cur;
5340 cur = cur->right;
5341 KASSERT(cur != ubound,
5342 ("map %p cannot find %jx",
5343 map, (uintmax_t)entry->start));
5344 } else {
5345 KASSERT(cur == entry,
5346 ("map %p cannot find %jx",
5347 map, (uintmax_t)entry->start));
5348 break;
5349 }
5350 }
5351 max_left = vm_map_entry_max_free_left(entry, lbound);
5352 max_right = vm_map_entry_max_free_right(entry, ubound);
5353 KASSERT(entry->max_free == vm_size_max(max_left, max_right),
5354 ("map %p max = %jx, max_left = %jx, max_right = %jx", map,
5355 (uintmax_t)entry->max_free,
5356 (uintmax_t)max_left, (uintmax_t)max_right));
5357 prev = entry;
5358 }
5359 KASSERT(prev->end <= entry->start,
5360 ("map %p prev->end = %jx, start = %jx", map,
5361 (uintmax_t)prev->end, (uintmax_t)entry->start));
5362 }
5363 #endif
5364
5365 #include "opt_ddb.h"
5366 #ifdef DDB
5367 #include <sys/kernel.h>
5368
5369 #include <ddb/ddb.h>
5370
5371 static void
vm_map_print(vm_map_t map)5372 vm_map_print(vm_map_t map)
5373 {
5374 vm_map_entry_t entry, prev;
5375
5376 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
5377 (void *)map,
5378 (void *)map->pmap, map->nentries, map->timestamp);
5379
5380 db_indent += 2;
5381 prev = &map->header;
5382 VM_MAP_ENTRY_FOREACH(entry, map) {
5383 db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
5384 (void *)entry, (void *)entry->start, (void *)entry->end,
5385 entry->eflags);
5386 {
5387 static const char * const inheritance_name[4] =
5388 {"share", "copy", "none", "donate_copy"};
5389
5390 db_iprintf(" prot=%x/%x/%s",
5391 entry->protection,
5392 entry->max_protection,
5393 inheritance_name[(int)(unsigned char)
5394 entry->inheritance]);
5395 if (entry->wired_count != 0)
5396 db_printf(", wired");
5397 }
5398 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
5399 db_printf(", share=%p, offset=0x%jx\n",
5400 (void *)entry->object.sub_map,
5401 (uintmax_t)entry->offset);
5402 if (prev == &map->header ||
5403 prev->object.sub_map !=
5404 entry->object.sub_map) {
5405 db_indent += 2;
5406 vm_map_print((vm_map_t)entry->object.sub_map);
5407 db_indent -= 2;
5408 }
5409 } else {
5410 if (entry->cred != NULL)
5411 db_printf(", ruid %d", entry->cred->cr_ruid);
5412 db_printf(", object=%p, offset=0x%jx",
5413 (void *)entry->object.vm_object,
5414 (uintmax_t)entry->offset);
5415 if (entry->object.vm_object && entry->object.vm_object->cred)
5416 db_printf(", obj ruid %d charge %jx",
5417 entry->object.vm_object->cred->cr_ruid,
5418 (uintmax_t)entry->object.vm_object->charge);
5419 if (entry->eflags & MAP_ENTRY_COW)
5420 db_printf(", copy (%s)",
5421 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
5422 db_printf("\n");
5423
5424 if (prev == &map->header ||
5425 prev->object.vm_object !=
5426 entry->object.vm_object) {
5427 db_indent += 2;
5428 vm_object_print((db_expr_t)(intptr_t)
5429 entry->object.vm_object,
5430 0, 0, (char *)0);
5431 db_indent -= 2;
5432 }
5433 }
5434 prev = entry;
5435 }
5436 db_indent -= 2;
5437 }
5438
DB_SHOW_COMMAND(map,map)5439 DB_SHOW_COMMAND(map, map)
5440 {
5441
5442 if (!have_addr) {
5443 db_printf("usage: show map <addr>\n");
5444 return;
5445 }
5446 vm_map_print((vm_map_t)addr);
5447 }
5448
DB_SHOW_COMMAND(procvm,procvm)5449 DB_SHOW_COMMAND(procvm, procvm)
5450 {
5451 struct proc *p;
5452
5453 if (have_addr) {
5454 p = db_lookup_proc(addr);
5455 } else {
5456 p = curproc;
5457 }
5458
5459 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
5460 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
5461 (void *)vmspace_pmap(p->p_vmspace));
5462
5463 vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
5464 }
5465
5466 #endif /* DDB */
5467