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 <vm/vm_protos.h>
140 #include <i386/mp_desc.h>
141 #include <libkern/kernel_mach_header.h>
143 #include <pexpert/i386/efi.h>
149 #define POSTCODE_DELAY 1
150 #include <i386/postcode.h>
151 #endif /* IWANTTODEBUG */
154 #define DBG(x...) kprintf("DBG: " x)
158 /* Compile time assert to ensure adjacency/alignment of per-CPU data fields used
159 * in the trampolines for kernel/user boundary TLB coherency.
161 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];
162 boolean_t pmap_trace
= FALSE
;
164 boolean_t no_shared_cr3
= DEBUG
; /* TRUE for DEBUG by default */
166 int nx_enabled
= 1; /* enable no-execute protection */
167 int allow_data_exec
= VM_ABI_32
; /* 32-bit apps may execute data by default, 64-bit apps may not */
168 int allow_stack_exec
= 0; /* No apps may execute from the stack by default */
170 const boolean_t cpu_64bit
= TRUE
; /* Mais oui! */
172 uint64_t max_preemption_latency_tsc
= 0;
174 pv_hashed_entry_t
*pv_hash_table
; /* hash lists */
176 uint32_t npvhashmask
= 0, npvhashbuckets
= 0;
178 pv_hashed_entry_t pv_hashed_free_list
= PV_HASHED_ENTRY_NULL
;
179 pv_hashed_entry_t pv_hashed_kern_free_list
= PV_HASHED_ENTRY_NULL
;
180 decl_simple_lock_data(,pv_hashed_free_list_lock
)
181 decl_simple_lock_data(,pv_hashed_kern_free_list_lock
)
182 decl_simple_lock_data(,pv_hash_table_lock
)
184 decl_simple_lock_data(,phys_backup_lock
)
186 zone_t pv_hashed_list_zone
; /* zone of pv_hashed_entry structures */
189 * First and last physical addresses that we maintain any information
190 * for. Initialized to zero so that pmap operations done before
191 * pmap_init won't touch any non-existent structures.
193 boolean_t pmap_initialized
= FALSE
;/* Has pmap_init completed? */
195 static struct vm_object kptobj_object_store
;
196 static struct vm_object kpml4obj_object_store
;
197 static struct vm_object kpdptobj_object_store
;
200 * Array of physical page attribites for managed pages.
201 * One byte per physical page.
203 char *pmap_phys_attributes
;
204 ppnum_t last_managed_page
= 0;
207 * Amount of virtual memory mapped by one
208 * page-directory entry.
211 uint64_t pde_mapped_size
= PDE_MAPPED_SIZE
;
213 unsigned pmap_memory_region_count
;
214 unsigned pmap_memory_region_current
;
216 pmap_memory_region_t pmap_memory_regions
[PMAP_MEMORY_REGIONS_SIZE
];
219 * Other useful macros.
221 #define current_pmap() (vm_map_pmap(current_thread()->map))
223 struct pmap kernel_pmap_store
;
226 struct zone
*pmap_zone
; /* zone of pmap structures */
228 struct zone
*pmap_anchor_zone
;
229 int pmap_debug
= 0; /* flag for debugging prints */
231 unsigned int inuse_ptepages_count
= 0;
232 long long alloc_ptepages_count
__attribute__((aligned(8))) = 0; /* aligned for atomic access */
233 unsigned int bootstrap_wired_pages
= 0;
234 int pt_fake_zone_index
= -1;
236 extern long NMIPI_acks
;
238 boolean_t kernel_text_ps_4K
= TRUE
;
239 boolean_t wpkernel
= TRUE
;
245 pt_entry_t
*DMAP1
, *DMAP2
;
249 const boolean_t pmap_disable_kheap_nx
= FALSE
;
250 const boolean_t pmap_disable_kstack_nx
= FALSE
;
251 extern boolean_t doconstro_override
;
253 extern long __stack_chk_guard
[];
256 * Map memory at initialization. The physical addresses being
257 * mapped are not managed and are never unmapped.
259 * For now, VM is already on, we only need to map the
265 vm_map_offset_t start_addr
,
266 vm_map_offset_t end_addr
,
273 while (start_addr
< end_addr
) {
274 pmap_enter(kernel_pmap
, (vm_map_offset_t
)virt
,
275 (ppnum_t
) i386_btop(start_addr
), prot
, VM_PROT_NONE
, flags
, TRUE
);
282 extern char *first_avail
;
283 extern vm_offset_t virtual_avail
, virtual_end
;
284 extern pmap_paddr_t avail_start
, avail_end
;
285 extern vm_offset_t sHIB
;
286 extern vm_offset_t eHIB
;
287 extern vm_offset_t stext
;
288 extern vm_offset_t etext
;
289 extern vm_offset_t sdata
, edata
;
290 extern vm_offset_t sconstdata
, econstdata
;
292 extern void *KPTphys
;
294 boolean_t pmap_smep_enabled
= FALSE
;
295 boolean_t pmap_smap_enabled
= FALSE
;
300 cpu_data_t
*cdp
= current_cpu_datap();
302 * Here early in the life of a processor (from cpu_mode_init()).
303 * Ensure global page feature is disabled at this point.
306 set_cr4(get_cr4() &~ CR4_PGE
);
309 * Initialize the per-cpu, TLB-related fields.
311 cdp
->cpu_kernel_cr3
= kernel_pmap
->pm_cr3
;
312 cdp
->cpu_active_cr3
= kernel_pmap
->pm_cr3
;
313 cdp
->cpu_tlb_invalid
= FALSE
;
314 cdp
->cpu_task_map
= TASK_MAP_64BIT
;
315 pmap_pcid_configure();
316 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMEP
) {
318 if (!PE_parse_boot_argn("-pmap_smep_disable", &nsmep
, sizeof(nsmep
))) {
319 set_cr4(get_cr4() | CR4_SMEP
);
320 pmap_smep_enabled
= TRUE
;
323 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMAP
) {
325 if (!PE_parse_boot_argn("-pmap_smap_disable", &nsmap
, sizeof(nsmap
))) {
326 set_cr4(get_cr4() | CR4_SMAP
);
327 pmap_smap_enabled
= TRUE
;
331 if (cdp
->cpu_fixed_pmcs_enabled
) {
332 boolean_t enable
= TRUE
;
333 cpu_pmc_control(&enable
);
337 static uint32_t pmap_scale_shift(void) {
340 if (sane_size
<= 8*GB
) {
341 scale
= (uint32_t)(sane_size
/ (2 * GB
));
342 } else if (sane_size
<= 32*GB
) {
343 scale
= 4 + (uint32_t)((sane_size
- (8 * GB
))/ (4 * GB
));
345 scale
= 10 + (uint32_t)MIN(4, ((sane_size
- (32 * GB
))/ (8 * GB
)));
351 * Bootstrap the system enough to run with virtual memory.
352 * Map the kernel's code and data, and allocate the system page table.
353 * Called with mapping OFF. Page_size must already be set.
358 __unused vm_offset_t load_start
,
359 __unused boolean_t IA32e
)
361 #if NCOPY_WINDOWS > 0
367 vm_last_addr
= VM_MAX_KERNEL_ADDRESS
; /* Set the highest address
370 * The kernel's pmap is statically allocated so we don't
371 * have to use pmap_create, which is unlikely to work
372 * correctly at this part of the boot sequence.
375 kernel_pmap
= &kernel_pmap_store
;
376 kernel_pmap
->ref_count
= 1;
377 kernel_pmap
->nx_enabled
= TRUE
;
378 kernel_pmap
->pm_task_map
= TASK_MAP_64BIT
;
379 kernel_pmap
->pm_obj
= (vm_object_t
) NULL
;
380 kernel_pmap
->dirbase
= (pd_entry_t
*)((uintptr_t)IdlePTD
);
381 kernel_pmap
->pm_pdpt
= (pd_entry_t
*) ((uintptr_t)IdlePDPT
);
382 kernel_pmap
->pm_pml4
= IdlePML4
;
383 kernel_pmap
->pm_cr3
= (uintptr_t)ID_MAP_VTOP(IdlePML4
);
384 pmap_pcid_initialize_kernel(kernel_pmap
);
388 current_cpu_datap()->cpu_kernel_cr3
= (addr64_t
) kernel_pmap
->pm_cr3
;
391 OSAddAtomic(NKPT
, &inuse_ptepages_count
);
392 OSAddAtomic64(NKPT
, &alloc_ptepages_count
);
393 bootstrap_wired_pages
= NKPT
;
395 virtual_avail
= (vm_offset_t
)(VM_MIN_KERNEL_ADDRESS
) + (vm_offset_t
)first_avail
;
396 virtual_end
= (vm_offset_t
)(VM_MAX_KERNEL_ADDRESS
);
398 #if NCOPY_WINDOWS > 0
400 * Reserve some special page table entries/VA space for temporary
403 #define SYSMAP(c, p, v, n) \
404 v = (c)va; va += ((n)*INTEL_PGBYTES);
408 for (i
=0; i
<PMAP_NWINDOWS
; i
++) {
410 kprintf("trying to do SYSMAP idx %d %p\n", i
,
411 current_cpu_datap());
412 kprintf("cpu_pmap %p\n", current_cpu_datap()->cpu_pmap
);
413 kprintf("mapwindow %p\n", current_cpu_datap()->cpu_pmap
->mapwindow
);
414 kprintf("two stuff %p %p\n",
415 (void *)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
),
416 (void *)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
));
419 (current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
),
420 (current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
),
422 current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
=
423 &(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP_store
);
424 *current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
= 0;
427 /* DMAP user for debugger */
428 SYSMAP(caddr_t
, DMAP1
, DADDR1
, 1);
429 SYSMAP(caddr_t
, DMAP2
, DADDR2
, 1); /* XXX temporary - can remove */
433 if (!PE_parse_boot_argn("npvhash", &npvhashmask
, sizeof (npvhashmask
))) {
434 npvhashmask
= ((NPVHASHBUCKETS
) << pmap_scale_shift()) - 1;
438 npvhashbuckets
= npvhashmask
+ 1;
440 if (0 != ((npvhashbuckets
) & npvhashmask
)) {
441 panic("invalid hash %d, must be ((2^N)-1), "
442 "using default %d\n", npvhashmask
, NPVHASHMASK
);
445 simple_lock_init(&kernel_pmap
->lock
, 0);
446 simple_lock_init(&pv_hashed_free_list_lock
, 0);
447 simple_lock_init(&pv_hashed_kern_free_list_lock
, 0);
448 simple_lock_init(&pv_hash_table_lock
,0);
449 simple_lock_init(&phys_backup_lock
, 0);
454 printf("PMAP: PCID enabled\n");
456 if (pmap_smep_enabled
)
457 printf("PMAP: Supervisor Mode Execute Protection enabled\n");
458 if (pmap_smap_enabled
)
459 printf("PMAP: Supervisor Mode Access Protection enabled\n");
462 printf("Stack canary: 0x%lx\n", __stack_chk_guard
[0]);
463 printf("early_random(): 0x%qx\n", early_random());
466 /* Check if the user has requested disabling stack or heap no-execute
467 * enforcement. These are "const" variables; that qualifier is cast away
468 * when altering them. The TEXT/DATA const sections are marked
469 * write protected later in the kernel startup sequence, so altering
470 * them is possible at this point, in pmap_bootstrap().
472 if (PE_parse_boot_argn("-pmap_disable_kheap_nx", &ptmp
, sizeof(ptmp
))) {
473 boolean_t
*pdknxp
= (boolean_t
*) &pmap_disable_kheap_nx
;
477 if (PE_parse_boot_argn("-pmap_disable_kstack_nx", &ptmp
, sizeof(ptmp
))) {
478 boolean_t
*pdknhp
= (boolean_t
*) &pmap_disable_kstack_nx
;
482 boot_args
*args
= (boot_args
*)PE_state
.bootArgs
;
483 if (args
->efiMode
== kBootArgsEfiMode32
) {
484 printf("EFI32: kernel virtual space limited to 4GB\n");
485 virtual_end
= VM_MAX_KERNEL_ADDRESS_EFI32
;
487 kprintf("Kernel virtual space from 0x%lx to 0x%lx.\n",
488 (long)KERNEL_BASE
, (long)virtual_end
);
489 kprintf("Available physical space from 0x%llx to 0x%llx\n",
490 avail_start
, avail_end
);
493 * The -no_shared_cr3 boot-arg is a debugging feature (set by default
494 * in the DEBUG kernel) to force the kernel to switch to its own map
495 * (and cr3) when control is in kernelspace. The kernel's map does not
496 * include (i.e. share) userspace so wild references will cause
497 * a panic. Only copyin and copyout are exempt from this.
499 (void) PE_parse_boot_argn("-no_shared_cr3",
500 &no_shared_cr3
, sizeof (no_shared_cr3
));
502 kprintf("Kernel not sharing user map\n");
505 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace
, sizeof (pmap_trace
))) {
506 kprintf("Kernel traces for pmap operations enabled\n");
508 #endif /* PMAP_TRACES */
516 *startp
= virtual_avail
;
525 #include <IOKit/IOHibernatePrivate.h>
528 int32_t pmap_teardown_last_valid_compact_indx
= -1;
531 void hibernate_rebuild_pmap_structs(void);
532 void hibernate_teardown_pmap_structs(addr64_t
*, addr64_t
*);
533 void pmap_pack_index(uint32_t);
534 int32_t pmap_unpack_index(pv_rooted_entry_t
);
538 pmap_unpack_index(pv_rooted_entry_t pv_h
)
542 indx
= (int32_t)(*((uint64_t *)(&pv_h
->qlink
.next
)) >> 48);
544 indx
|= (int32_t)(*((uint64_t *)(&pv_h
->qlink
.prev
)) >> 48);
546 *((uint64_t *)(&pv_h
->qlink
.next
)) |= ((uint64_t)0xffff << 48);
547 *((uint64_t *)(&pv_h
->qlink
.prev
)) |= ((uint64_t)0xffff << 48);
554 pmap_pack_index(uint32_t indx
)
556 pv_rooted_entry_t pv_h
;
558 pv_h
= &pv_head_table
[indx
];
560 *((uint64_t *)(&pv_h
->qlink
.next
)) &= ~((uint64_t)0xffff << 48);
561 *((uint64_t *)(&pv_h
->qlink
.prev
)) &= ~((uint64_t)0xffff << 48);
563 *((uint64_t *)(&pv_h
->qlink
.next
)) |= ((uint64_t)(indx
>> 16)) << 48;
564 *((uint64_t *)(&pv_h
->qlink
.prev
)) |= ((uint64_t)(indx
& 0xffff)) << 48;
569 hibernate_teardown_pmap_structs(addr64_t
*unneeded_start
, addr64_t
*unneeded_end
)
572 int32_t compact_target_indx
;
574 compact_target_indx
= 0;
576 for (i
= 0; i
< pmap_npages
; i
++) {
577 if (pv_head_table
[i
].pmap
== PMAP_NULL
) {
579 if (pv_head_table
[compact_target_indx
].pmap
!= PMAP_NULL
)
580 compact_target_indx
= i
;
582 pmap_pack_index((uint32_t)i
);
584 if (pv_head_table
[compact_target_indx
].pmap
== PMAP_NULL
) {
586 * we've got a hole to fill, so
587 * move this pv_rooted_entry_t to it's new home
589 pv_head_table
[compact_target_indx
] = pv_head_table
[i
];
590 pv_head_table
[i
].pmap
= PMAP_NULL
;
592 pmap_teardown_last_valid_compact_indx
= compact_target_indx
;
593 compact_target_indx
++;
595 pmap_teardown_last_valid_compact_indx
= i
;
598 *unneeded_start
= (addr64_t
)&pv_head_table
[pmap_teardown_last_valid_compact_indx
+1];
599 *unneeded_end
= (addr64_t
)&pv_head_table
[pmap_npages
-1];
601 HIBLOG("hibernate_teardown_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx
);
606 hibernate_rebuild_pmap_structs(void)
608 int32_t cindx
, eindx
, rindx
;
609 pv_rooted_entry_t pv_h
;
611 eindx
= (int32_t)pmap_npages
;
613 for (cindx
= pmap_teardown_last_valid_compact_indx
; cindx
>= 0; cindx
--) {
615 pv_h
= &pv_head_table
[cindx
];
617 rindx
= pmap_unpack_index(pv_h
);
618 assert(rindx
< pmap_npages
);
620 if (rindx
!= cindx
) {
622 * this pv_rooted_entry_t was moved by hibernate_teardown_pmap_structs,
623 * so move it back to its real location
625 pv_head_table
[rindx
] = pv_head_table
[cindx
];
627 if (rindx
+1 != eindx
) {
629 * the 'hole' between this vm_rooted_entry_t and the previous
630 * vm_rooted_entry_t we moved needs to be initialized as
631 * a range of zero'd vm_rooted_entry_t's
633 bzero((char *)&pv_head_table
[rindx
+1], (eindx
- rindx
- 1) * sizeof (struct pv_rooted_entry
));
638 bzero ((char *)&pv_head_table
[0], rindx
* sizeof (struct pv_rooted_entry
));
640 HIBLOG("hibernate_rebuild_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx
);
646 * Initialize the pmap module.
647 * Called by vm_init, to initialize any structures that the pmap
648 * system needs to map virtual memory.
656 vm_map_offset_t vaddr
;
660 kernel_pmap
->pm_obj_pml4
= &kpml4obj_object_store
;
661 _vm_object_allocate((vm_object_size_t
)NPML4PGS
* PAGE_SIZE
, &kpml4obj_object_store
);
663 kernel_pmap
->pm_obj_pdpt
= &kpdptobj_object_store
;
664 _vm_object_allocate((vm_object_size_t
)NPDPTPGS
* PAGE_SIZE
, &kpdptobj_object_store
);
666 kernel_pmap
->pm_obj
= &kptobj_object_store
;
667 _vm_object_allocate((vm_object_size_t
)NPDEPGS
* PAGE_SIZE
, &kptobj_object_store
);
670 * Allocate memory for the pv_head_table and its lock bits,
671 * the modify bit array, and the pte_page table.
675 * zero bias all these arrays now instead of off avail_start
676 * so we cover all memory
679 npages
= i386_btop(avail_end
);
681 pmap_npages
= (uint32_t)npages
;
683 s
= (vm_size_t
) (sizeof(struct pv_rooted_entry
) * npages
684 + (sizeof (struct pv_hashed_entry_t
*) * (npvhashbuckets
))
685 + pv_lock_table_size(npages
)
686 + pv_hash_lock_table_size((npvhashbuckets
))
690 if (kernel_memory_allocate(kernel_map
, &addr
, s
, 0,
691 KMA_KOBJECT
| KMA_PERMANENT
)
695 memset((char *)addr
, 0, s
);
701 if (0 == npvhashmask
) panic("npvhashmask not initialized");
705 * Allocate the structures first to preserve word-alignment.
707 pv_head_table
= (pv_rooted_entry_t
) addr
;
708 addr
= (vm_offset_t
) (pv_head_table
+ npages
);
710 pv_hash_table
= (pv_hashed_entry_t
*)addr
;
711 addr
= (vm_offset_t
) (pv_hash_table
+ (npvhashbuckets
));
713 pv_lock_table
= (char *) addr
;
714 addr
= (vm_offset_t
) (pv_lock_table
+ pv_lock_table_size(npages
));
716 pv_hash_lock_table
= (char *) addr
;
717 addr
= (vm_offset_t
) (pv_hash_lock_table
+ pv_hash_lock_table_size((npvhashbuckets
)));
719 pmap_phys_attributes
= (char *) addr
;
721 ppnum_t last_pn
= i386_btop(avail_end
);
723 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
724 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
725 if (pmptr
->type
!= kEfiConventionalMemory
)
728 for (pn
= pmptr
->base
; pn
<= pmptr
->end
; pn
++) {
730 pmap_phys_attributes
[pn
] |= PHYS_MANAGED
;
732 if (pn
> last_managed_page
)
733 last_managed_page
= pn
;
735 if (pn
>= lowest_hi
&& pn
<= highest_hi
)
736 pmap_phys_attributes
[pn
] |= PHYS_NOENCRYPT
;
741 ppn
= pmap_find_phys(kernel_pmap
, vaddr
);
743 pmap_phys_attributes
[ppn
] |= PHYS_NOENCRYPT
;
749 * Create the zone of physical maps,
750 * and of the physical-to-virtual entries.
752 s
= (vm_size_t
) sizeof(struct pmap
);
753 pmap_zone
= zinit(s
, 400*s
, 4096, "pmap"); /* XXX */
754 zone_change(pmap_zone
, Z_NOENCRYPT
, TRUE
);
756 pmap_anchor_zone
= zinit(PAGE_SIZE
, task_max
, PAGE_SIZE
, "pagetable anchors");
757 zone_change(pmap_anchor_zone
, Z_NOENCRYPT
, TRUE
);
759 /* The anchor is required to be page aligned. Zone debugging adds
760 * padding which may violate that requirement. Tell the zone
761 * subsystem that alignment is required.
764 zone_change(pmap_anchor_zone
, Z_ALIGNMENT_REQUIRED
, TRUE
);
766 s
= (vm_size_t
) sizeof(struct pv_hashed_entry
);
767 pv_hashed_list_zone
= zinit(s
, 10000*s
/* Expandable zone */,
768 4096 * 3 /* LCM x86_64*/, "pv_list");
769 zone_change(pv_hashed_list_zone
, Z_NOENCRYPT
, TRUE
);
771 /* create pv entries for kernel pages mapped by low level
772 startup code. these have to exist so we can pmap_remove()
773 e.g. kext pages from the middle of our addr space */
775 vaddr
= (vm_map_offset_t
) VM_MIN_KERNEL_ADDRESS
;
776 for (ppn
= VM_MIN_KERNEL_PAGE
; ppn
< i386_btop(avail_start
); ppn
++) {
777 pv_rooted_entry_t pv_e
;
779 pv_e
= pai_to_pvh(ppn
);
782 pv_e
->pmap
= kernel_pmap
;
783 queue_init(&pv_e
->qlink
);
785 pmap_initialized
= TRUE
;
787 max_preemption_latency_tsc
= tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS
, tscFCvtn2t
);
790 * Ensure the kernel's PML4 entry exists for the basement
791 * before this is shared with any user.
793 pmap_expand_pml4(kernel_pmap
, KERNEL_BASEMENT
, PMAP_EXPAND_OPTIONS_NONE
);
797 void pmap_mark_range(pmap_t npmap
, uint64_t sv
, uint64_t nxrosz
, boolean_t NX
, boolean_t ro
) {
798 uint64_t ev
= sv
+ nxrosz
, cv
= sv
;
800 pt_entry_t
*ptep
= NULL
;
802 assert(((sv
& 0xFFFULL
) | (nxrosz
& 0xFFFULL
)) == 0);
804 for (pdep
= pmap_pde(npmap
, cv
); pdep
!= NULL
&& (cv
< ev
);) {
805 uint64_t pdev
= (cv
& ~((uint64_t)PDEMASK
));
807 if (*pdep
& INTEL_PTE_PS
) {
809 *pdep
|= INTEL_PTE_NX
;
811 *pdep
&= ~INTEL_PTE_WRITE
;
813 cv
&= ~((uint64_t) PDEMASK
);
814 pdep
= pmap_pde(npmap
, cv
);
818 for (ptep
= pmap_pte(npmap
, cv
); ptep
!= NULL
&& (cv
< (pdev
+ NBPD
)) && (cv
< ev
);) {
820 *ptep
|= INTEL_PTE_NX
;
822 *ptep
&= ~INTEL_PTE_WRITE
;
824 ptep
= pmap_pte(npmap
, cv
);
827 DPRINTF("%s(0x%llx, 0x%llx, %u, %u): 0x%llx, 0x%llx\n", __FUNCTION__
, sv
, nxrosz
, NX
, ro
, cv
, ptep
? *ptep
: 0);
831 * Called once VM is fully initialized so that we can release unused
832 * sections of low memory to the general pool.
833 * Also complete the set-up of identity-mapped sections of the kernel:
834 * 1) write-protect kernel text
835 * 2) map kernel text using large pages if possible
836 * 3) read and write-protect page zero (for K32)
837 * 4) map the global page at the appropriate virtual address.
841 * To effectively map and write-protect all kernel text pages, the text
842 * must be 2M-aligned at the base, and the data section above must also be
843 * 2M-aligned. That is, there's padding below and above. This is achieved
844 * through linker directives. Large pages are used only if this alignment
845 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
850 * sdata: ================== 2Meg
854 * etext: ------------------
862 * stext: ================== 2Meg
866 * eHIB: ------------------
870 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
871 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
872 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
873 * The now unused level-1 PTE pages are also freed.
875 extern ppnum_t vm_kernel_base_page
;
877 pmap_lowmem_finalize(void)
883 * Update wired memory statistics for early boot pages
885 PMAP_ZINFO_PALLOC(kernel_pmap
, bootstrap_wired_pages
* PAGE_SIZE
);
888 * Free pages in pmap regions below the base:
890 * We can't free all the pages to VM that EFI reports available.
891 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
892 * There's also a size miscalculation here: pend is one page less
893 * than it should be but this is not fixed to be backwards
895 * This is important for KASLR because up to 256*2MB = 512MB of space
896 * needs has to be released to VM.
899 pmap_memory_regions
[i
].end
< vm_kernel_base_page
;
901 vm_offset_t pbase
= i386_ptob(pmap_memory_regions
[i
].base
);
902 vm_offset_t pend
= i386_ptob(pmap_memory_regions
[i
].end
+1);
904 DBG("pmap region %d [%p..[%p\n",
905 i
, (void *) pbase
, (void *) pend
);
907 if (pmap_memory_regions
[i
].attribute
& EFI_MEMORY_KERN_RESERVED
)
911 * Adjust limits not to free pages in range 0xc0000-0xff000.
913 if (pbase
>= 0xc0000 && pend
<= 0x100000)
915 if (pbase
< 0xc0000 && pend
> 0x100000) {
916 /* page range entirely within region, free lower part */
917 DBG("- ml_static_mfree(%p,%p)\n",
918 (void *) ml_static_ptovirt(pbase
),
919 (void *) (0xc0000-pbase
));
920 ml_static_mfree(ml_static_ptovirt(pbase
),0xc0000-pbase
);
924 pend
= MIN(pend
, 0xc0000);
926 pbase
= MAX(pbase
, 0x100000);
927 DBG("- ml_static_mfree(%p,%p)\n",
928 (void *) ml_static_ptovirt(pbase
),
929 (void *) (pend
- pbase
));
930 ml_static_mfree(ml_static_ptovirt(pbase
), pend
- pbase
);
933 /* A final pass to get rid of all initial identity mappings to
936 DPRINTF("%s: Removing mappings from 0->0x%lx\n", __FUNCTION__
, vm_kernel_base
);
939 * Remove all mappings past the boot-cpu descriptor aliases and low globals.
940 * Non-boot-cpu GDT aliases will be remapped later as needed.
942 pmap_remove(kernel_pmap
, LOWGLOBAL_ALIAS
+ PAGE_SIZE
, vm_kernel_base
);
945 * If text and data are both 2MB-aligned,
946 * we can map text with large-pages,
947 * unless the -kernel_text_ps_4K boot-arg overrides.
949 if ((stext
& I386_LPGMASK
) == 0 && (sdata
& I386_LPGMASK
) == 0) {
950 kprintf("Kernel text is 2MB aligned");
951 kernel_text_ps_4K
= FALSE
;
952 if (PE_parse_boot_argn("-kernel_text_ps_4K",
954 sizeof (kernel_text_ps_4K
)))
955 kprintf(" but will be mapped with 4K pages\n");
957 kprintf(" and will be mapped with 2M pages\n");
960 (void) PE_parse_boot_argn("wpkernel", &wpkernel
, sizeof (wpkernel
));
962 kprintf("Kernel text %p-%p to be write-protected\n",
963 (void *) stext
, (void *) etext
);
968 * Scan over text if mappings are to be changed:
969 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
970 * - Change to large-pages if possible and not overriden.
972 if (kernel_text_ps_4K
&& wpkernel
) {
974 for (myva
= stext
; myva
< etext
; myva
+= PAGE_SIZE
) {
977 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
979 pmap_store_pte(ptep
, *ptep
& ~INTEL_PTE_WRITE
);
983 if (!kernel_text_ps_4K
) {
987 * Release zero-filled page padding used for 2M-alignment.
989 DBG("ml_static_mfree(%p,%p) for padding below text\n",
990 (void *) eHIB
, (void *) (stext
- eHIB
));
991 ml_static_mfree(eHIB
, stext
- eHIB
);
992 DBG("ml_static_mfree(%p,%p) for padding above text\n",
993 (void *) etext
, (void *) (sdata
- etext
));
994 ml_static_mfree(etext
, sdata
- etext
);
997 * Coalesce text pages into large pages.
999 for (myva
= stext
; myva
< sdata
; myva
+= I386_LPGBYTES
) {
1001 vm_offset_t pte_phys
;
1005 pdep
= pmap_pde(kernel_pmap
, (vm_map_offset_t
)myva
);
1006 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
1007 DBG("myva: %p pdep: %p ptep: %p\n",
1008 (void *) myva
, (void *) pdep
, (void *) ptep
);
1009 if ((*ptep
& INTEL_PTE_VALID
) == 0)
1011 pte_phys
= (vm_offset_t
)(*ptep
& PG_FRAME
);
1012 pde
= *pdep
& PTMASK
; /* page attributes from pde */
1013 pde
|= INTEL_PTE_PS
; /* make it a 2M entry */
1014 pde
|= pte_phys
; /* take page frame from pte */
1017 pde
&= ~INTEL_PTE_WRITE
;
1018 DBG("pmap_store_pte(%p,0x%llx)\n",
1020 pmap_store_pte(pdep
, pde
);
1023 * Free the now-unused level-1 pte.
1024 * Note: ptep is a virtual address to the pte in the
1025 * recursive map. We can't use this address to free
1026 * the page. Instead we need to compute its address
1027 * in the Idle PTEs in "low memory".
1029 vm_offset_t vm_ptep
= (vm_offset_t
) KPTphys
1030 + (pte_phys
>> PTPGSHIFT
);
1031 DBG("ml_static_mfree(%p,0x%x) for pte\n",
1032 (void *) vm_ptep
, PAGE_SIZE
);
1033 ml_static_mfree(vm_ptep
, PAGE_SIZE
);
1036 /* Change variable read by sysctl machdep.pmap */
1037 pmap_kernel_text_ps
= I386_LPGBYTES
;
1040 boolean_t doconstro
= TRUE
;
1042 (void) PE_parse_boot_argn("dataconstro", &doconstro
, sizeof(doconstro
));
1044 if ((sconstdata
| econstdata
) & PAGE_MASK
) {
1045 kprintf("Const DATA misaligned 0x%lx 0x%lx\n", sconstdata
, econstdata
);
1046 if ((sconstdata
& PAGE_MASK
) || (doconstro_override
== FALSE
))
1050 if ((sconstdata
> edata
) || (sconstdata
< sdata
) || ((econstdata
- sconstdata
) >= (edata
- sdata
))) {
1051 kprintf("Const DATA incorrect size 0x%lx 0x%lx 0x%lx 0x%lx\n", sconstdata
, econstdata
, sdata
, edata
);
1056 kprintf("Marking const DATA read-only\n");
1060 for (dva
= sdata
; dva
< edata
; dva
+= I386_PGBYTES
) {
1061 assert(((sdata
| edata
) & PAGE_MASK
) == 0);
1062 if ( (sdata
| edata
) & PAGE_MASK
) {
1063 kprintf("DATA misaligned, 0x%lx, 0x%lx\n", sdata
, edata
);
1067 pt_entry_t dpte
, *dptep
= pmap_pte(kernel_pmap
, dva
);
1071 assert((dpte
& INTEL_PTE_VALID
));
1072 if ((dpte
& INTEL_PTE_VALID
) == 0) {
1073 kprintf("Missing data mapping 0x%lx 0x%lx 0x%lx\n", dva
, sdata
, edata
);
1077 dpte
|= INTEL_PTE_NX
;
1078 if (doconstro
&& (dva
>= sconstdata
) && (dva
< econstdata
)) {
1079 dpte
&= ~INTEL_PTE_WRITE
;
1081 pmap_store_pte(dptep
, dpte
);
1083 kernel_segment_command_t
* seg
;
1084 kernel_section_t
* sec
;
1086 for (seg
= firstseg(); seg
!= NULL
; seg
= nextsegfromheader(&_mh_execute_header
, seg
)) {
1087 if (!strcmp(seg
->segname
, "__TEXT") ||
1088 !strcmp(seg
->segname
, "__DATA")) {
1092 if (!strcmp(seg
->segname
, "__KLD")) {
1095 if (!strcmp(seg
->segname
, "__HIB")) {
1096 for (sec
= firstsect(seg
); sec
!= NULL
; sec
= nextsect(seg
, sec
)) {
1097 if (sec
->addr
& PAGE_MASK
)
1098 panic("__HIB segment's sections misaligned");
1099 if (!strcmp(sec
->sectname
, "__text")) {
1100 pmap_mark_range(kernel_pmap
, sec
->addr
, round_page(sec
->size
), FALSE
, TRUE
);
1102 pmap_mark_range(kernel_pmap
, sec
->addr
, round_page(sec
->size
), TRUE
, FALSE
);
1106 pmap_mark_range(kernel_pmap
, seg
->vmaddr
, round_page_64(seg
->vmsize
), TRUE
, FALSE
);
1111 * If we're debugging, map the low global vector page at the fixed
1112 * virtual address. Otherwise, remove the mapping for this.
1114 if (debug_boot_arg
) {
1115 pt_entry_t
*pte
= NULL
;
1116 if (0 == (pte
= pmap_pte(kernel_pmap
, LOWGLOBAL_ALIAS
)))
1117 panic("lowmem pte");
1118 /* make sure it is defined on page boundary */
1119 assert(0 == ((vm_offset_t
) &lowGlo
& PAGE_MASK
));
1120 pmap_store_pte(pte
, kvtophys((vm_offset_t
)&lowGlo
)
1128 pmap_remove(kernel_pmap
,
1129 LOWGLOBAL_ALIAS
, LOWGLOBAL_ALIAS
+ PAGE_SIZE
);
1133 if (pmap_pcid_ncpus
)
1140 * this function is only used for debugging fron the vm layer
1146 pv_rooted_entry_t pv_h
;
1150 assert(pn
!= vm_page_fictitious_addr
);
1152 if (!pmap_initialized
)
1155 if (pn
== vm_page_guard_addr
)
1158 pai
= ppn_to_pai(pn
);
1159 if (!IS_MANAGED_PAGE(pai
))
1161 pv_h
= pai_to_pvh(pn
);
1162 result
= (pv_h
->pmap
== PMAP_NULL
);
1169 vm_map_offset_t va_start
,
1170 vm_map_offset_t va_end
)
1172 vm_map_offset_t offset
;
1175 if (pmap
== PMAP_NULL
) {
1180 * Check the resident page count
1181 * - if it's zero, the pmap is completely empty.
1182 * This short-circuit test prevents a virtual address scan which is
1183 * painfully slow for 64-bit spaces.
1184 * This assumes the count is correct
1185 * .. the debug kernel ought to be checking perhaps by page table walk.
1187 if (pmap
->stats
.resident_count
== 0)
1190 for (offset
= va_start
;
1192 offset
+= PAGE_SIZE_64
) {
1193 phys_page
= pmap_find_phys(pmap
, offset
);
1195 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
1196 "page %d at 0x%llx\n",
1197 pmap
, va_start
, va_end
, phys_page
, offset
);
1207 * Create and return a physical map.
1209 * If the size specified for the map
1210 * is zero, the map is an actual physical
1211 * map, and may be referenced by the
1214 * If the size specified is non-zero,
1215 * the map will be used in software only, and
1216 * is bounded by that size.
1227 pml4_entry_t
*kpml4
;
1229 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_START
,
1230 (uint32_t) (sz
>>32), (uint32_t) sz
, is_64bit
, 0, 0);
1232 size
= (vm_size_t
) sz
;
1235 * A software use-only map doesn't even need a map.
1242 p
= (pmap_t
) zalloc(pmap_zone
);
1244 panic("pmap_create zalloc");
1245 /* Zero all fields */
1246 bzero(p
, sizeof(*p
));
1247 /* init counts now since we'll be bumping some */
1248 simple_lock_init(&p
->lock
, 0);
1250 p
->stats
.resident_count
= 0;
1251 p
->stats
.resident_max
= 0;
1252 p
->stats
.wired_count
= 0;
1254 bzero(&p
->stats
, sizeof (p
->stats
));
1258 p
->pm_shared
= FALSE
;
1259 ledger_reference(ledger
);
1262 p
->pm_task_map
= is_64bit
? TASK_MAP_64BIT
: TASK_MAP_32BIT
;;
1263 if (pmap_pcid_ncpus
)
1264 pmap_pcid_initialize(p
);
1266 p
->pm_pml4
= zalloc(pmap_anchor_zone
);
1268 pmap_assert((((uintptr_t)p
->pm_pml4
) & PAGE_MASK
) == 0);
1270 memset((char *)p
->pm_pml4
, 0, PAGE_SIZE
);
1272 p
->pm_cr3
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_pml4
);
1274 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
1276 p
->pm_obj_pml4
= vm_object_allocate((vm_object_size_t
)(NPML4PGS
) * PAGE_SIZE
);
1277 if (NULL
== p
->pm_obj_pml4
)
1278 panic("pmap_create pdpt obj");
1280 p
->pm_obj_pdpt
= vm_object_allocate((vm_object_size_t
)(NPDPTPGS
) * PAGE_SIZE
);
1281 if (NULL
== p
->pm_obj_pdpt
)
1282 panic("pmap_create pdpt obj");
1284 p
->pm_obj
= vm_object_allocate((vm_object_size_t
)(NPDEPGS
) * PAGE_SIZE
);
1285 if (NULL
== p
->pm_obj
)
1286 panic("pmap_create pte obj");
1288 /* All pmaps share the kernel's pml4 */
1289 pml4
= pmap64_pml4(p
, 0ULL);
1290 kpml4
= kernel_pmap
->pm_pml4
;
1291 pml4
[KERNEL_PML4_INDEX
] = kpml4
[KERNEL_PML4_INDEX
];
1292 pml4
[KERNEL_KEXTS_INDEX
] = kpml4
[KERNEL_KEXTS_INDEX
];
1293 pml4
[KERNEL_PHYSMAP_PML4_INDEX
] = kpml4
[KERNEL_PHYSMAP_PML4_INDEX
];
1295 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_START
,
1296 p
, is_64bit
, 0, 0, 0);
1302 * Retire the given physical map from service.
1303 * Should only be called if the map contains
1304 * no valid mappings.
1308 pmap_destroy(pmap_t p
)
1315 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_START
,
1322 pmap_assert((current_thread() && (current_thread()->map
)) ? (current_thread()->map
->pmap
!= p
) : TRUE
);
1326 * If some cpu is not using the physical pmap pointer that it
1327 * is supposed to be (see set_dirbase), we might be using the
1328 * pmap that is being destroyed! Make sure we are
1329 * physically on the right pmap:
1331 PMAP_UPDATE_TLBS(p
, 0x0ULL
, 0xFFFFFFFFFFFFF000ULL
);
1332 if (pmap_pcid_ncpus
)
1333 pmap_destroy_pcid_sync(p
);
1339 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
,
1341 pmap_assert(p
== kernel_pmap
);
1342 return; /* still in use */
1346 * Free the memory maps, then the
1349 int inuse_ptepages
= 0;
1351 zfree(pmap_anchor_zone
, p
->pm_pml4
);
1353 inuse_ptepages
+= p
->pm_obj_pml4
->resident_page_count
;
1354 vm_object_deallocate(p
->pm_obj_pml4
);
1356 inuse_ptepages
+= p
->pm_obj_pdpt
->resident_page_count
;
1357 vm_object_deallocate(p
->pm_obj_pdpt
);
1359 inuse_ptepages
+= p
->pm_obj
->resident_page_count
;
1360 vm_object_deallocate(p
->pm_obj
);
1362 OSAddAtomic(-inuse_ptepages
, &inuse_ptepages_count
);
1363 PMAP_ZINFO_PFREE(p
, inuse_ptepages
* PAGE_SIZE
);
1364 ledger_dereference(p
->ledger
);
1365 zfree(pmap_zone
, p
);
1367 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
,
1372 * Add a reference to the specified pmap.
1376 pmap_reference(pmap_t p
)
1378 if (p
!= PMAP_NULL
) {
1386 * Remove phys addr if mapped in specified map
1390 pmap_remove_some_phys(
1391 __unused pmap_t map
,
1392 __unused ppnum_t pn
)
1395 /* Implement to support working set code */
1403 vm_map_offset_t sva
,
1404 vm_map_offset_t eva
,
1407 pmap_protect_options(map
, sva
, eva
, prot
, 0, NULL
);
1412 * Set the physical protection on the
1413 * specified range of this map as requested.
1414 * Will not increase permissions.
1417 pmap_protect_options(
1419 vm_map_offset_t sva
,
1420 vm_map_offset_t eva
,
1422 unsigned int options
,
1426 pt_entry_t
*spte
, *epte
;
1427 vm_map_offset_t lva
;
1428 vm_map_offset_t orig_sva
;
1434 if (map
== PMAP_NULL
)
1437 if (prot
== VM_PROT_NONE
) {
1438 pmap_remove_options(map
, sva
, eva
, options
);
1441 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_START
,
1443 (uint32_t) (sva
>> 32), (uint32_t) sva
,
1444 (uint32_t) (eva
>> 32), (uint32_t) eva
);
1446 if ((prot
& VM_PROT_EXECUTE
) || !nx_enabled
|| !map
->nx_enabled
)
1455 lva
= (sva
+ pde_mapped_size
) & ~(pde_mapped_size
- 1);
1458 pde
= pmap_pde(map
, sva
);
1459 if (pde
&& (*pde
& INTEL_PTE_VALID
)) {
1460 if (*pde
& INTEL_PTE_PS
) {
1463 epte
= spte
+1; /* excluded */
1465 spte
= pmap_pte(map
, (sva
& ~(pde_mapped_size
- 1)));
1466 spte
= &spte
[ptenum(sva
)];
1467 epte
= &spte
[intel_btop(lva
- sva
)];
1470 for (; spte
< epte
; spte
++) {
1471 if (!(*spte
& INTEL_PTE_VALID
))
1474 if (prot
& VM_PROT_WRITE
)
1475 pmap_update_pte(spte
, 0, INTEL_PTE_WRITE
);
1477 pmap_update_pte(spte
, INTEL_PTE_WRITE
, 0);
1480 pmap_update_pte(spte
, 0, INTEL_PTE_NX
);
1482 pmap_update_pte(spte
, INTEL_PTE_NX
, 0);
1489 if (options
& PMAP_OPTIONS_NOFLUSH
)
1490 PMAP_UPDATE_TLBS_DELAYED(map
, orig_sva
, eva
, (pmap_flush_context
*)arg
);
1492 PMAP_UPDATE_TLBS(map
, orig_sva
, eva
);
1496 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_END
,
1501 /* Map a (possibly) autogenned block */
1510 __unused
unsigned int flags
)
1515 if (attr
& VM_MEM_SUPERPAGE
)
1516 cur_page_size
= SUPERPAGE_SIZE
;
1518 cur_page_size
= PAGE_SIZE
;
1520 for (page
= 0; page
< size
; page
+=cur_page_size
/PAGE_SIZE
) {
1521 pmap_enter(pmap
, va
, pa
, prot
, VM_PROT_NONE
, attr
, TRUE
);
1522 va
+= cur_page_size
;
1523 pa
+=cur_page_size
/PAGE_SIZE
;
1530 vm_map_offset_t vaddr
,
1531 unsigned int options
)
1537 pml4_entry_t
*pml4p
;
1539 DBG("pmap_expand_pml4(%p,%p)\n", map
, (void *)vaddr
);
1542 * Allocate a VM page for the pml4 page
1544 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1545 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
1546 return KERN_RESOURCE_SHORTAGE
;
1550 * put the page into the pmap's obj list so it
1551 * can be found later.
1555 i
= pml4idx(map
, vaddr
);
1562 vm_page_lockspin_queues();
1564 vm_page_unlock_queues();
1566 OSAddAtomic(1, &inuse_ptepages_count
);
1567 OSAddAtomic64(1, &alloc_ptepages_count
);
1568 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1570 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1571 vm_object_lock(map
->pm_obj_pml4
);
1575 * See if someone else expanded us first
1577 if (pmap64_pdpt(map
, vaddr
) != PDPT_ENTRY_NULL
) {
1579 vm_object_unlock(map
->pm_obj_pml4
);
1583 OSAddAtomic(-1, &inuse_ptepages_count
);
1584 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1585 return KERN_SUCCESS
;
1589 if (0 != vm_page_lookup(map
->pm_obj_pml4
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1590 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1591 map
, map
->pm_obj_pml4
, vaddr
, i
);
1594 vm_page_insert(m
, map
->pm_obj_pml4
, (vm_object_offset_t
)i
* PAGE_SIZE
);
1595 vm_object_unlock(map
->pm_obj_pml4
);
1598 * Set the page directory entry for this page table.
1600 pml4p
= pmap64_pml4(map
, vaddr
); /* refetch under lock */
1602 pmap_store_pte(pml4p
, pa_to_pte(pa
)
1609 return KERN_SUCCESS
;
1613 pmap_expand_pdpt(pmap_t map
, vm_map_offset_t vaddr
, unsigned int options
)
1619 pdpt_entry_t
*pdptp
;
1621 DBG("pmap_expand_pdpt(%p,%p)\n", map
, (void *)vaddr
);
1623 while ((pdptp
= pmap64_pdpt(map
, vaddr
)) == PDPT_ENTRY_NULL
) {
1624 kern_return_t pep4kr
= pmap_expand_pml4(map
, vaddr
, options
);
1625 if (pep4kr
!= KERN_SUCCESS
)
1630 * Allocate a VM page for the pdpt page
1632 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1633 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
1634 return KERN_RESOURCE_SHORTAGE
;
1639 * put the page into the pmap's obj list so it
1640 * can be found later.
1644 i
= pdptidx(map
, vaddr
);
1651 vm_page_lockspin_queues();
1653 vm_page_unlock_queues();
1655 OSAddAtomic(1, &inuse_ptepages_count
);
1656 OSAddAtomic64(1, &alloc_ptepages_count
);
1657 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1659 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1660 vm_object_lock(map
->pm_obj_pdpt
);
1664 * See if someone else expanded us first
1666 if (pmap64_pde(map
, vaddr
) != PD_ENTRY_NULL
) {
1668 vm_object_unlock(map
->pm_obj_pdpt
);
1672 OSAddAtomic(-1, &inuse_ptepages_count
);
1673 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1674 return KERN_SUCCESS
;
1678 if (0 != vm_page_lookup(map
->pm_obj_pdpt
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1679 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1680 map
, map
->pm_obj_pdpt
, vaddr
, i
);
1683 vm_page_insert(m
, map
->pm_obj_pdpt
, (vm_object_offset_t
)i
* PAGE_SIZE
);
1684 vm_object_unlock(map
->pm_obj_pdpt
);
1687 * Set the page directory entry for this page table.
1689 pdptp
= pmap64_pdpt(map
, vaddr
); /* refetch under lock */
1691 pmap_store_pte(pdptp
, pa_to_pte(pa
)
1698 return KERN_SUCCESS
;
1705 * Routine: pmap_expand
1707 * Expands a pmap to be able to map the specified virtual address.
1709 * Allocates new virtual memory for the P0 or P1 portion of the
1710 * pmap, then re-maps the physical pages that were in the old
1711 * pmap to be in the new pmap.
1713 * Must be called with the pmap system and the pmap unlocked,
1714 * since these must be unlocked to use vm_allocate or vm_deallocate.
1715 * Thus it must be called in a loop that checks whether the map
1716 * has been expanded enough.
1717 * (We won't loop forever, since page tables aren't shrunk.)
1722 vm_map_offset_t vaddr
,
1723 unsigned int options
)
1726 register vm_page_t m
;
1727 register pmap_paddr_t pa
;
1733 * For the kernel, the virtual address must be in or above the basement
1734 * which is for kexts and is in the 512GB immediately below the kernel..
1735 * XXX - should use VM_MIN_KERNEL_AND_KEXT_ADDRESS not KERNEL_BASEMENT
1737 if (map
== kernel_pmap
&&
1738 !(vaddr
>= KERNEL_BASEMENT
&& vaddr
<= VM_MAX_KERNEL_ADDRESS
))
1739 panic("pmap_expand: bad vaddr 0x%llx for kernel pmap", vaddr
);
1742 while ((pdp
= pmap64_pde(map
, vaddr
)) == PD_ENTRY_NULL
) {
1743 kern_return_t pepkr
= pmap_expand_pdpt(map
, vaddr
, options
);
1744 if (pepkr
!= KERN_SUCCESS
)
1749 * Allocate a VM page for the pde entries.
1751 while ((m
= vm_page_grab()) == VM_PAGE_NULL
) {
1752 if (options
& PMAP_EXPAND_OPTIONS_NOWAIT
)
1753 return KERN_RESOURCE_SHORTAGE
;
1758 * put the page into the pmap's obj list so it
1759 * can be found later.
1763 i
= pdeidx(map
, vaddr
);
1770 vm_page_lockspin_queues();
1772 vm_page_unlock_queues();
1774 OSAddAtomic(1, &inuse_ptepages_count
);
1775 OSAddAtomic64(1, &alloc_ptepages_count
);
1776 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1778 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1779 vm_object_lock(map
->pm_obj
);
1784 * See if someone else expanded us first
1786 if (pmap_pte(map
, vaddr
) != PT_ENTRY_NULL
) {
1788 vm_object_unlock(map
->pm_obj
);
1792 OSAddAtomic(-1, &inuse_ptepages_count
);
1793 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1794 return KERN_SUCCESS
;
1798 if (0 != vm_page_lookup(map
->pm_obj
, (vm_object_offset_t
)i
* PAGE_SIZE
)) {
1799 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
1800 map
, map
->pm_obj
, vaddr
, i
);
1803 vm_page_insert(m
, map
->pm_obj
, (vm_object_offset_t
)i
* PAGE_SIZE
);
1804 vm_object_unlock(map
->pm_obj
);
1807 * Set the page directory entry for this page table.
1809 pdp
= pmap_pde(map
, vaddr
);
1810 pmap_store_pte(pdp
, pa_to_pte(pa
)
1817 return KERN_SUCCESS
;
1820 /* On K64 machines with more than 32GB of memory, pmap_steal_memory
1821 * will allocate past the 1GB of pre-expanded virtual kernel area. This
1822 * function allocates all the page tables using memory from the same pool
1823 * that pmap_steal_memory uses, rather than calling vm_page_grab (which
1824 * isn't available yet). */
1826 pmap_pre_expand(pmap_t pmap
, vm_map_offset_t vaddr
)
1833 if(pmap64_pdpt(pmap
, vaddr
) == PDPT_ENTRY_NULL
) {
1834 if (!pmap_next_page_hi(&pn
))
1835 panic("pmap_pre_expand");
1839 pte
= pmap64_pml4(pmap
, vaddr
);
1841 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
1847 if(pmap64_pde(pmap
, vaddr
) == PD_ENTRY_NULL
) {
1848 if (!pmap_next_page_hi(&pn
))
1849 panic("pmap_pre_expand");
1853 pte
= pmap64_pdpt(pmap
, vaddr
);
1855 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
1861 if(pmap_pte(pmap
, vaddr
) == PT_ENTRY_NULL
) {
1862 if (!pmap_next_page_hi(&pn
))
1863 panic("pmap_pre_expand");
1867 pte
= pmap64_pde(pmap
, vaddr
);
1869 pmap_store_pte(pte
, pa_to_pte(i386_ptob(pn
))
1879 * pmap_sync_page_data_phys(ppnum_t pa)
1881 * Invalidates all of the instruction cache on a physical page and
1882 * pushes any dirty data from the data cache for the same physical page
1883 * Not required in i386.
1886 pmap_sync_page_data_phys(__unused ppnum_t pa
)
1892 * pmap_sync_page_attributes_phys(ppnum_t pa)
1894 * Write back and invalidate all cachelines on a physical page.
1897 pmap_sync_page_attributes_phys(ppnum_t pa
)
1899 cache_flush_page_phys(pa
);
1904 #ifdef CURRENTLY_UNUSED_AND_UNTESTED
1910 * Routine: pmap_collect
1912 * Garbage collects the physical map system for
1913 * pages which are no longer used.
1914 * Success need not be guaranteed -- that is, there
1915 * may well be pages which are not referenced, but
1916 * others may be collected.
1918 * Called by the pageout daemon when pages are scarce.
1924 register pt_entry_t
*pdp
, *ptp
;
1931 if (p
== kernel_pmap
)
1935 * Garbage collect map.
1939 for (pdp
= (pt_entry_t
*)p
->dirbase
;
1940 pdp
< (pt_entry_t
*)&p
->dirbase
[(UMAXPTDI
+1)];
1943 if (*pdp
& INTEL_PTE_VALID
) {
1944 if(*pdp
& INTEL_PTE_REF
) {
1945 pmap_store_pte(pdp
, *pdp
& ~INTEL_PTE_REF
);
1949 ptp
= pmap_pte(p
, pdetova(pdp
- (pt_entry_t
*)p
->dirbase
));
1950 eptp
= ptp
+ NPTEPG
;
1953 * If the pte page has any wired mappings, we cannot
1958 register pt_entry_t
*ptep
;
1959 for (ptep
= ptp
; ptep
< eptp
; ptep
++) {
1960 if (iswired(*ptep
)) {
1968 * Remove the virtual addresses mapped by this pte page.
1970 pmap_remove_range(p
,
1971 pdetova(pdp
- (pt_entry_t
*)p
->dirbase
),
1976 * Invalidate the page directory pointer.
1978 pmap_store_pte(pdp
, 0x0);
1983 * And free the pte page itself.
1986 register vm_page_t m
;
1988 vm_object_lock(p
->pm_obj
);
1990 m
= vm_page_lookup(p
->pm_obj
,(vm_object_offset_t
)(pdp
- (pt_entry_t
*)&p
->dirbase
[0]) * PAGE_SIZE
);
1991 if (m
== VM_PAGE_NULL
)
1992 panic("pmap_collect: pte page not in object");
1994 vm_object_unlock(p
->pm_obj
);
1998 OSAddAtomic(-1, &inuse_ptepages_count
);
1999 PMAP_ZINFO_PFREE(p
, PAGE_SIZE
);
2008 PMAP_UPDATE_TLBS(p
, 0x0, 0xFFFFFFFFFFFFF000ULL
);
2017 pmap_copy_page(ppnum_t src
, ppnum_t dst
)
2019 bcopy_phys((addr64_t
)i386_ptob(src
),
2020 (addr64_t
)i386_ptob(dst
),
2026 * Routine: pmap_pageable
2028 * Make the specified pages (by pmap, offset)
2029 * pageable (or not) as requested.
2031 * A page which is not pageable may not take
2032 * a fault; therefore, its page table entry
2033 * must remain valid for the duration.
2035 * This routine is merely advisory; pmap_enter
2036 * will specify that these pages are to be wired
2037 * down (or not) as appropriate.
2041 __unused pmap_t pmap
,
2042 __unused vm_map_offset_t start_addr
,
2043 __unused vm_map_offset_t end_addr
,
2044 __unused boolean_t pageable
)
2047 pmap
++; start_addr
++; end_addr
++; pageable
++;
2052 invalidate_icache(__unused vm_offset_t addr
,
2053 __unused
unsigned cnt
,
2060 flush_dcache(__unused vm_offset_t addr
,
2061 __unused
unsigned count
,
2069 * Constrain DTrace copyin/copyout actions
2071 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
2072 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
2074 kern_return_t
dtrace_copyio_preflight(__unused addr64_t va
)
2076 thread_t thread
= current_thread();
2078 if (current_map() == kernel_map
)
2079 return KERN_FAILURE
;
2080 else if (((ccr3
= get_cr3_base()) != thread
->map
->pmap
->pm_cr3
) && (no_shared_cr3
== FALSE
))
2081 return KERN_FAILURE
;
2082 else if (no_shared_cr3
&& (ccr3
!= kernel_pmap
->pm_cr3
))
2083 return KERN_FAILURE
;
2085 return KERN_SUCCESS
;
2088 kern_return_t
dtrace_copyio_postflight(__unused addr64_t va
)
2090 return KERN_SUCCESS
;
2092 #endif /* CONFIG_DTRACE */
2094 #include <mach_vm_debug.h>
2096 #include <vm/vm_debug.h>
2099 pmap_list_resident_pages(
2100 __unused pmap_t pmap
,
2101 __unused vm_offset_t
*listp
,
2106 #endif /* MACH_VM_DEBUG */
2110 /* temporary workaround */
2112 coredumpok(__unused vm_map_t map
, __unused vm_offset_t va
)
2117 ptep
= pmap_pte(map
->pmap
, va
);
2120 return ((*ptep
& (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
)) != (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
));
2128 phys_page_exists(ppnum_t pn
)
2130 assert(pn
!= vm_page_fictitious_addr
);
2132 if (!pmap_initialized
)
2135 if (pn
== vm_page_guard_addr
)
2138 if (!IS_MANAGED_PAGE(ppn_to_pai(pn
)))
2147 pmap_switch(pmap_t tpmap
)
2151 s
= splhigh(); /* Make sure interruptions are disabled */
2152 set_dirbase(tpmap
, current_thread(), cpu_number());
2158 * disable no-execute capability on
2159 * the specified pmap
2162 pmap_disable_NX(pmap_t pmap
)
2164 pmap
->nx_enabled
= 0;
2168 pt_fake_zone_init(int zone_index
)
2170 pt_fake_zone_index
= zone_index
;
2176 vm_size_t
*cur_size
,
2177 vm_size_t
*max_size
,
2178 vm_size_t
*elem_size
,
2179 vm_size_t
*alloc_size
,
2185 *count
= inuse_ptepages_count
;
2186 *cur_size
= PAGE_SIZE
* inuse_ptepages_count
;
2187 *max_size
= PAGE_SIZE
* (inuse_ptepages_count
+
2188 vm_page_inactive_count
+
2189 vm_page_active_count
+
2190 vm_page_free_count
);
2191 *elem_size
= PAGE_SIZE
;
2192 *alloc_size
= PAGE_SIZE
;
2193 *sum_size
= alloc_ptepages_count
* PAGE_SIZE
;
2202 pmap_flush_context_init(pmap_flush_context
*pfc
)
2205 pfc
->pfc_invalid_global
= 0;
2208 extern unsigned TLBTimeOut
;
2211 pmap_flush_context
*pfc
)
2213 unsigned int my_cpu
;
2215 unsigned int cpu_bit
;
2216 cpumask_t cpus_to_respond
= 0;
2217 cpumask_t cpus_to_signal
= 0;
2218 cpumask_t cpus_signaled
= 0;
2219 boolean_t flush_self
= FALSE
;
2222 mp_disable_preemption();
2224 my_cpu
= cpu_number();
2225 cpus_to_signal
= pfc
->pfc_cpus
;
2227 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS
) | DBG_FUNC_START
,
2228 NULL
, cpus_to_signal
, 0, 0, 0);
2230 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
&& cpus_to_signal
; cpu
++, cpu_bit
<<= 1) {
2232 if (cpus_to_signal
& cpu_bit
) {
2234 cpus_to_signal
&= ~cpu_bit
;
2236 if (!cpu_datap(cpu
)->cpu_running
)
2239 if (pfc
->pfc_invalid_global
& cpu_bit
)
2240 cpu_datap(cpu
)->cpu_tlb_invalid_global
= TRUE
;
2242 cpu_datap(cpu
)->cpu_tlb_invalid_local
= TRUE
;
2245 if (cpu
== my_cpu
) {
2249 if (CPU_CR3_IS_ACTIVE(cpu
)) {
2250 cpus_to_respond
|= cpu_bit
;
2251 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2255 cpus_signaled
= cpus_to_respond
;
2258 * Flush local tlb if required.
2259 * Do this now to overlap with other processors responding.
2261 if (flush_self
&& cpu_datap(my_cpu
)->cpu_tlb_invalid
!= FALSE
)
2262 process_pmap_updates();
2264 if (cpus_to_respond
) {
2266 deadline
= mach_absolute_time() +
2267 (TLBTimeOut
? TLBTimeOut
: LockTimeOut
);
2268 boolean_t is_timeout_traced
= FALSE
;
2271 * Wait for those other cpus to acknowledge
2273 while (cpus_to_respond
!= 0) {
2276 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2277 /* Consider checking local/global invalidity
2278 * as appropriate in the PCID case.
2280 if ((cpus_to_respond
& cpu_bit
) != 0) {
2281 if (!cpu_datap(cpu
)->cpu_running
||
2282 cpu_datap(cpu
)->cpu_tlb_invalid
== FALSE
||
2283 !CPU_CR3_IS_ACTIVE(cpu
)) {
2284 cpus_to_respond
&= ~cpu_bit
;
2288 if (cpus_to_respond
== 0)
2291 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2292 if (machine_timeout_suspended())
2294 if (TLBTimeOut
== 0) {
2295 if (is_timeout_traced
)
2297 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS_TO
),
2298 NULL
, cpus_to_signal
, cpus_to_respond
, 0, 0);
2299 is_timeout_traced
= TRUE
;
2302 pmap_tlb_flush_timeout
= TRUE
;
2303 orig_acks
= NMIPI_acks
;
2304 mp_cpus_NMIPI(cpus_to_respond
);
2306 panic("TLB invalidation IPI timeout: "
2307 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%lx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2308 cpus_to_respond
, orig_acks
, NMIPI_acks
);
2312 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS
) | DBG_FUNC_END
,
2313 NULL
, cpus_signaled
, flush_self
, 0, 0);
2315 mp_enable_preemption();
2320 * Called with pmap locked, we:
2321 * - scan through per-cpu data to see which other cpus need to flush
2322 * - send an IPI to each non-idle cpu to be flushed
2323 * - wait for all to signal back that they are inactive or we see that
2324 * they are at a safe point (idle).
2325 * - flush the local tlb if active for this pmap
2326 * - return ... the caller will unlock the pmap
2330 pmap_flush_tlbs(pmap_t pmap
, vm_map_offset_t startv
, vm_map_offset_t endv
, int options
, pmap_flush_context
*pfc
)
2333 unsigned int cpu_bit
;
2334 cpumask_t cpus_to_signal
;
2335 unsigned int my_cpu
= cpu_number();
2336 pmap_paddr_t pmap_cr3
= pmap
->pm_cr3
;
2337 boolean_t flush_self
= FALSE
;
2339 boolean_t pmap_is_shared
= (pmap
->pm_shared
|| (pmap
== kernel_pmap
));
2340 boolean_t need_global_flush
= FALSE
;
2341 uint32_t event_code
;
2343 assert((processor_avail_count
< 2) ||
2344 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
2346 event_code
= (pmap
== kernel_pmap
) ? PMAP_CODE(PMAP__FLUSH_KERN_TLBS
)
2347 : PMAP_CODE(PMAP__FLUSH_TLBS
);
2348 PMAP_TRACE_CONSTANT(event_code
| DBG_FUNC_START
,
2349 pmap
, options
, startv
, endv
, 0);
2352 * Scan other cpus for matching active or task CR3.
2353 * For idle cpus (with no active map) we mark them invalid but
2354 * don't signal -- they'll check as they go busy.
2358 if (pmap_pcid_ncpus
) {
2360 need_global_flush
= TRUE
;
2361 pmap_pcid_invalidate_all_cpus(pmap
);
2364 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2365 if (!cpu_datap(cpu
)->cpu_running
)
2367 uint64_t cpu_active_cr3
= CPU_GET_ACTIVE_CR3(cpu
);
2368 uint64_t cpu_task_cr3
= CPU_GET_TASK_CR3(cpu
);
2370 if ((pmap_cr3
== cpu_task_cr3
) ||
2371 (pmap_cr3
== cpu_active_cr3
) ||
2374 if (options
& PMAP_DELAY_TLB_FLUSH
) {
2375 if (need_global_flush
== TRUE
)
2376 pfc
->pfc_invalid_global
|= cpu_bit
;
2377 pfc
->pfc_cpus
|= cpu_bit
;
2381 if (cpu
== my_cpu
) {
2385 if (need_global_flush
== TRUE
)
2386 cpu_datap(cpu
)->cpu_tlb_invalid_global
= TRUE
;
2388 cpu_datap(cpu
)->cpu_tlb_invalid_local
= TRUE
;
2392 * We don't need to signal processors which will flush
2393 * lazily at the idle state or kernel boundary.
2394 * For example, if we're invalidating the kernel pmap,
2395 * processors currently in userspace don't need to flush
2396 * their TLBs until the next time they enter the kernel.
2397 * Alterations to the address space of a task active
2398 * on a remote processor result in a signal, to
2399 * account for copy operations. (There may be room
2400 * for optimization in such cases).
2401 * The order of the loads below with respect
2402 * to the store to the "cpu_tlb_invalid" field above
2403 * is important--hence the barrier.
2405 if (CPU_CR3_IS_ACTIVE(cpu
) &&
2406 (pmap_cr3
== CPU_GET_ACTIVE_CR3(cpu
) ||
2408 (pmap_cr3
== CPU_GET_TASK_CR3(cpu
)))) {
2409 cpus_to_signal
|= cpu_bit
;
2410 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2414 if ((options
& PMAP_DELAY_TLB_FLUSH
))
2418 * Flush local tlb if required.
2419 * Do this now to overlap with other processors responding.
2422 if (pmap_pcid_ncpus
) {
2423 pmap_pcid_validate_cpu(pmap
, my_cpu
);
2433 if (cpus_to_signal
) {
2434 cpumask_t cpus_to_respond
= cpus_to_signal
;
2436 deadline
= mach_absolute_time() +
2437 (TLBTimeOut
? TLBTimeOut
: LockTimeOut
);
2438 boolean_t is_timeout_traced
= FALSE
;
2441 * Wait for those other cpus to acknowledge
2443 while (cpus_to_respond
!= 0) {
2446 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2447 /* Consider checking local/global invalidity
2448 * as appropriate in the PCID case.
2450 if ((cpus_to_respond
& cpu_bit
) != 0) {
2451 if (!cpu_datap(cpu
)->cpu_running
||
2452 cpu_datap(cpu
)->cpu_tlb_invalid
== FALSE
||
2453 !CPU_CR3_IS_ACTIVE(cpu
)) {
2454 cpus_to_respond
&= ~cpu_bit
;
2458 if (cpus_to_respond
== 0)
2461 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2462 if (machine_timeout_suspended())
2464 if (TLBTimeOut
== 0) {
2465 /* cut tracepoint but don't panic */
2466 if (is_timeout_traced
)
2468 PMAP_TRACE_CONSTANT(
2469 PMAP_CODE(PMAP__FLUSH_TLBS_TO
),
2470 pmap
, cpus_to_signal
, cpus_to_respond
, 0, 0);
2471 is_timeout_traced
= TRUE
;
2474 pmap_tlb_flush_timeout
= TRUE
;
2475 orig_acks
= NMIPI_acks
;
2476 mp_cpus_NMIPI(cpus_to_respond
);
2478 panic("TLB invalidation IPI timeout: "
2479 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%lx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2480 cpus_to_respond
, orig_acks
, NMIPI_acks
);
2485 if (__improbable((pmap
== kernel_pmap
) && (flush_self
!= TRUE
))) {
2486 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
);
2490 PMAP_TRACE_CONSTANT(event_code
| DBG_FUNC_END
,
2491 pmap
, cpus_to_signal
, startv
, endv
, 0);
2496 process_pmap_updates(void)
2498 int ccpu
= cpu_number();
2499 pmap_assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2500 if (pmap_pcid_ncpus
) {
2501 pmap_pcid_validate_current();
2502 if (cpu_datap(ccpu
)->cpu_tlb_invalid_global
) {
2503 cpu_datap(ccpu
)->cpu_tlb_invalid
= FALSE
;
2507 cpu_datap(ccpu
)->cpu_tlb_invalid_local
= FALSE
;
2512 current_cpu_datap()->cpu_tlb_invalid
= FALSE
;
2520 pmap_update_interrupt(void)
2522 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_START
,
2525 if (current_cpu_datap()->cpu_tlb_invalid
)
2526 process_pmap_updates();
2528 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_END
,
2532 #include <mach/mach_vm.h> /* mach_vm_region_recurse() */
2533 /* Scan kernel pmap for W+X PTEs, scan kernel VM map for W+X map entries
2534 * and identify ranges with mismatched VM permissions and PTE permissions
2537 pmap_permissions_verify(pmap_t ipmap
, vm_map_t ivmmap
, vm_offset_t sv
, vm_offset_t ev
) {
2538 vm_offset_t cv
= sv
;
2539 kern_return_t rv
= KERN_SUCCESS
;
2540 uint64_t skip4
= 0, skip2
= 0;
2542 sv
&= ~PAGE_MASK_64
;
2543 ev
&= ~PAGE_MASK_64
;
2545 if (__improbable((cv
> 0x00007FFFFFFFFFFFULL
) &&
2546 (cv
< 0xFFFF800000000000ULL
))) {
2547 cv
= 0xFFFF800000000000ULL
;
2549 /* Potential inconsistencies from not holding pmap lock
2550 * but harmless for the moment.
2552 if (((cv
& PML4MASK
) == 0) && (pmap64_pml4(ipmap
, cv
) == 0)) {
2553 if ((cv
+ NBPML4
) > cv
)
2560 if (((cv
& PDMASK
) == 0) && (pmap_pde(ipmap
, cv
) == 0)) {
2561 if ((cv
+ NBPD
) > cv
)
2569 pt_entry_t
*ptep
= pmap_pte(ipmap
, cv
);
2570 if (ptep
&& (*ptep
& INTEL_PTE_VALID
)) {
2571 if (*ptep
& INTEL_PTE_WRITE
) {
2572 if (!(*ptep
& INTEL_PTE_NX
)) {
2573 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
)))));
2580 kprintf("Completed pmap scan\n");
2583 struct vm_region_submap_info_64 vbr
;
2584 mach_msg_type_number_t vbrcount
= 0;
2585 mach_vm_size_t vmsize
;
2587 uint32_t nesting_depth
= 0;
2593 vbrcount
= VM_REGION_SUBMAP_INFO_COUNT_64
;
2594 if((kret
= mach_vm_region_recurse(ivmmap
,
2595 (mach_vm_address_t
*) &cv
, &vmsize
, &nesting_depth
,
2596 (vm_region_recurse_info_t
)&vbr
,
2597 &vbrcount
)) != KERN_SUCCESS
) {
2609 if(kret
!= KERN_SUCCESS
)
2612 prot
= vbr
.protection
;
2614 if ((prot
& (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) == (VM_PROT_WRITE
| VM_PROT_EXECUTE
)) {
2615 kprintf("W+X map entry at address 0x%lx\n", cv
);
2621 for (pcv
= cv
; pcv
< cv
+ vmsize
; pcv
+= PAGE_SIZE
) {
2622 pt_entry_t
*ptep
= pmap_pte(ipmap
, pcv
);
2625 if ((ptep
== NULL
) || !(*ptep
& INTEL_PTE_VALID
))
2627 tprot
= VM_PROT_READ
;
2628 if (*ptep
& INTEL_PTE_WRITE
)
2629 tprot
|= VM_PROT_WRITE
;
2630 if ((*ptep
& INTEL_PTE_NX
) == 0)
2631 tprot
|= VM_PROT_EXECUTE
;
2632 if (tprot
!= prot
) {
2633 kprintf("PTE/map entry permissions mismatch at address 0x%lx, pte: 0x%llx, protection: 0x%x\n", pcv
, *ptep
, prot
);