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 - VM_MIN_KERNEL_ADDRESS) / 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, int 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 do { 1212 if (find_space != VMFS_NO_SPACE) { 1213 if (vm_map_findspace(map, start, length, addr)) { 1214 vm_map_unlock(map); 1215 return (KERN_NO_SPACE); 1216 } 1217 if (find_space == VMFS_ALIGNED_SPACE) 1218 pmap_align_superpage(object, offset, addr, 1219 length); 1220 start = *addr; 1221 } 1222 result = vm_map_insert(map, object, offset, start, start + 1223 length, prot, max, cow); 1224 } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE); 1225 vm_map_unlock(map); 1226 return (result); 1227 } 1228 1229 /* 1230 * vm_map_simplify_entry: 1231 * 1232 * Simplify the given map entry by merging with either neighbor. This 1233 * routine also has the ability to merge with both neighbors. 1234 * 1235 * The map must be locked. 1236 * 1237 * This routine guarentees that the passed entry remains valid (though 1238 * possibly extended). When merging, this routine may delete one or 1239 * both neighbors. 1240 */ 1241 void 1242 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 1243 { 1244 vm_map_entry_t next, prev; 1245 vm_size_t prevsize, esize; 1246 1247 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) 1248 return; 1249 1250 prev = entry->prev; 1251 if (prev != &map->header) { 1252 prevsize = prev->end - prev->start; 1253 if ( (prev->end == entry->start) && 1254 (prev->object.vm_object == entry->object.vm_object) && 1255 (!prev->object.vm_object || 1256 (prev->offset + prevsize == entry->offset)) && 1257 (prev->eflags == entry->eflags) && 1258 (prev->protection == entry->protection) && 1259 (prev->max_protection == entry->max_protection) && 1260 (prev->inheritance == entry->inheritance) && 1261 (prev->wired_count == entry->wired_count)) { 1262 vm_map_entry_unlink(map, prev); 1263 entry->start = prev->start; 1264 entry->offset = prev->offset; 1265 if (entry->prev != &map->header) 1266 vm_map_entry_resize_free(map, entry->prev); 1267 if (prev->object.vm_object) 1268 vm_object_deallocate(prev->object.vm_object); 1269 vm_map_entry_dispose(map, prev); 1270 } 1271 } 1272 1273 next = entry->next; 1274 if (next != &map->header) { 1275 esize = entry->end - entry->start; 1276 if ((entry->end == next->start) && 1277 (next->object.vm_object == entry->object.vm_object) && 1278 (!entry->object.vm_object || 1279 (entry->offset + esize == next->offset)) && 1280 (next->eflags == entry->eflags) && 1281 (next->protection == entry->protection) && 1282 (next->max_protection == entry->max_protection) && 1283 (next->inheritance == entry->inheritance) && 1284 (next->wired_count == entry->wired_count)) { 1285 vm_map_entry_unlink(map, next); 1286 entry->end = next->end; 1287 vm_map_entry_resize_free(map, entry); 1288 if (next->object.vm_object) 1289 vm_object_deallocate(next->object.vm_object); 1290 vm_map_entry_dispose(map, next); 1291 } 1292 } 1293 } 1294 /* 1295 * vm_map_clip_start: [ internal use only ] 1296 * 1297 * Asserts that the given entry begins at or after 1298 * the specified address; if necessary, 1299 * it splits the entry into two. 1300 */ 1301 #define vm_map_clip_start(map, entry, startaddr) \ 1302 { \ 1303 if (startaddr > entry->start) \ 1304 _vm_map_clip_start(map, entry, startaddr); \ 1305 } 1306 1307 /* 1308 * This routine is called only when it is known that 1309 * the entry must be split. 1310 */ 1311 static void 1312 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1313 { 1314 vm_map_entry_t new_entry; 1315 1316 /* 1317 * Split off the front portion -- note that we must insert the new 1318 * entry BEFORE this one, so that this entry has the specified 1319 * starting address. 1320 */ 1321 vm_map_simplify_entry(map, entry); 1322 1323 /* 1324 * If there is no object backing this entry, we might as well create 1325 * one now. If we defer it, an object can get created after the map 1326 * is clipped, and individual objects will be created for the split-up 1327 * map. This is a bit of a hack, but is also about the best place to 1328 * put this improvement. 1329 */ 1330 if (entry->object.vm_object == NULL && !map->system_map) { 1331 vm_object_t object; 1332 object = vm_object_allocate(OBJT_DEFAULT, 1333 atop(entry->end - entry->start)); 1334 entry->object.vm_object = object; 1335 entry->offset = 0; 1336 } 1337 1338 new_entry = vm_map_entry_create(map); 1339 *new_entry = *entry; 1340 1341 new_entry->end = start; 1342 entry->offset += (start - entry->start); 1343 entry->start = start; 1344 1345 vm_map_entry_link(map, entry->prev, new_entry); 1346 1347 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1348 vm_object_reference(new_entry->object.vm_object); 1349 } 1350 } 1351 1352 /* 1353 * vm_map_clip_end: [ internal use only ] 1354 * 1355 * Asserts that the given entry ends at or before 1356 * the specified address; if necessary, 1357 * it splits the entry into two. 1358 */ 1359 #define vm_map_clip_end(map, entry, endaddr) \ 1360 { \ 1361 if ((endaddr) < (entry->end)) \ 1362 _vm_map_clip_end((map), (entry), (endaddr)); \ 1363 } 1364 1365 /* 1366 * This routine is called only when it is known that 1367 * the entry must be split. 1368 */ 1369 static void 1370 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1371 { 1372 vm_map_entry_t new_entry; 1373 1374 /* 1375 * If there is no object backing this entry, we might as well create 1376 * one now. If we defer it, an object can get created after the map 1377 * is clipped, and individual objects will be created for the split-up 1378 * map. This is a bit of a hack, but is also about the best place to 1379 * put this improvement. 1380 */ 1381 if (entry->object.vm_object == NULL && !map->system_map) { 1382 vm_object_t object; 1383 object = vm_object_allocate(OBJT_DEFAULT, 1384 atop(entry->end - entry->start)); 1385 entry->object.vm_object = object; 1386 entry->offset = 0; 1387 } 1388 1389 /* 1390 * Create a new entry and insert it AFTER the specified entry 1391 */ 1392 new_entry = vm_map_entry_create(map); 1393 *new_entry = *entry; 1394 1395 new_entry->start = entry->end = end; 1396 new_entry->offset += (end - entry->start); 1397 1398 vm_map_entry_link(map, entry, new_entry); 1399 1400 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1401 vm_object_reference(new_entry->object.vm_object); 1402 } 1403 } 1404 1405 /* 1406 * vm_map_submap: [ kernel use only ] 1407 * 1408 * Mark the given range as handled by a subordinate map. 1409 * 1410 * This range must have been created with vm_map_find, 1411 * and no other operations may have been performed on this 1412 * range prior to calling vm_map_submap. 1413 * 1414 * Only a limited number of operations can be performed 1415 * within this rage after calling vm_map_submap: 1416 * vm_fault 1417 * [Don't try vm_map_copy!] 1418 * 1419 * To remove a submapping, one must first remove the 1420 * range from the superior map, and then destroy the 1421 * submap (if desired). [Better yet, don't try it.] 1422 */ 1423 int 1424 vm_map_submap( 1425 vm_map_t map, 1426 vm_offset_t start, 1427 vm_offset_t end, 1428 vm_map_t submap) 1429 { 1430 vm_map_entry_t entry; 1431 int result = KERN_INVALID_ARGUMENT; 1432 1433 vm_map_lock(map); 1434 1435 VM_MAP_RANGE_CHECK(map, start, end); 1436 1437 if (vm_map_lookup_entry(map, start, &entry)) { 1438 vm_map_clip_start(map, entry, start); 1439 } else 1440 entry = entry->next; 1441 1442 vm_map_clip_end(map, entry, end); 1443 1444 if ((entry->start == start) && (entry->end == end) && 1445 ((entry->eflags & MAP_ENTRY_COW) == 0) && 1446 (entry->object.vm_object == NULL)) { 1447 entry->object.sub_map = submap; 1448 entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 1449 result = KERN_SUCCESS; 1450 } 1451 vm_map_unlock(map); 1452 1453 return (result); 1454 } 1455 1456 /* 1457 * The maximum number of pages to map 1458 */ 1459 #define MAX_INIT_PT 96 1460 1461 /* 1462 * vm_map_pmap_enter: 1463 * 1464 * Preload read-only mappings for the given object's resident pages into 1465 * the given map. This eliminates the soft faults on process startup and 1466 * immediately after an mmap(2). Because these are speculative mappings, 1467 * cached pages are not reactivated and mapped. 1468 */ 1469 void 1470 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 1471 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags) 1472 { 1473 vm_offset_t start; 1474 vm_page_t p, p_start; 1475 vm_pindex_t psize, tmpidx; 1476 boolean_t are_queues_locked; 1477 1478 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL) 1479 return; 1480 VM_OBJECT_LOCK(object); 1481 if (object->type == OBJT_DEVICE) { 1482 pmap_object_init_pt(map->pmap, addr, object, pindex, size); 1483 goto unlock_return; 1484 } 1485 1486 psize = atop(size); 1487 1488 if (object->type != OBJT_VNODE || 1489 ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) && 1490 (object->resident_page_count > MAX_INIT_PT))) { 1491 goto unlock_return; 1492 } 1493 1494 if (psize + pindex > object->size) { 1495 if (object->size < pindex) 1496 goto unlock_return; 1497 psize = object->size - pindex; 1498 } 1499 1500 are_queues_locked = FALSE; 1501 start = 0; 1502 p_start = NULL; 1503 1504 if ((p = TAILQ_FIRST(&object->memq)) != NULL) { 1505 if (p->pindex < pindex) { 1506 p = vm_page_splay(pindex, object->root); 1507 if ((object->root = p)->pindex < pindex) 1508 p = TAILQ_NEXT(p, listq); 1509 } 1510 } 1511 /* 1512 * Assert: the variable p is either (1) the page with the 1513 * least pindex greater than or equal to the parameter pindex 1514 * or (2) NULL. 1515 */ 1516 for (; 1517 p != NULL && (tmpidx = p->pindex - pindex) < psize; 1518 p = TAILQ_NEXT(p, listq)) { 1519 /* 1520 * don't allow an madvise to blow away our really 1521 * free pages allocating pv entries. 1522 */ 1523 if ((flags & MAP_PREFAULT_MADVISE) && 1524 cnt.v_free_count < cnt.v_free_reserved) { 1525 psize = tmpidx; 1526 break; 1527 } 1528 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL && 1529 (p->busy == 0)) { 1530 if (p_start == NULL) { 1531 start = addr + ptoa(tmpidx); 1532 p_start = p; 1533 } 1534 } else if (p_start != NULL) { 1535 if (!are_queues_locked) { 1536 are_queues_locked = TRUE; 1537 vm_page_lock_queues(); 1538 } 1539 pmap_enter_object(map->pmap, start, addr + 1540 ptoa(tmpidx), p_start, prot); 1541 p_start = NULL; 1542 } 1543 } 1544 if (p_start != NULL) { 1545 if (!are_queues_locked) { 1546 are_queues_locked = TRUE; 1547 vm_page_lock_queues(); 1548 } 1549 pmap_enter_object(map->pmap, start, addr + ptoa(psize), 1550 p_start, prot); 1551 } 1552 if (are_queues_locked) 1553 vm_page_unlock_queues(); 1554 unlock_return: 1555 VM_OBJECT_UNLOCK(object); 1556 } 1557 1558 /* 1559 * vm_map_protect: 1560 * 1561 * Sets the protection of the specified address 1562 * region in the target map. If "set_max" is 1563 * specified, the maximum protection is to be set; 1564 * otherwise, only the current protection is affected. 1565 */ 1566 int 1567 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1568 vm_prot_t new_prot, boolean_t set_max) 1569 { 1570 vm_map_entry_t current; 1571 vm_map_entry_t entry; 1572 1573 vm_map_lock(map); 1574 1575 VM_MAP_RANGE_CHECK(map, start, end); 1576 1577 if (vm_map_lookup_entry(map, start, &entry)) { 1578 vm_map_clip_start(map, entry, start); 1579 } else { 1580 entry = entry->next; 1581 } 1582 1583 /* 1584 * Make a first pass to check for protection violations. 1585 */ 1586 current = entry; 1587 while ((current != &map->header) && (current->start < end)) { 1588 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1589 vm_map_unlock(map); 1590 return (KERN_INVALID_ARGUMENT); 1591 } 1592 if ((new_prot & current->max_protection) != new_prot) { 1593 vm_map_unlock(map); 1594 return (KERN_PROTECTION_FAILURE); 1595 } 1596 current = current->next; 1597 } 1598 1599 /* 1600 * Go back and fix up protections. [Note that clipping is not 1601 * necessary the second time.] 1602 */ 1603 current = entry; 1604 while ((current != &map->header) && (current->start < end)) { 1605 vm_prot_t old_prot; 1606 1607 vm_map_clip_end(map, current, end); 1608 1609 old_prot = current->protection; 1610 if (set_max) 1611 current->protection = 1612 (current->max_protection = new_prot) & 1613 old_prot; 1614 else 1615 current->protection = new_prot; 1616 1617 /* 1618 * Update physical map if necessary. Worry about copy-on-write 1619 * here -- CHECK THIS XXX 1620 */ 1621 if (current->protection != old_prot) { 1622 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1623 VM_PROT_ALL) 1624 pmap_protect(map->pmap, current->start, 1625 current->end, 1626 current->protection & MASK(current)); 1627 #undef MASK 1628 } 1629 vm_map_simplify_entry(map, current); 1630 current = current->next; 1631 } 1632 vm_map_unlock(map); 1633 return (KERN_SUCCESS); 1634 } 1635 1636 /* 1637 * vm_map_madvise: 1638 * 1639 * This routine traverses a processes map handling the madvise 1640 * system call. Advisories are classified as either those effecting 1641 * the vm_map_entry structure, or those effecting the underlying 1642 * objects. 1643 */ 1644 int 1645 vm_map_madvise( 1646 vm_map_t map, 1647 vm_offset_t start, 1648 vm_offset_t end, 1649 int behav) 1650 { 1651 vm_map_entry_t current, entry; 1652 int modify_map = 0; 1653 1654 /* 1655 * Some madvise calls directly modify the vm_map_entry, in which case 1656 * we need to use an exclusive lock on the map and we need to perform 1657 * various clipping operations. Otherwise we only need a read-lock 1658 * on the map. 1659 */ 1660 switch(behav) { 1661 case MADV_NORMAL: 1662 case MADV_SEQUENTIAL: 1663 case MADV_RANDOM: 1664 case MADV_NOSYNC: 1665 case MADV_AUTOSYNC: 1666 case MADV_NOCORE: 1667 case MADV_CORE: 1668 modify_map = 1; 1669 vm_map_lock(map); 1670 break; 1671 case MADV_WILLNEED: 1672 case MADV_DONTNEED: 1673 case MADV_FREE: 1674 vm_map_lock_read(map); 1675 break; 1676 default: 1677 return (KERN_INVALID_ARGUMENT); 1678 } 1679 1680 /* 1681 * Locate starting entry and clip if necessary. 1682 */ 1683 VM_MAP_RANGE_CHECK(map, start, end); 1684 1685 if (vm_map_lookup_entry(map, start, &entry)) { 1686 if (modify_map) 1687 vm_map_clip_start(map, entry, start); 1688 } else { 1689 entry = entry->next; 1690 } 1691 1692 if (modify_map) { 1693 /* 1694 * madvise behaviors that are implemented in the vm_map_entry. 1695 * 1696 * We clip the vm_map_entry so that behavioral changes are 1697 * limited to the specified address range. 1698 */ 1699 for (current = entry; 1700 (current != &map->header) && (current->start < end); 1701 current = current->next 1702 ) { 1703 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1704 continue; 1705 1706 vm_map_clip_end(map, current, end); 1707 1708 switch (behav) { 1709 case MADV_NORMAL: 1710 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1711 break; 1712 case MADV_SEQUENTIAL: 1713 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1714 break; 1715 case MADV_RANDOM: 1716 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1717 break; 1718 case MADV_NOSYNC: 1719 current->eflags |= MAP_ENTRY_NOSYNC; 1720 break; 1721 case MADV_AUTOSYNC: 1722 current->eflags &= ~MAP_ENTRY_NOSYNC; 1723 break; 1724 case MADV_NOCORE: 1725 current->eflags |= MAP_ENTRY_NOCOREDUMP; 1726 break; 1727 case MADV_CORE: 1728 current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 1729 break; 1730 default: 1731 break; 1732 } 1733 vm_map_simplify_entry(map, current); 1734 } 1735 vm_map_unlock(map); 1736 } else { 1737 vm_pindex_t pindex; 1738 int count; 1739 1740 /* 1741 * madvise behaviors that are implemented in the underlying 1742 * vm_object. 1743 * 1744 * Since we don't clip the vm_map_entry, we have to clip 1745 * the vm_object pindex and count. 1746 */ 1747 for (current = entry; 1748 (current != &map->header) && (current->start < end); 1749 current = current->next 1750 ) { 1751 vm_offset_t useStart; 1752 1753 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1754 continue; 1755 1756 pindex = OFF_TO_IDX(current->offset); 1757 count = atop(current->end - current->start); 1758 useStart = current->start; 1759 1760 if (current->start < start) { 1761 pindex += atop(start - current->start); 1762 count -= atop(start - current->start); 1763 useStart = start; 1764 } 1765 if (current->end > end) 1766 count -= atop(current->end - end); 1767 1768 if (count <= 0) 1769 continue; 1770 1771 vm_object_madvise(current->object.vm_object, 1772 pindex, count, behav); 1773 if (behav == MADV_WILLNEED) { 1774 vm_map_pmap_enter(map, 1775 useStart, 1776 current->protection, 1777 current->object.vm_object, 1778 pindex, 1779 (count << PAGE_SHIFT), 1780 MAP_PREFAULT_MADVISE 1781 ); 1782 } 1783 } 1784 vm_map_unlock_read(map); 1785 } 1786 return (0); 1787 } 1788 1789 1790 /* 1791 * vm_map_inherit: 1792 * 1793 * Sets the inheritance of the specified address 1794 * range in the target map. Inheritance 1795 * affects how the map will be shared with 1796 * child maps at the time of vm_map_fork. 1797 */ 1798 int 1799 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1800 vm_inherit_t new_inheritance) 1801 { 1802 vm_map_entry_t entry; 1803 vm_map_entry_t temp_entry; 1804 1805 switch (new_inheritance) { 1806 case VM_INHERIT_NONE: 1807 case VM_INHERIT_COPY: 1808 case VM_INHERIT_SHARE: 1809 break; 1810 default: 1811 return (KERN_INVALID_ARGUMENT); 1812 } 1813 vm_map_lock(map); 1814 VM_MAP_RANGE_CHECK(map, start, end); 1815 if (vm_map_lookup_entry(map, start, &temp_entry)) { 1816 entry = temp_entry; 1817 vm_map_clip_start(map, entry, start); 1818 } else 1819 entry = temp_entry->next; 1820 while ((entry != &map->header) && (entry->start < end)) { 1821 vm_map_clip_end(map, entry, end); 1822 entry->inheritance = new_inheritance; 1823 vm_map_simplify_entry(map, entry); 1824 entry = entry->next; 1825 } 1826 vm_map_unlock(map); 1827 return (KERN_SUCCESS); 1828 } 1829 1830 /* 1831 * vm_map_unwire: 1832 * 1833 * Implements both kernel and user unwiring. 1834 */ 1835 int 1836 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1837 int flags) 1838 { 1839 vm_map_entry_t entry, first_entry, tmp_entry; 1840 vm_offset_t saved_start; 1841 unsigned int last_timestamp; 1842 int rv; 1843 boolean_t need_wakeup, result, user_unwire; 1844 1845 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 1846 vm_map_lock(map); 1847 VM_MAP_RANGE_CHECK(map, start, end); 1848 if (!vm_map_lookup_entry(map, start, &first_entry)) { 1849 if (flags & VM_MAP_WIRE_HOLESOK) 1850 first_entry = first_entry->next; 1851 else { 1852 vm_map_unlock(map); 1853 return (KERN_INVALID_ADDRESS); 1854 } 1855 } 1856 last_timestamp = map->timestamp; 1857 entry = first_entry; 1858 while (entry != &map->header && entry->start < end) { 1859 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 1860 /* 1861 * We have not yet clipped the entry. 1862 */ 1863 saved_start = (start >= entry->start) ? start : 1864 entry->start; 1865 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 1866 if (vm_map_unlock_and_wait(map, 0)) { 1867 /* 1868 * Allow interruption of user unwiring? 1869 */ 1870 } 1871 vm_map_lock(map); 1872 if (last_timestamp+1 != map->timestamp) { 1873 /* 1874 * Look again for the entry because the map was 1875 * modified while it was unlocked. 1876 * Specifically, the entry may have been 1877 * clipped, merged, or deleted. 1878 */ 1879 if (!vm_map_lookup_entry(map, saved_start, 1880 &tmp_entry)) { 1881 if (flags & VM_MAP_WIRE_HOLESOK) 1882 tmp_entry = tmp_entry->next; 1883 else { 1884 if (saved_start == start) { 1885 /* 1886 * First_entry has been deleted. 1887 */ 1888 vm_map_unlock(map); 1889 return (KERN_INVALID_ADDRESS); 1890 } 1891 end = saved_start; 1892 rv = KERN_INVALID_ADDRESS; 1893 goto done; 1894 } 1895 } 1896 if (entry == first_entry) 1897 first_entry = tmp_entry; 1898 else 1899 first_entry = NULL; 1900 entry = tmp_entry; 1901 } 1902 last_timestamp = map->timestamp; 1903 continue; 1904 } 1905 vm_map_clip_start(map, entry, start); 1906 vm_map_clip_end(map, entry, end); 1907 /* 1908 * Mark the entry in case the map lock is released. (See 1909 * above.) 1910 */ 1911 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 1912 /* 1913 * Check the map for holes in the specified region. 1914 * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 1915 */ 1916 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 1917 (entry->end < end && (entry->next == &map->header || 1918 entry->next->start > entry->end))) { 1919 end = entry->end; 1920 rv = KERN_INVALID_ADDRESS; 1921 goto done; 1922 } 1923 /* 1924 * If system unwiring, require that the entry is system wired. 1925 */ 1926 if (!user_unwire && 1927 vm_map_entry_system_wired_count(entry) == 0) { 1928 end = entry->end; 1929 rv = KERN_INVALID_ARGUMENT; 1930 goto done; 1931 } 1932 entry = entry->next; 1933 } 1934 rv = KERN_SUCCESS; 1935 done: 1936 need_wakeup = FALSE; 1937 if (first_entry == NULL) { 1938 result = vm_map_lookup_entry(map, start, &first_entry); 1939 if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 1940 first_entry = first_entry->next; 1941 else 1942 KASSERT(result, ("vm_map_unwire: lookup failed")); 1943 } 1944 entry = first_entry; 1945 while (entry != &map->header && entry->start < end) { 1946 if (rv == KERN_SUCCESS && (!user_unwire || 1947 (entry->eflags & MAP_ENTRY_USER_WIRED))) { 1948 if (user_unwire) 1949 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1950 entry->wired_count--; 1951 if (entry->wired_count == 0) { 1952 /* 1953 * Retain the map lock. 1954 */ 1955 vm_fault_unwire(map, entry->start, entry->end, 1956 entry->object.vm_object != NULL && 1957 entry->object.vm_object->type == OBJT_DEVICE); 1958 } 1959 } 1960 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 1961 ("vm_map_unwire: in-transition flag missing")); 1962 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 1963 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 1964 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 1965 need_wakeup = TRUE; 1966 } 1967 vm_map_simplify_entry(map, entry); 1968 entry = entry->next; 1969 } 1970 vm_map_unlock(map); 1971 if (need_wakeup) 1972 vm_map_wakeup(map); 1973 return (rv); 1974 } 1975 1976 /* 1977 * vm_map_wire: 1978 * 1979 * Implements both kernel and user wiring. 1980 */ 1981 int 1982 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1983 int flags) 1984 { 1985 vm_map_entry_t entry, first_entry, tmp_entry; 1986 vm_offset_t saved_end, saved_start; 1987 unsigned int last_timestamp; 1988 int rv; 1989 boolean_t fictitious, need_wakeup, result, user_wire; 1990 1991 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 1992 vm_map_lock(map); 1993 VM_MAP_RANGE_CHECK(map, start, end); 1994 if (!vm_map_lookup_entry(map, start, &first_entry)) { 1995 if (flags & VM_MAP_WIRE_HOLESOK) 1996 first_entry = first_entry->next; 1997 else { 1998 vm_map_unlock(map); 1999 return (KERN_INVALID_ADDRESS); 2000 } 2001 } 2002 last_timestamp = map->timestamp; 2003 entry = first_entry; 2004 while (entry != &map->header && entry->start < end) { 2005 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 2006 /* 2007 * We have not yet clipped the entry. 2008 */ 2009 saved_start = (start >= entry->start) ? start : 2010 entry->start; 2011 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2012 if (vm_map_unlock_and_wait(map, 0)) { 2013 /* 2014 * Allow interruption of user wiring? 2015 */ 2016 } 2017 vm_map_lock(map); 2018 if (last_timestamp + 1 != map->timestamp) { 2019 /* 2020 * Look again for the entry because the map was 2021 * modified while it was unlocked. 2022 * Specifically, the entry may have been 2023 * clipped, merged, or deleted. 2024 */ 2025 if (!vm_map_lookup_entry(map, saved_start, 2026 &tmp_entry)) { 2027 if (flags & VM_MAP_WIRE_HOLESOK) 2028 tmp_entry = tmp_entry->next; 2029 else { 2030 if (saved_start == start) { 2031 /* 2032 * first_entry has been deleted. 2033 */ 2034 vm_map_unlock(map); 2035 return (KERN_INVALID_ADDRESS); 2036 } 2037 end = saved_start; 2038 rv = KERN_INVALID_ADDRESS; 2039 goto done; 2040 } 2041 } 2042 if (entry == first_entry) 2043 first_entry = tmp_entry; 2044 else 2045 first_entry = NULL; 2046 entry = tmp_entry; 2047 } 2048 last_timestamp = map->timestamp; 2049 continue; 2050 } 2051 vm_map_clip_start(map, entry, start); 2052 vm_map_clip_end(map, entry, end); 2053 /* 2054 * Mark the entry in case the map lock is released. (See 2055 * above.) 2056 */ 2057 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 2058 /* 2059 * 2060 */ 2061 if (entry->wired_count == 0) { 2062 entry->wired_count++; 2063 saved_start = entry->start; 2064 saved_end = entry->end; 2065 fictitious = entry->object.vm_object != NULL && 2066 entry->object.vm_object->type == OBJT_DEVICE; 2067 /* 2068 * Release the map lock, relying on the in-transition 2069 * mark. 2070 */ 2071 vm_map_unlock(map); 2072 rv = vm_fault_wire(map, saved_start, saved_end, 2073 user_wire, fictitious); 2074 vm_map_lock(map); 2075 if (last_timestamp + 1 != map->timestamp) { 2076 /* 2077 * Look again for the entry because the map was 2078 * modified while it was unlocked. The entry 2079 * may have been clipped, but NOT merged or 2080 * deleted. 2081 */ 2082 result = vm_map_lookup_entry(map, saved_start, 2083 &tmp_entry); 2084 KASSERT(result, ("vm_map_wire: lookup failed")); 2085 if (entry == first_entry) 2086 first_entry = tmp_entry; 2087 else 2088 first_entry = NULL; 2089 entry = tmp_entry; 2090 while (entry->end < saved_end) { 2091 if (rv != KERN_SUCCESS) { 2092 KASSERT(entry->wired_count == 1, 2093 ("vm_map_wire: bad count")); 2094 entry->wired_count = -1; 2095 } 2096 entry = entry->next; 2097 } 2098 } 2099 last_timestamp = map->timestamp; 2100 if (rv != KERN_SUCCESS) { 2101 KASSERT(entry->wired_count == 1, 2102 ("vm_map_wire: bad count")); 2103 /* 2104 * Assign an out-of-range value to represent 2105 * the failure to wire this entry. 2106 */ 2107 entry->wired_count = -1; 2108 end = entry->end; 2109 goto done; 2110 } 2111 } else if (!user_wire || 2112 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 2113 entry->wired_count++; 2114 } 2115 /* 2116 * Check the map for holes in the specified region. 2117 * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 2118 */ 2119 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2120 (entry->end < end && (entry->next == &map->header || 2121 entry->next->start > entry->end))) { 2122 end = entry->end; 2123 rv = KERN_INVALID_ADDRESS; 2124 goto done; 2125 } 2126 entry = entry->next; 2127 } 2128 rv = KERN_SUCCESS; 2129 done: 2130 need_wakeup = FALSE; 2131 if (first_entry == NULL) { 2132 result = vm_map_lookup_entry(map, start, &first_entry); 2133 if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2134 first_entry = first_entry->next; 2135 else 2136 KASSERT(result, ("vm_map_wire: lookup failed")); 2137 } 2138 entry = first_entry; 2139 while (entry != &map->header && entry->start < end) { 2140 if (rv == KERN_SUCCESS) { 2141 if (user_wire) 2142 entry->eflags |= MAP_ENTRY_USER_WIRED; 2143 } else if (entry->wired_count == -1) { 2144 /* 2145 * Wiring failed on this entry. Thus, unwiring is 2146 * unnecessary. 2147 */ 2148 entry->wired_count = 0; 2149 } else { 2150 if (!user_wire || 2151 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) 2152 entry->wired_count--; 2153 if (entry->wired_count == 0) { 2154 /* 2155 * Retain the map lock. 2156 */ 2157 vm_fault_unwire(map, entry->start, entry->end, 2158 entry->object.vm_object != NULL && 2159 entry->object.vm_object->type == OBJT_DEVICE); 2160 } 2161 } 2162 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 2163 ("vm_map_wire: in-transition flag missing")); 2164 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 2165 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 2166 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 2167 need_wakeup = TRUE; 2168 } 2169 vm_map_simplify_entry(map, entry); 2170 entry = entry->next; 2171 } 2172 vm_map_unlock(map); 2173 if (need_wakeup) 2174 vm_map_wakeup(map); 2175 return (rv); 2176 } 2177 2178 /* 2179 * vm_map_sync 2180 * 2181 * Push any dirty cached pages in the address range to their pager. 2182 * If syncio is TRUE, dirty pages are written synchronously. 2183 * If invalidate is TRUE, any cached pages are freed as well. 2184 * 2185 * If the size of the region from start to end is zero, we are 2186 * supposed to flush all modified pages within the region containing 2187 * start. Unfortunately, a region can be split or coalesced with 2188 * neighboring regions, making it difficult to determine what the 2189 * original region was. Therefore, we approximate this requirement by 2190 * flushing the current region containing start. 2191 * 2192 * Returns an error if any part of the specified range is not mapped. 2193 */ 2194 int 2195 vm_map_sync( 2196 vm_map_t map, 2197 vm_offset_t start, 2198 vm_offset_t end, 2199 boolean_t syncio, 2200 boolean_t invalidate) 2201 { 2202 vm_map_entry_t current; 2203 vm_map_entry_t entry; 2204 vm_size_t size; 2205 vm_object_t object; 2206 vm_ooffset_t offset; 2207 2208 vm_map_lock_read(map); 2209 VM_MAP_RANGE_CHECK(map, start, end); 2210 if (!vm_map_lookup_entry(map, start, &entry)) { 2211 vm_map_unlock_read(map); 2212 return (KERN_INVALID_ADDRESS); 2213 } else if (start == end) { 2214 start = entry->start; 2215 end = entry->end; 2216 } 2217 /* 2218 * Make a first pass to check for user-wired memory and holes. 2219 */ 2220 for (current = entry; current != &map->header && current->start < end; 2221 current = current->next) { 2222 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) { 2223 vm_map_unlock_read(map); 2224 return (KERN_INVALID_ARGUMENT); 2225 } 2226 if (end > current->end && 2227 (current->next == &map->header || 2228 current->end != current->next->start)) { 2229 vm_map_unlock_read(map); 2230 return (KERN_INVALID_ADDRESS); 2231 } 2232 } 2233 2234 if (invalidate) 2235 pmap_remove(map->pmap, start, end); 2236 2237 /* 2238 * Make a second pass, cleaning/uncaching pages from the indicated 2239 * objects as we go. 2240 */ 2241 for (current = entry; current != &map->header && current->start < end; 2242 current = current->next) { 2243 offset = current->offset + (start - current->start); 2244 size = (end <= current->end ? end : current->end) - start; 2245 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2246 vm_map_t smap; 2247 vm_map_entry_t tentry; 2248 vm_size_t tsize; 2249 2250 smap = current->object.sub_map; 2251 vm_map_lock_read(smap); 2252 (void) vm_map_lookup_entry(smap, offset, &tentry); 2253 tsize = tentry->end - offset; 2254 if (tsize < size) 2255 size = tsize; 2256 object = tentry->object.vm_object; 2257 offset = tentry->offset + (offset - tentry->start); 2258 vm_map_unlock_read(smap); 2259 } else { 2260 object = current->object.vm_object; 2261 } 2262 vm_object_sync(object, offset, size, syncio, invalidate); 2263 start += size; 2264 } 2265 2266 vm_map_unlock_read(map); 2267 return (KERN_SUCCESS); 2268 } 2269 2270 /* 2271 * vm_map_entry_unwire: [ internal use only ] 2272 * 2273 * Make the region specified by this entry pageable. 2274 * 2275 * The map in question should be locked. 2276 * [This is the reason for this routine's existence.] 2277 */ 2278 static void 2279 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2280 { 2281 vm_fault_unwire(map, entry->start, entry->end, 2282 entry->object.vm_object != NULL && 2283 entry->object.vm_object->type == OBJT_DEVICE); 2284 entry->wired_count = 0; 2285 } 2286 2287 /* 2288 * vm_map_entry_delete: [ internal use only ] 2289 * 2290 * Deallocate the given entry from the target map. 2291 */ 2292 static void 2293 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2294 { 2295 vm_object_t object; 2296 vm_pindex_t offidxstart, offidxend, count; 2297 2298 vm_map_entry_unlink(map, entry); 2299 map->size -= entry->end - entry->start; 2300 2301 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 2302 (object = entry->object.vm_object) != NULL) { 2303 count = OFF_TO_IDX(entry->end - entry->start); 2304 offidxstart = OFF_TO_IDX(entry->offset); 2305 offidxend = offidxstart + count; 2306 VM_OBJECT_LOCK(object); 2307 if (object->ref_count != 1 && 2308 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING || 2309 object == kernel_object || object == kmem_object)) { 2310 vm_object_collapse(object); 2311 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 2312 if (object->type == OBJT_SWAP) 2313 swap_pager_freespace(object, offidxstart, count); 2314 if (offidxend >= object->size && 2315 offidxstart < object->size) 2316 object->size = offidxstart; 2317 } 2318 VM_OBJECT_UNLOCK(object); 2319 vm_object_deallocate(object); 2320 } 2321 2322 vm_map_entry_dispose(map, entry); 2323 } 2324 2325 /* 2326 * vm_map_delete: [ internal use only ] 2327 * 2328 * Deallocates the given address range from the target 2329 * map. 2330 */ 2331 int 2332 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 2333 { 2334 vm_map_entry_t entry; 2335 vm_map_entry_t first_entry; 2336 2337 /* 2338 * Find the start of the region, and clip it 2339 */ 2340 if (!vm_map_lookup_entry(map, start, &first_entry)) 2341 entry = first_entry->next; 2342 else { 2343 entry = first_entry; 2344 vm_map_clip_start(map, entry, start); 2345 } 2346 2347 /* 2348 * Step through all entries in this region 2349 */ 2350 while ((entry != &map->header) && (entry->start < end)) { 2351 vm_map_entry_t next; 2352 2353 /* 2354 * Wait for wiring or unwiring of an entry to complete. 2355 * Also wait for any system wirings to disappear on 2356 * user maps. 2357 */ 2358 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 || 2359 (vm_map_pmap(map) != kernel_pmap && 2360 vm_map_entry_system_wired_count(entry) != 0)) { 2361 unsigned int last_timestamp; 2362 vm_offset_t saved_start; 2363 vm_map_entry_t tmp_entry; 2364 2365 saved_start = entry->start; 2366 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2367 last_timestamp = map->timestamp; 2368 (void) vm_map_unlock_and_wait(map, 0); 2369 vm_map_lock(map); 2370 if (last_timestamp + 1 != map->timestamp) { 2371 /* 2372 * Look again for the entry because the map was 2373 * modified while it was unlocked. 2374 * Specifically, the entry may have been 2375 * clipped, merged, or deleted. 2376 */ 2377 if (!vm_map_lookup_entry(map, saved_start, 2378 &tmp_entry)) 2379 entry = tmp_entry->next; 2380 else { 2381 entry = tmp_entry; 2382 vm_map_clip_start(map, entry, 2383 saved_start); 2384 } 2385 } 2386 continue; 2387 } 2388 vm_map_clip_end(map, entry, end); 2389 2390 next = entry->next; 2391 2392 /* 2393 * Unwire before removing addresses from the pmap; otherwise, 2394 * unwiring will put the entries back in the pmap. 2395 */ 2396 if (entry->wired_count != 0) { 2397 vm_map_entry_unwire(map, entry); 2398 } 2399 2400 pmap_remove(map->pmap, entry->start, entry->end); 2401 2402 /* 2403 * Delete the entry (which may delete the object) only after 2404 * removing all pmap entries pointing to its pages. 2405 * (Otherwise, its page frames may be reallocated, and any 2406 * modify bits will be set in the wrong object!) 2407 */ 2408 vm_map_entry_delete(map, entry); 2409 entry = next; 2410 } 2411 return (KERN_SUCCESS); 2412 } 2413 2414 /* 2415 * vm_map_remove: 2416 * 2417 * Remove the given address range from the target map. 2418 * This is the exported form of vm_map_delete. 2419 */ 2420 int 2421 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 2422 { 2423 int result; 2424 2425 vm_map_lock(map); 2426 VM_MAP_RANGE_CHECK(map, start, end); 2427 result = vm_map_delete(map, start, end); 2428 vm_map_unlock(map); 2429 return (result); 2430 } 2431 2432 /* 2433 * vm_map_check_protection: 2434 * 2435 * Assert that the target map allows the specified privilege on the 2436 * entire address region given. The entire region must be allocated. 2437 * 2438 * WARNING! This code does not and should not check whether the 2439 * contents of the region is accessible. For example a smaller file 2440 * might be mapped into a larger address space. 2441 * 2442 * NOTE! This code is also called by munmap(). 2443 * 2444 * The map must be locked. A read lock is sufficient. 2445 */ 2446 boolean_t 2447 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 2448 vm_prot_t protection) 2449 { 2450 vm_map_entry_t entry; 2451 vm_map_entry_t tmp_entry; 2452 2453 if (!vm_map_lookup_entry(map, start, &tmp_entry)) 2454 return (FALSE); 2455 entry = tmp_entry; 2456 2457 while (start < end) { 2458 if (entry == &map->header) 2459 return (FALSE); 2460 /* 2461 * No holes allowed! 2462 */ 2463 if (start < entry->start) 2464 return (FALSE); 2465 /* 2466 * Check protection associated with entry. 2467 */ 2468 if ((entry->protection & protection) != protection) 2469 return (FALSE); 2470 /* go to next entry */ 2471 start = entry->end; 2472 entry = entry->next; 2473 } 2474 return (TRUE); 2475 } 2476 2477 /* 2478 * vm_map_copy_entry: 2479 * 2480 * Copies the contents of the source entry to the destination 2481 * entry. The entries *must* be aligned properly. 2482 */ 2483 static void 2484 vm_map_copy_entry( 2485 vm_map_t src_map, 2486 vm_map_t dst_map, 2487 vm_map_entry_t src_entry, 2488 vm_map_entry_t dst_entry) 2489 { 2490 vm_object_t src_object; 2491 2492 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2493 return; 2494 2495 if (src_entry->wired_count == 0) { 2496 2497 /* 2498 * If the source entry is marked needs_copy, it is already 2499 * write-protected. 2500 */ 2501 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2502 pmap_protect(src_map->pmap, 2503 src_entry->start, 2504 src_entry->end, 2505 src_entry->protection & ~VM_PROT_WRITE); 2506 } 2507 2508 /* 2509 * Make a copy of the object. 2510 */ 2511 if ((src_object = src_entry->object.vm_object) != NULL) { 2512 VM_OBJECT_LOCK(src_object); 2513 if ((src_object->handle == NULL) && 2514 (src_object->type == OBJT_DEFAULT || 2515 src_object->type == OBJT_SWAP)) { 2516 vm_object_collapse(src_object); 2517 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2518 vm_object_split(src_entry); 2519 src_object = src_entry->object.vm_object; 2520 } 2521 } 2522 vm_object_reference_locked(src_object); 2523 vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2524 VM_OBJECT_UNLOCK(src_object); 2525 dst_entry->object.vm_object = src_object; 2526 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2527 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2528 dst_entry->offset = src_entry->offset; 2529 } else { 2530 dst_entry->object.vm_object = NULL; 2531 dst_entry->offset = 0; 2532 } 2533 2534 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2535 dst_entry->end - dst_entry->start, src_entry->start); 2536 } else { 2537 /* 2538 * Of course, wired down pages can't be set copy-on-write. 2539 * Cause wired pages to be copied into the new map by 2540 * simulating faults (the new pages are pageable) 2541 */ 2542 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2543 } 2544 } 2545 2546 /* 2547 * vmspace_map_entry_forked: 2548 * Update the newly-forked vmspace each time a map entry is inherited 2549 * or copied. The values for vm_dsize and vm_tsize are approximate 2550 * (and mostly-obsolete ideas in the face of mmap(2) et al.) 2551 */ 2552 static void 2553 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2, 2554 vm_map_entry_t entry) 2555 { 2556 vm_size_t entrysize; 2557 vm_offset_t newend; 2558 2559 entrysize = entry->end - entry->start; 2560 vm2->vm_map.size += entrysize; 2561 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) { 2562 vm2->vm_ssize += btoc(entrysize); 2563 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr && 2564 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) { 2565 newend = MIN(entry->end, 2566 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)); 2567 vm2->vm_dsize += btoc(newend - entry->start); 2568 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr && 2569 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) { 2570 newend = MIN(entry->end, 2571 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)); 2572 vm2->vm_tsize += btoc(newend - entry->start); 2573 } 2574 } 2575 2576 /* 2577 * vmspace_fork: 2578 * Create a new process vmspace structure and vm_map 2579 * based on those of an existing process. The new map 2580 * is based on the old map, according to the inheritance 2581 * values on the regions in that map. 2582 * 2583 * XXX It might be worth coalescing the entries added to the new vmspace. 2584 * 2585 * The source map must not be locked. 2586 */ 2587 struct vmspace * 2588 vmspace_fork(struct vmspace *vm1) 2589 { 2590 struct vmspace *vm2; 2591 vm_map_t old_map = &vm1->vm_map; 2592 vm_map_t new_map; 2593 vm_map_entry_t old_entry; 2594 vm_map_entry_t new_entry; 2595 vm_object_t object; 2596 2597 vm_map_lock(old_map); 2598 2599 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2600 if (vm2 == NULL) 2601 goto unlock_and_return; 2602 vm2->vm_taddr = vm1->vm_taddr; 2603 vm2->vm_daddr = vm1->vm_daddr; 2604 vm2->vm_maxsaddr = vm1->vm_maxsaddr; 2605 new_map = &vm2->vm_map; /* XXX */ 2606 new_map->timestamp = 1; 2607 2608 old_entry = old_map->header.next; 2609 2610 while (old_entry != &old_map->header) { 2611 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2612 panic("vm_map_fork: encountered a submap"); 2613 2614 switch (old_entry->inheritance) { 2615 case VM_INHERIT_NONE: 2616 break; 2617 2618 case VM_INHERIT_SHARE: 2619 /* 2620 * Clone the entry, creating the shared object if necessary. 2621 */ 2622 object = old_entry->object.vm_object; 2623 if (object == NULL) { 2624 object = vm_object_allocate(OBJT_DEFAULT, 2625 atop(old_entry->end - old_entry->start)); 2626 old_entry->object.vm_object = object; 2627 old_entry->offset = 0; 2628 } 2629 2630 /* 2631 * Add the reference before calling vm_object_shadow 2632 * to insure that a shadow object is created. 2633 */ 2634 vm_object_reference(object); 2635 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2636 vm_object_shadow(&old_entry->object.vm_object, 2637 &old_entry->offset, 2638 atop(old_entry->end - old_entry->start)); 2639 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2640 /* Transfer the second reference too. */ 2641 vm_object_reference( 2642 old_entry->object.vm_object); 2643 vm_object_deallocate(object); 2644 object = old_entry->object.vm_object; 2645 } 2646 VM_OBJECT_LOCK(object); 2647 vm_object_clear_flag(object, OBJ_ONEMAPPING); 2648 VM_OBJECT_UNLOCK(object); 2649 2650 /* 2651 * Clone the entry, referencing the shared object. 2652 */ 2653 new_entry = vm_map_entry_create(new_map); 2654 *new_entry = *old_entry; 2655 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2656 new_entry->wired_count = 0; 2657 2658 /* 2659 * Insert the entry into the new map -- we know we're 2660 * inserting at the end of the new map. 2661 */ 2662 vm_map_entry_link(new_map, new_map->header.prev, 2663 new_entry); 2664 vmspace_map_entry_forked(vm1, vm2, new_entry); 2665 2666 /* 2667 * Update the physical map 2668 */ 2669 pmap_copy(new_map->pmap, old_map->pmap, 2670 new_entry->start, 2671 (old_entry->end - old_entry->start), 2672 old_entry->start); 2673 break; 2674 2675 case VM_INHERIT_COPY: 2676 /* 2677 * Clone the entry and link into the map. 2678 */ 2679 new_entry = vm_map_entry_create(new_map); 2680 *new_entry = *old_entry; 2681 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2682 new_entry->wired_count = 0; 2683 new_entry->object.vm_object = NULL; 2684 vm_map_entry_link(new_map, new_map->header.prev, 2685 new_entry); 2686 vmspace_map_entry_forked(vm1, vm2, new_entry); 2687 vm_map_copy_entry(old_map, new_map, old_entry, 2688 new_entry); 2689 break; 2690 } 2691 old_entry = old_entry->next; 2692 } 2693 unlock_and_return: 2694 vm_map_unlock(old_map); 2695 2696 return (vm2); 2697 } 2698 2699 int 2700 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 2701 vm_prot_t prot, vm_prot_t max, int cow) 2702 { 2703 vm_map_entry_t new_entry, prev_entry; 2704 vm_offset_t bot, top; 2705 vm_size_t init_ssize; 2706 int orient, rv; 2707 rlim_t vmemlim; 2708 2709 /* 2710 * The stack orientation is piggybacked with the cow argument. 2711 * Extract it into orient and mask the cow argument so that we 2712 * don't pass it around further. 2713 * NOTE: We explicitly allow bi-directional stacks. 2714 */ 2715 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP); 2716 cow &= ~orient; 2717 KASSERT(orient != 0, ("No stack grow direction")); 2718 2719 if (addrbos < vm_map_min(map) || 2720 addrbos > vm_map_max(map) || 2721 addrbos + max_ssize < addrbos) 2722 return (KERN_NO_SPACE); 2723 2724 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz; 2725 2726 PROC_LOCK(curthread->td_proc); 2727 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM); 2728 PROC_UNLOCK(curthread->td_proc); 2729 2730 vm_map_lock(map); 2731 2732 /* If addr is already mapped, no go */ 2733 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 2734 vm_map_unlock(map); 2735 return (KERN_NO_SPACE); 2736 } 2737 2738 /* If we would blow our VMEM resource limit, no go */ 2739 if (map->size + init_ssize > vmemlim) { 2740 vm_map_unlock(map); 2741 return (KERN_NO_SPACE); 2742 } 2743 2744 /* 2745 * If we can't accomodate max_ssize in the current mapping, no go. 2746 * However, we need to be aware that subsequent user mappings might 2747 * map into the space we have reserved for stack, and currently this 2748 * space is not protected. 2749 * 2750 * Hopefully we will at least detect this condition when we try to 2751 * grow the stack. 2752 */ 2753 if ((prev_entry->next != &map->header) && 2754 (prev_entry->next->start < addrbos + max_ssize)) { 2755 vm_map_unlock(map); 2756 return (KERN_NO_SPACE); 2757 } 2758 2759 /* 2760 * We initially map a stack of only init_ssize. We will grow as 2761 * needed later. Depending on the orientation of the stack (i.e. 2762 * the grow direction) we either map at the top of the range, the 2763 * bottom of the range or in the middle. 2764 * 2765 * Note: we would normally expect prot and max to be VM_PROT_ALL, 2766 * and cow to be 0. Possibly we should eliminate these as input 2767 * parameters, and just pass these values here in the insert call. 2768 */ 2769 if (orient == MAP_STACK_GROWS_DOWN) 2770 bot = addrbos + max_ssize - init_ssize; 2771 else if (orient == MAP_STACK_GROWS_UP) 2772 bot = addrbos; 2773 else 2774 bot = round_page(addrbos + max_ssize/2 - init_ssize/2); 2775 top = bot + init_ssize; 2776 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow); 2777 2778 /* Now set the avail_ssize amount. */ 2779 if (rv == KERN_SUCCESS) { 2780 if (prev_entry != &map->header) 2781 vm_map_clip_end(map, prev_entry, bot); 2782 new_entry = prev_entry->next; 2783 if (new_entry->end != top || new_entry->start != bot) 2784 panic("Bad entry start/end for new stack entry"); 2785 2786 new_entry->avail_ssize = max_ssize - init_ssize; 2787 if (orient & MAP_STACK_GROWS_DOWN) 2788 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 2789 if (orient & MAP_STACK_GROWS_UP) 2790 new_entry->eflags |= MAP_ENTRY_GROWS_UP; 2791 } 2792 2793 vm_map_unlock(map); 2794 return (rv); 2795 } 2796 2797 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 2798 * desired address is already mapped, or if we successfully grow 2799 * the stack. Also returns KERN_SUCCESS if addr is outside the 2800 * stack range (this is strange, but preserves compatibility with 2801 * the grow function in vm_machdep.c). 2802 */ 2803 int 2804 vm_map_growstack(struct proc *p, vm_offset_t addr) 2805 { 2806 vm_map_entry_t next_entry, prev_entry; 2807 vm_map_entry_t new_entry, stack_entry; 2808 struct vmspace *vm = p->p_vmspace; 2809 vm_map_t map = &vm->vm_map; 2810 vm_offset_t end; 2811 size_t grow_amount, max_grow; 2812 rlim_t stacklim, vmemlim; 2813 int is_procstack, rv; 2814 2815 Retry: 2816 PROC_LOCK(p); 2817 stacklim = lim_cur(p, RLIMIT_STACK); 2818 vmemlim = lim_cur(p, RLIMIT_VMEM); 2819 PROC_UNLOCK(p); 2820 2821 vm_map_lock_read(map); 2822 2823 /* If addr is already in the entry range, no need to grow.*/ 2824 if (vm_map_lookup_entry(map, addr, &prev_entry)) { 2825 vm_map_unlock_read(map); 2826 return (KERN_SUCCESS); 2827 } 2828 2829 next_entry = prev_entry->next; 2830 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) { 2831 /* 2832 * This entry does not grow upwards. Since the address lies 2833 * beyond this entry, the next entry (if one exists) has to 2834 * be a downward growable entry. The entry list header is 2835 * never a growable entry, so it suffices to check the flags. 2836 */ 2837 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) { 2838 vm_map_unlock_read(map); 2839 return (KERN_SUCCESS); 2840 } 2841 stack_entry = next_entry; 2842 } else { 2843 /* 2844 * This entry grows upward. If the next entry does not at 2845 * least grow downwards, this is the entry we need to grow. 2846 * otherwise we have two possible choices and we have to 2847 * select one. 2848 */ 2849 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) { 2850 /* 2851 * We have two choices; grow the entry closest to 2852 * the address to minimize the amount of growth. 2853 */ 2854 if (addr - prev_entry->end <= next_entry->start - addr) 2855 stack_entry = prev_entry; 2856 else 2857 stack_entry = next_entry; 2858 } else 2859 stack_entry = prev_entry; 2860 } 2861 2862 if (stack_entry == next_entry) { 2863 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo")); 2864 KASSERT(addr < stack_entry->start, ("foo")); 2865 end = (prev_entry != &map->header) ? prev_entry->end : 2866 stack_entry->start - stack_entry->avail_ssize; 2867 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE); 2868 max_grow = stack_entry->start - end; 2869 } else { 2870 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo")); 2871 KASSERT(addr >= stack_entry->end, ("foo")); 2872 end = (next_entry != &map->header) ? next_entry->start : 2873 stack_entry->end + stack_entry->avail_ssize; 2874 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE); 2875 max_grow = end - stack_entry->end; 2876 } 2877 2878 if (grow_amount > stack_entry->avail_ssize) { 2879 vm_map_unlock_read(map); 2880 return (KERN_NO_SPACE); 2881 } 2882 2883 /* 2884 * If there is no longer enough space between the entries nogo, and 2885 * adjust the available space. Note: this should only happen if the 2886 * user has mapped into the stack area after the stack was created, 2887 * and is probably an error. 2888 * 2889 * This also effectively destroys any guard page the user might have 2890 * intended by limiting the stack size. 2891 */ 2892 if (grow_amount > max_grow) { 2893 if (vm_map_lock_upgrade(map)) 2894 goto Retry; 2895 2896 stack_entry->avail_ssize = max_grow; 2897 2898 vm_map_unlock(map); 2899 return (KERN_NO_SPACE); 2900 } 2901 2902 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0; 2903 2904 /* 2905 * If this is the main process stack, see if we're over the stack 2906 * limit. 2907 */ 2908 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 2909 vm_map_unlock_read(map); 2910 return (KERN_NO_SPACE); 2911 } 2912 2913 /* Round up the grow amount modulo SGROWSIZ */ 2914 grow_amount = roundup (grow_amount, sgrowsiz); 2915 if (grow_amount > stack_entry->avail_ssize) 2916 grow_amount = stack_entry->avail_ssize; 2917 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 2918 grow_amount = stacklim - ctob(vm->vm_ssize); 2919 } 2920 2921 /* If we would blow our VMEM resource limit, no go */ 2922 if (map->size + grow_amount > vmemlim) { 2923 vm_map_unlock_read(map); 2924 return (KERN_NO_SPACE); 2925 } 2926 2927 if (vm_map_lock_upgrade(map)) 2928 goto Retry; 2929 2930 if (stack_entry == next_entry) { 2931 /* 2932 * Growing downward. 2933 */ 2934 /* Get the preliminary new entry start value */ 2935 addr = stack_entry->start - grow_amount; 2936 2937 /* 2938 * If this puts us into the previous entry, cut back our 2939 * growth to the available space. Also, see the note above. 2940 */ 2941 if (addr < end) { 2942 stack_entry->avail_ssize = max_grow; 2943 addr = end; 2944 } 2945 2946 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 2947 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0); 2948 2949 /* Adjust the available stack space by the amount we grew. */ 2950 if (rv == KERN_SUCCESS) { 2951 if (prev_entry != &map->header) 2952 vm_map_clip_end(map, prev_entry, addr); 2953 new_entry = prev_entry->next; 2954 KASSERT(new_entry == stack_entry->prev, ("foo")); 2955 KASSERT(new_entry->end == stack_entry->start, ("foo")); 2956 KASSERT(new_entry->start == addr, ("foo")); 2957 grow_amount = new_entry->end - new_entry->start; 2958 new_entry->avail_ssize = stack_entry->avail_ssize - 2959 grow_amount; 2960 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN; 2961 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 2962 } 2963 } else { 2964 /* 2965 * Growing upward. 2966 */ 2967 addr = stack_entry->end + grow_amount; 2968 2969 /* 2970 * If this puts us into the next entry, cut back our growth 2971 * to the available space. Also, see the note above. 2972 */ 2973 if (addr > end) { 2974 stack_entry->avail_ssize = end - stack_entry->end; 2975 addr = end; 2976 } 2977 2978 grow_amount = addr - stack_entry->end; 2979 2980 /* Grow the underlying object if applicable. */ 2981 if (stack_entry->object.vm_object == NULL || 2982 vm_object_coalesce(stack_entry->object.vm_object, 2983 stack_entry->offset, 2984 (vm_size_t)(stack_entry->end - stack_entry->start), 2985 (vm_size_t)grow_amount)) { 2986 map->size += (addr - stack_entry->end); 2987 /* Update the current entry. */ 2988 stack_entry->end = addr; 2989 stack_entry->avail_ssize -= grow_amount; 2990 vm_map_entry_resize_free(map, stack_entry); 2991 rv = KERN_SUCCESS; 2992 2993 if (next_entry != &map->header) 2994 vm_map_clip_start(map, next_entry, addr); 2995 } else 2996 rv = KERN_FAILURE; 2997 } 2998 2999 if (rv == KERN_SUCCESS && is_procstack) 3000 vm->vm_ssize += btoc(grow_amount); 3001 3002 vm_map_unlock(map); 3003 3004 /* 3005 * Heed the MAP_WIREFUTURE flag if it was set for this process. 3006 */ 3007 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) { 3008 vm_map_wire(map, 3009 (stack_entry == next_entry) ? addr : addr - grow_amount, 3010 (stack_entry == next_entry) ? stack_entry->start : addr, 3011 (p->p_flag & P_SYSTEM) 3012 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES 3013 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 3014 } 3015 3016 return (rv); 3017 } 3018 3019 /* 3020 * Unshare the specified VM space for exec. If other processes are 3021 * mapped to it, then create a new one. The new vmspace is null. 3022 */ 3023 int 3024 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser) 3025 { 3026 struct vmspace *oldvmspace = p->p_vmspace; 3027 struct vmspace *newvmspace; 3028 3029 newvmspace = vmspace_alloc(minuser, maxuser); 3030 if (newvmspace == NULL) 3031 return (ENOMEM); 3032 newvmspace->vm_swrss = oldvmspace->vm_swrss; 3033 /* 3034 * This code is written like this for prototype purposes. The 3035 * goal is to avoid running down the vmspace here, but let the 3036 * other process's that are still using the vmspace to finally 3037 * run it down. Even though there is little or no chance of blocking 3038 * here, it is a good idea to keep this form for future mods. 3039 */ 3040 PROC_VMSPACE_LOCK(p); 3041 p->p_vmspace = newvmspace; 3042 PROC_VMSPACE_UNLOCK(p); 3043 if (p == curthread->td_proc) 3044 pmap_activate(curthread); 3045 vmspace_free(oldvmspace); 3046 return (0); 3047 } 3048 3049 /* 3050 * Unshare the specified VM space for forcing COW. This 3051 * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 3052 */ 3053 int 3054 vmspace_unshare(struct proc *p) 3055 { 3056 struct vmspace *oldvmspace = p->p_vmspace; 3057 struct vmspace *newvmspace; 3058 3059 if (oldvmspace->vm_refcnt == 1) 3060 return (0); 3061 newvmspace = vmspace_fork(oldvmspace); 3062 if (newvmspace == NULL) 3063 return (ENOMEM); 3064 PROC_VMSPACE_LOCK(p); 3065 p->p_vmspace = newvmspace; 3066 PROC_VMSPACE_UNLOCK(p); 3067 if (p == curthread->td_proc) 3068 pmap_activate(curthread); 3069 vmspace_free(oldvmspace); 3070 return (0); 3071 } 3072 3073 /* 3074 * vm_map_lookup: 3075 * 3076 * Finds the VM object, offset, and 3077 * protection for a given virtual address in the 3078 * specified map, assuming a page fault of the 3079 * type specified. 3080 * 3081 * Leaves the map in question locked for read; return 3082 * values are guaranteed until a vm_map_lookup_done 3083 * call is performed. Note that the map argument 3084 * is in/out; the returned map must be used in 3085 * the call to vm_map_lookup_done. 3086 * 3087 * A handle (out_entry) is returned for use in 3088 * vm_map_lookup_done, to make that fast. 3089 * 3090 * If a lookup is requested with "write protection" 3091 * specified, the map may be changed to perform virtual 3092 * copying operations, although the data referenced will 3093 * remain the same. 3094 */ 3095 int 3096 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 3097 vm_offset_t vaddr, 3098 vm_prot_t fault_typea, 3099 vm_map_entry_t *out_entry, /* OUT */ 3100 vm_object_t *object, /* OUT */ 3101 vm_pindex_t *pindex, /* OUT */ 3102 vm_prot_t *out_prot, /* OUT */ 3103 boolean_t *wired) /* OUT */ 3104 { 3105 vm_map_entry_t entry; 3106 vm_map_t map = *var_map; 3107 vm_prot_t prot; 3108 vm_prot_t fault_type = fault_typea; 3109 3110 RetryLookup:; 3111 /* 3112 * Lookup the faulting address. 3113 */ 3114 3115 vm_map_lock_read(map); 3116 #define RETURN(why) \ 3117 { \ 3118 vm_map_unlock_read(map); \ 3119 return (why); \ 3120 } 3121 3122 /* 3123 * If the map has an interesting hint, try it before calling full 3124 * blown lookup routine. 3125 */ 3126 entry = map->root; 3127 *out_entry = entry; 3128 if (entry == NULL || 3129 (vaddr < entry->start) || (vaddr >= entry->end)) { 3130 /* 3131 * Entry was either not a valid hint, or the vaddr was not 3132 * contained in the entry, so do a full lookup. 3133 */ 3134 if (!vm_map_lookup_entry(map, vaddr, out_entry)) 3135 RETURN(KERN_INVALID_ADDRESS); 3136 3137 entry = *out_entry; 3138 } 3139 3140 /* 3141 * Handle submaps. 3142 */ 3143 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3144 vm_map_t old_map = map; 3145 3146 *var_map = map = entry->object.sub_map; 3147 vm_map_unlock_read(old_map); 3148 goto RetryLookup; 3149 } 3150 3151 /* 3152 * Check whether this task is allowed to have this page. 3153 * Note the special case for MAP_ENTRY_COW 3154 * pages with an override. This is to implement a forced 3155 * COW for debuggers. 3156 */ 3157 if (fault_type & VM_PROT_OVERRIDE_WRITE) 3158 prot = entry->max_protection; 3159 else 3160 prot = entry->protection; 3161 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 3162 if ((fault_type & prot) != fault_type) { 3163 RETURN(KERN_PROTECTION_FAILURE); 3164 } 3165 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 3166 (entry->eflags & MAP_ENTRY_COW) && 3167 (fault_type & VM_PROT_WRITE) && 3168 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 3169 RETURN(KERN_PROTECTION_FAILURE); 3170 } 3171 3172 /* 3173 * If this page is not pageable, we have to get it for all possible 3174 * accesses. 3175 */ 3176 *wired = (entry->wired_count != 0); 3177 if (*wired) 3178 prot = fault_type = entry->protection; 3179 3180 /* 3181 * If the entry was copy-on-write, we either ... 3182 */ 3183 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3184 /* 3185 * If we want to write the page, we may as well handle that 3186 * now since we've got the map locked. 3187 * 3188 * If we don't need to write the page, we just demote the 3189 * permissions allowed. 3190 */ 3191 if (fault_type & VM_PROT_WRITE) { 3192 /* 3193 * Make a new object, and place it in the object 3194 * chain. Note that no new references have appeared 3195 * -- one just moved from the map to the new 3196 * object. 3197 */ 3198 if (vm_map_lock_upgrade(map)) 3199 goto RetryLookup; 3200 3201 vm_object_shadow( 3202 &entry->object.vm_object, 3203 &entry->offset, 3204 atop(entry->end - entry->start)); 3205 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 3206 3207 vm_map_lock_downgrade(map); 3208 } else { 3209 /* 3210 * We're attempting to read a copy-on-write page -- 3211 * don't allow writes. 3212 */ 3213 prot &= ~VM_PROT_WRITE; 3214 } 3215 } 3216 3217 /* 3218 * Create an object if necessary. 3219 */ 3220 if (entry->object.vm_object == NULL && 3221 !map->system_map) { 3222 if (vm_map_lock_upgrade(map)) 3223 goto RetryLookup; 3224 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 3225 atop(entry->end - entry->start)); 3226 entry->offset = 0; 3227 vm_map_lock_downgrade(map); 3228 } 3229 3230 /* 3231 * Return the object/offset from this entry. If the entry was 3232 * copy-on-write or empty, it has been fixed up. 3233 */ 3234 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 3235 *object = entry->object.vm_object; 3236 3237 *out_prot = prot; 3238 return (KERN_SUCCESS); 3239 3240 #undef RETURN 3241 } 3242 3243 /* 3244 * vm_map_lookup_locked: 3245 * 3246 * Lookup the faulting address. A version of vm_map_lookup that returns 3247 * KERN_FAILURE instead of blocking on map lock or memory allocation. 3248 */ 3249 int 3250 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */ 3251 vm_offset_t vaddr, 3252 vm_prot_t fault_typea, 3253 vm_map_entry_t *out_entry, /* OUT */ 3254 vm_object_t *object, /* OUT */ 3255 vm_pindex_t *pindex, /* OUT */ 3256 vm_prot_t *out_prot, /* OUT */ 3257 boolean_t *wired) /* OUT */ 3258 { 3259 vm_map_entry_t entry; 3260 vm_map_t map = *var_map; 3261 vm_prot_t prot; 3262 vm_prot_t fault_type = fault_typea; 3263 3264 /* 3265 * If the map has an interesting hint, try it before calling full 3266 * blown lookup routine. 3267 */ 3268 entry = map->root; 3269 *out_entry = entry; 3270 if (entry == NULL || 3271 (vaddr < entry->start) || (vaddr >= entry->end)) { 3272 /* 3273 * Entry was either not a valid hint, or the vaddr was not 3274 * contained in the entry, so do a full lookup. 3275 */ 3276 if (!vm_map_lookup_entry(map, vaddr, out_entry)) 3277 return (KERN_INVALID_ADDRESS); 3278 3279 entry = *out_entry; 3280 } 3281 3282 /* 3283 * Fail if the entry refers to a submap. 3284 */ 3285 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 3286 return (KERN_FAILURE); 3287 3288 /* 3289 * Check whether this task is allowed to have this page. 3290 * Note the special case for MAP_ENTRY_COW 3291 * pages with an override. This is to implement a forced 3292 * COW for debuggers. 3293 */ 3294 if (fault_type & VM_PROT_OVERRIDE_WRITE) 3295 prot = entry->max_protection; 3296 else 3297 prot = entry->protection; 3298 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 3299 if ((fault_type & prot) != fault_type) 3300 return (KERN_PROTECTION_FAILURE); 3301 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 3302 (entry->eflags & MAP_ENTRY_COW) && 3303 (fault_type & VM_PROT_WRITE) && 3304 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) 3305 return (KERN_PROTECTION_FAILURE); 3306 3307 /* 3308 * If this page is not pageable, we have to get it for all possible 3309 * accesses. 3310 */ 3311 *wired = (entry->wired_count != 0); 3312 if (*wired) 3313 prot = fault_type = entry->protection; 3314 3315 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3316 /* 3317 * Fail if the entry was copy-on-write for a write fault. 3318 */ 3319 if (fault_type & VM_PROT_WRITE) 3320 return (KERN_FAILURE); 3321 /* 3322 * We're attempting to read a copy-on-write page -- 3323 * don't allow writes. 3324 */ 3325 prot &= ~VM_PROT_WRITE; 3326 } 3327 3328 /* 3329 * Fail if an object should be created. 3330 */ 3331 if (entry->object.vm_object == NULL && !map->system_map) 3332 return (KERN_FAILURE); 3333 3334 /* 3335 * Return the object/offset from this entry. If the entry was 3336 * copy-on-write or empty, it has been fixed up. 3337 */ 3338 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 3339 *object = entry->object.vm_object; 3340 3341 *out_prot = prot; 3342 return (KERN_SUCCESS); 3343 } 3344 3345 /* 3346 * vm_map_lookup_done: 3347 * 3348 * Releases locks acquired by a vm_map_lookup 3349 * (according to the handle returned by that lookup). 3350 */ 3351 void 3352 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 3353 { 3354 /* 3355 * Unlock the main-level map 3356 */ 3357 vm_map_unlock_read(map); 3358 } 3359 3360 #include "opt_ddb.h" 3361 #ifdef DDB 3362 #include <sys/kernel.h> 3363 3364 #include <ddb/ddb.h> 3365 3366 /* 3367 * vm_map_print: [ debug ] 3368 */ 3369 DB_SHOW_COMMAND(map, vm_map_print) 3370 { 3371 static int nlines; 3372 /* XXX convert args. */ 3373 vm_map_t map = (vm_map_t)addr; 3374 boolean_t full = have_addr; 3375 3376 vm_map_entry_t entry; 3377 3378 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 3379 (void *)map, 3380 (void *)map->pmap, map->nentries, map->timestamp); 3381 nlines++; 3382 3383 if (!full && db_indent) 3384 return; 3385 3386 db_indent += 2; 3387 for (entry = map->header.next; entry != &map->header; 3388 entry = entry->next) { 3389 db_iprintf("map entry %p: start=%p, end=%p\n", 3390 (void *)entry, (void *)entry->start, (void *)entry->end); 3391 nlines++; 3392 { 3393 static char *inheritance_name[4] = 3394 {"share", "copy", "none", "donate_copy"}; 3395 3396 db_iprintf(" prot=%x/%x/%s", 3397 entry->protection, 3398 entry->max_protection, 3399 inheritance_name[(int)(unsigned char)entry->inheritance]); 3400 if (entry->wired_count != 0) 3401 db_printf(", wired"); 3402 } 3403 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3404 db_printf(", share=%p, offset=0x%jx\n", 3405 (void *)entry->object.sub_map, 3406 (uintmax_t)entry->offset); 3407 nlines++; 3408 if ((entry->prev == &map->header) || 3409 (entry->prev->object.sub_map != 3410 entry->object.sub_map)) { 3411 db_indent += 2; 3412 vm_map_print((db_expr_t)(intptr_t) 3413 entry->object.sub_map, 3414 full, 0, (char *)0); 3415 db_indent -= 2; 3416 } 3417 } else { 3418 db_printf(", object=%p, offset=0x%jx", 3419 (void *)entry->object.vm_object, 3420 (uintmax_t)entry->offset); 3421 if (entry->eflags & MAP_ENTRY_COW) 3422 db_printf(", copy (%s)", 3423 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3424 db_printf("\n"); 3425 nlines++; 3426 3427 if ((entry->prev == &map->header) || 3428 (entry->prev->object.vm_object != 3429 entry->object.vm_object)) { 3430 db_indent += 2; 3431 vm_object_print((db_expr_t)(intptr_t) 3432 entry->object.vm_object, 3433 full, 0, (char *)0); 3434 nlines += 4; 3435 db_indent -= 2; 3436 } 3437 } 3438 } 3439 db_indent -= 2; 3440 if (db_indent == 0) 3441 nlines = 0; 3442 } 3443 3444 3445 DB_SHOW_COMMAND(procvm, procvm) 3446 { 3447 struct proc *p; 3448 3449 if (have_addr) { 3450 p = (struct proc *) addr; 3451 } else { 3452 p = curproc; 3453 } 3454 3455 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3456 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3457 (void *)vmspace_pmap(p->p_vmspace)); 3458 3459 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 3460 } 3461 3462 #endif /* DDB */ 3463