1 /* 2 * (MPSAFE) 3 * 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * The Mach Operating System project at Carnegie-Mellon University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94 35 * 36 * 37 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38 * All rights reserved. 39 * 40 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41 * 42 * Permission to use, copy, modify and distribute this software and 43 * its documentation is hereby granted, provided that both the copyright 44 * notice and this permission notice appear in all copies of the 45 * software, derivative works or modified versions, and any portions 46 * thereof, and that both notices appear in supporting documentation. 47 * 48 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51 * 52 * Carnegie Mellon requests users of this software to return to 53 * 54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55 * School of Computer Science 56 * Carnegie Mellon University 57 * Pittsburgh PA 15213-3890 58 * 59 * any improvements or extensions that they make and grant Carnegie the 60 * rights to redistribute these changes. 61 * 62 * $FreeBSD: src/sys/vm/vm_map.c,v 1.187.2.19 2003/05/27 00:47:02 alc Exp $ 63 */ 64 65 /* 66 * Virtual memory mapping module. 67 */ 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/proc.h> 73 #include <sys/serialize.h> 74 #include <sys/lock.h> 75 #include <sys/vmmeter.h> 76 #include <sys/mman.h> 77 #include <sys/vnode.h> 78 #include <sys/resourcevar.h> 79 #include <sys/shm.h> 80 #include <sys/tree.h> 81 #include <sys/malloc.h> 82 #include <sys/objcache.h> 83 84 #include <vm/vm.h> 85 #include <vm/vm_param.h> 86 #include <vm/pmap.h> 87 #include <vm/vm_map.h> 88 #include <vm/vm_page.h> 89 #include <vm/vm_object.h> 90 #include <vm/vm_pager.h> 91 #include <vm/vm_kern.h> 92 #include <vm/vm_extern.h> 93 #include <vm/swap_pager.h> 94 #include <vm/vm_zone.h> 95 96 #include <sys/random.h> 97 #include <sys/sysctl.h> 98 #include <sys/spinlock.h> 99 100 #include <sys/thread2.h> 101 #include <sys/spinlock2.h> 102 103 /* 104 * Virtual memory maps provide for the mapping, protection, and sharing 105 * of virtual memory objects. In addition, this module provides for an 106 * efficient virtual copy of memory from one map to another. 107 * 108 * Synchronization is required prior to most operations. 109 * 110 * Maps consist of an ordered doubly-linked list of simple entries. 111 * A hint and a RB tree is used to speed-up lookups. 112 * 113 * Callers looking to modify maps specify start/end addresses which cause 114 * the related map entry to be clipped if necessary, and then later 115 * recombined if the pieces remained compatible. 116 * 117 * Virtual copy operations are performed by copying VM object references 118 * from one map to another, and then marking both regions as copy-on-write. 119 */ 120 static boolean_t vmspace_ctor(void *obj, void *privdata, int ocflags); 121 static void vmspace_dtor(void *obj, void *privdata); 122 static void vmspace_terminate(struct vmspace *vm, int final); 123 124 MALLOC_DEFINE(M_VMSPACE, "vmspace", "vmspace objcache backingstore"); 125 static struct objcache *vmspace_cache; 126 127 /* 128 * per-cpu page table cross mappings are initialized in early boot 129 * and might require a considerable number of vm_map_entry structures. 130 */ 131 #define MAPENTRYBSP_CACHE (MAXCPU+1) 132 #define MAPENTRYAP_CACHE 8 133 134 static struct vm_zone mapentzone_store; 135 static vm_zone_t mapentzone; 136 static struct vm_object mapentobj; 137 138 static struct vm_map_entry map_entry_init[MAX_MAPENT]; 139 static struct vm_map_entry cpu_map_entry_init_bsp[MAPENTRYBSP_CACHE]; 140 static struct vm_map_entry cpu_map_entry_init_ap[MAXCPU][MAPENTRYAP_CACHE]; 141 142 static int randomize_mmap; 143 SYSCTL_INT(_vm, OID_AUTO, randomize_mmap, CTLFLAG_RW, &randomize_mmap, 0, 144 "Randomize mmap offsets"); 145 static int vm_map_relock_enable = 1; 146 SYSCTL_INT(_vm, OID_AUTO, map_relock_enable, CTLFLAG_RW, 147 &vm_map_relock_enable, 0, "Randomize mmap offsets"); 148 149 static void vmspace_drop_notoken(struct vmspace *vm); 150 static void vm_map_entry_shadow(vm_map_entry_t entry, int addref); 151 static vm_map_entry_t vm_map_entry_create(vm_map_t map, int *); 152 static void vm_map_entry_dispose (vm_map_t map, vm_map_entry_t entry, int *); 153 static void _vm_map_clip_end (vm_map_t, vm_map_entry_t, vm_offset_t, int *); 154 static void _vm_map_clip_start (vm_map_t, vm_map_entry_t, vm_offset_t, int *); 155 static void vm_map_entry_delete (vm_map_t, vm_map_entry_t, int *); 156 static void vm_map_entry_unwire (vm_map_t, vm_map_entry_t); 157 static void vm_map_copy_entry (vm_map_t, vm_map_t, vm_map_entry_t, 158 vm_map_entry_t); 159 static void vm_map_unclip_range (vm_map_t map, vm_map_entry_t start_entry, vm_offset_t start, vm_offset_t end, int *count, int flags); 160 161 /* 162 * Initialize the vm_map module. Must be called before any other vm_map 163 * routines. 164 * 165 * Map and entry structures are allocated from the general purpose 166 * memory pool with some exceptions: 167 * 168 * - The kernel map is allocated statically. 169 * - Initial kernel map entries are allocated out of a static pool. 170 * - We must set ZONE_SPECIAL here or the early boot code can get 171 * stuck if there are >63 cores. 172 * 173 * These restrictions are necessary since malloc() uses the 174 * maps and requires map entries. 175 * 176 * Called from the low level boot code only. 177 */ 178 void 179 vm_map_startup(void) 180 { 181 mapentzone = &mapentzone_store; 182 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry), 183 map_entry_init, MAX_MAPENT); 184 mapentzone_store.zflags |= ZONE_SPECIAL; 185 } 186 187 /* 188 * Called prior to any vmspace allocations. 189 * 190 * Called from the low level boot code only. 191 */ 192 void 193 vm_init2(void) 194 { 195 vmspace_cache = objcache_create_mbacked(M_VMSPACE, 196 sizeof(struct vmspace), 197 0, ncpus * 4, 198 vmspace_ctor, vmspace_dtor, 199 NULL); 200 zinitna(mapentzone, &mapentobj, NULL, 0, 0, 201 ZONE_USE_RESERVE | ZONE_SPECIAL); 202 pmap_init2(); 203 vm_object_init2(); 204 } 205 206 /* 207 * objcache support. We leave the pmap root cached as long as possible 208 * for performance reasons. 209 */ 210 static 211 boolean_t 212 vmspace_ctor(void *obj, void *privdata, int ocflags) 213 { 214 struct vmspace *vm = obj; 215 216 bzero(vm, sizeof(*vm)); 217 vm->vm_refcnt = VM_REF_DELETED; 218 219 return 1; 220 } 221 222 static 223 void 224 vmspace_dtor(void *obj, void *privdata) 225 { 226 struct vmspace *vm = obj; 227 228 KKASSERT(vm->vm_refcnt == VM_REF_DELETED); 229 pmap_puninit(vmspace_pmap(vm)); 230 } 231 232 /* 233 * Red black tree functions 234 * 235 * The caller must hold the related map lock. 236 */ 237 static int rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b); 238 RB_GENERATE(vm_map_rb_tree, vm_map_entry, rb_entry, rb_vm_map_compare); 239 240 /* a->start is address, and the only field has to be initialized */ 241 static int 242 rb_vm_map_compare(vm_map_entry_t a, vm_map_entry_t b) 243 { 244 if (a->start < b->start) 245 return(-1); 246 else if (a->start > b->start) 247 return(1); 248 return(0); 249 } 250 251 /* 252 * Initialize vmspace ref/hold counts vmspace0. There is a holdcnt for 253 * every refcnt. 254 */ 255 void 256 vmspace_initrefs(struct vmspace *vm) 257 { 258 vm->vm_refcnt = 1; 259 vm->vm_holdcnt = 1; 260 } 261 262 /* 263 * Allocate a vmspace structure, including a vm_map and pmap. 264 * Initialize numerous fields. While the initial allocation is zerod, 265 * subsequence reuse from the objcache leaves elements of the structure 266 * intact (particularly the pmap), so portions must be zerod. 267 * 268 * Returns a referenced vmspace. 269 * 270 * No requirements. 271 */ 272 struct vmspace * 273 vmspace_alloc(vm_offset_t min, vm_offset_t max) 274 { 275 struct vmspace *vm; 276 277 vm = objcache_get(vmspace_cache, M_WAITOK); 278 279 bzero(&vm->vm_startcopy, 280 (char *)&vm->vm_endcopy - (char *)&vm->vm_startcopy); 281 vm_map_init(&vm->vm_map, min, max, NULL); /* initializes token */ 282 283 /* 284 * NOTE: hold to acquires token for safety. 285 * 286 * On return vmspace is referenced (refs=1, hold=1). That is, 287 * each refcnt also has a holdcnt. There can be additional holds 288 * (holdcnt) above and beyond the refcnt. Finalization is handled in 289 * two stages, one on refs 1->0, and the the second on hold 1->0. 290 */ 291 KKASSERT(vm->vm_holdcnt == 0); 292 KKASSERT(vm->vm_refcnt == VM_REF_DELETED); 293 vmspace_initrefs(vm); 294 vmspace_hold(vm); 295 pmap_pinit(vmspace_pmap(vm)); /* (some fields reused) */ 296 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */ 297 vm->vm_shm = NULL; 298 vm->vm_flags = 0; 299 cpu_vmspace_alloc(vm); 300 vmspace_drop(vm); 301 302 return (vm); 303 } 304 305 /* 306 * NOTE: Can return 0 if the vmspace is exiting. 307 */ 308 int 309 vmspace_getrefs(struct vmspace *vm) 310 { 311 int32_t n; 312 313 n = vm->vm_refcnt; 314 cpu_ccfence(); 315 if (n & VM_REF_DELETED) 316 n = -1; 317 return n; 318 } 319 320 void 321 vmspace_hold(struct vmspace *vm) 322 { 323 atomic_add_int(&vm->vm_holdcnt, 1); 324 lwkt_gettoken(&vm->vm_map.token); 325 } 326 327 /* 328 * Drop with final termination interlock. 329 */ 330 void 331 vmspace_drop(struct vmspace *vm) 332 { 333 lwkt_reltoken(&vm->vm_map.token); 334 vmspace_drop_notoken(vm); 335 } 336 337 static void 338 vmspace_drop_notoken(struct vmspace *vm) 339 { 340 if (atomic_fetchadd_int(&vm->vm_holdcnt, -1) == 1) { 341 if (vm->vm_refcnt & VM_REF_DELETED) 342 vmspace_terminate(vm, 1); 343 } 344 } 345 346 /* 347 * A vmspace object must not be in a terminated state to be able to obtain 348 * additional refs on it. 349 * 350 * These are official references to the vmspace, the count is used to check 351 * for vmspace sharing. Foreign accessors should use 'hold' and not 'ref'. 352 * 353 * XXX we need to combine hold & ref together into one 64-bit field to allow 354 * holds to prevent stage-1 termination. 355 */ 356 void 357 vmspace_ref(struct vmspace *vm) 358 { 359 uint32_t n; 360 361 atomic_add_int(&vm->vm_holdcnt, 1); 362 n = atomic_fetchadd_int(&vm->vm_refcnt, 1); 363 KKASSERT((n & VM_REF_DELETED) == 0); 364 } 365 366 /* 367 * Release a ref on the vmspace. On the 1->0 transition we do stage-1 368 * termination of the vmspace. Then, on the final drop of the hold we 369 * will do stage-2 final termination. 370 */ 371 void 372 vmspace_rel(struct vmspace *vm) 373 { 374 uint32_t n; 375 376 /* 377 * Drop refs. Each ref also has a hold which is also dropped. 378 * 379 * When refs hits 0 compete to get the VM_REF_DELETED flag (hold 380 * prevent finalization) to start termination processing. 381 * Finalization occurs when the last hold count drops to 0. 382 */ 383 n = atomic_fetchadd_int(&vm->vm_refcnt, -1) - 1; 384 while (n == 0) { 385 if (atomic_cmpset_int(&vm->vm_refcnt, 0, VM_REF_DELETED)) { 386 vmspace_terminate(vm, 0); 387 break; 388 } 389 n = vm->vm_refcnt; 390 cpu_ccfence(); 391 } 392 vmspace_drop_notoken(vm); 393 } 394 395 /* 396 * This is called during exit indicating that the vmspace is no 397 * longer in used by an exiting process, but the process has not yet 398 * been reaped. 399 * 400 * We drop refs, allowing for stage-1 termination, but maintain a holdcnt 401 * to prevent stage-2 until the process is reaped. Note hte order of 402 * operation, we must hold first. 403 * 404 * No requirements. 405 */ 406 void 407 vmspace_relexit(struct vmspace *vm) 408 { 409 atomic_add_int(&vm->vm_holdcnt, 1); 410 vmspace_rel(vm); 411 } 412 413 /* 414 * Called during reap to disconnect the remainder of the vmspace from 415 * the process. On the hold drop the vmspace termination is finalized. 416 * 417 * No requirements. 418 */ 419 void 420 vmspace_exitfree(struct proc *p) 421 { 422 struct vmspace *vm; 423 424 vm = p->p_vmspace; 425 p->p_vmspace = NULL; 426 vmspace_drop_notoken(vm); 427 } 428 429 /* 430 * Called in two cases: 431 * 432 * (1) When the last refcnt is dropped and the vmspace becomes inactive, 433 * called with final == 0. refcnt will be (u_int)-1 at this point, 434 * and holdcnt will still be non-zero. 435 * 436 * (2) When holdcnt becomes 0, called with final == 1. There should no 437 * longer be anyone with access to the vmspace. 438 * 439 * VMSPACE_EXIT1 flags the primary deactivation 440 * VMSPACE_EXIT2 flags the last reap 441 */ 442 static void 443 vmspace_terminate(struct vmspace *vm, int final) 444 { 445 int count; 446 447 lwkt_gettoken(&vm->vm_map.token); 448 if (final == 0) { 449 KKASSERT((vm->vm_flags & VMSPACE_EXIT1) == 0); 450 vm->vm_flags |= VMSPACE_EXIT1; 451 452 /* 453 * Get rid of most of the resources. Leave the kernel pmap 454 * intact. 455 * 456 * If the pmap does not contain wired pages we can bulk-delete 457 * the pmap as a performance optimization before removing the 458 * related mappings. 459 * 460 * If the pmap contains wired pages we cannot do this 461 * pre-optimization because currently vm_fault_unwire() 462 * expects the pmap pages to exist and will not decrement 463 * p->wire_count if they do not. 464 */ 465 shmexit(vm); 466 if (vmspace_pmap(vm)->pm_stats.wired_count) { 467 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS, 468 VM_MAX_USER_ADDRESS); 469 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS, 470 VM_MAX_USER_ADDRESS); 471 } else { 472 pmap_remove_pages(vmspace_pmap(vm), VM_MIN_USER_ADDRESS, 473 VM_MAX_USER_ADDRESS); 474 vm_map_remove(&vm->vm_map, VM_MIN_USER_ADDRESS, 475 VM_MAX_USER_ADDRESS); 476 } 477 lwkt_reltoken(&vm->vm_map.token); 478 } else { 479 KKASSERT((vm->vm_flags & VMSPACE_EXIT1) != 0); 480 KKASSERT((vm->vm_flags & VMSPACE_EXIT2) == 0); 481 482 /* 483 * Get rid of remaining basic resources. 484 */ 485 vm->vm_flags |= VMSPACE_EXIT2; 486 shmexit(vm); 487 488 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 489 vm_map_lock(&vm->vm_map); 490 cpu_vmspace_free(vm); 491 492 /* 493 * Lock the map, to wait out all other references to it. 494 * Delete all of the mappings and pages they hold, then call 495 * the pmap module to reclaim anything left. 496 */ 497 vm_map_delete(&vm->vm_map, vm->vm_map.min_offset, 498 vm->vm_map.max_offset, &count); 499 vm_map_unlock(&vm->vm_map); 500 vm_map_entry_release(count); 501 502 pmap_release(vmspace_pmap(vm)); 503 lwkt_reltoken(&vm->vm_map.token); 504 objcache_put(vmspace_cache, vm); 505 } 506 } 507 508 /* 509 * Swap useage is determined by taking the proportional swap used by 510 * VM objects backing the VM map. To make up for fractional losses, 511 * if the VM object has any swap use at all the associated map entries 512 * count for at least 1 swap page. 513 * 514 * No requirements. 515 */ 516 vm_offset_t 517 vmspace_swap_count(struct vmspace *vm) 518 { 519 vm_map_t map = &vm->vm_map; 520 vm_map_entry_t cur; 521 vm_object_t object; 522 vm_offset_t count = 0; 523 vm_offset_t n; 524 525 vmspace_hold(vm); 526 for (cur = map->header.next; cur != &map->header; cur = cur->next) { 527 switch(cur->maptype) { 528 case VM_MAPTYPE_NORMAL: 529 case VM_MAPTYPE_VPAGETABLE: 530 if ((object = cur->object.vm_object) == NULL) 531 break; 532 if (object->swblock_count) { 533 n = (cur->end - cur->start) / PAGE_SIZE; 534 count += object->swblock_count * 535 SWAP_META_PAGES * n / object->size + 1; 536 } 537 break; 538 default: 539 break; 540 } 541 } 542 vmspace_drop(vm); 543 544 return(count); 545 } 546 547 /* 548 * Calculate the approximate number of anonymous pages in use by 549 * this vmspace. To make up for fractional losses, we count each 550 * VM object as having at least 1 anonymous page. 551 * 552 * No requirements. 553 */ 554 vm_offset_t 555 vmspace_anonymous_count(struct vmspace *vm) 556 { 557 vm_map_t map = &vm->vm_map; 558 vm_map_entry_t cur; 559 vm_object_t object; 560 vm_offset_t count = 0; 561 562 vmspace_hold(vm); 563 for (cur = map->header.next; cur != &map->header; cur = cur->next) { 564 switch(cur->maptype) { 565 case VM_MAPTYPE_NORMAL: 566 case VM_MAPTYPE_VPAGETABLE: 567 if ((object = cur->object.vm_object) == NULL) 568 break; 569 if (object->type != OBJT_DEFAULT && 570 object->type != OBJT_SWAP) { 571 break; 572 } 573 count += object->resident_page_count; 574 break; 575 default: 576 break; 577 } 578 } 579 vmspace_drop(vm); 580 581 return(count); 582 } 583 584 /* 585 * Initialize an existing vm_map structure such as that in the vmspace 586 * structure. The pmap is initialized elsewhere. 587 * 588 * No requirements. 589 */ 590 void 591 vm_map_init(struct vm_map *map, vm_offset_t min, vm_offset_t max, pmap_t pmap) 592 { 593 map->header.next = map->header.prev = &map->header; 594 RB_INIT(&map->rb_root); 595 spin_init(&map->ilock_spin, "ilock"); 596 map->ilock_base = NULL; 597 map->nentries = 0; 598 map->size = 0; 599 map->system_map = 0; 600 map->min_offset = min; 601 map->max_offset = max; 602 map->pmap = pmap; 603 map->first_free = &map->header; 604 map->hint = &map->header; 605 map->timestamp = 0; 606 map->flags = 0; 607 lwkt_token_init(&map->token, "vm_map"); 608 lockinit(&map->lock, "vm_maplk", (hz + 9) / 10, 0); 609 } 610 611 /* 612 * Shadow the vm_map_entry's object. This typically needs to be done when 613 * a write fault is taken on an entry which had previously been cloned by 614 * fork(). The shared object (which might be NULL) must become private so 615 * we add a shadow layer above it. 616 * 617 * Object allocation for anonymous mappings is defered as long as possible. 618 * When creating a shadow, however, the underlying object must be instantiated 619 * so it can be shared. 620 * 621 * If the map segment is governed by a virtual page table then it is 622 * possible to address offsets beyond the mapped area. Just allocate 623 * a maximally sized object for this case. 624 * 625 * If addref is non-zero an additional reference is added to the returned 626 * entry. This mechanic exists because the additional reference might have 627 * to be added atomically and not after return to prevent a premature 628 * collapse. 629 * 630 * The vm_map must be exclusively locked. 631 * No other requirements. 632 */ 633 static 634 void 635 vm_map_entry_shadow(vm_map_entry_t entry, int addref) 636 { 637 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) { 638 vm_object_shadow(&entry->object.vm_object, &entry->offset, 639 0x7FFFFFFF, addref); /* XXX */ 640 } else { 641 vm_object_shadow(&entry->object.vm_object, &entry->offset, 642 atop(entry->end - entry->start), addref); 643 } 644 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 645 } 646 647 /* 648 * Allocate an object for a vm_map_entry. 649 * 650 * Object allocation for anonymous mappings is defered as long as possible. 651 * This function is called when we can defer no longer, generally when a map 652 * entry might be split or forked or takes a page fault. 653 * 654 * If the map segment is governed by a virtual page table then it is 655 * possible to address offsets beyond the mapped area. Just allocate 656 * a maximally sized object for this case. 657 * 658 * The vm_map must be exclusively locked. 659 * No other requirements. 660 */ 661 void 662 vm_map_entry_allocate_object(vm_map_entry_t entry) 663 { 664 vm_object_t obj; 665 666 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) { 667 obj = vm_object_allocate(OBJT_DEFAULT, 0x7FFFFFFF); /* XXX */ 668 } else { 669 obj = vm_object_allocate(OBJT_DEFAULT, 670 atop(entry->end - entry->start)); 671 } 672 entry->object.vm_object = obj; 673 entry->offset = 0; 674 } 675 676 /* 677 * Set an initial negative count so the first attempt to reserve 678 * space preloads a bunch of vm_map_entry's for this cpu. Also 679 * pre-allocate 2 vm_map_entries which will be needed by zalloc() to 680 * map a new page for vm_map_entry structures. SMP systems are 681 * particularly sensitive. 682 * 683 * This routine is called in early boot so we cannot just call 684 * vm_map_entry_reserve(). 685 * 686 * Called from the low level boot code only (for each cpu) 687 * 688 * WARNING! Take care not to have too-big a static/BSS structure here 689 * as MAXCPU can be 256+, otherwise the loader's 64MB heap 690 * can get blown out by the kernel plus the initrd image. 691 */ 692 void 693 vm_map_entry_reserve_cpu_init(globaldata_t gd) 694 { 695 vm_map_entry_t entry; 696 int count; 697 int i; 698 699 gd->gd_vme_avail -= MAP_RESERVE_COUNT * 2; 700 if (gd->gd_cpuid == 0) { 701 entry = &cpu_map_entry_init_bsp[0]; 702 count = MAPENTRYBSP_CACHE; 703 } else { 704 entry = &cpu_map_entry_init_ap[gd->gd_cpuid][0]; 705 count = MAPENTRYAP_CACHE; 706 } 707 for (i = 0; i < count; ++i, ++entry) { 708 entry->next = gd->gd_vme_base; 709 gd->gd_vme_base = entry; 710 } 711 } 712 713 /* 714 * Reserves vm_map_entry structures so code later on can manipulate 715 * map_entry structures within a locked map without blocking trying 716 * to allocate a new vm_map_entry. 717 * 718 * No requirements. 719 */ 720 int 721 vm_map_entry_reserve(int count) 722 { 723 struct globaldata *gd = mycpu; 724 vm_map_entry_t entry; 725 726 /* 727 * Make sure we have enough structures in gd_vme_base to handle 728 * the reservation request. 729 * 730 * The critical section protects access to the per-cpu gd. 731 */ 732 crit_enter(); 733 while (gd->gd_vme_avail < count) { 734 entry = zalloc(mapentzone); 735 entry->next = gd->gd_vme_base; 736 gd->gd_vme_base = entry; 737 ++gd->gd_vme_avail; 738 } 739 gd->gd_vme_avail -= count; 740 crit_exit(); 741 742 return(count); 743 } 744 745 /* 746 * Releases previously reserved vm_map_entry structures that were not 747 * used. If we have too much junk in our per-cpu cache clean some of 748 * it out. 749 * 750 * No requirements. 751 */ 752 void 753 vm_map_entry_release(int count) 754 { 755 struct globaldata *gd = mycpu; 756 vm_map_entry_t entry; 757 758 crit_enter(); 759 gd->gd_vme_avail += count; 760 while (gd->gd_vme_avail > MAP_RESERVE_SLOP) { 761 entry = gd->gd_vme_base; 762 KKASSERT(entry != NULL); 763 gd->gd_vme_base = entry->next; 764 --gd->gd_vme_avail; 765 crit_exit(); 766 zfree(mapentzone, entry); 767 crit_enter(); 768 } 769 crit_exit(); 770 } 771 772 /* 773 * Reserve map entry structures for use in kernel_map itself. These 774 * entries have *ALREADY* been reserved on a per-cpu basis when the map 775 * was inited. This function is used by zalloc() to avoid a recursion 776 * when zalloc() itself needs to allocate additional kernel memory. 777 * 778 * This function works like the normal reserve but does not load the 779 * vm_map_entry cache (because that would result in an infinite 780 * recursion). Note that gd_vme_avail may go negative. This is expected. 781 * 782 * Any caller of this function must be sure to renormalize after 783 * potentially eating entries to ensure that the reserve supply 784 * remains intact. 785 * 786 * No requirements. 787 */ 788 int 789 vm_map_entry_kreserve(int count) 790 { 791 struct globaldata *gd = mycpu; 792 793 crit_enter(); 794 gd->gd_vme_avail -= count; 795 crit_exit(); 796 KASSERT(gd->gd_vme_base != NULL, 797 ("no reserved entries left, gd_vme_avail = %d", 798 gd->gd_vme_avail)); 799 return(count); 800 } 801 802 /* 803 * Release previously reserved map entries for kernel_map. We do not 804 * attempt to clean up like the normal release function as this would 805 * cause an unnecessary (but probably not fatal) deep procedure call. 806 * 807 * No requirements. 808 */ 809 void 810 vm_map_entry_krelease(int count) 811 { 812 struct globaldata *gd = mycpu; 813 814 crit_enter(); 815 gd->gd_vme_avail += count; 816 crit_exit(); 817 } 818 819 /* 820 * Allocates a VM map entry for insertion. No entry fields are filled in. 821 * 822 * The entries should have previously been reserved. The reservation count 823 * is tracked in (*countp). 824 * 825 * No requirements. 826 */ 827 static vm_map_entry_t 828 vm_map_entry_create(vm_map_t map, int *countp) 829 { 830 struct globaldata *gd = mycpu; 831 vm_map_entry_t entry; 832 833 KKASSERT(*countp > 0); 834 --*countp; 835 crit_enter(); 836 entry = gd->gd_vme_base; 837 KASSERT(entry != NULL, ("gd_vme_base NULL! count %d", *countp)); 838 gd->gd_vme_base = entry->next; 839 crit_exit(); 840 841 return(entry); 842 } 843 844 /* 845 * Dispose of a vm_map_entry that is no longer being referenced. 846 * 847 * No requirements. 848 */ 849 static void 850 vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry, int *countp) 851 { 852 struct globaldata *gd = mycpu; 853 854 KKASSERT(map->hint != entry); 855 KKASSERT(map->first_free != entry); 856 857 ++*countp; 858 crit_enter(); 859 entry->next = gd->gd_vme_base; 860 gd->gd_vme_base = entry; 861 crit_exit(); 862 } 863 864 865 /* 866 * Insert/remove entries from maps. 867 * 868 * The related map must be exclusively locked. 869 * The caller must hold map->token 870 * No other requirements. 871 */ 872 static __inline void 873 vm_map_entry_link(vm_map_t map, 874 vm_map_entry_t after_where, 875 vm_map_entry_t entry) 876 { 877 ASSERT_VM_MAP_LOCKED(map); 878 879 map->nentries++; 880 entry->prev = after_where; 881 entry->next = after_where->next; 882 entry->next->prev = entry; 883 after_where->next = entry; 884 if (vm_map_rb_tree_RB_INSERT(&map->rb_root, entry)) 885 panic("vm_map_entry_link: dup addr map %p ent %p", map, entry); 886 } 887 888 static __inline void 889 vm_map_entry_unlink(vm_map_t map, 890 vm_map_entry_t entry) 891 { 892 vm_map_entry_t prev; 893 vm_map_entry_t next; 894 895 ASSERT_VM_MAP_LOCKED(map); 896 897 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 898 panic("vm_map_entry_unlink: attempt to mess with " 899 "locked entry! %p", entry); 900 } 901 prev = entry->prev; 902 next = entry->next; 903 next->prev = prev; 904 prev->next = next; 905 vm_map_rb_tree_RB_REMOVE(&map->rb_root, entry); 906 map->nentries--; 907 } 908 909 /* 910 * Finds the map entry containing (or immediately preceding) the specified 911 * address in the given map. The entry is returned in (*entry). 912 * 913 * The boolean result indicates whether the address is actually contained 914 * in the map. 915 * 916 * The related map must be locked. 917 * No other requirements. 918 */ 919 boolean_t 920 vm_map_lookup_entry(vm_map_t map, vm_offset_t address, vm_map_entry_t *entry) 921 { 922 vm_map_entry_t tmp; 923 vm_map_entry_t last; 924 925 ASSERT_VM_MAP_LOCKED(map); 926 #if 0 927 /* 928 * XXX TEMPORARILY DISABLED. For some reason our attempt to revive 929 * the hint code with the red-black lookup meets with system crashes 930 * and lockups. We do not yet know why. 931 * 932 * It is possible that the problem is related to the setting 933 * of the hint during map_entry deletion, in the code specified 934 * at the GGG comment later on in this file. 935 * 936 * YYY More likely it's because this function can be called with 937 * a shared lock on the map, resulting in map->hint updates possibly 938 * racing. Fixed now but untested. 939 */ 940 /* 941 * Quickly check the cached hint, there's a good chance of a match. 942 */ 943 tmp = map->hint; 944 cpu_ccfence(); 945 if (tmp != &map->header) { 946 if (address >= tmp->start && address < tmp->end) { 947 *entry = tmp; 948 return(TRUE); 949 } 950 } 951 #endif 952 953 /* 954 * Locate the record from the top of the tree. 'last' tracks the 955 * closest prior record and is returned if no match is found, which 956 * in binary tree terms means tracking the most recent right-branch 957 * taken. If there is no prior record, &map->header is returned. 958 */ 959 last = &map->header; 960 tmp = RB_ROOT(&map->rb_root); 961 962 while (tmp) { 963 if (address >= tmp->start) { 964 if (address < tmp->end) { 965 *entry = tmp; 966 map->hint = tmp; 967 return(TRUE); 968 } 969 last = tmp; 970 tmp = RB_RIGHT(tmp, rb_entry); 971 } else { 972 tmp = RB_LEFT(tmp, rb_entry); 973 } 974 } 975 *entry = last; 976 return (FALSE); 977 } 978 979 /* 980 * Inserts the given whole VM object into the target map at the specified 981 * address range. The object's size should match that of the address range. 982 * 983 * The map must be exclusively locked. 984 * The object must be held. 985 * The caller must have reserved sufficient vm_map_entry structures. 986 * 987 * If object is non-NULL, ref count must be bumped by caller prior to 988 * making call to account for the new entry. 989 */ 990 int 991 vm_map_insert(vm_map_t map, int *countp, void *map_object, void *map_aux, 992 vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, 993 vm_maptype_t maptype, vm_subsys_t id, 994 vm_prot_t prot, vm_prot_t max, int cow) 995 { 996 vm_map_entry_t new_entry; 997 vm_map_entry_t prev_entry; 998 vm_map_entry_t temp_entry; 999 vm_eflags_t protoeflags; 1000 int must_drop = 0; 1001 vm_object_t object; 1002 1003 if (maptype == VM_MAPTYPE_UKSMAP) 1004 object = NULL; 1005 else 1006 object = map_object; 1007 1008 ASSERT_VM_MAP_LOCKED(map); 1009 if (object) 1010 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object)); 1011 1012 /* 1013 * Check that the start and end points are not bogus. 1014 */ 1015 if ((start < map->min_offset) || (end > map->max_offset) || 1016 (start >= end)) 1017 return (KERN_INVALID_ADDRESS); 1018 1019 /* 1020 * Find the entry prior to the proposed starting address; if it's part 1021 * of an existing entry, this range is bogus. 1022 */ 1023 if (vm_map_lookup_entry(map, start, &temp_entry)) 1024 return (KERN_NO_SPACE); 1025 1026 prev_entry = temp_entry; 1027 1028 /* 1029 * Assert that the next entry doesn't overlap the end point. 1030 */ 1031 1032 if ((prev_entry->next != &map->header) && 1033 (prev_entry->next->start < end)) 1034 return (KERN_NO_SPACE); 1035 1036 protoeflags = 0; 1037 1038 if (cow & MAP_COPY_ON_WRITE) 1039 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY; 1040 1041 if (cow & MAP_NOFAULT) { 1042 protoeflags |= MAP_ENTRY_NOFAULT; 1043 1044 KASSERT(object == NULL, 1045 ("vm_map_insert: paradoxical MAP_NOFAULT request")); 1046 } 1047 if (cow & MAP_DISABLE_SYNCER) 1048 protoeflags |= MAP_ENTRY_NOSYNC; 1049 if (cow & MAP_DISABLE_COREDUMP) 1050 protoeflags |= MAP_ENTRY_NOCOREDUMP; 1051 if (cow & MAP_IS_STACK) 1052 protoeflags |= MAP_ENTRY_STACK; 1053 if (cow & MAP_IS_KSTACK) 1054 protoeflags |= MAP_ENTRY_KSTACK; 1055 1056 lwkt_gettoken(&map->token); 1057 1058 if (object) { 1059 /* 1060 * When object is non-NULL, it could be shared with another 1061 * process. We have to set or clear OBJ_ONEMAPPING 1062 * appropriately. 1063 * 1064 * NOTE: This flag is only applicable to DEFAULT and SWAP 1065 * objects and will already be clear in other types 1066 * of objects, so a shared object lock is ok for 1067 * VNODE objects. 1068 */ 1069 if ((object->ref_count > 1) || (object->shadow_count != 0)) { 1070 vm_object_clear_flag(object, OBJ_ONEMAPPING); 1071 } 1072 } 1073 else if ((prev_entry != &map->header) && 1074 (prev_entry->eflags == protoeflags) && 1075 (prev_entry->end == start) && 1076 (prev_entry->wired_count == 0) && 1077 (prev_entry->id == id) && 1078 prev_entry->maptype == maptype && 1079 maptype == VM_MAPTYPE_NORMAL && 1080 ((prev_entry->object.vm_object == NULL) || 1081 vm_object_coalesce(prev_entry->object.vm_object, 1082 OFF_TO_IDX(prev_entry->offset), 1083 (vm_size_t)(prev_entry->end - prev_entry->start), 1084 (vm_size_t)(end - prev_entry->end)))) { 1085 /* 1086 * We were able to extend the object. Determine if we 1087 * can extend the previous map entry to include the 1088 * new range as well. 1089 */ 1090 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) && 1091 (prev_entry->protection == prot) && 1092 (prev_entry->max_protection == max)) { 1093 map->size += (end - prev_entry->end); 1094 prev_entry->end = end; 1095 vm_map_simplify_entry(map, prev_entry, countp); 1096 lwkt_reltoken(&map->token); 1097 return (KERN_SUCCESS); 1098 } 1099 1100 /* 1101 * If we can extend the object but cannot extend the 1102 * map entry, we have to create a new map entry. We 1103 * must bump the ref count on the extended object to 1104 * account for it. object may be NULL. 1105 * 1106 * XXX if object is NULL should we set offset to 0 here ? 1107 */ 1108 object = prev_entry->object.vm_object; 1109 offset = prev_entry->offset + 1110 (prev_entry->end - prev_entry->start); 1111 if (object) { 1112 vm_object_hold(object); 1113 vm_object_chain_wait(object, 0); 1114 vm_object_reference_locked(object); 1115 must_drop = 1; 1116 map_object = object; 1117 } 1118 } 1119 1120 /* 1121 * NOTE: if conditionals fail, object can be NULL here. This occurs 1122 * in things like the buffer map where we manage kva but do not manage 1123 * backing objects. 1124 */ 1125 1126 /* 1127 * Create a new entry 1128 */ 1129 1130 new_entry = vm_map_entry_create(map, countp); 1131 new_entry->start = start; 1132 new_entry->end = end; 1133 new_entry->id = id; 1134 1135 new_entry->maptype = maptype; 1136 new_entry->eflags = protoeflags; 1137 new_entry->object.map_object = map_object; 1138 new_entry->aux.master_pde = 0; /* in case size is different */ 1139 new_entry->aux.map_aux = map_aux; 1140 new_entry->offset = offset; 1141 1142 new_entry->inheritance = VM_INHERIT_DEFAULT; 1143 new_entry->protection = prot; 1144 new_entry->max_protection = max; 1145 new_entry->wired_count = 0; 1146 1147 /* 1148 * Insert the new entry into the list 1149 */ 1150 1151 vm_map_entry_link(map, prev_entry, new_entry); 1152 map->size += new_entry->end - new_entry->start; 1153 1154 /* 1155 * Update the free space hint. Entries cannot overlap. 1156 * An exact comparison is needed to avoid matching 1157 * against the map->header. 1158 */ 1159 if ((map->first_free == prev_entry) && 1160 (prev_entry->end == new_entry->start)) { 1161 map->first_free = new_entry; 1162 } 1163 1164 #if 0 1165 /* 1166 * Temporarily removed to avoid MAP_STACK panic, due to 1167 * MAP_STACK being a huge hack. Will be added back in 1168 * when MAP_STACK (and the user stack mapping) is fixed. 1169 */ 1170 /* 1171 * It may be possible to simplify the entry 1172 */ 1173 vm_map_simplify_entry(map, new_entry, countp); 1174 #endif 1175 1176 /* 1177 * Try to pre-populate the page table. Mappings governed by virtual 1178 * page tables cannot be prepopulated without a lot of work, so 1179 * don't try. 1180 */ 1181 if ((cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) && 1182 maptype != VM_MAPTYPE_VPAGETABLE && 1183 maptype != VM_MAPTYPE_UKSMAP) { 1184 int dorelock = 0; 1185 if (vm_map_relock_enable && (cow & MAP_PREFAULT_RELOCK)) { 1186 dorelock = 1; 1187 vm_object_lock_swap(); 1188 vm_object_drop(object); 1189 } 1190 pmap_object_init_pt(map->pmap, start, prot, 1191 object, OFF_TO_IDX(offset), end - start, 1192 cow & MAP_PREFAULT_PARTIAL); 1193 if (dorelock) { 1194 vm_object_hold(object); 1195 vm_object_lock_swap(); 1196 } 1197 } 1198 if (must_drop) 1199 vm_object_drop(object); 1200 1201 lwkt_reltoken(&map->token); 1202 return (KERN_SUCCESS); 1203 } 1204 1205 /* 1206 * Find sufficient space for `length' bytes in the given map, starting at 1207 * `start'. Returns 0 on success, 1 on no space. 1208 * 1209 * This function will returned an arbitrarily aligned pointer. If no 1210 * particular alignment is required you should pass align as 1. Note that 1211 * the map may return PAGE_SIZE aligned pointers if all the lengths used in 1212 * the map are a multiple of PAGE_SIZE, even if you pass a smaller align 1213 * argument. 1214 * 1215 * 'align' should be a power of 2 but is not required to be. 1216 * 1217 * The map must be exclusively locked. 1218 * No other requirements. 1219 */ 1220 int 1221 vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length, 1222 vm_size_t align, int flags, vm_offset_t *addr) 1223 { 1224 vm_map_entry_t entry, next; 1225 vm_offset_t end; 1226 vm_offset_t align_mask; 1227 1228 if (start < map->min_offset) 1229 start = map->min_offset; 1230 if (start > map->max_offset) 1231 return (1); 1232 1233 /* 1234 * If the alignment is not a power of 2 we will have to use 1235 * a mod/division, set align_mask to a special value. 1236 */ 1237 if ((align | (align - 1)) + 1 != (align << 1)) 1238 align_mask = (vm_offset_t)-1; 1239 else 1240 align_mask = align - 1; 1241 1242 /* 1243 * Look for the first possible address; if there's already something 1244 * at this address, we have to start after it. 1245 */ 1246 if (start == map->min_offset) { 1247 if ((entry = map->first_free) != &map->header) 1248 start = entry->end; 1249 } else { 1250 vm_map_entry_t tmp; 1251 1252 if (vm_map_lookup_entry(map, start, &tmp)) 1253 start = tmp->end; 1254 entry = tmp; 1255 } 1256 1257 /* 1258 * Look through the rest of the map, trying to fit a new region in the 1259 * gap between existing regions, or after the very last region. 1260 */ 1261 for (;; start = (entry = next)->end) { 1262 /* 1263 * Adjust the proposed start by the requested alignment, 1264 * be sure that we didn't wrap the address. 1265 */ 1266 if (align_mask == (vm_offset_t)-1) 1267 end = roundup(start, align); 1268 else 1269 end = (start + align_mask) & ~align_mask; 1270 if (end < start) 1271 return (1); 1272 start = end; 1273 /* 1274 * Find the end of the proposed new region. Be sure we didn't 1275 * go beyond the end of the map, or wrap around the address. 1276 * Then check to see if this is the last entry or if the 1277 * proposed end fits in the gap between this and the next 1278 * entry. 1279 */ 1280 end = start + length; 1281 if (end > map->max_offset || end < start) 1282 return (1); 1283 next = entry->next; 1284 1285 /* 1286 * If the next entry's start address is beyond the desired 1287 * end address we may have found a good entry. 1288 * 1289 * If the next entry is a stack mapping we do not map into 1290 * the stack's reserved space. 1291 * 1292 * XXX continue to allow mapping into the stack's reserved 1293 * space if doing a MAP_STACK mapping inside a MAP_STACK 1294 * mapping, for backwards compatibility. But the caller 1295 * really should use MAP_STACK | MAP_TRYFIXED if they 1296 * want to do that. 1297 */ 1298 if (next == &map->header) 1299 break; 1300 if (next->start >= end) { 1301 if ((next->eflags & MAP_ENTRY_STACK) == 0) 1302 break; 1303 if (flags & MAP_STACK) 1304 break; 1305 if (next->start - next->aux.avail_ssize >= end) 1306 break; 1307 } 1308 } 1309 map->hint = entry; 1310 1311 /* 1312 * Grow the kernel_map if necessary. pmap_growkernel() will panic 1313 * if it fails. The kernel_map is locked and nothing can steal 1314 * our address space if pmap_growkernel() blocks. 1315 * 1316 * NOTE: This may be unconditionally called for kldload areas on 1317 * x86_64 because these do not bump kernel_vm_end (which would 1318 * fill 128G worth of page tables!). Therefore we must not 1319 * retry. 1320 */ 1321 if (map == &kernel_map) { 1322 vm_offset_t kstop; 1323 1324 kstop = round_page(start + length); 1325 if (kstop > kernel_vm_end) 1326 pmap_growkernel(start, kstop); 1327 } 1328 *addr = start; 1329 return (0); 1330 } 1331 1332 /* 1333 * vm_map_find finds an unallocated region in the target address map with 1334 * the given length and allocates it. The search is defined to be first-fit 1335 * from the specified address; the region found is returned in the same 1336 * parameter. 1337 * 1338 * If object is non-NULL, ref count must be bumped by caller 1339 * prior to making call to account for the new entry. 1340 * 1341 * No requirements. This function will lock the map temporarily. 1342 */ 1343 int 1344 vm_map_find(vm_map_t map, void *map_object, void *map_aux, 1345 vm_ooffset_t offset, vm_offset_t *addr, 1346 vm_size_t length, vm_size_t align, boolean_t fitit, 1347 vm_maptype_t maptype, vm_subsys_t id, 1348 vm_prot_t prot, vm_prot_t max, int cow) 1349 { 1350 vm_offset_t start; 1351 vm_object_t object; 1352 int result; 1353 int count; 1354 1355 if (maptype == VM_MAPTYPE_UKSMAP) 1356 object = NULL; 1357 else 1358 object = map_object; 1359 1360 start = *addr; 1361 1362 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 1363 vm_map_lock(map); 1364 if (object) 1365 vm_object_hold_shared(object); 1366 if (fitit) { 1367 if (vm_map_findspace(map, start, length, align, 0, addr)) { 1368 if (object) 1369 vm_object_drop(object); 1370 vm_map_unlock(map); 1371 vm_map_entry_release(count); 1372 return (KERN_NO_SPACE); 1373 } 1374 start = *addr; 1375 } 1376 result = vm_map_insert(map, &count, map_object, map_aux, 1377 offset, start, start + length, 1378 maptype, id, prot, max, cow); 1379 if (object) 1380 vm_object_drop(object); 1381 vm_map_unlock(map); 1382 vm_map_entry_release(count); 1383 1384 return (result); 1385 } 1386 1387 /* 1388 * Simplify the given map entry by merging with either neighbor. This 1389 * routine also has the ability to merge with both neighbors. 1390 * 1391 * This routine guarentees that the passed entry remains valid (though 1392 * possibly extended). When merging, this routine may delete one or 1393 * both neighbors. No action is taken on entries which have their 1394 * in-transition flag set. 1395 * 1396 * The map must be exclusively locked. 1397 */ 1398 void 1399 vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry, int *countp) 1400 { 1401 vm_map_entry_t next, prev; 1402 vm_size_t prevsize, esize; 1403 1404 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 1405 ++mycpu->gd_cnt.v_intrans_coll; 1406 return; 1407 } 1408 1409 if (entry->maptype == VM_MAPTYPE_SUBMAP) 1410 return; 1411 if (entry->maptype == VM_MAPTYPE_UKSMAP) 1412 return; 1413 1414 prev = entry->prev; 1415 if (prev != &map->header) { 1416 prevsize = prev->end - prev->start; 1417 if ( (prev->end == entry->start) && 1418 (prev->maptype == entry->maptype) && 1419 (prev->object.vm_object == entry->object.vm_object) && 1420 (!prev->object.vm_object || 1421 (prev->offset + prevsize == entry->offset)) && 1422 (prev->eflags == entry->eflags) && 1423 (prev->protection == entry->protection) && 1424 (prev->max_protection == entry->max_protection) && 1425 (prev->inheritance == entry->inheritance) && 1426 (prev->id == entry->id) && 1427 (prev->wired_count == entry->wired_count)) { 1428 if (map->first_free == prev) 1429 map->first_free = entry; 1430 if (map->hint == prev) 1431 map->hint = entry; 1432 vm_map_entry_unlink(map, prev); 1433 entry->start = prev->start; 1434 entry->offset = prev->offset; 1435 if (prev->object.vm_object) 1436 vm_object_deallocate(prev->object.vm_object); 1437 vm_map_entry_dispose(map, prev, countp); 1438 } 1439 } 1440 1441 next = entry->next; 1442 if (next != &map->header) { 1443 esize = entry->end - entry->start; 1444 if ((entry->end == next->start) && 1445 (next->maptype == entry->maptype) && 1446 (next->object.vm_object == entry->object.vm_object) && 1447 (!entry->object.vm_object || 1448 (entry->offset + esize == next->offset)) && 1449 (next->eflags == entry->eflags) && 1450 (next->protection == entry->protection) && 1451 (next->max_protection == entry->max_protection) && 1452 (next->inheritance == entry->inheritance) && 1453 (next->id == entry->id) && 1454 (next->wired_count == entry->wired_count)) { 1455 if (map->first_free == next) 1456 map->first_free = entry; 1457 if (map->hint == next) 1458 map->hint = entry; 1459 vm_map_entry_unlink(map, next); 1460 entry->end = next->end; 1461 if (next->object.vm_object) 1462 vm_object_deallocate(next->object.vm_object); 1463 vm_map_entry_dispose(map, next, countp); 1464 } 1465 } 1466 } 1467 1468 /* 1469 * Asserts that the given entry begins at or after the specified address. 1470 * If necessary, it splits the entry into two. 1471 */ 1472 #define vm_map_clip_start(map, entry, startaddr, countp) \ 1473 { \ 1474 if (startaddr > entry->start) \ 1475 _vm_map_clip_start(map, entry, startaddr, countp); \ 1476 } 1477 1478 /* 1479 * This routine is called only when it is known that the entry must be split. 1480 * 1481 * The map must be exclusively locked. 1482 */ 1483 static void 1484 _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start, 1485 int *countp) 1486 { 1487 vm_map_entry_t new_entry; 1488 1489 /* 1490 * Split off the front portion -- note that we must insert the new 1491 * entry BEFORE this one, so that this entry has the specified 1492 * starting address. 1493 */ 1494 1495 vm_map_simplify_entry(map, entry, countp); 1496 1497 /* 1498 * If there is no object backing this entry, we might as well create 1499 * one now. If we defer it, an object can get created after the map 1500 * is clipped, and individual objects will be created for the split-up 1501 * map. This is a bit of a hack, but is also about the best place to 1502 * put this improvement. 1503 */ 1504 if (entry->object.vm_object == NULL && !map->system_map) { 1505 vm_map_entry_allocate_object(entry); 1506 } 1507 1508 new_entry = vm_map_entry_create(map, countp); 1509 *new_entry = *entry; 1510 1511 new_entry->end = start; 1512 entry->offset += (start - entry->start); 1513 entry->start = start; 1514 1515 vm_map_entry_link(map, entry->prev, new_entry); 1516 1517 switch(entry->maptype) { 1518 case VM_MAPTYPE_NORMAL: 1519 case VM_MAPTYPE_VPAGETABLE: 1520 if (new_entry->object.vm_object) { 1521 vm_object_hold(new_entry->object.vm_object); 1522 vm_object_chain_wait(new_entry->object.vm_object, 0); 1523 vm_object_reference_locked(new_entry->object.vm_object); 1524 vm_object_drop(new_entry->object.vm_object); 1525 } 1526 break; 1527 default: 1528 break; 1529 } 1530 } 1531 1532 /* 1533 * Asserts that the given entry ends at or before the specified address. 1534 * If necessary, it splits the entry into two. 1535 * 1536 * The map must be exclusively locked. 1537 */ 1538 #define vm_map_clip_end(map, entry, endaddr, countp) \ 1539 { \ 1540 if (endaddr < entry->end) \ 1541 _vm_map_clip_end(map, entry, endaddr, countp); \ 1542 } 1543 1544 /* 1545 * This routine is called only when it is known that the entry must be split. 1546 * 1547 * The map must be exclusively locked. 1548 */ 1549 static void 1550 _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end, 1551 int *countp) 1552 { 1553 vm_map_entry_t new_entry; 1554 1555 /* 1556 * If there is no object backing this entry, we might as well create 1557 * one now. If we defer it, an object can get created after the map 1558 * is clipped, and individual objects will be created for the split-up 1559 * map. This is a bit of a hack, but is also about the best place to 1560 * put this improvement. 1561 */ 1562 1563 if (entry->object.vm_object == NULL && !map->system_map) { 1564 vm_map_entry_allocate_object(entry); 1565 } 1566 1567 /* 1568 * Create a new entry and insert it AFTER the specified entry 1569 */ 1570 1571 new_entry = vm_map_entry_create(map, countp); 1572 *new_entry = *entry; 1573 1574 new_entry->start = entry->end = end; 1575 new_entry->offset += (end - entry->start); 1576 1577 vm_map_entry_link(map, entry, new_entry); 1578 1579 switch(entry->maptype) { 1580 case VM_MAPTYPE_NORMAL: 1581 case VM_MAPTYPE_VPAGETABLE: 1582 if (new_entry->object.vm_object) { 1583 vm_object_hold(new_entry->object.vm_object); 1584 vm_object_chain_wait(new_entry->object.vm_object, 0); 1585 vm_object_reference_locked(new_entry->object.vm_object); 1586 vm_object_drop(new_entry->object.vm_object); 1587 } 1588 break; 1589 default: 1590 break; 1591 } 1592 } 1593 1594 /* 1595 * Asserts that the starting and ending region addresses fall within the 1596 * valid range for the map. 1597 */ 1598 #define VM_MAP_RANGE_CHECK(map, start, end) \ 1599 { \ 1600 if (start < vm_map_min(map)) \ 1601 start = vm_map_min(map); \ 1602 if (end > vm_map_max(map)) \ 1603 end = vm_map_max(map); \ 1604 if (start > end) \ 1605 start = end; \ 1606 } 1607 1608 /* 1609 * Used to block when an in-transition collison occurs. The map 1610 * is unlocked for the sleep and relocked before the return. 1611 */ 1612 void 1613 vm_map_transition_wait(vm_map_t map) 1614 { 1615 tsleep_interlock(map, 0); 1616 vm_map_unlock(map); 1617 tsleep(map, PINTERLOCKED, "vment", 0); 1618 vm_map_lock(map); 1619 } 1620 1621 /* 1622 * When we do blocking operations with the map lock held it is 1623 * possible that a clip might have occured on our in-transit entry, 1624 * requiring an adjustment to the entry in our loop. These macros 1625 * help the pageable and clip_range code deal with the case. The 1626 * conditional costs virtually nothing if no clipping has occured. 1627 */ 1628 1629 #define CLIP_CHECK_BACK(entry, save_start) \ 1630 do { \ 1631 while (entry->start != save_start) { \ 1632 entry = entry->prev; \ 1633 KASSERT(entry != &map->header, ("bad entry clip")); \ 1634 } \ 1635 } while(0) 1636 1637 #define CLIP_CHECK_FWD(entry, save_end) \ 1638 do { \ 1639 while (entry->end != save_end) { \ 1640 entry = entry->next; \ 1641 KASSERT(entry != &map->header, ("bad entry clip")); \ 1642 } \ 1643 } while(0) 1644 1645 1646 /* 1647 * Clip the specified range and return the base entry. The 1648 * range may cover several entries starting at the returned base 1649 * and the first and last entry in the covering sequence will be 1650 * properly clipped to the requested start and end address. 1651 * 1652 * If no holes are allowed you should pass the MAP_CLIP_NO_HOLES 1653 * flag. 1654 * 1655 * The MAP_ENTRY_IN_TRANSITION flag will be set for the entries 1656 * covered by the requested range. 1657 * 1658 * The map must be exclusively locked on entry and will remain locked 1659 * on return. If no range exists or the range contains holes and you 1660 * specified that no holes were allowed, NULL will be returned. This 1661 * routine may temporarily unlock the map in order avoid a deadlock when 1662 * sleeping. 1663 */ 1664 static 1665 vm_map_entry_t 1666 vm_map_clip_range(vm_map_t map, vm_offset_t start, vm_offset_t end, 1667 int *countp, int flags) 1668 { 1669 vm_map_entry_t start_entry; 1670 vm_map_entry_t entry; 1671 1672 /* 1673 * Locate the entry and effect initial clipping. The in-transition 1674 * case does not occur very often so do not try to optimize it. 1675 */ 1676 again: 1677 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) 1678 return (NULL); 1679 entry = start_entry; 1680 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 1681 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 1682 ++mycpu->gd_cnt.v_intrans_coll; 1683 ++mycpu->gd_cnt.v_intrans_wait; 1684 vm_map_transition_wait(map); 1685 /* 1686 * entry and/or start_entry may have been clipped while 1687 * we slept, or may have gone away entirely. We have 1688 * to restart from the lookup. 1689 */ 1690 goto again; 1691 } 1692 1693 /* 1694 * Since we hold an exclusive map lock we do not have to restart 1695 * after clipping, even though clipping may block in zalloc. 1696 */ 1697 vm_map_clip_start(map, entry, start, countp); 1698 vm_map_clip_end(map, entry, end, countp); 1699 entry->eflags |= MAP_ENTRY_IN_TRANSITION; 1700 1701 /* 1702 * Scan entries covered by the range. When working on the next 1703 * entry a restart need only re-loop on the current entry which 1704 * we have already locked, since 'next' may have changed. Also, 1705 * even though entry is safe, it may have been clipped so we 1706 * have to iterate forwards through the clip after sleeping. 1707 */ 1708 while (entry->next != &map->header && entry->next->start < end) { 1709 vm_map_entry_t next = entry->next; 1710 1711 if (flags & MAP_CLIP_NO_HOLES) { 1712 if (next->start > entry->end) { 1713 vm_map_unclip_range(map, start_entry, 1714 start, entry->end, countp, flags); 1715 return(NULL); 1716 } 1717 } 1718 1719 if (next->eflags & MAP_ENTRY_IN_TRANSITION) { 1720 vm_offset_t save_end = entry->end; 1721 next->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 1722 ++mycpu->gd_cnt.v_intrans_coll; 1723 ++mycpu->gd_cnt.v_intrans_wait; 1724 vm_map_transition_wait(map); 1725 1726 /* 1727 * clips might have occured while we blocked. 1728 */ 1729 CLIP_CHECK_FWD(entry, save_end); 1730 CLIP_CHECK_BACK(start_entry, start); 1731 continue; 1732 } 1733 /* 1734 * No restart necessary even though clip_end may block, we 1735 * are holding the map lock. 1736 */ 1737 vm_map_clip_end(map, next, end, countp); 1738 next->eflags |= MAP_ENTRY_IN_TRANSITION; 1739 entry = next; 1740 } 1741 if (flags & MAP_CLIP_NO_HOLES) { 1742 if (entry->end != end) { 1743 vm_map_unclip_range(map, start_entry, 1744 start, entry->end, countp, flags); 1745 return(NULL); 1746 } 1747 } 1748 return(start_entry); 1749 } 1750 1751 /* 1752 * Undo the effect of vm_map_clip_range(). You should pass the same 1753 * flags and the same range that you passed to vm_map_clip_range(). 1754 * This code will clear the in-transition flag on the entries and 1755 * wake up anyone waiting. This code will also simplify the sequence 1756 * and attempt to merge it with entries before and after the sequence. 1757 * 1758 * The map must be locked on entry and will remain locked on return. 1759 * 1760 * Note that you should also pass the start_entry returned by 1761 * vm_map_clip_range(). However, if you block between the two calls 1762 * with the map unlocked please be aware that the start_entry may 1763 * have been clipped and you may need to scan it backwards to find 1764 * the entry corresponding with the original start address. You are 1765 * responsible for this, vm_map_unclip_range() expects the correct 1766 * start_entry to be passed to it and will KASSERT otherwise. 1767 */ 1768 static 1769 void 1770 vm_map_unclip_range(vm_map_t map, vm_map_entry_t start_entry, 1771 vm_offset_t start, vm_offset_t end, 1772 int *countp, int flags) 1773 { 1774 vm_map_entry_t entry; 1775 1776 entry = start_entry; 1777 1778 KASSERT(entry->start == start, ("unclip_range: illegal base entry")); 1779 while (entry != &map->header && entry->start < end) { 1780 KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 1781 ("in-transition flag not set during unclip on: %p", 1782 entry)); 1783 KASSERT(entry->end <= end, 1784 ("unclip_range: tail wasn't clipped")); 1785 entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 1786 if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 1787 entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 1788 wakeup(map); 1789 } 1790 entry = entry->next; 1791 } 1792 1793 /* 1794 * Simplification does not block so there is no restart case. 1795 */ 1796 entry = start_entry; 1797 while (entry != &map->header && entry->start < end) { 1798 vm_map_simplify_entry(map, entry, countp); 1799 entry = entry->next; 1800 } 1801 } 1802 1803 /* 1804 * Mark the given range as handled by a subordinate map. 1805 * 1806 * This range must have been created with vm_map_find(), and no other 1807 * operations may have been performed on this range prior to calling 1808 * vm_map_submap(). 1809 * 1810 * Submappings cannot be removed. 1811 * 1812 * No requirements. 1813 */ 1814 int 1815 vm_map_submap(vm_map_t map, vm_offset_t start, vm_offset_t end, vm_map_t submap) 1816 { 1817 vm_map_entry_t entry; 1818 int result = KERN_INVALID_ARGUMENT; 1819 int count; 1820 1821 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 1822 vm_map_lock(map); 1823 1824 VM_MAP_RANGE_CHECK(map, start, end); 1825 1826 if (vm_map_lookup_entry(map, start, &entry)) { 1827 vm_map_clip_start(map, entry, start, &count); 1828 } else { 1829 entry = entry->next; 1830 } 1831 1832 vm_map_clip_end(map, entry, end, &count); 1833 1834 if ((entry->start == start) && (entry->end == end) && 1835 ((entry->eflags & MAP_ENTRY_COW) == 0) && 1836 (entry->object.vm_object == NULL)) { 1837 entry->object.sub_map = submap; 1838 entry->maptype = VM_MAPTYPE_SUBMAP; 1839 result = KERN_SUCCESS; 1840 } 1841 vm_map_unlock(map); 1842 vm_map_entry_release(count); 1843 1844 return (result); 1845 } 1846 1847 /* 1848 * Sets the protection of the specified address region in the target map. 1849 * If "set_max" is specified, the maximum protection is to be set; 1850 * otherwise, only the current protection is affected. 1851 * 1852 * The protection is not applicable to submaps, but is applicable to normal 1853 * maps and maps governed by virtual page tables. For example, when operating 1854 * on a virtual page table our protection basically controls how COW occurs 1855 * on the backing object, whereas the virtual page table abstraction itself 1856 * is an abstraction for userland. 1857 * 1858 * No requirements. 1859 */ 1860 int 1861 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1862 vm_prot_t new_prot, boolean_t set_max) 1863 { 1864 vm_map_entry_t current; 1865 vm_map_entry_t entry; 1866 int count; 1867 1868 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 1869 vm_map_lock(map); 1870 1871 VM_MAP_RANGE_CHECK(map, start, end); 1872 1873 if (vm_map_lookup_entry(map, start, &entry)) { 1874 vm_map_clip_start(map, entry, start, &count); 1875 } else { 1876 entry = entry->next; 1877 } 1878 1879 /* 1880 * Make a first pass to check for protection violations. 1881 */ 1882 current = entry; 1883 while ((current != &map->header) && (current->start < end)) { 1884 if (current->maptype == VM_MAPTYPE_SUBMAP) { 1885 vm_map_unlock(map); 1886 vm_map_entry_release(count); 1887 return (KERN_INVALID_ARGUMENT); 1888 } 1889 if ((new_prot & current->max_protection) != new_prot) { 1890 vm_map_unlock(map); 1891 vm_map_entry_release(count); 1892 return (KERN_PROTECTION_FAILURE); 1893 } 1894 current = current->next; 1895 } 1896 1897 /* 1898 * Go back and fix up protections. [Note that clipping is not 1899 * necessary the second time.] 1900 */ 1901 current = entry; 1902 1903 while ((current != &map->header) && (current->start < end)) { 1904 vm_prot_t old_prot; 1905 1906 vm_map_clip_end(map, current, end, &count); 1907 1908 old_prot = current->protection; 1909 if (set_max) { 1910 current->max_protection = new_prot; 1911 current->protection = new_prot & old_prot; 1912 } else { 1913 current->protection = new_prot; 1914 } 1915 1916 /* 1917 * Update physical map if necessary. Worry about copy-on-write 1918 * here -- CHECK THIS XXX 1919 */ 1920 1921 if (current->protection != old_prot) { 1922 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1923 VM_PROT_ALL) 1924 1925 pmap_protect(map->pmap, current->start, 1926 current->end, 1927 current->protection & MASK(current)); 1928 #undef MASK 1929 } 1930 1931 vm_map_simplify_entry(map, current, &count); 1932 1933 current = current->next; 1934 } 1935 1936 vm_map_unlock(map); 1937 vm_map_entry_release(count); 1938 return (KERN_SUCCESS); 1939 } 1940 1941 /* 1942 * This routine traverses a processes map handling the madvise 1943 * system call. Advisories are classified as either those effecting 1944 * the vm_map_entry structure, or those effecting the underlying 1945 * objects. 1946 * 1947 * The <value> argument is used for extended madvise calls. 1948 * 1949 * No requirements. 1950 */ 1951 int 1952 vm_map_madvise(vm_map_t map, vm_offset_t start, vm_offset_t end, 1953 int behav, off_t value) 1954 { 1955 vm_map_entry_t current, entry; 1956 int modify_map = 0; 1957 int error = 0; 1958 int count; 1959 1960 /* 1961 * Some madvise calls directly modify the vm_map_entry, in which case 1962 * we need to use an exclusive lock on the map and we need to perform 1963 * various clipping operations. Otherwise we only need a read-lock 1964 * on the map. 1965 */ 1966 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 1967 1968 switch(behav) { 1969 case MADV_NORMAL: 1970 case MADV_SEQUENTIAL: 1971 case MADV_RANDOM: 1972 case MADV_NOSYNC: 1973 case MADV_AUTOSYNC: 1974 case MADV_NOCORE: 1975 case MADV_CORE: 1976 case MADV_SETMAP: 1977 modify_map = 1; 1978 vm_map_lock(map); 1979 break; 1980 case MADV_INVAL: 1981 case MADV_WILLNEED: 1982 case MADV_DONTNEED: 1983 case MADV_FREE: 1984 vm_map_lock_read(map); 1985 break; 1986 default: 1987 vm_map_entry_release(count); 1988 return (EINVAL); 1989 } 1990 1991 /* 1992 * Locate starting entry and clip if necessary. 1993 */ 1994 1995 VM_MAP_RANGE_CHECK(map, start, end); 1996 1997 if (vm_map_lookup_entry(map, start, &entry)) { 1998 if (modify_map) 1999 vm_map_clip_start(map, entry, start, &count); 2000 } else { 2001 entry = entry->next; 2002 } 2003 2004 if (modify_map) { 2005 /* 2006 * madvise behaviors that are implemented in the vm_map_entry. 2007 * 2008 * We clip the vm_map_entry so that behavioral changes are 2009 * limited to the specified address range. 2010 */ 2011 for (current = entry; 2012 (current != &map->header) && (current->start < end); 2013 current = current->next 2014 ) { 2015 if (current->maptype == VM_MAPTYPE_SUBMAP) 2016 continue; 2017 2018 vm_map_clip_end(map, current, end, &count); 2019 2020 switch (behav) { 2021 case MADV_NORMAL: 2022 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 2023 break; 2024 case MADV_SEQUENTIAL: 2025 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 2026 break; 2027 case MADV_RANDOM: 2028 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 2029 break; 2030 case MADV_NOSYNC: 2031 current->eflags |= MAP_ENTRY_NOSYNC; 2032 break; 2033 case MADV_AUTOSYNC: 2034 current->eflags &= ~MAP_ENTRY_NOSYNC; 2035 break; 2036 case MADV_NOCORE: 2037 current->eflags |= MAP_ENTRY_NOCOREDUMP; 2038 break; 2039 case MADV_CORE: 2040 current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 2041 break; 2042 case MADV_SETMAP: 2043 /* 2044 * Set the page directory page for a map 2045 * governed by a virtual page table. Mark 2046 * the entry as being governed by a virtual 2047 * page table if it is not. 2048 * 2049 * XXX the page directory page is stored 2050 * in the avail_ssize field if the map_entry. 2051 * 2052 * XXX the map simplification code does not 2053 * compare this field so weird things may 2054 * happen if you do not apply this function 2055 * to the entire mapping governed by the 2056 * virtual page table. 2057 */ 2058 if (current->maptype != VM_MAPTYPE_VPAGETABLE) { 2059 error = EINVAL; 2060 break; 2061 } 2062 current->aux.master_pde = value; 2063 pmap_remove(map->pmap, 2064 current->start, current->end); 2065 break; 2066 case MADV_INVAL: 2067 /* 2068 * Invalidate the related pmap entries, used 2069 * to flush portions of the real kernel's 2070 * pmap when the caller has removed or 2071 * modified existing mappings in a virtual 2072 * page table. 2073 * 2074 * (exclusive locked map version does not 2075 * need the range interlock). 2076 */ 2077 pmap_remove(map->pmap, 2078 current->start, current->end); 2079 break; 2080 default: 2081 error = EINVAL; 2082 break; 2083 } 2084 vm_map_simplify_entry(map, current, &count); 2085 } 2086 vm_map_unlock(map); 2087 } else { 2088 vm_pindex_t pindex; 2089 vm_pindex_t delta; 2090 2091 /* 2092 * madvise behaviors that are implemented in the underlying 2093 * vm_object. 2094 * 2095 * Since we don't clip the vm_map_entry, we have to clip 2096 * the vm_object pindex and count. 2097 * 2098 * NOTE! These functions are only supported on normal maps, 2099 * except MADV_INVAL which is also supported on 2100 * virtual page tables. 2101 */ 2102 for (current = entry; 2103 (current != &map->header) && (current->start < end); 2104 current = current->next 2105 ) { 2106 vm_offset_t useStart; 2107 2108 if (current->maptype != VM_MAPTYPE_NORMAL && 2109 (current->maptype != VM_MAPTYPE_VPAGETABLE || 2110 behav != MADV_INVAL)) { 2111 continue; 2112 } 2113 2114 pindex = OFF_TO_IDX(current->offset); 2115 delta = atop(current->end - current->start); 2116 useStart = current->start; 2117 2118 if (current->start < start) { 2119 pindex += atop(start - current->start); 2120 delta -= atop(start - current->start); 2121 useStart = start; 2122 } 2123 if (current->end > end) 2124 delta -= atop(current->end - end); 2125 2126 if ((vm_spindex_t)delta <= 0) 2127 continue; 2128 2129 if (behav == MADV_INVAL) { 2130 /* 2131 * Invalidate the related pmap entries, used 2132 * to flush portions of the real kernel's 2133 * pmap when the caller has removed or 2134 * modified existing mappings in a virtual 2135 * page table. 2136 * 2137 * (shared locked map version needs the 2138 * interlock, see vm_fault()). 2139 */ 2140 struct vm_map_ilock ilock; 2141 2142 KASSERT(useStart >= VM_MIN_USER_ADDRESS && 2143 useStart + ptoa(delta) <= 2144 VM_MAX_USER_ADDRESS, 2145 ("Bad range %016jx-%016jx (%016jx)", 2146 useStart, useStart + ptoa(delta), 2147 delta)); 2148 vm_map_interlock(map, &ilock, 2149 useStart, 2150 useStart + ptoa(delta)); 2151 pmap_remove(map->pmap, 2152 useStart, 2153 useStart + ptoa(delta)); 2154 vm_map_deinterlock(map, &ilock); 2155 } else { 2156 vm_object_madvise(current->object.vm_object, 2157 pindex, delta, behav); 2158 } 2159 2160 /* 2161 * Try to populate the page table. Mappings governed 2162 * by virtual page tables cannot be pre-populated 2163 * without a lot of work so don't try. 2164 */ 2165 if (behav == MADV_WILLNEED && 2166 current->maptype != VM_MAPTYPE_VPAGETABLE) { 2167 pmap_object_init_pt( 2168 map->pmap, 2169 useStart, 2170 current->protection, 2171 current->object.vm_object, 2172 pindex, 2173 (count << PAGE_SHIFT), 2174 MAP_PREFAULT_MADVISE 2175 ); 2176 } 2177 } 2178 vm_map_unlock_read(map); 2179 } 2180 vm_map_entry_release(count); 2181 return(error); 2182 } 2183 2184 2185 /* 2186 * Sets the inheritance of the specified address range in the target map. 2187 * Inheritance affects how the map will be shared with child maps at the 2188 * time of vm_map_fork. 2189 */ 2190 int 2191 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 2192 vm_inherit_t new_inheritance) 2193 { 2194 vm_map_entry_t entry; 2195 vm_map_entry_t temp_entry; 2196 int count; 2197 2198 switch (new_inheritance) { 2199 case VM_INHERIT_NONE: 2200 case VM_INHERIT_COPY: 2201 case VM_INHERIT_SHARE: 2202 break; 2203 default: 2204 return (KERN_INVALID_ARGUMENT); 2205 } 2206 2207 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 2208 vm_map_lock(map); 2209 2210 VM_MAP_RANGE_CHECK(map, start, end); 2211 2212 if (vm_map_lookup_entry(map, start, &temp_entry)) { 2213 entry = temp_entry; 2214 vm_map_clip_start(map, entry, start, &count); 2215 } else 2216 entry = temp_entry->next; 2217 2218 while ((entry != &map->header) && (entry->start < end)) { 2219 vm_map_clip_end(map, entry, end, &count); 2220 2221 entry->inheritance = new_inheritance; 2222 2223 vm_map_simplify_entry(map, entry, &count); 2224 2225 entry = entry->next; 2226 } 2227 vm_map_unlock(map); 2228 vm_map_entry_release(count); 2229 return (KERN_SUCCESS); 2230 } 2231 2232 /* 2233 * Implement the semantics of mlock 2234 */ 2235 int 2236 vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, 2237 boolean_t new_pageable) 2238 { 2239 vm_map_entry_t entry; 2240 vm_map_entry_t start_entry; 2241 vm_offset_t end; 2242 int rv = KERN_SUCCESS; 2243 int count; 2244 2245 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 2246 vm_map_lock(map); 2247 VM_MAP_RANGE_CHECK(map, start, real_end); 2248 end = real_end; 2249 2250 start_entry = vm_map_clip_range(map, start, end, &count, 2251 MAP_CLIP_NO_HOLES); 2252 if (start_entry == NULL) { 2253 vm_map_unlock(map); 2254 vm_map_entry_release(count); 2255 return (KERN_INVALID_ADDRESS); 2256 } 2257 2258 if (new_pageable == 0) { 2259 entry = start_entry; 2260 while ((entry != &map->header) && (entry->start < end)) { 2261 vm_offset_t save_start; 2262 vm_offset_t save_end; 2263 2264 /* 2265 * Already user wired or hard wired (trivial cases) 2266 */ 2267 if (entry->eflags & MAP_ENTRY_USER_WIRED) { 2268 entry = entry->next; 2269 continue; 2270 } 2271 if (entry->wired_count != 0) { 2272 entry->wired_count++; 2273 entry->eflags |= MAP_ENTRY_USER_WIRED; 2274 entry = entry->next; 2275 continue; 2276 } 2277 2278 /* 2279 * A new wiring requires instantiation of appropriate 2280 * management structures and the faulting in of the 2281 * page. 2282 */ 2283 if (entry->maptype == VM_MAPTYPE_NORMAL || 2284 entry->maptype == VM_MAPTYPE_VPAGETABLE) { 2285 int copyflag = entry->eflags & 2286 MAP_ENTRY_NEEDS_COPY; 2287 if (copyflag && ((entry->protection & 2288 VM_PROT_WRITE) != 0)) { 2289 vm_map_entry_shadow(entry, 0); 2290 } else if (entry->object.vm_object == NULL && 2291 !map->system_map) { 2292 vm_map_entry_allocate_object(entry); 2293 } 2294 } 2295 entry->wired_count++; 2296 entry->eflags |= MAP_ENTRY_USER_WIRED; 2297 2298 /* 2299 * Now fault in the area. Note that vm_fault_wire() 2300 * may release the map lock temporarily, it will be 2301 * relocked on return. The in-transition 2302 * flag protects the entries. 2303 */ 2304 save_start = entry->start; 2305 save_end = entry->end; 2306 rv = vm_fault_wire(map, entry, TRUE, 0); 2307 if (rv) { 2308 CLIP_CHECK_BACK(entry, save_start); 2309 for (;;) { 2310 KASSERT(entry->wired_count == 1, ("bad wired_count on entry")); 2311 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2312 entry->wired_count = 0; 2313 if (entry->end == save_end) 2314 break; 2315 entry = entry->next; 2316 KASSERT(entry != &map->header, ("bad entry clip during backout")); 2317 } 2318 end = save_start; /* unwire the rest */ 2319 break; 2320 } 2321 /* 2322 * note that even though the entry might have been 2323 * clipped, the USER_WIRED flag we set prevents 2324 * duplication so we do not have to do a 2325 * clip check. 2326 */ 2327 entry = entry->next; 2328 } 2329 2330 /* 2331 * If we failed fall through to the unwiring section to 2332 * unwire what we had wired so far. 'end' has already 2333 * been adjusted. 2334 */ 2335 if (rv) 2336 new_pageable = 1; 2337 2338 /* 2339 * start_entry might have been clipped if we unlocked the 2340 * map and blocked. No matter how clipped it has gotten 2341 * there should be a fragment that is on our start boundary. 2342 */ 2343 CLIP_CHECK_BACK(start_entry, start); 2344 } 2345 2346 /* 2347 * Deal with the unwiring case. 2348 */ 2349 if (new_pageable) { 2350 /* 2351 * This is the unwiring case. We must first ensure that the 2352 * range to be unwired is really wired down. We know there 2353 * are no holes. 2354 */ 2355 entry = start_entry; 2356 while ((entry != &map->header) && (entry->start < end)) { 2357 if ((entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 2358 rv = KERN_INVALID_ARGUMENT; 2359 goto done; 2360 } 2361 KASSERT(entry->wired_count != 0, ("wired count was 0 with USER_WIRED set! %p", entry)); 2362 entry = entry->next; 2363 } 2364 2365 /* 2366 * Now decrement the wiring count for each region. If a region 2367 * becomes completely unwired, unwire its physical pages and 2368 * mappings. 2369 */ 2370 /* 2371 * The map entries are processed in a loop, checking to 2372 * make sure the entry is wired and asserting it has a wired 2373 * count. However, another loop was inserted more-or-less in 2374 * the middle of the unwiring path. This loop picks up the 2375 * "entry" loop variable from the first loop without first 2376 * setting it to start_entry. Naturally, the secound loop 2377 * is never entered and the pages backing the entries are 2378 * never unwired. This can lead to a leak of wired pages. 2379 */ 2380 entry = start_entry; 2381 while ((entry != &map->header) && (entry->start < end)) { 2382 KASSERT(entry->eflags & MAP_ENTRY_USER_WIRED, 2383 ("expected USER_WIRED on entry %p", entry)); 2384 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2385 entry->wired_count--; 2386 if (entry->wired_count == 0) 2387 vm_fault_unwire(map, entry); 2388 entry = entry->next; 2389 } 2390 } 2391 done: 2392 vm_map_unclip_range(map, start_entry, start, real_end, &count, 2393 MAP_CLIP_NO_HOLES); 2394 map->timestamp++; 2395 vm_map_unlock(map); 2396 vm_map_entry_release(count); 2397 return (rv); 2398 } 2399 2400 /* 2401 * Sets the pageability of the specified address range in the target map. 2402 * Regions specified as not pageable require locked-down physical 2403 * memory and physical page maps. 2404 * 2405 * The map must not be locked, but a reference must remain to the map 2406 * throughout the call. 2407 * 2408 * This function may be called via the zalloc path and must properly 2409 * reserve map entries for kernel_map. 2410 * 2411 * No requirements. 2412 */ 2413 int 2414 vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t real_end, int kmflags) 2415 { 2416 vm_map_entry_t entry; 2417 vm_map_entry_t start_entry; 2418 vm_offset_t end; 2419 int rv = KERN_SUCCESS; 2420 int count; 2421 2422 if (kmflags & KM_KRESERVE) 2423 count = vm_map_entry_kreserve(MAP_RESERVE_COUNT); 2424 else 2425 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 2426 vm_map_lock(map); 2427 VM_MAP_RANGE_CHECK(map, start, real_end); 2428 end = real_end; 2429 2430 start_entry = vm_map_clip_range(map, start, end, &count, 2431 MAP_CLIP_NO_HOLES); 2432 if (start_entry == NULL) { 2433 vm_map_unlock(map); 2434 rv = KERN_INVALID_ADDRESS; 2435 goto failure; 2436 } 2437 if ((kmflags & KM_PAGEABLE) == 0) { 2438 /* 2439 * Wiring. 2440 * 2441 * 1. Holding the write lock, we create any shadow or zero-fill 2442 * objects that need to be created. Then we clip each map 2443 * entry to the region to be wired and increment its wiring 2444 * count. We create objects before clipping the map entries 2445 * to avoid object proliferation. 2446 * 2447 * 2. We downgrade to a read lock, and call vm_fault_wire to 2448 * fault in the pages for any newly wired area (wired_count is 2449 * 1). 2450 * 2451 * Downgrading to a read lock for vm_fault_wire avoids a 2452 * possible deadlock with another process that may have faulted 2453 * on one of the pages to be wired (it would mark the page busy, 2454 * blocking us, then in turn block on the map lock that we 2455 * hold). Because of problems in the recursive lock package, 2456 * we cannot upgrade to a write lock in vm_map_lookup. Thus, 2457 * any actions that require the write lock must be done 2458 * beforehand. Because we keep the read lock on the map, the 2459 * copy-on-write status of the entries we modify here cannot 2460 * change. 2461 */ 2462 entry = start_entry; 2463 while ((entry != &map->header) && (entry->start < end)) { 2464 /* 2465 * Trivial case if the entry is already wired 2466 */ 2467 if (entry->wired_count) { 2468 entry->wired_count++; 2469 entry = entry->next; 2470 continue; 2471 } 2472 2473 /* 2474 * The entry is being newly wired, we have to setup 2475 * appropriate management structures. A shadow 2476 * object is required for a copy-on-write region, 2477 * or a normal object for a zero-fill region. We 2478 * do not have to do this for entries that point to sub 2479 * maps because we won't hold the lock on the sub map. 2480 */ 2481 if (entry->maptype == VM_MAPTYPE_NORMAL || 2482 entry->maptype == VM_MAPTYPE_VPAGETABLE) { 2483 int copyflag = entry->eflags & 2484 MAP_ENTRY_NEEDS_COPY; 2485 if (copyflag && ((entry->protection & 2486 VM_PROT_WRITE) != 0)) { 2487 vm_map_entry_shadow(entry, 0); 2488 } else if (entry->object.vm_object == NULL && 2489 !map->system_map) { 2490 vm_map_entry_allocate_object(entry); 2491 } 2492 } 2493 2494 entry->wired_count++; 2495 entry = entry->next; 2496 } 2497 2498 /* 2499 * Pass 2. 2500 */ 2501 2502 /* 2503 * HACK HACK HACK HACK 2504 * 2505 * vm_fault_wire() temporarily unlocks the map to avoid 2506 * deadlocks. The in-transition flag from vm_map_clip_range 2507 * call should protect us from changes while the map is 2508 * unlocked. T 2509 * 2510 * NOTE: Previously this comment stated that clipping might 2511 * still occur while the entry is unlocked, but from 2512 * what I can tell it actually cannot. 2513 * 2514 * It is unclear whether the CLIP_CHECK_*() calls 2515 * are still needed but we keep them in anyway. 2516 * 2517 * HACK HACK HACK HACK 2518 */ 2519 2520 entry = start_entry; 2521 while (entry != &map->header && entry->start < end) { 2522 /* 2523 * If vm_fault_wire fails for any page we need to undo 2524 * what has been done. We decrement the wiring count 2525 * for those pages which have not yet been wired (now) 2526 * and unwire those that have (later). 2527 */ 2528 vm_offset_t save_start = entry->start; 2529 vm_offset_t save_end = entry->end; 2530 2531 if (entry->wired_count == 1) 2532 rv = vm_fault_wire(map, entry, FALSE, kmflags); 2533 if (rv) { 2534 CLIP_CHECK_BACK(entry, save_start); 2535 for (;;) { 2536 KASSERT(entry->wired_count == 1, ("wired_count changed unexpectedly")); 2537 entry->wired_count = 0; 2538 if (entry->end == save_end) 2539 break; 2540 entry = entry->next; 2541 KASSERT(entry != &map->header, ("bad entry clip during backout")); 2542 } 2543 end = save_start; 2544 break; 2545 } 2546 CLIP_CHECK_FWD(entry, save_end); 2547 entry = entry->next; 2548 } 2549 2550 /* 2551 * If a failure occured undo everything by falling through 2552 * to the unwiring code. 'end' has already been adjusted 2553 * appropriately. 2554 */ 2555 if (rv) 2556 kmflags |= KM_PAGEABLE; 2557 2558 /* 2559 * start_entry is still IN_TRANSITION but may have been 2560 * clipped since vm_fault_wire() unlocks and relocks the 2561 * map. No matter how clipped it has gotten there should 2562 * be a fragment that is on our start boundary. 2563 */ 2564 CLIP_CHECK_BACK(start_entry, start); 2565 } 2566 2567 if (kmflags & KM_PAGEABLE) { 2568 /* 2569 * This is the unwiring case. We must first ensure that the 2570 * range to be unwired is really wired down. We know there 2571 * are no holes. 2572 */ 2573 entry = start_entry; 2574 while ((entry != &map->header) && (entry->start < end)) { 2575 if (entry->wired_count == 0) { 2576 rv = KERN_INVALID_ARGUMENT; 2577 goto done; 2578 } 2579 entry = entry->next; 2580 } 2581 2582 /* 2583 * Now decrement the wiring count for each region. If a region 2584 * becomes completely unwired, unwire its physical pages and 2585 * mappings. 2586 */ 2587 entry = start_entry; 2588 while ((entry != &map->header) && (entry->start < end)) { 2589 entry->wired_count--; 2590 if (entry->wired_count == 0) 2591 vm_fault_unwire(map, entry); 2592 entry = entry->next; 2593 } 2594 } 2595 done: 2596 vm_map_unclip_range(map, start_entry, start, real_end, 2597 &count, MAP_CLIP_NO_HOLES); 2598 map->timestamp++; 2599 vm_map_unlock(map); 2600 failure: 2601 if (kmflags & KM_KRESERVE) 2602 vm_map_entry_krelease(count); 2603 else 2604 vm_map_entry_release(count); 2605 return (rv); 2606 } 2607 2608 /* 2609 * Mark a newly allocated address range as wired but do not fault in 2610 * the pages. The caller is expected to load the pages into the object. 2611 * 2612 * The map must be locked on entry and will remain locked on return. 2613 * No other requirements. 2614 */ 2615 void 2616 vm_map_set_wired_quick(vm_map_t map, vm_offset_t addr, vm_size_t size, 2617 int *countp) 2618 { 2619 vm_map_entry_t scan; 2620 vm_map_entry_t entry; 2621 2622 entry = vm_map_clip_range(map, addr, addr + size, 2623 countp, MAP_CLIP_NO_HOLES); 2624 for (scan = entry; 2625 scan != &map->header && scan->start < addr + size; 2626 scan = scan->next) { 2627 KKASSERT(scan->wired_count == 0); 2628 scan->wired_count = 1; 2629 } 2630 vm_map_unclip_range(map, entry, addr, addr + size, 2631 countp, MAP_CLIP_NO_HOLES); 2632 } 2633 2634 /* 2635 * Push any dirty cached pages in the address range to their pager. 2636 * If syncio is TRUE, dirty pages are written synchronously. 2637 * If invalidate is TRUE, any cached pages are freed as well. 2638 * 2639 * This routine is called by sys_msync() 2640 * 2641 * Returns an error if any part of the specified range is not mapped. 2642 * 2643 * No requirements. 2644 */ 2645 int 2646 vm_map_clean(vm_map_t map, vm_offset_t start, vm_offset_t end, 2647 boolean_t syncio, boolean_t invalidate) 2648 { 2649 vm_map_entry_t current; 2650 vm_map_entry_t entry; 2651 vm_size_t size; 2652 vm_object_t object; 2653 vm_object_t tobj; 2654 vm_ooffset_t offset; 2655 2656 vm_map_lock_read(map); 2657 VM_MAP_RANGE_CHECK(map, start, end); 2658 if (!vm_map_lookup_entry(map, start, &entry)) { 2659 vm_map_unlock_read(map); 2660 return (KERN_INVALID_ADDRESS); 2661 } 2662 lwkt_gettoken(&map->token); 2663 2664 /* 2665 * Make a first pass to check for holes. 2666 */ 2667 for (current = entry; current->start < end; current = current->next) { 2668 if (current->maptype == VM_MAPTYPE_SUBMAP) { 2669 lwkt_reltoken(&map->token); 2670 vm_map_unlock_read(map); 2671 return (KERN_INVALID_ARGUMENT); 2672 } 2673 if (end > current->end && 2674 (current->next == &map->header || 2675 current->end != current->next->start)) { 2676 lwkt_reltoken(&map->token); 2677 vm_map_unlock_read(map); 2678 return (KERN_INVALID_ADDRESS); 2679 } 2680 } 2681 2682 if (invalidate) 2683 pmap_remove(vm_map_pmap(map), start, end); 2684 2685 /* 2686 * Make a second pass, cleaning/uncaching pages from the indicated 2687 * objects as we go. 2688 */ 2689 for (current = entry; current->start < end; current = current->next) { 2690 offset = current->offset + (start - current->start); 2691 size = (end <= current->end ? end : current->end) - start; 2692 2693 switch(current->maptype) { 2694 case VM_MAPTYPE_SUBMAP: 2695 { 2696 vm_map_t smap; 2697 vm_map_entry_t tentry; 2698 vm_size_t tsize; 2699 2700 smap = current->object.sub_map; 2701 vm_map_lock_read(smap); 2702 vm_map_lookup_entry(smap, offset, &tentry); 2703 tsize = tentry->end - offset; 2704 if (tsize < size) 2705 size = tsize; 2706 object = tentry->object.vm_object; 2707 offset = tentry->offset + (offset - tentry->start); 2708 vm_map_unlock_read(smap); 2709 break; 2710 } 2711 case VM_MAPTYPE_NORMAL: 2712 case VM_MAPTYPE_VPAGETABLE: 2713 object = current->object.vm_object; 2714 break; 2715 default: 2716 object = NULL; 2717 break; 2718 } 2719 2720 if (object) 2721 vm_object_hold(object); 2722 2723 /* 2724 * Note that there is absolutely no sense in writing out 2725 * anonymous objects, so we track down the vnode object 2726 * to write out. 2727 * We invalidate (remove) all pages from the address space 2728 * anyway, for semantic correctness. 2729 * 2730 * note: certain anonymous maps, such as MAP_NOSYNC maps, 2731 * may start out with a NULL object. 2732 */ 2733 while (object && (tobj = object->backing_object) != NULL) { 2734 vm_object_hold(tobj); 2735 if (tobj == object->backing_object) { 2736 vm_object_lock_swap(); 2737 offset += object->backing_object_offset; 2738 vm_object_drop(object); 2739 object = tobj; 2740 if (object->size < OFF_TO_IDX(offset + size)) 2741 size = IDX_TO_OFF(object->size) - 2742 offset; 2743 break; 2744 } 2745 vm_object_drop(tobj); 2746 } 2747 if (object && (object->type == OBJT_VNODE) && 2748 (current->protection & VM_PROT_WRITE) && 2749 (object->flags & OBJ_NOMSYNC) == 0) { 2750 /* 2751 * Flush pages if writing is allowed, invalidate them 2752 * if invalidation requested. Pages undergoing I/O 2753 * will be ignored by vm_object_page_remove(). 2754 * 2755 * We cannot lock the vnode and then wait for paging 2756 * to complete without deadlocking against vm_fault. 2757 * Instead we simply call vm_object_page_remove() and 2758 * allow it to block internally on a page-by-page 2759 * basis when it encounters pages undergoing async 2760 * I/O. 2761 */ 2762 int flags; 2763 2764 /* no chain wait needed for vnode objects */ 2765 vm_object_reference_locked(object); 2766 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY); 2767 flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 2768 flags |= invalidate ? OBJPC_INVAL : 0; 2769 2770 /* 2771 * When operating on a virtual page table just 2772 * flush the whole object. XXX we probably ought 2773 * to 2774 */ 2775 switch(current->maptype) { 2776 case VM_MAPTYPE_NORMAL: 2777 vm_object_page_clean(object, 2778 OFF_TO_IDX(offset), 2779 OFF_TO_IDX(offset + size + PAGE_MASK), 2780 flags); 2781 break; 2782 case VM_MAPTYPE_VPAGETABLE: 2783 vm_object_page_clean(object, 0, 0, flags); 2784 break; 2785 } 2786 vn_unlock(((struct vnode *)object->handle)); 2787 vm_object_deallocate_locked(object); 2788 } 2789 if (object && invalidate && 2790 ((object->type == OBJT_VNODE) || 2791 (object->type == OBJT_DEVICE) || 2792 (object->type == OBJT_MGTDEVICE))) { 2793 int clean_only = 2794 ((object->type == OBJT_DEVICE) || 2795 (object->type == OBJT_MGTDEVICE)) ? FALSE : TRUE; 2796 /* no chain wait needed for vnode/device objects */ 2797 vm_object_reference_locked(object); 2798 switch(current->maptype) { 2799 case VM_MAPTYPE_NORMAL: 2800 vm_object_page_remove(object, 2801 OFF_TO_IDX(offset), 2802 OFF_TO_IDX(offset + size + PAGE_MASK), 2803 clean_only); 2804 break; 2805 case VM_MAPTYPE_VPAGETABLE: 2806 vm_object_page_remove(object, 0, 0, clean_only); 2807 break; 2808 } 2809 vm_object_deallocate_locked(object); 2810 } 2811 start += size; 2812 if (object) 2813 vm_object_drop(object); 2814 } 2815 2816 lwkt_reltoken(&map->token); 2817 vm_map_unlock_read(map); 2818 2819 return (KERN_SUCCESS); 2820 } 2821 2822 /* 2823 * Make the region specified by this entry pageable. 2824 * 2825 * The vm_map must be exclusively locked. 2826 */ 2827 static void 2828 vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2829 { 2830 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2831 entry->wired_count = 0; 2832 vm_fault_unwire(map, entry); 2833 } 2834 2835 /* 2836 * Deallocate the given entry from the target map. 2837 * 2838 * The vm_map must be exclusively locked. 2839 */ 2840 static void 2841 vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry, int *countp) 2842 { 2843 vm_map_entry_unlink(map, entry); 2844 map->size -= entry->end - entry->start; 2845 2846 switch(entry->maptype) { 2847 case VM_MAPTYPE_NORMAL: 2848 case VM_MAPTYPE_VPAGETABLE: 2849 case VM_MAPTYPE_SUBMAP: 2850 vm_object_deallocate(entry->object.vm_object); 2851 break; 2852 case VM_MAPTYPE_UKSMAP: 2853 /* XXX TODO */ 2854 break; 2855 default: 2856 break; 2857 } 2858 2859 vm_map_entry_dispose(map, entry, countp); 2860 } 2861 2862 /* 2863 * Deallocates the given address range from the target map. 2864 * 2865 * The vm_map must be exclusively locked. 2866 */ 2867 int 2868 vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end, int *countp) 2869 { 2870 vm_object_t object; 2871 vm_map_entry_t entry; 2872 vm_map_entry_t first_entry; 2873 2874 ASSERT_VM_MAP_LOCKED(map); 2875 lwkt_gettoken(&map->token); 2876 again: 2877 /* 2878 * Find the start of the region, and clip it. Set entry to point 2879 * at the first record containing the requested address or, if no 2880 * such record exists, the next record with a greater address. The 2881 * loop will run from this point until a record beyond the termination 2882 * address is encountered. 2883 * 2884 * map->hint must be adjusted to not point to anything we delete, 2885 * so set it to the entry prior to the one being deleted. 2886 * 2887 * GGG see other GGG comment. 2888 */ 2889 if (vm_map_lookup_entry(map, start, &first_entry)) { 2890 entry = first_entry; 2891 vm_map_clip_start(map, entry, start, countp); 2892 map->hint = entry->prev; /* possible problem XXX */ 2893 } else { 2894 map->hint = first_entry; /* possible problem XXX */ 2895 entry = first_entry->next; 2896 } 2897 2898 /* 2899 * If a hole opens up prior to the current first_free then 2900 * adjust first_free. As with map->hint, map->first_free 2901 * cannot be left set to anything we might delete. 2902 */ 2903 if (entry == &map->header) { 2904 map->first_free = &map->header; 2905 } else if (map->first_free->start >= start) { 2906 map->first_free = entry->prev; 2907 } 2908 2909 /* 2910 * Step through all entries in this region 2911 */ 2912 while ((entry != &map->header) && (entry->start < end)) { 2913 vm_map_entry_t next; 2914 vm_offset_t s, e; 2915 vm_pindex_t offidxstart, offidxend, count; 2916 2917 /* 2918 * If we hit an in-transition entry we have to sleep and 2919 * retry. It's easier (and not really slower) to just retry 2920 * since this case occurs so rarely and the hint is already 2921 * pointing at the right place. We have to reset the 2922 * start offset so as not to accidently delete an entry 2923 * another process just created in vacated space. 2924 */ 2925 if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 2926 entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 2927 start = entry->start; 2928 ++mycpu->gd_cnt.v_intrans_coll; 2929 ++mycpu->gd_cnt.v_intrans_wait; 2930 vm_map_transition_wait(map); 2931 goto again; 2932 } 2933 vm_map_clip_end(map, entry, end, countp); 2934 2935 s = entry->start; 2936 e = entry->end; 2937 next = entry->next; 2938 2939 offidxstart = OFF_TO_IDX(entry->offset); 2940 count = OFF_TO_IDX(e - s); 2941 2942 switch(entry->maptype) { 2943 case VM_MAPTYPE_NORMAL: 2944 case VM_MAPTYPE_VPAGETABLE: 2945 case VM_MAPTYPE_SUBMAP: 2946 object = entry->object.vm_object; 2947 break; 2948 default: 2949 object = NULL; 2950 break; 2951 } 2952 2953 /* 2954 * Unwire before removing addresses from the pmap; otherwise, 2955 * unwiring will put the entries back in the pmap. 2956 */ 2957 if (entry->wired_count != 0) 2958 vm_map_entry_unwire(map, entry); 2959 2960 offidxend = offidxstart + count; 2961 2962 if (object == &kernel_object) { 2963 vm_object_hold(object); 2964 vm_object_page_remove(object, offidxstart, 2965 offidxend, FALSE); 2966 vm_object_drop(object); 2967 } else if (object && object->type != OBJT_DEFAULT && 2968 object->type != OBJT_SWAP) { 2969 /* 2970 * vnode object routines cannot be chain-locked, 2971 * but since we aren't removing pages from the 2972 * object here we can use a shared hold. 2973 */ 2974 vm_object_hold_shared(object); 2975 pmap_remove(map->pmap, s, e); 2976 vm_object_drop(object); 2977 } else if (object) { 2978 vm_object_hold(object); 2979 vm_object_chain_acquire(object, 0); 2980 pmap_remove(map->pmap, s, e); 2981 2982 if (object != NULL && 2983 object->ref_count != 1 && 2984 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == 2985 OBJ_ONEMAPPING && 2986 (object->type == OBJT_DEFAULT || 2987 object->type == OBJT_SWAP)) { 2988 vm_object_collapse(object, NULL); 2989 vm_object_page_remove(object, offidxstart, 2990 offidxend, FALSE); 2991 if (object->type == OBJT_SWAP) { 2992 swap_pager_freespace(object, 2993 offidxstart, 2994 count); 2995 } 2996 if (offidxend >= object->size && 2997 offidxstart < object->size) { 2998 object->size = offidxstart; 2999 } 3000 } 3001 vm_object_chain_release(object); 3002 vm_object_drop(object); 3003 } else if (entry->maptype == VM_MAPTYPE_UKSMAP) { 3004 pmap_remove(map->pmap, s, e); 3005 } 3006 3007 /* 3008 * Delete the entry (which may delete the object) only after 3009 * removing all pmap entries pointing to its pages. 3010 * (Otherwise, its page frames may be reallocated, and any 3011 * modify bits will be set in the wrong object!) 3012 */ 3013 vm_map_entry_delete(map, entry, countp); 3014 entry = next; 3015 } 3016 lwkt_reltoken(&map->token); 3017 return (KERN_SUCCESS); 3018 } 3019 3020 /* 3021 * Remove the given address range from the target map. 3022 * This is the exported form of vm_map_delete. 3023 * 3024 * No requirements. 3025 */ 3026 int 3027 vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 3028 { 3029 int result; 3030 int count; 3031 3032 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 3033 vm_map_lock(map); 3034 VM_MAP_RANGE_CHECK(map, start, end); 3035 result = vm_map_delete(map, start, end, &count); 3036 vm_map_unlock(map); 3037 vm_map_entry_release(count); 3038 3039 return (result); 3040 } 3041 3042 /* 3043 * Assert that the target map allows the specified privilege on the 3044 * entire address region given. The entire region must be allocated. 3045 * 3046 * The caller must specify whether the vm_map is already locked or not. 3047 */ 3048 boolean_t 3049 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 3050 vm_prot_t protection, boolean_t have_lock) 3051 { 3052 vm_map_entry_t entry; 3053 vm_map_entry_t tmp_entry; 3054 boolean_t result; 3055 3056 if (have_lock == FALSE) 3057 vm_map_lock_read(map); 3058 3059 if (!vm_map_lookup_entry(map, start, &tmp_entry)) { 3060 if (have_lock == FALSE) 3061 vm_map_unlock_read(map); 3062 return (FALSE); 3063 } 3064 entry = tmp_entry; 3065 3066 result = TRUE; 3067 while (start < end) { 3068 if (entry == &map->header) { 3069 result = FALSE; 3070 break; 3071 } 3072 /* 3073 * No holes allowed! 3074 */ 3075 3076 if (start < entry->start) { 3077 result = FALSE; 3078 break; 3079 } 3080 /* 3081 * Check protection associated with entry. 3082 */ 3083 3084 if ((entry->protection & protection) != protection) { 3085 result = FALSE; 3086 break; 3087 } 3088 /* go to next entry */ 3089 3090 start = entry->end; 3091 entry = entry->next; 3092 } 3093 if (have_lock == FALSE) 3094 vm_map_unlock_read(map); 3095 return (result); 3096 } 3097 3098 /* 3099 * If appropriate this function shadows the original object with a new object 3100 * and moves the VM pages from the original object to the new object. 3101 * The original object will also be collapsed, if possible. 3102 * 3103 * We can only do this for normal memory objects with a single mapping, and 3104 * it only makes sense to do it if there are 2 or more refs on the original 3105 * object. i.e. typically a memory object that has been extended into 3106 * multiple vm_map_entry's with non-overlapping ranges. 3107 * 3108 * This makes it easier to remove unused pages and keeps object inheritance 3109 * from being a negative impact on memory usage. 3110 * 3111 * On return the (possibly new) entry->object.vm_object will have an 3112 * additional ref on it for the caller to dispose of (usually by cloning 3113 * the vm_map_entry). The additional ref had to be done in this routine 3114 * to avoid racing a collapse. The object's ONEMAPPING flag will also be 3115 * cleared. 3116 * 3117 * The vm_map must be locked and its token held. 3118 */ 3119 static void 3120 vm_map_split(vm_map_entry_t entry) 3121 { 3122 /* OPTIMIZED */ 3123 vm_object_t oobject, nobject, bobject; 3124 vm_offset_t s, e; 3125 vm_page_t m; 3126 vm_pindex_t offidxstart, offidxend, idx; 3127 vm_size_t size; 3128 vm_ooffset_t offset; 3129 int useshadowlist; 3130 3131 /* 3132 * Optimize away object locks for vnode objects. Important exit/exec 3133 * critical path. 3134 * 3135 * OBJ_ONEMAPPING doesn't apply to vnode objects but clear the flag 3136 * anyway. 3137 */ 3138 oobject = entry->object.vm_object; 3139 if (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) { 3140 vm_object_reference_quick(oobject); 3141 vm_object_clear_flag(oobject, OBJ_ONEMAPPING); 3142 return; 3143 } 3144 3145 /* 3146 * Setup. Chain lock the original object throughout the entire 3147 * routine to prevent new page faults from occuring. 3148 * 3149 * XXX can madvise WILLNEED interfere with us too? 3150 */ 3151 vm_object_hold(oobject); 3152 vm_object_chain_acquire(oobject, 0); 3153 3154 /* 3155 * Original object cannot be split? Might have also changed state. 3156 */ 3157 if (oobject->handle == NULL || (oobject->type != OBJT_DEFAULT && 3158 oobject->type != OBJT_SWAP)) { 3159 vm_object_chain_release(oobject); 3160 vm_object_reference_locked(oobject); 3161 vm_object_clear_flag(oobject, OBJ_ONEMAPPING); 3162 vm_object_drop(oobject); 3163 return; 3164 } 3165 3166 /* 3167 * Collapse original object with its backing store as an 3168 * optimization to reduce chain lengths when possible. 3169 * 3170 * If ref_count <= 1 there aren't other non-overlapping vm_map_entry's 3171 * for oobject, so there's no point collapsing it. 3172 * 3173 * Then re-check whether the object can be split. 3174 */ 3175 vm_object_collapse(oobject, NULL); 3176 3177 if (oobject->ref_count <= 1 || 3178 (oobject->type != OBJT_DEFAULT && oobject->type != OBJT_SWAP) || 3179 (oobject->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) != OBJ_ONEMAPPING) { 3180 vm_object_chain_release(oobject); 3181 vm_object_reference_locked(oobject); 3182 vm_object_clear_flag(oobject, OBJ_ONEMAPPING); 3183 vm_object_drop(oobject); 3184 return; 3185 } 3186 3187 /* 3188 * Acquire the chain lock on the backing object. 3189 * 3190 * Give bobject an additional ref count for when it will be shadowed 3191 * by nobject. 3192 */ 3193 useshadowlist = 0; 3194 if ((bobject = oobject->backing_object) != NULL) { 3195 if (bobject->type != OBJT_VNODE) { 3196 useshadowlist = 1; 3197 vm_object_hold(bobject); 3198 vm_object_chain_wait(bobject, 0); 3199 /* ref for shadowing below */ 3200 vm_object_reference_locked(bobject); 3201 vm_object_chain_acquire(bobject, 0); 3202 KKASSERT(bobject->backing_object == bobject); 3203 KKASSERT((bobject->flags & OBJ_DEAD) == 0); 3204 } else { 3205 /* 3206 * vnodes are not placed on the shadow list but 3207 * they still get another ref for the backing_object 3208 * reference. 3209 */ 3210 vm_object_reference_quick(bobject); 3211 } 3212 } 3213 3214 /* 3215 * Calculate the object page range and allocate the new object. 3216 */ 3217 offset = entry->offset; 3218 s = entry->start; 3219 e = entry->end; 3220 3221 offidxstart = OFF_TO_IDX(offset); 3222 offidxend = offidxstart + OFF_TO_IDX(e - s); 3223 size = offidxend - offidxstart; 3224 3225 switch(oobject->type) { 3226 case OBJT_DEFAULT: 3227 nobject = default_pager_alloc(NULL, IDX_TO_OFF(size), 3228 VM_PROT_ALL, 0); 3229 break; 3230 case OBJT_SWAP: 3231 nobject = swap_pager_alloc(NULL, IDX_TO_OFF(size), 3232 VM_PROT_ALL, 0); 3233 break; 3234 default: 3235 /* not reached */ 3236 nobject = NULL; 3237 KKASSERT(0); 3238 } 3239 3240 if (nobject == NULL) { 3241 if (bobject) { 3242 if (useshadowlist) { 3243 vm_object_chain_release(bobject); 3244 vm_object_deallocate(bobject); 3245 vm_object_drop(bobject); 3246 } else { 3247 vm_object_deallocate(bobject); 3248 } 3249 } 3250 vm_object_chain_release(oobject); 3251 vm_object_reference_locked(oobject); 3252 vm_object_clear_flag(oobject, OBJ_ONEMAPPING); 3253 vm_object_drop(oobject); 3254 return; 3255 } 3256 3257 /* 3258 * The new object will replace entry->object.vm_object so it needs 3259 * a second reference (the caller expects an additional ref). 3260 */ 3261 vm_object_hold(nobject); 3262 vm_object_reference_locked(nobject); 3263 vm_object_chain_acquire(nobject, 0); 3264 3265 /* 3266 * nobject shadows bobject (oobject already shadows bobject). 3267 * 3268 * Adding an object to bobject's shadow list requires refing bobject 3269 * which we did above in the useshadowlist case. 3270 */ 3271 if (bobject) { 3272 nobject->backing_object_offset = 3273 oobject->backing_object_offset + IDX_TO_OFF(offidxstart); 3274 nobject->backing_object = bobject; 3275 if (useshadowlist) { 3276 bobject->shadow_count++; 3277 atomic_add_int(&bobject->generation, 1); 3278 LIST_INSERT_HEAD(&bobject->shadow_head, 3279 nobject, shadow_list); 3280 vm_object_clear_flag(bobject, OBJ_ONEMAPPING); /*XXX*/ 3281 vm_object_chain_release(bobject); 3282 vm_object_drop(bobject); 3283 vm_object_set_flag(nobject, OBJ_ONSHADOW); 3284 } 3285 } 3286 3287 /* 3288 * Move the VM pages from oobject to nobject 3289 */ 3290 for (idx = 0; idx < size; idx++) { 3291 vm_page_t m; 3292 3293 m = vm_page_lookup_busy_wait(oobject, offidxstart + idx, 3294 TRUE, "vmpg"); 3295 if (m == NULL) 3296 continue; 3297 3298 /* 3299 * We must wait for pending I/O to complete before we can 3300 * rename the page. 3301 * 3302 * We do not have to VM_PROT_NONE the page as mappings should 3303 * not be changed by this operation. 3304 * 3305 * NOTE: The act of renaming a page updates chaingen for both 3306 * objects. 3307 */ 3308 vm_page_rename(m, nobject, idx); 3309 /* page automatically made dirty by rename and cache handled */ 3310 /* page remains busy */ 3311 } 3312 3313 if (oobject->type == OBJT_SWAP) { 3314 vm_object_pip_add(oobject, 1); 3315 /* 3316 * copy oobject pages into nobject and destroy unneeded 3317 * pages in shadow object. 3318 */ 3319 swap_pager_copy(oobject, nobject, offidxstart, 0); 3320 vm_object_pip_wakeup(oobject); 3321 } 3322 3323 /* 3324 * Wakeup the pages we played with. No spl protection is needed 3325 * for a simple wakeup. 3326 */ 3327 for (idx = 0; idx < size; idx++) { 3328 m = vm_page_lookup(nobject, idx); 3329 if (m) { 3330 KKASSERT(m->flags & PG_BUSY); 3331 vm_page_wakeup(m); 3332 } 3333 } 3334 entry->object.vm_object = nobject; 3335 entry->offset = 0LL; 3336 3337 /* 3338 * Cleanup 3339 * 3340 * NOTE: There is no need to remove OBJ_ONEMAPPING from oobject, the 3341 * related pages were moved and are no longer applicable to the 3342 * original object. 3343 * 3344 * NOTE: Deallocate oobject (due to its entry->object.vm_object being 3345 * replaced by nobject). 3346 */ 3347 vm_object_chain_release(nobject); 3348 vm_object_drop(nobject); 3349 if (bobject && useshadowlist) { 3350 vm_object_chain_release(bobject); 3351 vm_object_drop(bobject); 3352 } 3353 vm_object_chain_release(oobject); 3354 /*vm_object_clear_flag(oobject, OBJ_ONEMAPPING);*/ 3355 vm_object_deallocate_locked(oobject); 3356 vm_object_drop(oobject); 3357 } 3358 3359 /* 3360 * Copies the contents of the source entry to the destination 3361 * entry. The entries *must* be aligned properly. 3362 * 3363 * The vm_maps must be exclusively locked. 3364 * The vm_map's token must be held. 3365 * 3366 * Because the maps are locked no faults can be in progress during the 3367 * operation. 3368 */ 3369 static void 3370 vm_map_copy_entry(vm_map_t src_map, vm_map_t dst_map, 3371 vm_map_entry_t src_entry, vm_map_entry_t dst_entry) 3372 { 3373 vm_object_t src_object; 3374 3375 if (dst_entry->maptype == VM_MAPTYPE_SUBMAP || 3376 dst_entry->maptype == VM_MAPTYPE_UKSMAP) 3377 return; 3378 if (src_entry->maptype == VM_MAPTYPE_SUBMAP || 3379 src_entry->maptype == VM_MAPTYPE_UKSMAP) 3380 return; 3381 3382 if (src_entry->wired_count == 0) { 3383 /* 3384 * If the source entry is marked needs_copy, it is already 3385 * write-protected. 3386 */ 3387 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 3388 pmap_protect(src_map->pmap, 3389 src_entry->start, 3390 src_entry->end, 3391 src_entry->protection & ~VM_PROT_WRITE); 3392 } 3393 3394 /* 3395 * Make a copy of the object. 3396 * 3397 * The object must be locked prior to checking the object type 3398 * and for the call to vm_object_collapse() and vm_map_split(). 3399 * We cannot use *_hold() here because the split code will 3400 * probably try to destroy the object. The lock is a pool 3401 * token and doesn't care. 3402 * 3403 * We must bump src_map->timestamp when setting 3404 * MAP_ENTRY_NEEDS_COPY to force any concurrent fault 3405 * to retry, otherwise the concurrent fault might improperly 3406 * install a RW pte when its supposed to be a RO(COW) pte. 3407 * This race can occur because a vnode-backed fault may have 3408 * to temporarily release the map lock. 3409 */ 3410 if (src_entry->object.vm_object != NULL) { 3411 vm_map_split(src_entry); 3412 src_object = src_entry->object.vm_object; 3413 dst_entry->object.vm_object = src_object; 3414 src_entry->eflags |= (MAP_ENTRY_COW | 3415 MAP_ENTRY_NEEDS_COPY); 3416 dst_entry->eflags |= (MAP_ENTRY_COW | 3417 MAP_ENTRY_NEEDS_COPY); 3418 dst_entry->offset = src_entry->offset; 3419 ++src_map->timestamp; 3420 } else { 3421 dst_entry->object.vm_object = NULL; 3422 dst_entry->offset = 0; 3423 } 3424 3425 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 3426 dst_entry->end - dst_entry->start, src_entry->start); 3427 } else { 3428 /* 3429 * Of course, wired down pages can't be set copy-on-write. 3430 * Cause wired pages to be copied into the new map by 3431 * simulating faults (the new pages are pageable) 3432 */ 3433 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 3434 } 3435 } 3436 3437 /* 3438 * vmspace_fork: 3439 * Create a new process vmspace structure and vm_map 3440 * based on those of an existing process. The new map 3441 * is based on the old map, according to the inheritance 3442 * values on the regions in that map. 3443 * 3444 * The source map must not be locked. 3445 * No requirements. 3446 */ 3447 static void vmspace_fork_normal_entry(vm_map_t old_map, vm_map_t new_map, 3448 vm_map_entry_t old_entry, int *countp); 3449 static void vmspace_fork_uksmap_entry(vm_map_t old_map, vm_map_t new_map, 3450 vm_map_entry_t old_entry, int *countp); 3451 3452 struct vmspace * 3453 vmspace_fork(struct vmspace *vm1) 3454 { 3455 struct vmspace *vm2; 3456 vm_map_t old_map = &vm1->vm_map; 3457 vm_map_t new_map; 3458 vm_map_entry_t old_entry; 3459 int count; 3460 3461 lwkt_gettoken(&vm1->vm_map.token); 3462 vm_map_lock(old_map); 3463 3464 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 3465 lwkt_gettoken(&vm2->vm_map.token); 3466 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 3467 (caddr_t)&vm1->vm_endcopy - (caddr_t)&vm1->vm_startcopy); 3468 new_map = &vm2->vm_map; /* XXX */ 3469 new_map->timestamp = 1; 3470 3471 vm_map_lock(new_map); 3472 3473 count = 0; 3474 old_entry = old_map->header.next; 3475 while (old_entry != &old_map->header) { 3476 ++count; 3477 old_entry = old_entry->next; 3478 } 3479 3480 count = vm_map_entry_reserve(count + MAP_RESERVE_COUNT); 3481 3482 old_entry = old_map->header.next; 3483 while (old_entry != &old_map->header) { 3484 switch(old_entry->maptype) { 3485 case VM_MAPTYPE_SUBMAP: 3486 panic("vm_map_fork: encountered a submap"); 3487 break; 3488 case VM_MAPTYPE_UKSMAP: 3489 vmspace_fork_uksmap_entry(old_map, new_map, 3490 old_entry, &count); 3491 break; 3492 case VM_MAPTYPE_NORMAL: 3493 case VM_MAPTYPE_VPAGETABLE: 3494 vmspace_fork_normal_entry(old_map, new_map, 3495 old_entry, &count); 3496 break; 3497 } 3498 old_entry = old_entry->next; 3499 } 3500 3501 new_map->size = old_map->size; 3502 vm_map_unlock(old_map); 3503 vm_map_unlock(new_map); 3504 vm_map_entry_release(count); 3505 3506 lwkt_reltoken(&vm2->vm_map.token); 3507 lwkt_reltoken(&vm1->vm_map.token); 3508 3509 return (vm2); 3510 } 3511 3512 static 3513 void 3514 vmspace_fork_normal_entry(vm_map_t old_map, vm_map_t new_map, 3515 vm_map_entry_t old_entry, int *countp) 3516 { 3517 vm_map_entry_t new_entry; 3518 vm_object_t object; 3519 3520 switch (old_entry->inheritance) { 3521 case VM_INHERIT_NONE: 3522 break; 3523 case VM_INHERIT_SHARE: 3524 /* 3525 * Clone the entry, creating the shared object if 3526 * necessary. 3527 */ 3528 if (old_entry->object.vm_object == NULL) 3529 vm_map_entry_allocate_object(old_entry); 3530 3531 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3532 /* 3533 * Shadow a map_entry which needs a copy, 3534 * replacing its object with a new object 3535 * that points to the old one. Ask the 3536 * shadow code to automatically add an 3537 * additional ref. We can't do it afterwords 3538 * because we might race a collapse. The call 3539 * to vm_map_entry_shadow() will also clear 3540 * OBJ_ONEMAPPING. 3541 */ 3542 vm_map_entry_shadow(old_entry, 1); 3543 } else if (old_entry->object.vm_object) { 3544 /* 3545 * We will make a shared copy of the object, 3546 * and must clear OBJ_ONEMAPPING. 3547 * 3548 * Optimize vnode objects. OBJ_ONEMAPPING 3549 * is non-applicable but clear it anyway, 3550 * and its terminal so we don'th ave to deal 3551 * with chains. Reduces SMP conflicts. 3552 * 3553 * XXX assert that object.vm_object != NULL 3554 * since we allocate it above. 3555 */ 3556 object = old_entry->object.vm_object; 3557 if (object->type == OBJT_VNODE) { 3558 vm_object_reference_quick(object); 3559 vm_object_clear_flag(object, 3560 OBJ_ONEMAPPING); 3561 } else { 3562 vm_object_hold(object); 3563 vm_object_chain_wait(object, 0); 3564 vm_object_reference_locked(object); 3565 vm_object_clear_flag(object, 3566 OBJ_ONEMAPPING); 3567 vm_object_drop(object); 3568 } 3569 } 3570 3571 /* 3572 * Clone the entry. We've already bumped the ref on 3573 * any vm_object. 3574 */ 3575 new_entry = vm_map_entry_create(new_map, countp); 3576 *new_entry = *old_entry; 3577 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 3578 new_entry->wired_count = 0; 3579 3580 /* 3581 * Insert the entry into the new map -- we know we're 3582 * inserting at the end of the new map. 3583 */ 3584 3585 vm_map_entry_link(new_map, new_map->header.prev, 3586 new_entry); 3587 3588 /* 3589 * Update the physical map 3590 */ 3591 pmap_copy(new_map->pmap, old_map->pmap, 3592 new_entry->start, 3593 (old_entry->end - old_entry->start), 3594 old_entry->start); 3595 break; 3596 case VM_INHERIT_COPY: 3597 /* 3598 * Clone the entry and link into the map. 3599 */ 3600 new_entry = vm_map_entry_create(new_map, countp); 3601 *new_entry = *old_entry; 3602 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 3603 new_entry->wired_count = 0; 3604 new_entry->object.vm_object = NULL; 3605 vm_map_entry_link(new_map, new_map->header.prev, 3606 new_entry); 3607 vm_map_copy_entry(old_map, new_map, old_entry, 3608 new_entry); 3609 break; 3610 } 3611 } 3612 3613 /* 3614 * When forking user-kernel shared maps, the map might change in the 3615 * child so do not try to copy the underlying pmap entries. 3616 */ 3617 static 3618 void 3619 vmspace_fork_uksmap_entry(vm_map_t old_map, vm_map_t new_map, 3620 vm_map_entry_t old_entry, int *countp) 3621 { 3622 vm_map_entry_t new_entry; 3623 3624 new_entry = vm_map_entry_create(new_map, countp); 3625 *new_entry = *old_entry; 3626 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 3627 new_entry->wired_count = 0; 3628 vm_map_entry_link(new_map, new_map->header.prev, 3629 new_entry); 3630 } 3631 3632 /* 3633 * Create an auto-grow stack entry 3634 * 3635 * No requirements. 3636 */ 3637 int 3638 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 3639 int flags, vm_prot_t prot, vm_prot_t max, int cow) 3640 { 3641 vm_map_entry_t prev_entry; 3642 vm_map_entry_t new_stack_entry; 3643 vm_size_t init_ssize; 3644 int rv; 3645 int count; 3646 vm_offset_t tmpaddr; 3647 3648 cow |= MAP_IS_STACK; 3649 3650 if (max_ssize < sgrowsiz) 3651 init_ssize = max_ssize; 3652 else 3653 init_ssize = sgrowsiz; 3654 3655 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 3656 vm_map_lock(map); 3657 3658 /* 3659 * Find space for the mapping 3660 */ 3661 if ((flags & (MAP_FIXED | MAP_TRYFIXED)) == 0) { 3662 if (vm_map_findspace(map, addrbos, max_ssize, 1, 3663 flags, &tmpaddr)) { 3664 vm_map_unlock(map); 3665 vm_map_entry_release(count); 3666 return (KERN_NO_SPACE); 3667 } 3668 addrbos = tmpaddr; 3669 } 3670 3671 /* If addr is already mapped, no go */ 3672 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 3673 vm_map_unlock(map); 3674 vm_map_entry_release(count); 3675 return (KERN_NO_SPACE); 3676 } 3677 3678 #if 0 3679 /* XXX already handled by kern_mmap() */ 3680 /* If we would blow our VMEM resource limit, no go */ 3681 if (map->size + init_ssize > 3682 curproc->p_rlimit[RLIMIT_VMEM].rlim_cur) { 3683 vm_map_unlock(map); 3684 vm_map_entry_release(count); 3685 return (KERN_NO_SPACE); 3686 } 3687 #endif 3688 3689 /* 3690 * If we can't accomodate max_ssize in the current mapping, 3691 * no go. However, we need to be aware that subsequent user 3692 * mappings might map into the space we have reserved for 3693 * stack, and currently this space is not protected. 3694 * 3695 * Hopefully we will at least detect this condition 3696 * when we try to grow the stack. 3697 */ 3698 if ((prev_entry->next != &map->header) && 3699 (prev_entry->next->start < addrbos + max_ssize)) { 3700 vm_map_unlock(map); 3701 vm_map_entry_release(count); 3702 return (KERN_NO_SPACE); 3703 } 3704 3705 /* 3706 * We initially map a stack of only init_ssize. We will 3707 * grow as needed later. Since this is to be a grow 3708 * down stack, we map at the top of the range. 3709 * 3710 * Note: we would normally expect prot and max to be 3711 * VM_PROT_ALL, and cow to be 0. Possibly we should 3712 * eliminate these as input parameters, and just 3713 * pass these values here in the insert call. 3714 */ 3715 rv = vm_map_insert(map, &count, NULL, NULL, 3716 0, addrbos + max_ssize - init_ssize, 3717 addrbos + max_ssize, 3718 VM_MAPTYPE_NORMAL, 3719 VM_SUBSYS_STACK, prot, max, cow); 3720 3721 /* Now set the avail_ssize amount */ 3722 if (rv == KERN_SUCCESS) { 3723 if (prev_entry != &map->header) 3724 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize, &count); 3725 new_stack_entry = prev_entry->next; 3726 if (new_stack_entry->end != addrbos + max_ssize || 3727 new_stack_entry->start != addrbos + max_ssize - init_ssize) 3728 panic ("Bad entry start/end for new stack entry"); 3729 else 3730 new_stack_entry->aux.avail_ssize = max_ssize - init_ssize; 3731 } 3732 3733 vm_map_unlock(map); 3734 vm_map_entry_release(count); 3735 return (rv); 3736 } 3737 3738 /* 3739 * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 3740 * desired address is already mapped, or if we successfully grow 3741 * the stack. Also returns KERN_SUCCESS if addr is outside the 3742 * stack range (this is strange, but preserves compatibility with 3743 * the grow function in vm_machdep.c). 3744 * 3745 * No requirements. 3746 */ 3747 int 3748 vm_map_growstack (vm_map_t map, vm_offset_t addr) 3749 { 3750 vm_map_entry_t prev_entry; 3751 vm_map_entry_t stack_entry; 3752 vm_map_entry_t new_stack_entry; 3753 struct vmspace *vm; 3754 struct lwp *lp; 3755 struct proc *p; 3756 vm_offset_t end; 3757 int grow_amount; 3758 int rv = KERN_SUCCESS; 3759 int is_procstack; 3760 int use_read_lock = 1; 3761 int count; 3762 3763 /* 3764 * Find the vm 3765 */ 3766 lp = curthread->td_lwp; 3767 p = curthread->td_proc; 3768 KKASSERT(lp != NULL); 3769 vm = lp->lwp_vmspace; 3770 3771 /* 3772 * Growstack is only allowed on the current process. We disallow 3773 * other use cases, e.g. trying to access memory via procfs that 3774 * the stack hasn't grown into. 3775 */ 3776 if (map != &vm->vm_map) { 3777 return KERN_FAILURE; 3778 } 3779 3780 count = vm_map_entry_reserve(MAP_RESERVE_COUNT); 3781 Retry: 3782 if (use_read_lock) 3783 vm_map_lock_read(map); 3784 else 3785 vm_map_lock(map); 3786 3787 /* If addr is already in the entry range, no need to grow.*/ 3788 if (vm_map_lookup_entry(map, addr, &prev_entry)) 3789 goto done; 3790 3791 if ((stack_entry = prev_entry->next) == &map->header) 3792 goto done; 3793 if (prev_entry == &map->header) 3794 end = stack_entry->start - stack_entry->aux.avail_ssize; 3795 else 3796 end = prev_entry->end; 3797 3798 /* 3799 * This next test mimics the old grow function in vm_machdep.c. 3800 * It really doesn't quite make sense, but we do it anyway 3801 * for compatibility. 3802 * 3803 * If not growable stack, return success. This signals the 3804 * caller to proceed as he would normally with normal vm. 3805 */ 3806 if (stack_entry->aux.avail_ssize < 1 || 3807 addr >= stack_entry->start || 3808 addr < stack_entry->start - stack_entry->aux.avail_ssize) { 3809 goto done; 3810 } 3811 3812 /* Find the minimum grow amount */ 3813 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE); 3814 if (grow_amount > stack_entry->aux.avail_ssize) { 3815 rv = KERN_NO_SPACE; 3816 goto done; 3817 } 3818 3819 /* 3820 * If there is no longer enough space between the entries 3821 * nogo, and adjust the available space. Note: this 3822 * should only happen if the user has mapped into the 3823 * stack area after the stack was created, and is 3824 * probably an error. 3825 * 3826 * This also effectively destroys any guard page the user 3827 * might have intended by limiting the stack size. 3828 */ 3829 if (grow_amount > stack_entry->start - end) { 3830 if (use_read_lock && vm_map_lock_upgrade(map)) { 3831 /* lost lock */ 3832 use_read_lock = 0; 3833 goto Retry; 3834 } 3835 use_read_lock = 0; 3836 stack_entry->aux.avail_ssize = stack_entry->start - end; 3837 rv = KERN_NO_SPACE; 3838 goto done; 3839 } 3840 3841 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr; 3842 3843 /* If this is the main process stack, see if we're over the 3844 * stack limit. 3845 */ 3846 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 3847 p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 3848 rv = KERN_NO_SPACE; 3849 goto done; 3850 } 3851 3852 /* Round up the grow amount modulo SGROWSIZ */ 3853 grow_amount = roundup (grow_amount, sgrowsiz); 3854 if (grow_amount > stack_entry->aux.avail_ssize) { 3855 grow_amount = stack_entry->aux.avail_ssize; 3856 } 3857 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 3858 p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 3859 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur - 3860 ctob(vm->vm_ssize); 3861 } 3862 3863 /* If we would blow our VMEM resource limit, no go */ 3864 if (map->size + grow_amount > p->p_rlimit[RLIMIT_VMEM].rlim_cur) { 3865 rv = KERN_NO_SPACE; 3866 goto done; 3867 } 3868 3869 if (use_read_lock && vm_map_lock_upgrade(map)) { 3870 /* lost lock */ 3871 use_read_lock = 0; 3872 goto Retry; 3873 } 3874 use_read_lock = 0; 3875 3876 /* Get the preliminary new entry start value */ 3877 addr = stack_entry->start - grow_amount; 3878 3879 /* If this puts us into the previous entry, cut back our growth 3880 * to the available space. Also, see the note above. 3881 */ 3882 if (addr < end) { 3883 stack_entry->aux.avail_ssize = stack_entry->start - end; 3884 addr = end; 3885 } 3886 3887 rv = vm_map_insert(map, &count, NULL, NULL, 3888 0, addr, stack_entry->start, 3889 VM_MAPTYPE_NORMAL, 3890 VM_SUBSYS_STACK, VM_PROT_ALL, VM_PROT_ALL, 0); 3891 3892 /* Adjust the available stack space by the amount we grew. */ 3893 if (rv == KERN_SUCCESS) { 3894 if (prev_entry != &map->header) 3895 vm_map_clip_end(map, prev_entry, addr, &count); 3896 new_stack_entry = prev_entry->next; 3897 if (new_stack_entry->end != stack_entry->start || 3898 new_stack_entry->start != addr) 3899 panic ("Bad stack grow start/end in new stack entry"); 3900 else { 3901 new_stack_entry->aux.avail_ssize = 3902 stack_entry->aux.avail_ssize - 3903 (new_stack_entry->end - new_stack_entry->start); 3904 if (is_procstack) 3905 vm->vm_ssize += btoc(new_stack_entry->end - 3906 new_stack_entry->start); 3907 } 3908 3909 if (map->flags & MAP_WIREFUTURE) 3910 vm_map_unwire(map, new_stack_entry->start, 3911 new_stack_entry->end, FALSE); 3912 } 3913 3914 done: 3915 if (use_read_lock) 3916 vm_map_unlock_read(map); 3917 else 3918 vm_map_unlock(map); 3919 vm_map_entry_release(count); 3920 return (rv); 3921 } 3922 3923 /* 3924 * Unshare the specified VM space for exec. If other processes are 3925 * mapped to it, then create a new one. The new vmspace is null. 3926 * 3927 * No requirements. 3928 */ 3929 void 3930 vmspace_exec(struct proc *p, struct vmspace *vmcopy) 3931 { 3932 struct vmspace *oldvmspace = p->p_vmspace; 3933 struct vmspace *newvmspace; 3934 vm_map_t map = &p->p_vmspace->vm_map; 3935 3936 /* 3937 * If we are execing a resident vmspace we fork it, otherwise 3938 * we create a new vmspace. Note that exitingcnt is not 3939 * copied to the new vmspace. 3940 */ 3941 lwkt_gettoken(&oldvmspace->vm_map.token); 3942 if (vmcopy) { 3943 newvmspace = vmspace_fork(vmcopy); 3944 lwkt_gettoken(&newvmspace->vm_map.token); 3945 } else { 3946 newvmspace = vmspace_alloc(map->min_offset, map->max_offset); 3947 lwkt_gettoken(&newvmspace->vm_map.token); 3948 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy, 3949 (caddr_t)&oldvmspace->vm_endcopy - 3950 (caddr_t)&oldvmspace->vm_startcopy); 3951 } 3952 3953 /* 3954 * Finish initializing the vmspace before assigning it 3955 * to the process. The vmspace will become the current vmspace 3956 * if p == curproc. 3957 */ 3958 pmap_pinit2(vmspace_pmap(newvmspace)); 3959 pmap_replacevm(p, newvmspace, 0); 3960 lwkt_reltoken(&newvmspace->vm_map.token); 3961 lwkt_reltoken(&oldvmspace->vm_map.token); 3962 vmspace_rel(oldvmspace); 3963 } 3964 3965 /* 3966 * Unshare the specified VM space for forcing COW. This 3967 * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 3968 */ 3969 void 3970 vmspace_unshare(struct proc *p) 3971 { 3972 struct vmspace *oldvmspace = p->p_vmspace; 3973 struct vmspace *newvmspace; 3974 3975 lwkt_gettoken(&oldvmspace->vm_map.token); 3976 if (vmspace_getrefs(oldvmspace) == 1) { 3977 lwkt_reltoken(&oldvmspace->vm_map.token); 3978 return; 3979 } 3980 newvmspace = vmspace_fork(oldvmspace); 3981 lwkt_gettoken(&newvmspace->vm_map.token); 3982 pmap_pinit2(vmspace_pmap(newvmspace)); 3983 pmap_replacevm(p, newvmspace, 0); 3984 lwkt_reltoken(&newvmspace->vm_map.token); 3985 lwkt_reltoken(&oldvmspace->vm_map.token); 3986 vmspace_rel(oldvmspace); 3987 } 3988 3989 /* 3990 * vm_map_hint: return the beginning of the best area suitable for 3991 * creating a new mapping with "prot" protection. 3992 * 3993 * No requirements. 3994 */ 3995 vm_offset_t 3996 vm_map_hint(struct proc *p, vm_offset_t addr, vm_prot_t prot) 3997 { 3998 struct vmspace *vms = p->p_vmspace; 3999 4000 if (!randomize_mmap || addr != 0) { 4001 /* 4002 * Set a reasonable start point for the hint if it was 4003 * not specified or if it falls within the heap space. 4004 * Hinted mmap()s do not allocate out of the heap space. 4005 */ 4006 if (addr == 0 || 4007 (addr >= round_page((vm_offset_t)vms->vm_taddr) && 4008 addr < round_page((vm_offset_t)vms->vm_daddr + maxdsiz))) { 4009 addr = round_page((vm_offset_t)vms->vm_daddr + maxdsiz); 4010 } 4011 4012 return addr; 4013 } 4014 addr = (vm_offset_t)vms->vm_daddr + MAXDSIZ; 4015 addr += karc4random() & (MIN((256 * 1024 * 1024), MAXDSIZ) - 1); 4016 4017 return (round_page(addr)); 4018 } 4019 4020 /* 4021 * Finds the VM object, offset, and protection for a given virtual address 4022 * in the specified map, assuming a page fault of the type specified. 4023 * 4024 * Leaves the map in question locked for read; return values are guaranteed 4025 * until a vm_map_lookup_done call is performed. Note that the map argument 4026 * is in/out; the returned map must be used in the call to vm_map_lookup_done. 4027 * 4028 * A handle (out_entry) is returned for use in vm_map_lookup_done, to make 4029 * that fast. 4030 * 4031 * If a lookup is requested with "write protection" specified, the map may 4032 * be changed to perform virtual copying operations, although the data 4033 * referenced will remain the same. 4034 * 4035 * No requirements. 4036 */ 4037 int 4038 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 4039 vm_offset_t vaddr, 4040 vm_prot_t fault_typea, 4041 vm_map_entry_t *out_entry, /* OUT */ 4042 vm_object_t *object, /* OUT */ 4043 vm_pindex_t *pindex, /* OUT */ 4044 vm_prot_t *out_prot, /* OUT */ 4045 boolean_t *wired) /* OUT */ 4046 { 4047 vm_map_entry_t entry; 4048 vm_map_t map = *var_map; 4049 vm_prot_t prot; 4050 vm_prot_t fault_type = fault_typea; 4051 int use_read_lock = 1; 4052 int rv = KERN_SUCCESS; 4053 4054 RetryLookup: 4055 if (use_read_lock) 4056 vm_map_lock_read(map); 4057 else 4058 vm_map_lock(map); 4059 4060 /* 4061 * If the map has an interesting hint, try it before calling full 4062 * blown lookup routine. 4063 */ 4064 entry = map->hint; 4065 cpu_ccfence(); 4066 *out_entry = entry; 4067 *object = NULL; 4068 4069 if ((entry == &map->header) || 4070 (vaddr < entry->start) || (vaddr >= entry->end)) { 4071 vm_map_entry_t tmp_entry; 4072 4073 /* 4074 * Entry was either not a valid hint, or the vaddr was not 4075 * contained in the entry, so do a full lookup. 4076 */ 4077 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) { 4078 rv = KERN_INVALID_ADDRESS; 4079 goto done; 4080 } 4081 4082 entry = tmp_entry; 4083 *out_entry = entry; 4084 } 4085 4086 /* 4087 * Handle submaps. 4088 */ 4089 if (entry->maptype == VM_MAPTYPE_SUBMAP) { 4090 vm_map_t old_map = map; 4091 4092 *var_map = map = entry->object.sub_map; 4093 if (use_read_lock) 4094 vm_map_unlock_read(old_map); 4095 else 4096 vm_map_unlock(old_map); 4097 use_read_lock = 1; 4098 goto RetryLookup; 4099 } 4100 4101 /* 4102 * Check whether this task is allowed to have this page. 4103 * Note the special case for MAP_ENTRY_COW pages with an override. 4104 * This is to implement a forced COW for debuggers. 4105 */ 4106 if (fault_type & VM_PROT_OVERRIDE_WRITE) 4107 prot = entry->max_protection; 4108 else 4109 prot = entry->protection; 4110 4111 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 4112 if ((fault_type & prot) != fault_type) { 4113 rv = KERN_PROTECTION_FAILURE; 4114 goto done; 4115 } 4116 4117 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 4118 (entry->eflags & MAP_ENTRY_COW) && 4119 (fault_type & VM_PROT_WRITE) && 4120 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 4121 rv = KERN_PROTECTION_FAILURE; 4122 goto done; 4123 } 4124 4125 /* 4126 * If this page is not pageable, we have to get it for all possible 4127 * accesses. 4128 */ 4129 *wired = (entry->wired_count != 0); 4130 if (*wired) 4131 prot = fault_type = entry->protection; 4132 4133 /* 4134 * Virtual page tables may need to update the accessed (A) bit 4135 * in a page table entry. Upgrade the fault to a write fault for 4136 * that case if the map will support it. If the map does not support 4137 * it the page table entry simply will not be updated. 4138 */ 4139 if (entry->maptype == VM_MAPTYPE_VPAGETABLE) { 4140 if (prot & VM_PROT_WRITE) 4141 fault_type |= VM_PROT_WRITE; 4142 } 4143 4144 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace && 4145 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) { 4146 if ((prot & VM_PROT_WRITE) == 0) 4147 fault_type |= VM_PROT_WRITE; 4148 } 4149 4150 /* 4151 * Only NORMAL and VPAGETABLE maps are object-based. UKSMAPs are not. 4152 */ 4153 if (entry->maptype != VM_MAPTYPE_NORMAL && 4154 entry->maptype != VM_MAPTYPE_VPAGETABLE) { 4155 *object = NULL; 4156 goto skip; 4157 } 4158 4159 /* 4160 * If the entry was copy-on-write, we either ... 4161 */ 4162 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 4163 /* 4164 * If we want to write the page, we may as well handle that 4165 * now since we've got the map locked. 4166 * 4167 * If we don't need to write the page, we just demote the 4168 * permissions allowed. 4169 */ 4170 4171 if (fault_type & VM_PROT_WRITE) { 4172 /* 4173 * Not allowed if TDF_NOFAULT is set as the shadowing 4174 * operation can deadlock against the faulting 4175 * function due to the copy-on-write. 4176 */ 4177 if (curthread->td_flags & TDF_NOFAULT) { 4178 rv = KERN_FAILURE_NOFAULT; 4179 goto done; 4180 } 4181 4182 /* 4183 * Make a new object, and place it in the object 4184 * chain. Note that no new references have appeared 4185 * -- one just moved from the map to the new 4186 * object. 4187 */ 4188 4189 if (use_read_lock && vm_map_lock_upgrade(map)) { 4190 /* lost lock */ 4191 use_read_lock = 0; 4192 goto RetryLookup; 4193 } 4194 use_read_lock = 0; 4195 4196 vm_map_entry_shadow(entry, 0); 4197 } else { 4198 /* 4199 * We're attempting to read a copy-on-write page -- 4200 * don't allow writes. 4201 */ 4202 4203 prot &= ~VM_PROT_WRITE; 4204 } 4205 } 4206 4207 /* 4208 * Create an object if necessary. 4209 */ 4210 if (entry->object.vm_object == NULL && !map->system_map) { 4211 if (use_read_lock && vm_map_lock_upgrade(map)) { 4212 /* lost lock */ 4213 use_read_lock = 0; 4214 goto RetryLookup; 4215 } 4216 use_read_lock = 0; 4217 vm_map_entry_allocate_object(entry); 4218 } 4219 4220 /* 4221 * Return the object/offset from this entry. If the entry was 4222 * copy-on-write or empty, it has been fixed up. 4223 */ 4224 *object = entry->object.vm_object; 4225 4226 skip: 4227 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 4228 4229 /* 4230 * Return whether this is the only map sharing this data. On 4231 * success we return with a read lock held on the map. On failure 4232 * we return with the map unlocked. 4233 */ 4234 *out_prot = prot; 4235 done: 4236 if (rv == KERN_SUCCESS) { 4237 if (use_read_lock == 0) 4238 vm_map_lock_downgrade(map); 4239 } else if (use_read_lock) { 4240 vm_map_unlock_read(map); 4241 } else { 4242 vm_map_unlock(map); 4243 } 4244 return (rv); 4245 } 4246 4247 /* 4248 * Releases locks acquired by a vm_map_lookup() 4249 * (according to the handle returned by that lookup). 4250 * 4251 * No other requirements. 4252 */ 4253 void 4254 vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry, int count) 4255 { 4256 /* 4257 * Unlock the main-level map 4258 */ 4259 vm_map_unlock_read(map); 4260 if (count) 4261 vm_map_entry_release(count); 4262 } 4263 4264 /* 4265 * Quick hack, needs some help to make it more SMP friendly. 4266 */ 4267 void 4268 vm_map_interlock(vm_map_t map, struct vm_map_ilock *ilock, 4269 vm_offset_t ran_beg, vm_offset_t ran_end) 4270 { 4271 struct vm_map_ilock *scan; 4272 4273 ilock->ran_beg = ran_beg; 4274 ilock->ran_end = ran_end; 4275 ilock->flags = 0; 4276 4277 spin_lock(&map->ilock_spin); 4278 restart: 4279 for (scan = map->ilock_base; scan; scan = scan->next) { 4280 if (ran_end > scan->ran_beg && ran_beg < scan->ran_end) { 4281 scan->flags |= ILOCK_WAITING; 4282 ssleep(scan, &map->ilock_spin, 0, "ilock", 0); 4283 goto restart; 4284 } 4285 } 4286 ilock->next = map->ilock_base; 4287 map->ilock_base = ilock; 4288 spin_unlock(&map->ilock_spin); 4289 } 4290 4291 void 4292 vm_map_deinterlock(vm_map_t map, struct vm_map_ilock *ilock) 4293 { 4294 struct vm_map_ilock *scan; 4295 struct vm_map_ilock **scanp; 4296 4297 spin_lock(&map->ilock_spin); 4298 scanp = &map->ilock_base; 4299 while ((scan = *scanp) != NULL) { 4300 if (scan == ilock) { 4301 *scanp = ilock->next; 4302 spin_unlock(&map->ilock_spin); 4303 if (ilock->flags & ILOCK_WAITING) 4304 wakeup(ilock); 4305 return; 4306 } 4307 scanp = &scan->next; 4308 } 4309 spin_unlock(&map->ilock_spin); 4310 panic("vm_map_deinterlock: missing ilock!"); 4311 } 4312 4313 #include "opt_ddb.h" 4314 #ifdef DDB 4315 #include <sys/kernel.h> 4316 4317 #include <ddb/ddb.h> 4318 4319 /* 4320 * Debugging only 4321 */ 4322 DB_SHOW_COMMAND(map, vm_map_print) 4323 { 4324 static int nlines; 4325 /* XXX convert args. */ 4326 vm_map_t map = (vm_map_t)addr; 4327 boolean_t full = have_addr; 4328 4329 vm_map_entry_t entry; 4330 4331 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 4332 (void *)map, 4333 (void *)map->pmap, map->nentries, map->timestamp); 4334 nlines++; 4335 4336 if (!full && db_indent) 4337 return; 4338 4339 db_indent += 2; 4340 for (entry = map->header.next; entry != &map->header; 4341 entry = entry->next) { 4342 db_iprintf("map entry %p: start=%p, end=%p\n", 4343 (void *)entry, (void *)entry->start, (void *)entry->end); 4344 nlines++; 4345 { 4346 static char *inheritance_name[4] = 4347 {"share", "copy", "none", "donate_copy"}; 4348 4349 db_iprintf(" prot=%x/%x/%s", 4350 entry->protection, 4351 entry->max_protection, 4352 inheritance_name[(int)(unsigned char) 4353 entry->inheritance]); 4354 if (entry->wired_count != 0) 4355 db_printf(", wired"); 4356 } 4357 switch(entry->maptype) { 4358 case VM_MAPTYPE_SUBMAP: 4359 /* XXX no %qd in kernel. Truncate entry->offset. */ 4360 db_printf(", share=%p, offset=0x%lx\n", 4361 (void *)entry->object.sub_map, 4362 (long)entry->offset); 4363 nlines++; 4364 if ((entry->prev == &map->header) || 4365 (entry->prev->object.sub_map != 4366 entry->object.sub_map)) { 4367 db_indent += 2; 4368 vm_map_print((db_expr_t)(intptr_t) 4369 entry->object.sub_map, 4370 full, 0, NULL); 4371 db_indent -= 2; 4372 } 4373 break; 4374 case VM_MAPTYPE_NORMAL: 4375 case VM_MAPTYPE_VPAGETABLE: 4376 /* XXX no %qd in kernel. Truncate entry->offset. */ 4377 db_printf(", object=%p, offset=0x%lx", 4378 (void *)entry->object.vm_object, 4379 (long)entry->offset); 4380 if (entry->eflags & MAP_ENTRY_COW) 4381 db_printf(", copy (%s)", 4382 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 4383 db_printf("\n"); 4384 nlines++; 4385 4386 if ((entry->prev == &map->header) || 4387 (entry->prev->object.vm_object != 4388 entry->object.vm_object)) { 4389 db_indent += 2; 4390 vm_object_print((db_expr_t)(intptr_t) 4391 entry->object.vm_object, 4392 full, 0, NULL); 4393 nlines += 4; 4394 db_indent -= 2; 4395 } 4396 break; 4397 case VM_MAPTYPE_UKSMAP: 4398 db_printf(", uksmap=%p, offset=0x%lx", 4399 (void *)entry->object.uksmap, 4400 (long)entry->offset); 4401 if (entry->eflags & MAP_ENTRY_COW) 4402 db_printf(", copy (%s)", 4403 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 4404 db_printf("\n"); 4405 nlines++; 4406 break; 4407 default: 4408 break; 4409 } 4410 } 4411 db_indent -= 2; 4412 if (db_indent == 0) 4413 nlines = 0; 4414 } 4415 4416 /* 4417 * Debugging only 4418 */ 4419 DB_SHOW_COMMAND(procvm, procvm) 4420 { 4421 struct proc *p; 4422 4423 if (have_addr) { 4424 p = (struct proc *) addr; 4425 } else { 4426 p = curproc; 4427 } 4428 4429 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 4430 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 4431 (void *)vmspace_pmap(p->p_vmspace)); 4432 4433 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 4434 } 4435 4436 #endif /* DDB */ 4437