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