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