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