]> git.saurik.com Git - apple/xnu.git/blob - osfmk/x86_64/pmap.c
51b1b3348485e964ae2c34acffa21e73e9a5787a
[apple/xnu.git] / osfmk / x86_64 / pmap.c
1 /*
2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.c
61 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * (These guys wrote the Vax version)
63 *
64 * Physical Map management code for Intel i386, i486, and i860.
65 *
66 * Manages physical address maps.
67 *
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.
74 *
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
80 * requested.
81 *
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.
89 */
90
91 #include <string.h>
92 #include <mach_ldebug.h>
93
94 #include <libkern/OSAtomic.h>
95
96 #include <mach/machine/vm_types.h>
97
98 #include <mach/boolean.h>
99 #include <kern/thread.h>
100 #include <kern/zalloc.h>
101 #include <kern/queue.h>
102 #include <kern/ledger.h>
103 #include <kern/mach_param.h>
104
105 #include <kern/kalloc.h>
106 #include <kern/spl.h>
107
108 #include <vm/pmap.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>
115
116 #include <mach/machine/vm_param.h>
117 #include <machine/thread.h>
118
119 #include <kern/misc_protos.h> /* prototyping */
120 #include <i386/misc_protos.h>
121 #include <i386/i386_lowmem.h>
122 #include <x86_64/lowglobals.h>
123
124 #include <i386/cpuid.h>
125 #include <i386/cpu_data.h>
126 #include <i386/cpu_number.h>
127 #include <i386/machine_cpu.h>
128 #include <i386/seg.h>
129 #include <i386/serial_io.h>
130 #include <i386/cpu_capabilities.h>
131 #include <i386/machine_routines.h>
132 #include <i386/proc_reg.h>
133 #include <i386/tsc.h>
134 #include <i386/pmap_internal.h>
135 #include <i386/pmap_pcid.h>
136
137 #include <vm/vm_protos.h>
138
139 #include <i386/mp.h>
140 #include <i386/mp_desc.h>
141 #include <libkern/kernel_mach_header.h>
142
143 #include <pexpert/i386/efi.h>
144
145
146 #ifdef IWANTTODEBUG
147 #undef DEBUG
148 #define DEBUG 1
149 #define POSTCODE_DELAY 1
150 #include <i386/postcode.h>
151 #endif /* IWANTTODEBUG */
152
153 #ifdef PMAP_DEBUG
154 #define DBG(x...) kprintf("DBG: " x)
155 #else
156 #define DBG(x...)
157 #endif
158 /* Compile time assert to ensure adjacency/alignment of per-CPU data fields used
159 * in the trampolines for kernel/user boundary TLB coherency.
160 */
161 char pmap_cpu_data_assert[(((offsetof(cpu_data_t, cpu_tlb_invalid) - offsetof(cpu_data_t, cpu_active_cr3)) == 8) && (offsetof(cpu_data_t, cpu_active_cr3) % 64 == 0)) ? 1 : -1];
162 boolean_t pmap_trace = FALSE;
163
164 boolean_t no_shared_cr3 = DEBUG; /* TRUE for DEBUG by default */
165
166 int nx_enabled = 1; /* enable no-execute protection */
167 int allow_data_exec = VM_ABI_32; /* 32-bit apps may execute data by default, 64-bit apps may not */
168 int allow_stack_exec = 0; /* No apps may execute from the stack by default */
169
170 const boolean_t cpu_64bit = TRUE; /* Mais oui! */
171
172 uint64_t max_preemption_latency_tsc = 0;
173
174 pv_hashed_entry_t *pv_hash_table; /* hash lists */
175
176 uint32_t npvhashmask = 0, npvhashbuckets = 0;
177
178 pv_hashed_entry_t pv_hashed_free_list = PV_HASHED_ENTRY_NULL;
179 pv_hashed_entry_t pv_hashed_kern_free_list = PV_HASHED_ENTRY_NULL;
180 decl_simple_lock_data(,pv_hashed_free_list_lock)
181 decl_simple_lock_data(,pv_hashed_kern_free_list_lock)
182 decl_simple_lock_data(,pv_hash_table_lock)
183
184 decl_simple_lock_data(,phys_backup_lock)
185
186 zone_t pv_hashed_list_zone; /* zone of pv_hashed_entry structures */
187
188 /*
189 * First and last physical addresses that we maintain any information
190 * for. Initialized to zero so that pmap operations done before
191 * pmap_init won't touch any non-existent structures.
192 */
193 boolean_t pmap_initialized = FALSE;/* Has pmap_init completed? */
194
195 static struct vm_object kptobj_object_store;
196 static struct vm_object kpml4obj_object_store;
197 static struct vm_object kpdptobj_object_store;
198
199 /*
200 * Array of physical page attribites for managed pages.
201 * One byte per physical page.
202 */
203 char *pmap_phys_attributes;
204 ppnum_t last_managed_page = 0;
205
206 /*
207 * Amount of virtual memory mapped by one
208 * page-directory entry.
209 */
210
211 uint64_t pde_mapped_size = PDE_MAPPED_SIZE;
212
213 unsigned pmap_memory_region_count;
214 unsigned pmap_memory_region_current;
215
216 pmap_memory_region_t pmap_memory_regions[PMAP_MEMORY_REGIONS_SIZE];
217
218 /*
219 * Other useful macros.
220 */
221 #define current_pmap() (vm_map_pmap(current_thread()->map))
222
223 struct pmap kernel_pmap_store;
224 pmap_t kernel_pmap;
225
226 struct zone *pmap_zone; /* zone of pmap structures */
227
228 struct zone *pmap_anchor_zone;
229 int pmap_debug = 0; /* flag for debugging prints */
230
231 unsigned int inuse_ptepages_count = 0;
232 long long alloc_ptepages_count __attribute__((aligned(8))) = 0; /* aligned for atomic access */
233 unsigned int bootstrap_wired_pages = 0;
234 int pt_fake_zone_index = -1;
235
236 extern long NMIPI_acks;
237
238 boolean_t kernel_text_ps_4K = TRUE;
239 boolean_t wpkernel = TRUE;
240
241 extern char end;
242
243 static int nkpt;
244
245 pt_entry_t *DMAP1, *DMAP2;
246 caddr_t DADDR1;
247 caddr_t DADDR2;
248
249 const boolean_t pmap_disable_kheap_nx = FALSE;
250 const boolean_t pmap_disable_kstack_nx = FALSE;
251 extern boolean_t doconstro_override;
252
253 extern long __stack_chk_guard[];
254
255 /*
256 * Map memory at initialization. The physical addresses being
257 * mapped are not managed and are never unmapped.
258 *
259 * For now, VM is already on, we only need to map the
260 * specified memory.
261 */
262 vm_offset_t
263 pmap_map(
264 vm_offset_t virt,
265 vm_map_offset_t start_addr,
266 vm_map_offset_t end_addr,
267 vm_prot_t prot,
268 unsigned int flags)
269 {
270 int ps;
271
272 ps = PAGE_SIZE;
273 while (start_addr < end_addr) {
274 pmap_enter(kernel_pmap, (vm_map_offset_t)virt,
275 (ppnum_t) i386_btop(start_addr), prot, VM_PROT_NONE, flags, TRUE);
276 virt += ps;
277 start_addr += ps;
278 }
279 return(virt);
280 }
281
282 extern char *first_avail;
283 extern vm_offset_t virtual_avail, virtual_end;
284 extern pmap_paddr_t avail_start, avail_end;
285 extern vm_offset_t sHIB;
286 extern vm_offset_t eHIB;
287 extern vm_offset_t stext;
288 extern vm_offset_t etext;
289 extern vm_offset_t sdata, edata;
290 extern vm_offset_t sconstdata, econstdata;
291
292 extern void *KPTphys;
293
294 boolean_t pmap_smep_enabled = FALSE;
295 boolean_t pmap_smap_enabled = FALSE;
296
297 void
298 pmap_cpu_init(void)
299 {
300 cpu_data_t *cdp = current_cpu_datap();
301 /*
302 * Here early in the life of a processor (from cpu_mode_init()).
303 * Ensure global page feature is disabled at this point.
304 */
305
306 set_cr4(get_cr4() &~ CR4_PGE);
307
308 /*
309 * Initialize the per-cpu, TLB-related fields.
310 */
311 cdp->cpu_kernel_cr3 = kernel_pmap->pm_cr3;
312 cdp->cpu_active_cr3 = kernel_pmap->pm_cr3;
313 cdp->cpu_tlb_invalid = FALSE;
314 cdp->cpu_task_map = TASK_MAP_64BIT;
315 pmap_pcid_configure();
316 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMEP) {
317 boolean_t nsmep;
318 if (!PE_parse_boot_argn("-pmap_smep_disable", &nsmep, sizeof(nsmep))) {
319 set_cr4(get_cr4() | CR4_SMEP);
320 pmap_smep_enabled = TRUE;
321 }
322 }
323 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMAP) {
324 boolean_t nsmap;
325 if (!PE_parse_boot_argn("-pmap_smap_disable", &nsmap, sizeof(nsmap))) {
326 set_cr4(get_cr4() | CR4_SMAP);
327 pmap_smap_enabled = TRUE;
328 }
329 }
330
331 if (cdp->cpu_fixed_pmcs_enabled) {
332 boolean_t enable = TRUE;
333 cpu_pmc_control(&enable);
334 }
335 }
336
337 static uint32_t pmap_scale_shift(void) {
338 uint32_t scale = 0;
339
340 if (sane_size <= 8*GB) {
341 scale = (uint32_t)(sane_size / (2 * GB));
342 } else if (sane_size <= 32*GB) {
343 scale = 4 + (uint32_t)((sane_size - (8 * GB))/ (4 * GB));
344 } else {
345 scale = 10 + (uint32_t)MIN(4, ((sane_size - (32 * GB))/ (8 * GB)));
346 }
347 return scale;
348 }
349
350 /*
351 * Bootstrap the system enough to run with virtual memory.
352 * Map the kernel's code and data, and allocate the system page table.
353 * Called with mapping OFF. Page_size must already be set.
354 */
355
356 void
357 pmap_bootstrap(
358 __unused vm_offset_t load_start,
359 __unused boolean_t IA32e)
360 {
361 #if NCOPY_WINDOWS > 0
362 vm_offset_t va;
363 int i;
364 #endif
365 assert(IA32e);
366
367 vm_last_addr = VM_MAX_KERNEL_ADDRESS; /* Set the highest address
368 * known to VM */
369 /*
370 * The kernel's pmap is statically allocated so we don't
371 * have to use pmap_create, which is unlikely to work
372 * correctly at this part of the boot sequence.
373 */
374
375 kernel_pmap = &kernel_pmap_store;
376 kernel_pmap->ref_count = 1;
377 kernel_pmap->nx_enabled = TRUE;
378 kernel_pmap->pm_task_map = TASK_MAP_64BIT;
379 kernel_pmap->pm_obj = (vm_object_t) NULL;
380 kernel_pmap->dirbase = (pd_entry_t *)((uintptr_t)IdlePTD);
381 kernel_pmap->pm_pdpt = (pd_entry_t *) ((uintptr_t)IdlePDPT);
382 kernel_pmap->pm_pml4 = IdlePML4;
383 kernel_pmap->pm_cr3 = (uintptr_t)ID_MAP_VTOP(IdlePML4);
384 pmap_pcid_initialize_kernel(kernel_pmap);
385
386
387
388 current_cpu_datap()->cpu_kernel_cr3 = (addr64_t) kernel_pmap->pm_cr3;
389
390 nkpt = NKPT;
391 OSAddAtomic(NKPT, &inuse_ptepages_count);
392 OSAddAtomic64(NKPT, &alloc_ptepages_count);
393 bootstrap_wired_pages = NKPT;
394
395 virtual_avail = (vm_offset_t)(VM_MIN_KERNEL_ADDRESS) + (vm_offset_t)first_avail;
396 virtual_end = (vm_offset_t)(VM_MAX_KERNEL_ADDRESS);
397
398 #if NCOPY_WINDOWS > 0
399 /*
400 * Reserve some special page table entries/VA space for temporary
401 * mapping of pages.
402 */
403 #define SYSMAP(c, p, v, n) \
404 v = (c)va; va += ((n)*INTEL_PGBYTES);
405
406 va = virtual_avail;
407
408 for (i=0; i<PMAP_NWINDOWS; i++) {
409 #if 1
410 kprintf("trying to do SYSMAP idx %d %p\n", i,
411 current_cpu_datap());
412 kprintf("cpu_pmap %p\n", current_cpu_datap()->cpu_pmap);
413 kprintf("mapwindow %p\n", current_cpu_datap()->cpu_pmap->mapwindow);
414 kprintf("two stuff %p %p\n",
415 (void *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP),
416 (void *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR));
417 #endif
418 SYSMAP(caddr_t,
419 (current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP),
420 (current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR),
421 1);
422 current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP =
423 &(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP_store);
424 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
425 }
426
427 /* DMAP user for debugger */
428 SYSMAP(caddr_t, DMAP1, DADDR1, 1);
429 SYSMAP(caddr_t, DMAP2, DADDR2, 1); /* XXX temporary - can remove */
430
431 virtual_avail = va;
432 #endif
433 if (!PE_parse_boot_argn("npvhash", &npvhashmask, sizeof (npvhashmask))) {
434 npvhashmask = ((NPVHASHBUCKETS) << pmap_scale_shift()) - 1;
435
436 }
437
438 npvhashbuckets = npvhashmask + 1;
439
440 if (0 != ((npvhashbuckets) & npvhashmask)) {
441 panic("invalid hash %d, must be ((2^N)-1), "
442 "using default %d\n", npvhashmask, NPVHASHMASK);
443 }
444
445 simple_lock_init(&kernel_pmap->lock, 0);
446 simple_lock_init(&pv_hashed_free_list_lock, 0);
447 simple_lock_init(&pv_hashed_kern_free_list_lock, 0);
448 simple_lock_init(&pv_hash_table_lock,0);
449 simple_lock_init(&phys_backup_lock, 0);
450
451 pmap_cpu_init();
452
453 if (pmap_pcid_ncpus)
454 printf("PMAP: PCID enabled\n");
455
456 if (pmap_smep_enabled)
457 printf("PMAP: Supervisor Mode Execute Protection enabled\n");
458 if (pmap_smap_enabled)
459 printf("PMAP: Supervisor Mode Access Protection enabled\n");
460
461 #if DEBUG
462 printf("Stack canary: 0x%lx\n", __stack_chk_guard[0]);
463 printf("early_random(): 0x%qx\n", early_random());
464 #endif
465 boolean_t ptmp;
466 /* Check if the user has requested disabling stack or heap no-execute
467 * enforcement. These are "const" variables; that qualifier is cast away
468 * when altering them. The TEXT/DATA const sections are marked
469 * write protected later in the kernel startup sequence, so altering
470 * them is possible at this point, in pmap_bootstrap().
471 */
472 if (PE_parse_boot_argn("-pmap_disable_kheap_nx", &ptmp, sizeof(ptmp))) {
473 boolean_t *pdknxp = (boolean_t *) &pmap_disable_kheap_nx;
474 *pdknxp = TRUE;
475 }
476
477 if (PE_parse_boot_argn("-pmap_disable_kstack_nx", &ptmp, sizeof(ptmp))) {
478 boolean_t *pdknhp = (boolean_t *) &pmap_disable_kstack_nx;
479 *pdknhp = TRUE;
480 }
481
482 boot_args *args = (boot_args *)PE_state.bootArgs;
483 if (args->efiMode == kBootArgsEfiMode32) {
484 printf("EFI32: kernel virtual space limited to 4GB\n");
485 virtual_end = VM_MAX_KERNEL_ADDRESS_EFI32;
486 }
487 kprintf("Kernel virtual space from 0x%lx to 0x%lx.\n",
488 (long)KERNEL_BASE, (long)virtual_end);
489 kprintf("Available physical space from 0x%llx to 0x%llx\n",
490 avail_start, avail_end);
491
492 /*
493 * The -no_shared_cr3 boot-arg is a debugging feature (set by default
494 * in the DEBUG kernel) to force the kernel to switch to its own map
495 * (and cr3) when control is in kernelspace. The kernel's map does not
496 * include (i.e. share) userspace so wild references will cause
497 * a panic. Only copyin and copyout are exempt from this.
498 */
499 (void) PE_parse_boot_argn("-no_shared_cr3",
500 &no_shared_cr3, sizeof (no_shared_cr3));
501 if (no_shared_cr3)
502 kprintf("Kernel not sharing user map\n");
503
504 #ifdef PMAP_TRACES
505 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace, sizeof (pmap_trace))) {
506 kprintf("Kernel traces for pmap operations enabled\n");
507 }
508 #endif /* PMAP_TRACES */
509 }
510
511 void
512 pmap_virtual_space(
513 vm_offset_t *startp,
514 vm_offset_t *endp)
515 {
516 *startp = virtual_avail;
517 *endp = virtual_end;
518 }
519
520
521
522
523 #if HIBERNATION
524
525 #include <IOKit/IOHibernatePrivate.h>
526
527 int32_t pmap_npages;
528 int32_t pmap_teardown_last_valid_compact_indx = -1;
529
530
531 void hibernate_rebuild_pmap_structs(void);
532 void hibernate_teardown_pmap_structs(addr64_t *, addr64_t *);
533 void pmap_pack_index(uint32_t);
534 int32_t pmap_unpack_index(pv_rooted_entry_t);
535
536
537 int32_t
538 pmap_unpack_index(pv_rooted_entry_t pv_h)
539 {
540 int32_t indx = 0;
541
542 indx = (int32_t)(*((uint64_t *)(&pv_h->qlink.next)) >> 48);
543 indx = indx << 16;
544 indx |= (int32_t)(*((uint64_t *)(&pv_h->qlink.prev)) >> 48);
545
546 *((uint64_t *)(&pv_h->qlink.next)) |= ((uint64_t)0xffff << 48);
547 *((uint64_t *)(&pv_h->qlink.prev)) |= ((uint64_t)0xffff << 48);
548
549 return (indx);
550 }
551
552
553 void
554 pmap_pack_index(uint32_t indx)
555 {
556 pv_rooted_entry_t pv_h;
557
558 pv_h = &pv_head_table[indx];
559
560 *((uint64_t *)(&pv_h->qlink.next)) &= ~((uint64_t)0xffff << 48);
561 *((uint64_t *)(&pv_h->qlink.prev)) &= ~((uint64_t)0xffff << 48);
562
563 *((uint64_t *)(&pv_h->qlink.next)) |= ((uint64_t)(indx >> 16)) << 48;
564 *((uint64_t *)(&pv_h->qlink.prev)) |= ((uint64_t)(indx & 0xffff)) << 48;
565 }
566
567
568 void
569 hibernate_teardown_pmap_structs(addr64_t *unneeded_start, addr64_t *unneeded_end)
570 {
571 int32_t i;
572 int32_t compact_target_indx;
573
574 compact_target_indx = 0;
575
576 for (i = 0; i < pmap_npages; i++) {
577 if (pv_head_table[i].pmap == PMAP_NULL) {
578
579 if (pv_head_table[compact_target_indx].pmap != PMAP_NULL)
580 compact_target_indx = i;
581 } else {
582 pmap_pack_index((uint32_t)i);
583
584 if (pv_head_table[compact_target_indx].pmap == PMAP_NULL) {
585 /*
586 * we've got a hole to fill, so
587 * move this pv_rooted_entry_t to it's new home
588 */
589 pv_head_table[compact_target_indx] = pv_head_table[i];
590 pv_head_table[i].pmap = PMAP_NULL;
591
592 pmap_teardown_last_valid_compact_indx = compact_target_indx;
593 compact_target_indx++;
594 } else
595 pmap_teardown_last_valid_compact_indx = i;
596 }
597 }
598 *unneeded_start = (addr64_t)&pv_head_table[pmap_teardown_last_valid_compact_indx+1];
599 *unneeded_end = (addr64_t)&pv_head_table[pmap_npages-1];
600
601 HIBLOG("hibernate_teardown_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx);
602 }
603
604
605 void
606 hibernate_rebuild_pmap_structs(void)
607 {
608 int32_t cindx, eindx, rindx;
609 pv_rooted_entry_t pv_h;
610
611 eindx = (int32_t)pmap_npages;
612
613 for (cindx = pmap_teardown_last_valid_compact_indx; cindx >= 0; cindx--) {
614
615 pv_h = &pv_head_table[cindx];
616
617 rindx = pmap_unpack_index(pv_h);
618 assert(rindx < pmap_npages);
619
620 if (rindx != cindx) {
621 /*
622 * this pv_rooted_entry_t was moved by hibernate_teardown_pmap_structs,
623 * so move it back to its real location
624 */
625 pv_head_table[rindx] = pv_head_table[cindx];
626 }
627 if (rindx+1 != eindx) {
628 /*
629 * the 'hole' between this vm_rooted_entry_t and the previous
630 * vm_rooted_entry_t we moved needs to be initialized as
631 * a range of zero'd vm_rooted_entry_t's
632 */
633 bzero((char *)&pv_head_table[rindx+1], (eindx - rindx - 1) * sizeof (struct pv_rooted_entry));
634 }
635 eindx = rindx;
636 }
637 if (rindx)
638 bzero ((char *)&pv_head_table[0], rindx * sizeof (struct pv_rooted_entry));
639
640 HIBLOG("hibernate_rebuild_pmap_structs done: last_valid_compact_indx %d\n", pmap_teardown_last_valid_compact_indx);
641 }
642
643 #endif
644
645 /*
646 * Initialize the pmap module.
647 * Called by vm_init, to initialize any structures that the pmap
648 * system needs to map virtual memory.
649 */
650 void
651 pmap_init(void)
652 {
653 long npages;
654 vm_offset_t addr;
655 vm_size_t s, vsize;
656 vm_map_offset_t vaddr;
657 ppnum_t ppn;
658
659
660 kernel_pmap->pm_obj_pml4 = &kpml4obj_object_store;
661 _vm_object_allocate((vm_object_size_t)NPML4PGS * PAGE_SIZE, &kpml4obj_object_store);
662
663 kernel_pmap->pm_obj_pdpt = &kpdptobj_object_store;
664 _vm_object_allocate((vm_object_size_t)NPDPTPGS * PAGE_SIZE, &kpdptobj_object_store);
665
666 kernel_pmap->pm_obj = &kptobj_object_store;
667 _vm_object_allocate((vm_object_size_t)NPDEPGS * PAGE_SIZE, &kptobj_object_store);
668
669 /*
670 * Allocate memory for the pv_head_table and its lock bits,
671 * the modify bit array, and the pte_page table.
672 */
673
674 /*
675 * zero bias all these arrays now instead of off avail_start
676 * so we cover all memory
677 */
678
679 npages = i386_btop(avail_end);
680 #if HIBERNATION
681 pmap_npages = (uint32_t)npages;
682 #endif
683 s = (vm_size_t) (sizeof(struct pv_rooted_entry) * npages
684 + (sizeof (struct pv_hashed_entry_t *) * (npvhashbuckets))
685 + pv_lock_table_size(npages)
686 + pv_hash_lock_table_size((npvhashbuckets))
687 + npages);
688
689 s = round_page(s);
690 if (kernel_memory_allocate(kernel_map, &addr, s, 0,
691 KMA_KOBJECT | KMA_PERMANENT)
692 != KERN_SUCCESS)
693 panic("pmap_init");
694
695 memset((char *)addr, 0, s);
696
697 vaddr = addr;
698 vsize = s;
699
700 #if PV_DEBUG
701 if (0 == npvhashmask) panic("npvhashmask not initialized");
702 #endif
703
704 /*
705 * Allocate the structures first to preserve word-alignment.
706 */
707 pv_head_table = (pv_rooted_entry_t) addr;
708 addr = (vm_offset_t) (pv_head_table + npages);
709
710 pv_hash_table = (pv_hashed_entry_t *)addr;
711 addr = (vm_offset_t) (pv_hash_table + (npvhashbuckets));
712
713 pv_lock_table = (char *) addr;
714 addr = (vm_offset_t) (pv_lock_table + pv_lock_table_size(npages));
715
716 pv_hash_lock_table = (char *) addr;
717 addr = (vm_offset_t) (pv_hash_lock_table + pv_hash_lock_table_size((npvhashbuckets)));
718
719 pmap_phys_attributes = (char *) addr;
720
721 ppnum_t last_pn = i386_btop(avail_end);
722 unsigned int i;
723 pmap_memory_region_t *pmptr = pmap_memory_regions;
724 for (i = 0; i < pmap_memory_region_count; i++, pmptr++) {
725 if (pmptr->type != kEfiConventionalMemory)
726 continue;
727 ppnum_t pn;
728 for (pn = pmptr->base; pn <= pmptr->end; pn++) {
729 if (pn < last_pn) {
730 pmap_phys_attributes[pn] |= PHYS_MANAGED;
731
732 if (pn > last_managed_page)
733 last_managed_page = pn;
734
735 if (pn >= lowest_hi && pn <= highest_hi)
736 pmap_phys_attributes[pn] |= PHYS_NOENCRYPT;
737 }
738 }
739 }
740 while (vsize) {
741 ppn = pmap_find_phys(kernel_pmap, vaddr);
742
743 pmap_phys_attributes[ppn] |= PHYS_NOENCRYPT;
744
745 vaddr += PAGE_SIZE;
746 vsize -= PAGE_SIZE;
747 }
748 /*
749 * Create the zone of physical maps,
750 * and of the physical-to-virtual entries.
751 */
752 s = (vm_size_t) sizeof(struct pmap);
753 pmap_zone = zinit(s, 400*s, 4096, "pmap"); /* XXX */
754 zone_change(pmap_zone, Z_NOENCRYPT, TRUE);
755
756 pmap_anchor_zone = zinit(PAGE_SIZE, task_max, PAGE_SIZE, "pagetable anchors");
757 zone_change(pmap_anchor_zone, Z_NOENCRYPT, TRUE);
758
759 /* The anchor is required to be page aligned. Zone debugging adds
760 * padding which may violate that requirement. Tell the zone
761 * subsystem that alignment is required.
762 */
763
764 zone_change(pmap_anchor_zone, Z_ALIGNMENT_REQUIRED, TRUE);
765
766 s = (vm_size_t) sizeof(struct pv_hashed_entry);
767 pv_hashed_list_zone = zinit(s, 10000*s /* Expandable zone */,
768 4096 * 3 /* LCM x86_64*/, "pv_list");
769 zone_change(pv_hashed_list_zone, Z_NOENCRYPT, TRUE);
770
771 /* create pv entries for kernel pages mapped by low level
772 startup code. these have to exist so we can pmap_remove()
773 e.g. kext pages from the middle of our addr space */
774
775 vaddr = (vm_map_offset_t) VM_MIN_KERNEL_ADDRESS;
776 for (ppn = VM_MIN_KERNEL_PAGE; ppn < i386_btop(avail_start); ppn++) {
777 pv_rooted_entry_t pv_e;
778
779 pv_e = pai_to_pvh(ppn);
780 pv_e->va = vaddr;
781 vaddr += PAGE_SIZE;
782 pv_e->pmap = kernel_pmap;
783 queue_init(&pv_e->qlink);
784 }
785 pmap_initialized = TRUE;
786
787 max_preemption_latency_tsc = tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS, tscFCvtn2t);
788
789 /*
790 * Ensure the kernel's PML4 entry exists for the basement
791 * before this is shared with any user.
792 */
793 pmap_expand_pml4(kernel_pmap, KERNEL_BASEMENT, PMAP_EXPAND_OPTIONS_NONE);
794 }
795
796 static
797 void pmap_mark_range(pmap_t npmap, uint64_t sv, uint64_t nxrosz, boolean_t NX, boolean_t ro) {
798 uint64_t ev = sv + nxrosz, cv = sv;
799 pd_entry_t *pdep;
800 pt_entry_t *ptep = NULL;
801
802 assert(((sv & 0xFFFULL) | (nxrosz & 0xFFFULL)) == 0);
803
804 for (pdep = pmap_pde(npmap, cv); pdep != NULL && (cv < ev);) {
805 uint64_t pdev = (cv & ~((uint64_t)PDEMASK));
806
807 if (*pdep & INTEL_PTE_PS) {
808 if (NX)
809 *pdep |= INTEL_PTE_NX;
810 if (ro)
811 *pdep &= ~INTEL_PTE_WRITE;
812 cv += NBPD;
813 cv &= ~((uint64_t) PDEMASK);
814 pdep = pmap_pde(npmap, cv);
815 continue;
816 }
817
818 for (ptep = pmap_pte(npmap, cv); ptep != NULL && (cv < (pdev + NBPD)) && (cv < ev);) {
819 if (NX)
820 *ptep |= INTEL_PTE_NX;
821 if (ro)
822 *ptep &= ~INTEL_PTE_WRITE;
823 cv += NBPT;
824 ptep = pmap_pte(npmap, cv);
825 }
826 }
827 DPRINTF("%s(0x%llx, 0x%llx, %u, %u): 0x%llx, 0x%llx\n", __FUNCTION__, sv, nxrosz, NX, ro, cv, ptep ? *ptep: 0);
828 }
829
830 /*
831 * Called once VM is fully initialized so that we can release unused
832 * sections of low memory to the general pool.
833 * Also complete the set-up of identity-mapped sections of the kernel:
834 * 1) write-protect kernel text
835 * 2) map kernel text using large pages if possible
836 * 3) read and write-protect page zero (for K32)
837 * 4) map the global page at the appropriate virtual address.
838 *
839 * Use of large pages
840 * ------------------
841 * To effectively map and write-protect all kernel text pages, the text
842 * must be 2M-aligned at the base, and the data section above must also be
843 * 2M-aligned. That is, there's padding below and above. This is achieved
844 * through linker directives. Large pages are used only if this alignment
845 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
846 * memory layout is:
847 *
848 * : :
849 * | __DATA |
850 * sdata: ================== 2Meg
851 * | |
852 * | zero-padding |
853 * | |
854 * etext: ------------------
855 * | |
856 * : :
857 * | |
858 * | __TEXT |
859 * | |
860 * : :
861 * | |
862 * stext: ================== 2Meg
863 * | |
864 * | zero-padding |
865 * | |
866 * eHIB: ------------------
867 * | __HIB |
868 * : :
869 *
870 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
871 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
872 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
873 * The now unused level-1 PTE pages are also freed.
874 */
875 extern ppnum_t vm_kernel_base_page;
876 void
877 pmap_lowmem_finalize(void)
878 {
879 spl_t spl;
880 int i;
881
882 /*
883 * Update wired memory statistics for early boot pages
884 */
885 PMAP_ZINFO_PALLOC(kernel_pmap, bootstrap_wired_pages * PAGE_SIZE);
886
887 /*
888 * Free pages in pmap regions below the base:
889 * rdar://6332712
890 * We can't free all the pages to VM that EFI reports available.
891 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
892 * There's also a size miscalculation here: pend is one page less
893 * than it should be but this is not fixed to be backwards
894 * compatible.
895 * This is important for KASLR because up to 256*2MB = 512MB of space
896 * needs has to be released to VM.
897 */
898 for (i = 0;
899 pmap_memory_regions[i].end < vm_kernel_base_page;
900 i++) {
901 vm_offset_t pbase = i386_ptob(pmap_memory_regions[i].base);
902 vm_offset_t pend = i386_ptob(pmap_memory_regions[i].end+1);
903
904 DBG("pmap region %d [%p..[%p\n",
905 i, (void *) pbase, (void *) pend);
906
907 if (pmap_memory_regions[i].attribute & EFI_MEMORY_KERN_RESERVED)
908 continue;
909 /*
910 * rdar://6332712
911 * Adjust limits not to free pages in range 0xc0000-0xff000.
912 */
913 if (pbase >= 0xc0000 && pend <= 0x100000)
914 continue;
915 if (pbase < 0xc0000 && pend > 0x100000) {
916 /* page range entirely within region, free lower part */
917 DBG("- ml_static_mfree(%p,%p)\n",
918 (void *) ml_static_ptovirt(pbase),
919 (void *) (0xc0000-pbase));
920 ml_static_mfree(ml_static_ptovirt(pbase),0xc0000-pbase);
921 pbase = 0x100000;
922 }
923 if (pbase < 0xc0000)
924 pend = MIN(pend, 0xc0000);
925 if (pend > 0x100000)
926 pbase = MAX(pbase, 0x100000);
927 DBG("- ml_static_mfree(%p,%p)\n",
928 (void *) ml_static_ptovirt(pbase),
929 (void *) (pend - pbase));
930 ml_static_mfree(ml_static_ptovirt(pbase), pend - pbase);
931 }
932
933 /* A final pass to get rid of all initial identity mappings to
934 * low pages.
935 */
936 DPRINTF("%s: Removing mappings from 0->0x%lx\n", __FUNCTION__, vm_kernel_base);
937
938 /*
939 * Remove all mappings past the boot-cpu descriptor aliases and low globals.
940 * Non-boot-cpu GDT aliases will be remapped later as needed.
941 */
942 pmap_remove(kernel_pmap, LOWGLOBAL_ALIAS + PAGE_SIZE, vm_kernel_base);
943
944 /*
945 * If text and data are both 2MB-aligned,
946 * we can map text with large-pages,
947 * unless the -kernel_text_ps_4K boot-arg overrides.
948 */
949 if ((stext & I386_LPGMASK) == 0 && (sdata & I386_LPGMASK) == 0) {
950 kprintf("Kernel text is 2MB aligned");
951 kernel_text_ps_4K = FALSE;
952 if (PE_parse_boot_argn("-kernel_text_ps_4K",
953 &kernel_text_ps_4K,
954 sizeof (kernel_text_ps_4K)))
955 kprintf(" but will be mapped with 4K pages\n");
956 else
957 kprintf(" and will be mapped with 2M pages\n");
958 }
959
960 (void) PE_parse_boot_argn("wpkernel", &wpkernel, sizeof (wpkernel));
961 if (wpkernel)
962 kprintf("Kernel text %p-%p to be write-protected\n",
963 (void *) stext, (void *) etext);
964
965 spl = splhigh();
966
967 /*
968 * Scan over text if mappings are to be changed:
969 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
970 * - Change to large-pages if possible and not overriden.
971 */
972 if (kernel_text_ps_4K && wpkernel) {
973 vm_offset_t myva;
974 for (myva = stext; myva < etext; myva += PAGE_SIZE) {
975 pt_entry_t *ptep;
976
977 ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)myva);
978 if (ptep)
979 pmap_store_pte(ptep, *ptep & ~INTEL_PTE_WRITE);
980 }
981 }
982
983 if (!kernel_text_ps_4K) {
984 vm_offset_t myva;
985
986 /*
987 * Release zero-filled page padding used for 2M-alignment.
988 */
989 DBG("ml_static_mfree(%p,%p) for padding below text\n",
990 (void *) eHIB, (void *) (stext - eHIB));
991 ml_static_mfree(eHIB, stext - eHIB);
992 DBG("ml_static_mfree(%p,%p) for padding above text\n",
993 (void *) etext, (void *) (sdata - etext));
994 ml_static_mfree(etext, sdata - etext);
995
996 /*
997 * Coalesce text pages into large pages.
998 */
999 for (myva = stext; myva < sdata; myva += I386_LPGBYTES) {
1000 pt_entry_t *ptep;
1001 vm_offset_t pte_phys;
1002 pt_entry_t *pdep;
1003 pt_entry_t pde;
1004
1005 pdep = pmap_pde(kernel_pmap, (vm_map_offset_t)myva);
1006 ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)myva);
1007 DBG("myva: %p pdep: %p ptep: %p\n",
1008 (void *) myva, (void *) pdep, (void *) ptep);
1009 if ((*ptep & INTEL_PTE_VALID) == 0)
1010 continue;
1011 pte_phys = (vm_offset_t)(*ptep & PG_FRAME);
1012 pde = *pdep & PTMASK; /* page attributes from pde */
1013 pde |= INTEL_PTE_PS; /* make it a 2M entry */
1014 pde |= pte_phys; /* take page frame from pte */
1015
1016 if (wpkernel)
1017 pde &= ~INTEL_PTE_WRITE;
1018 DBG("pmap_store_pte(%p,0x%llx)\n",
1019 (void *)pdep, pde);
1020 pmap_store_pte(pdep, pde);
1021
1022 /*
1023 * Free the now-unused level-1 pte.
1024 * Note: ptep is a virtual address to the pte in the
1025 * recursive map. We can't use this address to free
1026 * the page. Instead we need to compute its address
1027 * in the Idle PTEs in "low memory".
1028 */
1029 vm_offset_t vm_ptep = (vm_offset_t) KPTphys
1030 + (pte_phys >> PTPGSHIFT);
1031 DBG("ml_static_mfree(%p,0x%x) for pte\n",
1032 (void *) vm_ptep, PAGE_SIZE);
1033 ml_static_mfree(vm_ptep, PAGE_SIZE);
1034 }
1035
1036 /* Change variable read by sysctl machdep.pmap */
1037 pmap_kernel_text_ps = I386_LPGBYTES;
1038 }
1039
1040 boolean_t doconstro = TRUE;
1041
1042 (void) PE_parse_boot_argn("dataconstro", &doconstro, sizeof(doconstro));
1043
1044 if ((sconstdata | econstdata) & PAGE_MASK) {
1045 kprintf("Const DATA misaligned 0x%lx 0x%lx\n", sconstdata, econstdata);
1046 if ((sconstdata & PAGE_MASK) || (doconstro_override == FALSE))
1047 doconstro = FALSE;
1048 }
1049
1050 if ((sconstdata > edata) || (sconstdata < sdata) || ((econstdata - sconstdata) >= (edata - sdata))) {
1051 kprintf("Const DATA incorrect size 0x%lx 0x%lx 0x%lx 0x%lx\n", sconstdata, econstdata, sdata, edata);
1052 doconstro = FALSE;
1053 }
1054
1055 if (doconstro)
1056 kprintf("Marking const DATA read-only\n");
1057
1058 vm_offset_t dva;
1059
1060 for (dva = sdata; dva < edata; dva += I386_PGBYTES) {
1061 assert(((sdata | edata) & PAGE_MASK) == 0);
1062 if ( (sdata | edata) & PAGE_MASK) {
1063 kprintf("DATA misaligned, 0x%lx, 0x%lx\n", sdata, edata);
1064 break;
1065 }
1066
1067 pt_entry_t dpte, *dptep = pmap_pte(kernel_pmap, dva);
1068
1069 dpte = *dptep;
1070
1071 assert((dpte & INTEL_PTE_VALID));
1072 if ((dpte & INTEL_PTE_VALID) == 0) {
1073 kprintf("Missing data mapping 0x%lx 0x%lx 0x%lx\n", dva, sdata, edata);
1074 continue;
1075 }
1076
1077 dpte |= INTEL_PTE_NX;
1078 if (doconstro && (dva >= sconstdata) && (dva < econstdata)) {
1079 dpte &= ~INTEL_PTE_WRITE;
1080 }
1081 pmap_store_pte(dptep, dpte);
1082 }
1083 kernel_segment_command_t * seg;
1084 kernel_section_t * sec;
1085
1086 for (seg = firstseg(); seg != NULL; seg = nextsegfromheader(&_mh_execute_header, seg)) {
1087 if (!strcmp(seg->segname, "__TEXT") ||
1088 !strcmp(seg->segname, "__DATA")) {
1089 continue;
1090 }
1091 //XXX
1092 if (!strcmp(seg->segname, "__KLD")) {
1093 continue;
1094 }
1095 if (!strcmp(seg->segname, "__HIB")) {
1096 for (sec = firstsect(seg); sec != NULL; sec = nextsect(seg, sec)) {
1097 if (sec->addr & PAGE_MASK)
1098 panic("__HIB segment's sections misaligned");
1099 if (!strcmp(sec->sectname, "__text")) {
1100 pmap_mark_range(kernel_pmap, sec->addr, round_page(sec->size), FALSE, TRUE);
1101 } else {
1102 pmap_mark_range(kernel_pmap, sec->addr, round_page(sec->size), TRUE, FALSE);
1103 }
1104 }
1105 } else {
1106 pmap_mark_range(kernel_pmap, seg->vmaddr, round_page_64(seg->vmsize), TRUE, FALSE);
1107 }
1108 }
1109
1110 /*
1111 * If we're debugging, map the low global vector page at the fixed
1112 * virtual address. Otherwise, remove the mapping for this.
1113 */
1114 if (debug_boot_arg) {
1115 pt_entry_t *pte = NULL;
1116 if (0 == (pte = pmap_pte(kernel_pmap, LOWGLOBAL_ALIAS)))
1117 panic("lowmem pte");
1118 /* make sure it is defined on page boundary */
1119 assert(0 == ((vm_offset_t) &lowGlo & PAGE_MASK));
1120 pmap_store_pte(pte, kvtophys((vm_offset_t)&lowGlo)
1121 | INTEL_PTE_REF
1122 | INTEL_PTE_MOD
1123 | INTEL_PTE_WIRED
1124 | INTEL_PTE_VALID
1125 | INTEL_PTE_WRITE
1126 | INTEL_PTE_NX);
1127 } else {
1128 pmap_remove(kernel_pmap,
1129 LOWGLOBAL_ALIAS, LOWGLOBAL_ALIAS + PAGE_SIZE);
1130 }
1131
1132 splx(spl);
1133 if (pmap_pcid_ncpus)
1134 tlb_flush_global();
1135 else
1136 flush_tlb_raw();
1137 }
1138
1139 /*
1140 * this function is only used for debugging fron the vm layer
1141 */
1142 boolean_t
1143 pmap_verify_free(
1144 ppnum_t pn)
1145 {
1146 pv_rooted_entry_t pv_h;
1147 int pai;
1148 boolean_t result;
1149
1150 assert(pn != vm_page_fictitious_addr);
1151
1152 if (!pmap_initialized)
1153 return(TRUE);
1154
1155 if (pn == vm_page_guard_addr)
1156 return TRUE;
1157
1158 pai = ppn_to_pai(pn);
1159 if (!IS_MANAGED_PAGE(pai))
1160 return(FALSE);
1161 pv_h = pai_to_pvh(pn);
1162 result = (pv_h->pmap == PMAP_NULL);
1163 return(result);
1164 }
1165
1166 boolean_t
1167 pmap_is_empty(
1168 pmap_t pmap,
1169 vm_map_offset_t va_start,
1170 vm_map_offset_t va_end)
1171 {
1172 vm_map_offset_t offset;
1173 ppnum_t phys_page;
1174
1175 if (pmap == PMAP_NULL) {
1176 return TRUE;
1177 }
1178
1179 /*
1180 * Check the resident page count
1181 * - if it's zero, the pmap is completely empty.
1182 * This short-circuit test prevents a virtual address scan which is
1183 * painfully slow for 64-bit spaces.
1184 * This assumes the count is correct
1185 * .. the debug kernel ought to be checking perhaps by page table walk.
1186 */
1187 if (pmap->stats.resident_count == 0)
1188 return TRUE;
1189
1190 for (offset = va_start;
1191 offset < va_end;
1192 offset += PAGE_SIZE_64) {
1193 phys_page = pmap_find_phys(pmap, offset);
1194 if (phys_page) {
1195 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
1196 "page %d at 0x%llx\n",
1197 pmap, va_start, va_end, phys_page, offset);
1198 return FALSE;
1199 }
1200 }
1201
1202 return TRUE;
1203 }
1204
1205
1206 /*
1207 * Create and return a physical map.
1208 *
1209 * If the size specified for the map
1210 * is zero, the map is an actual physical
1211 * map, and may be referenced by the
1212 * hardware.
1213 *
1214 * If the size specified is non-zero,
1215 * the map will be used in software only, and
1216 * is bounded by that size.
1217 */
1218 pmap_t
1219 pmap_create(
1220 ledger_t ledger,
1221 vm_map_size_t sz,
1222 boolean_t is_64bit)
1223 {
1224 pmap_t p;
1225 vm_size_t size;
1226 pml4_entry_t *pml4;
1227 pml4_entry_t *kpml4;
1228
1229 PMAP_TRACE(PMAP_CODE(PMAP__CREATE) | DBG_FUNC_START,
1230 (uint32_t) (sz>>32), (uint32_t) sz, is_64bit, 0, 0);
1231
1232 size = (vm_size_t) sz;
1233
1234 /*
1235 * A software use-only map doesn't even need a map.
1236 */
1237
1238 if (size != 0) {
1239 return(PMAP_NULL);
1240 }
1241
1242 p = (pmap_t) zalloc(pmap_zone);
1243 if (PMAP_NULL == p)
1244 panic("pmap_create zalloc");
1245 /* Zero all fields */
1246 bzero(p, sizeof(*p));
1247 /* init counts now since we'll be bumping some */
1248 simple_lock_init(&p->lock, 0);
1249 #if 00
1250 p->stats.resident_count = 0;
1251 p->stats.resident_max = 0;
1252 p->stats.wired_count = 0;
1253 #else
1254 bzero(&p->stats, sizeof (p->stats));
1255 #endif
1256 p->ref_count = 1;
1257 p->nx_enabled = 1;
1258 p->pm_shared = FALSE;
1259 ledger_reference(ledger);
1260 p->ledger = ledger;
1261
1262 p->pm_task_map = is_64bit ? TASK_MAP_64BIT : TASK_MAP_32BIT;;
1263 if (pmap_pcid_ncpus)
1264 pmap_pcid_initialize(p);
1265
1266 p->pm_pml4 = zalloc(pmap_anchor_zone);
1267
1268 pmap_assert((((uintptr_t)p->pm_pml4) & PAGE_MASK) == 0);
1269
1270 memset((char *)p->pm_pml4, 0, PAGE_SIZE);
1271
1272 p->pm_cr3 = (pmap_paddr_t)kvtophys((vm_offset_t)p->pm_pml4);
1273
1274 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
1275
1276 p->pm_obj_pml4 = vm_object_allocate((vm_object_size_t)(NPML4PGS) * PAGE_SIZE);
1277 if (NULL == p->pm_obj_pml4)
1278 panic("pmap_create pdpt obj");
1279
1280 p->pm_obj_pdpt = vm_object_allocate((vm_object_size_t)(NPDPTPGS) * PAGE_SIZE);
1281 if (NULL == p->pm_obj_pdpt)
1282 panic("pmap_create pdpt obj");
1283
1284 p->pm_obj = vm_object_allocate((vm_object_size_t)(NPDEPGS) * PAGE_SIZE);
1285 if (NULL == p->pm_obj)
1286 panic("pmap_create pte obj");
1287
1288 /* All pmaps share the kernel's pml4 */
1289 pml4 = pmap64_pml4(p, 0ULL);
1290 kpml4 = kernel_pmap->pm_pml4;
1291 pml4[KERNEL_PML4_INDEX] = kpml4[KERNEL_PML4_INDEX];
1292 pml4[KERNEL_KEXTS_INDEX] = kpml4[KERNEL_KEXTS_INDEX];
1293 pml4[KERNEL_PHYSMAP_PML4_INDEX] = kpml4[KERNEL_PHYSMAP_PML4_INDEX];
1294
1295 PMAP_TRACE(PMAP_CODE(PMAP__CREATE) | DBG_FUNC_START,
1296 p, is_64bit, 0, 0, 0);
1297
1298 return(p);
1299 }
1300
1301 /*
1302 * Retire the given physical map from service.
1303 * Should only be called if the map contains
1304 * no valid mappings.
1305 */
1306
1307 void
1308 pmap_destroy(pmap_t p)
1309 {
1310 int c;
1311
1312 if (p == PMAP_NULL)
1313 return;
1314
1315 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_START,
1316 p, 0, 0, 0, 0);
1317
1318 PMAP_LOCK(p);
1319
1320 c = --p->ref_count;
1321
1322 pmap_assert((current_thread() && (current_thread()->map)) ? (current_thread()->map->pmap != p) : TRUE);
1323
1324 if (c == 0) {
1325 /*
1326 * If some cpu is not using the physical pmap pointer that it
1327 * is supposed to be (see set_dirbase), we might be using the
1328 * pmap that is being destroyed! Make sure we are
1329 * physically on the right pmap:
1330 */
1331 PMAP_UPDATE_TLBS(p, 0x0ULL, 0xFFFFFFFFFFFFF000ULL);
1332 if (pmap_pcid_ncpus)
1333 pmap_destroy_pcid_sync(p);
1334 }
1335
1336 PMAP_UNLOCK(p);
1337
1338 if (c != 0) {
1339 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_END,
1340 p, 1, 0, 0, 0);
1341 pmap_assert(p == kernel_pmap);
1342 return; /* still in use */
1343 }
1344
1345 /*
1346 * Free the memory maps, then the
1347 * pmap structure.
1348 */
1349 int inuse_ptepages = 0;
1350
1351 zfree(pmap_anchor_zone, p->pm_pml4);
1352
1353 inuse_ptepages += p->pm_obj_pml4->resident_page_count;
1354 vm_object_deallocate(p->pm_obj_pml4);
1355
1356 inuse_ptepages += p->pm_obj_pdpt->resident_page_count;
1357 vm_object_deallocate(p->pm_obj_pdpt);
1358
1359 inuse_ptepages += p->pm_obj->resident_page_count;
1360 vm_object_deallocate(p->pm_obj);
1361
1362 OSAddAtomic(-inuse_ptepages, &inuse_ptepages_count);
1363 PMAP_ZINFO_PFREE(p, inuse_ptepages * PAGE_SIZE);
1364 ledger_dereference(p->ledger);
1365 zfree(pmap_zone, p);
1366
1367 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_END,
1368 0, 0, 0, 0, 0);
1369 }
1370
1371 /*
1372 * Add a reference to the specified pmap.
1373 */
1374
1375 void
1376 pmap_reference(pmap_t p)
1377 {
1378 if (p != PMAP_NULL) {
1379 PMAP_LOCK(p);
1380 p->ref_count++;
1381 PMAP_UNLOCK(p);;
1382 }
1383 }
1384
1385 /*
1386 * Remove phys addr if mapped in specified map
1387 *
1388 */
1389 void
1390 pmap_remove_some_phys(
1391 __unused pmap_t map,
1392 __unused ppnum_t pn)
1393 {
1394
1395 /* Implement to support working set code */
1396
1397 }
1398
1399
1400 void
1401 pmap_protect(
1402 pmap_t map,
1403 vm_map_offset_t sva,
1404 vm_map_offset_t eva,
1405 vm_prot_t prot)
1406 {
1407 pmap_protect_options(map, sva, eva, prot, 0, NULL);
1408 }
1409
1410
1411 /*
1412 * Set the physical protection on the
1413 * specified range of this map as requested.
1414 * Will not increase permissions.
1415 */
1416 void
1417 pmap_protect_options(
1418 pmap_t map,
1419 vm_map_offset_t sva,
1420 vm_map_offset_t eva,
1421 vm_prot_t prot,
1422 unsigned int options,
1423 void *arg)
1424 {
1425 pt_entry_t *pde;
1426 pt_entry_t *spte, *epte;
1427 vm_map_offset_t lva;
1428 vm_map_offset_t orig_sva;
1429 boolean_t set_NX;
1430 int num_found = 0;
1431
1432 pmap_intr_assert();
1433
1434 if (map == PMAP_NULL)
1435 return;
1436
1437 if (prot == VM_PROT_NONE) {
1438 pmap_remove_options(map, sva, eva, options);
1439 return;
1440 }
1441 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT) | DBG_FUNC_START,
1442 map,
1443 (uint32_t) (sva >> 32), (uint32_t) sva,
1444 (uint32_t) (eva >> 32), (uint32_t) eva);
1445
1446 if ((prot & VM_PROT_EXECUTE) || !nx_enabled || !map->nx_enabled)
1447 set_NX = FALSE;
1448 else
1449 set_NX = TRUE;
1450
1451 PMAP_LOCK(map);
1452
1453 orig_sva = sva;
1454 while (sva < eva) {
1455 lva = (sva + pde_mapped_size) & ~(pde_mapped_size - 1);
1456 if (lva > eva)
1457 lva = eva;
1458 pde = pmap_pde(map, sva);
1459 if (pde && (*pde & INTEL_PTE_VALID)) {
1460 if (*pde & INTEL_PTE_PS) {
1461 /* superpage */
1462 spte = pde;
1463 epte = spte+1; /* excluded */
1464 } else {
1465 spte = pmap_pte(map, (sva & ~(pde_mapped_size - 1)));
1466 spte = &spte[ptenum(sva)];
1467 epte = &spte[intel_btop(lva - sva)];
1468 }
1469
1470 for (; spte < epte; spte++) {
1471 if (!(*spte & INTEL_PTE_VALID))
1472 continue;
1473
1474 if (prot & VM_PROT_WRITE)
1475 pmap_update_pte(spte, 0, INTEL_PTE_WRITE);
1476 else
1477 pmap_update_pte(spte, INTEL_PTE_WRITE, 0);
1478
1479 if (set_NX)
1480 pmap_update_pte(spte, 0, INTEL_PTE_NX);
1481 else
1482 pmap_update_pte(spte, INTEL_PTE_NX, 0);
1483 num_found++;
1484 }
1485 }
1486 sva = lva;
1487 }
1488 if (num_found) {
1489 if (options & PMAP_OPTIONS_NOFLUSH)
1490 PMAP_UPDATE_TLBS_DELAYED(map, orig_sva, eva, (pmap_flush_context *)arg);
1491 else
1492 PMAP_UPDATE_TLBS(map, orig_sva, eva);
1493 }
1494 PMAP_UNLOCK(map);
1495
1496 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT) | DBG_FUNC_END,
1497 0, 0, 0, 0, 0);
1498
1499 }
1500
1501 /* Map a (possibly) autogenned block */
1502 void
1503 pmap_map_block(
1504 pmap_t pmap,
1505 addr64_t va,
1506 ppnum_t pa,
1507 uint32_t size,
1508 vm_prot_t prot,
1509 int attr,
1510 __unused unsigned int flags)
1511 {
1512 uint32_t page;
1513 int cur_page_size;
1514
1515 if (attr & VM_MEM_SUPERPAGE)
1516 cur_page_size = SUPERPAGE_SIZE;
1517 else
1518 cur_page_size = PAGE_SIZE;
1519
1520 for (page = 0; page < size; page+=cur_page_size/PAGE_SIZE) {
1521 pmap_enter(pmap, va, pa, prot, VM_PROT_NONE, attr, TRUE);
1522 va += cur_page_size;
1523 pa+=cur_page_size/PAGE_SIZE;
1524 }
1525 }
1526
1527 kern_return_t
1528 pmap_expand_pml4(
1529 pmap_t map,
1530 vm_map_offset_t vaddr,
1531 unsigned int options)
1532 {
1533 vm_page_t m;
1534 pmap_paddr_t pa;
1535 uint64_t i;
1536 ppnum_t pn;
1537 pml4_entry_t *pml4p;
1538
1539 DBG("pmap_expand_pml4(%p,%p)\n", map, (void *)vaddr);
1540
1541 /*
1542 * Allocate a VM page for the pml4 page
1543 */
1544 while ((m = vm_page_grab()) == VM_PAGE_NULL) {
1545 if (options & PMAP_EXPAND_OPTIONS_NOWAIT)
1546 return KERN_RESOURCE_SHORTAGE;
1547 VM_PAGE_WAIT();
1548 }
1549 /*
1550 * put the page into the pmap's obj list so it
1551 * can be found later.
1552 */
1553 pn = m->phys_page;
1554 pa = i386_ptob(pn);
1555 i = pml4idx(map, vaddr);
1556
1557 /*
1558 * Zero the page.
1559 */
1560 pmap_zero_page(pn);
1561
1562 vm_page_lockspin_queues();
1563 vm_page_wire(m);
1564 vm_page_unlock_queues();
1565
1566 OSAddAtomic(1, &inuse_ptepages_count);
1567 OSAddAtomic64(1, &alloc_ptepages_count);
1568 PMAP_ZINFO_PALLOC(map, PAGE_SIZE);
1569
1570 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1571 vm_object_lock(map->pm_obj_pml4);
1572
1573 PMAP_LOCK(map);
1574 /*
1575 * See if someone else expanded us first
1576 */
1577 if (pmap64_pdpt(map, vaddr) != PDPT_ENTRY_NULL) {
1578 PMAP_UNLOCK(map);
1579 vm_object_unlock(map->pm_obj_pml4);
1580
1581 VM_PAGE_FREE(m);
1582
1583 OSAddAtomic(-1, &inuse_ptepages_count);
1584 PMAP_ZINFO_PFREE(map, PAGE_SIZE);
1585 return KERN_SUCCESS;
1586 }
1587
1588 #if 0 /* DEBUG */
1589 if (0 != vm_page_lookup(map->pm_obj_pml4, (vm_object_offset_t)i * PAGE_SIZE)) {
1590 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1591 map, map->pm_obj_pml4, vaddr, i);
1592 }
1593 #endif
1594 vm_page_insert(m, map->pm_obj_pml4, (vm_object_offset_t)i * PAGE_SIZE);
1595 vm_object_unlock(map->pm_obj_pml4);
1596
1597 /*
1598 * Set the page directory entry for this page table.
1599 */
1600 pml4p = pmap64_pml4(map, vaddr); /* refetch under lock */
1601
1602 pmap_store_pte(pml4p, pa_to_pte(pa)
1603 | INTEL_PTE_VALID
1604 | INTEL_PTE_USER
1605 | INTEL_PTE_WRITE);
1606
1607 PMAP_UNLOCK(map);
1608
1609 return KERN_SUCCESS;
1610 }
1611
1612 kern_return_t
1613 pmap_expand_pdpt(pmap_t map, vm_map_offset_t vaddr, unsigned int options)
1614 {
1615 vm_page_t m;
1616 pmap_paddr_t pa;
1617 uint64_t i;
1618 ppnum_t pn;
1619 pdpt_entry_t *pdptp;
1620
1621 DBG("pmap_expand_pdpt(%p,%p)\n", map, (void *)vaddr);
1622
1623 while ((pdptp = pmap64_pdpt(map, vaddr)) == PDPT_ENTRY_NULL) {
1624 kern_return_t pep4kr = pmap_expand_pml4(map, vaddr, options);
1625 if (pep4kr != KERN_SUCCESS)
1626 return pep4kr;
1627 }
1628
1629 /*
1630 * Allocate a VM page for the pdpt page
1631 */
1632 while ((m = vm_page_grab()) == VM_PAGE_NULL) {
1633 if (options & PMAP_EXPAND_OPTIONS_NOWAIT)
1634 return KERN_RESOURCE_SHORTAGE;
1635 VM_PAGE_WAIT();
1636 }
1637
1638 /*
1639 * put the page into the pmap's obj list so it
1640 * can be found later.
1641 */
1642 pn = m->phys_page;
1643 pa = i386_ptob(pn);
1644 i = pdptidx(map, vaddr);
1645
1646 /*
1647 * Zero the page.
1648 */
1649 pmap_zero_page(pn);
1650
1651 vm_page_lockspin_queues();
1652 vm_page_wire(m);
1653 vm_page_unlock_queues();
1654
1655 OSAddAtomic(1, &inuse_ptepages_count);
1656 OSAddAtomic64(1, &alloc_ptepages_count);
1657 PMAP_ZINFO_PALLOC(map, PAGE_SIZE);
1658
1659 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1660 vm_object_lock(map->pm_obj_pdpt);
1661
1662 PMAP_LOCK(map);
1663 /*
1664 * See if someone else expanded us first
1665 */
1666 if (pmap64_pde(map, vaddr) != PD_ENTRY_NULL) {
1667 PMAP_UNLOCK(map);
1668 vm_object_unlock(map->pm_obj_pdpt);
1669
1670 VM_PAGE_FREE(m);
1671
1672 OSAddAtomic(-1, &inuse_ptepages_count);
1673 PMAP_ZINFO_PFREE(map, PAGE_SIZE);
1674 return KERN_SUCCESS;
1675 }
1676
1677 #if 0 /* DEBUG */
1678 if (0 != vm_page_lookup(map->pm_obj_pdpt, (vm_object_offset_t)i * PAGE_SIZE)) {
1679 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1680 map, map->pm_obj_pdpt, vaddr, i);
1681 }
1682 #endif
1683 vm_page_insert(m, map->pm_obj_pdpt, (vm_object_offset_t)i * PAGE_SIZE);
1684 vm_object_unlock(map->pm_obj_pdpt);
1685
1686 /*
1687 * Set the page directory entry for this page table.
1688 */
1689 pdptp = pmap64_pdpt(map, vaddr); /* refetch under lock */
1690
1691 pmap_store_pte(pdptp, pa_to_pte(pa)
1692 | INTEL_PTE_VALID
1693 | INTEL_PTE_USER
1694 | INTEL_PTE_WRITE);
1695
1696 PMAP_UNLOCK(map);
1697
1698 return KERN_SUCCESS;
1699
1700 }
1701
1702
1703
1704 /*
1705 * Routine: pmap_expand
1706 *
1707 * Expands a pmap to be able to map the specified virtual address.
1708 *
1709 * Allocates new virtual memory for the P0 or P1 portion of the
1710 * pmap, then re-maps the physical pages that were in the old
1711 * pmap to be in the new pmap.
1712 *
1713 * Must be called with the pmap system and the pmap unlocked,
1714 * since these must be unlocked to use vm_allocate or vm_deallocate.
1715 * Thus it must be called in a loop that checks whether the map
1716 * has been expanded enough.
1717 * (We won't loop forever, since page tables aren't shrunk.)
1718 */
1719 kern_return_t
1720 pmap_expand(
1721 pmap_t map,
1722 vm_map_offset_t vaddr,
1723 unsigned int options)
1724 {
1725 pt_entry_t *pdp;
1726 register vm_page_t m;
1727 register pmap_paddr_t pa;
1728 uint64_t i;
1729 ppnum_t pn;
1730
1731
1732 /*
1733 * For the kernel, the virtual address must be in or above the basement
1734 * which is for kexts and is in the 512GB immediately below the kernel..
1735 * XXX - should use VM_MIN_KERNEL_AND_KEXT_ADDRESS not KERNEL_BASEMENT
1736 */
1737 if (map == kernel_pmap &&
1738 !(vaddr >= KERNEL_BASEMENT && vaddr <= VM_MAX_KERNEL_ADDRESS))
1739 panic("pmap_expand: bad vaddr 0x%llx for kernel pmap", vaddr);
1740
1741
1742 while ((pdp = pmap64_pde(map, vaddr)) == PD_ENTRY_NULL) {
1743 kern_return_t pepkr = pmap_expand_pdpt(map, vaddr, options);
1744 if (pepkr != KERN_SUCCESS)
1745 return pepkr;
1746 }
1747
1748 /*
1749 * Allocate a VM page for the pde entries.
1750 */
1751 while ((m = vm_page_grab()) == VM_PAGE_NULL) {
1752 if (options & PMAP_EXPAND_OPTIONS_NOWAIT)
1753 return KERN_RESOURCE_SHORTAGE;
1754 VM_PAGE_WAIT();
1755 }
1756
1757 /*
1758 * put the page into the pmap's obj list so it
1759 * can be found later.
1760 */
1761 pn = m->phys_page;
1762 pa = i386_ptob(pn);
1763 i = pdeidx(map, vaddr);
1764
1765 /*
1766 * Zero the page.
1767 */
1768 pmap_zero_page(pn);
1769
1770 vm_page_lockspin_queues();
1771 vm_page_wire(m);
1772 vm_page_unlock_queues();
1773
1774 OSAddAtomic(1, &inuse_ptepages_count);
1775 OSAddAtomic64(1, &alloc_ptepages_count);
1776 PMAP_ZINFO_PALLOC(map, PAGE_SIZE);
1777
1778 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1779 vm_object_lock(map->pm_obj);
1780
1781 PMAP_LOCK(map);
1782
1783 /*
1784 * See if someone else expanded us first
1785 */
1786 if (pmap_pte(map, vaddr) != PT_ENTRY_NULL) {
1787 PMAP_UNLOCK(map);
1788 vm_object_unlock(map->pm_obj);
1789
1790 VM_PAGE_FREE(m);
1791
1792 OSAddAtomic(-1, &inuse_ptepages_count);
1793 PMAP_ZINFO_PFREE(map, PAGE_SIZE);
1794 return KERN_SUCCESS;
1795 }
1796
1797 #if 0 /* DEBUG */
1798 if (0 != vm_page_lookup(map->pm_obj, (vm_object_offset_t)i * PAGE_SIZE)) {
1799 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
1800 map, map->pm_obj, vaddr, i);
1801 }
1802 #endif
1803 vm_page_insert(m, map->pm_obj, (vm_object_offset_t)i * PAGE_SIZE);
1804 vm_object_unlock(map->pm_obj);
1805
1806 /*
1807 * Set the page directory entry for this page table.
1808 */
1809 pdp = pmap_pde(map, vaddr);
1810 pmap_store_pte(pdp, pa_to_pte(pa)
1811 | INTEL_PTE_VALID
1812 | INTEL_PTE_USER
1813 | INTEL_PTE_WRITE);
1814
1815 PMAP_UNLOCK(map);
1816
1817 return KERN_SUCCESS;
1818 }
1819
1820 /* On K64 machines with more than 32GB of memory, pmap_steal_memory
1821 * will allocate past the 1GB of pre-expanded virtual kernel area. This
1822 * function allocates all the page tables using memory from the same pool
1823 * that pmap_steal_memory uses, rather than calling vm_page_grab (which
1824 * isn't available yet). */
1825 void
1826 pmap_pre_expand(pmap_t pmap, vm_map_offset_t vaddr)
1827 {
1828 ppnum_t pn;
1829 pt_entry_t *pte;
1830
1831 PMAP_LOCK(pmap);
1832
1833 if(pmap64_pdpt(pmap, vaddr) == PDPT_ENTRY_NULL) {
1834 if (!pmap_next_page_hi(&pn))
1835 panic("pmap_pre_expand");
1836
1837 pmap_zero_page(pn);
1838
1839 pte = pmap64_pml4(pmap, vaddr);
1840
1841 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1842 | INTEL_PTE_VALID
1843 | INTEL_PTE_USER
1844 | INTEL_PTE_WRITE);
1845 }
1846
1847 if(pmap64_pde(pmap, vaddr) == PD_ENTRY_NULL) {
1848 if (!pmap_next_page_hi(&pn))
1849 panic("pmap_pre_expand");
1850
1851 pmap_zero_page(pn);
1852
1853 pte = pmap64_pdpt(pmap, vaddr);
1854
1855 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1856 | INTEL_PTE_VALID
1857 | INTEL_PTE_USER
1858 | INTEL_PTE_WRITE);
1859 }
1860
1861 if(pmap_pte(pmap, vaddr) == PT_ENTRY_NULL) {
1862 if (!pmap_next_page_hi(&pn))
1863 panic("pmap_pre_expand");
1864
1865 pmap_zero_page(pn);
1866
1867 pte = pmap64_pde(pmap, vaddr);
1868
1869 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1870 | INTEL_PTE_VALID
1871 | INTEL_PTE_USER
1872 | INTEL_PTE_WRITE);
1873 }
1874
1875 PMAP_UNLOCK(pmap);
1876 }
1877
1878 /*
1879 * pmap_sync_page_data_phys(ppnum_t pa)
1880 *
1881 * Invalidates all of the instruction cache on a physical page and
1882 * pushes any dirty data from the data cache for the same physical page
1883 * Not required in i386.
1884 */
1885 void
1886 pmap_sync_page_data_phys(__unused ppnum_t pa)
1887 {
1888 return;
1889 }
1890
1891 /*
1892 * pmap_sync_page_attributes_phys(ppnum_t pa)
1893 *
1894 * Write back and invalidate all cachelines on a physical page.
1895 */
1896 void
1897 pmap_sync_page_attributes_phys(ppnum_t pa)
1898 {
1899 cache_flush_page_phys(pa);
1900 }
1901
1902
1903
1904 #ifdef CURRENTLY_UNUSED_AND_UNTESTED
1905
1906 int collect_ref;
1907 int collect_unref;
1908
1909 /*
1910 * Routine: pmap_collect
1911 * Function:
1912 * Garbage collects the physical map system for
1913 * pages which are no longer used.
1914 * Success need not be guaranteed -- that is, there
1915 * may well be pages which are not referenced, but
1916 * others may be collected.
1917 * Usage:
1918 * Called by the pageout daemon when pages are scarce.
1919 */
1920 void
1921 pmap_collect(
1922 pmap_t p)
1923 {
1924 register pt_entry_t *pdp, *ptp;
1925 pt_entry_t *eptp;
1926 int wired;
1927
1928 if (p == PMAP_NULL)
1929 return;
1930
1931 if (p == kernel_pmap)
1932 return;
1933
1934 /*
1935 * Garbage collect map.
1936 */
1937 PMAP_LOCK(p);
1938
1939 for (pdp = (pt_entry_t *)p->dirbase;
1940 pdp < (pt_entry_t *)&p->dirbase[(UMAXPTDI+1)];
1941 pdp++)
1942 {
1943 if (*pdp & INTEL_PTE_VALID) {
1944 if(*pdp & INTEL_PTE_REF) {
1945 pmap_store_pte(pdp, *pdp & ~INTEL_PTE_REF);
1946 collect_ref++;
1947 } else {
1948 collect_unref++;
1949 ptp = pmap_pte(p, pdetova(pdp - (pt_entry_t *)p->dirbase));
1950 eptp = ptp + NPTEPG;
1951
1952 /*
1953 * If the pte page has any wired mappings, we cannot
1954 * free it.
1955 */
1956 wired = 0;
1957 {
1958 register pt_entry_t *ptep;
1959 for (ptep = ptp; ptep < eptp; ptep++) {
1960 if (iswired(*ptep)) {
1961 wired = 1;
1962 break;
1963 }
1964 }
1965 }
1966 if (!wired) {
1967 /*
1968 * Remove the virtual addresses mapped by this pte page.
1969 */
1970 pmap_remove_range(p,
1971 pdetova(pdp - (pt_entry_t *)p->dirbase),
1972 ptp,
1973 eptp);
1974
1975 /*
1976 * Invalidate the page directory pointer.
1977 */
1978 pmap_store_pte(pdp, 0x0);
1979
1980 PMAP_UNLOCK(p);
1981
1982 /*
1983 * And free the pte page itself.
1984 */
1985 {
1986 register vm_page_t m;
1987
1988 vm_object_lock(p->pm_obj);
1989
1990 m = vm_page_lookup(p->pm_obj,(vm_object_offset_t)(pdp - (pt_entry_t *)&p->dirbase[0]) * PAGE_SIZE);
1991 if (m == VM_PAGE_NULL)
1992 panic("pmap_collect: pte page not in object");
1993
1994 vm_object_unlock(p->pm_obj);
1995
1996 VM_PAGE_FREE(m);
1997
1998 OSAddAtomic(-1, &inuse_ptepages_count);
1999 PMAP_ZINFO_PFREE(p, PAGE_SIZE);
2000 }
2001
2002 PMAP_LOCK(p);
2003 }
2004 }
2005 }
2006 }
2007
2008 PMAP_UPDATE_TLBS(p, 0x0, 0xFFFFFFFFFFFFF000ULL);
2009 PMAP_UNLOCK(p);
2010 return;
2011
2012 }
2013 #endif
2014
2015
2016 void
2017 pmap_copy_page(ppnum_t src, ppnum_t dst)
2018 {
2019 bcopy_phys((addr64_t)i386_ptob(src),
2020 (addr64_t)i386_ptob(dst),
2021 PAGE_SIZE);
2022 }
2023
2024
2025 /*
2026 * Routine: pmap_pageable
2027 * Function:
2028 * Make the specified pages (by pmap, offset)
2029 * pageable (or not) as requested.
2030 *
2031 * A page which is not pageable may not take
2032 * a fault; therefore, its page table entry
2033 * must remain valid for the duration.
2034 *
2035 * This routine is merely advisory; pmap_enter
2036 * will specify that these pages are to be wired
2037 * down (or not) as appropriate.
2038 */
2039 void
2040 pmap_pageable(
2041 __unused pmap_t pmap,
2042 __unused vm_map_offset_t start_addr,
2043 __unused vm_map_offset_t end_addr,
2044 __unused boolean_t pageable)
2045 {
2046 #ifdef lint
2047 pmap++; start_addr++; end_addr++; pageable++;
2048 #endif /* lint */
2049 }
2050
2051 void
2052 invalidate_icache(__unused vm_offset_t addr,
2053 __unused unsigned cnt,
2054 __unused int phys)
2055 {
2056 return;
2057 }
2058
2059 void
2060 flush_dcache(__unused vm_offset_t addr,
2061 __unused unsigned count,
2062 __unused int phys)
2063 {
2064 return;
2065 }
2066
2067 #if CONFIG_DTRACE
2068 /*
2069 * Constrain DTrace copyin/copyout actions
2070 */
2071 extern kern_return_t dtrace_copyio_preflight(addr64_t);
2072 extern kern_return_t dtrace_copyio_postflight(addr64_t);
2073
2074 kern_return_t dtrace_copyio_preflight(__unused addr64_t va)
2075 {
2076 thread_t thread = current_thread();
2077 uint64_t ccr3;
2078 if (current_map() == kernel_map)
2079 return KERN_FAILURE;
2080 else if (((ccr3 = get_cr3_base()) != thread->map->pmap->pm_cr3) && (no_shared_cr3 == FALSE))
2081 return KERN_FAILURE;
2082 else if (no_shared_cr3 && (ccr3 != kernel_pmap->pm_cr3))
2083 return KERN_FAILURE;
2084 else
2085 return KERN_SUCCESS;
2086 }
2087
2088 kern_return_t dtrace_copyio_postflight(__unused addr64_t va)
2089 {
2090 return KERN_SUCCESS;
2091 }
2092 #endif /* CONFIG_DTRACE */
2093
2094 #include <mach_vm_debug.h>
2095 #if MACH_VM_DEBUG
2096 #include <vm/vm_debug.h>
2097
2098 int
2099 pmap_list_resident_pages(
2100 __unused pmap_t pmap,
2101 __unused vm_offset_t *listp,
2102 __unused int space)
2103 {
2104 return 0;
2105 }
2106 #endif /* MACH_VM_DEBUG */
2107
2108
2109
2110 /* temporary workaround */
2111 boolean_t
2112 coredumpok(__unused vm_map_t map, __unused vm_offset_t va)
2113 {
2114 #if 0
2115 pt_entry_t *ptep;
2116
2117 ptep = pmap_pte(map->pmap, va);
2118 if (0 == ptep)
2119 return FALSE;
2120 return ((*ptep & (INTEL_PTE_NCACHE | INTEL_PTE_WIRED)) != (INTEL_PTE_NCACHE | INTEL_PTE_WIRED));
2121 #else
2122 return TRUE;
2123 #endif
2124 }
2125
2126
2127 boolean_t
2128 phys_page_exists(ppnum_t pn)
2129 {
2130 assert(pn != vm_page_fictitious_addr);
2131
2132 if (!pmap_initialized)
2133 return TRUE;
2134
2135 if (pn == vm_page_guard_addr)
2136 return FALSE;
2137
2138 if (!IS_MANAGED_PAGE(ppn_to_pai(pn)))
2139 return FALSE;
2140
2141 return TRUE;
2142 }
2143
2144
2145
2146 void
2147 pmap_switch(pmap_t tpmap)
2148 {
2149 spl_t s;
2150
2151 s = splhigh(); /* Make sure interruptions are disabled */
2152 set_dirbase(tpmap, current_thread(), cpu_number());
2153 splx(s);
2154 }
2155
2156
2157 /*
2158 * disable no-execute capability on
2159 * the specified pmap
2160 */
2161 void
2162 pmap_disable_NX(pmap_t pmap)
2163 {
2164 pmap->nx_enabled = 0;
2165 }
2166
2167 void
2168 pt_fake_zone_init(int zone_index)
2169 {
2170 pt_fake_zone_index = zone_index;
2171 }
2172
2173 void
2174 pt_fake_zone_info(
2175 int *count,
2176 vm_size_t *cur_size,
2177 vm_size_t *max_size,
2178 vm_size_t *elem_size,
2179 vm_size_t *alloc_size,
2180 uint64_t *sum_size,
2181 int *collectable,
2182 int *exhaustable,
2183 int *caller_acct)
2184 {
2185 *count = inuse_ptepages_count;
2186 *cur_size = PAGE_SIZE * inuse_ptepages_count;
2187 *max_size = PAGE_SIZE * (inuse_ptepages_count +
2188 vm_page_inactive_count +
2189 vm_page_active_count +
2190 vm_page_free_count);
2191 *elem_size = PAGE_SIZE;
2192 *alloc_size = PAGE_SIZE;
2193 *sum_size = alloc_ptepages_count * PAGE_SIZE;
2194
2195 *collectable = 1;
2196 *exhaustable = 0;
2197 *caller_acct = 1;
2198 }
2199
2200
2201 void
2202 pmap_flush_context_init(pmap_flush_context *pfc)
2203 {
2204 pfc->pfc_cpus = 0;
2205 pfc->pfc_invalid_global = 0;
2206 }
2207
2208 extern unsigned TLBTimeOut;
2209 void
2210 pmap_flush(
2211 pmap_flush_context *pfc)
2212 {
2213 unsigned int my_cpu;
2214 unsigned int cpu;
2215 unsigned int cpu_bit;
2216 cpumask_t cpus_to_respond = 0;
2217 cpumask_t cpus_to_signal = 0;
2218 cpumask_t cpus_signaled = 0;
2219 boolean_t flush_self = FALSE;
2220 uint64_t deadline;
2221
2222 mp_disable_preemption();
2223
2224 my_cpu = cpu_number();
2225 cpus_to_signal = pfc->pfc_cpus;
2226
2227 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS) | DBG_FUNC_START,
2228 NULL, cpus_to_signal, 0, 0, 0);
2229
2230 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus && cpus_to_signal; cpu++, cpu_bit <<= 1) {
2231
2232 if (cpus_to_signal & cpu_bit) {
2233
2234 cpus_to_signal &= ~cpu_bit;
2235
2236 if (!cpu_datap(cpu)->cpu_running)
2237 continue;
2238
2239 if (pfc->pfc_invalid_global & cpu_bit)
2240 cpu_datap(cpu)->cpu_tlb_invalid_global = TRUE;
2241 else
2242 cpu_datap(cpu)->cpu_tlb_invalid_local = TRUE;
2243 mfence();
2244
2245 if (cpu == my_cpu) {
2246 flush_self = TRUE;
2247 continue;
2248 }
2249 if (CPU_CR3_IS_ACTIVE(cpu)) {
2250 cpus_to_respond |= cpu_bit;
2251 i386_signal_cpu(cpu, MP_TLB_FLUSH, ASYNC);
2252 }
2253 }
2254 }
2255 cpus_signaled = cpus_to_respond;
2256
2257 /*
2258 * Flush local tlb if required.
2259 * Do this now to overlap with other processors responding.
2260 */
2261 if (flush_self && cpu_datap(my_cpu)->cpu_tlb_invalid != FALSE)
2262 process_pmap_updates();
2263
2264 if (cpus_to_respond) {
2265
2266 deadline = mach_absolute_time() +
2267 (TLBTimeOut ? TLBTimeOut : LockTimeOut);
2268 boolean_t is_timeout_traced = FALSE;
2269
2270 /*
2271 * Wait for those other cpus to acknowledge
2272 */
2273 while (cpus_to_respond != 0) {
2274 long orig_acks = 0;
2275
2276 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2277 /* Consider checking local/global invalidity
2278 * as appropriate in the PCID case.
2279 */
2280 if ((cpus_to_respond & cpu_bit) != 0) {
2281 if (!cpu_datap(cpu)->cpu_running ||
2282 cpu_datap(cpu)->cpu_tlb_invalid == FALSE ||
2283 !CPU_CR3_IS_ACTIVE(cpu)) {
2284 cpus_to_respond &= ~cpu_bit;
2285 }
2286 cpu_pause();
2287 }
2288 if (cpus_to_respond == 0)
2289 break;
2290 }
2291 if (cpus_to_respond && (mach_absolute_time() > deadline)) {
2292 if (machine_timeout_suspended())
2293 continue;
2294 if (TLBTimeOut == 0) {
2295 if (is_timeout_traced)
2296 continue;
2297 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS_TO),
2298 NULL, cpus_to_signal, cpus_to_respond, 0, 0);
2299 is_timeout_traced = TRUE;
2300 continue;
2301 }
2302 pmap_tlb_flush_timeout = TRUE;
2303 orig_acks = NMIPI_acks;
2304 mp_cpus_NMIPI(cpus_to_respond);
2305
2306 panic("TLB invalidation IPI timeout: "
2307 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%lx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2308 cpus_to_respond, orig_acks, NMIPI_acks);
2309 }
2310 }
2311 }
2312 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_DELAYED_TLBS) | DBG_FUNC_END,
2313 NULL, cpus_signaled, flush_self, 0, 0);
2314
2315 mp_enable_preemption();
2316 }
2317
2318
2319 /*
2320 * Called with pmap locked, we:
2321 * - scan through per-cpu data to see which other cpus need to flush
2322 * - send an IPI to each non-idle cpu to be flushed
2323 * - wait for all to signal back that they are inactive or we see that
2324 * they are at a safe point (idle).
2325 * - flush the local tlb if active for this pmap
2326 * - return ... the caller will unlock the pmap
2327 */
2328
2329 void
2330 pmap_flush_tlbs(pmap_t pmap, vm_map_offset_t startv, vm_map_offset_t endv, int options, pmap_flush_context *pfc)
2331 {
2332 unsigned int cpu;
2333 unsigned int cpu_bit;
2334 cpumask_t cpus_to_signal;
2335 unsigned int my_cpu = cpu_number();
2336 pmap_paddr_t pmap_cr3 = pmap->pm_cr3;
2337 boolean_t flush_self = FALSE;
2338 uint64_t deadline;
2339 boolean_t pmap_is_shared = (pmap->pm_shared || (pmap == kernel_pmap));
2340 boolean_t need_global_flush = FALSE;
2341 uint32_t event_code;
2342
2343 assert((processor_avail_count < 2) ||
2344 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
2345
2346 event_code = (pmap == kernel_pmap) ? PMAP_CODE(PMAP__FLUSH_KERN_TLBS)
2347 : PMAP_CODE(PMAP__FLUSH_TLBS);
2348 PMAP_TRACE_CONSTANT(event_code | DBG_FUNC_START,
2349 pmap, options, startv, endv, 0);
2350
2351 /*
2352 * Scan other cpus for matching active or task CR3.
2353 * For idle cpus (with no active map) we mark them invalid but
2354 * don't signal -- they'll check as they go busy.
2355 */
2356 cpus_to_signal = 0;
2357
2358 if (pmap_pcid_ncpus) {
2359 if (pmap_is_shared)
2360 need_global_flush = TRUE;
2361 pmap_pcid_invalidate_all_cpus(pmap);
2362 mfence();
2363 }
2364 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2365 if (!cpu_datap(cpu)->cpu_running)
2366 continue;
2367 uint64_t cpu_active_cr3 = CPU_GET_ACTIVE_CR3(cpu);
2368 uint64_t cpu_task_cr3 = CPU_GET_TASK_CR3(cpu);
2369
2370 if ((pmap_cr3 == cpu_task_cr3) ||
2371 (pmap_cr3 == cpu_active_cr3) ||
2372 (pmap_is_shared)) {
2373
2374 if (options & PMAP_DELAY_TLB_FLUSH) {
2375 if (need_global_flush == TRUE)
2376 pfc->pfc_invalid_global |= cpu_bit;
2377 pfc->pfc_cpus |= cpu_bit;
2378
2379 continue;
2380 }
2381 if (cpu == my_cpu) {
2382 flush_self = TRUE;
2383 continue;
2384 }
2385 if (need_global_flush == TRUE)
2386 cpu_datap(cpu)->cpu_tlb_invalid_global = TRUE;
2387 else
2388 cpu_datap(cpu)->cpu_tlb_invalid_local = TRUE;
2389 mfence();
2390
2391 /*
2392 * We don't need to signal processors which will flush
2393 * lazily at the idle state or kernel boundary.
2394 * For example, if we're invalidating the kernel pmap,
2395 * processors currently in userspace don't need to flush
2396 * their TLBs until the next time they enter the kernel.
2397 * Alterations to the address space of a task active
2398 * on a remote processor result in a signal, to
2399 * account for copy operations. (There may be room
2400 * for optimization in such cases).
2401 * The order of the loads below with respect
2402 * to the store to the "cpu_tlb_invalid" field above
2403 * is important--hence the barrier.
2404 */
2405 if (CPU_CR3_IS_ACTIVE(cpu) &&
2406 (pmap_cr3 == CPU_GET_ACTIVE_CR3(cpu) ||
2407 pmap->pm_shared ||
2408 (pmap_cr3 == CPU_GET_TASK_CR3(cpu)))) {
2409 cpus_to_signal |= cpu_bit;
2410 i386_signal_cpu(cpu, MP_TLB_FLUSH, ASYNC);
2411 }
2412 }
2413 }
2414 if ((options & PMAP_DELAY_TLB_FLUSH))
2415 goto out;
2416
2417 /*
2418 * Flush local tlb if required.
2419 * Do this now to overlap with other processors responding.
2420 */
2421 if (flush_self) {
2422 if (pmap_pcid_ncpus) {
2423 pmap_pcid_validate_cpu(pmap, my_cpu);
2424 if (pmap_is_shared)
2425 tlb_flush_global();
2426 else
2427 flush_tlb_raw();
2428 }
2429 else
2430 flush_tlb_raw();
2431 }
2432
2433 if (cpus_to_signal) {
2434 cpumask_t cpus_to_respond = cpus_to_signal;
2435
2436 deadline = mach_absolute_time() +
2437 (TLBTimeOut ? TLBTimeOut : LockTimeOut);
2438 boolean_t is_timeout_traced = FALSE;
2439
2440 /*
2441 * Wait for those other cpus to acknowledge
2442 */
2443 while (cpus_to_respond != 0) {
2444 long orig_acks = 0;
2445
2446 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2447 /* Consider checking local/global invalidity
2448 * as appropriate in the PCID case.
2449 */
2450 if ((cpus_to_respond & cpu_bit) != 0) {
2451 if (!cpu_datap(cpu)->cpu_running ||
2452 cpu_datap(cpu)->cpu_tlb_invalid == FALSE ||
2453 !CPU_CR3_IS_ACTIVE(cpu)) {
2454 cpus_to_respond &= ~cpu_bit;
2455 }
2456 cpu_pause();
2457 }
2458 if (cpus_to_respond == 0)
2459 break;
2460 }
2461 if (cpus_to_respond && (mach_absolute_time() > deadline)) {
2462 if (machine_timeout_suspended())
2463 continue;
2464 if (TLBTimeOut == 0) {
2465 /* cut tracepoint but don't panic */
2466 if (is_timeout_traced)
2467 continue;
2468 PMAP_TRACE_CONSTANT(
2469 PMAP_CODE(PMAP__FLUSH_TLBS_TO),
2470 pmap, cpus_to_signal, cpus_to_respond, 0, 0);
2471 is_timeout_traced = TRUE;
2472 continue;
2473 }
2474 pmap_tlb_flush_timeout = TRUE;
2475 orig_acks = NMIPI_acks;
2476 mp_cpus_NMIPI(cpus_to_respond);
2477
2478 panic("TLB invalidation IPI timeout: "
2479 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%lx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2480 cpus_to_respond, orig_acks, NMIPI_acks);
2481 }
2482 }
2483 }
2484
2485 if (__improbable((pmap == kernel_pmap) && (flush_self != TRUE))) {
2486 panic("pmap_flush_tlbs: pmap == kernel_pmap && flush_self != TRUE; kernel CR3: 0x%llX, pmap_cr3: 0x%llx, CPU active CR3: 0x%llX, CPU Task Map: %d", kernel_pmap->pm_cr3, pmap_cr3, current_cpu_datap()->cpu_active_cr3, current_cpu_datap()->cpu_task_map);
2487 }
2488
2489 out:
2490 PMAP_TRACE_CONSTANT(event_code | DBG_FUNC_END,
2491 pmap, cpus_to_signal, startv, endv, 0);
2492
2493 }
2494
2495 void
2496 process_pmap_updates(void)
2497 {
2498 int ccpu = cpu_number();
2499 pmap_assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2500 if (pmap_pcid_ncpus) {
2501 pmap_pcid_validate_current();
2502 if (cpu_datap(ccpu)->cpu_tlb_invalid_global) {
2503 cpu_datap(ccpu)->cpu_tlb_invalid = FALSE;
2504 tlb_flush_global();
2505 }
2506 else {
2507 cpu_datap(ccpu)->cpu_tlb_invalid_local = FALSE;
2508 flush_tlb_raw();
2509 }
2510 }
2511 else {
2512 current_cpu_datap()->cpu_tlb_invalid = FALSE;
2513 flush_tlb_raw();
2514 }
2515
2516 mfence();
2517 }
2518
2519 void
2520 pmap_update_interrupt(void)
2521 {
2522 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT) | DBG_FUNC_START,
2523 0, 0, 0, 0, 0);
2524
2525 if (current_cpu_datap()->cpu_tlb_invalid)
2526 process_pmap_updates();
2527
2528 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT) | DBG_FUNC_END,
2529 0, 0, 0, 0, 0);
2530 }
2531
2532 #include <mach/mach_vm.h> /* mach_vm_region_recurse() */
2533 /* Scan kernel pmap for W+X PTEs, scan kernel VM map for W+X map entries
2534 * and identify ranges with mismatched VM permissions and PTE permissions
2535 */
2536 kern_return_t
2537 pmap_permissions_verify(pmap_t ipmap, vm_map_t ivmmap, vm_offset_t sv, vm_offset_t ev) {
2538 vm_offset_t cv = sv;
2539 kern_return_t rv = KERN_SUCCESS;
2540 uint64_t skip4 = 0, skip2 = 0;
2541
2542 sv &= ~PAGE_MASK_64;
2543 ev &= ~PAGE_MASK_64;
2544 while (cv < ev) {
2545 if (__improbable((cv > 0x00007FFFFFFFFFFFULL) &&
2546 (cv < 0xFFFF800000000000ULL))) {
2547 cv = 0xFFFF800000000000ULL;
2548 }
2549 /* Potential inconsistencies from not holding pmap lock
2550 * but harmless for the moment.
2551 */
2552 if (((cv & PML4MASK) == 0) && (pmap64_pml4(ipmap, cv) == 0)) {
2553 if ((cv + NBPML4) > cv)
2554 cv += NBPML4;
2555 else
2556 break;
2557 skip4++;
2558 continue;
2559 }
2560 if (((cv & PDMASK) == 0) && (pmap_pde(ipmap, cv) == 0)) {
2561 if ((cv + NBPD) > cv)
2562 cv += NBPD;
2563 else
2564 break;
2565 skip2++;
2566 continue;
2567 }
2568
2569 pt_entry_t *ptep = pmap_pte(ipmap, cv);
2570 if (ptep && (*ptep & INTEL_PTE_VALID)) {
2571 if (*ptep & INTEL_PTE_WRITE) {
2572 if (!(*ptep & INTEL_PTE_NX)) {
2573 kprintf("W+X PTE at 0x%lx, P4: 0x%llx, P3: 0x%llx, P2: 0x%llx, PT: 0x%llx, VP: %u\n", cv, *pmap64_pml4(ipmap, cv), *pmap64_pdpt(ipmap, cv), *pmap64_pde(ipmap, cv), *ptep, pmap_valid_page((ppnum_t)(i386_btop(pte_to_pa(*ptep)))));
2574 rv = KERN_FAILURE;
2575 }
2576 }
2577 }
2578 cv += PAGE_SIZE;
2579 }
2580 kprintf("Completed pmap scan\n");
2581 cv = sv;
2582
2583 struct vm_region_submap_info_64 vbr;
2584 mach_msg_type_number_t vbrcount = 0;
2585 mach_vm_size_t vmsize;
2586 vm_prot_t prot;
2587 uint32_t nesting_depth = 0;
2588 kern_return_t kret;
2589
2590 while (cv < ev) {
2591
2592 for (;;) {
2593 vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
2594 if((kret = mach_vm_region_recurse(ivmmap,
2595 (mach_vm_address_t *) &cv, &vmsize, &nesting_depth,
2596 (vm_region_recurse_info_t)&vbr,
2597 &vbrcount)) != KERN_SUCCESS) {
2598 break;
2599 }
2600
2601 if(vbr.is_submap) {
2602 nesting_depth++;
2603 continue;
2604 } else {
2605 break;
2606 }
2607 }
2608
2609 if(kret != KERN_SUCCESS)
2610 break;
2611
2612 prot = vbr.protection;
2613
2614 if ((prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) == (VM_PROT_WRITE | VM_PROT_EXECUTE)) {
2615 kprintf("W+X map entry at address 0x%lx\n", cv);
2616 rv = KERN_FAILURE;
2617 }
2618
2619 if (prot) {
2620 vm_offset_t pcv;
2621 for (pcv = cv; pcv < cv + vmsize; pcv += PAGE_SIZE) {
2622 pt_entry_t *ptep = pmap_pte(ipmap, pcv);
2623 vm_prot_t tprot;
2624
2625 if ((ptep == NULL) || !(*ptep & INTEL_PTE_VALID))
2626 continue;
2627 tprot = VM_PROT_READ;
2628 if (*ptep & INTEL_PTE_WRITE)
2629 tprot |= VM_PROT_WRITE;
2630 if ((*ptep & INTEL_PTE_NX) == 0)
2631 tprot |= VM_PROT_EXECUTE;
2632 if (tprot != prot) {
2633 kprintf("PTE/map entry permissions mismatch at address 0x%lx, pte: 0x%llx, protection: 0x%x\n", pcv, *ptep, prot);
2634 rv = KERN_FAILURE;
2635 }
2636 }
2637 }
2638 cv += vmsize;
2639 }
2640 return rv;
2641 }