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);
396 * Calculate NKPT - number of kernel page tables. We have to
397 * accomodoate prealloction of the vm_page_array, dump bitmap,
398 * MSGBUF_SIZE, and other stuff. Be generous.
400 * Maxmem is in pages.
402 nkpt = (Maxmem * (sizeof(struct vm_page) * 2) + MSGBUF_SIZE) / NBPDR;
407 KPML4phys = allocpages(firstaddr, 1);
408 KPDPphys = allocpages(firstaddr, NKPML4E);
409 KPDphys = allocpages(firstaddr, NKPDPE);
410 KPTphys = allocpages(firstaddr, nkpt);
412 KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
413 KPDPvirt = (pdp_entry_t *)PHYS_TO_DMAP(KPDPphys);
414 KPDvirt = (pd_entry_t *)PHYS_TO_DMAP(KPDphys);
415 KPTvirt = (pt_entry_t *)PHYS_TO_DMAP(KPTphys);
417 bzero(KPML4virt, 1 * PAGE_SIZE);
418 bzero(KPDPvirt, NKPML4E * PAGE_SIZE);
419 bzero(KPDvirt, NKPDPE * PAGE_SIZE);
420 bzero(KPTvirt, nkpt * PAGE_SIZE);
422 /* Now map the page tables at their location within PTmap */
423 for (i = 0; i < nkpt; i++) {
424 KPDvirt[i] = KPTphys + (i << PAGE_SHIFT);
425 KPDvirt[i] |= VPTE_R | VPTE_W | VPTE_V;
428 /* And connect up the PD to the PDP */
429 for (i = 0; i < NKPDPE; i++) {
430 KPDPvirt[i + kpdpi] = KPDphys + (i << PAGE_SHIFT);
431 KPDPvirt[i + kpdpi] |= VPTE_R | VPTE_W | VPTE_V;
434 /* And recursively map PML4 to itself in order to get PTmap */
435 KPML4virt[PML4PML4I] = KPML4phys;
436 KPML4virt[PML4PML4I] |= VPTE_R | VPTE_W | VPTE_V;
438 /* Connect the KVA slot up to the PML4 */
439 KPML4virt[kpml4i] = KPDPphys;
440 KPML4virt[kpml4i] |= VPTE_R | VPTE_W | VPTE_V;
444 * Bootstrap the system enough to run with virtual memory.
446 * On the i386 this is called after mapping has already been enabled
447 * and just syncs the pmap module with what has already been done.
448 * [We can't call it easily with mapping off since the kernel is not
449 * mapped with PA == VA, hence we would have to relocate every address
450 * from the linked base (virtual) address "KERNBASE" to the actual
451 * (physical) address starting relative to 0]
454 pmap_bootstrap(vm_paddr_t *firstaddr, int64_t ptov_offset)
460 * Create an initial set of page tables to run the kernel in.
462 create_pagetables(firstaddr, ptov_offset);
464 virtual_start = KvaStart + *firstaddr;
465 virtual_end = KvaEnd;
468 * Initialize protection array.
470 i386_protection_init();
473 * The kernel's pmap is statically allocated so we don't have to use
474 * pmap_create, which is unlikely to work correctly at this part of
475 * the boot sequence (XXX and which no longer exists).
477 kernel_pmap.pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
478 kernel_pmap.pm_count = 1;
479 kernel_pmap.pm_active = (cpumask_t)-1; /* don't allow deactivation */
480 TAILQ_INIT(&kernel_pmap.pm_pvlist);
483 * Reserve some special page table entries/VA space for temporary
486 #define SYSMAP(c, p, v, n) \
487 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
490 pte = pmap_pte(&kernel_pmap, va);
493 * CMAP1/CMAP2 are used for zeroing and copying pages.
495 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
501 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
505 * ptvmmap is used for reading arbitrary physical pages via
508 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
511 * msgbufp is used to map the system message buffer.
512 * XXX msgbufmap is not used.
514 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
515 atop(round_page(MSGBUF_SIZE)))
525 * Initialize the pmap module.
526 * Called by vm_init, to initialize any structures that the pmap
527 * system needs to map virtual memory.
528 * pmap_init has been enhanced to support in a fairly consistant
529 * way, discontiguous physical memory.
538 * object for kernel page table pages
540 /* JG I think the number can be arbitrary */
541 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
544 * Allocate memory for random pmap data structures. Includes the
548 for(i = 0; i < vm_page_array_size; i++) {
551 m = &vm_page_array[i];
552 TAILQ_INIT(&m->md.pv_list);
553 m->md.pv_list_count = 0;
557 * init the pv free list
559 initial_pvs = vm_page_array_size;
560 if (initial_pvs < MINPV)
562 pvzone = &pvzone_store;
563 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
564 initial_pvs * sizeof (struct pv_entry));
565 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
569 * Now it is safe to enable pv_table recording.
571 pmap_initialized = TRUE;
575 * Initialize the address space (zone) for the pv_entries. Set a
576 * high water mark so that the system can recover from excessive
577 * numbers of pv entries.
582 int shpgperproc = PMAP_SHPGPERPROC;
584 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
585 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
586 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
587 pv_entry_high_water = 9 * (pv_entry_max / 10);
588 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
592 /***************************************************
593 * Low level helper routines.....
594 ***************************************************/
597 * The modification bit is not tracked for any pages in this range. XXX
598 * such pages in this maps should always use pmap_k*() functions and not
601 * XXX User and kernel address spaces are independant for virtual kernels,
602 * this function only applies to the kernel pmap.
605 pmap_track_modified(pmap_t pmap, vm_offset_t va)
607 if (pmap != &kernel_pmap)
609 if ((va < clean_sva) || (va >= clean_eva))
616 * Extract the physical page address associated with the map/VA pair.
621 pmap_extract(pmap_t pmap, vm_offset_t va)
625 pd_entry_t pde, *pdep;
627 lwkt_gettoken(&vm_token);
629 pdep = pmap_pde(pmap, va);
633 if ((pde & VPTE_PS) != 0) {
635 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
637 pte = pmap_pde_to_pte(pdep, va);
638 rtval = (*pte & VPTE_FRAME) | (va & PAGE_MASK);
642 lwkt_reltoken(&vm_token);
647 * Routine: pmap_kextract
649 * Extract the physical page address associated
650 * kernel virtual address.
653 pmap_kextract(vm_offset_t va)
658 KKASSERT(va >= KvaStart && va < KvaEnd);
661 * The DMAP region is not included in [KvaStart, KvaEnd)
664 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
665 pa = DMAP_TO_PHYS(va);
671 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
674 * Beware of a concurrent promotion that changes the
675 * PDE at this point! For example, vtopte() must not
676 * be used to access the PTE because it would use the
677 * new PDE. It is, however, safe to use the old PDE
678 * because the page table page is preserved by the
681 pa = *pmap_pde_to_pte(&pde, va);
682 pa = (pa & VPTE_FRAME) | (va & PAGE_MASK);
690 /***************************************************
691 * Low level mapping routines.....
692 ***************************************************/
695 * Enter a mapping into kernel_pmap. Mappings created in this fashion
696 * are not managed. Mappings must be immediately accessible on all cpus.
698 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
699 * real pmap and handle related races before storing the new vpte.
702 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
707 KKASSERT(va >= KvaStart && va < KvaEnd);
708 npte = pa | VPTE_R | VPTE_W | VPTE_V;
711 pmap_inval_pte(pte, &kernel_pmap, va);
716 * Enter an unmanaged KVA mapping for the private use of the current
717 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
720 * It is illegal for the mapping to be accessed by other cpus unleess
721 * pmap_kenter_sync*() is called.
724 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
729 KKASSERT(va >= KvaStart && va < KvaEnd);
731 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
734 pmap_inval_pte_quick(pte, &kernel_pmap, va);
736 //cpu_invlpg((void *)va);
740 * Synchronize a kvm mapping originally made for the private use on
741 * some other cpu so it can be used on all cpus.
743 * XXX add MADV_RESYNC to improve performance.
746 pmap_kenter_sync(vm_offset_t va)
748 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
752 * Synchronize a kvm mapping originally made for the private use on
753 * some other cpu so it can be used on our cpu. Turns out to be the
754 * same madvise() call, because we have to sync the real pmaps anyway.
756 * XXX add MADV_RESYNC to improve performance.
759 pmap_kenter_sync_quick(vm_offset_t va)
761 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
765 * Remove an unmanaged mapping created with pmap_kenter*().
768 pmap_kremove(vm_offset_t va)
772 KKASSERT(va >= KvaStart && va < KvaEnd);
776 pmap_inval_pte(pte, &kernel_pmap, va);
781 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
782 * only with this cpu.
784 * Unfortunately because we optimize new entries by testing VPTE_V later
785 * on, we actually still have to synchronize with all the cpus. XXX maybe
786 * store a junk value and test against 0 in the other places instead?
789 pmap_kremove_quick(vm_offset_t va)
793 KKASSERT(va >= KvaStart && va < KvaEnd);
797 pmap_inval_pte(pte, &kernel_pmap, va); /* NOT _quick */
802 * Used to map a range of physical addresses into kernel
803 * virtual address space.
805 * For now, VM is already on, we only need to map the
809 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
811 return PHYS_TO_DMAP(start);
816 * Map a set of unmanaged VM pages into KVM.
819 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
823 end_va = va + count * PAGE_SIZE;
824 KKASSERT(va >= KvaStart && end_va < KvaEnd);
826 while (va < end_va) {
831 pmap_inval_pte(pte, &kernel_pmap, va);
832 *pte = VM_PAGE_TO_PHYS(*m) | VPTE_R | VPTE_W | VPTE_V;
839 * Undo the effects of pmap_qenter*().
842 pmap_qremove(vm_offset_t va, int count)
846 end_va = va + count * PAGE_SIZE;
847 KKASSERT(va >= KvaStart && end_va < KvaEnd);
849 while (va < end_va) {
854 pmap_inval_pte(pte, &kernel_pmap, va);
861 * This routine works like vm_page_lookup() but also blocks as long as the
862 * page is busy. This routine does not busy the page it returns.
864 * Unless the caller is managing objects whos pages are in a known state,
865 * the call should be made with a critical section held so the page's object
866 * association remains valid on return.
869 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
874 m = vm_page_lookup(object, pindex);
875 } while (m && vm_page_sleep_busy(m, 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 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
930 KASSERT(m->queue == PQ_NONE,
931 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
933 if (m->hold_count == 1) {
935 * Unmap the page table page.
939 /* pmap_inval_add(info, pmap, -1); */
941 if (m->pindex >= (NUPDE + NUPDPE)) {
944 pml4 = pmap_pml4e(pmap, va);
946 } else if (m->pindex >= NUPDE) {
949 pdp = pmap_pdpe(pmap, va);
954 pd = pmap_pde(pmap, va);
958 KKASSERT(pmap->pm_stats.resident_count > 0);
959 --pmap->pm_stats.resident_count;
961 if (pmap->pm_ptphint == m)
962 pmap->pm_ptphint = NULL;
964 if (m->pindex < NUPDE) {
965 /* We just released a PT, unhold the matching PD */
968 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & VPTE_FRAME);
969 pmap_unwire_pte_hold(pmap, va, pdpg);
971 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
972 /* We just released a PD, unhold the matching PDP */
975 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & VPTE_FRAME);
976 pmap_unwire_pte_hold(pmap, va, pdppg);
980 * This was our last hold, the page had better be unwired
981 * after we decrement wire_count.
983 * FUTURE NOTE: shared page directory page could result in
984 * multiple wire counts.
988 KKASSERT(m->wire_count == 0);
989 --vmstats.v_wire_count;
990 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
992 vm_page_free_zero(m);
995 KKASSERT(m->hold_count > 1);
1002 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
1004 KKASSERT(m->hold_count > 0);
1005 if (m->hold_count > 1) {
1009 return _pmap_unwire_pte_hold(pmap, va, m);
1014 * After removing a page table entry, this routine is used to
1015 * conditionally free the page, and manage the hold/wire counts.
1018 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1020 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1021 vm_pindex_t ptepindex;
1025 * page table pages in the kernel_pmap are not managed.
1027 if (pmap == &kernel_pmap)
1029 ptepindex = pmap_pde_pindex(va);
1030 if (pmap->pm_ptphint &&
1031 (pmap->pm_ptphint->pindex == ptepindex)) {
1032 mpte = pmap->pm_ptphint;
1034 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1035 pmap->pm_ptphint = mpte;
1039 return pmap_unwire_pte_hold(pmap, va, mpte);
1043 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
1044 * just dummy it up so it works well enough for fork().
1046 * In DragonFly, process pmaps may only be used to manipulate user address
1047 * space, never kernel address space.
1050 pmap_pinit0(struct pmap *pmap)
1056 * Initialize a preallocated and zeroed pmap structure,
1057 * such as one in a vmspace structure.
1060 pmap_pinit(struct pmap *pmap)
1065 * No need to allocate page table space yet but we do need a valid
1066 * page directory table.
1068 if (pmap->pm_pml4 == NULL) {
1070 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1074 * Allocate an object for the ptes
1076 if (pmap->pm_pteobj == NULL)
1077 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1080 * Allocate the page directory page, unless we already have
1081 * one cached. If we used the cached page the wire_count will
1082 * already be set appropriately.
1084 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1085 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1086 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1087 pmap->pm_pdirm = ptdpg;
1088 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1089 ptdpg->valid = VM_PAGE_BITS_ALL;
1090 if (ptdpg->wire_count == 0)
1091 ++vmstats.v_wire_count;
1092 ptdpg->wire_count = 1;
1093 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1095 if ((ptdpg->flags & PG_ZERO) == 0)
1096 bzero(pmap->pm_pml4, PAGE_SIZE);
1099 pmap->pm_active = 0;
1100 pmap->pm_ptphint = NULL;
1101 TAILQ_INIT(&pmap->pm_pvlist);
1102 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1103 pmap->pm_stats.resident_count = 1;
1107 * Clean up a pmap structure so it can be physically freed. This routine
1108 * is called by the vmspace dtor function. A great deal of pmap data is
1109 * left passively mapped to improve vmspace management so we have a bit
1110 * of cleanup work to do here.
1115 pmap_puninit(pmap_t pmap)
1119 KKASSERT(pmap->pm_active == 0);
1120 lwkt_gettoken(&vm_token);
1121 if ((p = pmap->pm_pdirm) != NULL) {
1122 KKASSERT(pmap->pm_pml4 != NULL);
1123 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1125 vmstats.v_wire_count--;
1126 KKASSERT((p->flags & PG_BUSY) == 0);
1128 vm_page_free_zero(p);
1129 pmap->pm_pdirm = NULL;
1131 lwkt_reltoken(&vm_token);
1132 if (pmap->pm_pml4) {
1133 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1134 pmap->pm_pml4 = NULL;
1136 if (pmap->pm_pteobj) {
1137 vm_object_deallocate(pmap->pm_pteobj);
1138 pmap->pm_pteobj = NULL;
1143 * Wire in kernel global address entries. To avoid a race condition
1144 * between pmap initialization and pmap_growkernel, this procedure
1145 * adds the pmap to the master list (which growkernel scans to update),
1146 * then copies the template.
1148 * In a virtual kernel there are no kernel global address entries.
1153 pmap_pinit2(struct pmap *pmap)
1156 lwkt_gettoken(&vm_token);
1157 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1158 lwkt_reltoken(&vm_token);
1163 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1164 * 0 on failure (if the procedure had to sleep).
1166 * When asked to remove the page directory page itself, we actually just
1167 * leave it cached so we do not have to incur the SMP inval overhead of
1168 * removing the kernel mapping. pmap_puninit() will take care of it.
1171 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1174 * This code optimizes the case of freeing non-busy
1175 * page-table pages. Those pages are zero now, and
1176 * might as well be placed directly into the zero queue.
1178 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1184 * Remove the page table page from the processes address space.
1186 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1188 * We are the pml4 table itself.
1190 /* XXX anything to do here? */
1191 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1193 * We are a PDP page.
1194 * We look for the PML4 entry that points to us.
1196 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1197 KKASSERT(m4 != NULL);
1198 pml4_entry_t *pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1199 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1200 KKASSERT(pml4[idx] != 0);
1203 /* JG What about wire_count? */
1204 } else if (p->pindex >= NUPDE) {
1207 * We look for the PDP entry that points to us.
1209 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1210 KKASSERT(m3 != NULL);
1211 pdp_entry_t *pdp = (pdp_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1212 int idx = (p->pindex - NUPDE) % NPDPEPG;
1213 KKASSERT(pdp[idx] != 0);
1216 /* JG What about wire_count? */
1218 /* We are a PT page.
1219 * We look for the PD entry that points to us.
1221 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1222 KKASSERT(m2 != NULL);
1223 pd_entry_t *pd = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1224 int idx = p->pindex % NPDEPG;
1227 /* JG What about wire_count? */
1229 KKASSERT(pmap->pm_stats.resident_count > 0);
1230 --pmap->pm_stats.resident_count;
1232 if (p->hold_count) {
1233 panic("pmap_release: freeing held page table page");
1235 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1236 pmap->pm_ptphint = NULL;
1239 * We leave the top-level page table page cached, wired, and mapped in
1240 * the pmap until the dtor function (pmap_puninit()) gets called.
1241 * However, still clean it up so we can set PG_ZERO.
1243 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1244 bzero(pmap->pm_pml4, PAGE_SIZE);
1245 vm_page_flag_set(p, PG_ZERO);
1250 vmstats.v_wire_count--;
1251 /* JG eventually revert to using vm_page_free_zero() */
1258 * this routine is called if the page table page is not
1262 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1264 vm_page_t m, pdppg, pdpg;
1267 * Find or fabricate a new pagetable page
1269 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1270 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1272 if ((m->flags & PG_ZERO) == 0) {
1273 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1276 KASSERT(m->queue == PQ_NONE,
1277 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1280 * Increment the hold count for the page we will be returning to
1285 if (m->wire_count == 0)
1286 vmstats.v_wire_count++;
1290 * Map the pagetable page into the process address space, if
1291 * it isn't already there.
1294 ++pmap->pm_stats.resident_count;
1296 if (ptepindex >= (NUPDE + NUPDPE)) {
1298 vm_pindex_t pml4index;
1300 /* Wire up a new PDP page */
1301 pml4index = ptepindex - (NUPDE + NUPDPE);
1302 pml4 = &pmap->pm_pml4[pml4index];
1303 *pml4 = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1305 } else if (ptepindex >= NUPDE) {
1306 vm_pindex_t pml4index;
1307 vm_pindex_t pdpindex;
1311 /* Wire up a new PD page */
1312 pdpindex = ptepindex - NUPDE;
1313 pml4index = pdpindex >> NPML4EPGSHIFT;
1315 pml4 = &pmap->pm_pml4[pml4index];
1316 if ((*pml4 & VPTE_V) == 0) {
1317 /* Have to allocate a new PDP page, recurse */
1318 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1325 /* Add reference to the PDP page */
1326 pdppg = PHYS_TO_VM_PAGE(*pml4 & VPTE_FRAME);
1327 pdppg->hold_count++;
1329 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1331 /* Now find the pdp page */
1332 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1333 KKASSERT(*pdp == 0); /* JG DEBUG64 */
1334 *pdp = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1337 vm_pindex_t pml4index;
1338 vm_pindex_t pdpindex;
1343 /* Wire up a new PT page */
1344 pdpindex = ptepindex >> NPDPEPGSHIFT;
1345 pml4index = pdpindex >> NPML4EPGSHIFT;
1347 /* First, find the pdp and check that its valid. */
1348 pml4 = &pmap->pm_pml4[pml4index];
1349 if ((*pml4 & VPTE_V) == 0) {
1350 /* We miss a PDP page. We ultimately need a PD page.
1351 * Recursively allocating a PD page will allocate
1352 * the missing PDP page and will also allocate
1353 * the PD page we need.
1355 /* Have to allocate a new PD page, recurse */
1356 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1362 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1363 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1365 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1366 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1367 if ((*pdp & VPTE_V) == 0) {
1368 /* Have to allocate a new PD page, recurse */
1369 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1376 /* Add reference to the PD page */
1377 pdpg = PHYS_TO_VM_PAGE(*pdp & VPTE_FRAME);
1381 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & VPTE_FRAME);
1383 /* Now we know where the page directory page is */
1384 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1385 KKASSERT(*pd == 0); /* JG DEBUG64 */
1386 *pd = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1391 * Set the page table hint
1393 pmap->pm_ptphint = m;
1395 m->valid = VM_PAGE_BITS_ALL;
1396 vm_page_flag_clear(m, PG_ZERO);
1397 vm_page_flag_set(m, PG_MAPPED);
1404 * Determine the page table page required to access the VA in the pmap
1405 * and allocate it if necessary. Return a held vm_page_t for the page.
1407 * Only used with user pmaps.
1410 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1412 vm_pindex_t ptepindex;
1417 * Calculate pagetable page index
1419 ptepindex = pmap_pde_pindex(va);
1422 * Get the page directory entry
1424 pd = pmap_pde(pmap, va);
1427 * This supports switching from a 2MB page to a
1430 if (pd != NULL && (*pd & (VPTE_PS | VPTE_V)) == (VPTE_PS | VPTE_V)) {
1431 panic("no promotion/demotion yet");
1439 * If the page table page is mapped, we just increment the
1440 * hold count, and activate it.
1442 if (pd != NULL && (*pd & VPTE_V) != 0) {
1443 /* YYY hint is used here on i386 */
1444 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1445 pmap->pm_ptphint = m;
1450 * Here if the pte page isn't mapped, or if it has been deallocated.
1452 return _pmap_allocpte(pmap, ptepindex);
1456 /***************************************************
1457 * Pmap allocation/deallocation routines.
1458 ***************************************************/
1461 * Release any resources held by the given physical map.
1462 * Called when a pmap initialized by pmap_pinit is being released.
1463 * Should only be called if the map contains no valid mappings.
1467 static int pmap_release_callback(struct vm_page *p, void *data);
1470 pmap_release(struct pmap *pmap)
1472 vm_object_t object = pmap->pm_pteobj;
1473 struct rb_vm_page_scan_info info;
1475 KKASSERT(pmap != &kernel_pmap);
1477 #if defined(DIAGNOSTIC)
1478 if (object->ref_count != 1)
1479 panic("pmap_release: pteobj reference count != 1");
1483 info.object = object;
1485 lwkt_gettoken(&vm_token);
1486 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1493 info.limit = object->generation;
1495 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1496 pmap_release_callback, &info);
1497 if (info.error == 0 && info.mpte) {
1498 if (!pmap_release_free_page(pmap, info.mpte))
1502 } while (info.error);
1503 lwkt_reltoken(&vm_token);
1507 pmap_release_callback(struct vm_page *p, void *data)
1509 struct rb_vm_page_scan_info *info = data;
1511 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1515 if (!pmap_release_free_page(info->pmap, p)) {
1519 if (info->object->generation != info->limit) {
1527 * Grow the number of kernel page table entries, if needed.
1532 pmap_growkernel(vm_offset_t addr)
1535 vm_offset_t ptppaddr;
1537 pd_entry_t *pde, newpdir;
1541 lwkt_gettoken(&vm_token);
1542 if (kernel_vm_end == 0) {
1543 kernel_vm_end = KvaStart;
1545 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & VPTE_V) != 0) {
1546 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1548 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1549 kernel_vm_end = kernel_map.max_offset;
1554 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1555 if (addr - 1 >= kernel_map.max_offset)
1556 addr = kernel_map.max_offset;
1557 while (kernel_vm_end < addr) {
1558 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1560 /* We need a new PDP entry */
1561 nkpg = vm_page_alloc(kptobj, nkpt,
1562 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1563 | VM_ALLOC_INTERRUPT);
1565 panic("pmap_growkernel: no memory to grow kernel");
1566 paddr = VM_PAGE_TO_PHYS(nkpg);
1567 if ((nkpg->flags & PG_ZERO) == 0)
1568 pmap_zero_page(paddr);
1569 vm_page_flag_clear(nkpg, PG_ZERO);
1570 newpdp = (pdp_entry_t)
1571 (paddr | VPTE_V | VPTE_R | VPTE_W | VPTE_A | VPTE_M);
1572 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1574 continue; /* try again */
1576 if ((*pde & VPTE_V) != 0) {
1577 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1578 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1579 kernel_vm_end = kernel_map.max_offset;
1586 * This index is bogus, but out of the way
1588 nkpg = vm_page_alloc(kptobj, nkpt,
1589 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1591 panic("pmap_growkernel: no memory to grow kernel");
1594 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1595 pmap_zero_page(ptppaddr);
1596 vm_page_flag_clear(nkpg, PG_ZERO);
1597 newpdir = (pd_entry_t) (ptppaddr | VPTE_V | VPTE_R | VPTE_W | VPTE_A | VPTE_M);
1598 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1601 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1602 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1603 kernel_vm_end = kernel_map.max_offset;
1607 lwkt_reltoken(&vm_token);
1612 * Retire the given physical map from service. Should only be called
1613 * if the map contains no valid mappings.
1618 pmap_destroy(pmap_t pmap)
1623 lwkt_gettoken(&vm_token);
1624 if (--pmap->pm_count == 0) {
1626 panic("destroying a pmap is not yet implemented");
1628 lwkt_reltoken(&vm_token);
1632 * Add a reference to the specified pmap.
1637 pmap_reference(pmap_t pmap)
1640 lwkt_gettoken(&vm_token);
1642 lwkt_reltoken(&vm_token);
1646 /************************************************************************
1647 * VMSPACE MANAGEMENT *
1648 ************************************************************************
1650 * The VMSPACE management we do in our virtual kernel must be reflected
1651 * in the real kernel. This is accomplished by making vmspace system
1652 * calls to the real kernel.
1655 cpu_vmspace_alloc(struct vmspace *vm)
1661 #define USER_SIZE (VM_MAX_USER_ADDRESS - VM_MIN_USER_ADDRESS)
1663 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
1664 panic("vmspace_create() failed");
1666 rp = vmspace_mmap(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1667 PROT_READ|PROT_WRITE,
1668 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
1670 if (rp == MAP_FAILED)
1671 panic("vmspace_mmap: failed");
1672 vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1674 vpte = VM_PAGE_TO_PHYS(vmspace_pmap(vm)->pm_pdirm) | VPTE_R | VPTE_W | VPTE_V;
1675 r = vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1678 panic("vmspace_mcontrol: failed");
1682 cpu_vmspace_free(struct vmspace *vm)
1684 if (vmspace_destroy(&vm->vm_pmap) < 0)
1685 panic("vmspace_destroy() failed");
1688 /***************************************************
1689 * page management routines.
1690 ***************************************************/
1693 * free the pv_entry back to the free list. This function may be
1694 * called from an interrupt.
1696 static __inline void
1697 free_pv_entry(pv_entry_t pv)
1700 KKASSERT(pv_entry_count >= 0);
1705 * get a new pv_entry, allocating a block from the system
1706 * when needed. This function may be called from an interrupt.
1712 if (pv_entry_high_water &&
1713 (pv_entry_count > pv_entry_high_water) &&
1714 (pmap_pagedaemon_waken == 0)) {
1715 pmap_pagedaemon_waken = 1;
1716 wakeup(&vm_pages_needed);
1718 return zalloc(pvzone);
1722 * This routine is very drastic, but can save the system
1732 static int warningdone=0;
1734 if (pmap_pagedaemon_waken == 0)
1736 lwkt_gettoken(&vm_token);
1737 pmap_pagedaemon_waken = 0;
1739 if (warningdone < 5) {
1740 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1744 for(i = 0; i < vm_page_array_size; i++) {
1745 m = &vm_page_array[i];
1746 if (m->wire_count || m->hold_count || m->busy ||
1747 (m->flags & PG_BUSY))
1751 lwkt_reltoken(&vm_token);
1756 * If it is the first entry on the list, it is actually
1757 * in the header and we must copy the following entry up
1758 * to the header. Otherwise we must search the list for
1759 * the entry. In either case we free the now unused entry.
1762 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1768 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1769 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1770 if (pmap == pv->pv_pmap && va == pv->pv_va)
1774 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1775 if (va == pv->pv_va)
1781 * Note that pv_ptem is NULL if the page table page itself is not
1782 * managed, even if the page being removed IS managed.
1785 /* JGXXX When can 'pv' be NULL? */
1787 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1788 m->md.pv_list_count--;
1789 KKASSERT(m->md.pv_list_count >= 0);
1790 if (TAILQ_EMPTY(&m->md.pv_list))
1791 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1792 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1793 ++pmap->pm_generation;
1794 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1802 * Create a pv entry for page at pa for (pmap, va). If the page table page
1803 * holding the VA is managed, mpte will be non-NULL.
1806 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1811 pv = get_pv_entry();
1816 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1817 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1818 m->md.pv_list_count++;
1824 * pmap_remove_pte: do the things to unmap a page in a process
1827 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1832 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1833 if (oldpte & VPTE_WIRED)
1834 --pmap->pm_stats.wired_count;
1835 KKASSERT(pmap->pm_stats.wired_count >= 0);
1839 * Machines that don't support invlpg, also don't support
1840 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1844 cpu_invlpg((void *)va);
1846 KKASSERT(pmap->pm_stats.resident_count > 0);
1847 --pmap->pm_stats.resident_count;
1848 if (oldpte & VPTE_MANAGED) {
1849 m = PHYS_TO_VM_PAGE(oldpte);
1850 if (oldpte & VPTE_M) {
1851 #if defined(PMAP_DIAGNOSTIC)
1852 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1854 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
1858 if (pmap_track_modified(pmap, va))
1861 if (oldpte & VPTE_A)
1862 vm_page_flag_set(m, PG_REFERENCED);
1863 return pmap_remove_entry(pmap, m, va);
1865 return pmap_unuse_pt(pmap, va, NULL);
1874 * Remove a single page from a process address space.
1876 * This function may not be called from an interrupt if the pmap is
1880 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1884 pte = pmap_pte(pmap, va);
1887 if ((*pte & VPTE_V) == 0)
1889 pmap_remove_pte(pmap, pte, va);
1893 * Remove the given range of addresses from the specified map.
1895 * It is assumed that the start and end are properly rounded to
1898 * This function may not be called from an interrupt if the pmap is
1904 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1906 vm_offset_t va_next;
1907 pml4_entry_t *pml4e;
1909 pd_entry_t ptpaddr, *pde;
1915 lwkt_gettoken(&vm_token);
1916 KKASSERT(pmap->pm_stats.resident_count >= 0);
1917 if (pmap->pm_stats.resident_count == 0) {
1918 lwkt_reltoken(&vm_token);
1923 * special handling of removing one page. a very
1924 * common operation and easy to short circuit some
1927 if (sva + PAGE_SIZE == eva) {
1928 pde = pmap_pde(pmap, sva);
1929 if (pde && (*pde & VPTE_PS) == 0) {
1930 pmap_remove_page(pmap, sva);
1931 lwkt_reltoken(&vm_token);
1936 for (; sva < eva; sva = va_next) {
1937 pml4e = pmap_pml4e(pmap, sva);
1938 if ((*pml4e & VPTE_V) == 0) {
1939 va_next = (sva + NBPML4) & ~PML4MASK;
1945 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
1946 if ((*pdpe & VPTE_V) == 0) {
1947 va_next = (sva + NBPDP) & ~PDPMASK;
1954 * Calculate index for next page table.
1956 va_next = (sva + NBPDR) & ~PDRMASK;
1960 pde = pmap_pdpe_to_pde(pdpe, sva);
1964 * Weed out invalid mappings.
1970 * Check for large page.
1972 if ((ptpaddr & VPTE_PS) != 0) {
1973 /* JG FreeBSD has more complex treatment here */
1974 KKASSERT(*pde != 0);
1975 pmap_inval_pde(pde, pmap, sva);
1976 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1981 * Limit our scan to either the end of the va represented
1982 * by the current page table page, or to the end of the
1983 * range being removed.
1989 * NOTE: pmap_remove_pte() can block.
1991 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1995 if (pmap_remove_pte(pmap, pte, sva))
1999 lwkt_reltoken(&vm_token);
2003 * Removes this physical page from all physical maps in which it resides.
2004 * Reflects back modify bits to the pager.
2006 * This routine may not be called from an interrupt.
2012 pmap_remove_all(vm_page_t m)
2014 pt_entry_t *pte, tpte;
2017 #if defined(PMAP_DIAGNOSTIC)
2019 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
2022 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
2023 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
2028 lwkt_gettoken(&vm_token);
2029 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2030 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2031 --pv->pv_pmap->pm_stats.resident_count;
2033 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2034 KKASSERT(pte != NULL);
2036 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
2037 if (tpte & VPTE_WIRED)
2038 pv->pv_pmap->pm_stats.wired_count--;
2039 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
2042 vm_page_flag_set(m, PG_REFERENCED);
2045 * Update the vm_page_t clean and reference bits.
2047 if (tpte & VPTE_M) {
2048 #if defined(PMAP_DIAGNOSTIC)
2049 if (pmap_nw_modified(tpte)) {
2051 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2055 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
2058 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2059 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2060 ++pv->pv_pmap->pm_generation;
2061 m->md.pv_list_count--;
2062 KKASSERT(m->md.pv_list_count >= 0);
2063 if (TAILQ_EMPTY(&m->md.pv_list))
2064 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2065 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2068 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2069 lwkt_reltoken(&vm_token);
2074 * Set the physical protection on the specified range of this map
2077 * This function may not be called from an interrupt if the map is
2078 * not the kernel_pmap.
2083 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2085 vm_offset_t va_next;
2086 pml4_entry_t *pml4e;
2088 pd_entry_t ptpaddr, *pde;
2091 /* JG review for NX */
2096 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2097 pmap_remove(pmap, sva, eva);
2101 if (prot & VM_PROT_WRITE)
2104 lwkt_gettoken(&vm_token);
2106 for (; sva < eva; sva = va_next) {
2108 pml4e = pmap_pml4e(pmap, sva);
2109 if ((*pml4e & VPTE_V) == 0) {
2110 va_next = (sva + NBPML4) & ~PML4MASK;
2116 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2117 if ((*pdpe & VPTE_V) == 0) {
2118 va_next = (sva + NBPDP) & ~PDPMASK;
2124 va_next = (sva + NBPDR) & ~PDRMASK;
2128 pde = pmap_pdpe_to_pde(pdpe, sva);
2132 * Check for large page.
2134 if ((ptpaddr & VPTE_PS) != 0) {
2136 pmap_clean_pde(pde, pmap, sva);
2137 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2142 * Weed out invalid mappings. Note: we assume that the page
2143 * directory table is always allocated, and in kernel virtual.
2151 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2157 * Clean managed pages and also check the accessed
2158 * bit. Just remove write perms for unmanaged
2159 * pages. Be careful of races, turning off write
2160 * access will force a fault rather then setting
2161 * the modified bit at an unexpected time.
2163 if (*pte & VPTE_MANAGED) {
2164 pbits = pmap_clean_pte(pte, pmap, sva);
2166 if (pbits & VPTE_A) {
2167 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2168 vm_page_flag_set(m, PG_REFERENCED);
2169 atomic_clear_long(pte, VPTE_A);
2171 if (pbits & VPTE_M) {
2172 if (pmap_track_modified(pmap, sva)) {
2174 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2179 pbits = pmap_setro_pte(pte, pmap, sva);
2183 lwkt_reltoken(&vm_token);
2187 * Enter a managed page into a pmap. If the page is not wired related pmap
2188 * data can be destroyed at any time for later demand-operation.
2190 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
2191 * specified protection, and wire the mapping if requested.
2193 * NOTE: This routine may not lazy-evaluate or lose information. The
2194 * page must actually be inserted into the given map NOW.
2196 * NOTE: When entering a page at a KVA address, the pmap must be the
2202 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2209 pt_entry_t origpte, newpte;
2215 va = trunc_page(va);
2217 lwkt_gettoken(&vm_token);
2220 * Get the page table page. The kernel_pmap's page table pages
2221 * are preallocated and have no associated vm_page_t.
2223 if (pmap == &kernel_pmap)
2226 mpte = pmap_allocpte(pmap, va);
2228 pde = pmap_pde(pmap, va);
2229 if (pde != NULL && (*pde & VPTE_V) != 0) {
2230 if ((*pde & VPTE_PS) != 0)
2231 panic("pmap_enter: attempted pmap_enter on 2MB page");
2232 pte = pmap_pde_to_pte(pde, va);
2234 panic("pmap_enter: invalid page directory va=%#lx", va);
2237 KKASSERT(pte != NULL);
2239 * Deal with races on the original mapping (though don't worry
2240 * about VPTE_A races) by cleaning it. This will force a fault
2241 * if an attempt is made to write to the page.
2243 pa = VM_PAGE_TO_PHYS(m);
2244 origpte = pmap_clean_pte(pte, pmap, va);
2245 opa = origpte & VPTE_FRAME;
2247 if (origpte & VPTE_PS)
2248 panic("pmap_enter: attempted pmap_enter on 2MB page");
2251 * Mapping has not changed, must be protection or wiring change.
2253 if (origpte && (opa == pa)) {
2255 * Wiring change, just update stats. We don't worry about
2256 * wiring PT pages as they remain resident as long as there
2257 * are valid mappings in them. Hence, if a user page is wired,
2258 * the PT page will be also.
2260 if (wired && ((origpte & VPTE_WIRED) == 0))
2261 ++pmap->pm_stats.wired_count;
2262 else if (!wired && (origpte & VPTE_WIRED))
2263 --pmap->pm_stats.wired_count;
2266 * Remove the extra pte reference. Note that we cannot
2267 * optimize the RO->RW case because we have adjusted the
2268 * wiring count above and may need to adjust the wiring
2275 * We might be turning off write access to the page,
2276 * so we go ahead and sense modify status.
2278 if (origpte & VPTE_MANAGED) {
2279 if ((origpte & VPTE_M) &&
2280 pmap_track_modified(pmap, va)) {
2282 om = PHYS_TO_VM_PAGE(opa);
2286 KKASSERT(m->flags & PG_MAPPED);
2291 * Mapping has changed, invalidate old range and fall through to
2292 * handle validating new mapping.
2296 err = pmap_remove_pte(pmap, pte, va);
2298 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2302 * Enter on the PV list if part of our managed memory. Note that we
2303 * raise IPL while manipulating pv_table since pmap_enter can be
2304 * called at interrupt time.
2306 if (pmap_initialized &&
2307 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2308 pmap_insert_entry(pmap, va, mpte, m);
2310 vm_page_flag_set(m, PG_MAPPED);
2314 * Increment counters
2316 ++pmap->pm_stats.resident_count;
2318 pmap->pm_stats.wired_count++;
2322 * Now validate mapping with desired protection/wiring.
2324 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | VPTE_V);
2327 newpte |= VPTE_WIRED;
2328 if (pmap != &kernel_pmap)
2332 * If the mapping or permission bits are different from the
2333 * (now cleaned) original pte, an update is needed. We've
2334 * already downgraded or invalidated the page so all we have
2335 * to do now is update the bits.
2337 * XXX should we synchronize RO->RW changes to avoid another
2340 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
2341 *pte = newpte | VPTE_A;
2342 if (newpte & VPTE_W)
2343 vm_page_flag_set(m, PG_WRITEABLE);
2345 KKASSERT((newpte & VPTE_MANAGED) == 0 || (m->flags & PG_MAPPED));
2346 lwkt_reltoken(&vm_token);
2350 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2352 * Currently this routine may only be used on user pmaps, not kernel_pmap.
2357 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2362 vm_pindex_t ptepindex;
2365 KKASSERT(pmap != &kernel_pmap);
2367 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
2370 * Calculate pagetable page index
2372 ptepindex = pmap_pde_pindex(va);
2374 lwkt_gettoken(&vm_token);
2378 * Get the page directory entry
2380 ptepa = pmap_pde(pmap, va);
2383 * If the page table page is mapped, we just increment
2384 * the hold count, and activate it.
2386 if (ptepa && (*ptepa & VPTE_V) != 0) {
2387 if (*ptepa & VPTE_PS)
2388 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2389 if (pmap->pm_ptphint &&
2390 (pmap->pm_ptphint->pindex == ptepindex)) {
2391 mpte = pmap->pm_ptphint;
2393 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2394 pmap->pm_ptphint = mpte;
2399 mpte = _pmap_allocpte(pmap, ptepindex);
2401 } while (mpte == NULL);
2404 * Ok, now that the page table page has been validated, get the pte.
2405 * If the pte is already mapped undo mpte's hold_count and
2408 pte = pmap_pte(pmap, va);
2409 if (*pte & VPTE_V) {
2410 KKASSERT(mpte != NULL);
2411 pmap_unwire_pte_hold(pmap, va, mpte);
2412 pa = VM_PAGE_TO_PHYS(m);
2413 KKASSERT(((*pte ^ pa) & VPTE_FRAME) == 0);
2414 lwkt_reltoken(&vm_token);
2419 * Enter on the PV list if part of our managed memory
2421 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2422 pmap_insert_entry(pmap, va, mpte, m);
2423 vm_page_flag_set(m, PG_MAPPED);
2427 * Increment counters
2429 ++pmap->pm_stats.resident_count;
2431 pa = VM_PAGE_TO_PHYS(m);
2434 * Now validate mapping with RO protection
2436 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2437 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2439 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2440 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2441 /*pmap_inval_flush(&info); don't need for vkernel */
2442 lwkt_reltoken(&vm_token);
2446 * Make a temporary mapping for a physical address. This is only intended
2447 * to be used for panic dumps.
2450 pmap_kenter_temporary(vm_paddr_t pa, int i)
2452 pmap_kenter(crashdumpmap + (i * PAGE_SIZE), pa);
2453 return ((void *)crashdumpmap);
2456 #define MAX_INIT_PT (96)
2459 * This routine preloads the ptes for a given object into the specified pmap.
2460 * This eliminates the blast of soft faults on process startup and
2461 * immediately after an mmap.
2465 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2468 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2469 vm_object_t object, vm_pindex_t pindex,
2470 vm_size_t size, int limit)
2472 struct rb_vm_page_scan_info info;
2477 * We can't preinit if read access isn't set or there is no pmap
2480 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2484 * We can't preinit if the pmap is not the current pmap
2486 lp = curthread->td_lwp;
2487 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2490 psize = x86_64_btop(size);
2492 if ((object->type != OBJT_VNODE) ||
2493 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2494 (object->resident_page_count > MAX_INIT_PT))) {
2498 if (psize + pindex > object->size) {
2499 if (object->size < pindex)
2501 psize = object->size - pindex;
2508 * Use a red-black scan to traverse the requested range and load
2509 * any valid pages found into the pmap.
2511 * We cannot safely scan the object's memq unless we are in a
2512 * critical section since interrupts can remove pages from objects.
2514 info.start_pindex = pindex;
2515 info.end_pindex = pindex + psize - 1;
2522 lwkt_gettoken(&vm_token);
2523 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2524 pmap_object_init_pt_callback, &info);
2525 lwkt_reltoken(&vm_token);
2531 pmap_object_init_pt_callback(vm_page_t p, void *data)
2533 struct rb_vm_page_scan_info *info = data;
2534 vm_pindex_t rel_index;
2536 * don't allow an madvise to blow away our really
2537 * free pages allocating pv entries.
2539 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2540 vmstats.v_free_count < vmstats.v_free_reserved) {
2543 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2544 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2545 if ((p->queue - p->pc) == PQ_CACHE)
2546 vm_page_deactivate(p);
2548 rel_index = p->pindex - info->start_pindex;
2549 pmap_enter_quick(info->pmap,
2550 info->addr + x86_64_ptob(rel_index), p);
2557 * Return TRUE if the pmap is in shape to trivially
2558 * pre-fault the specified address.
2560 * Returns FALSE if it would be non-trivial or if a
2561 * pte is already loaded into the slot.
2566 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2572 lwkt_gettoken(&vm_token);
2573 pde = pmap_pde(pmap, addr);
2574 if (pde == NULL || *pde == 0) {
2577 pte = pmap_pde_to_pte(pde, addr);
2578 ret = (*pte) ? 0 : 1;
2580 lwkt_reltoken(&vm_token);
2585 * Change the wiring attribute for a map/virtual-address pair.
2587 * The mapping must already exist in the pmap.
2588 * No other requirements.
2591 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2598 lwkt_gettoken(&vm_token);
2599 pte = pmap_pte(pmap, va);
2601 if (wired && !pmap_pte_w(pte))
2602 pmap->pm_stats.wired_count++;
2603 else if (!wired && pmap_pte_w(pte))
2604 pmap->pm_stats.wired_count--;
2607 * Wiring is not a hardware characteristic so there is no need to
2608 * invalidate TLB. However, in an SMP environment we must use
2609 * a locked bus cycle to update the pte (if we are not using
2610 * the pmap_inval_*() API that is)... it's ok to do this for simple
2614 atomic_set_long(pte, VPTE_WIRED);
2616 atomic_clear_long(pte, VPTE_WIRED);
2617 lwkt_reltoken(&vm_token);
2621 * Copy the range specified by src_addr/len
2622 * from the source map to the range dst_addr/len
2623 * in the destination map.
2625 * This routine is only advisory and need not do anything.
2628 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2629 vm_size_t len, vm_offset_t src_addr)
2632 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2633 * valid through blocking calls, and that's just not going to
2644 * Zero the specified physical page.
2646 * This function may be called from an interrupt and no locking is
2650 pmap_zero_page(vm_paddr_t phys)
2652 vm_offset_t va = PHYS_TO_DMAP(phys);
2654 bzero((void *)va, PAGE_SIZE);
2658 * pmap_page_assertzero:
2660 * Assert that a page is empty, panic if it isn't.
2663 pmap_page_assertzero(vm_paddr_t phys)
2668 vm_offset_t virt = PHYS_TO_DMAP(phys);
2670 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
2671 if (*(int *)((char *)virt + i) != 0) {
2672 panic("pmap_page_assertzero() @ %p not zero!\n",
2682 * Zero part of a physical page by mapping it into memory and clearing
2683 * its contents with bzero.
2685 * off and size may not cover an area beyond a single hardware page.
2688 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2691 vm_offset_t virt = PHYS_TO_DMAP(phys);
2692 bzero((char *)virt + off, size);
2699 * Copy the physical page from the source PA to the target PA.
2700 * This function may be called from an interrupt. No locking
2704 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2706 vm_offset_t src_virt, dst_virt;
2709 src_virt = PHYS_TO_DMAP(src);
2710 dst_virt = PHYS_TO_DMAP(dst);
2711 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
2716 * pmap_copy_page_frag:
2718 * Copy the physical page from the source PA to the target PA.
2719 * This function may be called from an interrupt. No locking
2723 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2725 vm_offset_t src_virt, dst_virt;
2728 src_virt = PHYS_TO_DMAP(src);
2729 dst_virt = PHYS_TO_DMAP(dst);
2730 bcopy((char *)src_virt + (src & PAGE_MASK),
2731 (char *)dst_virt + (dst & PAGE_MASK),
2737 * Returns true if the pmap's pv is one of the first 16 pvs linked to
2738 * from this page. This count may be changed upwards or downwards
2739 * in the future; it is only necessary that true be returned for a small
2740 * subset of pmaps for proper page aging.
2742 * No other requirements.
2745 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2750 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2754 lwkt_gettoken(&vm_token);
2756 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2757 if (pv->pv_pmap == pmap) {
2758 lwkt_reltoken(&vm_token);
2766 lwkt_reltoken(&vm_token);
2772 * Remove all pages from specified address space this aids process
2773 * exit speeds. Also, this code is special cased for current
2774 * process only, but can have the more generic (and slightly slower)
2775 * mode enabled. This is much faster than pmap_remove in the case
2776 * of running down an entire address space.
2778 * No other requirements.
2781 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2783 pt_entry_t *pte, tpte;
2786 int save_generation;
2789 lwkt_gettoken(&vm_token);
2790 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2791 if (pv->pv_va >= eva || pv->pv_va < sva) {
2792 npv = TAILQ_NEXT(pv, pv_plist);
2796 KKASSERT(pmap == pv->pv_pmap);
2798 pte = pmap_pte(pmap, pv->pv_va);
2801 * We cannot remove wired pages from a process' mapping
2804 if (*pte & VPTE_WIRED) {
2805 npv = TAILQ_NEXT(pv, pv_plist);
2808 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2810 m = PHYS_TO_VM_PAGE(tpte & VPTE_FRAME);
2812 KASSERT(m < &vm_page_array[vm_page_array_size],
2813 ("pmap_remove_pages: bad tpte %lx", tpte));
2815 KKASSERT(pmap->pm_stats.resident_count > 0);
2816 --pmap->pm_stats.resident_count;
2819 * Update the vm_page_t clean and reference bits.
2821 if (tpte & VPTE_M) {
2825 npv = TAILQ_NEXT(pv, pv_plist);
2826 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2827 save_generation = ++pmap->pm_generation;
2829 m->md.pv_list_count--;
2830 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2831 if (TAILQ_EMPTY(&m->md.pv_list))
2832 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2834 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2838 * Restart the scan if we blocked during the unuse or free
2839 * calls and other removals were made.
2841 if (save_generation != pmap->pm_generation) {
2842 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2843 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2846 lwkt_reltoken(&vm_token);
2851 * pmap_testbit tests bits in active mappings of a VM page.
2854 pmap_testbit(vm_page_t m, int bit)
2859 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2862 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2867 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2869 * if the bit being tested is the modified bit, then
2870 * mark clean_map and ptes as never
2873 if (bit & (VPTE_A|VPTE_M)) {
2874 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2878 #if defined(PMAP_DIAGNOSTIC)
2879 if (pv->pv_pmap == NULL) {
2880 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
2884 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2895 * This routine is used to clear bits in ptes. Certain bits require special
2896 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2898 * This routine is only called with certain VPTE_* bit combinations.
2900 static __inline void
2901 pmap_clearbit(vm_page_t m, int bit)
2907 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2913 * Loop over all current mappings setting/clearing as appropos If
2914 * setting RO do we need to clear the VAC?
2916 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2918 * don't write protect pager mappings
2920 if (bit == VPTE_W) {
2921 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2925 #if defined(PMAP_DIAGNOSTIC)
2926 if (pv->pv_pmap == NULL) {
2927 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
2933 * Careful here. We can use a locked bus instruction to
2934 * clear VPTE_A or VPTE_M safely but we need to synchronize
2935 * with the target cpus when we mess with VPTE_W.
2937 * On virtual kernels we must force a new fault-on-write
2938 * in the real kernel if we clear the Modify bit ourselves,
2939 * otherwise the real kernel will not get a new fault and
2940 * will never set our Modify bit again.
2942 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2944 if (bit == VPTE_W) {
2946 * We must also clear VPTE_M when clearing
2949 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2953 } else if (bit == VPTE_M) {
2955 * We do not have to make the page read-only
2956 * when clearing the Modify bit. The real
2957 * kernel will make the real PTE read-only
2958 * or otherwise detect the write and set
2959 * our VPTE_M again simply by us invalidating
2960 * the real kernel VA for the pmap (as we did
2961 * above). This allows the real kernel to
2962 * handle the write fault without forwarding
2965 atomic_clear_long(pte, VPTE_M);
2966 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2968 * We've been asked to clear W & M, I guess
2969 * the caller doesn't want us to update
2970 * the dirty status of the VM page.
2972 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2975 * We've been asked to clear bits that do
2976 * not interact with hardware.
2978 atomic_clear_long(pte, bit);
2986 * Lower the permission for all mappings to a given page.
2988 * No other requirements.
2991 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2993 /* JG NX support? */
2994 if ((prot & VM_PROT_WRITE) == 0) {
2995 lwkt_gettoken(&vm_token);
2996 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2997 pmap_clearbit(m, VPTE_W);
2998 vm_page_flag_clear(m, PG_WRITEABLE);
3002 lwkt_reltoken(&vm_token);
3007 pmap_phys_address(vm_pindex_t ppn)
3009 return (x86_64_ptob(ppn));
3013 * Return a count of reference bits for a page, clearing those bits.
3014 * It is not necessary for every reference bit to be cleared, but it
3015 * is necessary that 0 only be returned when there are truly no
3016 * reference bits set.
3018 * XXX: The exact number of bits to check and clear is a matter that
3019 * should be tested and standardized at some point in the future for
3020 * optimal aging of shared pages.
3022 * No other requirements.
3025 pmap_ts_referenced(vm_page_t m)
3027 pv_entry_t pv, pvf, pvn;
3031 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3035 lwkt_gettoken(&vm_token);
3037 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3042 pvn = TAILQ_NEXT(pv, pv_list);
3044 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3046 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3048 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3051 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3053 if (pte && (*pte & VPTE_A)) {
3055 atomic_clear_long(pte, VPTE_A);
3057 atomic_clear_long_nonlocked(pte, VPTE_A);
3064 } while ((pv = pvn) != NULL && pv != pvf);
3066 lwkt_reltoken(&vm_token);
3073 * Return whether or not the specified physical page was modified
3074 * in any physical maps.
3076 * No other requirements.
3079 pmap_is_modified(vm_page_t m)
3083 lwkt_gettoken(&vm_token);
3084 res = pmap_testbit(m, VPTE_M);
3085 lwkt_reltoken(&vm_token);
3090 * Clear the modify bits on the specified physical page.
3092 * No other requirements.
3095 pmap_clear_modify(vm_page_t m)
3097 lwkt_gettoken(&vm_token);
3098 pmap_clearbit(m, VPTE_M);
3099 lwkt_reltoken(&vm_token);
3103 * Clear the reference bit on the specified physical page.
3105 * No other requirements.
3108 pmap_clear_reference(vm_page_t m)
3110 lwkt_gettoken(&vm_token);
3111 pmap_clearbit(m, VPTE_A);
3112 lwkt_reltoken(&vm_token);
3116 * Miscellaneous support routines follow
3120 i386_protection_init(void)
3124 kp = protection_codes;
3125 for (prot = 0; prot < 8; prot++) {
3126 if (prot & VM_PROT_READ)
3128 if (prot & VM_PROT_WRITE)
3130 if (prot & VM_PROT_EXECUTE)
3137 * Perform the pmap work for mincore
3139 * No other requirements.
3142 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3144 pt_entry_t *ptep, pte;
3148 lwkt_gettoken(&vm_token);
3149 ptep = pmap_pte(pmap, addr);
3151 if (ptep && (pte = *ptep) != 0) {
3154 val = MINCORE_INCORE;
3155 if ((pte & VPTE_MANAGED) == 0)
3158 pa = pte & VPTE_FRAME;
3160 m = PHYS_TO_VM_PAGE(pa);
3166 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3168 * Modified by someone
3170 else if (m->dirty || pmap_is_modified(m))
3171 val |= MINCORE_MODIFIED_OTHER;
3176 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3179 * Referenced by someone
3181 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3182 val |= MINCORE_REFERENCED_OTHER;
3183 vm_page_flag_set(m, PG_REFERENCED);
3187 lwkt_reltoken(&vm_token);
3192 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3193 * vmspace will be ref'd and the old one will be deref'd.
3196 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3198 struct vmspace *oldvm;
3202 oldvm = p->p_vmspace;
3203 if (oldvm != newvm) {
3204 p->p_vmspace = newvm;
3205 KKASSERT(p->p_nthreads == 1);
3206 lp = RB_ROOT(&p->p_lwp_tree);
3207 pmap_setlwpvm(lp, newvm);
3209 sysref_get(&newvm->vm_sysref);
3210 sysref_put(&oldvm->vm_sysref);
3217 * Set the vmspace for a LWP. The vmspace is almost universally set the
3218 * same as the process vmspace, but virtual kernels need to swap out contexts
3219 * on a per-lwp basis.
3222 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3224 struct vmspace *oldvm;
3228 oldvm = lp->lwp_vmspace;
3230 if (oldvm != newvm) {
3231 lp->lwp_vmspace = newvm;
3232 if (curthread->td_lwp == lp) {
3233 pmap = vmspace_pmap(newvm);
3235 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3237 pmap->pm_active |= 1;
3239 #if defined(SWTCH_OPTIM_STATS)
3242 pmap = vmspace_pmap(oldvm);
3244 atomic_clear_int(&pmap->pm_active,
3245 1 << mycpu->gd_cpuid);
3247 pmap->pm_active &= ~1;
3255 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3258 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3262 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);