4 * Copyright (c) 1991 Regents of the University of California.
5 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
7 * Copyright (c) 2003 Peter Wemm
8 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
9 * Copyright (c) 2008, 2009 The DragonFly Project.
10 * Copyright (c) 2008, 2009 Jordan Gordeev.
11 * All rights reserved.
13 * This code is derived from software contributed to Berkeley by
14 * the Systems Programming Group of the University of Utah Computer
15 * Science Department and William Jolitz of UUNET Technologies Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
46 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
50 * Manages physical address maps.
52 * In most cases the vm_token must be held when manipulating a user pmap
53 * or elements within a vm_page, and the kvm_token must be held when
54 * manipulating the kernel pmap. Operations on user pmaps may require
55 * additional synchronization.
57 * In some cases the caller may hold the required tokens to prevent pmap
58 * functions from blocking on those same tokens. This typically only works
59 * for lookup-style operations.
65 #include "opt_msgbuf.h"
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
71 #include <sys/msgbuf.h>
72 #include <sys/vmmeter.h>
74 #include <sys/vmspace.h>
77 #include <vm/vm_param.h>
78 #include <sys/sysctl.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_extern.h>
85 #include <vm/vm_pageout.h>
86 #include <vm/vm_pager.h>
87 #include <vm/vm_zone.h>
90 #include <sys/thread2.h>
91 #include <sys/sysref2.h>
93 #include <machine/cputypes.h>
94 #include <machine/md_var.h>
95 #include <machine/specialreg.h>
96 #include <machine/smp.h>
97 #include <machine/globaldata.h>
98 #include <machine/pmap.h>
99 #include <machine/pmap_inval.h>
107 #define PMAP_KEEP_PDIRS
108 #ifndef PMAP_SHPGPERPROC
109 #define PMAP_SHPGPERPROC 200
112 #if defined(DIAGNOSTIC)
113 #define PMAP_DIAGNOSTIC
118 #if !defined(PMAP_DIAGNOSTIC)
119 #define PMAP_INLINE __inline
125 * Get PDEs and PTEs for user/kernel address space
127 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
128 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
130 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & VPTE_V) != 0)
131 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & VPTE_WIRED) != 0)
132 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & VPTE_M) != 0)
133 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & VPTE_A) != 0)
134 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & VPTE_V) != 0)
137 * Given a map and a machine independent protection code,
138 * convert to a vax protection code.
140 #define pte_prot(m, p) \
141 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
142 static int protection_codes[8];
144 struct pmap kernel_pmap;
145 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
147 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
149 static vm_object_t kptobj;
153 static uint64_t KPDphys; /* phys addr of kernel level 2 */
154 uint64_t KPDPphys; /* phys addr of kernel level 3 */
155 uint64_t KPML4phys; /* phys addr of kernel level 4 */
159 * Data for the pv entry allocation mechanism
161 static vm_zone_t pvzone;
162 static struct vm_zone pvzone_store;
163 static struct vm_object pvzone_obj;
164 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
165 static int pmap_pagedaemon_waken = 0;
166 static struct pv_entry *pvinit;
169 * All those kernel PT submaps that BSD is so fond of
171 pt_entry_t *CMAP1 = 0, *ptmmap;
173 static pt_entry_t *msgbufmap;
177 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
178 static pv_entry_t get_pv_entry (void);
179 static void i386_protection_init (void);
180 static __inline void pmap_clearbit (vm_page_t m, int bit);
182 static void pmap_remove_all (vm_page_t m);
183 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
185 static void pmap_remove_page (struct pmap *pmap, vm_offset_t va);
186 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
188 static boolean_t pmap_testbit (vm_page_t m, int bit);
189 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
190 vm_page_t mpte, vm_page_t m);
192 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
194 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
195 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
197 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
199 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
200 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t);
205 * Super fast pmap_pte routine best used when scanning the pv lists.
206 * This eliminates many course-grained invltlb calls. Note that many of
207 * the pv list scans are across different pmaps and it is very wasteful
208 * to do an entire invltlb when checking a single mapping.
210 * Should only be called while in a critical section.
213 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
216 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
218 return pmap_pte(pmap, va);
222 /* Return a non-clipped PD index for a given VA */
223 static __inline vm_pindex_t
224 pmap_pde_pindex(vm_offset_t va)
226 return va >> PDRSHIFT;
229 /* Return various clipped indexes for a given VA */
230 static __inline vm_pindex_t
231 pmap_pte_index(vm_offset_t va)
234 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
237 static __inline vm_pindex_t
238 pmap_pde_index(vm_offset_t va)
241 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
244 static __inline vm_pindex_t
245 pmap_pdpe_index(vm_offset_t va)
248 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
251 static __inline vm_pindex_t
252 pmap_pml4e_index(vm_offset_t va)
255 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
258 /* Return a pointer to the PML4 slot that corresponds to a VA */
259 static __inline pml4_entry_t *
260 pmap_pml4e(pmap_t pmap, vm_offset_t va)
263 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
266 /* Return a pointer to the PDP slot that corresponds to a VA */
267 static __inline pdp_entry_t *
268 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
272 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & VPTE_FRAME);
273 return (&pdpe[pmap_pdpe_index(va)]);
276 /* Return a pointer to the PDP slot that corresponds to a VA */
277 static __inline pdp_entry_t *
278 pmap_pdpe(pmap_t pmap, vm_offset_t va)
282 pml4e = pmap_pml4e(pmap, va);
283 if ((*pml4e & VPTE_V) == 0)
285 return (pmap_pml4e_to_pdpe(pml4e, va));
288 /* Return a pointer to the PD slot that corresponds to a VA */
289 static __inline pd_entry_t *
290 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
294 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & VPTE_FRAME);
295 return (&pde[pmap_pde_index(va)]);
298 /* Return a pointer to the PD slot that corresponds to a VA */
299 static __inline pd_entry_t *
300 pmap_pde(pmap_t pmap, vm_offset_t va)
304 pdpe = pmap_pdpe(pmap, va);
305 if (pdpe == NULL || (*pdpe & VPTE_V) == 0)
307 return (pmap_pdpe_to_pde(pdpe, va));
310 /* Return a pointer to the PT slot that corresponds to a VA */
311 static __inline pt_entry_t *
312 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
316 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & VPTE_FRAME);
317 return (&pte[pmap_pte_index(va)]);
320 /* Return a pointer to the PT slot that corresponds to a VA */
321 static __inline pt_entry_t *
322 pmap_pte(pmap_t pmap, vm_offset_t va)
326 pde = pmap_pde(pmap, va);
327 if (pde == NULL || (*pde & VPTE_V) == 0)
329 if ((*pde & VPTE_PS) != 0) /* compat with i386 pmap_pte() */
330 return ((pt_entry_t *)pde);
331 return (pmap_pde_to_pte(pde, va));
336 PMAP_INLINE pt_entry_t *
337 vtopte(vm_offset_t va)
339 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
341 return (PTmap + ((va >> PAGE_SHIFT) & mask));
344 static __inline pd_entry_t *
345 vtopde(vm_offset_t va)
347 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
349 return (PDmap + ((va >> PDRSHIFT) & mask));
352 static PMAP_INLINE pt_entry_t *
353 vtopte(vm_offset_t va)
356 x = pmap_pte(&kernel_pmap, va);
361 static __inline pd_entry_t *
362 vtopde(vm_offset_t va)
365 x = pmap_pde(&kernel_pmap, va);
372 allocpages(vm_paddr_t *firstaddr, int n)
378 bzero((void *)ret, n * PAGE_SIZE);
380 *firstaddr += n * PAGE_SIZE;
385 create_pagetables(vm_paddr_t *firstaddr, int64_t ptov_offset)
388 pml4_entry_t *KPML4virt;
389 pdp_entry_t *KPDPvirt;
392 int kpml4i = pmap_pml4e_index(ptov_offset);
393 int kpdpi = pmap_pdpe_index(ptov_offset);
397 KPML4phys = allocpages(firstaddr, 1);
398 KPDPphys = allocpages(firstaddr, NKPML4E);
399 KPDphys = allocpages(firstaddr, NKPDPE);
400 KPTphys = allocpages(firstaddr, NKPT);
402 KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
403 KPDPvirt = (pdp_entry_t *)PHYS_TO_DMAP(KPDPphys);
404 KPDvirt = (pd_entry_t *)PHYS_TO_DMAP(KPDphys);
405 KPTvirt = (pt_entry_t *)PHYS_TO_DMAP(KPTphys);
407 bzero(KPML4virt, 1 * PAGE_SIZE);
408 bzero(KPDPvirt, NKPML4E * PAGE_SIZE);
409 bzero(KPDvirt, NKPDPE * PAGE_SIZE);
410 bzero(KPTvirt, NKPT * PAGE_SIZE);
412 /* Now map the page tables at their location within PTmap */
413 for (i = 0; i < NKPT; i++) {
414 KPDvirt[i] = KPTphys + (i << PAGE_SHIFT);
415 KPDvirt[i] |= VPTE_R | VPTE_W | VPTE_V;
418 /* And connect up the PD to the PDP */
419 for (i = 0; i < NKPDPE; i++) {
420 KPDPvirt[i + kpdpi] = KPDphys + (i << PAGE_SHIFT);
421 KPDPvirt[i + kpdpi] |= VPTE_R | VPTE_W | VPTE_V;
424 /* And recursively map PML4 to itself in order to get PTmap */
425 KPML4virt[PML4PML4I] = KPML4phys;
426 KPML4virt[PML4PML4I] |= VPTE_R | VPTE_W | VPTE_V;
428 /* Connect the KVA slot up to the PML4 */
429 KPML4virt[kpml4i] = KPDPphys;
430 KPML4virt[kpml4i] |= VPTE_R | VPTE_W | VPTE_V;
434 * Bootstrap the system enough to run with virtual memory.
436 * On the i386 this is called after mapping has already been enabled
437 * and just syncs the pmap module with what has already been done.
438 * [We can't call it easily with mapping off since the kernel is not
439 * mapped with PA == VA, hence we would have to relocate every address
440 * from the linked base (virtual) address "KERNBASE" to the actual
441 * (physical) address starting relative to 0]
444 pmap_bootstrap(vm_paddr_t *firstaddr, int64_t ptov_offset)
450 * Create an initial set of page tables to run the kernel in.
452 create_pagetables(firstaddr, ptov_offset);
454 virtual_start = KvaStart + *firstaddr;
455 virtual_end = KvaEnd;
458 * Initialize protection array.
460 i386_protection_init();
463 * The kernel's pmap is statically allocated so we don't have to use
464 * pmap_create, which is unlikely to work correctly at this part of
465 * the boot sequence (XXX and which no longer exists).
467 kernel_pmap.pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
468 kernel_pmap.pm_count = 1;
469 kernel_pmap.pm_active = (cpumask_t)-1; /* don't allow deactivation */
470 TAILQ_INIT(&kernel_pmap.pm_pvlist);
474 * Reserve some special page table entries/VA space for temporary
477 #define SYSMAP(c, p, v, n) \
478 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
481 pte = pmap_pte(&kernel_pmap, va);
484 * CMAP1/CMAP2 are used for zeroing and copying pages.
486 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
492 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
496 * ptvmmap is used for reading arbitrary physical pages via
499 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
502 * msgbufp is used to map the system message buffer.
503 * XXX msgbufmap is not used.
505 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
506 atop(round_page(MSGBUF_SIZE)))
516 * Initialize the pmap module.
517 * Called by vm_init, to initialize any structures that the pmap
518 * system needs to map virtual memory.
519 * pmap_init has been enhanced to support in a fairly consistant
520 * way, discontiguous physical memory.
529 * object for kernel page table pages
531 /* JG I think the number can be arbitrary */
532 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
535 * Allocate memory for random pmap data structures. Includes the
539 for(i = 0; i < vm_page_array_size; i++) {
542 m = &vm_page_array[i];
543 TAILQ_INIT(&m->md.pv_list);
544 m->md.pv_list_count = 0;
548 * init the pv free list
550 initial_pvs = vm_page_array_size;
551 if (initial_pvs < MINPV)
553 pvzone = &pvzone_store;
554 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
555 initial_pvs * sizeof (struct pv_entry));
556 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
560 * Now it is safe to enable pv_table recording.
562 pmap_initialized = TRUE;
566 * Initialize the address space (zone) for the pv_entries. Set a
567 * high water mark so that the system can recover from excessive
568 * numbers of pv entries.
573 int shpgperproc = PMAP_SHPGPERPROC;
575 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
576 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
577 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
578 pv_entry_high_water = 9 * (pv_entry_max / 10);
579 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
583 /***************************************************
584 * Low level helper routines.....
585 ***************************************************/
588 * The modification bit is not tracked for any pages in this range. XXX
589 * such pages in this maps should always use pmap_k*() functions and not
592 * XXX User and kernel address spaces are independant for virtual kernels,
593 * this function only applies to the kernel pmap.
596 pmap_track_modified(pmap_t pmap, vm_offset_t va)
598 if (pmap != &kernel_pmap)
600 if ((va < clean_sva) || (va >= clean_eva))
607 * Extract the physical page address associated with the map/VA pair.
612 pmap_extract(pmap_t pmap, vm_offset_t va)
616 pd_entry_t pde, *pdep;
618 lwkt_gettoken(&vm_token);
620 pdep = pmap_pde(pmap, va);
624 if ((pde & VPTE_PS) != 0) {
626 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
628 pte = pmap_pde_to_pte(pdep, va);
629 rtval = (*pte & VPTE_FRAME) | (va & PAGE_MASK);
633 lwkt_reltoken(&vm_token);
638 * Routine: pmap_kextract
640 * Extract the physical page address associated
641 * kernel virtual address.
644 pmap_kextract(vm_offset_t va)
649 KKASSERT(va >= KvaStart && va < KvaEnd);
652 * The DMAP region is not included in [KvaStart, KvaEnd)
655 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
656 pa = DMAP_TO_PHYS(va);
662 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
665 * Beware of a concurrent promotion that changes the
666 * PDE at this point! For example, vtopte() must not
667 * be used to access the PTE because it would use the
668 * new PDE. It is, however, safe to use the old PDE
669 * because the page table page is preserved by the
672 pa = *pmap_pde_to_pte(&pde, va);
673 pa = (pa & VPTE_FRAME) | (va & PAGE_MASK);
681 /***************************************************
682 * Low level mapping routines.....
683 ***************************************************/
686 * Enter a mapping into kernel_pmap. Mappings created in this fashion
687 * are not managed. Mappings must be immediately accessible on all cpus.
689 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
690 * real pmap and handle related races before storing the new vpte.
693 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
698 KKASSERT(va >= KvaStart && va < KvaEnd);
699 npte = pa | VPTE_R | VPTE_W | VPTE_V;
702 pmap_inval_pte(pte, &kernel_pmap, va);
707 * Enter an unmanaged KVA mapping for the private use of the current
708 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
711 * It is illegal for the mapping to be accessed by other cpus unleess
712 * pmap_kenter_sync*() is called.
715 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
720 KKASSERT(va >= KvaStart && va < KvaEnd);
722 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
725 pmap_inval_pte_quick(pte, &kernel_pmap, va);
727 //cpu_invlpg((void *)va);
731 * Synchronize a kvm mapping originally made for the private use on
732 * some other cpu so it can be used on all cpus.
734 * XXX add MADV_RESYNC to improve performance.
737 pmap_kenter_sync(vm_offset_t va)
739 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
743 * Synchronize a kvm mapping originally made for the private use on
744 * some other cpu so it can be used on our cpu. Turns out to be the
745 * same madvise() call, because we have to sync the real pmaps anyway.
747 * XXX add MADV_RESYNC to improve performance.
750 pmap_kenter_sync_quick(vm_offset_t va)
752 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
756 * Remove an unmanaged mapping created with pmap_kenter*().
759 pmap_kremove(vm_offset_t va)
763 KKASSERT(va >= KvaStart && va < KvaEnd);
767 pmap_inval_pte(pte, &kernel_pmap, va);
772 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
773 * only with this cpu.
775 * Unfortunately because we optimize new entries by testing VPTE_V later
776 * on, we actually still have to synchronize with all the cpus. XXX maybe
777 * store a junk value and test against 0 in the other places instead?
780 pmap_kremove_quick(vm_offset_t va)
784 KKASSERT(va >= KvaStart && va < KvaEnd);
788 pmap_inval_pte(pte, &kernel_pmap, va); /* NOT _quick */
793 * Used to map a range of physical addresses into kernel
794 * virtual address space.
796 * For now, VM is already on, we only need to map the
800 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
802 return PHYS_TO_DMAP(start);
807 * Map a set of unmanaged VM pages into KVM.
810 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
814 end_va = va + count * PAGE_SIZE;
815 KKASSERT(va >= KvaStart && end_va < KvaEnd);
817 while (va < end_va) {
822 pmap_inval_pte(pte, &kernel_pmap, va);
823 *pte = VM_PAGE_TO_PHYS(*m) | VPTE_R | VPTE_W | VPTE_V;
830 * Undo the effects of pmap_qenter*().
833 pmap_qremove(vm_offset_t va, int count)
837 end_va = va + count * PAGE_SIZE;
838 KKASSERT(va >= KvaStart && end_va < KvaEnd);
840 while (va < end_va) {
845 pmap_inval_pte(pte, &kernel_pmap, va);
852 * This routine works like vm_page_lookup() but also blocks as long as the
853 * page is busy. This routine does not busy the page it returns.
855 * Unless the caller is managing objects whos pages are in a known state,
856 * the call should be made with a critical section held so the page's object
857 * association remains valid on return.
860 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
865 m = vm_page_lookup(object, pindex);
866 } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
872 * Create a new thread and optionally associate it with a (new) process.
873 * NOTE! the new thread's cpu may not equal the current cpu.
876 pmap_init_thread(thread_t td)
878 /* enforce pcb placement */
879 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
880 td->td_savefpu = &td->td_pcb->pcb_save;
881 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */
885 * This routine directly affects the fork perf for a process.
888 pmap_init_proc(struct proc *p)
893 * Dispose the UPAGES for a process that has exited.
894 * This routine directly impacts the exit perf of a process.
897 pmap_dispose_proc(struct proc *p)
899 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
902 /***************************************************
903 * Page table page management routines.....
904 ***************************************************/
906 static __inline int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va,
910 * This routine unholds page table pages, and if the hold count
911 * drops to zero, then it decrements the wire count.
913 * We must recheck that this is the last hold reference after busy-sleeping
917 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
919 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
921 KASSERT(m->queue == PQ_NONE,
922 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
924 if (m->hold_count == 1) {
926 * Unmap the page table page.
930 /* pmap_inval_add(info, pmap, -1); */
932 if (m->pindex >= (NUPDE + NUPDPE)) {
935 pml4 = pmap_pml4e(pmap, va);
937 } else if (m->pindex >= NUPDE) {
940 pdp = pmap_pdpe(pmap, va);
945 pd = pmap_pde(pmap, va);
949 KKASSERT(pmap->pm_stats.resident_count > 0);
950 --pmap->pm_stats.resident_count;
952 if (pmap->pm_ptphint == m)
953 pmap->pm_ptphint = NULL;
955 if (m->pindex < NUPDE) {
956 /* We just released a PT, unhold the matching PD */
959 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & VPTE_FRAME);
960 pmap_unwire_pte_hold(pmap, va, pdpg);
962 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
963 /* We just released a PD, unhold the matching PDP */
966 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & VPTE_FRAME);
967 pmap_unwire_pte_hold(pmap, va, pdppg);
971 * This was our last hold, the page had better be unwired
972 * after we decrement wire_count.
974 * FUTURE NOTE: shared page directory page could result in
975 * multiple wire counts.
979 KKASSERT(m->wire_count == 0);
980 --vmstats.v_wire_count;
981 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
983 vm_page_free_zero(m);
986 KKASSERT(m->hold_count > 1);
993 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
995 KKASSERT(m->hold_count > 0);
996 if (m->hold_count > 1) {
1000 return _pmap_unwire_pte_hold(pmap, va, m);
1005 * After removing a page table entry, this routine is used to
1006 * conditionally free the page, and manage the hold/wire counts.
1009 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1011 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1012 vm_pindex_t ptepindex;
1016 * page table pages in the kernel_pmap are not managed.
1018 if (pmap == &kernel_pmap)
1020 ptepindex = pmap_pde_pindex(va);
1021 if (pmap->pm_ptphint &&
1022 (pmap->pm_ptphint->pindex == ptepindex)) {
1023 mpte = pmap->pm_ptphint;
1025 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1026 pmap->pm_ptphint = mpte;
1030 return pmap_unwire_pte_hold(pmap, va, mpte);
1034 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
1035 * just dummy it up so it works well enough for fork().
1037 * In DragonFly, process pmaps may only be used to manipulate user address
1038 * space, never kernel address space.
1041 pmap_pinit0(struct pmap *pmap)
1047 * Initialize a preallocated and zeroed pmap structure,
1048 * such as one in a vmspace structure.
1051 pmap_pinit(struct pmap *pmap)
1056 * No need to allocate page table space yet but we do need a valid
1057 * page directory table.
1059 if (pmap->pm_pml4 == NULL) {
1061 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1065 * Allocate an object for the ptes
1067 if (pmap->pm_pteobj == NULL)
1068 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1071 * Allocate the page directory page, unless we already have
1072 * one cached. If we used the cached page the wire_count will
1073 * already be set appropriately.
1075 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1076 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1077 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1078 pmap->pm_pdirm = ptdpg;
1079 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1080 ptdpg->valid = VM_PAGE_BITS_ALL;
1081 if (ptdpg->wire_count == 0)
1082 ++vmstats.v_wire_count;
1083 ptdpg->wire_count = 1;
1084 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1086 if ((ptdpg->flags & PG_ZERO) == 0)
1087 bzero(pmap->pm_pml4, PAGE_SIZE);
1090 pmap->pm_active = 0;
1091 pmap->pm_ptphint = NULL;
1092 TAILQ_INIT(&pmap->pm_pvlist);
1093 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1094 pmap->pm_stats.resident_count = 1;
1098 * Clean up a pmap structure so it can be physically freed. This routine
1099 * is called by the vmspace dtor function. A great deal of pmap data is
1100 * left passively mapped to improve vmspace management so we have a bit
1101 * of cleanup work to do here.
1106 pmap_puninit(pmap_t pmap)
1110 KKASSERT(pmap->pm_active == 0);
1111 lwkt_gettoken(&vm_token);
1112 if ((p = pmap->pm_pdirm) != NULL) {
1113 KKASSERT(pmap->pm_pml4 != NULL);
1114 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1116 vmstats.v_wire_count--;
1117 KKASSERT((p->flags & PG_BUSY) == 0);
1119 vm_page_free_zero(p);
1120 pmap->pm_pdirm = NULL;
1122 lwkt_reltoken(&vm_token);
1123 if (pmap->pm_pml4) {
1124 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1125 pmap->pm_pml4 = NULL;
1127 if (pmap->pm_pteobj) {
1128 vm_object_deallocate(pmap->pm_pteobj);
1129 pmap->pm_pteobj = NULL;
1134 * Wire in kernel global address entries. To avoid a race condition
1135 * between pmap initialization and pmap_growkernel, this procedure
1136 * adds the pmap to the master list (which growkernel scans to update),
1137 * then copies the template.
1139 * In a virtual kernel there are no kernel global address entries.
1144 pmap_pinit2(struct pmap *pmap)
1147 lwkt_gettoken(&vm_token);
1148 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1149 lwkt_reltoken(&vm_token);
1154 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1155 * 0 on failure (if the procedure had to sleep).
1157 * When asked to remove the page directory page itself, we actually just
1158 * leave it cached so we do not have to incur the SMP inval overhead of
1159 * removing the kernel mapping. pmap_puninit() will take care of it.
1162 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1165 * This code optimizes the case of freeing non-busy
1166 * page-table pages. Those pages are zero now, and
1167 * might as well be placed directly into the zero queue.
1169 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1175 * Remove the page table page from the processes address space.
1177 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1179 * We are the pml4 table itself.
1181 /* XXX anything to do here? */
1182 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1184 * We are a PDP page.
1185 * We look for the PML4 entry that points to us.
1187 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1188 KKASSERT(m4 != NULL);
1189 pml4_entry_t *pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1190 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1191 KKASSERT(pml4[idx] != 0);
1194 /* JG What about wire_count? */
1195 } else if (p->pindex >= NUPDE) {
1198 * We look for the PDP entry that points to us.
1200 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1201 KKASSERT(m3 != NULL);
1202 pdp_entry_t *pdp = (pdp_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1203 int idx = (p->pindex - NUPDE) % NPDPEPG;
1204 KKASSERT(pdp[idx] != 0);
1207 /* JG What about wire_count? */
1209 /* We are a PT page.
1210 * We look for the PD entry that points to us.
1212 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1213 KKASSERT(m2 != NULL);
1214 pd_entry_t *pd = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1215 int idx = p->pindex % NPDEPG;
1218 /* JG What about wire_count? */
1220 KKASSERT(pmap->pm_stats.resident_count > 0);
1221 --pmap->pm_stats.resident_count;
1223 if (p->hold_count) {
1224 panic("pmap_release: freeing held page table page");
1226 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1227 pmap->pm_ptphint = NULL;
1230 * We leave the top-level page table page cached, wired, and mapped in
1231 * the pmap until the dtor function (pmap_puninit()) gets called.
1232 * However, still clean it up so we can set PG_ZERO.
1234 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1235 bzero(pmap->pm_pml4, PAGE_SIZE);
1236 vm_page_flag_set(p, PG_ZERO);
1241 vmstats.v_wire_count--;
1242 /* JG eventually revert to using vm_page_free_zero() */
1249 * this routine is called if the page table page is not
1253 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1255 vm_page_t m, pdppg, pdpg;
1258 * Find or fabricate a new pagetable page
1260 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1261 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1263 if ((m->flags & PG_ZERO) == 0) {
1264 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1267 KASSERT(m->queue == PQ_NONE,
1268 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1271 * Increment the hold count for the page we will be returning to
1276 if (m->wire_count == 0)
1277 vmstats.v_wire_count++;
1281 * Map the pagetable page into the process address space, if
1282 * it isn't already there.
1285 ++pmap->pm_stats.resident_count;
1287 if (ptepindex >= (NUPDE + NUPDPE)) {
1289 vm_pindex_t pml4index;
1291 /* Wire up a new PDP page */
1292 pml4index = ptepindex - (NUPDE + NUPDPE);
1293 pml4 = &pmap->pm_pml4[pml4index];
1294 *pml4 = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1296 } else if (ptepindex >= NUPDE) {
1297 vm_pindex_t pml4index;
1298 vm_pindex_t pdpindex;
1302 /* Wire up a new PD page */
1303 pdpindex = ptepindex - NUPDE;
1304 pml4index = pdpindex >> NPML4EPGSHIFT;
1306 pml4 = &pmap->pm_pml4[pml4index];
1307 if ((*pml4 & VPTE_V) == 0) {
1308 /* Have to allocate a new PDP page, recurse */
1309 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1316 /* Add reference to the PDP page */
1317 pdppg = PHYS_TO_VM_PAGE(*pml4 & VPTE_FRAME);
1318 pdppg->hold_count++;
1320 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1322 /* Now find the pdp page */
1323 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1324 KKASSERT(*pdp == 0); /* JG DEBUG64 */
1325 *pdp = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1328 vm_pindex_t pml4index;
1329 vm_pindex_t pdpindex;
1334 /* Wire up a new PT page */
1335 pdpindex = ptepindex >> NPDPEPGSHIFT;
1336 pml4index = pdpindex >> NPML4EPGSHIFT;
1338 /* First, find the pdp and check that its valid. */
1339 pml4 = &pmap->pm_pml4[pml4index];
1340 if ((*pml4 & VPTE_V) == 0) {
1341 /* We miss a PDP page. We ultimately need a PD page.
1342 * Recursively allocating a PD page will allocate
1343 * the missing PDP page and will also allocate
1344 * the PD page we need.
1346 /* Have to allocate a new PD page, recurse */
1347 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1353 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1354 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1356 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1357 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1358 if ((*pdp & VPTE_V) == 0) {
1359 /* Have to allocate a new PD page, recurse */
1360 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1367 /* Add reference to the PD page */
1368 pdpg = PHYS_TO_VM_PAGE(*pdp & VPTE_FRAME);
1372 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & VPTE_FRAME);
1374 /* Now we know where the page directory page is */
1375 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1376 KKASSERT(*pd == 0); /* JG DEBUG64 */
1377 *pd = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1382 * Set the page table hint
1384 pmap->pm_ptphint = m;
1386 m->valid = VM_PAGE_BITS_ALL;
1387 vm_page_flag_clear(m, PG_ZERO);
1388 vm_page_flag_set(m, PG_MAPPED);
1395 * Determine the page table page required to access the VA in the pmap
1396 * and allocate it if necessary. Return a held vm_page_t for the page.
1398 * Only used with user pmaps.
1401 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1403 vm_pindex_t ptepindex;
1408 * Calculate pagetable page index
1410 ptepindex = pmap_pde_pindex(va);
1413 * Get the page directory entry
1415 pd = pmap_pde(pmap, va);
1418 * This supports switching from a 2MB page to a
1421 if (pd != NULL && (*pd & (VPTE_PS | VPTE_V)) == (VPTE_PS | VPTE_V)) {
1422 panic("no promotion/demotion yet");
1430 * If the page table page is mapped, we just increment the
1431 * hold count, and activate it.
1433 if (pd != NULL && (*pd & VPTE_V) != 0) {
1434 /* YYY hint is used here on i386 */
1435 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1436 pmap->pm_ptphint = m;
1441 * Here if the pte page isn't mapped, or if it has been deallocated.
1443 return _pmap_allocpte(pmap, ptepindex);
1447 /***************************************************
1448 * Pmap allocation/deallocation routines.
1449 ***************************************************/
1452 * Release any resources held by the given physical map.
1453 * Called when a pmap initialized by pmap_pinit is being released.
1454 * Should only be called if the map contains no valid mappings.
1458 static int pmap_release_callback(struct vm_page *p, void *data);
1461 pmap_release(struct pmap *pmap)
1463 vm_object_t object = pmap->pm_pteobj;
1464 struct rb_vm_page_scan_info info;
1466 KKASSERT(pmap != &kernel_pmap);
1468 #if defined(DIAGNOSTIC)
1469 if (object->ref_count != 1)
1470 panic("pmap_release: pteobj reference count != 1");
1474 info.object = object;
1476 lwkt_gettoken(&vm_token);
1477 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1484 info.limit = object->generation;
1486 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1487 pmap_release_callback, &info);
1488 if (info.error == 0 && info.mpte) {
1489 if (!pmap_release_free_page(pmap, info.mpte))
1493 } while (info.error);
1494 lwkt_reltoken(&vm_token);
1498 pmap_release_callback(struct vm_page *p, void *data)
1500 struct rb_vm_page_scan_info *info = data;
1502 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1506 if (!pmap_release_free_page(info->pmap, p)) {
1510 if (info->object->generation != info->limit) {
1518 * Grow the number of kernel page table entries, if needed.
1523 pmap_growkernel(vm_offset_t addr)
1526 vm_offset_t ptppaddr;
1528 pd_entry_t *pde, newpdir;
1532 lwkt_gettoken(&vm_token);
1533 if (kernel_vm_end == 0) {
1534 kernel_vm_end = KvaStart;
1536 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & VPTE_V) != 0) {
1537 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1539 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1540 kernel_vm_end = kernel_map.max_offset;
1545 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1546 if (addr - 1 >= kernel_map.max_offset)
1547 addr = kernel_map.max_offset;
1548 while (kernel_vm_end < addr) {
1549 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1551 /* We need a new PDP entry */
1552 nkpg = vm_page_alloc(kptobj, nkpt,
1553 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1554 | VM_ALLOC_INTERRUPT);
1556 panic("pmap_growkernel: no memory to grow kernel");
1557 paddr = VM_PAGE_TO_PHYS(nkpg);
1558 if ((nkpg->flags & PG_ZERO) == 0)
1559 pmap_zero_page(paddr);
1560 vm_page_flag_clear(nkpg, PG_ZERO);
1561 newpdp = (pdp_entry_t)
1562 (paddr | VPTE_V | VPTE_R | VPTE_W | VPTE_A | VPTE_M);
1563 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1565 continue; /* try again */
1567 if ((*pde & VPTE_V) != 0) {
1568 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1569 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1570 kernel_vm_end = kernel_map.max_offset;
1577 * This index is bogus, but out of the way
1579 nkpg = vm_page_alloc(kptobj, nkpt,
1580 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1582 panic("pmap_growkernel: no memory to grow kernel");
1585 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1586 pmap_zero_page(ptppaddr);
1587 vm_page_flag_clear(nkpg, PG_ZERO);
1588 newpdir = (pd_entry_t) (ptppaddr | VPTE_V | VPTE_R | VPTE_W | VPTE_A | VPTE_M);
1589 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1592 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1593 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1594 kernel_vm_end = kernel_map.max_offset;
1598 lwkt_reltoken(&vm_token);
1603 * Retire the given physical map from service. Should only be called
1604 * if the map contains no valid mappings.
1609 pmap_destroy(pmap_t pmap)
1614 lwkt_gettoken(&vm_token);
1615 if (--pmap->pm_count == 0) {
1617 panic("destroying a pmap is not yet implemented");
1619 lwkt_reltoken(&vm_token);
1623 * Add a reference to the specified pmap.
1628 pmap_reference(pmap_t pmap)
1631 lwkt_gettoken(&vm_token);
1633 lwkt_reltoken(&vm_token);
1637 /************************************************************************
1638 * VMSPACE MANAGEMENT *
1639 ************************************************************************
1641 * The VMSPACE management we do in our virtual kernel must be reflected
1642 * in the real kernel. This is accomplished by making vmspace system
1643 * calls to the real kernel.
1646 cpu_vmspace_alloc(struct vmspace *vm)
1652 #define USER_SIZE (VM_MAX_USER_ADDRESS - VM_MIN_USER_ADDRESS)
1654 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
1655 panic("vmspace_create() failed");
1657 rp = vmspace_mmap(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1658 PROT_READ|PROT_WRITE,
1659 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
1661 if (rp == MAP_FAILED)
1662 panic("vmspace_mmap: failed");
1663 vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1665 vpte = VM_PAGE_TO_PHYS(vmspace_pmap(vm)->pm_pdirm) | VPTE_R | VPTE_W | VPTE_V;
1666 r = vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1669 panic("vmspace_mcontrol: failed");
1673 cpu_vmspace_free(struct vmspace *vm)
1675 if (vmspace_destroy(&vm->vm_pmap) < 0)
1676 panic("vmspace_destroy() failed");
1679 /***************************************************
1680 * page management routines.
1681 ***************************************************/
1684 * free the pv_entry back to the free list. This function may be
1685 * called from an interrupt.
1687 static __inline void
1688 free_pv_entry(pv_entry_t pv)
1691 KKASSERT(pv_entry_count >= 0);
1696 * get a new pv_entry, allocating a block from the system
1697 * when needed. This function may be called from an interrupt.
1703 if (pv_entry_high_water &&
1704 (pv_entry_count > pv_entry_high_water) &&
1705 (pmap_pagedaemon_waken == 0)) {
1706 pmap_pagedaemon_waken = 1;
1707 wakeup(&vm_pages_needed);
1709 return zalloc(pvzone);
1713 * This routine is very drastic, but can save the system
1723 static int warningdone=0;
1725 if (pmap_pagedaemon_waken == 0)
1727 lwkt_gettoken(&vm_token);
1728 pmap_pagedaemon_waken = 0;
1730 if (warningdone < 5) {
1731 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1735 for(i = 0; i < vm_page_array_size; i++) {
1736 m = &vm_page_array[i];
1737 if (m->wire_count || m->hold_count || m->busy ||
1738 (m->flags & PG_BUSY))
1742 lwkt_reltoken(&vm_token);
1747 * If it is the first entry on the list, it is actually
1748 * in the header and we must copy the following entry up
1749 * to the header. Otherwise we must search the list for
1750 * the entry. In either case we free the now unused entry.
1753 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1759 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1760 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1761 if (pmap == pv->pv_pmap && va == pv->pv_va)
1765 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1766 if (va == pv->pv_va)
1772 * Note that pv_ptem is NULL if the page table page itself is not
1773 * managed, even if the page being removed IS managed.
1776 /* JGXXX When can 'pv' be NULL? */
1778 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1779 m->md.pv_list_count--;
1780 KKASSERT(m->md.pv_list_count >= 0);
1781 if (TAILQ_EMPTY(&m->md.pv_list))
1782 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1783 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1784 ++pmap->pm_generation;
1785 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1793 * Create a pv entry for page at pa for (pmap, va). If the page table page
1794 * holding the VA is managed, mpte will be non-NULL.
1797 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1802 pv = get_pv_entry();
1807 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1808 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1809 m->md.pv_list_count++;
1815 * pmap_remove_pte: do the things to unmap a page in a process
1818 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1823 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1824 if (oldpte & VPTE_WIRED)
1825 --pmap->pm_stats.wired_count;
1826 KKASSERT(pmap->pm_stats.wired_count >= 0);
1830 * Machines that don't support invlpg, also don't support
1831 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1835 cpu_invlpg((void *)va);
1837 KKASSERT(pmap->pm_stats.resident_count > 0);
1838 --pmap->pm_stats.resident_count;
1839 if (oldpte & VPTE_MANAGED) {
1840 m = PHYS_TO_VM_PAGE(oldpte);
1841 if (oldpte & VPTE_M) {
1842 #if defined(PMAP_DIAGNOSTIC)
1843 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1845 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
1849 if (pmap_track_modified(pmap, va))
1852 if (oldpte & VPTE_A)
1853 vm_page_flag_set(m, PG_REFERENCED);
1854 return pmap_remove_entry(pmap, m, va);
1856 return pmap_unuse_pt(pmap, va, NULL);
1865 * Remove a single page from a process address space.
1867 * This function may not be called from an interrupt if the pmap is
1871 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1875 pte = pmap_pte(pmap, va);
1878 if ((*pte & VPTE_V) == 0)
1880 pmap_remove_pte(pmap, pte, va);
1884 * Remove the given range of addresses from the specified map.
1886 * It is assumed that the start and end are properly rounded to
1889 * This function may not be called from an interrupt if the pmap is
1895 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1897 vm_offset_t va_next;
1898 pml4_entry_t *pml4e;
1900 pd_entry_t ptpaddr, *pde;
1906 lwkt_gettoken(&vm_token);
1907 KKASSERT(pmap->pm_stats.resident_count >= 0);
1908 if (pmap->pm_stats.resident_count == 0) {
1909 lwkt_reltoken(&vm_token);
1914 * special handling of removing one page. a very
1915 * common operation and easy to short circuit some
1918 if (sva + PAGE_SIZE == eva) {
1919 pde = pmap_pde(pmap, sva);
1920 if (pde && (*pde & VPTE_PS) == 0) {
1921 pmap_remove_page(pmap, sva);
1922 lwkt_reltoken(&vm_token);
1927 for (; sva < eva; sva = va_next) {
1928 pml4e = pmap_pml4e(pmap, sva);
1929 if ((*pml4e & VPTE_V) == 0) {
1930 va_next = (sva + NBPML4) & ~PML4MASK;
1936 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
1937 if ((*pdpe & VPTE_V) == 0) {
1938 va_next = (sva + NBPDP) & ~PDPMASK;
1945 * Calculate index for next page table.
1947 va_next = (sva + NBPDR) & ~PDRMASK;
1951 pde = pmap_pdpe_to_pde(pdpe, sva);
1955 * Weed out invalid mappings.
1961 * Check for large page.
1963 if ((ptpaddr & VPTE_PS) != 0) {
1964 /* JG FreeBSD has more complex treatment here */
1965 KKASSERT(*pde != 0);
1966 pmap_inval_pde(pde, pmap, sva);
1967 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1972 * Limit our scan to either the end of the va represented
1973 * by the current page table page, or to the end of the
1974 * range being removed.
1980 * NOTE: pmap_remove_pte() can block.
1982 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1986 if (pmap_remove_pte(pmap, pte, sva))
1990 lwkt_reltoken(&vm_token);
1994 * Removes this physical page from all physical maps in which it resides.
1995 * Reflects back modify bits to the pager.
1997 * This routine may not be called from an interrupt.
2003 pmap_remove_all(vm_page_t m)
2005 pt_entry_t *pte, tpte;
2008 #if defined(PMAP_DIAGNOSTIC)
2010 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
2013 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
2014 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
2019 lwkt_gettoken(&vm_token);
2020 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2021 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2022 --pv->pv_pmap->pm_stats.resident_count;
2024 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2025 KKASSERT(pte != NULL);
2027 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
2028 if (tpte & VPTE_WIRED)
2029 pv->pv_pmap->pm_stats.wired_count--;
2030 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
2033 vm_page_flag_set(m, PG_REFERENCED);
2036 * Update the vm_page_t clean and reference bits.
2038 if (tpte & VPTE_M) {
2039 #if defined(PMAP_DIAGNOSTIC)
2040 if (pmap_nw_modified(tpte)) {
2042 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2046 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
2049 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2050 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2051 ++pv->pv_pmap->pm_generation;
2052 m->md.pv_list_count--;
2053 KKASSERT(m->md.pv_list_count >= 0);
2054 if (TAILQ_EMPTY(&m->md.pv_list))
2055 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2056 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2059 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2060 lwkt_reltoken(&vm_token);
2065 * Set the physical protection on the specified range of this map
2068 * This function may not be called from an interrupt if the map is
2069 * not the kernel_pmap.
2074 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2076 vm_offset_t va_next;
2077 pml4_entry_t *pml4e;
2079 pd_entry_t ptpaddr, *pde;
2082 /* JG review for NX */
2087 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2088 pmap_remove(pmap, sva, eva);
2092 if (prot & VM_PROT_WRITE)
2095 lwkt_gettoken(&vm_token);
2097 for (; sva < eva; sva = va_next) {
2099 pml4e = pmap_pml4e(pmap, sva);
2100 if ((*pml4e & VPTE_V) == 0) {
2101 va_next = (sva + NBPML4) & ~PML4MASK;
2107 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2108 if ((*pdpe & VPTE_V) == 0) {
2109 va_next = (sva + NBPDP) & ~PDPMASK;
2115 va_next = (sva + NBPDR) & ~PDRMASK;
2119 pde = pmap_pdpe_to_pde(pdpe, sva);
2123 * Check for large page.
2125 if ((ptpaddr & VPTE_PS) != 0) {
2127 pmap_clean_pde(pde, pmap, sva);
2128 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2133 * Weed out invalid mappings. Note: we assume that the page
2134 * directory table is always allocated, and in kernel virtual.
2142 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2148 * Clean managed pages and also check the accessed
2149 * bit. Just remove write perms for unmanaged
2150 * pages. Be careful of races, turning off write
2151 * access will force a fault rather then setting
2152 * the modified bit at an unexpected time.
2154 if (*pte & VPTE_MANAGED) {
2155 pbits = pmap_clean_pte(pte, pmap, sva);
2157 if (pbits & VPTE_A) {
2158 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2159 vm_page_flag_set(m, PG_REFERENCED);
2160 atomic_clear_long(pte, VPTE_A);
2162 if (pbits & VPTE_M) {
2163 if (pmap_track_modified(pmap, sva)) {
2165 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2170 pbits = pmap_setro_pte(pte, pmap, sva);
2174 lwkt_reltoken(&vm_token);
2178 * Enter a managed page into a pmap. If the page is not wired related pmap
2179 * data can be destroyed at any time for later demand-operation.
2181 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
2182 * specified protection, and wire the mapping if requested.
2184 * NOTE: This routine may not lazy-evaluate or lose information. The
2185 * page must actually be inserted into the given map NOW.
2187 * NOTE: When entering a page at a KVA address, the pmap must be the
2193 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2200 pt_entry_t origpte, newpte;
2206 va = trunc_page(va);
2208 lwkt_gettoken(&vm_token);
2211 * Get the page table page. The kernel_pmap's page table pages
2212 * are preallocated and have no associated vm_page_t.
2214 if (pmap == &kernel_pmap)
2217 mpte = pmap_allocpte(pmap, va);
2219 pde = pmap_pde(pmap, va);
2220 if (pde != NULL && (*pde & VPTE_V) != 0) {
2221 if ((*pde & VPTE_PS) != 0)
2222 panic("pmap_enter: attempted pmap_enter on 2MB page");
2223 pte = pmap_pde_to_pte(pde, va);
2225 panic("pmap_enter: invalid page directory va=%#lx", va);
2228 KKASSERT(pte != NULL);
2230 * Deal with races on the original mapping (though don't worry
2231 * about VPTE_A races) by cleaning it. This will force a fault
2232 * if an attempt is made to write to the page.
2234 pa = VM_PAGE_TO_PHYS(m);
2235 origpte = pmap_clean_pte(pte, pmap, va);
2236 opa = origpte & VPTE_FRAME;
2238 if (origpte & VPTE_PS)
2239 panic("pmap_enter: attempted pmap_enter on 2MB page");
2242 * Mapping has not changed, must be protection or wiring change.
2244 if (origpte && (opa == pa)) {
2246 * Wiring change, just update stats. We don't worry about
2247 * wiring PT pages as they remain resident as long as there
2248 * are valid mappings in them. Hence, if a user page is wired,
2249 * the PT page will be also.
2251 if (wired && ((origpte & VPTE_WIRED) == 0))
2252 ++pmap->pm_stats.wired_count;
2253 else if (!wired && (origpte & VPTE_WIRED))
2254 --pmap->pm_stats.wired_count;
2257 * Remove the extra pte reference. Note that we cannot
2258 * optimize the RO->RW case because we have adjusted the
2259 * wiring count above and may need to adjust the wiring
2266 * We might be turning off write access to the page,
2267 * so we go ahead and sense modify status.
2269 if (origpte & VPTE_MANAGED) {
2270 if ((origpte & VPTE_M) &&
2271 pmap_track_modified(pmap, va)) {
2273 om = PHYS_TO_VM_PAGE(opa);
2277 KKASSERT(m->flags & PG_MAPPED);
2282 * Mapping has changed, invalidate old range and fall through to
2283 * handle validating new mapping.
2287 err = pmap_remove_pte(pmap, pte, va);
2289 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2293 * Enter on the PV list if part of our managed memory. Note that we
2294 * raise IPL while manipulating pv_table since pmap_enter can be
2295 * called at interrupt time.
2297 if (pmap_initialized &&
2298 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2299 pmap_insert_entry(pmap, va, mpte, m);
2301 vm_page_flag_set(m, PG_MAPPED);
2305 * Increment counters
2307 ++pmap->pm_stats.resident_count;
2309 pmap->pm_stats.wired_count++;
2313 * Now validate mapping with desired protection/wiring.
2315 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | VPTE_V);
2318 newpte |= VPTE_WIRED;
2319 if (pmap != &kernel_pmap)
2323 * If the mapping or permission bits are different from the
2324 * (now cleaned) original pte, an update is needed. We've
2325 * already downgraded or invalidated the page so all we have
2326 * to do now is update the bits.
2328 * XXX should we synchronize RO->RW changes to avoid another
2331 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
2332 *pte = newpte | VPTE_A;
2333 if (newpte & VPTE_W)
2334 vm_page_flag_set(m, PG_WRITEABLE);
2336 KKASSERT((newpte & VPTE_MANAGED) == 0 || (m->flags & PG_MAPPED));
2337 lwkt_reltoken(&vm_token);
2341 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2343 * Currently this routine may only be used on user pmaps, not kernel_pmap.
2348 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2353 vm_pindex_t ptepindex;
2356 KKASSERT(pmap != &kernel_pmap);
2358 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
2361 * Calculate pagetable page index
2363 ptepindex = pmap_pde_pindex(va);
2365 lwkt_gettoken(&vm_token);
2369 * Get the page directory entry
2371 ptepa = pmap_pde(pmap, va);
2374 * If the page table page is mapped, we just increment
2375 * the hold count, and activate it.
2377 if (ptepa && (*ptepa & VPTE_V) != 0) {
2378 if (*ptepa & VPTE_PS)
2379 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2380 if (pmap->pm_ptphint &&
2381 (pmap->pm_ptphint->pindex == ptepindex)) {
2382 mpte = pmap->pm_ptphint;
2384 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2385 pmap->pm_ptphint = mpte;
2390 mpte = _pmap_allocpte(pmap, ptepindex);
2392 } while (mpte == NULL);
2395 * Ok, now that the page table page has been validated, get the pte.
2396 * If the pte is already mapped undo mpte's hold_count and
2399 pte = pmap_pte(pmap, va);
2400 if (*pte & VPTE_V) {
2401 KKASSERT(mpte != NULL);
2402 pmap_unwire_pte_hold(pmap, va, mpte);
2403 pa = VM_PAGE_TO_PHYS(m);
2404 KKASSERT(((*pte ^ pa) & VPTE_FRAME) == 0);
2405 lwkt_reltoken(&vm_token);
2410 * Enter on the PV list if part of our managed memory
2412 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2413 pmap_insert_entry(pmap, va, mpte, m);
2414 vm_page_flag_set(m, PG_MAPPED);
2418 * Increment counters
2420 ++pmap->pm_stats.resident_count;
2422 pa = VM_PAGE_TO_PHYS(m);
2425 * Now validate mapping with RO protection
2427 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2428 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2430 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2431 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2432 /*pmap_inval_flush(&info); don't need for vkernel */
2433 lwkt_reltoken(&vm_token);
2437 * Make a temporary mapping for a physical address. This is only intended
2438 * to be used for panic dumps.
2441 pmap_kenter_temporary(vm_paddr_t pa, int i)
2443 pmap_kenter(crashdumpmap + (i * PAGE_SIZE), pa);
2444 return ((void *)crashdumpmap);
2447 #define MAX_INIT_PT (96)
2450 * This routine preloads the ptes for a given object into the specified pmap.
2451 * This eliminates the blast of soft faults on process startup and
2452 * immediately after an mmap.
2456 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2459 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2460 vm_object_t object, vm_pindex_t pindex,
2461 vm_size_t size, int limit)
2463 struct rb_vm_page_scan_info info;
2468 * We can't preinit if read access isn't set or there is no pmap
2471 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2475 * We can't preinit if the pmap is not the current pmap
2477 lp = curthread->td_lwp;
2478 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2481 psize = x86_64_btop(size);
2483 if ((object->type != OBJT_VNODE) ||
2484 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2485 (object->resident_page_count > MAX_INIT_PT))) {
2489 if (psize + pindex > object->size) {
2490 if (object->size < pindex)
2492 psize = object->size - pindex;
2499 * Use a red-black scan to traverse the requested range and load
2500 * any valid pages found into the pmap.
2502 * We cannot safely scan the object's memq unless we are in a
2503 * critical section since interrupts can remove pages from objects.
2505 info.start_pindex = pindex;
2506 info.end_pindex = pindex + psize - 1;
2513 lwkt_gettoken(&vm_token);
2514 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2515 pmap_object_init_pt_callback, &info);
2516 lwkt_reltoken(&vm_token);
2522 pmap_object_init_pt_callback(vm_page_t p, void *data)
2524 struct rb_vm_page_scan_info *info = data;
2525 vm_pindex_t rel_index;
2527 * don't allow an madvise to blow away our really
2528 * free pages allocating pv entries.
2530 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2531 vmstats.v_free_count < vmstats.v_free_reserved) {
2534 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2535 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2536 if ((p->queue - p->pc) == PQ_CACHE)
2537 vm_page_deactivate(p);
2539 rel_index = p->pindex - info->start_pindex;
2540 pmap_enter_quick(info->pmap,
2541 info->addr + x86_64_ptob(rel_index), p);
2548 * Return TRUE if the pmap is in shape to trivially
2549 * pre-fault the specified address.
2551 * Returns FALSE if it would be non-trivial or if a
2552 * pte is already loaded into the slot.
2557 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2563 lwkt_gettoken(&vm_token);
2564 pde = pmap_pde(pmap, addr);
2565 if (pde == NULL || *pde == 0) {
2568 pte = pmap_pde_to_pte(pde, addr);
2569 ret = (*pte) ? 0 : 1;
2571 lwkt_reltoken(&vm_token);
2576 * Change the wiring attribute for a map/virtual-address pair.
2578 * The mapping must already exist in the pmap.
2579 * No other requirements.
2582 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2589 lwkt_gettoken(&vm_token);
2590 pte = pmap_pte(pmap, va);
2592 if (wired && !pmap_pte_w(pte))
2593 pmap->pm_stats.wired_count++;
2594 else if (!wired && pmap_pte_w(pte))
2595 pmap->pm_stats.wired_count--;
2598 * Wiring is not a hardware characteristic so there is no need to
2599 * invalidate TLB. However, in an SMP environment we must use
2600 * a locked bus cycle to update the pte (if we are not using
2601 * the pmap_inval_*() API that is)... it's ok to do this for simple
2605 atomic_set_long(pte, VPTE_WIRED);
2607 atomic_clear_long(pte, VPTE_WIRED);
2608 lwkt_reltoken(&vm_token);
2612 * Copy the range specified by src_addr/len
2613 * from the source map to the range dst_addr/len
2614 * in the destination map.
2616 * This routine is only advisory and need not do anything.
2619 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2620 vm_size_t len, vm_offset_t src_addr)
2623 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2624 * valid through blocking calls, and that's just not going to
2635 * Zero the specified physical page.
2637 * This function may be called from an interrupt and no locking is
2641 pmap_zero_page(vm_paddr_t phys)
2643 vm_offset_t va = PHYS_TO_DMAP(phys);
2645 bzero((void *)va, PAGE_SIZE);
2649 * pmap_page_assertzero:
2651 * Assert that a page is empty, panic if it isn't.
2654 pmap_page_assertzero(vm_paddr_t phys)
2659 vm_offset_t virt = PHYS_TO_DMAP(phys);
2661 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
2662 if (*(int *)((char *)virt + i) != 0) {
2663 panic("pmap_page_assertzero() @ %p not zero!\n",
2673 * Zero part of a physical page by mapping it into memory and clearing
2674 * its contents with bzero.
2676 * off and size may not cover an area beyond a single hardware page.
2679 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2682 vm_offset_t virt = PHYS_TO_DMAP(phys);
2683 bzero((char *)virt + off, size);
2690 * Copy the physical page from the source PA to the target PA.
2691 * This function may be called from an interrupt. No locking
2695 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2697 vm_offset_t src_virt, dst_virt;
2700 src_virt = PHYS_TO_DMAP(src);
2701 dst_virt = PHYS_TO_DMAP(dst);
2702 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
2707 * pmap_copy_page_frag:
2709 * Copy the physical page from the source PA to the target PA.
2710 * This function may be called from an interrupt. No locking
2714 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2716 vm_offset_t src_virt, dst_virt;
2719 src_virt = PHYS_TO_DMAP(src);
2720 dst_virt = PHYS_TO_DMAP(dst);
2721 bcopy((char *)src_virt + (src & PAGE_MASK),
2722 (char *)dst_virt + (dst & PAGE_MASK),
2728 * Returns true if the pmap's pv is one of the first 16 pvs linked to
2729 * from this page. This count may be changed upwards or downwards
2730 * in the future; it is only necessary that true be returned for a small
2731 * subset of pmaps for proper page aging.
2733 * No other requirements.
2736 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2741 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2745 lwkt_gettoken(&vm_token);
2747 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2748 if (pv->pv_pmap == pmap) {
2749 lwkt_reltoken(&vm_token);
2757 lwkt_reltoken(&vm_token);
2763 * Remove all pages from specified address space this aids process
2764 * exit speeds. Also, this code is special cased for current
2765 * process only, but can have the more generic (and slightly slower)
2766 * mode enabled. This is much faster than pmap_remove in the case
2767 * of running down an entire address space.
2769 * No other requirements.
2772 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2774 pt_entry_t *pte, tpte;
2777 int save_generation;
2780 lwkt_gettoken(&vm_token);
2781 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2782 if (pv->pv_va >= eva || pv->pv_va < sva) {
2783 npv = TAILQ_NEXT(pv, pv_plist);
2787 KKASSERT(pmap == pv->pv_pmap);
2789 pte = pmap_pte(pmap, pv->pv_va);
2792 * We cannot remove wired pages from a process' mapping
2795 if (*pte & VPTE_WIRED) {
2796 npv = TAILQ_NEXT(pv, pv_plist);
2799 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2801 m = PHYS_TO_VM_PAGE(tpte & VPTE_FRAME);
2803 KASSERT(m < &vm_page_array[vm_page_array_size],
2804 ("pmap_remove_pages: bad tpte %lx", tpte));
2806 KKASSERT(pmap->pm_stats.resident_count > 0);
2807 --pmap->pm_stats.resident_count;
2810 * Update the vm_page_t clean and reference bits.
2812 if (tpte & VPTE_M) {
2816 npv = TAILQ_NEXT(pv, pv_plist);
2817 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2818 save_generation = ++pmap->pm_generation;
2820 m->md.pv_list_count--;
2821 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2822 if (TAILQ_EMPTY(&m->md.pv_list))
2823 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2825 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2829 * Restart the scan if we blocked during the unuse or free
2830 * calls and other removals were made.
2832 if (save_generation != pmap->pm_generation) {
2833 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2834 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2837 lwkt_reltoken(&vm_token);
2842 * pmap_testbit tests bits in active mappings of a VM page.
2845 pmap_testbit(vm_page_t m, int bit)
2850 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2853 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2858 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2860 * if the bit being tested is the modified bit, then
2861 * mark clean_map and ptes as never
2864 if (bit & (VPTE_A|VPTE_M)) {
2865 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2869 #if defined(PMAP_DIAGNOSTIC)
2870 if (pv->pv_pmap == NULL) {
2871 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
2875 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2886 * This routine is used to clear bits in ptes. Certain bits require special
2887 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2889 * This routine is only called with certain VPTE_* bit combinations.
2891 static __inline void
2892 pmap_clearbit(vm_page_t m, int bit)
2898 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2904 * Loop over all current mappings setting/clearing as appropos If
2905 * setting RO do we need to clear the VAC?
2907 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2909 * don't write protect pager mappings
2911 if (bit == VPTE_W) {
2912 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2916 #if defined(PMAP_DIAGNOSTIC)
2917 if (pv->pv_pmap == NULL) {
2918 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
2924 * Careful here. We can use a locked bus instruction to
2925 * clear VPTE_A or VPTE_M safely but we need to synchronize
2926 * with the target cpus when we mess with VPTE_W.
2928 * On virtual kernels we must force a new fault-on-write
2929 * in the real kernel if we clear the Modify bit ourselves,
2930 * otherwise the real kernel will not get a new fault and
2931 * will never set our Modify bit again.
2933 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2935 if (bit == VPTE_W) {
2937 * We must also clear VPTE_M when clearing
2940 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2944 } else if (bit == VPTE_M) {
2946 * We do not have to make the page read-only
2947 * when clearing the Modify bit. The real
2948 * kernel will make the real PTE read-only
2949 * or otherwise detect the write and set
2950 * our VPTE_M again simply by us invalidating
2951 * the real kernel VA for the pmap (as we did
2952 * above). This allows the real kernel to
2953 * handle the write fault without forwarding
2956 atomic_clear_long(pte, VPTE_M);
2957 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2959 * We've been asked to clear W & M, I guess
2960 * the caller doesn't want us to update
2961 * the dirty status of the VM page.
2963 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2966 * We've been asked to clear bits that do
2967 * not interact with hardware.
2969 atomic_clear_long(pte, bit);
2977 * Lower the permission for all mappings to a given page.
2979 * No other requirements.
2982 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2984 /* JG NX support? */
2985 if ((prot & VM_PROT_WRITE) == 0) {
2986 lwkt_gettoken(&vm_token);
2987 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2988 pmap_clearbit(m, VPTE_W);
2989 vm_page_flag_clear(m, PG_WRITEABLE);
2993 lwkt_reltoken(&vm_token);
2998 pmap_phys_address(vm_pindex_t ppn)
3000 return (x86_64_ptob(ppn));
3004 * Return a count of reference bits for a page, clearing those bits.
3005 * It is not necessary for every reference bit to be cleared, but it
3006 * is necessary that 0 only be returned when there are truly no
3007 * reference bits set.
3009 * XXX: The exact number of bits to check and clear is a matter that
3010 * should be tested and standardized at some point in the future for
3011 * optimal aging of shared pages.
3013 * No other requirements.
3016 pmap_ts_referenced(vm_page_t m)
3018 pv_entry_t pv, pvf, pvn;
3022 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3026 lwkt_gettoken(&vm_token);
3028 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3033 pvn = TAILQ_NEXT(pv, pv_list);
3035 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3037 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3039 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3042 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3044 if (pte && (*pte & VPTE_A)) {
3046 atomic_clear_long(pte, VPTE_A);
3048 atomic_clear_long_nonlocked(pte, VPTE_A);
3055 } while ((pv = pvn) != NULL && pv != pvf);
3057 lwkt_reltoken(&vm_token);
3064 * Return whether or not the specified physical page was modified
3065 * in any physical maps.
3067 * No other requirements.
3070 pmap_is_modified(vm_page_t m)
3074 lwkt_gettoken(&vm_token);
3075 res = pmap_testbit(m, VPTE_M);
3076 lwkt_reltoken(&vm_token);
3081 * Clear the modify bits on the specified physical page.
3083 * No other requirements.
3086 pmap_clear_modify(vm_page_t m)
3088 lwkt_gettoken(&vm_token);
3089 pmap_clearbit(m, VPTE_M);
3090 lwkt_reltoken(&vm_token);
3094 * Clear the reference bit on the specified physical page.
3096 * No other requirements.
3099 pmap_clear_reference(vm_page_t m)
3101 lwkt_gettoken(&vm_token);
3102 pmap_clearbit(m, VPTE_A);
3103 lwkt_reltoken(&vm_token);
3107 * Miscellaneous support routines follow
3111 i386_protection_init(void)
3115 kp = protection_codes;
3116 for (prot = 0; prot < 8; prot++) {
3117 if (prot & VM_PROT_READ)
3119 if (prot & VM_PROT_WRITE)
3121 if (prot & VM_PROT_EXECUTE)
3128 * Perform the pmap work for mincore
3130 * No other requirements.
3133 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3135 pt_entry_t *ptep, pte;
3139 lwkt_gettoken(&vm_token);
3140 ptep = pmap_pte(pmap, addr);
3142 if (ptep && (pte = *ptep) != 0) {
3145 val = MINCORE_INCORE;
3146 if ((pte & VPTE_MANAGED) == 0)
3149 pa = pte & VPTE_FRAME;
3151 m = PHYS_TO_VM_PAGE(pa);
3157 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3159 * Modified by someone
3161 else if (m->dirty || pmap_is_modified(m))
3162 val |= MINCORE_MODIFIED_OTHER;
3167 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3170 * Referenced by someone
3172 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3173 val |= MINCORE_REFERENCED_OTHER;
3174 vm_page_flag_set(m, PG_REFERENCED);
3178 lwkt_reltoken(&vm_token);
3183 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3184 * vmspace will be ref'd and the old one will be deref'd.
3187 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3189 struct vmspace *oldvm;
3193 oldvm = p->p_vmspace;
3194 if (oldvm != newvm) {
3195 p->p_vmspace = newvm;
3196 KKASSERT(p->p_nthreads == 1);
3197 lp = RB_ROOT(&p->p_lwp_tree);
3198 pmap_setlwpvm(lp, newvm);
3200 sysref_get(&newvm->vm_sysref);
3201 sysref_put(&oldvm->vm_sysref);
3208 * Set the vmspace for a LWP. The vmspace is almost universally set the
3209 * same as the process vmspace, but virtual kernels need to swap out contexts
3210 * on a per-lwp basis.
3213 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3215 struct vmspace *oldvm;
3219 oldvm = lp->lwp_vmspace;
3221 if (oldvm != newvm) {
3222 lp->lwp_vmspace = newvm;
3223 if (curthread->td_lwp == lp) {
3224 pmap = vmspace_pmap(newvm);
3226 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3228 pmap->pm_active |= 1;
3230 #if defined(SWTCH_OPTIM_STATS)
3233 pmap = vmspace_pmap(oldvm);
3235 atomic_clear_int(&pmap->pm_active,
3236 1 << mycpu->gd_cpuid);
3238 pmap->pm_active &= ~1;
3246 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3249 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3253 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);