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