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) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
11 * This code is derived from software contributed to Berkeley by
12 * the Systems Programming Group of the University of Utah Computer
13 * Science Department and William Jolitz of UUNET Technologies Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
46 * Copyright (c) 2003 Networks Associates Technology, Inc.
47 * All rights reserved.
49 * This software was developed for the FreeBSD Project by Jake Burkholder,
50 * Safeport Network Services, and Network Associates Laboratories, the
51 * Security Research Division of Network Associates, Inc. under
52 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
53 * CHATS research program.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
64 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 #include <sys/cdefs.h>
78 __FBSDID("$FreeBSD$");
81 * Manages physical address maps.
83 * In addition to hardware address maps, this
84 * module is called upon to provide software-use-only
85 * maps which may or may not be stored in the same
86 * form as hardware maps. These pseudo-maps are
87 * used to store intermediate results from copy
88 * operations to and from address spaces.
90 * Since the information managed by this module is
91 * also stored by the logical address mapping module,
92 * this module may throw away valid virtual-to-physical
93 * mappings at almost any time. However, invalidations
94 * of virtual-to-physical mappings must be done as
97 * In order to cope with hardware architectures which
98 * make virtual-to-physical map invalidates expensive,
99 * this module may delay invalidate or reduced protection
100 * operations until such time as they are actually
101 * necessary. This module is given full information as
102 * to which processors are currently using which maps,
103 * and to when physical maps must be made correct.
107 #include "opt_pmap.h"
108 #include "opt_msgbuf.h"
110 #include "opt_xbox.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>
122 #include <sys/sf_buf.h>
124 #include <sys/vmmeter.h>
125 #include <sys/sched.h>
126 #include <sys/sysctl.h>
132 #include <vm/vm_param.h>
133 #include <vm/vm_kern.h>
134 #include <vm/vm_page.h>
135 #include <vm/vm_map.h>
136 #include <vm/vm_object.h>
137 #include <vm/vm_extern.h>
138 #include <vm/vm_pageout.h>
139 #include <vm/vm_pager.h>
140 #include <vm/vm_reserv.h>
143 #include <machine/cpu.h>
144 #include <machine/cputypes.h>
145 #include <machine/md_var.h>
146 #include <machine/pcb.h>
147 #include <machine/specialreg.h>
149 #include <machine/smp.h>
153 #include <machine/xbox.h>
156 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
157 #define CPU_ENABLE_SSE
160 #ifndef PMAP_SHPGPERPROC
161 #define PMAP_SHPGPERPROC 200
164 #if !defined(DIAGNOSTIC)
165 #define PMAP_INLINE __gnu89_inline
172 #define PV_STAT(x) do { x ; } while (0)
174 #define PV_STAT(x) do { } while (0)
177 #define pa_index(pa) ((pa) >> PDRSHIFT)
178 #define pa_to_pvh(pa) (&pv_table[pa_index(pa)])
181 * Get PDEs and PTEs for user/kernel address space
183 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
184 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
186 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
187 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
188 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
189 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
190 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
192 #define pmap_pte_set_w(pte, v) ((v) ? atomic_set_int((u_int *)(pte), PG_W) : \
193 atomic_clear_int((u_int *)(pte), PG_W))
194 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
196 struct pmap kernel_pmap_store;
197 LIST_HEAD(pmaplist, pmap);
198 static struct pmaplist allpmaps;
199 static struct mtx allpmaps_lock;
201 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
202 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
203 int pgeflag = 0; /* PG_G or-in */
204 int pseflag = 0; /* PG_PS or-in */
207 vm_offset_t kernel_vm_end;
208 extern u_int32_t KERNend;
209 extern u_int32_t KPTphys;
213 static uma_zone_t pdptzone;
216 static int pat_works; /* Is page attribute table sane? */
218 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
220 static int pg_ps_enabled;
221 SYSCTL_INT(_vm_pmap, OID_AUTO, pg_ps_enabled, CTLFLAG_RDTUN, &pg_ps_enabled, 0,
222 "Are large page mappings enabled?");
225 * Data for the pv entry allocation mechanism
227 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
228 static struct md_page *pv_table;
229 static int shpgperproc = PMAP_SHPGPERPROC;
231 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
232 int pv_maxchunks; /* How many chunks we have KVA for */
233 vm_offset_t pv_vafree; /* freelist stored in the PTE */
236 * All those kernel PT submaps that BSD is so fond of
245 static struct sysmaps sysmaps_pcpu[MAXCPU];
246 pt_entry_t *CMAP1 = 0, *KPTmap;
247 static pt_entry_t *CMAP3;
248 static pd_entry_t *KPTD;
249 caddr_t CADDR1 = 0, ptvmmap = 0;
250 static caddr_t CADDR3;
251 struct msgbuf *msgbufp = 0;
256 static caddr_t crashdumpmap;
258 static pt_entry_t *PMAP1 = 0, *PMAP2;
259 static pt_entry_t *PADDR1 = 0, *PADDR2;
262 static int PMAP1changedcpu;
263 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
265 "Number of times pmap_pte_quick changed CPU with same PMAP1");
267 static int PMAP1changed;
268 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
270 "Number of times pmap_pte_quick changed PMAP1");
271 static int PMAP1unchanged;
272 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
274 "Number of times pmap_pte_quick didn't change PMAP1");
275 static struct mtx PMAP2mutex;
277 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
278 static pv_entry_t get_pv_entry(pmap_t locked_pmap, int try);
279 static void pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
280 static boolean_t pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
281 static void pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
282 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
283 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
285 static int pmap_pvh_wired_mappings(struct md_page *pvh, int count);
287 static boolean_t pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
288 static boolean_t pmap_enter_pde(pmap_t pmap, vm_offset_t va, vm_page_t m,
290 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
291 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
292 static void pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte);
293 static void pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte);
294 static boolean_t pmap_is_modified_pvh(struct md_page *pvh);
295 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
296 static void pmap_kenter_pde(vm_offset_t va, pd_entry_t newpde);
297 static vm_page_t pmap_lookup_pt_page(pmap_t pmap, vm_offset_t va);
298 static void pmap_pde_attr(pd_entry_t *pde, int cache_bits);
299 static void pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
300 static boolean_t pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva,
302 static void pmap_pte_attr(pt_entry_t *pte, int cache_bits);
303 static void pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
305 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva,
307 static void pmap_remove_pt_page(pmap_t pmap, vm_page_t mpte);
308 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va,
310 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
312 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
313 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
315 static void pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
317 static void pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde);
319 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
321 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags);
322 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free);
323 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
324 static void pmap_pte_release(pt_entry_t *pte);
325 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t *);
326 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
328 static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
331 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
332 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
335 * If you get an error here, then you set KVA_PAGES wrong! See the
336 * description of KVA_PAGES in sys/i386/include/pmap.h. It must be
337 * multiple of 4 for a normal kernel, or a multiple of 8 for a PAE.
339 CTASSERT(KERNBASE % (1 << 24) == 0);
342 * Move the kernel virtual free pointer to the next
343 * 4MB. This is used to help improve performance
344 * by using a large (4MB) page for much of the kernel
345 * (.text, .data, .bss)
348 pmap_kmem_choose(vm_offset_t addr)
350 vm_offset_t newaddr = addr;
353 if (cpu_feature & CPUID_PSE)
354 newaddr = (addr + PDRMASK) & ~PDRMASK;
360 * Bootstrap the system enough to run with virtual memory.
362 * On the i386 this is called after mapping has already been enabled
363 * and just syncs the pmap module with what has already been done.
364 * [We can't call it easily with mapping off since the kernel is not
365 * mapped with PA == VA, hence we would have to relocate every address
366 * from the linked base (virtual) address "KERNBASE" to the actual
367 * (physical) address starting relative to 0]
370 pmap_bootstrap(vm_paddr_t firstaddr)
373 pt_entry_t *pte, *unused;
374 struct sysmaps *sysmaps;
378 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
379 * large. It should instead be correctly calculated in locore.s and
380 * not based on 'first' (which is a physical address, not a virtual
381 * address, for the start of unused physical memory). The kernel
382 * page tables are NOT double mapped and thus should not be included
383 * in this calculation.
385 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
386 virtual_avail = pmap_kmem_choose(virtual_avail);
388 virtual_end = VM_MAX_KERNEL_ADDRESS;
391 * Initialize the kernel pmap (which is statically allocated).
393 PMAP_LOCK_INIT(kernel_pmap);
394 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
396 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
398 kernel_pmap->pm_root = NULL;
399 kernel_pmap->pm_active = -1; /* don't allow deactivation */
400 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
401 LIST_INIT(&allpmaps);
404 * Request a spin mutex so that changes to allpmaps cannot be
405 * preempted by smp_rendezvous_cpus(). Otherwise,
406 * pmap_update_pde_kernel() could access allpmaps while it is
409 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
410 mtx_lock_spin(&allpmaps_lock);
411 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
412 mtx_unlock_spin(&allpmaps_lock);
416 * Reserve some special page table entries/VA space for temporary
419 #define SYSMAP(c, p, v, n) \
420 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
426 * CMAP1/CMAP2 are used for zeroing and copying pages.
427 * CMAP3 is used for the idle process page zeroing.
429 for (i = 0; i < MAXCPU; i++) {
430 sysmaps = &sysmaps_pcpu[i];
431 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
432 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
433 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
435 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
436 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
441 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
444 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
446 SYSMAP(caddr_t, unused, ptvmmap, 1)
449 * msgbufp is used to map the system message buffer.
451 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(MSGBUF_SIZE)))
454 * KPTmap is used by pmap_kextract().
456 SYSMAP(pt_entry_t *, KPTD, KPTmap, KVA_PAGES)
458 for (i = 0; i < NKPT; i++)
459 KPTD[i] = (KPTphys + (i << PAGE_SHIFT)) | pgeflag | PG_RW | PG_V;
462 * Adjust the start of the KPTD and KPTmap so that the implementation
463 * of pmap_kextract() and pmap_growkernel() can be made simpler.
466 KPTmap -= i386_btop(KPTDI << PDRSHIFT);
469 * ptemap is used for pmap_pte_quick
471 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1)
472 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1)
474 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
479 * Leave in place an identity mapping (virt == phys) for the low 1 MB
480 * physical memory region that is used by the ACPI wakeup code. This
481 * mapping must not have PG_G set.
484 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
485 * an early stadium, we cannot yet neatly map video memory ... :-(
486 * Better fixes are very welcome! */
487 if (!arch_i386_is_xbox)
489 for (i = 1; i < NKPT; i++)
492 /* Initialize the PAT MSR if present. */
495 /* Turn on PG_G on kernel page(s) */
507 /* Bail if this CPU doesn't implement PAT. */
508 if (!(cpu_feature & CPUID_PAT))
511 if (cpu_vendor_id != CPU_VENDOR_INTEL ||
512 (CPUID_TO_FAMILY(cpu_id) == 6 && CPUID_TO_MODEL(cpu_id) >= 0xe)) {
514 * Leave the indices 0-3 at the default of WB, WT, UC, and UC-.
515 * Program 4 and 5 as WP and WC.
516 * Leave 6 and 7 as UC and UC-.
518 pat_msr = rdmsr(MSR_PAT);
519 pat_msr &= ~(PAT_MASK(4) | PAT_MASK(5));
520 pat_msr |= PAT_VALUE(4, PAT_WRITE_PROTECTED) |
521 PAT_VALUE(5, PAT_WRITE_COMBINING);
525 * Due to some Intel errata, we can only safely use the lower 4
526 * PAT entries. Thus, just replace PAT Index 2 with WC instead
529 * Intel Pentium III Processor Specification Update
530 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
533 * Intel Pentium IV Processor Specification Update
534 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
536 pat_msr = rdmsr(MSR_PAT);
537 pat_msr &= ~PAT_MASK(2);
538 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
541 wrmsr(MSR_PAT, pat_msr);
545 * Set PG_G on kernel pages. Only the BSP calls this when SMP is turned on.
551 vm_offset_t va, endva;
556 endva = KERNBASE + KERNend;
559 va = KERNBASE + KERNLOAD;
561 pdir_pde(PTD, va) |= pgeflag;
562 invltlb(); /* Play it safe, invltlb() every time */
566 va = (vm_offset_t)btext;
571 invltlb(); /* Play it safe, invltlb() every time */
578 * Initialize a vm_page's machine-dependent fields.
581 pmap_page_init(vm_page_t m)
584 TAILQ_INIT(&m->md.pv_list);
585 m->md.pat_mode = PAT_WRITE_BACK;
590 pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
593 /* Inform UMA that this allocator uses kernel_map/object. */
594 *flags = UMA_SLAB_KERNEL;
595 return ((void *)kmem_alloc_contig(kernel_map, bytes, wait, 0x0ULL,
596 0xffffffffULL, 1, 0, VM_MEMATTR_DEFAULT));
601 * ABuse the pte nodes for unmapped kva to thread a kva freelist through.
603 * - Must deal with pages in order to ensure that none of the PG_* bits
604 * are ever set, PG_V in particular.
605 * - Assumes we can write to ptes without pte_store() atomic ops, even
606 * on PAE systems. This should be ok.
607 * - Assumes nothing will ever test these addresses for 0 to indicate
608 * no mapping instead of correctly checking PG_V.
609 * - Assumes a vm_offset_t will fit in a pte (true for i386).
610 * Because PG_V is never set, there can be no mappings to invalidate.
613 pmap_ptelist_alloc(vm_offset_t *head)
620 return (va); /* Out of memory */
624 panic("pmap_ptelist_alloc: va with PG_V set!");
630 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
635 panic("pmap_ptelist_free: freeing va with PG_V set!");
637 *pte = *head; /* virtual! PG_V is 0 though */
642 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
648 for (i = npages - 1; i >= 0; i--) {
649 va = (vm_offset_t)base + i * PAGE_SIZE;
650 pmap_ptelist_free(head, va);
656 * Initialize the pmap module.
657 * Called by vm_init, to initialize any structures that the pmap
658 * system needs to map virtual memory.
668 * Initialize the vm page array entries for the kernel pmap's
671 for (i = 0; i < NKPT; i++) {
672 mpte = PHYS_TO_VM_PAGE(KPTphys + (i << PAGE_SHIFT));
673 KASSERT(mpte >= vm_page_array &&
674 mpte < &vm_page_array[vm_page_array_size],
675 ("pmap_init: page table page is out of range"));
676 mpte->pindex = i + KPTDI;
677 mpte->phys_addr = KPTphys + (i << PAGE_SHIFT);
681 * Initialize the address space (zone) for the pv entries. Set a
682 * high water mark so that the system can recover from excessive
683 * numbers of pv entries.
685 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
686 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
687 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
688 pv_entry_max = roundup(pv_entry_max, _NPCPV);
689 pv_entry_high_water = 9 * (pv_entry_max / 10);
692 * If the kernel is running in a virtual machine on an AMD Family 10h
693 * processor, then it must assume that MCA is enabled by the virtual
696 if (vm_guest == VM_GUEST_VM && cpu_vendor_id == CPU_VENDOR_AMD &&
697 CPUID_TO_FAMILY(cpu_id) == 0x10)
698 workaround_erratum383 = 1;
701 * Are large page mappings supported and enabled?
703 TUNABLE_INT_FETCH("vm.pmap.pg_ps_enabled", &pg_ps_enabled);
706 else if (pg_ps_enabled) {
707 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
708 ("pmap_init: can't assign to pagesizes[1]"));
709 pagesizes[1] = NBPDR;
713 * Calculate the size of the pv head table for superpages.
715 for (i = 0; phys_avail[i + 1]; i += 2);
716 pv_npg = round_4mpage(phys_avail[(i - 2) + 1]) / NBPDR;
719 * Allocate memory for the pv head table for superpages.
721 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
723 pv_table = (struct md_page *)kmem_alloc(kernel_map, s);
724 for (i = 0; i < pv_npg; i++)
725 TAILQ_INIT(&pv_table[i].pv_list);
727 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
728 pv_chunkbase = (struct pv_chunk *)kmem_alloc_nofault(kernel_map,
729 PAGE_SIZE * pv_maxchunks);
730 if (pv_chunkbase == NULL)
731 panic("pmap_init: not enough kvm for pv chunks");
732 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
734 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
735 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
736 UMA_ZONE_VM | UMA_ZONE_NOFREE);
737 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
742 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
743 "Max number of PV entries");
744 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
745 "Page share factor per proc");
747 SYSCTL_NODE(_vm_pmap, OID_AUTO, pde, CTLFLAG_RD, 0,
748 "2/4MB page mapping counters");
750 static u_long pmap_pde_demotions;
751 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, demotions, CTLFLAG_RD,
752 &pmap_pde_demotions, 0, "2/4MB page demotions");
754 static u_long pmap_pde_mappings;
755 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, mappings, CTLFLAG_RD,
756 &pmap_pde_mappings, 0, "2/4MB page mappings");
758 static u_long pmap_pde_p_failures;
759 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, p_failures, CTLFLAG_RD,
760 &pmap_pde_p_failures, 0, "2/4MB page promotion failures");
762 static u_long pmap_pde_promotions;
763 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, promotions, CTLFLAG_RD,
764 &pmap_pde_promotions, 0, "2/4MB page promotions");
766 /***************************************************
767 * Low level helper routines.....
768 ***************************************************/
771 * Determine the appropriate bits to set in a PTE or PDE for a specified
775 pmap_cache_bits(int mode, boolean_t is_pde)
777 int pat_flag, pat_index, cache_bits;
779 /* The PAT bit is different for PTE's and PDE's. */
780 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
782 /* If we don't support PAT, map extended modes to older ones. */
783 if (!(cpu_feature & CPUID_PAT)) {
785 case PAT_UNCACHEABLE:
786 case PAT_WRITE_THROUGH:
790 case PAT_WRITE_COMBINING:
791 case PAT_WRITE_PROTECTED:
792 mode = PAT_UNCACHEABLE;
797 /* Map the caching mode to a PAT index. */
800 case PAT_UNCACHEABLE:
803 case PAT_WRITE_THROUGH:
812 case PAT_WRITE_COMBINING:
815 case PAT_WRITE_PROTECTED:
819 panic("Unknown caching mode %d\n", mode);
824 case PAT_UNCACHEABLE:
825 case PAT_WRITE_PROTECTED:
828 case PAT_WRITE_THROUGH:
834 case PAT_WRITE_COMBINING:
838 panic("Unknown caching mode %d\n", mode);
842 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
845 cache_bits |= pat_flag;
847 cache_bits |= PG_NC_PCD;
849 cache_bits |= PG_NC_PWT;
854 * The caller is responsible for maintaining TLB consistency.
857 pmap_kenter_pde(vm_offset_t va, pd_entry_t newpde)
861 boolean_t PTD_updated;
864 mtx_lock_spin(&allpmaps_lock);
865 LIST_FOREACH(pmap, &allpmaps, pm_list) {
866 if ((pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] &
869 pde = pmap_pde(pmap, va);
870 pde_store(pde, newpde);
872 mtx_unlock_spin(&allpmaps_lock);
874 ("pmap_kenter_pde: current page table is not in allpmaps"));
878 * After changing the page size for the specified virtual address in the page
879 * table, flush the corresponding entries from the processor's TLB. Only the
880 * calling processor's TLB is affected.
882 * The calling thread must be pinned to a processor.
885 pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde)
889 if ((newpde & PG_PS) == 0)
890 /* Demotion: flush a specific 2MB page mapping. */
892 else if ((newpde & PG_G) == 0)
894 * Promotion: flush every 4KB page mapping from the TLB
895 * because there are too many to flush individually.
900 * Promotion: flush every 4KB page mapping from the TLB,
901 * including any global (PG_G) mappings.
904 load_cr4(cr4 & ~CR4_PGE);
906 * Although preemption at this point could be detrimental to
907 * performance, it would not lead to an error. PG_G is simply
908 * ignored if CR4.PGE is clear. Moreover, in case this block
909 * is re-entered, the load_cr4() either above or below will
910 * modify CR4.PGE flushing the TLB.
912 load_cr4(cr4 | CR4_PGE);
917 * For SMP, these functions have to use the IPI mechanism for coherence.
919 * N.B.: Before calling any of the following TLB invalidation functions,
920 * the calling processor must ensure that all stores updating a non-
921 * kernel page table are globally performed. Otherwise, another
922 * processor could cache an old, pre-update entry without being
923 * invalidated. This can happen one of two ways: (1) The pmap becomes
924 * active on another processor after its pm_active field is checked by
925 * one of the following functions but before a store updating the page
926 * table is globally performed. (2) The pmap becomes active on another
927 * processor before its pm_active field is checked but due to
928 * speculative loads one of the following functions stills reads the
929 * pmap as inactive on the other processor.
931 * The kernel page table is exempt because its pm_active field is
932 * immutable. The kernel page table is always active on every
936 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
942 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
946 cpumask = PCPU_GET(cpumask);
947 other_cpus = PCPU_GET(other_cpus);
948 if (pmap->pm_active & cpumask)
950 if (pmap->pm_active & other_cpus)
951 smp_masked_invlpg(pmap->pm_active & other_cpus, va);
957 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
964 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
965 for (addr = sva; addr < eva; addr += PAGE_SIZE)
967 smp_invlpg_range(sva, eva);
969 cpumask = PCPU_GET(cpumask);
970 other_cpus = PCPU_GET(other_cpus);
971 if (pmap->pm_active & cpumask)
972 for (addr = sva; addr < eva; addr += PAGE_SIZE)
974 if (pmap->pm_active & other_cpus)
975 smp_masked_invlpg_range(pmap->pm_active & other_cpus,
982 pmap_invalidate_all(pmap_t pmap)
988 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
992 cpumask = PCPU_GET(cpumask);
993 other_cpus = PCPU_GET(other_cpus);
994 if (pmap->pm_active & cpumask)
996 if (pmap->pm_active & other_cpus)
997 smp_masked_invltlb(pmap->pm_active & other_cpus);
1003 pmap_invalidate_cache(void)
1013 cpumask_t store; /* processor that updates the PDE */
1014 cpumask_t invalidate; /* processors that invalidate their TLB */
1021 pmap_update_pde_kernel(void *arg)
1023 struct pde_action *act = arg;
1027 if (act->store == PCPU_GET(cpumask))
1029 * Elsewhere, this operation requires allpmaps_lock for
1030 * synchronization. Here, it does not because it is being
1031 * performed in the context of an all_cpus rendezvous.
1033 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1034 pde = pmap_pde(pmap, act->va);
1035 pde_store(pde, act->newpde);
1040 pmap_update_pde_user(void *arg)
1042 struct pde_action *act = arg;
1044 if (act->store == PCPU_GET(cpumask))
1045 pde_store(act->pde, act->newpde);
1049 pmap_update_pde_teardown(void *arg)
1051 struct pde_action *act = arg;
1053 if ((act->invalidate & PCPU_GET(cpumask)) != 0)
1054 pmap_update_pde_invalidate(act->va, act->newpde);
1058 * Change the page size for the specified virtual address in a way that
1059 * prevents any possibility of the TLB ever having two entries that map the
1060 * same virtual address using different page sizes. This is the recommended
1061 * workaround for Erratum 383 on AMD Family 10h processors. It prevents a
1062 * machine check exception for a TLB state that is improperly diagnosed as a
1066 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1068 struct pde_action act;
1069 cpumask_t active, cpumask;
1072 cpumask = PCPU_GET(cpumask);
1073 if (pmap == kernel_pmap)
1076 active = pmap->pm_active;
1077 if ((active & PCPU_GET(other_cpus)) != 0) {
1078 act.store = cpumask;
1079 act.invalidate = active;
1082 act.newpde = newpde;
1083 smp_rendezvous_cpus(cpumask | active,
1084 smp_no_rendevous_barrier, pmap == kernel_pmap ?
1085 pmap_update_pde_kernel : pmap_update_pde_user,
1086 pmap_update_pde_teardown, &act);
1088 if (pmap == kernel_pmap)
1089 pmap_kenter_pde(va, newpde);
1091 pde_store(pde, newpde);
1092 if ((active & cpumask) != 0)
1093 pmap_update_pde_invalidate(va, newpde);
1099 * Normal, non-SMP, 486+ invalidation functions.
1100 * We inline these within pmap.c for speed.
1103 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
1106 if (pmap == kernel_pmap || pmap->pm_active)
1111 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1115 if (pmap == kernel_pmap || pmap->pm_active)
1116 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1121 pmap_invalidate_all(pmap_t pmap)
1124 if (pmap == kernel_pmap || pmap->pm_active)
1129 pmap_invalidate_cache(void)
1136 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1139 if (pmap == kernel_pmap)
1140 pmap_kenter_pde(va, newpde);
1142 pde_store(pde, newpde);
1143 if (pmap == kernel_pmap || pmap->pm_active)
1144 pmap_update_pde_invalidate(va, newpde);
1149 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva)
1152 KASSERT((sva & PAGE_MASK) == 0,
1153 ("pmap_invalidate_cache_range: sva not page-aligned"));
1154 KASSERT((eva & PAGE_MASK) == 0,
1155 ("pmap_invalidate_cache_range: eva not page-aligned"));
1157 if (cpu_feature & CPUID_SS)
1158 ; /* If "Self Snoop" is supported, do nothing. */
1159 else if ((cpu_feature & CPUID_CLFSH) != 0 &&
1160 eva - sva < 2 * 1024 * 1024) {
1163 * Otherwise, do per-cache line flush. Use the mfence
1164 * instruction to insure that previous stores are
1165 * included in the write-back. The processor
1166 * propagates flush to other processors in the cache
1170 for (; sva < eva; sva += cpu_clflush_line_size)
1176 * No targeted cache flush methods are supported by CPU,
1177 * or the supplied range is bigger than 2MB.
1178 * Globally invalidate cache.
1180 pmap_invalidate_cache();
1185 * Are we current address space or kernel? N.B. We return FALSE when
1186 * a pmap's page table is in use because a kernel thread is borrowing
1187 * it. The borrowed page table can change spontaneously, making any
1188 * dependence on its continued use subject to a race condition.
1191 pmap_is_current(pmap_t pmap)
1194 return (pmap == kernel_pmap ||
1195 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
1196 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
1200 * If the given pmap is not the current or kernel pmap, the returned pte must
1201 * be released by passing it to pmap_pte_release().
1204 pmap_pte(pmap_t pmap, vm_offset_t va)
1209 pde = pmap_pde(pmap, va);
1213 /* are we current address space or kernel? */
1214 if (pmap_is_current(pmap))
1215 return (vtopte(va));
1216 mtx_lock(&PMAP2mutex);
1217 newpf = *pde & PG_FRAME;
1218 if ((*PMAP2 & PG_FRAME) != newpf) {
1219 *PMAP2 = newpf | PG_RW | PG_V | PG_A | PG_M;
1220 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR2);
1222 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
1228 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
1231 static __inline void
1232 pmap_pte_release(pt_entry_t *pte)
1235 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2)
1236 mtx_unlock(&PMAP2mutex);
1239 static __inline void
1240 invlcaddr(void *caddr)
1243 invlpg((u_int)caddr);
1247 * Super fast pmap_pte routine best used when scanning
1248 * the pv lists. This eliminates many coarse-grained
1249 * invltlb calls. Note that many of the pv list
1250 * scans are across different pmaps. It is very wasteful
1251 * to do an entire invltlb for checking a single mapping.
1253 * If the given pmap is not the current pmap, vm_page_queue_mtx
1254 * must be held and curthread pinned to a CPU.
1257 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
1262 pde = pmap_pde(pmap, va);
1266 /* are we current address space or kernel? */
1267 if (pmap_is_current(pmap))
1268 return (vtopte(va));
1269 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1270 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1271 newpf = *pde & PG_FRAME;
1272 if ((*PMAP1 & PG_FRAME) != newpf) {
1273 *PMAP1 = newpf | PG_RW | PG_V | PG_A | PG_M;
1275 PMAP1cpu = PCPU_GET(cpuid);
1281 if (PMAP1cpu != PCPU_GET(cpuid)) {
1282 PMAP1cpu = PCPU_GET(cpuid);
1288 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
1294 * Routine: pmap_extract
1296 * Extract the physical page address associated
1297 * with the given map/virtual_address pair.
1300 pmap_extract(pmap_t pmap, vm_offset_t va)
1308 pde = pmap->pm_pdir[va >> PDRSHIFT];
1310 if ((pde & PG_PS) != 0)
1311 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
1313 pte = pmap_pte(pmap, va);
1314 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1315 pmap_pte_release(pte);
1323 * Routine: pmap_extract_and_hold
1325 * Atomically extract and hold the physical page
1326 * with the given pmap and virtual address pair
1327 * if that mapping permits the given protection.
1330 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1337 vm_page_lock_queues();
1339 pde = *pmap_pde(pmap, va);
1342 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1343 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
1349 pte = *pmap_pte_quick(pmap, va);
1351 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1352 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1358 vm_page_unlock_queues();
1363 /***************************************************
1364 * Low level mapping routines.....
1365 ***************************************************/
1368 * Add a wired page to the kva.
1369 * Note: not SMP coherent.
1372 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1377 pte_store(pte, pa | PG_RW | PG_V | pgeflag);
1380 static __inline void
1381 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1386 pte_store(pte, pa | PG_RW | PG_V | pgeflag | pmap_cache_bits(mode, 0));
1390 * Remove a page from the kernel pagetables.
1391 * Note: not SMP coherent.
1394 pmap_kremove(vm_offset_t va)
1403 * Used to map a range of physical addresses into kernel
1404 * virtual address space.
1406 * The value passed in '*virt' is a suggested virtual address for
1407 * the mapping. Architectures which can support a direct-mapped
1408 * physical to virtual region can return the appropriate address
1409 * within that region, leaving '*virt' unchanged. Other
1410 * architectures should map the pages starting at '*virt' and
1411 * update '*virt' with the first usable address after the mapped
1415 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1417 vm_offset_t va, sva;
1420 while (start < end) {
1421 pmap_kenter(va, start);
1425 pmap_invalidate_range(kernel_pmap, sva, va);
1432 * Add a list of wired pages to the kva
1433 * this routine is only used for temporary
1434 * kernel mappings that do not need to have
1435 * page modification or references recorded.
1436 * Note that old mappings are simply written
1437 * over. The page *must* be wired.
1438 * Note: SMP coherent. Uses a ranged shootdown IPI.
1441 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1443 pt_entry_t *endpte, oldpte, pa, *pte;
1448 endpte = pte + count;
1449 while (pte < endpte) {
1451 pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.pat_mode, 0);
1452 if ((*pte & (PG_FRAME | PG_PTE_CACHE)) != pa) {
1454 pte_store(pte, pa | pgeflag | PG_RW | PG_V);
1458 if (__predict_false((oldpte & PG_V) != 0))
1459 pmap_invalidate_range(kernel_pmap, sva, sva + count *
1464 * This routine tears out page mappings from the
1465 * kernel -- it is meant only for temporary mappings.
1466 * Note: SMP coherent. Uses a ranged shootdown IPI.
1469 pmap_qremove(vm_offset_t sva, int count)
1474 while (count-- > 0) {
1478 pmap_invalidate_range(kernel_pmap, sva, va);
1481 /***************************************************
1482 * Page table page management routines.....
1483 ***************************************************/
1484 static __inline void
1485 pmap_free_zero_pages(vm_page_t free)
1489 while (free != NULL) {
1492 /* Preserve the page's PG_ZERO setting. */
1493 vm_page_free_toq(m);
1498 * Schedule the specified unused page table page to be freed. Specifically,
1499 * add the page to the specified list of pages that will be released to the
1500 * physical memory manager after the TLB has been updated.
1502 static __inline void
1503 pmap_add_delayed_free_list(vm_page_t m, vm_page_t *free, boolean_t set_PG_ZERO)
1507 m->flags |= PG_ZERO;
1509 m->flags &= ~PG_ZERO;
1515 * Inserts the specified page table page into the specified pmap's collection
1516 * of idle page table pages. Each of a pmap's page table pages is responsible
1517 * for mapping a distinct range of virtual addresses. The pmap's collection is
1518 * ordered by this virtual address range.
1521 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte)
1525 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1526 root = pmap->pm_root;
1531 root = vm_page_splay(mpte->pindex, root);
1532 if (mpte->pindex < root->pindex) {
1533 mpte->left = root->left;
1536 } else if (mpte->pindex == root->pindex)
1537 panic("pmap_insert_pt_page: pindex already inserted");
1539 mpte->right = root->right;
1544 pmap->pm_root = mpte;
1548 * Looks for a page table page mapping the specified virtual address in the
1549 * specified pmap's collection of idle page table pages. Returns NULL if there
1550 * is no page table page corresponding to the specified virtual address.
1553 pmap_lookup_pt_page(pmap_t pmap, vm_offset_t va)
1556 vm_pindex_t pindex = va >> PDRSHIFT;
1558 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1559 if ((mpte = pmap->pm_root) != NULL && mpte->pindex != pindex) {
1560 mpte = vm_page_splay(pindex, mpte);
1561 if ((pmap->pm_root = mpte)->pindex != pindex)
1568 * Removes the specified page table page from the specified pmap's collection
1569 * of idle page table pages. The specified page table page must be a member of
1570 * the pmap's collection.
1573 pmap_remove_pt_page(pmap_t pmap, vm_page_t mpte)
1577 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1578 if (mpte != pmap->pm_root)
1579 vm_page_splay(mpte->pindex, pmap->pm_root);
1580 if (mpte->left == NULL)
1583 root = vm_page_splay(mpte->pindex, mpte->left);
1584 root->right = mpte->right;
1586 pmap->pm_root = root;
1590 * This routine unholds page table pages, and if the hold count
1591 * drops to zero, then it decrements the wire count.
1594 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free)
1598 if (m->wire_count == 0)
1599 return _pmap_unwire_pte_hold(pmap, m, free);
1605 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free)
1610 * unmap the page table page
1612 pmap->pm_pdir[m->pindex] = 0;
1613 --pmap->pm_stats.resident_count;
1616 * This is a release store so that the ordinary store unmapping
1617 * the page table page is globally performed before TLB shoot-
1620 atomic_subtract_rel_int(&cnt.v_wire_count, 1);
1623 * Do an invltlb to make the invalidated mapping
1624 * take effect immediately.
1626 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1627 pmap_invalidate_page(pmap, pteva);
1630 * Put page on a list so that it is released after
1631 * *ALL* TLB shootdown is done
1633 pmap_add_delayed_free_list(m, free, TRUE);
1639 * After removing a page table entry, this routine is used to
1640 * conditionally free the page, and manage the hold/wire counts.
1643 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t *free)
1648 if (va >= VM_MAXUSER_ADDRESS)
1650 ptepde = *pmap_pde(pmap, va);
1651 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1652 return pmap_unwire_pte_hold(pmap, mpte, free);
1656 pmap_pinit0(pmap_t pmap)
1659 PMAP_LOCK_INIT(pmap);
1660 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1662 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1664 pmap->pm_root = NULL;
1665 pmap->pm_active = 0;
1666 PCPU_SET(curpmap, pmap);
1667 TAILQ_INIT(&pmap->pm_pvchunk);
1668 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1669 mtx_lock_spin(&allpmaps_lock);
1670 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1671 mtx_unlock_spin(&allpmaps_lock);
1675 * Initialize a preallocated and zeroed pmap structure,
1676 * such as one in a vmspace structure.
1679 pmap_pinit(pmap_t pmap)
1681 vm_page_t m, ptdpg[NPGPTD];
1686 PMAP_LOCK_INIT(pmap);
1689 * No need to allocate page table space yet but we do need a valid
1690 * page directory table.
1692 if (pmap->pm_pdir == NULL) {
1693 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map,
1696 if (pmap->pm_pdir == NULL) {
1697 PMAP_LOCK_DESTROY(pmap);
1701 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
1702 KASSERT(((vm_offset_t)pmap->pm_pdpt &
1703 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
1704 ("pmap_pinit: pdpt misaligned"));
1705 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
1706 ("pmap_pinit: pdpt above 4g"));
1708 pmap->pm_root = NULL;
1710 KASSERT(pmap->pm_root == NULL,
1711 ("pmap_pinit: pmap has reserved page table page(s)"));
1714 * allocate the page directory page(s)
1716 for (i = 0; i < NPGPTD;) {
1717 m = vm_page_alloc(NULL, color++,
1718 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1727 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1729 for (i = 0; i < NPGPTD; i++) {
1730 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1731 bzero(pmap->pm_pdir + (i * NPDEPG), PAGE_SIZE);
1734 mtx_lock_spin(&allpmaps_lock);
1735 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1736 mtx_unlock_spin(&allpmaps_lock);
1737 /* Wire in kernel global address entries. */
1738 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1740 /* install self-referential address mapping entry(s) */
1741 for (i = 0; i < NPGPTD; i++) {
1742 pa = VM_PAGE_TO_PHYS(ptdpg[i]);
1743 pmap->pm_pdir[PTDPTDI + i] = pa | PG_V | PG_RW | PG_A | PG_M;
1745 pmap->pm_pdpt[i] = pa | PG_V;
1749 pmap->pm_active = 0;
1750 TAILQ_INIT(&pmap->pm_pvchunk);
1751 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1757 * this routine is called if the page table page is not
1761 _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags)
1766 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1767 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1768 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1771 * Allocate a page table page.
1773 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1774 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1775 if (flags & M_WAITOK) {
1777 vm_page_unlock_queues();
1779 vm_page_lock_queues();
1784 * Indicate the need to retry. While waiting, the page table
1785 * page may have been allocated.
1789 if ((m->flags & PG_ZERO) == 0)
1793 * Map the pagetable page into the process address space, if
1794 * it isn't already there.
1797 pmap->pm_stats.resident_count++;
1799 ptepa = VM_PAGE_TO_PHYS(m);
1800 pmap->pm_pdir[ptepindex] =
1801 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1807 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1813 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1814 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1815 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1818 * Calculate pagetable page index
1820 ptepindex = va >> PDRSHIFT;
1823 * Get the page directory entry
1825 ptepa = pmap->pm_pdir[ptepindex];
1828 * This supports switching from a 4MB page to a
1831 if (ptepa & PG_PS) {
1832 (void)pmap_demote_pde(pmap, &pmap->pm_pdir[ptepindex], va);
1833 ptepa = pmap->pm_pdir[ptepindex];
1837 * If the page table page is mapped, we just increment the
1838 * hold count, and activate it.
1841 m = PHYS_TO_VM_PAGE(ptepa & PG_FRAME);
1845 * Here if the pte page isn't mapped, or if it has
1848 m = _pmap_allocpte(pmap, ptepindex, flags);
1849 if (m == NULL && (flags & M_WAITOK))
1856 /***************************************************
1857 * Pmap allocation/deallocation routines.
1858 ***************************************************/
1862 * Deal with a SMP shootdown of other users of the pmap that we are
1863 * trying to dispose of. This can be a bit hairy.
1865 static cpumask_t *lazymask;
1866 static u_int lazyptd;
1867 static volatile u_int lazywait;
1869 void pmap_lazyfix_action(void);
1872 pmap_lazyfix_action(void)
1874 cpumask_t mymask = PCPU_GET(cpumask);
1877 (*ipi_lazypmap_counts[PCPU_GET(cpuid)])++;
1879 if (rcr3() == lazyptd)
1880 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1881 atomic_clear_int(lazymask, mymask);
1882 atomic_store_rel_int(&lazywait, 1);
1886 pmap_lazyfix_self(cpumask_t mymask)
1889 if (rcr3() == lazyptd)
1890 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1891 atomic_clear_int(lazymask, mymask);
1896 pmap_lazyfix(pmap_t pmap)
1898 cpumask_t mymask, mask;
1901 while ((mask = pmap->pm_active) != 0) {
1903 mask = mask & -mask; /* Find least significant set bit */
1904 mtx_lock_spin(&smp_ipi_mtx);
1906 lazyptd = vtophys(pmap->pm_pdpt);
1908 lazyptd = vtophys(pmap->pm_pdir);
1910 mymask = PCPU_GET(cpumask);
1911 if (mask == mymask) {
1912 lazymask = &pmap->pm_active;
1913 pmap_lazyfix_self(mymask);
1915 atomic_store_rel_int((u_int *)&lazymask,
1916 (u_int)&pmap->pm_active);
1917 atomic_store_rel_int(&lazywait, 0);
1918 ipi_selected(mask, IPI_LAZYPMAP);
1919 while (lazywait == 0) {
1925 mtx_unlock_spin(&smp_ipi_mtx);
1927 printf("pmap_lazyfix: spun for 50000000\n");
1934 * Cleaning up on uniprocessor is easy. For various reasons, we're
1935 * unlikely to have to even execute this code, including the fact
1936 * that the cleanup is deferred until the parent does a wait(2), which
1937 * means that another userland process has run.
1940 pmap_lazyfix(pmap_t pmap)
1944 cr3 = vtophys(pmap->pm_pdir);
1945 if (cr3 == rcr3()) {
1946 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1947 pmap->pm_active &= ~(PCPU_GET(cpumask));
1953 * Release any resources held by the given physical map.
1954 * Called when a pmap initialized by pmap_pinit is being released.
1955 * Should only be called if the map contains no valid mappings.
1958 pmap_release(pmap_t pmap)
1960 vm_page_t m, ptdpg[NPGPTD];
1963 KASSERT(pmap->pm_stats.resident_count == 0,
1964 ("pmap_release: pmap resident count %ld != 0",
1965 pmap->pm_stats.resident_count));
1966 KASSERT(pmap->pm_root == NULL,
1967 ("pmap_release: pmap has reserved page table page(s)"));
1970 mtx_lock_spin(&allpmaps_lock);
1971 LIST_REMOVE(pmap, pm_list);
1972 mtx_unlock_spin(&allpmaps_lock);
1974 for (i = 0; i < NPGPTD; i++)
1975 ptdpg[i] = PHYS_TO_VM_PAGE(pmap->pm_pdir[PTDPTDI + i] &
1978 bzero(pmap->pm_pdir + PTDPTDI, (nkpt + NPGPTD) *
1979 sizeof(*pmap->pm_pdir));
1981 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1983 for (i = 0; i < NPGPTD; i++) {
1986 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME),
1987 ("pmap_release: got wrong ptd page"));
1990 atomic_subtract_int(&cnt.v_wire_count, 1);
1991 vm_page_free_zero(m);
1993 PMAP_LOCK_DESTROY(pmap);
1997 kvm_size(SYSCTL_HANDLER_ARGS)
1999 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
2001 return sysctl_handle_long(oidp, &ksize, 0, req);
2003 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
2004 0, 0, kvm_size, "IU", "Size of KVM");
2007 kvm_free(SYSCTL_HANDLER_ARGS)
2009 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
2011 return sysctl_handle_long(oidp, &kfree, 0, req);
2013 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
2014 0, 0, kvm_free, "IU", "Amount of KVM free");
2017 * grow the number of kernel page table entries, if needed
2020 pmap_growkernel(vm_offset_t addr)
2022 vm_paddr_t ptppaddr;
2026 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2027 if (kernel_vm_end == 0) {
2028 kernel_vm_end = KERNBASE;
2030 while (pdir_pde(PTD, kernel_vm_end)) {
2031 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
2033 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2034 kernel_vm_end = kernel_map->max_offset;
2039 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
2040 if (addr - 1 >= kernel_map->max_offset)
2041 addr = kernel_map->max_offset;
2042 while (kernel_vm_end < addr) {
2043 if (pdir_pde(PTD, kernel_vm_end)) {
2044 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
2045 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2046 kernel_vm_end = kernel_map->max_offset;
2052 nkpg = vm_page_alloc(NULL, kernel_vm_end >> PDRSHIFT,
2053 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
2056 panic("pmap_growkernel: no memory to grow kernel");
2060 if ((nkpg->flags & PG_ZERO) == 0)
2061 pmap_zero_page(nkpg);
2062 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2063 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2064 pdir_pde(KPTD, kernel_vm_end) = pgeflag | newpdir;
2066 pmap_kenter_pde(kernel_vm_end, newpdir);
2067 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
2068 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2069 kernel_vm_end = kernel_map->max_offset;
2076 /***************************************************
2077 * page management routines.
2078 ***************************************************/
2080 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
2081 CTASSERT(_NPCM == 11);
2083 static __inline struct pv_chunk *
2084 pv_to_chunk(pv_entry_t pv)
2087 return (struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK);
2090 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
2092 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
2093 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
2095 static uint32_t pc_freemask[11] = {
2096 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2097 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2098 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2099 PC_FREE0_9, PC_FREE10
2102 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
2103 "Current number of pv entries");
2106 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2108 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
2109 "Current number of pv entry chunks");
2110 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
2111 "Current number of pv entry chunks allocated");
2112 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
2113 "Current number of pv entry chunks frees");
2114 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
2115 "Number of times tried to get a chunk page but failed.");
2117 static long pv_entry_frees, pv_entry_allocs;
2118 static int pv_entry_spare;
2120 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
2121 "Current number of pv entry frees");
2122 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
2123 "Current number of pv entry allocs");
2124 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
2125 "Current number of spare pv entries");
2127 static int pmap_collect_inactive, pmap_collect_active;
2129 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_inactive, CTLFLAG_RD, &pmap_collect_inactive, 0,
2130 "Current number times pmap_collect called on inactive queue");
2131 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_active, CTLFLAG_RD, &pmap_collect_active, 0,
2132 "Current number times pmap_collect called on active queue");
2136 * We are in a serious low memory condition. Resort to
2137 * drastic measures to free some pages so we can allocate
2138 * another pv entry chunk. This is normally called to
2139 * unmap inactive pages, and if necessary, active pages.
2142 pmap_collect(pmap_t locked_pmap, struct vpgqueues *vpq)
2144 struct md_page *pvh;
2147 pt_entry_t *pte, tpte;
2148 pv_entry_t next_pv, pv;
2153 TAILQ_FOREACH(m, &vpq->pl, pageq) {
2154 if (m->hold_count || m->busy)
2156 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
2159 /* Avoid deadlock and lock recursion. */
2160 if (pmap > locked_pmap)
2162 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
2164 pmap->pm_stats.resident_count--;
2165 pde = pmap_pde(pmap, va);
2166 KASSERT((*pde & PG_PS) == 0, ("pmap_collect: found"
2167 " a 4mpage in page %p's pv list", m));
2168 pte = pmap_pte_quick(pmap, va);
2169 tpte = pte_load_clear(pte);
2170 KASSERT((tpte & PG_W) == 0,
2171 ("pmap_collect: wired pte %#jx", (uintmax_t)tpte));
2173 vm_page_flag_set(m, PG_REFERENCED);
2174 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2177 pmap_unuse_pt(pmap, va, &free);
2178 pmap_invalidate_page(pmap, va);
2179 pmap_free_zero_pages(free);
2180 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2181 if (TAILQ_EMPTY(&m->md.pv_list)) {
2182 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2183 if (TAILQ_EMPTY(&pvh->pv_list))
2184 vm_page_flag_clear(m, PG_WRITEABLE);
2186 free_pv_entry(pmap, pv);
2187 if (pmap != locked_pmap)
2196 * free the pv_entry back to the free list
2199 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2202 struct pv_chunk *pc;
2203 int idx, field, bit;
2205 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2206 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2207 PV_STAT(pv_entry_frees++);
2208 PV_STAT(pv_entry_spare++);
2210 pc = pv_to_chunk(pv);
2211 idx = pv - &pc->pc_pventry[0];
2214 pc->pc_map[field] |= 1ul << bit;
2215 /* move to head of list */
2216 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2217 for (idx = 0; idx < _NPCM; idx++)
2218 if (pc->pc_map[idx] != pc_freemask[idx]) {
2219 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2222 PV_STAT(pv_entry_spare -= _NPCPV);
2223 PV_STAT(pc_chunk_count--);
2224 PV_STAT(pc_chunk_frees++);
2225 /* entire chunk is free, return it */
2226 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2227 pmap_qremove((vm_offset_t)pc, 1);
2228 vm_page_unwire(m, 0);
2230 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2234 * get a new pv_entry, allocating a block from the system
2238 get_pv_entry(pmap_t pmap, int try)
2240 static const struct timeval printinterval = { 60, 0 };
2241 static struct timeval lastprint;
2242 static vm_pindex_t colour;
2243 struct vpgqueues *pq;
2246 struct pv_chunk *pc;
2249 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2250 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2251 PV_STAT(pv_entry_allocs++);
2253 if (pv_entry_count > pv_entry_high_water)
2254 if (ratecheck(&lastprint, &printinterval))
2255 printf("Approaching the limit on PV entries, consider "
2256 "increasing either the vm.pmap.shpgperproc or the "
2257 "vm.pmap.pv_entry_max tunable.\n");
2260 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2262 for (field = 0; field < _NPCM; field++) {
2263 if (pc->pc_map[field]) {
2264 bit = bsfl(pc->pc_map[field]);
2268 if (field < _NPCM) {
2269 pv = &pc->pc_pventry[field * 32 + bit];
2270 pc->pc_map[field] &= ~(1ul << bit);
2271 /* If this was the last item, move it to tail */
2272 for (field = 0; field < _NPCM; field++)
2273 if (pc->pc_map[field] != 0) {
2274 PV_STAT(pv_entry_spare--);
2275 return (pv); /* not full, return */
2277 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2278 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2279 PV_STAT(pv_entry_spare--);
2284 * Access to the ptelist "pv_vafree" is synchronized by the page
2285 * queues lock. If "pv_vafree" is currently non-empty, it will
2286 * remain non-empty until pmap_ptelist_alloc() completes.
2288 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, colour, (pq ==
2289 &vm_page_queues[PQ_ACTIVE] ? VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL) |
2290 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
2293 PV_STAT(pc_chunk_tryfail++);
2297 * Reclaim pv entries: At first, destroy mappings to
2298 * inactive pages. After that, if a pv chunk entry
2299 * is still needed, destroy mappings to active pages.
2302 PV_STAT(pmap_collect_inactive++);
2303 pq = &vm_page_queues[PQ_INACTIVE];
2304 } else if (pq == &vm_page_queues[PQ_INACTIVE]) {
2305 PV_STAT(pmap_collect_active++);
2306 pq = &vm_page_queues[PQ_ACTIVE];
2308 panic("get_pv_entry: increase vm.pmap.shpgperproc");
2309 pmap_collect(pmap, pq);
2312 PV_STAT(pc_chunk_count++);
2313 PV_STAT(pc_chunk_allocs++);
2315 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
2316 pmap_qenter((vm_offset_t)pc, &m, 1);
2318 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
2319 for (field = 1; field < _NPCM; field++)
2320 pc->pc_map[field] = pc_freemask[field];
2321 pv = &pc->pc_pventry[0];
2322 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2323 PV_STAT(pv_entry_spare += _NPCPV - 1);
2327 static __inline pv_entry_t
2328 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2332 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2333 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
2334 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2335 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
2343 pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2345 struct md_page *pvh;
2347 vm_offset_t va_last;
2350 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2351 KASSERT((pa & PDRMASK) == 0,
2352 ("pmap_pv_demote_pde: pa is not 4mpage aligned"));
2355 * Transfer the 4mpage's pv entry for this mapping to the first
2358 pvh = pa_to_pvh(pa);
2359 va = trunc_4mpage(va);
2360 pv = pmap_pvh_remove(pvh, pmap, va);
2361 KASSERT(pv != NULL, ("pmap_pv_demote_pde: pv not found"));
2362 m = PHYS_TO_VM_PAGE(pa);
2363 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2364 /* Instantiate the remaining NPTEPG - 1 pv entries. */
2365 va_last = va + NBPDR - PAGE_SIZE;
2368 KASSERT((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0,
2369 ("pmap_pv_demote_pde: page %p is not managed", m));
2371 pmap_insert_entry(pmap, va, m);
2372 } while (va < va_last);
2376 pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2378 struct md_page *pvh;
2380 vm_offset_t va_last;
2383 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2384 KASSERT((pa & PDRMASK) == 0,
2385 ("pmap_pv_promote_pde: pa is not 4mpage aligned"));
2388 * Transfer the first page's pv entry for this mapping to the
2389 * 4mpage's pv list. Aside from avoiding the cost of a call
2390 * to get_pv_entry(), a transfer avoids the possibility that
2391 * get_pv_entry() calls pmap_collect() and that pmap_collect()
2392 * removes one of the mappings that is being promoted.
2394 m = PHYS_TO_VM_PAGE(pa);
2395 va = trunc_4mpage(va);
2396 pv = pmap_pvh_remove(&m->md, pmap, va);
2397 KASSERT(pv != NULL, ("pmap_pv_promote_pde: pv not found"));
2398 pvh = pa_to_pvh(pa);
2399 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_list);
2400 /* Free the remaining NPTEPG - 1 pv entries. */
2401 va_last = va + NBPDR - PAGE_SIZE;
2405 pmap_pvh_free(&m->md, pmap, va);
2406 } while (va < va_last);
2410 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2414 pv = pmap_pvh_remove(pvh, pmap, va);
2415 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2416 free_pv_entry(pmap, pv);
2420 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
2422 struct md_page *pvh;
2424 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2425 pmap_pvh_free(&m->md, pmap, va);
2426 if (TAILQ_EMPTY(&m->md.pv_list)) {
2427 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2428 if (TAILQ_EMPTY(&pvh->pv_list))
2429 vm_page_flag_clear(m, PG_WRITEABLE);
2434 * Create a pv entry for page at pa for
2438 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2442 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2443 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2444 pv = get_pv_entry(pmap, FALSE);
2446 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2450 * Conditionally create a pv entry.
2453 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2457 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2458 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2459 if (pv_entry_count < pv_entry_high_water &&
2460 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2462 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2469 * Create the pv entries for each of the pages within a superpage.
2472 pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2474 struct md_page *pvh;
2477 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2478 if (pv_entry_count < pv_entry_high_water &&
2479 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2481 pvh = pa_to_pvh(pa);
2482 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_list);
2489 * Fills a page table page with mappings to consecutive physical pages.
2492 pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte)
2496 for (pte = firstpte; pte < firstpte + NPTEPG; pte++) {
2498 newpte += PAGE_SIZE;
2503 * Tries to demote a 2- or 4MB page mapping. If demotion fails, the
2504 * 2- or 4MB page mapping is invalidated.
2507 pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
2509 pd_entry_t newpde, oldpde;
2510 pt_entry_t *firstpte, newpte;
2512 vm_page_t free, mpte;
2514 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2516 KASSERT((oldpde & (PG_PS | PG_V)) == (PG_PS | PG_V),
2517 ("pmap_demote_pde: oldpde is missing PG_PS and/or PG_V"));
2518 mpte = pmap_lookup_pt_page(pmap, va);
2520 pmap_remove_pt_page(pmap, mpte);
2522 KASSERT((oldpde & PG_W) == 0,
2523 ("pmap_demote_pde: page table page for a wired mapping"
2527 * Invalidate the 2- or 4MB page mapping and return
2528 * "failure" if the mapping was never accessed or the
2529 * allocation of the new page table page fails.
2531 if ((oldpde & PG_A) == 0 || (mpte = vm_page_alloc(NULL,
2532 va >> PDRSHIFT, VM_ALLOC_NOOBJ | VM_ALLOC_NORMAL |
2533 VM_ALLOC_WIRED)) == NULL) {
2535 pmap_remove_pde(pmap, pde, trunc_4mpage(va), &free);
2536 pmap_invalidate_page(pmap, trunc_4mpage(va));
2537 pmap_free_zero_pages(free);
2538 CTR2(KTR_PMAP, "pmap_demote_pde: failure for va %#x"
2539 " in pmap %p", va, pmap);
2542 if (va < VM_MAXUSER_ADDRESS)
2543 pmap->pm_stats.resident_count++;
2545 mptepa = VM_PAGE_TO_PHYS(mpte);
2548 * If the page mapping is in the kernel's address space, then the
2549 * KPTmap can provide access to the page table page. Otherwise,
2550 * temporarily map the page table page (mpte) into the kernel's
2551 * address space at either PADDR1 or PADDR2.
2554 firstpte = &KPTmap[i386_btop(trunc_4mpage(va))];
2555 else if (curthread->td_pinned > 0 && mtx_owned(&vm_page_queue_mtx)) {
2556 if ((*PMAP1 & PG_FRAME) != mptepa) {
2557 *PMAP1 = mptepa | PG_RW | PG_V | PG_A | PG_M;
2559 PMAP1cpu = PCPU_GET(cpuid);
2565 if (PMAP1cpu != PCPU_GET(cpuid)) {
2566 PMAP1cpu = PCPU_GET(cpuid);
2574 mtx_lock(&PMAP2mutex);
2575 if ((*PMAP2 & PG_FRAME) != mptepa) {
2576 *PMAP2 = mptepa | PG_RW | PG_V | PG_A | PG_M;
2577 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR2);
2581 newpde = mptepa | PG_M | PG_A | (oldpde & PG_U) | PG_RW | PG_V;
2582 KASSERT((oldpde & PG_A) != 0,
2583 ("pmap_demote_pde: oldpde is missing PG_A"));
2584 KASSERT((oldpde & (PG_M | PG_RW)) != PG_RW,
2585 ("pmap_demote_pde: oldpde is missing PG_M"));
2586 newpte = oldpde & ~PG_PS;
2587 if ((newpte & PG_PDE_PAT) != 0)
2588 newpte ^= PG_PDE_PAT | PG_PTE_PAT;
2591 * If the page table page is new, initialize it.
2593 if (mpte->wire_count == 1) {
2594 mpte->wire_count = NPTEPG;
2595 pmap_fill_ptp(firstpte, newpte);
2597 KASSERT((*firstpte & PG_FRAME) == (newpte & PG_FRAME),
2598 ("pmap_demote_pde: firstpte and newpte map different physical"
2602 * If the mapping has changed attributes, update the page table
2605 if ((*firstpte & PG_PTE_PROMOTE) != (newpte & PG_PTE_PROMOTE))
2606 pmap_fill_ptp(firstpte, newpte);
2609 * Demote the mapping. This pmap is locked. The old PDE has
2610 * PG_A set. If the old PDE has PG_RW set, it also has PG_M
2611 * set. Thus, there is no danger of a race with another
2612 * processor changing the setting of PG_A and/or PG_M between
2613 * the read above and the store below.
2615 if (workaround_erratum383)
2616 pmap_update_pde(pmap, va, pde, newpde);
2617 else if (pmap == kernel_pmap)
2618 pmap_kenter_pde(va, newpde);
2620 pde_store(pde, newpde);
2621 if (firstpte == PADDR2)
2622 mtx_unlock(&PMAP2mutex);
2625 * Invalidate the recursive mapping of the page table page.
2627 pmap_invalidate_page(pmap, (vm_offset_t)vtopte(va));
2630 * Demote the pv entry. This depends on the earlier demotion
2631 * of the mapping. Specifically, the (re)creation of a per-
2632 * page pv entry might trigger the execution of pmap_collect(),
2633 * which might reclaim a newly (re)created per-page pv entry
2634 * and destroy the associated mapping. In order to destroy
2635 * the mapping, the PDE must have already changed from mapping
2636 * the 2mpage to referencing the page table page.
2638 if ((oldpde & PG_MANAGED) != 0)
2639 pmap_pv_demote_pde(pmap, va, oldpde & PG_PS_FRAME);
2641 pmap_pde_demotions++;
2642 CTR2(KTR_PMAP, "pmap_demote_pde: success for va %#x"
2643 " in pmap %p", va, pmap);
2648 * pmap_remove_pde: do the things to unmap a superpage in a process
2651 pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
2654 struct md_page *pvh;
2656 vm_offset_t eva, va;
2659 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2660 KASSERT((sva & PDRMASK) == 0,
2661 ("pmap_remove_pde: sva is not 4mpage aligned"));
2662 oldpde = pte_load_clear(pdq);
2664 pmap->pm_stats.wired_count -= NBPDR / PAGE_SIZE;
2667 * Machines that don't support invlpg, also don't support
2671 pmap_invalidate_page(kernel_pmap, sva);
2672 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2673 if (oldpde & PG_MANAGED) {
2674 pvh = pa_to_pvh(oldpde & PG_PS_FRAME);
2675 pmap_pvh_free(pvh, pmap, sva);
2677 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
2678 va < eva; va += PAGE_SIZE, m++) {
2679 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
2682 vm_page_flag_set(m, PG_REFERENCED);
2683 if (TAILQ_EMPTY(&m->md.pv_list) &&
2684 TAILQ_EMPTY(&pvh->pv_list))
2685 vm_page_flag_clear(m, PG_WRITEABLE);
2688 if (pmap == kernel_pmap) {
2689 if (!pmap_demote_pde(pmap, pdq, sva))
2690 panic("pmap_remove_pde: failed demotion");
2692 mpte = pmap_lookup_pt_page(pmap, sva);
2694 pmap_remove_pt_page(pmap, mpte);
2695 pmap->pm_stats.resident_count--;
2696 KASSERT(mpte->wire_count == NPTEPG,
2697 ("pmap_remove_pde: pte page wire count error"));
2698 mpte->wire_count = 0;
2699 pmap_add_delayed_free_list(mpte, free, FALSE);
2700 atomic_subtract_int(&cnt.v_wire_count, 1);
2706 * pmap_remove_pte: do the things to unmap a page in a process
2709 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va, vm_page_t *free)
2714 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2715 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2716 oldpte = pte_load_clear(ptq);
2718 pmap->pm_stats.wired_count -= 1;
2720 * Machines that don't support invlpg, also don't support
2724 pmap_invalidate_page(kernel_pmap, va);
2725 pmap->pm_stats.resident_count -= 1;
2726 if (oldpte & PG_MANAGED) {
2727 m = PHYS_TO_VM_PAGE(oldpte & PG_FRAME);
2728 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2731 vm_page_flag_set(m, PG_REFERENCED);
2732 pmap_remove_entry(pmap, m, va);
2734 return (pmap_unuse_pt(pmap, va, free));
2738 * Remove a single page from a process address space
2741 pmap_remove_page(pmap_t pmap, vm_offset_t va, vm_page_t *free)
2745 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2746 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
2747 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2748 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
2750 pmap_remove_pte(pmap, pte, va, free);
2751 pmap_invalidate_page(pmap, va);
2755 * Remove the given range of addresses from the specified map.
2757 * It is assumed that the start and end are properly
2758 * rounded to the page size.
2761 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2766 vm_page_t free = NULL;
2770 * Perform an unsynchronized read. This is, however, safe.
2772 if (pmap->pm_stats.resident_count == 0)
2777 vm_page_lock_queues();
2782 * special handling of removing one page. a very
2783 * common operation and easy to short circuit some
2786 if ((sva + PAGE_SIZE == eva) &&
2787 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
2788 pmap_remove_page(pmap, sva, &free);
2792 for (; sva < eva; sva = pdnxt) {
2796 * Calculate index for next page table.
2798 pdnxt = (sva + NBPDR) & ~PDRMASK;
2801 if (pmap->pm_stats.resident_count == 0)
2804 pdirindex = sva >> PDRSHIFT;
2805 ptpaddr = pmap->pm_pdir[pdirindex];
2808 * Weed out invalid mappings. Note: we assume that the page
2809 * directory table is always allocated, and in kernel virtual.
2815 * Check for large page.
2817 if ((ptpaddr & PG_PS) != 0) {
2819 * Are we removing the entire large page? If not,
2820 * demote the mapping and fall through.
2822 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
2824 * The TLB entry for a PG_G mapping is
2825 * invalidated by pmap_remove_pde().
2827 if ((ptpaddr & PG_G) == 0)
2829 pmap_remove_pde(pmap,
2830 &pmap->pm_pdir[pdirindex], sva, &free);
2832 } else if (!pmap_demote_pde(pmap,
2833 &pmap->pm_pdir[pdirindex], sva)) {
2834 /* The large page mapping was destroyed. */
2840 * Limit our scan to either the end of the va represented
2841 * by the current page table page, or to the end of the
2842 * range being removed.
2847 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2853 * The TLB entry for a PG_G mapping is invalidated
2854 * by pmap_remove_pte().
2856 if ((*pte & PG_G) == 0)
2858 if (pmap_remove_pte(pmap, pte, sva, &free))
2865 pmap_invalidate_all(pmap);
2866 vm_page_unlock_queues();
2868 pmap_free_zero_pages(free);
2872 * Routine: pmap_remove_all
2874 * Removes this physical page from
2875 * all physical maps in which it resides.
2876 * Reflects back modify bits to the pager.
2879 * Original versions of this routine were very
2880 * inefficient because they iteratively called
2881 * pmap_remove (slow...)
2885 pmap_remove_all(vm_page_t m)
2887 struct md_page *pvh;
2890 pt_entry_t *pte, tpte;
2895 KASSERT((m->flags & PG_FICTITIOUS) == 0,
2896 ("pmap_remove_all: page %p is fictitious", m));
2897 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2899 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2900 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
2904 pde = pmap_pde(pmap, va);
2905 (void)pmap_demote_pde(pmap, pde, va);
2908 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2911 pmap->pm_stats.resident_count--;
2912 pde = pmap_pde(pmap, pv->pv_va);
2913 KASSERT((*pde & PG_PS) == 0, ("pmap_remove_all: found"
2914 " a 4mpage in page %p's pv list", m));
2915 pte = pmap_pte_quick(pmap, pv->pv_va);
2916 tpte = pte_load_clear(pte);
2918 pmap->pm_stats.wired_count--;
2920 vm_page_flag_set(m, PG_REFERENCED);
2923 * Update the vm_page_t clean and reference bits.
2925 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2928 pmap_unuse_pt(pmap, pv->pv_va, &free);
2929 pmap_invalidate_page(pmap, pv->pv_va);
2930 pmap_free_zero_pages(free);
2931 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2932 free_pv_entry(pmap, pv);
2935 vm_page_flag_clear(m, PG_WRITEABLE);
2940 * pmap_protect_pde: do the things to protect a 4mpage in a process
2943 pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva, vm_prot_t prot)
2945 pd_entry_t newpde, oldpde;
2946 vm_offset_t eva, va;
2948 boolean_t anychanged;
2950 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2951 KASSERT((sva & PDRMASK) == 0,
2952 ("pmap_protect_pde: sva is not 4mpage aligned"));
2955 oldpde = newpde = *pde;
2956 if (oldpde & PG_MANAGED) {
2958 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
2959 va < eva; va += PAGE_SIZE, m++) {
2961 * In contrast to the analogous operation on a 4KB page
2962 * mapping, the mapping's PG_A flag is not cleared and
2963 * the page's PG_REFERENCED flag is not set. The
2964 * reason is that pmap_demote_pde() expects that a 2/4MB
2965 * page mapping with a stored page table page has PG_A
2968 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
2972 if ((prot & VM_PROT_WRITE) == 0)
2973 newpde &= ~(PG_RW | PG_M);
2975 if ((prot & VM_PROT_EXECUTE) == 0)
2978 if (newpde != oldpde) {
2979 if (!pde_cmpset(pde, oldpde, newpde))
2982 pmap_invalidate_page(pmap, sva);
2986 return (anychanged);
2990 * Set the physical protection on the
2991 * specified range of this map as requested.
2994 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3001 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
3002 pmap_remove(pmap, sva, eva);
3007 if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) ==
3008 (VM_PROT_WRITE|VM_PROT_EXECUTE))
3011 if (prot & VM_PROT_WRITE)
3017 vm_page_lock_queues();
3020 for (; sva < eva; sva = pdnxt) {
3021 pt_entry_t obits, pbits;
3024 pdnxt = (sva + NBPDR) & ~PDRMASK;
3028 pdirindex = sva >> PDRSHIFT;
3029 ptpaddr = pmap->pm_pdir[pdirindex];
3032 * Weed out invalid mappings. Note: we assume that the page
3033 * directory table is always allocated, and in kernel virtual.
3039 * Check for large page.
3041 if ((ptpaddr & PG_PS) != 0) {
3043 * Are we protecting the entire large page? If not,
3044 * demote the mapping and fall through.
3046 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
3048 * The TLB entry for a PG_G mapping is
3049 * invalidated by pmap_protect_pde().
3051 if (pmap_protect_pde(pmap,
3052 &pmap->pm_pdir[pdirindex], sva, prot))
3055 } else if (!pmap_demote_pde(pmap,
3056 &pmap->pm_pdir[pdirindex], sva)) {
3057 /* The large page mapping was destroyed. */
3065 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
3071 * Regardless of whether a pte is 32 or 64 bits in
3072 * size, PG_RW, PG_A, and PG_M are among the least
3073 * significant 32 bits.
3075 obits = pbits = *pte;
3076 if ((pbits & PG_V) == 0)
3078 if (pbits & PG_MANAGED) {
3081 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
3082 vm_page_flag_set(m, PG_REFERENCED);
3085 if ((pbits & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
3087 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
3092 if ((prot & VM_PROT_WRITE) == 0)
3093 pbits &= ~(PG_RW | PG_M);
3095 if ((prot & VM_PROT_EXECUTE) == 0)
3099 if (pbits != obits) {
3101 if (!atomic_cmpset_64(pte, obits, pbits))
3104 if (!atomic_cmpset_int((u_int *)pte, obits,
3109 pmap_invalidate_page(pmap, sva);
3117 pmap_invalidate_all(pmap);
3118 vm_page_unlock_queues();
3123 * Tries to promote the 512 or 1024, contiguous 4KB page mappings that are
3124 * within a single page table page (PTP) to a single 2- or 4MB page mapping.
3125 * For promotion to occur, two conditions must be met: (1) the 4KB page
3126 * mappings must map aligned, contiguous physical memory and (2) the 4KB page
3127 * mappings must have identical characteristics.
3129 * Managed (PG_MANAGED) mappings within the kernel address space are not
3130 * promoted. The reason is that kernel PDEs are replicated in each pmap but
3131 * pmap_clear_ptes() and pmap_ts_referenced() only read the PDE from the kernel
3135 pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
3138 pt_entry_t *firstpte, oldpte, pa, *pte;
3139 vm_offset_t oldpteva;
3142 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3145 * Examine the first PTE in the specified PTP. Abort if this PTE is
3146 * either invalid, unused, or does not map the first 4KB physical page
3147 * within a 2- or 4MB page.
3149 firstpte = pmap_pte_quick(pmap, trunc_4mpage(va));
3152 if ((newpde & ((PG_FRAME & PDRMASK) | PG_A | PG_V)) != (PG_A | PG_V)) {
3153 pmap_pde_p_failures++;
3154 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3155 " in pmap %p", va, pmap);
3158 if ((*firstpte & PG_MANAGED) != 0 && pmap == kernel_pmap) {
3159 pmap_pde_p_failures++;
3160 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3161 " in pmap %p", va, pmap);
3164 if ((newpde & (PG_M | PG_RW)) == PG_RW) {
3166 * When PG_M is already clear, PG_RW can be cleared without
3167 * a TLB invalidation.
3169 if (!atomic_cmpset_int((u_int *)firstpte, newpde, newpde &
3176 * Examine each of the other PTEs in the specified PTP. Abort if this
3177 * PTE maps an unexpected 4KB physical page or does not have identical
3178 * characteristics to the first PTE.
3180 pa = (newpde & (PG_PS_FRAME | PG_A | PG_V)) + NBPDR - PAGE_SIZE;
3181 for (pte = firstpte + NPTEPG - 1; pte > firstpte; pte--) {
3184 if ((oldpte & (PG_FRAME | PG_A | PG_V)) != pa) {
3185 pmap_pde_p_failures++;
3186 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3187 " in pmap %p", va, pmap);
3190 if ((oldpte & (PG_M | PG_RW)) == PG_RW) {
3192 * When PG_M is already clear, PG_RW can be cleared
3193 * without a TLB invalidation.
3195 if (!atomic_cmpset_int((u_int *)pte, oldpte,
3199 oldpteva = (oldpte & PG_FRAME & PDRMASK) |
3201 CTR2(KTR_PMAP, "pmap_promote_pde: protect for va %#x"
3202 " in pmap %p", oldpteva, pmap);
3204 if ((oldpte & PG_PTE_PROMOTE) != (newpde & PG_PTE_PROMOTE)) {
3205 pmap_pde_p_failures++;
3206 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3207 " in pmap %p", va, pmap);
3214 * Save the page table page in its current state until the PDE
3215 * mapping the superpage is demoted by pmap_demote_pde() or
3216 * destroyed by pmap_remove_pde().
3218 mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
3219 KASSERT(mpte >= vm_page_array &&
3220 mpte < &vm_page_array[vm_page_array_size],
3221 ("pmap_promote_pde: page table page is out of range"));
3222 KASSERT(mpte->pindex == va >> PDRSHIFT,
3223 ("pmap_promote_pde: page table page's pindex is wrong"));
3224 pmap_insert_pt_page(pmap, mpte);
3227 * Promote the pv entries.
3229 if ((newpde & PG_MANAGED) != 0)
3230 pmap_pv_promote_pde(pmap, va, newpde & PG_PS_FRAME);
3233 * Propagate the PAT index to its proper position.
3235 if ((newpde & PG_PTE_PAT) != 0)
3236 newpde ^= PG_PDE_PAT | PG_PTE_PAT;
3239 * Map the superpage.
3241 if (workaround_erratum383)
3242 pmap_update_pde(pmap, va, pde, PG_PS | newpde);
3243 else if (pmap == kernel_pmap)
3244 pmap_kenter_pde(va, PG_PS | newpde);
3246 pde_store(pde, PG_PS | newpde);
3248 pmap_pde_promotions++;
3249 CTR2(KTR_PMAP, "pmap_promote_pde: success for va %#x"
3250 " in pmap %p", va, pmap);
3254 * Insert the given physical page (p) at
3255 * the specified virtual address (v) in the
3256 * target physical map with the protection requested.
3258 * If specified, the page will be wired down, meaning
3259 * that the related pte can not be reclaimed.
3261 * NB: This is the only routine which MAY NOT lazy-evaluate
3262 * or lose information. That is, this routine must actually
3263 * insert this page into the given map NOW.
3266 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
3267 vm_prot_t prot, boolean_t wired)
3273 pt_entry_t origpte, newpte;
3277 va = trunc_page(va);
3278 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
3279 KASSERT(va < UPT_MIN_ADDRESS || va >= UPT_MAX_ADDRESS,
3280 ("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va));
3284 vm_page_lock_queues();
3289 * In the case that a page table page is not
3290 * resident, we are creating it here.
3292 if (va < VM_MAXUSER_ADDRESS) {
3293 mpte = pmap_allocpte(pmap, va, M_WAITOK);
3296 pde = pmap_pde(pmap, va);
3297 if ((*pde & PG_PS) != 0)
3298 panic("pmap_enter: attempted pmap_enter on 4MB page");
3299 pte = pmap_pte_quick(pmap, va);
3302 * Page Directory table entry not valid, we need a new PT page
3305 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x",
3306 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
3309 pa = VM_PAGE_TO_PHYS(m);
3312 opa = origpte & PG_FRAME;
3315 * Mapping has not changed, must be protection or wiring change.
3317 if (origpte && (opa == pa)) {
3319 * Wiring change, just update stats. We don't worry about
3320 * wiring PT pages as they remain resident as long as there
3321 * are valid mappings in them. Hence, if a user page is wired,
3322 * the PT page will be also.
3324 if (wired && ((origpte & PG_W) == 0))
3325 pmap->pm_stats.wired_count++;
3326 else if (!wired && (origpte & PG_W))
3327 pmap->pm_stats.wired_count--;
3330 * Remove extra pte reference
3336 * We might be turning off write access to the page,
3337 * so we go ahead and sense modify status.
3339 if (origpte & PG_MANAGED) {
3346 * Mapping has changed, invalidate old range and fall through to
3347 * handle validating new mapping.
3351 pmap->pm_stats.wired_count--;
3352 if (origpte & PG_MANAGED) {
3353 om = PHYS_TO_VM_PAGE(opa);
3354 pmap_remove_entry(pmap, om, va);
3358 KASSERT(mpte->wire_count > 0,
3359 ("pmap_enter: missing reference to page table page,"
3363 pmap->pm_stats.resident_count++;
3366 * Enter on the PV list if part of our managed memory.
3368 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
3369 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
3370 ("pmap_enter: managed mapping within the clean submap"));
3371 pmap_insert_entry(pmap, va, m);
3376 * Increment counters
3379 pmap->pm_stats.wired_count++;
3383 * Now validate mapping with desired protection/wiring.
3385 newpte = (pt_entry_t)(pa | pmap_cache_bits(m->md.pat_mode, 0) | PG_V);
3386 if ((prot & VM_PROT_WRITE) != 0) {
3388 vm_page_flag_set(m, PG_WRITEABLE);
3391 if ((prot & VM_PROT_EXECUTE) == 0)
3396 if (va < VM_MAXUSER_ADDRESS)
3398 if (pmap == kernel_pmap)
3402 * if the mapping or permission bits are different, we need
3403 * to update the pte.
3405 if ((origpte & ~(PG_M|PG_A)) != newpte) {
3407 if ((access & VM_PROT_WRITE) != 0)
3409 if (origpte & PG_V) {
3411 origpte = pte_load_store(pte, newpte);
3412 if (origpte & PG_A) {
3413 if (origpte & PG_MANAGED)
3414 vm_page_flag_set(om, PG_REFERENCED);
3415 if (opa != VM_PAGE_TO_PHYS(m))
3418 if ((origpte & PG_NX) == 0 &&
3419 (newpte & PG_NX) != 0)
3423 if ((origpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
3424 if ((origpte & PG_MANAGED) != 0)
3426 if ((prot & VM_PROT_WRITE) == 0)
3430 pmap_invalidate_page(pmap, va);
3432 pte_store(pte, newpte);
3436 * If both the page table page and the reservation are fully
3437 * populated, then attempt promotion.
3439 if ((mpte == NULL || mpte->wire_count == NPTEPG) &&
3440 pg_ps_enabled && vm_reserv_level_iffullpop(m) == 0)
3441 pmap_promote_pde(pmap, pde, va);
3444 vm_page_unlock_queues();
3449 * Tries to create a 2- or 4MB page mapping. Returns TRUE if successful and
3450 * FALSE otherwise. Fails if (1) a page table page cannot be allocated without
3451 * blocking, (2) a mapping already exists at the specified virtual address, or
3452 * (3) a pv entry cannot be allocated without reclaiming another pv entry.
3455 pmap_enter_pde(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3457 pd_entry_t *pde, newpde;
3459 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3460 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3461 pde = pmap_pde(pmap, va);
3463 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3464 " in pmap %p", va, pmap);
3467 newpde = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.pat_mode, 1) |
3469 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
3470 newpde |= PG_MANAGED;
3473 * Abort this mapping if its PV entry could not be created.
3475 if (!pmap_pv_insert_pde(pmap, va, VM_PAGE_TO_PHYS(m))) {
3476 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3477 " in pmap %p", va, pmap);
3482 if ((prot & VM_PROT_EXECUTE) == 0)
3485 if (va < VM_MAXUSER_ADDRESS)
3489 * Increment counters.
3491 pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3494 * Map the superpage.
3496 pde_store(pde, newpde);
3498 pmap_pde_mappings++;
3499 CTR2(KTR_PMAP, "pmap_enter_pde: success for va %#lx"
3500 " in pmap %p", va, pmap);
3505 * Maps a sequence of resident pages belonging to the same object.
3506 * The sequence begins with the given page m_start. This page is
3507 * mapped at the given virtual address start. Each subsequent page is
3508 * mapped at a virtual address that is offset from start by the same
3509 * amount as the page is offset from m_start within the object. The
3510 * last page in the sequence is the page with the largest offset from
3511 * m_start that can be mapped at a virtual address less than the given
3512 * virtual address end. Not every virtual page between start and end
3513 * is mapped; only those for which a resident page exists with the
3514 * corresponding offset from m_start are mapped.
3517 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3518 vm_page_t m_start, vm_prot_t prot)
3522 vm_pindex_t diff, psize;
3524 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
3525 psize = atop(end - start);
3529 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3530 va = start + ptoa(diff);
3531 if ((va & PDRMASK) == 0 && va + NBPDR <= end &&
3532 (VM_PAGE_TO_PHYS(m) & PDRMASK) == 0 &&
3533 pg_ps_enabled && vm_reserv_level_iffullpop(m) == 0 &&
3534 pmap_enter_pde(pmap, va, m, prot))
3535 m = &m[NBPDR / PAGE_SIZE - 1];
3537 mpte = pmap_enter_quick_locked(pmap, va, m, prot,
3539 m = TAILQ_NEXT(m, listq);
3545 * this code makes some *MAJOR* assumptions:
3546 * 1. Current pmap & pmap exists.
3549 * 4. No page table pages.
3550 * but is *MUCH* faster than pmap_enter...
3554 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3558 (void) pmap_enter_quick_locked(pmap, va, m, prot, NULL);
3563 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
3564 vm_prot_t prot, vm_page_t mpte)
3570 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
3571 (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0,
3572 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
3573 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3574 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3577 * In the case that a page table page is not
3578 * resident, we are creating it here.
3580 if (va < VM_MAXUSER_ADDRESS) {
3585 * Calculate pagetable page index
3587 ptepindex = va >> PDRSHIFT;
3588 if (mpte && (mpte->pindex == ptepindex)) {
3592 * Get the page directory entry
3594 ptepa = pmap->pm_pdir[ptepindex];
3597 * If the page table page is mapped, we just increment
3598 * the hold count, and activate it.
3603 mpte = PHYS_TO_VM_PAGE(ptepa & PG_FRAME);
3606 mpte = _pmap_allocpte(pmap, ptepindex,
3617 * This call to vtopte makes the assumption that we are
3618 * entering the page into the current pmap. In order to support
3619 * quick entry into any pmap, one would likely use pmap_pte_quick.
3620 * But that isn't as quick as vtopte.
3632 * Enter on the PV list if part of our managed memory.
3634 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0 &&
3635 !pmap_try_insert_pv_entry(pmap, va, m)) {
3638 if (pmap_unwire_pte_hold(pmap, mpte, &free)) {
3639 pmap_invalidate_page(pmap, va);
3640 pmap_free_zero_pages(free);
3649 * Increment counters
3651 pmap->pm_stats.resident_count++;
3653 pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.pat_mode, 0);
3655 if ((prot & VM_PROT_EXECUTE) == 0)
3660 * Now validate mapping with RO protection
3662 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
3663 pte_store(pte, pa | PG_V | PG_U);
3665 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
3670 * Make a temporary mapping for a physical address. This is only intended
3671 * to be used for panic dumps.
3674 pmap_kenter_temporary(vm_paddr_t pa, int i)
3678 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
3679 pmap_kenter(va, pa);
3681 return ((void *)crashdumpmap);
3685 * This code maps large physical mmap regions into the
3686 * processor address space. Note that some shortcuts
3687 * are taken, but the code works.
3690 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3691 vm_pindex_t pindex, vm_size_t size)
3694 vm_paddr_t pa, ptepa;
3698 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3699 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3700 ("pmap_object_init_pt: non-device object"));
3702 (addr & (NBPDR - 1)) == 0 && (size & (NBPDR - 1)) == 0) {
3703 if (!vm_object_populate(object, pindex, pindex + atop(size)))
3705 p = vm_page_lookup(object, pindex);
3706 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3707 ("pmap_object_init_pt: invalid page %p", p));
3708 pat_mode = p->md.pat_mode;
3711 * Abort the mapping if the first page is not physically
3712 * aligned to a 2/4MB page boundary.
3714 ptepa = VM_PAGE_TO_PHYS(p);
3715 if (ptepa & (NBPDR - 1))
3719 * Skip the first page. Abort the mapping if the rest of
3720 * the pages are not physically contiguous or have differing
3721 * memory attributes.
3723 p = TAILQ_NEXT(p, listq);
3724 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size;
3726 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3727 ("pmap_object_init_pt: invalid page %p", p));
3728 if (pa != VM_PAGE_TO_PHYS(p) ||
3729 pat_mode != p->md.pat_mode)
3731 p = TAILQ_NEXT(p, listq);
3735 * Map using 2/4MB pages. Since "ptepa" is 2/4M aligned and
3736 * "size" is a multiple of 2/4M, adding the PAT setting to
3737 * "pa" will not affect the termination of this loop.
3740 for (pa = ptepa | pmap_cache_bits(pat_mode, 1); pa < ptepa +
3741 size; pa += NBPDR) {
3742 pde = pmap_pde(pmap, addr);
3744 pde_store(pde, pa | PG_PS | PG_M | PG_A |
3745 PG_U | PG_RW | PG_V);
3746 pmap->pm_stats.resident_count += NBPDR /
3748 pmap_pde_mappings++;
3750 /* Else continue on if the PDE is already valid. */
3758 * Routine: pmap_change_wiring
3759 * Function: Change the wiring attribute for a map/virtual-address
3761 * In/out conditions:
3762 * The mapping must already exist in the pmap.
3765 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3769 boolean_t are_queues_locked;
3771 are_queues_locked = FALSE;
3774 pde = pmap_pde(pmap, va);
3775 if ((*pde & PG_PS) != 0) {
3776 if (!wired != ((*pde & PG_W) == 0)) {
3777 if (!are_queues_locked) {
3778 are_queues_locked = TRUE;
3779 if (!mtx_trylock(&vm_page_queue_mtx)) {
3781 vm_page_lock_queues();
3785 if (!pmap_demote_pde(pmap, pde, va))
3786 panic("pmap_change_wiring: demotion failed");
3790 pte = pmap_pte(pmap, va);
3792 if (wired && !pmap_pte_w(pte))
3793 pmap->pm_stats.wired_count++;
3794 else if (!wired && pmap_pte_w(pte))
3795 pmap->pm_stats.wired_count--;
3798 * Wiring is not a hardware characteristic so there is no need to
3801 pmap_pte_set_w(pte, wired);
3802 pmap_pte_release(pte);
3804 if (are_queues_locked)
3805 vm_page_unlock_queues();
3812 * Copy the range specified by src_addr/len
3813 * from the source map to the range dst_addr/len
3814 * in the destination map.
3816 * This routine is only advisory and need not do anything.
3820 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
3821 vm_offset_t src_addr)
3825 vm_offset_t end_addr = src_addr + len;
3828 if (dst_addr != src_addr)
3831 if (!pmap_is_current(src_pmap))
3834 vm_page_lock_queues();
3835 if (dst_pmap < src_pmap) {
3836 PMAP_LOCK(dst_pmap);
3837 PMAP_LOCK(src_pmap);
3839 PMAP_LOCK(src_pmap);
3840 PMAP_LOCK(dst_pmap);
3843 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3844 pt_entry_t *src_pte, *dst_pte;
3845 vm_page_t dstmpte, srcmpte;
3846 pd_entry_t srcptepaddr;
3849 KASSERT(addr < UPT_MIN_ADDRESS,
3850 ("pmap_copy: invalid to pmap_copy page tables"));
3852 pdnxt = (addr + NBPDR) & ~PDRMASK;
3855 ptepindex = addr >> PDRSHIFT;
3857 srcptepaddr = src_pmap->pm_pdir[ptepindex];
3858 if (srcptepaddr == 0)
3861 if (srcptepaddr & PG_PS) {
3862 if (dst_pmap->pm_pdir[ptepindex] == 0 &&
3863 ((srcptepaddr & PG_MANAGED) == 0 ||
3864 pmap_pv_insert_pde(dst_pmap, addr, srcptepaddr &
3866 dst_pmap->pm_pdir[ptepindex] = srcptepaddr &
3868 dst_pmap->pm_stats.resident_count +=
3874 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr & PG_FRAME);
3875 KASSERT(srcmpte->wire_count > 0,
3876 ("pmap_copy: source page table page is unused"));
3878 if (pdnxt > end_addr)
3881 src_pte = vtopte(addr);
3882 while (addr < pdnxt) {
3886 * we only virtual copy managed pages
3888 if ((ptetemp & PG_MANAGED) != 0) {
3889 dstmpte = pmap_allocpte(dst_pmap, addr,
3891 if (dstmpte == NULL)
3893 dst_pte = pmap_pte_quick(dst_pmap, addr);
3894 if (*dst_pte == 0 &&
3895 pmap_try_insert_pv_entry(dst_pmap, addr,
3896 PHYS_TO_VM_PAGE(ptetemp & PG_FRAME))) {
3898 * Clear the wired, modified, and
3899 * accessed (referenced) bits
3902 *dst_pte = ptetemp & ~(PG_W | PG_M |
3904 dst_pmap->pm_stats.resident_count++;
3907 if (pmap_unwire_pte_hold(dst_pmap,
3909 pmap_invalidate_page(dst_pmap,
3911 pmap_free_zero_pages(free);
3915 if (dstmpte->wire_count >= srcmpte->wire_count)
3924 vm_page_unlock_queues();
3925 PMAP_UNLOCK(src_pmap);
3926 PMAP_UNLOCK(dst_pmap);
3929 static __inline void
3930 pagezero(void *page)
3932 #if defined(I686_CPU)
3933 if (cpu_class == CPUCLASS_686) {
3934 #if defined(CPU_ENABLE_SSE)
3935 if (cpu_feature & CPUID_SSE2)
3936 sse2_pagezero(page);
3939 i686_pagezero(page);
3942 bzero(page, PAGE_SIZE);
3946 * pmap_zero_page zeros the specified hardware page by mapping
3947 * the page into KVM and using bzero to clear its contents.
3950 pmap_zero_page(vm_page_t m)
3952 struct sysmaps *sysmaps;
3954 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3955 mtx_lock(&sysmaps->lock);
3956 if (*sysmaps->CMAP2)
3957 panic("pmap_zero_page: CMAP2 busy");
3959 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
3960 pmap_cache_bits(m->md.pat_mode, 0);
3961 invlcaddr(sysmaps->CADDR2);
3962 pagezero(sysmaps->CADDR2);
3963 *sysmaps->CMAP2 = 0;
3965 mtx_unlock(&sysmaps->lock);
3969 * pmap_zero_page_area zeros the specified hardware page by mapping
3970 * the page into KVM and using bzero to clear its contents.
3972 * off and size may not cover an area beyond a single hardware page.
3975 pmap_zero_page_area(vm_page_t m, int off, int size)
3977 struct sysmaps *sysmaps;
3979 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3980 mtx_lock(&sysmaps->lock);
3981 if (*sysmaps->CMAP2)
3982 panic("pmap_zero_page_area: CMAP2 busy");
3984 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
3985 pmap_cache_bits(m->md.pat_mode, 0);
3986 invlcaddr(sysmaps->CADDR2);
3987 if (off == 0 && size == PAGE_SIZE)
3988 pagezero(sysmaps->CADDR2);
3990 bzero((char *)sysmaps->CADDR2 + off, size);
3991 *sysmaps->CMAP2 = 0;
3993 mtx_unlock(&sysmaps->lock);
3997 * pmap_zero_page_idle zeros the specified hardware page by mapping
3998 * the page into KVM and using bzero to clear its contents. This
3999 * is intended to be called from the vm_pagezero process only and
4003 pmap_zero_page_idle(vm_page_t m)
4007 panic("pmap_zero_page_idle: CMAP3 busy");
4009 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
4010 pmap_cache_bits(m->md.pat_mode, 0);
4018 * pmap_copy_page copies the specified (machine independent)
4019 * page by mapping the page into virtual memory and using
4020 * bcopy to copy the page, one machine dependent page at a
4024 pmap_copy_page(vm_page_t src, vm_page_t dst)
4026 struct sysmaps *sysmaps;
4028 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4029 mtx_lock(&sysmaps->lock);
4030 if (*sysmaps->CMAP1)
4031 panic("pmap_copy_page: CMAP1 busy");
4032 if (*sysmaps->CMAP2)
4033 panic("pmap_copy_page: CMAP2 busy");
4035 invlpg((u_int)sysmaps->CADDR1);
4036 invlpg((u_int)sysmaps->CADDR2);
4037 *sysmaps->CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A |
4038 pmap_cache_bits(src->md.pat_mode, 0);
4039 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M |
4040 pmap_cache_bits(dst->md.pat_mode, 0);
4041 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
4042 *sysmaps->CMAP1 = 0;
4043 *sysmaps->CMAP2 = 0;
4045 mtx_unlock(&sysmaps->lock);
4049 * Returns true if the pmap's pv is one of the first
4050 * 16 pvs linked to from this page. This count may
4051 * be changed upwards or downwards in the future; it
4052 * is only necessary that true be returned for a small
4053 * subset of pmaps for proper page aging.
4056 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4058 struct md_page *pvh;
4062 if (m->flags & PG_FICTITIOUS)
4065 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4066 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4067 if (PV_PMAP(pv) == pmap) {
4075 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4076 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
4077 if (PV_PMAP(pv) == pmap)
4088 * pmap_page_wired_mappings:
4090 * Return the number of managed mappings to the given physical page
4094 pmap_page_wired_mappings(vm_page_t m)
4099 if ((m->flags & PG_FICTITIOUS) != 0)
4101 count = pmap_pvh_wired_mappings(&m->md, count);
4102 return (pmap_pvh_wired_mappings(pa_to_pvh(VM_PAGE_TO_PHYS(m)), count));
4106 * pmap_pvh_wired_mappings:
4108 * Return the updated number "count" of managed mappings that are wired.
4111 pmap_pvh_wired_mappings(struct md_page *pvh, int count)
4117 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4119 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
4122 pte = pmap_pte_quick(pmap, pv->pv_va);
4123 if ((*pte & PG_W) != 0)
4132 * Returns TRUE if the given page is mapped individually or as part of
4133 * a 4mpage. Otherwise, returns FALSE.
4136 pmap_page_is_mapped(vm_page_t m)
4138 struct md_page *pvh;
4140 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
4142 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4143 if (TAILQ_EMPTY(&m->md.pv_list)) {
4144 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4145 return (!TAILQ_EMPTY(&pvh->pv_list));
4151 * Remove all pages from specified address space
4152 * this aids process exit speeds. Also, this code
4153 * is special cased for current process only, but
4154 * can have the more generic (and slightly slower)
4155 * mode enabled. This is much faster than pmap_remove
4156 * in the case of running down an entire address space.
4159 pmap_remove_pages(pmap_t pmap)
4161 pt_entry_t *pte, tpte;
4162 vm_page_t free = NULL;
4163 vm_page_t m, mpte, mt;
4165 struct md_page *pvh;
4166 struct pv_chunk *pc, *npc;
4169 uint32_t inuse, bitmask;
4172 if (pmap != PCPU_GET(curpmap)) {
4173 printf("warning: pmap_remove_pages called with non-current pmap\n");
4176 vm_page_lock_queues();
4179 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4181 for (field = 0; field < _NPCM; field++) {
4182 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
4183 while (inuse != 0) {
4185 bitmask = 1UL << bit;
4186 idx = field * 32 + bit;
4187 pv = &pc->pc_pventry[idx];
4190 pte = pmap_pde(pmap, pv->pv_va);
4192 if ((tpte & PG_PS) == 0) {
4193 pte = vtopte(pv->pv_va);
4194 tpte = *pte & ~PG_PTE_PAT;
4199 "TPTE at %p IS ZERO @ VA %08x\n",
4205 * We cannot remove wired pages from a process' mapping at this time
4212 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
4213 KASSERT(m->phys_addr == (tpte & PG_FRAME),
4214 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
4215 m, (uintmax_t)m->phys_addr,
4218 KASSERT(m < &vm_page_array[vm_page_array_size],
4219 ("pmap_remove_pages: bad tpte %#jx",
4225 * Update the vm_page_t clean/reference bits.
4227 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4228 if ((tpte & PG_PS) != 0) {
4229 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4236 PV_STAT(pv_entry_frees++);
4237 PV_STAT(pv_entry_spare++);
4239 pc->pc_map[field] |= bitmask;
4240 if ((tpte & PG_PS) != 0) {
4241 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
4242 pvh = pa_to_pvh(tpte & PG_PS_FRAME);
4243 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
4244 if (TAILQ_EMPTY(&pvh->pv_list)) {
4245 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4246 if (TAILQ_EMPTY(&mt->md.pv_list))
4247 vm_page_flag_clear(mt, PG_WRITEABLE);
4249 mpte = pmap_lookup_pt_page(pmap, pv->pv_va);
4251 pmap_remove_pt_page(pmap, mpte);
4252 pmap->pm_stats.resident_count--;
4253 KASSERT(mpte->wire_count == NPTEPG,
4254 ("pmap_remove_pages: pte page wire count error"));
4255 mpte->wire_count = 0;
4256 pmap_add_delayed_free_list(mpte, &free, FALSE);
4257 atomic_subtract_int(&cnt.v_wire_count, 1);
4260 pmap->pm_stats.resident_count--;
4261 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
4262 if (TAILQ_EMPTY(&m->md.pv_list)) {
4263 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4264 if (TAILQ_EMPTY(&pvh->pv_list))
4265 vm_page_flag_clear(m, PG_WRITEABLE);
4267 pmap_unuse_pt(pmap, pv->pv_va, &free);
4272 PV_STAT(pv_entry_spare -= _NPCPV);
4273 PV_STAT(pc_chunk_count--);
4274 PV_STAT(pc_chunk_frees++);
4275 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4276 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
4277 pmap_qremove((vm_offset_t)pc, 1);
4278 vm_page_unwire(m, 0);
4280 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
4284 pmap_invalidate_all(pmap);
4285 vm_page_unlock_queues();
4287 pmap_free_zero_pages(free);
4293 * Return whether or not the specified physical page was modified
4294 * in any physical maps.
4297 pmap_is_modified(vm_page_t m)
4300 if (m->flags & PG_FICTITIOUS)
4302 if (pmap_is_modified_pvh(&m->md))
4304 return (pmap_is_modified_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
4308 * Returns TRUE if any of the given mappings were used to modify
4309 * physical memory. Otherwise, returns FALSE. Both page and 2mpage
4310 * mappings are supported.
4313 pmap_is_modified_pvh(struct md_page *pvh)
4320 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4323 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
4326 pte = pmap_pte_quick(pmap, pv->pv_va);
4327 rv = (*pte & (PG_M | PG_RW)) == (PG_M | PG_RW);
4337 * pmap_is_prefaultable:
4339 * Return whether or not the specified virtual address is elgible
4343 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
4351 pde = pmap_pde(pmap, addr);
4352 if (*pde != 0 && (*pde & PG_PS) == 0) {
4361 * Clear the write and modified bits in each of the given page's mappings.
4364 pmap_remove_write(vm_page_t m)
4366 struct md_page *pvh;
4367 pv_entry_t next_pv, pv;
4370 pt_entry_t oldpte, *pte;
4373 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4374 if ((m->flags & PG_FICTITIOUS) != 0 ||
4375 (m->flags & PG_WRITEABLE) == 0)
4378 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4379 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4383 pde = pmap_pde(pmap, va);
4384 if ((*pde & PG_RW) != 0)
4385 (void)pmap_demote_pde(pmap, pde, va);
4388 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4391 pde = pmap_pde(pmap, pv->pv_va);
4392 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_write: found"
4393 " a 4mpage in page %p's pv list", m));
4394 pte = pmap_pte_quick(pmap, pv->pv_va);
4397 if ((oldpte & PG_RW) != 0) {
4399 * Regardless of whether a pte is 32 or 64 bits
4400 * in size, PG_RW and PG_M are among the least
4401 * significant 32 bits.
4403 if (!atomic_cmpset_int((u_int *)pte, oldpte,
4404 oldpte & ~(PG_RW | PG_M)))
4406 if ((oldpte & PG_M) != 0)
4408 pmap_invalidate_page(pmap, pv->pv_va);
4412 vm_page_flag_clear(m, PG_WRITEABLE);
4417 * pmap_ts_referenced:
4419 * Return a count of reference bits for a page, clearing those bits.
4420 * It is not necessary for every reference bit to be cleared, but it
4421 * is necessary that 0 only be returned when there are truly no
4422 * reference bits set.
4424 * XXX: The exact number of bits to check and clear is a matter that
4425 * should be tested and standardized at some point in the future for
4426 * optimal aging of shared pages.
4429 pmap_ts_referenced(vm_page_t m)
4431 struct md_page *pvh;
4432 pv_entry_t pv, pvf, pvn;
4434 pd_entry_t oldpde, *pde;
4439 if (m->flags & PG_FICTITIOUS)
4442 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4443 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4444 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, pvn) {
4448 pde = pmap_pde(pmap, va);
4450 if ((oldpde & PG_A) != 0) {
4451 if (pmap_demote_pde(pmap, pde, va)) {
4452 if ((oldpde & PG_W) == 0) {
4454 * Remove the mapping to a single page
4455 * so that a subsequent access may
4456 * repromote. Since the underlying
4457 * page table page is fully populated,
4458 * this removal never frees a page
4461 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4463 pmap_remove_page(pmap, va, NULL);
4474 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
4477 pvn = TAILQ_NEXT(pv, pv_list);
4478 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
4479 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
4482 pde = pmap_pde(pmap, pv->pv_va);
4483 KASSERT((*pde & PG_PS) == 0, ("pmap_ts_referenced:"
4484 " found a 4mpage in page %p's pv list", m));
4485 pte = pmap_pte_quick(pmap, pv->pv_va);
4486 if ((*pte & PG_A) != 0) {
4487 atomic_clear_int((u_int *)pte, PG_A);
4488 pmap_invalidate_page(pmap, pv->pv_va);
4494 } while ((pv = pvn) != NULL && pv != pvf);
4502 * Clear the modify bits on the specified physical page.
4505 pmap_clear_modify(vm_page_t m)
4507 struct md_page *pvh;
4508 pv_entry_t next_pv, pv;
4510 pd_entry_t oldpde, *pde;
4511 pt_entry_t oldpte, *pte;
4514 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4515 if ((m->flags & PG_FICTITIOUS) != 0)
4518 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4519 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4523 pde = pmap_pde(pmap, va);
4525 if ((oldpde & PG_RW) != 0) {
4526 if (pmap_demote_pde(pmap, pde, va)) {
4527 if ((oldpde & PG_W) == 0) {
4529 * Write protect the mapping to a
4530 * single page so that a subsequent
4531 * write access may repromote.
4533 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4535 pte = pmap_pte_quick(pmap, va);
4537 if ((oldpte & PG_V) != 0) {
4539 * Regardless of whether a pte is 32 or 64 bits
4540 * in size, PG_RW and PG_M are among the least
4541 * significant 32 bits.
4543 while (!atomic_cmpset_int((u_int *)pte,
4545 oldpte & ~(PG_M | PG_RW)))
4548 pmap_invalidate_page(pmap, va);
4555 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4558 pde = pmap_pde(pmap, pv->pv_va);
4559 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_modify: found"
4560 " a 4mpage in page %p's pv list", m));
4561 pte = pmap_pte_quick(pmap, pv->pv_va);
4562 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4564 * Regardless of whether a pte is 32 or 64 bits
4565 * in size, PG_M is among the least significant
4568 atomic_clear_int((u_int *)pte, PG_M);
4569 pmap_invalidate_page(pmap, pv->pv_va);
4577 * pmap_clear_reference:
4579 * Clear the reference bit on the specified physical page.
4582 pmap_clear_reference(vm_page_t m)
4584 struct md_page *pvh;
4585 pv_entry_t next_pv, pv;
4587 pd_entry_t oldpde, *pde;
4591 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4592 if ((m->flags & PG_FICTITIOUS) != 0)
4595 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4596 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4600 pde = pmap_pde(pmap, va);
4602 if ((oldpde & PG_A) != 0) {
4603 if (pmap_demote_pde(pmap, pde, va)) {
4605 * Remove the mapping to a single page so
4606 * that a subsequent access may repromote.
4607 * Since the underlying page table page is
4608 * fully populated, this removal never frees
4609 * a page table page.
4611 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4613 pmap_remove_page(pmap, va, NULL);
4618 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4621 pde = pmap_pde(pmap, pv->pv_va);
4622 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_reference: found"
4623 " a 4mpage in page %p's pv list", m));
4624 pte = pmap_pte_quick(pmap, pv->pv_va);
4625 if ((*pte & PG_A) != 0) {
4627 * Regardless of whether a pte is 32 or 64 bits
4628 * in size, PG_A is among the least significant
4631 atomic_clear_int((u_int *)pte, PG_A);
4632 pmap_invalidate_page(pmap, pv->pv_va);
4640 * Miscellaneous support routines follow
4643 /* Adjust the cache mode for a 4KB page mapped via a PTE. */
4644 static __inline void
4645 pmap_pte_attr(pt_entry_t *pte, int cache_bits)
4650 * The cache mode bits are all in the low 32-bits of the
4651 * PTE, so we can just spin on updating the low 32-bits.
4654 opte = *(u_int *)pte;
4655 npte = opte & ~PG_PTE_CACHE;
4657 } while (npte != opte && !atomic_cmpset_int((u_int *)pte, opte, npte));
4660 /* Adjust the cache mode for a 2/4MB page mapped via a PDE. */
4661 static __inline void
4662 pmap_pde_attr(pd_entry_t *pde, int cache_bits)
4667 * The cache mode bits are all in the low 32-bits of the
4668 * PDE, so we can just spin on updating the low 32-bits.
4671 opde = *(u_int *)pde;
4672 npde = opde & ~PG_PDE_CACHE;
4674 } while (npde != opde && !atomic_cmpset_int((u_int *)pde, opde, npde));
4678 * Map a set of physical memory pages into the kernel virtual
4679 * address space. Return a pointer to where it is mapped. This
4680 * routine is intended to be used for mapping device memory,
4684 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
4686 vm_offset_t va, offset;
4689 offset = pa & PAGE_MASK;
4690 size = roundup(offset + size, PAGE_SIZE);
4693 if (pa < KERNLOAD && pa + size <= KERNLOAD)
4696 va = kmem_alloc_nofault(kernel_map, size);
4698 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4700 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
4701 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
4702 pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
4703 pmap_invalidate_cache_range(va, va + size);
4704 return ((void *)(va + offset));
4708 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
4711 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
4715 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
4718 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
4722 pmap_unmapdev(vm_offset_t va, vm_size_t size)
4724 vm_offset_t base, offset, tmpva;
4726 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
4728 base = trunc_page(va);
4729 offset = va & PAGE_MASK;
4730 size = roundup(offset + size, PAGE_SIZE);
4731 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
4732 pmap_kremove(tmpva);
4733 pmap_invalidate_range(kernel_pmap, va, tmpva);
4734 kmem_free(kernel_map, base, size);
4738 * Sets the memory attribute for the specified page.
4741 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
4743 struct sysmaps *sysmaps;
4744 vm_offset_t sva, eva;
4746 m->md.pat_mode = ma;
4747 if ((m->flags & PG_FICTITIOUS) != 0)
4751 * If "m" is a normal page, flush it from the cache.
4752 * See pmap_invalidate_cache_range().
4754 * First, try to find an existing mapping of the page by sf
4755 * buffer. sf_buf_invalidate_cache() modifies mapping and
4756 * flushes the cache.
4758 if (sf_buf_invalidate_cache(m))
4762 * If page is not mapped by sf buffer, but CPU does not
4763 * support self snoop, map the page transient and do
4764 * invalidation. In the worst case, whole cache is flushed by
4765 * pmap_invalidate_cache_range().
4767 if ((cpu_feature & (CPUID_SS|CPUID_CLFSH)) == CPUID_CLFSH) {
4768 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4769 mtx_lock(&sysmaps->lock);
4770 if (*sysmaps->CMAP2)
4771 panic("pmap_page_set_memattr: CMAP2 busy");
4773 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) |
4774 PG_A | PG_M | pmap_cache_bits(m->md.pat_mode, 0);
4775 invlcaddr(sysmaps->CADDR2);
4776 sva = (vm_offset_t)sysmaps->CADDR2;
4777 eva = sva + PAGE_SIZE;
4779 sva = eva = 0; /* gcc */
4780 pmap_invalidate_cache_range(sva, eva);
4782 *sysmaps->CMAP2 = 0;
4784 mtx_unlock(&sysmaps->lock);
4789 * Changes the specified virtual address range's memory type to that given by
4790 * the parameter "mode". The specified virtual address range must be
4791 * completely contained within either the kernel map.
4793 * Returns zero if the change completed successfully, and either EINVAL or
4794 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
4795 * of the virtual address range was not mapped, and ENOMEM is returned if
4796 * there was insufficient memory available to complete the change.
4799 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
4801 vm_offset_t base, offset, tmpva;
4804 int cache_bits_pte, cache_bits_pde;
4807 base = trunc_page(va);
4808 offset = va & PAGE_MASK;
4809 size = roundup(offset + size, PAGE_SIZE);
4812 * Only supported on kernel virtual addresses above the recursive map.
4814 if (base < VM_MIN_KERNEL_ADDRESS)
4817 cache_bits_pde = pmap_cache_bits(mode, 1);
4818 cache_bits_pte = pmap_cache_bits(mode, 0);
4822 * Pages that aren't mapped aren't supported. Also break down
4823 * 2/4MB pages into 4KB pages if required.
4825 PMAP_LOCK(kernel_pmap);
4826 for (tmpva = base; tmpva < base + size; ) {
4827 pde = pmap_pde(kernel_pmap, tmpva);
4829 PMAP_UNLOCK(kernel_pmap);
4834 * If the current 2/4MB page already has
4835 * the required memory type, then we need not
4836 * demote this page. Just increment tmpva to
4837 * the next 2/4MB page frame.
4839 if ((*pde & PG_PDE_CACHE) == cache_bits_pde) {
4840 tmpva = trunc_4mpage(tmpva) + NBPDR;
4845 * If the current offset aligns with a 2/4MB
4846 * page frame and there is at least 2/4MB left
4847 * within the range, then we need not break
4848 * down this page into 4KB pages.
4850 if ((tmpva & PDRMASK) == 0 &&
4851 tmpva + PDRMASK < base + size) {
4855 if (!pmap_demote_pde(kernel_pmap, pde, tmpva)) {
4856 PMAP_UNLOCK(kernel_pmap);
4860 pte = vtopte(tmpva);
4862 PMAP_UNLOCK(kernel_pmap);
4867 PMAP_UNLOCK(kernel_pmap);
4870 * Ok, all the pages exist, so run through them updating their
4871 * cache mode if required.
4873 for (tmpva = base; tmpva < base + size; ) {
4874 pde = pmap_pde(kernel_pmap, tmpva);
4876 if ((*pde & PG_PDE_CACHE) != cache_bits_pde) {
4877 pmap_pde_attr(pde, cache_bits_pde);
4880 tmpva = trunc_4mpage(tmpva) + NBPDR;
4882 pte = vtopte(tmpva);
4883 if ((*pte & PG_PTE_CACHE) != cache_bits_pte) {
4884 pmap_pte_attr(pte, cache_bits_pte);
4892 * Flush CPU caches to make sure any data isn't cached that
4893 * shouldn't be, etc.
4896 pmap_invalidate_range(kernel_pmap, base, tmpva);
4897 pmap_invalidate_cache_range(base, tmpva);
4903 * perform the pmap work for mincore
4906 pmap_mincore(pmap_t pmap, vm_offset_t addr)
4909 pt_entry_t *ptep, pte;
4915 pdep = pmap_pde(pmap, addr);
4917 if (*pdep & PG_PS) {
4919 val = MINCORE_SUPER;
4920 /* Compute the physical address of the 4KB page. */
4921 pa = ((*pdep & PG_PS_FRAME) | (addr & PDRMASK)) &
4924 ptep = pmap_pte(pmap, addr);
4926 pmap_pte_release(ptep);
4927 pa = pte & PG_FRAME;
4936 val |= MINCORE_INCORE;
4937 if ((pte & PG_MANAGED) == 0)
4940 m = PHYS_TO_VM_PAGE(pa);
4945 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
4946 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
4949 * Modified by someone else
4951 vm_page_lock_queues();
4952 if (m->dirty || pmap_is_modified(m))
4953 val |= MINCORE_MODIFIED_OTHER;
4954 vm_page_unlock_queues();
4960 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
4963 * Referenced by someone else
4965 vm_page_lock_queues();
4966 if ((m->flags & PG_REFERENCED) ||
4967 pmap_ts_referenced(m)) {
4968 val |= MINCORE_REFERENCED_OTHER;
4969 vm_page_flag_set(m, PG_REFERENCED);
4971 vm_page_unlock_queues();
4978 pmap_activate(struct thread *td)
4980 pmap_t pmap, oldpmap;
4984 pmap = vmspace_pmap(td->td_proc->p_vmspace);
4985 oldpmap = PCPU_GET(curpmap);
4987 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask));
4988 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
4990 oldpmap->pm_active &= ~1;
4991 pmap->pm_active |= 1;
4994 cr3 = vtophys(pmap->pm_pdpt);
4996 cr3 = vtophys(pmap->pm_pdir);
4999 * pmap_activate is for the current thread on the current cpu
5001 td->td_pcb->pcb_cr3 = cr3;
5003 PCPU_SET(curpmap, pmap);
5008 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
5013 * Increase the starting virtual address of the given mapping if a
5014 * different alignment might result in more superpage mappings.
5017 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
5018 vm_offset_t *addr, vm_size_t size)
5020 vm_offset_t superpage_offset;
5024 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
5025 offset += ptoa(object->pg_color);
5026 superpage_offset = offset & PDRMASK;
5027 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
5028 (*addr & PDRMASK) == superpage_offset)
5030 if ((*addr & PDRMASK) < superpage_offset)
5031 *addr = (*addr & ~PDRMASK) + superpage_offset;
5033 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
5037 #if defined(PMAP_DEBUG)
5038 pmap_pid_dump(int pid)
5045 sx_slock(&allproc_lock);
5046 FOREACH_PROC_IN_SYSTEM(p) {
5047 if (p->p_pid != pid)
5053 pmap = vmspace_pmap(p->p_vmspace);
5054 for (i = 0; i < NPDEPTD; i++) {
5057 vm_offset_t base = i << PDRSHIFT;
5059 pde = &pmap->pm_pdir[i];
5060 if (pde && pmap_pde_v(pde)) {
5061 for (j = 0; j < NPTEPG; j++) {
5062 vm_offset_t va = base + (j << PAGE_SHIFT);
5063 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
5068 sx_sunlock(&allproc_lock);
5071 pte = pmap_pte(pmap, va);
5072 if (pte && pmap_pte_v(pte)) {
5076 m = PHYS_TO_VM_PAGE(pa & PG_FRAME);
5077 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
5078 va, pa, m->hold_count, m->wire_count, m->flags);
5093 sx_sunlock(&allproc_lock);
5100 static void pads(pmap_t pm);
5101 void pmap_pvdump(vm_offset_t pa);
5103 /* print address space of pmap*/
5111 if (pm == kernel_pmap)
5113 for (i = 0; i < NPDEPTD; i++)
5115 for (j = 0; j < NPTEPG; j++) {
5116 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
5117 if (pm == kernel_pmap && va < KERNBASE)
5119 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
5121 ptep = pmap_pte(pm, va);
5122 if (pmap_pte_v(ptep))
5123 printf("%x:%x ", va, *ptep);
5129 pmap_pvdump(vm_paddr_t pa)
5135 printf("pa %x", pa);
5136 m = PHYS_TO_VM_PAGE(pa);
5137 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
5139 printf(" -> pmap %p, va %x", (void *)pmap, pv->pv_va);