2 * Copyright (c) 1991 Regents of the University of California.
3 * Copyright (c) 1994 John S. Dyson
4 * Copyright (c) 1994 David Greenman
5 * Copyright (c) 2003 Peter Wemm
6 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
7 * Copyright (c) 2008, 2009 The DragonFly Project.
8 * Copyright (c) 2008, 2009 Jordan Gordeev.
9 * Copyright (c) 2011 Matthew Dillon
10 * All rights reserved.
12 * This code is derived from software contributed to Berkeley by
13 * the Systems Programming Group of the University of Utah Computer
14 * Science Department and William Jolitz of UUNET Technologies Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * Manage physical address maps for x86-64 systems.
49 #include "opt_disable_pse.h"
52 #include "opt_msgbuf.h"
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
58 #include <sys/msgbuf.h>
59 #include <sys/vmmeter.h>
63 #include <vm/vm_param.h>
64 #include <sys/sysctl.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_page.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_pageout.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vm_zone.h>
76 #include <sys/thread2.h>
77 #include <sys/sysref2.h>
78 #include <sys/spinlock2.h>
79 #include <vm/vm_page2.h>
81 #include <machine/cputypes.h>
82 #include <machine/md_var.h>
83 #include <machine/specialreg.h>
84 #include <machine/smp.h>
85 #include <machine_base/apic/apicreg.h>
86 #include <machine/globaldata.h>
87 #include <machine/pmap.h>
88 #include <machine/pmap_inval.h>
89 #include <machine/inttypes.h>
93 #define PMAP_KEEP_PDIRS
94 #ifndef PMAP_SHPGPERPROC
95 #define PMAP_SHPGPERPROC 2000
98 #if defined(DIAGNOSTIC)
99 #define PMAP_DIAGNOSTIC
105 * pmap debugging will report who owns a pv lock when blocking.
109 #define PMAP_DEBUG_DECL ,const char *func, int lineno
110 #define PMAP_DEBUG_ARGS , __func__, __LINE__
111 #define PMAP_DEBUG_COPY , func, lineno
113 #define pv_get(pmap, pindex) _pv_get(pmap, pindex \
115 #define pv_lock(pv) _pv_lock(pv \
117 #define pv_hold_try(pv) _pv_hold_try(pv \
119 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp \
124 #define PMAP_DEBUG_DECL
125 #define PMAP_DEBUG_ARGS
126 #define PMAP_DEBUG_COPY
128 #define pv_get(pmap, pindex) _pv_get(pmap, pindex)
129 #define pv_lock(pv) _pv_lock(pv)
130 #define pv_hold_try(pv) _pv_hold_try(pv)
131 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp)
136 * Get PDEs and PTEs for user/kernel address space
138 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
140 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & PG_V) != 0)
141 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & PG_W) != 0)
142 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & PG_M) != 0)
143 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & PG_A) != 0)
144 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & PG_V) != 0)
147 * Given a map and a machine independent protection code,
148 * convert to a vax protection code.
150 #define pte_prot(m, p) \
151 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
152 static int protection_codes[8];
154 struct pmap kernel_pmap;
155 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
157 vm_paddr_t avail_start; /* PA of first available physical page */
158 vm_paddr_t avail_end; /* PA of last available physical page */
159 vm_offset_t virtual2_start; /* cutout free area prior to kernel start */
160 vm_offset_t virtual2_end;
161 vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */
162 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
163 vm_offset_t KvaStart; /* VA start of KVA space */
164 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
165 vm_offset_t KvaSize; /* max size of kernel virtual address space */
166 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
167 static int pgeflag; /* PG_G or-in */
168 static int pseflag; /* PG_PS or-in */
171 static vm_paddr_t dmaplimit;
173 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
175 static uint64_t KPTbase;
176 static uint64_t KPTphys;
177 static uint64_t KPDphys; /* phys addr of kernel level 2 */
178 static uint64_t KPDbase; /* phys addr of kernel level 2 @ KERNBASE */
179 uint64_t KPDPphys; /* phys addr of kernel level 3 */
180 uint64_t KPML4phys; /* phys addr of kernel level 4 */
182 static uint64_t DMPDphys; /* phys addr of direct mapped level 2 */
183 static uint64_t DMPDPphys; /* phys addr of direct mapped level 3 */
186 * Data for the pv entry allocation mechanism
188 static vm_zone_t pvzone;
189 static struct vm_zone pvzone_store;
190 static struct vm_object pvzone_obj;
191 static int pv_entry_max=0, pv_entry_high_water=0;
192 static int pmap_pagedaemon_waken = 0;
193 static struct pv_entry *pvinit;
196 * All those kernel PT submaps that BSD is so fond of
198 pt_entry_t *CMAP1 = 0, *ptmmap;
199 caddr_t CADDR1 = 0, ptvmmap = 0;
200 static pt_entry_t *msgbufmap;
201 struct msgbuf *msgbufp=0;
206 static pt_entry_t *pt_crashdumpmap;
207 static caddr_t crashdumpmap;
209 static int pmap_yield_count = 64;
210 SYSCTL_INT(_machdep, OID_AUTO, pmap_yield_count, CTLFLAG_RW,
211 &pmap_yield_count, 0, "Yield during init_pt/release");
215 static void pv_hold(pv_entry_t pv);
216 static int _pv_hold_try(pv_entry_t pv
218 static void pv_drop(pv_entry_t pv);
219 static void _pv_lock(pv_entry_t pv
221 static void pv_unlock(pv_entry_t pv);
222 static pv_entry_t _pv_alloc(pmap_t pmap, vm_pindex_t pindex, int *isnew
224 static pv_entry_t _pv_get(pmap_t pmap, vm_pindex_t pindex
226 static pv_entry_t pv_get_try(pmap_t pmap, vm_pindex_t pindex, int *errorp);
227 static pv_entry_t pv_find(pmap_t pmap, vm_pindex_t pindex);
228 static void pv_put(pv_entry_t pv);
229 static void pv_free(pv_entry_t pv);
230 static void *pv_pte_lookup(pv_entry_t pv, vm_pindex_t pindex);
231 static pv_entry_t pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex,
233 static void pmap_remove_pv_pte(pv_entry_t pv, pv_entry_t pvp,
234 struct pmap_inval_info *info);
235 static vm_page_t pmap_remove_pv_page(pv_entry_t pv);
237 static void pmap_remove_callback(pmap_t pmap, struct pmap_inval_info *info,
238 pv_entry_t pte_pv, pv_entry_t pt_pv, vm_offset_t va,
239 pt_entry_t *ptep, void *arg __unused);
240 static void pmap_protect_callback(pmap_t pmap, struct pmap_inval_info *info,
241 pv_entry_t pte_pv, pv_entry_t pt_pv, vm_offset_t va,
242 pt_entry_t *ptep, void *arg __unused);
244 static void i386_protection_init (void);
245 static void create_pagetables(vm_paddr_t *firstaddr);
246 static void pmap_remove_all (vm_page_t m);
247 static boolean_t pmap_testbit (vm_page_t m, int bit);
249 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
250 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
252 static unsigned pdir4mb;
255 pv_entry_compare(pv_entry_t pv1, pv_entry_t pv2)
257 if (pv1->pv_pindex < pv2->pv_pindex)
259 if (pv1->pv_pindex > pv2->pv_pindex)
264 RB_GENERATE2(pv_entry_rb_tree, pv_entry, pv_entry,
265 pv_entry_compare, vm_pindex_t, pv_pindex);
268 * Move the kernel virtual free pointer to the next
269 * 2MB. This is used to help improve performance
270 * by using a large (2MB) page for much of the kernel
271 * (.text, .data, .bss)
275 pmap_kmem_choose(vm_offset_t addr)
277 vm_offset_t newaddr = addr;
279 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
286 * Super fast pmap_pte routine best used when scanning the pv lists.
287 * This eliminates many course-grained invltlb calls. Note that many of
288 * the pv list scans are across different pmaps and it is very wasteful
289 * to do an entire invltlb when checking a single mapping.
291 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
295 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
297 return pmap_pte(pmap, va);
301 * Returns the pindex of a page table entry (representing a terminal page).
302 * There are NUPTE_TOTAL page table entries possible (a huge number)
304 * x86-64 has a 48-bit address space, where bit 47 is sign-extended out.
305 * We want to properly translate negative KVAs.
309 pmap_pte_pindex(vm_offset_t va)
311 return ((va >> PAGE_SHIFT) & (NUPTE_TOTAL - 1));
315 * Returns the pindex of a page table.
319 pmap_pt_pindex(vm_offset_t va)
321 return (NUPTE_TOTAL + ((va >> PDRSHIFT) & (NUPT_TOTAL - 1)));
325 * Returns the pindex of a page directory.
329 pmap_pd_pindex(vm_offset_t va)
331 return (NUPTE_TOTAL + NUPT_TOTAL +
332 ((va >> PDPSHIFT) & (NUPD_TOTAL - 1)));
337 pmap_pdp_pindex(vm_offset_t va)
339 return (NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL +
340 ((va >> PML4SHIFT) & (NUPDP_TOTAL - 1)));
345 pmap_pml4_pindex(void)
347 return (NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL + NUPDP_TOTAL);
351 * Return various clipped indexes for a given VA
353 * Returns the index of a pte in a page table, representing a terminal
358 pmap_pte_index(vm_offset_t va)
360 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
364 * Returns the index of a pt in a page directory, representing a page
369 pmap_pt_index(vm_offset_t va)
371 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
375 * Returns the index of a pd in a page directory page, representing a page
380 pmap_pd_index(vm_offset_t va)
382 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
386 * Returns the index of a pdp in the pml4 table, representing a page
391 pmap_pdp_index(vm_offset_t va)
393 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
397 * Generic procedure to index a pte from a pt, pd, or pdp.
401 pv_pte_lookup(pv_entry_t pv, vm_pindex_t pindex)
405 pte = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pv->pv_m));
406 return(&pte[pindex]);
410 * Return pointer to PDP slot in the PML4
414 pmap_pdp(pmap_t pmap, vm_offset_t va)
416 return (&pmap->pm_pml4[pmap_pdp_index(va)]);
420 * Return pointer to PD slot in the PDP given a pointer to the PDP
424 pmap_pdp_to_pd(pml4_entry_t *pdp, vm_offset_t va)
428 pd = (pdp_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
429 return (&pd[pmap_pd_index(va)]);
433 * Return pointer to PD slot in the PDP
437 pmap_pd(pmap_t pmap, vm_offset_t va)
441 pdp = pmap_pdp(pmap, va);
442 if ((*pdp & PG_V) == 0)
444 return (pmap_pdp_to_pd(pdp, va));
448 * Return pointer to PT slot in the PD given a pointer to the PD
452 pmap_pd_to_pt(pdp_entry_t *pd, vm_offset_t va)
456 pt = (pd_entry_t *)PHYS_TO_DMAP(*pd & PG_FRAME);
457 return (&pt[pmap_pt_index(va)]);
461 * Return pointer to PT slot in the PD
465 pmap_pt(pmap_t pmap, vm_offset_t va)
469 pd = pmap_pd(pmap, va);
470 if (pd == NULL || (*pd & PG_V) == 0)
472 return (pmap_pd_to_pt(pd, va));
476 * Return pointer to PTE slot in the PT given a pointer to the PT
480 pmap_pt_to_pte(pd_entry_t *pt, vm_offset_t va)
484 pte = (pt_entry_t *)PHYS_TO_DMAP(*pt & PG_FRAME);
485 return (&pte[pmap_pte_index(va)]);
489 * Return pointer to PTE slot in the PT
493 pmap_pte(pmap_t pmap, vm_offset_t va)
497 pt = pmap_pt(pmap, va);
498 if (pt == NULL || (*pt & PG_V) == 0)
500 if ((*pt & PG_PS) != 0)
501 return ((pt_entry_t *)pt);
502 return (pmap_pt_to_pte(pt, va));
506 * Of all the layers (PTE, PT, PD, PDP, PML4) the best one to cache is
507 * the PT layer. This will speed up core pmap operations considerably.
511 pv_cache(pv_entry_t pv, vm_pindex_t pindex)
513 if (pindex >= pmap_pt_pindex(0) && pindex <= pmap_pd_pindex(0))
514 pv->pv_pmap->pm_pvhint = pv;
519 * KVM - return address of PT slot in PD
523 vtopt(vm_offset_t va)
525 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT +
526 NPML4EPGSHIFT)) - 1);
528 return (PDmap + ((va >> PDRSHIFT) & mask));
532 * KVM - return address of PTE slot in PT
536 vtopte(vm_offset_t va)
538 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT +
539 NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
541 return (PTmap + ((va >> PAGE_SHIFT) & mask));
545 allocpages(vm_paddr_t *firstaddr, long n)
550 bzero((void *)ret, n * PAGE_SIZE);
551 *firstaddr += n * PAGE_SIZE;
557 create_pagetables(vm_paddr_t *firstaddr)
559 long i; /* must be 64 bits */
565 * We are running (mostly) V=P at this point
567 * Calculate NKPT - number of kernel page tables. We have to
568 * accomodoate prealloction of the vm_page_array, dump bitmap,
569 * MSGBUF_SIZE, and other stuff. Be generous.
571 * Maxmem is in pages.
573 * ndmpdp is the number of 1GB pages we wish to map.
575 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
576 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
578 KKASSERT(ndmpdp <= NKPDPE * NPDEPG);
581 * Starting at the beginning of kvm (not KERNBASE).
583 nkpt_phys = (Maxmem * sizeof(struct vm_page) + NBPDR - 1) / NBPDR;
584 nkpt_phys += (Maxmem * sizeof(struct pv_entry) + NBPDR - 1) / NBPDR;
585 nkpt_phys += ((nkpt + nkpt + 1 + NKPML4E + NKPDPE + NDMPML4E +
586 ndmpdp) + 511) / 512;
590 * Starting at KERNBASE - map 2G worth of page table pages.
591 * KERNBASE is offset -2G from the end of kvm.
593 nkpt_base = (NPDPEPG - KPDPI) * NPTEPG; /* typically 2 x 512 */
598 KPTbase = allocpages(firstaddr, nkpt_base);
599 KPTphys = allocpages(firstaddr, nkpt_phys);
600 KPML4phys = allocpages(firstaddr, 1);
601 KPDPphys = allocpages(firstaddr, NKPML4E);
602 KPDphys = allocpages(firstaddr, NKPDPE);
605 * Calculate the page directory base for KERNBASE,
606 * that is where we start populating the page table pages.
607 * Basically this is the end - 2.
609 KPDbase = KPDphys + ((NKPDPE - (NPDPEPG - KPDPI)) << PAGE_SHIFT);
611 DMPDPphys = allocpages(firstaddr, NDMPML4E);
612 if ((amd_feature & AMDID_PAGE1GB) == 0)
613 DMPDphys = allocpages(firstaddr, ndmpdp);
614 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
617 * Fill in the underlying page table pages for the area around
618 * KERNBASE. This remaps low physical memory to KERNBASE.
620 * Read-only from zero to physfree
621 * XXX not fully used, underneath 2M pages
623 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
624 ((pt_entry_t *)KPTbase)[i] = i << PAGE_SHIFT;
625 ((pt_entry_t *)KPTbase)[i] |= PG_RW | PG_V | PG_G;
629 * Now map the initial kernel page tables. One block of page
630 * tables is placed at the beginning of kernel virtual memory,
631 * and another block is placed at KERNBASE to map the kernel binary,
632 * data, bss, and initial pre-allocations.
634 for (i = 0; i < nkpt_base; i++) {
635 ((pd_entry_t *)KPDbase)[i] = KPTbase + (i << PAGE_SHIFT);
636 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V;
638 for (i = 0; i < nkpt_phys; i++) {
639 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
640 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
644 * Map from zero to end of allocations using 2M pages as an
645 * optimization. This will bypass some of the KPTBase pages
646 * above in the KERNBASE area.
648 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
649 ((pd_entry_t *)KPDbase)[i] = i << PDRSHIFT;
650 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V | PG_PS | PG_G;
654 * And connect up the PD to the PDP. The kernel pmap is expected
655 * to pre-populate all of its PDs. See NKPDPE in vmparam.h.
657 for (i = 0; i < NKPDPE; i++) {
658 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] =
659 KPDphys + (i << PAGE_SHIFT);
660 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] |=
665 * Now set up the direct map space using either 2MB or 1GB pages
666 * Preset PG_M and PG_A because demotion expects it.
668 * When filling in entries in the PD pages make sure any excess
669 * entries are set to zero as we allocated enough PD pages
671 if ((amd_feature & AMDID_PAGE1GB) == 0) {
672 for (i = 0; i < NPDEPG * ndmpdp; i++) {
673 ((pd_entry_t *)DMPDphys)[i] = i << PDRSHIFT;
674 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
679 * And the direct map space's PDP
681 for (i = 0; i < ndmpdp; i++) {
682 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
684 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
687 for (i = 0; i < ndmpdp; i++) {
688 ((pdp_entry_t *)DMPDPphys)[i] =
689 (vm_paddr_t)i << PDPSHIFT;
690 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
695 /* And recursively map PML4 to itself in order to get PTmap */
696 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
697 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
700 * Connect the Direct Map slots up to the PML4
702 for (j = 0; j < NDMPML4E; ++j) {
703 ((pdp_entry_t *)KPML4phys)[DMPML4I + j] =
704 (DMPDPphys + ((vm_paddr_t)j << PML4SHIFT)) |
709 * Connect the KVA slot up to the PML4
711 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
712 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
716 * Bootstrap the system enough to run with virtual memory.
718 * On the i386 this is called after mapping has already been enabled
719 * and just syncs the pmap module with what has already been done.
720 * [We can't call it easily with mapping off since the kernel is not
721 * mapped with PA == VA, hence we would have to relocate every address
722 * from the linked base (virtual) address "KERNBASE" to the actual
723 * (physical) address starting relative to 0]
726 pmap_bootstrap(vm_paddr_t *firstaddr)
730 struct mdglobaldata *gd;
733 KvaStart = VM_MIN_KERNEL_ADDRESS;
734 KvaEnd = VM_MAX_KERNEL_ADDRESS;
735 KvaSize = KvaEnd - KvaStart;
737 avail_start = *firstaddr;
740 * Create an initial set of page tables to run the kernel in.
742 create_pagetables(firstaddr);
744 virtual2_start = KvaStart;
745 virtual2_end = PTOV_OFFSET;
747 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
748 virtual_start = pmap_kmem_choose(virtual_start);
750 virtual_end = VM_MAX_KERNEL_ADDRESS;
752 /* XXX do %cr0 as well */
753 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
757 * Initialize protection array.
759 i386_protection_init();
762 * The kernel's pmap is statically allocated so we don't have to use
763 * pmap_create, which is unlikely to work correctly at this part of
764 * the boot sequence (XXX and which no longer exists).
766 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
767 kernel_pmap.pm_count = 1;
768 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
769 RB_INIT(&kernel_pmap.pm_pvroot);
770 spin_init(&kernel_pmap.pm_spin);
771 lwkt_token_init(&kernel_pmap.pm_token, "kpmap_tok");
774 * Reserve some special page table entries/VA space for temporary
777 #define SYSMAP(c, p, v, n) \
778 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
784 * CMAP1/CMAP2 are used for zeroing and copying pages.
786 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
791 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
794 * ptvmmap is used for reading arbitrary physical pages via
797 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
800 * msgbufp is used to map the system message buffer.
801 * XXX msgbufmap is not used.
803 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
804 atop(round_page(MSGBUF_SIZE)))
811 * PG_G is terribly broken on SMP because we IPI invltlb's in some
812 * cases rather then invl1pg. Actually, I don't even know why it
813 * works under UP because self-referential page table mappings
818 if (cpu_feature & CPUID_PGE)
823 * Initialize the 4MB page size flag
827 * The 4MB page version of the initial
828 * kernel page mapping.
832 #if !defined(DISABLE_PSE)
833 if (cpu_feature & CPUID_PSE) {
836 * Note that we have enabled PSE mode
839 ptditmp = *(PTmap + x86_64_btop(KERNBASE));
840 ptditmp &= ~(NBPDR - 1);
841 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
846 * Enable the PSE mode. If we are SMP we can't do this
847 * now because the APs will not be able to use it when
850 load_cr4(rcr4() | CR4_PSE);
853 * We can do the mapping here for the single processor
854 * case. We simply ignore the old page table page from
858 * For SMP, we still need 4K pages to bootstrap APs,
859 * PSE will be enabled as soon as all APs are up.
861 PTD[KPTDI] = (pd_entry_t)ptditmp;
868 * We need to finish setting up the globaldata page for the BSP.
869 * locore has already populated the page table for the mdglobaldata
872 pg = MDGLOBALDATA_BASEALLOC_PAGES;
873 gd = &CPU_prvspace[0].mdglobaldata;
880 * Set 4mb pdir for mp startup
885 if (pseflag && (cpu_feature & CPUID_PSE)) {
886 load_cr4(rcr4() | CR4_PSE);
887 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
895 * Initialize the pmap module.
896 * Called by vm_init, to initialize any structures that the pmap
897 * system needs to map virtual memory.
898 * pmap_init has been enhanced to support in a fairly consistant
899 * way, discontiguous physical memory.
908 * Allocate memory for random pmap data structures. Includes the
912 for (i = 0; i < vm_page_array_size; i++) {
915 m = &vm_page_array[i];
916 TAILQ_INIT(&m->md.pv_list);
920 * init the pv free list
922 initial_pvs = vm_page_array_size;
923 if (initial_pvs < MINPV)
925 pvzone = &pvzone_store;
926 pvinit = (void *)kmem_alloc(&kernel_map,
927 initial_pvs * sizeof (struct pv_entry));
928 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry),
929 pvinit, initial_pvs);
932 * Now it is safe to enable pv_table recording.
934 pmap_initialized = TRUE;
938 * Initialize the address space (zone) for the pv_entries. Set a
939 * high water mark so that the system can recover from excessive
940 * numbers of pv entries.
945 int shpgperproc = PMAP_SHPGPERPROC;
948 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
949 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
950 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
951 pv_entry_high_water = 9 * (pv_entry_max / 10);
954 * Subtract out pages already installed in the zone (hack)
956 entry_max = pv_entry_max - vm_page_array_size;
960 zinitna(pvzone, &pvzone_obj, NULL, 0, entry_max, ZONE_INTERRUPT, 1);
964 /***************************************************
965 * Low level helper routines.....
966 ***************************************************/
968 #if defined(PMAP_DIAGNOSTIC)
971 * This code checks for non-writeable/modified pages.
972 * This should be an invalid condition.
976 pmap_nw_modified(pt_entry_t pte)
978 if ((pte & (PG_M|PG_RW)) == PG_M)
987 * this routine defines the region(s) of memory that should
988 * not be tested for the modified bit.
992 pmap_track_modified(vm_pindex_t pindex)
994 vm_offset_t va = (vm_offset_t)pindex << PAGE_SHIFT;
995 if ((va < clean_sva) || (va >= clean_eva))
1002 * Extract the physical page address associated with the map/VA pair.
1003 * The page must be wired for this to work reliably.
1005 * XXX for the moment we're using pv_find() instead of pv_get(), as
1006 * callers might be expecting non-blocking operation.
1009 pmap_extract(pmap_t pmap, vm_offset_t va)
1016 if (va >= VM_MAX_USER_ADDRESS) {
1018 * Kernel page directories might be direct-mapped and
1019 * there is typically no PV tracking of pte's
1023 pt = pmap_pt(pmap, va);
1024 if (pt && (*pt & PG_V)) {
1026 rtval = *pt & PG_PS_FRAME;
1027 rtval |= va & PDRMASK;
1029 ptep = pmap_pt_to_pte(pt, va);
1031 rtval = *ptep & PG_FRAME;
1032 rtval |= va & PAGE_MASK;
1038 * User pages currently do not direct-map the page directory
1039 * and some pages might not used managed PVs. But all PT's
1042 pt_pv = pv_find(pmap, pmap_pt_pindex(va));
1044 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
1046 rtval = *ptep & PG_FRAME;
1047 rtval |= va & PAGE_MASK;
1056 * Extract the physical page address associated kernel virtual address.
1059 pmap_kextract(vm_offset_t va)
1061 pd_entry_t pt; /* pt entry in pd */
1064 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
1065 pa = DMAP_TO_PHYS(va);
1069 pa = (pt & PG_PS_FRAME) | (va & PDRMASK);
1072 * Beware of a concurrent promotion that changes the
1073 * PDE at this point! For example, vtopte() must not
1074 * be used to access the PTE because it would use the
1075 * new PDE. It is, however, safe to use the old PDE
1076 * because the page table page is preserved by the
1079 pa = *pmap_pt_to_pte(&pt, va);
1080 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1086 /***************************************************
1087 * Low level mapping routines.....
1088 ***************************************************/
1091 * Routine: pmap_kenter
1093 * Add a wired page to the KVA
1094 * NOTE! note that in order for the mapping to take effect -- you
1095 * should do an invltlb after doing the pmap_kenter().
1098 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1102 pmap_inval_info info;
1104 pmap_inval_init(&info); /* XXX remove */
1105 npte = pa | PG_RW | PG_V | pgeflag;
1107 pmap_inval_interlock(&info, &kernel_pmap, va); /* XXX remove */
1109 pmap_inval_deinterlock(&info, &kernel_pmap); /* XXX remove */
1110 pmap_inval_done(&info); /* XXX remove */
1114 * Routine: pmap_kenter_quick
1116 * Similar to pmap_kenter(), except we only invalidate the
1117 * mapping on the current CPU.
1120 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
1125 npte = pa | PG_RW | PG_V | pgeflag;
1128 cpu_invlpg((void *)va);
1132 pmap_kenter_sync(vm_offset_t va)
1134 pmap_inval_info info;
1136 pmap_inval_init(&info);
1137 pmap_inval_interlock(&info, &kernel_pmap, va);
1138 pmap_inval_deinterlock(&info, &kernel_pmap);
1139 pmap_inval_done(&info);
1143 pmap_kenter_sync_quick(vm_offset_t va)
1145 cpu_invlpg((void *)va);
1149 * remove a page from the kernel pagetables
1152 pmap_kremove(vm_offset_t va)
1155 pmap_inval_info info;
1157 pmap_inval_init(&info);
1159 pmap_inval_interlock(&info, &kernel_pmap, va);
1160 (void)pte_load_clear(pte);
1161 pmap_inval_deinterlock(&info, &kernel_pmap);
1162 pmap_inval_done(&info);
1166 pmap_kremove_quick(vm_offset_t va)
1170 (void)pte_load_clear(pte);
1171 cpu_invlpg((void *)va);
1175 * XXX these need to be recoded. They are not used in any critical path.
1178 pmap_kmodify_rw(vm_offset_t va)
1180 atomic_set_long(vtopte(va), PG_RW);
1181 cpu_invlpg((void *)va);
1185 pmap_kmodify_nc(vm_offset_t va)
1187 atomic_set_long(vtopte(va), PG_N);
1188 cpu_invlpg((void *)va);
1192 * Used to map a range of physical addresses into kernel virtual
1193 * address space during the low level boot, typically to map the
1194 * dump bitmap, message buffer, and vm_page_array.
1196 * These mappings are typically made at some pointer after the end of the
1199 * We could return PHYS_TO_DMAP(start) here and not allocate any
1200 * via (*virtp), but then kmem from userland and kernel dumps won't
1201 * have access to the related pointers.
1204 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
1207 vm_offset_t va_start;
1209 /*return PHYS_TO_DMAP(start);*/
1214 while (start < end) {
1215 pmap_kenter_quick(va, start);
1225 * Add a list of wired pages to the kva
1226 * this routine is only used for temporary
1227 * kernel mappings that do not need to have
1228 * page modification or references recorded.
1229 * Note that old mappings are simply written
1230 * over. The page *must* be wired.
1233 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1237 end_va = va + count * PAGE_SIZE;
1239 while (va < end_va) {
1243 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1244 cpu_invlpg((void *)va);
1252 * This routine jerks page mappings from the
1253 * kernel -- it is meant only for temporary mappings.
1255 * MPSAFE, INTERRUPT SAFE (cluster callback)
1258 pmap_qremove(vm_offset_t va, int count)
1262 end_va = va + count * PAGE_SIZE;
1264 while (va < end_va) {
1268 (void)pte_load_clear(pte);
1269 cpu_invlpg((void *)va);
1276 * Create a new thread and optionally associate it with a (new) process.
1277 * NOTE! the new thread's cpu may not equal the current cpu.
1280 pmap_init_thread(thread_t td)
1282 /* enforce pcb placement & alignment */
1283 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1284 td->td_pcb = (struct pcb *)((intptr_t)td->td_pcb & ~(intptr_t)0xF);
1285 td->td_savefpu = &td->td_pcb->pcb_save;
1286 td->td_sp = (char *)td->td_pcb; /* no -16 */
1290 * This routine directly affects the fork perf for a process.
1293 pmap_init_proc(struct proc *p)
1298 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1299 * it, and IdlePTD, represents the template used to update all other pmaps.
1301 * On architectures where the kernel pmap is not integrated into the user
1302 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1303 * kernel_pmap should be used to directly access the kernel_pmap.
1306 pmap_pinit0(struct pmap *pmap)
1308 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1310 pmap->pm_active = 0;
1311 pmap->pm_pvhint = NULL;
1312 RB_INIT(&pmap->pm_pvroot);
1313 spin_init(&pmap->pm_spin);
1314 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1315 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1319 * Initialize a preallocated and zeroed pmap structure,
1320 * such as one in a vmspace structure.
1323 pmap_pinit(struct pmap *pmap)
1329 * Misc initialization
1332 pmap->pm_active = 0;
1333 pmap->pm_pvhint = NULL;
1334 if (pmap->pm_pmlpv == NULL) {
1335 RB_INIT(&pmap->pm_pvroot);
1336 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1337 spin_init(&pmap->pm_spin);
1338 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1342 * No need to allocate page table space yet but we do need a valid
1343 * page directory table.
1345 if (pmap->pm_pml4 == NULL) {
1347 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1351 * Allocate the page directory page, which wires it even though
1352 * it isn't being entered into some higher level page table (it
1353 * being the highest level). If one is already cached we don't
1354 * have to do anything.
1356 if ((pv = pmap->pm_pmlpv) == NULL) {
1357 pv = pmap_allocpte(pmap, pmap_pml4_pindex(), NULL);
1358 pmap->pm_pmlpv = pv;
1359 pmap_kenter((vm_offset_t)pmap->pm_pml4,
1360 VM_PAGE_TO_PHYS(pv->pv_m));
1364 * Install DMAP and KMAP.
1366 for (j = 0; j < NDMPML4E; ++j) {
1367 pmap->pm_pml4[DMPML4I + j] =
1368 (DMPDPphys + ((vm_paddr_t)j << PML4SHIFT)) |
1369 PG_RW | PG_V | PG_U;
1371 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1374 * install self-referential address mapping entry
1376 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(pv->pv_m) |
1377 PG_V | PG_RW | PG_A | PG_M;
1379 KKASSERT(pv->pv_m->flags & PG_MAPPED);
1380 KKASSERT(pv->pv_m->flags & PG_WRITEABLE);
1385 * Clean up a pmap structure so it can be physically freed. This routine
1386 * is called by the vmspace dtor function. A great deal of pmap data is
1387 * left passively mapped to improve vmspace management so we have a bit
1388 * of cleanup work to do here.
1391 pmap_puninit(pmap_t pmap)
1396 KKASSERT(pmap->pm_active == 0);
1397 if ((pv = pmap->pm_pmlpv) != NULL) {
1398 if (pv_hold_try(pv) == 0)
1400 p = pmap_remove_pv_page(pv);
1402 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1403 vm_page_busy_wait(p, FALSE, "pgpun");
1404 KKASSERT(p->flags & (PG_FICTITIOUS|PG_UNMANAGED));
1405 vm_page_unwire(p, 0);
1406 vm_page_flag_clear(p, PG_MAPPED | PG_WRITEABLE);
1409 * XXX eventually clean out PML4 static entries and
1410 * use vm_page_free_zero()
1413 pmap->pm_pmlpv = NULL;
1415 if (pmap->pm_pml4) {
1416 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1417 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1418 pmap->pm_pml4 = NULL;
1420 KKASSERT(pmap->pm_stats.resident_count == 0);
1421 KKASSERT(pmap->pm_stats.wired_count == 0);
1425 * Wire in kernel global address entries. To avoid a race condition
1426 * between pmap initialization and pmap_growkernel, this procedure
1427 * adds the pmap to the master list (which growkernel scans to update),
1428 * then copies the template.
1431 pmap_pinit2(struct pmap *pmap)
1434 * XXX copies current process, does not fill in MPPTDI
1436 spin_lock(&pmap_spin);
1437 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1438 spin_unlock(&pmap_spin);
1442 * This routine is called when various levels in the page table need to
1443 * be populated. This routine cannot fail.
1445 * This function returns two locked pv_entry's, one representing the
1446 * requested pv and one representing the requested pv's parent pv. If
1447 * the pv did not previously exist it will be mapped into its parent
1448 * and wired, otherwise no additional wire count will be added.
1452 pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, pv_entry_t *pvpp)
1457 vm_pindex_t pt_pindex;
1462 * If the pv already exists and we aren't being asked for the
1463 * parent page table page we can just return it. A locked+held pv
1466 pv = pv_alloc(pmap, ptepindex, &isnew);
1467 if (isnew == 0 && pvpp == NULL)
1471 * This is a new PV, we have to resolve its parent page table and
1472 * add an additional wiring to the page if necessary.
1476 * Special case terminal PVs. These are not page table pages so
1477 * no vm_page is allocated (the caller supplied the vm_page). If
1478 * pvpp is non-NULL we are being asked to also removed the pt_pv
1481 * Note that pt_pv's are only returned for user VAs. We assert that
1482 * a pt_pv is not being requested for kernel VAs.
1484 if (ptepindex < pmap_pt_pindex(0)) {
1485 if (ptepindex >= NUPTE_USER)
1486 KKASSERT(pvpp == NULL);
1488 KKASSERT(pvpp != NULL);
1490 pt_pindex = NUPTE_TOTAL + (ptepindex >> NPTEPGSHIFT);
1491 pvp = pmap_allocpte(pmap, pt_pindex, NULL);
1493 vm_page_wire_quick(pvp->pv_m);
1502 * Non-terminal PVs allocate a VM page to represent the page table,
1503 * so we have to resolve pvp and calculate ptepindex for the pvp
1504 * and then for the page table entry index in the pvp for
1507 if (ptepindex < pmap_pd_pindex(0)) {
1509 * pv is PT, pvp is PD
1511 ptepindex = (ptepindex - pmap_pt_pindex(0)) >> NPDEPGSHIFT;
1512 ptepindex += NUPTE_TOTAL + NUPT_TOTAL;
1513 pvp = pmap_allocpte(pmap, ptepindex, NULL);
1520 ptepindex = pv->pv_pindex - pmap_pt_pindex(0);
1521 ptepindex &= ((1ul << NPDEPGSHIFT) - 1);
1522 } else if (ptepindex < pmap_pdp_pindex(0)) {
1524 * pv is PD, pvp is PDP
1526 ptepindex = (ptepindex - pmap_pd_pindex(0)) >> NPDPEPGSHIFT;
1527 ptepindex += NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL;
1528 pvp = pmap_allocpte(pmap, ptepindex, NULL);
1535 ptepindex = pv->pv_pindex - pmap_pd_pindex(0);
1536 ptepindex &= ((1ul << NPDPEPGSHIFT) - 1);
1537 } else if (ptepindex < pmap_pml4_pindex()) {
1539 * pv is PDP, pvp is the root pml4 table
1541 pvp = pmap_allocpte(pmap, pmap_pml4_pindex(), NULL);
1548 ptepindex = pv->pv_pindex - pmap_pdp_pindex(0);
1549 ptepindex &= ((1ul << NPML4EPGSHIFT) - 1);
1552 * pv represents the top-level PML4, there is no parent.
1560 * This code is only reached if isnew is TRUE and this is not a
1561 * terminal PV. We need to allocate a vm_page for the page table
1562 * at this level and enter it into the parent page table.
1564 * page table pages are marked PG_WRITEABLE and PG_MAPPED.
1567 m = vm_page_alloc(NULL, pv->pv_pindex,
1568 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM |
1569 VM_ALLOC_INTERRUPT);
1574 vm_page_spin_lock(m);
1575 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1577 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1578 vm_page_spin_unlock(m);
1579 vm_page_unmanage(m); /* m must be spinunlocked */
1581 if ((m->flags & PG_ZERO) == 0) {
1582 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1586 pmap_page_assertzero(VM_PAGE_TO_PHYS(m));
1589 m->valid = VM_PAGE_BITS_ALL;
1590 vm_page_flag_clear(m, PG_ZERO);
1591 vm_page_wire(m); /* wire for mapping in parent */
1594 * Wire the page into pvp, bump the wire-count for pvp's page table
1595 * page. Bump the resident_count for the pmap. There is no pvp
1596 * for the top level, address the pm_pml4[] array directly.
1598 * If the caller wants the parent we return it, otherwise
1599 * we just put it away.
1601 * No interlock is needed for pte 0 -> non-zero.
1604 vm_page_wire_quick(pvp->pv_m);
1605 ptep = pv_pte_lookup(pvp, ptepindex);
1606 KKASSERT((*ptep & PG_V) == 0);
1607 *ptep = VM_PAGE_TO_PHYS(m) | (PG_U | PG_RW | PG_V |
1620 * Release any resources held by the given physical map.
1622 * Called when a pmap initialized by pmap_pinit is being released. Should
1623 * only be called if the map contains no valid mappings.
1625 * Caller must hold pmap->pm_token
1627 struct pmap_release_info {
1632 static int pmap_release_callback(pv_entry_t pv, void *data);
1635 pmap_release(struct pmap *pmap)
1637 struct pmap_release_info info;
1639 KASSERT(pmap->pm_active == 0,
1640 ("pmap still active! %016jx", (uintmax_t)pmap->pm_active));
1642 spin_lock(&pmap_spin);
1643 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1644 spin_unlock(&pmap_spin);
1647 * Pull pv's off the RB tree in order from low to high and release
1653 spin_lock(&pmap->pm_spin);
1654 RB_SCAN(pv_entry_rb_tree, &pmap->pm_pvroot, NULL,
1655 pmap_release_callback, &info);
1656 spin_unlock(&pmap->pm_spin);
1657 } while (info.retry);
1661 * One resident page (the pml4 page) should remain.
1662 * No wired pages should remain.
1664 KKASSERT(pmap->pm_stats.resident_count == 1);
1665 KKASSERT(pmap->pm_stats.wired_count == 0);
1669 pmap_release_callback(pv_entry_t pv, void *data)
1671 struct pmap_release_info *info = data;
1672 pmap_t pmap = info->pmap;
1675 if (pv_hold_try(pv)) {
1676 spin_unlock(&pmap->pm_spin);
1678 spin_unlock(&pmap->pm_spin);
1680 if (pv->pv_pmap != pmap) {
1682 spin_lock(&pmap->pm_spin);
1689 * The pmap is currently not spinlocked, pv is held+locked.
1690 * Remove the pv's page from its parent's page table. The
1691 * parent's page table page's wire_count will be decremented.
1693 pmap_remove_pv_pte(pv, NULL, NULL);
1696 * Terminal pvs are unhooked from their vm_pages. Because
1697 * terminal pages aren't page table pages they aren't wired
1698 * by us, so we have to be sure not to unwire them either.
1700 if (pv->pv_pindex < pmap_pt_pindex(0)) {
1701 pmap_remove_pv_page(pv);
1706 * We leave the top-level page table page cached, wired, and
1707 * mapped in the pmap until the dtor function (pmap_puninit())
1710 * Since we are leaving the top-level pv intact we need
1711 * to break out of what would otherwise be an infinite loop.
1713 if (pv->pv_pindex == pmap_pml4_pindex()) {
1715 spin_lock(&pmap->pm_spin);
1720 * For page table pages (other than the top-level page),
1721 * remove and free the vm_page. The representitive mapping
1722 * removed above by pmap_remove_pv_pte() did not undo the
1723 * last wire_count so we have to do that as well.
1725 p = pmap_remove_pv_page(pv);
1726 vm_page_busy_wait(p, FALSE, "pmaprl");
1727 if (p->wire_count != 1) {
1728 kprintf("p->wire_count was %016lx %d\n",
1729 pv->pv_pindex, p->wire_count);
1731 KKASSERT(p->wire_count == 1);
1732 KKASSERT(p->flags & PG_UNMANAGED);
1734 vm_page_unwire(p, 0);
1735 KKASSERT(p->wire_count == 0);
1736 /* JG eventually revert to using vm_page_free_zero() */
1740 spin_lock(&pmap->pm_spin);
1745 * This function will remove the pte associated with a pv from its parent.
1746 * Terminal pv's are supported. The removal will be interlocked if info
1747 * is non-NULL. The caller must dispose of pv instead of just unlocking
1750 * The wire count will be dropped on the parent page table. The wire
1751 * count on the page being removed (pv->pv_m) from the parent page table
1752 * is NOT touched. Note that terminal pages will not have any additional
1753 * wire counts while page table pages will have at least one representing
1754 * the mapping, plus others representing sub-mappings.
1756 * NOTE: Cannot be called on kernel page table pages, only KVM terminal
1757 * pages and user page table and terminal pages.
1759 * The pv must be locked.
1761 * XXX must lock parent pv's if they exist to remove pte XXX
1765 pmap_remove_pv_pte(pv_entry_t pv, pv_entry_t pvp, struct pmap_inval_info *info)
1767 vm_pindex_t ptepindex = pv->pv_pindex;
1768 pmap_t pmap = pv->pv_pmap;
1774 if (ptepindex == pmap_pml4_pindex()) {
1776 * We are the top level pml4 table, there is no parent.
1778 p = pmap->pm_pmlpv->pv_m;
1779 } else if (ptepindex >= pmap_pdp_pindex(0)) {
1781 * Remove a PDP page from the pml4e. This can only occur
1782 * with user page tables. We do not have to lock the
1783 * pml4 PV so just ignore pvp.
1785 vm_pindex_t pml4_pindex;
1786 vm_pindex_t pdp_index;
1789 pdp_index = ptepindex - pmap_pdp_pindex(0);
1791 pml4_pindex = pmap_pml4_pindex();
1792 pvp = pv_get(pv->pv_pmap, pml4_pindex);
1795 pdp = &pmap->pm_pml4[pdp_index & ((1ul << NPML4EPGSHIFT) - 1)];
1796 KKASSERT((*pdp & PG_V) != 0);
1797 p = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1799 KKASSERT(info == NULL);
1800 } else if (ptepindex >= pmap_pd_pindex(0)) {
1802 * Remove a PD page from the pdp
1804 vm_pindex_t pdp_pindex;
1805 vm_pindex_t pd_index;
1808 pd_index = ptepindex - pmap_pd_pindex(0);
1811 pdp_pindex = NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL +
1812 (pd_index >> NPML4EPGSHIFT);
1813 pvp = pv_get(pv->pv_pmap, pdp_pindex);
1816 pd = pv_pte_lookup(pvp, pd_index & ((1ul << NPDPEPGSHIFT) - 1));
1817 KKASSERT((*pd & PG_V) != 0);
1818 p = PHYS_TO_VM_PAGE(*pd & PG_FRAME);
1820 KKASSERT(info == NULL);
1821 } else if (ptepindex >= pmap_pt_pindex(0)) {
1823 * Remove a PT page from the pd
1825 vm_pindex_t pd_pindex;
1826 vm_pindex_t pt_index;
1829 pt_index = ptepindex - pmap_pt_pindex(0);
1832 pd_pindex = NUPTE_TOTAL + NUPT_TOTAL +
1833 (pt_index >> NPDPEPGSHIFT);
1834 pvp = pv_get(pv->pv_pmap, pd_pindex);
1837 pt = pv_pte_lookup(pvp, pt_index & ((1ul << NPDPEPGSHIFT) - 1));
1838 KKASSERT((*pt & PG_V) != 0);
1839 p = PHYS_TO_VM_PAGE(*pt & PG_FRAME);
1841 KKASSERT(info == NULL);
1844 * Remove a PTE from the PT page
1846 * NOTE: pv's must be locked bottom-up to avoid deadlocking.
1847 * pv is a pte_pv so we can safely lock pt_pv.
1849 vm_pindex_t pt_pindex;
1854 pt_pindex = ptepindex >> NPTEPGSHIFT;
1855 va = (vm_offset_t)ptepindex << PAGE_SHIFT;
1857 if (ptepindex >= NUPTE_USER) {
1858 ptep = vtopte(ptepindex << PAGE_SHIFT);
1859 KKASSERT(pvp == NULL);
1862 pt_pindex = NUPTE_TOTAL +
1863 (ptepindex >> NPDPEPGSHIFT);
1864 pvp = pv_get(pv->pv_pmap, pt_pindex);
1867 ptep = pv_pte_lookup(pvp, ptepindex &
1868 ((1ul << NPDPEPGSHIFT) - 1));
1872 pmap_inval_interlock(info, pmap, va);
1873 pte = pte_load_clear(ptep);
1875 pmap_inval_deinterlock(info, pmap);
1877 cpu_invlpg((void *)va);
1880 * Now update the vm_page_t
1882 if ((pte & (PG_MANAGED|PG_V)) != (PG_MANAGED|PG_V)) {
1883 kprintf("remove_pte badpte %016lx %016lx %d\n",
1885 pv->pv_pindex < pmap_pt_pindex(0));
1887 /*KKASSERT((pte & (PG_MANAGED|PG_V)) == (PG_MANAGED|PG_V));*/
1888 p = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1891 if (pmap_track_modified(ptepindex))
1895 vm_page_flag_set(p, PG_REFERENCED);
1898 atomic_add_long(&pmap->pm_stats.wired_count, -1);
1900 cpu_invlpg((void *)va);
1904 * Unwire the parent page table page. The wire_count cannot go below
1905 * 1 here because the parent page table page is itself still mapped.
1907 * XXX remove the assertions later.
1909 KKASSERT(pv->pv_m == p);
1910 if (pvp && vm_page_unwire_quick(pvp->pv_m))
1911 panic("pmap_remove_pv_pte: Insufficient wire_count");
1919 pmap_remove_pv_page(pv_entry_t pv)
1925 vm_page_spin_lock(m);
1927 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1930 atomic_add_int(&m->object->agg_pv_list_count, -1);
1932 if (TAILQ_EMPTY(&m->md.pv_list))
1933 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1934 vm_page_spin_unlock(m);
1939 * Grow the number of kernel page table entries, if needed.
1941 * This routine is always called to validate any address space
1942 * beyond KERNBASE (for kldloads). kernel_vm_end only governs the address
1943 * space below KERNBASE.
1946 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
1949 vm_offset_t ptppaddr;
1951 pd_entry_t *pt, newpt;
1953 int update_kernel_vm_end;
1956 * bootstrap kernel_vm_end on first real VM use
1958 if (kernel_vm_end == 0) {
1959 kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
1961 while ((*pmap_pt(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1962 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1963 ~(PAGE_SIZE * NPTEPG - 1);
1965 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1966 kernel_vm_end = kernel_map.max_offset;
1973 * Fill in the gaps. kernel_vm_end is only adjusted for ranges
1974 * below KERNBASE. Ranges above KERNBASE are kldloaded and we
1975 * do not want to force-fill 128G worth of page tables.
1977 if (kstart < KERNBASE) {
1978 if (kstart > kernel_vm_end)
1979 kstart = kernel_vm_end;
1980 KKASSERT(kend <= KERNBASE);
1981 update_kernel_vm_end = 1;
1983 update_kernel_vm_end = 0;
1986 kstart = rounddown2(kstart, PAGE_SIZE * NPTEPG);
1987 kend = roundup2(kend, PAGE_SIZE * NPTEPG);
1989 if (kend - 1 >= kernel_map.max_offset)
1990 kend = kernel_map.max_offset;
1992 while (kstart < kend) {
1993 pt = pmap_pt(&kernel_pmap, kstart);
1995 /* We need a new PDP entry */
1996 nkpg = vm_page_alloc(NULL, nkpt,
1999 VM_ALLOC_INTERRUPT);
2001 panic("pmap_growkernel: no memory to grow "
2004 paddr = VM_PAGE_TO_PHYS(nkpg);
2005 if ((nkpg->flags & PG_ZERO) == 0)
2006 pmap_zero_page(paddr);
2007 vm_page_flag_clear(nkpg, PG_ZERO);
2008 newpd = (pdp_entry_t)
2009 (paddr | PG_V | PG_RW | PG_A | PG_M);
2010 *pmap_pd(&kernel_pmap, kstart) = newpd;
2012 continue; /* try again */
2014 if ((*pt & PG_V) != 0) {
2015 kstart = (kstart + PAGE_SIZE * NPTEPG) &
2016 ~(PAGE_SIZE * NPTEPG - 1);
2017 if (kstart - 1 >= kernel_map.max_offset) {
2018 kstart = kernel_map.max_offset;
2025 * This index is bogus, but out of the way
2027 nkpg = vm_page_alloc(NULL, nkpt,
2030 VM_ALLOC_INTERRUPT);
2032 panic("pmap_growkernel: no memory to grow kernel");
2035 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2036 pmap_zero_page(ptppaddr);
2037 vm_page_flag_clear(nkpg, PG_ZERO);
2038 newpt = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2039 *pmap_pt(&kernel_pmap, kstart) = newpt;
2042 kstart = (kstart + PAGE_SIZE * NPTEPG) &
2043 ~(PAGE_SIZE * NPTEPG - 1);
2045 if (kstart - 1 >= kernel_map.max_offset) {
2046 kstart = kernel_map.max_offset;
2052 * Only update kernel_vm_end for areas below KERNBASE.
2054 if (update_kernel_vm_end && kernel_vm_end < kstart)
2055 kernel_vm_end = kstart;
2059 * Retire the given physical map from service.
2060 * Should only be called if the map contains
2061 * no valid mappings.
2064 pmap_destroy(pmap_t pmap)
2071 lwkt_gettoken(&pmap->pm_token);
2072 count = --pmap->pm_count;
2074 pmap_release(pmap); /* eats pm_token */
2075 panic("destroying a pmap is not yet implemented");
2077 lwkt_reltoken(&pmap->pm_token);
2081 * Add a reference to the specified pmap.
2084 pmap_reference(pmap_t pmap)
2087 lwkt_gettoken(&pmap->pm_token);
2089 lwkt_reltoken(&pmap->pm_token);
2093 /***************************************************
2094 * page management routines.
2095 ***************************************************/
2098 * Hold a pv without locking it
2101 pv_hold(pv_entry_t pv)
2105 if (atomic_cmpset_int(&pv->pv_hold, 0, 1))
2109 count = pv->pv_hold;
2111 if (atomic_cmpset_int(&pv->pv_hold, count, count + 1))
2118 * Hold a pv_entry, preventing its destruction. TRUE is returned if the pv
2119 * was successfully locked, FALSE if it wasn't. The caller must dispose of
2122 * Either the pmap->pm_spin or the related vm_page_spin (if traversing a
2123 * pv list via its page) must be held by the caller.
2126 _pv_hold_try(pv_entry_t pv PMAP_DEBUG_DECL)
2130 if (atomic_cmpset_int(&pv->pv_hold, 0, PV_HOLD_LOCKED | 1)) {
2133 pv->pv_line = lineno;
2139 count = pv->pv_hold;
2141 if ((count & PV_HOLD_LOCKED) == 0) {
2142 if (atomic_cmpset_int(&pv->pv_hold, count,
2143 (count + 1) | PV_HOLD_LOCKED)) {
2146 pv->pv_line = lineno;
2151 if (atomic_cmpset_int(&pv->pv_hold, count, count + 1))
2159 * Drop a previously held pv_entry which could not be locked, allowing its
2162 * Must not be called with a spinlock held as we might zfree() the pv if it
2163 * is no longer associated with a pmap and this was the last hold count.
2166 pv_drop(pv_entry_t pv)
2170 if (atomic_cmpset_int(&pv->pv_hold, 1, 0)) {
2171 if (pv->pv_pmap == NULL)
2177 count = pv->pv_hold;
2179 KKASSERT((count & PV_HOLD_MASK) > 0);
2180 KKASSERT((count & (PV_HOLD_LOCKED | PV_HOLD_MASK)) !=
2181 (PV_HOLD_LOCKED | 1));
2182 if (atomic_cmpset_int(&pv->pv_hold, count, count - 1)) {
2183 if (count == 1 && pv->pv_pmap == NULL)
2192 * Find or allocate the requested PV entry, returning a locked pv
2196 _pv_alloc(pmap_t pmap, vm_pindex_t pindex, int *isnew PMAP_DEBUG_DECL)
2199 pv_entry_t pnew = NULL;
2201 spin_lock(&pmap->pm_spin);
2203 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex) {
2204 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot,
2209 spin_unlock(&pmap->pm_spin);
2210 pnew = zalloc(pvzone);
2211 spin_lock(&pmap->pm_spin);
2214 pnew->pv_pmap = pmap;
2215 pnew->pv_pindex = pindex;
2216 pnew->pv_hold = PV_HOLD_LOCKED | 1;
2218 pnew->pv_func = func;
2219 pnew->pv_line = lineno;
2221 pv_entry_rb_tree_RB_INSERT(&pmap->pm_pvroot, pnew);
2222 atomic_add_long(&pmap->pm_stats.resident_count, 1);
2223 spin_unlock(&pmap->pm_spin);
2228 spin_unlock(&pmap->pm_spin);
2229 zfree(pvzone, pnew);
2231 spin_lock(&pmap->pm_spin);
2234 if (_pv_hold_try(pv PMAP_DEBUG_COPY)) {
2235 spin_unlock(&pmap->pm_spin);
2239 spin_unlock(&pmap->pm_spin);
2240 _pv_lock(pv PMAP_DEBUG_COPY);
2241 if (pv->pv_pmap == pmap && pv->pv_pindex == pindex) {
2246 spin_lock(&pmap->pm_spin);
2253 * Find the requested PV entry, returning a locked+held pv or NULL
2257 _pv_get(pmap_t pmap, vm_pindex_t pindex PMAP_DEBUG_DECL)
2261 spin_lock(&pmap->pm_spin);
2266 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex) {
2267 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot,
2271 spin_unlock(&pmap->pm_spin);
2274 if (_pv_hold_try(pv PMAP_DEBUG_COPY)) {
2275 pv_cache(pv, pindex);
2276 spin_unlock(&pmap->pm_spin);
2279 spin_unlock(&pmap->pm_spin);
2280 _pv_lock(pv PMAP_DEBUG_COPY);
2281 if (pv->pv_pmap == pmap && pv->pv_pindex == pindex)
2284 spin_lock(&pmap->pm_spin);
2289 * Lookup, hold, and attempt to lock (pmap,pindex).
2291 * If the entry does not exist NULL is returned and *errorp is set to 0
2293 * If the entry exists and could be successfully locked it is returned and
2294 * errorp is set to 0.
2296 * If the entry exists but could NOT be successfully locked it is returned
2297 * held and *errorp is set to 1.
2301 pv_get_try(pmap_t pmap, vm_pindex_t pindex, int *errorp)
2305 spin_lock(&pmap->pm_spin);
2306 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex)
2307 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pindex);
2309 spin_unlock(&pmap->pm_spin);
2313 if (pv_hold_try(pv)) {
2314 pv_cache(pv, pindex);
2315 spin_unlock(&pmap->pm_spin);
2317 return(pv); /* lock succeeded */
2319 spin_unlock(&pmap->pm_spin);
2321 return (pv); /* lock failed */
2325 * Find the requested PV entry, returning a held pv or NULL
2329 pv_find(pmap_t pmap, vm_pindex_t pindex)
2333 spin_lock(&pmap->pm_spin);
2335 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex)
2336 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pindex);
2338 spin_unlock(&pmap->pm_spin);
2342 pv_cache(pv, pindex);
2343 spin_unlock(&pmap->pm_spin);
2348 * Lock a held pv, keeping the hold count
2352 _pv_lock(pv_entry_t pv PMAP_DEBUG_DECL)
2357 count = pv->pv_hold;
2359 if ((count & PV_HOLD_LOCKED) == 0) {
2360 if (atomic_cmpset_int(&pv->pv_hold, count,
2361 count | PV_HOLD_LOCKED)) {
2364 pv->pv_line = lineno;
2370 tsleep_interlock(pv, 0);
2371 if (atomic_cmpset_int(&pv->pv_hold, count,
2372 count | PV_HOLD_WAITING)) {
2374 kprintf("pv waiting on %s:%d\n",
2375 pv->pv_func, pv->pv_line);
2377 tsleep(pv, PINTERLOCKED, "pvwait", hz);
2384 * Unlock a held and locked pv, keeping the hold count.
2388 pv_unlock(pv_entry_t pv)
2392 if (atomic_cmpset_int(&pv->pv_hold, PV_HOLD_LOCKED | 1, 1))
2396 count = pv->pv_hold;
2398 KKASSERT((count & (PV_HOLD_LOCKED|PV_HOLD_MASK)) >=
2399 (PV_HOLD_LOCKED | 1));
2400 if (atomic_cmpset_int(&pv->pv_hold, count,
2402 ~(PV_HOLD_LOCKED | PV_HOLD_WAITING))) {
2403 if (count & PV_HOLD_WAITING)
2411 * Unlock and drop a pv. If the pv is no longer associated with a pmap
2412 * and the hold count drops to zero we will free it.
2414 * Caller should not hold any spin locks. We are protected from hold races
2415 * by virtue of holds only occuring only with a pmap_spin or vm_page_spin
2416 * lock held. A pv cannot be located otherwise.
2420 pv_put(pv_entry_t pv)
2422 if (atomic_cmpset_int(&pv->pv_hold, PV_HOLD_LOCKED | 1, 0)) {
2423 if (pv->pv_pmap == NULL)
2432 * Unlock, drop, and free a pv, destroying it. The pv is removed from its
2433 * pmap. Any pte operations must have already been completed.
2437 pv_free(pv_entry_t pv)
2441 KKASSERT(pv->pv_m == NULL);
2442 if ((pmap = pv->pv_pmap) != NULL) {
2443 spin_lock(&pmap->pm_spin);
2444 pv_entry_rb_tree_RB_REMOVE(&pmap->pm_pvroot, pv);
2445 if (pmap->pm_pvhint == pv)
2446 pmap->pm_pvhint = NULL;
2447 atomic_add_long(&pmap->pm_stats.resident_count, -1);
2450 spin_unlock(&pmap->pm_spin);
2456 * This routine is very drastic, but can save the system
2464 static int warningdone=0;
2466 if (pmap_pagedaemon_waken == 0)
2468 pmap_pagedaemon_waken = 0;
2469 if (warningdone < 5) {
2470 kprintf("pmap_collect: collecting pv entries -- "
2471 "suggest increasing PMAP_SHPGPERPROC\n");
2475 for (i = 0; i < vm_page_array_size; i++) {
2476 m = &vm_page_array[i];
2477 if (m->wire_count || m->hold_count)
2479 if (vm_page_busy_try(m, TRUE) == 0) {
2480 if (m->wire_count == 0 && m->hold_count == 0) {
2489 * Scan the pmap for active page table entries and issue a callback.
2490 * The callback must dispose of pte_pv.
2492 * NOTE: Unmanaged page table entries will not have a pte_pv
2494 * NOTE: Kernel page table entries will not have a pt_pv. That is, wiring
2495 * counts are not tracked in kernel page table pages.
2497 * It is assumed that the start and end are properly rounded to the page size.
2500 pmap_scan(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva,
2501 void (*func)(pmap_t, struct pmap_inval_info *,
2502 pv_entry_t, pv_entry_t, vm_offset_t,
2503 pt_entry_t *, void *),
2506 pv_entry_t pdp_pv; /* A page directory page PV */
2507 pv_entry_t pd_pv; /* A page directory PV */
2508 pv_entry_t pt_pv; /* A page table PV */
2509 pv_entry_t pte_pv; /* A page table entry PV */
2511 vm_offset_t va_next;
2512 struct pmap_inval_info info;
2519 * Hold the token for stability; if the pmap is empty we have nothing
2522 lwkt_gettoken(&pmap->pm_token);
2524 if (pmap->pm_stats.resident_count == 0) {
2525 lwkt_reltoken(&pmap->pm_token);
2530 pmap_inval_init(&info);
2533 * Special handling for removing one page, which is a very common
2534 * operation (it is?).
2535 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4
2537 if (sva + PAGE_SIZE == eva) {
2538 if (sva >= VM_MAX_USER_ADDRESS) {
2540 * Kernel mappings do not track wire counts on
2544 pte_pv = pv_get(pmap, pmap_pte_pindex(sva));
2548 * User mappings may or may not have a pte_pv but
2549 * will always have a pt_pv if the page is present.
2551 pte_pv = pv_get(pmap, pmap_pte_pindex(sva));
2552 pt_pv = pv_get(pmap, pmap_pt_pindex(sva));
2553 if (pt_pv == NULL) {
2554 KKASSERT(pte_pv == NULL);
2557 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(sva));
2561 * Unlike the pv_find() case below we actually
2562 * acquired a locked pv in this case so any
2563 * race should have been resolved. It is expected
2566 KKASSERT(pte_pv == NULL);
2567 } else if (pte_pv) {
2568 KASSERT((*ptep & (PG_MANAGED|PG_V)) == (PG_MANAGED|
2570 ("bad *ptep %016lx sva %016lx pte_pv %p",
2571 *ptep, sva, pte_pv));
2572 func(pmap, &info, pte_pv, pt_pv, sva, ptep, arg);
2574 KASSERT((*ptep & (PG_MANAGED|PG_V)) == PG_V,
2575 ("bad *ptep %016lx sva %016lx pte_pv NULL",
2577 func(pmap, &info, pte_pv, pt_pv, sva, ptep, arg);
2582 pmap_inval_done(&info);
2583 lwkt_reltoken(&pmap->pm_token);
2588 * NOTE: kernel mappings do not track page table pages, only
2591 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4.
2592 * However, for the scan to be efficient we try to
2593 * cache items top-down.
2599 for (; sva < eva; sva = va_next) {
2601 if (sva >= VM_MAX_USER_ADDRESS) {
2612 if (pdp_pv == NULL) {
2613 pdp_pv = pv_get(pmap, pmap_pdp_pindex(sva));
2614 } else if (pdp_pv->pv_pindex != pmap_pdp_pindex(sva)) {
2616 pdp_pv = pv_get(pmap, pmap_pdp_pindex(sva));
2618 if (pdp_pv == NULL) {
2619 va_next = (sva + NBPML4) & ~PML4MASK;
2628 if (pd_pv == NULL) {
2633 pd_pv = pv_get(pmap, pmap_pd_pindex(sva));
2634 } else if (pd_pv->pv_pindex != pmap_pd_pindex(sva)) {
2640 pd_pv = pv_get(pmap, pmap_pd_pindex(sva));
2642 if (pd_pv == NULL) {
2643 va_next = (sva + NBPDP) & ~PDPMASK;
2652 if (pt_pv == NULL) {
2661 pt_pv = pv_get(pmap, pmap_pt_pindex(sva));
2662 } else if (pt_pv->pv_pindex != pmap_pt_pindex(sva)) {
2672 pt_pv = pv_get(pmap, pmap_pt_pindex(sva));
2676 * We will scan or skip a page table page so adjust va_next
2679 if (pt_pv == NULL) {
2680 va_next = (sva + NBPDR) & ~PDRMASK;
2687 * From this point in the loop testing pt_pv for non-NULL
2688 * means we are in UVM, else if it is NULL we are in KVM.
2691 va_next = (sva + NBPDR) & ~PDRMASK;
2696 * Limit our scan to either the end of the va represented
2697 * by the current page table page, or to the end of the
2698 * range being removed.
2700 * Scan the page table for pages. Some pages may not be
2701 * managed (might not have a pv_entry).
2703 * There is no page table management for kernel pages so
2704 * pt_pv will be NULL in that case, but otherwise pt_pv
2705 * is non-NULL, locked, and referenced.
2711 * At this point a non-NULL pt_pv means a UVA, and a NULL
2712 * pt_pv means a KVA.
2715 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(sva));
2719 while (sva < va_next) {
2721 * Acquire the related pte_pv, if any. If *ptep == 0
2722 * the related pte_pv should not exist, but if *ptep
2723 * is not zero the pte_pv may or may not exist (e.g.
2724 * will not exist for an unmanaged page).
2726 * However a multitude of races are possible here.
2728 * In addition, the (pt_pv, pte_pv) lock order is
2729 * backwards, so we have to be careful in aquiring
2730 * a properly locked pte_pv.
2734 pte_pv = pv_get_try(pmap, pmap_pte_pindex(sva),
2745 pv_put(pt_pv); /* must be non-NULL */
2747 pv_lock(pte_pv); /* safe to block now */
2750 pt_pv = pv_get(pmap,
2751 pmap_pt_pindex(sva));
2755 pte_pv = pv_get(pmap, pmap_pte_pindex(sva));
2759 * Ok, if *ptep == 0 we had better NOT have a pte_pv.
2763 kprintf("Unexpected non-NULL pte_pv "
2764 "%p pt_pv %p *ptep = %016lx\n",
2765 pte_pv, pt_pv, *ptep);
2766 panic("Unexpected non-NULL pte_pv");
2774 * Ready for the callback. The locked pte_pv (if any)
2775 * is consumed by the callback. pte_pv will exist if
2776 * the page is managed, and will not exist if it
2780 KASSERT((*ptep & (PG_MANAGED|PG_V)) ==
2782 ("bad *ptep %016lx sva %016lx "
2784 *ptep, sva, pte_pv));
2785 func(pmap, &info, pte_pv, pt_pv, sva,
2788 KASSERT((*ptep & (PG_MANAGED|PG_V)) ==
2790 ("bad *ptep %016lx sva %016lx "
2793 func(pmap, &info, pte_pv, pt_pv, sva,
2813 pmap_inval_done(&info);
2814 lwkt_reltoken(&pmap->pm_token);
2818 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2820 pmap_scan(pmap, sva, eva, pmap_remove_callback, NULL);
2824 pmap_remove_callback(pmap_t pmap, struct pmap_inval_info *info,
2825 pv_entry_t pte_pv, pv_entry_t pt_pv, vm_offset_t va,
2826 pt_entry_t *ptep, void *arg __unused)
2832 * This will also drop pt_pv's wire_count. Note that
2833 * terminal pages are not wired based on mmu presence.
2835 pmap_remove_pv_pte(pte_pv, pt_pv, info);
2836 pmap_remove_pv_page(pte_pv);
2840 * pt_pv's wire_count is still bumped by unmanaged pages
2841 * so we must decrement it manually.
2843 pmap_inval_interlock(info, pmap, va);
2844 pte = pte_load_clear(ptep);
2845 pmap_inval_deinterlock(info, pmap);
2847 atomic_add_long(&pmap->pm_stats.wired_count, -1);
2848 atomic_add_long(&pmap->pm_stats.resident_count, -1);
2849 if (pt_pv && vm_page_unwire_quick(pt_pv->pv_m))
2850 panic("pmap_remove: insufficient wirecount");
2855 * Removes this physical page from all physical maps in which it resides.
2856 * Reflects back modify bits to the pager.
2858 * This routine may not be called from an interrupt.
2862 pmap_remove_all(vm_page_t m)
2864 struct pmap_inval_info info;
2867 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2870 pmap_inval_init(&info);
2871 vm_page_spin_lock(m);
2872 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2873 KKASSERT(pv->pv_m == m);
2874 if (pv_hold_try(pv)) {
2875 vm_page_spin_unlock(m);
2877 vm_page_spin_unlock(m);
2879 if (pv->pv_m != m) {
2881 vm_page_spin_lock(m);
2886 * Holding no spinlocks, pv is locked.
2888 pmap_remove_pv_pte(pv, NULL, &info);
2889 pmap_remove_pv_page(pv);
2891 vm_page_spin_lock(m);
2893 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2894 vm_page_spin_unlock(m);
2895 pmap_inval_done(&info);
2901 * Set the physical protection on the specified range of this map
2904 * This function may not be called from an interrupt if the map is
2905 * not the kernel_pmap.
2908 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2910 /* JG review for NX */
2914 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2915 pmap_remove(pmap, sva, eva);
2918 if (prot & VM_PROT_WRITE)
2920 pmap_scan(pmap, sva, eva, pmap_protect_callback, &prot);
2925 pmap_protect_callback(pmap_t pmap, struct pmap_inval_info *info,
2926 pv_entry_t pte_pv, pv_entry_t pt_pv, vm_offset_t va,
2927 pt_entry_t *ptep, void *arg __unused)
2936 pmap_inval_interlock(info, pmap, va);
2943 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2944 KKASSERT(m == pte_pv->pv_m);
2945 vm_page_flag_set(m, PG_REFERENCED);
2949 if (pmap_track_modified(pte_pv->pv_pindex)) {
2951 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2958 if (pbits != cbits && !atomic_cmpset_long(ptep, pbits, cbits)) {
2961 pmap_inval_deinterlock(info, pmap);
2967 * Insert the vm_page (m) at the virtual address (va), replacing any prior
2968 * mapping at that address. Set protection and wiring as requested.
2970 * NOTE: This routine MUST insert the page into the pmap now, it cannot
2974 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2977 pmap_inval_info info;
2978 pv_entry_t pt_pv; /* page table */
2979 pv_entry_t pte_pv; /* page table entry */
2982 pt_entry_t origpte, newpte;
2987 va = trunc_page(va);
2988 #ifdef PMAP_DIAGNOSTIC
2990 panic("pmap_enter: toobig");
2991 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2992 panic("pmap_enter: invalid to pmap_enter page table "
2993 "pages (va: 0x%lx)", va);
2995 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2996 kprintf("Warning: pmap_enter called on UVA with "
2999 db_print_backtrace();
3002 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
3003 kprintf("Warning: pmap_enter called on KVA without"
3006 db_print_backtrace();
3011 * Get locked PV entries for our new page table entry (pte_pv)
3012 * and for its parent page table (pt_pv). We need the parent
3013 * so we can resolve the location of the ptep.
3015 * Only hardware MMU actions can modify the ptep out from
3018 * if (m) is fictitious or unmanaged we do not create a managing
3019 * pte_pv for it. Any pre-existing page's management state must
3020 * match (avoiding code complexity).
3022 * If the pmap is still being initialized we assume existing
3025 * Kernel mapppings do not track page table pages (i.e. pt_pv).
3026 * pmap_allocpte() checks the
3028 if (pmap_initialized == FALSE) {
3032 } else if (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) {
3034 if (va >= VM_MAX_USER_ADDRESS) {
3038 pt_pv = pmap_allocpte(pmap, pmap_pt_pindex(va), NULL);
3039 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
3041 KKASSERT(*ptep == 0 || (*ptep & PG_MANAGED) == 0);
3043 if (va >= VM_MAX_USER_ADDRESS) {
3045 pte_pv = pmap_allocpte(pmap, pmap_pte_pindex(va), NULL);
3048 pte_pv = pmap_allocpte(pmap, pmap_pte_pindex(va),
3050 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
3052 KKASSERT(*ptep == 0 || (*ptep & PG_MANAGED));
3055 pa = VM_PAGE_TO_PHYS(m);
3057 opa = origpte & PG_FRAME;
3059 newpte = (pt_entry_t)(pa | pte_prot(pmap, prot) | PG_V | PG_A);
3062 if (va < VM_MAX_USER_ADDRESS)
3065 newpte |= PG_MANAGED;
3066 if (pmap == &kernel_pmap)
3070 * It is possible for multiple faults to occur in threaded
3071 * environments, the existing pte might be correct.
3073 if (((origpte ^ newpte) & ~(pt_entry_t)(PG_M|PG_A)) == 0)
3076 if ((prot & VM_PROT_NOSYNC) == 0)
3077 pmap_inval_init(&info);
3080 * Ok, either the address changed or the protection or wiring
3083 * Clear the current entry, interlocking the removal. For managed
3084 * pte's this will also flush the modified state to the vm_page.
3085 * Atomic ops are mandatory in order to ensure that PG_M events are
3086 * not lost during any transition.
3091 * pmap_remove_pv_pte() unwires pt_pv and assumes
3092 * we will free pte_pv, but since we are reusing
3093 * pte_pv we want to retain the wire count.
3095 * pt_pv won't exist for a kernel page (managed or
3099 vm_page_wire_quick(pt_pv->pv_m);
3100 if (prot & VM_PROT_NOSYNC)
3101 pmap_remove_pv_pte(pte_pv, pt_pv, NULL);
3103 pmap_remove_pv_pte(pte_pv, pt_pv, &info);
3105 pmap_remove_pv_page(pte_pv);
3106 } else if (prot & VM_PROT_NOSYNC) {
3107 /* leave wire count on PT page intact */
3108 (void)pte_load_clear(ptep);
3109 cpu_invlpg((void *)va);
3110 atomic_add_long(&pmap->pm_stats.resident_count, -1);
3112 /* leave wire count on PT page intact */
3113 pmap_inval_interlock(&info, pmap, va);
3114 (void)pte_load_clear(ptep);
3115 pmap_inval_deinterlock(&info, pmap);
3116 atomic_add_long(&pmap->pm_stats.resident_count, -1);
3118 KKASSERT(*ptep == 0);
3123 * Enter on the PV list if part of our managed memory.
3124 * Wiring of the PT page is already handled.
3126 KKASSERT(pte_pv->pv_m == NULL);
3127 vm_page_spin_lock(m);
3129 TAILQ_INSERT_TAIL(&m->md.pv_list, pte_pv, pv_list);
3132 atomic_add_int(&m->object->agg_pv_list_count, 1);
3134 vm_page_flag_set(m, PG_MAPPED);
3135 vm_page_spin_unlock(m);
3136 } else if (pt_pv && opa == 0) {
3138 * We have to adjust the wire count on the PT page ourselves
3139 * for unmanaged entries. If opa was non-zero we retained
3140 * the existing wire count from the removal.
3142 vm_page_wire_quick(pt_pv->pv_m);
3146 * Ok, for UVM (pt_pv != NULL) we don't need to interlock or
3147 * invalidate anything, the TLB won't have any stale entries to
3150 * For KVM there appear to still be issues. Theoretically we
3151 * should be able to scrap the interlocks entirely but we
3154 if ((prot & VM_PROT_NOSYNC) == 0 && pt_pv == NULL)
3155 pmap_inval_interlock(&info, pmap, va);
3156 *(volatile pt_entry_t *)ptep = newpte;
3158 if ((prot & VM_PROT_NOSYNC) == 0 && pt_pv == NULL)
3159 pmap_inval_deinterlock(&info, pmap);
3160 else if (pt_pv == NULL)
3161 cpu_invlpg((void *)va);
3164 atomic_add_long(&pmap->pm_stats.wired_count, 1);
3166 vm_page_flag_set(m, PG_WRITEABLE);
3168 atomic_add_long(&pmap->pm_stats.resident_count, 1);
3173 if ((prot & VM_PROT_NOSYNC) == 0 || pte_pv == NULL)
3174 pmap_inval_done(&info);
3176 KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
3179 * Cleanup the pv entry, allowing other accessors.
3188 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
3189 * This code also assumes that the pmap has no pre-existing entry for this
3192 * This code currently may only be used on user pmaps, not kernel_pmap.
3195 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
3197 pmap_enter(pmap, va, m, VM_PROT_READ, FALSE);
3201 * Make a temporary mapping for a physical address. This is only intended
3202 * to be used for panic dumps.
3204 * The caller is responsible for calling smp_invltlb().
3207 pmap_kenter_temporary(vm_paddr_t pa, long i)
3209 pmap_kenter_quick((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
3210 return ((void *)crashdumpmap);
3213 #define MAX_INIT_PT (96)
3216 * This routine preloads the ptes for a given object into the specified pmap.
3217 * This eliminates the blast of soft faults on process startup and
3218 * immediately after an mmap.
3220 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
3223 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
3224 vm_object_t object, vm_pindex_t pindex,
3225 vm_size_t size, int limit)
3227 struct rb_vm_page_scan_info info;
3232 * We can't preinit if read access isn't set or there is no pmap
3235 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
3239 * We can't preinit if the pmap is not the current pmap
3241 lp = curthread->td_lwp;
3242 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
3245 psize = x86_64_btop(size);
3247 if ((object->type != OBJT_VNODE) ||
3248 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
3249 (object->resident_page_count > MAX_INIT_PT))) {
3253 if (pindex + psize > object->size) {
3254 if (object->size < pindex)
3256 psize = object->size - pindex;
3263 * Use a red-black scan to traverse the requested range and load
3264 * any valid pages found into the pmap.
3266 * We cannot safely scan the object's memq without holding the
3269 info.start_pindex = pindex;
3270 info.end_pindex = pindex + psize - 1;
3276 vm_object_hold_shared(object);
3277 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
3278 pmap_object_init_pt_callback, &info);
3279 vm_object_drop(object);
3284 pmap_object_init_pt_callback(vm_page_t p, void *data)
3286 struct rb_vm_page_scan_info *info = data;
3287 vm_pindex_t rel_index;
3290 * don't allow an madvise to blow away our really
3291 * free pages allocating pv entries.
3293 if ((info->limit & MAP_PREFAULT_MADVISE) &&
3294 vmstats.v_free_count < vmstats.v_free_reserved) {
3297 if (vm_page_busy_try(p, TRUE))
3299 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
3300 (p->flags & PG_FICTITIOUS) == 0) {
3301 if ((p->queue - p->pc) == PQ_CACHE)
3302 vm_page_deactivate(p);
3303 rel_index = p->pindex - info->start_pindex;
3304 pmap_enter_quick(info->pmap,
3305 info->addr + x86_64_ptob(rel_index), p);
3313 * Return TRUE if the pmap is in shape to trivially pre-fault the specified
3316 * Returns FALSE if it would be non-trivial or if a pte is already loaded
3319 * XXX This is safe only because page table pages are not freed.
3322 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
3326 /*spin_lock(&pmap->pm_spin);*/
3327 if ((pte = pmap_pte(pmap, addr)) != NULL) {
3329 /*spin_unlock(&pmap->pm_spin);*/
3333 /*spin_unlock(&pmap->pm_spin);*/
3338 * Change the wiring attribute for a pmap/va pair. The mapping must already
3339 * exist in the pmap. The mapping may or may not be managed.
3342 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3349 lwkt_gettoken(&pmap->pm_token);
3350 pv = pmap_allocpte(pmap, pmap_pt_pindex(va), NULL);
3351 ptep = pv_pte_lookup(pv, pmap_pte_index(va));
3353 if (wired && !pmap_pte_w(ptep))
3354 atomic_add_long(&pmap->pm_stats.wired_count, 1);
3355 else if (!wired && pmap_pte_w(ptep))
3356 atomic_add_long(&pmap->pm_stats.wired_count, -1);
3359 * Wiring is not a hardware characteristic so there is no need to
3360 * invalidate TLB. However, in an SMP environment we must use
3361 * a locked bus cycle to update the pte (if we are not using
3362 * the pmap_inval_*() API that is)... it's ok to do this for simple
3367 atomic_set_long(ptep, PG_W);
3369 atomic_clear_long(ptep, PG_W);
3372 atomic_set_long_nonlocked(ptep, PG_W);
3374 atomic_clear_long_nonlocked(ptep, PG_W);
3377 lwkt_reltoken(&pmap->pm_token);
3383 * Copy the range specified by src_addr/len from the source map to
3384 * the range dst_addr/len in the destination map.
3386 * This routine is only advisory and need not do anything.
3389 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3390 vm_size_t len, vm_offset_t src_addr)
3397 * Zero the specified physical page.
3399 * This function may be called from an interrupt and no locking is
3403 pmap_zero_page(vm_paddr_t phys)
3405 vm_offset_t va = PHYS_TO_DMAP(phys);
3407 pagezero((void *)va);
3411 * pmap_page_assertzero:
3413 * Assert that a page is empty, panic if it isn't.
3416 pmap_page_assertzero(vm_paddr_t phys)
3418 vm_offset_t va = PHYS_TO_DMAP(phys);
3421 for (i = 0; i < PAGE_SIZE; i += sizeof(long)) {
3422 if (*(long *)((char *)va + i) != 0) {
3423 panic("pmap_page_assertzero() @ %p not zero!\n",
3424 (void *)(intptr_t)va);
3432 * Zero part of a physical page by mapping it into memory and clearing
3433 * its contents with bzero.
3435 * off and size may not cover an area beyond a single hardware page.
3438 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3440 vm_offset_t virt = PHYS_TO_DMAP(phys);
3442 bzero((char *)virt + off, size);
3448 * Copy the physical page from the source PA to the target PA.
3449 * This function may be called from an interrupt. No locking
3453 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3455 vm_offset_t src_virt, dst_virt;
3457 src_virt = PHYS_TO_DMAP(src);
3458 dst_virt = PHYS_TO_DMAP(dst);
3459 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
3463 * pmap_copy_page_frag:
3465 * Copy the physical page from the source PA to the target PA.
3466 * This function may be called from an interrupt. No locking
3470 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3472 vm_offset_t src_virt, dst_virt;
3474 src_virt = PHYS_TO_DMAP(src);
3475 dst_virt = PHYS_TO_DMAP(dst);
3477 bcopy((char *)src_virt + (src & PAGE_MASK),
3478 (char *)dst_virt + (dst & PAGE_MASK),
3483 * Returns true if the pmap's pv is one of the first 16 pvs linked to from
3484 * this page. This count may be changed upwards or downwards in the future;
3485 * it is only necessary that true be returned for a small subset of pmaps
3486 * for proper page aging.
3489 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3494 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3497 vm_page_spin_lock(m);
3498 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3499 if (pv->pv_pmap == pmap) {
3500 vm_page_spin_unlock(m);
3507 vm_page_spin_unlock(m);
3512 * Remove all pages from specified address space this aids process exit
3513 * speeds. Also, this code may be special cased for the current process
3517 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3519 pmap_remove(pmap, sva, eva);
3523 * pmap_testbit tests bits in pte's note that the testbit/clearbit
3524 * routines are inline, and a lot of things compile-time evaluate.
3528 pmap_testbit(vm_page_t m, int bit)
3533 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3536 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3538 vm_page_spin_lock(m);
3539 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
3540 vm_page_spin_unlock(m);
3544 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3546 * if the bit being tested is the modified bit, then
3547 * mark clean_map and ptes as never
3550 if (bit & (PG_A|PG_M)) {
3551 if (!pmap_track_modified(pv->pv_pindex))
3555 #if defined(PMAP_DIAGNOSTIC)
3556 if (pv->pv_pmap == NULL) {
3557 kprintf("Null pmap (tb) at pindex: %"PRIu64"\n",
3562 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
3564 vm_page_spin_unlock(m);
3568 vm_page_spin_unlock(m);
3573 * This routine is used to modify bits in ptes. Only one bit should be
3574 * specified. PG_RW requires special handling.
3576 * Caller must NOT hold any spin locks
3580 pmap_clearbit(vm_page_t m, int bit)
3582 struct pmap_inval_info info;
3586 vm_pindex_t save_pindex;
3590 vm_page_flag_clear(m, PG_WRITEABLE);
3591 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
3598 * Loop over all current mappings setting/clearing as appropos If
3599 * setting RO do we need to clear the VAC?
3601 * NOTE: When clearing PG_M we could also (not implemented) drop
3602 * through to the PG_RW code and clear PG_RW too, forcing
3603 * a fault on write to redetect PG_M for virtual kernels, but
3604 * it isn't necessary since virtual kernels invalidate the
3605 * pte when they clear the VPTE_M bit in their virtual page
3608 * NOTE: Does not re-dirty the page when clearing only PG_M.
3610 if ((bit & PG_RW) == 0) {
3611 vm_page_spin_lock(m);
3612 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3613 #if defined(PMAP_DIAGNOSTIC)
3614 if (pv->pv_pmap == NULL) {
3615 kprintf("Null pmap (cb) at pindex: %"PRIu64"\n",
3620 pte = pmap_pte_quick(pv->pv_pmap,
3621 pv->pv_pindex << PAGE_SHIFT);
3624 atomic_clear_long(pte, bit);
3626 vm_page_spin_unlock(m);
3631 * Clear PG_RW. Also clears PG_M and marks the page dirty if PG_M
3634 pmap_inval_init(&info);
3637 vm_page_spin_lock(m);
3638 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3640 * don't write protect pager mappings
3642 if (!pmap_track_modified(pv->pv_pindex))
3645 #if defined(PMAP_DIAGNOSTIC)
3646 if (pv->pv_pmap == NULL) {
3647 kprintf("Null pmap (cb) at pindex: %"PRIu64"\n",
3653 * Skip pages which do not have PG_RW set.
3655 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
3656 if ((*pte & PG_RW) == 0)
3662 if (pv_hold_try(pv) == 0) {
3663 vm_page_spin_unlock(m);
3664 pv_lock(pv); /* held, now do a blocking lock */
3665 pv_put(pv); /* and release */
3666 goto restart; /* anything could have happened */
3669 save_pmap = pv->pv_pmap;
3670 vm_page_spin_unlock(m);
3671 pmap_inval_interlock(&info, save_pmap,
3672 (vm_offset_t)save_pindex << PAGE_SHIFT);
3673 KKASSERT(pv->pv_pmap == save_pmap);
3677 if (atomic_cmpset_long(pte, pbits,
3678 pbits & ~(PG_RW|PG_M))) {
3682 pmap_inval_deinterlock(&info, save_pmap);
3683 vm_page_spin_lock(m);
3686 * If PG_M was found to be set while we were clearing PG_RW
3687 * we also clear PG_M (done above) and mark the page dirty.
3688 * Callers expect this behavior.
3694 vm_page_spin_unlock(m);
3695 pmap_inval_done(&info);
3699 * Lower the permission for all mappings to a given page.
3701 * Page must be busied by caller.
3704 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3706 /* JG NX support? */
3707 if ((prot & VM_PROT_WRITE) == 0) {
3708 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3710 * NOTE: pmap_clearbit(.. PG_RW) also clears
3711 * the PG_WRITEABLE flag in (m).
3713 pmap_clearbit(m, PG_RW);
3721 pmap_phys_address(vm_pindex_t ppn)
3723 return (x86_64_ptob(ppn));
3727 * Return a count of reference bits for a page, clearing those bits.
3728 * It is not necessary for every reference bit to be cleared, but it
3729 * is necessary that 0 only be returned when there are truly no
3730 * reference bits set.
3732 * XXX: The exact number of bits to check and clear is a matter that
3733 * should be tested and standardized at some point in the future for
3734 * optimal aging of shared pages.
3736 * This routine may not block.
3739 pmap_ts_referenced(vm_page_t m)
3745 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3748 vm_page_spin_lock(m);
3749 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3750 if (!pmap_track_modified(pv->pv_pindex))
3752 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
3753 if (pte && (*pte & PG_A)) {
3755 atomic_clear_long(pte, PG_A);
3757 atomic_clear_long_nonlocked(pte, PG_A);
3764 vm_page_spin_unlock(m);
3771 * Return whether or not the specified physical page was modified
3772 * in any physical maps.
3775 pmap_is_modified(vm_page_t m)
3779 res = pmap_testbit(m, PG_M);
3784 * Clear the modify bits on the specified physical page.
3787 pmap_clear_modify(vm_page_t m)
3789 pmap_clearbit(m, PG_M);
3793 * pmap_clear_reference:
3795 * Clear the reference bit on the specified physical page.
3798 pmap_clear_reference(vm_page_t m)
3800 pmap_clearbit(m, PG_A);
3804 * Miscellaneous support routines follow
3809 i386_protection_init(void)
3813 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
3814 kp = protection_codes;
3815 for (prot = 0; prot < 8; prot++) {
3817 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3819 * Read access is also 0. There isn't any execute bit,
3820 * so just make it readable.
3822 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3823 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3824 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3827 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3828 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3829 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3830 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3838 * Map a set of physical memory pages into the kernel virtual
3839 * address space. Return a pointer to where it is mapped. This
3840 * routine is intended to be used for mapping device memory,
3843 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3847 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3849 vm_offset_t va, tmpva, offset;
3852 offset = pa & PAGE_MASK;
3853 size = roundup(offset + size, PAGE_SIZE);
3855 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3857 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3859 pa = pa & ~PAGE_MASK;
3860 for (tmpva = va; size > 0;) {
3861 pte = vtopte(tmpva);
3862 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3870 return ((void *)(va + offset));
3874 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
3876 vm_offset_t va, tmpva, offset;
3879 offset = pa & PAGE_MASK;
3880 size = roundup(offset + size, PAGE_SIZE);
3882 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3884 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3886 pa = pa & ~PAGE_MASK;
3887 for (tmpva = va; size > 0;) {
3888 pte = vtopte(tmpva);
3889 *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */
3897 return ((void *)(va + offset));
3901 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3903 vm_offset_t base, offset;
3905 base = va & ~PAGE_MASK;
3906 offset = va & PAGE_MASK;
3907 size = roundup(offset + size, PAGE_SIZE);
3908 pmap_qremove(va, size >> PAGE_SHIFT);
3909 kmem_free(&kernel_map, base, size);
3913 * perform the pmap work for mincore
3916 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3918 pt_entry_t *ptep, pte;
3922 lwkt_gettoken(&pmap->pm_token);
3923 ptep = pmap_pte(pmap, addr);
3925 if (ptep && (pte = *ptep) != 0) {
3928 val = MINCORE_INCORE;
3929 if ((pte & PG_MANAGED) == 0)
3932 pa = pte & PG_FRAME;
3934 m = PHYS_TO_VM_PAGE(pa);
3940 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3942 * Modified by someone
3944 else if (m->dirty || pmap_is_modified(m))
3945 val |= MINCORE_MODIFIED_OTHER;
3950 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3953 * Referenced by someone
3955 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3956 val |= MINCORE_REFERENCED_OTHER;
3957 vm_page_flag_set(m, PG_REFERENCED);
3961 lwkt_reltoken(&pmap->pm_token);
3967 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3968 * vmspace will be ref'd and the old one will be deref'd.
3970 * The vmspace for all lwps associated with the process will be adjusted
3971 * and cr3 will be reloaded if any lwp is the current lwp.
3973 * The process must hold the vmspace->vm_map.token for oldvm and newvm
3976 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3978 struct vmspace *oldvm;
3981 oldvm = p->p_vmspace;
3982 if (oldvm != newvm) {
3984 sysref_get(&newvm->vm_sysref);
3985 p->p_vmspace = newvm;
3986 KKASSERT(p->p_nthreads == 1);
3987 lp = RB_ROOT(&p->p_lwp_tree);
3988 pmap_setlwpvm(lp, newvm);
3990 sysref_put(&oldvm->vm_sysref);
3995 * Set the vmspace for a LWP. The vmspace is almost universally set the
3996 * same as the process vmspace, but virtual kernels need to swap out contexts
3997 * on a per-lwp basis.
3999 * Caller does not necessarily hold any vmspace tokens. Caller must control
4000 * the lwp (typically be in the context of the lwp). We use a critical
4001 * section to protect against statclock and hardclock (statistics collection).
4004 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
4006 struct vmspace *oldvm;
4009 oldvm = lp->lwp_vmspace;
4011 if (oldvm != newvm) {
4013 lp->lwp_vmspace = newvm;
4014 if (curthread->td_lwp == lp) {
4015 pmap = vmspace_pmap(newvm);
4017 atomic_set_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
4018 if (pmap->pm_active & CPUMASK_LOCK)
4019 pmap_interlock_wait(newvm);
4021 pmap->pm_active |= 1;
4023 #if defined(SWTCH_OPTIM_STATS)
4026 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
4027 curthread->td_pcb->pcb_cr3 |= PG_RW | PG_U | PG_V;
4028 load_cr3(curthread->td_pcb->pcb_cr3);
4029 pmap = vmspace_pmap(oldvm);
4031 atomic_clear_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
4033 pmap->pm_active &= ~(cpumask_t)1;
4043 * Called when switching to a locked pmap, used to interlock against pmaps
4044 * undergoing modifications to prevent us from activating the MMU for the
4045 * target pmap until all such modifications have completed. We have to do
4046 * this because the thread making the modifications has already set up its
4047 * SMP synchronization mask.
4052 pmap_interlock_wait(struct vmspace *vm)
4054 struct pmap *pmap = &vm->vm_pmap;
4056 if (pmap->pm_active & CPUMASK_LOCK) {
4058 DEBUG_PUSH_INFO("pmap_interlock_wait");
4059 while (pmap->pm_active & CPUMASK_LOCK) {
4061 lwkt_process_ipiq();
4071 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
4074 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
4078 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
4083 * Used by kmalloc/kfree, page already exists at va
4086 pmap_kvtom(vm_offset_t va)
4088 return(PHYS_TO_VM_PAGE(*vtopte(va) & PG_FRAME));