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>
104 #include <kern/lock.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>
122 #include <i386/cpuid.h>
123 #include <i386/cpu_data.h>
124 #include <i386/cpu_number.h>
125 #include <i386/machine_cpu.h>
126 #include <i386/seg.h>
127 #include <i386/serial_io.h>
128 #include <i386/cpu_capabilities.h>
129 #include <i386/machine_routines.h>
130 #include <i386/proc_reg.h>
131 #include <i386/tsc.h>
132 #include <i386/acpi.h>
133 #include <i386/pmap_internal.h>
135 #include <vm/vm_protos.h>
138 #include <i386/mp_desc.h>
139 #include <i386/i386_lowmem.h>
140 #include <i386/lowglobals.h>
143 /* #define DEBUGINTERRUPTS 1 uncomment to ensure pmap callers have interrupts enabled */
144 #ifdef DEBUGINTERRUPTS
145 #define pmap_intr_assert() {if (processor_avail_count > 1 && !ml_get_interrupts_enabled()) panic("pmap interrupt assert %s, %d",__FILE__, __LINE__);}
147 #define pmap_intr_assert()
153 #define POSTCODE_DELAY 1
154 #include <i386/postcode.h>
155 #endif /* IWANTTODEBUG */
158 void dump_pmap(pmap_t
);
159 void dump_4GB_pdpt(pmap_t p
);
160 void dump_4GB_pdpt_thread(thread_t tp
);
163 int nx_enabled
= 1; /* enable no-execute protection */
164 #ifdef CONFIG_EMBEDDED
165 int allow_data_exec
= 0; /* no exec from data, embedded is hardcore like that */
167 int allow_data_exec
= VM_ABI_32
; /* 32-bit apps may execute data by default, 64-bit apps may not */
169 int allow_stack_exec
= 0; /* No apps may execute from the stack by default */
172 boolean_t cpu_64bit
= FALSE
;
174 const boolean_t cpu_64bit
= TRUE
;
176 boolean_t pmap_trace
= FALSE
;
178 uint64_t max_preemption_latency_tsc
= 0;
180 pv_hashed_entry_t
*pv_hash_table
; /* hash lists */
182 uint32_t npvhash
= 0;
185 * pv_list entries are kept on a list that can only be accessed
186 * with the pmap system locked (at SPLVM, not in the cpus_active set).
187 * The list is refilled from the pv_hashed_list_zone if it becomes empty.
189 pv_rooted_entry_t pv_free_list
= PV_ROOTED_ENTRY_NULL
; /* free list at SPLVM */
190 pv_hashed_entry_t pv_hashed_free_list
= PV_HASHED_ENTRY_NULL
;
191 pv_hashed_entry_t pv_hashed_kern_free_list
= PV_HASHED_ENTRY_NULL
;
192 decl_simple_lock_data(,pv_hashed_free_list_lock
)
193 decl_simple_lock_data(,pv_hashed_kern_free_list_lock
)
194 decl_simple_lock_data(,pv_hash_table_lock
)
196 zone_t pv_hashed_list_zone
; /* zone of pv_hashed_entry structures */
198 static zone_t pdpt_zone
;
201 * First and last physical addresses that we maintain any information
202 * for. Initialized to zero so that pmap operations done before
203 * pmap_init won't touch any non-existent structures.
205 boolean_t pmap_initialized
= FALSE
;/* Has pmap_init completed? */
207 static struct vm_object kptobj_object_store
;
208 static vm_object_t kptobj
;
211 * Index into pv_head table, its lock bits, and the modify/reference and managed bits
215 * Array of physical page attribites for managed pages.
216 * One byte per physical page.
218 char *pmap_phys_attributes
;
219 unsigned int last_managed_page
= 0;
221 uint64_t pde_mapped_size
;
223 const boolean_t pmap_disable_kheap_nx
= TRUE
;
224 const boolean_t pmap_disable_kstack_nx
= TRUE
;
228 extern int max_lock_loops
;
230 unsigned int loop_count; \
231 loop_count = disable_serial_output ? max_lock_loops \
233 #define LOOP_CHECK(msg, pmap) \
234 if (--loop_count == 0) { \
235 mp_disable_preemption(); \
236 kprintf("%s: cpu %d pmap %x\n", \
237 msg, cpu_number(), pmap); \
238 Debugger("deadlock detection"); \
239 mp_enable_preemption(); \
240 loop_count = max_lock_loops; \
242 #else /* USLOCK_DEBUG */
244 #define LOOP_CHECK(msg, pmap)
245 #endif /* USLOCK_DEBUG */
247 unsigned pmap_memory_region_count
;
248 unsigned pmap_memory_region_current
;
250 pmap_memory_region_t pmap_memory_regions
[PMAP_MEMORY_REGIONS_SIZE
];
253 * Other useful macros.
255 #define current_pmap() (vm_map_pmap(current_thread()->map))
257 struct pmap kernel_pmap_store
;
260 pd_entry_t high_shared_pde
;
261 pd_entry_t commpage64_pde
;
263 struct zone
*pmap_zone
; /* zone of pmap structures */
265 int pmap_debug
= 0; /* flag for debugging prints */
267 unsigned int inuse_ptepages_count
= 0;
268 long long alloc_ptepages_count
__attribute__((aligned(8))) = 0LL; /* aligned for atomic access */
269 unsigned int bootstrap_wired_pages
= 0;
270 int pt_fake_zone_index
= -1;
272 extern long NMIPI_acks
;
274 addr64_t kernel64_cr3
;
275 boolean_t no_shared_cr3
= FALSE
; /* -no_shared_cr3 boot arg */
277 boolean_t kernel_text_ps_4K
= TRUE
;
278 boolean_t wpkernel
= TRUE
;
283 pt_entry_t
*DMAP1
, *DMAP2
;
288 * for legacy, returns the address of the pde entry.
289 * for 64 bit, causes the pdpt page containing the pde entry to be mapped,
290 * then returns the mapped address of the pde entry in that page
293 pmap_pde(pmap_t m
, vm_map_offset_t v
)
296 if (!cpu_64bit
|| (m
== kernel_pmap
)) {
297 pde
= (&((m
)->dirbase
[(vm_offset_t
)(v
) >> PDESHIFT
]));
300 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
301 pde
= pmap64_pde(m
, v
);
307 * the single pml4 page per pmap is allocated at pmap create time and exists
308 * for the duration of the pmap. we allocate this page in kernel vm (to save us one
309 * level of page table dynamic mapping.
310 * this returns the address of the requested pml4 entry in the top level page.
314 pmap64_pml4(pmap_t pmap
, vm_map_offset_t vaddr
)
316 return ((pml4_entry_t
*)pmap
->pm_hold
+ ((vm_offset_t
)((vaddr
>>PML4SHIFT
)&(NPML4PG
-1))));
320 * maps in the pml4 page, if any, containing the pdpt entry requested
321 * and returns the address of the pdpt entry in that mapped page
324 pmap64_pdpt(pmap_t pmap
, vm_map_offset_t vaddr
)
331 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
332 if ((vaddr
> 0x00007FFFFFFFFFFFULL
) && (vaddr
< 0xFFFF800000000000ULL
)) {
336 pml4
= pmap64_pml4(pmap
, vaddr
);
338 if (pml4
&& ((*pml4
& INTEL_PTE_VALID
))) {
340 newpf
= *pml4
& PG_FRAME
;
343 for (i
=PMAP_PDPT_FIRST_WINDOW
; i
< PMAP_PDPT_FIRST_WINDOW
+PMAP_PDPT_NWINDOWS
; i
++) {
344 if (((*(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
)) & PG_FRAME
) == newpf
) {
345 return((pdpt_entry_t
*)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
) +
346 ((vm_offset_t
)((vaddr
>>PDPTSHIFT
)&(NPDPTPG
-1))));
350 current_cpu_datap()->cpu_pmap
->pdpt_window_index
++;
351 if (current_cpu_datap()->cpu_pmap
->pdpt_window_index
> (PMAP_PDPT_FIRST_WINDOW
+PMAP_PDPT_NWINDOWS
-1))
352 current_cpu_datap()->cpu_pmap
->pdpt_window_index
= PMAP_PDPT_FIRST_WINDOW
;
354 (current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pdpt_window_index
].prv_CMAP
),
355 newpf
| INTEL_PTE_RW
| INTEL_PTE_VALID
);
356 invlpg((u_int
)(current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pdpt_window_index
].prv_CADDR
));
357 return ((pdpt_entry_t
*)(current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pdpt_window_index
].prv_CADDR
) +
358 ((vm_offset_t
)((vaddr
>>PDPTSHIFT
)&(NPDPTPG
-1))));
365 * maps in the pdpt page, if any, containing the pde entry requested
366 * and returns the address of the pde entry in that mapped page
369 pmap64_pde(pmap_t pmap
, vm_map_offset_t vaddr
)
376 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
377 if ((vaddr
> 0x00007FFFFFFFFFFFULL
) && (vaddr
< 0xFFFF800000000000ULL
)) {
381 /* if (vaddr & (1ULL << 63)) panic("neg addr");*/
382 pdpt
= pmap64_pdpt(pmap
, vaddr
);
384 if (pdpt
&& ((*pdpt
& INTEL_PTE_VALID
))) {
386 newpf
= *pdpt
& PG_FRAME
;
388 for (i
=PMAP_PDE_FIRST_WINDOW
; i
< PMAP_PDE_FIRST_WINDOW
+PMAP_PDE_NWINDOWS
; i
++) {
389 if (((*(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
)) & PG_FRAME
) == newpf
) {
390 return((pd_entry_t
*)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
) +
391 ((vm_offset_t
)((vaddr
>>PDSHIFT
)&(NPDPG
-1))));
395 current_cpu_datap()->cpu_pmap
->pde_window_index
++;
396 if (current_cpu_datap()->cpu_pmap
->pde_window_index
> (PMAP_PDE_FIRST_WINDOW
+PMAP_PDE_NWINDOWS
-1))
397 current_cpu_datap()->cpu_pmap
->pde_window_index
= PMAP_PDE_FIRST_WINDOW
;
399 (current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pde_window_index
].prv_CMAP
),
400 newpf
| INTEL_PTE_RW
| INTEL_PTE_VALID
);
401 invlpg((u_int
)(current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pde_window_index
].prv_CADDR
));
402 return ((pd_entry_t
*)(current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pde_window_index
].prv_CADDR
) +
403 ((vm_offset_t
)((vaddr
>>PDSHIFT
)&(NPDPG
-1))));
410 * Because the page tables (top 3 levels) are mapped into per cpu windows,
411 * callers must either disable interrupts or disable preemption before calling
412 * one of the pte mapping routines (e.g. pmap_pte()) as the returned vaddr
413 * is in one of those mapped windows and that cannot be allowed to change until
414 * the caller is done using the returned pte pointer. When done, the caller
415 * restores interrupts or preemption to its previous state after which point the
416 * vaddr for the returned pte can no longer be used
421 * return address of mapped pte for vaddr va in pmap pmap.
422 * must be called with pre-emption or interrupts disabled
423 * if targeted pmap is not the kernel pmap
424 * since we may be passing back a virtual address that is
425 * associated with this cpu... pre-emption or interrupts
426 * must remain disabled until the caller is done using
427 * the pointer that was passed back .
429 * maps the pde page, if any, containing the pte in and returns
430 * the address of the pte in that mapped page
433 pmap_pte(pmap_t pmap
, vm_map_offset_t vaddr
)
440 pde
= pmap_pde(pmap
,vaddr
);
442 if (pde
&& ((*pde
& INTEL_PTE_VALID
))) {
443 if (*pde
& INTEL_PTE_PS
)
445 if (pmap
== kernel_pmap
)
446 return (vtopte(vaddr
)); /* compat kernel still has pte's mapped */
448 if (ml_get_interrupts_enabled() && get_preemption_level() == 0)
449 panic("pmap_pte: unsafe call");
451 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
453 newpf
= *pde
& PG_FRAME
;
455 for (i
=PMAP_PTE_FIRST_WINDOW
; i
< PMAP_PTE_FIRST_WINDOW
+PMAP_PTE_NWINDOWS
; i
++) {
456 if (((*(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
)) & PG_FRAME
) == newpf
) {
457 return((pt_entry_t
*)(current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
) +
458 ((vm_offset_t
)i386_btop(vaddr
) & (NPTEPG
-1)));
462 current_cpu_datap()->cpu_pmap
->pte_window_index
++;
463 if (current_cpu_datap()->cpu_pmap
->pte_window_index
> (PMAP_PTE_FIRST_WINDOW
+PMAP_PTE_NWINDOWS
-1))
464 current_cpu_datap()->cpu_pmap
->pte_window_index
= PMAP_PTE_FIRST_WINDOW
;
466 (current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pte_window_index
].prv_CMAP
),
467 newpf
| INTEL_PTE_RW
| INTEL_PTE_VALID
);
468 invlpg((u_int
)(current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pte_window_index
].prv_CADDR
));
469 return ((pt_entry_t
*)(current_cpu_datap()->cpu_pmap
->mapwindow
[current_cpu_datap()->cpu_pmap
->pte_window_index
].prv_CADDR
) +
470 ((vm_offset_t
)i386_btop(vaddr
) & (NPTEPG
-1)));
478 * Map memory at initialization. The physical addresses being
479 * mapped are not managed and are never unmapped.
481 * For now, VM is already on, we only need to map the
487 vm_map_offset_t start_addr
,
488 vm_map_offset_t end_addr
,
495 while (start_addr
< end_addr
) {
496 pmap_enter(kernel_pmap
, (vm_map_offset_t
)virt
,
497 (ppnum_t
) i386_btop(start_addr
), prot
, VM_PROT_NONE
, flags
, FALSE
);
504 extern pmap_paddr_t first_avail
;
505 extern vm_offset_t virtual_avail
, virtual_end
;
506 extern pmap_paddr_t avail_start
, avail_end
;
507 extern vm_offset_t sHIB
;
508 extern vm_offset_t eHIB
;
509 extern vm_offset_t stext
;
510 extern vm_offset_t etext
;
511 extern vm_offset_t sdata
;
513 extern void *KPTphys
;
519 * Here early in the life of a processor (from cpu_mode_init()).
523 * Initialize the per-cpu, TLB-related fields.
525 current_cpu_datap()->cpu_active_cr3
= kernel_pmap
->pm_cr3
;
526 current_cpu_datap()->cpu_tlb_invalid
= FALSE
;
530 pmap_high_shared_remap(enum high_fixed_addresses e
, vm_offset_t va
, int sz
)
532 vm_offset_t ve
= pmap_index_to_virt(e
);
538 assert(0 == (va
& PAGE_MASK
)); /* expecting page aligned */
540 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)ve
);
542 for (i
=0; i
< sz
; i
++) {
543 pa
= (pmap_paddr_t
) kvtophys(va
);
544 pmap_store_pte(ptep
, (pa
& PG_FRAME
)
558 pmap_cpu_high_shared_remap(int cpu
, enum high_cpu_types e
, vm_offset_t va
, int sz
)
560 enum high_fixed_addresses a
= e
+ HIGH_CPU_END
* cpu
;
561 return pmap_high_shared_remap(HIGH_FIXED_CPUS_BEGIN
+ a
, va
, sz
);
564 void pmap_init_high_shared(void);
566 extern vm_offset_t gdtptr
, idtptr
;
568 extern uint32_t low_intstack
;
570 extern struct fake_descriptor ldt_desc_pattern
;
571 extern struct fake_descriptor tss_desc_pattern
;
573 extern char hi_remap_text
, hi_remap_etext
;
574 extern char t_zero_div
;
576 pt_entry_t
*pte_unique_base
;
579 pmap_init_high_shared(void)
585 cpu_desc_index_t
* cdi
= &cpu_data_master
.cpu_desc_index
;
587 kprintf("HIGH_MEM_BASE 0x%x fixed per-cpu begin 0x%x\n",
588 HIGH_MEM_BASE
,pmap_index_to_virt(HIGH_FIXED_CPUS_BEGIN
));
590 pte_unique_base
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)pmap_index_to_virt(HIGH_FIXED_CPUS_BEGIN
));
593 if (i386_btop(&hi_remap_etext
- &hi_remap_text
+ 1) >
594 HIGH_FIXED_TRAMPS_END
- HIGH_FIXED_TRAMPS
+ 1)
595 panic("tramps too large");
596 haddr
= pmap_high_shared_remap(HIGH_FIXED_TRAMPS
,
597 (vm_offset_t
) &hi_remap_text
, 3);
598 kprintf("tramp: 0x%x, ",haddr
);
599 /* map gdt up high and update ptr for reload */
600 haddr
= pmap_high_shared_remap(HIGH_FIXED_GDT
,
601 (vm_offset_t
) master_gdt
, 1);
602 cdi
->cdi_gdt
.ptr
= (void *)haddr
;
603 kprintf("GDT: 0x%x, ",haddr
);
604 /* map ldt up high */
605 haddr
= pmap_high_shared_remap(HIGH_FIXED_LDT_BEGIN
,
606 (vm_offset_t
) master_ldt
,
607 HIGH_FIXED_LDT_END
- HIGH_FIXED_LDT_BEGIN
+ 1);
608 cdi
->cdi_ldt
= (struct fake_descriptor
*)haddr
;
609 kprintf("LDT: 0x%x, ",haddr
);
610 /* put new ldt addr into gdt */
611 struct fake_descriptor temp_fake_desc
;
612 temp_fake_desc
= ldt_desc_pattern
;
613 temp_fake_desc
.offset
= (vm_offset_t
) haddr
;
614 fix_desc(&temp_fake_desc
, 1);
616 *(struct fake_descriptor
*) &master_gdt
[sel_idx(KERNEL_LDT
)] = temp_fake_desc
;
617 *(struct fake_descriptor
*) &master_gdt
[sel_idx(USER_LDT
)] = temp_fake_desc
;
619 /* map idt up high */
620 haddr
= pmap_high_shared_remap(HIGH_FIXED_IDT
,
621 (vm_offset_t
) master_idt
, 1);
622 cdi
->cdi_idt
.ptr
= (void *)haddr
;
623 kprintf("IDT: 0x%x, ", haddr
);
624 /* remap ktss up high and put new high addr into gdt */
625 haddr
= pmap_high_shared_remap(HIGH_FIXED_KTSS
,
626 (vm_offset_t
) &master_ktss
, 1);
628 temp_fake_desc
= tss_desc_pattern
;
629 temp_fake_desc
.offset
= (vm_offset_t
) haddr
;
630 fix_desc(&temp_fake_desc
, 1);
631 *(struct fake_descriptor
*) &master_gdt
[sel_idx(KERNEL_TSS
)] = temp_fake_desc
;
632 kprintf("KTSS: 0x%x, ",haddr
);
634 /* remap dftss up high and put new high addr into gdt */
635 haddr
= pmap_high_shared_remap(HIGH_FIXED_DFTSS
,
636 (vm_offset_t
) &master_dftss
, 1);
637 temp_fake_desc
= tss_desc_pattern
;
638 temp_fake_desc
.offset
= (vm_offset_t
) haddr
;
639 fix_desc(&temp_fake_desc
, 1);
640 *(struct fake_descriptor
*) &master_gdt
[sel_idx(DF_TSS
)] = temp_fake_desc
;
641 kprintf("DFTSS: 0x%x\n",haddr
);
643 /* remap mctss up high and put new high addr into gdt */
644 haddr
= pmap_high_shared_remap(HIGH_FIXED_DFTSS
,
645 (vm_offset_t
) &master_mctss
, 1);
646 temp_fake_desc
= tss_desc_pattern
;
647 temp_fake_desc
.offset
= (vm_offset_t
) haddr
;
648 fix_desc(&temp_fake_desc
, 1);
649 *(struct fake_descriptor
*) &master_gdt
[sel_idx(MC_TSS
)] = temp_fake_desc
;
650 kprintf("MCTSS: 0x%x\n",haddr
);
652 cpu_desc_load(&cpu_data_master
);
657 * Bootstrap the system enough to run with virtual memory.
658 * Map the kernel's code and data, and allocate the system page table.
659 * Called with mapping OFF. Page_size must already be set.
664 __unused vm_offset_t load_start
,
672 vm_last_addr
= VM_MAX_KERNEL_ADDRESS
; /* Set the highest address
675 * The kernel's pmap is statically allocated so we don't
676 * have to use pmap_create, which is unlikely to work
677 * correctly at this part of the boot sequence.
681 kernel_pmap
= &kernel_pmap_store
;
682 kernel_pmap
->ref_count
= 1;
683 kernel_pmap
->nx_enabled
= FALSE
;
684 kernel_pmap
->pm_task_map
= TASK_MAP_32BIT
;
685 kernel_pmap
->pm_obj
= (vm_object_t
) NULL
;
686 kernel_pmap
->dirbase
= (pd_entry_t
*)((unsigned int)IdlePTD
| KERNBASE
);
687 kernel_pmap
->pdirbase
= (pmap_paddr_t
)((int)IdlePTD
);
688 pdpt
= (pd_entry_t
*)((unsigned int)IdlePDPT
| KERNBASE
);
689 kernel_pmap
->pm_pdpt
= pdpt
;
690 kernel_pmap
->pm_cr3
= (pmap_paddr_t
)((int)IdlePDPT
);
693 va
= (vm_offset_t
)kernel_pmap
->dirbase
;
694 /* setup self referential mapping(s) */
695 for (i
= 0; i
< NPGPTD
; i
++, pdpt
++) {
697 pa
= (pmap_paddr_t
) kvtophys((vm_offset_t
)(va
+ i386_ptob(i
)));
699 (pd_entry_t
*) (kernel_pmap
->dirbase
+ PTDPTDI
+ i
),
700 (pa
& PG_FRAME
) | INTEL_PTE_VALID
| INTEL_PTE_RW
| INTEL_PTE_REF
|
701 INTEL_PTE_MOD
| INTEL_PTE_WIRED
) ;
702 pmap_store_pte(pdpt
, pa
| INTEL_PTE_VALID
);
706 /* 32-bit and legacy support depends on IA32e mode being disabled */
710 lo_kernel_cr3
= kernel_pmap
->pm_cr3
;
711 current_cpu_datap()->cpu_kernel_cr3
= (addr64_t
) kernel_pmap
->pm_cr3
;
713 /* save the value we stuff into created pmaps to share the gdts etc */
714 high_shared_pde
= *pmap_pde(kernel_pmap
, HIGH_MEM_BASE
);
715 /* make sure G bit is on for high shared pde entry */
716 high_shared_pde
|= INTEL_PTE_GLOBAL
;
718 pmap_store_pte(pmap_pde(kernel_pmap
, HIGH_MEM_BASE
), high_shared_pde
);
722 OSAddAtomic(NKPT
, &inuse_ptepages_count
);
723 OSAddAtomic64(NKPT
, &alloc_ptepages_count
);
724 bootstrap_wired_pages
= NKPT
;
726 virtual_avail
= (vm_offset_t
)VADDR(KPTDI
,0) + (vm_offset_t
)first_avail
;
727 virtual_end
= (vm_offset_t
)(VM_MAX_KERNEL_ADDRESS
);
730 * Reserve some special page table entries/VA space for temporary
736 #define SYSMAP(c, p, v, n) \
737 v = (c)va; va += ((n)*INTEL_PGBYTES); p = pte; pte += (n)
739 for (i
=0; i
<PMAP_NWINDOWS
; i
++) {
741 (current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
),
742 (current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CADDR
),
744 *current_cpu_datap()->cpu_pmap
->mapwindow
[i
].prv_CMAP
= 0;
747 /* DMAP user for debugger */
748 SYSMAP(caddr_t
, DMAP1
, DADDR1
, 1);
749 SYSMAP(caddr_t
, DMAP2
, DADDR2
, 1); /* XXX temporary - can remove */
753 if (PE_parse_boot_argn("npvhash", &npvhash
, sizeof (npvhash
))) {
754 if (0 != ((npvhash
+1) & npvhash
)) {
755 kprintf("invalid hash %d, must be ((2^N)-1), using default %d\n",npvhash
,NPVHASH
);
761 printf("npvhash=%d\n",npvhash
);
763 simple_lock_init(&kernel_pmap
->lock
, 0);
764 simple_lock_init(&pv_hashed_free_list_lock
, 0);
765 simple_lock_init(&pv_hashed_kern_free_list_lock
, 0);
766 simple_lock_init(&pv_hash_table_lock
,0);
768 pmap_init_high_shared();
770 pde_mapped_size
= PDE_MAPPED_SIZE
;
773 pdpt_entry_t
*ppdpt
= IdlePDPT
;
774 pdpt_entry_t
*ppdpt64
= (pdpt_entry_t
*)IdlePDPT64
;
775 pdpt_entry_t
*ppml4
= (pdpt_entry_t
*)IdlePML4
;
776 int istate
= ml_set_interrupts_enabled(FALSE
);
779 * Clone a new 64-bit 3rd-level page table directory, IdlePML4,
780 * with page bits set for the correct IA-32e operation and so that
781 * the legacy-mode IdlePDPT is retained for slave processor start-up.
782 * This is necessary due to the incompatible use of page bits between
783 * 64-bit and legacy modes.
785 kernel_pmap
->pm_cr3
= (pmap_paddr_t
)((int)IdlePML4
); /* setup in start.s for us */
786 kernel_pmap
->pm_pml4
= IdlePML4
;
787 kernel_pmap
->pm_pdpt
= (pd_entry_t
*)
788 ((unsigned int)IdlePDPT64
| KERNBASE
);
789 #define PAGE_BITS INTEL_PTE_VALID|INTEL_PTE_RW|INTEL_PTE_USER|INTEL_PTE_REF
790 pmap_store_pte(kernel_pmap
->pm_pml4
,
791 (uint32_t)IdlePDPT64
| PAGE_BITS
);
792 pmap_store_pte((ppdpt64
+0), *(ppdpt
+0) | PAGE_BITS
);
793 pmap_store_pte((ppdpt64
+1), *(ppdpt
+1) | PAGE_BITS
);
794 pmap_store_pte((ppdpt64
+2), *(ppdpt
+2) | PAGE_BITS
);
795 pmap_store_pte((ppdpt64
+3), *(ppdpt
+3) | PAGE_BITS
);
798 * The kernel is also mapped in the uber-sapce at the 4GB starting
799 * 0xFFFFFF80:00000000. This is the highest entry in the 4th-level.
801 pmap_store_pte((ppml4
+KERNEL_UBER_PML4_INDEX
), *(ppml4
+0));
803 kernel64_cr3
= (addr64_t
) kernel_pmap
->pm_cr3
;
805 /* Re-initialize descriptors and prepare to switch modes */
806 cpu_desc_init64(&cpu_data_master
);
807 current_cpu_datap()->cpu_is64bit
= TRUE
;
808 current_cpu_datap()->cpu_active_cr3
= kernel64_cr3
;
810 pde_mapped_size
= 512*4096 ;
812 ml_set_interrupts_enabled(istate
);
815 /* Sets 64-bit mode if required. */
816 cpu_mode_init(&cpu_data_master
);
817 /* Update in-kernel CPUID information if we're now in 64-bit mode */
821 kernel_pmap
->pm_hold
= (vm_offset_t
)kernel_pmap
->pm_pml4
;
823 kprintf("Kernel virtual space from 0x%x to 0x%x.\n",
824 VADDR(KPTDI
,0), virtual_end
);
825 printf("PAE enabled\n");
827 printf("64 bit mode enabled\n");kprintf("64 bit mode enabled\n"); }
829 kprintf("Available physical space from 0x%llx to 0x%llx\n",
830 avail_start
, avail_end
);
833 * By default for 64-bit users loaded at 4GB, share kernel mapping.
834 * But this may be overridden by the -no_shared_cr3 boot-arg.
836 if (PE_parse_boot_argn("-no_shared_cr3", &no_shared_cr3
, sizeof (no_shared_cr3
))) {
837 kprintf("Shared kernel address space disabled\n");
841 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace
, sizeof (pmap_trace
))) {
842 kprintf("Kernel traces for pmap operations enabled\n");
844 #endif /* PMAP_TRACES */
852 *startp
= virtual_avail
;
857 * Initialize the pmap module.
858 * Called by vm_init, to initialize any structures that the pmap
859 * system needs to map virtual memory.
865 vm_map_offset_t vaddr
;
871 * Allocate memory for the pv_head_table and its lock bits,
872 * the modify bit array, and the pte_page table.
876 * zero bias all these arrays now instead of off avail_start
877 * so we cover all memory
880 npages
= (long)i386_btop(avail_end
);
881 s
= (vm_size_t
) (sizeof(struct pv_rooted_entry
) * npages
882 + (sizeof (struct pv_hashed_entry_t
*) * (npvhash
+1))
883 + pv_lock_table_size(npages
)
884 + pv_hash_lock_table_size((npvhash
+1))
888 if (kernel_memory_allocate(kernel_map
, &addr
, s
, 0,
889 KMA_KOBJECT
| KMA_PERMANENT
)
893 memset((char *)addr
, 0, s
);
899 if (0 == npvhash
) panic("npvhash not initialized");
903 * Allocate the structures first to preserve word-alignment.
905 pv_head_table
= (pv_rooted_entry_t
) addr
;
906 addr
= (vm_offset_t
) (pv_head_table
+ npages
);
908 pv_hash_table
= (pv_hashed_entry_t
*)addr
;
909 addr
= (vm_offset_t
) (pv_hash_table
+ (npvhash
+ 1));
911 pv_lock_table
= (char *) addr
;
912 addr
= (vm_offset_t
) (pv_lock_table
+ pv_lock_table_size(npages
));
914 pv_hash_lock_table
= (char *) addr
;
915 addr
= (vm_offset_t
) (pv_hash_lock_table
+ pv_hash_lock_table_size((npvhash
+1)));
917 pmap_phys_attributes
= (char *) addr
;
922 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
924 last_pn
= (ppnum_t
)i386_btop(avail_end
);
926 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
927 if (pmptr
->type
== kEfiConventionalMemory
) {
929 for (pn
= pmptr
->base
; pn
<= pmptr
->end
; pn
++) {
931 pmap_phys_attributes
[pn
] |= PHYS_MANAGED
;
933 if (pn
> last_managed_page
)
934 last_managed_page
= pn
;
936 if (pn
>= lowest_hi
&& pn
<= highest_hi
)
937 pmap_phys_attributes
[pn
] |= PHYS_NOENCRYPT
;
944 ppn
= pmap_find_phys(kernel_pmap
, vaddr
);
946 pmap_phys_attributes
[ppn
] |= PHYS_NOENCRYPT
;
952 * Create the zone of physical maps,
953 * and of the physical-to-virtual entries.
955 s
= (vm_size_t
) sizeof(struct pmap
);
956 pmap_zone
= zinit(s
, 400*s
, 4096, "pmap"); /* XXX */
957 zone_change(pmap_zone
, Z_NOENCRYPT
, TRUE
);
959 s
= (vm_size_t
) sizeof(struct pv_hashed_entry
);
960 pv_hashed_list_zone
= zinit(s
, 10000*s
/* Expandable zone */,
961 4096 * 4 /* LCM i386 */, "pv_list");
962 zone_change(pv_hashed_list_zone
, Z_NOENCRYPT
, TRUE
);
965 pdpt_zone
= zinit(s
, 400*s
, 4096, "pdpt"); /* XXX */
966 zone_change(pdpt_zone
, Z_NOENCRYPT
, TRUE
);
968 kptobj
= &kptobj_object_store
;
969 _vm_object_allocate((vm_object_size_t
)(NPGPTD
*NPTDPG
), kptobj
);
970 kernel_pmap
->pm_obj
= kptobj
;
972 /* create pv entries for kernel pages mapped by low level
973 startup code. these have to exist so we can pmap_remove()
974 e.g. kext pages from the middle of our addr space */
976 vaddr
= (vm_map_offset_t
)0;
977 for (ppn
= 0; ppn
< i386_btop(avail_start
) ; ppn
++ ) {
978 pv_rooted_entry_t pv_e
;
980 pv_e
= pai_to_pvh(ppn
);
983 pv_e
->pmap
= kernel_pmap
;
984 queue_init(&pv_e
->qlink
);
987 pmap_initialized
= TRUE
;
989 max_preemption_latency_tsc
= tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS
, tscFCvtn2t
);
994 #define DBG(x...) kprintf("DBG: " x)
1000 * Called once VM is fully initialized so that we can release unused
1001 * sections of low memory to the general pool.
1002 * Also complete the set-up of identity-mapped sections of the kernel:
1003 * 1) write-protect kernel text
1004 * 2) map kernel text using large pages if possible
1005 * 3) read and write-protect page zero (for K32)
1006 * 4) map the global page at the appropriate virtual address.
1008 * Use of large pages
1009 * ------------------
1010 * To effectively map and write-protect all kernel text pages, the text
1011 * must be 2M-aligned at the base, and the data section above must also be
1012 * 2M-aligned. That is, there's padding below and above. This is achieved
1013 * through linker directives. Large pages are used only if this alignment
1014 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
1019 * sdata: ================== 2Meg
1023 * etext: ------------------
1031 * stext: ================== 2Meg
1035 * eHIB: ------------------
1039 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
1040 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
1041 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
1042 * The now unused level-1 PTE pages are also freed.
1044 extern uint32_t pmap_reserved_ranges
;
1046 pmap_lowmem_finalize(void)
1051 /* Check the kernel is linked at the expected base address */
1052 if (i386_btop(kvtophys((vm_offset_t
) &IdlePML4
)) !=
1053 I386_KERNEL_IMAGE_BASE_PAGE
)
1054 panic("pmap_lowmem_finalize() unexpected kernel base address");
1057 * Update wired memory statistics for early boot pages
1059 PMAP_ZINFO_PALLOC(kernel_pmap
, bootstrap_wired_pages
* PAGE_SIZE
);
1062 * Free all pages in pmap regions below the base:
1064 * We can't free all the pages to VM that EFI reports available.
1065 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
1066 * There's also a size miscalculation here: pend is one page less
1067 * than it should be but this is not fixed to be backwards
1069 * Due to this current EFI limitation, we take only the first
1070 * entry in the memory region table. However, the loop is retained
1071 * (with the intended termination criteria commented out) in the
1072 * hope that some day we can free all low-memory ranges.
1075 // pmap_memory_regions[i].end <= I386_KERNEL_IMAGE_BASE_PAGE;
1076 i
< 1 && (pmap_reserved_ranges
== 0);
1078 vm_offset_t pbase
= (vm_offset_t
)i386_ptob(pmap_memory_regions
[i
].base
);
1079 vm_offset_t pend
= (vm_offset_t
)i386_ptob(pmap_memory_regions
[i
].end
);
1080 // vm_offset_t pend = i386_ptob(pmap_memory_regions[i].end+1);
1082 DBG("ml_static_mfree(%p,%p) for pmap region %d\n",
1083 (void *) ml_static_ptovirt(pbase
),
1084 (void *) (pend
- pbase
), i
);
1085 ml_static_mfree(ml_static_ptovirt(pbase
), pend
- pbase
);
1089 * If text and data are both 2MB-aligned,
1090 * we can map text with large-pages,
1091 * unless the -kernel_text_ps_4K boot-arg overrides.
1093 if ((stext
& I386_LPGMASK
) == 0 && (sdata
& I386_LPGMASK
) == 0) {
1094 kprintf("Kernel text is 2MB aligned");
1095 kernel_text_ps_4K
= FALSE
;
1096 if (PE_parse_boot_argn("-kernel_text_ps_4K",
1098 sizeof (kernel_text_ps_4K
)))
1099 kprintf(" but will be mapped with 4K pages\n");
1101 kprintf(" and will be mapped with 2M pages\n");
1104 (void) PE_parse_boot_argn("wpkernel", &wpkernel
, sizeof (wpkernel
));
1106 kprintf("Kernel text %p-%p to be write-protected\n",
1107 (void *) stext
, (void *) etext
);
1112 * Scan over text if mappings are to be changed:
1113 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
1114 * - Change to large-pages if possible and not overriden.
1116 if (kernel_text_ps_4K
&& wpkernel
) {
1118 for (myva
= stext
; myva
< etext
; myva
+= PAGE_SIZE
) {
1121 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
1123 pmap_store_pte(ptep
, *ptep
& ~INTEL_PTE_RW
);
1127 if (!kernel_text_ps_4K
) {
1131 * Release zero-filled page padding used for 2M-alignment.
1133 DBG("ml_static_mfree(%p,%p) for padding below text\n",
1134 (void *) eHIB
, (void *) (stext
- eHIB
));
1135 ml_static_mfree(eHIB
, stext
- eHIB
);
1136 DBG("ml_static_mfree(%p,%p) for padding above text\n",
1137 (void *) etext
, (void *) (sdata
- etext
));
1138 ml_static_mfree(etext
, sdata
- etext
);
1141 * Coalesce text pages into large pages.
1143 for (myva
= stext
; myva
< sdata
; myva
+= I386_LPGBYTES
) {
1145 vm_offset_t pte_phys
;
1149 pdep
= pmap_pde(kernel_pmap
, (vm_map_offset_t
)myva
);
1150 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
1151 DBG("myva: %p pdep: %p ptep: %p\n",
1152 (void *) myva
, (void *) pdep
, (void *) ptep
);
1153 if ((*ptep
& INTEL_PTE_VALID
) == 0)
1155 pte_phys
= (vm_offset_t
)(*ptep
& PG_FRAME
);
1156 pde
= *pdep
& PTMASK
; /* page attributes from pde */
1157 pde
|= INTEL_PTE_PS
; /* make it a 2M entry */
1158 pde
|= pte_phys
; /* take page frame from pte */
1161 pde
&= ~INTEL_PTE_RW
;
1162 DBG("pmap_store_pte(%p,0x%llx)\n",
1164 pmap_store_pte(pdep
, pde
);
1167 * Free the now-unused level-1 pte.
1168 * Note: ptep is a virtual address to the pte in the
1169 * recursive map. We can't use this address to free
1170 * the page. Instead we need to compute its address
1171 * in the Idle PTEs in "low memory".
1173 vm_offset_t vm_ptep
= (vm_offset_t
) KPTphys
1174 + (pte_phys
>> PTPGSHIFT
);
1175 DBG("ml_static_mfree(%p,0x%x) for pte\n",
1176 (void *) vm_ptep
, PAGE_SIZE
);
1177 ml_static_mfree(vm_ptep
, PAGE_SIZE
);
1180 /* Change variable read by sysctl machdep.pmap */
1181 pmap_kernel_text_ps
= I386_LPGBYTES
;
1184 /* no matter what, kernel page zero is not accessible */
1185 pmap_store_pte(pmap_pte(kernel_pmap
, 0), INTEL_PTE_INVALID
);
1187 /* map lowmem global page into fixed addr */
1188 pt_entry_t
*pte
= NULL
;
1189 if (0 == (pte
= pmap_pte(kernel_pmap
,
1190 VM_MIN_KERNEL_LOADED_ADDRESS
+ 0x2000)))
1191 panic("lowmem pte");
1192 /* make sure it is defined on page boundary */
1193 assert(0 == ((vm_offset_t
) &lowGlo
& PAGE_MASK
));
1194 pmap_store_pte(pte
, kvtophys((vm_offset_t
)&lowGlo
)
1204 #define managed_page(x) ( (unsigned int)x <= last_managed_page && (pmap_phys_attributes[x] & PHYS_MANAGED) )
1207 * this function is only used for debugging fron the vm layer
1213 pv_rooted_entry_t pv_h
;
1217 assert(pn
!= vm_page_fictitious_addr
);
1219 if (!pmap_initialized
)
1222 if (pn
== vm_page_guard_addr
)
1225 pai
= ppn_to_pai(pn
);
1226 if (!managed_page(pai
))
1228 pv_h
= pai_to_pvh(pn
);
1229 result
= (pv_h
->pmap
== PMAP_NULL
);
1236 vm_map_offset_t va_start
,
1237 vm_map_offset_t va_end
)
1239 vm_map_offset_t offset
;
1242 if (pmap
== PMAP_NULL
) {
1247 * Check the resident page count
1248 * - if it's zero, the pmap is completely empty.
1249 * This short-circuit test prevents a virtual address scan which is
1250 * painfully slow for 64-bit spaces.
1251 * This assumes the count is correct
1252 * .. the debug kernel ought to be checking perhaps by page table walk.
1254 if (pmap
->stats
.resident_count
== 0)
1257 for (offset
= va_start
;
1259 offset
+= PAGE_SIZE_64
) {
1260 phys_page
= pmap_find_phys(pmap
, offset
);
1262 if (pmap
!= kernel_pmap
&&
1263 pmap
->pm_task_map
== TASK_MAP_32BIT
&&
1264 offset
>= HIGH_MEM_BASE
) {
1266 * The "high_shared_pde" is used to share
1267 * the entire top-most 2MB of address space
1268 * between the kernel and all 32-bit tasks.
1269 * So none of this can be removed from 32-bit
1271 * Let's pretend there's nothing up
1276 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
1277 "page %d at 0x%llx\n",
1278 pmap
, va_start
, va_end
, phys_page
, offset
);
1288 * Create and return a physical map.
1290 * If the size specified for the map
1291 * is zero, the map is an actual physical
1292 * map, and may be referenced by the
1295 * If the size specified is non-zero,
1296 * the map will be used in software only, and
1297 * is bounded by that size.
1310 pml4_entry_t
*pml4p
;
1315 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_START
,
1316 (int) (sz
>>32), (int) sz
, (int) is_64bit
, 0, 0);
1318 size
= (vm_size_t
) sz
;
1321 * A software use-only map doesn't even need a map.
1328 p
= (pmap_t
) zalloc(pmap_zone
);
1330 panic("pmap_create zalloc");
1332 /* init counts now since we'll be bumping some */
1333 simple_lock_init(&p
->lock
, 0);
1334 p
->stats
.resident_count
= 0;
1335 p
->stats
.resident_max
= 0;
1336 p
->stats
.wired_count
= 0;
1337 ledger_reference(ledger
);
1341 p
->pm_shared
= FALSE
;
1343 assert(!is_64bit
|| cpu_64bit
);
1344 p
->pm_task_map
= is_64bit
? TASK_MAP_64BIT
: TASK_MAP_32BIT
;;
1347 /* legacy 32 bit setup */
1348 /* in the legacy case the pdpt layer is hardwired to 4 entries and each
1349 * entry covers 1GB of addr space */
1350 if (KERN_SUCCESS
!= kmem_alloc_kobject(kernel_map
, (vm_offset_t
*)(&p
->dirbase
), NBPTD
))
1351 panic("pmap_create kmem_alloc_kobject");
1352 p
->pm_hold
= (vm_offset_t
)zalloc(pdpt_zone
);
1353 if ((vm_offset_t
)NULL
== p
->pm_hold
) {
1354 panic("pdpt zalloc");
1356 pdpt
= (pdpt_entry_t
*) (( p
->pm_hold
+ 31) & ~31);
1357 p
->pm_cr3
= (pmap_paddr_t
)kvtophys((vm_offset_t
)pdpt
);
1358 if (NULL
== (p
->pm_obj
= vm_object_allocate((vm_object_size_t
)(NPGPTD
*NPTDPG
))))
1359 panic("pmap_create vm_object_allocate");
1361 memset((char *)p
->dirbase
, 0, NBPTD
);
1363 va
= (vm_offset_t
)p
->dirbase
;
1364 p
->pdirbase
= kvtophys(va
);
1366 PMAP_ZINFO_SALLOC(p
,NBPTD
);
1368 template = INTEL_PTE_VALID
;
1369 for (i
= 0; i
< NPGPTD
; i
++, pdpt
++ ) {
1371 pa
= (pmap_paddr_t
) kvtophys((vm_offset_t
)(va
+ i386_ptob(i
)));
1372 pmap_store_pte(pdpt
, pa
| template);
1375 /* map the high shared pde */
1377 pmap_store_pte(pmap_pde(p
, HIGH_MEM_BASE
), high_shared_pde
);
1383 /* alloc the pml4 page in kernel vm */
1384 if (KERN_SUCCESS
!= kmem_alloc_kobject(kernel_map
, (vm_offset_t
*)(&p
->pm_hold
), PAGE_SIZE
))
1385 panic("pmap_create kmem_alloc_kobject pml4");
1387 memset((char *)p
->pm_hold
, 0, PAGE_SIZE
);
1388 p
->pm_cr3
= (pmap_paddr_t
)kvtophys((vm_offset_t
)p
->pm_hold
);
1390 OSAddAtomic(1, &inuse_ptepages_count
);
1391 OSAddAtomic64(1, &alloc_ptepages_count
);
1392 PMAP_ZINFO_SALLOC(p
, PAGE_SIZE
);
1394 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
1396 if (NULL
== (p
->pm_obj_pml4
= vm_object_allocate((vm_object_size_t
)(NPML4PGS
))))
1397 panic("pmap_create pdpt obj");
1399 if (NULL
== (p
->pm_obj_pdpt
= vm_object_allocate((vm_object_size_t
)(NPDPTPGS
))))
1400 panic("pmap_create pdpt obj");
1402 if (NULL
== (p
->pm_obj
= vm_object_allocate((vm_object_size_t
)(NPDEPGS
))))
1403 panic("pmap_create pte obj");
1405 /* uber space points to uber mapped kernel */
1407 pml4p
= pmap64_pml4(p
, 0ULL);
1408 pmap_store_pte((pml4p
+KERNEL_UBER_PML4_INDEX
),*kernel_pmap
->pm_pml4
);
1412 while ((pdp
= pmap64_pde(p
, (uint64_t)HIGH_MEM_BASE
)) == PD_ENTRY_NULL
) {
1414 pmap_expand_pdpt(p
, (uint64_t)HIGH_MEM_BASE
, PMAP_EXPAND_OPTIONS_NONE
); /* need room for another pde entry */
1417 pmap_store_pte(pdp
, high_shared_pde
);
1422 PMAP_TRACE(PMAP_CODE(PMAP__CREATE
) | DBG_FUNC_START
,
1423 (int) p
, is_64bit
, 0, 0, 0);
1429 * The following routines implement the shared address optmization for 64-bit
1430 * users with a 4GB page zero.
1432 * pmap_set_4GB_pagezero()
1433 * is called in the exec and fork paths to mirror the kernel's
1434 * mapping in the bottom 4G of the user's pmap. The task mapping changes
1435 * from TASK_MAP_64BIT to TASK_MAP_64BIT_SHARED. This routine returns
1436 * without doing anything if the -no_shared_cr3 boot-arg is set.
1438 * pmap_clear_4GB_pagezero()
1439 * is called in the exec/exit paths to undo this mirror. The task mapping
1440 * reverts to TASK_MAP_64BIT. In addition, we switch to the kernel's
1441 * CR3 by calling pmap_load_kernel_cr3().
1443 * pmap_load_kernel_cr3()
1444 * loads cr3 with the kernel's page table. In addition to being called
1445 * by pmap_clear_4GB_pagezero(), it is used both prior to teardown and
1446 * when we go idle in the context of a shared map.
1448 * Further notes on per-cpu data used:
1450 * cpu_kernel_cr3 is the cr3 for the kernel's pmap.
1451 * This is loaded in a trampoline on entering the kernel
1452 * from a 32-bit user (or non-shared-cr3 64-bit user).
1453 * cpu_task_cr3 is the cr3 for the current thread.
1454 * This is loaded in a trampoline as we exit the kernel.
1455 * cpu_active_cr3 reflects the cr3 currently loaded.
1456 * However, the low order bit is set when the
1457 * processor is idle or interrupts are disabled
1458 * while the system pmap lock is held. It is used by
1460 * cpu_task_map indicates whether the task cr3 belongs to
1461 * a 32-bit, a 64-bit or a 64-bit shared map.
1462 * The latter allows the avoidance of the cr3 load
1463 * on kernel entry and exit.
1464 * cpu_tlb_invalid set TRUE when a tlb flush is requested.
1465 * If the cr3 is "inactive" (the cpu is idle or the
1466 * system-wide pmap lock is held) this not serviced by
1467 * an IPI but at time when the cr3 becomes "active".
1471 pmap_set_4GB_pagezero(pmap_t p
)
1473 pdpt_entry_t
*user_pdptp
;
1474 pdpt_entry_t
*kern_pdptp
;
1476 assert(p
->pm_task_map
!= TASK_MAP_32BIT
);
1478 /* Kernel-shared cr3 may be disabled by boot arg. */
1483 * Set the bottom 4 3rd-level pte's to be the kernel's.
1486 while ((user_pdptp
= pmap64_pdpt(p
, 0x0)) == PDPT_ENTRY_NULL
) {
1488 pmap_expand_pml4(p
, 0x0, PMAP_EXPAND_OPTIONS_NONE
);
1491 kern_pdptp
= kernel_pmap
->pm_pdpt
;
1492 pmap_store_pte(user_pdptp
+0, *(kern_pdptp
+0));
1493 pmap_store_pte(user_pdptp
+1, *(kern_pdptp
+1));
1494 pmap_store_pte(user_pdptp
+2, *(kern_pdptp
+2));
1495 pmap_store_pte(user_pdptp
+3, *(kern_pdptp
+3));
1496 p
->pm_task_map
= TASK_MAP_64BIT_SHARED
;
1501 pmap_clear_4GB_pagezero(pmap_t p
)
1503 pdpt_entry_t
*user_pdptp
;
1506 if (p
->pm_task_map
!= TASK_MAP_64BIT_SHARED
)
1511 p
->pm_task_map
= TASK_MAP_64BIT
;
1513 istate
= ml_set_interrupts_enabled(FALSE
);
1515 if (current_cpu_datap()->cpu_task_map
== TASK_MAP_64BIT_SHARED
)
1516 current_cpu_datap()->cpu_task_map
= TASK_MAP_64BIT
;
1518 pmap_load_kernel_cr3();
1520 user_pdptp
= pmap64_pdpt(p
, 0x0);
1521 pmap_store_pte(user_pdptp
+0, 0);
1522 pmap_store_pte(user_pdptp
+1, 0);
1523 pmap_store_pte(user_pdptp
+2, 0);
1524 pmap_store_pte(user_pdptp
+3, 0);
1526 ml_set_interrupts_enabled(istate
);
1532 pmap_load_kernel_cr3(void)
1534 uint64_t kernel_cr3
;
1536 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
1539 * Reload cr3 with the true kernel cr3.
1541 kernel_cr3
= current_cpu_datap()->cpu_kernel_cr3
;
1542 set64_cr3(kernel_cr3
);
1543 current_cpu_datap()->cpu_active_cr3
= kernel_cr3
;
1544 current_cpu_datap()->cpu_tlb_invalid
= FALSE
;
1545 __asm__
volatile("mfence");
1549 * Retire the given physical map from service.
1550 * Should only be called if the map contains
1551 * no valid mappings.
1563 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_START
,
1564 (int) p
, 0, 0, 0, 0);
1572 * If some cpu is not using the physical pmap pointer that it
1573 * is supposed to be (see set_dirbase), we might be using the
1574 * pmap that is being destroyed! Make sure we are
1575 * physically on the right pmap:
1579 0xFFFFFFFFFFFFF000ULL
);
1585 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
,
1586 (int) p
, 1, 0, 0, 0);
1587 return; /* still in use */
1591 * Free the memory maps, then the
1595 OSAddAtomic(-p
->pm_obj
->resident_page_count
, &inuse_ptepages_count
);
1596 PMAP_ZINFO_PFREE(p
, p
->pm_obj
->resident_page_count
* PAGE_SIZE
);
1598 kmem_free(kernel_map
, (vm_offset_t
)p
->dirbase
, NBPTD
);
1599 PMAP_ZINFO_SFREE(p
, NBPTD
);
1601 zfree(pdpt_zone
, (void *)p
->pm_hold
);
1603 vm_object_deallocate(p
->pm_obj
);
1606 int inuse_ptepages
= 0;
1608 /* free 64 bit mode structs */
1609 kmem_free(kernel_map
, (vm_offset_t
)p
->pm_hold
, PAGE_SIZE
);
1610 PMAP_ZINFO_SFREE(p
, PAGE_SIZE
);
1612 inuse_ptepages
+= p
->pm_obj_pml4
->resident_page_count
;
1613 vm_object_deallocate(p
->pm_obj_pml4
);
1615 inuse_ptepages
+= p
->pm_obj_pdpt
->resident_page_count
;
1616 vm_object_deallocate(p
->pm_obj_pdpt
);
1618 inuse_ptepages
+= p
->pm_obj
->resident_page_count
;
1619 vm_object_deallocate(p
->pm_obj
);
1621 OSAddAtomic(-(inuse_ptepages
+1), &inuse_ptepages_count
);
1622 PMAP_ZINFO_PFREE(p
, inuse_ptepages
* PAGE_SIZE
);
1624 ledger_dereference(p
->ledger
);
1626 zfree(pmap_zone
, p
);
1628 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY
) | DBG_FUNC_END
,
1634 * Add a reference to the specified pmap.
1642 if (p
!= PMAP_NULL
) {
1650 * Remove phys addr if mapped in specified map
1654 pmap_remove_some_phys(
1655 __unused pmap_t map
,
1656 __unused ppnum_t pn
)
1659 /* Implement to support working set code */
1664 * Set the physical protection on the
1665 * specified range of this map as requested.
1666 * Will not increase permissions.
1671 vm_map_offset_t sva
,
1672 vm_map_offset_t eva
,
1675 register pt_entry_t
*pde
;
1676 register pt_entry_t
*spte
, *epte
;
1677 vm_map_offset_t lva
;
1678 vm_map_offset_t orig_sva
;
1684 if (map
== PMAP_NULL
)
1687 if (prot
== VM_PROT_NONE
) {
1688 pmap_remove(map
, sva
, eva
);
1692 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_START
,
1694 (int) (sva
>>32), (int) sva
,
1695 (int) (eva
>>32), (int) eva
);
1697 if ( (prot
& VM_PROT_EXECUTE
) || !nx_enabled
|| !map
->nx_enabled
)
1706 lva
= (sva
+ pde_mapped_size
) & ~(pde_mapped_size
-1);
1709 pde
= pmap_pde(map
, sva
);
1710 if (pde
&& (*pde
& INTEL_PTE_VALID
)) {
1711 spte
= (pt_entry_t
*)pmap_pte(map
, (sva
& ~(pde_mapped_size
-1)));
1712 spte
= &spte
[ptenum(sva
)];
1713 epte
= &spte
[intel_btop(lva
-sva
)];
1715 while (spte
< epte
) {
1717 if (*spte
& INTEL_PTE_VALID
) {
1718 if (prot
& VM_PROT_WRITE
)
1719 pmap_update_pte(spte
, 0, INTEL_PTE_WRITE
);
1721 pmap_update_pte(spte
, INTEL_PTE_WRITE
, 0);
1724 pmap_update_pte(spte
,0, INTEL_PTE_NX
);
1726 pmap_update_pte(spte
, INTEL_PTE_NX
, 0);
1737 PMAP_UPDATE_TLBS(map
, orig_sva
, eva
);
1742 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT
) | DBG_FUNC_END
,
1747 /* Map a (possibly) autogenned block */
1756 __unused
unsigned int flags
)
1760 for (page
= 0; page
< size
; page
++) {
1761 pmap_enter(pmap
, va
, pa
, prot
, VM_PROT_NONE
, attr
, TRUE
);
1768 * Routine: pmap_extract
1770 * Extract the physical page address associated
1771 * with the given map/virtual_address pair.
1772 * Change to shim for backwards compatibility but will not
1773 * work for 64 bit systems. Some old drivers that we cannot
1779 register pmap_t pmap
,
1780 vm_map_offset_t vaddr
)
1785 paddr
= (vm_offset_t
)0;
1786 ppn
= pmap_find_phys(pmap
, vaddr
);
1789 paddr
= ((vm_offset_t
)i386_ptob(ppn
)) | ((vm_offset_t
)vaddr
& INTEL_OFFMASK
);
1797 vm_map_offset_t vaddr
,
1798 __unused
unsigned int options
)
1800 register vm_page_t m
;
1801 register pmap_paddr_t pa
;
1805 pml4_entry_t
*pml4p
;
1807 if (kernel_pmap
== map
) panic("expand kernel pml4");
1810 pml4p
= pmap64_pml4(map
, vaddr
);
1812 if (PML4_ENTRY_NULL
== pml4p
) panic("pmap_expand_pml4 no pml4p");
1815 * Allocate a VM page for the pml4 page
1817 while ((m
= vm_page_grab()) == VM_PAGE_NULL
)
1821 * put the page into the pmap's obj list so it
1822 * can be found later.
1826 i
= pml4idx(map
, vaddr
);
1833 vm_page_lockspin_queues();
1835 vm_page_unlock_queues();
1837 OSAddAtomic(1, &inuse_ptepages_count
);
1838 OSAddAtomic64(1, &alloc_ptepages_count
);
1839 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1841 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1842 vm_object_lock(map
->pm_obj_pml4
);
1846 * See if someone else expanded us first
1848 if (pmap64_pdpt(map
, vaddr
) != PDPT_ENTRY_NULL
) {
1850 vm_object_unlock(map
->pm_obj_pml4
);
1854 OSAddAtomic(-1, &inuse_ptepages_count
);
1855 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1856 return KERN_SUCCESS
;
1858 pmap_set_noencrypt(pn
);
1861 if (0 != vm_page_lookup(map
->pm_obj_pml4
, (vm_object_offset_t
)i
)) {
1862 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1863 map
, map
->pm_obj_pml4
, vaddr
, i
);
1866 vm_page_insert(m
, map
->pm_obj_pml4
, (vm_object_offset_t
)i
);
1867 vm_object_unlock(map
->pm_obj_pml4
);
1870 * Set the page directory entry for this page table.
1872 pml4p
= pmap64_pml4(map
, vaddr
); /* refetch under lock */
1874 pmap_store_pte(pml4p
, pa_to_pte(pa
)
1881 return KERN_SUCCESS
;
1885 pmap_expand_pdpt(pmap_t map
, vm_map_offset_t vaddr
, __unused
unsigned int options
)
1887 register vm_page_t m
;
1888 register pmap_paddr_t pa
;
1892 pdpt_entry_t
*pdptp
;
1894 if (kernel_pmap
== map
) panic("expand kernel pdpt");
1897 while ((pdptp
= pmap64_pdpt(map
, vaddr
)) == PDPT_ENTRY_NULL
) {
1899 pmap_expand_pml4(map
, vaddr
, PMAP_EXPAND_OPTIONS_NONE
); /* need room for another pdpt entry */
1905 * Allocate a VM page for the pdpt page
1907 while ((m
= vm_page_grab()) == VM_PAGE_NULL
)
1911 * put the page into the pmap's obj list so it
1912 * can be found later.
1916 i
= pdptidx(map
, vaddr
);
1923 vm_page_lockspin_queues();
1925 vm_page_unlock_queues();
1927 OSAddAtomic(1, &inuse_ptepages_count
);
1928 OSAddAtomic64(1, &alloc_ptepages_count
);
1929 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
1931 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1932 vm_object_lock(map
->pm_obj_pdpt
);
1936 * See if someone else expanded us first
1938 if (pmap64_pde(map
, vaddr
) != PD_ENTRY_NULL
) {
1940 vm_object_unlock(map
->pm_obj_pdpt
);
1944 OSAddAtomic(-1, &inuse_ptepages_count
);
1945 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
1946 return KERN_SUCCESS
;
1948 pmap_set_noencrypt(pn
);
1951 if (0 != vm_page_lookup(map
->pm_obj_pdpt
, (vm_object_offset_t
)i
)) {
1952 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1953 map
, map
->pm_obj_pdpt
, vaddr
, i
);
1956 vm_page_insert(m
, map
->pm_obj_pdpt
, (vm_object_offset_t
)i
);
1957 vm_object_unlock(map
->pm_obj_pdpt
);
1960 * Set the page directory entry for this page table.
1962 pdptp
= pmap64_pdpt(map
, vaddr
); /* refetch under lock */
1964 pmap_store_pte(pdptp
, pa_to_pte(pa
)
1971 return KERN_SUCCESS
;
1977 * Routine: pmap_expand
1979 * Expands a pmap to be able to map the specified virtual address.
1981 * Allocates new virtual memory for the P0 or P1 portion of the
1982 * pmap, then re-maps the physical pages that were in the old
1983 * pmap to be in the new pmap.
1985 * Must be called with the pmap system and the pmap unlocked,
1986 * since these must be unlocked to use vm_allocate or vm_deallocate.
1987 * Thus it must be called in a loop that checks whether the map
1988 * has been expanded enough.
1989 * (We won't loop forever, since page tables aren't shrunk.)
1994 vm_map_offset_t vaddr
,
1995 __unused
unsigned int options
)
1998 register vm_page_t m
;
1999 register pmap_paddr_t pa
;
2005 * if not the kernel map (while we are still compat kernel mode)
2006 * and we are 64 bit, propagate expand upwards
2009 if (cpu_64bit
&& (map
!= kernel_pmap
)) {
2011 while ((pdp
= pmap64_pde(map
, vaddr
)) == PD_ENTRY_NULL
) {
2013 pmap_expand_pdpt(map
, vaddr
, PMAP_EXPAND_OPTIONS_NONE
); /* need room for another pde entry */
2020 * Allocate a VM page for the pde entries.
2022 while ((m
= vm_page_grab()) == VM_PAGE_NULL
)
2026 * put the page into the pmap's obj list so it
2027 * can be found later.
2031 i
= pdeidx(map
, vaddr
);
2038 vm_page_lockspin_queues();
2040 vm_page_unlock_queues();
2042 OSAddAtomic(1, &inuse_ptepages_count
);
2043 OSAddAtomic64(1, &alloc_ptepages_count
);
2044 PMAP_ZINFO_PALLOC(map
, PAGE_SIZE
);
2046 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
2047 vm_object_lock(map
->pm_obj
);
2051 * See if someone else expanded us first
2054 if (pmap_pte(map
, vaddr
) != PT_ENTRY_NULL
) {
2056 vm_object_unlock(map
->pm_obj
);
2060 OSAddAtomic(-1, &inuse_ptepages_count
);
2061 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
2062 return KERN_SUCCESS
;
2064 pmap_set_noencrypt(pn
);
2067 if (0 != vm_page_lookup(map
->pm_obj
, (vm_object_offset_t
)i
)) {
2068 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
2069 map
, map
->pm_obj
, vaddr
, i
);
2072 vm_page_insert(m
, map
->pm_obj
, (vm_object_offset_t
)i
);
2073 vm_object_unlock(map
->pm_obj
);
2076 * refetch while locked
2079 pdp
= pmap_pde(map
, vaddr
);
2082 * Set the page directory entry for this page table.
2084 pmap_store_pte(pdp
, pa_to_pte(pa
)
2091 return KERN_SUCCESS
;
2096 * pmap_sync_page_data_phys(ppnum_t pa)
2098 * Invalidates all of the instruction cache on a physical page and
2099 * pushes any dirty data from the data cache for the same physical page
2100 * Not required in i386.
2103 pmap_sync_page_data_phys(__unused ppnum_t pa
)
2109 * pmap_sync_page_attributes_phys(ppnum_t pa)
2111 * Write back and invalidate all cachelines on a physical page.
2114 pmap_sync_page_attributes_phys(ppnum_t pa
)
2116 cache_flush_page_phys(pa
);
2121 #ifdef CURRENTLY_UNUSED_AND_UNTESTED
2127 * Routine: pmap_collect
2129 * Garbage collects the physical map system for
2130 * pages which are no longer used.
2131 * Success need not be guaranteed -- that is, there
2132 * may well be pages which are not referenced, but
2133 * others may be collected.
2135 * Called by the pageout daemon when pages are scarce.
2141 register pt_entry_t
*pdp
, *ptp
;
2148 if (p
== kernel_pmap
)
2152 * Garbage collect map.
2156 for (pdp
= (pt_entry_t
*)p
->dirbase
;
2157 pdp
< (pt_entry_t
*)&p
->dirbase
[(UMAXPTDI
+1)];
2160 if (*pdp
& INTEL_PTE_VALID
) {
2161 if(*pdp
& INTEL_PTE_REF
) {
2162 pmap_store_pte(pdp
, *pdp
& ~INTEL_PTE_REF
);
2166 ptp
= pmap_pte(p
, pdetova(pdp
- (pt_entry_t
*)p
->dirbase
));
2167 eptp
= ptp
+ NPTEPG
;
2170 * If the pte page has any wired mappings, we cannot
2175 register pt_entry_t
*ptep
;
2176 for (ptep
= ptp
; ptep
< eptp
; ptep
++) {
2177 if (iswired(*ptep
)) {
2185 * Remove the virtual addresses mapped by this pte page.
2187 pmap_remove_range(p
,
2188 pdetova(pdp
- (pt_entry_t
*)p
->dirbase
),
2193 * Invalidate the page directory pointer.
2195 pmap_store_pte(pdp
, 0x0);
2200 * And free the pte page itself.
2203 register vm_page_t m
;
2205 vm_object_lock(p
->pm_obj
);
2207 m
= vm_page_lookup(p
->pm_obj
,(vm_object_offset_t
)(pdp
- (pt_entry_t
*)&p
->dirbase
[0]));
2208 if (m
== VM_PAGE_NULL
)
2209 panic("pmap_collect: pte page not in object");
2211 vm_object_unlock(p
->pm_obj
);
2215 OSAddAtomic(-1, &inuse_ptepages_count
);
2216 PMAP_ZINFO_PFREE(map
, PAGE_SIZE
);
2225 PMAP_UPDATE_TLBS(p
, 0x0, 0xFFFFFFFFFFFFF000ULL
);
2234 pmap_copy_page(ppnum_t src
, ppnum_t dst
)
2236 bcopy_phys((addr64_t
)i386_ptob(src
),
2237 (addr64_t
)i386_ptob(dst
),
2243 * Routine: pmap_pageable
2245 * Make the specified pages (by pmap, offset)
2246 * pageable (or not) as requested.
2248 * A page which is not pageable may not take
2249 * a fault; therefore, its page table entry
2250 * must remain valid for the duration.
2252 * This routine is merely advisory; pmap_enter
2253 * will specify that these pages are to be wired
2254 * down (or not) as appropriate.
2258 __unused pmap_t pmap
,
2259 __unused vm_map_offset_t start_addr
,
2260 __unused vm_map_offset_t end_addr
,
2261 __unused boolean_t pageable
)
2264 pmap
++; start_addr
++; end_addr
++; pageable
++;
2269 invalidate_icache(__unused vm_offset_t addr
,
2270 __unused
unsigned cnt
,
2276 flush_dcache(__unused vm_offset_t addr
,
2277 __unused
unsigned count
,
2285 * Constrain DTrace copyin/copyout actions
2287 extern kern_return_t
dtrace_copyio_preflight(addr64_t
);
2288 extern kern_return_t
dtrace_copyio_postflight(addr64_t
);
2290 kern_return_t
dtrace_copyio_preflight(__unused addr64_t va
)
2292 thread_t thread
= current_thread();
2294 if (current_map() == kernel_map
)
2295 return KERN_FAILURE
;
2296 else if (thread
->machine
.specFlags
& CopyIOActive
)
2297 return KERN_FAILURE
;
2299 return KERN_SUCCESS
;
2302 kern_return_t
dtrace_copyio_postflight(__unused addr64_t va
)
2304 return KERN_SUCCESS
;
2306 #endif /* CONFIG_DTRACE */
2308 #include <mach_vm_debug.h>
2310 #include <vm/vm_debug.h>
2313 pmap_list_resident_pages(
2314 __unused pmap_t pmap
,
2315 __unused vm_offset_t
*listp
,
2320 #endif /* MACH_VM_DEBUG */
2324 /* temporary workaround */
2326 coredumpok(__unused vm_map_t map
, __unused vm_offset_t va
)
2331 ptep
= pmap_pte(map
->pmap
, va
);
2334 return ((*ptep
& (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
)) != (INTEL_PTE_NCACHE
| INTEL_PTE_WIRED
));
2345 assert(pn
!= vm_page_fictitious_addr
);
2347 if (!pmap_initialized
)
2350 if (pn
== vm_page_guard_addr
)
2353 if (!managed_page(ppn_to_pai(pn
)))
2360 pmap_commpage32_init(vm_offset_t kernel_commpage
, vm_offset_t user_commpage
, int cnt
)
2363 pt_entry_t
*opte
, *npte
;
2367 for (i
= 0; i
< cnt
; i
++) {
2369 opte
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)kernel_commpage
);
2371 panic("kernel_commpage");
2372 pte
= *opte
| INTEL_PTE_USER
|INTEL_PTE_GLOBAL
;
2373 pte
&= ~INTEL_PTE_WRITE
; // ensure read only
2374 npte
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)user_commpage
);
2376 panic("user_commpage");
2377 pmap_store_pte(npte
, pte
);
2379 kernel_commpage
+= INTEL_PGBYTES
;
2380 user_commpage
+= INTEL_PGBYTES
;
2385 #define PMAP_COMMPAGE64_CNT (_COMM_PAGE64_AREA_USED/PAGE_SIZE)
2386 pt_entry_t pmap_commpage64_ptes
[PMAP_COMMPAGE64_CNT
];
2389 pmap_commpage64_init(vm_offset_t kernel_commpage
, __unused vm_map_offset_t user_commpage
, int cnt
)
2394 PMAP_LOCK(kernel_pmap
);
2396 for (i
= 0; i
< cnt
; i
++) {
2397 kptep
= pmap_pte(kernel_pmap
, (uint64_t)kernel_commpage
+ (i
*PAGE_SIZE
));
2398 if ((0 == kptep
) || (0 == (*kptep
& INTEL_PTE_VALID
)))
2399 panic("pmap_commpage64_init pte");
2400 pmap_commpage64_ptes
[i
] = ((*kptep
& ~INTEL_PTE_WRITE
) | INTEL_PTE_USER
);
2402 PMAP_UNLOCK(kernel_pmap
);
2406 static cpu_pmap_t cpu_pmap_master
;
2409 pmap_cpu_alloc(boolean_t is_boot_cpu
)
2414 vm_offset_t address
;
2415 vm_map_address_t mapaddr
;
2416 vm_map_entry_t entry
;
2420 cp
= &cpu_pmap_master
;
2423 * The per-cpu pmap data structure itself.
2425 ret
= kmem_alloc(kernel_map
,
2426 (vm_offset_t
*) &cp
, sizeof(cpu_pmap_t
));
2427 if (ret
!= KERN_SUCCESS
) {
2428 printf("pmap_cpu_alloc() failed ret=%d\n", ret
);
2431 bzero((void *)cp
, sizeof(cpu_pmap_t
));
2434 * The temporary windows used for copy/zero - see loose_ends.c
2436 ret
= vm_map_find_space(kernel_map
,
2437 &mapaddr
, PMAP_NWINDOWS
*PAGE_SIZE
, (vm_map_offset_t
)0, 0, &entry
);
2438 if (ret
!= KERN_SUCCESS
) {
2439 printf("pmap_cpu_alloc() "
2440 "vm_map_find_space ret=%d\n", ret
);
2444 address
= (vm_offset_t
)mapaddr
;
2446 for (i
= 0; i
< PMAP_NWINDOWS
; i
++, address
+= PAGE_SIZE
) {
2449 while ((pte
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)address
)) == 0)
2450 pmap_expand(kernel_pmap
, (vm_map_offset_t
)address
, PMAP_EXPAND_OPTIONS_NONE
);
2452 cp
->mapwindow
[i
].prv_CADDR
= (caddr_t
) address
;
2453 cp
->mapwindow
[i
].prv_CMAP
= pte
;
2456 vm_map_unlock(kernel_map
);
2459 cp
->pdpt_window_index
= PMAP_PDPT_FIRST_WINDOW
;
2460 cp
->pde_window_index
= PMAP_PDE_FIRST_WINDOW
;
2461 cp
->pte_window_index
= PMAP_PTE_FIRST_WINDOW
;
2467 pmap_cpu_free(struct cpu_pmap
*cp
)
2469 if (cp
!= NULL
&& cp
!= &cpu_pmap_master
) {
2470 kfree((void *) cp
, sizeof(cpu_pmap_t
));
2475 pmap_get_mapwindow(pt_entry_t pentry
)
2480 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2481 /* fold in cache attributes for this physical page */
2482 pentry
|= pmap_get_cache_attributes(i386_btop(pte_to_pa(pentry
)));
2484 * Note: 0th map reserved for pmap_pte()
2486 for (i
= PMAP_NWINDOWS_FIRSTFREE
; i
< PMAP_NWINDOWS
; i
++) {
2487 mp
= ¤t_cpu_datap()->cpu_pmap
->mapwindow
[i
];
2489 if (*mp
->prv_CMAP
== 0) {
2490 pmap_store_pte(mp
->prv_CMAP
, pentry
);
2492 invlpg((uintptr_t)mp
->prv_CADDR
);
2497 panic("pmap_get_mapwindow: no windows available");
2504 pmap_put_mapwindow(mapwindow_t
*mp
)
2506 pmap_store_pte(mp
->prv_CMAP
, 0);
2510 pmap_switch(pmap_t tpmap
)
2514 s
= splhigh(); /* Make sure interruptions are disabled */
2516 set_dirbase(tpmap
, current_thread());
2523 * disable no-execute capability on
2524 * the specified pmap
2526 void pmap_disable_NX(pmap_t pmap
) {
2528 pmap
->nx_enabled
= 0;
2532 pt_fake_zone_init(int zone_index
)
2534 pt_fake_zone_index
= zone_index
;
2538 pt_fake_zone_info(int *count
,
2539 vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
, vm_size_t
*alloc_size
,
2540 uint64_t *sum_size
, int *collectable
, int *exhaustable
, int *caller_acct
)
2542 *count
= inuse_ptepages_count
;
2543 *cur_size
= PAGE_SIZE
* inuse_ptepages_count
;
2544 *max_size
= PAGE_SIZE
* (inuse_ptepages_count
+ vm_page_inactive_count
+ vm_page_active_count
+ vm_page_free_count
);
2545 *elem_size
= PAGE_SIZE
;
2546 *alloc_size
= PAGE_SIZE
;
2547 *sum_size
= alloc_ptepages_count
* PAGE_SIZE
;
2554 vm_offset_t
pmap_cpu_high_map_vaddr(int cpu
, enum high_cpu_types e
)
2556 enum high_fixed_addresses a
;
2557 a
= e
+ HIGH_CPU_END
* cpu
;
2558 return pmap_index_to_virt(HIGH_FIXED_CPUS_BEGIN
+ a
);
2561 vm_offset_t
pmap_high_map_vaddr(enum high_cpu_types e
)
2563 return pmap_cpu_high_map_vaddr(cpu_number(), e
);
2566 vm_offset_t
pmap_high_map(pt_entry_t pte
, enum high_cpu_types e
)
2568 enum high_fixed_addresses a
;
2571 a
= e
+ HIGH_CPU_END
* cpu_number();
2572 vaddr
= (vm_offset_t
)pmap_index_to_virt(HIGH_FIXED_CPUS_BEGIN
+ a
);
2573 pmap_store_pte(pte_unique_base
+ a
, pte
);
2575 /* TLB flush for this page for this cpu */
2576 invlpg((uintptr_t)vaddr
);
2582 pmap_cpuset_NMIPI(cpu_set cpu_mask
) {
2583 unsigned int cpu
, cpu_bit
;
2586 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2587 if (cpu_mask
& cpu_bit
)
2588 cpu_NMI_interrupt(cpu
);
2590 deadline
= mach_absolute_time() + (((uint64_t)LockTimeOut
) * 3);
2591 while (mach_absolute_time() < deadline
)
2596 * Called with pmap locked, we:
2597 * - scan through per-cpu data to see which other cpus need to flush
2598 * - send an IPI to each non-idle cpu to be flushed
2599 * - wait for all to signal back that they are inactive or we see that
2600 * they are in an interrupt handler or at a safe point
2601 * - flush the local tlb is active for this pmap
2602 * - return ... the caller will unlock the pmap
2605 pmap_flush_tlbs(pmap_t pmap
, vm_map_offset_t startv
, vm_map_offset_t endv
)
2608 unsigned int cpu_bit
;
2609 cpu_set cpus_to_signal
;
2610 unsigned int my_cpu
= cpu_number();
2611 pmap_paddr_t pmap_cr3
= pmap
->pm_cr3
;
2612 boolean_t flush_self
= FALSE
;
2615 assert((processor_avail_count
< 2) ||
2616 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
2619 * Scan other cpus for matching active or task CR3.
2620 * For idle cpus (with no active map) we mark them invalid but
2621 * don't signal -- they'll check as they go busy.
2622 * Note: for the kernel pmap we look for 64-bit shared address maps.
2625 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2626 if (!cpu_datap(cpu
)->cpu_running
)
2628 if ((cpu_datap(cpu
)->cpu_task_cr3
== pmap_cr3
) ||
2629 (CPU_GET_ACTIVE_CR3(cpu
) == pmap_cr3
) ||
2630 (pmap
->pm_shared
) ||
2631 ((pmap
== kernel_pmap
) &&
2632 (!CPU_CR3_IS_ACTIVE(cpu
) ||
2633 cpu_datap(cpu
)->cpu_task_map
== TASK_MAP_64BIT_SHARED
))) {
2634 if (cpu
== my_cpu
) {
2638 cpu_datap(cpu
)->cpu_tlb_invalid
= TRUE
;
2639 __asm__
volatile("mfence");
2641 if (CPU_CR3_IS_ACTIVE(cpu
)) {
2642 cpus_to_signal
|= cpu_bit
;
2643 i386_signal_cpu(cpu
, MP_TLB_FLUSH
, ASYNC
);
2648 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS
) | DBG_FUNC_START
,
2649 (uintptr_t) pmap
, cpus_to_signal
, flush_self
, startv
, 0);
2651 if (cpus_to_signal
) {
2652 cpu_set cpus_to_respond
= cpus_to_signal
;
2654 deadline
= mach_absolute_time() + LockTimeOut
;
2656 * Wait for those other cpus to acknowledge
2658 while (cpus_to_respond
!= 0) {
2661 for (cpu
= 0, cpu_bit
= 1; cpu
< real_ncpus
; cpu
++, cpu_bit
<<= 1) {
2662 if ((cpus_to_respond
& cpu_bit
) != 0) {
2663 if (!cpu_datap(cpu
)->cpu_running
||
2664 cpu_datap(cpu
)->cpu_tlb_invalid
== FALSE
||
2665 !CPU_CR3_IS_ACTIVE(cpu
)) {
2666 cpus_to_respond
&= ~cpu_bit
;
2670 if (cpus_to_respond
== 0)
2674 if (cpus_to_respond
&& (mach_absolute_time() > deadline
)) {
2675 if (machine_timeout_suspended())
2677 pmap_tlb_flush_timeout
= TRUE
;
2678 orig_acks
= NMIPI_acks
;
2679 pmap_cpuset_NMIPI(cpus_to_respond
);
2681 panic("TLB invalidation IPI timeout: "
2682 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%lx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2683 cpus_to_respond
, orig_acks
, NMIPI_acks
);
2688 * Flush local tlb if required.
2689 * We need this flush even if the pmap being changed
2690 * is the user map... in case we do a copyin/out
2691 * before returning to user mode.
2696 if ((pmap
== kernel_pmap
) && (flush_self
!= TRUE
)) {
2697 panic("pmap_flush_tlbs: pmap == kernel_pmap && flush_self != TRUE; kernel CR3: 0x%llX, CPU active CR3: 0x%llX, CPU Task Map: %d", kernel_pmap
->pm_cr3
, current_cpu_datap()->cpu_active_cr3
, current_cpu_datap()->cpu_task_map
);
2700 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS
) | DBG_FUNC_END
,
2701 (uintptr_t) pmap
, cpus_to_signal
, startv
, endv
, 0);
2705 process_pmap_updates(void)
2707 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2711 current_cpu_datap()->cpu_tlb_invalid
= FALSE
;
2712 __asm__
volatile("mfence");
2716 pmap_update_interrupt(void)
2718 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_START
,
2721 process_pmap_updates();
2723 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT
) | DBG_FUNC_END
,
2732 kprintf("pmap 0x%x\n",p
);
2734 kprintf(" pm_cr3 0x%llx\n",p
->pm_cr3
);
2735 kprintf(" pm_pml4 0x%x\n",p
->pm_pml4
);
2736 kprintf(" pm_pdpt 0x%x\n",p
->pm_pdpt
);
2738 kprintf(" pml4[0] 0x%llx\n",*p
->pm_pml4
);
2740 kprintf(" pdpt[%d] 0x%llx\n",i
, p
->pm_pdpt
[i
]);
2743 void pmap_dump_wrap(void)
2745 pmap_dump(current_cpu_datap()->cpu_active_thread
->task
->map
->pmap
);
2749 dump_4GB_pdpt(pmap_t p
)
2752 pdpt_entry_t
*user_pdptp
;
2753 pdpt_entry_t
*kern_pdptp
;
2754 pdpt_entry_t
*pml4p
;
2757 while ((user_pdptp
= pmap64_pdpt(p
, 0x0)) == PDPT_ENTRY_NULL
) {
2759 pmap_expand_pml4(p
, 0x0, PMAP_EXPAND_OPTIONS_NONE
);
2762 kern_pdptp
= kernel_pmap
->pm_pdpt
;
2763 if (kern_pdptp
== NULL
)
2764 panic("kern_pdptp == NULL");
2765 kprintf("dump_4GB_pdpt(%p)\n"
2766 "kern_pdptp=%p (phys=0x%016llx)\n"
2767 "\t 0x%08x: 0x%016llx\n"
2768 "\t 0x%08x: 0x%016llx\n"
2769 "\t 0x%08x: 0x%016llx\n"
2770 "\t 0x%08x: 0x%016llx\n"
2771 "\t 0x%08x: 0x%016llx\n"
2772 "user_pdptp=%p (phys=0x%016llx)\n"
2773 "\t 0x%08x: 0x%016llx\n"
2774 "\t 0x%08x: 0x%016llx\n"
2775 "\t 0x%08x: 0x%016llx\n"
2776 "\t 0x%08x: 0x%016llx\n"
2777 "\t 0x%08x: 0x%016llx\n",
2778 p
, kern_pdptp
, kvtophys(kern_pdptp
),
2779 kern_pdptp
+0, *(kern_pdptp
+0),
2780 kern_pdptp
+1, *(kern_pdptp
+1),
2781 kern_pdptp
+2, *(kern_pdptp
+2),
2782 kern_pdptp
+3, *(kern_pdptp
+3),
2783 kern_pdptp
+4, *(kern_pdptp
+4),
2784 user_pdptp
, kvtophys(user_pdptp
),
2785 user_pdptp
+0, *(user_pdptp
+0),
2786 user_pdptp
+1, *(user_pdptp
+1),
2787 user_pdptp
+2, *(user_pdptp
+2),
2788 user_pdptp
+3, *(user_pdptp
+3),
2789 user_pdptp
+4, *(user_pdptp
+4));
2790 kprintf("user pm_cr3=0x%016llx pm_hold=0x%08x pm_pml4=0x%08x\n",
2791 p
->pm_cr3
, p
->pm_hold
, p
->pm_pml4
);
2792 pml4p
= (pdpt_entry_t
*)p
->pm_hold
;
2794 panic("user pml4p == NULL");
2795 kprintf("\t 0x%08x: 0x%016llx\n"
2796 "\t 0x%08x: 0x%016llx\n",
2798 pml4p
+KERNEL_UBER_PML4_INDEX
, *(pml4p
+KERNEL_UBER_PML4_INDEX
));
2799 kprintf("kern pm_cr3=0x%016llx pm_hold=0x%08x pm_pml4=0x%08x\n",
2800 kernel_pmap
->pm_cr3
, kernel_pmap
->pm_hold
, kernel_pmap
->pm_pml4
);
2801 pml4p
= (pdpt_entry_t
*)kernel_pmap
->pm_hold
;
2803 panic("kern pml4p == NULL");
2804 kprintf("\t 0x%08x: 0x%016llx\n"
2805 "\t 0x%08x: 0x%016llx\n",
2807 pml4p
+511, *(pml4p
+511));
2811 void dump_4GB_pdpt_thread(thread_t tp
)
2813 dump_4GB_pdpt(tp
->map
->pmap
);