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