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