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