4 * Copyright (c) 1991 Regents of the University of California.
5 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
7 * Copyright (c) 2003 Peter Wemm
8 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
9 * Copyright (c) 2008, 2009 The DragonFly Project.
10 * Copyright (c) 2008, 2009 Jordan Gordeev.
11 * All rights reserved.
13 * This code is derived from software contributed to Berkeley by
14 * the Systems Programming Group of the University of Utah Computer
15 * Science Department and William Jolitz of UUNET Technologies Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
46 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
50 * Manages physical address maps.
52 * In addition to hardware address maps, this
53 * module is called upon to provide software-use-only
54 * maps which may or may not be stored in the same
55 * form as hardware maps. These pseudo-maps are
56 * used to store intermediate results from copy
57 * operations to and from address spaces.
59 * Since the information managed by this module is
60 * also stored by the logical address mapping module,
61 * this module may throw away valid virtual-to-physical
62 * mappings at almost any time. However, invalidations
63 * of virtual-to-physical mappings must be done as
66 * In order to cope with hardware architectures which
67 * make virtual-to-physical map invalidates expensive,
68 * this module may delay invalidate or reduced protection
69 * operations until such time as they are actually
70 * necessary. This module is given full information as
71 * to which processors are currently using which maps,
72 * and to when physical maps must be made correct.
76 #include "opt_disable_pse.h"
79 #include "opt_msgbuf.h"
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
85 #include <sys/msgbuf.h>
86 #include <sys/vmmeter.h>
90 #include <vm/vm_param.h>
91 #include <sys/sysctl.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_extern.h>
98 #include <vm/vm_pageout.h>
99 #include <vm/vm_pager.h>
100 #include <vm/vm_zone.h>
102 #include <sys/user.h>
103 #include <sys/thread2.h>
104 #include <sys/sysref2.h>
106 #include <machine/cputypes.h>
107 #include <machine/md_var.h>
108 #include <machine/specialreg.h>
109 #include <machine/smp.h>
110 #include <machine_base/apic/apicreg.h>
111 #include <machine/globaldata.h>
112 #include <machine/pmap.h>
113 #include <machine/pmap_inval.h>
117 #define PMAP_KEEP_PDIRS
118 #ifndef PMAP_SHPGPERPROC
119 #define PMAP_SHPGPERPROC 200
122 #if defined(DIAGNOSTIC)
123 #define PMAP_DIAGNOSTIC
129 * Get PDEs and PTEs for user/kernel address space
131 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
132 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
134 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & PG_V) != 0)
135 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & PG_W) != 0)
136 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & PG_M) != 0)
137 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & PG_A) != 0)
138 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & PG_V) != 0)
142 * Given a map and a machine independent protection code,
143 * convert to a vax protection code.
145 #define pte_prot(m, p) \
146 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
147 static int protection_codes[8];
149 struct pmap kernel_pmap;
150 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
152 vm_paddr_t avail_start; /* PA of first available physical page */
153 vm_paddr_t avail_end; /* PA of last available physical page */
154 vm_offset_t virtual2_start; /* cutout free area prior to kernel start */
155 vm_offset_t virtual2_end;
156 vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */
157 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
158 vm_offset_t KvaStart; /* VA start of KVA space */
159 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
160 vm_offset_t KvaSize; /* max size of kernel virtual address space */
161 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
162 static int pgeflag; /* PG_G or-in */
163 static int pseflag; /* PG_PS or-in */
165 static vm_object_t kptobj;
168 static vm_paddr_t dmaplimit;
170 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
172 static uint64_t KPTbase;
173 static uint64_t KPTphys;
174 static uint64_t KPDphys; /* phys addr of kernel level 2 */
175 static uint64_t KPDbase; /* phys addr of kernel level 2 @ KERNBASE */
176 uint64_t KPDPphys; /* phys addr of kernel level 3 */
177 uint64_t KPML4phys; /* phys addr of kernel level 4 */
179 static uint64_t DMPDphys; /* phys addr of direct mapped level 2 */
180 static uint64_t DMPDPphys; /* phys addr of direct mapped level 3 */
183 * Data for the pv entry allocation mechanism
185 static vm_zone_t pvzone;
186 static struct vm_zone pvzone_store;
187 static struct vm_object pvzone_obj;
188 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
189 static int pmap_pagedaemon_waken = 0;
190 static struct pv_entry *pvinit;
193 * All those kernel PT submaps that BSD is so fond of
195 pt_entry_t *CMAP1 = 0, *ptmmap;
196 caddr_t CADDR1 = 0, ptvmmap = 0;
197 static pt_entry_t *msgbufmap;
198 struct msgbuf *msgbufp=0;
203 static pt_entry_t *pt_crashdumpmap;
204 static caddr_t crashdumpmap;
206 extern pt_entry_t *SMPpt;
207 extern uint64_t SMPptpa;
211 static pv_entry_t get_pv_entry (void);
212 static void i386_protection_init (void);
213 static void create_pagetables(vm_paddr_t *firstaddr);
214 static void pmap_remove_all (vm_page_t m);
215 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
216 vm_offset_t sva, pmap_inval_info_t info);
217 static void pmap_remove_page (struct pmap *pmap,
218 vm_offset_t va, pmap_inval_info_t info);
219 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
220 vm_offset_t va, pmap_inval_info_t info);
221 static boolean_t pmap_testbit (vm_page_t m, int bit);
222 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
223 vm_page_t mpte, vm_page_t m);
225 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
227 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
228 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
229 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
230 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
231 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
232 pmap_inval_info_t info);
233 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
234 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
236 static unsigned pdir4mb;
239 * Move the kernel virtual free pointer to the next
240 * 2MB. This is used to help improve performance
241 * by using a large (2MB) page for much of the kernel
242 * (.text, .data, .bss)
246 pmap_kmem_choose(vm_offset_t addr)
248 vm_offset_t newaddr = addr;
250 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
257 * Super fast pmap_pte routine best used when scanning the pv lists.
258 * This eliminates many course-grained invltlb calls. Note that many of
259 * the pv list scans are across different pmaps and it is very wasteful
260 * to do an entire invltlb when checking a single mapping.
262 * Should only be called while in a critical section.
264 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
268 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
270 return pmap_pte(pmap, va);
273 /* Return a non-clipped PD index for a given VA */
276 pmap_pde_pindex(vm_offset_t va)
278 return va >> PDRSHIFT;
281 /* Return various clipped indexes for a given VA */
284 pmap_pte_index(vm_offset_t va)
287 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
292 pmap_pde_index(vm_offset_t va)
295 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
300 pmap_pdpe_index(vm_offset_t va)
303 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
308 pmap_pml4e_index(vm_offset_t va)
311 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
314 /* Return a pointer to the PML4 slot that corresponds to a VA */
317 pmap_pml4e(pmap_t pmap, vm_offset_t va)
320 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
323 /* Return a pointer to the PDP slot that corresponds to a VA */
326 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
330 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME);
331 return (&pdpe[pmap_pdpe_index(va)]);
334 /* Return a pointer to the PDP slot that corresponds to a VA */
337 pmap_pdpe(pmap_t pmap, vm_offset_t va)
341 pml4e = pmap_pml4e(pmap, va);
342 if ((*pml4e & PG_V) == 0)
344 return (pmap_pml4e_to_pdpe(pml4e, va));
347 /* Return a pointer to the PD slot that corresponds to a VA */
350 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
354 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME);
355 return (&pde[pmap_pde_index(va)]);
358 /* Return a pointer to the PD slot that corresponds to a VA */
361 pmap_pde(pmap_t pmap, vm_offset_t va)
365 pdpe = pmap_pdpe(pmap, va);
366 if (pdpe == NULL || (*pdpe & PG_V) == 0)
368 return (pmap_pdpe_to_pde(pdpe, va));
371 /* Return a pointer to the PT slot that corresponds to a VA */
374 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
378 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
379 return (&pte[pmap_pte_index(va)]);
382 /* Return a pointer to the PT slot that corresponds to a VA */
385 pmap_pte(pmap_t pmap, vm_offset_t va)
389 pde = pmap_pde(pmap, va);
390 if (pde == NULL || (*pde & PG_V) == 0)
392 if ((*pde & PG_PS) != 0) /* compat with i386 pmap_pte() */
393 return ((pt_entry_t *)pde);
394 return (pmap_pde_to_pte(pde, va));
399 vtopte(vm_offset_t va)
401 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
403 return (PTmap + ((va >> PAGE_SHIFT) & mask));
408 vtopde(vm_offset_t va)
410 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
412 return (PDmap + ((va >> PDRSHIFT) & mask));
416 allocpages(vm_paddr_t *firstaddr, int n)
421 bzero((void *)ret, n * PAGE_SIZE);
422 *firstaddr += n * PAGE_SIZE;
428 create_pagetables(vm_paddr_t *firstaddr)
433 * We are running (mostly) V=P at this point
435 * Calculate NKPT - number of kernel page tables. We have to
436 * accomodoate prealloction of the vm_page_array, dump bitmap,
437 * MSGBUF_SIZE, and other stuff. Be generous.
439 * Maxmem is in pages.
441 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
442 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
445 nkpt = (Maxmem * sizeof(struct vm_page) + NBPDR - 1) / NBPDR;
446 nkpt += ((nkpt + nkpt + 1 + NKPML4E + NKPDPE + NDMPML4E + ndmpdp) +
453 KPTbase = allocpages(firstaddr, nkpt);
454 KPTphys = allocpages(firstaddr, nkpt);
455 KPML4phys = allocpages(firstaddr, 1);
456 KPDPphys = allocpages(firstaddr, NKPML4E);
459 * Calculate the page directory base for KERNBASE,
460 * that is where we start populating the page table pages.
461 * Basically this is the end - 2.
463 KPDphys = allocpages(firstaddr, NKPDPE);
464 KPDbase = KPDphys + ((NKPDPE - (NPDPEPG - KPDPI)) << PAGE_SHIFT);
466 DMPDPphys = allocpages(firstaddr, NDMPML4E);
467 if ((amd_feature & AMDID_PAGE1GB) == 0)
468 DMPDphys = allocpages(firstaddr, ndmpdp);
469 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
472 * Fill in the underlying page table pages for the area around
473 * KERNBASE. This remaps low physical memory to KERNBASE.
475 * Read-only from zero to physfree
476 * XXX not fully used, underneath 2M pages
478 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
479 ((pt_entry_t *)KPTbase)[i] = i << PAGE_SHIFT;
480 ((pt_entry_t *)KPTbase)[i] |= PG_RW | PG_V | PG_G;
484 * Now map the initial kernel page tables. One block of page
485 * tables is placed at the beginning of kernel virtual memory,
486 * and another block is placed at KERNBASE to map the kernel binary,
487 * data, bss, and initial pre-allocations.
489 for (i = 0; i < nkpt; i++) {
490 ((pd_entry_t *)KPDbase)[i] = KPTbase + (i << PAGE_SHIFT);
491 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V;
493 for (i = 0; i < nkpt; i++) {
494 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
495 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
499 * Map from zero to end of allocations using 2M pages as an
500 * optimization. This will bypass some of the KPTBase pages
501 * above in the KERNBASE area.
503 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
504 ((pd_entry_t *)KPDbase)[i] = i << PDRSHIFT;
505 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V | PG_PS | PG_G;
509 * And connect up the PD to the PDP. The kernel pmap is expected
510 * to pre-populate all of its PDs. See NKPDPE in vmparam.h.
512 for (i = 0; i < NKPDPE; i++) {
513 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] =
514 KPDphys + (i << PAGE_SHIFT);
515 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] |=
519 /* Now set up the direct map space using either 2MB or 1GB pages */
520 /* Preset PG_M and PG_A because demotion expects it */
521 if ((amd_feature & AMDID_PAGE1GB) == 0) {
522 for (i = 0; i < NPDEPG * ndmpdp; i++) {
523 ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT;
524 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
527 /* And the direct map space's PDP */
528 for (i = 0; i < ndmpdp; i++) {
529 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
531 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
534 for (i = 0; i < ndmpdp; i++) {
535 ((pdp_entry_t *)DMPDPphys)[i] =
536 (vm_paddr_t)i << PDPSHIFT;
537 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
542 /* And recursively map PML4 to itself in order to get PTmap */
543 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
544 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
546 /* Connect the Direct Map slot up to the PML4 */
547 ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
548 ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
550 /* Connect the KVA slot up to the PML4 */
551 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
552 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
556 * Bootstrap the system enough to run with virtual memory.
558 * On the i386 this is called after mapping has already been enabled
559 * and just syncs the pmap module with what has already been done.
560 * [We can't call it easily with mapping off since the kernel is not
561 * mapped with PA == VA, hence we would have to relocate every address
562 * from the linked base (virtual) address "KERNBASE" to the actual
563 * (physical) address starting relative to 0]
566 pmap_bootstrap(vm_paddr_t *firstaddr)
570 struct mdglobaldata *gd;
573 KvaStart = VM_MIN_KERNEL_ADDRESS;
574 KvaEnd = VM_MAX_KERNEL_ADDRESS;
575 KvaSize = KvaEnd - KvaStart;
577 avail_start = *firstaddr;
580 * Create an initial set of page tables to run the kernel in.
582 create_pagetables(firstaddr);
584 virtual2_start = KvaStart;
585 virtual2_end = PTOV_OFFSET;
587 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
588 virtual_start = pmap_kmem_choose(virtual_start);
590 virtual_end = VM_MAX_KERNEL_ADDRESS;
592 /* XXX do %cr0 as well */
593 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
597 * Initialize protection array.
599 i386_protection_init();
602 * The kernel's pmap is statically allocated so we don't have to use
603 * pmap_create, which is unlikely to work correctly at this part of
604 * the boot sequence (XXX and which no longer exists).
606 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
607 kernel_pmap.pm_count = 1;
608 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
609 TAILQ_INIT(&kernel_pmap.pm_pvlist);
612 * Reserve some special page table entries/VA space for temporary
615 #define SYSMAP(c, p, v, n) \
616 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
622 * CMAP1/CMAP2 are used for zeroing and copying pages.
624 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
629 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
632 * ptvmmap is used for reading arbitrary physical pages via
635 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
638 * msgbufp is used to map the system message buffer.
639 * XXX msgbufmap is not used.
641 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
642 atop(round_page(MSGBUF_SIZE)))
649 * PG_G is terribly broken on SMP because we IPI invltlb's in some
650 * cases rather then invl1pg. Actually, I don't even know why it
651 * works under UP because self-referential page table mappings
656 if (cpu_feature & CPUID_PGE)
661 * Initialize the 4MB page size flag
665 * The 4MB page version of the initial
666 * kernel page mapping.
670 #if !defined(DISABLE_PSE)
671 if (cpu_feature & CPUID_PSE) {
674 * Note that we have enabled PSE mode
677 ptditmp = *(PTmap + x86_64_btop(KERNBASE));
678 ptditmp &= ~(NBPDR - 1);
679 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
684 * Enable the PSE mode. If we are SMP we can't do this
685 * now because the APs will not be able to use it when
688 load_cr4(rcr4() | CR4_PSE);
691 * We can do the mapping here for the single processor
692 * case. We simply ignore the old page table page from
696 * For SMP, we still need 4K pages to bootstrap APs,
697 * PSE will be enabled as soon as all APs are up.
699 PTD[KPTDI] = (pd_entry_t)ptditmp;
706 * We need to finish setting up the globaldata page for the BSP.
707 * locore has already populated the page table for the mdglobaldata
710 pg = MDGLOBALDATA_BASEALLOC_PAGES;
711 gd = &CPU_prvspace[0].mdglobaldata;
712 gd->gd_CMAP1 = &SMPpt[pg + 0];
713 gd->gd_CMAP2 = &SMPpt[pg + 1];
714 gd->gd_CMAP3 = &SMPpt[pg + 2];
715 gd->gd_PMAP1 = &SMPpt[pg + 3];
716 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
717 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
718 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
719 gd->gd_PADDR1 = (pt_entry_t *)CPU_prvspace[0].PPAGE1;
726 * Set 4mb pdir for mp startup
731 if (pseflag && (cpu_feature & CPUID_PSE)) {
732 load_cr4(rcr4() | CR4_PSE);
733 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
741 * XXX: Hack. Required by pmap_init()
743 extern vm_offset_t cpu_apic_addr;
746 * Initialize the pmap module.
747 * Called by vm_init, to initialize any structures that the pmap
748 * system needs to map virtual memory.
749 * pmap_init has been enhanced to support in a fairly consistant
750 * way, discontiguous physical memory.
759 * object for kernel page table pages
761 /* JG I think the number can be arbitrary */
762 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
765 * Allocate memory for random pmap data structures. Includes the
769 for(i = 0; i < vm_page_array_size; i++) {
772 m = &vm_page_array[i];
773 TAILQ_INIT(&m->md.pv_list);
774 m->md.pv_list_count = 0;
778 * init the pv free list
780 initial_pvs = vm_page_array_size;
781 if (initial_pvs < MINPV)
783 pvzone = &pvzone_store;
784 pvinit = (void *)kmem_alloc(&kernel_map,
785 initial_pvs * sizeof (struct pv_entry));
786 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry),
787 pvinit, initial_pvs);
790 * Now it is safe to enable pv_table recording.
792 pmap_initialized = TRUE;
797 lapic = pmap_mapdev_uncacheable(cpu_apic_addr, sizeof(struct LAPIC));
802 * Initialize the address space (zone) for the pv_entries. Set a
803 * high water mark so that the system can recover from excessive
804 * numbers of pv entries.
809 int shpgperproc = PMAP_SHPGPERPROC;
812 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
813 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
814 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
815 pv_entry_high_water = 9 * (pv_entry_max / 10);
818 * Subtract out pages already installed in the zone (hack)
820 entry_max = pv_entry_max - vm_page_array_size;
824 zinitna(pvzone, &pvzone_obj, NULL, 0, entry_max, ZONE_INTERRUPT, 1);
828 /***************************************************
829 * Low level helper routines.....
830 ***************************************************/
832 #if defined(PMAP_DIAGNOSTIC)
835 * This code checks for non-writeable/modified pages.
836 * This should be an invalid condition.
840 pmap_nw_modified(pt_entry_t pte)
842 if ((pte & (PG_M|PG_RW)) == PG_M)
851 * this routine defines the region(s) of memory that should
852 * not be tested for the modified bit.
856 pmap_track_modified(vm_offset_t va)
858 if ((va < clean_sva) || (va >= clean_eva))
865 * Extract the physical page address associated with the map/VA pair.
867 * The caller must hold vm_token if non-blocking operation is desired.
870 pmap_extract(pmap_t pmap, vm_offset_t va)
874 pd_entry_t pde, *pdep;
876 lwkt_gettoken(&vm_token);
878 pdep = pmap_pde(pmap, va);
882 if ((pde & PG_PS) != 0) {
883 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
885 pte = pmap_pde_to_pte(pdep, va);
886 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
890 lwkt_reltoken(&vm_token);
895 * Extract the physical page address associated kernel virtual address.
898 pmap_kextract(vm_offset_t va)
903 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
904 pa = DMAP_TO_PHYS(va);
908 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
911 * Beware of a concurrent promotion that changes the
912 * PDE at this point! For example, vtopte() must not
913 * be used to access the PTE because it would use the
914 * new PDE. It is, however, safe to use the old PDE
915 * because the page table page is preserved by the
918 pa = *pmap_pde_to_pte(&pde, va);
919 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
925 /***************************************************
926 * Low level mapping routines.....
927 ***************************************************/
930 * Routine: pmap_kenter
932 * Add a wired page to the KVA
933 * NOTE! note that in order for the mapping to take effect -- you
934 * should do an invltlb after doing the pmap_kenter().
937 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
941 pmap_inval_info info;
943 pmap_inval_init(&info);
944 npte = pa | PG_RW | PG_V | pgeflag;
946 pmap_inval_interlock(&info, &kernel_pmap, va);
948 pmap_inval_deinterlock(&info, &kernel_pmap);
949 pmap_inval_done(&info);
953 * Routine: pmap_kenter_quick
955 * Similar to pmap_kenter(), except we only invalidate the
956 * mapping on the current CPU.
959 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
964 npte = pa | PG_RW | PG_V | pgeflag;
967 cpu_invlpg((void *)va);
971 pmap_kenter_sync(vm_offset_t va)
973 pmap_inval_info info;
975 pmap_inval_init(&info);
976 pmap_inval_interlock(&info, &kernel_pmap, va);
977 pmap_inval_deinterlock(&info, &kernel_pmap);
978 pmap_inval_done(&info);
982 pmap_kenter_sync_quick(vm_offset_t va)
984 cpu_invlpg((void *)va);
988 * remove a page from the kernel pagetables
991 pmap_kremove(vm_offset_t va)
994 pmap_inval_info info;
996 pmap_inval_init(&info);
998 pmap_inval_interlock(&info, &kernel_pmap, va);
1000 pmap_inval_deinterlock(&info, &kernel_pmap);
1001 pmap_inval_done(&info);
1005 pmap_kremove_quick(vm_offset_t va)
1010 cpu_invlpg((void *)va);
1014 * XXX these need to be recoded. They are not used in any critical path.
1017 pmap_kmodify_rw(vm_offset_t va)
1019 *vtopte(va) |= PG_RW;
1020 cpu_invlpg((void *)va);
1024 pmap_kmodify_nc(vm_offset_t va)
1026 *vtopte(va) |= PG_N;
1027 cpu_invlpg((void *)va);
1031 * Used to map a range of physical addresses into kernel virtual
1032 * address space during the low level boot, typically to map the
1033 * dump bitmap, message buffer, and vm_page_array.
1035 * These mappings are typically made at some pointer after the end of the
1038 * We could return PHYS_TO_DMAP(start) here and not allocate any
1039 * via (*virtp), but then kmem from userland and kernel dumps won't
1040 * have access to the related pointers.
1043 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
1046 vm_offset_t va_start;
1048 /*return PHYS_TO_DMAP(start);*/
1053 while (start < end) {
1054 pmap_kenter_quick(va, start);
1064 * Add a list of wired pages to the kva
1065 * this routine is only used for temporary
1066 * kernel mappings that do not need to have
1067 * page modification or references recorded.
1068 * Note that old mappings are simply written
1069 * over. The page *must* be wired.
1072 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1076 end_va = va + count * PAGE_SIZE;
1078 while (va < end_va) {
1082 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1083 cpu_invlpg((void *)va);
1091 * This routine jerks page mappings from the
1092 * kernel -- it is meant only for temporary mappings.
1094 * MPSAFE, INTERRUPT SAFE (cluster callback)
1097 pmap_qremove(vm_offset_t va, int count)
1101 end_va = va + count * PAGE_SIZE;
1103 while (va < end_va) {
1108 cpu_invlpg((void *)va);
1115 * This routine works like vm_page_lookup() but also blocks as long as the
1116 * page is busy. This routine does not busy the page it returns.
1118 * Unless the caller is managing objects whos pages are in a known state,
1119 * the call should be made with a critical section held so the page's object
1120 * association remains valid on return.
1124 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
1129 m = vm_page_lookup(object, pindex);
1130 } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
1136 * Create a new thread and optionally associate it with a (new) process.
1137 * NOTE! the new thread's cpu may not equal the current cpu.
1140 pmap_init_thread(thread_t td)
1142 /* enforce pcb placement & alignment */
1143 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1144 td->td_pcb = (struct pcb *)((intptr_t)td->td_pcb & ~(intptr_t)0xF);
1145 td->td_savefpu = &td->td_pcb->pcb_save;
1146 td->td_sp = (char *)td->td_pcb; /* no -16 */
1150 * This routine directly affects the fork perf for a process.
1153 pmap_init_proc(struct proc *p)
1158 * Dispose the UPAGES for a process that has exited.
1159 * This routine directly impacts the exit perf of a process.
1162 pmap_dispose_proc(struct proc *p)
1164 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1167 /***************************************************
1168 * Page table page management routines.....
1169 ***************************************************/
1172 * This routine unholds page table pages, and if the hold count
1173 * drops to zero, then it decrements the wire count.
1177 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1178 pmap_inval_info_t info)
1180 KKASSERT(m->hold_count > 0);
1181 if (m->hold_count > 1) {
1185 return _pmap_unwire_pte_hold(pmap, va, m, info);
1191 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1192 pmap_inval_info_t info)
1195 * Wait until we can busy the page ourselves. We cannot have
1196 * any active flushes if we block. We own one hold count on the
1197 * page so it cannot be freed out from under us.
1199 if (m->flags & PG_BUSY) {
1200 pmap_inval_flush(info);
1201 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1204 KASSERT(m->queue == PQ_NONE,
1205 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1208 * This case can occur if new references were acquired while
1211 if (m->hold_count > 1) {
1212 KKASSERT(m->hold_count > 1);
1218 * Unmap the page table page
1220 KKASSERT(m->hold_count == 1);
1222 pmap_inval_interlock(info, pmap, -1);
1224 if (m->pindex >= (NUPDE + NUPDPE)) {
1227 pml4 = pmap_pml4e(pmap, va);
1229 } else if (m->pindex >= NUPDE) {
1232 pdp = pmap_pdpe(pmap, va);
1237 pd = pmap_pde(pmap, va);
1241 KKASSERT(pmap->pm_stats.resident_count > 0);
1242 --pmap->pm_stats.resident_count;
1244 if (pmap->pm_ptphint == m)
1245 pmap->pm_ptphint = NULL;
1246 pmap_inval_deinterlock(info, pmap);
1248 if (m->pindex < NUPDE) {
1249 /* We just released a PT, unhold the matching PD */
1252 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1253 pmap_unwire_pte_hold(pmap, va, pdpg, info);
1255 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1256 /* We just released a PD, unhold the matching PDP */
1259 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1260 pmap_unwire_pte_hold(pmap, va, pdppg, info);
1264 * This was our last hold, the page had better be unwired
1265 * after we decrement wire_count.
1267 * FUTURE NOTE: shared page directory page could result in
1268 * multiple wire counts.
1272 KKASSERT(m->wire_count == 0);
1273 --vmstats.v_wire_count;
1274 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1276 vm_page_free_zero(m);
1282 * After removing a page table entry, this routine is used to
1283 * conditionally free the page, and manage the hold/wire counts.
1287 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1288 pmap_inval_info_t info)
1290 vm_pindex_t ptepindex;
1292 if (va >= VM_MAX_USER_ADDRESS)
1296 ptepindex = pmap_pde_pindex(va);
1298 if (pmap->pm_ptphint &&
1299 (pmap->pm_ptphint->pindex == ptepindex)) {
1300 mpte = pmap->pm_ptphint;
1303 pmap_inval_flush(info);
1304 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1305 pmap->pm_ptphint = mpte;
1310 return pmap_unwire_pte_hold(pmap, va, mpte, info);
1314 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1315 * it, and IdlePTD, represents the template used to update all other pmaps.
1317 * On architectures where the kernel pmap is not integrated into the user
1318 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1319 * kernel_pmap should be used to directly access the kernel_pmap.
1322 pmap_pinit0(struct pmap *pmap)
1324 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1326 pmap->pm_active = 0;
1327 pmap->pm_ptphint = NULL;
1328 TAILQ_INIT(&pmap->pm_pvlist);
1329 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1333 * Initialize a preallocated and zeroed pmap structure,
1334 * such as one in a vmspace structure.
1337 pmap_pinit(struct pmap *pmap)
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 an object for the ptes
1353 if (pmap->pm_pteobj == NULL)
1354 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1357 * Allocate the page directory page, unless we already have
1358 * one cached. If we used the cached page the wire_count will
1359 * already be set appropriately.
1361 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1362 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1363 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1364 pmap->pm_pdirm = ptdpg;
1365 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1366 ptdpg->valid = VM_PAGE_BITS_ALL;
1367 if (ptdpg->wire_count == 0)
1368 ++vmstats.v_wire_count;
1369 ptdpg->wire_count = 1;
1370 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1372 if ((ptdpg->flags & PG_ZERO) == 0)
1373 bzero(pmap->pm_pml4, PAGE_SIZE);
1376 pmap_page_assertzero(VM_PAGE_TO_PHYS(ptdpg));
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(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1386 pmap->pm_active = 0;
1387 pmap->pm_ptphint = NULL;
1388 TAILQ_INIT(&pmap->pm_pvlist);
1389 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1390 pmap->pm_stats.resident_count = 1;
1394 * Clean up a pmap structure so it can be physically freed. This routine
1395 * is called by the vmspace dtor function. A great deal of pmap data is
1396 * left passively mapped to improve vmspace management so we have a bit
1397 * of cleanup work to do here.
1400 pmap_puninit(pmap_t pmap)
1404 KKASSERT(pmap->pm_active == 0);
1405 lwkt_gettoken(&vm_token);
1406 if ((p = pmap->pm_pdirm) != NULL) {
1407 KKASSERT(pmap->pm_pml4 != NULL);
1408 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1409 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1411 vmstats.v_wire_count--;
1412 KKASSERT((p->flags & PG_BUSY) == 0);
1414 vm_page_free_zero(p);
1415 pmap->pm_pdirm = NULL;
1417 if (pmap->pm_pml4) {
1418 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1419 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1420 pmap->pm_pml4 = NULL;
1422 if (pmap->pm_pteobj) {
1423 vm_object_deallocate(pmap->pm_pteobj);
1424 pmap->pm_pteobj = NULL;
1426 lwkt_reltoken(&vm_token);
1430 * Wire in kernel global address entries. To avoid a race condition
1431 * between pmap initialization and pmap_growkernel, this procedure
1432 * adds the pmap to the master list (which growkernel scans to update),
1433 * then copies the template.
1436 pmap_pinit2(struct pmap *pmap)
1439 lwkt_gettoken(&vm_token);
1440 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1441 /* XXX copies current process, does not fill in MPPTDI */
1442 lwkt_reltoken(&vm_token);
1447 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1448 * 0 on failure (if the procedure had to sleep).
1450 * When asked to remove the page directory page itself, we actually just
1451 * leave it cached so we do not have to incur the SMP inval overhead of
1452 * removing the kernel mapping. pmap_puninit() will take care of it.
1456 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1459 * This code optimizes the case of freeing non-busy
1460 * page-table pages. Those pages are zero now, and
1461 * might as well be placed directly into the zero queue.
1463 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1469 * Remove the page table page from the processes address space.
1471 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1473 * We are the pml4 table itself.
1475 /* XXX anything to do here? */
1476 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1478 * Remove a PDP page from the PML4. We do not maintain
1479 * hold counts on the PML4 page.
1485 m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1486 KKASSERT(m4 != NULL);
1487 pml4 = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1488 idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1489 KKASSERT(pml4[idx] != 0);
1491 } else if (p->pindex >= NUPDE) {
1493 * Remove a PD page from the PDP and drop the hold count
1494 * on the PDP. The PDP is left cached in the pmap if
1495 * the hold count drops to 0 so the wire count remains
1502 m3 = vm_page_lookup(pmap->pm_pteobj,
1503 NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1504 KKASSERT(m3 != NULL);
1505 pdp = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1506 idx = (p->pindex - NUPDE) % NPDPEPG;
1507 KKASSERT(pdp[idx] != 0);
1512 * Remove a PT page from the PD and drop the hold count
1513 * on the PD. The PD is left cached in the pmap if
1514 * the hold count drops to 0 so the wire count remains
1521 m2 = vm_page_lookup(pmap->pm_pteobj,
1522 NUPDE + p->pindex / NPDEPG);
1523 KKASSERT(m2 != NULL);
1524 pd = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1525 idx = p->pindex % NPDEPG;
1531 * One fewer mappings in the pmap. p's hold count had better
1534 KKASSERT(pmap->pm_stats.resident_count > 0);
1535 --pmap->pm_stats.resident_count;
1537 panic("pmap_release: freeing held page table page");
1538 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1539 pmap->pm_ptphint = NULL;
1542 * We leave the top-level page table page cached, wired, and mapped in
1543 * the pmap until the dtor function (pmap_puninit()) gets called.
1544 * However, still clean it up so we can set PG_ZERO.
1546 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1547 bzero(pmap->pm_pml4, PAGE_SIZE);
1548 vm_page_flag_set(p, PG_ZERO);
1552 KKASSERT(p->wire_count == 0);
1553 vmstats.v_wire_count--;
1554 /* JG eventually revert to using vm_page_free_zero() */
1561 * This routine is called when various levels in the page table need to
1562 * be populated. This routine cannot fail.
1566 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1571 * Find or fabricate a new pagetable page. This will busy the page.
1573 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1574 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1575 if ((m->flags & PG_ZERO) == 0) {
1576 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1580 pmap_page_assertzero(VM_PAGE_TO_PHYS(m));
1584 KASSERT(m->queue == PQ_NONE,
1585 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1588 * Increment the hold count for the page we will be returning to
1592 if (m->wire_count++ == 0)
1593 vmstats.v_wire_count++;
1594 m->valid = VM_PAGE_BITS_ALL;
1595 vm_page_flag_clear(m, PG_ZERO);
1598 * Map the pagetable page into the process address space, if
1599 * it isn't already there.
1601 * It is possible that someone else got in and mapped the page
1602 * directory page while we were blocked, if so just unbusy and
1603 * return the held page.
1605 if (ptepindex >= (NUPDE + NUPDPE)) {
1607 * Wire up a new PDP page in the PML4
1609 vm_pindex_t pml4index;
1612 pml4index = ptepindex - (NUPDE + NUPDPE);
1613 pml4 = &pmap->pm_pml4[pml4index];
1615 if (--m->wire_count == 0)
1616 --vmstats.v_wire_count;
1620 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1621 } else if (ptepindex >= NUPDE) {
1623 * Wire up a new PD page in the PDP
1625 vm_pindex_t pml4index;
1626 vm_pindex_t pdpindex;
1631 pdpindex = ptepindex - NUPDE;
1632 pml4index = pdpindex >> NPML4EPGSHIFT;
1634 pml4 = &pmap->pm_pml4[pml4index];
1635 if ((*pml4 & PG_V) == 0) {
1637 * Have to allocate a new PDP page, recurse.
1638 * This always succeeds. Returned page will
1641 pdppg = _pmap_allocpte(pmap,
1642 NUPDE + NUPDPE + pml4index);
1645 * Add a held reference to the PDP page.
1647 pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1648 pdppg->hold_count++;
1652 * Now find the pdp_entry and map the PDP. If the PDP
1653 * has already been mapped unwind and return the
1654 * already-mapped PDP held.
1656 * pdppg is left held (hold_count is incremented for
1657 * each PD in the PDP).
1659 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1660 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1662 vm_page_unhold(pdppg);
1663 if (--m->wire_count == 0)
1664 --vmstats.v_wire_count;
1668 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1671 * Wire up the new PT page in the PD
1673 vm_pindex_t pml4index;
1674 vm_pindex_t pdpindex;
1680 pdpindex = ptepindex >> NPDPEPGSHIFT;
1681 pml4index = pdpindex >> NPML4EPGSHIFT;
1684 * Locate the PDP page in the PML4, then the PD page in
1685 * the PDP. If either does not exist we simply recurse
1688 * We can just recurse on the PD page as it will recurse
1689 * on the PDP if necessary.
1691 pml4 = &pmap->pm_pml4[pml4index];
1692 if ((*pml4 & PG_V) == 0) {
1693 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1694 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1695 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1697 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1698 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1699 if ((*pdp & PG_V) == 0) {
1700 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1702 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1708 * Now fill in the pte in the PD. If the pte already exists
1709 * (again, if we raced the grab), unhold pdpg and unwire
1710 * m, returning a held m.
1712 * pdpg is left held (hold_count is incremented for
1713 * each PT in the PD).
1715 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1716 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1718 vm_page_unhold(pdpg);
1719 if (--m->wire_count == 0)
1720 --vmstats.v_wire_count;
1724 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1728 * We successfully loaded a PDP, PD, or PTE. Set the page table hint,
1729 * valid bits, mapped flag, unbusy, and we're done.
1731 pmap->pm_ptphint = m;
1732 ++pmap->pm_stats.resident_count;
1735 m->valid = VM_PAGE_BITS_ALL;
1736 vm_page_flag_clear(m, PG_ZERO);
1738 vm_page_flag_set(m, PG_MAPPED);
1746 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1748 vm_pindex_t ptepindex;
1753 * Calculate pagetable page index
1755 ptepindex = pmap_pde_pindex(va);
1758 * Get the page directory entry
1760 pd = pmap_pde(pmap, va);
1763 * This supports switching from a 2MB page to a
1766 if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1767 panic("no promotion/demotion yet");
1775 * If the page table page is mapped, we just increment the
1776 * hold count, and activate it.
1778 if (pd != NULL && (*pd & PG_V) != 0) {
1779 /* YYY hint is used here on i386 */
1780 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1781 pmap->pm_ptphint = m;
1786 * Here if the pte page isn't mapped, or if it has been deallocated.
1788 return _pmap_allocpte(pmap, ptepindex);
1792 /***************************************************
1793 * Pmap allocation/deallocation routines.
1794 ***************************************************/
1797 * Release any resources held by the given physical map.
1798 * Called when a pmap initialized by pmap_pinit is being released.
1799 * Should only be called if the map contains no valid mappings.
1801 static int pmap_release_callback(struct vm_page *p, void *data);
1804 pmap_release(struct pmap *pmap)
1806 vm_object_t object = pmap->pm_pteobj;
1807 struct rb_vm_page_scan_info info;
1809 KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active));
1810 #if defined(DIAGNOSTIC)
1811 if (object->ref_count != 1)
1812 panic("pmap_release: pteobj reference count != 1");
1816 info.object = object;
1818 lwkt_gettoken(&vm_token);
1819 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1826 info.limit = object->generation;
1828 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1829 pmap_release_callback, &info);
1830 if (info.error == 0 && info.mpte) {
1831 if (!pmap_release_free_page(pmap, info.mpte))
1835 } while (info.error);
1836 lwkt_reltoken(&vm_token);
1841 pmap_release_callback(struct vm_page *p, void *data)
1843 struct rb_vm_page_scan_info *info = data;
1845 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1849 if (!pmap_release_free_page(info->pmap, p)) {
1853 if (info->object->generation != info->limit) {
1861 * Grow the number of kernel page table entries, if needed.
1863 * This routine is always called to validate any address space
1864 * beyond KERNBASE (for kldloads). kernel_vm_end only governs the address
1865 * space below KERNBASE.
1868 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
1871 vm_offset_t ptppaddr;
1873 pd_entry_t *pde, newpdir;
1875 int update_kernel_vm_end;
1878 lwkt_gettoken(&vm_token);
1881 * bootstrap kernel_vm_end on first real VM use
1883 if (kernel_vm_end == 0) {
1884 kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
1886 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1887 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1888 ~(PAGE_SIZE * NPTEPG - 1);
1890 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1891 kernel_vm_end = kernel_map.max_offset;
1898 * Fill in the gaps. kernel_vm_end is only adjusted for ranges
1899 * below KERNBASE. Ranges above KERNBASE are kldloaded and we
1900 * do not want to force-fill 128G worth of page tables.
1902 if (kstart < KERNBASE) {
1903 if (kstart > kernel_vm_end)
1904 kstart = kernel_vm_end;
1905 KKASSERT(kend <= KERNBASE);
1906 update_kernel_vm_end = 1;
1908 update_kernel_vm_end = 0;
1911 kstart = rounddown2(kstart, PAGE_SIZE * NPTEPG);
1912 kend = roundup2(kend, PAGE_SIZE * NPTEPG);
1914 if (kend - 1 >= kernel_map.max_offset)
1915 kend = kernel_map.max_offset;
1917 while (kstart < kend) {
1918 pde = pmap_pde(&kernel_pmap, kstart);
1920 /* We need a new PDP entry */
1921 nkpg = vm_page_alloc(kptobj, nkpt,
1924 VM_ALLOC_INTERRUPT);
1926 panic("pmap_growkernel: no memory to grow "
1929 paddr = VM_PAGE_TO_PHYS(nkpg);
1930 if ((nkpg->flags & PG_ZERO) == 0)
1931 pmap_zero_page(paddr);
1932 vm_page_flag_clear(nkpg, PG_ZERO);
1933 newpdp = (pdp_entry_t)
1934 (paddr | PG_V | PG_RW | PG_A | PG_M);
1935 *pmap_pdpe(&kernel_pmap, kstart) = newpdp;
1937 continue; /* try again */
1939 if ((*pde & PG_V) != 0) {
1940 kstart = (kstart + PAGE_SIZE * NPTEPG) &
1941 ~(PAGE_SIZE * NPTEPG - 1);
1942 if (kstart - 1 >= kernel_map.max_offset) {
1943 kstart = kernel_map.max_offset;
1950 * This index is bogus, but out of the way
1952 nkpg = vm_page_alloc(kptobj, nkpt,
1955 VM_ALLOC_INTERRUPT);
1957 panic("pmap_growkernel: no memory to grow kernel");
1960 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1961 pmap_zero_page(ptppaddr);
1962 vm_page_flag_clear(nkpg, PG_ZERO);
1963 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1964 *pmap_pde(&kernel_pmap, kstart) = newpdir;
1967 kstart = (kstart + PAGE_SIZE * NPTEPG) &
1968 ~(PAGE_SIZE * NPTEPG - 1);
1970 if (kstart - 1 >= kernel_map.max_offset) {
1971 kstart = kernel_map.max_offset;
1977 * Only update kernel_vm_end for areas below KERNBASE.
1979 if (update_kernel_vm_end && kernel_vm_end < kstart)
1980 kernel_vm_end = kstart;
1982 lwkt_reltoken(&vm_token);
1987 * Retire the given physical map from service.
1988 * Should only be called if the map contains
1989 * no valid mappings.
1992 pmap_destroy(pmap_t pmap)
1999 lwkt_gettoken(&vm_token);
2000 count = --pmap->pm_count;
2003 panic("destroying a pmap is not yet implemented");
2005 lwkt_reltoken(&vm_token);
2009 * Add a reference to the specified pmap.
2012 pmap_reference(pmap_t pmap)
2015 lwkt_gettoken(&vm_token);
2017 lwkt_reltoken(&vm_token);
2021 /***************************************************
2022 * page management routines.
2023 ***************************************************/
2026 * free the pv_entry back to the free list. This function may be
2027 * called from an interrupt.
2031 free_pv_entry(pv_entry_t pv)
2034 KKASSERT(pv_entry_count >= 0);
2039 * get a new pv_entry, allocating a block from the system
2040 * when needed. This function may be called from an interrupt.
2047 if (pv_entry_high_water &&
2048 (pv_entry_count > pv_entry_high_water) &&
2049 (pmap_pagedaemon_waken == 0)) {
2050 pmap_pagedaemon_waken = 1;
2051 wakeup(&vm_pages_needed);
2053 return zalloc(pvzone);
2057 * This routine is very drastic, but can save the system
2065 static int warningdone=0;
2067 if (pmap_pagedaemon_waken == 0)
2069 lwkt_gettoken(&vm_token);
2070 if (warningdone < 5) {
2071 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
2075 for(i = 0; i < vm_page_array_size; i++) {
2076 m = &vm_page_array[i];
2077 if (m->wire_count || m->hold_count || m->busy ||
2078 (m->flags & PG_BUSY))
2082 pmap_pagedaemon_waken = 0;
2083 lwkt_reltoken(&vm_token);
2088 * If it is the first entry on the list, it is actually
2089 * in the header and we must copy the following entry up
2090 * to the header. Otherwise we must search the list for
2091 * the entry. In either case we free the now unused entry.
2095 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
2096 vm_offset_t va, pmap_inval_info_t info)
2102 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
2103 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2104 if (pmap == pv->pv_pmap && va == pv->pv_va)
2108 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
2109 if (va == pv->pv_va)
2117 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2118 m->md.pv_list_count--;
2119 m->object->agg_pv_list_count--;
2120 KKASSERT(m->md.pv_list_count >= 0);
2121 if (TAILQ_EMPTY(&m->md.pv_list))
2122 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2123 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2124 ++pmap->pm_generation;
2125 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
2133 * Create a pv entry for page at pa for
2138 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
2143 pv = get_pv_entry();
2148 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
2149 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2150 ++pmap->pm_generation;
2151 m->md.pv_list_count++;
2152 m->object->agg_pv_list_count++;
2158 * pmap_remove_pte: do the things to unmap a page in a process
2162 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
2163 pmap_inval_info_t info)
2168 pmap_inval_interlock(info, pmap, va);
2169 oldpte = pte_load_clear(ptq);
2170 pmap_inval_deinterlock(info, pmap);
2172 pmap->pm_stats.wired_count -= 1;
2174 * Machines that don't support invlpg, also don't support
2175 * PG_G. XXX PG_G is disabled for SMP so don't worry about
2179 cpu_invlpg((void *)va);
2180 KKASSERT(pmap->pm_stats.resident_count > 0);
2181 --pmap->pm_stats.resident_count;
2182 if (oldpte & PG_MANAGED) {
2183 m = PHYS_TO_VM_PAGE(oldpte);
2184 if (oldpte & PG_M) {
2185 #if defined(PMAP_DIAGNOSTIC)
2186 if (pmap_nw_modified((pt_entry_t) oldpte)) {
2188 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2192 if (pmap_track_modified(va))
2196 vm_page_flag_set(m, PG_REFERENCED);
2197 return pmap_remove_entry(pmap, m, va, info);
2199 return pmap_unuse_pt(pmap, va, NULL, info);
2208 * Remove a single page from a process address space.
2210 * This function may not be called from an interrupt if the pmap is
2215 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
2219 pte = pmap_pte(pmap, va);
2222 if ((*pte & PG_V) == 0)
2224 pmap_remove_pte(pmap, pte, va, info);
2230 * Remove the given range of addresses from the specified map.
2232 * It is assumed that the start and end are properly
2233 * rounded to the page size.
2235 * This function may not be called from an interrupt if the pmap is
2239 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2241 vm_offset_t va_next;
2242 pml4_entry_t *pml4e;
2244 pd_entry_t ptpaddr, *pde;
2246 struct pmap_inval_info info;
2251 lwkt_gettoken(&vm_token);
2252 if (pmap->pm_stats.resident_count == 0) {
2253 lwkt_reltoken(&vm_token);
2257 pmap_inval_init(&info);
2260 * special handling of removing one page. a very
2261 * common operation and easy to short circuit some
2264 if (sva + PAGE_SIZE == eva) {
2265 pde = pmap_pde(pmap, sva);
2266 if (pde && (*pde & PG_PS) == 0) {
2267 pmap_remove_page(pmap, sva, &info);
2268 pmap_inval_done(&info);
2269 lwkt_reltoken(&vm_token);
2274 for (; sva < eva; sva = va_next) {
2275 pml4e = pmap_pml4e(pmap, sva);
2276 if ((*pml4e & PG_V) == 0) {
2277 va_next = (sva + NBPML4) & ~PML4MASK;
2283 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2284 if ((*pdpe & PG_V) == 0) {
2285 va_next = (sva + NBPDP) & ~PDPMASK;
2292 * Calculate index for next page table.
2294 va_next = (sva + NBPDR) & ~PDRMASK;
2298 pde = pmap_pdpe_to_pde(pdpe, sva);
2302 * Weed out invalid mappings.
2308 * Check for large page.
2310 if ((ptpaddr & PG_PS) != 0) {
2311 /* JG FreeBSD has more complex treatment here */
2312 pmap_inval_interlock(&info, pmap, -1);
2314 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2315 pmap_inval_deinterlock(&info, pmap);
2320 * Limit our scan to either the end of the va represented
2321 * by the current page table page, or to the end of the
2322 * range being removed.
2328 * NOTE: pmap_remove_pte() can block.
2330 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2334 if (pmap_remove_pte(pmap, pte, sva, &info))
2338 pmap_inval_done(&info);
2339 lwkt_reltoken(&vm_token);
2345 * Removes this physical page from all physical maps in which it resides.
2346 * Reflects back modify bits to the pager.
2348 * This routine may not be called from an interrupt.
2353 pmap_remove_all(vm_page_t m)
2355 struct pmap_inval_info info;
2356 pt_entry_t *pte, tpte;
2359 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2362 lwkt_gettoken(&vm_token);
2363 pmap_inval_init(&info);
2365 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2366 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2367 --pv->pv_pmap->pm_stats.resident_count;
2369 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2370 pmap_inval_interlock(&info, pv->pv_pmap, pv->pv_va);
2371 tpte = pte_load_clear(pte);
2373 pv->pv_pmap->pm_stats.wired_count--;
2374 pmap_inval_deinterlock(&info, pv->pv_pmap);
2376 vm_page_flag_set(m, PG_REFERENCED);
2379 * Update the vm_page_t clean and reference bits.
2382 #if defined(PMAP_DIAGNOSTIC)
2383 if (pmap_nw_modified(tpte)) {
2385 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2389 if (pmap_track_modified(pv->pv_va))
2392 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2393 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2394 ++pv->pv_pmap->pm_generation;
2395 m->md.pv_list_count--;
2396 m->object->agg_pv_list_count--;
2397 KKASSERT(m->md.pv_list_count >= 0);
2398 if (TAILQ_EMPTY(&m->md.pv_list))
2399 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2400 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2404 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2405 pmap_inval_done(&info);
2406 lwkt_reltoken(&vm_token);
2412 * Set the physical protection on the specified range of this map
2415 * This function may not be called from an interrupt if the map is
2416 * not the kernel_pmap.
2419 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2421 vm_offset_t va_next;
2422 pml4_entry_t *pml4e;
2424 pd_entry_t ptpaddr, *pde;
2426 pmap_inval_info info;
2428 /* JG review for NX */
2433 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2434 pmap_remove(pmap, sva, eva);
2438 if (prot & VM_PROT_WRITE)
2441 lwkt_gettoken(&vm_token);
2442 pmap_inval_init(&info);
2444 for (; sva < eva; sva = va_next) {
2446 pml4e = pmap_pml4e(pmap, sva);
2447 if ((*pml4e & PG_V) == 0) {
2448 va_next = (sva + NBPML4) & ~PML4MASK;
2454 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2455 if ((*pdpe & PG_V) == 0) {
2456 va_next = (sva + NBPDP) & ~PDPMASK;
2462 va_next = (sva + NBPDR) & ~PDRMASK;
2466 pde = pmap_pdpe_to_pde(pdpe, sva);
2470 * Check for large page.
2472 if ((ptpaddr & PG_PS) != 0) {
2473 pmap_inval_interlock(&info, pmap, -1);
2474 *pde &= ~(PG_M|PG_RW);
2475 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2476 pmap_inval_deinterlock(&info, pmap);
2481 * Weed out invalid mappings. Note: we assume that the page
2482 * directory table is always allocated, and in kernel virtual.
2490 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2497 * XXX non-optimal. Note also that there can be
2498 * no pmap_inval_flush() calls until after we modify
2499 * ptbase[sindex] (or otherwise we have to do another
2500 * pmap_inval_add() call).
2502 pmap_inval_interlock(&info, pmap, sva);
2506 if ((pbits & PG_V) == 0) {
2507 pmap_inval_deinterlock(&info, pmap);
2510 if (pbits & PG_MANAGED) {
2513 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2514 vm_page_flag_set(m, PG_REFERENCED);
2518 if (pmap_track_modified(sva)) {
2520 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2527 if (pbits != cbits &&
2528 !atomic_cmpset_long(pte, pbits, cbits)) {
2531 pmap_inval_deinterlock(&info, pmap);
2534 pmap_inval_done(&info);
2535 lwkt_reltoken(&vm_token);
2539 * Insert the given physical page (p) at
2540 * the specified virtual address (v) in the
2541 * target physical map with the protection requested.
2543 * If specified, the page will be wired down, meaning
2544 * that the related pte can not be reclaimed.
2546 * NB: This is the only routine which MAY NOT lazy-evaluate
2547 * or lose information. That is, this routine must actually
2548 * insert this page into the given map NOW.
2551 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2558 pt_entry_t origpte, newpte;
2560 pmap_inval_info info;
2565 va = trunc_page(va);
2566 #ifdef PMAP_DIAGNOSTIC
2568 panic("pmap_enter: toobig");
2569 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2570 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va);
2572 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2573 kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n");
2575 db_print_backtrace();
2578 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2579 kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n");
2581 db_print_backtrace();
2585 lwkt_gettoken(&vm_token);
2588 * In the case that a page table page is not
2589 * resident, we are creating it here.
2591 if (va < VM_MAX_USER_ADDRESS)
2592 mpte = pmap_allocpte(pmap, va);
2596 pmap_inval_init(&info);
2597 pde = pmap_pde(pmap, va);
2598 if (pde != NULL && (*pde & PG_V) != 0) {
2599 if ((*pde & PG_PS) != 0)
2600 panic("pmap_enter: attempted pmap_enter on 2MB page");
2601 pte = pmap_pde_to_pte(pde, va);
2603 panic("pmap_enter: invalid page directory va=%#lx", va);
2605 KKASSERT(pte != NULL);
2606 pa = VM_PAGE_TO_PHYS(m);
2608 opa = origpte & PG_FRAME;
2611 * Mapping has not changed, must be protection or wiring change.
2613 if (origpte && (opa == pa)) {
2615 * Wiring change, just update stats. We don't worry about
2616 * wiring PT pages as they remain resident as long as there
2617 * are valid mappings in them. Hence, if a user page is wired,
2618 * the PT page will be also.
2620 if (wired && ((origpte & PG_W) == 0))
2621 pmap->pm_stats.wired_count++;
2622 else if (!wired && (origpte & PG_W))
2623 pmap->pm_stats.wired_count--;
2625 #if defined(PMAP_DIAGNOSTIC)
2626 if (pmap_nw_modified(origpte)) {
2628 "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2634 * Remove the extra pte reference. Note that we cannot
2635 * optimize the RO->RW case because we have adjusted the
2636 * wiring count above and may need to adjust the wiring
2643 * We might be turning off write access to the page,
2644 * so we go ahead and sense modify status.
2646 if (origpte & PG_MANAGED) {
2647 if ((origpte & PG_M) && pmap_track_modified(va)) {
2649 om = PHYS_TO_VM_PAGE(opa);
2653 KKASSERT(m->flags & PG_MAPPED);
2658 * Mapping has changed, invalidate old range and fall through to
2659 * handle validating new mapping.
2663 err = pmap_remove_pte(pmap, pte, va, &info);
2665 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2667 opa = origpte & PG_FRAME;
2669 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
2675 * Enter on the PV list if part of our managed memory. Note that we
2676 * raise IPL while manipulating pv_table since pmap_enter can be
2677 * called at interrupt time.
2679 if (pmap_initialized &&
2680 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2681 pmap_insert_entry(pmap, va, mpte, m);
2683 vm_page_flag_set(m, PG_MAPPED);
2687 * Increment counters
2689 ++pmap->pm_stats.resident_count;
2691 pmap->pm_stats.wired_count++;
2695 * Now validate mapping with desired protection/wiring.
2697 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V);
2701 if (va < VM_MAX_USER_ADDRESS)
2703 if (pmap == &kernel_pmap)
2707 * if the mapping or permission bits are different, we need
2708 * to update the pte.
2710 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2711 pmap_inval_interlock(&info, pmap, va);
2712 *pte = newpte | PG_A;
2713 pmap_inval_deinterlock(&info, pmap);
2715 vm_page_flag_set(m, PG_WRITEABLE);
2717 KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
2718 pmap_inval_done(&info);
2719 lwkt_reltoken(&vm_token);
2723 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2724 * This code also assumes that the pmap has no pre-existing entry for this
2727 * This code currently may only be used on user pmaps, not kernel_pmap.
2730 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2735 vm_pindex_t ptepindex;
2737 pmap_inval_info info;
2739 lwkt_gettoken(&vm_token);
2740 pmap_inval_init(&info);
2742 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2743 kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n");
2745 db_print_backtrace();
2748 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2749 kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n");
2751 db_print_backtrace();
2755 KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */
2758 * Calculate the page table page (mpte), allocating it if necessary.
2760 * A held page table page (mpte), or NULL, is passed onto the
2761 * section following.
2763 if (va < VM_MAX_USER_ADDRESS) {
2765 * Calculate pagetable page index
2767 ptepindex = pmap_pde_pindex(va);
2771 * Get the page directory entry
2773 ptepa = pmap_pde(pmap, va);
2776 * If the page table page is mapped, we just increment
2777 * the hold count, and activate it.
2779 if (ptepa && (*ptepa & PG_V) != 0) {
2781 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2782 // if (pmap->pm_ptphint &&
2783 // (pmap->pm_ptphint->pindex == ptepindex)) {
2784 // mpte = pmap->pm_ptphint;
2786 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2787 pmap->pm_ptphint = mpte;
2792 mpte = _pmap_allocpte(pmap, ptepindex);
2794 } while (mpte == NULL);
2797 /* this code path is not yet used */
2801 * With a valid (and held) page directory page, we can just use
2802 * vtopte() to get to the pte. If the pte is already present
2803 * we do not disturb it.
2808 pmap_unwire_pte_hold(pmap, va, mpte, &info);
2809 pa = VM_PAGE_TO_PHYS(m);
2810 KKASSERT(((*pte ^ pa) & PG_FRAME) == 0);
2811 pmap_inval_done(&info);
2812 lwkt_reltoken(&vm_token);
2817 * Enter on the PV list if part of our managed memory
2819 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2820 pmap_insert_entry(pmap, va, mpte, m);
2821 vm_page_flag_set(m, PG_MAPPED);
2825 * Increment counters
2827 ++pmap->pm_stats.resident_count;
2829 pa = VM_PAGE_TO_PHYS(m);
2832 * Now validate mapping with RO protection
2834 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2835 *pte = pa | PG_V | PG_U;
2837 *pte = pa | PG_V | PG_U | PG_MANAGED;
2838 /* pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */
2839 pmap_inval_done(&info);
2840 lwkt_reltoken(&vm_token);
2844 * Make a temporary mapping for a physical address. This is only intended
2845 * to be used for panic dumps.
2847 /* JG Needed on x86_64? */
2849 pmap_kenter_temporary(vm_paddr_t pa, int i)
2851 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2852 return ((void *)crashdumpmap);
2855 #define MAX_INIT_PT (96)
2858 * This routine preloads the ptes for a given object into the specified pmap.
2859 * This eliminates the blast of soft faults on process startup and
2860 * immediately after an mmap.
2862 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2865 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2866 vm_object_t object, vm_pindex_t pindex,
2867 vm_size_t size, int limit)
2869 struct rb_vm_page_scan_info info;
2874 * We can't preinit if read access isn't set or there is no pmap
2877 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2881 * We can't preinit if the pmap is not the current pmap
2883 lp = curthread->td_lwp;
2884 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2887 psize = x86_64_btop(size);
2889 if ((object->type != OBJT_VNODE) ||
2890 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2891 (object->resident_page_count > MAX_INIT_PT))) {
2895 if (psize + pindex > object->size) {
2896 if (object->size < pindex)
2898 psize = object->size - pindex;
2905 * Use a red-black scan to traverse the requested range and load
2906 * any valid pages found into the pmap.
2908 * We cannot safely scan the object's memq unless we are in a
2909 * critical section since interrupts can remove pages from objects.
2911 info.start_pindex = pindex;
2912 info.end_pindex = pindex + psize - 1;
2919 lwkt_gettoken(&vm_token);
2920 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2921 pmap_object_init_pt_callback, &info);
2922 lwkt_reltoken(&vm_token);
2928 pmap_object_init_pt_callback(vm_page_t p, void *data)
2930 struct rb_vm_page_scan_info *info = data;
2931 vm_pindex_t rel_index;
2933 * don't allow an madvise to blow away our really
2934 * free pages allocating pv entries.
2936 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2937 vmstats.v_free_count < vmstats.v_free_reserved) {
2940 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2941 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2942 if ((p->queue - p->pc) == PQ_CACHE)
2943 vm_page_deactivate(p);
2945 rel_index = p->pindex - info->start_pindex;
2946 pmap_enter_quick(info->pmap,
2947 info->addr + x86_64_ptob(rel_index), p);
2954 * Return TRUE if the pmap is in shape to trivially
2955 * pre-fault the specified address.
2957 * Returns FALSE if it would be non-trivial or if a
2958 * pte is already loaded into the slot.
2961 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2967 lwkt_gettoken(&vm_token);
2968 pde = pmap_pde(pmap, addr);
2969 if (pde == NULL || *pde == 0) {
2973 ret = (*pte) ? 0 : 1;
2975 lwkt_reltoken(&vm_token);
2980 * Routine: pmap_change_wiring
2981 * Function: Change the wiring attribute for a map/virtual-address
2983 * In/out conditions:
2984 * The mapping must already exist in the pmap.
2987 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2994 lwkt_gettoken(&vm_token);
2995 pte = pmap_pte(pmap, va);
2997 if (wired && !pmap_pte_w(pte))
2998 pmap->pm_stats.wired_count++;
2999 else if (!wired && pmap_pte_w(pte))
3000 pmap->pm_stats.wired_count--;
3003 * Wiring is not a hardware characteristic so there is no need to
3004 * invalidate TLB. However, in an SMP environment we must use
3005 * a locked bus cycle to update the pte (if we are not using
3006 * the pmap_inval_*() API that is)... it's ok to do this for simple
3011 atomic_set_long(pte, PG_W);
3013 atomic_clear_long(pte, PG_W);
3016 atomic_set_long_nonlocked(pte, PG_W);
3018 atomic_clear_long_nonlocked(pte, PG_W);
3020 lwkt_reltoken(&vm_token);
3026 * Copy the range specified by src_addr/len
3027 * from the source map to the range dst_addr/len
3028 * in the destination map.
3030 * This routine is only advisory and need not do anything.
3033 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3034 vm_size_t len, vm_offset_t src_addr)
3038 pmap_inval_info info;
3040 vm_offset_t end_addr = src_addr + len;
3042 pd_entry_t src_frame, dst_frame;
3045 if (dst_addr != src_addr)
3048 src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3049 if (src_frame != (PTDpde & PG_FRAME)) {
3053 dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3054 if (dst_frame != (APTDpde & PG_FRAME)) {
3055 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
3056 /* The page directory is not shared between CPUs */
3060 pmap_inval_init(&info);
3061 pmap_inval_add(&info, dst_pmap, -1);
3062 pmap_inval_add(&info, src_pmap, -1);
3065 * critical section protection is required to maintain the page/object
3066 * association, interrupts can free pages and remove them from
3070 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3071 pt_entry_t *src_pte, *dst_pte;
3072 vm_page_t dstmpte, srcmpte;
3073 vm_offset_t srcptepaddr;
3074 vm_pindex_t ptepindex;
3076 if (addr >= UPT_MIN_ADDRESS)
3077 panic("pmap_copy: invalid to pmap_copy page tables\n");
3080 * Don't let optional prefaulting of pages make us go
3081 * way below the low water mark of free pages or way
3082 * above high water mark of used pv entries.
3084 if (vmstats.v_free_count < vmstats.v_free_reserved ||
3085 pv_entry_count > pv_entry_high_water)
3088 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
3089 ptepindex = addr >> PDRSHIFT;
3092 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
3094 if (srcptepaddr == 0)
3097 if (srcptepaddr & PG_PS) {
3099 if (dst_pmap->pm_pdir[ptepindex] == 0) {
3100 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
3101 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3107 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
3108 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
3109 (srcmpte->flags & PG_BUSY)) {
3113 if (pdnxt > end_addr)
3116 src_pte = vtopte(addr);
3118 dst_pte = avtopte(addr);
3120 while (addr < pdnxt) {
3125 * we only virtual copy managed pages
3127 if ((ptetemp & PG_MANAGED) != 0) {
3129 * We have to check after allocpte for the
3130 * pte still being around... allocpte can
3133 * pmap_allocpte() can block. If we lose
3134 * our page directory mappings we stop.
3136 dstmpte = pmap_allocpte(dst_pmap, addr);
3139 if (src_frame != (PTDpde & PG_FRAME) ||
3140 dst_frame != (APTDpde & PG_FRAME)
3142 kprintf("WARNING: pmap_copy: detected and corrected race\n");
3143 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3145 } else if ((*dst_pte == 0) &&
3146 (ptetemp = *src_pte) != 0 &&
3147 (ptetemp & PG_MANAGED)) {
3149 * Clear the modified and
3150 * accessed (referenced) bits
3153 m = PHYS_TO_VM_PAGE(ptetemp);
3154 *dst_pte = ptetemp & ~(PG_M | PG_A);
3155 ++dst_pmap->pm_stats.resident_count;
3156 pmap_insert_entry(dst_pmap, addr,
3158 KKASSERT(m->flags & PG_MAPPED);
3160 kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n");
3161 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3165 if (dstmpte->hold_count >= srcmpte->hold_count)
3175 pmap_inval_done(&info);
3182 * Zero the specified physical page.
3184 * This function may be called from an interrupt and no locking is
3188 pmap_zero_page(vm_paddr_t phys)
3190 vm_offset_t va = PHYS_TO_DMAP(phys);
3192 pagezero((void *)va);
3196 * pmap_page_assertzero:
3198 * Assert that a page is empty, panic if it isn't.
3201 pmap_page_assertzero(vm_paddr_t phys)
3203 vm_offset_t va = PHYS_TO_DMAP(phys);
3206 for (i = 0; i < PAGE_SIZE; i += sizeof(long)) {
3207 if (*(long *)((char *)va + i) != 0) {
3208 panic("pmap_page_assertzero() @ %p not zero!\n",
3209 (void *)(intptr_t)va);
3217 * Zero part of a physical page by mapping it into memory and clearing
3218 * its contents with bzero.
3220 * off and size may not cover an area beyond a single hardware page.
3223 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3225 vm_offset_t virt = PHYS_TO_DMAP(phys);
3227 bzero((char *)virt + off, size);
3233 * Copy the physical page from the source PA to the target PA.
3234 * This function may be called from an interrupt. No locking
3238 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3240 vm_offset_t src_virt, dst_virt;
3242 src_virt = PHYS_TO_DMAP(src);
3243 dst_virt = PHYS_TO_DMAP(dst);
3244 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
3248 * pmap_copy_page_frag:
3250 * Copy the physical page from the source PA to the target PA.
3251 * This function may be called from an interrupt. No locking
3255 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3257 vm_offset_t src_virt, dst_virt;
3259 src_virt = PHYS_TO_DMAP(src);
3260 dst_virt = PHYS_TO_DMAP(dst);
3262 bcopy((char *)src_virt + (src & PAGE_MASK),
3263 (char *)dst_virt + (dst & PAGE_MASK),
3268 * Returns true if the pmap's pv is one of the first
3269 * 16 pvs linked to from this page. This count may
3270 * be changed upwards or downwards in the future; it
3271 * is only necessary that true be returned for a small
3272 * subset of pmaps for proper page aging.
3275 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3280 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3284 lwkt_gettoken(&vm_token);
3286 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3287 if (pv->pv_pmap == pmap) {
3288 lwkt_reltoken(&vm_token);
3296 lwkt_reltoken(&vm_token);
3302 * Remove all pages from specified address space
3303 * this aids process exit speeds. Also, this code
3304 * is special cased for current process only, but
3305 * can have the more generic (and slightly slower)
3306 * mode enabled. This is much faster than pmap_remove
3307 * in the case of running down an entire address space.
3310 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3313 pt_entry_t *pte, tpte;
3316 pmap_inval_info info;
3318 int save_generation;
3320 lp = curthread->td_lwp;
3321 if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
3326 lwkt_gettoken(&vm_token);
3327 pmap_inval_init(&info);
3328 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3329 if (pv->pv_va >= eva || pv->pv_va < sva) {
3330 npv = TAILQ_NEXT(pv, pv_plist);
3334 KKASSERT(pmap == pv->pv_pmap);
3337 pte = vtopte(pv->pv_va);
3339 pte = pmap_pte_quick(pmap, pv->pv_va);
3340 pmap_inval_interlock(&info, pmap, pv->pv_va);
3343 * We cannot remove wired pages from a process' mapping
3347 pmap_inval_deinterlock(&info, pmap);
3348 npv = TAILQ_NEXT(pv, pv_plist);
3351 tpte = pte_load_clear(pte);
3353 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3355 KASSERT(m < &vm_page_array[vm_page_array_size],
3356 ("pmap_remove_pages: bad tpte %lx", tpte));
3358 KKASSERT(pmap->pm_stats.resident_count > 0);
3359 --pmap->pm_stats.resident_count;
3360 pmap_inval_deinterlock(&info, pmap);
3363 * Update the vm_page_t clean and reference bits.
3369 npv = TAILQ_NEXT(pv, pv_plist);
3370 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
3371 save_generation = ++pmap->pm_generation;
3373 m->md.pv_list_count--;
3374 m->object->agg_pv_list_count--;
3375 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3376 if (TAILQ_EMPTY(&m->md.pv_list))
3377 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3379 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
3383 * Restart the scan if we blocked during the unuse or free
3384 * calls and other removals were made.
3386 if (save_generation != pmap->pm_generation) {
3387 kprintf("Warning: pmap_remove_pages race-A avoided\n");
3388 npv = TAILQ_FIRST(&pmap->pm_pvlist);
3391 pmap_inval_done(&info);
3392 lwkt_reltoken(&vm_token);
3396 * pmap_testbit tests bits in pte's
3397 * note that the testbit/clearbit routines are inline,
3398 * and a lot of things compile-time evaluate.
3402 pmap_testbit(vm_page_t m, int bit)
3407 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3410 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3415 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3417 * if the bit being tested is the modified bit, then
3418 * mark clean_map and ptes as never
3421 if (bit & (PG_A|PG_M)) {
3422 if (!pmap_track_modified(pv->pv_va))
3426 #if defined(PMAP_DIAGNOSTIC)
3427 if (pv->pv_pmap == NULL) {
3428 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3432 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3443 * this routine is used to modify bits in ptes
3447 pmap_clearbit(vm_page_t m, int bit)
3449 struct pmap_inval_info info;
3454 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3457 pmap_inval_init(&info);
3460 * Loop over all current mappings setting/clearing as appropos If
3461 * setting RO do we need to clear the VAC?
3463 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3465 * don't write protect pager mappings
3468 if (!pmap_track_modified(pv->pv_va))
3472 #if defined(PMAP_DIAGNOSTIC)
3473 if (pv->pv_pmap == NULL) {
3474 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3480 * Careful here. We can use a locked bus instruction to
3481 * clear PG_A or PG_M safely but we need to synchronize
3482 * with the target cpus when we mess with PG_RW.
3484 * We do not have to force synchronization when clearing
3485 * PG_M even for PTEs generated via virtual memory maps,
3486 * because the virtual kernel will invalidate the pmap
3487 * entry when/if it needs to resynchronize the Modify bit.
3490 pmap_inval_interlock(&info, pv->pv_pmap, pv->pv_va);
3491 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3498 atomic_clear_long(pte, PG_M|PG_RW);
3501 * The cpu may be trying to set PG_M
3502 * simultaniously with our clearing
3505 if (!atomic_cmpset_long(pte, pbits,
3509 } else if (bit == PG_M) {
3511 * We could also clear PG_RW here to force
3512 * a fault on write to redetect PG_M for
3513 * virtual kernels, but it isn't necessary
3514 * since virtual kernels invalidate the pte
3515 * when they clear the VPTE_M bit in their
3516 * virtual page tables.
3518 atomic_clear_long(pte, PG_M);
3520 atomic_clear_long(pte, bit);
3524 pmap_inval_deinterlock(&info, pv->pv_pmap);
3526 pmap_inval_done(&info);
3530 * pmap_page_protect:
3532 * Lower the permission for all mappings to a given page.
3535 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3537 /* JG NX support? */
3538 if ((prot & VM_PROT_WRITE) == 0) {
3539 lwkt_gettoken(&vm_token);
3540 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3541 pmap_clearbit(m, PG_RW);
3542 vm_page_flag_clear(m, PG_WRITEABLE);
3546 lwkt_reltoken(&vm_token);
3551 pmap_phys_address(vm_pindex_t ppn)
3553 return (x86_64_ptob(ppn));
3557 * pmap_ts_referenced:
3559 * Return a count of reference bits for a page, clearing those bits.
3560 * It is not necessary for every reference bit to be cleared, but it
3561 * is necessary that 0 only be returned when there are truly no
3562 * reference bits set.
3564 * XXX: The exact number of bits to check and clear is a matter that
3565 * should be tested and standardized at some point in the future for
3566 * optimal aging of shared pages.
3569 pmap_ts_referenced(vm_page_t m)
3571 pv_entry_t pv, pvf, pvn;
3575 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3579 lwkt_gettoken(&vm_token);
3581 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3586 pvn = TAILQ_NEXT(pv, pv_list);
3589 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3590 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3593 if (!pmap_track_modified(pv->pv_va))
3596 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3598 if (pte && (*pte & PG_A)) {
3600 atomic_clear_long(pte, PG_A);
3602 atomic_clear_long_nonlocked(pte, PG_A);
3609 } while ((pv = pvn) != NULL && pv != pvf);
3611 lwkt_reltoken(&vm_token);
3620 * Return whether or not the specified physical page was modified
3621 * in any physical maps.
3624 pmap_is_modified(vm_page_t m)
3628 lwkt_gettoken(&vm_token);
3629 res = pmap_testbit(m, PG_M);
3630 lwkt_reltoken(&vm_token);
3635 * Clear the modify bits on the specified physical page.
3638 pmap_clear_modify(vm_page_t m)
3640 lwkt_gettoken(&vm_token);
3641 pmap_clearbit(m, PG_M);
3642 lwkt_reltoken(&vm_token);
3646 * pmap_clear_reference:
3648 * Clear the reference bit on the specified physical page.
3651 pmap_clear_reference(vm_page_t m)
3653 lwkt_gettoken(&vm_token);
3654 pmap_clearbit(m, PG_A);
3655 lwkt_reltoken(&vm_token);
3659 * Miscellaneous support routines follow
3664 i386_protection_init(void)
3668 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
3669 kp = protection_codes;
3670 for (prot = 0; prot < 8; prot++) {
3672 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3674 * Read access is also 0. There isn't any execute bit,
3675 * so just make it readable.
3677 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3678 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3679 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3682 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3683 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3684 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3685 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3693 * Map a set of physical memory pages into the kernel virtual
3694 * address space. Return a pointer to where it is mapped. This
3695 * routine is intended to be used for mapping device memory,
3698 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3702 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3704 vm_offset_t va, tmpva, offset;
3707 offset = pa & PAGE_MASK;
3708 size = roundup(offset + size, PAGE_SIZE);
3710 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3712 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3714 pa = pa & ~PAGE_MASK;
3715 for (tmpva = va; size > 0;) {
3716 pte = vtopte(tmpva);
3717 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3725 return ((void *)(va + offset));
3729 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
3731 vm_offset_t va, tmpva, offset;
3734 offset = pa & PAGE_MASK;
3735 size = roundup(offset + size, PAGE_SIZE);
3737 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3739 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3741 pa = pa & ~PAGE_MASK;
3742 for (tmpva = va; size > 0;) {
3743 pte = vtopte(tmpva);
3744 *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */
3752 return ((void *)(va + offset));
3756 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3758 vm_offset_t base, offset;
3760 base = va & ~PAGE_MASK;
3761 offset = va & PAGE_MASK;
3762 size = roundup(offset + size, PAGE_SIZE);
3763 pmap_qremove(va, size >> PAGE_SHIFT);
3764 kmem_free(&kernel_map, base, size);
3768 * perform the pmap work for mincore
3771 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3773 pt_entry_t *ptep, pte;
3777 lwkt_gettoken(&vm_token);
3778 ptep = pmap_pte(pmap, addr);
3780 if (ptep && (pte = *ptep) != 0) {
3783 val = MINCORE_INCORE;
3784 if ((pte & PG_MANAGED) == 0)
3787 pa = pte & PG_FRAME;
3789 m = PHYS_TO_VM_PAGE(pa);
3795 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3797 * Modified by someone
3799 else if (m->dirty || pmap_is_modified(m))
3800 val |= MINCORE_MODIFIED_OTHER;
3805 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3808 * Referenced by someone
3810 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3811 val |= MINCORE_REFERENCED_OTHER;
3812 vm_page_flag_set(m, PG_REFERENCED);
3816 lwkt_reltoken(&vm_token);
3821 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3822 * vmspace will be ref'd and the old one will be deref'd.
3824 * The vmspace for all lwps associated with the process will be adjusted
3825 * and cr3 will be reloaded if any lwp is the current lwp.
3828 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3830 struct vmspace *oldvm;
3834 oldvm = p->p_vmspace;
3835 if (oldvm != newvm) {
3836 p->p_vmspace = newvm;
3837 KKASSERT(p->p_nthreads == 1);
3838 lp = RB_ROOT(&p->p_lwp_tree);
3839 pmap_setlwpvm(lp, newvm);
3841 sysref_get(&newvm->vm_sysref);
3842 sysref_put(&oldvm->vm_sysref);
3849 * Set the vmspace for a LWP. The vmspace is almost universally set the
3850 * same as the process vmspace, but virtual kernels need to swap out contexts
3851 * on a per-lwp basis.
3854 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3856 struct vmspace *oldvm;
3860 oldvm = lp->lwp_vmspace;
3862 if (oldvm != newvm) {
3863 lp->lwp_vmspace = newvm;
3864 if (curthread->td_lwp == lp) {
3865 pmap = vmspace_pmap(newvm);
3867 atomic_set_int(&pmap->pm_active, mycpu->gd_cpumask);
3868 if (pmap->pm_active & CPUMASK_LOCK)
3869 pmap_interlock_wait(newvm);
3871 pmap->pm_active |= 1;
3873 #if defined(SWTCH_OPTIM_STATS)
3876 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
3877 curthread->td_pcb->pcb_cr3 |= PG_RW | PG_U | PG_V;
3878 load_cr3(curthread->td_pcb->pcb_cr3);
3879 pmap = vmspace_pmap(oldvm);
3881 atomic_clear_int(&pmap->pm_active, mycpu->gd_cpumask);
3883 pmap->pm_active &= ~1;
3893 * Called when switching to a locked pmap
3896 pmap_interlock_wait(struct vmspace *vm)
3898 struct pmap *pmap = &vm->vm_pmap;
3900 if (pmap->pm_active & CPUMASK_LOCK) {
3901 while (pmap->pm_active & CPUMASK_LOCK) {
3904 lwkt_process_ipiq();
3912 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3915 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3919 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3924 * Used by kmalloc/kfree, page already exists at va
3927 pmap_kvtom(vm_offset_t va)
3929 return(PHYS_TO_VM_PAGE(*vtopte(va) & PG_FRAME));