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_paddr_t avail_end; /* PA of last available physical page */
200 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
201 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
202 int pgeflag = 0; /* PG_G or-in */
203 int pseflag = 0; /* PG_PS or-in */
206 vm_offset_t kernel_vm_end;
207 extern u_int32_t KERNend;
210 static uma_zone_t pdptzone;
214 * Data for the pv entry allocation mechanism
216 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
217 static int shpgperproc = PMAP_SHPGPERPROC;
219 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
220 int pv_maxchunks; /* How many chunks we have KVA for */
221 vm_offset_t pv_vafree; /* freelist stored in the PTE */
224 * All those kernel PT submaps that BSD is so fond of
233 static struct sysmaps sysmaps_pcpu[MAXCPU];
234 pt_entry_t *CMAP1 = 0;
235 static pt_entry_t *CMAP3;
236 caddr_t CADDR1 = 0, ptvmmap = 0;
237 static caddr_t CADDR3;
238 struct msgbuf *msgbufp = 0;
243 static caddr_t crashdumpmap;
246 extern pt_entry_t *SMPpt;
248 static pt_entry_t *PMAP1 = 0, *PMAP2;
249 static pt_entry_t *PADDR1 = 0, *PADDR2;
252 static int PMAP1changedcpu;
253 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
255 "Number of times pmap_pte_quick changed CPU with same PMAP1");
257 static int PMAP1changed;
258 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
260 "Number of times pmap_pte_quick changed PMAP1");
261 static int PMAP1unchanged;
262 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
264 "Number of times pmap_pte_quick didn't change PMAP1");
265 static struct mtx PMAP2mutex;
267 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
268 static pv_entry_t get_pv_entry(pmap_t locked_pmap, int try);
270 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
271 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
272 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva);
273 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
274 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
276 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
277 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
280 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
282 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags);
283 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m);
284 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
285 static void pmap_pte_release(pt_entry_t *pte);
286 static int pmap_unuse_pt(pmap_t, vm_offset_t);
287 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
289 static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
292 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
293 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
296 * Move the kernel virtual free pointer to the next
297 * 4MB. This is used to help improve performance
298 * by using a large (4MB) page for much of the kernel
299 * (.text, .data, .bss)
302 pmap_kmem_choose(vm_offset_t addr)
304 vm_offset_t newaddr = addr;
307 if (cpu_feature & CPUID_PSE)
308 newaddr = (addr + PDRMASK) & ~PDRMASK;
314 * Bootstrap the system enough to run with virtual memory.
316 * On the i386 this is called after mapping has already been enabled
317 * and just syncs the pmap module with what has already been done.
318 * [We can't call it easily with mapping off since the kernel is not
319 * mapped with PA == VA, hence we would have to relocate every address
320 * from the linked base (virtual) address "KERNBASE" to the actual
321 * (physical) address starting relative to 0]
324 pmap_bootstrap(vm_paddr_t firstaddr, vm_paddr_t loadaddr)
327 pt_entry_t *pte, *unused;
328 struct sysmaps *sysmaps;
332 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
333 * large. It should instead be correctly calculated in locore.s and
334 * not based on 'first' (which is a physical address, not a virtual
335 * address, for the start of unused physical memory). The kernel
336 * page tables are NOT double mapped and thus should not be included
337 * in this calculation.
339 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
340 virtual_avail = pmap_kmem_choose(virtual_avail);
342 virtual_end = VM_MAX_KERNEL_ADDRESS;
345 * Initialize the kernel pmap (which is statically allocated).
347 PMAP_LOCK_INIT(kernel_pmap);
348 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
350 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
352 kernel_pmap->pm_active = -1; /* don't allow deactivation */
353 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
354 LIST_INIT(&allpmaps);
355 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
356 mtx_lock_spin(&allpmaps_lock);
357 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
358 mtx_unlock_spin(&allpmaps_lock);
362 * Reserve some special page table entries/VA space for temporary
365 #define SYSMAP(c, p, v, n) \
366 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
372 * CMAP1/CMAP2 are used for zeroing and copying pages.
373 * CMAP3 is used for the idle process page zeroing.
375 for (i = 0; i < MAXCPU; i++) {
376 sysmaps = &sysmaps_pcpu[i];
377 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
378 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
379 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
381 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
382 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
388 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
391 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
393 SYSMAP(caddr_t, unused, ptvmmap, 1)
396 * msgbufp is used to map the system message buffer.
398 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(MSGBUF_SIZE)))
401 * ptemap is used for pmap_pte_quick
403 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1);
404 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1);
406 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
413 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
414 * an early stadium, we cannot yet neatly map video memory ... :-(
415 * Better fixes are very welcome! */
416 if (!arch_i386_is_xbox)
418 for (i = 0; i < NKPT; i++)
421 /* Initialize the PAT MSR if present. */
424 /* Turn on PG_G on kernel page(s) */
436 /* Bail if this CPU doesn't implement PAT. */
437 if (!(cpu_feature & CPUID_PAT))
442 * Leave the indices 0-3 at the default of WB, WT, UC, and UC-.
443 * Program 4 and 5 as WP and WC.
444 * Leave 6 and 7 as UC and UC-.
446 pat_msr = rdmsr(MSR_PAT);
447 pat_msr &= ~(PAT_MASK(4) | PAT_MASK(5));
448 pat_msr |= PAT_VALUE(4, PAT_WRITE_PROTECTED) |
449 PAT_VALUE(5, PAT_WRITE_COMBINING);
452 * Due to some Intel errata, we can only safely use the lower 4
453 * PAT entries. Thus, just replace PAT Index 2 with WC instead
456 * Intel Pentium III Processor Specification Update
457 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
460 * Intel Pentium IV Processor Specification Update
461 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
463 pat_msr = rdmsr(MSR_PAT);
464 pat_msr &= ~PAT_MASK(2);
465 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
467 wrmsr(MSR_PAT, pat_msr);
471 * Set PG_G on kernel pages. Only the BSP calls this when SMP is turned on.
478 vm_offset_t va, endva;
485 endva = KERNBASE + KERNend;
488 va = KERNBASE + KERNLOAD;
490 pdir = kernel_pmap->pm_pdir[KPTDI+i];
492 kernel_pmap->pm_pdir[KPTDI+i] = PTD[KPTDI+i] = pdir;
493 invltlb(); /* Play it safe, invltlb() every time */
498 va = (vm_offset_t)btext;
503 invltlb(); /* Play it safe, invltlb() every time */
510 * Initialize a vm_page's machine-dependent fields.
513 pmap_page_init(vm_page_t m)
516 TAILQ_INIT(&m->md.pv_list);
517 m->md.pv_list_count = 0;
522 static MALLOC_DEFINE(M_PMAPPDPT, "pmap", "pmap pdpt");
525 pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
527 *flags = UMA_SLAB_PRIV;
528 return (contigmalloc(PAGE_SIZE, M_PMAPPDPT, 0, 0x0ULL, 0xffffffffULL,
534 * ABuse the pte nodes for unmapped kva to thread a kva freelist through.
536 * - Must deal with pages in order to ensure that none of the PG_* bits
537 * are ever set, PG_V in particular.
538 * - Assumes we can write to ptes without pte_store() atomic ops, even
539 * on PAE systems. This should be ok.
540 * - Assumes nothing will ever test these addresses for 0 to indicate
541 * no mapping instead of correctly checking PG_V.
542 * - Assumes a vm_offset_t will fit in a pte (true for i386).
543 * Because PG_V is never set, there can be no mappings to invalidate.
546 pmap_ptelist_alloc(vm_offset_t *head)
553 return (va); /* Out of memory */
557 panic("pmap_ptelist_alloc: va with PG_V set!");
563 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
568 panic("pmap_ptelist_free: freeing va with PG_V set!");
570 *pte = *head; /* virtual! PG_V is 0 though */
575 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
581 for (i = npages - 1; i >= 0; i--) {
582 va = (vm_offset_t)base + i * PAGE_SIZE;
583 pmap_ptelist_free(head, va);
589 * Initialize the pmap module.
590 * Called by vm_init, to initialize any structures that the pmap
591 * system needs to map virtual memory.
598 * Initialize the address space (zone) for the pv entries. Set a
599 * high water mark so that the system can recover from excessive
600 * numbers of pv entries.
602 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
603 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
604 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
605 pv_entry_max = roundup(pv_entry_max, _NPCPV);
606 pv_entry_high_water = 9 * (pv_entry_max / 10);
608 pv_maxchunks = pv_entry_max / _NPCPV;
609 pv_chunkbase = (struct pv_chunk *)kmem_alloc_nofault(kernel_map,
610 PAGE_SIZE * pv_maxchunks);
611 if (pv_chunkbase == NULL)
612 panic("pmap_init: not enough kvm for pv chunks");
613 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
615 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
616 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
617 UMA_ZONE_VM | UMA_ZONE_NOFREE);
618 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
623 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
624 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
625 "Max number of PV entries");
626 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
627 "Page share factor per proc");
629 /***************************************************
630 * Low level helper routines.....
631 ***************************************************/
634 * Determine the appropriate bits to set in a PTE or PDE for a specified
638 pmap_cache_bits(int mode, boolean_t is_pde)
640 int pat_flag, pat_index, cache_bits;
642 /* The PAT bit is different for PTE's and PDE's. */
643 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
645 /* If we don't support PAT, map extended modes to older ones. */
646 if (!(cpu_feature & CPUID_PAT)) {
648 case PAT_UNCACHEABLE:
649 case PAT_WRITE_THROUGH:
653 case PAT_WRITE_COMBINING:
654 case PAT_WRITE_PROTECTED:
655 mode = PAT_UNCACHEABLE;
660 /* Map the caching mode to a PAT index. */
663 case PAT_UNCACHEABLE:
666 case PAT_WRITE_THROUGH:
675 case PAT_WRITE_COMBINING:
678 case PAT_WRITE_PROTECTED:
683 case PAT_UNCACHEABLE:
684 case PAT_WRITE_PROTECTED:
687 case PAT_WRITE_THROUGH:
693 case PAT_WRITE_COMBINING:
698 panic("Unknown caching mode %d\n", mode);
701 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
704 cache_bits |= pat_flag;
706 cache_bits |= PG_NC_PCD;
708 cache_bits |= PG_NC_PWT;
713 * For SMP, these functions have to use the IPI mechanism for coherence.
716 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
722 if (!(read_eflags() & PSL_I))
723 panic("%s: interrupts disabled", __func__);
724 mtx_lock_spin(&smp_ipi_mtx);
728 * We need to disable interrupt preemption but MUST NOT have
729 * interrupts disabled here.
730 * XXX we may need to hold schedlock to get a coherent pm_active
731 * XXX critical sections disable interrupts again
733 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
737 cpumask = PCPU_GET(cpumask);
738 other_cpus = PCPU_GET(other_cpus);
739 if (pmap->pm_active & cpumask)
741 if (pmap->pm_active & other_cpus)
742 smp_masked_invlpg(pmap->pm_active & other_cpus, va);
745 mtx_unlock_spin(&smp_ipi_mtx);
751 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
758 if (!(read_eflags() & PSL_I))
759 panic("%s: interrupts disabled", __func__);
760 mtx_lock_spin(&smp_ipi_mtx);
764 * We need to disable interrupt preemption but MUST NOT have
765 * interrupts disabled here.
766 * XXX we may need to hold schedlock to get a coherent pm_active
767 * XXX critical sections disable interrupts again
769 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
770 for (addr = sva; addr < eva; addr += PAGE_SIZE)
772 smp_invlpg_range(sva, eva);
774 cpumask = PCPU_GET(cpumask);
775 other_cpus = PCPU_GET(other_cpus);
776 if (pmap->pm_active & cpumask)
777 for (addr = sva; addr < eva; addr += PAGE_SIZE)
779 if (pmap->pm_active & other_cpus)
780 smp_masked_invlpg_range(pmap->pm_active & other_cpus,
784 mtx_unlock_spin(&smp_ipi_mtx);
790 pmap_invalidate_all(pmap_t pmap)
796 if (!(read_eflags() & PSL_I))
797 panic("%s: interrupts disabled", __func__);
798 mtx_lock_spin(&smp_ipi_mtx);
802 * We need to disable interrupt preemption but MUST NOT have
803 * interrupts disabled here.
804 * XXX we may need to hold schedlock to get a coherent pm_active
805 * XXX critical sections disable interrupts again
807 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
811 cpumask = PCPU_GET(cpumask);
812 other_cpus = PCPU_GET(other_cpus);
813 if (pmap->pm_active & cpumask)
815 if (pmap->pm_active & other_cpus)
816 smp_masked_invltlb(pmap->pm_active & other_cpus);
819 mtx_unlock_spin(&smp_ipi_mtx);
825 pmap_invalidate_cache(void)
829 if (!(read_eflags() & PSL_I))
830 panic("%s: interrupts disabled", __func__);
831 mtx_lock_spin(&smp_ipi_mtx);
835 * We need to disable interrupt preemption but MUST NOT have
836 * interrupts disabled here.
837 * XXX we may need to hold schedlock to get a coherent pm_active
838 * XXX critical sections disable interrupts again
843 mtx_unlock_spin(&smp_ipi_mtx);
849 * Normal, non-SMP, 486+ invalidation functions.
850 * We inline these within pmap.c for speed.
853 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
856 if (pmap == kernel_pmap || pmap->pm_active)
861 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
865 if (pmap == kernel_pmap || pmap->pm_active)
866 for (addr = sva; addr < eva; addr += PAGE_SIZE)
871 pmap_invalidate_all(pmap_t pmap)
874 if (pmap == kernel_pmap || pmap->pm_active)
879 pmap_invalidate_cache(void)
887 * Are we current address space or kernel? N.B. We return FALSE when
888 * a pmap's page table is in use because a kernel thread is borrowing
889 * it. The borrowed page table can change spontaneously, making any
890 * dependence on its continued use subject to a race condition.
893 pmap_is_current(pmap_t pmap)
896 return (pmap == kernel_pmap ||
897 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
898 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
902 * If the given pmap is not the current or kernel pmap, the returned pte must
903 * be released by passing it to pmap_pte_release().
906 pmap_pte(pmap_t pmap, vm_offset_t va)
911 pde = pmap_pde(pmap, va);
915 /* are we current address space or kernel? */
916 if (pmap_is_current(pmap))
918 mtx_lock(&PMAP2mutex);
919 newpf = *pde & PG_FRAME;
920 if ((*PMAP2 & PG_FRAME) != newpf) {
921 *PMAP2 = newpf | PG_RW | PG_V | PG_A | PG_M;
922 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR2);
924 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
930 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
934 pmap_pte_release(pt_entry_t *pte)
937 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2)
938 mtx_unlock(&PMAP2mutex);
942 invlcaddr(void *caddr)
945 invlpg((u_int)caddr);
949 * Super fast pmap_pte routine best used when scanning
950 * the pv lists. This eliminates many coarse-grained
951 * invltlb calls. Note that many of the pv list
952 * scans are across different pmaps. It is very wasteful
953 * to do an entire invltlb for checking a single mapping.
955 * If the given pmap is not the current pmap, vm_page_queue_mtx
956 * must be held and curthread pinned to a CPU.
959 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
964 pde = pmap_pde(pmap, va);
968 /* are we current address space or kernel? */
969 if (pmap_is_current(pmap))
971 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
972 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
973 newpf = *pde & PG_FRAME;
974 if ((*PMAP1 & PG_FRAME) != newpf) {
975 *PMAP1 = newpf | PG_RW | PG_V | PG_A | PG_M;
977 PMAP1cpu = PCPU_GET(cpuid);
983 if (PMAP1cpu != PCPU_GET(cpuid)) {
984 PMAP1cpu = PCPU_GET(cpuid);
990 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
996 * Routine: pmap_extract
998 * Extract the physical page address associated
999 * with the given map/virtual_address pair.
1002 pmap_extract(pmap_t pmap, vm_offset_t va)
1010 pde = pmap->pm_pdir[va >> PDRSHIFT];
1012 if ((pde & PG_PS) != 0) {
1013 rtval = (pde & ~PDRMASK) | (va & PDRMASK);
1017 pte = pmap_pte(pmap, va);
1018 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1019 pmap_pte_release(pte);
1026 * Routine: pmap_extract_and_hold
1028 * Atomically extract and hold the physical page
1029 * with the given pmap and virtual address pair
1030 * if that mapping permits the given protection.
1033 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1040 vm_page_lock_queues();
1042 pde = *pmap_pde(pmap, va);
1045 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1046 m = PHYS_TO_VM_PAGE((pde & ~PDRMASK) |
1052 pte = *pmap_pte_quick(pmap, va);
1054 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1055 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1061 vm_page_unlock_queues();
1066 /***************************************************
1067 * Low level mapping routines.....
1068 ***************************************************/
1071 * Add a wired page to the kva.
1072 * Note: not SMP coherent.
1075 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1080 pte_store(pte, pa | PG_RW | PG_V | pgeflag);
1084 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1089 pte_store(pte, pa | PG_RW | PG_V | pgeflag | pmap_cache_bits(mode, 0));
1093 * Remove a page from the kernel pagetables.
1094 * Note: not SMP coherent.
1097 pmap_kremove(vm_offset_t va)
1106 * Used to map a range of physical addresses into kernel
1107 * virtual address space.
1109 * The value passed in '*virt' is a suggested virtual address for
1110 * the mapping. Architectures which can support a direct-mapped
1111 * physical to virtual region can return the appropriate address
1112 * within that region, leaving '*virt' unchanged. Other
1113 * architectures should map the pages starting at '*virt' and
1114 * update '*virt' with the first usable address after the mapped
1118 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1120 vm_offset_t va, sva;
1123 while (start < end) {
1124 pmap_kenter(va, start);
1128 pmap_invalidate_range(kernel_pmap, sva, va);
1135 * Add a list of wired pages to the kva
1136 * this routine is only used for temporary
1137 * kernel mappings that do not need to have
1138 * page modification or references recorded.
1139 * Note that old mappings are simply written
1140 * over. The page *must* be wired.
1141 * Note: SMP coherent. Uses a ranged shootdown IPI.
1144 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1146 pt_entry_t *endpte, oldpte, *pte;
1150 endpte = pte + count;
1151 while (pte < endpte) {
1153 pte_store(pte, VM_PAGE_TO_PHYS(*ma) | pgeflag | PG_RW | PG_V);
1157 if ((oldpte & PG_V) != 0)
1158 pmap_invalidate_range(kernel_pmap, sva, sva + count *
1163 * This routine tears out page mappings from the
1164 * kernel -- it is meant only for temporary mappings.
1165 * Note: SMP coherent. Uses a ranged shootdown IPI.
1168 pmap_qremove(vm_offset_t sva, int count)
1173 while (count-- > 0) {
1177 pmap_invalidate_range(kernel_pmap, sva, va);
1180 /***************************************************
1181 * Page table page management routines.....
1182 ***************************************************/
1185 * This routine unholds page table pages, and if the hold count
1186 * drops to zero, then it decrements the wire count.
1188 static PMAP_INLINE int
1189 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1193 if (m->wire_count == 0)
1194 return _pmap_unwire_pte_hold(pmap, m);
1200 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1205 * unmap the page table page
1207 pmap->pm_pdir[m->pindex] = 0;
1208 --pmap->pm_stats.resident_count;
1211 * Do an invltlb to make the invalidated mapping
1212 * take effect immediately.
1214 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1215 pmap_invalidate_page(pmap, pteva);
1217 vm_page_free_zero(m);
1218 atomic_subtract_int(&cnt.v_wire_count, 1);
1223 * After removing a page table entry, this routine is used to
1224 * conditionally free the page, and manage the hold/wire counts.
1227 pmap_unuse_pt(pmap_t pmap, vm_offset_t va)
1232 if (va >= VM_MAXUSER_ADDRESS)
1234 ptepde = *pmap_pde(pmap, va);
1235 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1236 return pmap_unwire_pte_hold(pmap, mpte);
1240 pmap_pinit0(pmap_t pmap)
1243 PMAP_LOCK_INIT(pmap);
1244 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1246 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1248 pmap->pm_active = 0;
1249 PCPU_SET(curpmap, pmap);
1250 TAILQ_INIT(&pmap->pm_pvchunk);
1251 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1252 mtx_lock_spin(&allpmaps_lock);
1253 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1254 mtx_unlock_spin(&allpmaps_lock);
1258 * Initialize a preallocated and zeroed pmap structure,
1259 * such as one in a vmspace structure.
1262 pmap_pinit(pmap_t pmap)
1264 vm_page_t m, ptdpg[NPGPTD];
1269 PMAP_LOCK_INIT(pmap);
1272 * No need to allocate page table space yet but we do need a valid
1273 * page directory table.
1275 if (pmap->pm_pdir == NULL) {
1276 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map,
1279 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
1280 KASSERT(((vm_offset_t)pmap->pm_pdpt &
1281 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
1282 ("pmap_pinit: pdpt misaligned"));
1283 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
1284 ("pmap_pinit: pdpt above 4g"));
1289 * allocate the page directory page(s)
1291 for (i = 0; i < NPGPTD;) {
1292 m = vm_page_alloc(NULL, color++,
1293 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1302 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1304 for (i = 0; i < NPGPTD; i++) {
1305 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1306 bzero(pmap->pm_pdir + (i * NPDEPG), PAGE_SIZE);
1309 mtx_lock_spin(&allpmaps_lock);
1310 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1311 mtx_unlock_spin(&allpmaps_lock);
1312 /* Wire in kernel global address entries. */
1313 /* XXX copies current process, does not fill in MPPTDI */
1314 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1316 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1319 /* install self-referential address mapping entry(s) */
1320 for (i = 0; i < NPGPTD; i++) {
1321 pa = VM_PAGE_TO_PHYS(ptdpg[i]);
1322 pmap->pm_pdir[PTDPTDI + i] = pa | PG_V | PG_RW | PG_A | PG_M;
1324 pmap->pm_pdpt[i] = pa | PG_V;
1328 pmap->pm_active = 0;
1329 TAILQ_INIT(&pmap->pm_pvchunk);
1330 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1334 * this routine is called if the page table page is not
1338 _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags)
1343 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1344 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1345 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1348 * Allocate a page table page.
1350 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1351 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1352 if (flags & M_WAITOK) {
1354 vm_page_unlock_queues();
1356 vm_page_lock_queues();
1361 * Indicate the need to retry. While waiting, the page table
1362 * page may have been allocated.
1366 if ((m->flags & PG_ZERO) == 0)
1370 * Map the pagetable page into the process address space, if
1371 * it isn't already there.
1374 pmap->pm_stats.resident_count++;
1376 ptepa = VM_PAGE_TO_PHYS(m);
1377 pmap->pm_pdir[ptepindex] =
1378 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1384 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1390 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1391 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1392 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1395 * Calculate pagetable page index
1397 ptepindex = va >> PDRSHIFT;
1400 * Get the page directory entry
1402 ptepa = pmap->pm_pdir[ptepindex];
1405 * This supports switching from a 4MB page to a
1408 if (ptepa & PG_PS) {
1409 pmap->pm_pdir[ptepindex] = 0;
1411 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1412 pmap_invalidate_all(kernel_pmap);
1416 * If the page table page is mapped, we just increment the
1417 * hold count, and activate it.
1420 m = PHYS_TO_VM_PAGE(ptepa);
1424 * Here if the pte page isn't mapped, or if it has
1427 m = _pmap_allocpte(pmap, ptepindex, flags);
1428 if (m == NULL && (flags & M_WAITOK))
1435 /***************************************************
1436 * Pmap allocation/deallocation routines.
1437 ***************************************************/
1441 * Deal with a SMP shootdown of other users of the pmap that we are
1442 * trying to dispose of. This can be a bit hairy.
1444 static u_int *lazymask;
1445 static u_int lazyptd;
1446 static volatile u_int lazywait;
1448 void pmap_lazyfix_action(void);
1451 pmap_lazyfix_action(void)
1453 u_int mymask = PCPU_GET(cpumask);
1456 *ipi_lazypmap_counts[PCPU_GET(cpuid)]++;
1458 if (rcr3() == lazyptd)
1459 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1460 atomic_clear_int(lazymask, mymask);
1461 atomic_store_rel_int(&lazywait, 1);
1465 pmap_lazyfix_self(u_int mymask)
1468 if (rcr3() == lazyptd)
1469 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1470 atomic_clear_int(lazymask, mymask);
1475 pmap_lazyfix(pmap_t pmap)
1481 while ((mask = pmap->pm_active) != 0) {
1483 mask = mask & -mask; /* Find least significant set bit */
1484 mtx_lock_spin(&smp_ipi_mtx);
1486 lazyptd = vtophys(pmap->pm_pdpt);
1488 lazyptd = vtophys(pmap->pm_pdir);
1490 mymask = PCPU_GET(cpumask);
1491 if (mask == mymask) {
1492 lazymask = &pmap->pm_active;
1493 pmap_lazyfix_self(mymask);
1495 atomic_store_rel_int((u_int *)&lazymask,
1496 (u_int)&pmap->pm_active);
1497 atomic_store_rel_int(&lazywait, 0);
1498 ipi_selected(mask, IPI_LAZYPMAP);
1499 while (lazywait == 0) {
1505 mtx_unlock_spin(&smp_ipi_mtx);
1507 printf("pmap_lazyfix: spun for 50000000\n");
1514 * Cleaning up on uniprocessor is easy. For various reasons, we're
1515 * unlikely to have to even execute this code, including the fact
1516 * that the cleanup is deferred until the parent does a wait(2), which
1517 * means that another userland process has run.
1520 pmap_lazyfix(pmap_t pmap)
1524 cr3 = vtophys(pmap->pm_pdir);
1525 if (cr3 == rcr3()) {
1526 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1527 pmap->pm_active &= ~(PCPU_GET(cpumask));
1533 * Release any resources held by the given physical map.
1534 * Called when a pmap initialized by pmap_pinit is being released.
1535 * Should only be called if the map contains no valid mappings.
1538 pmap_release(pmap_t pmap)
1540 vm_page_t m, ptdpg[NPGPTD];
1543 KASSERT(pmap->pm_stats.resident_count == 0,
1544 ("pmap_release: pmap resident count %ld != 0",
1545 pmap->pm_stats.resident_count));
1548 mtx_lock_spin(&allpmaps_lock);
1549 LIST_REMOVE(pmap, pm_list);
1550 mtx_unlock_spin(&allpmaps_lock);
1552 for (i = 0; i < NPGPTD; i++)
1553 ptdpg[i] = PHYS_TO_VM_PAGE(pmap->pm_pdir[PTDPTDI + i]);
1555 bzero(pmap->pm_pdir + PTDPTDI, (nkpt + NPGPTD) *
1556 sizeof(*pmap->pm_pdir));
1558 pmap->pm_pdir[MPPTDI] = 0;
1561 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1563 vm_page_lock_queues();
1564 for (i = 0; i < NPGPTD; i++) {
1567 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME),
1568 ("pmap_release: got wrong ptd page"));
1571 atomic_subtract_int(&cnt.v_wire_count, 1);
1572 vm_page_free_zero(m);
1574 vm_page_unlock_queues();
1575 PMAP_LOCK_DESTROY(pmap);
1579 kvm_size(SYSCTL_HANDLER_ARGS)
1581 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1583 return sysctl_handle_long(oidp, &ksize, 0, req);
1585 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1586 0, 0, kvm_size, "IU", "Size of KVM");
1589 kvm_free(SYSCTL_HANDLER_ARGS)
1591 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1593 return sysctl_handle_long(oidp, &kfree, 0, req);
1595 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1596 0, 0, kvm_free, "IU", "Amount of KVM free");
1599 * grow the number of kernel page table entries, if needed
1602 pmap_growkernel(vm_offset_t addr)
1605 vm_paddr_t ptppaddr;
1610 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1611 if (kernel_vm_end == 0) {
1612 kernel_vm_end = KERNBASE;
1614 while (pdir_pde(PTD, kernel_vm_end)) {
1615 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1617 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1618 kernel_vm_end = kernel_map->max_offset;
1623 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1624 if (addr - 1 >= kernel_map->max_offset)
1625 addr = kernel_map->max_offset;
1626 while (kernel_vm_end < addr) {
1627 if (pdir_pde(PTD, kernel_vm_end)) {
1628 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1629 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1630 kernel_vm_end = kernel_map->max_offset;
1637 * This index is bogus, but out of the way
1639 nkpg = vm_page_alloc(NULL, nkpt,
1640 VM_ALLOC_NOOBJ | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
1642 panic("pmap_growkernel: no memory to grow kernel");
1646 pmap_zero_page(nkpg);
1647 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1648 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1649 pdir_pde(PTD, kernel_vm_end) = newpdir;
1651 mtx_lock_spin(&allpmaps_lock);
1652 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1653 pde = pmap_pde(pmap, kernel_vm_end);
1654 pde_store(pde, newpdir);
1656 mtx_unlock_spin(&allpmaps_lock);
1657 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1658 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1659 kernel_vm_end = kernel_map->max_offset;
1666 /***************************************************
1667 * page management routines.
1668 ***************************************************/
1670 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1671 CTASSERT(_NPCM == 11);
1673 static __inline struct pv_chunk *
1674 pv_to_chunk(pv_entry_t pv)
1677 return (struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK);
1680 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1682 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
1683 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
1685 static uint32_t pc_freemask[11] = {
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_FREE0_9, PC_FREE0_9,
1689 PC_FREE0_9, PC_FREE10
1692 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1693 "Current number of pv entries");
1696 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1698 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1699 "Current number of pv entry chunks");
1700 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1701 "Current number of pv entry chunks allocated");
1702 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1703 "Current number of pv entry chunks frees");
1704 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1705 "Number of times tried to get a chunk page but failed.");
1707 static long pv_entry_frees, pv_entry_allocs;
1708 static int pv_entry_spare;
1710 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1711 "Current number of pv entry frees");
1712 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1713 "Current number of pv entry allocs");
1714 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1715 "Current number of spare pv entries");
1717 static int pmap_collect_inactive, pmap_collect_active;
1719 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_inactive, CTLFLAG_RD, &pmap_collect_inactive, 0,
1720 "Current number times pmap_collect called on inactive queue");
1721 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_active, CTLFLAG_RD, &pmap_collect_active, 0,
1722 "Current number times pmap_collect called on active queue");
1726 * We are in a serious low memory condition. Resort to
1727 * drastic measures to free some pages so we can allocate
1728 * another pv entry chunk. This is normally called to
1729 * unmap inactive pages, and if necessary, active pages.
1732 pmap_collect(pmap_t locked_pmap, struct vpgqueues *vpq)
1735 pt_entry_t *pte, tpte;
1736 pv_entry_t next_pv, pv;
1741 TAILQ_FOREACH(m, &vpq->pl, pageq) {
1742 if (m->hold_count || m->busy || (m->flags & PG_BUSY))
1744 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
1747 /* Avoid deadlock and lock recursion. */
1748 if (pmap > locked_pmap)
1750 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
1752 pmap->pm_stats.resident_count--;
1753 pte = pmap_pte_quick(pmap, va);
1754 tpte = pte_load_clear(pte);
1755 KASSERT((tpte & PG_W) == 0,
1756 ("pmap_collect: wired pte %#jx", (uintmax_t)tpte));
1758 vm_page_flag_set(m, PG_REFERENCED);
1760 KASSERT((tpte & PG_RW),
1761 ("pmap_collect: modified page not writable: va: %#x, pte: %#jx",
1762 va, (uintmax_t)tpte));
1765 pmap_invalidate_page(pmap, va);
1766 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1767 if (TAILQ_EMPTY(&m->md.pv_list))
1768 vm_page_flag_clear(m, PG_WRITEABLE);
1769 m->md.pv_list_count--;
1770 pmap_unuse_pt(pmap, va);
1771 free_pv_entry(pmap, pv);
1772 if (pmap != locked_pmap)
1781 * free the pv_entry back to the free list
1784 free_pv_entry(pmap_t pmap, pv_entry_t pv)
1787 struct pv_chunk *pc;
1788 int idx, field, bit;
1790 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1791 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1792 PV_STAT(pv_entry_frees++);
1793 PV_STAT(pv_entry_spare++);
1795 pc = pv_to_chunk(pv);
1796 idx = pv - &pc->pc_pventry[0];
1799 pc->pc_map[field] |= 1ul << bit;
1800 /* move to head of list */
1801 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1802 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
1803 for (idx = 0; idx < _NPCM; idx++)
1804 if (pc->pc_map[idx] != pc_freemask[idx])
1806 PV_STAT(pv_entry_spare -= _NPCPV);
1807 PV_STAT(pc_chunk_count--);
1808 PV_STAT(pc_chunk_frees++);
1809 /* entire chunk is free, return it */
1810 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1811 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
1812 pmap_qremove((vm_offset_t)pc, 1);
1813 vm_page_unwire(m, 0);
1815 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
1819 * get a new pv_entry, allocating a block from the system
1823 get_pv_entry(pmap_t pmap, int try)
1825 static const struct timeval printinterval = { 60, 0 };
1826 static struct timeval lastprint;
1827 static vm_pindex_t colour;
1828 int bit, field, page_req;
1830 struct pv_chunk *pc;
1833 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1834 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1835 PV_STAT(pv_entry_allocs++);
1837 if (pv_entry_count > pv_entry_high_water)
1838 pagedaemon_wakeup();
1839 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
1841 for (field = 0; field < _NPCM; field++) {
1842 if (pc->pc_map[field]) {
1843 bit = bsfl(pc->pc_map[field]);
1847 if (field < _NPCM) {
1848 pv = &pc->pc_pventry[field * 32 + bit];
1849 pc->pc_map[field] &= ~(1ul << bit);
1850 /* If this was the last item, move it to tail */
1851 for (field = 0; field < _NPCM; field++)
1852 if (pc->pc_map[field] != 0) {
1853 PV_STAT(pv_entry_spare--);
1854 return (pv); /* not full, return */
1856 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
1857 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
1858 PV_STAT(pv_entry_spare--);
1862 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
1863 page_req = try ? VM_ALLOC_NORMAL : VM_ALLOC_SYSTEM;
1864 m = vm_page_alloc(NULL, colour, page_req |
1865 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
1866 if (m == NULL || pc == NULL) {
1869 PV_STAT(pc_chunk_tryfail++);
1871 vm_page_lock_queues();
1872 vm_page_unwire(m, 0);
1874 vm_page_unlock_queues();
1877 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
1881 * Reclaim pv entries: At first, destroy mappings to
1882 * inactive pages. After that, if a pv chunk entry
1883 * is still needed, destroy mappings to active pages.
1885 if (ratecheck(&lastprint, &printinterval))
1886 printf("Approaching the limit on PV entries, "
1887 "consider increasing tunables "
1888 "vm.pmap.shpgperproc or "
1889 "vm.pmap.pv_entry_max\n");
1890 PV_STAT(pmap_collect_inactive++);
1891 pmap_collect(pmap, &vm_page_queues[PQ_INACTIVE]);
1893 m = vm_page_alloc(NULL, colour, VM_ALLOC_SYSTEM |
1894 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
1896 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
1897 if (m == NULL || pc == NULL) {
1898 PV_STAT(pmap_collect_active++);
1899 pmap_collect(pmap, &vm_page_queues[PQ_ACTIVE]);
1901 m = vm_page_alloc(NULL, colour,
1902 VM_ALLOC_SYSTEM | VM_ALLOC_NOOBJ |
1905 pc = (struct pv_chunk *)
1906 pmap_ptelist_alloc(&pv_vafree);
1907 if (m == NULL || pc == NULL)
1908 panic("get_pv_entry: increase vm.pmap.shpgperproc");
1911 PV_STAT(pc_chunk_count++);
1912 PV_STAT(pc_chunk_allocs++);
1914 pmap_qenter((vm_offset_t)pc, &m, 1);
1916 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
1917 for (field = 1; field < _NPCM; field++)
1918 pc->pc_map[field] = pc_freemask[field];
1919 pv = &pc->pc_pventry[0];
1920 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
1921 PV_STAT(pv_entry_spare += _NPCPV - 1);
1926 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1930 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1931 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1932 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1933 if (pmap == PV_PMAP(pv) && va == pv->pv_va)
1936 KASSERT(pv != NULL, ("pmap_remove_entry: pv not found"));
1937 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1938 m->md.pv_list_count--;
1939 if (TAILQ_EMPTY(&m->md.pv_list))
1940 vm_page_flag_clear(m, PG_WRITEABLE);
1941 free_pv_entry(pmap, pv);
1945 * Create a pv entry for page at pa for
1949 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
1953 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1954 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1955 pv = get_pv_entry(pmap, FALSE);
1957 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1958 m->md.pv_list_count++;
1962 * Conditionally create a pv entry.
1965 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
1969 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1970 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1971 if (pv_entry_count < pv_entry_high_water &&
1972 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
1974 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1975 m->md.pv_list_count++;
1982 * pmap_remove_pte: do the things to unmap a page in a process
1985 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va)
1990 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1991 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1992 oldpte = pte_load_clear(ptq);
1994 pmap->pm_stats.wired_count -= 1;
1996 * Machines that don't support invlpg, also don't support
2000 pmap_invalidate_page(kernel_pmap, va);
2001 pmap->pm_stats.resident_count -= 1;
2002 if (oldpte & PG_MANAGED) {
2003 m = PHYS_TO_VM_PAGE(oldpte);
2004 if (oldpte & PG_M) {
2005 KASSERT((oldpte & PG_RW),
2006 ("pmap_remove_pte: modified page not writable: va: %#x, pte: %#jx",
2007 va, (uintmax_t)oldpte));
2011 vm_page_flag_set(m, PG_REFERENCED);
2012 pmap_remove_entry(pmap, m, va);
2014 return (pmap_unuse_pt(pmap, va));
2018 * Remove a single page from a process address space
2021 pmap_remove_page(pmap_t pmap, vm_offset_t va)
2025 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2026 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
2027 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2028 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
2030 pmap_remove_pte(pmap, pte, va);
2031 pmap_invalidate_page(pmap, va);
2035 * Remove the given range of addresses from the specified map.
2037 * It is assumed that the start and end are properly
2038 * rounded to the page size.
2041 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2049 * Perform an unsynchronized read. This is, however, safe.
2051 if (pmap->pm_stats.resident_count == 0)
2056 vm_page_lock_queues();
2061 * special handling of removing one page. a very
2062 * common operation and easy to short circuit some
2065 if ((sva + PAGE_SIZE == eva) &&
2066 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
2067 pmap_remove_page(pmap, sva);
2071 for (; sva < eva; sva = pdnxt) {
2075 * Calculate index for next page table.
2077 pdnxt = (sva + NBPDR) & ~PDRMASK;
2078 if (pmap->pm_stats.resident_count == 0)
2081 pdirindex = sva >> PDRSHIFT;
2082 ptpaddr = pmap->pm_pdir[pdirindex];
2085 * Weed out invalid mappings. Note: we assume that the page
2086 * directory table is always allocated, and in kernel virtual.
2092 * Check for large page.
2094 if ((ptpaddr & PG_PS) != 0) {
2095 pmap->pm_pdir[pdirindex] = 0;
2096 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2102 * Limit our scan to either the end of the va represented
2103 * by the current page table page, or to the end of the
2104 * range being removed.
2109 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2115 * The TLB entry for a PG_G mapping is invalidated
2116 * by pmap_remove_pte().
2118 if ((*pte & PG_G) == 0)
2120 if (pmap_remove_pte(pmap, pte, sva))
2126 vm_page_unlock_queues();
2128 pmap_invalidate_all(pmap);
2133 * Routine: pmap_remove_all
2135 * Removes this physical page from
2136 * all physical maps in which it resides.
2137 * Reflects back modify bits to the pager.
2140 * Original versions of this routine were very
2141 * inefficient because they iteratively called
2142 * pmap_remove (slow...)
2146 pmap_remove_all(vm_page_t m)
2150 pt_entry_t *pte, tpte;
2152 #if defined(PMAP_DIAGNOSTIC)
2154 * XXX This makes pmap_remove_all() illegal for non-managed pages!
2156 if (m->flags & PG_FICTITIOUS) {
2157 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%x",
2158 VM_PAGE_TO_PHYS(m));
2161 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2163 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2166 pmap->pm_stats.resident_count--;
2167 pte = pmap_pte_quick(pmap, pv->pv_va);
2168 tpte = pte_load_clear(pte);
2170 pmap->pm_stats.wired_count--;
2172 vm_page_flag_set(m, PG_REFERENCED);
2175 * Update the vm_page_t clean and reference bits.
2178 KASSERT((tpte & PG_RW),
2179 ("pmap_remove_all: modified page not writable: va: %#x, pte: %#jx",
2180 pv->pv_va, (uintmax_t)tpte));
2183 pmap_invalidate_page(pmap, pv->pv_va);
2184 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2185 m->md.pv_list_count--;
2186 pmap_unuse_pt(pmap, pv->pv_va);
2187 free_pv_entry(pmap, pv);
2190 vm_page_flag_clear(m, PG_WRITEABLE);
2195 * Set the physical protection on the
2196 * specified range of this map as requested.
2199 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2206 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2207 pmap_remove(pmap, sva, eva);
2211 if (prot & VM_PROT_WRITE)
2216 vm_page_lock_queues();
2219 for (; sva < eva; sva = pdnxt) {
2220 unsigned obits, pbits, pdirindex;
2222 pdnxt = (sva + NBPDR) & ~PDRMASK;
2224 pdirindex = sva >> PDRSHIFT;
2225 ptpaddr = pmap->pm_pdir[pdirindex];
2228 * Weed out invalid mappings. Note: we assume that the page
2229 * directory table is always allocated, and in kernel virtual.
2235 * Check for large page.
2237 if ((ptpaddr & PG_PS) != 0) {
2238 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
2246 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2252 * Regardless of whether a pte is 32 or 64 bits in
2253 * size, PG_RW, PG_A, and PG_M are among the least
2254 * significant 32 bits.
2256 obits = pbits = *(u_int *)pte;
2257 if (pbits & PG_MANAGED) {
2260 m = PHYS_TO_VM_PAGE(*pte);
2261 vm_page_flag_set(m, PG_REFERENCED);
2264 if ((pbits & PG_M) != 0) {
2266 m = PHYS_TO_VM_PAGE(*pte);
2271 pbits &= ~(PG_RW | PG_M);
2273 if (pbits != obits) {
2274 if (!atomic_cmpset_int((u_int *)pte, obits,
2278 pmap_invalidate_page(pmap, sva);
2285 vm_page_unlock_queues();
2287 pmap_invalidate_all(pmap);
2292 * Insert the given physical page (p) at
2293 * the specified virtual address (v) in the
2294 * target physical map with the protection requested.
2296 * If specified, the page will be wired down, meaning
2297 * that the related pte can not be reclaimed.
2299 * NB: This is the only routine which MAY NOT lazy-evaluate
2300 * or lose information. That is, this routine must actually
2301 * insert this page into the given map NOW.
2304 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2311 pt_entry_t origpte, newpte;
2316 #ifdef PMAP_DIAGNOSTIC
2317 if (va > VM_MAX_KERNEL_ADDRESS)
2318 panic("pmap_enter: toobig");
2319 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2320 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
2325 vm_page_lock_queues();
2330 * In the case that a page table page is not
2331 * resident, we are creating it here.
2333 if (va < VM_MAXUSER_ADDRESS) {
2334 mpte = pmap_allocpte(pmap, va, M_WAITOK);
2336 #if 0 && defined(PMAP_DIAGNOSTIC)
2338 pd_entry_t *pdeaddr = pmap_pde(pmap, va);
2340 if ((origpte & PG_V) == 0) {
2341 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n",
2342 pmap->pm_pdir[PTDPTDI], origpte, va);
2347 pde = pmap_pde(pmap, va);
2348 if ((*pde & PG_PS) != 0)
2349 panic("pmap_enter: attempted pmap_enter on 4MB page");
2350 pte = pmap_pte_quick(pmap, va);
2353 * Page Directory table entry not valid, we need a new PT page
2356 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x\n",
2357 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
2360 pa = VM_PAGE_TO_PHYS(m);
2363 opa = origpte & PG_FRAME;
2366 * Mapping has not changed, must be protection or wiring change.
2368 if (origpte && (opa == pa)) {
2370 * Wiring change, just update stats. We don't worry about
2371 * wiring PT pages as they remain resident as long as there
2372 * are valid mappings in them. Hence, if a user page is wired,
2373 * the PT page will be also.
2375 if (wired && ((origpte & PG_W) == 0))
2376 pmap->pm_stats.wired_count++;
2377 else if (!wired && (origpte & PG_W))
2378 pmap->pm_stats.wired_count--;
2381 * Remove extra pte reference
2387 * We might be turning off write access to the page,
2388 * so we go ahead and sense modify status.
2390 if (origpte & PG_MANAGED) {
2397 * Mapping has changed, invalidate old range and fall through to
2398 * handle validating new mapping.
2402 pmap->pm_stats.wired_count--;
2403 if (origpte & PG_MANAGED) {
2404 om = PHYS_TO_VM_PAGE(opa);
2405 pmap_remove_entry(pmap, om, va);
2409 KASSERT(mpte->wire_count > 0,
2410 ("pmap_enter: missing reference to page table page,"
2414 pmap->pm_stats.resident_count++;
2417 * Enter on the PV list if part of our managed memory.
2419 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
2420 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
2421 ("pmap_enter: managed mapping within the clean submap"));
2422 pmap_insert_entry(pmap, va, m);
2427 * Increment counters
2430 pmap->pm_stats.wired_count++;
2434 * Now validate mapping with desired protection/wiring.
2436 newpte = (pt_entry_t)(pa | PG_V);
2437 if ((prot & VM_PROT_WRITE) != 0)
2441 if (va < VM_MAXUSER_ADDRESS)
2443 if (pmap == kernel_pmap)
2447 * if the mapping or permission bits are different, we need
2448 * to update the pte.
2450 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2451 if (origpte & PG_V) {
2453 origpte = pte_load_store(pte, newpte | PG_A);
2454 if (origpte & PG_A) {
2455 if (origpte & PG_MANAGED)
2456 vm_page_flag_set(om, PG_REFERENCED);
2457 if (opa != VM_PAGE_TO_PHYS(m))
2460 if (origpte & PG_M) {
2461 KASSERT((origpte & PG_RW),
2462 ("pmap_enter: modified page not writable: va: %#x, pte: %#jx",
2463 va, (uintmax_t)origpte));
2464 if ((origpte & PG_MANAGED) != 0)
2466 if ((prot & VM_PROT_WRITE) == 0)
2470 pmap_invalidate_page(pmap, va);
2472 pte_store(pte, newpte | PG_A);
2475 vm_page_unlock_queues();
2480 * Maps a sequence of resident pages belonging to the same object.
2481 * The sequence begins with the given page m_start. This page is
2482 * mapped at the given virtual address start. Each subsequent page is
2483 * mapped at a virtual address that is offset from start by the same
2484 * amount as the page is offset from m_start within the object. The
2485 * last page in the sequence is the page with the largest offset from
2486 * m_start that can be mapped at a virtual address less than the given
2487 * virtual address end. Not every virtual page between start and end
2488 * is mapped; only those for which a resident page exists with the
2489 * corresponding offset from m_start are mapped.
2492 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2493 vm_page_t m_start, vm_prot_t prot)
2496 vm_pindex_t diff, psize;
2498 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
2499 psize = atop(end - start);
2503 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2504 mpte = pmap_enter_quick_locked(pmap, start + ptoa(diff), m,
2506 m = TAILQ_NEXT(m, listq);
2512 * this code makes some *MAJOR* assumptions:
2513 * 1. Current pmap & pmap exists.
2516 * 4. No page table pages.
2517 * but is *MUCH* faster than pmap_enter...
2521 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
2525 (void) pmap_enter_quick_locked(pmap, va, m, prot, NULL);
2530 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
2531 vm_prot_t prot, vm_page_t mpte)
2536 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
2537 (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0,
2538 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
2539 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2540 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2543 * In the case that a page table page is not
2544 * resident, we are creating it here.
2546 if (va < VM_MAXUSER_ADDRESS) {
2551 * Calculate pagetable page index
2553 ptepindex = va >> PDRSHIFT;
2554 if (mpte && (mpte->pindex == ptepindex)) {
2558 * Get the page directory entry
2560 ptepa = pmap->pm_pdir[ptepindex];
2563 * If the page table page is mapped, we just increment
2564 * the hold count, and activate it.
2568 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2569 mpte = PHYS_TO_VM_PAGE(ptepa);
2572 mpte = _pmap_allocpte(pmap, ptepindex,
2583 * This call to vtopte makes the assumption that we are
2584 * entering the page into the current pmap. In order to support
2585 * quick entry into any pmap, one would likely use pmap_pte_quick.
2586 * But that isn't as quick as vtopte.
2591 pmap_unwire_pte_hold(pmap, mpte);
2598 * Enter on the PV list if part of our managed memory.
2600 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0 &&
2601 !pmap_try_insert_pv_entry(pmap, va, m)) {
2603 pmap_unwire_pte_hold(pmap, mpte);
2610 * Increment counters
2612 pmap->pm_stats.resident_count++;
2614 pa = VM_PAGE_TO_PHYS(m);
2617 * Now validate mapping with RO protection
2619 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2620 pte_store(pte, pa | PG_V | PG_U);
2622 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
2627 * Make a temporary mapping for a physical address. This is only intended
2628 * to be used for panic dumps.
2631 pmap_kenter_temporary(vm_paddr_t pa, int i)
2635 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
2636 pmap_kenter(va, pa);
2638 return ((void *)crashdumpmap);
2642 * This code maps large physical mmap regions into the
2643 * processor address space. Note that some shortcuts
2644 * are taken, but the code works.
2647 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2648 vm_object_t object, vm_pindex_t pindex,
2653 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2654 KASSERT(object->type == OBJT_DEVICE,
2655 ("pmap_object_init_pt: non-device object"));
2657 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) {
2660 unsigned int ptepindex;
2665 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2669 p = vm_page_lookup(object, pindex);
2671 if (vm_page_sleep_if_busy(p, FALSE, "init4p"))
2674 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2679 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2680 vm_page_lock_queues();
2682 vm_page_unlock_queues();
2686 p = vm_page_lookup(object, pindex);
2687 vm_page_lock_queues();
2689 vm_page_unlock_queues();
2692 ptepa = VM_PAGE_TO_PHYS(p);
2693 if (ptepa & (NBPDR - 1))
2696 p->valid = VM_PAGE_BITS_ALL;
2699 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2700 npdes = size >> PDRSHIFT;
2701 for(i = 0; i < npdes; i++) {
2702 pde_store(&pmap->pm_pdir[ptepindex],
2703 ptepa | PG_U | PG_RW | PG_V | PG_PS);
2707 pmap_invalidate_all(pmap);
2714 * Routine: pmap_change_wiring
2715 * Function: Change the wiring attribute for a map/virtual-address
2717 * In/out conditions:
2718 * The mapping must already exist in the pmap.
2721 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2726 pte = pmap_pte(pmap, va);
2728 if (wired && !pmap_pte_w(pte))
2729 pmap->pm_stats.wired_count++;
2730 else if (!wired && pmap_pte_w(pte))
2731 pmap->pm_stats.wired_count--;
2734 * Wiring is not a hardware characteristic so there is no need to
2737 pmap_pte_set_w(pte, wired);
2738 pmap_pte_release(pte);
2745 * Copy the range specified by src_addr/len
2746 * from the source map to the range dst_addr/len
2747 * in the destination map.
2749 * This routine is only advisory and need not do anything.
2753 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
2754 vm_offset_t src_addr)
2757 vm_offset_t end_addr = src_addr + len;
2760 if (dst_addr != src_addr)
2763 if (!pmap_is_current(src_pmap))
2766 vm_page_lock_queues();
2767 if (dst_pmap < src_pmap) {
2768 PMAP_LOCK(dst_pmap);
2769 PMAP_LOCK(src_pmap);
2771 PMAP_LOCK(src_pmap);
2772 PMAP_LOCK(dst_pmap);
2775 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2776 pt_entry_t *src_pte, *dst_pte;
2777 vm_page_t dstmpte, srcmpte;
2778 pd_entry_t srcptepaddr;
2781 if (addr >= UPT_MIN_ADDRESS)
2782 panic("pmap_copy: invalid to pmap_copy page tables");
2784 pdnxt = (addr + NBPDR) & ~PDRMASK;
2785 ptepindex = addr >> PDRSHIFT;
2787 srcptepaddr = src_pmap->pm_pdir[ptepindex];
2788 if (srcptepaddr == 0)
2791 if (srcptepaddr & PG_PS) {
2792 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2793 dst_pmap->pm_pdir[ptepindex] = srcptepaddr &
2795 dst_pmap->pm_stats.resident_count +=
2801 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
2802 if (srcmpte->wire_count == 0)
2803 panic("pmap_copy: source page table page is unused");
2805 if (pdnxt > end_addr)
2808 src_pte = vtopte(addr);
2809 while (addr < pdnxt) {
2813 * we only virtual copy managed pages
2815 if ((ptetemp & PG_MANAGED) != 0) {
2816 dstmpte = pmap_allocpte(dst_pmap, addr,
2818 if (dstmpte == NULL)
2820 dst_pte = pmap_pte_quick(dst_pmap, addr);
2821 if (*dst_pte == 0 &&
2822 pmap_try_insert_pv_entry(dst_pmap, addr,
2823 PHYS_TO_VM_PAGE(ptetemp & PG_FRAME))) {
2825 * Clear the wired, modified, and
2826 * accessed (referenced) bits
2829 *dst_pte = ptetemp & ~(PG_W | PG_M |
2831 dst_pmap->pm_stats.resident_count++;
2833 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2834 if (dstmpte->wire_count >= srcmpte->wire_count)
2842 vm_page_unlock_queues();
2843 PMAP_UNLOCK(src_pmap);
2844 PMAP_UNLOCK(dst_pmap);
2847 static __inline void
2848 pagezero(void *page)
2850 #if defined(I686_CPU)
2851 if (cpu_class == CPUCLASS_686) {
2852 #if defined(CPU_ENABLE_SSE)
2853 if (cpu_feature & CPUID_SSE2)
2854 sse2_pagezero(page);
2857 i686_pagezero(page);
2860 bzero(page, PAGE_SIZE);
2864 * pmap_zero_page zeros the specified hardware page by mapping
2865 * the page into KVM and using bzero to clear its contents.
2868 pmap_zero_page(vm_page_t m)
2870 struct sysmaps *sysmaps;
2872 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2873 mtx_lock(&sysmaps->lock);
2874 if (*sysmaps->CMAP2)
2875 panic("pmap_zero_page: CMAP2 busy");
2877 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2878 invlcaddr(sysmaps->CADDR2);
2879 pagezero(sysmaps->CADDR2);
2880 *sysmaps->CMAP2 = 0;
2882 mtx_unlock(&sysmaps->lock);
2886 * pmap_zero_page_area zeros the specified hardware page by mapping
2887 * the page into KVM and using bzero to clear its contents.
2889 * off and size may not cover an area beyond a single hardware page.
2892 pmap_zero_page_area(vm_page_t m, int off, int size)
2894 struct sysmaps *sysmaps;
2896 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2897 mtx_lock(&sysmaps->lock);
2898 if (*sysmaps->CMAP2)
2899 panic("pmap_zero_page: CMAP2 busy");
2901 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2902 invlcaddr(sysmaps->CADDR2);
2903 if (off == 0 && size == PAGE_SIZE)
2904 pagezero(sysmaps->CADDR2);
2906 bzero((char *)sysmaps->CADDR2 + off, size);
2907 *sysmaps->CMAP2 = 0;
2909 mtx_unlock(&sysmaps->lock);
2913 * pmap_zero_page_idle zeros the specified hardware page by mapping
2914 * the page into KVM and using bzero to clear its contents. This
2915 * is intended to be called from the vm_pagezero process only and
2919 pmap_zero_page_idle(vm_page_t m)
2923 panic("pmap_zero_page: CMAP3 busy");
2925 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2933 * pmap_copy_page copies the specified (machine independent)
2934 * page by mapping the page into virtual memory and using
2935 * bcopy to copy the page, one machine dependent page at a
2939 pmap_copy_page(vm_page_t src, vm_page_t dst)
2941 struct sysmaps *sysmaps;
2943 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
2944 mtx_lock(&sysmaps->lock);
2945 if (*sysmaps->CMAP1)
2946 panic("pmap_copy_page: CMAP1 busy");
2947 if (*sysmaps->CMAP2)
2948 panic("pmap_copy_page: CMAP2 busy");
2950 invlpg((u_int)sysmaps->CADDR1);
2951 invlpg((u_int)sysmaps->CADDR2);
2952 *sysmaps->CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A;
2953 *sysmaps->CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M;
2954 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
2955 *sysmaps->CMAP1 = 0;
2956 *sysmaps->CMAP2 = 0;
2958 mtx_unlock(&sysmaps->lock);
2962 * Returns true if the pmap's pv is one of the first
2963 * 16 pvs linked to from this page. This count may
2964 * be changed upwards or downwards in the future; it
2965 * is only necessary that true be returned for a small
2966 * subset of pmaps for proper page aging.
2969 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2974 if (m->flags & PG_FICTITIOUS)
2977 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2978 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2979 if (PV_PMAP(pv) == pmap) {
2990 * Remove all pages from specified address space
2991 * this aids process exit speeds. Also, this code
2992 * is special cased for current process only, but
2993 * can have the more generic (and slightly slower)
2994 * mode enabled. This is much faster than pmap_remove
2995 * in the case of running down an entire address space.
2998 pmap_remove_pages(pmap_t pmap)
3000 pt_entry_t *pte, tpte;
3003 struct pv_chunk *pc, *npc;
3006 uint32_t inuse, bitmask;
3009 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
3010 printf("warning: pmap_remove_pages called with non-current pmap\n");
3013 vm_page_lock_queues();
3016 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
3018 for (field = 0; field < _NPCM; field++) {
3019 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
3020 while (inuse != 0) {
3022 bitmask = 1UL << bit;
3023 idx = field * 32 + bit;
3024 pv = &pc->pc_pventry[idx];
3027 pte = vtopte(pv->pv_va);
3032 "TPTE at %p IS ZERO @ VA %08x\n",
3038 * We cannot remove wired pages from a process' mapping at this time
3045 m = PHYS_TO_VM_PAGE(tpte);
3046 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3047 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
3048 m, (uintmax_t)m->phys_addr,
3051 KASSERT(m < &vm_page_array[vm_page_array_size],
3052 ("pmap_remove_pages: bad tpte %#jx",
3055 pmap->pm_stats.resident_count--;
3060 * Update the vm_page_t clean/reference bits.
3066 PV_STAT(pv_entry_frees++);
3067 PV_STAT(pv_entry_spare++);
3069 pc->pc_map[field] |= bitmask;
3070 m->md.pv_list_count--;
3071 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3072 if (TAILQ_EMPTY(&m->md.pv_list))
3073 vm_page_flag_clear(m, PG_WRITEABLE);
3075 pmap_unuse_pt(pmap, pv->pv_va);
3079 PV_STAT(pv_entry_spare -= _NPCPV);
3080 PV_STAT(pc_chunk_count--);
3081 PV_STAT(pc_chunk_frees++);
3082 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3083 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
3084 pmap_qremove((vm_offset_t)pc, 1);
3085 vm_page_unwire(m, 0);
3087 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
3091 vm_page_unlock_queues();
3092 pmap_invalidate_all(pmap);
3099 * Return whether or not the specified physical page was modified
3100 * in any physical maps.
3103 pmap_is_modified(vm_page_t m)
3111 if (m->flags & PG_FICTITIOUS)
3115 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3116 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3119 pte = pmap_pte_quick(pmap, pv->pv_va);
3120 rv = (*pte & PG_M) != 0;
3130 * pmap_is_prefaultable:
3132 * Return whether or not the specified virtual address is elgible
3136 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3143 if (*pmap_pde(pmap, addr)) {
3152 * Clear the write and modified bits in each of the given page's mappings.
3155 pmap_remove_write(vm_page_t m)
3159 pt_entry_t oldpte, *pte;
3161 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3162 if ((m->flags & PG_FICTITIOUS) != 0 ||
3163 (m->flags & PG_WRITEABLE) == 0)
3166 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3169 pte = pmap_pte_quick(pmap, pv->pv_va);
3172 if ((oldpte & PG_RW) != 0) {
3174 * Regardless of whether a pte is 32 or 64 bits
3175 * in size, PG_RW and PG_M are among the least
3176 * significant 32 bits.
3178 if (!atomic_cmpset_int((u_int *)pte, oldpte,
3179 oldpte & ~(PG_RW | PG_M)))
3181 if ((oldpte & PG_M) != 0)
3183 pmap_invalidate_page(pmap, pv->pv_va);
3187 vm_page_flag_clear(m, PG_WRITEABLE);
3192 * pmap_ts_referenced:
3194 * Return a count of reference bits for a page, clearing those bits.
3195 * It is not necessary for every reference bit to be cleared, but it
3196 * is necessary that 0 only be returned when there are truly no
3197 * reference bits set.
3199 * XXX: The exact number of bits to check and clear is a matter that
3200 * should be tested and standardized at some point in the future for
3201 * optimal aging of shared pages.
3204 pmap_ts_referenced(vm_page_t m)
3206 pv_entry_t pv, pvf, pvn;
3211 if (m->flags & PG_FICTITIOUS)
3214 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3215 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3218 pvn = TAILQ_NEXT(pv, pv_list);
3219 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3220 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3223 pte = pmap_pte_quick(pmap, pv->pv_va);
3224 if ((*pte & PG_A) != 0) {
3225 atomic_clear_int((u_int *)pte, PG_A);
3226 pmap_invalidate_page(pmap, pv->pv_va);
3232 } while ((pv = pvn) != NULL && pv != pvf);
3239 * Clear the modify bits on the specified physical page.
3242 pmap_clear_modify(vm_page_t m)
3248 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3249 if ((m->flags & PG_FICTITIOUS) != 0)
3252 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3255 pte = pmap_pte_quick(pmap, pv->pv_va);
3256 if ((*pte & PG_M) != 0) {
3258 * Regardless of whether a pte is 32 or 64 bits
3259 * in size, PG_M is among the least significant
3262 atomic_clear_int((u_int *)pte, PG_M);
3263 pmap_invalidate_page(pmap, pv->pv_va);
3271 * pmap_clear_reference:
3273 * Clear the reference bit on the specified physical page.
3276 pmap_clear_reference(vm_page_t m)
3282 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3283 if ((m->flags & PG_FICTITIOUS) != 0)
3286 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3289 pte = pmap_pte_quick(pmap, pv->pv_va);
3290 if ((*pte & PG_A) != 0) {
3292 * Regardless of whether a pte is 32 or 64 bits
3293 * in size, PG_A is among the least significant
3296 atomic_clear_int((u_int *)pte, PG_A);
3297 pmap_invalidate_page(pmap, pv->pv_va);
3305 * Miscellaneous support routines follow
3309 * Map a set of physical memory pages into the kernel virtual
3310 * address space. Return a pointer to where it is mapped. This
3311 * routine is intended to be used for mapping device memory,
3315 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
3317 vm_offset_t va, tmpva, offset;
3319 offset = pa & PAGE_MASK;
3320 size = roundup(offset + size, PAGE_SIZE);
3323 if (pa < KERNLOAD && pa + size <= KERNLOAD)
3326 va = kmem_alloc_nofault(kernel_map, size);
3328 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3330 for (tmpva = va; size > 0; ) {
3331 pmap_kenter_attr(tmpva, pa, mode);
3336 pmap_invalidate_range(kernel_pmap, va, tmpva);
3337 pmap_invalidate_cache();
3338 return ((void *)(va + offset));
3342 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3345 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
3349 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
3352 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
3356 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3358 vm_offset_t base, offset, tmpva;
3360 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
3362 base = va & PG_FRAME;
3363 offset = va & PAGE_MASK;
3364 size = roundup(offset + size, PAGE_SIZE);
3365 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
3366 pmap_kremove(tmpva);
3367 pmap_invalidate_range(kernel_pmap, va, tmpva);
3368 kmem_free(kernel_map, base, size);
3372 pmap_change_attr(va, size, mode)
3377 vm_offset_t base, offset, tmpva;
3382 base = va & PG_FRAME;
3383 offset = va & PAGE_MASK;
3384 size = roundup(offset + size, PAGE_SIZE);
3386 /* Only supported on kernel virtual addresses. */
3387 if (base <= VM_MAXUSER_ADDRESS)
3390 /* 4MB pages and pages that aren't mapped aren't supported. */
3391 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) {
3392 pde = pmap_pde(kernel_pmap, tmpva);
3403 * Ok, all the pages exist and are 4k, so run through them updating
3406 for (tmpva = base; size > 0; ) {
3407 pte = vtopte(tmpva);
3410 * The cache mode bits are all in the low 32-bits of the
3411 * PTE, so we can just spin on updating the low 32-bits.
3414 opte = *(u_int *)pte;
3415 npte = opte & ~(PG_PTE_PAT | PG_NC_PCD | PG_NC_PWT);
3416 npte |= pmap_cache_bits(mode, 0);
3417 } while (npte != opte &&
3418 !atomic_cmpset_int((u_int *)pte, opte, npte));
3424 * Flush CPU caches to make sure any data isn't cached that shouldn't
3427 pmap_invalidate_range(kernel_pmap, base, tmpva);
3428 pmap_invalidate_cache();
3433 * perform the pmap work for mincore
3436 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3438 pt_entry_t *ptep, pte;
3443 ptep = pmap_pte(pmap, addr);
3444 pte = (ptep != NULL) ? *ptep : 0;
3445 pmap_pte_release(ptep);
3451 val = MINCORE_INCORE;
3452 if ((pte & PG_MANAGED) == 0)
3455 pa = pte & PG_FRAME;
3457 m = PHYS_TO_VM_PAGE(pa);
3463 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3466 * Modified by someone else
3468 vm_page_lock_queues();
3469 if (m->dirty || pmap_is_modified(m))
3470 val |= MINCORE_MODIFIED_OTHER;
3471 vm_page_unlock_queues();
3477 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3480 * Referenced by someone else
3482 vm_page_lock_queues();
3483 if ((m->flags & PG_REFERENCED) ||
3484 pmap_ts_referenced(m)) {
3485 val |= MINCORE_REFERENCED_OTHER;
3486 vm_page_flag_set(m, PG_REFERENCED);
3488 vm_page_unlock_queues();
3495 pmap_activate(struct thread *td)
3497 pmap_t pmap, oldpmap;
3501 pmap = vmspace_pmap(td->td_proc->p_vmspace);
3502 oldpmap = PCPU_GET(curpmap);
3504 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask));
3505 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
3507 oldpmap->pm_active &= ~1;
3508 pmap->pm_active |= 1;
3511 cr3 = vtophys(pmap->pm_pdpt);
3513 cr3 = vtophys(pmap->pm_pdir);
3516 * pmap_activate is for the current thread on the current cpu
3518 td->td_pcb->pcb_cr3 = cr3;
3520 PCPU_SET(curpmap, pmap);
3525 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3528 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3532 addr = (addr + PDRMASK) & ~PDRMASK;
3537 #if defined(PMAP_DEBUG)
3538 pmap_pid_dump(int pid)
3545 sx_slock(&allproc_lock);
3546 LIST_FOREACH(p, &allproc, p_list) {
3547 if (p->p_pid != pid)
3553 pmap = vmspace_pmap(p->p_vmspace);
3554 for (i = 0; i < NPDEPTD; i++) {
3557 vm_offset_t base = i << PDRSHIFT;
3559 pde = &pmap->pm_pdir[i];
3560 if (pde && pmap_pde_v(pde)) {
3561 for (j = 0; j < NPTEPG; j++) {
3562 vm_offset_t va = base + (j << PAGE_SHIFT);
3563 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3568 sx_sunlock(&allproc_lock);
3571 pte = pmap_pte(pmap, va);
3572 if (pte && pmap_pte_v(pte)) {
3576 m = PHYS_TO_VM_PAGE(pa);
3577 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3578 va, pa, m->hold_count, m->wire_count, m->flags);
3593 sx_sunlock(&allproc_lock);
3600 static void pads(pmap_t pm);
3601 void pmap_pvdump(vm_offset_t pa);
3603 /* print address space of pmap*/
3611 if (pm == kernel_pmap)
3613 for (i = 0; i < NPDEPTD; i++)
3615 for (j = 0; j < NPTEPG; j++) {
3616 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3617 if (pm == kernel_pmap && va < KERNBASE)
3619 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3621 ptep = pmap_pte(pm, va);
3622 if (pmap_pte_v(ptep))
3623 printf("%x:%x ", va, *ptep);
3629 pmap_pvdump(vm_paddr_t pa)
3635 printf("pa %x", pa);
3636 m = PHYS_TO_VM_PAGE(pa);
3637 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3639 printf(" -> pmap %p, va %x", (void *)pmap, pv->pv_va);