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 vm_page_lock_queues(); 1395 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1396 VM_PROT_ALL) 1397 pmap_protect(map->pmap, current->start, 1398 current->end, 1399 current->protection & MASK(current)); 1400 #undef MASK 1401 vm_page_unlock_queues(); 1402 mtx_unlock(&Giant); 1403 } 1404 vm_map_simplify_entry(map, current); 1405 current = current->next; 1406 } 1407 vm_map_unlock(map); 1408 return (KERN_SUCCESS); 1409 } 1410 1411 /* 1412 * vm_map_madvise: 1413 * 1414 * This routine traverses a processes map handling the madvise 1415 * system call. Advisories are classified as either those effecting 1416 * the vm_map_entry structure, or those effecting the underlying 1417 * objects. 1418 */ 1419 int 1420 vm_map_madvise( 1421 vm_map_t map, 1422 vm_offset_t start, 1423 vm_offset_t end, 1424 int behav) 1425 { 1426 vm_map_entry_t current, entry; 1427 int modify_map = 0; 1428 1429 /* 1430 * Some madvise calls directly modify the vm_map_entry, in which case 1431 * we need to use an exclusive lock on the map and we need to perform 1432 * various clipping operations. Otherwise we only need a read-lock 1433 * on the map. 1434 */ 1435 switch(behav) { 1436 case MADV_NORMAL: 1437 case MADV_SEQUENTIAL: 1438 case MADV_RANDOM: 1439 case MADV_NOSYNC: 1440 case MADV_AUTOSYNC: 1441 case MADV_NOCORE: 1442 case MADV_CORE: 1443 modify_map = 1; 1444 vm_map_lock(map); 1445 break; 1446 case MADV_WILLNEED: 1447 case MADV_DONTNEED: 1448 case MADV_FREE: 1449 vm_map_lock_read(map); 1450 break; 1451 default: 1452 return (KERN_INVALID_ARGUMENT); 1453 } 1454 1455 /* 1456 * Locate starting entry and clip if necessary. 1457 */ 1458 VM_MAP_RANGE_CHECK(map, start, end); 1459 1460 if (vm_map_lookup_entry(map, start, &entry)) { 1461 if (modify_map) 1462 vm_map_clip_start(map, entry, start); 1463 } else { 1464 entry = entry->next; 1465 } 1466 1467 if (modify_map) { 1468 /* 1469 * madvise behaviors that are implemented in the vm_map_entry. 1470 * 1471 * We clip the vm_map_entry so that behavioral changes are 1472 * limited to the specified address range. 1473 */ 1474 for (current = entry; 1475 (current != &map->header) && (current->start < end); 1476 current = current->next 1477 ) { 1478 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1479 continue; 1480 1481 vm_map_clip_end(map, current, end); 1482 1483 switch (behav) { 1484 case MADV_NORMAL: 1485 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1486 break; 1487 case MADV_SEQUENTIAL: 1488 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1489 break; 1490 case MADV_RANDOM: 1491 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1492 break; 1493 case MADV_NOSYNC: 1494 current->eflags |= MAP_ENTRY_NOSYNC; 1495 break; 1496 case MADV_AUTOSYNC: 1497 current->eflags &= ~MAP_ENTRY_NOSYNC; 1498 break; 1499 case MADV_NOCORE: 1500 current->eflags |= MAP_ENTRY_NOCOREDUMP; 1501 break; 1502 case MADV_CORE: 1503 current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 1504 break; 1505 default: 1506 break; 1507 } 1508 vm_map_simplify_entry(map, current); 1509 } 1510 vm_map_unlock(map); 1511 } else { 1512 vm_pindex_t pindex; 1513 int count; 1514 1515 /* 1516 * madvise behaviors that are implemented in the underlying 1517 * vm_object. 1518 * 1519 * Since we don't clip the vm_map_entry, we have to clip 1520 * the vm_object pindex and count. 1521 */ 1522 for (current = entry; 1523 (current != &map->header) && (current->start < end); 1524 current = current->next 1525 ) { 1526 vm_offset_t useStart; 1527 1528 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1529 continue; 1530 1531 pindex = OFF_TO_IDX(current->offset); 1532 count = atop(current->end - current->start); 1533 useStart = current->start; 1534 1535 if (current->start < start) { 1536 pindex += atop(start - current->start); 1537 count -= atop(start - current->start); 1538 useStart = start; 1539 } 1540 if (current->end > end) 1541 count -= atop(current->end - end); 1542 1543 if (count <= 0) 1544 continue; 1545 1546 vm_object_madvise(current->object.vm_object, 1547 pindex, count, behav); 1548 if (behav == MADV_WILLNEED) { 1549 vm_map_pmap_enter(map, 1550 useStart, 1551 current->protection, 1552 current->object.vm_object, 1553 pindex, 1554 (count << PAGE_SHIFT), 1555 MAP_PREFAULT_MADVISE 1556 ); 1557 } 1558 } 1559 vm_map_unlock_read(map); 1560 } 1561 return (0); 1562 } 1563 1564 1565 /* 1566 * vm_map_inherit: 1567 * 1568 * Sets the inheritance of the specified address 1569 * range in the target map. Inheritance 1570 * affects how the map will be shared with 1571 * child maps at the time of vm_map_fork. 1572 */ 1573 int 1574 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1575 vm_inherit_t new_inheritance) 1576 { 1577 vm_map_entry_t entry; 1578 vm_map_entry_t temp_entry; 1579 1580 switch (new_inheritance) { 1581 case VM_INHERIT_NONE: 1582 case VM_INHERIT_COPY: 1583 case VM_INHERIT_SHARE: 1584 break; 1585 default: 1586 return (KERN_INVALID_ARGUMENT); 1587 } 1588 vm_map_lock(map); 1589 VM_MAP_RANGE_CHECK(map, start, end); 1590 if (vm_map_lookup_entry(map, start, &temp_entry)) { 1591 entry = temp_entry; 1592 vm_map_clip_start(map, entry, start); 1593 } else 1594 entry = temp_entry->next; 1595 while ((entry != &map->header) && (entry->start < end)) { 1596 vm_map_clip_end(map, entry, end); 1597 entry->inheritance = new_inheritance; 1598 vm_map_simplify_entry(map, entry); 1599 entry = entry->next; 1600 } 1601 vm_map_unlock(map); 1602 return (KERN_SUCCESS); 1603 } 1604 1605 /* 1606 * vm_map_unwire: 1607 * 1608 * Implements both kernel and user unwiring. 1609 */ 1610 int 1611 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1612 int flags) 1613 { 1614 vm_map_entry_t entry, first_entry, tmp_entry; 1615 vm_offset_t saved_start; 1616 unsigned int last_timestamp; 1617 int rv; 1618 boolean_t need_wakeup, result, user_unwire; 1619 1620 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 1621 vm_map_lock(map); 1622 VM_MAP_RANGE_CHECK(map, start, end); 1623 if (!vm_map_lookup_entry(map, start, &first_entry)) { 1624 if (flags & VM_MAP_WIRE_HOLESOK) 1625 first_entry = first_entry->next; 1626 else { 1627 vm_map_unlock(map); 1628 return (KERN_INVALID_ADDRESS); 1629 } 1630 } 1631 last_timestamp = map->timestamp; 1632 entry = first_entry; 1633 while (entry != &map->header && entry->start < end) { 1634 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 1635 /* 1636 * We have not yet clipped the entry. 1637 */ 1638 saved_start = (start >= entry->start) ? start : 1639 entry->start; 1640 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 1641 if (vm_map_unlock_and_wait(map, user_unwire)) { 1642 /* 1643 * Allow interruption of user unwiring? 1644 */ 1645 } 1646 vm_map_lock(map); 1647 if (last_timestamp+1 != map->timestamp) { 1648 /* 1649 * Look again for the entry because the map was 1650 * modified while it was unlocked. 1651 * Specifically, the entry may have been 1652 * clipped, merged, or deleted. 1653 */ 1654 if (!vm_map_lookup_entry(map, saved_start, 1655 &tmp_entry)) { 1656 if (flags & VM_MAP_WIRE_HOLESOK) 1657 tmp_entry = tmp_entry->next; 1658 else { 1659 if (saved_start == start) { 1660 /* 1661 * First_entry has been deleted. 1662 */ 1663 vm_map_unlock(map); 1664 return (KERN_INVALID_ADDRESS); 1665 } 1666 end = saved_start; 1667 rv = KERN_INVALID_ADDRESS; 1668 goto done; 1669 } 1670 } 1671 if (entry == first_entry) 1672 first_entry = tmp_entry; 1673 else 1674 first_entry = NULL; 1675 entry = tmp_entry; 1676 } 1677 last_timestamp = map->timestamp; 1678 continue; 1679 } 1680 vm_map_clip_start(map, entry, start); 1681 vm_map_clip_end(map, entry, end); 1682 /* 1683 * Mark the entry in case the map lock is released. (See 1684 * above.) 1685 */ 1686 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 1687 /* 1688 * Check the map for holes in the specified region. 1689 * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 1690 */ 1691 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 1692 (entry->end < end && (entry->next == &map->header || 1693 entry->next->start > entry->end))) { 1694 end = entry->end; 1695 rv = KERN_INVALID_ADDRESS; 1696 goto done; 1697 } 1698 /* 1699 * If system unwiring, require that the entry is system wired. 1700 */ 1701 if (!user_unwire && entry->wired_count < ((entry->eflags & 1702 MAP_ENTRY_USER_WIRED) ? 2 : 1)) { 1703 end = entry->end; 1704 rv = KERN_INVALID_ARGUMENT; 1705 goto done; 1706 } 1707 entry = entry->next; 1708 } 1709 rv = KERN_SUCCESS; 1710 done: 1711 need_wakeup = FALSE; 1712 if (first_entry == NULL) { 1713 result = vm_map_lookup_entry(map, start, &first_entry); 1714 if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 1715 first_entry = first_entry->next; 1716 else 1717 KASSERT(result, ("vm_map_unwire: lookup failed")); 1718 } 1719 entry = first_entry; 1720 while (entry != &map->header && entry->start < end) { 1721 if (rv == KERN_SUCCESS && (!user_unwire || 1722 (entry->eflags & MAP_ENTRY_USER_WIRED))) { 1723 if (user_unwire) 1724 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1725 entry->wired_count--; 1726 if (entry->wired_count == 0) { 1727 /* 1728 * Retain the map lock. 1729 */ 1730 vm_fault_unwire(map, entry->start, entry->end, 1731 entry->object.vm_object != NULL && 1732 entry->object.vm_object->type == OBJT_DEVICE); 1733 } 1734 } 1735 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 1736 ("vm_map_unwire: in-transition flag missing")); 1737 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 1738 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 1739 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 1740 need_wakeup = TRUE; 1741 } 1742 vm_map_simplify_entry(map, entry); 1743 entry = entry->next; 1744 } 1745 vm_map_unlock(map); 1746 if (need_wakeup) 1747 vm_map_wakeup(map); 1748 return (rv); 1749 } 1750 1751 /* 1752 * vm_map_wire: 1753 * 1754 * Implements both kernel and user wiring. 1755 */ 1756 int 1757 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1758 int flags) 1759 { 1760 vm_map_entry_t entry, first_entry, tmp_entry; 1761 vm_offset_t saved_end, saved_start; 1762 unsigned int last_timestamp; 1763 int rv; 1764 boolean_t fictitious, need_wakeup, result, user_wire; 1765 1766 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 1767 vm_map_lock(map); 1768 VM_MAP_RANGE_CHECK(map, start, end); 1769 if (!vm_map_lookup_entry(map, start, &first_entry)) { 1770 if (flags & VM_MAP_WIRE_HOLESOK) 1771 first_entry = first_entry->next; 1772 else { 1773 vm_map_unlock(map); 1774 return (KERN_INVALID_ADDRESS); 1775 } 1776 } 1777 last_timestamp = map->timestamp; 1778 entry = first_entry; 1779 while (entry != &map->header && entry->start < end) { 1780 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 1781 /* 1782 * We have not yet clipped the entry. 1783 */ 1784 saved_start = (start >= entry->start) ? start : 1785 entry->start; 1786 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 1787 if (vm_map_unlock_and_wait(map, user_wire)) { 1788 /* 1789 * Allow interruption of user wiring? 1790 */ 1791 } 1792 vm_map_lock(map); 1793 if (last_timestamp + 1 != map->timestamp) { 1794 /* 1795 * Look again for the entry because the map was 1796 * modified while it was unlocked. 1797 * Specifically, the entry may have been 1798 * clipped, merged, or deleted. 1799 */ 1800 if (!vm_map_lookup_entry(map, saved_start, 1801 &tmp_entry)) { 1802 if (flags & VM_MAP_WIRE_HOLESOK) 1803 tmp_entry = tmp_entry->next; 1804 else { 1805 if (saved_start == start) { 1806 /* 1807 * first_entry has been deleted. 1808 */ 1809 vm_map_unlock(map); 1810 return (KERN_INVALID_ADDRESS); 1811 } 1812 end = saved_start; 1813 rv = KERN_INVALID_ADDRESS; 1814 goto done; 1815 } 1816 } 1817 if (entry == first_entry) 1818 first_entry = tmp_entry; 1819 else 1820 first_entry = NULL; 1821 entry = tmp_entry; 1822 } 1823 last_timestamp = map->timestamp; 1824 continue; 1825 } 1826 vm_map_clip_start(map, entry, start); 1827 vm_map_clip_end(map, entry, end); 1828 /* 1829 * Mark the entry in case the map lock is released. (See 1830 * above.) 1831 */ 1832 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 1833 /* 1834 * 1835 */ 1836 if (entry->wired_count == 0) { 1837 entry->wired_count++; 1838 saved_start = entry->start; 1839 saved_end = entry->end; 1840 fictitious = entry->object.vm_object != NULL && 1841 entry->object.vm_object->type == OBJT_DEVICE; 1842 /* 1843 * Release the map lock, relying on the in-transition 1844 * mark. 1845 */ 1846 vm_map_unlock(map); 1847 rv = vm_fault_wire(map, saved_start, saved_end, 1848 user_wire, fictitious); 1849 vm_map_lock(map); 1850 if (last_timestamp + 1 != map->timestamp) { 1851 /* 1852 * Look again for the entry because the map was 1853 * modified while it was unlocked. The entry 1854 * may have been clipped, but NOT merged or 1855 * deleted. 1856 */ 1857 result = vm_map_lookup_entry(map, saved_start, 1858 &tmp_entry); 1859 KASSERT(result, ("vm_map_wire: lookup failed")); 1860 if (entry == first_entry) 1861 first_entry = tmp_entry; 1862 else 1863 first_entry = NULL; 1864 entry = tmp_entry; 1865 while (entry->end < saved_end) { 1866 if (rv != KERN_SUCCESS) { 1867 KASSERT(entry->wired_count == 1, 1868 ("vm_map_wire: bad count")); 1869 entry->wired_count = -1; 1870 } 1871 entry = entry->next; 1872 } 1873 } 1874 last_timestamp = map->timestamp; 1875 if (rv != KERN_SUCCESS) { 1876 KASSERT(entry->wired_count == 1, 1877 ("vm_map_wire: bad count")); 1878 /* 1879 * Assign an out-of-range value to represent 1880 * the failure to wire this entry. 1881 */ 1882 entry->wired_count = -1; 1883 end = entry->end; 1884 goto done; 1885 } 1886 } else if (!user_wire || 1887 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 1888 entry->wired_count++; 1889 } 1890 /* 1891 * Check the map for holes in the specified region. 1892 * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 1893 */ 1894 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 1895 (entry->end < end && (entry->next == &map->header || 1896 entry->next->start > entry->end))) { 1897 end = entry->end; 1898 rv = KERN_INVALID_ADDRESS; 1899 goto done; 1900 } 1901 entry = entry->next; 1902 } 1903 rv = KERN_SUCCESS; 1904 done: 1905 need_wakeup = FALSE; 1906 if (first_entry == NULL) { 1907 result = vm_map_lookup_entry(map, start, &first_entry); 1908 if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 1909 first_entry = first_entry->next; 1910 else 1911 KASSERT(result, ("vm_map_wire: lookup failed")); 1912 } 1913 entry = first_entry; 1914 while (entry != &map->header && entry->start < end) { 1915 if (rv == KERN_SUCCESS) { 1916 if (user_wire) 1917 entry->eflags |= MAP_ENTRY_USER_WIRED; 1918 } else if (entry->wired_count == -1) { 1919 /* 1920 * Wiring failed on this entry. Thus, unwiring is 1921 * unnecessary. 1922 */ 1923 entry->wired_count = 0; 1924 } else { 1925 if (!user_wire || 1926 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) 1927 entry->wired_count--; 1928 if (entry->wired_count == 0) { 1929 /* 1930 * Retain the map lock. 1931 */ 1932 vm_fault_unwire(map, entry->start, entry->end, 1933 entry->object.vm_object != NULL && 1934 entry->object.vm_object->type == OBJT_DEVICE); 1935 } 1936 } 1937 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 1938 ("vm_map_wire: in-transition flag missing")); 1939 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 1940 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 1941 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 1942 need_wakeup = TRUE; 1943 } 1944 vm_map_simplify_entry(map, entry); 1945 entry = entry->next; 1946 } 1947 vm_map_unlock(map); 1948 if (need_wakeup) 1949 vm_map_wakeup(map); 1950 return (rv); 1951 } 1952 1953 /* 1954 * vm_map_sync 1955 * 1956 * Push any dirty cached pages in the address range to their pager. 1957 * If syncio is TRUE, dirty pages are written synchronously. 1958 * If invalidate is TRUE, any cached pages are freed as well. 1959 * 1960 * If the size of the region from start to end is zero, we are 1961 * supposed to flush all modified pages within the region containing 1962 * start. Unfortunately, a region can be split or coalesced with 1963 * neighboring regions, making it difficult to determine what the 1964 * original region was. Therefore, we approximate this requirement by 1965 * flushing the current region containing start. 1966 * 1967 * Returns an error if any part of the specified range is not mapped. 1968 */ 1969 int 1970 vm_map_sync( 1971 vm_map_t map, 1972 vm_offset_t start, 1973 vm_offset_t end, 1974 boolean_t syncio, 1975 boolean_t invalidate) 1976 { 1977 vm_map_entry_t current; 1978 vm_map_entry_t entry; 1979 vm_size_t size; 1980 vm_object_t object; 1981 vm_ooffset_t offset; 1982 1983 vm_map_lock_read(map); 1984 VM_MAP_RANGE_CHECK(map, start, end); 1985 if (!vm_map_lookup_entry(map, start, &entry)) { 1986 vm_map_unlock_read(map); 1987 return (KERN_INVALID_ADDRESS); 1988 } else if (start == end) { 1989 start = entry->start; 1990 end = entry->end; 1991 } 1992 /* 1993 * Make a first pass to check for user-wired memory and holes. 1994 */ 1995 for (current = entry; current->start < end; current = current->next) { 1996 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) { 1997 vm_map_unlock_read(map); 1998 return (KERN_INVALID_ARGUMENT); 1999 } 2000 if (end > current->end && 2001 (current->next == &map->header || 2002 current->end != current->next->start)) { 2003 vm_map_unlock_read(map); 2004 return (KERN_INVALID_ADDRESS); 2005 } 2006 } 2007 2008 if (invalidate) { 2009 mtx_lock(&Giant); 2010 vm_page_lock_queues(); 2011 pmap_remove(map->pmap, start, end); 2012 vm_page_unlock_queues(); 2013 mtx_unlock(&Giant); 2014 } 2015 /* 2016 * Make a second pass, cleaning/uncaching pages from the indicated 2017 * objects as we go. 2018 */ 2019 for (current = entry; current->start < end; current = current->next) { 2020 offset = current->offset + (start - current->start); 2021 size = (end <= current->end ? end : current->end) - start; 2022 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2023 vm_map_t smap; 2024 vm_map_entry_t tentry; 2025 vm_size_t tsize; 2026 2027 smap = current->object.sub_map; 2028 vm_map_lock_read(smap); 2029 (void) vm_map_lookup_entry(smap, offset, &tentry); 2030 tsize = tentry->end - offset; 2031 if (tsize < size) 2032 size = tsize; 2033 object = tentry->object.vm_object; 2034 offset = tentry->offset + (offset - tentry->start); 2035 vm_map_unlock_read(smap); 2036 } else { 2037 object = current->object.vm_object; 2038 } 2039 vm_object_sync(object, offset, size, syncio, invalidate); 2040 start += size; 2041 } 2042 2043 vm_map_unlock_read(map); 2044 return (KERN_SUCCESS); 2045 } 2046 2047 /* 2048 * vm_map_entry_unwire: [ internal use only ] 2049 * 2050 * Make the region specified by this entry pageable. 2051 * 2052 * The map in question should be locked. 2053 * [This is the reason for this routine's existence.] 2054 */ 2055 static void 2056 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2057 { 2058 vm_fault_unwire(map, entry->start, entry->end, 2059 entry->object.vm_object != NULL && 2060 entry->object.vm_object->type == OBJT_DEVICE); 2061 entry->wired_count = 0; 2062 } 2063 2064 /* 2065 * vm_map_entry_delete: [ internal use only ] 2066 * 2067 * Deallocate the given entry from the target map. 2068 */ 2069 static void 2070 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2071 { 2072 vm_object_t object; 2073 vm_pindex_t offidxstart, offidxend, count; 2074 2075 vm_map_entry_unlink(map, entry); 2076 map->size -= entry->end - entry->start; 2077 2078 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 2079 (object = entry->object.vm_object) != NULL) { 2080 count = OFF_TO_IDX(entry->end - entry->start); 2081 offidxstart = OFF_TO_IDX(entry->offset); 2082 offidxend = offidxstart + count; 2083 VM_OBJECT_LOCK(object); 2084 if (object->ref_count != 1 && 2085 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING || 2086 object == kernel_object || object == kmem_object) && 2087 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 2088 vm_object_collapse(object); 2089 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 2090 if (object->type == OBJT_SWAP) 2091 swap_pager_freespace(object, offidxstart, count); 2092 if (offidxend >= object->size && 2093 offidxstart < object->size) 2094 object->size = offidxstart; 2095 } 2096 VM_OBJECT_UNLOCK(object); 2097 vm_object_deallocate(object); 2098 } 2099 2100 vm_map_entry_dispose(map, entry); 2101 } 2102 2103 /* 2104 * vm_map_delete: [ internal use only ] 2105 * 2106 * Deallocates the given address range from the target 2107 * map. 2108 */ 2109 int 2110 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 2111 { 2112 vm_map_entry_t entry; 2113 vm_map_entry_t first_entry; 2114 2115 /* 2116 * Find the start of the region, and clip it 2117 */ 2118 if (!vm_map_lookup_entry(map, start, &first_entry)) 2119 entry = first_entry->next; 2120 else { 2121 entry = first_entry; 2122 vm_map_clip_start(map, entry, start); 2123 } 2124 2125 /* 2126 * Save the free space hint 2127 */ 2128 if (entry == &map->header) { 2129 map->first_free = &map->header; 2130 } else if (map->first_free->start >= start) { 2131 map->first_free = entry->prev; 2132 } 2133 2134 /* 2135 * Step through all entries in this region 2136 */ 2137 while ((entry != &map->header) && (entry->start < end)) { 2138 vm_map_entry_t next; 2139 2140 /* 2141 * Wait for wiring or unwiring of an entry to complete. 2142 */ 2143 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0) { 2144 unsigned int last_timestamp; 2145 vm_offset_t saved_start; 2146 vm_map_entry_t tmp_entry; 2147 2148 saved_start = entry->start; 2149 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2150 last_timestamp = map->timestamp; 2151 (void) vm_map_unlock_and_wait(map, FALSE); 2152 vm_map_lock(map); 2153 if (last_timestamp + 1 != map->timestamp) { 2154 /* 2155 * Look again for the entry because the map was 2156 * modified while it was unlocked. 2157 * Specifically, the entry may have been 2158 * clipped, merged, or deleted. 2159 */ 2160 if (!vm_map_lookup_entry(map, saved_start, 2161 &tmp_entry)) 2162 entry = tmp_entry->next; 2163 else { 2164 entry = tmp_entry; 2165 vm_map_clip_start(map, entry, 2166 saved_start); 2167 } 2168 } 2169 continue; 2170 } 2171 vm_map_clip_end(map, entry, end); 2172 2173 next = entry->next; 2174 2175 /* 2176 * Unwire before removing addresses from the pmap; otherwise, 2177 * unwiring will put the entries back in the pmap. 2178 */ 2179 if (entry->wired_count != 0) { 2180 vm_map_entry_unwire(map, entry); 2181 } 2182 2183 if (!map->system_map) 2184 mtx_lock(&Giant); 2185 vm_page_lock_queues(); 2186 pmap_remove(map->pmap, entry->start, entry->end); 2187 vm_page_unlock_queues(); 2188 if (!map->system_map) 2189 mtx_unlock(&Giant); 2190 2191 /* 2192 * Delete the entry (which may delete the object) only after 2193 * removing all pmap entries pointing to its pages. 2194 * (Otherwise, its page frames may be reallocated, and any 2195 * modify bits will be set in the wrong object!) 2196 */ 2197 vm_map_entry_delete(map, entry); 2198 entry = next; 2199 } 2200 return (KERN_SUCCESS); 2201 } 2202 2203 /* 2204 * vm_map_remove: 2205 * 2206 * Remove the given address range from the target map. 2207 * This is the exported form of vm_map_delete. 2208 */ 2209 int 2210 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 2211 { 2212 int result, s = 0; 2213 2214 if (map == kmem_map) 2215 s = splvm(); 2216 2217 vm_map_lock(map); 2218 VM_MAP_RANGE_CHECK(map, start, end); 2219 result = vm_map_delete(map, start, end); 2220 vm_map_unlock(map); 2221 2222 if (map == kmem_map) 2223 splx(s); 2224 2225 return (result); 2226 } 2227 2228 /* 2229 * vm_map_check_protection: 2230 * 2231 * Assert that the target map allows the specified privilege on the 2232 * entire address region given. The entire region must be allocated. 2233 * 2234 * WARNING! This code does not and should not check whether the 2235 * contents of the region is accessible. For example a smaller file 2236 * might be mapped into a larger address space. 2237 * 2238 * NOTE! This code is also called by munmap(). 2239 * 2240 * The map must be locked. A read lock is sufficient. 2241 */ 2242 boolean_t 2243 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 2244 vm_prot_t protection) 2245 { 2246 vm_map_entry_t entry; 2247 vm_map_entry_t tmp_entry; 2248 2249 if (!vm_map_lookup_entry(map, start, &tmp_entry)) 2250 return (FALSE); 2251 entry = tmp_entry; 2252 2253 while (start < end) { 2254 if (entry == &map->header) 2255 return (FALSE); 2256 /* 2257 * No holes allowed! 2258 */ 2259 if (start < entry->start) 2260 return (FALSE); 2261 /* 2262 * Check protection associated with entry. 2263 */ 2264 if ((entry->protection & protection) != protection) 2265 return (FALSE); 2266 /* go to next entry */ 2267 start = entry->end; 2268 entry = entry->next; 2269 } 2270 return (TRUE); 2271 } 2272 2273 /* 2274 * vm_map_copy_entry: 2275 * 2276 * Copies the contents of the source entry to the destination 2277 * entry. The entries *must* be aligned properly. 2278 */ 2279 static void 2280 vm_map_copy_entry( 2281 vm_map_t src_map, 2282 vm_map_t dst_map, 2283 vm_map_entry_t src_entry, 2284 vm_map_entry_t dst_entry) 2285 { 2286 vm_object_t src_object; 2287 2288 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2289 return; 2290 2291 if (src_entry->wired_count == 0) { 2292 2293 /* 2294 * If the source entry is marked needs_copy, it is already 2295 * write-protected. 2296 */ 2297 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2298 vm_page_lock_queues(); 2299 pmap_protect(src_map->pmap, 2300 src_entry->start, 2301 src_entry->end, 2302 src_entry->protection & ~VM_PROT_WRITE); 2303 vm_page_unlock_queues(); 2304 } 2305 2306 /* 2307 * Make a copy of the object. 2308 */ 2309 if ((src_object = src_entry->object.vm_object) != NULL) { 2310 VM_OBJECT_LOCK(src_object); 2311 if ((src_object->handle == NULL) && 2312 (src_object->type == OBJT_DEFAULT || 2313 src_object->type == OBJT_SWAP)) { 2314 vm_object_collapse(src_object); 2315 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2316 vm_object_split(src_entry); 2317 src_object = src_entry->object.vm_object; 2318 } 2319 } 2320 vm_object_reference_locked(src_object); 2321 vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2322 VM_OBJECT_UNLOCK(src_object); 2323 dst_entry->object.vm_object = src_object; 2324 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2325 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2326 dst_entry->offset = src_entry->offset; 2327 } else { 2328 dst_entry->object.vm_object = NULL; 2329 dst_entry->offset = 0; 2330 } 2331 2332 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2333 dst_entry->end - dst_entry->start, src_entry->start); 2334 } else { 2335 /* 2336 * Of course, wired down pages can't be set copy-on-write. 2337 * Cause wired pages to be copied into the new map by 2338 * simulating faults (the new pages are pageable) 2339 */ 2340 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2341 } 2342 } 2343 2344 /* 2345 * vmspace_fork: 2346 * Create a new process vmspace structure and vm_map 2347 * based on those of an existing process. The new map 2348 * is based on the old map, according to the inheritance 2349 * values on the regions in that map. 2350 * 2351 * The source map must not be locked. 2352 */ 2353 struct vmspace * 2354 vmspace_fork(struct vmspace *vm1) 2355 { 2356 struct vmspace *vm2; 2357 vm_map_t old_map = &vm1->vm_map; 2358 vm_map_t new_map; 2359 vm_map_entry_t old_entry; 2360 vm_map_entry_t new_entry; 2361 vm_object_t object; 2362 2363 GIANT_REQUIRED; 2364 2365 vm_map_lock(old_map); 2366 old_map->infork = 1; 2367 2368 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2369 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 2370 (caddr_t) &vm1->vm_endcopy - (caddr_t) &vm1->vm_startcopy); 2371 new_map = &vm2->vm_map; /* XXX */ 2372 new_map->timestamp = 1; 2373 2374 /* Do not inherit the MAP_WIREFUTURE property. */ 2375 if ((new_map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE) 2376 new_map->flags &= ~MAP_WIREFUTURE; 2377 2378 old_entry = old_map->header.next; 2379 2380 while (old_entry != &old_map->header) { 2381 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2382 panic("vm_map_fork: encountered a submap"); 2383 2384 switch (old_entry->inheritance) { 2385 case VM_INHERIT_NONE: 2386 break; 2387 2388 case VM_INHERIT_SHARE: 2389 /* 2390 * Clone the entry, creating the shared object if necessary. 2391 */ 2392 object = old_entry->object.vm_object; 2393 if (object == NULL) { 2394 object = vm_object_allocate(OBJT_DEFAULT, 2395 atop(old_entry->end - old_entry->start)); 2396 old_entry->object.vm_object = object; 2397 old_entry->offset = (vm_offset_t) 0; 2398 } 2399 2400 /* 2401 * Add the reference before calling vm_object_shadow 2402 * to insure that a shadow object is created. 2403 */ 2404 vm_object_reference(object); 2405 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2406 vm_object_shadow(&old_entry->object.vm_object, 2407 &old_entry->offset, 2408 atop(old_entry->end - old_entry->start)); 2409 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2410 /* Transfer the second reference too. */ 2411 vm_object_reference( 2412 old_entry->object.vm_object); 2413 vm_object_deallocate(object); 2414 object = old_entry->object.vm_object; 2415 } 2416 VM_OBJECT_LOCK(object); 2417 vm_object_clear_flag(object, OBJ_ONEMAPPING); 2418 VM_OBJECT_UNLOCK(object); 2419 2420 /* 2421 * Clone the entry, referencing the shared object. 2422 */ 2423 new_entry = vm_map_entry_create(new_map); 2424 *new_entry = *old_entry; 2425 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2426 new_entry->wired_count = 0; 2427 2428 /* 2429 * Insert the entry into the new map -- we know we're 2430 * inserting at the end of the new map. 2431 */ 2432 vm_map_entry_link(new_map, new_map->header.prev, 2433 new_entry); 2434 2435 /* 2436 * Update the physical map 2437 */ 2438 pmap_copy(new_map->pmap, old_map->pmap, 2439 new_entry->start, 2440 (old_entry->end - old_entry->start), 2441 old_entry->start); 2442 break; 2443 2444 case VM_INHERIT_COPY: 2445 /* 2446 * Clone the entry and link into the map. 2447 */ 2448 new_entry = vm_map_entry_create(new_map); 2449 *new_entry = *old_entry; 2450 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2451 new_entry->wired_count = 0; 2452 new_entry->object.vm_object = NULL; 2453 vm_map_entry_link(new_map, new_map->header.prev, 2454 new_entry); 2455 vm_map_copy_entry(old_map, new_map, old_entry, 2456 new_entry); 2457 break; 2458 } 2459 old_entry = old_entry->next; 2460 } 2461 2462 new_map->size = old_map->size; 2463 old_map->infork = 0; 2464 vm_map_unlock(old_map); 2465 2466 return (vm2); 2467 } 2468 2469 int 2470 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 2471 vm_prot_t prot, vm_prot_t max, int cow) 2472 { 2473 vm_map_entry_t new_entry, prev_entry; 2474 vm_offset_t bot, top; 2475 vm_size_t init_ssize; 2476 int orient, rv; 2477 rlim_t vmemlim; 2478 2479 /* 2480 * The stack orientation is piggybacked with the cow argument. 2481 * Extract it into orient and mask the cow argument so that we 2482 * don't pass it around further. 2483 * NOTE: We explicitly allow bi-directional stacks. 2484 */ 2485 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP); 2486 cow &= ~orient; 2487 KASSERT(orient != 0, ("No stack grow direction")); 2488 2489 if (addrbos < vm_map_min(map) || addrbos > map->max_offset) 2490 return (KERN_NO_SPACE); 2491 2492 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz; 2493 2494 PROC_LOCK(curthread->td_proc); 2495 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM); 2496 PROC_UNLOCK(curthread->td_proc); 2497 2498 vm_map_lock(map); 2499 2500 /* If addr is already mapped, no go */ 2501 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 2502 vm_map_unlock(map); 2503 return (KERN_NO_SPACE); 2504 } 2505 2506 /* If we would blow our VMEM resource limit, no go */ 2507 if (map->size + init_ssize > vmemlim) { 2508 vm_map_unlock(map); 2509 return (KERN_NO_SPACE); 2510 } 2511 2512 /* 2513 * If we can't accomodate max_ssize in the current mapping, no go. 2514 * However, we need to be aware that subsequent user mappings might 2515 * map into the space we have reserved for stack, and currently this 2516 * space is not protected. 2517 * 2518 * Hopefully we will at least detect this condition when we try to 2519 * grow the stack. 2520 */ 2521 if ((prev_entry->next != &map->header) && 2522 (prev_entry->next->start < addrbos + max_ssize)) { 2523 vm_map_unlock(map); 2524 return (KERN_NO_SPACE); 2525 } 2526 2527 /* 2528 * We initially map a stack of only init_ssize. We will grow as 2529 * needed later. Depending on the orientation of the stack (i.e. 2530 * the grow direction) we either map at the top of the range, the 2531 * bottom of the range or in the middle. 2532 * 2533 * Note: we would normally expect prot and max to be VM_PROT_ALL, 2534 * and cow to be 0. Possibly we should eliminate these as input 2535 * parameters, and just pass these values here in the insert call. 2536 */ 2537 if (orient == MAP_STACK_GROWS_DOWN) 2538 bot = addrbos + max_ssize - init_ssize; 2539 else if (orient == MAP_STACK_GROWS_UP) 2540 bot = addrbos; 2541 else 2542 bot = round_page(addrbos + max_ssize/2 - init_ssize/2); 2543 top = bot + init_ssize; 2544 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow); 2545 2546 /* Now set the avail_ssize amount. */ 2547 if (rv == KERN_SUCCESS) { 2548 if (prev_entry != &map->header) 2549 vm_map_clip_end(map, prev_entry, bot); 2550 new_entry = prev_entry->next; 2551 if (new_entry->end != top || new_entry->start != bot) 2552 panic("Bad entry start/end for new stack entry"); 2553 2554 new_entry->avail_ssize = max_ssize - init_ssize; 2555 if (orient & MAP_STACK_GROWS_DOWN) 2556 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 2557 if (orient & MAP_STACK_GROWS_UP) 2558 new_entry->eflags |= MAP_ENTRY_GROWS_UP; 2559 } 2560 2561 vm_map_unlock(map); 2562 return (rv); 2563 } 2564 2565 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 2566 * desired address is already mapped, or if we successfully grow 2567 * the stack. Also returns KERN_SUCCESS if addr is outside the 2568 * stack range (this is strange, but preserves compatibility with 2569 * the grow function in vm_machdep.c). 2570 */ 2571 int 2572 vm_map_growstack(struct proc *p, vm_offset_t addr) 2573 { 2574 vm_map_entry_t next_entry, prev_entry; 2575 vm_map_entry_t new_entry, stack_entry; 2576 struct vmspace *vm = p->p_vmspace; 2577 vm_map_t map = &vm->vm_map; 2578 vm_offset_t end; 2579 size_t grow_amount, max_grow; 2580 rlim_t stacklim, vmemlim; 2581 int is_procstack, rv; 2582 2583 Retry: 2584 PROC_LOCK(p); 2585 stacklim = lim_cur(p, RLIMIT_STACK); 2586 vmemlim = lim_cur(p, RLIMIT_VMEM); 2587 PROC_UNLOCK(p); 2588 2589 vm_map_lock_read(map); 2590 2591 /* If addr is already in the entry range, no need to grow.*/ 2592 if (vm_map_lookup_entry(map, addr, &prev_entry)) { 2593 vm_map_unlock_read(map); 2594 return (KERN_SUCCESS); 2595 } 2596 2597 next_entry = prev_entry->next; 2598 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) { 2599 /* 2600 * This entry does not grow upwards. Since the address lies 2601 * beyond this entry, the next entry (if one exists) has to 2602 * be a downward growable entry. The entry list header is 2603 * never a growable entry, so it suffices to check the flags. 2604 */ 2605 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) { 2606 vm_map_unlock_read(map); 2607 return (KERN_SUCCESS); 2608 } 2609 stack_entry = next_entry; 2610 } else { 2611 /* 2612 * This entry grows upward. If the next entry does not at 2613 * least grow downwards, this is the entry we need to grow. 2614 * otherwise we have two possible choices and we have to 2615 * select one. 2616 */ 2617 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) { 2618 /* 2619 * We have two choices; grow the entry closest to 2620 * the address to minimize the amount of growth. 2621 */ 2622 if (addr - prev_entry->end <= next_entry->start - addr) 2623 stack_entry = prev_entry; 2624 else 2625 stack_entry = next_entry; 2626 } else 2627 stack_entry = prev_entry; 2628 } 2629 2630 if (stack_entry == next_entry) { 2631 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo")); 2632 KASSERT(addr < stack_entry->start, ("foo")); 2633 end = (prev_entry != &map->header) ? prev_entry->end : 2634 stack_entry->start - stack_entry->avail_ssize; 2635 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE); 2636 max_grow = stack_entry->start - end; 2637 } else { 2638 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo")); 2639 KASSERT(addr >= stack_entry->end, ("foo")); 2640 end = (next_entry != &map->header) ? next_entry->start : 2641 stack_entry->end + stack_entry->avail_ssize; 2642 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE); 2643 max_grow = end - stack_entry->end; 2644 } 2645 2646 if (grow_amount > stack_entry->avail_ssize) { 2647 vm_map_unlock_read(map); 2648 return (KERN_NO_SPACE); 2649 } 2650 2651 /* 2652 * If there is no longer enough space between the entries nogo, and 2653 * adjust the available space. Note: this should only happen if the 2654 * user has mapped into the stack area after the stack was created, 2655 * and is probably an error. 2656 * 2657 * This also effectively destroys any guard page the user might have 2658 * intended by limiting the stack size. 2659 */ 2660 if (grow_amount > max_grow) { 2661 if (vm_map_lock_upgrade(map)) 2662 goto Retry; 2663 2664 stack_entry->avail_ssize = max_grow; 2665 2666 vm_map_unlock(map); 2667 return (KERN_NO_SPACE); 2668 } 2669 2670 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0; 2671 2672 /* 2673 * If this is the main process stack, see if we're over the stack 2674 * limit. 2675 */ 2676 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 2677 vm_map_unlock_read(map); 2678 return (KERN_NO_SPACE); 2679 } 2680 2681 /* Round up the grow amount modulo SGROWSIZ */ 2682 grow_amount = roundup (grow_amount, sgrowsiz); 2683 if (grow_amount > stack_entry->avail_ssize) 2684 grow_amount = stack_entry->avail_ssize; 2685 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 2686 grow_amount = stacklim - ctob(vm->vm_ssize); 2687 } 2688 2689 /* If we would blow our VMEM resource limit, no go */ 2690 if (map->size + grow_amount > vmemlim) { 2691 vm_map_unlock_read(map); 2692 return (KERN_NO_SPACE); 2693 } 2694 2695 if (vm_map_lock_upgrade(map)) 2696 goto Retry; 2697 2698 if (stack_entry == next_entry) { 2699 /* 2700 * Growing downward. 2701 */ 2702 /* Get the preliminary new entry start value */ 2703 addr = stack_entry->start - grow_amount; 2704 2705 /* 2706 * If this puts us into the previous entry, cut back our 2707 * growth to the available space. Also, see the note above. 2708 */ 2709 if (addr < end) { 2710 stack_entry->avail_ssize = max_grow; 2711 addr = end; 2712 } 2713 2714 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 2715 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0); 2716 2717 /* Adjust the available stack space by the amount we grew. */ 2718 if (rv == KERN_SUCCESS) { 2719 if (prev_entry != &map->header) 2720 vm_map_clip_end(map, prev_entry, addr); 2721 new_entry = prev_entry->next; 2722 KASSERT(new_entry == stack_entry->prev, ("foo")); 2723 KASSERT(new_entry->end == stack_entry->start, ("foo")); 2724 KASSERT(new_entry->start == addr, ("foo")); 2725 grow_amount = new_entry->end - new_entry->start; 2726 new_entry->avail_ssize = stack_entry->avail_ssize - 2727 grow_amount; 2728 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN; 2729 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 2730 } 2731 } else { 2732 /* 2733 * Growing upward. 2734 */ 2735 addr = stack_entry->end + grow_amount; 2736 2737 /* 2738 * If this puts us into the next entry, cut back our growth 2739 * to the available space. Also, see the note above. 2740 */ 2741 if (addr > end) { 2742 stack_entry->avail_ssize = end - stack_entry->end; 2743 addr = end; 2744 } 2745 2746 grow_amount = addr - stack_entry->end; 2747 2748 /* Grow the underlying object if applicable. */ 2749 if (stack_entry->object.vm_object == NULL || 2750 vm_object_coalesce(stack_entry->object.vm_object, 2751 OFF_TO_IDX(stack_entry->offset), 2752 (vm_size_t)(stack_entry->end - stack_entry->start), 2753 (vm_size_t)grow_amount)) { 2754 map->size += (addr - stack_entry->end); 2755 /* Update the current entry. */ 2756 stack_entry->end = addr; 2757 stack_entry->avail_ssize -= grow_amount; 2758 rv = KERN_SUCCESS; 2759 2760 if (next_entry != &map->header) 2761 vm_map_clip_start(map, next_entry, addr); 2762 } else 2763 rv = KERN_FAILURE; 2764 } 2765 2766 if (rv == KERN_SUCCESS && is_procstack) 2767 vm->vm_ssize += btoc(grow_amount); 2768 2769 vm_map_unlock(map); 2770 2771 /* 2772 * Heed the MAP_WIREFUTURE flag if it was set for this process. 2773 */ 2774 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) { 2775 vm_map_wire(map, 2776 (stack_entry == next_entry) ? addr : addr - grow_amount, 2777 (stack_entry == next_entry) ? stack_entry->start : addr, 2778 (p->p_flag & P_SYSTEM) 2779 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES 2780 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 2781 } 2782 2783 return (rv); 2784 } 2785 2786 /* 2787 * Unshare the specified VM space for exec. If other processes are 2788 * mapped to it, then create a new one. The new vmspace is null. 2789 */ 2790 void 2791 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser) 2792 { 2793 struct vmspace *oldvmspace = p->p_vmspace; 2794 struct vmspace *newvmspace; 2795 2796 GIANT_REQUIRED; 2797 newvmspace = vmspace_alloc(minuser, maxuser); 2798 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy, 2799 (caddr_t) &newvmspace->vm_endcopy - 2800 (caddr_t) &newvmspace->vm_startcopy); 2801 /* 2802 * This code is written like this for prototype purposes. The 2803 * goal is to avoid running down the vmspace here, but let the 2804 * other process's that are still using the vmspace to finally 2805 * run it down. Even though there is little or no chance of blocking 2806 * here, it is a good idea to keep this form for future mods. 2807 */ 2808 p->p_vmspace = newvmspace; 2809 if (p == curthread->td_proc) /* XXXKSE ? */ 2810 pmap_activate(curthread); 2811 vmspace_free(oldvmspace); 2812 } 2813 2814 /* 2815 * Unshare the specified VM space for forcing COW. This 2816 * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 2817 */ 2818 void 2819 vmspace_unshare(struct proc *p) 2820 { 2821 struct vmspace *oldvmspace = p->p_vmspace; 2822 struct vmspace *newvmspace; 2823 2824 GIANT_REQUIRED; 2825 if (oldvmspace->vm_refcnt == 1) 2826 return; 2827 newvmspace = vmspace_fork(oldvmspace); 2828 p->p_vmspace = newvmspace; 2829 if (p == curthread->td_proc) /* XXXKSE ? */ 2830 pmap_activate(curthread); 2831 vmspace_free(oldvmspace); 2832 } 2833 2834 /* 2835 * vm_map_lookup: 2836 * 2837 * Finds the VM object, offset, and 2838 * protection for a given virtual address in the 2839 * specified map, assuming a page fault of the 2840 * type specified. 2841 * 2842 * Leaves the map in question locked for read; return 2843 * values are guaranteed until a vm_map_lookup_done 2844 * call is performed. Note that the map argument 2845 * is in/out; the returned map must be used in 2846 * the call to vm_map_lookup_done. 2847 * 2848 * A handle (out_entry) is returned for use in 2849 * vm_map_lookup_done, to make that fast. 2850 * 2851 * If a lookup is requested with "write protection" 2852 * specified, the map may be changed to perform virtual 2853 * copying operations, although the data referenced will 2854 * remain the same. 2855 */ 2856 int 2857 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 2858 vm_offset_t vaddr, 2859 vm_prot_t fault_typea, 2860 vm_map_entry_t *out_entry, /* OUT */ 2861 vm_object_t *object, /* OUT */ 2862 vm_pindex_t *pindex, /* OUT */ 2863 vm_prot_t *out_prot, /* OUT */ 2864 boolean_t *wired) /* OUT */ 2865 { 2866 vm_map_entry_t entry; 2867 vm_map_t map = *var_map; 2868 vm_prot_t prot; 2869 vm_prot_t fault_type = fault_typea; 2870 2871 RetryLookup:; 2872 /* 2873 * Lookup the faulting address. 2874 */ 2875 2876 vm_map_lock_read(map); 2877 #define RETURN(why) \ 2878 { \ 2879 vm_map_unlock_read(map); \ 2880 return (why); \ 2881 } 2882 2883 /* 2884 * If the map has an interesting hint, try it before calling full 2885 * blown lookup routine. 2886 */ 2887 entry = map->root; 2888 *out_entry = entry; 2889 if (entry == NULL || 2890 (vaddr < entry->start) || (vaddr >= entry->end)) { 2891 /* 2892 * Entry was either not a valid hint, or the vaddr was not 2893 * contained in the entry, so do a full lookup. 2894 */ 2895 if (!vm_map_lookup_entry(map, vaddr, out_entry)) 2896 RETURN(KERN_INVALID_ADDRESS); 2897 2898 entry = *out_entry; 2899 } 2900 2901 /* 2902 * Handle submaps. 2903 */ 2904 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2905 vm_map_t old_map = map; 2906 2907 *var_map = map = entry->object.sub_map; 2908 vm_map_unlock_read(old_map); 2909 goto RetryLookup; 2910 } 2911 2912 /* 2913 * Check whether this task is allowed to have this page. 2914 * Note the special case for MAP_ENTRY_COW 2915 * pages with an override. This is to implement a forced 2916 * COW for debuggers. 2917 */ 2918 if (fault_type & VM_PROT_OVERRIDE_WRITE) 2919 prot = entry->max_protection; 2920 else 2921 prot = entry->protection; 2922 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 2923 if ((fault_type & prot) != fault_type) { 2924 RETURN(KERN_PROTECTION_FAILURE); 2925 } 2926 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 2927 (entry->eflags & MAP_ENTRY_COW) && 2928 (fault_type & VM_PROT_WRITE) && 2929 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 2930 RETURN(KERN_PROTECTION_FAILURE); 2931 } 2932 2933 /* 2934 * If this page is not pageable, we have to get it for all possible 2935 * accesses. 2936 */ 2937 *wired = (entry->wired_count != 0); 2938 if (*wired) 2939 prot = fault_type = entry->protection; 2940 2941 /* 2942 * If the entry was copy-on-write, we either ... 2943 */ 2944 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2945 /* 2946 * If we want to write the page, we may as well handle that 2947 * now since we've got the map locked. 2948 * 2949 * If we don't need to write the page, we just demote the 2950 * permissions allowed. 2951 */ 2952 if (fault_type & VM_PROT_WRITE) { 2953 /* 2954 * Make a new object, and place it in the object 2955 * chain. Note that no new references have appeared 2956 * -- one just moved from the map to the new 2957 * object. 2958 */ 2959 if (vm_map_lock_upgrade(map)) 2960 goto RetryLookup; 2961 2962 vm_object_shadow( 2963 &entry->object.vm_object, 2964 &entry->offset, 2965 atop(entry->end - entry->start)); 2966 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2967 2968 vm_map_lock_downgrade(map); 2969 } else { 2970 /* 2971 * We're attempting to read a copy-on-write page -- 2972 * don't allow writes. 2973 */ 2974 prot &= ~VM_PROT_WRITE; 2975 } 2976 } 2977 2978 /* 2979 * Create an object if necessary. 2980 */ 2981 if (entry->object.vm_object == NULL && 2982 !map->system_map) { 2983 if (vm_map_lock_upgrade(map)) 2984 goto RetryLookup; 2985 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 2986 atop(entry->end - entry->start)); 2987 entry->offset = 0; 2988 vm_map_lock_downgrade(map); 2989 } 2990 2991 /* 2992 * Return the object/offset from this entry. If the entry was 2993 * copy-on-write or empty, it has been fixed up. 2994 */ 2995 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 2996 *object = entry->object.vm_object; 2997 2998 /* 2999 * Return whether this is the only map sharing this data. 3000 */ 3001 *out_prot = prot; 3002 return (KERN_SUCCESS); 3003 3004 #undef RETURN 3005 } 3006 3007 /* 3008 * vm_map_lookup_done: 3009 * 3010 * Releases locks acquired by a vm_map_lookup 3011 * (according to the handle returned by that lookup). 3012 */ 3013 void 3014 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 3015 { 3016 /* 3017 * Unlock the main-level map 3018 */ 3019 vm_map_unlock_read(map); 3020 } 3021 3022 #include "opt_ddb.h" 3023 #ifdef DDB 3024 #include <sys/kernel.h> 3025 3026 #include <ddb/ddb.h> 3027 3028 /* 3029 * vm_map_print: [ debug ] 3030 */ 3031 DB_SHOW_COMMAND(map, vm_map_print) 3032 { 3033 static int nlines; 3034 /* XXX convert args. */ 3035 vm_map_t map = (vm_map_t)addr; 3036 boolean_t full = have_addr; 3037 3038 vm_map_entry_t entry; 3039 3040 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 3041 (void *)map, 3042 (void *)map->pmap, map->nentries, map->timestamp); 3043 nlines++; 3044 3045 if (!full && db_indent) 3046 return; 3047 3048 db_indent += 2; 3049 for (entry = map->header.next; entry != &map->header; 3050 entry = entry->next) { 3051 db_iprintf("map entry %p: start=%p, end=%p\n", 3052 (void *)entry, (void *)entry->start, (void *)entry->end); 3053 nlines++; 3054 { 3055 static char *inheritance_name[4] = 3056 {"share", "copy", "none", "donate_copy"}; 3057 3058 db_iprintf(" prot=%x/%x/%s", 3059 entry->protection, 3060 entry->max_protection, 3061 inheritance_name[(int)(unsigned char)entry->inheritance]); 3062 if (entry->wired_count != 0) 3063 db_printf(", wired"); 3064 } 3065 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3066 db_printf(", share=%p, offset=0x%jx\n", 3067 (void *)entry->object.sub_map, 3068 (uintmax_t)entry->offset); 3069 nlines++; 3070 if ((entry->prev == &map->header) || 3071 (entry->prev->object.sub_map != 3072 entry->object.sub_map)) { 3073 db_indent += 2; 3074 vm_map_print((db_expr_t)(intptr_t) 3075 entry->object.sub_map, 3076 full, 0, (char *)0); 3077 db_indent -= 2; 3078 } 3079 } else { 3080 db_printf(", object=%p, offset=0x%jx", 3081 (void *)entry->object.vm_object, 3082 (uintmax_t)entry->offset); 3083 if (entry->eflags & MAP_ENTRY_COW) 3084 db_printf(", copy (%s)", 3085 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3086 db_printf("\n"); 3087 nlines++; 3088 3089 if ((entry->prev == &map->header) || 3090 (entry->prev->object.vm_object != 3091 entry->object.vm_object)) { 3092 db_indent += 2; 3093 vm_object_print((db_expr_t)(intptr_t) 3094 entry->object.vm_object, 3095 full, 0, (char *)0); 3096 nlines += 4; 3097 db_indent -= 2; 3098 } 3099 } 3100 } 3101 db_indent -= 2; 3102 if (db_indent == 0) 3103 nlines = 0; 3104 } 3105 3106 3107 DB_SHOW_COMMAND(procvm, procvm) 3108 { 3109 struct proc *p; 3110 3111 if (have_addr) { 3112 p = (struct proc *) addr; 3113 } else { 3114 p = curproc; 3115 } 3116 3117 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3118 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3119 (void *)vmspace_pmap(p->p_vmspace)); 3120 3121 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 3122 } 3123 3124 #endif /* DDB */ 3125