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_offset_t end; 1265 int result; 1266 1267 vm_map_lock(map); 1268 end = start + length; 1269 VM_MAP_RANGE_CHECK(map, start, end); 1270 (void) vm_map_delete(map, start, end); 1271 result = vm_map_insert(map, object, offset, start, end, prot, 1272 max, cow); 1273 vm_map_unlock(map); 1274 return (result); 1275 } 1276 1277 /* 1278 * vm_map_find finds an unallocated region in the target address 1279 * map with the given length. The search is defined to be 1280 * first-fit from the specified address; the region found is 1281 * returned in the same parameter. 1282 * 1283 * If object is non-NULL, ref count must be bumped by caller 1284 * prior to making call to account for the new entry. 1285 */ 1286 int 1287 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1288 vm_offset_t *addr, /* IN/OUT */ 1289 vm_size_t length, int find_space, vm_prot_t prot, 1290 vm_prot_t max, int cow) 1291 { 1292 vm_offset_t start; 1293 int result; 1294 1295 start = *addr; 1296 vm_map_lock(map); 1297 do { 1298 if (find_space != VMFS_NO_SPACE) { 1299 if (vm_map_findspace(map, start, length, addr)) { 1300 vm_map_unlock(map); 1301 return (KERN_NO_SPACE); 1302 } 1303 if (find_space == VMFS_ALIGNED_SPACE) 1304 pmap_align_superpage(object, offset, addr, 1305 length); 1306 start = *addr; 1307 } 1308 result = vm_map_insert(map, object, offset, start, start + 1309 length, prot, max, cow); 1310 } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE); 1311 vm_map_unlock(map); 1312 return (result); 1313 } 1314 1315 /* 1316 * vm_map_simplify_entry: 1317 * 1318 * Simplify the given map entry by merging with either neighbor. This 1319 * routine also has the ability to merge with both neighbors. 1320 * 1321 * The map must be locked. 1322 * 1323 * This routine guarentees that the passed entry remains valid (though 1324 * possibly extended). When merging, this routine may delete one or 1325 * both neighbors. 1326 */ 1327 void 1328 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 1329 { 1330 vm_map_entry_t next, prev; 1331 vm_size_t prevsize, esize; 1332 1333 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) 1334 return; 1335 1336 prev = entry->prev; 1337 if (prev != &map->header) { 1338 prevsize = prev->end - prev->start; 1339 if ( (prev->end == entry->start) && 1340 (prev->object.vm_object == entry->object.vm_object) && 1341 (!prev->object.vm_object || 1342 (prev->offset + prevsize == entry->offset)) && 1343 (prev->eflags == entry->eflags) && 1344 (prev->protection == entry->protection) && 1345 (prev->max_protection == entry->max_protection) && 1346 (prev->inheritance == entry->inheritance) && 1347 (prev->wired_count == entry->wired_count)) { 1348 vm_map_entry_unlink(map, prev); 1349 entry->start = prev->start; 1350 entry->offset = prev->offset; 1351 if (entry->prev != &map->header) 1352 vm_map_entry_resize_free(map, entry->prev); 1353 if (prev->object.vm_object) 1354 vm_object_deallocate(prev->object.vm_object); 1355 vm_map_entry_dispose(map, prev); 1356 } 1357 } 1358 1359 next = entry->next; 1360 if (next != &map->header) { 1361 esize = entry->end - entry->start; 1362 if ((entry->end == next->start) && 1363 (next->object.vm_object == entry->object.vm_object) && 1364 (!entry->object.vm_object || 1365 (entry->offset + esize == next->offset)) && 1366 (next->eflags == entry->eflags) && 1367 (next->protection == entry->protection) && 1368 (next->max_protection == entry->max_protection) && 1369 (next->inheritance == entry->inheritance) && 1370 (next->wired_count == entry->wired_count)) { 1371 vm_map_entry_unlink(map, next); 1372 entry->end = next->end; 1373 vm_map_entry_resize_free(map, entry); 1374 if (next->object.vm_object) 1375 vm_object_deallocate(next->object.vm_object); 1376 vm_map_entry_dispose(map, next); 1377 } 1378 } 1379 } 1380 /* 1381 * vm_map_clip_start: [ internal use only ] 1382 * 1383 * Asserts that the given entry begins at or after 1384 * the specified address; if necessary, 1385 * it splits the entry into two. 1386 */ 1387 #define vm_map_clip_start(map, entry, startaddr) \ 1388 { \ 1389 if (startaddr > entry->start) \ 1390 _vm_map_clip_start(map, entry, startaddr); \ 1391 } 1392 1393 /* 1394 * This routine is called only when it is known that 1395 * the entry must be split. 1396 */ 1397 static void 1398 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1399 { 1400 vm_map_entry_t new_entry; 1401 1402 /* 1403 * Split off the front portion -- note that we must insert the new 1404 * entry BEFORE this one, so that this entry has the specified 1405 * starting address. 1406 */ 1407 vm_map_simplify_entry(map, entry); 1408 1409 /* 1410 * If there is no object backing this entry, we might as well create 1411 * one now. If we defer it, an object can get created after the map 1412 * is clipped, and individual objects will be created for the split-up 1413 * map. This is a bit of a hack, but is also about the best place to 1414 * put this improvement. 1415 */ 1416 if (entry->object.vm_object == NULL && !map->system_map) { 1417 vm_object_t object; 1418 object = vm_object_allocate(OBJT_DEFAULT, 1419 atop(entry->end - entry->start)); 1420 entry->object.vm_object = object; 1421 entry->offset = 0; 1422 } 1423 1424 new_entry = vm_map_entry_create(map); 1425 *new_entry = *entry; 1426 1427 new_entry->end = start; 1428 entry->offset += (start - entry->start); 1429 entry->start = start; 1430 1431 vm_map_entry_link(map, entry->prev, new_entry); 1432 1433 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1434 vm_object_reference(new_entry->object.vm_object); 1435 } 1436 } 1437 1438 /* 1439 * vm_map_clip_end: [ internal use only ] 1440 * 1441 * Asserts that the given entry ends at or before 1442 * the specified address; if necessary, 1443 * it splits the entry into two. 1444 */ 1445 #define vm_map_clip_end(map, entry, endaddr) \ 1446 { \ 1447 if ((endaddr) < (entry->end)) \ 1448 _vm_map_clip_end((map), (entry), (endaddr)); \ 1449 } 1450 1451 /* 1452 * This routine is called only when it is known that 1453 * the entry must be split. 1454 */ 1455 static void 1456 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1457 { 1458 vm_map_entry_t new_entry; 1459 1460 /* 1461 * If there is no object backing this entry, we might as well create 1462 * one now. If we defer it, an object can get created after the map 1463 * is clipped, and individual objects will be created for the split-up 1464 * map. This is a bit of a hack, but is also about the best place to 1465 * put this improvement. 1466 */ 1467 if (entry->object.vm_object == NULL && !map->system_map) { 1468 vm_object_t object; 1469 object = vm_object_allocate(OBJT_DEFAULT, 1470 atop(entry->end - entry->start)); 1471 entry->object.vm_object = object; 1472 entry->offset = 0; 1473 } 1474 1475 /* 1476 * Create a new entry and insert it AFTER the specified entry 1477 */ 1478 new_entry = vm_map_entry_create(map); 1479 *new_entry = *entry; 1480 1481 new_entry->start = entry->end = end; 1482 new_entry->offset += (end - entry->start); 1483 1484 vm_map_entry_link(map, entry, new_entry); 1485 1486 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1487 vm_object_reference(new_entry->object.vm_object); 1488 } 1489 } 1490 1491 /* 1492 * vm_map_submap: [ kernel use only ] 1493 * 1494 * Mark the given range as handled by a subordinate map. 1495 * 1496 * This range must have been created with vm_map_find, 1497 * and no other operations may have been performed on this 1498 * range prior to calling vm_map_submap. 1499 * 1500 * Only a limited number of operations can be performed 1501 * within this rage after calling vm_map_submap: 1502 * vm_fault 1503 * [Don't try vm_map_copy!] 1504 * 1505 * To remove a submapping, one must first remove the 1506 * range from the superior map, and then destroy the 1507 * submap (if desired). [Better yet, don't try it.] 1508 */ 1509 int 1510 vm_map_submap( 1511 vm_map_t map, 1512 vm_offset_t start, 1513 vm_offset_t end, 1514 vm_map_t submap) 1515 { 1516 vm_map_entry_t entry; 1517 int result = KERN_INVALID_ARGUMENT; 1518 1519 vm_map_lock(map); 1520 1521 VM_MAP_RANGE_CHECK(map, start, end); 1522 1523 if (vm_map_lookup_entry(map, start, &entry)) { 1524 vm_map_clip_start(map, entry, start); 1525 } else 1526 entry = entry->next; 1527 1528 vm_map_clip_end(map, entry, end); 1529 1530 if ((entry->start == start) && (entry->end == end) && 1531 ((entry->eflags & MAP_ENTRY_COW) == 0) && 1532 (entry->object.vm_object == NULL)) { 1533 entry->object.sub_map = submap; 1534 entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 1535 result = KERN_SUCCESS; 1536 } 1537 vm_map_unlock(map); 1538 1539 return (result); 1540 } 1541 1542 /* 1543 * The maximum number of pages to map 1544 */ 1545 #define MAX_INIT_PT 96 1546 1547 /* 1548 * vm_map_pmap_enter: 1549 * 1550 * Preload read-only mappings for the given object's resident pages into 1551 * the given map. This eliminates the soft faults on process startup and 1552 * immediately after an mmap(2). Because these are speculative mappings, 1553 * cached pages are not reactivated and mapped. 1554 */ 1555 void 1556 vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 1557 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags) 1558 { 1559 vm_offset_t start; 1560 vm_page_t p, p_start; 1561 vm_pindex_t psize, tmpidx; 1562 boolean_t are_queues_locked; 1563 1564 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL) 1565 return; 1566 VM_OBJECT_LOCK(object); 1567 if (object->type == OBJT_DEVICE) { 1568 pmap_object_init_pt(map->pmap, addr, object, pindex, size); 1569 goto unlock_return; 1570 } 1571 1572 psize = atop(size); 1573 1574 if (object->type != OBJT_VNODE || 1575 ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) && 1576 (object->resident_page_count > MAX_INIT_PT))) { 1577 goto unlock_return; 1578 } 1579 1580 if (psize + pindex > object->size) { 1581 if (object->size < pindex) 1582 goto unlock_return; 1583 psize = object->size - pindex; 1584 } 1585 1586 are_queues_locked = FALSE; 1587 start = 0; 1588 p_start = NULL; 1589 1590 if ((p = TAILQ_FIRST(&object->memq)) != NULL) { 1591 if (p->pindex < pindex) { 1592 p = vm_page_splay(pindex, object->root); 1593 if ((object->root = p)->pindex < pindex) 1594 p = TAILQ_NEXT(p, listq); 1595 } 1596 } 1597 /* 1598 * Assert: the variable p is either (1) the page with the 1599 * least pindex greater than or equal to the parameter pindex 1600 * or (2) NULL. 1601 */ 1602 for (; 1603 p != NULL && (tmpidx = p->pindex - pindex) < psize; 1604 p = TAILQ_NEXT(p, listq)) { 1605 /* 1606 * don't allow an madvise to blow away our really 1607 * free pages allocating pv entries. 1608 */ 1609 if ((flags & MAP_PREFAULT_MADVISE) && 1610 cnt.v_free_count < cnt.v_free_reserved) { 1611 psize = tmpidx; 1612 break; 1613 } 1614 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL && 1615 (p->busy == 0)) { 1616 if (p_start == NULL) { 1617 start = addr + ptoa(tmpidx); 1618 p_start = p; 1619 } 1620 } else if (p_start != NULL) { 1621 if (!are_queues_locked) { 1622 are_queues_locked = TRUE; 1623 vm_page_lock_queues(); 1624 } 1625 pmap_enter_object(map->pmap, start, addr + 1626 ptoa(tmpidx), p_start, prot); 1627 p_start = NULL; 1628 } 1629 } 1630 if (p_start != NULL) { 1631 if (!are_queues_locked) { 1632 are_queues_locked = TRUE; 1633 vm_page_lock_queues(); 1634 } 1635 pmap_enter_object(map->pmap, start, addr + ptoa(psize), 1636 p_start, prot); 1637 } 1638 if (are_queues_locked) 1639 vm_page_unlock_queues(); 1640 unlock_return: 1641 VM_OBJECT_UNLOCK(object); 1642 } 1643 1644 /* 1645 * vm_map_protect: 1646 * 1647 * Sets the protection of the specified address 1648 * region in the target map. If "set_max" is 1649 * specified, the maximum protection is to be set; 1650 * otherwise, only the current protection is affected. 1651 */ 1652 int 1653 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1654 vm_prot_t new_prot, boolean_t set_max) 1655 { 1656 vm_map_entry_t current; 1657 vm_map_entry_t entry; 1658 1659 vm_map_lock(map); 1660 1661 VM_MAP_RANGE_CHECK(map, start, end); 1662 1663 if (vm_map_lookup_entry(map, start, &entry)) { 1664 vm_map_clip_start(map, entry, start); 1665 } else { 1666 entry = entry->next; 1667 } 1668 1669 /* 1670 * Make a first pass to check for protection violations. 1671 */ 1672 current = entry; 1673 while ((current != &map->header) && (current->start < end)) { 1674 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1675 vm_map_unlock(map); 1676 return (KERN_INVALID_ARGUMENT); 1677 } 1678 if ((new_prot & current->max_protection) != new_prot) { 1679 vm_map_unlock(map); 1680 return (KERN_PROTECTION_FAILURE); 1681 } 1682 current = current->next; 1683 } 1684 1685 /* 1686 * Go back and fix up protections. [Note that clipping is not 1687 * necessary the second time.] 1688 */ 1689 current = entry; 1690 while ((current != &map->header) && (current->start < end)) { 1691 vm_prot_t old_prot; 1692 1693 vm_map_clip_end(map, current, end); 1694 1695 old_prot = current->protection; 1696 if (set_max) 1697 current->protection = 1698 (current->max_protection = new_prot) & 1699 old_prot; 1700 else 1701 current->protection = new_prot; 1702 1703 /* 1704 * Update physical map if necessary. Worry about copy-on-write 1705 * here. 1706 */ 1707 if (current->protection != old_prot) { 1708 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1709 VM_PROT_ALL) 1710 pmap_protect(map->pmap, current->start, 1711 current->end, 1712 current->protection & MASK(current)); 1713 #undef MASK 1714 } 1715 vm_map_simplify_entry(map, current); 1716 current = current->next; 1717 } 1718 vm_map_unlock(map); 1719 return (KERN_SUCCESS); 1720 } 1721 1722 /* 1723 * vm_map_madvise: 1724 * 1725 * This routine traverses a processes map handling the madvise 1726 * system call. Advisories are classified as either those effecting 1727 * the vm_map_entry structure, or those effecting the underlying 1728 * objects. 1729 */ 1730 int 1731 vm_map_madvise( 1732 vm_map_t map, 1733 vm_offset_t start, 1734 vm_offset_t end, 1735 int behav) 1736 { 1737 vm_map_entry_t current, entry; 1738 int modify_map = 0; 1739 1740 /* 1741 * Some madvise calls directly modify the vm_map_entry, in which case 1742 * we need to use an exclusive lock on the map and we need to perform 1743 * various clipping operations. Otherwise we only need a read-lock 1744 * on the map. 1745 */ 1746 switch(behav) { 1747 case MADV_NORMAL: 1748 case MADV_SEQUENTIAL: 1749 case MADV_RANDOM: 1750 case MADV_NOSYNC: 1751 case MADV_AUTOSYNC: 1752 case MADV_NOCORE: 1753 case MADV_CORE: 1754 modify_map = 1; 1755 vm_map_lock(map); 1756 break; 1757 case MADV_WILLNEED: 1758 case MADV_DONTNEED: 1759 case MADV_FREE: 1760 vm_map_lock_read(map); 1761 break; 1762 default: 1763 return (KERN_INVALID_ARGUMENT); 1764 } 1765 1766 /* 1767 * Locate starting entry and clip if necessary. 1768 */ 1769 VM_MAP_RANGE_CHECK(map, start, end); 1770 1771 if (vm_map_lookup_entry(map, start, &entry)) { 1772 if (modify_map) 1773 vm_map_clip_start(map, entry, start); 1774 } else { 1775 entry = entry->next; 1776 } 1777 1778 if (modify_map) { 1779 /* 1780 * madvise behaviors that are implemented in the vm_map_entry. 1781 * 1782 * We clip the vm_map_entry so that behavioral changes are 1783 * limited to the specified address range. 1784 */ 1785 for (current = entry; 1786 (current != &map->header) && (current->start < end); 1787 current = current->next 1788 ) { 1789 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1790 continue; 1791 1792 vm_map_clip_end(map, current, end); 1793 1794 switch (behav) { 1795 case MADV_NORMAL: 1796 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1797 break; 1798 case MADV_SEQUENTIAL: 1799 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1800 break; 1801 case MADV_RANDOM: 1802 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1803 break; 1804 case MADV_NOSYNC: 1805 current->eflags |= MAP_ENTRY_NOSYNC; 1806 break; 1807 case MADV_AUTOSYNC: 1808 current->eflags &= ~MAP_ENTRY_NOSYNC; 1809 break; 1810 case MADV_NOCORE: 1811 current->eflags |= MAP_ENTRY_NOCOREDUMP; 1812 break; 1813 case MADV_CORE: 1814 current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 1815 break; 1816 default: 1817 break; 1818 } 1819 vm_map_simplify_entry(map, current); 1820 } 1821 vm_map_unlock(map); 1822 } else { 1823 vm_pindex_t pindex; 1824 int count; 1825 1826 /* 1827 * madvise behaviors that are implemented in the underlying 1828 * vm_object. 1829 * 1830 * Since we don't clip the vm_map_entry, we have to clip 1831 * the vm_object pindex and count. 1832 */ 1833 for (current = entry; 1834 (current != &map->header) && (current->start < end); 1835 current = current->next 1836 ) { 1837 vm_offset_t useStart; 1838 1839 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1840 continue; 1841 1842 pindex = OFF_TO_IDX(current->offset); 1843 count = atop(current->end - current->start); 1844 useStart = current->start; 1845 1846 if (current->start < start) { 1847 pindex += atop(start - current->start); 1848 count -= atop(start - current->start); 1849 useStart = start; 1850 } 1851 if (current->end > end) 1852 count -= atop(current->end - end); 1853 1854 if (count <= 0) 1855 continue; 1856 1857 vm_object_madvise(current->object.vm_object, 1858 pindex, count, behav); 1859 if (behav == MADV_WILLNEED) { 1860 vm_map_pmap_enter(map, 1861 useStart, 1862 current->protection, 1863 current->object.vm_object, 1864 pindex, 1865 (count << PAGE_SHIFT), 1866 MAP_PREFAULT_MADVISE 1867 ); 1868 } 1869 } 1870 vm_map_unlock_read(map); 1871 } 1872 return (0); 1873 } 1874 1875 1876 /* 1877 * vm_map_inherit: 1878 * 1879 * Sets the inheritance of the specified address 1880 * range in the target map. Inheritance 1881 * affects how the map will be shared with 1882 * child maps at the time of vmspace_fork. 1883 */ 1884 int 1885 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1886 vm_inherit_t new_inheritance) 1887 { 1888 vm_map_entry_t entry; 1889 vm_map_entry_t temp_entry; 1890 1891 switch (new_inheritance) { 1892 case VM_INHERIT_NONE: 1893 case VM_INHERIT_COPY: 1894 case VM_INHERIT_SHARE: 1895 break; 1896 default: 1897 return (KERN_INVALID_ARGUMENT); 1898 } 1899 vm_map_lock(map); 1900 VM_MAP_RANGE_CHECK(map, start, end); 1901 if (vm_map_lookup_entry(map, start, &temp_entry)) { 1902 entry = temp_entry; 1903 vm_map_clip_start(map, entry, start); 1904 } else 1905 entry = temp_entry->next; 1906 while ((entry != &map->header) && (entry->start < end)) { 1907 vm_map_clip_end(map, entry, end); 1908 entry->inheritance = new_inheritance; 1909 vm_map_simplify_entry(map, entry); 1910 entry = entry->next; 1911 } 1912 vm_map_unlock(map); 1913 return (KERN_SUCCESS); 1914 } 1915 1916 /* 1917 * vm_map_unwire: 1918 * 1919 * Implements both kernel and user unwiring. 1920 */ 1921 int 1922 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1923 int flags) 1924 { 1925 vm_map_entry_t entry, first_entry, tmp_entry; 1926 vm_offset_t saved_start; 1927 unsigned int last_timestamp; 1928 int rv; 1929 boolean_t need_wakeup, result, user_unwire; 1930 1931 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 1932 vm_map_lock(map); 1933 VM_MAP_RANGE_CHECK(map, start, end); 1934 if (!vm_map_lookup_entry(map, start, &first_entry)) { 1935 if (flags & VM_MAP_WIRE_HOLESOK) 1936 first_entry = first_entry->next; 1937 else { 1938 vm_map_unlock(map); 1939 return (KERN_INVALID_ADDRESS); 1940 } 1941 } 1942 last_timestamp = map->timestamp; 1943 entry = first_entry; 1944 while (entry != &map->header && entry->start < end) { 1945 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 1946 /* 1947 * We have not yet clipped the entry. 1948 */ 1949 saved_start = (start >= entry->start) ? start : 1950 entry->start; 1951 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 1952 if (vm_map_unlock_and_wait(map, 0)) { 1953 /* 1954 * Allow interruption of user unwiring? 1955 */ 1956 } 1957 vm_map_lock(map); 1958 if (last_timestamp+1 != map->timestamp) { 1959 /* 1960 * Look again for the entry because the map was 1961 * modified while it was unlocked. 1962 * Specifically, the entry may have been 1963 * clipped, merged, or deleted. 1964 */ 1965 if (!vm_map_lookup_entry(map, saved_start, 1966 &tmp_entry)) { 1967 if (flags & VM_MAP_WIRE_HOLESOK) 1968 tmp_entry = tmp_entry->next; 1969 else { 1970 if (saved_start == start) { 1971 /* 1972 * First_entry has been deleted. 1973 */ 1974 vm_map_unlock(map); 1975 return (KERN_INVALID_ADDRESS); 1976 } 1977 end = saved_start; 1978 rv = KERN_INVALID_ADDRESS; 1979 goto done; 1980 } 1981 } 1982 if (entry == first_entry) 1983 first_entry = tmp_entry; 1984 else 1985 first_entry = NULL; 1986 entry = tmp_entry; 1987 } 1988 last_timestamp = map->timestamp; 1989 continue; 1990 } 1991 vm_map_clip_start(map, entry, start); 1992 vm_map_clip_end(map, entry, end); 1993 /* 1994 * Mark the entry in case the map lock is released. (See 1995 * above.) 1996 */ 1997 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 1998 /* 1999 * Check the map for holes in the specified region. 2000 * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 2001 */ 2002 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2003 (entry->end < end && (entry->next == &map->header || 2004 entry->next->start > entry->end))) { 2005 end = entry->end; 2006 rv = KERN_INVALID_ADDRESS; 2007 goto done; 2008 } 2009 /* 2010 * If system unwiring, require that the entry is system wired. 2011 */ 2012 if (!user_unwire && 2013 vm_map_entry_system_wired_count(entry) == 0) { 2014 end = entry->end; 2015 rv = KERN_INVALID_ARGUMENT; 2016 goto done; 2017 } 2018 entry = entry->next; 2019 } 2020 rv = KERN_SUCCESS; 2021 done: 2022 need_wakeup = FALSE; 2023 if (first_entry == NULL) { 2024 result = vm_map_lookup_entry(map, start, &first_entry); 2025 if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2026 first_entry = first_entry->next; 2027 else 2028 KASSERT(result, ("vm_map_unwire: lookup failed")); 2029 } 2030 entry = first_entry; 2031 while (entry != &map->header && entry->start < end) { 2032 if (rv == KERN_SUCCESS && (!user_unwire || 2033 (entry->eflags & MAP_ENTRY_USER_WIRED))) { 2034 if (user_unwire) 2035 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2036 entry->wired_count--; 2037 if (entry->wired_count == 0) { 2038 /* 2039 * Retain the map lock. 2040 */ 2041 vm_fault_unwire(map, entry->start, entry->end, 2042 entry->object.vm_object != NULL && 2043 entry->object.vm_object->type == OBJT_DEVICE); 2044 } 2045 } 2046 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 2047 ("vm_map_unwire: in-transition flag missing")); 2048 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 2049 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 2050 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 2051 need_wakeup = TRUE; 2052 } 2053 vm_map_simplify_entry(map, entry); 2054 entry = entry->next; 2055 } 2056 vm_map_unlock(map); 2057 if (need_wakeup) 2058 vm_map_wakeup(map); 2059 return (rv); 2060 } 2061 2062 /* 2063 * vm_map_wire: 2064 * 2065 * Implements both kernel and user wiring. 2066 */ 2067 int 2068 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 2069 int flags) 2070 { 2071 vm_map_entry_t entry, first_entry, tmp_entry; 2072 vm_offset_t saved_end, saved_start; 2073 unsigned int last_timestamp; 2074 int rv; 2075 boolean_t fictitious, need_wakeup, result, user_wire; 2076 2077 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 2078 vm_map_lock(map); 2079 VM_MAP_RANGE_CHECK(map, start, end); 2080 if (!vm_map_lookup_entry(map, start, &first_entry)) { 2081 if (flags & VM_MAP_WIRE_HOLESOK) 2082 first_entry = first_entry->next; 2083 else { 2084 vm_map_unlock(map); 2085 return (KERN_INVALID_ADDRESS); 2086 } 2087 } 2088 last_timestamp = map->timestamp; 2089 entry = first_entry; 2090 while (entry != &map->header && entry->start < end) { 2091 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 2092 /* 2093 * We have not yet clipped the entry. 2094 */ 2095 saved_start = (start >= entry->start) ? start : 2096 entry->start; 2097 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2098 if (vm_map_unlock_and_wait(map, 0)) { 2099 /* 2100 * Allow interruption of user wiring? 2101 */ 2102 } 2103 vm_map_lock(map); 2104 if (last_timestamp + 1 != map->timestamp) { 2105 /* 2106 * Look again for the entry because the map was 2107 * modified while it was unlocked. 2108 * Specifically, the entry may have been 2109 * clipped, merged, or deleted. 2110 */ 2111 if (!vm_map_lookup_entry(map, saved_start, 2112 &tmp_entry)) { 2113 if (flags & VM_MAP_WIRE_HOLESOK) 2114 tmp_entry = tmp_entry->next; 2115 else { 2116 if (saved_start == start) { 2117 /* 2118 * first_entry has been deleted. 2119 */ 2120 vm_map_unlock(map); 2121 return (KERN_INVALID_ADDRESS); 2122 } 2123 end = saved_start; 2124 rv = KERN_INVALID_ADDRESS; 2125 goto done; 2126 } 2127 } 2128 if (entry == first_entry) 2129 first_entry = tmp_entry; 2130 else 2131 first_entry = NULL; 2132 entry = tmp_entry; 2133 } 2134 last_timestamp = map->timestamp; 2135 continue; 2136 } 2137 vm_map_clip_start(map, entry, start); 2138 vm_map_clip_end(map, entry, end); 2139 /* 2140 * Mark the entry in case the map lock is released. (See 2141 * above.) 2142 */ 2143 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 2144 /* 2145 * 2146 */ 2147 if (entry->wired_count == 0) { 2148 entry->wired_count++; 2149 saved_start = entry->start; 2150 saved_end = entry->end; 2151 fictitious = entry->object.vm_object != NULL && 2152 entry->object.vm_object->type == OBJT_DEVICE; 2153 /* 2154 * Release the map lock, relying on the in-transition 2155 * mark. 2156 */ 2157 vm_map_unlock(map); 2158 rv = vm_fault_wire(map, saved_start, saved_end, 2159 user_wire, fictitious); 2160 vm_map_lock(map); 2161 if (last_timestamp + 1 != map->timestamp) { 2162 /* 2163 * Look again for the entry because the map was 2164 * modified while it was unlocked. The entry 2165 * may have been clipped, but NOT merged or 2166 * deleted. 2167 */ 2168 result = vm_map_lookup_entry(map, saved_start, 2169 &tmp_entry); 2170 KASSERT(result, ("vm_map_wire: lookup failed")); 2171 if (entry == first_entry) 2172 first_entry = tmp_entry; 2173 else 2174 first_entry = NULL; 2175 entry = tmp_entry; 2176 while (entry->end < saved_end) { 2177 if (rv != KERN_SUCCESS) { 2178 KASSERT(entry->wired_count == 1, 2179 ("vm_map_wire: bad count")); 2180 entry->wired_count = -1; 2181 } 2182 entry = entry->next; 2183 } 2184 } 2185 last_timestamp = map->timestamp; 2186 if (rv != KERN_SUCCESS) { 2187 KASSERT(entry->wired_count == 1, 2188 ("vm_map_wire: bad count")); 2189 /* 2190 * Assign an out-of-range value to represent 2191 * the failure to wire this entry. 2192 */ 2193 entry->wired_count = -1; 2194 end = entry->end; 2195 goto done; 2196 } 2197 } else if (!user_wire || 2198 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 2199 entry->wired_count++; 2200 } 2201 /* 2202 * Check the map for holes in the specified region. 2203 * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 2204 */ 2205 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2206 (entry->end < end && (entry->next == &map->header || 2207 entry->next->start > entry->end))) { 2208 end = entry->end; 2209 rv = KERN_INVALID_ADDRESS; 2210 goto done; 2211 } 2212 entry = entry->next; 2213 } 2214 rv = KERN_SUCCESS; 2215 done: 2216 need_wakeup = FALSE; 2217 if (first_entry == NULL) { 2218 result = vm_map_lookup_entry(map, start, &first_entry); 2219 if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2220 first_entry = first_entry->next; 2221 else 2222 KASSERT(result, ("vm_map_wire: lookup failed")); 2223 } 2224 entry = first_entry; 2225 while (entry != &map->header && entry->start < end) { 2226 if (rv == KERN_SUCCESS) { 2227 if (user_wire) 2228 entry->eflags |= MAP_ENTRY_USER_WIRED; 2229 } else if (entry->wired_count == -1) { 2230 /* 2231 * Wiring failed on this entry. Thus, unwiring is 2232 * unnecessary. 2233 */ 2234 entry->wired_count = 0; 2235 } else { 2236 if (!user_wire || 2237 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) 2238 entry->wired_count--; 2239 if (entry->wired_count == 0) { 2240 /* 2241 * Retain the map lock. 2242 */ 2243 vm_fault_unwire(map, entry->start, entry->end, 2244 entry->object.vm_object != NULL && 2245 entry->object.vm_object->type == OBJT_DEVICE); 2246 } 2247 } 2248 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 2249 ("vm_map_wire: in-transition flag missing")); 2250 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 2251 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 2252 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 2253 need_wakeup = TRUE; 2254 } 2255 vm_map_simplify_entry(map, entry); 2256 entry = entry->next; 2257 } 2258 vm_map_unlock(map); 2259 if (need_wakeup) 2260 vm_map_wakeup(map); 2261 return (rv); 2262 } 2263 2264 /* 2265 * vm_map_sync 2266 * 2267 * Push any dirty cached pages in the address range to their pager. 2268 * If syncio is TRUE, dirty pages are written synchronously. 2269 * If invalidate is TRUE, any cached pages are freed as well. 2270 * 2271 * If the size of the region from start to end is zero, we are 2272 * supposed to flush all modified pages within the region containing 2273 * start. Unfortunately, a region can be split or coalesced with 2274 * neighboring regions, making it difficult to determine what the 2275 * original region was. Therefore, we approximate this requirement by 2276 * flushing the current region containing start. 2277 * 2278 * Returns an error if any part of the specified range is not mapped. 2279 */ 2280 int 2281 vm_map_sync( 2282 vm_map_t map, 2283 vm_offset_t start, 2284 vm_offset_t end, 2285 boolean_t syncio, 2286 boolean_t invalidate) 2287 { 2288 vm_map_entry_t current; 2289 vm_map_entry_t entry; 2290 vm_size_t size; 2291 vm_object_t object; 2292 vm_ooffset_t offset; 2293 2294 vm_map_lock_read(map); 2295 VM_MAP_RANGE_CHECK(map, start, end); 2296 if (!vm_map_lookup_entry(map, start, &entry)) { 2297 vm_map_unlock_read(map); 2298 return (KERN_INVALID_ADDRESS); 2299 } else if (start == end) { 2300 start = entry->start; 2301 end = entry->end; 2302 } 2303 /* 2304 * Make a first pass to check for user-wired memory and holes. 2305 */ 2306 for (current = entry; current != &map->header && current->start < end; 2307 current = current->next) { 2308 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) { 2309 vm_map_unlock_read(map); 2310 return (KERN_INVALID_ARGUMENT); 2311 } 2312 if (end > current->end && 2313 (current->next == &map->header || 2314 current->end != current->next->start)) { 2315 vm_map_unlock_read(map); 2316 return (KERN_INVALID_ADDRESS); 2317 } 2318 } 2319 2320 if (invalidate) 2321 pmap_remove(map->pmap, start, end); 2322 2323 /* 2324 * Make a second pass, cleaning/uncaching pages from the indicated 2325 * objects as we go. 2326 */ 2327 for (current = entry; current != &map->header && current->start < end; 2328 current = current->next) { 2329 offset = current->offset + (start - current->start); 2330 size = (end <= current->end ? end : current->end) - start; 2331 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2332 vm_map_t smap; 2333 vm_map_entry_t tentry; 2334 vm_size_t tsize; 2335 2336 smap = current->object.sub_map; 2337 vm_map_lock_read(smap); 2338 (void) vm_map_lookup_entry(smap, offset, &tentry); 2339 tsize = tentry->end - offset; 2340 if (tsize < size) 2341 size = tsize; 2342 object = tentry->object.vm_object; 2343 offset = tentry->offset + (offset - tentry->start); 2344 vm_map_unlock_read(smap); 2345 } else { 2346 object = current->object.vm_object; 2347 } 2348 vm_object_sync(object, offset, size, syncio, invalidate); 2349 start += size; 2350 } 2351 2352 vm_map_unlock_read(map); 2353 return (KERN_SUCCESS); 2354 } 2355 2356 /* 2357 * vm_map_entry_unwire: [ internal use only ] 2358 * 2359 * Make the region specified by this entry pageable. 2360 * 2361 * The map in question should be locked. 2362 * [This is the reason for this routine's existence.] 2363 */ 2364 static void 2365 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2366 { 2367 vm_fault_unwire(map, entry->start, entry->end, 2368 entry->object.vm_object != NULL && 2369 entry->object.vm_object->type == OBJT_DEVICE); 2370 entry->wired_count = 0; 2371 } 2372 2373 /* 2374 * vm_map_entry_delete: [ internal use only ] 2375 * 2376 * Deallocate the given entry from the target map. 2377 */ 2378 static void 2379 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2380 { 2381 vm_object_t object; 2382 vm_pindex_t offidxstart, offidxend, count; 2383 2384 vm_map_entry_unlink(map, entry); 2385 map->size -= entry->end - entry->start; 2386 2387 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 2388 (object = entry->object.vm_object) != NULL) { 2389 count = OFF_TO_IDX(entry->end - entry->start); 2390 offidxstart = OFF_TO_IDX(entry->offset); 2391 offidxend = offidxstart + count; 2392 VM_OBJECT_LOCK(object); 2393 if (object->ref_count != 1 && 2394 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING || 2395 object == kernel_object || object == kmem_object)) { 2396 vm_object_collapse(object); 2397 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 2398 if (object->type == OBJT_SWAP) 2399 swap_pager_freespace(object, offidxstart, count); 2400 if (offidxend >= object->size && 2401 offidxstart < object->size) 2402 object->size = offidxstart; 2403 } 2404 VM_OBJECT_UNLOCK(object); 2405 vm_object_deallocate(object); 2406 } 2407 2408 vm_map_entry_dispose(map, entry); 2409 } 2410 2411 /* 2412 * vm_map_delete: [ internal use only ] 2413 * 2414 * Deallocates the given address range from the target 2415 * map. 2416 */ 2417 int 2418 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 2419 { 2420 vm_map_entry_t entry; 2421 vm_map_entry_t first_entry; 2422 2423 /* 2424 * Find the start of the region, and clip it 2425 */ 2426 if (!vm_map_lookup_entry(map, start, &first_entry)) 2427 entry = first_entry->next; 2428 else { 2429 entry = first_entry; 2430 vm_map_clip_start(map, entry, start); 2431 } 2432 2433 /* 2434 * Step through all entries in this region 2435 */ 2436 while ((entry != &map->header) && (entry->start < end)) { 2437 vm_map_entry_t next; 2438 2439 /* 2440 * Wait for wiring or unwiring of an entry to complete. 2441 * Also wait for any system wirings to disappear on 2442 * user maps. 2443 */ 2444 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 || 2445 (vm_map_pmap(map) != kernel_pmap && 2446 vm_map_entry_system_wired_count(entry) != 0)) { 2447 unsigned int last_timestamp; 2448 vm_offset_t saved_start; 2449 vm_map_entry_t tmp_entry; 2450 2451 saved_start = entry->start; 2452 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2453 last_timestamp = map->timestamp; 2454 (void) vm_map_unlock_and_wait(map, 0); 2455 vm_map_lock(map); 2456 if (last_timestamp + 1 != map->timestamp) { 2457 /* 2458 * Look again for the entry because the map was 2459 * modified while it was unlocked. 2460 * Specifically, the entry may have been 2461 * clipped, merged, or deleted. 2462 */ 2463 if (!vm_map_lookup_entry(map, saved_start, 2464 &tmp_entry)) 2465 entry = tmp_entry->next; 2466 else { 2467 entry = tmp_entry; 2468 vm_map_clip_start(map, entry, 2469 saved_start); 2470 } 2471 } 2472 continue; 2473 } 2474 vm_map_clip_end(map, entry, end); 2475 2476 next = entry->next; 2477 2478 /* 2479 * Unwire before removing addresses from the pmap; otherwise, 2480 * unwiring will put the entries back in the pmap. 2481 */ 2482 if (entry->wired_count != 0) { 2483 vm_map_entry_unwire(map, entry); 2484 } 2485 2486 pmap_remove(map->pmap, entry->start, entry->end); 2487 2488 /* 2489 * Delete the entry (which may delete the object) only after 2490 * removing all pmap entries pointing to its pages. 2491 * (Otherwise, its page frames may be reallocated, and any 2492 * modify bits will be set in the wrong object!) 2493 */ 2494 vm_map_entry_delete(map, entry); 2495 entry = next; 2496 } 2497 return (KERN_SUCCESS); 2498 } 2499 2500 /* 2501 * vm_map_remove: 2502 * 2503 * Remove the given address range from the target map. 2504 * This is the exported form of vm_map_delete. 2505 */ 2506 int 2507 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 2508 { 2509 int result; 2510 2511 vm_map_lock(map); 2512 VM_MAP_RANGE_CHECK(map, start, end); 2513 result = vm_map_delete(map, start, end); 2514 vm_map_unlock(map); 2515 return (result); 2516 } 2517 2518 /* 2519 * vm_map_check_protection: 2520 * 2521 * Assert that the target map allows the specified privilege on the 2522 * entire address region given. The entire region must be allocated. 2523 * 2524 * WARNING! This code does not and should not check whether the 2525 * contents of the region is accessible. For example a smaller file 2526 * might be mapped into a larger address space. 2527 * 2528 * NOTE! This code is also called by munmap(). 2529 * 2530 * The map must be locked. A read lock is sufficient. 2531 */ 2532 boolean_t 2533 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 2534 vm_prot_t protection) 2535 { 2536 vm_map_entry_t entry; 2537 vm_map_entry_t tmp_entry; 2538 2539 if (!vm_map_lookup_entry(map, start, &tmp_entry)) 2540 return (FALSE); 2541 entry = tmp_entry; 2542 2543 while (start < end) { 2544 if (entry == &map->header) 2545 return (FALSE); 2546 /* 2547 * No holes allowed! 2548 */ 2549 if (start < entry->start) 2550 return (FALSE); 2551 /* 2552 * Check protection associated with entry. 2553 */ 2554 if ((entry->protection & protection) != protection) 2555 return (FALSE); 2556 /* go to next entry */ 2557 start = entry->end; 2558 entry = entry->next; 2559 } 2560 return (TRUE); 2561 } 2562 2563 /* 2564 * vm_map_copy_entry: 2565 * 2566 * Copies the contents of the source entry to the destination 2567 * entry. The entries *must* be aligned properly. 2568 */ 2569 static void 2570 vm_map_copy_entry( 2571 vm_map_t src_map, 2572 vm_map_t dst_map, 2573 vm_map_entry_t src_entry, 2574 vm_map_entry_t dst_entry) 2575 { 2576 vm_object_t src_object; 2577 2578 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2579 return; 2580 2581 if (src_entry->wired_count == 0) { 2582 2583 /* 2584 * If the source entry is marked needs_copy, it is already 2585 * write-protected. 2586 */ 2587 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2588 pmap_protect(src_map->pmap, 2589 src_entry->start, 2590 src_entry->end, 2591 src_entry->protection & ~VM_PROT_WRITE); 2592 } 2593 2594 /* 2595 * Make a copy of the object. 2596 */ 2597 if ((src_object = src_entry->object.vm_object) != NULL) { 2598 VM_OBJECT_LOCK(src_object); 2599 if ((src_object->handle == NULL) && 2600 (src_object->type == OBJT_DEFAULT || 2601 src_object->type == OBJT_SWAP)) { 2602 vm_object_collapse(src_object); 2603 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2604 vm_object_split(src_entry); 2605 src_object = src_entry->object.vm_object; 2606 } 2607 } 2608 vm_object_reference_locked(src_object); 2609 vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2610 VM_OBJECT_UNLOCK(src_object); 2611 dst_entry->object.vm_object = src_object; 2612 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2613 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2614 dst_entry->offset = src_entry->offset; 2615 } else { 2616 dst_entry->object.vm_object = NULL; 2617 dst_entry->offset = 0; 2618 } 2619 2620 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2621 dst_entry->end - dst_entry->start, src_entry->start); 2622 } else { 2623 /* 2624 * Of course, wired down pages can't be set copy-on-write. 2625 * Cause wired pages to be copied into the new map by 2626 * simulating faults (the new pages are pageable) 2627 */ 2628 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2629 } 2630 } 2631 2632 /* 2633 * vmspace_map_entry_forked: 2634 * Update the newly-forked vmspace each time a map entry is inherited 2635 * or copied. The values for vm_dsize and vm_tsize are approximate 2636 * (and mostly-obsolete ideas in the face of mmap(2) et al.) 2637 */ 2638 static void 2639 vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2, 2640 vm_map_entry_t entry) 2641 { 2642 vm_size_t entrysize; 2643 vm_offset_t newend; 2644 2645 entrysize = entry->end - entry->start; 2646 vm2->vm_map.size += entrysize; 2647 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) { 2648 vm2->vm_ssize += btoc(entrysize); 2649 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr && 2650 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) { 2651 newend = MIN(entry->end, 2652 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)); 2653 vm2->vm_dsize += btoc(newend - entry->start); 2654 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr && 2655 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) { 2656 newend = MIN(entry->end, 2657 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)); 2658 vm2->vm_tsize += btoc(newend - entry->start); 2659 } 2660 } 2661 2662 /* 2663 * vmspace_fork: 2664 * Create a new process vmspace structure and vm_map 2665 * based on those of an existing process. The new map 2666 * is based on the old map, according to the inheritance 2667 * values on the regions in that map. 2668 * 2669 * XXX It might be worth coalescing the entries added to the new vmspace. 2670 * 2671 * The source map must not be locked. 2672 */ 2673 struct vmspace * 2674 vmspace_fork(struct vmspace *vm1) 2675 { 2676 struct vmspace *vm2; 2677 vm_map_t old_map = &vm1->vm_map; 2678 vm_map_t new_map; 2679 vm_map_entry_t old_entry; 2680 vm_map_entry_t new_entry; 2681 vm_object_t object; 2682 2683 vm_map_lock(old_map); 2684 2685 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2686 if (vm2 == NULL) 2687 goto unlock_and_return; 2688 vm2->vm_taddr = vm1->vm_taddr; 2689 vm2->vm_daddr = vm1->vm_daddr; 2690 vm2->vm_maxsaddr = vm1->vm_maxsaddr; 2691 new_map = &vm2->vm_map; /* XXX */ 2692 new_map->timestamp = 1; 2693 2694 old_entry = old_map->header.next; 2695 2696 while (old_entry != &old_map->header) { 2697 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2698 panic("vm_map_fork: encountered a submap"); 2699 2700 switch (old_entry->inheritance) { 2701 case VM_INHERIT_NONE: 2702 break; 2703 2704 case VM_INHERIT_SHARE: 2705 /* 2706 * Clone the entry, creating the shared object if necessary. 2707 */ 2708 object = old_entry->object.vm_object; 2709 if (object == NULL) { 2710 object = vm_object_allocate(OBJT_DEFAULT, 2711 atop(old_entry->end - old_entry->start)); 2712 old_entry->object.vm_object = object; 2713 old_entry->offset = 0; 2714 } 2715 2716 /* 2717 * Add the reference before calling vm_object_shadow 2718 * to insure that a shadow object is created. 2719 */ 2720 vm_object_reference(object); 2721 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2722 vm_object_shadow(&old_entry->object.vm_object, 2723 &old_entry->offset, 2724 atop(old_entry->end - old_entry->start)); 2725 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2726 /* Transfer the second reference too. */ 2727 vm_object_reference( 2728 old_entry->object.vm_object); 2729 vm_object_deallocate(object); 2730 object = old_entry->object.vm_object; 2731 } 2732 VM_OBJECT_LOCK(object); 2733 vm_object_clear_flag(object, OBJ_ONEMAPPING); 2734 VM_OBJECT_UNLOCK(object); 2735 2736 /* 2737 * Clone the entry, referencing the shared object. 2738 */ 2739 new_entry = vm_map_entry_create(new_map); 2740 *new_entry = *old_entry; 2741 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2742 new_entry->wired_count = 0; 2743 2744 /* 2745 * Insert the entry into the new map -- we know we're 2746 * inserting at the end of the new map. 2747 */ 2748 vm_map_entry_link(new_map, new_map->header.prev, 2749 new_entry); 2750 vmspace_map_entry_forked(vm1, vm2, new_entry); 2751 2752 /* 2753 * Update the physical map 2754 */ 2755 pmap_copy(new_map->pmap, old_map->pmap, 2756 new_entry->start, 2757 (old_entry->end - old_entry->start), 2758 old_entry->start); 2759 break; 2760 2761 case VM_INHERIT_COPY: 2762 /* 2763 * Clone the entry and link into the map. 2764 */ 2765 new_entry = vm_map_entry_create(new_map); 2766 *new_entry = *old_entry; 2767 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2768 new_entry->wired_count = 0; 2769 new_entry->object.vm_object = NULL; 2770 vm_map_entry_link(new_map, new_map->header.prev, 2771 new_entry); 2772 vmspace_map_entry_forked(vm1, vm2, new_entry); 2773 vm_map_copy_entry(old_map, new_map, old_entry, 2774 new_entry); 2775 break; 2776 } 2777 old_entry = old_entry->next; 2778 } 2779 unlock_and_return: 2780 vm_map_unlock(old_map); 2781 2782 return (vm2); 2783 } 2784 2785 int 2786 vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 2787 vm_prot_t prot, vm_prot_t max, int cow) 2788 { 2789 vm_map_entry_t new_entry, prev_entry; 2790 vm_offset_t bot, top; 2791 vm_size_t init_ssize; 2792 int orient, rv; 2793 rlim_t vmemlim; 2794 2795 /* 2796 * The stack orientation is piggybacked with the cow argument. 2797 * Extract it into orient and mask the cow argument so that we 2798 * don't pass it around further. 2799 * NOTE: We explicitly allow bi-directional stacks. 2800 */ 2801 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP); 2802 cow &= ~orient; 2803 KASSERT(orient != 0, ("No stack grow direction")); 2804 2805 if (addrbos < vm_map_min(map) || 2806 addrbos > vm_map_max(map) || 2807 addrbos + max_ssize < addrbos) 2808 return (KERN_NO_SPACE); 2809 2810 init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz; 2811 2812 PROC_LOCK(curthread->td_proc); 2813 vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM); 2814 PROC_UNLOCK(curthread->td_proc); 2815 2816 vm_map_lock(map); 2817 2818 /* If addr is already mapped, no go */ 2819 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 2820 vm_map_unlock(map); 2821 return (KERN_NO_SPACE); 2822 } 2823 2824 /* If we would blow our VMEM resource limit, no go */ 2825 if (map->size + init_ssize > vmemlim) { 2826 vm_map_unlock(map); 2827 return (KERN_NO_SPACE); 2828 } 2829 2830 /* 2831 * If we can't accomodate max_ssize in the current mapping, no go. 2832 * However, we need to be aware that subsequent user mappings might 2833 * map into the space we have reserved for stack, and currently this 2834 * space is not protected. 2835 * 2836 * Hopefully we will at least detect this condition when we try to 2837 * grow the stack. 2838 */ 2839 if ((prev_entry->next != &map->header) && 2840 (prev_entry->next->start < addrbos + max_ssize)) { 2841 vm_map_unlock(map); 2842 return (KERN_NO_SPACE); 2843 } 2844 2845 /* 2846 * We initially map a stack of only init_ssize. We will grow as 2847 * needed later. Depending on the orientation of the stack (i.e. 2848 * the grow direction) we either map at the top of the range, the 2849 * bottom of the range or in the middle. 2850 * 2851 * Note: we would normally expect prot and max to be VM_PROT_ALL, 2852 * and cow to be 0. Possibly we should eliminate these as input 2853 * parameters, and just pass these values here in the insert call. 2854 */ 2855 if (orient == MAP_STACK_GROWS_DOWN) 2856 bot = addrbos + max_ssize - init_ssize; 2857 else if (orient == MAP_STACK_GROWS_UP) 2858 bot = addrbos; 2859 else 2860 bot = round_page(addrbos + max_ssize/2 - init_ssize/2); 2861 top = bot + init_ssize; 2862 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow); 2863 2864 /* Now set the avail_ssize amount. */ 2865 if (rv == KERN_SUCCESS) { 2866 if (prev_entry != &map->header) 2867 vm_map_clip_end(map, prev_entry, bot); 2868 new_entry = prev_entry->next; 2869 if (new_entry->end != top || new_entry->start != bot) 2870 panic("Bad entry start/end for new stack entry"); 2871 2872 new_entry->avail_ssize = max_ssize - init_ssize; 2873 if (orient & MAP_STACK_GROWS_DOWN) 2874 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 2875 if (orient & MAP_STACK_GROWS_UP) 2876 new_entry->eflags |= MAP_ENTRY_GROWS_UP; 2877 } 2878 2879 vm_map_unlock(map); 2880 return (rv); 2881 } 2882 2883 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 2884 * desired address is already mapped, or if we successfully grow 2885 * the stack. Also returns KERN_SUCCESS if addr is outside the 2886 * stack range (this is strange, but preserves compatibility with 2887 * the grow function in vm_machdep.c). 2888 */ 2889 int 2890 vm_map_growstack(struct proc *p, vm_offset_t addr) 2891 { 2892 vm_map_entry_t next_entry, prev_entry; 2893 vm_map_entry_t new_entry, stack_entry; 2894 struct vmspace *vm = p->p_vmspace; 2895 vm_map_t map = &vm->vm_map; 2896 vm_offset_t end; 2897 size_t grow_amount, max_grow; 2898 rlim_t stacklim, vmemlim; 2899 int is_procstack, rv; 2900 2901 Retry: 2902 PROC_LOCK(p); 2903 stacklim = lim_cur(p, RLIMIT_STACK); 2904 vmemlim = lim_cur(p, RLIMIT_VMEM); 2905 PROC_UNLOCK(p); 2906 2907 vm_map_lock_read(map); 2908 2909 /* If addr is already in the entry range, no need to grow.*/ 2910 if (vm_map_lookup_entry(map, addr, &prev_entry)) { 2911 vm_map_unlock_read(map); 2912 return (KERN_SUCCESS); 2913 } 2914 2915 next_entry = prev_entry->next; 2916 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) { 2917 /* 2918 * This entry does not grow upwards. Since the address lies 2919 * beyond this entry, the next entry (if one exists) has to 2920 * be a downward growable entry. The entry list header is 2921 * never a growable entry, so it suffices to check the flags. 2922 */ 2923 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) { 2924 vm_map_unlock_read(map); 2925 return (KERN_SUCCESS); 2926 } 2927 stack_entry = next_entry; 2928 } else { 2929 /* 2930 * This entry grows upward. If the next entry does not at 2931 * least grow downwards, this is the entry we need to grow. 2932 * otherwise we have two possible choices and we have to 2933 * select one. 2934 */ 2935 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) { 2936 /* 2937 * We have two choices; grow the entry closest to 2938 * the address to minimize the amount of growth. 2939 */ 2940 if (addr - prev_entry->end <= next_entry->start - addr) 2941 stack_entry = prev_entry; 2942 else 2943 stack_entry = next_entry; 2944 } else 2945 stack_entry = prev_entry; 2946 } 2947 2948 if (stack_entry == next_entry) { 2949 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo")); 2950 KASSERT(addr < stack_entry->start, ("foo")); 2951 end = (prev_entry != &map->header) ? prev_entry->end : 2952 stack_entry->start - stack_entry->avail_ssize; 2953 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE); 2954 max_grow = stack_entry->start - end; 2955 } else { 2956 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo")); 2957 KASSERT(addr >= stack_entry->end, ("foo")); 2958 end = (next_entry != &map->header) ? next_entry->start : 2959 stack_entry->end + stack_entry->avail_ssize; 2960 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE); 2961 max_grow = end - stack_entry->end; 2962 } 2963 2964 if (grow_amount > stack_entry->avail_ssize) { 2965 vm_map_unlock_read(map); 2966 return (KERN_NO_SPACE); 2967 } 2968 2969 /* 2970 * If there is no longer enough space between the entries nogo, and 2971 * adjust the available space. Note: this should only happen if the 2972 * user has mapped into the stack area after the stack was created, 2973 * and is probably an error. 2974 * 2975 * This also effectively destroys any guard page the user might have 2976 * intended by limiting the stack size. 2977 */ 2978 if (grow_amount > max_grow) { 2979 if (vm_map_lock_upgrade(map)) 2980 goto Retry; 2981 2982 stack_entry->avail_ssize = max_grow; 2983 2984 vm_map_unlock(map); 2985 return (KERN_NO_SPACE); 2986 } 2987 2988 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0; 2989 2990 /* 2991 * If this is the main process stack, see if we're over the stack 2992 * limit. 2993 */ 2994 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 2995 vm_map_unlock_read(map); 2996 return (KERN_NO_SPACE); 2997 } 2998 2999 /* Round up the grow amount modulo SGROWSIZ */ 3000 grow_amount = roundup (grow_amount, sgrowsiz); 3001 if (grow_amount > stack_entry->avail_ssize) 3002 grow_amount = stack_entry->avail_ssize; 3003 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 3004 grow_amount = stacklim - ctob(vm->vm_ssize); 3005 } 3006 3007 /* If we would blow our VMEM resource limit, no go */ 3008 if (map->size + grow_amount > vmemlim) { 3009 vm_map_unlock_read(map); 3010 return (KERN_NO_SPACE); 3011 } 3012 3013 if (vm_map_lock_upgrade(map)) 3014 goto Retry; 3015 3016 if (stack_entry == next_entry) { 3017 /* 3018 * Growing downward. 3019 */ 3020 /* Get the preliminary new entry start value */ 3021 addr = stack_entry->start - grow_amount; 3022 3023 /* 3024 * If this puts us into the previous entry, cut back our 3025 * growth to the available space. Also, see the note above. 3026 */ 3027 if (addr < end) { 3028 stack_entry->avail_ssize = max_grow; 3029 addr = end; 3030 } 3031 3032 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 3033 p->p_sysent->sv_stackprot, VM_PROT_ALL, 0); 3034 3035 /* Adjust the available stack space by the amount we grew. */ 3036 if (rv == KERN_SUCCESS) { 3037 if (prev_entry != &map->header) 3038 vm_map_clip_end(map, prev_entry, addr); 3039 new_entry = prev_entry->next; 3040 KASSERT(new_entry == stack_entry->prev, ("foo")); 3041 KASSERT(new_entry->end == stack_entry->start, ("foo")); 3042 KASSERT(new_entry->start == addr, ("foo")); 3043 grow_amount = new_entry->end - new_entry->start; 3044 new_entry->avail_ssize = stack_entry->avail_ssize - 3045 grow_amount; 3046 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN; 3047 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 3048 } 3049 } else { 3050 /* 3051 * Growing upward. 3052 */ 3053 addr = stack_entry->end + grow_amount; 3054 3055 /* 3056 * If this puts us into the next entry, cut back our growth 3057 * to the available space. Also, see the note above. 3058 */ 3059 if (addr > end) { 3060 stack_entry->avail_ssize = end - stack_entry->end; 3061 addr = end; 3062 } 3063 3064 grow_amount = addr - stack_entry->end; 3065 3066 /* Grow the underlying object if applicable. */ 3067 if (stack_entry->object.vm_object == NULL || 3068 vm_object_coalesce(stack_entry->object.vm_object, 3069 stack_entry->offset, 3070 (vm_size_t)(stack_entry->end - stack_entry->start), 3071 (vm_size_t)grow_amount)) { 3072 map->size += (addr - stack_entry->end); 3073 /* Update the current entry. */ 3074 stack_entry->end = addr; 3075 stack_entry->avail_ssize -= grow_amount; 3076 vm_map_entry_resize_free(map, stack_entry); 3077 rv = KERN_SUCCESS; 3078 3079 if (next_entry != &map->header) 3080 vm_map_clip_start(map, next_entry, addr); 3081 } else 3082 rv = KERN_FAILURE; 3083 } 3084 3085 if (rv == KERN_SUCCESS && is_procstack) 3086 vm->vm_ssize += btoc(grow_amount); 3087 3088 vm_map_unlock(map); 3089 3090 /* 3091 * Heed the MAP_WIREFUTURE flag if it was set for this process. 3092 */ 3093 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) { 3094 vm_map_wire(map, 3095 (stack_entry == next_entry) ? addr : addr - grow_amount, 3096 (stack_entry == next_entry) ? stack_entry->start : addr, 3097 (p->p_flag & P_SYSTEM) 3098 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES 3099 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 3100 } 3101 3102 return (rv); 3103 } 3104 3105 /* 3106 * Unshare the specified VM space for exec. If other processes are 3107 * mapped to it, then create a new one. The new vmspace is null. 3108 */ 3109 int 3110 vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser) 3111 { 3112 struct vmspace *oldvmspace = p->p_vmspace; 3113 struct vmspace *newvmspace; 3114 3115 newvmspace = vmspace_alloc(minuser, maxuser); 3116 if (newvmspace == NULL) 3117 return (ENOMEM); 3118 newvmspace->vm_swrss = oldvmspace->vm_swrss; 3119 /* 3120 * This code is written like this for prototype purposes. The 3121 * goal is to avoid running down the vmspace here, but let the 3122 * other process's that are still using the vmspace to finally 3123 * run it down. Even though there is little or no chance of blocking 3124 * here, it is a good idea to keep this form for future mods. 3125 */ 3126 PROC_VMSPACE_LOCK(p); 3127 p->p_vmspace = newvmspace; 3128 PROC_VMSPACE_UNLOCK(p); 3129 if (p == curthread->td_proc) 3130 pmap_activate(curthread); 3131 vmspace_free(oldvmspace); 3132 return (0); 3133 } 3134 3135 /* 3136 * Unshare the specified VM space for forcing COW. This 3137 * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 3138 */ 3139 int 3140 vmspace_unshare(struct proc *p) 3141 { 3142 struct vmspace *oldvmspace = p->p_vmspace; 3143 struct vmspace *newvmspace; 3144 3145 if (oldvmspace->vm_refcnt == 1) 3146 return (0); 3147 newvmspace = vmspace_fork(oldvmspace); 3148 if (newvmspace == NULL) 3149 return (ENOMEM); 3150 PROC_VMSPACE_LOCK(p); 3151 p->p_vmspace = newvmspace; 3152 PROC_VMSPACE_UNLOCK(p); 3153 if (p == curthread->td_proc) 3154 pmap_activate(curthread); 3155 vmspace_free(oldvmspace); 3156 return (0); 3157 } 3158 3159 /* 3160 * vm_map_lookup: 3161 * 3162 * Finds the VM object, offset, and 3163 * protection for a given virtual address in the 3164 * specified map, assuming a page fault of the 3165 * type specified. 3166 * 3167 * Leaves the map in question locked for read; return 3168 * values are guaranteed until a vm_map_lookup_done 3169 * call is performed. Note that the map argument 3170 * is in/out; the returned map must be used in 3171 * the call to vm_map_lookup_done. 3172 * 3173 * A handle (out_entry) is returned for use in 3174 * vm_map_lookup_done, to make that fast. 3175 * 3176 * If a lookup is requested with "write protection" 3177 * specified, the map may be changed to perform virtual 3178 * copying operations, although the data referenced will 3179 * remain the same. 3180 */ 3181 int 3182 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 3183 vm_offset_t vaddr, 3184 vm_prot_t fault_typea, 3185 vm_map_entry_t *out_entry, /* OUT */ 3186 vm_object_t *object, /* OUT */ 3187 vm_pindex_t *pindex, /* OUT */ 3188 vm_prot_t *out_prot, /* OUT */ 3189 boolean_t *wired) /* OUT */ 3190 { 3191 vm_map_entry_t entry; 3192 vm_map_t map = *var_map; 3193 vm_prot_t prot; 3194 vm_prot_t fault_type = fault_typea; 3195 3196 RetryLookup:; 3197 3198 vm_map_lock_read(map); 3199 3200 /* 3201 * Lookup the faulting address. 3202 */ 3203 if (!vm_map_lookup_entry(map, vaddr, out_entry)) { 3204 vm_map_unlock_read(map); 3205 return (KERN_INVALID_ADDRESS); 3206 } 3207 3208 entry = *out_entry; 3209 3210 /* 3211 * Handle submaps. 3212 */ 3213 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3214 vm_map_t old_map = map; 3215 3216 *var_map = map = entry->object.sub_map; 3217 vm_map_unlock_read(old_map); 3218 goto RetryLookup; 3219 } 3220 3221 /* 3222 * Check whether this task is allowed to have this page. 3223 * Note the special case for MAP_ENTRY_COW 3224 * pages with an override. This is to implement a forced 3225 * COW for debuggers. 3226 */ 3227 if (fault_type & VM_PROT_OVERRIDE_WRITE) 3228 prot = entry->max_protection; 3229 else 3230 prot = entry->protection; 3231 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 3232 if ((fault_type & prot) != fault_type) { 3233 vm_map_unlock_read(map); 3234 return (KERN_PROTECTION_FAILURE); 3235 } 3236 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 3237 (entry->eflags & MAP_ENTRY_COW) && 3238 (fault_type & VM_PROT_WRITE) && 3239 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 3240 vm_map_unlock_read(map); 3241 return (KERN_PROTECTION_FAILURE); 3242 } 3243 3244 /* 3245 * If this page is not pageable, we have to get it for all possible 3246 * accesses. 3247 */ 3248 *wired = (entry->wired_count != 0); 3249 if (*wired) 3250 prot = fault_type = entry->protection; 3251 3252 /* 3253 * If the entry was copy-on-write, we either ... 3254 */ 3255 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3256 /* 3257 * If we want to write the page, we may as well handle that 3258 * now since we've got the map locked. 3259 * 3260 * If we don't need to write the page, we just demote the 3261 * permissions allowed. 3262 */ 3263 if (fault_type & VM_PROT_WRITE) { 3264 /* 3265 * Make a new object, and place it in the object 3266 * chain. Note that no new references have appeared 3267 * -- one just moved from the map to the new 3268 * object. 3269 */ 3270 if (vm_map_lock_upgrade(map)) 3271 goto RetryLookup; 3272 3273 vm_object_shadow( 3274 &entry->object.vm_object, 3275 &entry->offset, 3276 atop(entry->end - entry->start)); 3277 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 3278 3279 vm_map_lock_downgrade(map); 3280 } else { 3281 /* 3282 * We're attempting to read a copy-on-write page -- 3283 * don't allow writes. 3284 */ 3285 prot &= ~VM_PROT_WRITE; 3286 } 3287 } 3288 3289 /* 3290 * Create an object if necessary. 3291 */ 3292 if (entry->object.vm_object == NULL && 3293 !map->system_map) { 3294 if (vm_map_lock_upgrade(map)) 3295 goto RetryLookup; 3296 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 3297 atop(entry->end - entry->start)); 3298 entry->offset = 0; 3299 vm_map_lock_downgrade(map); 3300 } 3301 3302 /* 3303 * Return the object/offset from this entry. If the entry was 3304 * copy-on-write or empty, it has been fixed up. 3305 */ 3306 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 3307 *object = entry->object.vm_object; 3308 3309 *out_prot = prot; 3310 return (KERN_SUCCESS); 3311 } 3312 3313 /* 3314 * vm_map_lookup_locked: 3315 * 3316 * Lookup the faulting address. A version of vm_map_lookup that returns 3317 * KERN_FAILURE instead of blocking on map lock or memory allocation. 3318 */ 3319 int 3320 vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */ 3321 vm_offset_t vaddr, 3322 vm_prot_t fault_typea, 3323 vm_map_entry_t *out_entry, /* OUT */ 3324 vm_object_t *object, /* OUT */ 3325 vm_pindex_t *pindex, /* OUT */ 3326 vm_prot_t *out_prot, /* OUT */ 3327 boolean_t *wired) /* OUT */ 3328 { 3329 vm_map_entry_t entry; 3330 vm_map_t map = *var_map; 3331 vm_prot_t prot; 3332 vm_prot_t fault_type = fault_typea; 3333 3334 /* 3335 * Lookup the faulting address. 3336 */ 3337 if (!vm_map_lookup_entry(map, vaddr, out_entry)) 3338 return (KERN_INVALID_ADDRESS); 3339 3340 entry = *out_entry; 3341 3342 /* 3343 * Fail if the entry refers to a submap. 3344 */ 3345 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 3346 return (KERN_FAILURE); 3347 3348 /* 3349 * Check whether this task is allowed to have this page. 3350 * Note the special case for MAP_ENTRY_COW 3351 * pages with an override. This is to implement a forced 3352 * COW for debuggers. 3353 */ 3354 if (fault_type & VM_PROT_OVERRIDE_WRITE) 3355 prot = entry->max_protection; 3356 else 3357 prot = entry->protection; 3358 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 3359 if ((fault_type & prot) != fault_type) 3360 return (KERN_PROTECTION_FAILURE); 3361 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 3362 (entry->eflags & MAP_ENTRY_COW) && 3363 (fault_type & VM_PROT_WRITE) && 3364 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) 3365 return (KERN_PROTECTION_FAILURE); 3366 3367 /* 3368 * If this page is not pageable, we have to get it for all possible 3369 * accesses. 3370 */ 3371 *wired = (entry->wired_count != 0); 3372 if (*wired) 3373 prot = fault_type = entry->protection; 3374 3375 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3376 /* 3377 * Fail if the entry was copy-on-write for a write fault. 3378 */ 3379 if (fault_type & VM_PROT_WRITE) 3380 return (KERN_FAILURE); 3381 /* 3382 * We're attempting to read a copy-on-write page -- 3383 * don't allow writes. 3384 */ 3385 prot &= ~VM_PROT_WRITE; 3386 } 3387 3388 /* 3389 * Fail if an object should be created. 3390 */ 3391 if (entry->object.vm_object == NULL && !map->system_map) 3392 return (KERN_FAILURE); 3393 3394 /* 3395 * Return the object/offset from this entry. If the entry was 3396 * copy-on-write or empty, it has been fixed up. 3397 */ 3398 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 3399 *object = entry->object.vm_object; 3400 3401 *out_prot = prot; 3402 return (KERN_SUCCESS); 3403 } 3404 3405 /* 3406 * vm_map_lookup_done: 3407 * 3408 * Releases locks acquired by a vm_map_lookup 3409 * (according to the handle returned by that lookup). 3410 */ 3411 void 3412 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 3413 { 3414 /* 3415 * Unlock the main-level map 3416 */ 3417 vm_map_unlock_read(map); 3418 } 3419 3420 #include "opt_ddb.h" 3421 #ifdef DDB 3422 #include <sys/kernel.h> 3423 3424 #include <ddb/ddb.h> 3425 3426 /* 3427 * vm_map_print: [ debug ] 3428 */ 3429 DB_SHOW_COMMAND(map, vm_map_print) 3430 { 3431 static int nlines; 3432 /* XXX convert args. */ 3433 vm_map_t map = (vm_map_t)addr; 3434 boolean_t full = have_addr; 3435 3436 vm_map_entry_t entry; 3437 3438 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 3439 (void *)map, 3440 (void *)map->pmap, map->nentries, map->timestamp); 3441 nlines++; 3442 3443 if (!full && db_indent) 3444 return; 3445 3446 db_indent += 2; 3447 for (entry = map->header.next; entry != &map->header; 3448 entry = entry->next) { 3449 db_iprintf("map entry %p: start=%p, end=%p\n", 3450 (void *)entry, (void *)entry->start, (void *)entry->end); 3451 nlines++; 3452 { 3453 static char *inheritance_name[4] = 3454 {"share", "copy", "none", "donate_copy"}; 3455 3456 db_iprintf(" prot=%x/%x/%s", 3457 entry->protection, 3458 entry->max_protection, 3459 inheritance_name[(int)(unsigned char)entry->inheritance]); 3460 if (entry->wired_count != 0) 3461 db_printf(", wired"); 3462 } 3463 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3464 db_printf(", share=%p, offset=0x%jx\n", 3465 (void *)entry->object.sub_map, 3466 (uintmax_t)entry->offset); 3467 nlines++; 3468 if ((entry->prev == &map->header) || 3469 (entry->prev->object.sub_map != 3470 entry->object.sub_map)) { 3471 db_indent += 2; 3472 vm_map_print((db_expr_t)(intptr_t) 3473 entry->object.sub_map, 3474 full, 0, (char *)0); 3475 db_indent -= 2; 3476 } 3477 } else { 3478 db_printf(", object=%p, offset=0x%jx", 3479 (void *)entry->object.vm_object, 3480 (uintmax_t)entry->offset); 3481 if (entry->eflags & MAP_ENTRY_COW) 3482 db_printf(", copy (%s)", 3483 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3484 db_printf("\n"); 3485 nlines++; 3486 3487 if ((entry->prev == &map->header) || 3488 (entry->prev->object.vm_object != 3489 entry->object.vm_object)) { 3490 db_indent += 2; 3491 vm_object_print((db_expr_t)(intptr_t) 3492 entry->object.vm_object, 3493 full, 0, (char *)0); 3494 nlines += 4; 3495 db_indent -= 2; 3496 } 3497 } 3498 } 3499 db_indent -= 2; 3500 if (db_indent == 0) 3501 nlines = 0; 3502 } 3503 3504 3505 DB_SHOW_COMMAND(procvm, procvm) 3506 { 3507 struct proc *p; 3508 3509 if (have_addr) { 3510 p = (struct proc *) addr; 3511 } else { 3512 p = curproc; 3513 } 3514 3515 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3516 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3517 (void *)vmspace_pmap(p->p_vmspace)); 3518 3519 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 3520 } 3521 3522 #endif /* DDB */ 3523