1 /*
2 * Copyright (c) 2007-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 * Machine-dependent structures for the physical map module.
30 *
31 * This header file contains the types and prototypes that make up the public
32 * pmap API that's exposed to the rest of the kernel. Any types/prototypes used
33 * strictly by the pmap itself should be placed into one of the osfmk/arm/pmap/
34 * header files.
35 *
36 * To prevent circular dependencies and exposing anything not needed by the
37 * rest of the kernel, this file shouldn't include ANY of the internal
38 * osfmk/arm/pmap/ header files.
39 */
40 #ifndef _ARM_PMAP_H_
41 #define _ARM_PMAP_H_
42
43 #include <mach_assert.h>
44
45 #include <arm/proc_reg.h>
46 #if defined(__arm64__)
47 #include <arm64/proc_reg.h>
48 #endif /* defined(__arm64__) */
49
50 #ifndef ASSEMBLER
51
52 #include <stdatomic.h>
53 #include <stdbool.h>
54 #include <libkern/section_keywords.h>
55 #include <mach/kern_return.h>
56 #include <mach/machine/vm_types.h>
57 #include <arm/pmap_public.h>
58 #include <kern/ast.h>
59 #include <mach/arm/thread_status.h>
60
61 #if defined(__arm64__)
62 #include <arm64/tlb.h>
63 #else /* defined(__arm64__) */
64 #include <arm/tlb.h>
65 #endif /* defined(__arm64__) */
66
67
68 /* Shift for 2048 max virtual ASIDs (2048 pmaps). */
69 #define ASID_SHIFT (11)
70
71 /* Max supported ASIDs (can be virtual). */
72 #define MAX_ASIDS (1 << ASID_SHIFT)
73
74 /* Shift for the maximum ARM ASID value (256) */
75 #ifndef ARM_ASID_SHIFT
76 #define ARM_ASID_SHIFT (8)
77 #endif /* ARM_ASID_SHIFT */
78
79 /* Max ASIDs supported by the hardware. */
80 #define ARM_MAX_ASIDS (1 << ARM_ASID_SHIFT)
81
82 /* Number of bits in a byte. */
83 #define NBBY (8)
84
85 /**
86 * The maximum number of hardware ASIDs used by the pmap for user address spaces.
87 *
88 * One ASID is always dedicated to the kernel (ASID 0). On systems with software-
89 * based spectre/meltdown mitigations, each address space technically uses two
90 * hardware ASIDs (one for EL1 and one for EL0) so the total number of available
91 * ASIDs a user process can use is halved on those systems.
92 */
93 #if __ARM_KERNEL_PROTECT__
94 #define MAX_HW_ASIDS ((ARM_MAX_ASIDS >> 1) - 1)
95 #else /* __ARM_KERNEL_PROTECT__ */
96 #define MAX_HW_ASIDS (ARM_MAX_ASIDS - 1)
97 #endif /* __ARM_KERNEL_PROTECT__ */
98
99 /* Maximum number of Virtual Machine IDs */
100 #ifndef ARM_VMID_SHIFT
101 #define ARM_VMID_SHIFT (8)
102 #endif /* ARM_VMID_SHIFT */
103 #define ARM_MAX_VMIDS (1 << ARM_VMID_SHIFT)
104
105 /* XPRR virtual register map */
106
107 /* Maximum number of CPU windows per-cpu. */
108 #define CPUWINDOWS_MAX 4
109
110 #if defined(__arm64__)
111
112 #if defined(ARM_LARGE_MEMORY)
113 /*
114 * 2 L1 tables (Linear KVA and V=P), plus 2*16 L2 tables map up to (16*64GB) 1TB of DRAM
115 * Upper limit on how many pages can be consumed by bootstrap page tables
116 */
117 #define BOOTSTRAP_TABLE_SIZE (ARM_PGBYTES * 34)
118 #else /* defined(ARM_LARGE_MEMORY) */
119 #define BOOTSTRAP_TABLE_SIZE (ARM_PGBYTES * 8)
120 #endif /* defined(ARM_LARGE_MEMORY) */
121
122 typedef uint64_t tt_entry_t; /* translation table entry type */
123 typedef uint64_t pt_entry_t; /* page table entry type */
124 #elif defined(__arm__)
125 typedef uint32_t tt_entry_t; /* translation table entry type */
126 typedef uint32_t pt_entry_t; /* page table entry type */
127 #else /* defined(__arm64__) */
128 #error unknown arch
129 #endif /* defined(__arm64__) */
130
131 /* Used to represent a NULL page/translation table entry pointer. */
132 #define PT_ENTRY_NULL ((pt_entry_t *) 0)
133 #define TT_ENTRY_NULL ((tt_entry_t *) 0)
134
135 /**
136 * Number of PTE pointers in a single PVE. This must be 2, since the algorithm
137 * has been optimized to that case. Should this change in the future, both
138 * enter_pv() and remove_pv() will need to be modified accordingly. In addition
139 * to this, the documentation and the LLDB macros that walk PV lists will also
140 * need to be adapted.
141 */
142 #define PTE_PER_PVE 2
143 _Static_assert(PTE_PER_PVE == 2, "PTE_PER_PVE is not 2");
144
145 /**
146 * Structure to track the active mappings for a given page. This structure is
147 * used in the pv_head_table when a physical page has more than one mapping to
148 * it. Each entry in this linked list of structures can represent
149 * up to PTE_PER_PVE mappings.
150 */
151 typedef struct pv_entry {
152 /* Linked list to the next mapping of the physical page. */
153 struct pv_entry *pve_next;
154
155 /* Pointer to the page table entry for this mapping. */
156 pt_entry_t *pve_ptep[PTE_PER_PVE];
157 }
158 #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
159 /**
160 * For the newer ARMv7k ABI where 64-bit types are 64-bit aligned, but pointers
161 * are 32-bit: since pt_desc is 64-bit aligned and we cast often from pv_entry
162 * to pt_desc.
163 */
164 __attribute__ ((aligned(8))) pv_entry_t;
165 #else /* __arm__ && (__BIGGEST_ALIGNMENT__ > 4) */
166 pv_entry_t;
167 #endif /* __arm__ && (__BIGGEST_ALIGNMENT__ > 4) */
168
169 /**
170 * Structure that tracks free pv_entry nodes for the pv_head_table. Each one
171 * of these nodes represents a single mapping to a physical page, so a new node
172 * is allocated whenever a new mapping is created.
173 */
174 typedef struct {
175 pv_entry_t *list;
176 uint32_t count;
177 } pv_free_list_t;
178
179 /**
180 * Forward declaration of the structure that controls page table geometry and
181 * TTE/PTE format.
182 */
183 struct page_table_attr;
184
185 struct pmap_cpu_data {
186 #if XNU_MONITOR
187 const volatile struct pmap * _Atomic active_pmap;
188 const volatile struct pmap * _Atomic inflight_pmap;
189 uint64_t pvh_info[4];
190 void *ppl_kern_saved_sp;
191 void *ppl_stack;
192 arm_context_t *save_area;
193 unsigned int ppl_state;
194 #endif /* XNU_MONITOR */
195 pmap_t cpu_nested_pmap;
196 #if __ARM_MIXED_PAGE_SIZE__
197 uint64_t commpage_page_shift;
198 #endif
199 #if defined(__arm64__)
200 const struct page_table_attr *cpu_nested_pmap_attr;
201 vm_map_address_t cpu_nested_region_addr;
202 vm_map_offset_t cpu_nested_region_size;
203 #else /* defined(__arm64__) */
204 pmap_t cpu_user_pmap;
205 unsigned int cpu_user_pmap_stamp;
206 #endif /* defined(__arm64__) */
207 unsigned int cpu_number;
208 bool copywindow_strong_sync[CPUWINDOWS_MAX];
209 bool inflight_disconnect;
210 pv_free_list_t pv_free;
211 pv_entry_t *pv_free_spill_marker;
212
213 /*
214 * This supports overloading of ARM ASIDs by the pmap. The field needs
215 * to be wide enough to cover all the virtual bits in a virtual ASID.
216 * With 256 physical ASIDs, 8-bit fields let us support up to 65536
217 * Virtual ASIDs, minus all that would map on to 0 (as 0 is a global
218 * ASID).
219 *
220 * If we were to use bitfield shenanigans here, we could save a bit of
221 * memory by only having enough bits to support MAX_ASIDS. However, such
222 * an implementation would be more error prone.
223 */
224 uint8_t cpu_sw_asids[MAX_HW_ASIDS];
225 };
226 typedef struct pmap_cpu_data pmap_cpu_data_t;
227
228 #include <mach/vm_prot.h>
229 #include <mach/vm_statistics.h>
230 #include <mach/machine/vm_param.h>
231 #include <kern/kern_types.h>
232 #include <kern/thread.h>
233 #include <kern/queue.h>
234
235
236 #include <sys/cdefs.h>
237
238 /* Base address for low globals. */
239 #if defined(ARM_LARGE_MEMORY)
240 #define LOW_GLOBAL_BASE_ADDRESS 0xfffffe0000000000ULL
241 #else /* defined(ARM_LARGE_MEMORY) */
242 #define LOW_GLOBAL_BASE_ADDRESS 0xfffffff000000000ULL
243 #endif /* defined(ARM_LARGE_MEMORY) */
244
245 /*
246 * This indicates (roughly) where there is free space for the VM
247 * to use for the heap; this does not need to be precise.
248 */
249 #if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
250 #if defined(ARM_LARGE_MEMORY)
251 #define KERNEL_PMAP_HEAP_RANGE_START (VM_MIN_KERNEL_AND_KEXT_ADDRESS+ARM_TT_L1_SIZE)
252 #else /* defined(ARM_LARGE_MEMORY) */
253 #define KERNEL_PMAP_HEAP_RANGE_START VM_MIN_KERNEL_AND_KEXT_ADDRESS
254 #endif /* defined(ARM_LARGE_MEMORY) */
255 #else /* defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) */
256 #if defined(ARM_LARGE_MEMORY)
257 /* For large memory systems with no KTRR/CTRR such as virtual machines */
258 #define KERNEL_PMAP_HEAP_RANGE_START (VM_MIN_KERNEL_AND_KEXT_ADDRESS+ARM_TT_L1_SIZE)
259 #else
260 #define KERNEL_PMAP_HEAP_RANGE_START LOW_GLOBAL_BASE_ADDRESS
261 #endif
262 #endif /* defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR) */
263
264 /**
265 * For setups where the VM page size does not match the hardware page size (the
266 * VM page size must be a multiple of the hardware page size), we will need to
267 * determine what the page ratio is.
268 */
269 #define PAGE_RATIO ((1 << PAGE_SHIFT) >> ARM_PGSHIFT)
270 #define TEST_PAGE_RATIO_4 (PAGE_RATIO == 4)
271
272
273 /* superpages */
274 #define SUPERPAGE_NBASEPAGES 1 /* No superpages support */
275
276 /* Convert addresses to pages and vice versa. No rounding is used. */
277 #define arm_atop(x) (((vm_map_address_t)(x)) >> ARM_PGSHIFT)
278 #define arm_ptoa(x) (((vm_map_address_t)(x)) << ARM_PGSHIFT)
279
280 /**
281 * Round off or truncate to the nearest page. These will work for either
282 * addresses or counts (i.e. 1 byte rounds to 1 page bytes).
283 */
284 #define arm_round_page(x) ((((vm_map_address_t)(x)) + ARM_PGMASK) & ~ARM_PGMASK)
285 #define arm_trunc_page(x) (((vm_map_address_t)(x)) & ~ARM_PGMASK)
286
287 #if __arm__
288 /* Convert address offset to page table index */
289 #define ptenum(a) ((((a) & ARM_TT_LEAF_INDEX_MASK) >> ARM_TT_LEAF_SHIFT))
290 #endif /* __arm__ */
291
292 extern void flush_mmu_tlb_region(vm_offset_t va, unsigned length);
293
294 #if defined(__arm64__)
295 extern uint64_t get_mmu_control(void);
296 extern uint64_t get_aux_control(void);
297 extern void set_aux_control(uint64_t);
298 extern void set_mmu_ttb(uint64_t);
299 extern void set_mmu_ttb_alternate(uint64_t);
300 extern uint64_t get_tcr(void);
301 extern void set_tcr(uint64_t);
302 extern uint64_t pmap_get_arm64_prot(pmap_t, vm_offset_t);
303 #else /* defined(__arm64__) */
304 extern uint32_t get_mmu_control(void);
305 extern void set_mmu_control(uint32_t);
306 extern uint32_t get_aux_control(void);
307 extern void set_aux_control(uint32_t);
308 extern void set_mmu_ttb(pmap_paddr_t);
309 extern void set_mmu_ttb_alternate(pmap_paddr_t);
310 extern void set_context_id(uint32_t);
311 #endif /* defined(__arm64__) */
312
313 extern pmap_paddr_t get_mmu_ttb(void);
314 extern pmap_paddr_t mmu_kvtop(vm_offset_t va);
315 extern pmap_paddr_t mmu_kvtop_wpreflight(vm_offset_t va);
316 extern pmap_paddr_t mmu_uvtop(vm_offset_t va);
317
318 #if (__ARM_VMSA__ <= 7)
319 /* Convert address offset to translation table index */
320 #define ttenum(a) ((a) >> ARM_TT_L1_SHIFT)
321
322 /* Convert translation table index to user virtual address */
323 #define tteitova(a) ((a) << ARM_TT_L1_SHIFT)
324
325 #define pa_to_suptte(a) ((a) & ARM_TTE_SUPER_L1_MASK)
326 #define suptte_to_pa(p) ((p) & ARM_TTE_SUPER_L1_MASK)
327
328 #define pa_to_sectte(a) ((a) & ARM_TTE_BLOCK_L1_MASK)
329 #define sectte_to_pa(p) ((p) & ARM_TTE_BLOCK_L1_MASK)
330
331 #define pa_to_tte(a) ((a) & ARM_TTE_TABLE_MASK)
332 #define tte_to_pa(p) ((p) & ARM_TTE_TABLE_MASK)
333
334 #define pa_to_pte(a) ((a) & ARM_PTE_PAGE_MASK)
335 #define pte_to_pa(p) ((p) & ARM_PTE_PAGE_MASK)
336 #define pte_increment_pa(p) ((p) += ptoa(1))
337
338 #define ARM_NESTING_SIZE_MIN ((PAGE_SIZE/0x1000)*4*ARM_TT_L1_SIZE)
339
340 #else /* __ARM_VMSA__ <= 7 */
341
342 /* Convert address offset to translation table index */
343 #define ttel0num(a) ((a & ARM_TTE_L0_MASK) >> ARM_TT_L0_SHIFT)
344 #define ttel1num(a) ((a & ARM_TTE_L1_MASK) >> ARM_TT_L1_SHIFT)
345 #define ttel2num(a) ((a & ARM_TTE_L2_MASK) >> ARM_TT_L2_SHIFT)
346
347 #define pa_to_tte(a) ((a) & ARM_TTE_TABLE_MASK)
348 #define tte_to_pa(p) ((p) & ARM_TTE_TABLE_MASK)
349
350 #define pa_to_pte(a) ((a) & ARM_PTE_PAGE_MASK)
351 #define pte_to_pa(p) ((p) & ARM_PTE_PAGE_MASK)
352 #define pte_to_ap(p) (((p) & ARM_PTE_APMASK) >> ARM_PTE_APSHIFT)
353 #define pte_increment_pa(p) ((p) += ptoa(1))
354
355 #define TLBFLUSH_SIZE (ARM_TTE_MAX/((sizeof(unsigned int))*BYTE_SIZE))
356
357 #endif /* __ARM_VMSA__ <= 7 */
358
359 /* Status flags used by the pmap garbage collection code. */
360 #define PMAP_GC_INFLIGHT 1
361 #define PMAP_GC_WAIT 2
362
363
364 #define pmap_cs_log(level, fmt, args...)
365 #define pmap_cs_log_debug(fmt, args...)
366 #define pmap_cs_log_info(fmt, args...)
367 #define pmap_cs_log_error(fmt, args...)
368 #define pmap_cs_log_force(level, fmt, args...)
369
370
371
372
373 /* Convert translation/page table entry to kernel virtual address. */
374 #define ttetokv(a) (phystokv(tte_to_pa(a)))
375 #define ptetokv(a) (phystokv(pte_to_pa(a)))
376
377 struct pmap {
378 /* Pointer to the root translation table. */
379 tt_entry_t *tte;
380
381 /* Physical page of the root translation table. */
382 pmap_paddr_t ttep;
383
384 /*
385 * The min and max fields represent the lowest and highest addressable VAs
386 * as dictated strictly by the paging hierarchy (root level + root table size)
387 * in conjunction with whether the root table is used with TTBR0, TTBR1, or VTTBR.
388 * These fields do not encapsulate any higher-level address-space partitioning
389 * policies.
390 */
391
392 /* Lowest supported VA (inclusive) */
393 vm_map_address_t min;
394
395 /* Highest supported VA (exclusive) */
396 vm_map_address_t max;
397
398 #if ARM_PARAMETERIZED_PMAP
399 /* Details about the page table layout. */
400 const struct page_table_attr * pmap_pt_attr;
401 #endif /* ARM_PARAMETERIZED_PMAP */
402
403 /* Ledger tracking phys mappings */
404 ledger_t ledger;
405
406 decl_lck_rw_data(, rwlock);
407
408 /* Global list of pmaps */
409 queue_chain_t pmaps;
410
411 /* Free list of translation table pages. */
412 tt_entry_t *tt_entry_free;
413
414 /* Information representing the "nested" (shared) region in this pmap. */
415 struct pmap *nested_pmap;
416 vm_map_address_t nested_region_addr;
417 vm_map_offset_t nested_region_size;
418 vm_map_offset_t nested_region_true_start;
419 vm_map_offset_t nested_region_true_end;
420 unsigned int *nested_region_asid_bitmap;
421 unsigned int nested_region_asid_bitmap_size;
422
423 #if (__ARM_VMSA__ <= 7)
424 /* Maximum number of translation table entries being used in the root table. */
425 unsigned int tte_index_max;
426 #endif /* (__ARM_VMSA__ <= 7) */
427
428 void * reserved0;
429 void * reserved1;
430 uint64_t reserved2;
431 uint64_t reserved3;
432
433 /* Creation stamp. Incremented by one for each pmap in the system. */
434 unsigned int stamp;
435
436 /* PMAP reference count */
437 _Atomic int32_t ref_count;
438
439 #if XNU_MONITOR
440 /* number of pmaps in which this pmap is nested */
441 _Atomic int32_t nested_count;
442 #endif
443
444 /* Garbage Collection status */
445 unsigned int gc_status;
446
447 /* Number of pmaps that nested this pmap without bounds set. */
448 uint32_t nested_no_bounds_refcnt;
449
450 /**
451 * Represents the real hardware ASID inserted into each TLB entry within
452 * this address space.
453 */
454 uint16_t hw_asid;
455
456 /**
457 * Represents the virtual "software" ASID. Any real hardware ASID can have
458 * multiple software ASIDs associated with it. This is used to know when to
459 * perform TLB flushes during context switches.
460 */
461 uint8_t sw_asid;
462
463 #if MACH_ASSERT
464 int pmap_pid;
465 char pmap_procname[17];
466 bool pmap_stats_assert;
467 #endif /* MACH_ASSERT */
468
469 bool reserved4;
470
471 bool pmap_vm_map_cs_enforced;
472
473 boolean_t reserved5;
474 unsigned int reserved6;
475 unsigned int reserved7;
476
477 bool reserved8;
478 bool reserved9;
479
480 #if DEVELOPMENT || DEBUG
481 bool footprint_suspended;
482 bool footprint_was_suspended;
483 #endif /* DEVELOPMENT || DEBUG */
484
485 /* Whether the No-Execute functionality is enabled. */
486 bool nx_enabled;
487
488 /* Whether this pmap represents a 64-bit address space. */
489 bool is_64bit;
490
491 /* Nested a pmap when the bounds were not set. */
492 bool nested_has_no_bounds_ref;
493
494 /* The nesting bounds have been set. */
495 bool nested_bounds_set;
496
497 #if HAS_APPLE_PAC
498 bool disable_jop;
499 #else
500 bool reserved10;
501 #endif /* HAS_APPLE_PAC */
502
503 #define PMAP_TYPE_USER 0 /* ordinary pmap */
504 #define PMAP_TYPE_KERNEL 1 /* kernel pmap */
505 #define PMAP_TYPE_COMMPAGE 2 /* commpage pmap */
506 #define PMAP_TYPE_NESTED 3 /* pmap nested within another pmap */
507 uint8_t type;
508 };
509
510 #define PMAP_VASID(pmap) (((uint32_t)((pmap)->sw_asid) << 16) | pmap->hw_asid)
511
512 #if VM_DEBUG
513 extern int pmap_list_resident_pages(
514 pmap_t pmap,
515 vm_offset_t *listp,
516 int space);
517 #else /* VM_DEBUG */
518 #define pmap_list_resident_pages(pmap, listp, space) (0)
519 #endif /* VM_DEBUG */
520
521 extern int copysafe(vm_map_address_t from, vm_map_address_t to, uint32_t cnt, int type, uint32_t *bytes_copied);
522
523 /* Globals shared between arm_vm_init and pmap */
524 extern tt_entry_t *cpu_tte; /* First CPUs translation table (shared with kernel pmap) */
525 extern pmap_paddr_t cpu_ttep; /* Physical translation table addr */
526
527 #if __arm64__
528 extern void *ropagetable_begin;
529 extern void *ropagetable_end;
530 #endif /* __arm64__ */
531
532 #if __arm64__
533 extern tt_entry_t *invalid_tte; /* Global invalid translation table */
534 extern pmap_paddr_t invalid_ttep; /* Physical invalid translation table addr */
535 #endif /* __arm64__ */
536
537 #define PMAP_CONTEXT(pmap, thread)
538
539 /**
540 * Platform dependent Prototypes
541 */
542 extern void pmap_clear_user_ttb(void);
543 extern void pmap_bootstrap(vm_offset_t);
544 extern vm_map_address_t pmap_ptov(pmap_t, ppnum_t);
545 extern pmap_paddr_t pmap_find_pa(pmap_t map, addr64_t va);
546 extern pmap_paddr_t pmap_find_pa_nofault(pmap_t map, addr64_t va);
547 extern ppnum_t pmap_find_phys(pmap_t map, addr64_t va);
548 extern ppnum_t pmap_find_phys_nofault(pmap_t map, addr64_t va);
549 extern void pmap_switch_user(thread_t th, vm_map_t map);
550 extern void pmap_set_pmap(pmap_t pmap, thread_t thread);
551 extern void pmap_collect(pmap_t pmap);
552 extern void pmap_gc(void);
553 #if HAS_APPLE_PAC
554 extern void * pmap_sign_user_ptr(void *value, ptrauth_key key, uint64_t data, uint64_t jop_key);
555 extern void * pmap_auth_user_ptr(void *value, ptrauth_key key, uint64_t data, uint64_t jop_key);
556 #endif /* HAS_APPLE_PAC */
557
558 /**
559 * Interfaces implemented as macros.
560 */
561
562 #define PMAP_SWITCH_USER(th, new_map, my_cpu) pmap_switch_user((th), (new_map))
563
564 #define pmap_kernel() (kernel_pmap)
565
566 #define pmap_kernel_va(VA) \
567 (((VA) >= VM_MIN_KERNEL_ADDRESS) && ((VA) <= VM_MAX_KERNEL_ADDRESS))
568
569 #define pmap_attribute(pmap, addr, size, attr, value) (KERN_INVALID_ADDRESS)
570
571 #define copyinmsg(from, to, cnt) copyin(from, to, cnt)
572 #define copyoutmsg(from, to, cnt) copyout(from, to, cnt)
573
574 /* Unimplemented interfaces. */
575 #define MACRO_NOOP
576 #define pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr) MACRO_NOOP
577 #define pmap_pageable(pmap, start, end, pageable) MACRO_NOOP
578
579 extern pmap_paddr_t kvtophys(vm_offset_t va);
580 extern pmap_paddr_t kvtophys_nofail(vm_offset_t va);
581 extern vm_map_address_t phystokv(pmap_paddr_t pa);
582 extern vm_map_address_t phystokv_range(pmap_paddr_t pa, vm_size_t *max_len);
583
584 extern vm_map_address_t pmap_map(vm_map_address_t va, vm_offset_t sa, vm_offset_t ea, vm_prot_t prot, unsigned int flags);
585 extern vm_map_address_t pmap_map_high_window_bd( vm_offset_t pa, vm_size_t len, vm_prot_t prot);
586 extern kern_return_t pmap_map_block(pmap_t pmap, addr64_t va, ppnum_t pa, uint32_t size, vm_prot_t prot, int attr, unsigned int flags);
587 extern kern_return_t pmap_map_block_addr(pmap_t pmap, addr64_t va, pmap_paddr_t pa, uint32_t size, vm_prot_t prot, int attr, unsigned int flags);
588 extern void pmap_map_globals(void);
589
590 #define PMAP_MAP_BD_DEVICE 0x0
591 #define PMAP_MAP_BD_WCOMB 0x1
592 #define PMAP_MAP_BD_POSTED 0x2
593 #define PMAP_MAP_BD_POSTED_REORDERED 0x3
594 #define PMAP_MAP_BD_POSTED_COMBINED_REORDERED 0x4
595 #define PMAP_MAP_BD_MASK 0x7
596
597 extern vm_map_address_t pmap_map_bd_with_options(vm_map_address_t va, vm_offset_t sa, vm_offset_t ea, vm_prot_t prot, int32_t options);
598 extern vm_map_address_t pmap_map_bd(vm_map_address_t va, vm_offset_t sa, vm_offset_t ea, vm_prot_t prot);
599
600 extern void pmap_init_pte_page(pmap_t, pt_entry_t *, vm_offset_t, unsigned int ttlevel, boolean_t alloc_ptd);
601
602 extern boolean_t pmap_valid_address(pmap_paddr_t addr);
603 extern void pmap_disable_NX(pmap_t pmap);
604 extern void pmap_set_nested(pmap_t pmap);
605 extern void pmap_create_sharedpages(vm_map_address_t *kernel_data_addr, vm_map_address_t *kernel_text_addr, vm_map_address_t *user_text_addr);
606 extern void pmap_insert_sharedpage(pmap_t pmap);
607 extern void pmap_protect_sharedpage(void);
608
609 extern vm_offset_t pmap_cpu_windows_copy_addr(int cpu_num, unsigned int index);
610 extern unsigned int pmap_map_cpu_windows_copy(ppnum_t pn, vm_prot_t prot, unsigned int wimg_bits);
611 extern void pmap_unmap_cpu_windows_copy(unsigned int index);
612
613 extern void pmap_ro_zone_memcpy(zone_id_t zid, vm_offset_t va, vm_offset_t offset, vm_offset_t new_data, vm_size_t new_data_size);
614 extern void pmap_ro_zone_bzero(zone_id_t zid, vm_offset_t va, vm_offset_t offset, vm_size_t size);
615
616 #if XNU_MONITOR
617 /* exposed for use by the HMAC SHA driver */
618 extern void pmap_invoke_with_page(ppnum_t page_number, void *ctx,
619 void (*callback)(void *ctx, ppnum_t page_number, const void *page));
620 extern void pmap_hibernate_invoke(void *ctx, void (*callback)(void *ctx, uint64_t addr, uint64_t len));
621 extern void pmap_set_ppl_hashed_flag(const pmap_paddr_t addr);
622 extern void pmap_clear_ppl_hashed_flag_all(void);
623 extern void pmap_check_ppl_hashed_flag_all(void);
624 #endif /* XNU_MONITOR */
625
626 extern boolean_t pmap_valid_page(ppnum_t pn);
627 extern boolean_t pmap_bootloader_page(ppnum_t pn);
628
629 extern boolean_t pmap_is_empty(pmap_t pmap, vm_map_offset_t start, vm_map_offset_t end);
630
631 #define ARM_PMAP_MAX_OFFSET_DEFAULT 0x01
632 #define ARM_PMAP_MAX_OFFSET_MIN 0x02
633 #define ARM_PMAP_MAX_OFFSET_MAX 0x04
634 #define ARM_PMAP_MAX_OFFSET_DEVICE 0x08
635 #define ARM_PMAP_MAX_OFFSET_JUMBO 0x10
636
637 extern vm_map_offset_t pmap_max_offset(boolean_t is64, unsigned int option);
638 extern vm_map_offset_t pmap_max_64bit_offset(unsigned int option);
639 extern vm_map_offset_t pmap_max_32bit_offset(unsigned int option);
640
641 boolean_t pmap_virtual_region(unsigned int region_select, vm_map_offset_t *startp, vm_map_size_t *size);
642
643 boolean_t pmap_enforces_execute_only(pmap_t pmap);
644
645
646
647 /* pmap dispatch indices */
648 #define ARM_FAST_FAULT_INDEX 0
649 #define ARM_FORCE_FAST_FAULT_INDEX 1
650 #define MAPPING_FREE_PRIME_INDEX 2
651 #define MAPPING_REPLENISH_INDEX 3
652 #define PHYS_ATTRIBUTE_CLEAR_INDEX 4
653 #define PHYS_ATTRIBUTE_SET_INDEX 5
654 #define PMAP_BATCH_SET_CACHE_ATTRIBUTES_INDEX 6
655 #define PMAP_CHANGE_WIRING_INDEX 7
656 #define PMAP_CREATE_INDEX 8
657 #define PMAP_DESTROY_INDEX 9
658 #define PMAP_ENTER_OPTIONS_INDEX 10
659 /* #define PMAP_EXTRACT_INDEX 11 -- Not used*/
660 #define PMAP_FIND_PA_INDEX 12
661 #define PMAP_INSERT_SHAREDPAGE_INDEX 13
662 #define PMAP_IS_EMPTY_INDEX 14
663 #define PMAP_MAP_CPU_WINDOWS_COPY_INDEX 15
664 #define PMAP_MARK_PAGE_AS_PMAP_PAGE_INDEX 16
665 #define PMAP_NEST_INDEX 17
666 #define PMAP_PAGE_PROTECT_OPTIONS_INDEX 18
667 #define PMAP_PROTECT_OPTIONS_INDEX 19
668 #define PMAP_QUERY_PAGE_INFO_INDEX 20
669 #define PMAP_QUERY_RESIDENT_INDEX 21
670 #define PMAP_REFERENCE_INDEX 22
671 #define PMAP_REMOVE_OPTIONS_INDEX 23
672 #define PMAP_SET_CACHE_ATTRIBUTES_INDEX 25
673 #define PMAP_SET_NESTED_INDEX 26
674 #define PMAP_SET_PROCESS_INDEX 27
675 #define PMAP_SWITCH_INDEX 28
676 #define PMAP_SWITCH_USER_TTB_INDEX 29
677 #define PMAP_CLEAR_USER_TTB_INDEX 30
678 #define PMAP_UNMAP_CPU_WINDOWS_COPY_INDEX 31
679 #define PMAP_UNNEST_OPTIONS_INDEX 32
680 #define PMAP_FOOTPRINT_SUSPEND_INDEX 33
681 #define PMAP_CPU_DATA_INIT_INDEX 34
682 #define PMAP_RELEASE_PAGES_TO_KERNEL_INDEX 35
683 #define PMAP_SET_JIT_ENTITLED_INDEX 36
684
685
686 #define PMAP_UPDATE_COMPRESSOR_PAGE_INDEX 55
687 #define PMAP_TRIM_INDEX 56
688 #define PMAP_LEDGER_VERIFY_SIZE_INDEX 57
689 #define PMAP_LEDGER_ALLOC_INDEX 58
690 #define PMAP_LEDGER_FREE_INDEX 59
691
692 #if HAS_APPLE_PAC
693 #define PMAP_SIGN_USER_PTR 60
694 #define PMAP_AUTH_USER_PTR 61
695 #endif /* HAS_APPLE_PAC */
696
697 #define PHYS_ATTRIBUTE_CLEAR_RANGE_INDEX 66
698
699
700 #if __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX)
701 #define PMAP_DISABLE_USER_JOP_INDEX 69
702 #endif /* __has_feature(ptrauth_calls) && defined(XNU_TARGET_OS_OSX) */
703
704
705
706 #define PMAP_SET_VM_MAP_CS_ENFORCED_INDEX 72
707
708 #define PMAP_SET_COMPILATION_SERVICE_CDHASH_INDEX 73
709 #define PMAP_MATCH_COMPILATION_SERVICE_CDHASH_INDEX 74
710 #define PMAP_NOP_INDEX 75
711
712 #define PMAP_RO_ZONE_MEMCPY_INDEX 76
713
714 #if DEVELOPMENT || DEBUG
715 #define PMAP_TEST_TEXT_CORRUPTION_INDEX 79
716 #endif /* DEVELOPMENT || DEBUG */
717
718
719
720 #define PMAP_SET_LOCAL_SIGNING_PUBLIC_KEY_INDEX 84
721 #define PMAP_UNRESTRICT_LOCAL_SIGNING_INDEX 85
722
723
724
725 #define PMAP_RO_ZONE_BZERO_INDEX 90
726
727
728 #define PMAP_COUNT 96
729
730 /**
731 * Value used when initializing pmap per-cpu data to denote that the structure
732 * hasn't been initialized with its associated CPU number yet.
733 */
734 #define PMAP_INVALID_CPU_NUM (~0U)
735
736 /**
737 * Align the pmap per-cpu data to the L2 cache size for each individual CPU's
738 * data. This prevents accesses from one CPU affecting another, especially
739 * when atomically updating fields.
740 */
741 struct pmap_cpu_data_array_entry {
742 pmap_cpu_data_t cpu_data;
743 } __attribute__((aligned(MAX_L2_CLINE_BYTES)));
744
745 /* Initialize the pmap per-CPU data for the current CPU. */
746 extern void pmap_cpu_data_init(void);
747
748 /* Get the pmap per-CPU data for the current CPU. */
749 extern pmap_cpu_data_t *pmap_get_cpu_data(void);
750
751 /* Get the pmap per-CPU data for an arbitrary CPU. */
752 extern pmap_cpu_data_t *pmap_get_remote_cpu_data(unsigned int cpu);
753
754 /*
755 * For long-running PV list operations, we pick a reasonable maximum chunk size
756 * beyond which we will exit to preemptible context to avoid excessive preemption
757 * latency and PVH lock timeouts.
758 */
759 #define PMAP_MAX_PV_LIST_CHUNK_SIZE 64
760
761 /*
762 * For most batched page operations, we pick a sane default page count
763 * interval at which to check for pending preemption and exit the PPL if found.
764 */
765 #define PMAP_DEFAULT_PREEMPTION_CHECK_PAGE_INTERVAL 64
766
767 inline bool
_pmap_pending_preemption_real(void)768 _pmap_pending_preemption_real(void)
769 {
770 return !!(*((volatile ast_t*)ast_pending()) & AST_URGENT);
771 }
772
773 #if SCHED_PREEMPTION_DISABLE_DEBUG && (DEBUG || DEVELOPMENT)
774 bool pmap_pending_preemption(void); // more complicated, so externally defined
775 #else /* SCHED_PREEMPTION_DISABLE_DEBUG && (DEBUG || DEVELOPMENT) */
776 #define pmap_pending_preemption _pmap_pending_preemption_real
777 #endif /* SCHED_PREEMPTION_DISABLE_DEBUG && (DEBUG || DEVELOPMENT) */
778
779 #if XNU_MONITOR
780 extern boolean_t pmap_ppl_locked_down;
781
782 /*
783 * Denotes the bounds of the PPL stacks. These are visible so that other code
784 * can check if addresses are part of the PPL stacks.
785 */
786 extern void *pmap_stacks_start;
787 extern void *pmap_stacks_end;
788
789 /* Asks if a page belongs to the monitor. */
790 extern boolean_t pmap_is_monitor(ppnum_t pn);
791
792 /*
793 * Indicates that we are done with our static bootstrap
794 * allocations, so the monitor may now mark the pages
795 * that it owns.
796 */
797 extern void pmap_static_allocations_done(void);
798
799 /*
800 * Indicates that we are done mutating sensitive state in the system, and that
801 * the PPL may now restict write access to PPL owned mappings.
802 */
803 extern void pmap_lockdown_ppl(void);
804
805
806 #ifdef KASAN
807 #define PPL_STACK_SIZE (PAGE_SIZE << 2)
808 #else /* KASAN */
809 #define PPL_STACK_SIZE PAGE_SIZE
810 #endif /* KASAN */
811
812 /* One stack for each CPU, plus a guard page below each stack and above the last stack */
813 #define PPL_STACK_REGION_SIZE ((MAX_CPUS * (PPL_STACK_SIZE + ARM_PGBYTES)) + ARM_PGBYTES)
814
815 #define PPL_DATA_SEGMENT_SECTION_NAME "__PPLDATA,__data"
816 #define PPL_TEXT_SEGMENT_SECTION_NAME "__PPLTEXT,__text,regular,pure_instructions"
817 #define PPL_DATACONST_SEGMENT_SECTION_NAME "__PPLDATA,__const"
818
819 #define MARK_AS_PMAP_DATA \
820 __PLACE_IN_SECTION(PPL_DATA_SEGMENT_SECTION_NAME)
821 #define MARK_AS_PMAP_TEXT \
822 __attribute__((used, section(PPL_TEXT_SEGMENT_SECTION_NAME), noinline))
823 #define MARK_AS_PMAP_RODATA \
824 __PLACE_IN_SECTION(PPL_DATACONST_SEGMENT_SECTION_NAME)
825
826 #else /* XNU_MONITOR */
827
828 #define MARK_AS_PMAP_TEXT
829 #define MARK_AS_PMAP_DATA
830 #define MARK_AS_PMAP_RODATA
831
832 #endif /* XNU_MONITOR */
833
834
835 extern void pmap_nop(pmap_t);
836
837 extern lck_grp_t pmap_lck_grp;
838
839 #if XNU_MONITOR
840 extern void CleanPoC_DcacheRegion_Force_nopreempt(vm_offset_t va, size_t length);
841 #define pmap_force_dcache_clean(va, sz) CleanPoC_DcacheRegion_Force_nopreempt(va, sz)
842 #define pmap_simple_lock(l) simple_lock_nopreempt(l, &pmap_lck_grp)
843 #define pmap_simple_unlock(l) simple_unlock_nopreempt(l)
844 #define pmap_simple_lock_try(l) simple_lock_try_nopreempt(l, &pmap_lck_grp)
845 #define pmap_simple_lock_assert(l, t) simple_lock_assert(l, t)
846 #define pmap_lock_bit(l, i) hw_lock_bit_nopreempt(l, i, &pmap_lck_grp)
847 #define pmap_unlock_bit(l, i) hw_unlock_bit_nopreempt(l, i)
848 #else /* XNU_MONITOR */
849 #define pmap_force_dcache_clean(va, sz) CleanPoC_DcacheRegion_Force(va, sz)
850 #define pmap_simple_lock(l) simple_lock(l, &pmap_lck_grp)
851 #define pmap_simple_unlock(l) simple_unlock(l)
852 #define pmap_simple_lock_try(l) simple_lock_try(l, &pmap_lck_grp)
853 #define pmap_simple_lock_assert(l, t) simple_lock_assert(l, t)
854 #define pmap_lock_bit(l, i) hw_lock_bit(l, i, &pmap_lck_grp)
855 #define pmap_unlock_bit(l, i) hw_unlock_bit(l, i)
856 #endif /* XNU_MONITOR */
857
858 #if DEVELOPMENT || DEBUG
859 extern kern_return_t pmap_test_text_corruption(pmap_paddr_t);
860 #endif /* DEVELOPMENT || DEBUG */
861
862 #endif /* #ifndef ASSEMBLER */
863
864 #if __ARM_KERNEL_PROTECT__
865 /*
866 * The exception vector mappings start at the middle of the kernel page table
867 * range (so that the EL0 mapping can be located at the base of the range).
868 */
869 #define ARM_KERNEL_PROTECT_EXCEPTION_START ((~((ARM_TT_ROOT_SIZE + ARM_TT_ROOT_INDEX_MASK) / 2ULL)) + 1ULL)
870 #endif /* __ARM_KERNEL_PROTECT__ */
871
872 #endif /* #ifndef _ARM_PMAP_H_ */
873