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