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
173 #define PV_STAT(x) do { x ; } while (0)
175 #define PV_STAT(x) do { } while (0)
179 * Get PDEs and PTEs for user/kernel address space
181 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
182 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
184 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
185 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
186 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
187 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
188 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
190 #define pmap_pte_set_w(pte, v) ((v) ? atomic_set_int((u_int *)(pte), PG_W) : \
191 atomic_clear_int((u_int *)(pte), PG_W))
192 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
194 struct pmap kernel_pmap_store;
195 LIST_HEAD(pmaplist, pmap);
196 static struct pmaplist allpmaps;
197 static struct mtx allpmaps_lock;
199 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
200 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
201 int pgeflag = 0; /* PG_G or-in */
202 int pseflag = 0; /* PG_PS or-in */
205 vm_offset_t kernel_vm_end;
206 extern u_int32_t KERNend;
209 static uma_zone_t pdptzone;
213 * Data for the pv entry allocation mechanism
215 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
216 static int shpgperproc = PMAP_SHPGPERPROC;
218 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
219 int pv_maxchunks; /* How many chunks we have KVA for */
220 vm_offset_t pv_vafree; /* freelist stored in the PTE */
223 * All those kernel PT submaps that BSD is so fond of
232 static struct sysmaps sysmaps_pcpu[MAXCPU];
233 pt_entry_t *CMAP1 = 0;
234 static pt_entry_t *CMAP3;
235 caddr_t CADDR1 = 0, ptvmmap = 0;
236 static caddr_t CADDR3;
237 struct msgbuf *msgbufp = 0;
242 static caddr_t crashdumpmap;
245 extern pt_entry_t *SMPpt;
247 static pt_entry_t *PMAP1 = 0, *PMAP2;
248 static pt_entry_t *PADDR1 = 0, *PADDR2;
251 static int PMAP1changedcpu;
252 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
254 "Number of times pmap_pte_quick changed CPU with same PMAP1");
256 static int PMAP1changed;
257 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
259 "Number of times pmap_pte_quick changed PMAP1");
260 static int PMAP1unchanged;
261 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
263 "Number of times pmap_pte_quick didn't change PMAP1");
264 static struct mtx PMAP2mutex;
266 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
267 static pv_entry_t get_pv_entry(pmap_t locked_pmap, int try);
269 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
270 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
271 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva);
272 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
273 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
275 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
276 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
279 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
281 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags);
282 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m);
283 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
284 static void pmap_pte_release(pt_entry_t *pte);
285 static int pmap_unuse_pt(pmap_t, vm_offset_t);
286 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
288 static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
291 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
292 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
295 * Move the kernel virtual free pointer to the next
296 * 4MB. This is used to help improve performance
297 * by using a large (4MB) page for much of the kernel
298 * (.text, .data, .bss)
301 pmap_kmem_choose(vm_offset_t addr)
303 vm_offset_t newaddr = addr;
306 if (cpu_feature & CPUID_PSE)
307 newaddr = (addr + PDRMASK) & ~PDRMASK;
313 * Bootstrap the system enough to run with virtual memory.
315 * On the i386 this is called after mapping has already been enabled
316 * and just syncs the pmap module with what has already been done.
317 * [We can't call it easily with mapping off since the kernel is not
318 * mapped with PA == VA, hence we would have to relocate every address
319 * from the linked base (virtual) address "KERNBASE" to the actual
320 * (physical) address starting relative to 0]
323 pmap_bootstrap(vm_paddr_t firstaddr, vm_paddr_t loadaddr)
326 pt_entry_t *pte, *unused;
327 struct sysmaps *sysmaps;
331 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
332 * large. It should instead be correctly calculated in locore.s and
333 * not based on 'first' (which is a physical address, not a virtual
334 * address, for the start of unused physical memory). The kernel
335 * page tables are NOT double mapped and thus should not be included
336 * in this calculation.
338 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
339 virtual_avail = pmap_kmem_choose(virtual_avail);
341 virtual_end = VM_MAX_KERNEL_ADDRESS;
344 * Initialize the kernel pmap (which is statically allocated).
346 PMAP_LOCK_INIT(kernel_pmap);
347 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
349 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
351 kernel_pmap->pm_active = -1; /* don't allow deactivation */
352 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
353 LIST_INIT(&allpmaps);
354 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
355 mtx_lock_spin(&allpmaps_lock);
356 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
357 mtx_unlock_spin(&allpmaps_lock);
361 * Reserve some special page table entries/VA space for temporary
364 #define SYSMAP(c, p, v, n) \
365 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
371 * CMAP1/CMAP2 are used for zeroing and copying pages.
372 * CMAP3 is used for the idle process page zeroing.
374 for (i = 0; i < MAXCPU; i++) {
375 sysmaps = &sysmaps_pcpu[i];
376 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
377 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
378 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
380 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
381 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
387 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
390 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
392 SYSMAP(caddr_t, unused, ptvmmap, 1)
395 * msgbufp is used to map the system message buffer.
397 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(MSGBUF_SIZE)))
400 * ptemap is used for pmap_pte_quick
402 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1);
403 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1);
405 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
412 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
413 * an early stadium, we cannot yet neatly map video memory ... :-(
414 * Better fixes are very welcome! */
415 if (!arch_i386_is_xbox)
417 for (i = 0; i < NKPT; i++)
420 /* Initialize the PAT MSR if present. */
423 /* Turn on PG_G on kernel page(s) */
435 /* Bail if this CPU doesn't implement PAT. */
436 if (!(cpu_feature & CPUID_PAT))
441 * Leave the indices 0-3 at the default of WB, WT, UC, and UC-.
442 * Program 4 and 5 as WP and WC.
443 * Leave 6 and 7 as UC and UC-.
445 pat_msr = rdmsr(MSR_PAT);
446 pat_msr &= ~(PAT_MASK(4) | PAT_MASK(5));
447 pat_msr |= PAT_VALUE(4, PAT_WRITE_PROTECTED) |
448 PAT_VALUE(5, PAT_WRITE_COMBINING);
451 * Due to some Intel errata, we can only safely use the lower 4
452 * PAT entries. Thus, just replace PAT Index 2 with WC instead
455 * Intel Pentium III Processor Specification Update
456 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
459 * Intel Pentium IV Processor Specification Update
460 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
462 pat_msr = rdmsr(MSR_PAT);
463 pat_msr &= ~PAT_MASK(2);
464 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
466 wrmsr(MSR_PAT, pat_msr);
470 * Set PG_G on kernel pages. Only the BSP calls this when SMP is turned on.
477 vm_offset_t va, endva;
484 endva = KERNBASE + KERNend;
487 va = KERNBASE + KERNLOAD;
489 pdir = kernel_pmap->pm_pdir[KPTDI+i];
491 kernel_pmap->pm_pdir[KPTDI+i] = PTD[KPTDI+i] = pdir;
492 invltlb(); /* Play it safe, invltlb() every time */
497 va = (vm_offset_t)btext;
502 invltlb(); /* Play it safe, invltlb() every time */
509 * Initialize a vm_page's machine-dependent fields.
512 pmap_page_init(vm_page_t m)
515 TAILQ_INIT(&m->md.pv_list);
516 m->md.pv_list_count = 0;
521 static MALLOC_DEFINE(M_PMAPPDPT, "pmap", "pmap pdpt");
524 pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
526 *flags = UMA_SLAB_PRIV;
527 return (contigmalloc(PAGE_SIZE, M_PMAPPDPT, 0, 0x0ULL, 0xffffffffULL,
533 * ABuse the pte nodes for unmapped kva to thread a kva freelist through.
535 * - Must deal with pages in order to ensure that none of the PG_* bits
536 * are ever set, PG_V in particular.
537 * - Assumes we can write to ptes without pte_store() atomic ops, even
538 * on PAE systems. This should be ok.
539 * - Assumes nothing will ever test these addresses for 0 to indicate
540 * no mapping instead of correctly checking PG_V.
541 * - Assumes a vm_offset_t will fit in a pte (true for i386).
542 * Because PG_V is never set, there can be no mappings to invalidate.
545 pmap_ptelist_alloc(vm_offset_t *head)
552 return (va); /* Out of memory */
556 panic("pmap_ptelist_alloc: va with PG_V set!");
562 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
567 panic("pmap_ptelist_free: freeing va with PG_V set!");
569 *pte = *head; /* virtual! PG_V is 0 though */
574 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
580 for (i = npages - 1; i >= 0; i--) {
581 va = (vm_offset_t)base + i * PAGE_SIZE;
582 pmap_ptelist_free(head, va);
588 * Initialize the pmap module.
589 * Called by vm_init, to initialize any structures that the pmap
590 * system needs to map virtual memory.
597 * Initialize the address space (zone) for the pv entries. Set a
598 * high water mark so that the system can recover from excessive
599 * numbers of pv entries.
601 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
602 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
603 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
604 pv_entry_max = roundup(pv_entry_max, _NPCPV);
605 pv_entry_high_water = 9 * (pv_entry_max / 10);
607 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
608 pv_chunkbase = (struct pv_chunk *)kmem_alloc_nofault(kernel_map,
609 PAGE_SIZE * pv_maxchunks);
610 if (pv_chunkbase == NULL)
611 panic("pmap_init: not enough kvm for pv chunks");
612 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
614 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
615 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
616 UMA_ZONE_VM | UMA_ZONE_NOFREE);
617 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
622 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
623 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
624 "Max number of PV entries");
625 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
626 "Page share factor per proc");
628 /***************************************************
629 * Low level helper routines.....
630 ***************************************************/
633 * Determine the appropriate bits to set in a PTE or PDE for a specified
637 pmap_cache_bits(int mode, boolean_t is_pde)
639 int pat_flag, pat_index, cache_bits;
641 /* The PAT bit is different for PTE's and PDE's. */
642 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
644 /* If we don't support PAT, map extended modes to older ones. */
645 if (!(cpu_feature & CPUID_PAT)) {
647 case PAT_UNCACHEABLE:
648 case PAT_WRITE_THROUGH:
652 case PAT_WRITE_COMBINING:
653 case PAT_WRITE_PROTECTED:
654 mode = PAT_UNCACHEABLE;
659 /* Map the caching mode to a PAT index. */
662 case PAT_UNCACHEABLE:
665 case PAT_WRITE_THROUGH:
674 case PAT_WRITE_COMBINING:
677 case PAT_WRITE_PROTECTED:
682 case PAT_UNCACHEABLE:
683 case PAT_WRITE_PROTECTED:
686 case PAT_WRITE_THROUGH:
692 case PAT_WRITE_COMBINING:
697 panic("Unknown caching mode %d\n", mode);
700 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
703 cache_bits |= pat_flag;
705 cache_bits |= PG_NC_PCD;
707 cache_bits |= PG_NC_PWT;
712 * For SMP, these functions have to use the IPI mechanism for coherence.
715 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
721 if (!(read_eflags() & PSL_I))
722 panic("%s: interrupts disabled", __func__);
723 mtx_lock_spin(&smp_ipi_mtx);
727 * We need to disable interrupt preemption but MUST NOT have
728 * interrupts disabled here.
729 * XXX we may need to hold schedlock to get a coherent pm_active
730 * XXX critical sections disable interrupts again
732 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
736 cpumask = PCPU_GET(cpumask);
737 other_cpus = PCPU_GET(other_cpus);
738 if (pmap->pm_active & cpumask)
740 if (pmap->pm_active & other_cpus)
741 smp_masked_invlpg(pmap->pm_active & other_cpus, va);
744 mtx_unlock_spin(&smp_ipi_mtx);
750 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
757 if (!(read_eflags() & PSL_I))
758 panic("%s: interrupts disabled", __func__);
759 mtx_lock_spin(&smp_ipi_mtx);
763 * We need to disable interrupt preemption but MUST NOT have
764 * interrupts disabled here.
765 * XXX we may need to hold schedlock to get a coherent pm_active
766 * XXX critical sections disable interrupts again
768 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
769 for (addr = sva; addr < eva; addr += PAGE_SIZE)
771 smp_invlpg_range(sva, eva);
773 cpumask = PCPU_GET(cpumask);
774 other_cpus = PCPU_GET(other_cpus);
775 if (pmap->pm_active & cpumask)
776 for (addr = sva; addr < eva; addr += PAGE_SIZE)
778 if (pmap->pm_active & other_cpus)
779 smp_masked_invlpg_range(pmap->pm_active & other_cpus,
783 mtx_unlock_spin(&smp_ipi_mtx);
789 pmap_invalidate_all(pmap_t pmap)
795 if (!(read_eflags() & PSL_I))
796 panic("%s: interrupts disabled", __func__);
797 mtx_lock_spin(&smp_ipi_mtx);
801 * We need to disable interrupt preemption but MUST NOT have
802 * interrupts disabled here.
803 * XXX we may need to hold schedlock to get a coherent pm_active
804 * XXX critical sections disable interrupts again
806 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
810 cpumask = PCPU_GET(cpumask);
811 other_cpus = PCPU_GET(other_cpus);
812 if (pmap->pm_active & cpumask)
814 if (pmap->pm_active & other_cpus)
815 smp_masked_invltlb(pmap->pm_active & other_cpus);
818 mtx_unlock_spin(&smp_ipi_mtx);
824 pmap_invalidate_cache(void)
828 if (!(read_eflags() & PSL_I))
829 panic("%s: interrupts disabled", __func__);
830 mtx_lock_spin(&smp_ipi_mtx);
834 * We need to disable interrupt preemption but MUST NOT have
835 * interrupts disabled here.
836 * XXX we may need to hold schedlock to get a coherent pm_active
837 * XXX critical sections disable interrupts again
842 mtx_unlock_spin(&smp_ipi_mtx);
848 * Normal, non-SMP, 486+ invalidation functions.
849 * We inline these within pmap.c for speed.
852 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
855 if (pmap == kernel_pmap || pmap->pm_active)
860 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
864 if (pmap == kernel_pmap || pmap->pm_active)
865 for (addr = sva; addr < eva; addr += PAGE_SIZE)
870 pmap_invalidate_all(pmap_t pmap)
873 if (pmap == kernel_pmap || pmap->pm_active)
878 pmap_invalidate_cache(void)
886 * Are we current address space or kernel? N.B. We return FALSE when
887 * a pmap's page table is in use because a kernel thread is borrowing
888 * it. The borrowed page table can change spontaneously, making any
889 * dependence on its continued use subject to a race condition.
892 pmap_is_current(pmap_t pmap)
895 return (pmap == kernel_pmap ||
896 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
897 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
901 * If the given pmap is not the current or kernel pmap, the returned pte must
902 * be released by passing it to pmap_pte_release().
905 pmap_pte(pmap_t pmap, vm_offset_t va)
910 pde = pmap_pde(pmap, va);
914 /* are we current address space or kernel? */
915 if (pmap_is_current(pmap))
917 mtx_lock(&PMAP2mutex);
918 newpf = *pde & PG_FRAME;
919 if ((*PMAP2 & PG_FRAME) != newpf) {
920 *PMAP2 = newpf | PG_RW | PG_V | PG_A | PG_M;
921 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR2);
923 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
929 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
933 pmap_pte_release(pt_entry_t *pte)
936 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2)
937 mtx_unlock(&PMAP2mutex);
941 invlcaddr(void *caddr)
944 invlpg((u_int)caddr);
948 * Super fast pmap_pte routine best used when scanning
949 * the pv lists. This eliminates many coarse-grained
950 * invltlb calls. Note that many of the pv list
951 * scans are across different pmaps. It is very wasteful
952 * to do an entire invltlb for checking a single mapping.
954 * If the given pmap is not the current pmap, vm_page_queue_mtx
955 * must be held and curthread pinned to a CPU.
958 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
963 pde = pmap_pde(pmap, va);
967 /* are we current address space or kernel? */
968 if (pmap_is_current(pmap))
970 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
971 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
972 newpf = *pde & PG_FRAME;
973 if ((*PMAP1 & PG_FRAME) != newpf) {
974 *PMAP1 = newpf | PG_RW | PG_V | PG_A | PG_M;
976 PMAP1cpu = PCPU_GET(cpuid);
982 if (PMAP1cpu != PCPU_GET(cpuid)) {
983 PMAP1cpu = PCPU_GET(cpuid);
989 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
995 * Routine: pmap_extract
997 * Extract the physical page address associated
998 * with the given map/virtual_address pair.
1001 pmap_extract(pmap_t pmap, vm_offset_t va)
1009 pde = pmap->pm_pdir[va >> PDRSHIFT];
1011 if ((pde & PG_PS) != 0) {
1012 rtval = (pde & ~PDRMASK) | (va & PDRMASK);
1016 pte = pmap_pte(pmap, va);
1017 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1018 pmap_pte_release(pte);
1025 * Routine: pmap_extract_and_hold
1027 * Atomically extract and hold the physical page
1028 * with the given pmap and virtual address pair
1029 * if that mapping permits the given protection.
1032 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1039 vm_page_lock_queues();
1041 pde = *pmap_pde(pmap, va);
1044 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1045 m = PHYS_TO_VM_PAGE((pde & ~PDRMASK) |
1051 pte = *pmap_pte_quick(pmap, va);
1053 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1054 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1060 vm_page_unlock_queues();
1065 /***************************************************
1066 * Low level mapping routines.....
1067 ***************************************************/
1070 * Add a wired page to the kva.
1071 * Note: not SMP coherent.
1074 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1079 pte_store(pte, pa | PG_RW | PG_V | pgeflag);
1083 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1088 pte_store(pte, pa | PG_RW | PG_V | pgeflag | pmap_cache_bits(mode, 0));
1092 * Remove a page from the kernel pagetables.
1093 * Note: not SMP coherent.
1096 pmap_kremove(vm_offset_t va)
1105 * Used to map a range of physical addresses into kernel
1106 * virtual address space.
1108 * The value passed in '*virt' is a suggested virtual address for
1109 * the mapping. Architectures which can support a direct-mapped
1110 * physical to virtual region can return the appropriate address
1111 * within that region, leaving '*virt' unchanged. Other
1112 * architectures should map the pages starting at '*virt' and
1113 * update '*virt' with the first usable address after the mapped
1117 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1119 vm_offset_t va, sva;
1122 while (start < end) {
1123 pmap_kenter(va, start);
1127 pmap_invalidate_range(kernel_pmap, sva, va);
1134 * Add a list of wired pages to the kva
1135 * this routine is only used for temporary
1136 * kernel mappings that do not need to have
1137 * page modification or references recorded.
1138 * Note that old mappings are simply written
1139 * over. The page *must* be wired.
1140 * Note: SMP coherent. Uses a ranged shootdown IPI.
1143 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1145 pt_entry_t *endpte, oldpte, *pte;
1149 endpte = pte + count;
1150 while (pte < endpte) {
1152 pte_store(pte, VM_PAGE_TO_PHYS(*ma) | pgeflag | PG_RW | PG_V);
1156 if ((oldpte & PG_V) != 0)
1157 pmap_invalidate_range(kernel_pmap, sva, sva + count *
1162 * This routine tears out page mappings from the
1163 * kernel -- it is meant only for temporary mappings.
1164 * Note: SMP coherent. Uses a ranged shootdown IPI.
1167 pmap_qremove(vm_offset_t sva, int count)
1172 while (count-- > 0) {
1176 pmap_invalidate_range(kernel_pmap, sva, va);
1179 /***************************************************
1180 * Page table page management routines.....
1181 ***************************************************/
1184 * This routine unholds page table pages, and if the hold count
1185 * drops to zero, then it decrements the wire count.
1187 static PMAP_INLINE int
1188 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1192 if (m->wire_count == 0)
1193 return _pmap_unwire_pte_hold(pmap, m);
1199 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1204 * unmap the page table page
1206 pmap->pm_pdir[m->pindex] = 0;
1207 --pmap->pm_stats.resident_count;
1210 * Do an invltlb to make the invalidated mapping
1211 * take effect immediately.
1213 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1214 pmap_invalidate_page(pmap, pteva);
1216 vm_page_free_zero(m);
1217 atomic_subtract_int(&cnt.v_wire_count, 1);
1222 * After removing a page table entry, this routine is used to
1223 * conditionally free the page, and manage the hold/wire counts.
1226 pmap_unuse_pt(pmap_t pmap, vm_offset_t va)
1231 if (va >= VM_MAXUSER_ADDRESS)
1233 ptepde = *pmap_pde(pmap, va);
1234 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1235 return pmap_unwire_pte_hold(pmap, mpte);
1239 pmap_pinit0(pmap_t pmap)
1242 PMAP_LOCK_INIT(pmap);
1243 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1245 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1247 pmap->pm_active = 0;
1248 PCPU_SET(curpmap, pmap);
1249 TAILQ_INIT(&pmap->pm_pvchunk);
1250 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1251 mtx_lock_spin(&allpmaps_lock);
1252 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1253 mtx_unlock_spin(&allpmaps_lock);
1257 * Initialize a preallocated and zeroed pmap structure,
1258 * such as one in a vmspace structure.
1261 pmap_pinit(pmap_t pmap)
1263 vm_page_t m, ptdpg[NPGPTD];
1268 PMAP_LOCK_INIT(pmap);
1271 * No need to allocate page table space yet but we do need a valid
1272 * page directory table.
1274 if (pmap->pm_pdir == NULL) {
1275 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map,
1278 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
1279 KASSERT(((vm_offset_t)pmap->pm_pdpt &
1280 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
1281 ("pmap_pinit: pdpt misaligned"));
1282 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
1283 ("pmap_pinit: pdpt above 4g"));
1288 * allocate the page directory page(s)
1290 for (i = 0; i < NPGPTD;) {
1291 m = vm_page_alloc(NULL, color++,
1292 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1301 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1303 for (i = 0; i < NPGPTD; i++) {
1304 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1305 bzero(pmap->pm_pdir + (i * NPDEPG), PAGE_SIZE);
1308 mtx_lock_spin(&allpmaps_lock);
1309 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1310 mtx_unlock_spin(&allpmaps_lock);
1311 /* Wire in kernel global address entries. */
1312 /* XXX copies current process, does not fill in MPPTDI */
1313 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1315 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1318 /* install self-referential address mapping entry(s) */
1319 for (i = 0; i < NPGPTD; i++) {
1320 pa = VM_PAGE_TO_PHYS(ptdpg[i]);
1321 pmap->pm_pdir[PTDPTDI + i] = pa | PG_V | PG_RW | PG_A | PG_M;
1323 pmap->pm_pdpt[i] = pa | PG_V;
1327 pmap->pm_active = 0;
1328 TAILQ_INIT(&pmap->pm_pvchunk);
1329 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1333 * this routine is called if the page table page is not
1337 _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags)
1342 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1343 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1344 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1347 * Allocate a page table page.
1349 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1350 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1351 if (flags & M_WAITOK) {
1353 vm_page_unlock_queues();
1355 vm_page_lock_queues();
1360 * Indicate the need to retry. While waiting, the page table
1361 * page may have been allocated.
1365 if ((m->flags & PG_ZERO) == 0)
1369 * Map the pagetable page into the process address space, if
1370 * it isn't already there.
1373 pmap->pm_stats.resident_count++;
1375 ptepa = VM_PAGE_TO_PHYS(m);
1376 pmap->pm_pdir[ptepindex] =
1377 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1383 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1389 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1390 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1391 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1394 * Calculate pagetable page index
1396 ptepindex = va >> PDRSHIFT;
1399 * Get the page directory entry
1401 ptepa = pmap->pm_pdir[ptepindex];
1404 * This supports switching from a 4MB page to a
1407 if (ptepa & PG_PS) {
1408 pmap->pm_pdir[ptepindex] = 0;
1410 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1411 pmap_invalidate_all(kernel_pmap);
1415 * If the page table page is mapped, we just increment the
1416 * hold count, and activate it.
1419 m = PHYS_TO_VM_PAGE(ptepa);
1423 * Here if the pte page isn't mapped, or if it has
1426 m = _pmap_allocpte(pmap, ptepindex, flags);
1427 if (m == NULL && (flags & M_WAITOK))
1434 /***************************************************
1435 * Pmap allocation/deallocation routines.
1436 ***************************************************/
1440 * Deal with a SMP shootdown of other users of the pmap that we are
1441 * trying to dispose of. This can be a bit hairy.
1443 static u_int *lazymask;
1444 static u_int lazyptd;
1445 static volatile u_int lazywait;
1447 void pmap_lazyfix_action(void);
1450 pmap_lazyfix_action(void)
1452 u_int mymask = PCPU_GET(cpumask);
1455 *ipi_lazypmap_counts[PCPU_GET(cpuid)]++;
1457 if (rcr3() == lazyptd)
1458 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1459 atomic_clear_int(lazymask, mymask);
1460 atomic_store_rel_int(&lazywait, 1);
1464 pmap_lazyfix_self(u_int mymask)
1467 if (rcr3() == lazyptd)
1468 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1469 atomic_clear_int(lazymask, mymask);
1474 pmap_lazyfix(pmap_t pmap)
1480 while ((mask = pmap->pm_active) != 0) {
1482 mask = mask & -mask; /* Find least significant set bit */
1483 mtx_lock_spin(&smp_ipi_mtx);
1485 lazyptd = vtophys(pmap->pm_pdpt);
1487 lazyptd = vtophys(pmap->pm_pdir);
1489 mymask = PCPU_GET(cpumask);
1490 if (mask == mymask) {
1491 lazymask = &pmap->pm_active;
1492 pmap_lazyfix_self(mymask);
1494 atomic_store_rel_int((u_int *)&lazymask,
1495 (u_int)&pmap->pm_active);
1496 atomic_store_rel_int(&lazywait, 0);
1497 ipi_selected(mask, IPI_LAZYPMAP);
1498 while (lazywait == 0) {
1504 mtx_unlock_spin(&smp_ipi_mtx);
1506 printf("pmap_lazyfix: spun for 50000000\n");
1513 * Cleaning up on uniprocessor is easy. For various reasons, we're
1514 * unlikely to have to even execute this code, including the fact
1515 * that the cleanup is deferred until the parent does a wait(2), which
1516 * means that another userland process has run.
1519 pmap_lazyfix(pmap_t pmap)
1523 cr3 = vtophys(pmap->pm_pdir);
1524 if (cr3 == rcr3()) {
1525 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1526 pmap->pm_active &= ~(PCPU_GET(cpumask));
1532 * Release any resources held by the given physical map.
1533 * Called when a pmap initialized by pmap_pinit is being released.
1534 * Should only be called if the map contains no valid mappings.
1537 pmap_release(pmap_t pmap)
1539 vm_page_t m, ptdpg[NPGPTD];
1542 KASSERT(pmap->pm_stats.resident_count == 0,
1543 ("pmap_release: pmap resident count %ld != 0",
1544 pmap->pm_stats.resident_count));
1547 mtx_lock_spin(&allpmaps_lock);
1548 LIST_REMOVE(pmap, pm_list);
1549 mtx_unlock_spin(&allpmaps_lock);
1551 for (i = 0; i < NPGPTD; i++)
1552 ptdpg[i] = PHYS_TO_VM_PAGE(pmap->pm_pdir[PTDPTDI + i]);
1554 bzero(pmap->pm_pdir + PTDPTDI, (nkpt + NPGPTD) *
1555 sizeof(*pmap->pm_pdir));
1557 pmap->pm_pdir[MPPTDI] = 0;
1560 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1562 vm_page_lock_queues();
1563 for (i = 0; i < NPGPTD; i++) {
1566 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME),
1567 ("pmap_release: got wrong ptd page"));
1570 atomic_subtract_int(&cnt.v_wire_count, 1);
1571 vm_page_free_zero(m);
1573 vm_page_unlock_queues();
1574 PMAP_LOCK_DESTROY(pmap);
1578 kvm_size(SYSCTL_HANDLER_ARGS)
1580 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1582 return sysctl_handle_long(oidp, &ksize, 0, req);
1584 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1585 0, 0, kvm_size, "IU", "Size of KVM");
1588 kvm_free(SYSCTL_HANDLER_ARGS)
1590 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1592 return sysctl_handle_long(oidp, &kfree, 0, req);
1594 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1595 0, 0, kvm_free, "IU", "Amount of KVM free");
1598 * grow the number of kernel page table entries, if needed
1601 pmap_growkernel(vm_offset_t addr)
1604 vm_paddr_t ptppaddr;
1609 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1610 if (kernel_vm_end == 0) {
1611 kernel_vm_end = KERNBASE;
1613 while (pdir_pde(PTD, kernel_vm_end)) {
1614 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1616 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1617 kernel_vm_end = kernel_map->max_offset;
1622 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1623 if (addr - 1 >= kernel_map->max_offset)
1624 addr = kernel_map->max_offset;
1625 while (kernel_vm_end < addr) {
1626 if (pdir_pde(PTD, kernel_vm_end)) {
1627 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1628 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1629 kernel_vm_end = kernel_map->max_offset;
1636 * This index is bogus, but out of the way
1638 nkpg = vm_page_alloc(NULL, nkpt,
1639 VM_ALLOC_NOOBJ | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
1641 panic("pmap_growkernel: no memory to grow kernel");
1645 pmap_zero_page(nkpg);
1646 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1647 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1648 pdir_pde(PTD, kernel_vm_end) = newpdir;
1650 mtx_lock_spin(&allpmaps_lock);
1651 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1652 pde = pmap_pde(pmap, kernel_vm_end);
1653 pde_store(pde, newpdir);
1655 mtx_unlock_spin(&allpmaps_lock);
1656 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1657 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1658 kernel_vm_end = kernel_map->max_offset;
1665 /***************************************************
1666 * page management routines.
1667 ***************************************************/
1669 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1670 CTASSERT(_NPCM == 11);
1672 static __inline struct pv_chunk *
1673 pv_to_chunk(pv_entry_t pv)
1676 return (struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK);
1679 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1681 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
1682 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
1684 static uint32_t pc_freemask[11] = {
1685 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1686 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1687 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1688 PC_FREE0_9, PC_FREE10
1691 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1692 "Current number of pv entries");
1695 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1697 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1698 "Current number of pv entry chunks");
1699 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1700 "Current number of pv entry chunks allocated");
1701 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1702 "Current number of pv entry chunks frees");
1703 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1704 "Number of times tried to get a chunk page but failed.");
1706 static long pv_entry_frees, pv_entry_allocs;
1707 static int pv_entry_spare;
1709 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1710 "Current number of pv entry frees");
1711 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1712 "Current number of pv entry allocs");
1713 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1714 "Current number of spare pv entries");
1716 static int pmap_collect_inactive, pmap_collect_active;
1718 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_inactive, CTLFLAG_RD, &pmap_collect_inactive, 0,
1719 "Current number times pmap_collect called on inactive queue");
1720 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_active, CTLFLAG_RD, &pmap_collect_active, 0,
1721 "Current number times pmap_collect called on active queue");
1725 * We are in a serious low memory condition. Resort to
1726 * drastic measures to free some pages so we can allocate
1727 * another pv entry chunk. This is normally called to
1728 * unmap inactive pages, and if necessary, active pages.
1731 pmap_collect(pmap_t locked_pmap, struct vpgqueues *vpq)
1734 pt_entry_t *pte, tpte;
1735 pv_entry_t next_pv, pv;
1740 TAILQ_FOREACH(m, &vpq->pl, pageq) {
1741 if (m->hold_count || m->busy)
1743 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
1746 /* Avoid deadlock and lock recursion. */
1747 if (pmap > locked_pmap)
1749 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
1751 pmap->pm_stats.resident_count--;
1752 pte = pmap_pte_quick(pmap, va);
1753 tpte = pte_load_clear(pte);
1754 KASSERT((tpte & PG_W) == 0,
1755 ("pmap_collect: wired pte %#jx", (uintmax_t)tpte));
1757 vm_page_flag_set(m, PG_REFERENCED);
1759 KASSERT((tpte & PG_RW),
1760 ("pmap_collect: modified page not writable: va: %#x, pte: %#jx",
1761 va, (uintmax_t)tpte));
1764 pmap_invalidate_page(pmap, va);
1765 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1766 if (TAILQ_EMPTY(&m->md.pv_list))
1767 vm_page_flag_clear(m, PG_WRITEABLE);
1768 m->md.pv_list_count--;
1769 pmap_unuse_pt(pmap, va);
1770 free_pv_entry(pmap, pv);
1771 if (pmap != locked_pmap)
1780 * free the pv_entry back to the free list
1783 free_pv_entry(pmap_t pmap, pv_entry_t pv)
1786 struct pv_chunk *pc;
1787 int idx, field, bit;
1789 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1790 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1791 PV_STAT(pv_entry_frees++);
1792 PV_STAT(pv_entry_spare++);
1794 pc = pv_to_chunk(pv);
1795 idx = pv - &pc->pc_pventry[0];
1798 pc->pc_map[field] |= 1ul << bit;
1799 /* move to head of list */
1800 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1801 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
1802 for (idx = 0; idx < _NPCM; idx++)
1803 if (pc->pc_map[idx] != pc_freemask[idx])
1805 PV_STAT(pv_entry_spare -= _NPCPV);
1806 PV_STAT(pc_chunk_count--);
1807 PV_STAT(pc_chunk_frees++);
1808 /* entire chunk is free, return it */
1809 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1810 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
1811 pmap_qremove((vm_offset_t)pc, 1);
1812 vm_page_unwire(m, 0);
1814 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
1818 * get a new pv_entry, allocating a block from the system
1822 get_pv_entry(pmap_t pmap, int try)
1824 static const struct timeval printinterval = { 60, 0 };
1825 static struct timeval lastprint;
1826 static vm_pindex_t colour;
1827 int bit, field, page_req;
1829 struct pv_chunk *pc;
1832 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1833 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1834 PV_STAT(pv_entry_allocs++);
1836 if (pv_entry_count > pv_entry_high_water)
1837 pagedaemon_wakeup();
1838 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
1840 for (field = 0; field < _NPCM; field++) {
1841 if (pc->pc_map[field]) {
1842 bit = bsfl(pc->pc_map[field]);
1846 if (field < _NPCM) {
1847 pv = &pc->pc_pventry[field * 32 + bit];
1848 pc->pc_map[field] &= ~(1ul << bit);
1849 /* If this was the last item, move it to tail */
1850 for (field = 0; field < _NPCM; field++)
1851 if (pc->pc_map[field] != 0) {
1852 PV_STAT(pv_entry_spare--);
1853 return (pv); /* not full, return */
1855 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1856 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
1857 PV_STAT(pv_entry_spare--);
1861 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
1862 page_req = try ? VM_ALLOC_NORMAL : VM_ALLOC_SYSTEM;
1863 m = vm_page_alloc(NULL, colour, page_req |
1864 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
1865 if (m == NULL || pc == NULL) {
1868 PV_STAT(pc_chunk_tryfail++);
1870 vm_page_lock_queues();
1871 vm_page_unwire(m, 0);
1873 vm_page_unlock_queues();
1876 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
1880 * Reclaim pv entries: At first, destroy mappings to
1881 * inactive pages. After that, if a pv chunk entry
1882 * is still needed, destroy mappings to active pages.
1884 if (ratecheck(&lastprint, &printinterval))
1885 printf("Approaching the limit on PV entries, "
1886 "consider increasing tunables "
1887 "vm.pmap.shpgperproc or "
1888 "vm.pmap.pv_entry_max\n");
1889 PV_STAT(pmap_collect_inactive++);
1890 pmap_collect(pmap, &vm_page_queues[PQ_INACTIVE]);
1892 m = vm_page_alloc(NULL, colour, VM_ALLOC_SYSTEM |
1893 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
1895 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
1896 if (m == NULL || pc == NULL) {
1897 PV_STAT(pmap_collect_active++);
1898 pmap_collect(pmap, &vm_page_queues[PQ_ACTIVE]);
1900 m = vm_page_alloc(NULL, colour,
1901 VM_ALLOC_SYSTEM | VM_ALLOC_NOOBJ |
1904 pc = (struct pv_chunk *)
1905 pmap_ptelist_alloc(&pv_vafree);
1906 if (m == NULL || pc == NULL)
1907 panic("get_pv_entry: increase vm.pmap.shpgperproc");
1910 PV_STAT(pc_chunk_count++);
1911 PV_STAT(pc_chunk_allocs++);
1913 pmap_qenter((vm_offset_t)pc, &m, 1);
1915 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
1916 for (field = 1; field < _NPCM; field++)
1917 pc->pc_map[field] = pc_freemask[field];
1918 pv = &pc->pc_pventry[0];
1919 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
1920 PV_STAT(pv_entry_spare += _NPCPV - 1);
1925 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1929 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1930 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1931 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1932 if (pmap == PV_PMAP(pv) && va == pv->pv_va)
1935 KASSERT(pv != NULL, ("pmap_remove_entry: pv not found"));
1936 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1937 m->md.pv_list_count--;
1938 if (TAILQ_EMPTY(&m->md.pv_list))
1939 vm_page_flag_clear(m, PG_WRITEABLE);
1940 free_pv_entry(pmap, pv);
1944 * Create a pv entry for page at pa for
1948 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
1952 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1953 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1954 pv = get_pv_entry(pmap, FALSE);
1956 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1957 m->md.pv_list_count++;
1961 * Conditionally create a pv entry.
1964 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
1968 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1969 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1970 if (pv_entry_count < pv_entry_high_water &&
1971 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
1973 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1974 m->md.pv_list_count++;
1981 * pmap_remove_pte: do the things to unmap a page in a process
1984 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va)
1989 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1990 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1991 oldpte = pte_load_clear(ptq);
1993 pmap->pm_stats.wired_count -= 1;
1995 * Machines that don't support invlpg, also don't support
1999 pmap_invalidate_page(kernel_pmap, va);
2000 pmap->pm_stats.resident_count -= 1;
2001 if (oldpte & PG_MANAGED) {
2002 m = PHYS_TO_VM_PAGE(oldpte);
2003 if (oldpte & PG_M) {
2004 KASSERT((oldpte & PG_RW),
2005 ("pmap_remove_pte: modified page not writable: va: %#x, pte: %#jx",
2006 va, (uintmax_t)oldpte));
2010 vm_page_flag_set(m, PG_REFERENCED);
2011 pmap_remove_entry(pmap, m, va);
2013 return (pmap_unuse_pt(pmap, va));
2017 * Remove a single page from a process address space
2020 pmap_remove_page(pmap_t pmap, vm_offset_t va)
2024 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2025 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
2026 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2027 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
2029 pmap_remove_pte(pmap, pte, va);
2030 pmap_invalidate_page(pmap, va);
2034 * Remove the given range of addresses from the specified map.
2036 * It is assumed that the start and end are properly
2037 * rounded to the page size.
2040 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2048 * Perform an unsynchronized read. This is, however, safe.
2050 if (pmap->pm_stats.resident_count == 0)
2055 vm_page_lock_queues();
2060 * special handling of removing one page. a very
2061 * common operation and easy to short circuit some
2064 if ((sva + PAGE_SIZE == eva) &&
2065 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
2066 pmap_remove_page(pmap, sva);
2070 for (; sva < eva; sva = pdnxt) {
2074 * Calculate index for next page table.
2076 pdnxt = (sva + NBPDR) & ~PDRMASK;
2077 if (pmap->pm_stats.resident_count == 0)
2080 pdirindex = sva >> PDRSHIFT;
2081 ptpaddr = pmap->pm_pdir[pdirindex];
2084 * Weed out invalid mappings. Note: we assume that the page
2085 * directory table is always allocated, and in kernel virtual.
2091 * Check for large page.
2093 if ((ptpaddr & PG_PS) != 0) {
2094 pmap->pm_pdir[pdirindex] = 0;
2095 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2101 * Limit our scan to either the end of the va represented
2102 * by the current page table page, or to the end of the
2103 * range being removed.
2108 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2114 * The TLB entry for a PG_G mapping is invalidated
2115 * by pmap_remove_pte().
2117 if ((*pte & PG_G) == 0)
2119 if (pmap_remove_pte(pmap, pte, sva))
2125 vm_page_unlock_queues();
2127 pmap_invalidate_all(pmap);
2132 * Routine: pmap_remove_all
2134 * Removes this physical page from
2135 * all physical maps in which it resides.
2136 * Reflects back modify bits to the pager.
2139 * Original versions of this routine were very
2140 * inefficient because they iteratively called
2141 * pmap_remove (slow...)
2145 pmap_remove_all(vm_page_t m)
2149 pt_entry_t *pte, tpte;
2151 #if defined(PMAP_DIAGNOSTIC)
2153 * XXX This makes pmap_remove_all() illegal for non-managed pages!
2155 if (m->flags & PG_FICTITIOUS) {
2156 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%x",
2157 VM_PAGE_TO_PHYS(m));
2160 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2162 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2165 pmap->pm_stats.resident_count--;
2166 pte = pmap_pte_quick(pmap, pv->pv_va);
2167 tpte = pte_load_clear(pte);
2169 pmap->pm_stats.wired_count--;
2171 vm_page_flag_set(m, PG_REFERENCED);
2174 * Update the vm_page_t clean and reference bits.
2177 KASSERT((tpte & PG_RW),
2178 ("pmap_remove_all: modified page not writable: va: %#x, pte: %#jx",
2179 pv->pv_va, (uintmax_t)tpte));
2182 pmap_invalidate_page(pmap, pv->pv_va);
2183 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2184 m->md.pv_list_count--;
2185 pmap_unuse_pt(pmap, pv->pv_va);
2186 free_pv_entry(pmap, pv);
2189 vm_page_flag_clear(m, PG_WRITEABLE);
2194 * Set the physical protection on the
2195 * specified range of this map as requested.
2198 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2205 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2206 pmap_remove(pmap, sva, eva);
2210 if (prot & VM_PROT_WRITE)
2215 vm_page_lock_queues();
2218 for (; sva < eva; sva = pdnxt) {
2219 unsigned obits, pbits, pdirindex;
2221 pdnxt = (sva + NBPDR) & ~PDRMASK;
2223 pdirindex = sva >> PDRSHIFT;
2224 ptpaddr = pmap->pm_pdir[pdirindex];
2227 * Weed out invalid mappings. Note: we assume that the page
2228 * directory table is always allocated, and in kernel virtual.
2234 * Check for large page.
2236 if ((ptpaddr & PG_PS) != 0) {
2237 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
2245 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2251 * Regardless of whether a pte is 32 or 64 bits in
2252 * size, PG_RW, PG_A, and PG_M are among the least
2253 * significant 32 bits.
2255 obits = pbits = *(u_int *)pte;
2256 if (pbits & PG_MANAGED) {
2259 m = PHYS_TO_VM_PAGE(*pte);
2260 vm_page_flag_set(m, PG_REFERENCED);
2263 if ((pbits & PG_M) != 0) {
2265 m = PHYS_TO_VM_PAGE(*pte);
2270 pbits &= ~(PG_RW | PG_M);
2272 if (pbits != obits) {
2273 if (!atomic_cmpset_int((u_int *)pte, obits,
2277 pmap_invalidate_page(pmap, sva);
2284 vm_page_unlock_queues();
2286 pmap_invalidate_all(pmap);
2291 * Insert the given physical page (p) at
2292 * the specified virtual address (v) in the
2293 * target physical map with the protection requested.
2295 * If specified, the page will be wired down, meaning
2296 * that the related pte can not be reclaimed.
2298 * NB: This is the only routine which MAY NOT lazy-evaluate
2299 * or lose information. That is, this routine must actually
2300 * insert this page into the given map NOW.
2303 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2310 pt_entry_t origpte, newpte;
2315 #ifdef PMAP_DIAGNOSTIC
2316 if (va > VM_MAX_KERNEL_ADDRESS)
2317 panic("pmap_enter: toobig");
2318 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2319 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
2324 vm_page_lock_queues();
2329 * In the case that a page table page is not
2330 * resident, we are creating it here.
2332 if (va < VM_MAXUSER_ADDRESS) {
2333 mpte = pmap_allocpte(pmap, va, M_WAITOK);
2335 #if 0 && defined(PMAP_DIAGNOSTIC)
2337 pd_entry_t *pdeaddr = pmap_pde(pmap, va);
2339 if ((origpte & PG_V) == 0) {
2340 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n",
2341 pmap->pm_pdir[PTDPTDI], origpte, va);
2346 pde = pmap_pde(pmap, va);
2347 if ((*pde & PG_PS) != 0)
2348 panic("pmap_enter: attempted pmap_enter on 4MB page");
2349 pte = pmap_pte_quick(pmap, va);
2352 * Page Directory table entry not valid, we need a new PT page
2355 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x\n",
2356 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
2359 pa = VM_PAGE_TO_PHYS(m);
2362 opa = origpte & PG_FRAME;
2365 * Mapping has not changed, must be protection or wiring change.
2367 if (origpte && (opa == pa)) {
2369 * Wiring change, just update stats. We don't worry about
2370 * wiring PT pages as they remain resident as long as there
2371 * are valid mappings in them. Hence, if a user page is wired,
2372 * the PT page will be also.
2374 if (wired && ((origpte & PG_W) == 0))
2375 pmap->pm_stats.wired_count++;
2376 else if (!wired && (origpte & PG_W))
2377 pmap->pm_stats.wired_count--;
2380 * Remove extra pte reference
2386 * We might be turning off write access to the page,
2387 * so we go ahead and sense modify status.
2389 if (origpte & PG_MANAGED) {
2396 * Mapping has changed, invalidate old range and fall through to
2397 * handle validating new mapping.
2401 pmap->pm_stats.wired_count--;
2402 if (origpte & PG_MANAGED) {
2403 om = PHYS_TO_VM_PAGE(opa);
2404 pmap_remove_entry(pmap, om, va);
2408 KASSERT(mpte->wire_count > 0,
2409 ("pmap_enter: missing reference to page table page,"
2413 pmap->pm_stats.resident_count++;
2416 * Enter on the PV list if part of our managed memory.
2418 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
2419 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
2420 ("pmap_enter: managed mapping within the clean submap"));
2421 pmap_insert_entry(pmap, va, m);
2426 * Increment counters
2429 pmap->pm_stats.wired_count++;
2433 * Now validate mapping with desired protection/wiring.
2435 newpte = (pt_entry_t)(pa | PG_V);
2436 if ((prot & VM_PROT_WRITE) != 0) {
2438 vm_page_flag_set(m, PG_WRITEABLE);
2442 if (va < VM_MAXUSER_ADDRESS)
2444 if (pmap == kernel_pmap)
2448 * if the mapping or permission bits are different, we need
2449 * to update the pte.
2451 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2452 if (origpte & PG_V) {
2454 origpte = pte_load_store(pte, newpte | PG_A);
2455 if (origpte & PG_A) {
2456 if (origpte & PG_MANAGED)
2457 vm_page_flag_set(om, PG_REFERENCED);
2458 if (opa != VM_PAGE_TO_PHYS(m))
2461 if (origpte & PG_M) {
2462 KASSERT((origpte & PG_RW),
2463 ("pmap_enter: modified page not writable: va: %#x, pte: %#jx",
2464 va, (uintmax_t)origpte));
2465 if ((origpte & PG_MANAGED) != 0)
2467 if ((prot & VM_PROT_WRITE) == 0)
2471 pmap_invalidate_page(pmap, va);
2473 pte_store(pte, newpte | PG_A);
2476 vm_page_unlock_queues();
2481 * Maps a sequence of resident pages belonging to the same object.
2482 * The sequence begins with the given page m_start. This page is
2483 * mapped at the given virtual address start. Each subsequent page is
2484 * mapped at a virtual address that is offset from start by the same
2485 * amount as the page is offset from m_start within the object. The
2486 * last page in the sequence is the page with the largest offset from
2487 * m_start that can be mapped at a virtual address less than the given
2488 * virtual address end. Not every virtual page between start and end
2489 * is mapped; only those for which a resident page exists with the
2490 * corresponding offset from m_start are mapped.
2493 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2494 vm_page_t m_start, vm_prot_t prot)
2497 vm_pindex_t diff, psize;
2499 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
2500 psize = atop(end - start);
2504 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2505 mpte = pmap_enter_quick_locked(pmap, start + ptoa(diff), m,
2507 m = TAILQ_NEXT(m, listq);
2513 * this code makes some *MAJOR* assumptions:
2514 * 1. Current pmap & pmap exists.
2517 * 4. No page table pages.
2518 * but is *MUCH* faster than pmap_enter...
2522 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
2526 (void) pmap_enter_quick_locked(pmap, va, m, prot, NULL);
2531 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
2532 vm_prot_t prot, vm_page_t mpte)
2537 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
2538 (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0,
2539 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
2540 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2541 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2544 * In the case that a page table page is not
2545 * resident, we are creating it here.
2547 if (va < VM_MAXUSER_ADDRESS) {
2552 * Calculate pagetable page index
2554 ptepindex = va >> PDRSHIFT;
2555 if (mpte && (mpte->pindex == ptepindex)) {
2559 * Get the page directory entry
2561 ptepa = pmap->pm_pdir[ptepindex];
2564 * If the page table page is mapped, we just increment
2565 * the hold count, and activate it.
2569 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2570 mpte = PHYS_TO_VM_PAGE(ptepa);
2573 mpte = _pmap_allocpte(pmap, ptepindex,
2584 * This call to vtopte makes the assumption that we are
2585 * entering the page into the current pmap. In order to support
2586 * quick entry into any pmap, one would likely use pmap_pte_quick.
2587 * But that isn't as quick as vtopte.
2592 pmap_unwire_pte_hold(pmap, mpte);
2599 * Enter on the PV list if part of our managed memory.
2601 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0 &&
2602 !pmap_try_insert_pv_entry(pmap, va, m)) {
2604 pmap_unwire_pte_hold(pmap, mpte);
2611 * Increment counters
2613 pmap->pm_stats.resident_count++;
2615 pa = VM_PAGE_TO_PHYS(m);
2618 * Now validate mapping with RO protection
2620 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2621 pte_store(pte, pa | PG_V | PG_U);
2623 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
2628 * Make a temporary mapping for a physical address. This is only intended
2629 * to be used for panic dumps.
2632 pmap_kenter_temporary(vm_paddr_t pa, int i)
2636 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
2637 pmap_kenter(va, pa);
2639 return ((void *)crashdumpmap);
2643 * This code maps large physical mmap regions into the
2644 * processor address space. Note that some shortcuts
2645 * are taken, but the code works.
2648 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2649 vm_object_t object, vm_pindex_t pindex,
2654 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2655 KASSERT(object->type == OBJT_DEVICE,
2656 ("pmap_object_init_pt: non-device object"));
2658 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) {
2661 unsigned int ptepindex;
2666 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2670 p = vm_page_lookup(object, pindex);
2672 if (vm_page_sleep_if_busy(p, FALSE, "init4p"))
2675 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2680 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2681 vm_page_lock_queues();
2683 vm_page_unlock_queues();
2687 p = vm_page_lookup(object, pindex);
2688 vm_page_lock_queues();
2690 vm_page_unlock_queues();
2693 ptepa = VM_PAGE_TO_PHYS(p);
2694 if (ptepa & (NBPDR - 1))
2697 p->valid = VM_PAGE_BITS_ALL;
2700 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2701 npdes = size >> PDRSHIFT;
2702 for(i = 0; i < npdes; i++) {
2703 pde_store(&pmap->pm_pdir[ptepindex],
2704 ptepa | PG_U | PG_RW | PG_V | PG_PS);
2708 pmap_invalidate_all(pmap);
2715 * Routine: pmap_change_wiring
2716 * Function: Change the wiring attribute for a map/virtual-address
2718 * In/out conditions:
2719 * The mapping must already exist in the pmap.
2722 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2727 pte = pmap_pte(pmap, va);
2729 if (wired && !pmap_pte_w(pte))
2730 pmap->pm_stats.wired_count++;
2731 else if (!wired && pmap_pte_w(pte))
2732 pmap->pm_stats.wired_count--;
2735 * Wiring is not a hardware characteristic so there is no need to
2738 pmap_pte_set_w(pte, wired);
2739 pmap_pte_release(pte);
2746 * Copy the range specified by src_addr/len
2747 * from the source map to the range dst_addr/len
2748 * in the destination map.
2750 * This routine is only advisory and need not do anything.
2754 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
2755 vm_offset_t src_addr)
2758 vm_offset_t end_addr = src_addr + len;
2761 if (dst_addr != src_addr)
2764 if (!pmap_is_current(src_pmap))
2767 vm_page_lock_queues();
2768 if (dst_pmap < src_pmap) {
2769 PMAP_LOCK(dst_pmap);
2770 PMAP_LOCK(src_pmap);
2772 PMAP_LOCK(src_pmap);
2773 PMAP_LOCK(dst_pmap);
2776 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2777 pt_entry_t *src_pte, *dst_pte;
2778 vm_page_t dstmpte, srcmpte;
2779 pd_entry_t srcptepaddr;
2782 if (addr >= UPT_MIN_ADDRESS)
2783 panic("pmap_copy: invalid to pmap_copy page tables");
2785 pdnxt = (addr + NBPDR) & ~PDRMASK;
2786 ptepindex = addr >> PDRSHIFT;
2788 srcptepaddr = src_pmap->pm_pdir[ptepindex];
2789 if (srcptepaddr == 0)
2792 if (srcptepaddr & PG_PS) {
2793 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2794 dst_pmap->pm_pdir[ptepindex] = srcptepaddr &
2796 dst_pmap->pm_stats.resident_count +=
2802 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
2803 if (srcmpte->wire_count == 0)
2804 panic("pmap_copy: source page table page is unused");
2806 if (pdnxt > end_addr)
2809 src_pte = vtopte(addr);
2810 while (addr < pdnxt) {
2814 * we only virtual copy managed pages
2816 if ((ptetemp & PG_MANAGED) != 0) {
2817 dstmpte = pmap_allocpte(dst_pmap, addr,
2819 if (dstmpte == NULL)
2821 dst_pte = pmap_pte_quick(dst_pmap, addr);
2822 if (*dst_pte == 0 &&
2823 pmap_try_insert_pv_entry(dst_pmap, addr,
2824 PHYS_TO_VM_PAGE(ptetemp & PG_FRAME))) {
2826 * Clear the wired, modified, and
2827 * accessed (referenced) bits
2830 *dst_pte = ptetemp & ~(PG_W | PG_M |
2832 dst_pmap->pm_stats.resident_count++;
2834 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2835 if (dstmpte->wire_count >= srcmpte->wire_count)
2843 vm_page_unlock_queues();
2844 PMAP_UNLOCK(src_pmap);
2845 PMAP_UNLOCK(dst_pmap);
2848 static __inline void
2849 pagezero(void *page)
2851 #if defined(I686_CPU)
2852 if (cpu_class == CPUCLASS_686) {
2853 #if defined(CPU_ENABLE_SSE)
2854 if (cpu_feature & CPUID_SSE2)
2855 sse2_pagezero(page);
2858 i686_pagezero(page);
2861 bzero(page, PAGE_SIZE);
2865 * pmap_zero_page zeros the specified hardware page by mapping
2866 * the page into KVM and using bzero to clear its contents.
2869 pmap_zero_page(vm_page_t m)
2871 struct sysmaps *sysmaps;
2873 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2874 mtx_lock(&sysmaps->lock);
2875 if (*sysmaps->CMAP2)
2876 panic("pmap_zero_page: CMAP2 busy");
2878 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2879 invlcaddr(sysmaps->CADDR2);
2880 pagezero(sysmaps->CADDR2);
2881 *sysmaps->CMAP2 = 0;
2883 mtx_unlock(&sysmaps->lock);
2887 * pmap_zero_page_area zeros the specified hardware page by mapping
2888 * the page into KVM and using bzero to clear its contents.
2890 * off and size may not cover an area beyond a single hardware page.
2893 pmap_zero_page_area(vm_page_t m, int off, int size)
2895 struct sysmaps *sysmaps;
2897 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2898 mtx_lock(&sysmaps->lock);
2899 if (*sysmaps->CMAP2)
2900 panic("pmap_zero_page: CMAP2 busy");
2902 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2903 invlcaddr(sysmaps->CADDR2);
2904 if (off == 0 && size == PAGE_SIZE)
2905 pagezero(sysmaps->CADDR2);
2907 bzero((char *)sysmaps->CADDR2 + off, size);
2908 *sysmaps->CMAP2 = 0;
2910 mtx_unlock(&sysmaps->lock);
2914 * pmap_zero_page_idle zeros the specified hardware page by mapping
2915 * the page into KVM and using bzero to clear its contents. This
2916 * is intended to be called from the vm_pagezero process only and
2920 pmap_zero_page_idle(vm_page_t m)
2924 panic("pmap_zero_page: CMAP3 busy");
2926 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2934 * pmap_copy_page copies the specified (machine independent)
2935 * page by mapping the page into virtual memory and using
2936 * bcopy to copy the page, one machine dependent page at a
2940 pmap_copy_page(vm_page_t src, vm_page_t dst)
2942 struct sysmaps *sysmaps;
2944 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2945 mtx_lock(&sysmaps->lock);
2946 if (*sysmaps->CMAP1)
2947 panic("pmap_copy_page: CMAP1 busy");
2948 if (*sysmaps->CMAP2)
2949 panic("pmap_copy_page: CMAP2 busy");
2951 invlpg((u_int)sysmaps->CADDR1);
2952 invlpg((u_int)sysmaps->CADDR2);
2953 *sysmaps->CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A;
2954 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M;
2955 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
2956 *sysmaps->CMAP1 = 0;
2957 *sysmaps->CMAP2 = 0;
2959 mtx_unlock(&sysmaps->lock);
2963 * Returns true if the pmap's pv is one of the first
2964 * 16 pvs linked to from this page. This count may
2965 * be changed upwards or downwards in the future; it
2966 * is only necessary that true be returned for a small
2967 * subset of pmaps for proper page aging.
2970 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2975 if (m->flags & PG_FICTITIOUS)
2978 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2979 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2980 if (PV_PMAP(pv) == pmap) {
2991 * Remove all pages from specified address space
2992 * this aids process exit speeds. Also, this code
2993 * is special cased for current process only, but
2994 * can have the more generic (and slightly slower)
2995 * mode enabled. This is much faster than pmap_remove
2996 * in the case of running down an entire address space.
2999 pmap_remove_pages(pmap_t pmap)
3001 pt_entry_t *pte, tpte;
3004 struct pv_chunk *pc, *npc;
3007 uint32_t inuse, bitmask;
3010 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
3011 printf("warning: pmap_remove_pages called with non-current pmap\n");
3014 vm_page_lock_queues();
3017 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
3019 for (field = 0; field < _NPCM; field++) {
3020 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
3021 while (inuse != 0) {
3023 bitmask = 1UL << bit;
3024 idx = field * 32 + bit;
3025 pv = &pc->pc_pventry[idx];
3028 pte = vtopte(pv->pv_va);
3033 "TPTE at %p IS ZERO @ VA %08x\n",
3039 * We cannot remove wired pages from a process' mapping at this time
3046 m = PHYS_TO_VM_PAGE(tpte);
3047 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3048 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
3049 m, (uintmax_t)m->phys_addr,
3052 KASSERT(m < &vm_page_array[vm_page_array_size],
3053 ("pmap_remove_pages: bad tpte %#jx",
3056 pmap->pm_stats.resident_count--;
3061 * Update the vm_page_t clean/reference bits.
3067 PV_STAT(pv_entry_frees++);
3068 PV_STAT(pv_entry_spare++);
3070 pc->pc_map[field] |= bitmask;
3071 m->md.pv_list_count--;
3072 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3073 if (TAILQ_EMPTY(&m->md.pv_list))
3074 vm_page_flag_clear(m, PG_WRITEABLE);
3076 pmap_unuse_pt(pmap, pv->pv_va);
3080 PV_STAT(pv_entry_spare -= _NPCPV);
3081 PV_STAT(pc_chunk_count--);
3082 PV_STAT(pc_chunk_frees++);
3083 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3084 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
3085 pmap_qremove((vm_offset_t)pc, 1);
3086 vm_page_unwire(m, 0);
3088 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
3092 vm_page_unlock_queues();
3093 pmap_invalidate_all(pmap);
3100 * Return whether or not the specified physical page was modified
3101 * in any physical maps.
3104 pmap_is_modified(vm_page_t m)
3112 if (m->flags & PG_FICTITIOUS)
3116 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3117 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3120 pte = pmap_pte_quick(pmap, pv->pv_va);
3121 rv = (*pte & PG_M) != 0;
3131 * pmap_is_prefaultable:
3133 * Return whether or not the specified virtual address is elgible
3137 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3144 if (*pmap_pde(pmap, addr)) {
3153 * Clear the write and modified bits in each of the given page's mappings.
3156 pmap_remove_write(vm_page_t m)
3160 pt_entry_t oldpte, *pte;
3162 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3163 if ((m->flags & PG_FICTITIOUS) != 0 ||
3164 (m->flags & PG_WRITEABLE) == 0)
3167 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3170 pte = pmap_pte_quick(pmap, pv->pv_va);
3173 if ((oldpte & PG_RW) != 0) {
3175 * Regardless of whether a pte is 32 or 64 bits
3176 * in size, PG_RW and PG_M are among the least
3177 * significant 32 bits.
3179 if (!atomic_cmpset_int((u_int *)pte, oldpte,
3180 oldpte & ~(PG_RW | PG_M)))
3182 if ((oldpte & PG_M) != 0)
3184 pmap_invalidate_page(pmap, pv->pv_va);
3188 vm_page_flag_clear(m, PG_WRITEABLE);
3193 * pmap_ts_referenced:
3195 * Return a count of reference bits for a page, clearing those bits.
3196 * It is not necessary for every reference bit to be cleared, but it
3197 * is necessary that 0 only be returned when there are truly no
3198 * reference bits set.
3200 * XXX: The exact number of bits to check and clear is a matter that
3201 * should be tested and standardized at some point in the future for
3202 * optimal aging of shared pages.
3205 pmap_ts_referenced(vm_page_t m)
3207 pv_entry_t pv, pvf, pvn;
3212 if (m->flags & PG_FICTITIOUS)
3215 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3216 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3219 pvn = TAILQ_NEXT(pv, pv_list);
3220 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3221 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3224 pte = pmap_pte_quick(pmap, pv->pv_va);
3225 if ((*pte & PG_A) != 0) {
3226 atomic_clear_int((u_int *)pte, PG_A);
3227 pmap_invalidate_page(pmap, pv->pv_va);
3233 } while ((pv = pvn) != NULL && pv != pvf);
3240 * Clear the modify bits on the specified physical page.
3243 pmap_clear_modify(vm_page_t m)
3249 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3250 if ((m->flags & PG_FICTITIOUS) != 0)
3253 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3256 pte = pmap_pte_quick(pmap, pv->pv_va);
3257 if ((*pte & PG_M) != 0) {
3259 * Regardless of whether a pte is 32 or 64 bits
3260 * in size, PG_M is among the least significant
3263 atomic_clear_int((u_int *)pte, PG_M);
3264 pmap_invalidate_page(pmap, pv->pv_va);
3272 * pmap_clear_reference:
3274 * Clear the reference bit on the specified physical page.
3277 pmap_clear_reference(vm_page_t m)
3283 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3284 if ((m->flags & PG_FICTITIOUS) != 0)
3287 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3290 pte = pmap_pte_quick(pmap, pv->pv_va);
3291 if ((*pte & PG_A) != 0) {
3293 * Regardless of whether a pte is 32 or 64 bits
3294 * in size, PG_A is among the least significant
3297 atomic_clear_int((u_int *)pte, PG_A);
3298 pmap_invalidate_page(pmap, pv->pv_va);
3306 * Miscellaneous support routines follow
3310 * Map a set of physical memory pages into the kernel virtual
3311 * address space. Return a pointer to where it is mapped. This
3312 * routine is intended to be used for mapping device memory,
3316 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
3318 vm_offset_t va, tmpva, offset;
3320 offset = pa & PAGE_MASK;
3321 size = roundup(offset + size, PAGE_SIZE);
3324 if (pa < KERNLOAD && pa + size <= KERNLOAD)
3327 va = kmem_alloc_nofault(kernel_map, size);
3329 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3331 for (tmpva = va; size > 0; ) {
3332 pmap_kenter_attr(tmpva, pa, mode);
3337 pmap_invalidate_range(kernel_pmap, va, tmpva);
3338 pmap_invalidate_cache();
3339 return ((void *)(va + offset));
3343 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3346 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
3350 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
3353 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
3357 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3359 vm_offset_t base, offset, tmpva;
3361 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
3363 base = va & PG_FRAME;
3364 offset = va & PAGE_MASK;
3365 size = roundup(offset + size, PAGE_SIZE);
3366 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
3367 pmap_kremove(tmpva);
3368 pmap_invalidate_range(kernel_pmap, va, tmpva);
3369 kmem_free(kernel_map, base, size);
3373 pmap_change_attr(va, size, mode)
3378 vm_offset_t base, offset, tmpva;
3383 base = va & PG_FRAME;
3384 offset = va & PAGE_MASK;
3385 size = roundup(offset + size, PAGE_SIZE);
3387 /* Only supported on kernel virtual addresses. */
3388 if (base <= VM_MAXUSER_ADDRESS)
3391 /* 4MB pages and pages that aren't mapped aren't supported. */
3392 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) {
3393 pde = pmap_pde(kernel_pmap, tmpva);
3404 * Ok, all the pages exist and are 4k, so run through them updating
3407 for (tmpva = base; size > 0; ) {
3408 pte = vtopte(tmpva);
3411 * The cache mode bits are all in the low 32-bits of the
3412 * PTE, so we can just spin on updating the low 32-bits.
3415 opte = *(u_int *)pte;
3416 npte = opte & ~(PG_PTE_PAT | PG_NC_PCD | PG_NC_PWT);
3417 npte |= pmap_cache_bits(mode, 0);
3418 } while (npte != opte &&
3419 !atomic_cmpset_int((u_int *)pte, opte, npte));
3425 * Flush CPU caches to make sure any data isn't cached that shouldn't
3428 pmap_invalidate_range(kernel_pmap, base, tmpva);
3429 pmap_invalidate_cache();
3434 * perform the pmap work for mincore
3437 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3439 pt_entry_t *ptep, pte;
3444 ptep = pmap_pte(pmap, addr);
3445 pte = (ptep != NULL) ? *ptep : 0;
3446 pmap_pte_release(ptep);
3452 val = MINCORE_INCORE;
3453 if ((pte & PG_MANAGED) == 0)
3456 pa = pte & PG_FRAME;
3458 m = PHYS_TO_VM_PAGE(pa);
3464 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3467 * Modified by someone else
3469 vm_page_lock_queues();
3470 if (m->dirty || pmap_is_modified(m))
3471 val |= MINCORE_MODIFIED_OTHER;
3472 vm_page_unlock_queues();
3478 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3481 * Referenced by someone else
3483 vm_page_lock_queues();
3484 if ((m->flags & PG_REFERENCED) ||
3485 pmap_ts_referenced(m)) {
3486 val |= MINCORE_REFERENCED_OTHER;
3487 vm_page_flag_set(m, PG_REFERENCED);
3489 vm_page_unlock_queues();
3496 pmap_activate(struct thread *td)
3498 pmap_t pmap, oldpmap;
3502 pmap = vmspace_pmap(td->td_proc->p_vmspace);
3503 oldpmap = PCPU_GET(curpmap);
3505 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask));
3506 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
3508 oldpmap->pm_active &= ~1;
3509 pmap->pm_active |= 1;
3512 cr3 = vtophys(pmap->pm_pdpt);
3514 cr3 = vtophys(pmap->pm_pdir);
3517 * pmap_activate is for the current thread on the current cpu
3519 td->td_pcb->pcb_cr3 = cr3;
3521 PCPU_SET(curpmap, pmap);
3526 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3529 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3533 addr = (addr + PDRMASK) & ~PDRMASK;
3538 #if defined(PMAP_DEBUG)
3539 pmap_pid_dump(int pid)
3546 sx_slock(&allproc_lock);
3547 LIST_FOREACH(p, &allproc, p_list) {
3548 if (p->p_pid != pid)
3554 pmap = vmspace_pmap(p->p_vmspace);
3555 for (i = 0; i < NPDEPTD; i++) {
3558 vm_offset_t base = i << PDRSHIFT;
3560 pde = &pmap->pm_pdir[i];
3561 if (pde && pmap_pde_v(pde)) {
3562 for (j = 0; j < NPTEPG; j++) {
3563 vm_offset_t va = base + (j << PAGE_SHIFT);
3564 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3569 sx_sunlock(&allproc_lock);
3572 pte = pmap_pte(pmap, va);
3573 if (pte && pmap_pte_v(pte)) {
3577 m = PHYS_TO_VM_PAGE(pa);
3578 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3579 va, pa, m->hold_count, m->wire_count, m->flags);
3594 sx_sunlock(&allproc_lock);
3601 static void pads(pmap_t pm);
3602 void pmap_pvdump(vm_offset_t pa);
3604 /* print address space of pmap*/
3612 if (pm == kernel_pmap)
3614 for (i = 0; i < NPDEPTD; i++)
3616 for (j = 0; j < NPTEPG; j++) {
3617 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3618 if (pm == kernel_pmap && va < KERNBASE)
3620 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3622 ptep = pmap_pte(pm, va);
3623 if (pmap_pte_v(ptep))
3624 printf("%x:%x ", va, *ptep);
3630 pmap_pvdump(vm_paddr_t pa)
3636 printf("pa %x", pa);
3637 m = PHYS_TO_VM_PAGE(pa);
3638 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3640 printf(" -> pmap %p, va %x", (void *)pmap, pv->pv_va);