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/pmap.h 60 * Author: Avadis Tevanian, Jr. 61 * Date: 1985 62 * 63 * Machine address mapping definitions -- machine-independent 64 * section. [For machine-dependent section, see "machine/pmap.h".] 65 */ 66 67 #ifndef _VM_PMAP_H_ 68 #define _VM_PMAP_H_ 69 70 #include <mach/kern_return.h> 71 #include <mach/vm_param.h> 72 #include <mach/vm_types.h> 73 #include <mach/vm_attributes.h> 74 #include <mach/boolean.h> 75 #include <mach/vm_prot.h> 76 #include <kern/trustcache.h> 77 78 #if __has_include(<CoreEntitlements/CoreEntitlements.h>) 79 #include <CoreEntitlements/CoreEntitlements.h> 80 #endif 81 82 #ifdef KERNEL_PRIVATE 83 84 /* 85 * The following is a description of the interface to the 86 * machine-dependent "physical map" data structure. The module 87 * must provide a "pmap_t" data type that represents the 88 * set of valid virtual-to-physical addresses for one user 89 * address space. [The kernel address space is represented 90 * by a distinguished "pmap_t".] The routines described manage 91 * this type, install and update virtual-to-physical mappings, 92 * and perform operations on physical addresses common to 93 * many address spaces. 94 */ 95 96 /* Copy between a physical page and a virtual address */ 97 /* LP64todo - switch to vm_map_offset_t when it grows */ 98 extern kern_return_t copypv( 99 addr64_t source, 100 addr64_t sink, 101 unsigned int size, 102 int which); 103 #define cppvPsnk 1 104 #define cppvPsnkb 31 105 #define cppvPsrc 2 106 #define cppvPsrcb 30 107 #define cppvFsnk 4 108 #define cppvFsnkb 29 109 #define cppvFsrc 8 110 #define cppvFsrcb 28 111 #define cppvNoModSnk 16 112 #define cppvNoModSnkb 27 113 #define cppvNoRefSrc 32 114 #define cppvNoRefSrcb 26 115 #define cppvKmap 64 /* Use the kernel's vm_map */ 116 #define cppvKmapb 25 117 118 extern boolean_t pmap_has_managed_page(ppnum_t first, ppnum_t last); 119 120 #if MACH_KERNEL_PRIVATE || BSD_KERNEL_PRIVATE 121 #include <mach/mach_types.h> 122 #include <vm/memory_types.h> 123 124 /* 125 * Routines used during BSD process creation. 126 */ 127 128 extern pmap_t pmap_create_options( /* Create a pmap_t. */ 129 ledger_t ledger, 130 vm_map_size_t size, 131 unsigned int flags); 132 133 #if __has_feature(ptrauth_calls) && (defined(XNU_TARGET_OS_OSX) || (DEVELOPMENT || DEBUG)) 134 /** 135 * Informs the pmap layer that a process will be running with user JOP disabled, 136 * as if PMAP_CREATE_DISABLE_JOP had been passed during pmap creation. 137 * 138 * @note This function cannot be used once the target process has started 139 * executing code. It is intended for cases where user JOP is disabled based on 140 * the code signature (e.g., special "keys-off" entitlements), which is too late 141 * to change the flags passed to pmap_create_options. 142 * 143 * @param pmap The pmap belonging to the target process 144 */ 145 extern void pmap_disable_user_jop( 146 pmap_t pmap); 147 #endif /* __has_feature(ptrauth_calls) && (defined(XNU_TARGET_OS_OSX) || (DEVELOPMENT || DEBUG)) */ 148 #endif /* MACH_KERNEL_PRIVATE || BSD_KERNEL_PRIVATE */ 149 150 #ifdef MACH_KERNEL_PRIVATE 151 152 #include <mach_assert.h> 153 154 #include <machine/pmap.h> 155 /* 156 * Routines used for initialization. 157 * There is traditionally also a pmap_bootstrap, 158 * used very early by machine-dependent code, 159 * but it is not part of the interface. 160 * 161 * LP64todo - 162 * These interfaces are tied to the size of the 163 * kernel pmap - and therefore use the "local" 164 * vm_offset_t, etc... types. 165 */ 166 167 extern void *pmap_steal_memory(vm_size_t size, vm_size_t alignment); /* Early memory allocation */ 168 extern void *pmap_steal_freeable_memory(vm_size_t size); /* Early memory allocation */ 169 extern void *pmap_steal_zone_memory(vm_size_t size, vm_size_t alignment); /* Early zone memory allocation */ 170 171 extern uint_t pmap_free_pages(void); /* report remaining unused physical pages */ 172 #if defined(__arm__) || defined(__arm64__) 173 extern uint_t pmap_free_pages_span(void); /* report phys address range of unused physical pages */ 174 #endif /* defined(__arm__) || defined(__arm64__) */ 175 176 extern void pmap_startup(vm_offset_t *startp, vm_offset_t *endp); /* allocate vm_page structs */ 177 178 extern void pmap_init(void); /* Initialization, once we have kernel virtual memory. */ 179 180 extern void mapping_adjust(void); /* Adjust free mapping count */ 181 182 extern void mapping_free_prime(void); /* Primes the mapping block release list */ 183 184 #ifndef MACHINE_PAGES 185 /* 186 * If machine/pmap.h defines MACHINE_PAGES, it must implement 187 * the above functions. The pmap module has complete control. 188 * Otherwise, it must implement the following functions: 189 * pmap_free_pages 190 * pmap_virtual_space 191 * pmap_next_page 192 * pmap_init 193 * and vm/vm_resident.c implements pmap_steal_memory and pmap_startup 194 * using pmap_free_pages, pmap_next_page, pmap_virtual_space, 195 * and pmap_enter. pmap_free_pages may over-estimate the number 196 * of unused physical pages, and pmap_next_page may return FALSE 197 * to indicate that there are no more unused pages to return. 198 * However, for best performance pmap_free_pages should be accurate. 199 */ 200 201 /* 202 * Routines to return the next unused physical page. 203 */ 204 extern boolean_t pmap_next_page(ppnum_t *pnum); 205 extern boolean_t pmap_next_page_hi(ppnum_t *pnum, boolean_t might_free); 206 #ifdef __x86_64__ 207 extern kern_return_t pmap_next_page_large(ppnum_t *pnum); 208 extern void pmap_hi_pages_done(void); 209 #endif 210 211 /* 212 * Report virtual space available for the kernel. 213 */ 214 extern void pmap_virtual_space( 215 vm_offset_t *virtual_start, 216 vm_offset_t *virtual_end); 217 #endif /* MACHINE_PAGES */ 218 219 /* 220 * Routines to manage the physical map data structure. 221 */ 222 extern pmap_t(pmap_kernel)(void); /* Return the kernel's pmap */ 223 extern void pmap_reference(pmap_t pmap); /* Gain a reference. */ 224 extern void pmap_destroy(pmap_t pmap); /* Release a reference. */ 225 extern void pmap_switch(pmap_t); 226 extern void pmap_require(pmap_t pmap); 227 228 #if MACH_ASSERT 229 extern void pmap_set_process(pmap_t pmap, 230 int pid, 231 char *procname); 232 #endif /* MACH_ASSERT */ 233 234 extern kern_return_t pmap_enter( /* Enter a mapping */ 235 pmap_t pmap, 236 vm_map_offset_t v, 237 ppnum_t pn, 238 vm_prot_t prot, 239 vm_prot_t fault_type, 240 unsigned int flags, 241 boolean_t wired); 242 243 extern kern_return_t pmap_enter_options( 244 pmap_t pmap, 245 vm_map_offset_t v, 246 ppnum_t pn, 247 vm_prot_t prot, 248 vm_prot_t fault_type, 249 unsigned int flags, 250 boolean_t wired, 251 unsigned int options, 252 void *arg); 253 extern kern_return_t pmap_enter_options_addr( 254 pmap_t pmap, 255 vm_map_offset_t v, 256 pmap_paddr_t pa, 257 vm_prot_t prot, 258 vm_prot_t fault_type, 259 unsigned int flags, 260 boolean_t wired, 261 unsigned int options, 262 void *arg); 263 264 extern void pmap_remove_some_phys( 265 pmap_t pmap, 266 ppnum_t pn); 267 268 extern void pmap_lock_phys_page( 269 ppnum_t pn); 270 271 extern void pmap_unlock_phys_page( 272 ppnum_t pn); 273 274 275 /* 276 * Routines that operate on physical addresses. 277 */ 278 279 extern void pmap_page_protect( /* Restrict access to page. */ 280 ppnum_t phys, 281 vm_prot_t prot); 282 283 extern void pmap_page_protect_options( /* Restrict access to page. */ 284 ppnum_t phys, 285 vm_prot_t prot, 286 unsigned int options, 287 void *arg); 288 289 extern void(pmap_zero_page)( 290 ppnum_t pn); 291 292 extern void(pmap_zero_part_page)( 293 ppnum_t pn, 294 vm_offset_t offset, 295 vm_size_t len); 296 297 extern void(pmap_copy_page)( 298 ppnum_t src, 299 ppnum_t dest); 300 301 extern void(pmap_copy_part_page)( 302 ppnum_t src, 303 vm_offset_t src_offset, 304 ppnum_t dst, 305 vm_offset_t dst_offset, 306 vm_size_t len); 307 308 extern void(pmap_copy_part_lpage)( 309 vm_offset_t src, 310 ppnum_t dst, 311 vm_offset_t dst_offset, 312 vm_size_t len); 313 314 extern void(pmap_copy_part_rpage)( 315 ppnum_t src, 316 vm_offset_t src_offset, 317 vm_offset_t dst, 318 vm_size_t len); 319 320 extern unsigned int(pmap_disconnect)( /* disconnect mappings and return reference and change */ 321 ppnum_t phys); 322 323 extern unsigned int(pmap_disconnect_options)( /* disconnect mappings and return reference and change */ 324 ppnum_t phys, 325 unsigned int options, 326 void *arg); 327 328 extern kern_return_t(pmap_attribute_cache_sync)( /* Flush appropriate 329 * cache based on 330 * page number sent */ 331 ppnum_t pn, 332 vm_size_t size, 333 vm_machine_attribute_t attribute, 334 vm_machine_attribute_val_t* value); 335 336 extern unsigned int(pmap_cache_attributes)( 337 ppnum_t pn); 338 339 /* 340 * Set (override) cache attributes for the specified physical page 341 */ 342 extern void pmap_set_cache_attributes( 343 ppnum_t, 344 unsigned int); 345 346 extern void *pmap_map_compressor_page( 347 ppnum_t); 348 349 extern void pmap_unmap_compressor_page( 350 ppnum_t, 351 void*); 352 353 #if defined(__arm__) || defined(__arm64__) 354 extern bool pmap_batch_set_cache_attributes( 355 upl_page_info_array_t, 356 unsigned int, 357 unsigned int); 358 #endif 359 extern void pmap_sync_page_data_phys(ppnum_t pa); 360 extern void pmap_sync_page_attributes_phys(ppnum_t pa); 361 362 /* 363 * debug/assertions. pmap_verify_free returns true iff 364 * the given physical page is mapped into no pmap. 365 * pmap_assert_free() will panic() if pn is not free. 366 */ 367 extern bool pmap_verify_free(ppnum_t pn); 368 #if MACH_ASSERT 369 extern void pmap_assert_free(ppnum_t pn); 370 #endif 371 372 373 /* 374 * Sundry required (internal) routines 375 */ 376 #ifdef CURRENTLY_UNUSED_AND_UNTESTED 377 extern void pmap_collect(pmap_t pmap);/* Perform garbage 378 * collection, if any */ 379 #endif 380 /* 381 * Optional routines 382 */ 383 extern void(pmap_copy)( /* Copy range of mappings, 384 * if desired. */ 385 pmap_t dest, 386 pmap_t source, 387 vm_map_offset_t dest_va, 388 vm_map_size_t size, 389 vm_map_offset_t source_va); 390 391 extern kern_return_t(pmap_attribute)( /* Get/Set special memory 392 * attributes */ 393 pmap_t pmap, 394 vm_map_offset_t va, 395 vm_map_size_t size, 396 vm_machine_attribute_t attribute, 397 vm_machine_attribute_val_t* value); 398 399 /* 400 * Routines defined as macros. 401 */ 402 #ifndef PMAP_ACTIVATE_USER 403 #ifndef PMAP_ACTIVATE 404 #define PMAP_ACTIVATE_USER(thr, cpu) 405 #else /* PMAP_ACTIVATE */ 406 #define PMAP_ACTIVATE_USER(thr, cpu) { \ 407 pmap_t pmap; \ 408 \ 409 pmap = (thr)->map->pmap; \ 410 if (pmap != pmap_kernel()) \ 411 PMAP_ACTIVATE(pmap, (thr), (cpu)); \ 412 } 413 #endif /* PMAP_ACTIVATE */ 414 #endif /* PMAP_ACTIVATE_USER */ 415 416 #ifndef PMAP_DEACTIVATE_USER 417 #ifndef PMAP_DEACTIVATE 418 #define PMAP_DEACTIVATE_USER(thr, cpu) 419 #else /* PMAP_DEACTIVATE */ 420 #define PMAP_DEACTIVATE_USER(thr, cpu) { \ 421 pmap_t pmap; \ 422 \ 423 pmap = (thr)->map->pmap; \ 424 if ((pmap) != pmap_kernel()) \ 425 PMAP_DEACTIVATE(pmap, (thr), (cpu)); \ 426 } 427 #endif /* PMAP_DEACTIVATE */ 428 #endif /* PMAP_DEACTIVATE_USER */ 429 430 #ifndef PMAP_ACTIVATE_KERNEL 431 #ifndef PMAP_ACTIVATE 432 #define PMAP_ACTIVATE_KERNEL(cpu) 433 #else /* PMAP_ACTIVATE */ 434 #define PMAP_ACTIVATE_KERNEL(cpu) \ 435 PMAP_ACTIVATE(pmap_kernel(), THREAD_NULL, cpu) 436 #endif /* PMAP_ACTIVATE */ 437 #endif /* PMAP_ACTIVATE_KERNEL */ 438 439 #ifndef PMAP_DEACTIVATE_KERNEL 440 #ifndef PMAP_DEACTIVATE 441 #define PMAP_DEACTIVATE_KERNEL(cpu) 442 #else /* PMAP_DEACTIVATE */ 443 #define PMAP_DEACTIVATE_KERNEL(cpu) \ 444 PMAP_DEACTIVATE(pmap_kernel(), THREAD_NULL, cpu) 445 #endif /* PMAP_DEACTIVATE */ 446 #endif /* PMAP_DEACTIVATE_KERNEL */ 447 448 #ifndef PMAP_SET_CACHE_ATTR 449 #define PMAP_SET_CACHE_ATTR(mem, object, cache_attr, batch_pmap_op) \ 450 MACRO_BEGIN \ 451 if (!batch_pmap_op) { \ 452 pmap_set_cache_attributes(VM_PAGE_GET_PHYS_PAGE(mem), cache_attr); \ 453 object->set_cache_attr = TRUE; \ 454 } \ 455 MACRO_END 456 #endif /* PMAP_SET_CACHE_ATTR */ 457 458 #ifndef PMAP_BATCH_SET_CACHE_ATTR 459 #if defined(__arm__) || defined(__arm64__) 460 #define PMAP_BATCH_SET_CACHE_ATTR(object, user_page_list, \ 461 cache_attr, num_pages, batch_pmap_op) \ 462 MACRO_BEGIN \ 463 if ((batch_pmap_op)) { \ 464 (void)pmap_batch_set_cache_attributes( \ 465 (user_page_list), \ 466 (num_pages), \ 467 (cache_attr)); \ 468 (object)->set_cache_attr = TRUE; \ 469 } \ 470 MACRO_END 471 #else 472 #define PMAP_BATCH_SET_CACHE_ATTR(object, user_page_list, \ 473 cache_attr, num_pages, batch_pmap_op) \ 474 MACRO_BEGIN \ 475 if ((batch_pmap_op)) { \ 476 unsigned int __page_idx=0; \ 477 while (__page_idx < (num_pages)) { \ 478 pmap_set_cache_attributes( \ 479 user_page_list[__page_idx].phys_addr, \ 480 (cache_attr)); \ 481 __page_idx++; \ 482 } \ 483 (object)->set_cache_attr = TRUE; \ 484 } \ 485 MACRO_END 486 #endif 487 #endif /* PMAP_BATCH_SET_CACHE_ATTR */ 488 489 /* 490 * Routines to manage reference/modify bits based on 491 * physical addresses, simulating them if not provided 492 * by the hardware. 493 */ 494 struct pfc { 495 long pfc_cpus; 496 long pfc_invalid_global; 497 }; 498 499 typedef struct pfc pmap_flush_context; 500 501 /* Clear reference bit */ 502 extern void pmap_clear_reference(ppnum_t pn); 503 /* Return reference bit */ 504 extern boolean_t(pmap_is_referenced)(ppnum_t pn); 505 /* Set modify bit */ 506 extern void pmap_set_modify(ppnum_t pn); 507 /* Clear modify bit */ 508 extern void pmap_clear_modify(ppnum_t pn); 509 /* Return modify bit */ 510 extern boolean_t pmap_is_modified(ppnum_t pn); 511 /* Return modified and referenced bits */ 512 extern unsigned int pmap_get_refmod(ppnum_t pn); 513 /* Clear modified and referenced bits */ 514 extern void pmap_clear_refmod(ppnum_t pn, unsigned int mask); 515 #define VM_MEM_MODIFIED 0x01 /* Modified bit */ 516 #define VM_MEM_REFERENCED 0x02 /* Referenced bit */ 517 extern void pmap_clear_refmod_options(ppnum_t pn, unsigned int mask, unsigned int options, void *); 518 519 /* 520 * Clears the reference and/or modified bits on a range of virtually 521 * contiguous pages. 522 * It returns true if the operation succeeded. If it returns false, 523 * nothing has been modified. 524 * This operation is only supported on some platforms, so callers MUST 525 * handle the case where it returns false. 526 */ 527 extern bool 528 pmap_clear_refmod_range_options( 529 pmap_t pmap, 530 vm_map_address_t start, 531 vm_map_address_t end, 532 unsigned int mask, 533 unsigned int options); 534 535 536 extern void pmap_flush_context_init(pmap_flush_context *); 537 extern void pmap_flush(pmap_flush_context *); 538 539 /* 540 * Routines that operate on ranges of virtual addresses. 541 */ 542 extern void pmap_protect( /* Change protections. */ 543 pmap_t map, 544 vm_map_offset_t s, 545 vm_map_offset_t e, 546 vm_prot_t prot); 547 548 extern void pmap_protect_options( /* Change protections. */ 549 pmap_t map, 550 vm_map_offset_t s, 551 vm_map_offset_t e, 552 vm_prot_t prot, 553 unsigned int options, 554 void *arg); 555 556 extern void(pmap_pageable)( 557 pmap_t pmap, 558 vm_map_offset_t start, 559 vm_map_offset_t end, 560 boolean_t pageable); 561 562 extern uint64_t pmap_shared_region_size_min(pmap_t map); 563 564 extern kern_return_t pmap_nest(pmap_t, 565 pmap_t, 566 addr64_t, 567 uint64_t); 568 extern kern_return_t pmap_unnest(pmap_t, 569 addr64_t, 570 uint64_t); 571 572 #define PMAP_UNNEST_CLEAN 1 573 574 #if __arm64__ 575 #define PMAP_FORK_NEST 1 576 extern kern_return_t pmap_fork_nest( 577 pmap_t old_pmap, 578 pmap_t new_pmap, 579 vm_map_offset_t *nesting_start, 580 vm_map_offset_t *nesting_end); 581 #endif /* __arm64__ */ 582 583 extern kern_return_t pmap_unnest_options(pmap_t, 584 addr64_t, 585 uint64_t, 586 unsigned int); 587 extern boolean_t pmap_adjust_unnest_parameters(pmap_t, vm_map_offset_t *, vm_map_offset_t *); 588 extern void pmap_advise_pagezero_range(pmap_t, uint64_t); 589 #endif /* MACH_KERNEL_PRIVATE */ 590 591 extern boolean_t pmap_is_noencrypt(ppnum_t); 592 extern void pmap_set_noencrypt(ppnum_t pn); 593 extern void pmap_clear_noencrypt(ppnum_t pn); 594 595 /* 596 * JMM - This portion is exported to other kernel components right now, 597 * but will be pulled back in the future when the needed functionality 598 * is provided in a cleaner manner. 599 */ 600 601 extern const pmap_t kernel_pmap; /* The kernel's map */ 602 #define pmap_kernel() (kernel_pmap) 603 604 #define VM_MEM_SUPERPAGE 0x100 /* map a superpage instead of a base page */ 605 #define VM_MEM_STACK 0x200 606 607 /* N.B. These use the same numerical space as the PMAP_EXPAND_OPTIONS 608 * definitions in i386/pmap_internal.h 609 */ 610 #define PMAP_CREATE_64BIT 0x1 611 612 #if __x86_64__ 613 614 #define PMAP_CREATE_EPT 0x2 615 #define PMAP_CREATE_TEST 0x4 /* pmap will be used for testing purposes only */ 616 #define PMAP_CREATE_KNOWN_FLAGS (PMAP_CREATE_64BIT | PMAP_CREATE_EPT | PMAP_CREATE_TEST) 617 618 #else 619 620 #define PMAP_CREATE_STAGE2 0 621 #if __arm64e__ 622 #define PMAP_CREATE_DISABLE_JOP 0x4 623 #else 624 #define PMAP_CREATE_DISABLE_JOP 0 625 #endif 626 #if __ARM_MIXED_PAGE_SIZE__ 627 #define PMAP_CREATE_FORCE_4K_PAGES 0x8 628 #else 629 #define PMAP_CREATE_FORCE_4K_PAGES 0 630 #endif /* __ARM_MIXED_PAGE_SIZE__ */ 631 #define PMAP_CREATE_X86_64 0 632 #if CONFIG_ROSETTA 633 #define PMAP_CREATE_ROSETTA 0x20 634 #else 635 #define PMAP_CREATE_ROSETTA 0 636 #endif /* CONFIG_ROSETTA */ 637 638 #define PMAP_CREATE_TEST 0x40 /* pmap will be used for testing purposes only */ 639 640 /* Define PMAP_CREATE_KNOWN_FLAGS in terms of optional flags */ 641 #define PMAP_CREATE_KNOWN_FLAGS (PMAP_CREATE_64BIT | PMAP_CREATE_STAGE2 | PMAP_CREATE_DISABLE_JOP | \ 642 PMAP_CREATE_FORCE_4K_PAGES | PMAP_CREATE_X86_64 | PMAP_CREATE_ROSETTA | PMAP_CREATE_TEST) 643 644 #endif /* __x86_64__ */ 645 646 #define PMAP_OPTIONS_NOWAIT 0x1 /* don't block, return 647 * KERN_RESOURCE_SHORTAGE 648 * instead */ 649 #define PMAP_OPTIONS_NOENTER 0x2 /* expand pmap if needed 650 * but don't enter mapping 651 */ 652 #define PMAP_OPTIONS_COMPRESSOR 0x4 /* credit the compressor for 653 * this operation */ 654 #define PMAP_OPTIONS_INTERNAL 0x8 /* page from internal object */ 655 #define PMAP_OPTIONS_REUSABLE 0x10 /* page is "reusable" */ 656 #define PMAP_OPTIONS_NOFLUSH 0x20 /* delay flushing of pmap */ 657 #define PMAP_OPTIONS_NOREFMOD 0x40 /* don't need ref/mod on disconnect */ 658 #define PMAP_OPTIONS_ALT_ACCT 0x80 /* use alternate accounting scheme for page */ 659 #define PMAP_OPTIONS_REMOVE 0x100 /* removing a mapping */ 660 #define PMAP_OPTIONS_SET_REUSABLE 0x200 /* page is now "reusable" */ 661 #define PMAP_OPTIONS_CLEAR_REUSABLE 0x400 /* page no longer "reusable" */ 662 #define PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED 0x800 /* credit the compressor 663 * iff page was modified */ 664 #define PMAP_OPTIONS_PROTECT_IMMEDIATE 0x1000 /* allow protections to be 665 * be upgraded */ 666 #define PMAP_OPTIONS_CLEAR_WRITE 0x2000 667 #define PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE 0x4000 /* Honor execute for translated processes */ 668 #if defined(__arm__) || defined(__arm64__) 669 #define PMAP_OPTIONS_FF_LOCKED 0x8000 670 #define PMAP_OPTIONS_FF_WIRED 0x10000 671 #endif 672 #define PMAP_OPTIONS_XNU_USER_DEBUG 0x20000 673 674 #define PMAP_OPTIONS_MAP_TPRO 0x40000 675 676 #define PMAP_OPTIONS_RESERVED_MASK 0xFF000000 /* encoding space reserved for internal pmap use */ 677 678 #if !defined(__LP64__) 679 extern vm_offset_t pmap_extract(pmap_t pmap, 680 vm_map_offset_t va); 681 #endif 682 extern void pmap_change_wiring( /* Specify pageability */ 683 pmap_t pmap, 684 vm_map_offset_t va, 685 boolean_t wired); 686 687 /* LP64todo - switch to vm_map_offset_t when it grows */ 688 extern void pmap_remove( /* Remove mappings. */ 689 pmap_t map, 690 vm_map_offset_t s, 691 vm_map_offset_t e); 692 693 extern void pmap_remove_options( /* Remove mappings. */ 694 pmap_t map, 695 vm_map_offset_t s, 696 vm_map_offset_t e, 697 int options); 698 699 extern void fillPage(ppnum_t pa, unsigned int fill); 700 701 #if defined(__LP64__) 702 extern void pmap_pre_expand(pmap_t pmap, vm_map_offset_t vaddr); 703 extern kern_return_t pmap_pre_expand_large(pmap_t pmap, vm_map_offset_t vaddr); 704 extern vm_size_t pmap_query_pagesize(pmap_t map, vm_map_offset_t vaddr); 705 #endif 706 707 mach_vm_size_t pmap_query_resident(pmap_t pmap, 708 vm_map_offset_t s, 709 vm_map_offset_t e, 710 mach_vm_size_t *compressed_bytes_p); 711 712 extern void pmap_set_vm_map_cs_enforced(pmap_t pmap, bool new_value); 713 extern bool pmap_get_vm_map_cs_enforced(pmap_t pmap); 714 715 /* Inform the pmap layer that there is a JIT entry in this map. */ 716 extern void pmap_set_jit_entitled(pmap_t pmap); 717 718 /* Ask the pmap layer if there is a JIT entry in this map. */ 719 extern bool pmap_get_jit_entitled(pmap_t pmap); 720 721 /* Inform the pmap layer that the XO register is repurposed for this map */ 722 extern void pmap_set_tpro(pmap_t pmap); 723 724 /* Ask the pmap layer if there is a TPRO entry in this map. */ 725 extern bool pmap_get_tpro(pmap_t pmap); 726 727 /* 728 * Tell the pmap layer what range within the nested region the VM intends to 729 * use. 730 */ 731 extern void pmap_trim(pmap_t grand, pmap_t subord, addr64_t vstart, uint64_t size); 732 733 extern bool pmap_is_nested(pmap_t pmap); 734 735 /* 736 * Dump page table contents into the specified buffer. Returns KERN_INSUFFICIENT_BUFFER_SIZE 737 * if insufficient space, KERN_NOT_SUPPORTED if unsupported in the current configuration. 738 * This is expected to only be called from kernel debugger context, 739 * so synchronization is not required. 740 */ 741 742 extern kern_return_t pmap_dump_page_tables(pmap_t pmap, void *bufp, void *buf_end, unsigned int level_mask, size_t *bytes_copied); 743 744 /* Asks the pmap layer for number of bits used for VA address. */ 745 extern uint32_t pmap_user_va_bits(pmap_t pmap); 746 extern uint32_t pmap_kernel_va_bits(void); 747 748 /* 749 * Indicates if any special policy is applied to this protection by the pmap 750 * layer. 751 */ 752 bool pmap_has_prot_policy(pmap_t pmap, bool translated_allow_execute, vm_prot_t prot); 753 754 /* 755 * Causes the pmap to return any available pages that it can return cheaply to 756 * the VM. 757 */ 758 uint64_t pmap_release_pages_fast(void); 759 760 #define PMAP_QUERY_PAGE_PRESENT 0x01 761 #define PMAP_QUERY_PAGE_REUSABLE 0x02 762 #define PMAP_QUERY_PAGE_INTERNAL 0x04 763 #define PMAP_QUERY_PAGE_ALTACCT 0x08 764 #define PMAP_QUERY_PAGE_COMPRESSED 0x10 765 #define PMAP_QUERY_PAGE_COMPRESSED_ALTACCT 0x20 766 extern kern_return_t pmap_query_page_info( 767 pmap_t pmap, 768 vm_map_offset_t va, 769 int *disp); 770 771 extern bool pmap_in_ppl(void); 772 773 extern uint32_t pmap_lookup_in_static_trust_cache(const uint8_t cdhash[CS_CDHASH_LEN]); 774 extern bool pmap_lookup_in_loaded_trust_caches(const uint8_t cdhash[CS_CDHASH_LEN]); 775 776 /** 777 * Indicates whether the device supports register-level MMIO access control. 778 * 779 * @note Unlike the pmap-io-ranges mechanism, which enforces PPL-only register 780 * writability at page granularity, this mechanism allows specific registers 781 * on a read-mostly page to be written using a dedicated guarded mode trap 782 * without requiring a full PPL driver extension. 783 * 784 * @return True if the device supports register-level MMIO access control. 785 */ 786 extern bool pmap_has_iofilter_protected_write(void); 787 788 /** 789 * Performs a write to the I/O register specified by addr on supported devices. 790 * 791 * @note On supported devices (determined by pmap_has_iofilter_protected_write()), this 792 * function goes over the sorted I/O filter entry table. If there is a hit, the 793 * write is performed from Guarded Mode. Otherwise, the write is performed from 794 * Normal Mode (kernel mode). Note that you can still hit an exception if the 795 * register is owned by PPL but not allowed by an io-filter-entry in the device tree. 796 * 797 * @note On unsupported devices, this function will panic. 798 * 799 * @param addr The address of the register. 800 * @param value The value to be written. 801 * @param width The width of the I/O register, supported values are 1, 2, 4 and 8. 802 */ 803 extern void pmap_iofilter_protected_write(vm_address_t addr, uint64_t value, uint64_t width); 804 805 extern void *pmap_claim_reserved_ppl_page(void); 806 extern void pmap_free_reserved_ppl_page(void *kva); 807 808 extern void pmap_ledger_verify_size(size_t); 809 extern ledger_t pmap_ledger_alloc(void); 810 extern void pmap_ledger_free(ledger_t); 811 812 extern bool pmap_is_bad_ram(ppnum_t ppn); 813 814 #if __arm64__ 815 extern bool pmap_is_exotic(pmap_t pmap); 816 #else /* __arm64__ */ 817 #define pmap_is_exotic(pmap) false 818 #endif /* __arm64__ */ 819 820 821 /* 822 * Returns a subset of pmap_cs non-default configuration, 823 * e.g. loosening up of some restrictions through pmap_cs or amfi 824 * boot-args. The return value is a bit field with possible bits 825 * described below. If default, the function will return 0. Note that 826 * this does not work the other way: 0 does not imply that pmap_cs 827 * runs in default configuration, and only a small configuration 828 * subset is returned by this function. 829 * 830 * Never assume the system is "secure" if this returns 0. 831 */ 832 extern int pmap_cs_configuration(void); 833 834 #if XNU_KERNEL_PRIVATE 835 836 #if defined(__arm64__) 837 838 /** 839 * Check if a particular pmap is used for stage2 translations or not. 840 */ 841 extern bool 842 pmap_performs_stage2_translations(const pmap_t pmap); 843 844 #endif /* defined(__arm64__) */ 845 #endif /* XNU_KERNEL_PRIVATE */ 846 847 848 849 #endif /* KERNEL_PRIVATE */ 850 851 #endif /* _VM_PMAP_H_ */ 852