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 * Copyright (c) 2011 Matthew Dillon
12 * All rights reserved.
14 * This code is derived from software contributed to Berkeley by
15 * the Systems Programming Group of the University of Utah Computer
16 * Science Department and William Jolitz of UUNET Technologies Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
47 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
51 * Manages physical address maps.
53 * In addition to hardware address maps, this
54 * module is called upon to provide software-use-only
55 * maps which may or may not be stored in the same
56 * form as hardware maps. These pseudo-maps are
57 * used to store intermediate results from copy
58 * operations to and from address spaces.
60 * Since the information managed by this module is
61 * also stored by the logical address mapping module,
62 * this module may throw away valid virtual-to-physical
63 * mappings at almost any time. However, invalidations
64 * of virtual-to-physical mappings must be done as
67 * In order to cope with hardware architectures which
68 * make virtual-to-physical map invalidates expensive,
69 * this module may delay invalidate or reduced protection
70 * operations until such time as they are actually
71 * necessary. This module is given full information as
72 * to which processors are currently using which maps,
73 * and to when physical maps must be made correct.
77 #include "opt_disable_pse.h"
80 #include "opt_msgbuf.h"
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
86 #include <sys/msgbuf.h>
87 #include <sys/vmmeter.h>
91 #include <vm/vm_param.h>
92 #include <sys/sysctl.h>
94 #include <vm/vm_kern.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_map.h>
97 #include <vm/vm_object.h>
98 #include <vm/vm_extern.h>
99 #include <vm/vm_pageout.h>
100 #include <vm/vm_pager.h>
101 #include <vm/vm_zone.h>
103 #include <sys/user.h>
104 #include <sys/thread2.h>
105 #include <sys/sysref2.h>
106 #include <sys/spinlock2.h>
107 #include <vm/vm_page2.h>
109 #include <machine/cputypes.h>
110 #include <machine/md_var.h>
111 #include <machine/specialreg.h>
112 #include <machine/smp.h>
113 #include <machine_base/apic/apicreg.h>
114 #include <machine/globaldata.h>
115 #include <machine/pmap.h>
116 #include <machine/pmap_inval.h>
120 #define PMAP_KEEP_PDIRS
121 #ifndef PMAP_SHPGPERPROC
122 #define PMAP_SHPGPERPROC 200
125 #if defined(DIAGNOSTIC)
126 #define PMAP_DIAGNOSTIC
132 * pmap debugging will report who owns a pv lock when blocking.
136 #define PMAP_DEBUG_DECL ,const char *func, int lineno
137 #define PMAP_DEBUG_ARGS , __func__, __LINE__
138 #define PMAP_DEBUG_COPY , func, lineno
140 #define pv_get(pmap, pindex) _pv_get(pmap, pindex \
142 #define pv_lock(pv) _pv_lock(pv \
144 #define pv_hold_try(pv) _pv_hold_try(pv \
146 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp \
151 #define PMAP_DEBUG_DECL
152 #define PMAP_DEBUG_ARGS
153 #define PMAP_DEBUG_COPY
155 #define pv_get(pmap, pindex) _pv_get(pmap, pindex)
156 #define pv_lock(pv) _pv_lock(pv)
157 #define pv_hold_try(pv) _pv_hold_try(pv)
158 #define pv_alloc(pmap, pindex, isnewp) _pv_alloc(pmap, pindex, isnewp)
163 * Get PDEs and PTEs for user/kernel address space
165 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
167 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & PG_V) != 0)
168 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & PG_W) != 0)
169 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & PG_M) != 0)
170 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & PG_A) != 0)
171 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & PG_V) != 0)
174 * Given a map and a machine independent protection code,
175 * convert to a vax protection code.
177 #define pte_prot(m, p) \
178 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
179 static int protection_codes[8];
181 struct pmap kernel_pmap;
182 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
184 vm_paddr_t avail_start; /* PA of first available physical page */
185 vm_paddr_t avail_end; /* PA of last available physical page */
186 vm_offset_t virtual2_start; /* cutout free area prior to kernel start */
187 vm_offset_t virtual2_end;
188 vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */
189 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
190 vm_offset_t KvaStart; /* VA start of KVA space */
191 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
192 vm_offset_t KvaSize; /* max size of kernel virtual address space */
193 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
194 static int pgeflag; /* PG_G or-in */
195 static int pseflag; /* PG_PS or-in */
198 static vm_paddr_t dmaplimit;
200 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
202 static uint64_t KPTbase;
203 static uint64_t KPTphys;
204 static uint64_t KPDphys; /* phys addr of kernel level 2 */
205 static uint64_t KPDbase; /* phys addr of kernel level 2 @ KERNBASE */
206 uint64_t KPDPphys; /* phys addr of kernel level 3 */
207 uint64_t KPML4phys; /* phys addr of kernel level 4 */
209 static uint64_t DMPDphys; /* phys addr of direct mapped level 2 */
210 static uint64_t DMPDPphys; /* phys addr of direct mapped level 3 */
213 * Data for the pv entry allocation mechanism
215 static vm_zone_t pvzone;
216 static struct vm_zone pvzone_store;
217 static struct vm_object pvzone_obj;
218 static int pv_entry_max=0, pv_entry_high_water=0;
219 static int pmap_pagedaemon_waken = 0;
220 static struct pv_entry *pvinit;
223 * All those kernel PT submaps that BSD is so fond of
225 pt_entry_t *CMAP1 = 0, *ptmmap;
226 caddr_t CADDR1 = 0, ptvmmap = 0;
227 static pt_entry_t *msgbufmap;
228 struct msgbuf *msgbufp=0;
233 static pt_entry_t *pt_crashdumpmap;
234 static caddr_t crashdumpmap;
236 static int pmap_yield_count = 64;
237 SYSCTL_INT(_machdep, OID_AUTO, pmap_yield_count, CTLFLAG_RW,
238 &pmap_yield_count, 0, "Yield during init_pt/release");
242 static void pv_hold(pv_entry_t pv);
243 static int _pv_hold_try(pv_entry_t pv
245 static void pv_drop(pv_entry_t pv);
246 static void _pv_lock(pv_entry_t pv
248 static void pv_unlock(pv_entry_t pv);
249 static pv_entry_t _pv_alloc(pmap_t pmap, vm_pindex_t pindex, int *isnew
251 static pv_entry_t _pv_get(pmap_t pmap, vm_pindex_t pindex
253 static pv_entry_t pv_get_try(pmap_t pmap, vm_pindex_t pindex, int *errorp);
254 static pv_entry_t pv_find(pmap_t pmap, vm_pindex_t pindex);
255 static void pv_put(pv_entry_t pv);
256 static void pv_free(pv_entry_t pv);
257 static void *pv_pte_lookup(pv_entry_t pv, vm_pindex_t pindex);
258 static pv_entry_t pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex,
260 static void pmap_remove_pv_pte(pv_entry_t pv, pv_entry_t pvp,
261 struct pmap_inval_info *info);
262 static vm_page_t pmap_remove_pv_page(pv_entry_t pv, int holdpg);
264 static void pmap_remove_callback(pmap_t pmap, struct pmap_inval_info *info,
265 pv_entry_t pte_pv, pv_entry_t pt_pv, vm_offset_t va,
266 pt_entry_t *ptep, void *arg __unused);
267 static void pmap_protect_callback(pmap_t pmap, struct pmap_inval_info *info,
268 pv_entry_t pte_pv, pv_entry_t pt_pv, vm_offset_t va,
269 pt_entry_t *ptep, void *arg __unused);
271 static void i386_protection_init (void);
272 static void create_pagetables(vm_paddr_t *firstaddr);
273 static void pmap_remove_all (vm_page_t m);
274 static boolean_t pmap_testbit (vm_page_t m, int bit);
276 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
277 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
279 static unsigned pdir4mb;
282 pv_entry_compare(pv_entry_t pv1, pv_entry_t pv2)
284 if (pv1->pv_pindex < pv2->pv_pindex)
286 if (pv1->pv_pindex > pv2->pv_pindex)
291 RB_GENERATE2(pv_entry_rb_tree, pv_entry, pv_entry,
292 pv_entry_compare, vm_pindex_t, pv_pindex);
295 * Move the kernel virtual free pointer to the next
296 * 2MB. This is used to help improve performance
297 * by using a large (2MB) page for much of the kernel
298 * (.text, .data, .bss)
302 pmap_kmem_choose(vm_offset_t addr)
304 vm_offset_t newaddr = addr;
306 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
313 * Super fast pmap_pte routine best used when scanning the pv lists.
314 * This eliminates many course-grained invltlb calls. Note that many of
315 * the pv list scans are across different pmaps and it is very wasteful
316 * to do an entire invltlb when checking a single mapping.
318 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
322 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
324 return pmap_pte(pmap, va);
328 * Returns the pindex of a page table entry (representing a terminal page).
329 * There are NUPTE_TOTAL page table entries possible (a huge number)
331 * x86-64 has a 48-bit address space, where bit 47 is sign-extended out.
332 * We want to properly translate negative KVAs.
336 pmap_pte_pindex(vm_offset_t va)
338 return ((va >> PAGE_SHIFT) & (NUPTE_TOTAL - 1));
342 * Returns the pindex of a page table.
346 pmap_pt_pindex(vm_offset_t va)
348 return (NUPTE_TOTAL + ((va >> PDRSHIFT) & (NUPT_TOTAL - 1)));
352 * Returns the pindex of a page directory.
356 pmap_pd_pindex(vm_offset_t va)
358 return (NUPTE_TOTAL + NUPT_TOTAL +
359 ((va >> PDPSHIFT) & (NUPD_TOTAL - 1)));
364 pmap_pdp_pindex(vm_offset_t va)
366 return (NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL +
367 ((va >> PML4SHIFT) & (NUPDP_TOTAL - 1)));
372 pmap_pml4_pindex(void)
374 return (NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL + NUPDP_TOTAL);
378 * Return various clipped indexes for a given VA
380 * Returns the index of a pte in a page table, representing a terminal
385 pmap_pte_index(vm_offset_t va)
387 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
391 * Returns the index of a pt in a page directory, representing a page
396 pmap_pt_index(vm_offset_t va)
398 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
402 * Returns the index of a pd in a page directory page, representing a page
407 pmap_pd_index(vm_offset_t va)
409 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
413 * Returns the index of a pdp in the pml4 table, representing a page
418 pmap_pdp_index(vm_offset_t va)
420 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
424 * Generic procedure to index a pte from a pt, pd, or pdp.
428 pv_pte_lookup(pv_entry_t pv, vm_pindex_t pindex)
432 pte = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pv->pv_m));
433 return(&pte[pindex]);
437 * Return pointer to PDP slot in the PML4
441 pmap_pdp(pmap_t pmap, vm_offset_t va)
443 return (&pmap->pm_pml4[pmap_pdp_index(va)]);
447 * Return pointer to PD slot in the PDP given a pointer to the PDP
451 pmap_pdp_to_pd(pml4_entry_t *pdp, vm_offset_t va)
455 pd = (pdp_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
456 return (&pd[pmap_pd_index(va)]);
460 * Return pointer to PD slot in the PDP
464 pmap_pd(pmap_t pmap, vm_offset_t va)
468 pdp = pmap_pdp(pmap, va);
469 if ((*pdp & PG_V) == 0)
471 return (pmap_pdp_to_pd(pdp, va));
475 * Return pointer to PT slot in the PD given a pointer to the PD
479 pmap_pd_to_pt(pdp_entry_t *pd, vm_offset_t va)
483 pt = (pd_entry_t *)PHYS_TO_DMAP(*pd & PG_FRAME);
484 return (&pt[pmap_pt_index(va)]);
488 * Return pointer to PT slot in the PD
492 pmap_pt(pmap_t pmap, vm_offset_t va)
496 pd = pmap_pd(pmap, va);
497 if (pd == NULL || (*pd & PG_V) == 0)
499 return (pmap_pd_to_pt(pd, va));
503 * Return pointer to PTE slot in the PT given a pointer to the PT
507 pmap_pt_to_pte(pd_entry_t *pt, vm_offset_t va)
511 pte = (pt_entry_t *)PHYS_TO_DMAP(*pt & PG_FRAME);
512 return (&pte[pmap_pte_index(va)]);
516 * Return pointer to PTE slot in the PT
520 pmap_pte(pmap_t pmap, vm_offset_t va)
524 pt = pmap_pt(pmap, va);
525 if (pt == NULL || (*pt & PG_V) == 0)
527 if ((*pt & PG_PS) != 0)
528 return ((pt_entry_t *)pt);
529 return (pmap_pt_to_pte(pt, va));
533 * Of all the layers (PTE, PT, PD, PDP, PML4) the best one to cache is
534 * the PT layer. This will speed up core pmap operations considerably.
538 pv_cache(pv_entry_t pv, vm_pindex_t pindex)
540 if (pindex >= pmap_pt_pindex(0) && pindex <= pmap_pd_pindex(0))
541 pv->pv_pmap->pm_pvhint = pv;
546 * KVM - return address of PT slot in PD
550 vtopt(vm_offset_t va)
552 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT +
553 NPML4EPGSHIFT)) - 1);
555 return (PDmap + ((va >> PDRSHIFT) & mask));
559 * KVM - return address of PTE slot in PT
563 vtopte(vm_offset_t va)
565 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT +
566 NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
568 return (PTmap + ((va >> PAGE_SHIFT) & mask));
572 allocpages(vm_paddr_t *firstaddr, long n)
577 bzero((void *)ret, n * PAGE_SIZE);
578 *firstaddr += n * PAGE_SIZE;
584 create_pagetables(vm_paddr_t *firstaddr)
586 long i; /* must be 64 bits */
591 * We are running (mostly) V=P at this point
593 * Calculate NKPT - number of kernel page tables. We have to
594 * accomodoate prealloction of the vm_page_array, dump bitmap,
595 * MSGBUF_SIZE, and other stuff. Be generous.
597 * Maxmem is in pages.
599 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
600 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
604 * Starting at the beginning of kvm (not KERNBASE).
606 nkpt_phys = (Maxmem * sizeof(struct vm_page) + NBPDR - 1) / NBPDR;
607 nkpt_phys += (Maxmem * sizeof(struct pv_entry) + NBPDR - 1) / NBPDR;
608 nkpt_phys += ((nkpt + nkpt + 1 + NKPML4E + NKPDPE + NDMPML4E + ndmpdp) +
613 * Starting at KERNBASE - map 2G worth of page table pages.
614 * KERNBASE is offset -2G from the end of kvm.
616 nkpt_base = (NPDPEPG - KPDPI) * NPTEPG; /* typically 2 x 512 */
621 KPTbase = allocpages(firstaddr, nkpt_base);
622 KPTphys = allocpages(firstaddr, nkpt_phys);
623 KPML4phys = allocpages(firstaddr, 1);
624 KPDPphys = allocpages(firstaddr, NKPML4E);
625 KPDphys = allocpages(firstaddr, NKPDPE);
628 * Calculate the page directory base for KERNBASE,
629 * that is where we start populating the page table pages.
630 * Basically this is the end - 2.
632 KPDbase = KPDphys + ((NKPDPE - (NPDPEPG - KPDPI)) << PAGE_SHIFT);
634 DMPDPphys = allocpages(firstaddr, NDMPML4E);
635 if ((amd_feature & AMDID_PAGE1GB) == 0)
636 DMPDphys = allocpages(firstaddr, ndmpdp);
637 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
640 * Fill in the underlying page table pages for the area around
641 * KERNBASE. This remaps low physical memory to KERNBASE.
643 * Read-only from zero to physfree
644 * XXX not fully used, underneath 2M pages
646 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
647 ((pt_entry_t *)KPTbase)[i] = i << PAGE_SHIFT;
648 ((pt_entry_t *)KPTbase)[i] |= PG_RW | PG_V | PG_G;
652 * Now map the initial kernel page tables. One block of page
653 * tables is placed at the beginning of kernel virtual memory,
654 * and another block is placed at KERNBASE to map the kernel binary,
655 * data, bss, and initial pre-allocations.
657 for (i = 0; i < nkpt_base; i++) {
658 ((pd_entry_t *)KPDbase)[i] = KPTbase + (i << PAGE_SHIFT);
659 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V;
661 for (i = 0; i < nkpt_phys; i++) {
662 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
663 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
667 * Map from zero to end of allocations using 2M pages as an
668 * optimization. This will bypass some of the KPTBase pages
669 * above in the KERNBASE area.
671 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
672 ((pd_entry_t *)KPDbase)[i] = i << PDRSHIFT;
673 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V | PG_PS | PG_G;
677 * And connect up the PD to the PDP. The kernel pmap is expected
678 * to pre-populate all of its PDs. See NKPDPE in vmparam.h.
680 for (i = 0; i < NKPDPE; i++) {
681 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] =
682 KPDphys + (i << PAGE_SHIFT);
683 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] |=
687 /* Now set up the direct map space using either 2MB or 1GB pages */
688 /* Preset PG_M and PG_A because demotion expects it */
689 if ((amd_feature & AMDID_PAGE1GB) == 0) {
690 for (i = 0; i < NPDEPG * ndmpdp; i++) {
691 ((pd_entry_t *)DMPDphys)[i] = i << PDRSHIFT;
692 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
695 /* And the direct map space's PDP */
696 for (i = 0; i < ndmpdp; i++) {
697 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
699 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
702 for (i = 0; i < ndmpdp; i++) {
703 ((pdp_entry_t *)DMPDPphys)[i] =
704 (vm_paddr_t)i << PDPSHIFT;
705 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
710 /* And recursively map PML4 to itself in order to get PTmap */
711 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
712 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
714 /* Connect the Direct Map slot up to the PML4 */
715 ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
716 ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
718 /* Connect the KVA slot up to the PML4 */
719 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
720 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
724 * Bootstrap the system enough to run with virtual memory.
726 * On the i386 this is called after mapping has already been enabled
727 * and just syncs the pmap module with what has already been done.
728 * [We can't call it easily with mapping off since the kernel is not
729 * mapped with PA == VA, hence we would have to relocate every address
730 * from the linked base (virtual) address "KERNBASE" to the actual
731 * (physical) address starting relative to 0]
734 pmap_bootstrap(vm_paddr_t *firstaddr)
738 struct mdglobaldata *gd;
741 KvaStart = VM_MIN_KERNEL_ADDRESS;
742 KvaEnd = VM_MAX_KERNEL_ADDRESS;
743 KvaSize = KvaEnd - KvaStart;
745 avail_start = *firstaddr;
748 * Create an initial set of page tables to run the kernel in.
750 create_pagetables(firstaddr);
752 virtual2_start = KvaStart;
753 virtual2_end = PTOV_OFFSET;
755 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
756 virtual_start = pmap_kmem_choose(virtual_start);
758 virtual_end = VM_MAX_KERNEL_ADDRESS;
760 /* XXX do %cr0 as well */
761 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
765 * Initialize protection array.
767 i386_protection_init();
770 * The kernel's pmap is statically allocated so we don't have to use
771 * pmap_create, which is unlikely to work correctly at this part of
772 * the boot sequence (XXX and which no longer exists).
774 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
775 kernel_pmap.pm_count = 1;
776 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
777 RB_INIT(&kernel_pmap.pm_pvroot);
778 spin_init(&kernel_pmap.pm_spin);
779 lwkt_token_init(&kernel_pmap.pm_token, "kpmap_tok");
782 * Reserve some special page table entries/VA space for temporary
785 #define SYSMAP(c, p, v, n) \
786 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
792 * CMAP1/CMAP2 are used for zeroing and copying pages.
794 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
799 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
802 * ptvmmap is used for reading arbitrary physical pages via
805 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
808 * msgbufp is used to map the system message buffer.
809 * XXX msgbufmap is not used.
811 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
812 atop(round_page(MSGBUF_SIZE)))
819 * PG_G is terribly broken on SMP because we IPI invltlb's in some
820 * cases rather then invl1pg. Actually, I don't even know why it
821 * works under UP because self-referential page table mappings
826 if (cpu_feature & CPUID_PGE)
831 * Initialize the 4MB page size flag
835 * The 4MB page version of the initial
836 * kernel page mapping.
840 #if !defined(DISABLE_PSE)
841 if (cpu_feature & CPUID_PSE) {
844 * Note that we have enabled PSE mode
847 ptditmp = *(PTmap + x86_64_btop(KERNBASE));
848 ptditmp &= ~(NBPDR - 1);
849 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
854 * Enable the PSE mode. If we are SMP we can't do this
855 * now because the APs will not be able to use it when
858 load_cr4(rcr4() | CR4_PSE);
861 * We can do the mapping here for the single processor
862 * case. We simply ignore the old page table page from
866 * For SMP, we still need 4K pages to bootstrap APs,
867 * PSE will be enabled as soon as all APs are up.
869 PTD[KPTDI] = (pd_entry_t)ptditmp;
876 * We need to finish setting up the globaldata page for the BSP.
877 * locore has already populated the page table for the mdglobaldata
880 pg = MDGLOBALDATA_BASEALLOC_PAGES;
881 gd = &CPU_prvspace[0].mdglobaldata;
888 * Set 4mb pdir for mp startup
893 if (pseflag && (cpu_feature & CPUID_PSE)) {
894 load_cr4(rcr4() | CR4_PSE);
895 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
903 * Initialize the pmap module.
904 * Called by vm_init, to initialize any structures that the pmap
905 * system needs to map virtual memory.
906 * pmap_init has been enhanced to support in a fairly consistant
907 * way, discontiguous physical memory.
916 * Allocate memory for random pmap data structures. Includes the
920 for (i = 0; i < vm_page_array_size; i++) {
923 m = &vm_page_array[i];
924 TAILQ_INIT(&m->md.pv_list);
928 * init the pv free list
930 initial_pvs = vm_page_array_size;
931 if (initial_pvs < MINPV)
933 pvzone = &pvzone_store;
934 pvinit = (void *)kmem_alloc(&kernel_map,
935 initial_pvs * sizeof (struct pv_entry));
936 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry),
937 pvinit, initial_pvs);
940 * Now it is safe to enable pv_table recording.
942 pmap_initialized = TRUE;
946 * Initialize the address space (zone) for the pv_entries. Set a
947 * high water mark so that the system can recover from excessive
948 * numbers of pv entries.
953 int shpgperproc = PMAP_SHPGPERPROC;
956 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
957 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
958 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
959 pv_entry_high_water = 9 * (pv_entry_max / 10);
962 * Subtract out pages already installed in the zone (hack)
964 entry_max = pv_entry_max - vm_page_array_size;
968 zinitna(pvzone, &pvzone_obj, NULL, 0, entry_max, ZONE_INTERRUPT, 1);
972 /***************************************************
973 * Low level helper routines.....
974 ***************************************************/
976 #if defined(PMAP_DIAGNOSTIC)
979 * This code checks for non-writeable/modified pages.
980 * This should be an invalid condition.
984 pmap_nw_modified(pt_entry_t pte)
986 if ((pte & (PG_M|PG_RW)) == PG_M)
995 * this routine defines the region(s) of memory that should
996 * not be tested for the modified bit.
1000 pmap_track_modified(vm_pindex_t pindex)
1002 vm_offset_t va = (vm_offset_t)pindex << PAGE_SHIFT;
1003 if ((va < clean_sva) || (va >= clean_eva))
1010 * Extract the physical page address associated with the map/VA pair.
1011 * The page must be wired for this to work reliably.
1013 * XXX for the moment we're using pv_find() instead of pv_get(), as
1014 * callers might be expecting non-blocking operation.
1017 pmap_extract(pmap_t pmap, vm_offset_t va)
1024 if (va >= VM_MAX_USER_ADDRESS) {
1026 * Kernel page directories might be direct-mapped and
1027 * there is typically no PV tracking of pte's
1031 pt = pmap_pt(pmap, va);
1032 if (pt && (*pt & PG_V)) {
1034 rtval = *pt & PG_PS_FRAME;
1035 rtval |= va & PDRMASK;
1037 ptep = pmap_pt_to_pte(pt, va);
1039 rtval = *ptep & PG_FRAME;
1040 rtval |= va & PAGE_MASK;
1046 * User pages currently do not direct-map the page directory
1047 * and some pages might not used managed PVs. But all PT's
1050 pt_pv = pv_find(pmap, pmap_pt_pindex(va));
1052 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
1054 rtval = *ptep & PG_FRAME;
1055 rtval |= va & PAGE_MASK;
1064 * Extract the physical page address associated kernel virtual address.
1067 pmap_kextract(vm_offset_t va)
1069 pd_entry_t pt; /* pt entry in pd */
1072 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
1073 pa = DMAP_TO_PHYS(va);
1077 pa = (pt & PG_PS_FRAME) | (va & PDRMASK);
1080 * Beware of a concurrent promotion that changes the
1081 * PDE at this point! For example, vtopte() must not
1082 * be used to access the PTE because it would use the
1083 * new PDE. It is, however, safe to use the old PDE
1084 * because the page table page is preserved by the
1087 pa = *pmap_pt_to_pte(&pt, va);
1088 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1094 /***************************************************
1095 * Low level mapping routines.....
1096 ***************************************************/
1099 * Routine: pmap_kenter
1101 * Add a wired page to the KVA
1102 * NOTE! note that in order for the mapping to take effect -- you
1103 * should do an invltlb after doing the pmap_kenter().
1106 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1110 pmap_inval_info info;
1112 pmap_inval_init(&info); /* XXX remove */
1113 npte = pa | PG_RW | PG_V | pgeflag;
1115 pmap_inval_interlock(&info, &kernel_pmap, va); /* XXX remove */
1117 pmap_inval_deinterlock(&info, &kernel_pmap); /* XXX remove */
1118 pmap_inval_done(&info); /* XXX remove */
1122 * Routine: pmap_kenter_quick
1124 * Similar to pmap_kenter(), except we only invalidate the
1125 * mapping on the current CPU.
1128 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
1133 npte = pa | PG_RW | PG_V | pgeflag;
1136 cpu_invlpg((void *)va);
1140 pmap_kenter_sync(vm_offset_t va)
1142 pmap_inval_info info;
1144 pmap_inval_init(&info);
1145 pmap_inval_interlock(&info, &kernel_pmap, va);
1146 pmap_inval_deinterlock(&info, &kernel_pmap);
1147 pmap_inval_done(&info);
1151 pmap_kenter_sync_quick(vm_offset_t va)
1153 cpu_invlpg((void *)va);
1157 * remove a page from the kernel pagetables
1160 pmap_kremove(vm_offset_t va)
1163 pmap_inval_info info;
1165 pmap_inval_init(&info);
1167 pmap_inval_interlock(&info, &kernel_pmap, va);
1169 pmap_inval_deinterlock(&info, &kernel_pmap);
1170 pmap_inval_done(&info);
1174 pmap_kremove_quick(vm_offset_t va)
1179 cpu_invlpg((void *)va);
1183 * XXX these need to be recoded. They are not used in any critical path.
1186 pmap_kmodify_rw(vm_offset_t va)
1188 atomic_set_long(vtopte(va), PG_RW);
1189 cpu_invlpg((void *)va);
1193 pmap_kmodify_nc(vm_offset_t va)
1195 atomic_set_long(vtopte(va), PG_N);
1196 cpu_invlpg((void *)va);
1200 * Used to map a range of physical addresses into kernel virtual
1201 * address space during the low level boot, typically to map the
1202 * dump bitmap, message buffer, and vm_page_array.
1204 * These mappings are typically made at some pointer after the end of the
1207 * We could return PHYS_TO_DMAP(start) here and not allocate any
1208 * via (*virtp), but then kmem from userland and kernel dumps won't
1209 * have access to the related pointers.
1212 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
1215 vm_offset_t va_start;
1217 /*return PHYS_TO_DMAP(start);*/
1222 while (start < end) {
1223 pmap_kenter_quick(va, start);
1233 * Add a list of wired pages to the kva
1234 * this routine is only used for temporary
1235 * kernel mappings that do not need to have
1236 * page modification or references recorded.
1237 * Note that old mappings are simply written
1238 * over. The page *must* be wired.
1241 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1245 end_va = va + count * PAGE_SIZE;
1247 while (va < end_va) {
1251 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1252 cpu_invlpg((void *)va);
1260 * This routine jerks page mappings from the
1261 * kernel -- it is meant only for temporary mappings.
1263 * MPSAFE, INTERRUPT SAFE (cluster callback)
1266 pmap_qremove(vm_offset_t va, int count)
1270 end_va = va + count * PAGE_SIZE;
1272 while (va < end_va) {
1277 cpu_invlpg((void *)va);
1284 * Create a new thread and optionally associate it with a (new) process.
1285 * NOTE! the new thread's cpu may not equal the current cpu.
1288 pmap_init_thread(thread_t td)
1290 /* enforce pcb placement & alignment */
1291 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1292 td->td_pcb = (struct pcb *)((intptr_t)td->td_pcb & ~(intptr_t)0xF);
1293 td->td_savefpu = &td->td_pcb->pcb_save;
1294 td->td_sp = (char *)td->td_pcb; /* no -16 */
1298 * This routine directly affects the fork perf for a process.
1301 pmap_init_proc(struct proc *p)
1306 * Dispose the UPAGES for a process that has exited.
1307 * This routine directly impacts the exit perf of a process.
1310 pmap_dispose_proc(struct proc *p)
1312 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1316 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1317 * it, and IdlePTD, represents the template used to update all other pmaps.
1319 * On architectures where the kernel pmap is not integrated into the user
1320 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1321 * kernel_pmap should be used to directly access the kernel_pmap.
1324 pmap_pinit0(struct pmap *pmap)
1326 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1328 pmap->pm_active = 0;
1329 pmap->pm_pvhint = NULL;
1330 RB_INIT(&pmap->pm_pvroot);
1331 spin_init(&pmap->pm_spin);
1332 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1333 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1337 * Initialize a preallocated and zeroed pmap structure,
1338 * such as one in a vmspace structure.
1341 pmap_pinit(struct pmap *pmap)
1346 * Misc initialization
1349 pmap->pm_active = 0;
1350 pmap->pm_pvhint = NULL;
1351 if (pmap->pm_pmlpv == NULL) {
1352 RB_INIT(&pmap->pm_pvroot);
1353 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1354 spin_init(&pmap->pm_spin);
1355 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1359 * No need to allocate page table space yet but we do need a valid
1360 * page directory table.
1362 if (pmap->pm_pml4 == NULL) {
1364 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1368 * Allocate the page directory page, which wires it even though
1369 * it isn't being entered into some higher level page table (it
1370 * being the highest level). If one is already cached we don't
1371 * have to do anything.
1373 if ((pv = pmap->pm_pmlpv) == NULL) {
1374 pv = pmap_allocpte(pmap, pmap_pml4_pindex(), NULL);
1375 pmap->pm_pmlpv = pv;
1376 pmap_kenter((vm_offset_t)pmap->pm_pml4,
1377 VM_PAGE_TO_PHYS(pv->pv_m));
1379 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1380 pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1382 /* install self-referential address mapping entry */
1383 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(pv->pv_m) |
1384 PG_V | PG_RW | PG_A | PG_M;
1386 KKASSERT(pv->pv_m->flags & PG_MAPPED);
1387 KKASSERT(pv->pv_m->flags & PG_WRITEABLE);
1392 * Clean up a pmap structure so it can be physically freed. This routine
1393 * is called by the vmspace dtor function. A great deal of pmap data is
1394 * left passively mapped to improve vmspace management so we have a bit
1395 * of cleanup work to do here.
1398 pmap_puninit(pmap_t pmap)
1403 KKASSERT(pmap->pm_active == 0);
1404 if ((pv = pmap->pm_pmlpv) != NULL) {
1405 if (pv_hold_try(pv) == 0)
1407 p = pmap_remove_pv_page(pv, 1);
1409 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1410 vm_page_busy_wait(p, FALSE, "pgpun");
1412 KKASSERT(p->flags & (PG_FICTITIOUS|PG_UNMANAGED));
1413 vm_page_unwire(p, 0);
1414 vm_page_flag_clear(p, PG_MAPPED | PG_WRITEABLE);
1417 * XXX eventually clean out PML4 static entries and
1418 * use vm_page_free_zero()
1421 pmap->pm_pmlpv = NULL;
1423 if (pmap->pm_pml4) {
1424 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1425 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1426 pmap->pm_pml4 = NULL;
1428 KKASSERT(pmap->pm_stats.resident_count == 0);
1429 KKASSERT(pmap->pm_stats.wired_count == 0);
1433 * Wire in kernel global address entries. To avoid a race condition
1434 * between pmap initialization and pmap_growkernel, this procedure
1435 * adds the pmap to the master list (which growkernel scans to update),
1436 * then copies the template.
1439 pmap_pinit2(struct pmap *pmap)
1442 * XXX copies current process, does not fill in MPPTDI
1444 spin_lock(&pmap_spin);
1445 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1446 spin_unlock(&pmap_spin);
1450 * This routine is called when various levels in the page table need to
1451 * be populated. This routine cannot fail.
1453 * This function returns two locked pv_entry's, one representing the
1454 * requested pv and one representing the requested pv's parent pv. If
1455 * the pv did not previously exist it will be mapped into its parent
1456 * and wired, otherwise no additional wire count will be added.
1460 pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, pv_entry_t *pvpp)
1465 vm_pindex_t pt_pindex;
1470 * If the pv already exists and we aren't being asked for the
1471 * parent page table page we can just return it. A locked+held pv
1474 pv = pv_alloc(pmap, ptepindex, &isnew);
1475 if (isnew == 0 && pvpp == NULL)
1479 * This is a new PV, we have to resolve its parent page table and
1480 * add an additional wiring to the page if necessary.
1484 * Special case terminal PVs. These are not page table pages so
1485 * no vm_page is allocated (the caller supplied the vm_page). If
1486 * pvpp is non-NULL we are being asked to also removed the pt_pv
1489 * Note that pt_pv's are only returned for user VAs. We assert that
1490 * a pt_pv is not being requested for kernel VAs.
1492 if (ptepindex < pmap_pt_pindex(0)) {
1493 if (ptepindex >= NUPTE_USER)
1494 KKASSERT(pvpp == NULL);
1496 KKASSERT(pvpp != NULL);
1498 pt_pindex = NUPTE_TOTAL + (ptepindex >> NPTEPGSHIFT);
1499 pvp = pmap_allocpte(pmap, pt_pindex, NULL);
1501 vm_page_wire_quick(pvp->pv_m);
1510 * Non-terminal PVs allocate a VM page to represent the page table,
1511 * so we have to resolve pvp and calculate ptepindex for the pvp
1512 * and then for the page table entry index in the pvp for
1515 if (ptepindex < pmap_pd_pindex(0)) {
1517 * pv is PT, pvp is PD
1519 ptepindex = (ptepindex - pmap_pt_pindex(0)) >> NPDEPGSHIFT;
1520 ptepindex += NUPTE_TOTAL + NUPT_TOTAL;
1521 pvp = pmap_allocpte(pmap, ptepindex, NULL);
1528 ptepindex = pv->pv_pindex - pmap_pt_pindex(0);
1529 ptepindex &= ((1ul << NPDEPGSHIFT) - 1);
1530 } else if (ptepindex < pmap_pdp_pindex(0)) {
1532 * pv is PD, pvp is PDP
1534 ptepindex = (ptepindex - pmap_pd_pindex(0)) >> NPDPEPGSHIFT;
1535 ptepindex += NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL;
1536 pvp = pmap_allocpte(pmap, ptepindex, NULL);
1543 ptepindex = pv->pv_pindex - pmap_pd_pindex(0);
1544 ptepindex &= ((1ul << NPDPEPGSHIFT) - 1);
1545 } else if (ptepindex < pmap_pml4_pindex()) {
1547 * pv is PDP, pvp is the root pml4 table
1549 pvp = pmap_allocpte(pmap, pmap_pml4_pindex(), NULL);
1556 ptepindex = pv->pv_pindex - pmap_pdp_pindex(0);
1557 ptepindex &= ((1ul << NPML4EPGSHIFT) - 1);
1560 * pv represents the top-level PML4, there is no parent.
1568 * This code is only reached if isnew is TRUE and this is not a
1569 * terminal PV. We need to allocate a vm_page for the page table
1570 * at this level and enter it into the parent page table.
1572 * page table pages are marked PG_WRITEABLE and PG_MAPPED.
1575 m = vm_page_alloc(NULL, pv->pv_pindex,
1576 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM |
1577 VM_ALLOC_INTERRUPT);
1582 vm_page_spin_lock(m);
1583 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1585 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1586 vm_page_spin_unlock(m);
1587 vm_page_unmanage(m); /* m must be spinunlocked */
1589 if ((m->flags & PG_ZERO) == 0) {
1590 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1594 pmap_page_assertzero(VM_PAGE_TO_PHYS(m));
1597 m->valid = VM_PAGE_BITS_ALL;
1598 vm_page_flag_clear(m, PG_ZERO);
1599 vm_page_wire(m); /* wire for mapping in parent */
1602 * Wire the page into pvp, bump the wire-count for pvp's page table
1603 * page. Bump the resident_count for the pmap. There is no pvp
1604 * for the top level, address the pm_pml4[] array directly.
1606 * If the caller wants the parent we return it, otherwise
1607 * we just put it away.
1609 * No interlock is needed for pte 0 -> non-zero.
1612 vm_page_wire_quick(pvp->pv_m);
1613 ptep = pv_pte_lookup(pvp, ptepindex);
1614 KKASSERT((*ptep & PG_V) == 0);
1615 *ptep = VM_PAGE_TO_PHYS(m) | (PG_U | PG_RW | PG_V |
1628 * Release any resources held by the given physical map.
1630 * Called when a pmap initialized by pmap_pinit is being released. Should
1631 * only be called if the map contains no valid mappings.
1633 * Caller must hold pmap->pm_token
1635 struct pmap_release_info {
1640 static int pmap_release_callback(pv_entry_t pv, void *data);
1643 pmap_release(struct pmap *pmap)
1645 struct pmap_release_info info;
1647 KASSERT(pmap->pm_active == 0,
1648 ("pmap still active! %016jx", (uintmax_t)pmap->pm_active));
1649 #if defined(DIAGNOSTIC)
1650 if (object->ref_count != 1)
1651 panic("pmap_release: pteobj reference count != 1");
1654 spin_lock(&pmap_spin);
1655 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1656 spin_unlock(&pmap_spin);
1659 * Pull pv's off the RB tree in order from low to high and release
1665 spin_lock(&pmap->pm_spin);
1666 RB_SCAN(pv_entry_rb_tree, &pmap->pm_pvroot, NULL,
1667 pmap_release_callback, &info);
1668 spin_unlock(&pmap->pm_spin);
1669 } while (info.retry);
1673 * One resident page (the pml4 page) should remain.
1674 * No wired pages should remain.
1676 KKASSERT(pmap->pm_stats.resident_count == 1);
1677 KKASSERT(pmap->pm_stats.wired_count == 0);
1681 pmap_release_callback(pv_entry_t pv, void *data)
1683 struct pmap_release_info *info = data;
1684 pmap_t pmap = info->pmap;
1687 if (pv_hold_try(pv)) {
1688 spin_unlock(&pmap->pm_spin);
1690 spin_unlock(&pmap->pm_spin);
1692 if (pv->pv_pmap != pmap) {
1694 spin_lock(&pmap->pm_spin);
1701 * The pmap is currently not spinlocked, pv is held+locked.
1702 * Remove the pv's page from its parent's page table. The
1703 * parent's page table page's wire_count will be decremented.
1705 pmap_remove_pv_pte(pv, NULL, NULL);
1708 * Terminal pvs are unhooked from their vm_pages. Because
1709 * terminal pages aren't page table pages they aren't wired
1710 * by us, so we have to be sure not to unwire them either.
1712 if (pv->pv_pindex < pmap_pt_pindex(0)) {
1713 pmap_remove_pv_page(pv, 0);
1718 * We leave the top-level page table page cached, wired, and
1719 * mapped in the pmap until the dtor function (pmap_puninit())
1722 * Since we are leaving the top-level pv intact we need
1723 * to break out of what would otherwise be an infinite loop.
1725 if (pv->pv_pindex == pmap_pml4_pindex()) {
1727 spin_lock(&pmap->pm_spin);
1732 * For page table pages (other than the top-level page),
1733 * remove and free the vm_page. The representitive mapping
1734 * removed above by pmap_remove_pv_pte() did not undo the
1735 * last wire_count so we have to do that as well.
1737 p = pmap_remove_pv_page(pv, 1);
1738 vm_page_busy_wait(p, FALSE, "pmaprl");
1740 if (p->wire_count != 1) {
1741 kprintf("p->wire_count was %016lx %d\n",
1742 pv->pv_pindex, p->wire_count);
1744 KKASSERT(p->wire_count == 1);
1745 KKASSERT(p->flags & PG_UNMANAGED);
1747 vm_page_unwire(p, 0);
1748 KKASSERT(p->wire_count == 0);
1749 /* JG eventually revert to using vm_page_free_zero() */
1753 spin_lock(&pmap->pm_spin);
1758 * This function will remove the pte associated with a pv from its parent.
1759 * Terminal pv's are supported. The removal will be interlocked if info
1760 * is non-NULL. The caller must dispose of pv instead of just unlocking
1763 * The wire count will be dropped on the parent page table. The wire
1764 * count on the page being removed (pv->pv_m) from the parent page table
1765 * is NOT touched. Note that terminal pages will not have any additional
1766 * wire counts while page table pages will have at least one representing
1767 * the mapping, plus others representing sub-mappings.
1769 * NOTE: Cannot be called on kernel page table pages, only KVM terminal
1770 * pages and user page table and terminal pages.
1772 * The pv must be locked.
1774 * XXX must lock parent pv's if they exist to remove pte XXX
1778 pmap_remove_pv_pte(pv_entry_t pv, pv_entry_t pvp, struct pmap_inval_info *info)
1780 vm_pindex_t ptepindex = pv->pv_pindex;
1781 pmap_t pmap = pv->pv_pmap;
1787 if (ptepindex == pmap_pml4_pindex()) {
1789 * We are the top level pml4 table, there is no parent.
1791 p = pmap->pm_pmlpv->pv_m;
1792 } else if (ptepindex >= pmap_pdp_pindex(0)) {
1794 * Remove a PDP page from the pml4e. This can only occur
1795 * with user page tables. We do not have to lock the
1796 * pml4 PV so just ignore pvp.
1798 vm_pindex_t pml4_pindex;
1799 vm_pindex_t pdp_index;
1802 pdp_index = ptepindex - pmap_pdp_pindex(0);
1804 pml4_pindex = pmap_pml4_pindex();
1805 pvp = pv_get(pv->pv_pmap, pml4_pindex);
1808 pdp = &pmap->pm_pml4[pdp_index & ((1ul << NPML4EPGSHIFT) - 1)];
1809 KKASSERT((*pdp & PG_V) != 0);
1810 p = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1812 KKASSERT(info == NULL);
1813 } else if (ptepindex >= pmap_pd_pindex(0)) {
1815 * Remove a PD page from the pdp
1817 vm_pindex_t pdp_pindex;
1818 vm_pindex_t pd_index;
1821 pd_index = ptepindex - pmap_pd_pindex(0);
1824 pdp_pindex = NUPTE_TOTAL + NUPT_TOTAL + NUPD_TOTAL +
1825 (pd_index >> NPML4EPGSHIFT);
1826 pvp = pv_get(pv->pv_pmap, pdp_pindex);
1829 pd = pv_pte_lookup(pvp, pd_index & ((1ul << NPDPEPGSHIFT) - 1));
1830 KKASSERT((*pd & PG_V) != 0);
1831 p = PHYS_TO_VM_PAGE(*pd & PG_FRAME);
1833 KKASSERT(info == NULL);
1834 } else if (ptepindex >= pmap_pt_pindex(0)) {
1836 * Remove a PT page from the pd
1838 vm_pindex_t pd_pindex;
1839 vm_pindex_t pt_index;
1842 pt_index = ptepindex - pmap_pt_pindex(0);
1845 pd_pindex = NUPTE_TOTAL + NUPT_TOTAL +
1846 (pt_index >> NPDPEPGSHIFT);
1847 pvp = pv_get(pv->pv_pmap, pd_pindex);
1850 pt = pv_pte_lookup(pvp, pt_index & ((1ul << NPDPEPGSHIFT) - 1));
1851 KKASSERT((*pt & PG_V) != 0);
1852 p = PHYS_TO_VM_PAGE(*pt & PG_FRAME);
1854 KKASSERT(info == NULL);
1857 * Remove a PTE from the PT page
1859 * NOTE: pv's must be locked bottom-up to avoid deadlocking.
1860 * pv is a pte_pv so we can safely lock pt_pv.
1862 vm_pindex_t pt_pindex;
1867 pt_pindex = ptepindex >> NPTEPGSHIFT;
1868 va = (vm_offset_t)ptepindex << PAGE_SHIFT;
1870 if (ptepindex >= NUPTE_USER) {
1871 ptep = vtopte(ptepindex << PAGE_SHIFT);
1872 KKASSERT(pvp == NULL);
1875 pt_pindex = NUPTE_TOTAL +
1876 (ptepindex >> NPDPEPGSHIFT);
1877 pvp = pv_get(pv->pv_pmap, pt_pindex);
1880 ptep = pv_pte_lookup(pvp, ptepindex &
1881 ((1ul << NPDPEPGSHIFT) - 1));
1885 pmap_inval_interlock(info, pmap, va);
1886 pte = pte_load_clear(ptep);
1888 pmap_inval_deinterlock(info, pmap);
1891 * Now update the vm_page_t
1893 if ((pte & (PG_MANAGED|PG_V)) != (PG_MANAGED|PG_V)) {
1894 kprintf("remove_pte badpte %016lx %016lx %d\n",
1896 pv->pv_pindex < pmap_pt_pindex(0));
1898 /*KKASSERT((pte & (PG_MANAGED|PG_V)) == (PG_MANAGED|PG_V));*/
1899 p = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1902 if (pmap_track_modified(ptepindex))
1906 vm_page_flag_set(p, PG_REFERENCED);
1909 atomic_add_long(&pmap->pm_stats.wired_count, -1);
1911 cpu_invlpg((void *)va);
1915 * Unwire the parent page table page. The wire_count cannot go below
1916 * 1 here because the parent page table page is itself still mapped.
1918 * XXX remove the assertions later.
1920 KKASSERT(pv->pv_m == p);
1921 if (pvp && vm_page_unwire_quick(pvp->pv_m))
1922 panic("pmap_remove_pv_pte: Insufficient wire_count");
1930 pmap_remove_pv_page(pv_entry_t pv, int holdpg)
1938 vm_page_spin_lock(m);
1940 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1943 atomic_add_int(&m->object->agg_pv_list_count, -1);
1945 if (TAILQ_EMPTY(&m->md.pv_list))
1946 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1947 vm_page_spin_unlock(m);
1954 * Grow the number of kernel page table entries, if needed.
1956 * This routine is always called to validate any address space
1957 * beyond KERNBASE (for kldloads). kernel_vm_end only governs the address
1958 * space below KERNBASE.
1961 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
1964 vm_offset_t ptppaddr;
1966 pd_entry_t *pt, newpt;
1968 int update_kernel_vm_end;
1971 * bootstrap kernel_vm_end on first real VM use
1973 if (kernel_vm_end == 0) {
1974 kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
1976 while ((*pmap_pt(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1977 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1978 ~(PAGE_SIZE * NPTEPG - 1);
1980 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1981 kernel_vm_end = kernel_map.max_offset;
1988 * Fill in the gaps. kernel_vm_end is only adjusted for ranges
1989 * below KERNBASE. Ranges above KERNBASE are kldloaded and we
1990 * do not want to force-fill 128G worth of page tables.
1992 if (kstart < KERNBASE) {
1993 if (kstart > kernel_vm_end)
1994 kstart = kernel_vm_end;
1995 KKASSERT(kend <= KERNBASE);
1996 update_kernel_vm_end = 1;
1998 update_kernel_vm_end = 0;
2001 kstart = rounddown2(kstart, PAGE_SIZE * NPTEPG);
2002 kend = roundup2(kend, PAGE_SIZE * NPTEPG);
2004 if (kend - 1 >= kernel_map.max_offset)
2005 kend = kernel_map.max_offset;
2007 while (kstart < kend) {
2008 pt = pmap_pt(&kernel_pmap, kstart);
2010 /* We need a new PDP entry */
2011 nkpg = vm_page_alloc(NULL, nkpt,
2014 VM_ALLOC_INTERRUPT);
2016 panic("pmap_growkernel: no memory to grow "
2019 paddr = VM_PAGE_TO_PHYS(nkpg);
2020 if ((nkpg->flags & PG_ZERO) == 0)
2021 pmap_zero_page(paddr);
2022 vm_page_flag_clear(nkpg, PG_ZERO);
2023 newpd = (pdp_entry_t)
2024 (paddr | PG_V | PG_RW | PG_A | PG_M);
2025 *pmap_pd(&kernel_pmap, kstart) = newpd;
2027 continue; /* try again */
2029 if ((*pt & PG_V) != 0) {
2030 kstart = (kstart + PAGE_SIZE * NPTEPG) &
2031 ~(PAGE_SIZE * NPTEPG - 1);
2032 if (kstart - 1 >= kernel_map.max_offset) {
2033 kstart = kernel_map.max_offset;
2040 * This index is bogus, but out of the way
2042 nkpg = vm_page_alloc(NULL, nkpt,
2045 VM_ALLOC_INTERRUPT);
2047 panic("pmap_growkernel: no memory to grow kernel");
2050 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2051 pmap_zero_page(ptppaddr);
2052 vm_page_flag_clear(nkpg, PG_ZERO);
2053 newpt = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2054 *pmap_pt(&kernel_pmap, kstart) = newpt;
2057 kstart = (kstart + PAGE_SIZE * NPTEPG) &
2058 ~(PAGE_SIZE * NPTEPG - 1);
2060 if (kstart - 1 >= kernel_map.max_offset) {
2061 kstart = kernel_map.max_offset;
2067 * Only update kernel_vm_end for areas below KERNBASE.
2069 if (update_kernel_vm_end && kernel_vm_end < kstart)
2070 kernel_vm_end = kstart;
2074 * Retire the given physical map from service.
2075 * Should only be called if the map contains
2076 * no valid mappings.
2079 pmap_destroy(pmap_t pmap)
2086 lwkt_gettoken(&pmap->pm_token);
2087 count = --pmap->pm_count;
2089 pmap_release(pmap); /* eats pm_token */
2090 panic("destroying a pmap is not yet implemented");
2092 lwkt_reltoken(&pmap->pm_token);
2096 * Add a reference to the specified pmap.
2099 pmap_reference(pmap_t pmap)
2102 lwkt_gettoken(&pmap->pm_token);
2104 lwkt_reltoken(&pmap->pm_token);
2108 /***************************************************
2109 * page management routines.
2110 ***************************************************/
2113 * Hold a pv without locking it
2116 pv_hold(pv_entry_t pv)
2120 if (atomic_cmpset_int(&pv->pv_hold, 0, 1))
2124 count = pv->pv_hold;
2126 if (atomic_cmpset_int(&pv->pv_hold, count, count + 1))
2133 * Hold a pv_entry, preventing its destruction. TRUE is returned if the pv
2134 * was successfully locked, FALSE if it wasn't. The caller must dispose of
2137 * Either the pmap->pm_spin or the related vm_page_spin (if traversing a
2138 * pv list via its page) must be held by the caller.
2141 _pv_hold_try(pv_entry_t pv PMAP_DEBUG_DECL)
2145 if (atomic_cmpset_int(&pv->pv_hold, 0, PV_HOLD_LOCKED | 1)) {
2148 pv->pv_line = lineno;
2154 count = pv->pv_hold;
2156 if ((count & PV_HOLD_LOCKED) == 0) {
2157 if (atomic_cmpset_int(&pv->pv_hold, count,
2158 (count + 1) | PV_HOLD_LOCKED)) {
2161 pv->pv_line = lineno;
2166 if (atomic_cmpset_int(&pv->pv_hold, count, count + 1))
2174 * Drop a previously held pv_entry which could not be locked, allowing its
2177 * Must not be called with a spinlock held as we might zfree() the pv if it
2178 * is no longer associated with a pmap and this was the last hold count.
2181 pv_drop(pv_entry_t pv)
2185 if (atomic_cmpset_int(&pv->pv_hold, 1, 0)) {
2186 if (pv->pv_pmap == NULL)
2192 count = pv->pv_hold;
2194 KKASSERT((count & PV_HOLD_MASK) > 0);
2195 KKASSERT((count & (PV_HOLD_LOCKED | PV_HOLD_MASK)) !=
2196 (PV_HOLD_LOCKED | 1));
2197 if (atomic_cmpset_int(&pv->pv_hold, count, count - 1)) {
2198 if (count == 1 && pv->pv_pmap == NULL)
2207 * Find or allocate the requested PV entry, returning a locked pv
2211 _pv_alloc(pmap_t pmap, vm_pindex_t pindex, int *isnew PMAP_DEBUG_DECL)
2214 pv_entry_t pnew = NULL;
2216 spin_lock(&pmap->pm_spin);
2218 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex) {
2219 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot,
2224 spin_unlock(&pmap->pm_spin);
2225 pnew = zalloc(pvzone);
2226 spin_lock(&pmap->pm_spin);
2229 pnew->pv_pmap = pmap;
2230 pnew->pv_pindex = pindex;
2231 pnew->pv_hold = PV_HOLD_LOCKED | 1;
2233 pnew->pv_func = func;
2234 pnew->pv_line = lineno;
2236 pv_entry_rb_tree_RB_INSERT(&pmap->pm_pvroot, pnew);
2237 atomic_add_long(&pmap->pm_stats.resident_count, 1);
2238 spin_unlock(&pmap->pm_spin);
2243 spin_unlock(&pmap->pm_spin);
2244 zfree(pvzone, pnew);
2246 spin_lock(&pmap->pm_spin);
2249 if (_pv_hold_try(pv PMAP_DEBUG_COPY)) {
2250 spin_unlock(&pmap->pm_spin);
2254 spin_unlock(&pmap->pm_spin);
2255 _pv_lock(pv PMAP_DEBUG_COPY);
2256 if (pv->pv_pmap == pmap && pv->pv_pindex == pindex) {
2261 spin_lock(&pmap->pm_spin);
2268 * Find the requested PV entry, returning a locked+held pv or NULL
2272 _pv_get(pmap_t pmap, vm_pindex_t pindex PMAP_DEBUG_DECL)
2276 spin_lock(&pmap->pm_spin);
2281 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex) {
2282 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot,
2286 spin_unlock(&pmap->pm_spin);
2289 if (_pv_hold_try(pv PMAP_DEBUG_COPY)) {
2290 pv_cache(pv, pindex);
2291 spin_unlock(&pmap->pm_spin);
2294 spin_unlock(&pmap->pm_spin);
2295 _pv_lock(pv PMAP_DEBUG_COPY);
2296 if (pv->pv_pmap == pmap && pv->pv_pindex == pindex)
2299 spin_lock(&pmap->pm_spin);
2304 * Lookup, hold, and attempt to lock (pmap,pindex).
2306 * If the entry does not exist NULL is returned and *errorp is set to 0
2308 * If the entry exists and could be successfully locked it is returned and
2309 * errorp is set to 0.
2311 * If the entry exists but could NOT be successfully locked it is returned
2312 * held and *errorp is set to 1.
2316 pv_get_try(pmap_t pmap, vm_pindex_t pindex, int *errorp)
2320 spin_lock(&pmap->pm_spin);
2321 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex)
2322 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pindex);
2324 spin_unlock(&pmap->pm_spin);
2328 if (pv_hold_try(pv)) {
2329 pv_cache(pv, pindex);
2330 spin_unlock(&pmap->pm_spin);
2332 return(pv); /* lock succeeded */
2334 spin_unlock(&pmap->pm_spin);
2336 return (pv); /* lock failed */
2340 * Find the requested PV entry, returning a held pv or NULL
2344 pv_find(pmap_t pmap, vm_pindex_t pindex)
2348 spin_lock(&pmap->pm_spin);
2350 if ((pv = pmap->pm_pvhint) == NULL || pv->pv_pindex != pindex)
2351 pv = pv_entry_rb_tree_RB_LOOKUP(&pmap->pm_pvroot, pindex);
2353 spin_unlock(&pmap->pm_spin);
2357 pv_cache(pv, pindex);
2358 spin_unlock(&pmap->pm_spin);
2363 * Lock a held pv, keeping the hold count
2367 _pv_lock(pv_entry_t pv PMAP_DEBUG_DECL)
2372 count = pv->pv_hold;
2374 if ((count & PV_HOLD_LOCKED) == 0) {
2375 if (atomic_cmpset_int(&pv->pv_hold, count,
2376 count | PV_HOLD_LOCKED)) {
2379 pv->pv_line = lineno;
2385 tsleep_interlock(pv, 0);
2386 if (atomic_cmpset_int(&pv->pv_hold, count,
2387 count | PV_HOLD_WAITING)) {
2389 kprintf("pv waiting on %s:%d\n",
2390 pv->pv_func, pv->pv_line);
2392 tsleep(pv, PINTERLOCKED, "pvwait", hz);
2399 * Unlock a held and locked pv, keeping the hold count.
2403 pv_unlock(pv_entry_t pv)
2407 if (atomic_cmpset_int(&pv->pv_hold, PV_HOLD_LOCKED | 1, 1))
2411 count = pv->pv_hold;
2413 KKASSERT((count & (PV_HOLD_LOCKED|PV_HOLD_MASK)) >=
2414 (PV_HOLD_LOCKED | 1));
2415 if (atomic_cmpset_int(&pv->pv_hold, count,
2417 ~(PV_HOLD_LOCKED | PV_HOLD_WAITING))) {
2418 if (count & PV_HOLD_WAITING)
2426 * Unlock and drop a pv. If the pv is no longer associated with a pmap
2427 * and the hold count drops to zero we will free it.
2429 * Caller should not hold any spin locks. We are protected from hold races
2430 * by virtue of holds only occuring only with a pmap_spin or vm_page_spin
2431 * lock held. A pv cannot be located otherwise.
2435 pv_put(pv_entry_t pv)
2437 if (atomic_cmpset_int(&pv->pv_hold, PV_HOLD_LOCKED | 1, 0)) {
2438 if (pv->pv_pmap == NULL)
2447 * Unlock, drop, and free a pv, destroying it. The pv is removed from its
2448 * pmap. Any pte operations must have already been completed.
2452 pv_free(pv_entry_t pv)
2456 KKASSERT(pv->pv_m == NULL);
2457 if ((pmap = pv->pv_pmap) != NULL) {
2458 spin_lock(&pmap->pm_spin);
2459 pv_entry_rb_tree_RB_REMOVE(&pmap->pm_pvroot, pv);
2460 if (pmap->pm_pvhint == pv)
2461 pmap->pm_pvhint = NULL;
2462 atomic_add_long(&pmap->pm_stats.resident_count, -1);
2465 spin_unlock(&pmap->pm_spin);
2471 * This routine is very drastic, but can save the system
2479 static int warningdone=0;
2481 if (pmap_pagedaemon_waken == 0)
2483 pmap_pagedaemon_waken = 0;
2484 if (warningdone < 5) {
2485 kprintf("pmap_collect: collecting pv entries -- "
2486 "suggest increasing PMAP_SHPGPERPROC\n");
2490 for (i = 0; i < vm_page_array_size; i++) {
2491 m = &vm_page_array[i];
2492 if (m->wire_count || m->hold_count)
2494 if (vm_page_busy_try(m, TRUE) == 0) {
2495 if (m->wire_count == 0 && m->hold_count == 0) {
2504 * Scan the pmap for active page table entries and issue a callback.
2505 * The callback must dispose of pte_pv.
2507 * NOTE: Unmanaged page table entries will not have a pte_pv
2509 * NOTE: Kernel page table entries will not have a pt_pv. That is, wiring
2510 * counts are not tracked in kernel page table pages.
2512 * It is assumed that the start and end are properly rounded to the page size.
2515 pmap_scan(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva,
2516 void (*func)(pmap_t, struct pmap_inval_info *,
2517 pv_entry_t, pv_entry_t, vm_offset_t,
2518 pt_entry_t *, void *),
2521 pv_entry_t pdp_pv; /* A page directory page PV */
2522 pv_entry_t pd_pv; /* A page directory PV */
2523 pv_entry_t pt_pv; /* A page table PV */
2524 pv_entry_t pte_pv; /* A page table entry PV */
2526 vm_offset_t va_next;
2527 struct pmap_inval_info info;
2534 * Hold the token for stability; if the pmap is empty we have nothing
2537 lwkt_gettoken(&pmap->pm_token);
2539 if (pmap->pm_stats.resident_count == 0) {
2540 lwkt_reltoken(&pmap->pm_token);
2545 pmap_inval_init(&info);
2548 * Special handling for removing one page, which is a very common
2549 * operation (it is?).
2550 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4
2552 if (sva + PAGE_SIZE == eva) {
2553 if (sva >= VM_MAX_USER_ADDRESS) {
2555 * Kernel mappings do not track wire counts on
2559 pte_pv = pv_get(pmap, pmap_pte_pindex(sva));
2563 * User mappings may or may not have a pte_pv but
2564 * will always have a pt_pv if the page is present.
2566 pte_pv = pv_get(pmap, pmap_pte_pindex(sva));
2567 pt_pv = pv_get(pmap, pmap_pt_pindex(sva));
2568 if (pt_pv == NULL) {
2569 KKASSERT(pte_pv == NULL);
2572 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(sva));
2576 * Unlike the pv_find() case below we actually
2577 * acquired a locked pv in this case so any
2578 * race should have been resolved. It is expected
2581 KKASSERT(pte_pv == NULL);
2582 } else if (pte_pv) {
2583 KKASSERT((*ptep & (PG_MANAGED|PG_V)) ==
2585 func(pmap, &info, pte_pv, pt_pv, sva, ptep, arg);
2587 KKASSERT((*ptep & (PG_MANAGED|PG_V)) ==
2589 func(pmap, &info, pte_pv, pt_pv, sva, ptep, arg);
2594 pmap_inval_done(&info);
2595 lwkt_reltoken(&pmap->pm_token);
2600 * NOTE: kernel mappings do not track page table pages, only
2603 * NOTE: Locks must be ordered bottom-up. pte,pt,pd,pdp,pml4.
2604 * However, for the scan to be efficient we try to
2605 * cache items top-down.
2611 for (; sva < eva; sva = va_next) {
2613 if (sva >= VM_MAX_USER_ADDRESS) {
2624 if (pdp_pv == NULL) {
2625 pdp_pv = pv_get(pmap, pmap_pdp_pindex(sva));
2626 } else if (pdp_pv->pv_pindex != pmap_pdp_pindex(sva)) {
2628 pdp_pv = pv_get(pmap, pmap_pdp_pindex(sva));
2630 if (pdp_pv == NULL) {
2631 va_next = (sva + NBPML4) & ~PML4MASK;
2640 if (pd_pv == NULL) {
2645 pd_pv = pv_get(pmap, pmap_pd_pindex(sva));
2646 } else if (pd_pv->pv_pindex != pmap_pd_pindex(sva)) {
2652 pd_pv = pv_get(pmap, pmap_pd_pindex(sva));
2654 if (pd_pv == NULL) {
2655 va_next = (sva + NBPDP) & ~PDPMASK;
2665 if (pt_pv == NULL) {
2674 pt_pv = pv_get(pmap, pmap_pt_pindex(sva));
2675 } else if (pt_pv->pv_pindex != pmap_pt_pindex(sva)) {
2685 pt_pv = pv_get(pmap, pmap_pt_pindex(sva));
2689 * We will scan or skip a page table page so adjust va_next
2692 if (pt_pv == NULL) {
2693 va_next = (sva + NBPDR) & ~PDRMASK;
2700 * From this point in the loop testing pt_pv for non-NULL
2701 * means we are in UVM, else if it is NULL we are in KVM.
2704 va_next = (sva + NBPDR) & ~PDRMASK;
2709 * Limit our scan to either the end of the va represented
2710 * by the current page table page, or to the end of the
2711 * range being removed.
2713 * Scan the page table for pages. Some pages may not be
2714 * managed (might not have a pv_entry).
2716 * There is no page table management for kernel pages so
2717 * pt_pv will be NULL in that case, but otherwise pt_pv
2718 * is non-NULL, locked, and referenced.
2724 * At this point a non-NULL pt_pv means a UVA, and a NULL
2725 * pt_pv means a KVA.
2728 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(sva));
2732 while (sva < va_next) {
2734 * NOTE: It is possible for an empty ptep to
2735 * race a pv removal by some other means
2736 * (typically via the vm_page_t).
2738 * For now debug the situation by locking
2739 * the potentially conflicting pv and
2744 pte_pv = pv_find(pmap, pmap_pte_pindex(sva));
2745 if (pte_pv == NULL) {
2752 * Possible race against another thread
2755 kprintf("pmap_scan: caught race func %p", func);
2757 pv_put(pt_pv); /* must be non-NULL */
2760 pv_lock(pte_pv); /* safe to block now */
2761 if (pte_pv->pv_pmap) {
2762 kprintf(", uh oh, pte_pv still good\n");
2763 panic("unexpected non-NULL pte_pv %p",
2766 kprintf(", resolved ok\n");
2774 * We need a locked pte_pv as well but just like the
2775 * above, the lock order is all wrong so if we
2776 * cannot acquire it non-blocking we will have to
2777 * undo a bunch of stuff and retry.
2780 pte_pv = pv_get_try(pmap, pmap_pte_pindex(sva),
2791 pv_put(pt_pv); /* must be non-NULL */
2793 pv_lock(pte_pv); /* safe to block now */
2796 pt_pv = pv_get(pmap,
2797 pmap_pt_pindex(sva));
2801 pte_pv = pv_get(pmap, pmap_pte_pindex(sva));
2805 * Ready for the callback. The locked pte_pv (if
2806 * not NULL) is consumed by the callback.
2809 KKASSERT((*ptep & (PG_MANAGED|PG_V)) ==
2811 func(pmap, &info, pte_pv, pt_pv, sva,
2814 KKASSERT((*ptep & (PG_MANAGED|PG_V)) ==
2816 func(pmap, &info, pte_pv, pt_pv, sva,
2836 pmap_inval_done(&info);
2837 lwkt_reltoken(&pmap->pm_token);
2841 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2843 pmap_scan(pmap, sva, eva, pmap_remove_callback, NULL);
2847 pmap_remove_callback(pmap_t pmap, struct pmap_inval_info *info,
2848 pv_entry_t pte_pv, pv_entry_t pt_pv, vm_offset_t va,
2849 pt_entry_t *ptep, void *arg __unused)
2855 * This will also drop pt_pv's wire_count. Note that
2856 * terminal pages are not wired based on mmu presence.
2858 pmap_remove_pv_pte(pte_pv, pt_pv, info);
2859 pmap_remove_pv_page(pte_pv, 0);
2863 * pt_pv's wire_count is still bumped by unmanaged pages
2864 * so we must decrement it manually.
2866 pmap_inval_interlock(info, pmap, va);
2867 pte = pte_load_clear(ptep);
2868 pmap_inval_deinterlock(info, pmap);
2870 atomic_add_long(&pmap->pm_stats.wired_count, -1);
2871 atomic_add_long(&pmap->pm_stats.resident_count, -1);
2872 if (pt_pv && vm_page_unwire_quick(pt_pv->pv_m))
2873 panic("pmap_remove: insufficient wirecount");
2878 * Removes this physical page from all physical maps in which it resides.
2879 * Reflects back modify bits to the pager.
2881 * This routine may not be called from an interrupt.
2885 pmap_remove_all(vm_page_t m)
2887 struct pmap_inval_info info;
2890 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2893 pmap_inval_init(&info);
2894 vm_page_spin_lock(m);
2895 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2896 KKASSERT(pv->pv_m == m);
2897 if (pv_hold_try(pv)) {
2898 vm_page_spin_unlock(m);
2900 vm_page_spin_unlock(m);
2902 if (pv->pv_m != m) {
2904 vm_page_spin_lock(m);
2909 * Holding no spinlocks, pv is locked.
2911 pmap_remove_pv_pte(pv, NULL, &info);
2912 pmap_remove_pv_page(pv, 0);
2914 vm_page_spin_lock(m);
2916 vm_page_spin_unlock(m);
2917 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2918 pmap_inval_done(&info);
2924 * Set the physical protection on the specified range of this map
2927 * This function may not be called from an interrupt if the map is
2928 * not the kernel_pmap.
2931 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2933 /* JG review for NX */
2937 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2938 pmap_remove(pmap, sva, eva);
2941 if (prot & VM_PROT_WRITE)
2943 pmap_scan(pmap, sva, eva, pmap_protect_callback, &prot);
2948 pmap_protect_callback(pmap_t pmap, struct pmap_inval_info *info,
2949 pv_entry_t pte_pv, pv_entry_t pt_pv, vm_offset_t va,
2950 pt_entry_t *ptep, void *arg __unused)
2959 pmap_inval_interlock(info, pmap, va);
2966 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2967 KKASSERT(m == pte_pv->pv_m);
2968 vm_page_flag_set(m, PG_REFERENCED);
2972 if (pmap_track_modified(pte_pv->pv_pindex)) {
2974 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2981 if (pbits != cbits && !atomic_cmpset_long(ptep, pbits, cbits)) {
2984 pmap_inval_deinterlock(info, pmap);
2990 * Insert the vm_page (m) at the virtual address (va), replacing any prior
2991 * mapping at that address. Set protection and wiring as requested.
2993 * NOTE: This routine MUST insert the page into the pmap now, it cannot
2997 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3000 pmap_inval_info info;
3001 pv_entry_t pt_pv; /* page table */
3002 pv_entry_t pte_pv; /* page table entry */
3005 pt_entry_t origpte, newpte;
3010 va = trunc_page(va);
3011 #ifdef PMAP_DIAGNOSTIC
3013 panic("pmap_enter: toobig");
3014 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
3015 panic("pmap_enter: invalid to pmap_enter page table "
3016 "pages (va: 0x%lx)", va);
3018 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
3019 kprintf("Warning: pmap_enter called on UVA with "
3022 db_print_backtrace();
3025 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
3026 kprintf("Warning: pmap_enter called on KVA without"
3029 db_print_backtrace();
3034 * Get locked PV entries for our new page table entry (pte_pv)
3035 * and for its parent page table (pt_pv). We need the parent
3036 * so we can resolve the location of the ptep.
3038 * Only hardware MMU actions can modify the ptep out from
3041 * if (m) is fictitious or unmanaged we do not create a managing
3042 * pte_pv for it. Any pre-existing page's management state must
3043 * match (avoiding code complexity).
3045 * If the pmap is still being initialized we assume existing
3048 * Kernel mapppings do not track page table pages (i.e. pt_pv).
3049 * pmap_allocpte() checks the
3051 if (pmap_initialized == FALSE) {
3055 } else if (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) {
3057 if (va >= VM_MAX_USER_ADDRESS) {
3061 pt_pv = pmap_allocpte(pmap, pmap_pt_pindex(va), NULL);
3062 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
3064 KKASSERT(*ptep == 0 || (*ptep & PG_MANAGED) == 0);
3066 if (va >= VM_MAX_USER_ADDRESS) {
3068 pte_pv = pmap_allocpte(pmap, pmap_pte_pindex(va), NULL);
3071 pte_pv = pmap_allocpte(pmap, pmap_pte_pindex(va),
3073 ptep = pv_pte_lookup(pt_pv, pmap_pte_index(va));
3075 KKASSERT(*ptep == 0 || (*ptep & PG_MANAGED));
3078 if ((prot & VM_PROT_NOSYNC) == 0)
3079 pmap_inval_init(&info);
3081 pa = VM_PAGE_TO_PHYS(m);
3083 opa = origpte & PG_FRAME;
3086 * Mapping has not changed, must be protection or wiring change.
3088 if (origpte && (opa == pa)) {
3090 * Wiring change, just update stats. We don't worry about
3091 * wiring PT pages as they remain resident as long as there
3092 * are valid mappings in them. Hence, if a user page is wired,
3093 * the PT page will be also.
3095 KKASSERT(pte_pv == NULL || m == pte_pv->pv_m);
3096 if (wired && ((origpte & PG_W) == 0))
3097 atomic_add_long(&pmap->pm_stats.wired_count, 1);
3098 else if (!wired && (origpte & PG_W))
3099 atomic_add_long(&pmap->pm_stats.wired_count, -1);
3101 #if defined(PMAP_DIAGNOSTIC)
3102 if (pmap_nw_modified(origpte)) {
3103 kprintf("pmap_enter: modified page not writable: "
3104 "va: 0x%lx, pte: 0x%lx\n", va, origpte);
3109 * We might be turning off write access to the page,
3110 * so we go ahead and sense modify status.
3113 if ((origpte & PG_M) &&
3114 pmap_track_modified(pte_pv->pv_pindex)) {
3117 KKASSERT(PHYS_TO_VM_PAGE(opa) == om);
3126 * Mapping has changed, invalidate old range and fall through to
3127 * handle validating new mapping.
3129 * We always interlock pte removals.
3133 /* XXX pmap_remove_pv_pte() unwires pt_pv */
3134 vm_page_wire_quick(pt_pv->pv_m);
3135 if (prot & VM_PROT_NOSYNC)
3136 pmap_remove_pv_pte(pte_pv, pt_pv, NULL);
3138 pmap_remove_pv_pte(pte_pv, pt_pv, &info);
3140 pmap_remove_pv_page(pte_pv, 0);
3141 } else if (prot & VM_PROT_NOSYNC) {
3143 cpu_invlpg((void *)va);
3144 atomic_add_long(&pmap->pm_stats.resident_count, -1);
3146 pmap_inval_interlock(&info, pmap, va);
3148 pmap_inval_deinterlock(&info, pmap);
3149 atomic_add_long(&pmap->pm_stats.resident_count, -1);
3151 KKASSERT(*ptep == 0);
3155 * Enter on the PV list if part of our managed memory. Wiring is
3156 * handled automatically.
3159 KKASSERT(pte_pv->pv_m == NULL);
3160 vm_page_spin_lock(m);
3162 TAILQ_INSERT_TAIL(&m->md.pv_list, pte_pv, pv_list);
3165 atomic_add_int(&m->object->agg_pv_list_count, 1);
3167 vm_page_flag_set(m, PG_MAPPED);
3168 vm_page_spin_unlock(m);
3170 } else if (pt_pv && opa == 0) {
3171 vm_page_wire_quick(pt_pv->pv_m);
3175 * Increment counters
3178 atomic_add_long(&pmap->pm_stats.wired_count, 1);
3182 * Now validate mapping with desired protection/wiring.
3184 newpte = (pt_entry_t)(pa | pte_prot(pmap, prot) | PG_V);
3188 if (va < VM_MAX_USER_ADDRESS)
3190 if (pmap == &kernel_pmap)
3194 * If the mapping or permission bits are different, we need
3195 * to update the pte.
3197 * We do not have to interlock pte insertions as no other
3198 * cpu will have a TLB entry.
3200 if ((origpte & ~(PG_M|PG_A)) != newpte) {
3202 if ((prot & VM_PROT_NOSYNC) == 0)
3203 pmap_inval_interlock(&info, pmap, va);
3205 *ptep = newpte | PG_A;
3206 cpu_invlpg((void *)va);
3208 if (prot & VM_PROT_NOSYNC)
3209 cpu_invlpg((void *)va);
3211 pmap_inval_deinterlock(&info, pmap);
3214 vm_page_flag_set(m, PG_WRITEABLE);
3216 atomic_add_long(&pmap->pm_stats.resident_count, 1);
3219 KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
3220 if ((prot & VM_PROT_NOSYNC) == 0)
3221 pmap_inval_done(&info);
3224 * Cleanup the pv entry, allowing other accessors.
3233 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
3234 * This code also assumes that the pmap has no pre-existing entry for this
3237 * This code currently may only be used on user pmaps, not kernel_pmap.
3240 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
3242 pmap_enter(pmap, va, m, VM_PROT_READ, FALSE);
3246 * Make a temporary mapping for a physical address. This is only intended
3247 * to be used for panic dumps.
3249 * The caller is responsible for calling smp_invltlb().
3252 pmap_kenter_temporary(vm_paddr_t pa, long i)
3254 pmap_kenter_quick((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
3255 return ((void *)crashdumpmap);
3258 #define MAX_INIT_PT (96)
3261 * This routine preloads the ptes for a given object into the specified pmap.
3262 * This eliminates the blast of soft faults on process startup and
3263 * immediately after an mmap.
3265 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
3268 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
3269 vm_object_t object, vm_pindex_t pindex,
3270 vm_size_t size, int limit)
3272 struct rb_vm_page_scan_info info;
3277 * We can't preinit if read access isn't set or there is no pmap
3280 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
3284 * We can't preinit if the pmap is not the current pmap
3286 lp = curthread->td_lwp;
3287 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
3290 psize = x86_64_btop(size);
3292 if ((object->type != OBJT_VNODE) ||
3293 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
3294 (object->resident_page_count > MAX_INIT_PT))) {
3298 if (pindex + psize > object->size) {
3299 if (object->size < pindex)
3301 psize = object->size - pindex;
3308 * Use a red-black scan to traverse the requested range and load
3309 * any valid pages found into the pmap.
3311 * We cannot safely scan the object's memq without holding the
3314 info.start_pindex = pindex;
3315 info.end_pindex = pindex + psize - 1;
3321 vm_object_hold(object);
3322 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
3323 pmap_object_init_pt_callback, &info);
3324 vm_object_drop(object);
3329 pmap_object_init_pt_callback(vm_page_t p, void *data)
3331 struct rb_vm_page_scan_info *info = data;
3332 vm_pindex_t rel_index;
3335 * don't allow an madvise to blow away our really
3336 * free pages allocating pv entries.
3338 if ((info->limit & MAP_PREFAULT_MADVISE) &&
3339 vmstats.v_free_count < vmstats.v_free_reserved) {
3342 if (vm_page_busy_try(p, TRUE))
3344 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
3345 (p->flags & PG_FICTITIOUS) == 0) {
3346 if ((p->queue - p->pc) == PQ_CACHE)
3347 vm_page_deactivate(p);
3348 rel_index = p->pindex - info->start_pindex;
3349 pmap_enter_quick(info->pmap,
3350 info->addr + x86_64_ptob(rel_index), p);
3358 * Return TRUE if the pmap is in shape to trivially pre-fault the specified
3361 * Returns FALSE if it would be non-trivial or if a pte is already loaded
3365 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
3369 spin_lock(&pmap->pm_spin);
3370 if ((pte = pmap_pte(pmap, addr)) != NULL) {
3372 spin_unlock(&pmap->pm_spin);
3376 spin_unlock(&pmap->pm_spin);
3381 * Change the wiring attribute for a pmap/va pair. The mapping must already
3382 * exist in the pmap. The mapping may or may not be managed.
3385 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3392 lwkt_gettoken(&pmap->pm_token);
3393 pv = pmap_allocpte(pmap, pmap_pt_pindex(va), NULL);
3394 ptep = pv_pte_lookup(pv, pmap_pte_index(va));
3396 if (wired && !pmap_pte_w(ptep))
3397 atomic_add_long(&pmap->pm_stats.wired_count, 1);
3398 else if (!wired && pmap_pte_w(ptep))
3399 atomic_add_long(&pmap->pm_stats.wired_count, -1);
3402 * Wiring is not a hardware characteristic so there is no need to
3403 * invalidate TLB. However, in an SMP environment we must use
3404 * a locked bus cycle to update the pte (if we are not using
3405 * the pmap_inval_*() API that is)... it's ok to do this for simple
3410 atomic_set_long(ptep, PG_W);
3412 atomic_clear_long(ptep, PG_W);
3415 atomic_set_long_nonlocked(ptep, PG_W);
3417 atomic_clear_long_nonlocked(ptep, PG_W);
3420 lwkt_reltoken(&pmap->pm_token);
3426 * Copy the range specified by src_addr/len from the source map to
3427 * the range dst_addr/len in the destination map.
3429 * This routine is only advisory and need not do anything.
3432 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3433 vm_size_t len, vm_offset_t src_addr)
3440 * Zero the specified physical page.
3442 * This function may be called from an interrupt and no locking is
3446 pmap_zero_page(vm_paddr_t phys)
3448 vm_offset_t va = PHYS_TO_DMAP(phys);
3450 pagezero((void *)va);
3454 * pmap_page_assertzero:
3456 * Assert that a page is empty, panic if it isn't.
3459 pmap_page_assertzero(vm_paddr_t phys)
3461 vm_offset_t va = PHYS_TO_DMAP(phys);
3464 for (i = 0; i < PAGE_SIZE; i += sizeof(long)) {
3465 if (*(long *)((char *)va + i) != 0) {
3466 panic("pmap_page_assertzero() @ %p not zero!\n",
3467 (void *)(intptr_t)va);
3475 * Zero part of a physical page by mapping it into memory and clearing
3476 * its contents with bzero.
3478 * off and size may not cover an area beyond a single hardware page.
3481 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3483 vm_offset_t virt = PHYS_TO_DMAP(phys);
3485 bzero((char *)virt + off, size);
3491 * Copy the physical page from the source PA to the target PA.
3492 * This function may be called from an interrupt. No locking
3496 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3498 vm_offset_t src_virt, dst_virt;
3500 src_virt = PHYS_TO_DMAP(src);
3501 dst_virt = PHYS_TO_DMAP(dst);
3502 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
3506 * pmap_copy_page_frag:
3508 * Copy the physical page from the source PA to the target PA.
3509 * This function may be called from an interrupt. No locking
3513 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3515 vm_offset_t src_virt, dst_virt;
3517 src_virt = PHYS_TO_DMAP(src);
3518 dst_virt = PHYS_TO_DMAP(dst);
3520 bcopy((char *)src_virt + (src & PAGE_MASK),
3521 (char *)dst_virt + (dst & PAGE_MASK),
3526 * Returns true if the pmap's pv is one of the first 16 pvs linked to from
3527 * this page. This count may be changed upwards or downwards in the future;
3528 * it is only necessary that true be returned for a small subset of pmaps
3529 * for proper page aging.
3532 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3537 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3540 vm_page_spin_lock(m);
3541 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3542 if (pv->pv_pmap == pmap) {
3543 vm_page_spin_unlock(m);
3550 vm_page_spin_unlock(m);
3555 * Remove all pages from specified address space this aids process exit
3556 * speeds. Also, this code may be special cased for the current process
3560 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3562 pmap_remove(pmap, sva, eva);
3566 * pmap_testbit tests bits in pte's note that the testbit/clearbit
3567 * routines are inline, and a lot of things compile-time evaluate.
3571 pmap_testbit(vm_page_t m, int bit)
3576 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3579 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3581 vm_page_spin_lock(m);
3582 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
3583 vm_page_spin_unlock(m);
3587 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3589 * if the bit being tested is the modified bit, then
3590 * mark clean_map and ptes as never
3593 if (bit & (PG_A|PG_M)) {
3594 if (!pmap_track_modified(pv->pv_pindex))
3598 #if defined(PMAP_DIAGNOSTIC)
3599 if (pv->pv_pmap == NULL) {
3600 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3604 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
3606 vm_page_spin_unlock(m);
3610 vm_page_spin_unlock(m);
3615 * This routine is used to modify bits in ptes
3617 * Caller must NOT hold any spin locks
3621 pmap_clearbit(vm_page_t m, int bit)
3623 struct pmap_inval_info info;
3627 vm_pindex_t save_pindex;
3631 vm_page_flag_clear(m, PG_WRITEABLE);
3632 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
3636 pmap_inval_init(&info);
3639 * Loop over all current mappings setting/clearing as appropos If
3640 * setting RO do we need to clear the VAC?
3642 vm_page_spin_lock(m);
3644 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3646 * don't write protect pager mappings
3649 if (!pmap_track_modified(pv->pv_pindex))
3653 #if defined(PMAP_DIAGNOSTIC)
3654 if (pv->pv_pmap == NULL) {
3655 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3661 * Careful here. We can use a locked bus instruction to
3662 * clear PG_A or PG_M safely but we need to synchronize
3663 * with the target cpus when we mess with PG_RW.
3665 * We do not have to force synchronization when clearing
3666 * PG_M even for PTEs generated via virtual memory maps,
3667 * because the virtual kernel will invalidate the pmap
3668 * entry when/if it needs to resynchronize the Modify bit.
3671 save_pmap = pv->pv_pmap;
3672 save_pindex = pv->pv_pindex;
3674 vm_page_spin_unlock(m);
3675 pmap_inval_interlock(&info, save_pmap,
3676 (vm_offset_t)save_pindex << PAGE_SHIFT);
3677 vm_page_spin_lock(m);
3678 if (pv->pv_pmap == NULL) {
3684 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
3691 atomic_clear_long(pte, PG_M|PG_RW);
3694 * The cpu may be trying to set PG_M
3695 * simultaniously with our clearing
3698 if (!atomic_cmpset_long(pte, pbits,
3702 } else if (bit == PG_M) {
3704 * We could also clear PG_RW here to force
3705 * a fault on write to redetect PG_M for
3706 * virtual kernels, but it isn't necessary
3707 * since virtual kernels invalidate the pte
3708 * when they clear the VPTE_M bit in their
3709 * virtual page tables.
3711 atomic_clear_long(pte, PG_M);
3713 atomic_clear_long(pte, bit);
3717 save_pmap = pv->pv_pmap;
3719 vm_page_spin_unlock(m);
3720 pmap_inval_deinterlock(&info, save_pmap);
3721 vm_page_spin_lock(m);
3722 if (pv->pv_pmap == NULL) {
3729 vm_page_spin_unlock(m);
3730 pmap_inval_done(&info);
3734 * Lower the permission for all mappings to a given page.
3736 * Page must be busied by caller.
3739 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3741 /* JG NX support? */
3742 if ((prot & VM_PROT_WRITE) == 0) {
3743 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3745 * NOTE: pmap_clearbit(.. PG_RW) also clears
3746 * the PG_WRITEABLE flag in (m).
3748 pmap_clearbit(m, PG_RW);
3756 pmap_phys_address(vm_pindex_t ppn)
3758 return (x86_64_ptob(ppn));
3762 * Return a count of reference bits for a page, clearing those bits.
3763 * It is not necessary for every reference bit to be cleared, but it
3764 * is necessary that 0 only be returned when there are truly no
3765 * reference bits set.
3767 * XXX: The exact number of bits to check and clear is a matter that
3768 * should be tested and standardized at some point in the future for
3769 * optimal aging of shared pages.
3771 * This routine may not block.
3774 pmap_ts_referenced(vm_page_t m)
3780 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3783 vm_page_spin_lock(m);
3784 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3785 if (!pmap_track_modified(pv->pv_pindex))
3787 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_pindex << PAGE_SHIFT);
3788 if (pte && (*pte & PG_A)) {
3790 atomic_clear_long(pte, PG_A);
3792 atomic_clear_long_nonlocked(pte, PG_A);
3799 vm_page_spin_unlock(m);
3806 * Return whether or not the specified physical page was modified
3807 * in any physical maps.
3810 pmap_is_modified(vm_page_t m)
3814 res = pmap_testbit(m, PG_M);
3819 * Clear the modify bits on the specified physical page.
3822 pmap_clear_modify(vm_page_t m)
3824 pmap_clearbit(m, PG_M);
3828 * pmap_clear_reference:
3830 * Clear the reference bit on the specified physical page.
3833 pmap_clear_reference(vm_page_t m)
3835 pmap_clearbit(m, PG_A);
3839 * Miscellaneous support routines follow
3844 i386_protection_init(void)
3848 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
3849 kp = protection_codes;
3850 for (prot = 0; prot < 8; prot++) {
3852 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3854 * Read access is also 0. There isn't any execute bit,
3855 * so just make it readable.
3857 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3858 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3859 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3862 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3863 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3864 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3865 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3873 * Map a set of physical memory pages into the kernel virtual
3874 * address space. Return a pointer to where it is mapped. This
3875 * routine is intended to be used for mapping device memory,
3878 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3882 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3884 vm_offset_t va, tmpva, offset;
3887 offset = pa & PAGE_MASK;
3888 size = roundup(offset + size, PAGE_SIZE);
3890 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3892 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3894 pa = pa & ~PAGE_MASK;
3895 for (tmpva = va; size > 0;) {
3896 pte = vtopte(tmpva);
3897 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3905 return ((void *)(va + offset));
3909 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
3911 vm_offset_t va, tmpva, offset;
3914 offset = pa & PAGE_MASK;
3915 size = roundup(offset + size, PAGE_SIZE);
3917 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3919 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3921 pa = pa & ~PAGE_MASK;
3922 for (tmpva = va; size > 0;) {
3923 pte = vtopte(tmpva);
3924 *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */
3932 return ((void *)(va + offset));
3936 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3938 vm_offset_t base, offset;
3940 base = va & ~PAGE_MASK;
3941 offset = va & PAGE_MASK;
3942 size = roundup(offset + size, PAGE_SIZE);
3943 pmap_qremove(va, size >> PAGE_SHIFT);
3944 kmem_free(&kernel_map, base, size);
3948 * perform the pmap work for mincore
3951 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3953 pt_entry_t *ptep, pte;
3957 lwkt_gettoken(&pmap->pm_token);
3958 ptep = pmap_pte(pmap, addr);
3960 if (ptep && (pte = *ptep) != 0) {
3963 val = MINCORE_INCORE;
3964 if ((pte & PG_MANAGED) == 0)
3967 pa = pte & PG_FRAME;
3969 m = PHYS_TO_VM_PAGE(pa);
3975 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3977 * Modified by someone
3979 else if (m->dirty || pmap_is_modified(m))
3980 val |= MINCORE_MODIFIED_OTHER;
3985 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3988 * Referenced by someone
3990 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3991 val |= MINCORE_REFERENCED_OTHER;
3992 vm_page_flag_set(m, PG_REFERENCED);
3996 lwkt_reltoken(&pmap->pm_token);
4002 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
4003 * vmspace will be ref'd and the old one will be deref'd.
4005 * The vmspace for all lwps associated with the process will be adjusted
4006 * and cr3 will be reloaded if any lwp is the current lwp.
4008 * The process must hold the vmspace->vm_map.token for oldvm and newvm
4011 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
4013 struct vmspace *oldvm;
4016 oldvm = p->p_vmspace;
4017 if (oldvm != newvm) {
4019 sysref_get(&newvm->vm_sysref);
4020 p->p_vmspace = newvm;
4021 KKASSERT(p->p_nthreads == 1);
4022 lp = RB_ROOT(&p->p_lwp_tree);
4023 pmap_setlwpvm(lp, newvm);
4025 sysref_put(&oldvm->vm_sysref);
4030 * Set the vmspace for a LWP. The vmspace is almost universally set the
4031 * same as the process vmspace, but virtual kernels need to swap out contexts
4032 * on a per-lwp basis.
4034 * Caller does not necessarily hold any vmspace tokens. Caller must control
4035 * the lwp (typically be in the context of the lwp). We use a critical
4036 * section to protect against statclock and hardclock (statistics collection).
4039 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
4041 struct vmspace *oldvm;
4044 oldvm = lp->lwp_vmspace;
4046 if (oldvm != newvm) {
4048 lp->lwp_vmspace = newvm;
4049 if (curthread->td_lwp == lp) {
4050 pmap = vmspace_pmap(newvm);
4052 atomic_set_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
4053 if (pmap->pm_active & CPUMASK_LOCK)
4054 pmap_interlock_wait(newvm);
4056 pmap->pm_active |= 1;
4058 #if defined(SWTCH_OPTIM_STATS)
4061 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
4062 curthread->td_pcb->pcb_cr3 |= PG_RW | PG_U | PG_V;
4063 load_cr3(curthread->td_pcb->pcb_cr3);
4064 pmap = vmspace_pmap(oldvm);
4066 atomic_clear_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
4068 pmap->pm_active &= ~(cpumask_t)1;
4078 * Called when switching to a locked pmap, used to interlock against pmaps
4079 * undergoing modifications to prevent us from activating the MMU for the
4080 * target pmap until all such modifications have completed. We have to do
4081 * this because the thread making the modifications has already set up its
4082 * SMP synchronization mask.
4087 pmap_interlock_wait(struct vmspace *vm)
4089 struct pmap *pmap = &vm->vm_pmap;
4091 if (pmap->pm_active & CPUMASK_LOCK) {
4093 DEBUG_PUSH_INFO("pmap_interlock_wait");
4094 while (pmap->pm_active & CPUMASK_LOCK) {
4096 lwkt_process_ipiq();
4106 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
4109 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
4113 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
4118 * Used by kmalloc/kfree, page already exists at va
4121 pmap_kvtom(vm_offset_t va)
4123 return(PHYS_TO_VM_PAGE(*vtopte(va) & PG_FRAME));