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 * Since the information managed by this module is
84 * also stored by the logical address mapping module,
85 * this module may throw away valid virtual-to-physical
86 * mappings at almost any time. However, invalidations
87 * of virtual-to-physical mappings must be done as
90 * In order to cope with hardware architectures which
91 * make virtual-to-physical map invalidates expensive,
92 * this module may delay invalidate or reduced protection
93 * operations until such time as they are actually
94 * necessary. This module is given full information as
95 * to which processors are currently using which maps,
96 * and to when physical maps must be made correct.
100 #include "opt_pmap.h"
102 #include "opt_xbox.h"
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/kernel.h>
108 #include <sys/lock.h>
109 #include <sys/malloc.h>
110 #include <sys/mman.h>
111 #include <sys/msgbuf.h>
112 #include <sys/mutex.h>
113 #include <sys/proc.h>
114 #include <sys/rwlock.h>
115 #include <sys/sf_buf.h>
117 #include <sys/vmmeter.h>
118 #include <sys/sched.h>
119 #include <sys/sysctl.h>
123 #include <sys/cpuset.h>
127 #include <vm/vm_param.h>
128 #include <vm/vm_kern.h>
129 #include <vm/vm_page.h>
130 #include <vm/vm_map.h>
131 #include <vm/vm_object.h>
132 #include <vm/vm_extern.h>
133 #include <vm/vm_pageout.h>
134 #include <vm/vm_pager.h>
137 #include <machine/cpu.h>
138 #include <machine/cputypes.h>
139 #include <machine/md_var.h>
140 #include <machine/pcb.h>
141 #include <machine/specialreg.h>
143 #include <machine/smp.h>
147 #include <machine/xbox.h>
150 #include <xen/interface/xen.h>
151 #include <xen/hypervisor.h>
152 #include <machine/xen/hypercall.h>
153 #include <machine/xen/xenvar.h>
154 #include <machine/xen/xenfunc.h>
156 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
157 #define CPU_ENABLE_SSE
160 #ifndef PMAP_SHPGPERPROC
161 #define PMAP_SHPGPERPROC 200
166 #if !defined(DIAGNOSTIC)
167 #ifdef __GNUC_GNU_INLINE__
168 #define PMAP_INLINE __attribute__((__gnu_inline__)) inline
170 #define PMAP_INLINE extern inline
177 #define PV_STAT(x) do { x ; } while (0)
179 #define PV_STAT(x) do { } while (0)
183 * Get PDEs and PTEs for user/kernel address space
185 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
186 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
188 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
189 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
190 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
191 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
192 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
194 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
196 #define HAMFISTED_LOCKING
197 #ifdef HAMFISTED_LOCKING
198 static struct mtx createdelete_lock;
201 struct pmap kernel_pmap_store;
202 LIST_HEAD(pmaplist, pmap);
203 static struct pmaplist allpmaps;
204 static struct mtx allpmaps_lock;
206 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
207 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
208 int pgeflag = 0; /* PG_G or-in */
209 int pseflag = 0; /* PG_PS or-in */
212 vm_offset_t kernel_vm_end;
213 extern u_int32_t KERNend;
219 static SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
221 static int pat_works; /* Is page attribute table sane? */
224 * This lock is defined as static in other pmap implementations. It cannot,
225 * however, be defined as static here, because it is (ab)used to serialize
226 * queued page table changes in other sources files.
228 struct rwlock pvh_global_lock;
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];
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, u_int flags);
303 static vm_page_t _pmap_allocpte(pmap_t pmap, u_int ptepindex, u_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);
393 * Initialize the global pv list lock.
395 rw_init_flags(&pvh_global_lock, "pmap pv global", RW_RECURSE);
397 LIST_INIT(&allpmaps);
398 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
399 mtx_lock_spin(&allpmaps_lock);
400 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
401 mtx_unlock_spin(&allpmaps_lock);
406 * Reserve some special page table entries/VA space for temporary
409 #define SYSMAP(c, p, v, n) \
410 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
416 * CMAP1/CMAP2 are used for zeroing and copying pages.
417 * CMAP3 is used for the idle process page zeroing.
419 for (i = 0; i < MAXCPU; i++) {
420 sysmaps = &sysmaps_pcpu[i];
421 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
422 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
423 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
424 PT_SET_MA(sysmaps->CADDR1, 0);
425 PT_SET_MA(sysmaps->CADDR2, 0);
427 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
428 PT_SET_MA(CADDR3, 0);
433 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
436 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
438 SYSMAP(caddr_t, unused, ptvmmap, 1)
441 * msgbufp is used to map the system message buffer.
443 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(msgbufsize)))
446 * PADDR1 and PADDR2 are used by pmap_pte_quick() and pmap_pte(),
449 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1)
450 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1)
452 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
457 * Leave in place an identity mapping (virt == phys) for the low 1 MB
458 * physical memory region that is used by the ACPI wakeup code. This
459 * mapping must not have PG_G set.
463 * leave here deliberately to show that this is not supported
466 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
467 * an early stadium, we cannot yet neatly map video memory ... :-(
468 * Better fixes are very welcome! */
469 if (!arch_i386_is_xbox)
471 for (i = 1; i < NKPT; i++)
474 /* Initialize the PAT MSR if present. */
477 /* Turn on PG_G on kernel page(s) */
481 #ifdef HAMFISTED_LOCKING
482 mtx_init(&createdelete_lock, "pmap create/delete", NULL, MTX_DEF);
494 /* Bail if this CPU doesn't implement PAT. */
495 if (!(cpu_feature & CPUID_PAT))
498 if (cpu_vendor_id != CPU_VENDOR_INTEL ||
499 (CPUID_TO_FAMILY(cpu_id) == 6 && CPUID_TO_MODEL(cpu_id) >= 0xe)) {
501 * Leave the indices 0-3 at the default of WB, WT, UC, and UC-.
502 * Program 4 and 5 as WP and WC.
503 * Leave 6 and 7 as UC and UC-.
505 pat_msr = rdmsr(MSR_PAT);
506 pat_msr &= ~(PAT_MASK(4) | PAT_MASK(5));
507 pat_msr |= PAT_VALUE(4, PAT_WRITE_PROTECTED) |
508 PAT_VALUE(5, PAT_WRITE_COMBINING);
512 * Due to some Intel errata, we can only safely use the lower 4
513 * PAT entries. Thus, just replace PAT Index 2 with WC instead
516 * Intel Pentium III Processor Specification Update
517 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
520 * Intel Pentium IV Processor Specification Update
521 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
523 pat_msr = rdmsr(MSR_PAT);
524 pat_msr &= ~PAT_MASK(2);
525 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
528 wrmsr(MSR_PAT, pat_msr);
532 * Initialize a vm_page's machine-dependent fields.
535 pmap_page_init(vm_page_t m)
538 TAILQ_INIT(&m->md.pv_list);
539 m->md.pat_mode = PAT_WRITE_BACK;
543 * ABuse the pte nodes for unmapped kva to thread a kva freelist through.
545 * - Must deal with pages in order to ensure that none of the PG_* bits
546 * are ever set, PG_V in particular.
547 * - Assumes we can write to ptes without pte_store() atomic ops, even
548 * on PAE systems. This should be ok.
549 * - Assumes nothing will ever test these addresses for 0 to indicate
550 * no mapping instead of correctly checking PG_V.
551 * - Assumes a vm_offset_t will fit in a pte (true for i386).
552 * Because PG_V is never set, there can be no mappings to invalidate.
554 static int ptelist_count = 0;
556 pmap_ptelist_alloc(vm_offset_t *head)
559 vm_offset_t *phead = (vm_offset_t *)*head;
561 if (ptelist_count == 0) {
562 printf("out of memory!!!!!!\n");
563 return (0); /* Out of memory */
566 va = phead[ptelist_count];
571 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
573 vm_offset_t *phead = (vm_offset_t *)*head;
575 phead[ptelist_count++] = va;
579 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
585 nstackpages = (npages + PAGE_SIZE/sizeof(vm_offset_t) - 1)/ (PAGE_SIZE/sizeof(vm_offset_t));
586 for (i = 0; i < nstackpages; i++) {
587 va = (vm_offset_t)base + i * PAGE_SIZE;
588 m = vm_page_alloc(NULL, i,
589 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
591 pmap_qenter(va, &m, 1);
594 *head = (vm_offset_t)base;
595 for (i = npages - 1; i >= nstackpages; i--) {
596 va = (vm_offset_t)base + i * PAGE_SIZE;
597 pmap_ptelist_free(head, va);
603 * Initialize the pmap module.
604 * Called by vm_init, to initialize any structures that the pmap
605 * system needs to map virtual memory.
612 * Initialize the address space (zone) for the pv entries. Set a
613 * high water mark so that the system can recover from excessive
614 * numbers of pv entries.
616 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
617 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
618 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
619 pv_entry_max = roundup(pv_entry_max, _NPCPV);
620 pv_entry_high_water = 9 * (pv_entry_max / 10);
622 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
623 pv_chunkbase = (struct pv_chunk *)kva_alloc(PAGE_SIZE * pv_maxchunks);
624 if (pv_chunkbase == NULL)
625 panic("pmap_init: not enough kvm for pv chunks");
626 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
630 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
631 "Max number of PV entries");
632 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
633 "Page share factor per proc");
635 static SYSCTL_NODE(_vm_pmap, OID_AUTO, pde, CTLFLAG_RD, 0,
636 "2/4MB page mapping counters");
638 static u_long pmap_pde_mappings;
639 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, mappings, CTLFLAG_RD,
640 &pmap_pde_mappings, 0, "2/4MB page mappings");
642 /***************************************************
643 * Low level helper routines.....
644 ***************************************************/
647 * Determine the appropriate bits to set in a PTE or PDE for a specified
651 pmap_cache_bits(int mode, boolean_t is_pde)
653 int pat_flag, pat_index, cache_bits;
655 /* The PAT bit is different for PTE's and PDE's. */
656 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
658 /* If we don't support PAT, map extended modes to older ones. */
659 if (!(cpu_feature & CPUID_PAT)) {
661 case PAT_UNCACHEABLE:
662 case PAT_WRITE_THROUGH:
666 case PAT_WRITE_COMBINING:
667 case PAT_WRITE_PROTECTED:
668 mode = PAT_UNCACHEABLE;
673 /* Map the caching mode to a PAT index. */
676 case PAT_UNCACHEABLE:
679 case PAT_WRITE_THROUGH:
688 case PAT_WRITE_COMBINING:
691 case PAT_WRITE_PROTECTED:
695 panic("Unknown caching mode %d\n", mode);
700 case PAT_UNCACHEABLE:
701 case PAT_WRITE_PROTECTED:
704 case PAT_WRITE_THROUGH:
710 case PAT_WRITE_COMBINING:
714 panic("Unknown caching mode %d\n", mode);
718 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
721 cache_bits |= pat_flag;
723 cache_bits |= PG_NC_PCD;
725 cache_bits |= PG_NC_PWT;
730 * For SMP, these functions have to use the IPI mechanism for coherence.
732 * N.B.: Before calling any of the following TLB invalidation functions,
733 * the calling processor must ensure that all stores updating a non-
734 * kernel page table are globally performed. Otherwise, another
735 * processor could cache an old, pre-update entry without being
736 * invalidated. This can happen one of two ways: (1) The pmap becomes
737 * active on another processor after its pm_active field is checked by
738 * one of the following functions but before a store updating the page
739 * table is globally performed. (2) The pmap becomes active on another
740 * processor before its pm_active field is checked but due to
741 * speculative loads one of the following functions stills reads the
742 * pmap as inactive on the other processor.
744 * The kernel page table is exempt because its pm_active field is
745 * immutable. The kernel page table is always active on every
749 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
754 CTR2(KTR_PMAP, "pmap_invalidate_page: pmap=%p va=0x%x",
758 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
762 cpuid = PCPU_GET(cpuid);
763 other_cpus = all_cpus;
764 CPU_CLR(cpuid, &other_cpus);
765 if (CPU_ISSET(cpuid, &pmap->pm_active))
767 CPU_AND(&other_cpus, &pmap->pm_active);
768 if (!CPU_EMPTY(&other_cpus))
769 smp_masked_invlpg(other_cpus, va);
776 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
782 CTR3(KTR_PMAP, "pmap_invalidate_page: pmap=%p eva=0x%x sva=0x%x",
786 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
787 for (addr = sva; addr < eva; addr += PAGE_SIZE)
789 smp_invlpg_range(sva, eva);
791 cpuid = PCPU_GET(cpuid);
792 other_cpus = all_cpus;
793 CPU_CLR(cpuid, &other_cpus);
794 if (CPU_ISSET(cpuid, &pmap->pm_active))
795 for (addr = sva; addr < eva; addr += PAGE_SIZE)
797 CPU_AND(&other_cpus, &pmap->pm_active);
798 if (!CPU_EMPTY(&other_cpus))
799 smp_masked_invlpg_range(other_cpus, sva, eva);
806 pmap_invalidate_all(pmap_t pmap)
811 CTR1(KTR_PMAP, "pmap_invalidate_page: pmap=%p", pmap);
814 if (pmap == kernel_pmap || !CPU_CMP(&pmap->pm_active, &all_cpus)) {
818 cpuid = PCPU_GET(cpuid);
819 other_cpus = all_cpus;
820 CPU_CLR(cpuid, &other_cpus);
821 if (CPU_ISSET(cpuid, &pmap->pm_active))
823 CPU_AND(&other_cpus, &pmap->pm_active);
824 if (!CPU_EMPTY(&other_cpus))
825 smp_masked_invltlb(other_cpus);
831 pmap_invalidate_cache(void)
841 * Normal, non-SMP, 486+ invalidation functions.
842 * We inline these within pmap.c for speed.
845 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
847 CTR2(KTR_PMAP, "pmap_invalidate_page: pmap=%p va=0x%x",
850 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
856 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
860 if (eva - sva > PAGE_SIZE)
861 CTR3(KTR_PMAP, "pmap_invalidate_range: pmap=%p sva=0x%x eva=0x%x",
864 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
865 for (addr = sva; addr < eva; addr += PAGE_SIZE)
871 pmap_invalidate_all(pmap_t pmap)
874 CTR1(KTR_PMAP, "pmap_invalidate_all: pmap=%p", pmap);
876 if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
881 pmap_invalidate_cache(void)
888 #define PMAP_CLFLUSH_THRESHOLD (2 * 1024 * 1024)
891 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva)
894 KASSERT((sva & PAGE_MASK) == 0,
895 ("pmap_invalidate_cache_range: sva not page-aligned"));
896 KASSERT((eva & PAGE_MASK) == 0,
897 ("pmap_invalidate_cache_range: eva not page-aligned"));
899 if (cpu_feature & CPUID_SS)
900 ; /* If "Self Snoop" is supported, do nothing. */
901 else if ((cpu_feature & CPUID_CLFSH) != 0 &&
902 eva - sva < PMAP_CLFLUSH_THRESHOLD) {
905 * Otherwise, do per-cache line flush. Use the mfence
906 * instruction to insure that previous stores are
907 * included in the write-back. The processor
908 * propagates flush to other processors in the cache
912 for (; sva < eva; sva += cpu_clflush_line_size)
918 * No targeted cache flush methods are supported by CPU,
919 * or the supplied range is bigger than 2MB.
920 * Globally invalidate cache.
922 pmap_invalidate_cache();
927 pmap_invalidate_cache_pages(vm_page_t *pages, int count)
931 if (count >= PMAP_CLFLUSH_THRESHOLD / PAGE_SIZE ||
932 (cpu_feature & CPUID_CLFSH) == 0) {
933 pmap_invalidate_cache();
935 for (i = 0; i < count; i++)
936 pmap_flush_page(pages[i]);
941 * Are we current address space or kernel? N.B. We return FALSE when
942 * a pmap's page table is in use because a kernel thread is borrowing
943 * it. The borrowed page table can change spontaneously, making any
944 * dependence on its continued use subject to a race condition.
947 pmap_is_current(pmap_t pmap)
950 return (pmap == kernel_pmap ||
951 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
952 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
956 * If the given pmap is not the current or kernel pmap, the returned pte must
957 * be released by passing it to pmap_pte_release().
960 pmap_pte(pmap_t pmap, vm_offset_t va)
965 pde = pmap_pde(pmap, va);
969 /* are we current address space or kernel? */
970 if (pmap_is_current(pmap))
972 mtx_lock(&PMAP2mutex);
973 newpf = *pde & PG_FRAME;
974 if ((*PMAP2 & PG_FRAME) != newpf) {
975 PT_SET_MA(PADDR2, newpf | PG_V | PG_A | PG_M);
976 CTR3(KTR_PMAP, "pmap_pte: pmap=%p va=0x%x newpte=0x%08x",
977 pmap, va, (*PMAP2 & 0xffffffff));
979 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
985 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
989 pmap_pte_release(pt_entry_t *pte)
992 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2) {
993 CTR1(KTR_PMAP, "pmap_pte_release: pte=0x%jx",
995 rw_wlock(&pvh_global_lock);
996 PT_SET_VA(PMAP2, 0, TRUE);
997 rw_wunlock(&pvh_global_lock);
998 mtx_unlock(&PMAP2mutex);
1002 static __inline void
1003 invlcaddr(void *caddr)
1006 invlpg((u_int)caddr);
1011 * Super fast pmap_pte routine best used when scanning
1012 * the pv lists. This eliminates many coarse-grained
1013 * invltlb calls. Note that many of the pv list
1014 * scans are across different pmaps. It is very wasteful
1015 * to do an entire invltlb for checking a single mapping.
1017 * If the given pmap is not the current pmap, pvh_global_lock
1018 * must be held and curthread pinned to a CPU.
1021 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
1026 pde = pmap_pde(pmap, va);
1030 /* are we current address space or kernel? */
1031 if (pmap_is_current(pmap))
1032 return (vtopte(va));
1033 rw_assert(&pvh_global_lock, RA_WLOCKED);
1034 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1035 newpf = *pde & PG_FRAME;
1036 if ((*PMAP1 & PG_FRAME) != newpf) {
1037 PT_SET_MA(PADDR1, newpf | PG_V | PG_A | PG_M);
1038 CTR3(KTR_PMAP, "pmap_pte_quick: pmap=%p va=0x%x newpte=0x%08x",
1039 pmap, va, (u_long)*PMAP1);
1042 PMAP1cpu = PCPU_GET(cpuid);
1047 if (PMAP1cpu != PCPU_GET(cpuid)) {
1048 PMAP1cpu = PCPU_GET(cpuid);
1054 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
1060 * Routine: pmap_extract
1062 * Extract the physical page address associated
1063 * with the given map/virtual_address pair.
1066 pmap_extract(pmap_t pmap, vm_offset_t va)
1075 pde = pmap->pm_pdir[va >> PDRSHIFT];
1077 if ((pde & PG_PS) != 0) {
1078 rtval = xpmap_mtop(pde & PG_PS_FRAME) | (va & PDRMASK);
1082 pte = pmap_pte(pmap, va);
1083 pteval = *pte ? xpmap_mtop(*pte) : 0;
1084 rtval = (pteval & PG_FRAME) | (va & PAGE_MASK);
1085 pmap_pte_release(pte);
1092 * Routine: pmap_extract_ma
1094 * Like pmap_extract, but returns machine address
1097 pmap_extract_ma(pmap_t pmap, vm_offset_t va)
1105 pde = pmap->pm_pdir[va >> PDRSHIFT];
1107 if ((pde & PG_PS) != 0) {
1108 rtval = (pde & ~PDRMASK) | (va & PDRMASK);
1112 pte = pmap_pte(pmap, va);
1113 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1114 pmap_pte_release(pte);
1121 * Routine: pmap_extract_and_hold
1123 * Atomically extract and hold the physical page
1124 * with the given pmap and virtual address pair
1125 * if that mapping permits the given protection.
1128 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1131 pt_entry_t pte, *ptep;
1139 pde = PT_GET(pmap_pde(pmap, va));
1142 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1143 if (vm_page_pa_tryrelock(pmap, (pde &
1144 PG_PS_FRAME) | (va & PDRMASK), &pa))
1146 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
1151 ptep = pmap_pte(pmap, va);
1153 pmap_pte_release(ptep);
1155 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1156 if (vm_page_pa_tryrelock(pmap, pte & PG_FRAME,
1159 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1169 /***************************************************
1170 * Low level mapping routines.....
1171 ***************************************************/
1174 * Add a wired page to the kva.
1175 * Note: not SMP coherent.
1177 * This function may be used before pmap_bootstrap() is called.
1180 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1183 PT_SET_MA(va, xpmap_ptom(pa)| PG_RW | PG_V | pgeflag);
1187 pmap_kenter_ma(vm_offset_t va, vm_paddr_t ma)
1192 pte_store_ma(pte, ma | PG_RW | PG_V | pgeflag);
1195 static __inline void
1196 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1199 PT_SET_MA(va, pa | PG_RW | PG_V | pgeflag | pmap_cache_bits(mode, 0));
1203 * Remove a page from the kernel pagetables.
1204 * Note: not SMP coherent.
1206 * This function may be used before pmap_bootstrap() is called.
1209 pmap_kremove(vm_offset_t va)
1214 PT_CLEAR_VA(pte, FALSE);
1218 * Used to map a range of physical addresses into kernel
1219 * virtual address space.
1221 * The value passed in '*virt' is a suggested virtual address for
1222 * the mapping. Architectures which can support a direct-mapped
1223 * physical to virtual region can return the appropriate address
1224 * within that region, leaving '*virt' unchanged. Other
1225 * architectures should map the pages starting at '*virt' and
1226 * update '*virt' with the first usable address after the mapped
1230 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1232 vm_offset_t va, sva;
1235 CTR4(KTR_PMAP, "pmap_map: va=0x%x start=0x%jx end=0x%jx prot=0x%x",
1236 va, start, end, prot);
1237 while (start < end) {
1238 pmap_kenter(va, start);
1242 pmap_invalidate_range(kernel_pmap, sva, va);
1249 * Add a list of wired pages to the kva
1250 * this routine is only used for temporary
1251 * kernel mappings that do not need to have
1252 * page modification or references recorded.
1253 * Note that old mappings are simply written
1254 * over. The page *must* be wired.
1255 * Note: SMP coherent. Uses a ranged shootdown IPI.
1258 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1260 pt_entry_t *endpte, *pte;
1262 vm_offset_t va = sva;
1264 multicall_entry_t mcl[16];
1265 multicall_entry_t *mclp = mcl;
1268 CTR2(KTR_PMAP, "pmap_qenter:sva=0x%x count=%d", va, count);
1270 endpte = pte + count;
1271 while (pte < endpte) {
1272 pa = VM_PAGE_TO_MACH(*ma) | pgeflag | PG_RW | PG_V | PG_M | PG_A;
1274 mclp->op = __HYPERVISOR_update_va_mapping;
1276 mclp->args[1] = (uint32_t)(pa & 0xffffffff);
1277 mclp->args[2] = (uint32_t)(pa >> 32);
1278 mclp->args[3] = (*pte & PG_V) ? UVMF_INVLPG|UVMF_ALL : 0;
1285 if (mclcount == 16) {
1286 error = HYPERVISOR_multicall(mcl, mclcount);
1289 KASSERT(error == 0, ("bad multicall %d", error));
1293 error = HYPERVISOR_multicall(mcl, mclcount);
1294 KASSERT(error == 0, ("bad multicall %d", error));
1298 for (pte = vtopte(sva), mclcount = 0; mclcount < count; mclcount++, pte++)
1299 KASSERT(*pte, ("pte not set for va=0x%x", sva + mclcount*PAGE_SIZE));
1304 * This routine tears out page mappings from the
1305 * kernel -- it is meant only for temporary mappings.
1306 * Note: SMP coherent. Uses a ranged shootdown IPI.
1309 pmap_qremove(vm_offset_t sva, int count)
1313 CTR2(KTR_PMAP, "pmap_qremove: sva=0x%x count=%d", sva, count);
1315 rw_wlock(&pvh_global_lock);
1317 while (count-- > 0) {
1322 pmap_invalidate_range(kernel_pmap, sva, va);
1324 rw_wunlock(&pvh_global_lock);
1327 /***************************************************
1328 * Page table page management routines.....
1329 ***************************************************/
1330 static __inline void
1331 pmap_free_zero_pages(vm_page_t free)
1335 while (free != NULL) {
1337 free = (void *)m->object;
1339 vm_page_free_zero(m);
1344 * Decrements a page table page's wire count, which is used to record the
1345 * number of valid page table entries within the page. If the wire count
1346 * drops to zero, then the page table page is unmapped. Returns TRUE if the
1347 * page table page was unmapped and FALSE otherwise.
1349 static inline boolean_t
1350 pmap_unwire_ptp(pmap_t pmap, vm_page_t m, vm_page_t *free)
1354 if (m->wire_count == 0) {
1355 _pmap_unwire_ptp(pmap, m, free);
1362 _pmap_unwire_ptp(pmap_t pmap, vm_page_t m, vm_page_t *free)
1368 * unmap the page table page
1370 xen_pt_unpin(pmap->pm_pdir[m->pindex]);
1372 * page *might* contain residual mapping :-/
1374 PD_CLEAR_VA(pmap, m->pindex, TRUE);
1376 --pmap->pm_stats.resident_count;
1379 * This is a release store so that the ordinary store unmapping
1380 * the page table page is globally performed before TLB shoot-
1383 atomic_subtract_rel_int(&cnt.v_wire_count, 1);
1386 * Do an invltlb to make the invalidated mapping
1387 * take effect immediately.
1389 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1390 pmap_invalidate_page(pmap, pteva);
1393 * Put page on a list so that it is released after
1394 * *ALL* TLB shootdown is done
1396 m->object = (void *)*free;
1401 * After removing a page table entry, this routine is used to
1402 * conditionally free the page, and manage the hold/wire counts.
1405 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t *free)
1410 if (va >= VM_MAXUSER_ADDRESS)
1412 ptepde = PT_GET(pmap_pde(pmap, va));
1413 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1414 return (pmap_unwire_ptp(pmap, mpte, free));
1418 * Initialize the pmap for the swapper process.
1421 pmap_pinit0(pmap_t pmap)
1424 PMAP_LOCK_INIT(pmap);
1426 * Since the page table directory is shared with the kernel pmap,
1427 * which is already included in the list "allpmaps", this pmap does
1428 * not need to be inserted into that list.
1430 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1432 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1434 CPU_ZERO(&pmap->pm_active);
1435 PCPU_SET(curpmap, pmap);
1436 TAILQ_INIT(&pmap->pm_pvchunk);
1437 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1441 * Initialize a preallocated and zeroed pmap structure,
1442 * such as one in a vmspace structure.
1445 pmap_pinit(pmap_t pmap)
1447 vm_page_t m, ptdpg[NPGPTD + 1];
1448 int npgptd = NPGPTD + 1;
1451 #ifdef HAMFISTED_LOCKING
1452 mtx_lock(&createdelete_lock);
1456 * No need to allocate page table space yet but we do need a valid
1457 * page directory table.
1459 if (pmap->pm_pdir == NULL) {
1460 pmap->pm_pdir = (pd_entry_t *)kva_alloc(NBPTD);
1461 if (pmap->pm_pdir == NULL) {
1462 #ifdef HAMFISTED_LOCKING
1463 mtx_unlock(&createdelete_lock);
1468 pmap->pm_pdpt = (pd_entry_t *)kva_alloc(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 rw_wlock(&pvh_global_lock);
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 rw_wunlock(&pvh_global_lock);
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, u_int flags)
1554 * Allocate a page table page.
1556 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1557 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1558 if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
1560 rw_wunlock(&pvh_global_lock);
1562 rw_wlock(&pvh_global_lock);
1567 * Indicate the need to retry. While waiting, the page table
1568 * page may have been allocated.
1572 if ((m->flags & PG_ZERO) == 0)
1576 * Map the pagetable page into the process address space, if
1577 * it isn't already there.
1580 pmap->pm_stats.resident_count++;
1582 ptema = VM_PAGE_TO_MACH(m);
1584 PT_SET_VA_MA(&pmap->pm_pdir[ptepindex],
1585 (ptema | PG_U | PG_RW | PG_V | PG_A | PG_M), TRUE);
1587 KASSERT(pmap->pm_pdir[ptepindex],
1588 ("_pmap_allocpte: ptepindex=%d did not get mapped", ptepindex));
1593 pmap_allocpte(pmap_t pmap, vm_offset_t va, u_int flags)
1600 * Calculate pagetable page index
1602 ptepindex = va >> PDRSHIFT;
1605 * Get the page directory entry
1607 ptema = pmap->pm_pdir[ptepindex];
1610 * This supports switching from a 4MB page to a
1613 if (ptema & PG_PS) {
1617 pmap->pm_pdir[ptepindex] = 0;
1619 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1620 pmap_invalidate_all(kernel_pmap);
1624 * If the page table page is mapped, we just increment the
1625 * hold count, and activate it.
1628 m = PHYS_TO_VM_PAGE(xpmap_mtop(ptema) & PG_FRAME);
1632 * Here if the pte page isn't mapped, or if it has
1635 CTR3(KTR_PMAP, "pmap_allocpte: pmap=%p va=0x%08x flags=0x%x",
1637 m = _pmap_allocpte(pmap, ptepindex, flags);
1638 if (m == NULL && (flags & PMAP_ENTER_NOSLEEP) == 0)
1641 KASSERT(pmap->pm_pdir[ptepindex], ("ptepindex=%d did not get mapped", ptepindex));
1647 /***************************************************
1648 * Pmap allocation/deallocation routines.
1649 ***************************************************/
1653 * Deal with a SMP shootdown of other users of the pmap that we are
1654 * trying to dispose of. This can be a bit hairy.
1656 static cpuset_t *lazymask;
1657 static u_int lazyptd;
1658 static volatile u_int lazywait;
1660 void pmap_lazyfix_action(void);
1663 pmap_lazyfix_action(void)
1667 (*ipi_lazypmap_counts[PCPU_GET(cpuid)])++;
1669 if (rcr3() == lazyptd)
1670 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1671 CPU_CLR_ATOMIC(PCPU_GET(cpuid), lazymask);
1672 atomic_store_rel_int(&lazywait, 1);
1676 pmap_lazyfix_self(u_int cpuid)
1679 if (rcr3() == lazyptd)
1680 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1681 CPU_CLR_ATOMIC(cpuid, lazymask);
1686 pmap_lazyfix(pmap_t pmap)
1688 cpuset_t mymask, mask;
1692 mask = pmap->pm_active;
1693 while (!CPU_EMPTY(&mask)) {
1696 /* Find least significant set bit. */
1697 lsb = CPU_FFS(&mask);
1700 CPU_SETOF(lsb, &mask);
1701 mtx_lock_spin(&smp_ipi_mtx);
1703 lazyptd = vtophys(pmap->pm_pdpt);
1705 lazyptd = vtophys(pmap->pm_pdir);
1707 cpuid = PCPU_GET(cpuid);
1709 /* Use a cpuset just for having an easy check. */
1710 CPU_SETOF(cpuid, &mymask);
1711 if (!CPU_CMP(&mask, &mymask)) {
1712 lazymask = &pmap->pm_active;
1713 pmap_lazyfix_self(cpuid);
1715 atomic_store_rel_int((u_int *)&lazymask,
1716 (u_int)&pmap->pm_active);
1717 atomic_store_rel_int(&lazywait, 0);
1718 ipi_selected(mask, IPI_LAZYPMAP);
1719 while (lazywait == 0) {
1725 mtx_unlock_spin(&smp_ipi_mtx);
1727 printf("pmap_lazyfix: spun for 50000000\n");
1728 mask = pmap->pm_active;
1735 * Cleaning up on uniprocessor is easy. For various reasons, we're
1736 * unlikely to have to even execute this code, including the fact
1737 * that the cleanup is deferred until the parent does a wait(2), which
1738 * means that another userland process has run.
1741 pmap_lazyfix(pmap_t pmap)
1745 cr3 = vtophys(pmap->pm_pdir);
1746 if (cr3 == rcr3()) {
1747 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1748 CPU_CLR(PCPU_GET(cpuid), &pmap->pm_active);
1754 * Release any resources held by the given physical map.
1755 * Called when a pmap initialized by pmap_pinit is being released.
1756 * Should only be called if the map contains no valid mappings.
1759 pmap_release(pmap_t pmap)
1761 vm_page_t m, ptdpg[2*NPGPTD+1];
1765 int npgptd = NPGPTD + 1;
1767 int npgptd = NPGPTD;
1770 KASSERT(pmap->pm_stats.resident_count == 0,
1771 ("pmap_release: pmap resident count %ld != 0",
1772 pmap->pm_stats.resident_count));
1775 #ifdef HAMFISTED_LOCKING
1776 mtx_lock(&createdelete_lock);
1780 mtx_lock_spin(&allpmaps_lock);
1781 LIST_REMOVE(pmap, pm_list);
1782 mtx_unlock_spin(&allpmaps_lock);
1784 for (i = 0; i < NPGPTD; i++)
1785 ptdpg[i] = PHYS_TO_VM_PAGE(vtophys(pmap->pm_pdir + (i*NPDEPG)) & PG_FRAME);
1786 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1788 ptdpg[NPGPTD] = PHYS_TO_VM_PAGE(vtophys(pmap->pm_pdpt));
1791 for (i = 0; i < npgptd; i++) {
1793 ma = VM_PAGE_TO_MACH(m);
1794 /* unpinning L1 and L2 treated the same */
1803 KASSERT(VM_PAGE_TO_MACH(m) == (pmap->pm_pdpt[i] & PG_FRAME),
1804 ("pmap_release: got wrong ptd page"));
1807 atomic_subtract_int(&cnt.v_wire_count, 1);
1811 pmap_qremove((vm_offset_t)pmap->pm_pdpt, 1);
1814 #ifdef HAMFISTED_LOCKING
1815 mtx_unlock(&createdelete_lock);
1820 kvm_size(SYSCTL_HANDLER_ARGS)
1822 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1824 return (sysctl_handle_long(oidp, &ksize, 0, req));
1826 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1827 0, 0, kvm_size, "IU", "Size of KVM");
1830 kvm_free(SYSCTL_HANDLER_ARGS)
1832 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1834 return (sysctl_handle_long(oidp, &kfree, 0, req));
1836 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1837 0, 0, kvm_free, "IU", "Amount of KVM free");
1840 * grow the number of kernel page table entries, if needed
1843 pmap_growkernel(vm_offset_t addr)
1846 vm_paddr_t ptppaddr;
1850 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1851 if (kernel_vm_end == 0) {
1852 kernel_vm_end = KERNBASE;
1854 while (pdir_pde(PTD, kernel_vm_end)) {
1855 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1857 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1858 kernel_vm_end = kernel_map->max_offset;
1863 addr = roundup2(addr, NBPDR);
1864 if (addr - 1 >= kernel_map->max_offset)
1865 addr = kernel_map->max_offset;
1866 while (kernel_vm_end < addr) {
1867 if (pdir_pde(PTD, kernel_vm_end)) {
1868 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1869 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1870 kernel_vm_end = kernel_map->max_offset;
1876 nkpg = vm_page_alloc(NULL, kernel_vm_end >> PDRSHIFT,
1877 VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1880 panic("pmap_growkernel: no memory to grow kernel");
1884 if ((nkpg->flags & PG_ZERO) == 0)
1885 pmap_zero_page(nkpg);
1886 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1887 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1888 rw_wlock(&pvh_global_lock);
1889 PD_SET_VA(kernel_pmap, (kernel_vm_end >> PDRSHIFT), newpdir, TRUE);
1890 mtx_lock_spin(&allpmaps_lock);
1891 LIST_FOREACH(pmap, &allpmaps, pm_list)
1892 PD_SET_VA(pmap, (kernel_vm_end >> PDRSHIFT), newpdir, TRUE);
1894 mtx_unlock_spin(&allpmaps_lock);
1895 rw_wunlock(&pvh_global_lock);
1897 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
1898 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1899 kernel_vm_end = kernel_map->max_offset;
1906 /***************************************************
1907 * page management routines.
1908 ***************************************************/
1910 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1911 CTASSERT(_NPCM == 11);
1912 CTASSERT(_NPCPV == 336);
1914 static __inline struct pv_chunk *
1915 pv_to_chunk(pv_entry_t pv)
1918 return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
1921 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1923 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
1924 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
1926 static const uint32_t pc_freemask[_NPCM] = {
1927 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1928 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1929 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1930 PC_FREE0_9, PC_FREE10
1933 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1934 "Current number of pv entries");
1937 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1939 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
1940 "Current number of pv entry chunks");
1941 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
1942 "Current number of pv entry chunks allocated");
1943 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
1944 "Current number of pv entry chunks frees");
1945 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
1946 "Number of times tried to get a chunk page but failed.");
1948 static long pv_entry_frees, pv_entry_allocs;
1949 static int pv_entry_spare;
1951 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
1952 "Current number of pv entry frees");
1953 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
1954 "Current number of pv entry allocs");
1955 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
1956 "Current number of spare pv entries");
1960 * We are in a serious low memory condition. Resort to
1961 * drastic measures to free some pages so we can allocate
1962 * another pv entry chunk.
1965 pmap_pv_reclaim(pmap_t locked_pmap)
1968 struct pv_chunk *pc;
1970 pt_entry_t *pte, tpte;
1973 vm_page_t free, m, m_pc;
1975 int bit, field, freed;
1977 PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
1980 TAILQ_INIT(&newtail);
1981 while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL && (pv_vafree == 0 ||
1983 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
1984 if (pmap != pc->pc_pmap) {
1986 pmap_invalidate_all(pmap);
1987 if (pmap != locked_pmap)
1991 /* Avoid deadlock and lock recursion. */
1992 if (pmap > locked_pmap)
1994 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
1996 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2002 * Destroy every non-wired, 4 KB page mapping in the chunk.
2005 for (field = 0; field < _NPCM; field++) {
2006 for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2007 inuse != 0; inuse &= ~(1UL << bit)) {
2009 pv = &pc->pc_pventry[field * 32 + bit];
2011 pte = pmap_pte(pmap, va);
2013 if ((tpte & PG_W) == 0)
2014 tpte = pte_load_clear(pte);
2015 pmap_pte_release(pte);
2016 if ((tpte & PG_W) != 0)
2019 ("pmap_pv_reclaim: pmap %p va %x zero pte",
2021 if ((tpte & PG_G) != 0)
2022 pmap_invalidate_page(pmap, va);
2023 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
2024 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2026 if ((tpte & PG_A) != 0)
2027 vm_page_aflag_set(m, PGA_REFERENCED);
2028 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2029 if (TAILQ_EMPTY(&m->md.pv_list))
2030 vm_page_aflag_clear(m, PGA_WRITEABLE);
2031 pc->pc_map[field] |= 1UL << bit;
2032 pmap_unuse_pt(pmap, va, &free);
2037 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2040 /* Every freed mapping is for a 4 KB page. */
2041 pmap->pm_stats.resident_count -= freed;
2042 PV_STAT(pv_entry_frees += freed);
2043 PV_STAT(pv_entry_spare += freed);
2044 pv_entry_count -= freed;
2045 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2046 for (field = 0; field < _NPCM; field++)
2047 if (pc->pc_map[field] != pc_freemask[field]) {
2048 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2050 TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2053 * One freed pv entry in locked_pmap is
2056 if (pmap == locked_pmap)
2060 if (field == _NPCM) {
2061 PV_STAT(pv_entry_spare -= _NPCPV);
2062 PV_STAT(pc_chunk_count--);
2063 PV_STAT(pc_chunk_frees++);
2064 /* Entire chunk is free; return it. */
2065 m_pc = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2066 pmap_qremove((vm_offset_t)pc, 1);
2067 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2072 TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
2074 pmap_invalidate_all(pmap);
2075 if (pmap != locked_pmap)
2078 if (m_pc == NULL && pv_vafree != 0 && free != NULL) {
2080 free = (void *)m_pc->object;
2081 /* Recycle a freed page table page. */
2082 m_pc->wire_count = 1;
2083 atomic_add_int(&cnt.v_wire_count, 1);
2085 pmap_free_zero_pages(free);
2090 * free the pv_entry back to the free list
2093 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2095 struct pv_chunk *pc;
2096 int idx, field, bit;
2098 rw_assert(&pvh_global_lock, RA_WLOCKED);
2099 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2100 PV_STAT(pv_entry_frees++);
2101 PV_STAT(pv_entry_spare++);
2103 pc = pv_to_chunk(pv);
2104 idx = pv - &pc->pc_pventry[0];
2107 pc->pc_map[field] |= 1ul << bit;
2108 for (idx = 0; idx < _NPCM; idx++)
2109 if (pc->pc_map[idx] != pc_freemask[idx]) {
2111 * 98% of the time, pc is already at the head of the
2112 * list. If it isn't already, move it to the head.
2114 if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
2116 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2117 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2122 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2127 free_pv_chunk(struct pv_chunk *pc)
2131 TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2132 PV_STAT(pv_entry_spare -= _NPCPV);
2133 PV_STAT(pc_chunk_count--);
2134 PV_STAT(pc_chunk_frees++);
2135 /* entire chunk is free, return it */
2136 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2137 pmap_qremove((vm_offset_t)pc, 1);
2138 vm_page_unwire(m, 0);
2140 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2144 * get a new pv_entry, allocating a block from the system
2148 get_pv_entry(pmap_t pmap, boolean_t try)
2150 static const struct timeval printinterval = { 60, 0 };
2151 static struct timeval lastprint;
2154 struct pv_chunk *pc;
2157 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2158 rw_assert(&pvh_global_lock, RA_WLOCKED);
2159 PV_STAT(pv_entry_allocs++);
2161 if (pv_entry_count > pv_entry_high_water)
2162 if (ratecheck(&lastprint, &printinterval))
2163 printf("Approaching the limit on PV entries, consider "
2164 "increasing either the vm.pmap.shpgperproc or the "
2165 "vm.pmap.pv_entry_max tunable.\n");
2167 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2169 for (field = 0; field < _NPCM; field++) {
2170 if (pc->pc_map[field]) {
2171 bit = bsfl(pc->pc_map[field]);
2175 if (field < _NPCM) {
2176 pv = &pc->pc_pventry[field * 32 + bit];
2177 pc->pc_map[field] &= ~(1ul << bit);
2178 /* If this was the last item, move it to tail */
2179 for (field = 0; field < _NPCM; field++)
2180 if (pc->pc_map[field] != 0) {
2181 PV_STAT(pv_entry_spare--);
2182 return (pv); /* not full, return */
2184 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2185 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2186 PV_STAT(pv_entry_spare--);
2191 * Access to the ptelist "pv_vafree" is synchronized by the page
2192 * queues lock. If "pv_vafree" is currently non-empty, it will
2193 * remain non-empty until pmap_ptelist_alloc() completes.
2195 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
2196 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
2199 PV_STAT(pc_chunk_tryfail++);
2202 m = pmap_pv_reclaim(pmap);
2206 PV_STAT(pc_chunk_count++);
2207 PV_STAT(pc_chunk_allocs++);
2208 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
2209 pmap_qenter((vm_offset_t)pc, &m, 1);
2210 if ((m->flags & PG_ZERO) == 0)
2213 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
2214 for (field = 1; field < _NPCM; field++)
2215 pc->pc_map[field] = pc_freemask[field];
2216 TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
2217 pv = &pc->pc_pventry[0];
2218 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2219 PV_STAT(pv_entry_spare += _NPCPV - 1);
2223 static __inline pv_entry_t
2224 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2228 rw_assert(&pvh_global_lock, RA_WLOCKED);
2229 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
2230 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
2231 TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
2239 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
2243 pv = pmap_pvh_remove(pvh, pmap, va);
2244 KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
2245 free_pv_entry(pmap, pv);
2249 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
2252 rw_assert(&pvh_global_lock, RA_WLOCKED);
2253 pmap_pvh_free(&m->md, pmap, va);
2254 if (TAILQ_EMPTY(&m->md.pv_list))
2255 vm_page_aflag_clear(m, PGA_WRITEABLE);
2259 * Conditionally create a pv entry.
2262 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2266 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2267 rw_assert(&pvh_global_lock, RA_WLOCKED);
2268 if (pv_entry_count < pv_entry_high_water &&
2269 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2271 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
2278 * pmap_remove_pte: do the things to unmap a page in a process
2281 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va, vm_page_t *free)
2286 CTR3(KTR_PMAP, "pmap_remove_pte: pmap=%p *ptq=0x%x va=0x%x",
2287 pmap, (u_long)*ptq, va);
2289 rw_assert(&pvh_global_lock, RA_WLOCKED);
2290 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2292 PT_SET_VA_MA(ptq, 0, TRUE);
2293 KASSERT(oldpte != 0,
2294 ("pmap_remove_pte: pmap %p va %x zero pte", pmap, va));
2296 pmap->pm_stats.wired_count -= 1;
2298 * Machines that don't support invlpg, also don't support
2302 pmap_invalidate_page(kernel_pmap, va);
2303 pmap->pm_stats.resident_count -= 1;
2304 if (oldpte & PG_MANAGED) {
2305 m = PHYS_TO_VM_PAGE(xpmap_mtop(oldpte) & PG_FRAME);
2306 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2309 vm_page_aflag_set(m, PGA_REFERENCED);
2310 pmap_remove_entry(pmap, m, va);
2312 return (pmap_unuse_pt(pmap, va, free));
2316 * Remove a single page from a process address space
2319 pmap_remove_page(pmap_t pmap, vm_offset_t va, vm_page_t *free)
2323 CTR2(KTR_PMAP, "pmap_remove_page: pmap=%p va=0x%x",
2326 rw_assert(&pvh_global_lock, RA_WLOCKED);
2327 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
2328 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2329 if ((pte = pmap_pte_quick(pmap, va)) == NULL || (*pte & PG_V) == 0)
2331 pmap_remove_pte(pmap, pte, va, free);
2332 pmap_invalidate_page(pmap, va);
2334 PT_SET_MA(PADDR1, 0);
2339 * Remove the given range of addresses from the specified map.
2341 * It is assumed that the start and end are properly
2342 * rounded to the page size.
2345 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2350 vm_page_t free = NULL;
2353 CTR3(KTR_PMAP, "pmap_remove: pmap=%p sva=0x%x eva=0x%x",
2357 * Perform an unsynchronized read. This is, however, safe.
2359 if (pmap->pm_stats.resident_count == 0)
2364 rw_wlock(&pvh_global_lock);
2369 * special handling of removing one page. a very
2370 * common operation and easy to short circuit some
2373 if ((sva + PAGE_SIZE == eva) &&
2374 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
2375 pmap_remove_page(pmap, sva, &free);
2379 for (; sva < eva; sva = pdnxt) {
2383 * Calculate index for next page table.
2385 pdnxt = (sva + NBPDR) & ~PDRMASK;
2388 if (pmap->pm_stats.resident_count == 0)
2391 pdirindex = sva >> PDRSHIFT;
2392 ptpaddr = pmap->pm_pdir[pdirindex];
2395 * Weed out invalid mappings. Note: we assume that the page
2396 * directory table is always allocated, and in kernel virtual.
2402 * Check for large page.
2404 if ((ptpaddr & PG_PS) != 0) {
2405 PD_CLEAR_VA(pmap, pdirindex, TRUE);
2406 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2412 * Limit our scan to either the end of the va represented
2413 * by the current page table page, or to the end of the
2414 * range being removed.
2419 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2421 if ((*pte & PG_V) == 0)
2425 * The TLB entry for a PG_G mapping is invalidated
2426 * by pmap_remove_pte().
2428 if ((*pte & PG_G) == 0)
2430 if (pmap_remove_pte(pmap, pte, sva, &free))
2436 PT_SET_VA_MA(PMAP1, 0, TRUE);
2439 pmap_invalidate_all(pmap);
2441 rw_wunlock(&pvh_global_lock);
2443 pmap_free_zero_pages(free);
2447 * Routine: pmap_remove_all
2449 * Removes this physical page from
2450 * all physical maps in which it resides.
2451 * Reflects back modify bits to the pager.
2454 * Original versions of this routine were very
2455 * inefficient because they iteratively called
2456 * pmap_remove (slow...)
2460 pmap_remove_all(vm_page_t m)
2464 pt_entry_t *pte, tpte;
2467 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2468 ("pmap_remove_all: page %p is not managed", m));
2470 rw_wlock(&pvh_global_lock);
2472 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2475 pmap->pm_stats.resident_count--;
2476 pte = pmap_pte_quick(pmap, pv->pv_va);
2478 PT_SET_VA_MA(pte, 0, TRUE);
2479 KASSERT(tpte != 0, ("pmap_remove_all: pmap %p va %x zero pte",
2482 pmap->pm_stats.wired_count--;
2484 vm_page_aflag_set(m, PGA_REFERENCED);
2487 * Update the vm_page_t clean and reference bits.
2489 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2491 pmap_unuse_pt(pmap, pv->pv_va, &free);
2492 pmap_invalidate_page(pmap, pv->pv_va);
2493 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2494 free_pv_entry(pmap, pv);
2497 vm_page_aflag_clear(m, PGA_WRITEABLE);
2500 PT_SET_MA(PADDR1, 0);
2502 rw_wunlock(&pvh_global_lock);
2503 pmap_free_zero_pages(free);
2507 * Set the physical protection on the
2508 * specified range of this map as requested.
2511 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2518 CTR4(KTR_PMAP, "pmap_protect: pmap=%p sva=0x%x eva=0x%x prot=0x%x",
2519 pmap, sva, eva, prot);
2521 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2522 pmap_remove(pmap, sva, eva);
2527 if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) ==
2528 (VM_PROT_WRITE|VM_PROT_EXECUTE))
2531 if (prot & VM_PROT_WRITE)
2537 rw_wlock(&pvh_global_lock);
2540 for (; sva < eva; sva = pdnxt) {
2541 pt_entry_t obits, pbits;
2544 pdnxt = (sva + NBPDR) & ~PDRMASK;
2548 pdirindex = sva >> PDRSHIFT;
2549 ptpaddr = pmap->pm_pdir[pdirindex];
2552 * Weed out invalid mappings. Note: we assume that the page
2553 * directory table is always allocated, and in kernel virtual.
2559 * Check for large page.
2561 if ((ptpaddr & PG_PS) != 0) {
2562 if ((prot & VM_PROT_WRITE) == 0)
2563 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
2565 if ((prot & VM_PROT_EXECUTE) == 0)
2566 pmap->pm_pdir[pdirindex] |= pg_nx;
2575 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2581 * Regardless of whether a pte is 32 or 64 bits in
2582 * size, PG_RW, PG_A, and PG_M are among the least
2583 * significant 32 bits.
2585 obits = pbits = *pte;
2586 if ((pbits & PG_V) == 0)
2589 if ((prot & VM_PROT_WRITE) == 0) {
2590 if ((pbits & (PG_MANAGED | PG_M | PG_RW)) ==
2591 (PG_MANAGED | PG_M | PG_RW)) {
2592 m = PHYS_TO_VM_PAGE(xpmap_mtop(pbits) &
2596 pbits &= ~(PG_RW | PG_M);
2599 if ((prot & VM_PROT_EXECUTE) == 0)
2603 if (pbits != obits) {
2605 PT_SET_VA_MA(pte, pbits, TRUE);
2609 pmap_invalidate_page(pmap, sva);
2617 PT_SET_VA_MA(PMAP1, 0, TRUE);
2619 pmap_invalidate_all(pmap);
2621 rw_wunlock(&pvh_global_lock);
2626 * Insert the given physical page (p) at
2627 * the specified virtual address (v) in the
2628 * target physical map with the protection requested.
2630 * If specified, the page will be wired down, meaning
2631 * that the related pte can not be reclaimed.
2633 * NB: This is the only routine which MAY NOT lazy-evaluate
2634 * or lose information. That is, this routine must actually
2635 * insert this page into the given map NOW.
2638 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2639 u_int flags, int8_t psind __unused)
2643 pt_entry_t newpte, origpte;
2647 boolean_t invlva, wired;
2650 "pmap_enter: pmap=%08p va=0x%08x ma=0x%08x prot=0x%x flags=0x%x",
2651 pmap, va, VM_PAGE_TO_MACH(m), prot, flags);
2652 va = trunc_page(va);
2653 KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
2654 KASSERT(va < UPT_MIN_ADDRESS || va >= UPT_MAX_ADDRESS,
2655 ("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)",
2657 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
2658 VM_OBJECT_ASSERT_LOCKED(m->object);
2661 wired = (flags & PMAP_ENTER_WIRED) != 0;
2663 rw_wlock(&pvh_global_lock);
2668 * In the case that a page table page is not
2669 * resident, we are creating it here.
2671 if (va < VM_MAXUSER_ADDRESS) {
2672 mpte = pmap_allocpte(pmap, va, flags);
2674 KASSERT((flags & PMAP_ENTER_NOSLEEP) != 0,
2675 ("pmap_allocpte failed with sleep allowed"));
2677 rw_wunlock(&pvh_global_lock);
2679 return (KERN_RESOURCE_SHORTAGE);
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_next);
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 rw_wunlock(&pvh_global_lock);
2846 return (KERN_SUCCESS);
2850 * Maps a sequence of resident pages belonging to the same object.
2851 * The sequence begins with the given page m_start. This page is
2852 * mapped at the given virtual address start. Each subsequent page is
2853 * mapped at a virtual address that is offset from start by the same
2854 * amount as the page is offset from m_start within the object. The
2855 * last page in the sequence is the page with the largest offset from
2856 * m_start that can be mapped at a virtual address less than the given
2857 * virtual address end. Not every virtual page between start and end
2858 * is mapped; only those for which a resident page exists with the
2859 * corresponding offset from m_start are mapped.
2862 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2863 vm_page_t m_start, vm_prot_t prot)
2866 vm_pindex_t diff, psize;
2867 multicall_entry_t mcl[16];
2868 multicall_entry_t *mclp = mcl;
2869 int error, count = 0;
2871 VM_OBJECT_ASSERT_LOCKED(m_start->object);
2873 psize = atop(end - start);
2876 rw_wlock(&pvh_global_lock);
2878 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2879 mpte = pmap_enter_quick_locked(&mclp, &count, pmap, start + ptoa(diff), m,
2881 m = TAILQ_NEXT(m, listq);
2883 error = HYPERVISOR_multicall(mcl, count);
2884 KASSERT(error == 0, ("bad multicall %d", error));
2890 error = HYPERVISOR_multicall(mcl, count);
2891 KASSERT(error == 0, ("bad multicall %d", error));
2893 rw_wunlock(&pvh_global_lock);
2898 * this code makes some *MAJOR* assumptions:
2899 * 1. Current pmap & pmap exists.
2902 * 4. No page table pages.
2903 * but is *MUCH* faster than pmap_enter...
2907 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
2909 multicall_entry_t mcl, *mclp;
2913 CTR4(KTR_PMAP, "pmap_enter_quick: pmap=%p va=0x%x m=%p prot=0x%x",
2916 rw_wlock(&pvh_global_lock);
2918 (void)pmap_enter_quick_locked(&mclp, &count, pmap, va, m, prot, NULL);
2920 HYPERVISOR_multicall(&mcl, count);
2921 rw_wunlock(&pvh_global_lock);
2927 pmap_enter_quick_range(pmap_t pmap, vm_offset_t *addrs, vm_page_t *pages, vm_prot_t *prots, int count)
2929 int i, error, index = 0;
2930 multicall_entry_t mcl[16];
2931 multicall_entry_t *mclp = mcl;
2934 for (i = 0; i < count; i++, addrs++, pages++, prots++) {
2935 if (!pmap_is_prefaultable_locked(pmap, *addrs))
2938 (void) pmap_enter_quick_locked(&mclp, &index, pmap, *addrs, *pages, *prots, NULL);
2940 error = HYPERVISOR_multicall(mcl, index);
2943 KASSERT(error == 0, ("bad multicall %d", error));
2947 error = HYPERVISOR_multicall(mcl, index);
2948 KASSERT(error == 0, ("bad multicall %d", error));
2956 pmap_enter_quick_locked(multicall_entry_t **mclpp, int *count, pmap_t pmap, vm_offset_t va, vm_page_t m,
2957 vm_prot_t prot, vm_page_t mpte)
2962 multicall_entry_t *mcl = *mclpp;
2964 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
2965 (m->oflags & VPO_UNMANAGED) != 0,
2966 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
2967 rw_assert(&pvh_global_lock, RA_WLOCKED);
2968 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2971 * In the case that a page table page is not
2972 * resident, we are creating it here.
2974 if (va < VM_MAXUSER_ADDRESS) {
2979 * Calculate pagetable page index
2981 ptepindex = va >> PDRSHIFT;
2982 if (mpte && (mpte->pindex == ptepindex)) {
2986 * Get the page directory entry
2988 ptema = pmap->pm_pdir[ptepindex];
2991 * If the page table page is mapped, we just increment
2992 * the hold count, and activate it.
2996 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2997 mpte = PHYS_TO_VM_PAGE(xpmap_mtop(ptema) & PG_FRAME);
3000 mpte = _pmap_allocpte(pmap, ptepindex,
3001 PMAP_ENTER_NOSLEEP);
3011 * This call to vtopte makes the assumption that we are
3012 * entering the page into the current pmap. In order to support
3013 * quick entry into any pmap, one would likely use pmap_pte_quick.
3014 * But that isn't as quick as vtopte.
3016 KASSERT(pmap_is_current(pmap), ("entering pages in non-current pmap"));
3027 * Enter on the PV list if part of our managed memory.
3029 if ((m->oflags & VPO_UNMANAGED) == 0 &&
3030 !pmap_try_insert_pv_entry(pmap, va, m)) {
3033 if (pmap_unwire_ptp(pmap, mpte, &free)) {
3034 pmap_invalidate_page(pmap, va);
3035 pmap_free_zero_pages(free);
3044 * Increment counters
3046 pmap->pm_stats.resident_count++;
3048 pa = VM_PAGE_TO_PHYS(m);
3050 if ((prot & VM_PROT_EXECUTE) == 0)
3056 * Now validate mapping with RO protection
3058 if ((m->oflags & VPO_UNMANAGED) != 0)
3059 pte_store(pte, pa | PG_V | PG_U);
3061 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
3064 * Now validate mapping with RO protection
3066 if ((m->oflags & VPO_UNMANAGED) != 0)
3067 pa = xpmap_ptom(pa | PG_V | PG_U);
3069 pa = xpmap_ptom(pa | PG_V | PG_U | PG_MANAGED);
3071 mcl->op = __HYPERVISOR_update_va_mapping;
3073 mcl->args[1] = (uint32_t)(pa & 0xffffffff);
3074 mcl->args[2] = (uint32_t)(pa >> 32);
3077 *count = *count + 1;
3083 * Make a temporary mapping for a physical address. This is only intended
3084 * to be used for panic dumps.
3087 pmap_kenter_temporary(vm_paddr_t pa, int i)
3090 vm_paddr_t ma = xpmap_ptom(pa);
3092 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
3093 PT_SET_MA(va, (ma & ~PAGE_MASK) | PG_V | pgeflag);
3095 return ((void *)crashdumpmap);
3099 * This code maps large physical mmap regions into the
3100 * processor address space. Note that some shortcuts
3101 * are taken, but the code works.
3104 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3105 vm_pindex_t pindex, vm_size_t size)
3108 vm_paddr_t pa, ptepa;
3112 VM_OBJECT_ASSERT_WLOCKED(object);
3113 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3114 ("pmap_object_init_pt: non-device object"));
3116 (addr & (NBPDR - 1)) == 0 && (size & (NBPDR - 1)) == 0) {
3117 if (!vm_object_populate(object, pindex, pindex + atop(size)))
3119 p = vm_page_lookup(object, pindex);
3120 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3121 ("pmap_object_init_pt: invalid page %p", p));
3122 pat_mode = p->md.pat_mode;
3125 * Abort the mapping if the first page is not physically
3126 * aligned to a 2/4MB page boundary.
3128 ptepa = VM_PAGE_TO_PHYS(p);
3129 if (ptepa & (NBPDR - 1))
3133 * Skip the first page. Abort the mapping if the rest of
3134 * the pages are not physically contiguous or have differing
3135 * memory attributes.
3137 p = TAILQ_NEXT(p, listq);
3138 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size;
3140 KASSERT(p->valid == VM_PAGE_BITS_ALL,
3141 ("pmap_object_init_pt: invalid page %p", p));
3142 if (pa != VM_PAGE_TO_PHYS(p) ||
3143 pat_mode != p->md.pat_mode)
3145 p = TAILQ_NEXT(p, listq);
3149 * Map using 2/4MB pages. Since "ptepa" is 2/4M aligned and
3150 * "size" is a multiple of 2/4M, adding the PAT setting to
3151 * "pa" will not affect the termination of this loop.
3154 for (pa = ptepa | pmap_cache_bits(pat_mode, 1); pa < ptepa +
3155 size; pa += NBPDR) {
3156 pde = pmap_pde(pmap, addr);
3158 pde_store(pde, pa | PG_PS | PG_M | PG_A |
3159 PG_U | PG_RW | PG_V);
3160 pmap->pm_stats.resident_count += NBPDR /
3162 pmap_pde_mappings++;
3164 /* Else continue on if the PDE is already valid. */
3172 * Clear the wired attribute from the mappings for the specified range of
3173 * addresses in the given pmap. Every valid mapping within that range
3174 * must have the wired attribute set. In contrast, invalid mappings
3175 * cannot have the wired attribute set, so they are ignored.
3177 * The wired attribute of the page table entry is not a hardware feature,
3178 * so there is no need to invalidate any TLB entries.
3181 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3187 CTR3(KTR_PMAP, "pmap_unwire: pmap=%p sva=0x%x eva=0x%x", pmap, sva,
3189 rw_wlock(&pvh_global_lock);
3192 for (; sva < eva; sva = pdnxt) {
3193 pdnxt = (sva + NBPDR) & ~PDRMASK;
3196 pde = pmap_pde(pmap, sva);
3197 if ((*pde & PG_V) == 0)
3199 if ((*pde & PG_PS) != 0)
3200 panic("pmap_unwire: unexpected PG_PS in pde %#jx",
3204 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
3206 if ((*pte & PG_V) == 0)
3208 if ((*pte & PG_W) == 0)
3209 panic("pmap_unwire: pte %#jx is missing PG_W",
3211 PT_SET_VA_MA(pte, *pte & ~PG_W, FALSE);
3212 pmap->pm_stats.wired_count--;
3216 PT_CLEAR_VA(PMAP1, FALSE);
3219 rw_wunlock(&pvh_global_lock);
3225 * Copy the range specified by src_addr/len
3226 * from the source map to the range dst_addr/len
3227 * in the destination map.
3229 * This routine is only advisory and need not do anything.
3233 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
3234 vm_offset_t src_addr)
3238 vm_offset_t end_addr = src_addr + len;
3241 if (dst_addr != src_addr)
3244 if (!pmap_is_current(src_pmap)) {
3246 "pmap_copy, skipping: pdir[PTDPTDI]=0x%jx PTDpde[0]=0x%jx",
3247 (src_pmap->pm_pdir[PTDPTDI] & PG_FRAME), (PTDpde[0] & PG_FRAME));
3251 CTR5(KTR_PMAP, "pmap_copy: dst_pmap=%p src_pmap=%p dst_addr=0x%x len=%d src_addr=0x%x",
3252 dst_pmap, src_pmap, dst_addr, len, src_addr);
3254 #ifdef HAMFISTED_LOCKING
3255 mtx_lock(&createdelete_lock);
3258 rw_wlock(&pvh_global_lock);
3259 if (dst_pmap < src_pmap) {
3260 PMAP_LOCK(dst_pmap);
3261 PMAP_LOCK(src_pmap);
3263 PMAP_LOCK(src_pmap);
3264 PMAP_LOCK(dst_pmap);
3267 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3268 pt_entry_t *src_pte, *dst_pte;
3269 vm_page_t dstmpte, srcmpte;
3270 pd_entry_t srcptepaddr;
3273 KASSERT(addr < UPT_MIN_ADDRESS,
3274 ("pmap_copy: invalid to pmap_copy page tables"));
3276 pdnxt = (addr + NBPDR) & ~PDRMASK;
3279 ptepindex = addr >> PDRSHIFT;
3281 srcptepaddr = PT_GET(&src_pmap->pm_pdir[ptepindex]);
3282 if (srcptepaddr == 0)
3285 if (srcptepaddr & PG_PS) {
3286 if (dst_pmap->pm_pdir[ptepindex] == 0) {
3287 PD_SET_VA(dst_pmap, ptepindex, srcptepaddr & ~PG_W, TRUE);
3288 dst_pmap->pm_stats.resident_count +=
3294 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr & PG_FRAME);
3295 KASSERT(srcmpte->wire_count > 0,
3296 ("pmap_copy: source page table page is unused"));
3298 if (pdnxt > end_addr)
3301 src_pte = vtopte(addr);
3302 while (addr < pdnxt) {
3306 * we only virtual copy managed pages
3308 if ((ptetemp & PG_MANAGED) != 0) {
3309 dstmpte = pmap_allocpte(dst_pmap, addr,
3310 PMAP_ENTER_NOSLEEP);
3311 if (dstmpte == NULL)
3313 dst_pte = pmap_pte_quick(dst_pmap, addr);
3314 if (*dst_pte == 0 &&
3315 pmap_try_insert_pv_entry(dst_pmap, addr,
3316 PHYS_TO_VM_PAGE(xpmap_mtop(ptetemp) & PG_FRAME))) {
3318 * Clear the wired, modified, and
3319 * accessed (referenced) bits
3322 KASSERT(ptetemp != 0, ("src_pte not set"));
3323 PT_SET_VA_MA(dst_pte, ptetemp & ~(PG_W | PG_M | PG_A), TRUE /* XXX debug */);
3324 KASSERT(*dst_pte == (ptetemp & ~(PG_W | PG_M | PG_A)),
3325 ("no pmap copy expected: 0x%jx saw: 0x%jx",
3326 ptetemp & ~(PG_W | PG_M | PG_A), *dst_pte));
3327 dst_pmap->pm_stats.resident_count++;
3330 if (pmap_unwire_ptp(dst_pmap, dstmpte,
3332 pmap_invalidate_page(dst_pmap,
3334 pmap_free_zero_pages(free);
3338 if (dstmpte->wire_count >= srcmpte->wire_count)
3348 rw_wunlock(&pvh_global_lock);
3349 PMAP_UNLOCK(src_pmap);
3350 PMAP_UNLOCK(dst_pmap);
3352 #ifdef HAMFISTED_LOCKING
3353 mtx_unlock(&createdelete_lock);
3357 static __inline void
3358 pagezero(void *page)
3360 #if defined(I686_CPU)
3361 if (cpu_class == CPUCLASS_686) {
3362 #if defined(CPU_ENABLE_SSE)
3363 if (cpu_feature & CPUID_SSE2)
3364 sse2_pagezero(page);
3367 i686_pagezero(page);
3370 bzero(page, PAGE_SIZE);
3374 * pmap_zero_page zeros the specified hardware page by mapping
3375 * the page into KVM and using bzero to clear its contents.
3378 pmap_zero_page(vm_page_t m)
3380 struct sysmaps *sysmaps;
3382 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3383 mtx_lock(&sysmaps->lock);
3384 if (*sysmaps->CMAP2)
3385 panic("pmap_zero_page: CMAP2 busy");
3387 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | VM_PAGE_TO_MACH(m) | PG_A | PG_M);
3388 pagezero(sysmaps->CADDR2);
3389 PT_SET_MA(sysmaps->CADDR2, 0);
3391 mtx_unlock(&sysmaps->lock);
3395 * pmap_zero_page_area zeros the specified hardware page by mapping
3396 * the page into KVM and using bzero to clear its contents.
3398 * off and size may not cover an area beyond a single hardware page.
3401 pmap_zero_page_area(vm_page_t m, int off, int size)
3403 struct sysmaps *sysmaps;
3405 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3406 mtx_lock(&sysmaps->lock);
3407 if (*sysmaps->CMAP2)
3408 panic("pmap_zero_page_area: CMAP2 busy");
3410 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | VM_PAGE_TO_MACH(m) | PG_A | PG_M);
3412 if (off == 0 && size == PAGE_SIZE)
3413 pagezero(sysmaps->CADDR2);
3415 bzero((char *)sysmaps->CADDR2 + off, size);
3416 PT_SET_MA(sysmaps->CADDR2, 0);
3418 mtx_unlock(&sysmaps->lock);
3422 * pmap_zero_page_idle zeros the specified hardware page by mapping
3423 * the page into KVM and using bzero to clear its contents. This
3424 * is intended to be called from the vm_pagezero process only and
3428 pmap_zero_page_idle(vm_page_t m)
3432 panic("pmap_zero_page_idle: CMAP3 busy");
3434 PT_SET_MA(CADDR3, PG_V | PG_RW | VM_PAGE_TO_MACH(m) | PG_A | PG_M);
3436 PT_SET_MA(CADDR3, 0);
3441 * pmap_copy_page copies the specified (machine independent)
3442 * page by mapping the page into virtual memory and using
3443 * bcopy to copy the page, one machine dependent page at a
3447 pmap_copy_page(vm_page_t src, vm_page_t dst)
3449 struct sysmaps *sysmaps;
3451 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3452 mtx_lock(&sysmaps->lock);
3453 if (*sysmaps->CMAP1)
3454 panic("pmap_copy_page: CMAP1 busy");
3455 if (*sysmaps->CMAP2)
3456 panic("pmap_copy_page: CMAP2 busy");
3458 PT_SET_MA(sysmaps->CADDR1, PG_V | VM_PAGE_TO_MACH(src) | PG_A);
3459 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | VM_PAGE_TO_MACH(dst) | PG_A | PG_M);
3460 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
3461 PT_SET_MA(sysmaps->CADDR1, 0);
3462 PT_SET_MA(sysmaps->CADDR2, 0);
3464 mtx_unlock(&sysmaps->lock);
3467 int unmapped_buf_allowed = 1;
3470 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
3471 vm_offset_t b_offset, int xfersize)
3473 struct sysmaps *sysmaps;
3474 vm_page_t a_pg, b_pg;
3476 vm_offset_t a_pg_offset, b_pg_offset;
3479 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3480 mtx_lock(&sysmaps->lock);
3481 if (*sysmaps->CMAP1 != 0)
3482 panic("pmap_copy_pages: CMAP1 busy");
3483 if (*sysmaps->CMAP2 != 0)
3484 panic("pmap_copy_pages: CMAP2 busy");
3486 while (xfersize > 0) {
3487 a_pg = ma[a_offset >> PAGE_SHIFT];
3488 a_pg_offset = a_offset & PAGE_MASK;
3489 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
3490 b_pg = mb[b_offset >> PAGE_SHIFT];
3491 b_pg_offset = b_offset & PAGE_MASK;
3492 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
3493 PT_SET_MA(sysmaps->CADDR1, PG_V | VM_PAGE_TO_MACH(a_pg) | PG_A);
3494 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW |
3495 VM_PAGE_TO_MACH(b_pg) | PG_A | PG_M);
3496 a_cp = sysmaps->CADDR1 + a_pg_offset;
3497 b_cp = sysmaps->CADDR2 + b_pg_offset;
3498 bcopy(a_cp, b_cp, cnt);
3503 PT_SET_MA(sysmaps->CADDR1, 0);
3504 PT_SET_MA(sysmaps->CADDR2, 0);
3506 mtx_unlock(&sysmaps->lock);
3510 * Returns true if the pmap's pv is one of the first
3511 * 16 pvs linked to from this page. This count may
3512 * be changed upwards or downwards in the future; it
3513 * is only necessary that true be returned for a small
3514 * subset of pmaps for proper page aging.
3517 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3523 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3524 ("pmap_page_exists_quick: page %p is not managed", m));
3526 rw_wlock(&pvh_global_lock);
3527 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
3528 if (PV_PMAP(pv) == pmap) {
3536 rw_wunlock(&pvh_global_lock);
3541 * pmap_page_wired_mappings:
3543 * Return the number of managed mappings to the given physical page
3547 pmap_page_wired_mappings(vm_page_t m)
3555 if ((m->oflags & VPO_UNMANAGED) != 0)
3557 rw_wlock(&pvh_global_lock);
3559 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
3562 pte = pmap_pte_quick(pmap, pv->pv_va);
3563 if ((*pte & PG_W) != 0)
3568 rw_wunlock(&pvh_global_lock);
3573 * Returns TRUE if the given page is mapped. Otherwise, returns FALSE.
3576 pmap_page_is_mapped(vm_page_t m)
3579 if ((m->oflags & VPO_UNMANAGED) != 0)
3581 return (!TAILQ_EMPTY(&m->md.pv_list));
3585 * Remove all pages from specified address space
3586 * this aids process exit speeds. Also, this code
3587 * is special cased for current process only, but
3588 * can have the more generic (and slightly slower)
3589 * mode enabled. This is much faster than pmap_remove
3590 * in the case of running down an entire address space.
3593 pmap_remove_pages(pmap_t pmap)
3595 pt_entry_t *pte, tpte;
3596 vm_page_t m, free = NULL;
3598 struct pv_chunk *pc, *npc;
3601 uint32_t inuse, bitmask;
3604 CTR1(KTR_PMAP, "pmap_remove_pages: pmap=%p", pmap);
3606 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
3607 printf("warning: pmap_remove_pages called with non-current pmap\n");
3610 rw_wlock(&pvh_global_lock);
3611 KASSERT(pmap_is_current(pmap), ("removing pages from non-current pmap"));
3614 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
3615 KASSERT(pc->pc_pmap == pmap, ("Wrong pmap %p %p", pmap,
3618 for (field = 0; field < _NPCM; field++) {
3619 inuse = ~pc->pc_map[field] & pc_freemask[field];
3620 while (inuse != 0) {
3622 bitmask = 1UL << bit;
3623 idx = field * 32 + bit;
3624 pv = &pc->pc_pventry[idx];
3627 pte = vtopte(pv->pv_va);
3628 tpte = *pte ? xpmap_mtop(*pte) : 0;
3632 "TPTE at %p IS ZERO @ VA %08x\n",
3638 * We cannot remove wired pages from a process' mapping at this time
3645 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3646 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3647 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
3648 m, (uintmax_t)m->phys_addr,
3651 KASSERT(m < &vm_page_array[vm_page_array_size],
3652 ("pmap_remove_pages: bad tpte %#jx",
3656 PT_CLEAR_VA(pte, FALSE);
3659 * Update the vm_page_t clean/reference bits.
3664 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
3665 if (TAILQ_EMPTY(&m->md.pv_list))
3666 vm_page_aflag_clear(m, PGA_WRITEABLE);
3668 pmap_unuse_pt(pmap, pv->pv_va, &free);
3671 PV_STAT(pv_entry_frees++);
3672 PV_STAT(pv_entry_spare++);
3674 pc->pc_map[field] |= bitmask;
3675 pmap->pm_stats.resident_count--;
3680 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3686 PT_SET_MA(PADDR1, 0);
3689 pmap_invalidate_all(pmap);
3690 rw_wunlock(&pvh_global_lock);
3692 pmap_free_zero_pages(free);
3698 * Return whether or not the specified physical page was modified
3699 * in any physical maps.
3702 pmap_is_modified(vm_page_t m)
3709 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3710 ("pmap_is_modified: page %p is not managed", m));
3714 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
3715 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
3716 * is clear, no PTEs can have PG_M set.
3718 VM_OBJECT_ASSERT_WLOCKED(m->object);
3719 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
3721 rw_wlock(&pvh_global_lock);
3723 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
3726 pte = pmap_pte_quick(pmap, pv->pv_va);
3727 rv = (*pte & PG_M) != 0;
3733 PT_SET_MA(PADDR1, 0);
3735 rw_wunlock(&pvh_global_lock);
3740 * pmap_is_prefaultable:
3742 * Return whether or not the specified virtual address is elgible
3746 pmap_is_prefaultable_locked(pmap_t pmap, vm_offset_t addr)
3749 boolean_t rv = FALSE;
3753 if (pmap_is_current(pmap) && *pmap_pde(pmap, addr)) {
3761 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3766 rv = pmap_is_prefaultable_locked(pmap, addr);
3772 pmap_is_referenced(vm_page_t m)
3779 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3780 ("pmap_is_referenced: page %p is not managed", m));
3782 rw_wlock(&pvh_global_lock);
3784 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
3787 pte = pmap_pte_quick(pmap, pv->pv_va);
3788 rv = (*pte & (PG_A | PG_V)) == (PG_A | PG_V);
3794 PT_SET_MA(PADDR1, 0);
3796 rw_wunlock(&pvh_global_lock);
3801 pmap_map_readonly(pmap_t pmap, vm_offset_t va, int len)
3803 int i, npages = round_page(len) >> PAGE_SHIFT;
3804 for (i = 0; i < npages; i++) {
3806 pte = pmap_pte(pmap, (vm_offset_t)(va + i*PAGE_SIZE));
3807 rw_wlock(&pvh_global_lock);
3808 pte_store(pte, xpmap_mtop(*pte & ~(PG_RW|PG_M)));
3809 rw_wunlock(&pvh_global_lock);
3810 PMAP_MARK_PRIV(xpmap_mtop(*pte));
3811 pmap_pte_release(pte);
3816 pmap_map_readwrite(pmap_t pmap, vm_offset_t va, int len)
3818 int i, npages = round_page(len) >> PAGE_SHIFT;
3819 for (i = 0; i < npages; i++) {
3821 pte = pmap_pte(pmap, (vm_offset_t)(va + i*PAGE_SIZE));
3822 PMAP_MARK_UNPRIV(xpmap_mtop(*pte));
3823 rw_wlock(&pvh_global_lock);
3824 pte_store(pte, xpmap_mtop(*pte) | (PG_RW|PG_M));
3825 rw_wunlock(&pvh_global_lock);
3826 pmap_pte_release(pte);
3831 * Clear the write and modified bits in each of the given page's mappings.
3834 pmap_remove_write(vm_page_t m)
3838 pt_entry_t oldpte, *pte;
3840 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3841 ("pmap_remove_write: page %p is not managed", m));
3844 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
3845 * set by another thread while the object is locked. Thus,
3846 * if PGA_WRITEABLE is clear, no page table entries need updating.
3848 VM_OBJECT_ASSERT_WLOCKED(m->object);
3849 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
3851 rw_wlock(&pvh_global_lock);
3853 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
3856 pte = pmap_pte_quick(pmap, pv->pv_va);
3859 if ((oldpte & PG_RW) != 0) {
3860 vm_paddr_t newpte = oldpte & ~(PG_RW | PG_M);
3863 * Regardless of whether a pte is 32 or 64 bits
3864 * in size, PG_RW and PG_M are among the least
3865 * significant 32 bits.
3867 PT_SET_VA_MA(pte, newpte, TRUE);
3871 if ((oldpte & PG_M) != 0)
3873 pmap_invalidate_page(pmap, pv->pv_va);
3877 vm_page_aflag_clear(m, PGA_WRITEABLE);
3880 PT_SET_MA(PADDR1, 0);
3882 rw_wunlock(&pvh_global_lock);
3886 * pmap_ts_referenced:
3888 * Return a count of reference bits for a page, clearing those bits.
3889 * It is not necessary for every reference bit to be cleared, but it
3890 * is necessary that 0 only be returned when there are truly no
3891 * reference bits set.
3893 * XXX: The exact number of bits to check and clear is a matter that
3894 * should be tested and standardized at some point in the future for
3895 * optimal aging of shared pages.
3898 pmap_ts_referenced(vm_page_t m)
3900 pv_entry_t pv, pvf, pvn;
3905 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3906 ("pmap_ts_referenced: page %p is not managed", m));
3907 rw_wlock(&pvh_global_lock);
3909 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3912 pvn = TAILQ_NEXT(pv, pv_next);
3913 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
3914 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3917 pte = pmap_pte_quick(pmap, pv->pv_va);
3918 if ((*pte & PG_A) != 0) {
3919 PT_SET_VA_MA(pte, *pte & ~PG_A, FALSE);
3920 pmap_invalidate_page(pmap, pv->pv_va);
3926 } while ((pv = pvn) != NULL && pv != pvf);
3930 PT_SET_MA(PADDR1, 0);
3932 rw_wunlock(&pvh_global_lock);
3937 * Apply the given advice to the specified range of addresses within the
3938 * given pmap. Depending on the advice, clear the referenced and/or
3939 * modified flags in each mapping and set the mapped page's dirty field.
3942 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
3948 boolean_t anychanged;
3950 if (advice != MADV_DONTNEED && advice != MADV_FREE)
3953 rw_wlock(&pvh_global_lock);
3956 for (; sva < eva; sva = pdnxt) {
3957 pdnxt = (sva + NBPDR) & ~PDRMASK;
3960 oldpde = pmap->pm_pdir[sva >> PDRSHIFT];
3961 if ((oldpde & (PG_PS | PG_V)) != PG_V)
3965 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
3967 if ((*pte & (PG_MANAGED | PG_V)) != (PG_MANAGED |
3970 else if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
3971 if (advice == MADV_DONTNEED) {
3973 * Future calls to pmap_is_modified()
3974 * can be avoided by making the page
3977 m = PHYS_TO_VM_PAGE(xpmap_mtop(*pte) &
3981 PT_SET_VA_MA(pte, *pte & ~(PG_M | PG_A), TRUE);
3982 } else if ((*pte & PG_A) != 0)
3983 PT_SET_VA_MA(pte, *pte & ~PG_A, TRUE);
3986 if ((*pte & PG_G) != 0)
3987 pmap_invalidate_page(pmap, sva);
3994 PT_SET_VA_MA(PMAP1, 0, TRUE);
3996 pmap_invalidate_all(pmap);
3998 rw_wunlock(&pvh_global_lock);
4003 * Clear the modify bits on the specified physical page.
4006 pmap_clear_modify(vm_page_t m)
4012 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4013 ("pmap_clear_modify: page %p is not managed", m));
4014 VM_OBJECT_ASSERT_WLOCKED(m->object);
4015 KASSERT(!vm_page_xbusied(m),
4016 ("pmap_clear_modify: page %p is exclusive busied", m));
4019 * If the page is not PGA_WRITEABLE, then no PTEs can have PG_M set.
4020 * If the object containing the page is locked and the page is not
4021 * exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
4023 if ((m->aflags & PGA_WRITEABLE) == 0)
4025 rw_wlock(&pvh_global_lock);
4027 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4030 pte = pmap_pte_quick(pmap, pv->pv_va);
4031 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
4033 * Regardless of whether a pte is 32 or 64 bits
4034 * in size, PG_M is among the least significant
4037 PT_SET_VA_MA(pte, *pte & ~PG_M, FALSE);
4038 pmap_invalidate_page(pmap, pv->pv_va);
4043 rw_wunlock(&pvh_global_lock);
4047 * Miscellaneous support routines follow
4051 * Map a set of physical memory pages into the kernel virtual
4052 * address space. Return a pointer to where it is mapped. This
4053 * routine is intended to be used for mapping device memory,
4057 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
4059 vm_offset_t va, offset;
4062 offset = pa & PAGE_MASK;
4063 size = round_page(offset + size);
4066 if (pa < KERNLOAD && pa + size <= KERNLOAD)
4069 va = kva_alloc(size);
4071 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4073 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
4074 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
4075 pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
4076 pmap_invalidate_cache_range(va, va + size);
4077 return ((void *)(va + offset));
4081 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
4084 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
4088 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
4091 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
4095 pmap_unmapdev(vm_offset_t va, vm_size_t size)
4097 vm_offset_t base, offset;
4099 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
4101 base = trunc_page(va);
4102 offset = va & PAGE_MASK;
4103 size = round_page(offset + size);
4104 kva_free(base, size);
4108 * Sets the memory attribute for the specified page.
4111 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
4114 m->md.pat_mode = ma;
4115 if ((m->flags & PG_FICTITIOUS) != 0)
4119 * If "m" is a normal page, flush it from the cache.
4120 * See pmap_invalidate_cache_range().
4122 * First, try to find an existing mapping of the page by sf
4123 * buffer. sf_buf_invalidate_cache() modifies mapping and
4124 * flushes the cache.
4126 if (sf_buf_invalidate_cache(m))
4130 * If page is not mapped by sf buffer, but CPU does not
4131 * support self snoop, map the page transient and do
4132 * invalidation. In the worst case, whole cache is flushed by
4133 * pmap_invalidate_cache_range().
4135 if ((cpu_feature & CPUID_SS) == 0)
4140 pmap_flush_page(vm_page_t m)
4142 struct sysmaps *sysmaps;
4143 vm_offset_t sva, eva;
4145 if ((cpu_feature & CPUID_CLFSH) != 0) {
4146 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4147 mtx_lock(&sysmaps->lock);
4148 if (*sysmaps->CMAP2)
4149 panic("pmap_flush_page: CMAP2 busy");
4151 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW |
4152 VM_PAGE_TO_MACH(m) | PG_A | PG_M |
4153 pmap_cache_bits(m->md.pat_mode, 0));
4154 invlcaddr(sysmaps->CADDR2);
4155 sva = (vm_offset_t)sysmaps->CADDR2;
4156 eva = sva + PAGE_SIZE;
4159 * Use mfence despite the ordering implied by
4160 * mtx_{un,}lock() because clflush is not guaranteed
4161 * to be ordered by any other instruction.
4164 for (; sva < eva; sva += cpu_clflush_line_size)
4167 PT_SET_MA(sysmaps->CADDR2, 0);
4169 mtx_unlock(&sysmaps->lock);
4171 pmap_invalidate_cache();
4175 * Changes the specified virtual address range's memory type to that given by
4176 * the parameter "mode". The specified virtual address range must be
4177 * completely contained within either the kernel map.
4179 * Returns zero if the change completed successfully, and either EINVAL or
4180 * ENOMEM if the change failed. Specifically, EINVAL is returned if some part
4181 * of the virtual address range was not mapped, and ENOMEM is returned if
4182 * there was insufficient memory available to complete the change.
4185 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
4187 vm_offset_t base, offset, tmpva;
4193 base = trunc_page(va);
4194 offset = va & PAGE_MASK;
4195 size = round_page(offset + size);
4197 /* Only supported on kernel virtual addresses. */
4198 if (base <= VM_MAXUSER_ADDRESS)
4201 /* 4MB pages and pages that aren't mapped aren't supported. */
4202 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) {
4203 pde = pmap_pde(kernel_pmap, tmpva);
4206 if ((*pde & PG_V) == 0)
4209 if ((*pte & PG_V) == 0)
4216 * Ok, all the pages exist and are 4k, so run through them updating
4219 for (tmpva = base; size > 0; ) {
4220 pte = vtopte(tmpva);
4223 * The cache mode bits are all in the low 32-bits of the
4224 * PTE, so we can just spin on updating the low 32-bits.
4227 opte = *(u_int *)pte;
4228 npte = opte & ~(PG_PTE_PAT | PG_NC_PCD | PG_NC_PWT);
4229 npte |= pmap_cache_bits(mode, 0);
4230 PT_SET_VA_MA(pte, npte, TRUE);
4231 } while (npte != opte && (*pte != npte));
4239 * Flush CPU caches to make sure any data isn't cached that
4240 * shouldn't be, etc.
4243 pmap_invalidate_range(kernel_pmap, base, tmpva);
4244 pmap_invalidate_cache_range(base, tmpva);
4250 * perform the pmap work for mincore
4253 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
4255 pt_entry_t *ptep, pte;
4261 ptep = pmap_pte(pmap, addr);
4262 pte = (ptep != NULL) ? PT_GET(ptep) : 0;
4263 pmap_pte_release(ptep);
4265 if ((pte & PG_V) != 0) {
4266 val |= MINCORE_INCORE;
4267 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
4268 val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
4269 if ((pte & PG_A) != 0)
4270 val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
4272 if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
4273 (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
4274 (pte & (PG_MANAGED | PG_V)) == (PG_MANAGED | PG_V)) {
4275 pa = pte & PG_FRAME;
4276 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
4277 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
4280 PA_UNLOCK_COND(*locked_pa);
4286 pmap_activate(struct thread *td)
4288 pmap_t pmap, oldpmap;
4293 pmap = vmspace_pmap(td->td_proc->p_vmspace);
4294 oldpmap = PCPU_GET(curpmap);
4295 cpuid = PCPU_GET(cpuid);
4297 CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
4298 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
4300 CPU_CLR(cpuid, &oldpmap->pm_active);
4301 CPU_SET(cpuid, &pmap->pm_active);
4304 cr3 = vtophys(pmap->pm_pdpt);
4306 cr3 = vtophys(pmap->pm_pdir);
4309 * pmap_activate is for the current thread on the current cpu
4311 td->td_pcb->pcb_cr3 = cr3;
4314 PCPU_SET(curpmap, pmap);
4319 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
4324 * Increase the starting virtual address of the given mapping if a
4325 * different alignment might result in more superpage mappings.
4328 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4329 vm_offset_t *addr, vm_size_t size)
4331 vm_offset_t superpage_offset;
4335 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
4336 offset += ptoa(object->pg_color);
4337 superpage_offset = offset & PDRMASK;
4338 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
4339 (*addr & PDRMASK) == superpage_offset)
4341 if ((*addr & PDRMASK) < superpage_offset)
4342 *addr = (*addr & ~PDRMASK) + superpage_offset;
4344 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
4351 int i, pdir, offset;
4356 * We need to remove the recursive mapping structure from all
4357 * our pmaps so that Xen doesn't get confused when it restores
4358 * the page tables. The recursive map lives at page directory
4359 * index PTDPTDI. We assume that the suspend code has stopped
4360 * the other vcpus (if any).
4362 LIST_FOREACH(pmap, &allpmaps, pm_list) {
4363 for (i = 0; i < 4; i++) {
4365 * Figure out which page directory (L2) page
4366 * contains this bit of the recursive map and
4367 * the offset within that page of the map
4370 pdir = (PTDPTDI + i) / NPDEPG;
4371 offset = (PTDPTDI + i) % NPDEPG;
4372 pdirma = pmap->pm_pdpt[pdir] & PG_FRAME;
4373 mu[i].ptr = pdirma + offset * sizeof(pd_entry_t);
4376 HYPERVISOR_mmu_update(mu, 4, NULL, DOMID_SELF);
4384 int i, pdir, offset;
4389 * Restore the recursive map that we removed on suspend.
4391 LIST_FOREACH(pmap, &allpmaps, pm_list) {
4392 for (i = 0; i < 4; i++) {
4394 * Figure out which page directory (L2) page
4395 * contains this bit of the recursive map and
4396 * the offset within that page of the map
4399 pdir = (PTDPTDI + i) / NPDEPG;
4400 offset = (PTDPTDI + i) % NPDEPG;
4401 pdirma = pmap->pm_pdpt[pdir] & PG_FRAME;
4402 mu[i].ptr = pdirma + offset * sizeof(pd_entry_t);
4403 mu[i].val = (pmap->pm_pdpt[i] & PG_FRAME) | PG_V;
4405 HYPERVISOR_mmu_update(mu, 4, NULL, DOMID_SELF);
4409 #if defined(PMAP_DEBUG)
4410 pmap_pid_dump(int pid)
4417 sx_slock(&allproc_lock);
4418 FOREACH_PROC_IN_SYSTEM(p) {
4419 if (p->p_pid != pid)
4425 pmap = vmspace_pmap(p->p_vmspace);
4426 for (i = 0; i < NPDEPTD; i++) {
4429 vm_offset_t base = i << PDRSHIFT;
4431 pde = &pmap->pm_pdir[i];
4432 if (pde && pmap_pde_v(pde)) {
4433 for (j = 0; j < NPTEPG; j++) {
4434 vm_offset_t va = base + (j << PAGE_SHIFT);
4435 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
4440 sx_sunlock(&allproc_lock);
4443 pte = pmap_pte(pmap, va);
4444 if (pte && pmap_pte_v(pte)) {
4448 m = PHYS_TO_VM_PAGE(pa & PG_FRAME);
4449 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
4450 va, pa, m->hold_count, m->wire_count, m->flags);
4465 sx_sunlock(&allproc_lock);
4472 static void pads(pmap_t pm);
4473 void pmap_pvdump(vm_paddr_t pa);
4475 /* print address space of pmap*/
4483 if (pm == kernel_pmap)
4485 for (i = 0; i < NPDEPTD; i++)
4487 for (j = 0; j < NPTEPG; j++) {
4488 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
4489 if (pm == kernel_pmap && va < KERNBASE)
4491 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
4493 ptep = pmap_pte(pm, va);
4494 if (pmap_pte_v(ptep))
4495 printf("%x:%x ", va, *ptep);
4501 pmap_pvdump(vm_paddr_t pa)
4507 printf("pa %x", pa);
4508 m = PHYS_TO_VM_PAGE(pa);
4509 TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
4511 printf(" -> pmap %p, va %x", (void *)pmap, pv->pv_va);