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