]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39236c6e | 2 | * Copyright (c) 2000-2012 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
8f6c56a5 | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
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. | |
41 | * | |
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. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | /* | |
57 | */ | |
58 | ||
59 | /* | |
60 | * File: pmap.h | |
61 | * | |
62 | * Authors: Avadis Tevanian, Jr., Michael Wayne Young | |
63 | * Date: 1985 | |
64 | * | |
65 | * Machine-dependent structures for the physical map module. | |
66 | */ | |
0c530ab8 | 67 | #ifdef KERNEL_PRIVATE |
1c79356b A |
68 | #ifndef _PMAP_MACHINE_ |
69 | #define _PMAP_MACHINE_ 1 | |
70 | ||
71 | #ifndef ASSEMBLER | |
72 | ||
73 | #include <platforms.h> | |
1c79356b A |
74 | |
75 | #include <mach/kern_return.h> | |
76 | #include <mach/machine/vm_types.h> | |
77 | #include <mach/vm_prot.h> | |
78 | #include <mach/vm_statistics.h> | |
79 | #include <mach/machine/vm_param.h> | |
80 | #include <kern/kern_types.h> | |
91447636 | 81 | #include <kern/thread.h> |
1c79356b | 82 | #include <kern/lock.h> |
6d2010ae | 83 | #include <mach/branch_predicates.h> |
0c530ab8 A |
84 | |
85 | #include <i386/mp.h> | |
86 | #include <i386/proc_reg.h> | |
1c79356b | 87 | |
6d2010ae A |
88 | #include <i386/pal_routines.h> |
89 | ||
1c79356b A |
90 | /* |
91 | * Define the generic in terms of the specific | |
92 | */ | |
93 | ||
94 | #define INTEL_PGBYTES I386_PGBYTES | |
95 | #define INTEL_PGSHIFT I386_PGSHIFT | |
96 | #define intel_btop(x) i386_btop(x) | |
97 | #define intel_ptob(x) i386_ptob(x) | |
98 | #define intel_round_page(x) i386_round_page(x) | |
99 | #define intel_trunc_page(x) i386_trunc_page(x) | |
100 | #define trunc_intel_to_vm(x) trunc_i386_to_vm(x) | |
101 | #define round_intel_to_vm(x) round_i386_to_vm(x) | |
102 | #define vm_to_intel(x) vm_to_i386(x) | |
103 | ||
104 | /* | |
105 | * i386/i486/i860 Page Table Entry | |
106 | */ | |
107 | ||
1c79356b A |
108 | #endif /* ASSEMBLER */ |
109 | ||
316670eb A |
110 | #define NPGPTD 4ULL |
111 | #define PDESHIFT 21ULL | |
112 | #define PTEMASK 0x1ffULL | |
113 | #define PTEINDX 3ULL | |
91447636 | 114 | |
316670eb | 115 | #define PTESHIFT 12ULL |
b0d623f7 | 116 | |
b0d623f7 A |
117 | |
118 | #ifdef __x86_64__ | |
119 | #define LOW_4GB_MASK ((vm_offset_t)0x00000000FFFFFFFFUL) | |
120 | #endif | |
121 | ||
91447636 A |
122 | #define PDESIZE sizeof(pd_entry_t) /* for assembly files */ |
123 | #define PTESIZE sizeof(pt_entry_t) /* for assembly files */ | |
124 | ||
125 | #define INTEL_OFFMASK (I386_PGBYTES - 1) | |
b0d623f7 | 126 | #define INTEL_LOFFMASK (I386_LPGBYTES - 1) |
0c530ab8 | 127 | #define PG_FRAME 0x000FFFFFFFFFF000ULL |
91447636 | 128 | #define NPTEPG (PAGE_SIZE/(sizeof (pt_entry_t))) |
0c530ab8 | 129 | #define NPTDPG (PAGE_SIZE/(sizeof (pd_entry_t))) |
1c79356b | 130 | |
91447636 A |
131 | #define NBPTD (NPGPTD << PAGE_SHIFT) |
132 | #define NPDEPTD (NBPTD / (sizeof (pd_entry_t))) | |
133 | #define NPDEPG (PAGE_SIZE/(sizeof (pd_entry_t))) | |
316670eb | 134 | #define NBPDE (1ULL << PDESHIFT) |
91447636 | 135 | #define PDEMASK (NBPDE - 1) |
9bccf70c | 136 | |
b0d623f7 A |
137 | #define PTE_PER_PAGE 512 /* number of PTE's per page on any level */ |
138 | ||
0c530ab8 A |
139 | /* cleanly define parameters for all the page table levels */ |
140 | typedef uint64_t pml4_entry_t; | |
141 | #define NPML4PG (PAGE_SIZE/(sizeof (pml4_entry_t))) | |
142 | #define PML4SHIFT 39 | |
143 | #define PML4PGSHIFT 9 | |
144 | #define NBPML4 (1ULL << PML4SHIFT) | |
145 | #define PML4MASK (NBPML4-1) | |
146 | #define PML4_ENTRY_NULL ((pml4_entry_t *) 0) | |
147 | ||
148 | typedef uint64_t pdpt_entry_t; | |
149 | #define NPDPTPG (PAGE_SIZE/(sizeof (pdpt_entry_t))) | |
150 | #define PDPTSHIFT 30 | |
151 | #define PDPTPGSHIFT 9 | |
316670eb | 152 | #define NBPDPT (1ULL << PDPTSHIFT) |
0c530ab8 A |
153 | #define PDPTMASK (NBPDPT-1) |
154 | #define PDPT_ENTRY_NULL ((pdpt_entry_t *) 0) | |
155 | ||
156 | typedef uint64_t pd_entry_t; | |
157 | #define NPDPG (PAGE_SIZE/(sizeof (pd_entry_t))) | |
158 | #define PDSHIFT 21 | |
159 | #define PDPGSHIFT 9 | |
316670eb | 160 | #define NBPD (1ULL << PDSHIFT) |
0c530ab8 A |
161 | #define PDMASK (NBPD-1) |
162 | #define PD_ENTRY_NULL ((pd_entry_t *) 0) | |
163 | ||
164 | typedef uint64_t pt_entry_t; | |
165 | #define NPTPG (PAGE_SIZE/(sizeof (pt_entry_t))) | |
166 | #define PTSHIFT 12 | |
167 | #define PTPGSHIFT 9 | |
316670eb | 168 | #define NBPT (1ULL << PTSHIFT) |
0c530ab8 A |
169 | #define PTMASK (NBPT-1) |
170 | #define PT_ENTRY_NULL ((pt_entry_t *) 0) | |
171 | ||
172 | typedef uint64_t pmap_paddr_t; | |
173 | ||
6d2010ae A |
174 | #if DEBUG |
175 | #define PMAP_ASSERT 1 | |
176 | #endif | |
177 | #if PMAP_ASSERT | |
178 | #define pmap_assert(ex) ((ex) ? (void)0 : Assert(__FILE__, __LINE__, # ex)) | |
179 | ||
180 | #define pmap_assert2(ex, fmt, args...) \ | |
181 | do { \ | |
182 | if (!(ex)) { \ | |
183 | kprintf("Assertion %s failed (%s:%d, caller %p) " fmt , #ex, __FILE__, __LINE__, __builtin_return_address(0), ##args); \ | |
184 | panic("Assertion %s failed (%s:%d, caller %p) " fmt , #ex, __FILE__, __LINE__, __builtin_return_address(0), ##args); \ | |
185 | } \ | |
186 | } while(0) | |
187 | #else | |
188 | #define pmap_assert(ex) | |
189 | #define pmap_assert2(ex, fmt, args...) | |
190 | #endif | |
191 | ||
b0d623f7 A |
192 | /* superpages */ |
193 | #ifdef __x86_64__ | |
194 | #define SUPERPAGE_NBASEPAGES 512 | |
195 | #else | |
196 | #define SUPERPAGE_NBASEPAGES 1 /* we don't support superpages on i386 */ | |
197 | #endif | |
198 | ||
0c530ab8 A |
199 | /* |
200 | * Atomic 64-bit store of a page table entry. | |
201 | */ | |
202 | static inline void | |
203 | pmap_store_pte(pt_entry_t *entryp, pt_entry_t value) | |
204 | { | |
b0d623f7 A |
205 | /* |
206 | * In the 32-bit kernel a compare-and-exchange loop was | |
207 | * required to provide atomicity. For K64, life is easier: | |
208 | */ | |
209 | *entryp = value; | |
0c530ab8 A |
210 | } |
211 | ||
0c530ab8 A |
212 | /* in 64 bit spaces, the number of each type of page in the page tables */ |
213 | #define NPML4PGS (1ULL * (PAGE_SIZE/(sizeof (pml4_entry_t)))) | |
214 | #define NPDPTPGS (NPML4PGS * (PAGE_SIZE/(sizeof (pdpt_entry_t)))) | |
215 | #define NPDEPGS (NPDPTPGS * (PAGE_SIZE/(sizeof (pd_entry_t)))) | |
216 | #define NPTEPGS (NPDEPGS * (PAGE_SIZE/(sizeof (pt_entry_t)))) | |
217 | ||
316670eb | 218 | #define KERNEL_PML4_INDEX 511 |
b0d623f7 | 219 | #define KERNEL_KEXTS_INDEX 510 /* Home of KEXTs - the basement */ |
316670eb | 220 | #define KERNEL_PHYSMAP_PML4_INDEX 509 /* virtual to physical map */ |
b0d623f7 A |
221 | #define KERNEL_BASE (0ULL - NBPML4) |
222 | #define KERNEL_BASEMENT (KERNEL_BASE - NBPML4) | |
0c530ab8 | 223 | |
55e303ae | 224 | #define VM_WIMG_COPYBACK VM_MEM_COHERENT |
316670eb | 225 | #define VM_WIMG_COPYBACKLW VM_WIMG_COPYBACK |
9bccf70c | 226 | #define VM_WIMG_DEFAULT VM_MEM_COHERENT |
55e303ae A |
227 | /* ?? intel ?? */ |
228 | #define VM_WIMG_IO (VM_MEM_COHERENT | \ | |
229 | VM_MEM_NOT_CACHEABLE | VM_MEM_GUARDED) | |
230 | #define VM_WIMG_WTHRU (VM_MEM_WRITE_THROUGH | VM_MEM_COHERENT | VM_MEM_GUARDED) | |
231 | /* write combining mode, aka store gather */ | |
232 | #define VM_WIMG_WCOMB (VM_MEM_NOT_CACHEABLE | VM_MEM_COHERENT) | |
316670eb | 233 | #define VM_WIMG_INNERWBACK VM_MEM_COHERENT |
0c530ab8 A |
234 | /* |
235 | * Pte related macros | |
236 | */ | |
b0d623f7 A |
237 | #define KVADDR(pmi, pdpi, pdi, pti) \ |
238 | ((vm_offset_t) \ | |
239 | ((uint64_t) -1 << 47) | \ | |
240 | ((uint64_t)(pmi) << PML4SHIFT) | \ | |
241 | ((uint64_t)(pdpi) << PDPTSHIFT) | \ | |
242 | ((uint64_t)(pdi) << PDESHIFT) | \ | |
243 | ((uint64_t)(pti) << PTESHIFT)) | |
0c530ab8 | 244 | |
1c79356b | 245 | /* |
91447636 A |
246 | * Size of Kernel address space. This is the number of page table pages |
247 | * (4MB each) to use for the kernel. 256 pages == 1 Gigabyte. | |
248 | * This **MUST** be a multiple of 4 (eg: 252, 256, 260, etc). | |
1c79356b | 249 | */ |
91447636 | 250 | #ifndef KVA_PAGES |
0c530ab8 | 251 | #define KVA_PAGES 1024 |
91447636 | 252 | #endif |
1c79356b | 253 | |
91447636 | 254 | #ifndef NKPT |
91447636 | 255 | #define NKPT 500 /* actual number of kernel page tables */ |
91447636 A |
256 | #endif |
257 | #ifndef NKPDE | |
258 | #define NKPDE (KVA_PAGES - 1) /* addressable number of page tables/pde's */ | |
259 | #endif | |
260 | ||
0c530ab8 | 261 | |
0c530ab8 | 262 | |
1c79356b A |
263 | /* |
264 | * Convert address offset to page descriptor index | |
265 | */ | |
b0d623f7 A |
266 | #define pdptnum(pmap, a) (((vm_offset_t)(a) >> PDPTSHIFT) & PDPTMASK) |
267 | #define pdenum(pmap, a) (((vm_offset_t)(a) >> PDESHIFT) & PDEMASK) | |
268 | #define PMAP_INVALID_PDPTNUM (~0ULL) | |
91447636 | 269 | |
0c530ab8 A |
270 | #define pdeidx(pmap, a) (((a) >> PDSHIFT) & ((1ULL<<(48 - PDSHIFT)) -1)) |
271 | #define pdptidx(pmap, a) (((a) >> PDPTSHIFT) & ((1ULL<<(48 - PDPTSHIFT)) -1)) | |
272 | #define pml4idx(pmap, a) (((a) >> PML4SHIFT) & ((1ULL<<(48 - PML4SHIFT)) -1)) | |
6d2010ae | 273 | |
1c79356b A |
274 | |
275 | /* | |
276 | * Convert page descriptor index to user virtual address | |
277 | */ | |
278 | #define pdetova(a) ((vm_offset_t)(a) << PDESHIFT) | |
279 | ||
280 | /* | |
281 | * Convert address offset to page table index | |
282 | */ | |
0c530ab8 | 283 | #define ptenum(a) (((vm_offset_t)(a) >> PTESHIFT) & PTEMASK) |
1c79356b | 284 | |
1c79356b A |
285 | /* |
286 | * Hardware pte bit definitions (to be used directly on the ptes | |
287 | * without using the bit fields). | |
288 | */ | |
289 | ||
316670eb A |
290 | #define INTEL_PTE_VALID 0x00000001ULL |
291 | #define INTEL_PTE_WRITE 0x00000002ULL | |
292 | #define INTEL_PTE_RW 0x00000002ULL | |
293 | #define INTEL_PTE_USER 0x00000004ULL | |
294 | #define INTEL_PTE_WTHRU 0x00000008ULL | |
295 | #define INTEL_PTE_NCACHE 0x00000010ULL | |
296 | #define INTEL_PTE_REF 0x00000020ULL | |
297 | #define INTEL_PTE_MOD 0x00000040ULL | |
298 | #define INTEL_PTE_PS 0x00000080ULL | |
299 | #define INTEL_PTE_PTA 0x00000080ULL | |
300 | #define INTEL_PTE_GLOBAL 0x00000100ULL | |
301 | #define INTEL_PTE_WIRED 0x00000200ULL | |
302 | #define INTEL_PDPTE_NESTED 0x00000400ULL | |
0c530ab8 | 303 | #define INTEL_PTE_PFN PG_FRAME |
1c79356b | 304 | |
0c530ab8 A |
305 | #define INTEL_PTE_NX (1ULL << 63) |
306 | ||
307 | #define INTEL_PTE_INVALID 0 | |
b7266188 | 308 | /* This is conservative, but suffices */ |
6d2010ae A |
309 | #define INTEL_PTE_RSVD ((1ULL << 10) | (1ULL << 11) | (0x1FFULL << 54)) |
310 | ||
39236c6e A |
311 | #define INTEL_PTE_COMPRESSED INTEL_PTE_REF /* marker, for invalid PTE only */ |
312 | ||
91447636 A |
313 | #define pa_to_pte(a) ((a) & INTEL_PTE_PFN) /* XXX */ |
314 | #define pte_to_pa(p) ((p) & INTEL_PTE_PFN) /* XXX */ | |
1c79356b A |
315 | #define pte_increment_pa(p) ((p) += INTEL_OFFMASK+1) |
316 | ||
0c530ab8 A |
317 | #define pte_kernel_rw(p) ((pt_entry_t)(pa_to_pte(p) | INTEL_PTE_VALID|INTEL_PTE_RW)) |
318 | #define pte_kernel_ro(p) ((pt_entry_t)(pa_to_pte(p) | INTEL_PTE_VALID)) | |
319 | #define pte_user_rw(p) ((pt_entry)t)(pa_to_pte(p) | INTEL_PTE_VALID|INTEL_PTE_USER|INTEL_PTE_RW)) | |
320 | #define pte_user_ro(p) ((pt_entry_t)(pa_to_pte(p) | INTEL_PTE_VALID|INTEL_PTE_USER)) | |
321 | ||
9bccf70c A |
322 | #define PMAP_DEFAULT_CACHE 0 |
323 | #define PMAP_INHIBIT_CACHE 1 | |
324 | #define PMAP_GUARDED_CACHE 2 | |
325 | #define PMAP_ACTIVATE_CACHE 4 | |
326 | #define PMAP_NO_GUARD_CACHE 8 | |
327 | ||
328 | ||
91447636 A |
329 | #ifndef ASSEMBLER |
330 | ||
331 | #include <sys/queue.h> | |
332 | ||
1c79356b | 333 | /* |
91447636 A |
334 | * Address of current and alternate address space page table maps |
335 | * and directories. | |
1c79356b | 336 | */ |
1c79356b | 337 | |
b0d623f7 | 338 | extern pt_entry_t *PTmap; |
316670eb A |
339 | extern pdpt_entry_t *IdlePDPT; |
340 | extern pml4_entry_t *IdlePML4; | |
b0d623f7 A |
341 | extern boolean_t no_shared_cr3; |
342 | extern addr64_t kernel64_cr3; | |
343 | extern pd_entry_t *IdlePTD; /* physical addr of "Idle" state PTD */ | |
b0d623f7 A |
344 | |
345 | extern uint64_t pmap_pv_hashlist_walks; | |
346 | extern uint64_t pmap_pv_hashlist_cnts; | |
347 | extern uint32_t pmap_pv_hashlist_max; | |
348 | extern uint32_t pmap_kernel_text_ps; | |
349 | ||
b0d623f7 | 350 | |
316670eb | 351 | |
b0d623f7 A |
352 | #ifdef __x86_64__ |
353 | #define ID_MAP_VTOP(x) ((void *)(((uint64_t)(x)) & LOW_4GB_MASK)) | |
91447636 | 354 | |
316670eb A |
355 | extern uint64_t physmap_base, physmap_max; |
356 | ||
7ddcb079 | 357 | #define NPHYSMAP (MAX(K64_MAXMEM/GB + 4, 4)) |
7ddcb079 A |
358 | |
359 | static inline boolean_t physmap_enclosed(addr64_t a) { | |
360 | return (a < (NPHYSMAP * GB)); | |
361 | } | |
316670eb A |
362 | |
363 | static inline void * PHYSMAP_PTOV_check(void *paddr) { | |
364 | uint64_t pvaddr = (uint64_t)paddr + physmap_base; | |
365 | ||
366 | if (__improbable(pvaddr >= physmap_max)) | |
367 | panic("PHYSMAP_PTOV bounds exceeded, 0x%qx, 0x%qx, 0x%qx", | |
368 | pvaddr, physmap_base, physmap_max); | |
369 | ||
370 | return (void *)pvaddr; | |
371 | } | |
372 | ||
373 | #define PHYSMAP_PTOV(x) (PHYSMAP_PTOV_check((void*) (x))) | |
374 | ||
375 | /* | |
376 | * For KASLR, we alias the master processor's IDT and GDT at fixed | |
377 | * virtual addresses to defeat SIDT/SGDT address leakage. | |
378 | */ | |
379 | #define MASTER_IDT_ALIAS (VM_MIN_KERNEL_ADDRESS + 0x0000) | |
380 | #define MASTER_GDT_ALIAS (VM_MIN_KERNEL_ADDRESS + 0x1000) | |
381 | ||
382 | /* | |
383 | * The low global vector page is mapped at a fixed alias also. | |
384 | */ | |
385 | #define LOWGLOBAL_ALIAS (VM_MIN_KERNEL_ADDRESS + 0x2000) | |
386 | ||
387 | #endif /*__x86_64__ */ | |
91447636 | 388 | |
1c79356b A |
389 | typedef volatile long cpu_set; /* set of CPUs - must be <= 32 */ |
390 | /* changed by other processors */ | |
91447636 A |
391 | #include <vm/vm_page.h> |
392 | ||
393 | /* | |
394 | * For each vm_page_t, there is a list of all currently | |
395 | * valid virtual mappings of that page. An entry is | |
396 | * a pv_entry_t; the list is the pv_table. | |
397 | */ | |
1c79356b A |
398 | |
399 | struct pmap { | |
6d2010ae A |
400 | decl_simple_lock_data(,lock) /* lock on map */ |
401 | pmap_paddr_t pm_cr3; /* physical addr */ | |
402 | boolean_t pm_shared; | |
0c530ab8 | 403 | pd_entry_t *dirbase; /* page directory pointer */ |
0c530ab8 | 404 | vm_object_t pm_obj; /* object to hold pde's */ |
2d21ac55 | 405 | task_map_t pm_task_map; |
0c530ab8 A |
406 | pdpt_entry_t *pm_pdpt; /* KVA of 3rd level page */ |
407 | pml4_entry_t *pm_pml4; /* VKA of top level */ | |
408 | vm_object_t pm_obj_pdpt; /* holds pdpt pages */ | |
409 | vm_object_t pm_obj_pml4; /* holds pml4 pages */ | |
6d2010ae A |
410 | #define PMAP_PCID_MAX_CPUS (48) /* Must be a multiple of 8 */ |
411 | pcid_t pmap_pcid_cpus[PMAP_PCID_MAX_CPUS]; | |
412 | volatile uint8_t pmap_pcid_coherency_vector[PMAP_PCID_MAX_CPUS]; | |
413 | struct pmap_statistics stats; /* map statistics */ | |
414 | int ref_count; /* reference count */ | |
415 | int nx_enabled; | |
316670eb | 416 | ledger_t ledger; /* ledger tracking phys mappings */ |
1c79356b A |
417 | }; |
418 | ||
0c530ab8 | 419 | |
b0d623f7 | 420 | #if NCOPY_WINDOWS > 0 |
0c530ab8 A |
421 | #define PMAP_PDPT_FIRST_WINDOW 0 |
422 | #define PMAP_PDPT_NWINDOWS 4 | |
423 | #define PMAP_PDE_FIRST_WINDOW (PMAP_PDPT_NWINDOWS) | |
424 | #define PMAP_PDE_NWINDOWS 4 | |
425 | #define PMAP_PTE_FIRST_WINDOW (PMAP_PDE_FIRST_WINDOW + PMAP_PDE_NWINDOWS) | |
426 | #define PMAP_PTE_NWINDOWS 4 | |
427 | ||
428 | #define PMAP_NWINDOWS_FIRSTFREE (PMAP_PTE_FIRST_WINDOW + PMAP_PTE_NWINDOWS) | |
429 | #define PMAP_WINDOW_SIZE 8 | |
430 | #define PMAP_NWINDOWS (PMAP_NWINDOWS_FIRSTFREE + PMAP_WINDOW_SIZE) | |
431 | ||
91447636 A |
432 | typedef struct { |
433 | pt_entry_t *prv_CMAP; | |
434 | caddr_t prv_CADDR; | |
435 | } mapwindow_t; | |
436 | ||
437 | typedef struct cpu_pmap { | |
0c530ab8 A |
438 | int pdpt_window_index; |
439 | int pde_window_index; | |
440 | int pte_window_index; | |
91447636 | 441 | mapwindow_t mapwindow[PMAP_NWINDOWS]; |
91447636 A |
442 | } cpu_pmap_t; |
443 | ||
0c530ab8 A |
444 | |
445 | extern mapwindow_t *pmap_get_mapwindow(pt_entry_t pentry); | |
2d21ac55 | 446 | extern void pmap_put_mapwindow(mapwindow_t *map); |
b0d623f7 | 447 | #endif |
91447636 A |
448 | |
449 | typedef struct pmap_memory_regions { | |
39236c6e A |
450 | ppnum_t base; /* first page of this region */ |
451 | ppnum_t alloc_up; /* pages below this one have been "stolen" */ | |
452 | ppnum_t alloc_down; /* pages above this one have been "stolen" */ | |
453 | ppnum_t end; /* last page of this region */ | |
7ddcb079 A |
454 | uint32_t type; |
455 | uint64_t attribute; | |
91447636 A |
456 | } pmap_memory_region_t; |
457 | ||
b0d623f7 A |
458 | extern unsigned pmap_memory_region_count; |
459 | extern unsigned pmap_memory_region_current; | |
91447636 | 460 | |
0c530ab8 | 461 | #define PMAP_MEMORY_REGIONS_SIZE 128 |
91447636 A |
462 | |
463 | extern pmap_memory_region_t pmap_memory_regions[]; | |
6d2010ae | 464 | #include <i386/pmap_pcid.h> |
91447636 | 465 | |
b0d623f7 A |
466 | static inline void |
467 | set_dirbase(pmap_t tpmap, __unused thread_t thread) { | |
6d2010ae A |
468 | int ccpu = cpu_number(); |
469 | cpu_datap(ccpu)->cpu_task_cr3 = tpmap->pm_cr3; | |
470 | cpu_datap(ccpu)->cpu_task_map = tpmap->pm_task_map; | |
b0d623f7 A |
471 | /* |
472 | * Switch cr3 if necessary | |
473 | * - unless running with no_shared_cr3 debugging mode | |
474 | * and we're not on the kernel's cr3 (after pre-empted copyio) | |
475 | */ | |
6d2010ae A |
476 | if (__probable(!no_shared_cr3)) { |
477 | if (get_cr3_base() != tpmap->pm_cr3) { | |
478 | if (pmap_pcid_ncpus) { | |
479 | pmap_pcid_activate(tpmap, ccpu); | |
480 | } | |
481 | else | |
482 | set_cr3_raw(tpmap->pm_cr3); | |
483 | } | |
b0d623f7 | 484 | } else { |
6d2010ae A |
485 | if (get_cr3_base() != cpu_datap(ccpu)->cpu_kernel_cr3) |
486 | set_cr3_raw(cpu_datap(ccpu)->cpu_kernel_cr3); | |
b0d623f7 | 487 | } |
1c79356b A |
488 | } |
489 | ||
1c79356b A |
490 | /* |
491 | * External declarations for PMAP_ACTIVATE. | |
492 | */ | |
493 | ||
0c530ab8 | 494 | extern void process_pmap_updates(void); |
1c79356b | 495 | extern void pmap_update_interrupt(void); |
1c79356b A |
496 | |
497 | /* | |
498 | * Machine dependent routines that are used only for i386/i486/i860. | |
499 | */ | |
1c79356b | 500 | |
0c530ab8 | 501 | extern addr64_t (kvtophys)( |
1c79356b A |
502 | vm_offset_t addr); |
503 | ||
316670eb | 504 | extern kern_return_t pmap_expand( |
2d21ac55 | 505 | pmap_t pmap, |
316670eb A |
506 | vm_map_offset_t addr, |
507 | unsigned int options); | |
6d2010ae | 508 | #if !defined(__x86_64__) |
1c79356b A |
509 | extern pt_entry_t *pmap_pte( |
510 | struct pmap *pmap, | |
0c530ab8 A |
511 | vm_map_offset_t addr); |
512 | ||
513 | extern pd_entry_t *pmap_pde( | |
514 | struct pmap *pmap, | |
515 | vm_map_offset_t addr); | |
516 | ||
517 | extern pd_entry_t *pmap64_pde( | |
518 | struct pmap *pmap, | |
519 | vm_map_offset_t addr); | |
520 | ||
521 | extern pdpt_entry_t *pmap64_pdpt( | |
522 | struct pmap *pmap, | |
523 | vm_map_offset_t addr); | |
6d2010ae | 524 | #endif |
1c79356b A |
525 | extern vm_offset_t pmap_map( |
526 | vm_offset_t virt, | |
0c530ab8 A |
527 | vm_map_offset_t start, |
528 | vm_map_offset_t end, | |
529 | vm_prot_t prot, | |
530 | unsigned int flags); | |
1c79356b A |
531 | |
532 | extern vm_offset_t pmap_map_bd( | |
533 | vm_offset_t virt, | |
0c530ab8 A |
534 | vm_map_offset_t start, |
535 | vm_map_offset_t end, | |
536 | vm_prot_t prot, | |
537 | unsigned int flags); | |
1c79356b A |
538 | |
539 | extern void pmap_bootstrap( | |
0c530ab8 A |
540 | vm_offset_t load_start, |
541 | boolean_t IA32e); | |
1c79356b A |
542 | |
543 | extern boolean_t pmap_valid_page( | |
91447636 | 544 | ppnum_t pn); |
1c79356b A |
545 | |
546 | extern int pmap_list_resident_pages( | |
547 | struct pmap *pmap, | |
548 | vm_offset_t *listp, | |
549 | int space); | |
060df5ea | 550 | extern void x86_filter_TLB_coherency_interrupts(boolean_t); |
6d2010ae A |
551 | /* |
552 | * Get cache attributes (as pagetable bits) for the specified phys page | |
553 | */ | |
554 | extern unsigned pmap_get_cache_attributes(ppnum_t); | |
b0d623f7 | 555 | #if NCOPY_WINDOWS > 0 |
91447636 A |
556 | extern struct cpu_pmap *pmap_cpu_alloc( |
557 | boolean_t is_boot_cpu); | |
558 | extern void pmap_cpu_free( | |
559 | struct cpu_pmap *cp); | |
b0d623f7 | 560 | #endif |
0c530ab8 A |
561 | |
562 | extern void pmap_map_block( | |
563 | pmap_t pmap, | |
564 | addr64_t va, | |
565 | ppnum_t pa, | |
566 | uint32_t size, | |
567 | vm_prot_t prot, | |
568 | int attr, | |
569 | unsigned int flags); | |
91447636 | 570 | |
1c79356b A |
571 | extern void invalidate_icache(vm_offset_t addr, unsigned cnt, int phys); |
572 | extern void flush_dcache(vm_offset_t addr, unsigned count, int phys); | |
55e303ae | 573 | extern ppnum_t pmap_find_phys(pmap_t map, addr64_t va); |
1c79356b | 574 | |
2d21ac55 | 575 | extern void pmap_cpu_init(void); |
0c530ab8 | 576 | extern void pmap_disable_NX(pmap_t pmap); |
0c530ab8 | 577 | |
6d2010ae A |
578 | extern void pt_fake_zone_init(int); |
579 | extern void pt_fake_zone_info(int *, vm_size_t *, vm_size_t *, vm_size_t *, vm_size_t *, | |
580 | uint64_t *, int *, int *, int *); | |
b7266188 | 581 | extern void pmap_pagetable_corruption_msg_log(int (*)(const char * fmt, ...)__printflike(1,2)); |
0c530ab8 | 582 | |
1c79356b A |
583 | /* |
584 | * Macros for speed. | |
585 | */ | |
586 | ||
1c79356b A |
587 | |
588 | #include <kern/spl.h> | |
589 | ||
b0d623f7 A |
590 | |
591 | #define PMAP_ACTIVATE_MAP(map, thread) { \ | |
55e303ae | 592 | register pmap_t tpmap; \ |
0c530ab8 A |
593 | \ |
594 | tpmap = vm_map_pmap(map); \ | |
b0d623f7 | 595 | set_dirbase(tpmap, thread); \ |
1c79356b A |
596 | } |
597 | ||
39236c6e | 598 | #if defined(__x86_64__) |
6d2010ae A |
599 | #define PMAP_DEACTIVATE_MAP(map, thread) \ |
600 | pmap_assert(pmap_pcid_ncpus ? (pcid_for_pmap_cpu_tuple(map->pmap, cpu_number()) == (get_cr3_raw() & 0xFFF)) : TRUE); | |
b0d623f7 | 601 | #else |
6d2010ae | 602 | #define PMAP_DEACTIVATE_MAP(map, thread) |
b0d623f7 | 603 | #endif |
1c79356b | 604 | |
b0d623f7 | 605 | #define PMAP_SWITCH_CONTEXT(old_th, new_th, my_cpu) { \ |
b0d623f7 | 606 | \ |
6d2010ae | 607 | pmap_assert(ml_get_interrupts_enabled() == FALSE); \ |
b0d623f7 A |
608 | if (old_th->map != new_th->map) { \ |
609 | PMAP_DEACTIVATE_MAP(old_th->map, old_th); \ | |
610 | PMAP_ACTIVATE_MAP(new_th->map, new_th); \ | |
611 | } \ | |
b0d623f7 | 612 | } |
b0d623f7 | 613 | |
6d2010ae | 614 | #if NCOPY_WINDOWS > 0 |
1c79356b A |
615 | #define PMAP_SWITCH_USER(th, new_map, my_cpu) { \ |
616 | spl_t spl; \ | |
617 | \ | |
0c530ab8 | 618 | spl = splhigh(); \ |
b0d623f7 | 619 | PMAP_DEACTIVATE_MAP(th->map, th); \ |
1c79356b | 620 | th->map = new_map; \ |
b0d623f7 | 621 | PMAP_ACTIVATE_MAP(th->map, th); \ |
1c79356b | 622 | splx(spl); \ |
6d2010ae | 623 | inval_copy_windows(th); \ |
1c79356b | 624 | } |
b0d623f7 A |
625 | #else |
626 | #define PMAP_SWITCH_USER(th, new_map, my_cpu) { \ | |
627 | spl_t spl; \ | |
628 | \ | |
629 | spl = splhigh(); \ | |
630 | PMAP_DEACTIVATE_MAP(th->map, th); \ | |
631 | th->map = new_map; \ | |
632 | PMAP_ACTIVATE_MAP(th->map, th); \ | |
633 | splx(spl); \ | |
634 | } | |
635 | #endif | |
1c79356b | 636 | |
0c530ab8 A |
637 | /* |
638 | * Marking the current cpu's cr3 inactive is achieved by setting its lsb. | |
639 | * Marking the current cpu's cr3 active once more involves clearng this bit. | |
640 | * Note that valid page tables are page-aligned and so the bottom 12 bits | |
6d2010ae | 641 | * are normally zero, modulo PCID. |
0c530ab8 A |
642 | * We can only mark the current cpu active/inactive but we can test any cpu. |
643 | */ | |
644 | #define CPU_CR3_MARK_INACTIVE() \ | |
645 | current_cpu_datap()->cpu_active_cr3 |= 1 | |
646 | ||
647 | #define CPU_CR3_MARK_ACTIVE() \ | |
648 | current_cpu_datap()->cpu_active_cr3 &= ~1 | |
649 | ||
650 | #define CPU_CR3_IS_ACTIVE(cpu) \ | |
651 | ((cpu_datap(cpu)->cpu_active_cr3 & 1) == 0) | |
652 | ||
2d21ac55 A |
653 | #define CPU_GET_ACTIVE_CR3(cpu) \ |
654 | (cpu_datap(cpu)->cpu_active_cr3 & ~1) | |
0c530ab8 | 655 | |
b0d623f7 A |
656 | #define CPU_GET_TASK_CR3(cpu) \ |
657 | (cpu_datap(cpu)->cpu_task_cr3) | |
658 | ||
659 | /* | |
660 | * Mark this cpu idle, and remove it from the active set, | |
661 | * since it is not actively using any pmap. Signal_cpus | |
662 | * will notice that it is idle, and avoid signaling it, | |
663 | * but will queue the update request for when the cpu | |
664 | * becomes active. | |
665 | */ | |
b0d623f7 | 666 | #define MARK_CPU_IDLE(my_cpu) { \ |
6d2010ae | 667 | assert(ml_get_interrupts_enabled() == FALSE); \ |
b0d623f7 | 668 | CPU_CR3_MARK_INACTIVE(); \ |
39236c6e | 669 | mfence(); \ |
1c79356b A |
670 | } |
671 | ||
0c530ab8 | 672 | #define MARK_CPU_ACTIVE(my_cpu) { \ |
6d2010ae | 673 | assert(ml_get_interrupts_enabled() == FALSE); \ |
1c79356b A |
674 | /* \ |
675 | * If a kernel_pmap update was requested while this cpu \ | |
676 | * was idle, process it as if we got the interrupt. \ | |
677 | * Before doing so, remove this cpu from the idle set. \ | |
678 | * Since we do not grab any pmap locks while we flush \ | |
679 | * our TLB, another cpu may start an update operation \ | |
680 | * before we finish. Removing this cpu from the idle \ | |
681 | * set assures that we will receive another update \ | |
682 | * interrupt if this happens. \ | |
683 | */ \ | |
0c530ab8 | 684 | CPU_CR3_MARK_ACTIVE(); \ |
39236c6e | 685 | mfence(); \ |
55e303ae | 686 | \ |
0c530ab8 A |
687 | if (current_cpu_datap()->cpu_tlb_invalid) \ |
688 | process_pmap_updates(); \ | |
1c79356b A |
689 | } |
690 | ||
1c79356b A |
691 | #define PMAP_CONTEXT(pmap, thread) |
692 | ||
693 | #define pmap_kernel_va(VA) \ | |
0c530ab8 A |
694 | ((((vm_offset_t) (VA)) >= vm_min_kernel_address) && \ |
695 | (((vm_offset_t) (VA)) <= vm_max_kernel_address)) | |
696 | ||
1c79356b A |
697 | |
698 | #define pmap_resident_count(pmap) ((pmap)->stats.resident_count) | |
2d21ac55 | 699 | #define pmap_resident_max(pmap) ((pmap)->stats.resident_max) |
1c79356b A |
700 | #define pmap_copy(dst_pmap,src_pmap,dst_addr,len,src_addr) |
701 | #define pmap_attribute(pmap,addr,size,attr,value) \ | |
702 | (KERN_INVALID_ADDRESS) | |
9bccf70c A |
703 | #define pmap_attribute_cache_sync(addr,size,attr,value) \ |
704 | (KERN_INVALID_ADDRESS) | |
765c9de3 | 705 | |
6d2010ae | 706 | #define MACHINE_PMAP_IS_EMPTY 1 |
2d21ac55 A |
707 | extern boolean_t pmap_is_empty(pmap_t pmap, |
708 | vm_map_offset_t start, | |
709 | vm_map_offset_t end); | |
710 | ||
6d2010ae A |
711 | #define MACHINE_BOOTSTRAPPTD 1 /* Static bootstrap page-tables */ |
712 | ||
316670eb A |
713 | kern_return_t |
714 | pmap_permissions_verify(pmap_t, vm_map_t, vm_offset_t, vm_offset_t); | |
b0d623f7 | 715 | |
1c79356b A |
716 | #endif /* ASSEMBLER */ |
717 | ||
0c530ab8 | 718 | |
1c79356b | 719 | #endif /* _PMAP_MACHINE_ */ |
0c530ab8 A |
720 | |
721 | ||
722 | #endif /* KERNEL_PRIVATE */ |