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