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 nkpt = (Maxmem * (sizeof(struct vm_page) * 2) + MSGBUF_SIZE) / NBPDR;
446 KPTbase = allocpages(firstaddr, nkpt);
447 KPTphys = allocpages(firstaddr, nkpt);
448 KPML4phys = allocpages(firstaddr, 1);
449 KPDPphys = allocpages(firstaddr, NKPML4E);
452 * Calculate the page directory base for KERNBASE,
453 * that is where we start populating the page table pages.
454 * Basically this is the end - 2.
456 KPDphys = allocpages(firstaddr, NKPDPE);
457 KPDbase = KPDphys + ((NKPDPE - (NPDPEPG - KPDPI)) << PAGE_SHIFT);
459 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
460 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
462 DMPDPphys = allocpages(firstaddr, NDMPML4E);
463 if ((amd_feature & AMDID_PAGE1GB) == 0)
464 DMPDphys = allocpages(firstaddr, ndmpdp);
465 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
468 * Fill in the underlying page table pages for the area around
469 * KERNBASE. This remaps low physical memory to KERNBASE.
471 * Read-only from zero to physfree
472 * XXX not fully used, underneath 2M pages
474 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
475 ((pt_entry_t *)KPTbase)[i] = i << PAGE_SHIFT;
476 ((pt_entry_t *)KPTbase)[i] |= PG_RW | PG_V | PG_G;
480 * Now map the initial kernel page tables. One block of page
481 * tables is placed at the beginning of kernel virtual memory,
482 * and another block is placed at KERNBASE to map the kernel binary,
483 * data, bss, and initial pre-allocations.
485 for (i = 0; i < nkpt; i++) {
486 ((pd_entry_t *)KPDbase)[i] = KPTbase + (i << PAGE_SHIFT);
487 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V;
489 for (i = 0; i < nkpt; i++) {
490 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
491 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
495 * Map from zero to end of allocations using 2M pages as an
496 * optimization. This will bypass some of the KPTBase pages
497 * above in the KERNBASE area.
499 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
500 ((pd_entry_t *)KPDbase)[i] = i << PDRSHIFT;
501 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V | PG_PS | PG_G;
505 * And connect up the PD to the PDP. The kernel pmap is expected
506 * to pre-populate all of its PDs. See NKPDPE in vmparam.h.
508 for (i = 0; i < NKPDPE; i++) {
509 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] =
510 KPDphys + (i << PAGE_SHIFT);
511 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] |=
515 /* Now set up the direct map space using either 2MB or 1GB pages */
516 /* Preset PG_M and PG_A because demotion expects it */
517 if ((amd_feature & AMDID_PAGE1GB) == 0) {
518 for (i = 0; i < NPDEPG * ndmpdp; i++) {
519 ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT;
520 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
523 /* And the direct map space's PDP */
524 for (i = 0; i < ndmpdp; i++) {
525 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
527 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
530 for (i = 0; i < ndmpdp; i++) {
531 ((pdp_entry_t *)DMPDPphys)[i] =
532 (vm_paddr_t)i << PDPSHIFT;
533 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
538 /* And recursively map PML4 to itself in order to get PTmap */
539 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
540 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
542 /* Connect the Direct Map slot up to the PML4 */
543 ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
544 ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
546 /* Connect the KVA slot up to the PML4 */
547 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
548 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
552 * Bootstrap the system enough to run with virtual memory.
554 * On the i386 this is called after mapping has already been enabled
555 * and just syncs the pmap module with what has already been done.
556 * [We can't call it easily with mapping off since the kernel is not
557 * mapped with PA == VA, hence we would have to relocate every address
558 * from the linked base (virtual) address "KERNBASE" to the actual
559 * (physical) address starting relative to 0]
562 pmap_bootstrap(vm_paddr_t *firstaddr)
566 struct mdglobaldata *gd;
569 KvaStart = VM_MIN_KERNEL_ADDRESS;
570 KvaEnd = VM_MAX_KERNEL_ADDRESS;
571 KvaSize = KvaEnd - KvaStart;
573 avail_start = *firstaddr;
576 * Create an initial set of page tables to run the kernel in.
578 create_pagetables(firstaddr);
580 virtual2_start = KvaStart;
581 virtual2_end = PTOV_OFFSET;
583 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
584 virtual_start = pmap_kmem_choose(virtual_start);
586 virtual_end = VM_MAX_KERNEL_ADDRESS;
588 /* XXX do %cr0 as well */
589 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
593 * Initialize protection array.
595 i386_protection_init();
598 * The kernel's pmap is statically allocated so we don't have to use
599 * pmap_create, which is unlikely to work correctly at this part of
600 * the boot sequence (XXX and which no longer exists).
602 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
603 kernel_pmap.pm_count = 1;
604 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
605 TAILQ_INIT(&kernel_pmap.pm_pvlist);
608 * Reserve some special page table entries/VA space for temporary
611 #define SYSMAP(c, p, v, n) \
612 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
618 * CMAP1/CMAP2 are used for zeroing and copying pages.
620 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
625 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
628 * ptvmmap is used for reading arbitrary physical pages via
631 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
634 * msgbufp is used to map the system message buffer.
635 * XXX msgbufmap is not used.
637 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
638 atop(round_page(MSGBUF_SIZE)))
645 * PG_G is terribly broken on SMP because we IPI invltlb's in some
646 * cases rather then invl1pg. Actually, I don't even know why it
647 * works under UP because self-referential page table mappings
652 if (cpu_feature & CPUID_PGE)
657 * Initialize the 4MB page size flag
661 * The 4MB page version of the initial
662 * kernel page mapping.
666 #if !defined(DISABLE_PSE)
667 if (cpu_feature & CPUID_PSE) {
670 * Note that we have enabled PSE mode
673 ptditmp = *(PTmap + x86_64_btop(KERNBASE));
674 ptditmp &= ~(NBPDR - 1);
675 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
680 * Enable the PSE mode. If we are SMP we can't do this
681 * now because the APs will not be able to use it when
684 load_cr4(rcr4() | CR4_PSE);
687 * We can do the mapping here for the single processor
688 * case. We simply ignore the old page table page from
692 * For SMP, we still need 4K pages to bootstrap APs,
693 * PSE will be enabled as soon as all APs are up.
695 PTD[KPTDI] = (pd_entry_t)ptditmp;
701 if (cpu_apic_address == 0)
702 panic("pmap_bootstrap: no local apic!");
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 * Initialize the pmap module.
742 * Called by vm_init, to initialize any structures that the pmap
743 * system needs to map virtual memory.
744 * pmap_init has been enhanced to support in a fairly consistant
745 * way, discontiguous physical memory.
754 * object for kernel page table pages
756 /* JG I think the number can be arbitrary */
757 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
760 * Allocate memory for random pmap data structures. Includes the
764 for(i = 0; i < vm_page_array_size; i++) {
767 m = &vm_page_array[i];
768 TAILQ_INIT(&m->md.pv_list);
769 m->md.pv_list_count = 0;
773 * init the pv free list
775 initial_pvs = vm_page_array_size;
776 if (initial_pvs < MINPV)
778 pvzone = &pvzone_store;
779 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
780 initial_pvs * sizeof (struct pv_entry));
781 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
785 * Now it is safe to enable pv_table recording.
787 pmap_initialized = TRUE;
789 lapic = pmap_mapdev_uncacheable(cpu_apic_address, sizeof(struct LAPIC));
794 * Initialize the address space (zone) for the pv_entries. Set a
795 * high water mark so that the system can recover from excessive
796 * numbers of pv entries.
801 int shpgperproc = PMAP_SHPGPERPROC;
803 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
804 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
805 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
806 pv_entry_high_water = 9 * (pv_entry_max / 10);
807 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
811 /***************************************************
812 * Low level helper routines.....
813 ***************************************************/
815 #if defined(PMAP_DIAGNOSTIC)
818 * This code checks for non-writeable/modified pages.
819 * This should be an invalid condition.
823 pmap_nw_modified(pt_entry_t pte)
825 if ((pte & (PG_M|PG_RW)) == PG_M)
834 * this routine defines the region(s) of memory that should
835 * not be tested for the modified bit.
839 pmap_track_modified(vm_offset_t va)
841 if ((va < clean_sva) || (va >= clean_eva))
848 * Extract the physical page address associated with the map/VA pair.
850 * The caller must hold vm_token if non-blocking operation is desired.
853 pmap_extract(pmap_t pmap, vm_offset_t va)
857 pd_entry_t pde, *pdep;
859 lwkt_gettoken(&vm_token);
861 pdep = pmap_pde(pmap, va);
865 if ((pde & PG_PS) != 0) {
866 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
868 pte = pmap_pde_to_pte(pdep, va);
869 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
873 lwkt_reltoken(&vm_token);
878 * Extract the physical page address associated kernel virtual address.
881 pmap_kextract(vm_offset_t va)
886 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
887 pa = DMAP_TO_PHYS(va);
891 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
894 * Beware of a concurrent promotion that changes the
895 * PDE at this point! For example, vtopte() must not
896 * be used to access the PTE because it would use the
897 * new PDE. It is, however, safe to use the old PDE
898 * because the page table page is preserved by the
901 pa = *pmap_pde_to_pte(&pde, va);
902 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
908 /***************************************************
909 * Low level mapping routines.....
910 ***************************************************/
913 * Routine: pmap_kenter
915 * Add a wired page to the KVA
916 * NOTE! note that in order for the mapping to take effect -- you
917 * should do an invltlb after doing the pmap_kenter().
920 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
924 pmap_inval_info info;
926 pmap_inval_init(&info);
927 npte = pa | PG_RW | PG_V | pgeflag;
929 pmap_inval_interlock(&info, &kernel_pmap, va);
931 pmap_inval_deinterlock(&info, &kernel_pmap);
932 pmap_inval_done(&info);
936 * Routine: pmap_kenter_quick
938 * Similar to pmap_kenter(), except we only invalidate the
939 * mapping on the current CPU.
942 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
947 npte = pa | PG_RW | PG_V | pgeflag;
950 cpu_invlpg((void *)va);
954 pmap_kenter_sync(vm_offset_t va)
956 pmap_inval_info info;
958 pmap_inval_init(&info);
959 pmap_inval_interlock(&info, &kernel_pmap, va);
960 pmap_inval_deinterlock(&info, &kernel_pmap);
961 pmap_inval_done(&info);
965 pmap_kenter_sync_quick(vm_offset_t va)
967 cpu_invlpg((void *)va);
971 * remove a page from the kernel pagetables
974 pmap_kremove(vm_offset_t va)
977 pmap_inval_info info;
979 pmap_inval_init(&info);
981 pmap_inval_interlock(&info, &kernel_pmap, va);
983 pmap_inval_deinterlock(&info, &kernel_pmap);
984 pmap_inval_done(&info);
988 pmap_kremove_quick(vm_offset_t va)
993 cpu_invlpg((void *)va);
997 * XXX these need to be recoded. They are not used in any critical path.
1000 pmap_kmodify_rw(vm_offset_t va)
1002 *vtopte(va) |= PG_RW;
1003 cpu_invlpg((void *)va);
1007 pmap_kmodify_nc(vm_offset_t va)
1009 *vtopte(va) |= PG_N;
1010 cpu_invlpg((void *)va);
1014 * Used to map a range of physical addresses into kernel virtual
1015 * address space during the low level boot, typically to map the
1016 * dump bitmap, message buffer, and vm_page_array.
1018 * These mappings are typically made at some pointer after the end of the
1021 * We could return PHYS_TO_DMAP(start) here and not allocate any
1022 * via (*virtp), but then kmem from userland and kernel dumps won't
1023 * have access to the related pointers.
1026 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
1029 vm_offset_t va_start;
1031 /*return PHYS_TO_DMAP(start);*/
1036 while (start < end) {
1037 pmap_kenter_quick(va, start);
1047 * Add a list of wired pages to the kva
1048 * this routine is only used for temporary
1049 * kernel mappings that do not need to have
1050 * page modification or references recorded.
1051 * Note that old mappings are simply written
1052 * over. The page *must* be wired.
1055 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1059 end_va = va + count * PAGE_SIZE;
1061 while (va < end_va) {
1065 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1066 cpu_invlpg((void *)va);
1071 smp_invltlb(); /* XXX */
1076 * This routine jerks page mappings from the
1077 * kernel -- it is meant only for temporary mappings.
1079 * MPSAFE, INTERRUPT SAFE (cluster callback)
1082 pmap_qremove(vm_offset_t va, int count)
1086 end_va = va + count * PAGE_SIZE;
1088 while (va < end_va) {
1093 cpu_invlpg((void *)va);
1102 * This routine works like vm_page_lookup() but also blocks as long as the
1103 * page is busy. This routine does not busy the page it returns.
1105 * Unless the caller is managing objects whos pages are in a known state,
1106 * the call should be made with a critical section held so the page's object
1107 * association remains valid on return.
1111 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
1116 m = vm_page_lookup(object, pindex);
1117 } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
1123 * Create a new thread and optionally associate it with a (new) process.
1124 * NOTE! the new thread's cpu may not equal the current cpu.
1127 pmap_init_thread(thread_t td)
1129 /* enforce pcb placement */
1130 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1131 td->td_savefpu = &td->td_pcb->pcb_save;
1132 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */
1136 * This routine directly affects the fork perf for a process.
1139 pmap_init_proc(struct proc *p)
1144 * Dispose the UPAGES for a process that has exited.
1145 * This routine directly impacts the exit perf of a process.
1148 pmap_dispose_proc(struct proc *p)
1150 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1153 /***************************************************
1154 * Page table page management routines.....
1155 ***************************************************/
1158 * This routine unholds page table pages, and if the hold count
1159 * drops to zero, then it decrements the wire count.
1163 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1164 pmap_inval_info_t info)
1166 KKASSERT(m->hold_count > 0);
1167 if (m->hold_count > 1) {
1171 return _pmap_unwire_pte_hold(pmap, va, m, info);
1177 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1178 pmap_inval_info_t info)
1181 * Wait until we can busy the page ourselves. We cannot have
1182 * any active flushes if we block. We own one hold count on the
1183 * page so it cannot be freed out from under us.
1185 if (m->flags & PG_BUSY) {
1186 pmap_inval_flush(info);
1187 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1190 KASSERT(m->queue == PQ_NONE,
1191 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1194 * This case can occur if new references were acquired while
1197 if (m->hold_count > 1) {
1198 KKASSERT(m->hold_count > 1);
1204 * Unmap the page table page
1206 KKASSERT(m->hold_count == 1);
1208 pmap_inval_interlock(info, pmap, -1);
1210 if (m->pindex >= (NUPDE + NUPDPE)) {
1213 pml4 = pmap_pml4e(pmap, va);
1215 } else if (m->pindex >= NUPDE) {
1218 pdp = pmap_pdpe(pmap, va);
1223 pd = pmap_pde(pmap, va);
1227 KKASSERT(pmap->pm_stats.resident_count > 0);
1228 --pmap->pm_stats.resident_count;
1230 if (pmap->pm_ptphint == m)
1231 pmap->pm_ptphint = NULL;
1232 pmap_inval_deinterlock(info, pmap);
1234 if (m->pindex < NUPDE) {
1235 /* We just released a PT, unhold the matching PD */
1238 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1239 pmap_unwire_pte_hold(pmap, va, pdpg, info);
1241 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1242 /* We just released a PD, unhold the matching PDP */
1245 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1246 pmap_unwire_pte_hold(pmap, va, pdppg, info);
1250 * This was our last hold, the page had better be unwired
1251 * after we decrement wire_count.
1253 * FUTURE NOTE: shared page directory page could result in
1254 * multiple wire counts.
1258 KKASSERT(m->wire_count == 0);
1259 --vmstats.v_wire_count;
1260 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1262 vm_page_free_zero(m);
1268 * After removing a page table entry, this routine is used to
1269 * conditionally free the page, and manage the hold/wire counts.
1273 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1274 pmap_inval_info_t info)
1276 vm_pindex_t ptepindex;
1278 if (va >= VM_MAX_USER_ADDRESS)
1282 ptepindex = pmap_pde_pindex(va);
1284 if (pmap->pm_ptphint &&
1285 (pmap->pm_ptphint->pindex == ptepindex)) {
1286 mpte = pmap->pm_ptphint;
1289 pmap_inval_flush(info);
1290 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1291 pmap->pm_ptphint = mpte;
1296 return pmap_unwire_pte_hold(pmap, va, mpte, info);
1300 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1301 * it, and IdlePTD, represents the template used to update all other pmaps.
1303 * On architectures where the kernel pmap is not integrated into the user
1304 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1305 * kernel_pmap should be used to directly access the kernel_pmap.
1308 pmap_pinit0(struct pmap *pmap)
1310 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1312 pmap->pm_active = 0;
1313 pmap->pm_ptphint = NULL;
1314 TAILQ_INIT(&pmap->pm_pvlist);
1315 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1319 * Initialize a preallocated and zeroed pmap structure,
1320 * such as one in a vmspace structure.
1323 pmap_pinit(struct pmap *pmap)
1328 * No need to allocate page table space yet but we do need a valid
1329 * page directory table.
1331 if (pmap->pm_pml4 == NULL) {
1333 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1337 * Allocate an object for the ptes
1339 if (pmap->pm_pteobj == NULL)
1340 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1343 * Allocate the page directory page, unless we already have
1344 * one cached. If we used the cached page the wire_count will
1345 * already be set appropriately.
1347 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1348 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1349 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1350 pmap->pm_pdirm = ptdpg;
1351 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1352 ptdpg->valid = VM_PAGE_BITS_ALL;
1353 if (ptdpg->wire_count == 0)
1354 ++vmstats.v_wire_count;
1355 ptdpg->wire_count = 1;
1356 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1358 if ((ptdpg->flags & PG_ZERO) == 0)
1359 bzero(pmap->pm_pml4, PAGE_SIZE);
1361 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1362 pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1364 /* install self-referential address mapping entry */
1365 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1368 pmap->pm_active = 0;
1369 pmap->pm_ptphint = NULL;
1370 TAILQ_INIT(&pmap->pm_pvlist);
1371 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1372 pmap->pm_stats.resident_count = 1;
1376 * Clean up a pmap structure so it can be physically freed. This routine
1377 * is called by the vmspace dtor function. A great deal of pmap data is
1378 * left passively mapped to improve vmspace management so we have a bit
1379 * of cleanup work to do here.
1382 pmap_puninit(pmap_t pmap)
1386 KKASSERT(pmap->pm_active == 0);
1387 lwkt_gettoken(&vm_token);
1388 if ((p = pmap->pm_pdirm) != NULL) {
1389 KKASSERT(pmap->pm_pml4 != NULL);
1390 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1391 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1393 vmstats.v_wire_count--;
1394 KKASSERT((p->flags & PG_BUSY) == 0);
1396 vm_page_free_zero(p);
1397 pmap->pm_pdirm = NULL;
1399 if (pmap->pm_pml4) {
1400 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1401 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1402 pmap->pm_pml4 = NULL;
1404 if (pmap->pm_pteobj) {
1405 vm_object_deallocate(pmap->pm_pteobj);
1406 pmap->pm_pteobj = NULL;
1408 lwkt_reltoken(&vm_token);
1412 * Wire in kernel global address entries. To avoid a race condition
1413 * between pmap initialization and pmap_growkernel, this procedure
1414 * adds the pmap to the master list (which growkernel scans to update),
1415 * then copies the template.
1418 pmap_pinit2(struct pmap *pmap)
1421 lwkt_gettoken(&vm_token);
1422 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1423 /* XXX copies current process, does not fill in MPPTDI */
1424 lwkt_reltoken(&vm_token);
1429 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1430 * 0 on failure (if the procedure had to sleep).
1432 * When asked to remove the page directory page itself, we actually just
1433 * leave it cached so we do not have to incur the SMP inval overhead of
1434 * removing the kernel mapping. pmap_puninit() will take care of it.
1438 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1441 * This code optimizes the case of freeing non-busy
1442 * page-table pages. Those pages are zero now, and
1443 * might as well be placed directly into the zero queue.
1445 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1451 * Remove the page table page from the processes address space.
1453 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1455 * We are the pml4 table itself.
1457 /* XXX anything to do here? */
1458 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1460 * Remove a PDP page from the PML4. We do not maintain
1461 * hold counts on the PML4 page.
1467 m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1468 KKASSERT(m4 != NULL);
1469 pml4 = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1470 idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1471 KKASSERT(pml4[idx] != 0);
1473 } else if (p->pindex >= NUPDE) {
1475 * Remove a PD page from the PDP and drop the hold count
1476 * on the PDP. The PDP is left cached in the pmap if
1477 * the hold count drops to 0 so the wire count remains
1484 m3 = vm_page_lookup(pmap->pm_pteobj,
1485 NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1486 KKASSERT(m3 != NULL);
1487 pdp = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1488 idx = (p->pindex - NUPDE) % NPDPEPG;
1489 KKASSERT(pdp[idx] != 0);
1494 * Remove a PT page from the PD and drop the hold count
1495 * on the PD. The PD is left cached in the pmap if
1496 * the hold count drops to 0 so the wire count remains
1503 m2 = vm_page_lookup(pmap->pm_pteobj,
1504 NUPDE + p->pindex / NPDEPG);
1505 KKASSERT(m2 != NULL);
1506 pd = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1507 idx = p->pindex % NPDEPG;
1513 * One fewer mappings in the pmap. p's hold count had better
1516 KKASSERT(pmap->pm_stats.resident_count > 0);
1517 --pmap->pm_stats.resident_count;
1519 panic("pmap_release: freeing held page table page");
1520 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1521 pmap->pm_ptphint = NULL;
1524 * We leave the top-level page table page cached, wired, and mapped in
1525 * the pmap until the dtor function (pmap_puninit()) gets called.
1526 * However, still clean it up so we can set PG_ZERO.
1528 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1529 bzero(pmap->pm_pml4, PAGE_SIZE);
1530 vm_page_flag_set(p, PG_ZERO);
1534 KKASSERT(p->wire_count == 0);
1535 vmstats.v_wire_count--;
1536 /* JG eventually revert to using vm_page_free_zero() */
1543 * This routine is called when various levels in the page table need to
1544 * be populated. This routine cannot fail.
1548 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1553 * Find or fabricate a new pagetable page. This will busy the page.
1555 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1556 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1557 if ((m->flags & PG_ZERO) == 0) {
1558 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1561 KASSERT(m->queue == PQ_NONE,
1562 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1565 * Increment the hold count for the page we will be returning to
1569 if (m->wire_count++ == 0)
1570 vmstats.v_wire_count++;
1573 * Map the pagetable page into the process address space, if
1574 * it isn't already there.
1576 * It is possible that someone else got in and mapped the page
1577 * directory page while we were blocked, if so just unbusy and
1578 * return the held page.
1580 if (ptepindex >= (NUPDE + NUPDPE)) {
1582 * Wire up a new PDP page in the PML4
1584 vm_pindex_t pml4index;
1587 pml4index = ptepindex - (NUPDE + NUPDPE);
1588 pml4 = &pmap->pm_pml4[pml4index];
1590 if (--m->wire_count == 0)
1591 --vmstats.v_wire_count;
1595 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1596 } else if (ptepindex >= NUPDE) {
1598 * Wire up a new PD page in the PDP
1600 vm_pindex_t pml4index;
1601 vm_pindex_t pdpindex;
1606 pdpindex = ptepindex - NUPDE;
1607 pml4index = pdpindex >> NPML4EPGSHIFT;
1609 pml4 = &pmap->pm_pml4[pml4index];
1610 if ((*pml4 & PG_V) == 0) {
1612 * Have to allocate a new PDP page, recurse.
1613 * This always succeeds. Returned page will
1616 pdppg = _pmap_allocpte(pmap,
1617 NUPDE + NUPDPE + pml4index);
1620 * Add a held reference to the PDP page.
1622 pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1623 pdppg->hold_count++;
1627 * Now find the pdp_entry and map the PDP. If the PDP
1628 * has already been mapped unwind and return the
1629 * already-mapped PDP held.
1631 * pdppg is left held (hold_count is incremented for
1632 * each PD in the PDP).
1634 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1635 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1637 vm_page_unhold(pdppg);
1638 if (--m->wire_count == 0)
1639 --vmstats.v_wire_count;
1643 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1646 * Wire up the new PT page in the PD
1648 vm_pindex_t pml4index;
1649 vm_pindex_t pdpindex;
1655 pdpindex = ptepindex >> NPDPEPGSHIFT;
1656 pml4index = pdpindex >> NPML4EPGSHIFT;
1659 * Locate the PDP page in the PML4, then the PD page in
1660 * the PDP. If either does not exist we simply recurse
1663 * We can just recurse on the PD page as it will recurse
1664 * on the PDP if necessary.
1666 pml4 = &pmap->pm_pml4[pml4index];
1667 if ((*pml4 & PG_V) == 0) {
1668 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1669 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1670 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1672 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1673 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1674 if ((*pdp & PG_V) == 0) {
1675 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1677 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1683 * Now fill in the pte in the PD. If the pte already exists
1684 * (again, if we raced the grab), unhold pdpg and unwire
1685 * m, returning a held m.
1687 * pdpg is left held (hold_count is incremented for
1688 * each PT in the PD).
1690 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1691 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1693 vm_page_unhold(pdpg);
1694 if (--m->wire_count == 0)
1695 --vmstats.v_wire_count;
1699 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1703 * We successfully loaded a PDP, PD, or PTE. Set the page table hint,
1704 * valid bits, mapped flag, unbusy, and we're done.
1706 pmap->pm_ptphint = m;
1707 ++pmap->pm_stats.resident_count;
1709 m->valid = VM_PAGE_BITS_ALL;
1710 vm_page_flag_clear(m, PG_ZERO);
1711 vm_page_flag_set(m, PG_MAPPED);
1719 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1721 vm_pindex_t ptepindex;
1726 * Calculate pagetable page index
1728 ptepindex = pmap_pde_pindex(va);
1731 * Get the page directory entry
1733 pd = pmap_pde(pmap, va);
1736 * This supports switching from a 2MB page to a
1739 if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1740 panic("no promotion/demotion yet");
1748 * If the page table page is mapped, we just increment the
1749 * hold count, and activate it.
1751 if (pd != NULL && (*pd & PG_V) != 0) {
1752 /* YYY hint is used here on i386 */
1753 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1754 pmap->pm_ptphint = m;
1759 * Here if the pte page isn't mapped, or if it has been deallocated.
1761 return _pmap_allocpte(pmap, ptepindex);
1765 /***************************************************
1766 * Pmap allocation/deallocation routines.
1767 ***************************************************/
1770 * Release any resources held by the given physical map.
1771 * Called when a pmap initialized by pmap_pinit is being released.
1772 * Should only be called if the map contains no valid mappings.
1774 static int pmap_release_callback(struct vm_page *p, void *data);
1777 pmap_release(struct pmap *pmap)
1779 vm_object_t object = pmap->pm_pteobj;
1780 struct rb_vm_page_scan_info info;
1782 KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active));
1783 #if defined(DIAGNOSTIC)
1784 if (object->ref_count != 1)
1785 panic("pmap_release: pteobj reference count != 1");
1789 info.object = object;
1791 lwkt_gettoken(&vm_token);
1792 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1799 info.limit = object->generation;
1801 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1802 pmap_release_callback, &info);
1803 if (info.error == 0 && info.mpte) {
1804 if (!pmap_release_free_page(pmap, info.mpte))
1808 } while (info.error);
1809 lwkt_reltoken(&vm_token);
1814 pmap_release_callback(struct vm_page *p, void *data)
1816 struct rb_vm_page_scan_info *info = data;
1818 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1822 if (!pmap_release_free_page(info->pmap, p)) {
1826 if (info->object->generation != info->limit) {
1834 * Grow the number of kernel page table entries, if needed.
1837 pmap_growkernel(vm_offset_t addr)
1840 vm_offset_t ptppaddr;
1842 pd_entry_t *pde, newpdir;
1846 lwkt_gettoken(&vm_token);
1847 if (kernel_vm_end == 0) {
1848 kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
1850 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1851 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1853 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1854 kernel_vm_end = kernel_map.max_offset;
1859 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1860 if (addr - 1 >= kernel_map.max_offset)
1861 addr = kernel_map.max_offset;
1862 while (kernel_vm_end < addr) {
1863 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1865 /* We need a new PDP entry */
1866 nkpg = vm_page_alloc(kptobj, nkpt,
1867 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1868 | VM_ALLOC_INTERRUPT);
1870 panic("pmap_growkernel: no memory to grow kernel");
1871 paddr = VM_PAGE_TO_PHYS(nkpg);
1872 if ((nkpg->flags & PG_ZERO) == 0)
1873 pmap_zero_page(paddr);
1874 vm_page_flag_clear(nkpg, PG_ZERO);
1875 newpdp = (pdp_entry_t)
1876 (paddr | PG_V | PG_RW | PG_A | PG_M);
1877 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1879 continue; /* try again */
1881 if ((*pde & PG_V) != 0) {
1882 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1883 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1884 kernel_vm_end = kernel_map.max_offset;
1891 * This index is bogus, but out of the way
1893 nkpg = vm_page_alloc(kptobj, nkpt,
1894 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1896 panic("pmap_growkernel: no memory to grow kernel");
1899 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1900 pmap_zero_page(ptppaddr);
1901 vm_page_flag_clear(nkpg, PG_ZERO);
1902 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1903 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1906 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1907 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1908 kernel_vm_end = kernel_map.max_offset;
1912 lwkt_reltoken(&vm_token);
1917 * Retire the given physical map from service.
1918 * Should only be called if the map contains
1919 * no valid mappings.
1922 pmap_destroy(pmap_t pmap)
1929 lwkt_gettoken(&vm_token);
1930 count = --pmap->pm_count;
1933 panic("destroying a pmap is not yet implemented");
1935 lwkt_reltoken(&vm_token);
1939 * Add a reference to the specified pmap.
1942 pmap_reference(pmap_t pmap)
1945 lwkt_gettoken(&vm_token);
1947 lwkt_reltoken(&vm_token);
1951 /***************************************************
1952 * page management routines.
1953 ***************************************************/
1956 * free the pv_entry back to the free list. This function may be
1957 * called from an interrupt.
1961 free_pv_entry(pv_entry_t pv)
1964 KKASSERT(pv_entry_count >= 0);
1969 * get a new pv_entry, allocating a block from the system
1970 * when needed. This function may be called from an interrupt.
1977 if (pv_entry_high_water &&
1978 (pv_entry_count > pv_entry_high_water) &&
1979 (pmap_pagedaemon_waken == 0)) {
1980 pmap_pagedaemon_waken = 1;
1981 wakeup(&vm_pages_needed);
1983 return zalloc(pvzone);
1987 * This routine is very drastic, but can save the system
1995 static int warningdone=0;
1997 if (pmap_pagedaemon_waken == 0)
1999 lwkt_gettoken(&vm_token);
2000 if (warningdone < 5) {
2001 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
2005 for(i = 0; i < vm_page_array_size; i++) {
2006 m = &vm_page_array[i];
2007 if (m->wire_count || m->hold_count || m->busy ||
2008 (m->flags & PG_BUSY))
2012 pmap_pagedaemon_waken = 0;
2013 lwkt_reltoken(&vm_token);
2018 * If it is the first entry on the list, it is actually
2019 * in the header and we must copy the following entry up
2020 * to the header. Otherwise we must search the list for
2021 * the entry. In either case we free the now unused entry.
2025 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
2026 vm_offset_t va, pmap_inval_info_t info)
2032 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
2033 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2034 if (pmap == pv->pv_pmap && va == pv->pv_va)
2038 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
2039 if (va == pv->pv_va)
2047 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2048 m->md.pv_list_count--;
2049 KKASSERT(m->md.pv_list_count >= 0);
2050 if (TAILQ_EMPTY(&m->md.pv_list))
2051 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2052 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2053 ++pmap->pm_generation;
2054 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
2062 * Create a pv entry for page at pa for
2067 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
2072 pv = get_pv_entry();
2077 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
2078 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2079 ++pmap->pm_generation;
2080 m->md.pv_list_count++;
2086 * pmap_remove_pte: do the things to unmap a page in a process
2090 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
2091 pmap_inval_info_t info)
2096 pmap_inval_interlock(info, pmap, va);
2097 oldpte = pte_load_clear(ptq);
2098 pmap_inval_deinterlock(info, pmap);
2100 pmap->pm_stats.wired_count -= 1;
2102 * Machines that don't support invlpg, also don't support
2103 * PG_G. XXX PG_G is disabled for SMP so don't worry about
2107 cpu_invlpg((void *)va);
2108 KKASSERT(pmap->pm_stats.resident_count > 0);
2109 --pmap->pm_stats.resident_count;
2110 if (oldpte & PG_MANAGED) {
2111 m = PHYS_TO_VM_PAGE(oldpte);
2112 if (oldpte & PG_M) {
2113 #if defined(PMAP_DIAGNOSTIC)
2114 if (pmap_nw_modified((pt_entry_t) oldpte)) {
2116 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2120 if (pmap_track_modified(va))
2124 vm_page_flag_set(m, PG_REFERENCED);
2125 return pmap_remove_entry(pmap, m, va, info);
2127 return pmap_unuse_pt(pmap, va, NULL, info);
2136 * Remove a single page from a process address space.
2138 * This function may not be called from an interrupt if the pmap is
2143 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
2147 pte = pmap_pte(pmap, va);
2150 if ((*pte & PG_V) == 0)
2152 pmap_remove_pte(pmap, pte, va, info);
2158 * Remove the given range of addresses from the specified map.
2160 * It is assumed that the start and end are properly
2161 * rounded to the page size.
2163 * This function may not be called from an interrupt if the pmap is
2167 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2169 vm_offset_t va_next;
2170 pml4_entry_t *pml4e;
2172 pd_entry_t ptpaddr, *pde;
2174 struct pmap_inval_info info;
2179 lwkt_gettoken(&vm_token);
2180 if (pmap->pm_stats.resident_count == 0) {
2181 lwkt_reltoken(&vm_token);
2185 pmap_inval_init(&info);
2188 * special handling of removing one page. a very
2189 * common operation and easy to short circuit some
2192 if (sva + PAGE_SIZE == eva) {
2193 pde = pmap_pde(pmap, sva);
2194 if (pde && (*pde & PG_PS) == 0) {
2195 pmap_remove_page(pmap, sva, &info);
2196 pmap_inval_done(&info);
2197 lwkt_reltoken(&vm_token);
2202 for (; sva < eva; sva = va_next) {
2203 pml4e = pmap_pml4e(pmap, sva);
2204 if ((*pml4e & PG_V) == 0) {
2205 va_next = (sva + NBPML4) & ~PML4MASK;
2211 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2212 if ((*pdpe & PG_V) == 0) {
2213 va_next = (sva + NBPDP) & ~PDPMASK;
2220 * Calculate index for next page table.
2222 va_next = (sva + NBPDR) & ~PDRMASK;
2226 pde = pmap_pdpe_to_pde(pdpe, sva);
2230 * Weed out invalid mappings.
2236 * Check for large page.
2238 if ((ptpaddr & PG_PS) != 0) {
2239 /* JG FreeBSD has more complex treatment here */
2240 pmap_inval_interlock(&info, pmap, -1);
2242 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2243 pmap_inval_deinterlock(&info, pmap);
2248 * Limit our scan to either the end of the va represented
2249 * by the current page table page, or to the end of the
2250 * range being removed.
2256 * NOTE: pmap_remove_pte() can block.
2258 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2262 if (pmap_remove_pte(pmap, pte, sva, &info))
2266 pmap_inval_done(&info);
2267 lwkt_reltoken(&vm_token);
2273 * Removes this physical page from all physical maps in which it resides.
2274 * Reflects back modify bits to the pager.
2276 * This routine may not be called from an interrupt.
2281 pmap_remove_all(vm_page_t m)
2283 struct pmap_inval_info info;
2284 pt_entry_t *pte, tpte;
2287 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2290 lwkt_gettoken(&vm_token);
2291 pmap_inval_init(&info);
2293 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2294 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2295 --pv->pv_pmap->pm_stats.resident_count;
2297 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2298 pmap_inval_interlock(&info, pv->pv_pmap, pv->pv_va);
2299 tpte = pte_load_clear(pte);
2301 pv->pv_pmap->pm_stats.wired_count--;
2302 pmap_inval_deinterlock(&info, pv->pv_pmap);
2304 vm_page_flag_set(m, PG_REFERENCED);
2307 * Update the vm_page_t clean and reference bits.
2310 #if defined(PMAP_DIAGNOSTIC)
2311 if (pmap_nw_modified(tpte)) {
2313 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2317 if (pmap_track_modified(pv->pv_va))
2320 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2321 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2322 ++pv->pv_pmap->pm_generation;
2323 m->md.pv_list_count--;
2324 KKASSERT(m->md.pv_list_count >= 0);
2325 if (TAILQ_EMPTY(&m->md.pv_list))
2326 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2327 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2331 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2332 pmap_inval_done(&info);
2333 lwkt_reltoken(&vm_token);
2339 * Set the physical protection on the specified range of this map
2342 * This function may not be called from an interrupt if the map is
2343 * not the kernel_pmap.
2346 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2348 vm_offset_t va_next;
2349 pml4_entry_t *pml4e;
2351 pd_entry_t ptpaddr, *pde;
2353 pmap_inval_info info;
2355 /* JG review for NX */
2360 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2361 pmap_remove(pmap, sva, eva);
2365 if (prot & VM_PROT_WRITE)
2368 lwkt_gettoken(&vm_token);
2369 pmap_inval_init(&info);
2371 for (; sva < eva; sva = va_next) {
2373 pml4e = pmap_pml4e(pmap, sva);
2374 if ((*pml4e & PG_V) == 0) {
2375 va_next = (sva + NBPML4) & ~PML4MASK;
2381 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2382 if ((*pdpe & PG_V) == 0) {
2383 va_next = (sva + NBPDP) & ~PDPMASK;
2389 va_next = (sva + NBPDR) & ~PDRMASK;
2393 pde = pmap_pdpe_to_pde(pdpe, sva);
2397 * Check for large page.
2399 if ((ptpaddr & PG_PS) != 0) {
2400 pmap_inval_interlock(&info, pmap, -1);
2401 *pde &= ~(PG_M|PG_RW);
2402 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2403 pmap_inval_deinterlock(&info, pmap);
2408 * Weed out invalid mappings. Note: we assume that the page
2409 * directory table is always allocated, and in kernel virtual.
2417 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2424 * XXX non-optimal. Note also that there can be
2425 * no pmap_inval_flush() calls until after we modify
2426 * ptbase[sindex] (or otherwise we have to do another
2427 * pmap_inval_add() call).
2429 pmap_inval_interlock(&info, pmap, sva);
2433 if ((pbits & PG_V) == 0) {
2434 pmap_inval_deinterlock(&info, pmap);
2437 if (pbits & PG_MANAGED) {
2440 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2441 vm_page_flag_set(m, PG_REFERENCED);
2445 if (pmap_track_modified(sva)) {
2447 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2454 if (pbits != cbits &&
2455 !atomic_cmpset_long(pte, pbits, cbits)) {
2458 pmap_inval_deinterlock(&info, pmap);
2461 pmap_inval_done(&info);
2462 lwkt_reltoken(&vm_token);
2466 * Insert the given physical page (p) at
2467 * the specified virtual address (v) in the
2468 * target physical map with the protection requested.
2470 * If specified, the page will be wired down, meaning
2471 * that the related pte can not be reclaimed.
2473 * NB: This is the only routine which MAY NOT lazy-evaluate
2474 * or lose information. That is, this routine must actually
2475 * insert this page into the given map NOW.
2478 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2485 pt_entry_t origpte, newpte;
2487 pmap_inval_info info;
2492 va = trunc_page(va);
2493 #ifdef PMAP_DIAGNOSTIC
2495 panic("pmap_enter: toobig");
2496 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2497 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va);
2499 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2500 kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n");
2502 db_print_backtrace();
2505 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2506 kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n");
2508 db_print_backtrace();
2512 lwkt_gettoken(&vm_token);
2515 * In the case that a page table page is not
2516 * resident, we are creating it here.
2518 if (va < VM_MAX_USER_ADDRESS)
2519 mpte = pmap_allocpte(pmap, va);
2523 pmap_inval_init(&info);
2524 pde = pmap_pde(pmap, va);
2525 if (pde != NULL && (*pde & PG_V) != 0) {
2526 if ((*pde & PG_PS) != 0)
2527 panic("pmap_enter: attempted pmap_enter on 2MB page");
2528 pte = pmap_pde_to_pte(pde, va);
2530 panic("pmap_enter: invalid page directory va=%#lx", va);
2532 KKASSERT(pte != NULL);
2533 pa = VM_PAGE_TO_PHYS(m);
2535 opa = origpte & PG_FRAME;
2538 * Mapping has not changed, must be protection or wiring change.
2540 if (origpte && (opa == pa)) {
2542 * Wiring change, just update stats. We don't worry about
2543 * wiring PT pages as they remain resident as long as there
2544 * are valid mappings in them. Hence, if a user page is wired,
2545 * the PT page will be also.
2547 if (wired && ((origpte & PG_W) == 0))
2548 pmap->pm_stats.wired_count++;
2549 else if (!wired && (origpte & PG_W))
2550 pmap->pm_stats.wired_count--;
2552 #if defined(PMAP_DIAGNOSTIC)
2553 if (pmap_nw_modified(origpte)) {
2555 "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2561 * Remove the extra pte reference. Note that we cannot
2562 * optimize the RO->RW case because we have adjusted the
2563 * wiring count above and may need to adjust the wiring
2570 * We might be turning off write access to the page,
2571 * so we go ahead and sense modify status.
2573 if (origpte & PG_MANAGED) {
2574 if ((origpte & PG_M) && pmap_track_modified(va)) {
2576 om = PHYS_TO_VM_PAGE(opa);
2580 KKASSERT(m->flags & PG_MAPPED);
2585 * Mapping has changed, invalidate old range and fall through to
2586 * handle validating new mapping.
2590 err = pmap_remove_pte(pmap, pte, va, &info);
2592 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2594 opa = origpte & PG_FRAME;
2596 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
2602 * Enter on the PV list if part of our managed memory. Note that we
2603 * raise IPL while manipulating pv_table since pmap_enter can be
2604 * called at interrupt time.
2606 if (pmap_initialized &&
2607 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2608 pmap_insert_entry(pmap, va, mpte, m);
2610 vm_page_flag_set(m, PG_MAPPED);
2614 * Increment counters
2616 ++pmap->pm_stats.resident_count;
2618 pmap->pm_stats.wired_count++;
2622 * Now validate mapping with desired protection/wiring.
2624 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V);
2628 if (va < VM_MAX_USER_ADDRESS)
2630 if (pmap == &kernel_pmap)
2634 * if the mapping or permission bits are different, we need
2635 * to update the pte.
2637 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2638 pmap_inval_interlock(&info, pmap, va);
2639 *pte = newpte | PG_A;
2640 pmap_inval_deinterlock(&info, pmap);
2642 vm_page_flag_set(m, PG_WRITEABLE);
2644 KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
2645 pmap_inval_done(&info);
2646 lwkt_reltoken(&vm_token);
2650 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2651 * This code also assumes that the pmap has no pre-existing entry for this
2654 * This code currently may only be used on user pmaps, not kernel_pmap.
2657 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2662 vm_pindex_t ptepindex;
2664 pmap_inval_info info;
2666 lwkt_gettoken(&vm_token);
2667 pmap_inval_init(&info);
2669 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2670 kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n");
2672 db_print_backtrace();
2675 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2676 kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n");
2678 db_print_backtrace();
2682 KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */
2685 * Calculate the page table page (mpte), allocating it if necessary.
2687 * A held page table page (mpte), or NULL, is passed onto the
2688 * section following.
2690 if (va < VM_MAX_USER_ADDRESS) {
2692 * Calculate pagetable page index
2694 ptepindex = pmap_pde_pindex(va);
2698 * Get the page directory entry
2700 ptepa = pmap_pde(pmap, va);
2703 * If the page table page is mapped, we just increment
2704 * the hold count, and activate it.
2706 if (ptepa && (*ptepa & PG_V) != 0) {
2708 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2709 // if (pmap->pm_ptphint &&
2710 // (pmap->pm_ptphint->pindex == ptepindex)) {
2711 // mpte = pmap->pm_ptphint;
2713 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2714 pmap->pm_ptphint = mpte;
2719 mpte = _pmap_allocpte(pmap, ptepindex);
2721 } while (mpte == NULL);
2724 /* this code path is not yet used */
2728 * With a valid (and held) page directory page, we can just use
2729 * vtopte() to get to the pte. If the pte is already present
2730 * we do not disturb it.
2735 pmap_unwire_pte_hold(pmap, va, mpte, &info);
2736 pa = VM_PAGE_TO_PHYS(m);
2737 KKASSERT(((*pte ^ pa) & PG_FRAME) == 0);
2738 pmap_inval_done(&info);
2739 lwkt_reltoken(&vm_token);
2744 * Enter on the PV list if part of our managed memory
2746 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2747 pmap_insert_entry(pmap, va, mpte, m);
2748 vm_page_flag_set(m, PG_MAPPED);
2752 * Increment counters
2754 ++pmap->pm_stats.resident_count;
2756 pa = VM_PAGE_TO_PHYS(m);
2759 * Now validate mapping with RO protection
2761 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2762 *pte = pa | PG_V | PG_U;
2764 *pte = pa | PG_V | PG_U | PG_MANAGED;
2765 /* pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */
2766 pmap_inval_done(&info);
2767 lwkt_reltoken(&vm_token);
2771 * Make a temporary mapping for a physical address. This is only intended
2772 * to be used for panic dumps.
2774 /* JG Needed on x86_64? */
2776 pmap_kenter_temporary(vm_paddr_t pa, int i)
2778 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2779 return ((void *)crashdumpmap);
2782 #define MAX_INIT_PT (96)
2785 * This routine preloads the ptes for a given object into the specified pmap.
2786 * This eliminates the blast of soft faults on process startup and
2787 * immediately after an mmap.
2789 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2792 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2793 vm_object_t object, vm_pindex_t pindex,
2794 vm_size_t size, int limit)
2796 struct rb_vm_page_scan_info info;
2801 * We can't preinit if read access isn't set or there is no pmap
2804 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2808 * We can't preinit if the pmap is not the current pmap
2810 lp = curthread->td_lwp;
2811 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2814 psize = x86_64_btop(size);
2816 if ((object->type != OBJT_VNODE) ||
2817 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2818 (object->resident_page_count > MAX_INIT_PT))) {
2822 if (psize + pindex > object->size) {
2823 if (object->size < pindex)
2825 psize = object->size - pindex;
2832 * Use a red-black scan to traverse the requested range and load
2833 * any valid pages found into the pmap.
2835 * We cannot safely scan the object's memq unless we are in a
2836 * critical section since interrupts can remove pages from objects.
2838 info.start_pindex = pindex;
2839 info.end_pindex = pindex + psize - 1;
2846 lwkt_gettoken(&vm_token);
2847 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2848 pmap_object_init_pt_callback, &info);
2849 lwkt_reltoken(&vm_token);
2855 pmap_object_init_pt_callback(vm_page_t p, void *data)
2857 struct rb_vm_page_scan_info *info = data;
2858 vm_pindex_t rel_index;
2860 * don't allow an madvise to blow away our really
2861 * free pages allocating pv entries.
2863 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2864 vmstats.v_free_count < vmstats.v_free_reserved) {
2867 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2868 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2869 if ((p->queue - p->pc) == PQ_CACHE)
2870 vm_page_deactivate(p);
2872 rel_index = p->pindex - info->start_pindex;
2873 pmap_enter_quick(info->pmap,
2874 info->addr + x86_64_ptob(rel_index), p);
2881 * Return TRUE if the pmap is in shape to trivially
2882 * pre-fault the specified address.
2884 * Returns FALSE if it would be non-trivial or if a
2885 * pte is already loaded into the slot.
2888 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2894 lwkt_gettoken(&vm_token);
2895 pde = pmap_pde(pmap, addr);
2896 if (pde == NULL || *pde == 0) {
2900 ret = (*pte) ? 0 : 1;
2902 lwkt_reltoken(&vm_token);
2907 * Routine: pmap_change_wiring
2908 * Function: Change the wiring attribute for a map/virtual-address
2910 * In/out conditions:
2911 * The mapping must already exist in the pmap.
2914 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2921 lwkt_gettoken(&vm_token);
2922 pte = pmap_pte(pmap, va);
2924 if (wired && !pmap_pte_w(pte))
2925 pmap->pm_stats.wired_count++;
2926 else if (!wired && pmap_pte_w(pte))
2927 pmap->pm_stats.wired_count--;
2930 * Wiring is not a hardware characteristic so there is no need to
2931 * invalidate TLB. However, in an SMP environment we must use
2932 * a locked bus cycle to update the pte (if we are not using
2933 * the pmap_inval_*() API that is)... it's ok to do this for simple
2938 atomic_set_long(pte, PG_W);
2940 atomic_clear_long(pte, PG_W);
2943 atomic_set_long_nonlocked(pte, PG_W);
2945 atomic_clear_long_nonlocked(pte, PG_W);
2947 lwkt_reltoken(&vm_token);
2953 * Copy the range specified by src_addr/len
2954 * from the source map to the range dst_addr/len
2955 * in the destination map.
2957 * This routine is only advisory and need not do anything.
2960 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2961 vm_size_t len, vm_offset_t src_addr)
2965 pmap_inval_info info;
2967 vm_offset_t end_addr = src_addr + len;
2969 pd_entry_t src_frame, dst_frame;
2972 if (dst_addr != src_addr)
2975 src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2976 if (src_frame != (PTDpde & PG_FRAME)) {
2980 dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2981 if (dst_frame != (APTDpde & PG_FRAME)) {
2982 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2983 /* The page directory is not shared between CPUs */
2987 pmap_inval_init(&info);
2988 pmap_inval_add(&info, dst_pmap, -1);
2989 pmap_inval_add(&info, src_pmap, -1);
2992 * critical section protection is required to maintain the page/object
2993 * association, interrupts can free pages and remove them from
2997 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2998 pt_entry_t *src_pte, *dst_pte;
2999 vm_page_t dstmpte, srcmpte;
3000 vm_offset_t srcptepaddr;
3001 vm_pindex_t ptepindex;
3003 if (addr >= UPT_MIN_ADDRESS)
3004 panic("pmap_copy: invalid to pmap_copy page tables\n");
3007 * Don't let optional prefaulting of pages make us go
3008 * way below the low water mark of free pages or way
3009 * above high water mark of used pv entries.
3011 if (vmstats.v_free_count < vmstats.v_free_reserved ||
3012 pv_entry_count > pv_entry_high_water)
3015 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
3016 ptepindex = addr >> PDRSHIFT;
3019 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
3021 if (srcptepaddr == 0)
3024 if (srcptepaddr & PG_PS) {
3026 if (dst_pmap->pm_pdir[ptepindex] == 0) {
3027 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
3028 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3034 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
3035 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
3036 (srcmpte->flags & PG_BUSY)) {
3040 if (pdnxt > end_addr)
3043 src_pte = vtopte(addr);
3045 dst_pte = avtopte(addr);
3047 while (addr < pdnxt) {
3052 * we only virtual copy managed pages
3054 if ((ptetemp & PG_MANAGED) != 0) {
3056 * We have to check after allocpte for the
3057 * pte still being around... allocpte can
3060 * pmap_allocpte() can block. If we lose
3061 * our page directory mappings we stop.
3063 dstmpte = pmap_allocpte(dst_pmap, addr);
3066 if (src_frame != (PTDpde & PG_FRAME) ||
3067 dst_frame != (APTDpde & PG_FRAME)
3069 kprintf("WARNING: pmap_copy: detected and corrected race\n");
3070 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3072 } else if ((*dst_pte == 0) &&
3073 (ptetemp = *src_pte) != 0 &&
3074 (ptetemp & PG_MANAGED)) {
3076 * Clear the modified and
3077 * accessed (referenced) bits
3080 m = PHYS_TO_VM_PAGE(ptetemp);
3081 *dst_pte = ptetemp & ~(PG_M | PG_A);
3082 ++dst_pmap->pm_stats.resident_count;
3083 pmap_insert_entry(dst_pmap, addr,
3085 KKASSERT(m->flags & PG_MAPPED);
3087 kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n");
3088 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3092 if (dstmpte->hold_count >= srcmpte->hold_count)
3102 pmap_inval_done(&info);
3109 * Zero the specified physical page.
3111 * This function may be called from an interrupt and no locking is
3115 pmap_zero_page(vm_paddr_t phys)
3117 vm_offset_t va = PHYS_TO_DMAP(phys);
3119 pagezero((void *)va);
3123 * pmap_page_assertzero:
3125 * Assert that a page is empty, panic if it isn't.
3128 pmap_page_assertzero(vm_paddr_t phys)
3130 vm_offset_t virt = PHYS_TO_DMAP(phys);
3133 for (i = 0; i < PAGE_SIZE; i += sizeof(long)) {
3134 if (*(long *)((char *)virt + i) != 0) {
3135 panic("pmap_page_assertzero() @ %p not zero!\n", (void *)virt);
3143 * Zero part of a physical page by mapping it into memory and clearing
3144 * its contents with bzero.
3146 * off and size may not cover an area beyond a single hardware page.
3149 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3151 vm_offset_t virt = PHYS_TO_DMAP(phys);
3153 bzero((char *)virt + off, size);
3159 * Copy the physical page from the source PA to the target PA.
3160 * This function may be called from an interrupt. No locking
3164 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3166 vm_offset_t src_virt, dst_virt;
3168 src_virt = PHYS_TO_DMAP(src);
3169 dst_virt = PHYS_TO_DMAP(dst);
3170 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
3174 * pmap_copy_page_frag:
3176 * Copy the physical page from the source PA to the target PA.
3177 * This function may be called from an interrupt. No locking
3181 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3183 vm_offset_t src_virt, dst_virt;
3185 src_virt = PHYS_TO_DMAP(src);
3186 dst_virt = PHYS_TO_DMAP(dst);
3188 bcopy((char *)src_virt + (src & PAGE_MASK),
3189 (char *)dst_virt + (dst & PAGE_MASK),
3194 * Returns true if the pmap's pv is one of the first
3195 * 16 pvs linked to from this page. This count may
3196 * be changed upwards or downwards in the future; it
3197 * is only necessary that true be returned for a small
3198 * subset of pmaps for proper page aging.
3201 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3206 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3210 lwkt_gettoken(&vm_token);
3212 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3213 if (pv->pv_pmap == pmap) {
3214 lwkt_reltoken(&vm_token);
3222 lwkt_reltoken(&vm_token);
3228 * Remove all pages from specified address space
3229 * this aids process exit speeds. Also, this code
3230 * is special cased for current process only, but
3231 * can have the more generic (and slightly slower)
3232 * mode enabled. This is much faster than pmap_remove
3233 * in the case of running down an entire address space.
3236 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3239 pt_entry_t *pte, tpte;
3242 pmap_inval_info info;
3244 int save_generation;
3246 lp = curthread->td_lwp;
3247 if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
3252 lwkt_gettoken(&vm_token);
3253 pmap_inval_init(&info);
3254 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3255 if (pv->pv_va >= eva || pv->pv_va < sva) {
3256 npv = TAILQ_NEXT(pv, pv_plist);
3260 KKASSERT(pmap == pv->pv_pmap);
3263 pte = vtopte(pv->pv_va);
3265 pte = pmap_pte_quick(pmap, pv->pv_va);
3266 pmap_inval_interlock(&info, pmap, pv->pv_va);
3269 * We cannot remove wired pages from a process' mapping
3273 pmap_inval_deinterlock(&info, pmap);
3274 npv = TAILQ_NEXT(pv, pv_plist);
3277 tpte = pte_load_clear(pte);
3279 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3281 KASSERT(m < &vm_page_array[vm_page_array_size],
3282 ("pmap_remove_pages: bad tpte %lx", tpte));
3284 KKASSERT(pmap->pm_stats.resident_count > 0);
3285 --pmap->pm_stats.resident_count;
3286 pmap_inval_deinterlock(&info, pmap);
3289 * Update the vm_page_t clean and reference bits.
3295 npv = TAILQ_NEXT(pv, pv_plist);
3296 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
3297 save_generation = ++pmap->pm_generation;
3299 m->md.pv_list_count--;
3300 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3301 if (TAILQ_EMPTY(&m->md.pv_list))
3302 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3304 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
3308 * Restart the scan if we blocked during the unuse or free
3309 * calls and other removals were made.
3311 if (save_generation != pmap->pm_generation) {
3312 kprintf("Warning: pmap_remove_pages race-A avoided\n");
3313 npv = TAILQ_FIRST(&pmap->pm_pvlist);
3316 pmap_inval_done(&info);
3317 lwkt_reltoken(&vm_token);
3321 * pmap_testbit tests bits in pte's
3322 * note that the testbit/clearbit routines are inline,
3323 * and a lot of things compile-time evaluate.
3327 pmap_testbit(vm_page_t m, int bit)
3332 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3335 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3340 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3342 * if the bit being tested is the modified bit, then
3343 * mark clean_map and ptes as never
3346 if (bit & (PG_A|PG_M)) {
3347 if (!pmap_track_modified(pv->pv_va))
3351 #if defined(PMAP_DIAGNOSTIC)
3352 if (pv->pv_pmap == NULL) {
3353 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3357 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3368 * this routine is used to modify bits in ptes
3372 pmap_clearbit(vm_page_t m, int bit)
3374 struct pmap_inval_info info;
3379 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3382 pmap_inval_init(&info);
3385 * Loop over all current mappings setting/clearing as appropos If
3386 * setting RO do we need to clear the VAC?
3388 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3390 * don't write protect pager mappings
3393 if (!pmap_track_modified(pv->pv_va))
3397 #if defined(PMAP_DIAGNOSTIC)
3398 if (pv->pv_pmap == NULL) {
3399 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3405 * Careful here. We can use a locked bus instruction to
3406 * clear PG_A or PG_M safely but we need to synchronize
3407 * with the target cpus when we mess with PG_RW.
3409 * We do not have to force synchronization when clearing
3410 * PG_M even for PTEs generated via virtual memory maps,
3411 * because the virtual kernel will invalidate the pmap
3412 * entry when/if it needs to resynchronize the Modify bit.
3415 pmap_inval_interlock(&info, pv->pv_pmap, pv->pv_va);
3416 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3423 atomic_clear_long(pte, PG_M|PG_RW);
3426 * The cpu may be trying to set PG_M
3427 * simultaniously with our clearing
3430 if (!atomic_cmpset_long(pte, pbits,
3434 } else if (bit == PG_M) {
3436 * We could also clear PG_RW here to force
3437 * a fault on write to redetect PG_M for
3438 * virtual kernels, but it isn't necessary
3439 * since virtual kernels invalidate the pte
3440 * when they clear the VPTE_M bit in their
3441 * virtual page tables.
3443 atomic_clear_long(pte, PG_M);
3445 atomic_clear_long(pte, bit);
3449 pmap_inval_deinterlock(&info, pv->pv_pmap);
3451 pmap_inval_done(&info);
3455 * pmap_page_protect:
3457 * Lower the permission for all mappings to a given page.
3460 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3462 /* JG NX support? */
3463 if ((prot & VM_PROT_WRITE) == 0) {
3464 lwkt_gettoken(&vm_token);
3465 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3466 pmap_clearbit(m, PG_RW);
3467 vm_page_flag_clear(m, PG_WRITEABLE);
3471 lwkt_reltoken(&vm_token);
3476 pmap_phys_address(vm_pindex_t ppn)
3478 return (x86_64_ptob(ppn));
3482 * pmap_ts_referenced:
3484 * Return a count of reference bits for a page, clearing those bits.
3485 * It is not necessary for every reference bit to be cleared, but it
3486 * is necessary that 0 only be returned when there are truly no
3487 * reference bits set.
3489 * XXX: The exact number of bits to check and clear is a matter that
3490 * should be tested and standardized at some point in the future for
3491 * optimal aging of shared pages.
3494 pmap_ts_referenced(vm_page_t m)
3496 pv_entry_t pv, pvf, pvn;
3500 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3504 lwkt_gettoken(&vm_token);
3506 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3511 pvn = TAILQ_NEXT(pv, pv_list);
3514 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3515 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3518 if (!pmap_track_modified(pv->pv_va))
3521 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3523 if (pte && (*pte & PG_A)) {
3525 atomic_clear_long(pte, PG_A);
3527 atomic_clear_long_nonlocked(pte, PG_A);
3534 } while ((pv = pvn) != NULL && pv != pvf);
3536 lwkt_reltoken(&vm_token);
3545 * Return whether or not the specified physical page was modified
3546 * in any physical maps.
3549 pmap_is_modified(vm_page_t m)
3553 lwkt_gettoken(&vm_token);
3554 res = pmap_testbit(m, PG_M);
3555 lwkt_reltoken(&vm_token);
3560 * Clear the modify bits on the specified physical page.
3563 pmap_clear_modify(vm_page_t m)
3565 lwkt_gettoken(&vm_token);
3566 pmap_clearbit(m, PG_M);
3567 lwkt_reltoken(&vm_token);
3571 * pmap_clear_reference:
3573 * Clear the reference bit on the specified physical page.
3576 pmap_clear_reference(vm_page_t m)
3578 lwkt_gettoken(&vm_token);
3579 pmap_clearbit(m, PG_A);
3580 lwkt_reltoken(&vm_token);
3584 * Miscellaneous support routines follow
3589 i386_protection_init(void)
3593 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
3594 kp = protection_codes;
3595 for (prot = 0; prot < 8; prot++) {
3597 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3599 * Read access is also 0. There isn't any execute bit,
3600 * so just make it readable.
3602 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3603 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3604 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3607 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3608 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3609 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3610 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3618 * Map a set of physical memory pages into the kernel virtual
3619 * address space. Return a pointer to where it is mapped. This
3620 * routine is intended to be used for mapping device memory,
3623 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3627 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3629 vm_offset_t va, tmpva, offset;
3632 offset = pa & PAGE_MASK;
3633 size = roundup(offset + size, PAGE_SIZE);
3635 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3637 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3639 pa = pa & ~PAGE_MASK;
3640 for (tmpva = va; size > 0;) {
3641 pte = vtopte(tmpva);
3642 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3650 return ((void *)(va + offset));
3654 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
3656 vm_offset_t va, tmpva, offset;
3659 offset = pa & PAGE_MASK;
3660 size = roundup(offset + size, PAGE_SIZE);
3662 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3664 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3666 pa = pa & ~PAGE_MASK;
3667 for (tmpva = va; size > 0;) {
3668 pte = vtopte(tmpva);
3669 *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */
3677 return ((void *)(va + offset));
3681 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3683 vm_offset_t base, offset;
3685 base = va & ~PAGE_MASK;
3686 offset = va & PAGE_MASK;
3687 size = roundup(offset + size, PAGE_SIZE);
3688 pmap_qremove(va, size >> PAGE_SHIFT);
3689 kmem_free(&kernel_map, base, size);
3693 * perform the pmap work for mincore
3696 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3698 pt_entry_t *ptep, pte;
3702 lwkt_gettoken(&vm_token);
3703 ptep = pmap_pte(pmap, addr);
3705 if (ptep && (pte = *ptep) != 0) {
3708 val = MINCORE_INCORE;
3709 if ((pte & PG_MANAGED) == 0)
3712 pa = pte & PG_FRAME;
3714 m = PHYS_TO_VM_PAGE(pa);
3720 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3722 * Modified by someone
3724 else if (m->dirty || pmap_is_modified(m))
3725 val |= MINCORE_MODIFIED_OTHER;
3730 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3733 * Referenced by someone
3735 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3736 val |= MINCORE_REFERENCED_OTHER;
3737 vm_page_flag_set(m, PG_REFERENCED);
3741 lwkt_reltoken(&vm_token);
3746 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3747 * vmspace will be ref'd and the old one will be deref'd.
3749 * The vmspace for all lwps associated with the process will be adjusted
3750 * and cr3 will be reloaded if any lwp is the current lwp.
3753 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3755 struct vmspace *oldvm;
3759 oldvm = p->p_vmspace;
3760 if (oldvm != newvm) {
3761 p->p_vmspace = newvm;
3762 KKASSERT(p->p_nthreads == 1);
3763 lp = RB_ROOT(&p->p_lwp_tree);
3764 pmap_setlwpvm(lp, newvm);
3766 sysref_get(&newvm->vm_sysref);
3767 sysref_put(&oldvm->vm_sysref);
3774 * Set the vmspace for a LWP. The vmspace is almost universally set the
3775 * same as the process vmspace, but virtual kernels need to swap out contexts
3776 * on a per-lwp basis.
3779 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3781 struct vmspace *oldvm;
3785 oldvm = lp->lwp_vmspace;
3787 if (oldvm != newvm) {
3788 lp->lwp_vmspace = newvm;
3789 if (curthread->td_lwp == lp) {
3790 pmap = vmspace_pmap(newvm);
3792 atomic_set_int(&pmap->pm_active, mycpu->gd_cpumask);
3793 if (pmap->pm_active & CPUMASK_LOCK)
3794 pmap_interlock_wait(newvm);
3796 pmap->pm_active |= 1;
3798 #if defined(SWTCH_OPTIM_STATS)
3801 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
3802 curthread->td_pcb->pcb_cr3 |= PG_RW | PG_U | PG_V;
3803 load_cr3(curthread->td_pcb->pcb_cr3);
3804 pmap = vmspace_pmap(oldvm);
3806 atomic_clear_int(&pmap->pm_active, mycpu->gd_cpumask);
3808 pmap->pm_active &= ~1;
3818 * Called when switching to a locked pmap
3821 pmap_interlock_wait(struct vmspace *vm)
3823 struct pmap *pmap = &vm->vm_pmap;
3825 if (pmap->pm_active & CPUMASK_LOCK) {
3826 while (pmap->pm_active & CPUMASK_LOCK) {
3829 lwkt_process_ipiq();
3837 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3840 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3844 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);