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: mach/vm_statistics.h 60 * Author: Avadis Tevanian, Jr., Michael Wayne Young, David Golub 61 * 62 * Virtual memory statistics structure. 63 * 64 */ 65 66 #ifndef _MACH_VM_STATISTICS_H_ 67 #define _MACH_VM_STATISTICS_H_ 68 69 70 #include <Availability.h> 71 #include <os/base.h> 72 #include <stdbool.h> 73 #include <sys/cdefs.h> 74 75 #include <mach/machine/vm_types.h> 76 #include <mach/machine/kern_return.h> 77 78 __BEGIN_DECLS 79 80 #pragma mark VM Statistics 81 82 /* 83 * vm_statistics 84 * 85 * History: 86 * rev0 - original structure. 87 * rev1 - added purgable info (purgable_count and purges). 88 * rev2 - added speculative_count. 89 * 90 * Note: you cannot add any new fields to this structure. Add them below in 91 * vm_statistics64. 92 */ 93 94 struct vm_statistics { 95 natural_t free_count; /* # of pages free */ 96 natural_t active_count; /* # of pages active */ 97 natural_t inactive_count; /* # of pages inactive */ 98 natural_t wire_count; /* # of pages wired down */ 99 natural_t zero_fill_count; /* # of zero fill pages */ 100 natural_t reactivations; /* # of pages reactivated */ 101 natural_t pageins; /* # of pageins */ 102 natural_t pageouts; /* # of pageouts */ 103 natural_t faults; /* # of faults */ 104 natural_t cow_faults; /* # of copy-on-writes */ 105 natural_t lookups; /* object cache lookups */ 106 natural_t hits; /* object cache hits */ 107 108 /* added for rev1 */ 109 natural_t purgeable_count; /* # of pages purgeable */ 110 natural_t purges; /* # of pages purged */ 111 112 /* added for rev2 */ 113 /* 114 * NB: speculative pages are already accounted for in "free_count", 115 * so "speculative_count" is the number of "free" pages that are 116 * used to hold data that was read speculatively from disk but 117 * haven't actually been used by anyone so far. 118 */ 119 natural_t speculative_count; /* # of pages speculative */ 120 }; 121 122 /* Used by all architectures */ 123 typedef struct vm_statistics *vm_statistics_t; 124 typedef struct vm_statistics vm_statistics_data_t; 125 126 /* 127 * vm_statistics64 128 * 129 * History: 130 * rev0 - original structure. 131 * rev1 - added purgable info (purgable_count and purges). 132 * rev2 - added speculative_count. 133 * ---- 134 * rev3 - changed name to vm_statistics64. 135 * changed some fields in structure to 64-bit on 136 * arm, i386 and x86_64 architectures. 137 * rev4 - require 64-bit alignment for efficient access 138 * in the kernel. No change to reported data. 139 * 140 */ 141 142 struct vm_statistics64 { 143 natural_t free_count; /* # of pages free */ 144 natural_t active_count; /* # of pages active */ 145 natural_t inactive_count; /* # of pages inactive */ 146 natural_t wire_count; /* # of pages wired down */ 147 uint64_t zero_fill_count; /* # of zero fill pages */ 148 uint64_t reactivations; /* # of pages reactivated */ 149 uint64_t pageins; /* # of pageins (lifetime) */ 150 uint64_t pageouts; /* # of pageouts */ 151 uint64_t faults; /* # of faults */ 152 uint64_t cow_faults; /* # of copy-on-writes */ 153 uint64_t lookups; /* object cache lookups */ 154 uint64_t hits; /* object cache hits */ 155 uint64_t purges; /* # of pages purged */ 156 natural_t purgeable_count; /* # of pages purgeable */ 157 /* 158 * NB: speculative pages are already accounted for in "free_count", 159 * so "speculative_count" is the number of "free" pages that are 160 * used to hold data that was read speculatively from disk but 161 * haven't actually been used by anyone so far. 162 */ 163 natural_t speculative_count; /* # of pages speculative */ 164 165 /* added for rev1 */ 166 uint64_t decompressions; /* # of pages decompressed (lifetime) */ 167 uint64_t compressions; /* # of pages compressed (lifetime) */ 168 uint64_t swapins; /* # of pages swapped in via compressor segments (lifetime) */ 169 uint64_t swapouts; /* # of pages swapped out via compressor segments (lifetime) */ 170 natural_t compressor_page_count; /* # of pages used by the compressed pager to hold all the compressed data */ 171 natural_t throttled_count; /* # of pages throttled */ 172 natural_t external_page_count; /* # of pages that are file-backed (non-swap) */ 173 natural_t internal_page_count; /* # of pages that are anonymous */ 174 uint64_t total_uncompressed_pages_in_compressor; /* # of pages (uncompressed) held within the compressor. */ 175 /* added for rev2 */ 176 uint64_t swapped_count; /* # of compressor-stored pages currently stored in swap */ 177 } __attribute__((aligned(8))); 178 179 typedef struct vm_statistics64 *vm_statistics64_t; 180 typedef struct vm_statistics64 vm_statistics64_data_t; 181 182 kern_return_t vm_stats(void *info, unsigned int *count); 183 184 /* 185 * VM_STATISTICS_TRUNCATE_TO_32_BIT 186 * 187 * This is used by host_statistics() to truncate and peg the 64-bit in-kernel values from 188 * vm_statistics64 to the 32-bit values of the older structure above (vm_statistics). 189 */ 190 #define VM_STATISTICS_TRUNCATE_TO_32_BIT(value) ((uint32_t)(((value) > UINT32_MAX ) ? UINT32_MAX : (value))) 191 192 /* 193 * vm_extmod_statistics 194 * 195 * Structure to record modifications to a task by an 196 * external agent. 197 * 198 * History: 199 * rev0 - original structure. 200 */ 201 202 struct vm_extmod_statistics { 203 int64_t task_for_pid_count; /* # of times task port was looked up */ 204 int64_t task_for_pid_caller_count; /* # of times this task called task_for_pid */ 205 int64_t thread_creation_count; /* # of threads created in task */ 206 int64_t thread_creation_caller_count; /* # of threads created by task */ 207 int64_t thread_set_state_count; /* # of register state sets in task */ 208 int64_t thread_set_state_caller_count; /* # of register state sets by task */ 209 } __attribute__((aligned(8))); 210 211 typedef struct vm_extmod_statistics *vm_extmod_statistics_t; 212 typedef struct vm_extmod_statistics vm_extmod_statistics_data_t; 213 214 typedef struct vm_purgeable_stat { 215 uint64_t count; 216 uint64_t size; 217 }vm_purgeable_stat_t; 218 219 struct vm_purgeable_info { 220 vm_purgeable_stat_t fifo_data[8]; 221 vm_purgeable_stat_t obsolete_data; 222 vm_purgeable_stat_t lifo_data[8]; 223 }; 224 225 typedef struct vm_purgeable_info *vm_purgeable_info_t; 226 227 /* included for the vm_map_page_query call */ 228 229 #define VM_PAGE_QUERY_PAGE_PRESENT 0x1 230 #define VM_PAGE_QUERY_PAGE_FICTITIOUS 0x2 231 #define VM_PAGE_QUERY_PAGE_REF 0x4 232 #define VM_PAGE_QUERY_PAGE_DIRTY 0x8 233 #define VM_PAGE_QUERY_PAGE_PAGED_OUT 0x10 234 #define VM_PAGE_QUERY_PAGE_COPIED 0x20 235 #define VM_PAGE_QUERY_PAGE_SPECULATIVE 0x40 236 #define VM_PAGE_QUERY_PAGE_EXTERNAL 0x80 237 #define VM_PAGE_QUERY_PAGE_CS_VALIDATED 0x100 238 #define VM_PAGE_QUERY_PAGE_CS_TAINTED 0x200 239 #define VM_PAGE_QUERY_PAGE_CS_NX 0x400 240 #define VM_PAGE_QUERY_PAGE_REUSABLE 0x800 241 242 #pragma mark User Flags 243 244 /* 245 * VM allocation flags: 246 * 247 * VM_FLAGS_FIXED 248 * (really the absence of VM_FLAGS_ANYWHERE) 249 * Allocate new VM region at the specified virtual address, if possible. 250 * 251 * VM_FLAGS_ANYWHERE 252 * Allocate new VM region anywhere it would fit in the address space. 253 * 254 * VM_FLAGS_PURGABLE 255 * Create a purgable VM object for that new VM region. 256 * 257 * VM_FLAGS_4GB_CHUNK 258 * The new VM region will be chunked up into 4GB sized pieces. 259 * 260 * VM_FLAGS_NO_PMAP_CHECK 261 * (for DEBUG kernel config only, ignored for other configs) 262 * Do not check that there is no stale pmap mapping for the new VM region. 263 * This is useful for kernel memory allocations at bootstrap when building 264 * the initial kernel address space while some memory is already in use. 265 * 266 * VM_FLAGS_OVERWRITE 267 * The new VM region can replace existing VM regions if necessary 268 * (to be used in combination with VM_FLAGS_FIXED). 269 * 270 * VM_FLAGS_NO_CACHE 271 * Pages brought in to this VM region are placed on the speculative 272 * queue instead of the active queue. In other words, they are not 273 * cached so that they will be stolen first if memory runs low. 274 */ 275 276 #define VM_FLAGS_FIXED 0x00000000 277 #define VM_FLAGS_ANYWHERE 0x00000001 278 #define VM_FLAGS_PURGABLE 0x00000002 279 #define VM_FLAGS_4GB_CHUNK 0x00000004 280 #define VM_FLAGS_RANDOM_ADDR 0x00000008 281 #define VM_FLAGS_NO_CACHE 0x00000010 282 #define VM_FLAGS_RESILIENT_CODESIGN 0x00000020 283 #define VM_FLAGS_RESILIENT_MEDIA 0x00000040 284 #define VM_FLAGS_PERMANENT 0x00000080 285 #define VM_FLAGS_TPRO 0x00001000 286 #define VM_FLAGS_OVERWRITE 0x00004000 /* delete any existing mappings first */ 287 /* 288 * VM_FLAGS_SUPERPAGE_MASK 289 * 3 bits that specify whether large pages should be used instead of 290 * base pages (!=0), as well as the requested page size. 291 */ 292 #define VM_FLAGS_SUPERPAGE_MASK 0x00070000 /* bits 0x10000, 0x20000, 0x40000 */ 293 #define VM_FLAGS_RETURN_DATA_ADDR 0x00100000 /* Return address of target data, rather than base of page */ 294 #define VM_FLAGS_RETURN_4K_DATA_ADDR 0x00800000 /* Return 4K aligned address of target data */ 295 #define VM_FLAGS_ALIAS_MASK 0xFF000000 296 #define VM_GET_FLAGS_ALIAS(flags, alias) \ 297 (alias) = (((flags) >> 24) & 0xff) 298 #if !XNU_KERNEL_PRIVATE 299 #define VM_SET_FLAGS_ALIAS(flags, alias) \ 300 (flags) = (((flags) & ~VM_FLAGS_ALIAS_MASK) | \ 301 (((alias) & ~VM_FLAGS_ALIAS_MASK) << 24)) 302 #endif /* !XNU_KERNEL_PRIVATE */ 303 304 #if XNU_KERNEL_PRIVATE 305 /* 306 * When making a new VM_FLAG_*: 307 * - add it to this mask 308 * - add a vmf_* field to vm_map_kernel_flags_t in the right spot 309 * - add a check in vm_map_kernel_flags_check_vmflags() 310 * - update tests vm_parameter_validation_[user|kern] and their expected 311 * results; they deliberately call VM functions with invalid flag values 312 * and you may be turning one of those invalid flags valid. 313 */ 314 #define VM_FLAGS_ANY_MASK (VM_FLAGS_FIXED | \ 315 VM_FLAGS_ANYWHERE | \ 316 VM_FLAGS_PURGABLE | \ 317 VM_FLAGS_4GB_CHUNK | \ 318 VM_FLAGS_RANDOM_ADDR | \ 319 VM_FLAGS_NO_CACHE | \ 320 VM_FLAGS_RESILIENT_CODESIGN | \ 321 VM_FLAGS_RESILIENT_MEDIA | \ 322 VM_FLAGS_PERMANENT | \ 323 VM_FLAGS_TPRO | \ 324 VM_FLAGS_OVERWRITE | \ 325 VM_FLAGS_SUPERPAGE_MASK | \ 326 VM_FLAGS_RETURN_DATA_ADDR | \ 327 VM_FLAGS_RETURN_4K_DATA_ADDR | \ 328 VM_FLAGS_ALIAS_MASK) 329 #endif /* XNU_KERNEL_PRIVATE */ 330 #define VM_FLAGS_HW (VM_FLAGS_TPRO) 331 332 /* These are the flags that we accept from user-space */ 333 #define VM_FLAGS_USER_ALLOCATE (VM_FLAGS_FIXED | \ 334 VM_FLAGS_ANYWHERE | \ 335 VM_FLAGS_PURGABLE | \ 336 VM_FLAGS_4GB_CHUNK | \ 337 VM_FLAGS_RANDOM_ADDR | \ 338 VM_FLAGS_NO_CACHE | \ 339 VM_FLAGS_PERMANENT | \ 340 VM_FLAGS_OVERWRITE | \ 341 VM_FLAGS_SUPERPAGE_MASK | \ 342 VM_FLAGS_HW | \ 343 VM_FLAGS_ALIAS_MASK) 344 345 #define VM_FLAGS_USER_MAP (VM_FLAGS_USER_ALLOCATE | \ 346 VM_FLAGS_RETURN_4K_DATA_ADDR | \ 347 VM_FLAGS_RETURN_DATA_ADDR) 348 349 #define VM_FLAGS_USER_REMAP (VM_FLAGS_FIXED | \ 350 VM_FLAGS_ANYWHERE | \ 351 VM_FLAGS_RANDOM_ADDR | \ 352 VM_FLAGS_OVERWRITE| \ 353 VM_FLAGS_RETURN_DATA_ADDR | \ 354 VM_FLAGS_RESILIENT_CODESIGN | \ 355 VM_FLAGS_RESILIENT_MEDIA) 356 357 #define VM_FLAGS_SUPERPAGE_SHIFT 16 358 #define SUPERPAGE_NONE 0 /* no superpages, if all bits are 0 */ 359 #define SUPERPAGE_SIZE_ANY 1 360 #define VM_FLAGS_SUPERPAGE_NONE (SUPERPAGE_NONE << VM_FLAGS_SUPERPAGE_SHIFT) 361 #define VM_FLAGS_SUPERPAGE_SIZE_ANY (SUPERPAGE_SIZE_ANY << VM_FLAGS_SUPERPAGE_SHIFT) 362 #if defined(__x86_64__) || !defined(KERNEL) 363 #define SUPERPAGE_SIZE_2MB 2 364 #define VM_FLAGS_SUPERPAGE_SIZE_2MB (SUPERPAGE_SIZE_2MB<<VM_FLAGS_SUPERPAGE_SHIFT) 365 #endif 366 367 /* 368 * EXC_GUARD definitions for virtual memory. 369 */ 370 #define GUARD_TYPE_VIRT_MEMORY 0x5 371 372 /* Reasons for exception for virtual memory */ 373 __enum_decl(virtual_memory_guard_exception_code_t, uint32_t, { 374 kGUARD_EXC_DEALLOC_GAP = 1, 375 kGUARD_EXC_RECLAIM_COPYIO_FAILURE = 2, 376 kGUARD_EXC_RECLAIM_INDEX_FAILURE = 4, 377 kGUARD_EXC_RECLAIM_DEALLOCATE_FAILURE = 8, 378 kGUARD_EXC_RECLAIM_ACCOUNTING_FAILURE = 9, 379 kGUARD_EXC_SEC_IOPL_ON_EXEC_PAGE = 10, 380 kGUARD_EXC_SEC_EXEC_ON_IOPL_PAGE = 11, 381 kGUARD_EXC_SEC_UPL_WRITE_ON_EXEC_REGION = 12, 382 /* 383 * rdar://151450801 (Remove spurious kGUARD_EXC_SEC_ACCESS_FAULT and kGUARD_EXC_SEC_ASYNC_ACCESS_FAULT once CrashReporter is aligned) 384 */ 385 kGUARD_EXC_SEC_ACCESS_FAULT = 98, 386 kGUARD_EXC_SEC_ASYNC_ACCESS_FAULT = 99, 387 /* VM policy decisions */ 388 kGUARD_EXC_SEC_COPY_DENIED = 100, 389 kGUARD_EXC_SEC_SHARING_DENIED = 101, 390 391 }); 392 393 394 #ifdef XNU_KERNEL_PRIVATE 395 396 397 #pragma mark Map Ranges 398 399 /*! 400 * @enum vm_map_range_id_t 401 * 402 * @brief 403 * Enumerate a particular vm_map range. 404 * 405 * @discussion 406 * The kernel_map VA has been split into the following ranges. Userspace 407 * VA for any given process can also optionally be split by the following user 408 * ranges. 409 * 410 * @const KMEM_RANGE_ID_NONE 411 * This range is only used for early initialization. 412 * 413 * @const KMEM_RANGE_ID_PTR_* 414 * Range containing general purpose allocations from kalloc, etc that 415 * contain pointers. 416 * 417 * @const KMEM_RANGE_ID_SPRAYQTN 418 * The spray quarantine range contains allocations that have the following 419 * properties: 420 * - An attacker could control the size, lifetime and number of allocations 421 * of this type (or from this callsite). 422 * - The pointer to the allocation is zeroed to ensure that it isn't left 423 * dangling limiting the use of UaFs. 424 * - OOBs on the allocation is carefully considered and sufficiently 425 * addressed. 426 * 427 * @const KMEM_RANGE_ID_DATA 428 * Range containing allocations that are bags of bytes and contain no 429 * pointers. 430 * 431 * @const KMEM_RANGE_ID_DATA_SHARED 432 * Range containing allocations that are bags of bytes and contain no 433 * pointers and meant to be shared with external domains. 434 */ 435 __enum_decl(vm_map_range_id_t, uint8_t, { 436 KMEM_RANGE_ID_NONE, 437 KMEM_RANGE_ID_PTR_0, 438 KMEM_RANGE_ID_PTR_1, 439 KMEM_RANGE_ID_PTR_2, 440 KMEM_RANGE_ID_SPRAYQTN, 441 KMEM_RANGE_ID_DATA, 442 KMEM_RANGE_ID_DATA_SHARED, 443 444 KMEM_RANGE_ID_FIRST = KMEM_RANGE_ID_PTR_0, 445 KMEM_RANGE_ID_NUM_PTR = KMEM_RANGE_ID_PTR_2, 446 KMEM_RANGE_ID_MAX = KMEM_RANGE_ID_DATA_SHARED, 447 448 /* these UMEM_* correspond to the MACH_VM_RANGE_* tags and are ABI */ 449 UMEM_RANGE_ID_DEFAULT = 0, /* same as MACH_VM_RANGE_DEFAULT */ 450 UMEM_RANGE_ID_HEAP, /* same as MACH_VM_RANGE_DATA */ 451 UMEM_RANGE_ID_FIXED, /* same as MACH_VM_RANGE_FIXED */ 452 UMEM_RANGE_ID_LARGE_FILE, 453 454 /* these UMEM_* are XNU internal only range IDs, and aren't ABI */ 455 UMEM_RANGE_ID_MAX = UMEM_RANGE_ID_LARGE_FILE, 456 457 #define KMEM_RANGE_COUNT (KMEM_RANGE_ID_MAX + 1) 458 }); 459 460 typedef vm_map_range_id_t kmem_range_id_t; 461 462 #define kmem_log2down(mask) (31 - __builtin_clz(mask)) 463 #define KMEM_RANGE_MAX (UMEM_RANGE_ID_MAX < KMEM_RANGE_ID_MAX \ 464 ? KMEM_RANGE_ID_MAX : UMEM_RANGE_ID_MAX) 465 #define KMEM_RANGE_BITS kmem_log2down(2 * KMEM_RANGE_MAX - 1) 466 467 #pragma mark Kernel Flags 468 469 typedef union { 470 struct { 471 unsigned long long 472 /* 473 * VM_FLAG_* flags 474 */ 475 vmf_fixed:1, 476 vmf_purgeable:1, 477 vmf_4gb_chunk:1, 478 vmf_random_addr:1, 479 vmf_no_cache:1, 480 vmf_resilient_codesign:1, 481 vmf_resilient_media:1, 482 vmf_permanent:1, 483 484 __unused_bit_8:1, 485 __unused_bit_9:1, 486 __unused_bit_10:1, 487 __unused_bit_11:1, 488 vmf_tpro:1, 489 __unused_bit_13:1, 490 vmf_overwrite:1, 491 __unused_bit_15:1, 492 493 vmf_superpage_size:3, 494 __unused_bit_19:1, 495 vmf_return_data_addr:1, 496 __unused_bit_21:1, 497 __unused_bit_22:1, 498 vmf_return_4k_data_addr:1, 499 500 /* 501 * VM tag (user or kernel) 502 * 503 * User tags are limited to 8 bits, 504 * kernel tags can use up to 12 bits 505 * with -zt or similar features. 506 */ 507 vm_tag : 12, /* same as VME_ALIAS_BITS */ 508 509 /* 510 * General kernel flags 511 */ 512 vmkf_already:1, /* OK if same mapping already exists */ 513 vmkf_beyond_max:1, /* map beyond the map's max offset */ 514 vmkf_no_pmap_check:1, /* do not check that pmap is empty */ 515 vmkf_map_jit:1, /* mark entry as JIT region */ 516 vmkf_iokit_acct:1, /* IOKit accounting */ 517 vmkf_keep_map_locked:1, /* keep map locked when returning from vm_map_enter() */ 518 vmkf_overwrite_immutable:1, /* can overwrite immutable mappings */ 519 vmkf_remap_prot_copy:1, /* vm_remap for VM_PROT_COPY */ 520 vmkf_remap_legacy_mode:1, /* vm_remap, not vm_remap_new */ 521 vmkf_cs_enforcement_override:1, /* override CS_ENFORCEMENT */ 522 vmkf_cs_enforcement:1, /* new value for CS_ENFORCEMENT */ 523 vmkf_nested_pmap:1, /* use a nested pmap */ 524 vmkf_no_copy_on_read:1, /* do not use copy_on_read */ 525 vmkf_copy_single_object:1, /* vm_map_copy only 1 VM object */ 526 vmkf_copy_pageable:1, /* vm_map_copy with pageable entries */ 527 vmkf_copy_same_map:1, /* vm_map_copy to remap in original map */ 528 vmkf_translated_allow_execute:1, /* allow execute in translated processes */ 529 vmkf_tpro_enforcement_override:1, /* override TPRO propagation */ 530 vmkf_no_soft_limit:1, /* override soft allocation size limit */ 531 532 /* 533 * Submap creation, altering vm_map_enter() only 534 */ 535 vmkf_submap:1, /* mapping a VM submap */ 536 vmkf_submap_atomic:1, /* keep entry atomic (no splitting/coalescing) */ 537 vmkf_submap_adjust:1, /* the submap needs to be adjusted */ 538 539 /* 540 * Flags altering the behavior of vm_map_locate_space_anywhere() 541 */ 542 vmkf_32bit_map_va:1, /* allocate in low 32-bits range */ 543 vmkf_guard_before:1, /* guard page before the mapping */ 544 vmkf_last_free:1, /* find space from the end */ 545 vmkf_range_id:KMEM_RANGE_BITS; /* kmem range to allocate in */ 546 547 unsigned long long 548 __vmkf_unused2:64; 549 }; 550 551 /* 552 * do not access these directly, 553 * use vm_map_kernel_flags_check_vmflags*() 554 */ 555 uint32_t __vm_flags : 24; 556 } vm_map_kernel_flags_t; 557 558 /* 559 * using this means that vmf_* flags can't be used 560 * until vm_map_kernel_flags_set_vmflags() is set, 561 * or some manual careful init is done. 562 * 563 * Prefer VM_MAP_KERNEL_FLAGS_(FIXED,ANYWHERE) instead. 564 */ 565 #define VM_MAP_KERNEL_FLAGS_NONE \ 566 (vm_map_kernel_flags_t){ } 567 568 #define VM_MAP_KERNEL_FLAGS_FIXED(...) \ 569 (vm_map_kernel_flags_t){ .vmf_fixed = true, __VA_ARGS__ } 570 571 #define VM_MAP_KERNEL_FLAGS_ANYWHERE(...) \ 572 (vm_map_kernel_flags_t){ .vmf_fixed = false, __VA_ARGS__ } 573 574 #define VM_MAP_KERNEL_FLAGS_FIXED_PERMANENT(...) \ 575 VM_MAP_KERNEL_FLAGS_FIXED(.vmf_permanent = true, __VA_ARGS__) 576 577 #define VM_MAP_KERNEL_FLAGS_ANYWHERE_PERMANENT(...) \ 578 VM_MAP_KERNEL_FLAGS_ANYWHERE(.vmf_permanent = true, __VA_ARGS__) 579 580 #define VM_MAP_KERNEL_FLAGS_DATA_BUFFERS_ANYWHERE(...) \ 581 VM_MAP_KERNEL_FLAGS_ANYWHERE(.vmkf_range_id = KMEM_RANGE_ID_DATA, __VA_ARGS__) 582 583 #define VM_MAP_KERNEL_FLAGS_DATA_SHARED_ANYWHERE(...) \ 584 VM_MAP_KERNEL_FLAGS_ANYWHERE(.vmkf_range_id = kmem_needs_data_share_range() ? \ 585 KMEM_RANGE_ID_DATA_SHARED : KMEM_RANGE_ID_DATA, __VA_ARGS__) 586 587 typedef struct { 588 unsigned int 589 vmnekf_ledger_tag:3, 590 vmnekf_ledger_no_footprint:1, 591 __vmnekf_unused:28; 592 } vm_named_entry_kernel_flags_t; 593 #define VM_NAMED_ENTRY_KERNEL_FLAGS_NONE (vm_named_entry_kernel_flags_t) { \ 594 .vmnekf_ledger_tag = 0, \ 595 .vmnekf_ledger_no_footprint = 0, \ 596 .__vmnekf_unused = 0 \ 597 } 598 599 #endif /* XNU_KERNEL_PRIVATE */ 600 601 #pragma mark Ledger Tags 602 603 /* current accounting postmark */ 604 #define __VM_LEDGER_ACCOUNTING_POSTMARK 2019032600 605 606 /* 607 * When making a new VM_LEDGER_TAG_* or VM_LEDGER_FLAG_*, update tests 608 * vm_parameter_validation_[user|kern] and their expected results; they 609 * deliberately call VM functions with invalid ledger values and you may 610 * be turning one of those invalid tags/flags valid. 611 */ 612 /* discrete values: */ 613 #define VM_LEDGER_TAG_NONE 0x00000000 614 #define VM_LEDGER_TAG_DEFAULT 0x00000001 615 #define VM_LEDGER_TAG_NETWORK 0x00000002 616 #define VM_LEDGER_TAG_MEDIA 0x00000003 617 #define VM_LEDGER_TAG_GRAPHICS 0x00000004 618 #define VM_LEDGER_TAG_NEURAL 0x00000005 619 #define VM_LEDGER_TAG_MAX 0x00000005 620 #define VM_LEDGER_TAG_UNCHANGED ((int)-1) 621 622 /* individual bits: */ 623 #define VM_LEDGER_FLAG_NO_FOOTPRINT (1 << 0) 624 #define VM_LEDGER_FLAG_NO_FOOTPRINT_FOR_DEBUG (1 << 1) 625 #define VM_LEDGER_FLAG_FROM_KERNEL (1 << 2) 626 627 #define VM_LEDGER_FLAGS_USER (VM_LEDGER_FLAG_NO_FOOTPRINT | VM_LEDGER_FLAG_NO_FOOTPRINT_FOR_DEBUG) 628 #define VM_LEDGER_FLAGS_ALL (VM_LEDGER_FLAGS_USER | VM_LEDGER_FLAG_FROM_KERNEL) 629 630 #pragma mark User Memory Tags 631 632 /* 633 * These tags may be used to identify memory regions created with 634 * `mach_vm_map()` or `mach_vm_allocate()` via the top 8 bits of the `flags` 635 * parameter. Users should pass `VM_MAKE_TAG(tag) | flags` (see section 636 * "User Flags"). 637 */ 638 #define VM_MEMORY_MALLOC 1 639 #define VM_MEMORY_MALLOC_SMALL 2 640 #define VM_MEMORY_MALLOC_LARGE 3 641 #define VM_MEMORY_MALLOC_HUGE 4 642 #define VM_MEMORY_SBRK 5// uninteresting -- no one should call 643 #define VM_MEMORY_REALLOC 6 644 #define VM_MEMORY_MALLOC_TINY 7 645 #define VM_MEMORY_MALLOC_LARGE_REUSABLE 8 646 #define VM_MEMORY_MALLOC_LARGE_REUSED 9 647 648 #define VM_MEMORY_ANALYSIS_TOOL 10 649 650 #define VM_MEMORY_MALLOC_NANO 11 651 #define VM_MEMORY_MALLOC_MEDIUM 12 652 #define VM_MEMORY_MALLOC_PROB_GUARD 13 653 654 #define VM_MEMORY_MACH_MSG 20 655 #define VM_MEMORY_IOKIT 21 656 #define VM_MEMORY_STACK 30 657 #define VM_MEMORY_GUARD 31 658 #define VM_MEMORY_SHARED_PMAP 32 659 /* memory containing a dylib */ 660 #define VM_MEMORY_DYLIB 33 661 #define VM_MEMORY_OBJC_DISPATCHERS 34 662 663 /* Was a nested pmap (VM_MEMORY_SHARED_PMAP) which has now been unnested */ 664 #define VM_MEMORY_UNSHARED_PMAP 35 665 666 /* for libchannel memory, mostly used on visionOS for communication with realtime threads */ 667 #define VM_MEMORY_LIBCHANNEL 36 668 669 // Placeholders for now -- as we analyze the libraries and find how they 670 // use memory, we can make these labels more specific. 671 #define VM_MEMORY_APPKIT 40 672 #define VM_MEMORY_FOUNDATION 41 673 #define VM_MEMORY_COREGRAPHICS 42 674 #define VM_MEMORY_CORESERVICES 43 675 #define VM_MEMORY_CARBON VM_MEMORY_CORESERVICES 676 #define VM_MEMORY_JAVA 44 677 #define VM_MEMORY_COREDATA 45 678 #define VM_MEMORY_COREDATA_OBJECTIDS 46 679 680 #define VM_MEMORY_ATS 50 681 #define VM_MEMORY_LAYERKIT 51 682 #define VM_MEMORY_CGIMAGE 52 683 #define VM_MEMORY_TCMALLOC 53 684 685 /* private raster data (i.e. layers, some images, QGL allocator) */ 686 #define VM_MEMORY_COREGRAPHICS_DATA 54 687 688 /* shared image and font caches */ 689 #define VM_MEMORY_COREGRAPHICS_SHARED 55 690 691 /* Memory used for virtual framebuffers, shadowing buffers, etc... */ 692 #define VM_MEMORY_COREGRAPHICS_FRAMEBUFFERS 56 693 694 /* Window backing stores, custom shadow data, and compressed backing stores */ 695 #define VM_MEMORY_COREGRAPHICS_BACKINGSTORES 57 696 697 /* x-alloc'd memory */ 698 #define VM_MEMORY_COREGRAPHICS_XALLOC 58 699 700 /* catch-all for other uses, such as the read-only shared data page */ 701 #define VM_MEMORY_COREGRAPHICS_MISC VM_MEMORY_COREGRAPHICS 702 703 /* memory allocated by the dynamic loader for itself */ 704 #define VM_MEMORY_DYLD 60 705 /* malloc'd memory created by dyld */ 706 #define VM_MEMORY_DYLD_MALLOC 61 707 708 /* Used for sqlite page cache */ 709 #define VM_MEMORY_SQLITE 62 710 711 /* JavaScriptCore heaps */ 712 #define VM_MEMORY_JAVASCRIPT_CORE 63 713 #define VM_MEMORY_WEBASSEMBLY VM_MEMORY_JAVASCRIPT_CORE 714 /* memory allocated for the JIT */ 715 #define VM_MEMORY_JAVASCRIPT_JIT_EXECUTABLE_ALLOCATOR 64 716 #define VM_MEMORY_JAVASCRIPT_JIT_REGISTER_FILE 65 717 718 /* memory allocated for GLSL */ 719 #define VM_MEMORY_GLSL 66 720 721 /* memory allocated for OpenCL.framework */ 722 #define VM_MEMORY_OPENCL 67 723 724 /* memory allocated for QuartzCore.framework */ 725 #define VM_MEMORY_COREIMAGE 68 726 727 /* memory allocated for WebCore Purgeable Buffers */ 728 #define VM_MEMORY_WEBCORE_PURGEABLE_BUFFERS 69 729 730 /* ImageIO memory */ 731 #define VM_MEMORY_IMAGEIO 70 732 733 /* CoreProfile memory */ 734 #define VM_MEMORY_COREPROFILE 71 735 736 /* assetsd / MobileSlideShow memory */ 737 #define VM_MEMORY_ASSETSD 72 738 739 /* libsystem_kernel os_once_alloc */ 740 #define VM_MEMORY_OS_ALLOC_ONCE 73 741 742 /* libdispatch internal allocator */ 743 #define VM_MEMORY_LIBDISPATCH 74 744 745 /* Accelerate.framework image backing stores */ 746 #define VM_MEMORY_ACCELERATE 75 747 748 /* CoreUI image block data */ 749 #define VM_MEMORY_COREUI 76 750 751 /* CoreUI image file */ 752 #define VM_MEMORY_COREUIFILE 77 753 754 /* Genealogy buffers */ 755 #define VM_MEMORY_GENEALOGY 78 756 757 /* RawCamera VM allocated memory */ 758 #define VM_MEMORY_RAWCAMERA 79 759 760 /* corpse info for dead process */ 761 #define VM_MEMORY_CORPSEINFO 80 762 763 /* Apple System Logger (ASL) messages */ 764 #define VM_MEMORY_ASL 81 765 766 /* Swift runtime */ 767 #define VM_MEMORY_SWIFT_RUNTIME 82 768 769 /* Swift metadata */ 770 #define VM_MEMORY_SWIFT_METADATA 83 771 772 /* DHMM data */ 773 #define VM_MEMORY_DHMM 84 774 775 /* memory needed for DFR related actions */ 776 #define VM_MEMORY_DFR 85 777 778 /* memory allocated by SceneKit.framework */ 779 #define VM_MEMORY_SCENEKIT 86 780 781 /* memory allocated by skywalk networking */ 782 #define VM_MEMORY_SKYWALK 87 783 784 #define VM_MEMORY_IOSURFACE 88 785 786 #define VM_MEMORY_LIBNETWORK 89 787 788 #define VM_MEMORY_AUDIO 90 789 790 #define VM_MEMORY_VIDEOBITSTREAM 91 791 792 /* memory allocated by CoreMedia */ 793 #define VM_MEMORY_CM_XPC 92 794 795 #define VM_MEMORY_CM_RPC 93 796 797 #define VM_MEMORY_CM_MEMORYPOOL 94 798 799 #define VM_MEMORY_CM_READCACHE 95 800 801 #define VM_MEMORY_CM_CRABS 96 802 803 /* memory allocated for QuickLookThumbnailing */ 804 #define VM_MEMORY_QUICKLOOK_THUMBNAILS 97 805 806 /* memory allocated by Accounts framework */ 807 #define VM_MEMORY_ACCOUNTS 98 808 809 /* memory allocated by Sanitizer runtime libraries */ 810 #define VM_MEMORY_SANITIZER 99 811 812 /* Differentiate memory needed by GPU drivers and frameworks from generic IOKit allocations */ 813 #define VM_MEMORY_IOACCELERATOR 100 814 815 /* memory allocated by CoreMedia for global image registration of frames */ 816 #define VM_MEMORY_CM_REGWARP 101 817 818 /* memory allocated by EmbeddedAcousticRecognition for speech decoder */ 819 #define VM_MEMORY_EAR_DECODER 102 820 821 /* CoreUI cached image data */ 822 #define VM_MEMORY_COREUI_CACHED_IMAGE_DATA 103 823 824 /* ColorSync is using mmap for read-only copies of ICC profile data */ 825 #define VM_MEMORY_COLORSYNC 104 826 827 /* backtrace info for simulated crashes */ 828 #define VM_MEMORY_BTINFO 105 829 830 /* memory allocated by CoreMedia */ 831 #define VM_MEMORY_CM_HLS 106 832 833 /* memory allocated for CompositorServices */ 834 #define VM_MEMORY_COMPOSITOR_SERVICES 107 835 836 /* Reserve 230-239 for Rosetta */ 837 #define VM_MEMORY_ROSETTA 230 838 #define VM_MEMORY_ROSETTA_THREAD_CONTEXT 231 839 #define VM_MEMORY_ROSETTA_INDIRECT_BRANCH_MAP 232 840 #define VM_MEMORY_ROSETTA_RETURN_STACK 233 841 #define VM_MEMORY_ROSETTA_EXECUTABLE_HEAP 234 842 #define VM_MEMORY_ROSETTA_USER_LDT 235 843 #define VM_MEMORY_ROSETTA_ARENA 236 844 #define VM_MEMORY_ROSETTA_10 239 845 846 /* Reserve 240-255 for application */ 847 #define VM_MEMORY_APPLICATION_SPECIFIC_1 240 848 #define VM_MEMORY_APPLICATION_SPECIFIC_2 241 849 #define VM_MEMORY_APPLICATION_SPECIFIC_3 242 850 #define VM_MEMORY_APPLICATION_SPECIFIC_4 243 851 #define VM_MEMORY_APPLICATION_SPECIFIC_5 244 852 #define VM_MEMORY_APPLICATION_SPECIFIC_6 245 853 #define VM_MEMORY_APPLICATION_SPECIFIC_7 246 854 #define VM_MEMORY_APPLICATION_SPECIFIC_8 247 855 #define VM_MEMORY_APPLICATION_SPECIFIC_9 248 856 #define VM_MEMORY_APPLICATION_SPECIFIC_10 249 857 #define VM_MEMORY_APPLICATION_SPECIFIC_11 250 858 #define VM_MEMORY_APPLICATION_SPECIFIC_12 251 859 #define VM_MEMORY_APPLICATION_SPECIFIC_13 252 860 #define VM_MEMORY_APPLICATION_SPECIFIC_14 253 861 #define VM_MEMORY_APPLICATION_SPECIFIC_15 254 862 #define VM_MEMORY_APPLICATION_SPECIFIC_16 255 863 864 #define VM_MEMORY_COUNT 256 865 866 #if !XNU_KERNEL_PRIVATE 867 #define VM_MAKE_TAG(tag) ((tag) << 24) 868 #endif /* XNU_KERNEL_PRIVATE */ 869 870 #if PRIVATE && !KERNEL 871 /// 872 /// Return a human-readable description for a given VM user tag. 873 /// 874 /// - Parameters: 875 /// - tag: A VM tag between `[0,VM_MEMORY_COUNT)` 876 /// 877 /// - Returns: A string literal description of the tag 878 /// 879 __SPI_AVAILABLE(macos(16.0), ios(19.0), watchos(12.0), tvos(19.0), visionos(3.0), bridgeos(10.0)) 880 OS_EXPORT 881 const char *mach_vm_tag_describe(unsigned int tag); 882 #endif /* PRIVATE && !KERNEL */ 883 884 #if KERNEL_PRIVATE 885 886 #pragma mark Kernel Tags 887 888 #if XNU_KERNEL_PRIVATE 889 /* 890 * When making a new VM_KERN_MEMORY_*, update: 891 * - tests vm_parameter_validation_[user|kern] 892 * and their expected results; they deliberately call VM functions with invalid 893 * kernel tag values and you may be turning one of those invalid tags valid. 894 * - vm_kern_memory_names, which is used to map tags to their string name 895 */ 896 #endif /* XNU_KERNEL_PRIVATE */ 897 898 #define VM_KERN_MEMORY_NONE 0 899 900 #define VM_KERN_MEMORY_OSFMK 1 901 #define VM_KERN_MEMORY_BSD 2 902 #define VM_KERN_MEMORY_IOKIT 3 903 #define VM_KERN_MEMORY_LIBKERN 4 904 #define VM_KERN_MEMORY_OSKEXT 5 905 #define VM_KERN_MEMORY_KEXT 6 906 #define VM_KERN_MEMORY_IPC 7 907 #define VM_KERN_MEMORY_STACK 8 908 #define VM_KERN_MEMORY_CPU 9 909 #define VM_KERN_MEMORY_PMAP 10 910 #define VM_KERN_MEMORY_PTE 11 911 #define VM_KERN_MEMORY_ZONE 12 912 #define VM_KERN_MEMORY_KALLOC 13 913 #define VM_KERN_MEMORY_COMPRESSOR 14 914 #define VM_KERN_MEMORY_COMPRESSED_DATA 15 915 #define VM_KERN_MEMORY_PHANTOM_CACHE 16 916 #define VM_KERN_MEMORY_WAITQ 17 917 #define VM_KERN_MEMORY_DIAG 18 918 #define VM_KERN_MEMORY_LOG 19 919 #define VM_KERN_MEMORY_FILE 20 920 #define VM_KERN_MEMORY_MBUF 21 921 #define VM_KERN_MEMORY_UBC 22 922 #define VM_KERN_MEMORY_SECURITY 23 923 #define VM_KERN_MEMORY_MLOCK 24 924 #define VM_KERN_MEMORY_REASON 25 925 #define VM_KERN_MEMORY_SKYWALK 26 926 #define VM_KERN_MEMORY_LTABLE 27 927 #define VM_KERN_MEMORY_HV 28 928 #define VM_KERN_MEMORY_KALLOC_DATA 29 929 #define VM_KERN_MEMORY_RETIRED 30 930 #define VM_KERN_MEMORY_KALLOC_TYPE 31 931 #define VM_KERN_MEMORY_TRIAGE 32 932 #define VM_KERN_MEMORY_RECOUNT 33 933 #define VM_KERN_MEMORY_EXCLAVES 35 934 #define VM_KERN_MEMORY_EXCLAVES_SHARED 36 935 #define VM_KERN_MEMORY_KALLOC_SHARED 37 936 /* add new tags here and adjust first-dynamic value */ 937 #define VM_KERN_MEMORY_CPUTRACE 38 938 #define VM_KERN_MEMORY_FIRST_DYNAMIC 39 939 940 /* out of tags: */ 941 #define VM_KERN_MEMORY_ANY 255 942 #define VM_KERN_MEMORY_COUNT 256 943 944 #pragma mark Kernel Wired Counts 945 946 // mach_memory_info.flags 947 #define VM_KERN_SITE_TYPE 0x000000FF 948 #define VM_KERN_SITE_TAG 0x00000000 949 #define VM_KERN_SITE_KMOD 0x00000001 950 #define VM_KERN_SITE_KERNEL 0x00000002 951 #define VM_KERN_SITE_COUNTER 0x00000003 952 #define VM_KERN_SITE_WIRED 0x00000100 /* add to wired count */ 953 #define VM_KERN_SITE_HIDE 0x00000200 /* no zprint */ 954 #define VM_KERN_SITE_NAMED 0x00000400 955 #define VM_KERN_SITE_ZONE 0x00000800 956 #define VM_KERN_SITE_ZONE_VIEW 0x00001000 957 #define VM_KERN_SITE_KALLOC 0x00002000 /* zone field is size class */ 958 959 /* Kernel Memory Counters */ 960 #if XNU_KERNEL_PRIVATE 961 /* 962 * When making a new VM_KERN_COUNT_*, also update vm_kern_count_names 963 */ 964 #endif /* XNU_KERNEL_PRIVATE */ 965 966 #define VM_KERN_COUNT_MANAGED 0 967 #define VM_KERN_COUNT_RESERVED 1 968 #define VM_KERN_COUNT_WIRED 2 969 #define VM_KERN_COUNT_WIRED_MANAGED 3 970 #define VM_KERN_COUNT_STOLEN 4 971 #define VM_KERN_COUNT_LOPAGE 5 972 #define VM_KERN_COUNT_MAP_KERNEL 6 973 #define VM_KERN_COUNT_MAP_ZONE 7 974 #define VM_KERN_COUNT_MAP_KALLOC 8 975 976 #define VM_KERN_COUNT_WIRED_BOOT 9 977 978 #define VM_KERN_COUNT_BOOT_STOLEN 10 979 980 /* The number of bytes from the kernel cache that are wired in memory */ 981 #define VM_KERN_COUNT_WIRED_STATIC_KERNELCACHE 11 982 983 #define VM_KERN_COUNT_MAP_KALLOC_LARGE VM_KERN_COUNT_MAP_KALLOC 984 #define VM_KERN_COUNT_MAP_KALLOC_LARGE_DATA 12 985 #define VM_KERN_COUNT_MAP_KERNEL_DATA 13 986 987 /* The size of the exclaves iboot carveout (exclaves memory not from XNU) in bytes. */ 988 #define VM_KERN_COUNT_EXCLAVES_CARVEOUT 14 989 990 /* The number of VM_KERN_COUNT_ stats. New VM_KERN_COUNT_ entries should be less than this. */ 991 #define VM_KERN_COUNTER_COUNT 15 992 993 #endif /* KERNEL_PRIVATE */ 994 995 __END_DECLS 996 997 #endif /* _MACH_VM_STATISTICS_H_ */ 998