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