2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
8 * Copyright (c) 2003 Peter Wemm
10 * Copyright (c) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
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
48 * Copyright (c) 2003 Networks Associates Technology, Inc.
49 * All rights reserved.
51 * This software was developed for the FreeBSD Project by Jake Burkholder,
52 * Safeport Network Services, and Network Associates Laboratories, the
53 * Security Research Division of Network Associates, Inc. under
54 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
55 * CHATS research program.
57 * Redistribution and use in source and binary forms, with or without
58 * modification, are permitted provided that the following conditions
60 * 1. Redistributions of source code must retain the above copyright
61 * notice, this list of conditions and the following disclaimer.
62 * 2. Redistributions in binary form must reproduce the above copyright
63 * notice, this list of conditions and the following disclaimer in the
64 * documentation and/or other materials provided with the distribution.
66 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
79 #include <sys/cdefs.h>
80 __FBSDID("$FreeBSD$");
83 * Manages physical address maps.
85 * In addition to hardware address maps, this
86 * module is called upon to provide software-use-only
87 * maps which may or may not be stored in the same
88 * form as hardware maps. These pseudo-maps are
89 * used to store intermediate results from copy
90 * operations to and from address spaces.
92 * Since the information managed by this module is
93 * also stored by the logical address mapping module,
94 * this module may throw away valid virtual-to-physical
95 * mappings at almost any time. However, invalidations
96 * of virtual-to-physical mappings must be done as
99 * In order to cope with hardware architectures which
100 * make virtual-to-physical map invalidates expensive,
101 * this module may delay invalidate or reduced protection
102 * operations until such time as they are actually
103 * necessary. This module is given full information as
104 * to which processors are currently using which maps,
105 * and to when physical maps must be made correct.
108 #include "opt_msgbuf.h"
109 #include "opt_pmap.h"
112 #include <sys/param.h>
113 #include <sys/systm.h>
114 #include <sys/kernel.h>
116 #include <sys/lock.h>
117 #include <sys/malloc.h>
118 #include <sys/mman.h>
119 #include <sys/msgbuf.h>
120 #include <sys/mutex.h>
121 #include <sys/proc.h>
123 #include <sys/vmmeter.h>
124 #include <sys/sched.h>
125 #include <sys/sysctl.h>
131 #include <vm/vm_param.h>
132 #include <vm/vm_kern.h>
133 #include <vm/vm_page.h>
134 #include <vm/vm_map.h>
135 #include <vm/vm_object.h>
136 #include <vm/vm_extern.h>
137 #include <vm/vm_pageout.h>
138 #include <vm/vm_pager.h>
139 #include <vm/vm_reserv.h>
142 #include <machine/cpu.h>
143 #include <machine/cputypes.h>
144 #include <machine/md_var.h>
145 #include <machine/pcb.h>
146 #include <machine/specialreg.h>
148 #include <machine/smp.h>
151 #ifndef PMAP_SHPGPERPROC
152 #define PMAP_SHPGPERPROC 200
155 #if !defined(DIAGNOSTIC)
156 #define PMAP_INLINE __gnu89_inline
163 #define PV_STAT(x) do { x ; } while (0)
165 #define PV_STAT(x) do { } while (0)
168 #define pa_index(pa) ((pa) >> PDRSHIFT)
169 #define pa_to_pvh(pa) (&pv_table[pa_index(pa)])
171 struct pmap kernel_pmap_store;
173 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
174 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
177 static vm_paddr_t dmaplimit;
178 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
181 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
183 static int pg_ps_enabled = 1;
184 SYSCTL_INT(_vm_pmap, OID_AUTO, pg_ps_enabled, CTLFLAG_RDTUN, &pg_ps_enabled, 0,
185 "Are large page mappings enabled?");
187 static u_int64_t KPTphys; /* phys addr of kernel level 1 */
188 static u_int64_t KPDphys; /* phys addr of kernel level 2 */
189 u_int64_t KPDPphys; /* phys addr of kernel level 3 */
190 u_int64_t KPML4phys; /* phys addr of kernel level 4 */
192 static u_int64_t DMPDphys; /* phys addr of direct mapped level 2 */
193 static u_int64_t DMPDPphys; /* phys addr of direct mapped level 3 */
196 * Data for the pv entry allocation mechanism
198 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
199 static struct md_page *pv_table;
200 static int shpgperproc = PMAP_SHPGPERPROC;
203 * All those kernel PT submaps that BSD is so fond of
205 pt_entry_t *CMAP1 = 0;
207 struct msgbuf *msgbufp = 0;
212 static caddr_t crashdumpmap;
214 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
215 static pv_entry_t get_pv_entry(pmap_t locked_pmap, int try);
216 static void pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
217 static boolean_t pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
218 static void pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
219 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
220 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
222 static int pmap_pvh_wired_mappings(struct md_page *pvh, int count);
224 static int pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode);
225 static boolean_t pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
226 static boolean_t pmap_demote_pdpe(pmap_t pmap, pdp_entry_t *pdpe,
228 static boolean_t pmap_enter_pde(pmap_t pmap, vm_offset_t va, vm_page_t m,
230 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
231 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
232 static void pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte);
233 static void pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte);
234 static void pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva);
235 static boolean_t pmap_is_modified_pvh(struct md_page *pvh);
236 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
237 static vm_page_t pmap_lookup_pt_page(pmap_t pmap, vm_offset_t va);
238 static void pmap_pde_attr(pd_entry_t *pde, int cache_bits);
239 static void pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
240 static boolean_t pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva,
242 static void pmap_pte_attr(pt_entry_t *pte, int cache_bits);
243 static int pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
245 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq,
246 vm_offset_t sva, pd_entry_t ptepde, vm_page_t *free);
247 static void pmap_remove_pt_page(pmap_t pmap, vm_page_t mpte);
248 static void pmap_remove_page(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
250 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
252 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
253 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
255 static void pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
257 static void pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde);
259 static vm_page_t pmap_allocpde(pmap_t pmap, vm_offset_t va, int flags);
260 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
262 static vm_page_t _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, int flags);
263 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
265 static int pmap_unuse_pt(pmap_t, vm_offset_t, pd_entry_t, vm_page_t *);
266 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
268 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
269 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
272 * Move the kernel virtual free pointer to the next
273 * 2MB. This is used to help improve performance
274 * by using a large (2MB) page for much of the kernel
275 * (.text, .data, .bss)
278 pmap_kmem_choose(vm_offset_t addr)
280 vm_offset_t newaddr = addr;
282 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
286 /********************/
287 /* Inline functions */
288 /********************/
290 /* Return a non-clipped PD index for a given VA */
291 static __inline vm_pindex_t
292 pmap_pde_pindex(vm_offset_t va)
294 return va >> PDRSHIFT;
298 /* Return various clipped indexes for a given VA */
299 static __inline vm_pindex_t
300 pmap_pte_index(vm_offset_t va)
303 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
306 static __inline vm_pindex_t
307 pmap_pde_index(vm_offset_t va)
310 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
313 static __inline vm_pindex_t
314 pmap_pdpe_index(vm_offset_t va)
317 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
320 static __inline vm_pindex_t
321 pmap_pml4e_index(vm_offset_t va)
324 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
327 /* Return a pointer to the PML4 slot that corresponds to a VA */
328 static __inline pml4_entry_t *
329 pmap_pml4e(pmap_t pmap, vm_offset_t va)
332 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
335 /* Return a pointer to the PDP slot that corresponds to a VA */
336 static __inline pdp_entry_t *
337 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
341 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME);
342 return (&pdpe[pmap_pdpe_index(va)]);
345 /* Return a pointer to the PDP slot that corresponds to a VA */
346 static __inline pdp_entry_t *
347 pmap_pdpe(pmap_t pmap, vm_offset_t va)
351 pml4e = pmap_pml4e(pmap, va);
352 if ((*pml4e & PG_V) == 0)
354 return (pmap_pml4e_to_pdpe(pml4e, va));
357 /* Return a pointer to the PD slot that corresponds to a VA */
358 static __inline pd_entry_t *
359 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
363 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME);
364 return (&pde[pmap_pde_index(va)]);
367 /* Return a pointer to the PD slot that corresponds to a VA */
368 static __inline pd_entry_t *
369 pmap_pde(pmap_t pmap, vm_offset_t va)
373 pdpe = pmap_pdpe(pmap, va);
374 if (pdpe == NULL || (*pdpe & PG_V) == 0)
376 return (pmap_pdpe_to_pde(pdpe, va));
379 /* Return a pointer to the PT slot that corresponds to a VA */
380 static __inline pt_entry_t *
381 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
385 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
386 return (&pte[pmap_pte_index(va)]);
389 /* Return a pointer to the PT slot that corresponds to a VA */
390 static __inline pt_entry_t *
391 pmap_pte(pmap_t pmap, vm_offset_t va)
395 pde = pmap_pde(pmap, va);
396 if (pde == NULL || (*pde & PG_V) == 0)
398 if ((*pde & PG_PS) != 0) /* compat with i386 pmap_pte() */
399 return ((pt_entry_t *)pde);
400 return (pmap_pde_to_pte(pde, va));
404 PMAP_INLINE pt_entry_t *
405 vtopte(vm_offset_t va)
407 u_int64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
409 return (PTmap + ((va >> PAGE_SHIFT) & mask));
412 static __inline pd_entry_t *
413 vtopde(vm_offset_t va)
415 u_int64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
417 return (PDmap + ((va >> PDRSHIFT) & mask));
421 allocpages(vm_paddr_t *firstaddr, int n)
426 bzero((void *)ret, n * PAGE_SIZE);
427 *firstaddr += n * PAGE_SIZE;
432 create_pagetables(vm_paddr_t *firstaddr)
437 KPTphys = allocpages(firstaddr, NKPT);
438 KPML4phys = allocpages(firstaddr, 1);
439 KPDPphys = allocpages(firstaddr, NKPML4E);
440 KPDphys = allocpages(firstaddr, NKPDPE);
442 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
443 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
445 DMPDPphys = allocpages(firstaddr, NDMPML4E);
446 if (TRUE || (amd_feature & AMDID_PAGE1GB) == 0)
447 DMPDphys = allocpages(firstaddr, ndmpdp);
448 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
450 /* Fill in the underlying page table pages */
451 /* Read-only from zero to physfree */
452 /* XXX not fully used, underneath 2M pages */
453 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
454 ((pt_entry_t *)KPTphys)[i] = i << PAGE_SHIFT;
455 ((pt_entry_t *)KPTphys)[i] |= PG_RW | PG_V | PG_G;
458 /* Now map the page tables at their location within PTmap */
459 for (i = 0; i < NKPT; i++) {
460 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
461 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
464 /* Map from zero to end of allocations under 2M pages */
465 /* This replaces some of the KPTphys entries above */
466 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
467 ((pd_entry_t *)KPDphys)[i] = i << PDRSHIFT;
468 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V | PG_PS | PG_G;
471 /* And connect up the PD to the PDP */
472 for (i = 0; i < NKPDPE; i++) {
473 ((pdp_entry_t *)KPDPphys)[i + KPDPI] = KPDphys +
475 ((pdp_entry_t *)KPDPphys)[i + KPDPI] |= PG_RW | PG_V | PG_U;
478 /* Now set up the direct map space using either 2MB or 1GB pages */
479 /* Preset PG_M and PG_A because demotion expects it */
480 if (TRUE || (amd_feature & AMDID_PAGE1GB) == 0) {
481 for (i = 0; i < NPDEPG * ndmpdp; i++) {
482 ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT;
483 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
486 /* And the direct map space's PDP */
487 for (i = 0; i < ndmpdp; i++) {
488 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
490 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
493 for (i = 0; i < ndmpdp; i++) {
494 ((pdp_entry_t *)DMPDPphys)[i] =
495 (vm_paddr_t)i << PDPSHIFT;
496 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
501 /* And recursively map PML4 to itself in order to get PTmap */
502 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
503 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
505 /* Connect the Direct Map slot up to the PML4 */
506 ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
507 ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
509 /* Connect the KVA slot up to the PML4 */
510 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
511 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
515 * Bootstrap the system enough to run with virtual memory.
517 * On amd64 this is called after mapping has already been enabled
518 * and just syncs the pmap module with what has already been done.
519 * [We can't call it easily with mapping off since the kernel is not
520 * mapped with PA == VA, hence we would have to relocate every address
521 * from the linked base (virtual) address "KERNBASE" to the actual
522 * (physical) address starting relative to 0]
525 pmap_bootstrap(vm_paddr_t *firstaddr)
528 pt_entry_t *pte, *unused;
531 * Create an initial set of page tables to run the kernel in.
533 create_pagetables(firstaddr);
535 virtual_avail = (vm_offset_t) KERNBASE + *firstaddr;
536 virtual_avail = pmap_kmem_choose(virtual_avail);
538 virtual_end = VM_MAX_KERNEL_ADDRESS;
541 /* XXX do %cr0 as well */
542 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
546 * Initialize the kernel pmap (which is statically allocated).
548 PMAP_LOCK_INIT(kernel_pmap);
549 kernel_pmap->pm_pml4 = (pdp_entry_t *)PHYS_TO_DMAP(KPML4phys);
550 kernel_pmap->pm_root = NULL;
551 kernel_pmap->pm_active = -1; /* don't allow deactivation */
552 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
555 * Reserve some special page table entries/VA space for temporary
558 #define SYSMAP(c, p, v, n) \
559 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
565 * CMAP1 is only used for the memory test.
567 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
572 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
575 * msgbufp is used to map the system message buffer.
577 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(MSGBUF_SIZE)))
585 /* Initialize the PAT MSR. */
597 /* Bail if this CPU doesn't implement PAT. */
598 if (!(cpu_feature & CPUID_PAT))
602 * Leave the indices 0-3 at the default of WB, WT, UC, and UC-.
603 * Program 4 and 5 as WP and WC.
604 * Leave 6 and 7 as UC and UC-.
606 pat_msr = rdmsr(MSR_PAT);
607 pat_msr &= ~(PAT_MASK(4) | PAT_MASK(5));
608 pat_msr |= PAT_VALUE(4, PAT_WRITE_PROTECTED) |
609 PAT_VALUE(5, PAT_WRITE_COMBINING);
610 wrmsr(MSR_PAT, pat_msr);
614 * Initialize a vm_page's machine-dependent fields.
617 pmap_page_init(vm_page_t m)
620 TAILQ_INIT(&m->md.pv_list);
621 m->md.pat_mode = PAT_WRITE_BACK;
625 * Initialize the pmap module.
626 * Called by vm_init, to initialize any structures that the pmap
627 * system needs to map virtual memory.
637 * Initialize the vm page array entries for the kernel pmap's
640 for (i = 0; i < NKPT; i++) {
641 mpte = PHYS_TO_VM_PAGE(KPTphys + (i << PAGE_SHIFT));
642 KASSERT(mpte >= vm_page_array &&
643 mpte < &vm_page_array[vm_page_array_size],
644 ("pmap_init: page table page is out of range"));
645 mpte->pindex = pmap_pde_pindex(KERNBASE) + i;
646 mpte->phys_addr = KPTphys + (i << PAGE_SHIFT);
650 * Initialize the address space (zone) for the pv entries. Set a
651 * high water mark so that the system can recover from excessive
652 * numbers of pv entries.
654 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
655 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
656 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
657 pv_entry_high_water = 9 * (pv_entry_max / 10);
660 * If the kernel is running in a virtual machine on an AMD Family 10h
661 * processor, then it must assume that MCA is enabled by the virtual
664 if (vm_guest == VM_GUEST_VM && cpu_vendor_id == CPU_VENDOR_AMD &&
665 CPUID_TO_FAMILY(cpu_id) == 0x10)
666 workaround_erratum383 = 1;
669 * Are large page mappings enabled?
671 TUNABLE_INT_FETCH("vm.pmap.pg_ps_enabled", &pg_ps_enabled);
673 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
674 ("pmap_init: can't assign to pagesizes[1]"));
675 pagesizes[1] = NBPDR;
679 * Calculate the size of the pv head table for superpages.
681 for (i = 0; phys_avail[i + 1]; i += 2);
682 pv_npg = round_2mpage(phys_avail[(i - 2) + 1]) / NBPDR;
685 * Allocate memory for the pv head table for superpages.
687 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
689 pv_table = (struct md_page *)kmem_alloc(kernel_map, s);
690 for (i = 0; i < pv_npg; i++)
691 TAILQ_INIT(&pv_table[i].pv_list);
695 pmap_pventry_proc(SYSCTL_HANDLER_ARGS)
699 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
700 if (error == 0 && req->newptr) {
701 shpgperproc = (pv_entry_max - cnt.v_page_count) / maxproc;
702 pv_entry_high_water = 9 * (pv_entry_max / 10);
706 SYSCTL_PROC(_vm_pmap, OID_AUTO, pv_entry_max, CTLTYPE_INT|CTLFLAG_RW,
707 &pv_entry_max, 0, pmap_pventry_proc, "IU", "Max number of PV entries");
710 pmap_shpgperproc_proc(SYSCTL_HANDLER_ARGS)
714 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
715 if (error == 0 && req->newptr) {
716 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
717 pv_entry_high_water = 9 * (pv_entry_max / 10);
721 SYSCTL_PROC(_vm_pmap, OID_AUTO, shpgperproc, CTLTYPE_INT|CTLFLAG_RW,
722 &shpgperproc, 0, pmap_shpgperproc_proc, "IU", "Page share factor per proc");
724 SYSCTL_NODE(_vm_pmap, OID_AUTO, pde, CTLFLAG_RD, 0,
725 "2MB page mapping counters");
727 static u_long pmap_pde_demotions;
728 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, demotions, CTLFLAG_RD,
729 &pmap_pde_demotions, 0, "2MB page demotions");
731 static u_long pmap_pde_mappings;
732 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, mappings, CTLFLAG_RD,
733 &pmap_pde_mappings, 0, "2MB page mappings");
735 static u_long pmap_pde_p_failures;
736 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, p_failures, CTLFLAG_RD,
737 &pmap_pde_p_failures, 0, "2MB page promotion failures");
739 static u_long pmap_pde_promotions;
740 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, promotions, CTLFLAG_RD,
741 &pmap_pde_promotions, 0, "2MB page promotions");
743 SYSCTL_NODE(_vm_pmap, OID_AUTO, pdpe, CTLFLAG_RD, 0,
744 "1GB page mapping counters");
746 static u_long pmap_pdpe_demotions;
747 SYSCTL_ULONG(_vm_pmap_pdpe, OID_AUTO, demotions, CTLFLAG_RD,
748 &pmap_pdpe_demotions, 0, "1GB page demotions");
751 /***************************************************
752 * Low level helper routines.....
753 ***************************************************/
756 * Determine the appropriate bits to set in a PTE or PDE for a specified
760 pmap_cache_bits(int mode, boolean_t is_pde)
762 int pat_flag, pat_index, cache_bits;
764 /* The PAT bit is different for PTE's and PDE's. */
765 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
767 /* Map the caching mode to a PAT index. */
769 case PAT_UNCACHEABLE:
772 case PAT_WRITE_THROUGH:
781 case PAT_WRITE_COMBINING:
784 case PAT_WRITE_PROTECTED:
788 panic("Unknown caching mode %d\n", mode);
791 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
794 cache_bits |= pat_flag;
796 cache_bits |= PG_NC_PCD;
798 cache_bits |= PG_NC_PWT;
803 * After changing the page size for the specified virtual address in the page
804 * table, flush the corresponding entries from the processor's TLB. Only the
805 * calling processor's TLB is affected.
807 * The calling thread must be pinned to a processor.
810 pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde)
814 if ((newpde & PG_PS) == 0)
815 /* Demotion: flush a specific 2MB page mapping. */
817 else if ((newpde & PG_G) == 0)
819 * Promotion: flush every 4KB page mapping from the TLB
820 * because there are too many to flush individually.
825 * Promotion: flush every 4KB page mapping from the TLB,
826 * including any global (PG_G) mappings.
829 load_cr4(cr4 & ~CR4_PGE);
831 * Although preemption at this point could be detrimental to
832 * performance, it would not lead to an error. PG_G is simply
833 * ignored if CR4.PGE is clear. Moreover, in case this block
834 * is re-entered, the load_cr4() either above or below will
835 * modify CR4.PGE flushing the TLB.
837 load_cr4(cr4 | CR4_PGE);
842 * For SMP, these functions have to use the IPI mechanism for coherence.
844 * N.B.: Before calling any of the following TLB invalidation functions,
845 * the calling processor must ensure that all stores updating a non-
846 * kernel page table are globally performed. Otherwise, another
847 * processor could cache an old, pre-update entry without being
848 * invalidated. This can happen one of two ways: (1) The pmap becomes
849 * active on another processor after its pm_active field is checked by
850 * one of the following functions but before a store updating the page
851 * table is globally performed. (2) The pmap becomes active on another
852 * processor before its pm_active field is checked but due to
853 * speculative loads one of the following functions stills reads the
854 * pmap as inactive on the other processor.
856 * The kernel page table is exempt because its pm_active field is
857 * immutable. The kernel page table is always active on every
861 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
867 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
871 cpumask = PCPU_GET(cpumask);
872 other_cpus = PCPU_GET(other_cpus);
873 if (pmap->pm_active & cpumask)
875 if (pmap->pm_active & other_cpus)
876 smp_masked_invlpg(pmap->pm_active & other_cpus, va);
882 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
889 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
890 for (addr = sva; addr < eva; addr += PAGE_SIZE)
892 smp_invlpg_range(sva, eva);
894 cpumask = PCPU_GET(cpumask);
895 other_cpus = PCPU_GET(other_cpus);
896 if (pmap->pm_active & cpumask)
897 for (addr = sva; addr < eva; addr += PAGE_SIZE)
899 if (pmap->pm_active & other_cpus)
900 smp_masked_invlpg_range(pmap->pm_active & other_cpus,
907 pmap_invalidate_all(pmap_t pmap)
913 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
917 cpumask = PCPU_GET(cpumask);
918 other_cpus = PCPU_GET(other_cpus);
919 if (pmap->pm_active & cpumask)
921 if (pmap->pm_active & other_cpus)
922 smp_masked_invltlb(pmap->pm_active & other_cpus);
928 pmap_invalidate_cache(void)
938 cpumask_t store; /* processor that updates the PDE */
939 cpumask_t invalidate; /* processors that invalidate their TLB */
946 pmap_update_pde_action(void *arg)
948 struct pde_action *act = arg;
950 if (act->store == PCPU_GET(cpumask))
951 pde_store(act->pde, act->newpde);
955 pmap_update_pde_teardown(void *arg)
957 struct pde_action *act = arg;
959 if ((act->invalidate & PCPU_GET(cpumask)) != 0)
960 pmap_update_pde_invalidate(act->va, act->newpde);
964 * Change the page size for the specified virtual address in a way that
965 * prevents any possibility of the TLB ever having two entries that map the
966 * same virtual address using different page sizes. This is the recommended
967 * workaround for Erratum 383 on AMD Family 10h processors. It prevents a
968 * machine check exception for a TLB state that is improperly diagnosed as a
972 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
974 struct pde_action act;
975 cpumask_t active, cpumask;
978 cpumask = PCPU_GET(cpumask);
979 if (pmap == kernel_pmap)
982 active = pmap->pm_active;
983 if ((active & PCPU_GET(other_cpus)) != 0) {
985 act.invalidate = active;
989 smp_rendezvous_cpus(cpumask | active,
990 smp_no_rendevous_barrier, pmap_update_pde_action,
991 pmap_update_pde_teardown, &act);
993 pde_store(pde, newpde);
994 if ((active & cpumask) != 0)
995 pmap_update_pde_invalidate(va, newpde);
1001 * Normal, non-SMP, invalidation functions.
1002 * We inline these within pmap.c for speed.
1005 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
1008 if (pmap == kernel_pmap || pmap->pm_active)
1013 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1017 if (pmap == kernel_pmap || pmap->pm_active)
1018 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1023 pmap_invalidate_all(pmap_t pmap)
1026 if (pmap == kernel_pmap || pmap->pm_active)
1031 pmap_invalidate_cache(void)
1038 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1041 pde_store(pde, newpde);
1042 if (pmap == kernel_pmap || pmap->pm_active)
1043 pmap_update_pde_invalidate(va, newpde);
1048 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva)
1051 KASSERT((sva & PAGE_MASK) == 0,
1052 ("pmap_invalidate_cache_range: sva not page-aligned"));
1053 KASSERT((eva & PAGE_MASK) == 0,
1054 ("pmap_invalidate_cache_range: eva not page-aligned"));
1056 if (cpu_feature & CPUID_SS)
1057 ; /* If "Self Snoop" is supported, do nothing. */
1058 else if ((cpu_feature & CPUID_CLFSH) != 0 &&
1059 eva - sva < 2 * 1024 * 1024) {
1062 * Otherwise, do per-cache line flush. Use the mfence
1063 * instruction to insure that previous stores are
1064 * included in the write-back. The processor
1065 * propagates flush to other processors in the cache
1069 for (; sva < eva; sva += cpu_clflush_line_size)
1075 * No targeted cache flush methods are supported by CPU,
1076 * or the supplied range is bigger than 2MB.
1077 * Globally invalidate cache.
1079 pmap_invalidate_cache();
1084 * Are we current address space or kernel?
1087 pmap_is_current(pmap_t pmap)
1089 return (pmap == kernel_pmap ||
1090 (pmap->pm_pml4[PML4PML4I] & PG_FRAME) == (PML4pml4e[0] & PG_FRAME));
1094 * Routine: pmap_extract
1096 * Extract the physical page address associated
1097 * with the given map/virtual_address pair.
1100 pmap_extract(pmap_t pmap, vm_offset_t va)
1104 pd_entry_t pde, *pdep;
1108 pdep = pmap_pde(pmap, va);
1112 if ((pde & PG_PS) != 0)
1113 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
1115 pte = pmap_pde_to_pte(pdep, va);
1116 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1125 * Routine: pmap_extract_and_hold
1127 * Atomically extract and hold the physical page
1128 * with the given pmap and virtual address pair
1129 * if that mapping permits the given protection.
1132 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1134 pd_entry_t pde, *pdep;
1139 vm_page_lock_queues();
1141 pdep = pmap_pde(pmap, va);
1142 if (pdep != NULL && (pde = *pdep)) {
1144 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1145 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
1150 pte = *pmap_pde_to_pte(pdep, va);
1152 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1153 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1158 vm_page_unlock_queues();
1164 pmap_kextract(vm_offset_t va)
1169 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
1170 pa = DMAP_TO_PHYS(va);
1174 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
1177 * Beware of a concurrent promotion that changes the
1178 * PDE at this point! For example, vtopte() must not
1179 * be used to access the PTE because it would use the
1180 * new PDE. It is, however, safe to use the old PDE
1181 * because the page table page is preserved by the
1184 pa = *pmap_pde_to_pte(&pde, va);
1185 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1191 /***************************************************
1192 * Low level mapping routines.....
1193 ***************************************************/
1196 * Add a wired page to the kva.
1197 * Note: not SMP coherent.
1200 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1205 pte_store(pte, pa | PG_RW | PG_V | PG_G);
1208 static __inline void
1209 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1214 pte_store(pte, pa | PG_RW | PG_V | PG_G | pmap_cache_bits(mode, 0));
1218 * Remove a page from the kernel pagetables.
1219 * Note: not SMP coherent.
1222 pmap_kremove(vm_offset_t va)
1231 * Used to map a range of physical addresses into kernel
1232 * virtual address space.
1234 * The value passed in '*virt' is a suggested virtual address for
1235 * the mapping. Architectures which can support a direct-mapped
1236 * physical to virtual region can return the appropriate address
1237 * within that region, leaving '*virt' unchanged. Other
1238 * architectures should map the pages starting at '*virt' and
1239 * update '*virt' with the first usable address after the mapped
1243 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1245 return PHYS_TO_DMAP(start);
1250 * Add a list of wired pages to the kva
1251 * this routine is only used for temporary
1252 * kernel mappings that do not need to have
1253 * page modification or references recorded.
1254 * Note that old mappings are simply written
1255 * over. The page *must* be wired.
1256 * Note: SMP coherent. Uses a ranged shootdown IPI.
1259 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1261 pt_entry_t *endpte, oldpte, *pte;
1265 endpte = pte + count;
1266 while (pte < endpte) {
1268 pte_store(pte, VM_PAGE_TO_PHYS(*ma) | PG_G |
1269 pmap_cache_bits((*ma)->md.pat_mode, 0) | PG_RW | PG_V);
1273 if ((oldpte & PG_V) != 0)
1274 pmap_invalidate_range(kernel_pmap, sva, sva + count *
1279 * This routine tears out page mappings from the
1280 * kernel -- it is meant only for temporary mappings.
1281 * Note: SMP coherent. Uses a ranged shootdown IPI.
1284 pmap_qremove(vm_offset_t sva, int count)
1289 while (count-- > 0) {
1293 pmap_invalidate_range(kernel_pmap, sva, va);
1296 /***************************************************
1297 * Page table page management routines.....
1298 ***************************************************/
1299 static __inline void
1300 pmap_free_zero_pages(vm_page_t free)
1304 while (free != NULL) {
1307 /* Preserve the page's PG_ZERO setting. */
1308 vm_page_free_toq(m);
1313 * Schedule the specified unused page table page to be freed. Specifically,
1314 * add the page to the specified list of pages that will be released to the
1315 * physical memory manager after the TLB has been updated.
1317 static __inline void
1318 pmap_add_delayed_free_list(vm_page_t m, vm_page_t *free, boolean_t set_PG_ZERO)
1322 m->flags |= PG_ZERO;
1324 m->flags &= ~PG_ZERO;
1330 * Inserts the specified page table page into the specified pmap's collection
1331 * of idle page table pages. Each of a pmap's page table pages is responsible
1332 * for mapping a distinct range of virtual addresses. The pmap's collection is
1333 * ordered by this virtual address range.
1336 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte)
1340 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1341 root = pmap->pm_root;
1346 root = vm_page_splay(mpte->pindex, root);
1347 if (mpte->pindex < root->pindex) {
1348 mpte->left = root->left;
1351 } else if (mpte->pindex == root->pindex)
1352 panic("pmap_insert_pt_page: pindex already inserted");
1354 mpte->right = root->right;
1359 pmap->pm_root = mpte;
1363 * Looks for a page table page mapping the specified virtual address in the
1364 * specified pmap's collection of idle page table pages. Returns NULL if there
1365 * is no page table page corresponding to the specified virtual address.
1368 pmap_lookup_pt_page(pmap_t pmap, vm_offset_t va)
1371 vm_pindex_t pindex = pmap_pde_pindex(va);
1373 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1374 if ((mpte = pmap->pm_root) != NULL && mpte->pindex != pindex) {
1375 mpte = vm_page_splay(pindex, mpte);
1376 if ((pmap->pm_root = mpte)->pindex != pindex)
1383 * Removes the specified page table page from the specified pmap's collection
1384 * of idle page table pages. The specified page table page must be a member of
1385 * the pmap's collection.
1388 pmap_remove_pt_page(pmap_t pmap, vm_page_t mpte)
1392 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1393 if (mpte != pmap->pm_root) {
1394 root = vm_page_splay(mpte->pindex, pmap->pm_root);
1395 KASSERT(mpte == root,
1396 ("pmap_remove_pt_page: mpte %p is missing from pmap %p",
1399 if (mpte->left == NULL)
1402 root = vm_page_splay(mpte->pindex, mpte->left);
1403 root->right = mpte->right;
1405 pmap->pm_root = root;
1409 * This routine unholds page table pages, and if the hold count
1410 * drops to zero, then it decrements the wire count.
1413 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t *free)
1417 if (m->wire_count == 0)
1418 return _pmap_unwire_pte_hold(pmap, va, m, free);
1424 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1429 * unmap the page table page
1431 if (m->pindex >= (NUPDE + NUPDPE)) {
1434 pml4 = pmap_pml4e(pmap, va);
1436 } else if (m->pindex >= NUPDE) {
1439 pdp = pmap_pdpe(pmap, va);
1444 pd = pmap_pde(pmap, va);
1447 --pmap->pm_stats.resident_count;
1448 if (m->pindex < NUPDE) {
1449 /* We just released a PT, unhold the matching PD */
1452 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1453 pmap_unwire_pte_hold(pmap, va, pdpg, free);
1455 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1456 /* We just released a PD, unhold the matching PDP */
1459 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1460 pmap_unwire_pte_hold(pmap, va, pdppg, free);
1464 * This is a release store so that the ordinary store unmapping
1465 * the page table page is globally performed before TLB shoot-
1468 atomic_subtract_rel_int(&cnt.v_wire_count, 1);
1471 * Put page on a list so that it is released after
1472 * *ALL* TLB shootdown is done
1474 pmap_add_delayed_free_list(m, free, TRUE);
1480 * After removing a page table entry, this routine is used to
1481 * conditionally free the page, and manage the hold/wire counts.
1484 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, pd_entry_t ptepde, vm_page_t *free)
1488 if (va >= VM_MAXUSER_ADDRESS)
1490 KASSERT(ptepde != 0, ("pmap_unuse_pt: ptepde != 0"));
1491 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1492 return pmap_unwire_pte_hold(pmap, va, mpte, free);
1496 pmap_pinit0(pmap_t pmap)
1499 PMAP_LOCK_INIT(pmap);
1500 pmap->pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
1501 pmap->pm_root = NULL;
1502 pmap->pm_active = 0;
1503 TAILQ_INIT(&pmap->pm_pvchunk);
1504 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1508 * Initialize a preallocated and zeroed pmap structure,
1509 * such as one in a vmspace structure.
1512 pmap_pinit(pmap_t pmap)
1515 static vm_pindex_t color;
1517 PMAP_LOCK_INIT(pmap);
1520 * allocate the page directory page
1522 while ((pml4pg = vm_page_alloc(NULL, color++, VM_ALLOC_NOOBJ |
1523 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL)
1526 pmap->pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pml4pg));
1528 if ((pml4pg->flags & PG_ZERO) == 0)
1529 pagezero(pmap->pm_pml4);
1531 /* Wire in kernel global address entries. */
1532 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1533 pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1535 /* install self-referential address mapping entry(s) */
1536 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(pml4pg) | PG_V | PG_RW | PG_A | PG_M;
1538 pmap->pm_root = NULL;
1539 pmap->pm_active = 0;
1540 TAILQ_INIT(&pmap->pm_pvchunk);
1541 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1547 * this routine is called if the page table page is not
1550 * Note: If a page allocation fails at page table level two or three,
1551 * one or two pages may be held during the wait, only to be released
1552 * afterwards. This conservative approach is easily argued to avoid
1556 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, int flags)
1558 vm_page_t m, pdppg, pdpg;
1560 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1561 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1562 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1565 * Allocate a page table page.
1567 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1568 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1569 if (flags & M_WAITOK) {
1571 vm_page_unlock_queues();
1573 vm_page_lock_queues();
1578 * Indicate the need to retry. While waiting, the page table
1579 * page may have been allocated.
1583 if ((m->flags & PG_ZERO) == 0)
1587 * Map the pagetable page into the process address space, if
1588 * it isn't already there.
1591 if (ptepindex >= (NUPDE + NUPDPE)) {
1593 vm_pindex_t pml4index;
1595 /* Wire up a new PDPE page */
1596 pml4index = ptepindex - (NUPDE + NUPDPE);
1597 pml4 = &pmap->pm_pml4[pml4index];
1598 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1600 } else if (ptepindex >= NUPDE) {
1601 vm_pindex_t pml4index;
1602 vm_pindex_t pdpindex;
1606 /* Wire up a new PDE page */
1607 pdpindex = ptepindex - NUPDE;
1608 pml4index = pdpindex >> NPML4EPGSHIFT;
1610 pml4 = &pmap->pm_pml4[pml4index];
1611 if ((*pml4 & PG_V) == 0) {
1612 /* Have to allocate a new pdp, recurse */
1613 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index,
1616 atomic_subtract_int(&cnt.v_wire_count, 1);
1617 vm_page_free_zero(m);
1621 /* Add reference to pdp page */
1622 pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1623 pdppg->wire_count++;
1625 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1627 /* Now find the pdp page */
1628 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1629 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1632 vm_pindex_t pml4index;
1633 vm_pindex_t pdpindex;
1638 /* Wire up a new PTE page */
1639 pdpindex = ptepindex >> NPDPEPGSHIFT;
1640 pml4index = pdpindex >> NPML4EPGSHIFT;
1642 /* First, find the pdp and check that its valid. */
1643 pml4 = &pmap->pm_pml4[pml4index];
1644 if ((*pml4 & PG_V) == 0) {
1645 /* Have to allocate a new pd, recurse */
1646 if (_pmap_allocpte(pmap, NUPDE + pdpindex,
1649 atomic_subtract_int(&cnt.v_wire_count, 1);
1650 vm_page_free_zero(m);
1653 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1654 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1656 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1657 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1658 if ((*pdp & PG_V) == 0) {
1659 /* Have to allocate a new pd, recurse */
1660 if (_pmap_allocpte(pmap, NUPDE + pdpindex,
1663 atomic_subtract_int(&cnt.v_wire_count,
1665 vm_page_free_zero(m);
1669 /* Add reference to the pd page */
1670 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1674 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1676 /* Now we know where the page directory page is */
1677 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1678 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1681 pmap->pm_stats.resident_count++;
1687 pmap_allocpde(pmap_t pmap, vm_offset_t va, int flags)
1689 vm_pindex_t pdpindex, ptepindex;
1693 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1694 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1695 ("pmap_allocpde: flags is neither M_NOWAIT nor M_WAITOK"));
1697 pdpe = pmap_pdpe(pmap, va);
1698 if (pdpe != NULL && (*pdpe & PG_V) != 0) {
1699 /* Add a reference to the pd page. */
1700 pdpg = PHYS_TO_VM_PAGE(*pdpe & PG_FRAME);
1703 /* Allocate a pd page. */
1704 ptepindex = pmap_pde_pindex(va);
1705 pdpindex = ptepindex >> NPDPEPGSHIFT;
1706 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex, flags);
1707 if (pdpg == NULL && (flags & M_WAITOK))
1714 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1716 vm_pindex_t ptepindex;
1720 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1721 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1722 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1725 * Calculate pagetable page index
1727 ptepindex = pmap_pde_pindex(va);
1730 * Get the page directory entry
1732 pd = pmap_pde(pmap, va);
1735 * This supports switching from a 2MB page to a
1738 if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1739 if (!pmap_demote_pde(pmap, pd, va)) {
1741 * Invalidation of the 2MB page mapping may have caused
1742 * the deallocation of the underlying PD page.
1749 * If the page table page is mapped, we just increment the
1750 * hold count, and activate it.
1752 if (pd != NULL && (*pd & PG_V) != 0) {
1753 m = PHYS_TO_VM_PAGE(*pd & PG_FRAME);
1757 * Here if the pte page isn't mapped, or if it has been
1760 m = _pmap_allocpte(pmap, ptepindex, flags);
1761 if (m == NULL && (flags & M_WAITOK))
1768 /***************************************************
1769 * Pmap allocation/deallocation routines.
1770 ***************************************************/
1773 * Release any resources held by the given physical map.
1774 * Called when a pmap initialized by pmap_pinit is being released.
1775 * Should only be called if the map contains no valid mappings.
1778 pmap_release(pmap_t pmap)
1782 KASSERT(pmap->pm_stats.resident_count == 0,
1783 ("pmap_release: pmap resident count %ld != 0",
1784 pmap->pm_stats.resident_count));
1785 KASSERT(pmap->pm_root == NULL,
1786 ("pmap_release: pmap has reserved page table page(s)"));
1788 m = PHYS_TO_VM_PAGE(pmap->pm_pml4[PML4PML4I] & PG_FRAME);
1790 pmap->pm_pml4[KPML4I] = 0; /* KVA */
1791 pmap->pm_pml4[DMPML4I] = 0; /* Direct Map */
1792 pmap->pm_pml4[PML4PML4I] = 0; /* Recursive Mapping */
1795 atomic_subtract_int(&cnt.v_wire_count, 1);
1796 vm_page_free_zero(m);
1797 PMAP_LOCK_DESTROY(pmap);
1801 kvm_size(SYSCTL_HANDLER_ARGS)
1803 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS;
1805 return sysctl_handle_long(oidp, &ksize, 0, req);
1807 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1808 0, 0, kvm_size, "LU", "Size of KVM");
1811 kvm_free(SYSCTL_HANDLER_ARGS)
1813 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1815 return sysctl_handle_long(oidp, &kfree, 0, req);
1817 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1818 0, 0, kvm_free, "LU", "Amount of KVM free");
1821 * grow the number of kernel page table entries, if needed
1824 pmap_growkernel(vm_offset_t addr)
1828 pd_entry_t *pde, newpdir;
1831 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1834 * Return if "addr" is within the range of kernel page table pages
1835 * that were preallocated during pmap bootstrap. Moreover, leave
1836 * "kernel_vm_end" and the kernel page table as they were.
1838 * The correctness of this action is based on the following
1839 * argument: vm_map_findspace() allocates contiguous ranges of the
1840 * kernel virtual address space. It calls this function if a range
1841 * ends after "kernel_vm_end". If the kernel is mapped between
1842 * "kernel_vm_end" and "addr", then the range cannot begin at
1843 * "kernel_vm_end". In fact, its beginning address cannot be less
1844 * than the kernel. Thus, there is no immediate need to allocate
1845 * any new kernel page table pages between "kernel_vm_end" and
1848 if (KERNBASE < addr && addr <= KERNBASE + NKPT * NBPDR)
1851 addr = roundup2(addr, NBPDR);
1852 if (addr - 1 >= kernel_map->max_offset)
1853 addr = kernel_map->max_offset;
1854 while (kernel_vm_end < addr) {
1855 pdpe = pmap_pdpe(kernel_pmap, kernel_vm_end);
1856 if ((*pdpe & PG_V) == 0) {
1857 /* We need a new PDP entry */
1858 nkpg = vm_page_alloc(NULL, kernel_vm_end >> PDPSHIFT,
1859 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
1860 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
1862 panic("pmap_growkernel: no memory to grow kernel");
1863 if ((nkpg->flags & PG_ZERO) == 0)
1864 pmap_zero_page(nkpg);
1865 paddr = VM_PAGE_TO_PHYS(nkpg);
1866 *pdpe = (pdp_entry_t)
1867 (paddr | PG_V | PG_RW | PG_A | PG_M);
1868 continue; /* try again */
1870 pde = pmap_pdpe_to_pde(pdpe, kernel_vm_end);
1871 if ((*pde & PG_V) != 0) {
1872 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1873 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1874 kernel_vm_end = kernel_map->max_offset;
1880 nkpg = vm_page_alloc(NULL, pmap_pde_pindex(kernel_vm_end),
1881 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1884 panic("pmap_growkernel: no memory to grow kernel");
1885 if ((nkpg->flags & PG_ZERO) == 0)
1886 pmap_zero_page(nkpg);
1887 paddr = VM_PAGE_TO_PHYS(nkpg);
1888 newpdir = (pd_entry_t) (paddr | PG_V | PG_RW | PG_A | PG_M);
1889 pde_store(pde, newpdir);
1891 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1892 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1893 kernel_vm_end = kernel_map->max_offset;
1900 /***************************************************
1901 * page management routines.
1902 ***************************************************/
1904 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1905 CTASSERT(_NPCM == 3);
1906 CTASSERT(_NPCPV == 168);
1908 static __inline struct pv_chunk *
1909 pv_to_chunk(pv_entry_t pv)
1912 return (struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK);
1915 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1917 #define PC_FREE0 0xfffffffffffffffful
1918 #define PC_FREE1 0xfffffffffffffffful
1919 #define PC_FREE2 0x000000fffffffffful
1921 static uint64_t pc_freemask[_NPCM] = { PC_FREE0, PC_FREE1, PC_FREE2 };
1923 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1924 "Current number of pv entries");
1927 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1929 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1930 "Current number of pv entry chunks");
1931 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1932 "Current number of pv entry chunks allocated");
1933 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1934 "Current number of pv entry chunks frees");
1935 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1936 "Number of times tried to get a chunk page but failed.");
1938 static long pv_entry_frees, pv_entry_allocs;
1939 static int pv_entry_spare;
1941 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1942 "Current number of pv entry frees");
1943 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1944 "Current number of pv entry allocs");
1945 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1946 "Current number of spare pv entries");
1948 static int pmap_collect_inactive, pmap_collect_active;
1950 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_inactive, CTLFLAG_RD, &pmap_collect_inactive, 0,
1951 "Current number times pmap_collect called on inactive queue");
1952 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_active, CTLFLAG_RD, &pmap_collect_active, 0,
1953 "Current number times pmap_collect called on active queue");
1957 * We are in a serious low memory condition. Resort to
1958 * drastic measures to free some pages so we can allocate
1959 * another pv entry chunk. This is normally called to
1960 * unmap inactive pages, and if necessary, active pages.
1962 * We do not, however, unmap 2mpages because subsequent accesses will
1963 * allocate per-page pv entries until repromotion occurs, thereby
1964 * exacerbating the shortage of free pv entries.
1967 pmap_collect(pmap_t locked_pmap, struct vpgqueues *vpq)
1969 struct md_page *pvh;
1972 pt_entry_t *pte, tpte;
1973 pv_entry_t next_pv, pv;
1977 TAILQ_FOREACH(m, &vpq->pl, pageq) {
1978 if (m->hold_count || m->busy)
1980 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
1983 /* Avoid deadlock and lock recursion. */
1984 if (pmap > locked_pmap)
1986 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
1988 pmap->pm_stats.resident_count--;
1989 pde = pmap_pde(pmap, va);
1990 KASSERT((*pde & PG_PS) == 0, ("pmap_collect: found"
1991 " a 2mpage in page %p's pv list", m));
1992 pte = pmap_pde_to_pte(pde, va);
1993 tpte = pte_load_clear(pte);
1994 KASSERT((tpte & PG_W) == 0,
1995 ("pmap_collect: wired pte %#lx", tpte));
1997 vm_page_flag_set(m, PG_REFERENCED);
1998 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2001 pmap_unuse_pt(pmap, va, *pde, &free);
2002 pmap_invalidate_page(pmap, va);
2003 pmap_free_zero_pages(free);
2004 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2005 if (TAILQ_EMPTY(&m->md.pv_list)) {
2006 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2007 if (TAILQ_EMPTY(&pvh->pv_list))
2008 vm_page_flag_clear(m, PG_WRITEABLE);
2010 free_pv_entry(pmap, pv);
2011 if (pmap != locked_pmap)
2019 * free the pv_entry back to the free list
2022 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2025 struct pv_chunk *pc;
2026 int idx, field, bit;
2028 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2029 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2030 PV_STAT(pv_entry_frees++);
2031 PV_STAT(pv_entry_spare++);
2033 pc = pv_to_chunk(pv);
2034 idx = pv - &pc->pc_pventry[0];
2037 pc->pc_map[field] |= 1ul << bit;
2038 /* move to head of list */
2039 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2040 if (pc->pc_map[0] != PC_FREE0 || pc->pc_map[1] != PC_FREE1 ||
2041 pc->pc_map[2] != PC_FREE2) {
2042 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2045 PV_STAT(pv_entry_spare -= _NPCPV);
2046 PV_STAT(pc_chunk_count--);
2047 PV_STAT(pc_chunk_frees++);
2048 /* entire chunk is free, return it */
2049 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
2050 dump_drop_page(m->phys_addr);
2051 vm_page_unwire(m, 0);
2056 * get a new pv_entry, allocating a block from the system
2060 get_pv_entry(pmap_t pmap, int try)
2062 static const struct timeval printinterval = { 60, 0 };
2063 static struct timeval lastprint;
2064 static vm_pindex_t colour;
2065 struct vpgqueues *pq;
2068 struct pv_chunk *pc;
2071 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2072 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2073 PV_STAT(pv_entry_allocs++);
2075 if (pv_entry_count > pv_entry_high_water)
2076 if (ratecheck(&lastprint, &printinterval))
2077 printf("Approaching the limit on PV entries, consider "
2078 "increasing either the vm.pmap.shpgperproc or the "
2079 "vm.pmap.pv_entry_max sysctl.\n");
2082 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2084 for (field = 0; field < _NPCM; field++) {
2085 if (pc->pc_map[field]) {
2086 bit = bsfq(pc->pc_map[field]);
2090 if (field < _NPCM) {
2091 pv = &pc->pc_pventry[field * 64 + bit];
2092 pc->pc_map[field] &= ~(1ul << bit);
2093 /* If this was the last item, move it to tail */
2094 if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0 &&
2095 pc->pc_map[2] == 0) {
2096 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2097 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2099 PV_STAT(pv_entry_spare--);
2103 /* No free items, allocate another chunk */
2104 m = vm_page_alloc(NULL, colour, (pq == &vm_page_queues[PQ_ACTIVE] ?
2105 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL) | VM_ALLOC_NOOBJ |
2110 PV_STAT(pc_chunk_tryfail++);
2114 * Reclaim pv entries: At first, destroy mappings to inactive
2115 * pages. After that, if a pv chunk entry is still needed,
2116 * destroy mappings to active pages.
2119 PV_STAT(pmap_collect_inactive++);
2120 pq = &vm_page_queues[PQ_INACTIVE];
2121 } else if (pq == &vm_page_queues[PQ_INACTIVE]) {
2122 PV_STAT(pmap_collect_active++);
2123 pq = &vm_page_queues[PQ_ACTIVE];
2125 panic("get_pv_entry: increase vm.pmap.shpgperproc");
2126 pmap_collect(pmap, pq);
2129 PV_STAT(pc_chunk_count++);
2130 PV_STAT(pc_chunk_allocs++);
2132 dump_add_page(m->phys_addr);
2133 pc = (void *)PHYS_TO_DMAP(m->phys_addr);
2135 pc->pc_map[0] = PC_FREE0 & ~1ul; /* preallocated bit 0 */
2136 pc->pc_map[1] = PC_FREE1;
2137 pc->pc_map[2] = PC_FREE2;
2138 pv = &pc->pc_pventry[0];
2139 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2140 PV_STAT(pv_entry_spare += _NPCPV - 1);
2145 * First find and then remove the pv entry for the specified pmap and virtual
2146 * address from the specified pv list. Returns the pv entry if found and NULL
2147 * otherwise. This operation can be performed on pv lists for either 4KB or
2148 * 2MB page mappings.
2150 static __inline pv_entry_t
2151 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2155 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2156 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
2157 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2158 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
2166 * After demotion from a 2MB page mapping to 512 4KB page mappings,
2167 * destroy the pv entry for the 2MB page mapping and reinstantiate the pv
2168 * entries for each of the 4KB page mappings.
2171 pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2173 struct md_page *pvh;
2175 vm_offset_t va_last;
2178 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2179 KASSERT((pa & PDRMASK) == 0,
2180 ("pmap_pv_demote_pde: pa is not 2mpage aligned"));
2183 * Transfer the 2mpage's pv entry for this mapping to the first
2186 pvh = pa_to_pvh(pa);
2187 va = trunc_2mpage(va);
2188 pv = pmap_pvh_remove(pvh, pmap, va);
2189 KASSERT(pv != NULL, ("pmap_pv_demote_pde: pv not found"));
2190 m = PHYS_TO_VM_PAGE(pa);
2191 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2192 /* Instantiate the remaining NPTEPG - 1 pv entries. */
2193 va_last = va + NBPDR - PAGE_SIZE;
2196 KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
2197 ("pmap_pv_demote_pde: page %p is not managed", m));
2199 pmap_insert_entry(pmap, va, m);
2200 } while (va < va_last);
2204 * After promotion from 512 4KB page mappings to a single 2MB page mapping,
2205 * replace the many pv entries for the 4KB page mappings by a single pv entry
2206 * for the 2MB page mapping.
2209 pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2211 struct md_page *pvh;
2213 vm_offset_t va_last;
2216 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2217 KASSERT((pa & PDRMASK) == 0,
2218 ("pmap_pv_promote_pde: pa is not 2mpage aligned"));
2221 * Transfer the first page's pv entry for this mapping to the
2222 * 2mpage's pv list. Aside from avoiding the cost of a call
2223 * to get_pv_entry(), a transfer avoids the possibility that
2224 * get_pv_entry() calls pmap_collect() and that pmap_collect()
2225 * removes one of the mappings that is being promoted.
2227 m = PHYS_TO_VM_PAGE(pa);
2228 va = trunc_2mpage(va);
2229 pv = pmap_pvh_remove(&m->md, pmap, va);
2230 KASSERT(pv != NULL, ("pmap_pv_promote_pde: pv not found"));
2231 pvh = pa_to_pvh(pa);
2232 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_list);
2233 /* Free the remaining NPTEPG - 1 pv entries. */
2234 va_last = va + NBPDR - PAGE_SIZE;
2238 pmap_pvh_free(&m->md, pmap, va);
2239 } while (va < va_last);
2243 * First find and then destroy the pv entry for the specified pmap and virtual
2244 * address. This operation can be performed on pv lists for either 4KB or 2MB
2248 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2252 pv = pmap_pvh_remove(pvh, pmap, va);
2253 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2254 free_pv_entry(pmap, pv);
2258 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
2260 struct md_page *pvh;
2262 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2263 pmap_pvh_free(&m->md, pmap, va);
2264 if (TAILQ_EMPTY(&m->md.pv_list)) {
2265 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2266 if (TAILQ_EMPTY(&pvh->pv_list))
2267 vm_page_flag_clear(m, PG_WRITEABLE);
2272 * Create a pv entry for page at pa for
2276 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2280 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2281 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2282 pv = get_pv_entry(pmap, FALSE);
2284 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2288 * Conditionally create a pv entry.
2291 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2295 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2296 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2297 if (pv_entry_count < pv_entry_high_water &&
2298 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2300 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2307 * Create the pv entry for a 2MB page mapping.
2310 pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2312 struct md_page *pvh;
2315 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2316 if (pv_entry_count < pv_entry_high_water &&
2317 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2319 pvh = pa_to_pvh(pa);
2320 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_list);
2327 * Fills a page table page with mappings to consecutive physical pages.
2330 pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte)
2334 for (pte = firstpte; pte < firstpte + NPTEPG; pte++) {
2336 newpte += PAGE_SIZE;
2341 * Tries to demote a 2MB page mapping. If demotion fails, the 2MB page
2342 * mapping is invalidated.
2345 pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
2347 pd_entry_t newpde, oldpde;
2348 pt_entry_t *firstpte, newpte;
2350 vm_page_t free, mpte;
2352 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2354 KASSERT((oldpde & (PG_PS | PG_V)) == (PG_PS | PG_V),
2355 ("pmap_demote_pde: oldpde is missing PG_PS and/or PG_V"));
2356 mpte = pmap_lookup_pt_page(pmap, va);
2358 pmap_remove_pt_page(pmap, mpte);
2360 KASSERT((oldpde & PG_W) == 0,
2361 ("pmap_demote_pde: page table page for a wired mapping"
2365 * Invalidate the 2MB page mapping and return "failure" if the
2366 * mapping was never accessed or the allocation of the new
2367 * page table page fails. If the 2MB page mapping belongs to
2368 * the direct map region of the kernel's address space, then
2369 * the page allocation request specifies the highest possible
2370 * priority (VM_ALLOC_INTERRUPT). Otherwise, the priority is
2371 * normal. Page table pages are preallocated for every other
2372 * part of the kernel address space, so the direct map region
2373 * is the only part of the kernel address space that must be
2376 if ((oldpde & PG_A) == 0 || (mpte = vm_page_alloc(NULL,
2377 pmap_pde_pindex(va), (va >= DMAP_MIN_ADDRESS && va <
2378 DMAP_MAX_ADDRESS ? VM_ALLOC_INTERRUPT : VM_ALLOC_NORMAL) |
2379 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
2381 pmap_remove_pde(pmap, pde, trunc_2mpage(va), &free);
2382 pmap_invalidate_page(pmap, trunc_2mpage(va));
2383 pmap_free_zero_pages(free);
2384 CTR2(KTR_PMAP, "pmap_demote_pde: failure for va %#lx"
2385 " in pmap %p", va, pmap);
2388 if (va < VM_MAXUSER_ADDRESS)
2389 pmap->pm_stats.resident_count++;
2391 mptepa = VM_PAGE_TO_PHYS(mpte);
2392 firstpte = (pt_entry_t *)PHYS_TO_DMAP(mptepa);
2393 newpde = mptepa | PG_M | PG_A | (oldpde & PG_U) | PG_RW | PG_V;
2394 KASSERT((oldpde & PG_A) != 0,
2395 ("pmap_demote_pde: oldpde is missing PG_A"));
2396 KASSERT((oldpde & (PG_M | PG_RW)) != PG_RW,
2397 ("pmap_demote_pde: oldpde is missing PG_M"));
2398 newpte = oldpde & ~PG_PS;
2399 if ((newpte & PG_PDE_PAT) != 0)
2400 newpte ^= PG_PDE_PAT | PG_PTE_PAT;
2403 * If the page table page is new, initialize it.
2405 if (mpte->wire_count == 1) {
2406 mpte->wire_count = NPTEPG;
2407 pmap_fill_ptp(firstpte, newpte);
2409 KASSERT((*firstpte & PG_FRAME) == (newpte & PG_FRAME),
2410 ("pmap_demote_pde: firstpte and newpte map different physical"
2414 * If the mapping has changed attributes, update the page table
2417 if ((*firstpte & PG_PTE_PROMOTE) != (newpte & PG_PTE_PROMOTE))
2418 pmap_fill_ptp(firstpte, newpte);
2421 * Demote the mapping. This pmap is locked. The old PDE has
2422 * PG_A set. If the old PDE has PG_RW set, it also has PG_M
2423 * set. Thus, there is no danger of a race with another
2424 * processor changing the setting of PG_A and/or PG_M between
2425 * the read above and the store below.
2427 if (workaround_erratum383)
2428 pmap_update_pde(pmap, va, pde, newpde);
2430 pde_store(pde, newpde);
2433 * Invalidate a stale recursive mapping of the page table page.
2435 if (va >= VM_MAXUSER_ADDRESS)
2436 pmap_invalidate_page(pmap, (vm_offset_t)vtopte(va));
2439 * Demote the pv entry. This depends on the earlier demotion
2440 * of the mapping. Specifically, the (re)creation of a per-
2441 * page pv entry might trigger the execution of pmap_collect(),
2442 * which might reclaim a newly (re)created per-page pv entry
2443 * and destroy the associated mapping. In order to destroy
2444 * the mapping, the PDE must have already changed from mapping
2445 * the 2mpage to referencing the page table page.
2447 if ((oldpde & PG_MANAGED) != 0)
2448 pmap_pv_demote_pde(pmap, va, oldpde & PG_PS_FRAME);
2450 pmap_pde_demotions++;
2451 CTR2(KTR_PMAP, "pmap_demote_pde: success for va %#lx"
2452 " in pmap %p", va, pmap);
2457 * pmap_remove_pde: do the things to unmap a superpage in a process
2460 pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
2463 struct md_page *pvh;
2465 vm_offset_t eva, va;
2468 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2469 KASSERT((sva & PDRMASK) == 0,
2470 ("pmap_remove_pde: sva is not 2mpage aligned"));
2471 oldpde = pte_load_clear(pdq);
2473 pmap->pm_stats.wired_count -= NBPDR / PAGE_SIZE;
2476 * Machines that don't support invlpg, also don't support
2480 pmap_invalidate_page(kernel_pmap, sva);
2481 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2482 if (oldpde & PG_MANAGED) {
2483 pvh = pa_to_pvh(oldpde & PG_PS_FRAME);
2484 pmap_pvh_free(pvh, pmap, sva);
2486 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
2487 va < eva; va += PAGE_SIZE, m++) {
2488 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
2491 vm_page_flag_set(m, PG_REFERENCED);
2492 if (TAILQ_EMPTY(&m->md.pv_list) &&
2493 TAILQ_EMPTY(&pvh->pv_list))
2494 vm_page_flag_clear(m, PG_WRITEABLE);
2497 if (pmap == kernel_pmap) {
2498 if (!pmap_demote_pde(pmap, pdq, sva))
2499 panic("pmap_remove_pde: failed demotion");
2501 mpte = pmap_lookup_pt_page(pmap, sva);
2503 pmap_remove_pt_page(pmap, mpte);
2504 pmap->pm_stats.resident_count--;
2505 KASSERT(mpte->wire_count == NPTEPG,
2506 ("pmap_remove_pde: pte page wire count error"));
2507 mpte->wire_count = 0;
2508 pmap_add_delayed_free_list(mpte, free, FALSE);
2509 atomic_subtract_int(&cnt.v_wire_count, 1);
2512 return (pmap_unuse_pt(pmap, sva, *pmap_pdpe(pmap, sva), free));
2516 * pmap_remove_pte: do the things to unmap a page in a process
2519 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va,
2520 pd_entry_t ptepde, vm_page_t *free)
2525 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2526 oldpte = pte_load_clear(ptq);
2528 pmap->pm_stats.wired_count -= 1;
2530 * Machines that don't support invlpg, also don't support
2534 pmap_invalidate_page(kernel_pmap, va);
2535 pmap->pm_stats.resident_count -= 1;
2536 if (oldpte & PG_MANAGED) {
2537 m = PHYS_TO_VM_PAGE(oldpte & PG_FRAME);
2538 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2541 vm_page_flag_set(m, PG_REFERENCED);
2542 pmap_remove_entry(pmap, m, va);
2544 return (pmap_unuse_pt(pmap, va, ptepde, free));
2548 * Remove a single page from a process address space
2551 pmap_remove_page(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, vm_page_t *free)
2555 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2556 if ((*pde & PG_V) == 0)
2558 pte = pmap_pde_to_pte(pde, va);
2559 if ((*pte & PG_V) == 0)
2561 pmap_remove_pte(pmap, pte, va, *pde, free);
2562 pmap_invalidate_page(pmap, va);
2566 * Remove the given range of addresses from the specified map.
2568 * It is assumed that the start and end are properly
2569 * rounded to the page size.
2572 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2574 vm_offset_t va_next;
2575 pml4_entry_t *pml4e;
2577 pd_entry_t ptpaddr, *pde;
2579 vm_page_t free = NULL;
2583 * Perform an unsynchronized read. This is, however, safe.
2585 if (pmap->pm_stats.resident_count == 0)
2590 vm_page_lock_queues();
2594 * special handling of removing one page. a very
2595 * common operation and easy to short circuit some
2598 if (sva + PAGE_SIZE == eva) {
2599 pde = pmap_pde(pmap, sva);
2600 if (pde && (*pde & PG_PS) == 0) {
2601 pmap_remove_page(pmap, sva, pde, &free);
2606 for (; sva < eva; sva = va_next) {
2608 if (pmap->pm_stats.resident_count == 0)
2611 pml4e = pmap_pml4e(pmap, sva);
2612 if ((*pml4e & PG_V) == 0) {
2613 va_next = (sva + NBPML4) & ~PML4MASK;
2619 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2620 if ((*pdpe & PG_V) == 0) {
2621 va_next = (sva + NBPDP) & ~PDPMASK;
2628 * Calculate index for next page table.
2630 va_next = (sva + NBPDR) & ~PDRMASK;
2634 pde = pmap_pdpe_to_pde(pdpe, sva);
2638 * Weed out invalid mappings.
2644 * Check for large page.
2646 if ((ptpaddr & PG_PS) != 0) {
2648 * Are we removing the entire large page? If not,
2649 * demote the mapping and fall through.
2651 if (sva + NBPDR == va_next && eva >= va_next) {
2653 * The TLB entry for a PG_G mapping is
2654 * invalidated by pmap_remove_pde().
2656 if ((ptpaddr & PG_G) == 0)
2658 pmap_remove_pde(pmap, pde, sva, &free);
2660 } else if (!pmap_demote_pde(pmap, pde, sva)) {
2661 /* The large page mapping was destroyed. */
2668 * Limit our scan to either the end of the va represented
2669 * by the current page table page, or to the end of the
2670 * range being removed.
2675 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2681 * The TLB entry for a PG_G mapping is invalidated
2682 * by pmap_remove_pte().
2684 if ((*pte & PG_G) == 0)
2686 if (pmap_remove_pte(pmap, pte, sva, ptpaddr, &free))
2692 pmap_invalidate_all(pmap);
2693 vm_page_unlock_queues();
2695 pmap_free_zero_pages(free);
2699 * Routine: pmap_remove_all
2701 * Removes this physical page from
2702 * all physical maps in which it resides.
2703 * Reflects back modify bits to the pager.
2706 * Original versions of this routine were very
2707 * inefficient because they iteratively called
2708 * pmap_remove (slow...)
2712 pmap_remove_all(vm_page_t m)
2714 struct md_page *pvh;
2717 pt_entry_t *pte, tpte;
2722 KASSERT((m->flags & PG_FICTITIOUS) == 0,
2723 ("pmap_remove_all: page %p is fictitious", m));
2724 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2725 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2726 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
2730 pde = pmap_pde(pmap, va);
2731 (void)pmap_demote_pde(pmap, pde, va);
2734 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2737 pmap->pm_stats.resident_count--;
2738 pde = pmap_pde(pmap, pv->pv_va);
2739 KASSERT((*pde & PG_PS) == 0, ("pmap_remove_all: found"
2740 " a 2mpage in page %p's pv list", m));
2741 pte = pmap_pde_to_pte(pde, pv->pv_va);
2742 tpte = pte_load_clear(pte);
2744 pmap->pm_stats.wired_count--;
2746 vm_page_flag_set(m, PG_REFERENCED);
2749 * Update the vm_page_t clean and reference bits.
2751 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2754 pmap_unuse_pt(pmap, pv->pv_va, *pde, &free);
2755 pmap_invalidate_page(pmap, pv->pv_va);
2756 pmap_free_zero_pages(free);
2757 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2758 free_pv_entry(pmap, pv);
2761 vm_page_flag_clear(m, PG_WRITEABLE);
2765 * pmap_protect_pde: do the things to protect a 2mpage in a process
2768 pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva, vm_prot_t prot)
2770 pd_entry_t newpde, oldpde;
2771 vm_offset_t eva, va;
2773 boolean_t anychanged;
2775 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2776 KASSERT((sva & PDRMASK) == 0,
2777 ("pmap_protect_pde: sva is not 2mpage aligned"));
2780 oldpde = newpde = *pde;
2781 if (oldpde & PG_MANAGED) {
2783 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
2784 va < eva; va += PAGE_SIZE, m++) {
2786 * In contrast to the analogous operation on a 4KB page
2787 * mapping, the mapping's PG_A flag is not cleared and
2788 * the page's PG_REFERENCED flag is not set. The
2789 * reason is that pmap_demote_pde() expects that a 2MB
2790 * page mapping with a stored page table page has PG_A
2793 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
2797 if ((prot & VM_PROT_WRITE) == 0)
2798 newpde &= ~(PG_RW | PG_M);
2799 if ((prot & VM_PROT_EXECUTE) == 0)
2801 if (newpde != oldpde) {
2802 if (!atomic_cmpset_long(pde, oldpde, newpde))
2805 pmap_invalidate_page(pmap, sva);
2809 return (anychanged);
2813 * Set the physical protection on the
2814 * specified range of this map as requested.
2817 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2819 vm_offset_t va_next;
2820 pml4_entry_t *pml4e;
2822 pd_entry_t ptpaddr, *pde;
2826 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2827 pmap_remove(pmap, sva, eva);
2831 if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) ==
2832 (VM_PROT_WRITE|VM_PROT_EXECUTE))
2837 vm_page_lock_queues();
2839 for (; sva < eva; sva = va_next) {
2841 pml4e = pmap_pml4e(pmap, sva);
2842 if ((*pml4e & PG_V) == 0) {
2843 va_next = (sva + NBPML4) & ~PML4MASK;
2849 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2850 if ((*pdpe & PG_V) == 0) {
2851 va_next = (sva + NBPDP) & ~PDPMASK;
2857 va_next = (sva + NBPDR) & ~PDRMASK;
2861 pde = pmap_pdpe_to_pde(pdpe, sva);
2865 * Weed out invalid mappings.
2871 * Check for large page.
2873 if ((ptpaddr & PG_PS) != 0) {
2875 * Are we protecting the entire large page? If not,
2876 * demote the mapping and fall through.
2878 if (sva + NBPDR == va_next && eva >= va_next) {
2880 * The TLB entry for a PG_G mapping is
2881 * invalidated by pmap_protect_pde().
2883 if (pmap_protect_pde(pmap, pde, sva, prot))
2886 } else if (!pmap_demote_pde(pmap, pde, sva)) {
2887 /* The large page mapping was destroyed. */
2895 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2897 pt_entry_t obits, pbits;
2901 obits = pbits = *pte;
2902 if ((pbits & PG_V) == 0)
2904 if (pbits & PG_MANAGED) {
2907 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2908 vm_page_flag_set(m, PG_REFERENCED);
2911 if ((pbits & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
2913 m = PHYS_TO_VM_PAGE(pbits &
2919 if ((prot & VM_PROT_WRITE) == 0)
2920 pbits &= ~(PG_RW | PG_M);
2921 if ((prot & VM_PROT_EXECUTE) == 0)
2924 if (pbits != obits) {
2925 if (!atomic_cmpset_long(pte, obits, pbits))
2928 pmap_invalidate_page(pmap, sva);
2935 pmap_invalidate_all(pmap);
2936 vm_page_unlock_queues();
2941 * Tries to promote the 512, contiguous 4KB page mappings that are within a
2942 * single page table page (PTP) to a single 2MB page mapping. For promotion
2943 * to occur, two conditions must be met: (1) the 4KB page mappings must map
2944 * aligned, contiguous physical memory and (2) the 4KB page mappings must have
2945 * identical characteristics.
2948 pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
2951 pt_entry_t *firstpte, oldpte, pa, *pte;
2952 vm_offset_t oldpteva;
2955 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2958 * Examine the first PTE in the specified PTP. Abort if this PTE is
2959 * either invalid, unused, or does not map the first 4KB physical page
2960 * within a 2MB page.
2962 firstpte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
2965 if ((newpde & ((PG_FRAME & PDRMASK) | PG_A | PG_V)) != (PG_A | PG_V)) {
2966 pmap_pde_p_failures++;
2967 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#lx"
2968 " in pmap %p", va, pmap);
2971 if ((newpde & (PG_M | PG_RW)) == PG_RW) {
2973 * When PG_M is already clear, PG_RW can be cleared without
2974 * a TLB invalidation.
2976 if (!atomic_cmpset_long(firstpte, newpde, newpde & ~PG_RW))
2982 * Examine each of the other PTEs in the specified PTP. Abort if this
2983 * PTE maps an unexpected 4KB physical page or does not have identical
2984 * characteristics to the first PTE.
2986 pa = (newpde & (PG_PS_FRAME | PG_A | PG_V)) + NBPDR - PAGE_SIZE;
2987 for (pte = firstpte + NPTEPG - 1; pte > firstpte; pte--) {
2990 if ((oldpte & (PG_FRAME | PG_A | PG_V)) != pa) {
2991 pmap_pde_p_failures++;
2992 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#lx"
2993 " in pmap %p", va, pmap);
2996 if ((oldpte & (PG_M | PG_RW)) == PG_RW) {
2998 * When PG_M is already clear, PG_RW can be cleared
2999 * without a TLB invalidation.
3001 if (!atomic_cmpset_long(pte, oldpte, oldpte & ~PG_RW))
3004 oldpteva = (oldpte & PG_FRAME & PDRMASK) |
3006 CTR2(KTR_PMAP, "pmap_promote_pde: protect for va %#lx"
3007 " in pmap %p", oldpteva, pmap);
3009 if ((oldpte & PG_PTE_PROMOTE) != (newpde & PG_PTE_PROMOTE)) {
3010 pmap_pde_p_failures++;
3011 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#lx"
3012 " in pmap %p", va, pmap);
3019 * Save the page table page in its current state until the PDE
3020 * mapping the superpage is demoted by pmap_demote_pde() or
3021 * destroyed by pmap_remove_pde().
3023 mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
3024 KASSERT(mpte >= vm_page_array &&
3025 mpte < &vm_page_array[vm_page_array_size],
3026 ("pmap_promote_pde: page table page is out of range"));
3027 KASSERT(mpte->pindex == pmap_pde_pindex(va),
3028 ("pmap_promote_pde: page table page's pindex is wrong"));
3029 pmap_insert_pt_page(pmap, mpte);
3032 * Promote the pv entries.
3034 if ((newpde & PG_MANAGED) != 0)
3035 pmap_pv_promote_pde(pmap, va, newpde & PG_PS_FRAME);
3038 * Propagate the PAT index to its proper position.
3040 if ((newpde & PG_PTE_PAT) != 0)
3041 newpde ^= PG_PDE_PAT | PG_PTE_PAT;
3044 * Map the superpage.
3046 if (workaround_erratum383)
3047 pmap_update_pde(pmap, va, pde, PG_PS | newpde);
3049 pde_store(pde, PG_PS | newpde);
3051 pmap_pde_promotions++;
3052 CTR2(KTR_PMAP, "pmap_promote_pde: success for va %#lx"
3053 " in pmap %p", va, pmap);
3057 * Insert the given physical page (p) at
3058 * the specified virtual address (v) in the
3059 * target physical map with the protection requested.
3061 * If specified, the page will be wired down, meaning
3062 * that the related pte can not be reclaimed.
3064 * NB: This is the only routine which MAY NOT lazy-evaluate
3065 * or lose information. That is, this routine must actually
3066 * insert this page into the given map NOW.
3069 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
3070 vm_prot_t prot, boolean_t wired)
3076 pt_entry_t origpte, newpte;
3080 va = trunc_page(va);
3081 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
3082 KASSERT(va < UPT_MIN_ADDRESS || va >= UPT_MAX_ADDRESS,
3083 ("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va));
3087 vm_page_lock_queues();
3091 * In the case that a page table page is not
3092 * resident, we are creating it here.
3094 if (va < VM_MAXUSER_ADDRESS) {
3095 mpte = pmap_allocpte(pmap, va, M_WAITOK);
3098 pde = pmap_pde(pmap, va);
3099 if (pde != NULL && (*pde & PG_V) != 0) {
3100 if ((*pde & PG_PS) != 0)
3101 panic("pmap_enter: attempted pmap_enter on 2MB page");
3102 pte = pmap_pde_to_pte(pde, va);
3104 panic("pmap_enter: invalid page directory va=%#lx", va);
3106 pa = VM_PAGE_TO_PHYS(m);
3109 opa = origpte & PG_FRAME;
3112 * Mapping has not changed, must be protection or wiring change.
3114 if (origpte && (opa == pa)) {
3116 * Wiring change, just update stats. We don't worry about
3117 * wiring PT pages as they remain resident as long as there
3118 * are valid mappings in them. Hence, if a user page is wired,
3119 * the PT page will be also.
3121 if (wired && ((origpte & PG_W) == 0))
3122 pmap->pm_stats.wired_count++;
3123 else if (!wired && (origpte & PG_W))
3124 pmap->pm_stats.wired_count--;
3127 * Remove extra pte reference
3133 * We might be turning off write access to the page,
3134 * so we go ahead and sense modify status.
3136 if (origpte & PG_MANAGED) {
3143 * Mapping has changed, invalidate old range and fall through to
3144 * handle validating new mapping.
3148 pmap->pm_stats.wired_count--;
3149 if (origpte & PG_MANAGED) {
3150 om = PHYS_TO_VM_PAGE(opa);
3151 pmap_remove_entry(pmap, om, va);
3155 KASSERT(mpte->wire_count > 0,
3156 ("pmap_enter: missing reference to page table page,"
3160 pmap->pm_stats.resident_count++;
3163 * Enter on the PV list if part of our managed memory.
3165 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
3166 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
3167 ("pmap_enter: managed mapping within the clean submap"));
3168 pmap_insert_entry(pmap, va, m);
3173 * Increment counters
3176 pmap->pm_stats.wired_count++;
3180 * Now validate mapping with desired protection/wiring.
3182 newpte = (pt_entry_t)(pa | pmap_cache_bits(m->md.pat_mode, 0) | PG_V);
3183 if ((prot & VM_PROT_WRITE) != 0) {
3185 vm_page_flag_set(m, PG_WRITEABLE);
3187 if ((prot & VM_PROT_EXECUTE) == 0)
3191 if (va < VM_MAXUSER_ADDRESS)
3193 if (pmap == kernel_pmap)
3197 * if the mapping or permission bits are different, we need
3198 * to update the pte.
3200 if ((origpte & ~(PG_M|PG_A)) != newpte) {
3202 if ((access & VM_PROT_WRITE) != 0)
3204 if (origpte & PG_V) {
3206 origpte = pte_load_store(pte, newpte);
3207 if (origpte & PG_A) {
3208 if (origpte & PG_MANAGED)
3209 vm_page_flag_set(om, PG_REFERENCED);
3210 if (opa != VM_PAGE_TO_PHYS(m) || ((origpte &
3211 PG_NX) == 0 && (newpte & PG_NX)))
3214 if ((origpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
3215 if ((origpte & PG_MANAGED) != 0)
3217 if ((newpte & PG_RW) == 0)
3221 pmap_invalidate_page(pmap, va);
3223 pte_store(pte, newpte);
3227 * If both the page table page and the reservation are fully
3228 * populated, then attempt promotion.
3230 if ((mpte == NULL || mpte->wire_count == NPTEPG) &&
3231 pg_ps_enabled && vm_reserv_level_iffullpop(m) == 0)
3232 pmap_promote_pde(pmap, pde, va);
3234 vm_page_unlock_queues();
3239 * Tries to create a 2MB page mapping. Returns TRUE if successful and FALSE
3240 * otherwise. Fails if (1) a page table page cannot be allocated without
3241 * blocking, (2) a mapping already exists at the specified virtual address, or
3242 * (3) a pv entry cannot be allocated without reclaiming another pv entry.
3245 pmap_enter_pde(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3247 pd_entry_t *pde, newpde;
3248 vm_page_t free, mpde;
3250 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3251 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3252 if ((mpde = pmap_allocpde(pmap, va, M_NOWAIT)) == NULL) {
3253 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3254 " in pmap %p", va, pmap);
3257 pde = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mpde));
3258 pde = &pde[pmap_pde_index(va)];
3259 if ((*pde & PG_V) != 0) {
3260 KASSERT(mpde->wire_count > 1,
3261 ("pmap_enter_pde: mpde's wire count is too low"));
3263 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3264 " in pmap %p", va, pmap);
3267 newpde = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.pat_mode, 1) |
3269 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
3270 newpde |= PG_MANAGED;
3273 * Abort this mapping if its PV entry could not be created.
3275 if (!pmap_pv_insert_pde(pmap, va, VM_PAGE_TO_PHYS(m))) {
3277 if (pmap_unwire_pte_hold(pmap, va, mpde, &free)) {
3278 pmap_invalidate_page(pmap, va);
3279 pmap_free_zero_pages(free);
3281 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3282 " in pmap %p", va, pmap);
3286 if ((prot & VM_PROT_EXECUTE) == 0)
3288 if (va < VM_MAXUSER_ADDRESS)
3292 * Increment counters.
3294 pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3297 * Map the superpage.
3299 pde_store(pde, newpde);
3301 pmap_pde_mappings++;
3302 CTR2(KTR_PMAP, "pmap_enter_pde: success for va %#lx"
3303 " in pmap %p", va, pmap);
3308 * Maps a sequence of resident pages belonging to the same object.
3309 * The sequence begins with the given page m_start. This page is
3310 * mapped at the given virtual address start. Each subsequent page is
3311 * mapped at a virtual address that is offset from start by the same
3312 * amount as the page is offset from m_start within the object. The
3313 * last page in the sequence is the page with the largest offset from
3314 * m_start that can be mapped at a virtual address less than the given
3315 * virtual address end. Not every virtual page between start and end
3316 * is mapped; only those for which a resident page exists with the
3317 * corresponding offset from m_start are mapped.
3320 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3321 vm_page_t m_start, vm_prot_t prot)
3325 vm_pindex_t diff, psize;
3327 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
3328 psize = atop(end - start);
3332 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3333 va = start + ptoa(diff);
3334 if ((va & PDRMASK) == 0 && va + NBPDR <= end &&
3335 (VM_PAGE_TO_PHYS(m) & PDRMASK) == 0 &&
3336 pg_ps_enabled && vm_reserv_level_iffullpop(m) == 0 &&
3337 pmap_enter_pde(pmap, va, m, prot))
3338 m = &m[NBPDR / PAGE_SIZE - 1];
3340 mpte = pmap_enter_quick_locked(pmap, va, m, prot,
3342 m = TAILQ_NEXT(m, listq);
3348 * this code makes some *MAJOR* assumptions:
3349 * 1. Current pmap & pmap exists.
3352 * 4. No page table pages.
3353 * but is *MUCH* faster than pmap_enter...
3357 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3361 (void) pmap_enter_quick_locked(pmap, va, m, prot, NULL);
3366 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
3367 vm_prot_t prot, vm_page_t mpte)
3373 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
3374 (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0,
3375 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
3376 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3377 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3380 * In the case that a page table page is not
3381 * resident, we are creating it here.
3383 if (va < VM_MAXUSER_ADDRESS) {
3384 vm_pindex_t ptepindex;
3388 * Calculate pagetable page index
3390 ptepindex = pmap_pde_pindex(va);
3391 if (mpte && (mpte->pindex == ptepindex)) {
3395 * Get the page directory entry
3397 ptepa = pmap_pde(pmap, va);
3400 * If the page table page is mapped, we just increment
3401 * the hold count, and activate it.
3403 if (ptepa && (*ptepa & PG_V) != 0) {
3406 mpte = PHYS_TO_VM_PAGE(*ptepa & PG_FRAME);
3409 mpte = _pmap_allocpte(pmap, ptepindex,
3415 pte = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mpte));
3416 pte = &pte[pmap_pte_index(va)];
3430 * Enter on the PV list if part of our managed memory.
3432 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0 &&
3433 !pmap_try_insert_pv_entry(pmap, va, m)) {
3436 if (pmap_unwire_pte_hold(pmap, va, mpte, &free)) {
3437 pmap_invalidate_page(pmap, va);
3438 pmap_free_zero_pages(free);
3446 * Increment counters
3448 pmap->pm_stats.resident_count++;
3450 pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.pat_mode, 0);
3451 if ((prot & VM_PROT_EXECUTE) == 0)
3455 * Now validate mapping with RO protection
3457 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
3458 pte_store(pte, pa | PG_V | PG_U);
3460 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
3465 * Make a temporary mapping for a physical address. This is only intended
3466 * to be used for panic dumps.
3469 pmap_kenter_temporary(vm_paddr_t pa, int i)
3473 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
3474 pmap_kenter(va, pa);
3476 return ((void *)crashdumpmap);
3480 * This code maps large physical mmap regions into the
3481 * processor address space. Note that some shortcuts
3482 * are taken, but the code works.
3485 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3486 vm_pindex_t pindex, vm_size_t size)
3489 vm_paddr_t pa, ptepa;
3493 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3494 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3495 ("pmap_object_init_pt: non-device object"));
3496 if ((addr & (NBPDR - 1)) == 0 && (size & (NBPDR - 1)) == 0) {
3497 if (!vm_object_populate(object, pindex, pindex + atop(size)))
3499 p = vm_page_lookup(object, pindex);
3500 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3501 ("pmap_object_init_pt: invalid page %p", p));
3502 pat_mode = p->md.pat_mode;
3505 * Abort the mapping if the first page is not physically
3506 * aligned to a 2MB page boundary.
3508 ptepa = VM_PAGE_TO_PHYS(p);
3509 if (ptepa & (NBPDR - 1))
3513 * Skip the first page. Abort the mapping if the rest of
3514 * the pages are not physically contiguous or have differing
3515 * memory attributes.
3517 p = TAILQ_NEXT(p, listq);
3518 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size;
3520 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3521 ("pmap_object_init_pt: invalid page %p", p));
3522 if (pa != VM_PAGE_TO_PHYS(p) ||
3523 pat_mode != p->md.pat_mode)
3525 p = TAILQ_NEXT(p, listq);
3529 * Map using 2MB pages. Since "ptepa" is 2M aligned and
3530 * "size" is a multiple of 2M, adding the PAT setting to "pa"
3531 * will not affect the termination of this loop.
3534 for (pa = ptepa | pmap_cache_bits(pat_mode, 1); pa < ptepa +
3535 size; pa += NBPDR) {
3536 pdpg = pmap_allocpde(pmap, addr, M_NOWAIT);
3539 * The creation of mappings below is only an
3540 * optimization. If a page directory page
3541 * cannot be allocated without blocking,
3542 * continue on to the next mapping rather than
3548 pde = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pdpg));
3549 pde = &pde[pmap_pde_index(addr)];
3550 if ((*pde & PG_V) == 0) {
3551 pde_store(pde, pa | PG_PS | PG_M | PG_A |
3552 PG_U | PG_RW | PG_V);
3553 pmap->pm_stats.resident_count += NBPDR /
3555 pmap_pde_mappings++;
3557 /* Continue on if the PDE is already valid. */
3559 KASSERT(pdpg->wire_count > 0,
3560 ("pmap_object_init_pt: missing reference "
3561 "to page directory page, va: 0x%lx", addr));
3570 * Routine: pmap_change_wiring
3571 * Function: Change the wiring attribute for a map/virtual-address
3573 * In/out conditions:
3574 * The mapping must already exist in the pmap.
3577 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3581 boolean_t are_queues_locked;
3583 are_queues_locked = FALSE;
3586 * Wiring is not a hardware characteristic so there is no need to
3591 pde = pmap_pde(pmap, va);
3592 if ((*pde & PG_PS) != 0) {
3593 if (!wired != ((*pde & PG_W) == 0)) {
3594 if (!are_queues_locked) {
3595 are_queues_locked = TRUE;
3596 if (!mtx_trylock(&vm_page_queue_mtx)) {
3598 vm_page_lock_queues();
3602 if (!pmap_demote_pde(pmap, pde, va))
3603 panic("pmap_change_wiring: demotion failed");
3607 pte = pmap_pde_to_pte(pde, va);
3608 if (wired && (*pte & PG_W) == 0) {
3609 pmap->pm_stats.wired_count++;
3610 atomic_set_long(pte, PG_W);
3611 } else if (!wired && (*pte & PG_W) != 0) {
3612 pmap->pm_stats.wired_count--;
3613 atomic_clear_long(pte, PG_W);
3616 if (are_queues_locked)
3617 vm_page_unlock_queues();
3624 * Copy the range specified by src_addr/len
3625 * from the source map to the range dst_addr/len
3626 * in the destination map.
3628 * This routine is only advisory and need not do anything.
3632 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
3633 vm_offset_t src_addr)
3637 vm_offset_t end_addr = src_addr + len;
3638 vm_offset_t va_next;
3640 if (dst_addr != src_addr)
3643 vm_page_lock_queues();
3644 if (dst_pmap < src_pmap) {
3645 PMAP_LOCK(dst_pmap);
3646 PMAP_LOCK(src_pmap);
3648 PMAP_LOCK(src_pmap);
3649 PMAP_LOCK(dst_pmap);
3651 for (addr = src_addr; addr < end_addr; addr = va_next) {
3652 pt_entry_t *src_pte, *dst_pte;
3653 vm_page_t dstmpde, dstmpte, srcmpte;
3654 pml4_entry_t *pml4e;
3656 pd_entry_t srcptepaddr, *pde;
3658 KASSERT(addr < UPT_MIN_ADDRESS,
3659 ("pmap_copy: invalid to pmap_copy page tables"));
3661 pml4e = pmap_pml4e(src_pmap, addr);
3662 if ((*pml4e & PG_V) == 0) {
3663 va_next = (addr + NBPML4) & ~PML4MASK;
3669 pdpe = pmap_pml4e_to_pdpe(pml4e, addr);
3670 if ((*pdpe & PG_V) == 0) {
3671 va_next = (addr + NBPDP) & ~PDPMASK;
3677 va_next = (addr + NBPDR) & ~PDRMASK;
3681 pde = pmap_pdpe_to_pde(pdpe, addr);
3683 if (srcptepaddr == 0)
3686 if (srcptepaddr & PG_PS) {
3687 dstmpde = pmap_allocpde(dst_pmap, addr, M_NOWAIT);
3688 if (dstmpde == NULL)
3690 pde = (pd_entry_t *)
3691 PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dstmpde));
3692 pde = &pde[pmap_pde_index(addr)];
3693 if (*pde == 0 && ((srcptepaddr & PG_MANAGED) == 0 ||
3694 pmap_pv_insert_pde(dst_pmap, addr, srcptepaddr &
3696 *pde = srcptepaddr & ~PG_W;
3697 dst_pmap->pm_stats.resident_count +=
3700 dstmpde->wire_count--;
3704 srcptepaddr &= PG_FRAME;
3705 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
3706 KASSERT(srcmpte->wire_count > 0,
3707 ("pmap_copy: source page table page is unused"));
3709 if (va_next > end_addr)
3712 src_pte = (pt_entry_t *)PHYS_TO_DMAP(srcptepaddr);
3713 src_pte = &src_pte[pmap_pte_index(addr)];
3715 while (addr < va_next) {
3719 * we only virtual copy managed pages
3721 if ((ptetemp & PG_MANAGED) != 0) {
3722 if (dstmpte != NULL &&
3723 dstmpte->pindex == pmap_pde_pindex(addr))
3724 dstmpte->wire_count++;
3725 else if ((dstmpte = pmap_allocpte(dst_pmap,
3726 addr, M_NOWAIT)) == NULL)
3728 dst_pte = (pt_entry_t *)
3729 PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dstmpte));
3730 dst_pte = &dst_pte[pmap_pte_index(addr)];
3731 if (*dst_pte == 0 &&
3732 pmap_try_insert_pv_entry(dst_pmap, addr,
3733 PHYS_TO_VM_PAGE(ptetemp & PG_FRAME))) {
3735 * Clear the wired, modified, and
3736 * accessed (referenced) bits
3739 *dst_pte = ptetemp & ~(PG_W | PG_M |
3741 dst_pmap->pm_stats.resident_count++;
3744 if (pmap_unwire_pte_hold(dst_pmap,
3745 addr, dstmpte, &free)) {
3746 pmap_invalidate_page(dst_pmap,
3748 pmap_free_zero_pages(free);
3752 if (dstmpte->wire_count >= srcmpte->wire_count)
3760 vm_page_unlock_queues();
3761 PMAP_UNLOCK(src_pmap);
3762 PMAP_UNLOCK(dst_pmap);
3766 * pmap_zero_page zeros the specified hardware page by mapping
3767 * the page into KVM and using bzero to clear its contents.
3770 pmap_zero_page(vm_page_t m)
3772 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3774 pagezero((void *)va);
3778 * pmap_zero_page_area zeros the specified hardware page by mapping
3779 * the page into KVM and using bzero to clear its contents.
3781 * off and size may not cover an area beyond a single hardware page.
3784 pmap_zero_page_area(vm_page_t m, int off, int size)
3786 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3788 if (off == 0 && size == PAGE_SIZE)
3789 pagezero((void *)va);
3791 bzero((char *)va + off, size);
3795 * pmap_zero_page_idle zeros the specified hardware page by mapping
3796 * the page into KVM and using bzero to clear its contents. This
3797 * is intended to be called from the vm_pagezero process only and
3801 pmap_zero_page_idle(vm_page_t m)
3803 vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
3805 pagezero((void *)va);
3809 * pmap_copy_page copies the specified (machine independent)
3810 * page by mapping the page into virtual memory and using
3811 * bcopy to copy the page, one machine dependent page at a
3815 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
3817 vm_offset_t src = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(msrc));
3818 vm_offset_t dst = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mdst));
3820 pagecopy((void *)src, (void *)dst);
3824 * Returns true if the pmap's pv is one of the first
3825 * 16 pvs linked to from this page. This count may
3826 * be changed upwards or downwards in the future; it
3827 * is only necessary that true be returned for a small
3828 * subset of pmaps for proper page aging.
3831 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3833 struct md_page *pvh;
3837 if (m->flags & PG_FICTITIOUS)
3840 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3841 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3842 if (PV_PMAP(pv) == pmap) {
3850 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3851 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
3852 if (PV_PMAP(pv) == pmap)
3863 * pmap_page_wired_mappings:
3865 * Return the number of managed mappings to the given physical page
3869 pmap_page_wired_mappings(vm_page_t m)
3874 if ((m->flags & PG_FICTITIOUS) != 0)
3876 count = pmap_pvh_wired_mappings(&m->md, count);
3877 return (pmap_pvh_wired_mappings(pa_to_pvh(VM_PAGE_TO_PHYS(m)), count));
3881 * pmap_pvh_wired_mappings:
3883 * Return the updated number "count" of managed mappings that are wired.
3886 pmap_pvh_wired_mappings(struct md_page *pvh, int count)
3892 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3893 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
3896 pte = pmap_pte(pmap, pv->pv_va);
3897 if ((*pte & PG_W) != 0)
3905 * Returns TRUE if the given page is mapped individually or as part of
3906 * a 2mpage. Otherwise, returns FALSE.
3909 pmap_page_is_mapped(vm_page_t m)
3911 struct md_page *pvh;
3913 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
3915 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3916 if (TAILQ_EMPTY(&m->md.pv_list)) {
3917 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3918 return (!TAILQ_EMPTY(&pvh->pv_list));
3924 * Remove all pages from specified address space
3925 * this aids process exit speeds. Also, this code
3926 * is special cased for current process only, but
3927 * can have the more generic (and slightly slower)
3928 * mode enabled. This is much faster than pmap_remove
3929 * in the case of running down an entire address space.
3932 pmap_remove_pages(pmap_t pmap)
3935 pt_entry_t *pte, tpte;
3936 vm_page_t free = NULL;
3937 vm_page_t m, mpte, mt;
3939 struct md_page *pvh;
3940 struct pv_chunk *pc, *npc;
3943 uint64_t inuse, bitmask;
3946 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
3947 printf("warning: pmap_remove_pages called with non-current pmap\n");
3950 vm_page_lock_queues();
3952 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
3954 for (field = 0; field < _NPCM; field++) {
3955 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
3956 while (inuse != 0) {
3958 bitmask = 1UL << bit;
3959 idx = field * 64 + bit;
3960 pv = &pc->pc_pventry[idx];
3963 pte = pmap_pdpe(pmap, pv->pv_va);
3965 pte = pmap_pdpe_to_pde(pte, pv->pv_va);
3967 if ((tpte & (PG_PS | PG_V)) == PG_V) {
3969 pte = (pt_entry_t *)PHYS_TO_DMAP(tpte &
3971 pte = &pte[pmap_pte_index(pv->pv_va)];
3972 tpte = *pte & ~PG_PTE_PAT;
3974 if ((tpte & PG_V) == 0)
3978 * We cannot remove wired pages from a process' mapping at this time
3985 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3986 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3987 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
3988 m, (uintmax_t)m->phys_addr,
3991 KASSERT(m < &vm_page_array[vm_page_array_size],
3992 ("pmap_remove_pages: bad tpte %#jx",
3998 * Update the vm_page_t clean/reference bits.
4000 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4001 if ((tpte & PG_PS) != 0) {
4002 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4009 PV_STAT(pv_entry_frees++);
4010 PV_STAT(pv_entry_spare++);
4012 pc->pc_map[field] |= bitmask;
4013 if ((tpte & PG_PS) != 0) {
4014 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
4015 pvh = pa_to_pvh(tpte & PG_PS_FRAME);
4016 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
4017 if (TAILQ_EMPTY(&pvh->pv_list)) {
4018 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4019 if (TAILQ_EMPTY(&mt->md.pv_list))
4020 vm_page_flag_clear(mt, PG_WRITEABLE);
4022 mpte = pmap_lookup_pt_page(pmap, pv->pv_va);
4024 pmap_remove_pt_page(pmap, mpte);
4025 pmap->pm_stats.resident_count--;
4026 KASSERT(mpte->wire_count == NPTEPG,
4027 ("pmap_remove_pages: pte page wire count error"));
4028 mpte->wire_count = 0;
4029 pmap_add_delayed_free_list(mpte, &free, FALSE);
4030 atomic_subtract_int(&cnt.v_wire_count, 1);
4033 pmap->pm_stats.resident_count--;
4034 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
4035 if (TAILQ_EMPTY(&m->md.pv_list)) {
4036 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4037 if (TAILQ_EMPTY(&pvh->pv_list))
4038 vm_page_flag_clear(m, PG_WRITEABLE);
4041 pmap_unuse_pt(pmap, pv->pv_va, ptepde, &free);
4045 PV_STAT(pv_entry_spare -= _NPCPV);
4046 PV_STAT(pc_chunk_count--);
4047 PV_STAT(pc_chunk_frees++);
4048 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4049 m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
4050 dump_drop_page(m->phys_addr);
4051 vm_page_unwire(m, 0);
4055 pmap_invalidate_all(pmap);
4056 vm_page_unlock_queues();
4058 pmap_free_zero_pages(free);
4064 * Return whether or not the specified physical page was modified
4065 * in any physical maps.
4068 pmap_is_modified(vm_page_t m)
4071 if (m->flags & PG_FICTITIOUS)
4073 if (pmap_is_modified_pvh(&m->md))
4075 return (pmap_is_modified_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
4079 * Returns TRUE if any of the given mappings were used to modify
4080 * physical memory. Otherwise, returns FALSE. Both page and 2mpage
4081 * mappings are supported.
4084 pmap_is_modified_pvh(struct md_page *pvh)
4091 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4093 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
4096 pte = pmap_pte(pmap, pv->pv_va);
4097 rv = (*pte & (PG_M | PG_RW)) == (PG_M | PG_RW);
4106 * pmap_is_prefaultable:
4108 * Return whether or not the specified virtual address is elgible
4112 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
4120 pde = pmap_pde(pmap, addr);
4121 if (pde != NULL && (*pde & (PG_PS | PG_V)) == PG_V) {
4122 pte = pmap_pde_to_pte(pde, addr);
4123 rv = (*pte & PG_V) == 0;
4130 * Clear the write and modified bits in each of the given page's mappings.
4133 pmap_remove_write(vm_page_t m)
4135 struct md_page *pvh;
4137 pv_entry_t next_pv, pv;
4139 pt_entry_t oldpte, *pte;
4142 if ((m->flags & PG_FICTITIOUS) != 0 ||
4143 (m->flags & PG_WRITEABLE) == 0)
4145 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4146 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4147 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4151 pde = pmap_pde(pmap, va);
4152 if ((*pde & PG_RW) != 0)
4153 (void)pmap_demote_pde(pmap, pde, va);
4156 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4159 pde = pmap_pde(pmap, pv->pv_va);
4160 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_write: found"
4161 " a 2mpage in page %p's pv list", m));
4162 pte = pmap_pde_to_pte(pde, pv->pv_va);
4165 if (oldpte & PG_RW) {
4166 if (!atomic_cmpset_long(pte, oldpte, oldpte &
4169 if ((oldpte & PG_M) != 0)
4171 pmap_invalidate_page(pmap, pv->pv_va);
4175 vm_page_flag_clear(m, PG_WRITEABLE);
4179 * pmap_ts_referenced:
4181 * Return a count of reference bits for a page, clearing those bits.
4182 * It is not necessary for every reference bit to be cleared, but it
4183 * is necessary that 0 only be returned when there are truly no
4184 * reference bits set.
4186 * XXX: The exact number of bits to check and clear is a matter that
4187 * should be tested and standardized at some point in the future for
4188 * optimal aging of shared pages.
4191 pmap_ts_referenced(vm_page_t m)
4193 struct md_page *pvh;
4194 pv_entry_t pv, pvf, pvn;
4196 pd_entry_t oldpde, *pde;
4201 if (m->flags & PG_FICTITIOUS)
4203 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4204 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4205 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, pvn) {
4209 pde = pmap_pde(pmap, va);
4211 if ((oldpde & PG_A) != 0) {
4212 if (pmap_demote_pde(pmap, pde, va)) {
4213 if ((oldpde & PG_W) == 0) {
4215 * Remove the mapping to a single page
4216 * so that a subsequent access may
4217 * repromote. Since the underlying
4218 * page table page is fully populated,
4219 * this removal never frees a page
4222 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4224 pmap_remove_page(pmap, va, pde, NULL);
4235 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
4238 pvn = TAILQ_NEXT(pv, pv_list);
4239 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
4240 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
4243 pde = pmap_pde(pmap, pv->pv_va);
4244 KASSERT((*pde & PG_PS) == 0, ("pmap_ts_referenced:"
4245 " found a 2mpage in page %p's pv list", m));
4246 pte = pmap_pde_to_pte(pde, pv->pv_va);
4247 if ((*pte & PG_A) != 0) {
4248 atomic_clear_long(pte, PG_A);
4249 pmap_invalidate_page(pmap, pv->pv_va);
4255 } while ((pv = pvn) != NULL && pv != pvf);
4261 * Clear the modify bits on the specified physical page.
4264 pmap_clear_modify(vm_page_t m)
4266 struct md_page *pvh;
4268 pv_entry_t next_pv, pv;
4269 pd_entry_t oldpde, *pde;
4270 pt_entry_t oldpte, *pte;
4273 if ((m->flags & PG_FICTITIOUS) != 0)
4275 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4276 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4277 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4281 pde = pmap_pde(pmap, va);
4283 if ((oldpde & PG_RW) != 0) {
4284 if (pmap_demote_pde(pmap, pde, va)) {
4285 if ((oldpde & PG_W) == 0) {
4287 * Write protect the mapping to a
4288 * single page so that a subsequent
4289 * write access may repromote.
4291 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4293 pte = pmap_pde_to_pte(pde, va);
4295 if ((oldpte & PG_V) != 0) {
4296 while (!atomic_cmpset_long(pte,
4298 oldpte & ~(PG_M | PG_RW)))
4301 pmap_invalidate_page(pmap, va);
4308 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4311 pde = pmap_pde(pmap, pv->pv_va);
4312 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_modify: found"
4313 " a 2mpage in page %p's pv list", m));
4314 pte = pmap_pde_to_pte(pde, pv->pv_va);
4315 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4316 atomic_clear_long(pte, PG_M);
4317 pmap_invalidate_page(pmap, pv->pv_va);
4324 * pmap_clear_reference:
4326 * Clear the reference bit on the specified physical page.
4329 pmap_clear_reference(vm_page_t m)
4331 struct md_page *pvh;
4333 pv_entry_t next_pv, pv;
4334 pd_entry_t oldpde, *pde;
4338 if ((m->flags & PG_FICTITIOUS) != 0)
4340 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4341 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4342 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4346 pde = pmap_pde(pmap, va);
4348 if ((oldpde & PG_A) != 0) {
4349 if (pmap_demote_pde(pmap, pde, va)) {
4351 * Remove the mapping to a single page so
4352 * that a subsequent access may repromote.
4353 * Since the underlying page table page is
4354 * fully populated, this removal never frees
4355 * a page table page.
4357 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4359 pmap_remove_page(pmap, va, pde, NULL);
4364 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4367 pde = pmap_pde(pmap, pv->pv_va);
4368 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_reference: found"
4369 " a 2mpage in page %p's pv list", m));
4370 pte = pmap_pde_to_pte(pde, pv->pv_va);
4372 atomic_clear_long(pte, PG_A);
4373 pmap_invalidate_page(pmap, pv->pv_va);
4380 * Miscellaneous support routines follow
4383 /* Adjust the cache mode for a 4KB page mapped via a PTE. */
4384 static __inline void
4385 pmap_pte_attr(pt_entry_t *pte, int cache_bits)
4390 * The cache mode bits are all in the low 32-bits of the
4391 * PTE, so we can just spin on updating the low 32-bits.
4394 opte = *(u_int *)pte;
4395 npte = opte & ~PG_PTE_CACHE;
4397 } while (npte != opte && !atomic_cmpset_int((u_int *)pte, opte, npte));
4400 /* Adjust the cache mode for a 2MB page mapped via a PDE. */
4401 static __inline void
4402 pmap_pde_attr(pd_entry_t *pde, int cache_bits)
4407 * The cache mode bits are all in the low 32-bits of the
4408 * PDE, so we can just spin on updating the low 32-bits.
4411 opde = *(u_int *)pde;
4412 npde = opde & ~PG_PDE_CACHE;
4414 } while (npde != opde && !atomic_cmpset_int((u_int *)pde, opde, npde));
4418 * Map a set of physical memory pages into the kernel virtual
4419 * address space. Return a pointer to where it is mapped. This
4420 * routine is intended to be used for mapping device memory,
4424 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
4426 vm_offset_t va, offset;
4430 * If the specified range of physical addresses fits within the direct
4431 * map window, use the direct map.
4433 if (pa < dmaplimit && pa + size < dmaplimit) {
4434 va = PHYS_TO_DMAP(pa);
4435 if (!pmap_change_attr(va, size, mode))
4436 return ((void *)va);
4438 offset = pa & PAGE_MASK;
4439 size = roundup(offset + size, PAGE_SIZE);
4440 va = kmem_alloc_nofault(kernel_map, size);
4442 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4443 pa = trunc_page(pa);
4444 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
4445 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
4446 pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
4447 pmap_invalidate_cache_range(va, va + tmpsize);
4448 return ((void *)(va + offset));
4452 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
4455 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
4459 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
4462 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
4466 pmap_unmapdev(vm_offset_t va, vm_size_t size)
4468 vm_offset_t base, offset, tmpva;
4470 /* If we gave a direct map region in pmap_mapdev, do nothing */
4471 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS)
4473 base = trunc_page(va);
4474 offset = va & PAGE_MASK;
4475 size = roundup(offset + size, PAGE_SIZE);
4476 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
4477 pmap_kremove(tmpva);
4478 pmap_invalidate_range(kernel_pmap, va, tmpva);
4479 kmem_free(kernel_map, base, size);
4483 * Tries to demote a 1GB page mapping.
4486 pmap_demote_pdpe(pmap_t pmap, pdp_entry_t *pdpe, vm_offset_t va)
4488 pdp_entry_t newpdpe, oldpdpe;
4489 pd_entry_t *firstpde, newpde, *pde;
4493 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4495 KASSERT((oldpdpe & (PG_PS | PG_V)) == (PG_PS | PG_V),
4496 ("pmap_demote_pdpe: oldpdpe is missing PG_PS and/or PG_V"));
4497 if ((mpde = vm_page_alloc(NULL, va >> PDPSHIFT, VM_ALLOC_INTERRUPT |
4498 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
4499 CTR2(KTR_PMAP, "pmap_demote_pdpe: failure for va %#lx"
4500 " in pmap %p", va, pmap);
4503 mpdepa = VM_PAGE_TO_PHYS(mpde);
4504 firstpde = (pd_entry_t *)PHYS_TO_DMAP(mpdepa);
4505 newpdpe = mpdepa | PG_M | PG_A | (oldpdpe & PG_U) | PG_RW | PG_V;
4506 KASSERT((oldpdpe & PG_A) != 0,
4507 ("pmap_demote_pdpe: oldpdpe is missing PG_A"));
4508 KASSERT((oldpdpe & (PG_M | PG_RW)) != PG_RW,
4509 ("pmap_demote_pdpe: oldpdpe is missing PG_M"));
4513 * Initialize the page directory page.
4515 for (pde = firstpde; pde < firstpde + NPDEPG; pde++) {
4521 * Demote the mapping.
4526 * Invalidate a stale recursive mapping of the page directory page.
4528 pmap_invalidate_page(pmap, (vm_offset_t)vtopde(va));
4530 pmap_pdpe_demotions++;
4531 CTR2(KTR_PMAP, "pmap_demote_pdpe: success for va %#lx"
4532 " in pmap %p", va, pmap);
4537 * Sets the memory attribute for the specified page.
4540 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
4543 m->md.pat_mode = ma;
4546 * If "m" is a normal page, update its direct mapping. This update
4547 * can be relied upon to perform any cache operations that are
4548 * required for data coherence.
4550 if ((m->flags & PG_FICTITIOUS) == 0 &&
4551 pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)), PAGE_SIZE,
4553 panic("memory attribute change on the direct map failed");
4557 * Changes the specified virtual address range's memory type to that given by
4558 * the parameter "mode". The specified virtual address range must be
4559 * completely contained within either the direct map or the kernel map. If
4560 * the virtual address range is contained within the kernel map, then the
4561 * memory type for each of the corresponding ranges of the direct map is also
4562 * changed. (The corresponding ranges of the direct map are those ranges that
4563 * map the same physical pages as the specified virtual address range.) These
4564 * changes to the direct map are necessary because Intel describes the
4565 * behavior of their processors as "undefined" if two or more mappings to the
4566 * same physical page have different memory types.
4568 * Returns zero if the change completed successfully, and either EINVAL or
4569 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
4570 * of the virtual address range was not mapped, and ENOMEM is returned if
4571 * there was insufficient memory available to complete the change. In the
4572 * latter case, the memory type may have been changed on some part of the
4573 * virtual address range or the direct map.
4576 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
4580 PMAP_LOCK(kernel_pmap);
4581 error = pmap_change_attr_locked(va, size, mode);
4582 PMAP_UNLOCK(kernel_pmap);
4587 pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode)
4589 vm_offset_t base, offset, tmpva;
4590 vm_paddr_t pa_start, pa_end;
4594 int cache_bits_pte, cache_bits_pde, error;
4597 PMAP_LOCK_ASSERT(kernel_pmap, MA_OWNED);
4598 base = trunc_page(va);
4599 offset = va & PAGE_MASK;
4600 size = roundup(offset + size, PAGE_SIZE);
4603 * Only supported on kernel virtual addresses, including the direct
4604 * map but excluding the recursive map.
4606 if (base < DMAP_MIN_ADDRESS)
4609 cache_bits_pde = pmap_cache_bits(mode, 1);
4610 cache_bits_pte = pmap_cache_bits(mode, 0);
4614 * Pages that aren't mapped aren't supported. Also break down 2MB pages
4615 * into 4KB pages if required.
4617 for (tmpva = base; tmpva < base + size; ) {
4618 pdpe = pmap_pdpe(kernel_pmap, tmpva);
4621 if (*pdpe & PG_PS) {
4623 * If the current 1GB page already has the required
4624 * memory type, then we need not demote this page. Just
4625 * increment tmpva to the next 1GB page frame.
4627 if ((*pdpe & PG_PDE_CACHE) == cache_bits_pde) {
4628 tmpva = trunc_1gpage(tmpva) + NBPDP;
4633 * If the current offset aligns with a 1GB page frame
4634 * and there is at least 1GB left within the range, then
4635 * we need not break down this page into 2MB pages.
4637 if ((tmpva & PDPMASK) == 0 &&
4638 tmpva + PDPMASK < base + size) {
4642 if (!pmap_demote_pdpe(kernel_pmap, pdpe, tmpva))
4645 pde = pmap_pdpe_to_pde(pdpe, tmpva);
4650 * If the current 2MB page already has the required
4651 * memory type, then we need not demote this page. Just
4652 * increment tmpva to the next 2MB page frame.
4654 if ((*pde & PG_PDE_CACHE) == cache_bits_pde) {
4655 tmpva = trunc_2mpage(tmpva) + NBPDR;
4660 * If the current offset aligns with a 2MB page frame
4661 * and there is at least 2MB left within the range, then
4662 * we need not break down this page into 4KB pages.
4664 if ((tmpva & PDRMASK) == 0 &&
4665 tmpva + PDRMASK < base + size) {
4669 if (!pmap_demote_pde(kernel_pmap, pde, tmpva))
4672 pte = pmap_pde_to_pte(pde, tmpva);
4680 * Ok, all the pages exist, so run through them updating their
4681 * cache mode if required.
4683 pa_start = pa_end = 0;
4684 for (tmpva = base; tmpva < base + size; ) {
4685 pdpe = pmap_pdpe(kernel_pmap, tmpva);
4686 if (*pdpe & PG_PS) {
4687 if ((*pdpe & PG_PDE_CACHE) != cache_bits_pde) {
4688 pmap_pde_attr(pdpe, cache_bits_pde);
4691 if (tmpva >= VM_MIN_KERNEL_ADDRESS) {
4692 if (pa_start == pa_end) {
4693 /* Start physical address run. */
4694 pa_start = *pdpe & PG_PS_FRAME;
4695 pa_end = pa_start + NBPDP;
4696 } else if (pa_end == (*pdpe & PG_PS_FRAME))
4699 /* Run ended, update direct map. */
4700 error = pmap_change_attr_locked(
4701 PHYS_TO_DMAP(pa_start),
4702 pa_end - pa_start, mode);
4705 /* Start physical address run. */
4706 pa_start = *pdpe & PG_PS_FRAME;
4707 pa_end = pa_start + NBPDP;
4710 tmpva = trunc_1gpage(tmpva) + NBPDP;
4713 pde = pmap_pdpe_to_pde(pdpe, tmpva);
4715 if ((*pde & PG_PDE_CACHE) != cache_bits_pde) {
4716 pmap_pde_attr(pde, cache_bits_pde);
4719 if (tmpva >= VM_MIN_KERNEL_ADDRESS) {
4720 if (pa_start == pa_end) {
4721 /* Start physical address run. */
4722 pa_start = *pde & PG_PS_FRAME;
4723 pa_end = pa_start + NBPDR;
4724 } else if (pa_end == (*pde & PG_PS_FRAME))
4727 /* Run ended, update direct map. */
4728 error = pmap_change_attr_locked(
4729 PHYS_TO_DMAP(pa_start),
4730 pa_end - pa_start, mode);
4733 /* Start physical address run. */
4734 pa_start = *pde & PG_PS_FRAME;
4735 pa_end = pa_start + NBPDR;
4738 tmpva = trunc_2mpage(tmpva) + NBPDR;
4740 pte = pmap_pde_to_pte(pde, tmpva);
4741 if ((*pte & PG_PTE_CACHE) != cache_bits_pte) {
4742 pmap_pte_attr(pte, cache_bits_pte);
4745 if (tmpva >= VM_MIN_KERNEL_ADDRESS) {
4746 if (pa_start == pa_end) {
4747 /* Start physical address run. */
4748 pa_start = *pte & PG_FRAME;
4749 pa_end = pa_start + PAGE_SIZE;
4750 } else if (pa_end == (*pte & PG_FRAME))
4751 pa_end += PAGE_SIZE;
4753 /* Run ended, update direct map. */
4754 error = pmap_change_attr_locked(
4755 PHYS_TO_DMAP(pa_start),
4756 pa_end - pa_start, mode);
4759 /* Start physical address run. */
4760 pa_start = *pte & PG_FRAME;
4761 pa_end = pa_start + PAGE_SIZE;
4767 if (error == 0 && pa_start != pa_end)
4768 error = pmap_change_attr_locked(PHYS_TO_DMAP(pa_start),
4769 pa_end - pa_start, mode);
4772 * Flush CPU caches if required to make sure any data isn't cached that
4773 * shouldn't be, etc.
4776 pmap_invalidate_range(kernel_pmap, base, tmpva);
4777 pmap_invalidate_cache_range(base, tmpva);
4783 * perform the pmap work for mincore
4786 pmap_mincore(pmap_t pmap, vm_offset_t addr)
4795 pdep = pmap_pde(pmap, addr);
4796 if (pdep != NULL && (*pdep & PG_V)) {
4797 if (*pdep & PG_PS) {
4799 val = MINCORE_SUPER;
4800 /* Compute the physical address of the 4KB page. */
4801 pa = ((*pdep & PG_PS_FRAME) | (addr & PDRMASK)) &
4804 pte = *pmap_pde_to_pte(pdep, addr);
4805 pa = pte & PG_FRAME;
4814 val |= MINCORE_INCORE;
4815 if ((pte & PG_MANAGED) == 0)
4818 m = PHYS_TO_VM_PAGE(pa);
4823 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
4824 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
4827 * Modified by someone else
4829 vm_page_lock_queues();
4830 if (m->dirty || pmap_is_modified(m))
4831 val |= MINCORE_MODIFIED_OTHER;
4832 vm_page_unlock_queues();
4838 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
4841 * Referenced by someone else
4843 vm_page_lock_queues();
4844 if ((m->flags & PG_REFERENCED) ||
4845 pmap_ts_referenced(m)) {
4846 val |= MINCORE_REFERENCED_OTHER;
4847 vm_page_flag_set(m, PG_REFERENCED);
4849 vm_page_unlock_queues();
4856 pmap_activate(struct thread *td)
4858 pmap_t pmap, oldpmap;
4862 pmap = vmspace_pmap(td->td_proc->p_vmspace);
4863 oldpmap = PCPU_GET(curpmap);
4865 if (oldpmap) /* XXX FIXME */
4866 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask));
4867 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
4869 if (oldpmap) /* XXX FIXME */
4870 oldpmap->pm_active &= ~PCPU_GET(cpumask);
4871 pmap->pm_active |= PCPU_GET(cpumask);
4873 cr3 = DMAP_TO_PHYS((vm_offset_t)pmap->pm_pml4);
4874 td->td_pcb->pcb_cr3 = cr3;
4880 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
4885 * Increase the starting virtual address of the given mapping if a
4886 * different alignment might result in more superpage mappings.
4889 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4890 vm_offset_t *addr, vm_size_t size)
4892 vm_offset_t superpage_offset;
4896 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
4897 offset += ptoa(object->pg_color);
4898 superpage_offset = offset & PDRMASK;
4899 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
4900 (*addr & PDRMASK) == superpage_offset)
4902 if ((*addr & PDRMASK) < superpage_offset)
4903 *addr = (*addr & ~PDRMASK) + superpage_offset;
4905 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;