2 * Copyright (c) 1991 Regents of the University of California.
3 * Copyright (c) 1994 John S. Dyson
4 * Copyright (c) 1994 David Greenman
5 * Copyright (c) 2003 Peter Wemm
6 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
7 * Copyright (c) 2008, 2009 The DragonFly Project.
8 * Copyright (c) 2008, 2009 Jordan Gordeev.
11 * This code is derived from software contributed to Berkeley by
12 * the Systems Programming Group of the University of Utah Computer
13 * Science Department and William Jolitz of UUNET Technologies Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
44 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
45 * $DragonFly: src/sys/platform/pc64/amd64/pmap.c,v 1.3 2008/08/29 17:07:10 dillon Exp $
49 * Manages physical address maps.
51 * In addition to hardware address maps, this
52 * module is called upon to provide software-use-only
53 * maps which may or may not be stored in the same
54 * form as hardware maps. These pseudo-maps are
55 * used to store intermediate results from copy
56 * operations to and from address spaces.
58 * Since the information managed by this module is
59 * also stored by the logical address mapping module,
60 * this module may throw away valid virtual-to-physical
61 * mappings at almost any time. However, invalidations
62 * of virtual-to-physical mappings must be done as
65 * In order to cope with hardware architectures which
66 * make virtual-to-physical map invalidates expensive,
67 * this module may delay invalidate or reduced protection
68 * operations until such time as they are actually
69 * necessary. This module is given full information as
70 * to which processors are currently using which maps,
71 * and to when physical maps must be made correct.
77 #include "opt_msgbuf.h"
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
83 #include <sys/msgbuf.h>
84 #include <sys/vmmeter.h>
86 #include <sys/vmspace.h>
89 #include <vm/vm_param.h>
90 #include <sys/sysctl.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_extern.h>
97 #include <vm/vm_pageout.h>
98 #include <vm/vm_pager.h>
99 #include <vm/vm_zone.h>
101 #include <sys/user.h>
102 #include <sys/thread2.h>
103 #include <sys/sysref2.h>
105 #include <machine/cputypes.h>
106 #include <machine/md_var.h>
107 #include <machine/specialreg.h>
108 #include <machine/smp.h>
109 #include <machine/globaldata.h>
110 #include <machine/pmap.h>
111 #include <machine/pmap_inval.h>
119 #define PMAP_KEEP_PDIRS
120 #ifndef PMAP_SHPGPERPROC
121 #define PMAP_SHPGPERPROC 200
124 #if defined(DIAGNOSTIC)
125 #define PMAP_DIAGNOSTIC
130 #if !defined(PMAP_DIAGNOSTIC)
131 #define PMAP_INLINE __inline
137 * Get PDEs and PTEs for user/kernel address space
139 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
140 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
142 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & VPTE_V) != 0)
143 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & VPTE_WIRED) != 0)
144 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & VPTE_M) != 0)
145 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & VPTE_A) != 0)
146 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & VPTE_V) != 0)
149 * Given a map and a machine independent protection code,
150 * convert to a vax protection code.
152 #define pte_prot(m, p) \
153 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
154 static int protection_codes[8];
156 struct pmap kernel_pmap;
157 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
159 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
161 static vm_object_t kptobj;
165 static uint64_t KPDphys; /* phys addr of kernel level 2 */
166 uint64_t KPDPphys; /* phys addr of kernel level 3 */
167 uint64_t KPML4phys; /* phys addr of kernel level 4 */
171 * Data for the pv entry allocation mechanism
173 static vm_zone_t pvzone;
174 static struct vm_zone pvzone_store;
175 static struct vm_object pvzone_obj;
176 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
177 static int pmap_pagedaemon_waken = 0;
178 static struct pv_entry *pvinit;
181 * All those kernel PT submaps that BSD is so fond of
183 pt_entry_t *CMAP1 = 0, *ptmmap;
185 static pt_entry_t *msgbufmap;
189 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
190 static pv_entry_t get_pv_entry (void);
191 static void i386_protection_init (void);
192 static __inline void pmap_clearbit (vm_page_t m, int bit);
194 static void pmap_remove_all (vm_page_t m);
195 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
197 static void pmap_remove_page (struct pmap *pmap, vm_offset_t va);
198 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
200 static boolean_t pmap_testbit (vm_page_t m, int bit);
201 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
202 vm_page_t mpte, vm_page_t m);
204 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
206 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
207 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
208 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
209 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
210 static int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m);
211 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t);
216 * Super fast pmap_pte routine best used when scanning the pv lists.
217 * This eliminates many course-grained invltlb calls. Note that many of
218 * the pv list scans are across different pmaps and it is very wasteful
219 * to do an entire invltlb when checking a single mapping.
221 * Should only be called while in a critical section.
223 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
226 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
228 return pmap_pte(pmap, va);
231 /* Return a non-clipped PD index for a given VA */
232 static __inline vm_pindex_t
233 pmap_pde_pindex(vm_offset_t va)
235 return va >> PDRSHIFT;
238 /* Return various clipped indexes for a given VA */
239 static __inline vm_pindex_t
240 pmap_pte_index(vm_offset_t va)
243 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
246 static __inline vm_pindex_t
247 pmap_pde_index(vm_offset_t va)
250 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
253 static __inline vm_pindex_t
254 pmap_pdpe_index(vm_offset_t va)
257 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
260 static __inline vm_pindex_t
261 pmap_pml4e_index(vm_offset_t va)
264 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
267 /* Return a pointer to the PML4 slot that corresponds to a VA */
268 static __inline pml4_entry_t *
269 pmap_pml4e(pmap_t pmap, vm_offset_t va)
272 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
275 /* Return a pointer to the PDP slot that corresponds to a VA */
276 static __inline pdp_entry_t *
277 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
281 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & VPTE_FRAME);
282 return (&pdpe[pmap_pdpe_index(va)]);
285 /* Return a pointer to the PDP slot that corresponds to a VA */
286 static __inline pdp_entry_t *
287 pmap_pdpe(pmap_t pmap, vm_offset_t va)
291 pml4e = pmap_pml4e(pmap, va);
292 if ((*pml4e & VPTE_V) == 0)
294 return (pmap_pml4e_to_pdpe(pml4e, va));
297 /* Return a pointer to the PD slot that corresponds to a VA */
298 static __inline pd_entry_t *
299 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
303 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & VPTE_FRAME);
304 return (&pde[pmap_pde_index(va)]);
307 /* Return a pointer to the PD slot that corresponds to a VA */
308 static __inline pd_entry_t *
309 pmap_pde(pmap_t pmap, vm_offset_t va)
313 pdpe = pmap_pdpe(pmap, va);
314 if (pdpe == NULL || (*pdpe & VPTE_V) == 0)
316 return (pmap_pdpe_to_pde(pdpe, va));
319 /* Return a pointer to the PT slot that corresponds to a VA */
320 static __inline pt_entry_t *
321 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
325 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & VPTE_FRAME);
326 return (&pte[pmap_pte_index(va)]);
329 /* Return a pointer to the PT slot that corresponds to a VA */
330 static __inline pt_entry_t *
331 pmap_pte(pmap_t pmap, vm_offset_t va)
335 pde = pmap_pde(pmap, va);
336 if (pde == NULL || (*pde & VPTE_V) == 0)
338 if ((*pde & VPTE_PS) != 0) /* compat with i386 pmap_pte() */
339 return ((pt_entry_t *)pde);
340 return (pmap_pde_to_pte(pde, va));
345 PMAP_INLINE pt_entry_t *
346 vtopte(vm_offset_t va)
348 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
350 return (PTmap + ((va >> PAGE_SHIFT) & mask));
353 static __inline pd_entry_t *
354 vtopde(vm_offset_t va)
356 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
358 return (PDmap + ((va >> PDRSHIFT) & mask));
361 PMAP_INLINE pt_entry_t *
362 vtopte(vm_offset_t va)
365 x = pmap_pte(&kernel_pmap, va);
370 static __inline pd_entry_t *
371 vtopde(vm_offset_t va)
374 x = pmap_pde(&kernel_pmap, va);
381 allocpages(vm_paddr_t *firstaddr, int n)
387 bzero((void *)ret, n * PAGE_SIZE);
389 *firstaddr += n * PAGE_SIZE;
394 create_pagetables(vm_paddr_t *firstaddr, int64_t ptov_offset)
397 pml4_entry_t *KPML4virt;
398 pdp_entry_t *KPDPvirt;
401 int kpml4i = pmap_pml4e_index(ptov_offset);
402 int kpdpi = pmap_pdpe_index(ptov_offset);
406 KPML4phys = allocpages(firstaddr, 1);
407 KPDPphys = allocpages(firstaddr, NKPML4E);
408 KPDphys = allocpages(firstaddr, NKPDPE);
409 KPTphys = allocpages(firstaddr, NKPT);
411 KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
412 KPDPvirt = (pdp_entry_t *)PHYS_TO_DMAP(KPDPphys);
413 KPDvirt = (pd_entry_t *)PHYS_TO_DMAP(KPDphys);
414 KPTvirt = (pt_entry_t *)PHYS_TO_DMAP(KPTphys);
416 bzero(KPML4virt, 1 * PAGE_SIZE);
417 bzero(KPDPvirt, NKPML4E * PAGE_SIZE);
418 bzero(KPDvirt, NKPDPE * PAGE_SIZE);
419 bzero(KPTvirt, NKPT * PAGE_SIZE);
421 /* Now map the page tables at their location within PTmap */
422 for (i = 0; i < NKPT; i++) {
423 KPDvirt[i] = KPTphys + (i << PAGE_SHIFT);
424 KPDvirt[i] |= VPTE_R | VPTE_W | VPTE_V;
427 /* And connect up the PD to the PDP */
428 for (i = 0; i < NKPDPE; i++) {
429 KPDPvirt[i + kpdpi] = KPDphys + (i << PAGE_SHIFT);
430 KPDPvirt[i + kpdpi] |= VPTE_R | VPTE_W | VPTE_V;
433 /* And recursively map PML4 to itself in order to get PTmap */
434 KPML4virt[PML4PML4I] = KPML4phys;
435 KPML4virt[PML4PML4I] |= VPTE_R | VPTE_W | VPTE_V;
437 /* Connect the KVA slot up to the PML4 */
438 KPML4virt[kpml4i] = KPDPphys;
439 KPML4virt[kpml4i] |= VPTE_R | VPTE_W | VPTE_V;
443 * Bootstrap the system enough to run with virtual memory.
445 * On the i386 this is called after mapping has already been enabled
446 * and just syncs the pmap module with what has already been done.
447 * [We can't call it easily with mapping off since the kernel is not
448 * mapped with PA == VA, hence we would have to relocate every address
449 * from the linked base (virtual) address "KERNBASE" to the actual
450 * (physical) address starting relative to 0]
453 pmap_bootstrap(vm_paddr_t *firstaddr, int64_t ptov_offset)
459 * Create an initial set of page tables to run the kernel in.
461 create_pagetables(firstaddr, ptov_offset);
463 virtual_start = KvaStart + *firstaddr;
464 virtual_end = KvaEnd;
467 * Initialize protection array.
469 i386_protection_init();
472 * The kernel's pmap is statically allocated so we don't have to use
473 * pmap_create, which is unlikely to work correctly at this part of
474 * the boot sequence (XXX and which no longer exists).
476 kernel_pmap.pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
477 kernel_pmap.pm_count = 1;
478 kernel_pmap.pm_active = (cpumask_t)-1; /* don't allow deactivation */
479 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))
618 * 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;
628 pdep = pmap_pde(pmap, va);
632 if ((pde & VPTE_PS) != 0) {
634 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
636 pte = pmap_pde_to_pte(pdep, va);
637 rtval = (*pte & VPTE_FRAME) | (va & PAGE_MASK);
645 * Routine: pmap_kextract
647 * Extract the physical page address associated
648 * kernel virtual address.
651 pmap_kextract(vm_offset_t va)
656 KKASSERT(va >= KvaStart && va < KvaEnd);
659 * The DMAP region is not included in [KvaStart, KvaEnd)
662 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
663 pa = DMAP_TO_PHYS(va);
669 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
672 * Beware of a concurrent promotion that changes the
673 * PDE at this point! For example, vtopte() must not
674 * be used to access the PTE because it would use the
675 * new PDE. It is, however, safe to use the old PDE
676 * because the page table page is preserved by the
679 pa = *pmap_pde_to_pte(&pde, va);
680 pa = (pa & VPTE_FRAME) | (va & PAGE_MASK);
688 /***************************************************
689 * Low level mapping routines.....
690 ***************************************************/
693 * Enter a mapping into kernel_pmap. Mappings created in this fashion
694 * are not managed. Mappings must be immediately accessible on all cpus.
696 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
697 * real pmap and handle related races before storing the new vpte.
700 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
705 KKASSERT(va >= KvaStart && va < KvaEnd);
706 npte = pa | VPTE_R | VPTE_W | VPTE_V;
709 pmap_inval_pte(pte, &kernel_pmap, va);
714 * Enter an unmanaged KVA mapping for the private use of the current
715 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
718 * It is illegal for the mapping to be accessed by other cpus unleess
719 * pmap_kenter_sync*() is called.
722 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
727 KKASSERT(va >= KvaStart && va < KvaEnd);
729 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
732 pmap_inval_pte_quick(pte, &kernel_pmap, va);
734 //cpu_invlpg((void *)va);
738 * Synchronize a kvm mapping originally made for the private use on
739 * some other cpu so it can be used on all cpus.
741 * XXX add MADV_RESYNC to improve performance.
744 pmap_kenter_sync(vm_offset_t va)
746 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
750 * Synchronize a kvm mapping originally made for the private use on
751 * some other cpu so it can be used on our cpu. Turns out to be the
752 * same madvise() call, because we have to sync the real pmaps anyway.
754 * XXX add MADV_RESYNC to improve performance.
757 pmap_kenter_sync_quick(vm_offset_t va)
759 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
763 * Remove an unmanaged mapping created with pmap_kenter*().
766 pmap_kremove(vm_offset_t va)
770 KKASSERT(va >= KvaStart && va < KvaEnd);
774 pmap_inval_pte(pte, &kernel_pmap, va);
779 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
780 * only with this cpu.
782 * Unfortunately because we optimize new entries by testing VPTE_V later
783 * on, we actually still have to synchronize with all the cpus. XXX maybe
784 * store a junk value and test against 0 in the other places instead?
787 pmap_kremove_quick(vm_offset_t va)
791 KKASSERT(va >= KvaStart && va < KvaEnd);
795 pmap_inval_pte(pte, &kernel_pmap, va); /* NOT _quick */
800 * Used to map a range of physical addresses into kernel
801 * virtual address space.
803 * For now, VM is already on, we only need to map the
807 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
809 return PHYS_TO_DMAP(start);
814 * Map a set of unmanaged VM pages into KVM.
817 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
821 end_va = va + count * PAGE_SIZE;
822 KKASSERT(va >= KvaStart && end_va < KvaEnd);
824 while (va < end_va) {
829 pmap_inval_pte(pte, &kernel_pmap, va);
830 *pte = VM_PAGE_TO_PHYS(*m) | VPTE_R | VPTE_W | VPTE_V;
837 * Map a set of VM pages to kernel virtual memory. If a mapping changes
838 * clear the supplied mask. The caller handles any SMP interactions.
839 * The mask is used to provide the caller with hints on what SMP interactions
843 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
846 cpumask_t cmask = mycpu->gd_cpumask;
848 end_va = va + count * PAGE_SIZE;
849 KKASSERT(va >= KvaStart && end_va < KvaEnd);
851 while (va < end_va) {
856 pteval = VM_PAGE_TO_PHYS(*m) | VPTE_R | VPTE_W | VPTE_V;
857 if (*pte != pteval) {
859 pmap_inval_pte_quick(pte, &kernel_pmap, va);
861 } else if ((*mask & cmask) == 0) {
862 pmap_kenter_sync_quick(va);
871 * Undo the effects of pmap_qenter*().
874 pmap_qremove(vm_offset_t va, int count)
878 end_va = va + count * PAGE_SIZE;
879 KKASSERT(va >= KvaStart && end_va < KvaEnd);
881 while (va < end_va) {
886 pmap_inval_pte(pte, &kernel_pmap, va);
893 * This routine works like vm_page_lookup() but also blocks as long as the
894 * page is busy. This routine does not busy the page it returns.
896 * Unless the caller is managing objects whos pages are in a known state,
897 * the call should be made with a critical section held so the page's object
898 * association remains valid on return.
901 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
906 m = vm_page_lookup(object, pindex);
907 } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
913 * Create a new thread and optionally associate it with a (new) process.
914 * NOTE! the new thread's cpu may not equal the current cpu.
917 pmap_init_thread(thread_t td)
919 /* enforce pcb placement */
920 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
921 td->td_savefpu = &td->td_pcb->pcb_save;
922 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on amd64? */
926 * This routine directly affects the fork perf for a process.
929 pmap_init_proc(struct proc *p)
934 * Dispose the UPAGES for a process that has exited.
935 * This routine directly impacts the exit perf of a process.
938 pmap_dispose_proc(struct proc *p)
940 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
943 /***************************************************
944 * Page table page management routines.....
945 ***************************************************/
948 * This routine unholds page table pages, and if the hold count
949 * drops to zero, then it decrements the wire count.
951 * We must recheck that this is the last hold reference after busy-sleeping
955 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
957 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
959 KASSERT(m->queue == PQ_NONE,
960 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
962 if (m->hold_count == 1) {
964 * Unmap the page table page.
968 /* pmap_inval_add(info, pmap, -1); */
970 if (m->pindex >= (NUPDE + NUPDPE)) {
973 pml4 = pmap_pml4e(pmap, va);
975 } else if (m->pindex >= NUPDE) {
978 pdp = pmap_pdpe(pmap, va);
983 pd = pmap_pde(pmap, va);
987 KKASSERT(pmap->pm_stats.resident_count > 0);
988 --pmap->pm_stats.resident_count;
990 if (pmap->pm_ptphint == m)
991 pmap->pm_ptphint = NULL;
993 if (m->pindex < NUPDE) {
994 /* We just released a PT, unhold the matching PD */
997 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & VPTE_FRAME);
998 pmap_unwire_pte_hold(pmap, va, pdpg);
1000 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1001 /* We just released a PD, unhold the matching PDP */
1004 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & VPTE_FRAME);
1005 pmap_unwire_pte_hold(pmap, va, pdppg);
1009 * This was our last hold, the page had better be unwired
1010 * after we decrement wire_count.
1012 * FUTURE NOTE: shared page directory page could result in
1013 * multiple wire counts.
1017 KKASSERT(m->wire_count == 0);
1018 --vmstats.v_wire_count;
1019 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1021 vm_page_free_zero(m);
1024 KKASSERT(m->hold_count > 1);
1031 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
1033 KKASSERT(m->hold_count > 0);
1034 if (m->hold_count > 1) {
1038 return _pmap_unwire_pte_hold(pmap, va, m);
1043 * After removing a page table entry, this routine is used to
1044 * conditionally free the page, and manage the hold/wire counts.
1047 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1049 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1050 vm_pindex_t ptepindex;
1054 * page table pages in the kernel_pmap are not managed.
1056 if (pmap == &kernel_pmap)
1058 ptepindex = pmap_pde_pindex(va);
1059 if (pmap->pm_ptphint &&
1060 (pmap->pm_ptphint->pindex == ptepindex)) {
1061 mpte = pmap->pm_ptphint;
1063 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1064 pmap->pm_ptphint = mpte;
1068 return pmap_unwire_pte_hold(pmap, va, mpte);
1072 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
1073 * just dummy it up so it works well enough for fork().
1075 * In DragonFly, process pmaps may only be used to manipulate user address
1076 * space, never kernel address space.
1079 pmap_pinit0(struct pmap *pmap)
1085 * Initialize a preallocated and zeroed pmap structure,
1086 * such as one in a vmspace structure.
1089 pmap_pinit(struct pmap *pmap)
1094 * No need to allocate page table space yet but we do need a valid
1095 * page directory table.
1097 if (pmap->pm_pml4 == NULL) {
1099 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1103 * Allocate an object for the ptes
1105 if (pmap->pm_pteobj == NULL)
1106 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1109 * Allocate the page directory page, unless we already have
1110 * one cached. If we used the cached page the wire_count will
1111 * already be set appropriately.
1113 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1114 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1115 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1116 pmap->pm_pdirm = ptdpg;
1117 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1118 ptdpg->valid = VM_PAGE_BITS_ALL;
1119 if (ptdpg->wire_count == 0)
1120 ++vmstats.v_wire_count;
1121 ptdpg->wire_count = 1;
1122 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1124 if ((ptdpg->flags & PG_ZERO) == 0)
1125 bzero(pmap->pm_pml4, PAGE_SIZE);
1128 pmap->pm_active = 0;
1129 pmap->pm_ptphint = NULL;
1130 TAILQ_INIT(&pmap->pm_pvlist);
1131 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1132 pmap->pm_stats.resident_count = 1;
1136 * Clean up a pmap structure so it can be physically freed. This routine
1137 * is called by the vmspace dtor function. A great deal of pmap data is
1138 * left passively mapped to improve vmspace management so we have a bit
1139 * of cleanup work to do here.
1142 pmap_puninit(pmap_t pmap)
1146 KKASSERT(pmap->pm_active == 0);
1147 if ((p = pmap->pm_pdirm) != NULL) {
1148 KKASSERT(pmap->pm_pml4 != NULL);
1149 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1151 vmstats.v_wire_count--;
1152 KKASSERT((p->flags & PG_BUSY) == 0);
1154 vm_page_free_zero(p);
1155 pmap->pm_pdirm = NULL;
1157 if (pmap->pm_pml4) {
1158 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1159 pmap->pm_pml4 = NULL;
1161 if (pmap->pm_pteobj) {
1162 vm_object_deallocate(pmap->pm_pteobj);
1163 pmap->pm_pteobj = NULL;
1168 * Wire in kernel global address entries. To avoid a race condition
1169 * between pmap initialization and pmap_growkernel, this procedure
1170 * adds the pmap to the master list (which growkernel scans to update),
1171 * then copies the template.
1173 * In a virtual kernel there are no kernel global address entries.
1176 pmap_pinit2(struct pmap *pmap)
1179 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1184 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1185 * 0 on failure (if the procedure had to sleep).
1187 * When asked to remove the page directory page itself, we actually just
1188 * leave it cached so we do not have to incur the SMP inval overhead of
1189 * removing the kernel mapping. pmap_puninit() will take care of it.
1192 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1194 pml4_entry_t *pml4 = pmap->pm_pml4;
1196 * This code optimizes the case of freeing non-busy
1197 * page-table pages. Those pages are zero now, and
1198 * might as well be placed directly into the zero queue.
1200 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1206 * Remove the page table page from the processes address space.
1208 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1210 * We are the pml4 table itself.
1212 /* XXX anything to do here? */
1213 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1215 * We are a PDP page.
1216 * We look for the PML4 entry that points to us.
1218 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1219 KKASSERT(m4 != NULL);
1220 pml4_entry_t *pml4 = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1221 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1222 KKASSERT(pml4[idx] != 0);
1225 /* JG What about wire_count? */
1226 } else if (p->pindex >= NUPDE) {
1229 * We look for the PDP entry that points to us.
1231 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1232 KKASSERT(m3 != NULL);
1233 pdp_entry_t *pdp = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1234 int idx = (p->pindex - NUPDE) % NPDPEPG;
1235 KKASSERT(pdp[idx] != 0);
1238 /* JG What about wire_count? */
1240 /* We are a PT page.
1241 * We look for the PD entry that points to us.
1243 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1244 KKASSERT(m2 != NULL);
1245 pd_entry_t *pd = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1246 int idx = p->pindex % NPDEPG;
1249 /* JG What about wire_count? */
1251 KKASSERT(pmap->pm_stats.resident_count > 0);
1252 --pmap->pm_stats.resident_count;
1254 if (p->hold_count) {
1255 panic("pmap_release: freeing held page table page");
1257 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1258 pmap->pm_ptphint = NULL;
1261 * We leave the top-level page table page cached, wired, and mapped in
1262 * the pmap until the dtor function (pmap_puninit()) gets called.
1263 * However, still clean it up so we can set PG_ZERO.
1265 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1266 bzero(pmap->pm_pml4, PAGE_SIZE);
1267 vm_page_flag_set(p, PG_ZERO);
1272 vmstats.v_wire_count--;
1273 /* JG eventually revert to using vm_page_free_zero() */
1280 * this routine is called if the page table page is not
1284 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1286 vm_page_t m, pdppg, pdpg;
1289 * Find or fabricate a new pagetable page
1291 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1292 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1294 if ((m->flags & PG_ZERO) == 0) {
1295 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1298 KASSERT(m->queue == PQ_NONE,
1299 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1302 * Increment the hold count for the page we will be returning to
1307 if (m->wire_count == 0)
1308 vmstats.v_wire_count++;
1312 * Map the pagetable page into the process address space, if
1313 * it isn't already there.
1316 ++pmap->pm_stats.resident_count;
1318 if (ptepindex >= (NUPDE + NUPDPE)) {
1320 vm_pindex_t pml4index;
1322 /* Wire up a new PDP page */
1323 pml4index = ptepindex - (NUPDE + NUPDPE);
1324 pml4 = &pmap->pm_pml4[pml4index];
1325 *pml4 = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1327 } else if (ptepindex >= NUPDE) {
1328 vm_pindex_t pml4index;
1329 vm_pindex_t pdpindex;
1333 /* Wire up a new PD page */
1334 pdpindex = ptepindex - NUPDE;
1335 pml4index = pdpindex >> NPML4EPGSHIFT;
1337 pml4 = &pmap->pm_pml4[pml4index];
1338 if ((*pml4 & VPTE_V) == 0) {
1339 /* Have to allocate a new PDP page, recurse */
1340 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1347 /* Add reference to the PDP page */
1348 pdppg = PHYS_TO_VM_PAGE(*pml4 & VPTE_FRAME);
1349 pdppg->hold_count++;
1351 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1353 /* Now find the pdp page */
1354 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1355 KKASSERT(*pdp == 0); /* JG DEBUG64 */
1356 *pdp = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1359 vm_pindex_t pml4index;
1360 vm_pindex_t pdpindex;
1365 /* Wire up a new PT page */
1366 pdpindex = ptepindex >> NPDPEPGSHIFT;
1367 pml4index = pdpindex >> NPML4EPGSHIFT;
1369 /* First, find the pdp and check that its valid. */
1370 pml4 = &pmap->pm_pml4[pml4index];
1371 if ((*pml4 & VPTE_V) == 0) {
1372 /* We miss a PDP page. We ultimately need a PD page.
1373 * Recursively allocating a PD page will allocate
1374 * the missing PDP page and will also allocate
1375 * the PD page we need.
1377 /* Have to allocate a new PD page, recurse */
1378 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1384 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1385 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1387 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1388 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1389 if ((*pdp & VPTE_V) == 0) {
1390 /* Have to allocate a new PD page, recurse */
1391 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1398 /* Add reference to the PD page */
1399 pdpg = PHYS_TO_VM_PAGE(*pdp & VPTE_FRAME);
1403 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & VPTE_FRAME);
1405 /* Now we know where the page directory page is */
1406 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1407 KKASSERT(*pd == 0); /* JG DEBUG64 */
1408 *pd = VM_PAGE_TO_PHYS(m) | VPTE_R | VPTE_W | VPTE_V |
1413 * Set the page table hint
1415 pmap->pm_ptphint = m;
1417 m->valid = VM_PAGE_BITS_ALL;
1418 vm_page_flag_clear(m, PG_ZERO);
1419 vm_page_flag_set(m, PG_MAPPED);
1426 * Determine the page table page required to access the VA in the pmap
1427 * and allocate it if necessary. Return a held vm_page_t for the page.
1429 * Only used with user pmaps.
1432 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1434 vm_pindex_t ptepindex;
1439 * Calculate pagetable page index
1441 ptepindex = pmap_pde_pindex(va);
1444 * Get the page directory entry
1446 pd = pmap_pde(pmap, va);
1449 * This supports switching from a 2MB page to a
1452 if (pd != NULL && (*pd & (VPTE_PS | VPTE_V)) == (VPTE_PS | VPTE_V)) {
1453 panic("no promotion/demotion yet");
1461 * If the page table page is mapped, we just increment the
1462 * hold count, and activate it.
1464 if (pd != NULL && (*pd & VPTE_V) != 0) {
1465 /* YYY hint is used here on i386 */
1466 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1467 pmap->pm_ptphint = m;
1472 * Here if the pte page isn't mapped, or if it has been deallocated.
1474 return _pmap_allocpte(pmap, ptepindex);
1478 /***************************************************
1479 * Pmap allocation/deallocation routines.
1480 ***************************************************/
1483 * Release any resources held by the given physical map.
1484 * Called when a pmap initialized by pmap_pinit is being released.
1485 * Should only be called if the map contains no valid mappings.
1487 static int pmap_release_callback(struct vm_page *p, void *data);
1490 pmap_release(struct pmap *pmap)
1492 vm_object_t object = pmap->pm_pteobj;
1493 struct rb_vm_page_scan_info info;
1495 KKASSERT(pmap != &kernel_pmap);
1497 #if defined(DIAGNOSTIC)
1498 if (object->ref_count != 1)
1499 panic("pmap_release: pteobj reference count != 1");
1503 info.object = object;
1505 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1512 info.limit = object->generation;
1514 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1515 pmap_release_callback, &info);
1516 if (info.error == 0 && info.mpte) {
1517 if (!pmap_release_free_page(pmap, info.mpte))
1521 } while (info.error);
1525 pmap_release_callback(struct vm_page *p, void *data)
1527 struct rb_vm_page_scan_info *info = data;
1529 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1533 if (!pmap_release_free_page(info->pmap, p)) {
1537 if (info->object->generation != info->limit) {
1545 * Grow the number of kernel page table entries, if needed.
1549 pmap_growkernel(vm_offset_t addr)
1552 vm_offset_t ptppaddr;
1554 pd_entry_t *pde, newpdir;
1558 if (kernel_vm_end == 0) {
1559 kernel_vm_end = KvaStart;
1561 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & VPTE_V) != 0) {
1562 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1564 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1565 kernel_vm_end = kernel_map.max_offset;
1570 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1571 if (addr - 1 >= kernel_map.max_offset)
1572 addr = kernel_map.max_offset;
1573 while (kernel_vm_end < addr) {
1574 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1576 /* We need a new PDP entry */
1577 nkpg = vm_page_alloc(kptobj, nkpt,
1578 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1579 | VM_ALLOC_INTERRUPT);
1581 panic("pmap_growkernel: no memory to grow kernel");
1582 paddr = VM_PAGE_TO_PHYS(nkpg);
1583 if ((nkpg->flags & PG_ZERO) == 0)
1584 pmap_zero_page(paddr);
1585 vm_page_flag_clear(nkpg, PG_ZERO);
1586 newpdp = (pdp_entry_t)
1587 (paddr | VPTE_V | VPTE_R | VPTE_W | VPTE_A | VPTE_M);
1588 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1590 continue; /* try again */
1592 if ((*pde & VPTE_V) != 0) {
1593 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1594 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1595 kernel_vm_end = kernel_map.max_offset;
1602 * This index is bogus, but out of the way
1604 nkpg = vm_page_alloc(kptobj, nkpt,
1605 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1607 panic("pmap_growkernel: no memory to grow kernel");
1610 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1611 pmap_zero_page(ptppaddr);
1612 vm_page_flag_clear(nkpg, PG_ZERO);
1613 newpdir = (pd_entry_t) (ptppaddr | VPTE_V | VPTE_R | VPTE_W | VPTE_A | VPTE_M);
1614 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1617 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1618 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1619 kernel_vm_end = kernel_map.max_offset;
1627 * Retire the given physical map from service.
1628 * Should only be called if the map contains
1629 * no valid mappings.
1632 pmap_destroy(pmap_t pmap)
1639 count = --pmap->pm_count;
1642 panic("destroying a pmap is not yet implemented");
1647 * Add a reference to the specified pmap.
1650 pmap_reference(pmap_t pmap)
1657 /************************************************************************
1658 * VMSPACE MANAGEMENT *
1659 ************************************************************************
1661 * The VMSPACE management we do in our virtual kernel must be reflected
1662 * in the real kernel. This is accomplished by making vmspace system
1663 * calls to the real kernel.
1666 cpu_vmspace_alloc(struct vmspace *vm)
1672 #define USER_SIZE (VM_MAX_USER_ADDRESS - VM_MIN_USER_ADDRESS)
1674 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
1675 panic("vmspace_create() failed");
1677 rp = vmspace_mmap(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1678 PROT_READ|PROT_WRITE,
1679 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
1681 if (rp == MAP_FAILED)
1682 panic("vmspace_mmap: failed");
1683 vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1685 vpte = VM_PAGE_TO_PHYS(vmspace_pmap(vm)->pm_pdirm) | VPTE_R | VPTE_W | VPTE_V;
1686 r = vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1689 panic("vmspace_mcontrol: failed");
1693 cpu_vmspace_free(struct vmspace *vm)
1695 if (vmspace_destroy(&vm->vm_pmap) < 0)
1696 panic("vmspace_destroy() failed");
1699 /***************************************************
1700 * page management routines.
1701 ***************************************************/
1704 * free the pv_entry back to the free list. This function may be
1705 * called from an interrupt.
1707 static __inline void
1708 free_pv_entry(pv_entry_t pv)
1711 KKASSERT(pv_entry_count >= 0);
1716 * get a new pv_entry, allocating a block from the system
1717 * when needed. This function may be called from an interrupt.
1723 if (pv_entry_high_water &&
1724 (pv_entry_count > pv_entry_high_water) &&
1725 (pmap_pagedaemon_waken == 0)) {
1726 pmap_pagedaemon_waken = 1;
1727 wakeup(&vm_pages_needed);
1729 return zalloc(pvzone);
1733 * This routine is very drastic, but can save the system
1741 static int warningdone=0;
1743 if (pmap_pagedaemon_waken == 0)
1745 pmap_pagedaemon_waken = 0;
1747 if (warningdone < 5) {
1748 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1752 for(i = 0; i < vm_page_array_size; i++) {
1753 m = &vm_page_array[i];
1754 if (m->wire_count || m->hold_count || m->busy ||
1755 (m->flags & PG_BUSY))
1763 * If it is the first entry on the list, it is actually
1764 * in the header and we must copy the following entry up
1765 * to the header. Otherwise we must search the list for
1766 * the entry. In either case we free the now unused entry.
1769 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1775 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1776 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1777 if (pmap == pv->pv_pmap && va == pv->pv_va)
1781 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1782 if (va == pv->pv_va)
1788 * Note that pv_ptem is NULL if the page table page itself is not
1789 * managed, even if the page being removed IS managed.
1792 /* JGXXX When can 'pv' be NULL? */
1794 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1795 m->md.pv_list_count--;
1796 KKASSERT(m->md.pv_list_count >= 0);
1797 if (TAILQ_EMPTY(&m->md.pv_list))
1798 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1799 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1800 ++pmap->pm_generation;
1801 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1809 * Create a pv entry for page at pa for (pmap, va). If the page table page
1810 * holding the VA is managed, mpte will be non-NULL.
1813 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1818 pv = get_pv_entry();
1823 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1824 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1825 m->md.pv_list_count++;
1831 * pmap_remove_pte: do the things to unmap a page in a process
1834 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1839 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1840 if (oldpte & VPTE_WIRED)
1841 --pmap->pm_stats.wired_count;
1842 KKASSERT(pmap->pm_stats.wired_count >= 0);
1846 * Machines that don't support invlpg, also don't support
1847 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1851 cpu_invlpg((void *)va);
1853 KKASSERT(pmap->pm_stats.resident_count > 0);
1854 --pmap->pm_stats.resident_count;
1855 if (oldpte & VPTE_MANAGED) {
1856 m = PHYS_TO_VM_PAGE(oldpte);
1857 if (oldpte & VPTE_M) {
1858 #if defined(PMAP_DIAGNOSTIC)
1859 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1861 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
1865 if (pmap_track_modified(pmap, va))
1868 if (oldpte & VPTE_A)
1869 vm_page_flag_set(m, PG_REFERENCED);
1870 return pmap_remove_entry(pmap, m, va);
1872 return pmap_unuse_pt(pmap, va, NULL);
1881 * Remove a single page from a process address space.
1883 * This function may not be called from an interrupt if the pmap is
1887 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1891 pte = pmap_pte(pmap, va);
1894 if ((*pte & VPTE_V) == 0)
1896 pmap_remove_pte(pmap, pte, va);
1902 * Remove the given range of addresses from the specified map.
1904 * It is assumed that the start and end are properly
1905 * rounded to the page size.
1907 * This function may not be called from an interrupt if the pmap is
1911 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1913 vm_offset_t va_next;
1914 pml4_entry_t *pml4e;
1916 pd_entry_t ptpaddr, *pde;
1922 KKASSERT(pmap->pm_stats.resident_count >= 0);
1923 if (pmap->pm_stats.resident_count == 0)
1927 * special handling of removing one page. a very
1928 * common operation and easy to short circuit some
1931 if (sva + PAGE_SIZE == eva) {
1932 pde = pmap_pde(pmap, sva);
1933 if (pde && (*pde & VPTE_PS) == 0) {
1934 pmap_remove_page(pmap, sva);
1939 for (; sva < eva; sva = va_next) {
1940 pml4e = pmap_pml4e(pmap, sva);
1941 if ((*pml4e & VPTE_V) == 0) {
1942 va_next = (sva + NBPML4) & ~PML4MASK;
1948 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
1949 if ((*pdpe & VPTE_V) == 0) {
1950 va_next = (sva + NBPDP) & ~PDPMASK;
1957 * Calculate index for next page table.
1959 va_next = (sva + NBPDR) & ~PDRMASK;
1963 pde = pmap_pdpe_to_pde(pdpe, sva);
1967 * Weed out invalid mappings.
1973 * Check for large page.
1975 if ((ptpaddr & VPTE_PS) != 0) {
1976 /* JG FreeBSD has more complex treatment here */
1977 KKASSERT(*pde != 0);
1978 pmap_inval_pde(pde, pmap, sva);
1979 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1984 * Limit our scan to either the end of the va represented
1985 * by the current page table page, or to the end of the
1986 * range being removed.
1992 * NOTE: pmap_remove_pte() can block.
1994 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
1998 if (pmap_remove_pte(pmap, pte, sva))
2007 * Removes this physical page from all physical maps in which it resides.
2008 * Reflects back modify bits to the pager.
2010 * This routine may not be called from an interrupt.
2014 pmap_remove_all(vm_page_t m)
2016 pt_entry_t *pte, tpte;
2019 #if defined(PMAP_DIAGNOSTIC)
2021 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
2024 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
2025 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
2030 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2031 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2032 --pv->pv_pmap->pm_stats.resident_count;
2034 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2035 KKASSERT(pte != NULL);
2037 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
2038 if (tpte & VPTE_WIRED)
2039 pv->pv_pmap->pm_stats.wired_count--;
2040 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
2043 vm_page_flag_set(m, PG_REFERENCED);
2046 * Update the vm_page_t clean and reference bits.
2048 if (tpte & VPTE_M) {
2049 #if defined(PMAP_DIAGNOSTIC)
2050 if (pmap_nw_modified(tpte)) {
2052 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2056 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
2059 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2060 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2061 ++pv->pv_pmap->pm_generation;
2062 m->md.pv_list_count--;
2063 KKASSERT(m->md.pv_list_count >= 0);
2064 if (TAILQ_EMPTY(&m->md.pv_list))
2065 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2066 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2069 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2076 * Set the physical protection on the specified range of this map
2079 * This function may not be called from an interrupt if the map is
2080 * 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 for (; sva < eva; sva = va_next) {
2106 pml4e = pmap_pml4e(pmap, sva);
2107 if ((*pml4e & VPTE_V) == 0) {
2108 va_next = (sva + NBPML4) & ~PML4MASK;
2114 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2115 if ((*pdpe & VPTE_V) == 0) {
2116 va_next = (sva + NBPDP) & ~PDPMASK;
2122 va_next = (sva + NBPDR) & ~PDRMASK;
2126 pde = pmap_pdpe_to_pde(pdpe, sva);
2130 * Check for large page.
2132 if ((ptpaddr & VPTE_PS) != 0) {
2134 pmap_clean_pde(pde, pmap, sva);
2135 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2140 * Weed out invalid mappings. Note: we assume that the page
2141 * directory table is always allocated, and in kernel virtual.
2149 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2151 pt_entry_t obits, pbits;
2155 * Clean managed pages and also check the accessed
2156 * bit. Just remove write perms for unmanaged
2157 * pages. Be careful of races, turning off write
2158 * access will force a fault rather then setting
2159 * the modified bit at an unexpected time.
2161 if (*pte & VPTE_MANAGED) {
2162 pbits = pmap_clean_pte(pte, pmap, sva);
2164 if (pbits & VPTE_A) {
2165 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2166 vm_page_flag_set(m, PG_REFERENCED);
2167 atomic_clear_long(pte, VPTE_A);
2169 if (pbits & VPTE_M) {
2170 if (pmap_track_modified(pmap, sva)) {
2172 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2177 pbits = pmap_setro_pte(pte, pmap, sva);
2184 * Enter a managed page into a pmap. If the page is not wired related pmap
2185 * data can be destroyed at any time for later demand-operation.
2187 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
2188 * specified protection, and wire the mapping if requested.
2190 * NOTE: This routine may not lazy-evaluate or lose information. The
2191 * page must actually be inserted into the given map NOW.
2193 * NOTE: When entering a page at a KVA address, the pmap must be the
2197 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2204 pt_entry_t origpte, newpte;
2210 va = trunc_page(va);
2213 * Get the page table page. The kernel_pmap's page table pages
2214 * are preallocated and have no associated vm_page_t.
2216 if (pmap == &kernel_pmap)
2219 mpte = pmap_allocpte(pmap, va);
2221 pde = pmap_pde(pmap, va);
2222 if (pde != NULL && (*pde & VPTE_V) != 0) {
2223 if ((*pde & VPTE_PS) != 0)
2224 panic("pmap_enter: attempted pmap_enter on 2MB page");
2225 pte = pmap_pde_to_pte(pde, va);
2227 panic("pmap_enter: invalid page directory va=%#lx", va);
2229 KKASSERT(pte != NULL);
2231 * Deal with races on the original mapping (though don't worry
2232 * about VPTE_A races) by cleaning it. This will force a fault
2233 * if an attempt is made to write to the page.
2235 pa = VM_PAGE_TO_PHYS(m);
2236 origpte = pmap_clean_pte(pte, pmap, va);
2237 opa = origpte & VPTE_FRAME;
2239 if (origpte & VPTE_PS)
2240 panic("pmap_enter: attempted pmap_enter on 2MB page");
2243 * Mapping has not changed, must be protection or wiring change.
2245 if (origpte && (opa == pa)) {
2247 * Wiring change, just update stats. We don't worry about
2248 * wiring PT pages as they remain resident as long as there
2249 * are valid mappings in them. Hence, if a user page is wired,
2250 * the PT page will be also.
2252 if (wired && ((origpte & VPTE_WIRED) == 0))
2253 ++pmap->pm_stats.wired_count;
2254 else if (!wired && (origpte & VPTE_WIRED))
2255 --pmap->pm_stats.wired_count;
2258 * Remove the extra pte reference. Note that we cannot
2259 * optimize the RO->RW case because we have adjusted the
2260 * wiring count above and may need to adjust the wiring
2267 * We might be turning off write access to the page,
2268 * so we go ahead and sense modify status.
2270 if (origpte & VPTE_MANAGED) {
2271 if ((origpte & VPTE_M) &&
2272 pmap_track_modified(pmap, va)) {
2274 om = PHYS_TO_VM_PAGE(opa);
2278 KKASSERT(m->flags & PG_MAPPED);
2283 * Mapping has changed, invalidate old range and fall through to
2284 * handle validating new mapping.
2288 err = pmap_remove_pte(pmap, pte, va);
2290 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2294 * Enter on the PV list if part of our managed memory. Note that we
2295 * raise IPL while manipulating pv_table since pmap_enter can be
2296 * called at interrupt time.
2298 if (pmap_initialized &&
2299 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2300 pmap_insert_entry(pmap, va, mpte, m);
2302 vm_page_flag_set(m, PG_MAPPED);
2306 * Increment counters
2308 ++pmap->pm_stats.resident_count;
2310 pmap->pm_stats.wired_count++;
2314 * Now validate mapping with desired protection/wiring.
2316 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | VPTE_V);
2319 newpte |= VPTE_WIRED;
2320 if (pmap != &kernel_pmap)
2324 * If the mapping or permission bits are different from the
2325 * (now cleaned) original pte, an update is needed. We've
2326 * already downgraded or invalidated the page so all we have
2327 * to do now is update the bits.
2329 * XXX should we synchronize RO->RW changes to avoid another
2332 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
2333 *pte = newpte | VPTE_A;
2334 if (newpte & VPTE_W)
2335 vm_page_flag_set(m, PG_WRITEABLE);
2337 KKASSERT((newpte & VPTE_MANAGED) == 0 || (m->flags & PG_MAPPED));
2341 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2343 * Currently this routine may only be used on user pmaps, not kernel_pmap.
2346 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2351 vm_pindex_t ptepindex;
2354 KKASSERT(pmap != &kernel_pmap);
2356 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
2359 * Calculate pagetable page index
2361 ptepindex = pmap_pde_pindex(va);
2365 * Get the page directory entry
2367 ptepa = pmap_pde(pmap, va);
2370 * If the page table page is mapped, we just increment
2371 * the hold count, and activate it.
2373 if (ptepa && (*ptepa & VPTE_V) != 0) {
2374 if (*ptepa & VPTE_PS)
2375 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2376 if (pmap->pm_ptphint &&
2377 (pmap->pm_ptphint->pindex == ptepindex)) {
2378 mpte = pmap->pm_ptphint;
2380 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2381 pmap->pm_ptphint = mpte;
2386 mpte = _pmap_allocpte(pmap, ptepindex);
2388 } while (mpte == NULL);
2391 * Ok, now that the page table page has been validated, get the pte.
2392 * If the pte is already mapped undo mpte's hold_count and
2395 pte = pmap_pte(pmap, va);
2396 if (*pte & VPTE_V) {
2397 KKASSERT(mpte != NULL);
2398 pmap_unwire_pte_hold(pmap, va, mpte);
2399 pa = VM_PAGE_TO_PHYS(m);
2400 KKASSERT(((*pte ^ pa) & VPTE_FRAME) == 0);
2405 * Enter on the PV list if part of our managed memory
2407 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2408 pmap_insert_entry(pmap, va, mpte, m);
2409 vm_page_flag_set(m, PG_MAPPED);
2413 * Increment counters
2415 ++pmap->pm_stats.resident_count;
2417 pa = VM_PAGE_TO_PHYS(m);
2420 * Now validate mapping with RO protection
2422 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2423 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2425 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2426 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2427 /*pmap_inval_flush(&info); don't need for vkernel */
2431 * Make a temporary mapping for a physical address. This is only intended
2432 * to be used for panic dumps.
2435 pmap_kenter_temporary(vm_paddr_t pa, int i)
2437 pmap_kenter(crashdumpmap + (i * PAGE_SIZE), pa);
2438 return ((void *)crashdumpmap);
2441 #define MAX_INIT_PT (96)
2444 * This routine preloads the ptes for a given object into the specified pmap.
2445 * This eliminates the blast of soft faults on process startup and
2446 * immediately after an mmap.
2448 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2451 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2452 vm_object_t object, vm_pindex_t pindex,
2453 vm_size_t size, int limit)
2455 struct rb_vm_page_scan_info info;
2460 * We can't preinit if read access isn't set or there is no pmap
2463 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2467 * We can't preinit if the pmap is not the current pmap
2469 lp = curthread->td_lwp;
2470 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2473 psize = x86_64_btop(size);
2475 if ((object->type != OBJT_VNODE) ||
2476 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2477 (object->resident_page_count > MAX_INIT_PT))) {
2481 if (psize + pindex > object->size) {
2482 if (object->size < pindex)
2484 psize = object->size - pindex;
2491 * Use a red-black scan to traverse the requested range and load
2492 * any valid pages found into the pmap.
2494 * We cannot safely scan the object's memq unless we are in a
2495 * critical section since interrupts can remove pages from objects.
2497 info.start_pindex = pindex;
2498 info.end_pindex = pindex + psize - 1;
2505 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2506 pmap_object_init_pt_callback, &info);
2512 pmap_object_init_pt_callback(vm_page_t p, void *data)
2514 struct rb_vm_page_scan_info *info = data;
2515 vm_pindex_t rel_index;
2517 * don't allow an madvise to blow away our really
2518 * free pages allocating pv entries.
2520 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2521 vmstats.v_free_count < vmstats.v_free_reserved) {
2524 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2525 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2526 if ((p->queue - p->pc) == PQ_CACHE)
2527 vm_page_deactivate(p);
2529 rel_index = p->pindex - info->start_pindex;
2530 pmap_enter_quick(info->pmap,
2531 info->addr + x86_64_ptob(rel_index), p);
2538 * Return TRUE if the pmap is in shape to trivially
2539 * pre-fault the specified address.
2541 * Returns FALSE if it would be non-trivial or if a
2542 * pte is already loaded into the slot.
2545 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2550 pde = pmap_pde(pmap, addr);
2551 if (pde == NULL || *pde == 0)
2554 pte = pmap_pde_to_pte(pde, addr);
2562 * Routine: pmap_change_wiring
2563 * Function: Change the wiring attribute for a map/virtual-address
2565 * In/out conditions:
2566 * The mapping must already exist in the pmap.
2569 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2576 pte = pmap_pte(pmap, va);
2578 if (wired && !pmap_pte_w(pte))
2579 pmap->pm_stats.wired_count++;
2580 else if (!wired && pmap_pte_w(pte))
2581 pmap->pm_stats.wired_count--;
2584 * Wiring is not a hardware characteristic so there is no need to
2585 * invalidate TLB. However, in an SMP environment we must use
2586 * a locked bus cycle to update the pte (if we are not using
2587 * the pmap_inval_*() API that is)... it's ok to do this for simple
2591 atomic_set_long(pte, VPTE_WIRED);
2593 atomic_clear_long(pte, VPTE_WIRED);
2597 * Copy the range specified by src_addr/len
2598 * from the source map to the range dst_addr/len
2599 * in the destination map.
2601 * This routine is only advisory and need not do anything.
2604 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2605 vm_size_t len, vm_offset_t src_addr)
2608 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2609 * valid through blocking calls, and that's just not going to
2620 * Zero the specified physical page.
2622 * This function may be called from an interrupt and no locking is
2626 pmap_zero_page(vm_paddr_t phys)
2628 vm_offset_t va = PHYS_TO_DMAP(phys);
2630 bzero((void *)va, PAGE_SIZE);
2634 * pmap_page_assertzero:
2636 * Assert that a page is empty, panic if it isn't.
2639 pmap_page_assertzero(vm_paddr_t phys)
2644 vm_offset_t virt = PHYS_TO_DMAP(phys);
2646 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
2647 if (*(int *)((char *)virt + i) != 0) {
2648 panic("pmap_page_assertzero() @ %p not zero!\n",
2658 * Zero part of a physical page by mapping it into memory and clearing
2659 * its contents with bzero.
2661 * off and size may not cover an area beyond a single hardware page.
2664 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2667 vm_offset_t virt = PHYS_TO_DMAP(phys);
2668 bzero((char *)virt + off, size);
2675 * Copy the physical page from the source PA to the target PA.
2676 * This function may be called from an interrupt. No locking
2680 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2682 vm_offset_t src_virt, dst_virt;
2685 src_virt = PHYS_TO_DMAP(src);
2686 dst_virt = PHYS_TO_DMAP(dst);
2687 bcopy(src_virt, dst_virt, PAGE_SIZE);
2692 * pmap_copy_page_frag:
2694 * Copy the physical page from the source PA to the target PA.
2695 * This function may be called from an interrupt. No locking
2699 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2701 vm_offset_t src_virt, dst_virt;
2704 src_virt = PHYS_TO_DMAP(src);
2705 dst_virt = PHYS_TO_DMAP(dst);
2706 bcopy((char *)src_virt + (src & PAGE_MASK),
2707 (char *)dst_virt + (dst & PAGE_MASK),
2713 * Returns true if the pmap's pv is one of the first
2714 * 16 pvs linked to from this page. This count may
2715 * be changed upwards or downwards in the future; it
2716 * is only necessary that true be returned for a small
2717 * subset of pmaps for proper page aging.
2720 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2725 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2730 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2731 if (pv->pv_pmap == pmap) {
2744 * Remove all pages from specified address space
2745 * this aids process exit speeds. Also, this code
2746 * is special cased for current process only, but
2747 * can have the more generic (and slightly slower)
2748 * mode enabled. This is much faster than pmap_remove
2749 * in the case of running down an entire address space.
2752 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2755 pt_entry_t *pte, tpte;
2759 int save_generation;
2761 lp = curthread->td_lwp;
2762 if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
2768 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2769 if (pv->pv_va >= eva || pv->pv_va < sva) {
2770 npv = TAILQ_NEXT(pv, pv_plist);
2774 KKASSERT(pmap == pv->pv_pmap);
2776 pte = pmap_pte(pmap, pv->pv_va);
2779 * We cannot remove wired pages from a process' mapping
2782 if (*pte & VPTE_WIRED) {
2783 npv = TAILQ_NEXT(pv, pv_plist);
2786 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2788 m = PHYS_TO_VM_PAGE(tpte & VPTE_FRAME);
2790 KASSERT(m < &vm_page_array[vm_page_array_size],
2791 ("pmap_remove_pages: bad tpte %lx", tpte));
2793 KKASSERT(pmap->pm_stats.resident_count > 0);
2794 --pmap->pm_stats.resident_count;
2797 * Update the vm_page_t clean and reference bits.
2799 if (tpte & VPTE_M) {
2803 npv = TAILQ_NEXT(pv, pv_plist);
2804 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2805 save_generation = ++pmap->pm_generation;
2807 m->md.pv_list_count--;
2808 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2809 if (TAILQ_EMPTY(&m->md.pv_list))
2810 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2812 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2816 * Restart the scan if we blocked during the unuse or free
2817 * calls and other removals were made.
2819 if (save_generation != pmap->pm_generation) {
2820 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2821 pv = TAILQ_FIRST(&pmap->pm_pvlist);
2828 * pmap_testbit tests bits in active mappings of a VM page.
2831 pmap_testbit(vm_page_t m, int bit)
2836 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2839 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2844 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2846 * if the bit being tested is the modified bit, then
2847 * mark clean_map and ptes as never
2850 if (bit & (VPTE_A|VPTE_M)) {
2851 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2855 #if defined(PMAP_DIAGNOSTIC)
2856 if (pv->pv_pmap == NULL) {
2857 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
2861 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2872 * This routine is used to clear bits in ptes. Certain bits require special
2873 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2875 * This routine is only called with certain VPTE_* bit combinations.
2877 static __inline void
2878 pmap_clearbit(vm_page_t m, int bit)
2884 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2890 * Loop over all current mappings setting/clearing as appropos If
2891 * setting RO do we need to clear the VAC?
2893 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2895 * don't write protect pager mappings
2897 if (bit == VPTE_W) {
2898 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2902 #if defined(PMAP_DIAGNOSTIC)
2903 if (pv->pv_pmap == NULL) {
2904 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
2910 * Careful here. We can use a locked bus instruction to
2911 * clear VPTE_A or VPTE_M safely but we need to synchronize
2912 * with the target cpus when we mess with VPTE_W.
2914 * On virtual kernels we must force a new fault-on-write
2915 * in the real kernel if we clear the Modify bit ourselves,
2916 * otherwise the real kernel will not get a new fault and
2917 * will never set our Modify bit again.
2919 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2921 if (bit == VPTE_W) {
2923 * We must also clear VPTE_M when clearing
2926 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2930 } else if (bit == VPTE_M) {
2932 * We do not have to make the page read-only
2933 * when clearing the Modify bit. The real
2934 * kernel will make the real PTE read-only
2935 * or otherwise detect the write and set
2936 * our VPTE_M again simply by us invalidating
2937 * the real kernel VA for the pmap (as we did
2938 * above). This allows the real kernel to
2939 * handle the write fault without forwarding
2942 atomic_clear_long(pte, VPTE_M);
2943 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2945 * We've been asked to clear W & M, I guess
2946 * the caller doesn't want us to update
2947 * the dirty status of the VM page.
2949 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2952 * We've been asked to clear bits that do
2953 * not interact with hardware.
2955 atomic_clear_long(pte, bit);
2963 * pmap_page_protect:
2965 * Lower the permission for all mappings to a given page.
2968 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2970 /* JG NX support? */
2971 if ((prot & VM_PROT_WRITE) == 0) {
2972 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2973 pmap_clearbit(m, VPTE_W);
2974 vm_page_flag_clear(m, PG_WRITEABLE);
2982 pmap_phys_address(vm_pindex_t ppn)
2984 return (x86_64_ptob(ppn));
2988 * pmap_ts_referenced:
2990 * Return a count of reference bits for a page, clearing those bits.
2991 * It is not necessary for every reference bit to be cleared, but it
2992 * is necessary that 0 only be returned when there are truly no
2993 * reference bits set.
2995 * XXX: The exact number of bits to check and clear is a matter that
2996 * should be tested and standardized at some point in the future for
2997 * optimal aging of shared pages.
3000 pmap_ts_referenced(vm_page_t m)
3002 pv_entry_t pv, pvf, pvn;
3006 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3011 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3016 pvn = TAILQ_NEXT(pv, pv_list);
3018 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3020 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3022 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3025 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3027 if (pte && (*pte & VPTE_A)) {
3029 atomic_clear_long(pte, VPTE_A);
3031 atomic_clear_long_nonlocked(pte, VPTE_A);
3038 } while ((pv = pvn) != NULL && pv != pvf);
3048 * Return whether or not the specified physical page was modified
3049 * in any physical maps.
3052 pmap_is_modified(vm_page_t m)
3054 return pmap_testbit(m, VPTE_M);
3058 * Clear the modify bits on the specified physical page.
3061 pmap_clear_modify(vm_page_t m)
3063 pmap_clearbit(m, VPTE_M);
3067 * pmap_clear_reference:
3069 * Clear the reference bit on the specified physical page.
3072 pmap_clear_reference(vm_page_t m)
3074 pmap_clearbit(m, VPTE_A);
3078 * Miscellaneous support routines follow
3082 i386_protection_init(void)
3086 kp = protection_codes;
3087 for (prot = 0; prot < 8; prot++) {
3088 if (prot & VM_PROT_READ)
3090 if (prot & VM_PROT_WRITE)
3092 if (prot & VM_PROT_EXECUTE)
3099 * perform the pmap work for mincore
3102 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3104 pt_entry_t *ptep, pte;
3108 ptep = pmap_pte(pmap, addr);
3113 if ((pte = *ptep) != 0) {
3116 val = MINCORE_INCORE;
3117 if ((pte & VPTE_MANAGED) == 0)
3120 pa = pte & VPTE_FRAME;
3122 m = PHYS_TO_VM_PAGE(pa);
3128 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3130 * Modified by someone
3132 else if (m->dirty || pmap_is_modified(m))
3133 val |= MINCORE_MODIFIED_OTHER;
3138 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3141 * Referenced by someone
3143 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3144 val |= MINCORE_REFERENCED_OTHER;
3145 vm_page_flag_set(m, PG_REFERENCED);
3152 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3153 * vmspace will be ref'd and the old one will be deref'd.
3156 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3158 struct vmspace *oldvm;
3162 oldvm = p->p_vmspace;
3163 if (oldvm != newvm) {
3164 p->p_vmspace = newvm;
3165 KKASSERT(p->p_nthreads == 1);
3166 lp = RB_ROOT(&p->p_lwp_tree);
3167 pmap_setlwpvm(lp, newvm);
3169 sysref_get(&newvm->vm_sysref);
3170 sysref_put(&oldvm->vm_sysref);
3177 * Set the vmspace for a LWP. The vmspace is almost universally set the
3178 * same as the process vmspace, but virtual kernels need to swap out contexts
3179 * on a per-lwp basis.
3182 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3184 struct vmspace *oldvm;
3188 oldvm = lp->lwp_vmspace;
3190 if (oldvm != newvm) {
3191 lp->lwp_vmspace = newvm;
3192 if (curthread->td_lwp == lp) {
3193 pmap = vmspace_pmap(newvm);
3195 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3197 pmap->pm_active |= 1;
3199 #if defined(SWTCH_OPTIM_STATS)
3202 pmap = vmspace_pmap(oldvm);
3204 atomic_clear_int(&pmap->pm_active,
3205 1 << mycpu->gd_cpuid);
3207 pmap->pm_active &= ~1;
3215 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3218 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3222 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3229 static void pads (pmap_t pm);
3230 void pmap_pvdump (vm_paddr_t pa);
3232 /* print address space of pmap*/
3240 if (pm == &kernel_pmap)
3243 for (i = 0; i < NPDEPG; i++) {
3245 if (pm->pm_pdir[i]) {
3246 for (j = 0; j < NPTEPG; j++) {
3247 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3248 if (pm == &kernel_pmap && va < KERNBASE)
3250 if (pm != &kernel_pmap && va > UPT_MAX_ADDRESS)
3252 ptep = pmap_pte_quick(pm, va);
3253 if (pmap_pte_v(ptep))
3254 kprintf("%lx:%lx ", va, *ptep);
3264 pmap_pvdump(vm_paddr_t pa)
3269 kprintf("pa %08llx", (long long)pa);
3270 m = PHYS_TO_VM_PAGE(pa);
3271 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3273 kprintf(" -> pmap %p, va %x, flags %x",
3274 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3276 kprintf(" -> pmap %p, va %lx", (void *)pv->pv_pmap, pv->pv_va);