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