2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
61 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * (These guys wrote the Vax version)
64 * Physical Map management code for Intel i386, i486, and i860.
66 * Manages physical address maps.
68 * In addition to hardware address maps, this
69 * module is called upon to provide software-use-only
70 * maps which may or may not be stored in the same
71 * form as hardware maps. These pseudo-maps are
72 * used to store intermediate results from copy
73 * operations to and from address spaces.
75 * Since the information managed by this module is
76 * also stored by the logical address mapping module,
77 * this module may throw away valid virtual-to-physical
78 * mappings at almost any time. However, invalidations
79 * of virtual-to-physical mappings must be done as
82 * In order to cope with hardware architectures which
83 * make virtual-to-physical map invalidates expensive,
84 * this module may delay invalidate or reduced protection
85 * operations until such time as they are actually
86 * necessary. This module is given full information as
87 * to which processors are currently using which maps,
88 * and to when physical maps must be made correct.
92 #include <mach_ldebug.h>
94 #include <libkern/OSAtomic.h>
96 #include <mach/machine/vm_types.h>
98 #include <mach/boolean.h>
99 #include <kern/thread.h>
100 #include <kern/zalloc.h>
101 #include <kern/queue.h>
102 #include <kern/ledger.h>
103 #include <kern/mach_param.h>
105 #include <kern/spl.h>
108 #include <vm/vm_map.h>
109 #include <vm/vm_kern.h>
110 #include <mach/vm_param.h>
111 #include <mach/vm_prot.h>
112 #include <vm/vm_object.h>
113 #include <vm/vm_page.h>
115 #include <mach/machine/vm_param.h>
116 #include <machine/thread.h>
118 #include <kern/misc_protos.h> /* prototyping */
119 #include <i386/misc_protos.h>
120 #include <i386/i386_lowmem.h>
121 #include <x86_64/lowglobals.h>
123 #include <i386/cpuid.h>
124 #include <i386/cpu_data.h>
125 #include <i386/cpu_number.h>
126 #include <i386/machine_cpu.h>
127 #include <i386/seg.h>
128 #include <i386/serial_io.h>
129 #include <i386/cpu_capabilities.h>
130 #include <i386/machine_routines.h>
131 #include <i386/proc_reg.h>
132 #include <i386/tsc.h>
133 #include <i386/pmap_internal.h>
134 #include <i386/pmap_pcid.h>
136 #include <i386/vmx/vmx_cpu.h>
139 #include <vm/vm_protos.h>
140 #include <san/kasan.h>
143 #include <i386/mp_desc.h>
144 #include <libkern/kernel_mach_header.h>
146 #include <pexpert/i386/efi.h>
147 #include <libkern/section_keywords.h>
149 int pmap_stats_assert
= 1;
150 #endif /* MACH_ASSERT */
155 #define POSTCODE_DELAY 1
156 #include <i386/postcode.h>
157 #endif /* IWANTTODEBUG */
160 #define DBG(x...) kprintf("DBG: " x)
164 /* Compile time assert to ensure adjacency/alignment of per-CPU data fields used
165 * in the trampolines for kernel/user boundary TLB coherency.
167 char pmap_cpu_data_assert
[(((offsetof(cpu_data_t
, cpu_tlb_invalid
) - offsetof(cpu_data_t
, cpu_active_cr3
)) == 8) && (offsetof(cpu_data_t
, cpu_active_cr3
) % 64 == 0)) ? 1 : -1];
168 boolean_t pmap_trace
= FALSE
;
170 boolean_t no_shared_cr3
= DEBUG
; /* TRUE for DEBUG by default */
172 #if DEVELOPMENT || DEBUG
173 int nx_enabled
= 1; /* enable no-execute protection -- set during boot */
175 const int nx_enabled
= 1;
178 #if DEBUG || DEVELOPMENT
179 int allow_data_exec
= VM_ABI_32
; /* 32-bit apps may execute data by default, 64-bit apps may not */
180 int allow_stack_exec
= 0; /* No apps may execute from the stack by default */
181 #else /* DEBUG || DEVELOPMENT */
182 const int allow_data_exec
= VM_ABI_32
; /* 32-bit apps may execute data by default, 64-bit apps may not */
183 const int allow_stack_exec
= 0; /* No apps may execute from the stack by default */
184 #endif /* DEBUG || DEVELOPMENT */
186 uint64_t max_preemption_latency_tsc
= 0;
188 pv_hashed_entry_t
*pv_hash_table
; /* hash lists */
190 uint32_t npvhashmask
= 0, npvhashbuckets
= 0;
192 pv_hashed_entry_t pv_hashed_free_list
= PV_HASHED_ENTRY_NULL
;
193 pv_hashed_entry_t pv_hashed_kern_free_list
= PV_HASHED_ENTRY_NULL
;
194 SIMPLE_LOCK_DECLARE(pv_hashed_free_list_lock
, 0);
195 SIMPLE_LOCK_DECLARE(pv_hashed_kern_free_list_lock
, 0);
196 SIMPLE_LOCK_DECLARE(pv_hash_table_lock
, 0);
197 SIMPLE_LOCK_DECLARE(phys_backup_lock
, 0);
199 SECURITY_READ_ONLY_LATE(zone_t
) pv_hashed_list_zone
; /* zone of pv_hashed_entry structures */
202 * First and last physical addresses that we maintain any information
203 * for. Initialized to zero so that pmap operations done before
204 * pmap_init won't touch any non-existent structures.
206 boolean_t pmap_initialized
= FALSE
;/* Has pmap_init completed? */
208 static struct vm_object kptobj_object_store VM_PAGE_PACKED_ALIGNED
;
209 static struct vm_object kpml4obj_object_store VM_PAGE_PACKED_ALIGNED
;
210 static struct vm_object kpdptobj_object_store VM_PAGE_PACKED_ALIGNED
;
213 * Array of physical page attribites for managed pages.
214 * One byte per physical page.
216 char *pmap_phys_attributes
;
217 ppnum_t last_managed_page
= 0;
219 unsigned pmap_memory_region_count
;
220 unsigned pmap_memory_region_current
;
222 pmap_memory_region_t pmap_memory_regions
[PMAP_MEMORY_REGIONS_SIZE
];
225 * Other useful macros.
227 #define current_pmap() (vm_map_pmap(current_thread()->map))
229 struct pmap kernel_pmap_store
;
230 SECURITY_READ_ONLY_LATE(pmap_t
) kernel_pmap
= NULL
;
231 SECURITY_READ_ONLY_LATE(zone_t
) pmap_zone
; /* zone of pmap structures */
232 SECURITY_READ_ONLY_LATE(zone_t
) pmap_anchor_zone
;
233 SECURITY_READ_ONLY_LATE(zone_t
) pmap_uanchor_zone
;
234 int pmap_debug
= 0; /* flag for debugging prints */
236 unsigned int inuse_ptepages_count
= 0;
237 long long alloc_ptepages_count
__attribute__((aligned(8))) = 0; /* aligned for atomic access */
238 unsigned int bootstrap_wired_pages
= 0;
240 extern long NMIPI_acks
;
242 SECURITY_READ_ONLY_LATE(boolean_t
) kernel_text_ps_4K
= TRUE
;
248 #if DEVELOPMENT || DEBUG
249 SECURITY_READ_ONLY_LATE(boolean_t
) pmap_disable_kheap_nx
= FALSE
;
250 SECURITY_READ_ONLY_LATE(boolean_t
) pmap_disable_kstack_nx
= FALSE
;
251 SECURITY_READ_ONLY_LATE(boolean_t
) wpkernel
= TRUE
;
253 const boolean_t wpkernel
= TRUE
;
256 extern long __stack_chk_guard
[];
258 static uint64_t pmap_eptp_flags
= 0;
259 boolean_t pmap_ept_support_ad
= FALSE
;
261 static void process_pmap_updates(pmap_t
, bool, addr64_t
, addr64_t
);
263 * Map memory at initialization. The physical addresses being
264 * mapped are not managed and are never unmapped.
266 * For now, VM is already on, we only need to map the
272 vm_map_offset_t start_addr
,
273 vm_map_offset_t end_addr
,
281 while (start_addr
< end_addr
) {
282 kr
= pmap_enter(kernel_pmap
, (vm_map_offset_t
)virt
,
283 (ppnum_t
) i386_btop(start_addr
), prot
, VM_PROT_NONE
, flags
, TRUE
);
285 if (kr
!= KERN_SUCCESS
) {
286 panic("%s: failed pmap_enter, "
287 "virt=%p, start_addr=%p, end_addr=%p, prot=%#x, flags=%#x",
289 (void *)virt
, (void *)start_addr
, (void *)end_addr
, prot
, flags
);
298 extern char *first_avail
;
299 extern vm_offset_t virtual_avail
, virtual_end
;
300 extern pmap_paddr_t avail_start
, avail_end
;
301 extern vm_offset_t sHIB
;
302 extern vm_offset_t eHIB
;
303 extern vm_offset_t stext
;
304 extern vm_offset_t etext
;
305 extern vm_offset_t sdata
, edata
;
306 extern vm_offset_t sconst
, econst
;
308 extern void *KPTphys
;
310 boolean_t pmap_smep_enabled
= FALSE
;
311 boolean_t pmap_smap_enabled
= FALSE
;
316 cpu_data_t
*cdp
= current_cpu_datap();
318 set_cr4(get_cr4() | CR4_PGE
);
321 * Initialize the per-cpu, TLB-related fields.
323 cdp
->cpu_kernel_cr3
= kernel_pmap
->pm_cr3
;
324 cpu_shadowp(cdp
->cpu_number
)->cpu_kernel_cr3
= cdp
->cpu_kernel_cr3
;
325 cdp
->cpu_active_cr3
= kernel_pmap
->pm_cr3
;
326 cdp
->cpu_tlb_invalid
= 0;
327 cdp
->cpu_task_map
= TASK_MAP_64BIT
;
329 pmap_pcid_configure();
330 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMEP
) {
331 pmap_smep_enabled
= TRUE
;
332 #if DEVELOPMENT || DEBUG
334 if (PE_parse_boot_argn("-pmap_smep_disable", &nsmep
, sizeof(nsmep
))) {
335 pmap_smep_enabled
= FALSE
;
338 if (pmap_smep_enabled
) {
339 set_cr4(get_cr4() | CR4_SMEP
);
342 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMAP
) {
343 pmap_smap_enabled
= TRUE
;
344 #if DEVELOPMENT || DEBUG
346 if (PE_parse_boot_argn("-pmap_smap_disable", &nsmap
, sizeof(nsmap
))) {
347 pmap_smap_enabled
= FALSE
;
350 if (pmap_smap_enabled
) {
351 set_cr4(get_cr4() | CR4_SMAP
);
356 if (cdp
->cpu_fixed_pmcs_enabled
) {
357 boolean_t enable
= TRUE
;
358 cpu_pmc_control(&enable
);
360 #endif /* !MONOTONIC */
364 pmap_scale_shift(void)
368 if (sane_size
<= 8 * GB
) {
369 scale
= (uint32_t)(sane_size
/ (2 * GB
));
370 } else if (sane_size
<= 32 * GB
) {
371 scale
= 4 + (uint32_t)((sane_size
- (8 * GB
)) / (4 * GB
));
373 scale
= 10 + (uint32_t)MIN(4, ((sane_size
- (32 * GB
)) / (8 * GB
)));
378 LCK_GRP_DECLARE(pmap_lck_grp
, "pmap");
379 LCK_ATTR_DECLARE(pmap_lck_rw_attr
, 0, LCK_ATTR_DEBUG
);
382 * Bootstrap the system enough to run with virtual memory.
383 * Map the kernel's code and data, and allocate the system page table.
384 * Called with mapping OFF. Page_size must already be set.
389 __unused vm_offset_t load_start
,
390 __unused boolean_t IA32e
)
394 vm_last_addr
= VM_MAX_KERNEL_ADDRESS
; /* Set the highest address
397 * The kernel's pmap is statically allocated so we don't
398 * have to use pmap_create, which is unlikely to work
399 * correctly at this part of the boot sequence.
402 kernel_pmap
= &kernel_pmap_store
;
403 os_ref_init(&kernel_pmap
->ref_count
, NULL
);
404 #if DEVELOPMENT || DEBUG
405 kernel_pmap
->nx_enabled
= TRUE
;
407 kernel_pmap
->pm_task_map
= TASK_MAP_64BIT
;
408 kernel_pmap
->pm_obj
= (vm_object_t
) NULL
;
409 kernel_pmap
->pm_pml4
= IdlePML4
;
410 kernel_pmap
->pm_upml4
= IdlePML4
;
411 kernel_pmap
->pm_cr3
= (uintptr_t)ID_MAP_VTOP(IdlePML4
);
412 kernel_pmap
->pm_ucr3
= (uintptr_t)ID_MAP_VTOP(IdlePML4
);
413 kernel_pmap
->pm_eptp
= 0;
415 pmap_pcid_initialize_kernel(kernel_pmap
);
417 current_cpu_datap()->cpu_kernel_cr3
= cpu_shadowp(cpu_number())->cpu_kernel_cr3
= (addr64_t
) kernel_pmap
->pm_cr3
;
420 OSAddAtomic(NKPT
, &inuse_ptepages_count
);
421 OSAddAtomic64(NKPT
, &alloc_ptepages_count
);
422 bootstrap_wired_pages
= NKPT
;
424 virtual_avail
= (vm_offset_t
)(VM_MIN_KERNEL_ADDRESS
) + (vm_offset_t
)first_avail
;
425 virtual_end
= (vm_offset_t
)(VM_MAX_KERNEL_ADDRESS
);
427 if (!PE_parse_boot_argn("npvhash", &npvhashmask
, sizeof(npvhashmask
))) {
428 npvhashmask
= ((NPVHASHBUCKETS
) << pmap_scale_shift()) - 1;
431 npvhashbuckets
= npvhashmask
+ 1;
433 if (0 != ((npvhashbuckets
) & npvhashmask
)) {
434 panic("invalid hash %d, must be ((2^N)-1), "
435 "using default %d\n", npvhashmask
, NPVHASHMASK
);
438 lck_rw_init(&kernel_pmap
->pmap_rwl
, &pmap_lck_grp
, &pmap_lck_rw_attr
);
439 kernel_pmap
->pmap_rwl
.lck_rw_can_sleep
= FALSE
;
443 if (pmap_pcid_ncpus
) {
444 printf("PMAP: PCID enabled\n");
447 if (pmap_smep_enabled
) {
448 printf("PMAP: Supervisor Mode Execute Protection enabled\n");
450 if (pmap_smap_enabled
) {
451 printf("PMAP: Supervisor Mode Access Protection enabled\n");
455 printf("Stack canary: 0x%lx\n", __stack_chk_guard
[0]);
456 printf("early_random(): 0x%qx\n", early_random());
458 #if DEVELOPMENT || DEBUG
460 /* Check if the user has requested disabling stack or heap no-execute
461 * enforcement. These are "const" variables; that qualifier is cast away
462 * when altering them. The TEXT/DATA const sections are marked
463 * write protected later in the kernel startup sequence, so altering
464 * them is possible at this point, in pmap_bootstrap().
466 if (PE_parse_boot_argn("-pmap_disable_kheap_nx", &ptmp
, sizeof(ptmp
))) {
467 boolean_t
*pdknxp
= (boolean_t
*) &pmap_disable_kheap_nx
;
471 if (PE_parse_boot_argn("-pmap_disable_kstack_nx", &ptmp
, sizeof(ptmp
))) {
472 boolean_t
*pdknhp
= (boolean_t
*) &pmap_disable_kstack_nx
;
475 #endif /* DEVELOPMENT || DEBUG */
477 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
478 if (args
->efiMode
== kBootArgsEfiMode32
) {
479 printf("EFI32: kernel virtual space limited to 4GB\n");
480 virtual_end
= VM_MAX_KERNEL_ADDRESS_EFI32
;
482 kprintf("Kernel virtual space from 0x%lx to 0x%lx.\n",
483 (long)KERNEL_BASE
, (long)virtual_end
);
484 kprintf("Available physical space from 0x%llx to 0x%llx\n",
485 avail_start
, avail_end
);
488 * The -no_shared_cr3 boot-arg is a debugging feature (set by default
489 * in the DEBUG kernel) to force the kernel to switch to its own map
490 * (and cr3) when control is in kernelspace. The kernel's map does not
491 * include (i.e. share) userspace so wild references will cause
492 * a panic. Only copyin and copyout are exempt from this.
494 (void) PE_parse_boot_argn("-no_shared_cr3",
495 &no_shared_cr3
, sizeof(no_shared_cr3
));
497 kprintf("Kernel not sharing user map\n");
501 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace
, sizeof(pmap_trace
))) {
502 kprintf("Kernel traces for pmap operations enabled\n");
504 #endif /* PMAP_TRACES */
507 PE_parse_boot_argn("pmap_asserts", &pmap_asserts_enabled
, sizeof(pmap_asserts_enabled
));
508 PE_parse_boot_argn("pmap_stats_assert",
510 sizeof(pmap_stats_assert
));
511 #endif /* MACH_ASSERT */
519 *startp
= virtual_avail
;
528 #include <IOKit/IOHibernatePrivate.h>
529 #include <machine/pal_hibernate.h>
532 int32_t pmap_teardown_last_valid_compact_indx
= -1;
534 void pmap_pack_index(uint32_t);
535 int32_t pmap_unpack_index(pv_rooted_entry_t
);
538 pmap_unpack_index(pv_rooted_entry_t pv_h
)
542 indx
= (int32_t)(*((uint64_t *)(&pv_h
->qlink
.next
)) >> 48);
544 indx
|= (int32_t)(*((uint64_t *)(&pv_h
->qlink
.prev
)) >> 48);
546 *((uint64_t *)(&pv_h
->qlink
.next
)) |= ((uint64_t)0xffff << 48);
547 *((uint64_t *)(&pv_h
->qlink
.prev
)) |= ((uint64_t)0xffff << 48);
554 pmap_pack_index(uint32_t indx
)
556 pv_rooted_entry_t pv_h
;
558 pv_h
= &pv_head_table
[indx
];
560 *((uint64_t *)(&pv_h
->qlink
.next
)) &= ~((uint64_t)0xffff << 48);
561 *((uint64_t *)(&pv_h
->qlink
.prev
)) &= ~((uint64_t)0xffff << 48);
563 *((uint64_t *)(&pv_h
->qlink
.next
)) |= ((uint64_t)(indx
>> 16)) << 48;
564 *((uint64_t *)(&pv_h
->qlink
.prev
)) |= ((uint64_t)(indx
& 0xffff)) << 48;
569 pal_hib_teardown_pmap_structs(addr64_t
*unneeded_start
, addr64_t
*unneeded_end
)
572 int32_t compact_target_indx
;
574 compact_target_indx
= 0;
576 for (i
= 0; i
< pmap_npages
; i
++) {
577 if (pv_head_table
[i
].pmap
== PMAP_NULL
) {
578 if (pv_head_table
[compact_target_indx
].pmap
!= PMAP_NULL
) {
579 compact_target_indx
= i
;
582 pmap_pack_index((uint32_t)i
);
584 if (pv_head_table
[compact_target_indx
].pmap
== PMAP_NULL
) {
586 * we've got a hole to fill, so
587 * move this pv_rooted_entry_t to it's new home
589 pv_head_table
[compact_target_indx
] = pv_head_table
[i
];
590 pv_head_table
[i
].pmap
= PMAP_NULL
;
592 pmap_teardown_last_valid_compact_indx
= compact_target_indx
;
593 compact_target_indx
++;
595 pmap_teardown_last_valid_compact_indx
= i
;
599 *unneeded_start
= (addr64_t
)&pv_head_table
[pmap_teardown_last_valid_compact_indx
+ 1];
600 *unneeded_end
= (addr64_t
)&pv_head_table
[pmap_npages
- 1];
602 HIBLOG("pal_hib_teardown_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx
);
607 pal_hib_rebuild_pmap_structs(void)
609 int32_t cindx
, eindx
, rindx
= 0;
610 pv_rooted_entry_t pv_h
;
612 eindx
= (int32_t)pmap_npages
;
614 for (cindx
= pmap_teardown_last_valid_compact_indx
; cindx
>= 0; cindx
--) {
615 pv_h
= &pv_head_table
[cindx
];
617 rindx
= pmap_unpack_index(pv_h
);
618 assert(rindx
< pmap_npages
);
620 if (rindx
!= cindx
) {
622 * this pv_rooted_entry_t was moved by pal_hib_teardown_pmap_structs,
623 * so move it back to its real location
625 pv_head_table
[rindx
] = pv_head_table
[cindx
];
627 if (rindx
+ 1 != eindx
) {
629 * the 'hole' between this vm_rooted_entry_t and the previous
630 * vm_rooted_entry_t we moved needs to be initialized as
631 * a range of zero'd vm_rooted_entry_t's
633 bzero((char *)&pv_head_table
[rindx
+ 1], (eindx
- rindx
- 1) * sizeof(struct pv_rooted_entry
));
638 bzero((char *)&pv_head_table
[0], rindx
* sizeof(struct pv_rooted_entry
));
641 HIBLOG("pal_hib_rebuild_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx
);
647 * Create pv entries for kernel pages mapped by early startup code.
648 * These have to exist so we can ml_static_mfree() them later.
651 pmap_pv_fixup(vm_offset_t start_va
, vm_offset_t end_va
)
654 pv_rooted_entry_t pv_h
;
657 start_va
= round_page(start_va
);
658 end_va
= trunc_page(end_va
);
659 while (start_va
< end_va
) {
661 ppn
= pmap_find_phys(kernel_pmap
, start_va
);
662 if (ppn
!= 0 && IS_MANAGED_PAGE(ppn
)) {
663 pv_h
= pai_to_pvh(ppn
);
664 assert(pv_h
->qlink
.next
== 0); /* shouldn't be init'd yet */
665 assert(pv_h
->pmap
== 0);
666 pv_h
->va_and_flags
= start_va
;
667 pv_h
->pmap
= kernel_pmap
;
668 queue_init(&pv_h
->qlink
);
669 if (pmap_query_pagesize(kernel_pmap
, start_va
) == I386_LPGBYTES
) {
670 pgsz
= I386_LPGBYTES
;
678 * Initialize the pmap module.
679 * Called by vm_init, to initialize any structures that the pmap
680 * system needs to map virtual memory.
688 vm_map_offset_t vaddr
;
692 kernel_pmap
->pm_obj_pml4
= &kpml4obj_object_store
;
693 _vm_object_allocate((vm_object_size_t
)NPML4PGS
* PAGE_SIZE
, &kpml4obj_object_store
);
695 kernel_pmap
->pm_obj_pdpt
= &kpdptobj_object_store
;
696 _vm_object_allocate((vm_object_size_t
)NPDPTPGS
* PAGE_SIZE
, &kpdptobj_object_store
);
698 kernel_pmap
->pm_obj
= &kptobj_object_store
;
699 _vm_object_allocate((vm_object_size_t
)NPDEPGS
* PAGE_SIZE
, &kptobj_object_store
);
702 * Allocate memory for the pv_head_table and its lock bits,
703 * the modify bit array, and the pte_page table.
707 * zero bias all these arrays now instead of off avail_start
708 * so we cover all memory
711 npages
= i386_btop(avail_end
);
713 pmap_npages
= (uint32_t)npages
;
715 s
= (vm_size_t
) (sizeof(struct pv_rooted_entry
) * npages
716 + (sizeof(struct pv_hashed_entry_t
*) * (npvhashbuckets
))
717 + pv_lock_table_size(npages
)
718 + pv_hash_lock_table_size((npvhashbuckets
))
721 if (kernel_memory_allocate(kernel_map
, &addr
, s
, 0,
722 KMA_KOBJECT
| KMA_PERMANENT
, VM_KERN_MEMORY_PMAP
)
727 memset((char *)addr
, 0, s
);
733 if (0 == npvhashmask
) {
734 panic("npvhashmask not initialized");
739 * Allocate the structures first to preserve word-alignment.
741 pv_head_table
= (pv_rooted_entry_t
) addr
;
742 addr
= (vm_offset_t
) (pv_head_table
+ npages
);
744 pv_hash_table
= (pv_hashed_entry_t
*)addr
;
745 addr
= (vm_offset_t
) (pv_hash_table
+ (npvhashbuckets
));
747 pv_lock_table
= (char *) addr
;
748 addr
= (vm_offset_t
) (pv_lock_table
+ pv_lock_table_size(npages
));
750 pv_hash_lock_table
= (char *) addr
;
751 addr
= (vm_offset_t
) (pv_hash_lock_table
+ pv_hash_lock_table_size((npvhashbuckets
)));
753 pmap_phys_attributes
= (char *) addr
;
755 ppnum_t last_pn
= i386_btop(avail_end
);
757 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
758 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
759 if (pmptr
->type
!= kEfiConventionalMemory
) {
763 for (pn
= pmptr
->base
; pn
<= pmptr
->end
; pn
++) {
765 pmap_phys_attributes
[pn
] |= PHYS_MANAGED
;
767 if (pn
> last_managed_page
) {
768 last_managed_page
= pn
;
771 if ((pmap_high_used_bottom
<= pn
&& pn
<= pmap_high_used_top
) ||
772 (pmap_middle_used_bottom
<= pn
&& pn
<= pmap_middle_used_top
)) {
773 pmap_phys_attributes
[pn
] |= PHYS_NOENCRYPT
;
779 ppn
= pmap_find_phys(kernel_pmap
, vaddr
);
781 pmap_phys_attributes
[ppn
] |= PHYS_NOENCRYPT
;
787 * Create the zone of physical maps,
788 * and of the physical-to-virtual entries.
790 pmap_zone
= zone_create_ext("pmap", sizeof(struct pmap
),
791 ZC_NOENCRYPT
| ZC_ZFREE_CLEARMEM
, ZONE_ID_PMAP
, NULL
);
793 /* The anchor is required to be page aligned. Zone debugging adds
794 * padding which may violate that requirement. Tell the zone
795 * subsystem that alignment is required.
797 pmap_anchor_zone
= zone_create("pagetable anchors", PAGE_SIZE
,
798 ZC_NOENCRYPT
| ZC_ALIGNMENT_REQUIRED
);
800 /* TODO: possible general optimisation...pre-allocate via zones commonly created
801 * level3/2 pagetables
803 /* The anchor is required to be page aligned. Zone debugging adds
804 * padding which may violate that requirement. Tell the zone
805 * subsystem that alignment is required.
807 pmap_uanchor_zone
= zone_create("pagetable user anchors", PAGE_SIZE
,
808 ZC_NOENCRYPT
| ZC_ALIGNMENT_REQUIRED
);
810 pv_hashed_list_zone
= zone_create("pv_list", sizeof(struct pv_hashed_entry
),
811 ZC_NOENCRYPT
| ZC_ALIGNMENT_REQUIRED
);
814 * Create pv entries for kernel pages that might get pmap_remove()ed.
816 * - very low pages that were identity mapped.
817 * - vm_pages[] entries that might be unused and reclaimed.
819 assert((uintptr_t)VM_MIN_KERNEL_ADDRESS
+ avail_start
<= (uintptr_t)vm_page_array_beginning_addr
);
820 pmap_pv_fixup((uintptr_t)VM_MIN_KERNEL_ADDRESS
, (uintptr_t)VM_MIN_KERNEL_ADDRESS
+ avail_start
);
821 pmap_pv_fixup((uintptr_t)vm_page_array_beginning_addr
, (uintptr_t)vm_page_array_ending_addr
);
823 pmap_initialized
= TRUE
;
825 max_preemption_latency_tsc
= tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS
, tscFCvtn2t
);
828 * Ensure the kernel's PML4 entry exists for the basement
829 * before this is shared with any user.
831 pmap_expand_pml4(kernel_pmap
, KERNEL_BASEMENT
, PMAP_EXPAND_OPTIONS_NONE
);
834 pmap_ept_support_ad
= vmx_hv_support() && (VMX_CAP(MSR_IA32_VMX_EPT_VPID_CAP
, MSR_IA32_VMX_EPT_VPID_CAP_AD_SHIFT
, 1) ? TRUE
: FALSE
);
835 pmap_eptp_flags
= HV_VMX_EPTP_MEMORY_TYPE_WB
| HV_VMX_EPTP_WALK_LENGTH(4) | (pmap_ept_support_ad
? HV_VMX_EPTP_ENABLE_AD_FLAGS
: 0);
836 #endif /* CONFIG_VMX */
840 pmap_mark_range(pmap_t npmap
, uint64_t sv
, uint64_t nxrosz
, boolean_t NX
, boolean_t ro
)
842 uint64_t ev
= sv
+ nxrosz
, cv
= sv
;
844 pt_entry_t
*ptep
= NULL
;
846 /* XXX what if nxrosz is 0? we end up marking the page whose address is passed in via sv -- is that kosher? */
847 assert(!is_ept_pmap(npmap
));
849 assert(((sv
& 0xFFFULL
) | (nxrosz
& 0xFFFULL
)) == 0);
851 for (pdep
= pmap_pde(npmap
, cv
); pdep
!= NULL
&& (cv
< ev
);) {
852 uint64_t pdev
= (cv
& ~((uint64_t)PDEMASK
));
854 if (*pdep
& INTEL_PTE_PS
) {
856 if ((NX
^ !!(*pdep
& INTEL_PTE_NX
)) || (ro
^ !!!(*pdep
& INTEL_PTE_WRITE
))) {
857 kprintf("WARNING: Remapping PDE for %p from %s%s%s to %s%s%s\n", (void *)cv
,
858 (*pdep
& INTEL_PTE_VALID
) ? "R" : "",
859 (*pdep
& INTEL_PTE_WRITE
) ? "W" : "",
860 (*pdep
& INTEL_PTE_NX
) ? "" : "X",
868 *pdep
|= INTEL_PTE_NX
;
870 *pdep
&= ~INTEL_PTE_NX
;
873 *pdep
&= ~INTEL_PTE_WRITE
;
875 *pdep
|= INTEL_PTE_WRITE
;
878 cv
&= ~((uint64_t) PDEMASK
);
879 pdep
= pmap_pde(npmap
, cv
);
883 for (ptep
= pmap_pte(npmap
, cv
); ptep
!= NULL
&& (cv
< (pdev
+ NBPD
)) && (cv
< ev
);) {
885 if ((NX
^ !!(*ptep
& INTEL_PTE_NX
)) || (ro
^ !!!(*ptep
& INTEL_PTE_WRITE
))) {
886 kprintf("WARNING: Remapping PTE for %p from %s%s%s to %s%s%s\n", (void *)cv
,
887 (*ptep
& INTEL_PTE_VALID
) ? "R" : "",
888 (*ptep
& INTEL_PTE_WRITE
) ? "W" : "",
889 (*ptep
& INTEL_PTE_NX
) ? "" : "X",
896 *ptep
|= INTEL_PTE_NX
;
898 *ptep
&= ~INTEL_PTE_NX
;
901 *ptep
&= ~INTEL_PTE_WRITE
;
903 *ptep
|= INTEL_PTE_WRITE
;
906 ptep
= pmap_pte(npmap
, cv
);
909 DPRINTF("%s(0x%llx, 0x%llx, %u, %u): 0x%llx, 0x%llx\n", __FUNCTION__
, sv
, nxrosz
, NX
, ro
, cv
, ptep
? *ptep
: 0);
913 * Reclaim memory for early boot 4K page tables that were converted to large page mappings.
914 * We know this memory is part of the KPTphys[] array that was allocated in Idle_PTs_init(),
915 * so we can free it using its address in that array.
918 pmap_free_early_PT(ppnum_t ppn
, uint32_t cnt
)
923 KPTphys_ppn
= pmap_find_phys(kernel_pmap
, (uintptr_t)KPTphys
);
924 assert(ppn
>= KPTphys_ppn
);
925 assert(ppn
+ cnt
<= KPTphys_ppn
+ NKPT
);
926 offset
= (ppn
- KPTphys_ppn
) << PAGE_SHIFT
;
927 ml_static_mfree((uintptr_t)KPTphys
+ offset
, PAGE_SIZE
* cnt
);
931 * Called once VM is fully initialized so that we can release unused
932 * sections of low memory to the general pool.
933 * Also complete the set-up of identity-mapped sections of the kernel:
934 * 1) write-protect kernel text
935 * 2) map kernel text using large pages if possible
936 * 3) read and write-protect page zero (for K32)
937 * 4) map the global page at the appropriate virtual address.
941 * To effectively map and write-protect all kernel text pages, the text
942 * must be 2M-aligned at the base, and the data section above must also be
943 * 2M-aligned. That is, there's padding below and above. This is achieved
944 * through linker directives. Large pages are used only if this alignment
945 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
950 * sdata: ================== 2Meg
954 * etext: ------------------
962 * stext: ================== 2Meg
966 * eHIB: ------------------
970 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
971 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
972 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
973 * The now unused level-1 PTE pages are also freed.
975 extern ppnum_t vm_kernel_base_page
;
976 static uint32_t dataptes
= 0;
979 pmap_lowmem_finalize(void)
985 * Update wired memory statistics for early boot pages
987 PMAP_ZINFO_PALLOC(kernel_pmap
, bootstrap_wired_pages
* PAGE_SIZE
);
990 * Free pages in pmap regions below the base:
992 * We can't free all the pages to VM that EFI reports available.
993 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
994 * There's also a size miscalculation here: pend is one page less
995 * than it should be but this is not fixed to be backwards
997 * This is important for KASLR because up to 256*2MB = 512MB of space
998 * needs has to be released to VM.
1001 pmap_memory_regions
[i
].end
< vm_kernel_base_page
;
1003 vm_offset_t pbase
= i386_ptob(pmap_memory_regions
[i
].base
);
1004 vm_offset_t pend
= i386_ptob(pmap_memory_regions
[i
].end
+ 1);
1006 DBG("pmap region %d [%p..[%p\n",
1007 i
, (void *) pbase
, (void *) pend
);
1009 if (pmap_memory_regions
[i
].attribute
& EFI_MEMORY_KERN_RESERVED
) {
1014 * Adjust limits not to free pages in range 0xc0000-0xff000.
1016 if (pbase
>= 0xc0000 && pend
<= 0x100000) {
1019 if (pbase
< 0xc0000 && pend
> 0x100000) {
1020 /* page range entirely within region, free lower part */
1021 DBG("- ml_static_mfree(%p,%p)\n",
1022 (void *) ml_static_ptovirt(pbase
),
1023 (void *) (0xc0000 - pbase
));
1024 ml_static_mfree(ml_static_ptovirt(pbase
), 0xc0000 - pbase
);
1027 if (pbase
< 0xc0000) {
1028 pend
= MIN(pend
, 0xc0000);
1030 if (pend
> 0x100000) {
1031 pbase
= MAX(pbase
, 0x100000);
1033 DBG("- ml_static_mfree(%p,%p)\n",
1034 (void *) ml_static_ptovirt(pbase
),
1035 (void *) (pend
- pbase
));
1036 ml_static_mfree(ml_static_ptovirt(pbase
), pend
- pbase
);
1039 /* A final pass to get rid of all initial identity mappings to
1042 DPRINTF("%s: Removing mappings from 0->0x%lx\n", __FUNCTION__
, vm_kernel_base
);
1045 * Remove all mappings past the boot-cpu descriptor aliases and low globals.
1046 * Non-boot-cpu GDT aliases will be remapped later as needed.
1048 pmap_remove(kernel_pmap
, LOWGLOBAL_ALIAS
+ PAGE_SIZE
, vm_kernel_base
);
1051 * Release any memory for early boot 4K page table pages that got replaced
1052 * with large page mappings for vm_pages[]. We know this memory is part of
1053 * the KPTphys[] array that was allocated in Idle_PTs_init(), so we can free
1054 * it using that address.
1056 pmap_free_early_PT(released_PT_ppn
, released_PT_cnt
);
1059 * If text and data are both 2MB-aligned,
1060 * we can map text with large-pages,
1061 * unless the -kernel_text_ps_4K boot-arg overrides.
1063 if ((stext
& I386_LPGMASK
) == 0 && (sdata
& I386_LPGMASK
) == 0) {
1064 kprintf("Kernel text is 2MB aligned");
1065 kernel_text_ps_4K
= FALSE
;
1066 if (PE_parse_boot_argn("-kernel_text_ps_4K",
1068 sizeof(kernel_text_ps_4K
))) {
1069 kprintf(" but will be mapped with 4K pages\n");
1071 kprintf(" and will be mapped with 2M pages\n");
1074 #if DEVELOPMENT || DEBUG
1075 (void) PE_parse_boot_argn("wpkernel", &wpkernel
, sizeof(wpkernel
));
1078 kprintf("Kernel text %p-%p to be write-protected\n",
1079 (void *) stext
, (void *) etext
);
1085 * Scan over text if mappings are to be changed:
1086 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
1087 * - Change to large-pages if possible and not overriden.
1089 if (kernel_text_ps_4K
&& wpkernel
) {
1091 for (myva
= stext
; myva
< etext
; myva
+= PAGE_SIZE
) {
1094 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
1096 pmap_store_pte(ptep
, *ptep
& ~INTEL_PTE_WRITE
);
1101 if (!kernel_text_ps_4K
) {
1105 * Release zero-filled page padding used for 2M-alignment.
1107 DBG("ml_static_mfree(%p,%p) for padding below text\n",
1108 (void *) eHIB
, (void *) (stext
- eHIB
));
1109 ml_static_mfree(eHIB
, stext
- eHIB
);
1110 DBG("ml_static_mfree(%p,%p) for padding above text\n",
1111 (void *) etext
, (void *) (sdata
- etext
));
1112 ml_static_mfree(etext
, sdata
- etext
);
1115 * Coalesce text pages into large pages.
1117 for (myva
= stext
; myva
< sdata
; myva
+= I386_LPGBYTES
) {
1119 vm_offset_t pte_phys
;
1124 pdep
= pmap_pde(kernel_pmap
, (vm_map_offset_t
)myva
);
1125 KPT_ppn
= (ppnum_t
)((*pdep
& PG_FRAME
) >> PAGE_SHIFT
);
1126 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
1127 DBG("myva: %p pdep: %p ptep: %p\n",
1128 (void *) myva
, (void *) pdep
, (void *) ptep
);
1129 if ((*ptep
& INTEL_PTE_VALID
) == 0) {
1132 pte_phys
= (vm_offset_t
)(*ptep
& PG_FRAME
);
1133 pde
= *pdep
& PTMASK
; /* page attributes from pde */
1134 pde
|= INTEL_PTE_PS
; /* make it a 2M entry */
1135 pde
|= pte_phys
; /* take page frame from pte */
1138 pde
&= ~INTEL_PTE_WRITE
;
1140 DBG("pmap_store_pte(%p,0x%llx)\n",
1142 pmap_store_pte(pdep
, pde
);
1145 * Free the now-unused level-1 pte.
1147 pmap_free_early_PT(KPT_ppn
, 1);
1150 /* Change variable read by sysctl machdep.pmap */
1151 pmap_kernel_text_ps
= I386_LPGBYTES
;
1156 for (dva
= sdata
; dva
< edata
; dva
+= I386_PGBYTES
) {
1157 assert(((sdata
| edata
) & PAGE_MASK
) == 0);
1158 pt_entry_t dpte
, *dptep
= pmap_pte(kernel_pmap
, dva
);
1161 assert((dpte
& INTEL_PTE_VALID
));
1162 dpte
|= INTEL_PTE_NX
;
1163 pmap_store_pte(dptep
, dpte
);
1166 assert(dataptes
> 0);
1168 kernel_segment_command_t
* seg
;
1169 kernel_section_t
* sec
;
1170 kc_format_t kc_format
;
1172 PE_get_primary_kc_format(&kc_format
);
1174 for (seg
= firstseg(); seg
!= NULL
; seg
= nextsegfromheader(&_mh_execute_header
, seg
)) {
1175 if (!strcmp(seg
->segname
, "__TEXT") ||
1176 !strcmp(seg
->segname
, "__DATA")) {
1180 /* XXX: FIXME_IN_dyld: This is a workaround (see below) */
1181 if (kc_format
!= KCFormatFileset
) {
1183 if (!strcmp(seg
->segname
, "__KLD")) {
1188 if (!strcmp(seg
->segname
, "__HIB")) {
1189 for (sec
= firstsect(seg
); sec
!= NULL
; sec
= nextsect(seg
, sec
)) {
1190 if (sec
->addr
& PAGE_MASK
) {
1191 panic("__HIB segment's sections misaligned");
1193 if (!strcmp(sec
->sectname
, "__text")) {
1194 pmap_mark_range(kernel_pmap
, sec
->addr
, round_page(sec
->size
), FALSE
, TRUE
);
1196 pmap_mark_range(kernel_pmap
, sec
->addr
, round_page(sec
->size
), TRUE
, FALSE
);
1200 if (kc_format
== KCFormatFileset
) {
1203 * This block of code is commented out because it may or may not have induced an earlier panic
1208 boolean_t NXbit
= !(seg
->initprot
& VM_PROT_EXECUTE
),
1209 robit
= (seg
->initprot
& (VM_PROT_READ
| VM_PROT_WRITE
)) == VM_PROT_READ
;
1212 * XXX: FIXME_IN_dyld: This is a workaround for primary KC containing incorrect inaccurate
1213 * initprot for segments containing code.
1215 if (!strcmp(seg
->segname
, "__KLD") || !strcmp(seg
->segname
, "__VECTORS")) {
1220 pmap_mark_range(kernel_pmap
, seg
->vmaddr
& ~(uint64_t)PAGE_MASK
,
1221 round_page_64(seg
->vmsize
), NXbit
, robit
);
1225 * XXX: We are marking *every* segment with rwx permissions as a workaround
1226 * XXX: until the primary KC's kernel segments are page-aligned.
1228 kprintf("Marking (%p, %p) as rwx\n", (void *)(seg
->vmaddr
& ~(uint64_t)PAGE_MASK
),
1229 (void *)((seg
->vmaddr
& ~(uint64_t)PAGE_MASK
) + round_page_64(seg
->vmsize
)));
1230 pmap_mark_range(kernel_pmap
, seg
->vmaddr
& ~(uint64_t)PAGE_MASK
,
1231 round_page_64(seg
->vmsize
), FALSE
, FALSE
);
1233 pmap_mark_range(kernel_pmap
, seg
->vmaddr
, round_page_64(seg
->vmsize
), TRUE
, FALSE
);
1239 * If we're debugging, map the low global vector page at the fixed
1240 * virtual address. Otherwise, remove the mapping for this.
1242 if (debug_boot_arg
) {
1243 pt_entry_t
*pte
= NULL
;
1244 if (0 == (pte
= pmap_pte(kernel_pmap
, LOWGLOBAL_ALIAS
))) {
1245 panic("lowmem pte");
1247 /* make sure it is defined on page boundary */
1248 assert(0 == ((vm_offset_t
) &lowGlo
& PAGE_MASK
));
1249 pmap_store_pte(pte
, kvtophys((vm_offset_t
)&lowGlo
)
1257 pmap_remove(kernel_pmap
,
1258 LOWGLOBAL_ALIAS
, LOWGLOBAL_ALIAS
+ PAGE_SIZE
);
1260 pmap_tlbi_range(0, ~0ULL, true, 0);
1265 * Mark the const data segment as read-only, non-executable.
1268 x86_64_protect_data_const()
1270 boolean_t doconstro
= TRUE
;
1271 #if DEVELOPMENT || DEBUG
1272 (void) PE_parse_boot_argn("dataconstro", &doconstro
, sizeof(doconstro
));
1275 if (sconst
& PAGE_MASK
) {
1276 panic("CONST segment misaligned 0x%lx 0x%lx\n",
1279 kprintf("Marking const DATA read-only\n");
1280 pmap_protect(kernel_pmap
, sconst
, econst
, VM_PROT_READ
);
1284 * this function is only used for debugging fron the vm layer
1290 pv_rooted_entry_t pv_h
;
1294 assert(pn
!= vm_page_fictitious_addr
);
1296 if (!pmap_initialized
) {
1300 if (pn
== vm_page_guard_addr
) {
1304 pai
= ppn_to_pai(pn
);
1305 if (!IS_MANAGED_PAGE(pai
)) {
1308 pv_h
= pai_to_pvh(pn
);
1309 result
= (pv_h
->pmap
== PMAP_NULL
);
1315 pmap_assert_free(ppnum_t pn
)
1318 pv_rooted_entry_t pv_h
= NULL
;
1321 static char buffer
[32];
1322 static char *pr_name
= "not managed pn";
1325 pt_entry_t pte
= -1ull;
1327 if (pmap_verify_free(pn
)) {
1331 if (pn
> last_managed_page
) {
1336 pai
= ppn_to_pai(pn
);
1337 attr
= pmap_phys_attributes
[pai
];
1338 pv_h
= pai_to_pvh(pai
);
1339 va
= pv_h
->va_and_flags
;
1341 if (pmap
== kernel_pmap
) {
1343 } else if (pmap
== NULL
) {
1344 pr_name
= "pmap NULL";
1345 } else if (pmap
->pmap_procname
[0] != 0) {
1346 pr_name
= &pmap
->pmap_procname
[0];
1348 snprintf(buffer
, sizeof(buffer
), "pmap %p", pv_h
->pmap
);
1353 ptep
= pmap_pte(pmap
, va
);
1355 pte
= (uintptr_t)*ptep
;
1360 panic("page not FREE page: 0x%lx attr: 0x%x %s va: 0x%lx PTE: 0x%llx",
1361 (ulong_t
)pn
, attr
, pr_name
, va
, pte
);
1363 #endif /* MACH_ASSERT */
1368 vm_map_offset_t va_start
,
1369 vm_map_offset_t va_end
)
1371 vm_map_offset_t offset
;
1374 if (pmap
== PMAP_NULL
) {
1379 * Check the resident page count
1380 * - if it's zero, the pmap is completely empty.
1381 * This short-circuit test prevents a virtual address scan which is
1382 * painfully slow for 64-bit spaces.
1383 * This assumes the count is correct
1384 * .. the debug kernel ought to be checking perhaps by page table walk.
1386 if (pmap
->stats
.resident_count
== 0) {
1390 for (offset
= va_start
;
1392 offset
+= PAGE_SIZE_64
) {
1393 phys_page
= pmap_find_phys(pmap
, offset
);
1395 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
1396 "page %d at 0x%llx\n",
1397 pmap
, va_start
, va_end
, phys_page
, offset
);
1406 hv_ept_pmap_create(void **ept_pmap
, void **eptp
)
1410 if ((ept_pmap
== NULL
) || (eptp
== NULL
)) {
1414 p
= pmap_create_options(get_task_ledger(current_task()), 0, (PMAP_CREATE_64BIT
| PMAP_CREATE_EPT
));
1415 if (p
== PMAP_NULL
) {
1421 assert(is_ept_pmap(p
));
1423 *ept_pmap
= (void*)p
;
1424 *eptp
= (void*)(p
->pm_eptp
);
1429 * pmap_create() is used by some special, legacy 3rd party kexts.
1430 * In our kernel code, always use pmap_create_options().
1432 extern pmap_t
pmap_create(ledger_t ledger
, vm_map_size_t sz
, boolean_t is_64bit
);
1434 __attribute__((used
))
1441 return pmap_create_options(ledger
, sz
, is_64bit
? PMAP_CREATE_64BIT
: 0);
1445 * Create and return a physical map.
1447 * If the size specified for the map
1448 * is zero, the map is an actual physical
1449 * map, and may be referenced by the
1452 * If the size specified is non-zero,
1453 * the map will be used in software only, and
1454 * is bounded by that size.
1458 pmap_create_options(
1466 pml4_entry_t
*kpml4
;
1469 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_START
, sz
, flags
);
1471 size
= (vm_size_t
) sz
;
1474 * A software use-only map doesn't even need a map.
1482 * Return error when unrecognized flags are passed.
1484 if (__improbable((flags
& ~(PMAP_CREATE_KNOWN_FLAGS
)) != 0)) {
1488 p
= (pmap_t
) zalloc(pmap_zone
);
1489 if (PMAP_NULL
== p
) {
1490 panic("pmap_create zalloc");
1493 /* Zero all fields */
1494 bzero(p
, sizeof(*p
));
1496 lck_rw_init(&p
->pmap_rwl
, &pmap_lck_grp
, &pmap_lck_rw_attr
);
1497 p
->pmap_rwl
.lck_rw_can_sleep
= FALSE
;
1499 bzero(&p
->stats
, sizeof(p
->stats
));
1500 os_ref_init(&p
->ref_count
, NULL
);
1501 #if DEVELOPMENT || DEBUG
1504 p
->pm_shared
= FALSE
;
1505 ledger_reference(ledger
);
1508 p
->pm_task_map
= ((flags
& PMAP_CREATE_64BIT
) ? TASK_MAP_64BIT
: TASK_MAP_32BIT
);
1510 p
->pagezero_accessible
= FALSE
;
1511 p
->pm_vm_map_cs_enforced
= FALSE
;
1513 if (pmap_pcid_ncpus
) {
1514 pmap_pcid_initialize(p
);
1517 p
->pm_pml4
= zalloc(pmap_anchor_zone
);
1518 p
->pm_upml4
= zalloc(pmap_uanchor_zone
); //cleanup for EPT
1520 pmap_assert((((uintptr_t)p
->pm_pml4
) & PAGE_MASK
) == 0);
1521 pmap_assert((((uintptr_t)p
->pm_upml4
) & PAGE_MASK
) == 0);
1523 memset((char *)p
->pm_pml4
, 0, PAGE_SIZE
);
1524 memset((char *)p
->pm_upml4
, 0, PAGE_SIZE
);
1526 if (flags
& PMAP_CREATE_EPT
) {
1527 p
->pm_eptp
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_pml4
) | pmap_eptp_flags
;
1531 p
->pm_cr3
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_pml4
);
1532 p
->pm_ucr3
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_upml4
);
1535 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
1537 p
->pm_obj_pml4
= vm_object_allocate((vm_object_size_t
)(NPML4PGS
) *PAGE_SIZE
);
1538 if (NULL
== p
->pm_obj_pml4
) {
1539 panic("pmap_create pdpt obj");
1542 p
->pm_obj_pdpt
= vm_object_allocate((vm_object_size_t
)(NPDPTPGS
) *PAGE_SIZE
);
1543 if (NULL
== p
->pm_obj_pdpt
) {
1544 panic("pmap_create pdpt obj");
1547 p
->pm_obj
= vm_object_allocate((vm_object_size_t
)(NPDEPGS
) *PAGE_SIZE
);
1548 if (NULL
== p
->pm_obj
) {
1549 panic("pmap_create pte obj");
1552 if (!(flags
& PMAP_CREATE_EPT
)) {
1553 /* All host pmaps share the kernel's pml4 */
1554 pml4
= pmap64_pml4(p
, 0ULL);
1555 kpml4
= kernel_pmap
->pm_pml4
;
1556 for (i
= KERNEL_PML4_INDEX
; i
< (KERNEL_PML4_INDEX
+ KERNEL_PML4_COUNT
); i
++) {
1559 pml4
[KERNEL_KEXTS_INDEX
] = kpml4
[KERNEL_KEXTS_INDEX
];
1560 for (i
= KERNEL_PHYSMAP_PML4_INDEX
; i
< (KERNEL_PHYSMAP_PML4_INDEX
+ KERNEL_PHYSMAP_PML4_COUNT
); i
++) {
1563 pml4
[KERNEL_DBLMAP_PML4_INDEX
] = kpml4
[KERNEL_DBLMAP_PML4_INDEX
];
1565 for (i
= KERNEL_KASAN_PML4_FIRST
; i
<= KERNEL_KASAN_PML4_LAST
; i
++) {
1569 pml4_entry_t
*pml4u
= pmap64_user_pml4(p
, 0ULL);
1570 pml4u
[KERNEL_DBLMAP_PML4_INDEX
] = kpml4
[KERNEL_DBLMAP_PML4_INDEX
];
1574 p
->pmap_stats_assert
= TRUE
;
1576 strlcpy(p
->pmap_procname
, "<nil>", sizeof(p
->pmap_procname
));
1577 #endif /* MACH_ASSERT */
1579 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_END
,
1580 VM_KERNEL_ADDRHIDE(p
));
1586 * We maintain stats and ledgers so that a task's physical footprint is:
1587 * phys_footprint = ((internal - alternate_accounting)
1588 * + (internal_compressed - alternate_accounting_compressed)
1590 * + purgeable_nonvolatile
1591 * + purgeable_nonvolatile_compressed
1593 * where "alternate_accounting" includes "iokit" and "purgeable" memory.
1597 static void pmap_check_ledgers(pmap_t pmap
);
1598 #else /* MACH_ASSERT */
1600 pmap_check_ledgers(__unused pmap_t pmap
)
1603 #endif /* MACH_ASSERT */
1606 * Retire the given physical map from service.
1607 * Should only be called if the map contains
1608 * no valid mappings.
1610 extern int vm_wired_objects_page_count
;
1613 pmap_destroy(pmap_t p
)
1617 if (p
== PMAP_NULL
) {
1621 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_START
,
1622 VM_KERNEL_ADDRHIDe(p
));
1624 PMAP_LOCK_EXCLUSIVE(p
);
1626 c
= os_ref_release_locked(&p
->ref_count
);
1628 pmap_assert((current_thread() && (current_thread()->map
)) ? (current_thread()->map
->pmap
!= p
) : TRUE
);
1632 * If some cpu is not using the physical pmap pointer that it
1633 * is supposed to be (see set_dirbase), we might be using the
1634 * pmap that is being destroyed! Make sure we are
1635 * physically on the right pmap:
1637 PMAP_UPDATE_TLBS(p
, 0x0ULL
, 0xFFFFFFFFFFFFF000ULL
);
1638 if (pmap_pcid_ncpus
) {
1639 pmap_destroy_pcid_sync(p
);
1643 PMAP_UNLOCK_EXCLUSIVE(p
);
1646 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
);
1647 pmap_assert(p
== kernel_pmap
);
1648 return; /* still in use */
1652 * Free the memory maps, then the
1655 int inuse_ptepages
= 0;
1657 zfree(pmap_anchor_zone
, p
->pm_pml4
);
1658 zfree(pmap_uanchor_zone
, p
->pm_upml4
);
1660 inuse_ptepages
+= p
->pm_obj_pml4
->resident_page_count
;
1661 vm_object_deallocate(p
->pm_obj_pml4
);
1663 inuse_ptepages
+= p
->pm_obj_pdpt
->resident_page_count
;
1664 vm_object_deallocate(p
->pm_obj_pdpt
);
1666 inuse_ptepages
+= p
->pm_obj
->resident_page_count
;
1667 vm_object_deallocate(p
->pm_obj
);
1669 OSAddAtomic(-inuse_ptepages
, &inuse_ptepages_count
);
1670 PMAP_ZINFO_PFREE(p
, inuse_ptepages
* PAGE_SIZE
);
1672 pmap_check_ledgers(p
);
1673 ledger_dereference(p
->ledger
);
1674 lck_rw_destroy(&p
->pmap_rwl
, &pmap_lck_grp
);
1675 zfree(pmap_zone
, p
);
1677 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
);
1681 * Add a reference to the specified pmap.
1685 pmap_reference(pmap_t p
)
1687 if (p
!= PMAP_NULL
) {
1688 PMAP_LOCK_EXCLUSIVE(p
);
1689 os_ref_retain_locked(&p
->ref_count
);
1690 PMAP_UNLOCK_EXCLUSIVE(p
);;
1695 * Remove phys addr if mapped in specified map
1699 pmap_remove_some_phys(
1700 __unused pmap_t map
,
1701 __unused ppnum_t pn
)
1703 /* Implement to support working set code */
1710 vm_map_offset_t sva
,
1711 vm_map_offset_t eva
,
1714 pmap_protect_options(map
, sva
, eva
, prot
, 0, NULL
);
1719 * Set the physical protection on the
1720 * specified range of this map as requested.
1722 * VERY IMPORTANT: Will *NOT* increase permissions.
1723 * pmap_protect_options() should protect the range against any access types
1724 * that are not in "prot" but it should never grant extra access.
1725 * For example, if "prot" is READ|EXECUTE, that means "remove write
1726 * access" but it does *not* mean "add read and execute" access.
1727 * VM relies on getting soft-faults to enforce extra checks (code
1728 * signing, for example), for example.
1729 * New access permissions are granted via pmap_enter() only.
1732 pmap_protect_options(
1734 vm_map_offset_t sva
,
1735 vm_map_offset_t eva
,
1737 unsigned int options
,
1741 pt_entry_t
*spte
, *epte
;
1742 vm_map_offset_t lva
;
1743 vm_map_offset_t orig_sva
;
1750 if (map
== PMAP_NULL
) {
1754 if (prot
== VM_PROT_NONE
) {
1755 pmap_remove_options(map
, sva
, eva
, options
);
1759 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_START
,
1760 VM_KERNEL_ADDRHIDE(map
), VM_KERNEL_ADDRHIDE(sva
),
1761 VM_KERNEL_ADDRHIDE(eva
));
1763 if (prot
& VM_PROT_EXECUTE
) {
1769 #if DEVELOPMENT || DEBUG
1770 if (__improbable(set_NX
&& (!nx_enabled
|| !map
->nx_enabled
))) {
1774 is_ept
= is_ept_pmap(map
);
1776 PMAP_LOCK_EXCLUSIVE(map
);
1780 lva
= (sva
+ PDE_MAPPED_SIZE
) & ~(PDE_MAPPED_SIZE
- 1);
1784 pde
= pmap_pde(map
, sva
);
1785 if (pde
&& (*pde
& PTE_VALID_MASK(is_ept
))) {
1786 if (*pde
& PTE_PS
) {
1789 epte
= spte
+ 1; /* excluded */
1791 spte
= pmap_pte(map
, (sva
& ~(PDE_MAPPED_SIZE
- 1)));
1792 spte
= &spte
[ptenum(sva
)];
1793 epte
= &spte
[intel_btop(lva
- sva
)];
1796 for (; spte
< epte
; spte
++) {
1797 if (!(*spte
& PTE_VALID_MASK(is_ept
))) {
1802 if (!(prot
& VM_PROT_READ
)) {
1803 pmap_update_pte(spte
, PTE_READ(is_ept
), 0);
1806 if (!(prot
& VM_PROT_WRITE
)) {
1807 pmap_update_pte(spte
, PTE_WRITE(is_ept
), 0);
1809 #if DEVELOPMENT || DEBUG
1810 else if ((options
& PMAP_OPTIONS_PROTECT_IMMEDIATE
) &&
1811 map
== kernel_pmap
) {
1812 pmap_update_pte(spte
, 0, PTE_WRITE(is_ept
));
1814 #endif /* DEVELOPMENT || DEBUG */
1818 pmap_update_pte(spte
, 0, INTEL_PTE_NX
);
1820 pmap_update_pte(spte
, INTEL_EPT_EX
, 0);
1829 if (options
& PMAP_OPTIONS_NOFLUSH
) {
1830 PMAP_UPDATE_TLBS_DELAYED(map
, orig_sva
, eva
, (pmap_flush_context
*)arg
);
1832 PMAP_UPDATE_TLBS(map
, orig_sva
, eva
);
1836 PMAP_UNLOCK_EXCLUSIVE(map
);
1838 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_END
);
1841 /* Map a (possibly) autogenned block */
1850 __unused
unsigned int flags
)
1853 addr64_t original_va
= va
;
1857 if (attr
& VM_MEM_SUPERPAGE
) {
1858 cur_page_size
= SUPERPAGE_SIZE
;
1860 cur_page_size
= PAGE_SIZE
;
1863 for (page
= 0; page
< size
; page
+= cur_page_size
/ PAGE_SIZE
) {
1864 kr
= pmap_enter(pmap
, va
, pa
, prot
, VM_PROT_NONE
, attr
, TRUE
);
1866 if (kr
!= KERN_SUCCESS
) {
1868 * This will panic for now, as it is unclear that
1869 * removing the mappings is correct.
1871 panic("%s: failed pmap_enter, "
1872 "pmap=%p, va=%#llx, pa=%u, size=%u, prot=%#x, flags=%#x",
1874 pmap
, va
, pa
, size
, prot
, flags
);
1876 pmap_remove(pmap
, original_va
, va
- original_va
);
1880 va
+= cur_page_size
;
1881 pa
+= cur_page_size
/ PAGE_SIZE
;
1884 return KERN_SUCCESS
;
1890 vm_map_offset_t vaddr
,
1891 unsigned int options
)
1897 pml4_entry_t
*pml4p
;
1898 boolean_t is_ept
= is_ept_pmap(map
);
1900 DBG("pmap_expand_pml4(%p,%p)\n", map
, (void *)vaddr
);
1902 /* With the exception of the kext "basement", the kernel's level 4
1903 * pagetables must not be dynamically expanded.
1905 assert(map
!= kernel_pmap
|| (vaddr
== KERNEL_BASEMENT
));
1907 * Allocate a VM page for the pml4 page
1909 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1910 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
) {
1911 return KERN_RESOURCE_SHORTAGE
;
1916 * put the page into the pmap's obj list so it
1917 * can be found later.
1919 pn
= VM_PAGE_GET_PHYS_PAGE(m
);
1921 i
= pml4idx(map
, vaddr
);
1928 vm_page_lockspin_queues();
1929 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
1930 vm_page_unlock_queues();
1932 OSAddAtomic(1, &inuse_ptepages_count
);
1933 OSAddAtomic64(1, &alloc_ptepages_count
);
1934 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1936 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1937 vm_object_lock(map
->pm_obj_pml4
);
1939 PMAP_LOCK_EXCLUSIVE(map
);
1941 * See if someone else expanded us first
1943 if (pmap64_pdpt(map
, vaddr
) != PDPT_ENTRY_NULL
) {
1944 PMAP_UNLOCK_EXCLUSIVE(map
);
1945 vm_object_unlock(map
->pm_obj_pml4
);
1949 OSAddAtomic(-1, &inuse_ptepages_count
);
1950 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1951 return KERN_SUCCESS
;
1955 if (0 != vm_page_lookup(map
->pm_obj_pml4
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1956 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1957 map
, map
->pm_obj_pml4
, vaddr
, i
);
1960 vm_page_insert_wired(m
, map
->pm_obj_pml4
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
1961 vm_object_unlock(map
->pm_obj_pml4
);
1964 * Set the page directory entry for this page table.
1966 pml4p
= pmap64_pml4(map
, vaddr
); /* refetch under lock */
1968 pmap_store_pte(pml4p
, pa_to_pte(pa
)
1970 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1971 | PTE_WRITE(is_ept
));
1972 pml4_entry_t
*upml4p
;
1974 upml4p
= pmap64_user_pml4(map
, vaddr
);
1975 pmap_store_pte(upml4p
, pa_to_pte(pa
)
1977 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1978 | PTE_WRITE(is_ept
));
1980 PMAP_UNLOCK_EXCLUSIVE(map
);
1982 return KERN_SUCCESS
;
1986 pmap_expand_pdpt(pmap_t map
, vm_map_offset_t vaddr
, unsigned int options
)
1992 pdpt_entry_t
*pdptp
;
1993 boolean_t is_ept
= is_ept_pmap(map
);
1995 DBG("pmap_expand_pdpt(%p,%p)\n", map
, (void *)vaddr
);
1997 while ((pdptp
= pmap64_pdpt(map
, vaddr
)) == PDPT_ENTRY_NULL
) {
1998 kern_return_t pep4kr
= pmap_expand_pml4(map
, vaddr
, options
);
1999 if (pep4kr
!= KERN_SUCCESS
) {
2005 * Allocate a VM page for the pdpt page
2007 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
2008 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
) {
2009 return KERN_RESOURCE_SHORTAGE
;
2015 * put the page into the pmap's obj list so it
2016 * can be found later.
2018 pn
= VM_PAGE_GET_PHYS_PAGE(m
);
2020 i
= pdptidx(map
, vaddr
);
2027 vm_page_lockspin_queues();
2028 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
2029 vm_page_unlock_queues();
2031 OSAddAtomic(1, &inuse_ptepages_count
);
2032 OSAddAtomic64(1, &alloc_ptepages_count
);
2033 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
2035 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
2036 vm_object_lock(map
->pm_obj_pdpt
);
2038 PMAP_LOCK_EXCLUSIVE(map
);
2040 * See if someone else expanded us first
2042 if (pmap_pde(map
, vaddr
) != PD_ENTRY_NULL
) {
2043 PMAP_UNLOCK_EXCLUSIVE(map
);
2044 vm_object_unlock(map
->pm_obj_pdpt
);
2048 OSAddAtomic(-1, &inuse_ptepages_count
);
2049 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
2050 return KERN_SUCCESS
;
2054 if (0 != vm_page_lookup(map
->pm_obj_pdpt
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
2055 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
2056 map
, map
->pm_obj_pdpt
, vaddr
, i
);
2059 vm_page_insert_wired(m
, map
->pm_obj_pdpt
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
2060 vm_object_unlock(map
->pm_obj_pdpt
);
2063 * Set the page directory entry for this page table.
2065 pdptp
= pmap64_pdpt(map
, vaddr
); /* refetch under lock */
2067 pmap_store_pte(pdptp
, pa_to_pte(pa
)
2069 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
2070 | PTE_WRITE(is_ept
));
2072 PMAP_UNLOCK_EXCLUSIVE(map
);
2074 return KERN_SUCCESS
;
2080 * Routine: pmap_expand
2082 * Expands a pmap to be able to map the specified virtual address.
2084 * Allocates new virtual memory for the P0 or P1 portion of the
2085 * pmap, then re-maps the physical pages that were in the old
2086 * pmap to be in the new pmap.
2088 * Must be called with the pmap system and the pmap unlocked,
2089 * since these must be unlocked to use vm_allocate or vm_deallocate.
2090 * Thus it must be called in a loop that checks whether the map
2091 * has been expanded enough.
2092 * (We won't loop forever, since page tables aren't shrunk.)
2097 vm_map_offset_t vaddr
,
2098 unsigned int options
)
2105 boolean_t is_ept
= is_ept_pmap(map
);
2109 * For the kernel, the virtual address must be in or above the basement
2110 * which is for kexts and is in the 512GB immediately below the kernel..
2111 * XXX - should use VM_MIN_KERNEL_AND_KEXT_ADDRESS not KERNEL_BASEMENT
2113 if (__improbable(map
== kernel_pmap
&&
2114 !(vaddr
>= KERNEL_BASEMENT
&& vaddr
<= VM_MAX_KERNEL_ADDRESS
))) {
2115 if ((options
& PMAP_EXPAND_OPTIONS_ALIASMAP
) == 0) {
2116 panic("pmap_expand: bad vaddr 0x%llx for kernel pmap", vaddr
);
2120 while ((pdp
= pmap_pde(map
, vaddr
)) == PD_ENTRY_NULL
) {
2121 assert((options
& PMAP_EXPAND_OPTIONS_ALIASMAP
) == 0);
2122 kern_return_t pepkr
= pmap_expand_pdpt(map
, vaddr
, options
);
2123 if (pepkr
!= KERN_SUCCESS
) {
2129 * Allocate a VM page for the pde entries.
2131 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
2132 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
) {
2133 return KERN_RESOURCE_SHORTAGE
;
2139 * put the page into the pmap's obj list so it
2140 * can be found later.
2142 pn
= VM_PAGE_GET_PHYS_PAGE(m
);
2144 i
= pdeidx(map
, vaddr
);
2151 vm_page_lockspin_queues();
2152 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
2153 vm_page_unlock_queues();
2155 OSAddAtomic(1, &inuse_ptepages_count
);
2156 OSAddAtomic64(1, &alloc_ptepages_count
);
2157 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
2159 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
2160 vm_object_lock(map
->pm_obj
);
2162 PMAP_LOCK_EXCLUSIVE(map
);
2165 * See if someone else expanded us first
2167 if (pmap_pte(map
, vaddr
) != PT_ENTRY_NULL
) {
2168 PMAP_UNLOCK_EXCLUSIVE(map
);
2169 vm_object_unlock(map
->pm_obj
);
2173 OSAddAtomic(-1, &inuse_ptepages_count
); //todo replace all with inlines
2174 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
2175 return KERN_SUCCESS
;
2179 if (0 != vm_page_lookup(map
->pm_obj
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
2180 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
2181 map
, map
->pm_obj
, vaddr
, i
);
2184 vm_page_insert_wired(m
, map
->pm_obj
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
2185 vm_object_unlock(map
->pm_obj
);
2188 * Set the page directory entry for this page table.
2190 pdp
= pmap_pde(map
, vaddr
);
2191 pmap_store_pte(pdp
, pa_to_pte(pa
)
2193 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
2194 | PTE_WRITE(is_ept
));
2196 PMAP_UNLOCK_EXCLUSIVE(map
);
2198 return KERN_SUCCESS
;
2201 * Query a pmap to see what size a given virtual address is mapped with.
2202 * If the vaddr is not mapped, returns 0.
2205 pmap_query_pagesize(
2207 vm_map_offset_t vaddr
)
2212 assert(!is_ept_pmap(pmap
));
2213 PMAP_LOCK_EXCLUSIVE(pmap
);
2215 pdep
= pmap_pde(pmap
, vaddr
);
2216 if (pdep
!= PD_ENTRY_NULL
) {
2217 if (*pdep
& INTEL_PTE_PS
) {
2218 size
= I386_LPGBYTES
;
2219 } else if (pmap_pte(pmap
, vaddr
) != PT_ENTRY_NULL
) {
2220 size
= I386_PGBYTES
;
2224 PMAP_UNLOCK_EXCLUSIVE(pmap
);
2230 * Ensure the page table hierarchy is filled in down to
2231 * the large page level. Additionally returns FAILURE if
2232 * a lower page table already exists.
2234 static kern_return_t
2235 pmap_pre_expand_large_internal(
2237 vm_map_offset_t vaddr
)
2241 boolean_t is_ept
= is_ept_pmap(pmap
);
2242 kern_return_t kr
= KERN_SUCCESS
;
2244 if (pmap64_pdpt(pmap
, vaddr
) == PDPT_ENTRY_NULL
) {
2245 if (!pmap_next_page_hi(&pn
, FALSE
)) {
2246 panic("pmap_pre_expand_large no PDPT");
2251 pte
= pmap64_pml4(pmap
, vaddr
);
2253 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
)) |
2255 (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
) |
2258 pte
= pmap64_user_pml4(pmap
, vaddr
);
2260 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
)) |
2262 (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
) |
2266 if (pmap_pde(pmap
, vaddr
) == PD_ENTRY_NULL
) {
2267 if (!pmap_next_page_hi(&pn
, FALSE
)) {
2268 panic("pmap_pre_expand_large no PDE");
2273 pte
= pmap64_pdpt(pmap
, vaddr
);
2275 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
)) |
2277 (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
) |
2279 } else if (pmap_pte(pmap
, vaddr
) != PT_ENTRY_NULL
) {
2287 * Wrapper that locks the pmap.
2290 pmap_pre_expand_large(
2292 vm_map_offset_t vaddr
)
2296 PMAP_LOCK_EXCLUSIVE(pmap
);
2297 kr
= pmap_pre_expand_large_internal(pmap
, vaddr
);
2298 PMAP_UNLOCK_EXCLUSIVE(pmap
);
2303 * On large memory machines, pmap_steal_memory() will allocate past
2304 * the 1GB of pre-allocated/mapped virtual kernel area. This function
2305 * expands kernel the page tables to cover a given vaddr. It uses pages
2306 * from the same pool that pmap_steal_memory() uses, since vm_page_grab()
2307 * isn't available yet.
2312 vm_map_offset_t vaddr
)
2316 boolean_t is_ept
= is_ept_pmap(pmap
);
2319 * This returns failure if a 4K page table already exists.
2320 * Othewise it fills in the page table hierarchy down
2323 PMAP_LOCK_EXCLUSIVE(pmap
);
2324 if (pmap_pre_expand_large_internal(pmap
, vaddr
) == KERN_FAILURE
) {
2325 PMAP_UNLOCK_EXCLUSIVE(pmap
);
2329 /* Add the lowest table */
2330 if (!pmap_next_page_hi(&pn
, FALSE
)) {
2331 panic("pmap_pre_expand");
2336 pte
= pmap_pde(pmap
, vaddr
);
2338 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
)) |
2340 (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
) |
2342 PMAP_UNLOCK_EXCLUSIVE(pmap
);
2346 * pmap_sync_page_data_phys(ppnum_t pa)
2348 * Invalidates all of the instruction cache on a physical page and
2349 * pushes any dirty data from the data cache for the same physical page
2350 * Not required in i386.
2353 pmap_sync_page_data_phys(__unused ppnum_t pa
)
2359 * pmap_sync_page_attributes_phys(ppnum_t pa)
2361 * Write back and invalidate all cachelines on a physical page.
2364 pmap_sync_page_attributes_phys(ppnum_t pa
)
2366 cache_flush_page_phys(pa
);
2370 pmap_copy_page(ppnum_t src
, ppnum_t dst
)
2372 bcopy_phys((addr64_t
)i386_ptob(src
),
2373 (addr64_t
)i386_ptob(dst
),
2379 * Routine: pmap_pageable
2381 * Make the specified pages (by pmap, offset)
2382 * pageable (or not) as requested.
2384 * A page which is not pageable may not take
2385 * a fault; therefore, its page table entry
2386 * must remain valid for the duration.
2388 * This routine is merely advisory; pmap_enter
2389 * will specify that these pages are to be wired
2390 * down (or not) as appropriate.
2394 __unused pmap_t pmap
,
2395 __unused vm_map_offset_t start_addr
,
2396 __unused vm_map_offset_t end_addr
,
2397 __unused boolean_t pageable
)
2400 pmap
++; start_addr
++; end_addr
++; pageable
++;
2405 invalidate_icache(__unused vm_offset_t addr
,
2406 __unused
unsigned cnt
,
2413 flush_dcache(__unused vm_offset_t addr
,
2414 __unused
unsigned count
,
2422 * Constrain DTrace copyin/copyout actions
2424 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
2425 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
2428 dtrace_copyio_preflight(__unused addr64_t va
)
2430 thread_t thread
= current_thread();
2432 if (current_map() == kernel_map
) {
2433 return KERN_FAILURE
;
2434 } else if (((ccr3
= get_cr3_base()) != thread
->map
->pmap
->pm_cr3
) && (no_shared_cr3
== FALSE
)) {
2435 return KERN_FAILURE
;
2436 } else if (no_shared_cr3
&& (ccr3
!= kernel_pmap
->pm_cr3
)) {
2437 return KERN_FAILURE
;
2439 return KERN_SUCCESS
;
2444 dtrace_copyio_postflight(__unused addr64_t va
)
2446 return KERN_SUCCESS
;
2448 #endif /* CONFIG_DTRACE */
2450 #include <mach_vm_debug.h>
2452 #include <vm/vm_debug.h>
2455 pmap_list_resident_pages(
2456 __unused pmap_t pmap
,
2457 __unused vm_offset_t
*listp
,
2462 #endif /* MACH_VM_DEBUG */
2466 /* temporary workaround */
2468 coredumpok(__unused vm_map_t map
, __unused mach_vm_offset_t va
)
2473 ptep
= pmap_pte(map
->pmap
, va
);
2477 return (*ptep
& (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
)) != (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
);
2485 phys_page_exists(ppnum_t pn
)
2487 assert(pn
!= vm_page_fictitious_addr
);
2489 if (!pmap_initialized
) {
2493 if (pn
== vm_page_guard_addr
) {
2497 if (!IS_MANAGED_PAGE(ppn_to_pai(pn
))) {
2507 pmap_switch(pmap_t tpmap
)
2509 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__SWITCH
) | DBG_FUNC_START
, VM_KERNEL_ADDRHIDE(tpmap
));
2510 assert(ml_get_interrupts_enabled() == FALSE
);
2511 set_dirbase(tpmap
, current_thread(), cpu_number());
2512 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__SWITCH
) | DBG_FUNC_END
);
2516 pmap_require(pmap_t pmap
)
2518 if (pmap
!= kernel_pmap
) {
2519 zone_id_require(ZONE_ID_PMAP
, sizeof(struct pmap
), pmap
);
2524 * disable no-execute capability on
2525 * the specified pmap
2528 pmap_disable_NX(__unused pmap_t pmap
)
2530 #if DEVELOPMENT || DEBUG
2531 pmap
->nx_enabled
= 0;
2536 pmap_flush_context_init(pmap_flush_context
*pfc
)
2539 pfc
->pfc_invalid_global
= 0;
2543 pmap_tlbi_response(uint32_t lcpu
, uint32_t rcpu
, bool ngflush
)
2545 bool responded
= false;
2546 bool gflushed
= (cpu_datap(rcpu
)->cpu_tlb_invalid_global_count
!=
2547 cpu_datap(lcpu
)->cpu_tlb_gen_counts_global
[rcpu
]);
2557 bool lflushed
= (cpu_datap(rcpu
)->cpu_tlb_invalid_local_count
!=
2558 cpu_datap(lcpu
)->cpu_tlb_gen_counts_local
[rcpu
]);
2565 if (responded
== false) {
2566 if ((cpu_datap(rcpu
)->cpu_tlb_invalid
== 0) ||
2567 !CPU_CR3_IS_ACTIVE(rcpu
) ||
2568 !cpu_is_running(rcpu
)) {
2575 extern uint64_t TLBTimeOut
;
2578 pmap_flush_context
*pfc
)
2580 unsigned int my_cpu
;
2583 cpumask_t cpus_to_respond
= 0;
2584 cpumask_t cpus_to_signal
= 0;
2585 cpumask_t cpus_signaled
= 0;
2586 boolean_t flush_self
= FALSE
;
2588 bool need_global_flush
= false;
2590 mp_disable_preemption();
2592 my_cpu
= cpu_number();
2593 cpus_to_signal
= pfc
->pfc_cpus
;
2595 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS
) | DBG_FUNC_START
,
2596 NULL
, cpus_to_signal
);
2598 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
&& cpus_to_signal
; cpu
++, cpu_bit
<<= 1) {
2599 if (cpus_to_signal
& cpu_bit
) {
2600 cpus_to_signal
&= ~cpu_bit
;
2602 if (!cpu_is_running(cpu
)) {
2606 if (pfc
->pfc_invalid_global
& cpu_bit
) {
2607 cpu_datap(cpu
)->cpu_tlb_invalid_global
= 1;
2608 need_global_flush
= true;
2610 cpu_datap(cpu
)->cpu_tlb_invalid_local
= 1;
2612 cpu_datap(my_cpu
)->cpu_tlb_gen_counts_global
[cpu
] = cpu_datap(cpu
)->cpu_tlb_invalid_global_count
;
2613 cpu_datap(my_cpu
)->cpu_tlb_gen_counts_local
[cpu
] = cpu_datap(cpu
)->cpu_tlb_invalid_local_count
;
2616 if (cpu
== my_cpu
) {
2620 if (CPU_CR3_IS_ACTIVE(cpu
)) {
2621 cpus_to_respond
|= cpu_bit
;
2622 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2626 cpus_signaled
= cpus_to_respond
;
2629 * Flush local tlb if required.
2630 * Do this now to overlap with other processors responding.
2633 process_pmap_updates(NULL
, (pfc
->pfc_invalid_global
!= 0), 0ULL, ~0ULL);
2636 if (cpus_to_respond
) {
2637 deadline
= mach_absolute_time() +
2638 (TLBTimeOut
? TLBTimeOut
: LockTimeOut
);
2639 boolean_t is_timeout_traced
= FALSE
;
2642 * Wait for those other cpus to acknowledge
2644 while (cpus_to_respond
!= 0) {
2647 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2648 bool responded
= false;
2649 if ((cpus_to_respond
& cpu_bit
) != 0) {
2650 responded
= pmap_tlbi_response(my_cpu
, cpu
, need_global_flush
);
2652 cpus_to_respond
&= ~cpu_bit
;
2657 if (cpus_to_respond
== 0) {
2661 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2662 if (machine_timeout_suspended()) {
2665 if (TLBTimeOut
== 0) {
2666 if (is_timeout_traced
) {
2670 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS_TO
),
2671 NULL
, cpus_to_signal
, cpus_to_respond
);
2673 is_timeout_traced
= TRUE
;
2676 orig_acks
= NMIPI_acks
;
2677 NMIPI_panic(cpus_to_respond
, TLB_FLUSH_TIMEOUT
);
2678 panic("Uninterruptible processor(s): CPU bitmap: 0x%llx, NMIPI acks: 0x%lx, now: 0x%lx, deadline: %llu",
2679 cpus_to_respond
, orig_acks
, NMIPI_acks
, deadline
);
2684 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS
) | DBG_FUNC_END
,
2685 NULL
, cpus_signaled
, flush_self
);
2687 mp_enable_preemption();
2697 } __attribute__((aligned(16), packed
)) invept_descriptor
= {(uint64_t)eptp
, 0};
2699 __asm__
volatile ("invept (%%rax), %%rcx"
2700 : : "c" (PMAP_INVEPT_SINGLE_CONTEXT
), "a" (&invept_descriptor
)
2705 * Called with pmap locked, we:
2706 * - scan through per-cpu data to see which other cpus need to flush
2707 * - send an IPI to each non-idle cpu to be flushed
2708 * - wait for all to signal back that they are inactive or we see that
2709 * they are at a safe point (idle).
2710 * - flush the local tlb if active for this pmap
2711 * - return ... the caller will unlock the pmap
2715 pmap_flush_tlbs(pmap_t pmap
, vm_map_offset_t startv
, vm_map_offset_t endv
, int options
, pmap_flush_context
*pfc
)
2719 cpumask_t cpus_to_signal
= 0;
2720 unsigned int my_cpu
= cpu_number();
2721 pmap_paddr_t pmap_cr3
= pmap
->pm_cr3
;
2722 boolean_t flush_self
= FALSE
;
2724 boolean_t pmap_is_shared
= (pmap
->pm_shared
|| (pmap
== kernel_pmap
));
2725 bool need_global_flush
= false;
2726 uint32_t event_code
;
2727 vm_map_offset_t event_startv
, event_endv
;
2728 boolean_t is_ept
= is_ept_pmap(pmap
);
2730 assert((processor_avail_count
< 2) ||
2731 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
2733 assert((endv
- startv
) >= PAGE_SIZE
);
2734 assert(((endv
| startv
) & PAGE_MASK
) == 0);
2736 if (__improbable(kdebug_enable
)) {
2737 if (pmap
== kernel_pmap
) {
2738 event_code
= PMAP_CODE(PMAP__FLUSH_KERN_TLBS
);
2739 event_startv
= VM_KERNEL_UNSLIDE_OR_PERM(startv
);
2740 event_endv
= VM_KERNEL_UNSLIDE_OR_PERM(endv
);
2741 } else if (__improbable(is_ept
)) {
2742 event_code
= PMAP_CODE(PMAP__FLUSH_EPT
);
2743 event_startv
= startv
;
2746 event_code
= PMAP_CODE(PMAP__FLUSH_TLBS
);
2747 event_startv
= startv
;
2752 PMAP_TRACE_CONSTANT(event_code
| DBG_FUNC_START
,
2753 VM_KERNEL_UNSLIDE_OR_PERM(pmap
), options
,
2754 event_startv
, event_endv
);
2756 if (__improbable(is_ept
)) {
2757 mp_cpus_call(CPUMASK_ALL
, ASYNC
, invept
, (void*)pmap
->pm_eptp
);
2762 * Scan other cpus for matching active or task CR3.
2763 * For idle cpus (with no active map) we mark them invalid but
2764 * don't signal -- they'll check as they go busy.
2766 if (pmap_pcid_ncpus
) {
2767 if (pmap_is_shared
) {
2768 need_global_flush
= true;
2770 pmap_pcid_invalidate_all_cpus(pmap
);
2774 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2775 if (!cpu_is_running(cpu
)) {
2778 uint64_t cpu_active_cr3
= CPU_GET_ACTIVE_CR3(cpu
);
2779 uint64_t cpu_task_cr3
= CPU_GET_TASK_CR3(cpu
);
2781 if ((pmap_cr3
== cpu_task_cr3
) ||
2782 (pmap_cr3
== cpu_active_cr3
) ||
2784 if (options
& PMAP_DELAY_TLB_FLUSH
) {
2785 if (need_global_flush
== true) {
2786 pfc
->pfc_invalid_global
|= cpu_bit
;
2788 pfc
->pfc_cpus
|= cpu_bit
;
2792 if (need_global_flush
== true) {
2793 cpu_datap(my_cpu
)->cpu_tlb_gen_counts_global
[cpu
] = cpu_datap(cpu
)->cpu_tlb_invalid_global_count
;
2794 cpu_datap(cpu
)->cpu_tlb_invalid_global
= 1;
2796 cpu_datap(my_cpu
)->cpu_tlb_gen_counts_local
[cpu
] = cpu_datap(cpu
)->cpu_tlb_invalid_local_count
;
2797 cpu_datap(cpu
)->cpu_tlb_invalid_local
= 1;
2800 if (cpu
== my_cpu
) {
2808 * We don't need to signal processors which will flush
2809 * lazily at the idle state or kernel boundary.
2810 * For example, if we're invalidating the kernel pmap,
2811 * processors currently in userspace don't need to flush
2812 * their TLBs until the next time they enter the kernel.
2813 * Alterations to the address space of a task active
2814 * on a remote processor result in a signal, to
2815 * account for copy operations. (There may be room
2816 * for optimization in such cases).
2817 * The order of the loads below with respect
2818 * to the store to the "cpu_tlb_invalid" field above
2819 * is important--hence the barrier.
2821 if (CPU_CR3_IS_ACTIVE(cpu
) &&
2822 (pmap_cr3
== CPU_GET_ACTIVE_CR3(cpu
) ||
2824 (pmap_cr3
== CPU_GET_TASK_CR3(cpu
)))) {
2825 cpus_to_signal
|= cpu_bit
;
2826 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2831 if ((options
& PMAP_DELAY_TLB_FLUSH
)) {
2836 * Flush local tlb if required.
2837 * Do this now to overlap with other processors responding.
2840 process_pmap_updates(pmap
, pmap_is_shared
, startv
, endv
);
2843 if (cpus_to_signal
) {
2844 cpumask_t cpus_to_respond
= cpus_to_signal
;
2846 deadline
= mach_absolute_time() +
2847 (TLBTimeOut
? TLBTimeOut
: LockTimeOut
);
2848 boolean_t is_timeout_traced
= FALSE
;
2851 * Wait for those other cpus to acknowledge
2853 while (cpus_to_respond
!= 0) {
2856 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2857 bool responded
= false;
2858 if ((cpus_to_respond
& cpu_bit
) != 0) {
2859 responded
= pmap_tlbi_response(my_cpu
, cpu
, need_global_flush
);
2861 cpus_to_respond
&= ~cpu_bit
;
2865 if (cpus_to_respond
== 0) {
2869 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2870 if (machine_timeout_suspended()) {
2873 if (TLBTimeOut
== 0) {
2874 /* cut tracepoint but don't panic */
2875 if (is_timeout_traced
) {
2879 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS_TO
),
2880 VM_KERNEL_UNSLIDE_OR_PERM(pmap
),
2884 is_timeout_traced
= TRUE
;
2887 orig_acks
= NMIPI_acks
;
2888 uint64_t tstamp1
= mach_absolute_time();
2889 NMIPI_panic(cpus_to_respond
, TLB_FLUSH_TIMEOUT
);
2890 uint64_t tstamp2
= mach_absolute_time();
2891 panic("IPI timeout, unresponsive CPU bitmap: 0x%llx, NMIPI acks: 0x%lx, now: 0x%lx, deadline: %llu, pre-NMIPI time: 0x%llx, current: 0x%llx, global: %d",
2892 cpus_to_respond
, orig_acks
, NMIPI_acks
, deadline
, tstamp1
, tstamp2
, need_global_flush
);
2897 if (__improbable((pmap
== kernel_pmap
) && (flush_self
!= TRUE
))) {
2898 panic("pmap_flush_tlbs: pmap == kernel_pmap && flush_self != TRUE; kernel CR3: 0x%llX, pmap_cr3: 0x%llx, CPU active CR3: 0x%llX, CPU Task Map: %d", kernel_pmap
->pm_cr3
, pmap_cr3
, current_cpu_datap()->cpu_active_cr3
, current_cpu_datap()->cpu_task_map
);
2902 PMAP_TRACE_CONSTANT(event_code
| DBG_FUNC_END
,
2903 VM_KERNEL_UNSLIDE_OR_PERM(pmap
), cpus_to_signal
,
2904 event_startv
, event_endv
);
2908 process_pmap_updates(pmap_t p
, bool pshared
, addr64_t istart
, addr64_t iend
)
2910 int ccpu
= cpu_number();
2913 pmap_assert(ml_get_interrupts_enabled() == 0 ||
2914 get_preemption_level() != 0);
2916 if (cpu_datap(ccpu
)->cpu_tlb_invalid_global
) {
2917 cpu_datap(ccpu
)->cpu_tlb_invalid_global_count
++;
2918 cpu_datap(ccpu
)->cpu_tlb_invalid
= 0;
2921 cpu_datap(ccpu
)->cpu_tlb_invalid_local_count
++;
2922 cpu_datap(ccpu
)->cpu_tlb_invalid_local
= 0;
2925 if (pmap_pcid_ncpus
) {
2927 /* TODO global generation count to
2928 * avoid potentially redundant
2929 * csw invalidations post-global invalidation
2931 pmap_pcid_validate_cpu(p
, ccpu
);
2932 pmap_tlbi_range(istart
, iend
, (pshared
|| gtlbf
), p
->pmap_pcid_cpus
[ccpu
]);
2934 pmap_pcid_validate_current();
2935 pmap_tlbi_range(istart
, iend
, true, 0);
2938 pmap_tlbi_range(0, ~0ULL, true, 0);
2943 pmap_update_interrupt(void)
2945 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_START
);
2947 if (current_cpu_datap()->cpu_tlb_invalid
) {
2948 process_pmap_updates(NULL
, true, 0ULL, ~0ULL);
2951 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_END
);
2954 #include <mach/mach_vm.h> /* mach_vm_region_recurse() */
2955 /* Scan kernel pmap for W+X PTEs, scan kernel VM map for W+X map entries
2956 * and identify ranges with mismatched VM permissions and PTE permissions
2959 pmap_permissions_verify(pmap_t ipmap
, vm_map_t ivmmap
, vm_offset_t sv
, vm_offset_t ev
)
2961 vm_offset_t cv
= sv
;
2962 kern_return_t rv
= KERN_SUCCESS
;
2963 uint64_t skip4
= 0, skip2
= 0;
2965 assert(!is_ept_pmap(ipmap
));
2967 sv
&= ~PAGE_MASK_64
;
2968 ev
&= ~PAGE_MASK_64
;
2970 if (__improbable((cv
> 0x00007FFFFFFFFFFFULL
) &&
2971 (cv
< 0xFFFF800000000000ULL
))) {
2972 cv
= 0xFFFF800000000000ULL
;
2974 /* Potential inconsistencies from not holding pmap lock
2975 * but harmless for the moment.
2977 if (((cv
& PML4MASK
) == 0) && (pmap64_pml4(ipmap
, cv
) == 0)) {
2978 if ((cv
+ NBPML4
) > cv
) {
2986 if (((cv
& PDMASK
) == 0) && (pmap_pde(ipmap
, cv
) == 0)) {
2987 if ((cv
+ NBPD
) > cv
) {
2996 pt_entry_t
*ptep
= pmap_pte(ipmap
, cv
);
2997 if (ptep
&& (*ptep
& INTEL_PTE_VALID
)) {
2998 if (*ptep
& INTEL_PTE_WRITE
) {
2999 if (!(*ptep
& INTEL_PTE_NX
)) {
3000 kprintf("W+X PTE at 0x%lx, P4: 0x%llx, P3: 0x%llx, P2: 0x%llx, PT: 0x%llx, VP: %u\n", cv
, *pmap64_pml4(ipmap
, cv
), *pmap64_pdpt(ipmap
, cv
), *pmap_pde(ipmap
, cv
), *ptep
, pmap_valid_page((ppnum_t
)(i386_btop(pte_to_pa(*ptep
)))));
3007 kprintf("Completed pmap scan\n");
3010 struct vm_region_submap_info_64 vbr
;
3011 mach_msg_type_number_t vbrcount
= 0;
3012 mach_vm_size_t vmsize
;
3014 uint32_t nesting_depth
= 0;
3019 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
3020 if ((kret
= mach_vm_region_recurse(ivmmap
,
3021 (mach_vm_address_t
*) &cv
, &vmsize
, &nesting_depth
,
3022 (vm_region_recurse_info_t
)&vbr
,
3023 &vbrcount
)) != KERN_SUCCESS
) {
3027 if (vbr
.is_submap
) {
3035 if (kret
!= KERN_SUCCESS
) {
3039 prot
= vbr
.protection
;
3041 if ((prot
& (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) == (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) {
3042 kprintf("W+X map entry at address 0x%lx\n", cv
);
3048 for (pcv
= cv
; pcv
< cv
+ vmsize
; pcv
+= PAGE_SIZE
) {
3049 pt_entry_t
*ptep
= pmap_pte(ipmap
, pcv
);
3052 if ((ptep
== NULL
) || !(*ptep
& INTEL_PTE_VALID
)) {
3055 tprot
= VM_PROT_READ
;
3056 if (*ptep
& INTEL_PTE_WRITE
) {
3057 tprot
|= VM_PROT_WRITE
;
3059 if ((*ptep
& INTEL_PTE_NX
) == 0) {
3060 tprot
|= VM_PROT_EXECUTE
;
3062 if (tprot
!= prot
) {
3063 kprintf("PTE/map entry permissions mismatch at address 0x%lx, pte: 0x%llx, protection: 0x%x\n", pcv
, *ptep
, prot
);
3074 extern int pmap_ledgers_panic
;
3075 extern int pmap_ledgers_panic_leeway
;
3084 if (pmap
->pmap_pid
== 0) {
3086 * This pmap was not or is no longer fully associated
3087 * with a task (e.g. the old pmap after a fork()/exec() or
3088 * spawn()). Its "ledger" still points at a task that is
3089 * now using a different (and active) address space, so
3090 * we can't check that all the pmap ledgers are balanced here.
3092 * If the "pid" is set, that means that we went through
3093 * pmap_set_process() in task_terminate_internal(), so
3094 * this task's ledger should not have been re-used and
3095 * all the pmap ledgers should be back to 0.
3100 pid
= pmap
->pmap_pid
;
3101 procname
= pmap
->pmap_procname
;
3103 vm_map_pmap_check_ledgers(pmap
, pmap
->ledger
, pid
, procname
);
3105 if (pmap
->stats
.resident_count
!= 0 ||
3108 * "wired_count" is unfortunately a bit inaccurate, so let's
3109 * tolerate some slight deviation to limit the amount of
3110 * somewhat-spurious assertion failures.
3112 pmap
->stats
.wired_count
> 10 ||
3113 #else /* 35156815 */
3114 pmap
->stats
.wired_count
!= 0 ||
3115 #endif /* 35156815 */
3116 pmap
->stats
.device
!= 0 ||
3117 pmap
->stats
.internal
!= 0 ||
3118 pmap
->stats
.external
!= 0 ||
3119 pmap
->stats
.reusable
!= 0 ||
3120 pmap
->stats
.compressed
!= 0) {
3121 if (pmap_stats_assert
&&
3122 pmap
->pmap_stats_assert
) {
3123 panic("pmap_destroy(%p) %d[%s] imbalanced stats: resident=%d wired=%d device=%d internal=%d external=%d reusable=%d compressed=%lld",
3124 pmap
, pid
, procname
,
3125 pmap
->stats
.resident_count
,
3126 pmap
->stats
.wired_count
,
3128 pmap
->stats
.internal
,
3129 pmap
->stats
.external
,
3130 pmap
->stats
.reusable
,
3131 pmap
->stats
.compressed
);
3133 printf("pmap_destroy(%p) %d[%s] imbalanced stats: resident=%d wired=%d device=%d internal=%d external=%d reusable=%d compressed=%lld",
3134 pmap
, pid
, procname
,
3135 pmap
->stats
.resident_count
,
3136 pmap
->stats
.wired_count
,
3138 pmap
->stats
.internal
,
3139 pmap
->stats
.external
,
3140 pmap
->stats
.reusable
,
3141 pmap
->stats
.compressed
);
3156 pmap
->pmap_pid
= pid
;
3157 strlcpy(pmap
->pmap_procname
, procname
, sizeof(pmap
->pmap_procname
));
3158 if (pmap_ledgers_panic_leeway
) {
3161 * Some processes somehow trigger some issues that make
3162 * the pmap stats and ledgers go off track, causing
3163 * some assertion failures and ledger panics.
3164 * Turn off the sanity checks if we allow some ledger leeway
3165 * because of that. We'll still do a final check in
3166 * pmap_check_ledgers() for discrepancies larger than the
3167 * allowed leeway after the address space has been fully
3170 pmap
->pmap_stats_assert
= FALSE
;
3171 ledger_disable_panic_on_negative(pmap
->ledger
,
3172 task_ledgers
.phys_footprint
);
3173 ledger_disable_panic_on_negative(pmap
->ledger
,
3174 task_ledgers
.internal
);
3175 ledger_disable_panic_on_negative(pmap
->ledger
,
3176 task_ledgers
.internal_compressed
);
3177 ledger_disable_panic_on_negative(pmap
->ledger
,
3178 task_ledgers
.iokit_mapped
);
3179 ledger_disable_panic_on_negative(pmap
->ledger
,
3180 task_ledgers
.alternate_accounting
);
3181 ledger_disable_panic_on_negative(pmap
->ledger
,
3182 task_ledgers
.alternate_accounting_compressed
);
3185 #endif /* MACH_ASSERT */
3188 #if DEVELOPMENT || DEBUG
3189 int pmap_pagezero_mitigation
= 1;
3193 pmap_advise_pagezero_range(pmap_t lpmap
, uint64_t low_bound
)
3195 #if DEVELOPMENT || DEBUG
3196 if (pmap_pagezero_mitigation
== 0) {
3197 lpmap
->pagezero_accessible
= FALSE
;
3201 lpmap
->pagezero_accessible
= ((pmap_smap_enabled
== FALSE
) && (low_bound
< 0x1000));
3202 if (lpmap
== current_pmap()) {
3203 mp_disable_preemption();
3204 current_cpu_datap()->cpu_pagezero_mapped
= lpmap
->pagezero_accessible
;
3205 mp_enable_preemption();
3210 pmap_verify_noncacheable(uintptr_t vaddr
)
3212 pt_entry_t
*ptep
= NULL
;
3213 ptep
= pmap_pte(kernel_pmap
, vaddr
);
3215 panic("pmap_verify_noncacheable: no translation for 0x%lx", vaddr
);
3217 /* Non-cacheable OK */
3218 if (*ptep
& (INTEL_PTE_NCACHE
)) {
3219 return pte_to_pa(*ptep
) | (vaddr
& INTEL_OFFMASK
);
3221 /* Write-combined OK */
3222 if (*ptep
& (INTEL_PTE_PAT
)) {
3223 return pte_to_pa(*ptep
) | (vaddr
& INTEL_OFFMASK
);
3225 panic("pmap_verify_noncacheable: IO read from a cacheable address? address: 0x%lx, PTE: %p, *PTE: 0x%llx", vaddr
, ptep
, *ptep
);
3231 trust_cache_init(void)
3233 // Unsupported on this architecture.
3237 pmap_load_legacy_trust_cache(struct pmap_legacy_trust_cache __unused
*trust_cache
,
3238 const vm_size_t __unused trust_cache_len
)
3240 // Unsupported on this architecture.
3241 return KERN_NOT_SUPPORTED
;
3245 pmap_load_image4_trust_cache(struct pmap_image4_trust_cache __unused
*trust_cache
,
3246 const vm_size_t __unused trust_cache_len
,
3247 uint8_t const * __unused img4_manifest
,
3248 const vm_size_t __unused img4_manifest_buffer_len
,
3249 const vm_size_t __unused img4_manifest_actual_len
,
3250 bool __unused dry_run
)
3252 // Unsupported on this architecture.
3253 return PMAP_TC_UNKNOWN_FORMAT
;
3258 pmap_is_trust_cache_loaded(const uuid_t __unused uuid
)
3260 // Unsupported on this architecture.
3265 pmap_lookup_in_loaded_trust_caches(const uint8_t __unused cdhash
[20])
3267 // Unsupported on this architecture.
3272 pmap_lookup_in_static_trust_cache(const uint8_t __unused cdhash
[20])
3274 // Unsupported on this architecture.
3278 SIMPLE_LOCK_DECLARE(pmap_compilation_service_cdhash_lock
, 0);
3279 uint8_t pmap_compilation_service_cdhash
[CS_CDHASH_LEN
] = { 0 };
3282 pmap_set_compilation_service_cdhash(const uint8_t cdhash
[CS_CDHASH_LEN
])
3284 simple_lock(&pmap_compilation_service_cdhash_lock
, LCK_GRP_NULL
);
3285 memcpy(pmap_compilation_service_cdhash
, cdhash
, CS_CDHASH_LEN
);
3286 simple_unlock(&pmap_compilation_service_cdhash_lock
);
3288 #if DEVELOPMENT || DEBUG
3289 printf("Added Compilation Service CDHash through the PMAP: 0x%02X 0x%02X 0x%02X 0x%02X\n", cdhash
[0], cdhash
[1], cdhash
[2], cdhash
[4]);
3294 pmap_match_compilation_service_cdhash(const uint8_t cdhash
[CS_CDHASH_LEN
])
3298 simple_lock(&pmap_compilation_service_cdhash_lock
, LCK_GRP_NULL
);
3299 if (bcmp(pmap_compilation_service_cdhash
, cdhash
, CS_CDHASH_LEN
) == 0) {
3302 simple_unlock(&pmap_compilation_service_cdhash_lock
);
3304 #if DEVELOPMENT || DEBUG
3306 printf("Matched Compilation Service CDHash through the PMAP\n");
3316 // Nonexistent on this architecture.
3321 pmap_lockdown_image4_slab(__unused vm_offset_t slab
, __unused vm_size_t slab_len
, __unused
uint64_t flags
)
3323 // Unsupported on this architecture.
3327 pmap_cs_allow_invalid(__unused pmap_t pmap
)
3329 // Unsupported on this architecture.
3330 return KERN_SUCCESS
;
3334 pmap_claim_reserved_ppl_page(void)
3336 // Unsupported on this architecture.
3341 pmap_free_reserved_ppl_page(void __unused
*kva
)
3343 // Unsupported on this architecture.
3346 #if DEVELOPMENT || DEBUG
3348 * Used for unit testing recovery from text corruptions.
3351 pmap_test_text_corruption(pmap_paddr_t pa
)
3356 pai
= ppn_to_pai(atop(pa
));
3357 if (!IS_MANAGED_PAGE(pai
)) {
3358 return KERN_FAILURE
;
3361 va
= (uint8_t *)PHYSMAP_PTOV(pa
);
3362 va
[0] = 0x0f; /* opcode for UD2 */
3365 return KERN_SUCCESS
;
3367 #endif /* DEVELOPMENT || DEBUG */