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 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1131 continue; 1132 1133 pindex = OFF_TO_IDX(current->offset); 1134 count = atop(current->end - current->start); 1135 1136 if (current->start < start) { 1137 pindex += atop(start - current->start); 1138 count -= atop(start - current->start); 1139 } 1140 if (current->end > end) 1141 count -= atop(current->end - end); 1142 1143 if (count <= 0) 1144 continue; 1145 1146 vm_object_madvise(current->object.vm_object, 1147 pindex, count, behav); 1148 if (behav == MADV_WILLNEED) { 1149 pmap_object_init_pt( 1150 map->pmap, 1151 current->start, 1152 current->object.vm_object, 1153 pindex, 1154 (count << PAGE_SHIFT), 1155 0 1156 ); 1157 } 1158 } 1159 vm_map_unlock_read(map); 1160 } 1161 return(0); 1162 } 1163 1164 1165 /* 1166 * vm_map_inherit: 1167 * 1168 * Sets the inheritance of the specified address 1169 * range in the target map. Inheritance 1170 * affects how the map will be shared with 1171 * child maps at the time of vm_map_fork. 1172 */ 1173 int 1174 vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1175 vm_inherit_t new_inheritance) 1176 { 1177 vm_map_entry_t entry; 1178 vm_map_entry_t temp_entry; 1179 1180 switch (new_inheritance) { 1181 case VM_INHERIT_NONE: 1182 case VM_INHERIT_COPY: 1183 case VM_INHERIT_SHARE: 1184 break; 1185 default: 1186 return (KERN_INVALID_ARGUMENT); 1187 } 1188 1189 vm_map_lock(map); 1190 1191 VM_MAP_RANGE_CHECK(map, start, end); 1192 1193 if (vm_map_lookup_entry(map, start, &temp_entry)) { 1194 entry = temp_entry; 1195 vm_map_clip_start(map, entry, start); 1196 } else 1197 entry = temp_entry->next; 1198 1199 while ((entry != &map->header) && (entry->start < end)) { 1200 vm_map_clip_end(map, entry, end); 1201 1202 entry->inheritance = new_inheritance; 1203 1204 vm_map_simplify_entry(map, entry); 1205 1206 entry = entry->next; 1207 } 1208 1209 vm_map_unlock(map); 1210 return (KERN_SUCCESS); 1211 } 1212 1213 /* 1214 * Implement the semantics of mlock 1215 */ 1216 int 1217 vm_map_user_pageable(map, start, end, new_pageable) 1218 vm_map_t map; 1219 vm_offset_t start; 1220 vm_offset_t end; 1221 boolean_t new_pageable; 1222 { 1223 vm_map_entry_t entry; 1224 vm_map_entry_t start_entry; 1225 vm_offset_t estart; 1226 int rv; 1227 1228 vm_map_lock(map); 1229 VM_MAP_RANGE_CHECK(map, start, end); 1230 1231 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1232 vm_map_unlock(map); 1233 return (KERN_INVALID_ADDRESS); 1234 } 1235 1236 if (new_pageable) { 1237 1238 entry = start_entry; 1239 vm_map_clip_start(map, entry, start); 1240 1241 /* 1242 * Now decrement the wiring count for each region. If a region 1243 * becomes completely unwired, unwire its physical pages and 1244 * mappings. 1245 */ 1246 while ((entry != &map->header) && (entry->start < end)) { 1247 if (entry->eflags & MAP_ENTRY_USER_WIRED) { 1248 vm_map_clip_end(map, entry, end); 1249 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1250 entry->wired_count--; 1251 if (entry->wired_count == 0) 1252 vm_fault_unwire(map, entry->start, entry->end); 1253 } 1254 vm_map_simplify_entry(map,entry); 1255 entry = entry->next; 1256 } 1257 } else { 1258 1259 entry = start_entry; 1260 1261 while ((entry != &map->header) && (entry->start < end)) { 1262 1263 if (entry->eflags & MAP_ENTRY_USER_WIRED) { 1264 entry = entry->next; 1265 continue; 1266 } 1267 1268 if (entry->wired_count != 0) { 1269 entry->wired_count++; 1270 entry->eflags |= MAP_ENTRY_USER_WIRED; 1271 entry = entry->next; 1272 continue; 1273 } 1274 1275 /* Here on entry being newly wired */ 1276 1277 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1278 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1279 if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) { 1280 1281 vm_object_shadow(&entry->object.vm_object, 1282 &entry->offset, 1283 atop(entry->end - entry->start)); 1284 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 1285 1286 } else if (entry->object.vm_object == NULL) { 1287 1288 entry->object.vm_object = 1289 vm_object_allocate(OBJT_DEFAULT, 1290 atop(entry->end - entry->start)); 1291 entry->offset = (vm_offset_t) 0; 1292 1293 } 1294 } 1295 1296 vm_map_clip_start(map, entry, start); 1297 vm_map_clip_end(map, entry, end); 1298 1299 entry->wired_count++; 1300 entry->eflags |= MAP_ENTRY_USER_WIRED; 1301 estart = entry->start; 1302 1303 /* First we need to allow map modifications */ 1304 vm_map_set_recursive(map); 1305 vm_map_lock_downgrade(map); 1306 map->timestamp++; 1307 1308 rv = vm_fault_user_wire(map, entry->start, entry->end); 1309 if (rv) { 1310 1311 entry->wired_count--; 1312 entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1313 1314 vm_map_clear_recursive(map); 1315 vm_map_unlock(map); 1316 1317 (void) vm_map_user_pageable(map, start, entry->start, TRUE); 1318 return rv; 1319 } 1320 1321 vm_map_clear_recursive(map); 1322 if (vm_map_lock_upgrade(map)) { 1323 vm_map_lock(map); 1324 if (vm_map_lookup_entry(map, estart, &entry) 1325 == FALSE) { 1326 vm_map_unlock(map); 1327 (void) vm_map_user_pageable(map, 1328 start, 1329 estart, 1330 TRUE); 1331 return (KERN_INVALID_ADDRESS); 1332 } 1333 } 1334 vm_map_simplify_entry(map,entry); 1335 } 1336 } 1337 map->timestamp++; 1338 vm_map_unlock(map); 1339 return KERN_SUCCESS; 1340 } 1341 1342 /* 1343 * vm_map_pageable: 1344 * 1345 * Sets the pageability of the specified address 1346 * range in the target map. Regions specified 1347 * as not pageable require locked-down physical 1348 * memory and physical page maps. 1349 * 1350 * The map must not be locked, but a reference 1351 * must remain to the map throughout the call. 1352 */ 1353 int 1354 vm_map_pageable(map, start, end, new_pageable) 1355 vm_map_t map; 1356 vm_offset_t start; 1357 vm_offset_t end; 1358 boolean_t new_pageable; 1359 { 1360 vm_map_entry_t entry; 1361 vm_map_entry_t start_entry; 1362 vm_offset_t failed = 0; 1363 int rv; 1364 1365 vm_map_lock(map); 1366 1367 VM_MAP_RANGE_CHECK(map, start, end); 1368 1369 /* 1370 * Only one pageability change may take place at one time, since 1371 * vm_fault assumes it will be called only once for each 1372 * wiring/unwiring. Therefore, we have to make sure we're actually 1373 * changing the pageability for the entire region. We do so before 1374 * making any changes. 1375 */ 1376 1377 if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1378 vm_map_unlock(map); 1379 return (KERN_INVALID_ADDRESS); 1380 } 1381 entry = start_entry; 1382 1383 /* 1384 * Actions are rather different for wiring and unwiring, so we have 1385 * two separate cases. 1386 */ 1387 1388 if (new_pageable) { 1389 1390 vm_map_clip_start(map, entry, start); 1391 1392 /* 1393 * Unwiring. First ensure that the range to be unwired is 1394 * really wired down and that there are no holes. 1395 */ 1396 while ((entry != &map->header) && (entry->start < end)) { 1397 1398 if (entry->wired_count == 0 || 1399 (entry->end < end && 1400 (entry->next == &map->header || 1401 entry->next->start > entry->end))) { 1402 vm_map_unlock(map); 1403 return (KERN_INVALID_ARGUMENT); 1404 } 1405 entry = entry->next; 1406 } 1407 1408 /* 1409 * Now decrement the wiring count for each region. If a region 1410 * becomes completely unwired, unwire its physical pages and 1411 * mappings. 1412 */ 1413 entry = start_entry; 1414 while ((entry != &map->header) && (entry->start < end)) { 1415 vm_map_clip_end(map, entry, end); 1416 1417 entry->wired_count--; 1418 if (entry->wired_count == 0) 1419 vm_fault_unwire(map, entry->start, entry->end); 1420 1421 vm_map_simplify_entry(map, entry); 1422 1423 entry = entry->next; 1424 } 1425 } else { 1426 /* 1427 * Wiring. We must do this in two passes: 1428 * 1429 * 1. Holding the write lock, we create any shadow or zero-fill 1430 * objects that need to be created. Then we clip each map 1431 * entry to the region to be wired and increment its wiring 1432 * count. We create objects before clipping the map entries 1433 * to avoid object proliferation. 1434 * 1435 * 2. We downgrade to a read lock, and call vm_fault_wire to 1436 * fault in the pages for any newly wired area (wired_count is 1437 * 1). 1438 * 1439 * Downgrading to a read lock for vm_fault_wire avoids a possible 1440 * deadlock with another process that may have faulted on one 1441 * of the pages to be wired (it would mark the page busy, 1442 * blocking us, then in turn block on the map lock that we 1443 * hold). Because of problems in the recursive lock package, 1444 * we cannot upgrade to a write lock in vm_map_lookup. Thus, 1445 * any actions that require the write lock must be done 1446 * beforehand. Because we keep the read lock on the map, the 1447 * copy-on-write status of the entries we modify here cannot 1448 * change. 1449 */ 1450 1451 /* 1452 * Pass 1. 1453 */ 1454 while ((entry != &map->header) && (entry->start < end)) { 1455 if (entry->wired_count == 0) { 1456 1457 /* 1458 * Perform actions of vm_map_lookup that need 1459 * the write lock on the map: create a shadow 1460 * object for a copy-on-write region, or an 1461 * object for a zero-fill region. 1462 * 1463 * We don't have to do this for entries that 1464 * point to sub maps, because we won't 1465 * hold the lock on the sub map. 1466 */ 1467 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1468 int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1469 if (copyflag && 1470 ((entry->protection & VM_PROT_WRITE) != 0)) { 1471 1472 vm_object_shadow(&entry->object.vm_object, 1473 &entry->offset, 1474 atop(entry->end - entry->start)); 1475 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 1476 } else if (entry->object.vm_object == NULL) { 1477 entry->object.vm_object = 1478 vm_object_allocate(OBJT_DEFAULT, 1479 atop(entry->end - entry->start)); 1480 entry->offset = (vm_offset_t) 0; 1481 } 1482 } 1483 } 1484 vm_map_clip_start(map, entry, start); 1485 vm_map_clip_end(map, entry, end); 1486 entry->wired_count++; 1487 1488 /* 1489 * Check for holes 1490 */ 1491 if (entry->end < end && 1492 (entry->next == &map->header || 1493 entry->next->start > entry->end)) { 1494 /* 1495 * Found one. Object creation actions do not 1496 * need to be undone, but the wired counts 1497 * need to be restored. 1498 */ 1499 while (entry != &map->header && entry->end > start) { 1500 entry->wired_count--; 1501 entry = entry->prev; 1502 } 1503 vm_map_unlock(map); 1504 return (KERN_INVALID_ARGUMENT); 1505 } 1506 entry = entry->next; 1507 } 1508 1509 /* 1510 * Pass 2. 1511 */ 1512 1513 /* 1514 * HACK HACK HACK HACK 1515 * 1516 * If we are wiring in the kernel map or a submap of it, 1517 * unlock the map to avoid deadlocks. We trust that the 1518 * kernel is well-behaved, and therefore will not do 1519 * anything destructive to this region of the map while 1520 * we have it unlocked. We cannot trust user processes 1521 * to do the same. 1522 * 1523 * HACK HACK HACK HACK 1524 */ 1525 if (vm_map_pmap(map) == kernel_pmap) { 1526 vm_map_unlock(map); /* trust me ... */ 1527 } else { 1528 vm_map_lock_downgrade(map); 1529 } 1530 1531 rv = 0; 1532 entry = start_entry; 1533 while (entry != &map->header && entry->start < end) { 1534 /* 1535 * If vm_fault_wire fails for any page we need to undo 1536 * what has been done. We decrement the wiring count 1537 * for those pages which have not yet been wired (now) 1538 * and unwire those that have (later). 1539 * 1540 * XXX this violates the locking protocol on the map, 1541 * needs to be fixed. 1542 */ 1543 if (rv) 1544 entry->wired_count--; 1545 else if (entry->wired_count == 1) { 1546 rv = vm_fault_wire(map, entry->start, entry->end); 1547 if (rv) { 1548 failed = entry->start; 1549 entry->wired_count--; 1550 } 1551 } 1552 entry = entry->next; 1553 } 1554 1555 if (vm_map_pmap(map) == kernel_pmap) { 1556 vm_map_lock(map); 1557 } 1558 if (rv) { 1559 vm_map_unlock(map); 1560 (void) vm_map_pageable(map, start, failed, TRUE); 1561 return (rv); 1562 } 1563 vm_map_simplify_entry(map, start_entry); 1564 } 1565 1566 vm_map_unlock(map); 1567 1568 return (KERN_SUCCESS); 1569 } 1570 1571 /* 1572 * vm_map_clean 1573 * 1574 * Push any dirty cached pages in the address range to their pager. 1575 * If syncio is TRUE, dirty pages are written synchronously. 1576 * If invalidate is TRUE, any cached pages are freed as well. 1577 * 1578 * Returns an error if any part of the specified range is not mapped. 1579 */ 1580 int 1581 vm_map_clean(map, start, end, syncio, invalidate) 1582 vm_map_t map; 1583 vm_offset_t start; 1584 vm_offset_t end; 1585 boolean_t syncio; 1586 boolean_t invalidate; 1587 { 1588 vm_map_entry_t current; 1589 vm_map_entry_t entry; 1590 vm_size_t size; 1591 vm_object_t object; 1592 vm_ooffset_t offset; 1593 1594 vm_map_lock_read(map); 1595 VM_MAP_RANGE_CHECK(map, start, end); 1596 if (!vm_map_lookup_entry(map, start, &entry)) { 1597 vm_map_unlock_read(map); 1598 return (KERN_INVALID_ADDRESS); 1599 } 1600 /* 1601 * Make a first pass to check for holes. 1602 */ 1603 for (current = entry; current->start < end; current = current->next) { 1604 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1605 vm_map_unlock_read(map); 1606 return (KERN_INVALID_ARGUMENT); 1607 } 1608 if (end > current->end && 1609 (current->next == &map->header || 1610 current->end != current->next->start)) { 1611 vm_map_unlock_read(map); 1612 return (KERN_INVALID_ADDRESS); 1613 } 1614 } 1615 1616 if (invalidate) 1617 pmap_remove(vm_map_pmap(map), start, end); 1618 /* 1619 * Make a second pass, cleaning/uncaching pages from the indicated 1620 * objects as we go. 1621 */ 1622 for (current = entry; current->start < end; current = current->next) { 1623 offset = current->offset + (start - current->start); 1624 size = (end <= current->end ? end : current->end) - start; 1625 if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1626 vm_map_t smap; 1627 vm_map_entry_t tentry; 1628 vm_size_t tsize; 1629 1630 smap = current->object.sub_map; 1631 vm_map_lock_read(smap); 1632 (void) vm_map_lookup_entry(smap, offset, &tentry); 1633 tsize = tentry->end - offset; 1634 if (tsize < size) 1635 size = tsize; 1636 object = tentry->object.vm_object; 1637 offset = tentry->offset + (offset - tentry->start); 1638 vm_map_unlock_read(smap); 1639 } else { 1640 object = current->object.vm_object; 1641 } 1642 /* 1643 * Note that there is absolutely no sense in writing out 1644 * anonymous objects, so we track down the vnode object 1645 * to write out. 1646 * We invalidate (remove) all pages from the address space 1647 * anyway, for semantic correctness. 1648 */ 1649 while (object->backing_object) { 1650 object = object->backing_object; 1651 offset += object->backing_object_offset; 1652 if (object->size < OFF_TO_IDX( offset + size)) 1653 size = IDX_TO_OFF(object->size) - offset; 1654 } 1655 if (object && (object->type == OBJT_VNODE) && 1656 (current->protection & VM_PROT_WRITE)) { 1657 /* 1658 * Flush pages if writing is allowed, invalidate them 1659 * if invalidation requested. Pages undergoing I/O 1660 * will be ignored by vm_object_page_remove(). 1661 * 1662 * We cannot lock the vnode and then wait for paging 1663 * to complete without deadlocking against vm_fault. 1664 * Instead we simply call vm_object_page_remove() and 1665 * allow it to block internally on a page-by-page 1666 * basis when it encounters pages undergoing async 1667 * I/O. 1668 */ 1669 int flags; 1670 1671 vm_object_reference(object); 1672 vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc); 1673 flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 1674 flags |= invalidate ? OBJPC_INVAL : 0; 1675 vm_object_page_clean(object, 1676 OFF_TO_IDX(offset), 1677 OFF_TO_IDX(offset + size + PAGE_MASK), 1678 flags); 1679 if (invalidate) { 1680 /*vm_object_pip_wait(object, "objmcl");*/ 1681 vm_object_page_remove(object, 1682 OFF_TO_IDX(offset), 1683 OFF_TO_IDX(offset + size + PAGE_MASK), 1684 FALSE); 1685 } 1686 VOP_UNLOCK(object->handle, 0, curproc); 1687 vm_object_deallocate(object); 1688 } 1689 start += size; 1690 } 1691 1692 vm_map_unlock_read(map); 1693 return (KERN_SUCCESS); 1694 } 1695 1696 /* 1697 * vm_map_entry_unwire: [ internal use only ] 1698 * 1699 * Make the region specified by this entry pageable. 1700 * 1701 * The map in question should be locked. 1702 * [This is the reason for this routine's existence.] 1703 */ 1704 static void 1705 vm_map_entry_unwire(map, entry) 1706 vm_map_t map; 1707 vm_map_entry_t entry; 1708 { 1709 vm_fault_unwire(map, entry->start, entry->end); 1710 entry->wired_count = 0; 1711 } 1712 1713 /* 1714 * vm_map_entry_delete: [ internal use only ] 1715 * 1716 * Deallocate the given entry from the target map. 1717 */ 1718 static void 1719 vm_map_entry_delete(map, entry) 1720 vm_map_t map; 1721 vm_map_entry_t entry; 1722 { 1723 vm_map_entry_unlink(map, entry); 1724 map->size -= entry->end - entry->start; 1725 1726 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1727 vm_object_deallocate(entry->object.vm_object); 1728 } 1729 1730 vm_map_entry_dispose(map, entry); 1731 } 1732 1733 /* 1734 * vm_map_delete: [ internal use only ] 1735 * 1736 * Deallocates the given address range from the target 1737 * map. 1738 */ 1739 int 1740 vm_map_delete(map, start, end) 1741 vm_map_t map; 1742 vm_offset_t start; 1743 vm_offset_t end; 1744 { 1745 vm_object_t object; 1746 vm_map_entry_t entry; 1747 vm_map_entry_t first_entry; 1748 1749 /* 1750 * Find the start of the region, and clip it 1751 */ 1752 1753 if (!vm_map_lookup_entry(map, start, &first_entry)) 1754 entry = first_entry->next; 1755 else { 1756 entry = first_entry; 1757 vm_map_clip_start(map, entry, start); 1758 /* 1759 * Fix the lookup hint now, rather than each time though the 1760 * loop. 1761 */ 1762 SAVE_HINT(map, entry->prev); 1763 } 1764 1765 /* 1766 * Save the free space hint 1767 */ 1768 1769 if (entry == &map->header) { 1770 map->first_free = &map->header; 1771 } else if (map->first_free->start >= start) { 1772 map->first_free = entry->prev; 1773 } 1774 1775 /* 1776 * Step through all entries in this region 1777 */ 1778 1779 while ((entry != &map->header) && (entry->start < end)) { 1780 vm_map_entry_t next; 1781 vm_offset_t s, e; 1782 vm_pindex_t offidxstart, offidxend, count; 1783 1784 vm_map_clip_end(map, entry, end); 1785 1786 s = entry->start; 1787 e = entry->end; 1788 next = entry->next; 1789 1790 offidxstart = OFF_TO_IDX(entry->offset); 1791 count = OFF_TO_IDX(e - s); 1792 object = entry->object.vm_object; 1793 1794 /* 1795 * Unwire before removing addresses from the pmap; otherwise, 1796 * unwiring will put the entries back in the pmap. 1797 */ 1798 if (entry->wired_count != 0) { 1799 vm_map_entry_unwire(map, entry); 1800 } 1801 1802 offidxend = offidxstart + count; 1803 1804 if ((object == kernel_object) || (object == kmem_object)) { 1805 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 1806 } else { 1807 pmap_remove(map->pmap, s, e); 1808 if (object != NULL && 1809 object->ref_count != 1 && 1810 (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING && 1811 (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 1812 vm_object_collapse(object); 1813 vm_object_page_remove(object, offidxstart, offidxend, FALSE); 1814 if (object->type == OBJT_SWAP) { 1815 swap_pager_freespace(object, offidxstart, count); 1816 } 1817 if (offidxend >= object->size && 1818 offidxstart < object->size) { 1819 object->size = offidxstart; 1820 } 1821 } 1822 } 1823 1824 /* 1825 * Delete the entry (which may delete the object) only after 1826 * removing all pmap entries pointing to its pages. 1827 * (Otherwise, its page frames may be reallocated, and any 1828 * modify bits will be set in the wrong object!) 1829 */ 1830 vm_map_entry_delete(map, entry); 1831 entry = next; 1832 } 1833 return (KERN_SUCCESS); 1834 } 1835 1836 /* 1837 * vm_map_remove: 1838 * 1839 * Remove the given address range from the target map. 1840 * This is the exported form of vm_map_delete. 1841 */ 1842 int 1843 vm_map_remove(map, start, end) 1844 vm_map_t map; 1845 vm_offset_t start; 1846 vm_offset_t end; 1847 { 1848 int result, s = 0; 1849 1850 if (map == kmem_map || map == mb_map) 1851 s = splvm(); 1852 1853 vm_map_lock(map); 1854 VM_MAP_RANGE_CHECK(map, start, end); 1855 result = vm_map_delete(map, start, end); 1856 vm_map_unlock(map); 1857 1858 if (map == kmem_map || map == mb_map) 1859 splx(s); 1860 1861 return (result); 1862 } 1863 1864 /* 1865 * vm_map_check_protection: 1866 * 1867 * Assert that the target map allows the specified 1868 * privilege on the entire address region given. 1869 * The entire region must be allocated. 1870 */ 1871 boolean_t 1872 vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 1873 vm_prot_t protection) 1874 { 1875 vm_map_entry_t entry; 1876 vm_map_entry_t tmp_entry; 1877 1878 if (!vm_map_lookup_entry(map, start, &tmp_entry)) { 1879 return (FALSE); 1880 } 1881 entry = tmp_entry; 1882 1883 while (start < end) { 1884 if (entry == &map->header) { 1885 return (FALSE); 1886 } 1887 /* 1888 * No holes allowed! 1889 */ 1890 1891 if (start < entry->start) { 1892 return (FALSE); 1893 } 1894 /* 1895 * Check protection associated with entry. 1896 */ 1897 1898 if ((entry->protection & protection) != protection) { 1899 return (FALSE); 1900 } 1901 /* go to next entry */ 1902 1903 start = entry->end; 1904 entry = entry->next; 1905 } 1906 return (TRUE); 1907 } 1908 1909 /* 1910 * Split the pages in a map entry into a new object. This affords 1911 * easier removal of unused pages, and keeps object inheritance from 1912 * being a negative impact on memory usage. 1913 */ 1914 static void 1915 vm_map_split(entry) 1916 vm_map_entry_t entry; 1917 { 1918 vm_page_t m; 1919 vm_object_t orig_object, new_object, source; 1920 vm_offset_t s, e; 1921 vm_pindex_t offidxstart, offidxend, idx; 1922 vm_size_t size; 1923 vm_ooffset_t offset; 1924 1925 orig_object = entry->object.vm_object; 1926 if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1927 return; 1928 if (orig_object->ref_count <= 1) 1929 return; 1930 1931 offset = entry->offset; 1932 s = entry->start; 1933 e = entry->end; 1934 1935 offidxstart = OFF_TO_IDX(offset); 1936 offidxend = offidxstart + OFF_TO_IDX(e - s); 1937 size = offidxend - offidxstart; 1938 1939 new_object = vm_pager_allocate(orig_object->type, 1940 NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL); 1941 if (new_object == NULL) 1942 return; 1943 1944 source = orig_object->backing_object; 1945 if (source != NULL) { 1946 vm_object_reference(source); /* Referenced by new_object */ 1947 TAILQ_INSERT_TAIL(&source->shadow_head, 1948 new_object, shadow_list); 1949 vm_object_clear_flag(source, OBJ_ONEMAPPING); 1950 new_object->backing_object_offset = 1951 orig_object->backing_object_offset + IDX_TO_OFF(offidxstart); 1952 new_object->backing_object = source; 1953 source->shadow_count++; 1954 source->generation++; 1955 } 1956 1957 for (idx = 0; idx < size; idx++) { 1958 vm_page_t m; 1959 1960 retry: 1961 m = vm_page_lookup(orig_object, offidxstart + idx); 1962 if (m == NULL) 1963 continue; 1964 1965 /* 1966 * We must wait for pending I/O to complete before we can 1967 * rename the page. 1968 * 1969 * We do not have to VM_PROT_NONE the page as mappings should 1970 * not be changed by this operation. 1971 */ 1972 if (vm_page_sleep_busy(m, TRUE, "spltwt")) 1973 goto retry; 1974 1975 vm_page_busy(m); 1976 vm_page_rename(m, new_object, idx); 1977 /* page automatically made dirty by rename and cache handled */ 1978 vm_page_busy(m); 1979 } 1980 1981 if (orig_object->type == OBJT_SWAP) { 1982 vm_object_pip_add(orig_object, 1); 1983 /* 1984 * copy orig_object pages into new_object 1985 * and destroy unneeded pages in 1986 * shadow object. 1987 */ 1988 swap_pager_copy(orig_object, new_object, offidxstart, 0); 1989 vm_object_pip_wakeup(orig_object); 1990 } 1991 1992 for (idx = 0; idx < size; idx++) { 1993 m = vm_page_lookup(new_object, idx); 1994 if (m) { 1995 vm_page_wakeup(m); 1996 } 1997 } 1998 1999 entry->object.vm_object = new_object; 2000 entry->offset = 0LL; 2001 vm_object_deallocate(orig_object); 2002 } 2003 2004 /* 2005 * vm_map_copy_entry: 2006 * 2007 * Copies the contents of the source entry to the destination 2008 * entry. The entries *must* be aligned properly. 2009 */ 2010 static void 2011 vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry) 2012 vm_map_t src_map, dst_map; 2013 vm_map_entry_t src_entry, dst_entry; 2014 { 2015 vm_object_t src_object; 2016 2017 if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2018 return; 2019 2020 if (src_entry->wired_count == 0) { 2021 2022 /* 2023 * If the source entry is marked needs_copy, it is already 2024 * write-protected. 2025 */ 2026 if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2027 pmap_protect(src_map->pmap, 2028 src_entry->start, 2029 src_entry->end, 2030 src_entry->protection & ~VM_PROT_WRITE); 2031 } 2032 2033 /* 2034 * Make a copy of the object. 2035 */ 2036 if ((src_object = src_entry->object.vm_object) != NULL) { 2037 2038 if ((src_object->handle == NULL) && 2039 (src_object->type == OBJT_DEFAULT || 2040 src_object->type == OBJT_SWAP)) { 2041 vm_object_collapse(src_object); 2042 if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2043 vm_map_split(src_entry); 2044 src_object = src_entry->object.vm_object; 2045 } 2046 } 2047 2048 vm_object_reference(src_object); 2049 vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2050 dst_entry->object.vm_object = src_object; 2051 src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2052 dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2053 dst_entry->offset = src_entry->offset; 2054 } else { 2055 dst_entry->object.vm_object = NULL; 2056 dst_entry->offset = 0; 2057 } 2058 2059 pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2060 dst_entry->end - dst_entry->start, src_entry->start); 2061 } else { 2062 /* 2063 * Of course, wired down pages can't be set copy-on-write. 2064 * Cause wired pages to be copied into the new map by 2065 * simulating faults (the new pages are pageable) 2066 */ 2067 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2068 } 2069 } 2070 2071 /* 2072 * vmspace_fork: 2073 * Create a new process vmspace structure and vm_map 2074 * based on those of an existing process. The new map 2075 * is based on the old map, according to the inheritance 2076 * values on the regions in that map. 2077 * 2078 * The source map must not be locked. 2079 */ 2080 struct vmspace * 2081 vmspace_fork(vm1) 2082 struct vmspace *vm1; 2083 { 2084 struct vmspace *vm2; 2085 vm_map_t old_map = &vm1->vm_map; 2086 vm_map_t new_map; 2087 vm_map_entry_t old_entry; 2088 vm_map_entry_t new_entry; 2089 vm_object_t object; 2090 2091 vm_map_lock(old_map); 2092 2093 vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2094 bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 2095 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy); 2096 new_map = &vm2->vm_map; /* XXX */ 2097 new_map->timestamp = 1; 2098 2099 old_entry = old_map->header.next; 2100 2101 while (old_entry != &old_map->header) { 2102 if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2103 panic("vm_map_fork: encountered a submap"); 2104 2105 switch (old_entry->inheritance) { 2106 case VM_INHERIT_NONE: 2107 break; 2108 2109 case VM_INHERIT_SHARE: 2110 /* 2111 * Clone the entry, creating the shared object if necessary. 2112 */ 2113 object = old_entry->object.vm_object; 2114 if (object == NULL) { 2115 object = vm_object_allocate(OBJT_DEFAULT, 2116 atop(old_entry->end - old_entry->start)); 2117 old_entry->object.vm_object = object; 2118 old_entry->offset = (vm_offset_t) 0; 2119 } 2120 2121 /* 2122 * Add the reference before calling vm_object_shadow 2123 * to insure that a shadow object is created. 2124 */ 2125 vm_object_reference(object); 2126 if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2127 vm_object_shadow(&old_entry->object.vm_object, 2128 &old_entry->offset, 2129 atop(old_entry->end - old_entry->start)); 2130 old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2131 object = old_entry->object.vm_object; 2132 } 2133 vm_object_clear_flag(object, OBJ_ONEMAPPING); 2134 2135 /* 2136 * Clone the entry, referencing the shared object. 2137 */ 2138 new_entry = vm_map_entry_create(new_map); 2139 *new_entry = *old_entry; 2140 new_entry->wired_count = 0; 2141 2142 /* 2143 * Insert the entry into the new map -- we know we're 2144 * inserting at the end of the new map. 2145 */ 2146 2147 vm_map_entry_link(new_map, new_map->header.prev, 2148 new_entry); 2149 2150 /* 2151 * Update the physical map 2152 */ 2153 2154 pmap_copy(new_map->pmap, old_map->pmap, 2155 new_entry->start, 2156 (old_entry->end - old_entry->start), 2157 old_entry->start); 2158 break; 2159 2160 case VM_INHERIT_COPY: 2161 /* 2162 * Clone the entry and link into the map. 2163 */ 2164 new_entry = vm_map_entry_create(new_map); 2165 *new_entry = *old_entry; 2166 new_entry->wired_count = 0; 2167 new_entry->object.vm_object = NULL; 2168 vm_map_entry_link(new_map, new_map->header.prev, 2169 new_entry); 2170 vm_map_copy_entry(old_map, new_map, old_entry, 2171 new_entry); 2172 break; 2173 } 2174 old_entry = old_entry->next; 2175 } 2176 2177 new_map->size = old_map->size; 2178 vm_map_unlock(old_map); 2179 2180 return (vm2); 2181 } 2182 2183 int 2184 vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 2185 vm_prot_t prot, vm_prot_t max, int cow) 2186 { 2187 vm_map_entry_t prev_entry; 2188 vm_map_entry_t new_stack_entry; 2189 vm_size_t init_ssize; 2190 int rv; 2191 2192 if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS) 2193 return (KERN_NO_SPACE); 2194 2195 if (max_ssize < SGROWSIZ) 2196 init_ssize = max_ssize; 2197 else 2198 init_ssize = SGROWSIZ; 2199 2200 vm_map_lock(map); 2201 2202 /* If addr is already mapped, no go */ 2203 if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 2204 vm_map_unlock(map); 2205 return (KERN_NO_SPACE); 2206 } 2207 2208 /* If we can't accomodate max_ssize in the current mapping, 2209 * no go. However, we need to be aware that subsequent user 2210 * mappings might map into the space we have reserved for 2211 * stack, and currently this space is not protected. 2212 * 2213 * Hopefully we will at least detect this condition 2214 * when we try to grow the stack. 2215 */ 2216 if ((prev_entry->next != &map->header) && 2217 (prev_entry->next->start < addrbos + max_ssize)) { 2218 vm_map_unlock(map); 2219 return (KERN_NO_SPACE); 2220 } 2221 2222 /* We initially map a stack of only init_ssize. We will 2223 * grow as needed later. Since this is to be a grow 2224 * down stack, we map at the top of the range. 2225 * 2226 * Note: we would normally expect prot and max to be 2227 * VM_PROT_ALL, and cow to be 0. Possibly we should 2228 * eliminate these as input parameters, and just 2229 * pass these values here in the insert call. 2230 */ 2231 rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize, 2232 addrbos + max_ssize, prot, max, cow); 2233 2234 /* Now set the avail_ssize amount */ 2235 if (rv == KERN_SUCCESS){ 2236 if (prev_entry != &map->header) 2237 vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize); 2238 new_stack_entry = prev_entry->next; 2239 if (new_stack_entry->end != addrbos + max_ssize || 2240 new_stack_entry->start != addrbos + max_ssize - init_ssize) 2241 panic ("Bad entry start/end for new stack entry"); 2242 else 2243 new_stack_entry->avail_ssize = max_ssize - init_ssize; 2244 } 2245 2246 vm_map_unlock(map); 2247 return (rv); 2248 } 2249 2250 /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 2251 * desired address is already mapped, or if we successfully grow 2252 * the stack. Also returns KERN_SUCCESS if addr is outside the 2253 * stack range (this is strange, but preserves compatibility with 2254 * the grow function in vm_machdep.c). 2255 */ 2256 int 2257 vm_map_growstack (struct proc *p, vm_offset_t addr) 2258 { 2259 vm_map_entry_t prev_entry; 2260 vm_map_entry_t stack_entry; 2261 vm_map_entry_t new_stack_entry; 2262 struct vmspace *vm = p->p_vmspace; 2263 vm_map_t map = &vm->vm_map; 2264 vm_offset_t end; 2265 int grow_amount; 2266 int rv; 2267 int is_procstack; 2268 Retry: 2269 vm_map_lock_read(map); 2270 2271 /* If addr is already in the entry range, no need to grow.*/ 2272 if (vm_map_lookup_entry(map, addr, &prev_entry)) { 2273 vm_map_unlock_read(map); 2274 return (KERN_SUCCESS); 2275 } 2276 2277 if ((stack_entry = prev_entry->next) == &map->header) { 2278 vm_map_unlock_read(map); 2279 return (KERN_SUCCESS); 2280 } 2281 if (prev_entry == &map->header) 2282 end = stack_entry->start - stack_entry->avail_ssize; 2283 else 2284 end = prev_entry->end; 2285 2286 /* This next test mimics the old grow function in vm_machdep.c. 2287 * It really doesn't quite make sense, but we do it anyway 2288 * for compatibility. 2289 * 2290 * If not growable stack, return success. This signals the 2291 * caller to proceed as he would normally with normal vm. 2292 */ 2293 if (stack_entry->avail_ssize < 1 || 2294 addr >= stack_entry->start || 2295 addr < stack_entry->start - stack_entry->avail_ssize) { 2296 vm_map_unlock_read(map); 2297 return (KERN_SUCCESS); 2298 } 2299 2300 /* Find the minimum grow amount */ 2301 grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE); 2302 if (grow_amount > stack_entry->avail_ssize) { 2303 vm_map_unlock_read(map); 2304 return (KERN_NO_SPACE); 2305 } 2306 2307 /* If there is no longer enough space between the entries 2308 * nogo, and adjust the available space. Note: this 2309 * should only happen if the user has mapped into the 2310 * stack area after the stack was created, and is 2311 * probably an error. 2312 * 2313 * This also effectively destroys any guard page the user 2314 * might have intended by limiting the stack size. 2315 */ 2316 if (grow_amount > stack_entry->start - end) { 2317 if (vm_map_lock_upgrade(map)) 2318 goto Retry; 2319 2320 stack_entry->avail_ssize = stack_entry->start - end; 2321 2322 vm_map_unlock(map); 2323 return (KERN_NO_SPACE); 2324 } 2325 2326 is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr; 2327 2328 /* If this is the main process stack, see if we're over the 2329 * stack limit. 2330 */ 2331 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 2332 p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 2333 vm_map_unlock_read(map); 2334 return (KERN_NO_SPACE); 2335 } 2336 2337 /* Round up the grow amount modulo SGROWSIZ */ 2338 grow_amount = roundup (grow_amount, SGROWSIZ); 2339 if (grow_amount > stack_entry->avail_ssize) { 2340 grow_amount = stack_entry->avail_ssize; 2341 } 2342 if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 2343 p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 2344 grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur - 2345 ctob(vm->vm_ssize); 2346 } 2347 2348 if (vm_map_lock_upgrade(map)) 2349 goto Retry; 2350 2351 /* Get the preliminary new entry start value */ 2352 addr = stack_entry->start - grow_amount; 2353 2354 /* If this puts us into the previous entry, cut back our growth 2355 * to the available space. Also, see the note above. 2356 */ 2357 if (addr < end) { 2358 stack_entry->avail_ssize = stack_entry->start - end; 2359 addr = end; 2360 } 2361 2362 rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 2363 VM_PROT_ALL, 2364 VM_PROT_ALL, 2365 0); 2366 2367 /* Adjust the available stack space by the amount we grew. */ 2368 if (rv == KERN_SUCCESS) { 2369 if (prev_entry != &map->header) 2370 vm_map_clip_end(map, prev_entry, addr); 2371 new_stack_entry = prev_entry->next; 2372 if (new_stack_entry->end != stack_entry->start || 2373 new_stack_entry->start != addr) 2374 panic ("Bad stack grow start/end in new stack entry"); 2375 else { 2376 new_stack_entry->avail_ssize = stack_entry->avail_ssize - 2377 (new_stack_entry->end - 2378 new_stack_entry->start); 2379 if (is_procstack) 2380 vm->vm_ssize += btoc(new_stack_entry->end - 2381 new_stack_entry->start); 2382 } 2383 } 2384 2385 vm_map_unlock(map); 2386 return (rv); 2387 2388 } 2389 2390 /* 2391 * Unshare the specified VM space for exec. If other processes are 2392 * mapped to it, then create a new one. The new vmspace is null. 2393 */ 2394 2395 void 2396 vmspace_exec(struct proc *p) { 2397 struct vmspace *oldvmspace = p->p_vmspace; 2398 struct vmspace *newvmspace; 2399 vm_map_t map = &p->p_vmspace->vm_map; 2400 2401 newvmspace = vmspace_alloc(map->min_offset, map->max_offset); 2402 bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy, 2403 (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy); 2404 /* 2405 * This code is written like this for prototype purposes. The 2406 * goal is to avoid running down the vmspace here, but let the 2407 * other process's that are still using the vmspace to finally 2408 * run it down. Even though there is little or no chance of blocking 2409 * here, it is a good idea to keep this form for future mods. 2410 */ 2411 vmspace_free(oldvmspace); 2412 p->p_vmspace = newvmspace; 2413 pmap_pinit2(vmspace_pmap(newvmspace)); 2414 if (p == curproc) 2415 pmap_activate(p); 2416 } 2417 2418 /* 2419 * Unshare the specified VM space for forcing COW. This 2420 * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 2421 */ 2422 2423 void 2424 vmspace_unshare(struct proc *p) { 2425 struct vmspace *oldvmspace = p->p_vmspace; 2426 struct vmspace *newvmspace; 2427 2428 if (oldvmspace->vm_refcnt == 1) 2429 return; 2430 newvmspace = vmspace_fork(oldvmspace); 2431 vmspace_free(oldvmspace); 2432 p->p_vmspace = newvmspace; 2433 pmap_pinit2(vmspace_pmap(newvmspace)); 2434 if (p == curproc) 2435 pmap_activate(p); 2436 } 2437 2438 2439 /* 2440 * vm_map_lookup: 2441 * 2442 * Finds the VM object, offset, and 2443 * protection for a given virtual address in the 2444 * specified map, assuming a page fault of the 2445 * type specified. 2446 * 2447 * Leaves the map in question locked for read; return 2448 * values are guaranteed until a vm_map_lookup_done 2449 * call is performed. Note that the map argument 2450 * is in/out; the returned map must be used in 2451 * the call to vm_map_lookup_done. 2452 * 2453 * A handle (out_entry) is returned for use in 2454 * vm_map_lookup_done, to make that fast. 2455 * 2456 * If a lookup is requested with "write protection" 2457 * specified, the map may be changed to perform virtual 2458 * copying operations, although the data referenced will 2459 * remain the same. 2460 */ 2461 int 2462 vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 2463 vm_offset_t vaddr, 2464 vm_prot_t fault_typea, 2465 vm_map_entry_t *out_entry, /* OUT */ 2466 vm_object_t *object, /* OUT */ 2467 vm_pindex_t *pindex, /* OUT */ 2468 vm_prot_t *out_prot, /* OUT */ 2469 boolean_t *wired) /* OUT */ 2470 { 2471 vm_map_entry_t entry; 2472 vm_map_t map = *var_map; 2473 vm_prot_t prot; 2474 vm_prot_t fault_type = fault_typea; 2475 2476 RetryLookup:; 2477 2478 /* 2479 * Lookup the faulting address. 2480 */ 2481 2482 vm_map_lock_read(map); 2483 2484 #define RETURN(why) \ 2485 { \ 2486 vm_map_unlock_read(map); \ 2487 return(why); \ 2488 } 2489 2490 /* 2491 * If the map has an interesting hint, try it before calling full 2492 * blown lookup routine. 2493 */ 2494 2495 entry = map->hint; 2496 2497 *out_entry = entry; 2498 2499 if ((entry == &map->header) || 2500 (vaddr < entry->start) || (vaddr >= entry->end)) { 2501 vm_map_entry_t tmp_entry; 2502 2503 /* 2504 * Entry was either not a valid hint, or the vaddr was not 2505 * contained in the entry, so do a full lookup. 2506 */ 2507 if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) 2508 RETURN(KERN_INVALID_ADDRESS); 2509 2510 entry = tmp_entry; 2511 *out_entry = entry; 2512 } 2513 2514 /* 2515 * Handle submaps. 2516 */ 2517 2518 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2519 vm_map_t old_map = map; 2520 2521 *var_map = map = entry->object.sub_map; 2522 vm_map_unlock_read(old_map); 2523 goto RetryLookup; 2524 } 2525 2526 /* 2527 * Check whether this task is allowed to have this page. 2528 * Note the special case for MAP_ENTRY_COW 2529 * pages with an override. This is to implement a forced 2530 * COW for debuggers. 2531 */ 2532 2533 if (fault_type & VM_PROT_OVERRIDE_WRITE) 2534 prot = entry->max_protection; 2535 else 2536 prot = entry->protection; 2537 2538 fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 2539 if ((fault_type & prot) != fault_type) { 2540 RETURN(KERN_PROTECTION_FAILURE); 2541 } 2542 2543 if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 2544 (entry->eflags & MAP_ENTRY_COW) && 2545 (fault_type & VM_PROT_WRITE) && 2546 (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 2547 RETURN(KERN_PROTECTION_FAILURE); 2548 } 2549 2550 /* 2551 * If this page is not pageable, we have to get it for all possible 2552 * accesses. 2553 */ 2554 2555 *wired = (entry->wired_count != 0); 2556 if (*wired) 2557 prot = fault_type = entry->protection; 2558 2559 /* 2560 * If the entry was copy-on-write, we either ... 2561 */ 2562 2563 if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2564 /* 2565 * If we want to write the page, we may as well handle that 2566 * now since we've got the map locked. 2567 * 2568 * If we don't need to write the page, we just demote the 2569 * permissions allowed. 2570 */ 2571 2572 if (fault_type & VM_PROT_WRITE) { 2573 /* 2574 * Make a new object, and place it in the object 2575 * chain. Note that no new references have appeared 2576 * -- one just moved from the map to the new 2577 * object. 2578 */ 2579 2580 if (vm_map_lock_upgrade(map)) 2581 goto RetryLookup; 2582 2583 vm_object_shadow( 2584 &entry->object.vm_object, 2585 &entry->offset, 2586 atop(entry->end - entry->start)); 2587 2588 entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2589 vm_map_lock_downgrade(map); 2590 } else { 2591 /* 2592 * We're attempting to read a copy-on-write page -- 2593 * don't allow writes. 2594 */ 2595 2596 prot &= ~VM_PROT_WRITE; 2597 } 2598 } 2599 2600 /* 2601 * Create an object if necessary. 2602 */ 2603 if (entry->object.vm_object == NULL) { 2604 if (vm_map_lock_upgrade(map)) 2605 goto RetryLookup; 2606 2607 entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 2608 atop(entry->end - entry->start)); 2609 entry->offset = 0; 2610 vm_map_lock_downgrade(map); 2611 } 2612 2613 /* 2614 * Return the object/offset from this entry. If the entry was 2615 * copy-on-write or empty, it has been fixed up. 2616 */ 2617 2618 *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 2619 *object = entry->object.vm_object; 2620 2621 /* 2622 * Return whether this is the only map sharing this data. 2623 */ 2624 2625 *out_prot = prot; 2626 return (KERN_SUCCESS); 2627 2628 #undef RETURN 2629 } 2630 2631 /* 2632 * vm_map_lookup_done: 2633 * 2634 * Releases locks acquired by a vm_map_lookup 2635 * (according to the handle returned by that lookup). 2636 */ 2637 2638 void 2639 vm_map_lookup_done(map, entry) 2640 vm_map_t map; 2641 vm_map_entry_t entry; 2642 { 2643 /* 2644 * Unlock the main-level map 2645 */ 2646 2647 vm_map_unlock_read(map); 2648 } 2649 2650 /* 2651 * Implement uiomove with VM operations. This handles (and collateral changes) 2652 * support every combination of source object modification, and COW type 2653 * operations. 2654 */ 2655 int 2656 vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages) 2657 vm_map_t mapa; 2658 vm_object_t srcobject; 2659 off_t cp; 2660 int cnta; 2661 vm_offset_t uaddra; 2662 int *npages; 2663 { 2664 vm_map_t map; 2665 vm_object_t first_object, oldobject, object; 2666 vm_map_entry_t entry; 2667 vm_prot_t prot; 2668 boolean_t wired; 2669 int tcnt, rv; 2670 vm_offset_t uaddr, start, end, tend; 2671 vm_pindex_t first_pindex, osize, oindex; 2672 off_t ooffset; 2673 int cnt; 2674 2675 if (npages) 2676 *npages = 0; 2677 2678 cnt = cnta; 2679 uaddr = uaddra; 2680 2681 while (cnt > 0) { 2682 map = mapa; 2683 2684 if ((vm_map_lookup(&map, uaddr, 2685 VM_PROT_READ, &entry, &first_object, 2686 &first_pindex, &prot, &wired)) != KERN_SUCCESS) { 2687 return EFAULT; 2688 } 2689 2690 vm_map_clip_start(map, entry, uaddr); 2691 2692 tcnt = cnt; 2693 tend = uaddr + tcnt; 2694 if (tend > entry->end) { 2695 tcnt = entry->end - uaddr; 2696 tend = entry->end; 2697 } 2698 2699 vm_map_clip_end(map, entry, tend); 2700 2701 start = entry->start; 2702 end = entry->end; 2703 2704 osize = atop(tcnt); 2705 2706 oindex = OFF_TO_IDX(cp); 2707 if (npages) { 2708 vm_pindex_t idx; 2709 for (idx = 0; idx < osize; idx++) { 2710 vm_page_t m; 2711 if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) { 2712 vm_map_lookup_done(map, entry); 2713 return 0; 2714 } 2715 /* 2716 * disallow busy or invalid pages, but allow 2717 * m->busy pages if they are entirely valid. 2718 */ 2719 if ((m->flags & PG_BUSY) || 2720 ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) { 2721 vm_map_lookup_done(map, entry); 2722 return 0; 2723 } 2724 } 2725 } 2726 2727 /* 2728 * If we are changing an existing map entry, just redirect 2729 * the object, and change mappings. 2730 */ 2731 if ((first_object->type == OBJT_VNODE) && 2732 ((oldobject = entry->object.vm_object) == first_object)) { 2733 2734 if ((entry->offset != cp) || (oldobject != srcobject)) { 2735 /* 2736 * Remove old window into the file 2737 */ 2738 pmap_remove (map->pmap, uaddr, tend); 2739 2740 /* 2741 * Force copy on write for mmaped regions 2742 */ 2743 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 2744 2745 /* 2746 * Point the object appropriately 2747 */ 2748 if (oldobject != srcobject) { 2749 2750 /* 2751 * Set the object optimization hint flag 2752 */ 2753 vm_object_set_flag(srcobject, OBJ_OPT); 2754 vm_object_reference(srcobject); 2755 entry->object.vm_object = srcobject; 2756 2757 if (oldobject) { 2758 vm_object_deallocate(oldobject); 2759 } 2760 } 2761 2762 entry->offset = cp; 2763 map->timestamp++; 2764 } else { 2765 pmap_remove (map->pmap, uaddr, tend); 2766 } 2767 2768 } else if ((first_object->ref_count == 1) && 2769 (first_object->size == osize) && 2770 ((first_object->type == OBJT_DEFAULT) || 2771 (first_object->type == OBJT_SWAP)) ) { 2772 2773 oldobject = first_object->backing_object; 2774 2775 if ((first_object->backing_object_offset != cp) || 2776 (oldobject != srcobject)) { 2777 /* 2778 * Remove old window into the file 2779 */ 2780 pmap_remove (map->pmap, uaddr, tend); 2781 2782 /* 2783 * Remove unneeded old pages 2784 */ 2785 vm_object_page_remove(first_object, 0, 0, 0); 2786 2787 /* 2788 * Invalidate swap space 2789 */ 2790 if (first_object->type == OBJT_SWAP) { 2791 swap_pager_freespace(first_object, 2792 0, 2793 first_object->size); 2794 } 2795 2796 /* 2797 * Force copy on write for mmaped regions 2798 */ 2799 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 2800 2801 /* 2802 * Point the object appropriately 2803 */ 2804 if (oldobject != srcobject) { 2805 2806 /* 2807 * Set the object optimization hint flag 2808 */ 2809 vm_object_set_flag(srcobject, OBJ_OPT); 2810 vm_object_reference(srcobject); 2811 2812 if (oldobject) { 2813 TAILQ_REMOVE(&oldobject->shadow_head, 2814 first_object, shadow_list); 2815 oldobject->shadow_count--; 2816 /* XXX bump generation? */ 2817 vm_object_deallocate(oldobject); 2818 } 2819 2820 TAILQ_INSERT_TAIL(&srcobject->shadow_head, 2821 first_object, shadow_list); 2822 srcobject->shadow_count++; 2823 /* XXX bump generation? */ 2824 2825 first_object->backing_object = srcobject; 2826 } 2827 first_object->backing_object_offset = cp; 2828 map->timestamp++; 2829 } else { 2830 pmap_remove (map->pmap, uaddr, tend); 2831 } 2832 /* 2833 * Otherwise, we have to do a logical mmap. 2834 */ 2835 } else { 2836 2837 vm_object_set_flag(srcobject, OBJ_OPT); 2838 vm_object_reference(srcobject); 2839 2840 pmap_remove (map->pmap, uaddr, tend); 2841 2842 vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 2843 vm_map_lock_upgrade(map); 2844 2845 if (entry == &map->header) { 2846 map->first_free = &map->header; 2847 } else if (map->first_free->start >= start) { 2848 map->first_free = entry->prev; 2849 } 2850 2851 SAVE_HINT(map, entry->prev); 2852 vm_map_entry_delete(map, entry); 2853 2854 object = srcobject; 2855 ooffset = cp; 2856 2857 rv = vm_map_insert(map, object, ooffset, start, tend, 2858 VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE); 2859 2860 if (rv != KERN_SUCCESS) 2861 panic("vm_uiomove: could not insert new entry: %d", rv); 2862 } 2863 2864 /* 2865 * Map the window directly, if it is already in memory 2866 */ 2867 pmap_object_init_pt(map->pmap, uaddr, 2868 srcobject, oindex, tcnt, 0); 2869 2870 map->timestamp++; 2871 vm_map_unlock(map); 2872 2873 cnt -= tcnt; 2874 uaddr += tcnt; 2875 cp += tcnt; 2876 if (npages) 2877 *npages += osize; 2878 } 2879 return 0; 2880 } 2881 2882 /* 2883 * Performs the copy_on_write operations necessary to allow the virtual copies 2884 * into user space to work. This has to be called for write(2) system calls 2885 * from other processes, file unlinking, and file size shrinkage. 2886 */ 2887 void 2888 vm_freeze_copyopts(object, froma, toa) 2889 vm_object_t object; 2890 vm_pindex_t froma, toa; 2891 { 2892 int rv; 2893 vm_object_t robject; 2894 vm_pindex_t idx; 2895 2896 if ((object == NULL) || 2897 ((object->flags & OBJ_OPT) == 0)) 2898 return; 2899 2900 if (object->shadow_count > object->ref_count) 2901 panic("vm_freeze_copyopts: sc > rc"); 2902 2903 while((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) { 2904 vm_pindex_t bo_pindex; 2905 vm_page_t m_in, m_out; 2906 2907 bo_pindex = OFF_TO_IDX(robject->backing_object_offset); 2908 2909 vm_object_reference(robject); 2910 2911 vm_object_pip_wait(robject, "objfrz"); 2912 2913 if (robject->ref_count == 1) { 2914 vm_object_deallocate(robject); 2915 continue; 2916 } 2917 2918 vm_object_pip_add(robject, 1); 2919 2920 for (idx = 0; idx < robject->size; idx++) { 2921 2922 m_out = vm_page_grab(robject, idx, 2923 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 2924 2925 if (m_out->valid == 0) { 2926 m_in = vm_page_grab(object, bo_pindex + idx, 2927 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 2928 if (m_in->valid == 0) { 2929 rv = vm_pager_get_pages(object, &m_in, 1, 0); 2930 if (rv != VM_PAGER_OK) { 2931 printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex); 2932 continue; 2933 } 2934 vm_page_deactivate(m_in); 2935 } 2936 2937 vm_page_protect(m_in, VM_PROT_NONE); 2938 pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out)); 2939 m_out->valid = m_in->valid; 2940 vm_page_dirty(m_out); 2941 vm_page_activate(m_out); 2942 vm_page_wakeup(m_in); 2943 } 2944 vm_page_wakeup(m_out); 2945 } 2946 2947 object->shadow_count--; 2948 object->ref_count--; 2949 TAILQ_REMOVE(&object->shadow_head, robject, shadow_list); 2950 robject->backing_object = NULL; 2951 robject->backing_object_offset = 0; 2952 2953 vm_object_pip_wakeup(robject); 2954 vm_object_deallocate(robject); 2955 } 2956 2957 vm_object_clear_flag(object, OBJ_OPT); 2958 } 2959 2960 #include "opt_ddb.h" 2961 #ifdef DDB 2962 #include <sys/kernel.h> 2963 2964 #include <ddb/ddb.h> 2965 2966 /* 2967 * vm_map_print: [ debug ] 2968 */ 2969 DB_SHOW_COMMAND(map, vm_map_print) 2970 { 2971 static int nlines; 2972 /* XXX convert args. */ 2973 vm_map_t map = (vm_map_t)addr; 2974 boolean_t full = have_addr; 2975 2976 vm_map_entry_t entry; 2977 2978 db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 2979 (void *)map, 2980 (void *)map->pmap, map->nentries, map->timestamp); 2981 nlines++; 2982 2983 if (!full && db_indent) 2984 return; 2985 2986 db_indent += 2; 2987 for (entry = map->header.next; entry != &map->header; 2988 entry = entry->next) { 2989 db_iprintf("map entry %p: start=%p, end=%p\n", 2990 (void *)entry, (void *)entry->start, (void *)entry->end); 2991 nlines++; 2992 { 2993 static char *inheritance_name[4] = 2994 {"share", "copy", "none", "donate_copy"}; 2995 2996 db_iprintf(" prot=%x/%x/%s", 2997 entry->protection, 2998 entry->max_protection, 2999 inheritance_name[(int)(unsigned char)entry->inheritance]); 3000 if (entry->wired_count != 0) 3001 db_printf(", wired"); 3002 } 3003 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3004 /* XXX no %qd in kernel. Truncate entry->offset. */ 3005 db_printf(", share=%p, offset=0x%lx\n", 3006 (void *)entry->object.sub_map, 3007 (long)entry->offset); 3008 nlines++; 3009 if ((entry->prev == &map->header) || 3010 (entry->prev->object.sub_map != 3011 entry->object.sub_map)) { 3012 db_indent += 2; 3013 vm_map_print((db_expr_t)(intptr_t) 3014 entry->object.sub_map, 3015 full, 0, (char *)0); 3016 db_indent -= 2; 3017 } 3018 } else { 3019 /* XXX no %qd in kernel. Truncate entry->offset. */ 3020 db_printf(", object=%p, offset=0x%lx", 3021 (void *)entry->object.vm_object, 3022 (long)entry->offset); 3023 if (entry->eflags & MAP_ENTRY_COW) 3024 db_printf(", copy (%s)", 3025 (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3026 db_printf("\n"); 3027 nlines++; 3028 3029 if ((entry->prev == &map->header) || 3030 (entry->prev->object.vm_object != 3031 entry->object.vm_object)) { 3032 db_indent += 2; 3033 vm_object_print((db_expr_t)(intptr_t) 3034 entry->object.vm_object, 3035 full, 0, (char *)0); 3036 nlines += 4; 3037 db_indent -= 2; 3038 } 3039 } 3040 } 3041 db_indent -= 2; 3042 if (db_indent == 0) 3043 nlines = 0; 3044 } 3045 3046 3047 DB_SHOW_COMMAND(procvm, procvm) 3048 { 3049 struct proc *p; 3050 3051 if (have_addr) { 3052 p = (struct proc *) addr; 3053 } else { 3054 p = curproc; 3055 } 3056 3057 db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3058 (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3059 (void *)vmspace_pmap(p->p_vmspace)); 3060 3061 vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 3062 } 3063 3064 #endif /* DDB */ 3065