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"
109 #include "opt_xbox.h"
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/kernel.h>
115 #include <sys/lock.h>
116 #include <sys/malloc.h>
117 #include <sys/mman.h>
118 #include <sys/msgbuf.h>
119 #include <sys/mutex.h>
120 #include <sys/proc.h>
121 #include <sys/sf_buf.h>
123 #include <sys/vmmeter.h>
124 #include <sys/sched.h>
125 #include <sys/sysctl.h>
129 #include <sys/cpuset.h>
133 #include <vm/vm_param.h>
134 #include <vm/vm_kern.h>
135 #include <vm/vm_page.h>
136 #include <vm/vm_map.h>
137 #include <vm/vm_object.h>
138 #include <vm/vm_extern.h>
139 #include <vm/vm_pageout.h>
140 #include <vm/vm_pager.h>
141 #include <vm/vm_reserv.h>
144 #include <machine/cpu.h>
145 #include <machine/cputypes.h>
146 #include <machine/md_var.h>
147 #include <machine/pcb.h>
148 #include <machine/specialreg.h>
150 #include <machine/smp.h>
154 #include <machine/xbox.h>
157 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
158 #define CPU_ENABLE_SSE
161 #ifndef PMAP_SHPGPERPROC
162 #define PMAP_SHPGPERPROC 200
165 #if !defined(DIAGNOSTIC)
166 #ifdef __GNUC_GNU_INLINE__
167 #define PMAP_INLINE __attribute__((__gnu_inline__)) inline
169 #define PMAP_INLINE extern inline
176 #define PV_STAT(x) do { x ; } while (0)
178 #define PV_STAT(x) do { } while (0)
181 #define pa_index(pa) ((pa) >> PDRSHIFT)
182 #define pa_to_pvh(pa) (&pv_table[pa_index(pa)])
185 * Get PDEs and PTEs for user/kernel address space
187 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
188 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
190 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
191 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
192 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
193 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
194 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
196 #define pmap_pte_set_w(pte, v) ((v) ? atomic_set_int((u_int *)(pte), PG_W) : \
197 atomic_clear_int((u_int *)(pte), PG_W))
198 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
200 struct pmap kernel_pmap_store;
201 LIST_HEAD(pmaplist, pmap);
202 static struct pmaplist allpmaps;
203 static struct mtx allpmaps_lock;
205 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
206 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
207 int pgeflag = 0; /* PG_G or-in */
208 int pseflag = 0; /* PG_PS or-in */
210 static int nkpt = NKPT;
211 vm_offset_t kernel_vm_end = KERNBASE + NKPT * NBPDR;
212 extern u_int32_t KERNend;
213 extern u_int32_t KPTphys;
217 static uma_zone_t pdptzone;
220 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
222 static int pat_works = 1;
223 SYSCTL_INT(_vm_pmap, OID_AUTO, pat_works, CTLFLAG_RD, &pat_works, 1,
224 "Is page attribute table fully functional?");
226 static int pg_ps_enabled = 1;
227 SYSCTL_INT(_vm_pmap, OID_AUTO, pg_ps_enabled, CTLFLAG_RDTUN, &pg_ps_enabled, 0,
228 "Are large page mappings enabled?");
230 #define PAT_INDEX_SIZE 8
231 static int pat_index[PAT_INDEX_SIZE]; /* cache mode to PAT index conversion */
234 * Data for the pv entry allocation mechanism
236 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
237 static struct md_page *pv_table;
238 static int shpgperproc = PMAP_SHPGPERPROC;
240 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
241 int pv_maxchunks; /* How many chunks we have KVA for */
242 vm_offset_t pv_vafree; /* freelist stored in the PTE */
245 * All those kernel PT submaps that BSD is so fond of
254 static struct sysmaps sysmaps_pcpu[MAXCPU];
255 pt_entry_t *CMAP1 = 0;
256 static pt_entry_t *CMAP3;
257 static pd_entry_t *KPTD;
258 caddr_t CADDR1 = 0, ptvmmap = 0;
259 static caddr_t CADDR3;
260 struct msgbuf *msgbufp = 0;
265 static caddr_t crashdumpmap;
267 static pt_entry_t *PMAP1 = 0, *PMAP2;
268 static pt_entry_t *PADDR1 = 0, *PADDR2;
271 static int PMAP1changedcpu;
272 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
274 "Number of times pmap_pte_quick changed CPU with same PMAP1");
276 static int PMAP1changed;
277 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
279 "Number of times pmap_pte_quick changed PMAP1");
280 static int PMAP1unchanged;
281 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
283 "Number of times pmap_pte_quick didn't change PMAP1");
284 static struct mtx PMAP2mutex;
286 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
287 static pv_entry_t get_pv_entry(pmap_t locked_pmap, int try);
288 static void pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
289 static boolean_t pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
290 static void pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa);
291 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
292 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
294 static int pmap_pvh_wired_mappings(struct md_page *pvh, int count);
296 static boolean_t pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
297 static boolean_t pmap_enter_pde(pmap_t pmap, vm_offset_t va, vm_page_t m,
299 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
300 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
301 static void pmap_flush_page(vm_page_t m);
302 static void pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte);
303 static void pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte);
304 static boolean_t pmap_is_modified_pvh(struct md_page *pvh);
305 static boolean_t pmap_is_referenced_pvh(struct md_page *pvh);
306 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
307 static void pmap_kenter_pde(vm_offset_t va, pd_entry_t newpde);
308 static vm_page_t pmap_lookup_pt_page(pmap_t pmap, vm_offset_t va);
309 static void pmap_pde_attr(pd_entry_t *pde, int cache_bits);
310 static void pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
311 static boolean_t pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva,
313 static void pmap_pte_attr(pt_entry_t *pte, int cache_bits);
314 static void pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
316 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva,
318 static void pmap_remove_pt_page(pmap_t pmap, vm_page_t mpte);
319 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va,
321 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
323 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
324 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
326 static void pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
328 static void pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde);
330 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
332 static vm_page_t _pmap_allocpte(pmap_t pmap, u_int ptepindex, int flags);
333 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free);
334 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
335 static void pmap_pte_release(pt_entry_t *pte);
336 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t *);
338 static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
340 static void pmap_set_pg(void);
342 static __inline void pagezero(void *page);
344 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
345 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
348 * If you get an error here, then you set KVA_PAGES wrong! See the
349 * description of KVA_PAGES in sys/i386/include/pmap.h. It must be
350 * multiple of 4 for a normal kernel, or a multiple of 8 for a PAE.
352 CTASSERT(KERNBASE % (1 << 24) == 0);
355 * Bootstrap the system enough to run with virtual memory.
357 * On the i386 this is called after mapping has already been enabled
358 * and just syncs the pmap module with what has already been done.
359 * [We can't call it easily with mapping off since the kernel is not
360 * mapped with PA == VA, hence we would have to relocate every address
361 * from the linked base (virtual) address "KERNBASE" to the actual
362 * (physical) address starting relative to 0]
365 pmap_bootstrap(vm_paddr_t firstaddr)
368 pt_entry_t *pte, *unused;
369 struct sysmaps *sysmaps;
373 * Initialize the first available kernel virtual address. However,
374 * using "firstaddr" may waste a few pages of the kernel virtual
375 * address space, because locore may not have mapped every physical
376 * page that it allocated. Preferably, locore would provide a first
377 * unused virtual address in addition to "firstaddr".
379 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
381 virtual_end = VM_MAX_KERNEL_ADDRESS;
384 * Initialize the kernel pmap (which is statically allocated).
386 PMAP_LOCK_INIT(kernel_pmap);
387 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
389 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
391 kernel_pmap->pm_root = NULL;
392 CPU_FILL(&kernel_pmap->pm_active); /* don't allow deactivation */
393 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
394 LIST_INIT(&allpmaps);
397 * Request a spin mutex so that changes to allpmaps cannot be
398 * preempted by smp_rendezvous_cpus(). Otherwise,
399 * pmap_update_pde_kernel() could access allpmaps while it is
402 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
403 mtx_lock_spin(&allpmaps_lock);
404 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
405 mtx_unlock_spin(&allpmaps_lock);
408 * Reserve some special page table entries/VA space for temporary
411 #define SYSMAP(c, p, v, n) \
412 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
418 * CMAP1/CMAP2 are used for zeroing and copying pages.
419 * CMAP3 is used for the idle process page zeroing.
421 for (i = 0; i < MAXCPU; i++) {
422 sysmaps = &sysmaps_pcpu[i];
423 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
424 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
425 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
427 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
428 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
433 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
436 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
438 SYSMAP(caddr_t, unused, ptvmmap, 1)
441 * msgbufp is used to map the system message buffer.
443 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(msgbufsize)))
446 * KPTmap is used by pmap_kextract().
448 * KPTmap is first initialized by locore. However, that initial
449 * KPTmap can only support NKPT page table pages. Here, a larger
450 * KPTmap is created that can support KVA_PAGES page table pages.
452 SYSMAP(pt_entry_t *, KPTD, KPTmap, KVA_PAGES)
454 for (i = 0; i < NKPT; i++)
455 KPTD[i] = (KPTphys + (i << PAGE_SHIFT)) | pgeflag | PG_RW | PG_V;
458 * Adjust the start of the KPTD and KPTmap so that the implementation
459 * of pmap_kextract() and pmap_growkernel() can be made simpler.
462 KPTmap -= i386_btop(KPTDI << PDRSHIFT);
465 * ptemap is used for pmap_pte_quick
467 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1)
468 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1)
470 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
475 * Leave in place an identity mapping (virt == phys) for the low 1 MB
476 * physical memory region that is used by the ACPI wakeup code. This
477 * mapping must not have PG_G set.
480 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
481 * an early stadium, we cannot yet neatly map video memory ... :-(
482 * Better fixes are very welcome! */
483 if (!arch_i386_is_xbox)
485 for (i = 1; i < NKPT; i++)
488 /* Initialize the PAT MSR if present. */
491 /* Turn on PG_G on kernel page(s) */
501 int pat_table[PAT_INDEX_SIZE];
506 /* Set default PAT index table. */
507 for (i = 0; i < PAT_INDEX_SIZE; i++)
509 pat_table[PAT_WRITE_BACK] = 0;
510 pat_table[PAT_WRITE_THROUGH] = 1;
511 pat_table[PAT_UNCACHEABLE] = 3;
512 pat_table[PAT_WRITE_COMBINING] = 3;
513 pat_table[PAT_WRITE_PROTECTED] = 3;
514 pat_table[PAT_UNCACHED] = 3;
516 /* Bail if this CPU doesn't implement PAT. */
517 if ((cpu_feature & CPUID_PAT) == 0) {
518 for (i = 0; i < PAT_INDEX_SIZE; i++)
519 pat_index[i] = pat_table[i];
525 * Due to some Intel errata, we can only safely use the lower 4
528 * Intel Pentium III Processor Specification Update
529 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
532 * Intel Pentium IV Processor Specification Update
533 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
535 if (cpu_vendor_id == CPU_VENDOR_INTEL &&
536 !(CPUID_TO_FAMILY(cpu_id) == 6 && CPUID_TO_MODEL(cpu_id) >= 0xe))
539 /* Initialize default PAT entries. */
540 pat_msr = PAT_VALUE(0, PAT_WRITE_BACK) |
541 PAT_VALUE(1, PAT_WRITE_THROUGH) |
542 PAT_VALUE(2, PAT_UNCACHED) |
543 PAT_VALUE(3, PAT_UNCACHEABLE) |
544 PAT_VALUE(4, PAT_WRITE_BACK) |
545 PAT_VALUE(5, PAT_WRITE_THROUGH) |
546 PAT_VALUE(6, PAT_UNCACHED) |
547 PAT_VALUE(7, PAT_UNCACHEABLE);
551 * Leave the indices 0-3 at the default of WB, WT, UC-, and UC.
552 * Program 5 and 6 as WP and WC.
553 * Leave 4 and 7 as WB and UC.
555 pat_msr &= ~(PAT_MASK(5) | PAT_MASK(6));
556 pat_msr |= PAT_VALUE(5, PAT_WRITE_PROTECTED) |
557 PAT_VALUE(6, PAT_WRITE_COMBINING);
558 pat_table[PAT_UNCACHED] = 2;
559 pat_table[PAT_WRITE_PROTECTED] = 5;
560 pat_table[PAT_WRITE_COMBINING] = 6;
563 * Just replace PAT Index 2 with WC instead of UC-.
565 pat_msr &= ~PAT_MASK(2);
566 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
567 pat_table[PAT_WRITE_COMBINING] = 2;
572 load_cr4(cr4 & ~CR4_PGE);
574 /* Disable caches (CD = 1, NW = 0). */
576 load_cr0((cr0 & ~CR0_NW) | CR0_CD);
578 /* Flushes caches and TLBs. */
582 /* Update PAT and index table. */
583 wrmsr(MSR_PAT, pat_msr);
584 for (i = 0; i < PAT_INDEX_SIZE; i++)
585 pat_index[i] = pat_table[i];
587 /* Flush caches and TLBs again. */
591 /* Restore caches and PGE. */
597 * Set PG_G on kernel pages. Only the BSP calls this when SMP is turned on.
603 vm_offset_t va, endva;
608 endva = KERNBASE + KERNend;
611 va = KERNBASE + KERNLOAD;
613 pdir_pde(PTD, va) |= pgeflag;
614 invltlb(); /* Play it safe, invltlb() every time */
618 va = (vm_offset_t)btext;
623 invltlb(); /* Play it safe, invltlb() every time */
630 * Initialize a vm_page's machine-dependent fields.
633 pmap_page_init(vm_page_t m)
636 TAILQ_INIT(&m->md.pv_list);
637 m->md.pat_mode = PAT_WRITE_BACK;
642 pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
645 /* Inform UMA that this allocator uses kernel_map/object. */
646 *flags = UMA_SLAB_KERNEL;
647 return ((void *)kmem_alloc_contig(kernel_map, bytes, wait, 0x0ULL,
648 0xffffffffULL, 1, 0, VM_MEMATTR_DEFAULT));
653 * ABuse the pte nodes for unmapped kva to thread a kva freelist through.
655 * - Must deal with pages in order to ensure that none of the PG_* bits
656 * are ever set, PG_V in particular.
657 * - Assumes we can write to ptes without pte_store() atomic ops, even
658 * on PAE systems. This should be ok.
659 * - Assumes nothing will ever test these addresses for 0 to indicate
660 * no mapping instead of correctly checking PG_V.
661 * - Assumes a vm_offset_t will fit in a pte (true for i386).
662 * Because PG_V is never set, there can be no mappings to invalidate.
665 pmap_ptelist_alloc(vm_offset_t *head)
672 return (va); /* Out of memory */
676 panic("pmap_ptelist_alloc: va with PG_V set!");
682 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
687 panic("pmap_ptelist_free: freeing va with PG_V set!");
689 *pte = *head; /* virtual! PG_V is 0 though */
694 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
700 for (i = npages - 1; i >= 0; i--) {
701 va = (vm_offset_t)base + i * PAGE_SIZE;
702 pmap_ptelist_free(head, va);
708 * Initialize the pmap module.
709 * Called by vm_init, to initialize any structures that the pmap
710 * system needs to map virtual memory.
720 * Initialize the vm page array entries for the kernel pmap's
723 for (i = 0; i < NKPT; i++) {
724 mpte = PHYS_TO_VM_PAGE(KPTphys + (i << PAGE_SHIFT));
725 KASSERT(mpte >= vm_page_array &&
726 mpte < &vm_page_array[vm_page_array_size],
727 ("pmap_init: page table page is out of range"));
728 mpte->pindex = i + KPTDI;
729 mpte->phys_addr = KPTphys + (i << PAGE_SHIFT);
733 * Initialize the address space (zone) for the pv entries. Set a
734 * high water mark so that the system can recover from excessive
735 * numbers of pv entries.
737 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
738 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
739 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
740 pv_entry_max = roundup(pv_entry_max, _NPCPV);
741 pv_entry_high_water = 9 * (pv_entry_max / 10);
744 * If the kernel is running in a virtual machine on an AMD Family 10h
745 * processor, then it must assume that MCA is enabled by the virtual
748 if (vm_guest == VM_GUEST_VM && cpu_vendor_id == CPU_VENDOR_AMD &&
749 CPUID_TO_FAMILY(cpu_id) == 0x10)
750 workaround_erratum383 = 1;
753 * Are large page mappings supported and enabled?
755 TUNABLE_INT_FETCH("vm.pmap.pg_ps_enabled", &pg_ps_enabled);
758 else if (pg_ps_enabled) {
759 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
760 ("pmap_init: can't assign to pagesizes[1]"));
761 pagesizes[1] = NBPDR;
765 * Calculate the size of the pv head table for superpages.
767 for (i = 0; phys_avail[i + 1]; i += 2);
768 pv_npg = round_4mpage(phys_avail[(i - 2) + 1]) / NBPDR;
771 * Allocate memory for the pv head table for superpages.
773 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
775 pv_table = (struct md_page *)kmem_alloc(kernel_map, s);
776 for (i = 0; i < pv_npg; i++)
777 TAILQ_INIT(&pv_table[i].pv_list);
779 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
780 pv_chunkbase = (struct pv_chunk *)kmem_alloc_nofault(kernel_map,
781 PAGE_SIZE * pv_maxchunks);
782 if (pv_chunkbase == NULL)
783 panic("pmap_init: not enough kvm for pv chunks");
784 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
786 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
787 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
788 UMA_ZONE_VM | UMA_ZONE_NOFREE);
789 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
794 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
795 "Max number of PV entries");
796 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
797 "Page share factor per proc");
799 SYSCTL_NODE(_vm_pmap, OID_AUTO, pde, CTLFLAG_RD, 0,
800 "2/4MB page mapping counters");
802 static u_long pmap_pde_demotions;
803 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, demotions, CTLFLAG_RD,
804 &pmap_pde_demotions, 0, "2/4MB page demotions");
806 static u_long pmap_pde_mappings;
807 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, mappings, CTLFLAG_RD,
808 &pmap_pde_mappings, 0, "2/4MB page mappings");
810 static u_long pmap_pde_p_failures;
811 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, p_failures, CTLFLAG_RD,
812 &pmap_pde_p_failures, 0, "2/4MB page promotion failures");
814 static u_long pmap_pde_promotions;
815 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, promotions, CTLFLAG_RD,
816 &pmap_pde_promotions, 0, "2/4MB page promotions");
818 /***************************************************
819 * Low level helper routines.....
820 ***************************************************/
823 * Determine the appropriate bits to set in a PTE or PDE for a specified
827 pmap_cache_bits(int mode, boolean_t is_pde)
829 int cache_bits, pat_flag, pat_idx;
831 if (mode < 0 || mode >= PAT_INDEX_SIZE || pat_index[mode] < 0)
832 panic("Unknown caching mode %d\n", mode);
834 /* The PAT bit is different for PTE's and PDE's. */
835 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
837 /* Map the caching mode to a PAT index. */
838 pat_idx = pat_index[mode];
840 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
843 cache_bits |= pat_flag;
845 cache_bits |= PG_NC_PCD;
847 cache_bits |= PG_NC_PWT;
852 * The caller is responsible for maintaining TLB consistency.
855 pmap_kenter_pde(vm_offset_t va, pd_entry_t newpde)
859 boolean_t PTD_updated;
862 mtx_lock_spin(&allpmaps_lock);
863 LIST_FOREACH(pmap, &allpmaps, pm_list) {
864 if ((pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] &
867 pde = pmap_pde(pmap, va);
868 pde_store(pde, newpde);
870 mtx_unlock_spin(&allpmaps_lock);
872 ("pmap_kenter_pde: current page table is not in allpmaps"));
876 * After changing the page size for the specified virtual address in the page
877 * table, flush the corresponding entries from the processor's TLB. Only the
878 * calling processor's TLB is affected.
880 * The calling thread must be pinned to a processor.
883 pmap_update_pde_invalidate(vm_offset_t va, pd_entry_t newpde)
887 if ((newpde & PG_PS) == 0)
888 /* Demotion: flush a specific 2MB page mapping. */
890 else if ((newpde & PG_G) == 0)
892 * Promotion: flush every 4KB page mapping from the TLB
893 * because there are too many to flush individually.
898 * Promotion: flush every 4KB page mapping from the TLB,
899 * including any global (PG_G) mappings.
902 load_cr4(cr4 & ~CR4_PGE);
904 * Although preemption at this point could be detrimental to
905 * performance, it would not lead to an error. PG_G is simply
906 * ignored if CR4.PGE is clear. Moreover, in case this block
907 * is re-entered, the load_cr4() either above or below will
908 * modify CR4.PGE flushing the TLB.
910 load_cr4(cr4 | CR4_PGE);
915 * For SMP, these functions have to use the IPI mechanism for coherence.
917 * N.B.: Before calling any of the following TLB invalidation functions,
918 * the calling processor must ensure that all stores updating a non-
919 * kernel page table are globally performed. Otherwise, another
920 * processor could cache an old, pre-update entry without being
921 * invalidated. This can happen one of two ways: (1) The pmap becomes
922 * active on another processor after its pm_active field is checked by
923 * one of the following functions but before a store updating the page
924 * table is globally performed. (2) The pmap becomes active on another
925 * processor before its pm_active field is checked but due to
926 * speculative loads one of the following functions stills reads the
927 * pmap as inactive on the other processor.
929 * The kernel page table is exempt because its pm_active field is
930 * immutable. The kernel page table is always active on every
934 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
940 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
944 cpuid = PCPU_GET(cpuid);
945 other_cpus = all_cpus;
946 CPU_CLR(cpuid, &other_cpus);
947 if (CPU_ISSET(cpuid, &pmap->pm_active))
949 CPU_AND(&other_cpus, &pmap->pm_active);
950 if (!CPU_EMPTY(&other_cpus))
951 smp_masked_invlpg(other_cpus, va);
957 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
964 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
965 for (addr = sva; addr < eva; addr += PAGE_SIZE)
967 smp_invlpg_range(sva, eva);
969 cpuid = PCPU_GET(cpuid);
970 other_cpus = all_cpus;
971 CPU_CLR(cpuid, &other_cpus);
972 if (CPU_ISSET(cpuid, &pmap->pm_active))
973 for (addr = sva; addr < eva; addr += PAGE_SIZE)
975 CPU_AND(&other_cpus, &pmap->pm_active);
976 if (!CPU_EMPTY(&other_cpus))
977 smp_masked_invlpg_range(other_cpus, sva, eva);
983 pmap_invalidate_all(pmap_t pmap)
989 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
993 cpuid = PCPU_GET(cpuid);
994 other_cpus = all_cpus;
995 CPU_CLR(cpuid, &other_cpus);
996 if (CPU_ISSET(cpuid, &pmap->pm_active))
998 CPU_AND(&other_cpus, &pmap->pm_active);
999 if (!CPU_EMPTY(&other_cpus))
1000 smp_masked_invltlb(other_cpus);
1006 pmap_invalidate_cache(void)
1016 cpuset_t invalidate; /* processors that invalidate their TLB */
1020 u_int store; /* processor that updates the PDE */
1024 pmap_update_pde_kernel(void *arg)
1026 struct pde_action *act = arg;
1030 if (act->store == PCPU_GET(cpuid)) {
1033 * Elsewhere, this operation requires allpmaps_lock for
1034 * synchronization. Here, it does not because it is being
1035 * performed in the context of an all_cpus rendezvous.
1037 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1038 pde = pmap_pde(pmap, act->va);
1039 pde_store(pde, act->newpde);
1045 pmap_update_pde_user(void *arg)
1047 struct pde_action *act = arg;
1049 if (act->store == PCPU_GET(cpuid))
1050 pde_store(act->pde, act->newpde);
1054 pmap_update_pde_teardown(void *arg)
1056 struct pde_action *act = arg;
1058 if (CPU_ISSET(PCPU_GET(cpuid), &act->invalidate))
1059 pmap_update_pde_invalidate(act->va, act->newpde);
1063 * Change the page size for the specified virtual address in a way that
1064 * prevents any possibility of the TLB ever having two entries that map the
1065 * same virtual address using different page sizes. This is the recommended
1066 * workaround for Erratum 383 on AMD Family 10h processors. It prevents a
1067 * machine check exception for a TLB state that is improperly diagnosed as a
1071 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1073 struct pde_action act;
1074 cpuset_t active, other_cpus;
1078 cpuid = PCPU_GET(cpuid);
1079 other_cpus = all_cpus;
1080 CPU_CLR(cpuid, &other_cpus);
1081 if (pmap == kernel_pmap)
1084 active = pmap->pm_active;
1085 if (CPU_OVERLAP(&active, &other_cpus)) {
1087 act.invalidate = active;
1090 act.newpde = newpde;
1091 CPU_SET(cpuid, &active);
1092 smp_rendezvous_cpus(active,
1093 smp_no_rendevous_barrier, pmap == kernel_pmap ?
1094 pmap_update_pde_kernel : pmap_update_pde_user,
1095 pmap_update_pde_teardown, &act);
1097 if (pmap == kernel_pmap)
1098 pmap_kenter_pde(va, newpde);
1100 pde_store(pde, newpde);
1101 if (CPU_ISSET(cpuid, &active))
1102 pmap_update_pde_invalidate(va, newpde);
1108 * Normal, non-SMP, 486+ invalidation functions.
1109 * We inline these within pmap.c for speed.
1112 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
1115 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1120 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1124 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1125 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1130 pmap_invalidate_all(pmap_t pmap)
1133 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1138 pmap_invalidate_cache(void)
1145 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1148 if (pmap == kernel_pmap)
1149 pmap_kenter_pde(va, newpde);
1151 pde_store(pde, newpde);
1152 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1153 pmap_update_pde_invalidate(va, newpde);
1157 #define PMAP_CLFLUSH_THRESHOLD (2 * 1024 * 1024)
1160 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva)
1163 KASSERT((sva & PAGE_MASK) == 0,
1164 ("pmap_invalidate_cache_range: sva not page-aligned"));
1165 KASSERT((eva & PAGE_MASK) == 0,
1166 ("pmap_invalidate_cache_range: eva not page-aligned"));
1168 if (cpu_feature & CPUID_SS)
1169 ; /* If "Self Snoop" is supported, do nothing. */
1170 else if ((cpu_feature & CPUID_CLFSH) != 0 &&
1171 eva - sva < PMAP_CLFLUSH_THRESHOLD) {
1174 * Otherwise, do per-cache line flush. Use the mfence
1175 * instruction to insure that previous stores are
1176 * included in the write-back. The processor
1177 * propagates flush to other processors in the cache
1181 for (; sva < eva; sva += cpu_clflush_line_size)
1187 * No targeted cache flush methods are supported by CPU,
1188 * or the supplied range is bigger than 2MB.
1189 * Globally invalidate cache.
1191 pmap_invalidate_cache();
1196 pmap_invalidate_cache_pages(vm_page_t *pages, int count)
1200 if (count >= PMAP_CLFLUSH_THRESHOLD / PAGE_SIZE ||
1201 (cpu_feature & CPUID_CLFSH) == 0) {
1202 pmap_invalidate_cache();
1204 for (i = 0; i < count; i++)
1205 pmap_flush_page(pages[i]);
1210 * Are we current address space or kernel? N.B. We return FALSE when
1211 * a pmap's page table is in use because a kernel thread is borrowing
1212 * it. The borrowed page table can change spontaneously, making any
1213 * dependence on its continued use subject to a race condition.
1216 pmap_is_current(pmap_t pmap)
1219 return (pmap == kernel_pmap ||
1220 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
1221 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
1225 * If the given pmap is not the current or kernel pmap, the returned pte must
1226 * be released by passing it to pmap_pte_release().
1229 pmap_pte(pmap_t pmap, vm_offset_t va)
1234 pde = pmap_pde(pmap, va);
1238 /* are we current address space or kernel? */
1239 if (pmap_is_current(pmap))
1240 return (vtopte(va));
1241 mtx_lock(&PMAP2mutex);
1242 newpf = *pde & PG_FRAME;
1243 if ((*PMAP2 & PG_FRAME) != newpf) {
1244 *PMAP2 = newpf | PG_RW | PG_V | PG_A | PG_M;
1245 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR2);
1247 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
1253 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
1256 static __inline void
1257 pmap_pte_release(pt_entry_t *pte)
1260 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2)
1261 mtx_unlock(&PMAP2mutex);
1264 static __inline void
1265 invlcaddr(void *caddr)
1268 invlpg((u_int)caddr);
1272 * Super fast pmap_pte routine best used when scanning
1273 * the pv lists. This eliminates many coarse-grained
1274 * invltlb calls. Note that many of the pv list
1275 * scans are across different pmaps. It is very wasteful
1276 * to do an entire invltlb for checking a single mapping.
1278 * If the given pmap is not the current pmap, vm_page_queue_mtx
1279 * must be held and curthread pinned to a CPU.
1282 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
1287 pde = pmap_pde(pmap, va);
1291 /* are we current address space or kernel? */
1292 if (pmap_is_current(pmap))
1293 return (vtopte(va));
1294 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1295 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1296 newpf = *pde & PG_FRAME;
1297 if ((*PMAP1 & PG_FRAME) != newpf) {
1298 *PMAP1 = newpf | PG_RW | PG_V | PG_A | PG_M;
1300 PMAP1cpu = PCPU_GET(cpuid);
1306 if (PMAP1cpu != PCPU_GET(cpuid)) {
1307 PMAP1cpu = PCPU_GET(cpuid);
1313 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
1319 * Routine: pmap_extract
1321 * Extract the physical page address associated
1322 * with the given map/virtual_address pair.
1325 pmap_extract(pmap_t pmap, vm_offset_t va)
1333 pde = pmap->pm_pdir[va >> PDRSHIFT];
1335 if ((pde & PG_PS) != 0)
1336 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
1338 pte = pmap_pte(pmap, va);
1339 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1340 pmap_pte_release(pte);
1348 * Routine: pmap_extract_and_hold
1350 * Atomically extract and hold the physical page
1351 * with the given pmap and virtual address pair
1352 * if that mapping permits the given protection.
1355 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1358 pt_entry_t pte, *ptep;
1366 pde = *pmap_pde(pmap, va);
1369 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1370 if (vm_page_pa_tryrelock(pmap, (pde &
1371 PG_PS_FRAME) | (va & PDRMASK), &pa))
1373 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
1378 ptep = pmap_pte(pmap, va);
1380 pmap_pte_release(ptep);
1382 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1383 if (vm_page_pa_tryrelock(pmap, pte & PG_FRAME,
1386 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1396 /***************************************************
1397 * Low level mapping routines.....
1398 ***************************************************/
1401 * Add a wired page to the kva.
1402 * Note: not SMP coherent.
1404 * This function may be used before pmap_bootstrap() is called.
1407 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1412 pte_store(pte, pa | PG_RW | PG_V | pgeflag);
1415 static __inline void
1416 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1421 pte_store(pte, pa | PG_RW | PG_V | pgeflag | pmap_cache_bits(mode, 0));
1425 * Remove a page from the kernel pagetables.
1426 * Note: not SMP coherent.
1428 * This function may be used before pmap_bootstrap() is called.
1431 pmap_kremove(vm_offset_t va)
1440 * Used to map a range of physical addresses into kernel
1441 * virtual address space.
1443 * The value passed in '*virt' is a suggested virtual address for
1444 * the mapping. Architectures which can support a direct-mapped
1445 * physical to virtual region can return the appropriate address
1446 * within that region, leaving '*virt' unchanged. Other
1447 * architectures should map the pages starting at '*virt' and
1448 * update '*virt' with the first usable address after the mapped
1452 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1454 vm_offset_t va, sva;
1455 vm_paddr_t superpage_offset;
1460 * Does the physical address range's size and alignment permit at
1461 * least one superpage mapping to be created?
1463 superpage_offset = start & PDRMASK;
1464 if ((end - start) - ((NBPDR - superpage_offset) & PDRMASK) >= NBPDR) {
1466 * Increase the starting virtual address so that its alignment
1467 * does not preclude the use of superpage mappings.
1469 if ((va & PDRMASK) < superpage_offset)
1470 va = (va & ~PDRMASK) + superpage_offset;
1471 else if ((va & PDRMASK) > superpage_offset)
1472 va = ((va + PDRMASK) & ~PDRMASK) + superpage_offset;
1475 while (start < end) {
1476 if ((start & PDRMASK) == 0 && end - start >= NBPDR &&
1478 KASSERT((va & PDRMASK) == 0,
1479 ("pmap_map: misaligned va %#x", va));
1480 newpde = start | PG_PS | pgeflag | PG_RW | PG_V;
1481 pmap_kenter_pde(va, newpde);
1485 pmap_kenter(va, start);
1490 pmap_invalidate_range(kernel_pmap, sva, va);
1497 * Add a list of wired pages to the kva
1498 * this routine is only used for temporary
1499 * kernel mappings that do not need to have
1500 * page modification or references recorded.
1501 * Note that old mappings are simply written
1502 * over. The page *must* be wired.
1503 * Note: SMP coherent. Uses a ranged shootdown IPI.
1506 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1508 pt_entry_t *endpte, oldpte, pa, *pte;
1513 endpte = pte + count;
1514 while (pte < endpte) {
1516 pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.pat_mode, 0);
1517 if ((*pte & (PG_FRAME | PG_PTE_CACHE)) != pa) {
1519 pte_store(pte, pa | pgeflag | PG_RW | PG_V);
1523 if (__predict_false((oldpte & PG_V) != 0))
1524 pmap_invalidate_range(kernel_pmap, sva, sva + count *
1529 * This routine tears out page mappings from the
1530 * kernel -- it is meant only for temporary mappings.
1531 * Note: SMP coherent. Uses a ranged shootdown IPI.
1534 pmap_qremove(vm_offset_t sva, int count)
1539 while (count-- > 0) {
1543 pmap_invalidate_range(kernel_pmap, sva, va);
1546 /***************************************************
1547 * Page table page management routines.....
1548 ***************************************************/
1549 static __inline void
1550 pmap_free_zero_pages(vm_page_t free)
1554 while (free != NULL) {
1557 /* Preserve the page's PG_ZERO setting. */
1558 vm_page_free_toq(m);
1563 * Schedule the specified unused page table page to be freed. Specifically,
1564 * add the page to the specified list of pages that will be released to the
1565 * physical memory manager after the TLB has been updated.
1567 static __inline void
1568 pmap_add_delayed_free_list(vm_page_t m, vm_page_t *free, boolean_t set_PG_ZERO)
1572 m->flags |= PG_ZERO;
1574 m->flags &= ~PG_ZERO;
1580 * Inserts the specified page table page into the specified pmap's collection
1581 * of idle page table pages. Each of a pmap's page table pages is responsible
1582 * for mapping a distinct range of virtual addresses. The pmap's collection is
1583 * ordered by this virtual address range.
1586 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte)
1590 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1591 root = pmap->pm_root;
1596 root = vm_page_splay(mpte->pindex, root);
1597 if (mpte->pindex < root->pindex) {
1598 mpte->left = root->left;
1601 } else if (mpte->pindex == root->pindex)
1602 panic("pmap_insert_pt_page: pindex already inserted");
1604 mpte->right = root->right;
1609 pmap->pm_root = mpte;
1613 * Looks for a page table page mapping the specified virtual address in the
1614 * specified pmap's collection of idle page table pages. Returns NULL if there
1615 * is no page table page corresponding to the specified virtual address.
1618 pmap_lookup_pt_page(pmap_t pmap, vm_offset_t va)
1621 vm_pindex_t pindex = va >> PDRSHIFT;
1623 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1624 if ((mpte = pmap->pm_root) != NULL && mpte->pindex != pindex) {
1625 mpte = vm_page_splay(pindex, mpte);
1626 if ((pmap->pm_root = mpte)->pindex != pindex)
1633 * Removes the specified page table page from the specified pmap's collection
1634 * of idle page table pages. The specified page table page must be a member of
1635 * the pmap's collection.
1638 pmap_remove_pt_page(pmap_t pmap, vm_page_t mpte)
1642 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1643 if (mpte != pmap->pm_root)
1644 vm_page_splay(mpte->pindex, pmap->pm_root);
1645 if (mpte->left == NULL)
1648 root = vm_page_splay(mpte->pindex, mpte->left);
1649 root->right = mpte->right;
1651 pmap->pm_root = root;
1655 * This routine unholds page table pages, and if the hold count
1656 * drops to zero, then it decrements the wire count.
1659 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free)
1663 if (m->wire_count == 0)
1664 return (_pmap_unwire_pte_hold(pmap, m, free));
1670 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free)
1675 * unmap the page table page
1677 pmap->pm_pdir[m->pindex] = 0;
1678 --pmap->pm_stats.resident_count;
1681 * This is a release store so that the ordinary store unmapping
1682 * the page table page is globally performed before TLB shoot-
1685 atomic_subtract_rel_int(&cnt.v_wire_count, 1);
1688 * Do an invltlb to make the invalidated mapping
1689 * take effect immediately.
1691 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1692 pmap_invalidate_page(pmap, pteva);
1695 * Put page on a list so that it is released after
1696 * *ALL* TLB shootdown is done
1698 pmap_add_delayed_free_list(m, free, TRUE);
1704 * After removing a page table entry, this routine is used to
1705 * conditionally free the page, and manage the hold/wire counts.
1708 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t *free)
1713 if (va >= VM_MAXUSER_ADDRESS)
1715 ptepde = *pmap_pde(pmap, va);
1716 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1717 return (pmap_unwire_pte_hold(pmap, mpte, free));
1721 * Initialize the pmap for the swapper process.
1724 pmap_pinit0(pmap_t pmap)
1727 PMAP_LOCK_INIT(pmap);
1729 * Since the page table directory is shared with the kernel pmap,
1730 * which is already included in the list "allpmaps", this pmap does
1731 * not need to be inserted into that list.
1733 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1735 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1737 pmap->pm_root = NULL;
1738 CPU_ZERO(&pmap->pm_active);
1739 PCPU_SET(curpmap, pmap);
1740 TAILQ_INIT(&pmap->pm_pvchunk);
1741 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1745 * Initialize a preallocated and zeroed pmap structure,
1746 * such as one in a vmspace structure.
1749 pmap_pinit(pmap_t pmap)
1751 vm_page_t m, ptdpg[NPGPTD];
1756 PMAP_LOCK_INIT(pmap);
1759 * No need to allocate page table space yet but we do need a valid
1760 * page directory table.
1762 if (pmap->pm_pdir == NULL) {
1763 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map,
1765 if (pmap->pm_pdir == NULL) {
1766 PMAP_LOCK_DESTROY(pmap);
1770 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
1771 KASSERT(((vm_offset_t)pmap->pm_pdpt &
1772 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
1773 ("pmap_pinit: pdpt misaligned"));
1774 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
1775 ("pmap_pinit: pdpt above 4g"));
1777 pmap->pm_root = NULL;
1779 KASSERT(pmap->pm_root == NULL,
1780 ("pmap_pinit: pmap has reserved page table page(s)"));
1783 * allocate the page directory page(s)
1785 for (i = 0; i < NPGPTD;) {
1786 m = vm_page_alloc(NULL, color++,
1787 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1796 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1798 for (i = 0; i < NPGPTD; i++)
1799 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1800 pagezero(pmap->pm_pdir + (i * NPDEPG));
1802 mtx_lock_spin(&allpmaps_lock);
1803 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1804 /* Copy the kernel page table directory entries. */
1805 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1806 mtx_unlock_spin(&allpmaps_lock);
1808 /* install self-referential address mapping entry(s) */
1809 for (i = 0; i < NPGPTD; i++) {
1810 pa = VM_PAGE_TO_PHYS(ptdpg[i]);
1811 pmap->pm_pdir[PTDPTDI + i] = pa | PG_V | PG_RW | PG_A | PG_M;
1813 pmap->pm_pdpt[i] = pa | PG_V;
1817 CPU_ZERO(&pmap->pm_active);
1818 TAILQ_INIT(&pmap->pm_pvchunk);
1819 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1825 * this routine is called if the page table page is not
1829 _pmap_allocpte(pmap_t pmap, u_int ptepindex, int flags)
1834 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1835 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1836 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1839 * Allocate a page table page.
1841 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1842 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1843 if (flags & M_WAITOK) {
1845 vm_page_unlock_queues();
1847 vm_page_lock_queues();
1852 * Indicate the need to retry. While waiting, the page table
1853 * page may have been allocated.
1857 if ((m->flags & PG_ZERO) == 0)
1861 * Map the pagetable page into the process address space, if
1862 * it isn't already there.
1865 pmap->pm_stats.resident_count++;
1867 ptepa = VM_PAGE_TO_PHYS(m);
1868 pmap->pm_pdir[ptepindex] =
1869 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1875 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1881 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1882 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1883 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1886 * Calculate pagetable page index
1888 ptepindex = va >> PDRSHIFT;
1891 * Get the page directory entry
1893 ptepa = pmap->pm_pdir[ptepindex];
1896 * This supports switching from a 4MB page to a
1899 if (ptepa & PG_PS) {
1900 (void)pmap_demote_pde(pmap, &pmap->pm_pdir[ptepindex], va);
1901 ptepa = pmap->pm_pdir[ptepindex];
1905 * If the page table page is mapped, we just increment the
1906 * hold count, and activate it.
1909 m = PHYS_TO_VM_PAGE(ptepa & PG_FRAME);
1913 * Here if the pte page isn't mapped, or if it has
1916 m = _pmap_allocpte(pmap, ptepindex, flags);
1917 if (m == NULL && (flags & M_WAITOK))
1924 /***************************************************
1925 * Pmap allocation/deallocation routines.
1926 ***************************************************/
1930 * Deal with a SMP shootdown of other users of the pmap that we are
1931 * trying to dispose of. This can be a bit hairy.
1933 static cpuset_t *lazymask;
1934 static u_int lazyptd;
1935 static volatile u_int lazywait;
1937 void pmap_lazyfix_action(void);
1940 pmap_lazyfix_action(void)
1944 (*ipi_lazypmap_counts[PCPU_GET(cpuid)])++;
1946 if (rcr3() == lazyptd)
1947 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1948 CPU_CLR_ATOMIC(PCPU_GET(cpuid), lazymask);
1949 atomic_store_rel_int(&lazywait, 1);
1953 pmap_lazyfix_self(u_int cpuid)
1956 if (rcr3() == lazyptd)
1957 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1958 CPU_CLR_ATOMIC(cpuid, lazymask);
1963 pmap_lazyfix(pmap_t pmap)
1965 cpuset_t mymask, mask;
1969 mask = pmap->pm_active;
1970 while (!CPU_EMPTY(&mask)) {
1973 /* Find least significant set bit. */
1974 lsb = cpusetobj_ffs(&mask);
1977 CPU_SETOF(lsb, &mask);
1978 mtx_lock_spin(&smp_ipi_mtx);
1980 lazyptd = vtophys(pmap->pm_pdpt);
1982 lazyptd = vtophys(pmap->pm_pdir);
1984 cpuid = PCPU_GET(cpuid);
1986 /* Use a cpuset just for having an easy check. */
1987 CPU_SETOF(cpuid, &mymask);
1988 if (!CPU_CMP(&mask, &mymask)) {
1989 lazymask = &pmap->pm_active;
1990 pmap_lazyfix_self(cpuid);
1992 atomic_store_rel_int((u_int *)&lazymask,
1993 (u_int)&pmap->pm_active);
1994 atomic_store_rel_int(&lazywait, 0);
1995 ipi_selected(mask, IPI_LAZYPMAP);
1996 while (lazywait == 0) {
2002 mtx_unlock_spin(&smp_ipi_mtx);
2004 printf("pmap_lazyfix: spun for 50000000\n");
2005 mask = pmap->pm_active;
2012 * Cleaning up on uniprocessor is easy. For various reasons, we're
2013 * unlikely to have to even execute this code, including the fact
2014 * that the cleanup is deferred until the parent does a wait(2), which
2015 * means that another userland process has run.
2018 pmap_lazyfix(pmap_t pmap)
2022 cr3 = vtophys(pmap->pm_pdir);
2023 if (cr3 == rcr3()) {
2024 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
2025 CPU_CLR(PCPU_GET(cpuid), &pmap->pm_active);
2031 * Release any resources held by the given physical map.
2032 * Called when a pmap initialized by pmap_pinit is being released.
2033 * Should only be called if the map contains no valid mappings.
2036 pmap_release(pmap_t pmap)
2038 vm_page_t m, ptdpg[NPGPTD];
2041 KASSERT(pmap->pm_stats.resident_count == 0,
2042 ("pmap_release: pmap resident count %ld != 0",
2043 pmap->pm_stats.resident_count));
2044 KASSERT(pmap->pm_root == NULL,
2045 ("pmap_release: pmap has reserved page table page(s)"));
2048 mtx_lock_spin(&allpmaps_lock);
2049 LIST_REMOVE(pmap, pm_list);
2050 mtx_unlock_spin(&allpmaps_lock);
2052 for (i = 0; i < NPGPTD; i++)
2053 ptdpg[i] = PHYS_TO_VM_PAGE(pmap->pm_pdir[PTDPTDI + i] &
2056 bzero(pmap->pm_pdir + PTDPTDI, (nkpt + NPGPTD) *
2057 sizeof(*pmap->pm_pdir));
2059 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
2061 for (i = 0; i < NPGPTD; i++) {
2064 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME),
2065 ("pmap_release: got wrong ptd page"));
2068 atomic_subtract_int(&cnt.v_wire_count, 1);
2069 vm_page_free_zero(m);
2071 PMAP_LOCK_DESTROY(pmap);
2075 kvm_size(SYSCTL_HANDLER_ARGS)
2077 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
2079 return (sysctl_handle_long(oidp, &ksize, 0, req));
2081 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
2082 0, 0, kvm_size, "IU", "Size of KVM");
2085 kvm_free(SYSCTL_HANDLER_ARGS)
2087 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
2089 return (sysctl_handle_long(oidp, &kfree, 0, req));
2091 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
2092 0, 0, kvm_free, "IU", "Amount of KVM free");
2095 * grow the number of kernel page table entries, if needed
2098 pmap_growkernel(vm_offset_t addr)
2100 vm_paddr_t ptppaddr;
2104 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2105 addr = roundup2(addr, NBPDR);
2106 if (addr - 1 >= kernel_map->max_offset)
2107 addr = kernel_map->max_offset;
2108 while (kernel_vm_end < addr) {
2109 if (pdir_pde(PTD, kernel_vm_end)) {
2110 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
2111 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2112 kernel_vm_end = kernel_map->max_offset;
2118 nkpg = vm_page_alloc(NULL, kernel_vm_end >> PDRSHIFT,
2119 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
2122 panic("pmap_growkernel: no memory to grow kernel");
2126 if ((nkpg->flags & PG_ZERO) == 0)
2127 pmap_zero_page(nkpg);
2128 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2129 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2130 pdir_pde(KPTD, kernel_vm_end) = pgeflag | newpdir;
2132 pmap_kenter_pde(kernel_vm_end, newpdir);
2133 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
2134 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2135 kernel_vm_end = kernel_map->max_offset;
2142 /***************************************************
2143 * page management routines.
2144 ***************************************************/
2146 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
2147 CTASSERT(_NPCM == 11);
2149 static __inline struct pv_chunk *
2150 pv_to_chunk(pv_entry_t pv)
2153 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
2156 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
2158 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
2159 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
2161 static uint32_t pc_freemask[11] = {
2162 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2163 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2164 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2165 PC_FREE0_9, PC_FREE10
2168 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
2169 "Current number of pv entries");
2172 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2174 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
2175 "Current number of pv entry chunks");
2176 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
2177 "Current number of pv entry chunks allocated");
2178 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
2179 "Current number of pv entry chunks frees");
2180 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
2181 "Number of times tried to get a chunk page but failed.");
2183 static long pv_entry_frees, pv_entry_allocs;
2184 static int pv_entry_spare;
2186 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
2187 "Current number of pv entry frees");
2188 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
2189 "Current number of pv entry allocs");
2190 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
2191 "Current number of spare pv entries");
2193 static int pmap_collect_inactive, pmap_collect_active;
2195 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_inactive, CTLFLAG_RD, &pmap_collect_inactive, 0,
2196 "Current number times pmap_collect called on inactive queue");
2197 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_active, CTLFLAG_RD, &pmap_collect_active, 0,
2198 "Current number times pmap_collect called on active queue");
2202 * We are in a serious low memory condition. Resort to
2203 * drastic measures to free some pages so we can allocate
2204 * another pv entry chunk. This is normally called to
2205 * unmap inactive pages, and if necessary, active pages.
2208 pmap_collect(pmap_t locked_pmap, struct vpgqueues *vpq)
2212 pt_entry_t *pte, tpte;
2213 pv_entry_t next_pv, pv;
2218 TAILQ_FOREACH(m, &vpq->pl, pageq) {
2219 if ((m->flags & PG_MARKER) != 0 || m->hold_count || m->busy)
2221 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
2224 /* Avoid deadlock and lock recursion. */
2225 if (pmap > locked_pmap)
2227 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
2229 pmap->pm_stats.resident_count--;
2230 pde = pmap_pde(pmap, va);
2231 KASSERT((*pde & PG_PS) == 0, ("pmap_collect: found"
2232 " a 4mpage in page %p's pv list", m));
2233 pte = pmap_pte_quick(pmap, va);
2234 tpte = pte_load_clear(pte);
2235 KASSERT((tpte & PG_W) == 0,
2236 ("pmap_collect: wired pte %#jx", (uintmax_t)tpte));
2238 vm_page_aflag_set(m, PGA_REFERENCED);
2239 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2242 pmap_unuse_pt(pmap, va, &free);
2243 pmap_invalidate_page(pmap, va);
2244 pmap_free_zero_pages(free);
2245 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2246 free_pv_entry(pmap, pv);
2247 if (pmap != locked_pmap)
2250 if (TAILQ_EMPTY(&m->md.pv_list) &&
2251 TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list))
2252 vm_page_aflag_clear(m, PGA_WRITEABLE);
2259 * free the pv_entry back to the free list
2262 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2265 struct pv_chunk *pc;
2266 int idx, field, bit;
2268 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2269 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2270 PV_STAT(pv_entry_frees++);
2271 PV_STAT(pv_entry_spare++);
2273 pc = pv_to_chunk(pv);
2274 idx = pv - &pc->pc_pventry[0];
2277 pc->pc_map[field] |= 1ul << bit;
2278 /* move to head of list */
2279 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2280 for (idx = 0; idx < _NPCM; idx++)
2281 if (pc->pc_map[idx] != pc_freemask[idx]) {
2282 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2285 PV_STAT(pv_entry_spare -= _NPCPV);
2286 PV_STAT(pc_chunk_count--);
2287 PV_STAT(pc_chunk_frees++);
2288 /* entire chunk is free, return it */
2289 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2290 pmap_qremove((vm_offset_t)pc, 1);
2291 vm_page_unwire(m, 0);
2293 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2297 * get a new pv_entry, allocating a block from the system
2301 get_pv_entry(pmap_t pmap, int try)
2303 static const struct timeval printinterval = { 60, 0 };
2304 static struct timeval lastprint;
2305 static vm_pindex_t colour;
2306 struct vpgqueues *pq;
2309 struct pv_chunk *pc;
2312 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2313 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2314 PV_STAT(pv_entry_allocs++);
2316 if (pv_entry_count > pv_entry_high_water)
2317 if (ratecheck(&lastprint, &printinterval))
2318 printf("Approaching the limit on PV entries, consider "
2319 "increasing either the vm.pmap.shpgperproc or the "
2320 "vm.pmap.pv_entry_max tunable.\n");
2323 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2325 for (field = 0; field < _NPCM; field++) {
2326 if (pc->pc_map[field]) {
2327 bit = bsfl(pc->pc_map[field]);
2331 if (field < _NPCM) {
2332 pv = &pc->pc_pventry[field * 32 + bit];
2333 pc->pc_map[field] &= ~(1ul << bit);
2334 /* If this was the last item, move it to tail */
2335 for (field = 0; field < _NPCM; field++)
2336 if (pc->pc_map[field] != 0) {
2337 PV_STAT(pv_entry_spare--);
2338 return (pv); /* not full, return */
2340 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2341 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2342 PV_STAT(pv_entry_spare--);
2347 * Access to the ptelist "pv_vafree" is synchronized by the page
2348 * queues lock. If "pv_vafree" is currently non-empty, it will
2349 * remain non-empty until pmap_ptelist_alloc() completes.
2351 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, colour, (pq ==
2352 &vm_page_queues[PQ_ACTIVE] ? VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL) |
2353 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
2356 PV_STAT(pc_chunk_tryfail++);
2360 * Reclaim pv entries: At first, destroy mappings to
2361 * inactive pages. After that, if a pv chunk entry
2362 * is still needed, destroy mappings to active pages.
2365 PV_STAT(pmap_collect_inactive++);
2366 pq = &vm_page_queues[PQ_INACTIVE];
2367 } else if (pq == &vm_page_queues[PQ_INACTIVE]) {
2368 PV_STAT(pmap_collect_active++);
2369 pq = &vm_page_queues[PQ_ACTIVE];
2371 panic("get_pv_entry: increase vm.pmap.shpgperproc");
2372 pmap_collect(pmap, pq);
2375 PV_STAT(pc_chunk_count++);
2376 PV_STAT(pc_chunk_allocs++);
2378 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
2379 pmap_qenter((vm_offset_t)pc, &m, 1);
2381 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
2382 for (field = 1; field < _NPCM; field++)
2383 pc->pc_map[field] = pc_freemask[field];
2384 pv = &pc->pc_pventry[0];
2385 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2386 PV_STAT(pv_entry_spare += _NPCPV - 1);
2390 static __inline pv_entry_t
2391 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2395 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2396 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
2397 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2398 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
2406 pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2408 struct md_page *pvh;
2410 vm_offset_t va_last;
2413 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2414 KASSERT((pa & PDRMASK) == 0,
2415 ("pmap_pv_demote_pde: pa is not 4mpage aligned"));
2418 * Transfer the 4mpage's pv entry for this mapping to the first
2421 pvh = pa_to_pvh(pa);
2422 va = trunc_4mpage(va);
2423 pv = pmap_pvh_remove(pvh, pmap, va);
2424 KASSERT(pv != NULL, ("pmap_pv_demote_pde: pv not found"));
2425 m = PHYS_TO_VM_PAGE(pa);
2426 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2427 /* Instantiate the remaining NPTEPG - 1 pv entries. */
2428 va_last = va + NBPDR - PAGE_SIZE;
2431 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2432 ("pmap_pv_demote_pde: page %p is not managed", m));
2434 pmap_insert_entry(pmap, va, m);
2435 } while (va < va_last);
2439 pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2441 struct md_page *pvh;
2443 vm_offset_t va_last;
2446 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2447 KASSERT((pa & PDRMASK) == 0,
2448 ("pmap_pv_promote_pde: pa is not 4mpage aligned"));
2451 * Transfer the first page's pv entry for this mapping to the
2452 * 4mpage's pv list. Aside from avoiding the cost of a call
2453 * to get_pv_entry(), a transfer avoids the possibility that
2454 * get_pv_entry() calls pmap_collect() and that pmap_collect()
2455 * removes one of the mappings that is being promoted.
2457 m = PHYS_TO_VM_PAGE(pa);
2458 va = trunc_4mpage(va);
2459 pv = pmap_pvh_remove(&m->md, pmap, va);
2460 KASSERT(pv != NULL, ("pmap_pv_promote_pde: pv not found"));
2461 pvh = pa_to_pvh(pa);
2462 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_list);
2463 /* Free the remaining NPTEPG - 1 pv entries. */
2464 va_last = va + NBPDR - PAGE_SIZE;
2468 pmap_pvh_free(&m->md, pmap, va);
2469 } while (va < va_last);
2473 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2477 pv = pmap_pvh_remove(pvh, pmap, va);
2478 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2479 free_pv_entry(pmap, pv);
2483 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
2485 struct md_page *pvh;
2487 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2488 pmap_pvh_free(&m->md, pmap, va);
2489 if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
2490 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2491 if (TAILQ_EMPTY(&pvh->pv_list))
2492 vm_page_aflag_clear(m, PGA_WRITEABLE);
2497 * Create a pv entry for page at pa for
2501 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2505 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2506 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2507 pv = get_pv_entry(pmap, FALSE);
2509 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2513 * Conditionally create a pv entry.
2516 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2520 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2521 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2522 if (pv_entry_count < pv_entry_high_water &&
2523 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2525 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2532 * Create the pv entries for each of the pages within a superpage.
2535 pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
2537 struct md_page *pvh;
2540 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2541 if (pv_entry_count < pv_entry_high_water &&
2542 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2544 pvh = pa_to_pvh(pa);
2545 TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_list);
2552 * Fills a page table page with mappings to consecutive physical pages.
2555 pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte)
2559 for (pte = firstpte; pte < firstpte + NPTEPG; pte++) {
2561 newpte += PAGE_SIZE;
2566 * Tries to demote a 2- or 4MB page mapping. If demotion fails, the
2567 * 2- or 4MB page mapping is invalidated.
2570 pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
2572 pd_entry_t newpde, oldpde;
2573 pt_entry_t *firstpte, newpte;
2575 vm_page_t free, mpte;
2577 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2579 KASSERT((oldpde & (PG_PS | PG_V)) == (PG_PS | PG_V),
2580 ("pmap_demote_pde: oldpde is missing PG_PS and/or PG_V"));
2581 mpte = pmap_lookup_pt_page(pmap, va);
2583 pmap_remove_pt_page(pmap, mpte);
2585 KASSERT((oldpde & PG_W) == 0,
2586 ("pmap_demote_pde: page table page for a wired mapping"
2590 * Invalidate the 2- or 4MB page mapping and return
2591 * "failure" if the mapping was never accessed or the
2592 * allocation of the new page table page fails.
2594 if ((oldpde & PG_A) == 0 || (mpte = vm_page_alloc(NULL,
2595 va >> PDRSHIFT, VM_ALLOC_NOOBJ | VM_ALLOC_NORMAL |
2596 VM_ALLOC_WIRED)) == NULL) {
2598 pmap_remove_pde(pmap, pde, trunc_4mpage(va), &free);
2599 pmap_invalidate_page(pmap, trunc_4mpage(va));
2600 pmap_free_zero_pages(free);
2601 CTR2(KTR_PMAP, "pmap_demote_pde: failure for va %#x"
2602 " in pmap %p", va, pmap);
2605 if (va < VM_MAXUSER_ADDRESS)
2606 pmap->pm_stats.resident_count++;
2608 mptepa = VM_PAGE_TO_PHYS(mpte);
2611 * If the page mapping is in the kernel's address space, then the
2612 * KPTmap can provide access to the page table page. Otherwise,
2613 * temporarily map the page table page (mpte) into the kernel's
2614 * address space at either PADDR1 or PADDR2.
2617 firstpte = &KPTmap[i386_btop(trunc_4mpage(va))];
2618 else if (curthread->td_pinned > 0 && mtx_owned(&vm_page_queue_mtx)) {
2619 if ((*PMAP1 & PG_FRAME) != mptepa) {
2620 *PMAP1 = mptepa | PG_RW | PG_V | PG_A | PG_M;
2622 PMAP1cpu = PCPU_GET(cpuid);
2628 if (PMAP1cpu != PCPU_GET(cpuid)) {
2629 PMAP1cpu = PCPU_GET(cpuid);
2637 mtx_lock(&PMAP2mutex);
2638 if ((*PMAP2 & PG_FRAME) != mptepa) {
2639 *PMAP2 = mptepa | PG_RW | PG_V | PG_A | PG_M;
2640 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR2);
2644 newpde = mptepa | PG_M | PG_A | (oldpde & PG_U) | PG_RW | PG_V;
2645 KASSERT((oldpde & PG_A) != 0,
2646 ("pmap_demote_pde: oldpde is missing PG_A"));
2647 KASSERT((oldpde & (PG_M | PG_RW)) != PG_RW,
2648 ("pmap_demote_pde: oldpde is missing PG_M"));
2649 newpte = oldpde & ~PG_PS;
2650 if ((newpte & PG_PDE_PAT) != 0)
2651 newpte ^= PG_PDE_PAT | PG_PTE_PAT;
2654 * If the page table page is new, initialize it.
2656 if (mpte->wire_count == 1) {
2657 mpte->wire_count = NPTEPG;
2658 pmap_fill_ptp(firstpte, newpte);
2660 KASSERT((*firstpte & PG_FRAME) == (newpte & PG_FRAME),
2661 ("pmap_demote_pde: firstpte and newpte map different physical"
2665 * If the mapping has changed attributes, update the page table
2668 if ((*firstpte & PG_PTE_PROMOTE) != (newpte & PG_PTE_PROMOTE))
2669 pmap_fill_ptp(firstpte, newpte);
2672 * Demote the mapping. This pmap is locked. The old PDE has
2673 * PG_A set. If the old PDE has PG_RW set, it also has PG_M
2674 * set. Thus, there is no danger of a race with another
2675 * processor changing the setting of PG_A and/or PG_M between
2676 * the read above and the store below.
2678 if (workaround_erratum383)
2679 pmap_update_pde(pmap, va, pde, newpde);
2680 else if (pmap == kernel_pmap)
2681 pmap_kenter_pde(va, newpde);
2683 pde_store(pde, newpde);
2684 if (firstpte == PADDR2)
2685 mtx_unlock(&PMAP2mutex);
2688 * Invalidate the recursive mapping of the page table page.
2690 pmap_invalidate_page(pmap, (vm_offset_t)vtopte(va));
2693 * Demote the pv entry. This depends on the earlier demotion
2694 * of the mapping. Specifically, the (re)creation of a per-
2695 * page pv entry might trigger the execution of pmap_collect(),
2696 * which might reclaim a newly (re)created per-page pv entry
2697 * and destroy the associated mapping. In order to destroy
2698 * the mapping, the PDE must have already changed from mapping
2699 * the 2mpage to referencing the page table page.
2701 if ((oldpde & PG_MANAGED) != 0)
2702 pmap_pv_demote_pde(pmap, va, oldpde & PG_PS_FRAME);
2704 pmap_pde_demotions++;
2705 CTR2(KTR_PMAP, "pmap_demote_pde: success for va %#x"
2706 " in pmap %p", va, pmap);
2711 * pmap_remove_pde: do the things to unmap a superpage in a process
2714 pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
2717 struct md_page *pvh;
2719 vm_offset_t eva, va;
2722 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2723 KASSERT((sva & PDRMASK) == 0,
2724 ("pmap_remove_pde: sva is not 4mpage aligned"));
2725 oldpde = pte_load_clear(pdq);
2727 pmap->pm_stats.wired_count -= NBPDR / PAGE_SIZE;
2730 * Machines that don't support invlpg, also don't support
2734 pmap_invalidate_page(kernel_pmap, sva);
2735 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2736 if (oldpde & PG_MANAGED) {
2737 pvh = pa_to_pvh(oldpde & PG_PS_FRAME);
2738 pmap_pvh_free(pvh, pmap, sva);
2740 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
2741 va < eva; va += PAGE_SIZE, m++) {
2742 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
2745 vm_page_aflag_set(m, PGA_REFERENCED);
2746 if (TAILQ_EMPTY(&m->md.pv_list) &&
2747 TAILQ_EMPTY(&pvh->pv_list))
2748 vm_page_aflag_clear(m, PGA_WRITEABLE);
2751 if (pmap == kernel_pmap) {
2752 if (!pmap_demote_pde(pmap, pdq, sva))
2753 panic("pmap_remove_pde: failed demotion");
2755 mpte = pmap_lookup_pt_page(pmap, sva);
2757 pmap_remove_pt_page(pmap, mpte);
2758 pmap->pm_stats.resident_count--;
2759 KASSERT(mpte->wire_count == NPTEPG,
2760 ("pmap_remove_pde: pte page wire count error"));
2761 mpte->wire_count = 0;
2762 pmap_add_delayed_free_list(mpte, free, FALSE);
2763 atomic_subtract_int(&cnt.v_wire_count, 1);
2769 * pmap_remove_pte: do the things to unmap a page in a process
2772 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va, vm_page_t *free)
2777 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2778 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2779 oldpte = pte_load_clear(ptq);
2781 pmap->pm_stats.wired_count -= 1;
2783 * Machines that don't support invlpg, also don't support
2787 pmap_invalidate_page(kernel_pmap, va);
2788 pmap->pm_stats.resident_count -= 1;
2789 if (oldpte & PG_MANAGED) {
2790 m = PHYS_TO_VM_PAGE(oldpte & PG_FRAME);
2791 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2794 vm_page_aflag_set(m, PGA_REFERENCED);
2795 pmap_remove_entry(pmap, m, va);
2797 return (pmap_unuse_pt(pmap, va, free));
2801 * Remove a single page from a process address space
2804 pmap_remove_page(pmap_t pmap, vm_offset_t va, vm_page_t *free)
2808 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2809 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
2810 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2811 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
2813 pmap_remove_pte(pmap, pte, va, free);
2814 pmap_invalidate_page(pmap, va);
2818 * Remove the given range of addresses from the specified map.
2820 * It is assumed that the start and end are properly
2821 * rounded to the page size.
2824 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2829 vm_page_t free = NULL;
2833 * Perform an unsynchronized read. This is, however, safe.
2835 if (pmap->pm_stats.resident_count == 0)
2840 vm_page_lock_queues();
2845 * special handling of removing one page. a very
2846 * common operation and easy to short circuit some
2849 if ((sva + PAGE_SIZE == eva) &&
2850 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
2851 pmap_remove_page(pmap, sva, &free);
2855 for (; sva < eva; sva = pdnxt) {
2859 * Calculate index for next page table.
2861 pdnxt = (sva + NBPDR) & ~PDRMASK;
2864 if (pmap->pm_stats.resident_count == 0)
2867 pdirindex = sva >> PDRSHIFT;
2868 ptpaddr = pmap->pm_pdir[pdirindex];
2871 * Weed out invalid mappings. Note: we assume that the page
2872 * directory table is always allocated, and in kernel virtual.
2878 * Check for large page.
2880 if ((ptpaddr & PG_PS) != 0) {
2882 * Are we removing the entire large page? If not,
2883 * demote the mapping and fall through.
2885 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
2887 * The TLB entry for a PG_G mapping is
2888 * invalidated by pmap_remove_pde().
2890 if ((ptpaddr & PG_G) == 0)
2892 pmap_remove_pde(pmap,
2893 &pmap->pm_pdir[pdirindex], sva, &free);
2895 } else if (!pmap_demote_pde(pmap,
2896 &pmap->pm_pdir[pdirindex], sva)) {
2897 /* The large page mapping was destroyed. */
2903 * Limit our scan to either the end of the va represented
2904 * by the current page table page, or to the end of the
2905 * range being removed.
2910 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2916 * The TLB entry for a PG_G mapping is invalidated
2917 * by pmap_remove_pte().
2919 if ((*pte & PG_G) == 0)
2921 if (pmap_remove_pte(pmap, pte, sva, &free))
2928 pmap_invalidate_all(pmap);
2929 vm_page_unlock_queues();
2931 pmap_free_zero_pages(free);
2935 * Routine: pmap_remove_all
2937 * Removes this physical page from
2938 * all physical maps in which it resides.
2939 * Reflects back modify bits to the pager.
2942 * Original versions of this routine were very
2943 * inefficient because they iteratively called
2944 * pmap_remove (slow...)
2948 pmap_remove_all(vm_page_t m)
2950 struct md_page *pvh;
2953 pt_entry_t *pte, tpte;
2958 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2959 ("pmap_remove_all: page %p is not managed", m));
2961 vm_page_lock_queues();
2963 if ((m->flags & PG_FICTITIOUS) != 0)
2964 goto small_mappings;
2965 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2966 while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
2970 pde = pmap_pde(pmap, va);
2971 (void)pmap_demote_pde(pmap, pde, va);
2975 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2978 pmap->pm_stats.resident_count--;
2979 pde = pmap_pde(pmap, pv->pv_va);
2980 KASSERT((*pde & PG_PS) == 0, ("pmap_remove_all: found"
2981 " a 4mpage in page %p's pv list", m));
2982 pte = pmap_pte_quick(pmap, pv->pv_va);
2983 tpte = pte_load_clear(pte);
2985 pmap->pm_stats.wired_count--;
2987 vm_page_aflag_set(m, PGA_REFERENCED);
2990 * Update the vm_page_t clean and reference bits.
2992 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2994 pmap_unuse_pt(pmap, pv->pv_va, &free);
2995 pmap_invalidate_page(pmap, pv->pv_va);
2996 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2997 free_pv_entry(pmap, pv);
3000 vm_page_aflag_clear(m, PGA_WRITEABLE);
3002 vm_page_unlock_queues();
3003 pmap_free_zero_pages(free);
3007 * pmap_protect_pde: do the things to protect a 4mpage in a process
3010 pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva, vm_prot_t prot)
3012 pd_entry_t newpde, oldpde;
3013 vm_offset_t eva, va;
3015 boolean_t anychanged;
3017 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3018 KASSERT((sva & PDRMASK) == 0,
3019 ("pmap_protect_pde: sva is not 4mpage aligned"));
3022 oldpde = newpde = *pde;
3023 if (oldpde & PG_MANAGED) {
3025 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
3026 va < eva; va += PAGE_SIZE, m++)
3027 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
3030 if ((prot & VM_PROT_WRITE) == 0)
3031 newpde &= ~(PG_RW | PG_M);
3033 if ((prot & VM_PROT_EXECUTE) == 0)
3036 if (newpde != oldpde) {
3037 if (!pde_cmpset(pde, oldpde, newpde))
3040 pmap_invalidate_page(pmap, sva);
3044 return (anychanged);
3048 * Set the physical protection on the
3049 * specified range of this map as requested.
3052 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3059 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
3060 pmap_remove(pmap, sva, eva);
3065 if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) ==
3066 (VM_PROT_WRITE|VM_PROT_EXECUTE))
3069 if (prot & VM_PROT_WRITE)
3075 vm_page_lock_queues();
3078 for (; sva < eva; sva = pdnxt) {
3079 pt_entry_t obits, pbits;
3082 pdnxt = (sva + NBPDR) & ~PDRMASK;
3086 pdirindex = sva >> PDRSHIFT;
3087 ptpaddr = pmap->pm_pdir[pdirindex];
3090 * Weed out invalid mappings. Note: we assume that the page
3091 * directory table is always allocated, and in kernel virtual.
3097 * Check for large page.
3099 if ((ptpaddr & PG_PS) != 0) {
3101 * Are we protecting the entire large page? If not,
3102 * demote the mapping and fall through.
3104 if (sva + NBPDR == pdnxt && eva >= pdnxt) {
3106 * The TLB entry for a PG_G mapping is
3107 * invalidated by pmap_protect_pde().
3109 if (pmap_protect_pde(pmap,
3110 &pmap->pm_pdir[pdirindex], sva, prot))
3113 } else if (!pmap_demote_pde(pmap,
3114 &pmap->pm_pdir[pdirindex], sva)) {
3115 /* The large page mapping was destroyed. */
3123 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
3129 * Regardless of whether a pte is 32 or 64 bits in
3130 * size, PG_RW, PG_A, and PG_M are among the least
3131 * significant 32 bits.
3133 obits = pbits = *pte;
3134 if ((pbits & PG_V) == 0)
3137 if ((prot & VM_PROT_WRITE) == 0) {
3138 if ((pbits & (PG_MANAGED | PG_M | PG_RW)) ==
3139 (PG_MANAGED | PG_M | PG_RW)) {
3140 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
3143 pbits &= ~(PG_RW | PG_M);
3146 if ((prot & VM_PROT_EXECUTE) == 0)
3150 if (pbits != obits) {
3152 if (!atomic_cmpset_64(pte, obits, pbits))
3155 if (!atomic_cmpset_int((u_int *)pte, obits,
3160 pmap_invalidate_page(pmap, sva);
3168 pmap_invalidate_all(pmap);
3169 vm_page_unlock_queues();
3174 * Tries to promote the 512 or 1024, contiguous 4KB page mappings that are
3175 * within a single page table page (PTP) to a single 2- or 4MB page mapping.
3176 * For promotion to occur, two conditions must be met: (1) the 4KB page
3177 * mappings must map aligned, contiguous physical memory and (2) the 4KB page
3178 * mappings must have identical characteristics.
3180 * Managed (PG_MANAGED) mappings within the kernel address space are not
3181 * promoted. The reason is that kernel PDEs are replicated in each pmap but
3182 * pmap_clear_ptes() and pmap_ts_referenced() only read the PDE from the kernel
3186 pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
3189 pt_entry_t *firstpte, oldpte, pa, *pte;
3190 vm_offset_t oldpteva;
3193 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3196 * Examine the first PTE in the specified PTP. Abort if this PTE is
3197 * either invalid, unused, or does not map the first 4KB physical page
3198 * within a 2- or 4MB page.
3200 firstpte = pmap_pte_quick(pmap, trunc_4mpage(va));
3203 if ((newpde & ((PG_FRAME & PDRMASK) | PG_A | PG_V)) != (PG_A | PG_V)) {
3204 pmap_pde_p_failures++;
3205 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3206 " in pmap %p", va, pmap);
3209 if ((*firstpte & PG_MANAGED) != 0 && pmap == kernel_pmap) {
3210 pmap_pde_p_failures++;
3211 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3212 " in pmap %p", va, pmap);
3215 if ((newpde & (PG_M | PG_RW)) == PG_RW) {
3217 * When PG_M is already clear, PG_RW can be cleared without
3218 * a TLB invalidation.
3220 if (!atomic_cmpset_int((u_int *)firstpte, newpde, newpde &
3227 * Examine each of the other PTEs in the specified PTP. Abort if this
3228 * PTE maps an unexpected 4KB physical page or does not have identical
3229 * characteristics to the first PTE.
3231 pa = (newpde & (PG_PS_FRAME | PG_A | PG_V)) + NBPDR - PAGE_SIZE;
3232 for (pte = firstpte + NPTEPG - 1; pte > firstpte; pte--) {
3235 if ((oldpte & (PG_FRAME | PG_A | PG_V)) != pa) {
3236 pmap_pde_p_failures++;
3237 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3238 " in pmap %p", va, pmap);
3241 if ((oldpte & (PG_M | PG_RW)) == PG_RW) {
3243 * When PG_M is already clear, PG_RW can be cleared
3244 * without a TLB invalidation.
3246 if (!atomic_cmpset_int((u_int *)pte, oldpte,
3250 oldpteva = (oldpte & PG_FRAME & PDRMASK) |
3252 CTR2(KTR_PMAP, "pmap_promote_pde: protect for va %#x"
3253 " in pmap %p", oldpteva, pmap);
3255 if ((oldpte & PG_PTE_PROMOTE) != (newpde & PG_PTE_PROMOTE)) {
3256 pmap_pde_p_failures++;
3257 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#x"
3258 " in pmap %p", va, pmap);
3265 * Save the page table page in its current state until the PDE
3266 * mapping the superpage is demoted by pmap_demote_pde() or
3267 * destroyed by pmap_remove_pde().
3269 mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
3270 KASSERT(mpte >= vm_page_array &&
3271 mpte < &vm_page_array[vm_page_array_size],
3272 ("pmap_promote_pde: page table page is out of range"));
3273 KASSERT(mpte->pindex == va >> PDRSHIFT,
3274 ("pmap_promote_pde: page table page's pindex is wrong"));
3275 pmap_insert_pt_page(pmap, mpte);
3278 * Promote the pv entries.
3280 if ((newpde & PG_MANAGED) != 0)
3281 pmap_pv_promote_pde(pmap, va, newpde & PG_PS_FRAME);
3284 * Propagate the PAT index to its proper position.
3286 if ((newpde & PG_PTE_PAT) != 0)
3287 newpde ^= PG_PDE_PAT | PG_PTE_PAT;
3290 * Map the superpage.
3292 if (workaround_erratum383)
3293 pmap_update_pde(pmap, va, pde, PG_PS | newpde);
3294 else if (pmap == kernel_pmap)
3295 pmap_kenter_pde(va, PG_PS | newpde);
3297 pde_store(pde, PG_PS | newpde);
3299 pmap_pde_promotions++;
3300 CTR2(KTR_PMAP, "pmap_promote_pde: success for va %#x"
3301 " in pmap %p", va, pmap);
3305 * Insert the given physical page (p) at
3306 * the specified virtual address (v) in the
3307 * target physical map with the protection requested.
3309 * If specified, the page will be wired down, meaning
3310 * that the related pte can not be reclaimed.
3312 * NB: This is the only routine which MAY NOT lazy-evaluate
3313 * or lose information. That is, this routine must actually
3314 * insert this page into the given map NOW.
3317 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
3318 vm_prot_t prot, boolean_t wired)
3322 pt_entry_t newpte, origpte;
3328 va = trunc_page(va);
3329 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
3330 KASSERT(va < UPT_MIN_ADDRESS || va >= UPT_MAX_ADDRESS,
3331 ("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)",
3333 KASSERT((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) != 0 ||
3334 VM_OBJECT_LOCKED(m->object),
3335 ("pmap_enter: page %p is not busy", m));
3339 vm_page_lock_queues();
3344 * In the case that a page table page is not
3345 * resident, we are creating it here.
3347 if (va < VM_MAXUSER_ADDRESS) {
3348 mpte = pmap_allocpte(pmap, va, M_WAITOK);
3351 pde = pmap_pde(pmap, va);
3352 if ((*pde & PG_PS) != 0)
3353 panic("pmap_enter: attempted pmap_enter on 4MB page");
3354 pte = pmap_pte_quick(pmap, va);
3357 * Page Directory table entry not valid, we need a new PT page
3360 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x",
3361 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
3364 pa = VM_PAGE_TO_PHYS(m);
3367 opa = origpte & PG_FRAME;
3370 * Mapping has not changed, must be protection or wiring change.
3372 if (origpte && (opa == pa)) {
3374 * Wiring change, just update stats. We don't worry about
3375 * wiring PT pages as they remain resident as long as there
3376 * are valid mappings in them. Hence, if a user page is wired,
3377 * the PT page will be also.
3379 if (wired && ((origpte & PG_W) == 0))
3380 pmap->pm_stats.wired_count++;
3381 else if (!wired && (origpte & PG_W))
3382 pmap->pm_stats.wired_count--;
3385 * Remove extra pte reference
3390 if (origpte & PG_MANAGED) {
3400 * Mapping has changed, invalidate old range and fall through to
3401 * handle validating new mapping.
3405 pmap->pm_stats.wired_count--;
3406 if (origpte & PG_MANAGED) {
3407 om = PHYS_TO_VM_PAGE(opa);
3408 pv = pmap_pvh_remove(&om->md, pmap, va);
3412 KASSERT(mpte->wire_count > 0,
3413 ("pmap_enter: missing reference to page table page,"
3417 pmap->pm_stats.resident_count++;
3420 * Enter on the PV list if part of our managed memory.
3422 if ((m->oflags & VPO_UNMANAGED) == 0) {
3423 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
3424 ("pmap_enter: managed mapping within the clean submap"));
3426 pv = get_pv_entry(pmap, FALSE);
3428 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3430 } else if (pv != NULL)
3431 free_pv_entry(pmap, pv);
3434 * Increment counters
3437 pmap->pm_stats.wired_count++;
3441 * Now validate mapping with desired protection/wiring.
3443 newpte = (pt_entry_t)(pa | pmap_cache_bits(m->md.pat_mode, 0) | PG_V);
3444 if ((prot & VM_PROT_WRITE) != 0) {
3446 if ((newpte & PG_MANAGED) != 0)
3447 vm_page_aflag_set(m, PGA_WRITEABLE);
3450 if ((prot & VM_PROT_EXECUTE) == 0)
3455 if (va < VM_MAXUSER_ADDRESS)
3457 if (pmap == kernel_pmap)
3461 * if the mapping or permission bits are different, we need
3462 * to update the pte.
3464 if ((origpte & ~(PG_M|PG_A)) != newpte) {
3466 if ((access & VM_PROT_WRITE) != 0)
3468 if (origpte & PG_V) {
3470 origpte = pte_load_store(pte, newpte);
3471 if (origpte & PG_A) {
3472 if (origpte & PG_MANAGED)
3473 vm_page_aflag_set(om, PGA_REFERENCED);
3474 if (opa != VM_PAGE_TO_PHYS(m))
3477 if ((origpte & PG_NX) == 0 &&
3478 (newpte & PG_NX) != 0)
3482 if ((origpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
3483 if ((origpte & PG_MANAGED) != 0)
3485 if ((prot & VM_PROT_WRITE) == 0)
3488 if ((origpte & PG_MANAGED) != 0 &&
3489 TAILQ_EMPTY(&om->md.pv_list) &&
3490 ((om->flags & PG_FICTITIOUS) != 0 ||
3491 TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
3492 vm_page_aflag_clear(om, PGA_WRITEABLE);
3494 pmap_invalidate_page(pmap, va);
3496 pte_store(pte, newpte);
3500 * If both the page table page and the reservation are fully
3501 * populated, then attempt promotion.
3503 if ((mpte == NULL || mpte->wire_count == NPTEPG) &&
3504 pg_ps_enabled && (m->flags & PG_FICTITIOUS) == 0 &&
3505 vm_reserv_level_iffullpop(m) == 0)
3506 pmap_promote_pde(pmap, pde, va);
3509 vm_page_unlock_queues();
3514 * Tries to create a 2- or 4MB page mapping. Returns TRUE if successful and
3515 * FALSE otherwise. Fails if (1) a page table page cannot be allocated without
3516 * blocking, (2) a mapping already exists at the specified virtual address, or
3517 * (3) a pv entry cannot be allocated without reclaiming another pv entry.
3520 pmap_enter_pde(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3522 pd_entry_t *pde, newpde;
3524 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3525 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3526 pde = pmap_pde(pmap, va);
3528 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3529 " in pmap %p", va, pmap);
3532 newpde = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.pat_mode, 1) |
3534 if ((m->oflags & VPO_UNMANAGED) == 0) {
3535 newpde |= PG_MANAGED;
3538 * Abort this mapping if its PV entry could not be created.
3540 if (!pmap_pv_insert_pde(pmap, va, VM_PAGE_TO_PHYS(m))) {
3541 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
3542 " in pmap %p", va, pmap);
3547 if ((prot & VM_PROT_EXECUTE) == 0)
3550 if (va < VM_MAXUSER_ADDRESS)
3554 * Increment counters.
3556 pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3559 * Map the superpage.
3561 pde_store(pde, newpde);
3563 pmap_pde_mappings++;
3564 CTR2(KTR_PMAP, "pmap_enter_pde: success for va %#lx"
3565 " in pmap %p", va, pmap);
3570 * Maps a sequence of resident pages belonging to the same object.
3571 * The sequence begins with the given page m_start. This page is
3572 * mapped at the given virtual address start. Each subsequent page is
3573 * mapped at a virtual address that is offset from start by the same
3574 * amount as the page is offset from m_start within the object. The
3575 * last page in the sequence is the page with the largest offset from
3576 * m_start that can be mapped at a virtual address less than the given
3577 * virtual address end. Not every virtual page between start and end
3578 * is mapped; only those for which a resident page exists with the
3579 * corresponding offset from m_start are mapped.
3582 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3583 vm_page_t m_start, vm_prot_t prot)
3587 vm_pindex_t diff, psize;
3589 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
3590 psize = atop(end - start);
3593 vm_page_lock_queues();
3595 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3596 va = start + ptoa(diff);
3597 if ((va & PDRMASK) == 0 && va + NBPDR <= end &&
3598 (VM_PAGE_TO_PHYS(m) & PDRMASK) == 0 &&
3599 pg_ps_enabled && vm_reserv_level_iffullpop(m) == 0 &&
3600 pmap_enter_pde(pmap, va, m, prot))
3601 m = &m[NBPDR / PAGE_SIZE - 1];
3603 mpte = pmap_enter_quick_locked(pmap, va, m, prot,
3605 m = TAILQ_NEXT(m, listq);
3607 vm_page_unlock_queues();
3612 * this code makes some *MAJOR* assumptions:
3613 * 1. Current pmap & pmap exists.
3616 * 4. No page table pages.
3617 * but is *MUCH* faster than pmap_enter...
3621 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3624 vm_page_lock_queues();
3626 (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
3627 vm_page_unlock_queues();
3632 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
3633 vm_prot_t prot, vm_page_t mpte)
3639 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
3640 (m->oflags & VPO_UNMANAGED) != 0,
3641 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
3642 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3643 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3646 * In the case that a page table page is not
3647 * resident, we are creating it here.
3649 if (va < VM_MAXUSER_ADDRESS) {
3654 * Calculate pagetable page index
3656 ptepindex = va >> PDRSHIFT;
3657 if (mpte && (mpte->pindex == ptepindex)) {
3661 * Get the page directory entry
3663 ptepa = pmap->pm_pdir[ptepindex];
3666 * If the page table page is mapped, we just increment
3667 * the hold count, and activate it.
3672 mpte = PHYS_TO_VM_PAGE(ptepa & PG_FRAME);
3675 mpte = _pmap_allocpte(pmap, ptepindex,
3686 * This call to vtopte makes the assumption that we are
3687 * entering the page into the current pmap. In order to support
3688 * quick entry into any pmap, one would likely use pmap_pte_quick.
3689 * But that isn't as quick as vtopte.
3701 * Enter on the PV list if part of our managed memory.
3703 if ((m->oflags & VPO_UNMANAGED) == 0 &&
3704 !pmap_try_insert_pv_entry(pmap, va, m)) {
3707 if (pmap_unwire_pte_hold(pmap, mpte, &free)) {
3708 pmap_invalidate_page(pmap, va);
3709 pmap_free_zero_pages(free);
3718 * Increment counters
3720 pmap->pm_stats.resident_count++;
3722 pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(m->md.pat_mode, 0);
3724 if ((prot & VM_PROT_EXECUTE) == 0)
3729 * Now validate mapping with RO protection
3731 if ((m->oflags & VPO_UNMANAGED) != 0)
3732 pte_store(pte, pa | PG_V | PG_U);
3734 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
3739 * Make a temporary mapping for a physical address. This is only intended
3740 * to be used for panic dumps.
3743 pmap_kenter_temporary(vm_paddr_t pa, int i)
3747 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
3748 pmap_kenter(va, pa);
3750 return ((void *)crashdumpmap);
3754 * This code maps large physical mmap regions into the
3755 * processor address space. Note that some shortcuts
3756 * are taken, but the code works.
3759 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3760 vm_pindex_t pindex, vm_size_t size)
3763 vm_paddr_t pa, ptepa;
3767 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3768 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3769 ("pmap_object_init_pt: non-device object"));
3771 (addr & (NBPDR - 1)) == 0 && (size & (NBPDR - 1)) == 0) {
3772 if (!vm_object_populate(object, pindex, pindex + atop(size)))
3774 p = vm_page_lookup(object, pindex);
3775 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3776 ("pmap_object_init_pt: invalid page %p", p));
3777 pat_mode = p->md.pat_mode;
3780 * Abort the mapping if the first page is not physically
3781 * aligned to a 2/4MB page boundary.
3783 ptepa = VM_PAGE_TO_PHYS(p);
3784 if (ptepa & (NBPDR - 1))
3788 * Skip the first page. Abort the mapping if the rest of
3789 * the pages are not physically contiguous or have differing
3790 * memory attributes.
3792 p = TAILQ_NEXT(p, listq);
3793 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size;
3795 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3796 ("pmap_object_init_pt: invalid page %p", p));
3797 if (pa != VM_PAGE_TO_PHYS(p) ||
3798 pat_mode != p->md.pat_mode)
3800 p = TAILQ_NEXT(p, listq);
3804 * Map using 2/4MB pages. Since "ptepa" is 2/4M aligned and
3805 * "size" is a multiple of 2/4M, adding the PAT setting to
3806 * "pa" will not affect the termination of this loop.
3809 for (pa = ptepa | pmap_cache_bits(pat_mode, 1); pa < ptepa +
3810 size; pa += NBPDR) {
3811 pde = pmap_pde(pmap, addr);
3813 pde_store(pde, pa | PG_PS | PG_M | PG_A |
3814 PG_U | PG_RW | PG_V);
3815 pmap->pm_stats.resident_count += NBPDR /
3817 pmap_pde_mappings++;
3819 /* Else continue on if the PDE is already valid. */
3827 * Routine: pmap_change_wiring
3828 * Function: Change the wiring attribute for a map/virtual-address
3830 * In/out conditions:
3831 * The mapping must already exist in the pmap.
3834 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3838 boolean_t are_queues_locked;
3840 are_queues_locked = FALSE;
3843 pde = pmap_pde(pmap, va);
3844 if ((*pde & PG_PS) != 0) {
3845 if (!wired != ((*pde & PG_W) == 0)) {
3846 if (!are_queues_locked) {
3847 are_queues_locked = TRUE;
3848 if (!mtx_trylock(&vm_page_queue_mtx)) {
3850 vm_page_lock_queues();
3854 if (!pmap_demote_pde(pmap, pde, va))
3855 panic("pmap_change_wiring: demotion failed");
3859 pte = pmap_pte(pmap, va);
3861 if (wired && !pmap_pte_w(pte))
3862 pmap->pm_stats.wired_count++;
3863 else if (!wired && pmap_pte_w(pte))
3864 pmap->pm_stats.wired_count--;
3867 * Wiring is not a hardware characteristic so there is no need to
3870 pmap_pte_set_w(pte, wired);
3871 pmap_pte_release(pte);
3873 if (are_queues_locked)
3874 vm_page_unlock_queues();
3881 * Copy the range specified by src_addr/len
3882 * from the source map to the range dst_addr/len
3883 * in the destination map.
3885 * This routine is only advisory and need not do anything.
3889 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
3890 vm_offset_t src_addr)
3894 vm_offset_t end_addr = src_addr + len;
3897 if (dst_addr != src_addr)
3900 if (!pmap_is_current(src_pmap))
3903 vm_page_lock_queues();
3904 if (dst_pmap < src_pmap) {
3905 PMAP_LOCK(dst_pmap);
3906 PMAP_LOCK(src_pmap);
3908 PMAP_LOCK(src_pmap);
3909 PMAP_LOCK(dst_pmap);
3912 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3913 pt_entry_t *src_pte, *dst_pte;
3914 vm_page_t dstmpte, srcmpte;
3915 pd_entry_t srcptepaddr;
3918 KASSERT(addr < UPT_MIN_ADDRESS,
3919 ("pmap_copy: invalid to pmap_copy page tables"));
3921 pdnxt = (addr + NBPDR) & ~PDRMASK;
3924 ptepindex = addr >> PDRSHIFT;
3926 srcptepaddr = src_pmap->pm_pdir[ptepindex];
3927 if (srcptepaddr == 0)
3930 if (srcptepaddr & PG_PS) {
3931 if (dst_pmap->pm_pdir[ptepindex] == 0 &&
3932 ((srcptepaddr & PG_MANAGED) == 0 ||
3933 pmap_pv_insert_pde(dst_pmap, addr, srcptepaddr &
3935 dst_pmap->pm_pdir[ptepindex] = srcptepaddr &
3937 dst_pmap->pm_stats.resident_count +=
3943 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr & PG_FRAME);
3944 KASSERT(srcmpte->wire_count > 0,
3945 ("pmap_copy: source page table page is unused"));
3947 if (pdnxt > end_addr)
3950 src_pte = vtopte(addr);
3951 while (addr < pdnxt) {
3955 * we only virtual copy managed pages
3957 if ((ptetemp & PG_MANAGED) != 0) {
3958 dstmpte = pmap_allocpte(dst_pmap, addr,
3960 if (dstmpte == NULL)
3962 dst_pte = pmap_pte_quick(dst_pmap, addr);
3963 if (*dst_pte == 0 &&
3964 pmap_try_insert_pv_entry(dst_pmap, addr,
3965 PHYS_TO_VM_PAGE(ptetemp & PG_FRAME))) {
3967 * Clear the wired, modified, and
3968 * accessed (referenced) bits
3971 *dst_pte = ptetemp & ~(PG_W | PG_M |
3973 dst_pmap->pm_stats.resident_count++;
3976 if (pmap_unwire_pte_hold(dst_pmap,
3978 pmap_invalidate_page(dst_pmap,
3980 pmap_free_zero_pages(free);
3984 if (dstmpte->wire_count >= srcmpte->wire_count)
3993 vm_page_unlock_queues();
3994 PMAP_UNLOCK(src_pmap);
3995 PMAP_UNLOCK(dst_pmap);
3998 static __inline void
3999 pagezero(void *page)
4001 #if defined(I686_CPU)
4002 if (cpu_class == CPUCLASS_686) {
4003 #if defined(CPU_ENABLE_SSE)
4004 if (cpu_feature & CPUID_SSE2)
4005 sse2_pagezero(page);
4008 i686_pagezero(page);
4011 bzero(page, PAGE_SIZE);
4015 * pmap_zero_page zeros the specified hardware page by mapping
4016 * the page into KVM and using bzero to clear its contents.
4019 pmap_zero_page(vm_page_t m)
4021 struct sysmaps *sysmaps;
4023 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4024 mtx_lock(&sysmaps->lock);
4025 if (*sysmaps->CMAP2)
4026 panic("pmap_zero_page: CMAP2 busy");
4028 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
4029 pmap_cache_bits(m->md.pat_mode, 0);
4030 invlcaddr(sysmaps->CADDR2);
4031 pagezero(sysmaps->CADDR2);
4032 *sysmaps->CMAP2 = 0;
4034 mtx_unlock(&sysmaps->lock);
4038 * pmap_zero_page_area zeros the specified hardware page by mapping
4039 * the page into KVM and using bzero to clear its contents.
4041 * off and size may not cover an area beyond a single hardware page.
4044 pmap_zero_page_area(vm_page_t m, int off, int size)
4046 struct sysmaps *sysmaps;
4048 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4049 mtx_lock(&sysmaps->lock);
4050 if (*sysmaps->CMAP2)
4051 panic("pmap_zero_page_area: CMAP2 busy");
4053 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
4054 pmap_cache_bits(m->md.pat_mode, 0);
4055 invlcaddr(sysmaps->CADDR2);
4056 if (off == 0 && size == PAGE_SIZE)
4057 pagezero(sysmaps->CADDR2);
4059 bzero((char *)sysmaps->CADDR2 + off, size);
4060 *sysmaps->CMAP2 = 0;
4062 mtx_unlock(&sysmaps->lock);
4066 * pmap_zero_page_idle zeros the specified hardware page by mapping
4067 * the page into KVM and using bzero to clear its contents. This
4068 * is intended to be called from the vm_pagezero process only and
4072 pmap_zero_page_idle(vm_page_t m)
4076 panic("pmap_zero_page_idle: CMAP3 busy");
4078 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M |
4079 pmap_cache_bits(m->md.pat_mode, 0);
4087 * pmap_copy_page copies the specified (machine independent)
4088 * page by mapping the page into virtual memory and using
4089 * bcopy to copy the page, one machine dependent page at a
4093 pmap_copy_page(vm_page_t src, vm_page_t dst)
4095 struct sysmaps *sysmaps;
4097 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4098 mtx_lock(&sysmaps->lock);
4099 if (*sysmaps->CMAP1)
4100 panic("pmap_copy_page: CMAP1 busy");
4101 if (*sysmaps->CMAP2)
4102 panic("pmap_copy_page: CMAP2 busy");
4104 invlpg((u_int)sysmaps->CADDR1);
4105 invlpg((u_int)sysmaps->CADDR2);
4106 *sysmaps->CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A |
4107 pmap_cache_bits(src->md.pat_mode, 0);
4108 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M |
4109 pmap_cache_bits(dst->md.pat_mode, 0);
4110 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
4111 *sysmaps->CMAP1 = 0;
4112 *sysmaps->CMAP2 = 0;
4114 mtx_unlock(&sysmaps->lock);
4118 * Returns true if the pmap's pv is one of the first
4119 * 16 pvs linked to from this page. This count may
4120 * be changed upwards or downwards in the future; it
4121 * is only necessary that true be returned for a small
4122 * subset of pmaps for proper page aging.
4125 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4127 struct md_page *pvh;
4132 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4133 ("pmap_page_exists_quick: page %p is not managed", m));
4135 vm_page_lock_queues();
4136 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4137 if (PV_PMAP(pv) == pmap) {
4145 if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
4146 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4147 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
4148 if (PV_PMAP(pv) == pmap) {
4157 vm_page_unlock_queues();
4162 * pmap_page_wired_mappings:
4164 * Return the number of managed mappings to the given physical page
4168 pmap_page_wired_mappings(vm_page_t m)
4173 if ((m->oflags & VPO_UNMANAGED) != 0)
4175 vm_page_lock_queues();
4176 count = pmap_pvh_wired_mappings(&m->md, count);
4177 if ((m->flags & PG_FICTITIOUS) == 0) {
4178 count = pmap_pvh_wired_mappings(pa_to_pvh(VM_PAGE_TO_PHYS(m)),
4181 vm_page_unlock_queues();
4186 * pmap_pvh_wired_mappings:
4188 * Return the updated number "count" of managed mappings that are wired.
4191 pmap_pvh_wired_mappings(struct md_page *pvh, int count)
4197 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4199 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
4202 pte = pmap_pte_quick(pmap, pv->pv_va);
4203 if ((*pte & PG_W) != 0)
4212 * Returns TRUE if the given page is mapped individually or as part of
4213 * a 4mpage. Otherwise, returns FALSE.
4216 pmap_page_is_mapped(vm_page_t m)
4220 if ((m->oflags & VPO_UNMANAGED) != 0)
4222 vm_page_lock_queues();
4223 rv = !TAILQ_EMPTY(&m->md.pv_list) ||
4224 ((m->flags & PG_FICTITIOUS) == 0 &&
4225 !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
4226 vm_page_unlock_queues();
4231 * Remove all pages from specified address space
4232 * this aids process exit speeds. Also, this code
4233 * is special cased for current process only, but
4234 * can have the more generic (and slightly slower)
4235 * mode enabled. This is much faster than pmap_remove
4236 * in the case of running down an entire address space.
4239 pmap_remove_pages(pmap_t pmap)
4241 pt_entry_t *pte, tpte;
4242 vm_page_t free = NULL;
4243 vm_page_t m, mpte, mt;
4245 struct md_page *pvh;
4246 struct pv_chunk *pc, *npc;
4249 uint32_t inuse, bitmask;
4252 if (pmap != PCPU_GET(curpmap)) {
4253 printf("warning: pmap_remove_pages called with non-current pmap\n");
4256 vm_page_lock_queues();
4259 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4261 for (field = 0; field < _NPCM; field++) {
4262 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
4263 while (inuse != 0) {
4265 bitmask = 1UL << bit;
4266 idx = field * 32 + bit;
4267 pv = &pc->pc_pventry[idx];
4270 pte = pmap_pde(pmap, pv->pv_va);
4272 if ((tpte & PG_PS) == 0) {
4273 pte = vtopte(pv->pv_va);
4274 tpte = *pte & ~PG_PTE_PAT;
4279 "TPTE at %p IS ZERO @ VA %08x\n",
4285 * We cannot remove wired pages from a process' mapping at this time
4292 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
4293 KASSERT(m->phys_addr == (tpte & PG_FRAME),
4294 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
4295 m, (uintmax_t)m->phys_addr,
4298 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4299 m < &vm_page_array[vm_page_array_size],
4300 ("pmap_remove_pages: bad tpte %#jx",
4306 * Update the vm_page_t clean/reference bits.
4308 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4309 if ((tpte & PG_PS) != 0) {
4310 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4317 PV_STAT(pv_entry_frees++);
4318 PV_STAT(pv_entry_spare++);
4320 pc->pc_map[field] |= bitmask;
4321 if ((tpte & PG_PS) != 0) {
4322 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
4323 pvh = pa_to_pvh(tpte & PG_PS_FRAME);
4324 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
4325 if (TAILQ_EMPTY(&pvh->pv_list)) {
4326 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4327 if (TAILQ_EMPTY(&mt->md.pv_list))
4328 vm_page_aflag_clear(mt, PGA_WRITEABLE);
4330 mpte = pmap_lookup_pt_page(pmap, pv->pv_va);
4332 pmap_remove_pt_page(pmap, mpte);
4333 pmap->pm_stats.resident_count--;
4334 KASSERT(mpte->wire_count == NPTEPG,
4335 ("pmap_remove_pages: pte page wire count error"));
4336 mpte->wire_count = 0;
4337 pmap_add_delayed_free_list(mpte, &free, FALSE);
4338 atomic_subtract_int(&cnt.v_wire_count, 1);
4341 pmap->pm_stats.resident_count--;
4342 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
4343 if (TAILQ_EMPTY(&m->md.pv_list) &&
4344 (m->flags & PG_FICTITIOUS) == 0) {
4345 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4346 if (TAILQ_EMPTY(&pvh->pv_list))
4347 vm_page_aflag_clear(m, PGA_WRITEABLE);
4349 pmap_unuse_pt(pmap, pv->pv_va, &free);
4354 PV_STAT(pv_entry_spare -= _NPCPV);
4355 PV_STAT(pc_chunk_count--);
4356 PV_STAT(pc_chunk_frees++);
4357 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4358 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
4359 pmap_qremove((vm_offset_t)pc, 1);
4360 vm_page_unwire(m, 0);
4362 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
4366 pmap_invalidate_all(pmap);
4367 vm_page_unlock_queues();
4369 pmap_free_zero_pages(free);
4375 * Return whether or not the specified physical page was modified
4376 * in any physical maps.
4379 pmap_is_modified(vm_page_t m)
4383 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4384 ("pmap_is_modified: page %p is not managed", m));
4387 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be
4388 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
4389 * is clear, no PTEs can have PG_M set.
4391 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
4392 if ((m->oflags & VPO_BUSY) == 0 &&
4393 (m->aflags & PGA_WRITEABLE) == 0)
4395 vm_page_lock_queues();
4396 rv = pmap_is_modified_pvh(&m->md) ||
4397 ((m->flags & PG_FICTITIOUS) == 0 &&
4398 pmap_is_modified_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
4399 vm_page_unlock_queues();
4404 * Returns TRUE if any of the given mappings were used to modify
4405 * physical memory. Otherwise, returns FALSE. Both page and 2mpage
4406 * mappings are supported.
4409 pmap_is_modified_pvh(struct md_page *pvh)
4416 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4419 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
4422 pte = pmap_pte_quick(pmap, pv->pv_va);
4423 rv = (*pte & (PG_M | PG_RW)) == (PG_M | PG_RW);
4433 * pmap_is_prefaultable:
4435 * Return whether or not the specified virtual address is elgible
4439 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
4447 pde = pmap_pde(pmap, addr);
4448 if (*pde != 0 && (*pde & PG_PS) == 0) {
4457 * pmap_is_referenced:
4459 * Return whether or not the specified physical page was referenced
4460 * in any physical maps.
4463 pmap_is_referenced(vm_page_t m)
4467 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4468 ("pmap_is_referenced: page %p is not managed", m));
4469 vm_page_lock_queues();
4470 rv = pmap_is_referenced_pvh(&m->md) ||
4471 ((m->flags & PG_FICTITIOUS) == 0 &&
4472 pmap_is_referenced_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
4473 vm_page_unlock_queues();
4478 * Returns TRUE if any of the given mappings were referenced and FALSE
4479 * otherwise. Both page and 4mpage mappings are supported.
4482 pmap_is_referenced_pvh(struct md_page *pvh)
4489 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
4492 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
4495 pte = pmap_pte_quick(pmap, pv->pv_va);
4496 rv = (*pte & (PG_A | PG_V)) == (PG_A | PG_V);
4506 * Clear the write and modified bits in each of the given page's mappings.
4509 pmap_remove_write(vm_page_t m)
4511 struct md_page *pvh;
4512 pv_entry_t next_pv, pv;
4515 pt_entry_t oldpte, *pte;
4518 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4519 ("pmap_remove_write: page %p is not managed", m));
4522 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by
4523 * another thread while the object is locked. Thus, if PGA_WRITEABLE
4524 * is clear, no page table entries need updating.
4526 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
4527 if ((m->oflags & VPO_BUSY) == 0 &&
4528 (m->aflags & PGA_WRITEABLE) == 0)
4530 vm_page_lock_queues();
4532 if ((m->flags & PG_FICTITIOUS) != 0)
4533 goto small_mappings;
4534 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4535 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4539 pde = pmap_pde(pmap, va);
4540 if ((*pde & PG_RW) != 0)
4541 (void)pmap_demote_pde(pmap, pde, va);
4545 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4548 pde = pmap_pde(pmap, pv->pv_va);
4549 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_write: found"
4550 " a 4mpage in page %p's pv list", m));
4551 pte = pmap_pte_quick(pmap, pv->pv_va);
4554 if ((oldpte & PG_RW) != 0) {
4556 * Regardless of whether a pte is 32 or 64 bits
4557 * in size, PG_RW and PG_M are among the least
4558 * significant 32 bits.
4560 if (!atomic_cmpset_int((u_int *)pte, oldpte,
4561 oldpte & ~(PG_RW | PG_M)))
4563 if ((oldpte & PG_M) != 0)
4565 pmap_invalidate_page(pmap, pv->pv_va);
4569 vm_page_aflag_clear(m, PGA_WRITEABLE);
4571 vm_page_unlock_queues();
4575 * pmap_ts_referenced:
4577 * Return a count of reference bits for a page, clearing those bits.
4578 * It is not necessary for every reference bit to be cleared, but it
4579 * is necessary that 0 only be returned when there are truly no
4580 * reference bits set.
4582 * XXX: The exact number of bits to check and clear is a matter that
4583 * should be tested and standardized at some point in the future for
4584 * optimal aging of shared pages.
4587 pmap_ts_referenced(vm_page_t m)
4589 struct md_page *pvh;
4590 pv_entry_t pv, pvf, pvn;
4592 pd_entry_t oldpde, *pde;
4597 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4598 ("pmap_ts_referenced: page %p is not managed", m));
4599 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4600 vm_page_lock_queues();
4602 if ((m->flags & PG_FICTITIOUS) != 0)
4603 goto small_mappings;
4604 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, pvn) {
4608 pde = pmap_pde(pmap, va);
4610 if ((oldpde & PG_A) != 0) {
4611 if (pmap_demote_pde(pmap, pde, va)) {
4612 if ((oldpde & PG_W) == 0) {
4614 * Remove the mapping to a single page
4615 * so that a subsequent access may
4616 * repromote. Since the underlying
4617 * page table page is fully populated,
4618 * this removal never frees a page
4621 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4623 pmap_remove_page(pmap, va, NULL);
4635 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
4638 pvn = TAILQ_NEXT(pv, pv_list);
4639 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
4640 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
4643 pde = pmap_pde(pmap, pv->pv_va);
4644 KASSERT((*pde & PG_PS) == 0, ("pmap_ts_referenced:"
4645 " found a 4mpage in page %p's pv list", m));
4646 pte = pmap_pte_quick(pmap, pv->pv_va);
4647 if ((*pte & PG_A) != 0) {
4648 atomic_clear_int((u_int *)pte, PG_A);
4649 pmap_invalidate_page(pmap, pv->pv_va);
4655 } while ((pv = pvn) != NULL && pv != pvf);
4659 vm_page_unlock_queues();
4664 * Clear the modify bits on the specified physical page.
4667 pmap_clear_modify(vm_page_t m)
4669 struct md_page *pvh;
4670 pv_entry_t next_pv, pv;
4672 pd_entry_t oldpde, *pde;
4673 pt_entry_t oldpte, *pte;
4676 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4677 ("pmap_clear_modify: page %p is not managed", m));
4678 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
4679 KASSERT((m->oflags & VPO_BUSY) == 0,
4680 ("pmap_clear_modify: page %p is busy", m));
4683 * If the page is not PGA_WRITEABLE, then no PTEs can have PG_M set.
4684 * If the object containing the page is locked and the page is not
4685 * VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set.
4687 if ((m->aflags & PGA_WRITEABLE) == 0)
4689 vm_page_lock_queues();
4691 if ((m->flags & PG_FICTITIOUS) != 0)
4692 goto small_mappings;
4693 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4694 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4698 pde = pmap_pde(pmap, va);
4700 if ((oldpde & PG_RW) != 0) {
4701 if (pmap_demote_pde(pmap, pde, va)) {
4702 if ((oldpde & PG_W) == 0) {
4704 * Write protect the mapping to a
4705 * single page so that a subsequent
4706 * write access may repromote.
4708 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4710 pte = pmap_pte_quick(pmap, va);
4712 if ((oldpte & PG_V) != 0) {
4714 * Regardless of whether a pte is 32 or 64 bits
4715 * in size, PG_RW and PG_M are among the least
4716 * significant 32 bits.
4718 while (!atomic_cmpset_int((u_int *)pte,
4720 oldpte & ~(PG_M | PG_RW)))
4723 pmap_invalidate_page(pmap, va);
4731 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4734 pde = pmap_pde(pmap, pv->pv_va);
4735 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_modify: found"
4736 " a 4mpage in page %p's pv list", m));
4737 pte = pmap_pte_quick(pmap, pv->pv_va);
4738 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4740 * Regardless of whether a pte is 32 or 64 bits
4741 * in size, PG_M is among the least significant
4744 atomic_clear_int((u_int *)pte, PG_M);
4745 pmap_invalidate_page(pmap, pv->pv_va);
4750 vm_page_unlock_queues();
4754 * pmap_clear_reference:
4756 * Clear the reference bit on the specified physical page.
4759 pmap_clear_reference(vm_page_t m)
4761 struct md_page *pvh;
4762 pv_entry_t next_pv, pv;
4764 pd_entry_t oldpde, *pde;
4768 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4769 ("pmap_clear_reference: page %p is not managed", m));
4770 vm_page_lock_queues();
4772 if ((m->flags & PG_FICTITIOUS) != 0)
4773 goto small_mappings;
4774 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4775 TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_list, next_pv) {
4779 pde = pmap_pde(pmap, va);
4781 if ((oldpde & PG_A) != 0) {
4782 if (pmap_demote_pde(pmap, pde, va)) {
4784 * Remove the mapping to a single page so
4785 * that a subsequent access may repromote.
4786 * Since the underlying page table page is
4787 * fully populated, this removal never frees
4788 * a page table page.
4790 va += VM_PAGE_TO_PHYS(m) - (oldpde &
4792 pmap_remove_page(pmap, va, NULL);
4798 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4801 pde = pmap_pde(pmap, pv->pv_va);
4802 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_reference: found"
4803 " a 4mpage in page %p's pv list", m));
4804 pte = pmap_pte_quick(pmap, pv->pv_va);
4805 if ((*pte & PG_A) != 0) {
4807 * Regardless of whether a pte is 32 or 64 bits
4808 * in size, PG_A is among the least significant
4811 atomic_clear_int((u_int *)pte, PG_A);
4812 pmap_invalidate_page(pmap, pv->pv_va);
4817 vm_page_unlock_queues();
4821 * Miscellaneous support routines follow
4824 /* Adjust the cache mode for a 4KB page mapped via a PTE. */
4825 static __inline void
4826 pmap_pte_attr(pt_entry_t *pte, int cache_bits)
4831 * The cache mode bits are all in the low 32-bits of the
4832 * PTE, so we can just spin on updating the low 32-bits.
4835 opte = *(u_int *)pte;
4836 npte = opte & ~PG_PTE_CACHE;
4838 } while (npte != opte && !atomic_cmpset_int((u_int *)pte, opte, npte));
4841 /* Adjust the cache mode for a 2/4MB page mapped via a PDE. */
4842 static __inline void
4843 pmap_pde_attr(pd_entry_t *pde, int cache_bits)
4848 * The cache mode bits are all in the low 32-bits of the
4849 * PDE, so we can just spin on updating the low 32-bits.
4852 opde = *(u_int *)pde;
4853 npde = opde & ~PG_PDE_CACHE;
4855 } while (npde != opde && !atomic_cmpset_int((u_int *)pde, opde, npde));
4859 * Map a set of physical memory pages into the kernel virtual
4860 * address space. Return a pointer to where it is mapped. This
4861 * routine is intended to be used for mapping device memory,
4865 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
4867 vm_offset_t va, offset;
4870 offset = pa & PAGE_MASK;
4871 size = roundup(offset + size, PAGE_SIZE);
4874 if (pa < KERNLOAD && pa + size <= KERNLOAD)
4877 va = kmem_alloc_nofault(kernel_map, size);
4879 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4881 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
4882 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
4883 pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
4884 pmap_invalidate_cache_range(va, va + size);
4885 return ((void *)(va + offset));
4889 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
4892 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
4896 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
4899 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
4903 pmap_unmapdev(vm_offset_t va, vm_size_t size)
4905 vm_offset_t base, offset, tmpva;
4907 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
4909 base = trunc_page(va);
4910 offset = va & PAGE_MASK;
4911 size = roundup(offset + size, PAGE_SIZE);
4912 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
4913 pmap_kremove(tmpva);
4914 pmap_invalidate_range(kernel_pmap, va, tmpva);
4915 kmem_free(kernel_map, base, size);
4919 * Sets the memory attribute for the specified page.
4922 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
4925 m->md.pat_mode = ma;
4926 if ((m->flags & PG_FICTITIOUS) != 0)
4930 * If "m" is a normal page, flush it from the cache.
4931 * See pmap_invalidate_cache_range().
4933 * First, try to find an existing mapping of the page by sf
4934 * buffer. sf_buf_invalidate_cache() modifies mapping and
4935 * flushes the cache.
4937 if (sf_buf_invalidate_cache(m))
4941 * If page is not mapped by sf buffer, but CPU does not
4942 * support self snoop, map the page transient and do
4943 * invalidation. In the worst case, whole cache is flushed by
4944 * pmap_invalidate_cache_range().
4946 if ((cpu_feature & CPUID_SS) == 0)
4951 pmap_flush_page(vm_page_t m)
4953 struct sysmaps *sysmaps;
4954 vm_offset_t sva, eva;
4956 if ((cpu_feature & CPUID_CLFSH) != 0) {
4957 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4958 mtx_lock(&sysmaps->lock);
4959 if (*sysmaps->CMAP2)
4960 panic("pmap_flush_page: CMAP2 busy");
4962 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) |
4963 PG_A | PG_M | pmap_cache_bits(m->md.pat_mode, 0);
4964 invlcaddr(sysmaps->CADDR2);
4965 sva = (vm_offset_t)sysmaps->CADDR2;
4966 eva = sva + PAGE_SIZE;
4969 * Use mfence despite the ordering implied by
4970 * mtx_{un,}lock() because clflush is not guaranteed
4971 * to be ordered by any other instruction.
4974 for (; sva < eva; sva += cpu_clflush_line_size)
4977 *sysmaps->CMAP2 = 0;
4979 mtx_unlock(&sysmaps->lock);
4981 pmap_invalidate_cache();
4985 * Changes the specified virtual address range's memory type to that given by
4986 * the parameter "mode". The specified virtual address range must be
4987 * completely contained within either the kernel map.
4989 * Returns zero if the change completed successfully, and either EINVAL or
4990 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
4991 * of the virtual address range was not mapped, and ENOMEM is returned if
4992 * there was insufficient memory available to complete the change.
4995 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
4997 vm_offset_t base, offset, tmpva;
5000 int cache_bits_pte, cache_bits_pde;
5003 base = trunc_page(va);
5004 offset = va & PAGE_MASK;
5005 size = roundup(offset + size, PAGE_SIZE);
5008 * Only supported on kernel virtual addresses above the recursive map.
5010 if (base < VM_MIN_KERNEL_ADDRESS)
5013 cache_bits_pde = pmap_cache_bits(mode, 1);
5014 cache_bits_pte = pmap_cache_bits(mode, 0);
5018 * Pages that aren't mapped aren't supported. Also break down
5019 * 2/4MB pages into 4KB pages if required.
5021 PMAP_LOCK(kernel_pmap);
5022 for (tmpva = base; tmpva < base + size; ) {
5023 pde = pmap_pde(kernel_pmap, tmpva);
5025 PMAP_UNLOCK(kernel_pmap);
5030 * If the current 2/4MB page already has
5031 * the required memory type, then we need not
5032 * demote this page. Just increment tmpva to
5033 * the next 2/4MB page frame.
5035 if ((*pde & PG_PDE_CACHE) == cache_bits_pde) {
5036 tmpva = trunc_4mpage(tmpva) + NBPDR;
5041 * If the current offset aligns with a 2/4MB
5042 * page frame and there is at least 2/4MB left
5043 * within the range, then we need not break
5044 * down this page into 4KB pages.
5046 if ((tmpva & PDRMASK) == 0 &&
5047 tmpva + PDRMASK < base + size) {
5051 if (!pmap_demote_pde(kernel_pmap, pde, tmpva)) {
5052 PMAP_UNLOCK(kernel_pmap);
5056 pte = vtopte(tmpva);
5058 PMAP_UNLOCK(kernel_pmap);
5063 PMAP_UNLOCK(kernel_pmap);
5066 * Ok, all the pages exist, so run through them updating their
5067 * cache mode if required.
5069 for (tmpva = base; tmpva < base + size; ) {
5070 pde = pmap_pde(kernel_pmap, tmpva);
5072 if ((*pde & PG_PDE_CACHE) != cache_bits_pde) {
5073 pmap_pde_attr(pde, cache_bits_pde);
5076 tmpva = trunc_4mpage(tmpva) + NBPDR;
5078 pte = vtopte(tmpva);
5079 if ((*pte & PG_PTE_CACHE) != cache_bits_pte) {
5080 pmap_pte_attr(pte, cache_bits_pte);
5088 * Flush CPU caches to make sure any data isn't cached that
5089 * shouldn't be, etc.
5092 pmap_invalidate_range(kernel_pmap, base, tmpva);
5093 pmap_invalidate_cache_range(base, tmpva);
5099 * perform the pmap work for mincore
5102 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
5105 pt_entry_t *ptep, pte;
5111 pdep = pmap_pde(pmap, addr);
5113 if (*pdep & PG_PS) {
5115 /* Compute the physical address of the 4KB page. */
5116 pa = ((*pdep & PG_PS_FRAME) | (addr & PDRMASK)) &
5118 val = MINCORE_SUPER;
5120 ptep = pmap_pte(pmap, addr);
5122 pmap_pte_release(ptep);
5123 pa = pte & PG_FRAME;
5131 if ((pte & PG_V) != 0) {
5132 val |= MINCORE_INCORE;
5133 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
5134 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
5135 if ((pte & PG_A) != 0)
5136 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
5138 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
5139 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
5140 (pte & (PG_MANAGED | PG_V)) == (PG_MANAGED | PG_V)) {
5141 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
5142 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
5145 PA_UNLOCK_COND(*locked_pa);
5151 pmap_activate(struct thread *td)
5153 pmap_t pmap, oldpmap;
5158 pmap = vmspace_pmap(td->td_proc->p_vmspace);
5159 oldpmap = PCPU_GET(curpmap);
5160 cpuid = PCPU_GET(cpuid);
5162 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
5163 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
5165 CPU_CLR(cpuid, &oldpmap->pm_active);
5166 CPU_SET(cpuid, &pmap->pm_active);
5169 cr3 = vtophys(pmap->pm_pdpt);
5171 cr3 = vtophys(pmap->pm_pdir);
5174 * pmap_activate is for the current thread on the current cpu
5176 td->td_pcb->pcb_cr3 = cr3;
5178 PCPU_SET(curpmap, pmap);
5183 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
5188 * Increase the starting virtual address of the given mapping if a
5189 * different alignment might result in more superpage mappings.
5192 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
5193 vm_offset_t *addr, vm_size_t size)
5195 vm_offset_t superpage_offset;
5199 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
5200 offset += ptoa(object->pg_color);
5201 superpage_offset = offset & PDRMASK;
5202 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
5203 (*addr & PDRMASK) == superpage_offset)
5205 if ((*addr & PDRMASK) < superpage_offset)
5206 *addr = (*addr & ~PDRMASK) + superpage_offset;
5208 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
5212 #if defined(PMAP_DEBUG)
5213 pmap_pid_dump(int pid)
5220 sx_slock(&allproc_lock);
5221 FOREACH_PROC_IN_SYSTEM(p) {
5222 if (p->p_pid != pid)
5228 pmap = vmspace_pmap(p->p_vmspace);
5229 for (i = 0; i < NPDEPTD; i++) {
5232 vm_offset_t base = i << PDRSHIFT;
5234 pde = &pmap->pm_pdir[i];
5235 if (pde && pmap_pde_v(pde)) {
5236 for (j = 0; j < NPTEPG; j++) {
5237 vm_offset_t va = base + (j << PAGE_SHIFT);
5238 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
5243 sx_sunlock(&allproc_lock);
5246 pte = pmap_pte(pmap, va);
5247 if (pte && pmap_pte_v(pte)) {
5251 m = PHYS_TO_VM_PAGE(pa & PG_FRAME);
5252 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
5253 va, pa, m->hold_count, m->wire_count, m->flags);
5268 sx_sunlock(&allproc_lock);
5275 static void pads(pmap_t pm);
5276 void pmap_pvdump(vm_paddr_t pa);
5278 /* print address space of pmap*/
5286 if (pm == kernel_pmap)
5288 for (i = 0; i < NPDEPTD; i++)
5290 for (j = 0; j < NPTEPG; j++) {
5291 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
5292 if (pm == kernel_pmap && va < KERNBASE)
5294 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
5296 ptep = pmap_pte(pm, va);
5297 if (pmap_pte_v(ptep))
5298 printf("%x:%x ", va, *ptep);
5304 pmap_pvdump(vm_paddr_t pa)
5310 printf("pa %x", pa);
5311 m = PHYS_TO_VM_PAGE(pa);
5312 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
5314 printf(" -> pmap %p, va %x", (void *)pmap, pv->pv_va);