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