2 * Copyright (c) 1991 Regents of the University of California.
3 * Copyright (c) 1994 John S. Dyson
4 * Copyright (c) 1994 David Greenman
5 * Copyright (c) 2003 Peter Wemm
6 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
7 * Copyright (c) 2008, 2009 The DragonFly Project.
8 * Copyright (c) 2008, 2009 Jordan Gordeev.
11 * This code is derived from software contributed to Berkeley by
12 * the Systems Programming Group of the University of Utah Computer
13 * Science Department and William Jolitz of UUNET Technologies Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
44 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
48 * Manages physical address maps.
50 * In addition to hardware address maps, this
51 * module is called upon to provide software-use-only
52 * maps which may or may not be stored in the same
53 * form as hardware maps. These pseudo-maps are
54 * used to store intermediate results from copy
55 * operations to and from address spaces.
57 * Since the information managed by this module is
58 * also stored by the logical address mapping module,
59 * this module may throw away valid virtual-to-physical
60 * mappings at almost any time. However, invalidations
61 * of virtual-to-physical mappings must be done as
64 * In order to cope with hardware architectures which
65 * make virtual-to-physical map invalidates expensive,
66 * this module may delay invalidate or reduced protection
67 * operations until such time as they are actually
68 * necessary. This module is given full information as
69 * to which processors are currently using which maps,
70 * and to when physical maps must be made correct.
74 #include "opt_disable_pse.h"
77 #include "opt_msgbuf.h"
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
83 #include <sys/msgbuf.h>
84 #include <sys/vmmeter.h>
88 #include <vm/vm_param.h>
89 #include <sys/sysctl.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_pageout.h>
97 #include <vm/vm_pager.h>
98 #include <vm/vm_zone.h>
100 #include <sys/user.h>
101 #include <sys/thread2.h>
102 #include <sys/sysref2.h>
104 #include <machine/cputypes.h>
105 #include <machine/md_var.h>
106 #include <machine/specialreg.h>
107 #include <machine/smp.h>
108 #include <machine_base/apic/apicreg.h>
109 #include <machine/globaldata.h>
110 #include <machine/pmap.h>
111 #include <machine/pmap_inval.h>
115 #define PMAP_KEEP_PDIRS
116 #ifndef PMAP_SHPGPERPROC
117 #define PMAP_SHPGPERPROC 200
120 #if defined(DIAGNOSTIC)
121 #define PMAP_DIAGNOSTIC
127 * Get PDEs and PTEs for user/kernel address space
129 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
130 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
132 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & PG_V) != 0)
133 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & PG_W) != 0)
134 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & PG_M) != 0)
135 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & PG_A) != 0)
136 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & PG_V) != 0)
140 * Given a map and a machine independent protection code,
141 * convert to a vax protection code.
143 #define pte_prot(m, p) \
144 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
145 static int protection_codes[8];
147 struct pmap kernel_pmap;
148 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
150 vm_paddr_t avail_start; /* PA of first available physical page */
151 vm_paddr_t avail_end; /* PA of last available physical page */
152 vm_offset_t virtual2_start; /* cutout free area prior to kernel start */
153 vm_offset_t virtual2_end;
154 vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */
155 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
156 vm_offset_t KvaStart; /* VA start of KVA space */
157 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
158 vm_offset_t KvaSize; /* max size of kernel virtual address space */
159 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
160 static int pgeflag; /* PG_G or-in */
161 static int pseflag; /* PG_PS or-in */
163 static vm_object_t kptobj;
166 static vm_paddr_t dmaplimit;
168 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
170 static uint64_t KPTbase;
171 static uint64_t KPTphys;
172 static uint64_t KPDphys; /* phys addr of kernel level 2 */
173 static uint64_t KPDbase; /* phys addr of kernel level 2 @ KERNBASE */
174 uint64_t KPDPphys; /* phys addr of kernel level 3 */
175 uint64_t KPML4phys; /* phys addr of kernel level 4 */
177 static uint64_t DMPDphys; /* phys addr of direct mapped level 2 */
178 static uint64_t DMPDPphys; /* phys addr of direct mapped level 3 */
181 * Data for the pv entry allocation mechanism
183 static vm_zone_t pvzone;
184 static struct vm_zone pvzone_store;
185 static struct vm_object pvzone_obj;
186 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
187 static int pmap_pagedaemon_waken = 0;
188 static struct pv_entry *pvinit;
191 * All those kernel PT submaps that BSD is so fond of
193 pt_entry_t *CMAP1 = 0, *ptmmap;
194 caddr_t CADDR1 = 0, ptvmmap = 0;
195 static pt_entry_t *msgbufmap;
196 struct msgbuf *msgbufp=0;
201 static pt_entry_t *pt_crashdumpmap;
202 static caddr_t crashdumpmap;
204 extern pt_entry_t *SMPpt;
205 extern uint64_t SMPptpa;
209 static pv_entry_t get_pv_entry (void);
210 static void i386_protection_init (void);
211 static void create_pagetables(vm_paddr_t *firstaddr);
212 static void pmap_remove_all (vm_page_t m);
213 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
214 vm_offset_t sva, pmap_inval_info_t info);
215 static void pmap_remove_page (struct pmap *pmap,
216 vm_offset_t va, pmap_inval_info_t info);
217 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
218 vm_offset_t va, pmap_inval_info_t info);
219 static boolean_t pmap_testbit (vm_page_t m, int bit);
220 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
221 vm_page_t mpte, vm_page_t m);
223 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
225 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
226 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
227 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
228 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
229 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
230 pmap_inval_info_t info);
231 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
232 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
234 static unsigned pdir4mb;
237 * Move the kernel virtual free pointer to the next
238 * 2MB. This is used to help improve performance
239 * by using a large (2MB) page for much of the kernel
240 * (.text, .data, .bss)
244 pmap_kmem_choose(vm_offset_t addr)
246 vm_offset_t newaddr = addr;
248 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
255 * Super fast pmap_pte routine best used when scanning the pv lists.
256 * This eliminates many course-grained invltlb calls. Note that many of
257 * the pv list scans are across different pmaps and it is very wasteful
258 * to do an entire invltlb when checking a single mapping.
260 * Should only be called while in a critical section.
262 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
266 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
268 return pmap_pte(pmap, va);
271 /* Return a non-clipped PD index for a given VA */
274 pmap_pde_pindex(vm_offset_t va)
276 return va >> PDRSHIFT;
279 /* Return various clipped indexes for a given VA */
282 pmap_pte_index(vm_offset_t va)
285 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
290 pmap_pde_index(vm_offset_t va)
293 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
298 pmap_pdpe_index(vm_offset_t va)
301 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
306 pmap_pml4e_index(vm_offset_t va)
309 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
312 /* Return a pointer to the PML4 slot that corresponds to a VA */
315 pmap_pml4e(pmap_t pmap, vm_offset_t va)
318 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
321 /* Return a pointer to the PDP slot that corresponds to a VA */
324 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
328 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME);
329 return (&pdpe[pmap_pdpe_index(va)]);
332 /* Return a pointer to the PDP slot that corresponds to a VA */
335 pmap_pdpe(pmap_t pmap, vm_offset_t va)
339 pml4e = pmap_pml4e(pmap, va);
340 if ((*pml4e & PG_V) == 0)
342 return (pmap_pml4e_to_pdpe(pml4e, va));
345 /* Return a pointer to the PD slot that corresponds to a VA */
348 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
352 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME);
353 return (&pde[pmap_pde_index(va)]);
356 /* Return a pointer to the PD slot that corresponds to a VA */
359 pmap_pde(pmap_t pmap, vm_offset_t va)
363 pdpe = pmap_pdpe(pmap, va);
364 if (pdpe == NULL || (*pdpe & PG_V) == 0)
366 return (pmap_pdpe_to_pde(pdpe, va));
369 /* Return a pointer to the PT slot that corresponds to a VA */
372 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
376 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
377 return (&pte[pmap_pte_index(va)]);
380 /* Return a pointer to the PT slot that corresponds to a VA */
383 pmap_pte(pmap_t pmap, vm_offset_t va)
387 pde = pmap_pde(pmap, va);
388 if (pde == NULL || (*pde & PG_V) == 0)
390 if ((*pde & PG_PS) != 0) /* compat with i386 pmap_pte() */
391 return ((pt_entry_t *)pde);
392 return (pmap_pde_to_pte(pde, va));
397 vtopte(vm_offset_t va)
399 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
401 return (PTmap + ((va >> PAGE_SHIFT) & mask));
406 vtopde(vm_offset_t va)
408 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
410 return (PDmap + ((va >> PDRSHIFT) & mask));
414 allocpages(vm_paddr_t *firstaddr, int n)
419 bzero((void *)ret, n * PAGE_SIZE);
420 *firstaddr += n * PAGE_SIZE;
426 create_pagetables(vm_paddr_t *firstaddr)
430 /* we are running (mostly) V=P at this point */
433 KPTbase = allocpages(firstaddr, NKPT);
434 KPTphys = allocpages(firstaddr, NKPT);
435 KPML4phys = allocpages(firstaddr, 1);
436 KPDPphys = allocpages(firstaddr, NKPML4E);
439 * Calculate the page directory base for KERNBASE,
440 * that is where we start populating the page table pages.
441 * Basically this is the end - 2.
443 KPDphys = allocpages(firstaddr, NKPDPE);
444 KPDbase = KPDphys + ((NKPDPE - (NPDPEPG - KPDPI)) << PAGE_SHIFT);
446 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
447 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
449 DMPDPphys = allocpages(firstaddr, NDMPML4E);
450 if ((amd_feature & AMDID_PAGE1GB) == 0)
451 DMPDphys = allocpages(firstaddr, ndmpdp);
452 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
455 * Fill in the underlying page table pages for the area around
456 * KERNBASE. This remaps low physical memory to KERNBASE.
458 * Read-only from zero to physfree
459 * XXX not fully used, underneath 2M pages
461 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
462 ((pt_entry_t *)KPTbase)[i] = i << PAGE_SHIFT;
463 ((pt_entry_t *)KPTbase)[i] |= PG_RW | PG_V | PG_G;
467 * Now map the initial kernel page tables. One block of page
468 * tables is placed at the beginning of kernel virtual memory,
469 * and another block is placed at KERNBASE to map the kernel binary,
470 * data, bss, and initial pre-allocations.
472 for (i = 0; i < NKPT; i++) {
473 ((pd_entry_t *)KPDbase)[i] = KPTbase + (i << PAGE_SHIFT);
474 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V;
476 for (i = 0; i < NKPT; i++) {
477 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
478 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
482 * Map from zero to end of allocations using 2M pages as an
483 * optimization. This will bypass some of the KPTBase pages
484 * above in the KERNBASE area.
486 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
487 ((pd_entry_t *)KPDbase)[i] = i << PDRSHIFT;
488 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V | PG_PS | PG_G;
492 * And connect up the PD to the PDP. The kernel pmap is expected
493 * to pre-populate all of its PDs. See NKPDPE in vmparam.h.
495 for (i = 0; i < NKPDPE; i++) {
496 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] =
497 KPDphys + (i << PAGE_SHIFT);
498 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] |=
502 /* Now set up the direct map space using either 2MB or 1GB pages */
503 /* Preset PG_M and PG_A because demotion expects it */
504 if ((amd_feature & AMDID_PAGE1GB) == 0) {
505 for (i = 0; i < NPDEPG * ndmpdp; i++) {
506 ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT;
507 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
510 /* And the direct map space's PDP */
511 for (i = 0; i < ndmpdp; i++) {
512 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
514 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
517 for (i = 0; i < ndmpdp; i++) {
518 ((pdp_entry_t *)DMPDPphys)[i] =
519 (vm_paddr_t)i << PDPSHIFT;
520 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
525 /* And recursively map PML4 to itself in order to get PTmap */
526 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
527 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
529 /* Connect the Direct Map slot up to the PML4 */
530 ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
531 ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
533 /* Connect the KVA slot up to the PML4 */
534 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
535 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
539 init_paging(vm_paddr_t *firstaddr)
541 create_pagetables(firstaddr);
545 * Bootstrap the system enough to run with virtual memory.
547 * On the i386 this is called after mapping has already been enabled
548 * and just syncs the pmap module with what has already been done.
549 * [We can't call it easily with mapping off since the kernel is not
550 * mapped with PA == VA, hence we would have to relocate every address
551 * from the linked base (virtual) address "KERNBASE" to the actual
552 * (physical) address starting relative to 0]
555 pmap_bootstrap(vm_paddr_t *firstaddr)
559 struct mdglobaldata *gd;
562 KvaStart = VM_MIN_KERNEL_ADDRESS;
563 KvaEnd = VM_MAX_KERNEL_ADDRESS;
564 KvaSize = KvaEnd - KvaStart;
566 avail_start = *firstaddr;
569 * Create an initial set of page tables to run the kernel in.
571 create_pagetables(firstaddr);
573 virtual2_start = KvaStart;
574 virtual2_end = PTOV_OFFSET;
576 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
577 virtual_start = pmap_kmem_choose(virtual_start);
579 virtual_end = VM_MAX_KERNEL_ADDRESS;
581 /* XXX do %cr0 as well */
582 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
586 * Initialize protection array.
588 i386_protection_init();
591 * The kernel's pmap is statically allocated so we don't have to use
592 * pmap_create, which is unlikely to work correctly at this part of
593 * the boot sequence (XXX and which no longer exists).
595 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
596 kernel_pmap.pm_count = 1;
597 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
598 TAILQ_INIT(&kernel_pmap.pm_pvlist);
602 * Reserve some special page table entries/VA space for temporary
605 #define SYSMAP(c, p, v, n) \
606 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
610 pte = (pt_entry_t *) pmap_pte(&kernel_pmap, va);
616 * CMAP1/CMAP2 are used for zeroing and copying pages.
618 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
623 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
626 * ptvmmap is used for reading arbitrary physical pages via
629 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
632 * msgbufp is used to map the system message buffer.
633 * XXX msgbufmap is not used.
635 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
636 atop(round_page(MSGBUF_SIZE)))
643 * PG_G is terribly broken on SMP because we IPI invltlb's in some
644 * cases rather then invl1pg. Actually, I don't even know why it
645 * works under UP because self-referential page table mappings
650 if (cpu_feature & CPUID_PGE)
655 * Initialize the 4MB page size flag
659 * The 4MB page version of the initial
660 * kernel page mapping.
664 #if !defined(DISABLE_PSE)
665 if (cpu_feature & CPUID_PSE) {
668 * Note that we have enabled PSE mode
671 ptditmp = *(PTmap + x86_64_btop(KERNBASE));
672 ptditmp &= ~(NBPDR - 1);
673 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
678 * Enable the PSE mode. If we are SMP we can't do this
679 * now because the APs will not be able to use it when
682 load_cr4(rcr4() | CR4_PSE);
685 * We can do the mapping here for the single processor
686 * case. We simply ignore the old page table page from
690 * For SMP, we still need 4K pages to bootstrap APs,
691 * PSE will be enabled as soon as all APs are up.
693 PTD[KPTDI] = (pd_entry_t)ptditmp;
699 if (cpu_apic_address == 0)
700 panic("pmap_bootstrap: no local apic!");
704 * We need to finish setting up the globaldata page for the BSP.
705 * locore has already populated the page table for the mdglobaldata
708 pg = MDGLOBALDATA_BASEALLOC_PAGES;
709 gd = &CPU_prvspace[0].mdglobaldata;
710 gd->gd_CMAP1 = &SMPpt[pg + 0];
711 gd->gd_CMAP2 = &SMPpt[pg + 1];
712 gd->gd_CMAP3 = &SMPpt[pg + 2];
713 gd->gd_PMAP1 = &SMPpt[pg + 3];
714 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
715 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
716 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
717 gd->gd_PADDR1 = (pt_entry_t *)CPU_prvspace[0].PPAGE1;
724 * Set 4mb pdir for mp startup
729 if (pseflag && (cpu_feature & CPUID_PSE)) {
730 load_cr4(rcr4() | CR4_PSE);
731 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
739 * Initialize the pmap module.
740 * Called by vm_init, to initialize any structures that the pmap
741 * system needs to map virtual memory.
742 * pmap_init has been enhanced to support in a fairly consistant
743 * way, discontiguous physical memory.
752 * object for kernel page table pages
754 /* JG I think the number can be arbitrary */
755 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
758 * Allocate memory for random pmap data structures. Includes the
762 for(i = 0; i < vm_page_array_size; i++) {
765 m = &vm_page_array[i];
766 TAILQ_INIT(&m->md.pv_list);
767 m->md.pv_list_count = 0;
771 * init the pv free list
773 initial_pvs = vm_page_array_size;
774 if (initial_pvs < MINPV)
776 pvzone = &pvzone_store;
777 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
778 initial_pvs * sizeof (struct pv_entry));
779 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
783 * Now it is safe to enable pv_table recording.
785 pmap_initialized = TRUE;
787 lapic = pmap_mapdev_uncacheable(cpu_apic_address, sizeof(struct LAPIC));
792 * Initialize the address space (zone) for the pv_entries. Set a
793 * high water mark so that the system can recover from excessive
794 * numbers of pv entries.
799 int shpgperproc = PMAP_SHPGPERPROC;
801 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
802 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
803 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
804 pv_entry_high_water = 9 * (pv_entry_max / 10);
805 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
809 /***************************************************
810 * Low level helper routines.....
811 ***************************************************/
813 #if defined(PMAP_DIAGNOSTIC)
816 * This code checks for non-writeable/modified pages.
817 * This should be an invalid condition.
821 pmap_nw_modified(pt_entry_t pte)
823 if ((pte & (PG_M|PG_RW)) == PG_M)
832 * this routine defines the region(s) of memory that should
833 * not be tested for the modified bit.
837 pmap_track_modified(vm_offset_t va)
839 if ((va < clean_sva) || (va >= clean_eva))
846 * Extract the physical page address associated with the map/VA pair.
848 * The caller must hold vm_token if non-blocking operation is desired.
851 pmap_extract(pmap_t pmap, vm_offset_t va)
855 pd_entry_t pde, *pdep;
857 lwkt_gettoken(&vm_token);
859 pdep = pmap_pde(pmap, va);
863 if ((pde & PG_PS) != 0) {
864 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
866 pte = pmap_pde_to_pte(pdep, va);
867 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
871 lwkt_reltoken(&vm_token);
876 * Extract the physical page address associated kernel virtual address.
879 pmap_kextract(vm_offset_t va)
884 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
885 pa = DMAP_TO_PHYS(va);
889 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
892 * Beware of a concurrent promotion that changes the
893 * PDE at this point! For example, vtopte() must not
894 * be used to access the PTE because it would use the
895 * new PDE. It is, however, safe to use the old PDE
896 * because the page table page is preserved by the
899 pa = *pmap_pde_to_pte(&pde, va);
900 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
906 /***************************************************
907 * Low level mapping routines.....
908 ***************************************************/
911 * Routine: pmap_kenter
913 * Add a wired page to the KVA
914 * NOTE! note that in order for the mapping to take effect -- you
915 * should do an invltlb after doing the pmap_kenter().
918 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
922 pmap_inval_info info;
924 pmap_inval_init(&info);
925 npte = pa | PG_RW | PG_V | pgeflag;
927 pmap_inval_interlock(&info, &kernel_pmap, va);
929 pmap_inval_deinterlock(&info, &kernel_pmap);
930 pmap_inval_done(&info);
934 * Routine: pmap_kenter_quick
936 * Similar to pmap_kenter(), except we only invalidate the
937 * mapping on the current CPU.
940 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
945 npte = pa | PG_RW | PG_V | pgeflag;
948 cpu_invlpg((void *)va);
952 pmap_kenter_sync(vm_offset_t va)
954 pmap_inval_info info;
956 pmap_inval_init(&info);
957 pmap_inval_interlock(&info, &kernel_pmap, va);
958 pmap_inval_deinterlock(&info, &kernel_pmap);
959 pmap_inval_done(&info);
963 pmap_kenter_sync_quick(vm_offset_t va)
965 cpu_invlpg((void *)va);
969 * remove a page from the kernel pagetables
972 pmap_kremove(vm_offset_t va)
975 pmap_inval_info info;
977 pmap_inval_init(&info);
979 pmap_inval_interlock(&info, &kernel_pmap, va);
981 pmap_inval_deinterlock(&info, &kernel_pmap);
982 pmap_inval_done(&info);
986 pmap_kremove_quick(vm_offset_t va)
991 cpu_invlpg((void *)va);
995 * XXX these need to be recoded. They are not used in any critical path.
998 pmap_kmodify_rw(vm_offset_t va)
1000 *vtopte(va) |= PG_RW;
1001 cpu_invlpg((void *)va);
1005 pmap_kmodify_nc(vm_offset_t va)
1007 *vtopte(va) |= PG_N;
1008 cpu_invlpg((void *)va);
1012 * Used to map a range of physical addresses into kernel
1013 * virtual address space.
1015 * For now, VM is already on, we only need to map the
1019 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
1021 return PHYS_TO_DMAP(start);
1026 * Add a list of wired pages to the kva
1027 * this routine is only used for temporary
1028 * kernel mappings that do not need to have
1029 * page modification or references recorded.
1030 * Note that old mappings are simply written
1031 * over. The page *must* be wired.
1034 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1038 end_va = va + count * PAGE_SIZE;
1040 while (va < end_va) {
1044 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1045 cpu_invlpg((void *)va);
1050 smp_invltlb(); /* XXX */
1055 * This routine jerks page mappings from the
1056 * kernel -- it is meant only for temporary mappings.
1058 * MPSAFE, INTERRUPT SAFE (cluster callback)
1061 pmap_qremove(vm_offset_t va, int count)
1065 end_va = va + count * PAGE_SIZE;
1067 while (va < end_va) {
1072 cpu_invlpg((void *)va);
1081 * This routine works like vm_page_lookup() but also blocks as long as the
1082 * page is busy. This routine does not busy the page it returns.
1084 * Unless the caller is managing objects whos pages are in a known state,
1085 * the call should be made with a critical section held so the page's object
1086 * association remains valid on return.
1090 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
1095 m = vm_page_lookup(object, pindex);
1096 } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
1102 * Create a new thread and optionally associate it with a (new) process.
1103 * NOTE! the new thread's cpu may not equal the current cpu.
1106 pmap_init_thread(thread_t td)
1108 /* enforce pcb placement */
1109 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1110 td->td_savefpu = &td->td_pcb->pcb_save;
1111 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */
1115 * This routine directly affects the fork perf for a process.
1118 pmap_init_proc(struct proc *p)
1123 * Dispose the UPAGES for a process that has exited.
1124 * This routine directly impacts the exit perf of a process.
1127 pmap_dispose_proc(struct proc *p)
1129 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1132 /***************************************************
1133 * Page table page management routines.....
1134 ***************************************************/
1137 * This routine unholds page table pages, and if the hold count
1138 * drops to zero, then it decrements the wire count.
1142 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1143 pmap_inval_info_t info)
1145 KKASSERT(m->hold_count > 0);
1146 if (m->hold_count > 1) {
1150 return _pmap_unwire_pte_hold(pmap, va, m, info);
1156 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1157 pmap_inval_info_t info)
1160 * Wait until we can busy the page ourselves. We cannot have
1161 * any active flushes if we block. We own one hold count on the
1162 * page so it cannot be freed out from under us.
1164 if (m->flags & PG_BUSY) {
1165 pmap_inval_flush(info);
1166 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1169 KASSERT(m->queue == PQ_NONE,
1170 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1173 * This case can occur if new references were acquired while
1176 if (m->hold_count > 1) {
1177 KKASSERT(m->hold_count > 1);
1183 * Unmap the page table page
1185 KKASSERT(m->hold_count == 1);
1187 pmap_inval_interlock(info, pmap, -1);
1189 if (m->pindex >= (NUPDE + NUPDPE)) {
1192 pml4 = pmap_pml4e(pmap, va);
1194 } else if (m->pindex >= NUPDE) {
1197 pdp = pmap_pdpe(pmap, va);
1202 pd = pmap_pde(pmap, va);
1206 KKASSERT(pmap->pm_stats.resident_count > 0);
1207 --pmap->pm_stats.resident_count;
1209 if (pmap->pm_ptphint == m)
1210 pmap->pm_ptphint = NULL;
1211 pmap_inval_deinterlock(info, pmap);
1213 if (m->pindex < NUPDE) {
1214 /* We just released a PT, unhold the matching PD */
1217 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1218 pmap_unwire_pte_hold(pmap, va, pdpg, info);
1220 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1221 /* We just released a PD, unhold the matching PDP */
1224 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1225 pmap_unwire_pte_hold(pmap, va, pdppg, info);
1229 * This was our last hold, the page had better be unwired
1230 * after we decrement wire_count.
1232 * FUTURE NOTE: shared page directory page could result in
1233 * multiple wire counts.
1237 KKASSERT(m->wire_count == 0);
1238 --vmstats.v_wire_count;
1239 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1241 vm_page_free_zero(m);
1247 * After removing a page table entry, this routine is used to
1248 * conditionally free the page, and manage the hold/wire counts.
1252 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1253 pmap_inval_info_t info)
1255 vm_pindex_t ptepindex;
1257 if (va >= VM_MAX_USER_ADDRESS)
1261 ptepindex = pmap_pde_pindex(va);
1263 if (pmap->pm_ptphint &&
1264 (pmap->pm_ptphint->pindex == ptepindex)) {
1265 mpte = pmap->pm_ptphint;
1268 pmap_inval_flush(info);
1269 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1270 pmap->pm_ptphint = mpte;
1275 return pmap_unwire_pte_hold(pmap, va, mpte, info);
1279 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1280 * it, and IdlePTD, represents the template used to update all other pmaps.
1282 * On architectures where the kernel pmap is not integrated into the user
1283 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1284 * kernel_pmap should be used to directly access the kernel_pmap.
1287 pmap_pinit0(struct pmap *pmap)
1289 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1291 pmap->pm_active = 0;
1292 pmap->pm_ptphint = NULL;
1293 TAILQ_INIT(&pmap->pm_pvlist);
1294 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1298 * Initialize a preallocated and zeroed pmap structure,
1299 * such as one in a vmspace structure.
1302 pmap_pinit(struct pmap *pmap)
1307 * No need to allocate page table space yet but we do need a valid
1308 * page directory table.
1310 if (pmap->pm_pml4 == NULL) {
1312 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1316 * Allocate an object for the ptes
1318 if (pmap->pm_pteobj == NULL)
1319 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1322 * Allocate the page directory page, unless we already have
1323 * one cached. If we used the cached page the wire_count will
1324 * already be set appropriately.
1326 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1327 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1328 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1329 pmap->pm_pdirm = ptdpg;
1330 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1331 ptdpg->valid = VM_PAGE_BITS_ALL;
1332 if (ptdpg->wire_count == 0)
1333 ++vmstats.v_wire_count;
1334 ptdpg->wire_count = 1;
1335 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1337 if ((ptdpg->flags & PG_ZERO) == 0)
1338 bzero(pmap->pm_pml4, PAGE_SIZE);
1340 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1341 pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1343 /* install self-referential address mapping entry */
1344 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1347 pmap->pm_active = 0;
1348 pmap->pm_ptphint = NULL;
1349 TAILQ_INIT(&pmap->pm_pvlist);
1350 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1351 pmap->pm_stats.resident_count = 1;
1355 * Clean up a pmap structure so it can be physically freed. This routine
1356 * is called by the vmspace dtor function. A great deal of pmap data is
1357 * left passively mapped to improve vmspace management so we have a bit
1358 * of cleanup work to do here.
1361 pmap_puninit(pmap_t pmap)
1365 KKASSERT(pmap->pm_active == 0);
1366 lwkt_gettoken(&vm_token);
1367 if ((p = pmap->pm_pdirm) != NULL) {
1368 KKASSERT(pmap->pm_pml4 != NULL);
1369 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1370 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1372 vmstats.v_wire_count--;
1373 KKASSERT((p->flags & PG_BUSY) == 0);
1375 vm_page_free_zero(p);
1376 pmap->pm_pdirm = NULL;
1378 if (pmap->pm_pml4) {
1379 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1380 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1381 pmap->pm_pml4 = NULL;
1383 if (pmap->pm_pteobj) {
1384 vm_object_deallocate(pmap->pm_pteobj);
1385 pmap->pm_pteobj = NULL;
1387 lwkt_reltoken(&vm_token);
1391 * Wire in kernel global address entries. To avoid a race condition
1392 * between pmap initialization and pmap_growkernel, this procedure
1393 * adds the pmap to the master list (which growkernel scans to update),
1394 * then copies the template.
1397 pmap_pinit2(struct pmap *pmap)
1400 lwkt_gettoken(&vm_token);
1401 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1402 /* XXX copies current process, does not fill in MPPTDI */
1403 lwkt_reltoken(&vm_token);
1408 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1409 * 0 on failure (if the procedure had to sleep).
1411 * When asked to remove the page directory page itself, we actually just
1412 * leave it cached so we do not have to incur the SMP inval overhead of
1413 * removing the kernel mapping. pmap_puninit() will take care of it.
1417 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1420 * This code optimizes the case of freeing non-busy
1421 * page-table pages. Those pages are zero now, and
1422 * might as well be placed directly into the zero queue.
1424 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1430 * Remove the page table page from the processes address space.
1432 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1434 * We are the pml4 table itself.
1436 /* XXX anything to do here? */
1437 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1439 * Remove a PDP page from the PML4. We do not maintain
1440 * hold counts on the PML4 page.
1446 m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1447 KKASSERT(m4 != NULL);
1448 pml4 = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1449 idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1450 KKASSERT(pml4[idx] != 0);
1452 } else if (p->pindex >= NUPDE) {
1454 * Remove a PD page from the PDP and drop the hold count
1455 * on the PDP. The PDP is left cached in the pmap if
1456 * the hold count drops to 0 so the wire count remains
1463 m3 = vm_page_lookup(pmap->pm_pteobj,
1464 NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1465 KKASSERT(m3 != NULL);
1466 pdp = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1467 idx = (p->pindex - NUPDE) % NPDPEPG;
1468 KKASSERT(pdp[idx] != 0);
1473 * Remove a PT page from the PD and drop the hold count
1474 * on the PD. The PD is left cached in the pmap if
1475 * the hold count drops to 0 so the wire count remains
1482 m2 = vm_page_lookup(pmap->pm_pteobj,
1483 NUPDE + p->pindex / NPDEPG);
1484 KKASSERT(m2 != NULL);
1485 pd = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1486 idx = p->pindex % NPDEPG;
1492 * One fewer mappings in the pmap. p's hold count had better
1495 KKASSERT(pmap->pm_stats.resident_count > 0);
1496 --pmap->pm_stats.resident_count;
1498 panic("pmap_release: freeing held page table page");
1499 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1500 pmap->pm_ptphint = NULL;
1503 * We leave the top-level page table page cached, wired, and mapped in
1504 * the pmap until the dtor function (pmap_puninit()) gets called.
1505 * However, still clean it up so we can set PG_ZERO.
1507 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1508 bzero(pmap->pm_pml4, PAGE_SIZE);
1509 vm_page_flag_set(p, PG_ZERO);
1513 KKASSERT(p->wire_count == 0);
1514 vmstats.v_wire_count--;
1515 /* JG eventually revert to using vm_page_free_zero() */
1522 * This routine is called when various levels in the page table need to
1523 * be populated. This routine cannot fail.
1527 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1532 * Find or fabricate a new pagetable page. This will busy the page.
1534 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1535 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1536 if ((m->flags & PG_ZERO) == 0) {
1537 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1540 KASSERT(m->queue == PQ_NONE,
1541 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1544 * Increment the hold count for the page we will be returning to
1548 if (m->wire_count++ == 0)
1549 vmstats.v_wire_count++;
1552 * Map the pagetable page into the process address space, if
1553 * it isn't already there.
1555 * It is possible that someone else got in and mapped the page
1556 * directory page while we were blocked, if so just unbusy and
1557 * return the held page.
1559 if (ptepindex >= (NUPDE + NUPDPE)) {
1561 * Wire up a new PDP page in the PML4
1563 vm_pindex_t pml4index;
1566 pml4index = ptepindex - (NUPDE + NUPDPE);
1567 pml4 = &pmap->pm_pml4[pml4index];
1569 if (--m->wire_count == 0)
1570 --vmstats.v_wire_count;
1574 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1575 } else if (ptepindex >= NUPDE) {
1577 * Wire up a new PD page in the PDP
1579 vm_pindex_t pml4index;
1580 vm_pindex_t pdpindex;
1585 pdpindex = ptepindex - NUPDE;
1586 pml4index = pdpindex >> NPML4EPGSHIFT;
1588 pml4 = &pmap->pm_pml4[pml4index];
1589 if ((*pml4 & PG_V) == 0) {
1591 * Have to allocate a new PDP page, recurse.
1592 * This always succeeds. Returned page will
1595 pdppg = _pmap_allocpte(pmap,
1596 NUPDE + NUPDPE + pml4index);
1599 * Add a held reference to the PDP page.
1601 pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1602 pdppg->hold_count++;
1606 * Now find the pdp_entry and map the PDP. If the PDP
1607 * has already been mapped unwind and return the
1608 * already-mapped PDP held.
1610 * pdppg is left held (hold_count is incremented for
1611 * each PD in the PDP).
1613 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1614 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1616 vm_page_unhold(pdppg);
1617 if (--m->wire_count == 0)
1618 --vmstats.v_wire_count;
1622 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1625 * Wire up the new PT page in the PD
1627 vm_pindex_t pml4index;
1628 vm_pindex_t pdpindex;
1634 pdpindex = ptepindex >> NPDPEPGSHIFT;
1635 pml4index = pdpindex >> NPML4EPGSHIFT;
1638 * Locate the PDP page in the PML4, then the PD page in
1639 * the PDP. If either does not exist we simply recurse
1642 * We can just recurse on the PD page as it will recurse
1643 * on the PDP if necessary.
1645 pml4 = &pmap->pm_pml4[pml4index];
1646 if ((*pml4 & PG_V) == 0) {
1647 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1648 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1649 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1651 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1652 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1653 if ((*pdp & PG_V) == 0) {
1654 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1656 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1662 * Now fill in the pte in the PD. If the pte already exists
1663 * (again, if we raced the grab), unhold pdpg and unwire
1664 * m, returning a held m.
1666 * pdpg is left held (hold_count is incremented for
1667 * each PT in the PD).
1669 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1670 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1672 vm_page_unhold(pdpg);
1673 if (--m->wire_count == 0)
1674 --vmstats.v_wire_count;
1678 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1682 * We successfully loaded a PDP, PD, or PTE. Set the page table hint,
1683 * valid bits, mapped flag, unbusy, and we're done.
1685 pmap->pm_ptphint = m;
1686 ++pmap->pm_stats.resident_count;
1688 m->valid = VM_PAGE_BITS_ALL;
1689 vm_page_flag_clear(m, PG_ZERO);
1690 vm_page_flag_set(m, PG_MAPPED);
1698 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1700 vm_pindex_t ptepindex;
1705 * Calculate pagetable page index
1707 ptepindex = pmap_pde_pindex(va);
1710 * Get the page directory entry
1712 pd = pmap_pde(pmap, va);
1715 * This supports switching from a 2MB page to a
1718 if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1719 panic("no promotion/demotion yet");
1727 * If the page table page is mapped, we just increment the
1728 * hold count, and activate it.
1730 if (pd != NULL && (*pd & PG_V) != 0) {
1731 /* YYY hint is used here on i386 */
1732 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1733 pmap->pm_ptphint = m;
1738 * Here if the pte page isn't mapped, or if it has been deallocated.
1740 return _pmap_allocpte(pmap, ptepindex);
1744 /***************************************************
1745 * Pmap allocation/deallocation routines.
1746 ***************************************************/
1749 * Release any resources held by the given physical map.
1750 * Called when a pmap initialized by pmap_pinit is being released.
1751 * Should only be called if the map contains no valid mappings.
1753 static int pmap_release_callback(struct vm_page *p, void *data);
1756 pmap_release(struct pmap *pmap)
1758 vm_object_t object = pmap->pm_pteobj;
1759 struct rb_vm_page_scan_info info;
1761 KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active));
1762 #if defined(DIAGNOSTIC)
1763 if (object->ref_count != 1)
1764 panic("pmap_release: pteobj reference count != 1");
1768 info.object = object;
1770 lwkt_gettoken(&vm_token);
1771 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1778 info.limit = object->generation;
1780 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1781 pmap_release_callback, &info);
1782 if (info.error == 0 && info.mpte) {
1783 if (!pmap_release_free_page(pmap, info.mpte))
1787 } while (info.error);
1788 lwkt_reltoken(&vm_token);
1793 pmap_release_callback(struct vm_page *p, void *data)
1795 struct rb_vm_page_scan_info *info = data;
1797 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1801 if (!pmap_release_free_page(info->pmap, p)) {
1805 if (info->object->generation != info->limit) {
1813 * Grow the number of kernel page table entries, if needed.
1816 pmap_growkernel(vm_offset_t addr)
1819 vm_offset_t ptppaddr;
1821 pd_entry_t *pde, newpdir;
1825 lwkt_gettoken(&vm_token);
1826 if (kernel_vm_end == 0) {
1827 kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
1829 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1830 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1832 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1833 kernel_vm_end = kernel_map.max_offset;
1838 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1839 if (addr - 1 >= kernel_map.max_offset)
1840 addr = kernel_map.max_offset;
1841 while (kernel_vm_end < addr) {
1842 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1844 /* We need a new PDP entry */
1845 nkpg = vm_page_alloc(kptobj, nkpt,
1846 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1847 | VM_ALLOC_INTERRUPT);
1849 panic("pmap_growkernel: no memory to grow kernel");
1850 paddr = VM_PAGE_TO_PHYS(nkpg);
1851 if ((nkpg->flags & PG_ZERO) == 0)
1852 pmap_zero_page(paddr);
1853 vm_page_flag_clear(nkpg, PG_ZERO);
1854 newpdp = (pdp_entry_t)
1855 (paddr | PG_V | PG_RW | PG_A | PG_M);
1856 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1858 continue; /* try again */
1860 if ((*pde & PG_V) != 0) {
1861 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1862 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1863 kernel_vm_end = kernel_map.max_offset;
1870 * This index is bogus, but out of the way
1872 nkpg = vm_page_alloc(kptobj, nkpt,
1873 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1875 panic("pmap_growkernel: no memory to grow kernel");
1878 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1879 pmap_zero_page(ptppaddr);
1880 vm_page_flag_clear(nkpg, PG_ZERO);
1881 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1882 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1885 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1886 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1887 kernel_vm_end = kernel_map.max_offset;
1891 lwkt_reltoken(&vm_token);
1896 * Retire the given physical map from service.
1897 * Should only be called if the map contains
1898 * no valid mappings.
1901 pmap_destroy(pmap_t pmap)
1908 lwkt_gettoken(&vm_token);
1909 count = --pmap->pm_count;
1912 panic("destroying a pmap is not yet implemented");
1914 lwkt_reltoken(&vm_token);
1918 * Add a reference to the specified pmap.
1921 pmap_reference(pmap_t pmap)
1924 lwkt_gettoken(&vm_token);
1926 lwkt_reltoken(&vm_token);
1930 /***************************************************
1931 * page management routines.
1932 ***************************************************/
1935 * free the pv_entry back to the free list. This function may be
1936 * called from an interrupt.
1940 free_pv_entry(pv_entry_t pv)
1943 KKASSERT(pv_entry_count >= 0);
1948 * get a new pv_entry, allocating a block from the system
1949 * when needed. This function may be called from an interrupt.
1956 if (pv_entry_high_water &&
1957 (pv_entry_count > pv_entry_high_water) &&
1958 (pmap_pagedaemon_waken == 0)) {
1959 pmap_pagedaemon_waken = 1;
1960 wakeup(&vm_pages_needed);
1962 return zalloc(pvzone);
1966 * This routine is very drastic, but can save the system
1974 static int warningdone=0;
1976 if (pmap_pagedaemon_waken == 0)
1978 lwkt_gettoken(&vm_token);
1979 if (warningdone < 5) {
1980 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1984 for(i = 0; i < vm_page_array_size; i++) {
1985 m = &vm_page_array[i];
1986 if (m->wire_count || m->hold_count || m->busy ||
1987 (m->flags & PG_BUSY))
1991 pmap_pagedaemon_waken = 0;
1992 lwkt_reltoken(&vm_token);
1997 * If it is the first entry on the list, it is actually
1998 * in the header and we must copy the following entry up
1999 * to the header. Otherwise we must search the list for
2000 * the entry. In either case we free the now unused entry.
2004 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
2005 vm_offset_t va, pmap_inval_info_t info)
2011 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
2012 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2013 if (pmap == pv->pv_pmap && va == pv->pv_va)
2017 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
2018 if (va == pv->pv_va)
2026 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2027 m->md.pv_list_count--;
2028 KKASSERT(m->md.pv_list_count >= 0);
2029 if (TAILQ_EMPTY(&m->md.pv_list))
2030 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2031 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2032 ++pmap->pm_generation;
2033 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
2041 * Create a pv entry for page at pa for
2046 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
2051 pv = get_pv_entry();
2056 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
2057 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2058 ++pmap->pm_generation;
2059 m->md.pv_list_count++;
2065 * pmap_remove_pte: do the things to unmap a page in a process
2069 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
2070 pmap_inval_info_t info)
2075 pmap_inval_interlock(info, pmap, va);
2076 oldpte = pte_load_clear(ptq);
2077 pmap_inval_deinterlock(info, pmap);
2079 pmap->pm_stats.wired_count -= 1;
2081 * Machines that don't support invlpg, also don't support
2082 * PG_G. XXX PG_G is disabled for SMP so don't worry about
2086 cpu_invlpg((void *)va);
2087 KKASSERT(pmap->pm_stats.resident_count > 0);
2088 --pmap->pm_stats.resident_count;
2089 if (oldpte & PG_MANAGED) {
2090 m = PHYS_TO_VM_PAGE(oldpte);
2091 if (oldpte & PG_M) {
2092 #if defined(PMAP_DIAGNOSTIC)
2093 if (pmap_nw_modified((pt_entry_t) oldpte)) {
2095 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2099 if (pmap_track_modified(va))
2103 vm_page_flag_set(m, PG_REFERENCED);
2104 return pmap_remove_entry(pmap, m, va, info);
2106 return pmap_unuse_pt(pmap, va, NULL, info);
2115 * Remove a single page from a process address space.
2117 * This function may not be called from an interrupt if the pmap is
2122 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
2126 pte = pmap_pte(pmap, va);
2129 if ((*pte & PG_V) == 0)
2131 pmap_remove_pte(pmap, pte, va, info);
2137 * Remove the given range of addresses from the specified map.
2139 * It is assumed that the start and end are properly
2140 * rounded to the page size.
2142 * This function may not be called from an interrupt if the pmap is
2146 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2148 vm_offset_t va_next;
2149 pml4_entry_t *pml4e;
2151 pd_entry_t ptpaddr, *pde;
2153 struct pmap_inval_info info;
2158 lwkt_gettoken(&vm_token);
2159 if (pmap->pm_stats.resident_count == 0) {
2160 lwkt_reltoken(&vm_token);
2164 pmap_inval_init(&info);
2167 * special handling of removing one page. a very
2168 * common operation and easy to short circuit some
2171 if (sva + PAGE_SIZE == eva) {
2172 pde = pmap_pde(pmap, sva);
2173 if (pde && (*pde & PG_PS) == 0) {
2174 pmap_remove_page(pmap, sva, &info);
2175 pmap_inval_done(&info);
2176 lwkt_reltoken(&vm_token);
2181 for (; sva < eva; sva = va_next) {
2182 pml4e = pmap_pml4e(pmap, sva);
2183 if ((*pml4e & PG_V) == 0) {
2184 va_next = (sva + NBPML4) & ~PML4MASK;
2190 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2191 if ((*pdpe & PG_V) == 0) {
2192 va_next = (sva + NBPDP) & ~PDPMASK;
2199 * Calculate index for next page table.
2201 va_next = (sva + NBPDR) & ~PDRMASK;
2205 pde = pmap_pdpe_to_pde(pdpe, sva);
2209 * Weed out invalid mappings.
2215 * Check for large page.
2217 if ((ptpaddr & PG_PS) != 0) {
2218 /* JG FreeBSD has more complex treatment here */
2219 pmap_inval_interlock(&info, pmap, -1);
2221 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2222 pmap_inval_deinterlock(&info, pmap);
2227 * Limit our scan to either the end of the va represented
2228 * by the current page table page, or to the end of the
2229 * range being removed.
2235 * NOTE: pmap_remove_pte() can block.
2237 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2241 if (pmap_remove_pte(pmap, pte, sva, &info))
2245 pmap_inval_done(&info);
2246 lwkt_reltoken(&vm_token);
2252 * Removes this physical page from all physical maps in which it resides.
2253 * Reflects back modify bits to the pager.
2255 * This routine may not be called from an interrupt.
2260 pmap_remove_all(vm_page_t m)
2262 struct pmap_inval_info info;
2263 pt_entry_t *pte, tpte;
2266 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2269 lwkt_gettoken(&vm_token);
2270 pmap_inval_init(&info);
2272 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2273 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2274 --pv->pv_pmap->pm_stats.resident_count;
2276 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2277 pmap_inval_interlock(&info, pv->pv_pmap, pv->pv_va);
2278 tpte = pte_load_clear(pte);
2280 pv->pv_pmap->pm_stats.wired_count--;
2281 pmap_inval_deinterlock(&info, pv->pv_pmap);
2283 vm_page_flag_set(m, PG_REFERENCED);
2286 * Update the vm_page_t clean and reference bits.
2289 #if defined(PMAP_DIAGNOSTIC)
2290 if (pmap_nw_modified(tpte)) {
2292 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2296 if (pmap_track_modified(pv->pv_va))
2299 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2300 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2301 ++pv->pv_pmap->pm_generation;
2302 m->md.pv_list_count--;
2303 KKASSERT(m->md.pv_list_count >= 0);
2304 if (TAILQ_EMPTY(&m->md.pv_list))
2305 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2306 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2310 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2311 pmap_inval_done(&info);
2312 lwkt_reltoken(&vm_token);
2318 * Set the physical protection on the specified range of this map
2321 * This function may not be called from an interrupt if the map is
2322 * not the kernel_pmap.
2325 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2327 vm_offset_t va_next;
2328 pml4_entry_t *pml4e;
2330 pd_entry_t ptpaddr, *pde;
2332 pmap_inval_info info;
2334 /* JG review for NX */
2339 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2340 pmap_remove(pmap, sva, eva);
2344 if (prot & VM_PROT_WRITE)
2347 lwkt_gettoken(&vm_token);
2348 pmap_inval_init(&info);
2350 for (; sva < eva; sva = va_next) {
2352 pml4e = pmap_pml4e(pmap, sva);
2353 if ((*pml4e & PG_V) == 0) {
2354 va_next = (sva + NBPML4) & ~PML4MASK;
2360 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2361 if ((*pdpe & PG_V) == 0) {
2362 va_next = (sva + NBPDP) & ~PDPMASK;
2368 va_next = (sva + NBPDR) & ~PDRMASK;
2372 pde = pmap_pdpe_to_pde(pdpe, sva);
2376 * Check for large page.
2378 if ((ptpaddr & PG_PS) != 0) {
2379 pmap_inval_interlock(&info, pmap, -1);
2380 *pde &= ~(PG_M|PG_RW);
2381 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2382 pmap_inval_deinterlock(&info, pmap);
2387 * Weed out invalid mappings. Note: we assume that the page
2388 * directory table is always allocated, and in kernel virtual.
2396 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2403 * XXX non-optimal. Note also that there can be
2404 * no pmap_inval_flush() calls until after we modify
2405 * ptbase[sindex] (or otherwise we have to do another
2406 * pmap_inval_add() call).
2408 pmap_inval_interlock(&info, pmap, sva);
2412 if ((pbits & PG_V) == 0) {
2413 pmap_inval_deinterlock(&info, pmap);
2416 if (pbits & PG_MANAGED) {
2419 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2420 vm_page_flag_set(m, PG_REFERENCED);
2424 if (pmap_track_modified(sva)) {
2426 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2433 if (pbits != cbits &&
2434 !atomic_cmpset_long(pte, pbits, cbits)) {
2437 pmap_inval_deinterlock(&info, pmap);
2440 pmap_inval_done(&info);
2441 lwkt_reltoken(&vm_token);
2445 * Insert the given physical page (p) at
2446 * the specified virtual address (v) in the
2447 * target physical map with the protection requested.
2449 * If specified, the page will be wired down, meaning
2450 * that the related pte can not be reclaimed.
2452 * NB: This is the only routine which MAY NOT lazy-evaluate
2453 * or lose information. That is, this routine must actually
2454 * insert this page into the given map NOW.
2457 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2464 pt_entry_t origpte, newpte;
2466 pmap_inval_info info;
2471 va = trunc_page(va);
2472 #ifdef PMAP_DIAGNOSTIC
2474 panic("pmap_enter: toobig");
2475 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2476 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va);
2478 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2479 kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n");
2481 db_print_backtrace();
2484 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2485 kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n");
2487 db_print_backtrace();
2491 lwkt_gettoken(&vm_token);
2494 * In the case that a page table page is not
2495 * resident, we are creating it here.
2497 if (va < VM_MAX_USER_ADDRESS)
2498 mpte = pmap_allocpte(pmap, va);
2502 pmap_inval_init(&info);
2503 pde = pmap_pde(pmap, va);
2504 if (pde != NULL && (*pde & PG_V) != 0) {
2505 if ((*pde & PG_PS) != 0)
2506 panic("pmap_enter: attempted pmap_enter on 2MB page");
2507 pte = pmap_pde_to_pte(pde, va);
2509 panic("pmap_enter: invalid page directory va=%#lx", va);
2511 KKASSERT(pte != NULL);
2512 pa = VM_PAGE_TO_PHYS(m);
2514 opa = origpte & PG_FRAME;
2517 * Mapping has not changed, must be protection or wiring change.
2519 if (origpte && (opa == pa)) {
2521 * Wiring change, just update stats. We don't worry about
2522 * wiring PT pages as they remain resident as long as there
2523 * are valid mappings in them. Hence, if a user page is wired,
2524 * the PT page will be also.
2526 if (wired && ((origpte & PG_W) == 0))
2527 pmap->pm_stats.wired_count++;
2528 else if (!wired && (origpte & PG_W))
2529 pmap->pm_stats.wired_count--;
2531 #if defined(PMAP_DIAGNOSTIC)
2532 if (pmap_nw_modified(origpte)) {
2534 "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2540 * Remove the extra pte reference. Note that we cannot
2541 * optimize the RO->RW case because we have adjusted the
2542 * wiring count above and may need to adjust the wiring
2549 * We might be turning off write access to the page,
2550 * so we go ahead and sense modify status.
2552 if (origpte & PG_MANAGED) {
2553 if ((origpte & PG_M) && pmap_track_modified(va)) {
2555 om = PHYS_TO_VM_PAGE(opa);
2559 KKASSERT(m->flags & PG_MAPPED);
2564 * Mapping has changed, invalidate old range and fall through to
2565 * handle validating new mapping.
2569 err = pmap_remove_pte(pmap, pte, va, &info);
2571 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2573 opa = origpte & PG_FRAME;
2575 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
2581 * Enter on the PV list if part of our managed memory. Note that we
2582 * raise IPL while manipulating pv_table since pmap_enter can be
2583 * called at interrupt time.
2585 if (pmap_initialized &&
2586 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2587 pmap_insert_entry(pmap, va, mpte, m);
2589 vm_page_flag_set(m, PG_MAPPED);
2593 * Increment counters
2595 ++pmap->pm_stats.resident_count;
2597 pmap->pm_stats.wired_count++;
2601 * Now validate mapping with desired protection/wiring.
2603 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V);
2607 if (va < VM_MAX_USER_ADDRESS)
2609 if (pmap == &kernel_pmap)
2613 * if the mapping or permission bits are different, we need
2614 * to update the pte.
2616 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2617 pmap_inval_interlock(&info, pmap, va);
2618 *pte = newpte | PG_A;
2619 pmap_inval_deinterlock(&info, pmap);
2621 vm_page_flag_set(m, PG_WRITEABLE);
2623 KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
2624 pmap_inval_done(&info);
2625 lwkt_reltoken(&vm_token);
2629 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2630 * This code also assumes that the pmap has no pre-existing entry for this
2633 * This code currently may only be used on user pmaps, not kernel_pmap.
2636 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2641 vm_pindex_t ptepindex;
2643 pmap_inval_info info;
2645 lwkt_gettoken(&vm_token);
2646 pmap_inval_init(&info);
2648 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2649 kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n");
2651 db_print_backtrace();
2654 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2655 kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n");
2657 db_print_backtrace();
2661 KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */
2664 * Calculate the page table page (mpte), allocating it if necessary.
2666 * A held page table page (mpte), or NULL, is passed onto the
2667 * section following.
2669 if (va < VM_MAX_USER_ADDRESS) {
2671 * Calculate pagetable page index
2673 ptepindex = pmap_pde_pindex(va);
2677 * Get the page directory entry
2679 ptepa = pmap_pde(pmap, va);
2682 * If the page table page is mapped, we just increment
2683 * the hold count, and activate it.
2685 if (ptepa && (*ptepa & PG_V) != 0) {
2687 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2688 // if (pmap->pm_ptphint &&
2689 // (pmap->pm_ptphint->pindex == ptepindex)) {
2690 // mpte = pmap->pm_ptphint;
2692 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2693 pmap->pm_ptphint = mpte;
2698 mpte = _pmap_allocpte(pmap, ptepindex);
2700 } while (mpte == NULL);
2703 /* this code path is not yet used */
2707 * With a valid (and held) page directory page, we can just use
2708 * vtopte() to get to the pte. If the pte is already present
2709 * we do not disturb it.
2714 pmap_unwire_pte_hold(pmap, va, mpte, &info);
2715 pa = VM_PAGE_TO_PHYS(m);
2716 KKASSERT(((*pte ^ pa) & PG_FRAME) == 0);
2717 pmap_inval_done(&info);
2718 lwkt_reltoken(&vm_token);
2723 * Enter on the PV list if part of our managed memory
2725 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2726 pmap_insert_entry(pmap, va, mpte, m);
2727 vm_page_flag_set(m, PG_MAPPED);
2731 * Increment counters
2733 ++pmap->pm_stats.resident_count;
2735 pa = VM_PAGE_TO_PHYS(m);
2738 * Now validate mapping with RO protection
2740 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2741 *pte = pa | PG_V | PG_U;
2743 *pte = pa | PG_V | PG_U | PG_MANAGED;
2744 /* pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */
2745 pmap_inval_done(&info);
2746 lwkt_reltoken(&vm_token);
2750 * Make a temporary mapping for a physical address. This is only intended
2751 * to be used for panic dumps.
2753 /* JG Needed on x86_64? */
2755 pmap_kenter_temporary(vm_paddr_t pa, int i)
2757 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2758 return ((void *)crashdumpmap);
2761 #define MAX_INIT_PT (96)
2764 * This routine preloads the ptes for a given object into the specified pmap.
2765 * This eliminates the blast of soft faults on process startup and
2766 * immediately after an mmap.
2768 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2771 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2772 vm_object_t object, vm_pindex_t pindex,
2773 vm_size_t size, int limit)
2775 struct rb_vm_page_scan_info info;
2780 * We can't preinit if read access isn't set or there is no pmap
2783 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2787 * We can't preinit if the pmap is not the current pmap
2789 lp = curthread->td_lwp;
2790 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2793 psize = x86_64_btop(size);
2795 if ((object->type != OBJT_VNODE) ||
2796 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2797 (object->resident_page_count > MAX_INIT_PT))) {
2801 if (psize + pindex > object->size) {
2802 if (object->size < pindex)
2804 psize = object->size - pindex;
2811 * Use a red-black scan to traverse the requested range and load
2812 * any valid pages found into the pmap.
2814 * We cannot safely scan the object's memq unless we are in a
2815 * critical section since interrupts can remove pages from objects.
2817 info.start_pindex = pindex;
2818 info.end_pindex = pindex + psize - 1;
2825 lwkt_gettoken(&vm_token);
2826 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2827 pmap_object_init_pt_callback, &info);
2828 lwkt_reltoken(&vm_token);
2834 pmap_object_init_pt_callback(vm_page_t p, void *data)
2836 struct rb_vm_page_scan_info *info = data;
2837 vm_pindex_t rel_index;
2839 * don't allow an madvise to blow away our really
2840 * free pages allocating pv entries.
2842 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2843 vmstats.v_free_count < vmstats.v_free_reserved) {
2846 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2847 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2848 if ((p->queue - p->pc) == PQ_CACHE)
2849 vm_page_deactivate(p);
2851 rel_index = p->pindex - info->start_pindex;
2852 pmap_enter_quick(info->pmap,
2853 info->addr + x86_64_ptob(rel_index), p);
2860 * Return TRUE if the pmap is in shape to trivially
2861 * pre-fault the specified address.
2863 * Returns FALSE if it would be non-trivial or if a
2864 * pte is already loaded into the slot.
2867 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2873 lwkt_gettoken(&vm_token);
2874 pde = pmap_pde(pmap, addr);
2875 if (pde == NULL || *pde == 0) {
2879 ret = (*pte) ? 0 : 1;
2881 lwkt_reltoken(&vm_token);
2886 * Routine: pmap_change_wiring
2887 * Function: Change the wiring attribute for a map/virtual-address
2889 * In/out conditions:
2890 * The mapping must already exist in the pmap.
2893 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2900 lwkt_gettoken(&vm_token);
2901 pte = pmap_pte(pmap, va);
2903 if (wired && !pmap_pte_w(pte))
2904 pmap->pm_stats.wired_count++;
2905 else if (!wired && pmap_pte_w(pte))
2906 pmap->pm_stats.wired_count--;
2909 * Wiring is not a hardware characteristic so there is no need to
2910 * invalidate TLB. However, in an SMP environment we must use
2911 * a locked bus cycle to update the pte (if we are not using
2912 * the pmap_inval_*() API that is)... it's ok to do this for simple
2917 atomic_set_long(pte, PG_W);
2919 atomic_clear_long(pte, PG_W);
2922 atomic_set_long_nonlocked(pte, PG_W);
2924 atomic_clear_long_nonlocked(pte, PG_W);
2926 lwkt_reltoken(&vm_token);
2932 * Copy the range specified by src_addr/len
2933 * from the source map to the range dst_addr/len
2934 * in the destination map.
2936 * This routine is only advisory and need not do anything.
2939 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2940 vm_size_t len, vm_offset_t src_addr)
2944 pmap_inval_info info;
2946 vm_offset_t end_addr = src_addr + len;
2948 pd_entry_t src_frame, dst_frame;
2951 if (dst_addr != src_addr)
2954 src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2955 if (src_frame != (PTDpde & PG_FRAME)) {
2959 dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2960 if (dst_frame != (APTDpde & PG_FRAME)) {
2961 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2962 /* The page directory is not shared between CPUs */
2966 pmap_inval_init(&info);
2967 pmap_inval_add(&info, dst_pmap, -1);
2968 pmap_inval_add(&info, src_pmap, -1);
2971 * critical section protection is required to maintain the page/object
2972 * association, interrupts can free pages and remove them from
2976 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2977 pt_entry_t *src_pte, *dst_pte;
2978 vm_page_t dstmpte, srcmpte;
2979 vm_offset_t srcptepaddr;
2980 vm_pindex_t ptepindex;
2982 if (addr >= UPT_MIN_ADDRESS)
2983 panic("pmap_copy: invalid to pmap_copy page tables\n");
2986 * Don't let optional prefaulting of pages make us go
2987 * way below the low water mark of free pages or way
2988 * above high water mark of used pv entries.
2990 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2991 pv_entry_count > pv_entry_high_water)
2994 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2995 ptepindex = addr >> PDRSHIFT;
2998 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
3000 if (srcptepaddr == 0)
3003 if (srcptepaddr & PG_PS) {
3005 if (dst_pmap->pm_pdir[ptepindex] == 0) {
3006 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
3007 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3013 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
3014 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
3015 (srcmpte->flags & PG_BUSY)) {
3019 if (pdnxt > end_addr)
3022 src_pte = vtopte(addr);
3024 dst_pte = avtopte(addr);
3026 while (addr < pdnxt) {
3031 * we only virtual copy managed pages
3033 if ((ptetemp & PG_MANAGED) != 0) {
3035 * We have to check after allocpte for the
3036 * pte still being around... allocpte can
3039 * pmap_allocpte() can block. If we lose
3040 * our page directory mappings we stop.
3042 dstmpte = pmap_allocpte(dst_pmap, addr);
3045 if (src_frame != (PTDpde & PG_FRAME) ||
3046 dst_frame != (APTDpde & PG_FRAME)
3048 kprintf("WARNING: pmap_copy: detected and corrected race\n");
3049 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3051 } else if ((*dst_pte == 0) &&
3052 (ptetemp = *src_pte) != 0 &&
3053 (ptetemp & PG_MANAGED)) {
3055 * Clear the modified and
3056 * accessed (referenced) bits
3059 m = PHYS_TO_VM_PAGE(ptetemp);
3060 *dst_pte = ptetemp & ~(PG_M | PG_A);
3061 ++dst_pmap->pm_stats.resident_count;
3062 pmap_insert_entry(dst_pmap, addr,
3064 KKASSERT(m->flags & PG_MAPPED);
3066 kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n");
3067 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3071 if (dstmpte->hold_count >= srcmpte->hold_count)
3081 pmap_inval_done(&info);
3088 * Zero the specified physical page.
3090 * This function may be called from an interrupt and no locking is
3094 pmap_zero_page(vm_paddr_t phys)
3096 vm_offset_t va = PHYS_TO_DMAP(phys);
3098 pagezero((void *)va);
3102 * pmap_page_assertzero:
3104 * Assert that a page is empty, panic if it isn't.
3107 pmap_page_assertzero(vm_paddr_t phys)
3109 vm_offset_t virt = PHYS_TO_DMAP(phys);
3112 for (i = 0; i < PAGE_SIZE; i += sizeof(long)) {
3113 if (*(long *)((char *)virt + i) != 0) {
3114 panic("pmap_page_assertzero() @ %p not zero!\n", (void *)virt);
3122 * Zero part of a physical page by mapping it into memory and clearing
3123 * its contents with bzero.
3125 * off and size may not cover an area beyond a single hardware page.
3128 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3130 vm_offset_t virt = PHYS_TO_DMAP(phys);
3132 bzero((char *)virt + off, size);
3138 * Copy the physical page from the source PA to the target PA.
3139 * This function may be called from an interrupt. No locking
3143 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3145 vm_offset_t src_virt, dst_virt;
3147 src_virt = PHYS_TO_DMAP(src);
3148 dst_virt = PHYS_TO_DMAP(dst);
3149 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
3153 * pmap_copy_page_frag:
3155 * Copy the physical page from the source PA to the target PA.
3156 * This function may be called from an interrupt. No locking
3160 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3162 vm_offset_t src_virt, dst_virt;
3164 src_virt = PHYS_TO_DMAP(src);
3165 dst_virt = PHYS_TO_DMAP(dst);
3167 bcopy((char *)src_virt + (src & PAGE_MASK),
3168 (char *)dst_virt + (dst & PAGE_MASK),
3173 * Returns true if the pmap's pv is one of the first
3174 * 16 pvs linked to from this page. This count may
3175 * be changed upwards or downwards in the future; it
3176 * is only necessary that true be returned for a small
3177 * subset of pmaps for proper page aging.
3180 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3185 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3189 lwkt_gettoken(&vm_token);
3191 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3192 if (pv->pv_pmap == pmap) {
3193 lwkt_reltoken(&vm_token);
3201 lwkt_reltoken(&vm_token);
3207 * Remove all pages from specified address space
3208 * this aids process exit speeds. Also, this code
3209 * is special cased for current process only, but
3210 * can have the more generic (and slightly slower)
3211 * mode enabled. This is much faster than pmap_remove
3212 * in the case of running down an entire address space.
3215 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3218 pt_entry_t *pte, tpte;
3221 pmap_inval_info info;
3223 int save_generation;
3225 lp = curthread->td_lwp;
3226 if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
3231 lwkt_gettoken(&vm_token);
3232 pmap_inval_init(&info);
3233 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3234 if (pv->pv_va >= eva || pv->pv_va < sva) {
3235 npv = TAILQ_NEXT(pv, pv_plist);
3239 KKASSERT(pmap == pv->pv_pmap);
3242 pte = vtopte(pv->pv_va);
3244 pte = pmap_pte_quick(pmap, pv->pv_va);
3245 pmap_inval_interlock(&info, pmap, pv->pv_va);
3248 * We cannot remove wired pages from a process' mapping
3252 pmap_inval_deinterlock(&info, pmap);
3253 npv = TAILQ_NEXT(pv, pv_plist);
3256 tpte = pte_load_clear(pte);
3258 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3260 KASSERT(m < &vm_page_array[vm_page_array_size],
3261 ("pmap_remove_pages: bad tpte %lx", tpte));
3263 KKASSERT(pmap->pm_stats.resident_count > 0);
3264 --pmap->pm_stats.resident_count;
3265 pmap_inval_deinterlock(&info, pmap);
3268 * Update the vm_page_t clean and reference bits.
3274 npv = TAILQ_NEXT(pv, pv_plist);
3275 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
3276 save_generation = ++pmap->pm_generation;
3278 m->md.pv_list_count--;
3279 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3280 if (TAILQ_EMPTY(&m->md.pv_list))
3281 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3283 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
3287 * Restart the scan if we blocked during the unuse or free
3288 * calls and other removals were made.
3290 if (save_generation != pmap->pm_generation) {
3291 kprintf("Warning: pmap_remove_pages race-A avoided\n");
3292 npv = TAILQ_FIRST(&pmap->pm_pvlist);
3295 pmap_inval_done(&info);
3296 lwkt_reltoken(&vm_token);
3300 * pmap_testbit tests bits in pte's
3301 * note that the testbit/clearbit routines are inline,
3302 * and a lot of things compile-time evaluate.
3306 pmap_testbit(vm_page_t m, int bit)
3311 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3314 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3319 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3321 * if the bit being tested is the modified bit, then
3322 * mark clean_map and ptes as never
3325 if (bit & (PG_A|PG_M)) {
3326 if (!pmap_track_modified(pv->pv_va))
3330 #if defined(PMAP_DIAGNOSTIC)
3331 if (pv->pv_pmap == NULL) {
3332 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3336 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3347 * this routine is used to modify bits in ptes
3351 pmap_clearbit(vm_page_t m, int bit)
3353 struct pmap_inval_info info;
3358 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3361 pmap_inval_init(&info);
3364 * Loop over all current mappings setting/clearing as appropos If
3365 * setting RO do we need to clear the VAC?
3367 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3369 * don't write protect pager mappings
3372 if (!pmap_track_modified(pv->pv_va))
3376 #if defined(PMAP_DIAGNOSTIC)
3377 if (pv->pv_pmap == NULL) {
3378 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3384 * Careful here. We can use a locked bus instruction to
3385 * clear PG_A or PG_M safely but we need to synchronize
3386 * with the target cpus when we mess with PG_RW.
3388 * We do not have to force synchronization when clearing
3389 * PG_M even for PTEs generated via virtual memory maps,
3390 * because the virtual kernel will invalidate the pmap
3391 * entry when/if it needs to resynchronize the Modify bit.
3394 pmap_inval_interlock(&info, pv->pv_pmap, pv->pv_va);
3395 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3402 atomic_clear_long(pte, PG_M|PG_RW);
3405 * The cpu may be trying to set PG_M
3406 * simultaniously with our clearing
3409 if (!atomic_cmpset_long(pte, pbits,
3413 } else if (bit == PG_M) {
3415 * We could also clear PG_RW here to force
3416 * a fault on write to redetect PG_M for
3417 * virtual kernels, but it isn't necessary
3418 * since virtual kernels invalidate the pte
3419 * when they clear the VPTE_M bit in their
3420 * virtual page tables.
3422 atomic_clear_long(pte, PG_M);
3424 atomic_clear_long(pte, bit);
3428 pmap_inval_deinterlock(&info, pv->pv_pmap);
3430 pmap_inval_done(&info);
3434 * pmap_page_protect:
3436 * Lower the permission for all mappings to a given page.
3439 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3441 /* JG NX support? */
3442 if ((prot & VM_PROT_WRITE) == 0) {
3443 lwkt_gettoken(&vm_token);
3444 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3445 pmap_clearbit(m, PG_RW);
3446 vm_page_flag_clear(m, PG_WRITEABLE);
3450 lwkt_reltoken(&vm_token);
3455 pmap_phys_address(vm_pindex_t ppn)
3457 return (x86_64_ptob(ppn));
3461 * pmap_ts_referenced:
3463 * Return a count of reference bits for a page, clearing those bits.
3464 * It is not necessary for every reference bit to be cleared, but it
3465 * is necessary that 0 only be returned when there are truly no
3466 * reference bits set.
3468 * XXX: The exact number of bits to check and clear is a matter that
3469 * should be tested and standardized at some point in the future for
3470 * optimal aging of shared pages.
3473 pmap_ts_referenced(vm_page_t m)
3475 pv_entry_t pv, pvf, pvn;
3479 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3483 lwkt_gettoken(&vm_token);
3485 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3490 pvn = TAILQ_NEXT(pv, pv_list);
3493 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3494 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3497 if (!pmap_track_modified(pv->pv_va))
3500 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3502 if (pte && (*pte & PG_A)) {
3504 atomic_clear_long(pte, PG_A);
3506 atomic_clear_long_nonlocked(pte, PG_A);
3513 } while ((pv = pvn) != NULL && pv != pvf);
3515 lwkt_reltoken(&vm_token);
3524 * Return whether or not the specified physical page was modified
3525 * in any physical maps.
3528 pmap_is_modified(vm_page_t m)
3532 lwkt_gettoken(&vm_token);
3533 res = pmap_testbit(m, PG_M);
3534 lwkt_reltoken(&vm_token);
3539 * Clear the modify bits on the specified physical page.
3542 pmap_clear_modify(vm_page_t m)
3544 lwkt_gettoken(&vm_token);
3545 pmap_clearbit(m, PG_M);
3546 lwkt_reltoken(&vm_token);
3550 * pmap_clear_reference:
3552 * Clear the reference bit on the specified physical page.
3555 pmap_clear_reference(vm_page_t m)
3557 lwkt_gettoken(&vm_token);
3558 pmap_clearbit(m, PG_A);
3559 lwkt_reltoken(&vm_token);
3563 * Miscellaneous support routines follow
3568 i386_protection_init(void)
3572 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
3573 kp = protection_codes;
3574 for (prot = 0; prot < 8; prot++) {
3576 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3578 * Read access is also 0. There isn't any execute bit,
3579 * so just make it readable.
3581 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3582 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3583 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3586 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3587 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3588 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3589 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3597 * Map a set of physical memory pages into the kernel virtual
3598 * address space. Return a pointer to where it is mapped. This
3599 * routine is intended to be used for mapping device memory,
3602 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3606 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3608 vm_offset_t va, tmpva, offset;
3611 offset = pa & PAGE_MASK;
3612 size = roundup(offset + size, PAGE_SIZE);
3614 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3616 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3618 pa = pa & ~PAGE_MASK;
3619 for (tmpva = va; size > 0;) {
3620 pte = vtopte(tmpva);
3621 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3629 return ((void *)(va + offset));
3633 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
3635 vm_offset_t va, tmpva, offset;
3638 offset = pa & PAGE_MASK;
3639 size = roundup(offset + size, PAGE_SIZE);
3641 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3643 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3645 pa = pa & ~PAGE_MASK;
3646 for (tmpva = va; size > 0;) {
3647 pte = vtopte(tmpva);
3648 *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */
3656 return ((void *)(va + offset));
3660 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3662 vm_offset_t base, offset;
3664 base = va & ~PAGE_MASK;
3665 offset = va & PAGE_MASK;
3666 size = roundup(offset + size, PAGE_SIZE);
3667 pmap_qremove(va, size >> PAGE_SHIFT);
3668 kmem_free(&kernel_map, base, size);
3672 * perform the pmap work for mincore
3675 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3677 pt_entry_t *ptep, pte;
3681 lwkt_gettoken(&vm_token);
3682 ptep = pmap_pte(pmap, addr);
3684 if (ptep && (pte = *ptep) != 0) {
3687 val = MINCORE_INCORE;
3688 if ((pte & PG_MANAGED) == 0)
3691 pa = pte & PG_FRAME;
3693 m = PHYS_TO_VM_PAGE(pa);
3699 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3701 * Modified by someone
3703 else if (m->dirty || pmap_is_modified(m))
3704 val |= MINCORE_MODIFIED_OTHER;
3709 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3712 * Referenced by someone
3714 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3715 val |= MINCORE_REFERENCED_OTHER;
3716 vm_page_flag_set(m, PG_REFERENCED);
3720 lwkt_reltoken(&vm_token);
3725 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3726 * vmspace will be ref'd and the old one will be deref'd.
3728 * The vmspace for all lwps associated with the process will be adjusted
3729 * and cr3 will be reloaded if any lwp is the current lwp.
3732 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3734 struct vmspace *oldvm;
3738 oldvm = p->p_vmspace;
3739 if (oldvm != newvm) {
3740 p->p_vmspace = newvm;
3741 KKASSERT(p->p_nthreads == 1);
3742 lp = RB_ROOT(&p->p_lwp_tree);
3743 pmap_setlwpvm(lp, newvm);
3745 sysref_get(&newvm->vm_sysref);
3746 sysref_put(&oldvm->vm_sysref);
3753 * Set the vmspace for a LWP. The vmspace is almost universally set the
3754 * same as the process vmspace, but virtual kernels need to swap out contexts
3755 * on a per-lwp basis.
3758 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3760 struct vmspace *oldvm;
3764 oldvm = lp->lwp_vmspace;
3766 if (oldvm != newvm) {
3767 lp->lwp_vmspace = newvm;
3768 if (curthread->td_lwp == lp) {
3769 pmap = vmspace_pmap(newvm);
3771 atomic_set_int(&pmap->pm_active, mycpu->gd_cpumask);
3772 if (pmap->pm_active & CPUMASK_LOCK)
3773 pmap_interlock_wait(newvm);
3775 pmap->pm_active |= 1;
3777 #if defined(SWTCH_OPTIM_STATS)
3780 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
3781 curthread->td_pcb->pcb_cr3 |= PG_RW | PG_U | PG_V;
3782 load_cr3(curthread->td_pcb->pcb_cr3);
3783 pmap = vmspace_pmap(oldvm);
3785 atomic_clear_int(&pmap->pm_active, mycpu->gd_cpumask);
3787 pmap->pm_active &= ~1;
3797 * Called when switching to a locked pmap
3800 pmap_interlock_wait(struct vmspace *vm)
3802 struct pmap *pmap = &vm->vm_pmap;
3804 if (pmap->pm_active & CPUMASK_LOCK) {
3805 while (pmap->pm_active & CPUMASK_LOCK) {
3808 lwkt_process_ipiq();
3816 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3819 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3823 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);