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