]> git.saurik.com Git - apple/xnu.git/blob - osfmk/x86_64/pmap.c
xnu-1699.32.7.tar.gz
[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_kdb.h>
93 #include <mach_ldebug.h>
94
95 #include <libkern/OSAtomic.h>
96
97 #include <mach/machine/vm_types.h>
98
99 #include <mach/boolean.h>
100 #include <kern/thread.h>
101 #include <kern/zalloc.h>
102 #include <kern/queue.h>
103 #include <kern/mach_param.h>
104
105 #include <kern/lock.h>
106 #include <kern/kalloc.h>
107 #include <kern/spl.h>
108
109 #include <vm/pmap.h>
110 #include <vm/vm_map.h>
111 #include <vm/vm_kern.h>
112 #include <mach/vm_param.h>
113 #include <mach/vm_prot.h>
114 #include <vm/vm_object.h>
115 #include <vm/vm_page.h>
116
117 #include <mach/machine/vm_param.h>
118 #include <machine/thread.h>
119
120 #include <kern/misc_protos.h> /* prototyping */
121 #include <i386/misc_protos.h>
122 #include <i386/i386_lowmem.h>
123 #include <x86_64/lowglobals.h>
124
125 #include <i386/cpuid.h>
126 #include <i386/cpu_data.h>
127 #include <i386/cpu_number.h>
128 #include <i386/machine_cpu.h>
129 #include <i386/seg.h>
130 #include <i386/serial_io.h>
131 #include <i386/cpu_capabilities.h>
132 #include <i386/machine_routines.h>
133 #include <i386/proc_reg.h>
134 #include <i386/tsc.h>
135 #include <i386/pmap_internal.h>
136 #include <i386/pmap_pcid.h>
137
138 #if MACH_KDB
139 #include <ddb/db_command.h>
140 #include <ddb/db_output.h>
141 #include <ddb/db_sym.h>
142 #include <ddb/db_print.h>
143 #endif /* MACH_KDB */
144
145 #include <vm/vm_protos.h>
146
147 #include <i386/mp.h>
148 #include <i386/mp_desc.h>
149
150
151 #ifdef IWANTTODEBUG
152 #undef DEBUG
153 #define DEBUG 1
154 #define POSTCODE_DELAY 1
155 #include <i386/postcode.h>
156 #endif /* IWANTTODEBUG */
157
158 #ifdef PMAP_DEBUG
159 #define DBG(x...) kprintf("DBG: " x)
160 #else
161 #define DBG(x...)
162 #endif
163 /* Compile time assert to ensure adjacency/alignment of per-CPU data fields used
164 * in the trampolines for kernel/user boundary TLB coherency.
165 */
166 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];
167 boolean_t pmap_trace = FALSE;
168
169 boolean_t no_shared_cr3 = DEBUG; /* TRUE for DEBUG by default */
170
171 int nx_enabled = 1; /* enable no-execute protection */
172 int allow_data_exec = VM_ABI_32; /* 32-bit apps may execute data by default, 64-bit apps may not */
173 int allow_stack_exec = 0; /* No apps may execute from the stack by default */
174
175 const boolean_t cpu_64bit = TRUE; /* Mais oui! */
176
177 uint64_t max_preemption_latency_tsc = 0;
178
179 pv_hashed_entry_t *pv_hash_table; /* hash lists */
180
181 uint32_t npvhash = 0;
182
183 pv_hashed_entry_t pv_hashed_free_list = PV_HASHED_ENTRY_NULL;
184 pv_hashed_entry_t pv_hashed_kern_free_list = PV_HASHED_ENTRY_NULL;
185 decl_simple_lock_data(,pv_hashed_free_list_lock)
186 decl_simple_lock_data(,pv_hashed_kern_free_list_lock)
187 decl_simple_lock_data(,pv_hash_table_lock)
188
189 zone_t pv_hashed_list_zone; /* zone of pv_hashed_entry structures */
190
191 /*
192 * First and last physical addresses that we maintain any information
193 * for. Initialized to zero so that pmap operations done before
194 * pmap_init won't touch any non-existent structures.
195 */
196 boolean_t pmap_initialized = FALSE;/* Has pmap_init completed? */
197
198 static struct vm_object kptobj_object_store;
199 static struct vm_object kpml4obj_object_store;
200 static struct vm_object kpdptobj_object_store;
201
202 /*
203 * Array of physical page attribites for managed pages.
204 * One byte per physical page.
205 */
206 char *pmap_phys_attributes;
207 unsigned int last_managed_page = 0;
208
209 /*
210 * Amount of virtual memory mapped by one
211 * page-directory entry.
212 */
213
214 uint64_t pde_mapped_size = PDE_MAPPED_SIZE;
215
216 unsigned pmap_memory_region_count;
217 unsigned pmap_memory_region_current;
218
219 pmap_memory_region_t pmap_memory_regions[PMAP_MEMORY_REGIONS_SIZE];
220
221 /*
222 * Other useful macros.
223 */
224 #define current_pmap() (vm_map_pmap(current_thread()->map))
225
226 struct pmap kernel_pmap_store;
227 pmap_t kernel_pmap;
228
229 pd_entry_t high_shared_pde;
230 pd_entry_t commpage64_pde;
231
232 struct zone *pmap_zone; /* zone of pmap structures */
233
234 struct zone *pmap_anchor_zone;
235 int pmap_debug = 0; /* flag for debugging prints */
236
237 unsigned int inuse_ptepages_count = 0;
238 long long alloc_ptepages_count __attribute__((aligned(8))) = 0; /* aligned for atomic access */
239 unsigned int bootstrap_wired_pages = 0;
240 int pt_fake_zone_index = -1;
241
242 extern long NMIPI_acks;
243
244 boolean_t kernel_text_ps_4K = TRUE;
245 boolean_t wpkernel = TRUE;
246
247 extern char end;
248
249 static int nkpt;
250
251 pt_entry_t *DMAP1, *DMAP2;
252 caddr_t DADDR1;
253 caddr_t DADDR2;
254
255 /*
256 * unlinks the pv_hashed_entry_t pvh from the singly linked hash chain.
257 * properly deals with the anchor.
258 * must be called with the hash locked, does not unlock it
259 */
260
261
262 /*
263 * Map memory at initialization. The physical addresses being
264 * mapped are not managed and are never unmapped.
265 *
266 * For now, VM is already on, we only need to map the
267 * specified memory.
268 */
269 vm_offset_t
270 pmap_map(
271 vm_offset_t virt,
272 vm_map_offset_t start_addr,
273 vm_map_offset_t end_addr,
274 vm_prot_t prot,
275 unsigned int flags)
276 {
277 int ps;
278
279 ps = PAGE_SIZE;
280 while (start_addr < end_addr) {
281 pmap_enter(kernel_pmap, (vm_map_offset_t)virt,
282 (ppnum_t) i386_btop(start_addr), prot, flags, FALSE);
283 virt += ps;
284 start_addr += ps;
285 }
286 return(virt);
287 }
288
289 extern char *first_avail;
290 extern vm_offset_t virtual_avail, virtual_end;
291 extern pmap_paddr_t avail_start, avail_end;
292 extern vm_offset_t sHIB;
293 extern vm_offset_t eHIB;
294 extern vm_offset_t stext;
295 extern vm_offset_t etext;
296 extern vm_offset_t sdata;
297
298 extern void *KPTphys;
299
300 boolean_t pmap_smep_enabled = FALSE;
301
302 void
303 pmap_cpu_init(void)
304 {
305 /*
306 * Here early in the life of a processor (from cpu_mode_init()).
307 * Ensure global page feature is disabled at this point.
308 */
309
310 set_cr4(get_cr4() &~ CR4_PGE);
311
312 /*
313 * Initialize the per-cpu, TLB-related fields.
314 */
315 current_cpu_datap()->cpu_kernel_cr3 = kernel_pmap->pm_cr3;
316 current_cpu_datap()->cpu_active_cr3 = kernel_pmap->pm_cr3;
317 current_cpu_datap()->cpu_tlb_invalid = FALSE;
318 current_cpu_datap()->cpu_task_map = TASK_MAP_64BIT;
319 pmap_pcid_configure();
320 if (cpuid_leaf7_features() & CPUID_LEAF7_FEATURE_SMEP) {
321 boolean_t nsmep;
322 if (!PE_parse_boot_argn("-pmap_smep_disable", &nsmep, sizeof(nsmep))) {
323 set_cr4(get_cr4() | CR4_SMEP);
324 pmap_smep_enabled = TRUE;
325 }
326 }
327 }
328
329
330
331 /*
332 * Bootstrap the system enough to run with virtual memory.
333 * Map the kernel's code and data, and allocate the system page table.
334 * Called with mapping OFF. Page_size must already be set.
335 */
336
337 void
338 pmap_bootstrap(
339 __unused vm_offset_t load_start,
340 __unused boolean_t IA32e)
341 {
342 #if NCOPY_WINDOWS > 0
343 vm_offset_t va;
344 int i;
345 #endif
346 assert(IA32e);
347
348 vm_last_addr = VM_MAX_KERNEL_ADDRESS; /* Set the highest address
349 * known to VM */
350 /*
351 * The kernel's pmap is statically allocated so we don't
352 * have to use pmap_create, which is unlikely to work
353 * correctly at this part of the boot sequence.
354 */
355
356 kernel_pmap = &kernel_pmap_store;
357 kernel_pmap->ref_count = 1;
358 kernel_pmap->nx_enabled = FALSE;
359 kernel_pmap->pm_task_map = TASK_MAP_64BIT;
360 kernel_pmap->pm_obj = (vm_object_t) NULL;
361 kernel_pmap->dirbase = (pd_entry_t *)((uintptr_t)IdlePTD);
362 kernel_pmap->pm_pdpt = (pd_entry_t *) ((uintptr_t)IdlePDPT);
363 kernel_pmap->pm_pml4 = IdlePML4;
364 kernel_pmap->pm_cr3 = (uintptr_t)ID_MAP_VTOP(IdlePML4);
365 pmap_pcid_initialize_kernel(kernel_pmap);
366
367
368
369 current_cpu_datap()->cpu_kernel_cr3 = (addr64_t) kernel_pmap->pm_cr3;
370
371 nkpt = NKPT;
372 OSAddAtomic(NKPT, &inuse_ptepages_count);
373 OSAddAtomic64(NKPT, &alloc_ptepages_count);
374 bootstrap_wired_pages = NKPT;
375
376 virtual_avail = (vm_offset_t)(VM_MIN_KERNEL_ADDRESS) + (vm_offset_t)first_avail;
377 virtual_end = (vm_offset_t)(VM_MAX_KERNEL_ADDRESS);
378
379 #if NCOPY_WINDOWS > 0
380 /*
381 * Reserve some special page table entries/VA space for temporary
382 * mapping of pages.
383 */
384 #define SYSMAP(c, p, v, n) \
385 v = (c)va; va += ((n)*INTEL_PGBYTES);
386
387 va = virtual_avail;
388
389 for (i=0; i<PMAP_NWINDOWS; i++) {
390 #if 1
391 kprintf("trying to do SYSMAP idx %d %p\n", i,
392 current_cpu_datap());
393 kprintf("cpu_pmap %p\n", current_cpu_datap()->cpu_pmap);
394 kprintf("mapwindow %p\n", current_cpu_datap()->cpu_pmap->mapwindow);
395 kprintf("two stuff %p %p\n",
396 (void *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP),
397 (void *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR));
398 #endif
399 SYSMAP(caddr_t,
400 (current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP),
401 (current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR),
402 1);
403 current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP =
404 &(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP_store);
405 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
406 }
407
408 /* DMAP user for debugger */
409 SYSMAP(caddr_t, DMAP1, DADDR1, 1);
410 SYSMAP(caddr_t, DMAP2, DADDR2, 1); /* XXX temporary - can remove */
411
412 virtual_avail = va;
413 #endif
414
415 if (PE_parse_boot_argn("npvhash", &npvhash, sizeof (npvhash))) {
416 if (0 != ((npvhash + 1) & npvhash)) {
417 kprintf("invalid hash %d, must be ((2^N)-1), "
418 "using default %d\n", npvhash, NPVHASH);
419 npvhash = NPVHASH;
420 }
421 } else {
422 npvhash = NPVHASH;
423 }
424
425 simple_lock_init(&kernel_pmap->lock, 0);
426 simple_lock_init(&pv_hashed_free_list_lock, 0);
427 simple_lock_init(&pv_hashed_kern_free_list_lock, 0);
428 simple_lock_init(&pv_hash_table_lock,0);
429
430 pmap_cpu_init();
431
432 if (pmap_pcid_ncpus)
433 printf("PMAP: PCID enabled\n");
434
435 if (pmap_smep_enabled)
436 printf("PMAP: Supervisor Mode Execute Protection enabled\n");
437
438 boot_args *args = (boot_args *)PE_state.bootArgs;
439 if (args->efiMode == kBootArgsEfiMode32) {
440 printf("EFI32: kernel virtual space limited to 4GB\n");
441 virtual_end = VM_MAX_KERNEL_ADDRESS_EFI32;
442 }
443 kprintf("Kernel virtual space from 0x%lx to 0x%lx.\n",
444 (long)KERNEL_BASE, (long)virtual_end);
445 kprintf("Available physical space from 0x%llx to 0x%llx\n",
446 avail_start, avail_end);
447
448 /*
449 * The -no_shared_cr3 boot-arg is a debugging feature (set by default
450 * in the DEBUG kernel) to force the kernel to switch to its own map
451 * (and cr3) when control is in kernelspace. The kernel's map does not
452 * include (i.e. share) userspace so wild references will cause
453 * a panic. Only copyin and copyout are exempt from this.
454 */
455 (void) PE_parse_boot_argn("-no_shared_cr3",
456 &no_shared_cr3, sizeof (no_shared_cr3));
457 if (no_shared_cr3)
458 kprintf("Kernel not sharing user map\n");
459
460 #ifdef PMAP_TRACES
461 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace, sizeof (pmap_trace))) {
462 kprintf("Kernel traces for pmap operations enabled\n");
463 }
464 #endif /* PMAP_TRACES */
465 }
466
467 void
468 pmap_virtual_space(
469 vm_offset_t *startp,
470 vm_offset_t *endp)
471 {
472 *startp = virtual_avail;
473 *endp = virtual_end;
474 }
475
476 /*
477 * Initialize the pmap module.
478 * Called by vm_init, to initialize any structures that the pmap
479 * system needs to map virtual memory.
480 */
481 void
482 pmap_init(void)
483 {
484 long npages;
485 vm_offset_t addr;
486 vm_size_t s, vsize;
487 vm_map_offset_t vaddr;
488 ppnum_t ppn;
489
490
491 kernel_pmap->pm_obj_pml4 = &kpml4obj_object_store;
492 _vm_object_allocate((vm_object_size_t)NPML4PGS, &kpml4obj_object_store);
493
494 kernel_pmap->pm_obj_pdpt = &kpdptobj_object_store;
495 _vm_object_allocate((vm_object_size_t)NPDPTPGS, &kpdptobj_object_store);
496
497 kernel_pmap->pm_obj = &kptobj_object_store;
498 _vm_object_allocate((vm_object_size_t)NPDEPGS, &kptobj_object_store);
499
500 /*
501 * Allocate memory for the pv_head_table and its lock bits,
502 * the modify bit array, and the pte_page table.
503 */
504
505 /*
506 * zero bias all these arrays now instead of off avail_start
507 * so we cover all memory
508 */
509
510 npages = i386_btop(avail_end);
511 s = (vm_size_t) (sizeof(struct pv_rooted_entry) * npages
512 + (sizeof (struct pv_hashed_entry_t *) * (npvhash+1))
513 + pv_lock_table_size(npages)
514 + pv_hash_lock_table_size((npvhash+1))
515 + npages);
516
517 s = round_page(s);
518 if (kernel_memory_allocate(kernel_map, &addr, s, 0,
519 KMA_KOBJECT | KMA_PERMANENT)
520 != KERN_SUCCESS)
521 panic("pmap_init");
522
523 memset((char *)addr, 0, s);
524
525 vaddr = addr;
526 vsize = s;
527
528 #if PV_DEBUG
529 if (0 == npvhash) panic("npvhash not initialized");
530 #endif
531
532 /*
533 * Allocate the structures first to preserve word-alignment.
534 */
535 pv_head_table = (pv_rooted_entry_t) addr;
536 addr = (vm_offset_t) (pv_head_table + npages);
537
538 pv_hash_table = (pv_hashed_entry_t *)addr;
539 addr = (vm_offset_t) (pv_hash_table + (npvhash + 1));
540
541 pv_lock_table = (char *) addr;
542 addr = (vm_offset_t) (pv_lock_table + pv_lock_table_size(npages));
543
544 pv_hash_lock_table = (char *) addr;
545 addr = (vm_offset_t) (pv_hash_lock_table + pv_hash_lock_table_size((npvhash+1)));
546
547 pmap_phys_attributes = (char *) addr;
548
549 ppnum_t last_pn = i386_btop(avail_end);
550 unsigned int i;
551 pmap_memory_region_t *pmptr = pmap_memory_regions;
552 for (i = 0; i < pmap_memory_region_count; i++, pmptr++) {
553 if (pmptr->type != kEfiConventionalMemory)
554 continue;
555 unsigned int pn;
556 for (pn = pmptr->base; pn <= pmptr->end; pn++) {
557 if (pn < last_pn) {
558 pmap_phys_attributes[pn] |= PHYS_MANAGED;
559
560 if (pn > last_managed_page)
561 last_managed_page = pn;
562
563 if (pn >= lowest_hi && pn <= highest_hi)
564 pmap_phys_attributes[pn] |= PHYS_NOENCRYPT;
565 }
566 }
567 }
568 while (vsize) {
569 ppn = pmap_find_phys(kernel_pmap, vaddr);
570
571 pmap_phys_attributes[ppn] |= PHYS_NOENCRYPT;
572
573 vaddr += PAGE_SIZE;
574 vsize -= PAGE_SIZE;
575 }
576 /*
577 * Create the zone of physical maps,
578 * and of the physical-to-virtual entries.
579 */
580 s = (vm_size_t) sizeof(struct pmap);
581 pmap_zone = zinit(s, 400*s, 4096, "pmap"); /* XXX */
582 zone_change(pmap_zone, Z_NOENCRYPT, TRUE);
583
584 pmap_anchor_zone = zinit(PAGE_SIZE, task_max, PAGE_SIZE, "pagetable anchors");
585 zone_change(pmap_anchor_zone, Z_NOENCRYPT, TRUE);
586
587 #if ZONE_DEBUG
588 /* The anchor is required to be page aligned. Zone debugging adds
589 * padding which may violate that requirement. Disable it
590 * to avoid assumptions.
591 */
592 zone_debug_disable(pmap_anchor_zone);
593 #endif
594
595 s = (vm_size_t) sizeof(struct pv_hashed_entry);
596 pv_hashed_list_zone = zinit(s, 10000*s /* Expandable zone */,
597 4096 * 3 /* LCM x86_64*/, "pv_list");
598 zone_change(pv_hashed_list_zone, Z_NOENCRYPT, TRUE);
599
600 /* create pv entries for kernel pages mapped by low level
601 startup code. these have to exist so we can pmap_remove()
602 e.g. kext pages from the middle of our addr space */
603
604 vaddr = (vm_map_offset_t) VM_MIN_KERNEL_ADDRESS;
605 for (ppn = VM_MIN_KERNEL_PAGE; ppn < i386_btop(avail_start); ppn++) {
606 pv_rooted_entry_t pv_e;
607
608 pv_e = pai_to_pvh(ppn);
609 pv_e->va = vaddr;
610 vaddr += PAGE_SIZE;
611 pv_e->pmap = kernel_pmap;
612 queue_init(&pv_e->qlink);
613 }
614 pmap_initialized = TRUE;
615
616 max_preemption_latency_tsc = tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS, tscFCvtn2t);
617
618 /*
619 * Ensure the kernel's PML4 entry exists for the basement
620 * before this is shared with any user.
621 */
622 pmap_expand_pml4(kernel_pmap, KERNEL_BASEMENT);
623 }
624
625 /*
626 * Called once VM is fully initialized so that we can release unused
627 * sections of low memory to the general pool.
628 * Also complete the set-up of identity-mapped sections of the kernel:
629 * 1) write-protect kernel text
630 * 2) map kernel text using large pages if possible
631 * 3) read and write-protect page zero (for K32)
632 * 4) map the global page at the appropriate virtual address.
633 *
634 * Use of large pages
635 * ------------------
636 * To effectively map and write-protect all kernel text pages, the text
637 * must be 2M-aligned at the base, and the data section above must also be
638 * 2M-aligned. That is, there's padding below and above. This is achieved
639 * through linker directives. Large pages are used only if this alignment
640 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
641 * memory layout is:
642 *
643 * : :
644 * | __DATA |
645 * sdata: ================== 2Meg
646 * | |
647 * | zero-padding |
648 * | |
649 * etext: ------------------
650 * | |
651 * : :
652 * | |
653 * | __TEXT |
654 * | |
655 * : :
656 * | |
657 * stext: ================== 2Meg
658 * | |
659 * | zero-padding |
660 * | |
661 * eHIB: ------------------
662 * | __HIB |
663 * : :
664 *
665 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
666 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
667 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
668 * The now unused level-1 PTE pages are also freed.
669 */
670 extern uint32_t pmap_reserved_ranges;
671 void
672 pmap_lowmem_finalize(void)
673 {
674 spl_t spl;
675 int i;
676
677 /* Check the kernel is linked at the expected base address */
678 if (i386_btop(kvtophys((vm_offset_t) &IdlePML4)) !=
679 I386_KERNEL_IMAGE_BASE_PAGE)
680 panic("pmap_lowmem_finalize() unexpected kernel base address");
681
682 /*
683 * Update wired memory statistics for early boot pages
684 */
685 PMAP_ZINFO_PALLOC(bootstrap_wired_pages * PAGE_SIZE);
686
687 /*
688 * Free all pages in pmap regions below the base:
689 * rdar://6332712
690 * We can't free all the pages to VM that EFI reports available.
691 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
692 * There's also a size miscalculation here: pend is one page less
693 * than it should be but this is not fixed to be backwards
694 * compatible.
695 * Due to this current EFI limitation, we take only the first
696 * entry in the memory region table. However, the loop is retained
697 * (with the intended termination criteria commented out) in the
698 * hope that some day we can free all low-memory ranges.
699 */
700 for (i = 0;
701 // pmap_memory_regions[i].end <= I386_KERNEL_IMAGE_BASE_PAGE;
702 i < 1 && (pmap_reserved_ranges == 0);
703 i++) {
704 vm_offset_t pbase = (vm_offset_t)i386_ptob(pmap_memory_regions[i].base);
705 vm_offset_t pend = (vm_offset_t)i386_ptob(pmap_memory_regions[i].end);
706 // vm_offset_t pend = i386_ptob(pmap_memory_regions[i].end+1);
707
708 DBG("ml_static_mfree(%p,%p) for pmap region %d\n",
709 (void *) ml_static_ptovirt(pbase),
710 (void *) (pend - pbase), i);
711 ml_static_mfree(ml_static_ptovirt(pbase), pend - pbase);
712 }
713
714 /*
715 * If text and data are both 2MB-aligned,
716 * we can map text with large-pages,
717 * unless the -kernel_text_ps_4K boot-arg overrides.
718 */
719 if ((stext & I386_LPGMASK) == 0 && (sdata & I386_LPGMASK) == 0) {
720 kprintf("Kernel text is 2MB aligned");
721 kernel_text_ps_4K = FALSE;
722 if (PE_parse_boot_argn("-kernel_text_ps_4K",
723 &kernel_text_ps_4K,
724 sizeof (kernel_text_ps_4K)))
725 kprintf(" but will be mapped with 4K pages\n");
726 else
727 kprintf(" and will be mapped with 2M pages\n");
728 }
729
730 (void) PE_parse_boot_argn("wpkernel", &wpkernel, sizeof (wpkernel));
731 if (wpkernel)
732 kprintf("Kernel text %p-%p to be write-protected\n",
733 (void *) stext, (void *) etext);
734
735 spl = splhigh();
736
737 /*
738 * Scan over text if mappings are to be changed:
739 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
740 * - Change to large-pages if possible and not overriden.
741 */
742 if (kernel_text_ps_4K && wpkernel) {
743 vm_offset_t myva;
744 for (myva = stext; myva < etext; myva += PAGE_SIZE) {
745 pt_entry_t *ptep;
746
747 ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)myva);
748 if (ptep)
749 pmap_store_pte(ptep, *ptep & ~INTEL_PTE_RW);
750 }
751 }
752
753 if (!kernel_text_ps_4K) {
754 vm_offset_t myva;
755
756 /*
757 * Release zero-filled page padding used for 2M-alignment.
758 */
759 DBG("ml_static_mfree(%p,%p) for padding below text\n",
760 (void *) eHIB, (void *) (stext - eHIB));
761 ml_static_mfree(eHIB, stext - eHIB);
762 DBG("ml_static_mfree(%p,%p) for padding above text\n",
763 (void *) etext, (void *) (sdata - etext));
764 ml_static_mfree(etext, sdata - etext);
765
766 /*
767 * Coalesce text pages into large pages.
768 */
769 for (myva = stext; myva < sdata; myva += I386_LPGBYTES) {
770 pt_entry_t *ptep;
771 vm_offset_t pte_phys;
772 pt_entry_t *pdep;
773 pt_entry_t pde;
774
775 pdep = pmap_pde(kernel_pmap, (vm_map_offset_t)myva);
776 ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)myva);
777 DBG("myva: %p pdep: %p ptep: %p\n",
778 (void *) myva, (void *) pdep, (void *) ptep);
779 if ((*ptep & INTEL_PTE_VALID) == 0)
780 continue;
781 pte_phys = (vm_offset_t)(*ptep & PG_FRAME);
782 pde = *pdep & PTMASK; /* page attributes from pde */
783 pde |= INTEL_PTE_PS; /* make it a 2M entry */
784 pde |= pte_phys; /* take page frame from pte */
785
786 if (wpkernel)
787 pde &= ~INTEL_PTE_RW;
788 DBG("pmap_store_pte(%p,0x%llx)\n",
789 (void *)pdep, pde);
790 pmap_store_pte(pdep, pde);
791
792 /*
793 * Free the now-unused level-1 pte.
794 * Note: ptep is a virtual address to the pte in the
795 * recursive map. We can't use this address to free
796 * the page. Instead we need to compute its address
797 * in the Idle PTEs in "low memory".
798 */
799 vm_offset_t vm_ptep = (vm_offset_t) KPTphys
800 + (pte_phys >> PTPGSHIFT);
801 DBG("ml_static_mfree(%p,0x%x) for pte\n",
802 (void *) vm_ptep, PAGE_SIZE);
803 ml_static_mfree(vm_ptep, PAGE_SIZE);
804 }
805
806 /* Change variable read by sysctl machdep.pmap */
807 pmap_kernel_text_ps = I386_LPGBYTES;
808 }
809
810 /* map lowmem global page into fixed addr */
811 pt_entry_t *pte = NULL;
812 if (0 == (pte = pmap_pte(kernel_pmap,
813 VM_MIN_KERNEL_LOADED_ADDRESS + 0x2000)))
814 panic("lowmem pte");
815 /* make sure it is defined on page boundary */
816 assert(0 == ((vm_offset_t) &lowGlo & PAGE_MASK));
817 pmap_store_pte(pte, kvtophys((vm_offset_t)&lowGlo)
818 | INTEL_PTE_REF
819 | INTEL_PTE_MOD
820 | INTEL_PTE_WIRED
821 | INTEL_PTE_VALID
822 | INTEL_PTE_RW);
823 splx(spl);
824 if (pmap_pcid_ncpus)
825 tlb_flush_global();
826 else
827 flush_tlb_raw();
828 }
829
830 /*
831 * this function is only used for debugging fron the vm layer
832 */
833 boolean_t
834 pmap_verify_free(
835 ppnum_t pn)
836 {
837 pv_rooted_entry_t pv_h;
838 int pai;
839 boolean_t result;
840
841 assert(pn != vm_page_fictitious_addr);
842
843 if (!pmap_initialized)
844 return(TRUE);
845
846 if (pn == vm_page_guard_addr)
847 return TRUE;
848
849 pai = ppn_to_pai(pn);
850 if (!IS_MANAGED_PAGE(pai))
851 return(FALSE);
852 pv_h = pai_to_pvh(pn);
853 result = (pv_h->pmap == PMAP_NULL);
854 return(result);
855 }
856
857 boolean_t
858 pmap_is_empty(
859 pmap_t pmap,
860 vm_map_offset_t va_start,
861 vm_map_offset_t va_end)
862 {
863 vm_map_offset_t offset;
864 ppnum_t phys_page;
865
866 if (pmap == PMAP_NULL) {
867 return TRUE;
868 }
869
870 /*
871 * Check the resident page count
872 * - if it's zero, the pmap is completely empty.
873 * This short-circuit test prevents a virtual address scan which is
874 * painfully slow for 64-bit spaces.
875 * This assumes the count is correct
876 * .. the debug kernel ought to be checking perhaps by page table walk.
877 */
878 if (pmap->stats.resident_count == 0)
879 return TRUE;
880
881 for (offset = va_start;
882 offset < va_end;
883 offset += PAGE_SIZE_64) {
884 phys_page = pmap_find_phys(pmap, offset);
885 if (phys_page) {
886 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
887 "page %d at 0x%llx\n",
888 pmap, va_start, va_end, phys_page, offset);
889 return FALSE;
890 }
891 }
892
893 return TRUE;
894 }
895
896
897 /*
898 * Create and return a physical map.
899 *
900 * If the size specified for the map
901 * is zero, the map is an actual physical
902 * map, and may be referenced by the
903 * hardware.
904 *
905 * If the size specified is non-zero,
906 * the map will be used in software only, and
907 * is bounded by that size.
908 */
909 pmap_t
910 pmap_create(
911 vm_map_size_t sz,
912 boolean_t is_64bit)
913 {
914 pmap_t p;
915 vm_size_t size;
916 pml4_entry_t *pml4;
917 pml4_entry_t *kpml4;
918
919 PMAP_TRACE(PMAP_CODE(PMAP__CREATE) | DBG_FUNC_START,
920 (uint32_t) (sz>>32), (uint32_t) sz, is_64bit, 0, 0);
921
922 size = (vm_size_t) sz;
923
924 /*
925 * A software use-only map doesn't even need a map.
926 */
927
928 if (size != 0) {
929 return(PMAP_NULL);
930 }
931
932 p = (pmap_t) zalloc(pmap_zone);
933 if (PMAP_NULL == p)
934 panic("pmap_create zalloc");
935 /* Zero all fields */
936 bzero(p, sizeof(*p));
937 /* init counts now since we'll be bumping some */
938 simple_lock_init(&p->lock, 0);
939 p->stats.resident_count = 0;
940 p->stats.resident_max = 0;
941 p->stats.wired_count = 0;
942 p->ref_count = 1;
943 p->nx_enabled = 1;
944 p->pm_shared = FALSE;
945
946 p->pm_task_map = is_64bit ? TASK_MAP_64BIT : TASK_MAP_32BIT;;
947 if (pmap_pcid_ncpus)
948 pmap_pcid_initialize(p);
949 p->pm_pml4 = zalloc(pmap_anchor_zone);
950
951 pmap_assert((((uintptr_t)p->pm_pml4) & PAGE_MASK) == 0);
952
953 memset((char *)p->pm_pml4, 0, PAGE_SIZE);
954
955 p->pm_cr3 = (pmap_paddr_t)kvtophys((vm_offset_t)p->pm_pml4);
956
957 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
958
959 p->pm_obj_pml4 = vm_object_allocate((vm_object_size_t)(NPML4PGS));
960 if (NULL == p->pm_obj_pml4)
961 panic("pmap_create pdpt obj");
962
963 p->pm_obj_pdpt = vm_object_allocate((vm_object_size_t)(NPDPTPGS));
964 if (NULL == p->pm_obj_pdpt)
965 panic("pmap_create pdpt obj");
966
967 p->pm_obj = vm_object_allocate((vm_object_size_t)(NPDEPGS));
968 if (NULL == p->pm_obj)
969 panic("pmap_create pte obj");
970
971 /* All pmaps share the kernel's pml4 */
972 pml4 = pmap64_pml4(p, 0ULL);
973 kpml4 = kernel_pmap->pm_pml4;
974 pml4[KERNEL_PML4_INDEX] = kpml4[KERNEL_PML4_INDEX];
975 pml4[KERNEL_KEXTS_INDEX] = kpml4[KERNEL_KEXTS_INDEX];
976 pml4[KERNEL_PHYSMAP_INDEX] = kpml4[KERNEL_PHYSMAP_INDEX];
977
978 PMAP_TRACE(PMAP_CODE(PMAP__CREATE) | DBG_FUNC_START,
979 p, is_64bit, 0, 0, 0);
980
981 return(p);
982 }
983
984 /*
985 * Retire the given physical map from service.
986 * Should only be called if the map contains
987 * no valid mappings.
988 */
989
990 void
991 pmap_destroy(pmap_t p)
992 {
993 int c;
994
995 if (p == PMAP_NULL)
996 return;
997
998 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_START,
999 p, 0, 0, 0, 0);
1000
1001 PMAP_LOCK(p);
1002
1003 c = --p->ref_count;
1004
1005 pmap_assert((current_thread() && (current_thread()->map)) ? (current_thread()->map->pmap != p) : TRUE);
1006
1007 if (c == 0) {
1008 /*
1009 * If some cpu is not using the physical pmap pointer that it
1010 * is supposed to be (see set_dirbase), we might be using the
1011 * pmap that is being destroyed! Make sure we are
1012 * physically on the right pmap:
1013 */
1014 PMAP_UPDATE_TLBS(p, 0x0ULL, 0xFFFFFFFFFFFFF000ULL);
1015 if (pmap_pcid_ncpus)
1016 pmap_destroy_pcid_sync(p);
1017 }
1018
1019 PMAP_UNLOCK(p);
1020
1021 if (c != 0) {
1022 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_END,
1023 p, 1, 0, 0, 0);
1024 pmap_assert(p == kernel_pmap);
1025 return; /* still in use */
1026 }
1027
1028 /*
1029 * Free the memory maps, then the
1030 * pmap structure.
1031 */
1032 int inuse_ptepages = 0;
1033
1034 zfree(pmap_anchor_zone, p->pm_pml4);
1035
1036 inuse_ptepages += p->pm_obj_pml4->resident_page_count;
1037 vm_object_deallocate(p->pm_obj_pml4);
1038
1039 inuse_ptepages += p->pm_obj_pdpt->resident_page_count;
1040 vm_object_deallocate(p->pm_obj_pdpt);
1041
1042 inuse_ptepages += p->pm_obj->resident_page_count;
1043 vm_object_deallocate(p->pm_obj);
1044
1045 OSAddAtomic(-inuse_ptepages, &inuse_ptepages_count);
1046 PMAP_ZINFO_PFREE(inuse_ptepages * PAGE_SIZE);
1047
1048 zfree(pmap_zone, p);
1049
1050 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_END,
1051 0, 0, 0, 0, 0);
1052 }
1053
1054 /*
1055 * Add a reference to the specified pmap.
1056 */
1057
1058 void
1059 pmap_reference(pmap_t p)
1060 {
1061 if (p != PMAP_NULL) {
1062 PMAP_LOCK(p);
1063 p->ref_count++;
1064 PMAP_UNLOCK(p);;
1065 }
1066 }
1067
1068 /*
1069 * Remove phys addr if mapped in specified map
1070 *
1071 */
1072 void
1073 pmap_remove_some_phys(
1074 __unused pmap_t map,
1075 __unused ppnum_t pn)
1076 {
1077
1078 /* Implement to support working set code */
1079
1080 }
1081
1082
1083 /*
1084 * Set the physical protection on the
1085 * specified range of this map as requested.
1086 * Will not increase permissions.
1087 */
1088 void
1089 pmap_protect(
1090 pmap_t map,
1091 vm_map_offset_t sva,
1092 vm_map_offset_t eva,
1093 vm_prot_t prot)
1094 {
1095 pt_entry_t *pde;
1096 pt_entry_t *spte, *epte;
1097 vm_map_offset_t lva;
1098 vm_map_offset_t orig_sva;
1099 boolean_t set_NX;
1100 int num_found = 0;
1101
1102 pmap_intr_assert();
1103
1104 if (map == PMAP_NULL)
1105 return;
1106
1107 if (prot == VM_PROT_NONE) {
1108 pmap_remove(map, sva, eva);
1109 return;
1110 }
1111 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT) | DBG_FUNC_START,
1112 map,
1113 (uint32_t) (sva >> 32), (uint32_t) sva,
1114 (uint32_t) (eva >> 32), (uint32_t) eva);
1115
1116 if ((prot & VM_PROT_EXECUTE) || !nx_enabled || !map->nx_enabled)
1117 set_NX = FALSE;
1118 else
1119 set_NX = TRUE;
1120
1121 PMAP_LOCK(map);
1122
1123 orig_sva = sva;
1124 while (sva < eva) {
1125 lva = (sva + pde_mapped_size) & ~(pde_mapped_size - 1);
1126 if (lva > eva)
1127 lva = eva;
1128 pde = pmap_pde(map, sva);
1129 if (pde && (*pde & INTEL_PTE_VALID)) {
1130 if (*pde & INTEL_PTE_PS) {
1131 /* superpage */
1132 spte = pde;
1133 epte = spte+1; /* excluded */
1134 } else {
1135 spte = pmap_pte(map, (sva & ~(pde_mapped_size - 1)));
1136 spte = &spte[ptenum(sva)];
1137 epte = &spte[intel_btop(lva - sva)];
1138 }
1139
1140 for (; spte < epte; spte++) {
1141 if (!(*spte & INTEL_PTE_VALID))
1142 continue;
1143
1144 if (prot & VM_PROT_WRITE)
1145 pmap_update_pte(spte, *spte,
1146 *spte | INTEL_PTE_WRITE);
1147 else
1148 pmap_update_pte(spte, *spte,
1149 *spte & ~INTEL_PTE_WRITE);
1150
1151 if (set_NX)
1152 pmap_update_pte(spte, *spte,
1153 *spte | INTEL_PTE_NX);
1154 else
1155 pmap_update_pte(spte, *spte,
1156 *spte & ~INTEL_PTE_NX);
1157
1158 num_found++;
1159 }
1160 }
1161 sva = lva;
1162 }
1163 if (num_found)
1164 PMAP_UPDATE_TLBS(map, orig_sva, eva);
1165
1166 PMAP_UNLOCK(map);
1167
1168 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT) | DBG_FUNC_END,
1169 0, 0, 0, 0, 0);
1170
1171 }
1172
1173 /* Map a (possibly) autogenned block */
1174 void
1175 pmap_map_block(
1176 pmap_t pmap,
1177 addr64_t va,
1178 ppnum_t pa,
1179 uint32_t size,
1180 vm_prot_t prot,
1181 int attr,
1182 __unused unsigned int flags)
1183 {
1184 uint32_t page;
1185 int cur_page_size;
1186
1187 if (attr & VM_MEM_SUPERPAGE)
1188 cur_page_size = SUPERPAGE_SIZE;
1189 else
1190 cur_page_size = PAGE_SIZE;
1191
1192 for (page = 0; page < size; page+=cur_page_size/PAGE_SIZE) {
1193 pmap_enter(pmap, va, pa, prot, attr, TRUE);
1194 va += cur_page_size;
1195 pa+=cur_page_size/PAGE_SIZE;
1196 }
1197 }
1198
1199
1200 void
1201 pmap_expand_pml4(
1202 pmap_t map,
1203 vm_map_offset_t vaddr)
1204 {
1205 vm_page_t m;
1206 pmap_paddr_t pa;
1207 uint64_t i;
1208 ppnum_t pn;
1209 pml4_entry_t *pml4p;
1210
1211 DBG("pmap_expand_pml4(%p,%p)\n", map, (void *)vaddr);
1212
1213 /*
1214 * Allocate a VM page for the pml4 page
1215 */
1216 while ((m = vm_page_grab()) == VM_PAGE_NULL)
1217 VM_PAGE_WAIT();
1218
1219 /*
1220 * put the page into the pmap's obj list so it
1221 * can be found later.
1222 */
1223 pn = m->phys_page;
1224 pa = i386_ptob(pn);
1225 i = pml4idx(map, vaddr);
1226
1227 /*
1228 * Zero the page.
1229 */
1230 pmap_zero_page(pn);
1231
1232 vm_page_lockspin_queues();
1233 vm_page_wire(m);
1234 vm_page_unlock_queues();
1235
1236 OSAddAtomic(1, &inuse_ptepages_count);
1237 OSAddAtomic64(1, &alloc_ptepages_count);
1238 PMAP_ZINFO_PALLOC(PAGE_SIZE);
1239
1240 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1241 vm_object_lock(map->pm_obj_pml4);
1242
1243 PMAP_LOCK(map);
1244 /*
1245 * See if someone else expanded us first
1246 */
1247 if (pmap64_pdpt(map, vaddr) != PDPT_ENTRY_NULL) {
1248 PMAP_UNLOCK(map);
1249 vm_object_unlock(map->pm_obj_pml4);
1250
1251 VM_PAGE_FREE(m);
1252
1253 OSAddAtomic(-1, &inuse_ptepages_count);
1254 PMAP_ZINFO_PFREE(PAGE_SIZE);
1255 return;
1256 }
1257
1258 #if 0 /* DEBUG */
1259 if (0 != vm_page_lookup(map->pm_obj_pml4, (vm_object_offset_t)i)) {
1260 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1261 map, map->pm_obj_pml4, vaddr, i);
1262 }
1263 #endif
1264 vm_page_insert(m, map->pm_obj_pml4, (vm_object_offset_t)i);
1265 vm_object_unlock(map->pm_obj_pml4);
1266
1267 /*
1268 * Set the page directory entry for this page table.
1269 */
1270 pml4p = pmap64_pml4(map, vaddr); /* refetch under lock */
1271
1272 pmap_store_pte(pml4p, pa_to_pte(pa)
1273 | INTEL_PTE_VALID
1274 | INTEL_PTE_USER
1275 | INTEL_PTE_WRITE);
1276
1277 PMAP_UNLOCK(map);
1278
1279 return;
1280 }
1281
1282 void
1283 pmap_expand_pdpt(
1284 pmap_t map,
1285 vm_map_offset_t vaddr)
1286 {
1287 vm_page_t m;
1288 pmap_paddr_t pa;
1289 uint64_t i;
1290 ppnum_t pn;
1291 pdpt_entry_t *pdptp;
1292
1293 DBG("pmap_expand_pdpt(%p,%p)\n", map, (void *)vaddr);
1294
1295 while ((pdptp = pmap64_pdpt(map, vaddr)) == PDPT_ENTRY_NULL) {
1296 pmap_expand_pml4(map, vaddr);
1297 }
1298
1299 /*
1300 * Allocate a VM page for the pdpt page
1301 */
1302 while ((m = vm_page_grab()) == VM_PAGE_NULL)
1303 VM_PAGE_WAIT();
1304
1305 /*
1306 * put the page into the pmap's obj list so it
1307 * can be found later.
1308 */
1309 pn = m->phys_page;
1310 pa = i386_ptob(pn);
1311 i = pdptidx(map, vaddr);
1312
1313 /*
1314 * Zero the page.
1315 */
1316 pmap_zero_page(pn);
1317
1318 vm_page_lockspin_queues();
1319 vm_page_wire(m);
1320 vm_page_unlock_queues();
1321
1322 OSAddAtomic(1, &inuse_ptepages_count);
1323 OSAddAtomic64(1, &alloc_ptepages_count);
1324 PMAP_ZINFO_PALLOC(PAGE_SIZE);
1325
1326 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1327 vm_object_lock(map->pm_obj_pdpt);
1328
1329 PMAP_LOCK(map);
1330 /*
1331 * See if someone else expanded us first
1332 */
1333 if (pmap64_pde(map, vaddr) != PD_ENTRY_NULL) {
1334 PMAP_UNLOCK(map);
1335 vm_object_unlock(map->pm_obj_pdpt);
1336
1337 VM_PAGE_FREE(m);
1338
1339 OSAddAtomic(-1, &inuse_ptepages_count);
1340 PMAP_ZINFO_PFREE(PAGE_SIZE);
1341 return;
1342 }
1343
1344 #if 0 /* DEBUG */
1345 if (0 != vm_page_lookup(map->pm_obj_pdpt, (vm_object_offset_t)i)) {
1346 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1347 map, map->pm_obj_pdpt, vaddr, i);
1348 }
1349 #endif
1350 vm_page_insert(m, map->pm_obj_pdpt, (vm_object_offset_t)i);
1351 vm_object_unlock(map->pm_obj_pdpt);
1352
1353 /*
1354 * Set the page directory entry for this page table.
1355 */
1356 pdptp = pmap64_pdpt(map, vaddr); /* refetch under lock */
1357
1358 pmap_store_pte(pdptp, pa_to_pte(pa)
1359 | INTEL_PTE_VALID
1360 | INTEL_PTE_USER
1361 | INTEL_PTE_WRITE);
1362
1363 PMAP_UNLOCK(map);
1364
1365 return;
1366
1367 }
1368
1369
1370
1371 /*
1372 * Routine: pmap_expand
1373 *
1374 * Expands a pmap to be able to map the specified virtual address.
1375 *
1376 * Allocates new virtual memory for the P0 or P1 portion of the
1377 * pmap, then re-maps the physical pages that were in the old
1378 * pmap to be in the new pmap.
1379 *
1380 * Must be called with the pmap system and the pmap unlocked,
1381 * since these must be unlocked to use vm_allocate or vm_deallocate.
1382 * Thus it must be called in a loop that checks whether the map
1383 * has been expanded enough.
1384 * (We won't loop forever, since page tables aren't shrunk.)
1385 */
1386 void
1387 pmap_expand(
1388 pmap_t map,
1389 vm_map_offset_t vaddr)
1390 {
1391 pt_entry_t *pdp;
1392 register vm_page_t m;
1393 register pmap_paddr_t pa;
1394 uint64_t i;
1395 ppnum_t pn;
1396
1397
1398 /*
1399 * For the kernel, the virtual address must be in or above the basement
1400 * which is for kexts and is in the 512GB immediately below the kernel..
1401 * XXX - should use VM_MIN_KERNEL_AND_KEXT_ADDRESS not KERNEL_BASEMENT
1402 */
1403 if (map == kernel_pmap &&
1404 !(vaddr >= KERNEL_BASEMENT && vaddr <= VM_MAX_KERNEL_ADDRESS))
1405 panic("pmap_expand: bad vaddr 0x%llx for kernel pmap", vaddr);
1406
1407
1408 while ((pdp = pmap64_pde(map, vaddr)) == PD_ENTRY_NULL) {
1409 /* need room for another pde entry */
1410 pmap_expand_pdpt(map, vaddr);
1411 }
1412
1413 /*
1414 * Allocate a VM page for the pde entries.
1415 */
1416 while ((m = vm_page_grab()) == VM_PAGE_NULL)
1417 VM_PAGE_WAIT();
1418
1419 /*
1420 * put the page into the pmap's obj list so it
1421 * can be found later.
1422 */
1423 pn = m->phys_page;
1424 pa = i386_ptob(pn);
1425 i = pdeidx(map, vaddr);
1426
1427 /*
1428 * Zero the page.
1429 */
1430 pmap_zero_page(pn);
1431
1432 vm_page_lockspin_queues();
1433 vm_page_wire(m);
1434 vm_page_unlock_queues();
1435
1436 OSAddAtomic(1, &inuse_ptepages_count);
1437 OSAddAtomic64(1, &alloc_ptepages_count);
1438 PMAP_ZINFO_PALLOC(PAGE_SIZE);
1439
1440 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1441 vm_object_lock(map->pm_obj);
1442
1443 PMAP_LOCK(map);
1444
1445 /*
1446 * See if someone else expanded us first
1447 */
1448 if (pmap_pte(map, vaddr) != PT_ENTRY_NULL) {
1449 PMAP_UNLOCK(map);
1450 vm_object_unlock(map->pm_obj);
1451
1452 VM_PAGE_FREE(m);
1453
1454 OSAddAtomic(-1, &inuse_ptepages_count);
1455 PMAP_ZINFO_PFREE(PAGE_SIZE);
1456 return;
1457 }
1458
1459 #if 0 /* DEBUG */
1460 if (0 != vm_page_lookup(map->pm_obj, (vm_object_offset_t)i)) {
1461 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
1462 map, map->pm_obj, vaddr, i);
1463 }
1464 #endif
1465 vm_page_insert(m, map->pm_obj, (vm_object_offset_t)i);
1466 vm_object_unlock(map->pm_obj);
1467
1468 /*
1469 * Set the page directory entry for this page table.
1470 */
1471 pdp = pmap_pde(map, vaddr);
1472 pmap_store_pte(pdp, pa_to_pte(pa)
1473 | INTEL_PTE_VALID
1474 | INTEL_PTE_USER
1475 | INTEL_PTE_WRITE);
1476
1477 PMAP_UNLOCK(map);
1478
1479 return;
1480 }
1481
1482 /* On K64 machines with more than 32GB of memory, pmap_steal_memory
1483 * will allocate past the 1GB of pre-expanded virtual kernel area. This
1484 * function allocates all the page tables using memory from the same pool
1485 * that pmap_steal_memory uses, rather than calling vm_page_grab (which
1486 * isn't available yet). */
1487 void
1488 pmap_pre_expand(pmap_t pmap, vm_map_offset_t vaddr)
1489 {
1490 ppnum_t pn;
1491 pt_entry_t *pte;
1492
1493 PMAP_LOCK(pmap);
1494
1495 if(pmap64_pdpt(pmap, vaddr) == PDPT_ENTRY_NULL) {
1496 if (!pmap_next_page_hi(&pn))
1497 panic("pmap_pre_expand");
1498
1499 pmap_zero_page(pn);
1500
1501 pte = pmap64_pml4(pmap, vaddr);
1502
1503 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1504 | INTEL_PTE_VALID
1505 | INTEL_PTE_USER
1506 | INTEL_PTE_WRITE);
1507 }
1508
1509 if(pmap64_pde(pmap, vaddr) == PD_ENTRY_NULL) {
1510 if (!pmap_next_page_hi(&pn))
1511 panic("pmap_pre_expand");
1512
1513 pmap_zero_page(pn);
1514
1515 pte = pmap64_pdpt(pmap, vaddr);
1516
1517 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1518 | INTEL_PTE_VALID
1519 | INTEL_PTE_USER
1520 | INTEL_PTE_WRITE);
1521 }
1522
1523 if(pmap_pte(pmap, vaddr) == PT_ENTRY_NULL) {
1524 if (!pmap_next_page_hi(&pn))
1525 panic("pmap_pre_expand");
1526
1527 pmap_zero_page(pn);
1528
1529 pte = pmap64_pde(pmap, vaddr);
1530
1531 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1532 | INTEL_PTE_VALID
1533 | INTEL_PTE_USER
1534 | INTEL_PTE_WRITE);
1535 }
1536
1537 PMAP_UNLOCK(pmap);
1538 }
1539
1540 /*
1541 * pmap_sync_page_data_phys(ppnum_t pa)
1542 *
1543 * Invalidates all of the instruction cache on a physical page and
1544 * pushes any dirty data from the data cache for the same physical page
1545 * Not required in i386.
1546 */
1547 void
1548 pmap_sync_page_data_phys(__unused ppnum_t pa)
1549 {
1550 return;
1551 }
1552
1553 /*
1554 * pmap_sync_page_attributes_phys(ppnum_t pa)
1555 *
1556 * Write back and invalidate all cachelines on a physical page.
1557 */
1558 void
1559 pmap_sync_page_attributes_phys(ppnum_t pa)
1560 {
1561 cache_flush_page_phys(pa);
1562 }
1563
1564
1565
1566 #ifdef CURRENTLY_UNUSED_AND_UNTESTED
1567
1568 int collect_ref;
1569 int collect_unref;
1570
1571 /*
1572 * Routine: pmap_collect
1573 * Function:
1574 * Garbage collects the physical map system for
1575 * pages which are no longer used.
1576 * Success need not be guaranteed -- that is, there
1577 * may well be pages which are not referenced, but
1578 * others may be collected.
1579 * Usage:
1580 * Called by the pageout daemon when pages are scarce.
1581 */
1582 void
1583 pmap_collect(
1584 pmap_t p)
1585 {
1586 register pt_entry_t *pdp, *ptp;
1587 pt_entry_t *eptp;
1588 int wired;
1589
1590 if (p == PMAP_NULL)
1591 return;
1592
1593 if (p == kernel_pmap)
1594 return;
1595
1596 /*
1597 * Garbage collect map.
1598 */
1599 PMAP_LOCK(p);
1600
1601 for (pdp = (pt_entry_t *)p->dirbase;
1602 pdp < (pt_entry_t *)&p->dirbase[(UMAXPTDI+1)];
1603 pdp++)
1604 {
1605 if (*pdp & INTEL_PTE_VALID) {
1606 if(*pdp & INTEL_PTE_REF) {
1607 pmap_store_pte(pdp, *pdp & ~INTEL_PTE_REF);
1608 collect_ref++;
1609 } else {
1610 collect_unref++;
1611 ptp = pmap_pte(p, pdetova(pdp - (pt_entry_t *)p->dirbase));
1612 eptp = ptp + NPTEPG;
1613
1614 /*
1615 * If the pte page has any wired mappings, we cannot
1616 * free it.
1617 */
1618 wired = 0;
1619 {
1620 register pt_entry_t *ptep;
1621 for (ptep = ptp; ptep < eptp; ptep++) {
1622 if (iswired(*ptep)) {
1623 wired = 1;
1624 break;
1625 }
1626 }
1627 }
1628 if (!wired) {
1629 /*
1630 * Remove the virtual addresses mapped by this pte page.
1631 */
1632 pmap_remove_range(p,
1633 pdetova(pdp - (pt_entry_t *)p->dirbase),
1634 ptp,
1635 eptp);
1636
1637 /*
1638 * Invalidate the page directory pointer.
1639 */
1640 pmap_store_pte(pdp, 0x0);
1641
1642 PMAP_UNLOCK(p);
1643
1644 /*
1645 * And free the pte page itself.
1646 */
1647 {
1648 register vm_page_t m;
1649
1650 vm_object_lock(p->pm_obj);
1651
1652 m = vm_page_lookup(p->pm_obj,(vm_object_offset_t)(pdp - (pt_entry_t *)&p->dirbase[0]));
1653 if (m == VM_PAGE_NULL)
1654 panic("pmap_collect: pte page not in object");
1655
1656 vm_object_unlock(p->pm_obj);
1657
1658 VM_PAGE_FREE(m);
1659
1660 OSAddAtomic(-1, &inuse_ptepages_count);
1661 PMAP_ZINFO_PFREE(PAGE_SIZE);
1662 }
1663
1664 PMAP_LOCK(p);
1665 }
1666 }
1667 }
1668 }
1669
1670 PMAP_UPDATE_TLBS(p, 0x0, 0xFFFFFFFFFFFFF000ULL);
1671 PMAP_UNLOCK(p);
1672 return;
1673
1674 }
1675 #endif
1676
1677
1678 void
1679 pmap_copy_page(ppnum_t src, ppnum_t dst)
1680 {
1681 bcopy_phys((addr64_t)i386_ptob(src),
1682 (addr64_t)i386_ptob(dst),
1683 PAGE_SIZE);
1684 }
1685
1686
1687 /*
1688 * Routine: pmap_pageable
1689 * Function:
1690 * Make the specified pages (by pmap, offset)
1691 * pageable (or not) as requested.
1692 *
1693 * A page which is not pageable may not take
1694 * a fault; therefore, its page table entry
1695 * must remain valid for the duration.
1696 *
1697 * This routine is merely advisory; pmap_enter
1698 * will specify that these pages are to be wired
1699 * down (or not) as appropriate.
1700 */
1701 void
1702 pmap_pageable(
1703 __unused pmap_t pmap,
1704 __unused vm_map_offset_t start_addr,
1705 __unused vm_map_offset_t end_addr,
1706 __unused boolean_t pageable)
1707 {
1708 #ifdef lint
1709 pmap++; start_addr++; end_addr++; pageable++;
1710 #endif /* lint */
1711 }
1712
1713
1714 void
1715 invalidate_icache(__unused vm_offset_t addr,
1716 __unused unsigned cnt,
1717 __unused int phys)
1718 {
1719 return;
1720 }
1721
1722 void
1723 flush_dcache(__unused vm_offset_t addr,
1724 __unused unsigned count,
1725 __unused int phys)
1726 {
1727 return;
1728 }
1729
1730 #if CONFIG_DTRACE
1731 /*
1732 * Constrain DTrace copyin/copyout actions
1733 */
1734 extern kern_return_t dtrace_copyio_preflight(addr64_t);
1735 extern kern_return_t dtrace_copyio_postflight(addr64_t);
1736
1737 kern_return_t dtrace_copyio_preflight(__unused addr64_t va)
1738 {
1739 thread_t thread = current_thread();
1740 uint64_t ccr3;
1741
1742 if (current_map() == kernel_map)
1743 return KERN_FAILURE;
1744 else if (((ccr3 = get_cr3_base()) != thread->map->pmap->pm_cr3) && (no_shared_cr3 == FALSE))
1745 return KERN_FAILURE;
1746 else if (no_shared_cr3 && (ccr3 != kernel_pmap->pm_cr3))
1747 return KERN_FAILURE;
1748 else if (thread->machine.specFlags & CopyIOActive)
1749 return KERN_FAILURE;
1750 else
1751 return KERN_SUCCESS;
1752 }
1753
1754 kern_return_t dtrace_copyio_postflight(__unused addr64_t va)
1755 {
1756 return KERN_SUCCESS;
1757 }
1758 #endif /* CONFIG_DTRACE */
1759
1760 #include <mach_vm_debug.h>
1761 #if MACH_VM_DEBUG
1762 #include <vm/vm_debug.h>
1763
1764 int
1765 pmap_list_resident_pages(
1766 __unused pmap_t pmap,
1767 __unused vm_offset_t *listp,
1768 __unused int space)
1769 {
1770 return 0;
1771 }
1772 #endif /* MACH_VM_DEBUG */
1773
1774
1775
1776 /* temporary workaround */
1777 boolean_t
1778 coredumpok(__unused vm_map_t map, __unused vm_offset_t va)
1779 {
1780 #if 0
1781 pt_entry_t *ptep;
1782
1783 ptep = pmap_pte(map->pmap, va);
1784 if (0 == ptep)
1785 return FALSE;
1786 return ((*ptep & (INTEL_PTE_NCACHE | INTEL_PTE_WIRED)) != (INTEL_PTE_NCACHE | INTEL_PTE_WIRED));
1787 #else
1788 return TRUE;
1789 #endif
1790 }
1791
1792
1793 boolean_t
1794 phys_page_exists(ppnum_t pn)
1795 {
1796 assert(pn != vm_page_fictitious_addr);
1797
1798 if (!pmap_initialized)
1799 return TRUE;
1800
1801 if (pn == vm_page_guard_addr)
1802 return FALSE;
1803
1804 if (!IS_MANAGED_PAGE(ppn_to_pai(pn)))
1805 return FALSE;
1806
1807 return TRUE;
1808 }
1809
1810
1811
1812 void
1813 pmap_switch(pmap_t tpmap)
1814 {
1815 spl_t s;
1816
1817 s = splhigh(); /* Make sure interruptions are disabled */
1818 set_dirbase(tpmap, current_thread());
1819 splx(s);
1820 }
1821
1822
1823 /*
1824 * disable no-execute capability on
1825 * the specified pmap
1826 */
1827 void
1828 pmap_disable_NX(pmap_t pmap)
1829 {
1830 pmap->nx_enabled = 0;
1831 }
1832
1833 void
1834 pt_fake_zone_init(int zone_index)
1835 {
1836 pt_fake_zone_index = zone_index;
1837 }
1838
1839 void
1840 pt_fake_zone_info(
1841 int *count,
1842 vm_size_t *cur_size,
1843 vm_size_t *max_size,
1844 vm_size_t *elem_size,
1845 vm_size_t *alloc_size,
1846 uint64_t *sum_size,
1847 int *collectable,
1848 int *exhaustable,
1849 int *caller_acct)
1850 {
1851 *count = inuse_ptepages_count;
1852 *cur_size = PAGE_SIZE * inuse_ptepages_count;
1853 *max_size = PAGE_SIZE * (inuse_ptepages_count +
1854 vm_page_inactive_count +
1855 vm_page_active_count +
1856 vm_page_free_count);
1857 *elem_size = PAGE_SIZE;
1858 *alloc_size = PAGE_SIZE;
1859 *sum_size = alloc_ptepages_count * PAGE_SIZE;
1860
1861 *collectable = 1;
1862 *exhaustable = 0;
1863 *caller_acct = 1;
1864 }
1865
1866 static inline void
1867 pmap_cpuset_NMIPI(cpu_set cpu_mask) {
1868 unsigned int cpu, cpu_bit;
1869 uint64_t deadline;
1870
1871 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
1872 if (cpu_mask & cpu_bit)
1873 cpu_NMI_interrupt(cpu);
1874 }
1875 deadline = mach_absolute_time() + (LockTimeOut);
1876 while (mach_absolute_time() < deadline)
1877 cpu_pause();
1878 }
1879
1880 /*
1881 * Called with pmap locked, we:
1882 * - scan through per-cpu data to see which other cpus need to flush
1883 * - send an IPI to each non-idle cpu to be flushed
1884 * - wait for all to signal back that they are inactive or we see that
1885 * they are at a safe point (idle).
1886 * - flush the local tlb if active for this pmap
1887 * - return ... the caller will unlock the pmap
1888 */
1889
1890 void
1891 pmap_flush_tlbs(pmap_t pmap, vm_map_offset_t startv, vm_map_offset_t endv)
1892 {
1893 unsigned int cpu;
1894 unsigned int cpu_bit;
1895 cpu_set cpus_to_signal;
1896 unsigned int my_cpu = cpu_number();
1897 pmap_paddr_t pmap_cr3 = pmap->pm_cr3;
1898 boolean_t flush_self = FALSE;
1899 uint64_t deadline;
1900 boolean_t pmap_is_shared = (pmap->pm_shared || (pmap == kernel_pmap));
1901
1902 assert((processor_avail_count < 2) ||
1903 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
1904
1905 /*
1906 * Scan other cpus for matching active or task CR3.
1907 * For idle cpus (with no active map) we mark them invalid but
1908 * don't signal -- they'll check as they go busy.
1909 */
1910 cpus_to_signal = 0;
1911
1912 if (pmap_pcid_ncpus) {
1913 pmap_pcid_invalidate_all_cpus(pmap);
1914 __asm__ volatile("mfence":::"memory");
1915 }
1916
1917 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
1918 if (!cpu_datap(cpu)->cpu_running)
1919 continue;
1920 uint64_t cpu_active_cr3 = CPU_GET_ACTIVE_CR3(cpu);
1921 uint64_t cpu_task_cr3 = CPU_GET_TASK_CR3(cpu);
1922
1923 if ((pmap_cr3 == cpu_task_cr3) ||
1924 (pmap_cr3 == cpu_active_cr3) ||
1925 (pmap_is_shared)) {
1926 if (cpu == my_cpu) {
1927 flush_self = TRUE;
1928 continue;
1929 }
1930 if (pmap_pcid_ncpus && pmap_is_shared)
1931 cpu_datap(cpu)->cpu_tlb_invalid_global = TRUE;
1932 else
1933 cpu_datap(cpu)->cpu_tlb_invalid_local = TRUE;
1934 __asm__ volatile("mfence":::"memory");
1935
1936 /*
1937 * We don't need to signal processors which will flush
1938 * lazily at the idle state or kernel boundary.
1939 * For example, if we're invalidating the kernel pmap,
1940 * processors currently in userspace don't need to flush
1941 * their TLBs until the next time they enter the kernel.
1942 * Alterations to the address space of a task active
1943 * on a remote processor result in a signal, to
1944 * account for copy operations. (There may be room
1945 * for optimization in such cases).
1946 * The order of the loads below with respect
1947 * to the store to the "cpu_tlb_invalid" field above
1948 * is important--hence the barrier.
1949 */
1950 if (CPU_CR3_IS_ACTIVE(cpu) &&
1951 (pmap_cr3 == CPU_GET_ACTIVE_CR3(cpu) ||
1952 pmap->pm_shared ||
1953 (pmap_cr3 == CPU_GET_TASK_CR3(cpu)))) {
1954 cpus_to_signal |= cpu_bit;
1955 i386_signal_cpu(cpu, MP_TLB_FLUSH, ASYNC);
1956 }
1957 }
1958 }
1959
1960 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS) | DBG_FUNC_START,
1961 pmap, cpus_to_signal, flush_self, startv, endv);
1962
1963 /*
1964 * Flush local tlb if required.
1965 * Do this now to overlap with other processors responding.
1966 */
1967 if (flush_self) {
1968 if (pmap_pcid_ncpus) {
1969 pmap_pcid_validate_cpu(pmap, my_cpu);
1970 if (pmap_is_shared)
1971 tlb_flush_global();
1972 else
1973 flush_tlb_raw();
1974 }
1975 else
1976 flush_tlb_raw();
1977 }
1978
1979 if (cpus_to_signal) {
1980 cpu_set cpus_to_respond = cpus_to_signal;
1981
1982 deadline = mach_absolute_time() + LockTimeOut;
1983 /*
1984 * Wait for those other cpus to acknowledge
1985 */
1986 while (cpus_to_respond != 0) {
1987 long orig_acks = 0;
1988
1989 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
1990 /* Consider checking local/global invalidity
1991 * as appropriate in the PCID case.
1992 */
1993 if ((cpus_to_respond & cpu_bit) != 0) {
1994 if (!cpu_datap(cpu)->cpu_running ||
1995 cpu_datap(cpu)->cpu_tlb_invalid == FALSE ||
1996 !CPU_CR3_IS_ACTIVE(cpu)) {
1997 cpus_to_respond &= ~cpu_bit;
1998 }
1999 cpu_pause();
2000 }
2001 if (cpus_to_respond == 0)
2002 break;
2003 }
2004 if (cpus_to_respond && (mach_absolute_time() > deadline)) {
2005 if (machine_timeout_suspended())
2006 continue;
2007 pmap_tlb_flush_timeout = TRUE;
2008 orig_acks = NMIPI_acks;
2009 pmap_cpuset_NMIPI(cpus_to_respond);
2010
2011 panic("TLB invalidation IPI timeout: "
2012 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%lx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2013 cpus_to_respond, orig_acks, NMIPI_acks);
2014 }
2015 }
2016 }
2017
2018 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS) | DBG_FUNC_END,
2019 pmap, cpus_to_signal, startv, endv, 0);
2020 }
2021
2022 void
2023 process_pmap_updates(void)
2024 {
2025 int ccpu = cpu_number();
2026 pmap_assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2027 if (pmap_pcid_ncpus) {
2028 pmap_pcid_validate_current();
2029 if (cpu_datap(ccpu)->cpu_tlb_invalid_global) {
2030 cpu_datap(ccpu)->cpu_tlb_invalid = FALSE;
2031 tlb_flush_global();
2032 }
2033 else {
2034 cpu_datap(ccpu)->cpu_tlb_invalid_local = FALSE;
2035 flush_tlb_raw();
2036 }
2037 }
2038 else {
2039 current_cpu_datap()->cpu_tlb_invalid = FALSE;
2040 flush_tlb_raw();
2041 }
2042
2043 __asm__ volatile("mfence");
2044 }
2045
2046 void
2047 pmap_update_interrupt(void)
2048 {
2049 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT) | DBG_FUNC_START,
2050 0, 0, 0, 0, 0);
2051
2052 process_pmap_updates();
2053
2054 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT) | DBG_FUNC_END,
2055 0, 0, 0, 0, 0);
2056 }