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