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