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