1 /* $OpenBSD: uvm_map.c,v 1.23 2001/08/11 10:57:22 art Exp $ */ 2 /* $NetBSD: uvm_map.c,v 1.77 2000/06/13 04:10:47 chs Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1991, 1993, The Regents of the University of California. 7 * 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by Charles D. Cranor, 24 * Washington University, the University of California, Berkeley and 25 * its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * @(#)vm_map.c 8.3 (Berkeley) 1/12/94 43 * from: Id: uvm_map.c,v 1.1.2.27 1998/02/07 01:16:54 chs Exp 44 * 45 * 46 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 47 * All rights reserved. 48 * 49 * Permission to use, copy, modify and distribute this software and 50 * its documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 */ 69 70 /* 71 * uvm_map.c: uvm map operations 72 */ 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/mman.h> 77 #include <sys/proc.h> 78 #include <sys/malloc.h> 79 #include <sys/pool.h> 80 81 #ifdef SYSVSHM 82 #include <sys/shm.h> 83 #endif 84 85 #include <vm/vm.h> 86 #include <vm/vm_page.h> 87 #include <vm/vm_kern.h> 88 89 #define UVM_MAP 90 #include <uvm/uvm.h> 91 92 #ifdef DDB 93 #include <uvm/uvm_ddb.h> 94 #endif 95 96 97 struct uvm_cnt uvm_map_call, map_backmerge, map_forwmerge; 98 struct uvm_cnt uvm_mlk_call, uvm_mlk_hint; 99 100 /* 101 * pool for vmspace structures. 102 */ 103 104 struct pool uvm_vmspace_pool; 105 106 /* 107 * pool for dynamically-allocated map entries. 108 */ 109 110 struct pool uvm_map_entry_pool; 111 112 #ifdef PMAP_GROWKERNEL 113 /* 114 * This global represents the end of the kernel virtual address 115 * space. If we want to exceed this, we must grow the kernel 116 * virtual address space dynamically. 117 * 118 * Note, this variable is locked by kernel_map's lock. 119 */ 120 vaddr_t uvm_maxkaddr; 121 #endif 122 123 /* 124 * macros 125 */ 126 127 /* 128 * uvm_map_entry_link: insert entry into a map 129 * 130 * => map must be locked 131 */ 132 #define uvm_map_entry_link(map, after_where, entry) do { \ 133 (map)->nentries++; \ 134 (entry)->prev = (after_where); \ 135 (entry)->next = (after_where)->next; \ 136 (entry)->prev->next = (entry); \ 137 (entry)->next->prev = (entry); \ 138 } while (0) 139 140 /* 141 * uvm_map_entry_unlink: remove entry from a map 142 * 143 * => map must be locked 144 */ 145 #define uvm_map_entry_unlink(map, entry) do { \ 146 (map)->nentries--; \ 147 (entry)->next->prev = (entry)->prev; \ 148 (entry)->prev->next = (entry)->next; \ 149 } while (0) 150 151 /* 152 * SAVE_HINT: saves the specified entry as the hint for future lookups. 153 * 154 * => map need not be locked (protected by hint_lock). 155 */ 156 #define SAVE_HINT(map,value) do { \ 157 simple_lock(&(map)->hint_lock); \ 158 (map)->hint = (value); \ 159 simple_unlock(&(map)->hint_lock); \ 160 } while (0) 161 162 /* 163 * VM_MAP_RANGE_CHECK: check and correct range 164 * 165 * => map must at least be read locked 166 */ 167 168 #define VM_MAP_RANGE_CHECK(map, start, end) do { \ 169 if (start < vm_map_min(map)) \ 170 start = vm_map_min(map); \ 171 if (end > vm_map_max(map)) \ 172 end = vm_map_max(map); \ 173 if (start > end) \ 174 start = end; \ 175 } while (0) 176 177 /* 178 * local prototypes 179 */ 180 181 static vm_map_entry_t uvm_mapent_alloc __P((vm_map_t)); 182 static void uvm_mapent_copy __P((vm_map_entry_t,vm_map_entry_t)); 183 static void uvm_mapent_free __P((vm_map_entry_t)); 184 static void uvm_map_entry_unwire __P((vm_map_t, vm_map_entry_t)); 185 186 /* 187 * local inlines 188 */ 189 190 /* 191 * uvm_mapent_alloc: allocate a map entry 192 * 193 * => XXX: static pool for kernel map? 194 */ 195 196 static __inline vm_map_entry_t 197 uvm_mapent_alloc(map) 198 vm_map_t map; 199 { 200 vm_map_entry_t me; 201 int s; 202 UVMHIST_FUNC("uvm_mapent_alloc"); 203 UVMHIST_CALLED(maphist); 204 205 if ((map->flags & VM_MAP_INTRSAFE) == 0 && 206 map != kernel_map && kernel_map != NULL /* XXX */) { 207 me = pool_get(&uvm_map_entry_pool, PR_WAITOK); 208 me->flags = 0; 209 /* me can't be null, wait ok */ 210 } else { 211 s = splimp(); /* protect kentry_free list with splimp */ 212 simple_lock(&uvm.kentry_lock); 213 me = uvm.kentry_free; 214 if (me) uvm.kentry_free = me->next; 215 simple_unlock(&uvm.kentry_lock); 216 splx(s); 217 if (!me) 218 panic("mapent_alloc: out of static map entries, check MAX_KMAPENT"); 219 me->flags = UVM_MAP_STATIC; 220 } 221 222 UVMHIST_LOG(maphist, "<- new entry=0x%x [kentry=%d]", 223 me, ((map->flags & VM_MAP_INTRSAFE) != 0 || map == kernel_map) 224 ? TRUE : FALSE, 0, 0); 225 return(me); 226 } 227 228 /* 229 * uvm_mapent_free: free map entry 230 * 231 * => XXX: static pool for kernel map? 232 */ 233 234 static __inline void 235 uvm_mapent_free(me) 236 vm_map_entry_t me; 237 { 238 int s; 239 UVMHIST_FUNC("uvm_mapent_free"); 240 UVMHIST_CALLED(maphist); 241 UVMHIST_LOG(maphist,"<- freeing map entry=0x%x [flags=%d]", 242 me, me->flags, 0, 0); 243 if ((me->flags & UVM_MAP_STATIC) == 0) { 244 pool_put(&uvm_map_entry_pool, me); 245 } else { 246 s = splimp(); /* protect kentry_free list with splimp */ 247 simple_lock(&uvm.kentry_lock); 248 me->next = uvm.kentry_free; 249 uvm.kentry_free = me; 250 simple_unlock(&uvm.kentry_lock); 251 splx(s); 252 } 253 } 254 255 /* 256 * uvm_mapent_copy: copy a map entry, preserving flags 257 */ 258 259 static __inline void 260 uvm_mapent_copy(src, dst) 261 vm_map_entry_t src; 262 vm_map_entry_t dst; 263 { 264 265 memcpy(dst, src, ((char *)&src->uvm_map_entry_stop_copy) - ((char*)src)); 266 } 267 268 /* 269 * uvm_map_entry_unwire: unwire a map entry 270 * 271 * => map should be locked by caller 272 */ 273 274 static __inline void 275 uvm_map_entry_unwire(map, entry) 276 vm_map_t map; 277 vm_map_entry_t entry; 278 { 279 280 entry->wired_count = 0; 281 uvm_fault_unwire_locked(map, entry->start, entry->end); 282 } 283 284 /* 285 * uvm_map_init: init mapping system at boot time. note that we allocate 286 * and init the static pool of vm_map_entry_t's for the kernel here. 287 */ 288 289 void 290 uvm_map_init() 291 { 292 static struct vm_map_entry kernel_map_entry[MAX_KMAPENT]; 293 #if defined(UVMHIST) 294 static struct uvm_history_ent maphistbuf[100]; 295 static struct uvm_history_ent pdhistbuf[100]; 296 #endif 297 int lcv; 298 299 /* 300 * first, init logging system. 301 */ 302 303 UVMHIST_FUNC("uvm_map_init"); 304 UVMHIST_INIT_STATIC(maphist, maphistbuf); 305 UVMHIST_INIT_STATIC(pdhist, pdhistbuf); 306 UVMHIST_CALLED(maphist); 307 UVMHIST_LOG(maphist,"<starting uvm map system>", 0, 0, 0, 0); 308 UVMCNT_INIT(uvm_map_call, UVMCNT_CNT, 0, 309 "# uvm_map() successful calls", 0); 310 UVMCNT_INIT(map_backmerge, UVMCNT_CNT, 0, "# uvm_map() back merges", 0); 311 UVMCNT_INIT(map_forwmerge, UVMCNT_CNT, 0, "# uvm_map() missed forward", 312 0); 313 UVMCNT_INIT(uvm_mlk_call, UVMCNT_CNT, 0, "# map lookup calls", 0); 314 UVMCNT_INIT(uvm_mlk_hint, UVMCNT_CNT, 0, "# map lookup hint hits", 0); 315 316 /* 317 * now set up static pool of kernel map entrys ... 318 */ 319 320 simple_lock_init(&uvm.kentry_lock); 321 uvm.kentry_free = NULL; 322 for (lcv = 0 ; lcv < MAX_KMAPENT ; lcv++) { 323 kernel_map_entry[lcv].next = uvm.kentry_free; 324 uvm.kentry_free = &kernel_map_entry[lcv]; 325 } 326 327 /* 328 * initialize the map-related pools. 329 */ 330 pool_init(&uvm_vmspace_pool, sizeof(struct vmspace), 331 0, 0, 0, "vmsppl", 0, 332 pool_page_alloc_nointr, pool_page_free_nointr, M_VMMAP); 333 pool_init(&uvm_map_entry_pool, sizeof(struct vm_map_entry), 334 0, 0, 0, "vmmpepl", 0, 335 pool_page_alloc_nointr, pool_page_free_nointr, M_VMMAP); 336 } 337 338 /* 339 * clippers 340 */ 341 342 /* 343 * uvm_map_clip_start: ensure that the entry begins at or after 344 * the starting address, if it doesn't we split the entry. 345 * 346 * => caller should use UVM_MAP_CLIP_START macro rather than calling 347 * this directly 348 * => map must be locked by caller 349 */ 350 351 void uvm_map_clip_start(map, entry, start) 352 vm_map_t map; 353 vm_map_entry_t entry; 354 vaddr_t start; 355 { 356 vm_map_entry_t new_entry; 357 vaddr_t new_adj; 358 359 /* uvm_map_simplify_entry(map, entry); */ /* XXX */ 360 361 /* 362 * Split off the front portion. note that we must insert the new 363 * entry BEFORE this one, so that this entry has the specified 364 * starting address. 365 */ 366 367 new_entry = uvm_mapent_alloc(map); 368 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */ 369 370 new_entry->end = start; 371 new_adj = start - new_entry->start; 372 if (entry->object.uvm_obj) 373 entry->offset += new_adj; /* shift start over */ 374 entry->start = start; 375 376 if (new_entry->aref.ar_amap) { 377 amap_splitref(&new_entry->aref, &entry->aref, new_adj); 378 } 379 380 uvm_map_entry_link(map, entry->prev, new_entry); 381 382 if (UVM_ET_ISSUBMAP(entry)) { 383 /* ... unlikely to happen, but play it safe */ 384 uvm_map_reference(new_entry->object.sub_map); 385 } else { 386 if (UVM_ET_ISOBJ(entry) && 387 entry->object.uvm_obj->pgops && 388 entry->object.uvm_obj->pgops->pgo_reference) 389 entry->object.uvm_obj->pgops->pgo_reference( 390 entry->object.uvm_obj); 391 } 392 } 393 394 /* 395 * uvm_map_clip_end: ensure that the entry ends at or before 396 * the ending address, if it does't we split the reference 397 * 398 * => caller should use UVM_MAP_CLIP_END macro rather than calling 399 * this directly 400 * => map must be locked by caller 401 */ 402 403 void 404 uvm_map_clip_end(map, entry, end) 405 vm_map_t map; 406 vm_map_entry_t entry; 407 vaddr_t end; 408 { 409 vm_map_entry_t new_entry; 410 vaddr_t new_adj; /* #bytes we move start forward */ 411 412 /* 413 * Create a new entry and insert it 414 * AFTER the specified entry 415 */ 416 417 new_entry = uvm_mapent_alloc(map); 418 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */ 419 420 new_entry->start = entry->end = end; 421 new_adj = end - entry->start; 422 if (new_entry->object.uvm_obj) 423 new_entry->offset += new_adj; 424 425 if (entry->aref.ar_amap) 426 amap_splitref(&entry->aref, &new_entry->aref, new_adj); 427 428 uvm_map_entry_link(map, entry, new_entry); 429 430 if (UVM_ET_ISSUBMAP(entry)) { 431 /* ... unlikely to happen, but play it safe */ 432 uvm_map_reference(new_entry->object.sub_map); 433 } else { 434 if (UVM_ET_ISOBJ(entry) && 435 entry->object.uvm_obj->pgops && 436 entry->object.uvm_obj->pgops->pgo_reference) 437 entry->object.uvm_obj->pgops->pgo_reference( 438 entry->object.uvm_obj); 439 } 440 } 441 442 443 /* 444 * M A P - m a i n e n t r y p o i n t 445 */ 446 /* 447 * uvm_map: establish a valid mapping in a map 448 * 449 * => assume startp is page aligned. 450 * => assume size is a multiple of PAGE_SIZE. 451 * => assume sys_mmap provides enough of a "hint" to have us skip 452 * over text/data/bss area. 453 * => map must be unlocked (we will lock it) 454 * => <uobj,uoffset> value meanings (4 cases): 455 * [1] <NULL,uoffset> == uoffset is a hint for PMAP_PREFER 456 * [2] <NULL,UVM_UNKNOWN_OFFSET> == don't PMAP_PREFER 457 * [3] <uobj,uoffset> == normal mapping 458 * [4] <uobj,UVM_UNKNOWN_OFFSET> == uvm_map finds offset based on VA 459 * 460 * case [4] is for kernel mappings where we don't know the offset until 461 * we've found a virtual address. note that kernel object offsets are 462 * always relative to vm_map_min(kernel_map). 463 * => XXXCDC: need way to map in external amap? 464 */ 465 466 int 467 uvm_map(map, startp, size, uobj, uoffset, flags) 468 vm_map_t map; 469 vaddr_t *startp; /* IN/OUT */ 470 vsize_t size; 471 struct uvm_object *uobj; 472 voff_t uoffset; 473 uvm_flag_t flags; 474 { 475 vm_map_entry_t prev_entry, new_entry; 476 vm_prot_t prot = UVM_PROTECTION(flags), maxprot = 477 UVM_MAXPROTECTION(flags); 478 vm_inherit_t inherit = UVM_INHERIT(flags); 479 int advice = UVM_ADVICE(flags); 480 UVMHIST_FUNC("uvm_map"); 481 UVMHIST_CALLED(maphist); 482 483 UVMHIST_LOG(maphist, "(map=0x%x, *startp=0x%x, size=%d, flags=0x%x)", 484 map, *startp, size, flags); 485 UVMHIST_LOG(maphist, " uobj/offset 0x%x/%d", uobj, uoffset,0,0); 486 487 /* 488 * step 0: sanity check of protection code 489 */ 490 491 if ((prot & maxprot) != prot) { 492 UVMHIST_LOG(maphist, "<- prot. failure: prot=0x%x, max=0x%x", 493 prot, maxprot,0,0); 494 return(KERN_PROTECTION_FAILURE); 495 } 496 497 /* 498 * step 1: figure out where to put new VM range 499 */ 500 501 if (vm_map_lock_try(map) == FALSE) { 502 if (flags & UVM_FLAG_TRYLOCK) 503 return(KERN_FAILURE); 504 vm_map_lock(map); /* could sleep here */ 505 } 506 if ((prev_entry = uvm_map_findspace(map, *startp, size, startp, 507 uobj, uoffset, flags & UVM_FLAG_FIXED)) == NULL) { 508 UVMHIST_LOG(maphist,"<- uvm_map_findspace failed!",0,0,0,0); 509 vm_map_unlock(map); 510 return (KERN_NO_SPACE); 511 } 512 513 #ifdef PMAP_GROWKERNEL 514 { 515 /* 516 * If the kernel pmap can't map the requested space, 517 * then allocate more resources for it. 518 */ 519 if (map == kernel_map && uvm_maxkaddr < (*startp + size)) 520 uvm_maxkaddr = pmap_growkernel(*startp + size); 521 } 522 #endif 523 524 UVMCNT_INCR(uvm_map_call); 525 526 /* 527 * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER 528 * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET. in 529 * either case we want to zero it before storing it in the map entry 530 * (because it looks strange and confusing when debugging...) 531 * 532 * if uobj is not null 533 * if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping 534 * and we do not need to change uoffset. 535 * if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset 536 * now (based on the starting address of the map). this case is 537 * for kernel object mappings where we don't know the offset until 538 * the virtual address is found (with uvm_map_findspace). the 539 * offset is the distance we are from the start of the map. 540 */ 541 542 if (uobj == NULL) { 543 uoffset = 0; 544 } else { 545 if (uoffset == UVM_UNKNOWN_OFFSET) { 546 #ifdef DIAGNOSTIC 547 if (UVM_OBJ_IS_KERN_OBJECT(uobj) == 0) 548 panic("uvm_map: unknown offset with " 549 "non-kernel object"); 550 #endif 551 uoffset = *startp - vm_map_min(kernel_map); 552 } 553 } 554 555 /* 556 * step 2: try and insert in map by extending previous entry, if 557 * possible 558 * XXX: we don't try and pull back the next entry. might be useful 559 * for a stack, but we are currently allocating our stack in advance. 560 */ 561 562 if ((flags & UVM_FLAG_NOMERGE) == 0 && 563 prev_entry->end == *startp && prev_entry != &map->header && 564 prev_entry->object.uvm_obj == uobj) { 565 566 if (uobj && prev_entry->offset + 567 (prev_entry->end - prev_entry->start) != uoffset) 568 goto step3; 569 570 if (UVM_ET_ISSUBMAP(prev_entry)) 571 goto step3; 572 573 if (prev_entry->protection != prot || 574 prev_entry->max_protection != maxprot) 575 goto step3; 576 577 if (prev_entry->inheritance != inherit || 578 prev_entry->advice != advice) 579 goto step3; 580 581 /* wiring status must match (new area is unwired) */ 582 if (VM_MAPENT_ISWIRED(prev_entry)) 583 goto step3; 584 585 /* 586 * can't extend a shared amap. note: no need to lock amap to 587 * look at refs since we don't care about its exact value. 588 * if it is one (i.e. we have only reference) it will stay there 589 */ 590 591 if (prev_entry->aref.ar_amap && 592 amap_refs(prev_entry->aref.ar_amap) != 1) { 593 goto step3; 594 } 595 596 /* got it! */ 597 598 UVMCNT_INCR(map_backmerge); 599 UVMHIST_LOG(maphist," starting back merge", 0, 0, 0, 0); 600 601 /* 602 * drop our reference to uobj since we are extending a reference 603 * that we already have (the ref count can not drop to zero). 604 */ 605 if (uobj && uobj->pgops->pgo_detach) 606 uobj->pgops->pgo_detach(uobj); 607 608 if (prev_entry->aref.ar_amap) { 609 amap_extend(prev_entry, size); 610 } 611 612 prev_entry->end += size; 613 map->size += size; 614 615 UVMHIST_LOG(maphist,"<- done (via backmerge)!", 0, 0, 0, 0); 616 vm_map_unlock(map); 617 return (KERN_SUCCESS); 618 619 } 620 step3: 621 UVMHIST_LOG(maphist," allocating new map entry", 0, 0, 0, 0); 622 623 /* 624 * check for possible forward merge (which we don't do) and count 625 * the number of times we missed a *possible* chance to merge more 626 */ 627 628 if ((flags & UVM_FLAG_NOMERGE) == 0 && 629 prev_entry->next != &map->header && 630 prev_entry->next->start == (*startp + size)) 631 UVMCNT_INCR(map_forwmerge); 632 633 /* 634 * step 3: allocate new entry and link it in 635 */ 636 637 new_entry = uvm_mapent_alloc(map); 638 new_entry->start = *startp; 639 new_entry->end = new_entry->start + size; 640 new_entry->object.uvm_obj = uobj; 641 new_entry->offset = uoffset; 642 643 if (uobj) 644 new_entry->etype = UVM_ET_OBJ; 645 else 646 new_entry->etype = 0; 647 648 if (flags & UVM_FLAG_COPYONW) { 649 new_entry->etype |= UVM_ET_COPYONWRITE; 650 if ((flags & UVM_FLAG_OVERLAY) == 0) 651 new_entry->etype |= UVM_ET_NEEDSCOPY; 652 } 653 654 new_entry->protection = prot; 655 new_entry->max_protection = maxprot; 656 new_entry->inheritance = inherit; 657 new_entry->wired_count = 0; 658 new_entry->advice = advice; 659 if (flags & UVM_FLAG_OVERLAY) { 660 /* 661 * to_add: for BSS we overallocate a little since we 662 * are likely to extend 663 */ 664 vaddr_t to_add = (flags & UVM_FLAG_AMAPPAD) ? 665 UVM_AMAP_CHUNK << PAGE_SHIFT : 0; 666 struct vm_amap *amap = amap_alloc(size, to_add, M_WAITOK); 667 new_entry->aref.ar_pageoff = 0; 668 new_entry->aref.ar_amap = amap; 669 } else { 670 new_entry->aref.ar_pageoff = 0; 671 new_entry->aref.ar_amap = NULL; 672 } 673 674 uvm_map_entry_link(map, prev_entry, new_entry); 675 676 map->size += size; 677 678 /* 679 * Update the free space hint 680 */ 681 682 if ((map->first_free == prev_entry) && 683 (prev_entry->end >= new_entry->start)) 684 map->first_free = new_entry; 685 686 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0); 687 vm_map_unlock(map); 688 return(KERN_SUCCESS); 689 } 690 691 /* 692 * uvm_map_lookup_entry: find map entry at or before an address 693 * 694 * => map must at least be read-locked by caller 695 * => entry is returned in "entry" 696 * => return value is true if address is in the returned entry 697 */ 698 699 boolean_t 700 uvm_map_lookup_entry(map, address, entry) 701 vm_map_t map; 702 vaddr_t address; 703 vm_map_entry_t *entry; /* OUT */ 704 { 705 vm_map_entry_t cur; 706 vm_map_entry_t last; 707 UVMHIST_FUNC("uvm_map_lookup_entry"); 708 UVMHIST_CALLED(maphist); 709 710 UVMHIST_LOG(maphist,"(map=0x%x,addr=0x%x,ent=0x%x)", 711 map, address, entry, 0); 712 713 /* 714 * start looking either from the head of the 715 * list, or from the hint. 716 */ 717 718 simple_lock(&map->hint_lock); 719 cur = map->hint; 720 simple_unlock(&map->hint_lock); 721 722 if (cur == &map->header) 723 cur = cur->next; 724 725 UVMCNT_INCR(uvm_mlk_call); 726 if (address >= cur->start) { 727 /* 728 * go from hint to end of list. 729 * 730 * but first, make a quick check to see if 731 * we are already looking at the entry we 732 * want (which is usually the case). 733 * note also that we don't need to save the hint 734 * here... it is the same hint (unless we are 735 * at the header, in which case the hint didn't 736 * buy us anything anyway). 737 */ 738 last = &map->header; 739 if ((cur != last) && (cur->end > address)) { 740 UVMCNT_INCR(uvm_mlk_hint); 741 *entry = cur; 742 UVMHIST_LOG(maphist,"<- got it via hint (0x%x)", 743 cur, 0, 0, 0); 744 return (TRUE); 745 } 746 } else { 747 /* 748 * go from start to hint, *inclusively* 749 */ 750 last = cur->next; 751 cur = map->header.next; 752 } 753 754 /* 755 * search linearly 756 */ 757 758 while (cur != last) { 759 if (cur->end > address) { 760 if (address >= cur->start) { 761 /* 762 * save this lookup for future 763 * hints, and return 764 */ 765 766 *entry = cur; 767 SAVE_HINT(map, cur); 768 UVMHIST_LOG(maphist,"<- search got it (0x%x)", 769 cur, 0, 0, 0); 770 return (TRUE); 771 } 772 break; 773 } 774 cur = cur->next; 775 } 776 *entry = cur->prev; 777 SAVE_HINT(map, *entry); 778 UVMHIST_LOG(maphist,"<- failed!",0,0,0,0); 779 return (FALSE); 780 } 781 782 783 /* 784 * uvm_map_findspace: find "length" sized space in "map". 785 * 786 * => "hint" is a hint about where we want it, unless fixed is true 787 * (in which case we insist on using "hint"). 788 * => "result" is VA returned 789 * => uobj/uoffset are to be used to handle VAC alignment, if required 790 * => caller must at least have read-locked map 791 * => returns NULL on failure, or pointer to prev. map entry if success 792 * => note this is a cross between the old vm_map_findspace and vm_map_find 793 */ 794 795 vm_map_entry_t 796 uvm_map_findspace(map, hint, length, result, uobj, uoffset, fixed) 797 vm_map_t map; 798 vaddr_t hint; 799 vsize_t length; 800 vaddr_t *result; /* OUT */ 801 struct uvm_object *uobj; 802 voff_t uoffset; 803 boolean_t fixed; 804 { 805 vm_map_entry_t entry, next, tmp; 806 vaddr_t end; 807 UVMHIST_FUNC("uvm_map_findspace"); 808 UVMHIST_CALLED(maphist); 809 810 UVMHIST_LOG(maphist, "(map=0x%x, hint=0x%x, len=%d, fixed=%d)", 811 map, hint, length, fixed); 812 813 if (hint < map->min_offset) { /* check ranges ... */ 814 if (fixed) { 815 UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0); 816 return(NULL); 817 } 818 hint = map->min_offset; 819 } 820 if (hint > map->max_offset) { 821 UVMHIST_LOG(maphist,"<- VA 0x%x > range [0x%x->0x%x]", 822 hint, map->min_offset, map->max_offset, 0); 823 return(NULL); 824 } 825 826 /* 827 * Look for the first possible address; if there's already 828 * something at this address, we have to start after it. 829 */ 830 831 if (!fixed && hint == map->min_offset) { 832 if ((entry = map->first_free) != &map->header) 833 hint = entry->end; 834 } else { 835 if (uvm_map_lookup_entry(map, hint, &tmp)) { 836 /* "hint" address already in use ... */ 837 if (fixed) { 838 UVMHIST_LOG(maphist,"<- fixed & VA in use", 839 0, 0, 0, 0); 840 return(NULL); 841 } 842 hint = tmp->end; 843 } 844 entry = tmp; 845 } 846 847 /* 848 * Look through the rest of the map, trying to fit a new region in 849 * the gap between existing regions, or after the very last region. 850 * note: entry->end = base VA of current gap, 851 * next->start = VA of end of current gap 852 */ 853 for (;; hint = (entry = next)->end) { 854 /* 855 * Find the end of the proposed new region. Be sure we didn't 856 * go beyond the end of the map, or wrap around the address; 857 * if so, we lose. Otherwise, if this is the last entry, or 858 * if the proposed new region fits before the next entry, we 859 * win. 860 */ 861 862 #ifdef PMAP_PREFER 863 /* 864 * push hint forward as needed to avoid VAC alias problems. 865 * we only do this if a valid offset is specified. 866 */ 867 if (!fixed && uoffset != UVM_UNKNOWN_OFFSET) 868 PMAP_PREFER(uoffset, &hint); 869 #endif 870 end = hint + length; 871 if (end > map->max_offset || end < hint) { 872 UVMHIST_LOG(maphist,"<- failed (off end)", 0,0,0,0); 873 return (NULL); 874 } 875 next = entry->next; 876 if (next == &map->header || next->start >= end) 877 break; 878 if (fixed) { 879 UVMHIST_LOG(maphist,"<- fixed mapping failed", 0,0,0,0); 880 return(NULL); /* only one shot at it ... */ 881 } 882 } 883 SAVE_HINT(map, entry); 884 *result = hint; 885 UVMHIST_LOG(maphist,"<- got it! (result=0x%x)", hint, 0,0,0); 886 return (entry); 887 } 888 889 /* 890 * U N M A P - m a i n h e l p e r f u n c t i o n s 891 */ 892 893 /* 894 * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop") 895 * 896 * => caller must check alignment and size 897 * => map must be locked by caller 898 * => we return a list of map entries that we've remove from the map 899 * in "entry_list" 900 */ 901 902 int 903 uvm_unmap_remove(map, start, end, entry_list) 904 vm_map_t map; 905 vaddr_t start,end; 906 vm_map_entry_t *entry_list; /* OUT */ 907 { 908 vm_map_entry_t entry, first_entry, next; 909 vaddr_t len; 910 UVMHIST_FUNC("uvm_unmap_remove"); 911 UVMHIST_CALLED(maphist); 912 913 UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)", 914 map, start, end, 0); 915 916 VM_MAP_RANGE_CHECK(map, start, end); 917 918 /* 919 * find first entry 920 */ 921 if (uvm_map_lookup_entry(map, start, &first_entry) == TRUE) { 922 /* clip and go... */ 923 entry = first_entry; 924 UVM_MAP_CLIP_START(map, entry, start); 925 /* critical! prevents stale hint */ 926 SAVE_HINT(map, entry->prev); 927 928 } else { 929 entry = first_entry->next; 930 } 931 932 /* 933 * Save the free space hint 934 */ 935 936 if (map->first_free->start >= start) 937 map->first_free = entry->prev; 938 939 /* 940 * note: we now re-use first_entry for a different task. we remove 941 * a number of map entries from the map and save them in a linked 942 * list headed by "first_entry". once we remove them from the map 943 * the caller should unlock the map and drop the references to the 944 * backing objects [c.f. uvm_unmap_detach]. the object is to 945 * separate unmapping from reference dropping. why? 946 * [1] the map has to be locked for unmapping 947 * [2] the map need not be locked for reference dropping 948 * [3] dropping references may trigger pager I/O, and if we hit 949 * a pager that does synchronous I/O we may have to wait for it. 950 * [4] we would like all waiting for I/O to occur with maps unlocked 951 * so that we don't block other threads. 952 */ 953 first_entry = NULL; 954 *entry_list = NULL; /* to be safe */ 955 956 /* 957 * break up the area into map entry sized regions and unmap. note 958 * that all mappings have to be removed before we can even consider 959 * dropping references to amaps or VM objects (otherwise we could end 960 * up with a mapping to a page on the free list which would be very bad) 961 */ 962 963 while ((entry != &map->header) && (entry->start < end)) { 964 965 UVM_MAP_CLIP_END(map, entry, end); 966 next = entry->next; 967 len = entry->end - entry->start; 968 969 /* 970 * unwire before removing addresses from the pmap; otherwise 971 * unwiring will put the entries back into the pmap (XXX). 972 */ 973 974 if (VM_MAPENT_ISWIRED(entry)) 975 uvm_map_entry_unwire(map, entry); 976 977 /* 978 * special case: handle mappings to anonymous kernel objects. 979 * we want to free these pages right away... 980 */ 981 if (UVM_ET_ISOBJ(entry) && 982 UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) { 983 #ifdef DIAGNOSTIC 984 if (vm_map_pmap(map) != pmap_kernel()) 985 panic("uvm_unmap_remove: kernel object " 986 "mapped by non-kernel map"); 987 #endif 988 989 /* 990 * note: kernel object mappings are currently used in 991 * two ways: 992 * [1] "normal" mappings of pages in the kernel object 993 * [2] uvm_km_valloc'd allocations in which we 994 * pmap_enter in some non-kernel-object page 995 * (e.g. vmapbuf). 996 * 997 * for case [1], we need to remove the mapping from 998 * the pmap and then remove the page from the kernel 999 * object (because, once pages in a kernel object are 1000 * unmapped they are no longer needed, unlike, say, 1001 * a vnode where you might want the data to persist 1002 * until flushed out of a queue). 1003 * 1004 * for case [2], we need to remove the mapping from 1005 * the pmap. there shouldn't be any pages at the 1006 * specified offset in the kernel object [but it 1007 * doesn't hurt to call uvm_km_pgremove just to be 1008 * safe?] 1009 * 1010 * uvm_km_pgremove currently does the following: 1011 * for pages in the kernel object in range: 1012 * - drops the swap slot 1013 * - uvm_pagefree the page 1014 * 1015 * note there is version of uvm_km_pgremove() that 1016 * is used for "intrsafe" objects. 1017 */ 1018 1019 /* 1020 * remove mappings from pmap and drop the pages 1021 * from the object. offsets are always relative 1022 * to vm_map_min(kernel_map). 1023 */ 1024 if (UVM_OBJ_IS_INTRSAFE_OBJECT(entry->object.uvm_obj)) { 1025 pmap_kremove(entry->start, len); 1026 uvm_km_pgremove_intrsafe(entry->object.uvm_obj, 1027 entry->start - vm_map_min(kernel_map), 1028 entry->end - vm_map_min(kernel_map)); 1029 } else { 1030 pmap_remove(pmap_kernel(), entry->start, 1031 entry->start + len); 1032 uvm_km_pgremove(entry->object.uvm_obj, 1033 entry->start - vm_map_min(kernel_map), 1034 entry->end - vm_map_min(kernel_map)); 1035 } 1036 1037 /* 1038 * null out kernel_object reference, we've just 1039 * dropped it 1040 */ 1041 entry->etype &= ~UVM_ET_OBJ; 1042 entry->object.uvm_obj = NULL; /* to be safe */ 1043 1044 } else { 1045 /* 1046 * remove mappings the standard way. 1047 */ 1048 pmap_remove(map->pmap, entry->start, entry->end); 1049 } 1050 1051 /* 1052 * remove entry from map and put it on our list of entries 1053 * that we've nuked. then go do next entry. 1054 */ 1055 UVMHIST_LOG(maphist, " removed map entry 0x%x", entry, 0, 0,0); 1056 1057 /* critical! prevents stale hint */ 1058 /* XXX: need SAVE_HINT with three parms */ 1059 simple_lock(&map->hint_lock); 1060 if (map->hint == entry) 1061 map->hint = entry->prev; 1062 simple_unlock(&map->hint_lock); 1063 1064 uvm_map_entry_unlink(map, entry); 1065 map->size -= len; 1066 entry->next = first_entry; 1067 first_entry = entry; 1068 entry = next; /* next entry, please */ 1069 } 1070 1071 /* 1072 * now we've cleaned up the map and are ready for the caller to drop 1073 * references to the mapped objects. 1074 */ 1075 1076 *entry_list = first_entry; 1077 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0); 1078 return(KERN_SUCCESS); 1079 } 1080 1081 /* 1082 * uvm_unmap_detach: drop references in a chain of map entries 1083 * 1084 * => we will free the map entries as we traverse the list. 1085 */ 1086 1087 void 1088 uvm_unmap_detach(first_entry, amap_unref_flags) 1089 vm_map_entry_t first_entry; 1090 int amap_unref_flags; 1091 { 1092 vm_map_entry_t next_entry; 1093 UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist); 1094 1095 while (first_entry) { 1096 1097 #ifdef DIAGNOSTIC 1098 /* 1099 * sanity check 1100 */ 1101 /* was part of vm_map_entry_delete() */ 1102 if (VM_MAPENT_ISWIRED(first_entry)) 1103 panic("unmap: still wired!"); 1104 #endif 1105 1106 UVMHIST_LOG(maphist, 1107 " detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d", 1108 first_entry, first_entry->aref.ar_amap, 1109 first_entry->object.uvm_obj, 1110 UVM_ET_ISSUBMAP(first_entry)); 1111 1112 /* 1113 * drop reference to amap, if we've got one 1114 */ 1115 1116 if (first_entry->aref.ar_amap) 1117 amap_unref(first_entry, amap_unref_flags); 1118 1119 /* 1120 * drop reference to our backing object, if we've got one 1121 */ 1122 1123 if (UVM_ET_ISSUBMAP(first_entry)) { 1124 /* ... unlikely to happen, but play it safe */ 1125 uvm_map_deallocate(first_entry->object.sub_map); 1126 } else { 1127 if (UVM_ET_ISOBJ(first_entry) && 1128 first_entry->object.uvm_obj->pgops->pgo_detach) 1129 first_entry->object.uvm_obj->pgops-> 1130 pgo_detach(first_entry->object.uvm_obj); 1131 } 1132 1133 /* 1134 * next entry 1135 */ 1136 next_entry = first_entry->next; 1137 uvm_mapent_free(first_entry); 1138 first_entry = next_entry; 1139 } 1140 1141 /* 1142 * done! 1143 */ 1144 UVMHIST_LOG(maphist, "<- done", 0,0,0,0); 1145 return; 1146 } 1147 1148 /* 1149 * E X T R A C T I O N F U N C T I O N S 1150 */ 1151 1152 /* 1153 * uvm_map_reserve: reserve space in a vm_map for future use. 1154 * 1155 * => we reserve space in a map by putting a dummy map entry in the 1156 * map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE) 1157 * => map should be unlocked (we will write lock it) 1158 * => we return true if we were able to reserve space 1159 * => XXXCDC: should be inline? 1160 */ 1161 1162 int 1163 uvm_map_reserve(map, size, offset, raddr) 1164 vm_map_t map; 1165 vsize_t size; 1166 vaddr_t offset; /* hint for pmap_prefer */ 1167 vaddr_t *raddr; /* IN:hint, OUT: reserved VA */ 1168 { 1169 UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist); 1170 1171 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)", 1172 map,size,offset,raddr); 1173 1174 size = round_page(size); 1175 if (*raddr < vm_map_min(map)) 1176 *raddr = vm_map_min(map); /* hint */ 1177 1178 /* 1179 * reserve some virtual space. 1180 */ 1181 1182 if (uvm_map(map, raddr, size, NULL, offset, 1183 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE, 1184 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != KERN_SUCCESS) { 1185 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0); 1186 return (FALSE); 1187 } 1188 1189 UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0); 1190 return (TRUE); 1191 } 1192 1193 /* 1194 * uvm_map_replace: replace a reserved (blank) area of memory with 1195 * real mappings. 1196 * 1197 * => caller must WRITE-LOCK the map 1198 * => we return TRUE if replacement was a success 1199 * => we expect the newents chain to have nnewents entrys on it and 1200 * we expect newents->prev to point to the last entry on the list 1201 * => note newents is allowed to be NULL 1202 */ 1203 1204 int 1205 uvm_map_replace(map, start, end, newents, nnewents) 1206 struct vm_map *map; 1207 vaddr_t start, end; 1208 vm_map_entry_t newents; 1209 int nnewents; 1210 { 1211 vm_map_entry_t oldent, last; 1212 UVMHIST_FUNC("uvm_map_replace"); 1213 UVMHIST_CALLED(maphist); 1214 1215 /* 1216 * first find the blank map entry at the specified address 1217 */ 1218 1219 if (!uvm_map_lookup_entry(map, start, &oldent)) { 1220 return(FALSE); 1221 } 1222 1223 /* 1224 * check to make sure we have a proper blank entry 1225 */ 1226 1227 if (oldent->start != start || oldent->end != end || 1228 oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) { 1229 return (FALSE); 1230 } 1231 1232 #ifdef DIAGNOSTIC 1233 /* 1234 * sanity check the newents chain 1235 */ 1236 { 1237 vm_map_entry_t tmpent = newents; 1238 int nent = 0; 1239 vaddr_t cur = start; 1240 1241 while (tmpent) { 1242 nent++; 1243 if (tmpent->start < cur) 1244 panic("uvm_map_replace1"); 1245 if (tmpent->start > tmpent->end || tmpent->end > end) { 1246 printf("tmpent->start=0x%lx, tmpent->end=0x%lx, end=0x%lx\n", 1247 tmpent->start, tmpent->end, end); 1248 panic("uvm_map_replace2"); 1249 } 1250 cur = tmpent->end; 1251 if (tmpent->next) { 1252 if (tmpent->next->prev != tmpent) 1253 panic("uvm_map_replace3"); 1254 } else { 1255 if (newents->prev != tmpent) 1256 panic("uvm_map_replace4"); 1257 } 1258 tmpent = tmpent->next; 1259 } 1260 if (nent != nnewents) 1261 panic("uvm_map_replace5"); 1262 } 1263 #endif 1264 1265 /* 1266 * map entry is a valid blank! replace it. (this does all the 1267 * work of map entry link/unlink...). 1268 */ 1269 1270 if (newents) { 1271 1272 last = newents->prev; /* we expect this */ 1273 1274 /* critical: flush stale hints out of map */ 1275 SAVE_HINT(map, newents); 1276 if (map->first_free == oldent) 1277 map->first_free = last; 1278 1279 last->next = oldent->next; 1280 last->next->prev = last; 1281 newents->prev = oldent->prev; 1282 newents->prev->next = newents; 1283 map->nentries = map->nentries + (nnewents - 1); 1284 1285 } else { 1286 1287 /* critical: flush stale hints out of map */ 1288 SAVE_HINT(map, oldent->prev); 1289 if (map->first_free == oldent) 1290 map->first_free = oldent->prev; 1291 1292 /* NULL list of new entries: just remove the old one */ 1293 uvm_map_entry_unlink(map, oldent); 1294 } 1295 1296 1297 /* 1298 * now we can free the old blank entry, unlock the map and return. 1299 */ 1300 1301 uvm_mapent_free(oldent); 1302 return(TRUE); 1303 } 1304 1305 /* 1306 * uvm_map_extract: extract a mapping from a map and put it somewhere 1307 * (maybe removing the old mapping) 1308 * 1309 * => maps should be unlocked (we will write lock them) 1310 * => returns 0 on success, error code otherwise 1311 * => start must be page aligned 1312 * => len must be page sized 1313 * => flags: 1314 * UVM_EXTRACT_REMOVE: remove mappings from srcmap 1315 * UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only) 1316 * UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs 1317 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go 1318 * >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<< 1319 * >>>NOTE: QREF's must be unmapped via the QREF path, thus should only 1320 * be used from within the kernel in a kernel level map <<< 1321 */ 1322 1323 int 1324 uvm_map_extract(srcmap, start, len, dstmap, dstaddrp, flags) 1325 vm_map_t srcmap, dstmap; 1326 vaddr_t start, *dstaddrp; 1327 vsize_t len; 1328 int flags; 1329 { 1330 vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge, 1331 oldstart; 1332 vm_map_entry_t chain, endchain, entry, orig_entry, newentry, deadentry; 1333 vm_map_entry_t oldentry; 1334 vsize_t elen; 1335 int nchain, error, copy_ok; 1336 UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist); 1337 UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start, 1338 len,0); 1339 UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0); 1340 1341 #ifdef DIAGNOSTIC 1342 /* 1343 * step 0: sanity check: start must be on a page boundary, length 1344 * must be page sized. can't ask for CONTIG/QREF if you asked for 1345 * REMOVE. 1346 */ 1347 if ((start & PAGE_MASK) || (len & PAGE_MASK)) 1348 panic("uvm_map_extract1"); 1349 if (flags & UVM_EXTRACT_REMOVE) 1350 if (flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) 1351 panic("uvm_map_extract2"); 1352 #endif 1353 1354 1355 /* 1356 * step 1: reserve space in the target map for the extracted area 1357 */ 1358 1359 dstaddr = vm_map_min(dstmap); 1360 if (uvm_map_reserve(dstmap, len, start, &dstaddr) == FALSE) 1361 return(ENOMEM); 1362 *dstaddrp = dstaddr; /* pass address back to caller */ 1363 UVMHIST_LOG(maphist, " dstaddr=0x%x", dstaddr,0,0,0); 1364 1365 1366 /* 1367 * step 2: setup for the extraction process loop by init'ing the 1368 * map entry chain, locking src map, and looking up the first useful 1369 * entry in the map. 1370 */ 1371 1372 end = start + len; 1373 newend = dstaddr + len; 1374 chain = endchain = NULL; 1375 nchain = 0; 1376 vm_map_lock(srcmap); 1377 1378 if (uvm_map_lookup_entry(srcmap, start, &entry)) { 1379 1380 /* "start" is within an entry */ 1381 if (flags & UVM_EXTRACT_QREF) { 1382 /* 1383 * for quick references we don't clip the entry, so 1384 * the entry may map space "before" the starting 1385 * virtual address... this is the "fudge" factor 1386 * (which can be non-zero only the first time 1387 * through the "while" loop in step 3). 1388 */ 1389 fudge = start - entry->start; 1390 } else { 1391 /* 1392 * normal reference: we clip the map to fit (thus 1393 * fudge is zero) 1394 */ 1395 UVM_MAP_CLIP_START(srcmap, entry, start); 1396 SAVE_HINT(srcmap, entry->prev); 1397 fudge = 0; 1398 } 1399 1400 } else { 1401 1402 /* "start" is not within an entry ... skip to next entry */ 1403 if (flags & UVM_EXTRACT_CONTIG) { 1404 error = EINVAL; 1405 goto bad; /* definite hole here ... */ 1406 } 1407 1408 entry = entry->next; 1409 fudge = 0; 1410 } 1411 /* save values from srcmap for step 6 */ 1412 orig_entry = entry; 1413 orig_fudge = fudge; 1414 1415 1416 /* 1417 * step 3: now start looping through the map entries, extracting 1418 * as we go. 1419 */ 1420 1421 while (entry->start < end && entry != &srcmap->header) { 1422 1423 /* if we are not doing a quick reference, clip it */ 1424 if ((flags & UVM_EXTRACT_QREF) == 0) 1425 UVM_MAP_CLIP_END(srcmap, entry, end); 1426 1427 /* clear needs_copy (allow chunking) */ 1428 if (UVM_ET_ISNEEDSCOPY(entry)) { 1429 if (fudge) 1430 oldstart = entry->start; 1431 else 1432 oldstart = 0; /* XXX: gcc */ 1433 amap_copy(srcmap, entry, M_NOWAIT, TRUE, start, end); 1434 if (UVM_ET_ISNEEDSCOPY(entry)) { /* failed? */ 1435 error = ENOMEM; 1436 goto bad; 1437 } 1438 /* amap_copy could clip (during chunk)! update fudge */ 1439 if (fudge) { 1440 fudge = fudge - (entry->start - oldstart); 1441 orig_fudge = fudge; 1442 } 1443 } 1444 1445 /* calculate the offset of this from "start" */ 1446 oldoffset = (entry->start + fudge) - start; 1447 1448 /* allocate a new map entry */ 1449 newentry = uvm_mapent_alloc(dstmap); 1450 if (newentry == NULL) { 1451 error = ENOMEM; 1452 goto bad; 1453 } 1454 1455 /* set up new map entry */ 1456 newentry->next = NULL; 1457 newentry->prev = endchain; 1458 newentry->start = dstaddr + oldoffset; 1459 newentry->end = 1460 newentry->start + (entry->end - (entry->start + fudge)); 1461 if (newentry->end > newend || newentry->end < newentry->start) 1462 newentry->end = newend; 1463 newentry->object.uvm_obj = entry->object.uvm_obj; 1464 if (newentry->object.uvm_obj) { 1465 if (newentry->object.uvm_obj->pgops->pgo_reference) 1466 newentry->object.uvm_obj->pgops-> 1467 pgo_reference(newentry->object.uvm_obj); 1468 newentry->offset = entry->offset + fudge; 1469 } else { 1470 newentry->offset = 0; 1471 } 1472 newentry->etype = entry->etype; 1473 newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ? 1474 entry->max_protection : entry->protection; 1475 newentry->max_protection = entry->max_protection; 1476 newentry->inheritance = entry->inheritance; 1477 newentry->wired_count = 0; 1478 newentry->aref.ar_amap = entry->aref.ar_amap; 1479 if (newentry->aref.ar_amap) { 1480 newentry->aref.ar_pageoff = 1481 entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT); 1482 amap_ref(newentry, AMAP_SHARED | 1483 ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0)); 1484 } else { 1485 newentry->aref.ar_pageoff = 0; 1486 } 1487 newentry->advice = entry->advice; 1488 1489 /* now link it on the chain */ 1490 nchain++; 1491 if (endchain == NULL) { 1492 chain = endchain = newentry; 1493 } else { 1494 endchain->next = newentry; 1495 endchain = newentry; 1496 } 1497 1498 /* end of 'while' loop! */ 1499 if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end && 1500 (entry->next == &srcmap->header || 1501 entry->next->start != entry->end)) { 1502 error = EINVAL; 1503 goto bad; 1504 } 1505 entry = entry->next; 1506 fudge = 0; 1507 } 1508 1509 1510 /* 1511 * step 4: close off chain (in format expected by uvm_map_replace) 1512 */ 1513 1514 if (chain) 1515 chain->prev = endchain; 1516 1517 1518 /* 1519 * step 5: attempt to lock the dest map so we can pmap_copy. 1520 * note usage of copy_ok: 1521 * 1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5) 1522 * 0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7 1523 */ 1524 1525 if (srcmap == dstmap || vm_map_lock_try(dstmap) == TRUE) { 1526 1527 copy_ok = 1; 1528 if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain, 1529 nchain)) { 1530 if (srcmap != dstmap) 1531 vm_map_unlock(dstmap); 1532 error = EIO; 1533 goto bad; 1534 } 1535 1536 } else { 1537 1538 copy_ok = 0; 1539 /* replace defered until step 7 */ 1540 1541 } 1542 1543 1544 /* 1545 * step 6: traverse the srcmap a second time to do the following: 1546 * - if we got a lock on the dstmap do pmap_copy 1547 * - if UVM_EXTRACT_REMOVE remove the entries 1548 * we make use of orig_entry and orig_fudge (saved in step 2) 1549 */ 1550 1551 if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) { 1552 1553 /* purge possible stale hints from srcmap */ 1554 if (flags & UVM_EXTRACT_REMOVE) { 1555 SAVE_HINT(srcmap, orig_entry->prev); 1556 if (srcmap->first_free->start >= start) 1557 srcmap->first_free = orig_entry->prev; 1558 } 1559 1560 entry = orig_entry; 1561 fudge = orig_fudge; 1562 deadentry = NULL; /* for UVM_EXTRACT_REMOVE */ 1563 1564 while (entry->start < end && entry != &srcmap->header) { 1565 1566 if (copy_ok) { 1567 oldoffset = (entry->start + fudge) - start; 1568 elen = min(end, entry->end) - 1569 (entry->start + fudge); 1570 pmap_copy(dstmap->pmap, srcmap->pmap, 1571 dstaddr + oldoffset, elen, 1572 entry->start + fudge); 1573 } 1574 1575 /* we advance "entry" in the following if statement */ 1576 if (flags & UVM_EXTRACT_REMOVE) { 1577 pmap_remove(srcmap->pmap, entry->start, 1578 entry->end); 1579 oldentry = entry; /* save entry */ 1580 entry = entry->next; /* advance */ 1581 uvm_map_entry_unlink(srcmap, oldentry); 1582 /* add to dead list */ 1583 oldentry->next = deadentry; 1584 deadentry = oldentry; 1585 } else { 1586 entry = entry->next; /* advance */ 1587 } 1588 1589 /* end of 'while' loop */ 1590 fudge = 0; 1591 } 1592 1593 /* 1594 * unlock dstmap. we will dispose of deadentry in 1595 * step 7 if needed 1596 */ 1597 if (copy_ok && srcmap != dstmap) 1598 vm_map_unlock(dstmap); 1599 1600 } 1601 else 1602 deadentry = NULL; /* XXX: gcc */ 1603 1604 /* 1605 * step 7: we are done with the source map, unlock. if copy_ok 1606 * is 0 then we have not replaced the dummy mapping in dstmap yet 1607 * and we need to do so now. 1608 */ 1609 1610 vm_map_unlock(srcmap); 1611 if ((flags & UVM_EXTRACT_REMOVE) && deadentry) 1612 uvm_unmap_detach(deadentry, 0); /* dispose of old entries */ 1613 1614 /* now do the replacement if we didn't do it in step 5 */ 1615 if (copy_ok == 0) { 1616 vm_map_lock(dstmap); 1617 error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain, 1618 nchain); 1619 vm_map_unlock(dstmap); 1620 1621 if (error == FALSE) { 1622 error = EIO; 1623 goto bad2; 1624 } 1625 } 1626 1627 /* 1628 * done! 1629 */ 1630 return(0); 1631 1632 /* 1633 * bad: failure recovery 1634 */ 1635 bad: 1636 vm_map_unlock(srcmap); 1637 bad2: /* src already unlocked */ 1638 if (chain) 1639 uvm_unmap_detach(chain, 1640 (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0); 1641 uvm_unmap(dstmap, dstaddr, dstaddr+len); /* ??? */ 1642 return(error); 1643 } 1644 1645 /* end of extraction functions */ 1646 1647 /* 1648 * uvm_map_submap: punch down part of a map into a submap 1649 * 1650 * => only the kernel_map is allowed to be submapped 1651 * => the purpose of submapping is to break up the locking granularity 1652 * of a larger map 1653 * => the range specified must have been mapped previously with a uvm_map() 1654 * call [with uobj==NULL] to create a blank map entry in the main map. 1655 * [And it had better still be blank!] 1656 * => maps which contain submaps should never be copied or forked. 1657 * => to remove a submap, use uvm_unmap() on the main map 1658 * and then uvm_map_deallocate() the submap. 1659 * => main map must be unlocked. 1660 * => submap must have been init'd and have a zero reference count. 1661 * [need not be locked as we don't actually reference it] 1662 */ 1663 1664 int 1665 uvm_map_submap(map, start, end, submap) 1666 vm_map_t map, submap; 1667 vaddr_t start, end; 1668 { 1669 vm_map_entry_t entry; 1670 int result; 1671 UVMHIST_FUNC("uvm_map_submap"); UVMHIST_CALLED(maphist); 1672 1673 vm_map_lock(map); 1674 1675 VM_MAP_RANGE_CHECK(map, start, end); 1676 1677 if (uvm_map_lookup_entry(map, start, &entry)) { 1678 UVM_MAP_CLIP_START(map, entry, start); 1679 UVM_MAP_CLIP_END(map, entry, end); /* to be safe */ 1680 } 1681 else { 1682 entry = NULL; 1683 } 1684 1685 if (entry != NULL && 1686 entry->start == start && entry->end == end && 1687 entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL && 1688 !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) { 1689 1690 /* 1691 * doit! 1692 */ 1693 entry->etype |= UVM_ET_SUBMAP; 1694 entry->object.sub_map = submap; 1695 entry->offset = 0; 1696 uvm_map_reference(submap); 1697 result = KERN_SUCCESS; 1698 } else { 1699 result = KERN_INVALID_ARGUMENT; 1700 } 1701 vm_map_unlock(map); 1702 1703 return(result); 1704 } 1705 1706 1707 /* 1708 * uvm_map_protect: change map protection 1709 * 1710 * => set_max means set max_protection. 1711 * => map must be unlocked. 1712 */ 1713 1714 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \ 1715 ~VM_PROT_WRITE : VM_PROT_ALL) 1716 #define max(a,b) ((a) > (b) ? (a) : (b)) 1717 1718 int 1719 uvm_map_protect(map, start, end, new_prot, set_max) 1720 vm_map_t map; 1721 vaddr_t start, end; 1722 vm_prot_t new_prot; 1723 boolean_t set_max; 1724 { 1725 vm_map_entry_t current, entry; 1726 int rv = KERN_SUCCESS; 1727 UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist); 1728 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)", 1729 map, start, end, new_prot); 1730 1731 vm_map_lock(map); 1732 1733 VM_MAP_RANGE_CHECK(map, start, end); 1734 1735 if (uvm_map_lookup_entry(map, start, &entry)) { 1736 UVM_MAP_CLIP_START(map, entry, start); 1737 } else { 1738 entry = entry->next; 1739 } 1740 1741 /* 1742 * make a first pass to check for protection violations. 1743 */ 1744 1745 current = entry; 1746 while ((current != &map->header) && (current->start < end)) { 1747 if (UVM_ET_ISSUBMAP(current)) { 1748 rv = KERN_INVALID_ARGUMENT; 1749 goto out; 1750 } 1751 if ((new_prot & current->max_protection) != new_prot) { 1752 rv = KERN_PROTECTION_FAILURE; 1753 goto out; 1754 } 1755 current = current->next; 1756 } 1757 1758 /* go back and fix up protections (no need to clip this time). */ 1759 1760 current = entry; 1761 1762 while ((current != &map->header) && (current->start < end)) { 1763 vm_prot_t old_prot; 1764 1765 UVM_MAP_CLIP_END(map, current, end); 1766 1767 old_prot = current->protection; 1768 if (set_max) 1769 current->protection = 1770 (current->max_protection = new_prot) & old_prot; 1771 else 1772 current->protection = new_prot; 1773 1774 /* 1775 * update physical map if necessary. worry about copy-on-write 1776 * here -- CHECK THIS XXX 1777 */ 1778 1779 if (current->protection != old_prot) { 1780 /* update pmap! */ 1781 pmap_protect(map->pmap, current->start, current->end, 1782 current->protection & MASK(entry)); 1783 } 1784 1785 /* 1786 * If the map is configured to lock any future mappings, 1787 * wire this entry now if the old protection was VM_PROT_NONE 1788 * and the new protection is not VM_PROT_NONE. 1789 */ 1790 1791 if ((map->flags & VM_MAP_WIREFUTURE) != 0 && 1792 VM_MAPENT_ISWIRED(entry) == 0 && 1793 old_prot == VM_PROT_NONE && 1794 new_prot != VM_PROT_NONE) { 1795 if (uvm_map_pageable(map, entry->start, 1796 entry->end, FALSE, 1797 UVM_LK_ENTER|UVM_LK_EXIT) != KERN_SUCCESS) { 1798 /* 1799 * If locking the entry fails, remember the 1800 * error if it's the first one. Note we 1801 * still continue setting the protection in 1802 * the map, but will return the resource 1803 * shortage condition regardless. 1804 * 1805 * XXX Ignore what the actual error is, 1806 * XXX just call it a resource shortage 1807 * XXX so that it doesn't get confused 1808 * XXX what uvm_map_protect() itself would 1809 * XXX normally return. 1810 */ 1811 rv = KERN_RESOURCE_SHORTAGE; 1812 } 1813 } 1814 1815 current = current->next; 1816 } 1817 1818 out: 1819 vm_map_unlock(map); 1820 UVMHIST_LOG(maphist, "<- done, rv=%d",rv,0,0,0); 1821 return (rv); 1822 } 1823 1824 #undef max 1825 #undef MASK 1826 1827 /* 1828 * uvm_map_inherit: set inheritance code for range of addrs in map. 1829 * 1830 * => map must be unlocked 1831 * => note that the inherit code is used during a "fork". see fork 1832 * code for details. 1833 */ 1834 1835 int 1836 uvm_map_inherit(map, start, end, new_inheritance) 1837 vm_map_t map; 1838 vaddr_t start; 1839 vaddr_t end; 1840 vm_inherit_t new_inheritance; 1841 { 1842 vm_map_entry_t entry, temp_entry; 1843 UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist); 1844 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)", 1845 map, start, end, new_inheritance); 1846 1847 switch (new_inheritance) { 1848 case VM_INHERIT_NONE: 1849 case VM_INHERIT_COPY: 1850 case VM_INHERIT_SHARE: 1851 break; 1852 default: 1853 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0); 1854 return (KERN_INVALID_ARGUMENT); 1855 } 1856 1857 vm_map_lock(map); 1858 1859 VM_MAP_RANGE_CHECK(map, start, end); 1860 1861 if (uvm_map_lookup_entry(map, start, &temp_entry)) { 1862 entry = temp_entry; 1863 UVM_MAP_CLIP_START(map, entry, start); 1864 } else { 1865 entry = temp_entry->next; 1866 } 1867 1868 while ((entry != &map->header) && (entry->start < end)) { 1869 UVM_MAP_CLIP_END(map, entry, end); 1870 1871 entry->inheritance = new_inheritance; 1872 1873 entry = entry->next; 1874 } 1875 1876 vm_map_unlock(map); 1877 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0); 1878 return(KERN_SUCCESS); 1879 } 1880 1881 /* 1882 * uvm_map_advice: set advice code for range of addrs in map. 1883 * 1884 * => map must be unlocked 1885 */ 1886 1887 int 1888 uvm_map_advice(map, start, end, new_advice) 1889 vm_map_t map; 1890 vaddr_t start; 1891 vaddr_t end; 1892 int new_advice; 1893 { 1894 vm_map_entry_t entry, temp_entry; 1895 UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist); 1896 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)", 1897 map, start, end, new_advice); 1898 1899 vm_map_lock(map); 1900 1901 VM_MAP_RANGE_CHECK(map, start, end); 1902 1903 if (uvm_map_lookup_entry(map, start, &temp_entry)) { 1904 entry = temp_entry; 1905 UVM_MAP_CLIP_START(map, entry, start); 1906 } else { 1907 entry = temp_entry->next; 1908 } 1909 1910 /* 1911 * XXXJRT: disallow holes? 1912 */ 1913 1914 while ((entry != &map->header) && (entry->start < end)) { 1915 UVM_MAP_CLIP_END(map, entry, end); 1916 1917 switch (new_advice) { 1918 case MADV_NORMAL: 1919 case MADV_RANDOM: 1920 case MADV_SEQUENTIAL: 1921 /* nothing special here */ 1922 break; 1923 1924 default: 1925 vm_map_unlock(map); 1926 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0); 1927 return (KERN_INVALID_ARGUMENT); 1928 } 1929 1930 1931 entry->advice = new_advice; 1932 1933 entry = entry->next; 1934 } 1935 1936 vm_map_unlock(map); 1937 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0); 1938 return (KERN_SUCCESS); 1939 } 1940 1941 /* 1942 * uvm_map_pageable: sets the pageability of a range in a map. 1943 * 1944 * => wires map entries. should not be used for transient page locking. 1945 * for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()). 1946 * => regions sepcified as not pageable require lock-down (wired) memory 1947 * and page tables. 1948 * => map must never be read-locked 1949 * => if islocked is TRUE, map is already write-locked 1950 * => we always unlock the map, since we must downgrade to a read-lock 1951 * to call uvm_fault_wire() 1952 * => XXXCDC: check this and try and clean it up. 1953 */ 1954 1955 int 1956 uvm_map_pageable(map, start, end, new_pageable, lockflags) 1957 vm_map_t map; 1958 vaddr_t start, end; 1959 boolean_t new_pageable; 1960 int lockflags; 1961 { 1962 vm_map_entry_t entry, start_entry, failed_entry; 1963 int rv; 1964 #ifdef DIAGNOSTIC 1965 u_int timestamp_save; 1966 #endif 1967 UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist); 1968 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)", 1969 map, start, end, new_pageable); 1970 1971 #ifdef DIAGNOSTIC 1972 if ((map->flags & VM_MAP_PAGEABLE) == 0) 1973 panic("uvm_map_pageable: map %p not pageable", map); 1974 #endif 1975 1976 if ((lockflags & UVM_LK_ENTER) == 0) 1977 vm_map_lock(map); 1978 1979 VM_MAP_RANGE_CHECK(map, start, end); 1980 1981 /* 1982 * only one pageability change may take place at one time, since 1983 * uvm_fault_wire assumes it will be called only once for each 1984 * wiring/unwiring. therefore, we have to make sure we're actually 1985 * changing the pageability for the entire region. we do so before 1986 * making any changes. 1987 */ 1988 1989 if (uvm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1990 if ((lockflags & UVM_LK_EXIT) == 0) 1991 vm_map_unlock(map); 1992 1993 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0); 1994 return (KERN_INVALID_ADDRESS); 1995 } 1996 entry = start_entry; 1997 1998 /* 1999 * handle wiring and unwiring separately. 2000 */ 2001 2002 if (new_pageable) { /* unwire */ 2003 UVM_MAP_CLIP_START(map, entry, start); 2004 /* 2005 * unwiring. first ensure that the range to be unwired is 2006 * really wired down and that there are no holes. 2007 */ 2008 while ((entry != &map->header) && (entry->start < end)) { 2009 if (entry->wired_count == 0 || 2010 (entry->end < end && 2011 (entry->next == &map->header || 2012 entry->next->start > entry->end))) { 2013 if ((lockflags & UVM_LK_EXIT) == 0) 2014 vm_map_unlock(map); 2015 UVMHIST_LOG(maphist, 2016 "<- done (INVALID UNWIRE ARG)",0,0,0,0); 2017 return (KERN_INVALID_ARGUMENT); 2018 } 2019 entry = entry->next; 2020 } 2021 2022 /* 2023 * POSIX 1003.1b - a single munlock call unlocks a region, 2024 * regardless of the number of mlock calls made on that 2025 * region. 2026 */ 2027 entry = start_entry; 2028 while ((entry != &map->header) && (entry->start < end)) { 2029 UVM_MAP_CLIP_END(map, entry, end); 2030 if (VM_MAPENT_ISWIRED(entry)) 2031 uvm_map_entry_unwire(map, entry); 2032 entry = entry->next; 2033 } 2034 if ((lockflags & UVM_LK_EXIT) == 0) 2035 vm_map_unlock(map); 2036 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0); 2037 return(KERN_SUCCESS); 2038 2039 /* 2040 * end of unwire case! 2041 */ 2042 } 2043 2044 /* 2045 * wire case: in two passes [XXXCDC: ugly block of code here] 2046 * 2047 * 1: holding the write lock, we create any anonymous maps that need 2048 * to be created. then we clip each map entry to the region to 2049 * be wired and increment its wiring count. 2050 * 2051 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault 2052 * in the pages for any newly wired area (wired_count == 1). 2053 * 2054 * downgrading to a read lock for uvm_fault_wire avoids a possible 2055 * deadlock with another thread that may have faulted on one of 2056 * the pages to be wired (it would mark the page busy, blocking 2057 * us, then in turn block on the map lock that we hold). because 2058 * of problems in the recursive lock package, we cannot upgrade 2059 * to a write lock in vm_map_lookup. thus, any actions that 2060 * require the write lock must be done beforehand. because we 2061 * keep the read lock on the map, the copy-on-write status of the 2062 * entries we modify here cannot change. 2063 */ 2064 2065 while ((entry != &map->header) && (entry->start < end)) { 2066 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */ 2067 /* 2068 * perform actions of vm_map_lookup that need the 2069 * write lock on the map: create an anonymous map 2070 * for a copy-on-write region, or an anonymous map 2071 * for a zero-fill region. (XXXCDC: submap case 2072 * ok?) 2073 */ 2074 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */ 2075 if (UVM_ET_ISNEEDSCOPY(entry) && 2076 ((entry->protection & VM_PROT_WRITE) || 2077 (entry->object.uvm_obj == NULL))) { 2078 amap_copy(map, entry, M_WAITOK, TRUE, 2079 start, end); 2080 /* XXXCDC: wait OK? */ 2081 } 2082 } 2083 } 2084 UVM_MAP_CLIP_START(map, entry, start); 2085 UVM_MAP_CLIP_END(map, entry, end); 2086 entry->wired_count++; 2087 2088 /* 2089 * Check for holes 2090 */ 2091 if (entry->protection == VM_PROT_NONE || 2092 (entry->end < end && 2093 (entry->next == &map->header || 2094 entry->next->start > entry->end))) { 2095 /* 2096 * found one. amap creation actions do not need to 2097 * be undone, but the wired counts need to be restored. 2098 */ 2099 while (entry != &map->header && entry->end > start) { 2100 entry->wired_count--; 2101 entry = entry->prev; 2102 } 2103 if ((lockflags & UVM_LK_EXIT) == 0) 2104 vm_map_unlock(map); 2105 UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0); 2106 return (KERN_INVALID_ARGUMENT); 2107 } 2108 entry = entry->next; 2109 } 2110 2111 /* 2112 * Pass 2. 2113 */ 2114 2115 #ifdef DIAGNOSTIC 2116 timestamp_save = map->timestamp; 2117 #endif 2118 vm_map_busy(map); 2119 vm_map_downgrade(map); 2120 2121 rv = 0; 2122 entry = start_entry; 2123 while (entry != &map->header && entry->start < end) { 2124 if (entry->wired_count == 1) { 2125 rv = uvm_fault_wire(map, entry->start, entry->end, 2126 entry->protection); 2127 if (rv) { 2128 /* 2129 * wiring failed. break out of the loop. 2130 * we'll clean up the map below, once we 2131 * have a write lock again. 2132 */ 2133 break; 2134 } 2135 } 2136 entry = entry->next; 2137 } 2138 2139 if (rv) { /* failed? */ 2140 /* 2141 * Get back to an exclusive (write) lock. 2142 */ 2143 vm_map_upgrade(map); 2144 vm_map_unbusy(map); 2145 2146 #ifdef DIAGNOSTIC 2147 if (timestamp_save != map->timestamp) 2148 panic("uvm_map_pageable: stale map"); 2149 #endif 2150 2151 /* 2152 * first drop the wiring count on all the entries 2153 * which haven't actually been wired yet. 2154 */ 2155 failed_entry = entry; 2156 while (entry != &map->header && entry->start < end) { 2157 entry->wired_count--; 2158 entry = entry->next; 2159 } 2160 2161 /* 2162 * now, unwire all the entries that were successfully 2163 * wired above. 2164 */ 2165 entry = start_entry; 2166 while (entry != failed_entry) { 2167 entry->wired_count--; 2168 if (VM_MAPENT_ISWIRED(entry) == 0) 2169 uvm_map_entry_unwire(map, entry); 2170 entry = entry->next; 2171 } 2172 if ((lockflags & UVM_LK_EXIT) == 0) 2173 vm_map_unlock(map); 2174 UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0); 2175 return(rv); 2176 } 2177 2178 /* We are holding a read lock here. */ 2179 if ((lockflags & UVM_LK_EXIT) == 0) { 2180 vm_map_unbusy(map); 2181 vm_map_unlock_read(map); 2182 } else { 2183 /* 2184 * Get back to an exclusive (write) lock. 2185 */ 2186 vm_map_upgrade(map); 2187 vm_map_unbusy(map); 2188 } 2189 2190 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0); 2191 return(KERN_SUCCESS); 2192 } 2193 2194 /* 2195 * uvm_map_pageable_all: special case of uvm_map_pageable - affects 2196 * all mapped regions. 2197 * 2198 * => map must not be locked. 2199 * => if no flags are specified, all regions are unwired. 2200 * => XXXJRT: has some of the same problems as uvm_map_pageable() above. 2201 */ 2202 2203 int 2204 uvm_map_pageable_all(map, flags, limit) 2205 vm_map_t map; 2206 int flags; 2207 vsize_t limit; 2208 { 2209 vm_map_entry_t entry, failed_entry; 2210 vsize_t size; 2211 int rv; 2212 #ifdef DIAGNOSTIC 2213 u_int timestamp_save; 2214 #endif 2215 UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist); 2216 UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0); 2217 2218 #ifdef DIAGNOSTIC 2219 if ((map->flags & VM_MAP_PAGEABLE) == 0) 2220 panic("uvm_map_pageable_all: map %p not pageable", map); 2221 #endif 2222 2223 vm_map_lock(map); 2224 2225 /* 2226 * handle wiring and unwiring separately. 2227 */ 2228 2229 if (flags == 0) { /* unwire */ 2230 /* 2231 * POSIX 1003.1b -- munlockall unlocks all regions, 2232 * regardless of how many times mlockall has been called. 2233 */ 2234 for (entry = map->header.next; entry != &map->header; 2235 entry = entry->next) { 2236 if (VM_MAPENT_ISWIRED(entry)) 2237 uvm_map_entry_unwire(map, entry); 2238 } 2239 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE); 2240 vm_map_unlock(map); 2241 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0); 2242 return (KERN_SUCCESS); 2243 2244 /* 2245 * end of unwire case! 2246 */ 2247 } 2248 2249 if (flags & MCL_FUTURE) { 2250 /* 2251 * must wire all future mappings; remember this. 2252 */ 2253 vm_map_modflags(map, VM_MAP_WIREFUTURE, 0); 2254 } 2255 2256 if ((flags & MCL_CURRENT) == 0) { 2257 /* 2258 * no more work to do! 2259 */ 2260 UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0); 2261 vm_map_unlock(map); 2262 return (KERN_SUCCESS); 2263 } 2264 2265 /* 2266 * wire case: in three passes [XXXCDC: ugly block of code here] 2267 * 2268 * 1: holding the write lock, count all pages mapped by non-wired 2269 * entries. if this would cause us to go over our limit, we fail. 2270 * 2271 * 2: still holding the write lock, we create any anonymous maps that 2272 * need to be created. then we increment its wiring count. 2273 * 2274 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault 2275 * in the pages for any newly wired area (wired_count == 1). 2276 * 2277 * downgrading to a read lock for uvm_fault_wire avoids a possible 2278 * deadlock with another thread that may have faulted on one of 2279 * the pages to be wired (it would mark the page busy, blocking 2280 * us, then in turn block on the map lock that we hold). because 2281 * of problems in the recursive lock package, we cannot upgrade 2282 * to a write lock in vm_map_lookup. thus, any actions that 2283 * require the write lock must be done beforehand. because we 2284 * keep the read lock on the map, the copy-on-write status of the 2285 * entries we modify here cannot change. 2286 */ 2287 2288 for (size = 0, entry = map->header.next; entry != &map->header; 2289 entry = entry->next) { 2290 if (entry->protection != VM_PROT_NONE && 2291 VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */ 2292 size += entry->end - entry->start; 2293 } 2294 } 2295 2296 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) { 2297 vm_map_unlock(map); 2298 return (KERN_NO_SPACE); /* XXX overloaded */ 2299 } 2300 2301 /* XXX non-pmap_wired_count case must be handled by caller */ 2302 #ifdef pmap_wired_count 2303 if (limit != 0 && 2304 (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) { 2305 vm_map_unlock(map); 2306 return (KERN_NO_SPACE); /* XXX overloaded */ 2307 } 2308 #endif 2309 2310 /* 2311 * Pass 2. 2312 */ 2313 2314 for (entry = map->header.next; entry != &map->header; 2315 entry = entry->next) { 2316 if (entry->protection == VM_PROT_NONE) 2317 continue; 2318 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */ 2319 /* 2320 * perform actions of vm_map_lookup that need the 2321 * write lock on the map: create an anonymous map 2322 * for a copy-on-write region, or an anonymous map 2323 * for a zero-fill region. (XXXCDC: submap case 2324 * ok?) 2325 */ 2326 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */ 2327 if (UVM_ET_ISNEEDSCOPY(entry) && 2328 ((entry->protection & VM_PROT_WRITE) || 2329 (entry->object.uvm_obj == NULL))) { 2330 amap_copy(map, entry, M_WAITOK, TRUE, 2331 entry->start, entry->end); 2332 /* XXXCDC: wait OK? */ 2333 } 2334 } 2335 } 2336 entry->wired_count++; 2337 } 2338 2339 /* 2340 * Pass 3. 2341 */ 2342 2343 #ifdef DIAGNOSTIC 2344 timestamp_save = map->timestamp; 2345 #endif 2346 vm_map_busy(map); 2347 vm_map_downgrade(map); 2348 2349 rv = KERN_SUCCESS; 2350 for (entry = map->header.next; entry != &map->header; 2351 entry = entry->next) { 2352 if (entry->wired_count == 1) { 2353 rv = uvm_fault_wire(map, entry->start, entry->end, 2354 entry->protection); 2355 if (rv) { 2356 /* 2357 * wiring failed. break out of the loop. 2358 * we'll clean up the map below, once we 2359 * have a write lock again. 2360 */ 2361 break; 2362 } 2363 } 2364 } 2365 2366 if (rv) { /* failed? */ 2367 /* 2368 * Get back an exclusive (write) lock. 2369 */ 2370 vm_map_upgrade(map); 2371 vm_map_unbusy(map); 2372 2373 #ifdef DIAGNOSTIC 2374 if (timestamp_save != map->timestamp) 2375 panic("uvm_map_pageable_all: stale map"); 2376 #endif 2377 2378 /* 2379 * first drop the wiring count on all the entries 2380 * which haven't actually been wired yet. 2381 * 2382 * Skip VM_PROT_NONE entries like we did above. 2383 */ 2384 failed_entry = entry; 2385 for (/* nothing */; entry != &map->header; 2386 entry = entry->next) { 2387 if (entry->protection == VM_PROT_NONE) 2388 continue; 2389 entry->wired_count--; 2390 } 2391 2392 /* 2393 * now, unwire all the entries that were successfully 2394 * wired above. 2395 * 2396 * Skip VM_PROT_NONE entries like we did above. 2397 */ 2398 for (entry = map->header.next; entry != failed_entry; 2399 entry = entry->next) { 2400 if (entry->protection == VM_PROT_NONE) 2401 continue; 2402 entry->wired_count--; 2403 if (VM_MAPENT_ISWIRED(entry)) 2404 uvm_map_entry_unwire(map, entry); 2405 } 2406 vm_map_unlock(map); 2407 UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0); 2408 return (rv); 2409 } 2410 2411 /* We are holding a read lock here. */ 2412 vm_map_unbusy(map); 2413 vm_map_unlock_read(map); 2414 2415 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0); 2416 return (KERN_SUCCESS); 2417 } 2418 2419 /* 2420 * uvm_map_clean: clean out a map range 2421 * 2422 * => valid flags: 2423 * if (flags & PGO_CLEANIT): dirty pages are cleaned first 2424 * if (flags & PGO_SYNCIO): dirty pages are written synchronously 2425 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean 2426 * if (flags & PGO_FREE): any cached pages are freed after clean 2427 * => returns an error if any part of the specified range isn't mapped 2428 * => never a need to flush amap layer since the anonymous memory has 2429 * no permanent home, but may deactivate pages there 2430 * => called from sys_msync() and sys_madvise() 2431 * => caller must not write-lock map (read OK). 2432 * => we may sleep while cleaning if SYNCIO [with map read-locked] 2433 */ 2434 2435 int amap_clean_works = 1; /* XXX for now, just in case... */ 2436 2437 int 2438 uvm_map_clean(map, start, end, flags) 2439 vm_map_t map; 2440 vaddr_t start, end; 2441 int flags; 2442 { 2443 vm_map_entry_t current, entry; 2444 struct uvm_object *uobj; 2445 struct vm_amap *amap; 2446 struct vm_anon *anon; 2447 struct vm_page *pg; 2448 vaddr_t offset; 2449 vsize_t size; 2450 int rv, error, refs; 2451 UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist); 2452 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)", 2453 map, start, end, flags); 2454 2455 #ifdef DIAGNOSTIC 2456 if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == (PGO_FREE|PGO_DEACTIVATE)) 2457 panic("uvm_map_clean: FREE and DEACTIVATE"); 2458 #endif 2459 2460 vm_map_lock_read(map); 2461 VM_MAP_RANGE_CHECK(map, start, end); 2462 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) { 2463 vm_map_unlock_read(map); 2464 return(KERN_INVALID_ADDRESS); 2465 } 2466 2467 /* 2468 * Make a first pass to check for holes. 2469 */ 2470 for (current = entry; current->start < end; current = current->next) { 2471 if (UVM_ET_ISSUBMAP(current)) { 2472 vm_map_unlock_read(map); 2473 return (KERN_INVALID_ARGUMENT); 2474 } 2475 if (end > current->end && (current->next == &map->header || 2476 current->end != current->next->start)) { 2477 vm_map_unlock_read(map); 2478 return (KERN_INVALID_ADDRESS); 2479 } 2480 } 2481 2482 error = KERN_SUCCESS; 2483 2484 for (current = entry; current->start < end; current = current->next) { 2485 amap = current->aref.ar_amap; /* top layer */ 2486 uobj = current->object.uvm_obj; /* bottom layer */ 2487 2488 #ifdef DIAGNOSTIC 2489 if (start < current->start) 2490 panic("uvm_map_clean: hole"); 2491 #endif 2492 2493 /* 2494 * No amap cleaning necessary if: 2495 * 2496 * (1) There's no amap. 2497 * 2498 * (2) We're not deactivating or freeing pages. 2499 */ 2500 if (amap == NULL || 2501 (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) 2502 goto flush_object; 2503 2504 /* XXX for now, just in case... */ 2505 if (amap_clean_works == 0) 2506 goto flush_object; 2507 2508 amap_lock(amap); 2509 2510 offset = start - current->start; 2511 size = (end <= current->end ? end : current->end) - 2512 start; 2513 2514 for (/* nothing */; size != 0; size -= PAGE_SIZE, 2515 offset += PAGE_SIZE) { 2516 anon = amap_lookup(¤t->aref, offset); 2517 if (anon == NULL) 2518 continue; 2519 2520 simple_lock(&anon->an_lock); 2521 2522 pg = anon->u.an_page; 2523 if (pg == NULL) { 2524 simple_unlock(&anon->an_lock); 2525 continue; 2526 } 2527 2528 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) { 2529 /* 2530 * XXX In these first 3 cases, we always just 2531 * XXX deactivate the page. We may want to 2532 * XXX handle the different cases more 2533 * XXX specifically, in the future. 2534 */ 2535 case PGO_CLEANIT|PGO_FREE: 2536 case PGO_CLEANIT|PGO_DEACTIVATE: 2537 case PGO_DEACTIVATE: 2538 deactivate_it: 2539 /* skip the page if it's loaned or wired */ 2540 if (pg->loan_count != 0 || 2541 pg->wire_count != 0) { 2542 simple_unlock(&anon->an_lock); 2543 continue; 2544 } 2545 2546 uvm_lock_pageq(); 2547 2548 /* 2549 * skip the page if it's not actually owned 2550 * by the anon (may simply be loaned to the 2551 * anon). 2552 */ 2553 if ((pg->pqflags & PQ_ANON) == 0) { 2554 #ifdef DIAGNOSTIC 2555 if (pg->uobject != NULL) 2556 panic("uvm_map_clean: " 2557 "page anon vs. object " 2558 "inconsistency"); 2559 #endif 2560 uvm_unlock_pageq(); 2561 simple_unlock(&anon->an_lock); 2562 continue; 2563 } 2564 2565 #ifdef DIAGNOSTIC 2566 if (pg->uanon != anon) 2567 panic("uvm_map_clean: anon " 2568 "inconsistency"); 2569 #endif 2570 2571 /* zap all mappings for the page. */ 2572 pmap_page_protect(pg, VM_PROT_NONE); 2573 2574 /* ...and deactivate the page. */ 2575 uvm_pagedeactivate(pg); 2576 2577 uvm_unlock_pageq(); 2578 simple_unlock(&anon->an_lock); 2579 continue; 2580 2581 case PGO_FREE: 2582 /* 2583 * If there are multiple references to 2584 * the amap, just deactivate the page. 2585 */ 2586 if (amap_refs(amap) > 1) 2587 goto deactivate_it; 2588 2589 /* XXX skip the page if it's wired */ 2590 if (pg->wire_count != 0) { 2591 simple_unlock(&anon->an_lock); 2592 continue; 2593 } 2594 amap_unadd(¤t->aref, offset); 2595 refs = --anon->an_ref; 2596 simple_unlock(&anon->an_lock); 2597 if (refs == 0) 2598 uvm_anfree(anon); 2599 continue; 2600 2601 default: 2602 panic("uvm_map_clean: wierd flags"); 2603 } 2604 #ifdef DIAGNOSTIC 2605 panic("uvm_map_clean: unreachable code"); 2606 #endif 2607 } 2608 2609 amap_unlock(amap); 2610 2611 flush_object: 2612 /* 2613 * flush pages if we've got a valid backing object. 2614 */ 2615 2616 offset = current->offset + (start - current->start); 2617 size = (end <= current->end ? end : current->end) - start; 2618 2619 if (uobj != NULL) { 2620 simple_lock(&uobj->vmobjlock); 2621 rv = uobj->pgops->pgo_flush(uobj, offset, 2622 offset + size, flags); 2623 simple_unlock(&uobj->vmobjlock); 2624 2625 if (rv == FALSE) 2626 error = KERN_FAILURE; 2627 } 2628 start += size; 2629 } 2630 2631 vm_map_unlock_read(map); 2632 return (error); 2633 } 2634 2635 2636 /* 2637 * uvm_map_checkprot: check protection in map 2638 * 2639 * => must allow specified protection in a fully allocated region. 2640 * => map must be read or write locked by caller. 2641 */ 2642 2643 boolean_t 2644 uvm_map_checkprot(map, start, end, protection) 2645 vm_map_t map; 2646 vaddr_t start, end; 2647 vm_prot_t protection; 2648 { 2649 vm_map_entry_t entry; 2650 vm_map_entry_t tmp_entry; 2651 2652 if (!uvm_map_lookup_entry(map, start, &tmp_entry)) { 2653 return(FALSE); 2654 } 2655 2656 entry = tmp_entry; 2657 2658 while (start < end) { 2659 if (entry == &map->header) { 2660 return(FALSE); 2661 } 2662 2663 /* 2664 * no holes allowed 2665 */ 2666 2667 if (start < entry->start) { 2668 return(FALSE); 2669 } 2670 2671 /* 2672 * check protection associated with entry 2673 */ 2674 2675 if ((entry->protection & protection) != protection) { 2676 return(FALSE); 2677 } 2678 2679 /* go to next entry */ 2680 2681 start = entry->end; 2682 entry = entry->next; 2683 } 2684 return(TRUE); 2685 } 2686 2687 /* 2688 * uvmspace_alloc: allocate a vmspace structure. 2689 * 2690 * - structure includes vm_map and pmap 2691 * - XXX: no locking on this structure 2692 * - refcnt set to 1, rest must be init'd by caller 2693 */ 2694 struct vmspace * 2695 uvmspace_alloc(min, max, pageable) 2696 vaddr_t min, max; 2697 int pageable; 2698 { 2699 struct vmspace *vm; 2700 UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist); 2701 2702 vm = pool_get(&uvm_vmspace_pool, PR_WAITOK); 2703 uvmspace_init(vm, NULL, min, max, pageable); 2704 UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0); 2705 return (vm); 2706 } 2707 2708 /* 2709 * uvmspace_init: initialize a vmspace structure. 2710 * 2711 * - XXX: no locking on this structure 2712 * - refcnt set to 1, rest must me init'd by caller 2713 */ 2714 void 2715 uvmspace_init(vm, pmap, min, max, pageable) 2716 struct vmspace *vm; 2717 struct pmap *pmap; 2718 vaddr_t min, max; 2719 boolean_t pageable; 2720 { 2721 UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist); 2722 2723 memset(vm, 0, sizeof(*vm)); 2724 2725 uvm_map_setup(&vm->vm_map, min, max, pageable ? VM_MAP_PAGEABLE : 0); 2726 2727 if (pmap) 2728 pmap_reference(pmap); 2729 else 2730 pmap = pmap_create(); 2731 vm->vm_map.pmap = pmap; 2732 2733 vm->vm_refcnt = 1; 2734 UVMHIST_LOG(maphist,"<- done",0,0,0,0); 2735 } 2736 2737 /* 2738 * uvmspace_share: share a vmspace between two proceses 2739 * 2740 * - XXX: no locking on vmspace 2741 * - used for vfork, threads(?) 2742 */ 2743 2744 void 2745 uvmspace_share(p1, p2) 2746 struct proc *p1, *p2; 2747 { 2748 p2->p_vmspace = p1->p_vmspace; 2749 p1->p_vmspace->vm_refcnt++; 2750 } 2751 2752 /* 2753 * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace 2754 * 2755 * - XXX: no locking on vmspace 2756 */ 2757 2758 void 2759 uvmspace_unshare(p) 2760 struct proc *p; 2761 { 2762 struct vmspace *nvm, *ovm = p->p_vmspace; 2763 2764 if (ovm->vm_refcnt == 1) 2765 /* nothing to do: vmspace isn't shared in the first place */ 2766 return; 2767 2768 /* make a new vmspace, still holding old one */ 2769 nvm = uvmspace_fork(ovm); 2770 2771 pmap_deactivate(p); /* unbind old vmspace */ 2772 p->p_vmspace = nvm; 2773 pmap_activate(p); /* switch to new vmspace */ 2774 2775 uvmspace_free(ovm); /* drop reference to old vmspace */ 2776 } 2777 2778 /* 2779 * uvmspace_exec: the process wants to exec a new program 2780 * 2781 * - XXX: no locking on vmspace 2782 */ 2783 2784 void 2785 uvmspace_exec(p) 2786 struct proc *p; 2787 { 2788 struct vmspace *nvm, *ovm = p->p_vmspace; 2789 vm_map_t map = &ovm->vm_map; 2790 2791 #ifdef __sparc__ 2792 /* XXX cgd 960926: the sparc #ifdef should be a MD hook */ 2793 kill_user_windows(p); /* before stack addresses go away */ 2794 #endif 2795 2796 /* 2797 * see if more than one process is using this vmspace... 2798 */ 2799 2800 if (ovm->vm_refcnt == 1) { 2801 2802 /* 2803 * if p is the only process using its vmspace then we can safely 2804 * recycle that vmspace for the program that is being exec'd. 2805 */ 2806 2807 #ifdef SYSVSHM 2808 /* 2809 * SYSV SHM semantics require us to kill all segments on an exec 2810 */ 2811 if (ovm->vm_shm) 2812 shmexit(ovm); 2813 #endif 2814 2815 /* 2816 * POSIX 1003.1b -- "lock future mappings" is revoked 2817 * when a process execs another program image. 2818 */ 2819 vm_map_lock(map); 2820 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE); 2821 vm_map_unlock(map); 2822 2823 /* 2824 * now unmap the old program 2825 */ 2826 uvm_unmap(map, VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 2827 2828 } else { 2829 2830 /* 2831 * p's vmspace is being shared, so we can't reuse it for p since 2832 * it is still being used for others. allocate a new vmspace 2833 * for p 2834 */ 2835 nvm = uvmspace_alloc(map->min_offset, map->max_offset, 2836 (map->flags & VM_MAP_PAGEABLE) ? TRUE : FALSE); 2837 2838 /* 2839 * install new vmspace and drop our ref to the old one. 2840 */ 2841 2842 pmap_deactivate(p); 2843 p->p_vmspace = nvm; 2844 pmap_activate(p); 2845 2846 uvmspace_free(ovm); 2847 } 2848 } 2849 2850 /* 2851 * uvmspace_free: free a vmspace data structure 2852 * 2853 * - XXX: no locking on vmspace 2854 */ 2855 2856 void 2857 uvmspace_free(vm) 2858 struct vmspace *vm; 2859 { 2860 vm_map_entry_t dead_entries; 2861 UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist); 2862 2863 UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0); 2864 if (--vm->vm_refcnt == 0) { 2865 /* 2866 * lock the map, to wait out all other references to it. delete 2867 * all of the mappings and pages they hold, then call the pmap 2868 * module to reclaim anything left. 2869 */ 2870 vm_map_lock(&vm->vm_map); 2871 if (vm->vm_map.nentries) { 2872 (void)uvm_unmap_remove(&vm->vm_map, 2873 vm->vm_map.min_offset, vm->vm_map.max_offset, 2874 &dead_entries); 2875 if (dead_entries != NULL) 2876 uvm_unmap_detach(dead_entries, 0); 2877 } 2878 pmap_destroy(vm->vm_map.pmap); 2879 vm->vm_map.pmap = NULL; 2880 pool_put(&uvm_vmspace_pool, vm); 2881 } 2882 UVMHIST_LOG(maphist,"<- done", 0,0,0,0); 2883 } 2884 2885 /* 2886 * F O R K - m a i n e n t r y p o i n t 2887 */ 2888 /* 2889 * uvmspace_fork: fork a process' main map 2890 * 2891 * => create a new vmspace for child process from parent. 2892 * => parent's map must not be locked. 2893 */ 2894 2895 struct vmspace * 2896 uvmspace_fork(vm1) 2897 struct vmspace *vm1; 2898 { 2899 struct vmspace *vm2; 2900 vm_map_t old_map = &vm1->vm_map; 2901 vm_map_t new_map; 2902 vm_map_entry_t old_entry; 2903 vm_map_entry_t new_entry; 2904 pmap_t new_pmap; 2905 boolean_t protect_child; 2906 UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist); 2907 2908 vm_map_lock(old_map); 2909 2910 vm2 = uvmspace_alloc(old_map->min_offset, old_map->max_offset, 2911 (old_map->flags & VM_MAP_PAGEABLE) ? TRUE : FALSE); 2912 memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy, 2913 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy); 2914 new_map = &vm2->vm_map; /* XXX */ 2915 new_pmap = new_map->pmap; 2916 2917 old_entry = old_map->header.next; 2918 2919 /* 2920 * go entry-by-entry 2921 */ 2922 2923 while (old_entry != &old_map->header) { 2924 2925 /* 2926 * first, some sanity checks on the old entry 2927 */ 2928 if (UVM_ET_ISSUBMAP(old_entry)) 2929 panic("fork: encountered a submap during fork (illegal)"); 2930 2931 if (!UVM_ET_ISCOPYONWRITE(old_entry) && 2932 UVM_ET_ISNEEDSCOPY(old_entry)) 2933 panic("fork: non-copy_on_write map entry marked needs_copy (illegal)"); 2934 2935 2936 switch (old_entry->inheritance) { 2937 case VM_INHERIT_NONE: 2938 /* 2939 * drop the mapping 2940 */ 2941 break; 2942 2943 case VM_INHERIT_SHARE: 2944 /* 2945 * share the mapping: this means we want the old and 2946 * new entries to share amaps and backing objects. 2947 */ 2948 2949 /* 2950 * if the old_entry needs a new amap (due to prev fork) 2951 * then we need to allocate it now so that we have 2952 * something we own to share with the new_entry. [in 2953 * other words, we need to clear needs_copy] 2954 */ 2955 2956 if (UVM_ET_ISNEEDSCOPY(old_entry)) { 2957 /* get our own amap, clears needs_copy */ 2958 amap_copy(old_map, old_entry, M_WAITOK, FALSE, 2959 0, 0); 2960 /* XXXCDC: WAITOK??? */ 2961 } 2962 2963 new_entry = uvm_mapent_alloc(new_map); 2964 /* old_entry -> new_entry */ 2965 uvm_mapent_copy(old_entry, new_entry); 2966 2967 /* new pmap has nothing wired in it */ 2968 new_entry->wired_count = 0; 2969 2970 /* 2971 * gain reference to object backing the map (can't 2972 * be a submap, already checked this case). 2973 */ 2974 if (new_entry->aref.ar_amap) 2975 /* share reference */ 2976 amap_ref(new_entry, AMAP_SHARED); 2977 2978 if (new_entry->object.uvm_obj && 2979 new_entry->object.uvm_obj->pgops->pgo_reference) 2980 new_entry->object.uvm_obj-> 2981 pgops->pgo_reference( 2982 new_entry->object.uvm_obj); 2983 2984 /* insert entry at end of new_map's entry list */ 2985 uvm_map_entry_link(new_map, new_map->header.prev, 2986 new_entry); 2987 2988 /* 2989 * pmap_copy the mappings: this routine is optional 2990 * but if it is there it will reduce the number of 2991 * page faults in the new proc. 2992 */ 2993 2994 pmap_copy(new_pmap, old_map->pmap, new_entry->start, 2995 (old_entry->end - old_entry->start), 2996 old_entry->start); 2997 2998 break; 2999 3000 case VM_INHERIT_COPY: 3001 3002 /* 3003 * copy-on-write the mapping (using mmap's 3004 * MAP_PRIVATE semantics) 3005 * 3006 * allocate new_entry, adjust reference counts. 3007 * (note that new references are read-only). 3008 */ 3009 3010 new_entry = uvm_mapent_alloc(new_map); 3011 /* old_entry -> new_entry */ 3012 uvm_mapent_copy(old_entry, new_entry); 3013 3014 if (new_entry->aref.ar_amap) 3015 amap_ref(new_entry, 0); 3016 3017 if (new_entry->object.uvm_obj && 3018 new_entry->object.uvm_obj->pgops->pgo_reference) 3019 new_entry->object.uvm_obj->pgops->pgo_reference 3020 (new_entry->object.uvm_obj); 3021 3022 /* new pmap has nothing wired in it */ 3023 new_entry->wired_count = 0; 3024 3025 new_entry->etype |= 3026 (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY); 3027 uvm_map_entry_link(new_map, new_map->header.prev, 3028 new_entry); 3029 3030 /* 3031 * the new entry will need an amap. it will either 3032 * need to be copied from the old entry or created 3033 * from scratch (if the old entry does not have an 3034 * amap). can we defer this process until later 3035 * (by setting "needs_copy") or do we need to copy 3036 * the amap now? 3037 * 3038 * we must copy the amap now if any of the following 3039 * conditions hold: 3040 * 1. the old entry has an amap and that amap is 3041 * being shared. this means that the old (parent) 3042 * process is sharing the amap with another 3043 * process. if we do not clear needs_copy here 3044 * we will end up in a situation where both the 3045 * parent and child process are refering to the 3046 * same amap with "needs_copy" set. if the 3047 * parent write-faults, the fault routine will 3048 * clear "needs_copy" in the parent by allocating 3049 * a new amap. this is wrong because the 3050 * parent is supposed to be sharing the old amap 3051 * and the new amap will break that. 3052 * 3053 * 2. if the old entry has an amap and a non-zero 3054 * wire count then we are going to have to call 3055 * amap_cow_now to avoid page faults in the 3056 * parent process. since amap_cow_now requires 3057 * "needs_copy" to be clear we might as well 3058 * clear it here as well. 3059 * 3060 */ 3061 3062 if (old_entry->aref.ar_amap != NULL) { 3063 3064 if ((amap_flags(old_entry->aref.ar_amap) & 3065 AMAP_SHARED) != 0 || 3066 VM_MAPENT_ISWIRED(old_entry)) { 3067 3068 amap_copy(new_map, new_entry, M_WAITOK, FALSE, 3069 0, 0); 3070 /* XXXCDC: M_WAITOK ... ok? */ 3071 } 3072 } 3073 3074 /* 3075 * if the parent's entry is wired down, then the 3076 * parent process does not want page faults on 3077 * access to that memory. this means that we 3078 * cannot do copy-on-write because we can't write 3079 * protect the old entry. in this case we 3080 * resolve all copy-on-write faults now, using 3081 * amap_cow_now. note that we have already 3082 * allocated any needed amap (above). 3083 */ 3084 3085 if (VM_MAPENT_ISWIRED(old_entry)) { 3086 3087 /* 3088 * resolve all copy-on-write faults now 3089 * (note that there is nothing to do if 3090 * the old mapping does not have an amap). 3091 * XXX: is it worthwhile to bother with pmap_copy 3092 * in this case? 3093 */ 3094 if (old_entry->aref.ar_amap) 3095 amap_cow_now(new_map, new_entry); 3096 3097 } else { 3098 3099 /* 3100 * setup mappings to trigger copy-on-write faults 3101 * we must write-protect the parent if it has 3102 * an amap and it is not already "needs_copy"... 3103 * if it is already "needs_copy" then the parent 3104 * has already been write-protected by a previous 3105 * fork operation. 3106 * 3107 * if we do not write-protect the parent, then 3108 * we must be sure to write-protect the child 3109 * after the pmap_copy() operation. 3110 * 3111 * XXX: pmap_copy should have some way of telling 3112 * us that it didn't do anything so we can avoid 3113 * calling pmap_protect needlessly. 3114 */ 3115 3116 if (old_entry->aref.ar_amap) { 3117 3118 if (!UVM_ET_ISNEEDSCOPY(old_entry)) { 3119 if (old_entry->max_protection & VM_PROT_WRITE) { 3120 pmap_protect(old_map->pmap, 3121 old_entry->start, 3122 old_entry->end, 3123 old_entry->protection & 3124 ~VM_PROT_WRITE); 3125 } 3126 old_entry->etype |= UVM_ET_NEEDSCOPY; 3127 } 3128 3129 /* 3130 * parent must now be write-protected 3131 */ 3132 protect_child = FALSE; 3133 } else { 3134 3135 /* 3136 * we only need to protect the child if the 3137 * parent has write access. 3138 */ 3139 if (old_entry->max_protection & VM_PROT_WRITE) 3140 protect_child = TRUE; 3141 else 3142 protect_child = FALSE; 3143 3144 } 3145 3146 /* 3147 * copy the mappings 3148 * XXX: need a way to tell if this does anything 3149 */ 3150 3151 pmap_copy(new_pmap, old_map->pmap, 3152 new_entry->start, 3153 (old_entry->end - old_entry->start), 3154 old_entry->start); 3155 3156 /* 3157 * protect the child's mappings if necessary 3158 */ 3159 if (protect_child) { 3160 pmap_protect(new_pmap, new_entry->start, 3161 new_entry->end, 3162 new_entry->protection & 3163 ~VM_PROT_WRITE); 3164 } 3165 3166 } 3167 break; 3168 } /* end of switch statement */ 3169 old_entry = old_entry->next; 3170 } 3171 3172 new_map->size = old_map->size; 3173 vm_map_unlock(old_map); 3174 3175 #ifdef SYSVSHM 3176 if (vm1->vm_shm) 3177 shmfork(vm1, vm2); 3178 #endif 3179 3180 #ifdef PMAP_FORK 3181 pmap_fork(vm1->vm_map.pmap, vm2->vm_map.pmap); 3182 #endif 3183 3184 UVMHIST_LOG(maphist,"<- done",0,0,0,0); 3185 return(vm2); 3186 } 3187 3188 3189 #if defined(DDB) 3190 3191 /* 3192 * DDB hooks 3193 */ 3194 3195 /* 3196 * uvm_map_print: print out a map 3197 */ 3198 3199 void 3200 uvm_map_print(map, full) 3201 vm_map_t map; 3202 boolean_t full; 3203 { 3204 3205 uvm_map_printit(map, full, printf); 3206 } 3207 3208 /* 3209 * uvm_map_printit: actually prints the map 3210 */ 3211 3212 void 3213 uvm_map_printit(map, full, pr) 3214 vm_map_t map; 3215 boolean_t full; 3216 int (*pr) __P((const char *, ...)); 3217 { 3218 vm_map_entry_t entry; 3219 3220 (*pr)("MAP %p: [0x%lx->0x%lx]\n", map, map->min_offset,map->max_offset); 3221 (*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n", 3222 map->nentries, map->size, map->ref_count, map->timestamp, 3223 map->flags); 3224 #ifdef pmap_resident_count 3225 (*pr)("\tpmap=%p(resident=%d)\n", map->pmap, 3226 pmap_resident_count(map->pmap)); 3227 #else 3228 /* XXXCDC: this should be required ... */ 3229 (*pr)("\tpmap=%p(resident=<<NOT SUPPORTED!!!>>)\n", map->pmap); 3230 #endif 3231 if (!full) 3232 return; 3233 for (entry = map->header.next; entry != &map->header; 3234 entry = entry->next) { 3235 (*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n", 3236 entry, entry->start, entry->end, entry->object.uvm_obj, 3237 (long long)entry->offset, entry->aref.ar_amap, entry->aref.ar_pageoff); 3238 (*pr)( 3239 "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, wc=%d, adv=%d\n", 3240 (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F', 3241 (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F', 3242 (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F', 3243 entry->protection, entry->max_protection, 3244 entry->inheritance, entry->wired_count, entry->advice); 3245 } 3246 } 3247 3248 /* 3249 * uvm_object_print: print out an object 3250 */ 3251 3252 void 3253 uvm_object_print(uobj, full) 3254 struct uvm_object *uobj; 3255 boolean_t full; 3256 { 3257 3258 uvm_object_printit(uobj, full, printf); 3259 } 3260 3261 /* 3262 * uvm_object_printit: actually prints the object 3263 */ 3264 3265 void 3266 uvm_object_printit(uobj, full, pr) 3267 struct uvm_object *uobj; 3268 boolean_t full; 3269 int (*pr) __P((const char *, ...)); 3270 { 3271 struct vm_page *pg; 3272 int cnt = 0; 3273 3274 (*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ", 3275 uobj, uobj->vmobjlock.lock_data, uobj->pgops, uobj->uo_npages); 3276 if (UVM_OBJ_IS_KERN_OBJECT(uobj)) 3277 (*pr)("refs=<SYSTEM>\n"); 3278 else 3279 (*pr)("refs=%d\n", uobj->uo_refs); 3280 3281 if (!full) { 3282 return; 3283 } 3284 (*pr)(" PAGES <pg,offset>:\n "); 3285 for (pg = TAILQ_FIRST(&uobj->memq); 3286 pg != NULL; 3287 pg = TAILQ_NEXT(pg, listq), cnt++) { 3288 (*pr)("<%p,0x%lx> ", pg, pg->offset); 3289 if ((cnt % 3) == 2) { 3290 (*pr)("\n "); 3291 } 3292 } 3293 if ((cnt % 3) != 2) { 3294 (*pr)("\n"); 3295 } 3296 } 3297 3298 const char page_flagbits[] = 3299 "\20\4CLEAN\5BUSY\6WANTED\7TABLED\12FAKE\13FILLED\14DIRTY\15RELEASED" 3300 "\16FAULTING\17CLEANCHK"; 3301 const char page_pqflagbits[] = 3302 "\20\1FREE\2INACTIVE\3ACTIVE\4LAUNDRY\5ANON\6AOBJ"; 3303 3304 /* 3305 * uvm_page_print: print out a page 3306 */ 3307 3308 void 3309 uvm_page_print(pg, full) 3310 struct vm_page *pg; 3311 boolean_t full; 3312 { 3313 3314 uvm_page_printit(pg, full, printf); 3315 } 3316 3317 /* 3318 * uvm_page_printit: actually print the page 3319 */ 3320 3321 void 3322 uvm_page_printit(pg, full, pr) 3323 struct vm_page *pg; 3324 boolean_t full; 3325 int (*pr) __P((const char *, ...)); 3326 { 3327 struct vm_page *lcv; 3328 struct uvm_object *uobj; 3329 struct pglist *pgl; 3330 char pgbuf[128]; 3331 char pqbuf[128]; 3332 3333 (*pr)("PAGE %p:\n", pg); 3334 snprintf(pgbuf, sizeof(pgbuf), "%b", pg->flags, page_flagbits); 3335 snprintf(pqbuf, sizeof(pqbuf), "%b", pg->pqflags, page_pqflagbits); 3336 (*pr)(" flags=%s, pqflags=%s, vers=%d, wire_count=%d, pa=0x%lx\n", 3337 pgbuf, pqbuf, pg->version, pg->wire_count, (long)pg->phys_addr); 3338 (*pr)(" uobject=%p, uanon=%p, offset=0x%lx loan_count=%d\n", 3339 pg->uobject, pg->uanon, pg->offset, pg->loan_count); 3340 #if defined(UVM_PAGE_TRKOWN) 3341 if (pg->flags & PG_BUSY) 3342 (*pr)(" owning process = %d, tag=%s\n", 3343 pg->owner, pg->owner_tag); 3344 else 3345 (*pr)(" page not busy, no owner\n"); 3346 #else 3347 (*pr)(" [page ownership tracking disabled]\n"); 3348 #endif 3349 3350 if (!full) 3351 return; 3352 3353 /* cross-verify object/anon */ 3354 if ((pg->pqflags & PQ_FREE) == 0) { 3355 if (pg->pqflags & PQ_ANON) { 3356 if (pg->uanon == NULL || pg->uanon->u.an_page != pg) 3357 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n", 3358 (pg->uanon) ? pg->uanon->u.an_page : NULL); 3359 else 3360 (*pr)(" anon backpointer is OK\n"); 3361 } else { 3362 uobj = pg->uobject; 3363 if (uobj) { 3364 (*pr)(" checking object list\n"); 3365 for (lcv = uobj->memq.tqh_first ; lcv ; 3366 lcv = lcv->listq.tqe_next) { 3367 if (lcv == pg) break; 3368 } 3369 if (lcv) 3370 (*pr)(" page found on object list\n"); 3371 else 3372 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n"); 3373 } 3374 } 3375 } 3376 3377 /* cross-verify page queue */ 3378 if (pg->pqflags & PQ_FREE) { 3379 int fl = uvm_page_lookup_freelist(pg); 3380 pgl = &uvm.page_free[fl].pgfl_queues[((pg)->flags & PG_ZERO) ? 3381 PGFL_ZEROS : PGFL_UNKNOWN]; 3382 } 3383 else if (pg->pqflags & PQ_INACTIVE) 3384 pgl = (pg->pqflags & PQ_SWAPBACKED) ? 3385 &uvm.page_inactive_swp : &uvm.page_inactive_obj; 3386 else if (pg->pqflags & PQ_ACTIVE) 3387 pgl = &uvm.page_active; 3388 else 3389 pgl = NULL; 3390 3391 if (pgl) { 3392 (*pr)(" checking pageq list\n"); 3393 for (lcv = pgl->tqh_first ; lcv ; lcv = lcv->pageq.tqe_next) { 3394 if (lcv == pg) break; 3395 } 3396 if (lcv) 3397 (*pr)(" page found on pageq list\n"); 3398 else 3399 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n"); 3400 } 3401 } 3402 #endif 3403