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