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