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>
143 #include <i386/mp_desc.h>
144 #include <libkern/kernel_mach_header.h>
146 #include <pexpert/i386/efi.h>
152 #define POSTCODE_DELAY 1
153 #include <i386/postcode.h>
154 #endif /* IWANTTODEBUG */
157 #define DBG(x...) kprintf("DBG: " x)
161 /* Compile time assert to ensure adjacency/alignment of per-CPU data fields used
162 * in the trampolines for kernel/user boundary TLB coherency.
164 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];
165 boolean_t pmap_trace
= FALSE
;
167 boolean_t no_shared_cr3
= DEBUG
; /* TRUE for DEBUG by default */
169 int nx_enabled
= 1; /* enable no-execute protection */
170 int allow_data_exec
= VM_ABI_32
; /* 32-bit apps may execute data by default, 64-bit apps may not */
171 int allow_stack_exec
= 0; /* No apps may execute from the stack by default */
173 const boolean_t cpu_64bit
= TRUE
; /* Mais oui! */
175 uint64_t max_preemption_latency_tsc
= 0;
177 pv_hashed_entry_t
*pv_hash_table
; /* hash lists */
179 uint32_t npvhashmask
= 0, npvhashbuckets
= 0;
181 pv_hashed_entry_t pv_hashed_free_list
= PV_HASHED_ENTRY_NULL
;
182 pv_hashed_entry_t pv_hashed_kern_free_list
= PV_HASHED_ENTRY_NULL
;
183 decl_simple_lock_data(,pv_hashed_free_list_lock
)
184 decl_simple_lock_data(,pv_hashed_kern_free_list_lock
)
185 decl_simple_lock_data(,pv_hash_table_lock
)
187 decl_simple_lock_data(,phys_backup_lock
)
189 zone_t pv_hashed_list_zone
; /* zone of pv_hashed_entry structures */
192 * First and last physical addresses that we maintain any information
193 * for. Initialized to zero so that pmap operations done before
194 * pmap_init won't touch any non-existent structures.
196 boolean_t pmap_initialized
= FALSE
;/* Has pmap_init completed? */
198 static struct vm_object kptobj_object_store
;
199 static struct vm_object kpml4obj_object_store
;
200 static struct vm_object kpdptobj_object_store
;
203 * Array of physical page attribites for managed pages.
204 * One byte per physical page.
206 char *pmap_phys_attributes
;
207 ppnum_t last_managed_page
= 0;
210 * Amount of virtual memory mapped by one
211 * page-directory entry.
214 uint64_t pde_mapped_size
= PDE_MAPPED_SIZE
;
216 unsigned pmap_memory_region_count
;
217 unsigned pmap_memory_region_current
;
219 pmap_memory_region_t pmap_memory_regions
[PMAP_MEMORY_REGIONS_SIZE
];
222 * Other useful macros.
224 #define current_pmap() (vm_map_pmap(current_thread()->map))
226 struct pmap kernel_pmap_store
;
229 struct zone
*pmap_zone
; /* zone of pmap structures */
231 struct zone
*pmap_anchor_zone
;
232 int pmap_debug
= 0; /* flag for debugging prints */
234 unsigned int inuse_ptepages_count
= 0;
235 long long alloc_ptepages_count
__attribute__((aligned(8))) = 0; /* aligned for atomic access */
236 unsigned int bootstrap_wired_pages
= 0;
237 int pt_fake_zone_index
= -1;
239 extern long NMIPI_acks
;
241 boolean_t kernel_text_ps_4K
= TRUE
;
242 boolean_t wpkernel
= TRUE
;
248 pt_entry_t
*DMAP1
, *DMAP2
;
252 boolean_t pmap_disable_kheap_nx
= FALSE
;
253 boolean_t pmap_disable_kstack_nx
= FALSE
;
254 extern boolean_t doconstro_override
;
256 extern long __stack_chk_guard
[];
258 boolean_t pmap_ept_support_ad
= FALSE
;
262 * Map memory at initialization. The physical addresses being
263 * mapped are not managed and are never unmapped.
265 * For now, VM is already on, we only need to map the
271 vm_map_offset_t start_addr
,
272 vm_map_offset_t end_addr
,
279 while (start_addr
< end_addr
) {
280 pmap_enter(kernel_pmap
, (vm_map_offset_t
)virt
,
281 (ppnum_t
) i386_btop(start_addr
), prot
, VM_PROT_NONE
, flags
, TRUE
);
288 extern char *first_avail
;
289 extern vm_offset_t virtual_avail
, virtual_end
;
290 extern pmap_paddr_t avail_start
, avail_end
;
291 extern vm_offset_t sHIB
;
292 extern vm_offset_t eHIB
;
293 extern vm_offset_t stext
;
294 extern vm_offset_t etext
;
295 extern vm_offset_t sdata
, edata
;
296 extern vm_offset_t sconstdata
, econstdata
;
298 extern void *KPTphys
;
300 boolean_t pmap_smep_enabled
= FALSE
;
301 boolean_t pmap_smap_enabled
= FALSE
;
306 cpu_data_t
*cdp
= current_cpu_datap();
308 * Here early in the life of a processor (from cpu_mode_init()).
309 * Ensure global page feature is disabled at this point.
312 set_cr4(get_cr4() &~ CR4_PGE
);
315 * Initialize the per-cpu, TLB-related fields.
317 cdp
->cpu_kernel_cr3
= kernel_pmap
->pm_cr3
;
318 cdp
->cpu_active_cr3
= kernel_pmap
->pm_cr3
;
319 cdp
->cpu_tlb_invalid
= FALSE
;
320 cdp
->cpu_task_map
= TASK_MAP_64BIT
;
321 pmap_pcid_configure();
322 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMEP
) {
324 if (!PE_parse_boot_argn("-pmap_smep_disable", &nsmep
, sizeof(nsmep
))) {
325 set_cr4(get_cr4() | CR4_SMEP
);
326 pmap_smep_enabled
= TRUE
;
329 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMAP
) {
331 if (!PE_parse_boot_argn("-pmap_smap_disable", &nsmap
, sizeof(nsmap
))) {
332 set_cr4(get_cr4() | CR4_SMAP
);
333 pmap_smap_enabled
= TRUE
;
337 if (cdp
->cpu_fixed_pmcs_enabled
) {
338 boolean_t enable
= TRUE
;
339 cpu_pmc_control(&enable
);
343 static uint32_t pmap_scale_shift(void) {
346 if (sane_size
<= 8*GB
) {
347 scale
= (uint32_t)(sane_size
/ (2 * GB
));
348 } else if (sane_size
<= 32*GB
) {
349 scale
= 4 + (uint32_t)((sane_size
- (8 * GB
))/ (4 * GB
));
351 scale
= 10 + (uint32_t)MIN(4, ((sane_size
- (32 * GB
))/ (8 * GB
)));
357 * Bootstrap the system enough to run with virtual memory.
358 * Map the kernel's code and data, and allocate the system page table.
359 * Called with mapping OFF. Page_size must already be set.
364 __unused vm_offset_t load_start
,
365 __unused boolean_t IA32e
)
367 #if NCOPY_WINDOWS > 0
373 vm_last_addr
= VM_MAX_KERNEL_ADDRESS
; /* Set the highest address
376 * The kernel's pmap is statically allocated so we don't
377 * have to use pmap_create, which is unlikely to work
378 * correctly at this part of the boot sequence.
381 kernel_pmap
= &kernel_pmap_store
;
382 kernel_pmap
->ref_count
= 1;
383 kernel_pmap
->nx_enabled
= TRUE
;
384 kernel_pmap
->pm_task_map
= TASK_MAP_64BIT
;
385 kernel_pmap
->pm_obj
= (vm_object_t
) NULL
;
386 kernel_pmap
->dirbase
= (pd_entry_t
*)((uintptr_t)IdlePTD
);
387 kernel_pmap
->pm_pdpt
= (pd_entry_t
*) ((uintptr_t)IdlePDPT
);
388 kernel_pmap
->pm_pml4
= IdlePML4
;
389 kernel_pmap
->pm_cr3
= (uintptr_t)ID_MAP_VTOP(IdlePML4
);
390 kernel_pmap
->pm_eptp
= 0;
391 pmap_pcid_initialize_kernel(kernel_pmap
);
395 current_cpu_datap()->cpu_kernel_cr3
= (addr64_t
) kernel_pmap
->pm_cr3
;
398 OSAddAtomic(NKPT
, &inuse_ptepages_count
);
399 OSAddAtomic64(NKPT
, &alloc_ptepages_count
);
400 bootstrap_wired_pages
= NKPT
;
402 virtual_avail
= (vm_offset_t
)(VM_MIN_KERNEL_ADDRESS
) + (vm_offset_t
)first_avail
;
403 virtual_end
= (vm_offset_t
)(VM_MAX_KERNEL_ADDRESS
);
405 #if NCOPY_WINDOWS > 0
407 * Reserve some special page table entries/VA space for temporary
410 #define SYSMAP(c, p, v, n) \
411 v = (c)va; va += ((n)*INTEL_PGBYTES);
415 for (i
=0; i
<PMAP_NWINDOWS
; i
++) {
417 kprintf("trying to do SYSMAP idx %d %p\n", i
,
418 current_cpu_datap());
419 kprintf("cpu_pmap %p\n", current_cpu_datap()->cpu_pmap
);
420 kprintf("mapwindow %p\n", current_cpu_datap()->cpu_pmap
->mapwindow
);
421 kprintf("two stuff %p %p\n",
422 (void *)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
),
423 (void *)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
));
426 (current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
),
427 (current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
),
429 current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
=
430 &(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP_store
);
431 *current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
= 0;
434 /* DMAP user for debugger */
435 SYSMAP(caddr_t
, DMAP1
, DADDR1
, 1);
436 SYSMAP(caddr_t
, DMAP2
, DADDR2
, 1); /* XXX temporary - can remove */
440 if (!PE_parse_boot_argn("npvhash", &npvhashmask
, sizeof (npvhashmask
))) {
441 npvhashmask
= ((NPVHASHBUCKETS
) << pmap_scale_shift()) - 1;
445 npvhashbuckets
= npvhashmask
+ 1;
447 if (0 != ((npvhashbuckets
) & npvhashmask
)) {
448 panic("invalid hash %d, must be ((2^N)-1), "
449 "using default %d\n", npvhashmask
, NPVHASHMASK
);
452 simple_lock_init(&kernel_pmap
->lock
, 0);
453 simple_lock_init(&pv_hashed_free_list_lock
, 0);
454 simple_lock_init(&pv_hashed_kern_free_list_lock
, 0);
455 simple_lock_init(&pv_hash_table_lock
,0);
456 simple_lock_init(&phys_backup_lock
, 0);
461 printf("PMAP: PCID enabled\n");
463 if (pmap_smep_enabled
)
464 printf("PMAP: Supervisor Mode Execute Protection enabled\n");
465 if (pmap_smap_enabled
)
466 printf("PMAP: Supervisor Mode Access Protection enabled\n");
469 printf("Stack canary: 0x%lx\n", __stack_chk_guard
[0]);
470 printf("early_random(): 0x%qx\n", early_random());
473 /* Check if the user has requested disabling stack or heap no-execute
474 * enforcement. These are "const" variables; that qualifier is cast away
475 * when altering them. The TEXT/DATA const sections are marked
476 * write protected later in the kernel startup sequence, so altering
477 * them is possible at this point, in pmap_bootstrap().
479 if (PE_parse_boot_argn("-pmap_disable_kheap_nx", &ptmp
, sizeof(ptmp
))) {
480 boolean_t
*pdknxp
= (boolean_t
*) &pmap_disable_kheap_nx
;
484 if (PE_parse_boot_argn("-pmap_disable_kstack_nx", &ptmp
, sizeof(ptmp
))) {
485 boolean_t
*pdknhp
= (boolean_t
*) &pmap_disable_kstack_nx
;
489 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
490 if (args
->efiMode
== kBootArgsEfiMode32
) {
491 printf("EFI32: kernel virtual space limited to 4GB\n");
492 virtual_end
= VM_MAX_KERNEL_ADDRESS_EFI32
;
494 kprintf("Kernel virtual space from 0x%lx to 0x%lx.\n",
495 (long)KERNEL_BASE
, (long)virtual_end
);
496 kprintf("Available physical space from 0x%llx to 0x%llx\n",
497 avail_start
, avail_end
);
500 * The -no_shared_cr3 boot-arg is a debugging feature (set by default
501 * in the DEBUG kernel) to force the kernel to switch to its own map
502 * (and cr3) when control is in kernelspace. The kernel's map does not
503 * include (i.e. share) userspace so wild references will cause
504 * a panic. Only copyin and copyout are exempt from this.
506 (void) PE_parse_boot_argn("-no_shared_cr3",
507 &no_shared_cr3
, sizeof (no_shared_cr3
));
509 kprintf("Kernel not sharing user map\n");
512 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace
, sizeof (pmap_trace
))) {
513 kprintf("Kernel traces for pmap operations enabled\n");
515 #endif /* PMAP_TRACES */
523 *startp
= virtual_avail
;
532 #include <IOKit/IOHibernatePrivate.h>
535 int32_t pmap_teardown_last_valid_compact_indx
= -1;
538 void hibernate_rebuild_pmap_structs(void);
539 void hibernate_teardown_pmap_structs(addr64_t
*, addr64_t
*);
540 void pmap_pack_index(uint32_t);
541 int32_t pmap_unpack_index(pv_rooted_entry_t
);
545 pmap_unpack_index(pv_rooted_entry_t pv_h
)
549 indx
= (int32_t)(*((uint64_t *)(&pv_h
->qlink
.next
)) >> 48);
551 indx
|= (int32_t)(*((uint64_t *)(&pv_h
->qlink
.prev
)) >> 48);
553 *((uint64_t *)(&pv_h
->qlink
.next
)) |= ((uint64_t)0xffff << 48);
554 *((uint64_t *)(&pv_h
->qlink
.prev
)) |= ((uint64_t)0xffff << 48);
561 pmap_pack_index(uint32_t indx
)
563 pv_rooted_entry_t pv_h
;
565 pv_h
= &pv_head_table
[indx
];
567 *((uint64_t *)(&pv_h
->qlink
.next
)) &= ~((uint64_t)0xffff << 48);
568 *((uint64_t *)(&pv_h
->qlink
.prev
)) &= ~((uint64_t)0xffff << 48);
570 *((uint64_t *)(&pv_h
->qlink
.next
)) |= ((uint64_t)(indx
>> 16)) << 48;
571 *((uint64_t *)(&pv_h
->qlink
.prev
)) |= ((uint64_t)(indx
& 0xffff)) << 48;
576 hibernate_teardown_pmap_structs(addr64_t
*unneeded_start
, addr64_t
*unneeded_end
)
579 int32_t compact_target_indx
;
581 compact_target_indx
= 0;
583 for (i
= 0; i
< pmap_npages
; i
++) {
584 if (pv_head_table
[i
].pmap
== PMAP_NULL
) {
586 if (pv_head_table
[compact_target_indx
].pmap
!= PMAP_NULL
)
587 compact_target_indx
= i
;
589 pmap_pack_index((uint32_t)i
);
591 if (pv_head_table
[compact_target_indx
].pmap
== PMAP_NULL
) {
593 * we've got a hole to fill, so
594 * move this pv_rooted_entry_t to it's new home
596 pv_head_table
[compact_target_indx
] = pv_head_table
[i
];
597 pv_head_table
[i
].pmap
= PMAP_NULL
;
599 pmap_teardown_last_valid_compact_indx
= compact_target_indx
;
600 compact_target_indx
++;
602 pmap_teardown_last_valid_compact_indx
= i
;
605 *unneeded_start
= (addr64_t
)&pv_head_table
[pmap_teardown_last_valid_compact_indx
+1];
606 *unneeded_end
= (addr64_t
)&pv_head_table
[pmap_npages
-1];
608 HIBLOG("hibernate_teardown_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx
);
613 hibernate_rebuild_pmap_structs(void)
615 int32_t cindx
, eindx
, rindx
;
616 pv_rooted_entry_t pv_h
;
618 eindx
= (int32_t)pmap_npages
;
620 for (cindx
= pmap_teardown_last_valid_compact_indx
; cindx
>= 0; cindx
--) {
622 pv_h
= &pv_head_table
[cindx
];
624 rindx
= pmap_unpack_index(pv_h
);
625 assert(rindx
< pmap_npages
);
627 if (rindx
!= cindx
) {
629 * this pv_rooted_entry_t was moved by hibernate_teardown_pmap_structs,
630 * so move it back to its real location
632 pv_head_table
[rindx
] = pv_head_table
[cindx
];
634 if (rindx
+1 != eindx
) {
636 * the 'hole' between this vm_rooted_entry_t and the previous
637 * vm_rooted_entry_t we moved needs to be initialized as
638 * a range of zero'd vm_rooted_entry_t's
640 bzero((char *)&pv_head_table
[rindx
+1], (eindx
- rindx
- 1) * sizeof (struct pv_rooted_entry
));
645 bzero ((char *)&pv_head_table
[0], rindx
* sizeof (struct pv_rooted_entry
));
647 HIBLOG("hibernate_rebuild_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx
);
653 * Initialize the pmap module.
654 * Called by vm_init, to initialize any structures that the pmap
655 * system needs to map virtual memory.
663 vm_map_offset_t vaddr
;
667 kernel_pmap
->pm_obj_pml4
= &kpml4obj_object_store
;
668 _vm_object_allocate((vm_object_size_t
)NPML4PGS
* PAGE_SIZE
, &kpml4obj_object_store
);
670 kernel_pmap
->pm_obj_pdpt
= &kpdptobj_object_store
;
671 _vm_object_allocate((vm_object_size_t
)NPDPTPGS
* PAGE_SIZE
, &kpdptobj_object_store
);
673 kernel_pmap
->pm_obj
= &kptobj_object_store
;
674 _vm_object_allocate((vm_object_size_t
)NPDEPGS
* PAGE_SIZE
, &kptobj_object_store
);
677 * Allocate memory for the pv_head_table and its lock bits,
678 * the modify bit array, and the pte_page table.
682 * zero bias all these arrays now instead of off avail_start
683 * so we cover all memory
686 npages
= i386_btop(avail_end
);
688 pmap_npages
= (uint32_t)npages
;
690 s
= (vm_size_t
) (sizeof(struct pv_rooted_entry
) * npages
691 + (sizeof (struct pv_hashed_entry_t
*) * (npvhashbuckets
))
692 + pv_lock_table_size(npages
)
693 + pv_hash_lock_table_size((npvhashbuckets
))
696 if (kernel_memory_allocate(kernel_map
, &addr
, s
, 0,
697 KMA_KOBJECT
| KMA_PERMANENT
, VM_KERN_MEMORY_PMAP
)
701 memset((char *)addr
, 0, s
);
707 if (0 == npvhashmask
) panic("npvhashmask not initialized");
711 * Allocate the structures first to preserve word-alignment.
713 pv_head_table
= (pv_rooted_entry_t
) addr
;
714 addr
= (vm_offset_t
) (pv_head_table
+ npages
);
716 pv_hash_table
= (pv_hashed_entry_t
*)addr
;
717 addr
= (vm_offset_t
) (pv_hash_table
+ (npvhashbuckets
));
719 pv_lock_table
= (char *) addr
;
720 addr
= (vm_offset_t
) (pv_lock_table
+ pv_lock_table_size(npages
));
722 pv_hash_lock_table
= (char *) addr
;
723 addr
= (vm_offset_t
) (pv_hash_lock_table
+ pv_hash_lock_table_size((npvhashbuckets
)));
725 pmap_phys_attributes
= (char *) addr
;
727 ppnum_t last_pn
= i386_btop(avail_end
);
729 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
730 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
731 if (pmptr
->type
!= kEfiConventionalMemory
)
734 for (pn
= pmptr
->base
; pn
<= pmptr
->end
; pn
++) {
736 pmap_phys_attributes
[pn
] |= PHYS_MANAGED
;
738 if (pn
> last_managed_page
)
739 last_managed_page
= pn
;
741 if (pn
>= lowest_hi
&& pn
<= highest_hi
)
742 pmap_phys_attributes
[pn
] |= PHYS_NOENCRYPT
;
747 ppn
= pmap_find_phys(kernel_pmap
, vaddr
);
749 pmap_phys_attributes
[ppn
] |= PHYS_NOENCRYPT
;
755 * Create the zone of physical maps,
756 * and of the physical-to-virtual entries.
758 s
= (vm_size_t
) sizeof(struct pmap
);
759 pmap_zone
= zinit(s
, 400*s
, 4096, "pmap"); /* XXX */
760 zone_change(pmap_zone
, Z_NOENCRYPT
, TRUE
);
762 pmap_anchor_zone
= zinit(PAGE_SIZE
, task_max
, PAGE_SIZE
, "pagetable anchors");
763 zone_change(pmap_anchor_zone
, Z_NOENCRYPT
, TRUE
);
765 /* The anchor is required to be page aligned. Zone debugging adds
766 * padding which may violate that requirement. Tell the zone
767 * subsystem that alignment is required.
770 zone_change(pmap_anchor_zone
, Z_ALIGNMENT_REQUIRED
, TRUE
);
772 s
= (vm_size_t
) sizeof(struct pv_hashed_entry
);
773 pv_hashed_list_zone
= zinit(s
, 10000*s
/* Expandable zone */,
774 4096 * 3 /* LCM x86_64*/, "pv_list");
775 zone_change(pv_hashed_list_zone
, Z_NOENCRYPT
, TRUE
);
777 /* create pv entries for kernel pages mapped by low level
778 startup code. these have to exist so we can pmap_remove()
779 e.g. kext pages from the middle of our addr space */
781 vaddr
= (vm_map_offset_t
) VM_MIN_KERNEL_ADDRESS
;
782 for (ppn
= VM_MIN_KERNEL_PAGE
; ppn
< i386_btop(avail_start
); ppn
++) {
783 pv_rooted_entry_t pv_e
;
785 pv_e
= pai_to_pvh(ppn
);
788 pv_e
->pmap
= kernel_pmap
;
789 queue_init(&pv_e
->qlink
);
791 pmap_initialized
= TRUE
;
793 max_preemption_latency_tsc
= tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS
, tscFCvtn2t
);
796 * Ensure the kernel's PML4 entry exists for the basement
797 * before this is shared with any user.
799 pmap_expand_pml4(kernel_pmap
, KERNEL_BASEMENT
, PMAP_EXPAND_OPTIONS_NONE
);
802 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
);
804 pmap_ept_support_ad
= FALSE
;
805 #endif /* CONFIG_VMX */
809 void pmap_mark_range(pmap_t npmap
, uint64_t sv
, uint64_t nxrosz
, boolean_t NX
, boolean_t ro
) {
810 uint64_t ev
= sv
+ nxrosz
, cv
= sv
;
812 pt_entry_t
*ptep
= NULL
;
814 assert(!is_ept_pmap(npmap
));
816 assert(((sv
& 0xFFFULL
) | (nxrosz
& 0xFFFULL
)) == 0);
818 for (pdep
= pmap_pde(npmap
, cv
); pdep
!= NULL
&& (cv
< ev
);) {
819 uint64_t pdev
= (cv
& ~((uint64_t)PDEMASK
));
821 if (*pdep
& INTEL_PTE_PS
) {
823 *pdep
|= INTEL_PTE_NX
;
825 *pdep
&= ~INTEL_PTE_WRITE
;
827 cv
&= ~((uint64_t) PDEMASK
);
828 pdep
= pmap_pde(npmap
, cv
);
832 for (ptep
= pmap_pte(npmap
, cv
); ptep
!= NULL
&& (cv
< (pdev
+ NBPD
)) && (cv
< ev
);) {
834 *ptep
|= INTEL_PTE_NX
;
836 *ptep
&= ~INTEL_PTE_WRITE
;
838 ptep
= pmap_pte(npmap
, cv
);
841 DPRINTF("%s(0x%llx, 0x%llx, %u, %u): 0x%llx, 0x%llx\n", __FUNCTION__
, sv
, nxrosz
, NX
, ro
, cv
, ptep
? *ptep
: 0);
845 * Called once VM is fully initialized so that we can release unused
846 * sections of low memory to the general pool.
847 * Also complete the set-up of identity-mapped sections of the kernel:
848 * 1) write-protect kernel text
849 * 2) map kernel text using large pages if possible
850 * 3) read and write-protect page zero (for K32)
851 * 4) map the global page at the appropriate virtual address.
855 * To effectively map and write-protect all kernel text pages, the text
856 * must be 2M-aligned at the base, and the data section above must also be
857 * 2M-aligned. That is, there's padding below and above. This is achieved
858 * through linker directives. Large pages are used only if this alignment
859 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
864 * sdata: ================== 2Meg
868 * etext: ------------------
876 * stext: ================== 2Meg
880 * eHIB: ------------------
884 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
885 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
886 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
887 * The now unused level-1 PTE pages are also freed.
889 extern ppnum_t vm_kernel_base_page
;
891 pmap_lowmem_finalize(void)
897 * Update wired memory statistics for early boot pages
899 PMAP_ZINFO_PALLOC(kernel_pmap
, bootstrap_wired_pages
* PAGE_SIZE
);
902 * Free pages in pmap regions below the base:
904 * We can't free all the pages to VM that EFI reports available.
905 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
906 * There's also a size miscalculation here: pend is one page less
907 * than it should be but this is not fixed to be backwards
909 * This is important for KASLR because up to 256*2MB = 512MB of space
910 * needs has to be released to VM.
913 pmap_memory_regions
[i
].end
< vm_kernel_base_page
;
915 vm_offset_t pbase
= i386_ptob(pmap_memory_regions
[i
].base
);
916 vm_offset_t pend
= i386_ptob(pmap_memory_regions
[i
].end
+1);
918 DBG("pmap region %d [%p..[%p\n",
919 i
, (void *) pbase
, (void *) pend
);
921 if (pmap_memory_regions
[i
].attribute
& EFI_MEMORY_KERN_RESERVED
)
925 * Adjust limits not to free pages in range 0xc0000-0xff000.
927 if (pbase
>= 0xc0000 && pend
<= 0x100000)
929 if (pbase
< 0xc0000 && pend
> 0x100000) {
930 /* page range entirely within region, free lower part */
931 DBG("- ml_static_mfree(%p,%p)\n",
932 (void *) ml_static_ptovirt(pbase
),
933 (void *) (0xc0000-pbase
));
934 ml_static_mfree(ml_static_ptovirt(pbase
),0xc0000-pbase
);
938 pend
= MIN(pend
, 0xc0000);
940 pbase
= MAX(pbase
, 0x100000);
941 DBG("- ml_static_mfree(%p,%p)\n",
942 (void *) ml_static_ptovirt(pbase
),
943 (void *) (pend
- pbase
));
944 ml_static_mfree(ml_static_ptovirt(pbase
), pend
- pbase
);
947 /* A final pass to get rid of all initial identity mappings to
950 DPRINTF("%s: Removing mappings from 0->0x%lx\n", __FUNCTION__
, vm_kernel_base
);
953 * Remove all mappings past the boot-cpu descriptor aliases and low globals.
954 * Non-boot-cpu GDT aliases will be remapped later as needed.
956 pmap_remove(kernel_pmap
, LOWGLOBAL_ALIAS
+ PAGE_SIZE
, vm_kernel_base
);
959 * If text and data are both 2MB-aligned,
960 * we can map text with large-pages,
961 * unless the -kernel_text_ps_4K boot-arg overrides.
963 if ((stext
& I386_LPGMASK
) == 0 && (sdata
& I386_LPGMASK
) == 0) {
964 kprintf("Kernel text is 2MB aligned");
965 kernel_text_ps_4K
= FALSE
;
966 if (PE_parse_boot_argn("-kernel_text_ps_4K",
968 sizeof (kernel_text_ps_4K
)))
969 kprintf(" but will be mapped with 4K pages\n");
971 kprintf(" and will be mapped with 2M pages\n");
974 (void) PE_parse_boot_argn("wpkernel", &wpkernel
, sizeof (wpkernel
));
976 kprintf("Kernel text %p-%p to be write-protected\n",
977 (void *) stext
, (void *) etext
);
982 * Scan over text if mappings are to be changed:
983 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
984 * - Change to large-pages if possible and not overriden.
986 if (kernel_text_ps_4K
&& wpkernel
) {
988 for (myva
= stext
; myva
< etext
; myva
+= PAGE_SIZE
) {
991 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
993 pmap_store_pte(ptep
, *ptep
& ~INTEL_PTE_WRITE
);
997 if (!kernel_text_ps_4K
) {
1001 * Release zero-filled page padding used for 2M-alignment.
1003 DBG("ml_static_mfree(%p,%p) for padding below text\n",
1004 (void *) eHIB
, (void *) (stext
- eHIB
));
1005 ml_static_mfree(eHIB
, stext
- eHIB
);
1006 DBG("ml_static_mfree(%p,%p) for padding above text\n",
1007 (void *) etext
, (void *) (sdata
- etext
));
1008 ml_static_mfree(etext
, sdata
- etext
);
1011 * Coalesce text pages into large pages.
1013 for (myva
= stext
; myva
< sdata
; myva
+= I386_LPGBYTES
) {
1015 vm_offset_t pte_phys
;
1019 pdep
= pmap_pde(kernel_pmap
, (vm_map_offset_t
)myva
);
1020 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
1021 DBG("myva: %p pdep: %p ptep: %p\n",
1022 (void *) myva
, (void *) pdep
, (void *) ptep
);
1023 if ((*ptep
& INTEL_PTE_VALID
) == 0)
1025 pte_phys
= (vm_offset_t
)(*ptep
& PG_FRAME
);
1026 pde
= *pdep
& PTMASK
; /* page attributes from pde */
1027 pde
|= INTEL_PTE_PS
; /* make it a 2M entry */
1028 pde
|= pte_phys
; /* take page frame from pte */
1031 pde
&= ~INTEL_PTE_WRITE
;
1032 DBG("pmap_store_pte(%p,0x%llx)\n",
1034 pmap_store_pte(pdep
, pde
);
1037 * Free the now-unused level-1 pte.
1038 * Note: ptep is a virtual address to the pte in the
1039 * recursive map. We can't use this address to free
1040 * the page. Instead we need to compute its address
1041 * in the Idle PTEs in "low memory".
1043 vm_offset_t vm_ptep
= (vm_offset_t
) KPTphys
1044 + (pte_phys
>> PTPGSHIFT
);
1045 DBG("ml_static_mfree(%p,0x%x) for pte\n",
1046 (void *) vm_ptep
, PAGE_SIZE
);
1047 ml_static_mfree(vm_ptep
, PAGE_SIZE
);
1050 /* Change variable read by sysctl machdep.pmap */
1051 pmap_kernel_text_ps
= I386_LPGBYTES
;
1054 boolean_t doconstro
= TRUE
;
1056 (void) PE_parse_boot_argn("dataconstro", &doconstro
, sizeof(doconstro
));
1058 if ((sconstdata
| econstdata
) & PAGE_MASK
) {
1059 kprintf("Const DATA misaligned 0x%lx 0x%lx\n", sconstdata
, econstdata
);
1060 if ((sconstdata
& PAGE_MASK
) || (doconstro_override
== FALSE
))
1064 if ((sconstdata
> edata
) || (sconstdata
< sdata
) || ((econstdata
- sconstdata
) >= (edata
- sdata
))) {
1065 kprintf("Const DATA incorrect size 0x%lx 0x%lx 0x%lx 0x%lx\n", sconstdata
, econstdata
, sdata
, edata
);
1070 kprintf("Marking const DATA read-only\n");
1074 for (dva
= sdata
; dva
< edata
; dva
+= I386_PGBYTES
) {
1075 assert(((sdata
| edata
) & PAGE_MASK
) == 0);
1076 if ( (sdata
| edata
) & PAGE_MASK
) {
1077 kprintf("DATA misaligned, 0x%lx, 0x%lx\n", sdata
, edata
);
1081 pt_entry_t dpte
, *dptep
= pmap_pte(kernel_pmap
, dva
);
1085 assert((dpte
& INTEL_PTE_VALID
));
1086 if ((dpte
& INTEL_PTE_VALID
) == 0) {
1087 kprintf("Missing data mapping 0x%lx 0x%lx 0x%lx\n", dva
, sdata
, edata
);
1091 dpte
|= INTEL_PTE_NX
;
1092 if (doconstro
&& (dva
>= sconstdata
) && (dva
< econstdata
)) {
1093 dpte
&= ~INTEL_PTE_WRITE
;
1095 pmap_store_pte(dptep
, dpte
);
1097 kernel_segment_command_t
* seg
;
1098 kernel_section_t
* sec
;
1100 for (seg
= firstseg(); seg
!= NULL
; seg
= nextsegfromheader(&_mh_execute_header
, seg
)) {
1101 if (!strcmp(seg
->segname
, "__TEXT") ||
1102 !strcmp(seg
->segname
, "__DATA")) {
1106 if (!strcmp(seg
->segname
, "__KLD")) {
1109 if (!strcmp(seg
->segname
, "__HIB")) {
1110 for (sec
= firstsect(seg
); sec
!= NULL
; sec
= nextsect(seg
, sec
)) {
1111 if (sec
->addr
& PAGE_MASK
)
1112 panic("__HIB segment's sections misaligned");
1113 if (!strcmp(sec
->sectname
, "__text")) {
1114 pmap_mark_range(kernel_pmap
, sec
->addr
, round_page(sec
->size
), FALSE
, TRUE
);
1116 pmap_mark_range(kernel_pmap
, sec
->addr
, round_page(sec
->size
), TRUE
, FALSE
);
1120 pmap_mark_range(kernel_pmap
, seg
->vmaddr
, round_page_64(seg
->vmsize
), TRUE
, FALSE
);
1125 * If we're debugging, map the low global vector page at the fixed
1126 * virtual address. Otherwise, remove the mapping for this.
1128 if (debug_boot_arg
) {
1129 pt_entry_t
*pte
= NULL
;
1130 if (0 == (pte
= pmap_pte(kernel_pmap
, LOWGLOBAL_ALIAS
)))
1131 panic("lowmem pte");
1132 /* make sure it is defined on page boundary */
1133 assert(0 == ((vm_offset_t
) &lowGlo
& PAGE_MASK
));
1134 pmap_store_pte(pte
, kvtophys((vm_offset_t
)&lowGlo
)
1142 pmap_remove(kernel_pmap
,
1143 LOWGLOBAL_ALIAS
, LOWGLOBAL_ALIAS
+ PAGE_SIZE
);
1147 if (pmap_pcid_ncpus
)
1154 * this function is only used for debugging fron the vm layer
1160 pv_rooted_entry_t pv_h
;
1164 assert(pn
!= vm_page_fictitious_addr
);
1166 if (!pmap_initialized
)
1169 if (pn
== vm_page_guard_addr
)
1172 pai
= ppn_to_pai(pn
);
1173 if (!IS_MANAGED_PAGE(pai
))
1175 pv_h
= pai_to_pvh(pn
);
1176 result
= (pv_h
->pmap
== PMAP_NULL
);
1183 vm_map_offset_t va_start
,
1184 vm_map_offset_t va_end
)
1186 vm_map_offset_t offset
;
1189 if (pmap
== PMAP_NULL
) {
1194 * Check the resident page count
1195 * - if it's zero, the pmap is completely empty.
1196 * This short-circuit test prevents a virtual address scan which is
1197 * painfully slow for 64-bit spaces.
1198 * This assumes the count is correct
1199 * .. the debug kernel ought to be checking perhaps by page table walk.
1201 if (pmap
->stats
.resident_count
== 0)
1204 for (offset
= va_start
;
1206 offset
+= PAGE_SIZE_64
) {
1207 phys_page
= pmap_find_phys(pmap
, offset
);
1209 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
1210 "page %d at 0x%llx\n",
1211 pmap
, va_start
, va_end
, phys_page
, offset
);
1220 hv_ept_pmap_create(void **ept_pmap
, void **eptp
)
1224 if ((ept_pmap
== NULL
) || (eptp
== NULL
)) {
1228 p
= pmap_create_options(get_task_ledger(current_task()), 0, (PMAP_CREATE_64BIT
| PMAP_CREATE_EPT
));
1229 if (p
== PMAP_NULL
) {
1235 assert(is_ept_pmap(p
));
1237 *ept_pmap
= (void*)p
;
1238 *eptp
= (void*)(p
->pm_eptp
);
1243 * Create and return a physical map.
1245 * If the size specified for the map
1246 * is zero, the map is an actual physical
1247 * map, and may be referenced by the
1250 * If the size specified is non-zero,
1251 * the map will be used in software only, and
1252 * is bounded by that size.
1255 pmap_create_options(
1263 pml4_entry_t
*kpml4
;
1265 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_START
,
1266 (uint32_t) (sz
>>32), (uint32_t) sz
, flags
, 0, 0);
1268 size
= (vm_size_t
) sz
;
1271 * A software use-only map doesn't even need a map.
1279 * Return error when unrecognized flags are passed.
1281 if ((flags
& ~(PMAP_CREATE_KNOWN_FLAGS
)) != 0) {
1285 p
= (pmap_t
) zalloc(pmap_zone
);
1287 panic("pmap_create zalloc");
1288 /* Zero all fields */
1289 bzero(p
, sizeof(*p
));
1290 /* init counts now since we'll be bumping some */
1291 simple_lock_init(&p
->lock
, 0);
1293 p
->stats
.resident_count
= 0;
1294 p
->stats
.resident_max
= 0;
1295 p
->stats
.wired_count
= 0;
1297 bzero(&p
->stats
, sizeof (p
->stats
));
1301 p
->pm_shared
= FALSE
;
1302 ledger_reference(ledger
);
1305 p
->pm_task_map
= ((flags
& PMAP_CREATE_64BIT
) ? TASK_MAP_64BIT
: TASK_MAP_32BIT
);
1306 if (pmap_pcid_ncpus
)
1307 pmap_pcid_initialize(p
);
1309 p
->pm_pml4
= zalloc(pmap_anchor_zone
);
1311 pmap_assert((((uintptr_t)p
->pm_pml4
) & PAGE_MASK
) == 0);
1313 memset((char *)p
->pm_pml4
, 0, PAGE_SIZE
);
1315 if (flags
& PMAP_CREATE_EPT
) {
1316 p
->pm_eptp
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_pml4
);
1320 p
->pm_cr3
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_pml4
);
1323 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
1325 p
->pm_obj_pml4
= vm_object_allocate((vm_object_size_t
)(NPML4PGS
) * PAGE_SIZE
);
1326 if (NULL
== p
->pm_obj_pml4
)
1327 panic("pmap_create pdpt obj");
1329 p
->pm_obj_pdpt
= vm_object_allocate((vm_object_size_t
)(NPDPTPGS
) * PAGE_SIZE
);
1330 if (NULL
== p
->pm_obj_pdpt
)
1331 panic("pmap_create pdpt obj");
1333 p
->pm_obj
= vm_object_allocate((vm_object_size_t
)(NPDEPGS
) * PAGE_SIZE
);
1334 if (NULL
== p
->pm_obj
)
1335 panic("pmap_create pte obj");
1337 /* All pmaps share the kernel's pml4 */
1338 pml4
= pmap64_pml4(p
, 0ULL);
1339 kpml4
= kernel_pmap
->pm_pml4
;
1340 pml4
[KERNEL_PML4_INDEX
] = kpml4
[KERNEL_PML4_INDEX
];
1341 pml4
[KERNEL_KEXTS_INDEX
] = kpml4
[KERNEL_KEXTS_INDEX
];
1342 pml4
[KERNEL_PHYSMAP_PML4_INDEX
] = kpml4
[KERNEL_PHYSMAP_PML4_INDEX
];
1344 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_START
,
1356 return pmap_create_options(ledger
, sz
, ((is_64bit
) ? PMAP_CREATE_64BIT
: 0));
1360 * Retire the given physical map from service.
1361 * Should only be called if the map contains
1362 * no valid mappings.
1364 extern int vm_wired_objects_page_count
;
1367 pmap_destroy(pmap_t p
)
1374 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_START
,
1381 pmap_assert((current_thread() && (current_thread()->map
)) ? (current_thread()->map
->pmap
!= p
) : TRUE
);
1385 * If some cpu is not using the physical pmap pointer that it
1386 * is supposed to be (see set_dirbase), we might be using the
1387 * pmap that is being destroyed! Make sure we are
1388 * physically on the right pmap:
1390 PMAP_UPDATE_TLBS(p
, 0x0ULL
, 0xFFFFFFFFFFFFF000ULL
);
1391 if (pmap_pcid_ncpus
)
1392 pmap_destroy_pcid_sync(p
);
1398 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
,
1400 pmap_assert(p
== kernel_pmap
);
1401 return; /* still in use */
1405 * Free the memory maps, then the
1408 int inuse_ptepages
= 0;
1410 zfree(pmap_anchor_zone
, p
->pm_pml4
);
1412 inuse_ptepages
+= p
->pm_obj_pml4
->resident_page_count
;
1413 vm_object_deallocate(p
->pm_obj_pml4
);
1415 inuse_ptepages
+= p
->pm_obj_pdpt
->resident_page_count
;
1416 vm_object_deallocate(p
->pm_obj_pdpt
);
1418 inuse_ptepages
+= p
->pm_obj
->resident_page_count
;
1419 vm_object_deallocate(p
->pm_obj
);
1421 OSAddAtomic(-inuse_ptepages
, &inuse_ptepages_count
);
1422 PMAP_ZINFO_PFREE(p
, inuse_ptepages
* PAGE_SIZE
);
1423 ledger_dereference(p
->ledger
);
1424 zfree(pmap_zone
, p
);
1426 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
,
1431 * Add a reference to the specified pmap.
1435 pmap_reference(pmap_t p
)
1437 if (p
!= PMAP_NULL
) {
1445 * Remove phys addr if mapped in specified map
1449 pmap_remove_some_phys(
1450 __unused pmap_t map
,
1451 __unused ppnum_t pn
)
1454 /* Implement to support working set code */
1462 vm_map_offset_t sva
,
1463 vm_map_offset_t eva
,
1466 pmap_protect_options(map
, sva
, eva
, prot
, 0, NULL
);
1471 * Set the physical protection on the
1472 * specified range of this map as requested.
1473 * Will not increase permissions.
1476 pmap_protect_options(
1478 vm_map_offset_t sva
,
1479 vm_map_offset_t eva
,
1481 unsigned int options
,
1485 pt_entry_t
*spte
, *epte
;
1486 vm_map_offset_t lva
;
1487 vm_map_offset_t orig_sva
;
1494 if (map
== PMAP_NULL
)
1497 if (prot
== VM_PROT_NONE
) {
1498 pmap_remove_options(map
, sva
, eva
, options
);
1501 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_START
,
1503 (uint32_t) (sva
>> 32), (uint32_t) sva
,
1504 (uint32_t) (eva
>> 32), (uint32_t) eva
);
1506 if ((prot
& VM_PROT_EXECUTE
) || !nx_enabled
|| !map
->nx_enabled
)
1511 is_ept
= is_ept_pmap(map
);
1518 lva
= (sva
+ pde_mapped_size
) & ~(pde_mapped_size
- 1);
1521 pde
= pmap_pde(map
, sva
);
1522 if (pde
&& (*pde
& PTE_VALID_MASK(is_ept
))) {
1523 if (*pde
& PTE_PS
) {
1526 epte
= spte
+1; /* excluded */
1528 spte
= pmap_pte(map
, (sva
& ~(pde_mapped_size
- 1)));
1529 spte
= &spte
[ptenum(sva
)];
1530 epte
= &spte
[intel_btop(lva
- sva
)];
1533 for (; spte
< epte
; spte
++) {
1534 if (!(*spte
& PTE_VALID_MASK(is_ept
)))
1538 if (prot
& VM_PROT_READ
)
1539 pmap_update_pte(spte
, 0, PTE_READ(is_ept
));
1541 pmap_update_pte(spte
, PTE_READ(is_ept
), 0);
1543 if (prot
& VM_PROT_WRITE
)
1544 pmap_update_pte(spte
, 0, PTE_WRITE(is_ept
));
1546 pmap_update_pte(spte
, PTE_WRITE(is_ept
), 0);
1550 pmap_update_pte(spte
, 0, INTEL_PTE_NX
);
1552 pmap_update_pte(spte
, INTEL_EPT_EX
, 0);
1555 pmap_update_pte(spte
, INTEL_PTE_NX
, 0);
1557 pmap_update_pte(spte
, 0, INTEL_EPT_EX
);
1565 if (options
& PMAP_OPTIONS_NOFLUSH
)
1566 PMAP_UPDATE_TLBS_DELAYED(map
, orig_sva
, eva
, (pmap_flush_context
*)arg
);
1568 PMAP_UPDATE_TLBS(map
, orig_sva
, eva
);
1572 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_END
,
1577 /* Map a (possibly) autogenned block */
1586 __unused
unsigned int flags
)
1591 if (attr
& VM_MEM_SUPERPAGE
)
1592 cur_page_size
= SUPERPAGE_SIZE
;
1594 cur_page_size
= PAGE_SIZE
;
1596 for (page
= 0; page
< size
; page
+=cur_page_size
/PAGE_SIZE
) {
1597 pmap_enter(pmap
, va
, pa
, prot
, VM_PROT_NONE
, attr
, TRUE
);
1598 va
+= cur_page_size
;
1599 pa
+=cur_page_size
/PAGE_SIZE
;
1606 vm_map_offset_t vaddr
,
1607 unsigned int options
)
1613 pml4_entry_t
*pml4p
;
1614 boolean_t is_ept
= is_ept_pmap(map
);
1616 DBG("pmap_expand_pml4(%p,%p)\n", map
, (void *)vaddr
);
1619 * Allocate a VM page for the pml4 page
1621 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1622 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
1623 return KERN_RESOURCE_SHORTAGE
;
1627 * put the page into the pmap's obj list so it
1628 * can be found later.
1632 i
= pml4idx(map
, vaddr
);
1639 vm_page_lockspin_queues();
1640 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
1641 vm_page_unlock_queues();
1643 OSAddAtomic(1, &inuse_ptepages_count
);
1644 OSAddAtomic64(1, &alloc_ptepages_count
);
1645 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1647 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1648 vm_object_lock(map
->pm_obj_pml4
);
1652 * See if someone else expanded us first
1654 if (pmap64_pdpt(map
, vaddr
) != PDPT_ENTRY_NULL
) {
1656 vm_object_unlock(map
->pm_obj_pml4
);
1660 OSAddAtomic(-1, &inuse_ptepages_count
);
1661 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1662 return KERN_SUCCESS
;
1666 if (0 != vm_page_lookup(map
->pm_obj_pml4
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1667 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1668 map
, map
->pm_obj_pml4
, vaddr
, i
);
1671 vm_page_insert_wired(m
, map
->pm_obj_pml4
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
1672 vm_object_unlock(map
->pm_obj_pml4
);
1675 * Set the page directory entry for this page table.
1677 pml4p
= pmap64_pml4(map
, vaddr
); /* refetch under lock */
1679 pmap_store_pte(pml4p
, pa_to_pte(pa
)
1681 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1682 | PTE_WRITE(is_ept
));
1686 return KERN_SUCCESS
;
1690 pmap_expand_pdpt(pmap_t map
, vm_map_offset_t vaddr
, unsigned int options
)
1696 pdpt_entry_t
*pdptp
;
1697 boolean_t is_ept
= is_ept_pmap(map
);
1699 DBG("pmap_expand_pdpt(%p,%p)\n", map
, (void *)vaddr
);
1701 while ((pdptp
= pmap64_pdpt(map
, vaddr
)) == PDPT_ENTRY_NULL
) {
1702 kern_return_t pep4kr
= pmap_expand_pml4(map
, vaddr
, options
);
1703 if (pep4kr
!= KERN_SUCCESS
)
1708 * Allocate a VM page for the pdpt page
1710 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1711 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
1712 return KERN_RESOURCE_SHORTAGE
;
1717 * put the page into the pmap's obj list so it
1718 * can be found later.
1722 i
= pdptidx(map
, vaddr
);
1729 vm_page_lockspin_queues();
1730 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
1731 vm_page_unlock_queues();
1733 OSAddAtomic(1, &inuse_ptepages_count
);
1734 OSAddAtomic64(1, &alloc_ptepages_count
);
1735 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1737 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1738 vm_object_lock(map
->pm_obj_pdpt
);
1742 * See if someone else expanded us first
1744 if (pmap64_pde(map
, vaddr
) != PD_ENTRY_NULL
) {
1746 vm_object_unlock(map
->pm_obj_pdpt
);
1750 OSAddAtomic(-1, &inuse_ptepages_count
);
1751 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1752 return KERN_SUCCESS
;
1756 if (0 != vm_page_lookup(map
->pm_obj_pdpt
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1757 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1758 map
, map
->pm_obj_pdpt
, vaddr
, i
);
1761 vm_page_insert_wired(m
, map
->pm_obj_pdpt
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
1762 vm_object_unlock(map
->pm_obj_pdpt
);
1765 * Set the page directory entry for this page table.
1767 pdptp
= pmap64_pdpt(map
, vaddr
); /* refetch under lock */
1769 pmap_store_pte(pdptp
, pa_to_pte(pa
)
1771 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1772 | PTE_WRITE(is_ept
));
1776 return KERN_SUCCESS
;
1783 * Routine: pmap_expand
1785 * Expands a pmap to be able to map the specified virtual address.
1787 * Allocates new virtual memory for the P0 or P1 portion of the
1788 * pmap, then re-maps the physical pages that were in the old
1789 * pmap to be in the new pmap.
1791 * Must be called with the pmap system and the pmap unlocked,
1792 * since these must be unlocked to use vm_allocate or vm_deallocate.
1793 * Thus it must be called in a loop that checks whether the map
1794 * has been expanded enough.
1795 * (We won't loop forever, since page tables aren't shrunk.)
1800 vm_map_offset_t vaddr
,
1801 unsigned int options
)
1804 register vm_page_t m
;
1805 register pmap_paddr_t pa
;
1808 boolean_t is_ept
= is_ept_pmap(map
);
1812 * For the kernel, the virtual address must be in or above the basement
1813 * which is for kexts and is in the 512GB immediately below the kernel..
1814 * XXX - should use VM_MIN_KERNEL_AND_KEXT_ADDRESS not KERNEL_BASEMENT
1816 if (map
== kernel_pmap
&&
1817 !(vaddr
>= KERNEL_BASEMENT
&& vaddr
<= VM_MAX_KERNEL_ADDRESS
))
1818 panic("pmap_expand: bad vaddr 0x%llx for kernel pmap", vaddr
);
1821 while ((pdp
= pmap64_pde(map
, vaddr
)) == PD_ENTRY_NULL
) {
1822 kern_return_t pepkr
= pmap_expand_pdpt(map
, vaddr
, options
);
1823 if (pepkr
!= KERN_SUCCESS
)
1828 * Allocate a VM page for the pde entries.
1830 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1831 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
1832 return KERN_RESOURCE_SHORTAGE
;
1837 * put the page into the pmap's obj list so it
1838 * can be found later.
1842 i
= pdeidx(map
, vaddr
);
1849 vm_page_lockspin_queues();
1850 vm_page_wire(m
, VM_KERN_MEMORY_PTE
, TRUE
);
1851 vm_page_unlock_queues();
1853 OSAddAtomic(1, &inuse_ptepages_count
);
1854 OSAddAtomic64(1, &alloc_ptepages_count
);
1855 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1857 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1858 vm_object_lock(map
->pm_obj
);
1863 * See if someone else expanded us first
1865 if (pmap_pte(map
, vaddr
) != PT_ENTRY_NULL
) {
1867 vm_object_unlock(map
->pm_obj
);
1871 OSAddAtomic(-1, &inuse_ptepages_count
);
1872 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1873 return KERN_SUCCESS
;
1877 if (0 != vm_page_lookup(map
->pm_obj
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1878 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
1879 map
, map
->pm_obj
, vaddr
, i
);
1882 vm_page_insert_wired(m
, map
->pm_obj
, (vm_object_offset_t
)i
* PAGE_SIZE
, VM_KERN_MEMORY_PTE
);
1883 vm_object_unlock(map
->pm_obj
);
1886 * Set the page directory entry for this page table.
1888 pdp
= pmap_pde(map
, vaddr
);
1889 pmap_store_pte(pdp
, pa_to_pte(pa
)
1891 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1892 | PTE_WRITE(is_ept
));
1896 return KERN_SUCCESS
;
1899 /* On K64 machines with more than 32GB of memory, pmap_steal_memory
1900 * will allocate past the 1GB of pre-expanded virtual kernel area. This
1901 * function allocates all the page tables using memory from the same pool
1902 * that pmap_steal_memory uses, rather than calling vm_page_grab (which
1903 * isn't available yet). */
1905 pmap_pre_expand(pmap_t pmap
, vm_map_offset_t vaddr
)
1909 boolean_t is_ept
= is_ept_pmap(pmap
);
1913 if(pmap64_pdpt(pmap
, vaddr
) == PDPT_ENTRY_NULL
) {
1914 if (!pmap_next_page_hi(&pn
))
1915 panic("pmap_pre_expand");
1919 pte
= pmap64_pml4(pmap
, vaddr
);
1921 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
1923 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1924 | PTE_WRITE(is_ept
));
1927 if(pmap64_pde(pmap
, vaddr
) == PD_ENTRY_NULL
) {
1928 if (!pmap_next_page_hi(&pn
))
1929 panic("pmap_pre_expand");
1933 pte
= pmap64_pdpt(pmap
, vaddr
);
1935 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
1937 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1938 | PTE_WRITE(is_ept
));
1941 if(pmap_pte(pmap
, vaddr
) == PT_ENTRY_NULL
) {
1942 if (!pmap_next_page_hi(&pn
))
1943 panic("pmap_pre_expand");
1947 pte
= pmap64_pde(pmap
, vaddr
);
1949 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
1951 | (is_ept
? INTEL_EPT_EX
: INTEL_PTE_USER
)
1952 | PTE_WRITE(is_ept
));
1959 * pmap_sync_page_data_phys(ppnum_t pa)
1961 * Invalidates all of the instruction cache on a physical page and
1962 * pushes any dirty data from the data cache for the same physical page
1963 * Not required in i386.
1966 pmap_sync_page_data_phys(__unused ppnum_t pa
)
1972 * pmap_sync_page_attributes_phys(ppnum_t pa)
1974 * Write back and invalidate all cachelines on a physical page.
1977 pmap_sync_page_attributes_phys(ppnum_t pa
)
1979 cache_flush_page_phys(pa
);
1984 #ifdef CURRENTLY_UNUSED_AND_UNTESTED
1990 * Routine: pmap_collect
1992 * Garbage collects the physical map system for
1993 * pages which are no longer used.
1994 * Success need not be guaranteed -- that is, there
1995 * may well be pages which are not referenced, but
1996 * others may be collected.
1998 * Called by the pageout daemon when pages are scarce.
2004 register pt_entry_t
*pdp
, *ptp
;
2012 if (p
== kernel_pmap
)
2015 is_ept
= is_ept_pmap(p
);
2018 * Garbage collect map.
2022 for (pdp
= (pt_entry_t
*)p
->dirbase
;
2023 pdp
< (pt_entry_t
*)&p
->dirbase
[(UMAXPTDI
+1)];
2026 if (*pdp
& PTE_VALID_MASK(is_ept
)) {
2027 if (*pdp
& PTE_REF(is_ept
)) {
2028 pmap_store_pte(pdp
, *pdp
& ~PTE_REF(is_ept
));
2032 ptp
= pmap_pte(p
, pdetova(pdp
- (pt_entry_t
*)p
->dirbase
));
2033 eptp
= ptp
+ NPTEPG
;
2036 * If the pte page has any wired mappings, we cannot
2041 register pt_entry_t
*ptep
;
2042 for (ptep
= ptp
; ptep
< eptp
; ptep
++) {
2043 if (iswired(*ptep
)) {
2051 * Remove the virtual addresses mapped by this pte page.
2053 pmap_remove_range(p
,
2054 pdetova(pdp
- (pt_entry_t
*)p
->dirbase
),
2059 * Invalidate the page directory pointer.
2061 pmap_store_pte(pdp
, 0x0);
2066 * And free the pte page itself.
2069 register vm_page_t m
;
2071 vm_object_lock(p
->pm_obj
);
2073 m
= vm_page_lookup(p
->pm_obj
,(vm_object_offset_t
)(pdp
- (pt_entry_t
*)&p
->dirbase
[0]) * PAGE_SIZE
);
2074 if (m
== VM_PAGE_NULL
)
2075 panic("pmap_collect: pte page not in object");
2077 vm_object_unlock(p
->pm_obj
);
2081 OSAddAtomic(-1, &inuse_ptepages_count
);
2082 PMAP_ZINFO_PFREE(p
, PAGE_SIZE
);
2091 PMAP_UPDATE_TLBS(p
, 0x0, 0xFFFFFFFFFFFFF000ULL
);
2099 pmap_copy_page(ppnum_t src
, ppnum_t dst
)
2101 bcopy_phys((addr64_t
)i386_ptob(src
),
2102 (addr64_t
)i386_ptob(dst
),
2108 * Routine: pmap_pageable
2110 * Make the specified pages (by pmap, offset)
2111 * pageable (or not) as requested.
2113 * A page which is not pageable may not take
2114 * a fault; therefore, its page table entry
2115 * must remain valid for the duration.
2117 * This routine is merely advisory; pmap_enter
2118 * will specify that these pages are to be wired
2119 * down (or not) as appropriate.
2123 __unused pmap_t pmap
,
2124 __unused vm_map_offset_t start_addr
,
2125 __unused vm_map_offset_t end_addr
,
2126 __unused boolean_t pageable
)
2129 pmap
++; start_addr
++; end_addr
++; pageable
++;
2134 invalidate_icache(__unused vm_offset_t addr
,
2135 __unused
unsigned cnt
,
2142 flush_dcache(__unused vm_offset_t addr
,
2143 __unused
unsigned count
,
2151 * Constrain DTrace copyin/copyout actions
2153 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
2154 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
2156 kern_return_t
dtrace_copyio_preflight(__unused addr64_t va
)
2158 thread_t thread
= current_thread();
2160 if (current_map() == kernel_map
)
2161 return KERN_FAILURE
;
2162 else if (((ccr3
= get_cr3_base()) != thread
->map
->pmap
->pm_cr3
) && (no_shared_cr3
== FALSE
))
2163 return KERN_FAILURE
;
2164 else if (no_shared_cr3
&& (ccr3
!= kernel_pmap
->pm_cr3
))
2165 return KERN_FAILURE
;
2167 return KERN_SUCCESS
;
2170 kern_return_t
dtrace_copyio_postflight(__unused addr64_t va
)
2172 return KERN_SUCCESS
;
2174 #endif /* CONFIG_DTRACE */
2176 #include <mach_vm_debug.h>
2178 #include <vm/vm_debug.h>
2181 pmap_list_resident_pages(
2182 __unused pmap_t pmap
,
2183 __unused vm_offset_t
*listp
,
2188 #endif /* MACH_VM_DEBUG */
2192 /* temporary workaround */
2194 coredumpok(__unused vm_map_t map
, __unused vm_offset_t va
)
2199 ptep
= pmap_pte(map
->pmap
, va
);
2202 return ((*ptep
& (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
)) != (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
));
2210 phys_page_exists(ppnum_t pn
)
2212 assert(pn
!= vm_page_fictitious_addr
);
2214 if (!pmap_initialized
)
2217 if (pn
== vm_page_guard_addr
)
2220 if (!IS_MANAGED_PAGE(ppn_to_pai(pn
)))
2229 pmap_switch(pmap_t tpmap
)
2233 s
= splhigh(); /* Make sure interruptions are disabled */
2234 set_dirbase(tpmap
, current_thread(), cpu_number());
2240 * disable no-execute capability on
2241 * the specified pmap
2244 pmap_disable_NX(pmap_t pmap
)
2246 pmap
->nx_enabled
= 0;
2250 pt_fake_zone_init(int zone_index
)
2252 pt_fake_zone_index
= zone_index
;
2258 vm_size_t
*cur_size
,
2259 vm_size_t
*max_size
,
2260 vm_size_t
*elem_size
,
2261 vm_size_t
*alloc_size
,
2267 *count
= inuse_ptepages_count
;
2268 *cur_size
= PAGE_SIZE
* inuse_ptepages_count
;
2269 *max_size
= PAGE_SIZE
* (inuse_ptepages_count
+
2270 vm_page_inactive_count
+
2271 vm_page_active_count
+
2272 vm_page_free_count
);
2273 *elem_size
= PAGE_SIZE
;
2274 *alloc_size
= PAGE_SIZE
;
2275 *sum_size
= alloc_ptepages_count
* PAGE_SIZE
;
2284 pmap_flush_context_init(pmap_flush_context
*pfc
)
2287 pfc
->pfc_invalid_global
= 0;
2290 extern unsigned TLBTimeOut
;
2293 pmap_flush_context
*pfc
)
2295 unsigned int my_cpu
;
2297 unsigned int cpu_bit
;
2298 cpumask_t cpus_to_respond
= 0;
2299 cpumask_t cpus_to_signal
= 0;
2300 cpumask_t cpus_signaled
= 0;
2301 boolean_t flush_self
= FALSE
;
2304 mp_disable_preemption();
2306 my_cpu
= cpu_number();
2307 cpus_to_signal
= pfc
->pfc_cpus
;
2309 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS
) | DBG_FUNC_START
,
2310 NULL
, cpus_to_signal
, 0, 0, 0);
2312 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
&& cpus_to_signal
; cpu
++, cpu_bit
<<= 1) {
2314 if (cpus_to_signal
& cpu_bit
) {
2316 cpus_to_signal
&= ~cpu_bit
;
2318 if (!cpu_datap(cpu
)->cpu_running
)
2321 if (pfc
->pfc_invalid_global
& cpu_bit
)
2322 cpu_datap(cpu
)->cpu_tlb_invalid_global
= TRUE
;
2324 cpu_datap(cpu
)->cpu_tlb_invalid_local
= TRUE
;
2327 if (cpu
== my_cpu
) {
2331 if (CPU_CR3_IS_ACTIVE(cpu
)) {
2332 cpus_to_respond
|= cpu_bit
;
2333 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2337 cpus_signaled
= cpus_to_respond
;
2340 * Flush local tlb if required.
2341 * Do this now to overlap with other processors responding.
2343 if (flush_self
&& cpu_datap(my_cpu
)->cpu_tlb_invalid
!= FALSE
)
2344 process_pmap_updates();
2346 if (cpus_to_respond
) {
2348 deadline
= mach_absolute_time() +
2349 (TLBTimeOut
? TLBTimeOut
: LockTimeOut
);
2350 boolean_t is_timeout_traced
= FALSE
;
2353 * Wait for those other cpus to acknowledge
2355 while (cpus_to_respond
!= 0) {
2358 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2359 /* Consider checking local/global invalidity
2360 * as appropriate in the PCID case.
2362 if ((cpus_to_respond
& cpu_bit
) != 0) {
2363 if (!cpu_datap(cpu
)->cpu_running
||
2364 cpu_datap(cpu
)->cpu_tlb_invalid
== FALSE
||
2365 !CPU_CR3_IS_ACTIVE(cpu
)) {
2366 cpus_to_respond
&= ~cpu_bit
;
2370 if (cpus_to_respond
== 0)
2373 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2374 if (machine_timeout_suspended())
2376 if (TLBTimeOut
== 0) {
2377 if (is_timeout_traced
)
2379 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS_TO
),
2380 NULL
, cpus_to_signal
, cpus_to_respond
, 0, 0);
2381 is_timeout_traced
= TRUE
;
2384 pmap_tlb_flush_timeout
= TRUE
;
2385 orig_acks
= NMIPI_acks
;
2386 mp_cpus_NMIPI(cpus_to_respond
);
2388 panic("TLB invalidation IPI timeout: "
2389 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%llx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2390 cpus_to_respond
, orig_acks
, NMIPI_acks
);
2394 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS
) | DBG_FUNC_END
,
2395 NULL
, cpus_signaled
, flush_self
, 0, 0);
2397 mp_enable_preemption();
2407 } __attribute__((aligned(16), packed
)) invept_descriptor
= {(uint64_t)eptp
, 0};
2409 __asm__
volatile("invept (%%rax), %%rcx"
2410 : : "c" (PMAP_INVEPT_SINGLE_CONTEXT
), "a" (&invept_descriptor
)
2415 * Called with pmap locked, we:
2416 * - scan through per-cpu data to see which other cpus need to flush
2417 * - send an IPI to each non-idle cpu to be flushed
2418 * - wait for all to signal back that they are inactive or we see that
2419 * they are at a safe point (idle).
2420 * - flush the local tlb if active for this pmap
2421 * - return ... the caller will unlock the pmap
2425 pmap_flush_tlbs(pmap_t pmap
, vm_map_offset_t startv
, vm_map_offset_t endv
, int options
, pmap_flush_context
*pfc
)
2428 unsigned int cpu_bit
;
2429 cpumask_t cpus_to_signal
;
2430 unsigned int my_cpu
= cpu_number();
2431 pmap_paddr_t pmap_cr3
= pmap
->pm_cr3
;
2432 boolean_t flush_self
= FALSE
;
2434 boolean_t pmap_is_shared
= (pmap
->pm_shared
|| (pmap
== kernel_pmap
));
2435 boolean_t need_global_flush
= FALSE
;
2436 uint32_t event_code
;
2437 vm_map_offset_t event_startv
, event_endv
;
2438 boolean_t is_ept
= is_ept_pmap(pmap
);
2440 assert((processor_avail_count
< 2) ||
2441 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
2443 if (pmap
== kernel_pmap
) {
2444 event_code
= PMAP_CODE(PMAP__FLUSH_KERN_TLBS
);
2445 event_startv
= VM_KERNEL_UNSLIDE_OR_PERM(startv
);
2446 event_endv
= VM_KERNEL_UNSLIDE_OR_PERM(endv
);
2447 } else if (is_ept
) {
2448 event_code
= PMAP_CODE(PMAP__FLUSH_EPT
);
2449 event_startv
= startv
;
2452 event_code
= PMAP_CODE(PMAP__FLUSH_TLBS
);
2453 event_startv
= startv
;
2457 PMAP_TRACE_CONSTANT(event_code
| DBG_FUNC_START
,
2458 VM_KERNEL_UNSLIDE_OR_PERM(pmap
), options
, event_startv
, event_endv
, 0);
2461 mp_cpus_call(CPUMASK_ALL
, ASYNC
, invept
, (void*)pmap
->pm_eptp
);
2466 * Scan other cpus for matching active or task CR3.
2467 * For idle cpus (with no active map) we mark them invalid but
2468 * don't signal -- they'll check as they go busy.
2472 if (pmap_pcid_ncpus
) {
2474 need_global_flush
= TRUE
;
2475 pmap_pcid_invalidate_all_cpus(pmap
);
2478 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2479 if (!cpu_datap(cpu
)->cpu_running
)
2481 uint64_t cpu_active_cr3
= CPU_GET_ACTIVE_CR3(cpu
);
2482 uint64_t cpu_task_cr3
= CPU_GET_TASK_CR3(cpu
);
2484 if ((pmap_cr3
== cpu_task_cr3
) ||
2485 (pmap_cr3
== cpu_active_cr3
) ||
2488 if (options
& PMAP_DELAY_TLB_FLUSH
) {
2489 if (need_global_flush
== TRUE
)
2490 pfc
->pfc_invalid_global
|= cpu_bit
;
2491 pfc
->pfc_cpus
|= cpu_bit
;
2495 if (cpu
== my_cpu
) {
2499 if (need_global_flush
== TRUE
)
2500 cpu_datap(cpu
)->cpu_tlb_invalid_global
= TRUE
;
2502 cpu_datap(cpu
)->cpu_tlb_invalid_local
= TRUE
;
2506 * We don't need to signal processors which will flush
2507 * lazily at the idle state or kernel boundary.
2508 * For example, if we're invalidating the kernel pmap,
2509 * processors currently in userspace don't need to flush
2510 * their TLBs until the next time they enter the kernel.
2511 * Alterations to the address space of a task active
2512 * on a remote processor result in a signal, to
2513 * account for copy operations. (There may be room
2514 * for optimization in such cases).
2515 * The order of the loads below with respect
2516 * to the store to the "cpu_tlb_invalid" field above
2517 * is important--hence the barrier.
2519 if (CPU_CR3_IS_ACTIVE(cpu
) &&
2520 (pmap_cr3
== CPU_GET_ACTIVE_CR3(cpu
) ||
2522 (pmap_cr3
== CPU_GET_TASK_CR3(cpu
)))) {
2523 cpus_to_signal
|= cpu_bit
;
2524 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2528 if ((options
& PMAP_DELAY_TLB_FLUSH
))
2532 * Flush local tlb if required.
2533 * Do this now to overlap with other processors responding.
2536 if (pmap_pcid_ncpus
) {
2537 pmap_pcid_validate_cpu(pmap
, my_cpu
);
2547 if (cpus_to_signal
) {
2548 cpumask_t cpus_to_respond
= cpus_to_signal
;
2550 deadline
= mach_absolute_time() +
2551 (TLBTimeOut
? TLBTimeOut
: LockTimeOut
);
2552 boolean_t is_timeout_traced
= FALSE
;
2555 * Wait for those other cpus to acknowledge
2557 while (cpus_to_respond
!= 0) {
2560 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2561 /* Consider checking local/global invalidity
2562 * as appropriate in the PCID case.
2564 if ((cpus_to_respond
& cpu_bit
) != 0) {
2565 if (!cpu_datap(cpu
)->cpu_running
||
2566 cpu_datap(cpu
)->cpu_tlb_invalid
== FALSE
||
2567 !CPU_CR3_IS_ACTIVE(cpu
)) {
2568 cpus_to_respond
&= ~cpu_bit
;
2572 if (cpus_to_respond
== 0)
2575 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2576 if (machine_timeout_suspended())
2578 if (TLBTimeOut
== 0) {
2579 /* cut tracepoint but don't panic */
2580 if (is_timeout_traced
)
2582 PMAP_TRACE_CONSTANT(
2583 PMAP_CODE(PMAP__FLUSH_TLBS_TO
),
2584 VM_KERNEL_UNSLIDE_OR_PERM(pmap
), cpus_to_signal
, cpus_to_respond
, 0, 0);
2585 is_timeout_traced
= TRUE
;
2588 pmap_tlb_flush_timeout
= TRUE
;
2589 orig_acks
= NMIPI_acks
;
2590 mp_cpus_NMIPI(cpus_to_respond
);
2592 panic("TLB invalidation IPI timeout: "
2593 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%llx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2594 cpus_to_respond
, orig_acks
, NMIPI_acks
);
2599 if (__improbable((pmap
== kernel_pmap
) && (flush_self
!= TRUE
))) {
2600 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
);
2604 PMAP_TRACE_CONSTANT(event_code
| DBG_FUNC_END
,
2605 VM_KERNEL_UNSLIDE_OR_PERM(pmap
), cpus_to_signal
, event_startv
, event_endv
, 0);
2610 process_pmap_updates(void)
2612 int ccpu
= cpu_number();
2613 pmap_assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2614 if (pmap_pcid_ncpus
) {
2615 pmap_pcid_validate_current();
2616 if (cpu_datap(ccpu
)->cpu_tlb_invalid_global
) {
2617 cpu_datap(ccpu
)->cpu_tlb_invalid
= FALSE
;
2621 cpu_datap(ccpu
)->cpu_tlb_invalid_local
= FALSE
;
2626 current_cpu_datap()->cpu_tlb_invalid
= FALSE
;
2634 pmap_update_interrupt(void)
2636 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_START
,
2639 if (current_cpu_datap()->cpu_tlb_invalid
)
2640 process_pmap_updates();
2642 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_END
,
2646 #include <mach/mach_vm.h> /* mach_vm_region_recurse() */
2647 /* Scan kernel pmap for W+X PTEs, scan kernel VM map for W+X map entries
2648 * and identify ranges with mismatched VM permissions and PTE permissions
2651 pmap_permissions_verify(pmap_t ipmap
, vm_map_t ivmmap
, vm_offset_t sv
, vm_offset_t ev
) {
2652 vm_offset_t cv
= sv
;
2653 kern_return_t rv
= KERN_SUCCESS
;
2654 uint64_t skip4
= 0, skip2
= 0;
2656 assert(!is_ept_pmap(ipmap
));
2658 sv
&= ~PAGE_MASK_64
;
2659 ev
&= ~PAGE_MASK_64
;
2661 if (__improbable((cv
> 0x00007FFFFFFFFFFFULL
) &&
2662 (cv
< 0xFFFF800000000000ULL
))) {
2663 cv
= 0xFFFF800000000000ULL
;
2665 /* Potential inconsistencies from not holding pmap lock
2666 * but harmless for the moment.
2668 if (((cv
& PML4MASK
) == 0) && (pmap64_pml4(ipmap
, cv
) == 0)) {
2669 if ((cv
+ NBPML4
) > cv
)
2676 if (((cv
& PDMASK
) == 0) && (pmap_pde(ipmap
, cv
) == 0)) {
2677 if ((cv
+ NBPD
) > cv
)
2685 pt_entry_t
*ptep
= pmap_pte(ipmap
, cv
);
2686 if (ptep
&& (*ptep
& INTEL_PTE_VALID
)) {
2687 if (*ptep
& INTEL_PTE_WRITE
) {
2688 if (!(*ptep
& INTEL_PTE_NX
)) {
2689 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
)))));
2696 kprintf("Completed pmap scan\n");
2699 struct vm_region_submap_info_64 vbr
;
2700 mach_msg_type_number_t vbrcount
= 0;
2701 mach_vm_size_t vmsize
;
2703 uint32_t nesting_depth
= 0;
2709 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
2710 if((kret
= mach_vm_region_recurse(ivmmap
,
2711 (mach_vm_address_t
*) &cv
, &vmsize
, &nesting_depth
,
2712 (vm_region_recurse_info_t
)&vbr
,
2713 &vbrcount
)) != KERN_SUCCESS
) {
2725 if(kret
!= KERN_SUCCESS
)
2728 prot
= vbr
.protection
;
2730 if ((prot
& (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) == (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) {
2731 kprintf("W+X map entry at address 0x%lx\n", cv
);
2737 for (pcv
= cv
; pcv
< cv
+ vmsize
; pcv
+= PAGE_SIZE
) {
2738 pt_entry_t
*ptep
= pmap_pte(ipmap
, pcv
);
2741 if ((ptep
== NULL
) || !(*ptep
& INTEL_PTE_VALID
))
2743 tprot
= VM_PROT_READ
;
2744 if (*ptep
& INTEL_PTE_WRITE
)
2745 tprot
|= VM_PROT_WRITE
;
2746 if ((*ptep
& INTEL_PTE_NX
) == 0)
2747 tprot
|= VM_PROT_EXECUTE
;
2748 if (tprot
!= prot
) {
2749 kprintf("PTE/map entry permissions mismatch at address 0x%lx, pte: 0x%llx, protection: 0x%x\n", pcv
, *ptep
, prot
);