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"
109 #include "opt_xbox.h"
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/kernel.h>
115 #include <sys/lock.h>
116 #include <sys/malloc.h>
117 #include <sys/mman.h>
118 #include <sys/msgbuf.h>
119 #include <sys/mutex.h>
120 #include <sys/proc.h>
121 #include <sys/sf_buf.h>
123 #include <sys/vmmeter.h>
124 #include <sys/sched.h>
125 #include <sys/sysctl.h>
129 #include <sys/cpuset.h>
133 #include <vm/vm_param.h>
134 #include <vm/vm_kern.h>
135 #include <vm/vm_page.h>
136 #include <vm/vm_map.h>
137 #include <vm/vm_object.h>
138 #include <vm/vm_extern.h>
139 #include <vm/vm_pageout.h>
140 #include <vm/vm_pager.h>
143 #include <machine/cpu.h>
144 #include <machine/cputypes.h>
145 #include <machine/md_var.h>
146 #include <machine/pcb.h>
147 #include <machine/specialreg.h>
149 #include <machine/smp.h>
153 #include <machine/xbox.h>
156 #include <xen/interface/xen.h>
157 #include <xen/hypervisor.h>
158 #include <machine/xen/hypercall.h>
159 #include <machine/xen/xenvar.h>
160 #include <machine/xen/xenfunc.h>
162 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
163 #define CPU_ENABLE_SSE
166 #ifndef PMAP_SHPGPERPROC
167 #define PMAP_SHPGPERPROC 200
172 #if !defined(DIAGNOSTIC)
173 #ifdef __GNUC_GNU_INLINE__
174 #define PMAP_INLINE __attribute__((__gnu_inline__)) inline
176 #define PMAP_INLINE extern inline
184 #define PV_STAT(x) do { x ; } while (0)
186 #define PV_STAT(x) do { } while (0)
190 * Get PDEs and PTEs for user/kernel address space
192 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
193 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
195 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
196 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
197 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
198 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
199 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
201 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
203 #define HAMFISTED_LOCKING
204 #ifdef HAMFISTED_LOCKING
205 static struct mtx createdelete_lock;
208 struct pmap kernel_pmap_store;
209 LIST_HEAD(pmaplist, pmap);
210 static struct pmaplist allpmaps;
211 static struct mtx allpmaps_lock;
213 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
214 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
215 int pgeflag = 0; /* PG_G or-in */
216 int pseflag = 0; /* PG_PS or-in */
219 vm_offset_t kernel_vm_end;
220 extern u_int32_t KERNend;
226 static SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
228 static int pat_works; /* Is page attribute table sane? */
231 * Data for the pv entry allocation mechanism
233 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
234 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
235 static int shpgperproc = PMAP_SHPGPERPROC;
237 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
238 int pv_maxchunks; /* How many chunks we have KVA for */
239 vm_offset_t pv_vafree; /* freelist stored in the PTE */
242 * All those kernel PT submaps that BSD is so fond of
251 static struct sysmaps sysmaps_pcpu[MAXCPU];
252 static pt_entry_t *CMAP3;
254 static caddr_t CADDR3;
255 struct msgbuf *msgbufp = 0;
260 static caddr_t crashdumpmap;
262 static pt_entry_t *PMAP1 = 0, *PMAP2;
263 static pt_entry_t *PADDR1 = 0, *PADDR2;
266 static int PMAP1changedcpu;
267 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
269 "Number of times pmap_pte_quick changed CPU with same PMAP1");
271 static int PMAP1changed;
272 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
274 "Number of times pmap_pte_quick changed PMAP1");
275 static int PMAP1unchanged;
276 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
278 "Number of times pmap_pte_quick didn't change PMAP1");
279 static struct mtx PMAP2mutex;
281 static void free_pv_chunk(struct pv_chunk *pc);
282 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
283 static pv_entry_t get_pv_entry(pmap_t pmap, boolean_t try);
284 static void pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
285 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
288 static vm_page_t pmap_enter_quick_locked(multicall_entry_t **mcl, int *count, pmap_t pmap, vm_offset_t va,
289 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
290 static void pmap_flush_page(vm_page_t m);
291 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
292 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva,
294 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va,
296 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
298 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
301 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
303 static vm_page_t _pmap_allocpte(pmap_t pmap, u_int ptepindex, int flags);
304 static void _pmap_unwire_ptp(pmap_t pmap, vm_page_t m, vm_page_t *free);
305 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
306 static void pmap_pte_release(pt_entry_t *pte);
307 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t *);
308 static boolean_t pmap_is_prefaultable_locked(pmap_t pmap, vm_offset_t addr);
310 static __inline void pagezero(void *page);
312 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
313 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
316 * If you get an error here, then you set KVA_PAGES wrong! See the
317 * description of KVA_PAGES in sys/i386/include/pmap.h. It must be
318 * multiple of 4 for a normal kernel, or a multiple of 8 for a PAE.
320 CTASSERT(KERNBASE % (1 << 24) == 0);
323 pd_set(struct pmap *pmap, int ptepindex, vm_paddr_t val, int type)
325 vm_paddr_t pdir_ma = vtomach(&pmap->pm_pdir[ptepindex]);
330 xen_queue_pt_update(shadow_pdir_ma,
331 xpmap_ptom(val & ~(PG_RW)));
333 xen_queue_pt_update(pdir_ma,
336 case SH_PD_SET_VA_MA:
338 xen_queue_pt_update(shadow_pdir_ma,
341 xen_queue_pt_update(pdir_ma, val);
343 case SH_PD_SET_VA_CLEAR:
345 xen_queue_pt_update(shadow_pdir_ma, 0);
347 xen_queue_pt_update(pdir_ma, 0);
353 * Bootstrap the system enough to run with virtual memory.
355 * On the i386 this is called after mapping has already been enabled
356 * and just syncs the pmap module with what has already been done.
357 * [We can't call it easily with mapping off since the kernel is not
358 * mapped with PA == VA, hence we would have to relocate every address
359 * from the linked base (virtual) address "KERNBASE" to the actual
360 * (physical) address starting relative to 0]
363 pmap_bootstrap(vm_paddr_t firstaddr)
366 pt_entry_t *pte, *unused;
367 struct sysmaps *sysmaps;
371 * Initialize the first available kernel virtual address. However,
372 * using "firstaddr" may waste a few pages of the kernel virtual
373 * address space, because locore may not have mapped every physical
374 * page that it allocated. Preferably, locore would provide a first
375 * unused virtual address in addition to "firstaddr".
377 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
379 virtual_end = VM_MAX_KERNEL_ADDRESS;
382 * Initialize the kernel pmap (which is statically allocated).
384 PMAP_LOCK_INIT(kernel_pmap);
385 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
387 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
389 CPU_FILL(&kernel_pmap->pm_active); /* don't allow deactivation */
390 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
391 LIST_INIT(&allpmaps);
392 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
393 mtx_lock_spin(&allpmaps_lock);
394 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
395 mtx_unlock_spin(&allpmaps_lock);
400 * Reserve some special page table entries/VA space for temporary
403 #define SYSMAP(c, p, v, n) \
404 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
410 * CMAP1/CMAP2 are used for zeroing and copying pages.
411 * CMAP3 is used for the idle process page zeroing.
413 for (i = 0; i < MAXCPU; i++) {
414 sysmaps = &sysmaps_pcpu[i];
415 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
416 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
417 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
418 PT_SET_MA(sysmaps->CADDR1, 0);
419 PT_SET_MA(sysmaps->CADDR2, 0);
421 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
422 PT_SET_MA(CADDR3, 0);
427 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
430 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
432 SYSMAP(caddr_t, unused, ptvmmap, 1)
435 * msgbufp is used to map the system message buffer.
437 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(msgbufsize)))
440 * PADDR1 and PADDR2 are used by pmap_pte_quick() and pmap_pte(),
443 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1)
444 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1)
446 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
451 * Leave in place an identity mapping (virt == phys) for the low 1 MB
452 * physical memory region that is used by the ACPI wakeup code. This
453 * mapping must not have PG_G set.
457 * leave here deliberately to show that this is not supported
460 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
461 * an early stadium, we cannot yet neatly map video memory ... :-(
462 * Better fixes are very welcome! */
463 if (!arch_i386_is_xbox)
465 for (i = 1; i < NKPT; i++)
468 /* Initialize the PAT MSR if present. */
471 /* Turn on PG_G on kernel page(s) */
475 #ifdef HAMFISTED_LOCKING
476 mtx_init(&createdelete_lock, "pmap create/delete", NULL, MTX_DEF);
488 /* Bail if this CPU doesn't implement PAT. */
489 if (!(cpu_feature & CPUID_PAT))
492 if (cpu_vendor_id != CPU_VENDOR_INTEL ||
493 (CPUID_TO_FAMILY(cpu_id) == 6 && CPUID_TO_MODEL(cpu_id) >= 0xe)) {
495 * Leave the indices 0-3 at the default of WB, WT, UC, and UC-.
496 * Program 4 and 5 as WP and WC.
497 * Leave 6 and 7 as UC and UC-.
499 pat_msr = rdmsr(MSR_PAT);
500 pat_msr &= ~(PAT_MASK(4) | PAT_MASK(5));
501 pat_msr |= PAT_VALUE(4, PAT_WRITE_PROTECTED) |
502 PAT_VALUE(5, PAT_WRITE_COMBINING);
506 * Due to some Intel errata, we can only safely use the lower 4
507 * PAT entries. Thus, just replace PAT Index 2 with WC instead
510 * Intel Pentium III Processor Specification Update
511 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
514 * Intel Pentium IV Processor Specification Update
515 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
517 pat_msr = rdmsr(MSR_PAT);
518 pat_msr &= ~PAT_MASK(2);
519 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
522 wrmsr(MSR_PAT, pat_msr);
526 * Initialize a vm_page's machine-dependent fields.
529 pmap_page_init(vm_page_t m)
532 TAILQ_INIT(&m->md.pv_list);
533 m->md.pat_mode = PAT_WRITE_BACK;
537 * ABuse the pte nodes for unmapped kva to thread a kva freelist through.
539 * - Must deal with pages in order to ensure that none of the PG_* bits
540 * are ever set, PG_V in particular.
541 * - Assumes we can write to ptes without pte_store() atomic ops, even
542 * on PAE systems. This should be ok.
543 * - Assumes nothing will ever test these addresses for 0 to indicate
544 * no mapping instead of correctly checking PG_V.
545 * - Assumes a vm_offset_t will fit in a pte (true for i386).
546 * Because PG_V is never set, there can be no mappings to invalidate.
548 static int ptelist_count = 0;
550 pmap_ptelist_alloc(vm_offset_t *head)
553 vm_offset_t *phead = (vm_offset_t *)*head;
555 if (ptelist_count == 0) {
556 printf("out of memory!!!!!!\n");
557 return (0); /* Out of memory */
560 va = phead[ptelist_count];
565 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
567 vm_offset_t *phead = (vm_offset_t *)*head;
569 phead[ptelist_count++] = va;
573 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
579 nstackpages = (npages + PAGE_SIZE/sizeof(vm_offset_t) - 1)/ (PAGE_SIZE/sizeof(vm_offset_t));
580 for (i = 0; i < nstackpages; i++) {
581 va = (vm_offset_t)base + i * PAGE_SIZE;
582 m = vm_page_alloc(NULL, i,
583 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
585 pmap_qenter(va, &m, 1);
588 *head = (vm_offset_t)base;
589 for (i = npages - 1; i >= nstackpages; i--) {
590 va = (vm_offset_t)base + i * PAGE_SIZE;
591 pmap_ptelist_free(head, va);
597 * Initialize the pmap module.
598 * Called by vm_init, to initialize any structures that the pmap
599 * system needs to map virtual memory.
606 * Initialize the address space (zone) for the pv entries. Set a
607 * high water mark so that the system can recover from excessive
608 * numbers of pv entries.
610 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
611 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
612 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
613 pv_entry_max = roundup(pv_entry_max, _NPCPV);
614 pv_entry_high_water = 9 * (pv_entry_max / 10);
616 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
617 pv_chunkbase = (struct pv_chunk *)kmem_alloc_nofault(kernel_map,
618 PAGE_SIZE * pv_maxchunks);
619 if (pv_chunkbase == NULL)
620 panic("pmap_init: not enough kvm for pv chunks");
621 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
625 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
626 "Max number of PV entries");
627 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
628 "Page share factor per proc");
630 static SYSCTL_NODE(_vm_pmap, OID_AUTO, pde, CTLFLAG_RD, 0,
631 "2/4MB page mapping counters");
633 static u_long pmap_pde_mappings;
634 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, mappings, CTLFLAG_RD,
635 &pmap_pde_mappings, 0, "2/4MB page mappings");
637 /***************************************************
638 * Low level helper routines.....
639 ***************************************************/
642 * Determine the appropriate bits to set in a PTE or PDE for a specified
646 pmap_cache_bits(int mode, boolean_t is_pde)
648 int pat_flag, pat_index, cache_bits;
650 /* The PAT bit is different for PTE's and PDE's. */
651 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
653 /* If we don't support PAT, map extended modes to older ones. */
654 if (!(cpu_feature & CPUID_PAT)) {
656 case PAT_UNCACHEABLE:
657 case PAT_WRITE_THROUGH:
661 case PAT_WRITE_COMBINING:
662 case PAT_WRITE_PROTECTED:
663 mode = PAT_UNCACHEABLE;
668 /* Map the caching mode to a PAT index. */
671 case PAT_UNCACHEABLE:
674 case PAT_WRITE_THROUGH:
683 case PAT_WRITE_COMBINING:
686 case PAT_WRITE_PROTECTED:
690 panic("Unknown caching mode %d\n", mode);
695 case PAT_UNCACHEABLE:
696 case PAT_WRITE_PROTECTED:
699 case PAT_WRITE_THROUGH:
705 case PAT_WRITE_COMBINING:
709 panic("Unknown caching mode %d\n", mode);
713 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
716 cache_bits |= pat_flag;
718 cache_bits |= PG_NC_PCD;
720 cache_bits |= PG_NC_PWT;
725 * For SMP, these functions have to use the IPI mechanism for coherence.
727 * N.B.: Before calling any of the following TLB invalidation functions,
728 * the calling processor must ensure that all stores updating a non-
729 * kernel page table are globally performed. Otherwise, another
730 * processor could cache an old, pre-update entry without being
731 * invalidated. This can happen one of two ways: (1) The pmap becomes
732 * active on another processor after its pm_active field is checked by
733 * one of the following functions but before a store updating the page
734 * table is globally performed. (2) The pmap becomes active on another
735 * processor before its pm_active field is checked but due to
736 * speculative loads one of the following functions stills reads the
737 * pmap as inactive on the other processor.
739 * The kernel page table is exempt because its pm_active field is
740 * immutable. The kernel page table is always active on every
744 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
749 CTR2(KTR_PMAP, "pmap_invalidate_page: pmap=%p va=0x%x",
753 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
757 cpuid = PCPU_GET(cpuid);
758 other_cpus = all_cpus;
759 CPU_CLR(cpuid, &other_cpus);
760 if (CPU_ISSET(cpuid, &pmap->pm_active))
762 CPU_AND(&other_cpus, &pmap->pm_active);
763 if (!CPU_EMPTY(&other_cpus))
764 smp_masked_invlpg(other_cpus, va);
771 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
777 CTR3(KTR_PMAP, "pmap_invalidate_page: pmap=%p eva=0x%x sva=0x%x",
781 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
782 for (addr = sva; addr < eva; addr += PAGE_SIZE)
784 smp_invlpg_range(sva, eva);
786 cpuid = PCPU_GET(cpuid);
787 other_cpus = all_cpus;
788 CPU_CLR(cpuid, &other_cpus);
789 if (CPU_ISSET(cpuid, &pmap->pm_active))
790 for (addr = sva; addr < eva; addr += PAGE_SIZE)
792 CPU_AND(&other_cpus, &pmap->pm_active);
793 if (!CPU_EMPTY(&other_cpus))
794 smp_masked_invlpg_range(other_cpus, sva, eva);
801 pmap_invalidate_all(pmap_t pmap)
806 CTR1(KTR_PMAP, "pmap_invalidate_page: pmap=%p", pmap);
809 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
813 cpuid = PCPU_GET(cpuid);
814 other_cpus = all_cpus;
815 CPU_CLR(cpuid, &other_cpus);
816 if (CPU_ISSET(cpuid, &pmap->pm_active))
818 CPU_AND(&other_cpus, &pmap->pm_active);
819 if (!CPU_EMPTY(&other_cpus))
820 smp_masked_invltlb(other_cpus);
826 pmap_invalidate_cache(void)
836 * Normal, non-SMP, 486+ invalidation functions.
837 * We inline these within pmap.c for speed.
840 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
842 CTR2(KTR_PMAP, "pmap_invalidate_page: pmap=%p va=0x%x",
845 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
851 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
855 if (eva - sva > PAGE_SIZE)
856 CTR3(KTR_PMAP, "pmap_invalidate_range: pmap=%p sva=0x%x eva=0x%x",
859 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
860 for (addr = sva; addr < eva; addr += PAGE_SIZE)
866 pmap_invalidate_all(pmap_t pmap)
869 CTR1(KTR_PMAP, "pmap_invalidate_all: pmap=%p", pmap);
871 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
876 pmap_invalidate_cache(void)
883 #define PMAP_CLFLUSH_THRESHOLD (2 * 1024 * 1024)
886 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva)
889 KASSERT((sva & PAGE_MASK) == 0,
890 ("pmap_invalidate_cache_range: sva not page-aligned"));
891 KASSERT((eva & PAGE_MASK) == 0,
892 ("pmap_invalidate_cache_range: eva not page-aligned"));
894 if (cpu_feature & CPUID_SS)
895 ; /* If "Self Snoop" is supported, do nothing. */
896 else if ((cpu_feature & CPUID_CLFSH) != 0 &&
897 eva - sva < PMAP_CLFLUSH_THRESHOLD) {
900 * Otherwise, do per-cache line flush. Use the mfence
901 * instruction to insure that previous stores are
902 * included in the write-back. The processor
903 * propagates flush to other processors in the cache
907 for (; sva < eva; sva += cpu_clflush_line_size)
913 * No targeted cache flush methods are supported by CPU,
914 * or the supplied range is bigger than 2MB.
915 * Globally invalidate cache.
917 pmap_invalidate_cache();
922 pmap_invalidate_cache_pages(vm_page_t *pages, int count)
926 if (count >= PMAP_CLFLUSH_THRESHOLD / PAGE_SIZE ||
927 (cpu_feature & CPUID_CLFSH) == 0) {
928 pmap_invalidate_cache();
930 for (i = 0; i < count; i++)
931 pmap_flush_page(pages[i]);
936 * Are we current address space or kernel? N.B. We return FALSE when
937 * a pmap's page table is in use because a kernel thread is borrowing
938 * it. The borrowed page table can change spontaneously, making any
939 * dependence on its continued use subject to a race condition.
942 pmap_is_current(pmap_t pmap)
945 return (pmap == kernel_pmap ||
946 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
947 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
951 * If the given pmap is not the current or kernel pmap, the returned pte must
952 * be released by passing it to pmap_pte_release().
955 pmap_pte(pmap_t pmap, vm_offset_t va)
960 pde = pmap_pde(pmap, va);
964 /* are we current address space or kernel? */
965 if (pmap_is_current(pmap))
967 mtx_lock(&PMAP2mutex);
968 newpf = *pde & PG_FRAME;
969 if ((*PMAP2 & PG_FRAME) != newpf) {
970 vm_page_lock_queues();
971 PT_SET_MA(PADDR2, newpf | PG_V | PG_A | PG_M);
972 vm_page_unlock_queues();
973 CTR3(KTR_PMAP, "pmap_pte: pmap=%p va=0x%x newpte=0x%08x",
974 pmap, va, (*PMAP2 & 0xffffffff));
976 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
982 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
986 pmap_pte_release(pt_entry_t *pte)
989 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2) {
990 CTR1(KTR_PMAP, "pmap_pte_release: pte=0x%jx",
992 vm_page_lock_queues();
993 PT_SET_VA(PMAP2, 0, TRUE);
994 vm_page_unlock_queues();
995 mtx_unlock(&PMAP2mutex);
1000 invlcaddr(void *caddr)
1003 invlpg((u_int)caddr);
1008 * Super fast pmap_pte routine best used when scanning
1009 * the pv lists. This eliminates many coarse-grained
1010 * invltlb calls. Note that many of the pv list
1011 * scans are across different pmaps. It is very wasteful
1012 * to do an entire invltlb for checking a single mapping.
1014 * If the given pmap is not the current pmap, vm_page_queue_mtx
1015 * must be held and curthread pinned to a CPU.
1018 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
1023 pde = pmap_pde(pmap, va);
1027 /* are we current address space or kernel? */
1028 if (pmap_is_current(pmap))
1029 return (vtopte(va));
1030 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1031 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1032 newpf = *pde & PG_FRAME;
1033 if ((*PMAP1 & PG_FRAME) != newpf) {
1034 PT_SET_MA(PADDR1, newpf | PG_V | PG_A | PG_M);
1035 CTR3(KTR_PMAP, "pmap_pte_quick: pmap=%p va=0x%x newpte=0x%08x",
1036 pmap, va, (u_long)*PMAP1);
1039 PMAP1cpu = PCPU_GET(cpuid);
1044 if (PMAP1cpu != PCPU_GET(cpuid)) {
1045 PMAP1cpu = PCPU_GET(cpuid);
1051 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
1057 * Routine: pmap_extract
1059 * Extract the physical page address associated
1060 * with the given map/virtual_address pair.
1063 pmap_extract(pmap_t pmap, vm_offset_t va)
1072 pde = pmap->pm_pdir[va >> PDRSHIFT];
1074 if ((pde & PG_PS) != 0) {
1075 rtval = xpmap_mtop(pde & PG_PS_FRAME) | (va & PDRMASK);
1079 pte = pmap_pte(pmap, va);
1080 pteval = *pte ? xpmap_mtop(*pte) : 0;
1081 rtval = (pteval & PG_FRAME) | (va & PAGE_MASK);
1082 pmap_pte_release(pte);
1089 * Routine: pmap_extract_ma
1091 * Like pmap_extract, but returns machine address
1094 pmap_extract_ma(pmap_t pmap, vm_offset_t va)
1102 pde = pmap->pm_pdir[va >> PDRSHIFT];
1104 if ((pde & PG_PS) != 0) {
1105 rtval = (pde & ~PDRMASK) | (va & PDRMASK);
1109 pte = pmap_pte(pmap, va);
1110 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1111 pmap_pte_release(pte);
1118 * Routine: pmap_extract_and_hold
1120 * Atomically extract and hold the physical page
1121 * with the given pmap and virtual address pair
1122 * if that mapping permits the given protection.
1125 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1128 pt_entry_t pte, *ptep;
1136 pde = PT_GET(pmap_pde(pmap, va));
1139 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1140 if (vm_page_pa_tryrelock(pmap, (pde &
1141 PG_PS_FRAME) | (va & PDRMASK), &pa))
1143 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
1148 ptep = pmap_pte(pmap, va);
1150 pmap_pte_release(ptep);
1152 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1153 if (vm_page_pa_tryrelock(pmap, pte & PG_FRAME,
1156 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1166 /***************************************************
1167 * Low level mapping routines.....
1168 ***************************************************/
1171 * Add a wired page to the kva.
1172 * Note: not SMP coherent.
1174 * This function may be used before pmap_bootstrap() is called.
1177 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1180 PT_SET_MA(va, xpmap_ptom(pa)| PG_RW | PG_V | pgeflag);
1184 pmap_kenter_ma(vm_offset_t va, vm_paddr_t ma)
1189 pte_store_ma(pte, ma | PG_RW | PG_V | pgeflag);
1192 static __inline void
1193 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1196 PT_SET_MA(va, pa | PG_RW | PG_V | pgeflag | pmap_cache_bits(mode, 0));
1200 * Remove a page from the kernel pagetables.
1201 * Note: not SMP coherent.
1203 * This function may be used before pmap_bootstrap() is called.
1206 pmap_kremove(vm_offset_t va)
1211 PT_CLEAR_VA(pte, FALSE);
1215 * Used to map a range of physical addresses into kernel
1216 * virtual address space.
1218 * The value passed in '*virt' is a suggested virtual address for
1219 * the mapping. Architectures which can support a direct-mapped
1220 * physical to virtual region can return the appropriate address
1221 * within that region, leaving '*virt' unchanged. Other
1222 * architectures should map the pages starting at '*virt' and
1223 * update '*virt' with the first usable address after the mapped
1227 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1229 vm_offset_t va, sva;
1232 CTR4(KTR_PMAP, "pmap_map: va=0x%x start=0x%jx end=0x%jx prot=0x%x",
1233 va, start, end, prot);
1234 while (start < end) {
1235 pmap_kenter(va, start);
1239 pmap_invalidate_range(kernel_pmap, sva, va);
1246 * Add a list of wired pages to the kva
1247 * this routine is only used for temporary
1248 * kernel mappings that do not need to have
1249 * page modification or references recorded.
1250 * Note that old mappings are simply written
1251 * over. The page *must* be wired.
1252 * Note: SMP coherent. Uses a ranged shootdown IPI.
1255 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1257 pt_entry_t *endpte, *pte;
1259 vm_offset_t va = sva;
1261 multicall_entry_t mcl[16];
1262 multicall_entry_t *mclp = mcl;
1265 CTR2(KTR_PMAP, "pmap_qenter:sva=0x%x count=%d", va, count);
1267 endpte = pte + count;
1268 while (pte < endpte) {
1269 pa = VM_PAGE_TO_MACH(*ma) | pgeflag | PG_RW | PG_V | PG_M | PG_A;
1271 mclp->op = __HYPERVISOR_update_va_mapping;
1273 mclp->args[1] = (uint32_t)(pa & 0xffffffff);
1274 mclp->args[2] = (uint32_t)(pa >> 32);
1275 mclp->args[3] = (*pte & PG_V) ? UVMF_INVLPG|UVMF_ALL : 0;
1282 if (mclcount == 16) {
1283 error = HYPERVISOR_multicall(mcl, mclcount);
1286 KASSERT(error == 0, ("bad multicall %d", error));
1290 error = HYPERVISOR_multicall(mcl, mclcount);
1291 KASSERT(error == 0, ("bad multicall %d", error));
1295 for (pte = vtopte(sva), mclcount = 0; mclcount < count; mclcount++, pte++)
1296 KASSERT(*pte, ("pte not set for va=0x%x", sva + mclcount*PAGE_SIZE));
1301 * This routine tears out page mappings from the
1302 * kernel -- it is meant only for temporary mappings.
1303 * Note: SMP coherent. Uses a ranged shootdown IPI.
1306 pmap_qremove(vm_offset_t sva, int count)
1310 CTR2(KTR_PMAP, "pmap_qremove: sva=0x%x count=%d", sva, count);
1312 vm_page_lock_queues();
1314 while (count-- > 0) {
1319 pmap_invalidate_range(kernel_pmap, sva, va);
1321 vm_page_unlock_queues();
1324 /***************************************************
1325 * Page table page management routines.....
1326 ***************************************************/
1327 static __inline void
1328 pmap_free_zero_pages(vm_page_t free)
1332 while (free != NULL) {
1335 vm_page_free_zero(m);
1340 * Decrements a page table page's wire count, which is used to record the
1341 * number of valid page table entries within the page. If the wire count
1342 * drops to zero, then the page table page is unmapped. Returns TRUE if the
1343 * page table page was unmapped and FALSE otherwise.
1345 static inline boolean_t
1346 pmap_unwire_ptp(pmap_t pmap, vm_page_t m, vm_page_t *free)
1350 if (m->wire_count == 0) {
1351 _pmap_unwire_ptp(pmap, m, free);
1358 _pmap_unwire_ptp(pmap_t pmap, vm_page_t m, vm_page_t *free)
1364 * unmap the page table page
1366 xen_pt_unpin(pmap->pm_pdir[m->pindex]);
1368 * page *might* contain residual mapping :-/
1370 PD_CLEAR_VA(pmap, m->pindex, TRUE);
1372 --pmap->pm_stats.resident_count;
1375 * This is a release store so that the ordinary store unmapping
1376 * the page table page is globally performed before TLB shoot-
1379 atomic_subtract_rel_int(&cnt.v_wire_count, 1);
1382 * Do an invltlb to make the invalidated mapping
1383 * take effect immediately.
1385 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1386 pmap_invalidate_page(pmap, pteva);
1389 * Put page on a list so that it is released after
1390 * *ALL* TLB shootdown is done
1397 * After removing a page table entry, this routine is used to
1398 * conditionally free the page, and manage the hold/wire counts.
1401 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t *free)
1406 if (va >= VM_MAXUSER_ADDRESS)
1408 ptepde = PT_GET(pmap_pde(pmap, va));
1409 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1410 return (pmap_unwire_ptp(pmap, mpte, free));
1414 * Initialize the pmap for the swapper process.
1417 pmap_pinit0(pmap_t pmap)
1420 PMAP_LOCK_INIT(pmap);
1422 * Since the page table directory is shared with the kernel pmap,
1423 * which is already included in the list "allpmaps", this pmap does
1424 * not need to be inserted into that list.
1426 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1428 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1430 CPU_ZERO(&pmap->pm_active);
1431 PCPU_SET(curpmap, pmap);
1432 TAILQ_INIT(&pmap->pm_pvchunk);
1433 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1437 * Initialize a preallocated and zeroed pmap structure,
1438 * such as one in a vmspace structure.
1441 pmap_pinit(pmap_t pmap)
1443 vm_page_t m, ptdpg[NPGPTD + 1];
1444 int npgptd = NPGPTD + 1;
1447 #ifdef HAMFISTED_LOCKING
1448 mtx_lock(&createdelete_lock);
1451 PMAP_LOCK_INIT(pmap);
1454 * No need to allocate page table space yet but we do need a valid
1455 * page directory table.
1457 if (pmap->pm_pdir == NULL) {
1458 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map,
1460 if (pmap->pm_pdir == NULL) {
1461 PMAP_LOCK_DESTROY(pmap);
1462 #ifdef HAMFISTED_LOCKING
1463 mtx_unlock(&createdelete_lock);
1468 pmap->pm_pdpt = (pd_entry_t *)kmem_alloc_nofault(kernel_map, 1);
1473 * allocate the page directory page(s)
1475 for (i = 0; i < npgptd;) {
1476 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
1477 VM_ALLOC_WIRED | VM_ALLOC_ZERO);
1485 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1487 for (i = 0; i < NPGPTD; i++)
1488 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1489 pagezero(pmap->pm_pdir + (i * NPDEPG));
1491 mtx_lock_spin(&allpmaps_lock);
1492 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1493 /* Copy the kernel page table directory entries. */
1494 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1495 mtx_unlock_spin(&allpmaps_lock);
1498 pmap_qenter((vm_offset_t)pmap->pm_pdpt, &ptdpg[NPGPTD], 1);
1499 if ((ptdpg[NPGPTD]->flags & PG_ZERO) == 0)
1500 bzero(pmap->pm_pdpt, PAGE_SIZE);
1501 for (i = 0; i < NPGPTD; i++) {
1504 ma = VM_PAGE_TO_MACH(ptdpg[i]);
1505 pmap->pm_pdpt[i] = ma | PG_V;
1509 for (i = 0; i < NPGPTD; i++) {
1513 ma = VM_PAGE_TO_MACH(ptdpg[i]);
1514 pd = pmap->pm_pdir + (i * NPDEPG);
1515 PT_SET_MA(pd, *vtopte((vm_offset_t)pd) & ~(PG_M|PG_A|PG_U|PG_RW));
1522 PT_SET_MA(pmap->pm_pdpt, *vtopte((vm_offset_t)pmap->pm_pdpt) & ~PG_RW);
1524 vm_page_lock_queues();
1526 xen_pgdpt_pin(VM_PAGE_TO_MACH(ptdpg[NPGPTD]));
1527 for (i = 0; i < NPGPTD; i++) {
1528 vm_paddr_t ma = VM_PAGE_TO_MACH(ptdpg[i]);
1529 PT_SET_VA_MA(&pmap->pm_pdir[PTDPTDI + i], ma | PG_V | PG_A, FALSE);
1532 vm_page_unlock_queues();
1533 CPU_ZERO(&pmap->pm_active);
1534 TAILQ_INIT(&pmap->pm_pvchunk);
1535 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1537 #ifdef HAMFISTED_LOCKING
1538 mtx_unlock(&createdelete_lock);
1544 * this routine is called if the page table page is not
1548 _pmap_allocpte(pmap_t pmap, u_int ptepindex, int flags)
1553 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1554 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1555 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1558 * Allocate a page table page.
1560 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1561 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1562 if (flags & M_WAITOK) {
1564 vm_page_unlock_queues();
1566 vm_page_lock_queues();
1571 * Indicate the need to retry. While waiting, the page table
1572 * page may have been allocated.
1576 if ((m->flags & PG_ZERO) == 0)
1580 * Map the pagetable page into the process address space, if
1581 * it isn't already there.
1584 pmap->pm_stats.resident_count++;
1586 ptema = VM_PAGE_TO_MACH(m);
1588 PT_SET_VA_MA(&pmap->pm_pdir[ptepindex],
1589 (ptema | PG_U | PG_RW | PG_V | PG_A | PG_M), TRUE);
1591 KASSERT(pmap->pm_pdir[ptepindex],
1592 ("_pmap_allocpte: ptepindex=%d did not get mapped", ptepindex));
1597 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1603 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1604 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1605 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1608 * Calculate pagetable page index
1610 ptepindex = va >> PDRSHIFT;
1613 * Get the page directory entry
1615 ptema = pmap->pm_pdir[ptepindex];
1618 * This supports switching from a 4MB page to a
1621 if (ptema & PG_PS) {
1625 pmap->pm_pdir[ptepindex] = 0;
1627 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1628 pmap_invalidate_all(kernel_pmap);
1632 * If the page table page is mapped, we just increment the
1633 * hold count, and activate it.
1636 m = PHYS_TO_VM_PAGE(xpmap_mtop(ptema) & PG_FRAME);
1640 * Here if the pte page isn't mapped, or if it has
1643 CTR3(KTR_PMAP, "pmap_allocpte: pmap=%p va=0x%08x flags=0x%x",
1645 m = _pmap_allocpte(pmap, ptepindex, flags);
1646 if (m == NULL && (flags & M_WAITOK))
1649 KASSERT(pmap->pm_pdir[ptepindex], ("ptepindex=%d did not get mapped", ptepindex));
1655 /***************************************************
1656 * Pmap allocation/deallocation routines.
1657 ***************************************************/
1661 * Deal with a SMP shootdown of other users of the pmap that we are
1662 * trying to dispose of. This can be a bit hairy.
1664 static cpuset_t *lazymask;
1665 static u_int lazyptd;
1666 static volatile u_int lazywait;
1668 void pmap_lazyfix_action(void);
1671 pmap_lazyfix_action(void)
1675 (*ipi_lazypmap_counts[PCPU_GET(cpuid)])++;
1677 if (rcr3() == lazyptd)
1678 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1679 CPU_CLR_ATOMIC(PCPU_GET(cpuid), lazymask);
1680 atomic_store_rel_int(&lazywait, 1);
1684 pmap_lazyfix_self(u_int cpuid)
1687 if (rcr3() == lazyptd)
1688 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1689 CPU_CLR_ATOMIC(cpuid, lazymask);
1694 pmap_lazyfix(pmap_t pmap)
1696 cpuset_t mymask, mask;
1700 mask = pmap->pm_active;
1701 while (!CPU_EMPTY(&mask)) {
1704 /* Find least significant set bit. */
1705 lsb = cpusetobj_ffs(&mask);
1708 CPU_SETOF(lsb, &mask);
1709 mtx_lock_spin(&smp_ipi_mtx);
1711 lazyptd = vtophys(pmap->pm_pdpt);
1713 lazyptd = vtophys(pmap->pm_pdir);
1715 cpuid = PCPU_GET(cpuid);
1717 /* Use a cpuset just for having an easy check. */
1718 CPU_SETOF(cpuid, &mymask);
1719 if (!CPU_CMP(&mask, &mymask)) {
1720 lazymask = &pmap->pm_active;
1721 pmap_lazyfix_self(cpuid);
1723 atomic_store_rel_int((u_int *)&lazymask,
1724 (u_int)&pmap->pm_active);
1725 atomic_store_rel_int(&lazywait, 0);
1726 ipi_selected(mask, IPI_LAZYPMAP);
1727 while (lazywait == 0) {
1733 mtx_unlock_spin(&smp_ipi_mtx);
1735 printf("pmap_lazyfix: spun for 50000000\n");
1736 mask = pmap->pm_active;
1743 * Cleaning up on uniprocessor is easy. For various reasons, we're
1744 * unlikely to have to even execute this code, including the fact
1745 * that the cleanup is deferred until the parent does a wait(2), which
1746 * means that another userland process has run.
1749 pmap_lazyfix(pmap_t pmap)
1753 cr3 = vtophys(pmap->pm_pdir);
1754 if (cr3 == rcr3()) {
1755 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1756 CPU_CLR(PCPU_GET(cpuid), &pmap->pm_active);
1762 * Release any resources held by the given physical map.
1763 * Called when a pmap initialized by pmap_pinit is being released.
1764 * Should only be called if the map contains no valid mappings.
1767 pmap_release(pmap_t pmap)
1769 vm_page_t m, ptdpg[2*NPGPTD+1];
1773 int npgptd = NPGPTD + 1;
1775 int npgptd = NPGPTD;
1778 KASSERT(pmap->pm_stats.resident_count == 0,
1779 ("pmap_release: pmap resident count %ld != 0",
1780 pmap->pm_stats.resident_count));
1783 #ifdef HAMFISTED_LOCKING
1784 mtx_lock(&createdelete_lock);
1788 mtx_lock_spin(&allpmaps_lock);
1789 LIST_REMOVE(pmap, pm_list);
1790 mtx_unlock_spin(&allpmaps_lock);
1792 for (i = 0; i < NPGPTD; i++)
1793 ptdpg[i] = PHYS_TO_VM_PAGE(vtophys(pmap->pm_pdir + (i*NPDEPG)) & PG_FRAME);
1794 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1796 ptdpg[NPGPTD] = PHYS_TO_VM_PAGE(vtophys(pmap->pm_pdpt));
1799 for (i = 0; i < npgptd; i++) {
1801 ma = VM_PAGE_TO_MACH(m);
1802 /* unpinning L1 and L2 treated the same */
1811 KASSERT(VM_PAGE_TO_MACH(m) == (pmap->pm_pdpt[i] & PG_FRAME),
1812 ("pmap_release: got wrong ptd page"));
1815 atomic_subtract_int(&cnt.v_wire_count, 1);
1819 pmap_qremove((vm_offset_t)pmap->pm_pdpt, 1);
1821 PMAP_LOCK_DESTROY(pmap);
1823 #ifdef HAMFISTED_LOCKING
1824 mtx_unlock(&createdelete_lock);
1829 kvm_size(SYSCTL_HANDLER_ARGS)
1831 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1833 return (sysctl_handle_long(oidp, &ksize, 0, req));
1835 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1836 0, 0, kvm_size, "IU", "Size of KVM");
1839 kvm_free(SYSCTL_HANDLER_ARGS)
1841 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1843 return (sysctl_handle_long(oidp, &kfree, 0, req));
1845 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1846 0, 0, kvm_free, "IU", "Amount of KVM free");
1849 * grow the number of kernel page table entries, if needed
1852 pmap_growkernel(vm_offset_t addr)
1855 vm_paddr_t ptppaddr;
1859 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1860 if (kernel_vm_end == 0) {
1861 kernel_vm_end = KERNBASE;
1863 while (pdir_pde(PTD, kernel_vm_end)) {
1864 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1866 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1867 kernel_vm_end = kernel_map->max_offset;
1872 addr = roundup2(addr, NBPDR);
1873 if (addr - 1 >= kernel_map->max_offset)
1874 addr = kernel_map->max_offset;
1875 while (kernel_vm_end < addr) {
1876 if (pdir_pde(PTD, kernel_vm_end)) {
1877 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1878 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1879 kernel_vm_end = kernel_map->max_offset;
1885 nkpg = vm_page_alloc(NULL, kernel_vm_end >> PDRSHIFT,
1886 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1889 panic("pmap_growkernel: no memory to grow kernel");
1893 if ((nkpg->flags & PG_ZERO) == 0)
1894 pmap_zero_page(nkpg);
1895 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1896 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1897 vm_page_lock_queues();
1898 PD_SET_VA(kernel_pmap, (kernel_vm_end >> PDRSHIFT), newpdir, TRUE);
1899 mtx_lock_spin(&allpmaps_lock);
1900 LIST_FOREACH(pmap, &allpmaps, pm_list)
1901 PD_SET_VA(pmap, (kernel_vm_end >> PDRSHIFT), newpdir, TRUE);
1903 mtx_unlock_spin(&allpmaps_lock);
1904 vm_page_unlock_queues();
1906 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1907 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1908 kernel_vm_end = kernel_map->max_offset;
1915 /***************************************************
1916 * page management routines.
1917 ***************************************************/
1919 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1920 CTASSERT(_NPCM == 11);
1921 CTASSERT(_NPCPV == 336);
1923 static __inline struct pv_chunk *
1924 pv_to_chunk(pv_entry_t pv)
1927 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
1930 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1932 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
1933 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
1935 static const uint32_t pc_freemask[_NPCM] = {
1936 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1937 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1938 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1939 PC_FREE0_9, PC_FREE10
1942 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1943 "Current number of pv entries");
1946 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1948 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1949 "Current number of pv entry chunks");
1950 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1951 "Current number of pv entry chunks allocated");
1952 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1953 "Current number of pv entry chunks frees");
1954 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1955 "Number of times tried to get a chunk page but failed.");
1957 static long pv_entry_frees, pv_entry_allocs;
1958 static int pv_entry_spare;
1960 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1961 "Current number of pv entry frees");
1962 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1963 "Current number of pv entry allocs");
1964 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1965 "Current number of spare pv entries");
1969 * We are in a serious low memory condition. Resort to
1970 * drastic measures to free some pages so we can allocate
1971 * another pv entry chunk.
1974 pmap_pv_reclaim(pmap_t locked_pmap)
1977 struct pv_chunk *pc;
1979 pt_entry_t *pte, tpte;
1982 vm_page_t free, m, m_pc;
1984 int bit, field, freed;
1986 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1989 TAILQ_INIT(&newtail);
1990 while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL && (pv_vafree == 0 ||
1992 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
1993 if (pmap != pc->pc_pmap) {
1995 pmap_invalidate_all(pmap);
1996 if (pmap != locked_pmap)
2000 /* Avoid deadlock and lock recursion. */
2001 if (pmap > locked_pmap)
2003 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
2005 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2011 * Destroy every non-wired, 4 KB page mapping in the chunk.
2014 for (field = 0; field < _NPCM; field++) {
2015 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2016 inuse != 0; inuse &= ~(1UL << bit)) {
2018 pv = &pc->pc_pventry[field * 32 + bit];
2020 pte = pmap_pte(pmap, va);
2022 if ((tpte & PG_W) == 0)
2023 tpte = pte_load_clear(pte);
2024 pmap_pte_release(pte);
2025 if ((tpte & PG_W) != 0)
2028 ("pmap_pv_reclaim: pmap %p va %x zero pte",
2030 if ((tpte & PG_G) != 0)
2031 pmap_invalidate_page(pmap, va);
2032 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
2033 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2035 if ((tpte & PG_A) != 0)
2036 vm_page_aflag_set(m, PGA_REFERENCED);
2037 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2038 if (TAILQ_EMPTY(&m->md.pv_list))
2039 vm_page_aflag_clear(m, PGA_WRITEABLE);
2040 pc->pc_map[field] |= 1UL << bit;
2041 pmap_unuse_pt(pmap, va, &free);
2046 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2049 /* Every freed mapping is for a 4 KB page. */
2050 pmap->pm_stats.resident_count -= freed;
2051 PV_STAT(pv_entry_frees += freed);
2052 PV_STAT(pv_entry_spare += freed);
2053 pv_entry_count -= freed;
2054 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2055 for (field = 0; field < _NPCM; field++)
2056 if (pc->pc_map[field] != pc_freemask[field]) {
2057 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2059 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2062 * One freed pv entry in locked_pmap is
2065 if (pmap == locked_pmap)
2069 if (field == _NPCM) {
2070 PV_STAT(pv_entry_spare -= _NPCPV);
2071 PV_STAT(pc_chunk_count--);
2072 PV_STAT(pc_chunk_frees++);
2073 /* Entire chunk is free; return it. */
2074 m_pc = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2075 pmap_qremove((vm_offset_t)pc, 1);
2076 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2081 TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
2083 pmap_invalidate_all(pmap);
2084 if (pmap != locked_pmap)
2087 if (m_pc == NULL && pv_vafree != 0 && free != NULL) {
2090 /* Recycle a freed page table page. */
2091 m_pc->wire_count = 1;
2092 atomic_add_int(&cnt.v_wire_count, 1);
2094 pmap_free_zero_pages(free);
2099 * free the pv_entry back to the free list
2102 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2104 struct pv_chunk *pc;
2105 int idx, field, bit;
2107 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2108 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2109 PV_STAT(pv_entry_frees++);
2110 PV_STAT(pv_entry_spare++);
2112 pc = pv_to_chunk(pv);
2113 idx = pv - &pc->pc_pventry[0];
2116 pc->pc_map[field] |= 1ul << bit;
2117 for (idx = 0; idx < _NPCM; idx++)
2118 if (pc->pc_map[idx] != pc_freemask[idx]) {
2120 * 98% of the time, pc is already at the head of the
2121 * list. If it isn't already, move it to the head.
2123 if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
2125 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2126 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2131 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2136 free_pv_chunk(struct pv_chunk *pc)
2140 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2141 PV_STAT(pv_entry_spare -= _NPCPV);
2142 PV_STAT(pc_chunk_count--);
2143 PV_STAT(pc_chunk_frees++);
2144 /* entire chunk is free, return it */
2145 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2146 pmap_qremove((vm_offset_t)pc, 1);
2147 vm_page_unwire(m, 0);
2149 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2153 * get a new pv_entry, allocating a block from the system
2157 get_pv_entry(pmap_t pmap, boolean_t try)
2159 static const struct timeval printinterval = { 60, 0 };
2160 static struct timeval lastprint;
2163 struct pv_chunk *pc;
2166 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2167 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2168 PV_STAT(pv_entry_allocs++);
2170 if (pv_entry_count > pv_entry_high_water)
2171 if (ratecheck(&lastprint, &printinterval))
2172 printf("Approaching the limit on PV entries, consider "
2173 "increasing either the vm.pmap.shpgperproc or the "
2174 "vm.pmap.pv_entry_max tunable.\n");
2176 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2178 for (field = 0; field < _NPCM; field++) {
2179 if (pc->pc_map[field]) {
2180 bit = bsfl(pc->pc_map[field]);
2184 if (field < _NPCM) {
2185 pv = &pc->pc_pventry[field * 32 + bit];
2186 pc->pc_map[field] &= ~(1ul << bit);
2187 /* If this was the last item, move it to tail */
2188 for (field = 0; field < _NPCM; field++)
2189 if (pc->pc_map[field] != 0) {
2190 PV_STAT(pv_entry_spare--);
2191 return (pv); /* not full, return */
2193 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2194 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2195 PV_STAT(pv_entry_spare--);
2200 * Access to the ptelist "pv_vafree" is synchronized by the page
2201 * queues lock. If "pv_vafree" is currently non-empty, it will
2202 * remain non-empty until pmap_ptelist_alloc() completes.
2204 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
2205 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
2208 PV_STAT(pc_chunk_tryfail++);
2211 m = pmap_pv_reclaim(pmap);
2215 PV_STAT(pc_chunk_count++);
2216 PV_STAT(pc_chunk_allocs++);
2217 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
2218 pmap_qenter((vm_offset_t)pc, &m, 1);
2219 if ((m->flags & PG_ZERO) == 0)
2222 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
2223 for (field = 1; field < _NPCM; field++)
2224 pc->pc_map[field] = pc_freemask[field];
2225 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
2226 pv = &pc->pc_pventry[0];
2227 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2228 PV_STAT(pv_entry_spare += _NPCPV - 1);
2232 static __inline pv_entry_t
2233 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2237 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2238 TAILQ_FOREACH(pv, &pvh->pv_list, pv_list) {
2239 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2240 TAILQ_REMOVE(&pvh->pv_list, pv, pv_list);
2248 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2252 pv = pmap_pvh_remove(pvh, pmap, va);
2253 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2254 free_pv_entry(pmap, pv);
2258 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
2261 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2262 pmap_pvh_free(&m->md, pmap, va);
2263 if (TAILQ_EMPTY(&m->md.pv_list))
2264 vm_page_aflag_clear(m, PGA_WRITEABLE);
2268 * Conditionally create a pv entry.
2271 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2275 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2276 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2277 if (pv_entry_count < pv_entry_high_water &&
2278 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2280 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2287 * pmap_remove_pte: do the things to unmap a page in a process
2290 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va, vm_page_t *free)
2295 CTR3(KTR_PMAP, "pmap_remove_pte: pmap=%p *ptq=0x%x va=0x%x",
2296 pmap, (u_long)*ptq, va);
2298 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2299 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2301 PT_SET_VA_MA(ptq, 0, TRUE);
2302 KASSERT(oldpte != 0,
2303 ("pmap_remove_pte: pmap %p va %x zero pte", pmap, va));
2305 pmap->pm_stats.wired_count -= 1;
2307 * Machines that don't support invlpg, also don't support
2311 pmap_invalidate_page(kernel_pmap, va);
2312 pmap->pm_stats.resident_count -= 1;
2313 if (oldpte & PG_MANAGED) {
2314 m = PHYS_TO_VM_PAGE(xpmap_mtop(oldpte) & PG_FRAME);
2315 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2318 vm_page_aflag_set(m, PGA_REFERENCED);
2319 pmap_remove_entry(pmap, m, va);
2321 return (pmap_unuse_pt(pmap, va, free));
2325 * Remove a single page from a process address space
2328 pmap_remove_page(pmap_t pmap, vm_offset_t va, vm_page_t *free)
2332 CTR2(KTR_PMAP, "pmap_remove_page: pmap=%p va=0x%x",
2335 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2336 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
2337 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2338 if ((pte = pmap_pte_quick(pmap, va)) == NULL || (*pte & PG_V) == 0)
2340 pmap_remove_pte(pmap, pte, va, free);
2341 pmap_invalidate_page(pmap, va);
2343 PT_SET_MA(PADDR1, 0);
2348 * Remove the given range of addresses from the specified map.
2350 * It is assumed that the start and end are properly
2351 * rounded to the page size.
2354 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2359 vm_page_t free = NULL;
2362 CTR3(KTR_PMAP, "pmap_remove: pmap=%p sva=0x%x eva=0x%x",
2366 * Perform an unsynchronized read. This is, however, safe.
2368 if (pmap->pm_stats.resident_count == 0)
2373 vm_page_lock_queues();
2378 * special handling of removing one page. a very
2379 * common operation and easy to short circuit some
2382 if ((sva + PAGE_SIZE == eva) &&
2383 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
2384 pmap_remove_page(pmap, sva, &free);
2388 for (; sva < eva; sva = pdnxt) {
2392 * Calculate index for next page table.
2394 pdnxt = (sva + NBPDR) & ~PDRMASK;
2397 if (pmap->pm_stats.resident_count == 0)
2400 pdirindex = sva >> PDRSHIFT;
2401 ptpaddr = pmap->pm_pdir[pdirindex];
2404 * Weed out invalid mappings. Note: we assume that the page
2405 * directory table is always allocated, and in kernel virtual.
2411 * Check for large page.
2413 if ((ptpaddr & PG_PS) != 0) {
2414 PD_CLEAR_VA(pmap, pdirindex, TRUE);
2415 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2421 * Limit our scan to either the end of the va represented
2422 * by the current page table page, or to the end of the
2423 * range being removed.
2428 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2430 if ((*pte & PG_V) == 0)
2434 * The TLB entry for a PG_G mapping is invalidated
2435 * by pmap_remove_pte().
2437 if ((*pte & PG_G) == 0)
2439 if (pmap_remove_pte(pmap, pte, sva, &free))
2445 PT_SET_VA_MA(PMAP1, 0, TRUE);
2448 pmap_invalidate_all(pmap);
2450 vm_page_unlock_queues();
2452 pmap_free_zero_pages(free);
2456 * Routine: pmap_remove_all
2458 * Removes this physical page from
2459 * all physical maps in which it resides.
2460 * Reflects back modify bits to the pager.
2463 * Original versions of this routine were very
2464 * inefficient because they iteratively called
2465 * pmap_remove (slow...)
2469 pmap_remove_all(vm_page_t m)
2473 pt_entry_t *pte, tpte;
2476 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2477 ("pmap_remove_all: page %p is not managed", m));
2479 vm_page_lock_queues();
2481 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2484 pmap->pm_stats.resident_count--;
2485 pte = pmap_pte_quick(pmap, pv->pv_va);
2487 PT_SET_VA_MA(pte, 0, TRUE);
2488 KASSERT(tpte != 0, ("pmap_remove_all: pmap %p va %x zero pte",
2491 pmap->pm_stats.wired_count--;
2493 vm_page_aflag_set(m, PGA_REFERENCED);
2496 * Update the vm_page_t clean and reference bits.
2498 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2500 pmap_unuse_pt(pmap, pv->pv_va, &free);
2501 pmap_invalidate_page(pmap, pv->pv_va);
2502 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2503 free_pv_entry(pmap, pv);
2506 vm_page_aflag_clear(m, PGA_WRITEABLE);
2509 PT_SET_MA(PADDR1, 0);
2511 vm_page_unlock_queues();
2512 pmap_free_zero_pages(free);
2516 * Set the physical protection on the
2517 * specified range of this map as requested.
2520 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2527 CTR4(KTR_PMAP, "pmap_protect: pmap=%p sva=0x%x eva=0x%x prot=0x%x",
2528 pmap, sva, eva, prot);
2530 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2531 pmap_remove(pmap, sva, eva);
2536 if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) ==
2537 (VM_PROT_WRITE|VM_PROT_EXECUTE))
2540 if (prot & VM_PROT_WRITE)
2546 vm_page_lock_queues();
2549 for (; sva < eva; sva = pdnxt) {
2550 pt_entry_t obits, pbits;
2553 pdnxt = (sva + NBPDR) & ~PDRMASK;
2557 pdirindex = sva >> PDRSHIFT;
2558 ptpaddr = pmap->pm_pdir[pdirindex];
2561 * Weed out invalid mappings. Note: we assume that the page
2562 * directory table is always allocated, and in kernel virtual.
2568 * Check for large page.
2570 if ((ptpaddr & PG_PS) != 0) {
2571 if ((prot & VM_PROT_WRITE) == 0)
2572 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
2574 if ((prot & VM_PROT_EXECUTE) == 0)
2575 pmap->pm_pdir[pdirindex] |= pg_nx;
2584 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2590 * Regardless of whether a pte is 32 or 64 bits in
2591 * size, PG_RW, PG_A, and PG_M are among the least
2592 * significant 32 bits.
2594 obits = pbits = *pte;
2595 if ((pbits & PG_V) == 0)
2598 if ((prot & VM_PROT_WRITE) == 0) {
2599 if ((pbits & (PG_MANAGED | PG_M | PG_RW)) ==
2600 (PG_MANAGED | PG_M | PG_RW)) {
2601 m = PHYS_TO_VM_PAGE(xpmap_mtop(pbits) &
2605 pbits &= ~(PG_RW | PG_M);
2608 if ((prot & VM_PROT_EXECUTE) == 0)
2612 if (pbits != obits) {
2614 PT_SET_VA_MA(pte, pbits, TRUE);
2618 pmap_invalidate_page(pmap, sva);
2626 PT_SET_VA_MA(PMAP1, 0, TRUE);
2628 pmap_invalidate_all(pmap);
2630 vm_page_unlock_queues();
2635 * Insert the given physical page (p) at
2636 * the specified virtual address (v) in the
2637 * target physical map with the protection requested.
2639 * If specified, the page will be wired down, meaning
2640 * that the related pte can not be reclaimed.
2642 * NB: This is the only routine which MAY NOT lazy-evaluate
2643 * or lose information. That is, this routine must actually
2644 * insert this page into the given map NOW.
2647 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
2648 vm_prot_t prot, boolean_t wired)
2652 pt_entry_t newpte, origpte;
2658 CTR6(KTR_PMAP, "pmap_enter: pmap=%08p va=0x%08x access=0x%x ma=0x%08x prot=0x%x wired=%d",
2659 pmap, va, access, VM_PAGE_TO_MACH(m), prot, wired);
2660 va = trunc_page(va);
2661 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
2662 KASSERT(va < UPT_MIN_ADDRESS || va >= UPT_MAX_ADDRESS,
2663 ("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)",
2665 KASSERT((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) != 0 ||
2666 VM_OBJECT_LOCKED(m->object),
2667 ("pmap_enter: page %p is not busy", m));
2671 vm_page_lock_queues();
2676 * In the case that a page table page is not
2677 * resident, we are creating it here.
2679 if (va < VM_MAXUSER_ADDRESS) {
2680 mpte = pmap_allocpte(pmap, va, M_WAITOK);
2683 pde = pmap_pde(pmap, va);
2684 if ((*pde & PG_PS) != 0)
2685 panic("pmap_enter: attempted pmap_enter on 4MB page");
2686 pte = pmap_pte_quick(pmap, va);
2689 * Page Directory table entry not valid, we need a new PT page
2692 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x",
2693 (uintmax_t)pmap->pm_pdir[va >> PDRSHIFT], va);
2696 pa = VM_PAGE_TO_PHYS(m);
2701 KASSERT((*pte & PG_V) || (*pte == 0), ("address set but not valid pte=%p *pte=0x%016jx",
2706 origpte = xpmap_mtop(origpte);
2707 opa = origpte & PG_FRAME;
2710 * Mapping has not changed, must be protection or wiring change.
2712 if (origpte && (opa == pa)) {
2714 * Wiring change, just update stats. We don't worry about
2715 * wiring PT pages as they remain resident as long as there
2716 * are valid mappings in them. Hence, if a user page is wired,
2717 * the PT page will be also.
2719 if (wired && ((origpte & PG_W) == 0))
2720 pmap->pm_stats.wired_count++;
2721 else if (!wired && (origpte & PG_W))
2722 pmap->pm_stats.wired_count--;
2725 * Remove extra pte reference
2730 if (origpte & PG_MANAGED) {
2740 * Mapping has changed, invalidate old range and fall through to
2741 * handle validating new mapping.
2745 pmap->pm_stats.wired_count--;
2746 if (origpte & PG_MANAGED) {
2747 om = PHYS_TO_VM_PAGE(opa);
2748 pv = pmap_pvh_remove(&om->md, pmap, va);
2749 } else if (va < VM_MAXUSER_ADDRESS)
2750 printf("va=0x%x is unmanaged :-( \n", va);
2754 KASSERT(mpte->wire_count > 0,
2755 ("pmap_enter: missing reference to page table page,"
2759 pmap->pm_stats.resident_count++;
2762 * Enter on the PV list if part of our managed memory.
2764 if ((m->oflags & VPO_UNMANAGED) == 0) {
2765 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
2766 ("pmap_enter: managed mapping within the clean submap"));
2768 pv = get_pv_entry(pmap, FALSE);
2770 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2772 } else if (pv != NULL)
2773 free_pv_entry(pmap, pv);
2776 * Increment counters
2779 pmap->pm_stats.wired_count++;
2783 * Now validate mapping with desired protection/wiring.
2785 newpte = (pt_entry_t)(pa | PG_V);
2786 if ((prot & VM_PROT_WRITE) != 0) {
2788 if ((newpte & PG_MANAGED) != 0)
2789 vm_page_aflag_set(m, PGA_WRITEABLE);
2792 if ((prot & VM_PROT_EXECUTE) == 0)
2797 if (va < VM_MAXUSER_ADDRESS)
2799 if (pmap == kernel_pmap)
2804 * if the mapping or permission bits are different, we need
2805 * to update the pte.
2807 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2811 PT_SET_VA(pte, newpte | PG_A, FALSE);
2812 if (origpte & PG_A) {
2813 if (origpte & PG_MANAGED)
2814 vm_page_aflag_set(om, PGA_REFERENCED);
2815 if (opa != VM_PAGE_TO_PHYS(m))
2818 if ((origpte & PG_NX) == 0 &&
2819 (newpte & PG_NX) != 0)
2823 if ((origpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
2824 if ((origpte & PG_MANAGED) != 0)
2826 if ((prot & VM_PROT_WRITE) == 0)
2829 if ((origpte & PG_MANAGED) != 0 &&
2830 TAILQ_EMPTY(&om->md.pv_list))
2831 vm_page_aflag_clear(om, PGA_WRITEABLE);
2833 pmap_invalidate_page(pmap, va);
2835 PT_SET_VA(pte, newpte | PG_A, FALSE);
2842 PT_SET_VA_MA(PMAP1, 0, TRUE);
2844 vm_page_unlock_queues();
2849 * Maps a sequence of resident pages belonging to the same object.
2850 * The sequence begins with the given page m_start. This page is
2851 * mapped at the given virtual address start. Each subsequent page is
2852 * mapped at a virtual address that is offset from start by the same
2853 * amount as the page is offset from m_start within the object. The
2854 * last page in the sequence is the page with the largest offset from
2855 * m_start that can be mapped at a virtual address less than the given
2856 * virtual address end. Not every virtual page between start and end
2857 * is mapped; only those for which a resident page exists with the
2858 * corresponding offset from m_start are mapped.
2861 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2862 vm_page_t m_start, vm_prot_t prot)
2865 vm_pindex_t diff, psize;
2866 multicall_entry_t mcl[16];
2867 multicall_entry_t *mclp = mcl;
2868 int error, count = 0;
2870 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
2871 psize = atop(end - start);
2874 vm_page_lock_queues();
2876 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2877 mpte = pmap_enter_quick_locked(&mclp, &count, pmap, start + ptoa(diff), m,
2879 m = TAILQ_NEXT(m, listq);
2881 error = HYPERVISOR_multicall(mcl, count);
2882 KASSERT(error == 0, ("bad multicall %d", error));
2888 error = HYPERVISOR_multicall(mcl, count);
2889 KASSERT(error == 0, ("bad multicall %d", error));
2891 vm_page_unlock_queues();
2896 * this code makes some *MAJOR* assumptions:
2897 * 1. Current pmap & pmap exists.
2900 * 4. No page table pages.
2901 * but is *MUCH* faster than pmap_enter...
2905 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
2907 multicall_entry_t mcl, *mclp;
2911 CTR4(KTR_PMAP, "pmap_enter_quick: pmap=%p va=0x%x m=%p prot=0x%x",
2914 vm_page_lock_queues();
2916 (void)pmap_enter_quick_locked(&mclp, &count, pmap, va, m, prot, NULL);
2918 HYPERVISOR_multicall(&mcl, count);
2919 vm_page_unlock_queues();
2925 pmap_enter_quick_range(pmap_t pmap, vm_offset_t *addrs, vm_page_t *pages, vm_prot_t *prots, int count)
2927 int i, error, index = 0;
2928 multicall_entry_t mcl[16];
2929 multicall_entry_t *mclp = mcl;
2932 for (i = 0; i < count; i++, addrs++, pages++, prots++) {
2933 if (!pmap_is_prefaultable_locked(pmap, *addrs))
2936 (void) pmap_enter_quick_locked(&mclp, &index, pmap, *addrs, *pages, *prots, NULL);
2938 error = HYPERVISOR_multicall(mcl, index);
2941 KASSERT(error == 0, ("bad multicall %d", error));
2945 error = HYPERVISOR_multicall(mcl, index);
2946 KASSERT(error == 0, ("bad multicall %d", error));
2954 pmap_enter_quick_locked(multicall_entry_t **mclpp, int *count, pmap_t pmap, vm_offset_t va, vm_page_t m,
2955 vm_prot_t prot, vm_page_t mpte)
2960 multicall_entry_t *mcl = *mclpp;
2962 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
2963 (m->oflags & VPO_UNMANAGED) != 0,
2964 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
2965 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2966 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2969 * In the case that a page table page is not
2970 * resident, we are creating it here.
2972 if (va < VM_MAXUSER_ADDRESS) {
2977 * Calculate pagetable page index
2979 ptepindex = va >> PDRSHIFT;
2980 if (mpte && (mpte->pindex == ptepindex)) {
2984 * Get the page directory entry
2986 ptema = pmap->pm_pdir[ptepindex];
2989 * If the page table page is mapped, we just increment
2990 * the hold count, and activate it.
2994 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2995 mpte = PHYS_TO_VM_PAGE(xpmap_mtop(ptema) & PG_FRAME);
2998 mpte = _pmap_allocpte(pmap, ptepindex,
3009 * This call to vtopte makes the assumption that we are
3010 * entering the page into the current pmap. In order to support
3011 * quick entry into any pmap, one would likely use pmap_pte_quick.
3012 * But that isn't as quick as vtopte.
3014 KASSERT(pmap_is_current(pmap), ("entering pages in non-current pmap"));
3025 * Enter on the PV list if part of our managed memory.
3027 if ((m->oflags & VPO_UNMANAGED) == 0 &&
3028 !pmap_try_insert_pv_entry(pmap, va, m)) {
3031 if (pmap_unwire_ptp(pmap, mpte, &free)) {
3032 pmap_invalidate_page(pmap, va);
3033 pmap_free_zero_pages(free);
3042 * Increment counters
3044 pmap->pm_stats.resident_count++;
3046 pa = VM_PAGE_TO_PHYS(m);
3048 if ((prot & VM_PROT_EXECUTE) == 0)
3054 * Now validate mapping with RO protection
3056 if ((m->oflags & VPO_UNMANAGED) != 0)
3057 pte_store(pte, pa | PG_V | PG_U);
3059 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
3062 * Now validate mapping with RO protection
3064 if ((m->oflags & VPO_UNMANAGED) != 0)
3065 pa = xpmap_ptom(pa | PG_V | PG_U);
3067 pa = xpmap_ptom(pa | PG_V | PG_U | PG_MANAGED);
3069 mcl->op = __HYPERVISOR_update_va_mapping;
3071 mcl->args[1] = (uint32_t)(pa & 0xffffffff);
3072 mcl->args[2] = (uint32_t)(pa >> 32);
3075 *count = *count + 1;
3081 * Make a temporary mapping for a physical address. This is only intended
3082 * to be used for panic dumps.
3085 pmap_kenter_temporary(vm_paddr_t pa, int i)
3088 vm_paddr_t ma = xpmap_ptom(pa);
3090 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
3091 PT_SET_MA(va, (ma & ~PAGE_MASK) | PG_V | pgeflag);
3093 return ((void *)crashdumpmap);
3097 * This code maps large physical mmap regions into the
3098 * processor address space. Note that some shortcuts
3099 * are taken, but the code works.
3102 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3103 vm_pindex_t pindex, vm_size_t size)
3106 vm_paddr_t pa, ptepa;
3110 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3111 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3112 ("pmap_object_init_pt: non-device object"));
3114 (addr & (NBPDR - 1)) == 0 && (size & (NBPDR - 1)) == 0) {
3115 if (!vm_object_populate(object, pindex, pindex + atop(size)))
3117 p = vm_page_lookup(object, pindex);
3118 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3119 ("pmap_object_init_pt: invalid page %p", p));
3120 pat_mode = p->md.pat_mode;
3123 * Abort the mapping if the first page is not physically
3124 * aligned to a 2/4MB page boundary.
3126 ptepa = VM_PAGE_TO_PHYS(p);
3127 if (ptepa & (NBPDR - 1))
3131 * Skip the first page. Abort the mapping if the rest of
3132 * the pages are not physically contiguous or have differing
3133 * memory attributes.
3135 p = TAILQ_NEXT(p, listq);
3136 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size;
3138 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3139 ("pmap_object_init_pt: invalid page %p", p));
3140 if (pa != VM_PAGE_TO_PHYS(p) ||
3141 pat_mode != p->md.pat_mode)
3143 p = TAILQ_NEXT(p, listq);
3147 * Map using 2/4MB pages. Since "ptepa" is 2/4M aligned and
3148 * "size" is a multiple of 2/4M, adding the PAT setting to
3149 * "pa" will not affect the termination of this loop.
3152 for (pa = ptepa | pmap_cache_bits(pat_mode, 1); pa < ptepa +
3153 size; pa += NBPDR) {
3154 pde = pmap_pde(pmap, addr);
3156 pde_store(pde, pa | PG_PS | PG_M | PG_A |
3157 PG_U | PG_RW | PG_V);
3158 pmap->pm_stats.resident_count += NBPDR /
3160 pmap_pde_mappings++;
3162 /* Else continue on if the PDE is already valid. */
3170 * Routine: pmap_change_wiring
3171 * Function: Change the wiring attribute for a map/virtual-address
3173 * In/out conditions:
3174 * The mapping must already exist in the pmap.
3177 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3181 vm_page_lock_queues();
3183 pte = pmap_pte(pmap, va);
3185 if (wired && !pmap_pte_w(pte)) {
3186 PT_SET_VA_MA((pte), *(pte) | PG_W, TRUE);
3187 pmap->pm_stats.wired_count++;
3188 } else if (!wired && pmap_pte_w(pte)) {
3189 PT_SET_VA_MA((pte), *(pte) & ~PG_W, TRUE);
3190 pmap->pm_stats.wired_count--;
3194 * Wiring is not a hardware characteristic so there is no need to
3197 pmap_pte_release(pte);
3199 vm_page_unlock_queues();
3205 * Copy the range specified by src_addr/len
3206 * from the source map to the range dst_addr/len
3207 * in the destination map.
3209 * This routine is only advisory and need not do anything.
3213 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
3214 vm_offset_t src_addr)
3218 vm_offset_t end_addr = src_addr + len;
3221 if (dst_addr != src_addr)
3224 if (!pmap_is_current(src_pmap)) {
3226 "pmap_copy, skipping: pdir[PTDPTDI]=0x%jx PTDpde[0]=0x%jx",
3227 (src_pmap->pm_pdir[PTDPTDI] & PG_FRAME), (PTDpde[0] & PG_FRAME));
3231 CTR5(KTR_PMAP, "pmap_copy: dst_pmap=%p src_pmap=%p dst_addr=0x%x len=%d src_addr=0x%x",
3232 dst_pmap, src_pmap, dst_addr, len, src_addr);
3234 #ifdef HAMFISTED_LOCKING
3235 mtx_lock(&createdelete_lock);
3238 vm_page_lock_queues();
3239 if (dst_pmap < src_pmap) {
3240 PMAP_LOCK(dst_pmap);
3241 PMAP_LOCK(src_pmap);
3243 PMAP_LOCK(src_pmap);
3244 PMAP_LOCK(dst_pmap);
3247 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3248 pt_entry_t *src_pte, *dst_pte;
3249 vm_page_t dstmpte, srcmpte;
3250 pd_entry_t srcptepaddr;
3253 KASSERT(addr < UPT_MIN_ADDRESS,
3254 ("pmap_copy: invalid to pmap_copy page tables"));
3256 pdnxt = (addr + NBPDR) & ~PDRMASK;
3259 ptepindex = addr >> PDRSHIFT;
3261 srcptepaddr = PT_GET(&src_pmap->pm_pdir[ptepindex]);
3262 if (srcptepaddr == 0)
3265 if (srcptepaddr & PG_PS) {
3266 if (dst_pmap->pm_pdir[ptepindex] == 0) {
3267 PD_SET_VA(dst_pmap, ptepindex, srcptepaddr & ~PG_W, TRUE);
3268 dst_pmap->pm_stats.resident_count +=
3274 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr & PG_FRAME);
3275 KASSERT(srcmpte->wire_count > 0,
3276 ("pmap_copy: source page table page is unused"));
3278 if (pdnxt > end_addr)
3281 src_pte = vtopte(addr);
3282 while (addr < pdnxt) {
3286 * we only virtual copy managed pages
3288 if ((ptetemp & PG_MANAGED) != 0) {
3289 dstmpte = pmap_allocpte(dst_pmap, addr,
3291 if (dstmpte == NULL)
3293 dst_pte = pmap_pte_quick(dst_pmap, addr);
3294 if (*dst_pte == 0 &&
3295 pmap_try_insert_pv_entry(dst_pmap, addr,
3296 PHYS_TO_VM_PAGE(xpmap_mtop(ptetemp) & PG_FRAME))) {
3298 * Clear the wired, modified, and
3299 * accessed (referenced) bits
3302 KASSERT(ptetemp != 0, ("src_pte not set"));
3303 PT_SET_VA_MA(dst_pte, ptetemp & ~(PG_W | PG_M | PG_A), TRUE /* XXX debug */);
3304 KASSERT(*dst_pte == (ptetemp & ~(PG_W | PG_M | PG_A)),
3305 ("no pmap copy expected: 0x%jx saw: 0x%jx",
3306 ptetemp & ~(PG_W | PG_M | PG_A), *dst_pte));
3307 dst_pmap->pm_stats.resident_count++;
3310 if (pmap_unwire_ptp(dst_pmap, dstmpte,
3312 pmap_invalidate_page(dst_pmap,
3314 pmap_free_zero_pages(free);
3318 if (dstmpte->wire_count >= srcmpte->wire_count)
3328 vm_page_unlock_queues();
3329 PMAP_UNLOCK(src_pmap);
3330 PMAP_UNLOCK(dst_pmap);
3332 #ifdef HAMFISTED_LOCKING
3333 mtx_unlock(&createdelete_lock);
3337 static __inline void
3338 pagezero(void *page)
3340 #if defined(I686_CPU)
3341 if (cpu_class == CPUCLASS_686) {
3342 #if defined(CPU_ENABLE_SSE)
3343 if (cpu_feature & CPUID_SSE2)
3344 sse2_pagezero(page);
3347 i686_pagezero(page);
3350 bzero(page, PAGE_SIZE);
3354 * pmap_zero_page zeros the specified hardware page by mapping
3355 * the page into KVM and using bzero to clear its contents.
3358 pmap_zero_page(vm_page_t m)
3360 struct sysmaps *sysmaps;
3362 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3363 mtx_lock(&sysmaps->lock);
3364 if (*sysmaps->CMAP2)
3365 panic("pmap_zero_page: CMAP2 busy");
3367 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | VM_PAGE_TO_MACH(m) | PG_A | PG_M);
3368 pagezero(sysmaps->CADDR2);
3369 PT_SET_MA(sysmaps->CADDR2, 0);
3371 mtx_unlock(&sysmaps->lock);
3375 * pmap_zero_page_area zeros the specified hardware page by mapping
3376 * the page into KVM and using bzero to clear its contents.
3378 * off and size may not cover an area beyond a single hardware page.
3381 pmap_zero_page_area(vm_page_t m, int off, int size)
3383 struct sysmaps *sysmaps;
3385 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3386 mtx_lock(&sysmaps->lock);
3387 if (*sysmaps->CMAP2)
3388 panic("pmap_zero_page_area: CMAP2 busy");
3390 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | VM_PAGE_TO_MACH(m) | PG_A | PG_M);
3392 if (off == 0 && size == PAGE_SIZE)
3393 pagezero(sysmaps->CADDR2);
3395 bzero((char *)sysmaps->CADDR2 + off, size);
3396 PT_SET_MA(sysmaps->CADDR2, 0);
3398 mtx_unlock(&sysmaps->lock);
3402 * pmap_zero_page_idle zeros the specified hardware page by mapping
3403 * the page into KVM and using bzero to clear its contents. This
3404 * is intended to be called from the vm_pagezero process only and
3408 pmap_zero_page_idle(vm_page_t m)
3412 panic("pmap_zero_page_idle: CMAP3 busy");
3414 PT_SET_MA(CADDR3, PG_V | PG_RW | VM_PAGE_TO_MACH(m) | PG_A | PG_M);
3416 PT_SET_MA(CADDR3, 0);
3421 * pmap_copy_page copies the specified (machine independent)
3422 * page by mapping the page into virtual memory and using
3423 * bcopy to copy the page, one machine dependent page at a
3427 pmap_copy_page(vm_page_t src, vm_page_t dst)
3429 struct sysmaps *sysmaps;
3431 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3432 mtx_lock(&sysmaps->lock);
3433 if (*sysmaps->CMAP1)
3434 panic("pmap_copy_page: CMAP1 busy");
3435 if (*sysmaps->CMAP2)
3436 panic("pmap_copy_page: CMAP2 busy");
3438 PT_SET_MA(sysmaps->CADDR1, PG_V | VM_PAGE_TO_MACH(src) | PG_A);
3439 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | VM_PAGE_TO_MACH(dst) | PG_A | PG_M);
3440 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
3441 PT_SET_MA(sysmaps->CADDR1, 0);
3442 PT_SET_MA(sysmaps->CADDR2, 0);
3444 mtx_unlock(&sysmaps->lock);
3447 int unmapped_buf_allowed = 1;
3450 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
3451 vm_offset_t b_offset, int xfersize)
3453 struct sysmaps *sysmaps;
3454 vm_page_t a_pg, b_pg;
3456 vm_offset_t a_pg_offset, b_pg_offset;
3459 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3460 mtx_lock(&sysmaps->lock);
3461 if (*sysmaps->CMAP1 != 0)
3462 panic("pmap_copy_pages: CMAP1 busy");
3463 if (*sysmaps->CMAP2 != 0)
3464 panic("pmap_copy_pages: CMAP2 busy");
3466 while (xfersize > 0) {
3467 a_pg = ma[a_offset >> PAGE_SHIFT];
3468 a_pg_offset = a_offset & PAGE_MASK;
3469 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
3470 b_pg = mb[b_offset >> PAGE_SHIFT];
3471 b_pg_offset = b_offset & PAGE_MASK;
3472 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
3473 PT_SET_MA(sysmaps->CADDR1, PG_V | VM_PAGE_TO_MACH(a_pg) | PG_A);
3474 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW |
3475 VM_PAGE_TO_MACH(b_pg) | PG_A | PG_M);
3476 a_cp = sysmaps->CADDR1 + a_pg_offset;
3477 b_cp = sysmaps->CADDR2 + b_pg_offset;
3478 bcopy(a_cp, b_cp, cnt);
3483 PT_SET_MA(sysmaps->CADDR1, 0);
3484 PT_SET_MA(sysmaps->CADDR2, 0);
3486 mtx_unlock(&sysmaps->lock);
3490 * Returns true if the pmap's pv is one of the first
3491 * 16 pvs linked to from this page. This count may
3492 * be changed upwards or downwards in the future; it
3493 * is only necessary that true be returned for a small
3494 * subset of pmaps for proper page aging.
3497 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3503 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3504 ("pmap_page_exists_quick: page %p is not managed", m));
3506 vm_page_lock_queues();
3507 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3508 if (PV_PMAP(pv) == pmap) {
3516 vm_page_unlock_queues();
3521 * pmap_page_wired_mappings:
3523 * Return the number of managed mappings to the given physical page
3527 pmap_page_wired_mappings(vm_page_t m)
3535 if ((m->oflags & VPO_UNMANAGED) != 0)
3537 vm_page_lock_queues();
3539 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3542 pte = pmap_pte_quick(pmap, pv->pv_va);
3543 if ((*pte & PG_W) != 0)
3548 vm_page_unlock_queues();
3553 * Returns TRUE if the given page is mapped. Otherwise, returns FALSE.
3556 pmap_page_is_mapped(vm_page_t m)
3559 if ((m->oflags & VPO_UNMANAGED) != 0)
3561 return (!TAILQ_EMPTY(&m->md.pv_list));
3565 * Remove all pages from specified address space
3566 * this aids process exit speeds. Also, this code
3567 * is special cased for current process only, but
3568 * can have the more generic (and slightly slower)
3569 * mode enabled. This is much faster than pmap_remove
3570 * in the case of running down an entire address space.
3573 pmap_remove_pages(pmap_t pmap)
3575 pt_entry_t *pte, tpte;
3576 vm_page_t m, free = NULL;
3578 struct pv_chunk *pc, *npc;
3581 uint32_t inuse, bitmask;
3584 CTR1(KTR_PMAP, "pmap_remove_pages: pmap=%p", pmap);
3586 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
3587 printf("warning: pmap_remove_pages called with non-current pmap\n");
3590 vm_page_lock_queues();
3591 KASSERT(pmap_is_current(pmap), ("removing pages from non-current pmap"));
3594 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
3595 KASSERT(pc->pc_pmap == pmap, ("Wrong pmap %p %p", pmap,
3598 for (field = 0; field < _NPCM; field++) {
3599 inuse = ~pc->pc_map[field] & pc_freemask[field];
3600 while (inuse != 0) {
3602 bitmask = 1UL << bit;
3603 idx = field * 32 + bit;
3604 pv = &pc->pc_pventry[idx];
3607 pte = vtopte(pv->pv_va);
3608 tpte = *pte ? xpmap_mtop(*pte) : 0;
3612 "TPTE at %p IS ZERO @ VA %08x\n",
3618 * We cannot remove wired pages from a process' mapping at this time
3625 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3626 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3627 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
3628 m, (uintmax_t)m->phys_addr,
3631 KASSERT(m < &vm_page_array[vm_page_array_size],
3632 ("pmap_remove_pages: bad tpte %#jx",
3636 PT_CLEAR_VA(pte, FALSE);
3639 * Update the vm_page_t clean/reference bits.
3644 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3645 if (TAILQ_EMPTY(&m->md.pv_list))
3646 vm_page_aflag_clear(m, PGA_WRITEABLE);
3648 pmap_unuse_pt(pmap, pv->pv_va, &free);
3651 PV_STAT(pv_entry_frees++);
3652 PV_STAT(pv_entry_spare++);
3654 pc->pc_map[field] |= bitmask;
3655 pmap->pm_stats.resident_count--;
3660 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3666 PT_SET_MA(PADDR1, 0);
3669 pmap_invalidate_all(pmap);
3670 vm_page_unlock_queues();
3672 pmap_free_zero_pages(free);
3678 * Return whether or not the specified physical page was modified
3679 * in any physical maps.
3682 pmap_is_modified(vm_page_t m)
3689 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3690 ("pmap_is_modified: page %p is not managed", m));
3694 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be
3695 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
3696 * is clear, no PTEs can have PG_M set.
3698 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
3699 if ((m->oflags & VPO_BUSY) == 0 &&
3700 (m->aflags & PGA_WRITEABLE) == 0)
3702 vm_page_lock_queues();
3704 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3707 pte = pmap_pte_quick(pmap, pv->pv_va);
3708 rv = (*pte & PG_M) != 0;
3714 PT_SET_MA(PADDR1, 0);
3716 vm_page_unlock_queues();
3721 * pmap_is_prefaultable:
3723 * Return whether or not the specified virtual address is elgible
3727 pmap_is_prefaultable_locked(pmap_t pmap, vm_offset_t addr)
3730 boolean_t rv = FALSE;
3734 if (pmap_is_current(pmap) && *pmap_pde(pmap, addr)) {
3742 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3747 rv = pmap_is_prefaultable_locked(pmap, addr);
3753 pmap_is_referenced(vm_page_t m)
3760 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3761 ("pmap_is_referenced: page %p is not managed", m));
3763 vm_page_lock_queues();
3765 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3768 pte = pmap_pte_quick(pmap, pv->pv_va);
3769 rv = (*pte & (PG_A | PG_V)) == (PG_A | PG_V);
3775 PT_SET_MA(PADDR1, 0);
3777 vm_page_unlock_queues();
3782 pmap_map_readonly(pmap_t pmap, vm_offset_t va, int len)
3784 int i, npages = round_page(len) >> PAGE_SHIFT;
3785 for (i = 0; i < npages; i++) {
3787 pte = pmap_pte(pmap, (vm_offset_t)(va + i*PAGE_SIZE));
3788 vm_page_lock_queues();
3789 pte_store(pte, xpmap_mtop(*pte & ~(PG_RW|PG_M)));
3790 vm_page_unlock_queues();
3791 PMAP_MARK_PRIV(xpmap_mtop(*pte));
3792 pmap_pte_release(pte);
3797 pmap_map_readwrite(pmap_t pmap, vm_offset_t va, int len)
3799 int i, npages = round_page(len) >> PAGE_SHIFT;
3800 for (i = 0; i < npages; i++) {
3802 pte = pmap_pte(pmap, (vm_offset_t)(va + i*PAGE_SIZE));
3803 PMAP_MARK_UNPRIV(xpmap_mtop(*pte));
3804 vm_page_lock_queues();
3805 pte_store(pte, xpmap_mtop(*pte) | (PG_RW|PG_M));
3806 vm_page_unlock_queues();
3807 pmap_pte_release(pte);
3812 * Clear the write and modified bits in each of the given page's mappings.
3815 pmap_remove_write(vm_page_t m)
3819 pt_entry_t oldpte, *pte;
3821 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3822 ("pmap_remove_write: page %p is not managed", m));
3825 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by
3826 * another thread while the object is locked. Thus, if PGA_WRITEABLE
3827 * is clear, no page table entries need updating.
3829 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
3830 if ((m->oflags & VPO_BUSY) == 0 &&
3831 (m->aflags & PGA_WRITEABLE) == 0)
3833 vm_page_lock_queues();
3835 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3838 pte = pmap_pte_quick(pmap, pv->pv_va);
3841 if ((oldpte & PG_RW) != 0) {
3842 vm_paddr_t newpte = oldpte & ~(PG_RW | PG_M);
3845 * Regardless of whether a pte is 32 or 64 bits
3846 * in size, PG_RW and PG_M are among the least
3847 * significant 32 bits.
3849 PT_SET_VA_MA(pte, newpte, TRUE);
3853 if ((oldpte & PG_M) != 0)
3855 pmap_invalidate_page(pmap, pv->pv_va);
3859 vm_page_aflag_clear(m, PGA_WRITEABLE);
3862 PT_SET_MA(PADDR1, 0);
3864 vm_page_unlock_queues();
3868 * pmap_ts_referenced:
3870 * Return a count of reference bits for a page, clearing those bits.
3871 * It is not necessary for every reference bit to be cleared, but it
3872 * is necessary that 0 only be returned when there are truly no
3873 * reference bits set.
3875 * XXX: The exact number of bits to check and clear is a matter that
3876 * should be tested and standardized at some point in the future for
3877 * optimal aging of shared pages.
3880 pmap_ts_referenced(vm_page_t m)
3882 pv_entry_t pv, pvf, pvn;
3887 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3888 ("pmap_ts_referenced: page %p is not managed", m));
3889 vm_page_lock_queues();
3891 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3894 pvn = TAILQ_NEXT(pv, pv_list);
3895 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3896 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3899 pte = pmap_pte_quick(pmap, pv->pv_va);
3900 if ((*pte & PG_A) != 0) {
3901 PT_SET_VA_MA(pte, *pte & ~PG_A, FALSE);
3902 pmap_invalidate_page(pmap, pv->pv_va);
3908 } while ((pv = pvn) != NULL && pv != pvf);
3912 PT_SET_MA(PADDR1, 0);
3914 vm_page_unlock_queues();
3919 * Clear the modify bits on the specified physical page.
3922 pmap_clear_modify(vm_page_t m)
3928 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3929 ("pmap_clear_modify: page %p is not managed", m));
3930 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
3931 KASSERT((m->oflags & VPO_BUSY) == 0,
3932 ("pmap_clear_modify: page %p is busy", m));
3935 * If the page is not PGA_WRITEABLE, then no PTEs can have PG_M set.
3936 * If the object containing the page is locked and the page is not
3937 * VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set.
3939 if ((m->aflags & PGA_WRITEABLE) == 0)
3941 vm_page_lock_queues();
3943 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3946 pte = pmap_pte_quick(pmap, pv->pv_va);
3947 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
3949 * Regardless of whether a pte is 32 or 64 bits
3950 * in size, PG_M is among the least significant
3953 PT_SET_VA_MA(pte, *pte & ~PG_M, FALSE);
3954 pmap_invalidate_page(pmap, pv->pv_va);
3959 vm_page_unlock_queues();
3963 * pmap_clear_reference:
3965 * Clear the reference bit on the specified physical page.
3968 pmap_clear_reference(vm_page_t m)
3974 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3975 ("pmap_clear_reference: page %p is not managed", m));
3976 vm_page_lock_queues();
3978 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3981 pte = pmap_pte_quick(pmap, pv->pv_va);
3982 if ((*pte & PG_A) != 0) {
3984 * Regardless of whether a pte is 32 or 64 bits
3985 * in size, PG_A is among the least significant
3988 PT_SET_VA_MA(pte, *pte & ~PG_A, FALSE);
3989 pmap_invalidate_page(pmap, pv->pv_va);
3994 vm_page_unlock_queues();
3998 * Miscellaneous support routines follow
4002 * Map a set of physical memory pages into the kernel virtual
4003 * address space. Return a pointer to where it is mapped. This
4004 * routine is intended to be used for mapping device memory,
4008 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
4010 vm_offset_t va, offset;
4013 offset = pa & PAGE_MASK;
4014 size = roundup(offset + size, PAGE_SIZE);
4017 if (pa < KERNLOAD && pa + size <= KERNLOAD)
4020 va = kmem_alloc_nofault(kernel_map, size);
4022 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4024 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
4025 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
4026 pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
4027 pmap_invalidate_cache_range(va, va + size);
4028 return ((void *)(va + offset));
4032 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
4035 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
4039 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
4042 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
4046 pmap_unmapdev(vm_offset_t va, vm_size_t size)
4048 vm_offset_t base, offset;
4050 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
4052 base = trunc_page(va);
4053 offset = va & PAGE_MASK;
4054 size = roundup(offset + size, PAGE_SIZE);
4055 kmem_free(kernel_map, base, size);
4059 * Sets the memory attribute for the specified page.
4062 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
4065 m->md.pat_mode = ma;
4066 if ((m->flags & PG_FICTITIOUS) != 0)
4070 * If "m" is a normal page, flush it from the cache.
4071 * See pmap_invalidate_cache_range().
4073 * First, try to find an existing mapping of the page by sf
4074 * buffer. sf_buf_invalidate_cache() modifies mapping and
4075 * flushes the cache.
4077 if (sf_buf_invalidate_cache(m))
4081 * If page is not mapped by sf buffer, but CPU does not
4082 * support self snoop, map the page transient and do
4083 * invalidation. In the worst case, whole cache is flushed by
4084 * pmap_invalidate_cache_range().
4086 if ((cpu_feature & CPUID_SS) == 0)
4091 pmap_flush_page(vm_page_t m)
4093 struct sysmaps *sysmaps;
4094 vm_offset_t sva, eva;
4096 if ((cpu_feature & CPUID_CLFSH) != 0) {
4097 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4098 mtx_lock(&sysmaps->lock);
4099 if (*sysmaps->CMAP2)
4100 panic("pmap_flush_page: CMAP2 busy");
4102 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW |
4103 VM_PAGE_TO_MACH(m) | PG_A | PG_M |
4104 pmap_cache_bits(m->md.pat_mode, 0));
4105 invlcaddr(sysmaps->CADDR2);
4106 sva = (vm_offset_t)sysmaps->CADDR2;
4107 eva = sva + PAGE_SIZE;
4110 * Use mfence despite the ordering implied by
4111 * mtx_{un,}lock() because clflush is not guaranteed
4112 * to be ordered by any other instruction.
4115 for (; sva < eva; sva += cpu_clflush_line_size)
4118 PT_SET_MA(sysmaps->CADDR2, 0);
4120 mtx_unlock(&sysmaps->lock);
4122 pmap_invalidate_cache();
4126 * Changes the specified virtual address range's memory type to that given by
4127 * the parameter "mode". The specified virtual address range must be
4128 * completely contained within either the kernel map.
4130 * Returns zero if the change completed successfully, and either EINVAL or
4131 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
4132 * of the virtual address range was not mapped, and ENOMEM is returned if
4133 * there was insufficient memory available to complete the change.
4136 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
4138 vm_offset_t base, offset, tmpva;
4144 base = trunc_page(va);
4145 offset = va & PAGE_MASK;
4146 size = roundup(offset + size, PAGE_SIZE);
4148 /* Only supported on kernel virtual addresses. */
4149 if (base <= VM_MAXUSER_ADDRESS)
4152 /* 4MB pages and pages that aren't mapped aren't supported. */
4153 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) {
4154 pde = pmap_pde(kernel_pmap, tmpva);
4157 if ((*pde & PG_V) == 0)
4160 if ((*pte & PG_V) == 0)
4167 * Ok, all the pages exist and are 4k, so run through them updating
4170 for (tmpva = base; size > 0; ) {
4171 pte = vtopte(tmpva);
4174 * The cache mode bits are all in the low 32-bits of the
4175 * PTE, so we can just spin on updating the low 32-bits.
4178 opte = *(u_int *)pte;
4179 npte = opte & ~(PG_PTE_PAT | PG_NC_PCD | PG_NC_PWT);
4180 npte |= pmap_cache_bits(mode, 0);
4181 PT_SET_VA_MA(pte, npte, TRUE);
4182 } while (npte != opte && (*pte != npte));
4190 * Flush CPU caches to make sure any data isn't cached that
4191 * shouldn't be, etc.
4194 pmap_invalidate_range(kernel_pmap, base, tmpva);
4195 pmap_invalidate_cache_range(base, tmpva);
4201 * perform the pmap work for mincore
4204 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
4206 pt_entry_t *ptep, pte;
4212 ptep = pmap_pte(pmap, addr);
4213 pte = (ptep != NULL) ? PT_GET(ptep) : 0;
4214 pmap_pte_release(ptep);
4216 if ((pte & PG_V) != 0) {
4217 val |= MINCORE_INCORE;
4218 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
4219 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
4220 if ((pte & PG_A) != 0)
4221 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
4223 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
4224 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
4225 (pte & (PG_MANAGED | PG_V)) == (PG_MANAGED | PG_V)) {
4226 pa = pte & PG_FRAME;
4227 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
4228 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
4231 PA_UNLOCK_COND(*locked_pa);
4237 pmap_activate(struct thread *td)
4239 pmap_t pmap, oldpmap;
4244 pmap = vmspace_pmap(td->td_proc->p_vmspace);
4245 oldpmap = PCPU_GET(curpmap);
4246 cpuid = PCPU_GET(cpuid);
4248 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
4249 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
4251 CPU_CLR(cpuid, &oldpmap->pm_active);
4252 CPU_SET(cpuid, &pmap->pm_active);
4255 cr3 = vtophys(pmap->pm_pdpt);
4257 cr3 = vtophys(pmap->pm_pdir);
4260 * pmap_activate is for the current thread on the current cpu
4262 td->td_pcb->pcb_cr3 = cr3;
4265 PCPU_SET(curpmap, pmap);
4270 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
4275 * Increase the starting virtual address of the given mapping if a
4276 * different alignment might result in more superpage mappings.
4279 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4280 vm_offset_t *addr, vm_size_t size)
4282 vm_offset_t superpage_offset;
4286 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
4287 offset += ptoa(object->pg_color);
4288 superpage_offset = offset & PDRMASK;
4289 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
4290 (*addr & PDRMASK) == superpage_offset)
4292 if ((*addr & PDRMASK) < superpage_offset)
4293 *addr = (*addr & ~PDRMASK) + superpage_offset;
4295 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
4302 int i, pdir, offset;
4307 * We need to remove the recursive mapping structure from all
4308 * our pmaps so that Xen doesn't get confused when it restores
4309 * the page tables. The recursive map lives at page directory
4310 * index PTDPTDI. We assume that the suspend code has stopped
4311 * the other vcpus (if any).
4313 LIST_FOREACH(pmap, &allpmaps, pm_list) {
4314 for (i = 0; i < 4; i++) {
4316 * Figure out which page directory (L2) page
4317 * contains this bit of the recursive map and
4318 * the offset within that page of the map
4321 pdir = (PTDPTDI + i) / NPDEPG;
4322 offset = (PTDPTDI + i) % NPDEPG;
4323 pdirma = pmap->pm_pdpt[pdir] & PG_FRAME;
4324 mu[i].ptr = pdirma + offset * sizeof(pd_entry_t);
4327 HYPERVISOR_mmu_update(mu, 4, NULL, DOMID_SELF);
4335 int i, pdir, offset;
4340 * Restore the recursive map that we removed on suspend.
4342 LIST_FOREACH(pmap, &allpmaps, pm_list) {
4343 for (i = 0; i < 4; i++) {
4345 * Figure out which page directory (L2) page
4346 * contains this bit of the recursive map and
4347 * the offset within that page of the map
4350 pdir = (PTDPTDI + i) / NPDEPG;
4351 offset = (PTDPTDI + i) % NPDEPG;
4352 pdirma = pmap->pm_pdpt[pdir] & PG_FRAME;
4353 mu[i].ptr = pdirma + offset * sizeof(pd_entry_t);
4354 mu[i].val = (pmap->pm_pdpt[i] & PG_FRAME) | PG_V;
4356 HYPERVISOR_mmu_update(mu, 4, NULL, DOMID_SELF);
4360 #if defined(PMAP_DEBUG)
4361 pmap_pid_dump(int pid)
4368 sx_slock(&allproc_lock);
4369 FOREACH_PROC_IN_SYSTEM(p) {
4370 if (p->p_pid != pid)
4376 pmap = vmspace_pmap(p->p_vmspace);
4377 for (i = 0; i < NPDEPTD; i++) {
4380 vm_offset_t base = i << PDRSHIFT;
4382 pde = &pmap->pm_pdir[i];
4383 if (pde && pmap_pde_v(pde)) {
4384 for (j = 0; j < NPTEPG; j++) {
4385 vm_offset_t va = base + (j << PAGE_SHIFT);
4386 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
4391 sx_sunlock(&allproc_lock);
4394 pte = pmap_pte(pmap, va);
4395 if (pte && pmap_pte_v(pte)) {
4399 m = PHYS_TO_VM_PAGE(pa & PG_FRAME);
4400 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
4401 va, pa, m->hold_count, m->wire_count, m->flags);
4416 sx_sunlock(&allproc_lock);
4423 static void pads(pmap_t pm);
4424 void pmap_pvdump(vm_paddr_t pa);
4426 /* print address space of pmap*/
4434 if (pm == kernel_pmap)
4436 for (i = 0; i < NPDEPTD; i++)
4438 for (j = 0; j < NPTEPG; j++) {
4439 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
4440 if (pm == kernel_pmap && va < KERNBASE)
4442 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
4444 ptep = pmap_pte(pm, va);
4445 if (pmap_pte_v(ptep))
4446 printf("%x:%x ", va, *ptep);
4452 pmap_pvdump(vm_paddr_t pa)
4458 printf("pa %x", pa);
4459 m = PHYS_TO_VM_PAGE(pa);
4460 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4462 printf(" -> pmap %p, va %x", (void *)pmap, pv->pv_va);