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