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