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 Alan L. Cox <alc@cs.rice.edu>
11 * This code is derived from software contributed to Berkeley by
12 * the Systems Programming Group of the University of Utah Computer
13 * Science Department and William Jolitz of UUNET Technologies Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
46 * Copyright (c) 2003 Networks Associates Technology, Inc.
47 * All rights reserved.
49 * This software was developed for the FreeBSD Project by Jake Burkholder,
50 * Safeport Network Services, and Network Associates Laboratories, the
51 * Security Research Division of Network Associates, Inc. under
52 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
53 * CHATS research program.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
64 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 #include <sys/cdefs.h>
78 __FBSDID("$FreeBSD$");
81 * Manages physical address maps.
83 * In addition to hardware address maps, this
84 * module is called upon to provide software-use-only
85 * maps which may or may not be stored in the same
86 * form as hardware maps. These pseudo-maps are
87 * used to store intermediate results from copy
88 * operations to and from address spaces.
90 * Since the information managed by this module is
91 * also stored by the logical address mapping module,
92 * this module may throw away valid virtual-to-physical
93 * mappings at almost any time. However, invalidations
94 * of virtual-to-physical mappings must be done as
97 * In order to cope with hardware architectures which
98 * make virtual-to-physical map invalidates expensive,
99 * this module may delay invalidate or reduced protection
100 * operations until such time as they are actually
101 * necessary. This module is given full information as
102 * to which processors are currently using which maps,
103 * and to when physical maps must be made correct.
107 #include "opt_pmap.h"
108 #include "opt_msgbuf.h"
110 #include "opt_xbox.h"
112 #include <sys/param.h>
113 #include <sys/systm.h>
114 #include <sys/kernel.h>
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>
122 #include <sys/vmmeter.h>
123 #include <sys/sched.h>
124 #include <sys/sysctl.h>
130 #include <vm/vm_param.h>
131 #include <vm/vm_kern.h>
132 #include <vm/vm_page.h>
133 #include <vm/vm_map.h>
134 #include <vm/vm_object.h>
135 #include <vm/vm_extern.h>
136 #include <vm/vm_pageout.h>
137 #include <vm/vm_pager.h>
140 #include <machine/cpu.h>
141 #include <machine/cputypes.h>
142 #include <machine/md_var.h>
143 #include <machine/pcb.h>
144 #include <machine/specialreg.h>
146 #include <machine/smp.h>
150 #include <machine/xbox.h>
153 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
154 #define CPU_ENABLE_SSE
157 #ifndef PMAP_SHPGPERPROC
158 #define PMAP_SHPGPERPROC 200
161 #if defined(DIAGNOSTIC)
162 #define PMAP_DIAGNOSTIC
165 #if !defined(PMAP_DIAGNOSTIC)
166 #define PMAP_INLINE __inline
172 * Get PDEs and PTEs for user/kernel address space
174 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
175 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
177 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
178 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
179 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
180 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
181 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
183 #define pmap_pte_set_w(pte, v) ((v) ? atomic_set_int((u_int *)(pte), PG_W) : \
184 atomic_clear_int((u_int *)(pte), PG_W))
185 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
187 struct pmap kernel_pmap_store;
188 LIST_HEAD(pmaplist, pmap);
189 static struct pmaplist allpmaps;
190 static struct mtx allpmaps_lock;
192 vm_paddr_t avail_end; /* PA of last available physical page */
193 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
194 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
195 int pgeflag = 0; /* PG_G or-in */
196 int pseflag = 0; /* PG_PS or-in */
199 vm_offset_t kernel_vm_end;
200 extern u_int32_t KERNend;
203 static uma_zone_t pdptzone;
207 * Data for the pv entry allocation mechanism
209 static uma_zone_t pvzone;
210 static struct vm_object pvzone_obj;
211 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
214 * All those kernel PT submaps that BSD is so fond of
223 static struct sysmaps sysmaps_pcpu[MAXCPU];
224 pt_entry_t *CMAP1 = 0;
225 static pt_entry_t *CMAP3;
226 caddr_t CADDR1 = 0, ptvmmap = 0;
227 static caddr_t CADDR3;
228 struct msgbuf *msgbufp = 0;
233 static caddr_t crashdumpmap;
236 extern pt_entry_t *SMPpt;
238 static pt_entry_t *PMAP1 = 0, *PMAP2;
239 static pt_entry_t *PADDR1 = 0, *PADDR2;
242 static int PMAP1changedcpu;
243 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
245 "Number of times pmap_pte_quick changed CPU with same PMAP1");
247 static int PMAP1changed;
248 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
250 "Number of times pmap_pte_quick changed PMAP1");
251 static int PMAP1unchanged;
252 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
254 "Number of times pmap_pte_quick didn't change PMAP1");
255 static struct mtx PMAP2mutex;
257 static PMAP_INLINE void free_pv_entry(pv_entry_t pv);
258 static pv_entry_t get_pv_entry(pmap_t locked_pmap);
259 static void pmap_clear_ptes(vm_page_t m, int bit);
261 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva);
262 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
263 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
265 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
267 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
269 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags);
270 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m);
271 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
272 static void pmap_pte_release(pt_entry_t *pte);
273 static int pmap_unuse_pt(pmap_t, vm_offset_t);
274 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
276 static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
279 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
280 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
283 * Move the kernel virtual free pointer to the next
284 * 4MB. This is used to help improve performance
285 * by using a large (4MB) page for much of the kernel
286 * (.text, .data, .bss)
289 pmap_kmem_choose(vm_offset_t addr)
291 vm_offset_t newaddr = addr;
294 if (cpu_feature & CPUID_PSE)
295 newaddr = (addr + PDRMASK) & ~PDRMASK;
301 * Bootstrap the system enough to run with virtual memory.
303 * On the i386 this is called after mapping has already been enabled
304 * and just syncs the pmap module with what has already been done.
305 * [We can't call it easily with mapping off since the kernel is not
306 * mapped with PA == VA, hence we would have to relocate every address
307 * from the linked base (virtual) address "KERNBASE" to the actual
308 * (physical) address starting relative to 0]
311 pmap_bootstrap(firstaddr, loadaddr)
312 vm_paddr_t firstaddr;
316 pt_entry_t *pte, *unused;
317 struct sysmaps *sysmaps;
321 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
322 * large. It should instead be correctly calculated in locore.s and
323 * not based on 'first' (which is a physical address, not a virtual
324 * address, for the start of unused physical memory). The kernel
325 * page tables are NOT double mapped and thus should not be included
326 * in this calculation.
328 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
329 virtual_avail = pmap_kmem_choose(virtual_avail);
331 virtual_end = VM_MAX_KERNEL_ADDRESS;
334 * Initialize the kernel pmap (which is statically allocated).
336 PMAP_LOCK_INIT(kernel_pmap);
337 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
339 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
341 kernel_pmap->pm_active = -1; /* don't allow deactivation */
342 TAILQ_INIT(&kernel_pmap->pm_pvlist);
343 LIST_INIT(&allpmaps);
344 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
345 mtx_lock_spin(&allpmaps_lock);
346 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
347 mtx_unlock_spin(&allpmaps_lock);
351 * Reserve some special page table entries/VA space for temporary
354 #define SYSMAP(c, p, v, n) \
355 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
361 * CMAP1/CMAP2 are used for zeroing and copying pages.
362 * CMAP3 is used for the idle process page zeroing.
364 for (i = 0; i < MAXCPU; i++) {
365 sysmaps = &sysmaps_pcpu[i];
366 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
367 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
368 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
370 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
371 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
377 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
380 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
382 SYSMAP(caddr_t, unused, ptvmmap, 1)
385 * msgbufp is used to map the system message buffer.
387 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(MSGBUF_SIZE)))
390 * ptemap is used for pmap_pte_quick
392 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1);
393 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1);
395 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
402 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
403 * an early stadium, we cannot yet neatly map video memory ... :-(
404 * Better fixes are very welcome! */
405 if (!arch_i386_is_xbox)
407 for (i = 0; i < NKPT; i++)
410 /* Turn on PG_G on kernel page(s) */
415 * Set PG_G on kernel pages. Only the BSP calls this when SMP is turned on.
422 vm_offset_t va, endva;
429 endva = KERNBASE + KERNend;
432 va = KERNBASE + KERNLOAD;
434 pdir = kernel_pmap->pm_pdir[KPTDI+i];
436 kernel_pmap->pm_pdir[KPTDI+i] = PTD[KPTDI+i] = pdir;
437 invltlb(); /* Play it safe, invltlb() every time */
442 va = (vm_offset_t)btext;
447 invltlb(); /* Play it safe, invltlb() every time */
454 * Initialize a vm_page's machine-dependent fields.
457 pmap_page_init(vm_page_t m)
460 TAILQ_INIT(&m->md.pv_list);
461 m->md.pv_list_count = 0;
466 static MALLOC_DEFINE(M_PMAPPDPT, "pmap", "pmap pdpt");
469 pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
471 *flags = UMA_SLAB_PRIV;
472 return (contigmalloc(PAGE_SIZE, M_PMAPPDPT, 0, 0x0ULL, 0xffffffffULL,
478 * Initialize the pmap module.
479 * Called by vm_init, to initialize any structures that the pmap
480 * system needs to map virtual memory.
485 int shpgperproc = PMAP_SHPGPERPROC;
488 * Initialize the address space (zone) for the pv entries. Set a
489 * high water mark so that the system can recover from excessive
490 * numbers of pv entries.
492 pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL,
493 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
494 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
495 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
496 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
497 pv_entry_high_water = 9 * (pv_entry_max / 10);
498 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
501 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
502 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
503 UMA_ZONE_VM | UMA_ZONE_NOFREE);
504 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
509 /***************************************************
510 * Low level helper routines.....
511 ***************************************************/
515 * this routine defines the region(s) of memory that should
516 * not be tested for the modified bit.
518 static PMAP_INLINE int
519 pmap_track_modified(vm_offset_t va)
521 if ((va < kmi.clean_sva) || (va >= kmi.clean_eva))
529 * For SMP, these functions have to use the IPI mechanism for coherence.
532 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
538 if (!(read_eflags() & PSL_I))
539 panic("%s: interrupts disabled", __func__);
540 mtx_lock_spin(&smp_ipi_mtx);
544 * We need to disable interrupt preemption but MUST NOT have
545 * interrupts disabled here.
546 * XXX we may need to hold schedlock to get a coherent pm_active
547 * XXX critical sections disable interrupts again
549 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
553 cpumask = PCPU_GET(cpumask);
554 other_cpus = PCPU_GET(other_cpus);
555 if (pmap->pm_active & cpumask)
557 if (pmap->pm_active & other_cpus)
558 smp_masked_invlpg(pmap->pm_active & other_cpus, va);
561 mtx_unlock_spin(&smp_ipi_mtx);
567 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
574 if (!(read_eflags() & PSL_I))
575 panic("%s: interrupts disabled", __func__);
576 mtx_lock_spin(&smp_ipi_mtx);
580 * We need to disable interrupt preemption but MUST NOT have
581 * interrupts disabled here.
582 * XXX we may need to hold schedlock to get a coherent pm_active
583 * XXX critical sections disable interrupts again
585 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
586 for (addr = sva; addr < eva; addr += PAGE_SIZE)
588 smp_invlpg_range(sva, eva);
590 cpumask = PCPU_GET(cpumask);
591 other_cpus = PCPU_GET(other_cpus);
592 if (pmap->pm_active & cpumask)
593 for (addr = sva; addr < eva; addr += PAGE_SIZE)
595 if (pmap->pm_active & other_cpus)
596 smp_masked_invlpg_range(pmap->pm_active & other_cpus,
600 mtx_unlock_spin(&smp_ipi_mtx);
606 pmap_invalidate_all(pmap_t pmap)
612 if (!(read_eflags() & PSL_I))
613 panic("%s: interrupts disabled", __func__);
614 mtx_lock_spin(&smp_ipi_mtx);
618 * We need to disable interrupt preemption but MUST NOT have
619 * interrupts disabled here.
620 * XXX we may need to hold schedlock to get a coherent pm_active
621 * XXX critical sections disable interrupts again
623 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
627 cpumask = PCPU_GET(cpumask);
628 other_cpus = PCPU_GET(other_cpus);
629 if (pmap->pm_active & cpumask)
631 if (pmap->pm_active & other_cpus)
632 smp_masked_invltlb(pmap->pm_active & other_cpus);
635 mtx_unlock_spin(&smp_ipi_mtx);
641 * Normal, non-SMP, 486+ invalidation functions.
642 * We inline these within pmap.c for speed.
645 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
648 if (pmap == kernel_pmap || pmap->pm_active)
653 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
657 if (pmap == kernel_pmap || pmap->pm_active)
658 for (addr = sva; addr < eva; addr += PAGE_SIZE)
663 pmap_invalidate_all(pmap_t pmap)
666 if (pmap == kernel_pmap || pmap->pm_active)
672 * Are we current address space or kernel? N.B. We return FALSE when
673 * a pmap's page table is in use because a kernel thread is borrowing
674 * it. The borrowed page table can change spontaneously, making any
675 * dependence on its continued use subject to a race condition.
678 pmap_is_current(pmap_t pmap)
681 return (pmap == kernel_pmap ||
682 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
683 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
687 * If the given pmap is not the current or kernel pmap, the returned pte must
688 * be released by passing it to pmap_pte_release().
691 pmap_pte(pmap_t pmap, vm_offset_t va)
696 pde = pmap_pde(pmap, va);
700 /* are we current address space or kernel? */
701 if (pmap_is_current(pmap))
703 mtx_lock(&PMAP2mutex);
704 newpf = *pde & PG_FRAME;
705 if ((*PMAP2 & PG_FRAME) != newpf) {
706 *PMAP2 = newpf | PG_RW | PG_V | PG_A | PG_M;
707 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR2);
709 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
715 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
719 pmap_pte_release(pt_entry_t *pte)
722 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2)
723 mtx_unlock(&PMAP2mutex);
727 invlcaddr(void *caddr)
730 invlpg((u_int)caddr);
734 * Super fast pmap_pte routine best used when scanning
735 * the pv lists. This eliminates many coarse-grained
736 * invltlb calls. Note that many of the pv list
737 * scans are across different pmaps. It is very wasteful
738 * to do an entire invltlb for checking a single mapping.
740 * If the given pmap is not the current pmap, vm_page_queue_mtx
741 * must be held and curthread pinned to a CPU.
744 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
749 pde = pmap_pde(pmap, va);
753 /* are we current address space or kernel? */
754 if (pmap_is_current(pmap))
756 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
757 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
758 newpf = *pde & PG_FRAME;
759 if ((*PMAP1 & PG_FRAME) != newpf) {
760 *PMAP1 = newpf | PG_RW | PG_V | PG_A | PG_M;
762 PMAP1cpu = PCPU_GET(cpuid);
768 if (PMAP1cpu != PCPU_GET(cpuid)) {
769 PMAP1cpu = PCPU_GET(cpuid);
775 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
781 * Routine: pmap_extract
783 * Extract the physical page address associated
784 * with the given map/virtual_address pair.
787 pmap_extract(pmap_t pmap, vm_offset_t va)
795 pde = pmap->pm_pdir[va >> PDRSHIFT];
797 if ((pde & PG_PS) != 0) {
798 rtval = (pde & ~PDRMASK) | (va & PDRMASK);
802 pte = pmap_pte(pmap, va);
803 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
804 pmap_pte_release(pte);
811 * Routine: pmap_extract_and_hold
813 * Atomically extract and hold the physical page
814 * with the given pmap and virtual address pair
815 * if that mapping permits the given protection.
818 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
825 vm_page_lock_queues();
827 pde = *pmap_pde(pmap, va);
830 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
831 m = PHYS_TO_VM_PAGE((pde & ~PDRMASK) |
837 pte = *pmap_pte_quick(pmap, va);
839 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
840 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
846 vm_page_unlock_queues();
851 /***************************************************
852 * Low level mapping routines.....
853 ***************************************************/
856 * Add a wired page to the kva.
857 * Note: not SMP coherent.
860 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
865 pte_store(pte, pa | PG_RW | PG_V | pgeflag);
869 * Remove a page from the kernel pagetables.
870 * Note: not SMP coherent.
873 pmap_kremove(vm_offset_t va)
882 * Used to map a range of physical addresses into kernel
883 * virtual address space.
885 * The value passed in '*virt' is a suggested virtual address for
886 * the mapping. Architectures which can support a direct-mapped
887 * physical to virtual region can return the appropriate address
888 * within that region, leaving '*virt' unchanged. Other
889 * architectures should map the pages starting at '*virt' and
890 * update '*virt' with the first usable address after the mapped
894 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
899 while (start < end) {
900 pmap_kenter(va, start);
904 pmap_invalidate_range(kernel_pmap, sva, va);
911 * Add a list of wired pages to the kva
912 * this routine is only used for temporary
913 * kernel mappings that do not need to have
914 * page modification or references recorded.
915 * Note that old mappings are simply written
916 * over. The page *must* be wired.
917 * Note: SMP coherent. Uses a ranged shootdown IPI.
920 pmap_qenter(vm_offset_t sva, vm_page_t *m, int count)
925 while (count-- > 0) {
926 pmap_kenter(va, VM_PAGE_TO_PHYS(*m));
930 pmap_invalidate_range(kernel_pmap, sva, va);
934 * This routine tears out page mappings from the
935 * kernel -- it is meant only for temporary mappings.
936 * Note: SMP coherent. Uses a ranged shootdown IPI.
939 pmap_qremove(vm_offset_t sva, int count)
944 while (count-- > 0) {
948 pmap_invalidate_range(kernel_pmap, sva, va);
951 /***************************************************
952 * Page table page management routines.....
953 ***************************************************/
956 * This routine unholds page table pages, and if the hold count
957 * drops to zero, then it decrements the wire count.
959 static PMAP_INLINE int
960 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
964 if (m->wire_count == 0)
965 return _pmap_unwire_pte_hold(pmap, m);
971 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
976 * unmap the page table page
978 pmap->pm_pdir[m->pindex] = 0;
979 --pmap->pm_stats.resident_count;
982 * Do an invltlb to make the invalidated mapping
983 * take effect immediately.
985 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
986 pmap_invalidate_page(pmap, pteva);
988 vm_page_free_zero(m);
989 atomic_subtract_int(&cnt.v_wire_count, 1);
994 * After removing a page table entry, this routine is used to
995 * conditionally free the page, and manage the hold/wire counts.
998 pmap_unuse_pt(pmap_t pmap, vm_offset_t va)
1003 if (va >= VM_MAXUSER_ADDRESS)
1005 ptepde = *pmap_pde(pmap, va);
1006 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1007 return pmap_unwire_pte_hold(pmap, mpte);
1015 PMAP_LOCK_INIT(pmap);
1016 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1018 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1020 pmap->pm_active = 0;
1021 PCPU_SET(curpmap, pmap);
1022 TAILQ_INIT(&pmap->pm_pvlist);
1023 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1024 mtx_lock_spin(&allpmaps_lock);
1025 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1026 mtx_unlock_spin(&allpmaps_lock);
1030 * Initialize a preallocated and zeroed pmap structure,
1031 * such as one in a vmspace structure.
1035 register struct pmap *pmap;
1037 vm_page_t m, ptdpg[NPGPTD];
1042 PMAP_LOCK_INIT(pmap);
1045 * No need to allocate page table space yet but we do need a valid
1046 * page directory table.
1048 if (pmap->pm_pdir == NULL) {
1049 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map,
1052 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
1053 KASSERT(((vm_offset_t)pmap->pm_pdpt &
1054 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
1055 ("pmap_pinit: pdpt misaligned"));
1056 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
1057 ("pmap_pinit: pdpt above 4g"));
1062 * allocate the page directory page(s)
1064 for (i = 0; i < NPGPTD;) {
1065 m = vm_page_alloc(NULL, color++,
1066 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1075 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1077 for (i = 0; i < NPGPTD; i++) {
1078 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1079 bzero(pmap->pm_pdir + (i * NPDEPG), PAGE_SIZE);
1082 mtx_lock_spin(&allpmaps_lock);
1083 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1084 mtx_unlock_spin(&allpmaps_lock);
1085 /* Wire in kernel global address entries. */
1086 /* XXX copies current process, does not fill in MPPTDI */
1087 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1089 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1092 /* install self-referential address mapping entry(s) */
1093 for (i = 0; i < NPGPTD; i++) {
1094 pa = VM_PAGE_TO_PHYS(ptdpg[i]);
1095 pmap->pm_pdir[PTDPTDI + i] = pa | PG_V | PG_RW | PG_A | PG_M;
1097 pmap->pm_pdpt[i] = pa | PG_V;
1101 pmap->pm_active = 0;
1102 TAILQ_INIT(&pmap->pm_pvlist);
1103 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1107 * this routine is called if the page table page is not
1111 _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags)
1116 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1117 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1118 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1121 * Allocate a page table page.
1123 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1124 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1125 if (flags & M_WAITOK) {
1127 vm_page_unlock_queues();
1129 vm_page_lock_queues();
1134 * Indicate the need to retry. While waiting, the page table
1135 * page may have been allocated.
1139 if ((m->flags & PG_ZERO) == 0)
1143 * Map the pagetable page into the process address space, if
1144 * it isn't already there.
1147 pmap->pm_stats.resident_count++;
1149 ptepa = VM_PAGE_TO_PHYS(m);
1150 pmap->pm_pdir[ptepindex] =
1151 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1157 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1163 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1164 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1165 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1168 * Calculate pagetable page index
1170 ptepindex = va >> PDRSHIFT;
1173 * Get the page directory entry
1175 ptepa = pmap->pm_pdir[ptepindex];
1178 * This supports switching from a 4MB page to a
1181 if (ptepa & PG_PS) {
1182 pmap->pm_pdir[ptepindex] = 0;
1184 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1185 pmap_invalidate_all(kernel_pmap);
1189 * If the page table page is mapped, we just increment the
1190 * hold count, and activate it.
1193 m = PHYS_TO_VM_PAGE(ptepa);
1197 * Here if the pte page isn't mapped, or if it has
1200 m = _pmap_allocpte(pmap, ptepindex, flags);
1201 if (m == NULL && (flags & M_WAITOK))
1208 /***************************************************
1209 * Pmap allocation/deallocation routines.
1210 ***************************************************/
1214 * Deal with a SMP shootdown of other users of the pmap that we are
1215 * trying to dispose of. This can be a bit hairy.
1217 static u_int *lazymask;
1218 static u_int lazyptd;
1219 static volatile u_int lazywait;
1221 void pmap_lazyfix_action(void);
1224 pmap_lazyfix_action(void)
1226 u_int mymask = PCPU_GET(cpumask);
1229 *ipi_lazypmap_counts[PCPU_GET(cpuid)]++;
1231 if (rcr3() == lazyptd)
1232 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1233 atomic_clear_int(lazymask, mymask);
1234 atomic_store_rel_int(&lazywait, 1);
1238 pmap_lazyfix_self(u_int mymask)
1241 if (rcr3() == lazyptd)
1242 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1243 atomic_clear_int(lazymask, mymask);
1248 pmap_lazyfix(pmap_t pmap)
1252 register u_int spins;
1254 while ((mask = pmap->pm_active) != 0) {
1256 mask = mask & -mask; /* Find least significant set bit */
1257 mtx_lock_spin(&smp_ipi_mtx);
1259 lazyptd = vtophys(pmap->pm_pdpt);
1261 lazyptd = vtophys(pmap->pm_pdir);
1263 mymask = PCPU_GET(cpumask);
1264 if (mask == mymask) {
1265 lazymask = &pmap->pm_active;
1266 pmap_lazyfix_self(mymask);
1268 atomic_store_rel_int((u_int *)&lazymask,
1269 (u_int)&pmap->pm_active);
1270 atomic_store_rel_int(&lazywait, 0);
1271 ipi_selected(mask, IPI_LAZYPMAP);
1272 while (lazywait == 0) {
1278 mtx_unlock_spin(&smp_ipi_mtx);
1280 printf("pmap_lazyfix: spun for 50000000\n");
1287 * Cleaning up on uniprocessor is easy. For various reasons, we're
1288 * unlikely to have to even execute this code, including the fact
1289 * that the cleanup is deferred until the parent does a wait(2), which
1290 * means that another userland process has run.
1293 pmap_lazyfix(pmap_t pmap)
1297 cr3 = vtophys(pmap->pm_pdir);
1298 if (cr3 == rcr3()) {
1299 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1300 pmap->pm_active &= ~(PCPU_GET(cpumask));
1306 * Release any resources held by the given physical map.
1307 * Called when a pmap initialized by pmap_pinit is being released.
1308 * Should only be called if the map contains no valid mappings.
1311 pmap_release(pmap_t pmap)
1313 vm_page_t m, ptdpg[NPGPTD];
1316 KASSERT(pmap->pm_stats.resident_count == 0,
1317 ("pmap_release: pmap resident count %ld != 0",
1318 pmap->pm_stats.resident_count));
1321 mtx_lock_spin(&allpmaps_lock);
1322 LIST_REMOVE(pmap, pm_list);
1323 mtx_unlock_spin(&allpmaps_lock);
1325 for (i = 0; i < NPGPTD; i++)
1326 ptdpg[i] = PHYS_TO_VM_PAGE(pmap->pm_pdir[PTDPTDI + i]);
1328 bzero(pmap->pm_pdir + PTDPTDI, (nkpt + NPGPTD) *
1329 sizeof(*pmap->pm_pdir));
1331 pmap->pm_pdir[MPPTDI] = 0;
1334 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1336 vm_page_lock_queues();
1337 for (i = 0; i < NPGPTD; i++) {
1340 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME),
1341 ("pmap_release: got wrong ptd page"));
1344 atomic_subtract_int(&cnt.v_wire_count, 1);
1345 vm_page_free_zero(m);
1347 vm_page_unlock_queues();
1348 PMAP_LOCK_DESTROY(pmap);
1352 kvm_size(SYSCTL_HANDLER_ARGS)
1354 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1356 return sysctl_handle_long(oidp, &ksize, 0, req);
1358 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1359 0, 0, kvm_size, "IU", "Size of KVM");
1362 kvm_free(SYSCTL_HANDLER_ARGS)
1364 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1366 return sysctl_handle_long(oidp, &kfree, 0, req);
1368 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1369 0, 0, kvm_free, "IU", "Amount of KVM free");
1372 * grow the number of kernel page table entries, if needed
1375 pmap_growkernel(vm_offset_t addr)
1378 vm_paddr_t ptppaddr;
1383 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1384 if (kernel_vm_end == 0) {
1385 kernel_vm_end = KERNBASE;
1387 while (pdir_pde(PTD, kernel_vm_end)) {
1388 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1390 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1391 kernel_vm_end = kernel_map->max_offset;
1396 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1397 if (addr - 1 >= kernel_map->max_offset)
1398 addr = kernel_map->max_offset;
1399 while (kernel_vm_end < addr) {
1400 if (pdir_pde(PTD, kernel_vm_end)) {
1401 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1402 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1403 kernel_vm_end = kernel_map->max_offset;
1410 * This index is bogus, but out of the way
1412 nkpg = vm_page_alloc(NULL, nkpt,
1413 VM_ALLOC_NOOBJ | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
1415 panic("pmap_growkernel: no memory to grow kernel");
1419 pmap_zero_page(nkpg);
1420 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1421 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1422 pdir_pde(PTD, kernel_vm_end) = newpdir;
1424 mtx_lock_spin(&allpmaps_lock);
1425 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1426 pde = pmap_pde(pmap, kernel_vm_end);
1427 pde_store(pde, newpdir);
1429 mtx_unlock_spin(&allpmaps_lock);
1430 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1431 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1432 kernel_vm_end = kernel_map->max_offset;
1439 /***************************************************
1440 * page management routines.
1441 ***************************************************/
1444 * free the pv_entry back to the free list
1446 static PMAP_INLINE void
1447 free_pv_entry(pv_entry_t pv)
1450 uma_zfree(pvzone, pv);
1454 * get a new pv_entry, allocating a block from the system
1458 get_pv_entry(pmap_t locked_pmap)
1460 static const struct timeval printinterval = { 60, 0 };
1461 static struct timeval lastprint;
1462 struct vpgqueues *vpq;
1464 pt_entry_t *pte, tpte;
1465 pv_entry_t allocated_pv, next_pv, pv;
1469 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1470 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1471 allocated_pv = uma_zalloc(pvzone, M_NOWAIT);
1472 if (allocated_pv != NULL) {
1474 if (pv_entry_count > pv_entry_high_water)
1475 pagedaemon_wakeup();
1477 return (allocated_pv);
1481 * Reclaim pv entries: At first, destroy mappings to inactive
1482 * pages. After that, if a pv entry is still needed, destroy
1483 * mappings to active pages.
1485 if (ratecheck(&lastprint, &printinterval))
1486 printf("Approaching the limit on PV entries, "
1487 "increase the vm.pmap.shpgperproc tunable.\n");
1488 vpq = &vm_page_queues[PQ_INACTIVE];
1491 TAILQ_FOREACH(m, &vpq->pl, pageq) {
1492 if (m->hold_count || m->busy || (m->flags & PG_BUSY))
1494 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
1497 /* Avoid deadlock and lock recursion. */
1498 if (pmap > locked_pmap)
1500 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
1502 pmap->pm_stats.resident_count--;
1503 pte = pmap_pte_quick(pmap, va);
1504 tpte = pte_load_clear(pte);
1505 KASSERT((tpte & PG_W) == 0,
1506 ("get_pv_entry: wired pte %#jx", (uintmax_t)tpte));
1508 vm_page_flag_set(m, PG_REFERENCED);
1510 KASSERT((tpte & PG_RW),
1511 ("get_pv_entry: modified page not writable: va: %#x, pte: %#jx",
1512 va, (uintmax_t)tpte));
1513 if (pmap_track_modified(va))
1516 pmap_invalidate_page(pmap, va);
1517 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1518 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1519 if (TAILQ_EMPTY(&m->md.pv_list))
1520 vm_page_flag_clear(m, PG_WRITEABLE);
1521 m->md.pv_list_count--;
1522 pmap_unuse_pt(pmap, va);
1523 if (pmap != locked_pmap)
1525 if (allocated_pv == NULL)
1532 if (allocated_pv == NULL) {
1533 if (vpq == &vm_page_queues[PQ_INACTIVE]) {
1534 vpq = &vm_page_queues[PQ_ACTIVE];
1537 panic("get_pv_entry: increase the vm.pmap.shpgperproc tunable");
1539 return (allocated_pv);
1543 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1547 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1548 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1549 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1550 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1551 if (pmap == pv->pv_pmap && va == pv->pv_va)
1555 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1556 if (va == pv->pv_va)
1560 KASSERT(pv != NULL, ("pmap_remove_entry: pv not found"));
1561 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1562 m->md.pv_list_count--;
1563 if (TAILQ_EMPTY(&m->md.pv_list))
1564 vm_page_flag_clear(m, PG_WRITEABLE);
1565 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1570 * Create a pv entry for page at pa for
1574 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
1578 pv = get_pv_entry(pmap);
1582 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1583 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1584 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1585 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1586 m->md.pv_list_count++;
1590 * pmap_remove_pte: do the things to unmap a page in a process
1593 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va)
1598 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1599 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1600 oldpte = pte_load_clear(ptq);
1602 pmap->pm_stats.wired_count -= 1;
1604 * Machines that don't support invlpg, also don't support
1608 pmap_invalidate_page(kernel_pmap, va);
1609 pmap->pm_stats.resident_count -= 1;
1610 if (oldpte & PG_MANAGED) {
1611 m = PHYS_TO_VM_PAGE(oldpte);
1612 if (oldpte & PG_M) {
1613 KASSERT((oldpte & PG_RW),
1614 ("pmap_remove_pte: modified page not writable: va: %#x, pte: %#jx",
1615 va, (uintmax_t)oldpte));
1616 if (pmap_track_modified(va))
1620 vm_page_flag_set(m, PG_REFERENCED);
1621 pmap_remove_entry(pmap, m, va);
1623 return (pmap_unuse_pt(pmap, va));
1627 * Remove a single page from a process address space
1630 pmap_remove_page(pmap_t pmap, vm_offset_t va)
1634 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1635 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1636 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1637 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
1639 pmap_remove_pte(pmap, pte, va);
1640 pmap_invalidate_page(pmap, va);
1644 * Remove the given range of addresses from the specified map.
1646 * It is assumed that the start and end are properly
1647 * rounded to the page size.
1650 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1658 * Perform an unsynchronized read. This is, however, safe.
1660 if (pmap->pm_stats.resident_count == 0)
1665 vm_page_lock_queues();
1670 * special handling of removing one page. a very
1671 * common operation and easy to short circuit some
1674 if ((sva + PAGE_SIZE == eva) &&
1675 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1676 pmap_remove_page(pmap, sva);
1680 for (; sva < eva; sva = pdnxt) {
1684 * Calculate index for next page table.
1686 pdnxt = (sva + NBPDR) & ~PDRMASK;
1687 if (pmap->pm_stats.resident_count == 0)
1690 pdirindex = sva >> PDRSHIFT;
1691 ptpaddr = pmap->pm_pdir[pdirindex];
1694 * Weed out invalid mappings. Note: we assume that the page
1695 * directory table is always allocated, and in kernel virtual.
1701 * Check for large page.
1703 if ((ptpaddr & PG_PS) != 0) {
1704 pmap->pm_pdir[pdirindex] = 0;
1705 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1711 * Limit our scan to either the end of the va represented
1712 * by the current page table page, or to the end of the
1713 * range being removed.
1718 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
1724 * The TLB entry for a PG_G mapping is invalidated
1725 * by pmap_remove_pte().
1727 if ((*pte & PG_G) == 0)
1729 if (pmap_remove_pte(pmap, pte, sva))
1735 vm_page_unlock_queues();
1737 pmap_invalidate_all(pmap);
1742 * Routine: pmap_remove_all
1744 * Removes this physical page from
1745 * all physical maps in which it resides.
1746 * Reflects back modify bits to the pager.
1749 * Original versions of this routine were very
1750 * inefficient because they iteratively called
1751 * pmap_remove (slow...)
1755 pmap_remove_all(vm_page_t m)
1757 register pv_entry_t pv;
1758 pt_entry_t *pte, tpte;
1760 #if defined(PMAP_DIAGNOSTIC)
1762 * XXX This makes pmap_remove_all() illegal for non-managed pages!
1764 if (m->flags & PG_FICTITIOUS) {
1765 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%x",
1766 VM_PAGE_TO_PHYS(m));
1769 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1771 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1772 PMAP_LOCK(pv->pv_pmap);
1773 pv->pv_pmap->pm_stats.resident_count--;
1774 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1775 tpte = pte_load_clear(pte);
1777 pv->pv_pmap->pm_stats.wired_count--;
1779 vm_page_flag_set(m, PG_REFERENCED);
1782 * Update the vm_page_t clean and reference bits.
1785 KASSERT((tpte & PG_RW),
1786 ("pmap_remove_all: modified page not writable: va: %#x, pte: %#jx",
1787 pv->pv_va, (uintmax_t)tpte));
1788 if (pmap_track_modified(pv->pv_va))
1791 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
1792 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1793 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1794 m->md.pv_list_count--;
1795 pmap_unuse_pt(pv->pv_pmap, pv->pv_va);
1796 PMAP_UNLOCK(pv->pv_pmap);
1799 vm_page_flag_clear(m, PG_WRITEABLE);
1804 * Set the physical protection on the
1805 * specified range of this map as requested.
1808 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1815 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1816 pmap_remove(pmap, sva, eva);
1820 if (prot & VM_PROT_WRITE)
1825 vm_page_lock_queues();
1828 for (; sva < eva; sva = pdnxt) {
1829 unsigned obits, pbits, pdirindex;
1831 pdnxt = (sva + NBPDR) & ~PDRMASK;
1833 pdirindex = sva >> PDRSHIFT;
1834 ptpaddr = pmap->pm_pdir[pdirindex];
1837 * Weed out invalid mappings. Note: we assume that the page
1838 * directory table is always allocated, and in kernel virtual.
1844 * Check for large page.
1846 if ((ptpaddr & PG_PS) != 0) {
1847 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1855 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
1861 * Regardless of whether a pte is 32 or 64 bits in
1862 * size, PG_RW, PG_A, and PG_M are among the least
1863 * significant 32 bits.
1865 obits = pbits = *(u_int *)pte;
1866 if (pbits & PG_MANAGED) {
1869 m = PHYS_TO_VM_PAGE(*pte);
1870 vm_page_flag_set(m, PG_REFERENCED);
1873 if ((pbits & PG_M) != 0 &&
1874 pmap_track_modified(sva)) {
1876 m = PHYS_TO_VM_PAGE(*pte);
1881 pbits &= ~(PG_RW | PG_M);
1883 if (pbits != obits) {
1884 if (!atomic_cmpset_int((u_int *)pte, obits,
1888 pmap_invalidate_page(pmap, sva);
1895 vm_page_unlock_queues();
1897 pmap_invalidate_all(pmap);
1902 * Insert the given physical page (p) at
1903 * the specified virtual address (v) in the
1904 * target physical map with the protection requested.
1906 * If specified, the page will be wired down, meaning
1907 * that the related pte can not be reclaimed.
1909 * NB: This is the only routine which MAY NOT lazy-evaluate
1910 * or lose information. That is, this routine must actually
1911 * insert this page into the given map NOW.
1914 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1918 register pt_entry_t *pte;
1920 pt_entry_t origpte, newpte;
1925 #ifdef PMAP_DIAGNOSTIC
1926 if (va > VM_MAX_KERNEL_ADDRESS)
1927 panic("pmap_enter: toobig");
1928 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1929 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1934 vm_page_lock_queues();
1939 * In the case that a page table page is not
1940 * resident, we are creating it here.
1942 if (va < VM_MAXUSER_ADDRESS) {
1943 mpte = pmap_allocpte(pmap, va, M_WAITOK);
1945 #if 0 && defined(PMAP_DIAGNOSTIC)
1947 pd_entry_t *pdeaddr = pmap_pde(pmap, va);
1949 if ((origpte & PG_V) == 0) {
1950 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n",
1951 pmap->pm_pdir[PTDPTDI], origpte, va);
1956 pte = pmap_pte_quick(pmap, va);
1959 * Page Directory table entry not valid, we need a new PT page
1962 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x\n",
1963 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
1966 pa = VM_PAGE_TO_PHYS(m);
1969 opa = origpte & PG_FRAME;
1971 if (origpte & PG_PS) {
1973 * Yes, I know this will truncate upper address bits for PAE,
1974 * but I'm actually more interested in the lower bits
1976 printf("pmap_enter: va %p, pte %p, origpte %p\n",
1977 (void *)va, (void *)pte, (void *)(uintptr_t)origpte);
1978 panic("pmap_enter: attempted pmap_enter on 4MB page");
1982 * Mapping has not changed, must be protection or wiring change.
1984 if (origpte && (opa == pa)) {
1986 * Wiring change, just update stats. We don't worry about
1987 * wiring PT pages as they remain resident as long as there
1988 * are valid mappings in them. Hence, if a user page is wired,
1989 * the PT page will be also.
1991 if (wired && ((origpte & PG_W) == 0))
1992 pmap->pm_stats.wired_count++;
1993 else if (!wired && (origpte & PG_W))
1994 pmap->pm_stats.wired_count--;
1997 * Remove extra pte reference
2003 * We might be turning off write access to the page,
2004 * so we go ahead and sense modify status.
2006 if (origpte & PG_MANAGED) {
2013 * Mapping has changed, invalidate old range and fall through to
2014 * handle validating new mapping.
2018 pmap->pm_stats.wired_count--;
2019 if (origpte & PG_MANAGED) {
2020 om = PHYS_TO_VM_PAGE(opa);
2021 pmap_remove_entry(pmap, om, va);
2025 KASSERT(mpte->wire_count > 0,
2026 ("pmap_enter: missing reference to page table page,"
2030 pmap->pm_stats.resident_count++;
2033 * Enter on the PV list if part of our managed memory.
2035 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
2036 pmap_insert_entry(pmap, va, m);
2041 * Increment counters
2044 pmap->pm_stats.wired_count++;
2048 * Now validate mapping with desired protection/wiring.
2050 newpte = (pt_entry_t)(pa | PG_V);
2051 if ((prot & VM_PROT_WRITE) != 0)
2055 if (va < VM_MAXUSER_ADDRESS)
2057 if (pmap == kernel_pmap)
2061 * if the mapping or permission bits are different, we need
2062 * to update the pte.
2064 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2065 if (origpte & PG_V) {
2067 origpte = pte_load_store(pte, newpte | PG_A);
2068 if (origpte & PG_A) {
2069 if (origpte & PG_MANAGED)
2070 vm_page_flag_set(om, PG_REFERENCED);
2071 if (opa != VM_PAGE_TO_PHYS(m))
2074 if (origpte & PG_M) {
2075 KASSERT((origpte & PG_RW),
2076 ("pmap_enter: modified page not writable: va: %#x, pte: %#jx",
2077 va, (uintmax_t)origpte));
2078 if ((origpte & PG_MANAGED) &&
2079 pmap_track_modified(va))
2081 if ((prot & VM_PROT_WRITE) == 0)
2085 pmap_invalidate_page(pmap, va);
2087 pte_store(pte, newpte | PG_A);
2090 vm_page_unlock_queues();
2095 * this code makes some *MAJOR* assumptions:
2096 * 1. Current pmap & pmap exists.
2099 * 4. No page table pages.
2100 * but is *MUCH* faster than pmap_enter...
2104 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2110 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2111 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2115 * In the case that a page table page is not
2116 * resident, we are creating it here.
2118 if (va < VM_MAXUSER_ADDRESS) {
2123 * Calculate pagetable page index
2125 ptepindex = va >> PDRSHIFT;
2126 if (mpte && (mpte->pindex == ptepindex)) {
2131 * Get the page directory entry
2133 ptepa = pmap->pm_pdir[ptepindex];
2136 * If the page table page is mapped, we just increment
2137 * the hold count, and activate it.
2141 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2142 mpte = PHYS_TO_VM_PAGE(ptepa);
2145 mpte = _pmap_allocpte(pmap, ptepindex,
2150 vm_page_unlock_queues();
2151 VM_OBJECT_UNLOCK(m->object);
2153 VM_OBJECT_LOCK(m->object);
2154 vm_page_lock_queues();
2166 * This call to vtopte makes the assumption that we are
2167 * entering the page into the current pmap. In order to support
2168 * quick entry into any pmap, one would likely use pmap_pte_quick.
2169 * But that isn't as quick as vtopte.
2174 pmap_unwire_pte_hold(pmap, mpte);
2181 * Enter on the PV list if part of our managed memory. Note that we
2182 * raise IPL while manipulating pv_table since pmap_enter can be
2183 * called at interrupt time.
2185 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2186 pmap_insert_entry(pmap, va, m);
2189 * Increment counters
2191 pmap->pm_stats.resident_count++;
2193 pa = VM_PAGE_TO_PHYS(m);
2196 * Now validate mapping with RO protection
2198 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2199 pte_store(pte, pa | PG_V | PG_U);
2201 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
2208 * Make a temporary mapping for a physical address. This is only intended
2209 * to be used for panic dumps.
2212 pmap_kenter_temporary(vm_paddr_t pa, int i)
2216 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
2217 pmap_kenter(va, pa);
2219 return ((void *)crashdumpmap);
2223 * This code maps large physical mmap regions into the
2224 * processor address space. Note that some shortcuts
2225 * are taken, but the code works.
2228 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2229 vm_object_t object, vm_pindex_t pindex,
2234 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2235 KASSERT(object->type == OBJT_DEVICE,
2236 ("pmap_object_init_pt: non-device object"));
2238 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) {
2241 unsigned int ptepindex;
2246 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2250 p = vm_page_lookup(object, pindex);
2252 vm_page_lock_queues();
2253 if (vm_page_sleep_if_busy(p, FALSE, "init4p"))
2256 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2261 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2262 vm_page_lock_queues();
2264 vm_page_unlock_queues();
2268 p = vm_page_lookup(object, pindex);
2269 vm_page_lock_queues();
2272 vm_page_unlock_queues();
2274 ptepa = VM_PAGE_TO_PHYS(p);
2275 if (ptepa & (NBPDR - 1))
2278 p->valid = VM_PAGE_BITS_ALL;
2281 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2282 npdes = size >> PDRSHIFT;
2283 for(i = 0; i < npdes; i++) {
2284 pde_store(&pmap->pm_pdir[ptepindex],
2285 ptepa | PG_U | PG_RW | PG_V | PG_PS);
2289 pmap_invalidate_all(pmap);
2296 * Routine: pmap_change_wiring
2297 * Function: Change the wiring attribute for a map/virtual-address
2299 * In/out conditions:
2300 * The mapping must already exist in the pmap.
2303 pmap_change_wiring(pmap, va, wired)
2304 register pmap_t pmap;
2308 register pt_entry_t *pte;
2311 pte = pmap_pte(pmap, va);
2313 if (wired && !pmap_pte_w(pte))
2314 pmap->pm_stats.wired_count++;
2315 else if (!wired && pmap_pte_w(pte))
2316 pmap->pm_stats.wired_count--;
2319 * Wiring is not a hardware characteristic so there is no need to
2322 pmap_pte_set_w(pte, wired);
2323 pmap_pte_release(pte);
2330 * Copy the range specified by src_addr/len
2331 * from the source map to the range dst_addr/len
2332 * in the destination map.
2334 * This routine is only advisory and need not do anything.
2338 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
2339 vm_offset_t src_addr)
2342 vm_offset_t end_addr = src_addr + len;
2346 if (dst_addr != src_addr)
2349 if (!pmap_is_current(src_pmap))
2352 vm_page_lock_queues();
2353 if (dst_pmap < src_pmap) {
2354 PMAP_LOCK(dst_pmap);
2355 PMAP_LOCK(src_pmap);
2357 PMAP_LOCK(src_pmap);
2358 PMAP_LOCK(dst_pmap);
2361 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2362 pt_entry_t *src_pte, *dst_pte;
2363 vm_page_t dstmpte, srcmpte;
2364 pd_entry_t srcptepaddr;
2367 if (addr >= UPT_MIN_ADDRESS)
2368 panic("pmap_copy: invalid to pmap_copy page tables");
2371 * Don't let optional prefaulting of pages make us go
2372 * way below the low water mark of free pages or way
2373 * above high water mark of used pv entries.
2375 if (cnt.v_free_count < cnt.v_free_reserved ||
2376 pv_entry_count > pv_entry_high_water)
2379 pdnxt = (addr + NBPDR) & ~PDRMASK;
2380 ptepindex = addr >> PDRSHIFT;
2382 srcptepaddr = src_pmap->pm_pdir[ptepindex];
2383 if (srcptepaddr == 0)
2386 if (srcptepaddr & PG_PS) {
2387 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2388 dst_pmap->pm_pdir[ptepindex] = srcptepaddr;
2389 dst_pmap->pm_stats.resident_count +=
2395 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
2396 if (srcmpte->wire_count == 0)
2397 panic("pmap_copy: source page table page is unused");
2399 if (pdnxt > end_addr)
2402 src_pte = vtopte(addr);
2403 while (addr < pdnxt) {
2407 * we only virtual copy managed pages
2409 if ((ptetemp & PG_MANAGED) != 0) {
2411 * We have to check after allocpte for the
2412 * pte still being around... allocpte can
2415 dstmpte = pmap_allocpte(dst_pmap, addr,
2417 if (dstmpte == NULL)
2419 dst_pte = pmap_pte_quick(dst_pmap, addr);
2420 if (*dst_pte == 0) {
2422 * Clear the modified and
2423 * accessed (referenced) bits
2426 m = PHYS_TO_VM_PAGE(ptetemp);
2427 *dst_pte = ptetemp & ~(PG_M | PG_A);
2428 dst_pmap->pm_stats.resident_count++;
2429 pmap_insert_entry(dst_pmap, addr, m);
2431 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2432 if (dstmpte->wire_count >= srcmpte->wire_count)
2440 vm_page_unlock_queues();
2441 PMAP_UNLOCK(src_pmap);
2442 PMAP_UNLOCK(dst_pmap);
2445 static __inline void
2446 pagezero(void *page)
2448 #if defined(I686_CPU)
2449 if (cpu_class == CPUCLASS_686) {
2450 #if defined(CPU_ENABLE_SSE)
2451 if (cpu_feature & CPUID_SSE2)
2452 sse2_pagezero(page);
2455 i686_pagezero(page);
2458 bzero(page, PAGE_SIZE);
2462 * pmap_zero_page zeros the specified hardware page by mapping
2463 * the page into KVM and using bzero to clear its contents.
2466 pmap_zero_page(vm_page_t m)
2468 struct sysmaps *sysmaps;
2470 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2471 mtx_lock(&sysmaps->lock);
2472 if (*sysmaps->CMAP2)
2473 panic("pmap_zero_page: CMAP2 busy");
2475 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2476 invlcaddr(sysmaps->CADDR2);
2477 pagezero(sysmaps->CADDR2);
2478 *sysmaps->CMAP2 = 0;
2480 mtx_unlock(&sysmaps->lock);
2484 * pmap_zero_page_area zeros the specified hardware page by mapping
2485 * the page into KVM and using bzero to clear its contents.
2487 * off and size may not cover an area beyond a single hardware page.
2490 pmap_zero_page_area(vm_page_t m, int off, int size)
2492 struct sysmaps *sysmaps;
2494 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2495 mtx_lock(&sysmaps->lock);
2496 if (*sysmaps->CMAP2)
2497 panic("pmap_zero_page: CMAP2 busy");
2499 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2500 invlcaddr(sysmaps->CADDR2);
2501 if (off == 0 && size == PAGE_SIZE)
2502 pagezero(sysmaps->CADDR2);
2504 bzero((char *)sysmaps->CADDR2 + off, size);
2505 *sysmaps->CMAP2 = 0;
2507 mtx_unlock(&sysmaps->lock);
2511 * pmap_zero_page_idle zeros the specified hardware page by mapping
2512 * the page into KVM and using bzero to clear its contents. This
2513 * is intended to be called from the vm_pagezero process only and
2517 pmap_zero_page_idle(vm_page_t m)
2521 panic("pmap_zero_page: CMAP3 busy");
2523 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2531 * pmap_copy_page copies the specified (machine independent)
2532 * page by mapping the page into virtual memory and using
2533 * bcopy to copy the page, one machine dependent page at a
2537 pmap_copy_page(vm_page_t src, vm_page_t dst)
2539 struct sysmaps *sysmaps;
2541 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2542 mtx_lock(&sysmaps->lock);
2543 if (*sysmaps->CMAP1)
2544 panic("pmap_copy_page: CMAP1 busy");
2545 if (*sysmaps->CMAP2)
2546 panic("pmap_copy_page: CMAP2 busy");
2548 invlpg((u_int)sysmaps->CADDR1);
2549 invlpg((u_int)sysmaps->CADDR2);
2550 *sysmaps->CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A;
2551 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M;
2552 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
2553 *sysmaps->CMAP1 = 0;
2554 *sysmaps->CMAP2 = 0;
2556 mtx_unlock(&sysmaps->lock);
2560 * Returns true if the pmap's pv is one of the first
2561 * 16 pvs linked to from this page. This count may
2562 * be changed upwards or downwards in the future; it
2563 * is only necessary that true be returned for a small
2564 * subset of pmaps for proper page aging.
2567 pmap_page_exists_quick(pmap, m)
2574 if (m->flags & PG_FICTITIOUS)
2577 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2578 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2579 if (pv->pv_pmap == pmap) {
2589 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2591 * Remove all pages from specified address space
2592 * this aids process exit speeds. Also, this code
2593 * is special cased for current process only, but
2594 * can have the more generic (and slightly slower)
2595 * mode enabled. This is much faster than pmap_remove
2596 * in the case of running down an entire address space.
2599 pmap_remove_pages(pmap, sva, eva)
2601 vm_offset_t sva, eva;
2603 pt_entry_t *pte, tpte;
2607 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2608 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
2609 printf("warning: pmap_remove_pages called with non-current pmap\n");
2613 vm_page_lock_queues();
2616 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2618 if (pv->pv_va >= eva || pv->pv_va < sva) {
2619 npv = TAILQ_NEXT(pv, pv_plist);
2623 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2624 pte = vtopte(pv->pv_va);
2626 pte = pmap_pte_quick(pmap, pv->pv_va);
2631 printf("TPTE at %p IS ZERO @ VA %08x\n",
2637 * We cannot remove wired pages from a process' mapping at this time
2640 npv = TAILQ_NEXT(pv, pv_plist);
2644 m = PHYS_TO_VM_PAGE(tpte);
2645 KASSERT(m->phys_addr == (tpte & PG_FRAME),
2646 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
2647 m, (uintmax_t)m->phys_addr, (uintmax_t)tpte));
2649 KASSERT(m < &vm_page_array[vm_page_array_size],
2650 ("pmap_remove_pages: bad tpte %#jx", (uintmax_t)tpte));
2652 pmap->pm_stats.resident_count--;
2657 * Update the vm_page_t clean and reference bits.
2663 npv = TAILQ_NEXT(pv, pv_plist);
2664 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2666 m->md.pv_list_count--;
2667 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2668 if (TAILQ_EMPTY(&m->md.pv_list))
2669 vm_page_flag_clear(m, PG_WRITEABLE);
2671 pmap_unuse_pt(pmap, pv->pv_va);
2675 pmap_invalidate_all(pmap);
2677 vm_page_unlock_queues();
2683 * Return whether or not the specified physical page was modified
2684 * in any physical maps.
2687 pmap_is_modified(vm_page_t m)
2694 if (m->flags & PG_FICTITIOUS)
2698 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2699 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2701 * if the bit being tested is the modified bit, then
2702 * mark clean_map and ptes as never
2705 if (!pmap_track_modified(pv->pv_va))
2707 PMAP_LOCK(pv->pv_pmap);
2708 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2709 rv = (*pte & PG_M) != 0;
2710 PMAP_UNLOCK(pv->pv_pmap);
2719 * pmap_is_prefaultable:
2721 * Return whether or not the specified virtual address is elgible
2725 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2732 if (*pmap_pde(pmap, addr)) {
2741 * Clear the given bit in each of the given page's ptes. The bit is
2742 * expressed as a 32-bit mask. Consequently, if the pte is 64 bits in
2743 * size, only a bit within the least significant 32 can be cleared.
2745 static __inline void
2746 pmap_clear_ptes(vm_page_t m, int bit)
2748 register pv_entry_t pv;
2749 pt_entry_t pbits, *pte;
2751 if ((m->flags & PG_FICTITIOUS) ||
2752 (bit == PG_RW && (m->flags & PG_WRITEABLE) == 0))
2756 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2758 * Loop over all current mappings setting/clearing as appropos If
2759 * setting RO do we need to clear the VAC?
2761 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2763 * don't write protect pager mappings
2766 if (!pmap_track_modified(pv->pv_va))
2770 PMAP_LOCK(pv->pv_pmap);
2771 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2777 * Regardless of whether a pte is 32 or 64 bits
2778 * in size, PG_RW and PG_M are among the least
2779 * significant 32 bits.
2781 if (!atomic_cmpset_int((u_int *)pte, pbits,
2782 pbits & ~(PG_RW | PG_M)))
2788 atomic_clear_int((u_int *)pte, bit);
2790 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
2792 PMAP_UNLOCK(pv->pv_pmap);
2795 vm_page_flag_clear(m, PG_WRITEABLE);
2800 * pmap_page_protect:
2802 * Lower the permission for all mappings to a given page.
2805 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2807 if ((prot & VM_PROT_WRITE) == 0) {
2808 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2809 pmap_clear_ptes(m, PG_RW);
2817 * pmap_ts_referenced:
2819 * Return a count of reference bits for a page, clearing those bits.
2820 * It is not necessary for every reference bit to be cleared, but it
2821 * is necessary that 0 only be returned when there are truly no
2822 * reference bits set.
2824 * XXX: The exact number of bits to check and clear is a matter that
2825 * should be tested and standardized at some point in the future for
2826 * optimal aging of shared pages.
2829 pmap_ts_referenced(vm_page_t m)
2831 register pv_entry_t pv, pvf, pvn;
2836 if (m->flags & PG_FICTITIOUS)
2840 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2841 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2846 pvn = TAILQ_NEXT(pv, pv_list);
2848 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2850 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2852 if (!pmap_track_modified(pv->pv_va))
2855 PMAP_LOCK(pv->pv_pmap);
2856 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2858 if (pte && ((v = pte_load(pte)) & PG_A) != 0) {
2859 atomic_clear_int((u_int *)pte, PG_A);
2860 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
2864 PMAP_UNLOCK(pv->pv_pmap);
2868 PMAP_UNLOCK(pv->pv_pmap);
2869 } while ((pv = pvn) != NULL && pv != pvf);
2877 * Clear the modify bits on the specified physical page.
2880 pmap_clear_modify(vm_page_t m)
2882 pmap_clear_ptes(m, PG_M);
2886 * pmap_clear_reference:
2888 * Clear the reference bit on the specified physical page.
2891 pmap_clear_reference(vm_page_t m)
2893 pmap_clear_ptes(m, PG_A);
2897 * Miscellaneous support routines follow
2901 * Map a set of physical memory pages into the kernel virtual
2902 * address space. Return a pointer to where it is mapped. This
2903 * routine is intended to be used for mapping device memory,
2907 pmap_mapdev(pa, size)
2911 vm_offset_t va, tmpva, offset;
2913 offset = pa & PAGE_MASK;
2914 size = roundup(offset + size, PAGE_SIZE);
2917 if (pa < KERNLOAD && pa + size <= KERNLOAD)
2920 va = kmem_alloc_nofault(kernel_map, size);
2922 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2924 for (tmpva = va; size > 0; ) {
2925 pmap_kenter(tmpva, pa);
2930 pmap_invalidate_range(kernel_pmap, va, tmpva);
2931 return ((void *)(va + offset));
2935 pmap_unmapdev(va, size)
2939 vm_offset_t base, offset, tmpva;
2941 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
2943 base = va & PG_FRAME;
2944 offset = va & PAGE_MASK;
2945 size = roundup(offset + size, PAGE_SIZE);
2946 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
2947 pmap_kremove(tmpva);
2948 pmap_invalidate_range(kernel_pmap, va, tmpva);
2949 kmem_free(kernel_map, base, size);
2953 * perform the pmap work for mincore
2956 pmap_mincore(pmap, addr)
2960 pt_entry_t *ptep, pte;
2965 ptep = pmap_pte(pmap, addr);
2966 pte = (ptep != NULL) ? *ptep : 0;
2967 pmap_pte_release(ptep);
2973 val = MINCORE_INCORE;
2974 if ((pte & PG_MANAGED) == 0)
2977 pa = pte & PG_FRAME;
2979 m = PHYS_TO_VM_PAGE(pa);
2985 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2988 * Modified by someone else
2990 vm_page_lock_queues();
2991 if (m->dirty || pmap_is_modified(m))
2992 val |= MINCORE_MODIFIED_OTHER;
2993 vm_page_unlock_queues();
2999 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3002 * Referenced by someone else
3004 vm_page_lock_queues();
3005 if ((m->flags & PG_REFERENCED) ||
3006 pmap_ts_referenced(m)) {
3007 val |= MINCORE_REFERENCED_OTHER;
3008 vm_page_flag_set(m, PG_REFERENCED);
3010 vm_page_unlock_queues();
3017 pmap_activate(struct thread *td)
3019 pmap_t pmap, oldpmap;
3023 pmap = vmspace_pmap(td->td_proc->p_vmspace);
3024 oldpmap = PCPU_GET(curpmap);
3026 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask));
3027 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
3029 oldpmap->pm_active &= ~1;
3030 pmap->pm_active |= 1;
3033 cr3 = vtophys(pmap->pm_pdpt);
3035 cr3 = vtophys(pmap->pm_pdir);
3038 * pmap_activate is for the current thread on the current cpu
3040 td->td_pcb->pcb_cr3 = cr3;
3042 PCPU_SET(curpmap, pmap);
3047 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3050 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3054 addr = (addr + PDRMASK) & ~PDRMASK;
3059 #if defined(PMAP_DEBUG)
3060 pmap_pid_dump(int pid)
3067 sx_slock(&allproc_lock);
3068 LIST_FOREACH(p, &allproc, p_list) {
3069 if (p->p_pid != pid)
3075 pmap = vmspace_pmap(p->p_vmspace);
3076 for (i = 0; i < NPDEPTD; i++) {
3079 vm_offset_t base = i << PDRSHIFT;
3081 pde = &pmap->pm_pdir[i];
3082 if (pde && pmap_pde_v(pde)) {
3083 for (j = 0; j < NPTEPG; j++) {
3084 vm_offset_t va = base + (j << PAGE_SHIFT);
3085 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3090 sx_sunlock(&allproc_lock);
3093 pte = pmap_pte(pmap, va);
3094 if (pte && pmap_pte_v(pte)) {
3098 m = PHYS_TO_VM_PAGE(pa);
3099 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3100 va, pa, m->hold_count, m->wire_count, m->flags);
3115 sx_sunlock(&allproc_lock);
3122 static void pads(pmap_t pm);
3123 void pmap_pvdump(vm_offset_t pa);
3125 /* print address space of pmap*/
3134 if (pm == kernel_pmap)
3136 for (i = 0; i < NPDEPTD; i++)
3138 for (j = 0; j < NPTEPG; j++) {
3139 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3140 if (pm == kernel_pmap && va < KERNBASE)
3142 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3144 ptep = pmap_pte(pm, va);
3145 if (pmap_pte_v(ptep))
3146 printf("%x:%x ", va, *ptep);
3158 printf("pa %x", pa);
3159 m = PHYS_TO_VM_PAGE(pa);
3160 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3161 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);