1 /* $NetBSD: uvm_map.c,v 1.210 2006/01/21 13:34:15 yamt Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Charles D. Cranor and Washington University. 5 * Copyright (c) 1991, 1993, The Regents of the University of California. 6 * 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * The Mach Operating System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by Charles D. Cranor, 23 * Washington University, the University of California, Berkeley and 24 * its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)vm_map.c 8.3 (Berkeley) 1/12/94 42 * from: Id: uvm_map.c,v 1.1.2.27 1998/02/07 01:16:54 chs Exp 43 * 44 * 45 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 46 * All rights reserved. 47 * 48 * Permission to use, copy, modify and distribute this software and 49 * its documentation is hereby granted, provided that both the copyright 50 * notice and this permission notice appear in all copies of the 51 * software, derivative works or modified versions, and any portions 52 * thereof, and that both notices appear in supporting documentation. 53 * 54 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 55 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 56 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 57 * 58 * Carnegie Mellon requests users of this software to return to 59 * 60 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 61 * School of Computer Science 62 * Carnegie Mellon University 63 * Pittsburgh PA 15213-3890 64 * 65 * any improvements or extensions that they make and grant Carnegie the 66 * rights to redistribute these changes. 67 */ 68 69 /* 70 * uvm_map.c: uvm map operations 71 */ 72 73 #include <sys/cdefs.h> 74 __KERNEL_RCSID(0, "$NetBSD: uvm_map.c,v 1.210 2006/01/21 13:34:15 yamt Exp $"); 75 76 #include "opt_ddb.h" 77 #include "opt_uvmhist.h" 78 #include "opt_uvm.h" 79 #include "opt_sysv.h" 80 81 #include <sys/param.h> 82 #include <sys/systm.h> 83 #include <sys/mman.h> 84 #include <sys/proc.h> 85 #include <sys/malloc.h> 86 #include <sys/pool.h> 87 #include <sys/kernel.h> 88 #include <sys/mount.h> 89 #include <sys/vnode.h> 90 91 #ifdef SYSVSHM 92 #include <sys/shm.h> 93 #endif 94 95 #define UVM_MAP_C 96 #include <uvm/uvm.h> 97 #undef RB_AUGMENT 98 #define RB_AUGMENT(x) uvm_rb_augment(x) 99 100 #ifdef DDB 101 #include <uvm/uvm_ddb.h> 102 #endif 103 104 #if defined(UVMMAP_NOCOUNTERS) 105 106 #define UVMMAP_EVCNT_DEFINE(name) /* nothing */ 107 #define UVMMAP_EVCNT_INCR(ev) /* nothing */ 108 #define UVMMAP_EVCNT_DECR(ev) /* nothing */ 109 110 #else /* defined(UVMMAP_NOCOUNTERS) */ 111 112 #include <sys/device.h> 113 #define UVMMAP_EVCNT_DEFINE(name) \ 114 struct evcnt uvmmap_evcnt_##name = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, \ 115 "uvmmap", #name); \ 116 EVCNT_ATTACH_STATIC(uvmmap_evcnt_##name); 117 #define UVMMAP_EVCNT_INCR(ev) uvmmap_evcnt_##ev.ev_count++ 118 #define UVMMAP_EVCNT_DECR(ev) uvmmap_evcnt_##ev.ev_count-- 119 120 #endif /* defined(UVMMAP_NOCOUNTERS) */ 121 122 UVMMAP_EVCNT_DEFINE(ubackmerge) 123 UVMMAP_EVCNT_DEFINE(uforwmerge) 124 UVMMAP_EVCNT_DEFINE(ubimerge) 125 UVMMAP_EVCNT_DEFINE(unomerge) 126 UVMMAP_EVCNT_DEFINE(kbackmerge) 127 UVMMAP_EVCNT_DEFINE(kforwmerge) 128 UVMMAP_EVCNT_DEFINE(kbimerge) 129 UVMMAP_EVCNT_DEFINE(knomerge) 130 UVMMAP_EVCNT_DEFINE(map_call) 131 UVMMAP_EVCNT_DEFINE(mlk_call) 132 UVMMAP_EVCNT_DEFINE(mlk_hint) 133 134 UVMMAP_EVCNT_DEFINE(uke_alloc) 135 UVMMAP_EVCNT_DEFINE(uke_free) 136 UVMMAP_EVCNT_DEFINE(ukh_alloc) 137 UVMMAP_EVCNT_DEFINE(ukh_free) 138 139 const char vmmapbsy[] = "vmmapbsy"; 140 141 /* 142 * pool for vmspace structures. 143 */ 144 145 POOL_INIT(uvm_vmspace_pool, sizeof(struct vmspace), 0, 0, 0, "vmsppl", 146 &pool_allocator_nointr); 147 148 /* 149 * pool for dynamically-allocated map entries. 150 */ 151 152 POOL_INIT(uvm_map_entry_pool, sizeof(struct vm_map_entry), 0, 0, 0, "vmmpepl", 153 &pool_allocator_nointr); 154 155 MALLOC_DEFINE(M_VMMAP, "VM map", "VM map structures"); 156 MALLOC_DEFINE(M_VMPMAP, "VM pmap", "VM pmap"); 157 158 #ifdef PMAP_GROWKERNEL 159 /* 160 * This global represents the end of the kernel virtual address 161 * space. If we want to exceed this, we must grow the kernel 162 * virtual address space dynamically. 163 * 164 * Note, this variable is locked by kernel_map's lock. 165 */ 166 vaddr_t uvm_maxkaddr; 167 #endif 168 169 /* 170 * macros 171 */ 172 173 /* 174 * VM_MAP_USE_KMAPENT: determine if uvm_kmapent_alloc/free is used 175 * for the vm_map. 176 */ 177 extern struct vm_map *pager_map; /* XXX */ 178 #define VM_MAP_USE_KMAPENT(map) \ 179 (((map)->flags & VM_MAP_INTRSAFE) || (map) == kernel_map) 180 181 /* 182 * UVM_ET_ISCOMPATIBLE: check some requirements for map entry merging 183 */ 184 185 #define UVM_ET_ISCOMPATIBLE(ent, type, uobj, meflags, \ 186 prot, maxprot, inh, adv, wire) \ 187 ((ent)->etype == (type) && \ 188 (((ent)->flags ^ (meflags)) & (UVM_MAP_NOMERGE | UVM_MAP_QUANTUM)) \ 189 == 0 && \ 190 (ent)->object.uvm_obj == (uobj) && \ 191 (ent)->protection == (prot) && \ 192 (ent)->max_protection == (maxprot) && \ 193 (ent)->inheritance == (inh) && \ 194 (ent)->advice == (adv) && \ 195 (ent)->wired_count == (wire)) 196 197 /* 198 * uvm_map_entry_link: insert entry into a map 199 * 200 * => map must be locked 201 */ 202 #define uvm_map_entry_link(map, after_where, entry) do { \ 203 KASSERT(entry->start < entry->end); \ 204 (map)->nentries++; \ 205 (entry)->prev = (after_where); \ 206 (entry)->next = (after_where)->next; \ 207 (entry)->prev->next = (entry); \ 208 (entry)->next->prev = (entry); \ 209 uvm_rb_insert((map), (entry)); \ 210 } while (/*CONSTCOND*/ 0) 211 212 /* 213 * uvm_map_entry_unlink: remove entry from a map 214 * 215 * => map must be locked 216 */ 217 #define uvm_map_entry_unlink(map, entry) do { \ 218 (map)->nentries--; \ 219 (entry)->next->prev = (entry)->prev; \ 220 (entry)->prev->next = (entry)->next; \ 221 uvm_rb_remove((map), (entry)); \ 222 } while (/*CONSTCOND*/ 0) 223 224 /* 225 * SAVE_HINT: saves the specified entry as the hint for future lookups. 226 * 227 * => map need not be locked (protected by hint_lock). 228 */ 229 #define SAVE_HINT(map,check,value) do { \ 230 simple_lock(&(map)->hint_lock); \ 231 if ((map)->hint == (check)) \ 232 (map)->hint = (value); \ 233 simple_unlock(&(map)->hint_lock); \ 234 } while (/*CONSTCOND*/ 0) 235 236 /* 237 * VM_MAP_RANGE_CHECK: check and correct range 238 * 239 * => map must at least be read locked 240 */ 241 242 #define VM_MAP_RANGE_CHECK(map, start, end) do { \ 243 if (start < vm_map_min(map)) \ 244 start = vm_map_min(map); \ 245 if (end > vm_map_max(map)) \ 246 end = vm_map_max(map); \ 247 if (start > end) \ 248 start = end; \ 249 } while (/*CONSTCOND*/ 0) 250 251 /* 252 * local prototypes 253 */ 254 255 static struct vm_map_entry * 256 uvm_mapent_alloc(struct vm_map *, int); 257 static struct vm_map_entry * 258 uvm_mapent_alloc_split(struct vm_map *, 259 const struct vm_map_entry *, int, 260 struct uvm_mapent_reservation *); 261 static void uvm_mapent_copy(struct vm_map_entry *, struct vm_map_entry *); 262 static void uvm_mapent_free(struct vm_map_entry *); 263 static struct vm_map_entry * 264 uvm_kmapent_alloc(struct vm_map *, int); 265 static void uvm_kmapent_free(struct vm_map_entry *); 266 static void uvm_map_entry_unwire(struct vm_map *, struct vm_map_entry *); 267 static void uvm_map_reference_amap(struct vm_map_entry *, int); 268 static int uvm_map_space_avail(vaddr_t *, vsize_t, voff_t, vsize_t, int, 269 struct vm_map_entry *); 270 static void uvm_map_unreference_amap(struct vm_map_entry *, int); 271 272 int _uvm_tree_sanity(struct vm_map *, const char *); 273 static vsize_t uvm_rb_subtree_space(const struct vm_map_entry *); 274 275 static inline int 276 uvm_compare(const struct vm_map_entry *a, const struct vm_map_entry *b) 277 { 278 279 if (a->start < b->start) 280 return (-1); 281 else if (a->start > b->start) 282 return (1); 283 284 return (0); 285 } 286 287 static inline void 288 uvm_rb_augment(struct vm_map_entry *entry) 289 { 290 291 entry->space = uvm_rb_subtree_space(entry); 292 } 293 294 RB_PROTOTYPE(uvm_tree, vm_map_entry, rb_entry, uvm_compare); 295 296 RB_GENERATE(uvm_tree, vm_map_entry, rb_entry, uvm_compare); 297 298 static inline vsize_t 299 uvm_rb_space(const struct vm_map *map, const struct vm_map_entry *entry) 300 { 301 /* XXX map is not used */ 302 303 KASSERT(entry->next != NULL); 304 return entry->next->start - entry->end; 305 } 306 307 static vsize_t 308 uvm_rb_subtree_space(const struct vm_map_entry *entry) 309 { 310 vaddr_t space, tmp; 311 312 space = entry->ownspace; 313 if (RB_LEFT(entry, rb_entry)) { 314 tmp = RB_LEFT(entry, rb_entry)->space; 315 if (tmp > space) 316 space = tmp; 317 } 318 319 if (RB_RIGHT(entry, rb_entry)) { 320 tmp = RB_RIGHT(entry, rb_entry)->space; 321 if (tmp > space) 322 space = tmp; 323 } 324 325 return (space); 326 } 327 328 static inline void 329 uvm_rb_fixup(struct vm_map *map, struct vm_map_entry *entry) 330 { 331 /* We need to traverse to the very top */ 332 do { 333 entry->ownspace = uvm_rb_space(map, entry); 334 entry->space = uvm_rb_subtree_space(entry); 335 } while ((entry = RB_PARENT(entry, rb_entry)) != NULL); 336 } 337 338 static void 339 uvm_rb_insert(struct vm_map *map, struct vm_map_entry *entry) 340 { 341 vaddr_t space = uvm_rb_space(map, entry); 342 struct vm_map_entry *tmp; 343 344 entry->ownspace = entry->space = space; 345 tmp = RB_INSERT(uvm_tree, &(map)->rbhead, entry); 346 #ifdef DIAGNOSTIC 347 if (tmp != NULL) 348 panic("uvm_rb_insert: duplicate entry?"); 349 #endif 350 uvm_rb_fixup(map, entry); 351 if (entry->prev != &map->header) 352 uvm_rb_fixup(map, entry->prev); 353 } 354 355 static void 356 uvm_rb_remove(struct vm_map *map, struct vm_map_entry *entry) 357 { 358 struct vm_map_entry *parent; 359 360 parent = RB_PARENT(entry, rb_entry); 361 RB_REMOVE(uvm_tree, &(map)->rbhead, entry); 362 if (entry->prev != &map->header) 363 uvm_rb_fixup(map, entry->prev); 364 if (parent) 365 uvm_rb_fixup(map, parent); 366 } 367 368 #ifdef DEBUG 369 int uvm_debug_check_rbtree = 0; 370 #define uvm_tree_sanity(x,y) \ 371 if (uvm_debug_check_rbtree) \ 372 _uvm_tree_sanity(x,y) 373 #else 374 #define uvm_tree_sanity(x,y) 375 #endif 376 377 int 378 _uvm_tree_sanity(struct vm_map *map, const char *name) 379 { 380 struct vm_map_entry *tmp, *trtmp; 381 int n = 0, i = 1; 382 383 RB_FOREACH(tmp, uvm_tree, &map->rbhead) { 384 if (tmp->ownspace != uvm_rb_space(map, tmp)) { 385 printf("%s: %d/%d ownspace %lx != %lx %s\n", 386 name, n + 1, map->nentries, 387 (ulong)tmp->ownspace, (ulong)uvm_rb_space(map, tmp), 388 tmp->next == &map->header ? "(last)" : ""); 389 goto error; 390 } 391 } 392 trtmp = NULL; 393 RB_FOREACH(tmp, uvm_tree, &map->rbhead) { 394 if (tmp->space != uvm_rb_subtree_space(tmp)) { 395 printf("%s: space %lx != %lx\n", 396 name, (ulong)tmp->space, 397 (ulong)uvm_rb_subtree_space(tmp)); 398 goto error; 399 } 400 if (trtmp != NULL && trtmp->start >= tmp->start) { 401 printf("%s: corrupt: 0x%lx >= 0x%lx\n", 402 name, trtmp->start, tmp->start); 403 goto error; 404 } 405 n++; 406 407 trtmp = tmp; 408 } 409 410 if (n != map->nentries) { 411 printf("%s: nentries: %d vs %d\n", 412 name, n, map->nentries); 413 goto error; 414 } 415 416 for (tmp = map->header.next; tmp && tmp != &map->header; 417 tmp = tmp->next, i++) { 418 trtmp = RB_FIND(uvm_tree, &map->rbhead, tmp); 419 if (trtmp != tmp) { 420 printf("%s: lookup: %d: %p - %p: %p\n", 421 name, i, tmp, trtmp, 422 RB_PARENT(tmp, rb_entry)); 423 goto error; 424 } 425 } 426 427 return (0); 428 error: 429 #if defined(DDB) && __GNUC__ < 4 430 /* handy breakpoint location for error case */ 431 __asm(".globl treesanity_label\ntreesanity_label:"); 432 #endif 433 return (-1); 434 } 435 436 #ifdef DIAGNOSTIC 437 static struct vm_map *uvm_kmapent_map(struct vm_map_entry *); 438 #endif 439 440 /* 441 * uvm_mapent_alloc: allocate a map entry 442 */ 443 444 static struct vm_map_entry * 445 uvm_mapent_alloc(struct vm_map *map, int flags) 446 { 447 struct vm_map_entry *me; 448 int pflags = (flags & UVM_FLAG_NOWAIT) ? PR_NOWAIT : PR_WAITOK; 449 UVMHIST_FUNC("uvm_mapent_alloc"); UVMHIST_CALLED(maphist); 450 451 if (VM_MAP_USE_KMAPENT(map)) { 452 me = uvm_kmapent_alloc(map, flags); 453 } else { 454 me = pool_get(&uvm_map_entry_pool, pflags); 455 if (__predict_false(me == NULL)) 456 return NULL; 457 me->flags = 0; 458 } 459 460 UVMHIST_LOG(maphist, "<- new entry=0x%x [kentry=%d]", me, 461 ((map->flags & VM_MAP_INTRSAFE) != 0 || map == kernel_map), 0, 0); 462 return (me); 463 } 464 465 /* 466 * uvm_mapent_alloc_split: allocate a map entry for clipping. 467 */ 468 469 static struct vm_map_entry * 470 uvm_mapent_alloc_split(struct vm_map *map, 471 const struct vm_map_entry *old_entry, int flags, 472 struct uvm_mapent_reservation *umr) 473 { 474 struct vm_map_entry *me; 475 476 KASSERT(!VM_MAP_USE_KMAPENT(map) || 477 (old_entry->flags & UVM_MAP_QUANTUM) || !UMR_EMPTY(umr)); 478 479 if (old_entry->flags & UVM_MAP_QUANTUM) { 480 int s; 481 struct vm_map_kernel *vmk = vm_map_to_kernel(map); 482 483 s = splvm(); 484 simple_lock(&uvm.kentry_lock); 485 me = vmk->vmk_merged_entries; 486 KASSERT(me); 487 vmk->vmk_merged_entries = me->next; 488 simple_unlock(&uvm.kentry_lock); 489 splx(s); 490 KASSERT(me->flags & UVM_MAP_QUANTUM); 491 } else { 492 me = uvm_mapent_alloc(map, flags); 493 } 494 495 return me; 496 } 497 498 /* 499 * uvm_mapent_free: free map entry 500 */ 501 502 static void 503 uvm_mapent_free(struct vm_map_entry *me) 504 { 505 UVMHIST_FUNC("uvm_mapent_free"); UVMHIST_CALLED(maphist); 506 507 UVMHIST_LOG(maphist,"<- freeing map entry=0x%x [flags=%d]", 508 me, me->flags, 0, 0); 509 if (me->flags & UVM_MAP_KERNEL) { 510 uvm_kmapent_free(me); 511 } else { 512 pool_put(&uvm_map_entry_pool, me); 513 } 514 } 515 516 /* 517 * uvm_mapent_free_merged: free merged map entry 518 * 519 * => keep the entry if needed. 520 * => caller shouldn't hold map locked if VM_MAP_USE_KMAPENT(map) is true. 521 */ 522 523 static void 524 uvm_mapent_free_merged(struct vm_map *map, struct vm_map_entry *me) 525 { 526 527 KASSERT(!(me->flags & UVM_MAP_KERNEL) || uvm_kmapent_map(me) == map); 528 529 if (me->flags & UVM_MAP_QUANTUM) { 530 /* 531 * keep this entry for later splitting. 532 */ 533 struct vm_map_kernel *vmk; 534 int s; 535 536 KASSERT(VM_MAP_IS_KERNEL(map)); 537 KASSERT(!VM_MAP_USE_KMAPENT(map) || 538 (me->flags & UVM_MAP_KERNEL)); 539 540 vmk = vm_map_to_kernel(map); 541 s = splvm(); 542 simple_lock(&uvm.kentry_lock); 543 me->next = vmk->vmk_merged_entries; 544 vmk->vmk_merged_entries = me; 545 simple_unlock(&uvm.kentry_lock); 546 splx(s); 547 } else { 548 uvm_mapent_free(me); 549 } 550 } 551 552 /* 553 * uvm_mapent_copy: copy a map entry, preserving flags 554 */ 555 556 static inline void 557 uvm_mapent_copy(struct vm_map_entry *src, struct vm_map_entry *dst) 558 { 559 560 memcpy(dst, src, ((char *)&src->uvm_map_entry_stop_copy) - 561 ((char *)src)); 562 } 563 564 /* 565 * uvm_map_entry_unwire: unwire a map entry 566 * 567 * => map should be locked by caller 568 */ 569 570 static inline void 571 uvm_map_entry_unwire(struct vm_map *map, struct vm_map_entry *entry) 572 { 573 574 entry->wired_count = 0; 575 uvm_fault_unwire_locked(map, entry->start, entry->end); 576 } 577 578 579 /* 580 * wrapper for calling amap_ref() 581 */ 582 static inline void 583 uvm_map_reference_amap(struct vm_map_entry *entry, int flags) 584 { 585 586 amap_ref(entry->aref.ar_amap, entry->aref.ar_pageoff, 587 (entry->end - entry->start) >> PAGE_SHIFT, flags); 588 } 589 590 591 /* 592 * wrapper for calling amap_unref() 593 */ 594 static inline void 595 uvm_map_unreference_amap(struct vm_map_entry *entry, int flags) 596 { 597 598 amap_unref(entry->aref.ar_amap, entry->aref.ar_pageoff, 599 (entry->end - entry->start) >> PAGE_SHIFT, flags); 600 } 601 602 603 /* 604 * uvm_map_init: init mapping system at boot time. note that we allocate 605 * and init the static pool of struct vm_map_entry *'s for the kernel here. 606 */ 607 608 void 609 uvm_map_init(void) 610 { 611 #if defined(UVMHIST) 612 static struct uvm_history_ent maphistbuf[100]; 613 static struct uvm_history_ent pdhistbuf[100]; 614 #endif 615 616 /* 617 * first, init logging system. 618 */ 619 620 UVMHIST_FUNC("uvm_map_init"); 621 UVMHIST_INIT_STATIC(maphist, maphistbuf); 622 UVMHIST_INIT_STATIC(pdhist, pdhistbuf); 623 UVMHIST_CALLED(maphist); 624 UVMHIST_LOG(maphist,"<starting uvm map system>", 0, 0, 0, 0); 625 626 /* 627 * initialize the global lock for kernel map entry. 628 * 629 * XXX is it worth to have per-map lock instead? 630 */ 631 632 simple_lock_init(&uvm.kentry_lock); 633 } 634 635 /* 636 * clippers 637 */ 638 639 /* 640 * uvm_map_clip_start: ensure that the entry begins at or after 641 * the starting address, if it doesn't we split the entry. 642 * 643 * => caller should use UVM_MAP_CLIP_START macro rather than calling 644 * this directly 645 * => map must be locked by caller 646 */ 647 648 void 649 uvm_map_clip_start(struct vm_map *map, struct vm_map_entry *entry, 650 vaddr_t start, struct uvm_mapent_reservation *umr) 651 { 652 struct vm_map_entry *new_entry; 653 vaddr_t new_adj; 654 655 /* uvm_map_simplify_entry(map, entry); */ /* XXX */ 656 657 uvm_tree_sanity(map, "clip_start entry"); 658 659 /* 660 * Split off the front portion. note that we must insert the new 661 * entry BEFORE this one, so that this entry has the specified 662 * starting address. 663 */ 664 new_entry = uvm_mapent_alloc_split(map, entry, 0, umr); 665 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */ 666 667 new_entry->end = start; 668 new_adj = start - new_entry->start; 669 if (entry->object.uvm_obj) 670 entry->offset += new_adj; /* shift start over */ 671 672 /* Does not change order for the RB tree */ 673 entry->start = start; 674 675 if (new_entry->aref.ar_amap) { 676 amap_splitref(&new_entry->aref, &entry->aref, new_adj); 677 } 678 679 uvm_map_entry_link(map, entry->prev, new_entry); 680 681 if (UVM_ET_ISSUBMAP(entry)) { 682 /* ... unlikely to happen, but play it safe */ 683 uvm_map_reference(new_entry->object.sub_map); 684 } else { 685 if (UVM_ET_ISOBJ(entry) && 686 entry->object.uvm_obj->pgops && 687 entry->object.uvm_obj->pgops->pgo_reference) 688 entry->object.uvm_obj->pgops->pgo_reference( 689 entry->object.uvm_obj); 690 } 691 692 uvm_tree_sanity(map, "clip_start leave"); 693 } 694 695 /* 696 * uvm_map_clip_end: ensure that the entry ends at or before 697 * the ending address, if it does't we split the reference 698 * 699 * => caller should use UVM_MAP_CLIP_END macro rather than calling 700 * this directly 701 * => map must be locked by caller 702 */ 703 704 void 705 uvm_map_clip_end(struct vm_map *map, struct vm_map_entry *entry, vaddr_t end, 706 struct uvm_mapent_reservation *umr) 707 { 708 struct vm_map_entry * new_entry; 709 vaddr_t new_adj; /* #bytes we move start forward */ 710 711 uvm_tree_sanity(map, "clip_end entry"); 712 713 /* 714 * Create a new entry and insert it 715 * AFTER the specified entry 716 */ 717 new_entry = uvm_mapent_alloc_split(map, entry, 0, umr); 718 uvm_mapent_copy(entry, new_entry); /* entry -> new_entry */ 719 720 new_entry->start = entry->end = end; 721 new_adj = end - entry->start; 722 if (new_entry->object.uvm_obj) 723 new_entry->offset += new_adj; 724 725 if (entry->aref.ar_amap) 726 amap_splitref(&entry->aref, &new_entry->aref, new_adj); 727 728 uvm_rb_fixup(map, entry); 729 730 uvm_map_entry_link(map, entry, new_entry); 731 732 if (UVM_ET_ISSUBMAP(entry)) { 733 /* ... unlikely to happen, but play it safe */ 734 uvm_map_reference(new_entry->object.sub_map); 735 } else { 736 if (UVM_ET_ISOBJ(entry) && 737 entry->object.uvm_obj->pgops && 738 entry->object.uvm_obj->pgops->pgo_reference) 739 entry->object.uvm_obj->pgops->pgo_reference( 740 entry->object.uvm_obj); 741 } 742 743 uvm_tree_sanity(map, "clip_end leave"); 744 } 745 746 747 /* 748 * M A P - m a i n e n t r y p o i n t 749 */ 750 /* 751 * uvm_map: establish a valid mapping in a map 752 * 753 * => assume startp is page aligned. 754 * => assume size is a multiple of PAGE_SIZE. 755 * => assume sys_mmap provides enough of a "hint" to have us skip 756 * over text/data/bss area. 757 * => map must be unlocked (we will lock it) 758 * => <uobj,uoffset> value meanings (4 cases): 759 * [1] <NULL,uoffset> == uoffset is a hint for PMAP_PREFER 760 * [2] <NULL,UVM_UNKNOWN_OFFSET> == don't PMAP_PREFER 761 * [3] <uobj,uoffset> == normal mapping 762 * [4] <uobj,UVM_UNKNOWN_OFFSET> == uvm_map finds offset based on VA 763 * 764 * case [4] is for kernel mappings where we don't know the offset until 765 * we've found a virtual address. note that kernel object offsets are 766 * always relative to vm_map_min(kernel_map). 767 * 768 * => if `align' is non-zero, we align the virtual address to the specified 769 * alignment. 770 * this is provided as a mechanism for large pages. 771 * 772 * => XXXCDC: need way to map in external amap? 773 */ 774 775 int 776 uvm_map(struct vm_map *map, vaddr_t *startp /* IN/OUT */, vsize_t size, 777 struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags) 778 { 779 struct uvm_map_args args; 780 struct vm_map_entry *new_entry; 781 int error; 782 783 KASSERT((flags & UVM_FLAG_QUANTUM) == 0 || VM_MAP_IS_KERNEL(map)); 784 KASSERT((size & PAGE_MASK) == 0); 785 786 /* 787 * for pager_map, allocate the new entry first to avoid sleeping 788 * for memory while we have the map locked. 789 * 790 * besides, because we allocates entries for in-kernel maps 791 * a bit differently (cf. uvm_kmapent_alloc/free), we need to 792 * allocate them before locking the map. 793 */ 794 795 new_entry = NULL; 796 if (VM_MAP_USE_KMAPENT(map) || (flags & UVM_FLAG_QUANTUM) || 797 map == pager_map) { 798 new_entry = uvm_mapent_alloc(map, (flags & UVM_FLAG_NOWAIT)); 799 if (__predict_false(new_entry == NULL)) 800 return ENOMEM; 801 if (flags & UVM_FLAG_QUANTUM) 802 new_entry->flags |= UVM_MAP_QUANTUM; 803 } 804 if (map == pager_map) 805 flags |= UVM_FLAG_NOMERGE; 806 807 error = uvm_map_prepare(map, *startp, size, uobj, uoffset, align, 808 flags, &args); 809 if (!error) { 810 error = uvm_map_enter(map, &args, new_entry); 811 *startp = args.uma_start; 812 } else if (new_entry) { 813 uvm_mapent_free(new_entry); 814 } 815 816 #if defined(DEBUG) 817 if (!error && VM_MAP_IS_KERNEL(map)) { 818 uvm_km_check_empty(*startp, *startp + size, 819 (map->flags & VM_MAP_INTRSAFE) != 0); 820 } 821 #endif /* defined(DEBUG) */ 822 823 return error; 824 } 825 826 int 827 uvm_map_prepare(struct vm_map *map, vaddr_t start, vsize_t size, 828 struct uvm_object *uobj, voff_t uoffset, vsize_t align, uvm_flag_t flags, 829 struct uvm_map_args *args) 830 { 831 struct vm_map_entry *prev_entry; 832 vm_prot_t prot = UVM_PROTECTION(flags); 833 vm_prot_t maxprot = UVM_MAXPROTECTION(flags); 834 835 UVMHIST_FUNC("uvm_map_prepare"); 836 UVMHIST_CALLED(maphist); 837 838 UVMHIST_LOG(maphist, "(map=0x%x, start=0x%x, size=%d, flags=0x%x)", 839 map, start, size, flags); 840 UVMHIST_LOG(maphist, " uobj/offset 0x%x/%d", uobj, uoffset,0,0); 841 842 /* 843 * detect a popular device driver bug. 844 */ 845 846 KASSERT(doing_shutdown || curlwp != NULL || 847 (map->flags & VM_MAP_INTRSAFE)); 848 849 /* 850 * zero-sized mapping doesn't make any sense. 851 */ 852 KASSERT(size > 0); 853 854 KASSERT((~flags & (UVM_FLAG_NOWAIT | UVM_FLAG_WAITVA)) != 0); 855 856 uvm_tree_sanity(map, "map entry"); 857 858 /* 859 * check sanity of protection code 860 */ 861 862 if ((prot & maxprot) != prot) { 863 UVMHIST_LOG(maphist, "<- prot. failure: prot=0x%x, max=0x%x", 864 prot, maxprot,0,0); 865 return EACCES; 866 } 867 868 /* 869 * figure out where to put new VM range 870 */ 871 872 retry: 873 if (vm_map_lock_try(map) == FALSE) { 874 if (flags & UVM_FLAG_TRYLOCK) { 875 return EAGAIN; 876 } 877 vm_map_lock(map); /* could sleep here */ 878 } 879 if ((prev_entry = uvm_map_findspace(map, start, size, &start, 880 uobj, uoffset, align, flags)) == NULL) { 881 unsigned int timestamp; 882 883 if ((flags & UVM_FLAG_WAITVA) == 0) { 884 UVMHIST_LOG(maphist,"<- uvm_map_findspace failed!", 885 0,0,0,0); 886 vm_map_unlock(map); 887 return ENOMEM; 888 } 889 timestamp = map->timestamp; 890 UVMHIST_LOG(maphist,"waiting va timestamp=0x%x", 891 timestamp,0,0,0); 892 simple_lock(&map->flags_lock); 893 map->flags |= VM_MAP_WANTVA; 894 simple_unlock(&map->flags_lock); 895 vm_map_unlock(map); 896 897 /* 898 * wait until someone does unmap. 899 * XXX fragile locking 900 */ 901 902 simple_lock(&map->flags_lock); 903 while ((map->flags & VM_MAP_WANTVA) != 0 && 904 map->timestamp == timestamp) { 905 ltsleep(&map->header, PVM, "vmmapva", 0, 906 &map->flags_lock); 907 } 908 simple_unlock(&map->flags_lock); 909 goto retry; 910 } 911 912 #ifdef PMAP_GROWKERNEL 913 /* 914 * If the kernel pmap can't map the requested space, 915 * then allocate more resources for it. 916 */ 917 if (map == kernel_map && uvm_maxkaddr < (start + size)) 918 uvm_maxkaddr = pmap_growkernel(start + size); 919 #endif 920 921 UVMMAP_EVCNT_INCR(map_call); 922 923 /* 924 * if uobj is null, then uoffset is either a VAC hint for PMAP_PREFER 925 * [typically from uvm_map_reserve] or it is UVM_UNKNOWN_OFFSET. in 926 * either case we want to zero it before storing it in the map entry 927 * (because it looks strange and confusing when debugging...) 928 * 929 * if uobj is not null 930 * if uoffset is not UVM_UNKNOWN_OFFSET then we have a normal mapping 931 * and we do not need to change uoffset. 932 * if uoffset is UVM_UNKNOWN_OFFSET then we need to find the offset 933 * now (based on the starting address of the map). this case is 934 * for kernel object mappings where we don't know the offset until 935 * the virtual address is found (with uvm_map_findspace). the 936 * offset is the distance we are from the start of the map. 937 */ 938 939 if (uobj == NULL) { 940 uoffset = 0; 941 } else { 942 if (uoffset == UVM_UNKNOWN_OFFSET) { 943 KASSERT(UVM_OBJ_IS_KERN_OBJECT(uobj)); 944 uoffset = start - vm_map_min(kernel_map); 945 } 946 } 947 948 args->uma_flags = flags; 949 args->uma_prev = prev_entry; 950 args->uma_start = start; 951 args->uma_size = size; 952 args->uma_uobj = uobj; 953 args->uma_uoffset = uoffset; 954 955 return 0; 956 } 957 958 int 959 uvm_map_enter(struct vm_map *map, const struct uvm_map_args *args, 960 struct vm_map_entry *new_entry) 961 { 962 struct vm_map_entry *prev_entry = args->uma_prev; 963 struct vm_map_entry *dead = NULL; 964 965 const uvm_flag_t flags = args->uma_flags; 966 const vm_prot_t prot = UVM_PROTECTION(flags); 967 const vm_prot_t maxprot = UVM_MAXPROTECTION(flags); 968 const vm_inherit_t inherit = UVM_INHERIT(flags); 969 const int amapwaitflag = (flags & UVM_FLAG_NOWAIT) ? 970 AMAP_EXTEND_NOWAIT : 0; 971 const int advice = UVM_ADVICE(flags); 972 const int meflagval = (flags & UVM_FLAG_QUANTUM) ? 973 UVM_MAP_QUANTUM : 0; 974 975 vaddr_t start = args->uma_start; 976 vsize_t size = args->uma_size; 977 struct uvm_object *uobj = args->uma_uobj; 978 voff_t uoffset = args->uma_uoffset; 979 980 const int kmap = (vm_map_pmap(map) == pmap_kernel()); 981 int merged = 0; 982 int error; 983 int newetype; 984 985 UVMHIST_FUNC("uvm_map_enter"); 986 UVMHIST_CALLED(maphist); 987 988 UVMHIST_LOG(maphist, "(map=0x%x, start=0x%x, size=%d, flags=0x%x)", 989 map, start, size, flags); 990 UVMHIST_LOG(maphist, " uobj/offset 0x%x/%d", uobj, uoffset,0,0); 991 992 if (flags & UVM_FLAG_QUANTUM) { 993 KASSERT(new_entry); 994 KASSERT(new_entry->flags & UVM_MAP_QUANTUM); 995 } 996 997 if (uobj) 998 newetype = UVM_ET_OBJ; 999 else 1000 newetype = 0; 1001 1002 if (flags & UVM_FLAG_COPYONW) { 1003 newetype |= UVM_ET_COPYONWRITE; 1004 if ((flags & UVM_FLAG_OVERLAY) == 0) 1005 newetype |= UVM_ET_NEEDSCOPY; 1006 } 1007 1008 /* 1009 * try and insert in map by extending previous entry, if possible. 1010 * XXX: we don't try and pull back the next entry. might be useful 1011 * for a stack, but we are currently allocating our stack in advance. 1012 */ 1013 1014 if (flags & UVM_FLAG_NOMERGE) 1015 goto nomerge; 1016 1017 if (prev_entry->end == start && 1018 prev_entry != &map->header && 1019 UVM_ET_ISCOMPATIBLE(prev_entry, newetype, uobj, meflagval, 1020 prot, maxprot, inherit, advice, 0)) { 1021 1022 if (uobj && prev_entry->offset + 1023 (prev_entry->end - prev_entry->start) != uoffset) 1024 goto forwardmerge; 1025 1026 /* 1027 * can't extend a shared amap. note: no need to lock amap to 1028 * look at refs since we don't care about its exact value. 1029 * if it is one (i.e. we have only reference) it will stay there 1030 */ 1031 1032 if (prev_entry->aref.ar_amap && 1033 amap_refs(prev_entry->aref.ar_amap) != 1) { 1034 goto forwardmerge; 1035 } 1036 1037 if (prev_entry->aref.ar_amap) { 1038 error = amap_extend(prev_entry, size, 1039 amapwaitflag | AMAP_EXTEND_FORWARDS); 1040 if (error) 1041 goto nomerge; 1042 } 1043 1044 if (kmap) 1045 UVMMAP_EVCNT_INCR(kbackmerge); 1046 else 1047 UVMMAP_EVCNT_INCR(ubackmerge); 1048 UVMHIST_LOG(maphist," starting back merge", 0, 0, 0, 0); 1049 1050 /* 1051 * drop our reference to uobj since we are extending a reference 1052 * that we already have (the ref count can not drop to zero). 1053 */ 1054 1055 if (uobj && uobj->pgops->pgo_detach) 1056 uobj->pgops->pgo_detach(uobj); 1057 1058 prev_entry->end += size; 1059 uvm_rb_fixup(map, prev_entry); 1060 1061 uvm_tree_sanity(map, "map backmerged"); 1062 1063 UVMHIST_LOG(maphist,"<- done (via backmerge)!", 0, 0, 0, 0); 1064 merged++; 1065 } 1066 1067 forwardmerge: 1068 if (prev_entry->next->start == (start + size) && 1069 prev_entry->next != &map->header && 1070 UVM_ET_ISCOMPATIBLE(prev_entry->next, newetype, uobj, meflagval, 1071 prot, maxprot, inherit, advice, 0)) { 1072 1073 if (uobj && prev_entry->next->offset != uoffset + size) 1074 goto nomerge; 1075 1076 /* 1077 * can't extend a shared amap. note: no need to lock amap to 1078 * look at refs since we don't care about its exact value. 1079 * if it is one (i.e. we have only reference) it will stay there. 1080 * 1081 * note that we also can't merge two amaps, so if we 1082 * merged with the previous entry which has an amap, 1083 * and the next entry also has an amap, we give up. 1084 * 1085 * Interesting cases: 1086 * amap, new, amap -> give up second merge (single fwd extend) 1087 * amap, new, none -> double forward extend (extend again here) 1088 * none, new, amap -> double backward extend (done here) 1089 * uobj, new, amap -> single backward extend (done here) 1090 * 1091 * XXX should we attempt to deal with someone refilling 1092 * the deallocated region between two entries that are 1093 * backed by the same amap (ie, arefs is 2, "prev" and 1094 * "next" refer to it, and adding this allocation will 1095 * close the hole, thus restoring arefs to 1 and 1096 * deallocating the "next" vm_map_entry)? -- @@@ 1097 */ 1098 1099 if (prev_entry->next->aref.ar_amap && 1100 (amap_refs(prev_entry->next->aref.ar_amap) != 1 || 1101 (merged && prev_entry->aref.ar_amap))) { 1102 goto nomerge; 1103 } 1104 1105 if (merged) { 1106 /* 1107 * Try to extend the amap of the previous entry to 1108 * cover the next entry as well. If it doesn't work 1109 * just skip on, don't actually give up, since we've 1110 * already completed the back merge. 1111 */ 1112 if (prev_entry->aref.ar_amap) { 1113 if (amap_extend(prev_entry, 1114 prev_entry->next->end - 1115 prev_entry->next->start, 1116 amapwaitflag | AMAP_EXTEND_FORWARDS)) 1117 goto nomerge; 1118 } 1119 1120 /* 1121 * Try to extend the amap of the *next* entry 1122 * back to cover the new allocation *and* the 1123 * previous entry as well (the previous merge 1124 * didn't have an amap already otherwise we 1125 * wouldn't be checking here for an amap). If 1126 * it doesn't work just skip on, again, don't 1127 * actually give up, since we've already 1128 * completed the back merge. 1129 */ 1130 else if (prev_entry->next->aref.ar_amap) { 1131 if (amap_extend(prev_entry->next, 1132 prev_entry->end - 1133 prev_entry->start, 1134 amapwaitflag | AMAP_EXTEND_BACKWARDS)) 1135 goto nomerge; 1136 } 1137 } else { 1138 /* 1139 * Pull the next entry's amap backwards to cover this 1140 * new allocation. 1141 */ 1142 if (prev_entry->next->aref.ar_amap) { 1143 error = amap_extend(prev_entry->next, size, 1144 amapwaitflag | AMAP_EXTEND_BACKWARDS); 1145 if (error) 1146 goto nomerge; 1147 } 1148 } 1149 1150 if (merged) { 1151 if (kmap) { 1152 UVMMAP_EVCNT_DECR(kbackmerge); 1153 UVMMAP_EVCNT_INCR(kbimerge); 1154 } else { 1155 UVMMAP_EVCNT_DECR(ubackmerge); 1156 UVMMAP_EVCNT_INCR(ubimerge); 1157 } 1158 } else { 1159 if (kmap) 1160 UVMMAP_EVCNT_INCR(kforwmerge); 1161 else 1162 UVMMAP_EVCNT_INCR(uforwmerge); 1163 } 1164 UVMHIST_LOG(maphist," starting forward merge", 0, 0, 0, 0); 1165 1166 /* 1167 * drop our reference to uobj since we are extending a reference 1168 * that we already have (the ref count can not drop to zero). 1169 * (if merged, we've already detached) 1170 */ 1171 if (uobj && uobj->pgops->pgo_detach && !merged) 1172 uobj->pgops->pgo_detach(uobj); 1173 1174 if (merged) { 1175 dead = prev_entry->next; 1176 prev_entry->end = dead->end; 1177 uvm_map_entry_unlink(map, dead); 1178 if (dead->aref.ar_amap != NULL) { 1179 prev_entry->aref = dead->aref; 1180 dead->aref.ar_amap = NULL; 1181 } 1182 } else { 1183 prev_entry->next->start -= size; 1184 if (prev_entry != &map->header) 1185 uvm_rb_fixup(map, prev_entry); 1186 if (uobj) 1187 prev_entry->next->offset = uoffset; 1188 } 1189 1190 uvm_tree_sanity(map, "map forwardmerged"); 1191 1192 UVMHIST_LOG(maphist,"<- done forwardmerge", 0, 0, 0, 0); 1193 merged++; 1194 } 1195 1196 nomerge: 1197 if (!merged) { 1198 UVMHIST_LOG(maphist," allocating new map entry", 0, 0, 0, 0); 1199 if (kmap) 1200 UVMMAP_EVCNT_INCR(knomerge); 1201 else 1202 UVMMAP_EVCNT_INCR(unomerge); 1203 1204 /* 1205 * allocate new entry and link it in. 1206 */ 1207 1208 if (new_entry == NULL) { 1209 new_entry = uvm_mapent_alloc(map, 1210 (flags & UVM_FLAG_NOWAIT)); 1211 if (__predict_false(new_entry == NULL)) { 1212 error = ENOMEM; 1213 goto done; 1214 } 1215 } 1216 new_entry->start = start; 1217 new_entry->end = new_entry->start + size; 1218 new_entry->object.uvm_obj = uobj; 1219 new_entry->offset = uoffset; 1220 1221 new_entry->etype = newetype; 1222 1223 if (flags & UVM_FLAG_NOMERGE) { 1224 new_entry->flags |= UVM_MAP_NOMERGE; 1225 } 1226 1227 new_entry->protection = prot; 1228 new_entry->max_protection = maxprot; 1229 new_entry->inheritance = inherit; 1230 new_entry->wired_count = 0; 1231 new_entry->advice = advice; 1232 if (flags & UVM_FLAG_OVERLAY) { 1233 1234 /* 1235 * to_add: for BSS we overallocate a little since we 1236 * are likely to extend 1237 */ 1238 1239 vaddr_t to_add = (flags & UVM_FLAG_AMAPPAD) ? 1240 UVM_AMAP_CHUNK << PAGE_SHIFT : 0; 1241 struct vm_amap *amap = amap_alloc(size, to_add, 1242 (flags & UVM_FLAG_NOWAIT) ? M_NOWAIT : M_WAITOK); 1243 if (__predict_false(amap == NULL)) { 1244 error = ENOMEM; 1245 goto done; 1246 } 1247 new_entry->aref.ar_pageoff = 0; 1248 new_entry->aref.ar_amap = amap; 1249 } else { 1250 new_entry->aref.ar_pageoff = 0; 1251 new_entry->aref.ar_amap = NULL; 1252 } 1253 uvm_map_entry_link(map, prev_entry, new_entry); 1254 1255 /* 1256 * Update the free space hint 1257 */ 1258 1259 if ((map->first_free == prev_entry) && 1260 (prev_entry->end >= new_entry->start)) 1261 map->first_free = new_entry; 1262 1263 new_entry = NULL; 1264 } 1265 1266 map->size += size; 1267 1268 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0); 1269 1270 error = 0; 1271 done: 1272 vm_map_unlock(map); 1273 if (new_entry) { 1274 if (error == 0) { 1275 KDASSERT(merged); 1276 uvm_mapent_free_merged(map, new_entry); 1277 } else { 1278 uvm_mapent_free(new_entry); 1279 } 1280 } 1281 if (dead) { 1282 KDASSERT(merged); 1283 uvm_mapent_free_merged(map, dead); 1284 } 1285 return error; 1286 } 1287 1288 /* 1289 * uvm_map_lookup_entry: find map entry at or before an address 1290 * 1291 * => map must at least be read-locked by caller 1292 * => entry is returned in "entry" 1293 * => return value is true if address is in the returned entry 1294 */ 1295 1296 boolean_t 1297 uvm_map_lookup_entry(struct vm_map *map, vaddr_t address, 1298 struct vm_map_entry **entry /* OUT */) 1299 { 1300 struct vm_map_entry *cur; 1301 boolean_t use_tree = FALSE; 1302 UVMHIST_FUNC("uvm_map_lookup_entry"); 1303 UVMHIST_CALLED(maphist); 1304 1305 UVMHIST_LOG(maphist,"(map=0x%x,addr=0x%x,ent=0x%x)", 1306 map, address, entry, 0); 1307 1308 /* 1309 * start looking either from the head of the 1310 * list, or from the hint. 1311 */ 1312 1313 simple_lock(&map->hint_lock); 1314 cur = map->hint; 1315 simple_unlock(&map->hint_lock); 1316 1317 if (cur == &map->header) 1318 cur = cur->next; 1319 1320 UVMMAP_EVCNT_INCR(mlk_call); 1321 if (address >= cur->start) { 1322 1323 /* 1324 * go from hint to end of list. 1325 * 1326 * but first, make a quick check to see if 1327 * we are already looking at the entry we 1328 * want (which is usually the case). 1329 * note also that we don't need to save the hint 1330 * here... it is the same hint (unless we are 1331 * at the header, in which case the hint didn't 1332 * buy us anything anyway). 1333 */ 1334 1335 if (cur != &map->header && cur->end > address) { 1336 UVMMAP_EVCNT_INCR(mlk_hint); 1337 *entry = cur; 1338 UVMHIST_LOG(maphist,"<- got it via hint (0x%x)", 1339 cur, 0, 0, 0); 1340 return (TRUE); 1341 } 1342 1343 if (map->nentries > 30) 1344 use_tree = TRUE; 1345 } else { 1346 1347 /* 1348 * invalid hint. use tree. 1349 */ 1350 use_tree = TRUE; 1351 } 1352 1353 uvm_tree_sanity(map, __func__); 1354 1355 if (use_tree) { 1356 struct vm_map_entry *prev = &map->header; 1357 cur = RB_ROOT(&map->rbhead); 1358 1359 /* 1360 * Simple lookup in the tree. Happens when the hint is 1361 * invalid, or nentries reach a threshold. 1362 */ 1363 while (cur) { 1364 if (address >= cur->start) { 1365 if (address < cur->end) { 1366 *entry = cur; 1367 goto got; 1368 } 1369 prev = cur; 1370 cur = RB_RIGHT(cur, rb_entry); 1371 } else 1372 cur = RB_LEFT(cur, rb_entry); 1373 } 1374 *entry = prev; 1375 goto failed; 1376 } 1377 1378 /* 1379 * search linearly 1380 */ 1381 1382 while (cur != &map->header) { 1383 if (cur->end > address) { 1384 if (address >= cur->start) { 1385 /* 1386 * save this lookup for future 1387 * hints, and return 1388 */ 1389 1390 *entry = cur; 1391 got: 1392 SAVE_HINT(map, map->hint, *entry); 1393 UVMHIST_LOG(maphist,"<- search got it (0x%x)", 1394 cur, 0, 0, 0); 1395 KDASSERT((*entry)->start <= address); 1396 KDASSERT(address < (*entry)->end); 1397 return (TRUE); 1398 } 1399 break; 1400 } 1401 cur = cur->next; 1402 } 1403 *entry = cur->prev; 1404 failed: 1405 SAVE_HINT(map, map->hint, *entry); 1406 UVMHIST_LOG(maphist,"<- failed!",0,0,0,0); 1407 KDASSERT((*entry) == &map->header || (*entry)->end <= address); 1408 KDASSERT((*entry)->next == &map->header || 1409 address < (*entry)->next->start); 1410 return (FALSE); 1411 } 1412 1413 /* 1414 * See if the range between start and start + length fits in the gap 1415 * entry->next->start and entry->end. Returns 1 if fits, 0 if doesn't 1416 * fit, and -1 address wraps around. 1417 */ 1418 static int 1419 uvm_map_space_avail(vaddr_t *start, vsize_t length, voff_t uoffset, 1420 vsize_t align, int topdown, struct vm_map_entry *entry) 1421 { 1422 vaddr_t end; 1423 1424 #ifdef PMAP_PREFER 1425 /* 1426 * push start address forward as needed to avoid VAC alias problems. 1427 * we only do this if a valid offset is specified. 1428 */ 1429 1430 if (uoffset != UVM_UNKNOWN_OFFSET) 1431 PMAP_PREFER(uoffset, start, length, topdown); 1432 #endif 1433 if (align != 0) { 1434 if ((*start & (align - 1)) != 0) { 1435 if (topdown) 1436 *start &= ~(align - 1); 1437 else 1438 *start = roundup(*start, align); 1439 } 1440 /* 1441 * XXX Should we PMAP_PREFER() here again? 1442 * eh...i think we're okay 1443 */ 1444 } 1445 1446 /* 1447 * Find the end of the proposed new region. Be sure we didn't 1448 * wrap around the address; if so, we lose. Otherwise, if the 1449 * proposed new region fits before the next entry, we win. 1450 */ 1451 1452 end = *start + length; 1453 if (end < *start) 1454 return (-1); 1455 1456 if (entry->next->start >= end && *start >= entry->end) 1457 return (1); 1458 1459 return (0); 1460 } 1461 1462 /* 1463 * uvm_map_findspace: find "length" sized space in "map". 1464 * 1465 * => "hint" is a hint about where we want it, unless UVM_FLAG_FIXED is 1466 * set in "flags" (in which case we insist on using "hint"). 1467 * => "result" is VA returned 1468 * => uobj/uoffset are to be used to handle VAC alignment, if required 1469 * => if "align" is non-zero, we attempt to align to that value. 1470 * => caller must at least have read-locked map 1471 * => returns NULL on failure, or pointer to prev. map entry if success 1472 * => note this is a cross between the old vm_map_findspace and vm_map_find 1473 */ 1474 1475 struct vm_map_entry * 1476 uvm_map_findspace(struct vm_map *map, vaddr_t hint, vsize_t length, 1477 vaddr_t *result /* OUT */, struct uvm_object *uobj, voff_t uoffset, 1478 vsize_t align, int flags) 1479 { 1480 struct vm_map_entry *entry; 1481 struct vm_map_entry *child, *prev, *tmp; 1482 vaddr_t orig_hint; 1483 const int topdown = map->flags & VM_MAP_TOPDOWN; 1484 UVMHIST_FUNC("uvm_map_findspace"); 1485 UVMHIST_CALLED(maphist); 1486 1487 UVMHIST_LOG(maphist, "(map=0x%x, hint=0x%x, len=%d, flags=0x%x)", 1488 map, hint, length, flags); 1489 KASSERT((align & (align - 1)) == 0); 1490 KASSERT((flags & UVM_FLAG_FIXED) == 0 || align == 0); 1491 1492 uvm_tree_sanity(map, "map_findspace entry"); 1493 1494 /* 1495 * remember the original hint. if we are aligning, then we 1496 * may have to try again with no alignment constraint if 1497 * we fail the first time. 1498 */ 1499 1500 orig_hint = hint; 1501 if (hint < vm_map_min(map)) { /* check ranges ... */ 1502 if (flags & UVM_FLAG_FIXED) { 1503 UVMHIST_LOG(maphist,"<- VA below map range",0,0,0,0); 1504 return (NULL); 1505 } 1506 hint = vm_map_min(map); 1507 } 1508 if (hint > vm_map_max(map)) { 1509 UVMHIST_LOG(maphist,"<- VA 0x%x > range [0x%x->0x%x]", 1510 hint, vm_map_min(map), vm_map_max(map), 0); 1511 return (NULL); 1512 } 1513 1514 /* 1515 * Look for the first possible address; if there's already 1516 * something at this address, we have to start after it. 1517 */ 1518 1519 /* 1520 * @@@: there are four, no, eight cases to consider. 1521 * 1522 * 0: found, fixed, bottom up -> fail 1523 * 1: found, fixed, top down -> fail 1524 * 2: found, not fixed, bottom up -> start after entry->end, 1525 * loop up 1526 * 3: found, not fixed, top down -> start before entry->start, 1527 * loop down 1528 * 4: not found, fixed, bottom up -> check entry->next->start, fail 1529 * 5: not found, fixed, top down -> check entry->next->start, fail 1530 * 6: not found, not fixed, bottom up -> check entry->next->start, 1531 * loop up 1532 * 7: not found, not fixed, top down -> check entry->next->start, 1533 * loop down 1534 * 1535 * as you can see, it reduces to roughly five cases, and that 1536 * adding top down mapping only adds one unique case (without 1537 * it, there would be four cases). 1538 */ 1539 1540 if ((flags & UVM_FLAG_FIXED) == 0 && hint == vm_map_min(map)) { 1541 entry = map->first_free; 1542 } else { 1543 if (uvm_map_lookup_entry(map, hint, &entry)) { 1544 /* "hint" address already in use ... */ 1545 if (flags & UVM_FLAG_FIXED) { 1546 UVMHIST_LOG(maphist, "<- fixed & VA in use", 1547 0, 0, 0, 0); 1548 return (NULL); 1549 } 1550 if (topdown) 1551 /* Start from lower gap. */ 1552 entry = entry->prev; 1553 } else if (flags & UVM_FLAG_FIXED) { 1554 if (entry->next->start >= hint + length && 1555 hint + length > hint) 1556 goto found; 1557 1558 /* "hint" address is gap but too small */ 1559 UVMHIST_LOG(maphist, "<- fixed mapping failed", 1560 0, 0, 0, 0); 1561 return (NULL); /* only one shot at it ... */ 1562 } else { 1563 /* 1564 * See if given hint fits in this gap. 1565 */ 1566 switch (uvm_map_space_avail(&hint, length, 1567 uoffset, align, topdown, entry)) { 1568 case 1: 1569 goto found; 1570 case -1: 1571 goto wraparound; 1572 } 1573 1574 if (topdown) { 1575 /* 1576 * Still there is a chance to fit 1577 * if hint > entry->end. 1578 */ 1579 } else { 1580 /* Start from higher gap. */ 1581 entry = entry->next; 1582 if (entry == &map->header) 1583 goto notfound; 1584 goto nextgap; 1585 } 1586 } 1587 } 1588 1589 /* 1590 * Note that all UVM_FLAGS_FIXED case is already handled. 1591 */ 1592 KDASSERT((flags & UVM_FLAG_FIXED) == 0); 1593 1594 /* Try to find the space in the red-black tree */ 1595 1596 /* Check slot before any entry */ 1597 hint = topdown ? entry->next->start - length : entry->end; 1598 switch (uvm_map_space_avail(&hint, length, uoffset, align, 1599 topdown, entry)) { 1600 case 1: 1601 goto found; 1602 case -1: 1603 goto wraparound; 1604 } 1605 1606 nextgap: 1607 KDASSERT((flags & UVM_FLAG_FIXED) == 0); 1608 /* If there is not enough space in the whole tree, we fail */ 1609 tmp = RB_ROOT(&map->rbhead); 1610 if (tmp == NULL || tmp->space < length) 1611 goto notfound; 1612 1613 prev = NULL; /* previous candidate */ 1614 1615 /* Find an entry close to hint that has enough space */ 1616 for (; tmp;) { 1617 KASSERT(tmp->next->start == tmp->end + tmp->ownspace); 1618 if (topdown) { 1619 if (tmp->next->start < hint + length && 1620 (prev == NULL || tmp->end > prev->end)) { 1621 if (tmp->ownspace >= length) 1622 prev = tmp; 1623 else if ((child = RB_LEFT(tmp, rb_entry)) 1624 != NULL && child->space >= length) 1625 prev = tmp; 1626 } 1627 } else { 1628 if (tmp->end >= hint && 1629 (prev == NULL || tmp->end < prev->end)) { 1630 if (tmp->ownspace >= length) 1631 prev = tmp; 1632 else if ((child = RB_RIGHT(tmp, rb_entry)) 1633 != NULL && child->space >= length) 1634 prev = tmp; 1635 } 1636 } 1637 if (tmp->next->start < hint + length) 1638 child = RB_RIGHT(tmp, rb_entry); 1639 else if (tmp->end > hint) 1640 child = RB_LEFT(tmp, rb_entry); 1641 else { 1642 if (tmp->ownspace >= length) 1643 break; 1644 if (topdown) 1645 child = RB_LEFT(tmp, rb_entry); 1646 else 1647 child = RB_RIGHT(tmp, rb_entry); 1648 } 1649 if (child == NULL || child->space < length) 1650 break; 1651 tmp = child; 1652 } 1653 1654 if (tmp != NULL && tmp->start < hint && hint < tmp->next->start) { 1655 /* 1656 * Check if the entry that we found satifies the 1657 * space requirement 1658 */ 1659 if (topdown) { 1660 if (hint > tmp->next->start - length) 1661 hint = tmp->next->start - length; 1662 } else { 1663 if (hint < tmp->end) 1664 hint = tmp->end; 1665 } 1666 switch (uvm_map_space_avail(&hint, length, uoffset, align, 1667 topdown, tmp)) { 1668 case 1: 1669 entry = tmp; 1670 goto found; 1671 case -1: 1672 goto wraparound; 1673 } 1674 if (tmp->ownspace >= length) 1675 goto listsearch; 1676 } 1677 if (prev == NULL) 1678 goto notfound; 1679 1680 if (topdown) { 1681 KASSERT(orig_hint >= prev->next->start - length || 1682 prev->next->start - length > prev->next->start); 1683 hint = prev->next->start - length; 1684 } else { 1685 KASSERT(orig_hint <= prev->end); 1686 hint = prev->end; 1687 } 1688 switch (uvm_map_space_avail(&hint, length, uoffset, align, 1689 topdown, prev)) { 1690 case 1: 1691 entry = prev; 1692 goto found; 1693 case -1: 1694 goto wraparound; 1695 } 1696 if (prev->ownspace >= length) 1697 goto listsearch; 1698 1699 if (topdown) 1700 tmp = RB_LEFT(prev, rb_entry); 1701 else 1702 tmp = RB_RIGHT(prev, rb_entry); 1703 for (;;) { 1704 KASSERT(tmp && tmp->space >= length); 1705 if (topdown) 1706 child = RB_RIGHT(tmp, rb_entry); 1707 else 1708 child = RB_LEFT(tmp, rb_entry); 1709 if (child && child->space >= length) { 1710 tmp = child; 1711 continue; 1712 } 1713 if (tmp->ownspace >= length) 1714 break; 1715 if (topdown) 1716 tmp = RB_LEFT(tmp, rb_entry); 1717 else 1718 tmp = RB_RIGHT(tmp, rb_entry); 1719 } 1720 1721 if (topdown) { 1722 KASSERT(orig_hint >= tmp->next->start - length || 1723 tmp->next->start - length > tmp->next->start); 1724 hint = tmp->next->start - length; 1725 } else { 1726 KASSERT(orig_hint <= tmp->end); 1727 hint = tmp->end; 1728 } 1729 switch (uvm_map_space_avail(&hint, length, uoffset, align, 1730 topdown, tmp)) { 1731 case 1: 1732 entry = tmp; 1733 goto found; 1734 case -1: 1735 goto wraparound; 1736 } 1737 1738 /* 1739 * The tree fails to find an entry because of offset or alignment 1740 * restrictions. Search the list instead. 1741 */ 1742 listsearch: 1743 /* 1744 * Look through the rest of the map, trying to fit a new region in 1745 * the gap between existing regions, or after the very last region. 1746 * note: entry->end = base VA of current gap, 1747 * entry->next->start = VA of end of current gap 1748 */ 1749 1750 for (;;) { 1751 /* Update hint for current gap. */ 1752 hint = topdown ? entry->next->start - length : entry->end; 1753 1754 /* See if it fits. */ 1755 switch (uvm_map_space_avail(&hint, length, uoffset, align, 1756 topdown, entry)) { 1757 case 1: 1758 goto found; 1759 case -1: 1760 goto wraparound; 1761 } 1762 1763 /* Advance to next/previous gap */ 1764 if (topdown) { 1765 if (entry == &map->header) { 1766 UVMHIST_LOG(maphist, "<- failed (off start)", 1767 0,0,0,0); 1768 goto notfound; 1769 } 1770 entry = entry->prev; 1771 } else { 1772 entry = entry->next; 1773 if (entry == &map->header) { 1774 UVMHIST_LOG(maphist, "<- failed (off end)", 1775 0,0,0,0); 1776 goto notfound; 1777 } 1778 } 1779 } 1780 1781 found: 1782 SAVE_HINT(map, map->hint, entry); 1783 *result = hint; 1784 UVMHIST_LOG(maphist,"<- got it! (result=0x%x)", hint, 0,0,0); 1785 KASSERT( topdown || hint >= orig_hint); 1786 KASSERT(!topdown || hint <= orig_hint); 1787 KASSERT(entry->end <= hint); 1788 KASSERT(hint + length <= entry->next->start); 1789 return (entry); 1790 1791 wraparound: 1792 UVMHIST_LOG(maphist, "<- failed (wrap around)", 0,0,0,0); 1793 1794 return (NULL); 1795 1796 notfound: 1797 UVMHIST_LOG(maphist, "<- failed (notfound)", 0,0,0,0); 1798 1799 return (NULL); 1800 } 1801 1802 /* 1803 * U N M A P - m a i n h e l p e r f u n c t i o n s 1804 */ 1805 1806 /* 1807 * uvm_unmap_remove: remove mappings from a vm_map (from "start" up to "stop") 1808 * 1809 * => caller must check alignment and size 1810 * => map must be locked by caller 1811 * => we return a list of map entries that we've remove from the map 1812 * in "entry_list" 1813 */ 1814 1815 void 1816 uvm_unmap_remove(struct vm_map *map, vaddr_t start, vaddr_t end, 1817 struct vm_map_entry **entry_list /* OUT */, 1818 struct uvm_mapent_reservation *umr, int flags) 1819 { 1820 struct vm_map_entry *entry, *first_entry, *next; 1821 vaddr_t len; 1822 UVMHIST_FUNC("uvm_unmap_remove"); UVMHIST_CALLED(maphist); 1823 1824 UVMHIST_LOG(maphist,"(map=0x%x, start=0x%x, end=0x%x)", 1825 map, start, end, 0); 1826 VM_MAP_RANGE_CHECK(map, start, end); 1827 1828 uvm_tree_sanity(map, "unmap_remove entry"); 1829 1830 /* 1831 * find first entry 1832 */ 1833 1834 if (uvm_map_lookup_entry(map, start, &first_entry) == TRUE) { 1835 /* clip and go... */ 1836 entry = first_entry; 1837 UVM_MAP_CLIP_START(map, entry, start, umr); 1838 /* critical! prevents stale hint */ 1839 SAVE_HINT(map, entry, entry->prev); 1840 } else { 1841 entry = first_entry->next; 1842 } 1843 1844 /* 1845 * Save the free space hint 1846 */ 1847 1848 if (map->first_free->start >= start) 1849 map->first_free = entry->prev; 1850 1851 /* 1852 * note: we now re-use first_entry for a different task. we remove 1853 * a number of map entries from the map and save them in a linked 1854 * list headed by "first_entry". once we remove them from the map 1855 * the caller should unlock the map and drop the references to the 1856 * backing objects [c.f. uvm_unmap_detach]. the object is to 1857 * separate unmapping from reference dropping. why? 1858 * [1] the map has to be locked for unmapping 1859 * [2] the map need not be locked for reference dropping 1860 * [3] dropping references may trigger pager I/O, and if we hit 1861 * a pager that does synchronous I/O we may have to wait for it. 1862 * [4] we would like all waiting for I/O to occur with maps unlocked 1863 * so that we don't block other threads. 1864 */ 1865 1866 first_entry = NULL; 1867 *entry_list = NULL; 1868 1869 /* 1870 * break up the area into map entry sized regions and unmap. note 1871 * that all mappings have to be removed before we can even consider 1872 * dropping references to amaps or VM objects (otherwise we could end 1873 * up with a mapping to a page on the free list which would be very bad) 1874 */ 1875 1876 while ((entry != &map->header) && (entry->start < end)) { 1877 KASSERT((entry->flags & UVM_MAP_FIRST) == 0); 1878 1879 UVM_MAP_CLIP_END(map, entry, end, umr); 1880 next = entry->next; 1881 len = entry->end - entry->start; 1882 1883 /* 1884 * unwire before removing addresses from the pmap; otherwise 1885 * unwiring will put the entries back into the pmap (XXX). 1886 */ 1887 1888 if (VM_MAPENT_ISWIRED(entry)) { 1889 uvm_map_entry_unwire(map, entry); 1890 } 1891 if (flags & UVM_FLAG_VAONLY) { 1892 1893 /* nothing */ 1894 1895 } else if ((map->flags & VM_MAP_PAGEABLE) == 0) { 1896 1897 /* 1898 * if the map is non-pageable, any pages mapped there 1899 * must be wired and entered with pmap_kenter_pa(), 1900 * and we should free any such pages immediately. 1901 * this is mostly used for kmem_map and mb_map. 1902 */ 1903 1904 if ((entry->flags & UVM_MAP_KMAPENT) == 0) { 1905 uvm_km_pgremove_intrsafe(entry->start, 1906 entry->end); 1907 pmap_kremove(entry->start, len); 1908 } 1909 } else if (UVM_ET_ISOBJ(entry) && 1910 UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) { 1911 KASSERT(vm_map_pmap(map) == pmap_kernel()); 1912 1913 /* 1914 * note: kernel object mappings are currently used in 1915 * two ways: 1916 * [1] "normal" mappings of pages in the kernel object 1917 * [2] uvm_km_valloc'd allocations in which we 1918 * pmap_enter in some non-kernel-object page 1919 * (e.g. vmapbuf). 1920 * 1921 * for case [1], we need to remove the mapping from 1922 * the pmap and then remove the page from the kernel 1923 * object (because, once pages in a kernel object are 1924 * unmapped they are no longer needed, unlike, say, 1925 * a vnode where you might want the data to persist 1926 * until flushed out of a queue). 1927 * 1928 * for case [2], we need to remove the mapping from 1929 * the pmap. there shouldn't be any pages at the 1930 * specified offset in the kernel object [but it 1931 * doesn't hurt to call uvm_km_pgremove just to be 1932 * safe?] 1933 * 1934 * uvm_km_pgremove currently does the following: 1935 * for pages in the kernel object in range: 1936 * - drops the swap slot 1937 * - uvm_pagefree the page 1938 */ 1939 1940 /* 1941 * remove mappings from pmap and drop the pages 1942 * from the object. offsets are always relative 1943 * to vm_map_min(kernel_map). 1944 */ 1945 1946 pmap_remove(pmap_kernel(), entry->start, 1947 entry->start + len); 1948 uvm_km_pgremove(entry->start, entry->end); 1949 1950 /* 1951 * null out kernel_object reference, we've just 1952 * dropped it 1953 */ 1954 1955 entry->etype &= ~UVM_ET_OBJ; 1956 entry->object.uvm_obj = NULL; 1957 } else if (UVM_ET_ISOBJ(entry) || entry->aref.ar_amap) { 1958 1959 /* 1960 * remove mappings the standard way. 1961 */ 1962 1963 pmap_remove(map->pmap, entry->start, entry->end); 1964 } 1965 1966 #if defined(DEBUG) 1967 if ((entry->flags & UVM_MAP_KMAPENT) == 0) { 1968 1969 /* 1970 * check if there's remaining mapping, 1971 * which is a bug in caller. 1972 */ 1973 1974 vaddr_t va; 1975 for (va = entry->start; va < entry->end; 1976 va += PAGE_SIZE) { 1977 if (pmap_extract(vm_map_pmap(map), va, NULL)) { 1978 panic("uvm_unmap_remove: has mapping"); 1979 } 1980 } 1981 1982 if (VM_MAP_IS_KERNEL(map)) { 1983 uvm_km_check_empty(entry->start, entry->end, 1984 (map->flags & VM_MAP_INTRSAFE) != 0); 1985 } 1986 } 1987 #endif /* defined(DEBUG) */ 1988 1989 /* 1990 * remove entry from map and put it on our list of entries 1991 * that we've nuked. then go to next entry. 1992 */ 1993 1994 UVMHIST_LOG(maphist, " removed map entry 0x%x", entry, 0, 0,0); 1995 1996 /* critical! prevents stale hint */ 1997 SAVE_HINT(map, entry, entry->prev); 1998 1999 uvm_map_entry_unlink(map, entry); 2000 KASSERT(map->size >= len); 2001 map->size -= len; 2002 entry->prev = NULL; 2003 entry->next = first_entry; 2004 first_entry = entry; 2005 entry = next; 2006 } 2007 if ((map->flags & VM_MAP_DYING) == 0) { 2008 pmap_update(vm_map_pmap(map)); 2009 } 2010 2011 uvm_tree_sanity(map, "unmap_remove leave"); 2012 2013 /* 2014 * now we've cleaned up the map and are ready for the caller to drop 2015 * references to the mapped objects. 2016 */ 2017 2018 *entry_list = first_entry; 2019 UVMHIST_LOG(maphist,"<- done!", 0, 0, 0, 0); 2020 2021 simple_lock(&map->flags_lock); 2022 if (map->flags & VM_MAP_WANTVA) { 2023 map->flags &= ~VM_MAP_WANTVA; 2024 wakeup(&map->header); 2025 } 2026 simple_unlock(&map->flags_lock); 2027 } 2028 2029 /* 2030 * uvm_unmap_detach: drop references in a chain of map entries 2031 * 2032 * => we will free the map entries as we traverse the list. 2033 */ 2034 2035 void 2036 uvm_unmap_detach(struct vm_map_entry *first_entry, int flags) 2037 { 2038 struct vm_map_entry *next_entry; 2039 UVMHIST_FUNC("uvm_unmap_detach"); UVMHIST_CALLED(maphist); 2040 2041 while (first_entry) { 2042 KASSERT(!VM_MAPENT_ISWIRED(first_entry)); 2043 UVMHIST_LOG(maphist, 2044 " detach 0x%x: amap=0x%x, obj=0x%x, submap?=%d", 2045 first_entry, first_entry->aref.ar_amap, 2046 first_entry->object.uvm_obj, 2047 UVM_ET_ISSUBMAP(first_entry)); 2048 2049 /* 2050 * drop reference to amap, if we've got one 2051 */ 2052 2053 if (first_entry->aref.ar_amap) 2054 uvm_map_unreference_amap(first_entry, flags); 2055 2056 /* 2057 * drop reference to our backing object, if we've got one 2058 */ 2059 2060 KASSERT(!UVM_ET_ISSUBMAP(first_entry)); 2061 if (UVM_ET_ISOBJ(first_entry) && 2062 first_entry->object.uvm_obj->pgops->pgo_detach) { 2063 (*first_entry->object.uvm_obj->pgops->pgo_detach) 2064 (first_entry->object.uvm_obj); 2065 } 2066 next_entry = first_entry->next; 2067 uvm_mapent_free(first_entry); 2068 first_entry = next_entry; 2069 } 2070 UVMHIST_LOG(maphist, "<- done", 0,0,0,0); 2071 } 2072 2073 /* 2074 * E X T R A C T I O N F U N C T I O N S 2075 */ 2076 2077 /* 2078 * uvm_map_reserve: reserve space in a vm_map for future use. 2079 * 2080 * => we reserve space in a map by putting a dummy map entry in the 2081 * map (dummy means obj=NULL, amap=NULL, prot=VM_PROT_NONE) 2082 * => map should be unlocked (we will write lock it) 2083 * => we return true if we were able to reserve space 2084 * => XXXCDC: should be inline? 2085 */ 2086 2087 int 2088 uvm_map_reserve(struct vm_map *map, vsize_t size, 2089 vaddr_t offset /* hint for pmap_prefer */, 2090 vsize_t align /* alignment hint */, 2091 vaddr_t *raddr /* IN:hint, OUT: reserved VA */, 2092 uvm_flag_t flags /* UVM_FLAG_FIXED or 0 */) 2093 { 2094 UVMHIST_FUNC("uvm_map_reserve"); UVMHIST_CALLED(maphist); 2095 2096 UVMHIST_LOG(maphist, "(map=0x%x, size=0x%x, offset=0x%x,addr=0x%x)", 2097 map,size,offset,raddr); 2098 2099 size = round_page(size); 2100 2101 /* 2102 * reserve some virtual space. 2103 */ 2104 2105 if (uvm_map(map, raddr, size, NULL, offset, 0, 2106 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE, 2107 UVM_ADV_RANDOM, UVM_FLAG_NOMERGE|flags)) != 0) { 2108 UVMHIST_LOG(maphist, "<- done (no VM)", 0,0,0,0); 2109 return (FALSE); 2110 } 2111 2112 UVMHIST_LOG(maphist, "<- done (*raddr=0x%x)", *raddr,0,0,0); 2113 return (TRUE); 2114 } 2115 2116 /* 2117 * uvm_map_replace: replace a reserved (blank) area of memory with 2118 * real mappings. 2119 * 2120 * => caller must WRITE-LOCK the map 2121 * => we return TRUE if replacement was a success 2122 * => we expect the newents chain to have nnewents entrys on it and 2123 * we expect newents->prev to point to the last entry on the list 2124 * => note newents is allowed to be NULL 2125 */ 2126 2127 int 2128 uvm_map_replace(struct vm_map *map, vaddr_t start, vaddr_t end, 2129 struct vm_map_entry *newents, int nnewents) 2130 { 2131 struct vm_map_entry *oldent, *last; 2132 2133 uvm_tree_sanity(map, "map_replace entry"); 2134 2135 /* 2136 * first find the blank map entry at the specified address 2137 */ 2138 2139 if (!uvm_map_lookup_entry(map, start, &oldent)) { 2140 return (FALSE); 2141 } 2142 2143 /* 2144 * check to make sure we have a proper blank entry 2145 */ 2146 2147 if (end < oldent->end && !VM_MAP_USE_KMAPENT(map)) { 2148 UVM_MAP_CLIP_END(map, oldent, end, NULL); 2149 } 2150 if (oldent->start != start || oldent->end != end || 2151 oldent->object.uvm_obj != NULL || oldent->aref.ar_amap != NULL) { 2152 return (FALSE); 2153 } 2154 2155 #ifdef DIAGNOSTIC 2156 2157 /* 2158 * sanity check the newents chain 2159 */ 2160 2161 { 2162 struct vm_map_entry *tmpent = newents; 2163 int nent = 0; 2164 vaddr_t cur = start; 2165 2166 while (tmpent) { 2167 nent++; 2168 if (tmpent->start < cur) 2169 panic("uvm_map_replace1"); 2170 if (tmpent->start > tmpent->end || tmpent->end > end) { 2171 printf("tmpent->start=0x%lx, tmpent->end=0x%lx, end=0x%lx\n", 2172 tmpent->start, tmpent->end, end); 2173 panic("uvm_map_replace2"); 2174 } 2175 cur = tmpent->end; 2176 if (tmpent->next) { 2177 if (tmpent->next->prev != tmpent) 2178 panic("uvm_map_replace3"); 2179 } else { 2180 if (newents->prev != tmpent) 2181 panic("uvm_map_replace4"); 2182 } 2183 tmpent = tmpent->next; 2184 } 2185 if (nent != nnewents) 2186 panic("uvm_map_replace5"); 2187 } 2188 #endif 2189 2190 /* 2191 * map entry is a valid blank! replace it. (this does all the 2192 * work of map entry link/unlink...). 2193 */ 2194 2195 if (newents) { 2196 last = newents->prev; 2197 2198 /* critical: flush stale hints out of map */ 2199 SAVE_HINT(map, map->hint, newents); 2200 if (map->first_free == oldent) 2201 map->first_free = last; 2202 2203 last->next = oldent->next; 2204 last->next->prev = last; 2205 2206 /* Fix RB tree */ 2207 uvm_rb_remove(map, oldent); 2208 2209 newents->prev = oldent->prev; 2210 newents->prev->next = newents; 2211 map->nentries = map->nentries + (nnewents - 1); 2212 2213 /* Fixup the RB tree */ 2214 { 2215 int i; 2216 struct vm_map_entry *tmp; 2217 2218 tmp = newents; 2219 for (i = 0; i < nnewents && tmp; i++) { 2220 uvm_rb_insert(map, tmp); 2221 tmp = tmp->next; 2222 } 2223 } 2224 } else { 2225 2226 /* critical: flush stale hints out of map */ 2227 SAVE_HINT(map, map->hint, oldent->prev); 2228 if (map->first_free == oldent) 2229 map->first_free = oldent->prev; 2230 2231 /* NULL list of new entries: just remove the old one */ 2232 uvm_map_entry_unlink(map, oldent); 2233 } 2234 2235 uvm_tree_sanity(map, "map_replace leave"); 2236 2237 /* 2238 * now we can free the old blank entry and return. 2239 */ 2240 2241 uvm_mapent_free(oldent); 2242 return (TRUE); 2243 } 2244 2245 /* 2246 * uvm_map_extract: extract a mapping from a map and put it somewhere 2247 * (maybe removing the old mapping) 2248 * 2249 * => maps should be unlocked (we will write lock them) 2250 * => returns 0 on success, error code otherwise 2251 * => start must be page aligned 2252 * => len must be page sized 2253 * => flags: 2254 * UVM_EXTRACT_REMOVE: remove mappings from srcmap 2255 * UVM_EXTRACT_CONTIG: abort if unmapped area (advisory only) 2256 * UVM_EXTRACT_QREF: for a temporary extraction do quick obj refs 2257 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go 2258 * >>>NOTE: if you set REMOVE, you are not allowed to use CONTIG or QREF!<<< 2259 * >>>NOTE: QREF's must be unmapped via the QREF path, thus should only 2260 * be used from within the kernel in a kernel level map <<< 2261 */ 2262 2263 int 2264 uvm_map_extract(struct vm_map *srcmap, vaddr_t start, vsize_t len, 2265 struct vm_map *dstmap, vaddr_t *dstaddrp, int flags) 2266 { 2267 vaddr_t dstaddr, end, newend, oldoffset, fudge, orig_fudge; 2268 struct vm_map_entry *chain, *endchain, *entry, *orig_entry, *newentry, 2269 *deadentry, *oldentry; 2270 vsize_t elen; 2271 int nchain, error, copy_ok; 2272 UVMHIST_FUNC("uvm_map_extract"); UVMHIST_CALLED(maphist); 2273 2274 UVMHIST_LOG(maphist,"(srcmap=0x%x,start=0x%x, len=0x%x", srcmap, start, 2275 len,0); 2276 UVMHIST_LOG(maphist," ...,dstmap=0x%x, flags=0x%x)", dstmap,flags,0,0); 2277 2278 uvm_tree_sanity(srcmap, "map_extract src enter"); 2279 uvm_tree_sanity(dstmap, "map_extract dst enter"); 2280 2281 /* 2282 * step 0: sanity check: start must be on a page boundary, length 2283 * must be page sized. can't ask for CONTIG/QREF if you asked for 2284 * REMOVE. 2285 */ 2286 2287 KASSERT((start & PAGE_MASK) == 0 && (len & PAGE_MASK) == 0); 2288 KASSERT((flags & UVM_EXTRACT_REMOVE) == 0 || 2289 (flags & (UVM_EXTRACT_CONTIG|UVM_EXTRACT_QREF)) == 0); 2290 2291 /* 2292 * step 1: reserve space in the target map for the extracted area 2293 */ 2294 2295 if ((flags & UVM_EXTRACT_RESERVED) == 0) { 2296 dstaddr = vm_map_min(dstmap); 2297 if (!uvm_map_reserve(dstmap, len, start, 0, &dstaddr, 0)) 2298 return (ENOMEM); 2299 *dstaddrp = dstaddr; /* pass address back to caller */ 2300 UVMHIST_LOG(maphist, " dstaddr=0x%x", dstaddr,0,0,0); 2301 } else { 2302 dstaddr = *dstaddrp; 2303 } 2304 2305 /* 2306 * step 2: setup for the extraction process loop by init'ing the 2307 * map entry chain, locking src map, and looking up the first useful 2308 * entry in the map. 2309 */ 2310 2311 end = start + len; 2312 newend = dstaddr + len; 2313 chain = endchain = NULL; 2314 nchain = 0; 2315 vm_map_lock(srcmap); 2316 2317 if (uvm_map_lookup_entry(srcmap, start, &entry)) { 2318 2319 /* "start" is within an entry */ 2320 if (flags & UVM_EXTRACT_QREF) { 2321 2322 /* 2323 * for quick references we don't clip the entry, so 2324 * the entry may map space "before" the starting 2325 * virtual address... this is the "fudge" factor 2326 * (which can be non-zero only the first time 2327 * through the "while" loop in step 3). 2328 */ 2329 2330 fudge = start - entry->start; 2331 } else { 2332 2333 /* 2334 * normal reference: we clip the map to fit (thus 2335 * fudge is zero) 2336 */ 2337 2338 UVM_MAP_CLIP_START(srcmap, entry, start, NULL); 2339 SAVE_HINT(srcmap, srcmap->hint, entry->prev); 2340 fudge = 0; 2341 } 2342 } else { 2343 2344 /* "start" is not within an entry ... skip to next entry */ 2345 if (flags & UVM_EXTRACT_CONTIG) { 2346 error = EINVAL; 2347 goto bad; /* definite hole here ... */ 2348 } 2349 2350 entry = entry->next; 2351 fudge = 0; 2352 } 2353 2354 /* save values from srcmap for step 6 */ 2355 orig_entry = entry; 2356 orig_fudge = fudge; 2357 2358 /* 2359 * step 3: now start looping through the map entries, extracting 2360 * as we go. 2361 */ 2362 2363 while (entry->start < end && entry != &srcmap->header) { 2364 2365 /* if we are not doing a quick reference, clip it */ 2366 if ((flags & UVM_EXTRACT_QREF) == 0) 2367 UVM_MAP_CLIP_END(srcmap, entry, end, NULL); 2368 2369 /* clear needs_copy (allow chunking) */ 2370 if (UVM_ET_ISNEEDSCOPY(entry)) { 2371 amap_copy(srcmap, entry, M_NOWAIT, TRUE, start, end); 2372 if (UVM_ET_ISNEEDSCOPY(entry)) { /* failed? */ 2373 error = ENOMEM; 2374 goto bad; 2375 } 2376 2377 /* amap_copy could clip (during chunk)! update fudge */ 2378 if (fudge) { 2379 fudge = start - entry->start; 2380 orig_fudge = fudge; 2381 } 2382 } 2383 2384 /* calculate the offset of this from "start" */ 2385 oldoffset = (entry->start + fudge) - start; 2386 2387 /* allocate a new map entry */ 2388 newentry = uvm_mapent_alloc(dstmap, 0); 2389 if (newentry == NULL) { 2390 error = ENOMEM; 2391 goto bad; 2392 } 2393 2394 /* set up new map entry */ 2395 newentry->next = NULL; 2396 newentry->prev = endchain; 2397 newentry->start = dstaddr + oldoffset; 2398 newentry->end = 2399 newentry->start + (entry->end - (entry->start + fudge)); 2400 if (newentry->end > newend || newentry->end < newentry->start) 2401 newentry->end = newend; 2402 newentry->object.uvm_obj = entry->object.uvm_obj; 2403 if (newentry->object.uvm_obj) { 2404 if (newentry->object.uvm_obj->pgops->pgo_reference) 2405 newentry->object.uvm_obj->pgops-> 2406 pgo_reference(newentry->object.uvm_obj); 2407 newentry->offset = entry->offset + fudge; 2408 } else { 2409 newentry->offset = 0; 2410 } 2411 newentry->etype = entry->etype; 2412 newentry->protection = (flags & UVM_EXTRACT_FIXPROT) ? 2413 entry->max_protection : entry->protection; 2414 newentry->max_protection = entry->max_protection; 2415 newentry->inheritance = entry->inheritance; 2416 newentry->wired_count = 0; 2417 newentry->aref.ar_amap = entry->aref.ar_amap; 2418 if (newentry->aref.ar_amap) { 2419 newentry->aref.ar_pageoff = 2420 entry->aref.ar_pageoff + (fudge >> PAGE_SHIFT); 2421 uvm_map_reference_amap(newentry, AMAP_SHARED | 2422 ((flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0)); 2423 } else { 2424 newentry->aref.ar_pageoff = 0; 2425 } 2426 newentry->advice = entry->advice; 2427 2428 /* now link it on the chain */ 2429 nchain++; 2430 if (endchain == NULL) { 2431 chain = endchain = newentry; 2432 } else { 2433 endchain->next = newentry; 2434 endchain = newentry; 2435 } 2436 2437 /* end of 'while' loop! */ 2438 if ((flags & UVM_EXTRACT_CONTIG) && entry->end < end && 2439 (entry->next == &srcmap->header || 2440 entry->next->start != entry->end)) { 2441 error = EINVAL; 2442 goto bad; 2443 } 2444 entry = entry->next; 2445 fudge = 0; 2446 } 2447 2448 /* 2449 * step 4: close off chain (in format expected by uvm_map_replace) 2450 */ 2451 2452 if (chain) 2453 chain->prev = endchain; 2454 2455 /* 2456 * step 5: attempt to lock the dest map so we can pmap_copy. 2457 * note usage of copy_ok: 2458 * 1 => dstmap locked, pmap_copy ok, and we "replace" here (step 5) 2459 * 0 => dstmap unlocked, NO pmap_copy, and we will "replace" in step 7 2460 */ 2461 2462 if (srcmap == dstmap || vm_map_lock_try(dstmap) == TRUE) { 2463 copy_ok = 1; 2464 if (!uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain, 2465 nchain)) { 2466 if (srcmap != dstmap) 2467 vm_map_unlock(dstmap); 2468 error = EIO; 2469 goto bad; 2470 } 2471 } else { 2472 copy_ok = 0; 2473 /* replace defered until step 7 */ 2474 } 2475 2476 /* 2477 * step 6: traverse the srcmap a second time to do the following: 2478 * - if we got a lock on the dstmap do pmap_copy 2479 * - if UVM_EXTRACT_REMOVE remove the entries 2480 * we make use of orig_entry and orig_fudge (saved in step 2) 2481 */ 2482 2483 if (copy_ok || (flags & UVM_EXTRACT_REMOVE)) { 2484 2485 /* purge possible stale hints from srcmap */ 2486 if (flags & UVM_EXTRACT_REMOVE) { 2487 SAVE_HINT(srcmap, srcmap->hint, orig_entry->prev); 2488 if (srcmap->first_free->start >= start) 2489 srcmap->first_free = orig_entry->prev; 2490 } 2491 2492 entry = orig_entry; 2493 fudge = orig_fudge; 2494 deadentry = NULL; /* for UVM_EXTRACT_REMOVE */ 2495 2496 while (entry->start < end && entry != &srcmap->header) { 2497 if (copy_ok) { 2498 oldoffset = (entry->start + fudge) - start; 2499 elen = MIN(end, entry->end) - 2500 (entry->start + fudge); 2501 pmap_copy(dstmap->pmap, srcmap->pmap, 2502 dstaddr + oldoffset, elen, 2503 entry->start + fudge); 2504 } 2505 2506 /* we advance "entry" in the following if statement */ 2507 if (flags & UVM_EXTRACT_REMOVE) { 2508 pmap_remove(srcmap->pmap, entry->start, 2509 entry->end); 2510 oldentry = entry; /* save entry */ 2511 entry = entry->next; /* advance */ 2512 uvm_map_entry_unlink(srcmap, oldentry); 2513 /* add to dead list */ 2514 oldentry->next = deadentry; 2515 deadentry = oldentry; 2516 } else { 2517 entry = entry->next; /* advance */ 2518 } 2519 2520 /* end of 'while' loop */ 2521 fudge = 0; 2522 } 2523 pmap_update(srcmap->pmap); 2524 2525 /* 2526 * unlock dstmap. we will dispose of deadentry in 2527 * step 7 if needed 2528 */ 2529 2530 if (copy_ok && srcmap != dstmap) 2531 vm_map_unlock(dstmap); 2532 2533 } else { 2534 deadentry = NULL; 2535 } 2536 2537 /* 2538 * step 7: we are done with the source map, unlock. if copy_ok 2539 * is 0 then we have not replaced the dummy mapping in dstmap yet 2540 * and we need to do so now. 2541 */ 2542 2543 vm_map_unlock(srcmap); 2544 if ((flags & UVM_EXTRACT_REMOVE) && deadentry) 2545 uvm_unmap_detach(deadentry, 0); /* dispose of old entries */ 2546 2547 /* now do the replacement if we didn't do it in step 5 */ 2548 if (copy_ok == 0) { 2549 vm_map_lock(dstmap); 2550 error = uvm_map_replace(dstmap, dstaddr, dstaddr+len, chain, 2551 nchain); 2552 vm_map_unlock(dstmap); 2553 2554 if (error == FALSE) { 2555 error = EIO; 2556 goto bad2; 2557 } 2558 } 2559 2560 uvm_tree_sanity(srcmap, "map_extract src leave"); 2561 uvm_tree_sanity(dstmap, "map_extract dst leave"); 2562 2563 return (0); 2564 2565 /* 2566 * bad: failure recovery 2567 */ 2568 bad: 2569 vm_map_unlock(srcmap); 2570 bad2: /* src already unlocked */ 2571 if (chain) 2572 uvm_unmap_detach(chain, 2573 (flags & UVM_EXTRACT_QREF) ? AMAP_REFALL : 0); 2574 2575 uvm_tree_sanity(srcmap, "map_extract src err leave"); 2576 uvm_tree_sanity(dstmap, "map_extract dst err leave"); 2577 2578 if ((flags & UVM_EXTRACT_RESERVED) == 0) { 2579 uvm_unmap(dstmap, dstaddr, dstaddr+len); /* ??? */ 2580 } 2581 return (error); 2582 } 2583 2584 /* end of extraction functions */ 2585 2586 /* 2587 * uvm_map_submap: punch down part of a map into a submap 2588 * 2589 * => only the kernel_map is allowed to be submapped 2590 * => the purpose of submapping is to break up the locking granularity 2591 * of a larger map 2592 * => the range specified must have been mapped previously with a uvm_map() 2593 * call [with uobj==NULL] to create a blank map entry in the main map. 2594 * [And it had better still be blank!] 2595 * => maps which contain submaps should never be copied or forked. 2596 * => to remove a submap, use uvm_unmap() on the main map 2597 * and then uvm_map_deallocate() the submap. 2598 * => main map must be unlocked. 2599 * => submap must have been init'd and have a zero reference count. 2600 * [need not be locked as we don't actually reference it] 2601 */ 2602 2603 int 2604 uvm_map_submap(struct vm_map *map, vaddr_t start, vaddr_t end, 2605 struct vm_map *submap) 2606 { 2607 struct vm_map_entry *entry; 2608 struct uvm_mapent_reservation umr; 2609 int error; 2610 2611 uvm_mapent_reserve(map, &umr, 2, 0); 2612 2613 vm_map_lock(map); 2614 VM_MAP_RANGE_CHECK(map, start, end); 2615 2616 if (uvm_map_lookup_entry(map, start, &entry)) { 2617 UVM_MAP_CLIP_START(map, entry, start, &umr); 2618 UVM_MAP_CLIP_END(map, entry, end, &umr); /* to be safe */ 2619 } else { 2620 entry = NULL; 2621 } 2622 2623 if (entry != NULL && 2624 entry->start == start && entry->end == end && 2625 entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL && 2626 !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) { 2627 entry->etype |= UVM_ET_SUBMAP; 2628 entry->object.sub_map = submap; 2629 entry->offset = 0; 2630 uvm_map_reference(submap); 2631 error = 0; 2632 } else { 2633 error = EINVAL; 2634 } 2635 vm_map_unlock(map); 2636 2637 uvm_mapent_unreserve(map, &umr); 2638 2639 return error; 2640 } 2641 2642 /* 2643 * uvm_map_setup_kernel: init in-kernel map 2644 * 2645 * => map must not be in service yet. 2646 */ 2647 2648 void 2649 uvm_map_setup_kernel(struct vm_map_kernel *map, 2650 vaddr_t vmin, vaddr_t vmax, int flags) 2651 { 2652 2653 uvm_map_setup(&map->vmk_map, vmin, vmax, flags); 2654 2655 LIST_INIT(&map->vmk_kentry_free); 2656 map->vmk_merged_entries = NULL; 2657 } 2658 2659 2660 /* 2661 * uvm_map_protect: change map protection 2662 * 2663 * => set_max means set max_protection. 2664 * => map must be unlocked. 2665 */ 2666 2667 #define MASK(entry) (UVM_ET_ISCOPYONWRITE(entry) ? \ 2668 ~VM_PROT_WRITE : VM_PROT_ALL) 2669 2670 int 2671 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end, 2672 vm_prot_t new_prot, boolean_t set_max) 2673 { 2674 struct vm_map_entry *current, *entry; 2675 int error = 0; 2676 UVMHIST_FUNC("uvm_map_protect"); UVMHIST_CALLED(maphist); 2677 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_prot=0x%x)", 2678 map, start, end, new_prot); 2679 2680 vm_map_lock(map); 2681 VM_MAP_RANGE_CHECK(map, start, end); 2682 if (uvm_map_lookup_entry(map, start, &entry)) { 2683 UVM_MAP_CLIP_START(map, entry, start, NULL); 2684 } else { 2685 entry = entry->next; 2686 } 2687 2688 /* 2689 * make a first pass to check for protection violations. 2690 */ 2691 2692 current = entry; 2693 while ((current != &map->header) && (current->start < end)) { 2694 if (UVM_ET_ISSUBMAP(current)) { 2695 error = EINVAL; 2696 goto out; 2697 } 2698 if ((new_prot & current->max_protection) != new_prot) { 2699 error = EACCES; 2700 goto out; 2701 } 2702 /* 2703 * Don't allow VM_PROT_EXECUTE to be set on entries that 2704 * point to vnodes that are associated with a NOEXEC file 2705 * system. 2706 */ 2707 if (UVM_ET_ISOBJ(current) && 2708 UVM_OBJ_IS_VNODE(current->object.uvm_obj)) { 2709 struct vnode *vp = 2710 (struct vnode *) current->object.uvm_obj; 2711 2712 if ((new_prot & VM_PROT_EXECUTE) != 0 && 2713 (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) { 2714 error = EACCES; 2715 goto out; 2716 } 2717 } 2718 current = current->next; 2719 } 2720 2721 /* go back and fix up protections (no need to clip this time). */ 2722 2723 current = entry; 2724 while ((current != &map->header) && (current->start < end)) { 2725 vm_prot_t old_prot; 2726 2727 UVM_MAP_CLIP_END(map, current, end, NULL); 2728 old_prot = current->protection; 2729 if (set_max) 2730 current->protection = 2731 (current->max_protection = new_prot) & old_prot; 2732 else 2733 current->protection = new_prot; 2734 2735 /* 2736 * update physical map if necessary. worry about copy-on-write 2737 * here -- CHECK THIS XXX 2738 */ 2739 2740 if (current->protection != old_prot) { 2741 /* update pmap! */ 2742 pmap_protect(map->pmap, current->start, current->end, 2743 current->protection & MASK(entry)); 2744 2745 /* 2746 * If this entry points at a vnode, and the 2747 * protection includes VM_PROT_EXECUTE, mark 2748 * the vnode as VEXECMAP. 2749 */ 2750 if (UVM_ET_ISOBJ(current)) { 2751 struct uvm_object *uobj = 2752 current->object.uvm_obj; 2753 2754 if (UVM_OBJ_IS_VNODE(uobj) && 2755 (current->protection & VM_PROT_EXECUTE)) 2756 vn_markexec((struct vnode *) uobj); 2757 } 2758 } 2759 2760 /* 2761 * If the map is configured to lock any future mappings, 2762 * wire this entry now if the old protection was VM_PROT_NONE 2763 * and the new protection is not VM_PROT_NONE. 2764 */ 2765 2766 if ((map->flags & VM_MAP_WIREFUTURE) != 0 && 2767 VM_MAPENT_ISWIRED(entry) == 0 && 2768 old_prot == VM_PROT_NONE && 2769 new_prot != VM_PROT_NONE) { 2770 if (uvm_map_pageable(map, entry->start, 2771 entry->end, FALSE, 2772 UVM_LK_ENTER|UVM_LK_EXIT) != 0) { 2773 2774 /* 2775 * If locking the entry fails, remember the 2776 * error if it's the first one. Note we 2777 * still continue setting the protection in 2778 * the map, but will return the error 2779 * condition regardless. 2780 * 2781 * XXX Ignore what the actual error is, 2782 * XXX just call it a resource shortage 2783 * XXX so that it doesn't get confused 2784 * XXX what uvm_map_protect() itself would 2785 * XXX normally return. 2786 */ 2787 2788 error = ENOMEM; 2789 } 2790 } 2791 current = current->next; 2792 } 2793 pmap_update(map->pmap); 2794 2795 out: 2796 vm_map_unlock(map); 2797 2798 UVMHIST_LOG(maphist, "<- done, error=%d",error,0,0,0); 2799 return error; 2800 } 2801 2802 #undef MASK 2803 2804 /* 2805 * uvm_map_inherit: set inheritance code for range of addrs in map. 2806 * 2807 * => map must be unlocked 2808 * => note that the inherit code is used during a "fork". see fork 2809 * code for details. 2810 */ 2811 2812 int 2813 uvm_map_inherit(struct vm_map *map, vaddr_t start, vaddr_t end, 2814 vm_inherit_t new_inheritance) 2815 { 2816 struct vm_map_entry *entry, *temp_entry; 2817 UVMHIST_FUNC("uvm_map_inherit"); UVMHIST_CALLED(maphist); 2818 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_inh=0x%x)", 2819 map, start, end, new_inheritance); 2820 2821 switch (new_inheritance) { 2822 case MAP_INHERIT_NONE: 2823 case MAP_INHERIT_COPY: 2824 case MAP_INHERIT_SHARE: 2825 break; 2826 default: 2827 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0); 2828 return EINVAL; 2829 } 2830 2831 vm_map_lock(map); 2832 VM_MAP_RANGE_CHECK(map, start, end); 2833 if (uvm_map_lookup_entry(map, start, &temp_entry)) { 2834 entry = temp_entry; 2835 UVM_MAP_CLIP_START(map, entry, start, NULL); 2836 } else { 2837 entry = temp_entry->next; 2838 } 2839 while ((entry != &map->header) && (entry->start < end)) { 2840 UVM_MAP_CLIP_END(map, entry, end, NULL); 2841 entry->inheritance = new_inheritance; 2842 entry = entry->next; 2843 } 2844 vm_map_unlock(map); 2845 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0); 2846 return 0; 2847 } 2848 2849 /* 2850 * uvm_map_advice: set advice code for range of addrs in map. 2851 * 2852 * => map must be unlocked 2853 */ 2854 2855 int 2856 uvm_map_advice(struct vm_map *map, vaddr_t start, vaddr_t end, int new_advice) 2857 { 2858 struct vm_map_entry *entry, *temp_entry; 2859 UVMHIST_FUNC("uvm_map_advice"); UVMHIST_CALLED(maphist); 2860 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_adv=0x%x)", 2861 map, start, end, new_advice); 2862 2863 vm_map_lock(map); 2864 VM_MAP_RANGE_CHECK(map, start, end); 2865 if (uvm_map_lookup_entry(map, start, &temp_entry)) { 2866 entry = temp_entry; 2867 UVM_MAP_CLIP_START(map, entry, start, NULL); 2868 } else { 2869 entry = temp_entry->next; 2870 } 2871 2872 /* 2873 * XXXJRT: disallow holes? 2874 */ 2875 2876 while ((entry != &map->header) && (entry->start < end)) { 2877 UVM_MAP_CLIP_END(map, entry, end, NULL); 2878 2879 switch (new_advice) { 2880 case MADV_NORMAL: 2881 case MADV_RANDOM: 2882 case MADV_SEQUENTIAL: 2883 /* nothing special here */ 2884 break; 2885 2886 default: 2887 vm_map_unlock(map); 2888 UVMHIST_LOG(maphist,"<- done (INVALID ARG)",0,0,0,0); 2889 return EINVAL; 2890 } 2891 entry->advice = new_advice; 2892 entry = entry->next; 2893 } 2894 2895 vm_map_unlock(map); 2896 UVMHIST_LOG(maphist,"<- done (OK)",0,0,0,0); 2897 return 0; 2898 } 2899 2900 /* 2901 * uvm_map_pageable: sets the pageability of a range in a map. 2902 * 2903 * => wires map entries. should not be used for transient page locking. 2904 * for that, use uvm_fault_wire()/uvm_fault_unwire() (see uvm_vslock()). 2905 * => regions sepcified as not pageable require lock-down (wired) memory 2906 * and page tables. 2907 * => map must never be read-locked 2908 * => if islocked is TRUE, map is already write-locked 2909 * => we always unlock the map, since we must downgrade to a read-lock 2910 * to call uvm_fault_wire() 2911 * => XXXCDC: check this and try and clean it up. 2912 */ 2913 2914 int 2915 uvm_map_pageable(struct vm_map *map, vaddr_t start, vaddr_t end, 2916 boolean_t new_pageable, int lockflags) 2917 { 2918 struct vm_map_entry *entry, *start_entry, *failed_entry; 2919 int rv; 2920 #ifdef DIAGNOSTIC 2921 u_int timestamp_save; 2922 #endif 2923 UVMHIST_FUNC("uvm_map_pageable"); UVMHIST_CALLED(maphist); 2924 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,new_pageable=0x%x)", 2925 map, start, end, new_pageable); 2926 KASSERT(map->flags & VM_MAP_PAGEABLE); 2927 2928 if ((lockflags & UVM_LK_ENTER) == 0) 2929 vm_map_lock(map); 2930 VM_MAP_RANGE_CHECK(map, start, end); 2931 2932 /* 2933 * only one pageability change may take place at one time, since 2934 * uvm_fault_wire assumes it will be called only once for each 2935 * wiring/unwiring. therefore, we have to make sure we're actually 2936 * changing the pageability for the entire region. we do so before 2937 * making any changes. 2938 */ 2939 2940 if (uvm_map_lookup_entry(map, start, &start_entry) == FALSE) { 2941 if ((lockflags & UVM_LK_EXIT) == 0) 2942 vm_map_unlock(map); 2943 2944 UVMHIST_LOG(maphist,"<- done (fault)",0,0,0,0); 2945 return EFAULT; 2946 } 2947 entry = start_entry; 2948 2949 /* 2950 * handle wiring and unwiring separately. 2951 */ 2952 2953 if (new_pageable) { /* unwire */ 2954 UVM_MAP_CLIP_START(map, entry, start, NULL); 2955 2956 /* 2957 * unwiring. first ensure that the range to be unwired is 2958 * really wired down and that there are no holes. 2959 */ 2960 2961 while ((entry != &map->header) && (entry->start < end)) { 2962 if (entry->wired_count == 0 || 2963 (entry->end < end && 2964 (entry->next == &map->header || 2965 entry->next->start > entry->end))) { 2966 if ((lockflags & UVM_LK_EXIT) == 0) 2967 vm_map_unlock(map); 2968 UVMHIST_LOG(maphist, "<- done (INVAL)",0,0,0,0); 2969 return EINVAL; 2970 } 2971 entry = entry->next; 2972 } 2973 2974 /* 2975 * POSIX 1003.1b - a single munlock call unlocks a region, 2976 * regardless of the number of mlock calls made on that 2977 * region. 2978 */ 2979 2980 entry = start_entry; 2981 while ((entry != &map->header) && (entry->start < end)) { 2982 UVM_MAP_CLIP_END(map, entry, end, NULL); 2983 if (VM_MAPENT_ISWIRED(entry)) 2984 uvm_map_entry_unwire(map, entry); 2985 entry = entry->next; 2986 } 2987 if ((lockflags & UVM_LK_EXIT) == 0) 2988 vm_map_unlock(map); 2989 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0); 2990 return 0; 2991 } 2992 2993 /* 2994 * wire case: in two passes [XXXCDC: ugly block of code here] 2995 * 2996 * 1: holding the write lock, we create any anonymous maps that need 2997 * to be created. then we clip each map entry to the region to 2998 * be wired and increment its wiring count. 2999 * 3000 * 2: we downgrade to a read lock, and call uvm_fault_wire to fault 3001 * in the pages for any newly wired area (wired_count == 1). 3002 * 3003 * downgrading to a read lock for uvm_fault_wire avoids a possible 3004 * deadlock with another thread that may have faulted on one of 3005 * the pages to be wired (it would mark the page busy, blocking 3006 * us, then in turn block on the map lock that we hold). because 3007 * of problems in the recursive lock package, we cannot upgrade 3008 * to a write lock in vm_map_lookup. thus, any actions that 3009 * require the write lock must be done beforehand. because we 3010 * keep the read lock on the map, the copy-on-write status of the 3011 * entries we modify here cannot change. 3012 */ 3013 3014 while ((entry != &map->header) && (entry->start < end)) { 3015 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */ 3016 3017 /* 3018 * perform actions of vm_map_lookup that need the 3019 * write lock on the map: create an anonymous map 3020 * for a copy-on-write region, or an anonymous map 3021 * for a zero-fill region. (XXXCDC: submap case 3022 * ok?) 3023 */ 3024 3025 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */ 3026 if (UVM_ET_ISNEEDSCOPY(entry) && 3027 ((entry->max_protection & VM_PROT_WRITE) || 3028 (entry->object.uvm_obj == NULL))) { 3029 amap_copy(map, entry, M_WAITOK, TRUE, 3030 start, end); 3031 /* XXXCDC: wait OK? */ 3032 } 3033 } 3034 } 3035 UVM_MAP_CLIP_START(map, entry, start, NULL); 3036 UVM_MAP_CLIP_END(map, entry, end, NULL); 3037 entry->wired_count++; 3038 3039 /* 3040 * Check for holes 3041 */ 3042 3043 if (entry->protection == VM_PROT_NONE || 3044 (entry->end < end && 3045 (entry->next == &map->header || 3046 entry->next->start > entry->end))) { 3047 3048 /* 3049 * found one. amap creation actions do not need to 3050 * be undone, but the wired counts need to be restored. 3051 */ 3052 3053 while (entry != &map->header && entry->end > start) { 3054 entry->wired_count--; 3055 entry = entry->prev; 3056 } 3057 if ((lockflags & UVM_LK_EXIT) == 0) 3058 vm_map_unlock(map); 3059 UVMHIST_LOG(maphist,"<- done (INVALID WIRE)",0,0,0,0); 3060 return EINVAL; 3061 } 3062 entry = entry->next; 3063 } 3064 3065 /* 3066 * Pass 2. 3067 */ 3068 3069 #ifdef DIAGNOSTIC 3070 timestamp_save = map->timestamp; 3071 #endif 3072 vm_map_busy(map); 3073 vm_map_downgrade(map); 3074 3075 rv = 0; 3076 entry = start_entry; 3077 while (entry != &map->header && entry->start < end) { 3078 if (entry->wired_count == 1) { 3079 rv = uvm_fault_wire(map, entry->start, entry->end, 3080 VM_FAULT_WIREMAX, entry->max_protection); 3081 if (rv) { 3082 3083 /* 3084 * wiring failed. break out of the loop. 3085 * we'll clean up the map below, once we 3086 * have a write lock again. 3087 */ 3088 3089 break; 3090 } 3091 } 3092 entry = entry->next; 3093 } 3094 3095 if (rv) { /* failed? */ 3096 3097 /* 3098 * Get back to an exclusive (write) lock. 3099 */ 3100 3101 vm_map_upgrade(map); 3102 vm_map_unbusy(map); 3103 3104 #ifdef DIAGNOSTIC 3105 if (timestamp_save != map->timestamp) 3106 panic("uvm_map_pageable: stale map"); 3107 #endif 3108 3109 /* 3110 * first drop the wiring count on all the entries 3111 * which haven't actually been wired yet. 3112 */ 3113 3114 failed_entry = entry; 3115 while (entry != &map->header && entry->start < end) { 3116 entry->wired_count--; 3117 entry = entry->next; 3118 } 3119 3120 /* 3121 * now, unwire all the entries that were successfully 3122 * wired above. 3123 */ 3124 3125 entry = start_entry; 3126 while (entry != failed_entry) { 3127 entry->wired_count--; 3128 if (VM_MAPENT_ISWIRED(entry) == 0) 3129 uvm_map_entry_unwire(map, entry); 3130 entry = entry->next; 3131 } 3132 if ((lockflags & UVM_LK_EXIT) == 0) 3133 vm_map_unlock(map); 3134 UVMHIST_LOG(maphist, "<- done (RV=%d)", rv,0,0,0); 3135 return (rv); 3136 } 3137 3138 /* We are holding a read lock here. */ 3139 if ((lockflags & UVM_LK_EXIT) == 0) { 3140 vm_map_unbusy(map); 3141 vm_map_unlock_read(map); 3142 } else { 3143 3144 /* 3145 * Get back to an exclusive (write) lock. 3146 */ 3147 3148 vm_map_upgrade(map); 3149 vm_map_unbusy(map); 3150 } 3151 3152 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0); 3153 return 0; 3154 } 3155 3156 /* 3157 * uvm_map_pageable_all: special case of uvm_map_pageable - affects 3158 * all mapped regions. 3159 * 3160 * => map must not be locked. 3161 * => if no flags are specified, all regions are unwired. 3162 * => XXXJRT: has some of the same problems as uvm_map_pageable() above. 3163 */ 3164 3165 int 3166 uvm_map_pageable_all(struct vm_map *map, int flags, vsize_t limit) 3167 { 3168 struct vm_map_entry *entry, *failed_entry; 3169 vsize_t size; 3170 int rv; 3171 #ifdef DIAGNOSTIC 3172 u_int timestamp_save; 3173 #endif 3174 UVMHIST_FUNC("uvm_map_pageable_all"); UVMHIST_CALLED(maphist); 3175 UVMHIST_LOG(maphist,"(map=0x%x,flags=0x%x)", map, flags, 0, 0); 3176 3177 KASSERT(map->flags & VM_MAP_PAGEABLE); 3178 3179 vm_map_lock(map); 3180 3181 /* 3182 * handle wiring and unwiring separately. 3183 */ 3184 3185 if (flags == 0) { /* unwire */ 3186 3187 /* 3188 * POSIX 1003.1b -- munlockall unlocks all regions, 3189 * regardless of how many times mlockall has been called. 3190 */ 3191 3192 for (entry = map->header.next; entry != &map->header; 3193 entry = entry->next) { 3194 if (VM_MAPENT_ISWIRED(entry)) 3195 uvm_map_entry_unwire(map, entry); 3196 } 3197 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE); 3198 vm_map_unlock(map); 3199 UVMHIST_LOG(maphist,"<- done (OK UNWIRE)",0,0,0,0); 3200 return 0; 3201 } 3202 3203 if (flags & MCL_FUTURE) { 3204 3205 /* 3206 * must wire all future mappings; remember this. 3207 */ 3208 3209 vm_map_modflags(map, VM_MAP_WIREFUTURE, 0); 3210 } 3211 3212 if ((flags & MCL_CURRENT) == 0) { 3213 3214 /* 3215 * no more work to do! 3216 */ 3217 3218 UVMHIST_LOG(maphist,"<- done (OK no wire)",0,0,0,0); 3219 vm_map_unlock(map); 3220 return 0; 3221 } 3222 3223 /* 3224 * wire case: in three passes [XXXCDC: ugly block of code here] 3225 * 3226 * 1: holding the write lock, count all pages mapped by non-wired 3227 * entries. if this would cause us to go over our limit, we fail. 3228 * 3229 * 2: still holding the write lock, we create any anonymous maps that 3230 * need to be created. then we increment its wiring count. 3231 * 3232 * 3: we downgrade to a read lock, and call uvm_fault_wire to fault 3233 * in the pages for any newly wired area (wired_count == 1). 3234 * 3235 * downgrading to a read lock for uvm_fault_wire avoids a possible 3236 * deadlock with another thread that may have faulted on one of 3237 * the pages to be wired (it would mark the page busy, blocking 3238 * us, then in turn block on the map lock that we hold). because 3239 * of problems in the recursive lock package, we cannot upgrade 3240 * to a write lock in vm_map_lookup. thus, any actions that 3241 * require the write lock must be done beforehand. because we 3242 * keep the read lock on the map, the copy-on-write status of the 3243 * entries we modify here cannot change. 3244 */ 3245 3246 for (size = 0, entry = map->header.next; entry != &map->header; 3247 entry = entry->next) { 3248 if (entry->protection != VM_PROT_NONE && 3249 VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */ 3250 size += entry->end - entry->start; 3251 } 3252 } 3253 3254 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) { 3255 vm_map_unlock(map); 3256 return ENOMEM; 3257 } 3258 3259 if (limit != 0 && 3260 (size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit)) { 3261 vm_map_unlock(map); 3262 return ENOMEM; 3263 } 3264 3265 /* 3266 * Pass 2. 3267 */ 3268 3269 for (entry = map->header.next; entry != &map->header; 3270 entry = entry->next) { 3271 if (entry->protection == VM_PROT_NONE) 3272 continue; 3273 if (VM_MAPENT_ISWIRED(entry) == 0) { /* not already wired? */ 3274 3275 /* 3276 * perform actions of vm_map_lookup that need the 3277 * write lock on the map: create an anonymous map 3278 * for a copy-on-write region, or an anonymous map 3279 * for a zero-fill region. (XXXCDC: submap case 3280 * ok?) 3281 */ 3282 3283 if (!UVM_ET_ISSUBMAP(entry)) { /* not submap */ 3284 if (UVM_ET_ISNEEDSCOPY(entry) && 3285 ((entry->max_protection & VM_PROT_WRITE) || 3286 (entry->object.uvm_obj == NULL))) { 3287 amap_copy(map, entry, M_WAITOK, TRUE, 3288 entry->start, entry->end); 3289 /* XXXCDC: wait OK? */ 3290 } 3291 } 3292 } 3293 entry->wired_count++; 3294 } 3295 3296 /* 3297 * Pass 3. 3298 */ 3299 3300 #ifdef DIAGNOSTIC 3301 timestamp_save = map->timestamp; 3302 #endif 3303 vm_map_busy(map); 3304 vm_map_downgrade(map); 3305 3306 rv = 0; 3307 for (entry = map->header.next; entry != &map->header; 3308 entry = entry->next) { 3309 if (entry->wired_count == 1) { 3310 rv = uvm_fault_wire(map, entry->start, entry->end, 3311 VM_FAULT_WIREMAX, entry->max_protection); 3312 if (rv) { 3313 3314 /* 3315 * wiring failed. break out of the loop. 3316 * we'll clean up the map below, once we 3317 * have a write lock again. 3318 */ 3319 3320 break; 3321 } 3322 } 3323 } 3324 3325 if (rv) { 3326 3327 /* 3328 * Get back an exclusive (write) lock. 3329 */ 3330 3331 vm_map_upgrade(map); 3332 vm_map_unbusy(map); 3333 3334 #ifdef DIAGNOSTIC 3335 if (timestamp_save != map->timestamp) 3336 panic("uvm_map_pageable_all: stale map"); 3337 #endif 3338 3339 /* 3340 * first drop the wiring count on all the entries 3341 * which haven't actually been wired yet. 3342 * 3343 * Skip VM_PROT_NONE entries like we did above. 3344 */ 3345 3346 failed_entry = entry; 3347 for (/* nothing */; entry != &map->header; 3348 entry = entry->next) { 3349 if (entry->protection == VM_PROT_NONE) 3350 continue; 3351 entry->wired_count--; 3352 } 3353 3354 /* 3355 * now, unwire all the entries that were successfully 3356 * wired above. 3357 * 3358 * Skip VM_PROT_NONE entries like we did above. 3359 */ 3360 3361 for (entry = map->header.next; entry != failed_entry; 3362 entry = entry->next) { 3363 if (entry->protection == VM_PROT_NONE) 3364 continue; 3365 entry->wired_count--; 3366 if (VM_MAPENT_ISWIRED(entry)) 3367 uvm_map_entry_unwire(map, entry); 3368 } 3369 vm_map_unlock(map); 3370 UVMHIST_LOG(maphist,"<- done (RV=%d)", rv,0,0,0); 3371 return (rv); 3372 } 3373 3374 /* We are holding a read lock here. */ 3375 vm_map_unbusy(map); 3376 vm_map_unlock_read(map); 3377 3378 UVMHIST_LOG(maphist,"<- done (OK WIRE)",0,0,0,0); 3379 return 0; 3380 } 3381 3382 /* 3383 * uvm_map_clean: clean out a map range 3384 * 3385 * => valid flags: 3386 * if (flags & PGO_CLEANIT): dirty pages are cleaned first 3387 * if (flags & PGO_SYNCIO): dirty pages are written synchronously 3388 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean 3389 * if (flags & PGO_FREE): any cached pages are freed after clean 3390 * => returns an error if any part of the specified range isn't mapped 3391 * => never a need to flush amap layer since the anonymous memory has 3392 * no permanent home, but may deactivate pages there 3393 * => called from sys_msync() and sys_madvise() 3394 * => caller must not write-lock map (read OK). 3395 * => we may sleep while cleaning if SYNCIO [with map read-locked] 3396 */ 3397 3398 int 3399 uvm_map_clean(struct vm_map *map, vaddr_t start, vaddr_t end, int flags) 3400 { 3401 struct vm_map_entry *current, *entry; 3402 struct uvm_object *uobj; 3403 struct vm_amap *amap; 3404 struct vm_anon *anon; 3405 struct vm_page *pg; 3406 vaddr_t offset; 3407 vsize_t size; 3408 voff_t uoff; 3409 int error, refs; 3410 UVMHIST_FUNC("uvm_map_clean"); UVMHIST_CALLED(maphist); 3411 3412 UVMHIST_LOG(maphist,"(map=0x%x,start=0x%x,end=0x%x,flags=0x%x)", 3413 map, start, end, flags); 3414 KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) != 3415 (PGO_FREE|PGO_DEACTIVATE)); 3416 3417 vm_map_lock_read(map); 3418 VM_MAP_RANGE_CHECK(map, start, end); 3419 if (uvm_map_lookup_entry(map, start, &entry) == FALSE) { 3420 vm_map_unlock_read(map); 3421 return EFAULT; 3422 } 3423 3424 /* 3425 * Make a first pass to check for holes and wiring problems. 3426 */ 3427 3428 for (current = entry; current->start < end; current = current->next) { 3429 if (UVM_ET_ISSUBMAP(current)) { 3430 vm_map_unlock_read(map); 3431 return EINVAL; 3432 } 3433 if ((flags & PGO_FREE) != 0 && VM_MAPENT_ISWIRED(entry)) { 3434 vm_map_unlock_read(map); 3435 return EBUSY; 3436 } 3437 if (end <= current->end) { 3438 break; 3439 } 3440 if (current->end != current->next->start) { 3441 vm_map_unlock_read(map); 3442 return EFAULT; 3443 } 3444 } 3445 3446 error = 0; 3447 for (current = entry; start < end; current = current->next) { 3448 amap = current->aref.ar_amap; /* top layer */ 3449 uobj = current->object.uvm_obj; /* bottom layer */ 3450 KASSERT(start >= current->start); 3451 3452 /* 3453 * No amap cleaning necessary if: 3454 * 3455 * (1) There's no amap. 3456 * 3457 * (2) We're not deactivating or freeing pages. 3458 */ 3459 3460 if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) 3461 goto flush_object; 3462 3463 amap_lock(amap); 3464 offset = start - current->start; 3465 size = MIN(end, current->end) - start; 3466 for ( ; size != 0; size -= PAGE_SIZE, offset += PAGE_SIZE) { 3467 anon = amap_lookup(¤t->aref, offset); 3468 if (anon == NULL) 3469 continue; 3470 3471 simple_lock(&anon->an_lock); 3472 pg = anon->an_page; 3473 if (pg == NULL) { 3474 simple_unlock(&anon->an_lock); 3475 continue; 3476 } 3477 3478 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) { 3479 3480 /* 3481 * In these first 3 cases, we just deactivate the page. 3482 */ 3483 3484 case PGO_CLEANIT|PGO_FREE: 3485 case PGO_CLEANIT|PGO_DEACTIVATE: 3486 case PGO_DEACTIVATE: 3487 deactivate_it: 3488 /* 3489 * skip the page if it's loaned or wired, 3490 * since it shouldn't be on a paging queue 3491 * at all in these cases. 3492 */ 3493 3494 uvm_lock_pageq(); 3495 if (pg->loan_count != 0 || 3496 pg->wire_count != 0) { 3497 uvm_unlock_pageq(); 3498 simple_unlock(&anon->an_lock); 3499 continue; 3500 } 3501 KASSERT(pg->uanon == anon); 3502 pmap_clear_reference(pg); 3503 uvm_pagedeactivate(pg); 3504 uvm_unlock_pageq(); 3505 simple_unlock(&anon->an_lock); 3506 continue; 3507 3508 case PGO_FREE: 3509 3510 /* 3511 * If there are multiple references to 3512 * the amap, just deactivate the page. 3513 */ 3514 3515 if (amap_refs(amap) > 1) 3516 goto deactivate_it; 3517 3518 /* skip the page if it's wired */ 3519 if (pg->wire_count != 0) { 3520 simple_unlock(&anon->an_lock); 3521 continue; 3522 } 3523 amap_unadd(¤t->aref, offset); 3524 refs = --anon->an_ref; 3525 simple_unlock(&anon->an_lock); 3526 if (refs == 0) 3527 uvm_anfree(anon); 3528 continue; 3529 } 3530 } 3531 amap_unlock(amap); 3532 3533 flush_object: 3534 /* 3535 * flush pages if we've got a valid backing object. 3536 * note that we must always clean object pages before 3537 * freeing them since otherwise we could reveal stale 3538 * data from files. 3539 */ 3540 3541 uoff = current->offset + (start - current->start); 3542 size = MIN(end, current->end) - start; 3543 if (uobj != NULL) { 3544 simple_lock(&uobj->vmobjlock); 3545 if (uobj->pgops->pgo_put != NULL) 3546 error = (uobj->pgops->pgo_put)(uobj, uoff, 3547 uoff + size, flags | PGO_CLEANIT); 3548 else 3549 error = 0; 3550 } 3551 start += size; 3552 } 3553 vm_map_unlock_read(map); 3554 return (error); 3555 } 3556 3557 3558 /* 3559 * uvm_map_checkprot: check protection in map 3560 * 3561 * => must allow specified protection in a fully allocated region. 3562 * => map must be read or write locked by caller. 3563 */ 3564 3565 boolean_t 3566 uvm_map_checkprot(struct vm_map *map, vaddr_t start, vaddr_t end, 3567 vm_prot_t protection) 3568 { 3569 struct vm_map_entry *entry; 3570 struct vm_map_entry *tmp_entry; 3571 3572 if (!uvm_map_lookup_entry(map, start, &tmp_entry)) { 3573 return (FALSE); 3574 } 3575 entry = tmp_entry; 3576 while (start < end) { 3577 if (entry == &map->header) { 3578 return (FALSE); 3579 } 3580 3581 /* 3582 * no holes allowed 3583 */ 3584 3585 if (start < entry->start) { 3586 return (FALSE); 3587 } 3588 3589 /* 3590 * check protection associated with entry 3591 */ 3592 3593 if ((entry->protection & protection) != protection) { 3594 return (FALSE); 3595 } 3596 start = entry->end; 3597 entry = entry->next; 3598 } 3599 return (TRUE); 3600 } 3601 3602 /* 3603 * uvmspace_alloc: allocate a vmspace structure. 3604 * 3605 * - structure includes vm_map and pmap 3606 * - XXX: no locking on this structure 3607 * - refcnt set to 1, rest must be init'd by caller 3608 */ 3609 struct vmspace * 3610 uvmspace_alloc(vaddr_t vmin, vaddr_t vmax) 3611 { 3612 struct vmspace *vm; 3613 UVMHIST_FUNC("uvmspace_alloc"); UVMHIST_CALLED(maphist); 3614 3615 vm = pool_get(&uvm_vmspace_pool, PR_WAITOK); 3616 uvmspace_init(vm, NULL, vmin, vmax); 3617 UVMHIST_LOG(maphist,"<- done (vm=0x%x)", vm,0,0,0); 3618 return (vm); 3619 } 3620 3621 /* 3622 * uvmspace_init: initialize a vmspace structure. 3623 * 3624 * - XXX: no locking on this structure 3625 * - refcnt set to 1, rest must be init'd by caller 3626 */ 3627 void 3628 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t vmin, vaddr_t vmax) 3629 { 3630 UVMHIST_FUNC("uvmspace_init"); UVMHIST_CALLED(maphist); 3631 3632 memset(vm, 0, sizeof(*vm)); 3633 uvm_map_setup(&vm->vm_map, vmin, vmax, VM_MAP_PAGEABLE 3634 #ifdef __USING_TOPDOWN_VM 3635 | VM_MAP_TOPDOWN 3636 #endif 3637 ); 3638 if (pmap) 3639 pmap_reference(pmap); 3640 else 3641 pmap = pmap_create(); 3642 vm->vm_map.pmap = pmap; 3643 vm->vm_refcnt = 1; 3644 UVMHIST_LOG(maphist,"<- done",0,0,0,0); 3645 } 3646 3647 /* 3648 * uvmspace_share: share a vmspace between two processes 3649 * 3650 * - used for vfork, threads(?) 3651 */ 3652 3653 void 3654 uvmspace_share(struct proc *p1, struct proc *p2) 3655 { 3656 struct simplelock *slock = &p1->p_vmspace->vm_map.ref_lock; 3657 3658 p2->p_vmspace = p1->p_vmspace; 3659 simple_lock(slock); 3660 p1->p_vmspace->vm_refcnt++; 3661 simple_unlock(slock); 3662 } 3663 3664 /* 3665 * uvmspace_unshare: ensure that process "p" has its own, unshared, vmspace 3666 * 3667 * - XXX: no locking on vmspace 3668 */ 3669 3670 void 3671 uvmspace_unshare(struct lwp *l) 3672 { 3673 struct proc *p = l->l_proc; 3674 struct vmspace *nvm, *ovm = p->p_vmspace; 3675 3676 if (ovm->vm_refcnt == 1) 3677 /* nothing to do: vmspace isn't shared in the first place */ 3678 return; 3679 3680 /* make a new vmspace, still holding old one */ 3681 nvm = uvmspace_fork(ovm); 3682 3683 pmap_deactivate(l); /* unbind old vmspace */ 3684 p->p_vmspace = nvm; 3685 pmap_activate(l); /* switch to new vmspace */ 3686 3687 uvmspace_free(ovm); /* drop reference to old vmspace */ 3688 } 3689 3690 /* 3691 * uvmspace_exec: the process wants to exec a new program 3692 */ 3693 3694 void 3695 uvmspace_exec(struct lwp *l, vaddr_t start, vaddr_t end) 3696 { 3697 struct proc *p = l->l_proc; 3698 struct vmspace *nvm, *ovm = p->p_vmspace; 3699 struct vm_map *map = &ovm->vm_map; 3700 3701 #ifdef __sparc__ 3702 /* XXX cgd 960926: the sparc #ifdef should be a MD hook */ 3703 kill_user_windows(l); /* before stack addresses go away */ 3704 #endif 3705 3706 /* 3707 * see if more than one process is using this vmspace... 3708 */ 3709 3710 if (ovm->vm_refcnt == 1) { 3711 3712 /* 3713 * if p is the only process using its vmspace then we can safely 3714 * recycle that vmspace for the program that is being exec'd. 3715 */ 3716 3717 #ifdef SYSVSHM 3718 /* 3719 * SYSV SHM semantics require us to kill all segments on an exec 3720 */ 3721 3722 if (ovm->vm_shm) 3723 shmexit(ovm); 3724 #endif 3725 3726 /* 3727 * POSIX 1003.1b -- "lock future mappings" is revoked 3728 * when a process execs another program image. 3729 */ 3730 3731 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE); 3732 3733 /* 3734 * now unmap the old program 3735 */ 3736 3737 pmap_remove_all(map->pmap); 3738 uvm_unmap(map, vm_map_min(map), vm_map_max(map)); 3739 KASSERT(map->header.prev == &map->header); 3740 KASSERT(map->nentries == 0); 3741 3742 /* 3743 * resize the map 3744 */ 3745 3746 vm_map_setmin(map, start); 3747 vm_map_setmax(map, end); 3748 } else { 3749 3750 /* 3751 * p's vmspace is being shared, so we can't reuse it for p since 3752 * it is still being used for others. allocate a new vmspace 3753 * for p 3754 */ 3755 3756 nvm = uvmspace_alloc(start, end); 3757 3758 /* 3759 * install new vmspace and drop our ref to the old one. 3760 */ 3761 3762 pmap_deactivate(l); 3763 p->p_vmspace = nvm; 3764 pmap_activate(l); 3765 3766 uvmspace_free(ovm); 3767 } 3768 } 3769 3770 /* 3771 * uvmspace_free: free a vmspace data structure 3772 */ 3773 3774 void 3775 uvmspace_free(struct vmspace *vm) 3776 { 3777 struct vm_map_entry *dead_entries; 3778 struct vm_map *map = &vm->vm_map; 3779 int n; 3780 3781 UVMHIST_FUNC("uvmspace_free"); UVMHIST_CALLED(maphist); 3782 3783 UVMHIST_LOG(maphist,"(vm=0x%x) ref=%d", vm, vm->vm_refcnt,0,0); 3784 simple_lock(&map->ref_lock); 3785 n = --vm->vm_refcnt; 3786 simple_unlock(&map->ref_lock); 3787 if (n > 0) 3788 return; 3789 3790 /* 3791 * at this point, there should be no other references to the map. 3792 * delete all of the mappings, then destroy the pmap. 3793 */ 3794 3795 map->flags |= VM_MAP_DYING; 3796 pmap_remove_all(map->pmap); 3797 #ifdef SYSVSHM 3798 /* Get rid of any SYSV shared memory segments. */ 3799 if (vm->vm_shm != NULL) 3800 shmexit(vm); 3801 #endif 3802 if (map->nentries) { 3803 uvm_unmap_remove(map, vm_map_min(map), vm_map_max(map), 3804 &dead_entries, NULL, 0); 3805 if (dead_entries != NULL) 3806 uvm_unmap_detach(dead_entries, 0); 3807 } 3808 KASSERT(map->nentries == 0); 3809 KASSERT(map->size == 0); 3810 pmap_destroy(map->pmap); 3811 pool_put(&uvm_vmspace_pool, vm); 3812 } 3813 3814 /* 3815 * F O R K - m a i n e n t r y p o i n t 3816 */ 3817 /* 3818 * uvmspace_fork: fork a process' main map 3819 * 3820 * => create a new vmspace for child process from parent. 3821 * => parent's map must not be locked. 3822 */ 3823 3824 struct vmspace * 3825 uvmspace_fork(struct vmspace *vm1) 3826 { 3827 struct vmspace *vm2; 3828 struct vm_map *old_map = &vm1->vm_map; 3829 struct vm_map *new_map; 3830 struct vm_map_entry *old_entry; 3831 struct vm_map_entry *new_entry; 3832 UVMHIST_FUNC("uvmspace_fork"); UVMHIST_CALLED(maphist); 3833 3834 vm_map_lock(old_map); 3835 3836 vm2 = uvmspace_alloc(vm_map_min(old_map), vm_map_max(old_map)); 3837 memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy, 3838 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy); 3839 new_map = &vm2->vm_map; /* XXX */ 3840 3841 old_entry = old_map->header.next; 3842 new_map->size = old_map->size; 3843 3844 /* 3845 * go entry-by-entry 3846 */ 3847 3848 while (old_entry != &old_map->header) { 3849 3850 /* 3851 * first, some sanity checks on the old entry 3852 */ 3853 3854 KASSERT(!UVM_ET_ISSUBMAP(old_entry)); 3855 KASSERT(UVM_ET_ISCOPYONWRITE(old_entry) || 3856 !UVM_ET_ISNEEDSCOPY(old_entry)); 3857 3858 switch (old_entry->inheritance) { 3859 case MAP_INHERIT_NONE: 3860 3861 /* 3862 * drop the mapping, modify size 3863 */ 3864 new_map->size -= old_entry->end - old_entry->start; 3865 break; 3866 3867 case MAP_INHERIT_SHARE: 3868 3869 /* 3870 * share the mapping: this means we want the old and 3871 * new entries to share amaps and backing objects. 3872 */ 3873 /* 3874 * if the old_entry needs a new amap (due to prev fork) 3875 * then we need to allocate it now so that we have 3876 * something we own to share with the new_entry. [in 3877 * other words, we need to clear needs_copy] 3878 */ 3879 3880 if (UVM_ET_ISNEEDSCOPY(old_entry)) { 3881 /* get our own amap, clears needs_copy */ 3882 amap_copy(old_map, old_entry, M_WAITOK, FALSE, 3883 0, 0); 3884 /* XXXCDC: WAITOK??? */ 3885 } 3886 3887 new_entry = uvm_mapent_alloc(new_map, 0); 3888 /* old_entry -> new_entry */ 3889 uvm_mapent_copy(old_entry, new_entry); 3890 3891 /* new pmap has nothing wired in it */ 3892 new_entry->wired_count = 0; 3893 3894 /* 3895 * gain reference to object backing the map (can't 3896 * be a submap, already checked this case). 3897 */ 3898 3899 if (new_entry->aref.ar_amap) 3900 uvm_map_reference_amap(new_entry, AMAP_SHARED); 3901 3902 if (new_entry->object.uvm_obj && 3903 new_entry->object.uvm_obj->pgops->pgo_reference) 3904 new_entry->object.uvm_obj-> 3905 pgops->pgo_reference( 3906 new_entry->object.uvm_obj); 3907 3908 /* insert entry at end of new_map's entry list */ 3909 uvm_map_entry_link(new_map, new_map->header.prev, 3910 new_entry); 3911 3912 break; 3913 3914 case MAP_INHERIT_COPY: 3915 3916 /* 3917 * copy-on-write the mapping (using mmap's 3918 * MAP_PRIVATE semantics) 3919 * 3920 * allocate new_entry, adjust reference counts. 3921 * (note that new references are read-only). 3922 */ 3923 3924 new_entry = uvm_mapent_alloc(new_map, 0); 3925 /* old_entry -> new_entry */ 3926 uvm_mapent_copy(old_entry, new_entry); 3927 3928 if (new_entry->aref.ar_amap) 3929 uvm_map_reference_amap(new_entry, 0); 3930 3931 if (new_entry->object.uvm_obj && 3932 new_entry->object.uvm_obj->pgops->pgo_reference) 3933 new_entry->object.uvm_obj->pgops->pgo_reference 3934 (new_entry->object.uvm_obj); 3935 3936 /* new pmap has nothing wired in it */ 3937 new_entry->wired_count = 0; 3938 3939 new_entry->etype |= 3940 (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY); 3941 uvm_map_entry_link(new_map, new_map->header.prev, 3942 new_entry); 3943 3944 /* 3945 * the new entry will need an amap. it will either 3946 * need to be copied from the old entry or created 3947 * from scratch (if the old entry does not have an 3948 * amap). can we defer this process until later 3949 * (by setting "needs_copy") or do we need to copy 3950 * the amap now? 3951 * 3952 * we must copy the amap now if any of the following 3953 * conditions hold: 3954 * 1. the old entry has an amap and that amap is 3955 * being shared. this means that the old (parent) 3956 * process is sharing the amap with another 3957 * process. if we do not clear needs_copy here 3958 * we will end up in a situation where both the 3959 * parent and child process are refering to the 3960 * same amap with "needs_copy" set. if the 3961 * parent write-faults, the fault routine will 3962 * clear "needs_copy" in the parent by allocating 3963 * a new amap. this is wrong because the 3964 * parent is supposed to be sharing the old amap 3965 * and the new amap will break that. 3966 * 3967 * 2. if the old entry has an amap and a non-zero 3968 * wire count then we are going to have to call 3969 * amap_cow_now to avoid page faults in the 3970 * parent process. since amap_cow_now requires 3971 * "needs_copy" to be clear we might as well 3972 * clear it here as well. 3973 * 3974 */ 3975 3976 if (old_entry->aref.ar_amap != NULL) { 3977 if ((amap_flags(old_entry->aref.ar_amap) & 3978 AMAP_SHARED) != 0 || 3979 VM_MAPENT_ISWIRED(old_entry)) { 3980 3981 amap_copy(new_map, new_entry, M_WAITOK, 3982 FALSE, 0, 0); 3983 /* XXXCDC: M_WAITOK ... ok? */ 3984 } 3985 } 3986 3987 /* 3988 * if the parent's entry is wired down, then the 3989 * parent process does not want page faults on 3990 * access to that memory. this means that we 3991 * cannot do copy-on-write because we can't write 3992 * protect the old entry. in this case we 3993 * resolve all copy-on-write faults now, using 3994 * amap_cow_now. note that we have already 3995 * allocated any needed amap (above). 3996 */ 3997 3998 if (VM_MAPENT_ISWIRED(old_entry)) { 3999 4000 /* 4001 * resolve all copy-on-write faults now 4002 * (note that there is nothing to do if 4003 * the old mapping does not have an amap). 4004 */ 4005 if (old_entry->aref.ar_amap) 4006 amap_cow_now(new_map, new_entry); 4007 4008 } else { 4009 4010 /* 4011 * setup mappings to trigger copy-on-write faults 4012 * we must write-protect the parent if it has 4013 * an amap and it is not already "needs_copy"... 4014 * if it is already "needs_copy" then the parent 4015 * has already been write-protected by a previous 4016 * fork operation. 4017 */ 4018 4019 if (old_entry->aref.ar_amap && 4020 !UVM_ET_ISNEEDSCOPY(old_entry)) { 4021 if (old_entry->max_protection & VM_PROT_WRITE) { 4022 pmap_protect(old_map->pmap, 4023 old_entry->start, 4024 old_entry->end, 4025 old_entry->protection & 4026 ~VM_PROT_WRITE); 4027 pmap_update(old_map->pmap); 4028 } 4029 old_entry->etype |= UVM_ET_NEEDSCOPY; 4030 } 4031 } 4032 break; 4033 } /* end of switch statement */ 4034 old_entry = old_entry->next; 4035 } 4036 4037 vm_map_unlock(old_map); 4038 4039 #ifdef SYSVSHM 4040 if (vm1->vm_shm) 4041 shmfork(vm1, vm2); 4042 #endif 4043 4044 #ifdef PMAP_FORK 4045 pmap_fork(vm1->vm_map.pmap, vm2->vm_map.pmap); 4046 #endif 4047 4048 UVMHIST_LOG(maphist,"<- done",0,0,0,0); 4049 return (vm2); 4050 } 4051 4052 4053 /* 4054 * in-kernel map entry allocation. 4055 */ 4056 4057 struct uvm_kmapent_hdr { 4058 LIST_ENTRY(uvm_kmapent_hdr) ukh_listq; 4059 int ukh_nused; 4060 struct vm_map_entry *ukh_freelist; 4061 struct vm_map *ukh_map; 4062 struct vm_map_entry ukh_entries[0]; 4063 }; 4064 4065 #define UVM_KMAPENT_CHUNK \ 4066 ((PAGE_SIZE - sizeof(struct uvm_kmapent_hdr)) \ 4067 / sizeof(struct vm_map_entry)) 4068 4069 #define UVM_KHDR_FIND(entry) \ 4070 ((struct uvm_kmapent_hdr *)(((vaddr_t)entry) & ~PAGE_MASK)) 4071 4072 4073 #ifdef DIAGNOSTIC 4074 static struct vm_map * 4075 uvm_kmapent_map(struct vm_map_entry *entry) 4076 { 4077 const struct uvm_kmapent_hdr *ukh; 4078 4079 ukh = UVM_KHDR_FIND(entry); 4080 return ukh->ukh_map; 4081 } 4082 #endif 4083 4084 static inline struct vm_map_entry * 4085 uvm_kmapent_get(struct uvm_kmapent_hdr *ukh) 4086 { 4087 struct vm_map_entry *entry; 4088 4089 KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK); 4090 KASSERT(ukh->ukh_nused >= 0); 4091 4092 entry = ukh->ukh_freelist; 4093 if (entry) { 4094 KASSERT((entry->flags & (UVM_MAP_KERNEL | UVM_MAP_KMAPENT)) 4095 == UVM_MAP_KERNEL); 4096 ukh->ukh_freelist = entry->next; 4097 ukh->ukh_nused++; 4098 KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK); 4099 } else { 4100 KASSERT(ukh->ukh_nused == UVM_KMAPENT_CHUNK); 4101 } 4102 4103 return entry; 4104 } 4105 4106 static inline void 4107 uvm_kmapent_put(struct uvm_kmapent_hdr *ukh, struct vm_map_entry *entry) 4108 { 4109 4110 KASSERT((entry->flags & (UVM_MAP_KERNEL | UVM_MAP_KMAPENT)) 4111 == UVM_MAP_KERNEL); 4112 KASSERT(ukh->ukh_nused <= UVM_KMAPENT_CHUNK); 4113 KASSERT(ukh->ukh_nused > 0); 4114 KASSERT(ukh->ukh_freelist != NULL || 4115 ukh->ukh_nused == UVM_KMAPENT_CHUNK); 4116 KASSERT(ukh->ukh_freelist == NULL || 4117 ukh->ukh_nused < UVM_KMAPENT_CHUNK); 4118 4119 ukh->ukh_nused--; 4120 entry->next = ukh->ukh_freelist; 4121 ukh->ukh_freelist = entry; 4122 } 4123 4124 /* 4125 * uvm_kmapent_alloc: allocate a map entry for in-kernel map 4126 */ 4127 4128 static struct vm_map_entry * 4129 uvm_kmapent_alloc(struct vm_map *map, int flags) 4130 { 4131 struct vm_page *pg; 4132 struct uvm_map_args args; 4133 struct uvm_kmapent_hdr *ukh; 4134 struct vm_map_entry *entry; 4135 uvm_flag_t mapflags = UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, 4136 UVM_INH_NONE, UVM_ADV_RANDOM, flags | UVM_FLAG_NOMERGE); 4137 vaddr_t va; 4138 int error; 4139 int i; 4140 int s; 4141 4142 KDASSERT(UVM_KMAPENT_CHUNK > 2); 4143 KDASSERT(kernel_map != NULL); 4144 KASSERT(vm_map_pmap(map) == pmap_kernel()); 4145 4146 UVMMAP_EVCNT_INCR(uke_alloc); 4147 entry = NULL; 4148 again: 4149 /* 4150 * try to grab an entry from freelist. 4151 */ 4152 s = splvm(); 4153 simple_lock(&uvm.kentry_lock); 4154 ukh = LIST_FIRST(&vm_map_to_kernel(map)->vmk_kentry_free); 4155 if (ukh) { 4156 entry = uvm_kmapent_get(ukh); 4157 if (ukh->ukh_nused == UVM_KMAPENT_CHUNK) 4158 LIST_REMOVE(ukh, ukh_listq); 4159 } 4160 simple_unlock(&uvm.kentry_lock); 4161 splx(s); 4162 4163 if (entry) 4164 return entry; 4165 4166 /* 4167 * there's no free entry for this vm_map. 4168 * now we need to allocate some vm_map_entry. 4169 * for simplicity, always allocate one page chunk of them at once. 4170 */ 4171 4172 pg = uvm_pagealloc(NULL, 0, NULL, 0); 4173 if (__predict_false(pg == NULL)) { 4174 if (flags & UVM_FLAG_NOWAIT) 4175 return NULL; 4176 uvm_wait("kme_alloc"); 4177 goto again; 4178 } 4179 4180 error = uvm_map_prepare(map, 0, PAGE_SIZE, NULL, 0, 0, mapflags, &args); 4181 if (error) { 4182 uvm_pagefree(pg); 4183 return NULL; 4184 } 4185 4186 va = args.uma_start; 4187 4188 pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg), VM_PROT_READ|VM_PROT_WRITE); 4189 pmap_update(vm_map_pmap(map)); 4190 4191 ukh = (void *)va; 4192 4193 /* 4194 * use the first entry for ukh itsself. 4195 */ 4196 4197 entry = &ukh->ukh_entries[0]; 4198 entry->flags = UVM_MAP_KERNEL | UVM_MAP_KMAPENT; 4199 error = uvm_map_enter(map, &args, entry); 4200 KASSERT(error == 0); 4201 4202 ukh->ukh_nused = UVM_KMAPENT_CHUNK; 4203 ukh->ukh_map = map; 4204 ukh->ukh_freelist = NULL; 4205 for (i = UVM_KMAPENT_CHUNK - 1; i >= 2; i--) { 4206 struct vm_map_entry *xentry = &ukh->ukh_entries[i]; 4207 4208 xentry->flags = UVM_MAP_KERNEL; 4209 uvm_kmapent_put(ukh, xentry); 4210 } 4211 KASSERT(ukh->ukh_nused == 2); 4212 4213 s = splvm(); 4214 simple_lock(&uvm.kentry_lock); 4215 LIST_INSERT_HEAD(&vm_map_to_kernel(map)->vmk_kentry_free, 4216 ukh, ukh_listq); 4217 simple_unlock(&uvm.kentry_lock); 4218 splx(s); 4219 4220 /* 4221 * return second entry. 4222 */ 4223 4224 entry = &ukh->ukh_entries[1]; 4225 entry->flags = UVM_MAP_KERNEL; 4226 UVMMAP_EVCNT_INCR(ukh_alloc); 4227 return entry; 4228 } 4229 4230 /* 4231 * uvm_mapent_free: free map entry for in-kernel map 4232 */ 4233 4234 static void 4235 uvm_kmapent_free(struct vm_map_entry *entry) 4236 { 4237 struct uvm_kmapent_hdr *ukh; 4238 struct vm_page *pg; 4239 struct vm_map *map; 4240 struct pmap *pmap; 4241 vaddr_t va; 4242 paddr_t pa; 4243 struct vm_map_entry *deadentry; 4244 int s; 4245 4246 UVMMAP_EVCNT_INCR(uke_free); 4247 ukh = UVM_KHDR_FIND(entry); 4248 map = ukh->ukh_map; 4249 4250 s = splvm(); 4251 simple_lock(&uvm.kentry_lock); 4252 uvm_kmapent_put(ukh, entry); 4253 if (ukh->ukh_nused > 1) { 4254 if (ukh->ukh_nused == UVM_KMAPENT_CHUNK - 1) 4255 LIST_INSERT_HEAD( 4256 &vm_map_to_kernel(map)->vmk_kentry_free, 4257 ukh, ukh_listq); 4258 simple_unlock(&uvm.kentry_lock); 4259 splx(s); 4260 return; 4261 } 4262 4263 /* 4264 * now we can free this ukh. 4265 * 4266 * however, keep an empty ukh to avoid ping-pong. 4267 */ 4268 4269 if (LIST_FIRST(&vm_map_to_kernel(map)->vmk_kentry_free) == ukh && 4270 LIST_NEXT(ukh, ukh_listq) == NULL) { 4271 simple_unlock(&uvm.kentry_lock); 4272 splx(s); 4273 return; 4274 } 4275 LIST_REMOVE(ukh, ukh_listq); 4276 simple_unlock(&uvm.kentry_lock); 4277 splx(s); 4278 4279 KASSERT(ukh->ukh_nused == 1); 4280 4281 /* 4282 * remove map entry for ukh itsself. 4283 */ 4284 4285 va = (vaddr_t)ukh; 4286 KASSERT((va & PAGE_MASK) == 0); 4287 vm_map_lock(map); 4288 uvm_unmap_remove(map, va, va + PAGE_SIZE, &deadentry, NULL, 0); 4289 KASSERT(deadentry->flags & UVM_MAP_KERNEL); 4290 KASSERT(deadentry->flags & UVM_MAP_KMAPENT); 4291 KASSERT(deadentry->next == NULL); 4292 KASSERT(deadentry == &ukh->ukh_entries[0]); 4293 4294 /* 4295 * unmap the page from pmap and free it. 4296 */ 4297 4298 pmap = vm_map_pmap(map); 4299 KASSERT(pmap == pmap_kernel()); 4300 if (!pmap_extract(pmap, va, &pa)) 4301 panic("%s: no mapping", __func__); 4302 pmap_kremove(va, PAGE_SIZE); 4303 vm_map_unlock(map); 4304 pg = PHYS_TO_VM_PAGE(pa); 4305 uvm_pagefree(pg); 4306 UVMMAP_EVCNT_INCR(ukh_free); 4307 } 4308 4309 /* 4310 * map entry reservation 4311 */ 4312 4313 /* 4314 * uvm_mapent_reserve: reserve map entries for clipping before locking map. 4315 * 4316 * => needed when unmapping entries allocated without UVM_FLAG_QUANTUM. 4317 * => caller shouldn't hold map locked. 4318 */ 4319 int 4320 uvm_mapent_reserve(struct vm_map *map, struct uvm_mapent_reservation *umr, 4321 int nentries, int flags) 4322 { 4323 4324 umr->umr_nentries = 0; 4325 4326 if ((flags & UVM_FLAG_QUANTUM) != 0) 4327 return 0; 4328 4329 if (!VM_MAP_USE_KMAPENT(map)) 4330 return 0; 4331 4332 while (nentries--) { 4333 struct vm_map_entry *ent; 4334 ent = uvm_kmapent_alloc(map, flags); 4335 if (!ent) { 4336 uvm_mapent_unreserve(map, umr); 4337 return ENOMEM; 4338 } 4339 UMR_PUTENTRY(umr, ent); 4340 } 4341 4342 return 0; 4343 } 4344 4345 /* 4346 * uvm_mapent_unreserve: 4347 * 4348 * => caller shouldn't hold map locked. 4349 * => never fail or sleep. 4350 */ 4351 void 4352 uvm_mapent_unreserve(struct vm_map *map, struct uvm_mapent_reservation *umr) 4353 { 4354 4355 while (!UMR_EMPTY(umr)) 4356 uvm_kmapent_free(UMR_GETENTRY(umr)); 4357 } 4358 4359 /* 4360 * uvm_mapent_trymerge: try to merge an entry with its neighbors. 4361 * 4362 * => called with map locked. 4363 * => return non zero if successfully merged. 4364 */ 4365 4366 int 4367 uvm_mapent_trymerge(struct vm_map *map, struct vm_map_entry *entry, int flags) 4368 { 4369 struct uvm_object *uobj; 4370 struct vm_map_entry *next; 4371 struct vm_map_entry *prev; 4372 vsize_t size; 4373 int merged = 0; 4374 boolean_t copying; 4375 int newetype; 4376 4377 if (VM_MAP_USE_KMAPENT(map)) { 4378 return 0; 4379 } 4380 if (entry->aref.ar_amap != NULL) { 4381 return 0; 4382 } 4383 if ((entry->flags & UVM_MAP_NOMERGE) != 0) { 4384 return 0; 4385 } 4386 4387 uobj = entry->object.uvm_obj; 4388 size = entry->end - entry->start; 4389 copying = (flags & UVM_MERGE_COPYING) != 0; 4390 newetype = copying ? (entry->etype & ~UVM_ET_NEEDSCOPY) : entry->etype; 4391 4392 next = entry->next; 4393 if (next != &map->header && 4394 next->start == entry->end && 4395 ((copying && next->aref.ar_amap != NULL && 4396 amap_refs(next->aref.ar_amap) == 1) || 4397 (!copying && next->aref.ar_amap == NULL)) && 4398 UVM_ET_ISCOMPATIBLE(next, newetype, 4399 uobj, entry->flags, entry->protection, 4400 entry->max_protection, entry->inheritance, entry->advice, 4401 entry->wired_count) && 4402 (uobj == NULL || entry->offset + size == next->offset)) { 4403 int error; 4404 4405 if (copying) { 4406 error = amap_extend(next, size, 4407 AMAP_EXTEND_NOWAIT|AMAP_EXTEND_BACKWARDS); 4408 } else { 4409 error = 0; 4410 } 4411 if (error == 0) { 4412 if (uobj) { 4413 if (uobj->pgops->pgo_detach) { 4414 uobj->pgops->pgo_detach(uobj); 4415 } 4416 } 4417 4418 entry->end = next->end; 4419 uvm_map_entry_unlink(map, next); 4420 if (copying) { 4421 entry->aref = next->aref; 4422 entry->etype &= ~UVM_ET_NEEDSCOPY; 4423 } 4424 uvm_tree_sanity(map, "trymerge forwardmerge"); 4425 uvm_mapent_free_merged(map, next); 4426 merged++; 4427 } 4428 } 4429 4430 prev = entry->prev; 4431 if (prev != &map->header && 4432 prev->end == entry->start && 4433 ((copying && !merged && prev->aref.ar_amap != NULL && 4434 amap_refs(prev->aref.ar_amap) == 1) || 4435 (!copying && prev->aref.ar_amap == NULL)) && 4436 UVM_ET_ISCOMPATIBLE(prev, newetype, 4437 uobj, entry->flags, entry->protection, 4438 entry->max_protection, entry->inheritance, entry->advice, 4439 entry->wired_count) && 4440 (uobj == NULL || 4441 prev->offset + prev->end - prev->start == entry->offset)) { 4442 int error; 4443 4444 if (copying) { 4445 error = amap_extend(prev, size, 4446 AMAP_EXTEND_NOWAIT|AMAP_EXTEND_FORWARDS); 4447 } else { 4448 error = 0; 4449 } 4450 if (error == 0) { 4451 if (uobj) { 4452 if (uobj->pgops->pgo_detach) { 4453 uobj->pgops->pgo_detach(uobj); 4454 } 4455 entry->offset = prev->offset; 4456 } 4457 4458 entry->start = prev->start; 4459 uvm_map_entry_unlink(map, prev); 4460 if (copying) { 4461 entry->aref = prev->aref; 4462 entry->etype &= ~UVM_ET_NEEDSCOPY; 4463 } 4464 uvm_tree_sanity(map, "trymerge backmerge"); 4465 uvm_mapent_free_merged(map, prev); 4466 merged++; 4467 } 4468 } 4469 4470 return merged; 4471 } 4472 4473 #if defined(DDB) 4474 4475 /* 4476 * DDB hooks 4477 */ 4478 4479 /* 4480 * uvm_map_printit: actually prints the map 4481 */ 4482 4483 void 4484 uvm_map_printit(struct vm_map *map, boolean_t full, 4485 void (*pr)(const char *, ...)) 4486 { 4487 struct vm_map_entry *entry; 4488 4489 (*pr)("MAP %p: [0x%lx->0x%lx]\n", map, vm_map_min(map), 4490 vm_map_max(map)); 4491 (*pr)("\t#ent=%d, sz=%d, ref=%d, version=%d, flags=0x%x\n", 4492 map->nentries, map->size, map->ref_count, map->timestamp, 4493 map->flags); 4494 (*pr)("\tpmap=%p(resident=%ld, wired=%ld)\n", map->pmap, 4495 pmap_resident_count(map->pmap), pmap_wired_count(map->pmap)); 4496 if (!full) 4497 return; 4498 for (entry = map->header.next; entry != &map->header; 4499 entry = entry->next) { 4500 (*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n", 4501 entry, entry->start, entry->end, entry->object.uvm_obj, 4502 (long long)entry->offset, entry->aref.ar_amap, 4503 entry->aref.ar_pageoff); 4504 (*pr)( 4505 "\tsubmap=%c, cow=%c, nc=%c, prot(max)=%d/%d, inh=%d, " 4506 "wc=%d, adv=%d\n", 4507 (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F', 4508 (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F', 4509 (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F', 4510 entry->protection, entry->max_protection, 4511 entry->inheritance, entry->wired_count, entry->advice); 4512 } 4513 } 4514 4515 /* 4516 * uvm_object_printit: actually prints the object 4517 */ 4518 4519 void 4520 uvm_object_printit(struct uvm_object *uobj, boolean_t full, 4521 void (*pr)(const char *, ...)) 4522 { 4523 struct vm_page *pg; 4524 int cnt = 0; 4525 4526 (*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ", 4527 uobj, uobj->vmobjlock.lock_data, uobj->pgops, uobj->uo_npages); 4528 if (UVM_OBJ_IS_KERN_OBJECT(uobj)) 4529 (*pr)("refs=<SYSTEM>\n"); 4530 else 4531 (*pr)("refs=%d\n", uobj->uo_refs); 4532 4533 if (!full) { 4534 return; 4535 } 4536 (*pr)(" PAGES <pg,offset>:\n "); 4537 TAILQ_FOREACH(pg, &uobj->memq, listq) { 4538 cnt++; 4539 (*pr)("<%p,0x%llx> ", pg, (long long)pg->offset); 4540 if ((cnt % 3) == 0) { 4541 (*pr)("\n "); 4542 } 4543 } 4544 if ((cnt % 3) != 0) { 4545 (*pr)("\n"); 4546 } 4547 } 4548 4549 /* 4550 * uvm_page_printit: actually print the page 4551 */ 4552 4553 static const char page_flagbits[] = 4554 "\20\1BUSY\2WANTED\3TABLED\4CLEAN\5PAGEOUT\6RELEASED\7FAKE\10RDONLY" 4555 "\11ZERO\15PAGER1"; 4556 static const char page_pqflagbits[] = 4557 "\20\1FREE\2INACTIVE\3ACTIVE\5ANON\6AOBJ"; 4558 4559 void 4560 uvm_page_printit(struct vm_page *pg, boolean_t full, 4561 void (*pr)(const char *, ...)) 4562 { 4563 struct vm_page *tpg; 4564 struct uvm_object *uobj; 4565 struct pglist *pgl; 4566 char pgbuf[128]; 4567 char pqbuf[128]; 4568 4569 (*pr)("PAGE %p:\n", pg); 4570 bitmask_snprintf(pg->flags, page_flagbits, pgbuf, sizeof(pgbuf)); 4571 bitmask_snprintf(pg->pqflags, page_pqflagbits, pqbuf, sizeof(pqbuf)); 4572 (*pr)(" flags=%s, pqflags=%s, wire_count=%d, pa=0x%lx\n", 4573 pgbuf, pqbuf, pg->wire_count, (long)VM_PAGE_TO_PHYS(pg)); 4574 (*pr)(" uobject=%p, uanon=%p, offset=0x%llx loan_count=%d\n", 4575 pg->uobject, pg->uanon, (long long)pg->offset, pg->loan_count); 4576 #if defined(UVM_PAGE_TRKOWN) 4577 if (pg->flags & PG_BUSY) 4578 (*pr)(" owning process = %d, tag=%s\n", 4579 pg->owner, pg->owner_tag); 4580 else 4581 (*pr)(" page not busy, no owner\n"); 4582 #else 4583 (*pr)(" [page ownership tracking disabled]\n"); 4584 #endif 4585 4586 if (!full) 4587 return; 4588 4589 /* cross-verify object/anon */ 4590 if ((pg->pqflags & PQ_FREE) == 0) { 4591 if (pg->pqflags & PQ_ANON) { 4592 if (pg->uanon == NULL || pg->uanon->an_page != pg) 4593 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n", 4594 (pg->uanon) ? pg->uanon->an_page : NULL); 4595 else 4596 (*pr)(" anon backpointer is OK\n"); 4597 } else { 4598 uobj = pg->uobject; 4599 if (uobj) { 4600 (*pr)(" checking object list\n"); 4601 TAILQ_FOREACH(tpg, &uobj->memq, listq) { 4602 if (tpg == pg) { 4603 break; 4604 } 4605 } 4606 if (tpg) 4607 (*pr)(" page found on object list\n"); 4608 else 4609 (*pr)(" >>> PAGE NOT FOUND ON OBJECT LIST! <<<\n"); 4610 } 4611 } 4612 } 4613 4614 /* cross-verify page queue */ 4615 if (pg->pqflags & PQ_FREE) { 4616 int fl = uvm_page_lookup_freelist(pg); 4617 int color = VM_PGCOLOR_BUCKET(pg); 4618 pgl = &uvm.page_free[fl].pgfl_buckets[color].pgfl_queues[ 4619 ((pg)->flags & PG_ZERO) ? PGFL_ZEROS : PGFL_UNKNOWN]; 4620 } else if (pg->pqflags & PQ_INACTIVE) { 4621 pgl = &uvm.page_inactive; 4622 } else if (pg->pqflags & PQ_ACTIVE) { 4623 pgl = &uvm.page_active; 4624 } else { 4625 pgl = NULL; 4626 } 4627 4628 if (pgl) { 4629 (*pr)(" checking pageq list\n"); 4630 TAILQ_FOREACH(tpg, pgl, pageq) { 4631 if (tpg == pg) { 4632 break; 4633 } 4634 } 4635 if (tpg) 4636 (*pr)(" page found on pageq list\n"); 4637 else 4638 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n"); 4639 } 4640 } 4641 #endif 4642