4 * Copyright (c) 1991 Regents of the University of California.
5 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
7 * Copyright (c) 2003 Peter Wemm
8 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
9 * Copyright (c) 2008, 2009 The DragonFly Project.
10 * Copyright (c) 2008, 2009 Jordan Gordeev.
11 * All rights reserved.
13 * This code is derived from software contributed to Berkeley by
14 * the Systems Programming Group of the University of Utah Computer
15 * Science Department and William Jolitz of UUNET Technologies Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
46 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
50 * Manages physical address maps.
56 #include "opt_msgbuf.h"
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
62 #include <sys/msgbuf.h>
63 #include <sys/vmmeter.h>
65 #include <sys/vmspace.h>
68 #include <vm/vm_param.h>
69 #include <sys/sysctl.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_zone.h>
81 #include <sys/thread2.h>
82 #include <sys/sysref2.h>
83 #include <sys/spinlock2.h>
84 #include <vm/vm_page2.h>
86 #include <machine/cputypes.h>
87 #include <machine/md_var.h>
88 #include <machine/specialreg.h>
89 #include <machine/smp.h>
90 #include <machine/globaldata.h>
91 #include <machine/pmap.h>
92 #include <machine/pmap_inval.h>
101 #define PMAP_KEEP_PDIRS
102 #ifndef PMAP_SHPGPERPROC
103 #define PMAP_SHPGPERPROC 1000
106 #if defined(DIAGNOSTIC)
107 #define PMAP_DIAGNOSTIC
112 #if !defined(PMAP_DIAGNOSTIC)
113 #define PMAP_INLINE __inline
119 * Get PDEs and PTEs for user/kernel address space
121 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
122 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
124 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & VPTE_V) != 0)
125 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & VPTE_WIRED) != 0)
126 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & VPTE_M) != 0)
127 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & VPTE_A) != 0)
128 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & VPTE_V) != 0)
131 * Given a map and a machine independent protection code,
132 * convert to a vax protection code.
134 #define pte_prot(m, p) \
135 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
136 static int protection_codes[8];
138 struct pmap kernel_pmap;
139 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
141 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
143 static vm_object_t kptobj;
147 static uint64_t KPDphys; /* phys addr of kernel level 2 */
148 uint64_t KPDPphys; /* phys addr of kernel level 3 */
149 uint64_t KPML4phys; /* phys addr of kernel level 4 */
151 extern int vmm_enabled;
152 extern void *vkernel_stack;
155 * Data for the pv entry allocation mechanism
157 static vm_zone_t pvzone;
158 static struct vm_zone pvzone_store;
159 static struct vm_object pvzone_obj;
160 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
161 static int pmap_pagedaemon_waken = 0;
162 static struct pv_entry *pvinit;
165 * All those kernel PT submaps that BSD is so fond of
167 pt_entry_t *CMAP1 = NULL, *ptmmap;
168 caddr_t CADDR1 = NULL;
169 static pt_entry_t *msgbufmap;
173 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
174 static pv_entry_t get_pv_entry (void);
175 static void i386_protection_init (void);
176 static __inline void pmap_clearbit (vm_page_t m, int bit);
178 static void pmap_remove_all (vm_page_t m);
179 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
181 static void pmap_remove_page (struct pmap *pmap, vm_offset_t va);
182 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
184 static boolean_t pmap_testbit (vm_page_t m, int bit);
185 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
186 vm_page_t mpte, vm_page_t m);
188 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
190 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
191 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
193 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
195 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
196 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t);
201 * Super fast pmap_pte routine best used when scanning the pv lists.
202 * This eliminates many course-grained invltlb calls. Note that many of
203 * the pv list scans are across different pmaps and it is very wasteful
204 * to do an entire invltlb when checking a single mapping.
206 * Should only be called while in a critical section.
209 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
212 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
214 return pmap_pte(pmap, va);
218 /* Return a non-clipped PD index for a given VA */
219 static __inline vm_pindex_t
220 pmap_pde_pindex(vm_offset_t va)
222 return va >> PDRSHIFT;
225 /* Return various clipped indexes for a given VA */
226 static __inline vm_pindex_t
227 pmap_pte_index(vm_offset_t va)
230 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
233 static __inline vm_pindex_t
234 pmap_pde_index(vm_offset_t va)
237 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
240 static __inline vm_pindex_t
241 pmap_pdpe_index(vm_offset_t va)
244 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
247 static __inline vm_pindex_t
248 pmap_pml4e_index(vm_offset_t va)
251 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
254 /* Return a pointer to the PML4 slot that corresponds to a VA */
255 static __inline pml4_entry_t *
256 pmap_pml4e(pmap_t pmap, vm_offset_t va)
259 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
262 /* Return a pointer to the PDP slot that corresponds to a VA */
263 static __inline pdp_entry_t *
264 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
268 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & VPTE_FRAME);
269 return (&pdpe[pmap_pdpe_index(va)]);
272 /* Return a pointer to the PDP slot that corresponds to a VA */
273 static __inline pdp_entry_t *
274 pmap_pdpe(pmap_t pmap, vm_offset_t va)
278 pml4e = pmap_pml4e(pmap, va);
279 if ((*pml4e & VPTE_V) == 0)
281 return (pmap_pml4e_to_pdpe(pml4e, va));
284 /* Return a pointer to the PD slot that corresponds to a VA */
285 static __inline pd_entry_t *
286 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
290 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & VPTE_FRAME);
291 return (&pde[pmap_pde_index(va)]);
294 /* Return a pointer to the PD slot that corresponds to a VA */
295 static __inline pd_entry_t *
296 pmap_pde(pmap_t pmap, vm_offset_t va)
300 pdpe = pmap_pdpe(pmap, va);
301 if (pdpe == NULL || (*pdpe & VPTE_V) == 0)
303 return (pmap_pdpe_to_pde(pdpe, va));
306 /* Return a pointer to the PT slot that corresponds to a VA */
307 static __inline pt_entry_t *
308 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
312 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & VPTE_FRAME);
313 return (&pte[pmap_pte_index(va)]);
316 /* Return a pointer to the PT slot that corresponds to a VA */
317 static __inline pt_entry_t *
318 pmap_pte(pmap_t pmap, vm_offset_t va)
322 pde = pmap_pde(pmap, va);
323 if (pde == NULL || (*pde & VPTE_V) == 0)
325 if ((*pde & VPTE_PS) != 0) /* compat with i386 pmap_pte() */
326 return ((pt_entry_t *)pde);
327 return (pmap_pde_to_pte(pde, va));
332 PMAP_INLINE pt_entry_t *
333 vtopte(vm_offset_t va)
335 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT +
336 NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
338 return (PTmap + ((va >> PAGE_SHIFT) & mask));
341 static __inline pd_entry_t *
342 vtopde(vm_offset_t va)
344 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT +
345 NPML4EPGSHIFT)) - 1);
347 return (PDmap + ((va >> PDRSHIFT) & mask));
350 static PMAP_INLINE pt_entry_t *
351 vtopte(vm_offset_t va)
354 x = pmap_pte(&kernel_pmap, va);
359 static __inline pd_entry_t *
360 vtopde(vm_offset_t va)
363 x = pmap_pde(&kernel_pmap, va);
370 allocpages(vm_paddr_t *firstaddr, int n)
376 bzero((void *)ret, n * PAGE_SIZE);
378 *firstaddr += n * PAGE_SIZE;
383 create_dmap_vmm(vm_paddr_t *firstaddr)
386 int pml4_stack_index;
393 uint64_t KPDP_DMAP_phys = allocpages(firstaddr, NDMPML4E);
394 uint64_t KPDP_VSTACK_phys = allocpages(firstaddr, 1);
395 uint64_t KPD_VSTACK_phys = allocpages(firstaddr, 1);
397 pml4_entry_t *KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
398 pdp_entry_t *KPDP_DMAP_virt = (pdp_entry_t *)PHYS_TO_DMAP(KPDP_DMAP_phys);
399 pdp_entry_t *KPDP_VSTACK_virt = (pdp_entry_t *)PHYS_TO_DMAP(KPDP_VSTACK_phys);
400 pd_entry_t *KPD_VSTACK_virt = (pd_entry_t *)PHYS_TO_DMAP(KPD_VSTACK_phys);
402 bzero(KPDP_DMAP_virt, NDMPML4E * PAGE_SIZE);
403 bzero(KPDP_VSTACK_virt, 1 * PAGE_SIZE);
404 bzero(KPD_VSTACK_virt, 1 * PAGE_SIZE);
406 do_cpuid(0x80000001, regs);
407 amd_feature = regs[3];
409 /* Build the mappings for the first 512GB */
410 if (amd_feature & AMDID_PAGE1GB) {
411 /* In pages of 1 GB, if supported */
412 for (i = 0; i < NPDPEPG; i++) {
413 KPDP_DMAP_virt[i] = ((uint64_t)i << PDPSHIFT);
414 KPDP_DMAP_virt[i] |= VPTE_RW | VPTE_V | VPTE_PS | VPTE_U;
417 /* In page of 2MB, otherwise */
418 for (i = 0; i < NPDPEPG; i++) {
419 uint64_t KPD_DMAP_phys = allocpages(firstaddr, 1);
420 pd_entry_t *KPD_DMAP_virt = (pd_entry_t *)PHYS_TO_DMAP(KPD_DMAP_phys);
422 bzero(KPD_DMAP_virt, PAGE_SIZE);
424 KPDP_DMAP_virt[i] = KPD_DMAP_phys;
425 KPDP_DMAP_virt[i] |= VPTE_RW | VPTE_V | VPTE_U;
427 /* For each PD, we have to allocate NPTEPG PT */
428 for (j = 0; j < NPTEPG; j++) {
429 KPD_DMAP_virt[j] = (i << PDPSHIFT) | (j << PDRSHIFT);
430 KPD_DMAP_virt[j] |= VPTE_RW | VPTE_V | VPTE_PS | VPTE_U;
435 /* DMAP for the first 512G */
436 KPML4virt[0] = KPDP_DMAP_phys;
437 KPML4virt[0] |= VPTE_RW | VPTE_V | VPTE_U;
439 /* create a 2 MB map of the new stack */
440 pml4_stack_index = (uint64_t)&stack_addr >> PML4SHIFT;
441 KPML4virt[pml4_stack_index] = KPDP_VSTACK_phys;
442 KPML4virt[pml4_stack_index] |= VPTE_RW | VPTE_V | VPTE_U;
444 pdp_stack_index = ((uint64_t)&stack_addr & PML4MASK) >> PDPSHIFT;
445 KPDP_VSTACK_virt[pdp_stack_index] = KPD_VSTACK_phys;
446 KPDP_VSTACK_virt[pdp_stack_index] |= VPTE_RW | VPTE_V | VPTE_U;
448 pd_stack_index = ((uint64_t)&stack_addr & PDPMASK) >> PDRSHIFT;
449 KPD_VSTACK_virt[pd_stack_index] = (uint64_t) vkernel_stack;
450 KPD_VSTACK_virt[pd_stack_index] |= VPTE_RW | VPTE_V | VPTE_U | VPTE_PS;
454 create_pagetables(vm_paddr_t *firstaddr, int64_t ptov_offset)
457 pml4_entry_t *KPML4virt;
458 pdp_entry_t *KPDPvirt;
461 int kpml4i = pmap_pml4e_index(ptov_offset);
462 int kpdpi = pmap_pdpe_index(ptov_offset);
463 int kpdi = pmap_pde_index(ptov_offset);
466 * Calculate NKPT - number of kernel page tables. We have to
467 * accomodoate prealloction of the vm_page_array, dump bitmap,
468 * MSGBUF_SIZE, and other stuff. Be generous.
470 * Maxmem is in pages.
472 nkpt = (Maxmem * (sizeof(struct vm_page) * 2) + MSGBUF_SIZE) / NBPDR;
476 KPML4phys = allocpages(firstaddr, 1);
477 KPDPphys = allocpages(firstaddr, NKPML4E);
478 KPDphys = allocpages(firstaddr, NKPDPE);
479 KPTphys = allocpages(firstaddr, nkpt);
481 KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
482 KPDPvirt = (pdp_entry_t *)PHYS_TO_DMAP(KPDPphys);
483 KPDvirt = (pd_entry_t *)PHYS_TO_DMAP(KPDphys);
484 KPTvirt = (pt_entry_t *)PHYS_TO_DMAP(KPTphys);
486 bzero(KPML4virt, 1 * PAGE_SIZE);
487 bzero(KPDPvirt, NKPML4E * PAGE_SIZE);
488 bzero(KPDvirt, NKPDPE * PAGE_SIZE);
489 bzero(KPTvirt, nkpt * PAGE_SIZE);
491 /* Now map the page tables at their location within PTmap */
492 for (i = 0; i < nkpt; i++) {
493 KPDvirt[i + kpdi] = KPTphys + (i << PAGE_SHIFT);
494 KPDvirt[i + kpdi] |= VPTE_RW | VPTE_V | VPTE_U;
497 /* And connect up the PD to the PDP */
498 for (i = 0; i < NKPDPE; i++) {
499 KPDPvirt[i + kpdpi] = KPDphys + (i << PAGE_SHIFT);
500 KPDPvirt[i + kpdpi] |= VPTE_RW | VPTE_V | VPTE_U;
503 /* And recursively map PML4 to itself in order to get PTmap */
504 KPML4virt[PML4PML4I] = KPML4phys;
505 KPML4virt[PML4PML4I] |= VPTE_RW | VPTE_V | VPTE_U;
507 /* Connect the KVA slot up to the PML4 */
508 KPML4virt[kpml4i] = KPDPphys;
509 KPML4virt[kpml4i] |= VPTE_RW | VPTE_V | VPTE_U;
513 * Typically used to initialize a fictitious page by vm/device_pager.c
516 pmap_page_init(struct vm_page *m)
519 TAILQ_INIT(&m->md.pv_list);
523 * Bootstrap the system enough to run with virtual memory.
525 * On the i386 this is called after mapping has already been enabled
526 * and just syncs the pmap module with what has already been done.
527 * [We can't call it easily with mapping off since the kernel is not
528 * mapped with PA == VA, hence we would have to relocate every address
529 * from the linked base (virtual) address "KERNBASE" to the actual
530 * (physical) address starting relative to 0]
533 pmap_bootstrap(vm_paddr_t *firstaddr, int64_t ptov_offset)
539 * Create an initial set of page tables to run the kernel in.
541 create_pagetables(firstaddr, ptov_offset);
543 /* Create the DMAP for the VMM */
545 create_dmap_vmm(firstaddr);
548 virtual_start = KvaStart;
549 virtual_end = KvaEnd;
552 * Initialize protection array.
554 i386_protection_init();
557 * The kernel's pmap is statically allocated so we don't have to use
558 * pmap_create, which is unlikely to work correctly at this part of
559 * the boot sequence (XXX and which no longer exists).
561 * The kernel_pmap's pm_pteobj is used only for locking and not
564 kernel_pmap.pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
565 kernel_pmap.pm_count = 1;
566 /* don't allow deactivation */
567 CPUMASK_ASSALLONES(kernel_pmap.pm_active);
568 kernel_pmap.pm_pteobj = NULL; /* see pmap_init */
569 TAILQ_INIT(&kernel_pmap.pm_pvlist);
570 TAILQ_INIT(&kernel_pmap.pm_pvlist_free);
571 lwkt_token_init(&kernel_pmap.pm_token, "kpmap_tok");
572 spin_init(&kernel_pmap.pm_spin, "pmapbootstrap");
575 * Reserve some special page table entries/VA space for temporary
578 #define SYSMAP(c, p, v, n) \
579 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
582 pte = pmap_pte(&kernel_pmap, va);
584 * CMAP1/CMAP2 are used for zeroing and copying pages.
586 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
592 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
596 * ptvmmap is used for reading arbitrary physical pages via
599 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
602 * msgbufp is used to map the system message buffer.
603 * XXX msgbufmap is not used.
605 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
606 atop(round_page(MSGBUF_SIZE)))
611 /* Not ready to do an invltlb yet for VMM*/
618 * Initialize the pmap module.
619 * Called by vm_init, to initialize any structures that the pmap
620 * system needs to map virtual memory.
621 * pmap_init has been enhanced to support in a fairly consistant
622 * way, discontiguous physical memory.
631 * object for kernel page table pages
633 /* JG I think the number can be arbitrary */
634 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
635 kernel_pmap.pm_pteobj = kptobj;
638 * Allocate memory for random pmap data structures. Includes the
641 for(i = 0; i < vm_page_array_size; i++) {
644 m = &vm_page_array[i];
645 TAILQ_INIT(&m->md.pv_list);
646 m->md.pv_list_count = 0;
650 * init the pv free list
652 initial_pvs = vm_page_array_size;
653 if (initial_pvs < MINPV)
655 pvzone = &pvzone_store;
656 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
657 initial_pvs * sizeof (struct pv_entry));
658 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
662 * Now it is safe to enable pv_table recording.
664 pmap_initialized = TRUE;
668 * Initialize the address space (zone) for the pv_entries. Set a
669 * high water mark so that the system can recover from excessive
670 * numbers of pv entries.
675 int shpgperproc = PMAP_SHPGPERPROC;
677 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
678 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
679 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
680 pv_entry_high_water = 9 * (pv_entry_max / 10);
681 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
685 /***************************************************
686 * Low level helper routines.....
687 ***************************************************/
690 * The modification bit is not tracked for any pages in this range. XXX
691 * such pages in this maps should always use pmap_k*() functions and not
694 * XXX User and kernel address spaces are independant for virtual kernels,
695 * this function only applies to the kernel pmap.
698 pmap_track_modified(pmap_t pmap, vm_offset_t va)
700 if (pmap != &kernel_pmap)
702 if ((va < clean_sva) || (va >= clean_eva))
709 * Extract the physical page address associated with the map/VA pair.
714 pmap_extract(pmap_t pmap, vm_offset_t va)
718 pd_entry_t pde, *pdep;
720 lwkt_gettoken(&vm_token);
722 pdep = pmap_pde(pmap, va);
726 if ((pde & VPTE_PS) != 0) {
728 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
730 pte = pmap_pde_to_pte(pdep, va);
731 rtval = (*pte & VPTE_FRAME) | (va & PAGE_MASK);
735 lwkt_reltoken(&vm_token);
740 * Similar to extract but checks protections, SMP-friendly short-cut for
741 * vm_fault_page[_quick]().
744 pmap_fault_page_quick(pmap_t pmap __unused, vm_offset_t vaddr __unused,
745 vm_prot_t prot __unused)
751 * Routine: pmap_kextract
753 * Extract the physical page address associated
754 * kernel virtual address.
757 pmap_kextract(vm_offset_t va)
762 KKASSERT(va >= KvaStart && va < KvaEnd);
765 * The DMAP region is not included in [KvaStart, KvaEnd)
768 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
769 pa = DMAP_TO_PHYS(va);
775 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
778 * Beware of a concurrent promotion that changes the
779 * PDE at this point! For example, vtopte() must not
780 * be used to access the PTE because it would use the
781 * new PDE. It is, however, safe to use the old PDE
782 * because the page table page is preserved by the
785 pa = *pmap_pde_to_pte(&pde, va);
786 pa = (pa & VPTE_FRAME) | (va & PAGE_MASK);
794 /***************************************************
795 * Low level mapping routines.....
796 ***************************************************/
799 * Enter a mapping into kernel_pmap. Mappings created in this fashion
800 * are not managed. Mappings must be immediately accessible on all cpus.
802 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
803 * real pmap and handle related races before storing the new vpte.
806 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
811 KKASSERT(va >= KvaStart && va < KvaEnd);
812 npte = pa | VPTE_RW | VPTE_V | VPTE_U;
815 pmap_inval_pte(pte, &kernel_pmap, va);
820 * Enter an unmanaged KVA mapping for the private use of the current
821 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
824 * It is illegal for the mapping to be accessed by other cpus unleess
825 * pmap_kenter_sync*() is called.
828 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
833 KKASSERT(va >= KvaStart && va < KvaEnd);
835 npte = (vpte_t)pa | VPTE_RW | VPTE_V | VPTE_U;
839 pmap_inval_pte_quick(pte, &kernel_pmap, va);
844 * Synchronize a kvm mapping originally made for the private use on
845 * some other cpu so it can be used on our cpu. Turns out to be the
846 * same madvise() call, because we have to sync the real pmaps anyway.
848 * XXX add MADV_RESYNC to improve performance.
851 pmap_kenter_sync_quick(vm_offset_t va)
853 cpu_invlpg((void *)va);
857 * Remove an unmanaged mapping created with pmap_kenter*().
860 pmap_kremove(vm_offset_t va)
864 KKASSERT(va >= KvaStart && va < KvaEnd);
868 pmap_inval_pte(pte, &kernel_pmap, va);
873 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
874 * only with this cpu.
876 * Unfortunately because we optimize new entries by testing VPTE_V later
877 * on, we actually still have to synchronize with all the cpus. XXX maybe
878 * store a junk value and test against 0 in the other places instead?
881 pmap_kremove_quick(vm_offset_t va)
885 KKASSERT(va >= KvaStart && va < KvaEnd);
889 pmap_inval_pte(pte, &kernel_pmap, va); /* NOT _quick */
894 * Used to map a range of physical addresses into kernel
895 * virtual address space.
897 * For now, VM is already on, we only need to map the
901 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
903 return PHYS_TO_DMAP(start);
908 * Map a set of unmanaged VM pages into KVM.
911 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
915 end_va = va + count * PAGE_SIZE;
916 KKASSERT(va >= KvaStart && end_va < KvaEnd);
918 while (va < end_va) {
923 pmap_inval_pte(pte, &kernel_pmap, va);
924 *pte = VM_PAGE_TO_PHYS(*m) | VPTE_RW | VPTE_V | VPTE_U;
931 * Undo the effects of pmap_qenter*().
934 pmap_qremove(vm_offset_t va, int count)
938 end_va = va + count * PAGE_SIZE;
939 KKASSERT(va >= KvaStart && end_va < KvaEnd);
941 while (va < end_va) {
946 pmap_inval_pte(pte, &kernel_pmap, va);
953 * This routine works like vm_page_lookup() but also blocks as long as the
954 * page is busy. This routine does not busy the page it returns.
956 * Unless the caller is managing objects whos pages are in a known state,
957 * the call should be made with a critical section held so the page's object
958 * association remains valid on return.
961 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
965 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
966 m = vm_page_lookup_busy_wait(object, pindex, FALSE, "pplookp");
972 * Create a new thread and optionally associate it with a (new) process.
973 * NOTE! the new thread's cpu may not equal the current cpu.
976 pmap_init_thread(thread_t td)
978 /* enforce pcb placement */
979 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
980 td->td_savefpu = &td->td_pcb->pcb_save;
981 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */
985 * This routine directly affects the fork perf for a process.
988 pmap_init_proc(struct proc *p)
992 /***************************************************
993 * Page table page management routines.....
994 ***************************************************/
996 static __inline int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va,
1000 * This routine unholds page table pages, and if the hold count
1001 * drops to zero, then it decrements the wire count.
1003 * We must recheck that this is the last hold reference after busy-sleeping
1007 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
1009 vm_page_busy_wait(m, FALSE, "pmuwpt");
1010 KASSERT(m->queue == PQ_NONE,
1011 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1013 if (m->hold_count == 1) {
1015 * Unmap the page table page.
1018 /* pmap_inval_add(info, pmap, -1); */
1020 if (m->pindex >= (NUPDE + NUPDPE)) {
1023 pml4 = pmap_pml4e(pmap, va);
1025 } else if (m->pindex >= NUPDE) {
1028 pdp = pmap_pdpe(pmap, va);
1033 pd = pmap_pde(pmap, va);
1037 KKASSERT(pmap->pm_stats.resident_count > 0);
1038 --pmap->pm_stats.resident_count;
1040 if (pmap->pm_ptphint == m)
1041 pmap->pm_ptphint = NULL;
1043 if (m->pindex < NUPDE) {
1044 /* We just released a PT, unhold the matching PD */
1047 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & VPTE_FRAME);
1048 pmap_unwire_pte_hold(pmap, va, pdpg);
1050 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1051 /* We just released a PD, unhold the matching PDP */
1054 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & VPTE_FRAME);
1055 pmap_unwire_pte_hold(pmap, va, pdppg);
1059 * This was our last hold, the page had better be unwired
1060 * after we decrement wire_count.
1062 * FUTURE NOTE: shared page directory page could result in
1063 * multiple wire counts.
1067 KKASSERT(m->wire_count == 0);
1068 atomic_add_int(&vmstats.v_wire_count, -1);
1069 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1071 vm_page_free_zero(m);
1074 KKASSERT(m->hold_count > 1);
1082 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
1084 KKASSERT(m->hold_count > 0);
1085 if (m->hold_count > 1) {
1089 return _pmap_unwire_pte_hold(pmap, va, m);
1094 * After removing a page table entry, this routine is used to
1095 * conditionally free the page, and manage the hold/wire counts.
1098 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1100 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1101 vm_pindex_t ptepindex;
1103 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1107 * page table pages in the kernel_pmap are not managed.
1109 if (pmap == &kernel_pmap)
1111 ptepindex = pmap_pde_pindex(va);
1112 if (pmap->pm_ptphint &&
1113 (pmap->pm_ptphint->pindex == ptepindex)) {
1114 mpte = pmap->pm_ptphint;
1116 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1117 pmap->pm_ptphint = mpte;
1118 vm_page_wakeup(mpte);
1122 return pmap_unwire_pte_hold(pmap, va, mpte);
1126 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
1127 * just dummy it up so it works well enough for fork().
1129 * In DragonFly, process pmaps may only be used to manipulate user address
1130 * space, never kernel address space.
1133 pmap_pinit0(struct pmap *pmap)
1139 * Initialize a preallocated and zeroed pmap structure,
1140 * such as one in a vmspace structure.
1143 pmap_pinit(struct pmap *pmap)
1148 * No need to allocate page table space yet but we do need a valid
1149 * page directory table.
1151 if (pmap->pm_pml4 == NULL) {
1153 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1157 * Allocate an object for the ptes
1159 if (pmap->pm_pteobj == NULL)
1160 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1163 * Allocate the page directory page, unless we already have
1164 * one cached. If we used the cached page the wire_count will
1165 * already be set appropriately.
1167 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1168 ptdpg = vm_page_grab(pmap->pm_pteobj,
1169 NUPDE + NUPDPE + PML4PML4I,
1170 VM_ALLOC_NORMAL | VM_ALLOC_RETRY |
1172 pmap->pm_pdirm = ptdpg;
1173 vm_page_flag_clear(ptdpg, PG_MAPPED);
1174 vm_page_wire(ptdpg);
1175 vm_page_wakeup(ptdpg);
1176 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1179 CPUMASK_ASSZERO(pmap->pm_active);
1180 pmap->pm_ptphint = NULL;
1181 TAILQ_INIT(&pmap->pm_pvlist);
1182 TAILQ_INIT(&pmap->pm_pvlist_free);
1183 spin_init(&pmap->pm_spin, "pmapinit");
1184 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1185 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1186 pmap->pm_stats.resident_count = 1;
1190 * Clean up a pmap structure so it can be physically freed. This routine
1191 * is called by the vmspace dtor function. A great deal of pmap data is
1192 * left passively mapped to improve vmspace management so we have a bit
1193 * of cleanup work to do here.
1198 pmap_puninit(pmap_t pmap)
1202 KKASSERT(CPUMASK_TESTZERO(pmap->pm_active));
1203 if ((p = pmap->pm_pdirm) != NULL) {
1204 KKASSERT(pmap->pm_pml4 != NULL);
1205 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1206 vm_page_busy_wait(p, FALSE, "pgpun");
1208 atomic_add_int(&vmstats.v_wire_count, -1);
1209 vm_page_free_zero(p);
1210 pmap->pm_pdirm = NULL;
1212 if (pmap->pm_pml4) {
1213 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1214 pmap->pm_pml4 = NULL;
1216 if (pmap->pm_pteobj) {
1217 vm_object_deallocate(pmap->pm_pteobj);
1218 pmap->pm_pteobj = NULL;
1223 * Wire in kernel global address entries. To avoid a race condition
1224 * between pmap initialization and pmap_growkernel, this procedure
1225 * adds the pmap to the master list (which growkernel scans to update),
1226 * then copies the template.
1228 * In a virtual kernel there are no kernel global address entries.
1233 pmap_pinit2(struct pmap *pmap)
1235 spin_lock(&pmap_spin);
1236 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1237 spin_unlock(&pmap_spin);
1241 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1242 * 0 on failure (if the procedure had to sleep).
1244 * When asked to remove the page directory page itself, we actually just
1245 * leave it cached so we do not have to incur the SMP inval overhead of
1246 * removing the kernel mapping. pmap_puninit() will take care of it.
1249 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1252 * This code optimizes the case of freeing non-busy
1253 * page-table pages. Those pages are zero now, and
1254 * might as well be placed directly into the zero queue.
1256 if (vm_page_busy_try(p, FALSE)) {
1257 vm_page_sleep_busy(p, FALSE, "pmaprl");
1262 * Remove the page table page from the processes address space.
1264 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1266 * We are the pml4 table itself.
1268 /* XXX anything to do here? */
1269 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1271 * We are a PDP page.
1272 * We look for the PML4 entry that points to us.
1274 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1275 KKASSERT(m4 != NULL);
1276 pml4_entry_t *pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1277 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1278 KKASSERT(pml4[idx] != 0);
1281 /* JG What about wire_count? */
1282 } else if (p->pindex >= NUPDE) {
1285 * We look for the PDP entry that points to us.
1287 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1288 KKASSERT(m3 != NULL);
1289 pdp_entry_t *pdp = (pdp_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1290 int idx = (p->pindex - NUPDE) % NPDPEPG;
1291 KKASSERT(pdp[idx] != 0);
1294 /* JG What about wire_count? */
1296 /* We are a PT page.
1297 * We look for the PD entry that points to us.
1299 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1300 KKASSERT(m2 != NULL);
1301 pd_entry_t *pd = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1302 int idx = p->pindex % NPDEPG;
1305 /* JG What about wire_count? */
1307 KKASSERT(pmap->pm_stats.resident_count > 0);
1308 --pmap->pm_stats.resident_count;
1310 if (p->hold_count) {
1311 panic("pmap_release: freeing held pt page "
1312 "pmap=%p pg=%p dmap=%p pi=%ld {%ld,%ld,%ld}",
1313 pmap, p, (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(p)),
1314 p->pindex, NUPDE, NUPDPE, PML4PML4I);
1316 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1317 pmap->pm_ptphint = NULL;
1320 * We leave the top-level page table page cached, wired, and mapped in
1321 * the pmap until the dtor function (pmap_puninit()) gets called.
1322 * However, still clean it up so we can set PG_ZERO.
1324 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1325 bzero(pmap->pm_pml4, PAGE_SIZE);
1326 vm_page_flag_set(p, PG_ZERO);
1331 atomic_add_int(&vmstats.v_wire_count, -1);
1332 /* JG eventually revert to using vm_page_free_zero() */
1339 * this routine is called if the page table page is not
1343 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1345 vm_page_t m, pdppg, pdpg;
1348 * Find or fabricate a new pagetable page. Handle allocation
1349 * races by checking m->valid.
1351 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1352 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1354 KASSERT(m->queue == PQ_NONE,
1355 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1358 * Increment the hold count for the page we will be returning to
1365 * Map the pagetable page into the process address space, if
1366 * it isn't already there.
1368 ++pmap->pm_stats.resident_count;
1370 if (ptepindex >= (NUPDE + NUPDPE)) {
1372 vm_pindex_t pml4index;
1374 /* Wire up a new PDP page */
1375 pml4index = ptepindex - (NUPDE + NUPDPE);
1376 pml4 = &pmap->pm_pml4[pml4index];
1377 *pml4 = VM_PAGE_TO_PHYS(m) |
1378 VPTE_RW | VPTE_V | VPTE_U |
1380 } else if (ptepindex >= NUPDE) {
1381 vm_pindex_t pml4index;
1382 vm_pindex_t pdpindex;
1386 /* Wire up a new PD page */
1387 pdpindex = ptepindex - NUPDE;
1388 pml4index = pdpindex >> NPML4EPGSHIFT;
1390 pml4 = &pmap->pm_pml4[pml4index];
1391 if ((*pml4 & VPTE_V) == 0) {
1392 /* Have to allocate a new PDP page, recurse */
1393 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1400 /* Add reference to the PDP page */
1401 pdppg = PHYS_TO_VM_PAGE(*pml4 & VPTE_FRAME);
1402 pdppg->hold_count++;
1404 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1406 /* Now find the pdp page */
1407 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1408 KKASSERT(*pdp == 0); /* JG DEBUG64 */
1409 *pdp = VM_PAGE_TO_PHYS(m) | VPTE_RW | VPTE_V | VPTE_U |
1412 vm_pindex_t pml4index;
1413 vm_pindex_t pdpindex;
1418 /* Wire up a new PT page */
1419 pdpindex = ptepindex >> NPDPEPGSHIFT;
1420 pml4index = pdpindex >> NPML4EPGSHIFT;
1422 /* First, find the pdp and check that its valid. */
1423 pml4 = &pmap->pm_pml4[pml4index];
1424 if ((*pml4 & VPTE_V) == 0) {
1425 /* We miss a PDP page. We ultimately need a PD page.
1426 * Recursively allocating a PD page will allocate
1427 * the missing PDP page and will also allocate
1428 * the PD page we need.
1430 /* Have to allocate a new PD page, recurse */
1431 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1437 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1438 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1440 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1441 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1442 if ((*pdp & VPTE_V) == 0) {
1443 /* Have to allocate a new PD page, recurse */
1444 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1451 /* Add reference to the PD page */
1452 pdpg = PHYS_TO_VM_PAGE(*pdp & VPTE_FRAME);
1456 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & VPTE_FRAME);
1458 /* Now we know where the page directory page is */
1459 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1460 KKASSERT(*pd == 0); /* JG DEBUG64 */
1461 *pd = VM_PAGE_TO_PHYS(m) | VPTE_RW | VPTE_V | VPTE_U |
1466 * Set the page table hint
1468 pmap->pm_ptphint = m;
1469 vm_page_flag_set(m, PG_MAPPED);
1476 * Determine the page table page required to access the VA in the pmap
1477 * and allocate it if necessary. Return a held vm_page_t for the page.
1479 * Only used with user pmaps.
1482 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1484 vm_pindex_t ptepindex;
1488 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1491 * Calculate pagetable page index
1493 ptepindex = pmap_pde_pindex(va);
1496 * Get the page directory entry
1498 pd = pmap_pde(pmap, va);
1501 * This supports switching from a 2MB page to a
1504 if (pd != NULL && (*pd & (VPTE_PS | VPTE_V)) == (VPTE_PS | VPTE_V)) {
1505 panic("no promotion/demotion yet");
1513 * If the page table page is mapped, we just increment the
1514 * hold count, and activate it.
1516 if (pd != NULL && (*pd & VPTE_V) != 0) {
1517 /* YYY hint is used here on i386 */
1518 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1519 pmap->pm_ptphint = m;
1525 * Here if the pte page isn't mapped, or if it has been deallocated.
1527 return _pmap_allocpte(pmap, ptepindex);
1531 /***************************************************
1532 * Pmap allocation/deallocation routines.
1533 ***************************************************/
1536 * Release any resources held by the given physical map.
1537 * Called when a pmap initialized by pmap_pinit is being released.
1538 * Should only be called if the map contains no valid mappings.
1540 * Caller must hold pmap->pm_token
1542 static int pmap_release_callback(struct vm_page *p, void *data);
1545 pmap_release(struct pmap *pmap)
1547 vm_object_t object = pmap->pm_pteobj;
1548 struct rb_vm_page_scan_info info;
1550 KKASSERT(pmap != &kernel_pmap);
1552 #if defined(DIAGNOSTIC)
1553 if (object->ref_count != 1)
1554 panic("pmap_release: pteobj reference count != 1");
1558 info.object = object;
1560 spin_lock(&pmap_spin);
1561 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1562 spin_unlock(&pmap_spin);
1564 vm_object_hold(object);
1568 info.limit = object->generation;
1570 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1571 pmap_release_callback, &info);
1572 if (info.error == 0 && info.mpte) {
1573 if (!pmap_release_free_page(pmap, info.mpte))
1576 } while (info.error);
1577 vm_object_drop(object);
1581 pmap_release_callback(struct vm_page *p, void *data)
1583 struct rb_vm_page_scan_info *info = data;
1585 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1589 if (!pmap_release_free_page(info->pmap, p)) {
1593 if (info->object->generation != info->limit) {
1601 * Grow the number of kernel page table entries, if needed.
1606 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
1610 vm_offset_t ptppaddr;
1612 pd_entry_t *pde, newpdir;
1617 vm_object_hold(kptobj);
1618 if (kernel_vm_end == 0) {
1619 kernel_vm_end = KvaStart;
1621 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & VPTE_V) != 0) {
1622 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1624 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1625 kernel_vm_end = kernel_map.max_offset;
1630 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1631 if (addr - 1 >= kernel_map.max_offset)
1632 addr = kernel_map.max_offset;
1633 while (kernel_vm_end < addr) {
1634 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1636 /* We need a new PDP entry */
1637 nkpg = vm_page_alloc(kptobj, nkpt,
1638 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1639 | VM_ALLOC_INTERRUPT);
1641 panic("pmap_growkernel: no memory to "
1644 paddr = VM_PAGE_TO_PHYS(nkpg);
1645 if ((nkpg->flags & PG_ZERO) == 0)
1646 pmap_zero_page(paddr);
1647 vm_page_flag_clear(nkpg, PG_ZERO);
1648 newpdp = (pdp_entry_t)(paddr |
1649 VPTE_V | VPTE_RW | VPTE_U |
1651 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1653 continue; /* try again */
1655 if ((*pde & VPTE_V) != 0) {
1656 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1657 ~(PAGE_SIZE * NPTEPG - 1);
1658 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1659 kernel_vm_end = kernel_map.max_offset;
1666 * This index is bogus, but out of the way
1668 nkpg = vm_page_alloc(kptobj, nkpt,
1671 VM_ALLOC_INTERRUPT);
1673 panic("pmap_growkernel: no memory to grow kernel");
1676 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1677 pmap_zero_page(ptppaddr);
1678 vm_page_flag_clear(nkpg, PG_ZERO);
1679 newpdir = (pd_entry_t)(ptppaddr |
1680 VPTE_V | VPTE_RW | VPTE_U |
1682 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1685 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1686 ~(PAGE_SIZE * NPTEPG - 1);
1687 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1688 kernel_vm_end = kernel_map.max_offset;
1692 vm_object_drop(kptobj);
1696 * Add a reference to the specified pmap.
1701 pmap_reference(pmap_t pmap)
1704 lwkt_gettoken(&vm_token);
1706 lwkt_reltoken(&vm_token);
1710 /************************************************************************
1711 * VMSPACE MANAGEMENT *
1712 ************************************************************************
1714 * The VMSPACE management we do in our virtual kernel must be reflected
1715 * in the real kernel. This is accomplished by making vmspace system
1716 * calls to the real kernel.
1719 cpu_vmspace_alloc(struct vmspace *vm)
1726 * If VMM enable, don't do nothing, we
1727 * are able to use real page tables
1732 #define USER_SIZE (VM_MAX_USER_ADDRESS - VM_MIN_USER_ADDRESS)
1734 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
1735 panic("vmspace_create() failed");
1737 rp = vmspace_mmap(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1738 PROT_READ|PROT_WRITE,
1739 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
1741 if (rp == MAP_FAILED)
1742 panic("vmspace_mmap: failed");
1743 vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1745 vpte = VM_PAGE_TO_PHYS(vmspace_pmap(vm)->pm_pdirm) | VPTE_RW | VPTE_V | VPTE_U;
1746 r = vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1749 panic("vmspace_mcontrol: failed");
1753 cpu_vmspace_free(struct vmspace *vm)
1756 * If VMM enable, don't do nothing, we
1757 * are able to use real page tables
1762 if (vmspace_destroy(&vm->vm_pmap) < 0)
1763 panic("vmspace_destroy() failed");
1766 /***************************************************
1767 * page management routines.
1768 ***************************************************/
1771 * free the pv_entry back to the free list. This function may be
1772 * called from an interrupt.
1774 static __inline void
1775 free_pv_entry(pv_entry_t pv)
1778 KKASSERT(pv_entry_count >= 0);
1783 * get a new pv_entry, allocating a block from the system
1784 * when needed. This function may be called from an interrupt.
1790 if (pv_entry_high_water &&
1791 (pv_entry_count > pv_entry_high_water) &&
1792 (pmap_pagedaemon_waken == 0)) {
1793 pmap_pagedaemon_waken = 1;
1794 wakeup(&vm_pages_needed);
1796 return zalloc(pvzone);
1800 * This routine is very drastic, but can save the system
1810 static int warningdone=0;
1812 if (pmap_pagedaemon_waken == 0)
1814 lwkt_gettoken(&vm_token);
1815 pmap_pagedaemon_waken = 0;
1817 if (warningdone < 5) {
1818 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1822 for (i = 0; i < vm_page_array_size; i++) {
1823 m = &vm_page_array[i];
1824 if (m->wire_count || m->hold_count)
1826 if (vm_page_busy_try(m, TRUE) == 0) {
1827 if (m->wire_count == 0 && m->hold_count == 0) {
1833 lwkt_reltoken(&vm_token);
1838 * If it is the first entry on the list, it is actually
1839 * in the header and we must copy the following entry up
1840 * to the header. Otherwise we must search the list for
1841 * the entry. In either case we free the now unused entry.
1843 * caller must hold vm_token.
1846 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1851 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1852 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1853 if (pmap == pv->pv_pmap && va == pv->pv_va)
1857 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1858 if (va == pv->pv_va)
1864 * Note that pv_ptem is NULL if the page table page itself is not
1865 * managed, even if the page being removed IS managed.
1868 /* JGXXX When can 'pv' be NULL? */
1870 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1871 m->md.pv_list_count--;
1872 atomic_add_int(&m->object->agg_pv_list_count, -1);
1873 KKASSERT(m->md.pv_list_count >= 0);
1874 if (TAILQ_EMPTY(&m->md.pv_list))
1875 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1876 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1877 ++pmap->pm_generation;
1878 KKASSERT(pmap->pm_pteobj != NULL);
1879 vm_object_hold(pmap->pm_pteobj);
1880 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1881 vm_object_drop(pmap->pm_pteobj);
1888 * Create a pv entry for page at pa for (pmap, va). If the page table page
1889 * holding the VA is managed, mpte will be non-NULL.
1892 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1897 pv = get_pv_entry();
1902 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1903 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1904 m->md.pv_list_count++;
1905 atomic_add_int(&m->object->agg_pv_list_count, 1);
1911 * pmap_remove_pte: do the things to unmap a page in a process
1914 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1919 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1920 if (oldpte & VPTE_WIRED)
1921 --pmap->pm_stats.wired_count;
1922 KKASSERT(pmap->pm_stats.wired_count >= 0);
1926 * Machines that don't support invlpg, also don't support
1927 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1931 cpu_invlpg((void *)va);
1933 KKASSERT(pmap->pm_stats.resident_count > 0);
1934 --pmap->pm_stats.resident_count;
1935 if (oldpte & VPTE_MANAGED) {
1936 m = PHYS_TO_VM_PAGE(oldpte);
1937 if (oldpte & VPTE_M) {
1938 #if defined(PMAP_DIAGNOSTIC)
1939 if (pmap_nw_modified(oldpte)) {
1940 kprintf("pmap_remove: modified page not "
1941 "writable: va: 0x%lx, pte: 0x%lx\n",
1945 if (pmap_track_modified(pmap, va))
1948 if (oldpte & VPTE_A)
1949 vm_page_flag_set(m, PG_REFERENCED);
1950 return pmap_remove_entry(pmap, m, va);
1952 return pmap_unuse_pt(pmap, va, NULL);
1961 * Remove a single page from a process address space.
1963 * This function may not be called from an interrupt if the pmap is
1967 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1971 pte = pmap_pte(pmap, va);
1974 if ((*pte & VPTE_V) == 0)
1976 pmap_remove_pte(pmap, pte, va);
1980 * Remove the given range of addresses from the specified map.
1982 * It is assumed that the start and end are properly rounded to
1985 * This function may not be called from an interrupt if the pmap is
1991 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1993 vm_offset_t va_next;
1994 pml4_entry_t *pml4e;
1996 pd_entry_t ptpaddr, *pde;
2002 vm_object_hold(pmap->pm_pteobj);
2003 lwkt_gettoken(&vm_token);
2004 KKASSERT(pmap->pm_stats.resident_count >= 0);
2005 if (pmap->pm_stats.resident_count == 0) {
2006 lwkt_reltoken(&vm_token);
2007 vm_object_drop(pmap->pm_pteobj);
2012 * special handling of removing one page. a very
2013 * common operation and easy to short circuit some
2016 if (sva + PAGE_SIZE == eva) {
2017 pde = pmap_pde(pmap, sva);
2018 if (pde && (*pde & VPTE_PS) == 0) {
2019 pmap_remove_page(pmap, sva);
2020 lwkt_reltoken(&vm_token);
2021 vm_object_drop(pmap->pm_pteobj);
2026 for (; sva < eva; sva = va_next) {
2027 pml4e = pmap_pml4e(pmap, sva);
2028 if ((*pml4e & VPTE_V) == 0) {
2029 va_next = (sva + NBPML4) & ~PML4MASK;
2035 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2036 if ((*pdpe & VPTE_V) == 0) {
2037 va_next = (sva + NBPDP) & ~PDPMASK;
2044 * Calculate index for next page table.
2046 va_next = (sva + NBPDR) & ~PDRMASK;
2050 pde = pmap_pdpe_to_pde(pdpe, sva);
2054 * Weed out invalid mappings.
2060 * Check for large page.
2062 if ((ptpaddr & VPTE_PS) != 0) {
2063 /* JG FreeBSD has more complex treatment here */
2064 KKASSERT(*pde != 0);
2065 pmap_inval_pde(pde, pmap, sva);
2066 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2071 * Limit our scan to either the end of the va represented
2072 * by the current page table page, or to the end of the
2073 * range being removed.
2079 * NOTE: pmap_remove_pte() can block.
2081 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2085 if (pmap_remove_pte(pmap, pte, sva))
2089 lwkt_reltoken(&vm_token);
2090 vm_object_drop(pmap->pm_pteobj);
2094 * Removes this physical page from all physical maps in which it resides.
2095 * Reflects back modify bits to the pager.
2097 * This routine may not be called from an interrupt.
2102 pmap_remove_all(vm_page_t m)
2104 pt_entry_t *pte, tpte;
2107 #if defined(PMAP_DIAGNOSTIC)
2109 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
2112 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
2113 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
2117 lwkt_gettoken(&vm_token);
2118 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2119 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2120 --pv->pv_pmap->pm_stats.resident_count;
2122 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2123 KKASSERT(pte != NULL);
2125 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
2126 if (tpte & VPTE_WIRED)
2127 pv->pv_pmap->pm_stats.wired_count--;
2128 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
2131 vm_page_flag_set(m, PG_REFERENCED);
2134 * Update the vm_page_t clean and reference bits.
2136 if (tpte & VPTE_M) {
2137 #if defined(PMAP_DIAGNOSTIC)
2138 if (pmap_nw_modified(tpte)) {
2140 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2144 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
2147 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2148 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2149 ++pv->pv_pmap->pm_generation;
2150 m->md.pv_list_count--;
2151 atomic_add_int(&m->object->agg_pv_list_count, -1);
2152 KKASSERT(m->md.pv_list_count >= 0);
2153 if (TAILQ_EMPTY(&m->md.pv_list))
2154 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2155 vm_object_hold(pv->pv_pmap->pm_pteobj);
2156 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2157 vm_object_drop(pv->pv_pmap->pm_pteobj);
2160 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2161 lwkt_reltoken(&vm_token);
2165 * Set the physical protection on the specified range of this map
2168 * This function may not be called from an interrupt if the map is
2169 * not the kernel_pmap.
2174 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2176 vm_offset_t va_next;
2177 pml4_entry_t *pml4e;
2179 pd_entry_t ptpaddr, *pde;
2182 /* JG review for NX */
2187 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2188 pmap_remove(pmap, sva, eva);
2192 if (prot & VM_PROT_WRITE)
2195 lwkt_gettoken(&vm_token);
2197 for (; sva < eva; sva = va_next) {
2199 pml4e = pmap_pml4e(pmap, sva);
2200 if ((*pml4e & VPTE_V) == 0) {
2201 va_next = (sva + NBPML4) & ~PML4MASK;
2207 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2208 if ((*pdpe & VPTE_V) == 0) {
2209 va_next = (sva + NBPDP) & ~PDPMASK;
2215 va_next = (sva + NBPDR) & ~PDRMASK;
2219 pde = pmap_pdpe_to_pde(pdpe, sva);
2223 * Check for large page.
2225 if ((ptpaddr & VPTE_PS) != 0) {
2227 pmap_clean_pde(pde, pmap, sva);
2228 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2233 * Weed out invalid mappings. Note: we assume that the page
2234 * directory table is always allocated, and in kernel virtual.
2242 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2248 * Clean managed pages and also check the accessed
2249 * bit. Just remove write perms for unmanaged
2250 * pages. Be careful of races, turning off write
2251 * access will force a fault rather then setting
2252 * the modified bit at an unexpected time.
2254 if (*pte & VPTE_MANAGED) {
2255 pbits = pmap_clean_pte(pte, pmap, sva);
2257 if (pbits & VPTE_A) {
2258 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2259 vm_page_flag_set(m, PG_REFERENCED);
2260 atomic_clear_long(pte, VPTE_A);
2262 if (pbits & VPTE_M) {
2263 if (pmap_track_modified(pmap, sva)) {
2265 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2270 pbits = pmap_setro_pte(pte, pmap, sva);
2274 lwkt_reltoken(&vm_token);
2278 * Enter a managed page into a pmap. If the page is not wired related pmap
2279 * data can be destroyed at any time for later demand-operation.
2281 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
2282 * specified protection, and wire the mapping if requested.
2284 * NOTE: This routine may not lazy-evaluate or lose information. The
2285 * page must actually be inserted into the given map NOW.
2287 * NOTE: When entering a page at a KVA address, the pmap must be the
2293 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2294 boolean_t wired, vm_map_entry_t entry __unused)
2300 pt_entry_t origpte, newpte;
2306 va = trunc_page(va);
2308 vm_object_hold(pmap->pm_pteobj);
2309 lwkt_gettoken(&vm_token);
2312 * Get the page table page. The kernel_pmap's page table pages
2313 * are preallocated and have no associated vm_page_t.
2315 if (pmap == &kernel_pmap)
2318 mpte = pmap_allocpte(pmap, va);
2320 pde = pmap_pde(pmap, va);
2321 if (pde != NULL && (*pde & VPTE_V) != 0) {
2322 if ((*pde & VPTE_PS) != 0)
2323 panic("pmap_enter: attempted pmap_enter on 2MB page");
2324 pte = pmap_pde_to_pte(pde, va);
2326 panic("pmap_enter: invalid page directory va=%#lx", va);
2329 KKASSERT(pte != NULL);
2331 * Deal with races on the original mapping (though don't worry
2332 * about VPTE_A races) by cleaning it. This will force a fault
2333 * if an attempt is made to write to the page.
2335 pa = VM_PAGE_TO_PHYS(m);
2336 origpte = pmap_clean_pte(pte, pmap, va);
2337 opa = origpte & VPTE_FRAME;
2339 if (origpte & VPTE_PS)
2340 panic("pmap_enter: attempted pmap_enter on 2MB page");
2343 * Mapping has not changed, must be protection or wiring change.
2345 if (origpte && (opa == pa)) {
2347 * Wiring change, just update stats. We don't worry about
2348 * wiring PT pages as they remain resident as long as there
2349 * are valid mappings in them. Hence, if a user page is wired,
2350 * the PT page will be also.
2352 if (wired && ((origpte & VPTE_WIRED) == 0))
2353 ++pmap->pm_stats.wired_count;
2354 else if (!wired && (origpte & VPTE_WIRED))
2355 --pmap->pm_stats.wired_count;
2358 * Remove the extra pte reference. Note that we cannot
2359 * optimize the RO->RW case because we have adjusted the
2360 * wiring count above and may need to adjust the wiring
2367 * We might be turning off write access to the page,
2368 * so we go ahead and sense modify status.
2370 if (origpte & VPTE_MANAGED) {
2371 if ((origpte & VPTE_M) &&
2372 pmap_track_modified(pmap, va)) {
2374 om = PHYS_TO_VM_PAGE(opa);
2378 KKASSERT(m->flags & PG_MAPPED);
2383 * Mapping has changed, invalidate old range and fall through to
2384 * handle validating new mapping.
2388 err = pmap_remove_pte(pmap, pte, va);
2390 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2394 * Enter on the PV list if part of our managed memory. Note that we
2395 * raise IPL while manipulating pv_table since pmap_enter can be
2396 * called at interrupt time.
2398 if (pmap_initialized &&
2399 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2400 pmap_insert_entry(pmap, va, mpte, m);
2402 vm_page_flag_set(m, PG_MAPPED);
2406 * Increment counters
2408 ++pmap->pm_stats.resident_count;
2410 pmap->pm_stats.wired_count++;
2414 * Now validate mapping with desired protection/wiring.
2416 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | VPTE_V | VPTE_U);
2419 newpte |= VPTE_WIRED;
2420 // if (pmap != &kernel_pmap)
2424 * If the mapping or permission bits are different from the
2425 * (now cleaned) original pte, an update is needed. We've
2426 * already downgraded or invalidated the page so all we have
2427 * to do now is update the bits.
2429 * XXX should we synchronize RO->RW changes to avoid another
2432 if ((origpte & ~(VPTE_RW|VPTE_M|VPTE_A)) != newpte) {
2433 *pte = newpte | VPTE_A;
2434 if (newpte & VPTE_RW)
2435 vm_page_flag_set(m, PG_WRITEABLE);
2437 KKASSERT((newpte & VPTE_MANAGED) == 0 || (m->flags & PG_MAPPED));
2438 lwkt_reltoken(&vm_token);
2439 vm_object_drop(pmap->pm_pteobj);
2443 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2445 * Currently this routine may only be used on user pmaps, not kernel_pmap.
2450 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2455 vm_pindex_t ptepindex;
2458 KKASSERT(pmap != &kernel_pmap);
2460 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
2463 * Calculate pagetable page index
2465 ptepindex = pmap_pde_pindex(va);
2467 vm_object_hold(pmap->pm_pteobj);
2468 lwkt_gettoken(&vm_token);
2472 * Get the page directory entry
2474 ptepa = pmap_pde(pmap, va);
2477 * If the page table page is mapped, we just increment
2478 * the hold count, and activate it.
2480 if (ptepa && (*ptepa & VPTE_V) != 0) {
2481 if (*ptepa & VPTE_PS)
2482 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2483 if (pmap->pm_ptphint &&
2484 (pmap->pm_ptphint->pindex == ptepindex)) {
2485 mpte = pmap->pm_ptphint;
2487 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2488 pmap->pm_ptphint = mpte;
2489 vm_page_wakeup(mpte);
2494 mpte = _pmap_allocpte(pmap, ptepindex);
2496 } while (mpte == NULL);
2499 * Ok, now that the page table page has been validated, get the pte.
2500 * If the pte is already mapped undo mpte's hold_count and
2503 pte = pmap_pte(pmap, va);
2504 if (*pte & VPTE_V) {
2505 KKASSERT(mpte != NULL);
2506 pmap_unwire_pte_hold(pmap, va, mpte);
2507 pa = VM_PAGE_TO_PHYS(m);
2508 KKASSERT(((*pte ^ pa) & VPTE_FRAME) == 0);
2509 lwkt_reltoken(&vm_token);
2510 vm_object_drop(pmap->pm_pteobj);
2515 * Enter on the PV list if part of our managed memory
2517 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2518 pmap_insert_entry(pmap, va, mpte, m);
2519 vm_page_flag_set(m, PG_MAPPED);
2523 * Increment counters
2525 ++pmap->pm_stats.resident_count;
2527 pa = VM_PAGE_TO_PHYS(m);
2530 * Now validate mapping with RO protection
2532 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2533 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2535 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2536 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2537 /*pmap_inval_flush(&info); don't need for vkernel */
2538 lwkt_reltoken(&vm_token);
2539 vm_object_drop(pmap->pm_pteobj);
2543 * Make a temporary mapping for a physical address. This is only intended
2544 * to be used for panic dumps.
2546 * The caller is responsible for calling smp_invltlb().
2549 pmap_kenter_temporary(vm_paddr_t pa, long i)
2551 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
2552 return ((void *)crashdumpmap);
2555 #define MAX_INIT_PT (96)
2558 * This routine preloads the ptes for a given object into the specified pmap.
2559 * This eliminates the blast of soft faults on process startup and
2560 * immediately after an mmap.
2564 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2567 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2568 vm_object_t object, vm_pindex_t pindex,
2569 vm_size_t size, int limit)
2571 struct rb_vm_page_scan_info info;
2576 * We can't preinit if read access isn't set or there is no pmap
2579 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2583 * We can't preinit if the pmap is not the current pmap
2585 lp = curthread->td_lwp;
2586 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2589 psize = x86_64_btop(size);
2591 if ((object->type != OBJT_VNODE) ||
2592 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2593 (object->resident_page_count > MAX_INIT_PT))) {
2597 if (psize + pindex > object->size) {
2598 if (object->size < pindex)
2600 psize = object->size - pindex;
2607 * Use a red-black scan to traverse the requested range and load
2608 * any valid pages found into the pmap.
2610 * We cannot safely scan the object's memq unless we are in a
2611 * critical section since interrupts can remove pages from objects.
2613 info.start_pindex = pindex;
2614 info.end_pindex = pindex + psize - 1;
2620 vm_object_hold_shared(object);
2621 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2622 pmap_object_init_pt_callback, &info);
2623 vm_object_drop(object);
2628 pmap_object_init_pt_callback(vm_page_t p, void *data)
2630 struct rb_vm_page_scan_info *info = data;
2631 vm_pindex_t rel_index;
2633 * don't allow an madvise to blow away our really
2634 * free pages allocating pv entries.
2636 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2637 vmstats.v_free_count < vmstats.v_free_reserved) {
2642 * Ignore list markers and ignore pages we cannot instantly
2643 * busy (while holding the object token).
2645 if (p->flags & PG_MARKER)
2647 if (vm_page_busy_try(p, TRUE))
2649 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2650 (p->flags & PG_FICTITIOUS) == 0) {
2651 if ((p->queue - p->pc) == PQ_CACHE)
2652 vm_page_deactivate(p);
2653 rel_index = p->pindex - info->start_pindex;
2654 pmap_enter_quick(info->pmap,
2655 info->addr + x86_64_ptob(rel_index), p);
2662 * Return TRUE if the pmap is in shape to trivially
2663 * pre-fault the specified address.
2665 * Returns FALSE if it would be non-trivial or if a
2666 * pte is already loaded into the slot.
2671 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2677 lwkt_gettoken(&vm_token);
2678 pde = pmap_pde(pmap, addr);
2679 if (pde == NULL || *pde == 0) {
2682 pte = pmap_pde_to_pte(pde, addr);
2683 ret = (*pte) ? 0 : 1;
2685 lwkt_reltoken(&vm_token);
2690 * Change the wiring attribute for a map/virtual-address pair.
2692 * The mapping must already exist in the pmap.
2693 * No other requirements.
2696 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired,
2697 vm_map_entry_t entry __unused)
2704 lwkt_gettoken(&vm_token);
2705 pte = pmap_pte(pmap, va);
2707 if (wired && !pmap_pte_w(pte))
2708 pmap->pm_stats.wired_count++;
2709 else if (!wired && pmap_pte_w(pte))
2710 pmap->pm_stats.wired_count--;
2713 * Wiring is not a hardware characteristic so there is no need to
2714 * invalidate TLB. However, in an SMP environment we must use
2715 * a locked bus cycle to update the pte (if we are not using
2716 * the pmap_inval_*() API that is)... it's ok to do this for simple
2720 atomic_set_long(pte, VPTE_WIRED);
2722 atomic_clear_long(pte, VPTE_WIRED);
2723 lwkt_reltoken(&vm_token);
2727 * Copy the range specified by src_addr/len
2728 * from the source map to the range dst_addr/len
2729 * in the destination map.
2731 * This routine is only advisory and need not do anything.
2734 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2735 vm_size_t len, vm_offset_t src_addr)
2738 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2739 * valid through blocking calls, and that's just not going to
2750 * Zero the specified physical page.
2752 * This function may be called from an interrupt and no locking is
2756 pmap_zero_page(vm_paddr_t phys)
2758 vm_offset_t va = PHYS_TO_DMAP(phys);
2760 bzero((void *)va, PAGE_SIZE);
2764 * pmap_page_assertzero:
2766 * Assert that a page is empty, panic if it isn't.
2769 pmap_page_assertzero(vm_paddr_t phys)
2774 vm_offset_t virt = PHYS_TO_DMAP(phys);
2776 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
2777 if (*(int *)((char *)virt + i) != 0) {
2778 panic("pmap_page_assertzero() @ %p not zero!",
2788 * Zero part of a physical page by mapping it into memory and clearing
2789 * its contents with bzero.
2791 * off and size may not cover an area beyond a single hardware page.
2794 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2797 vm_offset_t virt = PHYS_TO_DMAP(phys);
2798 bzero((char *)virt + off, size);
2805 * Copy the physical page from the source PA to the target PA.
2806 * This function may be called from an interrupt. No locking
2810 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2812 vm_offset_t src_virt, dst_virt;
2815 src_virt = PHYS_TO_DMAP(src);
2816 dst_virt = PHYS_TO_DMAP(dst);
2817 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
2822 * pmap_copy_page_frag:
2824 * Copy the physical page from the source PA to the target PA.
2825 * This function may be called from an interrupt. No locking
2829 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2831 vm_offset_t src_virt, dst_virt;
2834 src_virt = PHYS_TO_DMAP(src);
2835 dst_virt = PHYS_TO_DMAP(dst);
2836 bcopy((char *)src_virt + (src & PAGE_MASK),
2837 (char *)dst_virt + (dst & PAGE_MASK),
2843 * Returns true if the pmap's pv is one of the first 16 pvs linked to
2844 * from this page. This count may be changed upwards or downwards
2845 * in the future; it is only necessary that true be returned for a small
2846 * subset of pmaps for proper page aging.
2848 * No other requirements.
2851 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2856 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2860 lwkt_gettoken(&vm_token);
2862 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2863 if (pv->pv_pmap == pmap) {
2864 lwkt_reltoken(&vm_token);
2872 lwkt_reltoken(&vm_token);
2878 * Remove all pages from specified address space this aids process
2879 * exit speeds. Also, this code is special cased for current
2880 * process only, but can have the more generic (and slightly slower)
2881 * mode enabled. This is much faster than pmap_remove in the case
2882 * of running down an entire address space.
2884 * No other requirements.
2887 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2889 pt_entry_t *pte, tpte;
2892 int save_generation;
2894 if (pmap->pm_pteobj)
2895 vm_object_hold(pmap->pm_pteobj);
2896 lwkt_gettoken(&vm_token);
2898 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2899 if (pv->pv_va >= eva || pv->pv_va < sva) {
2900 npv = TAILQ_NEXT(pv, pv_plist);
2904 KKASSERT(pmap == pv->pv_pmap);
2906 pte = pmap_pte(pmap, pv->pv_va);
2909 * We cannot remove wired pages from a process' mapping
2912 if (*pte & VPTE_WIRED) {
2913 npv = TAILQ_NEXT(pv, pv_plist);
2916 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2918 m = PHYS_TO_VM_PAGE(tpte & VPTE_FRAME);
2920 KASSERT(m < &vm_page_array[vm_page_array_size],
2921 ("pmap_remove_pages: bad tpte %lx", tpte));
2923 KKASSERT(pmap->pm_stats.resident_count > 0);
2924 --pmap->pm_stats.resident_count;
2927 * Update the vm_page_t clean and reference bits.
2929 if (tpte & VPTE_M) {
2933 npv = TAILQ_NEXT(pv, pv_plist);
2934 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2935 save_generation = ++pmap->pm_generation;
2937 m->md.pv_list_count--;
2938 atomic_add_int(&m->object->agg_pv_list_count, -1);
2939 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2940 if (TAILQ_EMPTY(&m->md.pv_list))
2941 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2943 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2947 * Restart the scan if we blocked during the unuse or free
2948 * calls and other removals were made.
2950 if (save_generation != pmap->pm_generation) {
2951 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2952 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2955 lwkt_reltoken(&vm_token);
2956 if (pmap->pm_pteobj)
2957 vm_object_drop(pmap->pm_pteobj);
2961 * pmap_testbit tests bits in active mappings of a VM page.
2964 pmap_testbit(vm_page_t m, int bit)
2969 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2972 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2977 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2979 * if the bit being tested is the modified bit, then
2980 * mark clean_map and ptes as never
2983 if (bit & (VPTE_A|VPTE_M)) {
2984 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2988 #if defined(PMAP_DIAGNOSTIC)
2989 if (pv->pv_pmap == NULL) {
2990 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
2994 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3005 * This routine is used to clear bits in ptes. Certain bits require special
3006 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
3008 * This routine is only called with certain VPTE_* bit combinations.
3010 static __inline void
3011 pmap_clearbit(vm_page_t m, int bit)
3017 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3023 * Loop over all current mappings setting/clearing as appropos If
3024 * setting RO do we need to clear the VAC?
3026 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3028 * don't write protect pager mappings
3030 if (bit == VPTE_RW) {
3031 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3035 #if defined(PMAP_DIAGNOSTIC)
3036 if (pv->pv_pmap == NULL) {
3037 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3043 * Careful here. We can use a locked bus instruction to
3044 * clear VPTE_A or VPTE_M safely but we need to synchronize
3045 * with the target cpus when we mess with VPTE_RW.
3047 * On virtual kernels we must force a new fault-on-write
3048 * in the real kernel if we clear the Modify bit ourselves,
3049 * otherwise the real kernel will not get a new fault and
3050 * will never set our Modify bit again.
3052 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3054 if (bit == VPTE_RW) {
3056 * We must also clear VPTE_M when clearing
3059 pbits = pmap_clean_pte(pte, pv->pv_pmap,
3063 } else if (bit == VPTE_M) {
3065 * We do not have to make the page read-only
3066 * when clearing the Modify bit. The real
3067 * kernel will make the real PTE read-only
3068 * or otherwise detect the write and set
3069 * our VPTE_M again simply by us invalidating
3070 * the real kernel VA for the pmap (as we did
3071 * above). This allows the real kernel to
3072 * handle the write fault without forwarding
3075 atomic_clear_long(pte, VPTE_M);
3076 } else if ((bit & (VPTE_RW|VPTE_M)) == (VPTE_RW|VPTE_M)) {
3078 * We've been asked to clear W & M, I guess
3079 * the caller doesn't want us to update
3080 * the dirty status of the VM page.
3082 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
3085 * We've been asked to clear bits that do
3086 * not interact with hardware.
3088 atomic_clear_long(pte, bit);
3096 * Lower the permission for all mappings to a given page.
3098 * No other requirements.
3101 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3103 /* JG NX support? */
3104 if ((prot & VM_PROT_WRITE) == 0) {
3105 lwkt_gettoken(&vm_token);
3106 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3107 pmap_clearbit(m, VPTE_RW);
3108 vm_page_flag_clear(m, PG_WRITEABLE);
3112 lwkt_reltoken(&vm_token);
3117 pmap_phys_address(vm_pindex_t ppn)
3119 return (x86_64_ptob(ppn));
3123 * Return a count of reference bits for a page, clearing those bits.
3124 * It is not necessary for every reference bit to be cleared, but it
3125 * is necessary that 0 only be returned when there are truly no
3126 * reference bits set.
3128 * XXX: The exact number of bits to check and clear is a matter that
3129 * should be tested and standardized at some point in the future for
3130 * optimal aging of shared pages.
3132 * No other requirements.
3135 pmap_ts_referenced(vm_page_t m)
3137 pv_entry_t pv, pvf, pvn;
3141 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3145 lwkt_gettoken(&vm_token);
3147 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3152 pvn = TAILQ_NEXT(pv, pv_list);
3154 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3156 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3158 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3161 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3163 if (pte && (*pte & VPTE_A)) {
3164 atomic_clear_long(pte, VPTE_A);
3170 } while ((pv = pvn) != NULL && pv != pvf);
3172 lwkt_reltoken(&vm_token);
3179 * Return whether or not the specified physical page was modified
3180 * in any physical maps.
3182 * No other requirements.
3185 pmap_is_modified(vm_page_t m)
3189 lwkt_gettoken(&vm_token);
3190 res = pmap_testbit(m, VPTE_M);
3191 lwkt_reltoken(&vm_token);
3196 * Clear the modify bits on the specified physical page.
3198 * No other requirements.
3201 pmap_clear_modify(vm_page_t m)
3203 lwkt_gettoken(&vm_token);
3204 pmap_clearbit(m, VPTE_M);
3205 lwkt_reltoken(&vm_token);
3209 * Clear the reference bit on the specified physical page.
3211 * No other requirements.
3214 pmap_clear_reference(vm_page_t m)
3216 lwkt_gettoken(&vm_token);
3217 pmap_clearbit(m, VPTE_A);
3218 lwkt_reltoken(&vm_token);
3222 * Miscellaneous support routines follow
3226 i386_protection_init(void)
3230 kp = protection_codes;
3231 for (prot = 0; prot < 8; prot++) {
3232 if (prot & VM_PROT_READ)
3233 *kp |= 0; /* if it's VALID is readeable */
3234 if (prot & VM_PROT_WRITE)
3236 if (prot & VM_PROT_EXECUTE)
3237 *kp |= 0; /* if it's VALID is executable */
3243 * Sets the memory attribute for the specified page.
3246 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
3248 /* This is a vkernel, do nothing */
3252 * Change the PAT attribute on an existing kernel memory map. Caller
3253 * must ensure that the virtual memory in question is not accessed
3254 * during the adjustment.
3257 pmap_change_attr(vm_offset_t va, vm_size_t count, int mode)
3259 /* This is a vkernel, do nothing */
3263 * Perform the pmap work for mincore
3265 * No other requirements.
3268 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3270 pt_entry_t *ptep, pte;
3274 lwkt_gettoken(&vm_token);
3275 ptep = pmap_pte(pmap, addr);
3277 if (ptep && (pte = *ptep) != 0) {
3280 val = MINCORE_INCORE;
3281 if ((pte & VPTE_MANAGED) == 0)
3284 pa = pte & VPTE_FRAME;
3286 m = PHYS_TO_VM_PAGE(pa);
3292 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3294 * Modified by someone
3296 else if (m->dirty || pmap_is_modified(m))
3297 val |= MINCORE_MODIFIED_OTHER;
3302 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3305 * Referenced by someone
3307 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3308 val |= MINCORE_REFERENCED_OTHER;
3309 vm_page_flag_set(m, PG_REFERENCED);
3313 lwkt_reltoken(&vm_token);
3318 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3319 * vmspace will be ref'd and the old one will be deref'd.
3321 * Caller must hold vmspace->vm_map.token for oldvm and newvm
3324 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3326 struct vmspace *oldvm;
3330 oldvm = p->p_vmspace;
3331 if (oldvm != newvm) {
3332 p->p_vmspace = newvm;
3333 KKASSERT(p->p_nthreads == 1);
3334 lp = RB_ROOT(&p->p_lwp_tree);
3335 pmap_setlwpvm(lp, newvm);
3345 * Set the vmspace for a LWP. The vmspace is almost universally set the
3346 * same as the process vmspace, but virtual kernels need to swap out contexts
3347 * on a per-lwp basis.
3350 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3352 struct vmspace *oldvm;
3355 oldvm = lp->lwp_vmspace;
3358 lp->lwp_vmspace = newvm;
3359 if (curthread->td_lwp != lp)
3362 * NOTE: We don't have to worry about the CPULOCK here because
3363 * the virtual kernel doesn't call this function when VMM
3364 * is enabled (and depends on the host kernel when it isn't).
3367 pmap = vmspace_pmap(newvm);
3368 ATOMIC_CPUMASK_ORBIT(pmap->pm_active, mycpu->gd_cpuid);
3369 #if defined(SWTCH_OPTIM_STATS)
3372 pmap = vmspace_pmap(oldvm);
3373 ATOMIC_CPUMASK_NANDBIT(pmap->pm_active, mycpu->gd_cpuid);
3378 * The swtch code tried to switch in a heavy weight process whos pmap
3379 * is locked by another cpu. We have to wait for the lock to clear before
3380 * the pmap can be used.
3383 pmap_interlock_wait (struct vmspace *vm)
3385 pmap_t pmap = vmspace_pmap(vm);
3387 while (pmap->pm_active_lock & CPULOCK_EXCL)
3392 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3395 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3399 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3404 * Used by kmalloc/kfree, page already exists at va
3407 pmap_kvtom(vm_offset_t va)
3411 KKASSERT(va >= KvaStart && va < KvaEnd);
3413 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));
3417 pmap_object_init(vm_object_t object)
3423 pmap_object_free(vm_object_t object)