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