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