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>
84 #include <vm/vm_page2.h>
86 #include <machine/cputypes.h>
87 #include <machine/md_var.h>
88 #include <machine/specialreg.h>
89 #include <machine/smp.h>
90 #include <machine/globaldata.h>
91 #include <machine/pmap.h>
92 #include <machine/pmap_inval.h>
101 #define PMAP_KEEP_PDIRS
102 #ifndef PMAP_SHPGPERPROC
103 #define PMAP_SHPGPERPROC 1000
106 #if defined(DIAGNOSTIC)
107 #define PMAP_DIAGNOSTIC
112 #if !defined(PMAP_DIAGNOSTIC)
113 #define PMAP_INLINE __inline
119 * Get PDEs and PTEs for user/kernel address space
121 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
122 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
124 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & VPTE_V) != 0)
125 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & VPTE_WIRED) != 0)
126 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & VPTE_M) != 0)
127 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & VPTE_A) != 0)
128 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & VPTE_V) != 0)
131 * Given a map and a machine independent protection code,
132 * convert to a vax protection code.
134 #define pte_prot(m, p) \
135 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
136 static int protection_codes[8];
138 struct pmap kernel_pmap;
139 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
141 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
143 static vm_object_t kptobj;
147 static uint64_t KPDphys; /* phys addr of kernel level 2 */
148 uint64_t KPDPphys; /* phys addr of kernel level 3 */
149 uint64_t KPML4phys; /* phys addr of kernel level 4 */
151 extern int vmm_enabled;
152 extern void *vkernel_stack;
155 * Data for the pv entry allocation mechanism
157 static vm_zone_t pvzone;
158 static struct vm_zone pvzone_store;
159 static struct vm_object pvzone_obj;
160 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
161 static int pmap_pagedaemon_waken = 0;
162 static struct pv_entry *pvinit;
165 * All those kernel PT submaps that BSD is so fond of
167 pt_entry_t *CMAP1 = NULL, *ptmmap;
168 caddr_t CADDR1 = NULL;
169 static pt_entry_t *msgbufmap;
173 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
174 static pv_entry_t get_pv_entry (void);
175 static void i386_protection_init (void);
176 static __inline void pmap_clearbit (vm_page_t m, int bit);
178 static void pmap_remove_all (vm_page_t m);
179 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
181 static void pmap_remove_page (struct pmap *pmap, vm_offset_t va);
182 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
184 static boolean_t pmap_testbit (vm_page_t m, int bit);
185 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
186 vm_page_t mpte, vm_page_t m);
188 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
190 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
191 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
193 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
195 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
196 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t);
201 * Super fast pmap_pte routine best used when scanning the pv lists.
202 * This eliminates many course-grained invltlb calls. Note that many of
203 * the pv list scans are across different pmaps and it is very wasteful
204 * to do an entire invltlb when checking a single mapping.
206 * Should only be called while in a critical section.
209 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
212 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
214 return pmap_pte(pmap, va);
218 /* Return a non-clipped PD index for a given VA */
219 static __inline vm_pindex_t
220 pmap_pde_pindex(vm_offset_t va)
222 return va >> PDRSHIFT;
225 /* Return various clipped indexes for a given VA */
226 static __inline vm_pindex_t
227 pmap_pte_index(vm_offset_t va)
230 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
233 static __inline vm_pindex_t
234 pmap_pde_index(vm_offset_t va)
237 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
240 static __inline vm_pindex_t
241 pmap_pdpe_index(vm_offset_t va)
244 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
247 static __inline vm_pindex_t
248 pmap_pml4e_index(vm_offset_t va)
251 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
254 /* Return a pointer to the PML4 slot that corresponds to a VA */
255 static __inline pml4_entry_t *
256 pmap_pml4e(pmap_t pmap, vm_offset_t va)
259 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
262 /* Return a pointer to the PDP slot that corresponds to a VA */
263 static __inline pdp_entry_t *
264 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
268 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & VPTE_FRAME);
269 return (&pdpe[pmap_pdpe_index(va)]);
272 /* Return a pointer to the PDP slot that corresponds to a VA */
273 static __inline pdp_entry_t *
274 pmap_pdpe(pmap_t pmap, vm_offset_t va)
278 pml4e = pmap_pml4e(pmap, va);
279 if ((*pml4e & VPTE_V) == 0)
281 return (pmap_pml4e_to_pdpe(pml4e, va));
284 /* Return a pointer to the PD slot that corresponds to a VA */
285 static __inline pd_entry_t *
286 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
290 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & VPTE_FRAME);
291 return (&pde[pmap_pde_index(va)]);
294 /* Return a pointer to the PD slot that corresponds to a VA */
295 static __inline pd_entry_t *
296 pmap_pde(pmap_t pmap, vm_offset_t va)
300 pdpe = pmap_pdpe(pmap, va);
301 if (pdpe == NULL || (*pdpe & VPTE_V) == 0)
303 return (pmap_pdpe_to_pde(pdpe, va));
306 /* Return a pointer to the PT slot that corresponds to a VA */
307 static __inline pt_entry_t *
308 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
312 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & VPTE_FRAME);
313 return (&pte[pmap_pte_index(va)]);
316 /* Return a pointer to the PT slot that corresponds to a VA */
317 static __inline pt_entry_t *
318 pmap_pte(pmap_t pmap, vm_offset_t va)
322 pde = pmap_pde(pmap, va);
323 if (pde == NULL || (*pde & VPTE_V) == 0)
325 if ((*pde & VPTE_PS) != 0) /* compat with i386 pmap_pte() */
326 return ((pt_entry_t *)pde);
327 return (pmap_pde_to_pte(pde, va));
332 PMAP_INLINE pt_entry_t *
333 vtopte(vm_offset_t va)
335 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT +
336 NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
338 return (PTmap + ((va >> PAGE_SHIFT) & mask));
341 static __inline pd_entry_t *
342 vtopde(vm_offset_t va)
344 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT +
345 NPML4EPGSHIFT)) - 1);
347 return (PDmap + ((va >> PDRSHIFT) & mask));
350 static PMAP_INLINE pt_entry_t *
351 vtopte(vm_offset_t va)
354 x = pmap_pte(&kernel_pmap, va);
359 static __inline pd_entry_t *
360 vtopde(vm_offset_t va)
363 x = pmap_pde(&kernel_pmap, va);
370 allocpages(vm_paddr_t *firstaddr, int n)
376 bzero((void *)ret, n * PAGE_SIZE);
378 *firstaddr += n * PAGE_SIZE;
383 create_dmap_vmm(vm_paddr_t *firstaddr)
386 int pml4_stack_index;
393 uint64_t KPDP_DMAP_phys = allocpages(firstaddr, NDMPML4E);
394 uint64_t KPDP_VSTACK_phys = allocpages(firstaddr, 1);
395 uint64_t KPD_VSTACK_phys = allocpages(firstaddr, 1);
397 pml4_entry_t *KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
398 pdp_entry_t *KPDP_DMAP_virt = (pdp_entry_t *)PHYS_TO_DMAP(KPDP_DMAP_phys);
399 pdp_entry_t *KPDP_VSTACK_virt = (pdp_entry_t *)PHYS_TO_DMAP(KPDP_VSTACK_phys);
400 pd_entry_t *KPD_VSTACK_virt = (pd_entry_t *)PHYS_TO_DMAP(KPD_VSTACK_phys);
402 bzero(KPDP_DMAP_virt, NDMPML4E * PAGE_SIZE);
403 bzero(KPDP_VSTACK_virt, 1 * PAGE_SIZE);
404 bzero(KPD_VSTACK_virt, 1 * PAGE_SIZE);
406 do_cpuid(0x80000001, regs);
407 amd_feature = regs[3];
409 /* Build the mappings for the first 512GB */
410 if (amd_feature & AMDID_PAGE1GB) {
411 /* In pages of 1 GB, if supported */
412 for (i = 0; i < NPDPEPG; i++) {
413 KPDP_DMAP_virt[i] = ((uint64_t)i << PDPSHIFT);
414 KPDP_DMAP_virt[i] |= VPTE_RW | VPTE_V | VPTE_PS | VPTE_U;
417 /* In page of 2MB, otherwise */
418 for (i = 0; i < NPDPEPG; i++) {
419 uint64_t KPD_DMAP_phys = allocpages(firstaddr, 1);
420 pd_entry_t *KPD_DMAP_virt = (pd_entry_t *)PHYS_TO_DMAP(KPD_DMAP_phys);
422 bzero(KPD_DMAP_virt, PAGE_SIZE);
424 KPDP_DMAP_virt[i] = KPD_DMAP_phys;
425 KPDP_DMAP_virt[i] |= VPTE_RW | VPTE_V | VPTE_U;
427 /* For each PD, we have to allocate NPTEPG PT */
428 for (j = 0; j < NPTEPG; j++) {
429 KPD_DMAP_virt[j] = (i << PDPSHIFT) | (j << PDRSHIFT);
430 KPD_DMAP_virt[j] |= VPTE_RW | VPTE_V | VPTE_PS | VPTE_U;
435 /* DMAP for the first 512G */
436 KPML4virt[0] = KPDP_DMAP_phys;
437 KPML4virt[0] |= VPTE_RW | VPTE_V | VPTE_U;
439 /* create a 2 MB map of the new stack */
440 pml4_stack_index = (uint64_t)&stack_addr >> PML4SHIFT;
441 KPML4virt[pml4_stack_index] = KPDP_VSTACK_phys;
442 KPML4virt[pml4_stack_index] |= VPTE_RW | VPTE_V | VPTE_U;
444 pdp_stack_index = ((uint64_t)&stack_addr & PML4MASK) >> PDPSHIFT;
445 KPDP_VSTACK_virt[pdp_stack_index] = KPD_VSTACK_phys;
446 KPDP_VSTACK_virt[pdp_stack_index] |= VPTE_RW | VPTE_V | VPTE_U;
448 pd_stack_index = ((uint64_t)&stack_addr & PDPMASK) >> PDRSHIFT;
449 KPD_VSTACK_virt[pd_stack_index] = (uint64_t) vkernel_stack;
450 KPD_VSTACK_virt[pd_stack_index] |= VPTE_RW | VPTE_V | VPTE_U | VPTE_PS;
454 create_pagetables(vm_paddr_t *firstaddr, int64_t ptov_offset)
457 pml4_entry_t *KPML4virt;
458 pdp_entry_t *KPDPvirt;
461 int kpml4i = pmap_pml4e_index(ptov_offset);
462 int kpdpi = pmap_pdpe_index(ptov_offset);
463 int kpdi = pmap_pde_index(ptov_offset);
466 * Calculate NKPT - number of kernel page tables. We have to
467 * accomodoate prealloction of the vm_page_array, dump bitmap,
468 * MSGBUF_SIZE, and other stuff. Be generous.
470 * Maxmem is in pages.
472 nkpt = (Maxmem * (sizeof(struct vm_page) * 2) + MSGBUF_SIZE) / NBPDR;
476 KPML4phys = allocpages(firstaddr, 1);
477 KPDPphys = allocpages(firstaddr, NKPML4E);
478 KPDphys = allocpages(firstaddr, NKPDPE);
479 KPTphys = allocpages(firstaddr, nkpt);
481 KPML4virt = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
482 KPDPvirt = (pdp_entry_t *)PHYS_TO_DMAP(KPDPphys);
483 KPDvirt = (pd_entry_t *)PHYS_TO_DMAP(KPDphys);
484 KPTvirt = (pt_entry_t *)PHYS_TO_DMAP(KPTphys);
486 bzero(KPML4virt, 1 * PAGE_SIZE);
487 bzero(KPDPvirt, NKPML4E * PAGE_SIZE);
488 bzero(KPDvirt, NKPDPE * PAGE_SIZE);
489 bzero(KPTvirt, nkpt * PAGE_SIZE);
491 /* Now map the page tables at their location within PTmap */
492 for (i = 0; i < nkpt; i++) {
493 KPDvirt[i + kpdi] = KPTphys + (i << PAGE_SHIFT);
494 KPDvirt[i + kpdi] |= VPTE_RW | VPTE_V | VPTE_U;
497 /* And connect up the PD to the PDP */
498 for (i = 0; i < NKPDPE; i++) {
499 KPDPvirt[i + kpdpi] = KPDphys + (i << PAGE_SHIFT);
500 KPDPvirt[i + kpdpi] |= VPTE_RW | VPTE_V | VPTE_U;
503 /* And recursively map PML4 to itself in order to get PTmap */
504 KPML4virt[PML4PML4I] = KPML4phys;
505 KPML4virt[PML4PML4I] |= VPTE_RW | VPTE_V | VPTE_U;
507 /* Connect the KVA slot up to the PML4 */
508 KPML4virt[kpml4i] = KPDPphys;
509 KPML4virt[kpml4i] |= VPTE_RW | VPTE_V | VPTE_U;
513 * Typically used to initialize a fictitious page by vm/device_pager.c
516 pmap_page_init(struct vm_page *m)
519 TAILQ_INIT(&m->md.pv_list);
523 * Bootstrap the system enough to run with virtual memory.
525 * On the i386 this is called after mapping has already been enabled
526 * and just syncs the pmap module with what has already been done.
527 * [We can't call it easily with mapping off since the kernel is not
528 * mapped with PA == VA, hence we would have to relocate every address
529 * from the linked base (virtual) address "KERNBASE" to the actual
530 * (physical) address starting relative to 0]
533 pmap_bootstrap(vm_paddr_t *firstaddr, int64_t ptov_offset)
539 * Create an initial set of page tables to run the kernel in.
541 create_pagetables(firstaddr, ptov_offset);
543 /* Create the DMAP for the VMM */
545 create_dmap_vmm(firstaddr);
548 virtual_start = KvaStart;
549 virtual_end = KvaEnd;
552 * Initialize protection array.
554 i386_protection_init();
557 * The kernel's pmap is statically allocated so we don't have to use
558 * pmap_create, which is unlikely to work correctly at this part of
559 * the boot sequence (XXX and which no longer exists).
561 * The kernel_pmap's pm_pteobj is used only for locking and not
564 kernel_pmap.pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
565 kernel_pmap.pm_count = 1;
566 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK; /* don't allow deactivation */
567 kernel_pmap.pm_pteobj = &kernel_object;
568 TAILQ_INIT(&kernel_pmap.pm_pvlist);
569 TAILQ_INIT(&kernel_pmap.pm_pvlist_free);
570 lwkt_token_init(&kernel_pmap.pm_token, "kpmap_tok");
571 spin_init(&kernel_pmap.pm_spin);
574 * Reserve some special page table entries/VA space for temporary
577 #define SYSMAP(c, p, v, n) \
578 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
581 pte = pmap_pte(&kernel_pmap, va);
583 * CMAP1/CMAP2 are used for zeroing and copying pages.
585 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
591 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
595 * ptvmmap is used for reading arbitrary physical pages via
598 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
601 * msgbufp is used to map the system message buffer.
602 * XXX msgbufmap is not used.
604 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
605 atop(round_page(MSGBUF_SIZE)))
610 /* Not ready to do an invltlb yet for VMM*/
617 * Initialize the pmap module.
618 * Called by vm_init, to initialize any structures that the pmap
619 * system needs to map virtual memory.
620 * pmap_init has been enhanced to support in a fairly consistant
621 * way, discontiguous physical memory.
630 * object for kernel page table pages
632 /* JG I think the number can be arbitrary */
633 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
636 * Allocate memory for random pmap data structures. Includes the
640 for(i = 0; i < vm_page_array_size; i++) {
643 m = &vm_page_array[i];
644 TAILQ_INIT(&m->md.pv_list);
645 m->md.pv_list_count = 0;
649 * init the pv free list
651 initial_pvs = vm_page_array_size;
652 if (initial_pvs < MINPV)
654 pvzone = &pvzone_store;
655 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
656 initial_pvs * sizeof (struct pv_entry));
657 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
661 * Now it is safe to enable pv_table recording.
663 pmap_initialized = TRUE;
667 * Initialize the address space (zone) for the pv_entries. Set a
668 * high water mark so that the system can recover from excessive
669 * numbers of pv entries.
674 int shpgperproc = PMAP_SHPGPERPROC;
676 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
677 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
678 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
679 pv_entry_high_water = 9 * (pv_entry_max / 10);
680 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
684 /***************************************************
685 * Low level helper routines.....
686 ***************************************************/
689 * The modification bit is not tracked for any pages in this range. XXX
690 * such pages in this maps should always use pmap_k*() functions and not
693 * XXX User and kernel address spaces are independant for virtual kernels,
694 * this function only applies to the kernel pmap.
697 pmap_track_modified(pmap_t pmap, vm_offset_t va)
699 if (pmap != &kernel_pmap)
701 if ((va < clean_sva) || (va >= clean_eva))
708 * Extract the physical page address associated with the map/VA pair.
713 pmap_extract(pmap_t pmap, vm_offset_t va)
717 pd_entry_t pde, *pdep;
719 lwkt_gettoken(&vm_token);
721 pdep = pmap_pde(pmap, va);
725 if ((pde & VPTE_PS) != 0) {
727 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
729 pte = pmap_pde_to_pte(pdep, va);
730 rtval = (*pte & VPTE_FRAME) | (va & PAGE_MASK);
734 lwkt_reltoken(&vm_token);
739 * Similar to extract but checks protections, SMP-friendly short-cut for
740 * vm_fault_page[_quick]().
743 pmap_fault_page_quick(pmap_t pmap __unused, vm_offset_t vaddr __unused,
744 vm_prot_t prot __unused)
750 * Routine: pmap_kextract
752 * Extract the physical page address associated
753 * kernel virtual address.
756 pmap_kextract(vm_offset_t va)
761 KKASSERT(va >= KvaStart && va < KvaEnd);
764 * The DMAP region is not included in [KvaStart, KvaEnd)
767 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
768 pa = DMAP_TO_PHYS(va);
774 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
777 * Beware of a concurrent promotion that changes the
778 * PDE at this point! For example, vtopte() must not
779 * be used to access the PTE because it would use the
780 * new PDE. It is, however, safe to use the old PDE
781 * because the page table page is preserved by the
784 pa = *pmap_pde_to_pte(&pde, va);
785 pa = (pa & VPTE_FRAME) | (va & PAGE_MASK);
793 /***************************************************
794 * Low level mapping routines.....
795 ***************************************************/
798 * Enter a mapping into kernel_pmap. Mappings created in this fashion
799 * are not managed. Mappings must be immediately accessible on all cpus.
801 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
802 * real pmap and handle related races before storing the new vpte.
805 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
810 KKASSERT(va >= KvaStart && va < KvaEnd);
811 npte = pa | VPTE_RW | VPTE_V | VPTE_U;
814 pmap_inval_pte(pte, &kernel_pmap, va);
819 * Enter an unmanaged KVA mapping for the private use of the current
820 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
823 * It is illegal for the mapping to be accessed by other cpus unleess
824 * pmap_kenter_sync*() is called.
827 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
832 KKASSERT(va >= KvaStart && va < KvaEnd);
834 npte = (vpte_t)pa | VPTE_RW | VPTE_V | VPTE_U;
838 pmap_inval_pte_quick(pte, &kernel_pmap, va);
843 * Synchronize a kvm mapping originally made for the private use on
844 * some other cpu so it can be used on our cpu. Turns out to be the
845 * same madvise() call, because we have to sync the real pmaps anyway.
847 * XXX add MADV_RESYNC to improve performance.
850 pmap_kenter_sync_quick(vm_offset_t va)
852 cpu_invlpg((void *)va);
856 * Remove an unmanaged mapping created with pmap_kenter*().
859 pmap_kremove(vm_offset_t va)
863 KKASSERT(va >= KvaStart && va < KvaEnd);
867 pmap_inval_pte(pte, &kernel_pmap, va);
872 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
873 * only with this cpu.
875 * Unfortunately because we optimize new entries by testing VPTE_V later
876 * on, we actually still have to synchronize with all the cpus. XXX maybe
877 * store a junk value and test against 0 in the other places instead?
880 pmap_kremove_quick(vm_offset_t va)
884 KKASSERT(va >= KvaStart && va < KvaEnd);
888 pmap_inval_pte(pte, &kernel_pmap, va); /* NOT _quick */
893 * Used to map a range of physical addresses into kernel
894 * virtual address space.
896 * For now, VM is already on, we only need to map the
900 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
902 return PHYS_TO_DMAP(start);
907 * Map a set of unmanaged VM pages into KVM.
910 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
914 end_va = va + count * PAGE_SIZE;
915 KKASSERT(va >= KvaStart && end_va < KvaEnd);
917 while (va < end_va) {
922 pmap_inval_pte(pte, &kernel_pmap, va);
923 *pte = VM_PAGE_TO_PHYS(*m) | VPTE_RW | VPTE_V | VPTE_U;
930 * Undo the effects of pmap_qenter*().
933 pmap_qremove(vm_offset_t va, int count)
937 end_va = va + count * PAGE_SIZE;
938 KKASSERT(va >= KvaStart && end_va < KvaEnd);
940 while (va < end_va) {
945 pmap_inval_pte(pte, &kernel_pmap, va);
952 * This routine works like vm_page_lookup() but also blocks as long as the
953 * page is busy. This routine does not busy the page it returns.
955 * Unless the caller is managing objects whos pages are in a known state,
956 * the call should be made with a critical section held so the page's object
957 * association remains valid on return.
960 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
964 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
965 m = vm_page_lookup_busy_wait(object, pindex, FALSE, "pplookp");
971 * Create a new thread and optionally associate it with a (new) process.
972 * NOTE! the new thread's cpu may not equal the current cpu.
975 pmap_init_thread(thread_t td)
977 /* enforce pcb placement */
978 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
979 td->td_savefpu = &td->td_pcb->pcb_save;
980 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */
984 * This routine directly affects the fork perf for a process.
987 pmap_init_proc(struct proc *p)
991 /***************************************************
992 * Page table page management routines.....
993 ***************************************************/
995 static __inline int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va,
999 * This routine unholds page table pages, and if the hold count
1000 * drops to zero, then it decrements the wire count.
1002 * We must recheck that this is the last hold reference after busy-sleeping
1006 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
1008 vm_page_busy_wait(m, FALSE, "pmuwpt");
1009 KASSERT(m->queue == PQ_NONE,
1010 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1012 if (m->hold_count == 1) {
1014 * Unmap the page table page.
1017 /* pmap_inval_add(info, pmap, -1); */
1019 if (m->pindex >= (NUPDE + NUPDPE)) {
1022 pml4 = pmap_pml4e(pmap, va);
1024 } else if (m->pindex >= NUPDE) {
1027 pdp = pmap_pdpe(pmap, va);
1032 pd = pmap_pde(pmap, va);
1036 KKASSERT(pmap->pm_stats.resident_count > 0);
1037 --pmap->pm_stats.resident_count;
1039 if (pmap->pm_ptphint == m)
1040 pmap->pm_ptphint = NULL;
1042 if (m->pindex < NUPDE) {
1043 /* We just released a PT, unhold the matching PD */
1046 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & VPTE_FRAME);
1047 pmap_unwire_pte_hold(pmap, va, pdpg);
1049 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1050 /* We just released a PD, unhold the matching PDP */
1053 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & VPTE_FRAME);
1054 pmap_unwire_pte_hold(pmap, va, pdppg);
1058 * This was our last hold, the page had better be unwired
1059 * after we decrement wire_count.
1061 * FUTURE NOTE: shared page directory page could result in
1062 * multiple wire counts.
1066 KKASSERT(m->wire_count == 0);
1067 atomic_add_int(&vmstats.v_wire_count, -1);
1068 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1070 vm_page_free_zero(m);
1073 KKASSERT(m->hold_count > 1);
1081 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m)
1083 KKASSERT(m->hold_count > 0);
1084 if (m->hold_count > 1) {
1088 return _pmap_unwire_pte_hold(pmap, va, m);
1093 * After removing a page table entry, this routine is used to
1094 * conditionally free the page, and manage the hold/wire counts.
1097 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1099 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1100 vm_pindex_t ptepindex;
1102 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1106 * page table pages in the kernel_pmap are not managed.
1108 if (pmap == &kernel_pmap)
1110 ptepindex = pmap_pde_pindex(va);
1111 if (pmap->pm_ptphint &&
1112 (pmap->pm_ptphint->pindex == ptepindex)) {
1113 mpte = pmap->pm_ptphint;
1115 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1116 pmap->pm_ptphint = mpte;
1117 vm_page_wakeup(mpte);
1121 return pmap_unwire_pte_hold(pmap, va, mpte);
1125 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
1126 * just dummy it up so it works well enough for fork().
1128 * In DragonFly, process pmaps may only be used to manipulate user address
1129 * space, never kernel address space.
1132 pmap_pinit0(struct pmap *pmap)
1138 * Initialize a preallocated and zeroed pmap structure,
1139 * such as one in a vmspace structure.
1142 pmap_pinit(struct pmap *pmap)
1147 * No need to allocate page table space yet but we do need a valid
1148 * page directory table.
1150 if (pmap->pm_pml4 == NULL) {
1152 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1156 * Allocate an object for the ptes
1158 if (pmap->pm_pteobj == NULL)
1159 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1162 * Allocate the page directory page, unless we already have
1163 * one cached. If we used the cached page the wire_count will
1164 * already be set appropriately.
1166 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1167 ptdpg = vm_page_grab(pmap->pm_pteobj,
1168 NUPDE + NUPDPE + PML4PML4I,
1169 VM_ALLOC_NORMAL | VM_ALLOC_RETRY |
1171 pmap->pm_pdirm = ptdpg;
1172 vm_page_flag_clear(ptdpg, PG_MAPPED);
1173 vm_page_wire(ptdpg);
1174 vm_page_wakeup(ptdpg);
1175 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1178 pmap->pm_active = 0;
1179 pmap->pm_ptphint = NULL;
1180 TAILQ_INIT(&pmap->pm_pvlist);
1181 TAILQ_INIT(&pmap->pm_pvlist_free);
1182 spin_init(&pmap->pm_spin);
1183 lwkt_token_init(&pmap->pm_token, "pmap_tok");
1184 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1185 pmap->pm_stats.resident_count = 1;
1189 * Clean up a pmap structure so it can be physically freed. This routine
1190 * is called by the vmspace dtor function. A great deal of pmap data is
1191 * left passively mapped to improve vmspace management so we have a bit
1192 * of cleanup work to do here.
1197 pmap_puninit(pmap_t pmap)
1201 KKASSERT(pmap->pm_active == 0);
1202 if ((p = pmap->pm_pdirm) != NULL) {
1203 KKASSERT(pmap->pm_pml4 != NULL);
1204 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1205 vm_page_busy_wait(p, FALSE, "pgpun");
1207 atomic_add_int(&vmstats.v_wire_count, -1);
1208 vm_page_free_zero(p);
1209 pmap->pm_pdirm = NULL;
1211 if (pmap->pm_pml4) {
1212 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1213 pmap->pm_pml4 = NULL;
1215 if (pmap->pm_pteobj) {
1216 vm_object_deallocate(pmap->pm_pteobj);
1217 pmap->pm_pteobj = NULL;
1222 * Wire in kernel global address entries. To avoid a race condition
1223 * between pmap initialization and pmap_growkernel, this procedure
1224 * adds the pmap to the master list (which growkernel scans to update),
1225 * then copies the template.
1227 * In a virtual kernel there are no kernel global address entries.
1232 pmap_pinit2(struct pmap *pmap)
1234 spin_lock(&pmap_spin);
1235 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1236 spin_unlock(&pmap_spin);
1240 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1241 * 0 on failure (if the procedure had to sleep).
1243 * When asked to remove the page directory page itself, we actually just
1244 * leave it cached so we do not have to incur the SMP inval overhead of
1245 * removing the kernel mapping. pmap_puninit() will take care of it.
1248 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1251 * This code optimizes the case of freeing non-busy
1252 * page-table pages. Those pages are zero now, and
1253 * might as well be placed directly into the zero queue.
1255 if (vm_page_busy_try(p, FALSE)) {
1256 vm_page_sleep_busy(p, FALSE, "pmaprl");
1261 * Remove the page table page from the processes address space.
1263 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1265 * We are the pml4 table itself.
1267 /* XXX anything to do here? */
1268 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1270 * We are a PDP page.
1271 * We look for the PML4 entry that points to us.
1273 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1274 KKASSERT(m4 != NULL);
1275 pml4_entry_t *pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1276 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1277 KKASSERT(pml4[idx] != 0);
1280 /* JG What about wire_count? */
1281 } else if (p->pindex >= NUPDE) {
1284 * We look for the PDP entry that points to us.
1286 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1287 KKASSERT(m3 != NULL);
1288 pdp_entry_t *pdp = (pdp_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1289 int idx = (p->pindex - NUPDE) % NPDPEPG;
1290 KKASSERT(pdp[idx] != 0);
1293 /* JG What about wire_count? */
1295 /* We are a PT page.
1296 * We look for the PD entry that points to us.
1298 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1299 KKASSERT(m2 != NULL);
1300 pd_entry_t *pd = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1301 int idx = p->pindex % NPDEPG;
1304 /* JG What about wire_count? */
1306 KKASSERT(pmap->pm_stats.resident_count > 0);
1307 --pmap->pm_stats.resident_count;
1309 if (p->hold_count) {
1310 panic("pmap_release: freeing held page table page");
1312 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1313 pmap->pm_ptphint = NULL;
1316 * We leave the top-level page table page cached, wired, and mapped in
1317 * the pmap until the dtor function (pmap_puninit()) gets called.
1318 * However, still clean it up so we can set PG_ZERO.
1320 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1321 bzero(pmap->pm_pml4, PAGE_SIZE);
1322 vm_page_flag_set(p, PG_ZERO);
1327 atomic_add_int(&vmstats.v_wire_count, -1);
1328 /* JG eventually revert to using vm_page_free_zero() */
1335 * this routine is called if the page table page is not
1339 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1341 vm_page_t m, pdppg, pdpg;
1344 * Find or fabricate a new pagetable page. Handle allocation
1345 * races by checking m->valid.
1347 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1348 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1350 KASSERT(m->queue == PQ_NONE,
1351 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1354 * Increment the hold count for the page we will be returning to
1361 * Map the pagetable page into the process address space, if
1362 * it isn't already there.
1364 ++pmap->pm_stats.resident_count;
1366 if (ptepindex >= (NUPDE + NUPDPE)) {
1368 vm_pindex_t pml4index;
1370 /* Wire up a new PDP page */
1371 pml4index = ptepindex - (NUPDE + NUPDPE);
1372 pml4 = &pmap->pm_pml4[pml4index];
1373 *pml4 = VM_PAGE_TO_PHYS(m) |
1374 VPTE_RW | VPTE_V | VPTE_U |
1376 } else if (ptepindex >= NUPDE) {
1377 vm_pindex_t pml4index;
1378 vm_pindex_t pdpindex;
1382 /* Wire up a new PD page */
1383 pdpindex = ptepindex - NUPDE;
1384 pml4index = pdpindex >> NPML4EPGSHIFT;
1386 pml4 = &pmap->pm_pml4[pml4index];
1387 if ((*pml4 & VPTE_V) == 0) {
1388 /* Have to allocate a new PDP page, recurse */
1389 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1396 /* Add reference to the PDP page */
1397 pdppg = PHYS_TO_VM_PAGE(*pml4 & VPTE_FRAME);
1398 pdppg->hold_count++;
1400 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1402 /* Now find the pdp page */
1403 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1404 KKASSERT(*pdp == 0); /* JG DEBUG64 */
1405 *pdp = VM_PAGE_TO_PHYS(m) | VPTE_RW | VPTE_V | VPTE_U |
1408 vm_pindex_t pml4index;
1409 vm_pindex_t pdpindex;
1414 /* Wire up a new PT page */
1415 pdpindex = ptepindex >> NPDPEPGSHIFT;
1416 pml4index = pdpindex >> NPML4EPGSHIFT;
1418 /* First, find the pdp and check that its valid. */
1419 pml4 = &pmap->pm_pml4[pml4index];
1420 if ((*pml4 & VPTE_V) == 0) {
1421 /* We miss a PDP page. We ultimately need a PD page.
1422 * Recursively allocating a PD page will allocate
1423 * the missing PDP page and will also allocate
1424 * the PD page we need.
1426 /* Have to allocate a new PD page, recurse */
1427 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1433 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1434 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1436 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & VPTE_FRAME);
1437 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1438 if ((*pdp & VPTE_V) == 0) {
1439 /* Have to allocate a new PD page, recurse */
1440 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1447 /* Add reference to the PD page */
1448 pdpg = PHYS_TO_VM_PAGE(*pdp & VPTE_FRAME);
1452 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & VPTE_FRAME);
1454 /* Now we know where the page directory page is */
1455 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1456 KKASSERT(*pd == 0); /* JG DEBUG64 */
1457 *pd = VM_PAGE_TO_PHYS(m) | VPTE_RW | VPTE_V | VPTE_U |
1462 * Set the page table hint
1464 pmap->pm_ptphint = m;
1465 vm_page_flag_set(m, PG_MAPPED);
1472 * Determine the page table page required to access the VA in the pmap
1473 * and allocate it if necessary. Return a held vm_page_t for the page.
1475 * Only used with user pmaps.
1478 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1480 vm_pindex_t ptepindex;
1484 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1487 * Calculate pagetable page index
1489 ptepindex = pmap_pde_pindex(va);
1492 * Get the page directory entry
1494 pd = pmap_pde(pmap, va);
1497 * This supports switching from a 2MB page to a
1500 if (pd != NULL && (*pd & (VPTE_PS | VPTE_V)) == (VPTE_PS | VPTE_V)) {
1501 panic("no promotion/demotion yet");
1509 * If the page table page is mapped, we just increment the
1510 * hold count, and activate it.
1512 if (pd != NULL && (*pd & VPTE_V) != 0) {
1513 /* YYY hint is used here on i386 */
1514 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1515 pmap->pm_ptphint = m;
1521 * Here if the pte page isn't mapped, or if it has been deallocated.
1523 return _pmap_allocpte(pmap, ptepindex);
1527 /***************************************************
1528 * Pmap allocation/deallocation routines.
1529 ***************************************************/
1532 * Release any resources held by the given physical map.
1533 * Called when a pmap initialized by pmap_pinit is being released.
1534 * Should only be called if the map contains no valid mappings.
1536 * Caller must hold pmap->pm_token
1538 static int pmap_release_callback(struct vm_page *p, void *data);
1541 pmap_release(struct pmap *pmap)
1543 vm_object_t object = pmap->pm_pteobj;
1544 struct rb_vm_page_scan_info info;
1546 KKASSERT(pmap != &kernel_pmap);
1548 #if defined(DIAGNOSTIC)
1549 if (object->ref_count != 1)
1550 panic("pmap_release: pteobj reference count != 1");
1554 info.object = object;
1556 spin_lock(&pmap_spin);
1557 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1558 spin_unlock(&pmap_spin);
1560 vm_object_hold(object);
1564 info.limit = object->generation;
1566 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1567 pmap_release_callback, &info);
1568 if (info.error == 0 && info.mpte) {
1569 if (!pmap_release_free_page(pmap, info.mpte))
1572 } while (info.error);
1573 vm_object_drop(object);
1577 pmap_release_callback(struct vm_page *p, void *data)
1579 struct rb_vm_page_scan_info *info = data;
1581 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1585 if (!pmap_release_free_page(info->pmap, p)) {
1589 if (info->object->generation != info->limit) {
1597 * Grow the number of kernel page table entries, if needed.
1602 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
1606 vm_offset_t ptppaddr;
1608 pd_entry_t *pde, newpdir;
1613 vm_object_hold(kptobj);
1614 if (kernel_vm_end == 0) {
1615 kernel_vm_end = KvaStart;
1617 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & VPTE_V) != 0) {
1618 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1620 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1621 kernel_vm_end = kernel_map.max_offset;
1626 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1627 if (addr - 1 >= kernel_map.max_offset)
1628 addr = kernel_map.max_offset;
1629 while (kernel_vm_end < addr) {
1630 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1632 /* We need a new PDP entry */
1633 nkpg = vm_page_alloc(kptobj, nkpt,
1634 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1635 | VM_ALLOC_INTERRUPT);
1637 panic("pmap_growkernel: no memory to "
1640 paddr = VM_PAGE_TO_PHYS(nkpg);
1641 if ((nkpg->flags & PG_ZERO) == 0)
1642 pmap_zero_page(paddr);
1643 vm_page_flag_clear(nkpg, PG_ZERO);
1644 newpdp = (pdp_entry_t)(paddr |
1645 VPTE_V | VPTE_RW | VPTE_U |
1647 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1649 continue; /* try again */
1651 if ((*pde & VPTE_V) != 0) {
1652 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1653 ~(PAGE_SIZE * NPTEPG - 1);
1654 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1655 kernel_vm_end = kernel_map.max_offset;
1662 * This index is bogus, but out of the way
1664 nkpg = vm_page_alloc(kptobj, nkpt,
1667 VM_ALLOC_INTERRUPT);
1669 panic("pmap_growkernel: no memory to grow kernel");
1672 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1673 pmap_zero_page(ptppaddr);
1674 vm_page_flag_clear(nkpg, PG_ZERO);
1675 newpdir = (pd_entry_t)(ptppaddr |
1676 VPTE_V | VPTE_RW | VPTE_U |
1678 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1681 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1682 ~(PAGE_SIZE * NPTEPG - 1);
1683 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1684 kernel_vm_end = kernel_map.max_offset;
1688 vm_object_drop(kptobj);
1692 * Add a reference to the specified pmap.
1697 pmap_reference(pmap_t pmap)
1700 lwkt_gettoken(&vm_token);
1702 lwkt_reltoken(&vm_token);
1706 /************************************************************************
1707 * VMSPACE MANAGEMENT *
1708 ************************************************************************
1710 * The VMSPACE management we do in our virtual kernel must be reflected
1711 * in the real kernel. This is accomplished by making vmspace system
1712 * calls to the real kernel.
1715 cpu_vmspace_alloc(struct vmspace *vm)
1722 * If VMM enable, don't do nothing, we
1723 * are able to use real page tables
1728 #define USER_SIZE (VM_MAX_USER_ADDRESS - VM_MIN_USER_ADDRESS)
1730 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
1731 panic("vmspace_create() failed");
1733 rp = vmspace_mmap(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1734 PROT_READ|PROT_WRITE,
1735 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
1737 if (rp == MAP_FAILED)
1738 panic("vmspace_mmap: failed");
1739 vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1741 vpte = VM_PAGE_TO_PHYS(vmspace_pmap(vm)->pm_pdirm) | VPTE_RW | VPTE_V | VPTE_U;
1742 r = vmspace_mcontrol(&vm->vm_pmap, VM_MIN_USER_ADDRESS, USER_SIZE,
1745 panic("vmspace_mcontrol: failed");
1749 cpu_vmspace_free(struct vmspace *vm)
1752 * If VMM enable, don't do nothing, we
1753 * are able to use real page tables
1758 if (vmspace_destroy(&vm->vm_pmap) < 0)
1759 panic("vmspace_destroy() failed");
1762 /***************************************************
1763 * page management routines.
1764 ***************************************************/
1767 * free the pv_entry back to the free list. This function may be
1768 * called from an interrupt.
1770 static __inline void
1771 free_pv_entry(pv_entry_t pv)
1774 KKASSERT(pv_entry_count >= 0);
1779 * get a new pv_entry, allocating a block from the system
1780 * when needed. This function may be called from an interrupt.
1786 if (pv_entry_high_water &&
1787 (pv_entry_count > pv_entry_high_water) &&
1788 (pmap_pagedaemon_waken == 0)) {
1789 pmap_pagedaemon_waken = 1;
1790 wakeup(&vm_pages_needed);
1792 return zalloc(pvzone);
1796 * This routine is very drastic, but can save the system
1806 static int warningdone=0;
1808 if (pmap_pagedaemon_waken == 0)
1810 lwkt_gettoken(&vm_token);
1811 pmap_pagedaemon_waken = 0;
1813 if (warningdone < 5) {
1814 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1818 for (i = 0; i < vm_page_array_size; i++) {
1819 m = &vm_page_array[i];
1820 if (m->wire_count || m->hold_count)
1822 if (vm_page_busy_try(m, TRUE) == 0) {
1823 if (m->wire_count == 0 && m->hold_count == 0) {
1829 lwkt_reltoken(&vm_token);
1834 * If it is the first entry on the list, it is actually
1835 * in the header and we must copy the following entry up
1836 * to the header. Otherwise we must search the list for
1837 * the entry. In either case we free the now unused entry.
1839 * caller must hold vm_token.
1842 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1847 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1848 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1849 if (pmap == pv->pv_pmap && va == pv->pv_va)
1853 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1854 if (va == pv->pv_va)
1860 * Note that pv_ptem is NULL if the page table page itself is not
1861 * managed, even if the page being removed IS managed.
1864 /* JGXXX When can 'pv' be NULL? */
1866 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1867 m->md.pv_list_count--;
1868 atomic_add_int(&m->object->agg_pv_list_count, -1);
1869 KKASSERT(m->md.pv_list_count >= 0);
1870 if (TAILQ_EMPTY(&m->md.pv_list))
1871 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1872 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1873 ++pmap->pm_generation;
1874 KKASSERT(pmap->pm_pteobj != NULL);
1875 vm_object_hold(pmap->pm_pteobj);
1876 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1877 vm_object_drop(pmap->pm_pteobj);
1884 * Create a pv entry for page at pa for (pmap, va). If the page table page
1885 * holding the VA is managed, mpte will be non-NULL.
1888 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1893 pv = get_pv_entry();
1898 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1899 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1900 m->md.pv_list_count++;
1901 atomic_add_int(&m->object->agg_pv_list_count, 1);
1907 * pmap_remove_pte: do the things to unmap a page in a process
1910 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va)
1915 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1916 if (oldpte & VPTE_WIRED)
1917 --pmap->pm_stats.wired_count;
1918 KKASSERT(pmap->pm_stats.wired_count >= 0);
1922 * Machines that don't support invlpg, also don't support
1923 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1927 cpu_invlpg((void *)va);
1929 KKASSERT(pmap->pm_stats.resident_count > 0);
1930 --pmap->pm_stats.resident_count;
1931 if (oldpte & VPTE_MANAGED) {
1932 m = PHYS_TO_VM_PAGE(oldpte);
1933 if (oldpte & VPTE_M) {
1934 #if defined(PMAP_DIAGNOSTIC)
1935 if (pmap_nw_modified(oldpte)) {
1936 kprintf("pmap_remove: modified page not "
1937 "writable: va: 0x%lx, pte: 0x%lx\n",
1941 if (pmap_track_modified(pmap, va))
1944 if (oldpte & VPTE_A)
1945 vm_page_flag_set(m, PG_REFERENCED);
1946 return pmap_remove_entry(pmap, m, va);
1948 return pmap_unuse_pt(pmap, va, NULL);
1957 * Remove a single page from a process address space.
1959 * This function may not be called from an interrupt if the pmap is
1963 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1967 pte = pmap_pte(pmap, va);
1970 if ((*pte & VPTE_V) == 0)
1972 pmap_remove_pte(pmap, pte, va);
1976 * Remove the given range of addresses from the specified map.
1978 * It is assumed that the start and end are properly rounded to
1981 * This function may not be called from an interrupt if the pmap is
1987 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1989 vm_offset_t va_next;
1990 pml4_entry_t *pml4e;
1992 pd_entry_t ptpaddr, *pde;
1998 vm_object_hold(pmap->pm_pteobj);
1999 lwkt_gettoken(&vm_token);
2000 KKASSERT(pmap->pm_stats.resident_count >= 0);
2001 if (pmap->pm_stats.resident_count == 0) {
2002 lwkt_reltoken(&vm_token);
2003 vm_object_drop(pmap->pm_pteobj);
2008 * special handling of removing one page. a very
2009 * common operation and easy to short circuit some
2012 if (sva + PAGE_SIZE == eva) {
2013 pde = pmap_pde(pmap, sva);
2014 if (pde && (*pde & VPTE_PS) == 0) {
2015 pmap_remove_page(pmap, sva);
2016 lwkt_reltoken(&vm_token);
2017 vm_object_drop(pmap->pm_pteobj);
2022 for (; sva < eva; sva = va_next) {
2023 pml4e = pmap_pml4e(pmap, sva);
2024 if ((*pml4e & VPTE_V) == 0) {
2025 va_next = (sva + NBPML4) & ~PML4MASK;
2031 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2032 if ((*pdpe & VPTE_V) == 0) {
2033 va_next = (sva + NBPDP) & ~PDPMASK;
2040 * Calculate index for next page table.
2042 va_next = (sva + NBPDR) & ~PDRMASK;
2046 pde = pmap_pdpe_to_pde(pdpe, sva);
2050 * Weed out invalid mappings.
2056 * Check for large page.
2058 if ((ptpaddr & VPTE_PS) != 0) {
2059 /* JG FreeBSD has more complex treatment here */
2060 KKASSERT(*pde != 0);
2061 pmap_inval_pde(pde, pmap, sva);
2062 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2067 * Limit our scan to either the end of the va represented
2068 * by the current page table page, or to the end of the
2069 * range being removed.
2075 * NOTE: pmap_remove_pte() can block.
2077 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2081 if (pmap_remove_pte(pmap, pte, sva))
2085 lwkt_reltoken(&vm_token);
2086 vm_object_drop(pmap->pm_pteobj);
2090 * Removes this physical page from all physical maps in which it resides.
2091 * Reflects back modify bits to the pager.
2093 * This routine may not be called from an interrupt.
2098 pmap_remove_all(vm_page_t m)
2100 pt_entry_t *pte, tpte;
2103 #if defined(PMAP_DIAGNOSTIC)
2105 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
2108 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
2109 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
2113 lwkt_gettoken(&vm_token);
2114 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2115 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2116 --pv->pv_pmap->pm_stats.resident_count;
2118 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2119 KKASSERT(pte != NULL);
2121 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
2122 if (tpte & VPTE_WIRED)
2123 pv->pv_pmap->pm_stats.wired_count--;
2124 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
2127 vm_page_flag_set(m, PG_REFERENCED);
2130 * Update the vm_page_t clean and reference bits.
2132 if (tpte & VPTE_M) {
2133 #if defined(PMAP_DIAGNOSTIC)
2134 if (pmap_nw_modified(tpte)) {
2136 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2140 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
2143 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2144 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2145 ++pv->pv_pmap->pm_generation;
2146 m->md.pv_list_count--;
2147 atomic_add_int(&m->object->agg_pv_list_count, -1);
2148 KKASSERT(m->md.pv_list_count >= 0);
2149 if (TAILQ_EMPTY(&m->md.pv_list))
2150 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2151 vm_object_hold(pv->pv_pmap->pm_pteobj);
2152 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2153 vm_object_drop(pv->pv_pmap->pm_pteobj);
2156 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2157 lwkt_reltoken(&vm_token);
2161 * Set the physical protection on the specified range of this map
2164 * This function may not be called from an interrupt if the map is
2165 * not the kernel_pmap.
2170 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2172 vm_offset_t va_next;
2173 pml4_entry_t *pml4e;
2175 pd_entry_t ptpaddr, *pde;
2178 /* JG review for NX */
2183 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2184 pmap_remove(pmap, sva, eva);
2188 if (prot & VM_PROT_WRITE)
2191 lwkt_gettoken(&vm_token);
2193 for (; sva < eva; sva = va_next) {
2195 pml4e = pmap_pml4e(pmap, sva);
2196 if ((*pml4e & VPTE_V) == 0) {
2197 va_next = (sva + NBPML4) & ~PML4MASK;
2203 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2204 if ((*pdpe & VPTE_V) == 0) {
2205 va_next = (sva + NBPDP) & ~PDPMASK;
2211 va_next = (sva + NBPDR) & ~PDRMASK;
2215 pde = pmap_pdpe_to_pde(pdpe, sva);
2219 * Check for large page.
2221 if ((ptpaddr & VPTE_PS) != 0) {
2223 pmap_clean_pde(pde, pmap, sva);
2224 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2229 * Weed out invalid mappings. Note: we assume that the page
2230 * directory table is always allocated, and in kernel virtual.
2238 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2244 * Clean managed pages and also check the accessed
2245 * bit. Just remove write perms for unmanaged
2246 * pages. Be careful of races, turning off write
2247 * access will force a fault rather then setting
2248 * the modified bit at an unexpected time.
2250 if (*pte & VPTE_MANAGED) {
2251 pbits = pmap_clean_pte(pte, pmap, sva);
2253 if (pbits & VPTE_A) {
2254 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2255 vm_page_flag_set(m, PG_REFERENCED);
2256 atomic_clear_long(pte, VPTE_A);
2258 if (pbits & VPTE_M) {
2259 if (pmap_track_modified(pmap, sva)) {
2261 m = PHYS_TO_VM_PAGE(pbits & VPTE_FRAME);
2266 pbits = pmap_setro_pte(pte, pmap, sva);
2270 lwkt_reltoken(&vm_token);
2274 * Enter a managed page into a pmap. If the page is not wired related pmap
2275 * data can be destroyed at any time for later demand-operation.
2277 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
2278 * specified protection, and wire the mapping if requested.
2280 * NOTE: This routine may not lazy-evaluate or lose information. The
2281 * page must actually be inserted into the given map NOW.
2283 * NOTE: When entering a page at a KVA address, the pmap must be the
2289 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2290 boolean_t wired, vm_map_entry_t entry __unused)
2296 pt_entry_t origpte, newpte;
2302 va = trunc_page(va);
2304 vm_object_hold(pmap->pm_pteobj);
2305 lwkt_gettoken(&vm_token);
2308 * Get the page table page. The kernel_pmap's page table pages
2309 * are preallocated and have no associated vm_page_t.
2311 if (pmap == &kernel_pmap)
2314 mpte = pmap_allocpte(pmap, va);
2316 pde = pmap_pde(pmap, va);
2317 if (pde != NULL && (*pde & VPTE_V) != 0) {
2318 if ((*pde & VPTE_PS) != 0)
2319 panic("pmap_enter: attempted pmap_enter on 2MB page");
2320 pte = pmap_pde_to_pte(pde, va);
2322 panic("pmap_enter: invalid page directory va=%#lx", va);
2325 KKASSERT(pte != NULL);
2327 * Deal with races on the original mapping (though don't worry
2328 * about VPTE_A races) by cleaning it. This will force a fault
2329 * if an attempt is made to write to the page.
2331 pa = VM_PAGE_TO_PHYS(m);
2332 origpte = pmap_clean_pte(pte, pmap, va);
2333 opa = origpte & VPTE_FRAME;
2335 if (origpte & VPTE_PS)
2336 panic("pmap_enter: attempted pmap_enter on 2MB page");
2339 * Mapping has not changed, must be protection or wiring change.
2341 if (origpte && (opa == pa)) {
2343 * Wiring change, just update stats. We don't worry about
2344 * wiring PT pages as they remain resident as long as there
2345 * are valid mappings in them. Hence, if a user page is wired,
2346 * the PT page will be also.
2348 if (wired && ((origpte & VPTE_WIRED) == 0))
2349 ++pmap->pm_stats.wired_count;
2350 else if (!wired && (origpte & VPTE_WIRED))
2351 --pmap->pm_stats.wired_count;
2354 * Remove the extra pte reference. Note that we cannot
2355 * optimize the RO->RW case because we have adjusted the
2356 * wiring count above and may need to adjust the wiring
2363 * We might be turning off write access to the page,
2364 * so we go ahead and sense modify status.
2366 if (origpte & VPTE_MANAGED) {
2367 if ((origpte & VPTE_M) &&
2368 pmap_track_modified(pmap, va)) {
2370 om = PHYS_TO_VM_PAGE(opa);
2374 KKASSERT(m->flags & PG_MAPPED);
2379 * Mapping has changed, invalidate old range and fall through to
2380 * handle validating new mapping.
2384 err = pmap_remove_pte(pmap, pte, va);
2386 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2390 * Enter on the PV list if part of our managed memory. Note that we
2391 * raise IPL while manipulating pv_table since pmap_enter can be
2392 * called at interrupt time.
2394 if (pmap_initialized &&
2395 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2396 pmap_insert_entry(pmap, va, mpte, m);
2398 vm_page_flag_set(m, PG_MAPPED);
2402 * Increment counters
2404 ++pmap->pm_stats.resident_count;
2406 pmap->pm_stats.wired_count++;
2410 * Now validate mapping with desired protection/wiring.
2412 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | VPTE_V | VPTE_U);
2415 newpte |= VPTE_WIRED;
2416 // if (pmap != &kernel_pmap)
2420 * If the mapping or permission bits are different from the
2421 * (now cleaned) original pte, an update is needed. We've
2422 * already downgraded or invalidated the page so all we have
2423 * to do now is update the bits.
2425 * XXX should we synchronize RO->RW changes to avoid another
2428 if ((origpte & ~(VPTE_RW|VPTE_M|VPTE_A)) != newpte) {
2429 *pte = newpte | VPTE_A;
2430 if (newpte & VPTE_RW)
2431 vm_page_flag_set(m, PG_WRITEABLE);
2433 KKASSERT((newpte & VPTE_MANAGED) == 0 || (m->flags & PG_MAPPED));
2434 lwkt_reltoken(&vm_token);
2435 vm_object_drop(pmap->pm_pteobj);
2439 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2441 * Currently this routine may only be used on user pmaps, not kernel_pmap.
2446 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2451 vm_pindex_t ptepindex;
2454 KKASSERT(pmap != &kernel_pmap);
2456 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
2459 * Calculate pagetable page index
2461 ptepindex = pmap_pde_pindex(va);
2463 vm_object_hold(pmap->pm_pteobj);
2464 lwkt_gettoken(&vm_token);
2468 * Get the page directory entry
2470 ptepa = pmap_pde(pmap, va);
2473 * If the page table page is mapped, we just increment
2474 * the hold count, and activate it.
2476 if (ptepa && (*ptepa & VPTE_V) != 0) {
2477 if (*ptepa & VPTE_PS)
2478 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2479 if (pmap->pm_ptphint &&
2480 (pmap->pm_ptphint->pindex == ptepindex)) {
2481 mpte = pmap->pm_ptphint;
2483 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2484 pmap->pm_ptphint = mpte;
2485 vm_page_wakeup(mpte);
2490 mpte = _pmap_allocpte(pmap, ptepindex);
2492 } while (mpte == NULL);
2495 * Ok, now that the page table page has been validated, get the pte.
2496 * If the pte is already mapped undo mpte's hold_count and
2499 pte = pmap_pte(pmap, va);
2500 if (*pte & VPTE_V) {
2501 KKASSERT(mpte != NULL);
2502 pmap_unwire_pte_hold(pmap, va, mpte);
2503 pa = VM_PAGE_TO_PHYS(m);
2504 KKASSERT(((*pte ^ pa) & VPTE_FRAME) == 0);
2505 lwkt_reltoken(&vm_token);
2506 vm_object_drop(pmap->pm_pteobj);
2511 * Enter on the PV list if part of our managed memory
2513 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2514 pmap_insert_entry(pmap, va, mpte, m);
2515 vm_page_flag_set(m, PG_MAPPED);
2519 * Increment counters
2521 ++pmap->pm_stats.resident_count;
2523 pa = VM_PAGE_TO_PHYS(m);
2526 * Now validate mapping with RO protection
2528 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2529 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2531 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2532 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2533 /*pmap_inval_flush(&info); don't need for vkernel */
2534 lwkt_reltoken(&vm_token);
2535 vm_object_drop(pmap->pm_pteobj);
2539 * Make a temporary mapping for a physical address. This is only intended
2540 * to be used for panic dumps.
2542 * The caller is responsible for calling smp_invltlb().
2545 pmap_kenter_temporary(vm_paddr_t pa, long i)
2547 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
2548 return ((void *)crashdumpmap);
2551 #define MAX_INIT_PT (96)
2554 * This routine preloads the ptes for a given object into the specified pmap.
2555 * This eliminates the blast of soft faults on process startup and
2556 * immediately after an mmap.
2560 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2563 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2564 vm_object_t object, vm_pindex_t pindex,
2565 vm_size_t size, int limit)
2567 struct rb_vm_page_scan_info info;
2572 * We can't preinit if read access isn't set or there is no pmap
2575 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2579 * We can't preinit if the pmap is not the current pmap
2581 lp = curthread->td_lwp;
2582 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2585 psize = x86_64_btop(size);
2587 if ((object->type != OBJT_VNODE) ||
2588 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2589 (object->resident_page_count > MAX_INIT_PT))) {
2593 if (psize + pindex > object->size) {
2594 if (object->size < pindex)
2596 psize = object->size - pindex;
2603 * Use a red-black scan to traverse the requested range and load
2604 * any valid pages found into the pmap.
2606 * We cannot safely scan the object's memq unless we are in a
2607 * critical section since interrupts can remove pages from objects.
2609 info.start_pindex = pindex;
2610 info.end_pindex = pindex + psize - 1;
2616 vm_object_hold_shared(object);
2617 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2618 pmap_object_init_pt_callback, &info);
2619 vm_object_drop(object);
2624 pmap_object_init_pt_callback(vm_page_t p, void *data)
2626 struct rb_vm_page_scan_info *info = data;
2627 vm_pindex_t rel_index;
2629 * don't allow an madvise to blow away our really
2630 * free pages allocating pv entries.
2632 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2633 vmstats.v_free_count < vmstats.v_free_reserved) {
2638 * Ignore list markers and ignore pages we cannot instantly
2639 * busy (while holding the object token).
2641 if (p->flags & PG_MARKER)
2643 if (vm_page_busy_try(p, TRUE))
2645 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2646 (p->flags & PG_FICTITIOUS) == 0) {
2647 if ((p->queue - p->pc) == PQ_CACHE)
2648 vm_page_deactivate(p);
2649 rel_index = p->pindex - info->start_pindex;
2650 pmap_enter_quick(info->pmap,
2651 info->addr + x86_64_ptob(rel_index), p);
2658 * Return TRUE if the pmap is in shape to trivially
2659 * pre-fault the specified address.
2661 * Returns FALSE if it would be non-trivial or if a
2662 * pte is already loaded into the slot.
2667 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2673 lwkt_gettoken(&vm_token);
2674 pde = pmap_pde(pmap, addr);
2675 if (pde == NULL || *pde == 0) {
2678 pte = pmap_pde_to_pte(pde, addr);
2679 ret = (*pte) ? 0 : 1;
2681 lwkt_reltoken(&vm_token);
2686 * Change the wiring attribute for a map/virtual-address pair.
2688 * The mapping must already exist in the pmap.
2689 * No other requirements.
2692 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired,
2693 vm_map_entry_t entry __unused)
2700 lwkt_gettoken(&vm_token);
2701 pte = pmap_pte(pmap, va);
2703 if (wired && !pmap_pte_w(pte))
2704 pmap->pm_stats.wired_count++;
2705 else if (!wired && pmap_pte_w(pte))
2706 pmap->pm_stats.wired_count--;
2709 * Wiring is not a hardware characteristic so there is no need to
2710 * invalidate TLB. However, in an SMP environment we must use
2711 * a locked bus cycle to update the pte (if we are not using
2712 * the pmap_inval_*() API that is)... it's ok to do this for simple
2716 atomic_set_long(pte, VPTE_WIRED);
2718 atomic_clear_long(pte, VPTE_WIRED);
2719 lwkt_reltoken(&vm_token);
2723 * Copy the range specified by src_addr/len
2724 * from the source map to the range dst_addr/len
2725 * in the destination map.
2727 * This routine is only advisory and need not do anything.
2730 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2731 vm_size_t len, vm_offset_t src_addr)
2734 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2735 * valid through blocking calls, and that's just not going to
2746 * Zero the specified physical page.
2748 * This function may be called from an interrupt and no locking is
2752 pmap_zero_page(vm_paddr_t phys)
2754 vm_offset_t va = PHYS_TO_DMAP(phys);
2756 bzero((void *)va, PAGE_SIZE);
2760 * pmap_page_assertzero:
2762 * Assert that a page is empty, panic if it isn't.
2765 pmap_page_assertzero(vm_paddr_t phys)
2770 vm_offset_t virt = PHYS_TO_DMAP(phys);
2772 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
2773 if (*(int *)((char *)virt + i) != 0) {
2774 panic("pmap_page_assertzero() @ %p not zero!",
2784 * Zero part of a physical page by mapping it into memory and clearing
2785 * its contents with bzero.
2787 * off and size may not cover an area beyond a single hardware page.
2790 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2793 vm_offset_t virt = PHYS_TO_DMAP(phys);
2794 bzero((char *)virt + off, size);
2801 * Copy the physical page from the source PA to the target PA.
2802 * This function may be called from an interrupt. No locking
2806 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2808 vm_offset_t src_virt, dst_virt;
2811 src_virt = PHYS_TO_DMAP(src);
2812 dst_virt = PHYS_TO_DMAP(dst);
2813 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
2818 * pmap_copy_page_frag:
2820 * Copy the physical page from the source PA to the target PA.
2821 * This function may be called from an interrupt. No locking
2825 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2827 vm_offset_t src_virt, dst_virt;
2830 src_virt = PHYS_TO_DMAP(src);
2831 dst_virt = PHYS_TO_DMAP(dst);
2832 bcopy((char *)src_virt + (src & PAGE_MASK),
2833 (char *)dst_virt + (dst & PAGE_MASK),
2839 * Returns true if the pmap's pv is one of the first 16 pvs linked to
2840 * from this page. This count may be changed upwards or downwards
2841 * in the future; it is only necessary that true be returned for a small
2842 * subset of pmaps for proper page aging.
2844 * No other requirements.
2847 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2852 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2856 lwkt_gettoken(&vm_token);
2858 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2859 if (pv->pv_pmap == pmap) {
2860 lwkt_reltoken(&vm_token);
2868 lwkt_reltoken(&vm_token);
2874 * Remove all pages from specified address space this aids process
2875 * exit speeds. Also, this code is special cased for current
2876 * process only, but can have the more generic (and slightly slower)
2877 * mode enabled. This is much faster than pmap_remove in the case
2878 * of running down an entire address space.
2880 * No other requirements.
2883 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2885 pt_entry_t *pte, tpte;
2888 int save_generation;
2890 if (pmap->pm_pteobj)
2891 vm_object_hold(pmap->pm_pteobj);
2892 lwkt_gettoken(&vm_token);
2894 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2895 if (pv->pv_va >= eva || pv->pv_va < sva) {
2896 npv = TAILQ_NEXT(pv, pv_plist);
2900 KKASSERT(pmap == pv->pv_pmap);
2902 pte = pmap_pte(pmap, pv->pv_va);
2905 * We cannot remove wired pages from a process' mapping
2908 if (*pte & VPTE_WIRED) {
2909 npv = TAILQ_NEXT(pv, pv_plist);
2912 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2914 m = PHYS_TO_VM_PAGE(tpte & VPTE_FRAME);
2916 KASSERT(m < &vm_page_array[vm_page_array_size],
2917 ("pmap_remove_pages: bad tpte %lx", tpte));
2919 KKASSERT(pmap->pm_stats.resident_count > 0);
2920 --pmap->pm_stats.resident_count;
2923 * Update the vm_page_t clean and reference bits.
2925 if (tpte & VPTE_M) {
2929 npv = TAILQ_NEXT(pv, pv_plist);
2930 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2931 save_generation = ++pmap->pm_generation;
2933 m->md.pv_list_count--;
2934 atomic_add_int(&m->object->agg_pv_list_count, -1);
2935 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2936 if (TAILQ_EMPTY(&m->md.pv_list))
2937 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2939 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2943 * Restart the scan if we blocked during the unuse or free
2944 * calls and other removals were made.
2946 if (save_generation != pmap->pm_generation) {
2947 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2948 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2951 lwkt_reltoken(&vm_token);
2952 if (pmap->pm_pteobj)
2953 vm_object_drop(pmap->pm_pteobj);
2957 * pmap_testbit tests bits in active mappings of a VM page.
2960 pmap_testbit(vm_page_t m, int bit)
2965 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2968 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2973 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2975 * if the bit being tested is the modified bit, then
2976 * mark clean_map and ptes as never
2979 if (bit & (VPTE_A|VPTE_M)) {
2980 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2984 #if defined(PMAP_DIAGNOSTIC)
2985 if (pv->pv_pmap == NULL) {
2986 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
2990 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3001 * This routine is used to clear bits in ptes. Certain bits require special
3002 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
3004 * This routine is only called with certain VPTE_* bit combinations.
3006 static __inline void
3007 pmap_clearbit(vm_page_t m, int bit)
3013 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3019 * Loop over all current mappings setting/clearing as appropos If
3020 * setting RO do we need to clear the VAC?
3022 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3024 * don't write protect pager mappings
3026 if (bit == VPTE_RW) {
3027 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3031 #if defined(PMAP_DIAGNOSTIC)
3032 if (pv->pv_pmap == NULL) {
3033 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3039 * Careful here. We can use a locked bus instruction to
3040 * clear VPTE_A or VPTE_M safely but we need to synchronize
3041 * with the target cpus when we mess with VPTE_RW.
3043 * On virtual kernels we must force a new fault-on-write
3044 * in the real kernel if we clear the Modify bit ourselves,
3045 * otherwise the real kernel will not get a new fault and
3046 * will never set our Modify bit again.
3048 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3050 if (bit == VPTE_RW) {
3052 * We must also clear VPTE_M when clearing
3055 pbits = pmap_clean_pte(pte, pv->pv_pmap,
3059 } else if (bit == VPTE_M) {
3061 * We do not have to make the page read-only
3062 * when clearing the Modify bit. The real
3063 * kernel will make the real PTE read-only
3064 * or otherwise detect the write and set
3065 * our VPTE_M again simply by us invalidating
3066 * the real kernel VA for the pmap (as we did
3067 * above). This allows the real kernel to
3068 * handle the write fault without forwarding
3071 atomic_clear_long(pte, VPTE_M);
3072 } else if ((bit & (VPTE_RW|VPTE_M)) == (VPTE_RW|VPTE_M)) {
3074 * We've been asked to clear W & M, I guess
3075 * the caller doesn't want us to update
3076 * the dirty status of the VM page.
3078 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
3081 * We've been asked to clear bits that do
3082 * not interact with hardware.
3084 atomic_clear_long(pte, bit);
3092 * Lower the permission for all mappings to a given page.
3094 * No other requirements.
3097 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3099 /* JG NX support? */
3100 if ((prot & VM_PROT_WRITE) == 0) {
3101 lwkt_gettoken(&vm_token);
3102 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3103 pmap_clearbit(m, VPTE_RW);
3104 vm_page_flag_clear(m, PG_WRITEABLE);
3108 lwkt_reltoken(&vm_token);
3113 pmap_phys_address(vm_pindex_t ppn)
3115 return (x86_64_ptob(ppn));
3119 * Return a count of reference bits for a page, clearing those bits.
3120 * It is not necessary for every reference bit to be cleared, but it
3121 * is necessary that 0 only be returned when there are truly no
3122 * reference bits set.
3124 * XXX: The exact number of bits to check and clear is a matter that
3125 * should be tested and standardized at some point in the future for
3126 * optimal aging of shared pages.
3128 * No other requirements.
3131 pmap_ts_referenced(vm_page_t m)
3133 pv_entry_t pv, pvf, pvn;
3137 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3141 lwkt_gettoken(&vm_token);
3143 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3148 pvn = TAILQ_NEXT(pv, pv_list);
3150 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3152 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3154 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
3157 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
3159 if (pte && (*pte & VPTE_A)) {
3160 atomic_clear_long(pte, VPTE_A);
3166 } while ((pv = pvn) != NULL && pv != pvf);
3168 lwkt_reltoken(&vm_token);
3175 * Return whether or not the specified physical page was modified
3176 * in any physical maps.
3178 * No other requirements.
3181 pmap_is_modified(vm_page_t m)
3185 lwkt_gettoken(&vm_token);
3186 res = pmap_testbit(m, VPTE_M);
3187 lwkt_reltoken(&vm_token);
3192 * Clear the modify bits on the specified physical page.
3194 * No other requirements.
3197 pmap_clear_modify(vm_page_t m)
3199 lwkt_gettoken(&vm_token);
3200 pmap_clearbit(m, VPTE_M);
3201 lwkt_reltoken(&vm_token);
3205 * Clear the reference bit on the specified physical page.
3207 * No other requirements.
3210 pmap_clear_reference(vm_page_t m)
3212 lwkt_gettoken(&vm_token);
3213 pmap_clearbit(m, VPTE_A);
3214 lwkt_reltoken(&vm_token);
3218 * Miscellaneous support routines follow
3222 i386_protection_init(void)
3226 kp = protection_codes;
3227 for (prot = 0; prot < 8; prot++) {
3228 if (prot & VM_PROT_READ)
3229 *kp |= 0; /* if it's VALID is readeable */
3230 if (prot & VM_PROT_WRITE)
3232 if (prot & VM_PROT_EXECUTE)
3233 *kp |= 0; /* if it's VALID is executable */
3239 * Sets the memory attribute for the specified page.
3242 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
3244 /* This is a vkernel, do nothing */
3248 * Change the PAT attribute on an existing kernel memory map. Caller
3249 * must ensure that the virtual memory in question is not accessed
3250 * during the adjustment.
3253 pmap_change_attr(vm_offset_t va, vm_size_t count, int mode)
3255 /* This is a vkernel, do nothing */
3259 * Perform the pmap work for mincore
3261 * No other requirements.
3264 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3266 pt_entry_t *ptep, pte;
3270 lwkt_gettoken(&vm_token);
3271 ptep = pmap_pte(pmap, addr);
3273 if (ptep && (pte = *ptep) != 0) {
3276 val = MINCORE_INCORE;
3277 if ((pte & VPTE_MANAGED) == 0)
3280 pa = pte & VPTE_FRAME;
3282 m = PHYS_TO_VM_PAGE(pa);
3288 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3290 * Modified by someone
3292 else if (m->dirty || pmap_is_modified(m))
3293 val |= MINCORE_MODIFIED_OTHER;
3298 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3301 * Referenced by someone
3303 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3304 val |= MINCORE_REFERENCED_OTHER;
3305 vm_page_flag_set(m, PG_REFERENCED);
3309 lwkt_reltoken(&vm_token);
3314 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3315 * vmspace will be ref'd and the old one will be deref'd.
3317 * Caller must hold vmspace->vm_map.token for oldvm and newvm
3320 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3322 struct vmspace *oldvm;
3326 oldvm = p->p_vmspace;
3327 if (oldvm != newvm) {
3328 p->p_vmspace = newvm;
3329 KKASSERT(p->p_nthreads == 1);
3330 lp = RB_ROOT(&p->p_lwp_tree);
3331 pmap_setlwpvm(lp, newvm);
3333 sysref_get(&newvm->vm_sysref);
3334 sysref_put(&oldvm->vm_sysref);
3341 * Set the vmspace for a LWP. The vmspace is almost universally set the
3342 * same as the process vmspace, but virtual kernels need to swap out contexts
3343 * on a per-lwp basis.
3346 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3348 struct vmspace *oldvm;
3351 oldvm = lp->lwp_vmspace;
3354 lp->lwp_vmspace = newvm;
3355 if (curthread->td_lwp != lp)
3358 * NOTE: We don't have to worry about the CPULOCK here because
3359 * the virtual kernel doesn't call this function when VMM
3360 * is enabled (and depends on the host kernel when it isn't).
3363 pmap = vmspace_pmap(newvm);
3364 atomic_set_cpumask(&pmap->pm_active, CPUMASK(mycpu->gd_cpuid));
3365 #if defined(SWTCH_OPTIM_STATS)
3368 pmap = vmspace_pmap(oldvm);
3369 atomic_clear_cpumask(&pmap->pm_active, CPUMASK(mycpu->gd_cpuid));
3374 * The swtch code tried to switch in a heavy weight process whos pmap
3375 * is locked by another cpu. We have to wait for the lock to clear before
3376 * the pmap can be used.
3379 pmap_interlock_wait (struct vmspace *vm)
3381 pmap_t pmap = vmspace_pmap(vm);
3383 while (pmap->pm_active & CPUMASK_LOCK)
3388 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3391 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3395 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3400 * Used by kmalloc/kfree, page already exists at va
3403 pmap_kvtom(vm_offset_t va)
3407 KKASSERT(va >= KvaStart && va < KvaEnd);
3409 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));
3413 pmap_object_init(vm_object_t object)
3419 pmap_object_free(vm_object_t object)