1 /* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * The Mach Operating System project at Carnegie-Mellon University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94 37 * 38 * 39 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40 * All rights reserved. 41 * 42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43 * 44 * Permission to use, copy, modify and distribute this software and 45 * its documentation is hereby granted, provided that both the copyright 46 * notice and this permission notice appear in all copies of the 47 * software, derivative works or modified versions, and any portions 48 * thereof, and that both notices appear in supporting documentation. 49 * 50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53 * 54 * Carnegie Mellon requests users of this software to return to 55 * 56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57 * School of Computer Science 58 * Carnegie Mellon University 59 * Pittsburgh PA 15213-3890 60 * 61 * any improvements or extensions that they make and grant Carnegie the 62 * rights to redistribute these changes. 63 * 64 * $FreeBSD$ 65 */ 66 67 /* 68 * Virtual memory mapping module. 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/lock.h> 74 #include <sys/mutex.h> 75 #include <sys/proc.h> 76 #include <sys/vmmeter.h> 77 #include <sys/mman.h> 78 #include <sys/vnode.h> 79 #include <sys/resourcevar.h> 80 81 #include <vm/vm.h> 82 #include <vm/vm_param.h> 83 #include <vm/pmap.h> 84 #include <vm/vm_map.h> 85 #include <vm/vm_page.h> 86 #include <vm/vm_object.h> 87 #include <vm/vm_pager.h> 88 #include <vm/vm_kern.h> 89 #include <vm/vm_extern.h> 90 #include <vm/vm_zone.h> 91 #include <vm/swap_pager.h> 92 93 /* 94 * Virtual memory maps provide for the mapping, protection, 95 * and sharing of virtual memory objects. In addition, 96 * this module provides for an efficient virtual copy of 97 * memory from one map to another. 98 * 99 * Synchronization is required prior to most operations. 100 * 101 * Maps consist of an ordered doubly-linked list of simple 102 * entries; a single hint is used to speed up lookups. 103 * 104 * Since portions of maps are specified by start/end addresses, 105 * which may not align with existing map entries, all 106 * routines merely "clip" entries to these start/end values. 107 * [That is, an entry is split into two, bordering at a 108 * start or end value.] Note that these clippings may not 109 * always be necessary (as the two resulting entries are then 110 * not changed); however, the clipping is done for convenience. 111 * 112 * As mentioned above, virtual copy operations are performed 113 * by copying VM object references from one map to 114 * another, and then marking both regions as copy-on-write. 115 */ 116 117 /* 118 * vm_map_startup: 119 * 120 * Initialize the vm_map module. Must be called before 121 * any other vm_map routines. 122 * 123 * Map and entry structures are allocated from the general 124 * purpose memory pool with some exceptions: 125 * 126 * - The kernel map and kmem submap are allocated statically. 127 * - Kernel map entries are allocated out of a static pool. 128 * 129 * These restrictions are necessary since malloc() uses the 130 * maps and requires map entries. 131 */ 132 133 static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store; 134 static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone; 135 static struct vm_object kmapentobj, mapentobj, mapobj; 136 137 static struct vm_map_entry map_entry_init[MAX_MAPENT]; 138 static struct vm_map_entry kmap_entry_init[MAX_KMAPENT]; 139 static struct vm_map map_init[MAX_KMAP]; 140 141 static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t)); 142 static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t)); 143 static vm_map_entry_t vm_map_entry_create __P((vm_map_t)); 144 static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t)); 145 static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t)); 146 static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t)); 147 static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t, 148 vm_map_entry_t)); 149 static void vm_map_split __P((vm_map_entry_t)); 150 151 void 152 vm_map_startup() 153 { 154 mapzone = &mapzone_store; 155 zbootinit(mapzone, "MAP", sizeof (struct vm_map), 156 map_init, MAX_KMAP); 157 kmapentzone = &kmapentzone_store; 158 zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry), 159 kmap_entry_init, MAX_KMAPENT); 160 mapentzone = &mapentzone_store; 161 zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry), 162 map_entry_init, MAX_MAPENT); 163 } 164 165 /* 166 * Allocate a vmspace structure, including a vm_map and pmap, 167 * and initialize those structures. The refcnt is set to 1. 168 * The remaining fields must be initialized by the caller. 169 */ 170 struct vmspace * 171 vmspace_alloc(min, max) 172 vm_offset_t min, max; 173 { 174 struct vmspace *vm; 175 176 mtx_assert(&vm_mtx, MA_OWNED); 177 vm = zalloc(vmspace_zone); 178 CTR1(KTR_VM, "vmspace_alloc: %p", vm); 179 vm_map_init(&vm->vm_map, min, max); 180 pmap_pinit(vmspace_pmap(vm)); 181 vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */ 182 vm->vm_refcnt = 1; 183 vm->vm_shm = NULL; 184 return (vm); 185 } 186 187 void 188 vm_init2(void) { 189 zinitna(kmapentzone, &kmapentobj, 190 NULL, 0, cnt.v_page_count / 4, ZONE_INTERRUPT, 1); 191 zinitna(mapentzone, &mapentobj, 192 NULL, 0, 0, 0, 1); 193 zinitna(mapzone, &mapobj, 194 NULL, 0, 0, 0, 1); 195 vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3); 196 pmap_init2(); 197 vm_object_init2(); 198 } 199 200 void 201 vmspace_free(vm) 202 struct vmspace *vm; 203 { 204 205 mtx_assert(&vm_mtx, MA_OWNED); 206 if (vm->vm_refcnt == 0) 207 panic("vmspace_free: attempt to free already freed vmspace"); 208 209 if (--vm->vm_refcnt == 0) { 210 211 CTR1(KTR_VM, "vmspace_free: %p", vm); 212 /* 213 * Lock the map, to wait out all other references to it. 214 * Delete all of the mappings and pages they hold, then call 215 * the pmap module to reclaim anything left. 216 */ 217 vm_map_lock(&vm->vm_map); 218 (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset, 219 vm->vm_map.max_offset); 220 vm_map_unlock(&vm->vm_map); 221 222 pmap_release(vmspace_pmap(vm)); 223 vm_map_destroy(&vm->vm_map); 224 zfree(vmspace_zone, vm); 225 } 226 } 227 228 /* 229 * vm_map_create: 230 * 231 * Creates and returns a new empty VM map with 232 * the given physical map structure, and having 233 * the given lower and upper address bounds. 234 */ 235 vm_map_t 236 vm_map_create(pmap, min, max) 237 pmap_t pmap; 238 vm_offset_t min, max; 239 { 240 vm_map_t result; 241 242 mtx_assert(&vm_mtx, MA_OWNED); 243 result = zalloc(mapzone); 244 CTR1(KTR_VM, "vm_map_create: %p", result); 245 vm_map_init(result, min, max); 246 result->pmap = pmap; 247 return (result); 248 } 249 250 /* 251 * Initialize an existing vm_map structure 252 * such as that in the vmspace structure. 253 * The pmap is set elsewhere. 254 */ 255 void 256 vm_map_init(map, min, max) 257 struct vm_map *map; 258 vm_offset_t min, max; 259 { 260 261 mtx_assert(&vm_mtx, MA_OWNED); 262 map->header.next = map->header.prev = &map->header; 263 map->nentries = 0; 264 map->size = 0; 265 map->system_map = 0; 266 map->infork = 0; 267 map->min_offset = min; 268 map->max_offset = max; 269 map->first_free = &map->header; 270 map->hint = &map->header; 271 map->timestamp = 0; 272 lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE); 273 } 274 275 void 276 vm_map_destroy(map) 277 struct vm_map *map; 278 { 279 280 mtx_assert(&vm_mtx, MA_OWNED); 281 lockdestroy(&map->lock); 282 } 283 284 /* 285 * vm_map_entry_dispose: [ internal use only ] 286 * 287 * Inverse of vm_map_entry_create. 288 */ 289 static void 290 vm_map_entry_dispose(map, entry) 291 vm_map_t map; 292 vm_map_entry_t entry; 293 { 294 zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry); 295 } 296 297 /* 298 * vm_map_entry_create: [ internal use only ] 299 * 300 * Allocates a VM map entry for insertion. 301 * No entry fields are filled in. 302 */ 303 static vm_map_entry_t 304 vm_map_entry_create(map) 305 vm_map_t map; 306 { 307 vm_map_entry_t new_entry; 308 309 new_entry = zalloc((map->system_map || !mapentzone) ? 310 kmapentzone : mapentzone); 311 if (new_entry == NULL) 312 panic("vm_map_entry_create: kernel resources exhausted"); 313 return(new_entry); 314 } 315 316 /* 317 * vm_map_entry_{un,}link: 318 * 319 * Insert/remove entries from maps. 320 */ 321 static __inline void 322 vm_map_entry_link(vm_map_t map, 323 vm_map_entry_t after_where, 324 vm_map_entry_t entry) 325 { 326 327 CTR4(KTR_VM, 328 "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map, 329 map->nentries, entry, after_where); 330 map->nentries++; 331 entry->prev = after_where; 332 entry->next = after_where->next; 333 entry->next->prev = entry; 334 after_where->next = entry; 335 } 336 337 static __inline void 338 vm_map_entry_unlink(vm_map_t map, 339 vm_map_entry_t entry) 340 { 341 vm_map_entry_t prev = entry->prev; 342 vm_map_entry_t next = entry->next; 343 344 next->prev = prev; 345 prev->next = next; 346 map->nentries--; 347 CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map, 348 map->nentries, entry); 349 } 350 351 /* 352 * SAVE_HINT: 353 * 354 * Saves the specified entry as the hint for 355 * future lookups. 356 */ 357 #define SAVE_HINT(map,value) \ 358 (map)->hint = (value); 359 360 /* 361 * vm_map_lookup_entry: [ internal use only ] 362 * 363 * Finds the map entry containing (or 364 * immediately preceding) the specified address 365 * in the given map; the entry is returned 366 * in the "entry" parameter. The boolean 367 * result indicates whether the address is 368 * actually contained in the map. 369 * 370 * Doesn't block. 371 */ 372 boolean_t 373 vm_map_lookup_entry(map, address, entry) 374 vm_map_t map; 375 vm_offset_t address; 376 vm_map_entry_t *entry; /* OUT */ 377 { 378 vm_map_entry_t cur; 379 vm_map_entry_t last; 380 381 mtx_assert(&vm_mtx, MA_OWNED); 382 /* 383 * Start looking either from the head of the list, or from the hint. 384 */ 385 386 cur = map->hint; 387 388 if (cur == &map->header) 389 cur = cur->next; 390 391 if (address >= cur->start) { 392 /* 393 * Go from hint to end of list. 394 * 395 * But first, make a quick check to see if we are already looking 396 * at the entry we want (which is usually the case). Note also 397 * that we don't need to save the hint here... it is the same 398 * hint (unless we are at the header, in which case the hint 399 * didn't buy us anything anyway). 400 */ 401 last = &map->header; 402 if ((cur != last) && (cur->end > address)) { 403 *entry = cur; 404 return (TRUE); 405 } 406 } else { 407 /* 408 * Go from start to hint, *inclusively* 409 */ 410 last = cur->next; 411 cur = map->header.next; 412 } 413 414 /* 415 * Search linearly 416 */ 417 418 while (cur != last) { 419 if (cur->end > address) { 420 if (address >= cur->start) { 421 /* 422 * Save this lookup for future hints, and 423 * return 424 */ 425 426 *entry = cur; 427 SAVE_HINT(map, cur); 428 return (TRUE); 429 } 430 break; 431 } 432 cur = cur->next; 433 } 434 *entry = cur->prev; 435 SAVE_HINT(map, *entry); 436 return (FALSE); 437 } 438 439 /* 440 * vm_map_insert: 441 * 442 * Inserts the given whole VM object into the target 443 * map at the specified address range. The object's 444 * size should match that of the address range. 445 * 446 * Requires that the map be locked, and leaves it so. 447 * 448 * If object is non-NULL, ref count must be bumped by caller 449 * prior to making call to account for the new entry. 450 */ 451 int 452 vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 453 vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, 454 int cow) 455 { 456 vm_map_entry_t new_entry; 457 vm_map_entry_t prev_entry; 458 vm_map_entry_t temp_entry; 459 vm_eflags_t protoeflags; 460 461 mtx_assert(&vm_mtx, MA_OWNED); 462 /* 463 * Check that the start and end points are not bogus. 464 */ 465 466 if ((start < map->min_offset) || (end > map->max_offset) || 467 (start >= end)) 468 return (KERN_INVALID_ADDRESS); 469 470 /* 471 * Find the entry prior to the proposed starting address; if it's part 472 * of an existing entry, this range is bogus. 473 */ 474 475 if (vm_map_lookup_entry(map, start, &temp_entry)) 476 return (KERN_NO_SPACE); 477 478 prev_entry = temp_entry; 479 480 /* 481 * Assert that the next entry doesn't overlap the end point. 482 */ 483 484 if ((prev_entry->next != &map->header) && 485 (prev_entry->next->start < end)) 486 return (KERN_NO_SPACE); 487 488 protoeflags = 0; 489 490 if (cow & MAP_COPY_ON_WRITE) 491 protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY; 492 493 if (cow & MAP_NOFAULT) { 494 protoeflags |= MAP_ENTRY_NOFAULT; 495 496 KASSERT(object == NULL, 497 ("vm_map_insert: paradoxical MAP_NOFAULT request")); 498 } 499 if (cow & MAP_DISABLE_SYNCER) 500 protoeflags |= MAP_ENTRY_NOSYNC; 501 if (cow & MAP_DISABLE_COREDUMP) 502 protoeflags |= MAP_ENTRY_NOCOREDUMP; 503 504 if (object) { 505 /* 506 * When object is non-NULL, it could be shared with another 507 * process. We have to set or clear OBJ_ONEMAPPING 508 * appropriately. 509 */ 510 if ((object->ref_count > 1) || (object->shadow_count != 0)) { 511 vm_object_clear_flag(object, OBJ_ONEMAPPING); 512 } 513 } 514 else if ((prev_entry != &map->header) && 515 (prev_entry->eflags == protoeflags) && 516 (prev_entry->end == start) && 517 (prev_entry->wired_count == 0) && 518 ((prev_entry->object.vm_object == NULL) || 519 vm_object_coalesce(prev_entry->object.vm_object, 520 OFF_TO_IDX(prev_entry->offset), 521 (vm_size_t)(prev_entry->end - prev_entry->start), 522 (vm_size_t)(end - prev_entry->end)))) { 523 /* 524 * We were able to extend the object. Determine if we 525 * can extend the previous map entry to include the 526 * new range as well. 527 */ 528 if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) && 529 (prev_entry->protection == prot) && 530 (prev_entry->max_protection == max)) { 531 map->size += (end - prev_entry->end); 532 prev_entry->end = end; 533 vm_map_simplify_entry(map, prev_entry); 534 return (KERN_SUCCESS); 535 } 536 537 /* 538 * If we can extend the object but cannot extend the 539 * map entry, we have to create a new map entry. We 540 * must bump the ref count on the extended object to 541 * account for it. object may be NULL. 542 */ 543 object = prev_entry->object.vm_object; 544 offset = prev_entry->offset + 545 (prev_entry->end - prev_entry->start); 546 vm_object_reference(object); 547 } 548 549 /* 550 * NOTE: if conditionals fail, object can be NULL here. This occurs 551 * in things like the buffer map where we manage kva but do not manage 552 * backing objects. 553 */ 554 555 /* 556 * Create a new entry 557 */ 558 559 new_entry = vm_map_entry_create(map); 560 new_entry->start = start; 561 new_entry->end = end; 562 563 new_entry->eflags = protoeflags; 564 new_entry->object.vm_object = object; 565 new_entry->offset = offset; 566 new_entry->avail_ssize = 0; 567 568 new_entry->inheritance = VM_INHERIT_DEFAULT; 569 new_entry->protection = prot; 570 new_entry->max_protection = max; 571 new_entry->wired_count = 0; 572 573 /* 574 * Insert the new entry into the list 575 */ 576 577 vm_map_entry_link(map, prev_entry, new_entry); 578 map->size += new_entry->end - new_entry->start; 579 580 /* 581 * Update the free space hint 582 */ 583 if ((map->first_free == prev_entry) && 584 (prev_entry->end >= new_entry->start)) { 585 map->first_free = new_entry; 586 } 587 588 #if 0 589 /* 590 * Temporarily removed to avoid MAP_STACK panic, due to 591 * MAP_STACK being a huge hack. Will be added back in 592 * when MAP_STACK (and the user stack mapping) is fixed. 593 */ 594 /* 595 * It may be possible to simplify the entry 596 */ 597 vm_map_simplify_entry(map, new_entry); 598 #endif 599 600 if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) { 601 pmap_object_init_pt(map->pmap, start, 602 object, OFF_TO_IDX(offset), end - start, 603 cow & MAP_PREFAULT_PARTIAL); 604 } 605 606 return (KERN_SUCCESS); 607 } 608 609 /* 610 * Find sufficient space for `length' bytes in the given map, starting at 611 * `start'. The map must be locked. Returns 0 on success, 1 on no space. 612 */ 613 int 614 vm_map_findspace(map, start, length, addr) 615 vm_map_t map; 616 vm_offset_t start; 617 vm_size_t length; 618 vm_offset_t *addr; 619 { 620 vm_map_entry_t entry, next; 621 vm_offset_t end; 622 623 mtx_assert(&vm_mtx, MA_OWNED); 624 if (start < map->min_offset) 625 start = map->min_offset; 626 if (start > map->max_offset) 627 return (1); 628 629 /* 630 * Look for the first possible address; if there's already something 631 * at this address, we have to start after it. 632 */ 633 if (start == map->min_offset) { 634 if ((entry = map->first_free) != &map->header) 635 start = entry->end; 636 } else { 637 vm_map_entry_t tmp; 638 639 if (vm_map_lookup_entry(map, start, &tmp)) 640 start = tmp->end; 641 entry = tmp; 642 } 643 644 /* 645 * Look through the rest of the map, trying to fit a new region in the 646 * gap between existing regions, or after the very last region. 647 */ 648 for (;; start = (entry = next)->end) { 649 /* 650 * Find the end of the proposed new region. Be sure we didn't 651 * go beyond the end of the map, or wrap around the address; 652 * if so, we lose. Otherwise, if this is the last entry, or 653 * if the proposed new region fits before the next entry, we 654 * win. 655 */ 656 end = start + length; 657 if (end > map->max_offset || end < start) 658 return (1); 659 next = entry->next; 660 if (next == &map->header || next->start >= end) 661 break; 662 } 663 SAVE_HINT(map, entry); 664 *addr = start; 665 if (map == kernel_map) { 666 vm_offset_t ksize; 667 if ((ksize = round_page(start + length)) > kernel_vm_end) { 668 pmap_growkernel(ksize); 669 } 670 } 671 return (0); 672 } 673 674 /* 675 * vm_map_find finds an unallocated region in the target address 676 * map with the given length. The search is defined to be 677 * first-fit from the specified address; the region found is 678 * returned in the same parameter. 679 * 680 * If object is non-NULL, ref count must be bumped by caller 681 * prior to making call to account for the new entry. 682 */ 683 int 684 vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 685 vm_offset_t *addr, /* IN/OUT */ 686 vm_size_t length, boolean_t find_space, vm_prot_t prot, 687 vm_prot_t max, int cow) 688 { 689 vm_offset_t start; 690 int result, s = 0; 691 692 mtx_assert(&vm_mtx, MA_OWNED); 693 start = *addr; 694 695 if (map == kmem_map || map == mb_map) 696 s = splvm(); 697 698 vm_map_lock(map); 699 if (find_space) { 700 if (vm_map_findspace(map, start, length, addr)) { 701 vm_map_unlock(map); 702 if (map == kmem_map || map == mb_map) 703 splx(s); 704 return (KERN_NO_SPACE); 705 } 706 start = *addr; 707 } 708 result = vm_map_insert(map, object, offset, 709 start, start + length, prot, max, cow); 710 vm_map_unlock(map); 711 712 if (map == kmem_map || map == mb_map) 713 splx(s); 714 715 return (result); 716 } 717 718 /* 719 * vm_map_simplify_entry: 720 * 721 * Simplify the given map entry by merging with either neighbor. This 722 * routine also has the ability to merge with both neighbors. 723 * 724 * The map must be locked. 725 * 726 * This routine guarentees that the passed entry remains valid (though 727 * possibly extended). When merging, this routine may delete one or 728 * both neighbors. 729 */ 730 void 731 vm_map_simplify_entry(map, entry) 732 vm_map_t map; 733 vm_map_entry_t entry; 734 { 735 vm_map_entry_t next, prev; 736 vm_size_t prevsize, esize; 737 738 mtx_assert(&vm_mtx, MA_OWNED); 739 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 740 return; 741 742 prev = entry->prev; 743 if (prev != &map->header) { 744 prevsize = prev->end - prev->start; 745 if ( (prev->end == entry->start) && 746 (prev->object.vm_object == entry->object.vm_object) && 747 (!prev->object.vm_object || 748 (prev->offset + prevsize == entry->offset)) && 749 (prev->eflags == entry->eflags) && 750 (prev->protection == entry->protection) && 751 (prev->max_protection == entry->max_protection) && 752 (prev->inheritance == entry->inheritance) && 753 (prev->wired_count == entry->wired_count)) { 754 if (map->first_free == prev) 755 map->first_free = entry; 756 if (map->hint == prev) 757 map->hint = entry; 758 vm_map_entry_unlink(map, prev); 759 entry->start = prev->start; 760 entry->offset = prev->offset; 761 if (prev->object.vm_object) 762 vm_object_deallocate(prev->object.vm_object); 763 vm_map_entry_dispose(map, prev); 764 } 765 } 766 767 next = entry->next; 768 if (next != &map->header) { 769 esize = entry->end - entry->start; 770 if ((entry->end == next->start) && 771 (next->object.vm_object == entry->object.vm_object) && 772 (!entry->object.vm_object || 773 (entry->offset + esize == next->offset)) && 774 (next->eflags == entry->eflags) && 775 (next->protection == entry->protection) && 776 (next->max_protection == entry->max_protection) && 777 (next->inheritance == entry->inheritance) && 778 (next->wired_count == entry->wired_count)) { 779 if (map->first_free == next) 780 map->first_free = entry; 781 if (map->hint == next) 782 map->hint = entry; 783 vm_map_entry_unlink(map, next); 784 entry->end = next->end; 785 if (next->object.vm_object) 786 vm_object_deallocate(next->object.vm_object); 787 vm_map_entry_dispose(map, next); 788 } 789 } 790 } 791 /* 792 * vm_map_clip_start: [ internal use only ] 793 * 794 * Asserts that the given entry begins at or after 795 * the specified address; if necessary, 796 * it splits the entry into two. 797 */ 798 #define vm_map_clip_start(map, entry, startaddr) \ 799 { \ 800 if (startaddr > entry->start) \ 801 _vm_map_clip_start(map, entry, startaddr); \ 802 } 803 804 /* 805 * This routine is called only when it is known that 806 * the entry must be split. 807 */ 808 static void 809 _vm_map_clip_start(map, entry, start) 810 vm_map_t map; 811 vm_map_entry_t entry; 812 vm_offset_t start; 813 { 814 vm_map_entry_t new_entry; 815 816 /* 817 * Split off the front portion -- note that we must insert the new 818 * entry BEFORE this one, so that this entry has the specified 819 * starting address. 820 */ 821 822 vm_map_simplify_entry(map, entry); 823 824 /* 825 * If there is no object backing this entry, we might as well create 826 * one now. If we defer it, an object can get created after the map 827 * is clipped, and individual objects will be created for the split-up 828 * map. This is a bit of a hack, but is also about the best place to 829 * put this improvement. 830 */ 831 832 if (entry->object.vm_object == NULL && !map->system_map) { 833 vm_object_t object; 834 object = vm_object_allocate(OBJT_DEFAULT, 835 atop(entry->end - entry->start)); 836 entry->object.vm_object = object; 837 entry->offset = 0; 838 } 839 840 new_entry = vm_map_entry_create(map); 841 *new_entry = *entry; 842 843 new_entry->end = start; 844 entry->offset += (start - entry->start); 845 entry->start = start; 846 847 vm_map_entry_link(map, entry->prev, new_entry); 848 849 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 850 vm_object_reference(new_entry->object.vm_object); 851 } 852 } 853 854 /* 855 * vm_map_clip_end: [ internal use only ] 856 * 857 * Asserts that the given entry ends at or before 858 * the specified address; if necessary, 859 * it splits the entry into two. 860 */ 861 862 #define vm_map_clip_end(map, entry, endaddr) \ 863 { \ 864 if (endaddr < entry->end) \ 865 _vm_map_clip_end(map, entry, endaddr); \ 866 } 867 868 /* 869 * This routine is called only when it is known that 870 * the entry must be split. 871 */ 872 static void 873 _vm_map_clip_end(map, entry, end) 874 vm_map_t map; 875 vm_map_entry_t entry; 876 vm_offset_t end; 877 { 878 vm_map_entry_t new_entry; 879 880 /* 881 * If there is no object backing this entry, we might as well create 882 * one now. If we defer it, an object can get created after the map 883 * is clipped, and individual objects will be created for the split-up 884 * map. This is a bit of a hack, but is also about the best place to 885 * put this improvement. 886 */ 887 888 if (entry->object.vm_object == NULL && !map->system_map) { 889 vm_object_t object; 890 object = vm_object_allocate(OBJT_DEFAULT, 891 atop(entry->end - entry->start)); 892 entry->object.vm_object = object; 893 entry->offset = 0; 894 } 895 896 /* 897 * Create a new entry and insert it AFTER the specified entry 898 */ 899 900 new_entry = vm_map_entry_create(map); 901 *new_entry = *entry; 902 903 new_entry->start = entry->end = end; 904 new_entry->offset += (end - entry->start); 905 906 vm_map_entry_link(map, entry, new_entry); 907 908 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 909 vm_object_reference(new_entry->object.vm_object); 910 } 911 } 912 913 /* 914 * VM_MAP_RANGE_CHECK: [ internal use only ] 915 * 916 * Asserts that the starting and ending region 917 * addresses fall within the valid range of the map. 918 */ 919 #define VM_MAP_RANGE_CHECK(map, start, end) \ 920 { \ 921 if (start < vm_map_min(map)) \ 922 start = vm_map_min(map); \ 923 if (end > vm_map_max(map)) \ 924 end = vm_map_max(map); \ 925 if (start > end) \ 926 start = end; \ 927 } 928 929 /* 930 * vm_map_submap: [ kernel use only ] 931 * 932 * Mark the given range as handled by a subordinate map. 933 * 934 * This range must have been created with vm_map_find, 935 * and no other operations may have been performed on this 936 * range prior to calling vm_map_submap. 937 * 938 * Only a limited number of operations can be performed 939 * within this rage after calling vm_map_submap: 940 * vm_fault 941 * [Don't try vm_map_copy!] 942 * 943 * To remove a submapping, one must first remove the 944 * range from the superior map, and then destroy the 945 * submap (if desired). [Better yet, don't try it.] 946 */ 947 int 948 vm_map_submap(map, start, end, submap) 949 vm_map_t map; 950 vm_offset_t start; 951 vm_offset_t end; 952 vm_map_t submap; 953 { 954 vm_map_entry_t entry; 955 int result = KERN_INVALID_ARGUMENT; 956 957 mtx_assert(&vm_mtx, MA_OWNED); 958 vm_map_lock(map); 959 960 VM_MAP_RANGE_CHECK(map, start, end); 961 962 if (vm_map_lookup_entry(map, start, &entry)) { 963 vm_map_clip_start(map, entry, start); 964 } else 965 entry = entry->next; 966 967 vm_map_clip_end(map, entry, end); 968 969 if ((entry->start == start) && (entry->end == end) && 970 ((entry->eflags & MAP_ENTRY_COW) == 0) && 971 (entry->object.vm_object == NULL)) { 972 entry->object.sub_map = submap; 973 entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 974 result = KERN_SUCCESS; 975 } 976 vm_map_unlock(map); 977 978 return (result); 979 } 980 981 /* 982 * vm_map_protect: 983 * 984 * Sets the protection of the specified address 985 * region in the target map. If "set_max" is 986 * specified, the maximum protection is to be set; 987 * otherwise, only the current protection is affected. 988 */ 989 int 990 vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 991 vm_prot_t new_prot, boolean_t set_max) 992 { 993 vm_map_entry_t current; 994 vm_map_entry_t entry; 995 996 mtx_assert(&vm_mtx, MA_OWNED); 997 vm_map_lock(map); 998 999 VM_MAP_RANGE_CHECK(map, start, end); 1000 1001 if (vm_map_lookup_entry(map, start, &entry)) { 1002 vm_map_clip_start(map, entry, start); 1003 } else { 1004 entry = entry->next; 1005 } 1006 1007 /* 1008 * Make a first pass to check for protection violations. 1009 */ 1010 1011 current = entry; 1012 while ((current != &map->header) && (current->start < end)) { 1013 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1014 vm_map_unlock(map); 1015 return (KERN_INVALID_ARGUMENT); 1016 } 1017 if ((new_prot & current->max_protection) != new_prot) { 1018 vm_map_unlock(map); 1019 return (KERN_PROTECTION_FAILURE); 1020 } 1021 current = current->next; 1022 } 1023 1024 /* 1025 * Go back and fix up protections. [Note that clipping is not 1026 * necessary the second time.] 1027 */ 1028 1029 current = entry; 1030 1031 while ((current != &map->header) && (current->start < end)) { 1032 vm_prot_t old_prot; 1033 1034 vm_map_clip_end(map, current, end); 1035 1036 old_prot = current->protection; 1037 if (set_max) 1038 current->protection = 1039 (current->max_protection = new_prot) & 1040 old_prot; 1041 else 1042 current->protection = new_prot; 1043 1044 /* 1045 * Update physical map if necessary. Worry about copy-on-write 1046 * here -- CHECK THIS XXX 1047 */ 1048 1049 if (current->protection != old_prot) { 1050 #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1051 VM_PROT_ALL) 1052 1053 pmap_protect(map->pmap, current->start, 1054 current->end, 1055 current->protection & MASK(current)); 1056 #undef MASK 1057 } 1058 1059 vm_map_simplify_entry(map, current); 1060 1061 current = current->next; 1062 } 1063 1064 vm_map_unlock(map); 1065 return (KERN_SUCCESS); 1066 } 1067 1068 /* 1069 * vm_map_madvise: 1070 * 1071 * This routine traverses a processes map handling the madvise 1072 * system call. Advisories are classified as either those effecting 1073 * the vm_map_entry structure, or those effecting the underlying 1074 * objects. 1075 */ 1076 1077 int 1078 vm_map_madvise(map, start, end, behav) 1079 vm_map_t map; 1080 vm_offset_t start, end; 1081 int behav; 1082 { 1083 vm_map_entry_t current, entry; 1084 int modify_map = 0; 1085 1086 mtx_assert(&vm_mtx, MA_OWNED); 1087 /* 1088 * Some madvise calls directly modify the vm_map_entry, in which case 1089 * we need to use an exclusive lock on the map and we need to perform 1090 * various clipping operations. Otherwise we only need a read-lock 1091 * on the map. 1092 */ 1093 1094 switch(behav) { 1095 case MADV_NORMAL: 1096 case MADV_SEQUENTIAL: 1097 case MADV_RANDOM: 1098 case MADV_NOSYNC: 1099 case MADV_AUTOSYNC: 1100 case MADV_NOCORE: 1101 case MADV_CORE: 1102 modify_map = 1; 1103 vm_map_lock(map); 1104 break; 1105 case MADV_WILLNEED: 1106 case MADV_DONTNEED: 1107 case MADV_FREE: 1108 vm_map_lock_read(map); 1109 break; 1110 default: 1111 return (KERN_INVALID_ARGUMENT); 1112 } 1113 1114 /* 1115 * Locate starting entry and clip if necessary. 1116 */ 1117 1118 VM_MAP_RANGE_CHECK(map, start, end); 1119 1120 if (vm_map_lookup_entry(map, start, &entry)) { 1121 if (modify_map) 1122 vm_map_clip_start(map, entry, start); 1123 } else { 1124 entry = entry->next; 1125 } 1126 1127 if (modify_map) { 1128 /* 1129 * madvise behaviors that are implemented in the vm_map_entry. 1130 * 1131 * We clip the vm_map_entry so that behavioral changes are 1132 * limited to the specified address range. 1133 */ 1134 for (current = entry; 1135 (current != &map->header) && (current->start < end); 1136 current = current->next 1137 ) { 1138 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1139 continue; 1140 1141 vm_map_clip_end(map, current, end); 1142 1143 switch (behav) { 1144 case MADV_NORMAL: 1145 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1146 break; 1147 case MADV_SEQUENTIAL: 1148 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1149 break; 1150 case MADV_RANDOM: 1151 vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1152 break; 1153 case MADV_NOSYNC: 1154 current->eflags |= MAP_ENTRY_NOSYNC; 1155 break; 1156 case MADV_AUTOSYNC: 1157 current->eflags &= ~MAP_ENTRY_NOSYNC; 1158 break; 1159 case MADV_NOCORE: 1160 current->eflags |= MAP_ENTRY_NOCOREDUMP; 1161 break; 1162 case MADV_CORE: 1163 current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 1164 break; 1165 default: 1166 break; 1167 } 1168 vm_map_simplify_entry(map, current); 1169 } 1170 vm_map_unlock(map); 1171 } else { 1172 vm_pindex_t pindex; 1173 int count; 1174 1175 /* 1176 * madvise behaviors that are implemented in the underlying 1177 * vm_object. 1178 * 1179 * Since we don't clip the vm_map_entry, we have to clip 1180 * the vm_object pindex and count. 1181 */ 1182 for (current = entry; 1183 (current != &map->header) && (current->start < end); 1184 current = current->next 1185 ) { 1186 vm_offset_t useStart; 1187 1188 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1189 continue; 1190 1191 pindex = OFF_TO_IDX(current->offset); 1192 count = atop(current->end - current->start); 1193 useStart = current->start; 1194 1195 if (current->start < start) { 1196 pindex += atop(start - current->start); 1197 count -= atop(start - current->start); 1198 useStart = start; 1199 } 1200 if (current->end > end) 1201 count -= atop(current->end - end); 1202 1203 if (count <= 0) 1204 continue; 1205 1206 vm_object_madvise(current->object.vm_object, 1207 pindex, count, behav); 1208 if (behav == MADV_WILLNEED) { 1209 pmap_object_init_pt( 1210 map->pmap, 1211 useStart, 1212 current->object.vm_object, 1213 pindex, 1214 (count << PAGE_SHIFT), 1215 0 1216 ); 1217 } 1218 } 1219 vm_map_unlock_read(map); 1220 } 1221 return(0); 1222 } 1223 1224 1225 /* 1226 * vm_map_inherit: 1227 * 1228 * Sets the inheritance of the specified address 1229 * range in the target map. Inheritance 1230 * affects how the map will be shared with 1231 * child maps at the time of vm_map_fork. 1232 */ 1233 int 1234 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1235 vm_inherit_t new_inheritance) 1236 { 1237 vm_map_entry_t entry; 1238 vm_map_entry_t temp_entry; 1239 1240 mtx_assert(&vm_mtx, MA_OWNED); 1241 switch (new_inheritance) { 1242 case VM_INHERIT_NONE: 1243 case VM_INHERIT_COPY: 1244 case VM_INHERIT_SHARE: 1245 break; 1246 default: 1247 return (KERN_INVALID_ARGUMENT); 1248 } 1249 1250 vm_map_lock(map); 1251 1252 VM_MAP_RANGE_CHECK(map, start, end); 1253 1254 if (vm_map_lookup_entry(map, start, &temp_entry)) { 1255 entry = temp_entry; 1256 vm_map_clip_start(map, entry, start); 1257 } else 1258 entry = temp_entry->next; 1259 1260 while ((entry != &map->header) && (entry->start < end)) { 1261 vm_map_clip_end(map, entry, end); 1262 1263 entry->inheritance = new_inheritance; 1264 1265 vm_map_simplify_entry(map, entry); 1266 1267 entry = entry->next; 1268 } 1269 1270 vm_map_unlock(map); 1271 return (KERN_SUCCESS); 1272 } 1273 1274 /* 1275 * Implement the semantics of mlock 1276 */ 1277 int 1278 vm_map_user_pageable(map, start, end, new_pageable) 1279 vm_map_t map; 1280 vm_offset_t start; 1281 vm_offset_t end; 1282 boolean_t new_pageable; 1283 { 1284 vm_map_entry_t entry; 1285 vm_map_entry_t start_entry; 1286 vm_offset_t estart; 1287 int rv; 1288 1289 vm_map_lock(map); 1290 VM_MAP_RANGE_CHECK(map, start, end); 1291 1292 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1293 vm_map_unlock(map); 1294 return (KERN_INVALID_ADDRESS); 1295 } 1296 1297 if (new_pageable) { 1298 1299 entry = start_entry; 1300 vm_map_clip_start(map, entry, start); 1301 1302 /* 1303 * Now decrement the wiring count for each region. If a region 1304 * becomes completely unwired, unwire its physical pages and 1305 * mappings. 1306 */ 1307 while ((entry != &map->header) && (entry->start < end)) { 1308 if (entry->eflags & MAP_ENTRY_USER_WIRED) { 1309 vm_map_clip_end(map, entry, end); 1310 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1311 entry->wired_count--; 1312 if (entry->wired_count == 0) 1313 vm_fault_unwire(map, entry->start, entry->end); 1314 } 1315 vm_map_simplify_entry(map,entry); 1316 entry = entry->next; 1317 } 1318 } else { 1319 1320 entry = start_entry; 1321 1322 while ((entry != &map->header) && (entry->start < end)) { 1323 1324 if (entry->eflags & MAP_ENTRY_USER_WIRED) { 1325 entry = entry->next; 1326 continue; 1327 } 1328 1329 if (entry->wired_count != 0) { 1330 entry->wired_count++; 1331 entry->eflags |= MAP_ENTRY_USER_WIRED; 1332 entry = entry->next; 1333 continue; 1334 } 1335 1336 /* Here on entry being newly wired */ 1337 1338 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1339 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1340 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) { 1341 1342 vm_object_shadow(&entry->object.vm_object, 1343 &entry->offset, 1344 atop(entry->end - entry->start)); 1345 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 1346 1347 } else if (entry->object.vm_object == NULL && 1348 !map->system_map) { 1349 1350 entry->object.vm_object = 1351 vm_object_allocate(OBJT_DEFAULT, 1352 atop(entry->end - entry->start)); 1353 entry->offset = (vm_offset_t) 0; 1354 1355 } 1356 } 1357 1358 vm_map_clip_start(map, entry, start); 1359 vm_map_clip_end(map, entry, end); 1360 1361 entry->wired_count++; 1362 entry->eflags |= MAP_ENTRY_USER_WIRED; 1363 estart = entry->start; 1364 1365 /* First we need to allow map modifications */ 1366 vm_map_set_recursive(map); 1367 vm_map_lock_downgrade(map); 1368 map->timestamp++; 1369 1370 rv = vm_fault_user_wire(map, entry->start, entry->end); 1371 if (rv) { 1372 1373 entry->wired_count--; 1374 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1375 1376 vm_map_clear_recursive(map); 1377 vm_map_unlock(map); 1378 1379 (void) vm_map_user_pageable(map, start, entry->start, TRUE); 1380 return rv; 1381 } 1382 1383 vm_map_clear_recursive(map); 1384 if (vm_map_lock_upgrade(map)) { 1385 vm_map_lock(map); 1386 if (vm_map_lookup_entry(map, estart, &entry) 1387 == FALSE) { 1388 vm_map_unlock(map); 1389 (void) vm_map_user_pageable(map, 1390 start, 1391 estart, 1392 TRUE); 1393 return (KERN_INVALID_ADDRESS); 1394 } 1395 } 1396 vm_map_simplify_entry(map,entry); 1397 } 1398 } 1399 map->timestamp++; 1400 vm_map_unlock(map); 1401 return KERN_SUCCESS; 1402 } 1403 1404 /* 1405 * vm_map_pageable: 1406 * 1407 * Sets the pageability of the specified address 1408 * range in the target map. Regions specified 1409 * as not pageable require locked-down physical 1410 * memory and physical page maps. 1411 * 1412 * The map must not be locked, but a reference 1413 * must remain to the map throughout the call. 1414 */ 1415 int 1416 vm_map_pageable(map, start, end, new_pageable) 1417 vm_map_t map; 1418 vm_offset_t start; 1419 vm_offset_t end; 1420 boolean_t new_pageable; 1421 { 1422 vm_map_entry_t entry; 1423 vm_map_entry_t start_entry; 1424 vm_offset_t failed = 0; 1425 int rv; 1426 1427 mtx_assert(&vm_mtx, MA_OWNED); 1428 vm_map_lock(map); 1429 1430 VM_MAP_RANGE_CHECK(map, start, end); 1431 1432 /* 1433 * Only one pageability change may take place at one time, since 1434 * vm_fault assumes it will be called only once for each 1435 * wiring/unwiring. Therefore, we have to make sure we're actually 1436 * changing the pageability for the entire region. We do so before 1437 * making any changes. 1438 */ 1439 1440 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1441 vm_map_unlock(map); 1442 return (KERN_INVALID_ADDRESS); 1443 } 1444 entry = start_entry; 1445 1446 /* 1447 * Actions are rather different for wiring and unwiring, so we have 1448 * two separate cases. 1449 */ 1450 1451 if (new_pageable) { 1452 1453 vm_map_clip_start(map, entry, start); 1454 1455 /* 1456 * Unwiring. First ensure that the range to be unwired is 1457 * really wired down and that there are no holes. 1458 */ 1459 while ((entry != &map->header) && (entry->start < end)) { 1460 1461 if (entry->wired_count == 0 || 1462 (entry->end < end && 1463 (entry->next == &map->header || 1464 entry->next->start > entry->end))) { 1465 vm_map_unlock(map); 1466 return (KERN_INVALID_ARGUMENT); 1467 } 1468 entry = entry->next; 1469 } 1470 1471 /* 1472 * Now decrement the wiring count for each region. If a region 1473 * becomes completely unwired, unwire its physical pages and 1474 * mappings. 1475 */ 1476 entry = start_entry; 1477 while ((entry != &map->header) && (entry->start < end)) { 1478 vm_map_clip_end(map, entry, end); 1479 1480 entry->wired_count--; 1481 if (entry->wired_count == 0) 1482 vm_fault_unwire(map, entry->start, entry->end); 1483 1484 vm_map_simplify_entry(map, entry); 1485 1486 entry = entry->next; 1487 } 1488 } else { 1489 /* 1490 * Wiring. We must do this in two passes: 1491 * 1492 * 1. Holding the write lock, we create any shadow or zero-fill 1493 * objects that need to be created. Then we clip each map 1494 * entry to the region to be wired and increment its wiring 1495 * count. We create objects before clipping the map entries 1496 * to avoid object proliferation. 1497 * 1498 * 2. We downgrade to a read lock, and call vm_fault_wire to 1499 * fault in the pages for any newly wired area (wired_count is 1500 * 1). 1501 * 1502 * Downgrading to a read lock for vm_fault_wire avoids a possible 1503 * deadlock with another process that may have faulted on one 1504 * of the pages to be wired (it would mark the page busy, 1505 * blocking us, then in turn block on the map lock that we 1506 * hold). Because of problems in the recursive lock package, 1507 * we cannot upgrade to a write lock in vm_map_lookup. Thus, 1508 * any actions that require the write lock must be done 1509 * beforehand. Because we keep the read lock on the map, the 1510 * copy-on-write status of the entries we modify here cannot 1511 * change. 1512 */ 1513 1514 /* 1515 * Pass 1. 1516 */ 1517 while ((entry != &map->header) && (entry->start < end)) { 1518 if (entry->wired_count == 0) { 1519 1520 /* 1521 * Perform actions of vm_map_lookup that need 1522 * the write lock on the map: create a shadow 1523 * object for a copy-on-write region, or an 1524 * object for a zero-fill region. 1525 * 1526 * We don't have to do this for entries that 1527 * point to sub maps, because we won't 1528 * hold the lock on the sub map. 1529 */ 1530 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1531 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1532 if (copyflag && 1533 ((entry->protection & VM_PROT_WRITE) != 0)) { 1534 1535 vm_object_shadow(&entry->object.vm_object, 1536 &entry->offset, 1537 atop(entry->end - entry->start)); 1538 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 1539 } else if (entry->object.vm_object == NULL && 1540 !map->system_map) { 1541 entry->object.vm_object = 1542 vm_object_allocate(OBJT_DEFAULT, 1543 atop(entry->end - entry->start)); 1544 entry->offset = (vm_offset_t) 0; 1545 } 1546 } 1547 } 1548 vm_map_clip_start(map, entry, start); 1549 vm_map_clip_end(map, entry, end); 1550 entry->wired_count++; 1551 1552 /* 1553 * Check for holes 1554 */ 1555 if (entry->end < end && 1556 (entry->next == &map->header || 1557 entry->next->start > entry->end)) { 1558 /* 1559 * Found one. Object creation actions do not 1560 * need to be undone, but the wired counts 1561 * need to be restored. 1562 */ 1563 while (entry != &map->header && entry->end > start) { 1564 entry->wired_count--; 1565 entry = entry->prev; 1566 } 1567 vm_map_unlock(map); 1568 return (KERN_INVALID_ARGUMENT); 1569 } 1570 entry = entry->next; 1571 } 1572 1573 /* 1574 * Pass 2. 1575 */ 1576 1577 /* 1578 * HACK HACK HACK HACK 1579 * 1580 * If we are wiring in the kernel map or a submap of it, 1581 * unlock the map to avoid deadlocks. We trust that the 1582 * kernel is well-behaved, and therefore will not do 1583 * anything destructive to this region of the map while 1584 * we have it unlocked. We cannot trust user processes 1585 * to do the same. 1586 * 1587 * HACK HACK HACK HACK 1588 */ 1589 if (vm_map_pmap(map) == kernel_pmap) { 1590 vm_map_unlock(map); /* trust me ... */ 1591 } else { 1592 vm_map_lock_downgrade(map); 1593 } 1594 1595 rv = 0; 1596 entry = start_entry; 1597 while (entry != &map->header && entry->start < end) { 1598 /* 1599 * If vm_fault_wire fails for any page we need to undo 1600 * what has been done. We decrement the wiring count 1601 * for those pages which have not yet been wired (now) 1602 * and unwire those that have (later). 1603 * 1604 * XXX this violates the locking protocol on the map, 1605 * needs to be fixed. 1606 */ 1607 if (rv) 1608 entry->wired_count--; 1609 else if (entry->wired_count == 1) { 1610 rv = vm_fault_wire(map, entry->start, entry->end); 1611 if (rv) { 1612 failed = entry->start; 1613 entry->wired_count--; 1614 } 1615 } 1616 entry = entry->next; 1617 } 1618 1619 if (vm_map_pmap(map) == kernel_pmap) { 1620 vm_map_lock(map); 1621 } 1622 if (rv) { 1623 vm_map_unlock(map); 1624 (void) vm_map_pageable(map, start, failed, TRUE); 1625 return (rv); 1626 } 1627 vm_map_simplify_entry(map, start_entry); 1628 } 1629 1630 vm_map_unlock(map); 1631 1632 return (KERN_SUCCESS); 1633 } 1634 1635 /* 1636 * vm_map_clean 1637 * 1638 * Push any dirty cached pages in the address range to their pager. 1639 * If syncio is TRUE, dirty pages are written synchronously. 1640 * If invalidate is TRUE, any cached pages are freed as well. 1641 * 1642 * Returns an error if any part of the specified range is not mapped. 1643 */ 1644 int 1645 vm_map_clean(map, start, end, syncio, invalidate) 1646 vm_map_t map; 1647 vm_offset_t start; 1648 vm_offset_t end; 1649 boolean_t syncio; 1650 boolean_t invalidate; 1651 { 1652 vm_map_entry_t current; 1653 vm_map_entry_t entry; 1654 vm_size_t size; 1655 vm_object_t object; 1656 vm_ooffset_t offset; 1657 1658 mtx_assert(&Giant, MA_OWNED); 1659 mtx_assert(&vm_mtx, MA_OWNED); 1660 vm_map_lock_read(map); 1661 VM_MAP_RANGE_CHECK(map, start, end); 1662 if (!vm_map_lookup_entry(map, start, &entry)) { 1663 vm_map_unlock_read(map); 1664 return (KERN_INVALID_ADDRESS); 1665 } 1666 /* 1667 * Make a first pass to check for holes. 1668 */ 1669 for (current = entry; current->start < end; current = current->next) { 1670 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1671 vm_map_unlock_read(map); 1672 return (KERN_INVALID_ARGUMENT); 1673 } 1674 if (end > current->end && 1675 (current->next == &map->header || 1676 current->end != current->next->start)) { 1677 vm_map_unlock_read(map); 1678 return (KERN_INVALID_ADDRESS); 1679 } 1680 } 1681 1682 if (invalidate) 1683 pmap_remove(vm_map_pmap(map), start, end); 1684 /* 1685 * Make a second pass, cleaning/uncaching pages from the indicated 1686 * objects as we go. 1687 */ 1688 for (current = entry; current->start < end; current = current->next) { 1689 offset = current->offset + (start - current->start); 1690 size = (end <= current->end ? end : current->end) - start; 1691 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1692 vm_map_t smap; 1693 vm_map_entry_t tentry; 1694 vm_size_t tsize; 1695 1696 smap = current->object.sub_map; 1697 vm_map_lock_read(smap); 1698 (void) vm_map_lookup_entry(smap, offset, &tentry); 1699 tsize = tentry->end - offset; 1700 if (tsize < size) 1701 size = tsize; 1702 object = tentry->object.vm_object; 1703 offset = tentry->offset + (offset - tentry->start); 1704 vm_map_unlock_read(smap); 1705 } else { 1706 object = current->object.vm_object; 1707 } 1708 /* 1709 * Note that there is absolutely no sense in writing out 1710 * anonymous objects, so we track down the vnode object 1711 * to write out. 1712 * We invalidate (remove) all pages from the address space 1713 * anyway, for semantic correctness. 1714 */ 1715 while (object->backing_object) { 1716 object = object->backing_object; 1717 offset += object->backing_object_offset; 1718 if (object->size < OFF_TO_IDX( offset + size)) 1719 size = IDX_TO_OFF(object->size) - offset; 1720 } 1721 if (object && (object->type == OBJT_VNODE) && 1722 (current->protection & VM_PROT_WRITE)) { 1723 /* 1724 * Flush pages if writing is allowed, invalidate them 1725 * if invalidation requested. Pages undergoing I/O 1726 * will be ignored by vm_object_page_remove(). 1727 * 1728 * We cannot lock the vnode and then wait for paging 1729 * to complete without deadlocking against vm_fault. 1730 * Instead we simply call vm_object_page_remove() and 1731 * allow it to block internally on a page-by-page 1732 * basis when it encounters pages undergoing async 1733 * I/O. 1734 */ 1735 int flags; 1736 1737 vm_object_reference(object); 1738 mtx_unlock(&vm_mtx); 1739 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc); 1740 mtx_lock(&vm_mtx); 1741 flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 1742 flags |= invalidate ? OBJPC_INVAL : 0; 1743 vm_object_page_clean(object, 1744 OFF_TO_IDX(offset), 1745 OFF_TO_IDX(offset + size + PAGE_MASK), 1746 flags); 1747 if (invalidate) { 1748 /*vm_object_pip_wait(object, "objmcl");*/ 1749 vm_object_page_remove(object, 1750 OFF_TO_IDX(offset), 1751 OFF_TO_IDX(offset + size + PAGE_MASK), 1752 FALSE); 1753 } 1754 VOP_UNLOCK(object->handle, 0, curproc); 1755 vm_object_deallocate(object); 1756 } 1757 start += size; 1758 } 1759 1760 vm_map_unlock_read(map); 1761 return (KERN_SUCCESS); 1762 } 1763 1764 /* 1765 * vm_map_entry_unwire: [ internal use only ] 1766 * 1767 * Make the region specified by this entry pageable. 1768 * 1769 * The map in question should be locked. 1770 * [This is the reason for this routine's existence.] 1771 */ 1772 static void 1773 vm_map_entry_unwire(map, entry) 1774 vm_map_t map; 1775 vm_map_entry_t entry; 1776 { 1777 vm_fault_unwire(map, entry->start, entry->end); 1778 entry->wired_count = 0; 1779 } 1780 1781 /* 1782 * vm_map_entry_delete: [ internal use only ] 1783 * 1784 * Deallocate the given entry from the target map. 1785 */ 1786 static void 1787 vm_map_entry_delete(map, entry) 1788 vm_map_t map; 1789 vm_map_entry_t entry; 1790 { 1791 vm_map_entry_unlink(map, entry); 1792 map->size -= entry->end - entry->start; 1793 1794 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1795 vm_object_deallocate(entry->object.vm_object); 1796 } 1797 1798 vm_map_entry_dispose(map, entry); 1799 } 1800 1801 /* 1802 * vm_map_delete: [ internal use only ] 1803 * 1804 * Deallocates the given address range from the target 1805 * map. 1806 */ 1807 int 1808 vm_map_delete(map, start, end) 1809 vm_map_t map; 1810 vm_offset_t start; 1811 vm_offset_t end; 1812 { 1813 vm_object_t object; 1814 vm_map_entry_t entry; 1815 vm_map_entry_t first_entry; 1816 1817 mtx_assert(&vm_mtx, MA_OWNED); 1818 /* 1819 * Find the start of the region, and clip it 1820 */ 1821 1822 if (!vm_map_lookup_entry(map, start, &first_entry)) 1823 entry = first_entry->next; 1824 else { 1825 entry = first_entry; 1826 vm_map_clip_start(map, entry, start); 1827 /* 1828 * Fix the lookup hint now, rather than each time though the 1829 * loop. 1830 */ 1831 SAVE_HINT(map, entry->prev); 1832 } 1833 1834 /* 1835 * Save the free space hint 1836 */ 1837 1838 if (entry == &map->header) { 1839 map->first_free = &map->header; 1840 } else if (map->first_free->start >= start) { 1841 map->first_free = entry->prev; 1842 } 1843 1844 /* 1845 * Step through all entries in this region 1846 */ 1847 1848 while ((entry != &map->header) && (entry->start < end)) { 1849 vm_map_entry_t next; 1850 vm_offset_t s, e; 1851 vm_pindex_t offidxstart, offidxend, count; 1852 1853 vm_map_clip_end(map, entry, end); 1854 1855 s = entry->start; 1856 e = entry->end; 1857 next = entry->next; 1858 1859 offidxstart = OFF_TO_IDX(entry->offset); 1860 count = OFF_TO_IDX(e - s); 1861 object = entry->object.vm_object; 1862 1863 /* 1864 * Unwire before removing addresses from the pmap; otherwise, 1865 * unwiring will put the entries back in the pmap. 1866 */ 1867 if (entry->wired_count != 0) { 1868 vm_map_entry_unwire(map, entry); 1869 } 1870 1871 offidxend = offidxstart + count; 1872 1873 if ((object == kernel_object) || (object == kmem_object)) { 1874 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 1875 } else { 1876 pmap_remove(map->pmap, s, e); 1877 if (object != NULL && 1878 object->ref_count != 1 && 1879 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING && 1880 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1881 vm_object_collapse(object); 1882 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 1883 if (object->type == OBJT_SWAP) { 1884 swap_pager_freespace(object, offidxstart, count); 1885 } 1886 if (offidxend >= object->size && 1887 offidxstart < object->size) { 1888 object->size = offidxstart; 1889 } 1890 } 1891 } 1892 1893 /* 1894 * Delete the entry (which may delete the object) only after 1895 * removing all pmap entries pointing to its pages. 1896 * (Otherwise, its page frames may be reallocated, and any 1897 * modify bits will be set in the wrong object!) 1898 */ 1899 vm_map_entry_delete(map, entry); 1900 entry = next; 1901 } 1902 return (KERN_SUCCESS); 1903 } 1904 1905 /* 1906 * vm_map_remove: 1907 * 1908 * Remove the given address range from the target map. 1909 * This is the exported form of vm_map_delete. 1910 */ 1911 int 1912 vm_map_remove(map, start, end) 1913 vm_map_t map; 1914 vm_offset_t start; 1915 vm_offset_t end; 1916 { 1917 int result, s = 0; 1918 1919 mtx_assert(&vm_mtx, MA_OWNED); 1920 if (map == kmem_map || map == mb_map) 1921 s = splvm(); 1922 1923 vm_map_lock(map); 1924 VM_MAP_RANGE_CHECK(map, start, end); 1925 result = vm_map_delete(map, start, end); 1926 vm_map_unlock(map); 1927 1928 if (map == kmem_map || map == mb_map) 1929 splx(s); 1930 1931 return (result); 1932 } 1933 1934 /* 1935 * vm_map_check_protection: 1936 * 1937 * Assert that the target map allows the specified 1938 * privilege on the entire address region given. 1939 * The entire region must be allocated. 1940 */ 1941 boolean_t 1942 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 1943 vm_prot_t protection) 1944 { 1945 vm_map_entry_t entry; 1946 vm_map_entry_t tmp_entry; 1947 1948 mtx_assert(&vm_mtx, MA_OWNED); 1949 if (!vm_map_lookup_entry(map, start, &tmp_entry)) { 1950 return (FALSE); 1951 } 1952 entry = tmp_entry; 1953 1954 while (start < end) { 1955 if (entry == &map->header) { 1956 return (FALSE); 1957 } 1958 /* 1959 * No holes allowed! 1960 */ 1961 1962 if (start < entry->start) { 1963 return (FALSE); 1964 } 1965 /* 1966 * Check protection associated with entry. 1967 */ 1968 1969 if ((entry->protection & protection) != protection) { 1970 return (FALSE); 1971 } 1972 /* go to next entry */ 1973 1974 start = entry->end; 1975 entry = entry->next; 1976 } 1977 return (TRUE); 1978 } 1979 1980 /* 1981 * Split the pages in a map entry into a new object. This affords 1982 * easier removal of unused pages, and keeps object inheritance from 1983 * being a negative impact on memory usage. 1984 */ 1985 static void 1986 vm_map_split(entry) 1987 vm_map_entry_t entry; 1988 { 1989 vm_page_t m; 1990 vm_object_t orig_object, new_object, source; 1991 vm_offset_t s, e; 1992 vm_pindex_t offidxstart, offidxend, idx; 1993 vm_size_t size; 1994 vm_ooffset_t offset; 1995 1996 mtx_assert(&vm_mtx, MA_OWNED); 1997 orig_object = entry->object.vm_object; 1998 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1999 return; 2000 if (orig_object->ref_count <= 1) 2001 return; 2002 2003 offset = entry->offset; 2004 s = entry->start; 2005 e = entry->end; 2006 2007 offidxstart = OFF_TO_IDX(offset); 2008 offidxend = offidxstart + OFF_TO_IDX(e - s); 2009 size = offidxend - offidxstart; 2010 2011 new_object = vm_pager_allocate(orig_object->type, 2012 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL); 2013 if (new_object == NULL) 2014 return; 2015 2016 source = orig_object->backing_object; 2017 if (source != NULL) { 2018 vm_object_reference(source); /* Referenced by new_object */ 2019 TAILQ_INSERT_TAIL(&source->shadow_head, 2020 new_object, shadow_list); 2021 vm_object_clear_flag(source, OBJ_ONEMAPPING); 2022 new_object->backing_object_offset = 2023 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart); 2024 new_object->backing_object = source; 2025 source->shadow_count++; 2026 source->generation++; 2027 } 2028 2029 for (idx = 0; idx < size; idx++) { 2030 vm_page_t m; 2031 2032 retry: 2033 m = vm_page_lookup(orig_object, offidxstart + idx); 2034 if (m == NULL) 2035 continue; 2036 2037 /* 2038 * We must wait for pending I/O to complete before we can 2039 * rename the page. 2040 * 2041 * We do not have to VM_PROT_NONE the page as mappings should 2042 * not be changed by this operation. 2043 */ 2044 if (vm_page_sleep_busy(m, TRUE, "spltwt")) 2045 goto retry; 2046 2047 vm_page_busy(m); 2048 vm_page_rename(m, new_object, idx); 2049 /* page automatically made dirty by rename and cache handled */ 2050 vm_page_busy(m); 2051 } 2052 2053 if (orig_object->type == OBJT_SWAP) { 2054 vm_object_pip_add(orig_object, 1); 2055 /* 2056 * copy orig_object pages into new_object 2057 * and destroy unneeded pages in 2058 * shadow object. 2059 */ 2060 swap_pager_copy(orig_object, new_object, offidxstart, 0); 2061 vm_object_pip_wakeup(orig_object); 2062 } 2063 2064 for (idx = 0; idx < size; idx++) { 2065 m = vm_page_lookup(new_object, idx); 2066 if (m) { 2067 vm_page_wakeup(m); 2068 } 2069 } 2070 2071 entry->object.vm_object = new_object; 2072 entry->offset = 0LL; 2073 vm_object_deallocate(orig_object); 2074 } 2075 2076 /* 2077 * vm_map_copy_entry: 2078 * 2079 * Copies the contents of the source entry to the destination 2080 * entry. The entries *must* be aligned properly. 2081 */ 2082 static void 2083 vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry) 2084 vm_map_t src_map, dst_map; 2085 vm_map_entry_t src_entry, dst_entry; 2086 { 2087 vm_object_t src_object; 2088 2089 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2090 return; 2091 2092 if (src_entry->wired_count == 0) { 2093 2094 /* 2095 * If the source entry is marked needs_copy, it is already 2096 * write-protected. 2097 */ 2098 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2099 pmap_protect(src_map->pmap, 2100 src_entry->start, 2101 src_entry->end, 2102 src_entry->protection & ~VM_PROT_WRITE); 2103 } 2104 2105 /* 2106 * Make a copy of the object. 2107 */ 2108 if ((src_object = src_entry->object.vm_object) != NULL) { 2109 2110 if ((src_object->handle == NULL) && 2111 (src_object->type == OBJT_DEFAULT || 2112 src_object->type == OBJT_SWAP)) { 2113 vm_object_collapse(src_object); 2114 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2115 vm_map_split(src_entry); 2116 src_object = src_entry->object.vm_object; 2117 } 2118 } 2119 2120 vm_object_reference(src_object); 2121 vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2122 dst_entry->object.vm_object = src_object; 2123 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2124 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2125 dst_entry->offset = src_entry->offset; 2126 } else { 2127 dst_entry->object.vm_object = NULL; 2128 dst_entry->offset = 0; 2129 } 2130 2131 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2132 dst_entry->end - dst_entry->start, src_entry->start); 2133 } else { 2134 /* 2135 * Of course, wired down pages can't be set copy-on-write. 2136 * Cause wired pages to be copied into the new map by 2137 * simulating faults (the new pages are pageable) 2138 */ 2139 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2140 } 2141 } 2142 2143 /* 2144 * vmspace_fork: 2145 * Create a new process vmspace structure and vm_map 2146 * based on those of an existing process. The new map 2147 * is based on the old map, according to the inheritance 2148 * values on the regions in that map. 2149 * 2150 * The source map must not be locked. 2151 */ 2152 struct vmspace * 2153 vmspace_fork(vm1) 2154 struct vmspace *vm1; 2155 { 2156 struct vmspace *vm2; 2157 vm_map_t old_map = &vm1->vm_map; 2158 vm_map_t new_map; 2159 vm_map_entry_t old_entry; 2160 vm_map_entry_t new_entry; 2161 vm_object_t object; 2162 2163 mtx_assert(&vm_mtx, MA_OWNED); 2164 vm_map_lock(old_map); 2165 old_map->infork = 1; 2166 2167 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2168 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 2169 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy); 2170 new_map = &vm2->vm_map; /* XXX */ 2171 new_map->timestamp = 1; 2172 2173 old_entry = old_map->header.next; 2174 2175 while (old_entry != &old_map->header) { 2176 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2177 panic("vm_map_fork: encountered a submap"); 2178 2179 switch (old_entry->inheritance) { 2180 case VM_INHERIT_NONE: 2181 break; 2182 2183 case VM_INHERIT_SHARE: 2184 /* 2185 * Clone the entry, creating the shared object if necessary. 2186 */ 2187 object = old_entry->object.vm_object; 2188 if (object == NULL) { 2189 object = vm_object_allocate(OBJT_DEFAULT, 2190 atop(old_entry->end - old_entry->start)); 2191 old_entry->object.vm_object = object; 2192 old_entry->offset = (vm_offset_t) 0; 2193 } 2194 2195 /* 2196 * Add the reference before calling vm_object_shadow 2197 * to insure that a shadow object is created. 2198 */ 2199 vm_object_reference(object); 2200 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2201 vm_object_shadow(&old_entry->object.vm_object, 2202 &old_entry->offset, 2203 atop(old_entry->end - old_entry->start)); 2204 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2205 /* Transfer the second reference too. */ 2206 vm_object_reference( 2207 old_entry->object.vm_object); 2208 vm_object_deallocate(object); 2209 object = old_entry->object.vm_object; 2210 } 2211 vm_object_clear_flag(object, OBJ_ONEMAPPING); 2212 2213 /* 2214 * Clone the entry, referencing the shared object. 2215 */ 2216 new_entry = vm_map_entry_create(new_map); 2217 *new_entry = *old_entry; 2218 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2219 new_entry->wired_count = 0; 2220 2221 /* 2222 * Insert the entry into the new map -- we know we're 2223 * inserting at the end of the new map. 2224 */ 2225 2226 vm_map_entry_link(new_map, new_map->header.prev, 2227 new_entry); 2228 2229 /* 2230 * Update the physical map 2231 */ 2232 2233 pmap_copy(new_map->pmap, old_map->pmap, 2234 new_entry->start, 2235 (old_entry->end - old_entry->start), 2236 old_entry->start); 2237 break; 2238 2239 case VM_INHERIT_COPY: 2240 /* 2241 * Clone the entry and link into the map. 2242 */ 2243 new_entry = vm_map_entry_create(new_map); 2244 *new_entry = *old_entry; 2245 new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2246 new_entry->wired_count = 0; 2247 new_entry->object.vm_object = NULL; 2248 vm_map_entry_link(new_map, new_map->header.prev, 2249 new_entry); 2250 vm_map_copy_entry(old_map, new_map, old_entry, 2251 new_entry); 2252 break; 2253 } 2254 old_entry = old_entry->next; 2255 } 2256 2257 new_map->size = old_map->size; 2258 old_map->infork = 0; 2259 vm_map_unlock(old_map); 2260 2261 return (vm2); 2262 } 2263 2264 int 2265 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 2266 vm_prot_t prot, vm_prot_t max, int cow) 2267 { 2268 vm_map_entry_t prev_entry; 2269 vm_map_entry_t new_stack_entry; 2270 vm_size_t init_ssize; 2271 int rv; 2272 2273 mtx_assert(&vm_mtx, MA_OWNED); 2274 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS) 2275 return (KERN_NO_SPACE); 2276 2277 if (max_ssize < SGROWSIZ) 2278 init_ssize = max_ssize; 2279 else 2280 init_ssize = SGROWSIZ; 2281 2282 vm_map_lock(map); 2283 2284 /* If addr is already mapped, no go */ 2285 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 2286 vm_map_unlock(map); 2287 return (KERN_NO_SPACE); 2288 } 2289 2290 /* If we can't accomodate max_ssize in the current mapping, 2291 * no go. However, we need to be aware that subsequent user 2292 * mappings might map into the space we have reserved for 2293 * stack, and currently this space is not protected. 2294 * 2295 * Hopefully we will at least detect this condition 2296 * when we try to grow the stack. 2297 */ 2298 if ((prev_entry->next != &map->header) && 2299 (prev_entry->next->start < addrbos + max_ssize)) { 2300 vm_map_unlock(map); 2301 return (KERN_NO_SPACE); 2302 } 2303 2304 /* We initially map a stack of only init_ssize. We will 2305 * grow as needed later. Since this is to be a grow 2306 * down stack, we map at the top of the range. 2307 * 2308 * Note: we would normally expect prot and max to be 2309 * VM_PROT_ALL, and cow to be 0. Possibly we should 2310 * eliminate these as input parameters, and just 2311 * pass these values here in the insert call. 2312 */ 2313 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize, 2314 addrbos + max_ssize, prot, max, cow); 2315 2316 /* Now set the avail_ssize amount */ 2317 if (rv == KERN_SUCCESS){ 2318 if (prev_entry != &map->header) 2319 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize); 2320 new_stack_entry = prev_entry->next; 2321 if (new_stack_entry->end != addrbos + max_ssize || 2322 new_stack_entry->start != addrbos + max_ssize - init_ssize) 2323 panic ("Bad entry start/end for new stack entry"); 2324 else 2325 new_stack_entry->avail_ssize = max_ssize - init_ssize; 2326 } 2327 2328 vm_map_unlock(map); 2329 return (rv); 2330 } 2331 2332 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 2333 * desired address is already mapped, or if we successfully grow 2334 * the stack. Also returns KERN_SUCCESS if addr is outside the 2335 * stack range (this is strange, but preserves compatibility with 2336 * the grow function in vm_machdep.c). 2337 * 2338 * Will grab vm_mtx if needed 2339 */ 2340 int 2341 vm_map_growstack (struct proc *p, vm_offset_t addr) 2342 { 2343 vm_map_entry_t prev_entry; 2344 vm_map_entry_t stack_entry; 2345 vm_map_entry_t new_stack_entry; 2346 struct vmspace *vm = p->p_vmspace; 2347 vm_map_t map = &vm->vm_map; 2348 vm_offset_t end; 2349 int grow_amount; 2350 int rv; 2351 int is_procstack; 2352 int hadvmlock; 2353 2354 hadvmlock = mtx_owned(&vm_mtx); 2355 if (!hadvmlock) 2356 mtx_lock(&vm_mtx); 2357 #define myreturn(rval) do { \ 2358 if (!hadvmlock) \ 2359 mtx_unlock(&vm_mtx); \ 2360 return (rval); \ 2361 } while (0) 2362 2363 Retry: 2364 vm_map_lock_read(map); 2365 2366 /* If addr is already in the entry range, no need to grow.*/ 2367 if (vm_map_lookup_entry(map, addr, &prev_entry)) { 2368 vm_map_unlock_read(map); 2369 myreturn (KERN_SUCCESS); 2370 } 2371 2372 if ((stack_entry = prev_entry->next) == &map->header) { 2373 vm_map_unlock_read(map); 2374 myreturn (KERN_SUCCESS); 2375 } 2376 if (prev_entry == &map->header) 2377 end = stack_entry->start - stack_entry->avail_ssize; 2378 else 2379 end = prev_entry->end; 2380 2381 /* This next test mimics the old grow function in vm_machdep.c. 2382 * It really doesn't quite make sense, but we do it anyway 2383 * for compatibility. 2384 * 2385 * If not growable stack, return success. This signals the 2386 * caller to proceed as he would normally with normal vm. 2387 */ 2388 if (stack_entry->avail_ssize < 1 || 2389 addr >= stack_entry->start || 2390 addr < stack_entry->start - stack_entry->avail_ssize) { 2391 vm_map_unlock_read(map); 2392 myreturn (KERN_SUCCESS); 2393 } 2394 2395 /* Find the minimum grow amount */ 2396 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE); 2397 if (grow_amount > stack_entry->avail_ssize) { 2398 vm_map_unlock_read(map); 2399 myreturn (KERN_NO_SPACE); 2400 } 2401 2402 /* If there is no longer enough space between the entries 2403 * nogo, and adjust the available space. Note: this 2404 * should only happen if the user has mapped into the 2405 * stack area after the stack was created, and is 2406 * probably an error. 2407 * 2408 * This also effectively destroys any guard page the user 2409 * might have intended by limiting the stack size. 2410 */ 2411 if (grow_amount > stack_entry->start - end) { 2412 if (vm_map_lock_upgrade(map)) 2413 goto Retry; 2414 2415 stack_entry->avail_ssize = stack_entry->start - end; 2416 2417 vm_map_unlock(map); 2418 myreturn (KERN_NO_SPACE); 2419 } 2420 2421 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr; 2422 2423 /* If this is the main process stack, see if we're over the 2424 * stack limit. 2425 */ 2426 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 2427 p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 2428 vm_map_unlock_read(map); 2429 myreturn (KERN_NO_SPACE); 2430 } 2431 2432 /* Round up the grow amount modulo SGROWSIZ */ 2433 grow_amount = roundup (grow_amount, SGROWSIZ); 2434 if (grow_amount > stack_entry->avail_ssize) { 2435 grow_amount = stack_entry->avail_ssize; 2436 } 2437 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 2438 p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 2439 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur - 2440 ctob(vm->vm_ssize); 2441 } 2442 2443 if (vm_map_lock_upgrade(map)) 2444 goto Retry; 2445 2446 /* Get the preliminary new entry start value */ 2447 addr = stack_entry->start - grow_amount; 2448 2449 /* If this puts us into the previous entry, cut back our growth 2450 * to the available space. Also, see the note above. 2451 */ 2452 if (addr < end) { 2453 stack_entry->avail_ssize = stack_entry->start - end; 2454 addr = end; 2455 } 2456 2457 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 2458 VM_PROT_ALL, 2459 VM_PROT_ALL, 2460 0); 2461 2462 /* Adjust the available stack space by the amount we grew. */ 2463 if (rv == KERN_SUCCESS) { 2464 if (prev_entry != &map->header) 2465 vm_map_clip_end(map, prev_entry, addr); 2466 new_stack_entry = prev_entry->next; 2467 if (new_stack_entry->end != stack_entry->start || 2468 new_stack_entry->start != addr) 2469 panic ("Bad stack grow start/end in new stack entry"); 2470 else { 2471 new_stack_entry->avail_ssize = stack_entry->avail_ssize - 2472 (new_stack_entry->end - 2473 new_stack_entry->start); 2474 if (is_procstack) 2475 vm->vm_ssize += btoc(new_stack_entry->end - 2476 new_stack_entry->start); 2477 } 2478 } 2479 2480 vm_map_unlock(map); 2481 myreturn (rv); 2482 #undef myreturn 2483 } 2484 2485 /* 2486 * Unshare the specified VM space for exec. If other processes are 2487 * mapped to it, then create a new one. The new vmspace is null. 2488 */ 2489 2490 void 2491 vmspace_exec(struct proc *p) { 2492 struct vmspace *oldvmspace = p->p_vmspace; 2493 struct vmspace *newvmspace; 2494 vm_map_t map = &p->p_vmspace->vm_map; 2495 2496 mtx_assert(&vm_mtx, MA_OWNED); 2497 newvmspace = vmspace_alloc(map->min_offset, map->max_offset); 2498 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy, 2499 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy); 2500 /* 2501 * This code is written like this for prototype purposes. The 2502 * goal is to avoid running down the vmspace here, but let the 2503 * other process's that are still using the vmspace to finally 2504 * run it down. Even though there is little or no chance of blocking 2505 * here, it is a good idea to keep this form for future mods. 2506 */ 2507 p->p_vmspace = newvmspace; 2508 pmap_pinit2(vmspace_pmap(newvmspace)); 2509 vmspace_free(oldvmspace); 2510 if (p == curproc) 2511 pmap_activate(p); 2512 } 2513 2514 /* 2515 * Unshare the specified VM space for forcing COW. This 2516 * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 2517 */ 2518 2519 void 2520 vmspace_unshare(struct proc *p) { 2521 struct vmspace *oldvmspace = p->p_vmspace; 2522 struct vmspace *newvmspace; 2523 2524 mtx_assert(&vm_mtx, MA_OWNED); 2525 if (oldvmspace->vm_refcnt == 1) 2526 return; 2527 newvmspace = vmspace_fork(oldvmspace); 2528 p->p_vmspace = newvmspace; 2529 pmap_pinit2(vmspace_pmap(newvmspace)); 2530 vmspace_free(oldvmspace); 2531 if (p == curproc) 2532 pmap_activate(p); 2533 } 2534 2535 2536 /* 2537 * vm_map_lookup: 2538 * 2539 * Finds the VM object, offset, and 2540 * protection for a given virtual address in the 2541 * specified map, assuming a page fault of the 2542 * type specified. 2543 * 2544 * Leaves the map in question locked for read; return 2545 * values are guaranteed until a vm_map_lookup_done 2546 * call is performed. Note that the map argument 2547 * is in/out; the returned map must be used in 2548 * the call to vm_map_lookup_done. 2549 * 2550 * A handle (out_entry) is returned for use in 2551 * vm_map_lookup_done, to make that fast. 2552 * 2553 * If a lookup is requested with "write protection" 2554 * specified, the map may be changed to perform virtual 2555 * copying operations, although the data referenced will 2556 * remain the same. 2557 * 2558 * Can block locking maps and while calling vm_object_shadow(). 2559 * Will drop/reaquire the vm_mtx. 2560 */ 2561 int 2562 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 2563 vm_offset_t vaddr, 2564 vm_prot_t fault_typea, 2565 vm_map_entry_t *out_entry, /* OUT */ 2566 vm_object_t *object, /* OUT */ 2567 vm_pindex_t *pindex, /* OUT */ 2568 vm_prot_t *out_prot, /* OUT */ 2569 boolean_t *wired) /* OUT */ 2570 { 2571 vm_map_entry_t entry; 2572 vm_map_t map = *var_map; 2573 vm_prot_t prot; 2574 vm_prot_t fault_type = fault_typea; 2575 2576 mtx_assert(&vm_mtx, MA_OWNED); 2577 RetryLookup:; 2578 2579 /* 2580 * Lookup the faulting address. 2581 */ 2582 2583 vm_map_lock_read(map); 2584 2585 #define RETURN(why) \ 2586 { \ 2587 vm_map_unlock_read(map); \ 2588 return(why); \ 2589 } 2590 2591 /* 2592 * If the map has an interesting hint, try it before calling full 2593 * blown lookup routine. 2594 */ 2595 2596 entry = map->hint; 2597 2598 *out_entry = entry; 2599 2600 if ((entry == &map->header) || 2601 (vaddr < entry->start) || (vaddr >= entry->end)) { 2602 vm_map_entry_t tmp_entry; 2603 2604 /* 2605 * Entry was either not a valid hint, or the vaddr was not 2606 * contained in the entry, so do a full lookup. 2607 */ 2608 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) 2609 RETURN(KERN_INVALID_ADDRESS); 2610 2611 entry = tmp_entry; 2612 *out_entry = entry; 2613 } 2614 2615 /* 2616 * Handle submaps. 2617 */ 2618 2619 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2620 vm_map_t old_map = map; 2621 2622 *var_map = map = entry->object.sub_map; 2623 vm_map_unlock_read(old_map); 2624 goto RetryLookup; 2625 } 2626 2627 /* 2628 * Check whether this task is allowed to have this page. 2629 * Note the special case for MAP_ENTRY_COW 2630 * pages with an override. This is to implement a forced 2631 * COW for debuggers. 2632 */ 2633 2634 if (fault_type & VM_PROT_OVERRIDE_WRITE) 2635 prot = entry->max_protection; 2636 else 2637 prot = entry->protection; 2638 2639 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 2640 if ((fault_type & prot) != fault_type) { 2641 RETURN(KERN_PROTECTION_FAILURE); 2642 } 2643 2644 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 2645 (entry->eflags & MAP_ENTRY_COW) && 2646 (fault_type & VM_PROT_WRITE) && 2647 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 2648 RETURN(KERN_PROTECTION_FAILURE); 2649 } 2650 2651 /* 2652 * If this page is not pageable, we have to get it for all possible 2653 * accesses. 2654 */ 2655 2656 *wired = (entry->wired_count != 0); 2657 if (*wired) 2658 prot = fault_type = entry->protection; 2659 2660 /* 2661 * If the entry was copy-on-write, we either ... 2662 */ 2663 2664 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2665 /* 2666 * If we want to write the page, we may as well handle that 2667 * now since we've got the map locked. 2668 * 2669 * If we don't need to write the page, we just demote the 2670 * permissions allowed. 2671 */ 2672 2673 if (fault_type & VM_PROT_WRITE) { 2674 /* 2675 * Make a new object, and place it in the object 2676 * chain. Note that no new references have appeared 2677 * -- one just moved from the map to the new 2678 * object. 2679 */ 2680 2681 if (vm_map_lock_upgrade(map)) 2682 goto RetryLookup; 2683 2684 vm_object_shadow( 2685 &entry->object.vm_object, 2686 &entry->offset, 2687 atop(entry->end - entry->start)); 2688 2689 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2690 vm_map_lock_downgrade(map); 2691 } else { 2692 /* 2693 * We're attempting to read a copy-on-write page -- 2694 * don't allow writes. 2695 */ 2696 2697 prot &= ~VM_PROT_WRITE; 2698 } 2699 } 2700 2701 /* 2702 * Create an object if necessary. 2703 */ 2704 if (entry->object.vm_object == NULL && 2705 !map->system_map) { 2706 if (vm_map_lock_upgrade(map)) 2707 goto RetryLookup; 2708 2709 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 2710 atop(entry->end - entry->start)); 2711 entry->offset = 0; 2712 vm_map_lock_downgrade(map); 2713 } 2714 2715 /* 2716 * Return the object/offset from this entry. If the entry was 2717 * copy-on-write or empty, it has been fixed up. 2718 */ 2719 2720 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 2721 *object = entry->object.vm_object; 2722 2723 /* 2724 * Return whether this is the only map sharing this data. 2725 */ 2726 2727 *out_prot = prot; 2728 return (KERN_SUCCESS); 2729 2730 #undef RETURN 2731 } 2732 2733 /* 2734 * vm_map_lookup_done: 2735 * 2736 * Releases locks acquired by a vm_map_lookup 2737 * (according to the handle returned by that lookup). 2738 */ 2739 2740 void 2741 vm_map_lookup_done(map, entry) 2742 vm_map_t map; 2743 vm_map_entry_t entry; 2744 { 2745 /* 2746 * Unlock the main-level map 2747 */ 2748 2749 mtx_assert(&vm_mtx, MA_OWNED); 2750 vm_map_unlock_read(map); 2751 } 2752 2753 /* 2754 * Implement uiomove with VM operations. This handles (and collateral changes) 2755 * support every combination of source object modification, and COW type 2756 * operations. 2757 */ 2758 int 2759 vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages) 2760 vm_map_t mapa; 2761 vm_object_t srcobject; 2762 off_t cp; 2763 int cnta; 2764 vm_offset_t uaddra; 2765 int *npages; 2766 { 2767 vm_map_t map; 2768 vm_object_t first_object, oldobject, object; 2769 vm_map_entry_t entry; 2770 vm_prot_t prot; 2771 boolean_t wired; 2772 int tcnt, rv; 2773 vm_offset_t uaddr, start, end, tend; 2774 vm_pindex_t first_pindex, osize, oindex; 2775 off_t ooffset; 2776 int cnt; 2777 2778 mtx_assert(&vm_mtx, MA_OWNED); 2779 if (npages) 2780 *npages = 0; 2781 2782 cnt = cnta; 2783 uaddr = uaddra; 2784 2785 while (cnt > 0) { 2786 map = mapa; 2787 2788 if ((vm_map_lookup(&map, uaddr, 2789 VM_PROT_READ, &entry, &first_object, 2790 &first_pindex, &prot, &wired)) != KERN_SUCCESS) { 2791 return EFAULT; 2792 } 2793 2794 vm_map_clip_start(map, entry, uaddr); 2795 2796 tcnt = cnt; 2797 tend = uaddr + tcnt; 2798 if (tend > entry->end) { 2799 tcnt = entry->end - uaddr; 2800 tend = entry->end; 2801 } 2802 2803 vm_map_clip_end(map, entry, tend); 2804 2805 start = entry->start; 2806 end = entry->end; 2807 2808 osize = atop(tcnt); 2809 2810 oindex = OFF_TO_IDX(cp); 2811 if (npages) { 2812 vm_pindex_t idx; 2813 for (idx = 0; idx < osize; idx++) { 2814 vm_page_t m; 2815 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) { 2816 vm_map_lookup_done(map, entry); 2817 return 0; 2818 } 2819 /* 2820 * disallow busy or invalid pages, but allow 2821 * m->busy pages if they are entirely valid. 2822 */ 2823 if ((m->flags & PG_BUSY) || 2824 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) { 2825 vm_map_lookup_done(map, entry); 2826 return 0; 2827 } 2828 } 2829 } 2830 2831 /* 2832 * If we are changing an existing map entry, just redirect 2833 * the object, and change mappings. 2834 */ 2835 if ((first_object->type == OBJT_VNODE) && 2836 ((oldobject = entry->object.vm_object) == first_object)) { 2837 2838 if ((entry->offset != cp) || (oldobject != srcobject)) { 2839 /* 2840 * Remove old window into the file 2841 */ 2842 pmap_remove (map->pmap, uaddr, tend); 2843 2844 /* 2845 * Force copy on write for mmaped regions 2846 */ 2847 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 2848 2849 /* 2850 * Point the object appropriately 2851 */ 2852 if (oldobject != srcobject) { 2853 2854 /* 2855 * Set the object optimization hint flag 2856 */ 2857 vm_object_set_flag(srcobject, OBJ_OPT); 2858 vm_object_reference(srcobject); 2859 entry->object.vm_object = srcobject; 2860 2861 if (oldobject) { 2862 vm_object_deallocate(oldobject); 2863 } 2864 } 2865 2866 entry->offset = cp; 2867 map->timestamp++; 2868 } else { 2869 pmap_remove (map->pmap, uaddr, tend); 2870 } 2871 2872 } else if ((first_object->ref_count == 1) && 2873 (first_object->size == osize) && 2874 ((first_object->type == OBJT_DEFAULT) || 2875 (first_object->type == OBJT_SWAP)) ) { 2876 2877 oldobject = first_object->backing_object; 2878 2879 if ((first_object->backing_object_offset != cp) || 2880 (oldobject != srcobject)) { 2881 /* 2882 * Remove old window into the file 2883 */ 2884 pmap_remove (map->pmap, uaddr, tend); 2885 2886 /* 2887 * Remove unneeded old pages 2888 */ 2889 vm_object_page_remove(first_object, 0, 0, 0); 2890 2891 /* 2892 * Invalidate swap space 2893 */ 2894 if (first_object->type == OBJT_SWAP) { 2895 swap_pager_freespace(first_object, 2896 0, 2897 first_object->size); 2898 } 2899 2900 /* 2901 * Force copy on write for mmaped regions 2902 */ 2903 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 2904 2905 /* 2906 * Point the object appropriately 2907 */ 2908 if (oldobject != srcobject) { 2909 2910 /* 2911 * Set the object optimization hint flag 2912 */ 2913 vm_object_set_flag(srcobject, OBJ_OPT); 2914 vm_object_reference(srcobject); 2915 2916 if (oldobject) { 2917 TAILQ_REMOVE(&oldobject->shadow_head, 2918 first_object, shadow_list); 2919 oldobject->shadow_count--; 2920 /* XXX bump generation? */ 2921 vm_object_deallocate(oldobject); 2922 } 2923 2924 TAILQ_INSERT_TAIL(&srcobject->shadow_head, 2925 first_object, shadow_list); 2926 srcobject->shadow_count++; 2927 /* XXX bump generation? */ 2928 2929 first_object->backing_object = srcobject; 2930 } 2931 first_object->backing_object_offset = cp; 2932 map->timestamp++; 2933 } else { 2934 pmap_remove (map->pmap, uaddr, tend); 2935 } 2936 /* 2937 * Otherwise, we have to do a logical mmap. 2938 */ 2939 } else { 2940 2941 vm_object_set_flag(srcobject, OBJ_OPT); 2942 vm_object_reference(srcobject); 2943 2944 pmap_remove (map->pmap, uaddr, tend); 2945 2946 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 2947 vm_map_lock_upgrade(map); 2948 2949 if (entry == &map->header) { 2950 map->first_free = &map->header; 2951 } else if (map->first_free->start >= start) { 2952 map->first_free = entry->prev; 2953 } 2954 2955 SAVE_HINT(map, entry->prev); 2956 vm_map_entry_delete(map, entry); 2957 2958 object = srcobject; 2959 ooffset = cp; 2960 2961 rv = vm_map_insert(map, object, ooffset, start, tend, 2962 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE); 2963 2964 if (rv != KERN_SUCCESS) 2965 panic("vm_uiomove: could not insert new entry: %d", rv); 2966 } 2967 2968 /* 2969 * Map the window directly, if it is already in memory 2970 */ 2971 pmap_object_init_pt(map->pmap, uaddr, 2972 srcobject, oindex, tcnt, 0); 2973 2974 map->timestamp++; 2975 vm_map_unlock(map); 2976 2977 cnt -= tcnt; 2978 uaddr += tcnt; 2979 cp += tcnt; 2980 if (npages) 2981 *npages += osize; 2982 } 2983 return 0; 2984 } 2985 2986 /* 2987 * Performs the copy_on_write operations necessary to allow the virtual copies 2988 * into user space to work. This has to be called for write(2) system calls 2989 * from other processes, file unlinking, and file size shrinkage. 2990 * 2991 * Requires that the vm_mtx is held 2992 */ 2993 void 2994 vm_freeze_copyopts(object, froma, toa) 2995 vm_object_t object; 2996 vm_pindex_t froma, toa; 2997 { 2998 int rv; 2999 vm_object_t robject; 3000 vm_pindex_t idx; 3001 3002 mtx_assert(&vm_mtx, MA_OWNED); 3003 if ((object == NULL) || 3004 ((object->flags & OBJ_OPT) == 0)) 3005 return; 3006 3007 if (object->shadow_count > object->ref_count) 3008 panic("vm_freeze_copyopts: sc > rc"); 3009 3010 while((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) { 3011 vm_pindex_t bo_pindex; 3012 vm_page_t m_in, m_out; 3013 3014 bo_pindex = OFF_TO_IDX(robject->backing_object_offset); 3015 3016 vm_object_reference(robject); 3017 3018 vm_object_pip_wait(robject, "objfrz"); 3019 3020 if (robject->ref_count == 1) { 3021 vm_object_deallocate(robject); 3022 continue; 3023 } 3024 3025 vm_object_pip_add(robject, 1); 3026 3027 for (idx = 0; idx < robject->size; idx++) { 3028 3029 m_out = vm_page_grab(robject, idx, 3030 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 3031 3032 if (m_out->valid == 0) { 3033 m_in = vm_page_grab(object, bo_pindex + idx, 3034 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 3035 if (m_in->valid == 0) { 3036 rv = vm_pager_get_pages(object, &m_in, 1, 0); 3037 if (rv != VM_PAGER_OK) { 3038 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex); 3039 continue; 3040 } 3041 vm_page_deactivate(m_in); 3042 } 3043 3044 vm_page_protect(m_in, VM_PROT_NONE); 3045 pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out)); 3046 m_out->valid = m_in->valid; 3047 vm_page_dirty(m_out); 3048 vm_page_activate(m_out); 3049 vm_page_wakeup(m_in); 3050 } 3051 vm_page_wakeup(m_out); 3052 } 3053 3054 object->shadow_count--; 3055 object->ref_count--; 3056 TAILQ_REMOVE(&object->shadow_head, robject, shadow_list); 3057 robject->backing_object = NULL; 3058 robject->backing_object_offset = 0; 3059 3060 vm_object_pip_wakeup(robject); 3061 vm_object_deallocate(robject); 3062 } 3063 3064 vm_object_clear_flag(object, OBJ_OPT); 3065 } 3066 3067 #include "opt_ddb.h" 3068 #ifdef DDB 3069 #include <sys/kernel.h> 3070 3071 #include <ddb/ddb.h> 3072 3073 /* 3074 * vm_map_print: [ debug ] 3075 */ 3076 DB_SHOW_COMMAND(map, vm_map_print) 3077 { 3078 static int nlines; 3079 /* XXX convert args. */ 3080 vm_map_t map = (vm_map_t)addr; 3081 boolean_t full = have_addr; 3082 3083 vm_map_entry_t entry; 3084 3085 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 3086 (void *)map, 3087 (void *)map->pmap, map->nentries, map->timestamp); 3088 nlines++; 3089 3090 if (!full && db_indent) 3091 return; 3092 3093 db_indent += 2; 3094 for (entry = map->header.next; entry != &map->header; 3095 entry = entry->next) { 3096 db_iprintf("map entry %p: start=%p, end=%p\n", 3097 (void *)entry, (void *)entry->start, (void *)entry->end); 3098 nlines++; 3099 { 3100 static char *inheritance_name[4] = 3101 {"share", "copy", "none", "donate_copy"}; 3102 3103 db_iprintf(" prot=%x/%x/%s", 3104 entry->protection, 3105 entry->max_protection, 3106 inheritance_name[(int)(unsigned char)entry->inheritance]); 3107 if (entry->wired_count != 0) 3108 db_printf(", wired"); 3109 } 3110 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3111 /* XXX no %qd in kernel. Truncate entry->offset. */ 3112 db_printf(", share=%p, offset=0x%lx\n", 3113 (void *)entry->object.sub_map, 3114 (long)entry->offset); 3115 nlines++; 3116 if ((entry->prev == &map->header) || 3117 (entry->prev->object.sub_map != 3118 entry->object.sub_map)) { 3119 db_indent += 2; 3120 vm_map_print((db_expr_t)(intptr_t) 3121 entry->object.sub_map, 3122 full, 0, (char *)0); 3123 db_indent -= 2; 3124 } 3125 } else { 3126 /* XXX no %qd in kernel. Truncate entry->offset. */ 3127 db_printf(", object=%p, offset=0x%lx", 3128 (void *)entry->object.vm_object, 3129 (long)entry->offset); 3130 if (entry->eflags & MAP_ENTRY_COW) 3131 db_printf(", copy (%s)", 3132 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3133 db_printf("\n"); 3134 nlines++; 3135 3136 if ((entry->prev == &map->header) || 3137 (entry->prev->object.vm_object != 3138 entry->object.vm_object)) { 3139 db_indent += 2; 3140 vm_object_print((db_expr_t)(intptr_t) 3141 entry->object.vm_object, 3142 full, 0, (char *)0); 3143 nlines += 4; 3144 db_indent -= 2; 3145 } 3146 } 3147 } 3148 db_indent -= 2; 3149 if (db_indent == 0) 3150 nlines = 0; 3151 } 3152 3153 3154 DB_SHOW_COMMAND(procvm, procvm) 3155 { 3156 struct proc *p; 3157 3158 if (have_addr) { 3159 p = (struct proc *) addr; 3160 } else { 3161 p = curproc; 3162 } 3163 3164 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3165 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3166 (void *)vmspace_pmap(p->p_vmspace)); 3167 3168 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 3169 } 3170 3171 #endif /* DDB */ 3172