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