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