Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 | /* * Copyright (c) 2009-2020 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <kern/backtrace.h> #include <mach/sdt.h> #include <vm/vm_map_internal.h> #include <vm/vm_pageout.h> /* for vm_debug_events */ #include <sys/code_signing.h> #if MACH_ASSERT bool first_free_is_valid_store( vm_map_t map ) { return first_free_is_valid_ll( map ); } #endif bool vm_map_store_has_RB_support( struct vm_map_header *hdr ) { if ((void*)hdr->rb_head_store.rbh_root == (void*)(int)SKIP_RB_TREE) { return FALSE; } return TRUE; } void vm_map_store_init( struct vm_map_header *hdr ) { vm_map_store_init_ll( hdr ); #ifdef VM_MAP_STORE_USE_RB if (vm_map_store_has_RB_support( hdr )) { vm_map_store_init_rb( hdr ); } #endif } static inline bool _vm_map_store_lookup_entry( vm_map_t map, vm_map_offset_t address, vm_map_entry_t *entry) /* OUT */ { #ifdef VM_MAP_STORE_USE_RB if (vm_map_store_has_RB_support( &map->hdr )) { return vm_map_store_lookup_entry_rb( map, address, entry ); } else { panic("VM map lookups need RB tree support."); return FALSE; /* For compiler warning.*/ } #endif } __attribute__((noinline)) bool vm_map_store_lookup_entry( vm_map_t map, vm_map_offset_t address, vm_map_entry_t *entry) /* OUT */ { return _vm_map_store_lookup_entry(map, address, entry); } /* * vm_map_entry_{un,}link: * * Insert/remove entries from maps (or map copies). * The _vm_map_store_entry_{un,}link variants are used at * some places where updating first_free is not needed & * copy maps are being modified. Also note the first argument * is the map header. * Modifying the vm_map_store_entry_{un,}link functions to * deal with these call sites made the interface confusing * and clunky. */ void _vm_map_store_entry_link( struct vm_map_header *mapHdr, vm_map_entry_t after_where, vm_map_entry_t entry) { if (__improbable(entry->vme_end <= entry->vme_start)) { panic("maphdr %p entry %p start 0x%llx end 0x%llx\n", mapHdr, entry, (uint64_t)entry->vme_start, (uint64_t)entry->vme_end); } assert(entry->vme_start < entry->vme_end); if (__improbable(vm_debug_events)) { DTRACE_VM4(map_entry_link, vm_map_t, __container_of(mapHdr, struct _vm_map, hdr), vm_map_entry_t, entry, vm_address_t, entry->vme_start, vm_address_t, entry->vme_end); } vm_map_store_entry_link_ll(mapHdr, after_where, entry); #ifdef VM_MAP_STORE_USE_RB if (vm_map_store_has_RB_support( mapHdr )) { vm_map_store_entry_link_rb(mapHdr, entry); } #endif #if MAP_ENTRY_INSERTION_DEBUG if (entry->vme_start_original == 0 && entry->vme_end_original == 0) { entry->vme_start_original = entry->vme_start; entry->vme_end_original = entry->vme_end; } btref_put(entry->vme_insertion_bt); entry->vme_insertion_bt = btref_get(__builtin_frame_address(0), BTREF_GET_NOWAIT); #endif } void vm_map_store_entry_link( vm_map_t map, vm_map_entry_t after_where, vm_map_entry_t entry, vm_map_kernel_flags_t vmk_flags) { if (entry->is_sub_map) { assertf(VM_MAP_PAGE_SHIFT(VME_SUBMAP(entry)) >= VM_MAP_PAGE_SHIFT(map), "map %p (%d) entry %p submap %p (%d)\n", map, VM_MAP_PAGE_SHIFT(map), entry, VME_SUBMAP(entry), VM_MAP_PAGE_SHIFT(VME_SUBMAP(entry))); } _vm_map_store_entry_link(&map->hdr, after_where, entry); if (map->disable_vmentry_reuse == TRUE) { /* * GuardMalloc support: * Some of these entries are created with MAP_FIXED. * Some are created with a very high hint address. * So we use aliases and address ranges to make sure * that those special regions (nano, jit etc) don't * result in our highest hint being set to near * the end of the map and future alloctions getting * KERN_NO_SPACE when running with guardmalloc. */ int alias = VME_ALIAS(entry); assert(!map->is_nested_map); if (alias != VM_MEMORY_MALLOC_NANO && alias != VM_MEMORY_MALLOC_TINY && alias != VM_MEMORY_MALLOC_SMALL && alias != VM_MEMORY_MALLOC_MEDIUM && alias != VM_MEMORY_MALLOC_LARGE && alias != VM_MEMORY_MALLOC_HUGE && entry->used_for_jit == 0 && (entry->vme_start < SHARED_REGION_BASE || entry->vme_start >= (SHARED_REGION_BASE + SHARED_REGION_SIZE)) && map->highest_entry_end < entry->vme_end) { map->highest_entry_end = entry->vme_end; } } else { update_first_free_ll(map, map->first_free); #ifdef VM_MAP_STORE_USE_RB if (vm_map_store_has_RB_support(&map->hdr)) { update_first_free_rb(map, entry, TRUE); } #endif } #if CODE_SIGNING_MONITOR (void) vm_map_entry_cs_associate(map, entry, vmk_flags); #else (void) vmk_flags; #endif } void _vm_map_store_entry_unlink( struct vm_map_header * mapHdr, vm_map_entry_t entry, bool check_permanent) { if (__improbable(vm_debug_events)) { DTRACE_VM4(map_entry_unlink, vm_map_t, __container_of(mapHdr, struct _vm_map, hdr), vm_map_entry_t, entry, vm_address_t, entry->vme_start, vm_address_t, entry->vme_end); } /* * We should never unlink a "permanent" entry. The caller should * clear "permanent" first if it wants it to be bypassed. */ if (check_permanent) { assertf(!entry->vme_permanent, "mapHdr %p entry %p [ 0x%llx end 0x%llx ] prot 0x%x/0x%x submap %d\n", mapHdr, entry, (uint64_t)entry->vme_start, (uint64_t)entry->vme_end, entry->protection, entry->max_protection, entry->is_sub_map); } vm_map_store_entry_unlink_ll(mapHdr, entry); #ifdef VM_MAP_STORE_USE_RB if (vm_map_store_has_RB_support( mapHdr )) { vm_map_store_entry_unlink_rb(mapHdr, entry); } #endif } void vm_map_store_entry_unlink( vm_map_t map, vm_map_entry_t entry, bool check_permanent) { vm_map_t VMEU_map; vm_map_entry_t VMEU_entry = NULL; vm_map_entry_t VMEU_first_free = NULL; VMEU_map = (map); VMEU_entry = (entry); if (entry == map->hint) { map->hint = vm_map_to_entry(map); } if (map->holelistenabled == FALSE) { if (VMEU_entry->vme_start <= VMEU_map->first_free->vme_start) { VMEU_first_free = VMEU_entry->vme_prev; } else { VMEU_first_free = VMEU_map->first_free; } } _vm_map_store_entry_unlink(&VMEU_map->hdr, VMEU_entry, check_permanent); update_first_free_ll(VMEU_map, VMEU_first_free); #ifdef VM_MAP_STORE_USE_RB if (vm_map_store_has_RB_support( &VMEU_map->hdr )) { update_first_free_rb(VMEU_map, entry, FALSE); } #endif } void vm_map_store_copy_reset( vm_map_copy_t copy, vm_map_entry_t entry) { int nentries = copy->cpy_hdr.nentries; vm_map_store_copy_reset_ll(copy, entry, nentries); #ifdef VM_MAP_STORE_USE_RB if (vm_map_store_has_RB_support( ©->c_u.hdr )) { vm_map_store_copy_reset_rb(copy, entry, nentries); } #endif } void vm_map_store_update_first_free( vm_map_t map, vm_map_entry_t first_free_entry, bool new_entry_creation) { update_first_free_ll(map, first_free_entry); #ifdef VM_MAP_STORE_USE_RB if (vm_map_store_has_RB_support( &map->hdr )) { update_first_free_rb(map, first_free_entry, new_entry_creation); } #endif } __abortlike static void __vm_map_store_find_space_holelist_corruption( vm_map_t map, vm_map_offset_t start, vm_map_entry_t entry) { panic("Found an existing entry %p [0x%llx, 0x%llx) in map %p " "instead of potential hole at address: 0x%llx.", entry, entry->vme_start, entry->vme_end, map, start); } static void vm_map_store_convert_hole_to_entry( vm_map_t map, vm_map_offset_t addr, vm_map_entry_t *entry_p) { vm_map_entry_t entry = *entry_p; if (_vm_map_store_lookup_entry(map, entry->vme_start, entry_p)) { __vm_map_store_find_space_holelist_corruption(map, addr, entry); } } static struct vm_map_entry * vm_map_store_find_space_backwards( vm_map_t map, vm_map_offset_t end, vm_map_offset_t lowest_addr, vm_map_offset_t guard_offset, vm_map_size_t size, vm_map_offset_t mask, vm_map_offset_t *addr_out) { const vm_map_offset_t map_mask = VM_MAP_PAGE_MASK(map); const bool use_holes = map->holelistenabled; vm_map_offset_t start; vm_map_entry_t entry; /* * Find the entry we will scan from that is the closest * to our required scan hint "end". */ if (use_holes) { entry = CAST_TO_VM_MAP_ENTRY(map->holes_list); if (entry == VM_MAP_ENTRY_NULL) { return VM_MAP_ENTRY_NULL; } entry = entry->vme_prev; while (end <= entry->vme_start) { if (entry == CAST_TO_VM_MAP_ENTRY(map->holes_list)) { return VM_MAP_ENTRY_NULL; } entry = entry->vme_prev; } if (entry->vme_end < end) { end = entry->vme_end; } } else { if (map->max_offset <= end) { entry = vm_map_to_entry(map); end = map->max_offset; } else if (_vm_map_store_lookup_entry(map, end - 1, &entry)) { end = entry->vme_start; } else { entry = entry->vme_next; } } for (;;) { /* * The "entry" follows the proposed new region. */ end = vm_map_trunc_page(end, map_mask); start = (end - size) & ~mask; start = vm_map_trunc_page(start, map_mask); end = start + size; start -= guard_offset; if (end < start || start < lowest_addr) { /* * Fail: reached our scan lowest address limit, * without finding a large enough hole. */ return VM_MAP_ENTRY_NULL; } if (use_holes) { if (entry->vme_start <= start) { /* * Done: this hole is wide enough. */ vm_map_store_convert_hole_to_entry(map, start, &entry); break; } if (entry == CAST_TO_VM_MAP_ENTRY(map->holes_list)) { /* * Fail: wrapped around, no more holes */ return VM_MAP_ENTRY_NULL; } entry = entry->vme_prev; end = entry->vme_end; } else { entry = entry->vme_prev; if (entry == vm_map_to_entry(map)) { /* * Done: no more entries toward the start * of the map, only a big enough void. */ break; } if (entry->vme_end <= start) { /* * Done: the gap between the two consecutive * entries is large enough. */ break; } end = entry->vme_start; } } *addr_out = start; return entry; } static struct vm_map_entry * vm_map_store_find_space_forward( vm_map_t map, vm_map_offset_t start, vm_map_offset_t highest_addr, vm_map_offset_t guard_offset, vm_map_size_t size, vm_map_offset_t mask, vm_map_offset_t *addr_out) { const vm_map_offset_t map_mask = VM_MAP_PAGE_MASK(map); const bool use_holes = map->holelistenabled; vm_map_entry_t entry; /* * Find the entry we will scan from that is the closest * to our required scan hint "start". */ if (__improbable(map->disable_vmentry_reuse)) { assert(!map->is_nested_map); start = map->highest_entry_end + PAGE_SIZE_64; while (vm_map_lookup_entry(map, start, &entry)) { start = entry->vme_end + PAGE_SIZE_64; } } else if (use_holes) { entry = CAST_TO_VM_MAP_ENTRY(map->holes_list); if (entry == VM_MAP_ENTRY_NULL) { return VM_MAP_ENTRY_NULL; } while (entry->vme_end <= start) { entry = entry->vme_next; if (entry == CAST_TO_VM_MAP_ENTRY(map->holes_list)) { return VM_MAP_ENTRY_NULL; } } if (start < entry->vme_start) { start = entry->vme_start; } } else { vm_map_offset_t first_free_start; assert(first_free_is_valid(map)); entry = map->first_free; if (entry == vm_map_to_entry(map)) { first_free_start = map->min_offset; } else { first_free_start = entry->vme_end; } if (start <= first_free_start) { start = first_free_start; } else if (_vm_map_store_lookup_entry(map, start, &entry)) { start = entry->vme_end; } } for (;;) { vm_map_offset_t orig_start = start; vm_map_offset_t end, desired_empty_end; /* * The "entry" precedes the proposed new region. */ start = (start + guard_offset + mask) & ~mask; start = vm_map_round_page(start, map_mask); end = start + size; start -= guard_offset; /* * We want an entire page of empty space, * but don't increase the allocation size. */ desired_empty_end = vm_map_round_page(end, map_mask); if (start < orig_start || desired_empty_end < start || highest_addr < desired_empty_end) { /* * Fail: reached our scan highest address limit, * without finding a large enough hole. */ return VM_MAP_ENTRY_NULL; } if (use_holes) { if (desired_empty_end <= entry->vme_end) { /* * Done: this hole is wide enough. */ vm_map_store_convert_hole_to_entry(map, start, &entry); break; } entry = entry->vme_next; if (entry == CAST_TO_VM_MAP_ENTRY(map->holes_list)) { /* * Fail: wrapped around, no more holes */ return VM_MAP_ENTRY_NULL; } start = entry->vme_start; } else { vm_map_entry_t next = entry->vme_next; if (next == vm_map_to_entry(map)) { /* * Done: no more entries toward the end * of the map, only a big enough void. */ break; } if (desired_empty_end <= next->vme_start) { /* * Done: the gap between the two consecutive * entries is large enough. */ break; } entry = next; start = entry->vme_end; } } *addr_out = start; return entry; } struct vm_map_entry * vm_map_store_find_space( vm_map_t map, vm_map_offset_t hint, vm_map_offset_t limit, bool backwards, vm_map_offset_t guard_offset, vm_map_size_t size, vm_map_offset_t mask, vm_map_offset_t *addr_out) { vm_map_entry_t entry; #if defined VM_MAP_STORE_USE_RB __builtin_assume((void*)map->hdr.rb_head_store.rbh_root != (void*)(int)SKIP_RB_TREE); #endif if (backwards) { entry = vm_map_store_find_space_backwards(map, hint, limit, guard_offset, size, mask, addr_out); } else { entry = vm_map_store_find_space_forward(map, hint, limit, guard_offset, size, mask, addr_out); } return entry; } |