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