4 * Copyright (c) 1991 Regents of the University of California.
5 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
7 * Copyright (c) 2003 Peter Wemm
8 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
9 * Copyright (c) 2008, 2009 The DragonFly Project.
10 * Copyright (c) 2008, 2009 Jordan Gordeev.
11 * All rights reserved.
13 * This code is derived from software contributed to Berkeley by
14 * the Systems Programming Group of the University of Utah Computer
15 * Science Department and William Jolitz of UUNET Technologies Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
46 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
50 * Manages physical address maps.
56 #include "opt_msgbuf.h"
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
62 #include <sys/msgbuf.h>
63 #include <sys/vmmeter.h>
65 #include <sys/vmspace.h>
68 #include <vm/vm_param.h>
69 #include <sys/sysctl.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_zone.h>
81 #include <sys/thread2.h>
82 #include <sys/sysref2.h>
83 #include <sys/spinlock2.h>
85 #include <machine/cputypes.h>
86 #include <machine/md_var.h>
87 #include <machine/specialreg.h>
88 #include <machine/smp.h>
89 #include <machine/globaldata.h>
90 #include <machine/pmap.h>
91 #include <machine/pmap_inval.h>
99 #define PMAP_KEEP_PDIRS
100 #ifndef PMAP_SHPGPERPROC
101 #define PMAP_SHPGPERPROC 1000
104 #if defined(DIAGNOSTIC)
105 #define PMAP_DIAGNOSTIC
110 #if !defined(PMAP_DIAGNOSTIC)
111 #define PMAP_INLINE __inline
117 * Get PDEs and PTEs for user/kernel address space
119 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
120 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
122 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & VPTE_V) != 0)
123 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & VPTE_WIRED) != 0)
124 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & VPTE_M) != 0)
125 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & VPTE_A) != 0)
126 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & VPTE_V) != 0)
129 * Given a map and a machine independent protection code,
130 * convert to a vax protection code.
132 #define pte_prot(m, p) \
133 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
134 static int protection_codes[8];
136 struct pmap kernel_pmap;
137 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
139 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
141 static vm_object_t kptobj;
145 static uint64_t KPDphys; /* phys addr of kernel level 2 */
146 uint64_t KPDPphys; /* phys addr of kernel level 3 */
147 uint64_t KPML4phys; /* phys addr of kernel level 4 */
151 * Data for the pv entry allocation mechanism
153 static vm_zone_t pvzone;
154 static struct vm_zone pvzone_store;
155 static struct vm_object pvzone_obj;
156 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
157 static int pmap_pagedaemon_waken = 0;
158 static struct pv_entry *pvinit;
161 * All those kernel PT submaps that BSD is so fond of
163 pt_entry_t *CMAP1 = 0, *ptmmap;
165 static pt_entry_t *msgbufmap;
169 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
170 static pv_entry_t get_pv_entry (void);
171 static void i386_protection_init (void);
172 static __inline void pmap_clearbit (vm_page_t m, int bit);
174 static void pmap_remove_all (vm_page_t m);
175 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
177 static void pmap_remove_page (struct pmap *pmap, vm_offset_t va);
178 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
180 static boolean_t pmap_testbit (vm_page_t m, int bit);
181 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
182 vm_page_t mpte, vm_page_t m);
184 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
186 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
187 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
189 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
191 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
192 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t);
197 * Super fast pmap_pte routine best used when scanning the pv lists.
198 * This eliminates many course-grained invltlb calls. Note that many of
199 * the pv list scans are across different pmaps and it is very wasteful
200 * to do an entire invltlb when checking a single mapping.
202 * Should only be called while in a critical section.
205 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
208 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
210 return pmap_pte(pmap, va);
214 /* Return a non-clipped PD index for a given VA */
215 static __inline vm_pindex_t
216 pmap_pde_pindex(vm_offset_t va)
218 return va >> PDRSHIFT;
221 /* Return various clipped indexes for a given VA */
222 static __inline vm_pindex_t
223 pmap_pte_index(vm_offset_t va)
226 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
229 static __inline vm_pindex_t
230 pmap_pde_index(vm_offset_t va)
233 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
236 static __inline vm_pindex_t
237 pmap_pdpe_index(vm_offset_t va)
240 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
243 static __inline vm_pindex_t
244 pmap_pml4e_index(vm_offset_t va)
247 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
250 /* Return a pointer to the PML4 slot that corresponds to a VA */
251 static __inline pml4_entry_t *
252 pmap_pml4e(pmap_t pmap, vm_offset_t va)
255 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
258 /* Return a pointer to the PDP slot that corresponds to a VA */
259 static __inline pdp_entry_t *
260 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
264 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & VPTE_FRAME);
265 return (&pdpe[pmap_pdpe_index(va)]);
268 /* Return a pointer to the PDP slot that corresponds to a VA */
269 static __inline pdp_entry_t *
270 pmap_pdpe(pmap_t pmap, vm_offset_t va)
274 pml4e = pmap_pml4e(pmap, va);
275 if ((*pml4e & VPTE_V) == 0)
277 return (pmap_pml4e_to_pdpe(pml4e, va));
280 /* Return a pointer to the PD slot that corresponds to a VA */
281 static __inline pd_entry_t *
282 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
286 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & VPTE_FRAME);
287 return (&pde[pmap_pde_index(va)]);
290 /* Return a pointer to the PD slot that corresponds to a VA */
291 static __inline pd_entry_t *
292 pmap_pde(pmap_t pmap, vm_offset_t va)
296 pdpe = pmap_pdpe(pmap, va);
297 if (pdpe == NULL || (*pdpe & VPTE_V) == 0)
299 return (pmap_pdpe_to_pde(pdpe, va));
302 /* Return a pointer to the PT slot that corresponds to a VA */
303 static __inline pt_entry_t *
304 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
308 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & VPTE_FRAME);
309 return (&pte[pmap_pte_index(va)]);
312 /* Return a pointer to the PT slot that corresponds to a VA */
313 static __inline pt_entry_t *
314 pmap_pte(pmap_t pmap, vm_offset_t va)
318 pde = pmap_pde(pmap, va);
319 if (pde == NULL || (*pde & VPTE_V) == 0)
321 if ((*pde & VPTE_PS) != 0) /* compat with i386 pmap_pte() */
322 return ((pt_entry_t *)pde);
323 return (pmap_pde_to_pte(pde, va));
328 PMAP_INLINE pt_entry_t *
329 vtopte(vm_offset_t va)
331 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT +
332 NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
334 return (PTmap + ((va >> PAGE_SHIFT) & mask));
337 static __inline pd_entry_t *
338 vtopde(vm_offset_t va)
340 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT +
341 NPML4EPGSHIFT)) - 1);
343 return (PDmap + ((va >> PDRSHIFT) & mask));
346 static PMAP_INLINE pt_entry_t *
347 vtopte(vm_offset_t va)
350 x = pmap_pte(&kernel_pmap, va);
355 static __inline pd_entry_t *
356 vtopde(vm_offset_t va)
359 x = pmap_pde(&kernel_pmap, va);
366 allocpages(vm_paddr_t *firstaddr, int n)
372 bzero((void *)ret, n * PAGE_SIZE);
374 *firstaddr += n * PAGE_SIZE;
379 create_pagetables(vm_paddr_t *firstaddr, int64_t ptov_offset)
382 pml4_entry_t *KPML4virt;
383 pdp_entry_t *KPDPvirt;
386 int kpml4i = pmap_pml4e_index(ptov_offset);
387 int kpdpi = pmap_pdpe_index(ptov_offset);
390 * Calculate NKPT - number of kernel page tables. We have to
391 * accomodoate prealloction of the vm_page_array, dump bitmap,
392 * MSGBUF_SIZE, and other stuff. Be generous.
394 * Maxmem is in pages.
396 nkpt = (Maxmem * (sizeof(struct vm_page) * 2) + MSGBUF_SIZE) / NBPDR;
401 KPML4phys = allocpages(firstaddr, 1);
402 KPDPphys = allocpages(firstaddr, NKPML4E);
403 KPDphys = allocpages(firstaddr, NKPDPE);
404 KPTphys = allocpages(firstaddr, nkpt);
406 KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
407 KPDPvirt = (pdp_entry_t *)PHYS_TO_DMAP(KPDPphys);
408 KPDvirt = (pd_entry_t *)PHYS_TO_DMAP(KPDphys);
409 KPTvirt = (pt_entry_t *)PHYS_TO_DMAP(KPTphys);
411 bzero(KPML4virt, 1 * PAGE_SIZE);
412 bzero(KPDPvirt, NKPML4E * PAGE_SIZE);
413 bzero(KPDvirt, NKPDPE * PAGE_SIZE);
414 bzero(KPTvirt, nkpt * PAGE_SIZE);
416 /* Now map the page tables at their location within PTmap */
417 for (i = 0; i < nkpt; i++) {
418 KPDvirt[i] = KPTphys + (i << PAGE_SHIFT);
419 KPDvirt[i] |= VPTE_R | VPTE_W | VPTE_V;
422 /* And connect up the PD to the PDP */
423 for (i = 0; i < NKPDPE; i++) {
424 KPDPvirt[i + kpdpi] = KPDphys + (i << PAGE_SHIFT);
425 KPDPvirt[i + kpdpi] |= VPTE_R | VPTE_W | VPTE_V;
428 /* And recursively map PML4 to itself in order to get PTmap */
429 KPML4virt[PML4PML4I] = KPML4phys;
430 KPML4virt[PML4PML4I] |= VPTE_R | VPTE_W | VPTE_V;
432 /* Connect the KVA slot up to the PML4 */
433 KPML4virt[kpml4i] = KPDPphys;
434 KPML4virt[kpml4i] |= VPTE_R | VPTE_W | VPTE_V;
438 * Bootstrap the system enough to run with virtual memory.
440 * On the i386 this is called after mapping has already been enabled
441 * and just syncs the pmap module with what has already been done.
442 * [We can't call it easily with mapping off since the kernel is not
443 * mapped with PA == VA, hence we would have to relocate every address
444 * from the linked base (virtual) address "KERNBASE" to the actual
445 * (physical) address starting relative to 0]
448 pmap_bootstrap(vm_paddr_t *firstaddr, int64_t ptov_offset)
454 * Create an initial set of page tables to run the kernel in.
456 create_pagetables(firstaddr, ptov_offset);
458 virtual_start = KvaStart + *firstaddr;
459 virtual_end = KvaEnd;
462 * Initialize protection array.
464 i386_protection_init();
467 * The kernel's pmap is statically allocated so we don't have to use
468 * pmap_create, which is unlikely to work correctly at this part of
469 * the boot sequence (XXX and which no longer exists).
471 * The kernel_pmap's pm_pteobj is used only for locking and not
474 kernel_pmap.pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
475 kernel_pmap.pm_count = 1;
476 kernel_pmap.pm_active = (cpumask_t)-1; /* don't allow deactivation */
477 kernel_pmap.pm_pteobj = &kernel_object;
478 TAILQ_INIT(&kernel_pmap.pm_pvlist);
479 TAILQ_INIT(&kernel_pmap.pm_pvlist_free);
480 lwkt_token_init(&kernel_pmap.pm_token, "kpmap_tok");
481 spin_init(&kernel_pmap.pm_spin);
484 * Reserve some special page table entries/VA space for temporary
487 #define SYSMAP(c, p, v, n) \
488 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
491 pte = pmap_pte(&kernel_pmap, va);
494 * CMAP1/CMAP2 are used for zeroing and copying pages.
496 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
502 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
506 * ptvmmap is used for reading arbitrary physical pages via
509 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
512 * msgbufp is used to map the system message buffer.
513 * XXX msgbufmap is not used.
515 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
516 atop(round_page(MSGBUF_SIZE)))
526 * Initialize the pmap module.
527 * Called by vm_init, to initialize any structures that the pmap
528 * system needs to map virtual memory.
529 * pmap_init has been enhanced to support in a fairly consistant
530 * way, discontiguous physical memory.
539 * object for kernel page table pages
541 /* JG I think the number can be arbitrary */
542 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
545 * Allocate memory for random pmap data structures. Includes the
549 for(i = 0; i < vm_page_array_size; i++) {
552 m = &vm_page_array[i];
553 TAILQ_INIT(&m->md.pv_list);
554 m->md.pv_list_count = 0;
558 * init the pv free list
560 initial_pvs = vm_page_array_size;
561 if (initial_pvs < MINPV)
563 pvzone = &pvzone_store;
564 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
565 initial_pvs * sizeof (struct pv_entry));
566 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
570 * Now it is safe to enable pv_table recording.
572 pmap_initialized = TRUE;
576 * Initialize the address space (zone) for the pv_entries. Set a
577 * high water mark so that the system can recover from excessive
578 * numbers of pv entries.
583 int shpgperproc = PMAP_SHPGPERPROC;
585 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
586 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
587 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
588 pv_entry_high_water = 9 * (pv_entry_max / 10);
589 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
593 /***************************************************
594 * Low level helper routines.....
595 ***************************************************/
598 * The modification bit is not tracked for any pages in this range. XXX
599 * such pages in this maps should always use pmap_k*() functions and not
602 * XXX User and kernel address spaces are independant for virtual kernels,
603 * this function only applies to the kernel pmap.
606 pmap_track_modified(pmap_t pmap, vm_offset_t va)
608 if (pmap != &kernel_pmap)
610 if ((va < clean_sva) || (va >= clean_eva))
617 * Extract the physical page address associated with the map/VA pair.
622 pmap_extract(pmap_t pmap, vm_offset_t va)
626 pd_entry_t pde, *pdep;
628 lwkt_gettoken(&vm_token);
630 pdep = pmap_pde(pmap, va);
634 if ((pde & VPTE_PS) != 0) {
636 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
638 pte = pmap_pde_to_pte(pdep, va);
639 rtval = (*pte & VPTE_FRAME) | (va & PAGE_MASK);
643 lwkt_reltoken(&vm_token);
648 * Routine: pmap_kextract
650 * Extract the physical page address associated
651 * kernel virtual address.
654 pmap_kextract(vm_offset_t va)
659 KKASSERT(va >= KvaStart && va < KvaEnd);
662 * The DMAP region is not included in [KvaStart, KvaEnd)
665 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
666 pa = DMAP_TO_PHYS(va);
672 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
675 * Beware of a concurrent promotion that changes the
676 * PDE at this point! For example, vtopte() must not
677 * be used to access the PTE because it would use the
678 * new PDE. It is, however, safe to use the old PDE
679 * because the page table page is preserved by the
682 pa = *pmap_pde_to_pte(&pde, va);
683 pa = (pa & VPTE_FRAME) | (va & PAGE_MASK);
691 /***************************************************
692 * Low level mapping routines.....
693 ***************************************************/
696 * Enter a mapping into kernel_pmap. Mappings created in this fashion
697 * are not managed. Mappings must be immediately accessible on all cpus.
699 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
700 * real pmap and handle related races before storing the new vpte.
703 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
708 KKASSERT(va >= KvaStart && va < KvaEnd);
709 npte = pa | VPTE_R | VPTE_W | VPTE_V;
712 pmap_inval_pte(pte, &kernel_pmap, va);
717 * Enter an unmanaged KVA mapping for the private use of the current
718 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
721 * It is illegal for the mapping to be accessed by other cpus unleess
722 * pmap_kenter_sync*() is called.
725 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
730 KKASSERT(va >= KvaStart && va < KvaEnd);
732 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
735 pmap_inval_pte_quick(pte, &kernel_pmap, va);
737 //cpu_invlpg((void *)va);
741 * Synchronize a kvm mapping originally made for the private use on
742 * some other cpu so it can be used on all cpus.
744 * XXX add MADV_RESYNC to improve performance.
747 pmap_kenter_sync(vm_offset_t va)
749 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
753 * Synchronize a kvm mapping originally made for the private use on
754 * some other cpu so it can be used on our cpu. Turns out to be the
755 * same madvise() call, because we have to sync the real pmaps anyway.
757 * XXX add MADV_RESYNC to improve performance.
760 pmap_kenter_sync_quick(vm_offset_t va)
762 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
766 * Remove an unmanaged mapping created with pmap_kenter*().
769 pmap_kremove(vm_offset_t va)
773 KKASSERT(va >= KvaStart && va < KvaEnd);
777 pmap_inval_pte(pte, &kernel_pmap, va);
782 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
783 * only with this cpu.
785 * Unfortunately because we optimize new entries by testing VPTE_V later
786 * on, we actually still have to synchronize with all the cpus. XXX maybe
787 * store a junk value and test against 0 in the other places instead?
790 pmap_kremove_quick(vm_offset_t va)
794 KKASSERT(va >= KvaStart && va < KvaEnd);
798 pmap_inval_pte(pte, &kernel_pmap, va); /* NOT _quick */
803 * Used to map a range of physical addresses into kernel
804 * virtual address space.
806 * For now, VM is already on, we only need to map the
810 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
812 return PHYS_TO_DMAP(start);
817 * Map a set of unmanaged VM pages into KVM.
820 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
824 end_va = va + count * PAGE_SIZE;
825 KKASSERT(va >= KvaStart && end_va < KvaEnd);
827 while (va < end_va) {
832 pmap_inval_pte(pte, &kernel_pmap, va);
833 *pte = VM_PAGE_TO_PHYS(*m) | VPTE_R | VPTE_W | VPTE_V;
840 * Undo the effects of pmap_qenter*().
843 pmap_qremove(vm_offset_t va, int count)
847 end_va = va + count * PAGE_SIZE;
848 KKASSERT(va >= KvaStart && end_va < KvaEnd);
850 while (va < end_va) {
855 pmap_inval_pte(pte, &kernel_pmap, va);
862 * This routine works like vm_page_lookup() but also blocks as long as the
863 * page is busy. This routine does not busy the page it returns.
865 * Unless the caller is managing objects whos pages are in a known state,
866 * the call should be made with a critical section held so the page's object
867 * association remains valid on return.
870 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
874 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
875 m = vm_page_lookup_busy_wait(object, pindex, FALSE, "pplookp");
881 * Create a new thread and optionally associate it with a (new) process.
882 * NOTE! the new thread's cpu may not equal the current cpu.
885 pmap_init_thread(thread_t td)
887 /* enforce pcb placement */
888 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
889 td->td_savefpu = &td->td_pcb->pcb_save;
890 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */
894 * This routine directly affects the fork perf for a process.
897 pmap_init_proc(struct proc *p)
901 /***************************************************
902 * Page table page management routines.....
903 ***************************************************/
905 static __inline int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va,
909 * This routine unholds page table pages, and if the hold count
910 * drops to zero, then it decrements the wire count.
912 * We must recheck that this is the last hold reference after busy-sleeping
916 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
918 vm_page_busy_wait(m, FALSE, "pmuwpt");
919 KASSERT(m->queue == PQ_NONE,
920 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
922 if (m->hold_count == 1) {
924 * Unmap the page table page.
927 /* pmap_inval_add(info, pmap, -1); */
929 if (m->pindex >= (NUPDE + NUPDPE)) {
932 pml4 = pmap_pml4e(pmap, va);
934 } else if (m->pindex >= NUPDE) {
937 pdp = pmap_pdpe(pmap, va);
942 pd = pmap_pde(pmap, va);
946 KKASSERT(pmap->pm_stats.resident_count > 0);
947 --pmap->pm_stats.resident_count;
949 if (pmap->pm_ptphint == m)
950 pmap->pm_ptphint = NULL;
952 if (m->pindex < NUPDE) {
953 /* We just released a PT, unhold the matching PD */
956 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & VPTE_FRAME);
957 pmap_unwire_pte_hold(pmap, va, pdpg);
959 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
960 /* We just released a PD, unhold the matching PDP */
963 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & VPTE_FRAME);
964 pmap_unwire_pte_hold(pmap, va, pdppg);
968 * This was our last hold, the page had better be unwired
969 * after we decrement wire_count.
971 * FUTURE NOTE: shared page directory page could result in
972 * multiple wire counts.
976 KKASSERT(m->wire_count == 0);
977 atomic_add_int(&vmstats.v_wire_count, -1);
978 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
980 vm_page_free_zero(m);
983 KKASSERT(m->hold_count > 1);
991 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
993 KKASSERT(m->hold_count > 0);
994 if (m->hold_count > 1) {
998 return _pmap_unwire_pte_hold(pmap, va, m);
1003 * After removing a page table entry, this routine is used to
1004 * conditionally free the page, and manage the hold/wire counts.
1007 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1009 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1010 vm_pindex_t ptepindex;
1012 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
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;
1027 vm_page_wakeup(mpte);
1031 return pmap_unwire_pte_hold(pmap, va, mpte);
1035 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
1036 * just dummy it up so it works well enough for fork().
1038 * In DragonFly, process pmaps may only be used to manipulate user address
1039 * space, never kernel address space.
1042 pmap_pinit0(struct pmap *pmap)
1048 * Initialize a preallocated and zeroed pmap structure,
1049 * such as one in a vmspace structure.
1052 pmap_pinit(struct pmap *pmap)
1057 * No need to allocate page table space yet but we do need a valid
1058 * page directory table.
1060 if (pmap->pm_pml4 == NULL) {
1062 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1066 * Allocate an object for the ptes
1068 if (pmap->pm_pteobj == NULL)
1069 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1072 * Allocate the page directory page, unless we already have
1073 * one cached. If we used the cached page the wire_count will
1074 * already be set appropriately.
1076 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1077 ptdpg = vm_page_grab(pmap->pm_pteobj,
1078 NUPDE + NUPDPE + PML4PML4I,
1079 VM_ALLOC_NORMAL | VM_ALLOC_RETRY |
1081 pmap->pm_pdirm = ptdpg;
1082 vm_page_flag_clear(ptdpg, PG_MAPPED);
1083 if (ptdpg->wire_count == 0)
1084 atomic_add_int(&vmstats.v_wire_count, 1);
1085 ptdpg->wire_count = 1;
1086 vm_page_wakeup(ptdpg);
1087 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1090 pmap->pm_active = 0;
1091 pmap->pm_ptphint = NULL;
1092 TAILQ_INIT(&pmap->pm_pvlist);
1093 TAILQ_INIT(&pmap->pm_pvlist_free);
1094 spin_init(&pmap->pm_spin);
1095 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1096 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1097 pmap->pm_stats.resident_count = 1;
1101 * Clean up a pmap structure so it can be physically freed. This routine
1102 * is called by the vmspace dtor function. A great deal of pmap data is
1103 * left passively mapped to improve vmspace management so we have a bit
1104 * of cleanup work to do here.
1109 pmap_puninit(pmap_t pmap)
1113 KKASSERT(pmap->pm_active == 0);
1114 if ((p = pmap->pm_pdirm) != NULL) {
1115 KKASSERT(pmap->pm_pml4 != NULL);
1116 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1117 vm_page_busy_wait(p, FALSE, "pgpun");
1119 atomic_add_int(&vmstats.v_wire_count, -1);
1120 vm_page_free_zero(p);
1121 pmap->pm_pdirm = NULL;
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)
1146 spin_lock(&pmap_spin);
1147 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1148 spin_unlock(&pmap_spin);
1152 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1153 * 0 on failure (if the procedure had to sleep).
1155 * When asked to remove the page directory page itself, we actually just
1156 * leave it cached so we do not have to incur the SMP inval overhead of
1157 * removing the kernel mapping. pmap_puninit() will take care of it.
1160 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1163 * This code optimizes the case of freeing non-busy
1164 * page-table pages. Those pages are zero now, and
1165 * might as well be placed directly into the zero queue.
1167 if (vm_page_busy_try(p, FALSE)) {
1168 vm_page_sleep_busy(p, FALSE, "pmaprl");
1173 * Remove the page table page from the processes address space.
1175 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1177 * We are the pml4 table itself.
1179 /* XXX anything to do here? */
1180 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1182 * We are a PDP page.
1183 * We look for the PML4 entry that points to us.
1185 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1186 KKASSERT(m4 != NULL);
1187 pml4_entry_t *pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1188 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1189 KKASSERT(pml4[idx] != 0);
1192 /* JG What about wire_count? */
1193 } else if (p->pindex >= NUPDE) {
1196 * We look for the PDP entry that points to us.
1198 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1199 KKASSERT(m3 != NULL);
1200 pdp_entry_t *pdp = (pdp_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1201 int idx = (p->pindex - NUPDE) % NPDPEPG;
1202 KKASSERT(pdp[idx] != 0);
1205 /* JG What about wire_count? */
1207 /* We are a PT page.
1208 * We look for the PD entry that points to us.
1210 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1211 KKASSERT(m2 != NULL);
1212 pd_entry_t *pd = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1213 int idx = p->pindex % NPDEPG;
1216 /* JG What about wire_count? */
1218 KKASSERT(pmap->pm_stats.resident_count > 0);
1219 --pmap->pm_stats.resident_count;
1221 if (p->hold_count) {
1222 panic("pmap_release: freeing held page table page");
1224 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1225 pmap->pm_ptphint = NULL;
1228 * We leave the top-level page table page cached, wired, and mapped in
1229 * the pmap until the dtor function (pmap_puninit()) gets called.
1230 * However, still clean it up so we can set PG_ZERO.
1232 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1233 bzero(pmap->pm_pml4, PAGE_SIZE);
1234 vm_page_flag_set(p, PG_ZERO);
1239 atomic_add_int(&vmstats.v_wire_count, -1);
1240 /* JG eventually revert to using vm_page_free_zero() */
1247 * this routine is called if the page table page is not
1251 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1253 vm_page_t m, pdppg, pdpg;
1256 * Find or fabricate a new pagetable page. Handle allocation
1257 * races by checking m->valid.
1259 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1260 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1262 KASSERT(m->queue == PQ_NONE,
1263 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1266 * Increment the hold count for the page we will be returning to
1271 if (m->wire_count == 0)
1272 atomic_add_int(&vmstats.v_wire_count, 1);
1276 * Map the pagetable page into the process address space, if
1277 * it isn't already there.
1279 ++pmap->pm_stats.resident_count;
1281 if (ptepindex >= (NUPDE + NUPDPE)) {
1283 vm_pindex_t pml4index;
1285 /* Wire up a new PDP page */
1286 pml4index = ptepindex - (NUPDE + NUPDPE);
1287 pml4 = &pmap->pm_pml4[pml4index];
1288 *pml4 = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1290 } else if (ptepindex >= NUPDE) {
1291 vm_pindex_t pml4index;
1292 vm_pindex_t pdpindex;
1296 /* Wire up a new PD page */
1297 pdpindex = ptepindex - NUPDE;
1298 pml4index = pdpindex >> NPML4EPGSHIFT;
1300 pml4 = &pmap->pm_pml4[pml4index];
1301 if ((*pml4 & VPTE_V) == 0) {
1302 /* Have to allocate a new PDP page, recurse */
1303 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1310 /* Add reference to the PDP page */
1311 pdppg = PHYS_TO_VM_PAGE(*pml4 & VPTE_FRAME);
1312 pdppg->hold_count++;
1314 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1316 /* Now find the pdp page */
1317 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1318 KKASSERT(*pdp == 0); /* JG DEBUG64 */
1319 *pdp = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1322 vm_pindex_t pml4index;
1323 vm_pindex_t pdpindex;
1328 /* Wire up a new PT page */
1329 pdpindex = ptepindex >> NPDPEPGSHIFT;
1330 pml4index = pdpindex >> NPML4EPGSHIFT;
1332 /* First, find the pdp and check that its valid. */
1333 pml4 = &pmap->pm_pml4[pml4index];
1334 if ((*pml4 & VPTE_V) == 0) {
1335 /* We miss a PDP page. We ultimately need a PD page.
1336 * Recursively allocating a PD page will allocate
1337 * the missing PDP page and will also allocate
1338 * the PD page we need.
1340 /* Have to allocate a new PD page, recurse */
1341 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1347 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1348 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1350 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1351 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1352 if ((*pdp & VPTE_V) == 0) {
1353 /* Have to allocate a new PD page, recurse */
1354 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1361 /* Add reference to the PD page */
1362 pdpg = PHYS_TO_VM_PAGE(*pdp & VPTE_FRAME);
1366 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & VPTE_FRAME);
1368 /* Now we know where the page directory page is */
1369 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1370 KKASSERT(*pd == 0); /* JG DEBUG64 */
1371 *pd = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1376 * Set the page table hint
1378 pmap->pm_ptphint = m;
1379 vm_page_flag_set(m, PG_MAPPED);
1386 * Determine the page table page required to access the VA in the pmap
1387 * and allocate it if necessary. Return a held vm_page_t for the page.
1389 * Only used with user pmaps.
1392 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1394 vm_pindex_t ptepindex;
1398 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1401 * Calculate pagetable page index
1403 ptepindex = pmap_pde_pindex(va);
1406 * Get the page directory entry
1408 pd = pmap_pde(pmap, va);
1411 * This supports switching from a 2MB page to a
1414 if (pd != NULL && (*pd & (VPTE_PS | VPTE_V)) == (VPTE_PS | VPTE_V)) {
1415 panic("no promotion/demotion yet");
1423 * If the page table page is mapped, we just increment the
1424 * hold count, and activate it.
1426 if (pd != NULL && (*pd & VPTE_V) != 0) {
1427 /* YYY hint is used here on i386 */
1428 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1429 pmap->pm_ptphint = m;
1435 * Here if the pte page isn't mapped, or if it has been deallocated.
1437 return _pmap_allocpte(pmap, ptepindex);
1441 /***************************************************
1442 * Pmap allocation/deallocation routines.
1443 ***************************************************/
1446 * Release any resources held by the given physical map.
1447 * Called when a pmap initialized by pmap_pinit is being released.
1448 * Should only be called if the map contains no valid mappings.
1450 * Caller must hold pmap->pm_token
1452 static int pmap_release_callback(struct vm_page *p, void *data);
1455 pmap_release(struct pmap *pmap)
1457 vm_object_t object = pmap->pm_pteobj;
1458 struct rb_vm_page_scan_info info;
1460 KKASSERT(pmap != &kernel_pmap);
1462 #if defined(DIAGNOSTIC)
1463 if (object->ref_count != 1)
1464 panic("pmap_release: pteobj reference count != 1");
1468 info.object = object;
1470 spin_lock(&pmap_spin);
1471 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1472 spin_unlock(&pmap_spin);
1474 vm_object_hold(object);
1478 info.limit = object->generation;
1480 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1481 pmap_release_callback, &info);
1482 if (info.error == 0 && info.mpte) {
1483 if (!pmap_release_free_page(pmap, info.mpte))
1486 } while (info.error);
1487 vm_object_drop(object);
1491 pmap_release_callback(struct vm_page *p, void *data)
1493 struct rb_vm_page_scan_info *info = data;
1495 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1499 if (!pmap_release_free_page(info->pmap, p)) {
1503 if (info->object->generation != info->limit) {
1511 * Grow the number of kernel page table entries, if needed.
1516 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
1520 vm_offset_t ptppaddr;
1522 pd_entry_t *pde, newpdir;
1527 vm_object_hold(kptobj);
1528 if (kernel_vm_end == 0) {
1529 kernel_vm_end = KvaStart;
1531 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & VPTE_V) != 0) {
1532 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1534 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1535 kernel_vm_end = kernel_map.max_offset;
1540 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1541 if (addr - 1 >= kernel_map.max_offset)
1542 addr = kernel_map.max_offset;
1543 while (kernel_vm_end < addr) {
1544 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1546 /* We need a new PDP entry */
1547 nkpg = vm_page_alloc(kptobj, nkpt,
1548 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1549 | VM_ALLOC_INTERRUPT);
1551 panic("pmap_growkernel: no memory to "
1554 paddr = VM_PAGE_TO_PHYS(nkpg);
1555 if ((nkpg->flags & PG_ZERO) == 0)
1556 pmap_zero_page(paddr);
1557 vm_page_flag_clear(nkpg, PG_ZERO);
1558 newpdp = (pdp_entry_t)(paddr | VPTE_V | VPTE_R |
1559 VPTE_W | VPTE_A | VPTE_M);
1560 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1562 continue; /* try again */
1564 if ((*pde & VPTE_V) != 0) {
1565 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1566 ~(PAGE_SIZE * NPTEPG - 1);
1567 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1568 kernel_vm_end = kernel_map.max_offset;
1575 * This index is bogus, but out of the way
1577 nkpg = vm_page_alloc(kptobj, nkpt,
1580 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 |
1589 VPTE_W | VPTE_A | VPTE_M);
1590 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1593 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1594 ~(PAGE_SIZE * NPTEPG - 1);
1595 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1596 kernel_vm_end = kernel_map.max_offset;
1600 vm_object_drop(kptobj);
1604 * Retire the given physical map from service. Should only be called
1605 * if the map contains no valid mappings.
1610 pmap_destroy(pmap_t pmap)
1615 lwkt_gettoken(&vm_token);
1616 if (--pmap->pm_count == 0) {
1618 panic("destroying a pmap is not yet implemented");
1620 lwkt_reltoken(&vm_token);
1624 * Add a reference to the specified pmap.
1629 pmap_reference(pmap_t pmap)
1632 lwkt_gettoken(&vm_token);
1634 lwkt_reltoken(&vm_token);
1638 /************************************************************************
1639 * VMSPACE MANAGEMENT *
1640 ************************************************************************
1642 * The VMSPACE management we do in our virtual kernel must be reflected
1643 * in the real kernel. This is accomplished by making vmspace system
1644 * calls to the real kernel.
1647 cpu_vmspace_alloc(struct vmspace *vm)
1653 #define USER_SIZE (VM_MAX_USER_ADDRESS - VM_MIN_USER_ADDRESS)
1655 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
1656 panic("vmspace_create() failed");
1658 rp = vmspace_mmap(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1659 PROT_READ|PROT_WRITE,
1660 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
1662 if (rp == MAP_FAILED)
1663 panic("vmspace_mmap: failed");
1664 vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1666 vpte = VM_PAGE_TO_PHYS(vmspace_pmap(vm)->pm_pdirm) | VPTE_R | VPTE_W | VPTE_V;
1667 r = vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1670 panic("vmspace_mcontrol: failed");
1674 cpu_vmspace_free(struct vmspace *vm)
1676 if (vmspace_destroy(&vm->vm_pmap) < 0)
1677 panic("vmspace_destroy() failed");
1680 /***************************************************
1681 * page management routines.
1682 ***************************************************/
1685 * free the pv_entry back to the free list. This function may be
1686 * called from an interrupt.
1688 static __inline void
1689 free_pv_entry(pv_entry_t pv)
1692 KKASSERT(pv_entry_count >= 0);
1697 * get a new pv_entry, allocating a block from the system
1698 * when needed. This function may be called from an interrupt.
1704 if (pv_entry_high_water &&
1705 (pv_entry_count > pv_entry_high_water) &&
1706 (pmap_pagedaemon_waken == 0)) {
1707 pmap_pagedaemon_waken = 1;
1708 wakeup(&vm_pages_needed);
1710 return zalloc(pvzone);
1714 * This routine is very drastic, but can save the system
1724 static int warningdone=0;
1726 if (pmap_pagedaemon_waken == 0)
1728 lwkt_gettoken(&vm_token);
1729 pmap_pagedaemon_waken = 0;
1731 if (warningdone < 5) {
1732 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1736 for (i = 0; i < vm_page_array_size; i++) {
1737 m = &vm_page_array[i];
1738 if (m->wire_count || m->hold_count)
1740 if (vm_page_busy_try(m, TRUE) == 0) {
1741 if (m->wire_count == 0 && m->hold_count == 0) {
1747 lwkt_reltoken(&vm_token);
1752 * If it is the first entry on the list, it is actually
1753 * in the header and we must copy the following entry up
1754 * to the header. Otherwise we must search the list for
1755 * the entry. In either case we free the now unused entry.
1757 * caller must hold vm_token.
1760 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1765 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1766 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1767 if (pmap == pv->pv_pmap && va == pv->pv_va)
1771 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1772 if (va == pv->pv_va)
1778 * Note that pv_ptem is NULL if the page table page itself is not
1779 * managed, even if the page being removed IS managed.
1782 /* JGXXX When can 'pv' be NULL? */
1784 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1785 m->md.pv_list_count--;
1786 atomic_add_int(&m->object->agg_pv_list_count, -1);
1787 KKASSERT(m->md.pv_list_count >= 0);
1788 if (TAILQ_EMPTY(&m->md.pv_list))
1789 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1790 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1791 ++pmap->pm_generation;
1792 KKASSERT(pmap->pm_pteobj != NULL);
1793 vm_object_hold(pmap->pm_pteobj);
1794 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1795 vm_object_drop(pmap->pm_pteobj);
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++;
1819 atomic_add_int(&m->object->agg_pv_list_count, 1);
1825 * pmap_remove_pte: do the things to unmap a page in a process
1828 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1833 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1834 if (oldpte & VPTE_WIRED)
1835 --pmap->pm_stats.wired_count;
1836 KKASSERT(pmap->pm_stats.wired_count >= 0);
1840 * Machines that don't support invlpg, also don't support
1841 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1845 cpu_invlpg((void *)va);
1847 KKASSERT(pmap->pm_stats.resident_count > 0);
1848 --pmap->pm_stats.resident_count;
1849 if (oldpte & VPTE_MANAGED) {
1850 m = PHYS_TO_VM_PAGE(oldpte);
1851 if (oldpte & VPTE_M) {
1852 #if defined(PMAP_DIAGNOSTIC)
1853 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1854 kprintf("pmap_remove: modified page not "
1855 "writable: va: 0x%lx, pte: 0x%lx\n",
1859 if (pmap_track_modified(pmap, va))
1862 if (oldpte & VPTE_A)
1863 vm_page_flag_set(m, PG_REFERENCED);
1864 return pmap_remove_entry(pmap, m, va);
1866 return pmap_unuse_pt(pmap, va, NULL);
1875 * Remove a single page from a process address space.
1877 * This function may not be called from an interrupt if the pmap is
1881 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1885 pte = pmap_pte(pmap, va);
1888 if ((*pte & VPTE_V) == 0)
1890 pmap_remove_pte(pmap, pte, va);
1894 * Remove the given range of addresses from the specified map.
1896 * It is assumed that the start and end are properly rounded to
1899 * This function may not be called from an interrupt if the pmap is
1905 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1907 vm_offset_t va_next;
1908 pml4_entry_t *pml4e;
1910 pd_entry_t ptpaddr, *pde;
1916 vm_object_hold(pmap->pm_pteobj);
1917 lwkt_gettoken(&vm_token);
1918 KKASSERT(pmap->pm_stats.resident_count >= 0);
1919 if (pmap->pm_stats.resident_count == 0) {
1920 lwkt_reltoken(&vm_token);
1921 vm_object_drop(pmap->pm_pteobj);
1926 * special handling of removing one page. a very
1927 * common operation and easy to short circuit some
1930 if (sva + PAGE_SIZE == eva) {
1931 pde = pmap_pde(pmap, sva);
1932 if (pde && (*pde & VPTE_PS) == 0) {
1933 pmap_remove_page(pmap, sva);
1934 lwkt_reltoken(&vm_token);
1935 vm_object_drop(pmap->pm_pteobj);
1940 for (; sva < eva; sva = va_next) {
1941 pml4e = pmap_pml4e(pmap, sva);
1942 if ((*pml4e & VPTE_V) == 0) {
1943 va_next = (sva + NBPML4) & ~PML4MASK;
1949 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
1950 if ((*pdpe & VPTE_V) == 0) {
1951 va_next = (sva + NBPDP) & ~PDPMASK;
1958 * Calculate index for next page table.
1960 va_next = (sva + NBPDR) & ~PDRMASK;
1964 pde = pmap_pdpe_to_pde(pdpe, sva);
1968 * Weed out invalid mappings.
1974 * Check for large page.
1976 if ((ptpaddr & VPTE_PS) != 0) {
1977 /* JG FreeBSD has more complex treatment here */
1978 KKASSERT(*pde != 0);
1979 pmap_inval_pde(pde, pmap, sva);
1980 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1985 * Limit our scan to either the end of the va represented
1986 * by the current page table page, or to the end of the
1987 * range being removed.
1993 * NOTE: pmap_remove_pte() can block.
1995 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1999 if (pmap_remove_pte(pmap, pte, sva))
2003 lwkt_reltoken(&vm_token);
2004 vm_object_drop(pmap->pm_pteobj);
2008 * Removes this physical page from all physical maps in which it resides.
2009 * Reflects back modify bits to the pager.
2011 * This routine may not be called from an interrupt.
2016 pmap_remove_all(vm_page_t m)
2018 pt_entry_t *pte, tpte;
2021 #if defined(PMAP_DIAGNOSTIC)
2023 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
2026 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
2027 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
2031 lwkt_gettoken(&vm_token);
2032 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2033 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2034 --pv->pv_pmap->pm_stats.resident_count;
2036 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2037 KKASSERT(pte != NULL);
2039 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
2040 if (tpte & VPTE_WIRED)
2041 pv->pv_pmap->pm_stats.wired_count--;
2042 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
2045 vm_page_flag_set(m, PG_REFERENCED);
2048 * Update the vm_page_t clean and reference bits.
2050 if (tpte & VPTE_M) {
2051 #if defined(PMAP_DIAGNOSTIC)
2052 if (pmap_nw_modified(tpte)) {
2054 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2058 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
2061 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2062 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2063 ++pv->pv_pmap->pm_generation;
2064 m->md.pv_list_count--;
2065 atomic_add_int(&m->object->agg_pv_list_count, -1);
2066 KKASSERT(m->md.pv_list_count >= 0);
2067 if (TAILQ_EMPTY(&m->md.pv_list))
2068 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2069 vm_object_hold(pv->pv_pmap->pm_pteobj);
2070 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2071 vm_object_drop(pv->pv_pmap->pm_pteobj);
2074 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2075 lwkt_reltoken(&vm_token);
2079 * Set the physical protection on the specified range of this map
2082 * This function may not be called from an interrupt if the map is
2083 * not the kernel_pmap.
2088 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2090 vm_offset_t va_next;
2091 pml4_entry_t *pml4e;
2093 pd_entry_t ptpaddr, *pde;
2096 /* JG review for NX */
2101 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2102 pmap_remove(pmap, sva, eva);
2106 if (prot & VM_PROT_WRITE)
2109 lwkt_gettoken(&vm_token);
2111 for (; sva < eva; sva = va_next) {
2113 pml4e = pmap_pml4e(pmap, sva);
2114 if ((*pml4e & VPTE_V) == 0) {
2115 va_next = (sva + NBPML4) & ~PML4MASK;
2121 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2122 if ((*pdpe & VPTE_V) == 0) {
2123 va_next = (sva + NBPDP) & ~PDPMASK;
2129 va_next = (sva + NBPDR) & ~PDRMASK;
2133 pde = pmap_pdpe_to_pde(pdpe, sva);
2137 * Check for large page.
2139 if ((ptpaddr & VPTE_PS) != 0) {
2141 pmap_clean_pde(pde, pmap, sva);
2142 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2147 * Weed out invalid mappings. Note: we assume that the page
2148 * directory table is always allocated, and in kernel virtual.
2156 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2162 * Clean managed pages and also check the accessed
2163 * bit. Just remove write perms for unmanaged
2164 * pages. Be careful of races, turning off write
2165 * access will force a fault rather then setting
2166 * the modified bit at an unexpected time.
2168 if (*pte & VPTE_MANAGED) {
2169 pbits = pmap_clean_pte(pte, pmap, sva);
2171 if (pbits & VPTE_A) {
2172 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2173 vm_page_flag_set(m, PG_REFERENCED);
2174 atomic_clear_long(pte, VPTE_A);
2176 if (pbits & VPTE_M) {
2177 if (pmap_track_modified(pmap, sva)) {
2179 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2184 pbits = pmap_setro_pte(pte, pmap, sva);
2188 lwkt_reltoken(&vm_token);
2192 * Enter a managed page into a pmap. If the page is not wired related pmap
2193 * data can be destroyed at any time for later demand-operation.
2195 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
2196 * specified protection, and wire the mapping if requested.
2198 * NOTE: This routine may not lazy-evaluate or lose information. The
2199 * page must actually be inserted into the given map NOW.
2201 * NOTE: When entering a page at a KVA address, the pmap must be the
2207 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2214 pt_entry_t origpte, newpte;
2220 va = trunc_page(va);
2222 vm_object_hold(pmap->pm_pteobj);
2223 lwkt_gettoken(&vm_token);
2226 * Get the page table page. The kernel_pmap's page table pages
2227 * are preallocated and have no associated vm_page_t.
2229 if (pmap == &kernel_pmap)
2232 mpte = pmap_allocpte(pmap, va);
2234 pde = pmap_pde(pmap, va);
2235 if (pde != NULL && (*pde & VPTE_V) != 0) {
2236 if ((*pde & VPTE_PS) != 0)
2237 panic("pmap_enter: attempted pmap_enter on 2MB page");
2238 pte = pmap_pde_to_pte(pde, va);
2240 panic("pmap_enter: invalid page directory va=%#lx", va);
2243 KKASSERT(pte != NULL);
2245 * Deal with races on the original mapping (though don't worry
2246 * about VPTE_A races) by cleaning it. This will force a fault
2247 * if an attempt is made to write to the page.
2249 pa = VM_PAGE_TO_PHYS(m);
2250 origpte = pmap_clean_pte(pte, pmap, va);
2251 opa = origpte & VPTE_FRAME;
2253 if (origpte & VPTE_PS)
2254 panic("pmap_enter: attempted pmap_enter on 2MB page");
2257 * Mapping has not changed, must be protection or wiring change.
2259 if (origpte && (opa == pa)) {
2261 * Wiring change, just update stats. We don't worry about
2262 * wiring PT pages as they remain resident as long as there
2263 * are valid mappings in them. Hence, if a user page is wired,
2264 * the PT page will be also.
2266 if (wired && ((origpte & VPTE_WIRED) == 0))
2267 ++pmap->pm_stats.wired_count;
2268 else if (!wired && (origpte & VPTE_WIRED))
2269 --pmap->pm_stats.wired_count;
2272 * Remove the extra pte reference. Note that we cannot
2273 * optimize the RO->RW case because we have adjusted the
2274 * wiring count above and may need to adjust the wiring
2281 * We might be turning off write access to the page,
2282 * so we go ahead and sense modify status.
2284 if (origpte & VPTE_MANAGED) {
2285 if ((origpte & VPTE_M) &&
2286 pmap_track_modified(pmap, va)) {
2288 om = PHYS_TO_VM_PAGE(opa);
2292 KKASSERT(m->flags & PG_MAPPED);
2297 * Mapping has changed, invalidate old range and fall through to
2298 * handle validating new mapping.
2302 err = pmap_remove_pte(pmap, pte, va);
2304 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2308 * Enter on the PV list if part of our managed memory. Note that we
2309 * raise IPL while manipulating pv_table since pmap_enter can be
2310 * called at interrupt time.
2312 if (pmap_initialized &&
2313 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2314 pmap_insert_entry(pmap, va, mpte, m);
2316 vm_page_flag_set(m, PG_MAPPED);
2320 * Increment counters
2322 ++pmap->pm_stats.resident_count;
2324 pmap->pm_stats.wired_count++;
2328 * Now validate mapping with desired protection/wiring.
2330 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | VPTE_V);
2333 newpte |= VPTE_WIRED;
2334 if (pmap != &kernel_pmap)
2338 * If the mapping or permission bits are different from the
2339 * (now cleaned) original pte, an update is needed. We've
2340 * already downgraded or invalidated the page so all we have
2341 * to do now is update the bits.
2343 * XXX should we synchronize RO->RW changes to avoid another
2346 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
2347 *pte = newpte | VPTE_A;
2348 if (newpte & VPTE_W)
2349 vm_page_flag_set(m, PG_WRITEABLE);
2351 KKASSERT((newpte & VPTE_MANAGED) == 0 || (m->flags & PG_MAPPED));
2352 lwkt_reltoken(&vm_token);
2353 vm_object_drop(pmap->pm_pteobj);
2357 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2359 * Currently this routine may only be used on user pmaps, not kernel_pmap.
2364 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2369 vm_pindex_t ptepindex;
2372 KKASSERT(pmap != &kernel_pmap);
2374 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
2377 * Calculate pagetable page index
2379 ptepindex = pmap_pde_pindex(va);
2381 vm_object_hold(pmap->pm_pteobj);
2382 lwkt_gettoken(&vm_token);
2386 * Get the page directory entry
2388 ptepa = pmap_pde(pmap, va);
2391 * If the page table page is mapped, we just increment
2392 * the hold count, and activate it.
2394 if (ptepa && (*ptepa & VPTE_V) != 0) {
2395 if (*ptepa & VPTE_PS)
2396 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2397 if (pmap->pm_ptphint &&
2398 (pmap->pm_ptphint->pindex == ptepindex)) {
2399 mpte = pmap->pm_ptphint;
2401 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2402 pmap->pm_ptphint = mpte;
2403 vm_page_wakeup(mpte);
2408 mpte = _pmap_allocpte(pmap, ptepindex);
2410 } while (mpte == NULL);
2413 * Ok, now that the page table page has been validated, get the pte.
2414 * If the pte is already mapped undo mpte's hold_count and
2417 pte = pmap_pte(pmap, va);
2418 if (*pte & VPTE_V) {
2419 KKASSERT(mpte != NULL);
2420 pmap_unwire_pte_hold(pmap, va, mpte);
2421 pa = VM_PAGE_TO_PHYS(m);
2422 KKASSERT(((*pte ^ pa) & VPTE_FRAME) == 0);
2423 lwkt_reltoken(&vm_token);
2424 vm_object_drop(pmap->pm_pteobj);
2429 * Enter on the PV list if part of our managed memory
2431 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2432 pmap_insert_entry(pmap, va, mpte, m);
2433 vm_page_flag_set(m, PG_MAPPED);
2437 * Increment counters
2439 ++pmap->pm_stats.resident_count;
2441 pa = VM_PAGE_TO_PHYS(m);
2444 * Now validate mapping with RO protection
2446 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2447 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2449 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2450 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2451 /*pmap_inval_flush(&info); don't need for vkernel */
2452 lwkt_reltoken(&vm_token);
2453 vm_object_drop(pmap->pm_pteobj);
2457 * Make a temporary mapping for a physical address. This is only intended
2458 * to be used for panic dumps.
2460 * The caller is responsible for calling smp_invltlb().
2463 pmap_kenter_temporary(vm_paddr_t pa, long i)
2465 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
2466 return ((void *)crashdumpmap);
2469 #define MAX_INIT_PT (96)
2472 * This routine preloads the ptes for a given object into the specified pmap.
2473 * This eliminates the blast of soft faults on process startup and
2474 * immediately after an mmap.
2478 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2481 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2482 vm_object_t object, vm_pindex_t pindex,
2483 vm_size_t size, int limit)
2485 struct rb_vm_page_scan_info info;
2490 * We can't preinit if read access isn't set or there is no pmap
2493 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2497 * We can't preinit if the pmap is not the current pmap
2499 lp = curthread->td_lwp;
2500 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2503 psize = x86_64_btop(size);
2505 if ((object->type != OBJT_VNODE) ||
2506 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2507 (object->resident_page_count > MAX_INIT_PT))) {
2511 if (psize + pindex > object->size) {
2512 if (object->size < pindex)
2514 psize = object->size - pindex;
2521 * Use a red-black scan to traverse the requested range and load
2522 * any valid pages found into the pmap.
2524 * We cannot safely scan the object's memq unless we are in a
2525 * critical section since interrupts can remove pages from objects.
2527 info.start_pindex = pindex;
2528 info.end_pindex = pindex + psize - 1;
2534 vm_object_hold(object);
2535 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2536 pmap_object_init_pt_callback, &info);
2537 vm_object_drop(object);
2542 pmap_object_init_pt_callback(vm_page_t p, void *data)
2544 struct rb_vm_page_scan_info *info = data;
2545 vm_pindex_t rel_index;
2547 * don't allow an madvise to blow away our really
2548 * free pages allocating pv entries.
2550 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2551 vmstats.v_free_count < vmstats.v_free_reserved) {
2554 if (vm_page_busy_try(p, TRUE))
2556 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2557 (p->flags & PG_FICTITIOUS) == 0) {
2558 if ((p->queue - p->pc) == PQ_CACHE)
2559 vm_page_deactivate(p);
2560 rel_index = p->pindex - info->start_pindex;
2561 pmap_enter_quick(info->pmap,
2562 info->addr + x86_64_ptob(rel_index), p);
2569 * Return TRUE if the pmap is in shape to trivially
2570 * pre-fault the specified address.
2572 * Returns FALSE if it would be non-trivial or if a
2573 * pte is already loaded into the slot.
2578 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2584 lwkt_gettoken(&vm_token);
2585 pde = pmap_pde(pmap, addr);
2586 if (pde == NULL || *pde == 0) {
2589 pte = pmap_pde_to_pte(pde, addr);
2590 ret = (*pte) ? 0 : 1;
2592 lwkt_reltoken(&vm_token);
2597 * Change the wiring attribute for a map/virtual-address pair.
2599 * The mapping must already exist in the pmap.
2600 * No other requirements.
2603 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2610 lwkt_gettoken(&vm_token);
2611 pte = pmap_pte(pmap, va);
2613 if (wired && !pmap_pte_w(pte))
2614 pmap->pm_stats.wired_count++;
2615 else if (!wired && pmap_pte_w(pte))
2616 pmap->pm_stats.wired_count--;
2619 * Wiring is not a hardware characteristic so there is no need to
2620 * invalidate TLB. However, in an SMP environment we must use
2621 * a locked bus cycle to update the pte (if we are not using
2622 * the pmap_inval_*() API that is)... it's ok to do this for simple
2626 atomic_set_long(pte, VPTE_WIRED);
2628 atomic_clear_long(pte, VPTE_WIRED);
2629 lwkt_reltoken(&vm_token);
2633 * Copy the range specified by src_addr/len
2634 * from the source map to the range dst_addr/len
2635 * in the destination map.
2637 * This routine is only advisory and need not do anything.
2640 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2641 vm_size_t len, vm_offset_t src_addr)
2644 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2645 * valid through blocking calls, and that's just not going to
2656 * Zero the specified physical page.
2658 * This function may be called from an interrupt and no locking is
2662 pmap_zero_page(vm_paddr_t phys)
2664 vm_offset_t va = PHYS_TO_DMAP(phys);
2666 bzero((void *)va, PAGE_SIZE);
2670 * pmap_page_assertzero:
2672 * Assert that a page is empty, panic if it isn't.
2675 pmap_page_assertzero(vm_paddr_t phys)
2680 vm_offset_t virt = PHYS_TO_DMAP(phys);
2682 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
2683 if (*(int *)((char *)virt + i) != 0) {
2684 panic("pmap_page_assertzero() @ %p not zero!\n",
2694 * Zero part of a physical page by mapping it into memory and clearing
2695 * its contents with bzero.
2697 * off and size may not cover an area beyond a single hardware page.
2700 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2703 vm_offset_t virt = PHYS_TO_DMAP(phys);
2704 bzero((char *)virt + off, size);
2711 * Copy the physical page from the source PA to the target PA.
2712 * This function may be called from an interrupt. No locking
2716 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2718 vm_offset_t src_virt, dst_virt;
2721 src_virt = PHYS_TO_DMAP(src);
2722 dst_virt = PHYS_TO_DMAP(dst);
2723 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
2728 * pmap_copy_page_frag:
2730 * Copy the physical page from the source PA to the target PA.
2731 * This function may be called from an interrupt. No locking
2735 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2737 vm_offset_t src_virt, dst_virt;
2740 src_virt = PHYS_TO_DMAP(src);
2741 dst_virt = PHYS_TO_DMAP(dst);
2742 bcopy((char *)src_virt + (src & PAGE_MASK),
2743 (char *)dst_virt + (dst & PAGE_MASK),
2749 * Returns true if the pmap's pv is one of the first 16 pvs linked to
2750 * from this page. This count may be changed upwards or downwards
2751 * in the future; it is only necessary that true be returned for a small
2752 * subset of pmaps for proper page aging.
2754 * No other requirements.
2757 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2762 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2766 lwkt_gettoken(&vm_token);
2768 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2769 if (pv->pv_pmap == pmap) {
2770 lwkt_reltoken(&vm_token);
2778 lwkt_reltoken(&vm_token);
2784 * Remove all pages from specified address space this aids process
2785 * exit speeds. Also, this code is special cased for current
2786 * process only, but can have the more generic (and slightly slower)
2787 * mode enabled. This is much faster than pmap_remove in the case
2788 * of running down an entire address space.
2790 * No other requirements.
2793 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2795 pt_entry_t *pte, tpte;
2798 int save_generation;
2800 if (pmap->pm_pteobj)
2801 vm_object_hold(pmap->pm_pteobj);
2802 lwkt_gettoken(&vm_token);
2804 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2805 if (pv->pv_va >= eva || pv->pv_va < sva) {
2806 npv = TAILQ_NEXT(pv, pv_plist);
2810 KKASSERT(pmap == pv->pv_pmap);
2812 pte = pmap_pte(pmap, pv->pv_va);
2815 * We cannot remove wired pages from a process' mapping
2818 if (*pte & VPTE_WIRED) {
2819 npv = TAILQ_NEXT(pv, pv_plist);
2822 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2824 m = PHYS_TO_VM_PAGE(tpte & VPTE_FRAME);
2826 KASSERT(m < &vm_page_array[vm_page_array_size],
2827 ("pmap_remove_pages: bad tpte %lx", tpte));
2829 KKASSERT(pmap->pm_stats.resident_count > 0);
2830 --pmap->pm_stats.resident_count;
2833 * Update the vm_page_t clean and reference bits.
2835 if (tpte & VPTE_M) {
2839 npv = TAILQ_NEXT(pv, pv_plist);
2840 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2841 save_generation = ++pmap->pm_generation;
2843 m->md.pv_list_count--;
2844 atomic_add_int(&m->object->agg_pv_list_count, -1);
2845 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2846 if (TAILQ_EMPTY(&m->md.pv_list))
2847 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2849 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2853 * Restart the scan if we blocked during the unuse or free
2854 * calls and other removals were made.
2856 if (save_generation != pmap->pm_generation) {
2857 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2858 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2861 lwkt_reltoken(&vm_token);
2862 if (pmap->pm_pteobj)
2863 vm_object_drop(pmap->pm_pteobj);
2867 * pmap_testbit tests bits in active mappings of a VM page.
2870 pmap_testbit(vm_page_t m, int bit)
2875 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2878 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2883 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2885 * if the bit being tested is the modified bit, then
2886 * mark clean_map and ptes as never
2889 if (bit & (VPTE_A|VPTE_M)) {
2890 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2894 #if defined(PMAP_DIAGNOSTIC)
2895 if (pv->pv_pmap == NULL) {
2896 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
2900 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2911 * This routine is used to clear bits in ptes. Certain bits require special
2912 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2914 * This routine is only called with certain VPTE_* bit combinations.
2916 static __inline void
2917 pmap_clearbit(vm_page_t m, int bit)
2923 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2929 * Loop over all current mappings setting/clearing as appropos If
2930 * setting RO do we need to clear the VAC?
2932 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2934 * don't write protect pager mappings
2936 if (bit == VPTE_W) {
2937 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2941 #if defined(PMAP_DIAGNOSTIC)
2942 if (pv->pv_pmap == NULL) {
2943 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
2949 * Careful here. We can use a locked bus instruction to
2950 * clear VPTE_A or VPTE_M safely but we need to synchronize
2951 * with the target cpus when we mess with VPTE_W.
2953 * On virtual kernels we must force a new fault-on-write
2954 * in the real kernel if we clear the Modify bit ourselves,
2955 * otherwise the real kernel will not get a new fault and
2956 * will never set our Modify bit again.
2958 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2960 if (bit == VPTE_W) {
2962 * We must also clear VPTE_M when clearing
2965 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2969 } else if (bit == VPTE_M) {
2971 * We do not have to make the page read-only
2972 * when clearing the Modify bit. The real
2973 * kernel will make the real PTE read-only
2974 * or otherwise detect the write and set
2975 * our VPTE_M again simply by us invalidating
2976 * the real kernel VA for the pmap (as we did
2977 * above). This allows the real kernel to
2978 * handle the write fault without forwarding
2981 atomic_clear_long(pte, VPTE_M);
2982 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2984 * We've been asked to clear W & M, I guess
2985 * the caller doesn't want us to update
2986 * the dirty status of the VM page.
2988 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2991 * We've been asked to clear bits that do
2992 * not interact with hardware.
2994 atomic_clear_long(pte, bit);
3002 * Lower the permission for all mappings to a given page.
3004 * No other requirements.
3007 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3009 /* JG NX support? */
3010 if ((prot & VM_PROT_WRITE) == 0) {
3011 lwkt_gettoken(&vm_token);
3012 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3013 pmap_clearbit(m, VPTE_W);
3014 vm_page_flag_clear(m, PG_WRITEABLE);
3018 lwkt_reltoken(&vm_token);
3023 pmap_phys_address(vm_pindex_t ppn)
3025 return (x86_64_ptob(ppn));
3029 * Return a count of reference bits for a page, clearing those bits.
3030 * It is not necessary for every reference bit to be cleared, but it
3031 * is necessary that 0 only be returned when there are truly no
3032 * reference bits set.
3034 * XXX: The exact number of bits to check and clear is a matter that
3035 * should be tested and standardized at some point in the future for
3036 * optimal aging of shared pages.
3038 * No other requirements.
3041 pmap_ts_referenced(vm_page_t m)
3043 pv_entry_t pv, pvf, pvn;
3047 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3051 lwkt_gettoken(&vm_token);
3053 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3058 pvn = TAILQ_NEXT(pv, pv_list);
3060 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3062 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3064 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3067 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3069 if (pte && (*pte & VPTE_A)) {
3071 atomic_clear_long(pte, VPTE_A);
3073 atomic_clear_long_nonlocked(pte, VPTE_A);
3080 } while ((pv = pvn) != NULL && pv != pvf);
3082 lwkt_reltoken(&vm_token);
3089 * Return whether or not the specified physical page was modified
3090 * in any physical maps.
3092 * No other requirements.
3095 pmap_is_modified(vm_page_t m)
3099 lwkt_gettoken(&vm_token);
3100 res = pmap_testbit(m, VPTE_M);
3101 lwkt_reltoken(&vm_token);
3106 * Clear the modify bits on the specified physical page.
3108 * No other requirements.
3111 pmap_clear_modify(vm_page_t m)
3113 lwkt_gettoken(&vm_token);
3114 pmap_clearbit(m, VPTE_M);
3115 lwkt_reltoken(&vm_token);
3119 * Clear the reference bit on the specified physical page.
3121 * No other requirements.
3124 pmap_clear_reference(vm_page_t m)
3126 lwkt_gettoken(&vm_token);
3127 pmap_clearbit(m, VPTE_A);
3128 lwkt_reltoken(&vm_token);
3132 * Miscellaneous support routines follow
3136 i386_protection_init(void)
3140 kp = protection_codes;
3141 for (prot = 0; prot < 8; prot++) {
3142 if (prot & VM_PROT_READ)
3144 if (prot & VM_PROT_WRITE)
3146 if (prot & VM_PROT_EXECUTE)
3153 * Perform the pmap work for mincore
3155 * No other requirements.
3158 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3160 pt_entry_t *ptep, pte;
3164 lwkt_gettoken(&vm_token);
3165 ptep = pmap_pte(pmap, addr);
3167 if (ptep && (pte = *ptep) != 0) {
3170 val = MINCORE_INCORE;
3171 if ((pte & VPTE_MANAGED) == 0)
3174 pa = pte & VPTE_FRAME;
3176 m = PHYS_TO_VM_PAGE(pa);
3182 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3184 * Modified by someone
3186 else if (m->dirty || pmap_is_modified(m))
3187 val |= MINCORE_MODIFIED_OTHER;
3192 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3195 * Referenced by someone
3197 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3198 val |= MINCORE_REFERENCED_OTHER;
3199 vm_page_flag_set(m, PG_REFERENCED);
3203 lwkt_reltoken(&vm_token);
3208 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3209 * vmspace will be ref'd and the old one will be deref'd.
3211 * Caller must hold vmspace->vm_map.token for oldvm and newvm
3214 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3216 struct vmspace *oldvm;
3220 oldvm = p->p_vmspace;
3221 if (oldvm != newvm) {
3222 p->p_vmspace = newvm;
3223 KKASSERT(p->p_nthreads == 1);
3224 lp = RB_ROOT(&p->p_lwp_tree);
3225 pmap_setlwpvm(lp, newvm);
3227 sysref_get(&newvm->vm_sysref);
3228 sysref_put(&oldvm->vm_sysref);
3235 * Set the vmspace for a LWP. The vmspace is almost universally set the
3236 * same as the process vmspace, but virtual kernels need to swap out contexts
3237 * on a per-lwp basis.
3240 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3242 struct vmspace *oldvm;
3246 oldvm = lp->lwp_vmspace;
3248 if (oldvm != newvm) {
3249 lp->lwp_vmspace = newvm;
3250 if (curthread->td_lwp == lp) {
3251 pmap = vmspace_pmap(newvm);
3253 atomic_set_cpumask(&pmap->pm_active, CPUMASK(mycpu->gd_cpuid));
3255 pmap->pm_active |= 1;
3257 #if defined(SWTCH_OPTIM_STATS)
3260 pmap = vmspace_pmap(oldvm);
3262 atomic_clear_cpumask(&pmap->pm_active,
3263 CPUMASK(mycpu->gd_cpuid));
3265 pmap->pm_active &= ~(cpumask_t)1;
3273 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3276 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3280 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3285 * Used by kmalloc/kfree, page already exists at va
3288 pmap_kvtom(vm_offset_t va)
3292 KKASSERT(va >= KvaStart && va < KvaEnd);
3294 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));