1 /* 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 /* 29 * @OSF_COPYRIGHT@ 30 */ 31 /* 32 * Mach Operating System 33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University 34 * All Rights Reserved. 35 * 36 * Permission to use, copy, modify and distribute this software and its 37 * documentation is hereby granted, provided that both the copyright 38 * notice and this permission notice appear in all copies of the 39 * software, derivative works or modified versions, and any portions 40 * thereof, and that both notices appear in supporting documentation. 41 * 42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 45 * 46 * Carnegie Mellon requests users of this software to return to 47 * 48 * Software Distribution Coordinator or [email protected] 49 * School of Computer Science 50 * Carnegie Mellon University 51 * Pittsburgh PA 15213-3890 52 * 53 * any improvements or extensions that they make and grant Carnegie Mellon 54 * the rights to redistribute these changes. 55 */ 56 /* 57 */ 58 /* 59 * File: vm_object_xnu.h 60 * Author: Avadis Tevanian, Jr., Michael Wayne Young 61 * Date: 1985 62 * 63 * Virtual memory object module definitions. 64 */ 65 66 #ifndef _VM_VM_OBJECT_XNU_H_ 67 #define _VM_VM_OBJECT_XNU_H_ 68 69 #ifdef XNU_KERNEL_PRIVATE 70 71 #include <kern/queue.h> 72 73 #ifdef MACH_KERNEL_PRIVATE 74 75 #include <debug.h> 76 #include <mach_assert.h> 77 78 #include <mach/kern_return.h> 79 #include <mach/boolean.h> 80 #include <mach/memory_object_types.h> 81 #include <mach/port.h> 82 #include <mach/vm_prot.h> 83 #include <mach/vm_param.h> 84 #include <mach/machine/vm_types.h> 85 #include <kern/locks.h> 86 #include <kern/assert.h> 87 #include <kern/misc_protos.h> 88 #include <vm/pmap.h> 89 #include <vm/vm_external.h> 90 #include <vm/vm_options.h> 91 #include <kern/macro_help.h> 92 #include <ipc/ipc_types.h> 93 #include <vm/vm_page.h> 94 95 96 struct vm_page; 97 98 /* 99 * Types defined: 100 * 101 * vm_object_t Virtual memory object. 102 * vm_object_fault_info_t Used to determine cluster size. 103 */ 104 105 struct vm_object_fault_info { 106 int interruptible; 107 uint32_t user_tag; 108 vm_size_t cluster_size; 109 vm_behavior_t behavior; 110 vm_object_offset_t lo_offset; 111 vm_object_offset_t hi_offset; 112 unsigned int 113 /* boolean_t */ no_cache:1, 114 /* boolean_t */ stealth:1, 115 /* boolean_t */ io_sync:1, 116 /* boolean_t */ cs_bypass:1, 117 /* boolean_t */ csm_associated:1, 118 /* boolean_t */ mark_zf_absent:1, 119 /* boolean_t */ batch_pmap_op:1, 120 /* boolean_t */ resilient_media:1, 121 /* boolean_t */ no_copy_on_read:1, 122 /* boolean_t */ fi_xnu_user_debug:1, 123 /* boolean_t */ fi_used_for_tpro:1, 124 /* boolean_t */ fi_change_wiring:1, 125 /* boolean_t */ fi_no_sleep:1, 126 __vm_object_fault_info_unused_bits:19; 127 int pmap_options; 128 }; 129 130 #define vo_size vo_un1.vou_size 131 #define vo_cache_pages_to_scan vo_un1.vou_cache_pages_to_scan 132 #define vo_shadow_offset vo_un2.vou_shadow_offset 133 #define vo_cache_ts vo_un2.vou_cache_ts 134 #define vo_owner vo_un2.vou_owner 135 136 struct vm_object { 137 /* 138 * on 64 bit systems we pack the pointers hung off the memq. 139 * those pointers have to be able to point back to the memq. 140 * the packed pointers are required to be on a 64 byte boundary 141 * which means 2 things for the vm_object... (1) the memq 142 * struct has to be the first element of the structure so that 143 * we can control its alignment... (2) the vm_object must be 144 * aligned on a 64 byte boundary... for static vm_object's 145 * this is accomplished via the 'aligned' attribute... for 146 * vm_object's in the zone pool, this is accomplished by 147 * rounding the size of the vm_object element to the nearest 148 * 64 byte size before creating the zone. 149 */ 150 vm_page_queue_head_t memq; /* Resident memory - must be first */ 151 lck_rw_t Lock; /* Synchronization */ 152 153 union { 154 vm_object_size_t vou_size; /* Object size (only valid if internal) */ 155 int vou_cache_pages_to_scan; /* pages yet to be visited in an 156 * external object in cache 157 */ 158 } vo_un1; 159 160 struct vm_page *memq_hint; 161 os_ref_atomic_t ref_count; /* Number of references */ 162 unsigned int resident_page_count; 163 /* number of resident pages */ 164 unsigned int wired_page_count; /* number of wired pages 165 * use VM_OBJECT_WIRED_PAGE_UPDATE macros to update */ 166 unsigned int reusable_page_count; 167 168 struct vm_object *vo_copy; /* Object that should receive 169 * a copy of my changed pages, 170 * for copy_delay, or just the 171 * temporary object that 172 * shadows this object, for 173 * copy_call. 174 */ 175 uint32_t vo_copy_version; 176 uint32_t vo_inherit_copy_none:1, 177 __vo_unused_padding:31; 178 struct vm_object *shadow; /* My shadow */ 179 memory_object_t pager; /* Where to get data */ 180 181 union { 182 vm_object_offset_t vou_shadow_offset; /* Offset into shadow */ 183 clock_sec_t vou_cache_ts; /* age of an external object 184 * present in cache 185 */ 186 task_t vou_owner; /* If the object is purgeable 187 * or has a "ledger_tag", this 188 * is the task that owns it. 189 */ 190 } vo_un2; 191 192 vm_object_offset_t paging_offset; /* Offset into memory object */ 193 memory_object_control_t pager_control; /* Where data comes back */ 194 195 memory_object_copy_strategy_t 196 copy_strategy; /* How to handle data copy */ 197 198 /* 199 * Some user processes (mostly VirtualMachine software) take a large 200 * number of UPLs (via IOMemoryDescriptors) to wire pages in large 201 * VM objects and overflow the 16-bit "activity_in_progress" counter. 202 * Since we never enforced any limit there, let's give them 32 bits 203 * for backwards compatibility's sake. 204 */ 205 uint16_t paging_in_progress; 206 uint16_t vo_size_delta; 207 uint32_t activity_in_progress; 208 209 /* The memory object ports are 210 * being used (e.g., for pagein 211 * or pageout) -- don't change 212 * any of these fields (i.e., 213 * don't collapse, destroy or 214 * terminate) 215 */ 216 217 unsigned int 218 /* boolean_t array */ all_wanted:7, /* Bit array of "want to be 219 * awakened" notations. See 220 * VM_OBJECT_EVENT_* items 221 * below */ 222 /* boolean_t */ pager_created:1, /* Has pager been created? */ 223 /* boolean_t */ pager_initialized:1, /* Are fields ready to use? */ 224 /* boolean_t */ pager_ready:1, /* Will pager take requests? */ 225 226 /* boolean_t */ pager_trusted:1, /* The pager for this object 227 * is trusted. This is true for 228 * all internal objects (backed 229 * by the default pager) 230 */ 231 /* boolean_t */ can_persist:1, /* The kernel may keep the data 232 * for this object (and rights 233 * to the memory object) after 234 * all address map references 235 * are deallocated? 236 */ 237 /* boolean_t */ internal:1, /* Created by the kernel (and 238 * therefore, managed by the 239 * default memory manger) 240 */ 241 /* boolean_t */ private:1, /* magic device_pager object, 242 * holds private pages only */ 243 /* boolean_t */ pageout:1, /* pageout object. contains 244 * private pages that refer to 245 * a real memory object. */ 246 /* boolean_t */ alive:1, /* Not yet terminated */ 247 248 /* boolean_t */ purgable:2, /* Purgable state. See 249 * VM_PURGABLE_* 250 */ 251 /* boolean_t */ purgeable_only_by_kernel:1, 252 /* boolean_t */ purgeable_when_ripe:1, /* Purgeable when a token 253 * becomes ripe. 254 */ 255 /* boolean_t */ shadowed:1, /* Shadow may exist */ 256 /* boolean_t */ true_share:1, 257 /* This object is mapped 258 * in more than one place 259 * and hence cannot be 260 * coalesced */ 261 /* boolean_t */ terminating:1, 262 /* Allows vm_object_lookup 263 * and vm_object_deallocate 264 * to special case their 265 * behavior when they are 266 * called as a result of 267 * page cleaning during 268 * object termination 269 */ 270 /* boolean_t */ named:1, /* An enforces an internal 271 * naming convention, by 272 * calling the right routines 273 * for allocation and 274 * destruction, UBC references 275 * against the vm_object are 276 * checked. 277 */ 278 /* boolean_t */ shadow_severed:1, 279 /* When a permanent object 280 * backing a COW goes away 281 * unexpectedly. This bit 282 * allows vm_fault to return 283 * an error rather than a 284 * zero filled page. 285 */ 286 /* boolean_t */ phys_contiguous:1, 287 /* Memory is wired and 288 * guaranteed physically 289 * contiguous. However 290 * it is not device memory 291 * and obeys normal virtual 292 * memory rules w.r.t pmap 293 * access bits. 294 */ 295 /* boolean_t */ nophyscache:1, 296 /* When mapped at the 297 * pmap level, don't allow 298 * primary caching. (for 299 * I/O) 300 */ 301 /* boolean_t */ for_realtime:1, 302 /* Might be needed for realtime code path */ 303 /* vm_object_destroy_reason_t */ no_pager_reason:3, 304 /* differentiate known and unknown causes */ 305 #if FBDP_DEBUG_OBJECT_NO_PAGER 306 /* boolean_t */ fbdp_tracked:1; 307 #else /* FBDP_DEBUG_OBJECT_NO_PAGER */ 308 __object1_unused_bits:1; 309 #endif /* FBDP_DEBUG_OBJECT_NO_PAGER */ 310 311 queue_chain_t cached_list; /* Attachment point for the 312 * list of objects cached as a 313 * result of their can_persist 314 * value 315 */ 316 /* 317 * the following fields are not protected by any locks 318 * they are updated via atomic compare and swap 319 */ 320 vm_object_offset_t last_alloc; /* last allocation offset */ 321 vm_offset_t cow_hint; /* last page present in */ 322 /* shadow but not in object */ 323 int sequential; /* sequential access size */ 324 325 uint32_t pages_created; 326 uint32_t pages_used; 327 /* hold object lock when altering */ 328 unsigned int 329 wimg_bits:8, /* cache WIMG bits */ 330 code_signed:1, /* pages are signed and should be 331 * validated; the signatures are stored 332 * with the pager */ 333 transposed:1, /* object was transposed with another */ 334 mapping_in_progress:1, /* pager being mapped/unmapped */ 335 phantom_isssd:1, 336 volatile_empty:1, 337 volatile_fault:1, 338 all_reusable:1, 339 blocked_access:1, 340 set_cache_attr:1, 341 object_is_shared_cache:1, 342 purgeable_queue_type:2, 343 purgeable_queue_group:3, 344 io_tracking:1, 345 no_tag_update:1, /* */ 346 #if CONFIG_SECLUDED_MEMORY 347 eligible_for_secluded:1, 348 can_grab_secluded:1, 349 #else /* CONFIG_SECLUDED_MEMORY */ 350 __object3_unused_bits:2, 351 #endif /* CONFIG_SECLUDED_MEMORY */ 352 #if VM_OBJECT_ACCESS_TRACKING 353 access_tracking:1, 354 #else /* VM_OBJECT_ACCESS_TRACKING */ 355 __unused_access_tracking:1, 356 #endif /* VM_OBJECT_ACCESS_TRACKING */ 357 vo_ledger_tag:3, 358 vo_no_footprint:1; 359 360 #if VM_OBJECT_ACCESS_TRACKING 361 uint32_t access_tracking_reads; 362 uint32_t access_tracking_writes; 363 #endif /* VM_OBJECT_ACCESS_TRACKING */ 364 365 uint8_t scan_collisions; 366 uint8_t __object4_unused_bits[1]; 367 vm_tag_t wire_tag; 368 369 #if CONFIG_PHANTOM_CACHE 370 uint32_t phantom_object_id; 371 #endif 372 #if CONFIG_IOSCHED || UPL_DEBUG 373 queue_head_t uplq; /* List of outstanding upls */ 374 #endif 375 376 #ifdef VM_PIP_DEBUG 377 /* 378 * Keep track of the stack traces for the first holders 379 * of a "paging_in_progress" reference for this VM object. 380 */ 381 #define VM_PIP_DEBUG_STACK_FRAMES 25 /* depth of each stack trace */ 382 #define VM_PIP_DEBUG_MAX_REFS 10 /* track that many references */ 383 struct __pip_backtrace { 384 void *pip_retaddr[VM_PIP_DEBUG_STACK_FRAMES]; 385 } pip_holders[VM_PIP_DEBUG_MAX_REFS]; 386 #endif /* VM_PIP_DEBUG */ 387 388 queue_chain_t objq; /* object queue - currently used for purgable queues */ 389 queue_chain_t task_objq; /* objects owned by task - protected by task lock */ 390 391 #if !VM_TAG_ACTIVE_UPDATE 392 queue_chain_t wired_objq; 393 #endif /* !VM_TAG_ACTIVE_UPDATE */ 394 395 #if DEBUG 396 void *purgeable_owner_bt[16]; 397 task_t vo_purgeable_volatilizer; /* who made it volatile? */ 398 void *purgeable_volatilizer_bt[16]; 399 #endif /* DEBUG */ 400 401 /* 402 * If this object is backed by anonymous memory, this represents the ID of 403 * the vm_map that the memory originated from (i.e. this points backwards in 404 * shadow chains). Note that an originator is present even if the object 405 * hasn't been faulted into the backing pmap yet. 406 */ 407 vm_map_serial_t vmo_provenance; 408 }; 409 410 #define VM_OBJECT_PURGEABLE_FAULT_ERROR(object) \ 411 ((object)->volatile_fault && \ 412 ((object)->purgable == VM_PURGABLE_VOLATILE || \ 413 (object)->purgable == VM_PURGABLE_EMPTY)) 414 415 extern const vm_object_t kernel_object_default; /* the default kernel object */ 416 417 extern const vm_object_t compressor_object; /* the single compressor object, allocates pages for compressed 418 * buffers (not the segments) */ 419 420 extern const vm_object_t retired_pages_object; /* pages retired due to ECC, should never be used */ 421 422 423 #define is_kernel_object(object) ((object) == kernel_object_default) 424 425 426 extern const vm_object_t exclaves_object; /* holds VM pages owned by exclaves */ 427 428 # define VM_MSYNC_INITIALIZED 0 429 # define VM_MSYNC_SYNCHRONIZING 1 430 # define VM_MSYNC_DONE 2 431 432 433 extern lck_grp_t vm_map_lck_grp; 434 extern lck_attr_t vm_map_lck_attr; 435 436 /** os_refgrp_t for vm_objects */ 437 os_refgrp_decl_extern(vm_object_refgrp); 438 439 #ifndef VM_TAG_ACTIVE_UPDATE 440 #error VM_TAG_ACTIVE_UPDATE 441 #endif 442 443 #if VM_TAG_ACTIVE_UPDATE 444 #define VM_OBJECT_WIRED_ENQUEUE(object) panic("VM_OBJECT_WIRED_ENQUEUE") 445 #define VM_OBJECT_WIRED_DEQUEUE(object) panic("VM_OBJECT_WIRED_DEQUEUE") 446 #else /* VM_TAG_ACTIVE_UPDATE */ 447 #define VM_OBJECT_WIRED_ENQUEUE(object) \ 448 MACRO_BEGIN \ 449 lck_spin_lock_grp(&vm_objects_wired_lock, &vm_page_lck_grp_bucket); \ 450 assert(!(object)->wired_objq.next); \ 451 assert(!(object)->wired_objq.prev); \ 452 queue_enter(&vm_objects_wired, (object), \ 453 vm_object_t, wired_objq); \ 454 lck_spin_unlock(&vm_objects_wired_lock); \ 455 MACRO_END 456 #define VM_OBJECT_WIRED_DEQUEUE(object) \ 457 MACRO_BEGIN \ 458 if ((object)->wired_objq.next) { \ 459 lck_spin_lock_grp(&vm_objects_wired_lock, &vm_page_lck_grp_bucket); \ 460 queue_remove(&vm_objects_wired, (object), \ 461 vm_object_t, wired_objq); \ 462 lck_spin_unlock(&vm_objects_wired_lock); \ 463 } \ 464 MACRO_END 465 #endif /* VM_TAG_ACTIVE_UPDATE */ 466 467 #define VM_OBJECT_WIRED(object, tag) \ 468 MACRO_BEGIN \ 469 assert(VM_KERN_MEMORY_NONE != (tag)); \ 470 assert(VM_KERN_MEMORY_NONE == (object)->wire_tag); \ 471 (object)->wire_tag = (tag); \ 472 if (!VM_TAG_ACTIVE_UPDATE) { \ 473 VM_OBJECT_WIRED_ENQUEUE((object)); \ 474 } \ 475 MACRO_END 476 477 #define VM_OBJECT_UNWIRED(object) \ 478 MACRO_BEGIN \ 479 if (!VM_TAG_ACTIVE_UPDATE) { \ 480 VM_OBJECT_WIRED_DEQUEUE((object)); \ 481 } \ 482 if (VM_KERN_MEMORY_NONE != (object)->wire_tag) { \ 483 vm_tag_update_size((object)->wire_tag, -ptoa_64((object)->wired_page_count), (object)); \ 484 (object)->wire_tag = VM_KERN_MEMORY_NONE; \ 485 } \ 486 MACRO_END 487 488 // These two macros start & end a C block 489 #define VM_OBJECT_WIRED_PAGE_UPDATE_START(object) \ 490 MACRO_BEGIN \ 491 { \ 492 int64_t __wireddelta = 0; vm_tag_t __waswired = (object)->wire_tag; 493 494 #define VM_OBJECT_WIRED_PAGE_UPDATE_END(object, tag) \ 495 if (__wireddelta) { \ 496 boolean_t __overflow __assert_only = \ 497 os_add_overflow((object)->wired_page_count, __wireddelta, \ 498 &(object)->wired_page_count); \ 499 assert(!__overflow); \ 500 if (!(object)->internal && \ 501 (object)->vo_ledger_tag && \ 502 VM_OBJECT_OWNER((object)) != NULL) { \ 503 vm_object_wired_page_update_ledgers(object, __wireddelta); \ 504 } \ 505 if (!(object)->pageout && !(object)->no_tag_update) { \ 506 if (__wireddelta > 0) { \ 507 assert (VM_KERN_MEMORY_NONE != (tag)); \ 508 if (VM_KERN_MEMORY_NONE == __waswired) { \ 509 VM_OBJECT_WIRED((object), (tag)); \ 510 } \ 511 vm_tag_update_size((object)->wire_tag, ptoa_64(__wireddelta), (object)); \ 512 } else if (VM_KERN_MEMORY_NONE != __waswired) { \ 513 assert (VM_KERN_MEMORY_NONE != (object)->wire_tag); \ 514 vm_tag_update_size((object)->wire_tag, ptoa_64(__wireddelta), (object)); \ 515 if (!(object)->wired_page_count) { \ 516 VM_OBJECT_UNWIRED((object)); \ 517 } \ 518 } \ 519 } \ 520 } \ 521 } \ 522 MACRO_END 523 524 #define VM_OBJECT_WIRED_PAGE_COUNT(object, delta) \ 525 __wireddelta += delta; \ 526 527 #define VM_OBJECT_WIRED_PAGE_ADD(object, m) \ 528 if (vm_page_is_canonical(m)) __wireddelta++; 529 530 #define VM_OBJECT_WIRED_PAGE_REMOVE(object, m) \ 531 if (vm_page_is_canonical(m)) __wireddelta--; 532 533 #define OBJECT_LOCK_SHARED 0 534 #define OBJECT_LOCK_EXCLUSIVE 1 535 536 extern lck_grp_t vm_object_lck_grp; 537 extern lck_attr_t vm_object_lck_attr; 538 extern lck_attr_t kernel_object_lck_attr; 539 extern lck_attr_t compressor_object_lck_attr; 540 541 extern vm_object_t vm_pageout_scan_wants_object; 542 543 extern void vm_object_lock(vm_object_t); 544 extern bool vm_object_lock_check_contended(vm_object_t); 545 extern boolean_t vm_object_lock_try(vm_object_t); 546 extern boolean_t _vm_object_lock_try(vm_object_t); 547 extern boolean_t vm_object_lock_avoid(vm_object_t); 548 extern void vm_object_lock_shared(vm_object_t); 549 extern boolean_t vm_object_lock_yield_shared(vm_object_t); 550 extern boolean_t vm_object_lock_try_shared(vm_object_t); 551 extern void vm_object_unlock(vm_object_t); 552 extern boolean_t vm_object_lock_upgrade(vm_object_t); 553 554 extern void kdp_vm_object_sleep_find_owner( 555 event64_t wait_event, 556 block_hint_t wait_type, 557 thread_waitinfo_t *waitinfo); 558 559 #endif /* MACH_KERNEL_PRIVATE */ 560 561 #if CONFIG_IOSCHED 562 struct io_reprioritize_req { 563 uint64_t blkno; 564 uint32_t len; 565 int priority; 566 struct vnode *devvp; 567 struct mpsc_queue_chain iorr_elm; 568 }; 569 typedef struct io_reprioritize_req *io_reprioritize_req_t; 570 571 extern void vm_io_reprioritize_init(void); 572 #endif 573 574 extern void page_worker_init(void); 575 576 577 #endif /* XNU_KERNEL_PRIVATE */ 578 579 #endif /* _VM_VM_OBJECT_XNU_H_ */ 580