2 * Copyright (c) 2000-2010 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/kalloc.h>
106 #include <kern/spl.h>
109 #include <vm/vm_map.h>
110 #include <vm/vm_kern.h>
111 #include <mach/vm_param.h>
112 #include <mach/vm_prot.h>
113 #include <vm/vm_object.h>
114 #include <vm/vm_page.h>
116 #include <mach/machine/vm_param.h>
117 #include <machine/thread.h>
119 #include <kern/misc_protos.h> /* prototyping */
120 #include <i386/misc_protos.h>
121 #include <i386/i386_lowmem.h>
122 #include <x86_64/lowglobals.h>
124 #include <i386/cpuid.h>
125 #include <i386/cpu_data.h>
126 #include <i386/cpu_number.h>
127 #include <i386/machine_cpu.h>
128 #include <i386/seg.h>
129 #include <i386/serial_io.h>
130 #include <i386/cpu_capabilities.h>
131 #include <i386/machine_routines.h>
132 #include <i386/proc_reg.h>
133 #include <i386/tsc.h>
134 #include <i386/pmap_internal.h>
135 #include <i386/pmap_pcid.h>
137 #include <i386/vmx/vmx_cpu.h>
140 #include <vm/vm_protos.h>
141 #include <san/kasan.h>
144 #include <i386/mp_desc.h>
145 #include <libkern/kernel_mach_header.h>
147 #include <pexpert/i386/efi.h>
150 int pmap_stats_assert
= 1;
151 #endif /* MACH_ASSERT */
156 #define POSTCODE_DELAY 1
157 #include <i386/postcode.h>
158 #endif /* IWANTTODEBUG */
161 #define DBG(x...) kprintf("DBG: " x)
165 /* Compile time assert to ensure adjacency/alignment of per-CPU data fields used
166 * in the trampolines for kernel/user boundary TLB coherency.
168 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];
169 boolean_t pmap_trace
= FALSE
;
171 boolean_t no_shared_cr3
= DEBUG
; /* TRUE for DEBUG by default */
173 int nx_enabled
= 1; /* enable no-execute protection -- set during boot */
175 #if DEBUG || DEVELOPMENT
176 int allow_data_exec
= VM_ABI_32
; /* 32-bit apps may execute data by default, 64-bit apps may not */
177 int allow_stack_exec
= 0; /* No apps may execute from the stack by default */
178 #else /* DEBUG || DEVELOPMENT */
179 const int allow_data_exec
= VM_ABI_32
; /* 32-bit apps may execute data by default, 64-bit apps may not */
180 const int allow_stack_exec
= 0; /* No apps may execute from the stack by default */
181 #endif /* DEBUG || DEVELOPMENT */
183 const boolean_t cpu_64bit
= TRUE
; /* Mais oui! */
185 uint64_t max_preemption_latency_tsc
= 0;
187 pv_hashed_entry_t
*pv_hash_table
; /* hash lists */
189 uint32_t npvhashmask
= 0, npvhashbuckets
= 0;
191 pv_hashed_entry_t pv_hashed_free_list
= PV_HASHED_ENTRY_NULL
;
192 pv_hashed_entry_t pv_hashed_kern_free_list
= PV_HASHED_ENTRY_NULL
;
193 decl_simple_lock_data(,pv_hashed_free_list_lock
)
194 decl_simple_lock_data(,pv_hashed_kern_free_list_lock
)
195 decl_simple_lock_data(,pv_hash_table_lock
)
197 decl_simple_lock_data(,phys_backup_lock
)
199 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
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
209 static struct vm_object kpml4obj_object_store
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
210 static struct vm_object kpdptobj_object_store
__attribute__((aligned(VM_PACKED_POINTER_ALIGNMENT
)));
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;
220 * Amount of virtual memory mapped by one
221 * page-directory entry.
224 uint64_t pde_mapped_size
= PDE_MAPPED_SIZE
;
226 unsigned pmap_memory_region_count
;
227 unsigned pmap_memory_region_current
;
229 pmap_memory_region_t pmap_memory_regions
[PMAP_MEMORY_REGIONS_SIZE
];
232 * Other useful macros.
234 #define current_pmap() (vm_map_pmap(current_thread()->map))
236 struct pmap kernel_pmap_store
;
239 struct zone
*pmap_zone
; /* zone of pmap structures */
241 struct zone
*pmap_anchor_zone
;
242 struct zone
*pmap_uanchor_zone
;
243 int pmap_debug
= 0; /* flag for debugging prints */
245 unsigned int inuse_ptepages_count
= 0;
246 long long alloc_ptepages_count
__attribute__((aligned(8))) = 0; /* aligned for atomic access */
247 unsigned int bootstrap_wired_pages
= 0;
248 int pt_fake_zone_index
= -1;
250 extern long NMIPI_acks
;
252 boolean_t kernel_text_ps_4K
= TRUE
;
253 boolean_t wpkernel
= TRUE
;
259 pt_entry_t
*DMAP1
, *DMAP2
;
263 boolean_t pmap_disable_kheap_nx
= FALSE
;
264 boolean_t pmap_disable_kstack_nx
= FALSE
;
266 extern long __stack_chk_guard
[];
268 static uint64_t pmap_eptp_flags
= 0;
269 boolean_t pmap_ept_support_ad
= FALSE
;
273 * Map memory at initialization. The physical addresses being
274 * mapped are not managed and are never unmapped.
276 * For now, VM is already on, we only need to map the
282 vm_map_offset_t start_addr
,
283 vm_map_offset_t end_addr
,
291 while (start_addr
< end_addr
) {
292 kr
= pmap_enter(kernel_pmap
, (vm_map_offset_t
)virt
,
293 (ppnum_t
) i386_btop(start_addr
), prot
, VM_PROT_NONE
, flags
, TRUE
);
295 if (kr
!= KERN_SUCCESS
) {
296 panic("%s: failed pmap_enter, "
297 "virt=%p, start_addr=%p, end_addr=%p, prot=%#x, flags=%#x",
299 (void *)virt
, (void *)start_addr
, (void *)end_addr
, prot
, flags
);
308 extern char *first_avail
;
309 extern vm_offset_t virtual_avail
, virtual_end
;
310 extern pmap_paddr_t avail_start
, avail_end
;
311 extern vm_offset_t sHIB
;
312 extern vm_offset_t eHIB
;
313 extern vm_offset_t stext
;
314 extern vm_offset_t etext
;
315 extern vm_offset_t sdata
, edata
;
316 extern vm_offset_t sconst
, econst
;
318 extern void *KPTphys
;
320 boolean_t pmap_smep_enabled
= FALSE
;
321 boolean_t pmap_smap_enabled
= FALSE
;
326 cpu_data_t
*cdp
= current_cpu_datap();
328 set_cr4(get_cr4() | CR4_PGE
);
331 * Initialize the per-cpu, TLB-related fields.
333 cdp
->cpu_kernel_cr3
= kernel_pmap
->pm_cr3
;
334 cdp
->cpu_active_cr3
= kernel_pmap
->pm_cr3
;
335 cdp
->cpu_tlb_invalid
= FALSE
;
336 cdp
->cpu_task_map
= TASK_MAP_64BIT
;
338 pmap_pcid_configure();
339 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMEP
) {
340 pmap_smep_enabled
= TRUE
;
341 #if DEVELOPMENT || DEBUG
343 if (PE_parse_boot_argn("-pmap_smep_disable", &nsmep
, sizeof(nsmep
))) {
344 pmap_smep_enabled
= FALSE
;
347 if (pmap_smep_enabled
) {
348 set_cr4(get_cr4() | CR4_SMEP
);
352 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMAP
) {
353 pmap_smap_enabled
= TRUE
;
354 #if DEVELOPMENT || DEBUG
356 if (PE_parse_boot_argn("-pmap_smap_disable", &nsmap
, sizeof(nsmap
))) {
357 pmap_smap_enabled
= FALSE
;
360 if (pmap_smap_enabled
) {
361 set_cr4(get_cr4() | CR4_SMAP
);
366 if (cdp
->cpu_fixed_pmcs_enabled
) {
367 boolean_t enable
= TRUE
;
368 cpu_pmc_control(&enable
);
370 #endif /* !MONOTONIC */
373 static uint32_t pmap_scale_shift(void) {
376 if (sane_size
<= 8*GB
) {
377 scale
= (uint32_t)(sane_size
/ (2 * GB
));
378 } else if (sane_size
<= 32*GB
) {
379 scale
= 4 + (uint32_t)((sane_size
- (8 * GB
))/ (4 * GB
));
381 scale
= 10 + (uint32_t)MIN(4, ((sane_size
- (32 * GB
))/ (8 * GB
)));
387 * Bootstrap the system enough to run with virtual memory.
388 * Map the kernel's code and data, and allocate the system page table.
389 * Called with mapping OFF. Page_size must already be set.
394 __unused vm_offset_t load_start
,
395 __unused boolean_t IA32e
)
397 #if NCOPY_WINDOWS > 0
403 vm_last_addr
= VM_MAX_KERNEL_ADDRESS
; /* Set the highest address
406 * The kernel's pmap is statically allocated so we don't
407 * have to use pmap_create, which is unlikely to work
408 * correctly at this part of the boot sequence.
411 kernel_pmap
= &kernel_pmap_store
;
412 kernel_pmap
->ref_count
= 1;
413 kernel_pmap
->nx_enabled
= TRUE
;
414 kernel_pmap
->pm_task_map
= TASK_MAP_64BIT
;
415 kernel_pmap
->pm_obj
= (vm_object_t
) NULL
;
416 kernel_pmap
->pm_pml4
= IdlePML4
;
417 kernel_pmap
->pm_upml4
= IdlePML4
;
418 kernel_pmap
->pm_cr3
= (uintptr_t)ID_MAP_VTOP(IdlePML4
);
419 kernel_pmap
->pm_ucr3
= (uintptr_t)ID_MAP_VTOP(IdlePML4
);
420 kernel_pmap
->pm_eptp
= 0;
422 pmap_pcid_initialize_kernel(kernel_pmap
);
424 current_cpu_datap()->cpu_kernel_cr3
= (addr64_t
) kernel_pmap
->pm_cr3
;
427 OSAddAtomic(NKPT
, &inuse_ptepages_count
);
428 OSAddAtomic64(NKPT
, &alloc_ptepages_count
);
429 bootstrap_wired_pages
= NKPT
;
431 virtual_avail
= (vm_offset_t
)(VM_MIN_KERNEL_ADDRESS
) + (vm_offset_t
)first_avail
;
432 virtual_end
= (vm_offset_t
)(VM_MAX_KERNEL_ADDRESS
);
434 #if NCOPY_WINDOWS > 0
436 * Reserve some special page table entries/VA space for temporary
439 #define SYSMAP(c, p, v, n) \
440 v = (c)va; va += ((n)*INTEL_PGBYTES);
444 for (i
=0; i
<PMAP_NWINDOWS
; i
++) {
446 kprintf("trying to do SYSMAP idx %d %p\n", i
,
447 current_cpu_datap());
448 kprintf("cpu_pmap %p\n", current_cpu_datap()->cpu_pmap
);
449 kprintf("mapwindow %p\n", current_cpu_datap()->cpu_pmap
->mapwindow
);
450 kprintf("two stuff %p %p\n",
451 (void *)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
),
452 (void *)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
));
455 (current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
),
456 (current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
),
458 current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
=
459 &(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP_store
);
460 *current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
= 0;
463 /* DMAP user for debugger */
464 SYSMAP(caddr_t
, DMAP1
, DADDR1
, 1);
465 SYSMAP(caddr_t
, DMAP2
, DADDR2
, 1); /* XXX temporary - can remove */
469 if (!PE_parse_boot_argn("npvhash", &npvhashmask
, sizeof (npvhashmask
))) {
470 npvhashmask
= ((NPVHASHBUCKETS
) << pmap_scale_shift()) - 1;
474 npvhashbuckets
= npvhashmask
+ 1;
476 if (0 != ((npvhashbuckets
) & npvhashmask
)) {
477 panic("invalid hash %d, must be ((2^N)-1), "
478 "using default %d\n", npvhashmask
, NPVHASHMASK
);
481 simple_lock_init(&kernel_pmap
->lock
, 0);
482 simple_lock_init(&pv_hashed_free_list_lock
, 0);
483 simple_lock_init(&pv_hashed_kern_free_list_lock
, 0);
484 simple_lock_init(&pv_hash_table_lock
,0);
485 simple_lock_init(&phys_backup_lock
, 0);
490 printf("PMAP: PCID enabled\n");
492 if (pmap_smep_enabled
)
493 printf("PMAP: Supervisor Mode Execute Protection enabled\n");
494 if (pmap_smap_enabled
)
495 printf("PMAP: Supervisor Mode Access Protection enabled\n");
498 printf("Stack canary: 0x%lx\n", __stack_chk_guard
[0]);
499 printf("early_random(): 0x%qx\n", early_random());
501 #if DEVELOPMENT || DEBUG
503 /* Check if the user has requested disabling stack or heap no-execute
504 * enforcement. These are "const" variables; that qualifier is cast away
505 * when altering them. The TEXT/DATA const sections are marked
506 * write protected later in the kernel startup sequence, so altering
507 * them is possible at this point, in pmap_bootstrap().
509 if (PE_parse_boot_argn("-pmap_disable_kheap_nx", &ptmp
, sizeof(ptmp
))) {
510 boolean_t
*pdknxp
= (boolean_t
*) &pmap_disable_kheap_nx
;
514 if (PE_parse_boot_argn("-pmap_disable_kstack_nx", &ptmp
, sizeof(ptmp
))) {
515 boolean_t
*pdknhp
= (boolean_t
*) &pmap_disable_kstack_nx
;
518 #endif /* DEVELOPMENT || DEBUG */
520 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
521 if (args
->efiMode
== kBootArgsEfiMode32
) {
522 printf("EFI32: kernel virtual space limited to 4GB\n");
523 virtual_end
= VM_MAX_KERNEL_ADDRESS_EFI32
;
525 kprintf("Kernel virtual space from 0x%lx to 0x%lx.\n",
526 (long)KERNEL_BASE
, (long)virtual_end
);
527 kprintf("Available physical space from 0x%llx to 0x%llx\n",
528 avail_start
, avail_end
);
531 * The -no_shared_cr3 boot-arg is a debugging feature (set by default
532 * in the DEBUG kernel) to force the kernel to switch to its own map
533 * (and cr3) when control is in kernelspace. The kernel's map does not
534 * include (i.e. share) userspace so wild references will cause
535 * a panic. Only copyin and copyout are exempt from this.
537 (void) PE_parse_boot_argn("-no_shared_cr3",
538 &no_shared_cr3
, sizeof (no_shared_cr3
));
540 kprintf("Kernel not sharing user map\n");
543 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace
, sizeof (pmap_trace
))) {
544 kprintf("Kernel traces for pmap operations enabled\n");
546 #endif /* PMAP_TRACES */
549 PE_parse_boot_argn("pmap_stats_assert",
551 sizeof (pmap_stats_assert
));
552 #endif /* MACH_ASSERT */
560 *startp
= virtual_avail
;
569 #include <IOKit/IOHibernatePrivate.h>
572 int32_t pmap_teardown_last_valid_compact_indx
= -1;
575 void hibernate_rebuild_pmap_structs(void);
576 void hibernate_teardown_pmap_structs(addr64_t
*, addr64_t
*);
577 void pmap_pack_index(uint32_t);
578 int32_t pmap_unpack_index(pv_rooted_entry_t
);
582 pmap_unpack_index(pv_rooted_entry_t pv_h
)
586 indx
= (int32_t)(*((uint64_t *)(&pv_h
->qlink
.next
)) >> 48);
588 indx
|= (int32_t)(*((uint64_t *)(&pv_h
->qlink
.prev
)) >> 48);
590 *((uint64_t *)(&pv_h
->qlink
.next
)) |= ((uint64_t)0xffff << 48);
591 *((uint64_t *)(&pv_h
->qlink
.prev
)) |= ((uint64_t)0xffff << 48);
598 pmap_pack_index(uint32_t indx
)
600 pv_rooted_entry_t pv_h
;
602 pv_h
= &pv_head_table
[indx
];
604 *((uint64_t *)(&pv_h
->qlink
.next
)) &= ~((uint64_t)0xffff << 48);
605 *((uint64_t *)(&pv_h
->qlink
.prev
)) &= ~((uint64_t)0xffff << 48);
607 *((uint64_t *)(&pv_h
->qlink
.next
)) |= ((uint64_t)(indx
>> 16)) << 48;
608 *((uint64_t *)(&pv_h
->qlink
.prev
)) |= ((uint64_t)(indx
& 0xffff)) << 48;
613 hibernate_teardown_pmap_structs(addr64_t
*unneeded_start
, addr64_t
*unneeded_end
)
616 int32_t compact_target_indx
;
618 compact_target_indx
= 0;
620 for (i
= 0; i
< pmap_npages
; i
++) {
621 if (pv_head_table
[i
].pmap
== PMAP_NULL
) {
623 if (pv_head_table
[compact_target_indx
].pmap
!= PMAP_NULL
)
624 compact_target_indx
= i
;
626 pmap_pack_index((uint32_t)i
);
628 if (pv_head_table
[compact_target_indx
].pmap
== PMAP_NULL
) {
630 * we've got a hole to fill, so
631 * move this pv_rooted_entry_t to it's new home
633 pv_head_table
[compact_target_indx
] = pv_head_table
[i
];
634 pv_head_table
[i
].pmap
= PMAP_NULL
;
636 pmap_teardown_last_valid_compact_indx
= compact_target_indx
;
637 compact_target_indx
++;
639 pmap_teardown_last_valid_compact_indx
= i
;
642 *unneeded_start
= (addr64_t
)&pv_head_table
[pmap_teardown_last_valid_compact_indx
+1];
643 *unneeded_end
= (addr64_t
)&pv_head_table
[pmap_npages
-1];
645 HIBLOG("hibernate_teardown_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx
);
650 hibernate_rebuild_pmap_structs(void)
652 int32_t cindx
, eindx
, rindx
= 0;
653 pv_rooted_entry_t pv_h
;
655 eindx
= (int32_t)pmap_npages
;
657 for (cindx
= pmap_teardown_last_valid_compact_indx
; cindx
>= 0; cindx
--) {
659 pv_h
= &pv_head_table
[cindx
];
661 rindx
= pmap_unpack_index(pv_h
);
662 assert(rindx
< pmap_npages
);
664 if (rindx
!= cindx
) {
666 * this pv_rooted_entry_t was moved by hibernate_teardown_pmap_structs,
667 * so move it back to its real location
669 pv_head_table
[rindx
] = pv_head_table
[cindx
];
671 if (rindx
+1 != eindx
) {
673 * the 'hole' between this vm_rooted_entry_t and the previous
674 * vm_rooted_entry_t we moved needs to be initialized as
675 * a range of zero'd vm_rooted_entry_t's
677 bzero((char *)&pv_head_table
[rindx
+1], (eindx
- rindx
- 1) * sizeof (struct pv_rooted_entry
));
682 bzero ((char *)&pv_head_table
[0], rindx
* sizeof (struct pv_rooted_entry
));
684 HIBLOG("hibernate_rebuild_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx
);
690 * Initialize the pmap module.
691 * Called by vm_init, to initialize any structures that the pmap
692 * system needs to map virtual memory.
700 vm_map_offset_t vaddr
;
704 kernel_pmap
->pm_obj_pml4
= &kpml4obj_object_store
;
705 _vm_object_allocate((vm_object_size_t
)NPML4PGS
* PAGE_SIZE
, &kpml4obj_object_store
);
707 kernel_pmap
->pm_obj_pdpt
= &kpdptobj_object_store
;
708 _vm_object_allocate((vm_object_size_t
)NPDPTPGS
* PAGE_SIZE
, &kpdptobj_object_store
);
710 kernel_pmap
->pm_obj
= &kptobj_object_store
;
711 _vm_object_allocate((vm_object_size_t
)NPDEPGS
* PAGE_SIZE
, &kptobj_object_store
);
714 * Allocate memory for the pv_head_table and its lock bits,
715 * the modify bit array, and the pte_page table.
719 * zero bias all these arrays now instead of off avail_start
720 * so we cover all memory
723 npages
= i386_btop(avail_end
);
725 pmap_npages
= (uint32_t)npages
;
727 s
= (vm_size_t
) (sizeof(struct pv_rooted_entry
) * npages
728 + (sizeof (struct pv_hashed_entry_t
*) * (npvhashbuckets
))
729 + pv_lock_table_size(npages
)
730 + pv_hash_lock_table_size((npvhashbuckets
))
733 if (kernel_memory_allocate(kernel_map
, &addr
, s
, 0,
734 KMA_KOBJECT
| KMA_PERMANENT
, VM_KERN_MEMORY_PMAP
)
738 memset((char *)addr
, 0, s
);
744 if (0 == npvhashmask
) panic("npvhashmask not initialized");
748 * Allocate the structures first to preserve word-alignment.
750 pv_head_table
= (pv_rooted_entry_t
) addr
;
751 addr
= (vm_offset_t
) (pv_head_table
+ npages
);
753 pv_hash_table
= (pv_hashed_entry_t
*)addr
;
754 addr
= (vm_offset_t
) (pv_hash_table
+ (npvhashbuckets
));
756 pv_lock_table
= (char *) addr
;
757 addr
= (vm_offset_t
) (pv_lock_table
+ pv_lock_table_size(npages
));
759 pv_hash_lock_table
= (char *) addr
;
760 addr
= (vm_offset_t
) (pv_hash_lock_table
+ pv_hash_lock_table_size((npvhashbuckets
)));
762 pmap_phys_attributes
= (char *) addr
;
764 ppnum_t last_pn
= i386_btop(avail_end
);
766 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
767 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
768 if (pmptr
->type
!= kEfiConventionalMemory
)
771 for (pn
= pmptr
->base
; pn
<= pmptr
->end
; pn
++) {
773 pmap_phys_attributes
[pn
] |= PHYS_MANAGED
;
775 if (pn
> last_managed_page
)
776 last_managed_page
= pn
;
778 if (pn
>= lowest_hi
&& pn
<= highest_hi
)
779 pmap_phys_attributes
[pn
] |= PHYS_NOENCRYPT
;
784 ppn
= pmap_find_phys(kernel_pmap
, vaddr
);
786 pmap_phys_attributes
[ppn
] |= PHYS_NOENCRYPT
;
792 * Create the zone of physical maps,
793 * and of the physical-to-virtual entries.
795 s
= (vm_size_t
) sizeof(struct pmap
);
796 pmap_zone
= zinit(s
, 400*s
, 4096, "pmap"); /* XXX */
797 zone_change(pmap_zone
, Z_NOENCRYPT
, TRUE
);
799 pmap_anchor_zone
= zinit(PAGE_SIZE
, task_max
, PAGE_SIZE
, "pagetable anchors");
800 zone_change(pmap_anchor_zone
, Z_NOENCRYPT
, TRUE
);
802 /* The anchor is required to be page aligned. Zone debugging adds
803 * padding which may violate that requirement. Tell the zone
804 * subsystem that alignment is required.
807 zone_change(pmap_anchor_zone
, Z_ALIGNMENT_REQUIRED
, TRUE
);
808 /* TODO: possible general optimisation...pre-allocate via zones commonly created
809 * level3/2 pagetables
811 pmap_uanchor_zone
= zinit(PAGE_SIZE
, task_max
, PAGE_SIZE
, "pagetable user anchors");
812 zone_change(pmap_uanchor_zone
, Z_NOENCRYPT
, TRUE
);
814 /* The anchor is required to be page aligned. Zone debugging adds
815 * padding which may violate that requirement. Tell the zone
816 * subsystem that alignment is required.
819 zone_change(pmap_uanchor_zone
, Z_ALIGNMENT_REQUIRED
, TRUE
);
821 s
= (vm_size_t
) sizeof(struct pv_hashed_entry
);
822 pv_hashed_list_zone
= zinit(s
, 10000*s
/* Expandable zone */,
823 4096 * 3 /* LCM x86_64*/, "pv_list");
824 zone_change(pv_hashed_list_zone
, Z_NOENCRYPT
, TRUE
);
825 zone_change(pv_hashed_list_zone
, Z_GZALLOC_EXEMPT
, TRUE
);
827 /* create pv entries for kernel pages mapped by low level
828 startup code. these have to exist so we can pmap_remove()
829 e.g. kext pages from the middle of our addr space */
831 vaddr
= (vm_map_offset_t
) VM_MIN_KERNEL_ADDRESS
;
832 for (ppn
= VM_MIN_KERNEL_PAGE
; ppn
< i386_btop(avail_start
); ppn
++) {
833 pv_rooted_entry_t pv_e
;
835 pv_e
= pai_to_pvh(ppn
);
836 pv_e
->va_and_flags
= vaddr
;
838 pv_e
->pmap
= kernel_pmap
;
839 queue_init(&pv_e
->qlink
);
841 pmap_initialized
= TRUE
;
843 max_preemption_latency_tsc
= tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS
, tscFCvtn2t
);
846 * Ensure the kernel's PML4 entry exists for the basement
847 * before this is shared with any user.
849 pmap_expand_pml4(kernel_pmap
, KERNEL_BASEMENT
, PMAP_EXPAND_OPTIONS_NONE
);
852 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
);
853 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);
854 #endif /* CONFIG_VMX */
858 void pmap_mark_range(pmap_t npmap
, uint64_t sv
, uint64_t nxrosz
, boolean_t NX
, boolean_t ro
) {
859 uint64_t ev
= sv
+ nxrosz
, cv
= sv
;
861 pt_entry_t
*ptep
= NULL
;
863 assert(!is_ept_pmap(npmap
));
865 assert(((sv
& 0xFFFULL
) | (nxrosz
& 0xFFFULL
)) == 0);
867 for (pdep
= pmap_pde(npmap
, cv
); pdep
!= NULL
&& (cv
< ev
);) {
868 uint64_t pdev
= (cv
& ~((uint64_t)PDEMASK
));
870 if (*pdep
& INTEL_PTE_PS
) {
872 *pdep
|= INTEL_PTE_NX
;
874 *pdep
&= ~INTEL_PTE_WRITE
;
876 cv
&= ~((uint64_t) PDEMASK
);
877 pdep
= pmap_pde(npmap
, cv
);
881 for (ptep
= pmap_pte(npmap
, cv
); ptep
!= NULL
&& (cv
< (pdev
+ NBPD
)) && (cv
< ev
);) {
883 *ptep
|= INTEL_PTE_NX
;
885 *ptep
&= ~INTEL_PTE_WRITE
;
887 ptep
= pmap_pte(npmap
, cv
);
890 DPRINTF("%s(0x%llx, 0x%llx, %u, %u): 0x%llx, 0x%llx\n", __FUNCTION__
, sv
, nxrosz
, NX
, ro
, cv
, ptep
? *ptep
: 0);
894 * Called once VM is fully initialized so that we can release unused
895 * sections of low memory to the general pool.
896 * Also complete the set-up of identity-mapped sections of the kernel:
897 * 1) write-protect kernel text
898 * 2) map kernel text using large pages if possible
899 * 3) read and write-protect page zero (for K32)
900 * 4) map the global page at the appropriate virtual address.
904 * To effectively map and write-protect all kernel text pages, the text
905 * must be 2M-aligned at the base, and the data section above must also be
906 * 2M-aligned. That is, there's padding below and above. This is achieved
907 * through linker directives. Large pages are used only if this alignment
908 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
913 * sdata: ================== 2Meg
917 * etext: ------------------
925 * stext: ================== 2Meg
929 * eHIB: ------------------
933 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
934 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
935 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
936 * The now unused level-1 PTE pages are also freed.
938 extern ppnum_t vm_kernel_base_page
;
939 static uint32_t constptes
= 0, dataptes
= 0;
941 void pmap_lowmem_finalize(void) {
946 * Update wired memory statistics for early boot pages
948 PMAP_ZINFO_PALLOC(kernel_pmap
, bootstrap_wired_pages
* PAGE_SIZE
);
951 * Free pages in pmap regions below the base:
953 * We can't free all the pages to VM that EFI reports available.
954 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
955 * There's also a size miscalculation here: pend is one page less
956 * than it should be but this is not fixed to be backwards
958 * This is important for KASLR because up to 256*2MB = 512MB of space
959 * needs has to be released to VM.
962 pmap_memory_regions
[i
].end
< vm_kernel_base_page
;
964 vm_offset_t pbase
= i386_ptob(pmap_memory_regions
[i
].base
);
965 vm_offset_t pend
= i386_ptob(pmap_memory_regions
[i
].end
+1);
967 DBG("pmap region %d [%p..[%p\n",
968 i
, (void *) pbase
, (void *) pend
);
970 if (pmap_memory_regions
[i
].attribute
& EFI_MEMORY_KERN_RESERVED
)
974 * Adjust limits not to free pages in range 0xc0000-0xff000.
976 if (pbase
>= 0xc0000 && pend
<= 0x100000)
978 if (pbase
< 0xc0000 && pend
> 0x100000) {
979 /* page range entirely within region, free lower part */
980 DBG("- ml_static_mfree(%p,%p)\n",
981 (void *) ml_static_ptovirt(pbase
),
982 (void *) (0xc0000-pbase
));
983 ml_static_mfree(ml_static_ptovirt(pbase
),0xc0000-pbase
);
987 pend
= MIN(pend
, 0xc0000);
989 pbase
= MAX(pbase
, 0x100000);
990 DBG("- ml_static_mfree(%p,%p)\n",
991 (void *) ml_static_ptovirt(pbase
),
992 (void *) (pend
- pbase
));
993 ml_static_mfree(ml_static_ptovirt(pbase
), pend
- pbase
);
996 /* A final pass to get rid of all initial identity mappings to
999 DPRINTF("%s: Removing mappings from 0->0x%lx\n", __FUNCTION__
, vm_kernel_base
);
1002 * Remove all mappings past the boot-cpu descriptor aliases and low globals.
1003 * Non-boot-cpu GDT aliases will be remapped later as needed.
1005 pmap_remove(kernel_pmap
, LOWGLOBAL_ALIAS
+ PAGE_SIZE
, vm_kernel_base
);
1008 * If text and data are both 2MB-aligned,
1009 * we can map text with large-pages,
1010 * unless the -kernel_text_ps_4K boot-arg overrides.
1012 if ((stext
& I386_LPGMASK
) == 0 && (sdata
& I386_LPGMASK
) == 0) {
1013 kprintf("Kernel text is 2MB aligned");
1014 kernel_text_ps_4K
= FALSE
;
1015 if (PE_parse_boot_argn("-kernel_text_ps_4K",
1017 sizeof (kernel_text_ps_4K
)))
1018 kprintf(" but will be mapped with 4K pages\n");
1020 kprintf(" and will be mapped with 2M pages\n");
1023 (void) PE_parse_boot_argn("wpkernel", &wpkernel
, sizeof (wpkernel
));
1025 kprintf("Kernel text %p-%p to be write-protected\n",
1026 (void *) stext
, (void *) etext
);
1031 * Scan over text if mappings are to be changed:
1032 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
1033 * - Change to large-pages if possible and not overriden.
1035 if (kernel_text_ps_4K
&& wpkernel
) {
1037 for (myva
= stext
; myva
< etext
; myva
+= PAGE_SIZE
) {
1040 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
1042 pmap_store_pte(ptep
, *ptep
& ~INTEL_PTE_WRITE
);
1046 if (!kernel_text_ps_4K
) {
1050 * Release zero-filled page padding used for 2M-alignment.
1052 DBG("ml_static_mfree(%p,%p) for padding below text\n",
1053 (void *) eHIB
, (void *) (stext
- eHIB
));
1054 ml_static_mfree(eHIB
, stext
- eHIB
);
1055 DBG("ml_static_mfree(%p,%p) for padding above text\n",
1056 (void *) etext
, (void *) (sdata
- etext
));
1057 ml_static_mfree(etext
, sdata
- etext
);
1060 * Coalesce text pages into large pages.
1062 for (myva
= stext
; myva
< sdata
; myva
+= I386_LPGBYTES
) {
1064 vm_offset_t pte_phys
;
1068 pdep
= pmap_pde(kernel_pmap
, (vm_map_offset_t
)myva
);
1069 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
1070 DBG("myva: %p pdep: %p ptep: %p\n",
1071 (void *) myva
, (void *) pdep
, (void *) ptep
);
1072 if ((*ptep
& INTEL_PTE_VALID
) == 0)
1074 pte_phys
= (vm_offset_t
)(*ptep
& PG_FRAME
);
1075 pde
= *pdep
& PTMASK
; /* page attributes from pde */
1076 pde
|= INTEL_PTE_PS
; /* make it a 2M entry */
1077 pde
|= pte_phys
; /* take page frame from pte */
1080 pde
&= ~INTEL_PTE_WRITE
;
1081 DBG("pmap_store_pte(%p,0x%llx)\n",
1083 pmap_store_pte(pdep
, pde
);
1086 * Free the now-unused level-1 pte.
1087 * Note: ptep is a virtual address to the pte in the
1088 * recursive map. We can't use this address to free
1089 * the page. Instead we need to compute its address
1090 * in the Idle PTEs in "low memory".
1092 vm_offset_t vm_ptep
= (vm_offset_t
) KPTphys
1093 + (pte_phys
>> PTPGSHIFT
);
1094 DBG("ml_static_mfree(%p,0x%x) for pte\n",
1095 (void *) vm_ptep
, PAGE_SIZE
);
1096 ml_static_mfree(vm_ptep
, PAGE_SIZE
);
1099 /* Change variable read by sysctl machdep.pmap */
1100 pmap_kernel_text_ps
= I386_LPGBYTES
;
1103 boolean_t doconstro
= TRUE
;
1104 #if DEVELOPMENT || DEBUG
1105 (void) PE_parse_boot_argn("dataconstro", &doconstro
, sizeof(doconstro
));
1108 if (sconst
& PAGE_MASK
) {
1109 panic("CONST segment misaligned 0x%lx 0x%lx\n",
1112 kprintf("Marking const DATA read-only\n");
1117 for (dva
= sdata
; dva
< edata
; dva
+= I386_PGBYTES
) {
1118 assert(((sdata
| edata
) & PAGE_MASK
) == 0);
1119 pt_entry_t dpte
, *dptep
= pmap_pte(kernel_pmap
, dva
);
1122 assert((dpte
& INTEL_PTE_VALID
));
1123 dpte
|= INTEL_PTE_NX
;
1124 pmap_store_pte(dptep
, dpte
);
1127 assert(dataptes
> 0);
1129 for (dva
= sconst
; dva
< econst
; dva
+= I386_PGBYTES
) {
1130 pt_entry_t dpte
, *dptep
= pmap_pte(kernel_pmap
, dva
);
1134 assert((dpte
& INTEL_PTE_VALID
));
1135 dpte
|= INTEL_PTE_NX
;
1136 dpte
&= ~INTEL_PTE_WRITE
;
1138 pmap_store_pte(dptep
, dpte
);
1141 assert(constptes
> 0);
1143 kernel_segment_command_t
* seg
;
1144 kernel_section_t
* sec
;
1146 for (seg
= firstseg(); seg
!= NULL
; seg
= nextsegfromheader(&_mh_execute_header
, seg
)) {
1147 if (!strcmp(seg
->segname
, "__TEXT") ||
1148 !strcmp(seg
->segname
, "__DATA")) {
1152 if (!strcmp(seg
->segname
, "__KLD")) {
1155 if (!strcmp(seg
->segname
, "__HIB")) {
1156 for (sec
= firstsect(seg
); sec
!= NULL
; sec
= nextsect(seg
, sec
)) {
1157 if (sec
->addr
& PAGE_MASK
)
1158 panic("__HIB segment's sections misaligned");
1159 if (!strcmp(sec
->sectname
, "__text")) {
1160 pmap_mark_range(kernel_pmap
, sec
->addr
, round_page(sec
->size
), FALSE
, TRUE
);
1162 pmap_mark_range(kernel_pmap
, sec
->addr
, round_page(sec
->size
), TRUE
, FALSE
);
1166 pmap_mark_range(kernel_pmap
, seg
->vmaddr
, round_page_64(seg
->vmsize
), TRUE
, FALSE
);
1171 * If we're debugging, map the low global vector page at the fixed
1172 * virtual address. Otherwise, remove the mapping for this.
1174 if (debug_boot_arg
) {
1175 pt_entry_t
*pte
= NULL
;
1176 if (0 == (pte
= pmap_pte(kernel_pmap
, LOWGLOBAL_ALIAS
)))
1177 panic("lowmem pte");
1178 /* make sure it is defined on page boundary */
1179 assert(0 == ((vm_offset_t
) &lowGlo
& PAGE_MASK
));
1180 pmap_store_pte(pte
, kvtophys((vm_offset_t
)&lowGlo
)
1188 pmap_remove(kernel_pmap
,
1189 LOWGLOBAL_ALIAS
, LOWGLOBAL_ALIAS
+ PAGE_SIZE
);
1193 if (pmap_pcid_ncpus
)
1200 * this function is only used for debugging fron the vm layer
1206 pv_rooted_entry_t pv_h
;
1210 assert(pn
!= vm_page_fictitious_addr
);
1212 if (!pmap_initialized
)
1215 if (pn
== vm_page_guard_addr
)
1218 pai
= ppn_to_pai(pn
);
1219 if (!IS_MANAGED_PAGE(pai
))
1221 pv_h
= pai_to_pvh(pn
);
1222 result
= (pv_h
->pmap
== PMAP_NULL
);
1229 vm_map_offset_t va_start
,
1230 vm_map_offset_t va_end
)
1232 vm_map_offset_t offset
;
1235 if (pmap
== PMAP_NULL
) {
1240 * Check the resident page count
1241 * - if it's zero, the pmap is completely empty.
1242 * This short-circuit test prevents a virtual address scan which is
1243 * painfully slow for 64-bit spaces.
1244 * This assumes the count is correct
1245 * .. the debug kernel ought to be checking perhaps by page table walk.
1247 if (pmap
->stats
.resident_count
== 0)
1250 for (offset
= va_start
;
1252 offset
+= PAGE_SIZE_64
) {
1253 phys_page
= pmap_find_phys(pmap
, offset
);
1255 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
1256 "page %d at 0x%llx\n",
1257 pmap
, va_start
, va_end
, phys_page
, offset
);
1266 hv_ept_pmap_create(void **ept_pmap
, void **eptp
)
1270 if ((ept_pmap
== NULL
) || (eptp
== NULL
)) {
1274 p
= pmap_create_options(get_task_ledger(current_task()), 0, (PMAP_CREATE_64BIT
| PMAP_CREATE_EPT
));
1275 if (p
== PMAP_NULL
) {
1281 assert(is_ept_pmap(p
));
1283 *ept_pmap
= (void*)p
;
1284 *eptp
= (void*)(p
->pm_eptp
);
1289 * Create and return a physical map.
1291 * If the size specified for the map
1292 * is zero, the map is an actual physical
1293 * map, and may be referenced by the
1296 * If the size specified is non-zero,
1297 * the map will be used in software only, and
1298 * is bounded by that size.
1302 pmap_create_options(
1310 pml4_entry_t
*kpml4
;
1312 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_START
, sz
, flags
);
1314 size
= (vm_size_t
) sz
;
1317 * A software use-only map doesn't even need a map.
1325 * Return error when unrecognized flags are passed.
1327 if (__improbable((flags
& ~(PMAP_CREATE_KNOWN_FLAGS
)) != 0)) {
1331 p
= (pmap_t
) zalloc(pmap_zone
);
1333 panic("pmap_create zalloc");
1335 /* Zero all fields */
1336 bzero(p
, sizeof(*p
));
1337 /* init counts now since we'll be bumping some */
1338 simple_lock_init(&p
->lock
, 0);
1339 bzero(&p
->stats
, sizeof (p
->stats
));
1343 p
->pm_shared
= FALSE
;
1344 ledger_reference(ledger
);
1347 p
->pm_task_map
= ((flags
& PMAP_CREATE_64BIT
) ? TASK_MAP_64BIT
: TASK_MAP_32BIT
);
1349 p
->pagezero_accessible
= FALSE
;
1351 if (pmap_pcid_ncpus
) {
1352 pmap_pcid_initialize(p
);
1355 p
->pm_pml4
= zalloc(pmap_anchor_zone
);
1356 p
->pm_upml4
= zalloc(pmap_uanchor_zone
); //cleanup for EPT
1358 pmap_assert((((uintptr_t)p
->pm_pml4
) & PAGE_MASK
) == 0);
1359 pmap_assert((((uintptr_t)p
->pm_upml4
) & PAGE_MASK
) == 0);
1361 memset((char *)p
->pm_pml4
, 0, PAGE_SIZE
);
1362 memset((char *)p
->pm_upml4
, 0, PAGE_SIZE
);
1364 if (flags
& PMAP_CREATE_EPT
) {
1365 p
->pm_eptp
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_pml4
) | pmap_eptp_flags
;
1369 p
->pm_cr3
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_pml4
);
1370 p
->pm_ucr3
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_upml4
);
1373 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
1375 p
->pm_obj_pml4
= vm_object_allocate((vm_object_size_t
)(NPML4PGS
) * PAGE_SIZE
);
1376 if (NULL
== p
->pm_obj_pml4
)
1377 panic("pmap_create pdpt obj");
1379 p
->pm_obj_pdpt
= vm_object_allocate((vm_object_size_t
)(NPDPTPGS
) * PAGE_SIZE
);
1380 if (NULL
== p
->pm_obj_pdpt
)
1381 panic("pmap_create pdpt obj");
1383 p
->pm_obj
= vm_object_allocate((vm_object_size_t
)(NPDEPGS
) * PAGE_SIZE
);
1384 if (NULL
== p
->pm_obj
)
1385 panic("pmap_create pte obj");
1387 if (!(flags
& PMAP_CREATE_EPT
)) {
1388 /* All host pmaps share the kernel's pml4 */
1389 pml4
= pmap64_pml4(p
, 0ULL);
1390 kpml4
= kernel_pmap
->pm_pml4
;
1391 pml4
[KERNEL_PML4_INDEX
] = kpml4
[KERNEL_PML4_INDEX
];
1392 pml4
[KERNEL_KEXTS_INDEX
] = kpml4
[KERNEL_KEXTS_INDEX
];
1393 pml4
[KERNEL_PHYSMAP_PML4_INDEX
] = kpml4
[KERNEL_PHYSMAP_PML4_INDEX
];
1394 pml4
[KERNEL_DBLMAP_PML4_INDEX
] = kpml4
[KERNEL_DBLMAP_PML4_INDEX
];
1396 pml4
[KERNEL_KASAN_PML4_INDEX0
] = kpml4
[KERNEL_KASAN_PML4_INDEX0
];
1397 pml4
[KERNEL_KASAN_PML4_INDEX1
] = kpml4
[KERNEL_KASAN_PML4_INDEX1
];
1399 pml4_entry_t
*pml4u
= pmap64_user_pml4(p
, 0ULL);
1400 pml4u
[KERNEL_DBLMAP_PML4_INDEX
] = kpml4
[KERNEL_DBLMAP_PML4_INDEX
];
1405 strlcpy(p
->pmap_procname
, "<nil>", sizeof (p
->pmap_procname
));
1406 #endif /* MACH_ASSERT */
1408 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_END
,
1409 VM_KERNEL_ADDRHIDE(p
));
1420 return pmap_create_options(ledger
, sz
, ((is_64bit
) ? PMAP_CREATE_64BIT
: 0));
1424 * We maintain stats and ledgers so that a task's physical footprint is:
1425 * phys_footprint = ((internal - alternate_accounting)
1426 * + (internal_compressed - alternate_accounting_compressed)
1428 * + purgeable_nonvolatile
1429 * + purgeable_nonvolatile_compressed
1431 * where "alternate_accounting" includes "iokit" and "purgeable" memory.
1436 uint64_t num_pmaps_checked
;
1438 int phys_footprint_over
;
1439 ledger_amount_t phys_footprint_over_total
;
1440 ledger_amount_t phys_footprint_over_max
;
1441 int phys_footprint_under
;
1442 ledger_amount_t phys_footprint_under_total
;
1443 ledger_amount_t phys_footprint_under_max
;
1446 ledger_amount_t internal_over_total
;
1447 ledger_amount_t internal_over_max
;
1449 ledger_amount_t internal_under_total
;
1450 ledger_amount_t internal_under_max
;
1452 int internal_compressed_over
;
1453 ledger_amount_t internal_compressed_over_total
;
1454 ledger_amount_t internal_compressed_over_max
;
1455 int internal_compressed_under
;
1456 ledger_amount_t internal_compressed_under_total
;
1457 ledger_amount_t internal_compressed_under_max
;
1459 int iokit_mapped_over
;
1460 ledger_amount_t iokit_mapped_over_total
;
1461 ledger_amount_t iokit_mapped_over_max
;
1462 int iokit_mapped_under
;
1463 ledger_amount_t iokit_mapped_under_total
;
1464 ledger_amount_t iokit_mapped_under_max
;
1466 int alternate_accounting_over
;
1467 ledger_amount_t alternate_accounting_over_total
;
1468 ledger_amount_t alternate_accounting_over_max
;
1469 int alternate_accounting_under
;
1470 ledger_amount_t alternate_accounting_under_total
;
1471 ledger_amount_t alternate_accounting_under_max
;
1473 int alternate_accounting_compressed_over
;
1474 ledger_amount_t alternate_accounting_compressed_over_total
;
1475 ledger_amount_t alternate_accounting_compressed_over_max
;
1476 int alternate_accounting_compressed_under
;
1477 ledger_amount_t alternate_accounting_compressed_under_total
;
1478 ledger_amount_t alternate_accounting_compressed_under_max
;
1480 int page_table_over
;
1481 ledger_amount_t page_table_over_total
;
1482 ledger_amount_t page_table_over_max
;
1483 int page_table_under
;
1484 ledger_amount_t page_table_under_total
;
1485 ledger_amount_t page_table_under_max
;
1487 int purgeable_volatile_over
;
1488 ledger_amount_t purgeable_volatile_over_total
;
1489 ledger_amount_t purgeable_volatile_over_max
;
1490 int purgeable_volatile_under
;
1491 ledger_amount_t purgeable_volatile_under_total
;
1492 ledger_amount_t purgeable_volatile_under_max
;
1494 int purgeable_nonvolatile_over
;
1495 ledger_amount_t purgeable_nonvolatile_over_total
;
1496 ledger_amount_t purgeable_nonvolatile_over_max
;
1497 int purgeable_nonvolatile_under
;
1498 ledger_amount_t purgeable_nonvolatile_under_total
;
1499 ledger_amount_t purgeable_nonvolatile_under_max
;
1501 int purgeable_volatile_compressed_over
;
1502 ledger_amount_t purgeable_volatile_compressed_over_total
;
1503 ledger_amount_t purgeable_volatile_compressed_over_max
;
1504 int purgeable_volatile_compressed_under
;
1505 ledger_amount_t purgeable_volatile_compressed_under_total
;
1506 ledger_amount_t purgeable_volatile_compressed_under_max
;
1508 int purgeable_nonvolatile_compressed_over
;
1509 ledger_amount_t purgeable_nonvolatile_compressed_over_total
;
1510 ledger_amount_t purgeable_nonvolatile_compressed_over_max
;
1511 int purgeable_nonvolatile_compressed_under
;
1512 ledger_amount_t purgeable_nonvolatile_compressed_under_total
;
1513 ledger_amount_t purgeable_nonvolatile_compressed_under_max
;
1514 } pmap_ledgers_drift
;
1515 static void pmap_check_ledgers(pmap_t pmap
);
1516 #else /* MACH_ASSERT */
1517 static inline void pmap_check_ledgers(__unused pmap_t pmap
) {}
1518 #endif /* MACH_ASSERT */
1521 * Retire the given physical map from service.
1522 * Should only be called if the map contains
1523 * no valid mappings.
1525 extern int vm_wired_objects_page_count
;
1528 pmap_destroy(pmap_t p
)
1535 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_START
,
1536 VM_KERNEL_ADDRHIDe(p
));
1542 pmap_assert((current_thread() && (current_thread()->map
)) ? (current_thread()->map
->pmap
!= p
) : TRUE
);
1546 * If some cpu is not using the physical pmap pointer that it
1547 * is supposed to be (see set_dirbase), we might be using the
1548 * pmap that is being destroyed! Make sure we are
1549 * physically on the right pmap:
1551 PMAP_UPDATE_TLBS(p
, 0x0ULL
, 0xFFFFFFFFFFFFF000ULL
);
1552 if (pmap_pcid_ncpus
)
1553 pmap_destroy_pcid_sync(p
);
1559 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
);
1560 pmap_assert(p
== kernel_pmap
);
1561 return; /* still in use */
1565 * Free the memory maps, then the
1568 int inuse_ptepages
= 0;
1570 zfree(pmap_anchor_zone
, p
->pm_pml4
);
1571 zfree(pmap_uanchor_zone
, p
->pm_upml4
);
1573 inuse_ptepages
+= p
->pm_obj_pml4
->resident_page_count
;
1574 vm_object_deallocate(p
->pm_obj_pml4
);
1576 inuse_ptepages
+= p
->pm_obj_pdpt
->resident_page_count
;
1577 vm_object_deallocate(p
->pm_obj_pdpt
);
1579 inuse_ptepages
+= p
->pm_obj
->resident_page_count
;
1580 vm_object_deallocate(p
->pm_obj
);
1582 OSAddAtomic(-inuse_ptepages
, &inuse_ptepages_count
);
1583 PMAP_ZINFO_PFREE(p
, inuse_ptepages
* PAGE_SIZE
);
1585 pmap_check_ledgers(p
);
1586 ledger_dereference(p
->ledger
);
1587 zfree(pmap_zone
, p
);
1589 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
);
1593 * Add a reference to the specified pmap.
1597 pmap_reference(pmap_t p
)
1599 if (p
!= PMAP_NULL
) {
1607 * Remove phys addr if mapped in specified map
1611 pmap_remove_some_phys(
1612 __unused pmap_t map
,
1613 __unused ppnum_t pn
)
1616 /* Implement to support working set code */
1624 vm_map_offset_t sva
,
1625 vm_map_offset_t eva
,
1628 pmap_protect_options(map
, sva
, eva
, prot
, 0, NULL
);
1633 * Set the physical protection on the
1634 * specified range of this map as requested.
1635 * Will not increase permissions.
1638 pmap_protect_options(
1640 vm_map_offset_t sva
,
1641 vm_map_offset_t eva
,
1643 unsigned int options
,
1647 pt_entry_t
*spte
, *epte
;
1648 vm_map_offset_t lva
;
1649 vm_map_offset_t orig_sva
;
1656 if (map
== PMAP_NULL
)
1659 if (prot
== VM_PROT_NONE
) {
1660 pmap_remove_options(map
, sva
, eva
, options
);
1664 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_START
,
1665 VM_KERNEL_ADDRHIDE(map
), VM_KERNEL_ADDRHIDE(sva
),
1666 VM_KERNEL_ADDRHIDE(eva
));
1668 if ((prot
& VM_PROT_EXECUTE
) || !nx_enabled
|| !map
->nx_enabled
)
1673 is_ept
= is_ept_pmap(map
);
1680 lva
= (sva
+ pde_mapped_size
) & ~(pde_mapped_size
- 1);
1683 pde
= pmap_pde(map
, sva
);
1684 if (pde
&& (*pde
& PTE_VALID_MASK(is_ept
))) {
1685 if (*pde
& PTE_PS
) {
1688 epte
= spte
+1; /* excluded */
1690 spte
= pmap_pte(map
, (sva
& ~(pde_mapped_size
- 1)));
1691 spte
= &spte
[ptenum(sva
)];
1692 epte
= &spte
[intel_btop(lva
- sva
)];
1695 for (; spte
< epte
; spte
++) {
1696 if (!(*spte
& PTE_VALID_MASK(is_ept
)))
1700 if (prot
& VM_PROT_READ
)
1701 pmap_update_pte(spte
, 0, PTE_READ(is_ept
));
1703 pmap_update_pte(spte
, PTE_READ(is_ept
), 0);
1705 if (prot
& VM_PROT_WRITE
)
1706 pmap_update_pte(spte
, 0, PTE_WRITE(is_ept
));
1708 pmap_update_pte(spte
, PTE_WRITE(is_ept
), 0);
1712 pmap_update_pte(spte
, 0, INTEL_PTE_NX
);
1714 pmap_update_pte(spte
, INTEL_EPT_EX
, 0);
1717 pmap_update_pte(spte
, INTEL_PTE_NX
, 0);
1719 pmap_update_pte(spte
, 0, INTEL_EPT_EX
);
1727 if (options
& PMAP_OPTIONS_NOFLUSH
)
1728 PMAP_UPDATE_TLBS_DELAYED(map
, orig_sva
, eva
, (pmap_flush_context
*)arg
);
1730 PMAP_UPDATE_TLBS(map
, orig_sva
, eva
);
1734 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_END
);
1738 /* Map a (possibly) autogenned block */
1747 __unused
unsigned int flags
)
1750 addr64_t original_va
= va
;
1754 if (attr
& VM_MEM_SUPERPAGE
)
1755 cur_page_size
= SUPERPAGE_SIZE
;
1757 cur_page_size
= PAGE_SIZE
;
1759 for (page
= 0; page
< size
; page
+=cur_page_size
/PAGE_SIZE
) {
1760 kr
= pmap_enter(pmap
, va
, pa
, prot
, VM_PROT_NONE
, attr
, TRUE
);
1762 if (kr
!= KERN_SUCCESS
) {
1764 * This will panic for now, as it is unclear that
1765 * removing the mappings is correct.
1767 panic("%s: failed pmap_enter, "
1768 "pmap=%p, va=%#llx, pa=%u, size=%u, prot=%#x, flags=%#x",
1770 pmap
, va
, pa
, size
, prot
, flags
);
1772 pmap_remove(pmap
, original_va
, va
- original_va
);
1776 va
+= cur_page_size
;
1777 pa
+=cur_page_size
/PAGE_SIZE
;
1780 return KERN_SUCCESS
;
1786 vm_map_offset_t vaddr
,
1787 unsigned int options
)
1793 pml4_entry_t
*pml4p
;
1794 boolean_t is_ept
= is_ept_pmap(map
);
1796 DBG("pmap_expand_pml4(%p,%p)\n", map
, (void *)vaddr
);
1798 /* With the exception of the kext "basement", the kernel's level 4
1799 * pagetables must not be dynamically expanded.
1801 assert(map
!= kernel_pmap
|| (vaddr
== KERNEL_BASEMENT
));
1803 * Allocate a VM page for the pml4 page
1805 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1806 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
1807 return KERN_RESOURCE_SHORTAGE
;
1811 * put the page into the pmap's obj list so it
1812 * can be found later.
1814 pn
= VM_PAGE_GET_PHYS_PAGE(m
);
1816 i
= pml4idx(map
, vaddr
);
1823 vm_page_lockspin_queues();
1824 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
1825 vm_page_unlock_queues();
1827 OSAddAtomic(1, &inuse_ptepages_count
);
1828 OSAddAtomic64(1, &alloc_ptepages_count
);
1829 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1831 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1832 vm_object_lock(map
->pm_obj_pml4
);
1836 * See if someone else expanded us first
1838 if (pmap64_pdpt(map
, vaddr
) != PDPT_ENTRY_NULL
) {
1840 vm_object_unlock(map
->pm_obj_pml4
);
1844 OSAddAtomic(-1, &inuse_ptepages_count
);
1845 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1846 return KERN_SUCCESS
;
1850 if (0 != vm_page_lookup(map
->pm_obj_pml4
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1851 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1852 map
, map
->pm_obj_pml4
, vaddr
, i
);
1855 vm_page_insert_wired(m
, map
->pm_obj_pml4
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
1856 vm_object_unlock(map
->pm_obj_pml4
);
1859 * Set the page directory entry for this page table.
1861 pml4p
= pmap64_pml4(map
, vaddr
); /* refetch under lock */
1863 pmap_store_pte(pml4p
, pa_to_pte(pa
)
1865 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1866 | PTE_WRITE(is_ept
));
1867 pml4_entry_t
*upml4p
;
1869 upml4p
= pmap64_user_pml4(map
, vaddr
);
1870 pmap_store_pte(upml4p
, pa_to_pte(pa
)
1872 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1873 | PTE_WRITE(is_ept
));
1877 return KERN_SUCCESS
;
1881 pmap_expand_pdpt(pmap_t map
, vm_map_offset_t vaddr
, unsigned int options
)
1887 pdpt_entry_t
*pdptp
;
1888 boolean_t is_ept
= is_ept_pmap(map
);
1890 DBG("pmap_expand_pdpt(%p,%p)\n", map
, (void *)vaddr
);
1892 while ((pdptp
= pmap64_pdpt(map
, vaddr
)) == PDPT_ENTRY_NULL
) {
1893 kern_return_t pep4kr
= pmap_expand_pml4(map
, vaddr
, options
);
1894 if (pep4kr
!= KERN_SUCCESS
)
1899 * Allocate a VM page for the pdpt page
1901 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1902 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
1903 return KERN_RESOURCE_SHORTAGE
;
1908 * put the page into the pmap's obj list so it
1909 * can be found later.
1911 pn
= VM_PAGE_GET_PHYS_PAGE(m
);
1913 i
= pdptidx(map
, vaddr
);
1920 vm_page_lockspin_queues();
1921 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
1922 vm_page_unlock_queues();
1924 OSAddAtomic(1, &inuse_ptepages_count
);
1925 OSAddAtomic64(1, &alloc_ptepages_count
);
1926 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1928 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1929 vm_object_lock(map
->pm_obj_pdpt
);
1933 * See if someone else expanded us first
1935 if (pmap64_pde(map
, vaddr
) != PD_ENTRY_NULL
) {
1937 vm_object_unlock(map
->pm_obj_pdpt
);
1941 OSAddAtomic(-1, &inuse_ptepages_count
);
1942 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1943 return KERN_SUCCESS
;
1947 if (0 != vm_page_lookup(map
->pm_obj_pdpt
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1948 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1949 map
, map
->pm_obj_pdpt
, vaddr
, i
);
1952 vm_page_insert_wired(m
, map
->pm_obj_pdpt
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
1953 vm_object_unlock(map
->pm_obj_pdpt
);
1956 * Set the page directory entry for this page table.
1958 pdptp
= pmap64_pdpt(map
, vaddr
); /* refetch under lock */
1960 pmap_store_pte(pdptp
, pa_to_pte(pa
)
1962 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1963 | PTE_WRITE(is_ept
));
1967 return KERN_SUCCESS
;
1974 * Routine: pmap_expand
1976 * Expands a pmap to be able to map the specified virtual address.
1978 * Allocates new virtual memory for the P0 or P1 portion of the
1979 * pmap, then re-maps the physical pages that were in the old
1980 * pmap to be in the new pmap.
1982 * Must be called with the pmap system and the pmap unlocked,
1983 * since these must be unlocked to use vm_allocate or vm_deallocate.
1984 * Thus it must be called in a loop that checks whether the map
1985 * has been expanded enough.
1986 * (We won't loop forever, since page tables aren't shrunk.)
1991 vm_map_offset_t vaddr
,
1992 unsigned int options
)
1999 boolean_t is_ept
= is_ept_pmap(map
);
2003 * For the kernel, the virtual address must be in or above the basement
2004 * which is for kexts and is in the 512GB immediately below the kernel..
2005 * XXX - should use VM_MIN_KERNEL_AND_KEXT_ADDRESS not KERNEL_BASEMENT
2007 if (__improbable(map
== kernel_pmap
&&
2008 !(vaddr
>= KERNEL_BASEMENT
&& vaddr
<= VM_MAX_KERNEL_ADDRESS
))) {
2009 if ((options
& PMAP_EXPAND_OPTIONS_ALIASMAP
) == 0) {
2010 panic("pmap_expand: bad vaddr 0x%llx for kernel pmap", vaddr
);
2015 while ((pdp
= pmap64_pde(map
, vaddr
)) == PD_ENTRY_NULL
) {
2016 assert((options
& PMAP_EXPAND_OPTIONS_ALIASMAP
) == 0);
2017 kern_return_t pepkr
= pmap_expand_pdpt(map
, vaddr
, options
);
2018 if (pepkr
!= KERN_SUCCESS
)
2023 * Allocate a VM page for the pde entries.
2025 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
2026 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
2027 return KERN_RESOURCE_SHORTAGE
;
2032 * put the page into the pmap's obj list so it
2033 * can be found later.
2035 pn
= VM_PAGE_GET_PHYS_PAGE(m
);
2037 i
= pdeidx(map
, vaddr
);
2044 vm_page_lockspin_queues();
2045 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
2046 vm_page_unlock_queues();
2048 OSAddAtomic(1, &inuse_ptepages_count
);
2049 OSAddAtomic64(1, &alloc_ptepages_count
);
2050 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
2052 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
2053 vm_object_lock(map
->pm_obj
);
2058 * See if someone else expanded us first
2060 if (pmap_pte(map
, vaddr
) != PT_ENTRY_NULL
) {
2062 vm_object_unlock(map
->pm_obj
);
2066 OSAddAtomic(-1, &inuse_ptepages_count
);//todo replace all with inlines
2067 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
2068 return KERN_SUCCESS
;
2072 if (0 != vm_page_lookup(map
->pm_obj
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
2073 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
2074 map
, map
->pm_obj
, vaddr
, i
);
2077 vm_page_insert_wired(m
, map
->pm_obj
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
2078 vm_object_unlock(map
->pm_obj
);
2081 * Set the page directory entry for this page table.
2083 pdp
= pmap_pde(map
, vaddr
);
2084 pmap_store_pte(pdp
, pa_to_pte(pa
)
2086 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
2087 | PTE_WRITE(is_ept
));
2091 return KERN_SUCCESS
;
2094 /* On K64 machines with more than 32GB of memory, pmap_steal_memory
2095 * will allocate past the 1GB of pre-expanded virtual kernel area. This
2096 * function allocates all the page tables using memory from the same pool
2097 * that pmap_steal_memory uses, rather than calling vm_page_grab (which
2098 * isn't available yet). */
2100 pmap_pre_expand(pmap_t pmap
, vm_map_offset_t vaddr
)
2104 boolean_t is_ept
= is_ept_pmap(pmap
);
2108 if(pmap64_pdpt(pmap
, vaddr
) == PDPT_ENTRY_NULL
) {
2109 if (!pmap_next_page_hi(&pn
))
2110 panic("pmap_pre_expand");
2114 pte
= pmap64_pml4(pmap
, vaddr
);
2116 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
2118 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
2119 | PTE_WRITE(is_ept
));
2121 pte
= pmap64_user_pml4(pmap
, vaddr
);
2123 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
2125 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
2126 | PTE_WRITE(is_ept
));
2130 if(pmap64_pde(pmap
, vaddr
) == PD_ENTRY_NULL
) {
2131 if (!pmap_next_page_hi(&pn
))
2132 panic("pmap_pre_expand");
2136 pte
= pmap64_pdpt(pmap
, vaddr
);
2138 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
2140 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
2141 | PTE_WRITE(is_ept
));
2144 if(pmap_pte(pmap
, vaddr
) == PT_ENTRY_NULL
) {
2145 if (!pmap_next_page_hi(&pn
))
2146 panic("pmap_pre_expand");
2150 pte
= pmap64_pde(pmap
, vaddr
);
2152 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
2154 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
2155 | PTE_WRITE(is_ept
));
2162 * pmap_sync_page_data_phys(ppnum_t pa)
2164 * Invalidates all of the instruction cache on a physical page and
2165 * pushes any dirty data from the data cache for the same physical page
2166 * Not required in i386.
2169 pmap_sync_page_data_phys(__unused ppnum_t pa
)
2175 * pmap_sync_page_attributes_phys(ppnum_t pa)
2177 * Write back and invalidate all cachelines on a physical page.
2180 pmap_sync_page_attributes_phys(ppnum_t pa
)
2182 cache_flush_page_phys(pa
);
2187 #ifdef CURRENTLY_UNUSED_AND_UNTESTED
2193 * Routine: pmap_collect
2195 * Garbage collects the physical map system for
2196 * pages which are no longer used.
2197 * Success need not be guaranteed -- that is, there
2198 * may well be pages which are not referenced, but
2199 * others may be collected.
2201 * Called by the pageout daemon when pages are scarce.
2207 pt_entry_t
*pdp
, *ptp
;
2215 if (p
== kernel_pmap
)
2218 is_ept
= is_ept_pmap(p
);
2221 * Garbage collect map.
2225 for (pdp
= (pt_entry_t
*)p
->dirbase
;
2226 pdp
< (pt_entry_t
*)&p
->dirbase
[(UMAXPTDI
+1)];
2229 if (*pdp
& PTE_VALID_MASK(is_ept
)) {
2230 if (*pdp
& PTE_REF(is_ept
)) {
2231 pmap_store_pte(pdp
, *pdp
& ~PTE_REF(is_ept
));
2235 ptp
= pmap_pte(p
, pdetova(pdp
- (pt_entry_t
*)p
->dirbase
));
2236 eptp
= ptp
+ NPTEPG
;
2239 * If the pte page has any wired mappings, we cannot
2245 for (ptep
= ptp
; ptep
< eptp
; ptep
++) {
2246 if (iswired(*ptep
)) {
2254 * Remove the virtual addresses mapped by this pte page.
2256 pmap_remove_range(p
,
2257 pdetova(pdp
- (pt_entry_t
*)p
->dirbase
),
2262 * Invalidate the page directory pointer.
2264 pmap_store_pte(pdp
, 0x0);
2269 * And free the pte page itself.
2274 vm_object_lock(p
->pm_obj
);
2276 m
= vm_page_lookup(p
->pm_obj
,(vm_object_offset_t
)(pdp
- (pt_entry_t
*)&p
->dirbase
[0]) * PAGE_SIZE
);
2277 if (m
== VM_PAGE_NULL
)
2278 panic("pmap_collect: pte page not in object");
2280 vm_object_unlock(p
->pm_obj
);
2284 OSAddAtomic(-1, &inuse_ptepages_count
);
2285 PMAP_ZINFO_PFREE(p
, PAGE_SIZE
);
2294 PMAP_UPDATE_TLBS(p
, 0x0, 0xFFFFFFFFFFFFF000ULL
);
2302 pmap_copy_page(ppnum_t src
, ppnum_t dst
)
2304 bcopy_phys((addr64_t
)i386_ptob(src
),
2305 (addr64_t
)i386_ptob(dst
),
2311 * Routine: pmap_pageable
2313 * Make the specified pages (by pmap, offset)
2314 * pageable (or not) as requested.
2316 * A page which is not pageable may not take
2317 * a fault; therefore, its page table entry
2318 * must remain valid for the duration.
2320 * This routine is merely advisory; pmap_enter
2321 * will specify that these pages are to be wired
2322 * down (or not) as appropriate.
2326 __unused pmap_t pmap
,
2327 __unused vm_map_offset_t start_addr
,
2328 __unused vm_map_offset_t end_addr
,
2329 __unused boolean_t pageable
)
2332 pmap
++; start_addr
++; end_addr
++; pageable
++;
2337 invalidate_icache(__unused vm_offset_t addr
,
2338 __unused
unsigned cnt
,
2345 flush_dcache(__unused vm_offset_t addr
,
2346 __unused
unsigned count
,
2354 * Constrain DTrace copyin/copyout actions
2356 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
2357 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
2359 kern_return_t
dtrace_copyio_preflight(__unused addr64_t va
)
2361 thread_t thread
= current_thread();
2363 if (current_map() == kernel_map
)
2364 return KERN_FAILURE
;
2365 else if (((ccr3
= get_cr3_base()) != thread
->map
->pmap
->pm_cr3
) && (no_shared_cr3
== FALSE
))
2366 return KERN_FAILURE
;
2367 else if (no_shared_cr3
&& (ccr3
!= kernel_pmap
->pm_cr3
))
2368 return KERN_FAILURE
;
2370 return KERN_SUCCESS
;
2373 kern_return_t
dtrace_copyio_postflight(__unused addr64_t va
)
2375 return KERN_SUCCESS
;
2377 #endif /* CONFIG_DTRACE */
2379 #include <mach_vm_debug.h>
2381 #include <vm/vm_debug.h>
2384 pmap_list_resident_pages(
2385 __unused pmap_t pmap
,
2386 __unused vm_offset_t
*listp
,
2391 #endif /* MACH_VM_DEBUG */
2395 /* temporary workaround */
2397 coredumpok(__unused vm_map_t map
, __unused vm_offset_t va
)
2402 ptep
= pmap_pte(map
->pmap
, va
);
2405 return ((*ptep
& (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
)) != (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
));
2413 phys_page_exists(ppnum_t pn
)
2415 assert(pn
!= vm_page_fictitious_addr
);
2417 if (!pmap_initialized
)
2420 if (pn
== vm_page_guard_addr
)
2423 if (!IS_MANAGED_PAGE(ppn_to_pai(pn
)))
2432 pmap_switch(pmap_t tpmap
)
2436 s
= splhigh(); /* Make sure interruptions are disabled */
2437 set_dirbase(tpmap
, current_thread(), cpu_number());
2443 * disable no-execute capability on
2444 * the specified pmap
2447 pmap_disable_NX(pmap_t pmap
)
2449 pmap
->nx_enabled
= 0;
2453 pt_fake_zone_init(int zone_index
)
2455 pt_fake_zone_index
= zone_index
;
2461 vm_size_t
*cur_size
,
2462 vm_size_t
*max_size
,
2463 vm_size_t
*elem_size
,
2464 vm_size_t
*alloc_size
,
2470 *count
= inuse_ptepages_count
;
2471 *cur_size
= PAGE_SIZE
* inuse_ptepages_count
;
2472 *max_size
= PAGE_SIZE
* (inuse_ptepages_count
+
2473 vm_page_inactive_count
+
2474 vm_page_active_count
+
2475 vm_page_free_count
);
2476 *elem_size
= PAGE_SIZE
;
2477 *alloc_size
= PAGE_SIZE
;
2478 *sum_size
= alloc_ptepages_count
* PAGE_SIZE
;
2487 pmap_flush_context_init(pmap_flush_context
*pfc
)
2490 pfc
->pfc_invalid_global
= 0;
2493 extern uint64_t TLBTimeOut
;
2496 pmap_flush_context
*pfc
)
2498 unsigned int my_cpu
;
2500 unsigned int cpu_bit
;
2501 cpumask_t cpus_to_respond
= 0;
2502 cpumask_t cpus_to_signal
= 0;
2503 cpumask_t cpus_signaled
= 0;
2504 boolean_t flush_self
= FALSE
;
2507 mp_disable_preemption();
2509 my_cpu
= cpu_number();
2510 cpus_to_signal
= pfc
->pfc_cpus
;
2512 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS
) | DBG_FUNC_START
,
2513 NULL
, cpus_to_signal
);
2515 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
&& cpus_to_signal
; cpu
++, cpu_bit
<<= 1) {
2517 if (cpus_to_signal
& cpu_bit
) {
2519 cpus_to_signal
&= ~cpu_bit
;
2521 if (!cpu_datap(cpu
)->cpu_running
)
2524 if (pfc
->pfc_invalid_global
& cpu_bit
)
2525 cpu_datap(cpu
)->cpu_tlb_invalid_global
= TRUE
;
2527 cpu_datap(cpu
)->cpu_tlb_invalid_local
= TRUE
;
2530 if (cpu
== my_cpu
) {
2534 if (CPU_CR3_IS_ACTIVE(cpu
)) {
2535 cpus_to_respond
|= cpu_bit
;
2536 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2540 cpus_signaled
= cpus_to_respond
;
2543 * Flush local tlb if required.
2544 * Do this now to overlap with other processors responding.
2546 if (flush_self
&& cpu_datap(my_cpu
)->cpu_tlb_invalid
!= FALSE
)
2547 process_pmap_updates();
2549 if (cpus_to_respond
) {
2551 deadline
= mach_absolute_time() +
2552 (TLBTimeOut
? TLBTimeOut
: LockTimeOut
);
2553 boolean_t is_timeout_traced
= FALSE
;
2556 * Wait for those other cpus to acknowledge
2558 while (cpus_to_respond
!= 0) {
2561 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2562 /* Consider checking local/global invalidity
2563 * as appropriate in the PCID case.
2565 if ((cpus_to_respond
& cpu_bit
) != 0) {
2566 if (!cpu_datap(cpu
)->cpu_running
||
2567 cpu_datap(cpu
)->cpu_tlb_invalid
== FALSE
||
2568 !CPU_CR3_IS_ACTIVE(cpu
)) {
2569 cpus_to_respond
&= ~cpu_bit
;
2573 if (cpus_to_respond
== 0)
2576 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2577 if (machine_timeout_suspended())
2579 if (TLBTimeOut
== 0) {
2580 if (is_timeout_traced
)
2583 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS_TO
),
2584 NULL
, cpus_to_signal
, cpus_to_respond
);
2586 is_timeout_traced
= TRUE
;
2589 orig_acks
= NMIPI_acks
;
2590 NMIPI_panic(cpus_to_respond
, TLB_FLUSH_TIMEOUT
);
2591 panic("Uninterruptible processor(s): CPU bitmap: 0x%llx, NMIPI acks: 0x%lx, now: 0x%lx, deadline: %llu",
2592 cpus_to_respond
, orig_acks
, NMIPI_acks
, deadline
);
2597 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS
) | DBG_FUNC_END
,
2598 NULL
, cpus_signaled
, flush_self
);
2600 mp_enable_preemption();
2610 } __attribute__((aligned(16), packed
)) invept_descriptor
= {(uint64_t)eptp
, 0};
2612 __asm__
volatile("invept (%%rax), %%rcx"
2613 : : "c" (PMAP_INVEPT_SINGLE_CONTEXT
), "a" (&invept_descriptor
)
2618 * Called with pmap locked, we:
2619 * - scan through per-cpu data to see which other cpus need to flush
2620 * - send an IPI to each non-idle cpu to be flushed
2621 * - wait for all to signal back that they are inactive or we see that
2622 * they are at a safe point (idle).
2623 * - flush the local tlb if active for this pmap
2624 * - return ... the caller will unlock the pmap
2628 pmap_flush_tlbs(pmap_t pmap
, vm_map_offset_t startv
, vm_map_offset_t endv
, int options
, pmap_flush_context
*pfc
)
2631 unsigned int cpu_bit
;
2632 cpumask_t cpus_to_signal
= 0;
2633 unsigned int my_cpu
= cpu_number();
2634 pmap_paddr_t pmap_cr3
= pmap
->pm_cr3
;
2635 boolean_t flush_self
= FALSE
;
2637 boolean_t pmap_is_shared
= (pmap
->pm_shared
|| (pmap
== kernel_pmap
));
2638 boolean_t need_global_flush
= FALSE
;
2639 uint32_t event_code
;
2640 vm_map_offset_t event_startv
, event_endv
;
2641 boolean_t is_ept
= is_ept_pmap(pmap
);
2643 assert((processor_avail_count
< 2) ||
2644 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
2646 if (pmap
== kernel_pmap
) {
2647 event_code
= PMAP_CODE(PMAP__FLUSH_KERN_TLBS
);
2648 event_startv
= VM_KERNEL_UNSLIDE_OR_PERM(startv
);
2649 event_endv
= VM_KERNEL_UNSLIDE_OR_PERM(endv
);
2650 } else if (is_ept
) {
2651 event_code
= PMAP_CODE(PMAP__FLUSH_EPT
);
2652 event_startv
= startv
;
2655 event_code
= PMAP_CODE(PMAP__FLUSH_TLBS
);
2656 event_startv
= startv
;
2660 PMAP_TRACE_CONSTANT(event_code
| DBG_FUNC_START
,
2661 VM_KERNEL_UNSLIDE_OR_PERM(pmap
), options
,
2662 event_startv
, event_endv
);
2665 mp_cpus_call(CPUMASK_ALL
, ASYNC
, invept
, (void*)pmap
->pm_eptp
);
2670 * Scan other cpus for matching active or task CR3.
2671 * For idle cpus (with no active map) we mark them invalid but
2672 * don't signal -- they'll check as they go busy.
2674 if (pmap_pcid_ncpus
) {
2676 need_global_flush
= TRUE
;
2677 pmap_pcid_invalidate_all_cpus(pmap
);
2680 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2681 if (!cpu_datap(cpu
)->cpu_running
)
2683 uint64_t cpu_active_cr3
= CPU_GET_ACTIVE_CR3(cpu
);
2684 uint64_t cpu_task_cr3
= CPU_GET_TASK_CR3(cpu
);
2685 //recall that the shadowed task cr3 is pre-composed
2686 if ((pmap_cr3
== cpu_task_cr3
) ||
2687 (pmap_cr3
== cpu_active_cr3
) ||
2690 if (options
& PMAP_DELAY_TLB_FLUSH
) {
2691 if (need_global_flush
== TRUE
)
2692 pfc
->pfc_invalid_global
|= cpu_bit
;
2693 pfc
->pfc_cpus
|= cpu_bit
;
2697 if (cpu
== my_cpu
) {
2701 if (need_global_flush
== TRUE
)
2702 cpu_datap(cpu
)->cpu_tlb_invalid_global
= TRUE
;
2704 cpu_datap(cpu
)->cpu_tlb_invalid_local
= TRUE
;
2708 * We don't need to signal processors which will flush
2709 * lazily at the idle state or kernel boundary.
2710 * For example, if we're invalidating the kernel pmap,
2711 * processors currently in userspace don't need to flush
2712 * their TLBs until the next time they enter the kernel.
2713 * Alterations to the address space of a task active
2714 * on a remote processor result in a signal, to
2715 * account for copy operations. (There may be room
2716 * for optimization in such cases).
2717 * The order of the loads below with respect
2718 * to the store to the "cpu_tlb_invalid" field above
2719 * is important--hence the barrier.
2721 if (CPU_CR3_IS_ACTIVE(cpu
) &&
2722 (pmap_cr3
== CPU_GET_ACTIVE_CR3(cpu
) ||
2724 (pmap_cr3
== CPU_GET_TASK_CR3(cpu
)))) {
2725 cpus_to_signal
|= cpu_bit
;
2726 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2730 if ((options
& PMAP_DELAY_TLB_FLUSH
))
2734 * Flush local tlb if required.
2735 * Do this now to overlap with other processors responding.
2738 if (pmap_pcid_ncpus
) {
2739 pmap_pcid_validate_cpu(pmap
, my_cpu
);
2749 if (cpus_to_signal
) {
2750 cpumask_t cpus_to_respond
= cpus_to_signal
;
2752 deadline
= mach_absolute_time() +
2753 (TLBTimeOut
? TLBTimeOut
: LockTimeOut
);
2754 boolean_t is_timeout_traced
= FALSE
;
2757 * Wait for those other cpus to acknowledge
2759 while (cpus_to_respond
!= 0) {
2762 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2763 /* Consider checking local/global invalidity
2764 * as appropriate in the PCID case.
2766 if ((cpus_to_respond
& cpu_bit
) != 0) {
2767 if (!cpu_datap(cpu
)->cpu_running
||
2768 cpu_datap(cpu
)->cpu_tlb_invalid
== FALSE
||
2769 !CPU_CR3_IS_ACTIVE(cpu
)) {
2770 cpus_to_respond
&= ~cpu_bit
;
2774 if (cpus_to_respond
== 0)
2777 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2778 if (machine_timeout_suspended())
2780 if (TLBTimeOut
== 0) {
2781 /* cut tracepoint but don't panic */
2782 if (is_timeout_traced
)
2785 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS_TO
),
2786 VM_KERNEL_UNSLIDE_OR_PERM(pmap
),
2790 is_timeout_traced
= TRUE
;
2793 orig_acks
= NMIPI_acks
;
2795 NMIPI_panic(cpus_to_respond
, TLB_FLUSH_TIMEOUT
);
2796 panic("TLB invalidation IPI timeout, unresponsive CPU bitmap: 0x%llx, NMIPI acks: 0x%lx, now: 0x%lx, deadline: %llu",
2797 cpus_to_respond
, orig_acks
, NMIPI_acks
, deadline
);
2802 if (__improbable((pmap
== kernel_pmap
) && (flush_self
!= TRUE
))) {
2803 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
);
2807 PMAP_TRACE_CONSTANT(event_code
| DBG_FUNC_END
,
2808 VM_KERNEL_UNSLIDE_OR_PERM(pmap
), cpus_to_signal
,
2809 event_startv
, event_endv
);
2814 process_pmap_updates(void)
2816 int ccpu
= cpu_number();
2817 pmap_assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2818 if (pmap_pcid_ncpus
) {
2819 pmap_pcid_validate_current();
2820 cpu_datap(ccpu
)->cpu_tlb_invalid
= FALSE
;
2823 current_cpu_datap()->cpu_tlb_invalid
= FALSE
;
2831 pmap_update_interrupt(void)
2833 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_START
);
2835 if (current_cpu_datap()->cpu_tlb_invalid
)
2836 process_pmap_updates();
2838 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_END
);
2841 #include <mach/mach_vm.h> /* mach_vm_region_recurse() */
2842 /* Scan kernel pmap for W+X PTEs, scan kernel VM map for W+X map entries
2843 * and identify ranges with mismatched VM permissions and PTE permissions
2846 pmap_permissions_verify(pmap_t ipmap
, vm_map_t ivmmap
, vm_offset_t sv
, vm_offset_t ev
) {
2847 vm_offset_t cv
= sv
;
2848 kern_return_t rv
= KERN_SUCCESS
;
2849 uint64_t skip4
= 0, skip2
= 0;
2851 assert(!is_ept_pmap(ipmap
));
2853 sv
&= ~PAGE_MASK_64
;
2854 ev
&= ~PAGE_MASK_64
;
2856 if (__improbable((cv
> 0x00007FFFFFFFFFFFULL
) &&
2857 (cv
< 0xFFFF800000000000ULL
))) {
2858 cv
= 0xFFFF800000000000ULL
;
2860 /* Potential inconsistencies from not holding pmap lock
2861 * but harmless for the moment.
2863 if (((cv
& PML4MASK
) == 0) && (pmap64_pml4(ipmap
, cv
) == 0)) {
2864 if ((cv
+ NBPML4
) > cv
)
2871 if (((cv
& PDMASK
) == 0) && (pmap_pde(ipmap
, cv
) == 0)) {
2872 if ((cv
+ NBPD
) > cv
)
2880 pt_entry_t
*ptep
= pmap_pte(ipmap
, cv
);
2881 if (ptep
&& (*ptep
& INTEL_PTE_VALID
)) {
2882 if (*ptep
& INTEL_PTE_WRITE
) {
2883 if (!(*ptep
& INTEL_PTE_NX
)) {
2884 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
), *pmap64_pde(ipmap
, cv
), *ptep
, pmap_valid_page((ppnum_t
)(i386_btop(pte_to_pa(*ptep
)))));
2891 kprintf("Completed pmap scan\n");
2894 struct vm_region_submap_info_64 vbr
;
2895 mach_msg_type_number_t vbrcount
= 0;
2896 mach_vm_size_t vmsize
;
2898 uint32_t nesting_depth
= 0;
2904 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
2905 if((kret
= mach_vm_region_recurse(ivmmap
,
2906 (mach_vm_address_t
*) &cv
, &vmsize
, &nesting_depth
,
2907 (vm_region_recurse_info_t
)&vbr
,
2908 &vbrcount
)) != KERN_SUCCESS
) {
2920 if(kret
!= KERN_SUCCESS
)
2923 prot
= vbr
.protection
;
2925 if ((prot
& (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) == (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) {
2926 kprintf("W+X map entry at address 0x%lx\n", cv
);
2932 for (pcv
= cv
; pcv
< cv
+ vmsize
; pcv
+= PAGE_SIZE
) {
2933 pt_entry_t
*ptep
= pmap_pte(ipmap
, pcv
);
2936 if ((ptep
== NULL
) || !(*ptep
& INTEL_PTE_VALID
))
2938 tprot
= VM_PROT_READ
;
2939 if (*ptep
& INTEL_PTE_WRITE
)
2940 tprot
|= VM_PROT_WRITE
;
2941 if ((*ptep
& INTEL_PTE_NX
) == 0)
2942 tprot
|= VM_PROT_EXECUTE
;
2943 if (tprot
!= prot
) {
2944 kprintf("PTE/map entry permissions mismatch at address 0x%lx, pte: 0x%llx, protection: 0x%x\n", pcv
, *ptep
, prot
);
2955 extern int pmap_ledgers_panic
;
2960 ledger_amount_t bal
;
2965 if (pmap
->pmap_pid
== 0) {
2967 * This pmap was not or is no longer fully associated
2968 * with a task (e.g. the old pmap after a fork()/exec() or
2969 * spawn()). Its "ledger" still points at a task that is
2970 * now using a different (and active) address space, so
2971 * we can't check that all the pmap ledgers are balanced here.
2973 * If the "pid" is set, that means that we went through
2974 * pmap_set_process() in task_terminate_internal(), so
2975 * this task's ledger should not have been re-used and
2976 * all the pmap ledgers should be back to 0.
2982 pid
= pmap
->pmap_pid
;
2983 procname
= pmap
->pmap_procname
;
2985 pmap_ledgers_drift
.num_pmaps_checked
++;
2987 ledger_get_balance(pmap
->ledger
,
2988 task_ledgers
.phys_footprint
,
2992 printf("LEDGER BALANCE proc %d (%s) "
2993 "\"phys_footprint\" = %lld\n",
2994 pid
, procname
, bal
);
2996 pmap_ledgers_drift
.phys_footprint_over
++;
2997 pmap_ledgers_drift
.phys_footprint_over_total
+= bal
;
2998 if (bal
> pmap_ledgers_drift
.phys_footprint_over_max
) {
2999 pmap_ledgers_drift
.phys_footprint_over_max
= bal
;
3002 pmap_ledgers_drift
.phys_footprint_under
++;
3003 pmap_ledgers_drift
.phys_footprint_under_total
+= bal
;
3004 if (bal
< pmap_ledgers_drift
.phys_footprint_under_max
) {
3005 pmap_ledgers_drift
.phys_footprint_under_max
= bal
;
3009 ledger_get_balance(pmap
->ledger
,
3010 task_ledgers
.internal
,
3014 printf("LEDGER BALANCE proc %d (%s) "
3015 "\"internal\" = %lld\n",
3016 pid
, procname
, bal
);
3018 pmap_ledgers_drift
.internal_over
++;
3019 pmap_ledgers_drift
.internal_over_total
+= bal
;
3020 if (bal
> pmap_ledgers_drift
.internal_over_max
) {
3021 pmap_ledgers_drift
.internal_over_max
= bal
;
3024 pmap_ledgers_drift
.internal_under
++;
3025 pmap_ledgers_drift
.internal_under_total
+= bal
;
3026 if (bal
< pmap_ledgers_drift
.internal_under_max
) {
3027 pmap_ledgers_drift
.internal_under_max
= bal
;
3031 ledger_get_balance(pmap
->ledger
,
3032 task_ledgers
.internal_compressed
,
3036 printf("LEDGER BALANCE proc %d (%s) "
3037 "\"internal_compressed\" = %lld\n",
3038 pid
, procname
, bal
);
3040 pmap_ledgers_drift
.internal_compressed_over
++;
3041 pmap_ledgers_drift
.internal_compressed_over_total
+= bal
;
3042 if (bal
> pmap_ledgers_drift
.internal_compressed_over_max
) {
3043 pmap_ledgers_drift
.internal_compressed_over_max
= bal
;
3046 pmap_ledgers_drift
.internal_compressed_under
++;
3047 pmap_ledgers_drift
.internal_compressed_under_total
+= bal
;
3048 if (bal
< pmap_ledgers_drift
.internal_compressed_under_max
) {
3049 pmap_ledgers_drift
.internal_compressed_under_max
= bal
;
3053 ledger_get_balance(pmap
->ledger
,
3054 task_ledgers
.iokit_mapped
,
3058 printf("LEDGER BALANCE proc %d (%s) "
3059 "\"iokit_mapped\" = %lld\n",
3060 pid
, procname
, bal
);
3062 pmap_ledgers_drift
.iokit_mapped_over
++;
3063 pmap_ledgers_drift
.iokit_mapped_over_total
+= bal
;
3064 if (bal
> pmap_ledgers_drift
.iokit_mapped_over_max
) {
3065 pmap_ledgers_drift
.iokit_mapped_over_max
= bal
;
3068 pmap_ledgers_drift
.iokit_mapped_under
++;
3069 pmap_ledgers_drift
.iokit_mapped_under_total
+= bal
;
3070 if (bal
< pmap_ledgers_drift
.iokit_mapped_under_max
) {
3071 pmap_ledgers_drift
.iokit_mapped_under_max
= bal
;
3075 ledger_get_balance(pmap
->ledger
,
3076 task_ledgers
.alternate_accounting
,
3080 printf("LEDGER BALANCE proc %d (%s) "
3081 "\"alternate_accounting\" = %lld\n",
3082 pid
, procname
, bal
);
3084 pmap_ledgers_drift
.alternate_accounting_over
++;
3085 pmap_ledgers_drift
.alternate_accounting_over_total
+= bal
;
3086 if (bal
> pmap_ledgers_drift
.alternate_accounting_over_max
) {
3087 pmap_ledgers_drift
.alternate_accounting_over_max
= bal
;
3090 pmap_ledgers_drift
.alternate_accounting_under
++;
3091 pmap_ledgers_drift
.alternate_accounting_under_total
+= bal
;
3092 if (bal
< pmap_ledgers_drift
.alternate_accounting_under_max
) {
3093 pmap_ledgers_drift
.alternate_accounting_under_max
= bal
;
3097 ledger_get_balance(pmap
->ledger
,
3098 task_ledgers
.alternate_accounting_compressed
,
3102 printf("LEDGER BALANCE proc %d (%s) "
3103 "\"alternate_accounting_compressed\" = %lld\n",
3104 pid
, procname
, bal
);
3106 pmap_ledgers_drift
.alternate_accounting_compressed_over
++;
3107 pmap_ledgers_drift
.alternate_accounting_compressed_over_total
+= bal
;
3108 if (bal
> pmap_ledgers_drift
.alternate_accounting_compressed_over_max
) {
3109 pmap_ledgers_drift
.alternate_accounting_compressed_over_max
= bal
;
3112 pmap_ledgers_drift
.alternate_accounting_compressed_under
++;
3113 pmap_ledgers_drift
.alternate_accounting_compressed_under_total
+= bal
;
3114 if (bal
< pmap_ledgers_drift
.alternate_accounting_compressed_under_max
) {
3115 pmap_ledgers_drift
.alternate_accounting_compressed_under_max
= bal
;
3119 ledger_get_balance(pmap
->ledger
,
3120 task_ledgers
.page_table
,
3124 printf("LEDGER BALANCE proc %d (%s) "
3125 "\"page_table\" = %lld\n",
3126 pid
, procname
, bal
);
3128 pmap_ledgers_drift
.page_table_over
++;
3129 pmap_ledgers_drift
.page_table_over_total
+= bal
;
3130 if (bal
> pmap_ledgers_drift
.page_table_over_max
) {
3131 pmap_ledgers_drift
.page_table_over_max
= bal
;
3134 pmap_ledgers_drift
.page_table_under
++;
3135 pmap_ledgers_drift
.page_table_under_total
+= bal
;
3136 if (bal
< pmap_ledgers_drift
.page_table_under_max
) {
3137 pmap_ledgers_drift
.page_table_under_max
= bal
;
3141 ledger_get_balance(pmap
->ledger
,
3142 task_ledgers
.purgeable_volatile
,
3146 printf("LEDGER BALANCE proc %d (%s) "
3147 "\"purgeable_volatile\" = %lld\n",
3148 pid
, procname
, bal
);
3150 pmap_ledgers_drift
.purgeable_volatile_over
++;
3151 pmap_ledgers_drift
.purgeable_volatile_over_total
+= bal
;
3152 if (bal
> pmap_ledgers_drift
.purgeable_volatile_over_max
) {
3153 pmap_ledgers_drift
.purgeable_volatile_over_max
= bal
;
3156 pmap_ledgers_drift
.purgeable_volatile_under
++;
3157 pmap_ledgers_drift
.purgeable_volatile_under_total
+= bal
;
3158 if (bal
< pmap_ledgers_drift
.purgeable_volatile_under_max
) {
3159 pmap_ledgers_drift
.purgeable_volatile_under_max
= bal
;
3163 ledger_get_balance(pmap
->ledger
,
3164 task_ledgers
.purgeable_nonvolatile
,
3168 printf("LEDGER BALANCE proc %d (%s) "
3169 "\"purgeable_nonvolatile\" = %lld\n",
3170 pid
, procname
, bal
);
3172 pmap_ledgers_drift
.purgeable_nonvolatile_over
++;
3173 pmap_ledgers_drift
.purgeable_nonvolatile_over_total
+= bal
;
3174 if (bal
> pmap_ledgers_drift
.purgeable_nonvolatile_over_max
) {
3175 pmap_ledgers_drift
.purgeable_nonvolatile_over_max
= bal
;
3178 pmap_ledgers_drift
.purgeable_nonvolatile_under
++;
3179 pmap_ledgers_drift
.purgeable_nonvolatile_under_total
+= bal
;
3180 if (bal
< pmap_ledgers_drift
.purgeable_nonvolatile_under_max
) {
3181 pmap_ledgers_drift
.purgeable_nonvolatile_under_max
= bal
;
3185 ledger_get_balance(pmap
->ledger
,
3186 task_ledgers
.purgeable_volatile_compressed
,
3190 printf("LEDGER BALANCE proc %d (%s) "
3191 "\"purgeable_volatile_compressed\" = %lld\n",
3192 pid
, procname
, bal
);
3194 pmap_ledgers_drift
.purgeable_volatile_compressed_over
++;
3195 pmap_ledgers_drift
.purgeable_volatile_compressed_over_total
+= bal
;
3196 if (bal
> pmap_ledgers_drift
.purgeable_volatile_compressed_over_max
) {
3197 pmap_ledgers_drift
.purgeable_volatile_compressed_over_max
= bal
;
3200 pmap_ledgers_drift
.purgeable_volatile_compressed_under
++;
3201 pmap_ledgers_drift
.purgeable_volatile_compressed_under_total
+= bal
;
3202 if (bal
< pmap_ledgers_drift
.purgeable_volatile_compressed_under_max
) {
3203 pmap_ledgers_drift
.purgeable_volatile_compressed_under_max
= bal
;
3207 ledger_get_balance(pmap
->ledger
,
3208 task_ledgers
.purgeable_nonvolatile_compressed
,
3212 printf("LEDGER BALANCE proc %d (%s) "
3213 "\"purgeable_nonvolatile_compressed\" = %lld\n",
3214 pid
, procname
, bal
);
3216 pmap_ledgers_drift
.purgeable_nonvolatile_compressed_over
++;
3217 pmap_ledgers_drift
.purgeable_nonvolatile_compressed_over_total
+= bal
;
3218 if (bal
> pmap_ledgers_drift
.purgeable_nonvolatile_compressed_over_max
) {
3219 pmap_ledgers_drift
.purgeable_nonvolatile_compressed_over_max
= bal
;
3222 pmap_ledgers_drift
.purgeable_nonvolatile_compressed_under
++;
3223 pmap_ledgers_drift
.purgeable_nonvolatile_compressed_under_total
+= bal
;
3224 if (bal
< pmap_ledgers_drift
.purgeable_nonvolatile_compressed_under_max
) {
3225 pmap_ledgers_drift
.purgeable_nonvolatile_compressed_under_max
= bal
;
3231 if (pmap_ledgers_panic
) {
3232 panic("pmap_destroy(%p) %d[%s] has imbalanced ledgers\n",
3233 pmap
, pid
, procname
);
3235 printf("pmap_destroy(%p) %d[%s] has imbalanced ledgers\n",
3236 pmap
, pid
, procname
);
3240 if (pmap
->stats
.resident_count
!= 0 ||
3243 * "wired_count" is unfortunately a bit inaccurate, so let's
3244 * tolerate some slight deviation to limit the amount of
3245 * somewhat-spurious assertion failures.
3247 pmap
->stats
.wired_count
> 10 ||
3248 #else /* 35156815 */
3249 pmap
->stats
.wired_count
!= 0 ||
3250 #endif /* 35156815 */
3251 pmap
->stats
.device
!= 0 ||
3252 pmap
->stats
.internal
!= 0 ||
3253 pmap
->stats
.external
!= 0 ||
3254 pmap
->stats
.reusable
!= 0 ||
3255 pmap
->stats
.compressed
!= 0) {
3256 if (pmap_stats_assert
) {
3257 panic("pmap_destroy(%p) %d[%s] imbalanced stats: resident=%d wired=%d device=%d internal=%d external=%d reusable=%d compressed=%lld",
3258 pmap
, pid
, procname
,
3259 pmap
->stats
.resident_count
,
3260 pmap
->stats
.wired_count
,
3262 pmap
->stats
.internal
,
3263 pmap
->stats
.external
,
3264 pmap
->stats
.reusable
,
3265 pmap
->stats
.compressed
);
3267 printf("pmap_destroy(%p) %d[%s] imbalanced stats: resident=%d wired=%d device=%d internal=%d external=%d reusable=%d compressed=%lld",
3268 pmap
, pid
, procname
,
3269 pmap
->stats
.resident_count
,
3270 pmap
->stats
.wired_count
,
3272 pmap
->stats
.internal
,
3273 pmap
->stats
.external
,
3274 pmap
->stats
.reusable
,
3275 pmap
->stats
.compressed
);
3289 pmap
->pmap_pid
= pid
;
3290 strlcpy(pmap
->pmap_procname
, procname
, sizeof (pmap
->pmap_procname
));
3292 #endif /* MACH_ASSERT */
3295 #if DEVELOPMENT || DEBUG
3296 int pmap_pagezero_mitigation
= 1;
3299 void pmap_advise_pagezero_range(pmap_t lpmap
, uint64_t low_bound
) {
3300 #if DEVELOPMENT || DEBUG
3301 if (pmap_pagezero_mitigation
== 0) {
3302 lpmap
->pagezero_accessible
= FALSE
;
3306 lpmap
->pagezero_accessible
= ((pmap_smap_enabled
== FALSE
) && (low_bound
< 0x1000));
3307 if (lpmap
== current_pmap()) {
3308 mp_disable_preemption();
3309 current_cpu_datap()->cpu_pagezero_mapped
= lpmap
->pagezero_accessible
;
3310 mp_enable_preemption();
3314 void pmap_verify_noncacheable(uintptr_t vaddr
) {
3315 pt_entry_t
*ptep
= NULL
;
3316 ptep
= pmap_pte(kernel_pmap
, vaddr
);
3318 panic("pmap_verify_noncacheable: no translation for 0x%lx", vaddr
);
3320 /* Non-cacheable OK */
3321 if (*ptep
& (INTEL_PTE_NCACHE
))
3323 /* Write-combined OK */
3324 if (*ptep
& (INTEL_PTE_PTA
))
3326 panic("pmap_verify_noncacheable: IO read from a cacheable address? address: 0x%lx, PTE: %p, *PTE: 0x%llx", vaddr
, ptep
, *ptep
);