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