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