]> git.saurik.com Git - apple/xnu.git/blame - osfmk/x86_64/pmap.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / x86_64 / pmap.c
CommitLineData
b0d623f7
A
1
2/*
3 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 *
5 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. The rights granted to you under the License
11 * may not be used to create, or enable the creation or redistribution of,
12 * unlawful or unlicensed copies of an Apple operating system, or to
13 * circumvent, violate, or enable the circumvention or violation of, any
14 * terms of an Apple operating system software license agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 *
19 * The Original Code and all software distributed under the License are
20 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24 * Please see the License for the specific language governing rights and
25 * limitations under the License.
26 *
27 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 */
29/*
30 * @OSF_COPYRIGHT@
31 */
32/*
33 * Mach Operating System
34 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
35 * All Rights Reserved.
36 *
37 * Permission to use, copy, modify and distribute this software and its
38 * documentation is hereby granted, provided that both the copyright
39 * notice and this permission notice appear in all copies of the
40 * software, derivative works or modified versions, and any portions
41 * thereof, and that both notices appear in supporting documentation.
42 *
43 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
44 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
45 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 *
47 * Carnegie Mellon requests users of this software to return to
48 *
49 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
50 * School of Computer Science
51 * Carnegie Mellon University
52 * Pittsburgh PA 15213-3890
53 *
54 * any improvements or extensions that they make and grant Carnegie Mellon
55 * the rights to redistribute these changes.
56 */
57/*
58 */
59
60/*
61 * File: pmap.c
62 * Author: Avadis Tevanian, Jr., Michael Wayne Young
63 * (These guys wrote the Vax version)
64 *
65 * Physical Map management code for Intel i386, i486, and i860.
66 *
67 * Manages physical address maps.
68 *
69 * In addition to hardware address maps, this
70 * module is called upon to provide software-use-only
71 * maps which may or may not be stored in the same
72 * form as hardware maps. These pseudo-maps are
73 * used to store intermediate results from copy
74 * operations to and from address spaces.
75 *
76 * Since the information managed by this module is
77 * also stored by the logical address mapping module,
78 * this module may throw away valid virtual-to-physical
79 * mappings at almost any time. However, invalidations
80 * of virtual-to-physical mappings must be done as
81 * requested.
82 *
83 * In order to cope with hardware architectures which
84 * make virtual-to-physical map invalidates expensive,
85 * this module may delay invalidate or reduced protection
86 * operations until such time as they are actually
87 * necessary. This module is given full information as
88 * to which processors are currently using which maps,
89 * and to when physical maps must be made correct.
90 */
91
92#include <string.h>
b0d623f7
A
93#include <mach_kdb.h>
94#include <mach_ldebug.h>
95
96#include <libkern/OSAtomic.h>
97
98#include <mach/machine/vm_types.h>
99
100#include <mach/boolean.h>
101#include <kern/thread.h>
102#include <kern/zalloc.h>
103#include <kern/queue.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 <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
136#if MACH_KDB
137#include <ddb/db_command.h>
138#include <ddb/db_output.h>
139#include <ddb/db_sym.h>
140#include <ddb/db_print.h>
141#endif /* MACH_KDB */
142
143#include <vm/vm_protos.h>
144
145#include <i386/mp.h>
146#include <i386/mp_desc.h>
147
148
b0d623f7
A
149
150#ifdef IWANTTODEBUG
151#undef DEBUG
152#define DEBUG 1
153#define POSTCODE_DELAY 1
154#include <i386/postcode.h>
155#endif /* IWANTTODEBUG */
156
157boolean_t pmap_trace = FALSE;
158
159#if PMAP_DBG
160#define DBG(x...) kprintf("DBG: " x)
161#else
162#define DBG(x...)
163#endif
164
165boolean_t no_shared_cr3 = DEBUG; /* TRUE for DEBUG by default */
166
167/*
168 * Forward declarations for internal functions.
169 */
170
b0d623f7
A
171
172void phys_attribute_clear(
173 ppnum_t phys,
174 int bits);
175
176int phys_attribute_test(
177 ppnum_t phys,
178 int bits);
179
180void phys_attribute_set(
181 ppnum_t phys,
182 int bits);
183
184void pmap_set_reference(
185 ppnum_t pn);
186
187boolean_t phys_page_exists(
188 ppnum_t pn);
189
190
191int nx_enabled = 1; /* enable no-execute protection */
192int allow_data_exec = VM_ABI_32; /* 32-bit apps may execute data by default, 64-bit apps may not */
193int allow_stack_exec = 0; /* No apps may execute from the stack by default */
194
195const boolean_t cpu_64bit = TRUE; /* Mais oui! */
196
b0d623f7
A
197uint64_t max_preemption_latency_tsc = 0;
198
b0d623f7
A
199pv_hashed_entry_t *pv_hash_table; /* hash lists */
200
201uint32_t npvhash = 0;
202
b0d623f7
A
203pv_hashed_entry_t pv_hashed_free_list = PV_HASHED_ENTRY_NULL;
204pv_hashed_entry_t pv_hashed_kern_free_list = PV_HASHED_ENTRY_NULL;
205decl_simple_lock_data(,pv_hashed_free_list_lock)
206decl_simple_lock_data(,pv_hashed_kern_free_list_lock)
207decl_simple_lock_data(,pv_hash_table_lock)
208
209int pv_hashed_free_count = 0;
210int pv_hashed_kern_free_count = 0;
b0d623f7 211
b0d623f7
A
212
213zone_t pv_hashed_list_zone; /* zone of pv_hashed_entry structures */
214
b0d623f7
A
215/*
216 * Each entry in the pv_head_table is locked by a bit in the
217 * pv_lock_table. The lock bits are accessed by the physical
218 * address of the page they lock.
219 */
220
221char *pv_lock_table; /* pointer to array of bits */
b7266188 222
b0d623f7
A
223
224char *pv_hash_lock_table;
b7266188 225
b0d623f7
A
226
227/*
228 * First and last physical addresses that we maintain any information
229 * for. Initialized to zero so that pmap operations done before
230 * pmap_init won't touch any non-existent structures.
231 */
232boolean_t pmap_initialized = FALSE;/* Has pmap_init completed? */
233
234static struct vm_object kptobj_object_store;
235static struct vm_object kpml4obj_object_store;
236static struct vm_object kpdptobj_object_store;
237
238/*
b7266188 239 * Array of physical page attributes for managed pages.
b0d623f7
A
240 * One byte per physical page.
241 */
242char *pmap_phys_attributes;
243unsigned int last_managed_page = 0;
b0d623f7
A
244uint64_t pde_mapped_size = PDE_MAPPED_SIZE;
245
b0d623f7
A
246unsigned pmap_memory_region_count;
247unsigned pmap_memory_region_current;
248
249pmap_memory_region_t pmap_memory_regions[PMAP_MEMORY_REGIONS_SIZE];
250
251/*
252 * Other useful macros.
253 */
254#define current_pmap() (vm_map_pmap(current_thread()->map))
255
256struct pmap kernel_pmap_store;
257pmap_t kernel_pmap;
258
259pd_entry_t high_shared_pde;
260pd_entry_t commpage64_pde;
261
262struct zone *pmap_zone; /* zone of pmap structures */
263
b0d623f7
A
264unsigned int inuse_ptepages_count = 0;
265
266addr64_t kernel64_cr3;
267
268/*
269 * Pmap cache. Cache is threaded through ref_count field of pmap.
270 * Max will eventually be constant -- variable for experimentation.
271 */
272int pmap_cache_max = 32;
273int pmap_alloc_chunk = 8;
274pmap_t pmap_cache_list;
275int pmap_cache_count;
276decl_simple_lock_data(,pmap_cache_lock)
277
278extern char end;
279
280static int nkpt;
281
282pt_entry_t *DMAP1, *DMAP2;
283caddr_t DADDR1;
284caddr_t DADDR2;
b0d623f7
A
285/*
286 * for legacy, returns the address of the pde entry.
287 * for 64 bit, causes the pdpt page containing the pde entry to be mapped,
288 * then returns the mapped address of the pde entry in that page
289 */
290pd_entry_t *
291pmap_pde(pmap_t m, vm_map_offset_t v)
292{
293 pd_entry_t *pde;
294
295 assert(m);
296#if 0
297 if (m == kernel_pmap)
298 pde = (&((m)->dirbase[(vm_offset_t)(v) >> PDESHIFT]));
299 else
300#endif
301 pde = pmap64_pde(m, v);
302
303 return pde;
304}
305
306/*
307 * the single pml4 page per pmap is allocated at pmap create time and exists
308 * for the duration of the pmap. we allocate this page in kernel vm.
309 * this returns the address of the requested pml4 entry in the top level page.
310 */
311static inline
312pml4_entry_t *
313pmap64_pml4(pmap_t pmap, vm_map_offset_t vaddr)
314{
315 return &pmap->pm_pml4[(vaddr >> PML4SHIFT) & (NPML4PG-1)];
316}
317
318/*
319 * maps in the pml4 page, if any, containing the pdpt entry requested
320 * and returns the address of the pdpt entry in that mapped page
321 */
322pdpt_entry_t *
323pmap64_pdpt(pmap_t pmap, vm_map_offset_t vaddr)
324{
325 pml4_entry_t newpf;
326 pml4_entry_t *pml4;
327
328 assert(pmap);
329 if ((vaddr > 0x00007FFFFFFFFFFFULL) &&
330 (vaddr < 0xFFFF800000000000ULL)) {
331 return (0);
332 }
333
334 pml4 = pmap64_pml4(pmap, vaddr);
335 if (pml4 && ((*pml4 & INTEL_PTE_VALID))) {
336 newpf = *pml4 & PG_FRAME;
337 return &((pdpt_entry_t *) PHYSMAP_PTOV(newpf))
338 [(vaddr >> PDPTSHIFT) & (NPDPTPG-1)];
339 }
340 return (NULL);
341}
342/*
343 * maps in the pdpt page, if any, containing the pde entry requested
344 * and returns the address of the pde entry in that mapped page
345 */
346pd_entry_t *
347pmap64_pde(pmap_t pmap, vm_map_offset_t vaddr)
348{
349 pdpt_entry_t newpf;
350 pdpt_entry_t *pdpt;
351
352 assert(pmap);
353 if ((vaddr > 0x00007FFFFFFFFFFFULL) &&
354 (vaddr < 0xFFFF800000000000ULL)) {
355 return (0);
356 }
357
358 pdpt = pmap64_pdpt(pmap, vaddr);
359
360 if (pdpt && ((*pdpt & INTEL_PTE_VALID))) {
361 newpf = *pdpt & PG_FRAME;
362 return &((pd_entry_t *) PHYSMAP_PTOV(newpf))
363 [(vaddr >> PDSHIFT) & (NPDPG-1)];
364 }
365 return (NULL);
366}
367
368/*
369 * return address of mapped pte for vaddr va in pmap pmap.
370 *
371 * physically maps the pde page, if any, containing the pte in and returns
372 * the address of the pte in that mapped page
373 *
374 * In case the pde maps a superpage, return the pde, which, in this case
375 * is the actual page table entry.
376 */
377pt_entry_t *
378pmap_pte(pmap_t pmap, vm_map_offset_t vaddr)
379{
380 pd_entry_t *pde;
381 pd_entry_t newpf;
382
383 assert(pmap);
384 pde = pmap_pde(pmap, vaddr);
385
386 if (pde && ((*pde & INTEL_PTE_VALID))) {
387 if (*pde & INTEL_PTE_PS)
388 return pde;
389 newpf = *pde & PG_FRAME;
390 return &((pt_entry_t *)PHYSMAP_PTOV(newpf))
391 [i386_btop(vaddr) & (ppnum_t)(NPTEPG-1)];
392 }
393 return (NULL);
394}
395
396/*
397 * Map memory at initialization. The physical addresses being
398 * mapped are not managed and are never unmapped.
399 *
400 * For now, VM is already on, we only need to map the
401 * specified memory.
402 */
403vm_offset_t
404pmap_map(
405 vm_offset_t virt,
406 vm_map_offset_t start_addr,
407 vm_map_offset_t end_addr,
408 vm_prot_t prot,
409 unsigned int flags)
410{
411 int ps;
412
413 ps = PAGE_SIZE;
414 while (start_addr < end_addr) {
415 pmap_enter(kernel_pmap, (vm_map_offset_t)virt,
416 (ppnum_t) i386_btop(start_addr), prot, flags, FALSE);
417 virt += ps;
418 start_addr += ps;
419 }
420 return(virt);
421}
422
423/*
424 * Back-door routine for mapping kernel VM at initialization.
425 * Useful for mapping memory outside the range
426 * Sets no-cache, A, D.
427 * Otherwise like pmap_map.
428 */
429vm_offset_t
430pmap_map_bd(
431 vm_offset_t virt,
432 vm_map_offset_t start_addr,
433 vm_map_offset_t end_addr,
434 vm_prot_t prot,
435 unsigned int flags)
436{
437 pt_entry_t template;
438 pt_entry_t *pte;
439 spl_t spl;
440
441 template = pa_to_pte(start_addr)
442 | INTEL_PTE_REF
443 | INTEL_PTE_MOD
444 | INTEL_PTE_WIRED
445 | INTEL_PTE_VALID;
446
447 if (flags & (VM_MEM_NOT_CACHEABLE | VM_WIMG_USE_DEFAULT)) {
448 template |= INTEL_PTE_NCACHE;
449 if (!(flags & (VM_MEM_GUARDED | VM_WIMG_USE_DEFAULT)))
450 template |= INTEL_PTE_PTA;
451 }
452 if (prot & VM_PROT_WRITE)
453 template |= INTEL_PTE_WRITE;
454
455
456 while (start_addr < end_addr) {
457 spl = splhigh();
458 pte = pmap_pte(kernel_pmap, (vm_map_offset_t)virt);
459 if (pte == PT_ENTRY_NULL) {
460 panic("pmap_map_bd: Invalid kernel address\n");
461 }
462 pmap_store_pte(pte, template);
463 splx(spl);
464 pte_increment_pa(template);
465 virt += PAGE_SIZE;
466 start_addr += PAGE_SIZE;
467 }
468
469
470 flush_tlb();
471 return(virt);
472}
473
474extern char *first_avail;
475extern vm_offset_t virtual_avail, virtual_end;
476extern pmap_paddr_t avail_start, avail_end;
477extern vm_offset_t sHIB;
478extern vm_offset_t eHIB;
479extern vm_offset_t stext;
480extern vm_offset_t etext;
481extern vm_offset_t sdata;
482
483void
484pmap_cpu_init(void)
485{
486 /*
487 * Here early in the life of a processor (from cpu_mode_init()).
488 * Ensure global page feature is disabled.
489 */
490 set_cr4(get_cr4() &~ CR4_PGE);
491
492 /*
493 * Initialize the per-cpu, TLB-related fields.
494 */
495 current_cpu_datap()->cpu_kernel_cr3 = kernel_pmap->pm_cr3;
496 current_cpu_datap()->cpu_active_cr3 = kernel_pmap->pm_cr3;
497 current_cpu_datap()->cpu_tlb_invalid = FALSE;
498}
499
500
501
502/*
503 * Bootstrap the system enough to run with virtual memory.
504 * Map the kernel's code and data, and allocate the system page table.
505 * Called with mapping OFF. Page_size must already be set.
506 */
507
508void
509pmap_bootstrap(
510 __unused vm_offset_t load_start,
511 __unused boolean_t IA32e)
512{
513#if NCOPY_WINDOWS > 0
514 vm_offset_t va;
515 int i;
516#endif
517
518 assert(IA32e);
519
520 vm_last_addr = VM_MAX_KERNEL_ADDRESS; /* Set the highest address
521 * known to VM */
522 /*
523 * The kernel's pmap is statically allocated so we don't
524 * have to use pmap_create, which is unlikely to work
525 * correctly at this part of the boot sequence.
526 */
527
528 kernel_pmap = &kernel_pmap_store;
529 kernel_pmap->ref_count = 1;
530 kernel_pmap->nx_enabled = FALSE;
531 kernel_pmap->pm_task_map = TASK_MAP_64BIT;
532 kernel_pmap->pm_obj = (vm_object_t) NULL;
533 kernel_pmap->dirbase = (pd_entry_t *)((uintptr_t)IdlePTD);
534 kernel_pmap->pm_pdpt = (pd_entry_t *) ((uintptr_t)IdlePDPT);
535 kernel_pmap->pm_pml4 = IdlePML4;
536 kernel_pmap->pm_cr3 = (uintptr_t)ID_MAP_VTOP(IdlePML4);
537
538
539 current_cpu_datap()->cpu_kernel_cr3 = (addr64_t) kernel_pmap->pm_cr3;
540
541 nkpt = NKPT;
542 OSAddAtomic(NKPT, &inuse_ptepages_count);
543
544 virtual_avail = (vm_offset_t)(VM_MIN_KERNEL_ADDRESS) + (vm_offset_t)first_avail;
545 virtual_end = (vm_offset_t)(VM_MAX_KERNEL_ADDRESS);
546
547#if NCOPY_WINDOWS > 0
548 /*
549 * Reserve some special page table entries/VA space for temporary
550 * mapping of pages.
551 */
552#define SYSMAP(c, p, v, n) \
553 v = (c)va; va += ((n)*INTEL_PGBYTES);
554
555 va = virtual_avail;
556
557 for (i=0; i<PMAP_NWINDOWS; i++) {
558#if 1
559 kprintf("trying to do SYSMAP idx %d %p\n", i,
560 current_cpu_datap());
561 kprintf("cpu_pmap %p\n", current_cpu_datap()->cpu_pmap);
562 kprintf("mapwindow %p\n", current_cpu_datap()->cpu_pmap->mapwindow);
563 kprintf("two stuff %p %p\n",
564 (void *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP),
565 (void *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR));
566#endif
567 SYSMAP(caddr_t,
568 (current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP),
569 (current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR),
570 1);
571 current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP =
572 &(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP_store);
573 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
574 }
575
576 /* DMAP user for debugger */
577 SYSMAP(caddr_t, DMAP1, DADDR1, 1);
578 SYSMAP(caddr_t, DMAP2, DADDR2, 1); /* XXX temporary - can remove */
579
580 virtual_avail = va;
581#endif
582
583 if (PE_parse_boot_argn("npvhash", &npvhash, sizeof (npvhash))) {
584 if (0 != ((npvhash + 1) & npvhash)) {
585 kprintf("invalid hash %d, must be ((2^N)-1), "
586 "using default %d\n", npvhash, NPVHASH);
587 npvhash = NPVHASH;
588 }
589 } else {
590 npvhash = NPVHASH;
591 }
592
593 printf("npvhash=%d\n", npvhash);
594
595 simple_lock_init(&kernel_pmap->lock, 0);
596 simple_lock_init(&pv_hashed_free_list_lock, 0);
597 simple_lock_init(&pv_hashed_kern_free_list_lock, 0);
598 simple_lock_init(&pv_hash_table_lock,0);
599
600 pmap_cpu_init();
601
602 kprintf("Kernel virtual space from 0x%lx to 0x%lx.\n",
603 (long)KERNEL_BASE, (long)virtual_end);
604 kprintf("Available physical space from 0x%llx to 0x%llx\n",
605 avail_start, avail_end);
606
607 /*
608 * The -no_shared_cr3 boot-arg is a debugging feature (set by default
609 * in the DEBUG kernel) to force the kernel to switch to its own map
610 * (and cr3) when control is in kernelspace. The kernel's map does not
611 * include (i.e. share) userspace so wild references will cause
612 * a panic. Only copyin and copyout are exempt from this.
613 */
614 (void) PE_parse_boot_argn("-no_shared_cr3",
615 &no_shared_cr3, sizeof (no_shared_cr3));
616 if (no_shared_cr3)
617 kprintf("Kernel not sharing user map\n");
618
619#ifdef PMAP_TRACES
620 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace, sizeof (pmap_trace))) {
621 kprintf("Kernel traces for pmap operations enabled\n");
622 }
623#endif /* PMAP_TRACES */
624}
625
626void
627pmap_virtual_space(
628 vm_offset_t *startp,
629 vm_offset_t *endp)
630{
631 *startp = virtual_avail;
632 *endp = virtual_end;
633}
634
635/*
636 * Initialize the pmap module.
637 * Called by vm_init, to initialize any structures that the pmap
638 * system needs to map virtual memory.
639 */
640void
641pmap_init(void)
642{
643 long npages;
644 vm_offset_t addr;
060df5ea 645 vm_size_t s, vsize;
b0d623f7
A
646 vm_map_offset_t vaddr;
647 ppnum_t ppn;
648
649
650 kernel_pmap->pm_obj_pml4 = &kpml4obj_object_store;
651 _vm_object_allocate((vm_object_size_t)NPML4PGS, &kpml4obj_object_store);
652
653 kernel_pmap->pm_obj_pdpt = &kpdptobj_object_store;
654 _vm_object_allocate((vm_object_size_t)NPDPTPGS, &kpdptobj_object_store);
655
656 kernel_pmap->pm_obj = &kptobj_object_store;
657 _vm_object_allocate((vm_object_size_t)NPDEPGS, &kptobj_object_store);
658
659 /*
660 * Allocate memory for the pv_head_table and its lock bits,
661 * the modify bit array, and the pte_page table.
662 */
663
664 /*
665 * zero bias all these arrays now instead of off avail_start
666 * so we cover all memory
667 */
668
669 npages = i386_btop(avail_end);
670 s = (vm_size_t) (sizeof(struct pv_rooted_entry) * npages
671 + (sizeof (struct pv_hashed_entry_t *) * (npvhash+1))
672 + pv_lock_table_size(npages)
673 + pv_hash_lock_table_size((npvhash+1))
674 + npages);
675
676 s = round_page(s);
677 if (kernel_memory_allocate(kernel_map, &addr, s, 0,
678 KMA_KOBJECT | KMA_PERMANENT)
679 != KERN_SUCCESS)
680 panic("pmap_init");
681
682 memset((char *)addr, 0, s);
683
060df5ea
A
684 vaddr = addr;
685 vsize = s;
686
b0d623f7
A
687#if PV_DEBUG
688 if (0 == npvhash) panic("npvhash not initialized");
689#endif
690
691 /*
692 * Allocate the structures first to preserve word-alignment.
693 */
694 pv_head_table = (pv_rooted_entry_t) addr;
695 addr = (vm_offset_t) (pv_head_table + npages);
696
697 pv_hash_table = (pv_hashed_entry_t *)addr;
698 addr = (vm_offset_t) (pv_hash_table + (npvhash + 1));
699
700 pv_lock_table = (char *) addr;
701 addr = (vm_offset_t) (pv_lock_table + pv_lock_table_size(npages));
702
703 pv_hash_lock_table = (char *) addr;
704 addr = (vm_offset_t) (pv_hash_lock_table + pv_hash_lock_table_size((npvhash+1)));
705
706 pmap_phys_attributes = (char *) addr;
707
708 ppnum_t last_pn = i386_btop(avail_end);
709 unsigned int i;
710 pmap_memory_region_t *pmptr = pmap_memory_regions;
711 for (i = 0; i < pmap_memory_region_count; i++, pmptr++) {
712 if (pmptr->type != kEfiConventionalMemory)
713 continue;
714 unsigned int pn;
715 for (pn = pmptr->base; pn <= pmptr->end; pn++) {
716 if (pn < last_pn) {
717 pmap_phys_attributes[pn] |= PHYS_MANAGED;
060df5ea 718
b0d623f7
A
719 if (pn > last_managed_page)
720 last_managed_page = pn;
060df5ea
A
721
722 if (pn < lowest_lo)
723 pmap_phys_attributes[pn] |= PHYS_NOENCRYPT;
724 else if (pn >= lowest_hi && pn <= highest_hi)
725 pmap_phys_attributes[pn] |= PHYS_NOENCRYPT;
726
b0d623f7
A
727 }
728 }
729 }
060df5ea
A
730 while (vsize) {
731 ppn = pmap_find_phys(kernel_pmap, vaddr);
b0d623f7 732
060df5ea
A
733 pmap_phys_attributes[ppn] |= PHYS_NOENCRYPT;
734
735 vaddr += PAGE_SIZE;
736 vsize -= PAGE_SIZE;
737 }
b0d623f7
A
738 /*
739 * Create the zone of physical maps,
740 * and of the physical-to-virtual entries.
741 */
742 s = (vm_size_t) sizeof(struct pmap);
743 pmap_zone = zinit(s, 400*s, 4096, "pmap"); /* XXX */
060df5ea
A
744 zone_change(pmap_zone, Z_NOENCRYPT, TRUE);
745
b0d623f7
A
746 s = (vm_size_t) sizeof(struct pv_hashed_entry);
747 pv_hashed_list_zone = zinit(s, 10000*s, 4096, "pv_list"); /* XXX */
060df5ea 748 zone_change(pv_hashed_list_zone, Z_NOENCRYPT, TRUE);
b0d623f7
A
749
750 /* create pv entries for kernel pages mapped by low level
751 startup code. these have to exist so we can pmap_remove()
752 e.g. kext pages from the middle of our addr space */
753
754 vaddr = (vm_map_offset_t) VM_MIN_KERNEL_ADDRESS;
755 for (ppn = 0; ppn < i386_btop(avail_start); ppn++) {
756 pv_rooted_entry_t pv_e;
757
758 pv_e = pai_to_pvh(ppn);
759 pv_e->va = vaddr;
760 vaddr += PAGE_SIZE;
761 pv_e->pmap = kernel_pmap;
762 queue_init(&pv_e->qlink);
763 }
764 pmap_initialized = TRUE;
765
766 /*
767 * Initialize pmap cache.
768 */
769 pmap_cache_list = PMAP_NULL;
770 pmap_cache_count = 0;
771 simple_lock_init(&pmap_cache_lock, 0);
772
773 max_preemption_latency_tsc = tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS, tscFCvtn2t);
774
775 /*
776 * Ensure the kernel's PML4 entry exists for the basement
777 * before this is shared with any user.
778 */
779 pmap_expand_pml4(kernel_pmap, KERNEL_BASEMENT);
780}
781
782
783/*
784 * this function is only used for debugging fron the vm layer
785 */
786boolean_t
787pmap_verify_free(
788 ppnum_t pn)
789{
790 pv_rooted_entry_t pv_h;
791 int pai;
792 boolean_t result;
793
794 assert(pn != vm_page_fictitious_addr);
795
796 if (!pmap_initialized)
797 return(TRUE);
798
799 if (pn == vm_page_guard_addr)
800 return TRUE;
801
802 pai = ppn_to_pai(pn);
803 if (!IS_MANAGED_PAGE(pai))
804 return(FALSE);
805 pv_h = pai_to_pvh(pn);
806 result = (pv_h->pmap == PMAP_NULL);
807 return(result);
808}
809
810boolean_t
811pmap_is_empty(
812 pmap_t pmap,
813 vm_map_offset_t va_start,
814 vm_map_offset_t va_end)
815{
816 vm_map_offset_t offset;
817 ppnum_t phys_page;
818
819 if (pmap == PMAP_NULL) {
820 return TRUE;
821 }
822
823 /*
824 * Check the resident page count
825 * - if it's zero, the pmap is completely empty.
826 * This short-circuit test prevents a virtual address scan which is
827 * painfully slow for 64-bit spaces.
828 * This assumes the count is correct
829 * .. the debug kernel ought to be checking perhaps by page table walk.
830 */
831 if (pmap->stats.resident_count == 0)
832 return TRUE;
833
834 for (offset = va_start;
835 offset < va_end;
836 offset += PAGE_SIZE_64) {
837 phys_page = pmap_find_phys(pmap, offset);
838 if (phys_page) {
839 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
840 "page %d at 0x%llx\n",
841 pmap, va_start, va_end, phys_page, offset);
842 return FALSE;
843 }
844 }
845
846 return TRUE;
847}
848
849
850/*
851 * Create and return a physical map.
852 *
853 * If the size specified for the map
854 * is zero, the map is an actual physical
855 * map, and may be referenced by the
856 * hardware.
857 *
858 * If the size specified is non-zero,
859 * the map will be used in software only, and
860 * is bounded by that size.
861 */
862pmap_t
863pmap_create(
864 vm_map_size_t sz,
865 boolean_t is_64bit)
866{
867 pmap_t p;
868 vm_size_t size;
869 pml4_entry_t *pml4;
870 pml4_entry_t *kpml4;
871
872 PMAP_TRACE(PMAP_CODE(PMAP__CREATE) | DBG_FUNC_START,
873 (uint32_t) (sz>>32), (uint32_t) sz, is_64bit, 0, 0);
874
875 size = (vm_size_t) sz;
876
877 /*
878 * A software use-only map doesn't even need a map.
879 */
880
881 if (size != 0) {
882 return(PMAP_NULL);
883 }
884
885 p = (pmap_t) zalloc(pmap_zone);
886 if (PMAP_NULL == p)
887 panic("pmap_create zalloc");
888
889 /* init counts now since we'll be bumping some */
890 simple_lock_init(&p->lock, 0);
891 p->stats.resident_count = 0;
892 p->stats.resident_max = 0;
893 p->stats.wired_count = 0;
894 p->ref_count = 1;
895 p->nx_enabled = 1;
896 p->pm_shared = FALSE;
897
898 p->pm_task_map = is_64bit ? TASK_MAP_64BIT : TASK_MAP_32BIT;;
899
900 /* alloc the pml4 page in kernel vm */
901 if (KERN_SUCCESS != kmem_alloc_kobject(kernel_map, (vm_offset_t *)(&p->pm_pml4), PAGE_SIZE))
902 panic("pmap_create kmem_alloc_kobject pml4");
903
904 memset((char *)p->pm_pml4, 0, PAGE_SIZE);
905 p->pm_cr3 = (pmap_paddr_t)kvtophys((vm_offset_t)p->pm_pml4);
906
907 OSAddAtomic(1, &inuse_ptepages_count);
908
909 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
910
911 p->pm_obj_pml4 = vm_object_allocate((vm_object_size_t)(NPML4PGS));
912 if (NULL == p->pm_obj_pml4)
913 panic("pmap_create pdpt obj");
914
915 p->pm_obj_pdpt = vm_object_allocate((vm_object_size_t)(NPDPTPGS));
916 if (NULL == p->pm_obj_pdpt)
917 panic("pmap_create pdpt obj");
918
919 p->pm_obj = vm_object_allocate((vm_object_size_t)(NPDEPGS));
920 if (NULL == p->pm_obj)
921 panic("pmap_create pte obj");
922
923 /* All pmaps share the kennel's pml4 */
924 pml4 = pmap64_pml4(p, 0ULL);
925 kpml4 = kernel_pmap->pm_pml4;
926 pml4[KERNEL_PML4_INDEX] = kpml4[KERNEL_PML4_INDEX];
927 pml4[KERNEL_KEXTS_INDEX] = kpml4[KERNEL_KEXTS_INDEX];
928 pml4[KERNEL_PHYSMAP_INDEX] = kpml4[KERNEL_PHYSMAP_INDEX];
929
930 PMAP_TRACE(PMAP_CODE(PMAP__CREATE) | DBG_FUNC_START,
931 p, is_64bit, 0, 0, 0);
932
933 return(p);
934}
935
936/*
937 * Retire the given physical map from service.
938 * Should only be called if the map contains
939 * no valid mappings.
940 */
941
942void
943pmap_destroy(
944 register pmap_t p)
945{
946 register int c;
947
948 if (p == PMAP_NULL)
949 return;
950
951 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_START,
952 p, 0, 0, 0, 0);
953
954 PMAP_LOCK(p);
955
956 c = --p->ref_count;
957
958 if (c == 0) {
959 /*
960 * If some cpu is not using the physical pmap pointer that it
961 * is supposed to be (see set_dirbase), we might be using the
962 * pmap that is being destroyed! Make sure we are
963 * physically on the right pmap:
964 */
965 PMAP_UPDATE_TLBS(p, 0x0ULL, 0xFFFFFFFFFFFFF000ULL);
966 }
967
968 PMAP_UNLOCK(p);
969
970 if (c != 0) {
971 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_END,
972 p, 1, 0, 0, 0);
973 return; /* still in use */
974 }
975
976 /*
977 * Free the memory maps, then the
978 * pmap structure.
979 */
980 int inuse_ptepages = 0;
981
982 inuse_ptepages++;
983 kmem_free(kernel_map, (vm_offset_t)p->pm_pml4, PAGE_SIZE);
984
985 inuse_ptepages += p->pm_obj_pml4->resident_page_count;
986 vm_object_deallocate(p->pm_obj_pml4);
987
988 inuse_ptepages += p->pm_obj_pdpt->resident_page_count;
989 vm_object_deallocate(p->pm_obj_pdpt);
990
991 inuse_ptepages += p->pm_obj->resident_page_count;
992 vm_object_deallocate(p->pm_obj);
993
994 OSAddAtomic(-inuse_ptepages, &inuse_ptepages_count);
995
996 zfree(pmap_zone, p);
997
998 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_END,
999 0, 0, 0, 0, 0);
1000}
1001
1002/*
1003 * Add a reference to the specified pmap.
1004 */
1005
1006void
1007pmap_reference(pmap_t p)
1008{
1009 if (p != PMAP_NULL) {
1010 PMAP_LOCK(p);
1011 p->ref_count++;
1012 PMAP_UNLOCK(p);;
1013 }
1014}
1015
b0d623f7
A
1016/*
1017 * Remove phys addr if mapped in specified map
1018 *
1019 */
1020void
1021pmap_remove_some_phys(
1022 __unused pmap_t map,
1023 __unused ppnum_t pn)
1024{
1025
1026/* Implement to support working set code */
1027
1028}
1029
b0d623f7
A
1030
1031/*
1032 * Routine:
1033 * pmap_disconnect
1034 *
1035 * Function:
1036 * Disconnect all mappings for this page and return reference and change status
1037 * in generic format.
1038 *
1039 */
1040unsigned int pmap_disconnect(
1041 ppnum_t pa)
1042{
1043 pmap_page_protect(pa, 0); /* disconnect the page */
1044 return (pmap_get_refmod(pa)); /* return ref/chg status */
1045}
1046
1047/*
1048 * Set the physical protection on the
1049 * specified range of this map as requested.
1050 * Will not increase permissions.
1051 */
1052void
1053pmap_protect(
1054 pmap_t map,
1055 vm_map_offset_t sva,
1056 vm_map_offset_t eva,
1057 vm_prot_t prot)
1058{
1059 pt_entry_t *pde;
1060 pt_entry_t *spte, *epte;
1061 vm_map_offset_t lva;
1062 vm_map_offset_t orig_sva;
1063 boolean_t set_NX;
1064 int num_found = 0;
1065
1066 pmap_intr_assert();
1067
1068 if (map == PMAP_NULL)
1069 return;
1070
1071 if (prot == VM_PROT_NONE) {
1072 pmap_remove(map, sva, eva);
1073 return;
1074 }
1075 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT) | DBG_FUNC_START,
1076 map,
1077 (uint32_t) (sva >> 32), (uint32_t) sva,
1078 (uint32_t) (eva >> 32), (uint32_t) eva);
1079
1080 if ((prot & VM_PROT_EXECUTE) || !nx_enabled || !map->nx_enabled)
1081 set_NX = FALSE;
1082 else
1083 set_NX = TRUE;
1084
1085 PMAP_LOCK(map);
1086
1087 orig_sva = sva;
1088 while (sva < eva) {
1089 lva = (sva + pde_mapped_size) & ~(pde_mapped_size - 1);
1090 if (lva > eva)
1091 lva = eva;
1092 pde = pmap_pde(map, sva);
1093 if (pde && (*pde & INTEL_PTE_VALID)) {
1094 if (*pde & INTEL_PTE_PS) {
1095 /* superpage */
1096 spte = pde;
1097 epte = spte+1; /* excluded */
1098 } else {
1099 spte = pmap_pte(map, (sva & ~(pde_mapped_size - 1)));
1100 spte = &spte[ptenum(sva)];
1101 epte = &spte[intel_btop(lva - sva)];
1102 }
1103
1104 for (; spte < epte; spte++) {
1105 if (!(*spte & INTEL_PTE_VALID))
1106 continue;
1107
1108 if (prot & VM_PROT_WRITE)
1109 pmap_update_pte(spte, *spte,
1110 *spte | INTEL_PTE_WRITE);
1111 else
1112 pmap_update_pte(spte, *spte,
1113 *spte & ~INTEL_PTE_WRITE);
1114
1115 if (set_NX)
1116 pmap_update_pte(spte, *spte,
1117 *spte | INTEL_PTE_NX);
1118 else
1119 pmap_update_pte(spte, *spte,
1120 *spte & ~INTEL_PTE_NX);
1121
1122 num_found++;
1123 }
1124 }
1125 sva = lva;
1126 }
1127 if (num_found)
1128 PMAP_UPDATE_TLBS(map, orig_sva, eva);
1129
1130 PMAP_UNLOCK(map);
1131
1132 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT) | DBG_FUNC_END,
1133 0, 0, 0, 0, 0);
1134
1135}
1136
1137/* Map a (possibly) autogenned block */
1138void
1139pmap_map_block(
1140 pmap_t pmap,
1141 addr64_t va,
1142 ppnum_t pa,
1143 uint32_t size,
1144 vm_prot_t prot,
1145 int attr,
1146 __unused unsigned int flags)
1147{
1148 uint32_t page;
1149 int cur_page_size;
1150
1151 if (attr & VM_MEM_SUPERPAGE)
1152 cur_page_size = SUPERPAGE_SIZE;
1153 else
1154 cur_page_size = PAGE_SIZE;
1155
1156 for (page = 0; page < size; page+=cur_page_size/PAGE_SIZE) {
1157 pmap_enter(pmap, va, pa, prot, attr, TRUE);
1158 va += cur_page_size;
1159 pa+=cur_page_size/PAGE_SIZE;
1160 }
1161}
1162
b0d623f7
A
1163/*
1164 * Routine: pmap_change_wiring
1165 * Function: Change the wiring attribute for a map/virtual-address
1166 * pair.
1167 * In/out conditions:
1168 * The mapping must already exist in the pmap.
1169 */
1170void
1171pmap_change_wiring(
1172 pmap_t map,
1173 vm_map_offset_t vaddr,
1174 boolean_t wired)
1175{
1176 pt_entry_t *pte;
1177
1178 PMAP_LOCK(map);
1179
1180 if ((pte = pmap_pte(map, vaddr)) == PT_ENTRY_NULL)
1181 panic("pmap_change_wiring: pte missing");
1182
1183 if (wired && !iswired(*pte)) {
1184 /*
1185 * wiring down mapping
1186 */
1187 OSAddAtomic(+1, &map->stats.wired_count);
1188 pmap_update_pte(pte, *pte, (*pte | INTEL_PTE_WIRED));
1189 }
1190 else if (!wired && iswired(*pte)) {
1191 /*
1192 * unwiring mapping
1193 */
1194 assert(map->stats.wired_count >= 1);
1195 OSAddAtomic(-1, &map->stats.wired_count);
1196 pmap_update_pte(pte, *pte, (*pte & ~INTEL_PTE_WIRED));
1197 }
1198
1199 PMAP_UNLOCK(map);
1200}
1201
1202void
1203pmap_expand_pml4(
1204 pmap_t map,
1205 vm_map_offset_t vaddr)
1206{
1207 vm_page_t m;
1208 pmap_paddr_t pa;
1209 uint64_t i;
1210 ppnum_t pn;
1211 pml4_entry_t *pml4p;
1212
1213 DBG("pmap_expand_pml4(%p,%p)\n", map, (void *)vaddr);
1214
1215 /*
1216 * Allocate a VM page for the pml4 page
1217 */
1218 while ((m = vm_page_grab()) == VM_PAGE_NULL)
1219 VM_PAGE_WAIT();
1220
1221 /*
1222 * put the page into the pmap's obj list so it
1223 * can be found later.
1224 */
1225 pn = m->phys_page;
1226 pa = i386_ptob(pn);
1227 i = pml4idx(map, vaddr);
1228
1229 /*
1230 * Zero the page.
1231 */
1232 pmap_zero_page(pn);
1233
1234 vm_page_lockspin_queues();
1235 vm_page_wire(m);
1236 vm_page_unlock_queues();
1237
1238 OSAddAtomic(1, &inuse_ptepages_count);
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 return;
1255 }
1256
1257#if 0 /* DEBUG */
1258 if (0 != vm_page_lookup(map->pm_obj_pml4, (vm_object_offset_t)i)) {
1259 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1260 map, map->pm_obj_pml4, vaddr, i);
1261 }
1262#endif
1263 vm_page_insert(m, map->pm_obj_pml4, (vm_object_offset_t)i);
1264 vm_object_unlock(map->pm_obj_pml4);
1265
1266 /*
1267 * Set the page directory entry for this page table.
1268 */
1269 pml4p = pmap64_pml4(map, vaddr); /* refetch under lock */
1270
1271 pmap_store_pte(pml4p, pa_to_pte(pa)
1272 | INTEL_PTE_VALID
1273 | INTEL_PTE_USER
1274 | INTEL_PTE_WRITE);
1275
1276 PMAP_UNLOCK(map);
1277
1278 return;
1279}
1280
1281void
1282pmap_expand_pdpt(
1283 pmap_t map,
1284 vm_map_offset_t vaddr)
1285{
1286 vm_page_t m;
1287 pmap_paddr_t pa;
1288 uint64_t i;
1289 ppnum_t pn;
1290 pdpt_entry_t *pdptp;
1291
1292 DBG("pmap_expand_pdpt(%p,%p)\n", map, (void *)vaddr);
1293
1294 while ((pdptp = pmap64_pdpt(map, vaddr)) == PDPT_ENTRY_NULL) {
1295 pmap_expand_pml4(map, vaddr);
1296 }
1297
1298 /*
1299 * Allocate a VM page for the pdpt page
1300 */
1301 while ((m = vm_page_grab()) == VM_PAGE_NULL)
1302 VM_PAGE_WAIT();
1303
1304 /*
1305 * put the page into the pmap's obj list so it
1306 * can be found later.
1307 */
1308 pn = m->phys_page;
1309 pa = i386_ptob(pn);
1310 i = pdptidx(map, vaddr);
1311
1312 /*
1313 * Zero the page.
1314 */
1315 pmap_zero_page(pn);
1316
1317 vm_page_lockspin_queues();
1318 vm_page_wire(m);
1319 vm_page_unlock_queues();
1320
1321 OSAddAtomic(1, &inuse_ptepages_count);
1322
1323 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1324 vm_object_lock(map->pm_obj_pdpt);
1325
1326 PMAP_LOCK(map);
1327 /*
1328 * See if someone else expanded us first
1329 */
1330 if (pmap64_pde(map, vaddr) != PD_ENTRY_NULL) {
1331 PMAP_UNLOCK(map);
1332 vm_object_unlock(map->pm_obj_pdpt);
1333
1334 VM_PAGE_FREE(m);
1335
1336 OSAddAtomic(-1, &inuse_ptepages_count);
1337 return;
1338 }
1339
1340#if 0 /* DEBUG */
1341 if (0 != vm_page_lookup(map->pm_obj_pdpt, (vm_object_offset_t)i)) {
1342 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1343 map, map->pm_obj_pdpt, vaddr, i);
1344 }
1345#endif
1346 vm_page_insert(m, map->pm_obj_pdpt, (vm_object_offset_t)i);
1347 vm_object_unlock(map->pm_obj_pdpt);
1348
1349 /*
1350 * Set the page directory entry for this page table.
1351 */
1352 pdptp = pmap64_pdpt(map, vaddr); /* refetch under lock */
1353
1354 pmap_store_pte(pdptp, pa_to_pte(pa)
1355 | INTEL_PTE_VALID
1356 | INTEL_PTE_USER
1357 | INTEL_PTE_WRITE);
1358
1359 PMAP_UNLOCK(map);
1360
1361 return;
1362
1363}
1364
1365
1366
1367/*
1368 * Routine: pmap_expand
1369 *
1370 * Expands a pmap to be able to map the specified virtual address.
1371 *
1372 * Allocates new virtual memory for the P0 or P1 portion of the
1373 * pmap, then re-maps the physical pages that were in the old
1374 * pmap to be in the new pmap.
1375 *
1376 * Must be called with the pmap system and the pmap unlocked,
1377 * since these must be unlocked to use vm_allocate or vm_deallocate.
1378 * Thus it must be called in a loop that checks whether the map
1379 * has been expanded enough.
1380 * (We won't loop forever, since page tables aren't shrunk.)
1381 */
1382void
1383pmap_expand(
1384 pmap_t map,
1385 vm_map_offset_t vaddr)
1386{
1387 pt_entry_t *pdp;
1388 register vm_page_t m;
1389 register pmap_paddr_t pa;
1390 uint64_t i;
1391 ppnum_t pn;
1392
1393
1394 /*
1395 * For the kernel, the virtual address must be in or above the basement
1396 * which is for kexts and is in the 512GB immediately below the kernel..
1397 * XXX - should use VM_MIN_KERNEL_AND_KEXT_ADDRESS not KERNEL_BASEMENT
1398 */
1399 if (map == kernel_pmap &&
1400 !(vaddr >= KERNEL_BASEMENT && vaddr <= VM_MAX_KERNEL_ADDRESS))
1401 panic("pmap_expand: bad vaddr 0x%llx for kernel pmap", vaddr);
1402
1403
1404 while ((pdp = pmap64_pde(map, vaddr)) == PD_ENTRY_NULL) {
1405 /* need room for another pde entry */
1406 pmap_expand_pdpt(map, vaddr);
1407 }
1408
1409 /*
1410 * Allocate a VM page for the pde entries.
1411 */
1412 while ((m = vm_page_grab()) == VM_PAGE_NULL)
1413 VM_PAGE_WAIT();
1414
1415 /*
1416 * put the page into the pmap's obj list so it
1417 * can be found later.
1418 */
1419 pn = m->phys_page;
1420 pa = i386_ptob(pn);
1421 i = pdeidx(map, vaddr);
1422
1423 /*
1424 * Zero the page.
1425 */
1426 pmap_zero_page(pn);
1427
1428 vm_page_lockspin_queues();
1429 vm_page_wire(m);
1430 vm_page_unlock_queues();
1431
1432 OSAddAtomic(1, &inuse_ptepages_count);
1433
1434 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1435 vm_object_lock(map->pm_obj);
1436
1437 PMAP_LOCK(map);
1438
1439 /*
1440 * See if someone else expanded us first
1441 */
1442 if (pmap_pte(map, vaddr) != PT_ENTRY_NULL) {
1443 PMAP_UNLOCK(map);
1444 vm_object_unlock(map->pm_obj);
1445
1446 VM_PAGE_FREE(m);
1447
1448 OSAddAtomic(-1, &inuse_ptepages_count);
1449 return;
1450 }
1451
1452#if 0 /* DEBUG */
1453 if (0 != vm_page_lookup(map->pm_obj, (vm_object_offset_t)i)) {
1454 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
1455 map, map->pm_obj, vaddr, i);
1456 }
1457#endif
1458 vm_page_insert(m, map->pm_obj, (vm_object_offset_t)i);
1459 vm_object_unlock(map->pm_obj);
1460
1461 /*
1462 * Set the page directory entry for this page table.
1463 */
1464 pdp = pmap_pde(map, vaddr);
1465 pmap_store_pte(pdp, pa_to_pte(pa)
1466 | INTEL_PTE_VALID
1467 | INTEL_PTE_USER
1468 | INTEL_PTE_WRITE);
1469
1470 PMAP_UNLOCK(map);
1471
1472 return;
1473}
1474
1475/* On K64 machines with more than 32GB of memory, pmap_steal_memory
1476 * will allocate past the 1GB of pre-expanded virtual kernel area. This
1477 * function allocates all the page tables using memory from the same pool
1478 * that pmap_steal_memory uses, rather than calling vm_page_grab (which
1479 * isn't available yet). */
1480void
1481pmap_pre_expand(pmap_t pmap, vm_map_offset_t vaddr) {
1482 ppnum_t pn;
1483 pt_entry_t *pte;
1484
1485 PMAP_LOCK(pmap);
1486
1487 if(pmap64_pdpt(pmap, vaddr) == PDPT_ENTRY_NULL) {
0b4c1975 1488 if (!pmap_next_page_hi(&pn))
b0d623f7
A
1489 panic("pmap_pre_expand");
1490
1491 pmap_zero_page(pn);
1492
1493 pte = pmap64_pml4(pmap, vaddr);
1494
1495 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1496 | INTEL_PTE_VALID
1497 | INTEL_PTE_USER
1498 | INTEL_PTE_WRITE);
1499 }
1500
1501 if(pmap64_pde(pmap, vaddr) == PD_ENTRY_NULL) {
0b4c1975 1502 if (!pmap_next_page_hi(&pn))
b0d623f7
A
1503 panic("pmap_pre_expand");
1504
1505 pmap_zero_page(pn);
1506
1507 pte = pmap64_pdpt(pmap, vaddr);
1508
1509 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1510 | INTEL_PTE_VALID
1511 | INTEL_PTE_USER
1512 | INTEL_PTE_WRITE);
1513 }
1514
1515 if(pmap_pte(pmap, vaddr) == PT_ENTRY_NULL) {
0b4c1975 1516 if (!pmap_next_page_hi(&pn))
b0d623f7
A
1517 panic("pmap_pre_expand");
1518
1519 pmap_zero_page(pn);
1520
1521 pte = pmap64_pde(pmap, vaddr);
1522
1523 pmap_store_pte(pte, pa_to_pte(i386_ptob(pn))
1524 | INTEL_PTE_VALID
1525 | INTEL_PTE_USER
1526 | INTEL_PTE_WRITE);
1527 }
1528
1529 PMAP_UNLOCK(pmap);
1530}
1531
1532/*
1533 * pmap_sync_page_data_phys(ppnum_t pa)
1534 *
1535 * Invalidates all of the instruction cache on a physical page and
1536 * pushes any dirty data from the data cache for the same physical page
1537 * Not required in i386.
1538 */
1539void
1540pmap_sync_page_data_phys(__unused ppnum_t pa)
1541{
1542 return;
1543}
1544
1545/*
1546 * pmap_sync_page_attributes_phys(ppnum_t pa)
1547 *
1548 * Write back and invalidate all cachelines on a physical page.
1549 */
1550void
1551pmap_sync_page_attributes_phys(ppnum_t pa)
1552{
1553 cache_flush_page_phys(pa);
1554}
1555
1556
1557
1558#ifdef CURRENTLY_UNUSED_AND_UNTESTED
1559
1560int collect_ref;
1561int collect_unref;
1562
1563/*
1564 * Routine: pmap_collect
1565 * Function:
1566 * Garbage collects the physical map system for
1567 * pages which are no longer used.
1568 * Success need not be guaranteed -- that is, there
1569 * may well be pages which are not referenced, but
1570 * others may be collected.
1571 * Usage:
1572 * Called by the pageout daemon when pages are scarce.
1573 */
1574void
1575pmap_collect(
1576 pmap_t p)
1577{
1578 register pt_entry_t *pdp, *ptp;
1579 pt_entry_t *eptp;
1580 int wired;
1581
1582 if (p == PMAP_NULL)
1583 return;
1584
1585 if (p == kernel_pmap)
1586 return;
1587
1588 /*
1589 * Garbage collect map.
1590 */
1591 PMAP_LOCK(p);
1592
1593 for (pdp = (pt_entry_t *)p->dirbase;
1594 pdp < (pt_entry_t *)&p->dirbase[(UMAXPTDI+1)];
1595 pdp++)
1596 {
1597 if (*pdp & INTEL_PTE_VALID) {
1598 if(*pdp & INTEL_PTE_REF) {
1599 pmap_store_pte(pdp, *pdp & ~INTEL_PTE_REF);
1600 collect_ref++;
1601 } else {
1602 collect_unref++;
1603 ptp = pmap_pte(p, pdetova(pdp - (pt_entry_t *)p->dirbase));
1604 eptp = ptp + NPTEPG;
1605
1606 /*
1607 * If the pte page has any wired mappings, we cannot
1608 * free it.
1609 */
1610 wired = 0;
1611 {
1612 register pt_entry_t *ptep;
1613 for (ptep = ptp; ptep < eptp; ptep++) {
1614 if (iswired(*ptep)) {
1615 wired = 1;
1616 break;
1617 }
1618 }
1619 }
1620 if (!wired) {
1621 /*
1622 * Remove the virtual addresses mapped by this pte page.
1623 */
1624 pmap_remove_range(p,
1625 pdetova(pdp - (pt_entry_t *)p->dirbase),
1626 ptp,
1627 eptp);
1628
1629 /*
1630 * Invalidate the page directory pointer.
1631 */
1632 pmap_store_pte(pdp, 0x0);
1633
1634 PMAP_UNLOCK(p);
1635
1636 /*
1637 * And free the pte page itself.
1638 */
1639 {
1640 register vm_page_t m;
1641
1642 vm_object_lock(p->pm_obj);
1643
1644 m = vm_page_lookup(p->pm_obj,(vm_object_offset_t)(pdp - (pt_entry_t *)&p->dirbase[0]));
1645 if (m == VM_PAGE_NULL)
1646 panic("pmap_collect: pte page not in object");
1647
1648 VM_PAGE_FREE(m);
1649
1650 OSAddAtomic(-1, &inuse_ptepages_count);
1651
1652 vm_object_unlock(p->pm_obj);
1653 }
1654
1655 PMAP_LOCK(p);
1656 }
1657 }
1658 }
1659 }
1660
1661 PMAP_UPDATE_TLBS(p, 0x0, 0xFFFFFFFFFFFFF000ULL);
1662 PMAP_UNLOCK(p);
1663 return;
1664
1665}
1666#endif
1667
1668
1669void
1670pmap_copy_page(ppnum_t src, ppnum_t dst)
1671{
1672 bcopy_phys((addr64_t)i386_ptob(src),
1673 (addr64_t)i386_ptob(dst),
1674 PAGE_SIZE);
1675}
1676
1677
1678/*
1679 * Routine: pmap_pageable
1680 * Function:
1681 * Make the specified pages (by pmap, offset)
1682 * pageable (or not) as requested.
1683 *
1684 * A page which is not pageable may not take
1685 * a fault; therefore, its page table entry
1686 * must remain valid for the duration.
1687 *
1688 * This routine is merely advisory; pmap_enter
1689 * will specify that these pages are to be wired
1690 * down (or not) as appropriate.
1691 */
1692void
1693pmap_pageable(
1694 __unused pmap_t pmap,
1695 __unused vm_map_offset_t start_addr,
1696 __unused vm_map_offset_t end_addr,
1697 __unused boolean_t pageable)
1698{
1699#ifdef lint
1700 pmap++; start_addr++; end_addr++; pageable++;
1701#endif /* lint */
1702}
1703
1704/*
1705 * Clear specified attribute bits.
1706 */
1707void
1708phys_attribute_clear(
1709 ppnum_t pn,
1710 int bits)
1711{
1712 pv_rooted_entry_t pv_h;
1713 pv_hashed_entry_t pv_e;
1714 pt_entry_t *pte;
1715 int pai;
1716 pmap_t pmap;
1717
1718 pmap_intr_assert();
1719 assert(pn != vm_page_fictitious_addr);
1720 if (pn == vm_page_guard_addr)
1721 return;
1722
1723 pai = ppn_to_pai(pn);
1724
1725 if (!IS_MANAGED_PAGE(pai)) {
1726 /*
1727 * Not a managed page.
1728 */
1729 return;
1730 }
1731
1732
1733 PMAP_TRACE(PMAP_CODE(PMAP__ATTRIBUTE_CLEAR) | DBG_FUNC_START,
1734 pn, bits, 0, 0, 0);
1735
1736 pv_h = pai_to_pvh(pai);
1737
1738 LOCK_PVH(pai);
1739
1740 /*
1741 * Walk down PV list, clearing all modify or reference bits.
1742 * We do not have to lock the pv_list because we have
1743 * the entire pmap system locked.
1744 */
1745 if (pv_h->pmap != PMAP_NULL) {
1746 /*
1747 * There are some mappings.
1748 */
1749
1750 pv_e = (pv_hashed_entry_t)pv_h;
1751
1752 do {
1753 vm_map_offset_t va;
1754
1755 pmap = pv_e->pmap;
1756 va = pv_e->va;
1757
1758 /*
1759 * Clear modify and/or reference bits.
1760 */
1761 pte = pmap_pte(pmap, va);
1762 pmap_update_pte(pte, *pte, (*pte & ~bits));
1763 /* Ensure all processors using this translation
1764 * invalidate this TLB entry. The invalidation *must*
1765 * follow the PTE update, to ensure that the TLB
1766 * shadow of the 'D' bit (in particular) is
1767 * synchronized with the updated PTE.
1768 */
1769 PMAP_UPDATE_TLBS(pmap, va, va + PAGE_SIZE);
1770
1771 pv_e = (pv_hashed_entry_t)queue_next(&pv_e->qlink);
1772
1773 } while (pv_e != (pv_hashed_entry_t)pv_h);
1774 }
1775 pmap_phys_attributes[pai] &= ~bits;
1776
1777 UNLOCK_PVH(pai);
1778
1779 PMAP_TRACE(PMAP_CODE(PMAP__ATTRIBUTE_CLEAR) | DBG_FUNC_END,
1780 0, 0, 0, 0, 0);
1781}
1782
1783/*
1784 * Check specified attribute bits.
1785 */
1786int
1787phys_attribute_test(
1788 ppnum_t pn,
1789 int bits)
1790{
1791 pv_rooted_entry_t pv_h;
1792 pv_hashed_entry_t pv_e;
1793 pt_entry_t *pte;
1794 int pai;
1795 pmap_t pmap;
1796 int attributes = 0;
1797
1798 pmap_intr_assert();
1799 assert(pn != vm_page_fictitious_addr);
1800 if (pn == vm_page_guard_addr)
1801 return 0;
1802
1803 pai = ppn_to_pai(pn);
1804
1805 if (!IS_MANAGED_PAGE(pai)) {
1806 /*
1807 * Not a managed page.
1808 */
1809 return 0;
1810 }
1811
1812 /*
1813 * super fast check... if bits already collected
1814 * no need to take any locks...
1815 * if not set, we need to recheck after taking
1816 * the lock in case they got pulled in while
1817 * we were waiting for the lock
1818 */
1819 if ((pmap_phys_attributes[pai] & bits) == bits)
1820 return bits;
1821
1822 pv_h = pai_to_pvh(pai);
1823
1824 LOCK_PVH(pai);
1825
1826 attributes = pmap_phys_attributes[pai] & bits;
1827
1828
1829 /*
1830 * Walk down PV list, checking the mappings until we
1831 * reach the end or we've found the attributes we've asked for
1832 * We do not have to lock the pv_list because we have
1833 * the entire pmap system locked.
1834 */
1835 if (attributes != bits &&
1836 pv_h->pmap != PMAP_NULL) {
1837 /*
1838 * There are some mappings.
1839 */
1840 pv_e = (pv_hashed_entry_t)pv_h;
1841 do {
1842 vm_map_offset_t va;
1843
1844 pmap = pv_e->pmap;
1845 va = pv_e->va;
1846 /*
1847 * first make sure any processor actively
1848 * using this pmap, flushes its TLB state
1849 */
1850 PMAP_UPDATE_TLBS(pmap, va, va + PAGE_SIZE);
1851
1852 /*
1853 * pick up modify and/or reference bits from mapping
1854 */
1855
1856 pte = pmap_pte(pmap, va);
1857 attributes |= (int)(*pte & bits);
1858
1859 pv_e = (pv_hashed_entry_t)queue_next(&pv_e->qlink);
1860
1861 } while ((attributes != bits) &&
1862 (pv_e != (pv_hashed_entry_t)pv_h));
1863 }
1864
1865 UNLOCK_PVH(pai);
1866 return (attributes);
1867}
1868
1869/*
1870 * Set specified attribute bits.
1871 */
1872void
1873phys_attribute_set(
1874 ppnum_t pn,
1875 int bits)
1876{
1877 int pai;
1878
1879 pmap_intr_assert();
1880 assert(pn != vm_page_fictitious_addr);
1881 if (pn == vm_page_guard_addr)
1882 return;
1883
1884 pai = ppn_to_pai(pn);
1885
1886 if (!IS_MANAGED_PAGE(pai)) {
1887 /* Not a managed page. */
1888 return;
1889 }
1890
1891 LOCK_PVH(pai);
1892 pmap_phys_attributes[pai] |= bits;
1893 UNLOCK_PVH(pai);
1894}
1895
1896/*
1897 * Set the modify bit on the specified physical page.
1898 */
1899
1900void
1901pmap_set_modify(ppnum_t pn)
1902{
1903 phys_attribute_set(pn, PHYS_MODIFIED);
1904}
1905
1906/*
1907 * Clear the modify bits on the specified physical page.
1908 */
1909
1910void
1911pmap_clear_modify(ppnum_t pn)
1912{
1913 phys_attribute_clear(pn, PHYS_MODIFIED);
1914}
1915
1916/*
1917 * pmap_is_modified:
1918 *
1919 * Return whether or not the specified physical page is modified
1920 * by any physical maps.
1921 */
1922
1923boolean_t
1924pmap_is_modified(ppnum_t pn)
1925{
1926 if (phys_attribute_test(pn, PHYS_MODIFIED))
1927 return TRUE;
1928 return FALSE;
1929}
1930
1931/*
1932 * pmap_clear_reference:
1933 *
1934 * Clear the reference bit on the specified physical page.
1935 */
1936
1937void
1938pmap_clear_reference(ppnum_t pn)
1939{
1940 phys_attribute_clear(pn, PHYS_REFERENCED);
1941}
1942
1943void
1944pmap_set_reference(ppnum_t pn)
1945{
1946 phys_attribute_set(pn, PHYS_REFERENCED);
1947}
1948
1949/*
1950 * pmap_is_referenced:
1951 *
1952 * Return whether or not the specified physical page is referenced
1953 * by any physical maps.
1954 */
1955
1956boolean_t
1957pmap_is_referenced(ppnum_t pn)
1958{
1959 if (phys_attribute_test(pn, PHYS_REFERENCED))
1960 return TRUE;
1961 return FALSE;
1962}
1963
1964/*
1965 * pmap_get_refmod(phys)
1966 * returns the referenced and modified bits of the specified
1967 * physical page.
1968 */
1969unsigned int
1970pmap_get_refmod(ppnum_t pn)
1971{
1972 int refmod;
1973 unsigned int retval = 0;
1974
1975 refmod = phys_attribute_test(pn, PHYS_MODIFIED | PHYS_REFERENCED);
1976
1977 if (refmod & PHYS_MODIFIED)
1978 retval |= VM_MEM_MODIFIED;
1979 if (refmod & PHYS_REFERENCED)
1980 retval |= VM_MEM_REFERENCED;
1981
1982 return (retval);
1983}
1984
1985/*
1986 * pmap_clear_refmod(phys, mask)
1987 * clears the referenced and modified bits as specified by the mask
1988 * of the specified physical page.
1989 */
1990void
1991pmap_clear_refmod(ppnum_t pn, unsigned int mask)
1992{
1993 unsigned int x86Mask;
1994
1995 x86Mask = ( ((mask & VM_MEM_MODIFIED)? PHYS_MODIFIED : 0)
1996 | ((mask & VM_MEM_REFERENCED)? PHYS_REFERENCED : 0));
1997 phys_attribute_clear(pn, x86Mask);
1998}
1999
2000void
2001invalidate_icache(__unused vm_offset_t addr,
2002 __unused unsigned cnt,
2003 __unused int phys)
2004{
2005 return;
2006}
2007
2008void
2009flush_dcache(__unused vm_offset_t addr,
2010 __unused unsigned count,
2011 __unused int phys)
2012{
2013 return;
2014}
2015
2016#if CONFIG_DTRACE
2017/*
2018 * Constrain DTrace copyin/copyout actions
2019 */
2020extern kern_return_t dtrace_copyio_preflight(addr64_t);
2021extern kern_return_t dtrace_copyio_postflight(addr64_t);
2022
2023kern_return_t dtrace_copyio_preflight(__unused addr64_t va)
2024{
2025 thread_t thread = current_thread();
2026
2027 if (current_map() == kernel_map)
2028 return KERN_FAILURE;
2029 else if (get_cr3() != thread->map->pmap->pm_cr3)
2030 return KERN_FAILURE;
2031 else if (thread->machine.specFlags & CopyIOActive)
2032 return KERN_FAILURE;
2033 else
2034 return KERN_SUCCESS;
2035}
2036
2037kern_return_t dtrace_copyio_postflight(__unused addr64_t va)
2038{
2039 return KERN_SUCCESS;
2040}
2041#endif /* CONFIG_DTRACE */
2042
2043#include <mach_vm_debug.h>
2044#if MACH_VM_DEBUG
2045#include <vm/vm_debug.h>
2046
2047int
2048pmap_list_resident_pages(
2049 __unused pmap_t pmap,
2050 __unused vm_offset_t *listp,
2051 __unused int space)
2052{
2053 return 0;
2054}
2055#endif /* MACH_VM_DEBUG */
2056
2057
2058
2059/* temporary workaround */
2060boolean_t
2061coredumpok(__unused vm_map_t map, __unused vm_offset_t va)
2062{
2063#if 0
2064 pt_entry_t *ptep;
2065
2066 ptep = pmap_pte(map->pmap, va);
2067 if (0 == ptep)
2068 return FALSE;
2069 return ((*ptep & (INTEL_PTE_NCACHE | INTEL_PTE_WIRED)) != (INTEL_PTE_NCACHE | INTEL_PTE_WIRED));
2070#else
2071 return TRUE;
2072#endif
2073}
2074
2075
2076boolean_t
2077phys_page_exists(ppnum_t pn)
2078{
2079 assert(pn != vm_page_fictitious_addr);
2080
2081 if (!pmap_initialized)
2082 return TRUE;
2083
2084 if (pn == vm_page_guard_addr)
2085 return FALSE;
2086
2087 if (!IS_MANAGED_PAGE(ppn_to_pai(pn)))
2088 return FALSE;
2089
2090 return TRUE;
2091}
2092
b0d623f7
A
2093void
2094pmap_switch(pmap_t tpmap)
2095{
2096 spl_t s;
2097
2098 s = splhigh(); /* Make sure interruptions are disabled */
2099 set_dirbase(tpmap, current_thread());
2100 splx(s);
2101}
2102
2103
2104/*
2105 * disable no-execute capability on
2106 * the specified pmap
2107 */
2108void
2109pmap_disable_NX(pmap_t pmap)
2110{
2111 pmap->nx_enabled = 0;
2112}
2113
2114void
2115pt_fake_zone_info(
2116 int *count,
2117 vm_size_t *cur_size,
2118 vm_size_t *max_size,
2119 vm_size_t *elem_size,
2120 vm_size_t *alloc_size,
2121 int *collectable,
2122 int *exhaustable)
2123{
2124 *count = inuse_ptepages_count;
2125 *cur_size = PAGE_SIZE * inuse_ptepages_count;
2126 *max_size = PAGE_SIZE * (inuse_ptepages_count +
2127 vm_page_inactive_count +
2128 vm_page_active_count +
2129 vm_page_free_count);
2130 *elem_size = PAGE_SIZE;
2131 *alloc_size = PAGE_SIZE;
2132
2133 *collectable = 1;
2134 *exhaustable = 0;
2135}
2136
060df5ea
A
2137extern long NMIPI_acks;
2138
b0d623f7
A
2139static inline void
2140pmap_cpuset_NMIPI(cpu_set cpu_mask) {
2141 unsigned int cpu, cpu_bit;
2142 uint64_t deadline;
2143
2144 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2145 if (cpu_mask & cpu_bit)
2146 cpu_NMI_interrupt(cpu);
2147 }
2148 deadline = mach_absolute_time() + (LockTimeOut);
2149 while (mach_absolute_time() < deadline)
2150 cpu_pause();
2151}
2152
2153/*
2154 * Called with pmap locked, we:
2155 * - scan through per-cpu data to see which other cpus need to flush
2156 * - send an IPI to each non-idle cpu to be flushed
2157 * - wait for all to signal back that they are inactive or we see that
2158 * they are at a safe point (idle).
2159 * - flush the local tlb if active for this pmap
2160 * - return ... the caller will unlock the pmap
2161 */
2162void
2163pmap_flush_tlbs(pmap_t pmap)
2164{
2165 unsigned int cpu;
2166 unsigned int cpu_bit;
2167 cpu_set cpus_to_signal;
2168 unsigned int my_cpu = cpu_number();
2169 pmap_paddr_t pmap_cr3 = pmap->pm_cr3;
2170 boolean_t flush_self = FALSE;
2171 uint64_t deadline;
2172
2173 assert((processor_avail_count < 2) ||
2174 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
2175
2176 /*
2177 * Scan other cpus for matching active or task CR3.
2178 * For idle cpus (with no active map) we mark them invalid but
2179 * don't signal -- they'll check as they go busy.
2180 */
2181 cpus_to_signal = 0;
2182 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2183 if (!cpu_datap(cpu)->cpu_running)
2184 continue;
2185 uint64_t cpu_active_cr3 = CPU_GET_ACTIVE_CR3(cpu);
2186 uint64_t cpu_task_cr3 = CPU_GET_TASK_CR3(cpu);
2187
2188 if ((pmap_cr3 == cpu_task_cr3) ||
2189 (pmap_cr3 == cpu_active_cr3) ||
2190 (pmap->pm_shared) ||
2191 (pmap == kernel_pmap)) {
2192 if (cpu == my_cpu) {
2193 flush_self = TRUE;
2194 continue;
2195 }
2196 cpu_datap(cpu)->cpu_tlb_invalid = TRUE;
2197 __asm__ volatile("mfence");
2198
2199 /*
2200 * We don't need to signal processors which will flush
2201 * lazily at the idle state or kernel boundary.
2202 * For example, if we're invalidating the kernel pmap,
2203 * processors currently in userspace don't need to flush
2204 * their TLBs until the next time they enter the kernel.
2205 * Alterations to the address space of a task active
2206 * on a remote processor result in a signal, to
2207 * account for copy operations. (There may be room
2208 * for optimization in such cases).
2209 * The order of the loads below with respect
2210 * to the store to the "cpu_tlb_invalid" field above
2211 * is important--hence the barrier.
2212 */
2213 if (CPU_CR3_IS_ACTIVE(cpu) &&
2214 (pmap_cr3 == CPU_GET_ACTIVE_CR3(cpu) ||
2215 pmap->pm_shared ||
2216 (pmap_cr3 == CPU_GET_TASK_CR3(cpu)))) {
2217 cpus_to_signal |= cpu_bit;
2218 i386_signal_cpu(cpu, MP_TLB_FLUSH, ASYNC);
2219 }
2220 }
2221 }
2222
2223 PMAP_TRACE(PMAP_CODE(PMAP__FLUSH_TLBS) | DBG_FUNC_START,
2224 pmap, cpus_to_signal, flush_self, 0, 0);
2225
2226 /*
2227 * Flush local tlb if required.
2228 * Do this now to overlap with other processors responding.
2229 */
2230 if (flush_self)
2231 flush_tlb();
2232
2233 if (cpus_to_signal) {
2234 cpu_set cpus_to_respond = cpus_to_signal;
2235
2236 deadline = mach_absolute_time() + LockTimeOut;
2237 /*
2238 * Wait for those other cpus to acknowledge
2239 */
2240 while (cpus_to_respond != 0) {
060df5ea 2241 long orig_acks = 0;
b0d623f7
A
2242
2243 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2244 if ((cpus_to_respond & cpu_bit) != 0) {
2245 if (!cpu_datap(cpu)->cpu_running ||
2246 cpu_datap(cpu)->cpu_tlb_invalid == FALSE ||
2247 !CPU_CR3_IS_ACTIVE(cpu)) {
2248 cpus_to_respond &= ~cpu_bit;
2249 }
2250 cpu_pause();
2251 }
2252 if (cpus_to_respond == 0)
2253 break;
2254 }
060df5ea
A
2255 if (mach_absolute_time() > deadline) {
2256 if (machine_timeout_suspended())
2257 continue;
2258 pmap_tlb_flush_timeout = TRUE;
2259 orig_acks = NMIPI_acks;
2260 pmap_cpuset_NMIPI(cpus_to_respond);
2261
2262 panic("TLB invalidation IPI timeout: "
2263 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%lx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2264 cpus_to_respond, orig_acks, NMIPI_acks);
2265 }
b0d623f7
A
2266 }
2267 }
2268
2269 PMAP_TRACE(PMAP_CODE(PMAP__FLUSH_TLBS) | DBG_FUNC_END,
2270 pmap, cpus_to_signal, flush_self, 0, 0);
2271}
2272
2273void
2274process_pmap_updates(void)
2275{
2276 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2277
2278 flush_tlb();
2279
2280 current_cpu_datap()->cpu_tlb_invalid = FALSE;
2281 __asm__ volatile("mfence");
2282}
2283
2284void
2285pmap_update_interrupt(void)
2286{
2287 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT) | DBG_FUNC_START,
2288 0, 0, 0, 0, 0);
2289
2290 process_pmap_updates();
2291
2292 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT) | DBG_FUNC_END,
2293 0, 0, 0, 0, 0);
2294}
2295
2296
2297unsigned int
2298pmap_cache_attributes(ppnum_t pn)
2299{
2300 return IS_MANAGED_PAGE(ppn_to_pai(pn)) ? VM_WIMG_COPYBACK
2301 : VM_WIMG_IO;
2302}
2303
2304