1 /* $OpenBSD: uvm_map.c,v 1.326 2024/01/21 17:21:55 deraadt Exp $ */ 2 /* $NetBSD: uvm_map.c,v 1.86 2000/11/27 08:40:03 chs Exp $ */ 3 4 /* 5 * Copyright (c) 2011 Ariane van der Steldt <ariane@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 * 19 * 20 * Copyright (c) 1997 Charles D. Cranor and Washington University. 21 * Copyright (c) 1991, 1993, The Regents of the University of California. 22 * 23 * All rights reserved. 24 * 25 * This code is derived from software contributed to Berkeley by 26 * The Mach Operating System project at Carnegie-Mellon University. 27 * 28 * Redistribution and use in source and binary forms, with or without 29 * modification, are permitted provided that the following conditions 30 * are met: 31 * 1. Redistributions of source code must retain the above copyright 32 * notice, this list of conditions and the following disclaimer. 33 * 2. Redistributions in binary form must reproduce the above copyright 34 * notice, this list of conditions and the following disclaimer in the 35 * documentation and/or other materials provided with the distribution. 36 * 3. Neither the name of the University nor the names of its contributors 37 * may be used to endorse or promote products derived from this software 38 * without specific prior written permission. 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * 52 * @(#)vm_map.c 8.3 (Berkeley) 1/12/94 53 * from: Id: uvm_map.c,v 1.1.2.27 1998/02/07 01:16:54 chs Exp 54 * 55 * 56 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 57 * All rights reserved. 58 * 59 * Permission to use, copy, modify and distribute this software and 60 * its documentation is hereby granted, provided that both the copyright 61 * notice and this permission notice appear in all copies of the 62 * software, derivative works or modified versions, and any portions 63 * thereof, and that both notices appear in supporting documentation. 64 * 65 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 66 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 67 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 68 * 69 * Carnegie Mellon requests users of this software to return to 70 * 71 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 72 * School of Computer Science 73 * Carnegie Mellon University 74 * Pittsburgh PA 15213-3890 75 * 76 * any improvements or extensions that they make and grant Carnegie the 77 * rights to redistribute these changes. 78 */ 79 80 /* 81 * uvm_map.c: uvm map operations 82 */ 83 84 /* #define DEBUG */ 85 /* #define VMMAP_DEBUG */ 86 87 #include <sys/param.h> 88 #include <sys/systm.h> 89 #include <sys/acct.h> 90 #include <sys/mman.h> 91 #include <sys/proc.h> 92 #include <sys/malloc.h> 93 #include <sys/pool.h> 94 #include <sys/sysctl.h> 95 #include <sys/signalvar.h> 96 #include <sys/syslog.h> 97 #include <sys/user.h> 98 #include <sys/tracepoint.h> 99 100 #ifdef SYSVSHM 101 #include <sys/shm.h> 102 #endif 103 104 #include <uvm/uvm.h> 105 106 #ifdef DDB 107 #include <uvm/uvm_ddb.h> 108 #endif 109 110 #include <uvm/uvm_addr.h> 111 112 113 vsize_t uvmspace_dused(struct vm_map*, vaddr_t, vaddr_t); 114 int uvm_mapent_isjoinable(struct vm_map*, 115 struct vm_map_entry*, struct vm_map_entry*); 116 struct vm_map_entry *uvm_mapent_merge(struct vm_map*, struct vm_map_entry*, 117 struct vm_map_entry*, struct uvm_map_deadq*); 118 struct vm_map_entry *uvm_mapent_tryjoin(struct vm_map*, 119 struct vm_map_entry*, struct uvm_map_deadq*); 120 struct vm_map_entry *uvm_map_mkentry(struct vm_map*, struct vm_map_entry*, 121 struct vm_map_entry*, vaddr_t, vsize_t, int, 122 struct uvm_map_deadq*, struct vm_map_entry*); 123 struct vm_map_entry *uvm_mapent_alloc(struct vm_map*, int); 124 void uvm_mapent_free(struct vm_map_entry*); 125 void uvm_unmap_kill_entry(struct vm_map*, 126 struct vm_map_entry*); 127 void uvm_unmap_kill_entry_withlock(struct vm_map *, 128 struct vm_map_entry *, int); 129 void uvm_unmap_detach_intrsafe(struct uvm_map_deadq *); 130 void uvm_mapent_mkfree(struct vm_map*, 131 struct vm_map_entry*, struct vm_map_entry**, 132 struct uvm_map_deadq*, boolean_t); 133 void uvm_map_pageable_pgon(struct vm_map*, 134 struct vm_map_entry*, struct vm_map_entry*, 135 vaddr_t, vaddr_t); 136 int uvm_map_pageable_wire(struct vm_map*, 137 struct vm_map_entry*, struct vm_map_entry*, 138 vaddr_t, vaddr_t, int); 139 void uvm_map_setup_entries(struct vm_map*); 140 void uvm_map_setup_md(struct vm_map*); 141 void uvm_map_teardown(struct vm_map*); 142 void uvm_map_vmspace_update(struct vm_map*, 143 struct uvm_map_deadq*, int); 144 void uvm_map_kmem_grow(struct vm_map*, 145 struct uvm_map_deadq*, vsize_t, int); 146 void uvm_map_freelist_update_clear(struct vm_map*, 147 struct uvm_map_deadq*); 148 void uvm_map_freelist_update_refill(struct vm_map *, int); 149 void uvm_map_freelist_update(struct vm_map*, 150 struct uvm_map_deadq*, vaddr_t, vaddr_t, 151 vaddr_t, vaddr_t, int); 152 struct vm_map_entry *uvm_map_fix_space(struct vm_map*, struct vm_map_entry*, 153 vaddr_t, vaddr_t, int); 154 int uvm_map_findspace(struct vm_map*, 155 struct vm_map_entry**, struct vm_map_entry**, 156 vaddr_t*, vsize_t, vaddr_t, vaddr_t, vm_prot_t, 157 vaddr_t); 158 vsize_t uvm_map_addr_augment_get(struct vm_map_entry*); 159 void uvm_map_addr_augment(struct vm_map_entry*); 160 161 int uvm_map_inentry_recheck(u_long, vaddr_t, 162 struct p_inentry *); 163 boolean_t uvm_map_inentry_fix(struct proc *, struct p_inentry *, 164 vaddr_t, int (*)(vm_map_entry_t), u_long); 165 /* 166 * Tree management functions. 167 */ 168 169 static inline void uvm_mapent_copy(struct vm_map_entry*, 170 struct vm_map_entry*); 171 static inline int uvm_mapentry_addrcmp(const struct vm_map_entry*, 172 const struct vm_map_entry*); 173 void uvm_mapent_free_insert(struct vm_map*, 174 struct uvm_addr_state*, struct vm_map_entry*); 175 void uvm_mapent_free_remove(struct vm_map*, 176 struct uvm_addr_state*, struct vm_map_entry*); 177 void uvm_mapent_addr_insert(struct vm_map*, 178 struct vm_map_entry*); 179 void uvm_mapent_addr_remove(struct vm_map*, 180 struct vm_map_entry*); 181 void uvm_map_splitentry(struct vm_map*, 182 struct vm_map_entry*, struct vm_map_entry*, 183 vaddr_t); 184 vsize_t uvm_map_boundary(struct vm_map*, vaddr_t, vaddr_t); 185 186 /* 187 * uvm_vmspace_fork helper functions. 188 */ 189 struct vm_map_entry *uvm_mapent_clone(struct vm_map*, vaddr_t, vsize_t, 190 vsize_t, vm_prot_t, vm_prot_t, 191 struct vm_map_entry*, struct uvm_map_deadq*, int, 192 int); 193 struct vm_map_entry *uvm_mapent_share(struct vm_map*, vaddr_t, vsize_t, 194 vsize_t, vm_prot_t, vm_prot_t, struct vm_map*, 195 struct vm_map_entry*, struct uvm_map_deadq*); 196 struct vm_map_entry *uvm_mapent_forkshared(struct vmspace*, struct vm_map*, 197 struct vm_map*, struct vm_map_entry*, 198 struct uvm_map_deadq*); 199 struct vm_map_entry *uvm_mapent_forkcopy(struct vmspace*, struct vm_map*, 200 struct vm_map*, struct vm_map_entry*, 201 struct uvm_map_deadq*); 202 struct vm_map_entry *uvm_mapent_forkzero(struct vmspace*, struct vm_map*, 203 struct vm_map*, struct vm_map_entry*, 204 struct uvm_map_deadq*); 205 206 /* 207 * Tree validation. 208 */ 209 #ifdef VMMAP_DEBUG 210 void uvm_tree_assert(struct vm_map*, int, char*, 211 char*, int); 212 #define UVM_ASSERT(map, cond, file, line) \ 213 uvm_tree_assert((map), (cond), #cond, (file), (line)) 214 void uvm_tree_sanity(struct vm_map*, char*, int); 215 void uvm_tree_size_chk(struct vm_map*, char*, int); 216 void vmspace_validate(struct vm_map*); 217 #else 218 #define uvm_tree_sanity(_map, _file, _line) do {} while (0) 219 #define uvm_tree_size_chk(_map, _file, _line) do {} while (0) 220 #define vmspace_validate(_map) do {} while (0) 221 #endif 222 223 /* 224 * The kernel map will initially be VM_MAP_KSIZE_INIT bytes. 225 * Every time that gets cramped, we grow by at least VM_MAP_KSIZE_DELTA bytes. 226 * 227 * We attempt to grow by UVM_MAP_KSIZE_ALLOCMUL times the allocation size 228 * each time. 229 */ 230 #define VM_MAP_KSIZE_INIT (512 * (vaddr_t)PAGE_SIZE) 231 #define VM_MAP_KSIZE_DELTA (256 * (vaddr_t)PAGE_SIZE) 232 #define VM_MAP_KSIZE_ALLOCMUL 4 233 234 /* auto-allocate address lower bound */ 235 #define VMMAP_MIN_ADDR PAGE_SIZE 236 237 238 #ifdef DEADBEEF0 239 #define UVMMAP_DEADBEEF ((unsigned long)DEADBEEF0) 240 #else 241 #define UVMMAP_DEADBEEF ((unsigned long)0xdeadd0d0) 242 #endif 243 244 #ifdef DEBUG 245 int uvm_map_printlocks = 0; 246 247 #define LPRINTF(_args) \ 248 do { \ 249 if (uvm_map_printlocks) \ 250 printf _args; \ 251 } while (0) 252 #else 253 #define LPRINTF(_args) do {} while (0) 254 #endif 255 256 static struct mutex uvm_kmapent_mtx; 257 static struct timeval uvm_kmapent_last_warn_time; 258 static struct timeval uvm_kmapent_warn_rate = { 10, 0 }; 259 260 const char vmmapbsy[] = "vmmapbsy"; 261 262 /* 263 * pool for vmspace structures. 264 */ 265 struct pool uvm_vmspace_pool; 266 267 /* 268 * pool for dynamically-allocated map entries. 269 */ 270 struct pool uvm_map_entry_pool; 271 struct pool uvm_map_entry_kmem_pool; 272 273 /* 274 * This global represents the end of the kernel virtual address 275 * space. If we want to exceed this, we must grow the kernel 276 * virtual address space dynamically. 277 * 278 * Note, this variable is locked by kernel_map's lock. 279 */ 280 vaddr_t uvm_maxkaddr; 281 282 /* 283 * Locking predicate. 284 */ 285 #define UVM_MAP_REQ_WRITE(_map) \ 286 do { \ 287 if ((_map)->ref_count > 0) { \ 288 if (((_map)->flags & VM_MAP_INTRSAFE) == 0) \ 289 rw_assert_wrlock(&(_map)->lock); \ 290 else \ 291 MUTEX_ASSERT_LOCKED(&(_map)->mtx); \ 292 } \ 293 } while (0) 294 295 #define vm_map_modflags(map, set, clear) \ 296 do { \ 297 mtx_enter(&(map)->flags_lock); \ 298 (map)->flags = ((map)->flags | (set)) & ~(clear); \ 299 mtx_leave(&(map)->flags_lock); \ 300 } while (0) 301 302 303 /* 304 * Tree describing entries by address. 305 * 306 * Addresses are unique. 307 * Entries with start == end may only exist if they are the first entry 308 * (sorted by address) within a free-memory tree. 309 */ 310 311 static inline int 312 uvm_mapentry_addrcmp(const struct vm_map_entry *e1, 313 const struct vm_map_entry *e2) 314 { 315 return e1->start < e2->start ? -1 : e1->start > e2->start; 316 } 317 318 /* 319 * Copy mapentry. 320 */ 321 static inline void 322 uvm_mapent_copy(struct vm_map_entry *src, struct vm_map_entry *dst) 323 { 324 caddr_t csrc, cdst; 325 size_t sz; 326 327 csrc = (caddr_t)src; 328 cdst = (caddr_t)dst; 329 csrc += offsetof(struct vm_map_entry, uvm_map_entry_start_copy); 330 cdst += offsetof(struct vm_map_entry, uvm_map_entry_start_copy); 331 332 sz = offsetof(struct vm_map_entry, uvm_map_entry_stop_copy) - 333 offsetof(struct vm_map_entry, uvm_map_entry_start_copy); 334 memcpy(cdst, csrc, sz); 335 } 336 337 /* 338 * Handle free-list insertion. 339 */ 340 void 341 uvm_mapent_free_insert(struct vm_map *map, struct uvm_addr_state *uaddr, 342 struct vm_map_entry *entry) 343 { 344 const struct uvm_addr_functions *fun; 345 #ifdef VMMAP_DEBUG 346 vaddr_t min, max, bound; 347 #endif 348 349 #ifdef VMMAP_DEBUG 350 /* 351 * Boundary check. 352 * Boundaries are folded if they go on the same free list. 353 */ 354 min = VMMAP_FREE_START(entry); 355 max = VMMAP_FREE_END(entry); 356 357 while (min < max) { 358 bound = uvm_map_boundary(map, min, max); 359 KASSERT(uvm_map_uaddr(map, min) == uaddr); 360 min = bound; 361 } 362 #endif 363 KDASSERT((entry->fspace & (vaddr_t)PAGE_MASK) == 0); 364 KASSERT((entry->etype & UVM_ET_FREEMAPPED) == 0); 365 366 UVM_MAP_REQ_WRITE(map); 367 368 /* Actual insert: forward to uaddr pointer. */ 369 if (uaddr != NULL) { 370 fun = uaddr->uaddr_functions; 371 KDASSERT(fun != NULL); 372 if (fun->uaddr_free_insert != NULL) 373 (*fun->uaddr_free_insert)(map, uaddr, entry); 374 entry->etype |= UVM_ET_FREEMAPPED; 375 } 376 377 /* Update fspace augmentation. */ 378 uvm_map_addr_augment(entry); 379 } 380 381 /* 382 * Handle free-list removal. 383 */ 384 void 385 uvm_mapent_free_remove(struct vm_map *map, struct uvm_addr_state *uaddr, 386 struct vm_map_entry *entry) 387 { 388 const struct uvm_addr_functions *fun; 389 390 KASSERT((entry->etype & UVM_ET_FREEMAPPED) != 0 || uaddr == NULL); 391 KASSERT(uvm_map_uaddr_e(map, entry) == uaddr); 392 UVM_MAP_REQ_WRITE(map); 393 394 if (uaddr != NULL) { 395 fun = uaddr->uaddr_functions; 396 if (fun->uaddr_free_remove != NULL) 397 (*fun->uaddr_free_remove)(map, uaddr, entry); 398 entry->etype &= ~UVM_ET_FREEMAPPED; 399 } 400 } 401 402 /* 403 * Handle address tree insertion. 404 */ 405 void 406 uvm_mapent_addr_insert(struct vm_map *map, struct vm_map_entry *entry) 407 { 408 struct vm_map_entry *res; 409 410 if (!RBT_CHECK(uvm_map_addr, entry, UVMMAP_DEADBEEF)) 411 panic("uvm_mapent_addr_insert: entry still in addr list"); 412 KDASSERT(entry->start <= entry->end); 413 KDASSERT((entry->start & (vaddr_t)PAGE_MASK) == 0 && 414 (entry->end & (vaddr_t)PAGE_MASK) == 0); 415 416 TRACEPOINT(uvm, map_insert, 417 entry->start, entry->end, entry->protection, NULL); 418 419 UVM_MAP_REQ_WRITE(map); 420 res = RBT_INSERT(uvm_map_addr, &map->addr, entry); 421 if (res != NULL) { 422 panic("uvm_mapent_addr_insert: map %p entry %p " 423 "(0x%lx-0x%lx G=0x%lx F=0x%lx) insert collision " 424 "with entry %p (0x%lx-0x%lx G=0x%lx F=0x%lx)", 425 map, entry, 426 entry->start, entry->end, entry->guard, entry->fspace, 427 res, res->start, res->end, res->guard, res->fspace); 428 } 429 } 430 431 /* 432 * Handle address tree removal. 433 */ 434 void 435 uvm_mapent_addr_remove(struct vm_map *map, struct vm_map_entry *entry) 436 { 437 struct vm_map_entry *res; 438 439 TRACEPOINT(uvm, map_remove, 440 entry->start, entry->end, entry->protection, NULL); 441 442 UVM_MAP_REQ_WRITE(map); 443 res = RBT_REMOVE(uvm_map_addr, &map->addr, entry); 444 if (res != entry) 445 panic("uvm_mapent_addr_remove"); 446 RBT_POISON(uvm_map_addr, entry, UVMMAP_DEADBEEF); 447 } 448 449 /* 450 * uvm_map_reference: add reference to a map 451 * 452 * => map need not be locked 453 */ 454 void 455 uvm_map_reference(struct vm_map *map) 456 { 457 atomic_inc_int(&map->ref_count); 458 } 459 460 void 461 uvm_map_lock_entry(struct vm_map_entry *entry) 462 { 463 if (entry->aref.ar_amap != NULL) { 464 amap_lock(entry->aref.ar_amap); 465 } 466 if (UVM_ET_ISOBJ(entry)) { 467 rw_enter(entry->object.uvm_obj->vmobjlock, RW_WRITE); 468 } 469 } 470 471 void 472 uvm_map_unlock_entry(struct vm_map_entry *entry) 473 { 474 if (UVM_ET_ISOBJ(entry)) { 475 rw_exit(entry->object.uvm_obj->vmobjlock); 476 } 477 if (entry->aref.ar_amap != NULL) { 478 amap_unlock(entry->aref.ar_amap); 479 } 480 } 481 482 /* 483 * Calculate the dused delta. 484 */ 485 vsize_t 486 uvmspace_dused(struct vm_map *map, vaddr_t min, vaddr_t max) 487 { 488 struct vmspace *vm; 489 vsize_t sz; 490 vaddr_t lmax; 491 vaddr_t stack_begin, stack_end; /* Position of stack. */ 492 493 KASSERT(map->flags & VM_MAP_ISVMSPACE); 494 vm_map_assert_anylock(map); 495 496 vm = (struct vmspace *)map; 497 stack_begin = MIN((vaddr_t)vm->vm_maxsaddr, (vaddr_t)vm->vm_minsaddr); 498 stack_end = MAX((vaddr_t)vm->vm_maxsaddr, (vaddr_t)vm->vm_minsaddr); 499 500 sz = 0; 501 while (min != max) { 502 lmax = max; 503 if (min < stack_begin && lmax > stack_begin) 504 lmax = stack_begin; 505 else if (min < stack_end && lmax > stack_end) 506 lmax = stack_end; 507 508 if (min >= stack_begin && min < stack_end) { 509 /* nothing */ 510 } else 511 sz += lmax - min; 512 min = lmax; 513 } 514 515 return sz >> PAGE_SHIFT; 516 } 517 518 /* 519 * Find the entry describing the given address. 520 */ 521 struct vm_map_entry* 522 uvm_map_entrybyaddr(struct uvm_map_addr *atree, vaddr_t addr) 523 { 524 struct vm_map_entry *iter; 525 526 iter = RBT_ROOT(uvm_map_addr, atree); 527 while (iter != NULL) { 528 if (iter->start > addr) 529 iter = RBT_LEFT(uvm_map_addr, iter); 530 else if (VMMAP_FREE_END(iter) <= addr) 531 iter = RBT_RIGHT(uvm_map_addr, iter); 532 else 533 return iter; 534 } 535 return NULL; 536 } 537 538 /* 539 * DEAD_ENTRY_PUSH(struct vm_map_deadq *deadq, struct vm_map_entry *entry) 540 * 541 * Push dead entries into a linked list. 542 * Since the linked list abuses the address tree for storage, the entry 543 * may not be linked in a map. 544 * 545 * *head must be initialized to NULL before the first call to this macro. 546 * uvm_unmap_detach(*head, 0) will remove dead entries. 547 */ 548 static inline void 549 dead_entry_push(struct uvm_map_deadq *deadq, struct vm_map_entry *entry) 550 { 551 TAILQ_INSERT_TAIL(deadq, entry, dfree.deadq); 552 } 553 #define DEAD_ENTRY_PUSH(_headptr, _entry) \ 554 dead_entry_push((_headptr), (_entry)) 555 556 /* 557 * Test if memory starting at addr with sz bytes is free. 558 * 559 * Fills in *start_ptr and *end_ptr to be the first and last entry describing 560 * the space. 561 * If called with prefilled *start_ptr and *end_ptr, they are to be correct. 562 */ 563 int 564 uvm_map_isavail(struct vm_map *map, struct uvm_addr_state *uaddr, 565 struct vm_map_entry **start_ptr, struct vm_map_entry **end_ptr, 566 vaddr_t addr, vsize_t sz) 567 { 568 struct uvm_addr_state *free; 569 struct uvm_map_addr *atree; 570 struct vm_map_entry *i, *i_end; 571 572 if (addr + sz < addr) 573 return 0; 574 575 vm_map_assert_anylock(map); 576 577 /* 578 * Kernel memory above uvm_maxkaddr is considered unavailable. 579 */ 580 if ((map->flags & VM_MAP_ISVMSPACE) == 0) { 581 if (addr + sz > uvm_maxkaddr) 582 return 0; 583 } 584 585 atree = &map->addr; 586 587 /* 588 * Fill in first, last, so they point at the entries containing the 589 * first and last address of the range. 590 * Note that if they are not NULL, we don't perform the lookup. 591 */ 592 KDASSERT(atree != NULL && start_ptr != NULL && end_ptr != NULL); 593 if (*start_ptr == NULL) { 594 *start_ptr = uvm_map_entrybyaddr(atree, addr); 595 if (*start_ptr == NULL) 596 return 0; 597 } else 598 KASSERT(*start_ptr == uvm_map_entrybyaddr(atree, addr)); 599 if (*end_ptr == NULL) { 600 if (VMMAP_FREE_END(*start_ptr) >= addr + sz) 601 *end_ptr = *start_ptr; 602 else { 603 *end_ptr = uvm_map_entrybyaddr(atree, addr + sz - 1); 604 if (*end_ptr == NULL) 605 return 0; 606 } 607 } else 608 KASSERT(*end_ptr == uvm_map_entrybyaddr(atree, addr + sz - 1)); 609 610 /* Validation. */ 611 KDASSERT(*start_ptr != NULL && *end_ptr != NULL); 612 KDASSERT((*start_ptr)->start <= addr && 613 VMMAP_FREE_END(*start_ptr) > addr && 614 (*end_ptr)->start < addr + sz && 615 VMMAP_FREE_END(*end_ptr) >= addr + sz); 616 617 /* 618 * Check the none of the entries intersects with <addr, addr+sz>. 619 * Also, if the entry belong to uaddr_exe or uaddr_brk_stack, it is 620 * considered unavailable unless called by those allocators. 621 */ 622 i = *start_ptr; 623 i_end = RBT_NEXT(uvm_map_addr, *end_ptr); 624 for (; i != i_end; 625 i = RBT_NEXT(uvm_map_addr, i)) { 626 if (i->start != i->end && i->end > addr) 627 return 0; 628 629 /* 630 * uaddr_exe and uaddr_brk_stack may only be used 631 * by these allocators and the NULL uaddr (i.e. no 632 * uaddr). 633 * Reject if this requirement is not met. 634 */ 635 if (uaddr != NULL) { 636 free = uvm_map_uaddr_e(map, i); 637 638 if (uaddr != free && free != NULL && 639 (free == map->uaddr_exe || 640 free == map->uaddr_brk_stack)) 641 return 0; 642 } 643 } 644 645 return -1; 646 } 647 648 /* 649 * Invoke each address selector until an address is found. 650 * Will not invoke uaddr_exe. 651 */ 652 int 653 uvm_map_findspace(struct vm_map *map, struct vm_map_entry**first, 654 struct vm_map_entry**last, vaddr_t *addr, vsize_t sz, 655 vaddr_t pmap_align, vaddr_t pmap_offset, vm_prot_t prot, vaddr_t hint) 656 { 657 struct uvm_addr_state *uaddr; 658 int i; 659 660 /* 661 * Allocation for sz bytes at any address, 662 * using the addr selectors in order. 663 */ 664 for (i = 0; i < nitems(map->uaddr_any); i++) { 665 uaddr = map->uaddr_any[i]; 666 667 if (uvm_addr_invoke(map, uaddr, first, last, 668 addr, sz, pmap_align, pmap_offset, prot, hint) == 0) 669 return 0; 670 } 671 672 /* Fall back to brk() and stack() address selectors. */ 673 uaddr = map->uaddr_brk_stack; 674 if (uvm_addr_invoke(map, uaddr, first, last, 675 addr, sz, pmap_align, pmap_offset, prot, hint) == 0) 676 return 0; 677 678 return ENOMEM; 679 } 680 681 /* Calculate entry augmentation value. */ 682 vsize_t 683 uvm_map_addr_augment_get(struct vm_map_entry *entry) 684 { 685 vsize_t augment; 686 struct vm_map_entry *left, *right; 687 688 augment = entry->fspace; 689 if ((left = RBT_LEFT(uvm_map_addr, entry)) != NULL) 690 augment = MAX(augment, left->fspace_augment); 691 if ((right = RBT_RIGHT(uvm_map_addr, entry)) != NULL) 692 augment = MAX(augment, right->fspace_augment); 693 return augment; 694 } 695 696 /* 697 * Update augmentation data in entry. 698 */ 699 void 700 uvm_map_addr_augment(struct vm_map_entry *entry) 701 { 702 vsize_t augment; 703 704 while (entry != NULL) { 705 /* Calculate value for augmentation. */ 706 augment = uvm_map_addr_augment_get(entry); 707 708 /* 709 * Descend update. 710 * Once we find an entry that already has the correct value, 711 * stop, since it means all its parents will use the correct 712 * value too. 713 */ 714 if (entry->fspace_augment == augment) 715 return; 716 entry->fspace_augment = augment; 717 entry = RBT_PARENT(uvm_map_addr, entry); 718 } 719 } 720 721 /* 722 * uvm_mapanon: establish a valid mapping in map for an anon 723 * 724 * => *addr and sz must be a multiple of PAGE_SIZE. 725 * => *addr is ignored, except if flags contains UVM_FLAG_FIXED. 726 * => map must be unlocked. 727 * 728 * => align: align vaddr, must be a power-of-2. 729 * Align is only a hint and will be ignored if the alignment fails. 730 */ 731 int 732 uvm_mapanon(struct vm_map *map, vaddr_t *addr, vsize_t sz, 733 vsize_t align, unsigned int flags) 734 { 735 struct vm_map_entry *first, *last, *entry, *new; 736 struct uvm_map_deadq dead; 737 vm_prot_t prot; 738 vm_prot_t maxprot; 739 vm_inherit_t inherit; 740 int advice; 741 int error; 742 vaddr_t pmap_align, pmap_offset; 743 vaddr_t hint; 744 745 KASSERT((map->flags & VM_MAP_ISVMSPACE) == VM_MAP_ISVMSPACE); 746 KASSERT(map != kernel_map); 747 KASSERT((map->flags & UVM_FLAG_HOLE) == 0); 748 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0); 749 splassert(IPL_NONE); 750 KASSERT((flags & UVM_FLAG_TRYLOCK) == 0); 751 752 /* 753 * We use pmap_align and pmap_offset as alignment and offset variables. 754 * 755 * Because the align parameter takes precedence over pmap prefer, 756 * the pmap_align will need to be set to align, with pmap_offset = 0, 757 * if pmap_prefer will not align. 758 */ 759 pmap_align = MAX(align, PAGE_SIZE); 760 pmap_offset = 0; 761 762 /* Decode parameters. */ 763 prot = UVM_PROTECTION(flags); 764 maxprot = UVM_MAXPROTECTION(flags); 765 advice = UVM_ADVICE(flags); 766 inherit = UVM_INHERIT(flags); 767 error = 0; 768 hint = trunc_page(*addr); 769 TAILQ_INIT(&dead); 770 KASSERT((sz & (vaddr_t)PAGE_MASK) == 0); 771 KASSERT((align & (align - 1)) == 0); 772 773 /* Check protection. */ 774 if ((prot & maxprot) != prot) 775 return EACCES; 776 777 /* 778 * Before grabbing the lock, allocate a map entry for later 779 * use to ensure we don't wait for memory while holding the 780 * vm_map_lock. 781 */ 782 new = uvm_mapent_alloc(map, flags); 783 if (new == NULL) 784 return ENOMEM; 785 786 vm_map_lock(map); 787 first = last = NULL; 788 if (flags & UVM_FLAG_FIXED) { 789 /* 790 * Fixed location. 791 * 792 * Note: we ignore align, pmap_prefer. 793 * Fill in first, last and *addr. 794 */ 795 KASSERT((*addr & PAGE_MASK) == 0); 796 797 /* Check that the space is available. */ 798 if (flags & UVM_FLAG_UNMAP) { 799 if ((flags & UVM_FLAG_STACK) && 800 !uvm_map_is_stack_remappable(map, *addr, sz, 801 (flags & UVM_FLAG_SIGALTSTACK))) { 802 error = EINVAL; 803 goto unlock; 804 } 805 if (uvm_unmap_remove(map, *addr, *addr + sz, &dead, 806 FALSE, TRUE, 807 (flags & UVM_FLAG_SIGALTSTACK) ? FALSE : TRUE) != 0) { 808 error = EPERM; /* immutable entries found */ 809 goto unlock; 810 } 811 } 812 if (!uvm_map_isavail(map, NULL, &first, &last, *addr, sz)) { 813 error = ENOMEM; 814 goto unlock; 815 } 816 } else if (*addr != 0 && (*addr & PAGE_MASK) == 0 && 817 (align == 0 || (*addr & (align - 1)) == 0) && 818 uvm_map_isavail(map, NULL, &first, &last, *addr, sz)) { 819 /* 820 * Address used as hint. 821 * 822 * Note: we enforce the alignment restriction, 823 * but ignore pmap_prefer. 824 */ 825 } else if ((prot & PROT_EXEC) != 0 && map->uaddr_exe != NULL) { 826 /* Run selection algorithm for executables. */ 827 error = uvm_addr_invoke(map, map->uaddr_exe, &first, &last, 828 addr, sz, pmap_align, pmap_offset, prot, hint); 829 830 if (error != 0) 831 goto unlock; 832 } else { 833 /* Update freelists from vmspace. */ 834 uvm_map_vmspace_update(map, &dead, flags); 835 836 error = uvm_map_findspace(map, &first, &last, addr, sz, 837 pmap_align, pmap_offset, prot, hint); 838 839 if (error != 0) 840 goto unlock; 841 } 842 843 /* Double-check if selected address doesn't cause overflow. */ 844 if (*addr + sz < *addr) { 845 error = ENOMEM; 846 goto unlock; 847 } 848 849 /* If we only want a query, return now. */ 850 if (flags & UVM_FLAG_QUERY) { 851 error = 0; 852 goto unlock; 853 } 854 855 /* 856 * Create new entry. 857 * first and last may be invalidated after this call. 858 */ 859 entry = uvm_map_mkentry(map, first, last, *addr, sz, flags, &dead, 860 new); 861 if (entry == NULL) { 862 error = ENOMEM; 863 goto unlock; 864 } 865 new = NULL; 866 KDASSERT(entry->start == *addr && entry->end == *addr + sz); 867 entry->object.uvm_obj = NULL; 868 entry->offset = 0; 869 entry->protection = prot; 870 entry->max_protection = maxprot; 871 entry->inheritance = inherit; 872 entry->wired_count = 0; 873 entry->advice = advice; 874 if (prot & PROT_WRITE) 875 map->wserial++; 876 if (flags & UVM_FLAG_SYSCALL) { 877 entry->etype |= UVM_ET_SYSCALL; 878 map->wserial++; 879 } 880 if (flags & UVM_FLAG_STACK) { 881 entry->etype |= UVM_ET_STACK; 882 if (flags & (UVM_FLAG_FIXED | UVM_FLAG_UNMAP)) 883 map->sserial++; 884 } 885 if (flags & UVM_FLAG_COPYONW) { 886 entry->etype |= UVM_ET_COPYONWRITE; 887 if ((flags & UVM_FLAG_OVERLAY) == 0) 888 entry->etype |= UVM_ET_NEEDSCOPY; 889 } 890 if (flags & UVM_FLAG_CONCEAL) 891 entry->etype |= UVM_ET_CONCEAL; 892 if (flags & UVM_FLAG_OVERLAY) { 893 entry->aref.ar_pageoff = 0; 894 entry->aref.ar_amap = amap_alloc(sz, M_WAITOK, 0); 895 } 896 897 /* Update map and process statistics. */ 898 map->size += sz; 899 if (prot != PROT_NONE) { 900 ((struct vmspace *)map)->vm_dused += 901 uvmspace_dused(map, *addr, *addr + sz); 902 } 903 904 unlock: 905 vm_map_unlock(map); 906 907 /* 908 * Remove dead entries. 909 * 910 * Dead entries may be the result of merging. 911 * uvm_map_mkentry may also create dead entries, when it attempts to 912 * destroy free-space entries. 913 */ 914 uvm_unmap_detach(&dead, 0); 915 916 if (new) 917 uvm_mapent_free(new); 918 return error; 919 } 920 921 /* 922 * uvm_map: establish a valid mapping in map 923 * 924 * => *addr and sz must be a multiple of PAGE_SIZE. 925 * => map must be unlocked. 926 * => <uobj,uoffset> value meanings (4 cases): 927 * [1] <NULL,uoffset> == uoffset is a hint for PMAP_PREFER 928 * [2] <NULL,UVM_UNKNOWN_OFFSET> == don't PMAP_PREFER 929 * [3] <uobj,uoffset> == normal mapping 930 * [4] <uobj,UVM_UNKNOWN_OFFSET> == uvm_map finds offset based on VA 931 * 932 * case [4] is for kernel mappings where we don't know the offset until 933 * we've found a virtual address. note that kernel object offsets are 934 * always relative to vm_map_min(kernel_map). 935 * 936 * => align: align vaddr, must be a power-of-2. 937 * Align is only a hint and will be ignored if the alignment fails. 938 */ 939 int 940 uvm_map(struct vm_map *map, vaddr_t *addr, vsize_t sz, 941 struct uvm_object *uobj, voff_t uoffset, 942 vsize_t align, unsigned int flags) 943 { 944 struct vm_map_entry *first, *last, *entry, *new; 945 struct uvm_map_deadq dead; 946 vm_prot_t prot; 947 vm_prot_t maxprot; 948 vm_inherit_t inherit; 949 int advice; 950 int error; 951 vaddr_t pmap_align, pmap_offset; 952 vaddr_t hint; 953 954 if ((map->flags & VM_MAP_INTRSAFE) == 0) 955 splassert(IPL_NONE); 956 else 957 splassert(IPL_VM); 958 959 /* 960 * We use pmap_align and pmap_offset as alignment and offset variables. 961 * 962 * Because the align parameter takes precedence over pmap prefer, 963 * the pmap_align will need to be set to align, with pmap_offset = 0, 964 * if pmap_prefer will not align. 965 */ 966 if (uoffset == UVM_UNKNOWN_OFFSET) { 967 pmap_align = MAX(align, PAGE_SIZE); 968 pmap_offset = 0; 969 } else { 970 pmap_align = MAX(PMAP_PREFER_ALIGN(), PAGE_SIZE); 971 pmap_offset = PMAP_PREFER_OFFSET(uoffset); 972 973 if (align == 0 || 974 (align <= pmap_align && (pmap_offset & (align - 1)) == 0)) { 975 /* pmap_offset satisfies align, no change. */ 976 } else { 977 /* Align takes precedence over pmap prefer. */ 978 pmap_align = align; 979 pmap_offset = 0; 980 } 981 } 982 983 /* Decode parameters. */ 984 prot = UVM_PROTECTION(flags); 985 maxprot = UVM_MAXPROTECTION(flags); 986 advice = UVM_ADVICE(flags); 987 inherit = UVM_INHERIT(flags); 988 error = 0; 989 hint = trunc_page(*addr); 990 TAILQ_INIT(&dead); 991 KASSERT((sz & (vaddr_t)PAGE_MASK) == 0); 992 KASSERT((align & (align - 1)) == 0); 993 994 /* Holes are incompatible with other types of mappings. */ 995 if (flags & UVM_FLAG_HOLE) { 996 KASSERT(uobj == NULL && (flags & UVM_FLAG_FIXED) && 997 (flags & (UVM_FLAG_OVERLAY | UVM_FLAG_COPYONW)) == 0); 998 } 999 1000 /* Unset hint for kernel_map non-fixed allocations. */ 1001 if (!(map->flags & VM_MAP_ISVMSPACE) && !(flags & UVM_FLAG_FIXED)) 1002 hint = 0; 1003 1004 /* Check protection. */ 1005 if ((prot & maxprot) != prot) 1006 return EACCES; 1007 1008 if (map == kernel_map && 1009 (prot & (PROT_WRITE | PROT_EXEC)) == (PROT_WRITE | PROT_EXEC)) 1010 panic("uvm_map: kernel map W^X violation requested"); 1011 1012 /* 1013 * Before grabbing the lock, allocate a map entry for later 1014 * use to ensure we don't wait for memory while holding the 1015 * vm_map_lock. 1016 */ 1017 new = uvm_mapent_alloc(map, flags); 1018 if (new == NULL) 1019 return ENOMEM; 1020 1021 if (flags & UVM_FLAG_TRYLOCK) { 1022 if (vm_map_lock_try(map) == FALSE) { 1023 error = EFAULT; 1024 goto out; 1025 } 1026 } else { 1027 vm_map_lock(map); 1028 } 1029 1030 first = last = NULL; 1031 if (flags & UVM_FLAG_FIXED) { 1032 /* 1033 * Fixed location. 1034 * 1035 * Note: we ignore align, pmap_prefer. 1036 * Fill in first, last and *addr. 1037 */ 1038 KASSERT((*addr & PAGE_MASK) == 0); 1039 1040 /* 1041 * Grow pmap to include allocated address. 1042 * If the growth fails, the allocation will fail too. 1043 */ 1044 if ((map->flags & VM_MAP_ISVMSPACE) == 0 && 1045 uvm_maxkaddr < (*addr + sz)) { 1046 uvm_map_kmem_grow(map, &dead, 1047 *addr + sz - uvm_maxkaddr, flags); 1048 } 1049 1050 /* Check that the space is available. */ 1051 if (flags & UVM_FLAG_UNMAP) { 1052 if (uvm_unmap_remove(map, *addr, *addr + sz, &dead, 1053 FALSE, TRUE, TRUE) != 0) { 1054 error = EPERM; /* immutable entries found */ 1055 goto unlock; 1056 } 1057 } 1058 if (!uvm_map_isavail(map, NULL, &first, &last, *addr, sz)) { 1059 error = ENOMEM; 1060 goto unlock; 1061 } 1062 } else if (*addr != 0 && (*addr & PAGE_MASK) == 0 && 1063 (map->flags & VM_MAP_ISVMSPACE) == VM_MAP_ISVMSPACE && 1064 (align == 0 || (*addr & (align - 1)) == 0) && 1065 uvm_map_isavail(map, NULL, &first, &last, *addr, sz)) { 1066 /* 1067 * Address used as hint. 1068 * 1069 * Note: we enforce the alignment restriction, 1070 * but ignore pmap_prefer. 1071 */ 1072 } else if ((prot & PROT_EXEC) != 0 && map->uaddr_exe != NULL) { 1073 /* Run selection algorithm for executables. */ 1074 error = uvm_addr_invoke(map, map->uaddr_exe, &first, &last, 1075 addr, sz, pmap_align, pmap_offset, prot, hint); 1076 1077 /* Grow kernel memory and try again. */ 1078 if (error != 0 && (map->flags & VM_MAP_ISVMSPACE) == 0) { 1079 uvm_map_kmem_grow(map, &dead, sz, flags); 1080 1081 error = uvm_addr_invoke(map, map->uaddr_exe, 1082 &first, &last, addr, sz, 1083 pmap_align, pmap_offset, prot, hint); 1084 } 1085 1086 if (error != 0) 1087 goto unlock; 1088 } else { 1089 /* Update freelists from vmspace. */ 1090 if (map->flags & VM_MAP_ISVMSPACE) 1091 uvm_map_vmspace_update(map, &dead, flags); 1092 1093 error = uvm_map_findspace(map, &first, &last, addr, sz, 1094 pmap_align, pmap_offset, prot, hint); 1095 1096 /* Grow kernel memory and try again. */ 1097 if (error != 0 && (map->flags & VM_MAP_ISVMSPACE) == 0) { 1098 uvm_map_kmem_grow(map, &dead, sz, flags); 1099 1100 error = uvm_map_findspace(map, &first, &last, addr, sz, 1101 pmap_align, pmap_offset, prot, hint); 1102 } 1103 1104 if (error != 0) 1105 goto unlock; 1106 } 1107 1108 /* Double-check if selected address doesn't cause overflow. */ 1109 if (*addr + sz < *addr) { 1110 error = ENOMEM; 1111 goto unlock; 1112 } 1113 1114 KASSERT((map->flags & VM_MAP_ISVMSPACE) == VM_MAP_ISVMSPACE || 1115 uvm_maxkaddr >= *addr + sz); 1116 1117 /* If we only want a query, return now. */ 1118 if (flags & UVM_FLAG_QUERY) { 1119 error = 0; 1120 goto unlock; 1121 } 1122 1123 if (uobj == NULL) 1124 uoffset = 0; 1125 else if (uoffset == UVM_UNKNOWN_OFFSET) { 1126 KASSERT(UVM_OBJ_IS_KERN_OBJECT(uobj)); 1127 uoffset = *addr - vm_map_min(kernel_map); 1128 } 1129 1130 /* 1131 * Create new entry. 1132 * first and last may be invalidated after this call. 1133 */ 1134 entry = uvm_map_mkentry(map, first, last, *addr, sz, flags, &dead, 1135 new); 1136 if (entry == NULL) { 1137 error = ENOMEM; 1138 goto unlock; 1139 } 1140 new = NULL; 1141 KDASSERT(entry->start == *addr && entry->end == *addr + sz); 1142 entry->object.uvm_obj = uobj; 1143 entry->offset = uoffset; 1144 entry->protection = prot; 1145 entry->max_protection = maxprot; 1146 entry->inheritance = inherit; 1147 entry->wired_count = 0; 1148 entry->advice = advice; 1149 if (prot & PROT_WRITE) 1150 map->wserial++; 1151 if (flags & UVM_FLAG_SYSCALL) { 1152 entry->etype |= UVM_ET_SYSCALL; 1153 map->wserial++; 1154 } 1155 if (flags & UVM_FLAG_STACK) { 1156 entry->etype |= UVM_ET_STACK; 1157 if (flags & UVM_FLAG_UNMAP) 1158 map->sserial++; 1159 } 1160 if (uobj) 1161 entry->etype |= UVM_ET_OBJ; 1162 else if (flags & UVM_FLAG_HOLE) 1163 entry->etype |= UVM_ET_HOLE; 1164 if (flags & UVM_FLAG_NOFAULT) 1165 entry->etype |= UVM_ET_NOFAULT; 1166 if (flags & UVM_FLAG_WC) 1167 entry->etype |= UVM_ET_WC; 1168 if (flags & UVM_FLAG_COPYONW) { 1169 entry->etype |= UVM_ET_COPYONWRITE; 1170 if ((flags & UVM_FLAG_OVERLAY) == 0) 1171 entry->etype |= UVM_ET_NEEDSCOPY; 1172 } 1173 if (flags & UVM_FLAG_CONCEAL) 1174 entry->etype |= UVM_ET_CONCEAL; 1175 if (flags & UVM_FLAG_OVERLAY) { 1176 entry->aref.ar_pageoff = 0; 1177 entry->aref.ar_amap = amap_alloc(sz, M_WAITOK, 0); 1178 } 1179 1180 /* Update map and process statistics. */ 1181 if (!(flags & UVM_FLAG_HOLE)) { 1182 map->size += sz; 1183 if ((map->flags & VM_MAP_ISVMSPACE) && uobj == NULL && 1184 prot != PROT_NONE) { 1185 ((struct vmspace *)map)->vm_dused += 1186 uvmspace_dused(map, *addr, *addr + sz); 1187 } 1188 } 1189 1190 /* 1191 * Try to merge entry. 1192 * 1193 * Userland allocations are kept separated most of the time. 1194 * Forego the effort of merging what most of the time can't be merged 1195 * and only try the merge if it concerns a kernel entry. 1196 */ 1197 if ((flags & UVM_FLAG_NOMERGE) == 0 && 1198 (map->flags & VM_MAP_ISVMSPACE) == 0) 1199 uvm_mapent_tryjoin(map, entry, &dead); 1200 1201 unlock: 1202 vm_map_unlock(map); 1203 1204 /* 1205 * Remove dead entries. 1206 * 1207 * Dead entries may be the result of merging. 1208 * uvm_map_mkentry may also create dead entries, when it attempts to 1209 * destroy free-space entries. 1210 */ 1211 if (map->flags & VM_MAP_INTRSAFE) 1212 uvm_unmap_detach_intrsafe(&dead); 1213 else 1214 uvm_unmap_detach(&dead, 0); 1215 out: 1216 if (new) 1217 uvm_mapent_free(new); 1218 return error; 1219 } 1220 1221 /* 1222 * True iff e1 and e2 can be joined together. 1223 */ 1224 int 1225 uvm_mapent_isjoinable(struct vm_map *map, struct vm_map_entry *e1, 1226 struct vm_map_entry *e2) 1227 { 1228 KDASSERT(e1 != NULL && e2 != NULL); 1229 1230 /* Must be the same entry type and not have free memory between. */ 1231 if (e1->etype != e2->etype || e1->end != e2->start) 1232 return 0; 1233 1234 /* Submaps are never joined. */ 1235 if (UVM_ET_ISSUBMAP(e1)) 1236 return 0; 1237 1238 /* Never merge wired memory. */ 1239 if (VM_MAPENT_ISWIRED(e1) || VM_MAPENT_ISWIRED(e2)) 1240 return 0; 1241 1242 /* Protection, inheritance and advice must be equal. */ 1243 if (e1->protection != e2->protection || 1244 e1->max_protection != e2->max_protection || 1245 e1->inheritance != e2->inheritance || 1246 e1->advice != e2->advice) 1247 return 0; 1248 1249 /* If uvm_object: object itself and offsets within object must match. */ 1250 if (UVM_ET_ISOBJ(e1)) { 1251 if (e1->object.uvm_obj != e2->object.uvm_obj) 1252 return 0; 1253 if (e1->offset + (e1->end - e1->start) != e2->offset) 1254 return 0; 1255 } 1256 1257 /* 1258 * Cannot join shared amaps. 1259 * Note: no need to lock amap to look at refs, since we don't care 1260 * about its exact value. 1261 * If it is 1 (i.e. we have the only reference) it will stay there. 1262 */ 1263 if (e1->aref.ar_amap && amap_refs(e1->aref.ar_amap) != 1) 1264 return 0; 1265 if (e2->aref.ar_amap && amap_refs(e2->aref.ar_amap) != 1) 1266 return 0; 1267 1268 /* Apparently, e1 and e2 match. */ 1269 return 1; 1270 } 1271 1272 /* 1273 * Join support function. 1274 * 1275 * Returns the merged entry on success. 1276 * Returns NULL if the merge failed. 1277 */ 1278 struct vm_map_entry* 1279 uvm_mapent_merge(struct vm_map *map, struct vm_map_entry *e1, 1280 struct vm_map_entry *e2, struct uvm_map_deadq *dead) 1281 { 1282 struct uvm_addr_state *free; 1283 1284 /* 1285 * Merging is not supported for map entries that 1286 * contain an amap in e1. This should never happen 1287 * anyway, because only kernel entries are merged. 1288 * These do not contain amaps. 1289 * e2 contains no real information in its amap, 1290 * so it can be erased immediately. 1291 */ 1292 KASSERT(e1->aref.ar_amap == NULL); 1293 1294 /* 1295 * Don't drop obj reference: 1296 * uvm_unmap_detach will do this for us. 1297 */ 1298 free = uvm_map_uaddr_e(map, e1); 1299 uvm_mapent_free_remove(map, free, e1); 1300 1301 free = uvm_map_uaddr_e(map, e2); 1302 uvm_mapent_free_remove(map, free, e2); 1303 uvm_mapent_addr_remove(map, e2); 1304 e1->end = e2->end; 1305 e1->guard = e2->guard; 1306 e1->fspace = e2->fspace; 1307 uvm_mapent_free_insert(map, free, e1); 1308 1309 DEAD_ENTRY_PUSH(dead, e2); 1310 return e1; 1311 } 1312 1313 /* 1314 * Attempt forward and backward joining of entry. 1315 * 1316 * Returns entry after joins. 1317 * We are guaranteed that the amap of entry is either non-existent or 1318 * has never been used. 1319 */ 1320 struct vm_map_entry* 1321 uvm_mapent_tryjoin(struct vm_map *map, struct vm_map_entry *entry, 1322 struct uvm_map_deadq *dead) 1323 { 1324 struct vm_map_entry *other; 1325 struct vm_map_entry *merged; 1326 1327 /* Merge with previous entry. */ 1328 other = RBT_PREV(uvm_map_addr, entry); 1329 if (other && uvm_mapent_isjoinable(map, other, entry)) { 1330 merged = uvm_mapent_merge(map, other, entry, dead); 1331 if (merged) 1332 entry = merged; 1333 } 1334 1335 /* 1336 * Merge with next entry. 1337 * 1338 * Because amap can only extend forward and the next entry 1339 * probably contains sensible info, only perform forward merging 1340 * in the absence of an amap. 1341 */ 1342 other = RBT_NEXT(uvm_map_addr, entry); 1343 if (other && entry->aref.ar_amap == NULL && 1344 other->aref.ar_amap == NULL && 1345 uvm_mapent_isjoinable(map, entry, other)) { 1346 merged = uvm_mapent_merge(map, entry, other, dead); 1347 if (merged) 1348 entry = merged; 1349 } 1350 1351 return entry; 1352 } 1353 1354 /* 1355 * Kill entries that are no longer in a map. 1356 */ 1357 void 1358 uvm_unmap_detach(struct uvm_map_deadq *deadq, int flags) 1359 { 1360 struct vm_map_entry *entry, *tmp; 1361 int waitok = flags & UVM_PLA_WAITOK; 1362 1363 TAILQ_FOREACH_SAFE(entry, deadq, dfree.deadq, tmp) { 1364 /* Drop reference to amap, if we've got one. */ 1365 if (entry->aref.ar_amap) 1366 amap_unref(entry->aref.ar_amap, 1367 entry->aref.ar_pageoff, 1368 atop(entry->end - entry->start), 1369 flags & AMAP_REFALL); 1370 1371 /* Skip entries for which we have to grab the kernel lock. */ 1372 if (UVM_ET_ISSUBMAP(entry) || UVM_ET_ISOBJ(entry)) 1373 continue; 1374 1375 TAILQ_REMOVE(deadq, entry, dfree.deadq); 1376 uvm_mapent_free(entry); 1377 } 1378 1379 if (TAILQ_EMPTY(deadq)) 1380 return; 1381 1382 KERNEL_LOCK(); 1383 while ((entry = TAILQ_FIRST(deadq)) != NULL) { 1384 if (waitok) 1385 uvm_pause(); 1386 /* Drop reference to our backing object, if we've got one. */ 1387 if (UVM_ET_ISSUBMAP(entry)) { 1388 /* ... unlikely to happen, but play it safe */ 1389 uvm_map_deallocate(entry->object.sub_map); 1390 } else if (UVM_ET_ISOBJ(entry) && 1391 entry->object.uvm_obj->pgops->pgo_detach) { 1392 entry->object.uvm_obj->pgops->pgo_detach( 1393 entry->object.uvm_obj); 1394 } 1395 1396 /* Step to next. */ 1397 TAILQ_REMOVE(deadq, entry, dfree.deadq); 1398 uvm_mapent_free(entry); 1399 } 1400 KERNEL_UNLOCK(); 1401 } 1402 1403 void 1404 uvm_unmap_detach_intrsafe(struct uvm_map_deadq *deadq) 1405 { 1406 struct vm_map_entry *entry; 1407 1408 while ((entry = TAILQ_FIRST(deadq)) != NULL) { 1409 KASSERT(entry->aref.ar_amap == NULL); 1410 KASSERT(!UVM_ET_ISSUBMAP(entry)); 1411 KASSERT(!UVM_ET_ISOBJ(entry)); 1412 TAILQ_REMOVE(deadq, entry, dfree.deadq); 1413 uvm_mapent_free(entry); 1414 } 1415 } 1416 1417 /* 1418 * Create and insert new entry. 1419 * 1420 * Returned entry contains new addresses and is inserted properly in the tree. 1421 * first and last are (probably) no longer valid. 1422 */ 1423 struct vm_map_entry* 1424 uvm_map_mkentry(struct vm_map *map, struct vm_map_entry *first, 1425 struct vm_map_entry *last, vaddr_t addr, vsize_t sz, int flags, 1426 struct uvm_map_deadq *dead, struct vm_map_entry *new) 1427 { 1428 struct vm_map_entry *entry, *prev; 1429 struct uvm_addr_state *free; 1430 vaddr_t min, max; /* free space boundaries for new entry */ 1431 1432 KDASSERT(map != NULL); 1433 KDASSERT(first != NULL); 1434 KDASSERT(last != NULL); 1435 KDASSERT(dead != NULL); 1436 KDASSERT(sz > 0); 1437 KDASSERT(addr + sz > addr); 1438 KDASSERT(first->end <= addr && VMMAP_FREE_END(first) > addr); 1439 KDASSERT(last->start < addr + sz && VMMAP_FREE_END(last) >= addr + sz); 1440 KDASSERT(uvm_map_isavail(map, NULL, &first, &last, addr, sz)); 1441 uvm_tree_sanity(map, __FILE__, __LINE__); 1442 1443 min = addr + sz; 1444 max = VMMAP_FREE_END(last); 1445 1446 /* Initialize new entry. */ 1447 if (new == NULL) 1448 entry = uvm_mapent_alloc(map, flags); 1449 else 1450 entry = new; 1451 if (entry == NULL) 1452 return NULL; 1453 entry->offset = 0; 1454 entry->etype = 0; 1455 entry->wired_count = 0; 1456 entry->aref.ar_pageoff = 0; 1457 entry->aref.ar_amap = NULL; 1458 1459 entry->start = addr; 1460 entry->end = min; 1461 entry->guard = 0; 1462 entry->fspace = 0; 1463 1464 vm_map_assert_wrlock(map); 1465 1466 /* Reset free space in first. */ 1467 free = uvm_map_uaddr_e(map, first); 1468 uvm_mapent_free_remove(map, free, first); 1469 first->guard = 0; 1470 first->fspace = 0; 1471 1472 /* 1473 * Remove all entries that are fully replaced. 1474 * We are iterating using last in reverse order. 1475 */ 1476 for (; first != last; last = prev) { 1477 prev = RBT_PREV(uvm_map_addr, last); 1478 1479 KDASSERT(last->start == last->end); 1480 free = uvm_map_uaddr_e(map, last); 1481 uvm_mapent_free_remove(map, free, last); 1482 uvm_mapent_addr_remove(map, last); 1483 DEAD_ENTRY_PUSH(dead, last); 1484 } 1485 /* Remove first if it is entirely inside <addr, addr+sz>. */ 1486 if (first->start == addr) { 1487 uvm_mapent_addr_remove(map, first); 1488 DEAD_ENTRY_PUSH(dead, first); 1489 } else { 1490 uvm_map_fix_space(map, first, VMMAP_FREE_START(first), 1491 addr, flags); 1492 } 1493 1494 /* Finally, link in entry. */ 1495 uvm_mapent_addr_insert(map, entry); 1496 uvm_map_fix_space(map, entry, min, max, flags); 1497 1498 uvm_tree_sanity(map, __FILE__, __LINE__); 1499 return entry; 1500 } 1501 1502 1503 /* 1504 * uvm_mapent_alloc: allocate a map entry 1505 */ 1506 struct vm_map_entry * 1507 uvm_mapent_alloc(struct vm_map *map, int flags) 1508 { 1509 struct vm_map_entry *me, *ne; 1510 int pool_flags; 1511 int i; 1512 1513 pool_flags = PR_WAITOK; 1514 if (flags & UVM_FLAG_TRYLOCK) 1515 pool_flags = PR_NOWAIT; 1516 1517 if (map->flags & VM_MAP_INTRSAFE || cold) { 1518 mtx_enter(&uvm_kmapent_mtx); 1519 if (SLIST_EMPTY(&uvm.kentry_free)) { 1520 ne = km_alloc(PAGE_SIZE, &kv_page, &kp_dirty, 1521 &kd_nowait); 1522 if (ne == NULL) 1523 panic("uvm_mapent_alloc: cannot allocate map " 1524 "entry"); 1525 for (i = 0; i < PAGE_SIZE / sizeof(*ne); i++) { 1526 SLIST_INSERT_HEAD(&uvm.kentry_free, 1527 &ne[i], daddrs.addr_kentry); 1528 } 1529 if (ratecheck(&uvm_kmapent_last_warn_time, 1530 &uvm_kmapent_warn_rate)) 1531 printf("uvm_mapent_alloc: out of static " 1532 "map entries\n"); 1533 } 1534 me = SLIST_FIRST(&uvm.kentry_free); 1535 SLIST_REMOVE_HEAD(&uvm.kentry_free, daddrs.addr_kentry); 1536 uvmexp.kmapent++; 1537 mtx_leave(&uvm_kmapent_mtx); 1538 me->flags = UVM_MAP_STATIC; 1539 } else if (map == kernel_map) { 1540 splassert(IPL_NONE); 1541 me = pool_get(&uvm_map_entry_kmem_pool, pool_flags); 1542 if (me == NULL) 1543 goto out; 1544 me->flags = UVM_MAP_KMEM; 1545 } else { 1546 splassert(IPL_NONE); 1547 me = pool_get(&uvm_map_entry_pool, pool_flags); 1548 if (me == NULL) 1549 goto out; 1550 me->flags = 0; 1551 } 1552 1553 RBT_POISON(uvm_map_addr, me, UVMMAP_DEADBEEF); 1554 out: 1555 return me; 1556 } 1557 1558 /* 1559 * uvm_mapent_free: free map entry 1560 * 1561 * => XXX: static pool for kernel map? 1562 */ 1563 void 1564 uvm_mapent_free(struct vm_map_entry *me) 1565 { 1566 if (me->flags & UVM_MAP_STATIC) { 1567 mtx_enter(&uvm_kmapent_mtx); 1568 SLIST_INSERT_HEAD(&uvm.kentry_free, me, daddrs.addr_kentry); 1569 uvmexp.kmapent--; 1570 mtx_leave(&uvm_kmapent_mtx); 1571 } else if (me->flags & UVM_MAP_KMEM) { 1572 splassert(IPL_NONE); 1573 pool_put(&uvm_map_entry_kmem_pool, me); 1574 } else { 1575 splassert(IPL_NONE); 1576 pool_put(&uvm_map_entry_pool, me); 1577 } 1578 } 1579 1580 /* 1581 * uvm_map_lookup_entry: find map entry at or before an address. 1582 * 1583 * => map must at least be read-locked by caller 1584 * => entry is returned in "entry" 1585 * => return value is true if address is in the returned entry 1586 * ET_HOLE entries are considered to not contain a mapping, ergo FALSE is 1587 * returned for those mappings. 1588 */ 1589 boolean_t 1590 uvm_map_lookup_entry(struct vm_map *map, vaddr_t address, 1591 struct vm_map_entry **entry) 1592 { 1593 vm_map_assert_anylock(map); 1594 1595 *entry = uvm_map_entrybyaddr(&map->addr, address); 1596 return *entry != NULL && !UVM_ET_ISHOLE(*entry) && 1597 (*entry)->start <= address && (*entry)->end > address; 1598 } 1599 1600 /* 1601 * Stack must be in a MAP_STACK entry. PROT_NONE indicates stack not yet 1602 * grown -- then uvm_map_check_region_range() should not cache the entry 1603 * because growth won't be seen. 1604 */ 1605 int 1606 uvm_map_inentry_sp(vm_map_entry_t entry) 1607 { 1608 if ((entry->etype & UVM_ET_STACK) == 0) { 1609 if (entry->protection == PROT_NONE) 1610 return (-1); /* don't update range */ 1611 return (0); 1612 } 1613 return (1); 1614 } 1615 1616 /* 1617 * The system call must not come from a writeable entry, W^X is violated. 1618 * (Would be nice if we can spot aliasing, which is also kind of bad) 1619 * 1620 * The system call must come from an syscall-labeled entry (which are 1621 * the text regions of the main program, sigtramp, ld.so, or libc). 1622 */ 1623 int 1624 uvm_map_inentry_pc(vm_map_entry_t entry) 1625 { 1626 if (entry->protection & PROT_WRITE) 1627 return (0); /* not permitted */ 1628 if ((entry->etype & UVM_ET_SYSCALL) == 0) 1629 return (0); /* not permitted */ 1630 return (1); 1631 } 1632 1633 int 1634 uvm_map_inentry_recheck(u_long serial, vaddr_t addr, struct p_inentry *ie) 1635 { 1636 return (serial != ie->ie_serial || ie->ie_start == 0 || 1637 addr < ie->ie_start || addr >= ie->ie_end); 1638 } 1639 1640 /* 1641 * Inside a vm_map find the reg address and verify it via function. 1642 * Remember low and high addresses of region if valid and return TRUE, 1643 * else return FALSE. 1644 */ 1645 boolean_t 1646 uvm_map_inentry_fix(struct proc *p, struct p_inentry *ie, vaddr_t addr, 1647 int (*fn)(vm_map_entry_t), u_long serial) 1648 { 1649 vm_map_t map = &p->p_vmspace->vm_map; 1650 vm_map_entry_t entry; 1651 int ret; 1652 1653 if (addr < map->min_offset || addr >= map->max_offset) 1654 return (FALSE); 1655 1656 /* lock map */ 1657 vm_map_lock_read(map); 1658 1659 /* lookup */ 1660 if (!uvm_map_lookup_entry(map, trunc_page(addr), &entry)) { 1661 vm_map_unlock_read(map); 1662 return (FALSE); 1663 } 1664 1665 ret = (*fn)(entry); 1666 if (ret == 0) { 1667 vm_map_unlock_read(map); 1668 return (FALSE); 1669 } else if (ret == 1) { 1670 ie->ie_start = entry->start; 1671 ie->ie_end = entry->end; 1672 ie->ie_serial = serial; 1673 } else { 1674 /* do not update, re-check later */ 1675 } 1676 vm_map_unlock_read(map); 1677 return (TRUE); 1678 } 1679 1680 boolean_t 1681 uvm_map_inentry(struct proc *p, struct p_inentry *ie, vaddr_t addr, 1682 const char *fmt, int (*fn)(vm_map_entry_t), u_long serial) 1683 { 1684 union sigval sv; 1685 boolean_t ok = TRUE; 1686 1687 if (uvm_map_inentry_recheck(serial, addr, ie)) { 1688 ok = uvm_map_inentry_fix(p, ie, addr, fn, serial); 1689 if (!ok) { 1690 KERNEL_LOCK(); 1691 printf(fmt, p->p_p->ps_comm, p->p_p->ps_pid, p->p_tid, 1692 addr, ie->ie_start, ie->ie_end-1); 1693 p->p_p->ps_acflag |= AMAP; 1694 sv.sival_ptr = (void *)PROC_PC(p); 1695 trapsignal(p, SIGSEGV, 0, SEGV_ACCERR, sv); 1696 KERNEL_UNLOCK(); 1697 } 1698 } 1699 return (ok); 1700 } 1701 1702 /* 1703 * Check whether the given address range can be converted to a MAP_STACK 1704 * mapping. 1705 * 1706 * Must be called with map locked. 1707 */ 1708 boolean_t 1709 uvm_map_is_stack_remappable(struct vm_map *map, vaddr_t addr, vaddr_t sz, 1710 int sigaltstack_check) 1711 { 1712 vaddr_t end = addr + sz; 1713 struct vm_map_entry *first, *iter, *prev = NULL; 1714 1715 vm_map_assert_anylock(map); 1716 1717 if (!uvm_map_lookup_entry(map, addr, &first)) { 1718 printf("map stack 0x%lx-0x%lx of map %p failed: no mapping\n", 1719 addr, end, map); 1720 return FALSE; 1721 } 1722 1723 /* 1724 * Check that the address range exists and is contiguous. 1725 */ 1726 for (iter = first; iter != NULL && iter->start < end; 1727 prev = iter, iter = RBT_NEXT(uvm_map_addr, iter)) { 1728 /* 1729 * Make sure that we do not have holes in the range. 1730 */ 1731 #if 0 1732 if (prev != NULL) { 1733 printf("prev->start 0x%lx, prev->end 0x%lx, " 1734 "iter->start 0x%lx, iter->end 0x%lx\n", 1735 prev->start, prev->end, iter->start, iter->end); 1736 } 1737 #endif 1738 1739 if (prev != NULL && prev->end != iter->start) { 1740 printf("map stack 0x%lx-0x%lx of map %p failed: " 1741 "hole in range\n", addr, end, map); 1742 return FALSE; 1743 } 1744 if (iter->start == iter->end || UVM_ET_ISHOLE(iter)) { 1745 printf("map stack 0x%lx-0x%lx of map %p failed: " 1746 "hole in range\n", addr, end, map); 1747 return FALSE; 1748 } 1749 if (sigaltstack_check) { 1750 if ((iter->etype & UVM_ET_SYSCALL)) 1751 return FALSE; 1752 if (iter->protection != (PROT_READ | PROT_WRITE)) 1753 return FALSE; 1754 } 1755 } 1756 1757 return TRUE; 1758 } 1759 1760 /* 1761 * Remap the middle-pages of an existing mapping as a stack range. 1762 * If there exists a previous contiguous mapping with the given range 1763 * [addr, addr + sz), with protection PROT_READ|PROT_WRITE, then the 1764 * mapping is dropped, and a new anon mapping is created and marked as 1765 * a stack. 1766 * 1767 * Must be called with map unlocked. 1768 */ 1769 int 1770 uvm_map_remap_as_stack(struct proc *p, vaddr_t addr, vaddr_t sz) 1771 { 1772 vm_map_t map = &p->p_vmspace->vm_map; 1773 vaddr_t start, end; 1774 int error; 1775 int flags = UVM_MAPFLAG(PROT_READ | PROT_WRITE, 1776 PROT_READ | PROT_WRITE | PROT_EXEC, 1777 MAP_INHERIT_COPY, MADV_NORMAL, 1778 UVM_FLAG_STACK | UVM_FLAG_FIXED | UVM_FLAG_UNMAP | 1779 UVM_FLAG_COPYONW | UVM_FLAG_SIGALTSTACK); 1780 1781 start = round_page(addr); 1782 end = trunc_page(addr + sz); 1783 #ifdef MACHINE_STACK_GROWS_UP 1784 if (end == addr + sz) 1785 end -= PAGE_SIZE; 1786 #else 1787 if (start == addr) 1788 start += PAGE_SIZE; 1789 #endif 1790 1791 if (start < map->min_offset || end >= map->max_offset || end < start) 1792 return EINVAL; 1793 1794 /* 1795 * UVM_FLAG_SIGALTSTACK indicates that immutable may be bypassed, 1796 * but the range is checked that it is contiguous, is not a syscall 1797 * mapping, and protection RW. Then, a new mapping (all zero) is 1798 * placed upon the region, which prevents an attacker from pivoting 1799 * into pre-placed MAP_STACK space. 1800 */ 1801 error = uvm_mapanon(map, &start, end - start, 0, flags); 1802 if (error != 0) 1803 printf("map stack for pid %d failed\n", p->p_p->ps_pid); 1804 1805 return error; 1806 } 1807 1808 /* 1809 * uvm_map_pie: return a random load address for a PIE executable 1810 * properly aligned. 1811 */ 1812 #ifndef VM_PIE_MAX_ADDR 1813 #define VM_PIE_MAX_ADDR (VM_MAXUSER_ADDRESS / 4) 1814 #endif 1815 1816 #ifndef VM_PIE_MIN_ADDR 1817 #define VM_PIE_MIN_ADDR VM_MIN_ADDRESS 1818 #endif 1819 1820 #ifndef VM_PIE_MIN_ALIGN 1821 #define VM_PIE_MIN_ALIGN PAGE_SIZE 1822 #endif 1823 1824 vaddr_t 1825 uvm_map_pie(vaddr_t align) 1826 { 1827 vaddr_t addr, space, min; 1828 1829 align = MAX(align, VM_PIE_MIN_ALIGN); 1830 1831 /* round up to next alignment */ 1832 min = (VM_PIE_MIN_ADDR + align - 1) & ~(align - 1); 1833 1834 if (align >= VM_PIE_MAX_ADDR || min >= VM_PIE_MAX_ADDR) 1835 return (align); 1836 1837 space = (VM_PIE_MAX_ADDR - min) / align; 1838 space = MIN(space, (u_int32_t)-1); 1839 1840 addr = (vaddr_t)arc4random_uniform((u_int32_t)space) * align; 1841 addr += min; 1842 1843 return (addr); 1844 } 1845 1846 void 1847 uvm_unmap(struct vm_map *map, vaddr_t start, vaddr_t end) 1848 { 1849 struct uvm_map_deadq dead; 1850 1851 KASSERT((start & (vaddr_t)PAGE_MASK) == 0 && 1852 (end & (vaddr_t)PAGE_MASK) == 0); 1853 TAILQ_INIT(&dead); 1854 vm_map_lock(map); 1855 uvm_unmap_remove(map, start, end, &dead, FALSE, TRUE, FALSE); 1856 vm_map_unlock(map); 1857 1858 if (map->flags & VM_MAP_INTRSAFE) 1859 uvm_unmap_detach_intrsafe(&dead); 1860 else 1861 uvm_unmap_detach(&dead, 0); 1862 } 1863 1864 /* 1865 * Mark entry as free. 1866 * 1867 * entry will be put on the dead list. 1868 * The free space will be merged into the previous or a new entry, 1869 * unless markfree is false. 1870 */ 1871 void 1872 uvm_mapent_mkfree(struct vm_map *map, struct vm_map_entry *entry, 1873 struct vm_map_entry **prev_ptr, struct uvm_map_deadq *dead, 1874 boolean_t markfree) 1875 { 1876 struct uvm_addr_state *free; 1877 struct vm_map_entry *prev; 1878 vaddr_t addr; /* Start of freed range. */ 1879 vaddr_t end; /* End of freed range. */ 1880 1881 UVM_MAP_REQ_WRITE(map); 1882 1883 prev = *prev_ptr; 1884 if (prev == entry) 1885 *prev_ptr = prev = NULL; 1886 1887 if (prev == NULL || 1888 VMMAP_FREE_END(prev) != entry->start) 1889 prev = RBT_PREV(uvm_map_addr, entry); 1890 1891 /* Entry is describing only free memory and has nothing to drain into. */ 1892 if (prev == NULL && entry->start == entry->end && markfree) { 1893 *prev_ptr = entry; 1894 return; 1895 } 1896 1897 addr = entry->start; 1898 end = VMMAP_FREE_END(entry); 1899 free = uvm_map_uaddr_e(map, entry); 1900 uvm_mapent_free_remove(map, free, entry); 1901 uvm_mapent_addr_remove(map, entry); 1902 DEAD_ENTRY_PUSH(dead, entry); 1903 1904 if (markfree) { 1905 if (prev) { 1906 free = uvm_map_uaddr_e(map, prev); 1907 uvm_mapent_free_remove(map, free, prev); 1908 } 1909 *prev_ptr = uvm_map_fix_space(map, prev, addr, end, 0); 1910 } 1911 } 1912 1913 /* 1914 * Unwire and release referenced amap and object from map entry. 1915 */ 1916 void 1917 uvm_unmap_kill_entry_withlock(struct vm_map *map, struct vm_map_entry *entry, 1918 int needlock) 1919 { 1920 /* Unwire removed map entry. */ 1921 if (VM_MAPENT_ISWIRED(entry)) { 1922 KERNEL_LOCK(); 1923 entry->wired_count = 0; 1924 uvm_fault_unwire_locked(map, entry->start, entry->end); 1925 KERNEL_UNLOCK(); 1926 } 1927 1928 if (needlock) 1929 uvm_map_lock_entry(entry); 1930 1931 /* Entry-type specific code. */ 1932 if (UVM_ET_ISHOLE(entry)) { 1933 /* Nothing to be done for holes. */ 1934 } else if (map->flags & VM_MAP_INTRSAFE) { 1935 KASSERT(vm_map_pmap(map) == pmap_kernel()); 1936 1937 uvm_km_pgremove_intrsafe(entry->start, entry->end); 1938 } else if (UVM_ET_ISOBJ(entry) && 1939 UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj)) { 1940 KASSERT(vm_map_pmap(map) == pmap_kernel()); 1941 /* 1942 * Note: kernel object mappings are currently used in 1943 * two ways: 1944 * [1] "normal" mappings of pages in the kernel object 1945 * [2] uvm_km_valloc'd allocations in which we 1946 * pmap_enter in some non-kernel-object page 1947 * (e.g. vmapbuf). 1948 * 1949 * for case [1], we need to remove the mapping from 1950 * the pmap and then remove the page from the kernel 1951 * object (because, once pages in a kernel object are 1952 * unmapped they are no longer needed, unlike, say, 1953 * a vnode where you might want the data to persist 1954 * until flushed out of a queue). 1955 * 1956 * for case [2], we need to remove the mapping from 1957 * the pmap. there shouldn't be any pages at the 1958 * specified offset in the kernel object [but it 1959 * doesn't hurt to call uvm_km_pgremove just to be 1960 * safe?] 1961 * 1962 * uvm_km_pgremove currently does the following: 1963 * for pages in the kernel object range: 1964 * - drops the swap slot 1965 * - uvm_pagefree the page 1966 * 1967 * note there is version of uvm_km_pgremove() that 1968 * is used for "intrsafe" objects. 1969 */ 1970 /* 1971 * remove mappings from pmap and drop the pages 1972 * from the object. offsets are always relative 1973 * to vm_map_min(kernel_map). 1974 */ 1975 uvm_km_pgremove(entry->object.uvm_obj, entry->start, 1976 entry->end); 1977 } else { 1978 /* remove mappings the standard way. */ 1979 pmap_remove(map->pmap, entry->start, entry->end); 1980 } 1981 1982 if (needlock) 1983 uvm_map_unlock_entry(entry); 1984 } 1985 1986 void 1987 uvm_unmap_kill_entry(struct vm_map *map, struct vm_map_entry *entry) 1988 { 1989 uvm_unmap_kill_entry_withlock(map, entry, 0); 1990 } 1991 1992 /* 1993 * Remove all entries from start to end. 1994 * 1995 * If remove_holes, then remove ET_HOLE entries as well. 1996 * If markfree, entry will be properly marked free, otherwise, no replacement 1997 * entry will be put in the tree (corrupting the tree). 1998 */ 1999 int 2000 uvm_unmap_remove(struct vm_map *map, vaddr_t start, vaddr_t end, 2001 struct uvm_map_deadq *dead, boolean_t remove_holes, 2002 boolean_t markfree, boolean_t checkimmutable) 2003 { 2004 struct vm_map_entry *prev_hint, *next, *entry; 2005 2006 start = MAX(start, map->min_offset); 2007 end = MIN(end, map->max_offset); 2008 if (start >= end) 2009 return 0; 2010 2011 vm_map_assert_wrlock(map); 2012 2013 /* Find first affected entry. */ 2014 entry = uvm_map_entrybyaddr(&map->addr, start); 2015 KDASSERT(entry != NULL && entry->start <= start); 2016 2017 if (checkimmutable) { 2018 struct vm_map_entry *entry1 = entry; 2019 2020 /* Refuse to unmap if any entries are immutable */ 2021 if (entry1->end <= start) 2022 entry1 = RBT_NEXT(uvm_map_addr, entry1); 2023 for (; entry1 != NULL && entry1->start < end; entry1 = next) { 2024 KDASSERT(entry1->start >= start); 2025 next = RBT_NEXT(uvm_map_addr, entry1); 2026 /* Treat memory holes as free space. */ 2027 if (entry1->start == entry1->end || UVM_ET_ISHOLE(entry1)) 2028 continue; 2029 if (entry1->etype & UVM_ET_IMMUTABLE) 2030 return EPERM; 2031 } 2032 } 2033 2034 if (entry->end <= start && markfree) 2035 entry = RBT_NEXT(uvm_map_addr, entry); 2036 else 2037 UVM_MAP_CLIP_START(map, entry, start); 2038 2039 /* 2040 * Iterate entries until we reach end address. 2041 * prev_hint hints where the freed space can be appended to. 2042 */ 2043 prev_hint = NULL; 2044 for (; entry != NULL && entry->start < end; entry = next) { 2045 KDASSERT(entry->start >= start); 2046 if (entry->end > end || !markfree) 2047 UVM_MAP_CLIP_END(map, entry, end); 2048 KDASSERT(entry->start >= start && entry->end <= end); 2049 next = RBT_NEXT(uvm_map_addr, entry); 2050 2051 /* Don't remove holes unless asked to do so. */ 2052 if (UVM_ET_ISHOLE(entry)) { 2053 if (!remove_holes) { 2054 prev_hint = entry; 2055 continue; 2056 } 2057 } 2058 2059 /* A stack has been removed.. */ 2060 if (UVM_ET_ISSTACK(entry) && (map->flags & VM_MAP_ISVMSPACE)) 2061 map->sserial++; 2062 2063 /* Kill entry. */ 2064 uvm_unmap_kill_entry_withlock(map, entry, 1); 2065 2066 /* Update space usage. */ 2067 if ((map->flags & VM_MAP_ISVMSPACE) && 2068 entry->object.uvm_obj == NULL && 2069 entry->protection != PROT_NONE && 2070 !UVM_ET_ISHOLE(entry)) { 2071 ((struct vmspace *)map)->vm_dused -= 2072 uvmspace_dused(map, entry->start, entry->end); 2073 } 2074 if (!UVM_ET_ISHOLE(entry)) 2075 map->size -= entry->end - entry->start; 2076 2077 /* Actual removal of entry. */ 2078 uvm_mapent_mkfree(map, entry, &prev_hint, dead, markfree); 2079 } 2080 2081 pmap_update(vm_map_pmap(map)); 2082 2083 #ifdef VMMAP_DEBUG 2084 if (markfree) { 2085 for (entry = uvm_map_entrybyaddr(&map->addr, start); 2086 entry != NULL && entry->start < end; 2087 entry = RBT_NEXT(uvm_map_addr, entry)) { 2088 KDASSERT(entry->end <= start || 2089 entry->start == entry->end || 2090 UVM_ET_ISHOLE(entry)); 2091 } 2092 } else { 2093 vaddr_t a; 2094 for (a = start; a < end; a += PAGE_SIZE) 2095 KDASSERT(uvm_map_entrybyaddr(&map->addr, a) == NULL); 2096 } 2097 #endif 2098 return 0; 2099 } 2100 2101 /* 2102 * Mark all entries from first until end (exclusive) as pageable. 2103 * 2104 * Lock must be exclusive on entry and will not be touched. 2105 */ 2106 void 2107 uvm_map_pageable_pgon(struct vm_map *map, struct vm_map_entry *first, 2108 struct vm_map_entry *end, vaddr_t start_addr, vaddr_t end_addr) 2109 { 2110 struct vm_map_entry *iter; 2111 2112 for (iter = first; iter != end; 2113 iter = RBT_NEXT(uvm_map_addr, iter)) { 2114 KDASSERT(iter->start >= start_addr && iter->end <= end_addr); 2115 if (!VM_MAPENT_ISWIRED(iter) || UVM_ET_ISHOLE(iter)) 2116 continue; 2117 2118 iter->wired_count = 0; 2119 uvm_fault_unwire_locked(map, iter->start, iter->end); 2120 } 2121 } 2122 2123 /* 2124 * Mark all entries from first until end (exclusive) as wired. 2125 * 2126 * Lockflags determines the lock state on return from this function. 2127 * Lock must be exclusive on entry. 2128 */ 2129 int 2130 uvm_map_pageable_wire(struct vm_map *map, struct vm_map_entry *first, 2131 struct vm_map_entry *end, vaddr_t start_addr, vaddr_t end_addr, 2132 int lockflags) 2133 { 2134 struct vm_map_entry *iter; 2135 #ifdef DIAGNOSTIC 2136 unsigned int timestamp_save; 2137 #endif 2138 int error; 2139 2140 /* 2141 * Wire pages in two passes: 2142 * 2143 * 1: holding the write lock, we create any anonymous maps that need 2144 * to be created. then we clip each map entry to the region to 2145 * be wired and increment its wiring count. 2146 * 2147 * 2: we mark the map busy, unlock it and call uvm_fault_wire to fault 2148 * in the pages for any newly wired area (wired_count == 1). 2149 */ 2150 for (iter = first; iter != end; 2151 iter = RBT_NEXT(uvm_map_addr, iter)) { 2152 KDASSERT(iter->start >= start_addr && iter->end <= end_addr); 2153 if (UVM_ET_ISHOLE(iter) || iter->start == iter->end || 2154 iter->protection == PROT_NONE) 2155 continue; 2156 2157 /* 2158 * Perform actions of vm_map_lookup that need the write lock. 2159 * - create an anonymous map for copy-on-write 2160 * - anonymous map for zero-fill 2161 * Skip submaps. 2162 */ 2163 if (!VM_MAPENT_ISWIRED(iter) && !UVM_ET_ISSUBMAP(iter) && 2164 UVM_ET_ISNEEDSCOPY(iter) && 2165 ((iter->protection & PROT_WRITE) || 2166 iter->object.uvm_obj == NULL)) { 2167 amap_copy(map, iter, M_WAITOK, 2168 UVM_ET_ISSTACK(iter) ? FALSE : TRUE, 2169 iter->start, iter->end); 2170 } 2171 iter->wired_count++; 2172 } 2173 2174 /* 2175 * Pass 2. 2176 */ 2177 #ifdef DIAGNOSTIC 2178 timestamp_save = map->timestamp; 2179 #endif 2180 vm_map_busy(map); 2181 vm_map_unlock(map); 2182 2183 error = 0; 2184 for (iter = first; error == 0 && iter != end; 2185 iter = RBT_NEXT(uvm_map_addr, iter)) { 2186 if (UVM_ET_ISHOLE(iter) || iter->start == iter->end || 2187 iter->protection == PROT_NONE) 2188 continue; 2189 2190 error = uvm_fault_wire(map, iter->start, iter->end, 2191 iter->protection); 2192 } 2193 2194 vm_map_lock(map); 2195 vm_map_unbusy(map); 2196 2197 if (error) { 2198 #ifdef DIAGNOSTIC 2199 if (timestamp_save != map->timestamp) 2200 panic("uvm_map_pageable_wire: stale map"); 2201 #endif 2202 2203 /* 2204 * first is no longer needed to restart loops. 2205 * Use it as iterator to unmap successful mappings. 2206 */ 2207 for (; first != iter; 2208 first = RBT_NEXT(uvm_map_addr, first)) { 2209 if (UVM_ET_ISHOLE(first) || 2210 first->start == first->end || 2211 first->protection == PROT_NONE) 2212 continue; 2213 2214 first->wired_count--; 2215 if (!VM_MAPENT_ISWIRED(first)) { 2216 uvm_fault_unwire_locked(map, 2217 first->start, first->end); 2218 } 2219 } 2220 2221 /* decrease counter in the rest of the entries */ 2222 for (; iter != end; 2223 iter = RBT_NEXT(uvm_map_addr, iter)) { 2224 if (UVM_ET_ISHOLE(iter) || iter->start == iter->end || 2225 iter->protection == PROT_NONE) 2226 continue; 2227 2228 iter->wired_count--; 2229 } 2230 2231 if ((lockflags & UVM_LK_EXIT) == 0) 2232 vm_map_unlock(map); 2233 return error; 2234 } 2235 2236 2237 if ((lockflags & UVM_LK_EXIT) == 0) { 2238 vm_map_unlock(map); 2239 } else { 2240 #ifdef DIAGNOSTIC 2241 if (timestamp_save != map->timestamp) 2242 panic("uvm_map_pageable_wire: stale map"); 2243 #endif 2244 } 2245 return 0; 2246 } 2247 2248 /* 2249 * uvm_map_pageable: set pageability of a range in a map. 2250 * 2251 * Flags: 2252 * UVM_LK_ENTER: map is already locked by caller 2253 * UVM_LK_EXIT: don't unlock map on exit 2254 * 2255 * The full range must be in use (entries may not have fspace != 0). 2256 * UVM_ET_HOLE counts as unmapped. 2257 */ 2258 int 2259 uvm_map_pageable(struct vm_map *map, vaddr_t start, vaddr_t end, 2260 boolean_t new_pageable, int lockflags) 2261 { 2262 struct vm_map_entry *first, *last, *tmp; 2263 int error; 2264 2265 start = trunc_page(start); 2266 end = round_page(end); 2267 2268 if (start > end) 2269 return EINVAL; 2270 if (start == end) 2271 return 0; /* nothing to do */ 2272 if (start < map->min_offset) 2273 return EFAULT; /* why? see first XXX below */ 2274 if (end > map->max_offset) 2275 return EINVAL; /* why? see second XXX below */ 2276 2277 KASSERT(map->flags & VM_MAP_PAGEABLE); 2278 if ((lockflags & UVM_LK_ENTER) == 0) 2279 vm_map_lock(map); 2280 2281 /* 2282 * Find first entry. 2283 * 2284 * Initial test on start is different, because of the different 2285 * error returned. Rest is tested further down. 2286 */ 2287 first = uvm_map_entrybyaddr(&map->addr, start); 2288 if (first->end <= start || UVM_ET_ISHOLE(first)) { 2289 /* 2290 * XXX if the first address is not mapped, it is EFAULT? 2291 */ 2292 error = EFAULT; 2293 goto out; 2294 } 2295 2296 /* Check that the range has no holes. */ 2297 for (last = first; last != NULL && last->start < end; 2298 last = RBT_NEXT(uvm_map_addr, last)) { 2299 if (UVM_ET_ISHOLE(last) || 2300 (last->end < end && VMMAP_FREE_END(last) != last->end)) { 2301 /* 2302 * XXX unmapped memory in range, why is it EINVAL 2303 * instead of EFAULT? 2304 */ 2305 error = EINVAL; 2306 goto out; 2307 } 2308 } 2309 2310 /* 2311 * Last ended at the first entry after the range. 2312 * Move back one step. 2313 * 2314 * Note that last may be NULL. 2315 */ 2316 if (last == NULL) { 2317 last = RBT_MAX(uvm_map_addr, &map->addr); 2318 if (last->end < end) { 2319 error = EINVAL; 2320 goto out; 2321 } 2322 } else { 2323 KASSERT(last != first); 2324 last = RBT_PREV(uvm_map_addr, last); 2325 } 2326 2327 /* Wire/unwire pages here. */ 2328 if (new_pageable) { 2329 /* 2330 * Mark pageable. 2331 * entries that are not wired are untouched. 2332 */ 2333 if (VM_MAPENT_ISWIRED(first)) 2334 UVM_MAP_CLIP_START(map, first, start); 2335 /* 2336 * Split last at end. 2337 * Make tmp be the first entry after what is to be touched. 2338 * If last is not wired, don't touch it. 2339 */ 2340 if (VM_MAPENT_ISWIRED(last)) { 2341 UVM_MAP_CLIP_END(map, last, end); 2342 tmp = RBT_NEXT(uvm_map_addr, last); 2343 } else 2344 tmp = last; 2345 2346 uvm_map_pageable_pgon(map, first, tmp, start, end); 2347 error = 0; 2348 2349 out: 2350 if ((lockflags & UVM_LK_EXIT) == 0) 2351 vm_map_unlock(map); 2352 return error; 2353 } else { 2354 /* 2355 * Mark entries wired. 2356 * entries are always touched (because recovery needs this). 2357 */ 2358 if (!VM_MAPENT_ISWIRED(first)) 2359 UVM_MAP_CLIP_START(map, first, start); 2360 /* 2361 * Split last at end. 2362 * Make tmp be the first entry after what is to be touched. 2363 * If last is not wired, don't touch it. 2364 */ 2365 if (!VM_MAPENT_ISWIRED(last)) { 2366 UVM_MAP_CLIP_END(map, last, end); 2367 tmp = RBT_NEXT(uvm_map_addr, last); 2368 } else 2369 tmp = last; 2370 2371 return uvm_map_pageable_wire(map, first, tmp, start, end, 2372 lockflags); 2373 } 2374 } 2375 2376 /* 2377 * uvm_map_pageable_all: special case of uvm_map_pageable - affects 2378 * all mapped regions. 2379 * 2380 * Map must not be locked. 2381 * If no flags are specified, all regions are unwired. 2382 */ 2383 int 2384 uvm_map_pageable_all(struct vm_map *map, int flags, vsize_t limit) 2385 { 2386 vsize_t size; 2387 struct vm_map_entry *iter; 2388 2389 KASSERT(map->flags & VM_MAP_PAGEABLE); 2390 vm_map_lock(map); 2391 2392 if (flags == 0) { 2393 uvm_map_pageable_pgon(map, RBT_MIN(uvm_map_addr, &map->addr), 2394 NULL, map->min_offset, map->max_offset); 2395 2396 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE); 2397 vm_map_unlock(map); 2398 return 0; 2399 } 2400 2401 if (flags & MCL_FUTURE) 2402 vm_map_modflags(map, VM_MAP_WIREFUTURE, 0); 2403 if (!(flags & MCL_CURRENT)) { 2404 vm_map_unlock(map); 2405 return 0; 2406 } 2407 2408 /* 2409 * Count number of pages in all non-wired entries. 2410 * If the number exceeds the limit, abort. 2411 */ 2412 size = 0; 2413 RBT_FOREACH(iter, uvm_map_addr, &map->addr) { 2414 if (VM_MAPENT_ISWIRED(iter) || UVM_ET_ISHOLE(iter)) 2415 continue; 2416 2417 size += iter->end - iter->start; 2418 } 2419 2420 if (atop(size) + uvmexp.wired > uvmexp.wiredmax) { 2421 vm_map_unlock(map); 2422 return ENOMEM; 2423 } 2424 2425 /* XXX non-pmap_wired_count case must be handled by caller */ 2426 #ifdef pmap_wired_count 2427 if (limit != 0 && 2428 size + ptoa(pmap_wired_count(vm_map_pmap(map))) > limit) { 2429 vm_map_unlock(map); 2430 return ENOMEM; 2431 } 2432 #endif 2433 2434 /* 2435 * uvm_map_pageable_wire will release lock 2436 */ 2437 return uvm_map_pageable_wire(map, RBT_MIN(uvm_map_addr, &map->addr), 2438 NULL, map->min_offset, map->max_offset, 0); 2439 } 2440 2441 /* 2442 * Initialize map. 2443 * 2444 * Allocates sufficient entries to describe the free memory in the map. 2445 */ 2446 void 2447 uvm_map_setup(struct vm_map *map, pmap_t pmap, vaddr_t min, vaddr_t max, 2448 int flags) 2449 { 2450 int i; 2451 2452 KASSERT((min & (vaddr_t)PAGE_MASK) == 0); 2453 KASSERT((max & (vaddr_t)PAGE_MASK) == 0 || 2454 (max & (vaddr_t)PAGE_MASK) == (vaddr_t)PAGE_MASK); 2455 2456 /* 2457 * Update parameters. 2458 * 2459 * This code handles (vaddr_t)-1 and other page mask ending addresses 2460 * properly. 2461 * We lose the top page if the full virtual address space is used. 2462 */ 2463 if (max & (vaddr_t)PAGE_MASK) { 2464 max += 1; 2465 if (max == 0) /* overflow */ 2466 max -= PAGE_SIZE; 2467 } 2468 2469 RBT_INIT(uvm_map_addr, &map->addr); 2470 map->uaddr_exe = NULL; 2471 for (i = 0; i < nitems(map->uaddr_any); ++i) 2472 map->uaddr_any[i] = NULL; 2473 map->uaddr_brk_stack = NULL; 2474 2475 map->pmap = pmap; 2476 map->size = 0; 2477 map->ref_count = 0; 2478 map->min_offset = min; 2479 map->max_offset = max; 2480 map->b_start = map->b_end = 0; /* Empty brk() area by default. */ 2481 map->s_start = map->s_end = 0; /* Empty stack area by default. */ 2482 map->flags = flags; 2483 map->timestamp = 0; 2484 map->busy = NULL; 2485 if (flags & VM_MAP_ISVMSPACE) 2486 rw_init_flags(&map->lock, "vmmaplk", RWL_DUPOK); 2487 else 2488 rw_init(&map->lock, "kmmaplk"); 2489 mtx_init(&map->mtx, IPL_VM); 2490 mtx_init(&map->flags_lock, IPL_VM); 2491 2492 /* Configure the allocators. */ 2493 if (flags & VM_MAP_ISVMSPACE) 2494 uvm_map_setup_md(map); 2495 else 2496 map->uaddr_any[3] = &uaddr_kbootstrap; 2497 2498 /* 2499 * Fill map entries. 2500 * We do not need to write-lock the map here because only the current 2501 * thread sees it right now. Initialize ref_count to 0 above to avoid 2502 * bogus triggering of lock-not-held assertions. 2503 */ 2504 uvm_map_setup_entries(map); 2505 uvm_tree_sanity(map, __FILE__, __LINE__); 2506 map->ref_count = 1; 2507 } 2508 2509 /* 2510 * Destroy the map. 2511 * 2512 * This is the inverse operation to uvm_map_setup. 2513 */ 2514 void 2515 uvm_map_teardown(struct vm_map *map) 2516 { 2517 struct uvm_map_deadq dead_entries; 2518 struct vm_map_entry *entry, *tmp; 2519 #ifdef VMMAP_DEBUG 2520 size_t numq, numt; 2521 #endif 2522 int i; 2523 2524 KERNEL_ASSERT_LOCKED(); 2525 KERNEL_UNLOCK(); 2526 KERNEL_ASSERT_UNLOCKED(); 2527 2528 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0); 2529 2530 vm_map_lock(map); 2531 2532 /* Remove address selectors. */ 2533 uvm_addr_destroy(map->uaddr_exe); 2534 map->uaddr_exe = NULL; 2535 for (i = 0; i < nitems(map->uaddr_any); i++) { 2536 uvm_addr_destroy(map->uaddr_any[i]); 2537 map->uaddr_any[i] = NULL; 2538 } 2539 uvm_addr_destroy(map->uaddr_brk_stack); 2540 map->uaddr_brk_stack = NULL; 2541 2542 /* 2543 * Remove entries. 2544 * 2545 * The following is based on graph breadth-first search. 2546 * 2547 * In color terms: 2548 * - the dead_entries set contains all nodes that are reachable 2549 * (i.e. both the black and the grey nodes) 2550 * - any entry not in dead_entries is white 2551 * - any entry that appears in dead_entries before entry, 2552 * is black, the rest is grey. 2553 * The set [entry, end] is also referred to as the wavefront. 2554 * 2555 * Since the tree is always a fully connected graph, the breadth-first 2556 * search guarantees that each vmmap_entry is visited exactly once. 2557 * The vm_map is broken down in linear time. 2558 */ 2559 TAILQ_INIT(&dead_entries); 2560 if ((entry = RBT_ROOT(uvm_map_addr, &map->addr)) != NULL) 2561 DEAD_ENTRY_PUSH(&dead_entries, entry); 2562 while (entry != NULL) { 2563 sched_pause(yield); 2564 uvm_unmap_kill_entry(map, entry); 2565 if ((tmp = RBT_LEFT(uvm_map_addr, entry)) != NULL) 2566 DEAD_ENTRY_PUSH(&dead_entries, tmp); 2567 if ((tmp = RBT_RIGHT(uvm_map_addr, entry)) != NULL) 2568 DEAD_ENTRY_PUSH(&dead_entries, tmp); 2569 /* Update wave-front. */ 2570 entry = TAILQ_NEXT(entry, dfree.deadq); 2571 } 2572 2573 vm_map_unlock(map); 2574 2575 #ifdef VMMAP_DEBUG 2576 numt = numq = 0; 2577 RBT_FOREACH(entry, uvm_map_addr, &map->addr) 2578 numt++; 2579 TAILQ_FOREACH(entry, &dead_entries, dfree.deadq) 2580 numq++; 2581 KASSERT(numt == numq); 2582 #endif 2583 uvm_unmap_detach(&dead_entries, UVM_PLA_WAITOK); 2584 2585 KERNEL_LOCK(); 2586 2587 pmap_destroy(map->pmap); 2588 map->pmap = NULL; 2589 } 2590 2591 /* 2592 * Populate map with free-memory entries. 2593 * 2594 * Map must be initialized and empty. 2595 */ 2596 void 2597 uvm_map_setup_entries(struct vm_map *map) 2598 { 2599 KDASSERT(RBT_EMPTY(uvm_map_addr, &map->addr)); 2600 2601 uvm_map_fix_space(map, NULL, map->min_offset, map->max_offset, 0); 2602 } 2603 2604 /* 2605 * Split entry at given address. 2606 * 2607 * orig: entry that is to be split. 2608 * next: a newly allocated map entry that is not linked. 2609 * split: address at which the split is done. 2610 */ 2611 void 2612 uvm_map_splitentry(struct vm_map *map, struct vm_map_entry *orig, 2613 struct vm_map_entry *next, vaddr_t split) 2614 { 2615 struct uvm_addr_state *free, *free_before; 2616 vsize_t adj; 2617 2618 if ((split & PAGE_MASK) != 0) { 2619 panic("uvm_map_splitentry: split address 0x%lx " 2620 "not on page boundary!", split); 2621 } 2622 KDASSERT(map != NULL && orig != NULL && next != NULL); 2623 uvm_tree_sanity(map, __FILE__, __LINE__); 2624 KASSERT(orig->start < split && VMMAP_FREE_END(orig) > split); 2625 2626 #ifdef VMMAP_DEBUG 2627 KDASSERT(RBT_FIND(uvm_map_addr, &map->addr, orig) == orig); 2628 KDASSERT(RBT_FIND(uvm_map_addr, &map->addr, next) != next); 2629 #endif /* VMMAP_DEBUG */ 2630 2631 /* 2632 * Free space will change, unlink from free space tree. 2633 */ 2634 free = uvm_map_uaddr_e(map, orig); 2635 uvm_mapent_free_remove(map, free, orig); 2636 2637 adj = split - orig->start; 2638 2639 uvm_mapent_copy(orig, next); 2640 if (split >= orig->end) { 2641 next->etype = 0; 2642 next->offset = 0; 2643 next->wired_count = 0; 2644 next->start = next->end = split; 2645 next->guard = 0; 2646 next->fspace = VMMAP_FREE_END(orig) - split; 2647 next->aref.ar_amap = NULL; 2648 next->aref.ar_pageoff = 0; 2649 orig->guard = MIN(orig->guard, split - orig->end); 2650 orig->fspace = split - VMMAP_FREE_START(orig); 2651 } else { 2652 orig->fspace = 0; 2653 orig->guard = 0; 2654 orig->end = next->start = split; 2655 2656 if (next->aref.ar_amap) { 2657 amap_splitref(&orig->aref, &next->aref, adj); 2658 } 2659 if (UVM_ET_ISSUBMAP(orig)) { 2660 uvm_map_reference(next->object.sub_map); 2661 next->offset += adj; 2662 } else if (UVM_ET_ISOBJ(orig)) { 2663 if (next->object.uvm_obj->pgops && 2664 next->object.uvm_obj->pgops->pgo_reference) { 2665 KERNEL_LOCK(); 2666 next->object.uvm_obj->pgops->pgo_reference( 2667 next->object.uvm_obj); 2668 KERNEL_UNLOCK(); 2669 } 2670 next->offset += adj; 2671 } 2672 } 2673 2674 /* 2675 * Link next into address tree. 2676 * Link orig and next into free-space tree. 2677 * 2678 * Don't insert 'next' into the addr tree until orig has been linked, 2679 * in case the free-list looks at adjacent entries in the addr tree 2680 * for its decisions. 2681 */ 2682 if (orig->fspace > 0) 2683 free_before = free; 2684 else 2685 free_before = uvm_map_uaddr_e(map, orig); 2686 uvm_mapent_free_insert(map, free_before, orig); 2687 uvm_mapent_addr_insert(map, next); 2688 uvm_mapent_free_insert(map, free, next); 2689 2690 uvm_tree_sanity(map, __FILE__, __LINE__); 2691 } 2692 2693 2694 #ifdef VMMAP_DEBUG 2695 2696 void 2697 uvm_tree_assert(struct vm_map *map, int test, char *test_str, 2698 char *file, int line) 2699 { 2700 char* map_special; 2701 2702 if (test) 2703 return; 2704 2705 if (map == kernel_map) 2706 map_special = " (kernel_map)"; 2707 else if (map == kmem_map) 2708 map_special = " (kmem_map)"; 2709 else 2710 map_special = ""; 2711 panic("uvm_tree_sanity %p%s (%s %d): %s", map, map_special, file, 2712 line, test_str); 2713 } 2714 2715 /* 2716 * Check that map is sane. 2717 */ 2718 void 2719 uvm_tree_sanity(struct vm_map *map, char *file, int line) 2720 { 2721 struct vm_map_entry *iter; 2722 vaddr_t addr; 2723 vaddr_t min, max, bound; /* Bounds checker. */ 2724 struct uvm_addr_state *free; 2725 2726 addr = vm_map_min(map); 2727 RBT_FOREACH(iter, uvm_map_addr, &map->addr) { 2728 /* 2729 * Valid start, end. 2730 * Catch overflow for end+fspace. 2731 */ 2732 UVM_ASSERT(map, iter->end >= iter->start, file, line); 2733 UVM_ASSERT(map, VMMAP_FREE_END(iter) >= iter->end, file, line); 2734 2735 /* May not be empty. */ 2736 UVM_ASSERT(map, iter->start < VMMAP_FREE_END(iter), 2737 file, line); 2738 2739 /* Addresses for entry must lie within map boundaries. */ 2740 UVM_ASSERT(map, iter->start >= vm_map_min(map) && 2741 VMMAP_FREE_END(iter) <= vm_map_max(map), file, line); 2742 2743 /* Tree may not have gaps. */ 2744 UVM_ASSERT(map, iter->start == addr, file, line); 2745 addr = VMMAP_FREE_END(iter); 2746 2747 /* 2748 * Free space may not cross boundaries, unless the same 2749 * free list is used on both sides of the border. 2750 */ 2751 min = VMMAP_FREE_START(iter); 2752 max = VMMAP_FREE_END(iter); 2753 2754 while (min < max && 2755 (bound = uvm_map_boundary(map, min, max)) != max) { 2756 UVM_ASSERT(map, 2757 uvm_map_uaddr(map, bound - 1) == 2758 uvm_map_uaddr(map, bound), 2759 file, line); 2760 min = bound; 2761 } 2762 2763 free = uvm_map_uaddr_e(map, iter); 2764 if (free) { 2765 UVM_ASSERT(map, (iter->etype & UVM_ET_FREEMAPPED) != 0, 2766 file, line); 2767 } else { 2768 UVM_ASSERT(map, (iter->etype & UVM_ET_FREEMAPPED) == 0, 2769 file, line); 2770 } 2771 } 2772 UVM_ASSERT(map, addr == vm_map_max(map), file, line); 2773 } 2774 2775 void 2776 uvm_tree_size_chk(struct vm_map *map, char *file, int line) 2777 { 2778 struct vm_map_entry *iter; 2779 vsize_t size; 2780 2781 size = 0; 2782 RBT_FOREACH(iter, uvm_map_addr, &map->addr) { 2783 if (!UVM_ET_ISHOLE(iter)) 2784 size += iter->end - iter->start; 2785 } 2786 2787 if (map->size != size) 2788 printf("map size = 0x%lx, should be 0x%lx\n", map->size, size); 2789 UVM_ASSERT(map, map->size == size, file, line); 2790 2791 vmspace_validate(map); 2792 } 2793 2794 /* 2795 * This function validates the statistics on vmspace. 2796 */ 2797 void 2798 vmspace_validate(struct vm_map *map) 2799 { 2800 struct vmspace *vm; 2801 struct vm_map_entry *iter; 2802 vaddr_t imin, imax; 2803 vaddr_t stack_begin, stack_end; /* Position of stack. */ 2804 vsize_t stack, heap; /* Measured sizes. */ 2805 2806 if (!(map->flags & VM_MAP_ISVMSPACE)) 2807 return; 2808 2809 vm = (struct vmspace *)map; 2810 stack_begin = MIN((vaddr_t)vm->vm_maxsaddr, (vaddr_t)vm->vm_minsaddr); 2811 stack_end = MAX((vaddr_t)vm->vm_maxsaddr, (vaddr_t)vm->vm_minsaddr); 2812 2813 stack = heap = 0; 2814 RBT_FOREACH(iter, uvm_map_addr, &map->addr) { 2815 imin = imax = iter->start; 2816 2817 if (UVM_ET_ISHOLE(iter) || iter->object.uvm_obj != NULL || 2818 iter->protection != PROT_NONE) 2819 continue; 2820 2821 /* 2822 * Update stack, heap. 2823 * Keep in mind that (theoretically) the entries of 2824 * userspace and stack may be joined. 2825 */ 2826 while (imin != iter->end) { 2827 /* 2828 * Set imax to the first boundary crossed between 2829 * imin and stack addresses. 2830 */ 2831 imax = iter->end; 2832 if (imin < stack_begin && imax > stack_begin) 2833 imax = stack_begin; 2834 else if (imin < stack_end && imax > stack_end) 2835 imax = stack_end; 2836 2837 if (imin >= stack_begin && imin < stack_end) 2838 stack += imax - imin; 2839 else 2840 heap += imax - imin; 2841 imin = imax; 2842 } 2843 } 2844 2845 heap >>= PAGE_SHIFT; 2846 if (heap != vm->vm_dused) { 2847 printf("vmspace stack range: 0x%lx-0x%lx\n", 2848 stack_begin, stack_end); 2849 panic("vmspace_validate: vmspace.vm_dused invalid, " 2850 "expected %ld pgs, got %d pgs in map %p", 2851 heap, vm->vm_dused, 2852 map); 2853 } 2854 } 2855 2856 #endif /* VMMAP_DEBUG */ 2857 2858 /* 2859 * uvm_map_init: init mapping system at boot time. note that we allocate 2860 * and init the static pool of structs vm_map_entry for the kernel here. 2861 */ 2862 void 2863 uvm_map_init(void) 2864 { 2865 static struct vm_map_entry kernel_map_entry[MAX_KMAPENT]; 2866 int lcv; 2867 2868 /* now set up static pool of kernel map entries ... */ 2869 mtx_init(&uvm_kmapent_mtx, IPL_VM); 2870 SLIST_INIT(&uvm.kentry_free); 2871 for (lcv = 0 ; lcv < MAX_KMAPENT ; lcv++) { 2872 SLIST_INSERT_HEAD(&uvm.kentry_free, 2873 &kernel_map_entry[lcv], daddrs.addr_kentry); 2874 } 2875 2876 /* initialize the map-related pools. */ 2877 pool_init(&uvm_vmspace_pool, sizeof(struct vmspace), 0, 2878 IPL_NONE, PR_WAITOK, "vmsppl", NULL); 2879 pool_init(&uvm_map_entry_pool, sizeof(struct vm_map_entry), 0, 2880 IPL_VM, PR_WAITOK, "vmmpepl", NULL); 2881 pool_init(&uvm_map_entry_kmem_pool, sizeof(struct vm_map_entry), 0, 2882 IPL_VM, 0, "vmmpekpl", NULL); 2883 pool_sethiwat(&uvm_map_entry_pool, 8192); 2884 2885 uvm_addr_init(); 2886 } 2887 2888 #if defined(DDB) 2889 2890 /* 2891 * DDB hooks 2892 */ 2893 2894 /* 2895 * uvm_map_printit: actually prints the map 2896 */ 2897 void 2898 uvm_map_printit(struct vm_map *map, boolean_t full, 2899 int (*pr)(const char *, ...)) 2900 { 2901 struct vmspace *vm; 2902 struct vm_map_entry *entry; 2903 struct uvm_addr_state *free; 2904 int in_free, i; 2905 char buf[8]; 2906 2907 (*pr)("MAP %p: [0x%lx->0x%lx]\n", map, map->min_offset,map->max_offset); 2908 (*pr)("\tbrk() allocate range: 0x%lx-0x%lx\n", 2909 map->b_start, map->b_end); 2910 (*pr)("\tstack allocate range: 0x%lx-0x%lx\n", 2911 map->s_start, map->s_end); 2912 (*pr)("\tsz=%u, ref=%d, version=%u, flags=0x%x\n", 2913 map->size, map->ref_count, map->timestamp, 2914 map->flags); 2915 (*pr)("\tpmap=%p(resident=%d)\n", map->pmap, 2916 pmap_resident_count(map->pmap)); 2917 2918 /* struct vmspace handling. */ 2919 if (map->flags & VM_MAP_ISVMSPACE) { 2920 vm = (struct vmspace *)map; 2921 2922 (*pr)("\tvm_refcnt=%d vm_shm=%p vm_rssize=%u vm_swrss=%u\n", 2923 vm->vm_refcnt, vm->vm_shm, vm->vm_rssize, vm->vm_swrss); 2924 (*pr)("\tvm_tsize=%u vm_dsize=%u\n", 2925 vm->vm_tsize, vm->vm_dsize); 2926 (*pr)("\tvm_taddr=%p vm_daddr=%p\n", 2927 vm->vm_taddr, vm->vm_daddr); 2928 (*pr)("\tvm_maxsaddr=%p vm_minsaddr=%p\n", 2929 vm->vm_maxsaddr, vm->vm_minsaddr); 2930 } 2931 2932 if (!full) 2933 goto print_uaddr; 2934 RBT_FOREACH(entry, uvm_map_addr, &map->addr) { 2935 (*pr)(" - %p: 0x%lx->0x%lx: obj=%p/0x%llx, amap=%p/%d\n", 2936 entry, entry->start, entry->end, entry->object.uvm_obj, 2937 (long long)entry->offset, entry->aref.ar_amap, 2938 entry->aref.ar_pageoff); 2939 (*pr)("\tsubmap=%c, cow=%c, nc=%c, stack=%c, " 2940 "syscall=%c, prot(max)=%d/%d, inh=%d, " 2941 "wc=%d, adv=%d\n", 2942 (entry->etype & UVM_ET_SUBMAP) ? 'T' : 'F', 2943 (entry->etype & UVM_ET_COPYONWRITE) ? 'T' : 'F', 2944 (entry->etype & UVM_ET_NEEDSCOPY) ? 'T' : 'F', 2945 (entry->etype & UVM_ET_STACK) ? 'T' : 'F', 2946 (entry->etype & UVM_ET_SYSCALL) ? 'T' : 'F', 2947 entry->protection, entry->max_protection, 2948 entry->inheritance, entry->wired_count, entry->advice); 2949 2950 free = uvm_map_uaddr_e(map, entry); 2951 in_free = (free != NULL); 2952 (*pr)("\thole=%c, free=%c, guard=0x%lx, " 2953 "free=0x%lx-0x%lx\n", 2954 (entry->etype & UVM_ET_HOLE) ? 'T' : 'F', 2955 in_free ? 'T' : 'F', 2956 entry->guard, 2957 VMMAP_FREE_START(entry), VMMAP_FREE_END(entry)); 2958 (*pr)("\tfspace_augment=%lu\n", entry->fspace_augment); 2959 (*pr)("\tfreemapped=%c, uaddr=%p\n", 2960 (entry->etype & UVM_ET_FREEMAPPED) ? 'T' : 'F', free); 2961 if (free) { 2962 (*pr)("\t\t(0x%lx-0x%lx %s)\n", 2963 free->uaddr_minaddr, free->uaddr_maxaddr, 2964 free->uaddr_functions->uaddr_name); 2965 } 2966 } 2967 2968 print_uaddr: 2969 uvm_addr_print(map->uaddr_exe, "exe", full, pr); 2970 for (i = 0; i < nitems(map->uaddr_any); i++) { 2971 snprintf(&buf[0], sizeof(buf), "any[%d]", i); 2972 uvm_addr_print(map->uaddr_any[i], &buf[0], full, pr); 2973 } 2974 uvm_addr_print(map->uaddr_brk_stack, "brk/stack", full, pr); 2975 } 2976 2977 /* 2978 * uvm_object_printit: actually prints the object 2979 */ 2980 void 2981 uvm_object_printit(struct uvm_object *uobj, boolean_t full, 2982 int (*pr)(const char *, ...)) 2983 { 2984 struct vm_page *pg; 2985 int cnt = 0; 2986 2987 (*pr)("OBJECT %p: pgops=%p, npages=%d, ", 2988 uobj, uobj->pgops, uobj->uo_npages); 2989 if (UVM_OBJ_IS_KERN_OBJECT(uobj)) 2990 (*pr)("refs=<SYSTEM>\n"); 2991 else 2992 (*pr)("refs=%d\n", uobj->uo_refs); 2993 2994 if (!full) { 2995 return; 2996 } 2997 (*pr)(" PAGES <pg,offset>:\n "); 2998 RBT_FOREACH(pg, uvm_objtree, &uobj->memt) { 2999 (*pr)("<%p,0x%llx> ", pg, (long long)pg->offset); 3000 if ((cnt % 3) == 2) { 3001 (*pr)("\n "); 3002 } 3003 cnt++; 3004 } 3005 if ((cnt % 3) != 2) { 3006 (*pr)("\n"); 3007 } 3008 } 3009 3010 /* 3011 * uvm_page_printit: actually print the page 3012 */ 3013 static const char page_flagbits[] = 3014 "\20\1BUSY\2WANTED\3TABLED\4CLEAN\5CLEANCHK\6RELEASED\7FAKE\10RDONLY" 3015 "\11ZERO\12DEV\15PAGER1\21FREE\22INACTIVE\23ACTIVE\25ANON\26AOBJ" 3016 "\27ENCRYPT\31PMAP0\32PMAP1\33PMAP2\34PMAP3\35PMAP4\36PMAP5"; 3017 3018 void 3019 uvm_page_printit(struct vm_page *pg, boolean_t full, 3020 int (*pr)(const char *, ...)) 3021 { 3022 struct vm_page *tpg; 3023 struct uvm_object *uobj; 3024 struct pglist *pgl; 3025 3026 (*pr)("PAGE %p:\n", pg); 3027 (*pr)(" flags=%b, vers=%d, wire_count=%d, pa=0x%llx\n", 3028 pg->pg_flags, page_flagbits, pg->pg_version, pg->wire_count, 3029 (long long)pg->phys_addr); 3030 (*pr)(" uobject=%p, uanon=%p, offset=0x%llx\n", 3031 pg->uobject, pg->uanon, (long long)pg->offset); 3032 #if defined(UVM_PAGE_TRKOWN) 3033 if (pg->pg_flags & PG_BUSY) 3034 (*pr)(" owning thread = %d, tag=%s", 3035 pg->owner, pg->owner_tag); 3036 else 3037 (*pr)(" page not busy, no owner"); 3038 #else 3039 (*pr)(" [page ownership tracking disabled]"); 3040 #endif 3041 (*pr)("\tvm_page_md %p\n", &pg->mdpage); 3042 3043 if (!full) 3044 return; 3045 3046 /* cross-verify object/anon */ 3047 if ((pg->pg_flags & PQ_FREE) == 0) { 3048 if (pg->pg_flags & PQ_ANON) { 3049 if (pg->uanon == NULL || pg->uanon->an_page != pg) 3050 (*pr)(" >>> ANON DOES NOT POINT HERE <<< (%p)\n", 3051 (pg->uanon) ? pg->uanon->an_page : NULL); 3052 else 3053 (*pr)(" anon backpointer is OK\n"); 3054 } else { 3055 uobj = pg->uobject; 3056 if (uobj) { 3057 (*pr)(" checking object list\n"); 3058 RBT_FOREACH(tpg, uvm_objtree, &uobj->memt) { 3059 if (tpg == pg) { 3060 break; 3061 } 3062 } 3063 if (tpg) 3064 (*pr)(" page found on object list\n"); 3065 else 3066 (*pr)(" >>> PAGE NOT FOUND " 3067 "ON OBJECT LIST! <<<\n"); 3068 } 3069 } 3070 } 3071 3072 /* cross-verify page queue */ 3073 if (pg->pg_flags & PQ_FREE) { 3074 if (uvm_pmr_isfree(pg)) 3075 (*pr)(" page found in uvm_pmemrange\n"); 3076 else 3077 (*pr)(" >>> page not found in uvm_pmemrange <<<\n"); 3078 pgl = NULL; 3079 } else if (pg->pg_flags & PQ_INACTIVE) { 3080 pgl = &uvm.page_inactive; 3081 } else if (pg->pg_flags & PQ_ACTIVE) { 3082 pgl = &uvm.page_active; 3083 } else { 3084 pgl = NULL; 3085 } 3086 3087 if (pgl) { 3088 (*pr)(" checking pageq list\n"); 3089 TAILQ_FOREACH(tpg, pgl, pageq) { 3090 if (tpg == pg) { 3091 break; 3092 } 3093 } 3094 if (tpg) 3095 (*pr)(" page found on pageq list\n"); 3096 else 3097 (*pr)(" >>> PAGE NOT FOUND ON PAGEQ LIST! <<<\n"); 3098 } 3099 } 3100 #endif 3101 3102 /* 3103 * uvm_map_protect: change map protection 3104 * 3105 * => set_max means set max_protection. 3106 * => map must be unlocked. 3107 */ 3108 int 3109 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end, 3110 vm_prot_t new_prot, int etype, boolean_t set_max, boolean_t checkimmutable) 3111 { 3112 struct vm_map_entry *first, *iter; 3113 vm_prot_t old_prot; 3114 vm_prot_t mask; 3115 vsize_t dused; 3116 int error; 3117 3118 KASSERT((etype & ~UVM_ET_STACK) == 0); /* only UVM_ET_STACK allowed */ 3119 3120 if (start > end) 3121 return EINVAL; 3122 start = MAX(start, map->min_offset); 3123 end = MIN(end, map->max_offset); 3124 if (start >= end) 3125 return 0; 3126 3127 dused = 0; 3128 error = 0; 3129 vm_map_lock(map); 3130 3131 /* 3132 * Set up first and last. 3133 * - first will contain first entry at or after start. 3134 */ 3135 first = uvm_map_entrybyaddr(&map->addr, start); 3136 KDASSERT(first != NULL); 3137 if (first->end <= start) 3138 first = RBT_NEXT(uvm_map_addr, first); 3139 3140 /* First, check for protection violations. */ 3141 for (iter = first; iter != NULL && iter->start < end; 3142 iter = RBT_NEXT(uvm_map_addr, iter)) { 3143 /* Treat memory holes as free space. */ 3144 if (iter->start == iter->end || UVM_ET_ISHOLE(iter)) 3145 continue; 3146 3147 if (checkimmutable && (iter->etype & UVM_ET_IMMUTABLE)) { 3148 error = EPERM; 3149 goto out; 3150 } 3151 old_prot = iter->protection; 3152 if (old_prot == PROT_NONE && new_prot != old_prot) { 3153 dused += uvmspace_dused( 3154 map, MAX(start, iter->start), MIN(end, iter->end)); 3155 } 3156 3157 if (UVM_ET_ISSUBMAP(iter)) { 3158 error = EINVAL; 3159 goto out; 3160 } 3161 if ((new_prot & iter->max_protection) != new_prot) { 3162 error = EACCES; 3163 goto out; 3164 } 3165 if (map == kernel_map && 3166 (new_prot & (PROT_WRITE | PROT_EXEC)) == (PROT_WRITE | PROT_EXEC)) 3167 panic("uvm_map_protect: kernel map W^X violation requested"); 3168 } 3169 3170 /* Check limits. */ 3171 if (dused > 0 && (map->flags & VM_MAP_ISVMSPACE)) { 3172 vsize_t limit = lim_cur(RLIMIT_DATA); 3173 dused = ptoa(dused); 3174 if (limit < dused || 3175 limit - dused < ptoa(((struct vmspace *)map)->vm_dused)) { 3176 error = ENOMEM; 3177 goto out; 3178 } 3179 } 3180 3181 /* only apply UVM_ET_STACK on a mapping changing to RW */ 3182 if (etype && new_prot != (PROT_READ|PROT_WRITE)) 3183 etype = 0; 3184 3185 /* Fix protections. */ 3186 for (iter = first; iter != NULL && iter->start < end; 3187 iter = RBT_NEXT(uvm_map_addr, iter)) { 3188 /* Treat memory holes as free space. */ 3189 if (iter->start == iter->end || UVM_ET_ISHOLE(iter)) 3190 continue; 3191 3192 old_prot = iter->protection; 3193 3194 /* 3195 * Skip adapting protection iff old and new protection 3196 * are equal. 3197 */ 3198 if (set_max) { 3199 if (old_prot == (new_prot & old_prot) && 3200 iter->max_protection == new_prot) 3201 continue; 3202 } else { 3203 if (old_prot == new_prot) 3204 continue; 3205 } 3206 3207 UVM_MAP_CLIP_START(map, iter, start); 3208 UVM_MAP_CLIP_END(map, iter, end); 3209 3210 if (set_max) { 3211 iter->max_protection = new_prot; 3212 iter->protection &= new_prot; 3213 } else 3214 iter->protection = new_prot; 3215 iter->etype |= etype; /* potentially add UVM_ET_STACK */ 3216 3217 /* 3218 * update physical map if necessary. worry about copy-on-write 3219 * here -- CHECK THIS XXX 3220 */ 3221 if (iter->protection != old_prot) { 3222 mask = UVM_ET_ISCOPYONWRITE(iter) ? 3223 ~PROT_WRITE : PROT_MASK; 3224 3225 /* XXX should only wserial++ if no split occurs */ 3226 if (iter->protection & PROT_WRITE) 3227 map->wserial++; 3228 3229 if (map->flags & VM_MAP_ISVMSPACE) { 3230 if (old_prot == PROT_NONE) { 3231 ((struct vmspace *)map)->vm_dused += 3232 uvmspace_dused(map, iter->start, 3233 iter->end); 3234 } 3235 if (iter->protection == PROT_NONE) { 3236 ((struct vmspace *)map)->vm_dused -= 3237 uvmspace_dused(map, iter->start, 3238 iter->end); 3239 } 3240 } 3241 3242 /* update pmap */ 3243 if ((iter->protection & mask) == PROT_NONE && 3244 VM_MAPENT_ISWIRED(iter)) { 3245 /* 3246 * TODO(ariane) this is stupid. wired_count 3247 * is 0 if not wired, otherwise anything 3248 * larger than 0 (incremented once each time 3249 * wire is called). 3250 * Mostly to be able to undo the damage on 3251 * failure. Not the actually be a wired 3252 * refcounter... 3253 * Originally: iter->wired_count--; 3254 * (don't we have to unwire this in the pmap 3255 * as well?) 3256 */ 3257 iter->wired_count = 0; 3258 } 3259 uvm_map_lock_entry(iter); 3260 pmap_protect(map->pmap, iter->start, iter->end, 3261 iter->protection & mask); 3262 uvm_map_unlock_entry(iter); 3263 } 3264 3265 /* 3266 * If the map is configured to lock any future mappings, 3267 * wire this entry now if the old protection was PROT_NONE 3268 * and the new protection is not PROT_NONE. 3269 */ 3270 if ((map->flags & VM_MAP_WIREFUTURE) != 0 && 3271 VM_MAPENT_ISWIRED(iter) == 0 && 3272 old_prot == PROT_NONE && 3273 new_prot != PROT_NONE) { 3274 if (uvm_map_pageable(map, iter->start, iter->end, 3275 FALSE, UVM_LK_ENTER | UVM_LK_EXIT) != 0) { 3276 /* 3277 * If locking the entry fails, remember the 3278 * error if it's the first one. Note we 3279 * still continue setting the protection in 3280 * the map, but it will return the resource 3281 * storage condition regardless. 3282 * 3283 * XXX Ignore what the actual error is, 3284 * XXX just call it a resource shortage 3285 * XXX so that it doesn't get confused 3286 * XXX what uvm_map_protect() itself would 3287 * XXX normally return. 3288 */ 3289 error = ENOMEM; 3290 } 3291 } 3292 } 3293 pmap_update(map->pmap); 3294 3295 out: 3296 if (etype & UVM_ET_STACK) 3297 map->sserial++; 3298 vm_map_unlock(map); 3299 return error; 3300 } 3301 3302 /* 3303 * uvmspace_alloc: allocate a vmspace structure. 3304 * 3305 * - structure includes vm_map and pmap 3306 * - XXX: no locking on this structure 3307 * - refcnt set to 1, rest must be init'd by caller 3308 */ 3309 struct vmspace * 3310 uvmspace_alloc(vaddr_t min, vaddr_t max, boolean_t pageable, 3311 boolean_t remove_holes) 3312 { 3313 struct vmspace *vm; 3314 3315 vm = pool_get(&uvm_vmspace_pool, PR_WAITOK | PR_ZERO); 3316 uvmspace_init(vm, NULL, min, max, pageable, remove_holes); 3317 return (vm); 3318 } 3319 3320 /* 3321 * uvmspace_init: initialize a vmspace structure. 3322 * 3323 * - XXX: no locking on this structure 3324 * - refcnt set to 1, rest must be init'd by caller 3325 */ 3326 void 3327 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t min, vaddr_t max, 3328 boolean_t pageable, boolean_t remove_holes) 3329 { 3330 KASSERT(pmap == NULL || pmap == pmap_kernel()); 3331 3332 if (pmap) 3333 pmap_reference(pmap); 3334 else 3335 pmap = pmap_create(); 3336 3337 uvm_map_setup(&vm->vm_map, pmap, min, max, 3338 (pageable ? VM_MAP_PAGEABLE : 0) | VM_MAP_ISVMSPACE); 3339 3340 vm->vm_refcnt = 1; 3341 3342 if (remove_holes) 3343 pmap_remove_holes(vm); 3344 } 3345 3346 /* 3347 * uvmspace_share: share a vmspace between two processes 3348 * 3349 * - used for vfork 3350 */ 3351 3352 struct vmspace * 3353 uvmspace_share(struct process *pr) 3354 { 3355 struct vmspace *vm = pr->ps_vmspace; 3356 3357 uvmspace_addref(vm); 3358 return vm; 3359 } 3360 3361 /* 3362 * uvmspace_exec: the process wants to exec a new program 3363 * 3364 * - XXX: no locking on vmspace 3365 */ 3366 3367 void 3368 uvmspace_exec(struct proc *p, vaddr_t start, vaddr_t end) 3369 { 3370 struct process *pr = p->p_p; 3371 struct vmspace *nvm, *ovm = pr->ps_vmspace; 3372 struct vm_map *map = &ovm->vm_map; 3373 struct uvm_map_deadq dead_entries; 3374 3375 KASSERT((start & (vaddr_t)PAGE_MASK) == 0); 3376 KASSERT((end & (vaddr_t)PAGE_MASK) == 0 || 3377 (end & (vaddr_t)PAGE_MASK) == (vaddr_t)PAGE_MASK); 3378 3379 pmap_unuse_final(p); /* before stack addresses go away */ 3380 TAILQ_INIT(&dead_entries); 3381 3382 /* see if more than one process is using this vmspace... */ 3383 if (ovm->vm_refcnt == 1) { 3384 /* 3385 * If pr is the only process using its vmspace then 3386 * we can safely recycle that vmspace for the program 3387 * that is being exec'd. 3388 */ 3389 3390 #ifdef SYSVSHM 3391 /* 3392 * SYSV SHM semantics require us to kill all segments on an exec 3393 */ 3394 if (ovm->vm_shm) 3395 shmexit(ovm); 3396 #endif 3397 3398 /* 3399 * POSIX 1003.1b -- "lock future mappings" is revoked 3400 * when a process execs another program image. 3401 */ 3402 vm_map_lock(map); 3403 vm_map_modflags(map, 0, VM_MAP_WIREFUTURE | 3404 VM_MAP_SYSCALL_ONCE | VM_MAP_PINSYSCALL_ONCE); 3405 3406 /* 3407 * now unmap the old program 3408 * 3409 * Instead of attempting to keep the map valid, we simply 3410 * nuke all entries and ask uvm_map_setup to reinitialize 3411 * the map to the new boundaries. 3412 * 3413 * uvm_unmap_remove will actually nuke all entries for us 3414 * (as in, not replace them with free-memory entries). 3415 */ 3416 uvm_unmap_remove(map, map->min_offset, map->max_offset, 3417 &dead_entries, TRUE, FALSE, FALSE); 3418 3419 KDASSERT(RBT_EMPTY(uvm_map_addr, &map->addr)); 3420 3421 /* Nuke statistics and boundaries. */ 3422 memset(&ovm->vm_startcopy, 0, 3423 (caddr_t) (ovm + 1) - (caddr_t) &ovm->vm_startcopy); 3424 3425 3426 if (end & (vaddr_t)PAGE_MASK) { 3427 end += 1; 3428 if (end == 0) /* overflow */ 3429 end -= PAGE_SIZE; 3430 } 3431 3432 /* Setup new boundaries and populate map with entries. */ 3433 map->min_offset = start; 3434 map->max_offset = end; 3435 uvm_map_setup_entries(map); 3436 vm_map_unlock(map); 3437 3438 /* but keep MMU holes unavailable */ 3439 pmap_remove_holes(ovm); 3440 } else { 3441 /* 3442 * pr's vmspace is being shared, so we can't reuse 3443 * it for pr since it is still being used for others. 3444 * allocate a new vmspace for pr 3445 */ 3446 nvm = uvmspace_alloc(start, end, 3447 (map->flags & VM_MAP_PAGEABLE) ? TRUE : FALSE, TRUE); 3448 3449 /* install new vmspace and drop our ref to the old one. */ 3450 pmap_deactivate(p); 3451 p->p_vmspace = pr->ps_vmspace = nvm; 3452 pmap_activate(p); 3453 3454 uvmspace_free(ovm); 3455 } 3456 #ifdef PMAP_CHECK_COPYIN 3457 p->p_vmspace->vm_map.check_copyin_count = 0; /* disable checks */ 3458 #endif 3459 3460 /* Release dead entries */ 3461 uvm_unmap_detach(&dead_entries, 0); 3462 } 3463 3464 /* 3465 * uvmspace_addref: add a reference to a vmspace. 3466 */ 3467 void 3468 uvmspace_addref(struct vmspace *vm) 3469 { 3470 KERNEL_ASSERT_LOCKED(); 3471 KASSERT(vm->vm_refcnt > 0); 3472 3473 vm->vm_refcnt++; 3474 } 3475 3476 /* 3477 * uvmspace_free: free a vmspace data structure 3478 */ 3479 void 3480 uvmspace_free(struct vmspace *vm) 3481 { 3482 KERNEL_ASSERT_LOCKED(); 3483 3484 if (--vm->vm_refcnt == 0) { 3485 /* 3486 * lock the map, to wait out all other references to it. delete 3487 * all of the mappings and pages they hold, then call the pmap 3488 * module to reclaim anything left. 3489 */ 3490 #ifdef SYSVSHM 3491 /* Get rid of any SYSV shared memory segments. */ 3492 if (vm->vm_shm != NULL) 3493 shmexit(vm); 3494 #endif 3495 3496 uvm_map_teardown(&vm->vm_map); 3497 pool_put(&uvm_vmspace_pool, vm); 3498 } 3499 } 3500 3501 /* 3502 * uvm_share: Map the address range [srcaddr, srcaddr + sz) in 3503 * srcmap to the address range [dstaddr, dstaddr + sz) in 3504 * dstmap. 3505 * 3506 * The whole address range in srcmap must be backed by an object 3507 * (no holes). 3508 * 3509 * If successful, the address ranges share memory and the destination 3510 * address range uses the protection flags in prot. 3511 * 3512 * This routine assumes that sz is a multiple of PAGE_SIZE and 3513 * that dstaddr and srcaddr are page-aligned. 3514 */ 3515 int 3516 uvm_share(struct vm_map *dstmap, vaddr_t dstaddr, vm_prot_t prot, 3517 struct vm_map *srcmap, vaddr_t srcaddr, vsize_t sz) 3518 { 3519 int ret = 0; 3520 vaddr_t unmap_end; 3521 vaddr_t dstva; 3522 vsize_t s_off, len, n = sz, remain; 3523 struct vm_map_entry *first = NULL, *last = NULL; 3524 struct vm_map_entry *src_entry, *psrc_entry = NULL; 3525 struct uvm_map_deadq dead; 3526 3527 if (srcaddr >= srcmap->max_offset || sz > srcmap->max_offset - srcaddr) 3528 return EINVAL; 3529 3530 TAILQ_INIT(&dead); 3531 vm_map_lock(dstmap); 3532 vm_map_lock_read(srcmap); 3533 3534 if (!uvm_map_isavail(dstmap, NULL, &first, &last, dstaddr, sz)) { 3535 ret = ENOMEM; 3536 goto exit_unlock; 3537 } 3538 if (!uvm_map_lookup_entry(srcmap, srcaddr, &src_entry)) { 3539 ret = EINVAL; 3540 goto exit_unlock; 3541 } 3542 3543 dstva = dstaddr; 3544 unmap_end = dstaddr; 3545 for (; src_entry != NULL; 3546 psrc_entry = src_entry, 3547 src_entry = RBT_NEXT(uvm_map_addr, src_entry)) { 3548 /* hole in address space, bail out */ 3549 if (psrc_entry != NULL && psrc_entry->end != src_entry->start) 3550 break; 3551 if (src_entry->start >= srcaddr + sz) 3552 break; 3553 3554 if (UVM_ET_ISSUBMAP(src_entry)) 3555 panic("uvm_share: encountered a submap (illegal)"); 3556 if (!UVM_ET_ISCOPYONWRITE(src_entry) && 3557 UVM_ET_ISNEEDSCOPY(src_entry)) 3558 panic("uvm_share: non-copy_on_write map entries " 3559 "marked needs_copy (illegal)"); 3560 3561 /* 3562 * srcaddr > map entry start? means we are in the middle of a 3563 * map, so we calculate the offset to use in the source map. 3564 */ 3565 if (srcaddr > src_entry->start) 3566 s_off = srcaddr - src_entry->start; 3567 else if (srcaddr == src_entry->start) 3568 s_off = 0; 3569 else 3570 panic("uvm_share: map entry start > srcaddr"); 3571 3572 remain = src_entry->end - src_entry->start - s_off; 3573 3574 /* Determine how many bytes to share in this pass */ 3575 if (n < remain) 3576 len = n; 3577 else 3578 len = remain; 3579 3580 if (uvm_mapent_share(dstmap, dstva, len, s_off, prot, prot, 3581 srcmap, src_entry, &dead) == NULL) 3582 break; 3583 3584 n -= len; 3585 dstva += len; 3586 srcaddr += len; 3587 unmap_end = dstva + len; 3588 if (n == 0) 3589 goto exit_unlock; 3590 } 3591 3592 ret = EINVAL; 3593 uvm_unmap_remove(dstmap, dstaddr, unmap_end, &dead, FALSE, TRUE, FALSE); 3594 3595 exit_unlock: 3596 vm_map_unlock_read(srcmap); 3597 vm_map_unlock(dstmap); 3598 uvm_unmap_detach(&dead, 0); 3599 3600 return ret; 3601 } 3602 3603 /* 3604 * Clone map entry into other map. 3605 * 3606 * Mapping will be placed at dstaddr, for the same length. 3607 * Space must be available. 3608 * Reference counters are incremented. 3609 */ 3610 struct vm_map_entry * 3611 uvm_mapent_clone(struct vm_map *dstmap, vaddr_t dstaddr, vsize_t dstlen, 3612 vsize_t off, vm_prot_t prot, vm_prot_t maxprot, 3613 struct vm_map_entry *old_entry, struct uvm_map_deadq *dead, 3614 int mapent_flags, int amap_share_flags) 3615 { 3616 struct vm_map_entry *new_entry, *first, *last; 3617 3618 KDASSERT(!UVM_ET_ISSUBMAP(old_entry)); 3619 3620 /* Create new entry (linked in on creation). Fill in first, last. */ 3621 first = last = NULL; 3622 if (!uvm_map_isavail(dstmap, NULL, &first, &last, dstaddr, dstlen)) { 3623 panic("uvm_mapent_clone: no space in map for " 3624 "entry in empty map"); 3625 } 3626 new_entry = uvm_map_mkentry(dstmap, first, last, 3627 dstaddr, dstlen, mapent_flags, dead, NULL); 3628 if (new_entry == NULL) 3629 return NULL; 3630 /* old_entry -> new_entry */ 3631 new_entry->object = old_entry->object; 3632 new_entry->offset = old_entry->offset; 3633 new_entry->aref = old_entry->aref; 3634 new_entry->etype |= old_entry->etype & ~UVM_ET_FREEMAPPED; 3635 new_entry->protection = prot; 3636 new_entry->max_protection = maxprot; 3637 new_entry->inheritance = old_entry->inheritance; 3638 new_entry->advice = old_entry->advice; 3639 3640 /* gain reference to object backing the map (can't be a submap). */ 3641 if (new_entry->aref.ar_amap) { 3642 new_entry->aref.ar_pageoff += off >> PAGE_SHIFT; 3643 amap_ref(new_entry->aref.ar_amap, new_entry->aref.ar_pageoff, 3644 (new_entry->end - new_entry->start) >> PAGE_SHIFT, 3645 amap_share_flags); 3646 } 3647 3648 if (UVM_ET_ISOBJ(new_entry) && 3649 new_entry->object.uvm_obj->pgops->pgo_reference) { 3650 new_entry->offset += off; 3651 new_entry->object.uvm_obj->pgops->pgo_reference 3652 (new_entry->object.uvm_obj); 3653 } 3654 3655 return new_entry; 3656 } 3657 3658 struct vm_map_entry * 3659 uvm_mapent_share(struct vm_map *dstmap, vaddr_t dstaddr, vsize_t dstlen, 3660 vsize_t off, vm_prot_t prot, vm_prot_t maxprot, struct vm_map *old_map, 3661 struct vm_map_entry *old_entry, struct uvm_map_deadq *dead) 3662 { 3663 /* 3664 * If old_entry refers to a copy-on-write region that has not yet been 3665 * written to (needs_copy flag is set), then we need to allocate a new 3666 * amap for old_entry. 3667 * 3668 * If we do not do this, and the process owning old_entry does a copy-on 3669 * write later, old_entry and new_entry will refer to different memory 3670 * regions, and the memory between the processes is no longer shared. 3671 * 3672 * [in other words, we need to clear needs_copy] 3673 */ 3674 3675 if (UVM_ET_ISNEEDSCOPY(old_entry)) { 3676 /* get our own amap, clears needs_copy */ 3677 amap_copy(old_map, old_entry, M_WAITOK, FALSE, 0, 0); 3678 /* XXXCDC: WAITOK??? */ 3679 } 3680 3681 return uvm_mapent_clone(dstmap, dstaddr, dstlen, off, 3682 prot, maxprot, old_entry, dead, 0, AMAP_SHARED); 3683 } 3684 3685 /* 3686 * share the mapping: this means we want the old and 3687 * new entries to share amaps and backing objects. 3688 */ 3689 struct vm_map_entry * 3690 uvm_mapent_forkshared(struct vmspace *new_vm, struct vm_map *new_map, 3691 struct vm_map *old_map, 3692 struct vm_map_entry *old_entry, struct uvm_map_deadq *dead) 3693 { 3694 struct vm_map_entry *new_entry; 3695 3696 new_entry = uvm_mapent_share(new_map, old_entry->start, 3697 old_entry->end - old_entry->start, 0, old_entry->protection, 3698 old_entry->max_protection, old_map, old_entry, dead); 3699 3700 return (new_entry); 3701 } 3702 3703 /* 3704 * copy-on-write the mapping (using mmap's 3705 * MAP_PRIVATE semantics) 3706 * 3707 * allocate new_entry, adjust reference counts. 3708 * (note that new references are read-only). 3709 */ 3710 struct vm_map_entry * 3711 uvm_mapent_forkcopy(struct vmspace *new_vm, struct vm_map *new_map, 3712 struct vm_map *old_map, 3713 struct vm_map_entry *old_entry, struct uvm_map_deadq *dead) 3714 { 3715 struct vm_map_entry *new_entry; 3716 boolean_t protect_child; 3717 3718 new_entry = uvm_mapent_clone(new_map, old_entry->start, 3719 old_entry->end - old_entry->start, 0, old_entry->protection, 3720 old_entry->max_protection, old_entry, dead, 0, 0); 3721 3722 new_entry->etype |= 3723 (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY); 3724 3725 /* 3726 * the new entry will need an amap. it will either 3727 * need to be copied from the old entry or created 3728 * from scratch (if the old entry does not have an 3729 * amap). can we defer this process until later 3730 * (by setting "needs_copy") or do we need to copy 3731 * the amap now? 3732 * 3733 * we must copy the amap now if any of the following 3734 * conditions hold: 3735 * 1. the old entry has an amap and that amap is 3736 * being shared. this means that the old (parent) 3737 * process is sharing the amap with another 3738 * process. if we do not clear needs_copy here 3739 * we will end up in a situation where both the 3740 * parent and child process are referring to the 3741 * same amap with "needs_copy" set. if the 3742 * parent write-faults, the fault routine will 3743 * clear "needs_copy" in the parent by allocating 3744 * a new amap. this is wrong because the 3745 * parent is supposed to be sharing the old amap 3746 * and the new amap will break that. 3747 * 3748 * 2. if the old entry has an amap and a non-zero 3749 * wire count then we are going to have to call 3750 * amap_cow_now to avoid page faults in the 3751 * parent process. since amap_cow_now requires 3752 * "needs_copy" to be clear we might as well 3753 * clear it here as well. 3754 * 3755 */ 3756 if (old_entry->aref.ar_amap != NULL && 3757 ((amap_flags(old_entry->aref.ar_amap) & 3758 AMAP_SHARED) != 0 || 3759 VM_MAPENT_ISWIRED(old_entry))) { 3760 amap_copy(new_map, new_entry, M_WAITOK, FALSE, 3761 0, 0); 3762 /* XXXCDC: M_WAITOK ... ok? */ 3763 } 3764 3765 /* 3766 * if the parent's entry is wired down, then the 3767 * parent process does not want page faults on 3768 * access to that memory. this means that we 3769 * cannot do copy-on-write because we can't write 3770 * protect the old entry. in this case we 3771 * resolve all copy-on-write faults now, using 3772 * amap_cow_now. note that we have already 3773 * allocated any needed amap (above). 3774 */ 3775 if (VM_MAPENT_ISWIRED(old_entry)) { 3776 /* 3777 * resolve all copy-on-write faults now 3778 * (note that there is nothing to do if 3779 * the old mapping does not have an amap). 3780 */ 3781 if (old_entry->aref.ar_amap) 3782 amap_cow_now(new_map, new_entry); 3783 } else { 3784 if (old_entry->aref.ar_amap) { 3785 /* 3786 * setup mappings to trigger copy-on-write faults 3787 * we must write-protect the parent if it has 3788 * an amap and it is not already "needs_copy"... 3789 * if it is already "needs_copy" then the parent 3790 * has already been write-protected by a previous 3791 * fork operation. 3792 * 3793 * if we do not write-protect the parent, then 3794 * we must be sure to write-protect the child. 3795 */ 3796 if (!UVM_ET_ISNEEDSCOPY(old_entry)) { 3797 if (old_entry->max_protection & PROT_WRITE) { 3798 uvm_map_lock_entry(old_entry); 3799 pmap_protect(old_map->pmap, 3800 old_entry->start, 3801 old_entry->end, 3802 old_entry->protection & 3803 ~PROT_WRITE); 3804 uvm_map_unlock_entry(old_entry); 3805 pmap_update(old_map->pmap); 3806 } 3807 old_entry->etype |= UVM_ET_NEEDSCOPY; 3808 } 3809 3810 /* parent must now be write-protected */ 3811 protect_child = FALSE; 3812 } else { 3813 /* 3814 * we only need to protect the child if the 3815 * parent has write access. 3816 */ 3817 if (old_entry->max_protection & PROT_WRITE) 3818 protect_child = TRUE; 3819 else 3820 protect_child = FALSE; 3821 } 3822 3823 /* protect the child's mappings if necessary */ 3824 if (protect_child) { 3825 pmap_protect(new_map->pmap, new_entry->start, 3826 new_entry->end, 3827 new_entry->protection & 3828 ~PROT_WRITE); 3829 } 3830 } 3831 3832 return (new_entry); 3833 } 3834 3835 /* 3836 * zero the mapping: the new entry will be zero initialized 3837 */ 3838 struct vm_map_entry * 3839 uvm_mapent_forkzero(struct vmspace *new_vm, struct vm_map *new_map, 3840 struct vm_map *old_map, 3841 struct vm_map_entry *old_entry, struct uvm_map_deadq *dead) 3842 { 3843 struct vm_map_entry *new_entry; 3844 3845 new_entry = uvm_mapent_clone(new_map, old_entry->start, 3846 old_entry->end - old_entry->start, 0, old_entry->protection, 3847 old_entry->max_protection, old_entry, dead, 0, 0); 3848 3849 new_entry->etype |= 3850 (UVM_ET_COPYONWRITE|UVM_ET_NEEDSCOPY); 3851 3852 if (new_entry->aref.ar_amap) { 3853 amap_unref(new_entry->aref.ar_amap, new_entry->aref.ar_pageoff, 3854 atop(new_entry->end - new_entry->start), 0); 3855 new_entry->aref.ar_amap = NULL; 3856 new_entry->aref.ar_pageoff = 0; 3857 } 3858 3859 if (UVM_ET_ISOBJ(new_entry)) { 3860 if (new_entry->object.uvm_obj->pgops->pgo_detach) 3861 new_entry->object.uvm_obj->pgops->pgo_detach( 3862 new_entry->object.uvm_obj); 3863 new_entry->object.uvm_obj = NULL; 3864 new_entry->etype &= ~UVM_ET_OBJ; 3865 } 3866 3867 return (new_entry); 3868 } 3869 3870 /* 3871 * uvmspace_fork: fork a process' main map 3872 * 3873 * => create a new vmspace for child process from parent. 3874 * => parent's map must not be locked. 3875 */ 3876 struct vmspace * 3877 uvmspace_fork(struct process *pr) 3878 { 3879 struct vmspace *vm1 = pr->ps_vmspace; 3880 struct vmspace *vm2; 3881 struct vm_map *old_map = &vm1->vm_map; 3882 struct vm_map *new_map; 3883 struct vm_map_entry *old_entry, *new_entry; 3884 struct uvm_map_deadq dead; 3885 3886 vm_map_lock(old_map); 3887 3888 vm2 = uvmspace_alloc(old_map->min_offset, old_map->max_offset, 3889 (old_map->flags & VM_MAP_PAGEABLE) ? TRUE : FALSE, FALSE); 3890 memcpy(&vm2->vm_startcopy, &vm1->vm_startcopy, 3891 (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy); 3892 vm2->vm_dused = 0; /* Statistic managed by us. */ 3893 new_map = &vm2->vm_map; 3894 vm_map_lock(new_map); 3895 3896 /* go entry-by-entry */ 3897 TAILQ_INIT(&dead); 3898 RBT_FOREACH(old_entry, uvm_map_addr, &old_map->addr) { 3899 if (old_entry->start == old_entry->end) 3900 continue; 3901 3902 /* first, some sanity checks on the old entry */ 3903 if (UVM_ET_ISSUBMAP(old_entry)) { 3904 panic("fork: encountered a submap during fork " 3905 "(illegal)"); 3906 } 3907 3908 if (!UVM_ET_ISCOPYONWRITE(old_entry) && 3909 UVM_ET_ISNEEDSCOPY(old_entry)) { 3910 panic("fork: non-copy_on_write map entry marked " 3911 "needs_copy (illegal)"); 3912 } 3913 3914 /* Apply inheritance. */ 3915 switch (old_entry->inheritance) { 3916 case MAP_INHERIT_SHARE: 3917 new_entry = uvm_mapent_forkshared(vm2, new_map, 3918 old_map, old_entry, &dead); 3919 break; 3920 case MAP_INHERIT_COPY: 3921 new_entry = uvm_mapent_forkcopy(vm2, new_map, 3922 old_map, old_entry, &dead); 3923 break; 3924 case MAP_INHERIT_ZERO: 3925 new_entry = uvm_mapent_forkzero(vm2, new_map, 3926 old_map, old_entry, &dead); 3927 break; 3928 default: 3929 continue; 3930 } 3931 3932 /* Update process statistics. */ 3933 if (!UVM_ET_ISHOLE(new_entry)) 3934 new_map->size += new_entry->end - new_entry->start; 3935 if (!UVM_ET_ISOBJ(new_entry) && !UVM_ET_ISHOLE(new_entry) && 3936 new_entry->protection != PROT_NONE) { 3937 vm2->vm_dused += uvmspace_dused( 3938 new_map, new_entry->start, new_entry->end); 3939 } 3940 } 3941 new_map->flags |= old_map->flags & 3942 (VM_MAP_SYSCALL_ONCE | VM_MAP_PINSYSCALL_ONCE); 3943 #ifdef PMAP_CHECK_COPYIN 3944 if (PMAP_CHECK_COPYIN) { 3945 memcpy(&new_map->check_copyin, &old_map->check_copyin, 3946 sizeof(new_map->check_copyin)); 3947 membar_producer(); 3948 new_map->check_copyin_count = old_map->check_copyin_count; 3949 } 3950 #endif 3951 3952 vm_map_unlock(old_map); 3953 vm_map_unlock(new_map); 3954 3955 /* 3956 * This can actually happen, if multiple entries described a 3957 * space in which an entry was inherited. 3958 */ 3959 uvm_unmap_detach(&dead, 0); 3960 3961 #ifdef SYSVSHM 3962 if (vm1->vm_shm) 3963 shmfork(vm1, vm2); 3964 #endif 3965 3966 return vm2; 3967 } 3968 3969 /* 3970 * uvm_map_hint: return the beginning of the best area suitable for 3971 * creating a new mapping with "prot" protection. 3972 */ 3973 vaddr_t 3974 uvm_map_hint(struct vmspace *vm, vm_prot_t prot, vaddr_t minaddr, 3975 vaddr_t maxaddr) 3976 { 3977 vaddr_t addr; 3978 vaddr_t spacing; 3979 3980 #ifdef __i386__ 3981 /* 3982 * If executable skip first two pages, otherwise start 3983 * after data + heap region. 3984 */ 3985 if ((prot & PROT_EXEC) != 0 && 3986 (vaddr_t)vm->vm_daddr >= I386_MAX_EXE_ADDR) { 3987 addr = (PAGE_SIZE*2) + 3988 (arc4random() & (I386_MAX_EXE_ADDR / 2 - 1)); 3989 return (round_page(addr)); 3990 } 3991 #endif 3992 3993 #if defined (__LP64__) 3994 spacing = MIN(4UL * 1024 * 1024 * 1024, MAXDSIZ) - 1; 3995 #else 3996 spacing = MIN(1 * 1024 * 1024 * 1024, MAXDSIZ) - 1; 3997 #endif 3998 3999 /* 4000 * Start malloc/mmap after the brk. 4001 */ 4002 addr = (vaddr_t)vm->vm_daddr + BRKSIZ; 4003 addr = MAX(addr, minaddr); 4004 4005 if (addr < maxaddr) { 4006 while (spacing > maxaddr - addr) 4007 spacing >>= 1; 4008 } 4009 addr += arc4random() & spacing; 4010 return (round_page(addr)); 4011 } 4012 4013 /* 4014 * uvm_map_submap: punch down part of a map into a submap 4015 * 4016 * => only the kernel_map is allowed to be submapped 4017 * => the purpose of submapping is to break up the locking granularity 4018 * of a larger map 4019 * => the range specified must have been mapped previously with a uvm_map() 4020 * call [with uobj==NULL] to create a blank map entry in the main map. 4021 * [And it had better still be blank!] 4022 * => maps which contain submaps should never be copied or forked. 4023 * => to remove a submap, use uvm_unmap() on the main map 4024 * and then uvm_map_deallocate() the submap. 4025 * => main map must be unlocked. 4026 * => submap must have been init'd and have a zero reference count. 4027 * [need not be locked as we don't actually reference it] 4028 */ 4029 int 4030 uvm_map_submap(struct vm_map *map, vaddr_t start, vaddr_t end, 4031 struct vm_map *submap) 4032 { 4033 struct vm_map_entry *entry; 4034 int result; 4035 4036 if (start > map->max_offset || end > map->max_offset || 4037 start < map->min_offset || end < map->min_offset) 4038 return EINVAL; 4039 4040 vm_map_lock(map); 4041 4042 if (uvm_map_lookup_entry(map, start, &entry)) { 4043 UVM_MAP_CLIP_START(map, entry, start); 4044 UVM_MAP_CLIP_END(map, entry, end); 4045 } else 4046 entry = NULL; 4047 4048 if (entry != NULL && 4049 entry->start == start && entry->end == end && 4050 entry->object.uvm_obj == NULL && entry->aref.ar_amap == NULL && 4051 !UVM_ET_ISCOPYONWRITE(entry) && !UVM_ET_ISNEEDSCOPY(entry)) { 4052 entry->etype |= UVM_ET_SUBMAP; 4053 entry->object.sub_map = submap; 4054 entry->offset = 0; 4055 uvm_map_reference(submap); 4056 result = 0; 4057 } else 4058 result = EINVAL; 4059 4060 vm_map_unlock(map); 4061 return result; 4062 } 4063 4064 /* 4065 * uvm_map_checkprot: check protection in map 4066 * 4067 * => must allow specific protection in a fully allocated region. 4068 * => map must be read or write locked by caller. 4069 */ 4070 boolean_t 4071 uvm_map_checkprot(struct vm_map *map, vaddr_t start, vaddr_t end, 4072 vm_prot_t protection) 4073 { 4074 struct vm_map_entry *entry; 4075 4076 vm_map_assert_anylock(map); 4077 4078 if (start < map->min_offset || end > map->max_offset || start > end) 4079 return FALSE; 4080 if (start == end) 4081 return TRUE; 4082 4083 /* 4084 * Iterate entries. 4085 */ 4086 for (entry = uvm_map_entrybyaddr(&map->addr, start); 4087 entry != NULL && entry->start < end; 4088 entry = RBT_NEXT(uvm_map_addr, entry)) { 4089 /* Fail if a hole is found. */ 4090 if (UVM_ET_ISHOLE(entry) || 4091 (entry->end < end && entry->end != VMMAP_FREE_END(entry))) 4092 return FALSE; 4093 4094 /* Check protection. */ 4095 if ((entry->protection & protection) != protection) 4096 return FALSE; 4097 } 4098 return TRUE; 4099 } 4100 4101 /* 4102 * uvm_map_create: create map 4103 */ 4104 vm_map_t 4105 uvm_map_create(pmap_t pmap, vaddr_t min, vaddr_t max, int flags) 4106 { 4107 vm_map_t map; 4108 4109 map = malloc(sizeof *map, M_VMMAP, M_WAITOK); 4110 uvm_map_setup(map, pmap, min, max, flags); 4111 return (map); 4112 } 4113 4114 /* 4115 * uvm_map_deallocate: drop reference to a map 4116 * 4117 * => caller must not lock map 4118 * => we will zap map if ref count goes to zero 4119 */ 4120 void 4121 uvm_map_deallocate(vm_map_t map) 4122 { 4123 int c; 4124 struct uvm_map_deadq dead; 4125 4126 c = atomic_dec_int_nv(&map->ref_count); 4127 if (c > 0) { 4128 return; 4129 } 4130 4131 /* 4132 * all references gone. unmap and free. 4133 * 4134 * No lock required: we are only one to access this map. 4135 */ 4136 TAILQ_INIT(&dead); 4137 uvm_tree_sanity(map, __FILE__, __LINE__); 4138 vm_map_lock(map); 4139 uvm_unmap_remove(map, map->min_offset, map->max_offset, &dead, 4140 TRUE, FALSE, FALSE); 4141 vm_map_unlock(map); 4142 pmap_destroy(map->pmap); 4143 KASSERT(RBT_EMPTY(uvm_map_addr, &map->addr)); 4144 free(map, M_VMMAP, sizeof *map); 4145 4146 uvm_unmap_detach(&dead, 0); 4147 } 4148 4149 /* 4150 * uvm_map_inherit: set inheritance code for range of addrs in map. 4151 * 4152 * => map must be unlocked 4153 * => note that the inherit code is used during a "fork". see fork 4154 * code for details. 4155 */ 4156 int 4157 uvm_map_inherit(struct vm_map *map, vaddr_t start, vaddr_t end, 4158 vm_inherit_t new_inheritance) 4159 { 4160 struct vm_map_entry *entry, *entry1; 4161 int error = EPERM; 4162 4163 switch (new_inheritance) { 4164 case MAP_INHERIT_NONE: 4165 case MAP_INHERIT_COPY: 4166 case MAP_INHERIT_SHARE: 4167 case MAP_INHERIT_ZERO: 4168 break; 4169 default: 4170 return (EINVAL); 4171 } 4172 4173 if (start > end) 4174 return EINVAL; 4175 start = MAX(start, map->min_offset); 4176 end = MIN(end, map->max_offset); 4177 if (start >= end) 4178 return 0; 4179 4180 vm_map_lock(map); 4181 4182 entry = uvm_map_entrybyaddr(&map->addr, start); 4183 if (entry->end > start) 4184 UVM_MAP_CLIP_START(map, entry, start); 4185 else 4186 entry = RBT_NEXT(uvm_map_addr, entry); 4187 4188 /* First check for illegal operations */ 4189 entry1 = entry; 4190 while (entry1 != NULL && entry1->start < end) { 4191 if (entry1->etype & UVM_ET_IMMUTABLE) 4192 goto out; 4193 if (new_inheritance == MAP_INHERIT_ZERO && 4194 (entry1->protection & PROT_WRITE) == 0) 4195 goto out; 4196 entry1 = RBT_NEXT(uvm_map_addr, entry1); 4197 } 4198 4199 while (entry != NULL && entry->start < end) { 4200 UVM_MAP_CLIP_END(map, entry, end); 4201 entry->inheritance = new_inheritance; 4202 entry = RBT_NEXT(uvm_map_addr, entry); 4203 } 4204 4205 error = 0; 4206 out: 4207 vm_map_unlock(map); 4208 return (error); 4209 } 4210 4211 #ifdef PMAP_CHECK_COPYIN 4212 static void inline 4213 check_copyin_add(struct vm_map *map, vaddr_t start, vaddr_t end) 4214 { 4215 if (PMAP_CHECK_COPYIN == 0 || 4216 map->check_copyin_count >= UVM_MAP_CHECK_COPYIN_MAX) 4217 return; 4218 vm_map_assert_wrlock(map); 4219 map->check_copyin[map->check_copyin_count].start = start; 4220 map->check_copyin[map->check_copyin_count].end = end; 4221 membar_producer(); 4222 map->check_copyin_count++; 4223 } 4224 4225 /* 4226 * uvm_map_check_copyin_add: remember regions which are X-only for copyin(), 4227 * copyinstr(), uiomove(), and others 4228 * 4229 * => map must be unlocked 4230 */ 4231 int 4232 uvm_map_check_copyin_add(struct vm_map *map, vaddr_t start, vaddr_t end) 4233 { 4234 if (start > end) 4235 return EINVAL; 4236 start = MAX(start, map->min_offset); 4237 end = MIN(end, map->max_offset); 4238 if (start >= end) 4239 return 0; 4240 vm_map_lock(map); 4241 check_copyin_add(map, start, end); 4242 vm_map_unlock(map); 4243 return (0); 4244 } 4245 #endif /* PMAP_CHECK_COPYIN */ 4246 4247 /* 4248 * uvm_map_syscall: permit system calls for range of addrs in map. 4249 * 4250 * => map must be unlocked 4251 */ 4252 int 4253 uvm_map_syscall(struct vm_map *map, vaddr_t start, vaddr_t end) 4254 { 4255 struct vm_map_entry *entry; 4256 4257 if (start > end) 4258 return EINVAL; 4259 start = MAX(start, map->min_offset); 4260 end = MIN(end, map->max_offset); 4261 if (start >= end) 4262 return 0; 4263 if (map->flags & VM_MAP_SYSCALL_ONCE) /* only allowed once */ 4264 return (EPERM); 4265 4266 vm_map_lock(map); 4267 4268 entry = uvm_map_entrybyaddr(&map->addr, start); 4269 if (entry->end > start) 4270 UVM_MAP_CLIP_START(map, entry, start); 4271 else 4272 entry = RBT_NEXT(uvm_map_addr, entry); 4273 4274 while (entry != NULL && entry->start < end) { 4275 UVM_MAP_CLIP_END(map, entry, end); 4276 entry->etype |= UVM_ET_SYSCALL; 4277 entry = RBT_NEXT(uvm_map_addr, entry); 4278 } 4279 4280 #ifdef PMAP_CHECK_COPYIN 4281 check_copyin_add(map, start, end); /* Add libc's text segment */ 4282 #endif 4283 map->wserial++; 4284 map->flags |= VM_MAP_SYSCALL_ONCE; 4285 vm_map_unlock(map); 4286 return (0); 4287 } 4288 4289 /* 4290 * uvm_map_immutable: block mapping/mprotect for range of addrs in map. 4291 * 4292 * => map must be unlocked 4293 */ 4294 int 4295 uvm_map_immutable(struct vm_map *map, vaddr_t start, vaddr_t end, int imut) 4296 { 4297 struct vm_map_entry *entry, *entry1; 4298 int error = EPERM; 4299 4300 if (start > end) 4301 return EINVAL; 4302 start = MAX(start, map->min_offset); 4303 end = MIN(end, map->max_offset); 4304 if (start >= end) 4305 return 0; 4306 4307 vm_map_lock(map); 4308 4309 entry = uvm_map_entrybyaddr(&map->addr, start); 4310 if (entry->end > start) 4311 UVM_MAP_CLIP_START(map, entry, start); 4312 else 4313 entry = RBT_NEXT(uvm_map_addr, entry); 4314 4315 /* First check for illegal operations */ 4316 entry1 = entry; 4317 while (entry1 != NULL && entry1->start < end) { 4318 if (entry1->inheritance == MAP_INHERIT_ZERO) 4319 goto out; 4320 entry1 = RBT_NEXT(uvm_map_addr, entry1); 4321 } 4322 4323 while (entry != NULL && entry->start < end) { 4324 UVM_MAP_CLIP_END(map, entry, end); 4325 if (imut) 4326 entry->etype |= UVM_ET_IMMUTABLE; 4327 else 4328 entry->etype &= ~UVM_ET_IMMUTABLE; 4329 entry = RBT_NEXT(uvm_map_addr, entry); 4330 } 4331 4332 map->wserial++; 4333 error = 0; 4334 out: 4335 vm_map_unlock(map); 4336 return (0); 4337 } 4338 4339 /* 4340 * uvm_map_advice: set advice code for range of addrs in map. 4341 * 4342 * => map must be unlocked 4343 */ 4344 int 4345 uvm_map_advice(struct vm_map *map, vaddr_t start, vaddr_t end, int new_advice) 4346 { 4347 struct vm_map_entry *entry; 4348 4349 switch (new_advice) { 4350 case MADV_NORMAL: 4351 case MADV_RANDOM: 4352 case MADV_SEQUENTIAL: 4353 break; 4354 default: 4355 return (EINVAL); 4356 } 4357 4358 if (start > end) 4359 return EINVAL; 4360 start = MAX(start, map->min_offset); 4361 end = MIN(end, map->max_offset); 4362 if (start >= end) 4363 return 0; 4364 4365 vm_map_lock(map); 4366 4367 entry = uvm_map_entrybyaddr(&map->addr, start); 4368 if (entry != NULL && entry->end > start) 4369 UVM_MAP_CLIP_START(map, entry, start); 4370 else if (entry!= NULL) 4371 entry = RBT_NEXT(uvm_map_addr, entry); 4372 4373 /* 4374 * XXXJRT: disallow holes? 4375 */ 4376 while (entry != NULL && entry->start < end) { 4377 UVM_MAP_CLIP_END(map, entry, end); 4378 entry->advice = new_advice; 4379 entry = RBT_NEXT(uvm_map_addr, entry); 4380 } 4381 4382 vm_map_unlock(map); 4383 return (0); 4384 } 4385 4386 /* 4387 * uvm_map_extract: extract a mapping from a map and put it somewhere 4388 * in the kernel_map, setting protection to max_prot. 4389 * 4390 * => map should be unlocked (we will write lock it and kernel_map) 4391 * => returns 0 on success, error code otherwise 4392 * => start must be page aligned 4393 * => len must be page sized 4394 * => flags: 4395 * UVM_EXTRACT_FIXPROT: set prot to maxprot as we go 4396 * Mappings are QREF's. 4397 */ 4398 int 4399 uvm_map_extract(struct vm_map *srcmap, vaddr_t start, vsize_t len, 4400 vaddr_t *dstaddrp, int flags) 4401 { 4402 struct uvm_map_deadq dead; 4403 struct vm_map_entry *first, *entry, *newentry, *tmp1, *tmp2; 4404 vaddr_t dstaddr; 4405 vaddr_t end; 4406 vaddr_t cp_start; 4407 vsize_t cp_len, cp_off; 4408 int error; 4409 4410 TAILQ_INIT(&dead); 4411 end = start + len; 4412 4413 /* 4414 * Sanity check on the parameters. 4415 * Also, since the mapping may not contain gaps, error out if the 4416 * mapped area is not in source map. 4417 */ 4418 if ((start & (vaddr_t)PAGE_MASK) != 0 || 4419 (end & (vaddr_t)PAGE_MASK) != 0 || end < start) 4420 return EINVAL; 4421 if (start < srcmap->min_offset || end > srcmap->max_offset) 4422 return EINVAL; 4423 4424 /* Initialize dead entries. Handle len == 0 case. */ 4425 if (len == 0) 4426 return 0; 4427 4428 /* Acquire lock on srcmap. */ 4429 vm_map_lock(srcmap); 4430 4431 /* Lock srcmap, lookup first and last entry in <start,len>. */ 4432 first = uvm_map_entrybyaddr(&srcmap->addr, start); 4433 4434 /* Check that the range is contiguous. */ 4435 for (entry = first; entry != NULL && entry->end < end; 4436 entry = RBT_NEXT(uvm_map_addr, entry)) { 4437 if (VMMAP_FREE_END(entry) != entry->end || 4438 UVM_ET_ISHOLE(entry)) { 4439 error = EINVAL; 4440 goto fail; 4441 } 4442 } 4443 if (entry == NULL || UVM_ET_ISHOLE(entry)) { 4444 error = EINVAL; 4445 goto fail; 4446 } 4447 4448 /* 4449 * Handle need-copy flag. 4450 */ 4451 for (entry = first; entry != NULL && entry->start < end; 4452 entry = RBT_NEXT(uvm_map_addr, entry)) { 4453 if (UVM_ET_ISNEEDSCOPY(entry)) 4454 amap_copy(srcmap, entry, M_NOWAIT, 4455 UVM_ET_ISSTACK(entry) ? FALSE : TRUE, start, end); 4456 if (UVM_ET_ISNEEDSCOPY(entry)) { 4457 /* 4458 * amap_copy failure 4459 */ 4460 error = ENOMEM; 4461 goto fail; 4462 } 4463 } 4464 4465 /* Lock destination map (kernel_map). */ 4466 vm_map_lock(kernel_map); 4467 4468 if (uvm_map_findspace(kernel_map, &tmp1, &tmp2, &dstaddr, len, 4469 MAX(PAGE_SIZE, PMAP_PREFER_ALIGN()), PMAP_PREFER_OFFSET(start), 4470 PROT_NONE, 0) != 0) { 4471 error = ENOMEM; 4472 goto fail2; 4473 } 4474 *dstaddrp = dstaddr; 4475 4476 /* 4477 * We now have srcmap and kernel_map locked. 4478 * dstaddr contains the destination offset in dstmap. 4479 */ 4480 /* step 1: start looping through map entries, performing extraction. */ 4481 for (entry = first; entry != NULL && entry->start < end; 4482 entry = RBT_NEXT(uvm_map_addr, entry)) { 4483 KDASSERT(!UVM_ET_ISNEEDSCOPY(entry)); 4484 if (UVM_ET_ISHOLE(entry)) 4485 continue; 4486 4487 /* Calculate uvm_mapent_clone parameters. */ 4488 cp_start = entry->start; 4489 if (cp_start < start) { 4490 cp_off = start - cp_start; 4491 cp_start = start; 4492 } else 4493 cp_off = 0; 4494 cp_len = MIN(entry->end, end) - cp_start; 4495 4496 newentry = uvm_mapent_clone(kernel_map, 4497 cp_start - start + dstaddr, cp_len, cp_off, 4498 entry->protection, entry->max_protection, 4499 entry, &dead, flags, AMAP_SHARED | AMAP_REFALL); 4500 if (newentry == NULL) { 4501 error = ENOMEM; 4502 goto fail2_unmap; 4503 } 4504 kernel_map->size += cp_len; 4505 4506 /* Figure out the best protection */ 4507 if ((flags & UVM_EXTRACT_FIXPROT) && 4508 newentry->protection != PROT_NONE) 4509 newentry->protection = newentry->max_protection; 4510 newentry->protection &= ~PROT_EXEC; 4511 } 4512 pmap_update(kernel_map->pmap); 4513 4514 error = 0; 4515 4516 /* Unmap copied entries on failure. */ 4517 fail2_unmap: 4518 if (error) { 4519 uvm_unmap_remove(kernel_map, dstaddr, dstaddr + len, &dead, 4520 FALSE, TRUE, FALSE); 4521 } 4522 4523 /* Release maps, release dead entries. */ 4524 fail2: 4525 vm_map_unlock(kernel_map); 4526 4527 fail: 4528 vm_map_unlock(srcmap); 4529 4530 uvm_unmap_detach(&dead, 0); 4531 4532 return error; 4533 } 4534 4535 /* 4536 * uvm_map_clean: clean out a map range 4537 * 4538 * => valid flags: 4539 * if (flags & PGO_CLEANIT): dirty pages are cleaned first 4540 * if (flags & PGO_SYNCIO): dirty pages are written synchronously 4541 * if (flags & PGO_DEACTIVATE): any cached pages are deactivated after clean 4542 * if (flags & PGO_FREE): any cached pages are freed after clean 4543 * => returns an error if any part of the specified range isn't mapped 4544 * => never a need to flush amap layer since the anonymous memory has 4545 * no permanent home, but may deactivate pages there 4546 * => called from sys_msync() and sys_madvise() 4547 * => caller must not have map locked 4548 */ 4549 4550 int 4551 uvm_map_clean(struct vm_map *map, vaddr_t start, vaddr_t end, int flags) 4552 { 4553 struct vm_map_entry *first, *entry; 4554 struct vm_amap *amap; 4555 struct vm_anon *anon; 4556 struct vm_page *pg; 4557 struct uvm_object *uobj; 4558 vaddr_t cp_start, cp_end; 4559 int refs; 4560 int error; 4561 boolean_t rv; 4562 4563 KASSERT((flags & (PGO_FREE|PGO_DEACTIVATE)) != 4564 (PGO_FREE|PGO_DEACTIVATE)); 4565 4566 if (start > end || start < map->min_offset || end > map->max_offset) 4567 return EINVAL; 4568 4569 vm_map_lock(map); 4570 first = uvm_map_entrybyaddr(&map->addr, start); 4571 4572 /* Make a first pass to check for various conditions. */ 4573 for (entry = first; entry != NULL && entry->start < end; 4574 entry = RBT_NEXT(uvm_map_addr, entry)) { 4575 if (entry->etype & UVM_ET_IMMUTABLE) { 4576 vm_map_unlock(map); 4577 return EPERM; 4578 } 4579 if (UVM_ET_ISSUBMAP(entry)) { 4580 vm_map_unlock(map); 4581 return EINVAL; 4582 } 4583 if (UVM_ET_ISSUBMAP(entry) || 4584 UVM_ET_ISHOLE(entry) || 4585 (entry->end < end && 4586 VMMAP_FREE_END(entry) != entry->end)) { 4587 vm_map_unlock(map); 4588 return EFAULT; 4589 } 4590 } 4591 4592 vm_map_busy(map); 4593 vm_map_unlock(map); 4594 error = 0; 4595 for (entry = first; entry != NULL && entry->start < end; 4596 entry = RBT_NEXT(uvm_map_addr, entry)) { 4597 amap = entry->aref.ar_amap; /* top layer */ 4598 if (UVM_ET_ISOBJ(entry)) 4599 uobj = entry->object.uvm_obj; 4600 else 4601 uobj = NULL; 4602 4603 /* 4604 * No amap cleaning necessary if: 4605 * - there's no amap 4606 * - we're not deactivating or freeing pages. 4607 */ 4608 if (amap == NULL || (flags & (PGO_DEACTIVATE|PGO_FREE)) == 0) 4609 goto flush_object; 4610 4611 cp_start = MAX(entry->start, start); 4612 cp_end = MIN(entry->end, end); 4613 4614 amap_lock(amap); 4615 for (; cp_start != cp_end; cp_start += PAGE_SIZE) { 4616 anon = amap_lookup(&entry->aref, 4617 cp_start - entry->start); 4618 if (anon == NULL) 4619 continue; 4620 4621 KASSERT(anon->an_lock == amap->am_lock); 4622 pg = anon->an_page; 4623 if (pg == NULL) { 4624 continue; 4625 } 4626 KASSERT(pg->pg_flags & PQ_ANON); 4627 4628 switch (flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE)) { 4629 /* 4630 * XXX In these first 3 cases, we always just 4631 * XXX deactivate the page. We may want to 4632 * XXX handle the different cases more 4633 * XXX specifically, in the future. 4634 */ 4635 case PGO_CLEANIT|PGO_FREE: 4636 case PGO_CLEANIT|PGO_DEACTIVATE: 4637 case PGO_DEACTIVATE: 4638 deactivate_it: 4639 /* skip the page if it's wired */ 4640 if (pg->wire_count != 0) 4641 break; 4642 4643 uvm_lock_pageq(); 4644 4645 KASSERT(pg->uanon == anon); 4646 4647 /* zap all mappings for the page. */ 4648 pmap_page_protect(pg, PROT_NONE); 4649 4650 /* ...and deactivate the page. */ 4651 uvm_pagedeactivate(pg); 4652 4653 uvm_unlock_pageq(); 4654 break; 4655 case PGO_FREE: 4656 /* 4657 * If there are multiple references to 4658 * the amap, just deactivate the page. 4659 */ 4660 if (amap_refs(amap) > 1) 4661 goto deactivate_it; 4662 4663 /* XXX skip the page if it's wired */ 4664 if (pg->wire_count != 0) { 4665 break; 4666 } 4667 amap_unadd(&entry->aref, 4668 cp_start - entry->start); 4669 refs = --anon->an_ref; 4670 if (refs == 0) 4671 uvm_anfree(anon); 4672 break; 4673 default: 4674 panic("uvm_map_clean: weird flags"); 4675 } 4676 } 4677 amap_unlock(amap); 4678 4679 flush_object: 4680 cp_start = MAX(entry->start, start); 4681 cp_end = MIN(entry->end, end); 4682 4683 /* 4684 * flush pages if we've got a valid backing object. 4685 * 4686 * Don't PGO_FREE if we don't have write permission 4687 * and don't flush if this is a copy-on-write object 4688 * since we can't know our permissions on it. 4689 */ 4690 if (uobj != NULL && 4691 ((flags & PGO_FREE) == 0 || 4692 ((entry->max_protection & PROT_WRITE) != 0 && 4693 (entry->etype & UVM_ET_COPYONWRITE) == 0))) { 4694 rw_enter(uobj->vmobjlock, RW_WRITE); 4695 rv = uobj->pgops->pgo_flush(uobj, 4696 cp_start - entry->start + entry->offset, 4697 cp_end - entry->start + entry->offset, flags); 4698 rw_exit(uobj->vmobjlock); 4699 4700 if (rv == FALSE) 4701 error = EFAULT; 4702 } 4703 } 4704 4705 vm_map_unbusy(map); 4706 return error; 4707 } 4708 4709 /* 4710 * UVM_MAP_CLIP_END implementation 4711 */ 4712 void 4713 uvm_map_clip_end(struct vm_map *map, struct vm_map_entry *entry, vaddr_t addr) 4714 { 4715 struct vm_map_entry *tmp; 4716 4717 KASSERT(entry->start < addr && VMMAP_FREE_END(entry) > addr); 4718 tmp = uvm_mapent_alloc(map, 0); 4719 4720 /* Invoke splitentry. */ 4721 uvm_map_splitentry(map, entry, tmp, addr); 4722 } 4723 4724 /* 4725 * UVM_MAP_CLIP_START implementation 4726 * 4727 * Clippers are required to not change the pointers to the entry they are 4728 * clipping on. 4729 * Since uvm_map_splitentry turns the original entry into the lowest 4730 * entry (address wise) we do a swap between the new entry and the original 4731 * entry, prior to calling uvm_map_splitentry. 4732 */ 4733 void 4734 uvm_map_clip_start(struct vm_map *map, struct vm_map_entry *entry, vaddr_t addr) 4735 { 4736 struct vm_map_entry *tmp; 4737 struct uvm_addr_state *free; 4738 4739 /* Unlink original. */ 4740 free = uvm_map_uaddr_e(map, entry); 4741 uvm_mapent_free_remove(map, free, entry); 4742 uvm_mapent_addr_remove(map, entry); 4743 4744 /* Copy entry. */ 4745 KASSERT(entry->start < addr && VMMAP_FREE_END(entry) > addr); 4746 tmp = uvm_mapent_alloc(map, 0); 4747 uvm_mapent_copy(entry, tmp); 4748 4749 /* Put new entry in place of original entry. */ 4750 uvm_mapent_addr_insert(map, tmp); 4751 uvm_mapent_free_insert(map, free, tmp); 4752 4753 /* Invoke splitentry. */ 4754 uvm_map_splitentry(map, tmp, entry, addr); 4755 } 4756 4757 /* 4758 * Boundary fixer. 4759 */ 4760 static inline vaddr_t uvm_map_boundfix(vaddr_t, vaddr_t, vaddr_t); 4761 static inline vaddr_t 4762 uvm_map_boundfix(vaddr_t min, vaddr_t max, vaddr_t bound) 4763 { 4764 return (min < bound && max > bound) ? bound : max; 4765 } 4766 4767 /* 4768 * Choose free list based on address at start of free space. 4769 * 4770 * The uvm_addr_state returned contains addr and is the first of: 4771 * - uaddr_exe 4772 * - uaddr_brk_stack 4773 * - uaddr_any 4774 */ 4775 struct uvm_addr_state* 4776 uvm_map_uaddr(struct vm_map *map, vaddr_t addr) 4777 { 4778 struct uvm_addr_state *uaddr; 4779 int i; 4780 4781 /* Special case the first page, to prevent mmap from returning 0. */ 4782 if (addr < VMMAP_MIN_ADDR) 4783 return NULL; 4784 4785 /* Upper bound for kernel maps at uvm_maxkaddr. */ 4786 if ((map->flags & VM_MAP_ISVMSPACE) == 0) { 4787 if (addr >= uvm_maxkaddr) 4788 return NULL; 4789 } 4790 4791 /* Is the address inside the exe-only map? */ 4792 if (map->uaddr_exe != NULL && addr >= map->uaddr_exe->uaddr_minaddr && 4793 addr < map->uaddr_exe->uaddr_maxaddr) 4794 return map->uaddr_exe; 4795 4796 /* Check if the space falls inside brk/stack area. */ 4797 if ((addr >= map->b_start && addr < map->b_end) || 4798 (addr >= map->s_start && addr < map->s_end)) { 4799 if (map->uaddr_brk_stack != NULL && 4800 addr >= map->uaddr_brk_stack->uaddr_minaddr && 4801 addr < map->uaddr_brk_stack->uaddr_maxaddr) { 4802 return map->uaddr_brk_stack; 4803 } else 4804 return NULL; 4805 } 4806 4807 /* 4808 * Check the other selectors. 4809 * 4810 * These selectors are only marked as the owner, if they have insert 4811 * functions. 4812 */ 4813 for (i = 0; i < nitems(map->uaddr_any); i++) { 4814 uaddr = map->uaddr_any[i]; 4815 if (uaddr == NULL) 4816 continue; 4817 if (uaddr->uaddr_functions->uaddr_free_insert == NULL) 4818 continue; 4819 4820 if (addr >= uaddr->uaddr_minaddr && 4821 addr < uaddr->uaddr_maxaddr) 4822 return uaddr; 4823 } 4824 4825 return NULL; 4826 } 4827 4828 /* 4829 * Choose free list based on address at start of free space. 4830 * 4831 * The uvm_addr_state returned contains addr and is the first of: 4832 * - uaddr_exe 4833 * - uaddr_brk_stack 4834 * - uaddr_any 4835 */ 4836 struct uvm_addr_state* 4837 uvm_map_uaddr_e(struct vm_map *map, struct vm_map_entry *entry) 4838 { 4839 return uvm_map_uaddr(map, VMMAP_FREE_START(entry)); 4840 } 4841 4842 /* 4843 * Returns the first free-memory boundary that is crossed by [min-max]. 4844 */ 4845 vsize_t 4846 uvm_map_boundary(struct vm_map *map, vaddr_t min, vaddr_t max) 4847 { 4848 struct uvm_addr_state *uaddr; 4849 int i; 4850 4851 /* Never return first page. */ 4852 max = uvm_map_boundfix(min, max, VMMAP_MIN_ADDR); 4853 4854 /* Treat the maxkaddr special, if the map is a kernel_map. */ 4855 if ((map->flags & VM_MAP_ISVMSPACE) == 0) 4856 max = uvm_map_boundfix(min, max, uvm_maxkaddr); 4857 4858 /* Check for exe-only boundaries. */ 4859 if (map->uaddr_exe != NULL) { 4860 max = uvm_map_boundfix(min, max, map->uaddr_exe->uaddr_minaddr); 4861 max = uvm_map_boundfix(min, max, map->uaddr_exe->uaddr_maxaddr); 4862 } 4863 4864 /* Check for exe-only boundaries. */ 4865 if (map->uaddr_brk_stack != NULL) { 4866 max = uvm_map_boundfix(min, max, 4867 map->uaddr_brk_stack->uaddr_minaddr); 4868 max = uvm_map_boundfix(min, max, 4869 map->uaddr_brk_stack->uaddr_maxaddr); 4870 } 4871 4872 /* Check other boundaries. */ 4873 for (i = 0; i < nitems(map->uaddr_any); i++) { 4874 uaddr = map->uaddr_any[i]; 4875 if (uaddr != NULL) { 4876 max = uvm_map_boundfix(min, max, uaddr->uaddr_minaddr); 4877 max = uvm_map_boundfix(min, max, uaddr->uaddr_maxaddr); 4878 } 4879 } 4880 4881 /* Boundaries at stack and brk() area. */ 4882 max = uvm_map_boundfix(min, max, map->s_start); 4883 max = uvm_map_boundfix(min, max, map->s_end); 4884 max = uvm_map_boundfix(min, max, map->b_start); 4885 max = uvm_map_boundfix(min, max, map->b_end); 4886 4887 return max; 4888 } 4889 4890 /* 4891 * Update map allocation start and end addresses from proc vmspace. 4892 */ 4893 void 4894 uvm_map_vmspace_update(struct vm_map *map, 4895 struct uvm_map_deadq *dead, int flags) 4896 { 4897 struct vmspace *vm; 4898 vaddr_t b_start, b_end, s_start, s_end; 4899 4900 KASSERT(map->flags & VM_MAP_ISVMSPACE); 4901 KASSERT(offsetof(struct vmspace, vm_map) == 0); 4902 4903 /* 4904 * Derive actual allocation boundaries from vmspace. 4905 */ 4906 vm = (struct vmspace *)map; 4907 b_start = (vaddr_t)vm->vm_daddr; 4908 b_end = b_start + BRKSIZ; 4909 s_start = MIN((vaddr_t)vm->vm_maxsaddr, (vaddr_t)vm->vm_minsaddr); 4910 s_end = MAX((vaddr_t)vm->vm_maxsaddr, (vaddr_t)vm->vm_minsaddr); 4911 #ifdef DIAGNOSTIC 4912 if ((b_start & (vaddr_t)PAGE_MASK) != 0 || 4913 (b_end & (vaddr_t)PAGE_MASK) != 0 || 4914 (s_start & (vaddr_t)PAGE_MASK) != 0 || 4915 (s_end & (vaddr_t)PAGE_MASK) != 0) { 4916 panic("uvm_map_vmspace_update: vmspace %p invalid bounds: " 4917 "b=0x%lx-0x%lx s=0x%lx-0x%lx", 4918 vm, b_start, b_end, s_start, s_end); 4919 } 4920 #endif 4921 4922 if (__predict_true(map->b_start == b_start && map->b_end == b_end && 4923 map->s_start == s_start && map->s_end == s_end)) 4924 return; 4925 4926 uvm_map_freelist_update(map, dead, b_start, b_end, 4927 s_start, s_end, flags); 4928 } 4929 4930 /* 4931 * Grow kernel memory. 4932 * 4933 * This function is only called for kernel maps when an allocation fails. 4934 * 4935 * If the map has a gap that is large enough to accommodate alloc_sz, this 4936 * function will make sure map->free will include it. 4937 */ 4938 void 4939 uvm_map_kmem_grow(struct vm_map *map, struct uvm_map_deadq *dead, 4940 vsize_t alloc_sz, int flags) 4941 { 4942 vsize_t sz; 4943 vaddr_t end; 4944 struct vm_map_entry *entry; 4945 4946 /* Kernel memory only. */ 4947 KASSERT((map->flags & VM_MAP_ISVMSPACE) == 0); 4948 /* Destroy free list. */ 4949 uvm_map_freelist_update_clear(map, dead); 4950 4951 /* Include the guard page in the hard minimum requirement of alloc_sz. */ 4952 if (map->flags & VM_MAP_GUARDPAGES) 4953 alloc_sz += PAGE_SIZE; 4954 4955 /* 4956 * Grow by ALLOCMUL * alloc_sz, but at least VM_MAP_KSIZE_DELTA. 4957 * 4958 * Don't handle the case where the multiplication overflows: 4959 * if that happens, the allocation is probably too big anyway. 4960 */ 4961 sz = MAX(VM_MAP_KSIZE_ALLOCMUL * alloc_sz, VM_MAP_KSIZE_DELTA); 4962 4963 /* 4964 * Walk forward until a gap large enough for alloc_sz shows up. 4965 * 4966 * We assume the kernel map has no boundaries. 4967 * uvm_maxkaddr may be zero. 4968 */ 4969 end = MAX(uvm_maxkaddr, map->min_offset); 4970 entry = uvm_map_entrybyaddr(&map->addr, end); 4971 while (entry && entry->fspace < alloc_sz) 4972 entry = RBT_NEXT(uvm_map_addr, entry); 4973 if (entry) { 4974 end = MAX(VMMAP_FREE_START(entry), end); 4975 end += MIN(sz, map->max_offset - end); 4976 } else 4977 end = map->max_offset; 4978 4979 /* Reserve pmap entries. */ 4980 #ifdef PMAP_GROWKERNEL 4981 uvm_maxkaddr = pmap_growkernel(end); 4982 #else 4983 uvm_maxkaddr = MAX(uvm_maxkaddr, end); 4984 #endif 4985 4986 /* Rebuild free list. */ 4987 uvm_map_freelist_update_refill(map, flags); 4988 } 4989 4990 /* 4991 * Freelist update subfunction: unlink all entries from freelists. 4992 */ 4993 void 4994 uvm_map_freelist_update_clear(struct vm_map *map, struct uvm_map_deadq *dead) 4995 { 4996 struct uvm_addr_state *free; 4997 struct vm_map_entry *entry, *prev, *next; 4998 4999 prev = NULL; 5000 for (entry = RBT_MIN(uvm_map_addr, &map->addr); entry != NULL; 5001 entry = next) { 5002 next = RBT_NEXT(uvm_map_addr, entry); 5003 5004 free = uvm_map_uaddr_e(map, entry); 5005 uvm_mapent_free_remove(map, free, entry); 5006 5007 if (prev != NULL && entry->start == entry->end) { 5008 prev->fspace += VMMAP_FREE_END(entry) - entry->end; 5009 uvm_mapent_addr_remove(map, entry); 5010 DEAD_ENTRY_PUSH(dead, entry); 5011 } else 5012 prev = entry; 5013 } 5014 } 5015 5016 /* 5017 * Freelist update subfunction: refill the freelists with entries. 5018 */ 5019 void 5020 uvm_map_freelist_update_refill(struct vm_map *map, int flags) 5021 { 5022 struct vm_map_entry *entry; 5023 vaddr_t min, max; 5024 5025 RBT_FOREACH(entry, uvm_map_addr, &map->addr) { 5026 min = VMMAP_FREE_START(entry); 5027 max = VMMAP_FREE_END(entry); 5028 entry->fspace = 0; 5029 5030 entry = uvm_map_fix_space(map, entry, min, max, flags); 5031 } 5032 5033 uvm_tree_sanity(map, __FILE__, __LINE__); 5034 } 5035 5036 /* 5037 * Change {a,b}_{start,end} allocation ranges and associated free lists. 5038 */ 5039 void 5040 uvm_map_freelist_update(struct vm_map *map, struct uvm_map_deadq *dead, 5041 vaddr_t b_start, vaddr_t b_end, vaddr_t s_start, vaddr_t s_end, int flags) 5042 { 5043 KDASSERT(b_end >= b_start && s_end >= s_start); 5044 vm_map_assert_wrlock(map); 5045 5046 /* Clear all free lists. */ 5047 uvm_map_freelist_update_clear(map, dead); 5048 5049 /* Apply new bounds. */ 5050 map->b_start = b_start; 5051 map->b_end = b_end; 5052 map->s_start = s_start; 5053 map->s_end = s_end; 5054 5055 /* Refill free lists. */ 5056 uvm_map_freelist_update_refill(map, flags); 5057 } 5058 5059 /* 5060 * Assign a uvm_addr_state to the specified pointer in vm_map. 5061 * 5062 * May sleep. 5063 */ 5064 void 5065 uvm_map_set_uaddr(struct vm_map *map, struct uvm_addr_state **which, 5066 struct uvm_addr_state *newval) 5067 { 5068 struct uvm_map_deadq dead; 5069 5070 /* Pointer which must be in this map. */ 5071 KASSERT(which != NULL); 5072 KASSERT((void*)map <= (void*)(which) && 5073 (void*)(which) < (void*)(map + 1)); 5074 5075 vm_map_lock(map); 5076 TAILQ_INIT(&dead); 5077 uvm_map_freelist_update_clear(map, &dead); 5078 5079 uvm_addr_destroy(*which); 5080 *which = newval; 5081 5082 uvm_map_freelist_update_refill(map, 0); 5083 vm_map_unlock(map); 5084 uvm_unmap_detach(&dead, 0); 5085 } 5086 5087 /* 5088 * Correct space insert. 5089 * 5090 * Entry must not be on any freelist. 5091 */ 5092 struct vm_map_entry* 5093 uvm_map_fix_space(struct vm_map *map, struct vm_map_entry *entry, 5094 vaddr_t min, vaddr_t max, int flags) 5095 { 5096 struct uvm_addr_state *free, *entfree; 5097 vaddr_t lmax; 5098 5099 KASSERT(entry == NULL || (entry->etype & UVM_ET_FREEMAPPED) == 0); 5100 KDASSERT(min <= max); 5101 KDASSERT((entry != NULL && VMMAP_FREE_END(entry) == min) || 5102 min == map->min_offset); 5103 5104 UVM_MAP_REQ_WRITE(map); 5105 5106 /* 5107 * During the function, entfree will always point at the uaddr state 5108 * for entry. 5109 */ 5110 entfree = (entry == NULL ? NULL : 5111 uvm_map_uaddr_e(map, entry)); 5112 5113 while (min != max) { 5114 /* Claim guard page for entry. */ 5115 if ((map->flags & VM_MAP_GUARDPAGES) && entry != NULL && 5116 VMMAP_FREE_END(entry) == entry->end && 5117 entry->start != entry->end) { 5118 if (max - min == 2 * PAGE_SIZE) { 5119 /* 5120 * If the free-space gap is exactly 2 pages, 5121 * we make the guard 2 pages instead of 1. 5122 * Because in a guarded map, an area needs 5123 * at least 2 pages to allocate from: 5124 * one page for the allocation and one for 5125 * the guard. 5126 */ 5127 entry->guard = 2 * PAGE_SIZE; 5128 min = max; 5129 } else { 5130 entry->guard = PAGE_SIZE; 5131 min += PAGE_SIZE; 5132 } 5133 continue; 5134 } 5135 5136 /* 5137 * Handle the case where entry has a 2-page guard, but the 5138 * space after entry is freed. 5139 */ 5140 if (entry != NULL && entry->fspace == 0 && 5141 entry->guard > PAGE_SIZE) { 5142 entry->guard = PAGE_SIZE; 5143 min = VMMAP_FREE_START(entry); 5144 } 5145 5146 lmax = uvm_map_boundary(map, min, max); 5147 free = uvm_map_uaddr(map, min); 5148 5149 /* 5150 * Entries are merged if they point at the same uvm_free(). 5151 * Exception to that rule: if min == uvm_maxkaddr, a new 5152 * entry is started regardless (otherwise the allocators 5153 * will get confused). 5154 */ 5155 if (entry != NULL && free == entfree && 5156 !((map->flags & VM_MAP_ISVMSPACE) == 0 && 5157 min == uvm_maxkaddr)) { 5158 KDASSERT(VMMAP_FREE_END(entry) == min); 5159 entry->fspace += lmax - min; 5160 } else { 5161 /* 5162 * Commit entry to free list: it'll not be added to 5163 * anymore. 5164 * We'll start a new entry and add to that entry 5165 * instead. 5166 */ 5167 if (entry != NULL) 5168 uvm_mapent_free_insert(map, entfree, entry); 5169 5170 /* New entry for new uaddr. */ 5171 entry = uvm_mapent_alloc(map, flags); 5172 KDASSERT(entry != NULL); 5173 entry->end = entry->start = min; 5174 entry->guard = 0; 5175 entry->fspace = lmax - min; 5176 entry->object.uvm_obj = NULL; 5177 entry->offset = 0; 5178 entry->etype = 0; 5179 entry->protection = entry->max_protection = 0; 5180 entry->inheritance = 0; 5181 entry->wired_count = 0; 5182 entry->advice = 0; 5183 entry->aref.ar_pageoff = 0; 5184 entry->aref.ar_amap = NULL; 5185 uvm_mapent_addr_insert(map, entry); 5186 5187 entfree = free; 5188 } 5189 5190 min = lmax; 5191 } 5192 /* Finally put entry on the uaddr state. */ 5193 if (entry != NULL) 5194 uvm_mapent_free_insert(map, entfree, entry); 5195 5196 return entry; 5197 } 5198 5199 /* 5200 * MQuery style of allocation. 5201 * 5202 * This allocator searches forward until sufficient space is found to map 5203 * the given size. 5204 * 5205 * XXX: factor in offset (via pmap_prefer) and protection? 5206 */ 5207 int 5208 uvm_map_mquery(struct vm_map *map, vaddr_t *addr_p, vsize_t sz, voff_t offset, 5209 int flags) 5210 { 5211 struct vm_map_entry *entry, *last; 5212 vaddr_t addr; 5213 vaddr_t tmp, pmap_align, pmap_offset; 5214 int error; 5215 5216 addr = *addr_p; 5217 vm_map_lock_read(map); 5218 5219 /* Configure pmap prefer. */ 5220 if (offset != UVM_UNKNOWN_OFFSET) { 5221 pmap_align = MAX(PAGE_SIZE, PMAP_PREFER_ALIGN()); 5222 pmap_offset = PMAP_PREFER_OFFSET(offset); 5223 } else { 5224 pmap_align = PAGE_SIZE; 5225 pmap_offset = 0; 5226 } 5227 5228 /* Align address to pmap_prefer unless FLAG_FIXED is set. */ 5229 if (!(flags & UVM_FLAG_FIXED) && offset != UVM_UNKNOWN_OFFSET) { 5230 tmp = (addr & ~(pmap_align - 1)) | pmap_offset; 5231 if (tmp < addr) 5232 tmp += pmap_align; 5233 addr = tmp; 5234 } 5235 5236 /* First, check if the requested range is fully available. */ 5237 entry = uvm_map_entrybyaddr(&map->addr, addr); 5238 last = NULL; 5239 if (uvm_map_isavail(map, NULL, &entry, &last, addr, sz)) { 5240 error = 0; 5241 goto out; 5242 } 5243 if (flags & UVM_FLAG_FIXED) { 5244 error = EINVAL; 5245 goto out; 5246 } 5247 5248 error = ENOMEM; /* Default error from here. */ 5249 5250 /* 5251 * At this point, the memory at <addr, sz> is not available. 5252 * The reasons are: 5253 * [1] it's outside the map, 5254 * [2] it starts in used memory (and therefore needs to move 5255 * toward the first free page in entry), 5256 * [3] it starts in free memory but bumps into used memory. 5257 * 5258 * Note that for case [2], the forward moving is handled by the 5259 * for loop below. 5260 */ 5261 if (entry == NULL) { 5262 /* [1] Outside the map. */ 5263 if (addr >= map->max_offset) 5264 goto out; 5265 else 5266 entry = RBT_MIN(uvm_map_addr, &map->addr); 5267 } else if (VMMAP_FREE_START(entry) <= addr) { 5268 /* [3] Bumped into used memory. */ 5269 entry = RBT_NEXT(uvm_map_addr, entry); 5270 } 5271 5272 /* Test if the next entry is sufficient for the allocation. */ 5273 for (; entry != NULL; 5274 entry = RBT_NEXT(uvm_map_addr, entry)) { 5275 if (entry->fspace == 0) 5276 continue; 5277 addr = VMMAP_FREE_START(entry); 5278 5279 restart: /* Restart address checks on address change. */ 5280 tmp = (addr & ~(pmap_align - 1)) | pmap_offset; 5281 if (tmp < addr) 5282 tmp += pmap_align; 5283 addr = tmp; 5284 if (addr >= VMMAP_FREE_END(entry)) 5285 continue; 5286 5287 /* Skip brk() allocation addresses. */ 5288 if (addr + sz > map->b_start && addr < map->b_end) { 5289 if (VMMAP_FREE_END(entry) > map->b_end) { 5290 addr = map->b_end; 5291 goto restart; 5292 } else 5293 continue; 5294 } 5295 /* Skip stack allocation addresses. */ 5296 if (addr + sz > map->s_start && addr < map->s_end) { 5297 if (VMMAP_FREE_END(entry) > map->s_end) { 5298 addr = map->s_end; 5299 goto restart; 5300 } else 5301 continue; 5302 } 5303 5304 last = NULL; 5305 if (uvm_map_isavail(map, NULL, &entry, &last, addr, sz)) { 5306 error = 0; 5307 goto out; 5308 } 5309 } 5310 5311 out: 5312 vm_map_unlock_read(map); 5313 if (error == 0) 5314 *addr_p = addr; 5315 return error; 5316 } 5317 5318 boolean_t 5319 vm_map_lock_try_ln(struct vm_map *map, char *file, int line) 5320 { 5321 boolean_t rv; 5322 5323 if (map->flags & VM_MAP_INTRSAFE) { 5324 rv = mtx_enter_try(&map->mtx); 5325 } else { 5326 mtx_enter(&map->flags_lock); 5327 if ((map->flags & VM_MAP_BUSY) && (map->busy != curproc)) { 5328 mtx_leave(&map->flags_lock); 5329 return (FALSE); 5330 } 5331 mtx_leave(&map->flags_lock); 5332 rv = (rw_enter(&map->lock, RW_WRITE|RW_NOSLEEP) == 0); 5333 /* check if the lock is busy and back out if we won the race */ 5334 if (rv) { 5335 mtx_enter(&map->flags_lock); 5336 if ((map->flags & VM_MAP_BUSY) && 5337 (map->busy != curproc)) { 5338 rw_exit(&map->lock); 5339 rv = FALSE; 5340 } 5341 mtx_leave(&map->flags_lock); 5342 } 5343 } 5344 5345 if (rv) { 5346 map->timestamp++; 5347 LPRINTF(("map lock: %p (at %s %d)\n", map, file, line)); 5348 uvm_tree_sanity(map, file, line); 5349 uvm_tree_size_chk(map, file, line); 5350 } 5351 5352 return (rv); 5353 } 5354 5355 void 5356 vm_map_lock_ln(struct vm_map *map, char *file, int line) 5357 { 5358 if ((map->flags & VM_MAP_INTRSAFE) == 0) { 5359 do { 5360 mtx_enter(&map->flags_lock); 5361 tryagain: 5362 while ((map->flags & VM_MAP_BUSY) && 5363 (map->busy != curproc)) { 5364 map->flags |= VM_MAP_WANTLOCK; 5365 msleep_nsec(&map->flags, &map->flags_lock, 5366 PVM, vmmapbsy, INFSLP); 5367 } 5368 mtx_leave(&map->flags_lock); 5369 } while (rw_enter(&map->lock, RW_WRITE|RW_SLEEPFAIL) != 0); 5370 /* check if the lock is busy and back out if we won the race */ 5371 mtx_enter(&map->flags_lock); 5372 if ((map->flags & VM_MAP_BUSY) && (map->busy != curproc)) { 5373 rw_exit(&map->lock); 5374 goto tryagain; 5375 } 5376 mtx_leave(&map->flags_lock); 5377 } else { 5378 mtx_enter(&map->mtx); 5379 } 5380 5381 if (map->busy != curproc) 5382 map->timestamp++; 5383 LPRINTF(("map lock: %p (at %s %d)\n", map, file, line)); 5384 uvm_tree_sanity(map, file, line); 5385 uvm_tree_size_chk(map, file, line); 5386 } 5387 5388 void 5389 vm_map_lock_read_ln(struct vm_map *map, char *file, int line) 5390 { 5391 if ((map->flags & VM_MAP_INTRSAFE) == 0) 5392 rw_enter_read(&map->lock); 5393 else 5394 mtx_enter(&map->mtx); 5395 LPRINTF(("map lock: %p (at %s %d)\n", map, file, line)); 5396 uvm_tree_sanity(map, file, line); 5397 uvm_tree_size_chk(map, file, line); 5398 } 5399 5400 void 5401 vm_map_unlock_ln(struct vm_map *map, char *file, int line) 5402 { 5403 KASSERT(map->busy == NULL || map->busy == curproc); 5404 uvm_tree_sanity(map, file, line); 5405 uvm_tree_size_chk(map, file, line); 5406 LPRINTF(("map unlock: %p (at %s %d)\n", map, file, line)); 5407 if ((map->flags & VM_MAP_INTRSAFE) == 0) 5408 rw_exit(&map->lock); 5409 else 5410 mtx_leave(&map->mtx); 5411 } 5412 5413 void 5414 vm_map_unlock_read_ln(struct vm_map *map, char *file, int line) 5415 { 5416 /* XXX: RO */ uvm_tree_sanity(map, file, line); 5417 /* XXX: RO */ uvm_tree_size_chk(map, file, line); 5418 LPRINTF(("map unlock: %p (at %s %d)\n", map, file, line)); 5419 if ((map->flags & VM_MAP_INTRSAFE) == 0) 5420 rw_exit_read(&map->lock); 5421 else 5422 mtx_leave(&map->mtx); 5423 } 5424 5425 void 5426 vm_map_busy_ln(struct vm_map *map, char *file, int line) 5427 { 5428 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0); 5429 KASSERT(rw_write_held(&map->lock)); 5430 KASSERT(map->busy == NULL); 5431 5432 mtx_enter(&map->flags_lock); 5433 map->busy = curproc; 5434 map->flags |= VM_MAP_BUSY; 5435 mtx_leave(&map->flags_lock); 5436 } 5437 5438 void 5439 vm_map_unbusy_ln(struct vm_map *map, char *file, int line) 5440 { 5441 int oflags; 5442 5443 KASSERT((map->flags & VM_MAP_INTRSAFE) == 0); 5444 KASSERT(map->busy == curproc); 5445 5446 mtx_enter(&map->flags_lock); 5447 oflags = map->flags; 5448 map->busy = NULL; 5449 map->flags &= ~(VM_MAP_BUSY|VM_MAP_WANTLOCK); 5450 mtx_leave(&map->flags_lock); 5451 if (oflags & VM_MAP_WANTLOCK) 5452 wakeup(&map->flags); 5453 } 5454 5455 void 5456 vm_map_assert_anylock_ln(struct vm_map *map, char *file, int line) 5457 { 5458 LPRINTF(("map assert read or write locked: %p (at %s %d)\n", map, file, line)); 5459 if ((map->flags & VM_MAP_INTRSAFE) == 0) 5460 rw_assert_anylock(&map->lock); 5461 else 5462 MUTEX_ASSERT_LOCKED(&map->mtx); 5463 } 5464 5465 void 5466 vm_map_assert_wrlock_ln(struct vm_map *map, char *file, int line) 5467 { 5468 LPRINTF(("map assert write locked: %p (at %s %d)\n", map, file, line)); 5469 if ((map->flags & VM_MAP_INTRSAFE) == 0) { 5470 splassert(IPL_NONE); 5471 rw_assert_wrlock(&map->lock); 5472 } else 5473 MUTEX_ASSERT_LOCKED(&map->mtx); 5474 } 5475 5476 #ifndef SMALL_KERNEL 5477 int 5478 uvm_map_fill_vmmap(struct vm_map *map, struct kinfo_vmentry *kve, 5479 size_t *lenp) 5480 { 5481 struct vm_map_entry *entry; 5482 vaddr_t start; 5483 int cnt, maxcnt, error = 0; 5484 5485 KASSERT(*lenp > 0); 5486 KASSERT((*lenp % sizeof(*kve)) == 0); 5487 cnt = 0; 5488 maxcnt = *lenp / sizeof(*kve); 5489 KASSERT(maxcnt > 0); 5490 5491 /* 5492 * Return only entries whose address is above the given base 5493 * address. This allows userland to iterate without knowing the 5494 * number of entries beforehand. 5495 */ 5496 start = (vaddr_t)kve[0].kve_start; 5497 5498 vm_map_lock(map); 5499 RBT_FOREACH(entry, uvm_map_addr, &map->addr) { 5500 if (cnt == maxcnt) { 5501 error = ENOMEM; 5502 break; 5503 } 5504 if (start != 0 && entry->start < start) 5505 continue; 5506 kve->kve_start = entry->start; 5507 kve->kve_end = entry->end; 5508 kve->kve_guard = entry->guard; 5509 kve->kve_fspace = entry->fspace; 5510 kve->kve_fspace_augment = entry->fspace_augment; 5511 kve->kve_offset = entry->offset; 5512 kve->kve_wired_count = entry->wired_count; 5513 kve->kve_etype = entry->etype; 5514 kve->kve_protection = entry->protection; 5515 kve->kve_max_protection = entry->max_protection; 5516 kve->kve_advice = entry->advice; 5517 kve->kve_inheritance = entry->inheritance; 5518 kve->kve_flags = entry->flags; 5519 kve++; 5520 cnt++; 5521 } 5522 vm_map_unlock(map); 5523 5524 KASSERT(cnt <= maxcnt); 5525 5526 *lenp = sizeof(*kve) * cnt; 5527 return error; 5528 } 5529 #endif 5530 5531 5532 RBT_GENERATE_AUGMENT(uvm_map_addr, vm_map_entry, daddrs.addr_entry, 5533 uvm_mapentry_addrcmp, uvm_map_addr_augment); 5534 5535 5536 /* 5537 * MD code: vmspace allocator setup. 5538 */ 5539 5540 #ifdef __i386__ 5541 void 5542 uvm_map_setup_md(struct vm_map *map) 5543 { 5544 vaddr_t min, max; 5545 5546 min = map->min_offset; 5547 max = map->max_offset; 5548 5549 /* 5550 * Ensure the selectors will not try to manage page 0; 5551 * it's too special. 5552 */ 5553 if (min < VMMAP_MIN_ADDR) 5554 min = VMMAP_MIN_ADDR; 5555 5556 #if 0 /* Cool stuff, not yet */ 5557 /* Executable code is special. */ 5558 map->uaddr_exe = uaddr_rnd_create(min, I386_MAX_EXE_ADDR); 5559 /* Place normal allocations beyond executable mappings. */ 5560 map->uaddr_any[3] = uaddr_pivot_create(2 * I386_MAX_EXE_ADDR, max); 5561 #else /* Crappy stuff, for now */ 5562 map->uaddr_any[0] = uaddr_rnd_create(min, max); 5563 #endif 5564 5565 #ifndef SMALL_KERNEL 5566 map->uaddr_brk_stack = uaddr_stack_brk_create(min, max); 5567 #endif /* !SMALL_KERNEL */ 5568 } 5569 #elif __LP64__ 5570 void 5571 uvm_map_setup_md(struct vm_map *map) 5572 { 5573 vaddr_t min, max; 5574 5575 min = map->min_offset; 5576 max = map->max_offset; 5577 5578 /* 5579 * Ensure the selectors will not try to manage page 0; 5580 * it's too special. 5581 */ 5582 if (min < VMMAP_MIN_ADDR) 5583 min = VMMAP_MIN_ADDR; 5584 5585 #if 0 /* Cool stuff, not yet */ 5586 map->uaddr_any[3] = uaddr_pivot_create(MAX(min, 0x100000000ULL), max); 5587 #else /* Crappy stuff, for now */ 5588 map->uaddr_any[0] = uaddr_rnd_create(min, max); 5589 #endif 5590 5591 #ifndef SMALL_KERNEL 5592 map->uaddr_brk_stack = uaddr_stack_brk_create(min, max); 5593 #endif /* !SMALL_KERNEL */ 5594 } 5595 #else /* non-i386, 32 bit */ 5596 void 5597 uvm_map_setup_md(struct vm_map *map) 5598 { 5599 vaddr_t min, max; 5600 5601 min = map->min_offset; 5602 max = map->max_offset; 5603 5604 /* 5605 * Ensure the selectors will not try to manage page 0; 5606 * it's too special. 5607 */ 5608 if (min < VMMAP_MIN_ADDR) 5609 min = VMMAP_MIN_ADDR; 5610 5611 #if 0 /* Cool stuff, not yet */ 5612 map->uaddr_any[3] = uaddr_pivot_create(min, max); 5613 #else /* Crappy stuff, for now */ 5614 map->uaddr_any[0] = uaddr_rnd_create(min, max); 5615 #endif 5616 5617 #ifndef SMALL_KERNEL 5618 map->uaddr_brk_stack = uaddr_stack_brk_create(min, max); 5619 #endif /* !SMALL_KERNEL */ 5620 } 5621 #endif 5622