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 * $Id: vm_map.c,v 1.4 1994/08/04 19:40:47 davidg Exp $ 65 */ 66 67 /* 68 * Virtual memory mapping module. 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/malloc.h> 74 75 #include <vm/vm.h> 76 #include <vm/vm_page.h> 77 #include <vm/vm_object.h> 78 #include <vm/vm_kern.h> 79 80 /* 81 * Virtual memory maps provide for the mapping, protection, 82 * and sharing of virtual memory objects. In addition, 83 * this module provides for an efficient virtual copy of 84 * memory from one map to another. 85 * 86 * Synchronization is required prior to most operations. 87 * 88 * Maps consist of an ordered doubly-linked list of simple 89 * entries; a single hint is used to speed up lookups. 90 * 91 * In order to properly represent the sharing of virtual 92 * memory regions among maps, the map structure is bi-level. 93 * Top-level ("address") maps refer to regions of sharable 94 * virtual memory. These regions are implemented as 95 * ("sharing") maps, which then refer to the actual virtual 96 * memory objects. When two address maps "share" memory, 97 * their top-level maps both have references to the same 98 * sharing map. When memory is virtual-copied from one 99 * address map to another, the references in the sharing 100 * maps are actually copied -- no copying occurs at the 101 * virtual memory object level. 102 * 103 * Since portions of maps are specified by start/end addreses, 104 * which may not align with existing map entries, all 105 * routines merely "clip" entries to these start/end values. 106 * [That is, an entry is split into two, bordering at a 107 * start or end value.] Note that these clippings may not 108 * always be necessary (as the two resulting entries are then 109 * not changed); however, the clipping is done for convenience. 110 * No attempt is currently made to "glue back together" two 111 * abutting entries. 112 * 113 * As mentioned above, virtual copy operations are performed 114 * by copying VM object references from one sharing map to 115 * another, and then marking both regions as copy-on-write. 116 * It is important to note that only one writeable reference 117 * to a VM object region exists in any map -- this means that 118 * shadow object creation can be delayed until a write operation 119 * occurs. 120 */ 121 122 /* 123 * vm_map_startup: 124 * 125 * Initialize the vm_map module. Must be called before 126 * any other vm_map routines. 127 * 128 * Map and entry structures are allocated from the general 129 * purpose memory pool with some exceptions: 130 * 131 * - The kernel map and kmem submap are allocated statically. 132 * - Kernel map entries are allocated out of a static pool. 133 * 134 * These restrictions are necessary since malloc() uses the 135 * maps and requires map entries. 136 */ 137 138 vm_offset_t kentry_data; 139 vm_size_t kentry_data_size; 140 vm_map_entry_t kentry_free; 141 vm_map_t kmap_free; 142 143 int kentry_count; 144 static vm_offset_t mapvm=0; 145 static int mapvmpgcnt=0; 146 147 static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t)); 148 static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t)); 149 150 void vm_map_startup() 151 { 152 register int i; 153 register vm_map_entry_t mep; 154 vm_map_t mp; 155 156 /* 157 * Static map structures for allocation before initialization of 158 * kernel map or kmem map. vm_map_create knows how to deal with them. 159 */ 160 kmap_free = mp = (vm_map_t) kentry_data; 161 i = MAX_KMAP; 162 while (--i > 0) { 163 mp->header.next = (vm_map_entry_t) (mp + 1); 164 mp++; 165 } 166 mp++->header.next = NULL; 167 168 /* 169 * Form a free list of statically allocated kernel map entries 170 * with the rest. 171 */ 172 kentry_free = mep = (vm_map_entry_t) mp; 173 i = (kentry_data_size - MAX_KMAP * sizeof *mp) / sizeof *mep; 174 while (--i > 0) { 175 mep->next = mep + 1; 176 mep++; 177 } 178 mep->next = NULL; 179 } 180 181 /* 182 * Allocate a vmspace structure, including a vm_map and pmap, 183 * and initialize those structures. The refcnt is set to 1. 184 * The remaining fields must be initialized by the caller. 185 */ 186 struct vmspace * 187 vmspace_alloc(min, max, pageable) 188 vm_offset_t min, max; 189 int pageable; 190 { 191 register struct vmspace *vm; 192 if (mapvmpgcnt == 0 && mapvm == 0) { 193 int s; 194 mapvmpgcnt = (cnt.v_page_count * sizeof(struct vm_map_entry) + PAGE_SIZE - 1) / PAGE_SIZE; 195 s = splhigh(); 196 mapvm = kmem_alloc_pageable(kmem_map, mapvmpgcnt * PAGE_SIZE); 197 splx(s); 198 if (!mapvm) 199 mapvmpgcnt = 0; 200 } 201 202 MALLOC(vm, struct vmspace *, sizeof(struct vmspace), M_VMMAP, M_WAITOK); 203 bzero(vm, (caddr_t) &vm->vm_startcopy - (caddr_t) vm); 204 vm_map_init(&vm->vm_map, min, max, pageable); 205 pmap_pinit(&vm->vm_pmap); 206 vm->vm_map.pmap = &vm->vm_pmap; /* XXX */ 207 vm->vm_refcnt = 1; 208 return (vm); 209 } 210 211 void 212 vmspace_free(vm) 213 register struct vmspace *vm; 214 { 215 216 if (--vm->vm_refcnt == 0) { 217 /* 218 * Lock the map, to wait out all other references to it. 219 * Delete all of the mappings and pages they hold, 220 * then call the pmap module to reclaim anything left. 221 */ 222 vm_map_lock(&vm->vm_map); 223 (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset, 224 vm->vm_map.max_offset); 225 pmap_release(&vm->vm_pmap); 226 FREE(vm, M_VMMAP); 227 } 228 } 229 230 /* 231 * vm_map_create: 232 * 233 * Creates and returns a new empty VM map with 234 * the given physical map structure, and having 235 * the given lower and upper address bounds. 236 */ 237 vm_map_t vm_map_create(pmap, min, max, pageable) 238 pmap_t pmap; 239 vm_offset_t min, max; 240 boolean_t pageable; 241 { 242 register vm_map_t result; 243 244 if (kmem_map == NULL) { 245 result = kmap_free; 246 kmap_free = (vm_map_t) result->header.next; 247 if (result == NULL) 248 panic("vm_map_create: out of maps"); 249 } else 250 MALLOC(result, vm_map_t, sizeof(struct vm_map), 251 M_VMMAP, M_WAITOK); 252 253 vm_map_init(result, min, max, pageable); 254 result->pmap = pmap; 255 return(result); 256 } 257 258 /* 259 * Initialize an existing vm_map structure 260 * such as that in the vmspace structure. 261 * The pmap is set elsewhere. 262 */ 263 void 264 vm_map_init(map, min, max, pageable) 265 register struct vm_map *map; 266 vm_offset_t min, max; 267 boolean_t pageable; 268 { 269 map->header.next = map->header.prev = &map->header; 270 map->nentries = 0; 271 map->size = 0; 272 map->ref_count = 1; 273 map->is_main_map = TRUE; 274 map->min_offset = min; 275 map->max_offset = max; 276 map->entries_pageable = pageable; 277 map->first_free = &map->header; 278 map->hint = &map->header; 279 map->timestamp = 0; 280 lock_init(&map->lock, TRUE); 281 simple_lock_init(&map->ref_lock); 282 simple_lock_init(&map->hint_lock); 283 } 284 285 /* 286 * vm_map_entry_create: [ internal use only ] 287 * 288 * Allocates a VM map entry for insertion. 289 * No entry fields are filled in. This routine is 290 */ 291 static struct vm_map_entry *mappool; 292 static int mappoolcnt; 293 294 vm_map_entry_t 295 vm_map_entry_create(map) 296 vm_map_t map; 297 { 298 vm_map_entry_t entry; 299 int s; 300 int i; 301 #define KENTRY_LOW_WATER 64 302 #define MAPENTRY_LOW_WATER 64 303 304 /* 305 * This is a *very* nasty (and sort of incomplete) hack!!!! 306 */ 307 if (kentry_count < KENTRY_LOW_WATER) { 308 if (mapvmpgcnt && mapvm) { 309 vm_page_t m; 310 if (m = vm_page_alloc(kmem_object, mapvm-vm_map_min(kmem_map))) { 311 int newentries; 312 newentries = (NBPG/sizeof (struct vm_map_entry)); 313 vm_page_wire(m); 314 m->flags &= ~PG_BUSY; 315 pmap_enter(vm_map_pmap(kmem_map), mapvm, 316 VM_PAGE_TO_PHYS(m), VM_PROT_DEFAULT, 1); 317 318 entry = (vm_map_entry_t) mapvm; 319 mapvm += NBPG; 320 --mapvmpgcnt; 321 322 for (i = 0; i < newentries; i++) { 323 vm_map_entry_dispose(kernel_map, entry); 324 entry++; 325 } 326 } 327 } 328 } 329 330 if (map == kernel_map || map == kmem_map || map == pager_map) { 331 332 if (entry = kentry_free) { 333 kentry_free = entry->next; 334 --kentry_count; 335 return entry; 336 } 337 338 if (entry = mappool) { 339 mappool = entry->next; 340 --mappoolcnt; 341 return entry; 342 } 343 344 } else { 345 if (entry = mappool) { 346 mappool = entry->next; 347 --mappoolcnt; 348 return entry; 349 } 350 351 MALLOC(entry, vm_map_entry_t, sizeof(struct vm_map_entry), 352 M_VMMAPENT, M_WAITOK); 353 } 354 dopanic: 355 if (entry == NULL) 356 panic("vm_map_entry_create: out of map entries"); 357 358 return(entry); 359 } 360 361 /* 362 * vm_map_entry_dispose: [ internal use only ] 363 * 364 * Inverse of vm_map_entry_create. 365 */ 366 void 367 vm_map_entry_dispose(map, entry) 368 vm_map_t map; 369 vm_map_entry_t entry; 370 { 371 int s; 372 373 if (map == kernel_map || map == kmem_map || map == pager_map || 374 kentry_count < KENTRY_LOW_WATER) { 375 entry->next = kentry_free; 376 kentry_free = entry; 377 ++kentry_count; 378 } else { 379 if (mappoolcnt < MAPENTRY_LOW_WATER) { 380 entry->next = mappool; 381 mappool = entry; 382 ++mappoolcnt; 383 return; 384 } 385 386 FREE(entry, M_VMMAPENT); 387 } 388 } 389 390 /* 391 * vm_map_entry_{un,}link: 392 * 393 * Insert/remove entries from maps. 394 */ 395 #define vm_map_entry_link(map, after_where, entry) \ 396 { \ 397 (map)->nentries++; \ 398 (entry)->prev = (after_where); \ 399 (entry)->next = (after_where)->next; \ 400 (entry)->prev->next = (entry); \ 401 (entry)->next->prev = (entry); \ 402 } 403 #define vm_map_entry_unlink(map, entry) \ 404 { \ 405 (map)->nentries--; \ 406 (entry)->next->prev = (entry)->prev; \ 407 (entry)->prev->next = (entry)->next; \ 408 } 409 410 /* 411 * vm_map_reference: 412 * 413 * Creates another valid reference to the given map. 414 * 415 */ 416 void vm_map_reference(map) 417 register vm_map_t map; 418 { 419 if (map == NULL) 420 return; 421 422 simple_lock(&map->ref_lock); 423 map->ref_count++; 424 simple_unlock(&map->ref_lock); 425 } 426 427 /* 428 * vm_map_deallocate: 429 * 430 * Removes a reference from the specified map, 431 * destroying it if no references remain. 432 * The map should not be locked. 433 */ 434 void vm_map_deallocate(map) 435 register vm_map_t map; 436 { 437 register int c; 438 439 if (map == NULL) 440 return; 441 442 simple_lock(&map->ref_lock); 443 c = --map->ref_count; 444 simple_unlock(&map->ref_lock); 445 446 if (c > 0) { 447 return; 448 } 449 450 /* 451 * Lock the map, to wait out all other references 452 * to it. 453 */ 454 455 vm_map_lock(map); 456 457 (void) vm_map_delete(map, map->min_offset, map->max_offset); 458 459 pmap_destroy(map->pmap); 460 461 FREE(map, M_VMMAP); 462 } 463 464 /* 465 * vm_map_insert: 466 * 467 * Inserts the given whole VM object into the target 468 * map at the specified address range. The object's 469 * size should match that of the address range. 470 * 471 * Requires that the map be locked, and leaves it so. 472 */ 473 int 474 vm_map_insert(map, object, offset, start, end) 475 vm_map_t map; 476 vm_object_t object; 477 vm_offset_t offset; 478 vm_offset_t start; 479 vm_offset_t end; 480 { 481 register vm_map_entry_t new_entry; 482 register vm_map_entry_t prev_entry; 483 vm_map_entry_t temp_entry; 484 485 /* 486 * Check that the start and end points are not bogus. 487 */ 488 489 if ((start < map->min_offset) || (end > map->max_offset) || 490 (start >= end)) 491 return(KERN_INVALID_ADDRESS); 492 493 /* 494 * Find the entry prior to the proposed 495 * starting address; if it's part of an 496 * existing entry, this range is bogus. 497 */ 498 499 if (vm_map_lookup_entry(map, start, &temp_entry)) 500 return(KERN_NO_SPACE); 501 502 prev_entry = temp_entry; 503 504 /* 505 * Assert that the next entry doesn't overlap the 506 * end point. 507 */ 508 509 if ((prev_entry->next != &map->header) && 510 (prev_entry->next->start < end)) 511 return(KERN_NO_SPACE); 512 513 /* 514 * See if we can avoid creating a new entry by 515 * extending one of our neighbors. 516 */ 517 518 if (object == NULL) { 519 if ((prev_entry != &map->header) && 520 (prev_entry->end == start) && 521 (map->is_main_map) && 522 (prev_entry->is_a_map == FALSE) && 523 (prev_entry->is_sub_map == FALSE) && 524 (prev_entry->inheritance == VM_INHERIT_DEFAULT) && 525 (prev_entry->protection == VM_PROT_DEFAULT) && 526 (prev_entry->max_protection == VM_PROT_DEFAULT) && 527 (prev_entry->wired_count == 0)) { 528 529 if (vm_object_coalesce(prev_entry->object.vm_object, 530 NULL, 531 prev_entry->offset, 532 (vm_offset_t) 0, 533 (vm_size_t)(prev_entry->end 534 - prev_entry->start), 535 (vm_size_t)(end - prev_entry->end))) { 536 /* 537 * Coalesced the two objects - can extend 538 * the previous map entry to include the 539 * new range. 540 */ 541 map->size += (end - prev_entry->end); 542 prev_entry->end = end; 543 return(KERN_SUCCESS); 544 } 545 } 546 } 547 548 /* 549 * Create a new entry 550 */ 551 552 new_entry = vm_map_entry_create(map); 553 new_entry->start = start; 554 new_entry->end = end; 555 556 new_entry->is_a_map = FALSE; 557 new_entry->is_sub_map = FALSE; 558 new_entry->object.vm_object = object; 559 new_entry->offset = offset; 560 561 new_entry->copy_on_write = FALSE; 562 new_entry->needs_copy = FALSE; 563 564 if (map->is_main_map) { 565 new_entry->inheritance = VM_INHERIT_DEFAULT; 566 new_entry->protection = VM_PROT_DEFAULT; 567 new_entry->max_protection = VM_PROT_DEFAULT; 568 new_entry->wired_count = 0; 569 } 570 571 /* 572 * Insert the new entry into the list 573 */ 574 575 vm_map_entry_link(map, prev_entry, new_entry); 576 map->size += new_entry->end - new_entry->start; 577 578 /* 579 * Update the free space hint 580 */ 581 582 if ((map->first_free == prev_entry) && (prev_entry->end >= new_entry->start)) 583 map->first_free = new_entry; 584 585 return(KERN_SUCCESS); 586 } 587 588 /* 589 * SAVE_HINT: 590 * 591 * Saves the specified entry as the hint for 592 * future lookups. Performs necessary interlocks. 593 */ 594 #define SAVE_HINT(map,value) \ 595 simple_lock(&(map)->hint_lock); \ 596 (map)->hint = (value); \ 597 simple_unlock(&(map)->hint_lock); 598 599 /* 600 * vm_map_lookup_entry: [ internal use only ] 601 * 602 * Finds the map entry containing (or 603 * immediately preceding) the specified address 604 * in the given map; the entry is returned 605 * in the "entry" parameter. The boolean 606 * result indicates whether the address is 607 * actually contained in the map. 608 */ 609 boolean_t vm_map_lookup_entry(map, address, entry) 610 register vm_map_t map; 611 register vm_offset_t address; 612 vm_map_entry_t *entry; /* OUT */ 613 { 614 register vm_map_entry_t cur; 615 register vm_map_entry_t last; 616 617 /* 618 * Start looking either from the head of the 619 * list, or from the hint. 620 */ 621 622 simple_lock(&map->hint_lock); 623 cur = map->hint; 624 simple_unlock(&map->hint_lock); 625 626 if (cur == &map->header) 627 cur = cur->next; 628 629 if (address >= cur->start) { 630 /* 631 * Go from hint to end of list. 632 * 633 * But first, make a quick check to see if 634 * we are already looking at the entry we 635 * want (which is usually the case). 636 * Note also that we don't need to save the hint 637 * here... it is the same hint (unless we are 638 * at the header, in which case the hint didn't 639 * buy us anything anyway). 640 */ 641 last = &map->header; 642 if ((cur != last) && (cur->end > address)) { 643 *entry = cur; 644 return(TRUE); 645 } 646 } 647 else { 648 /* 649 * Go from start to hint, *inclusively* 650 */ 651 last = cur->next; 652 cur = map->header.next; 653 } 654 655 /* 656 * Search linearly 657 */ 658 659 while (cur != last) { 660 if (cur->end > address) { 661 if (address >= cur->start) { 662 /* 663 * Save this lookup for future 664 * hints, and return 665 */ 666 667 *entry = cur; 668 SAVE_HINT(map, cur); 669 return(TRUE); 670 } 671 break; 672 } 673 cur = cur->next; 674 } 675 *entry = cur->prev; 676 SAVE_HINT(map, *entry); 677 return(FALSE); 678 } 679 680 /* 681 * Find sufficient space for `length' bytes in the given map, starting at 682 * `start'. The map must be locked. Returns 0 on success, 1 on no space. 683 */ 684 int 685 vm_map_findspace(map, start, length, addr) 686 register vm_map_t map; 687 register vm_offset_t start; 688 vm_size_t length; 689 vm_offset_t *addr; 690 { 691 register vm_map_entry_t entry, next; 692 register vm_offset_t end; 693 694 if (start < map->min_offset) 695 start = map->min_offset; 696 if (start > map->max_offset) 697 return (1); 698 699 /* 700 * Look for the first possible address; if there's already 701 * something at this address, we have to start after it. 702 */ 703 if (start == map->min_offset) { 704 if ((entry = map->first_free) != &map->header) 705 start = entry->end; 706 } else { 707 vm_map_entry_t tmp; 708 if (vm_map_lookup_entry(map, start, &tmp)) 709 start = tmp->end; 710 entry = tmp; 711 } 712 713 /* 714 * Look through the rest of the map, trying to fit a new region in 715 * the gap between existing regions, or after the very last region. 716 */ 717 for (;; start = (entry = next)->end) { 718 /* 719 * Find the end of the proposed new region. Be sure we didn't 720 * go beyond the end of the map, or wrap around the address; 721 * if so, we lose. Otherwise, if this is the last entry, or 722 * if the proposed new region fits before the next entry, we 723 * win. 724 */ 725 end = start + length; 726 if (end > map->max_offset || end < start) 727 return (1); 728 next = entry->next; 729 if (next == &map->header || next->start >= end) 730 break; 731 } 732 SAVE_HINT(map, entry); 733 *addr = start; 734 return (0); 735 } 736 737 /* 738 * vm_map_find finds an unallocated region in the target address 739 * map with the given length. The search is defined to be 740 * first-fit from the specified address; the region found is 741 * returned in the same parameter. 742 * 743 */ 744 int 745 vm_map_find(map, object, offset, addr, length, find_space) 746 vm_map_t map; 747 vm_object_t object; 748 vm_offset_t offset; 749 vm_offset_t *addr; /* IN/OUT */ 750 vm_size_t length; 751 boolean_t find_space; 752 { 753 register vm_offset_t start; 754 int result; 755 756 start = *addr; 757 vm_map_lock(map); 758 if (find_space) { 759 if (vm_map_findspace(map, start, length, addr)) { 760 vm_map_unlock(map); 761 return (KERN_NO_SPACE); 762 } 763 start = *addr; 764 } 765 result = vm_map_insert(map, object, offset, start, start + length); 766 vm_map_unlock(map); 767 return (result); 768 } 769 770 /* 771 * vm_map_simplify_entry: [ internal use only ] 772 * 773 * Simplify the given map entry by: 774 * removing extra sharing maps 775 * [XXX maybe later] merging with a neighbor 776 */ 777 void vm_map_simplify_entry(map, entry) 778 vm_map_t map; 779 vm_map_entry_t entry; 780 { 781 #ifdef lint 782 map++; 783 #endif 784 785 /* 786 * If this entry corresponds to a sharing map, then 787 * see if we can remove the level of indirection. 788 * If it's not a sharing map, then it points to 789 * a VM object, so see if we can merge with either 790 * of our neighbors. 791 */ 792 793 if (entry->is_sub_map) 794 return; 795 if (entry->is_a_map) { 796 #if 0 797 vm_map_t my_share_map; 798 int count; 799 800 my_share_map = entry->object.share_map; 801 simple_lock(&my_share_map->ref_lock); 802 count = my_share_map->ref_count; 803 simple_unlock(&my_share_map->ref_lock); 804 805 if (count == 1) { 806 /* Can move the region from 807 * entry->start to entry->end (+ entry->offset) 808 * in my_share_map into place of entry. 809 * Later. 810 */ 811 } 812 #endif 813 } 814 else { 815 /* 816 * Try to merge with our neighbors. 817 * 818 * Conditions for merge are: 819 * 820 * 1. entries are adjacent. 821 * 2. both entries point to objects 822 * with null pagers. 823 * 824 * If a merge is possible, we replace the two 825 * entries with a single entry, then merge 826 * the two objects into a single object. 827 * 828 * Now, all that is left to do is write the 829 * code! 830 */ 831 } 832 } 833 834 /* 835 * vm_map_clip_start: [ internal use only ] 836 * 837 * Asserts that the given entry begins at or after 838 * the specified address; if necessary, 839 * it splits the entry into two. 840 */ 841 #define vm_map_clip_start(map, entry, startaddr) \ 842 { \ 843 if (startaddr > entry->start) \ 844 _vm_map_clip_start(map, entry, startaddr); \ 845 } 846 847 /* 848 * This routine is called only when it is known that 849 * the entry must be split. 850 */ 851 static void _vm_map_clip_start(map, entry, start) 852 register vm_map_t map; 853 register vm_map_entry_t entry; 854 register vm_offset_t start; 855 { 856 register vm_map_entry_t new_entry; 857 858 /* 859 * See if we can simplify this entry first 860 */ 861 862 /* vm_map_simplify_entry(map, entry); */ 863 864 /* 865 * Split off the front portion -- 866 * note that we must insert the new 867 * entry BEFORE this one, so that 868 * this entry has the specified starting 869 * address. 870 */ 871 872 new_entry = vm_map_entry_create(map); 873 *new_entry = *entry; 874 875 new_entry->end = start; 876 entry->offset += (start - entry->start); 877 entry->start = start; 878 879 vm_map_entry_link(map, entry->prev, new_entry); 880 881 if (entry->is_a_map || entry->is_sub_map) 882 vm_map_reference(new_entry->object.share_map); 883 else 884 vm_object_reference(new_entry->object.vm_object); 885 } 886 887 /* 888 * vm_map_clip_end: [ internal use only ] 889 * 890 * Asserts that the given entry ends at or before 891 * the specified address; if necessary, 892 * it splits the entry into two. 893 */ 894 895 #define vm_map_clip_end(map, entry, endaddr) \ 896 { \ 897 if (endaddr < entry->end) \ 898 _vm_map_clip_end(map, entry, endaddr); \ 899 } 900 901 /* 902 * This routine is called only when it is known that 903 * the entry must be split. 904 */ 905 static void _vm_map_clip_end(map, entry, end) 906 register vm_map_t map; 907 register vm_map_entry_t entry; 908 register vm_offset_t end; 909 { 910 register vm_map_entry_t new_entry; 911 912 /* 913 * Create a new entry and insert it 914 * AFTER the specified entry 915 */ 916 917 new_entry = vm_map_entry_create(map); 918 *new_entry = *entry; 919 920 new_entry->start = entry->end = end; 921 new_entry->offset += (end - entry->start); 922 923 vm_map_entry_link(map, entry, new_entry); 924 925 if (entry->is_a_map || entry->is_sub_map) 926 vm_map_reference(new_entry->object.share_map); 927 else 928 vm_object_reference(new_entry->object.vm_object); 929 } 930 931 /* 932 * VM_MAP_RANGE_CHECK: [ internal use only ] 933 * 934 * Asserts that the starting and ending region 935 * addresses fall within the valid range of the map. 936 */ 937 #define VM_MAP_RANGE_CHECK(map, start, end) \ 938 { \ 939 if (start < vm_map_min(map)) \ 940 start = vm_map_min(map); \ 941 if (end > vm_map_max(map)) \ 942 end = vm_map_max(map); \ 943 if (start > end) \ 944 start = end; \ 945 } 946 947 /* 948 * vm_map_submap: [ kernel use only ] 949 * 950 * Mark the given range as handled by a subordinate map. 951 * 952 * This range must have been created with vm_map_find, 953 * and no other operations may have been performed on this 954 * range prior to calling vm_map_submap. 955 * 956 * Only a limited number of operations can be performed 957 * within this rage after calling vm_map_submap: 958 * vm_fault 959 * [Don't try vm_map_copy!] 960 * 961 * To remove a submapping, one must first remove the 962 * range from the superior map, and then destroy the 963 * submap (if desired). [Better yet, don't try it.] 964 */ 965 int 966 vm_map_submap(map, start, end, submap) 967 register vm_map_t map; 968 register vm_offset_t start; 969 register vm_offset_t end; 970 vm_map_t submap; 971 { 972 vm_map_entry_t entry; 973 register int result = KERN_INVALID_ARGUMENT; 974 975 vm_map_lock(map); 976 977 VM_MAP_RANGE_CHECK(map, start, end); 978 979 if (vm_map_lookup_entry(map, start, &entry)) { 980 vm_map_clip_start(map, entry, start); 981 } 982 else 983 entry = entry->next; 984 985 vm_map_clip_end(map, entry, end); 986 987 if ((entry->start == start) && (entry->end == end) && 988 (!entry->is_a_map) && 989 (entry->object.vm_object == NULL) && 990 (!entry->copy_on_write)) { 991 entry->is_a_map = FALSE; 992 entry->is_sub_map = TRUE; 993 vm_map_reference(entry->object.sub_map = submap); 994 result = KERN_SUCCESS; 995 } 996 vm_map_unlock(map); 997 998 return(result); 999 } 1000 1001 /* 1002 * vm_map_protect: 1003 * 1004 * Sets the protection of the specified address 1005 * region in the target map. If "set_max" is 1006 * specified, the maximum protection is to be set; 1007 * otherwise, only the current protection is affected. 1008 */ 1009 int 1010 vm_map_protect(map, start, end, new_prot, set_max) 1011 register vm_map_t map; 1012 register vm_offset_t start; 1013 register vm_offset_t end; 1014 register vm_prot_t new_prot; 1015 register boolean_t set_max; 1016 { 1017 register vm_map_entry_t current; 1018 vm_map_entry_t entry; 1019 1020 vm_map_lock(map); 1021 1022 VM_MAP_RANGE_CHECK(map, start, end); 1023 1024 if (vm_map_lookup_entry(map, start, &entry)) { 1025 vm_map_clip_start(map, entry, start); 1026 } 1027 else 1028 entry = entry->next; 1029 1030 /* 1031 * Make a first pass to check for protection 1032 * violations. 1033 */ 1034 1035 current = entry; 1036 while ((current != &map->header) && (current->start < end)) { 1037 if (current->is_sub_map) 1038 return(KERN_INVALID_ARGUMENT); 1039 if ((new_prot & current->max_protection) != new_prot) { 1040 vm_map_unlock(map); 1041 return(KERN_PROTECTION_FAILURE); 1042 } 1043 1044 current = current->next; 1045 } 1046 1047 /* 1048 * Go back and fix up protections. 1049 * [Note that clipping is not necessary the second time.] 1050 */ 1051 1052 current = entry; 1053 1054 while ((current != &map->header) && (current->start < end)) { 1055 vm_prot_t old_prot; 1056 1057 vm_map_clip_end(map, current, end); 1058 1059 old_prot = current->protection; 1060 if (set_max) 1061 current->protection = 1062 (current->max_protection = new_prot) & 1063 old_prot; 1064 else 1065 current->protection = new_prot; 1066 1067 /* 1068 * Update physical map if necessary. 1069 * Worry about copy-on-write here -- CHECK THIS XXX 1070 */ 1071 1072 if (current->protection != old_prot) { 1073 1074 #define MASK(entry) ((entry)->copy_on_write ? ~VM_PROT_WRITE : \ 1075 VM_PROT_ALL) 1076 #define max(a,b) ((a) > (b) ? (a) : (b)) 1077 1078 if (current->is_a_map) { 1079 vm_map_entry_t share_entry; 1080 vm_offset_t share_end; 1081 1082 vm_map_lock(current->object.share_map); 1083 (void) vm_map_lookup_entry( 1084 current->object.share_map, 1085 current->offset, 1086 &share_entry); 1087 share_end = current->offset + 1088 (current->end - current->start); 1089 while ((share_entry != 1090 ¤t->object.share_map->header) && 1091 (share_entry->start < share_end)) { 1092 1093 pmap_protect(map->pmap, 1094 (max(share_entry->start, 1095 current->offset) - 1096 current->offset + 1097 current->start), 1098 min(share_entry->end, 1099 share_end) - 1100 current->offset + 1101 current->start, 1102 current->protection & 1103 MASK(share_entry)); 1104 1105 share_entry = share_entry->next; 1106 } 1107 vm_map_unlock(current->object.share_map); 1108 } 1109 else 1110 pmap_protect(map->pmap, current->start, 1111 current->end, 1112 current->protection & MASK(entry)); 1113 #undef max 1114 #undef MASK 1115 } 1116 current = current->next; 1117 } 1118 1119 vm_map_unlock(map); 1120 return(KERN_SUCCESS); 1121 } 1122 1123 /* 1124 * vm_map_inherit: 1125 * 1126 * Sets the inheritance of the specified address 1127 * range in the target map. Inheritance 1128 * affects how the map will be shared with 1129 * child maps at the time of vm_map_fork. 1130 */ 1131 int 1132 vm_map_inherit(map, start, end, new_inheritance) 1133 register vm_map_t map; 1134 register vm_offset_t start; 1135 register vm_offset_t end; 1136 register vm_inherit_t new_inheritance; 1137 { 1138 register vm_map_entry_t entry; 1139 vm_map_entry_t temp_entry; 1140 1141 switch (new_inheritance) { 1142 case VM_INHERIT_NONE: 1143 case VM_INHERIT_COPY: 1144 case VM_INHERIT_SHARE: 1145 break; 1146 default: 1147 return(KERN_INVALID_ARGUMENT); 1148 } 1149 1150 vm_map_lock(map); 1151 1152 VM_MAP_RANGE_CHECK(map, start, end); 1153 1154 if (vm_map_lookup_entry(map, start, &temp_entry)) { 1155 entry = temp_entry; 1156 vm_map_clip_start(map, entry, start); 1157 } 1158 else 1159 entry = temp_entry->next; 1160 1161 while ((entry != &map->header) && (entry->start < end)) { 1162 vm_map_clip_end(map, entry, end); 1163 1164 entry->inheritance = new_inheritance; 1165 1166 entry = entry->next; 1167 } 1168 1169 vm_map_unlock(map); 1170 return(KERN_SUCCESS); 1171 } 1172 1173 /* 1174 * vm_map_pageable: 1175 * 1176 * Sets the pageability of the specified address 1177 * range in the target map. Regions specified 1178 * as not pageable require locked-down physical 1179 * memory and physical page maps. 1180 * 1181 * The map must not be locked, but a reference 1182 * must remain to the map throughout the call. 1183 */ 1184 int 1185 vm_map_pageable(map, start, end, new_pageable) 1186 register vm_map_t map; 1187 register vm_offset_t start; 1188 register vm_offset_t end; 1189 register boolean_t new_pageable; 1190 { 1191 register vm_map_entry_t entry; 1192 vm_map_entry_t start_entry; 1193 register vm_offset_t failed = 0; 1194 int rv; 1195 1196 vm_map_lock(map); 1197 1198 VM_MAP_RANGE_CHECK(map, start, end); 1199 1200 /* 1201 * Only one pageability change may take place at one 1202 * time, since vm_fault assumes it will be called 1203 * only once for each wiring/unwiring. Therefore, we 1204 * have to make sure we're actually changing the pageability 1205 * for the entire region. We do so before making any changes. 1206 */ 1207 1208 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1209 vm_map_unlock(map); 1210 return(KERN_INVALID_ADDRESS); 1211 } 1212 entry = start_entry; 1213 1214 /* 1215 * Actions are rather different for wiring and unwiring, 1216 * so we have two separate cases. 1217 */ 1218 1219 if (new_pageable) { 1220 1221 vm_map_clip_start(map, entry, start); 1222 1223 /* 1224 * Unwiring. First ensure that the range to be 1225 * unwired is really wired down and that there 1226 * are no holes. 1227 */ 1228 while ((entry != &map->header) && (entry->start < end)) { 1229 1230 if (entry->wired_count == 0 || 1231 (entry->end < end && 1232 (entry->next == &map->header || 1233 entry->next->start > entry->end))) { 1234 vm_map_unlock(map); 1235 return(KERN_INVALID_ARGUMENT); 1236 } 1237 entry = entry->next; 1238 } 1239 1240 /* 1241 * Now decrement the wiring count for each region. 1242 * If a region becomes completely unwired, 1243 * unwire its physical pages and mappings. 1244 */ 1245 lock_set_recursive(&map->lock); 1246 1247 entry = start_entry; 1248 while ((entry != &map->header) && (entry->start < end)) { 1249 vm_map_clip_end(map, entry, end); 1250 1251 entry->wired_count--; 1252 if (entry->wired_count == 0) 1253 vm_fault_unwire(map, entry->start, entry->end); 1254 1255 entry = entry->next; 1256 } 1257 lock_clear_recursive(&map->lock); 1258 } 1259 1260 else { 1261 /* 1262 * Wiring. We must do this in two passes: 1263 * 1264 * 1. Holding the write lock, we create any shadow 1265 * or zero-fill objects that need to be created. 1266 * Then we clip each map entry to the region to be 1267 * wired and increment its wiring count. We 1268 * create objects before clipping the map entries 1269 * to avoid object proliferation. 1270 * 1271 * 2. We downgrade to a read lock, and call 1272 * vm_fault_wire to fault in the pages for any 1273 * newly wired area (wired_count is 1). 1274 * 1275 * Downgrading to a read lock for vm_fault_wire avoids 1276 * a possible deadlock with another thread that may have 1277 * faulted on one of the pages to be wired (it would mark 1278 * the page busy, blocking us, then in turn block on the 1279 * map lock that we hold). Because of problems in the 1280 * recursive lock package, we cannot upgrade to a write 1281 * lock in vm_map_lookup. Thus, any actions that require 1282 * the write lock must be done beforehand. Because we 1283 * keep the read lock on the map, the copy-on-write status 1284 * of the entries we modify here cannot change. 1285 */ 1286 1287 /* 1288 * Pass 1. 1289 */ 1290 while ((entry != &map->header) && (entry->start < end)) { 1291 if (entry->wired_count == 0) { 1292 1293 /* 1294 * Perform actions of vm_map_lookup that need 1295 * the write lock on the map: create a shadow 1296 * object for a copy-on-write region, or an 1297 * object for a zero-fill region. 1298 * 1299 * We don't have to do this for entries that 1300 * point to sharing maps, because we won't hold 1301 * the lock on the sharing map. 1302 */ 1303 if (!entry->is_a_map) { 1304 if (entry->needs_copy && 1305 ((entry->protection & VM_PROT_WRITE) != 0)) { 1306 1307 vm_object_shadow(&entry->object.vm_object, 1308 &entry->offset, 1309 (vm_size_t)(entry->end 1310 - entry->start)); 1311 entry->needs_copy = FALSE; 1312 } 1313 else if (entry->object.vm_object == NULL) { 1314 entry->object.vm_object = 1315 vm_object_allocate((vm_size_t)(entry->end 1316 - entry->start)); 1317 entry->offset = (vm_offset_t)0; 1318 } 1319 } 1320 } 1321 vm_map_clip_start(map, entry, start); 1322 vm_map_clip_end(map, entry, end); 1323 entry->wired_count++; 1324 1325 /* 1326 * Check for holes 1327 */ 1328 if (entry->end < end && 1329 (entry->next == &map->header || 1330 entry->next->start > entry->end)) { 1331 /* 1332 * Found one. Object creation actions 1333 * do not need to be undone, but the 1334 * wired counts need to be restored. 1335 */ 1336 while (entry != &map->header && entry->end > start) { 1337 entry->wired_count--; 1338 entry = entry->prev; 1339 } 1340 vm_map_unlock(map); 1341 return(KERN_INVALID_ARGUMENT); 1342 } 1343 entry = entry->next; 1344 } 1345 1346 /* 1347 * Pass 2. 1348 */ 1349 1350 /* 1351 * HACK HACK HACK HACK 1352 * 1353 * If we are wiring in the kernel map or a submap of it, 1354 * unlock the map to avoid deadlocks. We trust that the 1355 * kernel threads are well-behaved, and therefore will 1356 * not do anything destructive to this region of the map 1357 * while we have it unlocked. We cannot trust user threads 1358 * to do the same. 1359 * 1360 * HACK HACK HACK HACK 1361 */ 1362 if (vm_map_pmap(map) == kernel_pmap) { 1363 vm_map_unlock(map); /* trust me ... */ 1364 } 1365 else { 1366 lock_set_recursive(&map->lock); 1367 lock_write_to_read(&map->lock); 1368 } 1369 1370 rv = 0; 1371 entry = start_entry; 1372 while (entry != &map->header && entry->start < end) { 1373 /* 1374 * If vm_fault_wire fails for any page we need to 1375 * undo what has been done. We decrement the wiring 1376 * count for those pages which have not yet been 1377 * wired (now) and unwire those that have (later). 1378 * 1379 * XXX this violates the locking protocol on the map, 1380 * needs to be fixed. 1381 */ 1382 if (rv) 1383 entry->wired_count--; 1384 else if (entry->wired_count == 1) { 1385 rv = vm_fault_wire(map, entry->start, entry->end); 1386 if (rv) { 1387 failed = entry->start; 1388 entry->wired_count--; 1389 } 1390 } 1391 entry = entry->next; 1392 } 1393 1394 if (vm_map_pmap(map) == kernel_pmap) { 1395 vm_map_lock(map); 1396 } 1397 else { 1398 lock_clear_recursive(&map->lock); 1399 } 1400 if (rv) { 1401 vm_map_unlock(map); 1402 (void) vm_map_pageable(map, start, failed, TRUE); 1403 return(rv); 1404 } 1405 } 1406 1407 vm_map_unlock(map); 1408 1409 return(KERN_SUCCESS); 1410 } 1411 1412 /* 1413 * vm_map_clean 1414 * 1415 * Push any dirty cached pages in the address range to their pager. 1416 * If syncio is TRUE, dirty pages are written synchronously. 1417 * If invalidate is TRUE, any cached pages are freed as well. 1418 * 1419 * Returns an error if any part of the specified range is not mapped. 1420 */ 1421 int 1422 vm_map_clean(map, start, end, syncio, invalidate) 1423 vm_map_t map; 1424 vm_offset_t start; 1425 vm_offset_t end; 1426 boolean_t syncio; 1427 boolean_t invalidate; 1428 { 1429 register vm_map_entry_t current; 1430 vm_map_entry_t entry; 1431 vm_size_t size; 1432 vm_object_t object; 1433 vm_offset_t offset; 1434 1435 vm_map_lock_read(map); 1436 VM_MAP_RANGE_CHECK(map, start, end); 1437 if (!vm_map_lookup_entry(map, start, &entry)) { 1438 vm_map_unlock_read(map); 1439 return(KERN_INVALID_ADDRESS); 1440 } 1441 1442 /* 1443 * Make a first pass to check for holes. 1444 */ 1445 for (current = entry; current->start < end; current = current->next) { 1446 if (current->is_sub_map) { 1447 vm_map_unlock_read(map); 1448 return(KERN_INVALID_ARGUMENT); 1449 } 1450 if (end > current->end && 1451 (current->next == &map->header || 1452 current->end != current->next->start)) { 1453 vm_map_unlock_read(map); 1454 return(KERN_INVALID_ADDRESS); 1455 } 1456 } 1457 1458 /* 1459 * Make a second pass, cleaning/uncaching pages from the indicated 1460 * objects as we go. 1461 */ 1462 for (current = entry; current->start < end; current = current->next) { 1463 offset = current->offset + (start - current->start); 1464 size = (end <= current->end ? end : current->end) - start; 1465 if (current->is_a_map) { 1466 register vm_map_t smap; 1467 vm_map_entry_t tentry; 1468 vm_size_t tsize; 1469 1470 smap = current->object.share_map; 1471 vm_map_lock_read(smap); 1472 (void) vm_map_lookup_entry(smap, offset, &tentry); 1473 tsize = tentry->end - offset; 1474 if (tsize < size) 1475 size = tsize; 1476 object = tentry->object.vm_object; 1477 offset = tentry->offset + (offset - tentry->start); 1478 vm_object_lock(object); 1479 vm_map_unlock_read(smap); 1480 } else { 1481 object = current->object.vm_object; 1482 vm_object_lock(object); 1483 } 1484 /* 1485 * Flush pages if writing is allowed. 1486 * XXX should we continue on an error? 1487 */ 1488 if ((current->protection & VM_PROT_WRITE) && 1489 !vm_object_page_clean(object, offset, offset+size, 1490 syncio, FALSE)) { 1491 vm_object_unlock(object); 1492 vm_map_unlock_read(map); 1493 return(KERN_FAILURE); 1494 } 1495 if (invalidate) 1496 vm_object_page_remove(object, offset, offset+size); 1497 vm_object_unlock(object); 1498 start += size; 1499 } 1500 1501 vm_map_unlock_read(map); 1502 return(KERN_SUCCESS); 1503 } 1504 1505 /* 1506 * vm_map_entry_unwire: [ internal use only ] 1507 * 1508 * Make the region specified by this entry pageable. 1509 * 1510 * The map in question should be locked. 1511 * [This is the reason for this routine's existence.] 1512 */ 1513 void vm_map_entry_unwire(map, entry) 1514 vm_map_t map; 1515 register vm_map_entry_t entry; 1516 { 1517 vm_fault_unwire(map, entry->start, entry->end); 1518 entry->wired_count = 0; 1519 } 1520 1521 /* 1522 * vm_map_entry_delete: [ internal use only ] 1523 * 1524 * Deallocate the given entry from the target map. 1525 */ 1526 void vm_map_entry_delete(map, entry) 1527 register vm_map_t map; 1528 register vm_map_entry_t entry; 1529 { 1530 if (entry->wired_count != 0) 1531 vm_map_entry_unwire(map, entry); 1532 1533 vm_map_entry_unlink(map, entry); 1534 map->size -= entry->end - entry->start; 1535 1536 if (entry->is_a_map || entry->is_sub_map) 1537 vm_map_deallocate(entry->object.share_map); 1538 else 1539 vm_object_deallocate(entry->object.vm_object); 1540 1541 vm_map_entry_dispose(map, entry); 1542 } 1543 1544 /* 1545 * vm_map_delete: [ internal use only ] 1546 * 1547 * Deallocates the given address range from the target 1548 * map. 1549 * 1550 * When called with a sharing map, removes pages from 1551 * that region from all physical maps. 1552 */ 1553 int 1554 vm_map_delete(map, start, end) 1555 register vm_map_t map; 1556 vm_offset_t start; 1557 register vm_offset_t end; 1558 { 1559 register vm_map_entry_t entry; 1560 vm_map_entry_t first_entry; 1561 1562 /* 1563 * Find the start of the region, and clip it 1564 */ 1565 1566 if (!vm_map_lookup_entry(map, start, &first_entry)) 1567 entry = first_entry->next; 1568 else { 1569 entry = first_entry; 1570 vm_map_clip_start(map, entry, start); 1571 1572 /* 1573 * Fix the lookup hint now, rather than each 1574 * time though the loop. 1575 */ 1576 1577 SAVE_HINT(map, entry->prev); 1578 } 1579 1580 /* 1581 * Save the free space hint 1582 */ 1583 1584 if (map->first_free->start >= start) 1585 map->first_free = entry->prev; 1586 1587 /* 1588 * Step through all entries in this region 1589 */ 1590 1591 while ((entry != &map->header) && (entry->start < end)) { 1592 vm_map_entry_t next; 1593 register vm_offset_t s, e; 1594 register vm_object_t object; 1595 1596 vm_map_clip_end(map, entry, end); 1597 1598 next = entry->next; 1599 s = entry->start; 1600 e = entry->end; 1601 1602 /* 1603 * Unwire before removing addresses from the pmap; 1604 * otherwise, unwiring will put the entries back in 1605 * the pmap. 1606 */ 1607 1608 object = entry->object.vm_object; 1609 if (entry->wired_count != 0) 1610 vm_map_entry_unwire(map, entry); 1611 1612 /* 1613 * If this is a sharing map, we must remove 1614 * *all* references to this data, since we can't 1615 * find all of the physical maps which are sharing 1616 * it. 1617 */ 1618 1619 if (object == kernel_object || object == kmem_object) 1620 vm_object_page_remove(object, entry->offset, 1621 entry->offset + (e - s)); 1622 else if (!map->is_main_map) 1623 vm_object_pmap_remove(object, 1624 entry->offset, 1625 entry->offset + (e - s)); 1626 else 1627 pmap_remove(map->pmap, s, e); 1628 1629 /* 1630 * Delete the entry (which may delete the object) 1631 * only after removing all pmap entries pointing 1632 * to its pages. (Otherwise, its page frames may 1633 * be reallocated, and any modify bits will be 1634 * set in the wrong object!) 1635 */ 1636 1637 vm_map_entry_delete(map, entry); 1638 entry = next; 1639 } 1640 return(KERN_SUCCESS); 1641 } 1642 1643 /* 1644 * vm_map_remove: 1645 * 1646 * Remove the given address range from the target map. 1647 * This is the exported form of vm_map_delete. 1648 */ 1649 int 1650 vm_map_remove(map, start, end) 1651 register vm_map_t map; 1652 register vm_offset_t start; 1653 register vm_offset_t end; 1654 { 1655 register int result; 1656 1657 vm_map_lock(map); 1658 VM_MAP_RANGE_CHECK(map, start, end); 1659 result = vm_map_delete(map, start, end); 1660 vm_map_unlock(map); 1661 1662 return(result); 1663 } 1664 1665 /* 1666 * vm_map_check_protection: 1667 * 1668 * Assert that the target map allows the specified 1669 * privilege on the entire address region given. 1670 * The entire region must be allocated. 1671 */ 1672 boolean_t vm_map_check_protection(map, start, end, protection) 1673 register vm_map_t map; 1674 register vm_offset_t start; 1675 register vm_offset_t end; 1676 register vm_prot_t protection; 1677 { 1678 register vm_map_entry_t entry; 1679 vm_map_entry_t tmp_entry; 1680 1681 if (!vm_map_lookup_entry(map, start, &tmp_entry)) { 1682 return(FALSE); 1683 } 1684 1685 entry = tmp_entry; 1686 1687 while (start < end) { 1688 if (entry == &map->header) { 1689 return(FALSE); 1690 } 1691 1692 /* 1693 * No holes allowed! 1694 */ 1695 1696 if (start < entry->start) { 1697 return(FALSE); 1698 } 1699 1700 /* 1701 * Check protection associated with entry. 1702 */ 1703 1704 if ((entry->protection & protection) != protection) { 1705 return(FALSE); 1706 } 1707 1708 /* go to next entry */ 1709 1710 start = entry->end; 1711 entry = entry->next; 1712 } 1713 return(TRUE); 1714 } 1715 1716 /* 1717 * vm_map_copy_entry: 1718 * 1719 * Copies the contents of the source entry to the destination 1720 * entry. The entries *must* be aligned properly. 1721 */ 1722 void vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry) 1723 vm_map_t src_map, dst_map; 1724 register vm_map_entry_t src_entry, dst_entry; 1725 { 1726 vm_object_t temp_object; 1727 1728 if (src_entry->is_sub_map || dst_entry->is_sub_map) 1729 return; 1730 1731 if (dst_entry->object.vm_object != NULL && 1732 (dst_entry->object.vm_object->flags & OBJ_INTERNAL) == 0) 1733 printf("vm_map_copy_entry: copying over permanent data!\n"); 1734 1735 /* 1736 * If our destination map was wired down, 1737 * unwire it now. 1738 */ 1739 1740 if (dst_entry->wired_count != 0) 1741 vm_map_entry_unwire(dst_map, dst_entry); 1742 1743 /* 1744 * If we're dealing with a sharing map, we 1745 * must remove the destination pages from 1746 * all maps (since we cannot know which maps 1747 * this sharing map belongs in). 1748 */ 1749 1750 if (dst_map->is_main_map) 1751 pmap_remove(dst_map->pmap, dst_entry->start, dst_entry->end); 1752 else 1753 vm_object_pmap_remove(dst_entry->object.vm_object, 1754 dst_entry->offset, 1755 dst_entry->offset + 1756 (dst_entry->end - dst_entry->start)); 1757 1758 if (src_entry->wired_count == 0) { 1759 1760 boolean_t src_needs_copy; 1761 1762 /* 1763 * If the source entry is marked needs_copy, 1764 * it is already write-protected. 1765 */ 1766 if (!src_entry->needs_copy) { 1767 1768 boolean_t su; 1769 1770 /* 1771 * If the source entry has only one mapping, 1772 * we can just protect the virtual address 1773 * range. 1774 */ 1775 if (!(su = src_map->is_main_map)) { 1776 simple_lock(&src_map->ref_lock); 1777 su = (src_map->ref_count == 1); 1778 simple_unlock(&src_map->ref_lock); 1779 } 1780 1781 if (su) { 1782 pmap_protect(src_map->pmap, 1783 src_entry->start, 1784 src_entry->end, 1785 src_entry->protection & ~VM_PROT_WRITE); 1786 } 1787 else { 1788 vm_object_pmap_copy(src_entry->object.vm_object, 1789 src_entry->offset, 1790 src_entry->offset + (src_entry->end 1791 -src_entry->start)); 1792 } 1793 } 1794 1795 /* 1796 * Make a copy of the object. 1797 */ 1798 temp_object = dst_entry->object.vm_object; 1799 vm_object_copy(src_entry->object.vm_object, 1800 src_entry->offset, 1801 (vm_size_t)(src_entry->end - 1802 src_entry->start), 1803 &dst_entry->object.vm_object, 1804 &dst_entry->offset, 1805 &src_needs_copy); 1806 /* 1807 * If we didn't get a copy-object now, mark the 1808 * source map entry so that a shadow will be created 1809 * to hold its changed pages. 1810 */ 1811 if (src_needs_copy) 1812 src_entry->needs_copy = TRUE; 1813 1814 /* 1815 * The destination always needs to have a shadow 1816 * created. 1817 */ 1818 dst_entry->needs_copy = TRUE; 1819 1820 /* 1821 * Mark the entries copy-on-write, so that write-enabling 1822 * the entry won't make copy-on-write pages writable. 1823 */ 1824 src_entry->copy_on_write = TRUE; 1825 dst_entry->copy_on_write = TRUE; 1826 /* 1827 * Get rid of the old object. 1828 */ 1829 vm_object_deallocate(temp_object); 1830 1831 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 1832 dst_entry->end - dst_entry->start, src_entry->start); 1833 } 1834 else { 1835 /* 1836 * Of course, wired down pages can't be set copy-on-write. 1837 * Cause wired pages to be copied into the new 1838 * map by simulating faults (the new pages are 1839 * pageable) 1840 */ 1841 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 1842 } 1843 } 1844 1845 /* 1846 * vm_map_copy: 1847 * 1848 * Perform a virtual memory copy from the source 1849 * address map/range to the destination map/range. 1850 * 1851 * If src_destroy or dst_alloc is requested, 1852 * the source and destination regions should be 1853 * disjoint, not only in the top-level map, but 1854 * in the sharing maps as well. [The best way 1855 * to guarantee this is to use a new intermediate 1856 * map to make copies. This also reduces map 1857 * fragmentation.] 1858 */ 1859 int 1860 vm_map_copy(dst_map, src_map, 1861 dst_addr, len, src_addr, 1862 dst_alloc, src_destroy) 1863 vm_map_t dst_map; 1864 vm_map_t src_map; 1865 vm_offset_t dst_addr; 1866 vm_size_t len; 1867 vm_offset_t src_addr; 1868 boolean_t dst_alloc; 1869 boolean_t src_destroy; 1870 { 1871 register 1872 vm_map_entry_t src_entry; 1873 register 1874 vm_map_entry_t dst_entry; 1875 vm_map_entry_t tmp_entry; 1876 vm_offset_t src_start; 1877 vm_offset_t src_end; 1878 vm_offset_t dst_start; 1879 vm_offset_t dst_end; 1880 vm_offset_t src_clip; 1881 vm_offset_t dst_clip; 1882 int result; 1883 boolean_t old_src_destroy; 1884 1885 /* 1886 * XXX While we figure out why src_destroy screws up, 1887 * we'll do it by explicitly vm_map_delete'ing at the end. 1888 */ 1889 1890 old_src_destroy = src_destroy; 1891 src_destroy = FALSE; 1892 1893 /* 1894 * Compute start and end of region in both maps 1895 */ 1896 1897 src_start = src_addr; 1898 src_end = src_start + len; 1899 dst_start = dst_addr; 1900 dst_end = dst_start + len; 1901 1902 /* 1903 * Check that the region can exist in both source 1904 * and destination. 1905 */ 1906 1907 if ((dst_end < dst_start) || (src_end < src_start)) 1908 return(KERN_NO_SPACE); 1909 1910 /* 1911 * Lock the maps in question -- we avoid deadlock 1912 * by ordering lock acquisition by map value 1913 */ 1914 1915 if (src_map == dst_map) { 1916 vm_map_lock(src_map); 1917 } 1918 else if ((int) src_map < (int) dst_map) { 1919 vm_map_lock(src_map); 1920 vm_map_lock(dst_map); 1921 } else { 1922 vm_map_lock(dst_map); 1923 vm_map_lock(src_map); 1924 } 1925 1926 result = KERN_SUCCESS; 1927 1928 /* 1929 * Check protections... source must be completely readable and 1930 * destination must be completely writable. [Note that if we're 1931 * allocating the destination region, we don't have to worry 1932 * about protection, but instead about whether the region 1933 * exists.] 1934 */ 1935 1936 if (src_map->is_main_map && dst_map->is_main_map) { 1937 if (!vm_map_check_protection(src_map, src_start, src_end, 1938 VM_PROT_READ)) { 1939 result = KERN_PROTECTION_FAILURE; 1940 goto Return; 1941 } 1942 1943 if (dst_alloc) { 1944 /* XXX Consider making this a vm_map_find instead */ 1945 if ((result = vm_map_insert(dst_map, NULL, 1946 (vm_offset_t) 0, dst_start, dst_end)) != KERN_SUCCESS) 1947 goto Return; 1948 } 1949 else if (!vm_map_check_protection(dst_map, dst_start, dst_end, 1950 VM_PROT_WRITE)) { 1951 result = KERN_PROTECTION_FAILURE; 1952 goto Return; 1953 } 1954 } 1955 1956 /* 1957 * Find the start entries and clip. 1958 * 1959 * Note that checking protection asserts that the 1960 * lookup cannot fail. 1961 * 1962 * Also note that we wait to do the second lookup 1963 * until we have done the first clip, as the clip 1964 * may affect which entry we get! 1965 */ 1966 1967 (void) vm_map_lookup_entry(src_map, src_addr, &tmp_entry); 1968 src_entry = tmp_entry; 1969 vm_map_clip_start(src_map, src_entry, src_start); 1970 1971 (void) vm_map_lookup_entry(dst_map, dst_addr, &tmp_entry); 1972 dst_entry = tmp_entry; 1973 vm_map_clip_start(dst_map, dst_entry, dst_start); 1974 1975 /* 1976 * If both source and destination entries are the same, 1977 * retry the first lookup, as it may have changed. 1978 */ 1979 1980 if (src_entry == dst_entry) { 1981 (void) vm_map_lookup_entry(src_map, src_addr, &tmp_entry); 1982 src_entry = tmp_entry; 1983 } 1984 1985 /* 1986 * If source and destination entries are still the same, 1987 * a null copy is being performed. 1988 */ 1989 1990 if (src_entry == dst_entry) 1991 goto Return; 1992 1993 /* 1994 * Go through entries until we get to the end of the 1995 * region. 1996 */ 1997 1998 while (src_start < src_end) { 1999 /* 2000 * Clip the entries to the endpoint of the entire region. 2001 */ 2002 2003 vm_map_clip_end(src_map, src_entry, src_end); 2004 vm_map_clip_end(dst_map, dst_entry, dst_end); 2005 2006 /* 2007 * Clip each entry to the endpoint of the other entry. 2008 */ 2009 2010 src_clip = src_entry->start + (dst_entry->end - dst_entry->start); 2011 vm_map_clip_end(src_map, src_entry, src_clip); 2012 2013 dst_clip = dst_entry->start + (src_entry->end - src_entry->start); 2014 vm_map_clip_end(dst_map, dst_entry, dst_clip); 2015 2016 /* 2017 * Both entries now match in size and relative endpoints. 2018 * 2019 * If both entries refer to a VM object, we can 2020 * deal with them now. 2021 */ 2022 2023 if (!src_entry->is_a_map && !dst_entry->is_a_map) { 2024 vm_map_copy_entry(src_map, dst_map, src_entry, 2025 dst_entry); 2026 } 2027 else { 2028 register vm_map_t new_dst_map; 2029 vm_offset_t new_dst_start; 2030 vm_size_t new_size; 2031 vm_map_t new_src_map; 2032 vm_offset_t new_src_start; 2033 2034 /* 2035 * We have to follow at least one sharing map. 2036 */ 2037 2038 new_size = (dst_entry->end - dst_entry->start); 2039 2040 if (src_entry->is_a_map) { 2041 new_src_map = src_entry->object.share_map; 2042 new_src_start = src_entry->offset; 2043 } 2044 else { 2045 new_src_map = src_map; 2046 new_src_start = src_entry->start; 2047 lock_set_recursive(&src_map->lock); 2048 } 2049 2050 if (dst_entry->is_a_map) { 2051 vm_offset_t new_dst_end; 2052 2053 new_dst_map = dst_entry->object.share_map; 2054 new_dst_start = dst_entry->offset; 2055 2056 /* 2057 * Since the destination sharing entries 2058 * will be merely deallocated, we can 2059 * do that now, and replace the region 2060 * with a null object. [This prevents 2061 * splitting the source map to match 2062 * the form of the destination map.] 2063 * Note that we can only do so if the 2064 * source and destination do not overlap. 2065 */ 2066 2067 new_dst_end = new_dst_start + new_size; 2068 2069 if (new_dst_map != new_src_map) { 2070 vm_map_lock(new_dst_map); 2071 (void) vm_map_delete(new_dst_map, 2072 new_dst_start, 2073 new_dst_end); 2074 (void) vm_map_insert(new_dst_map, 2075 NULL, 2076 (vm_offset_t) 0, 2077 new_dst_start, 2078 new_dst_end); 2079 vm_map_unlock(new_dst_map); 2080 } 2081 } 2082 else { 2083 new_dst_map = dst_map; 2084 new_dst_start = dst_entry->start; 2085 lock_set_recursive(&dst_map->lock); 2086 } 2087 2088 /* 2089 * Recursively copy the sharing map. 2090 */ 2091 2092 (void) vm_map_copy(new_dst_map, new_src_map, 2093 new_dst_start, new_size, new_src_start, 2094 FALSE, FALSE); 2095 2096 if (dst_map == new_dst_map) 2097 lock_clear_recursive(&dst_map->lock); 2098 if (src_map == new_src_map) 2099 lock_clear_recursive(&src_map->lock); 2100 } 2101 2102 /* 2103 * Update variables for next pass through the loop. 2104 */ 2105 2106 src_start = src_entry->end; 2107 src_entry = src_entry->next; 2108 dst_start = dst_entry->end; 2109 dst_entry = dst_entry->next; 2110 2111 /* 2112 * If the source is to be destroyed, here is the 2113 * place to do it. 2114 */ 2115 2116 if (src_destroy && src_map->is_main_map && 2117 dst_map->is_main_map) 2118 vm_map_entry_delete(src_map, src_entry->prev); 2119 } 2120 2121 /* 2122 * Update the physical maps as appropriate 2123 */ 2124 2125 if (src_map->is_main_map && dst_map->is_main_map) { 2126 if (src_destroy) 2127 pmap_remove(src_map->pmap, src_addr, src_addr + len); 2128 } 2129 2130 /* 2131 * Unlock the maps 2132 */ 2133 2134 Return: ; 2135 2136 if (old_src_destroy) 2137 vm_map_delete(src_map, src_addr, src_addr + len); 2138 2139 vm_map_unlock(src_map); 2140 if (src_map != dst_map) 2141 vm_map_unlock(dst_map); 2142 2143 return(result); 2144 } 2145 2146 /* 2147 * vmspace_fork: 2148 * Create a new process vmspace structure and vm_map 2149 * based on those of an existing process. The new map 2150 * is based on the old map, according to the inheritance 2151 * values on the regions in that map. 2152 * 2153 * The source map must not be locked. 2154 */ 2155 struct vmspace * 2156 vmspace_fork(vm1) 2157 register struct vmspace *vm1; 2158 { 2159 register struct vmspace *vm2; 2160 vm_map_t old_map = &vm1->vm_map; 2161 vm_map_t new_map; 2162 vm_map_entry_t old_entry; 2163 vm_map_entry_t new_entry; 2164 pmap_t new_pmap; 2165 2166 vm_map_lock(old_map); 2167 2168 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset, 2169 old_map->entries_pageable); 2170 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 2171 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy); 2172 new_pmap = &vm2->vm_pmap; /* XXX */ 2173 new_map = &vm2->vm_map; /* XXX */ 2174 2175 old_entry = old_map->header.next; 2176 2177 while (old_entry != &old_map->header) { 2178 if (old_entry->is_sub_map) 2179 panic("vm_map_fork: encountered a submap"); 2180 2181 switch (old_entry->inheritance) { 2182 case VM_INHERIT_NONE: 2183 break; 2184 2185 case VM_INHERIT_SHARE: 2186 /* 2187 * If we don't already have a sharing map: 2188 */ 2189 2190 if (!old_entry->is_a_map) { 2191 vm_map_t new_share_map; 2192 vm_map_entry_t new_share_entry; 2193 2194 /* 2195 * Create a new sharing map 2196 */ 2197 2198 new_share_map = vm_map_create(NULL, 2199 old_entry->start, 2200 old_entry->end, 2201 TRUE); 2202 new_share_map->is_main_map = FALSE; 2203 2204 /* 2205 * Create the only sharing entry from the 2206 * old task map entry. 2207 */ 2208 2209 new_share_entry = 2210 vm_map_entry_create(new_share_map); 2211 *new_share_entry = *old_entry; 2212 new_share_entry->wired_count = 0; 2213 2214 /* 2215 * Insert the entry into the new sharing 2216 * map 2217 */ 2218 2219 vm_map_entry_link(new_share_map, 2220 new_share_map->header.prev, 2221 new_share_entry); 2222 2223 /* 2224 * Fix up the task map entry to refer 2225 * to the sharing map now. 2226 */ 2227 2228 old_entry->is_a_map = TRUE; 2229 old_entry->object.share_map = new_share_map; 2230 old_entry->offset = old_entry->start; 2231 } 2232 2233 /* 2234 * Clone the entry, referencing the sharing map. 2235 */ 2236 2237 new_entry = vm_map_entry_create(new_map); 2238 *new_entry = *old_entry; 2239 new_entry->wired_count = 0; 2240 vm_map_reference(new_entry->object.share_map); 2241 2242 /* 2243 * Insert the entry into the new map -- we 2244 * know we're inserting at the end of the new 2245 * map. 2246 */ 2247 2248 vm_map_entry_link(new_map, new_map->header.prev, 2249 new_entry); 2250 2251 /* 2252 * Update the physical map 2253 */ 2254 2255 pmap_copy(new_map->pmap, old_map->pmap, 2256 new_entry->start, 2257 (old_entry->end - old_entry->start), 2258 old_entry->start); 2259 break; 2260 2261 case VM_INHERIT_COPY: 2262 /* 2263 * Clone the entry and link into the map. 2264 */ 2265 2266 new_entry = vm_map_entry_create(new_map); 2267 *new_entry = *old_entry; 2268 new_entry->wired_count = 0; 2269 new_entry->object.vm_object = NULL; 2270 new_entry->is_a_map = FALSE; 2271 vm_map_entry_link(new_map, new_map->header.prev, 2272 new_entry); 2273 if (old_entry->is_a_map) { 2274 int check; 2275 2276 check = vm_map_copy(new_map, 2277 old_entry->object.share_map, 2278 new_entry->start, 2279 (vm_size_t)(new_entry->end - 2280 new_entry->start), 2281 old_entry->offset, 2282 FALSE, FALSE); 2283 if (check != KERN_SUCCESS) 2284 printf("vm_map_fork: copy in share_map region failed\n"); 2285 } 2286 else { 2287 vm_map_copy_entry(old_map, new_map, old_entry, 2288 new_entry); 2289 } 2290 break; 2291 } 2292 old_entry = old_entry->next; 2293 } 2294 2295 new_map->size = old_map->size; 2296 vm_map_unlock(old_map); 2297 2298 return(vm2); 2299 } 2300 2301 /* 2302 * vm_map_lookup: 2303 * 2304 * Finds the VM object, offset, and 2305 * protection for a given virtual address in the 2306 * specified map, assuming a page fault of the 2307 * type specified. 2308 * 2309 * Leaves the map in question locked for read; return 2310 * values are guaranteed until a vm_map_lookup_done 2311 * call is performed. Note that the map argument 2312 * is in/out; the returned map must be used in 2313 * the call to vm_map_lookup_done. 2314 * 2315 * A handle (out_entry) is returned for use in 2316 * vm_map_lookup_done, to make that fast. 2317 * 2318 * If a lookup is requested with "write protection" 2319 * specified, the map may be changed to perform virtual 2320 * copying operations, although the data referenced will 2321 * remain the same. 2322 */ 2323 int 2324 vm_map_lookup(var_map, vaddr, fault_type, out_entry, 2325 object, offset, out_prot, wired, single_use) 2326 vm_map_t *var_map; /* IN/OUT */ 2327 register vm_offset_t vaddr; 2328 register vm_prot_t fault_type; 2329 2330 vm_map_entry_t *out_entry; /* OUT */ 2331 vm_object_t *object; /* OUT */ 2332 vm_offset_t *offset; /* OUT */ 2333 vm_prot_t *out_prot; /* OUT */ 2334 boolean_t *wired; /* OUT */ 2335 boolean_t *single_use; /* OUT */ 2336 { 2337 vm_map_t share_map; 2338 vm_offset_t share_offset; 2339 register vm_map_entry_t entry; 2340 register vm_map_t map = *var_map; 2341 register vm_prot_t prot; 2342 register boolean_t su; 2343 2344 RetryLookup: ; 2345 2346 /* 2347 * Lookup the faulting address. 2348 */ 2349 2350 vm_map_lock_read(map); 2351 2352 #define RETURN(why) \ 2353 { \ 2354 vm_map_unlock_read(map); \ 2355 return(why); \ 2356 } 2357 2358 /* 2359 * If the map has an interesting hint, try it before calling 2360 * full blown lookup routine. 2361 */ 2362 2363 simple_lock(&map->hint_lock); 2364 entry = map->hint; 2365 simple_unlock(&map->hint_lock); 2366 2367 *out_entry = entry; 2368 2369 if ((entry == &map->header) || 2370 (vaddr < entry->start) || (vaddr >= entry->end)) { 2371 vm_map_entry_t tmp_entry; 2372 2373 /* 2374 * Entry was either not a valid hint, or the vaddr 2375 * was not contained in the entry, so do a full lookup. 2376 */ 2377 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) 2378 RETURN(KERN_INVALID_ADDRESS); 2379 2380 entry = tmp_entry; 2381 *out_entry = entry; 2382 } 2383 2384 /* 2385 * Handle submaps. 2386 */ 2387 2388 if (entry->is_sub_map) { 2389 vm_map_t old_map = map; 2390 2391 *var_map = map = entry->object.sub_map; 2392 vm_map_unlock_read(old_map); 2393 goto RetryLookup; 2394 } 2395 2396 /* 2397 * Check whether this task is allowed to have 2398 * this page. 2399 */ 2400 2401 prot = entry->protection; 2402 if ((fault_type & (prot)) != fault_type) 2403 RETURN(KERN_PROTECTION_FAILURE); 2404 2405 /* 2406 * If this page is not pageable, we have to get 2407 * it for all possible accesses. 2408 */ 2409 2410 if (*wired = (entry->wired_count != 0)) 2411 prot = fault_type = entry->protection; 2412 2413 /* 2414 * If we don't already have a VM object, track 2415 * it down. 2416 */ 2417 2418 if (su = !entry->is_a_map) { 2419 share_map = map; 2420 share_offset = vaddr; 2421 } 2422 else { 2423 vm_map_entry_t share_entry; 2424 2425 /* 2426 * Compute the sharing map, and offset into it. 2427 */ 2428 2429 share_map = entry->object.share_map; 2430 share_offset = (vaddr - entry->start) + entry->offset; 2431 2432 /* 2433 * Look for the backing store object and offset 2434 */ 2435 2436 vm_map_lock_read(share_map); 2437 2438 if (!vm_map_lookup_entry(share_map, share_offset, 2439 &share_entry)) { 2440 vm_map_unlock_read(share_map); 2441 RETURN(KERN_INVALID_ADDRESS); 2442 } 2443 entry = share_entry; 2444 } 2445 2446 /* 2447 * If the entry was copy-on-write, we either ... 2448 */ 2449 2450 if (entry->needs_copy) { 2451 /* 2452 * If we want to write the page, we may as well 2453 * handle that now since we've got the sharing 2454 * map locked. 2455 * 2456 * If we don't need to write the page, we just 2457 * demote the permissions allowed. 2458 */ 2459 2460 if (fault_type & VM_PROT_WRITE) { 2461 /* 2462 * Make a new object, and place it in the 2463 * object chain. Note that no new references 2464 * have appeared -- one just moved from the 2465 * share map to the new object. 2466 */ 2467 2468 if (lock_read_to_write(&share_map->lock)) { 2469 if (share_map != map) 2470 vm_map_unlock_read(map); 2471 goto RetryLookup; 2472 } 2473 2474 vm_object_shadow( 2475 &entry->object.vm_object, 2476 &entry->offset, 2477 (vm_size_t) (entry->end - entry->start)); 2478 2479 entry->needs_copy = FALSE; 2480 2481 lock_write_to_read(&share_map->lock); 2482 } 2483 else { 2484 /* 2485 * We're attempting to read a copy-on-write 2486 * page -- don't allow writes. 2487 */ 2488 2489 prot &= (~VM_PROT_WRITE); 2490 } 2491 } 2492 2493 /* 2494 * Create an object if necessary. 2495 */ 2496 if (entry->object.vm_object == NULL) { 2497 2498 if (lock_read_to_write(&share_map->lock)) { 2499 if (share_map != map) 2500 vm_map_unlock_read(map); 2501 goto RetryLookup; 2502 } 2503 2504 entry->object.vm_object = vm_object_allocate( 2505 (vm_size_t)(entry->end - entry->start)); 2506 entry->offset = 0; 2507 lock_write_to_read(&share_map->lock); 2508 } 2509 2510 /* 2511 * Return the object/offset from this entry. If the entry 2512 * was copy-on-write or empty, it has been fixed up. 2513 */ 2514 2515 *offset = (share_offset - entry->start) + entry->offset; 2516 *object = entry->object.vm_object; 2517 2518 /* 2519 * Return whether this is the only map sharing this data. 2520 */ 2521 2522 if (!su) { 2523 simple_lock(&share_map->ref_lock); 2524 su = (share_map->ref_count == 1); 2525 simple_unlock(&share_map->ref_lock); 2526 } 2527 2528 *out_prot = prot; 2529 *single_use = su; 2530 2531 return(KERN_SUCCESS); 2532 2533 #undef RETURN 2534 } 2535 2536 /* 2537 * vm_map_lookup_done: 2538 * 2539 * Releases locks acquired by a vm_map_lookup 2540 * (according to the handle returned by that lookup). 2541 */ 2542 2543 void vm_map_lookup_done(map, entry) 2544 register vm_map_t map; 2545 vm_map_entry_t entry; 2546 { 2547 /* 2548 * If this entry references a map, unlock it first. 2549 */ 2550 2551 if (entry->is_a_map) 2552 vm_map_unlock_read(entry->object.share_map); 2553 2554 /* 2555 * Unlock the main-level map 2556 */ 2557 2558 vm_map_unlock_read(map); 2559 } 2560 2561 /* 2562 * Routine: vm_map_simplify 2563 * Purpose: 2564 * Attempt to simplify the map representation in 2565 * the vicinity of the given starting address. 2566 * Note: 2567 * This routine is intended primarily to keep the 2568 * kernel maps more compact -- they generally don't 2569 * benefit from the "expand a map entry" technology 2570 * at allocation time because the adjacent entry 2571 * is often wired down. 2572 */ 2573 void vm_map_simplify(map, start) 2574 vm_map_t map; 2575 vm_offset_t start; 2576 { 2577 vm_map_entry_t this_entry; 2578 vm_map_entry_t prev_entry; 2579 2580 vm_map_lock(map); 2581 if ( 2582 (vm_map_lookup_entry(map, start, &this_entry)) && 2583 ((prev_entry = this_entry->prev) != &map->header) && 2584 2585 (prev_entry->end == start) && 2586 (map->is_main_map) && 2587 2588 (prev_entry->is_a_map == FALSE) && 2589 (prev_entry->is_sub_map == FALSE) && 2590 2591 (this_entry->is_a_map == FALSE) && 2592 (this_entry->is_sub_map == FALSE) && 2593 2594 (prev_entry->inheritance == this_entry->inheritance) && 2595 (prev_entry->protection == this_entry->protection) && 2596 (prev_entry->max_protection == this_entry->max_protection) && 2597 (prev_entry->wired_count == this_entry->wired_count) && 2598 2599 (prev_entry->copy_on_write == this_entry->copy_on_write) && 2600 (prev_entry->needs_copy == this_entry->needs_copy) && 2601 2602 (prev_entry->object.vm_object == this_entry->object.vm_object) && 2603 ((prev_entry->offset + (prev_entry->end - prev_entry->start)) 2604 == this_entry->offset) 2605 ) { 2606 if (map->first_free == this_entry) 2607 map->first_free = prev_entry; 2608 2609 if (!this_entry->object.vm_object->paging_in_progress) { 2610 SAVE_HINT(map, prev_entry); 2611 vm_map_entry_unlink(map, this_entry); 2612 prev_entry->end = this_entry->end; 2613 vm_object_deallocate(this_entry->object.vm_object); 2614 vm_map_entry_dispose(map, this_entry); 2615 } 2616 } 2617 vm_map_unlock(map); 2618 } 2619 2620 /* 2621 * vm_map_print: [ debug ] 2622 */ 2623 void vm_map_print(map, full) 2624 register vm_map_t map; 2625 boolean_t full; 2626 { 2627 register vm_map_entry_t entry; 2628 extern int indent; 2629 2630 iprintf("%s map 0x%x: pmap=0x%x,ref=%d,nentries=%d,version=%d\n", 2631 (map->is_main_map ? "Task" : "Share"), 2632 (int) map, (int) (map->pmap), map->ref_count, map->nentries, 2633 map->timestamp); 2634 2635 if (!full && indent) 2636 return; 2637 2638 indent += 2; 2639 for (entry = map->header.next; entry != &map->header; 2640 entry = entry->next) { 2641 iprintf("map entry 0x%x: start=0x%x, end=0x%x, ", 2642 (int) entry, (int) entry->start, (int) entry->end); 2643 if (map->is_main_map) { 2644 static char *inheritance_name[4] = 2645 { "share", "copy", "none", "donate_copy"}; 2646 printf("prot=%x/%x/%s, ", 2647 entry->protection, 2648 entry->max_protection, 2649 inheritance_name[entry->inheritance]); 2650 if (entry->wired_count != 0) 2651 printf("wired, "); 2652 } 2653 2654 if (entry->is_a_map || entry->is_sub_map) { 2655 printf("share=0x%x, offset=0x%x\n", 2656 (int) entry->object.share_map, 2657 (int) entry->offset); 2658 if ((entry->prev == &map->header) || 2659 (!entry->prev->is_a_map) || 2660 (entry->prev->object.share_map != 2661 entry->object.share_map)) { 2662 indent += 2; 2663 vm_map_print(entry->object.share_map, full); 2664 indent -= 2; 2665 } 2666 2667 } 2668 else { 2669 printf("object=0x%x, offset=0x%x", 2670 (int) entry->object.vm_object, 2671 (int) entry->offset); 2672 if (entry->copy_on_write) 2673 printf(", copy (%s)", 2674 entry->needs_copy ? "needed" : "done"); 2675 printf("\n"); 2676 2677 if ((entry->prev == &map->header) || 2678 (entry->prev->is_a_map) || 2679 (entry->prev->object.vm_object != 2680 entry->object.vm_object)) { 2681 indent += 2; 2682 vm_object_print(entry->object.vm_object, full); 2683 indent -= 2; 2684 } 2685 } 2686 } 2687 indent -= 2; 2688 } 2689