vm_map.c revision 267899
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 267899 2014-06-26 08:24:18Z 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 (void) vm_map_delete(map, start, end); 1411 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) { 1412 result = vm_map_stack_locked(map, start, length, sgrowsiz, 1413 prot, max, cow); 1414 } else { 1415 result = vm_map_insert(map, object, offset, start, end, 1416 prot, max, cow); 1417 } 1418 vm_map_unlock(map); 1419 return (result); 1420} 1421 1422/* 1423 * vm_map_find finds an unallocated region in the target address 1424 * map with the given length. The search is defined to be 1425 * first-fit from the specified address; the region found is 1426 * returned in the same parameter. 1427 * 1428 * If object is non-NULL, ref count must be bumped by caller 1429 * prior to making call to account for the new entry. 1430 */ 1431int 1432vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1433 vm_offset_t *addr, /* IN/OUT */ 1434 vm_size_t length, vm_offset_t max_addr, int find_space, 1435 vm_prot_t prot, vm_prot_t max, int cow) 1436{ 1437 vm_offset_t alignment, initial_addr, start; 1438 int result; 1439 1440 KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 || 1441 object == NULL, 1442 ("vm_map_find: non-NULL backing object for stack")); 1443 if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL || 1444 (object->flags & OBJ_COLORED) == 0)) 1445 find_space = VMFS_ANY_SPACE; 1446 if (find_space >> 8 != 0) { 1447 KASSERT((find_space & 0xff) == 0, ("bad VMFS flags")); 1448 alignment = (vm_offset_t)1 << (find_space >> 8); 1449 } else 1450 alignment = 0; 1451 initial_addr = *addr; 1452again: 1453 start = initial_addr; 1454 vm_map_lock(map); 1455 do { 1456 if (find_space != VMFS_NO_SPACE) { 1457 if (vm_map_findspace(map, start, length, addr) || 1458 (max_addr != 0 && *addr + length > max_addr)) { 1459 vm_map_unlock(map); 1460 if (find_space == VMFS_OPTIMAL_SPACE) { 1461 find_space = VMFS_ANY_SPACE; 1462 goto again; 1463 } 1464 return (KERN_NO_SPACE); 1465 } 1466 switch (find_space) { 1467 case VMFS_SUPER_SPACE: 1468 case VMFS_OPTIMAL_SPACE: 1469 pmap_align_superpage(object, offset, addr, 1470 length); 1471 break; 1472 case VMFS_ANY_SPACE: 1473 break; 1474 default: 1475 if ((*addr & (alignment - 1)) != 0) { 1476 *addr &= ~(alignment - 1); 1477 *addr += alignment; 1478 } 1479 break; 1480 } 1481 1482 start = *addr; 1483 } 1484 if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) { 1485 result = vm_map_stack_locked(map, start, length, 1486 sgrowsiz, prot, max, cow); 1487 } else { 1488 result = vm_map_insert(map, object, offset, start, 1489 start + length, prot, max, cow); 1490 } 1491 } while (result == KERN_NO_SPACE && find_space != VMFS_NO_SPACE && 1492 find_space != VMFS_ANY_SPACE); 1493 vm_map_unlock(map); 1494 return (result); 1495} 1496 1497/* 1498 * vm_map_simplify_entry: 1499 * 1500 * Simplify the given map entry by merging with either neighbor. This 1501 * routine also has the ability to merge with both neighbors. 1502 * 1503 * The map must be locked. 1504 * 1505 * This routine guarentees that the passed entry remains valid (though 1506 * possibly extended). When merging, this routine may delete one or 1507 * both neighbors. 1508 */ 1509void 1510vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 1511{ 1512 vm_map_entry_t next, prev; 1513 vm_size_t prevsize, esize; 1514 1515 if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) 1516 return; 1517 1518 prev = entry->prev; 1519 if (prev != &map->header) { 1520 prevsize = prev->end - prev->start; 1521 if ( (prev->end == entry->start) && 1522 (prev->object.vm_object == entry->object.vm_object) && 1523 (!prev->object.vm_object || 1524 (prev->offset + prevsize == entry->offset)) && 1525 (prev->eflags == entry->eflags) && 1526 (prev->protection == entry->protection) && 1527 (prev->max_protection == entry->max_protection) && 1528 (prev->inheritance == entry->inheritance) && 1529 (prev->wired_count == entry->wired_count) && 1530 (prev->cred == entry->cred)) { 1531 vm_map_entry_unlink(map, prev); 1532 entry->start = prev->start; 1533 entry->offset = prev->offset; 1534 if (entry->prev != &map->header) 1535 vm_map_entry_resize_free(map, entry->prev); 1536 1537 /* 1538 * If the backing object is a vnode object, 1539 * vm_object_deallocate() calls vrele(). 1540 * However, vrele() does not lock the vnode 1541 * because the vnode has additional 1542 * references. Thus, the map lock can be kept 1543 * without causing a lock-order reversal with 1544 * the vnode lock. 1545 * 1546 * Since we count the number of virtual page 1547 * mappings in object->un_pager.vnp.writemappings, 1548 * the writemappings value should not be adjusted 1549 * when the entry is disposed of. 1550 */ 1551 if (prev->object.vm_object) 1552 vm_object_deallocate(prev->object.vm_object); 1553 if (prev->cred != NULL) 1554 crfree(prev->cred); 1555 vm_map_entry_dispose(map, prev); 1556 } 1557 } 1558 1559 next = entry->next; 1560 if (next != &map->header) { 1561 esize = entry->end - entry->start; 1562 if ((entry->end == next->start) && 1563 (next->object.vm_object == entry->object.vm_object) && 1564 (!entry->object.vm_object || 1565 (entry->offset + esize == next->offset)) && 1566 (next->eflags == entry->eflags) && 1567 (next->protection == entry->protection) && 1568 (next->max_protection == entry->max_protection) && 1569 (next->inheritance == entry->inheritance) && 1570 (next->wired_count == entry->wired_count) && 1571 (next->cred == entry->cred)) { 1572 vm_map_entry_unlink(map, next); 1573 entry->end = next->end; 1574 vm_map_entry_resize_free(map, entry); 1575 1576 /* 1577 * See comment above. 1578 */ 1579 if (next->object.vm_object) 1580 vm_object_deallocate(next->object.vm_object); 1581 if (next->cred != NULL) 1582 crfree(next->cred); 1583 vm_map_entry_dispose(map, next); 1584 } 1585 } 1586} 1587/* 1588 * vm_map_clip_start: [ internal use only ] 1589 * 1590 * Asserts that the given entry begins at or after 1591 * the specified address; if necessary, 1592 * it splits the entry into two. 1593 */ 1594#define vm_map_clip_start(map, entry, startaddr) \ 1595{ \ 1596 if (startaddr > entry->start) \ 1597 _vm_map_clip_start(map, entry, startaddr); \ 1598} 1599 1600/* 1601 * This routine is called only when it is known that 1602 * the entry must be split. 1603 */ 1604static void 1605_vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1606{ 1607 vm_map_entry_t new_entry; 1608 1609 VM_MAP_ASSERT_LOCKED(map); 1610 1611 /* 1612 * Split off the front portion -- note that we must insert the new 1613 * entry BEFORE this one, so that this entry has the specified 1614 * starting address. 1615 */ 1616 vm_map_simplify_entry(map, entry); 1617 1618 /* 1619 * If there is no object backing this entry, we might as well create 1620 * one now. If we defer it, an object can get created after the map 1621 * is clipped, and individual objects will be created for the split-up 1622 * map. This is a bit of a hack, but is also about the best place to 1623 * put this improvement. 1624 */ 1625 if (entry->object.vm_object == NULL && !map->system_map) { 1626 vm_object_t object; 1627 object = vm_object_allocate(OBJT_DEFAULT, 1628 atop(entry->end - entry->start)); 1629 entry->object.vm_object = object; 1630 entry->offset = 0; 1631 if (entry->cred != NULL) { 1632 object->cred = entry->cred; 1633 object->charge = entry->end - entry->start; 1634 entry->cred = NULL; 1635 } 1636 } else if (entry->object.vm_object != NULL && 1637 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) && 1638 entry->cred != NULL) { 1639 VM_OBJECT_WLOCK(entry->object.vm_object); 1640 KASSERT(entry->object.vm_object->cred == NULL, 1641 ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry)); 1642 entry->object.vm_object->cred = entry->cred; 1643 entry->object.vm_object->charge = entry->end - entry->start; 1644 VM_OBJECT_WUNLOCK(entry->object.vm_object); 1645 entry->cred = NULL; 1646 } 1647 1648 new_entry = vm_map_entry_create(map); 1649 *new_entry = *entry; 1650 1651 new_entry->end = start; 1652 entry->offset += (start - entry->start); 1653 entry->start = start; 1654 if (new_entry->cred != NULL) 1655 crhold(entry->cred); 1656 1657 vm_map_entry_link(map, entry->prev, new_entry); 1658 1659 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1660 vm_object_reference(new_entry->object.vm_object); 1661 /* 1662 * The object->un_pager.vnp.writemappings for the 1663 * object of MAP_ENTRY_VN_WRITECNT type entry shall be 1664 * kept as is here. The virtual pages are 1665 * re-distributed among the clipped entries, so the sum is 1666 * left the same. 1667 */ 1668 } 1669} 1670 1671/* 1672 * vm_map_clip_end: [ internal use only ] 1673 * 1674 * Asserts that the given entry ends at or before 1675 * the specified address; if necessary, 1676 * it splits the entry into two. 1677 */ 1678#define vm_map_clip_end(map, entry, endaddr) \ 1679{ \ 1680 if ((endaddr) < (entry->end)) \ 1681 _vm_map_clip_end((map), (entry), (endaddr)); \ 1682} 1683 1684/* 1685 * This routine is called only when it is known that 1686 * the entry must be split. 1687 */ 1688static void 1689_vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1690{ 1691 vm_map_entry_t new_entry; 1692 1693 VM_MAP_ASSERT_LOCKED(map); 1694 1695 /* 1696 * If there is no object backing this entry, we might as well create 1697 * one now. If we defer it, an object can get created after the map 1698 * is clipped, and individual objects will be created for the split-up 1699 * map. This is a bit of a hack, but is also about the best place to 1700 * put this improvement. 1701 */ 1702 if (entry->object.vm_object == NULL && !map->system_map) { 1703 vm_object_t object; 1704 object = vm_object_allocate(OBJT_DEFAULT, 1705 atop(entry->end - entry->start)); 1706 entry->object.vm_object = object; 1707 entry->offset = 0; 1708 if (entry->cred != NULL) { 1709 object->cred = entry->cred; 1710 object->charge = entry->end - entry->start; 1711 entry->cred = NULL; 1712 } 1713 } else if (entry->object.vm_object != NULL && 1714 ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) && 1715 entry->cred != NULL) { 1716 VM_OBJECT_WLOCK(entry->object.vm_object); 1717 KASSERT(entry->object.vm_object->cred == NULL, 1718 ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry)); 1719 entry->object.vm_object->cred = entry->cred; 1720 entry->object.vm_object->charge = entry->end - entry->start; 1721 VM_OBJECT_WUNLOCK(entry->object.vm_object); 1722 entry->cred = NULL; 1723 } 1724 1725 /* 1726 * Create a new entry and insert it AFTER the specified entry 1727 */ 1728 new_entry = vm_map_entry_create(map); 1729 *new_entry = *entry; 1730 1731 new_entry->start = entry->end = end; 1732 new_entry->offset += (end - entry->start); 1733 if (new_entry->cred != NULL) 1734 crhold(entry->cred); 1735 1736 vm_map_entry_link(map, entry, new_entry); 1737 1738 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1739 vm_object_reference(new_entry->object.vm_object); 1740 } 1741} 1742 1743/* 1744 * vm_map_submap: [ kernel use only ] 1745 * 1746 * Mark the given range as handled by a subordinate map. 1747 * 1748 * This range must have been created with vm_map_find, 1749 * and no other operations may have been performed on this 1750 * range prior to calling vm_map_submap. 1751 * 1752 * Only a limited number of operations can be performed 1753 * within this rage after calling vm_map_submap: 1754 * vm_fault 1755 * [Don't try vm_map_copy!] 1756 * 1757 * To remove a submapping, one must first remove the 1758 * range from the superior map, and then destroy the 1759 * submap (if desired). [Better yet, don't try it.] 1760 */ 1761int 1762vm_map_submap( 1763 vm_map_t map, 1764 vm_offset_t start, 1765 vm_offset_t end, 1766 vm_map_t submap) 1767{ 1768 vm_map_entry_t entry; 1769 int result = KERN_INVALID_ARGUMENT; 1770 1771 vm_map_lock(map); 1772 1773 VM_MAP_RANGE_CHECK(map, start, end); 1774 1775 if (vm_map_lookup_entry(map, start, &entry)) { 1776 vm_map_clip_start(map, entry, start); 1777 } else 1778 entry = entry->next; 1779 1780 vm_map_clip_end(map, entry, end); 1781 1782 if ((entry->start == start) && (entry->end == end) && 1783 ((entry->eflags & MAP_ENTRY_COW) == 0) && 1784 (entry->object.vm_object == NULL)) { 1785 entry->object.sub_map = submap; 1786 entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 1787 result = KERN_SUCCESS; 1788 } 1789 vm_map_unlock(map); 1790 1791 return (result); 1792} 1793 1794/* 1795 * The maximum number of pages to map 1796 */ 1797#define MAX_INIT_PT 96 1798 1799/* 1800 * vm_map_pmap_enter: 1801 * 1802 * Preload read-only mappings for the specified object's resident pages 1803 * into the target map. If "flags" is MAP_PREFAULT_PARTIAL, then only 1804 * the resident pages within the address range [addr, addr + ulmin(size, 1805 * ptoa(MAX_INIT_PT))) are mapped. Otherwise, all resident pages within 1806 * the specified address range are mapped. This eliminates many soft 1807 * faults on process startup and immediately after an mmap(2). Because 1808 * these are speculative mappings, cached pages are not reactivated and 1809 * mapped. 1810 */ 1811void 1812vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 1813 vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags) 1814{ 1815 vm_offset_t start; 1816 vm_page_t p, p_start; 1817 vm_pindex_t psize, tmpidx; 1818 1819 if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL) 1820 return; 1821 VM_OBJECT_RLOCK(object); 1822 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) { 1823 VM_OBJECT_RUNLOCK(object); 1824 VM_OBJECT_WLOCK(object); 1825 if (object->type == OBJT_DEVICE || object->type == OBJT_SG) { 1826 pmap_object_init_pt(map->pmap, addr, object, pindex, 1827 size); 1828 VM_OBJECT_WUNLOCK(object); 1829 return; 1830 } 1831 VM_OBJECT_LOCK_DOWNGRADE(object); 1832 } 1833 1834 psize = atop(size); 1835 if (psize > MAX_INIT_PT && (flags & MAP_PREFAULT_PARTIAL) != 0) 1836 psize = MAX_INIT_PT; 1837 if (psize + pindex > object->size) { 1838 if (object->size < pindex) { 1839 VM_OBJECT_RUNLOCK(object); 1840 return; 1841 } 1842 psize = object->size - pindex; 1843 } 1844 1845 start = 0; 1846 p_start = NULL; 1847 1848 p = vm_page_find_least(object, pindex); 1849 /* 1850 * Assert: the variable p is either (1) the page with the 1851 * least pindex greater than or equal to the parameter pindex 1852 * or (2) NULL. 1853 */ 1854 for (; 1855 p != NULL && (tmpidx = p->pindex - pindex) < psize; 1856 p = TAILQ_NEXT(p, listq)) { 1857 /* 1858 * don't allow an madvise to blow away our really 1859 * free pages allocating pv entries. 1860 */ 1861 if ((flags & MAP_PREFAULT_MADVISE) && 1862 cnt.v_free_count < cnt.v_free_reserved) { 1863 psize = tmpidx; 1864 break; 1865 } 1866 if (p->valid == VM_PAGE_BITS_ALL) { 1867 if (p_start == NULL) { 1868 start = addr + ptoa(tmpidx); 1869 p_start = p; 1870 } 1871 } else if (p_start != NULL) { 1872 pmap_enter_object(map->pmap, start, addr + 1873 ptoa(tmpidx), p_start, prot); 1874 p_start = NULL; 1875 } 1876 } 1877 if (p_start != NULL) 1878 pmap_enter_object(map->pmap, start, addr + ptoa(psize), 1879 p_start, prot); 1880 VM_OBJECT_RUNLOCK(object); 1881} 1882 1883/* 1884 * vm_map_protect: 1885 * 1886 * Sets the protection of the specified address 1887 * region in the target map. If "set_max" is 1888 * specified, the maximum protection is to be set; 1889 * otherwise, only the current protection is affected. 1890 */ 1891int 1892vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1893 vm_prot_t new_prot, boolean_t set_max) 1894{ 1895 vm_map_entry_t current, entry; 1896 vm_object_t obj; 1897 struct ucred *cred; 1898 vm_prot_t old_prot; 1899 1900 if (start == end) 1901 return (KERN_SUCCESS); 1902 1903 vm_map_lock(map); 1904 1905 VM_MAP_RANGE_CHECK(map, start, end); 1906 1907 if (vm_map_lookup_entry(map, start, &entry)) { 1908 vm_map_clip_start(map, entry, start); 1909 } else { 1910 entry = entry->next; 1911 } 1912 1913 /* 1914 * Make a first pass to check for protection violations. 1915 */ 1916 current = entry; 1917 while ((current != &map->header) && (current->start < end)) { 1918 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1919 vm_map_unlock(map); 1920 return (KERN_INVALID_ARGUMENT); 1921 } 1922 if ((new_prot & current->max_protection) != new_prot) { 1923 vm_map_unlock(map); 1924 return (KERN_PROTECTION_FAILURE); 1925 } 1926 current = current->next; 1927 } 1928 1929 1930 /* 1931 * Do an accounting pass for private read-only mappings that 1932 * now will do cow due to allowed write (e.g. debugger sets 1933 * breakpoint on text segment) 1934 */ 1935 for (current = entry; (current != &map->header) && 1936 (current->start < end); current = current->next) { 1937 1938 vm_map_clip_end(map, current, end); 1939 1940 if (set_max || 1941 ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 || 1942 ENTRY_CHARGED(current)) { 1943 continue; 1944 } 1945 1946 cred = curthread->td_ucred; 1947 obj = current->object.vm_object; 1948 1949 if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) { 1950 if (!swap_reserve(current->end - current->start)) { 1951 vm_map_unlock(map); 1952 return (KERN_RESOURCE_SHORTAGE); 1953 } 1954 crhold(cred); 1955 current->cred = cred; 1956 continue; 1957 } 1958 1959 VM_OBJECT_WLOCK(obj); 1960 if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) { 1961 VM_OBJECT_WUNLOCK(obj); 1962 continue; 1963 } 1964 1965 /* 1966 * Charge for the whole object allocation now, since 1967 * we cannot distinguish between non-charged and 1968 * charged clipped mapping of the same object later. 1969 */ 1970 KASSERT(obj->charge == 0, 1971 ("vm_map_protect: object %p overcharged (entry %p)", 1972 obj, current)); 1973 if (!swap_reserve(ptoa(obj->size))) { 1974 VM_OBJECT_WUNLOCK(obj); 1975 vm_map_unlock(map); 1976 return (KERN_RESOURCE_SHORTAGE); 1977 } 1978 1979 crhold(cred); 1980 obj->cred = cred; 1981 obj->charge = ptoa(obj->size); 1982 VM_OBJECT_WUNLOCK(obj); 1983 } 1984 1985 /* 1986 * Go back and fix up protections. [Note that clipping is not 1987 * necessary the second time.] 1988 */ 1989 current = entry; 1990 while ((current != &map->header) && (current->start < end)) { 1991 old_prot = current->protection; 1992 1993 if (set_max) 1994 current->protection = 1995 (current->max_protection = new_prot) & 1996 old_prot; 1997 else 1998 current->protection = new_prot; 1999 2000 /* 2001 * For user wired map entries, the normal lazy evaluation of 2002 * write access upgrades through soft page faults is 2003 * undesirable. Instead, immediately copy any pages that are 2004 * copy-on-write and enable write access in the physical map. 2005 */ 2006 if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 && 2007 (current->protection & VM_PROT_WRITE) != 0 && 2008 (old_prot & VM_PROT_WRITE) == 0) 2009 vm_fault_copy_entry(map, map, current, current, NULL); 2010 2011 /* 2012 * When restricting access, update the physical map. Worry 2013 * about copy-on-write here. 2014 */ 2015 if ((old_prot & ~current->protection) != 0) { 2016#define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 2017 VM_PROT_ALL) 2018 pmap_protect(map->pmap, current->start, 2019 current->end, 2020 current->protection & MASK(current)); 2021#undef MASK 2022 } 2023 vm_map_simplify_entry(map, current); 2024 current = current->next; 2025 } 2026 vm_map_unlock(map); 2027 return (KERN_SUCCESS); 2028} 2029 2030/* 2031 * vm_map_madvise: 2032 * 2033 * This routine traverses a processes map handling the madvise 2034 * system call. Advisories are classified as either those effecting 2035 * the vm_map_entry structure, or those effecting the underlying 2036 * objects. 2037 */ 2038int 2039vm_map_madvise( 2040 vm_map_t map, 2041 vm_offset_t start, 2042 vm_offset_t end, 2043 int behav) 2044{ 2045 vm_map_entry_t current, entry; 2046 int modify_map = 0; 2047 2048 /* 2049 * Some madvise calls directly modify the vm_map_entry, in which case 2050 * we need to use an exclusive lock on the map and we need to perform 2051 * various clipping operations. Otherwise we only need a read-lock 2052 * on the map. 2053 */ 2054 switch(behav) { 2055 case MADV_NORMAL: 2056 case MADV_SEQUENTIAL: 2057 case MADV_RANDOM: 2058 case MADV_NOSYNC: 2059 case MADV_AUTOSYNC: 2060 case MADV_NOCORE: 2061 case MADV_CORE: 2062 if (start == end) 2063 return (KERN_SUCCESS); 2064 modify_map = 1; 2065 vm_map_lock(map); 2066 break; 2067 case MADV_WILLNEED: 2068 case MADV_DONTNEED: 2069 case MADV_FREE: 2070 if (start == end) 2071 return (KERN_SUCCESS); 2072 vm_map_lock_read(map); 2073 break; 2074 default: 2075 return (KERN_INVALID_ARGUMENT); 2076 } 2077 2078 /* 2079 * Locate starting entry and clip if necessary. 2080 */ 2081 VM_MAP_RANGE_CHECK(map, start, end); 2082 2083 if (vm_map_lookup_entry(map, start, &entry)) { 2084 if (modify_map) 2085 vm_map_clip_start(map, entry, start); 2086 } else { 2087 entry = entry->next; 2088 } 2089 2090 if (modify_map) { 2091 /* 2092 * madvise behaviors that are implemented in the vm_map_entry. 2093 * 2094 * We clip the vm_map_entry so that behavioral changes are 2095 * limited to the specified address range. 2096 */ 2097 for (current = entry; 2098 (current != &map->header) && (current->start < end); 2099 current = current->next 2100 ) { 2101 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 2102 continue; 2103 2104 vm_map_clip_end(map, current, end); 2105 2106 switch (behav) { 2107 case MADV_NORMAL: 2108 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 2109 break; 2110 case MADV_SEQUENTIAL: 2111 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 2112 break; 2113 case MADV_RANDOM: 2114 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 2115 break; 2116 case MADV_NOSYNC: 2117 current->eflags |= MAP_ENTRY_NOSYNC; 2118 break; 2119 case MADV_AUTOSYNC: 2120 current->eflags &= ~MAP_ENTRY_NOSYNC; 2121 break; 2122 case MADV_NOCORE: 2123 current->eflags |= MAP_ENTRY_NOCOREDUMP; 2124 break; 2125 case MADV_CORE: 2126 current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 2127 break; 2128 default: 2129 break; 2130 } 2131 vm_map_simplify_entry(map, current); 2132 } 2133 vm_map_unlock(map); 2134 } else { 2135 vm_pindex_t pstart, pend; 2136 2137 /* 2138 * madvise behaviors that are implemented in the underlying 2139 * vm_object. 2140 * 2141 * Since we don't clip the vm_map_entry, we have to clip 2142 * the vm_object pindex and count. 2143 */ 2144 for (current = entry; 2145 (current != &map->header) && (current->start < end); 2146 current = current->next 2147 ) { 2148 vm_offset_t useEnd, useStart; 2149 2150 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 2151 continue; 2152 2153 pstart = OFF_TO_IDX(current->offset); 2154 pend = pstart + atop(current->end - current->start); 2155 useStart = current->start; 2156 useEnd = current->end; 2157 2158 if (current->start < start) { 2159 pstart += atop(start - current->start); 2160 useStart = start; 2161 } 2162 if (current->end > end) { 2163 pend -= atop(current->end - end); 2164 useEnd = end; 2165 } 2166 2167 if (pstart >= pend) 2168 continue; 2169 2170 /* 2171 * Perform the pmap_advise() before clearing 2172 * PGA_REFERENCED in vm_page_advise(). Otherwise, a 2173 * concurrent pmap operation, such as pmap_remove(), 2174 * could clear a reference in the pmap and set 2175 * PGA_REFERENCED on the page before the pmap_advise() 2176 * had completed. Consequently, the page would appear 2177 * referenced based upon an old reference that 2178 * occurred before this pmap_advise() ran. 2179 */ 2180 if (behav == MADV_DONTNEED || behav == MADV_FREE) 2181 pmap_advise(map->pmap, useStart, useEnd, 2182 behav); 2183 2184 vm_object_madvise(current->object.vm_object, pstart, 2185 pend, behav); 2186 if (behav == MADV_WILLNEED) { 2187 vm_map_pmap_enter(map, 2188 useStart, 2189 current->protection, 2190 current->object.vm_object, 2191 pstart, 2192 ptoa(pend - pstart), 2193 MAP_PREFAULT_MADVISE 2194 ); 2195 } 2196 } 2197 vm_map_unlock_read(map); 2198 } 2199 return (0); 2200} 2201 2202 2203/* 2204 * vm_map_inherit: 2205 * 2206 * Sets the inheritance of the specified address 2207 * range in the target map. Inheritance 2208 * affects how the map will be shared with 2209 * child maps at the time of vmspace_fork. 2210 */ 2211int 2212vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 2213 vm_inherit_t new_inheritance) 2214{ 2215 vm_map_entry_t entry; 2216 vm_map_entry_t temp_entry; 2217 2218 switch (new_inheritance) { 2219 case VM_INHERIT_NONE: 2220 case VM_INHERIT_COPY: 2221 case VM_INHERIT_SHARE: 2222 break; 2223 default: 2224 return (KERN_INVALID_ARGUMENT); 2225 } 2226 if (start == end) 2227 return (KERN_SUCCESS); 2228 vm_map_lock(map); 2229 VM_MAP_RANGE_CHECK(map, start, end); 2230 if (vm_map_lookup_entry(map, start, &temp_entry)) { 2231 entry = temp_entry; 2232 vm_map_clip_start(map, entry, start); 2233 } else 2234 entry = temp_entry->next; 2235 while ((entry != &map->header) && (entry->start < end)) { 2236 vm_map_clip_end(map, entry, end); 2237 entry->inheritance = new_inheritance; 2238 vm_map_simplify_entry(map, entry); 2239 entry = entry->next; 2240 } 2241 vm_map_unlock(map); 2242 return (KERN_SUCCESS); 2243} 2244 2245/* 2246 * vm_map_unwire: 2247 * 2248 * Implements both kernel and user unwiring. 2249 */ 2250int 2251vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 2252 int flags) 2253{ 2254 vm_map_entry_t entry, first_entry, tmp_entry; 2255 vm_offset_t saved_start; 2256 unsigned int last_timestamp; 2257 int rv; 2258 boolean_t need_wakeup, result, user_unwire; 2259 2260 if (start == end) 2261 return (KERN_SUCCESS); 2262 user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 2263 vm_map_lock(map); 2264 VM_MAP_RANGE_CHECK(map, start, end); 2265 if (!vm_map_lookup_entry(map, start, &first_entry)) { 2266 if (flags & VM_MAP_WIRE_HOLESOK) 2267 first_entry = first_entry->next; 2268 else { 2269 vm_map_unlock(map); 2270 return (KERN_INVALID_ADDRESS); 2271 } 2272 } 2273 last_timestamp = map->timestamp; 2274 entry = first_entry; 2275 while (entry != &map->header && entry->start < end) { 2276 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 2277 /* 2278 * We have not yet clipped the entry. 2279 */ 2280 saved_start = (start >= entry->start) ? start : 2281 entry->start; 2282 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2283 if (vm_map_unlock_and_wait(map, 0)) { 2284 /* 2285 * Allow interruption of user unwiring? 2286 */ 2287 } 2288 vm_map_lock(map); 2289 if (last_timestamp+1 != map->timestamp) { 2290 /* 2291 * Look again for the entry because the map was 2292 * modified while it was unlocked. 2293 * Specifically, the entry may have been 2294 * clipped, merged, or deleted. 2295 */ 2296 if (!vm_map_lookup_entry(map, saved_start, 2297 &tmp_entry)) { 2298 if (flags & VM_MAP_WIRE_HOLESOK) 2299 tmp_entry = tmp_entry->next; 2300 else { 2301 if (saved_start == start) { 2302 /* 2303 * First_entry has been deleted. 2304 */ 2305 vm_map_unlock(map); 2306 return (KERN_INVALID_ADDRESS); 2307 } 2308 end = saved_start; 2309 rv = KERN_INVALID_ADDRESS; 2310 goto done; 2311 } 2312 } 2313 if (entry == first_entry) 2314 first_entry = tmp_entry; 2315 else 2316 first_entry = NULL; 2317 entry = tmp_entry; 2318 } 2319 last_timestamp = map->timestamp; 2320 continue; 2321 } 2322 vm_map_clip_start(map, entry, start); 2323 vm_map_clip_end(map, entry, end); 2324 /* 2325 * Mark the entry in case the map lock is released. (See 2326 * above.) 2327 */ 2328 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 && 2329 entry->wiring_thread == NULL, 2330 ("owned map entry %p", entry)); 2331 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 2332 entry->wiring_thread = curthread; 2333 /* 2334 * Check the map for holes in the specified region. 2335 * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 2336 */ 2337 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2338 (entry->end < end && (entry->next == &map->header || 2339 entry->next->start > entry->end))) { 2340 end = entry->end; 2341 rv = KERN_INVALID_ADDRESS; 2342 goto done; 2343 } 2344 /* 2345 * If system unwiring, require that the entry is system wired. 2346 */ 2347 if (!user_unwire && 2348 vm_map_entry_system_wired_count(entry) == 0) { 2349 end = entry->end; 2350 rv = KERN_INVALID_ARGUMENT; 2351 goto done; 2352 } 2353 entry = entry->next; 2354 } 2355 rv = KERN_SUCCESS; 2356done: 2357 need_wakeup = FALSE; 2358 if (first_entry == NULL) { 2359 result = vm_map_lookup_entry(map, start, &first_entry); 2360 if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2361 first_entry = first_entry->next; 2362 else 2363 KASSERT(result, ("vm_map_unwire: lookup failed")); 2364 } 2365 for (entry = first_entry; entry != &map->header && entry->start < end; 2366 entry = entry->next) { 2367 /* 2368 * If VM_MAP_WIRE_HOLESOK was specified, an empty 2369 * space in the unwired region could have been mapped 2370 * while the map lock was dropped for draining 2371 * MAP_ENTRY_IN_TRANSITION. Moreover, another thread 2372 * could be simultaneously wiring this new mapping 2373 * entry. Detect these cases and skip any entries 2374 * marked as in transition by us. 2375 */ 2376 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 || 2377 entry->wiring_thread != curthread) { 2378 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0, 2379 ("vm_map_unwire: !HOLESOK and new/changed entry")); 2380 continue; 2381 } 2382 2383 if (rv == KERN_SUCCESS && (!user_unwire || 2384 (entry->eflags & MAP_ENTRY_USER_WIRED))) { 2385 if (user_unwire) 2386 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2387 entry->wired_count--; 2388 if (entry->wired_count == 0) { 2389 /* 2390 * Retain the map lock. 2391 */ 2392 vm_fault_unwire(map, entry->start, entry->end, 2393 entry->object.vm_object != NULL && 2394 (entry->object.vm_object->flags & 2395 OBJ_FICTITIOUS) != 0); 2396 } 2397 } 2398 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0, 2399 ("vm_map_unwire: in-transition flag missing %p", entry)); 2400 KASSERT(entry->wiring_thread == curthread, 2401 ("vm_map_unwire: alien wire %p", entry)); 2402 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 2403 entry->wiring_thread = NULL; 2404 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 2405 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 2406 need_wakeup = TRUE; 2407 } 2408 vm_map_simplify_entry(map, entry); 2409 } 2410 vm_map_unlock(map); 2411 if (need_wakeup) 2412 vm_map_wakeup(map); 2413 return (rv); 2414} 2415 2416/* 2417 * vm_map_wire: 2418 * 2419 * Implements both kernel and user wiring. 2420 */ 2421int 2422vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 2423 int flags) 2424{ 2425 vm_map_entry_t entry, first_entry, tmp_entry; 2426 vm_offset_t saved_end, saved_start; 2427 unsigned int last_timestamp; 2428 int rv; 2429 boolean_t fictitious, need_wakeup, result, user_wire; 2430 vm_prot_t prot; 2431 2432 if (start == end) 2433 return (KERN_SUCCESS); 2434 prot = 0; 2435 if (flags & VM_MAP_WIRE_WRITE) 2436 prot |= VM_PROT_WRITE; 2437 user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 2438 vm_map_lock(map); 2439 VM_MAP_RANGE_CHECK(map, start, end); 2440 if (!vm_map_lookup_entry(map, start, &first_entry)) { 2441 if (flags & VM_MAP_WIRE_HOLESOK) 2442 first_entry = first_entry->next; 2443 else { 2444 vm_map_unlock(map); 2445 return (KERN_INVALID_ADDRESS); 2446 } 2447 } 2448 last_timestamp = map->timestamp; 2449 entry = first_entry; 2450 while (entry != &map->header && entry->start < end) { 2451 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 2452 /* 2453 * We have not yet clipped the entry. 2454 */ 2455 saved_start = (start >= entry->start) ? start : 2456 entry->start; 2457 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2458 if (vm_map_unlock_and_wait(map, 0)) { 2459 /* 2460 * Allow interruption of user wiring? 2461 */ 2462 } 2463 vm_map_lock(map); 2464 if (last_timestamp + 1 != map->timestamp) { 2465 /* 2466 * Look again for the entry because the map was 2467 * modified while it was unlocked. 2468 * Specifically, the entry may have been 2469 * clipped, merged, or deleted. 2470 */ 2471 if (!vm_map_lookup_entry(map, saved_start, 2472 &tmp_entry)) { 2473 if (flags & VM_MAP_WIRE_HOLESOK) 2474 tmp_entry = tmp_entry->next; 2475 else { 2476 if (saved_start == start) { 2477 /* 2478 * first_entry has been deleted. 2479 */ 2480 vm_map_unlock(map); 2481 return (KERN_INVALID_ADDRESS); 2482 } 2483 end = saved_start; 2484 rv = KERN_INVALID_ADDRESS; 2485 goto done; 2486 } 2487 } 2488 if (entry == first_entry) 2489 first_entry = tmp_entry; 2490 else 2491 first_entry = NULL; 2492 entry = tmp_entry; 2493 } 2494 last_timestamp = map->timestamp; 2495 continue; 2496 } 2497 vm_map_clip_start(map, entry, start); 2498 vm_map_clip_end(map, entry, end); 2499 /* 2500 * Mark the entry in case the map lock is released. (See 2501 * above.) 2502 */ 2503 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 && 2504 entry->wiring_thread == NULL, 2505 ("owned map entry %p", entry)); 2506 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 2507 entry->wiring_thread = curthread; 2508 if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 2509 || (entry->protection & prot) != prot) { 2510 entry->eflags |= MAP_ENTRY_WIRE_SKIPPED; 2511 if ((flags & VM_MAP_WIRE_HOLESOK) == 0) { 2512 end = entry->end; 2513 rv = KERN_INVALID_ADDRESS; 2514 goto done; 2515 } 2516 goto next_entry; 2517 } 2518 if (entry->wired_count == 0) { 2519 entry->wired_count++; 2520 saved_start = entry->start; 2521 saved_end = entry->end; 2522 fictitious = entry->object.vm_object != NULL && 2523 (entry->object.vm_object->flags & 2524 OBJ_FICTITIOUS) != 0; 2525 /* 2526 * Release the map lock, relying on the in-transition 2527 * mark. Mark the map busy for fork. 2528 */ 2529 vm_map_busy(map); 2530 vm_map_unlock(map); 2531 rv = vm_fault_wire(map, saved_start, saved_end, 2532 fictitious); 2533 vm_map_lock(map); 2534 vm_map_unbusy(map); 2535 if (last_timestamp + 1 != map->timestamp) { 2536 /* 2537 * Look again for the entry because the map was 2538 * modified while it was unlocked. The entry 2539 * may have been clipped, but NOT merged or 2540 * deleted. 2541 */ 2542 result = vm_map_lookup_entry(map, saved_start, 2543 &tmp_entry); 2544 KASSERT(result, ("vm_map_wire: lookup failed")); 2545 if (entry == first_entry) 2546 first_entry = tmp_entry; 2547 else 2548 first_entry = NULL; 2549 entry = tmp_entry; 2550 while (entry->end < saved_end) { 2551 if (rv != KERN_SUCCESS) { 2552 KASSERT(entry->wired_count == 1, 2553 ("vm_map_wire: bad count")); 2554 entry->wired_count = -1; 2555 } 2556 entry = entry->next; 2557 } 2558 } 2559 last_timestamp = map->timestamp; 2560 if (rv != KERN_SUCCESS) { 2561 KASSERT(entry->wired_count == 1, 2562 ("vm_map_wire: bad count")); 2563 /* 2564 * Assign an out-of-range value to represent 2565 * the failure to wire this entry. 2566 */ 2567 entry->wired_count = -1; 2568 end = entry->end; 2569 goto done; 2570 } 2571 } else if (!user_wire || 2572 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 2573 entry->wired_count++; 2574 } 2575 /* 2576 * Check the map for holes in the specified region. 2577 * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 2578 */ 2579 next_entry: 2580 if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2581 (entry->end < end && (entry->next == &map->header || 2582 entry->next->start > entry->end))) { 2583 end = entry->end; 2584 rv = KERN_INVALID_ADDRESS; 2585 goto done; 2586 } 2587 entry = entry->next; 2588 } 2589 rv = KERN_SUCCESS; 2590done: 2591 need_wakeup = FALSE; 2592 if (first_entry == NULL) { 2593 result = vm_map_lookup_entry(map, start, &first_entry); 2594 if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2595 first_entry = first_entry->next; 2596 else 2597 KASSERT(result, ("vm_map_wire: lookup failed")); 2598 } 2599 for (entry = first_entry; entry != &map->header && entry->start < end; 2600 entry = entry->next) { 2601 if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0) 2602 goto next_entry_done; 2603 2604 /* 2605 * If VM_MAP_WIRE_HOLESOK was specified, an empty 2606 * space in the unwired region could have been mapped 2607 * while the map lock was dropped for faulting in the 2608 * pages or draining MAP_ENTRY_IN_TRANSITION. 2609 * Moreover, another thread could be simultaneously 2610 * wiring this new mapping entry. Detect these cases 2611 * and skip any entries marked as in transition by us. 2612 */ 2613 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 || 2614 entry->wiring_thread != curthread) { 2615 KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0, 2616 ("vm_map_wire: !HOLESOK and new/changed entry")); 2617 continue; 2618 } 2619 2620 if (rv == KERN_SUCCESS) { 2621 if (user_wire) 2622 entry->eflags |= MAP_ENTRY_USER_WIRED; 2623 } else if (entry->wired_count == -1) { 2624 /* 2625 * Wiring failed on this entry. Thus, unwiring is 2626 * unnecessary. 2627 */ 2628 entry->wired_count = 0; 2629 } else { 2630 if (!user_wire || 2631 (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) 2632 entry->wired_count--; 2633 if (entry->wired_count == 0) { 2634 /* 2635 * Retain the map lock. 2636 */ 2637 vm_fault_unwire(map, entry->start, entry->end, 2638 entry->object.vm_object != NULL && 2639 (entry->object.vm_object->flags & 2640 OBJ_FICTITIOUS) != 0); 2641 } 2642 } 2643 next_entry_done: 2644 KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0, 2645 ("vm_map_wire: in-transition flag missing %p", entry)); 2646 KASSERT(entry->wiring_thread == curthread, 2647 ("vm_map_wire: alien wire %p", entry)); 2648 entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION | 2649 MAP_ENTRY_WIRE_SKIPPED); 2650 entry->wiring_thread = NULL; 2651 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 2652 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 2653 need_wakeup = TRUE; 2654 } 2655 vm_map_simplify_entry(map, entry); 2656 } 2657 vm_map_unlock(map); 2658 if (need_wakeup) 2659 vm_map_wakeup(map); 2660 return (rv); 2661} 2662 2663/* 2664 * vm_map_sync 2665 * 2666 * Push any dirty cached pages in the address range to their pager. 2667 * If syncio is TRUE, dirty pages are written synchronously. 2668 * If invalidate is TRUE, any cached pages are freed as well. 2669 * 2670 * If the size of the region from start to end is zero, we are 2671 * supposed to flush all modified pages within the region containing 2672 * start. Unfortunately, a region can be split or coalesced with 2673 * neighboring regions, making it difficult to determine what the 2674 * original region was. Therefore, we approximate this requirement by 2675 * flushing the current region containing start. 2676 * 2677 * Returns an error if any part of the specified range is not mapped. 2678 */ 2679int 2680vm_map_sync( 2681 vm_map_t map, 2682 vm_offset_t start, 2683 vm_offset_t end, 2684 boolean_t syncio, 2685 boolean_t invalidate) 2686{ 2687 vm_map_entry_t current; 2688 vm_map_entry_t entry; 2689 vm_size_t size; 2690 vm_object_t object; 2691 vm_ooffset_t offset; 2692 unsigned int last_timestamp; 2693 boolean_t failed; 2694 2695 vm_map_lock_read(map); 2696 VM_MAP_RANGE_CHECK(map, start, end); 2697 if (!vm_map_lookup_entry(map, start, &entry)) { 2698 vm_map_unlock_read(map); 2699 return (KERN_INVALID_ADDRESS); 2700 } else if (start == end) { 2701 start = entry->start; 2702 end = entry->end; 2703 } 2704 /* 2705 * Make a first pass to check for user-wired memory and holes. 2706 */ 2707 for (current = entry; current != &map->header && current->start < end; 2708 current = current->next) { 2709 if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) { 2710 vm_map_unlock_read(map); 2711 return (KERN_INVALID_ARGUMENT); 2712 } 2713 if (end > current->end && 2714 (current->next == &map->header || 2715 current->end != current->next->start)) { 2716 vm_map_unlock_read(map); 2717 return (KERN_INVALID_ADDRESS); 2718 } 2719 } 2720 2721 if (invalidate) 2722 pmap_remove(map->pmap, start, end); 2723 failed = FALSE; 2724 2725 /* 2726 * Make a second pass, cleaning/uncaching pages from the indicated 2727 * objects as we go. 2728 */ 2729 for (current = entry; current != &map->header && current->start < end;) { 2730 offset = current->offset + (start - current->start); 2731 size = (end <= current->end ? end : current->end) - start; 2732 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2733 vm_map_t smap; 2734 vm_map_entry_t tentry; 2735 vm_size_t tsize; 2736 2737 smap = current->object.sub_map; 2738 vm_map_lock_read(smap); 2739 (void) vm_map_lookup_entry(smap, offset, &tentry); 2740 tsize = tentry->end - offset; 2741 if (tsize < size) 2742 size = tsize; 2743 object = tentry->object.vm_object; 2744 offset = tentry->offset + (offset - tentry->start); 2745 vm_map_unlock_read(smap); 2746 } else { 2747 object = current->object.vm_object; 2748 } 2749 vm_object_reference(object); 2750 last_timestamp = map->timestamp; 2751 vm_map_unlock_read(map); 2752 if (!vm_object_sync(object, offset, size, syncio, invalidate)) 2753 failed = TRUE; 2754 start += size; 2755 vm_object_deallocate(object); 2756 vm_map_lock_read(map); 2757 if (last_timestamp == map->timestamp || 2758 !vm_map_lookup_entry(map, start, ¤t)) 2759 current = current->next; 2760 } 2761 2762 vm_map_unlock_read(map); 2763 return (failed ? KERN_FAILURE : KERN_SUCCESS); 2764} 2765 2766/* 2767 * vm_map_entry_unwire: [ internal use only ] 2768 * 2769 * Make the region specified by this entry pageable. 2770 * 2771 * The map in question should be locked. 2772 * [This is the reason for this routine's existence.] 2773 */ 2774static void 2775vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2776{ 2777 vm_fault_unwire(map, entry->start, entry->end, 2778 entry->object.vm_object != NULL && 2779 (entry->object.vm_object->flags & OBJ_FICTITIOUS) != 0); 2780 entry->wired_count = 0; 2781} 2782 2783static void 2784vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map) 2785{ 2786 2787 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) 2788 vm_object_deallocate(entry->object.vm_object); 2789 uma_zfree(system_map ? kmapentzone : mapentzone, entry); 2790} 2791 2792/* 2793 * vm_map_entry_delete: [ internal use only ] 2794 * 2795 * Deallocate the given entry from the target map. 2796 */ 2797static void 2798vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2799{ 2800 vm_object_t object; 2801 vm_pindex_t offidxstart, offidxend, count, size1; 2802 vm_ooffset_t size; 2803 2804 vm_map_entry_unlink(map, entry); 2805 object = entry->object.vm_object; 2806 size = entry->end - entry->start; 2807 map->size -= size; 2808 2809 if (entry->cred != NULL) { 2810 swap_release_by_cred(size, entry->cred); 2811 crfree(entry->cred); 2812 } 2813 2814 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 2815 (object != NULL)) { 2816 KASSERT(entry->cred == NULL || object->cred == NULL || 2817 (entry->eflags & MAP_ENTRY_NEEDS_COPY), 2818 ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry)); 2819 count = OFF_TO_IDX(size); 2820 offidxstart = OFF_TO_IDX(entry->offset); 2821 offidxend = offidxstart + count; 2822 VM_OBJECT_WLOCK(object); 2823 if (object->ref_count != 1 && 2824 ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING || 2825 object == kernel_object || object == kmem_object)) { 2826 vm_object_collapse(object); 2827 2828 /* 2829 * The option OBJPR_NOTMAPPED can be passed here 2830 * because vm_map_delete() already performed 2831 * pmap_remove() on the only mapping to this range 2832 * of pages. 2833 */ 2834 vm_object_page_remove(object, offidxstart, offidxend, 2835 OBJPR_NOTMAPPED); 2836 if (object->type == OBJT_SWAP) 2837 swap_pager_freespace(object, offidxstart, count); 2838 if (offidxend >= object->size && 2839 offidxstart < object->size) { 2840 size1 = object->size; 2841 object->size = offidxstart; 2842 if (object->cred != NULL) { 2843 size1 -= object->size; 2844 KASSERT(object->charge >= ptoa(size1), 2845 ("vm_map_entry_delete: object->charge < 0")); 2846 swap_release_by_cred(ptoa(size1), object->cred); 2847 object->charge -= ptoa(size1); 2848 } 2849 } 2850 } 2851 VM_OBJECT_WUNLOCK(object); 2852 } else 2853 entry->object.vm_object = NULL; 2854 if (map->system_map) 2855 vm_map_entry_deallocate(entry, TRUE); 2856 else { 2857 entry->next = curthread->td_map_def_user; 2858 curthread->td_map_def_user = entry; 2859 } 2860} 2861 2862/* 2863 * vm_map_delete: [ internal use only ] 2864 * 2865 * Deallocates the given address range from the target 2866 * map. 2867 */ 2868int 2869vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 2870{ 2871 vm_map_entry_t entry; 2872 vm_map_entry_t first_entry; 2873 2874 VM_MAP_ASSERT_LOCKED(map); 2875 if (start == end) 2876 return (KERN_SUCCESS); 2877 2878 /* 2879 * Find the start of the region, and clip it 2880 */ 2881 if (!vm_map_lookup_entry(map, start, &first_entry)) 2882 entry = first_entry->next; 2883 else { 2884 entry = first_entry; 2885 vm_map_clip_start(map, entry, start); 2886 } 2887 2888 /* 2889 * Step through all entries in this region 2890 */ 2891 while ((entry != &map->header) && (entry->start < end)) { 2892 vm_map_entry_t next; 2893 2894 /* 2895 * Wait for wiring or unwiring of an entry to complete. 2896 * Also wait for any system wirings to disappear on 2897 * user maps. 2898 */ 2899 if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 || 2900 (vm_map_pmap(map) != kernel_pmap && 2901 vm_map_entry_system_wired_count(entry) != 0)) { 2902 unsigned int last_timestamp; 2903 vm_offset_t saved_start; 2904 vm_map_entry_t tmp_entry; 2905 2906 saved_start = entry->start; 2907 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2908 last_timestamp = map->timestamp; 2909 (void) vm_map_unlock_and_wait(map, 0); 2910 vm_map_lock(map); 2911 if (last_timestamp + 1 != map->timestamp) { 2912 /* 2913 * Look again for the entry because the map was 2914 * modified while it was unlocked. 2915 * Specifically, the entry may have been 2916 * clipped, merged, or deleted. 2917 */ 2918 if (!vm_map_lookup_entry(map, saved_start, 2919 &tmp_entry)) 2920 entry = tmp_entry->next; 2921 else { 2922 entry = tmp_entry; 2923 vm_map_clip_start(map, entry, 2924 saved_start); 2925 } 2926 } 2927 continue; 2928 } 2929 vm_map_clip_end(map, entry, end); 2930 2931 next = entry->next; 2932 2933 /* 2934 * Unwire before removing addresses from the pmap; otherwise, 2935 * unwiring will put the entries back in the pmap. 2936 */ 2937 if (entry->wired_count != 0) { 2938 vm_map_entry_unwire(map, entry); 2939 } 2940 2941 pmap_remove(map->pmap, entry->start, entry->end); 2942 2943 /* 2944 * Delete the entry only after removing all pmap 2945 * entries pointing to its pages. (Otherwise, its 2946 * page frames may be reallocated, and any modify bits 2947 * will be set in the wrong object!) 2948 */ 2949 vm_map_entry_delete(map, entry); 2950 entry = next; 2951 } 2952 return (KERN_SUCCESS); 2953} 2954 2955/* 2956 * vm_map_remove: 2957 * 2958 * Remove the given address range from the target map. 2959 * This is the exported form of vm_map_delete. 2960 */ 2961int 2962vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 2963{ 2964 int result; 2965 2966 vm_map_lock(map); 2967 VM_MAP_RANGE_CHECK(map, start, end); 2968 result = vm_map_delete(map, start, end); 2969 vm_map_unlock(map); 2970 return (result); 2971} 2972 2973/* 2974 * vm_map_check_protection: 2975 * 2976 * Assert that the target map allows the specified privilege on the 2977 * entire address region given. The entire region must be allocated. 2978 * 2979 * WARNING! This code does not and should not check whether the 2980 * contents of the region is accessible. For example a smaller file 2981 * might be mapped into a larger address space. 2982 * 2983 * NOTE! This code is also called by munmap(). 2984 * 2985 * The map must be locked. A read lock is sufficient. 2986 */ 2987boolean_t 2988vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 2989 vm_prot_t protection) 2990{ 2991 vm_map_entry_t entry; 2992 vm_map_entry_t tmp_entry; 2993 2994 if (!vm_map_lookup_entry(map, start, &tmp_entry)) 2995 return (FALSE); 2996 entry = tmp_entry; 2997 2998 while (start < end) { 2999 if (entry == &map->header) 3000 return (FALSE); 3001 /* 3002 * No holes allowed! 3003 */ 3004 if (start < entry->start) 3005 return (FALSE); 3006 /* 3007 * Check protection associated with entry. 3008 */ 3009 if ((entry->protection & protection) != protection) 3010 return (FALSE); 3011 /* go to next entry */ 3012 start = entry->end; 3013 entry = entry->next; 3014 } 3015 return (TRUE); 3016} 3017 3018/* 3019 * vm_map_copy_entry: 3020 * 3021 * Copies the contents of the source entry to the destination 3022 * entry. The entries *must* be aligned properly. 3023 */ 3024static void 3025vm_map_copy_entry( 3026 vm_map_t src_map, 3027 vm_map_t dst_map, 3028 vm_map_entry_t src_entry, 3029 vm_map_entry_t dst_entry, 3030 vm_ooffset_t *fork_charge) 3031{ 3032 vm_object_t src_object; 3033 vm_map_entry_t fake_entry; 3034 vm_offset_t size; 3035 struct ucred *cred; 3036 int charged; 3037 3038 VM_MAP_ASSERT_LOCKED(dst_map); 3039 3040 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 3041 return; 3042 3043 if (src_entry->wired_count == 0 || 3044 (src_entry->protection & VM_PROT_WRITE) == 0) { 3045 /* 3046 * If the source entry is marked needs_copy, it is already 3047 * write-protected. 3048 */ 3049 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 && 3050 (src_entry->protection & VM_PROT_WRITE) != 0) { 3051 pmap_protect(src_map->pmap, 3052 src_entry->start, 3053 src_entry->end, 3054 src_entry->protection & ~VM_PROT_WRITE); 3055 } 3056 3057 /* 3058 * Make a copy of the object. 3059 */ 3060 size = src_entry->end - src_entry->start; 3061 if ((src_object = src_entry->object.vm_object) != NULL) { 3062 VM_OBJECT_WLOCK(src_object); 3063 charged = ENTRY_CHARGED(src_entry); 3064 if ((src_object->handle == NULL) && 3065 (src_object->type == OBJT_DEFAULT || 3066 src_object->type == OBJT_SWAP)) { 3067 vm_object_collapse(src_object); 3068 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 3069 vm_object_split(src_entry); 3070 src_object = src_entry->object.vm_object; 3071 } 3072 } 3073 vm_object_reference_locked(src_object); 3074 vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 3075 if (src_entry->cred != NULL && 3076 !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) { 3077 KASSERT(src_object->cred == NULL, 3078 ("OVERCOMMIT: vm_map_copy_entry: cred %p", 3079 src_object)); 3080 src_object->cred = src_entry->cred; 3081 src_object->charge = size; 3082 } 3083 VM_OBJECT_WUNLOCK(src_object); 3084 dst_entry->object.vm_object = src_object; 3085 if (charged) { 3086 cred = curthread->td_ucred; 3087 crhold(cred); 3088 dst_entry->cred = cred; 3089 *fork_charge += size; 3090 if (!(src_entry->eflags & 3091 MAP_ENTRY_NEEDS_COPY)) { 3092 crhold(cred); 3093 src_entry->cred = cred; 3094 *fork_charge += size; 3095 } 3096 } 3097 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 3098 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 3099 dst_entry->offset = src_entry->offset; 3100 if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) { 3101 /* 3102 * MAP_ENTRY_VN_WRITECNT cannot 3103 * indicate write reference from 3104 * src_entry, since the entry is 3105 * marked as needs copy. Allocate a 3106 * fake entry that is used to 3107 * decrement object->un_pager.vnp.writecount 3108 * at the appropriate time. Attach 3109 * fake_entry to the deferred list. 3110 */ 3111 fake_entry = vm_map_entry_create(dst_map); 3112 fake_entry->eflags = MAP_ENTRY_VN_WRITECNT; 3113 src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT; 3114 vm_object_reference(src_object); 3115 fake_entry->object.vm_object = src_object; 3116 fake_entry->start = src_entry->start; 3117 fake_entry->end = src_entry->end; 3118 fake_entry->next = curthread->td_map_def_user; 3119 curthread->td_map_def_user = fake_entry; 3120 } 3121 } else { 3122 dst_entry->object.vm_object = NULL; 3123 dst_entry->offset = 0; 3124 if (src_entry->cred != NULL) { 3125 dst_entry->cred = curthread->td_ucred; 3126 crhold(dst_entry->cred); 3127 *fork_charge += size; 3128 } 3129 } 3130 3131 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 3132 dst_entry->end - dst_entry->start, src_entry->start); 3133 } else { 3134 /* 3135 * We don't want to make writeable wired pages copy-on-write. 3136 * Immediately copy these pages into the new map by simulating 3137 * page faults. The new pages are pageable. 3138 */ 3139 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry, 3140 fork_charge); 3141 } 3142} 3143 3144/* 3145 * vmspace_map_entry_forked: 3146 * Update the newly-forked vmspace each time a map entry is inherited 3147 * or copied. The values for vm_dsize and vm_tsize are approximate 3148 * (and mostly-obsolete ideas in the face of mmap(2) et al.) 3149 */ 3150static void 3151vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2, 3152 vm_map_entry_t entry) 3153{ 3154 vm_size_t entrysize; 3155 vm_offset_t newend; 3156 3157 entrysize = entry->end - entry->start; 3158 vm2->vm_map.size += entrysize; 3159 if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) { 3160 vm2->vm_ssize += btoc(entrysize); 3161 } else if (entry->start >= (vm_offset_t)vm1->vm_daddr && 3162 entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) { 3163 newend = MIN(entry->end, 3164 (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)); 3165 vm2->vm_dsize += btoc(newend - entry->start); 3166 } else if (entry->start >= (vm_offset_t)vm1->vm_taddr && 3167 entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) { 3168 newend = MIN(entry->end, 3169 (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)); 3170 vm2->vm_tsize += btoc(newend - entry->start); 3171 } 3172} 3173 3174/* 3175 * vmspace_fork: 3176 * Create a new process vmspace structure and vm_map 3177 * based on those of an existing process. The new map 3178 * is based on the old map, according to the inheritance 3179 * values on the regions in that map. 3180 * 3181 * XXX It might be worth coalescing the entries added to the new vmspace. 3182 * 3183 * The source map must not be locked. 3184 */ 3185struct vmspace * 3186vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge) 3187{ 3188 struct vmspace *vm2; 3189 vm_map_t new_map, old_map; 3190 vm_map_entry_t new_entry, old_entry; 3191 vm_object_t object; 3192 int locked; 3193 3194 old_map = &vm1->vm_map; 3195 /* Copy immutable fields of vm1 to vm2. */ 3196 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset, NULL); 3197 if (vm2 == NULL) 3198 return (NULL); 3199 vm2->vm_taddr = vm1->vm_taddr; 3200 vm2->vm_daddr = vm1->vm_daddr; 3201 vm2->vm_maxsaddr = vm1->vm_maxsaddr; 3202 vm_map_lock(old_map); 3203 if (old_map->busy) 3204 vm_map_wait_busy(old_map); 3205 new_map = &vm2->vm_map; 3206 locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */ 3207 KASSERT(locked, ("vmspace_fork: lock failed")); 3208 3209 old_entry = old_map->header.next; 3210 3211 while (old_entry != &old_map->header) { 3212 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 3213 panic("vm_map_fork: encountered a submap"); 3214 3215 switch (old_entry->inheritance) { 3216 case VM_INHERIT_NONE: 3217 break; 3218 3219 case VM_INHERIT_SHARE: 3220 /* 3221 * Clone the entry, creating the shared object if necessary. 3222 */ 3223 object = old_entry->object.vm_object; 3224 if (object == NULL) { 3225 object = vm_object_allocate(OBJT_DEFAULT, 3226 atop(old_entry->end - old_entry->start)); 3227 old_entry->object.vm_object = object; 3228 old_entry->offset = 0; 3229 if (old_entry->cred != NULL) { 3230 object->cred = old_entry->cred; 3231 object->charge = old_entry->end - 3232 old_entry->start; 3233 old_entry->cred = NULL; 3234 } 3235 } 3236 3237 /* 3238 * Add the reference before calling vm_object_shadow 3239 * to insure that a shadow object is created. 3240 */ 3241 vm_object_reference(object); 3242 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3243 vm_object_shadow(&old_entry->object.vm_object, 3244 &old_entry->offset, 3245 old_entry->end - old_entry->start); 3246 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 3247 /* Transfer the second reference too. */ 3248 vm_object_reference( 3249 old_entry->object.vm_object); 3250 3251 /* 3252 * As in vm_map_simplify_entry(), the 3253 * vnode lock will not be acquired in 3254 * this call to vm_object_deallocate(). 3255 */ 3256 vm_object_deallocate(object); 3257 object = old_entry->object.vm_object; 3258 } 3259 VM_OBJECT_WLOCK(object); 3260 vm_object_clear_flag(object, OBJ_ONEMAPPING); 3261 if (old_entry->cred != NULL) { 3262 KASSERT(object->cred == NULL, ("vmspace_fork both cred")); 3263 object->cred = old_entry->cred; 3264 object->charge = old_entry->end - old_entry->start; 3265 old_entry->cred = NULL; 3266 } 3267 3268 /* 3269 * Assert the correct state of the vnode 3270 * v_writecount while the object is locked, to 3271 * not relock it later for the assertion 3272 * correctness. 3273 */ 3274 if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT && 3275 object->type == OBJT_VNODE) { 3276 KASSERT(((struct vnode *)object->handle)-> 3277 v_writecount > 0, 3278 ("vmspace_fork: v_writecount %p", object)); 3279 KASSERT(object->un_pager.vnp.writemappings > 0, 3280 ("vmspace_fork: vnp.writecount %p", 3281 object)); 3282 } 3283 VM_OBJECT_WUNLOCK(object); 3284 3285 /* 3286 * Clone the entry, referencing the shared object. 3287 */ 3288 new_entry = vm_map_entry_create(new_map); 3289 *new_entry = *old_entry; 3290 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED | 3291 MAP_ENTRY_IN_TRANSITION); 3292 new_entry->wiring_thread = NULL; 3293 new_entry->wired_count = 0; 3294 if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) { 3295 vnode_pager_update_writecount(object, 3296 new_entry->start, new_entry->end); 3297 } 3298 3299 /* 3300 * Insert the entry into the new map -- we know we're 3301 * inserting at the end of the new map. 3302 */ 3303 vm_map_entry_link(new_map, new_map->header.prev, 3304 new_entry); 3305 vmspace_map_entry_forked(vm1, vm2, new_entry); 3306 3307 /* 3308 * Update the physical map 3309 */ 3310 pmap_copy(new_map->pmap, old_map->pmap, 3311 new_entry->start, 3312 (old_entry->end - old_entry->start), 3313 old_entry->start); 3314 break; 3315 3316 case VM_INHERIT_COPY: 3317 /* 3318 * Clone the entry and link into the map. 3319 */ 3320 new_entry = vm_map_entry_create(new_map); 3321 *new_entry = *old_entry; 3322 /* 3323 * Copied entry is COW over the old object. 3324 */ 3325 new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED | 3326 MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT); 3327 new_entry->wiring_thread = NULL; 3328 new_entry->wired_count = 0; 3329 new_entry->object.vm_object = NULL; 3330 new_entry->cred = NULL; 3331 vm_map_entry_link(new_map, new_map->header.prev, 3332 new_entry); 3333 vmspace_map_entry_forked(vm1, vm2, new_entry); 3334 vm_map_copy_entry(old_map, new_map, old_entry, 3335 new_entry, fork_charge); 3336 break; 3337 } 3338 old_entry = old_entry->next; 3339 } 3340 /* 3341 * Use inlined vm_map_unlock() to postpone handling the deferred 3342 * map entries, which cannot be done until both old_map and 3343 * new_map locks are released. 3344 */ 3345 sx_xunlock(&old_map->lock); 3346 sx_xunlock(&new_map->lock); 3347 vm_map_process_deferred(); 3348 3349 return (vm2); 3350} 3351 3352int 3353vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 3354 vm_prot_t prot, vm_prot_t max, int cow) 3355{ 3356 vm_size_t growsize, init_ssize; 3357 rlim_t lmemlim, vmemlim; 3358 int rv; 3359 3360 growsize = sgrowsiz; 3361 init_ssize = (max_ssize < growsize) ? max_ssize : growsize; 3362 vm_map_lock(map); 3363 PROC_LOCK(curproc); 3364 lmemlim = lim_cur(curproc, RLIMIT_MEMLOCK); 3365 vmemlim = lim_cur(curproc, RLIMIT_VMEM); 3366 PROC_UNLOCK(curproc); 3367 if (!old_mlock && map->flags & MAP_WIREFUTURE) { 3368 if (ptoa(pmap_wired_count(map->pmap)) + init_ssize > lmemlim) { 3369 rv = KERN_NO_SPACE; 3370 goto out; 3371 } 3372 } 3373 /* If we would blow our VMEM resource limit, no go */ 3374 if (map->size + init_ssize > vmemlim) { 3375 rv = KERN_NO_SPACE; 3376 goto out; 3377 } 3378 rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot, 3379 max, cow); 3380out: 3381 vm_map_unlock(map); 3382 return (rv); 3383} 3384 3385static int 3386vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 3387 vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow) 3388{ 3389 vm_map_entry_t new_entry, prev_entry; 3390 vm_offset_t bot, top; 3391 vm_size_t init_ssize; 3392 int orient, rv; 3393 3394 /* 3395 * The stack orientation is piggybacked with the cow argument. 3396 * Extract it into orient and mask the cow argument so that we 3397 * don't pass it around further. 3398 * NOTE: We explicitly allow bi-directional stacks. 3399 */ 3400 orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP); 3401 KASSERT(orient != 0, ("No stack grow direction")); 3402 3403 if (addrbos < vm_map_min(map) || 3404 addrbos > vm_map_max(map) || 3405 addrbos + max_ssize < addrbos) 3406 return (KERN_NO_SPACE); 3407 3408 init_ssize = (max_ssize < growsize) ? max_ssize : growsize; 3409 3410 /* If addr is already mapped, no go */ 3411 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) 3412 return (KERN_NO_SPACE); 3413 3414 /* 3415 * If we can't accomodate max_ssize in the current mapping, no go. 3416 * However, we need to be aware that subsequent user mappings might 3417 * map into the space we have reserved for stack, and currently this 3418 * space is not protected. 3419 * 3420 * Hopefully we will at least detect this condition when we try to 3421 * grow the stack. 3422 */ 3423 if ((prev_entry->next != &map->header) && 3424 (prev_entry->next->start < addrbos + max_ssize)) 3425 return (KERN_NO_SPACE); 3426 3427 /* 3428 * We initially map a stack of only init_ssize. We will grow as 3429 * needed later. Depending on the orientation of the stack (i.e. 3430 * the grow direction) we either map at the top of the range, the 3431 * bottom of the range or in the middle. 3432 * 3433 * Note: we would normally expect prot and max to be VM_PROT_ALL, 3434 * and cow to be 0. Possibly we should eliminate these as input 3435 * parameters, and just pass these values here in the insert call. 3436 */ 3437 if (orient == MAP_STACK_GROWS_DOWN) 3438 bot = addrbos + max_ssize - init_ssize; 3439 else if (orient == MAP_STACK_GROWS_UP) 3440 bot = addrbos; 3441 else 3442 bot = round_page(addrbos + max_ssize/2 - init_ssize/2); 3443 top = bot + init_ssize; 3444 rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow); 3445 3446 /* Now set the avail_ssize amount. */ 3447 if (rv == KERN_SUCCESS) { 3448 if (prev_entry != &map->header) 3449 vm_map_clip_end(map, prev_entry, bot); 3450 new_entry = prev_entry->next; 3451 if (new_entry->end != top || new_entry->start != bot) 3452 panic("Bad entry start/end for new stack entry"); 3453 3454 new_entry->avail_ssize = max_ssize - init_ssize; 3455 if (orient & MAP_STACK_GROWS_DOWN) 3456 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 3457 if (orient & MAP_STACK_GROWS_UP) 3458 new_entry->eflags |= MAP_ENTRY_GROWS_UP; 3459 } 3460 3461 return (rv); 3462} 3463 3464static int stack_guard_page = 0; 3465TUNABLE_INT("security.bsd.stack_guard_page", &stack_guard_page); 3466SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RW, 3467 &stack_guard_page, 0, 3468 "Insert stack guard page ahead of the growable segments."); 3469 3470/* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 3471 * desired address is already mapped, or if we successfully grow 3472 * the stack. Also returns KERN_SUCCESS if addr is outside the 3473 * stack range (this is strange, but preserves compatibility with 3474 * the grow function in vm_machdep.c). 3475 */ 3476int 3477vm_map_growstack(struct proc *p, vm_offset_t addr) 3478{ 3479 vm_map_entry_t next_entry, prev_entry; 3480 vm_map_entry_t new_entry, stack_entry; 3481 struct vmspace *vm = p->p_vmspace; 3482 vm_map_t map = &vm->vm_map; 3483 vm_offset_t end; 3484 vm_size_t growsize; 3485 size_t grow_amount, max_grow; 3486 rlim_t lmemlim, stacklim, vmemlim; 3487 int is_procstack, rv; 3488 struct ucred *cred; 3489#ifdef notyet 3490 uint64_t limit; 3491#endif 3492#ifdef RACCT 3493 int error; 3494#endif 3495 3496Retry: 3497 PROC_LOCK(p); 3498 lmemlim = lim_cur(p, RLIMIT_MEMLOCK); 3499 stacklim = lim_cur(p, RLIMIT_STACK); 3500 vmemlim = lim_cur(p, RLIMIT_VMEM); 3501 PROC_UNLOCK(p); 3502 3503 vm_map_lock_read(map); 3504 3505 /* If addr is already in the entry range, no need to grow.*/ 3506 if (vm_map_lookup_entry(map, addr, &prev_entry)) { 3507 vm_map_unlock_read(map); 3508 return (KERN_SUCCESS); 3509 } 3510 3511 next_entry = prev_entry->next; 3512 if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) { 3513 /* 3514 * This entry does not grow upwards. Since the address lies 3515 * beyond this entry, the next entry (if one exists) has to 3516 * be a downward growable entry. The entry list header is 3517 * never a growable entry, so it suffices to check the flags. 3518 */ 3519 if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) { 3520 vm_map_unlock_read(map); 3521 return (KERN_SUCCESS); 3522 } 3523 stack_entry = next_entry; 3524 } else { 3525 /* 3526 * This entry grows upward. If the next entry does not at 3527 * least grow downwards, this is the entry we need to grow. 3528 * otherwise we have two possible choices and we have to 3529 * select one. 3530 */ 3531 if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) { 3532 /* 3533 * We have two choices; grow the entry closest to 3534 * the address to minimize the amount of growth. 3535 */ 3536 if (addr - prev_entry->end <= next_entry->start - addr) 3537 stack_entry = prev_entry; 3538 else 3539 stack_entry = next_entry; 3540 } else 3541 stack_entry = prev_entry; 3542 } 3543 3544 if (stack_entry == next_entry) { 3545 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo")); 3546 KASSERT(addr < stack_entry->start, ("foo")); 3547 end = (prev_entry != &map->header) ? prev_entry->end : 3548 stack_entry->start - stack_entry->avail_ssize; 3549 grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE); 3550 max_grow = stack_entry->start - end; 3551 } else { 3552 KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo")); 3553 KASSERT(addr >= stack_entry->end, ("foo")); 3554 end = (next_entry != &map->header) ? next_entry->start : 3555 stack_entry->end + stack_entry->avail_ssize; 3556 grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE); 3557 max_grow = end - stack_entry->end; 3558 } 3559 3560 if (grow_amount > stack_entry->avail_ssize) { 3561 vm_map_unlock_read(map); 3562 return (KERN_NO_SPACE); 3563 } 3564 3565 /* 3566 * If there is no longer enough space between the entries nogo, and 3567 * adjust the available space. Note: this should only happen if the 3568 * user has mapped into the stack area after the stack was created, 3569 * and is probably an error. 3570 * 3571 * This also effectively destroys any guard page the user might have 3572 * intended by limiting the stack size. 3573 */ 3574 if (grow_amount + (stack_guard_page ? PAGE_SIZE : 0) > max_grow) { 3575 if (vm_map_lock_upgrade(map)) 3576 goto Retry; 3577 3578 stack_entry->avail_ssize = max_grow; 3579 3580 vm_map_unlock(map); 3581 return (KERN_NO_SPACE); 3582 } 3583 3584 is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0; 3585 3586 /* 3587 * If this is the main process stack, see if we're over the stack 3588 * limit. 3589 */ 3590 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 3591 vm_map_unlock_read(map); 3592 return (KERN_NO_SPACE); 3593 } 3594#ifdef RACCT 3595 PROC_LOCK(p); 3596 if (is_procstack && 3597 racct_set(p, RACCT_STACK, ctob(vm->vm_ssize) + grow_amount)) { 3598 PROC_UNLOCK(p); 3599 vm_map_unlock_read(map); 3600 return (KERN_NO_SPACE); 3601 } 3602 PROC_UNLOCK(p); 3603#endif 3604 3605 /* Round up the grow amount modulo sgrowsiz */ 3606 growsize = sgrowsiz; 3607 grow_amount = roundup(grow_amount, growsize); 3608 if (grow_amount > stack_entry->avail_ssize) 3609 grow_amount = stack_entry->avail_ssize; 3610 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 3611 grow_amount = trunc_page((vm_size_t)stacklim) - 3612 ctob(vm->vm_ssize); 3613 } 3614#ifdef notyet 3615 PROC_LOCK(p); 3616 limit = racct_get_available(p, RACCT_STACK); 3617 PROC_UNLOCK(p); 3618 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit)) 3619 grow_amount = limit - ctob(vm->vm_ssize); 3620#endif 3621 if (!old_mlock && map->flags & MAP_WIREFUTURE) { 3622 if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) { 3623 vm_map_unlock_read(map); 3624 rv = KERN_NO_SPACE; 3625 goto out; 3626 } 3627#ifdef RACCT 3628 PROC_LOCK(p); 3629 if (racct_set(p, RACCT_MEMLOCK, 3630 ptoa(pmap_wired_count(map->pmap)) + grow_amount)) { 3631 PROC_UNLOCK(p); 3632 vm_map_unlock_read(map); 3633 rv = KERN_NO_SPACE; 3634 goto out; 3635 } 3636 PROC_UNLOCK(p); 3637#endif 3638 } 3639 /* If we would blow our VMEM resource limit, no go */ 3640 if (map->size + grow_amount > vmemlim) { 3641 vm_map_unlock_read(map); 3642 rv = KERN_NO_SPACE; 3643 goto out; 3644 } 3645#ifdef RACCT 3646 PROC_LOCK(p); 3647 if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) { 3648 PROC_UNLOCK(p); 3649 vm_map_unlock_read(map); 3650 rv = KERN_NO_SPACE; 3651 goto out; 3652 } 3653 PROC_UNLOCK(p); 3654#endif 3655 3656 if (vm_map_lock_upgrade(map)) 3657 goto Retry; 3658 3659 if (stack_entry == next_entry) { 3660 /* 3661 * Growing downward. 3662 */ 3663 /* Get the preliminary new entry start value */ 3664 addr = stack_entry->start - grow_amount; 3665 3666 /* 3667 * If this puts us into the previous entry, cut back our 3668 * growth to the available space. Also, see the note above. 3669 */ 3670 if (addr < end) { 3671 stack_entry->avail_ssize = max_grow; 3672 addr = end; 3673 if (stack_guard_page) 3674 addr += PAGE_SIZE; 3675 } 3676 3677 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 3678 next_entry->protection, next_entry->max_protection, 0); 3679 3680 /* Adjust the available stack space by the amount we grew. */ 3681 if (rv == KERN_SUCCESS) { 3682 if (prev_entry != &map->header) 3683 vm_map_clip_end(map, prev_entry, addr); 3684 new_entry = prev_entry->next; 3685 KASSERT(new_entry == stack_entry->prev, ("foo")); 3686 KASSERT(new_entry->end == stack_entry->start, ("foo")); 3687 KASSERT(new_entry->start == addr, ("foo")); 3688 grow_amount = new_entry->end - new_entry->start; 3689 new_entry->avail_ssize = stack_entry->avail_ssize - 3690 grow_amount; 3691 stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN; 3692 new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 3693 } 3694 } else { 3695 /* 3696 * Growing upward. 3697 */ 3698 addr = stack_entry->end + grow_amount; 3699 3700 /* 3701 * If this puts us into the next entry, cut back our growth 3702 * to the available space. Also, see the note above. 3703 */ 3704 if (addr > end) { 3705 stack_entry->avail_ssize = end - stack_entry->end; 3706 addr = end; 3707 if (stack_guard_page) 3708 addr -= PAGE_SIZE; 3709 } 3710 3711 grow_amount = addr - stack_entry->end; 3712 cred = stack_entry->cred; 3713 if (cred == NULL && stack_entry->object.vm_object != NULL) 3714 cred = stack_entry->object.vm_object->cred; 3715 if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred)) 3716 rv = KERN_NO_SPACE; 3717 /* Grow the underlying object if applicable. */ 3718 else if (stack_entry->object.vm_object == NULL || 3719 vm_object_coalesce(stack_entry->object.vm_object, 3720 stack_entry->offset, 3721 (vm_size_t)(stack_entry->end - stack_entry->start), 3722 (vm_size_t)grow_amount, cred != NULL)) { 3723 map->size += (addr - stack_entry->end); 3724 /* Update the current entry. */ 3725 stack_entry->end = addr; 3726 stack_entry->avail_ssize -= grow_amount; 3727 vm_map_entry_resize_free(map, stack_entry); 3728 rv = KERN_SUCCESS; 3729 3730 if (next_entry != &map->header) 3731 vm_map_clip_start(map, next_entry, addr); 3732 } else 3733 rv = KERN_FAILURE; 3734 } 3735 3736 if (rv == KERN_SUCCESS && is_procstack) 3737 vm->vm_ssize += btoc(grow_amount); 3738 3739 vm_map_unlock(map); 3740 3741 /* 3742 * Heed the MAP_WIREFUTURE flag if it was set for this process. 3743 */ 3744 if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) { 3745 vm_map_wire(map, 3746 (stack_entry == next_entry) ? addr : addr - grow_amount, 3747 (stack_entry == next_entry) ? stack_entry->start : addr, 3748 (p->p_flag & P_SYSTEM) 3749 ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES 3750 : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 3751 } 3752 3753out: 3754#ifdef RACCT 3755 if (rv != KERN_SUCCESS) { 3756 PROC_LOCK(p); 3757 error = racct_set(p, RACCT_VMEM, map->size); 3758 KASSERT(error == 0, ("decreasing RACCT_VMEM failed")); 3759 if (!old_mlock) { 3760 error = racct_set(p, RACCT_MEMLOCK, 3761 ptoa(pmap_wired_count(map->pmap))); 3762 KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed")); 3763 } 3764 error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize)); 3765 KASSERT(error == 0, ("decreasing RACCT_STACK failed")); 3766 PROC_UNLOCK(p); 3767 } 3768#endif 3769 3770 return (rv); 3771} 3772 3773/* 3774 * Unshare the specified VM space for exec. If other processes are 3775 * mapped to it, then create a new one. The new vmspace is null. 3776 */ 3777int 3778vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser) 3779{ 3780 struct vmspace *oldvmspace = p->p_vmspace; 3781 struct vmspace *newvmspace; 3782 3783 KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0, 3784 ("vmspace_exec recursed")); 3785 newvmspace = vmspace_alloc(minuser, maxuser, NULL); 3786 if (newvmspace == NULL) 3787 return (ENOMEM); 3788 newvmspace->vm_swrss = oldvmspace->vm_swrss; 3789 /* 3790 * This code is written like this for prototype purposes. The 3791 * goal is to avoid running down the vmspace here, but let the 3792 * other process's that are still using the vmspace to finally 3793 * run it down. Even though there is little or no chance of blocking 3794 * here, it is a good idea to keep this form for future mods. 3795 */ 3796 PROC_VMSPACE_LOCK(p); 3797 p->p_vmspace = newvmspace; 3798 PROC_VMSPACE_UNLOCK(p); 3799 if (p == curthread->td_proc) 3800 pmap_activate(curthread); 3801 curthread->td_pflags |= TDP_EXECVMSPC; 3802 return (0); 3803} 3804 3805/* 3806 * Unshare the specified VM space for forcing COW. This 3807 * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 3808 */ 3809int 3810vmspace_unshare(struct proc *p) 3811{ 3812 struct vmspace *oldvmspace = p->p_vmspace; 3813 struct vmspace *newvmspace; 3814 vm_ooffset_t fork_charge; 3815 3816 if (oldvmspace->vm_refcnt == 1) 3817 return (0); 3818 fork_charge = 0; 3819 newvmspace = vmspace_fork(oldvmspace, &fork_charge); 3820 if (newvmspace == NULL) 3821 return (ENOMEM); 3822 if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) { 3823 vmspace_free(newvmspace); 3824 return (ENOMEM); 3825 } 3826 PROC_VMSPACE_LOCK(p); 3827 p->p_vmspace = newvmspace; 3828 PROC_VMSPACE_UNLOCK(p); 3829 if (p == curthread->td_proc) 3830 pmap_activate(curthread); 3831 vmspace_free(oldvmspace); 3832 return (0); 3833} 3834 3835/* 3836 * vm_map_lookup: 3837 * 3838 * Finds the VM object, offset, and 3839 * protection for a given virtual address in the 3840 * specified map, assuming a page fault of the 3841 * type specified. 3842 * 3843 * Leaves the map in question locked for read; return 3844 * values are guaranteed until a vm_map_lookup_done 3845 * call is performed. Note that the map argument 3846 * is in/out; the returned map must be used in 3847 * the call to vm_map_lookup_done. 3848 * 3849 * A handle (out_entry) is returned for use in 3850 * vm_map_lookup_done, to make that fast. 3851 * 3852 * If a lookup is requested with "write protection" 3853 * specified, the map may be changed to perform virtual 3854 * copying operations, although the data referenced will 3855 * remain the same. 3856 */ 3857int 3858vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 3859 vm_offset_t vaddr, 3860 vm_prot_t fault_typea, 3861 vm_map_entry_t *out_entry, /* OUT */ 3862 vm_object_t *object, /* OUT */ 3863 vm_pindex_t *pindex, /* OUT */ 3864 vm_prot_t *out_prot, /* OUT */ 3865 boolean_t *wired) /* OUT */ 3866{ 3867 vm_map_entry_t entry; 3868 vm_map_t map = *var_map; 3869 vm_prot_t prot; 3870 vm_prot_t fault_type = fault_typea; 3871 vm_object_t eobject; 3872 vm_size_t size; 3873 struct ucred *cred; 3874 3875RetryLookup:; 3876 3877 vm_map_lock_read(map); 3878 3879 /* 3880 * Lookup the faulting address. 3881 */ 3882 if (!vm_map_lookup_entry(map, vaddr, out_entry)) { 3883 vm_map_unlock_read(map); 3884 return (KERN_INVALID_ADDRESS); 3885 } 3886 3887 entry = *out_entry; 3888 3889 /* 3890 * Handle submaps. 3891 */ 3892 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3893 vm_map_t old_map = map; 3894 3895 *var_map = map = entry->object.sub_map; 3896 vm_map_unlock_read(old_map); 3897 goto RetryLookup; 3898 } 3899 3900 /* 3901 * Check whether this task is allowed to have this page. 3902 */ 3903 prot = entry->protection; 3904 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 3905 if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) { 3906 vm_map_unlock_read(map); 3907 return (KERN_PROTECTION_FAILURE); 3908 } 3909 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 3910 (entry->eflags & MAP_ENTRY_COW) && 3911 (fault_type & VM_PROT_WRITE)) { 3912 vm_map_unlock_read(map); 3913 return (KERN_PROTECTION_FAILURE); 3914 } 3915 if ((fault_typea & VM_PROT_COPY) != 0 && 3916 (entry->max_protection & VM_PROT_WRITE) == 0 && 3917 (entry->eflags & MAP_ENTRY_COW) == 0) { 3918 vm_map_unlock_read(map); 3919 return (KERN_PROTECTION_FAILURE); 3920 } 3921 3922 /* 3923 * If this page is not pageable, we have to get it for all possible 3924 * accesses. 3925 */ 3926 *wired = (entry->wired_count != 0); 3927 if (*wired) 3928 fault_type = entry->protection; 3929 size = entry->end - entry->start; 3930 /* 3931 * If the entry was copy-on-write, we either ... 3932 */ 3933 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3934 /* 3935 * If we want to write the page, we may as well handle that 3936 * now since we've got the map locked. 3937 * 3938 * If we don't need to write the page, we just demote the 3939 * permissions allowed. 3940 */ 3941 if ((fault_type & VM_PROT_WRITE) != 0 || 3942 (fault_typea & VM_PROT_COPY) != 0) { 3943 /* 3944 * Make a new object, and place it in the object 3945 * chain. Note that no new references have appeared 3946 * -- one just moved from the map to the new 3947 * object. 3948 */ 3949 if (vm_map_lock_upgrade(map)) 3950 goto RetryLookup; 3951 3952 if (entry->cred == NULL) { 3953 /* 3954 * The debugger owner is charged for 3955 * the memory. 3956 */ 3957 cred = curthread->td_ucred; 3958 crhold(cred); 3959 if (!swap_reserve_by_cred(size, cred)) { 3960 crfree(cred); 3961 vm_map_unlock(map); 3962 return (KERN_RESOURCE_SHORTAGE); 3963 } 3964 entry->cred = cred; 3965 } 3966 vm_object_shadow(&entry->object.vm_object, 3967 &entry->offset, size); 3968 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 3969 eobject = entry->object.vm_object; 3970 if (eobject->cred != NULL) { 3971 /* 3972 * The object was not shadowed. 3973 */ 3974 swap_release_by_cred(size, entry->cred); 3975 crfree(entry->cred); 3976 entry->cred = NULL; 3977 } else if (entry->cred != NULL) { 3978 VM_OBJECT_WLOCK(eobject); 3979 eobject->cred = entry->cred; 3980 eobject->charge = size; 3981 VM_OBJECT_WUNLOCK(eobject); 3982 entry->cred = NULL; 3983 } 3984 3985 vm_map_lock_downgrade(map); 3986 } else { 3987 /* 3988 * We're attempting to read a copy-on-write page -- 3989 * don't allow writes. 3990 */ 3991 prot &= ~VM_PROT_WRITE; 3992 } 3993 } 3994 3995 /* 3996 * Create an object if necessary. 3997 */ 3998 if (entry->object.vm_object == NULL && 3999 !map->system_map) { 4000 if (vm_map_lock_upgrade(map)) 4001 goto RetryLookup; 4002 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 4003 atop(size)); 4004 entry->offset = 0; 4005 if (entry->cred != NULL) { 4006 VM_OBJECT_WLOCK(entry->object.vm_object); 4007 entry->object.vm_object->cred = entry->cred; 4008 entry->object.vm_object->charge = size; 4009 VM_OBJECT_WUNLOCK(entry->object.vm_object); 4010 entry->cred = NULL; 4011 } 4012 vm_map_lock_downgrade(map); 4013 } 4014 4015 /* 4016 * Return the object/offset from this entry. If the entry was 4017 * copy-on-write or empty, it has been fixed up. 4018 */ 4019 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 4020 *object = entry->object.vm_object; 4021 4022 *out_prot = prot; 4023 return (KERN_SUCCESS); 4024} 4025 4026/* 4027 * vm_map_lookup_locked: 4028 * 4029 * Lookup the faulting address. A version of vm_map_lookup that returns 4030 * KERN_FAILURE instead of blocking on map lock or memory allocation. 4031 */ 4032int 4033vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */ 4034 vm_offset_t vaddr, 4035 vm_prot_t fault_typea, 4036 vm_map_entry_t *out_entry, /* OUT */ 4037 vm_object_t *object, /* OUT */ 4038 vm_pindex_t *pindex, /* OUT */ 4039 vm_prot_t *out_prot, /* OUT */ 4040 boolean_t *wired) /* OUT */ 4041{ 4042 vm_map_entry_t entry; 4043 vm_map_t map = *var_map; 4044 vm_prot_t prot; 4045 vm_prot_t fault_type = fault_typea; 4046 4047 /* 4048 * Lookup the faulting address. 4049 */ 4050 if (!vm_map_lookup_entry(map, vaddr, out_entry)) 4051 return (KERN_INVALID_ADDRESS); 4052 4053 entry = *out_entry; 4054 4055 /* 4056 * Fail if the entry refers to a submap. 4057 */ 4058 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 4059 return (KERN_FAILURE); 4060 4061 /* 4062 * Check whether this task is allowed to have this page. 4063 */ 4064 prot = entry->protection; 4065 fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 4066 if ((fault_type & prot) != fault_type) 4067 return (KERN_PROTECTION_FAILURE); 4068 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 4069 (entry->eflags & MAP_ENTRY_COW) && 4070 (fault_type & VM_PROT_WRITE)) 4071 return (KERN_PROTECTION_FAILURE); 4072 4073 /* 4074 * If this page is not pageable, we have to get it for all possible 4075 * accesses. 4076 */ 4077 *wired = (entry->wired_count != 0); 4078 if (*wired) 4079 fault_type = entry->protection; 4080 4081 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 4082 /* 4083 * Fail if the entry was copy-on-write for a write fault. 4084 */ 4085 if (fault_type & VM_PROT_WRITE) 4086 return (KERN_FAILURE); 4087 /* 4088 * We're attempting to read a copy-on-write page -- 4089 * don't allow writes. 4090 */ 4091 prot &= ~VM_PROT_WRITE; 4092 } 4093 4094 /* 4095 * Fail if an object should be created. 4096 */ 4097 if (entry->object.vm_object == NULL && !map->system_map) 4098 return (KERN_FAILURE); 4099 4100 /* 4101 * Return the object/offset from this entry. If the entry was 4102 * copy-on-write or empty, it has been fixed up. 4103 */ 4104 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 4105 *object = entry->object.vm_object; 4106 4107 *out_prot = prot; 4108 return (KERN_SUCCESS); 4109} 4110 4111/* 4112 * vm_map_lookup_done: 4113 * 4114 * Releases locks acquired by a vm_map_lookup 4115 * (according to the handle returned by that lookup). 4116 */ 4117void 4118vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 4119{ 4120 /* 4121 * Unlock the main-level map 4122 */ 4123 vm_map_unlock_read(map); 4124} 4125 4126#include "opt_ddb.h" 4127#ifdef DDB 4128#include <sys/kernel.h> 4129 4130#include <ddb/ddb.h> 4131 4132static void 4133vm_map_print(vm_map_t map) 4134{ 4135 vm_map_entry_t entry; 4136 4137 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 4138 (void *)map, 4139 (void *)map->pmap, map->nentries, map->timestamp); 4140 4141 db_indent += 2; 4142 for (entry = map->header.next; entry != &map->header; 4143 entry = entry->next) { 4144 db_iprintf("map entry %p: start=%p, end=%p\n", 4145 (void *)entry, (void *)entry->start, (void *)entry->end); 4146 { 4147 static char *inheritance_name[4] = 4148 {"share", "copy", "none", "donate_copy"}; 4149 4150 db_iprintf(" prot=%x/%x/%s", 4151 entry->protection, 4152 entry->max_protection, 4153 inheritance_name[(int)(unsigned char)entry->inheritance]); 4154 if (entry->wired_count != 0) 4155 db_printf(", wired"); 4156 } 4157 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 4158 db_printf(", share=%p, offset=0x%jx\n", 4159 (void *)entry->object.sub_map, 4160 (uintmax_t)entry->offset); 4161 if ((entry->prev == &map->header) || 4162 (entry->prev->object.sub_map != 4163 entry->object.sub_map)) { 4164 db_indent += 2; 4165 vm_map_print((vm_map_t)entry->object.sub_map); 4166 db_indent -= 2; 4167 } 4168 } else { 4169 if (entry->cred != NULL) 4170 db_printf(", ruid %d", entry->cred->cr_ruid); 4171 db_printf(", object=%p, offset=0x%jx", 4172 (void *)entry->object.vm_object, 4173 (uintmax_t)entry->offset); 4174 if (entry->object.vm_object && entry->object.vm_object->cred) 4175 db_printf(", obj ruid %d charge %jx", 4176 entry->object.vm_object->cred->cr_ruid, 4177 (uintmax_t)entry->object.vm_object->charge); 4178 if (entry->eflags & MAP_ENTRY_COW) 4179 db_printf(", copy (%s)", 4180 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 4181 db_printf("\n"); 4182 4183 if ((entry->prev == &map->header) || 4184 (entry->prev->object.vm_object != 4185 entry->object.vm_object)) { 4186 db_indent += 2; 4187 vm_object_print((db_expr_t)(intptr_t) 4188 entry->object.vm_object, 4189 0, 0, (char *)0); 4190 db_indent -= 2; 4191 } 4192 } 4193 } 4194 db_indent -= 2; 4195} 4196 4197DB_SHOW_COMMAND(map, map) 4198{ 4199 4200 if (!have_addr) { 4201 db_printf("usage: show map <addr>\n"); 4202 return; 4203 } 4204 vm_map_print((vm_map_t)addr); 4205} 4206 4207DB_SHOW_COMMAND(procvm, procvm) 4208{ 4209 struct proc *p; 4210 4211 if (have_addr) { 4212 p = (struct proc *) addr; 4213 } else { 4214 p = curproc; 4215 } 4216 4217 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 4218 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 4219 (void *)vmspace_pmap(p->p_vmspace)); 4220 4221 vm_map_print((vm_map_t)&p->p_vmspace->vm_map); 4222} 4223 4224#endif /* DDB */ 4225