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