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