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.
56 #include "opt_msgbuf.h"
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
62 #include <sys/msgbuf.h>
63 #include <sys/vmmeter.h>
65 #include <sys/vmspace.h>
68 #include <vm/vm_param.h>
69 #include <sys/sysctl.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_zone.h>
81 #include <sys/thread2.h>
82 #include <sys/sysref2.h>
83 #include <sys/spinlock2.h>
85 #include <machine/cputypes.h>
86 #include <machine/md_var.h>
87 #include <machine/specialreg.h>
88 #include <machine/smp.h>
89 #include <machine/globaldata.h>
90 #include <machine/pmap.h>
91 #include <machine/pmap_inval.h>
99 #define PMAP_KEEP_PDIRS
100 #ifndef PMAP_SHPGPERPROC
101 #define PMAP_SHPGPERPROC 1000
104 #if defined(DIAGNOSTIC)
105 #define PMAP_DIAGNOSTIC
110 #if !defined(PMAP_DIAGNOSTIC)
111 #define PMAP_INLINE __inline
117 * Get PDEs and PTEs for user/kernel address space
119 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
120 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
122 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & VPTE_V) != 0)
123 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & VPTE_WIRED) != 0)
124 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & VPTE_M) != 0)
125 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & VPTE_A) != 0)
126 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & VPTE_V) != 0)
129 * Given a map and a machine independent protection code,
130 * convert to a vax protection code.
132 #define pte_prot(m, p) \
133 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
134 static int protection_codes[8];
136 struct pmap kernel_pmap;
137 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
139 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
141 static vm_object_t kptobj;
145 static uint64_t KPDphys; /* phys addr of kernel level 2 */
146 uint64_t KPDPphys; /* phys addr of kernel level 3 */
147 uint64_t KPML4phys; /* phys addr of kernel level 4 */
151 * Data for the pv entry allocation mechanism
153 static vm_zone_t pvzone;
154 static struct vm_zone pvzone_store;
155 static struct vm_object pvzone_obj;
156 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
157 static int pmap_pagedaemon_waken = 0;
158 static struct pv_entry *pvinit;
161 * All those kernel PT submaps that BSD is so fond of
163 pt_entry_t *CMAP1 = 0, *ptmmap;
165 static pt_entry_t *msgbufmap;
169 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
170 static pv_entry_t get_pv_entry (void);
171 static void i386_protection_init (void);
172 static __inline void pmap_clearbit (vm_page_t m, int bit);
174 static void pmap_remove_all (vm_page_t m);
175 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
177 static void pmap_remove_page (struct pmap *pmap, vm_offset_t va);
178 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
180 static boolean_t pmap_testbit (vm_page_t m, int bit);
181 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
182 vm_page_t mpte, vm_page_t m);
184 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
186 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
187 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
189 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
191 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
192 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t);
197 * Super fast pmap_pte routine best used when scanning the pv lists.
198 * This eliminates many course-grained invltlb calls. Note that many of
199 * the pv list scans are across different pmaps and it is very wasteful
200 * to do an entire invltlb when checking a single mapping.
202 * Should only be called while in a critical section.
205 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
208 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
210 return pmap_pte(pmap, va);
214 /* Return a non-clipped PD index for a given VA */
215 static __inline vm_pindex_t
216 pmap_pde_pindex(vm_offset_t va)
218 return va >> PDRSHIFT;
221 /* Return various clipped indexes for a given VA */
222 static __inline vm_pindex_t
223 pmap_pte_index(vm_offset_t va)
226 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
229 static __inline vm_pindex_t
230 pmap_pde_index(vm_offset_t va)
233 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
236 static __inline vm_pindex_t
237 pmap_pdpe_index(vm_offset_t va)
240 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
243 static __inline vm_pindex_t
244 pmap_pml4e_index(vm_offset_t va)
247 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
250 /* Return a pointer to the PML4 slot that corresponds to a VA */
251 static __inline pml4_entry_t *
252 pmap_pml4e(pmap_t pmap, vm_offset_t va)
255 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
258 /* Return a pointer to the PDP slot that corresponds to a VA */
259 static __inline pdp_entry_t *
260 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
264 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & VPTE_FRAME);
265 return (&pdpe[pmap_pdpe_index(va)]);
268 /* Return a pointer to the PDP slot that corresponds to a VA */
269 static __inline pdp_entry_t *
270 pmap_pdpe(pmap_t pmap, vm_offset_t va)
274 pml4e = pmap_pml4e(pmap, va);
275 if ((*pml4e & VPTE_V) == 0)
277 return (pmap_pml4e_to_pdpe(pml4e, va));
280 /* Return a pointer to the PD slot that corresponds to a VA */
281 static __inline pd_entry_t *
282 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
286 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & VPTE_FRAME);
287 return (&pde[pmap_pde_index(va)]);
290 /* Return a pointer to the PD slot that corresponds to a VA */
291 static __inline pd_entry_t *
292 pmap_pde(pmap_t pmap, vm_offset_t va)
296 pdpe = pmap_pdpe(pmap, va);
297 if (pdpe == NULL || (*pdpe & VPTE_V) == 0)
299 return (pmap_pdpe_to_pde(pdpe, va));
302 /* Return a pointer to the PT slot that corresponds to a VA */
303 static __inline pt_entry_t *
304 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
308 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & VPTE_FRAME);
309 return (&pte[pmap_pte_index(va)]);
312 /* Return a pointer to the PT slot that corresponds to a VA */
313 static __inline pt_entry_t *
314 pmap_pte(pmap_t pmap, vm_offset_t va)
318 pde = pmap_pde(pmap, va);
319 if (pde == NULL || (*pde & VPTE_V) == 0)
321 if ((*pde & VPTE_PS) != 0) /* compat with i386 pmap_pte() */
322 return ((pt_entry_t *)pde);
323 return (pmap_pde_to_pte(pde, va));
328 PMAP_INLINE pt_entry_t *
329 vtopte(vm_offset_t va)
331 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT +
332 NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
334 return (PTmap + ((va >> PAGE_SHIFT) & mask));
337 static __inline pd_entry_t *
338 vtopde(vm_offset_t va)
340 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT +
341 NPML4EPGSHIFT)) - 1);
343 return (PDmap + ((va >> PDRSHIFT) & mask));
346 static PMAP_INLINE pt_entry_t *
347 vtopte(vm_offset_t va)
350 x = pmap_pte(&kernel_pmap, va);
355 static __inline pd_entry_t *
356 vtopde(vm_offset_t va)
359 x = pmap_pde(&kernel_pmap, va);
366 allocpages(vm_paddr_t *firstaddr, int n)
372 bzero((void *)ret, n * PAGE_SIZE);
374 *firstaddr += n * PAGE_SIZE;
379 create_pagetables(vm_paddr_t *firstaddr, int64_t ptov_offset)
382 pml4_entry_t *KPML4virt;
383 pdp_entry_t *KPDPvirt;
386 int kpml4i = pmap_pml4e_index(ptov_offset);
387 int kpdpi = pmap_pdpe_index(ptov_offset);
390 * Calculate NKPT - number of kernel page tables. We have to
391 * accomodoate prealloction of the vm_page_array, dump bitmap,
392 * MSGBUF_SIZE, and other stuff. Be generous.
394 * Maxmem is in pages.
396 nkpt = (Maxmem * (sizeof(struct vm_page) * 2) + MSGBUF_SIZE) / NBPDR;
401 KPML4phys = allocpages(firstaddr, 1);
402 KPDPphys = allocpages(firstaddr, NKPML4E);
403 KPDphys = allocpages(firstaddr, NKPDPE);
404 KPTphys = allocpages(firstaddr, nkpt);
406 KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
407 KPDPvirt = (pdp_entry_t *)PHYS_TO_DMAP(KPDPphys);
408 KPDvirt = (pd_entry_t *)PHYS_TO_DMAP(KPDphys);
409 KPTvirt = (pt_entry_t *)PHYS_TO_DMAP(KPTphys);
411 bzero(KPML4virt, 1 * PAGE_SIZE);
412 bzero(KPDPvirt, NKPML4E * PAGE_SIZE);
413 bzero(KPDvirt, NKPDPE * PAGE_SIZE);
414 bzero(KPTvirt, nkpt * PAGE_SIZE);
416 /* Now map the page tables at their location within PTmap */
417 for (i = 0; i < nkpt; i++) {
418 KPDvirt[i] = KPTphys + (i << PAGE_SHIFT);
419 KPDvirt[i] |= VPTE_R | VPTE_W | VPTE_V;
422 /* And connect up the PD to the PDP */
423 for (i = 0; i < NKPDPE; i++) {
424 KPDPvirt[i + kpdpi] = KPDphys + (i << PAGE_SHIFT);
425 KPDPvirt[i + kpdpi] |= VPTE_R | VPTE_W | VPTE_V;
428 /* And recursively map PML4 to itself in order to get PTmap */
429 KPML4virt[PML4PML4I] = KPML4phys;
430 KPML4virt[PML4PML4I] |= VPTE_R | VPTE_W | VPTE_V;
432 /* Connect the KVA slot up to the PML4 */
433 KPML4virt[kpml4i] = KPDPphys;
434 KPML4virt[kpml4i] |= VPTE_R | VPTE_W | VPTE_V;
438 * Bootstrap the system enough to run with virtual memory.
440 * On the i386 this is called after mapping has already been enabled
441 * and just syncs the pmap module with what has already been done.
442 * [We can't call it easily with mapping off since the kernel is not
443 * mapped with PA == VA, hence we would have to relocate every address
444 * from the linked base (virtual) address "KERNBASE" to the actual
445 * (physical) address starting relative to 0]
448 pmap_bootstrap(vm_paddr_t *firstaddr, int64_t ptov_offset)
454 * Create an initial set of page tables to run the kernel in.
456 create_pagetables(firstaddr, ptov_offset);
458 virtual_start = KvaStart + *firstaddr;
459 virtual_end = KvaEnd;
462 * Initialize protection array.
464 i386_protection_init();
467 * The kernel's pmap is statically allocated so we don't have to use
468 * pmap_create, which is unlikely to work correctly at this part of
469 * the boot sequence (XXX and which no longer exists).
471 * The kernel_pmap's pm_pteobj is used only for locking and not
474 kernel_pmap.pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
475 kernel_pmap.pm_count = 1;
476 kernel_pmap.pm_active = (cpumask_t)-1; /* don't allow deactivation */
477 kernel_pmap.pm_pteobj = &kernel_object;
478 TAILQ_INIT(&kernel_pmap.pm_pvlist);
479 TAILQ_INIT(&kernel_pmap.pm_pvlist_free);
480 lwkt_token_init(&kernel_pmap.pm_token, "kpmap_tok");
481 spin_init(&kernel_pmap.pm_spin);
484 * Reserve some special page table entries/VA space for temporary
487 #define SYSMAP(c, p, v, n) \
488 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
491 pte = pmap_pte(&kernel_pmap, va);
494 * CMAP1/CMAP2 are used for zeroing and copying pages.
496 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
502 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
506 * ptvmmap is used for reading arbitrary physical pages via
509 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
512 * msgbufp is used to map the system message buffer.
513 * XXX msgbufmap is not used.
515 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
516 atop(round_page(MSGBUF_SIZE)))
526 * Initialize the pmap module.
527 * Called by vm_init, to initialize any structures that the pmap
528 * system needs to map virtual memory.
529 * pmap_init has been enhanced to support in a fairly consistant
530 * way, discontiguous physical memory.
539 * object for kernel page table pages
541 /* JG I think the number can be arbitrary */
542 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
545 * Allocate memory for random pmap data structures. Includes the
549 for(i = 0; i < vm_page_array_size; i++) {
552 m = &vm_page_array[i];
553 TAILQ_INIT(&m->md.pv_list);
554 m->md.pv_list_count = 0;
558 * init the pv free list
560 initial_pvs = vm_page_array_size;
561 if (initial_pvs < MINPV)
563 pvzone = &pvzone_store;
564 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
565 initial_pvs * sizeof (struct pv_entry));
566 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
570 * Now it is safe to enable pv_table recording.
572 pmap_initialized = TRUE;
576 * Initialize the address space (zone) for the pv_entries. Set a
577 * high water mark so that the system can recover from excessive
578 * numbers of pv entries.
583 int shpgperproc = PMAP_SHPGPERPROC;
585 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
586 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
587 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
588 pv_entry_high_water = 9 * (pv_entry_max / 10);
589 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
593 /***************************************************
594 * Low level helper routines.....
595 ***************************************************/
598 * The modification bit is not tracked for any pages in this range. XXX
599 * such pages in this maps should always use pmap_k*() functions and not
602 * XXX User and kernel address spaces are independant for virtual kernels,
603 * this function only applies to the kernel pmap.
606 pmap_track_modified(pmap_t pmap, vm_offset_t va)
608 if (pmap != &kernel_pmap)
610 if ((va < clean_sva) || (va >= clean_eva))
617 * Extract the physical page address associated with the map/VA pair.
622 pmap_extract(pmap_t pmap, vm_offset_t va)
626 pd_entry_t pde, *pdep;
628 lwkt_gettoken(&vm_token);
630 pdep = pmap_pde(pmap, va);
634 if ((pde & VPTE_PS) != 0) {
636 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
638 pte = pmap_pde_to_pte(pdep, va);
639 rtval = (*pte & VPTE_FRAME) | (va & PAGE_MASK);
643 lwkt_reltoken(&vm_token);
648 * Routine: pmap_kextract
650 * Extract the physical page address associated
651 * kernel virtual address.
654 pmap_kextract(vm_offset_t va)
659 KKASSERT(va >= KvaStart && va < KvaEnd);
662 * The DMAP region is not included in [KvaStart, KvaEnd)
665 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
666 pa = DMAP_TO_PHYS(va);
672 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
675 * Beware of a concurrent promotion that changes the
676 * PDE at this point! For example, vtopte() must not
677 * be used to access the PTE because it would use the
678 * new PDE. It is, however, safe to use the old PDE
679 * because the page table page is preserved by the
682 pa = *pmap_pde_to_pte(&pde, va);
683 pa = (pa & VPTE_FRAME) | (va & PAGE_MASK);
691 /***************************************************
692 * Low level mapping routines.....
693 ***************************************************/
696 * Enter a mapping into kernel_pmap. Mappings created in this fashion
697 * are not managed. Mappings must be immediately accessible on all cpus.
699 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
700 * real pmap and handle related races before storing the new vpte.
703 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
708 KKASSERT(va >= KvaStart && va < KvaEnd);
709 npte = pa | VPTE_R | VPTE_W | VPTE_V;
712 pmap_inval_pte(pte, &kernel_pmap, va);
717 * Enter an unmanaged KVA mapping for the private use of the current
718 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
721 * It is illegal for the mapping to be accessed by other cpus unleess
722 * pmap_kenter_sync*() is called.
725 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
730 KKASSERT(va >= KvaStart && va < KvaEnd);
732 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
735 pmap_inval_pte_quick(pte, &kernel_pmap, va);
737 //cpu_invlpg((void *)va);
741 * Synchronize a kvm mapping originally made for the private use on
742 * some other cpu so it can be used on all cpus.
744 * XXX add MADV_RESYNC to improve performance.
747 pmap_kenter_sync(vm_offset_t va)
749 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
753 * Synchronize a kvm mapping originally made for the private use on
754 * some other cpu so it can be used on our cpu. Turns out to be the
755 * same madvise() call, because we have to sync the real pmaps anyway.
757 * XXX add MADV_RESYNC to improve performance.
760 pmap_kenter_sync_quick(vm_offset_t va)
762 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
766 * Remove an unmanaged mapping created with pmap_kenter*().
769 pmap_kremove(vm_offset_t va)
773 KKASSERT(va >= KvaStart && va < KvaEnd);
777 pmap_inval_pte(pte, &kernel_pmap, va);
782 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
783 * only with this cpu.
785 * Unfortunately because we optimize new entries by testing VPTE_V later
786 * on, we actually still have to synchronize with all the cpus. XXX maybe
787 * store a junk value and test against 0 in the other places instead?
790 pmap_kremove_quick(vm_offset_t va)
794 KKASSERT(va >= KvaStart && va < KvaEnd);
798 pmap_inval_pte(pte, &kernel_pmap, va); /* NOT _quick */
803 * Used to map a range of physical addresses into kernel
804 * virtual address space.
806 * For now, VM is already on, we only need to map the
810 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
812 return PHYS_TO_DMAP(start);
817 * Map a set of unmanaged VM pages into KVM.
820 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
824 end_va = va + count * PAGE_SIZE;
825 KKASSERT(va >= KvaStart && end_va < KvaEnd);
827 while (va < end_va) {
832 pmap_inval_pte(pte, &kernel_pmap, va);
833 *pte = VM_PAGE_TO_PHYS(*m) | VPTE_R | VPTE_W | VPTE_V;
840 * Undo the effects of pmap_qenter*().
843 pmap_qremove(vm_offset_t va, int count)
847 end_va = va + count * PAGE_SIZE;
848 KKASSERT(va >= KvaStart && end_va < KvaEnd);
850 while (va < end_va) {
855 pmap_inval_pte(pte, &kernel_pmap, va);
862 * This routine works like vm_page_lookup() but also blocks as long as the
863 * page is busy. This routine does not busy the page it returns.
865 * Unless the caller is managing objects whos pages are in a known state,
866 * the call should be made with a critical section held so the page's object
867 * association remains valid on return.
870 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
874 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
875 m = vm_page_lookup_busy_wait(object, pindex, FALSE, "pplookp");
881 * Create a new thread and optionally associate it with a (new) process.
882 * NOTE! the new thread's cpu may not equal the current cpu.
885 pmap_init_thread(thread_t td)
887 /* enforce pcb placement */
888 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
889 td->td_savefpu = &td->td_pcb->pcb_save;
890 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */
894 * This routine directly affects the fork perf for a process.
897 pmap_init_proc(struct proc *p)
902 * Dispose the UPAGES for a process that has exited.
903 * This routine directly impacts the exit perf of a process.
906 pmap_dispose_proc(struct proc *p)
908 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
911 /***************************************************
912 * Page table page management routines.....
913 ***************************************************/
915 static __inline int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va,
919 * This routine unholds page table pages, and if the hold count
920 * drops to zero, then it decrements the wire count.
922 * We must recheck that this is the last hold reference after busy-sleeping
926 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
928 vm_page_busy_wait(m, FALSE, "pmuwpt");
929 KASSERT(m->queue == PQ_NONE,
930 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
932 if (m->hold_count == 1) {
934 * Unmap the page table page.
937 /* pmap_inval_add(info, pmap, -1); */
939 if (m->pindex >= (NUPDE + NUPDPE)) {
942 pml4 = pmap_pml4e(pmap, va);
944 } else if (m->pindex >= NUPDE) {
947 pdp = pmap_pdpe(pmap, va);
952 pd = pmap_pde(pmap, va);
956 KKASSERT(pmap->pm_stats.resident_count > 0);
957 --pmap->pm_stats.resident_count;
959 if (pmap->pm_ptphint == m)
960 pmap->pm_ptphint = NULL;
962 if (m->pindex < NUPDE) {
963 /* We just released a PT, unhold the matching PD */
966 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & VPTE_FRAME);
967 pmap_unwire_pte_hold(pmap, va, pdpg);
969 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
970 /* We just released a PD, unhold the matching PDP */
973 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & VPTE_FRAME);
974 pmap_unwire_pte_hold(pmap, va, pdppg);
978 * This was our last hold, the page had better be unwired
979 * after we decrement wire_count.
981 * FUTURE NOTE: shared page directory page could result in
982 * multiple wire counts.
986 KKASSERT(m->wire_count == 0);
987 atomic_add_int(&vmstats.v_wire_count, -1);
988 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
990 vm_page_free_zero(m);
993 KKASSERT(m->hold_count > 1);
1001 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
1003 KKASSERT(m->hold_count > 0);
1004 if (m->hold_count > 1) {
1008 return _pmap_unwire_pte_hold(pmap, va, m);
1013 * After removing a page table entry, this routine is used to
1014 * conditionally free the page, and manage the hold/wire counts.
1017 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1019 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1020 vm_pindex_t ptepindex;
1022 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1026 * page table pages in the kernel_pmap are not managed.
1028 if (pmap == &kernel_pmap)
1030 ptepindex = pmap_pde_pindex(va);
1031 if (pmap->pm_ptphint &&
1032 (pmap->pm_ptphint->pindex == ptepindex)) {
1033 mpte = pmap->pm_ptphint;
1035 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1036 pmap->pm_ptphint = mpte;
1037 vm_page_wakeup(mpte);
1041 return pmap_unwire_pte_hold(pmap, va, mpte);
1045 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
1046 * just dummy it up so it works well enough for fork().
1048 * In DragonFly, process pmaps may only be used to manipulate user address
1049 * space, never kernel address space.
1052 pmap_pinit0(struct pmap *pmap)
1058 * Initialize a preallocated and zeroed pmap structure,
1059 * such as one in a vmspace structure.
1062 pmap_pinit(struct pmap *pmap)
1067 * No need to allocate page table space yet but we do need a valid
1068 * page directory table.
1070 if (pmap->pm_pml4 == NULL) {
1072 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1076 * Allocate an object for the ptes
1078 if (pmap->pm_pteobj == NULL)
1079 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1082 * Allocate the page directory page, unless we already have
1083 * one cached. If we used the cached page the wire_count will
1084 * already be set appropriately.
1086 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1087 ptdpg = vm_page_grab(pmap->pm_pteobj,
1088 NUPDE + NUPDPE + PML4PML4I,
1089 VM_ALLOC_NORMAL | VM_ALLOC_RETRY |
1091 pmap->pm_pdirm = ptdpg;
1092 vm_page_flag_clear(ptdpg, PG_MAPPED);
1093 if (ptdpg->wire_count == 0)
1094 atomic_add_int(&vmstats.v_wire_count, 1);
1095 ptdpg->wire_count = 1;
1096 vm_page_wakeup(ptdpg);
1097 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1100 pmap->pm_active = 0;
1101 pmap->pm_ptphint = NULL;
1102 TAILQ_INIT(&pmap->pm_pvlist);
1103 TAILQ_INIT(&pmap->pm_pvlist_free);
1104 spin_init(&pmap->pm_spin);
1105 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1106 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1107 pmap->pm_stats.resident_count = 1;
1111 * Clean up a pmap structure so it can be physically freed. This routine
1112 * is called by the vmspace dtor function. A great deal of pmap data is
1113 * left passively mapped to improve vmspace management so we have a bit
1114 * of cleanup work to do here.
1119 pmap_puninit(pmap_t pmap)
1123 KKASSERT(pmap->pm_active == 0);
1124 if ((p = pmap->pm_pdirm) != NULL) {
1125 KKASSERT(pmap->pm_pml4 != NULL);
1126 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1127 vm_page_busy_wait(p, FALSE, "pgpun");
1129 atomic_add_int(&vmstats.v_wire_count, -1);
1130 vm_page_free_zero(p);
1131 pmap->pm_pdirm = NULL;
1133 if (pmap->pm_pml4) {
1134 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1135 pmap->pm_pml4 = NULL;
1137 if (pmap->pm_pteobj) {
1138 vm_object_deallocate(pmap->pm_pteobj);
1139 pmap->pm_pteobj = NULL;
1144 * Wire in kernel global address entries. To avoid a race condition
1145 * between pmap initialization and pmap_growkernel, this procedure
1146 * adds the pmap to the master list (which growkernel scans to update),
1147 * then copies the template.
1149 * In a virtual kernel there are no kernel global address entries.
1154 pmap_pinit2(struct pmap *pmap)
1156 spin_lock(&pmap_spin);
1157 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1158 spin_unlock(&pmap_spin);
1162 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1163 * 0 on failure (if the procedure had to sleep).
1165 * When asked to remove the page directory page itself, we actually just
1166 * leave it cached so we do not have to incur the SMP inval overhead of
1167 * removing the kernel mapping. pmap_puninit() will take care of it.
1170 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1173 * This code optimizes the case of freeing non-busy
1174 * page-table pages. Those pages are zero now, and
1175 * might as well be placed directly into the zero queue.
1177 if (vm_page_busy_try(p, FALSE)) {
1178 vm_page_sleep_busy(p, FALSE, "pmaprl");
1183 * Remove the page table page from the processes address space.
1185 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1187 * We are the pml4 table itself.
1189 /* XXX anything to do here? */
1190 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1192 * We are a PDP page.
1193 * We look for the PML4 entry that points to us.
1195 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1196 KKASSERT(m4 != NULL);
1197 pml4_entry_t *pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1198 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1199 KKASSERT(pml4[idx] != 0);
1202 /* JG What about wire_count? */
1203 } else if (p->pindex >= NUPDE) {
1206 * We look for the PDP entry that points to us.
1208 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1209 KKASSERT(m3 != NULL);
1210 pdp_entry_t *pdp = (pdp_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1211 int idx = (p->pindex - NUPDE) % NPDPEPG;
1212 KKASSERT(pdp[idx] != 0);
1215 /* JG What about wire_count? */
1217 /* We are a PT page.
1218 * We look for the PD entry that points to us.
1220 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1221 KKASSERT(m2 != NULL);
1222 pd_entry_t *pd = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1223 int idx = p->pindex % NPDEPG;
1226 /* JG What about wire_count? */
1228 KKASSERT(pmap->pm_stats.resident_count > 0);
1229 --pmap->pm_stats.resident_count;
1231 if (p->hold_count) {
1232 panic("pmap_release: freeing held page table page");
1234 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1235 pmap->pm_ptphint = NULL;
1238 * We leave the top-level page table page cached, wired, and mapped in
1239 * the pmap until the dtor function (pmap_puninit()) gets called.
1240 * However, still clean it up so we can set PG_ZERO.
1242 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1243 bzero(pmap->pm_pml4, PAGE_SIZE);
1244 vm_page_flag_set(p, PG_ZERO);
1249 atomic_add_int(&vmstats.v_wire_count, -1);
1250 /* JG eventually revert to using vm_page_free_zero() */
1257 * this routine is called if the page table page is not
1261 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1263 vm_page_t m, pdppg, pdpg;
1266 * Find or fabricate a new pagetable page. Handle allocation
1267 * races by checking m->valid.
1269 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1270 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1272 KASSERT(m->queue == PQ_NONE,
1273 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1276 * Increment the hold count for the page we will be returning to
1281 if (m->wire_count == 0)
1282 atomic_add_int(&vmstats.v_wire_count, 1);
1286 * Map the pagetable page into the process address space, if
1287 * it isn't already there.
1289 ++pmap->pm_stats.resident_count;
1291 if (ptepindex >= (NUPDE + NUPDPE)) {
1293 vm_pindex_t pml4index;
1295 /* Wire up a new PDP page */
1296 pml4index = ptepindex - (NUPDE + NUPDPE);
1297 pml4 = &pmap->pm_pml4[pml4index];
1298 *pml4 = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1300 } else if (ptepindex >= NUPDE) {
1301 vm_pindex_t pml4index;
1302 vm_pindex_t pdpindex;
1306 /* Wire up a new PD page */
1307 pdpindex = ptepindex - NUPDE;
1308 pml4index = pdpindex >> NPML4EPGSHIFT;
1310 pml4 = &pmap->pm_pml4[pml4index];
1311 if ((*pml4 & VPTE_V) == 0) {
1312 /* Have to allocate a new PDP page, recurse */
1313 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1320 /* Add reference to the PDP page */
1321 pdppg = PHYS_TO_VM_PAGE(*pml4 & VPTE_FRAME);
1322 pdppg->hold_count++;
1324 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1326 /* Now find the pdp page */
1327 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1328 KKASSERT(*pdp == 0); /* JG DEBUG64 */
1329 *pdp = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1332 vm_pindex_t pml4index;
1333 vm_pindex_t pdpindex;
1338 /* Wire up a new PT page */
1339 pdpindex = ptepindex >> NPDPEPGSHIFT;
1340 pml4index = pdpindex >> NPML4EPGSHIFT;
1342 /* First, find the pdp and check that its valid. */
1343 pml4 = &pmap->pm_pml4[pml4index];
1344 if ((*pml4 & VPTE_V) == 0) {
1345 /* We miss a PDP page. We ultimately need a PD page.
1346 * Recursively allocating a PD page will allocate
1347 * the missing PDP page and will also allocate
1348 * the PD page we need.
1350 /* Have to allocate a new PD page, recurse */
1351 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1357 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1358 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1360 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1361 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1362 if ((*pdp & VPTE_V) == 0) {
1363 /* Have to allocate a new PD page, recurse */
1364 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1371 /* Add reference to the PD page */
1372 pdpg = PHYS_TO_VM_PAGE(*pdp & VPTE_FRAME);
1376 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & VPTE_FRAME);
1378 /* Now we know where the page directory page is */
1379 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1380 KKASSERT(*pd == 0); /* JG DEBUG64 */
1381 *pd = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1386 * Set the page table hint
1388 pmap->pm_ptphint = m;
1389 vm_page_flag_set(m, PG_MAPPED);
1396 * Determine the page table page required to access the VA in the pmap
1397 * and allocate it if necessary. Return a held vm_page_t for the page.
1399 * Only used with user pmaps.
1402 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1404 vm_pindex_t ptepindex;
1408 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1411 * Calculate pagetable page index
1413 ptepindex = pmap_pde_pindex(va);
1416 * Get the page directory entry
1418 pd = pmap_pde(pmap, va);
1421 * This supports switching from a 2MB page to a
1424 if (pd != NULL && (*pd & (VPTE_PS | VPTE_V)) == (VPTE_PS | VPTE_V)) {
1425 panic("no promotion/demotion yet");
1433 * If the page table page is mapped, we just increment the
1434 * hold count, and activate it.
1436 if (pd != NULL && (*pd & VPTE_V) != 0) {
1437 /* YYY hint is used here on i386 */
1438 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1439 pmap->pm_ptphint = m;
1445 * Here if the pte page isn't mapped, or if it has been deallocated.
1447 return _pmap_allocpte(pmap, ptepindex);
1451 /***************************************************
1452 * Pmap allocation/deallocation routines.
1453 ***************************************************/
1456 * Release any resources held by the given physical map.
1457 * Called when a pmap initialized by pmap_pinit is being released.
1458 * Should only be called if the map contains no valid mappings.
1460 * Caller must hold pmap->pm_token
1462 static int pmap_release_callback(struct vm_page *p, void *data);
1465 pmap_release(struct pmap *pmap)
1467 vm_object_t object = pmap->pm_pteobj;
1468 struct rb_vm_page_scan_info info;
1470 KKASSERT(pmap != &kernel_pmap);
1472 #if defined(DIAGNOSTIC)
1473 if (object->ref_count != 1)
1474 panic("pmap_release: pteobj reference count != 1");
1478 info.object = object;
1480 spin_lock(&pmap_spin);
1481 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1482 spin_unlock(&pmap_spin);
1484 vm_object_hold(object);
1488 info.limit = object->generation;
1490 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1491 pmap_release_callback, &info);
1492 if (info.error == 0 && info.mpte) {
1493 if (!pmap_release_free_page(pmap, info.mpte))
1496 } while (info.error);
1497 vm_object_drop(object);
1501 pmap_release_callback(struct vm_page *p, void *data)
1503 struct rb_vm_page_scan_info *info = data;
1505 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1509 if (!pmap_release_free_page(info->pmap, p)) {
1513 if (info->object->generation != info->limit) {
1521 * Grow the number of kernel page table entries, if needed.
1526 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
1530 vm_offset_t ptppaddr;
1532 pd_entry_t *pde, newpdir;
1537 vm_object_hold(kptobj);
1538 if (kernel_vm_end == 0) {
1539 kernel_vm_end = KvaStart;
1541 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & VPTE_V) != 0) {
1542 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1544 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1545 kernel_vm_end = kernel_map.max_offset;
1550 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1551 if (addr - 1 >= kernel_map.max_offset)
1552 addr = kernel_map.max_offset;
1553 while (kernel_vm_end < addr) {
1554 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1556 /* We need a new PDP entry */
1557 nkpg = vm_page_alloc(kptobj, nkpt,
1558 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1559 | VM_ALLOC_INTERRUPT);
1561 panic("pmap_growkernel: no memory to "
1564 paddr = VM_PAGE_TO_PHYS(nkpg);
1565 if ((nkpg->flags & PG_ZERO) == 0)
1566 pmap_zero_page(paddr);
1567 vm_page_flag_clear(nkpg, PG_ZERO);
1568 newpdp = (pdp_entry_t)(paddr | VPTE_V | VPTE_R |
1569 VPTE_W | VPTE_A | VPTE_M);
1570 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1572 continue; /* try again */
1574 if ((*pde & VPTE_V) != 0) {
1575 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1576 ~(PAGE_SIZE * NPTEPG - 1);
1577 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1578 kernel_vm_end = kernel_map.max_offset;
1585 * This index is bogus, but out of the way
1587 nkpg = vm_page_alloc(kptobj, nkpt,
1590 VM_ALLOC_INTERRUPT);
1592 panic("pmap_growkernel: no memory to grow kernel");
1595 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1596 pmap_zero_page(ptppaddr);
1597 vm_page_flag_clear(nkpg, PG_ZERO);
1598 newpdir = (pd_entry_t)(ptppaddr | VPTE_V | VPTE_R |
1599 VPTE_W | VPTE_A | VPTE_M);
1600 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1603 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1604 ~(PAGE_SIZE * NPTEPG - 1);
1605 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1606 kernel_vm_end = kernel_map.max_offset;
1610 vm_object_drop(kptobj);
1614 * Retire the given physical map from service. Should only be called
1615 * if the map contains no valid mappings.
1620 pmap_destroy(pmap_t pmap)
1625 lwkt_gettoken(&vm_token);
1626 if (--pmap->pm_count == 0) {
1628 panic("destroying a pmap is not yet implemented");
1630 lwkt_reltoken(&vm_token);
1634 * Add a reference to the specified pmap.
1639 pmap_reference(pmap_t pmap)
1642 lwkt_gettoken(&vm_token);
1644 lwkt_reltoken(&vm_token);
1648 /************************************************************************
1649 * VMSPACE MANAGEMENT *
1650 ************************************************************************
1652 * The VMSPACE management we do in our virtual kernel must be reflected
1653 * in the real kernel. This is accomplished by making vmspace system
1654 * calls to the real kernel.
1657 cpu_vmspace_alloc(struct vmspace *vm)
1663 #define USER_SIZE (VM_MAX_USER_ADDRESS - VM_MIN_USER_ADDRESS)
1665 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
1666 panic("vmspace_create() failed");
1668 rp = vmspace_mmap(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1669 PROT_READ|PROT_WRITE,
1670 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
1672 if (rp == MAP_FAILED)
1673 panic("vmspace_mmap: failed");
1674 vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1676 vpte = VM_PAGE_TO_PHYS(vmspace_pmap(vm)->pm_pdirm) | VPTE_R | VPTE_W | VPTE_V;
1677 r = vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1680 panic("vmspace_mcontrol: failed");
1684 cpu_vmspace_free(struct vmspace *vm)
1686 if (vmspace_destroy(&vm->vm_pmap) < 0)
1687 panic("vmspace_destroy() failed");
1690 /***************************************************
1691 * page management routines.
1692 ***************************************************/
1695 * free the pv_entry back to the free list. This function may be
1696 * called from an interrupt.
1698 static __inline void
1699 free_pv_entry(pv_entry_t pv)
1702 KKASSERT(pv_entry_count >= 0);
1707 * get a new pv_entry, allocating a block from the system
1708 * when needed. This function may be called from an interrupt.
1714 if (pv_entry_high_water &&
1715 (pv_entry_count > pv_entry_high_water) &&
1716 (pmap_pagedaemon_waken == 0)) {
1717 pmap_pagedaemon_waken = 1;
1718 wakeup(&vm_pages_needed);
1720 return zalloc(pvzone);
1724 * This routine is very drastic, but can save the system
1734 static int warningdone=0;
1736 if (pmap_pagedaemon_waken == 0)
1738 lwkt_gettoken(&vm_token);
1739 pmap_pagedaemon_waken = 0;
1741 if (warningdone < 5) {
1742 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1746 for (i = 0; i < vm_page_array_size; i++) {
1747 m = &vm_page_array[i];
1748 if (m->wire_count || m->hold_count)
1750 if (vm_page_busy_try(m, TRUE) == 0) {
1751 if (m->wire_count == 0 && m->hold_count == 0) {
1757 lwkt_reltoken(&vm_token);
1762 * If it is the first entry on the list, it is actually
1763 * in the header and we must copy the following entry up
1764 * to the header. Otherwise we must search the list for
1765 * the entry. In either case we free the now unused entry.
1767 * caller must hold vm_token.
1770 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1775 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1776 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1777 if (pmap == pv->pv_pmap && va == pv->pv_va)
1781 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1782 if (va == pv->pv_va)
1788 * Note that pv_ptem is NULL if the page table page itself is not
1789 * managed, even if the page being removed IS managed.
1792 /* JGXXX When can 'pv' be NULL? */
1794 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1795 m->md.pv_list_count--;
1796 atomic_add_int(&m->object->agg_pv_list_count, -1);
1797 KKASSERT(m->md.pv_list_count >= 0);
1798 if (TAILQ_EMPTY(&m->md.pv_list))
1799 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1800 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1801 ++pmap->pm_generation;
1802 KKASSERT(pmap->pm_pteobj != NULL);
1803 vm_object_hold(pmap->pm_pteobj);
1804 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1805 vm_object_drop(pmap->pm_pteobj);
1812 * Create a pv entry for page at pa for (pmap, va). If the page table page
1813 * holding the VA is managed, mpte will be non-NULL.
1816 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1821 pv = get_pv_entry();
1826 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1827 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1828 m->md.pv_list_count++;
1829 atomic_add_int(&m->object->agg_pv_list_count, 1);
1835 * pmap_remove_pte: do the things to unmap a page in a process
1838 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1843 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1844 if (oldpte & VPTE_WIRED)
1845 --pmap->pm_stats.wired_count;
1846 KKASSERT(pmap->pm_stats.wired_count >= 0);
1850 * Machines that don't support invlpg, also don't support
1851 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1855 cpu_invlpg((void *)va);
1857 KKASSERT(pmap->pm_stats.resident_count > 0);
1858 --pmap->pm_stats.resident_count;
1859 if (oldpte & VPTE_MANAGED) {
1860 m = PHYS_TO_VM_PAGE(oldpte);
1861 if (oldpte & VPTE_M) {
1862 #if defined(PMAP_DIAGNOSTIC)
1863 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1864 kprintf("pmap_remove: modified page not "
1865 "writable: va: 0x%lx, pte: 0x%lx\n",
1869 if (pmap_track_modified(pmap, va))
1872 if (oldpte & VPTE_A)
1873 vm_page_flag_set(m, PG_REFERENCED);
1874 return pmap_remove_entry(pmap, m, va);
1876 return pmap_unuse_pt(pmap, va, NULL);
1885 * Remove a single page from a process address space.
1887 * This function may not be called from an interrupt if the pmap is
1891 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1895 pte = pmap_pte(pmap, va);
1898 if ((*pte & VPTE_V) == 0)
1900 pmap_remove_pte(pmap, pte, va);
1904 * Remove the given range of addresses from the specified map.
1906 * It is assumed that the start and end are properly rounded to
1909 * This function may not be called from an interrupt if the pmap is
1915 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1917 vm_offset_t va_next;
1918 pml4_entry_t *pml4e;
1920 pd_entry_t ptpaddr, *pde;
1926 vm_object_hold(pmap->pm_pteobj);
1927 lwkt_gettoken(&vm_token);
1928 KKASSERT(pmap->pm_stats.resident_count >= 0);
1929 if (pmap->pm_stats.resident_count == 0) {
1930 lwkt_reltoken(&vm_token);
1931 vm_object_drop(pmap->pm_pteobj);
1936 * special handling of removing one page. a very
1937 * common operation and easy to short circuit some
1940 if (sva + PAGE_SIZE == eva) {
1941 pde = pmap_pde(pmap, sva);
1942 if (pde && (*pde & VPTE_PS) == 0) {
1943 pmap_remove_page(pmap, sva);
1944 lwkt_reltoken(&vm_token);
1945 vm_object_drop(pmap->pm_pteobj);
1950 for (; sva < eva; sva = va_next) {
1951 pml4e = pmap_pml4e(pmap, sva);
1952 if ((*pml4e & VPTE_V) == 0) {
1953 va_next = (sva + NBPML4) & ~PML4MASK;
1959 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
1960 if ((*pdpe & VPTE_V) == 0) {
1961 va_next = (sva + NBPDP) & ~PDPMASK;
1968 * Calculate index for next page table.
1970 va_next = (sva + NBPDR) & ~PDRMASK;
1974 pde = pmap_pdpe_to_pde(pdpe, sva);
1978 * Weed out invalid mappings.
1984 * Check for large page.
1986 if ((ptpaddr & VPTE_PS) != 0) {
1987 /* JG FreeBSD has more complex treatment here */
1988 KKASSERT(*pde != 0);
1989 pmap_inval_pde(pde, pmap, sva);
1990 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1995 * Limit our scan to either the end of the va represented
1996 * by the current page table page, or to the end of the
1997 * range being removed.
2003 * NOTE: pmap_remove_pte() can block.
2005 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2009 if (pmap_remove_pte(pmap, pte, sva))
2013 lwkt_reltoken(&vm_token);
2014 vm_object_drop(pmap->pm_pteobj);
2018 * Removes this physical page from all physical maps in which it resides.
2019 * Reflects back modify bits to the pager.
2021 * This routine may not be called from an interrupt.
2026 pmap_remove_all(vm_page_t m)
2028 pt_entry_t *pte, tpte;
2031 #if defined(PMAP_DIAGNOSTIC)
2033 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
2036 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
2037 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
2041 lwkt_gettoken(&vm_token);
2042 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2043 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2044 --pv->pv_pmap->pm_stats.resident_count;
2046 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2047 KKASSERT(pte != NULL);
2049 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
2050 if (tpte & VPTE_WIRED)
2051 pv->pv_pmap->pm_stats.wired_count--;
2052 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
2055 vm_page_flag_set(m, PG_REFERENCED);
2058 * Update the vm_page_t clean and reference bits.
2060 if (tpte & VPTE_M) {
2061 #if defined(PMAP_DIAGNOSTIC)
2062 if (pmap_nw_modified(tpte)) {
2064 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2068 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
2071 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2072 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2073 ++pv->pv_pmap->pm_generation;
2074 m->md.pv_list_count--;
2075 atomic_add_int(&m->object->agg_pv_list_count, -1);
2076 KKASSERT(m->md.pv_list_count >= 0);
2077 if (TAILQ_EMPTY(&m->md.pv_list))
2078 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2079 vm_object_hold(pv->pv_pmap->pm_pteobj);
2080 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2081 vm_object_drop(pv->pv_pmap->pm_pteobj);
2084 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2085 lwkt_reltoken(&vm_token);
2089 * Set the physical protection on the specified range of this map
2092 * This function may not be called from an interrupt if the map is
2093 * not the kernel_pmap.
2098 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2100 vm_offset_t va_next;
2101 pml4_entry_t *pml4e;
2103 pd_entry_t ptpaddr, *pde;
2106 /* JG review for NX */
2111 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2112 pmap_remove(pmap, sva, eva);
2116 if (prot & VM_PROT_WRITE)
2119 lwkt_gettoken(&vm_token);
2121 for (; sva < eva; sva = va_next) {
2123 pml4e = pmap_pml4e(pmap, sva);
2124 if ((*pml4e & VPTE_V) == 0) {
2125 va_next = (sva + NBPML4) & ~PML4MASK;
2131 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2132 if ((*pdpe & VPTE_V) == 0) {
2133 va_next = (sva + NBPDP) & ~PDPMASK;
2139 va_next = (sva + NBPDR) & ~PDRMASK;
2143 pde = pmap_pdpe_to_pde(pdpe, sva);
2147 * Check for large page.
2149 if ((ptpaddr & VPTE_PS) != 0) {
2151 pmap_clean_pde(pde, pmap, sva);
2152 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2157 * Weed out invalid mappings. Note: we assume that the page
2158 * directory table is always allocated, and in kernel virtual.
2166 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2172 * Clean managed pages and also check the accessed
2173 * bit. Just remove write perms for unmanaged
2174 * pages. Be careful of races, turning off write
2175 * access will force a fault rather then setting
2176 * the modified bit at an unexpected time.
2178 if (*pte & VPTE_MANAGED) {
2179 pbits = pmap_clean_pte(pte, pmap, sva);
2181 if (pbits & VPTE_A) {
2182 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2183 vm_page_flag_set(m, PG_REFERENCED);
2184 atomic_clear_long(pte, VPTE_A);
2186 if (pbits & VPTE_M) {
2187 if (pmap_track_modified(pmap, sva)) {
2189 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2194 pbits = pmap_setro_pte(pte, pmap, sva);
2198 lwkt_reltoken(&vm_token);
2202 * Enter a managed page into a pmap. If the page is not wired related pmap
2203 * data can be destroyed at any time for later demand-operation.
2205 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
2206 * specified protection, and wire the mapping if requested.
2208 * NOTE: This routine may not lazy-evaluate or lose information. The
2209 * page must actually be inserted into the given map NOW.
2211 * NOTE: When entering a page at a KVA address, the pmap must be the
2217 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2224 pt_entry_t origpte, newpte;
2230 va = trunc_page(va);
2232 vm_object_hold(pmap->pm_pteobj);
2233 lwkt_gettoken(&vm_token);
2236 * Get the page table page. The kernel_pmap's page table pages
2237 * are preallocated and have no associated vm_page_t.
2239 if (pmap == &kernel_pmap)
2242 mpte = pmap_allocpte(pmap, va);
2244 pde = pmap_pde(pmap, va);
2245 if (pde != NULL && (*pde & VPTE_V) != 0) {
2246 if ((*pde & VPTE_PS) != 0)
2247 panic("pmap_enter: attempted pmap_enter on 2MB page");
2248 pte = pmap_pde_to_pte(pde, va);
2250 panic("pmap_enter: invalid page directory va=%#lx", va);
2253 KKASSERT(pte != NULL);
2255 * Deal with races on the original mapping (though don't worry
2256 * about VPTE_A races) by cleaning it. This will force a fault
2257 * if an attempt is made to write to the page.
2259 pa = VM_PAGE_TO_PHYS(m);
2260 origpte = pmap_clean_pte(pte, pmap, va);
2261 opa = origpte & VPTE_FRAME;
2263 if (origpte & VPTE_PS)
2264 panic("pmap_enter: attempted pmap_enter on 2MB page");
2267 * Mapping has not changed, must be protection or wiring change.
2269 if (origpte && (opa == pa)) {
2271 * Wiring change, just update stats. We don't worry about
2272 * wiring PT pages as they remain resident as long as there
2273 * are valid mappings in them. Hence, if a user page is wired,
2274 * the PT page will be also.
2276 if (wired && ((origpte & VPTE_WIRED) == 0))
2277 ++pmap->pm_stats.wired_count;
2278 else if (!wired && (origpte & VPTE_WIRED))
2279 --pmap->pm_stats.wired_count;
2282 * Remove the extra pte reference. Note that we cannot
2283 * optimize the RO->RW case because we have adjusted the
2284 * wiring count above and may need to adjust the wiring
2291 * We might be turning off write access to the page,
2292 * so we go ahead and sense modify status.
2294 if (origpte & VPTE_MANAGED) {
2295 if ((origpte & VPTE_M) &&
2296 pmap_track_modified(pmap, va)) {
2298 om = PHYS_TO_VM_PAGE(opa);
2302 KKASSERT(m->flags & PG_MAPPED);
2307 * Mapping has changed, invalidate old range and fall through to
2308 * handle validating new mapping.
2312 err = pmap_remove_pte(pmap, pte, va);
2314 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2318 * Enter on the PV list if part of our managed memory. Note that we
2319 * raise IPL while manipulating pv_table since pmap_enter can be
2320 * called at interrupt time.
2322 if (pmap_initialized &&
2323 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2324 pmap_insert_entry(pmap, va, mpte, m);
2326 vm_page_flag_set(m, PG_MAPPED);
2330 * Increment counters
2332 ++pmap->pm_stats.resident_count;
2334 pmap->pm_stats.wired_count++;
2338 * Now validate mapping with desired protection/wiring.
2340 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | VPTE_V);
2343 newpte |= VPTE_WIRED;
2344 if (pmap != &kernel_pmap)
2348 * If the mapping or permission bits are different from the
2349 * (now cleaned) original pte, an update is needed. We've
2350 * already downgraded or invalidated the page so all we have
2351 * to do now is update the bits.
2353 * XXX should we synchronize RO->RW changes to avoid another
2356 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
2357 *pte = newpte | VPTE_A;
2358 if (newpte & VPTE_W)
2359 vm_page_flag_set(m, PG_WRITEABLE);
2361 KKASSERT((newpte & VPTE_MANAGED) == 0 || (m->flags & PG_MAPPED));
2362 lwkt_reltoken(&vm_token);
2363 vm_object_drop(pmap->pm_pteobj);
2367 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2369 * Currently this routine may only be used on user pmaps, not kernel_pmap.
2374 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2379 vm_pindex_t ptepindex;
2382 KKASSERT(pmap != &kernel_pmap);
2384 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
2387 * Calculate pagetable page index
2389 ptepindex = pmap_pde_pindex(va);
2391 vm_object_hold(pmap->pm_pteobj);
2392 lwkt_gettoken(&vm_token);
2396 * Get the page directory entry
2398 ptepa = pmap_pde(pmap, va);
2401 * If the page table page is mapped, we just increment
2402 * the hold count, and activate it.
2404 if (ptepa && (*ptepa & VPTE_V) != 0) {
2405 if (*ptepa & VPTE_PS)
2406 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2407 if (pmap->pm_ptphint &&
2408 (pmap->pm_ptphint->pindex == ptepindex)) {
2409 mpte = pmap->pm_ptphint;
2411 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2412 pmap->pm_ptphint = mpte;
2413 vm_page_wakeup(mpte);
2418 mpte = _pmap_allocpte(pmap, ptepindex);
2420 } while (mpte == NULL);
2423 * Ok, now that the page table page has been validated, get the pte.
2424 * If the pte is already mapped undo mpte's hold_count and
2427 pte = pmap_pte(pmap, va);
2428 if (*pte & VPTE_V) {
2429 KKASSERT(mpte != NULL);
2430 pmap_unwire_pte_hold(pmap, va, mpte);
2431 pa = VM_PAGE_TO_PHYS(m);
2432 KKASSERT(((*pte ^ pa) & VPTE_FRAME) == 0);
2433 lwkt_reltoken(&vm_token);
2434 vm_object_drop(pmap->pm_pteobj);
2439 * Enter on the PV list if part of our managed memory
2441 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2442 pmap_insert_entry(pmap, va, mpte, m);
2443 vm_page_flag_set(m, PG_MAPPED);
2447 * Increment counters
2449 ++pmap->pm_stats.resident_count;
2451 pa = VM_PAGE_TO_PHYS(m);
2454 * Now validate mapping with RO protection
2456 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2457 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2459 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2460 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2461 /*pmap_inval_flush(&info); don't need for vkernel */
2462 lwkt_reltoken(&vm_token);
2463 vm_object_drop(pmap->pm_pteobj);
2467 * Make a temporary mapping for a physical address. This is only intended
2468 * to be used for panic dumps.
2470 * The caller is responsible for calling smp_invltlb().
2473 pmap_kenter_temporary(vm_paddr_t pa, long i)
2475 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
2476 return ((void *)crashdumpmap);
2479 #define MAX_INIT_PT (96)
2482 * This routine preloads the ptes for a given object into the specified pmap.
2483 * This eliminates the blast of soft faults on process startup and
2484 * immediately after an mmap.
2488 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2491 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2492 vm_object_t object, vm_pindex_t pindex,
2493 vm_size_t size, int limit)
2495 struct rb_vm_page_scan_info info;
2500 * We can't preinit if read access isn't set or there is no pmap
2503 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2507 * We can't preinit if the pmap is not the current pmap
2509 lp = curthread->td_lwp;
2510 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2513 psize = x86_64_btop(size);
2515 if ((object->type != OBJT_VNODE) ||
2516 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2517 (object->resident_page_count > MAX_INIT_PT))) {
2521 if (psize + pindex > object->size) {
2522 if (object->size < pindex)
2524 psize = object->size - pindex;
2531 * Use a red-black scan to traverse the requested range and load
2532 * any valid pages found into the pmap.
2534 * We cannot safely scan the object's memq unless we are in a
2535 * critical section since interrupts can remove pages from objects.
2537 info.start_pindex = pindex;
2538 info.end_pindex = pindex + psize - 1;
2544 vm_object_hold(object);
2545 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2546 pmap_object_init_pt_callback, &info);
2547 vm_object_drop(object);
2552 pmap_object_init_pt_callback(vm_page_t p, void *data)
2554 struct rb_vm_page_scan_info *info = data;
2555 vm_pindex_t rel_index;
2557 * don't allow an madvise to blow away our really
2558 * free pages allocating pv entries.
2560 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2561 vmstats.v_free_count < vmstats.v_free_reserved) {
2564 if (vm_page_busy_try(p, TRUE))
2566 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2567 (p->flags & PG_FICTITIOUS) == 0) {
2568 if ((p->queue - p->pc) == PQ_CACHE)
2569 vm_page_deactivate(p);
2570 rel_index = p->pindex - info->start_pindex;
2571 pmap_enter_quick(info->pmap,
2572 info->addr + x86_64_ptob(rel_index), p);
2579 * Return TRUE if the pmap is in shape to trivially
2580 * pre-fault the specified address.
2582 * Returns FALSE if it would be non-trivial or if a
2583 * pte is already loaded into the slot.
2588 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2594 lwkt_gettoken(&vm_token);
2595 pde = pmap_pde(pmap, addr);
2596 if (pde == NULL || *pde == 0) {
2599 pte = pmap_pde_to_pte(pde, addr);
2600 ret = (*pte) ? 0 : 1;
2602 lwkt_reltoken(&vm_token);
2607 * Change the wiring attribute for a map/virtual-address pair.
2609 * The mapping must already exist in the pmap.
2610 * No other requirements.
2613 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2620 lwkt_gettoken(&vm_token);
2621 pte = pmap_pte(pmap, va);
2623 if (wired && !pmap_pte_w(pte))
2624 pmap->pm_stats.wired_count++;
2625 else if (!wired && pmap_pte_w(pte))
2626 pmap->pm_stats.wired_count--;
2629 * Wiring is not a hardware characteristic so there is no need to
2630 * invalidate TLB. However, in an SMP environment we must use
2631 * a locked bus cycle to update the pte (if we are not using
2632 * the pmap_inval_*() API that is)... it's ok to do this for simple
2636 atomic_set_long(pte, VPTE_WIRED);
2638 atomic_clear_long(pte, VPTE_WIRED);
2639 lwkt_reltoken(&vm_token);
2643 * Copy the range specified by src_addr/len
2644 * from the source map to the range dst_addr/len
2645 * in the destination map.
2647 * This routine is only advisory and need not do anything.
2650 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2651 vm_size_t len, vm_offset_t src_addr)
2654 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2655 * valid through blocking calls, and that's just not going to
2666 * Zero the specified physical page.
2668 * This function may be called from an interrupt and no locking is
2672 pmap_zero_page(vm_paddr_t phys)
2674 vm_offset_t va = PHYS_TO_DMAP(phys);
2676 bzero((void *)va, PAGE_SIZE);
2680 * pmap_page_assertzero:
2682 * Assert that a page is empty, panic if it isn't.
2685 pmap_page_assertzero(vm_paddr_t phys)
2690 vm_offset_t virt = PHYS_TO_DMAP(phys);
2692 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
2693 if (*(int *)((char *)virt + i) != 0) {
2694 panic("pmap_page_assertzero() @ %p not zero!\n",
2704 * Zero part of a physical page by mapping it into memory and clearing
2705 * its contents with bzero.
2707 * off and size may not cover an area beyond a single hardware page.
2710 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2713 vm_offset_t virt = PHYS_TO_DMAP(phys);
2714 bzero((char *)virt + off, size);
2721 * Copy the physical page from the source PA to the target PA.
2722 * This function may be called from an interrupt. No locking
2726 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2728 vm_offset_t src_virt, dst_virt;
2731 src_virt = PHYS_TO_DMAP(src);
2732 dst_virt = PHYS_TO_DMAP(dst);
2733 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
2738 * pmap_copy_page_frag:
2740 * Copy the physical page from the source PA to the target PA.
2741 * This function may be called from an interrupt. No locking
2745 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2747 vm_offset_t src_virt, dst_virt;
2750 src_virt = PHYS_TO_DMAP(src);
2751 dst_virt = PHYS_TO_DMAP(dst);
2752 bcopy((char *)src_virt + (src & PAGE_MASK),
2753 (char *)dst_virt + (dst & PAGE_MASK),
2759 * Returns true if the pmap's pv is one of the first 16 pvs linked to
2760 * from this page. This count may be changed upwards or downwards
2761 * in the future; it is only necessary that true be returned for a small
2762 * subset of pmaps for proper page aging.
2764 * No other requirements.
2767 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2772 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2776 lwkt_gettoken(&vm_token);
2778 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2779 if (pv->pv_pmap == pmap) {
2780 lwkt_reltoken(&vm_token);
2788 lwkt_reltoken(&vm_token);
2794 * Remove all pages from specified address space this aids process
2795 * exit speeds. Also, this code is special cased for current
2796 * process only, but can have the more generic (and slightly slower)
2797 * mode enabled. This is much faster than pmap_remove in the case
2798 * of running down an entire address space.
2800 * No other requirements.
2803 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2805 pt_entry_t *pte, tpte;
2808 int save_generation;
2810 if (pmap->pm_pteobj)
2811 vm_object_hold(pmap->pm_pteobj);
2812 lwkt_gettoken(&vm_token);
2814 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2815 if (pv->pv_va >= eva || pv->pv_va < sva) {
2816 npv = TAILQ_NEXT(pv, pv_plist);
2820 KKASSERT(pmap == pv->pv_pmap);
2822 pte = pmap_pte(pmap, pv->pv_va);
2825 * We cannot remove wired pages from a process' mapping
2828 if (*pte & VPTE_WIRED) {
2829 npv = TAILQ_NEXT(pv, pv_plist);
2832 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2834 m = PHYS_TO_VM_PAGE(tpte & VPTE_FRAME);
2836 KASSERT(m < &vm_page_array[vm_page_array_size],
2837 ("pmap_remove_pages: bad tpte %lx", tpte));
2839 KKASSERT(pmap->pm_stats.resident_count > 0);
2840 --pmap->pm_stats.resident_count;
2843 * Update the vm_page_t clean and reference bits.
2845 if (tpte & VPTE_M) {
2849 npv = TAILQ_NEXT(pv, pv_plist);
2850 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2851 save_generation = ++pmap->pm_generation;
2853 m->md.pv_list_count--;
2854 atomic_add_int(&m->object->agg_pv_list_count, -1);
2855 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2856 if (TAILQ_EMPTY(&m->md.pv_list))
2857 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2859 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2863 * Restart the scan if we blocked during the unuse or free
2864 * calls and other removals were made.
2866 if (save_generation != pmap->pm_generation) {
2867 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2868 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2871 lwkt_reltoken(&vm_token);
2872 if (pmap->pm_pteobj)
2873 vm_object_drop(pmap->pm_pteobj);
2877 * pmap_testbit tests bits in active mappings of a VM page.
2880 pmap_testbit(vm_page_t m, int bit)
2885 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2888 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2893 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2895 * if the bit being tested is the modified bit, then
2896 * mark clean_map and ptes as never
2899 if (bit & (VPTE_A|VPTE_M)) {
2900 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2904 #if defined(PMAP_DIAGNOSTIC)
2905 if (pv->pv_pmap == NULL) {
2906 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
2910 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2921 * This routine is used to clear bits in ptes. Certain bits require special
2922 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2924 * This routine is only called with certain VPTE_* bit combinations.
2926 static __inline void
2927 pmap_clearbit(vm_page_t m, int bit)
2933 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2939 * Loop over all current mappings setting/clearing as appropos If
2940 * setting RO do we need to clear the VAC?
2942 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2944 * don't write protect pager mappings
2946 if (bit == VPTE_W) {
2947 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2951 #if defined(PMAP_DIAGNOSTIC)
2952 if (pv->pv_pmap == NULL) {
2953 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
2959 * Careful here. We can use a locked bus instruction to
2960 * clear VPTE_A or VPTE_M safely but we need to synchronize
2961 * with the target cpus when we mess with VPTE_W.
2963 * On virtual kernels we must force a new fault-on-write
2964 * in the real kernel if we clear the Modify bit ourselves,
2965 * otherwise the real kernel will not get a new fault and
2966 * will never set our Modify bit again.
2968 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2970 if (bit == VPTE_W) {
2972 * We must also clear VPTE_M when clearing
2975 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2979 } else if (bit == VPTE_M) {
2981 * We do not have to make the page read-only
2982 * when clearing the Modify bit. The real
2983 * kernel will make the real PTE read-only
2984 * or otherwise detect the write and set
2985 * our VPTE_M again simply by us invalidating
2986 * the real kernel VA for the pmap (as we did
2987 * above). This allows the real kernel to
2988 * handle the write fault without forwarding
2991 atomic_clear_long(pte, VPTE_M);
2992 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2994 * We've been asked to clear W & M, I guess
2995 * the caller doesn't want us to update
2996 * the dirty status of the VM page.
2998 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
3001 * We've been asked to clear bits that do
3002 * not interact with hardware.
3004 atomic_clear_long(pte, bit);
3012 * Lower the permission for all mappings to a given page.
3014 * No other requirements.
3017 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3019 /* JG NX support? */
3020 if ((prot & VM_PROT_WRITE) == 0) {
3021 lwkt_gettoken(&vm_token);
3022 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3023 pmap_clearbit(m, VPTE_W);
3024 vm_page_flag_clear(m, PG_WRITEABLE);
3028 lwkt_reltoken(&vm_token);
3033 pmap_phys_address(vm_pindex_t ppn)
3035 return (x86_64_ptob(ppn));
3039 * Return a count of reference bits for a page, clearing those bits.
3040 * It is not necessary for every reference bit to be cleared, but it
3041 * is necessary that 0 only be returned when there are truly no
3042 * reference bits set.
3044 * XXX: The exact number of bits to check and clear is a matter that
3045 * should be tested and standardized at some point in the future for
3046 * optimal aging of shared pages.
3048 * No other requirements.
3051 pmap_ts_referenced(vm_page_t m)
3053 pv_entry_t pv, pvf, pvn;
3057 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3061 lwkt_gettoken(&vm_token);
3063 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3068 pvn = TAILQ_NEXT(pv, pv_list);
3070 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3072 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3074 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3077 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3079 if (pte && (*pte & VPTE_A)) {
3081 atomic_clear_long(pte, VPTE_A);
3083 atomic_clear_long_nonlocked(pte, VPTE_A);
3090 } while ((pv = pvn) != NULL && pv != pvf);
3092 lwkt_reltoken(&vm_token);
3099 * Return whether or not the specified physical page was modified
3100 * in any physical maps.
3102 * No other requirements.
3105 pmap_is_modified(vm_page_t m)
3109 lwkt_gettoken(&vm_token);
3110 res = pmap_testbit(m, VPTE_M);
3111 lwkt_reltoken(&vm_token);
3116 * Clear the modify bits on the specified physical page.
3118 * No other requirements.
3121 pmap_clear_modify(vm_page_t m)
3123 lwkt_gettoken(&vm_token);
3124 pmap_clearbit(m, VPTE_M);
3125 lwkt_reltoken(&vm_token);
3129 * Clear the reference bit on the specified physical page.
3131 * No other requirements.
3134 pmap_clear_reference(vm_page_t m)
3136 lwkt_gettoken(&vm_token);
3137 pmap_clearbit(m, VPTE_A);
3138 lwkt_reltoken(&vm_token);
3142 * Miscellaneous support routines follow
3146 i386_protection_init(void)
3150 kp = protection_codes;
3151 for (prot = 0; prot < 8; prot++) {
3152 if (prot & VM_PROT_READ)
3154 if (prot & VM_PROT_WRITE)
3156 if (prot & VM_PROT_EXECUTE)
3163 * Perform the pmap work for mincore
3165 * No other requirements.
3168 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3170 pt_entry_t *ptep, pte;
3174 lwkt_gettoken(&vm_token);
3175 ptep = pmap_pte(pmap, addr);
3177 if (ptep && (pte = *ptep) != 0) {
3180 val = MINCORE_INCORE;
3181 if ((pte & VPTE_MANAGED) == 0)
3184 pa = pte & VPTE_FRAME;
3186 m = PHYS_TO_VM_PAGE(pa);
3192 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3194 * Modified by someone
3196 else if (m->dirty || pmap_is_modified(m))
3197 val |= MINCORE_MODIFIED_OTHER;
3202 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3205 * Referenced by someone
3207 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3208 val |= MINCORE_REFERENCED_OTHER;
3209 vm_page_flag_set(m, PG_REFERENCED);
3213 lwkt_reltoken(&vm_token);
3218 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3219 * vmspace will be ref'd and the old one will be deref'd.
3221 * Caller must hold vmspace->vm_map.token for oldvm and newvm
3224 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3226 struct vmspace *oldvm;
3230 oldvm = p->p_vmspace;
3231 if (oldvm != newvm) {
3232 p->p_vmspace = newvm;
3233 KKASSERT(p->p_nthreads == 1);
3234 lp = RB_ROOT(&p->p_lwp_tree);
3235 pmap_setlwpvm(lp, newvm);
3237 sysref_get(&newvm->vm_sysref);
3238 sysref_put(&oldvm->vm_sysref);
3245 * Set the vmspace for a LWP. The vmspace is almost universally set the
3246 * same as the process vmspace, but virtual kernels need to swap out contexts
3247 * on a per-lwp basis.
3250 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3252 struct vmspace *oldvm;
3256 oldvm = lp->lwp_vmspace;
3258 if (oldvm != newvm) {
3259 lp->lwp_vmspace = newvm;
3260 if (curthread->td_lwp == lp) {
3261 pmap = vmspace_pmap(newvm);
3263 atomic_set_cpumask(&pmap->pm_active, CPUMASK(mycpu->gd_cpuid));
3265 pmap->pm_active |= 1;
3267 #if defined(SWTCH_OPTIM_STATS)
3270 pmap = vmspace_pmap(oldvm);
3272 atomic_clear_cpumask(&pmap->pm_active,
3273 CPUMASK(mycpu->gd_cpuid));
3275 pmap->pm_active &= ~(cpumask_t)1;
3283 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3286 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3290 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3295 * Used by kmalloc/kfree, page already exists at va
3298 pmap_kvtom(vm_offset_t va)
3302 KKASSERT(va >= KvaStart && va < KvaEnd);
3304 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));