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