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);
1392 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1393 while (kernel_vm_end < addr) {
1394 if (pdir_pde(PTD, kernel_vm_end)) {
1395 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1400 * This index is bogus, but out of the way
1402 nkpg = vm_page_alloc(NULL, nkpt,
1403 VM_ALLOC_NOOBJ | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
1405 panic("pmap_growkernel: no memory to grow kernel");
1409 pmap_zero_page(nkpg);
1410 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1411 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1412 pdir_pde(PTD, kernel_vm_end) = newpdir;
1414 mtx_lock_spin(&allpmaps_lock);
1415 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1416 pde = pmap_pde(pmap, kernel_vm_end);
1417 pde_store(pde, newpdir);
1419 mtx_unlock_spin(&allpmaps_lock);
1420 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1425 /***************************************************
1426 * page management routines.
1427 ***************************************************/
1430 * free the pv_entry back to the free list
1432 static PMAP_INLINE void
1433 free_pv_entry(pv_entry_t pv)
1436 uma_zfree(pvzone, pv);
1440 * get a new pv_entry, allocating a block from the system
1444 get_pv_entry(pmap_t locked_pmap)
1446 static const struct timeval printinterval = { 60, 0 };
1447 static struct timeval lastprint;
1448 struct vpgqueues *vpq;
1450 pt_entry_t *pte, tpte;
1451 pv_entry_t allocated_pv, next_pv, pv;
1455 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1456 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1457 allocated_pv = uma_zalloc(pvzone, M_NOWAIT);
1458 if (allocated_pv != NULL) {
1460 if (pv_entry_count > pv_entry_high_water)
1461 pagedaemon_wakeup();
1463 return (allocated_pv);
1467 * Reclaim pv entries: At first, destroy mappings to inactive
1468 * pages. After that, if a pv entry is still needed, destroy
1469 * mappings to active pages.
1471 if (ratecheck(&lastprint, &printinterval))
1472 printf("Approaching the limit on PV entries, "
1473 "increase the vm.pmap.shpgperproc tunable.\n");
1474 vpq = &vm_page_queues[PQ_INACTIVE];
1477 TAILQ_FOREACH(m, &vpq->pl, pageq) {
1478 if (m->hold_count || m->busy || (m->flags & PG_BUSY))
1480 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
1483 /* Avoid deadlock and lock recursion. */
1484 if (pmap > locked_pmap)
1486 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
1488 pmap->pm_stats.resident_count--;
1489 pte = pmap_pte_quick(pmap, va);
1490 tpte = pte_load_clear(pte);
1491 KASSERT((tpte & PG_W) == 0,
1492 ("get_pv_entry: wired pte %#jx", (uintmax_t)tpte));
1494 vm_page_flag_set(m, PG_REFERENCED);
1496 KASSERT((tpte & PG_RW),
1497 ("get_pv_entry: modified page not writable: va: %#x, pte: %#jx",
1498 va, (uintmax_t)tpte));
1499 if (pmap_track_modified(va))
1502 pmap_invalidate_page(pmap, va);
1503 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1504 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1505 if (TAILQ_EMPTY(&m->md.pv_list))
1506 vm_page_flag_clear(m, PG_WRITEABLE);
1507 m->md.pv_list_count--;
1508 pmap_unuse_pt(pmap, va);
1509 if (pmap != locked_pmap)
1511 if (allocated_pv == NULL)
1518 if (allocated_pv == NULL) {
1519 if (vpq == &vm_page_queues[PQ_INACTIVE]) {
1520 vpq = &vm_page_queues[PQ_ACTIVE];
1523 panic("get_pv_entry: increase the vm.pmap.shpgperproc tunable");
1525 return (allocated_pv);
1529 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1533 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1534 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1535 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1536 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1537 if (pmap == pv->pv_pmap && va == pv->pv_va)
1541 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1542 if (va == pv->pv_va)
1546 KASSERT(pv != NULL, ("pmap_remove_entry: pv not found"));
1547 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1548 m->md.pv_list_count--;
1549 if (TAILQ_EMPTY(&m->md.pv_list))
1550 vm_page_flag_clear(m, PG_WRITEABLE);
1551 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1556 * Create a pv entry for page at pa for
1560 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
1564 pv = get_pv_entry(pmap);
1568 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1569 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1570 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1571 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1572 m->md.pv_list_count++;
1576 * pmap_remove_pte: do the things to unmap a page in a process
1579 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va)
1584 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1585 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1586 oldpte = pte_load_clear(ptq);
1588 pmap->pm_stats.wired_count -= 1;
1590 * Machines that don't support invlpg, also don't support
1594 pmap_invalidate_page(kernel_pmap, va);
1595 pmap->pm_stats.resident_count -= 1;
1596 if (oldpte & PG_MANAGED) {
1597 m = PHYS_TO_VM_PAGE(oldpte);
1598 if (oldpte & PG_M) {
1599 KASSERT((oldpte & PG_RW),
1600 ("pmap_remove_pte: modified page not writable: va: %#x, pte: %#jx",
1601 va, (uintmax_t)oldpte));
1602 if (pmap_track_modified(va))
1606 vm_page_flag_set(m, PG_REFERENCED);
1607 pmap_remove_entry(pmap, m, va);
1609 return (pmap_unuse_pt(pmap, va));
1613 * Remove a single page from a process address space
1616 pmap_remove_page(pmap_t pmap, vm_offset_t va)
1620 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1621 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1622 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1623 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
1625 pmap_remove_pte(pmap, pte, va);
1626 pmap_invalidate_page(pmap, va);
1630 * Remove the given range of addresses from the specified map.
1632 * It is assumed that the start and end are properly
1633 * rounded to the page size.
1636 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1644 * Perform an unsynchronized read. This is, however, safe.
1646 if (pmap->pm_stats.resident_count == 0)
1651 vm_page_lock_queues();
1656 * special handling of removing one page. a very
1657 * common operation and easy to short circuit some
1660 if ((sva + PAGE_SIZE == eva) &&
1661 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1662 pmap_remove_page(pmap, sva);
1666 for (; sva < eva; sva = pdnxt) {
1670 * Calculate index for next page table.
1672 pdnxt = (sva + NBPDR) & ~PDRMASK;
1673 if (pmap->pm_stats.resident_count == 0)
1676 pdirindex = sva >> PDRSHIFT;
1677 ptpaddr = pmap->pm_pdir[pdirindex];
1680 * Weed out invalid mappings. Note: we assume that the page
1681 * directory table is always allocated, and in kernel virtual.
1687 * Check for large page.
1689 if ((ptpaddr & PG_PS) != 0) {
1690 pmap->pm_pdir[pdirindex] = 0;
1691 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1697 * Limit our scan to either the end of the va represented
1698 * by the current page table page, or to the end of the
1699 * range being removed.
1704 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
1709 if (pmap_remove_pte(pmap, pte, sva))
1715 vm_page_unlock_queues();
1717 pmap_invalidate_all(pmap);
1722 * Routine: pmap_remove_all
1724 * Removes this physical page from
1725 * all physical maps in which it resides.
1726 * Reflects back modify bits to the pager.
1729 * Original versions of this routine were very
1730 * inefficient because they iteratively called
1731 * pmap_remove (slow...)
1735 pmap_remove_all(vm_page_t m)
1737 register pv_entry_t pv;
1738 pt_entry_t *pte, tpte;
1740 #if defined(PMAP_DIAGNOSTIC)
1742 * XXX This makes pmap_remove_all() illegal for non-managed pages!
1744 if (m->flags & PG_FICTITIOUS) {
1745 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%x",
1746 VM_PAGE_TO_PHYS(m));
1749 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1751 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1752 PMAP_LOCK(pv->pv_pmap);
1753 pv->pv_pmap->pm_stats.resident_count--;
1754 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1755 tpte = pte_load_clear(pte);
1757 pv->pv_pmap->pm_stats.wired_count--;
1759 vm_page_flag_set(m, PG_REFERENCED);
1762 * Update the vm_page_t clean and reference bits.
1765 KASSERT((tpte & PG_RW),
1766 ("pmap_remove_all: modified page not writable: va: %#x, pte: %#jx",
1767 pv->pv_va, (uintmax_t)tpte));
1768 if (pmap_track_modified(pv->pv_va))
1771 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
1772 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1773 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1774 m->md.pv_list_count--;
1775 pmap_unuse_pt(pv->pv_pmap, pv->pv_va);
1776 PMAP_UNLOCK(pv->pv_pmap);
1779 vm_page_flag_clear(m, PG_WRITEABLE);
1784 * Set the physical protection on the
1785 * specified range of this map as requested.
1788 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1795 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1796 pmap_remove(pmap, sva, eva);
1800 if (prot & VM_PROT_WRITE)
1805 vm_page_lock_queues();
1808 for (; sva < eva; sva = pdnxt) {
1809 unsigned obits, pbits, pdirindex;
1811 pdnxt = (sva + NBPDR) & ~PDRMASK;
1813 pdirindex = sva >> PDRSHIFT;
1814 ptpaddr = pmap->pm_pdir[pdirindex];
1817 * Weed out invalid mappings. Note: we assume that the page
1818 * directory table is always allocated, and in kernel virtual.
1824 * Check for large page.
1826 if ((ptpaddr & PG_PS) != 0) {
1827 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1835 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
1841 * Regardless of whether a pte is 32 or 64 bits in
1842 * size, PG_RW, PG_A, and PG_M are among the least
1843 * significant 32 bits.
1845 obits = pbits = *(u_int *)pte;
1846 if (pbits & PG_MANAGED) {
1849 m = PHYS_TO_VM_PAGE(*pte);
1850 vm_page_flag_set(m, PG_REFERENCED);
1853 if ((pbits & PG_M) != 0 &&
1854 pmap_track_modified(sva)) {
1856 m = PHYS_TO_VM_PAGE(*pte);
1861 pbits &= ~(PG_RW | PG_M);
1863 if (pbits != obits) {
1864 if (!atomic_cmpset_int((u_int *)pte, obits,
1868 pmap_invalidate_page(pmap, sva);
1875 vm_page_unlock_queues();
1877 pmap_invalidate_all(pmap);
1882 * Insert the given physical page (p) at
1883 * the specified virtual address (v) in the
1884 * target physical map with the protection requested.
1886 * If specified, the page will be wired down, meaning
1887 * that the related pte can not be reclaimed.
1889 * NB: This is the only routine which MAY NOT lazy-evaluate
1890 * or lose information. That is, this routine must actually
1891 * insert this page into the given map NOW.
1894 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1898 register pt_entry_t *pte;
1900 pt_entry_t origpte, newpte;
1905 #ifdef PMAP_DIAGNOSTIC
1906 if (va > VM_MAX_KERNEL_ADDRESS)
1907 panic("pmap_enter: toobig");
1908 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1909 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1914 vm_page_lock_queues();
1919 * In the case that a page table page is not
1920 * resident, we are creating it here.
1922 if (va < VM_MAXUSER_ADDRESS) {
1923 mpte = pmap_allocpte(pmap, va, M_WAITOK);
1925 #if 0 && defined(PMAP_DIAGNOSTIC)
1927 pd_entry_t *pdeaddr = pmap_pde(pmap, va);
1929 if ((origpte & PG_V) == 0) {
1930 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n",
1931 pmap->pm_pdir[PTDPTDI], origpte, va);
1936 pte = pmap_pte_quick(pmap, va);
1939 * Page Directory table entry not valid, we need a new PT page
1942 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x\n",
1943 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
1946 pa = VM_PAGE_TO_PHYS(m);
1949 opa = origpte & PG_FRAME;
1951 if (origpte & PG_PS) {
1953 * Yes, I know this will truncate upper address bits for PAE,
1954 * but I'm actually more interested in the lower bits
1956 printf("pmap_enter: va %p, pte %p, origpte %p\n",
1957 (void *)va, (void *)pte, (void *)(uintptr_t)origpte);
1958 panic("pmap_enter: attempted pmap_enter on 4MB page");
1962 * Mapping has not changed, must be protection or wiring change.
1964 if (origpte && (opa == pa)) {
1966 * Wiring change, just update stats. We don't worry about
1967 * wiring PT pages as they remain resident as long as there
1968 * are valid mappings in them. Hence, if a user page is wired,
1969 * the PT page will be also.
1971 if (wired && ((origpte & PG_W) == 0))
1972 pmap->pm_stats.wired_count++;
1973 else if (!wired && (origpte & PG_W))
1974 pmap->pm_stats.wired_count--;
1977 * Remove extra pte reference
1983 * We might be turning off write access to the page,
1984 * so we go ahead and sense modify status.
1986 if (origpte & PG_MANAGED) {
1993 * Mapping has changed, invalidate old range and fall through to
1994 * handle validating new mapping.
1998 pmap->pm_stats.wired_count--;
1999 if (origpte & PG_MANAGED) {
2000 om = PHYS_TO_VM_PAGE(opa);
2001 pmap_remove_entry(pmap, om, va);
2005 KASSERT(mpte->wire_count > 0,
2006 ("pmap_enter: missing reference to page table page,"
2010 pmap->pm_stats.resident_count++;
2013 * Enter on the PV list if part of our managed memory.
2015 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
2016 pmap_insert_entry(pmap, va, m);
2021 * Increment counters
2024 pmap->pm_stats.wired_count++;
2028 * Now validate mapping with desired protection/wiring.
2030 newpte = (pt_entry_t)(pa | PG_V);
2031 if ((prot & VM_PROT_WRITE) != 0)
2035 if (va < VM_MAXUSER_ADDRESS)
2037 if (pmap == kernel_pmap)
2041 * if the mapping or permission bits are different, we need
2042 * to update the pte.
2044 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2045 if (origpte & PG_V) {
2047 origpte = pte_load_store(pte, newpte | PG_A);
2048 if (origpte & PG_A) {
2049 if (origpte & PG_MANAGED)
2050 vm_page_flag_set(om, PG_REFERENCED);
2051 if (opa != VM_PAGE_TO_PHYS(m))
2054 if (origpte & PG_M) {
2055 KASSERT((origpte & PG_RW),
2056 ("pmap_enter: modified page not writable: va: %#x, pte: %#jx",
2057 va, (uintmax_t)origpte));
2058 if ((origpte & PG_MANAGED) &&
2059 pmap_track_modified(va))
2061 if ((prot & VM_PROT_WRITE) == 0)
2065 pmap_invalidate_page(pmap, va);
2067 pte_store(pte, newpte | PG_A);
2070 vm_page_unlock_queues();
2075 * this code makes some *MAJOR* assumptions:
2076 * 1. Current pmap & pmap exists.
2079 * 4. No page table pages.
2080 * but is *MUCH* faster than pmap_enter...
2084 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2090 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2091 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
2095 * In the case that a page table page is not
2096 * resident, we are creating it here.
2098 if (va < VM_MAXUSER_ADDRESS) {
2103 * Calculate pagetable page index
2105 ptepindex = va >> PDRSHIFT;
2106 if (mpte && (mpte->pindex == ptepindex)) {
2111 * Get the page directory entry
2113 ptepa = pmap->pm_pdir[ptepindex];
2116 * If the page table page is mapped, we just increment
2117 * the hold count, and activate it.
2121 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2122 mpte = PHYS_TO_VM_PAGE(ptepa);
2125 mpte = _pmap_allocpte(pmap, ptepindex,
2130 vm_page_unlock_queues();
2131 VM_OBJECT_UNLOCK(m->object);
2133 VM_OBJECT_LOCK(m->object);
2134 vm_page_lock_queues();
2146 * This call to vtopte makes the assumption that we are
2147 * entering the page into the current pmap. In order to support
2148 * quick entry into any pmap, one would likely use pmap_pte_quick.
2149 * But that isn't as quick as vtopte.
2154 pmap_unwire_pte_hold(pmap, mpte);
2161 * Enter on the PV list if part of our managed memory. Note that we
2162 * raise IPL while manipulating pv_table since pmap_enter can be
2163 * called at interrupt time.
2165 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2166 pmap_insert_entry(pmap, va, m);
2169 * Increment counters
2171 pmap->pm_stats.resident_count++;
2173 pa = VM_PAGE_TO_PHYS(m);
2176 * Now validate mapping with RO protection
2178 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2179 pte_store(pte, pa | PG_V | PG_U);
2181 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
2188 * Make a temporary mapping for a physical address. This is only intended
2189 * to be used for panic dumps.
2192 pmap_kenter_temporary(vm_paddr_t pa, int i)
2196 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
2197 pmap_kenter(va, pa);
2199 return ((void *)crashdumpmap);
2203 * This code maps large physical mmap regions into the
2204 * processor address space. Note that some shortcuts
2205 * are taken, but the code works.
2208 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2209 vm_object_t object, vm_pindex_t pindex,
2214 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2215 KASSERT(object->type == OBJT_DEVICE,
2216 ("pmap_object_init_pt: non-device object"));
2218 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) {
2221 unsigned int ptepindex;
2226 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2230 p = vm_page_lookup(object, pindex);
2232 vm_page_lock_queues();
2233 if (vm_page_sleep_if_busy(p, FALSE, "init4p"))
2236 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2241 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2242 vm_page_lock_queues();
2244 vm_page_unlock_queues();
2248 p = vm_page_lookup(object, pindex);
2249 vm_page_lock_queues();
2252 vm_page_unlock_queues();
2254 ptepa = VM_PAGE_TO_PHYS(p);
2255 if (ptepa & (NBPDR - 1))
2258 p->valid = VM_PAGE_BITS_ALL;
2261 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2262 npdes = size >> PDRSHIFT;
2263 for(i = 0; i < npdes; i++) {
2264 pde_store(&pmap->pm_pdir[ptepindex],
2265 ptepa | PG_U | PG_RW | PG_V | PG_PS);
2269 pmap_invalidate_all(pmap);
2276 * Routine: pmap_change_wiring
2277 * Function: Change the wiring attribute for a map/virtual-address
2279 * In/out conditions:
2280 * The mapping must already exist in the pmap.
2283 pmap_change_wiring(pmap, va, wired)
2284 register pmap_t pmap;
2288 register pt_entry_t *pte;
2291 pte = pmap_pte(pmap, va);
2293 if (wired && !pmap_pte_w(pte))
2294 pmap->pm_stats.wired_count++;
2295 else if (!wired && pmap_pte_w(pte))
2296 pmap->pm_stats.wired_count--;
2299 * Wiring is not a hardware characteristic so there is no need to
2302 pmap_pte_set_w(pte, wired);
2303 pmap_pte_release(pte);
2310 * Copy the range specified by src_addr/len
2311 * from the source map to the range dst_addr/len
2312 * in the destination map.
2314 * This routine is only advisory and need not do anything.
2318 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
2319 vm_offset_t src_addr)
2322 vm_offset_t end_addr = src_addr + len;
2326 if (dst_addr != src_addr)
2329 if (!pmap_is_current(src_pmap))
2332 vm_page_lock_queues();
2333 if (dst_pmap < src_pmap) {
2334 PMAP_LOCK(dst_pmap);
2335 PMAP_LOCK(src_pmap);
2337 PMAP_LOCK(src_pmap);
2338 PMAP_LOCK(dst_pmap);
2341 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2342 pt_entry_t *src_pte, *dst_pte;
2343 vm_page_t dstmpte, srcmpte;
2344 pd_entry_t srcptepaddr;
2347 if (addr >= UPT_MIN_ADDRESS)
2348 panic("pmap_copy: invalid to pmap_copy page tables");
2351 * Don't let optional prefaulting of pages make us go
2352 * way below the low water mark of free pages or way
2353 * above high water mark of used pv entries.
2355 if (cnt.v_free_count < cnt.v_free_reserved ||
2356 pv_entry_count > pv_entry_high_water)
2359 pdnxt = (addr + NBPDR) & ~PDRMASK;
2360 ptepindex = addr >> PDRSHIFT;
2362 srcptepaddr = src_pmap->pm_pdir[ptepindex];
2363 if (srcptepaddr == 0)
2366 if (srcptepaddr & PG_PS) {
2367 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2368 dst_pmap->pm_pdir[ptepindex] = srcptepaddr;
2369 dst_pmap->pm_stats.resident_count +=
2375 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
2376 if (srcmpte->wire_count == 0)
2377 panic("pmap_copy: source page table page is unused");
2379 if (pdnxt > end_addr)
2382 src_pte = vtopte(addr);
2383 while (addr < pdnxt) {
2387 * we only virtual copy managed pages
2389 if ((ptetemp & PG_MANAGED) != 0) {
2391 * We have to check after allocpte for the
2392 * pte still being around... allocpte can
2395 dstmpte = pmap_allocpte(dst_pmap, addr,
2397 if (dstmpte == NULL)
2399 dst_pte = pmap_pte_quick(dst_pmap, addr);
2400 if (*dst_pte == 0) {
2402 * Clear the modified and
2403 * accessed (referenced) bits
2406 m = PHYS_TO_VM_PAGE(ptetemp);
2407 *dst_pte = ptetemp & ~(PG_M | PG_A);
2408 dst_pmap->pm_stats.resident_count++;
2409 pmap_insert_entry(dst_pmap, addr, m);
2411 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2412 if (dstmpte->wire_count >= srcmpte->wire_count)
2420 vm_page_unlock_queues();
2421 PMAP_UNLOCK(src_pmap);
2422 PMAP_UNLOCK(dst_pmap);
2425 static __inline void
2426 pagezero(void *page)
2428 #if defined(I686_CPU)
2429 if (cpu_class == CPUCLASS_686) {
2430 #if defined(CPU_ENABLE_SSE)
2431 if (cpu_feature & CPUID_SSE2)
2432 sse2_pagezero(page);
2435 i686_pagezero(page);
2438 bzero(page, PAGE_SIZE);
2442 * pmap_zero_page zeros the specified hardware page by mapping
2443 * the page into KVM and using bzero to clear its contents.
2446 pmap_zero_page(vm_page_t m)
2448 struct sysmaps *sysmaps;
2450 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2451 mtx_lock(&sysmaps->lock);
2452 if (*sysmaps->CMAP2)
2453 panic("pmap_zero_page: CMAP2 busy");
2455 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2456 invlcaddr(sysmaps->CADDR2);
2457 pagezero(sysmaps->CADDR2);
2458 *sysmaps->CMAP2 = 0;
2460 mtx_unlock(&sysmaps->lock);
2464 * pmap_zero_page_area zeros the specified hardware page by mapping
2465 * the page into KVM and using bzero to clear its contents.
2467 * off and size may not cover an area beyond a single hardware page.
2470 pmap_zero_page_area(vm_page_t m, int off, int size)
2472 struct sysmaps *sysmaps;
2474 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2475 mtx_lock(&sysmaps->lock);
2476 if (*sysmaps->CMAP2)
2477 panic("pmap_zero_page: CMAP2 busy");
2479 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2480 invlcaddr(sysmaps->CADDR2);
2481 if (off == 0 && size == PAGE_SIZE)
2482 pagezero(sysmaps->CADDR2);
2484 bzero((char *)sysmaps->CADDR2 + off, size);
2485 *sysmaps->CMAP2 = 0;
2487 mtx_unlock(&sysmaps->lock);
2491 * pmap_zero_page_idle zeros the specified hardware page by mapping
2492 * the page into KVM and using bzero to clear its contents. This
2493 * is intended to be called from the vm_pagezero process only and
2497 pmap_zero_page_idle(vm_page_t m)
2501 panic("pmap_zero_page: CMAP3 busy");
2503 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2511 * pmap_copy_page copies the specified (machine independent)
2512 * page by mapping the page into virtual memory and using
2513 * bcopy to copy the page, one machine dependent page at a
2517 pmap_copy_page(vm_page_t src, vm_page_t dst)
2519 struct sysmaps *sysmaps;
2521 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2522 mtx_lock(&sysmaps->lock);
2523 if (*sysmaps->CMAP1)
2524 panic("pmap_copy_page: CMAP1 busy");
2525 if (*sysmaps->CMAP2)
2526 panic("pmap_copy_page: CMAP2 busy");
2528 invlpg((u_int)sysmaps->CADDR1);
2529 invlpg((u_int)sysmaps->CADDR2);
2530 *sysmaps->CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A;
2531 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M;
2532 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
2533 *sysmaps->CMAP1 = 0;
2534 *sysmaps->CMAP2 = 0;
2536 mtx_unlock(&sysmaps->lock);
2540 * Returns true if the pmap's pv is one of the first
2541 * 16 pvs linked to from this page. This count may
2542 * be changed upwards or downwards in the future; it
2543 * is only necessary that true be returned for a small
2544 * subset of pmaps for proper page aging.
2547 pmap_page_exists_quick(pmap, m)
2554 if (m->flags & PG_FICTITIOUS)
2557 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2558 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2559 if (pv->pv_pmap == pmap) {
2569 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2571 * Remove all pages from specified address space
2572 * this aids process exit speeds. Also, this code
2573 * is special cased for current process only, but
2574 * can have the more generic (and slightly slower)
2575 * mode enabled. This is much faster than pmap_remove
2576 * in the case of running down an entire address space.
2579 pmap_remove_pages(pmap, sva, eva)
2581 vm_offset_t sva, eva;
2583 pt_entry_t *pte, tpte;
2587 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2588 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
2589 printf("warning: pmap_remove_pages called with non-current pmap\n");
2593 vm_page_lock_queues();
2596 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2598 if (pv->pv_va >= eva || pv->pv_va < sva) {
2599 npv = TAILQ_NEXT(pv, pv_plist);
2603 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2604 pte = vtopte(pv->pv_va);
2606 pte = pmap_pte_quick(pmap, pv->pv_va);
2611 printf("TPTE at %p IS ZERO @ VA %08x\n",
2617 * We cannot remove wired pages from a process' mapping at this time
2620 npv = TAILQ_NEXT(pv, pv_plist);
2624 m = PHYS_TO_VM_PAGE(tpte);
2625 KASSERT(m->phys_addr == (tpte & PG_FRAME),
2626 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
2627 m, (uintmax_t)m->phys_addr, (uintmax_t)tpte));
2629 KASSERT(m < &vm_page_array[vm_page_array_size],
2630 ("pmap_remove_pages: bad tpte %#jx", (uintmax_t)tpte));
2632 pmap->pm_stats.resident_count--;
2637 * Update the vm_page_t clean and reference bits.
2643 npv = TAILQ_NEXT(pv, pv_plist);
2644 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2646 m->md.pv_list_count--;
2647 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2648 if (TAILQ_EMPTY(&m->md.pv_list))
2649 vm_page_flag_clear(m, PG_WRITEABLE);
2651 pmap_unuse_pt(pmap, pv->pv_va);
2655 pmap_invalidate_all(pmap);
2657 vm_page_unlock_queues();
2663 * Return whether or not the specified physical page was modified
2664 * in any physical maps.
2667 pmap_is_modified(vm_page_t m)
2674 if (m->flags & PG_FICTITIOUS)
2678 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2679 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2681 * if the bit being tested is the modified bit, then
2682 * mark clean_map and ptes as never
2685 if (!pmap_track_modified(pv->pv_va))
2687 PMAP_LOCK(pv->pv_pmap);
2688 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2689 rv = (*pte & PG_M) != 0;
2690 PMAP_UNLOCK(pv->pv_pmap);
2699 * pmap_is_prefaultable:
2701 * Return whether or not the specified virtual address is elgible
2705 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
2712 if (*pmap_pde(pmap, addr)) {
2721 * Clear the given bit in each of the given page's ptes. The bit is
2722 * expressed as a 32-bit mask. Consequently, if the pte is 64 bits in
2723 * size, only a bit within the least significant 32 can be cleared.
2725 static __inline void
2726 pmap_clear_ptes(vm_page_t m, int bit)
2728 register pv_entry_t pv;
2729 pt_entry_t pbits, *pte;
2731 if ((m->flags & PG_FICTITIOUS) ||
2732 (bit == PG_RW && (m->flags & PG_WRITEABLE) == 0))
2736 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2738 * Loop over all current mappings setting/clearing as appropos If
2739 * setting RO do we need to clear the VAC?
2741 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2743 * don't write protect pager mappings
2746 if (!pmap_track_modified(pv->pv_va))
2750 PMAP_LOCK(pv->pv_pmap);
2751 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2757 * Regardless of whether a pte is 32 or 64 bits
2758 * in size, PG_RW and PG_M are among the least
2759 * significant 32 bits.
2761 if (!atomic_cmpset_int((u_int *)pte, pbits,
2762 pbits & ~(PG_RW | PG_M)))
2768 atomic_clear_int((u_int *)pte, bit);
2770 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
2772 PMAP_UNLOCK(pv->pv_pmap);
2775 vm_page_flag_clear(m, PG_WRITEABLE);
2780 * pmap_page_protect:
2782 * Lower the permission for all mappings to a given page.
2785 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2787 if ((prot & VM_PROT_WRITE) == 0) {
2788 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2789 pmap_clear_ptes(m, PG_RW);
2797 * pmap_ts_referenced:
2799 * Return a count of reference bits for a page, clearing those bits.
2800 * It is not necessary for every reference bit to be cleared, but it
2801 * is necessary that 0 only be returned when there are truly no
2802 * reference bits set.
2804 * XXX: The exact number of bits to check and clear is a matter that
2805 * should be tested and standardized at some point in the future for
2806 * optimal aging of shared pages.
2809 pmap_ts_referenced(vm_page_t m)
2811 register pv_entry_t pv, pvf, pvn;
2816 if (m->flags & PG_FICTITIOUS)
2820 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2821 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2826 pvn = TAILQ_NEXT(pv, pv_list);
2828 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2830 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2832 if (!pmap_track_modified(pv->pv_va))
2835 PMAP_LOCK(pv->pv_pmap);
2836 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2838 if (pte && ((v = pte_load(pte)) & PG_A) != 0) {
2839 atomic_clear_int((u_int *)pte, PG_A);
2840 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
2844 PMAP_UNLOCK(pv->pv_pmap);
2848 PMAP_UNLOCK(pv->pv_pmap);
2849 } while ((pv = pvn) != NULL && pv != pvf);
2857 * Clear the modify bits on the specified physical page.
2860 pmap_clear_modify(vm_page_t m)
2862 pmap_clear_ptes(m, PG_M);
2866 * pmap_clear_reference:
2868 * Clear the reference bit on the specified physical page.
2871 pmap_clear_reference(vm_page_t m)
2873 pmap_clear_ptes(m, PG_A);
2877 * Miscellaneous support routines follow
2881 * Map a set of physical memory pages into the kernel virtual
2882 * address space. Return a pointer to where it is mapped. This
2883 * routine is intended to be used for mapping device memory,
2887 pmap_mapdev(pa, size)
2891 vm_offset_t va, tmpva, offset;
2893 offset = pa & PAGE_MASK;
2894 size = roundup(offset + size, PAGE_SIZE);
2897 if (pa < KERNLOAD && pa + size <= KERNLOAD)
2900 va = kmem_alloc_nofault(kernel_map, size);
2902 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2904 for (tmpva = va; size > 0; ) {
2905 pmap_kenter(tmpva, pa);
2910 pmap_invalidate_range(kernel_pmap, va, tmpva);
2911 return ((void *)(va + offset));
2915 pmap_unmapdev(va, size)
2919 vm_offset_t base, offset, tmpva;
2921 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
2923 base = va & PG_FRAME;
2924 offset = va & PAGE_MASK;
2925 size = roundup(offset + size, PAGE_SIZE);
2926 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
2927 pmap_kremove(tmpva);
2928 pmap_invalidate_range(kernel_pmap, va, tmpva);
2929 kmem_free(kernel_map, base, size);
2933 * perform the pmap work for mincore
2936 pmap_mincore(pmap, addr)
2940 pt_entry_t *ptep, pte;
2945 ptep = pmap_pte(pmap, addr);
2946 pte = (ptep != NULL) ? *ptep : 0;
2947 pmap_pte_release(ptep);
2953 val = MINCORE_INCORE;
2954 if ((pte & PG_MANAGED) == 0)
2957 pa = pte & PG_FRAME;
2959 m = PHYS_TO_VM_PAGE(pa);
2965 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2968 * Modified by someone else
2970 vm_page_lock_queues();
2971 if (m->dirty || pmap_is_modified(m))
2972 val |= MINCORE_MODIFIED_OTHER;
2973 vm_page_unlock_queues();
2979 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
2982 * Referenced by someone else
2984 vm_page_lock_queues();
2985 if ((m->flags & PG_REFERENCED) ||
2986 pmap_ts_referenced(m)) {
2987 val |= MINCORE_REFERENCED_OTHER;
2988 vm_page_flag_set(m, PG_REFERENCED);
2990 vm_page_unlock_queues();
2997 pmap_activate(struct thread *td)
2999 struct proc *p = td->td_proc;
3000 pmap_t pmap, oldpmap;
3004 pmap = vmspace_pmap(td->td_proc->p_vmspace);
3005 oldpmap = PCPU_GET(curpmap);
3007 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask));
3008 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
3010 oldpmap->pm_active &= ~1;
3011 pmap->pm_active |= 1;
3014 cr3 = vtophys(pmap->pm_pdpt);
3016 cr3 = vtophys(pmap->pm_pdir);
3018 /* XXXKSE this is wrong.
3019 * pmap_activate is for the current thread on the current cpu
3021 if (p->p_flag & P_SA) {
3022 /* Make sure all other cr3 entries are updated. */
3023 /* what if they are running? XXXKSE (maybe abort them) */
3024 FOREACH_THREAD_IN_PROC(p, td) {
3025 td->td_pcb->pcb_cr3 = cr3;
3028 td->td_pcb->pcb_cr3 = cr3;
3031 PCPU_SET(curpmap, pmap);
3036 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3039 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3043 addr = (addr + PDRMASK) & ~PDRMASK;
3048 #if defined(PMAP_DEBUG)
3049 pmap_pid_dump(int pid)
3056 sx_slock(&allproc_lock);
3057 LIST_FOREACH(p, &allproc, p_list) {
3058 if (p->p_pid != pid)
3064 pmap = vmspace_pmap(p->p_vmspace);
3065 for (i = 0; i < NPDEPTD; i++) {
3068 vm_offset_t base = i << PDRSHIFT;
3070 pde = &pmap->pm_pdir[i];
3071 if (pde && pmap_pde_v(pde)) {
3072 for (j = 0; j < NPTEPG; j++) {
3073 vm_offset_t va = base + (j << PAGE_SHIFT);
3074 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3079 sx_sunlock(&allproc_lock);
3082 pte = pmap_pte(pmap, va);
3083 if (pte && pmap_pte_v(pte)) {
3087 m = PHYS_TO_VM_PAGE(pa);
3088 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3089 va, pa, m->hold_count, m->wire_count, m->flags);
3104 sx_sunlock(&allproc_lock);
3111 static void pads(pmap_t pm);
3112 void pmap_pvdump(vm_offset_t pa);
3114 /* print address space of pmap*/
3123 if (pm == kernel_pmap)
3125 for (i = 0; i < NPDEPTD; i++)
3127 for (j = 0; j < NPTEPG; j++) {
3128 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3129 if (pm == kernel_pmap && va < KERNBASE)
3131 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3133 ptep = pmap_pte(pm, va);
3134 if (pmap_pte_v(ptep))
3135 printf("%x:%x ", va, *ptep);
3147 printf("pa %x", pa);
3148 m = PHYS_TO_VM_PAGE(pa);
3149 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3150 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);