xref: /xnu-11417.101.15/osfmk/vm/pmap.h (revision e3723e1f17661b24996789d8afc084c0c3303b26) !
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 
104 /* bcopy_phys and bzero_phys flags. */
105 #define cppvPsnk                0x000000001     /* Destination is a physical address */
106 #define cppvPsnkb               31
107 #define cppvPsrc                0x000000002     /* Source is a physical address */
108 #define cppvPsrcb               30
109 #define cppvFsnk                0x000000004     /* Destination requires flushing (only on non-coherent I/O) */
110 #define cppvFsnkb               29
111 #define cppvFsrc                0x000000008     /* Source requires flushing (only on non-coherent I/O) */
112 #define cppvFsrcb               28
113 #define cppvNoModSnk            0x000000010     /* Ignored in bcopy_phys() */
114 #define cppvNoModSnkb           27
115 #define cppvNoRefSrc            0x000000020     /* Ignored in bcopy_phys() */
116 #define cppvNoRefSrcb           26
117 #define cppvKmap                0x000000040     /* Use the kernel's vm_map */
118 #define cppvKmapb               25
119 
120 extern boolean_t pmap_has_managed_page(ppnum_t first, ppnum_t last);
121 
122 
123 #if MACH_KERNEL_PRIVATE || BSD_KERNEL_PRIVATE
124 #include <mach/mach_types.h>
125 #include <vm/memory_types.h>
126 
127 /*
128  * Routines used during BSD process creation.
129  */
130 
131 extern pmap_t           pmap_create_options(    /* Create a pmap_t. */
132 	ledger_t        ledger,
133 	vm_map_size_t   size,
134 	unsigned int    flags);
135 
136 #if __has_feature(ptrauth_calls) && (defined(XNU_TARGET_OS_OSX) || (DEVELOPMENT || DEBUG))
137 /**
138  * Informs the pmap layer that a process will be running with user JOP disabled,
139  * as if PMAP_CREATE_DISABLE_JOP had been passed during pmap creation.
140  *
141  * @note This function cannot be used once the target process has started
142  * executing code.  It is intended for cases where user JOP is disabled based on
143  * the code signature (e.g., special "keys-off" entitlements), which is too late
144  * to change the flags passed to pmap_create_options.
145  *
146  * @param pmap	The pmap belonging to the target process
147  */
148 extern void             pmap_disable_user_jop(
149 	pmap_t          pmap);
150 #endif /* __has_feature(ptrauth_calls) && (defined(XNU_TARGET_OS_OSX) || (DEVELOPMENT || DEBUG)) */
151 #endif /* MACH_KERNEL_PRIVATE || BSD_KERNEL_PRIVATE */
152 
153 #ifdef  MACH_KERNEL_PRIVATE
154 
155 #include <mach_assert.h>
156 
157 #include <machine/pmap.h>
158 /*
159  *	Routines used for initialization.
160  *	There is traditionally also a pmap_bootstrap,
161  *	used very early by machine-dependent code,
162  *	but it is not part of the interface.
163  *
164  *	LP64todo -
165  *	These interfaces are tied to the size of the
166  *	kernel pmap - and therefore use the "local"
167  *	vm_offset_t, etc... types.
168  */
169 
170 extern void *pmap_steal_memory(vm_size_t size, vm_size_t alignment); /* Early memory allocation */
171 extern void *pmap_steal_freeable_memory(vm_size_t size); /* Early memory allocation */
172 
173 extern uint_t pmap_free_pages(void); /* report remaining unused physical pages */
174 #if defined(__arm__) || defined(__arm64__)
175 extern ppnum_t pmap_first_pnum;           /* the first valid physical page on the system == atop(gDramBase) */
176 extern uint_t pmap_free_pages_span(void); /* report phys address range of unused physical pages */
177 #endif /* defined(__arm__) || defined(__arm64__) */
178 
179 extern void pmap_startup(vm_offset_t *startp, vm_offset_t *endp); /* allocate vm_page structs */
180 
181 extern void pmap_init(void); /* Initialization, once we have kernel virtual memory.  */
182 
183 extern void mapping_adjust(void); /* Adjust free mapping count */
184 
185 extern void mapping_free_prime(void); /* Primes the mapping block release list */
186 
187 #ifndef MACHINE_PAGES
188 /*
189  *	If machine/pmap.h defines MACHINE_PAGES, it must implement
190  *	the above functions.  The pmap module has complete control.
191  *	Otherwise, it must implement the following functions:
192  *		pmap_free_pages
193  *		pmap_virtual_space
194  *		pmap_next_page
195  *		pmap_init
196  *	and vm/vm_resident.c implements pmap_steal_memory and pmap_startup
197  *	using pmap_free_pages, pmap_next_page, pmap_virtual_space,
198  *	and pmap_enter.  pmap_free_pages may over-estimate the number
199  *	of unused physical pages, and pmap_next_page may return FALSE
200  *	to indicate that there are no more unused pages to return.
201  *	However, for best performance pmap_free_pages should be accurate.
202  */
203 
204 /*
205  * Routines to return the next unused physical page.
206  */
207 extern boolean_t pmap_next_page(ppnum_t *pnum);
208 extern boolean_t pmap_next_page_hi(ppnum_t *pnum, boolean_t might_free);
209 #ifdef __x86_64__
210 extern kern_return_t pmap_next_page_large(ppnum_t *pnum);
211 extern void pmap_hi_pages_done(void);
212 #endif
213 
214 #if CONFIG_SPTM
215 __enum_decl(pmap_mapping_type_t, uint8_t, {
216 	PMAP_MAPPING_TYPE_INFER = SPTM_UNTYPED,
217 	PMAP_MAPPING_TYPE_DEFAULT = XNU_DEFAULT,
218 	PMAP_MAPPING_TYPE_ROZONE = XNU_ROZONE,
219 	PMAP_MAPPING_TYPE_RESTRICTED = XNU_KERNEL_RESTRICTED
220 });
221 #else
222 __enum_decl(pmap_mapping_type_t, uint8_t, {
223 	PMAP_MAPPING_TYPE_INFER = 0,
224 	PMAP_MAPPING_TYPE_DEFAULT,
225 	PMAP_MAPPING_TYPE_ROZONE,
226 	PMAP_MAPPING_TYPE_RESTRICTED
227 });
228 #endif
229 
230 /*
231  * Report virtual space available for the kernel.
232  */
233 extern void pmap_virtual_space(
234 	vm_offset_t     *virtual_start,
235 	vm_offset_t     *virtual_end);
236 #endif  /* MACHINE_PAGES */
237 
238 /*
239  * Routines to manage the physical map data structure.
240  */
241 extern pmap_t(pmap_kernel)(void);               /* Return the kernel's pmap */
242 extern void             pmap_reference(pmap_t pmap);    /* Gain a reference. */
243 extern void             pmap_destroy(pmap_t pmap); /* Release a reference. */
244 extern void             pmap_switch(pmap_t pmap, thread_t thread);
245 extern void             pmap_require(pmap_t pmap);
246 
247 #if MACH_ASSERT
248 extern void pmap_set_process(pmap_t pmap,
249     int pid,
250     char *procname);
251 #endif /* MACH_ASSERT */
252 
253 extern kern_return_t    pmap_enter(     /* Enter a mapping */
254 	pmap_t          pmap,
255 	vm_map_offset_t v,
256 	ppnum_t         pn,
257 	vm_prot_t       prot,
258 	vm_prot_t       fault_type,
259 	unsigned int    flags,
260 	boolean_t       wired,
261 	pmap_mapping_type_t mapping_type);
262 
263 extern kern_return_t    pmap_enter_options(
264 	pmap_t pmap,
265 	vm_map_offset_t v,
266 	ppnum_t pn,
267 	vm_prot_t prot,
268 	vm_prot_t fault_type,
269 	unsigned int flags,
270 	boolean_t wired,
271 	unsigned int options,
272 	void *arg,
273 	pmap_mapping_type_t mapping_type);
274 extern kern_return_t    pmap_enter_options_addr(
275 	pmap_t pmap,
276 	vm_map_offset_t v,
277 	pmap_paddr_t pa,
278 	vm_prot_t prot,
279 	vm_prot_t fault_type,
280 	unsigned int flags,
281 	boolean_t wired,
282 	unsigned int options,
283 	void *arg,
284 	pmap_mapping_type_t mapping_type);
285 
286 extern void             pmap_remove_some_phys(
287 	pmap_t          pmap,
288 	ppnum_t         pn);
289 
290 extern void             pmap_lock_phys_page(
291 	ppnum_t         pn);
292 
293 extern void             pmap_unlock_phys_page(
294 	ppnum_t         pn);
295 
296 
297 /*
298  *	Routines that operate on physical addresses.
299  */
300 
301 extern void             pmap_page_protect(      /* Restrict access to page. */
302 	ppnum_t phys,
303 	vm_prot_t       prot);
304 
305 extern void             pmap_page_protect_options(      /* Restrict access to page. */
306 	ppnum_t phys,
307 	vm_prot_t       prot,
308 	unsigned int    options,
309 	void            *arg);
310 
311 extern void(pmap_zero_page)(
312 	ppnum_t         pn);
313 
314 extern void(pmap_zero_page_with_options)(
315 	ppnum_t         pn,
316 	int             options);
317 
318 extern void(pmap_zero_part_page)(
319 	ppnum_t         pn,
320 	vm_offset_t     offset,
321 	vm_size_t       len);
322 
323 extern void(pmap_copy_page)(
324 	ppnum_t         src,
325 	ppnum_t         dest,
326 	int             options);
327 
328 extern void(pmap_copy_part_page)(
329 	ppnum_t         src,
330 	vm_offset_t     src_offset,
331 	ppnum_t         dst,
332 	vm_offset_t     dst_offset,
333 	vm_size_t       len);
334 
335 extern void(pmap_copy_part_lpage)(
336 	vm_offset_t     src,
337 	ppnum_t         dst,
338 	vm_offset_t     dst_offset,
339 	vm_size_t       len);
340 
341 extern void(pmap_copy_part_rpage)(
342 	ppnum_t         src,
343 	vm_offset_t     src_offset,
344 	vm_offset_t     dst,
345 	vm_size_t       len);
346 
347 extern unsigned int(pmap_disconnect)(   /* disconnect mappings and return reference and change */
348 	ppnum_t         phys);
349 
350 extern unsigned int(pmap_disconnect_options)(   /* disconnect mappings and return reference and change */
351 	ppnum_t         phys,
352 	unsigned int    options,
353 	void            *arg);
354 
355 extern kern_return_t(pmap_attribute_cache_sync)(      /* Flush appropriate
356                                                        * cache based on
357                                                        * page number sent */
358 	ppnum_t         pn,
359 	vm_size_t       size,
360 	vm_machine_attribute_t attribute,
361 	vm_machine_attribute_val_t* value);
362 
363 extern unsigned int(pmap_cache_attributes)(
364 	ppnum_t         pn);
365 
366 /*
367  * Set (override) cache attributes for the specified physical page
368  */
369 extern  void            pmap_set_cache_attributes(
370 	ppnum_t,
371 	unsigned int);
372 
373 extern void            *pmap_map_compressor_page(
374 	ppnum_t);
375 
376 extern void             pmap_unmap_compressor_page(
377 	ppnum_t,
378 	void*);
379 
380 /**
381  * The following declarations are meant to provide a uniform interface by which the VM layer can
382  * pass batches of pages to the pmap layer directly, in the various page list formats natively
383  * used by the VM.  If a new type of list is to be added, the various structures and iterator
384  * functions below should be updated to understand it, and then it should "just work" with the
385  * pmap layer.
386  */
387 
388 /* The various supported page list types. */
389 __enum_decl(unified_page_list_type_t, uint8_t, {
390 	/* Universal page list array, essentially an array of ppnum_t. */
391 	UNIFIED_PAGE_LIST_TYPE_UPL_ARRAY,
392 	/**
393 	 * Singly-linked list of vm_page_t, using vmp_snext field.
394 	 * This is typically used to construct local lists of pages to be freed.
395 	 */
396 	UNIFIED_PAGE_LIST_TYPE_VM_PAGE_LIST,
397 	/* Doubly-linked queue of vm_page_t's associated with a VM object, using vmp_listq field. */
398 	UNIFIED_PAGE_LIST_TYPE_VM_PAGE_OBJ_Q,
399 	/* Doubly-linked queue of vm_page_t's in a FIFO queue or global free list, using vmp_pageq field. */
400 	UNIFIED_PAGE_LIST_TYPE_VM_PAGE_FIFO_Q,
401 });
402 
403 /* Uniform data structure encompassing the various page list types handled by the VM layer. */
404 typedef struct {
405 	union {
406 		/* Base address and size (in pages) of UPL array for type UNIFIED_PAGE_LIST_TYPE_UPL_ARRAY */
407 		struct {
408 			upl_page_info_array_t upl_info;
409 			unsigned int upl_size;
410 		} upl;
411 		/* Head of singly-linked vm_page_t list for UNIFIED_PAGE_LIST_TYPE_VM_PAGE_LIST */
412 		vm_page_t page_slist;
413 		/* Head of queue for UNIFIED_PAGE_LIST_TYPE_VM_PAGE_OBJ_Q and UNIFIED_PAGE_LIST_TYPE_VM_PAGE_FIFO_Q */
414 		void *pageq; /* vm_page_queue_head_t* */
415 	};
416 	unified_page_list_type_t type;
417 } unified_page_list_t;
418 
419 /* Uniform data structure representing an iterator position within a unified_page_list_t object. */
420 typedef struct {
421 	/* Pointer to list structure from which this iterator was created. */
422 	const unified_page_list_t *list;
423 	union {
424 		/* Position within UPL array, for UNIFIED_PAGE_LIST_TYPE_UPL_ARRAY */
425 		unsigned int upl_index;
426 		/* Position within page list or page queue, for all other types */
427 		vm_page_t pageq_pos;
428 	};
429 } unified_page_list_iterator_t;
430 
431 extern void unified_page_list_iterator_init(
432 	const unified_page_list_t *page_list,
433 	unified_page_list_iterator_t *iter);
434 
435 extern void unified_page_list_iterator_next(unified_page_list_iterator_t *iter);
436 
437 extern bool unified_page_list_iterator_end(const unified_page_list_iterator_t *iter);
438 
439 extern ppnum_t unified_page_list_iterator_page(
440 	const unified_page_list_iterator_t *iter,
441 	bool *is_fictitious);
442 
443 extern void pmap_batch_set_cache_attributes(
444 	const unified_page_list_t *,
445 	unsigned int);
446 extern void pmap_sync_page_data_phys(ppnum_t pa);
447 extern void pmap_sync_page_attributes_phys(ppnum_t pa);
448 
449 
450 /*
451  * debug/assertions. pmap_verify_free returns true iff
452  * the given physical page is mapped into no pmap.
453  * pmap_assert_free() will panic() if pn is not free.
454  */
455 extern bool pmap_verify_free(ppnum_t pn);
456 #if MACH_ASSERT
457 extern void pmap_assert_free(ppnum_t pn);
458 #endif
459 
460 
461 /*
462  *	Sundry required (internal) routines
463  */
464 #ifdef CURRENTLY_UNUSED_AND_UNTESTED
465 extern void             pmap_collect(pmap_t pmap);/* Perform garbage
466                                                    * collection, if any */
467 #endif
468 /*
469  *	Optional routines
470  */
471 extern void(pmap_copy)(                         /* Copy range of mappings,
472                                                  * if desired. */
473 	pmap_t          dest,
474 	pmap_t          source,
475 	vm_map_offset_t dest_va,
476 	vm_map_size_t   size,
477 	vm_map_offset_t source_va);
478 
479 extern kern_return_t(pmap_attribute)(           /* Get/Set special memory
480                                                  * attributes */
481 	pmap_t          pmap,
482 	vm_map_offset_t va,
483 	vm_map_size_t   size,
484 	vm_machine_attribute_t  attribute,
485 	vm_machine_attribute_val_t* value);
486 
487 /*
488  * Routines defined as macros.
489  */
490 #ifndef PMAP_ACTIVATE_USER
491 #ifndef PMAP_ACTIVATE
492 #define PMAP_ACTIVATE_USER(thr, cpu)
493 #else   /* PMAP_ACTIVATE */
494 #define PMAP_ACTIVATE_USER(thr, cpu) {                  \
495 	pmap_t  pmap;                                           \
496                                                                 \
497 	pmap = (thr)->map->pmap;                                \
498 	if (pmap != pmap_kernel())                              \
499 	        PMAP_ACTIVATE(pmap, (thr), (cpu));              \
500 }
501 #endif  /* PMAP_ACTIVATE */
502 #endif  /* PMAP_ACTIVATE_USER */
503 
504 #ifndef PMAP_DEACTIVATE_USER
505 #ifndef PMAP_DEACTIVATE
506 #define PMAP_DEACTIVATE_USER(thr, cpu)
507 #else   /* PMAP_DEACTIVATE */
508 #define PMAP_DEACTIVATE_USER(thr, cpu) {                        \
509 	pmap_t  pmap;                                           \
510                                                                 \
511 	pmap = (thr)->map->pmap;                                \
512 	if ((pmap) != pmap_kernel())                    \
513 	        PMAP_DEACTIVATE(pmap, (thr), (cpu));    \
514 }
515 #endif  /* PMAP_DEACTIVATE */
516 #endif  /* PMAP_DEACTIVATE_USER */
517 
518 #ifndef PMAP_ACTIVATE_KERNEL
519 #ifndef PMAP_ACTIVATE
520 #define PMAP_ACTIVATE_KERNEL(cpu)
521 #else   /* PMAP_ACTIVATE */
522 #define PMAP_ACTIVATE_KERNEL(cpu)                       \
523 	        PMAP_ACTIVATE(pmap_kernel(), THREAD_NULL, cpu)
524 #endif  /* PMAP_ACTIVATE */
525 #endif  /* PMAP_ACTIVATE_KERNEL */
526 
527 #ifndef PMAP_DEACTIVATE_KERNEL
528 #ifndef PMAP_DEACTIVATE
529 #define PMAP_DEACTIVATE_KERNEL(cpu)
530 #else   /* PMAP_DEACTIVATE */
531 #define PMAP_DEACTIVATE_KERNEL(cpu)                     \
532 	        PMAP_DEACTIVATE(pmap_kernel(), THREAD_NULL, cpu)
533 #endif  /* PMAP_DEACTIVATE */
534 #endif  /* PMAP_DEACTIVATE_KERNEL */
535 
536 #ifndef PMAP_SET_CACHE_ATTR
537 #define PMAP_SET_CACHE_ATTR(mem, object, cache_attr, batch_pmap_op)             \
538 	MACRO_BEGIN                                                             \
539 	        if (!batch_pmap_op) {                                           \
540 	                pmap_set_cache_attributes(VM_PAGE_GET_PHYS_PAGE(mem), cache_attr); \
541 	                (object)->set_cache_attr = TRUE;                        \
542 	        }                                                               \
543 	MACRO_END
544 #endif  /* PMAP_SET_CACHE_ATTR */
545 
546 #ifndef PMAP_BATCH_SET_CACHE_ATTR
547 #define PMAP_BATCH_SET_CACHE_ATTR(object, user_page_list,                   \
548 	    cache_attr, num_pages, batch_pmap_op)                               \
549 	MACRO_BEGIN                                                             \
550 	        if ((batch_pmap_op)) {                                          \
551 	                const unified_page_list_t __pmap_batch_list = {         \
552 	                        .upl = {.upl_info = (user_page_list),           \
553 	                                .upl_size = (num_pages),},              \
554 	                        .type = UNIFIED_PAGE_LIST_TYPE_UPL_ARRAY,       \
555 	                };                                                      \
556 	                pmap_batch_set_cache_attributes(                        \
557 	                                &__pmap_batch_list,                     \
558 	                                (cache_attr));                          \
559 	                (object)->set_cache_attr = TRUE;                        \
560 	        }                                                               \
561 	MACRO_END
562 #endif  /* PMAP_BATCH_SET_CACHE_ATTR */
563 
564 /*
565  *	Routines to manage reference/modify bits based on
566  *	physical addresses, simulating them if not provided
567  *	by the hardware.
568  */
569 struct pfc {
570 	long    pfc_cpus;
571 	long    pfc_invalid_global;
572 };
573 
574 typedef struct pfc      pmap_flush_context;
575 
576 /* Clear reference bit */
577 extern void             pmap_clear_reference(ppnum_t     pn);
578 /* Return reference bit */
579 extern boolean_t(pmap_is_referenced)(ppnum_t     pn);
580 /* Set modify bit */
581 extern void             pmap_set_modify(ppnum_t  pn);
582 /* Clear modify bit */
583 extern void             pmap_clear_modify(ppnum_t pn);
584 /* Return modify bit */
585 extern boolean_t        pmap_is_modified(ppnum_t pn);
586 /* Return modified and referenced bits */
587 extern unsigned int pmap_get_refmod(ppnum_t pn);
588 /* Clear modified and referenced bits */
589 extern void                     pmap_clear_refmod(ppnum_t pn, unsigned int mask);
590 #define VM_MEM_MODIFIED         0x01    /* Modified bit */
591 #define VM_MEM_REFERENCED       0x02    /* Referenced bit */
592 extern void                     pmap_clear_refmod_options(ppnum_t pn, unsigned int mask, unsigned int options, void *);
593 
594 /*
595  * Clears the reference and/or modified bits on a range of virtually
596  * contiguous pages.
597  * It returns true if the operation succeeded. If it returns false,
598  * nothing has been modified.
599  * This operation is only supported on some platforms, so callers MUST
600  * handle the case where it returns false.
601  */
602 extern bool
603 pmap_clear_refmod_range_options(
604 	pmap_t pmap,
605 	vm_map_address_t start,
606 	vm_map_address_t end,
607 	unsigned int mask,
608 	unsigned int options);
609 
610 
611 extern void pmap_flush_context_init(pmap_flush_context *);
612 extern void pmap_flush(pmap_flush_context *);
613 
614 /*
615  *	Routines that operate on ranges of virtual addresses.
616  */
617 extern void             pmap_protect(   /* Change protections. */
618 	pmap_t          map,
619 	vm_map_offset_t s,
620 	vm_map_offset_t e,
621 	vm_prot_t       prot);
622 
623 extern void             pmap_protect_options(   /* Change protections. */
624 	pmap_t          map,
625 	vm_map_offset_t s,
626 	vm_map_offset_t e,
627 	vm_prot_t       prot,
628 	unsigned int    options,
629 	void            *arg);
630 
631 extern void(pmap_pageable)(
632 	pmap_t          pmap,
633 	vm_map_offset_t start,
634 	vm_map_offset_t end,
635 	boolean_t       pageable);
636 
637 extern uint64_t pmap_shared_region_size_min(pmap_t map);
638 
639 extern kern_return_t pmap_nest(pmap_t,
640     pmap_t,
641     addr64_t,
642     uint64_t);
643 extern kern_return_t pmap_unnest(pmap_t,
644     addr64_t,
645     uint64_t);
646 
647 #define PMAP_UNNEST_CLEAN       1
648 
649 #if __arm64__
650 #if CONFIG_SPTM
651 #define PMAP_FORK_NEST 1
652 #endif /* CONFIG_SPTM */
653 
654 #if PMAP_FORK_NEST
655 extern kern_return_t pmap_fork_nest(
656 	pmap_t old_pmap,
657 	pmap_t new_pmap,
658 	vm_map_offset_t *nesting_start,
659 	vm_map_offset_t *nesting_end);
660 #endif /* PMAP_FORK_NEST */
661 #endif /* __arm64__ */
662 
663 extern kern_return_t pmap_unnest_options(pmap_t,
664     addr64_t,
665     uint64_t,
666     unsigned int);
667 extern boolean_t pmap_adjust_unnest_parameters(pmap_t, vm_map_offset_t *, vm_map_offset_t *);
668 extern void             pmap_advise_pagezero_range(pmap_t, uint64_t);
669 #endif  /* MACH_KERNEL_PRIVATE */
670 
671 extern boolean_t        pmap_is_noencrypt(ppnum_t);
672 extern void             pmap_set_noencrypt(ppnum_t pn);
673 extern void             pmap_clear_noencrypt(ppnum_t pn);
674 
675 /*
676  * JMM - This portion is exported to other kernel components right now,
677  * but will be pulled back in the future when the needed functionality
678  * is provided in a cleaner manner.
679  */
680 
681 extern const pmap_t     kernel_pmap;            /* The kernel's map */
682 #define pmap_kernel()   (kernel_pmap)
683 
684 #define VM_MEM_SUPERPAGE        0x100           /* map a superpage instead of a base page */
685 #define VM_MEM_STACK            0x200
686 
687 /* N.B. These use the same numerical space as the PMAP_EXPAND_OPTIONS
688  * definitions in i386/pmap_internal.h
689  */
690 #define PMAP_CREATE_64BIT          0x1
691 
692 #if __x86_64__
693 
694 #define PMAP_CREATE_EPT            0x2
695 #define PMAP_CREATE_TEST           0x4 /* pmap will be used for testing purposes only */
696 #define PMAP_CREATE_KNOWN_FLAGS (PMAP_CREATE_64BIT | PMAP_CREATE_EPT | PMAP_CREATE_TEST)
697 
698 #else
699 
700 #define PMAP_CREATE_STAGE2         0
701 #if __arm64e__
702 #define PMAP_CREATE_DISABLE_JOP    0x4
703 #else
704 #define PMAP_CREATE_DISABLE_JOP    0
705 #endif
706 #if __ARM_MIXED_PAGE_SIZE__
707 #define PMAP_CREATE_FORCE_4K_PAGES 0x8
708 #else
709 #define PMAP_CREATE_FORCE_4K_PAGES 0
710 #endif /* __ARM_MIXED_PAGE_SIZE__ */
711 #define PMAP_CREATE_X86_64         0
712 #if CONFIG_ROSETTA
713 #define PMAP_CREATE_ROSETTA        0x20
714 #else
715 #define PMAP_CREATE_ROSETTA        0
716 #endif /* CONFIG_ROSETTA */
717 
718 #define PMAP_CREATE_TEST           0x40 /* pmap will be used for testing purposes only */
719 
720 /* Define PMAP_CREATE_KNOWN_FLAGS in terms of optional flags */
721 #define PMAP_CREATE_KNOWN_FLAGS (PMAP_CREATE_64BIT | PMAP_CREATE_STAGE2 | PMAP_CREATE_DISABLE_JOP | \
722     PMAP_CREATE_FORCE_4K_PAGES | PMAP_CREATE_X86_64 | PMAP_CREATE_ROSETTA | PMAP_CREATE_TEST)
723 
724 #endif /* __x86_64__ */
725 
726 #define PMAP_OPTIONS_NOWAIT     0x1             /* don't block, return
727 	                                         * KERN_RESOURCE_SHORTAGE
728 	                                         * instead */
729 #define PMAP_OPTIONS_NOENTER    0x2             /* expand pmap if needed
730 	                                         * but don't enter mapping
731 	                                         */
732 #define PMAP_OPTIONS_COMPRESSOR 0x4             /* credit the compressor for
733 	                                         * this operation */
734 #define PMAP_OPTIONS_INTERNAL   0x8             /* page from internal object */
735 #define PMAP_OPTIONS_REUSABLE   0x10            /* page is "reusable" */
736 #define PMAP_OPTIONS_NOFLUSH    0x20            /* delay flushing of pmap */
737 #define PMAP_OPTIONS_NOREFMOD   0x40            /* don't need ref/mod on disconnect */
738 #define PMAP_OPTIONS_ALT_ACCT   0x80            /* use alternate accounting scheme for page */
739 #define PMAP_OPTIONS_REMOVE     0x100           /* removing a mapping */
740 #define PMAP_OPTIONS_SET_REUSABLE   0x200       /* page is now "reusable" */
741 #define PMAP_OPTIONS_CLEAR_REUSABLE 0x400       /* page no longer "reusable" */
742 #define PMAP_OPTIONS_COMPRESSOR_IFF_MODIFIED 0x800 /* credit the compressor
743 	                                            * iff page was modified */
744 #define PMAP_OPTIONS_PROTECT_IMMEDIATE 0x1000   /* allow protections to be
745 	                                         * be upgraded */
746 #define PMAP_OPTIONS_CLEAR_WRITE 0x2000
747 #define PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE 0x4000 /* Honor execute for translated processes */
748 #if defined(__arm__) || defined(__arm64__)
749 #define PMAP_OPTIONS_FF_LOCKED  0x8000
750 #define PMAP_OPTIONS_FF_WIRED   0x10000
751 #endif
752 #define PMAP_OPTIONS_XNU_USER_DEBUG 0x20000
753 
754 /* Indicates that pmap_enter() or pmap_remove() is being called with preemption already disabled. */
755 #define PMAP_OPTIONS_NOPREEMPT  0x80000
756 
757 #define PMAP_OPTIONS_MAP_TPRO 0x40000
758 
759 #define PMAP_OPTIONS_RESERVED_MASK 0xFF000000   /* encoding space reserved for internal pmap use */
760 
761 #if     !defined(__LP64__)
762 extern vm_offset_t      pmap_extract(pmap_t pmap,
763     vm_map_offset_t va);
764 #endif
765 extern void             pmap_change_wiring(     /* Specify pageability */
766 	pmap_t          pmap,
767 	vm_map_offset_t va,
768 	boolean_t       wired);
769 
770 /* LP64todo - switch to vm_map_offset_t when it grows */
771 extern void             pmap_remove(    /* Remove mappings. */
772 	pmap_t          map,
773 	vm_map_offset_t s,
774 	vm_map_offset_t e);
775 
776 extern void             pmap_remove_options(    /* Remove mappings. */
777 	pmap_t          map,
778 	vm_map_offset_t s,
779 	vm_map_offset_t e,
780 	int             options);
781 
782 extern void             fillPage(ppnum_t pa, unsigned int fill);
783 
784 #if defined(__LP64__)
785 extern void pmap_pre_expand(pmap_t pmap, vm_map_offset_t vaddr);
786 extern kern_return_t pmap_pre_expand_large(pmap_t pmap, vm_map_offset_t vaddr);
787 extern vm_size_t pmap_query_pagesize(pmap_t map, vm_map_offset_t vaddr);
788 #endif
789 
790 mach_vm_size_t pmap_query_resident(pmap_t pmap,
791     vm_map_offset_t s,
792     vm_map_offset_t e,
793     mach_vm_size_t *compressed_bytes_p);
794 
795 extern void pmap_set_vm_map_cs_enforced(pmap_t pmap, bool new_value);
796 extern bool pmap_get_vm_map_cs_enforced(pmap_t pmap);
797 
798 /* Inform the pmap layer that there is a JIT entry in this map. */
799 extern void pmap_set_jit_entitled(pmap_t pmap);
800 
801 /* Ask the pmap layer if there is a JIT entry in this map. */
802 extern bool pmap_get_jit_entitled(pmap_t pmap);
803 
804 /* Inform the pmap layer that the XO register is repurposed for this map */
805 extern void pmap_set_tpro(pmap_t pmap);
806 
807 
808 /* Ask the pmap layer if there is a TPRO entry in this map. */
809 extern bool pmap_get_tpro(pmap_t pmap);
810 
811 /*
812  * Tell the pmap layer what range within the nested region the VM intends to
813  * use.
814  */
815 extern void pmap_trim(pmap_t grand, pmap_t subord, addr64_t vstart, uint64_t size);
816 
817 extern bool pmap_is_nested(pmap_t pmap);
818 
819 /*
820  * Dump page table contents into the specified buffer.  Returns KERN_INSUFFICIENT_BUFFER_SIZE
821  * if insufficient space, KERN_NOT_SUPPORTED if unsupported in the current configuration.
822  * This is expected to only be called from kernel debugger context,
823  * so synchronization is not required.
824  */
825 
826 extern kern_return_t pmap_dump_page_tables(pmap_t pmap, void *bufp, void *buf_end, unsigned int level_mask, size_t *bytes_copied);
827 
828 /* Asks the pmap layer for number of bits used for VA address. */
829 extern uint32_t pmap_user_va_bits(pmap_t pmap);
830 extern uint32_t pmap_kernel_va_bits(void);
831 
832 /*
833  * Indicates if any special policy is applied to this protection by the pmap
834  * layer.
835  */
836 bool pmap_has_prot_policy(pmap_t pmap, bool translated_allow_execute, vm_prot_t prot);
837 
838 /*
839  * Causes the pmap to return any available pages that it can return cheaply to
840  * the VM.
841  */
842 uint64_t pmap_release_pages_fast(void);
843 
844 #define PMAP_QUERY_PAGE_PRESENT                 0x01
845 #define PMAP_QUERY_PAGE_REUSABLE                0x02
846 #define PMAP_QUERY_PAGE_INTERNAL                0x04
847 #define PMAP_QUERY_PAGE_ALTACCT                 0x08
848 #define PMAP_QUERY_PAGE_COMPRESSED              0x10
849 #define PMAP_QUERY_PAGE_COMPRESSED_ALTACCT      0x20
850 extern kern_return_t pmap_query_page_info(
851 	pmap_t          pmap,
852 	vm_map_offset_t va,
853 	int             *disp);
854 
855 extern bool pmap_in_ppl(void);
856 
857 extern uint32_t pmap_lookup_in_static_trust_cache(const uint8_t cdhash[CS_CDHASH_LEN]);
858 extern bool pmap_lookup_in_loaded_trust_caches(const uint8_t cdhash[CS_CDHASH_LEN]);
859 
860 /**
861  * Indicates whether the device supports register-level MMIO access control.
862  *
863  * @note Unlike the pmap-io-ranges mechanism, which enforces PPL-only register
864  *       writability at page granularity, this mechanism allows specific registers
865  *       on a read-mostly page to be written using a dedicated guarded mode trap
866  *       without requiring a full PPL driver extension.
867  *
868  * @return True if the device supports register-level MMIO access control.
869  */
870 extern bool pmap_has_iofilter_protected_write(void);
871 
872 /**
873  * Performs a write to the I/O register specified by addr on supported devices.
874  *
875  * @note On supported devices (determined by pmap_has_iofilter_protected_write()), this
876  *       function goes over the sorted I/O filter entry table. If there is a hit, the
877  *       write is performed from Guarded Mode. Otherwise, the write is performed from
878  *       Normal Mode (kernel mode). Note that you can still hit an exception if the
879  *       register is owned by PPL but not allowed by an io-filter-entry in the device tree.
880  *
881  * @note On unsupported devices, this function will panic.
882  *
883  * @param addr The address of the register.
884  * @param value The value to be written.
885  * @param width The width of the I/O register, supported values are 1, 2, 4 and 8.
886  */
887 extern void pmap_iofilter_protected_write(vm_address_t addr, uint64_t value, uint64_t width);
888 
889 extern void *pmap_claim_reserved_ppl_page(void);
890 extern void pmap_free_reserved_ppl_page(void *kva);
891 
892 extern void pmap_ledger_verify_size(size_t);
893 extern ledger_t pmap_ledger_alloc(void);
894 extern void pmap_ledger_free(ledger_t);
895 
896 extern bool pmap_is_bad_ram(ppnum_t ppn);
897 
898 extern bool pmap_is_page_restricted(ppnum_t pn);
899 
900 #if __arm64__
901 extern bool pmap_is_exotic(pmap_t pmap);
902 #else /* __arm64__ */
903 #define pmap_is_exotic(pmap) false
904 #endif /* __arm64__ */
905 
906 
907 /*
908  * Returns a subset of pmap_cs non-default configuration,
909  * e.g. loosening up of some restrictions through pmap_cs or amfi
910  * boot-args. The return value is a bit field with possible bits
911  * described below. If default, the function will return 0. Note that
912  * this does not work the other way: 0 does not imply that pmap_cs
913  * runs in default configuration, and only a small configuration
914  * subset is returned by this function.
915  *
916  * Never assume the system is "secure" if this returns 0.
917  */
918 extern int pmap_cs_configuration(void);
919 
920 #if XNU_KERNEL_PRIVATE
921 
922 typedef enum {
923 	PMAP_FEAT_UEXEC = 1
924 } pmap_feature_flags_t;
925 
926 #if defined(__x86_64__)
927 
928 extern bool             pmap_supported_feature(pmap_t pmap, pmap_feature_flags_t feat);
929 
930 #endif
931 #if defined(__arm64__)
932 
933 /**
934  * Check if a particular pmap is used for stage2 translations or not.
935  */
936 extern bool
937 pmap_performs_stage2_translations(const pmap_t pmap);
938 
939 #endif /* defined(__arm64__) */
940 
941 extern ppnum_t          kernel_pmap_present_mapping(uint64_t vaddr, uint64_t * pvincr, uintptr_t * pvphysaddr);
942 
943 #endif /* XNU_KERNEL_PRIVATE */
944 
945 #if CONFIG_SPTM
946 /*
947  * The TrustedExecutionMonitor address space data structure is kept within the
948  * pmap structure in order to provide a coherent API to the rest of the kernel
949  * for working with code signing monitors.
950  *
951  * However, a lot of parts of the kernel don't have visibility into the pmap
952  * data structure as they are opaque unless you're in the Mach portion of the
953  * kernel. To allievate this, we provide pmap APIs to the rest of the kernel.
954  */
955 #include <TrustedExecutionMonitor/API.h>
956 
957 /*
958  * All pages allocated by TXM are also kept within the TXM VM object, which allows
959  * tracking it for accounting and debugging purposes.
960  */
961 extern vm_object_t txm_vm_object;
962 
963 /**
964  * Acquire the pointer of the kernel pmap being used for the system.
965  */
966 extern pmap_t
967 pmap_txm_kernel_pmap(void);
968 
969 /**
970  * Acquire the TXM address space object stored within the pmap.
971  */
972 extern TXMAddressSpace_t*
973 pmap_txm_addr_space(const pmap_t pmap);
974 
975 /**
976  * Set the TXM address space object within the pmap.
977  */
978 extern void
979 pmap_txm_set_addr_space(
980 	pmap_t pmap,
981 	TXMAddressSpace_t *txm_addr_space);
982 
983 /**
984  * Set the trust level of the TXM address space object within the pmap.
985  */
986 extern void
987 pmap_txm_set_trust_level(
988 	pmap_t pmap,
989 	CSTrust_t trust_level);
990 
991 /**
992  * Get the trust level of the TXM address space object within the pmap.
993  */
994 extern kern_return_t
995 pmap_txm_get_trust_level_kdp(
996 	pmap_t pmap,
997 	CSTrust_t *trust_level);
998 
999 /**
1000  * Get the address range of the JIT region within the pmap, if any.
1001  */
1002 kern_return_t
1003 pmap_txm_get_jit_address_range_kdp(
1004 	pmap_t pmap,
1005 	uintptr_t *jit_region_start,
1006 	uintptr_t *jit_region_end);
1007 
1008 /**
1009  * Take a shared lock on the pmap in order to enforce safe concurrency for
1010  * an operation on the TXM address space object. Passing in NULL takes the lock
1011  * on the current pmap.
1012  */
1013 extern void
1014 pmap_txm_acquire_shared_lock(pmap_t pmap);
1015 
1016 /**
1017  * Release the shared lock which was previously acquired for operations on
1018  * the TXM address space object. Passing in NULL releases the lock for the
1019  * current pmap.
1020  */
1021 extern void
1022 pmap_txm_release_shared_lock(pmap_t pmap);
1023 
1024 /**
1025  * Take an exclusive lock on the pmap in order to enforce safe concurrency for
1026  * an operation on the TXM address space object. Passing in NULL takes the lock
1027  * on the current pmap.
1028  */
1029 extern void
1030 pmap_txm_acquire_exclusive_lock(pmap_t pmap);
1031 
1032 /**
1033  * Release the exclusive lock which was previously acquired for operations on
1034  * the TXM address space object. Passing in NULL releases the lock for the
1035  * current pmap.
1036  */
1037 extern void
1038 pmap_txm_release_exclusive_lock(pmap_t pmap);
1039 
1040 /**
1041  * Transfer a page to the TXM_DEFAULT type after resolving its mapping from its
1042  * virtual to physical address.
1043  */
1044 extern void
1045 pmap_txm_transfer_page(const vm_address_t addr);
1046 
1047 /**
1048  * Grab an available page from the VM free list, add it to the TXM VM object and
1049  * then transfer it to be owned by TXM.
1050  *
1051  * Returns the physical address of the page allocated.
1052  */
1053 extern vm_map_address_t
1054 pmap_txm_allocate_page(void);
1055 
1056 #endif /* CONFIG_SPTM */
1057 
1058 
1059 #endif  /* KERNEL_PRIVATE */
1060 
1061 #endif  /* _VM_PMAP_H_ */
1062