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