2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
8 * Copyright (c) 2005 Alan L. Cox <alc@cs.rice.edu>
11 * This code is derived from software contributed to Berkeley by
12 * the Systems Programming Group of the University of Utah Computer
13 * Science Department and William Jolitz of UUNET Technologies Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
46 * Copyright (c) 2003 Networks Associates Technology, Inc.
47 * All rights reserved.
49 * This software was developed for the FreeBSD Project by Jake Burkholder,
50 * Safeport Network Services, and Network Associates Laboratories, the
51 * Security Research Division of Network Associates, Inc. under
52 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
53 * CHATS research program.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
64 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 #include <sys/cdefs.h>
78 __FBSDID("$FreeBSD$");
81 * Manages physical address maps.
83 * In addition to hardware address maps, this
84 * module is called upon to provide software-use-only
85 * maps which may or may not be stored in the same
86 * form as hardware maps. These pseudo-maps are
87 * used to store intermediate results from copy
88 * operations to and from address spaces.
90 * Since the information managed by this module is
91 * also stored by the logical address mapping module,
92 * this module may throw away valid virtual-to-physical
93 * mappings at almost any time. However, invalidations
94 * of virtual-to-physical mappings must be done as
97 * In order to cope with hardware architectures which
98 * make virtual-to-physical map invalidates expensive,
99 * this module may delay invalidate or reduced protection
100 * operations until such time as they are actually
101 * necessary. This module is given full information as
102 * to which processors are currently using which maps,
103 * and to when physical maps must be made correct.
106 #define PMAP_DIAGNOSTIC
109 #include "opt_pmap.h"
110 #include "opt_msgbuf.h"
112 #include "opt_xbox.h"
114 #include <sys/param.h>
115 #include <sys/systm.h>
116 #include <sys/kernel.h>
118 #include <sys/lock.h>
119 #include <sys/malloc.h>
120 #include <sys/mman.h>
121 #include <sys/msgbuf.h>
122 #include <sys/mutex.h>
123 #include <sys/proc.h>
124 #include <sys/sf_buf.h>
126 #include <sys/vmmeter.h>
127 #include <sys/sched.h>
128 #include <sys/sysctl.h>
134 #include <vm/vm_param.h>
135 #include <vm/vm_kern.h>
136 #include <vm/vm_page.h>
137 #include <vm/vm_map.h>
138 #include <vm/vm_object.h>
139 #include <vm/vm_extern.h>
140 #include <vm/vm_pageout.h>
141 #include <vm/vm_pager.h>
144 #include <machine/cpu.h>
145 #include <machine/cputypes.h>
146 #include <machine/md_var.h>
147 #include <machine/pcb.h>
148 #include <machine/specialreg.h>
150 #include <machine/smp.h>
154 #include <machine/xbox.h>
157 #include <xen/interface/xen.h>
158 #include <xen/hypervisor.h>
159 #include <machine/xen/hypercall.h>
160 #include <machine/xen/xenvar.h>
161 #include <machine/xen/xenfunc.h>
163 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
164 #define CPU_ENABLE_SSE
167 #ifndef PMAP_SHPGPERPROC
168 #define PMAP_SHPGPERPROC 200
171 #if defined(DIAGNOSTIC)
172 #define PMAP_DIAGNOSTIC
175 #if !defined(PMAP_DIAGNOSTIC)
176 #define PMAP_INLINE __gnu89_inline
183 #define PV_STAT(x) do { x ; } while (0)
185 #define PV_STAT(x) do { } while (0)
188 #define pa_index(pa) ((pa) >> PDRSHIFT)
189 #define pa_to_pvh(pa) (&pv_table[pa_index(pa)])
192 * Get PDEs and PTEs for user/kernel address space
194 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
195 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
197 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
198 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
199 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
200 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
201 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
203 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
205 struct pmap kernel_pmap_store;
206 LIST_HEAD(pmaplist, pmap);
207 static struct pmaplist allpmaps;
208 static struct mtx allpmaps_lock;
210 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
211 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
212 int pgeflag = 0; /* PG_G or-in */
213 int pseflag = 0; /* PG_PS or-in */
216 vm_offset_t kernel_vm_end;
217 extern u_int32_t KERNend;
222 static uma_zone_t pdptzone;
227 * Data for the pv entry allocation mechanism
229 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
230 static struct md_page *pv_table;
231 static int shpgperproc = PMAP_SHPGPERPROC;
233 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
234 int pv_maxchunks; /* How many chunks we have KVA for */
235 vm_offset_t pv_vafree; /* freelist stored in the PTE */
238 * All those kernel PT submaps that BSD is so fond of
247 static struct sysmaps sysmaps_pcpu[MAXCPU];
248 pt_entry_t *CMAP1 = 0;
249 static pt_entry_t *CMAP3;
250 caddr_t CADDR1 = 0, ptvmmap = 0;
251 static caddr_t CADDR3;
252 struct msgbuf *msgbufp = 0;
257 static caddr_t crashdumpmap;
259 static pt_entry_t *PMAP1 = 0, *PMAP2;
260 static pt_entry_t *PADDR1 = 0, *PADDR2;
263 static int PMAP1changedcpu;
264 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
266 "Number of times pmap_pte_quick changed CPU with same PMAP1");
268 static int PMAP1changed;
269 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
271 "Number of times pmap_pte_quick changed PMAP1");
272 static int PMAP1unchanged;
273 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
275 "Number of times pmap_pte_quick didn't change PMAP1");
276 static struct mtx PMAP2mutex;
278 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
279 static int pg_ps_enabled;
280 SYSCTL_INT(_vm_pmap, OID_AUTO, pg_ps_enabled, CTLFLAG_RD, &pg_ps_enabled, 0,
281 "Are large page mappings enabled?");
283 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
284 "Max number of PV entries");
285 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
286 "Page share factor per proc");
288 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
289 static pv_entry_t get_pv_entry(pmap_t locked_pmap, int try);
291 static vm_page_t pmap_enter_quick_locked(multicall_entry_t **mcl, int *count, pmap_t pmap, vm_offset_t va,
292 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
293 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva,
295 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va,
297 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
299 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
300 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
303 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
305 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags);
306 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free);
307 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
308 static void pmap_pte_release(pt_entry_t *pte);
309 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t *);
310 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
311 static boolean_t pmap_is_prefaultable_locked(pmap_t pmap, vm_offset_t addr);
312 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
315 #if defined(PAE) && !defined(XEN)
316 static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
319 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
320 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
323 * If you get an error here, then you set KVA_PAGES wrong! See the
324 * description of KVA_PAGES in sys/i386/include/pmap.h. It must be
325 * multiple of 4 for a normal kernel, or a multiple of 8 for a PAE.
327 CTASSERT(KERNBASE % (1 << 24) == 0);
334 #if defined(I686_CPU)
335 if (cpu_class == CPUCLASS_686) {
336 #if defined(CPU_ENABLE_SSE)
337 if (cpu_feature & CPUID_SSE2)
344 bzero(page, PAGE_SIZE);
348 pd_set(struct pmap *pmap, int ptepindex, vm_paddr_t val, int type)
350 vm_paddr_t pdir_ma = vtomach(&pmap->pm_pdir[ptepindex]);
355 xen_queue_pt_update(shadow_pdir_ma,
356 xpmap_ptom(val & ~(PG_RW)));
358 xen_queue_pt_update(pdir_ma,
361 case SH_PD_SET_VA_MA:
363 xen_queue_pt_update(shadow_pdir_ma,
366 xen_queue_pt_update(pdir_ma, val);
368 case SH_PD_SET_VA_CLEAR:
370 xen_queue_pt_update(shadow_pdir_ma, 0);
372 xen_queue_pt_update(pdir_ma, 0);
378 * Move the kernel virtual free pointer to the next
379 * 4MB. This is used to help improve performance
380 * by using a large (4MB) page for much of the kernel
381 * (.text, .data, .bss)
384 pmap_kmem_choose(vm_offset_t addr)
386 vm_offset_t newaddr = addr;
389 if (cpu_feature & CPUID_PSE)
390 newaddr = (addr + PDRMASK) & ~PDRMASK;
396 * Bootstrap the system enough to run with virtual memory.
398 * On the i386 this is called after mapping has already been enabled
399 * and just syncs the pmap module with what has already been done.
400 * [We can't call it easily with mapping off since the kernel is not
401 * mapped with PA == VA, hence we would have to relocate every address
402 * from the linked base (virtual) address "KERNBASE" to the actual
403 * (physical) address starting relative to 0]
406 pmap_bootstrap(vm_paddr_t firstaddr)
409 pt_entry_t *pte, *unused;
410 struct sysmaps *sysmaps;
414 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
415 * large. It should instead be correctly calculated in locore.s and
416 * not based on 'first' (which is a physical address, not a virtual
417 * address, for the start of unused physical memory). The kernel
418 * page tables are NOT double mapped and thus should not be included
419 * in this calculation.
421 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
422 virtual_avail = pmap_kmem_choose(virtual_avail);
424 virtual_end = VM_MAX_KERNEL_ADDRESS;
427 * Initialize the kernel pmap (which is statically allocated).
429 PMAP_LOCK_INIT(kernel_pmap);
430 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
432 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
434 kernel_pmap->pm_active = -1; /* don't allow deactivation */
435 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
436 LIST_INIT(&allpmaps);
437 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
438 mtx_lock_spin(&allpmaps_lock);
439 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
440 mtx_unlock_spin(&allpmaps_lock);
445 * Reserve some special page table entries/VA space for temporary
448 #define SYSMAP(c, p, v, n) \
449 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
455 * CMAP1/CMAP2 are used for zeroing and copying pages.
456 * CMAP3 is used for the idle process page zeroing.
458 for (i = 0; i < MAXCPU; i++) {
459 sysmaps = &sysmaps_pcpu[i];
460 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
461 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
462 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
464 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
465 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
466 PT_SET_MA(CADDR3, 0);
471 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
474 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
476 SYSMAP(caddr_t, unused, ptvmmap, 1)
479 * msgbufp is used to map the system message buffer.
481 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(MSGBUF_SIZE)))
484 * ptemap is used for pmap_pte_quick
486 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1);
487 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1);
489 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
492 PT_SET_MA(CADDR1, 0);
495 * Leave in place an identity mapping (virt == phys) for the low 1 MB
496 * physical memory region that is used by the ACPI wakeup code. This
497 * mapping must not have PG_G set.
501 * leave here deliberately to show that this is not supported
504 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
505 * an early stadium, we cannot yet neatly map video memory ... :-(
506 * Better fixes are very welcome! */
507 if (!arch_i386_is_xbox)
509 for (i = 1; i < NKPT; i++)
512 /* Initialize the PAT MSR if present. */
515 /* Turn on PG_G on kernel page(s) */
528 /* Bail if this CPU doesn't implement PAT. */
529 if (!(cpu_feature & CPUID_PAT))
534 * Leave the indices 0-3 at the default of WB, WT, UC, and UC-.
535 * Program 4 and 5 as WP and WC.
536 * Leave 6 and 7 as UC and UC-.
538 pat_msr = rdmsr(MSR_PAT);
539 pat_msr &= ~(PAT_MASK(4) | PAT_MASK(5));
540 pat_msr |= PAT_VALUE(4, PAT_WRITE_PROTECTED) |
541 PAT_VALUE(5, PAT_WRITE_COMBINING);
544 * Due to some Intel errata, we can only safely use the lower 4
545 * PAT entries. Thus, just replace PAT Index 2 with WC instead
548 * Intel Pentium III Processor Specification Update
549 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
552 * Intel Pentium IV Processor Specification Update
553 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
555 pat_msr = rdmsr(MSR_PAT);
556 pat_msr &= ~PAT_MASK(2);
557 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
559 wrmsr(MSR_PAT, pat_msr);
563 * Set PG_G on kernel pages. Only the BSP calls this when SMP is turned on.
570 vm_offset_t va, endva;
577 endva = KERNBASE + KERNend;
580 va = KERNBASE + KERNLOAD;
582 pdir = kernel_pmap->pm_pdir[KPTDI+i];
584 kernel_pmap->pm_pdir[KPTDI+i] = PTD[KPTDI+i] = pdir;
585 invltlb(); /* Play it safe, invltlb() every time */
590 va = (vm_offset_t)btext;
595 invltlb(); /* Play it safe, invltlb() every time */
602 * Initialize a vm_page's machine-dependent fields.
605 pmap_page_init(vm_page_t m)
608 TAILQ_INIT(&m->md.pv_list);
609 m->md.pat_mode = PAT_WRITE_BACK;
612 #if defined(PAE) && !defined(XEN)
614 pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
617 /* Inform UMA that this allocator uses kernel_map/object. */
618 *flags = UMA_SLAB_KERNEL;
619 return ((void *)kmem_alloc_contig(kernel_map, bytes, wait, 0x0ULL,
620 0xffffffffULL, 1, 0, VM_MEMATTR_DEFAULT));
625 * ABuse the pte nodes for unmapped kva to thread a kva freelist through.
627 * - Must deal with pages in order to ensure that none of the PG_* bits
628 * are ever set, PG_V in particular.
629 * - Assumes we can write to ptes without pte_store() atomic ops, even
630 * on PAE systems. This should be ok.
631 * - Assumes nothing will ever test these addresses for 0 to indicate
632 * no mapping instead of correctly checking PG_V.
633 * - Assumes a vm_offset_t will fit in a pte (true for i386).
634 * Because PG_V is never set, there can be no mappings to invalidate.
636 static int ptelist_count = 0;
638 pmap_ptelist_alloc(vm_offset_t *head)
641 vm_offset_t *phead = (vm_offset_t *)*head;
643 if (ptelist_count == 0) {
644 printf("out of memory!!!!!!\n");
645 return (0); /* Out of memory */
648 va = phead[ptelist_count];
653 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
655 vm_offset_t *phead = (vm_offset_t *)*head;
657 phead[ptelist_count++] = va;
661 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
667 nstackpages = (npages + PAGE_SIZE/sizeof(vm_offset_t) - 1)/ (PAGE_SIZE/sizeof(vm_offset_t));
668 for (i = 0; i < nstackpages; i++) {
669 va = (vm_offset_t)base + i * PAGE_SIZE;
670 m = vm_page_alloc(NULL, i,
671 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
673 pmap_qenter(va, &m, 1);
676 *head = (vm_offset_t)base;
677 for (i = npages - 1; i >= nstackpages; i--) {
678 va = (vm_offset_t)base + i * PAGE_SIZE;
679 pmap_ptelist_free(head, va);
685 * Initialize the pmap module.
686 * Called by vm_init, to initialize any structures that the pmap
687 * system needs to map virtual memory.
697 * Initialize the vm page array entries for the kernel pmap's
700 for (i = 0; i < nkpt; i++) {
701 mpte = PHYS_TO_VM_PAGE(xpmap_mtop(PTD[i + KPTDI] & PG_FRAME));
702 KASSERT(mpte >= vm_page_array &&
703 mpte < &vm_page_array[vm_page_array_size],
704 ("pmap_init: page table page is out of range"));
705 mpte->pindex = i + KPTDI;
706 mpte->phys_addr = xpmap_mtop(PTD[i + KPTDI] & PG_FRAME);
710 * Initialize the address space (zone) for the pv entries. Set a
711 * high water mark so that the system can recover from excessive
712 * numbers of pv entries.
714 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
715 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
716 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
717 pv_entry_max = roundup(pv_entry_max, _NPCPV);
718 pv_entry_high_water = 9 * (pv_entry_max / 10);
721 * Are large page mappings enabled?
723 TUNABLE_INT_FETCH("vm.pmap.pg_ps_enabled", &pg_ps_enabled);
726 * Calculate the size of the pv head table for superpages.
728 for (i = 0; phys_avail[i + 1]; i += 2);
729 pv_npg = round_4mpage(phys_avail[(i - 2) + 1]) / NBPDR;
732 * Allocate memory for the pv head table for superpages.
734 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
736 pv_table = (struct md_page *)kmem_alloc(kernel_map, s);
737 for (i = 0; i < pv_npg; i++)
738 TAILQ_INIT(&pv_table[i].pv_list);
740 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
741 pv_chunkbase = (struct pv_chunk *)kmem_alloc_nofault(kernel_map,
742 PAGE_SIZE * pv_maxchunks);
743 if (pv_chunkbase == NULL)
744 panic("pmap_init: not enough kvm for pv chunks");
745 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
746 #if defined(PAE) && !defined(XEN)
747 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
748 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
749 UMA_ZONE_VM | UMA_ZONE_NOFREE);
750 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
755 /***************************************************
756 * Low level helper routines.....
757 ***************************************************/
760 * Determine the appropriate bits to set in a PTE or PDE for a specified
764 pmap_cache_bits(int mode, boolean_t is_pde)
766 int pat_flag, pat_index, cache_bits;
768 /* The PAT bit is different for PTE's and PDE's. */
769 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
771 /* If we don't support PAT, map extended modes to older ones. */
772 if (!(cpu_feature & CPUID_PAT)) {
774 case PAT_UNCACHEABLE:
775 case PAT_WRITE_THROUGH:
779 case PAT_WRITE_COMBINING:
780 case PAT_WRITE_PROTECTED:
781 mode = PAT_UNCACHEABLE;
786 /* Map the caching mode to a PAT index. */
789 case PAT_UNCACHEABLE:
792 case PAT_WRITE_THROUGH:
801 case PAT_WRITE_COMBINING:
804 case PAT_WRITE_PROTECTED:
809 case PAT_UNCACHEABLE:
810 case PAT_WRITE_PROTECTED:
813 case PAT_WRITE_THROUGH:
819 case PAT_WRITE_COMBINING:
824 panic("Unknown caching mode %d\n", mode);
827 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
830 cache_bits |= pat_flag;
832 cache_bits |= PG_NC_PCD;
834 cache_bits |= PG_NC_PWT;
839 * For SMP, these functions have to use the IPI mechanism for coherence.
841 * N.B.: Before calling any of the following TLB invalidation functions,
842 * the calling processor must ensure that all stores updating a non-
843 * kernel page table are globally performed. Otherwise, another
844 * processor could cache an old, pre-update entry without being
845 * invalidated. This can happen one of two ways: (1) The pmap becomes
846 * active on another processor after its pm_active field is checked by
847 * one of the following functions but before a store updating the page
848 * table is globally performed. (2) The pmap becomes active on another
849 * processor before its pm_active field is checked but due to
850 * speculative loads one of the following functions stills reads the
851 * pmap as inactive on the other processor.
853 * The kernel page table is exempt because its pm_active field is
854 * immutable. The kernel page table is always active on every
858 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
863 CTR2(KTR_PMAP, "pmap_invalidate_page: pmap=%p va=0x%x",
867 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
871 cpumask = PCPU_GET(cpumask);
872 other_cpus = PCPU_GET(other_cpus);
873 if (pmap->pm_active & cpumask)
875 if (pmap->pm_active & other_cpus)
876 smp_masked_invlpg(pmap->pm_active & other_cpus, va);
883 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
889 CTR3(KTR_PMAP, "pmap_invalidate_page: pmap=%p eva=0x%x sva=0x%x",
893 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
894 for (addr = sva; addr < eva; addr += PAGE_SIZE)
896 smp_invlpg_range(sva, eva);
898 cpumask = PCPU_GET(cpumask);
899 other_cpus = PCPU_GET(other_cpus);
900 if (pmap->pm_active & cpumask)
901 for (addr = sva; addr < eva; addr += PAGE_SIZE)
903 if (pmap->pm_active & other_cpus)
904 smp_masked_invlpg_range(pmap->pm_active & other_cpus,
912 pmap_invalidate_all(pmap_t pmap)
917 CTR1(KTR_PMAP, "pmap_invalidate_page: pmap=%p", pmap);
920 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
924 cpumask = PCPU_GET(cpumask);
925 other_cpus = PCPU_GET(other_cpus);
926 if (pmap->pm_active & cpumask)
928 if (pmap->pm_active & other_cpus)
929 smp_masked_invltlb(pmap->pm_active & other_cpus);
935 pmap_invalidate_cache(void)
945 * Normal, non-SMP, 486+ invalidation functions.
946 * We inline these within pmap.c for speed.
949 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
951 CTR2(KTR_PMAP, "pmap_invalidate_page: pmap=%p va=0x%x",
954 if (pmap == kernel_pmap || pmap->pm_active)
960 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
964 if (eva - sva > PAGE_SIZE)
965 CTR3(KTR_PMAP, "pmap_invalidate_range: pmap=%p sva=0x%x eva=0x%x",
968 if (pmap == kernel_pmap || pmap->pm_active)
969 for (addr = sva; addr < eva; addr += PAGE_SIZE)
975 pmap_invalidate_all(pmap_t pmap)
978 CTR1(KTR_PMAP, "pmap_invalidate_all: pmap=%p", pmap);
980 if (pmap == kernel_pmap || pmap->pm_active)
985 pmap_invalidate_cache(void)
993 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva)
996 KASSERT((sva & PAGE_MASK) == 0,
997 ("pmap_invalidate_cache_range: sva not page-aligned"));
998 KASSERT((eva & PAGE_MASK) == 0,
999 ("pmap_invalidate_cache_range: eva not page-aligned"));
1001 if (cpu_feature & CPUID_SS)
1002 ; /* If "Self Snoop" is supported, do nothing. */
1003 else if (cpu_feature & CPUID_CLFSH) {
1006 * Otherwise, do per-cache line flush. Use the mfence
1007 * instruction to insure that previous stores are
1008 * included in the write-back. The processor
1009 * propagates flush to other processors in the cache
1013 for (; sva < eva; sva += cpu_clflush_line_size)
1019 * No targeted cache flush methods are supported by CPU,
1020 * globally invalidate cache as a last resort.
1022 pmap_invalidate_cache();
1027 * Are we current address space or kernel? N.B. We return FALSE when
1028 * a pmap's page table is in use because a kernel thread is borrowing
1029 * it. The borrowed page table can change spontaneously, making any
1030 * dependence on its continued use subject to a race condition.
1033 pmap_is_current(pmap_t pmap)
1036 return (pmap == kernel_pmap ||
1037 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
1038 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
1042 * If the given pmap is not the current or kernel pmap, the returned pte must
1043 * be released by passing it to pmap_pte_release().
1046 pmap_pte(pmap_t pmap, vm_offset_t va)
1051 pde = pmap_pde(pmap, va);
1055 /* are we current address space or kernel? */
1056 if (pmap_is_current(pmap))
1057 return (vtopte(va));
1058 mtx_lock(&PMAP2mutex);
1059 newpf = *pde & PG_FRAME;
1060 if ((*PMAP2 & PG_FRAME) != newpf) {
1061 PT_SET_MA(PADDR2, newpf | PG_V | PG_A | PG_M);
1062 CTR3(KTR_PMAP, "pmap_pte: pmap=%p va=0x%x newpte=0x%08x",
1063 pmap, va, (*PMAP2 & 0xffffffff));
1066 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
1072 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
1075 static __inline void
1076 pmap_pte_release(pt_entry_t *pte)
1079 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2) {
1080 CTR1(KTR_PMAP, "pmap_pte_release: pte=0x%jx",
1082 PT_SET_VA(PMAP2, 0, TRUE);
1083 mtx_unlock(&PMAP2mutex);
1087 static __inline void
1088 invlcaddr(void *caddr)
1091 invlpg((u_int)caddr);
1096 * Super fast pmap_pte routine best used when scanning
1097 * the pv lists. This eliminates many coarse-grained
1098 * invltlb calls. Note that many of the pv list
1099 * scans are across different pmaps. It is very wasteful
1100 * to do an entire invltlb for checking a single mapping.
1102 * If the given pmap is not the current pmap, vm_page_queue_mtx
1103 * must be held and curthread pinned to a CPU.
1106 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
1111 pde = pmap_pde(pmap, va);
1115 /* are we current address space or kernel? */
1116 if (pmap_is_current(pmap))
1117 return (vtopte(va));
1118 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1119 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1120 newpf = *pde & PG_FRAME;
1121 if ((*PMAP1 & PG_FRAME) != newpf) {
1122 PT_SET_MA(PADDR1, newpf | PG_V | PG_A | PG_M);
1123 CTR3(KTR_PMAP, "pmap_pte_quick: pmap=%p va=0x%x newpte=0x%08x",
1124 pmap, va, (u_long)*PMAP1);
1127 PMAP1cpu = PCPU_GET(cpuid);
1132 if (PMAP1cpu != PCPU_GET(cpuid)) {
1133 PMAP1cpu = PCPU_GET(cpuid);
1139 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
1145 * Routine: pmap_extract
1147 * Extract the physical page address associated
1148 * with the given map/virtual_address pair.
1151 pmap_extract(pmap_t pmap, vm_offset_t va)
1160 pde = pmap->pm_pdir[va >> PDRSHIFT];
1162 if ((pde & PG_PS) != 0) {
1163 rtval = xpmap_mtop(pde & PG_PS_FRAME) | (va & PDRMASK);
1167 pte = pmap_pte(pmap, va);
1168 pteval = *pte ? xpmap_mtop(*pte) : 0;
1169 rtval = (pteval & PG_FRAME) | (va & PAGE_MASK);
1170 pmap_pte_release(pte);
1177 * Routine: pmap_extract_ma
1179 * Like pmap_extract, but returns machine address
1182 pmap_extract_ma(pmap_t pmap, vm_offset_t va)
1190 pde = pmap->pm_pdir[va >> PDRSHIFT];
1192 if ((pde & PG_PS) != 0) {
1193 rtval = (pde & ~PDRMASK) | (va & PDRMASK);
1197 pte = pmap_pte(pmap, va);
1198 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1199 pmap_pte_release(pte);
1206 * Routine: pmap_extract_and_hold
1208 * Atomically extract and hold the physical page
1209 * with the given pmap and virtual address pair
1210 * if that mapping permits the given protection.
1213 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1220 vm_page_lock_queues();
1222 pde = PT_GET(pmap_pde(pmap, va));
1225 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1226 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
1232 pte = PT_GET(pmap_pte_quick(pmap, va));
1234 PT_SET_MA(PADDR1, 0);
1236 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1237 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1243 vm_page_unlock_queues();
1248 /***************************************************
1249 * Low level mapping routines.....
1250 ***************************************************/
1253 * Add a wired page to the kva.
1254 * Note: not SMP coherent.
1257 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1259 PT_SET_MA(va, xpmap_ptom(pa)| PG_RW | PG_V | pgeflag);
1263 pmap_kenter_ma(vm_offset_t va, vm_paddr_t ma)
1268 pte_store_ma(pte, ma | PG_RW | PG_V | pgeflag);
1272 static __inline void
1273 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1275 PT_SET_MA(va, pa | PG_RW | PG_V | pgeflag | pmap_cache_bits(mode, 0));
1279 * Remove a page from the kernel pagetables.
1280 * Note: not SMP coherent.
1283 pmap_kremove(vm_offset_t va)
1288 PT_CLEAR_VA(pte, FALSE);
1292 * Used to map a range of physical addresses into kernel
1293 * virtual address space.
1295 * The value passed in '*virt' is a suggested virtual address for
1296 * the mapping. Architectures which can support a direct-mapped
1297 * physical to virtual region can return the appropriate address
1298 * within that region, leaving '*virt' unchanged. Other
1299 * architectures should map the pages starting at '*virt' and
1300 * update '*virt' with the first usable address after the mapped
1304 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1306 vm_offset_t va, sva;
1309 CTR4(KTR_PMAP, "pmap_map: va=0x%x start=0x%jx end=0x%jx prot=0x%x",
1310 va, start, end, prot);
1311 while (start < end) {
1312 pmap_kenter(va, start);
1316 pmap_invalidate_range(kernel_pmap, sva, va);
1323 * Add a list of wired pages to the kva
1324 * this routine is only used for temporary
1325 * kernel mappings that do not need to have
1326 * page modification or references recorded.
1327 * Note that old mappings are simply written
1328 * over. The page *must* be wired.
1329 * Note: SMP coherent. Uses a ranged shootdown IPI.
1332 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1334 pt_entry_t *endpte, *pte;
1336 vm_offset_t va = sva;
1338 multicall_entry_t mcl[16];
1339 multicall_entry_t *mclp = mcl;
1342 CTR2(KTR_PMAP, "pmap_qenter:sva=0x%x count=%d", va, count);
1344 endpte = pte + count;
1345 while (pte < endpte) {
1346 pa = xpmap_ptom(VM_PAGE_TO_PHYS(*ma)) | pgeflag | PG_RW | PG_V | PG_M | PG_A;
1348 mclp->op = __HYPERVISOR_update_va_mapping;
1350 mclp->args[1] = (uint32_t)(pa & 0xffffffff);
1351 mclp->args[2] = (uint32_t)(pa >> 32);
1352 mclp->args[3] = (*pte & PG_V) ? UVMF_INVLPG|UVMF_ALL : 0;
1359 if (mclcount == 16) {
1360 error = HYPERVISOR_multicall(mcl, mclcount);
1363 KASSERT(error == 0, ("bad multicall %d", error));
1367 error = HYPERVISOR_multicall(mcl, mclcount);
1368 KASSERT(error == 0, ("bad multicall %d", error));
1372 for (pte = vtopte(sva), mclcount = 0; mclcount < count; mclcount++, pte++)
1373 KASSERT(*pte, ("pte not set for va=0x%x", sva + mclcount*PAGE_SIZE));
1379 * This routine tears out page mappings from the
1380 * kernel -- it is meant only for temporary mappings.
1381 * Note: SMP coherent. Uses a ranged shootdown IPI.
1384 pmap_qremove(vm_offset_t sva, int count)
1388 CTR2(KTR_PMAP, "pmap_qremove: sva=0x%x count=%d", sva, count);
1390 vm_page_lock_queues();
1392 while (count-- > 0) {
1396 pmap_invalidate_range(kernel_pmap, sva, va);
1398 vm_page_unlock_queues();
1401 /***************************************************
1402 * Page table page management routines.....
1403 ***************************************************/
1404 static __inline void
1405 pmap_free_zero_pages(vm_page_t free)
1409 while (free != NULL) {
1412 vm_page_free_zero(m);
1417 * This routine unholds page table pages, and if the hold count
1418 * drops to zero, then it decrements the wire count.
1421 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free)
1425 if (m->wire_count == 0)
1426 return _pmap_unwire_pte_hold(pmap, m, free);
1432 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free)
1438 * unmap the page table page
1440 xen_pt_unpin(pmap->pm_pdir[m->pindex]);
1442 * page *might* contain residual mapping :-/
1444 PD_CLEAR_VA(pmap, m->pindex, TRUE);
1446 --pmap->pm_stats.resident_count;
1449 * This is a release store so that the ordinary store unmapping
1450 * the page table page is globally performed before TLB shoot-
1453 atomic_subtract_rel_int(&cnt.v_wire_count, 1);
1456 * Do an invltlb to make the invalidated mapping
1457 * take effect immediately.
1459 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1460 pmap_invalidate_page(pmap, pteva);
1463 * Put page on a list so that it is released after
1464 * *ALL* TLB shootdown is done
1473 * After removing a page table entry, this routine is used to
1474 * conditionally free the page, and manage the hold/wire counts.
1477 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t *free)
1482 if (va >= VM_MAXUSER_ADDRESS)
1484 ptepde = PT_GET(pmap_pde(pmap, va));
1485 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1486 return pmap_unwire_pte_hold(pmap, mpte, free);
1490 pmap_pinit0(pmap_t pmap)
1493 PMAP_LOCK_INIT(pmap);
1494 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1496 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1498 pmap->pm_active = 0;
1499 PCPU_SET(curpmap, pmap);
1500 TAILQ_INIT(&pmap->pm_pvchunk);
1501 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1502 mtx_lock_spin(&allpmaps_lock);
1503 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1504 mtx_unlock_spin(&allpmaps_lock);
1508 * Initialize a preallocated and zeroed pmap structure,
1509 * such as one in a vmspace structure.
1512 pmap_pinit(pmap_t pmap)
1514 vm_page_t m, ptdpg[NPGPTD + 1];
1515 int npgptd = NPGPTD + 1;
1519 PMAP_LOCK_INIT(pmap);
1522 * No need to allocate page table space yet but we do need a valid
1523 * page directory table.
1525 if (pmap->pm_pdir == NULL) {
1526 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map,
1528 if (pmap->pm_pdir == NULL) {
1529 PMAP_LOCK_DESTROY(pmap);
1532 #if defined(XEN) && defined(PAE)
1533 pmap->pm_pdpt = (pd_entry_t *)kmem_alloc_nofault(kernel_map, 1);
1536 #if defined(PAE) && !defined(XEN)
1537 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
1538 KASSERT(((vm_offset_t)pmap->pm_pdpt &
1539 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
1540 ("pmap_pinit: pdpt misaligned"));
1541 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
1542 ("pmap_pinit: pdpt above 4g"));
1547 * allocate the page directory page(s)
1549 for (i = 0; i < npgptd;) {
1550 m = vm_page_alloc(NULL, color++,
1551 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1559 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1560 for (i = 0; i < NPGPTD; i++) {
1561 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1562 pagezero(&pmap->pm_pdir[i*NPTEPG]);
1565 mtx_lock_spin(&allpmaps_lock);
1566 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1567 mtx_unlock_spin(&allpmaps_lock);
1568 /* Wire in kernel global address entries. */
1570 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1573 pmap_qenter((vm_offset_t)pmap->pm_pdpt, &ptdpg[NPGPTD], 1);
1574 if ((ptdpg[NPGPTD]->flags & PG_ZERO) == 0)
1575 bzero(pmap->pm_pdpt, PAGE_SIZE);
1577 for (i = 0; i < NPGPTD; i++) {
1580 ma = xpmap_ptom(VM_PAGE_TO_PHYS(ptdpg[i]));
1581 pmap->pm_pdpt[i] = ma | PG_V;
1586 for (i = 0; i < NPGPTD; i++) {
1590 ma = xpmap_ptom(VM_PAGE_TO_PHYS(ptdpg[i]));
1591 pd = pmap->pm_pdir + (i * NPDEPG);
1592 PT_SET_MA(pd, *vtopte((vm_offset_t)pd) & ~(PG_M|PG_A|PG_U|PG_RW));
1599 PT_SET_MA(pmap->pm_pdpt, *vtopte((vm_offset_t)pmap->pm_pdpt) & ~PG_RW);
1601 vm_page_lock_queues();
1603 xen_pgdpt_pin(xpmap_ptom(VM_PAGE_TO_PHYS(ptdpg[NPGPTD])));
1604 for (i = 0; i < NPGPTD; i++) {
1605 vm_paddr_t ma = xpmap_ptom(VM_PAGE_TO_PHYS(ptdpg[i]));
1606 PT_SET_VA_MA(&pmap->pm_pdir[PTDPTDI + i], ma | PG_V | PG_A, FALSE);
1609 vm_page_unlock_queues();
1611 pmap->pm_active = 0;
1612 TAILQ_INIT(&pmap->pm_pvchunk);
1613 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1619 * this routine is called if the page table page is not
1623 _pmap_allocpte(pmap_t pmap, unsigned int ptepindex, int flags)
1628 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1629 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1630 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1633 * Allocate a page table page.
1635 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1636 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1637 if (flags & M_WAITOK) {
1639 vm_page_unlock_queues();
1641 vm_page_lock_queues();
1646 * Indicate the need to retry. While waiting, the page table
1647 * page may have been allocated.
1651 if ((m->flags & PG_ZERO) == 0)
1655 * Map the pagetable page into the process address space, if
1656 * it isn't already there.
1658 pmap->pm_stats.resident_count++;
1660 ptema = xpmap_ptom(VM_PAGE_TO_PHYS(m));
1662 PT_SET_VA_MA(&pmap->pm_pdir[ptepindex],
1663 (ptema | PG_U | PG_RW | PG_V | PG_A | PG_M), TRUE);
1665 KASSERT(pmap->pm_pdir[ptepindex],
1666 ("_pmap_allocpte: ptepindex=%d did not get mapped", ptepindex));
1671 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1677 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1678 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1679 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1682 * Calculate pagetable page index
1684 ptepindex = va >> PDRSHIFT;
1687 * Get the page directory entry
1689 ptema = pmap->pm_pdir[ptepindex];
1692 * This supports switching from a 4MB page to a
1695 if (ptema & PG_PS) {
1699 pmap->pm_pdir[ptepindex] = 0;
1701 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1702 pmap_invalidate_all(kernel_pmap);
1706 * If the page table page is mapped, we just increment the
1707 * hold count, and activate it.
1710 m = PHYS_TO_VM_PAGE(xpmap_mtop(ptema) & PG_FRAME);
1714 * Here if the pte page isn't mapped, or if it has
1717 CTR3(KTR_PMAP, "pmap_allocpte: pmap=%p va=0x%08x flags=0x%x",
1719 m = _pmap_allocpte(pmap, ptepindex, flags);
1720 if (m == NULL && (flags & M_WAITOK))
1723 KASSERT(pmap->pm_pdir[ptepindex], ("ptepindex=%d did not get mapped", ptepindex));
1729 /***************************************************
1730 * Pmap allocation/deallocation routines.
1731 ***************************************************/
1735 * Deal with a SMP shootdown of other users of the pmap that we are
1736 * trying to dispose of. This can be a bit hairy.
1738 static u_int *lazymask;
1739 static u_int lazyptd;
1740 static volatile u_int lazywait;
1742 void pmap_lazyfix_action(void);
1745 pmap_lazyfix_action(void)
1747 u_int mymask = PCPU_GET(cpumask);
1750 (*ipi_lazypmap_counts[PCPU_GET(cpuid)])++;
1752 if (rcr3() == lazyptd)
1753 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1754 atomic_clear_int(lazymask, mymask);
1755 atomic_store_rel_int(&lazywait, 1);
1759 pmap_lazyfix_self(u_int mymask)
1762 if (rcr3() == lazyptd)
1763 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1764 atomic_clear_int(lazymask, mymask);
1769 pmap_lazyfix(pmap_t pmap)
1775 while ((mask = pmap->pm_active) != 0) {
1777 mask = mask & -mask; /* Find least significant set bit */
1778 mtx_lock_spin(&smp_ipi_mtx);
1780 lazyptd = vtophys(pmap->pm_pdpt);
1782 lazyptd = vtophys(pmap->pm_pdir);
1784 mymask = PCPU_GET(cpumask);
1785 if (mask == mymask) {
1786 lazymask = &pmap->pm_active;
1787 pmap_lazyfix_self(mymask);
1789 atomic_store_rel_int((u_int *)&lazymask,
1790 (u_int)&pmap->pm_active);
1791 atomic_store_rel_int(&lazywait, 0);
1792 ipi_selected(mask, IPI_LAZYPMAP);
1793 while (lazywait == 0) {
1799 mtx_unlock_spin(&smp_ipi_mtx);
1801 printf("pmap_lazyfix: spun for 50000000\n");
1808 * Cleaning up on uniprocessor is easy. For various reasons, we're
1809 * unlikely to have to even execute this code, including the fact
1810 * that the cleanup is deferred until the parent does a wait(2), which
1811 * means that another userland process has run.
1814 pmap_lazyfix(pmap_t pmap)
1818 cr3 = vtophys(pmap->pm_pdir);
1819 if (cr3 == rcr3()) {
1820 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1821 pmap->pm_active &= ~(PCPU_GET(cpumask));
1827 * Release any resources held by the given physical map.
1828 * Called when a pmap initialized by pmap_pinit is being released.
1829 * Should only be called if the map contains no valid mappings.
1832 pmap_release(pmap_t pmap)
1834 vm_page_t m, ptdpg[2*NPGPTD+1];
1839 int npgptd = NPGPTD + 1;
1841 int npgptd = NPGPTD;
1844 int npgptd = NPGPTD;
1846 KASSERT(pmap->pm_stats.resident_count == 0,
1847 ("pmap_release: pmap resident count %ld != 0",
1848 pmap->pm_stats.resident_count));
1852 mtx_lock_spin(&allpmaps_lock);
1853 LIST_REMOVE(pmap, pm_list);
1854 mtx_unlock_spin(&allpmaps_lock);
1856 for (i = 0; i < NPGPTD; i++)
1857 ptdpg[i] = PHYS_TO_VM_PAGE(vtophys(pmap->pm_pdir + (i*NPDEPG)) & PG_FRAME);
1858 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1859 #if defined(PAE) && defined(XEN)
1860 ptdpg[NPGPTD] = PHYS_TO_VM_PAGE(vtophys(pmap->pm_pdpt));
1863 for (i = 0; i < npgptd; i++) {
1865 ma = xpmap_ptom(VM_PAGE_TO_PHYS(m));
1866 /* unpinning L1 and L2 treated the same */
1869 KASSERT(xpmap_ptom(VM_PAGE_TO_PHYS(m)) == (pmap->pm_pdpt[i] & PG_FRAME),
1870 ("pmap_release: got wrong ptd page"));
1873 atomic_subtract_int(&cnt.v_wire_count, 1);
1876 PMAP_LOCK_DESTROY(pmap);
1880 kvm_size(SYSCTL_HANDLER_ARGS)
1882 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1884 return sysctl_handle_long(oidp, &ksize, 0, req);
1886 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1887 0, 0, kvm_size, "IU", "Size of KVM");
1890 kvm_free(SYSCTL_HANDLER_ARGS)
1892 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1894 return sysctl_handle_long(oidp, &kfree, 0, req);
1896 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1897 0, 0, kvm_free, "IU", "Amount of KVM free");
1900 * grow the number of kernel page table entries, if needed
1903 pmap_growkernel(vm_offset_t addr)
1906 vm_paddr_t ptppaddr;
1910 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1911 if (kernel_vm_end == 0) {
1912 kernel_vm_end = KERNBASE;
1914 while (pdir_pde(PTD, kernel_vm_end)) {
1915 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1917 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1918 kernel_vm_end = kernel_map->max_offset;
1923 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1924 if (addr - 1 >= kernel_map->max_offset)
1925 addr = kernel_map->max_offset;
1926 while (kernel_vm_end < addr) {
1927 if (pdir_pde(PTD, kernel_vm_end)) {
1928 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1929 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1930 kernel_vm_end = kernel_map->max_offset;
1937 * This index is bogus, but out of the way
1939 nkpg = vm_page_alloc(NULL, nkpt,
1940 VM_ALLOC_NOOBJ | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
1942 panic("pmap_growkernel: no memory to grow kernel");
1946 pmap_zero_page(nkpg);
1947 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1948 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1949 vm_page_lock_queues();
1950 PD_SET_VA(kernel_pmap, (kernel_vm_end >> PDRSHIFT), newpdir, TRUE);
1951 mtx_lock_spin(&allpmaps_lock);
1952 LIST_FOREACH(pmap, &allpmaps, pm_list)
1953 PD_SET_VA(pmap, (kernel_vm_end >> PDRSHIFT), newpdir, TRUE);
1955 mtx_unlock_spin(&allpmaps_lock);
1956 vm_page_unlock_queues();
1958 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1959 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
1960 kernel_vm_end = kernel_map->max_offset;
1967 /***************************************************
1968 * page management routines.
1969 ***************************************************/
1971 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
1972 CTASSERT(_NPCM == 11);
1974 static __inline struct pv_chunk *
1975 pv_to_chunk(pv_entry_t pv)
1978 return (struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK);
1981 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
1983 #define PC_FREE0_9 0xfffffffful /* Free values for index 0 through 9 */
1984 #define PC_FREE10 0x0000fffful /* Free values for index 10 */
1986 static uint32_t pc_freemask[11] = {
1987 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1988 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1989 PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
1990 PC_FREE0_9, PC_FREE10
1993 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
1994 "Current number of pv entries");
1997 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
1999 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
2000 "Current number of pv entry chunks");
2001 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
2002 "Current number of pv entry chunks allocated");
2003 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
2004 "Current number of pv entry chunks frees");
2005 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
2006 "Number of times tried to get a chunk page but failed.");
2008 static long pv_entry_frees, pv_entry_allocs;
2009 static int pv_entry_spare;
2011 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
2012 "Current number of pv entry frees");
2013 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
2014 "Current number of pv entry allocs");
2015 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
2016 "Current number of spare pv entries");
2018 static int pmap_collect_inactive, pmap_collect_active;
2020 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_inactive, CTLFLAG_RD, &pmap_collect_inactive, 0,
2021 "Current number times pmap_collect called on inactive queue");
2022 SYSCTL_INT(_vm_pmap, OID_AUTO, pmap_collect_active, CTLFLAG_RD, &pmap_collect_active, 0,
2023 "Current number times pmap_collect called on active queue");
2027 * We are in a serious low memory condition. Resort to
2028 * drastic measures to free some pages so we can allocate
2029 * another pv entry chunk. This is normally called to
2030 * unmap inactive pages, and if necessary, active pages.
2033 pmap_collect(pmap_t locked_pmap, struct vpgqueues *vpq)
2036 pt_entry_t *pte, tpte;
2037 pv_entry_t next_pv, pv;
2042 TAILQ_FOREACH(m, &vpq->pl, pageq) {
2043 if (m->hold_count || m->busy)
2045 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_list, next_pv) {
2048 /* Avoid deadlock and lock recursion. */
2049 if (pmap > locked_pmap)
2051 else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap))
2053 pmap->pm_stats.resident_count--;
2054 pte = pmap_pte_quick(pmap, va);
2055 tpte = pte_load_clear(pte);
2056 KASSERT((tpte & PG_W) == 0,
2057 ("pmap_collect: wired pte %#jx", (uintmax_t)tpte));
2059 vm_page_flag_set(m, PG_REFERENCED);
2061 KASSERT((tpte & PG_RW),
2062 ("pmap_collect: modified page not writable: va: %#x, pte: %#jx",
2063 va, (uintmax_t)tpte));
2067 pmap_unuse_pt(pmap, va, &free);
2068 pmap_invalidate_page(pmap, va);
2069 pmap_free_zero_pages(free);
2070 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2071 if (TAILQ_EMPTY(&m->md.pv_list))
2072 vm_page_flag_clear(m, PG_WRITEABLE);
2073 free_pv_entry(pmap, pv);
2074 if (pmap != locked_pmap)
2083 * free the pv_entry back to the free list
2086 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2089 struct pv_chunk *pc;
2090 int idx, field, bit;
2092 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2093 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2094 PV_STAT(pv_entry_frees++);
2095 PV_STAT(pv_entry_spare++);
2097 pc = pv_to_chunk(pv);
2098 idx = pv - &pc->pc_pventry[0];
2101 pc->pc_map[field] |= 1ul << bit;
2102 /* move to head of list */
2103 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2104 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2105 for (idx = 0; idx < _NPCM; idx++)
2106 if (pc->pc_map[idx] != pc_freemask[idx])
2108 PV_STAT(pv_entry_spare -= _NPCPV);
2109 PV_STAT(pc_chunk_count--);
2110 PV_STAT(pc_chunk_frees++);
2111 /* entire chunk is free, return it */
2112 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2113 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2114 pmap_qremove((vm_offset_t)pc, 1);
2115 vm_page_unwire(m, 0);
2117 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
2121 * get a new pv_entry, allocating a block from the system
2125 get_pv_entry(pmap_t pmap, int try)
2127 static const struct timeval printinterval = { 60, 0 };
2128 static struct timeval lastprint;
2129 static vm_pindex_t colour;
2130 struct vpgqueues *pq;
2133 struct pv_chunk *pc;
2136 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2137 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2138 PV_STAT(pv_entry_allocs++);
2140 if (pv_entry_count > pv_entry_high_water)
2141 if (ratecheck(&lastprint, &printinterval))
2142 printf("Approaching the limit on PV entries, consider "
2143 "increasing either the vm.pmap.shpgperproc or the "
2144 "vm.pmap.pv_entry_max tunable.\n");
2147 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
2149 for (field = 0; field < _NPCM; field++) {
2150 if (pc->pc_map[field]) {
2151 bit = bsfl(pc->pc_map[field]);
2155 if (field < _NPCM) {
2156 pv = &pc->pc_pventry[field * 32 + bit];
2157 pc->pc_map[field] &= ~(1ul << bit);
2158 /* If this was the last item, move it to tail */
2159 for (field = 0; field < _NPCM; field++)
2160 if (pc->pc_map[field] != 0) {
2161 PV_STAT(pv_entry_spare--);
2162 return (pv); /* not full, return */
2164 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2165 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
2166 PV_STAT(pv_entry_spare--);
2171 * Access to the ptelist "pv_vafree" is synchronized by the page
2172 * queues lock. If "pv_vafree" is currently non-empty, it will
2173 * remain non-empty until pmap_ptelist_alloc() completes.
2175 if (pv_vafree == 0 || (m = vm_page_alloc(NULL, colour, (pq ==
2176 &vm_page_queues[PQ_ACTIVE] ? VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL) |
2177 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
2180 PV_STAT(pc_chunk_tryfail++);
2184 * Reclaim pv entries: At first, destroy mappings to
2185 * inactive pages. After that, if a pv chunk entry
2186 * is still needed, destroy mappings to active pages.
2189 PV_STAT(pmap_collect_inactive++);
2190 pq = &vm_page_queues[PQ_INACTIVE];
2191 } else if (pq == &vm_page_queues[PQ_INACTIVE]) {
2192 PV_STAT(pmap_collect_active++);
2193 pq = &vm_page_queues[PQ_ACTIVE];
2195 panic("get_pv_entry: increase vm.pmap.shpgperproc");
2196 pmap_collect(pmap, pq);
2199 PV_STAT(pc_chunk_count++);
2200 PV_STAT(pc_chunk_allocs++);
2202 pc = (struct pv_chunk *)pmap_ptelist_alloc(&pv_vafree);
2203 pmap_qenter((vm_offset_t)pc, &m, 1);
2204 if ((m->flags & PG_ZERO) == 0)
2207 pc->pc_map[0] = pc_freemask[0] & ~1ul; /* preallocated bit 0 */
2208 for (field = 1; field < _NPCM; field++)
2209 pc->pc_map[field] = pc_freemask[field];
2210 pv = &pc->pc_pventry[0];
2211 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
2212 PV_STAT(pv_entry_spare += _NPCPV - 1);
2217 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
2221 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2222 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2223 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2224 if (pmap == PV_PMAP(pv) && va == pv->pv_va)
2227 KASSERT(pv != NULL, ("pmap_remove_entry: pv not found"));
2228 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2229 if (TAILQ_EMPTY(&m->md.pv_list))
2230 vm_page_flag_clear(m, PG_WRITEABLE);
2231 free_pv_entry(pmap, pv);
2235 * Create a pv entry for page at pa for
2239 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2243 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2244 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2245 pv = get_pv_entry(pmap, FALSE);
2247 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2251 * Conditionally create a pv entry.
2254 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
2258 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2259 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2260 if (pv_entry_count < pv_entry_high_water &&
2261 (pv = get_pv_entry(pmap, TRUE)) != NULL) {
2263 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2270 * pmap_remove_pte: do the things to unmap a page in a process
2273 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va, vm_page_t *free)
2278 CTR3(KTR_PMAP, "pmap_remove_pte: pmap=%p *ptq=0x%x va=0x%x",
2279 pmap, (u_long)*ptq, va);
2281 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2282 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2284 PT_SET_VA_MA(ptq, 0, TRUE);
2286 pmap->pm_stats.wired_count -= 1;
2288 * Machines that don't support invlpg, also don't support
2292 pmap_invalidate_page(kernel_pmap, va);
2293 pmap->pm_stats.resident_count -= 1;
2295 * XXX This is not strictly correctly, but somewhere along the line
2296 * we are losing the managed bit on some pages. It is unclear to me
2297 * why, but I think the most likely explanation is that xen's writable
2298 * page table implementation doesn't respect the unused bits.
2300 if ((oldpte & PG_MANAGED) || ((oldpte & PG_V) && (va < VM_MAXUSER_ADDRESS))
2302 m = PHYS_TO_VM_PAGE(xpmap_mtop(oldpte) & PG_FRAME);
2304 if (!(oldpte & PG_MANAGED))
2305 printf("va=0x%x is unmanaged :-( pte=0x%llx\n", va, oldpte);
2307 if (oldpte & PG_M) {
2308 KASSERT((oldpte & PG_RW),
2309 ("pmap_remove_pte: modified page not writable: va: %#x, pte: %#jx",
2310 va, (uintmax_t)oldpte));
2314 vm_page_flag_set(m, PG_REFERENCED);
2315 pmap_remove_entry(pmap, m, va);
2316 } else if ((va < VM_MAXUSER_ADDRESS) && (oldpte & PG_V))
2317 printf("va=0x%x is unmanaged :-( pte=0x%llx\n", va, oldpte);
2319 return (pmap_unuse_pt(pmap, va, free));
2323 * Remove a single page from a process address space
2326 pmap_remove_page(pmap_t pmap, vm_offset_t va, vm_page_t *free)
2330 CTR2(KTR_PMAP, "pmap_remove_page: pmap=%p va=0x%x",
2333 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2334 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
2335 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2336 if ((pte = pmap_pte_quick(pmap, va)) == NULL || (*pte & PG_V) == 0)
2338 pmap_remove_pte(pmap, pte, va, free);
2339 pmap_invalidate_page(pmap, va);
2341 PT_SET_MA(PADDR1, 0);
2346 * Remove the given range of addresses from the specified map.
2348 * It is assumed that the start and end are properly
2349 * rounded to the page size.
2352 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2357 vm_page_t free = NULL;
2360 CTR3(KTR_PMAP, "pmap_remove: pmap=%p sva=0x%x eva=0x%x",
2364 * Perform an unsynchronized read. This is, however, safe.
2366 if (pmap->pm_stats.resident_count == 0)
2371 vm_page_lock_queues();
2376 * special handling of removing one page. a very
2377 * common operation and easy to short circuit some
2380 if ((sva + PAGE_SIZE == eva) &&
2381 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
2382 pmap_remove_page(pmap, sva, &free);
2386 for (; sva < eva; sva = pdnxt) {
2390 * Calculate index for next page table.
2392 pdnxt = (sva + NBPDR) & ~PDRMASK;
2393 if (pmap->pm_stats.resident_count == 0)
2396 pdirindex = sva >> PDRSHIFT;
2397 ptpaddr = pmap->pm_pdir[pdirindex];
2400 * Weed out invalid mappings. Note: we assume that the page
2401 * directory table is always allocated, and in kernel virtual.
2407 * Check for large page.
2409 if ((ptpaddr & PG_PS) != 0) {
2410 PD_CLEAR_VA(pmap, pdirindex, TRUE);
2411 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2417 * Limit our scan to either the end of the va represented
2418 * by the current page table page, or to the end of the
2419 * range being removed.
2424 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2426 if ((*pte & PG_V) == 0)
2430 * The TLB entry for a PG_G mapping is invalidated
2431 * by pmap_remove_pte().
2433 if ((*pte & PG_G) == 0)
2435 if (pmap_remove_pte(pmap, pte, sva, &free))
2441 PT_SET_VA_MA(PMAP1, 0, TRUE);
2444 pmap_invalidate_all(pmap);
2446 vm_page_unlock_queues();
2448 pmap_free_zero_pages(free);
2452 * Routine: pmap_remove_all
2454 * Removes this physical page from
2455 * all physical maps in which it resides.
2456 * Reflects back modify bits to the pager.
2459 * Original versions of this routine were very
2460 * inefficient because they iteratively called
2461 * pmap_remove (slow...)
2465 pmap_remove_all(vm_page_t m)
2469 pt_entry_t *pte, tpte;
2472 #if defined(PMAP_DIAGNOSTIC)
2474 * XXX This makes pmap_remove_all() illegal for non-managed pages!
2476 if (m->flags & PG_FICTITIOUS) {
2477 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%jx",
2478 VM_PAGE_TO_PHYS(m) & 0xffffffff);
2481 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2483 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2486 pmap->pm_stats.resident_count--;
2487 pte = pmap_pte_quick(pmap, pv->pv_va);
2490 PT_SET_VA_MA(pte, 0, TRUE);
2492 pmap->pm_stats.wired_count--;
2494 vm_page_flag_set(m, PG_REFERENCED);
2497 * Update the vm_page_t clean and reference bits.
2500 KASSERT((tpte & PG_RW),
2501 ("pmap_remove_all: modified page not writable: va: %#x, pte: %#jx",
2502 pv->pv_va, (uintmax_t)tpte));
2506 pmap_unuse_pt(pmap, pv->pv_va, &free);
2507 pmap_invalidate_page(pmap, pv->pv_va);
2508 pmap_free_zero_pages(free);
2509 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2510 free_pv_entry(pmap, pv);
2513 vm_page_flag_clear(m, PG_WRITEABLE);
2516 PT_SET_MA(PADDR1, 0);
2521 * Set the physical protection on the
2522 * specified range of this map as requested.
2525 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2532 CTR4(KTR_PMAP, "pmap_protect: pmap=%p sva=0x%x eva=0x%x prot=0x%x",
2533 pmap, sva, eva, prot);
2535 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2536 pmap_remove(pmap, sva, eva);
2541 if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) ==
2542 (VM_PROT_WRITE|VM_PROT_EXECUTE))
2545 if (prot & VM_PROT_WRITE)
2551 vm_page_lock_queues();
2554 for (; sva < eva; sva = pdnxt) {
2555 pt_entry_t obits, pbits;
2558 pdnxt = (sva + NBPDR) & ~PDRMASK;
2560 pdirindex = sva >> PDRSHIFT;
2561 ptpaddr = pmap->pm_pdir[pdirindex];
2564 * Weed out invalid mappings. Note: we assume that the page
2565 * directory table is always allocated, and in kernel virtual.
2571 * Check for large page.
2573 if ((ptpaddr & PG_PS) != 0) {
2574 if ((prot & VM_PROT_WRITE) == 0)
2575 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
2577 if ((prot & VM_PROT_EXECUTE) == 0)
2578 pmap->pm_pdir[pdirindex] |= pg_nx;
2587 for (pte = pmap_pte_quick(pmap, sva); sva != pdnxt; pte++,
2593 * Regardless of whether a pte is 32 or 64 bits in
2594 * size, PG_RW, PG_A, and PG_M are among the least
2595 * significant 32 bits.
2597 obits = pbits = *pte;
2598 if ((pbits & PG_V) == 0)
2600 if (pbits & PG_MANAGED) {
2603 m = PHYS_TO_VM_PAGE(xpmap_mtop(pbits) & PG_FRAME);
2604 vm_page_flag_set(m, PG_REFERENCED);
2607 if ((pbits & PG_M) != 0) {
2609 m = PHYS_TO_VM_PAGE(xpmap_mtop(pbits) & PG_FRAME);
2614 if ((prot & VM_PROT_WRITE) == 0)
2615 pbits &= ~(PG_RW | PG_M);
2617 if ((prot & VM_PROT_EXECUTE) == 0)
2621 if (pbits != obits) {
2624 PT_SET_VA_MA(pte, pbits, TRUE);
2629 if (!atomic_cmpset_64(pte, obits, pbits))
2632 if (!atomic_cmpset_int((u_int *)pte, obits,
2638 pmap_invalidate_page(pmap, sva);
2646 PT_SET_VA_MA(PMAP1, 0, TRUE);
2648 pmap_invalidate_all(pmap);
2650 vm_page_unlock_queues();
2655 * Insert the given physical page (p) at
2656 * the specified virtual address (v) in the
2657 * target physical map with the protection requested.
2659 * If specified, the page will be wired down, meaning
2660 * that the related pte can not be reclaimed.
2662 * NB: This is the only routine which MAY NOT lazy-evaluate
2663 * or lose information. That is, this routine must actually
2664 * insert this page into the given map NOW.
2667 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
2668 vm_prot_t prot, boolean_t wired)
2674 pt_entry_t origpte, newpte;
2678 CTR6(KTR_PMAP, "pmap_enter: pmap=%08p va=0x%08x access=0x%x ma=0x%08x prot=0x%x wired=%d",
2679 pmap, va, access, xpmap_ptom(VM_PAGE_TO_PHYS(m)), prot, wired);
2680 va = trunc_page(va);
2681 #ifdef PMAP_DIAGNOSTIC
2682 if (va > VM_MAX_KERNEL_ADDRESS)
2683 panic("pmap_enter: toobig");
2684 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2685 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
2690 vm_page_lock_queues();
2695 * In the case that a page table page is not
2696 * resident, we are creating it here.
2698 if (va < VM_MAXUSER_ADDRESS) {
2699 mpte = pmap_allocpte(pmap, va, M_WAITOK);
2701 #if 0 && defined(PMAP_DIAGNOSTIC)
2703 pd_entry_t *pdeaddr = pmap_pde(pmap, va);
2705 if ((origpte & PG_V) == 0) {
2706 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n",
2707 pmap->pm_pdir[PTDPTDI], origpte, va);
2712 pde = pmap_pde(pmap, va);
2713 if ((*pde & PG_PS) != 0)
2714 panic("pmap_enter: attempted pmap_enter on 4MB page");
2715 pte = pmap_pte_quick(pmap, va);
2718 * Page Directory table entry not valid, we need a new PT page
2721 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x\n",
2722 (uintmax_t)pmap->pm_pdir[va >> PDRSHIFT], va);
2725 pa = VM_PAGE_TO_PHYS(m);
2730 KASSERT((*pte & PG_V) || (*pte == 0), ("address set but not valid pte=%p *pte=0x%016jx",
2735 origpte = xpmap_mtop(origpte);
2736 opa = origpte & PG_FRAME;
2739 * Mapping has not changed, must be protection or wiring change.
2741 if (origpte && (opa == pa)) {
2743 * Wiring change, just update stats. We don't worry about
2744 * wiring PT pages as they remain resident as long as there
2745 * are valid mappings in them. Hence, if a user page is wired,
2746 * the PT page will be also.
2748 if (wired && ((origpte & PG_W) == 0))
2749 pmap->pm_stats.wired_count++;
2750 else if (!wired && (origpte & PG_W))
2751 pmap->pm_stats.wired_count--;
2754 * Remove extra pte reference
2760 * We might be turning off write access to the page,
2761 * so we go ahead and sense modify status.
2763 if (origpte & PG_MANAGED) {
2770 * Mapping has changed, invalidate old range and fall through to
2771 * handle validating new mapping.
2775 pmap->pm_stats.wired_count--;
2776 if (origpte & PG_MANAGED) {
2777 om = PHYS_TO_VM_PAGE(opa);
2778 pmap_remove_entry(pmap, om, va);
2779 } else if (va < VM_MAXUSER_ADDRESS)
2780 printf("va=0x%x is unmanaged :-( \n", va);
2784 KASSERT(mpte->wire_count > 0,
2785 ("pmap_enter: missing reference to page table page,"
2789 pmap->pm_stats.resident_count++;
2792 * Enter on the PV list if part of our managed memory.
2794 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0) {
2795 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva,
2796 ("pmap_enter: managed mapping within the clean submap"));
2797 pmap_insert_entry(pmap, va, m);
2802 * Increment counters
2805 pmap->pm_stats.wired_count++;
2809 * Now validate mapping with desired protection/wiring.
2811 newpte = (pt_entry_t)(pa | PG_V);
2812 if ((prot & VM_PROT_WRITE) != 0) {
2814 vm_page_flag_set(m, PG_WRITEABLE);
2817 if ((prot & VM_PROT_EXECUTE) == 0)
2822 if (va < VM_MAXUSER_ADDRESS)
2824 if (pmap == kernel_pmap)
2829 * if the mapping or permission bits are different, we need
2830 * to update the pte.
2832 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2836 PT_SET_VA(pte, newpte | PG_A, FALSE);
2837 if (origpte & PG_A) {
2838 if (origpte & PG_MANAGED)
2839 vm_page_flag_set(om, PG_REFERENCED);
2840 if (opa != VM_PAGE_TO_PHYS(m))
2843 if ((origpte & PG_NX) == 0 &&
2844 (newpte & PG_NX) != 0)
2848 if (origpte & PG_M) {
2849 KASSERT((origpte & PG_RW),
2850 ("pmap_enter: modified page not writable: va: %#x, pte: %#jx",
2851 va, (uintmax_t)origpte));
2852 if ((origpte & PG_MANAGED) != 0)
2854 if ((prot & VM_PROT_WRITE) == 0)
2858 pmap_invalidate_page(pmap, va);
2860 PT_SET_VA(pte, newpte | PG_A, FALSE);
2867 PT_SET_VA_MA(PMAP1, 0, TRUE);
2869 vm_page_unlock_queues();
2874 * Maps a sequence of resident pages belonging to the same object.
2875 * The sequence begins with the given page m_start. This page is
2876 * mapped at the given virtual address start. Each subsequent page is
2877 * mapped at a virtual address that is offset from start by the same
2878 * amount as the page is offset from m_start within the object. The
2879 * last page in the sequence is the page with the largest offset from
2880 * m_start that can be mapped at a virtual address less than the given
2881 * virtual address end. Not every virtual page between start and end
2882 * is mapped; only those for which a resident page exists with the
2883 * corresponding offset from m_start are mapped.
2886 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
2887 vm_page_t m_start, vm_prot_t prot)
2890 vm_pindex_t diff, psize;
2891 multicall_entry_t mcl[16];
2892 multicall_entry_t *mclp = mcl;
2893 int error, count = 0;
2895 VM_OBJECT_LOCK_ASSERT(m_start->object, MA_OWNED);
2896 psize = atop(end - start);
2901 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
2902 mpte = pmap_enter_quick_locked(&mclp, &count, pmap, start + ptoa(diff), m,
2904 m = TAILQ_NEXT(m, listq);
2906 error = HYPERVISOR_multicall(mcl, count);
2907 KASSERT(error == 0, ("bad multicall %d", error));
2913 error = HYPERVISOR_multicall(mcl, count);
2914 KASSERT(error == 0, ("bad multicall %d", error));
2921 * this code makes some *MAJOR* assumptions:
2922 * 1. Current pmap & pmap exists.
2925 * 4. No page table pages.
2926 * but is *MUCH* faster than pmap_enter...
2930 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
2932 multicall_entry_t mcl, *mclp;
2936 CTR4(KTR_PMAP, "pmap_enter_quick: pmap=%p va=0x%x m=%p prot=0x%x",
2940 (void) pmap_enter_quick_locked(&mclp, &count, pmap, va, m, prot, NULL);
2942 HYPERVISOR_multicall(&mcl, count);
2948 pmap_enter_quick_range(pmap_t pmap, vm_offset_t *addrs, vm_page_t *pages, vm_prot_t *prots, int count)
2950 int i, error, index = 0;
2951 multicall_entry_t mcl[16];
2952 multicall_entry_t *mclp = mcl;
2955 for (i = 0; i < count; i++, addrs++, pages++, prots++) {
2956 if (!pmap_is_prefaultable_locked(pmap, *addrs))
2959 (void) pmap_enter_quick_locked(&mclp, &index, pmap, *addrs, *pages, *prots, NULL);
2961 error = HYPERVISOR_multicall(mcl, index);
2964 KASSERT(error == 0, ("bad multicall %d", error));
2968 error = HYPERVISOR_multicall(mcl, index);
2969 KASSERT(error == 0, ("bad multicall %d", error));
2977 pmap_enter_quick_locked(multicall_entry_t **mclpp, int *count, pmap_t pmap, vm_offset_t va, vm_page_t m,
2978 vm_prot_t prot, vm_page_t mpte)
2983 multicall_entry_t *mcl = *mclpp;
2985 KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
2986 (m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0,
2987 ("pmap_enter_quick_locked: managed mapping within the clean submap"));
2988 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2989 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2992 * In the case that a page table page is not
2993 * resident, we are creating it here.
2995 if (va < VM_MAXUSER_ADDRESS) {
3000 * Calculate pagetable page index
3002 ptepindex = va >> PDRSHIFT;
3003 if (mpte && (mpte->pindex == ptepindex)) {
3007 * Get the page directory entry
3009 ptema = pmap->pm_pdir[ptepindex];
3012 * If the page table page is mapped, we just increment
3013 * the hold count, and activate it.
3017 panic("pmap_enter_quick: unexpected mapping into 4MB page");
3018 mpte = PHYS_TO_VM_PAGE(xpmap_mtop(ptema) & PG_FRAME);
3021 mpte = _pmap_allocpte(pmap, ptepindex,
3032 * This call to vtopte makes the assumption that we are
3033 * entering the page into the current pmap. In order to support
3034 * quick entry into any pmap, one would likely use pmap_pte_quick.
3035 * But that isn't as quick as vtopte.
3037 KASSERT(pmap_is_current(pmap), ("entering pages in non-current pmap"));
3048 * Enter on the PV list if part of our managed memory.
3050 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) == 0 &&
3051 !pmap_try_insert_pv_entry(pmap, va, m)) {
3054 if (pmap_unwire_pte_hold(pmap, mpte, &free)) {
3055 pmap_invalidate_page(pmap, va);
3056 pmap_free_zero_pages(free);
3065 * Increment counters
3067 pmap->pm_stats.resident_count++;
3069 pa = VM_PAGE_TO_PHYS(m);
3071 if ((prot & VM_PROT_EXECUTE) == 0)
3077 * Now validate mapping with RO protection
3079 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
3080 pte_store(pte, pa | PG_V | PG_U);
3082 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
3085 * Now validate mapping with RO protection
3087 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
3088 pa = xpmap_ptom(pa | PG_V | PG_U);
3090 pa = xpmap_ptom(pa | PG_V | PG_U | PG_MANAGED);
3092 mcl->op = __HYPERVISOR_update_va_mapping;
3094 mcl->args[1] = (uint32_t)(pa & 0xffffffff);
3095 mcl->args[2] = (uint32_t)(pa >> 32);
3098 *count = *count + 1;
3104 * Make a temporary mapping for a physical address. This is only intended
3105 * to be used for panic dumps.
3108 pmap_kenter_temporary(vm_paddr_t pa, int i)
3112 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
3113 pmap_kenter(va, pa);
3115 return ((void *)crashdumpmap);
3119 * This code maps large physical mmap regions into the
3120 * processor address space. Note that some shortcuts
3121 * are taken, but the code works.
3124 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
3125 vm_object_t object, vm_pindex_t pindex,
3130 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3131 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3132 ("pmap_object_init_pt: non-device object"));
3134 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) {
3137 unsigned int ptepindex;
3142 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
3146 p = vm_page_lookup(object, pindex);
3148 if (vm_page_sleep_if_busy(p, FALSE, "init4p"))
3151 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
3156 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
3157 vm_page_lock_queues();
3159 vm_page_unlock_queues();
3163 p = vm_page_lookup(object, pindex);
3167 ptepa = VM_PAGE_TO_PHYS(p);
3168 if (ptepa & (NBPDR - 1))
3171 p->valid = VM_PAGE_BITS_ALL;
3174 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
3175 npdes = size >> PDRSHIFT;
3177 for(i = 0; i < npdes; i++) {
3178 PD_SET_VA(pmap, ptepindex,
3179 ptepa | PG_U | PG_M | PG_RW | PG_V | PG_PS, FALSE);
3183 pmap_invalidate_all(pmap);
3191 * Routine: pmap_change_wiring
3192 * Function: Change the wiring attribute for a map/virtual-address
3194 * In/out conditions:
3195 * The mapping must already exist in the pmap.
3198 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3202 vm_page_lock_queues();
3204 pte = pmap_pte(pmap, va);
3206 if (wired && !pmap_pte_w(pte)) {
3207 PT_SET_VA_MA((pte), *(pte) | PG_W, TRUE);
3208 pmap->pm_stats.wired_count++;
3209 } else if (!wired && pmap_pte_w(pte)) {
3210 PT_SET_VA_MA((pte), *(pte) & ~PG_W, TRUE);
3211 pmap->pm_stats.wired_count--;
3215 * Wiring is not a hardware characteristic so there is no need to
3218 pmap_pte_release(pte);
3220 vm_page_unlock_queues();
3226 * Copy the range specified by src_addr/len
3227 * from the source map to the range dst_addr/len
3228 * in the destination map.
3230 * This routine is only advisory and need not do anything.
3234 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
3235 vm_offset_t src_addr)
3239 vm_offset_t end_addr = src_addr + len;
3242 if (dst_addr != src_addr)
3245 if (!pmap_is_current(src_pmap)) {
3247 "pmap_copy, skipping: pdir[PTDPTDI]=0x%jx PTDpde[0]=0x%jx",
3248 (src_pmap->pm_pdir[PTDPTDI] & PG_FRAME), (PTDpde[0] & PG_FRAME));
3252 CTR5(KTR_PMAP, "pmap_copy: dst_pmap=%p src_pmap=%p dst_addr=0x%x len=%d src_addr=0x%x",
3253 dst_pmap, src_pmap, dst_addr, len, src_addr);
3255 vm_page_lock_queues();
3256 if (dst_pmap < src_pmap) {
3257 PMAP_LOCK(dst_pmap);
3258 PMAP_LOCK(src_pmap);
3260 PMAP_LOCK(src_pmap);
3261 PMAP_LOCK(dst_pmap);
3264 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3265 pt_entry_t *src_pte, *dst_pte;
3266 vm_page_t dstmpte, srcmpte;
3267 pd_entry_t srcptepaddr;
3270 if (addr >= UPT_MIN_ADDRESS)
3271 panic("pmap_copy: invalid to pmap_copy page tables");
3273 pdnxt = (addr + NBPDR) & ~PDRMASK;
3274 ptepindex = addr >> PDRSHIFT;
3276 srcptepaddr = PT_GET(&src_pmap->pm_pdir[ptepindex]);
3277 if (srcptepaddr == 0)
3280 if (srcptepaddr & PG_PS) {
3281 if (dst_pmap->pm_pdir[ptepindex] == 0) {
3282 PD_SET_VA(dst_pmap, ptepindex, srcptepaddr & ~PG_W, TRUE);
3283 dst_pmap->pm_stats.resident_count +=
3289 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr & PG_FRAME);
3290 if (srcmpte->wire_count == 0)
3291 panic("pmap_copy: source page table page is unused");
3293 if (pdnxt > end_addr)
3296 src_pte = vtopte(addr);
3297 while (addr < pdnxt) {
3301 * we only virtual copy managed pages
3303 if ((ptetemp & PG_MANAGED) != 0) {
3304 dstmpte = pmap_allocpte(dst_pmap, addr,
3306 if (dstmpte == NULL)
3308 dst_pte = pmap_pte_quick(dst_pmap, addr);
3309 if (*dst_pte == 0 &&
3310 pmap_try_insert_pv_entry(dst_pmap, addr,
3311 PHYS_TO_VM_PAGE(xpmap_mtop(ptetemp) & PG_FRAME))) {
3313 * Clear the wired, modified, and
3314 * accessed (referenced) bits
3317 KASSERT(ptetemp != 0, ("src_pte not set"));
3318 PT_SET_VA_MA(dst_pte, ptetemp & ~(PG_W | PG_M | PG_A), TRUE /* XXX debug */);
3319 KASSERT(*dst_pte == (ptetemp & ~(PG_W | PG_M | PG_A)),
3320 ("no pmap copy expected: 0x%jx saw: 0x%jx",
3321 ptetemp & ~(PG_W | PG_M | PG_A), *dst_pte));
3322 dst_pmap->pm_stats.resident_count++;
3325 if (pmap_unwire_pte_hold(dst_pmap,
3327 pmap_invalidate_page(dst_pmap,
3329 pmap_free_zero_pages(free);
3332 if (dstmpte->wire_count >= srcmpte->wire_count)
3341 vm_page_unlock_queues();
3342 PMAP_UNLOCK(src_pmap);
3343 PMAP_UNLOCK(dst_pmap);
3347 * pmap_zero_page zeros the specified hardware page by mapping
3348 * the page into KVM and using bzero to clear its contents.
3351 pmap_zero_page(vm_page_t m)
3353 struct sysmaps *sysmaps;
3355 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3356 mtx_lock(&sysmaps->lock);
3357 if (*sysmaps->CMAP2)
3358 panic("pmap_zero_page: CMAP2 busy");
3360 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | xpmap_ptom(VM_PAGE_TO_PHYS(m)) | PG_A | PG_M);
3361 pagezero(sysmaps->CADDR2);
3362 PT_SET_MA(sysmaps->CADDR2, 0);
3364 mtx_unlock(&sysmaps->lock);
3368 * pmap_zero_page_area zeros the specified hardware page by mapping
3369 * the page into KVM and using bzero to clear its contents.
3371 * off and size may not cover an area beyond a single hardware page.
3374 pmap_zero_page_area(vm_page_t m, int off, int size)
3376 struct sysmaps *sysmaps;
3378 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3379 mtx_lock(&sysmaps->lock);
3380 if (*sysmaps->CMAP2)
3381 panic("pmap_zero_page: CMAP2 busy");
3383 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | xpmap_ptom(VM_PAGE_TO_PHYS(m)) | PG_A | PG_M);
3385 if (off == 0 && size == PAGE_SIZE)
3386 pagezero(sysmaps->CADDR2);
3388 bzero((char *)sysmaps->CADDR2 + off, size);
3389 PT_SET_MA(sysmaps->CADDR2, 0);
3391 mtx_unlock(&sysmaps->lock);
3395 * pmap_zero_page_idle zeros the specified hardware page by mapping
3396 * the page into KVM and using bzero to clear its contents. This
3397 * is intended to be called from the vm_pagezero process only and
3401 pmap_zero_page_idle(vm_page_t m)
3405 panic("pmap_zero_page: CMAP3 busy");
3407 PT_SET_MA(CADDR3, PG_V | PG_RW | xpmap_ptom(VM_PAGE_TO_PHYS(m)) | PG_A | PG_M);
3409 PT_SET_MA(CADDR3, 0);
3414 * pmap_copy_page copies the specified (machine independent)
3415 * page by mapping the page into virtual memory and using
3416 * bcopy to copy the page, one machine dependent page at a
3420 pmap_copy_page(vm_page_t src, vm_page_t dst)
3422 struct sysmaps *sysmaps;
3424 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3425 mtx_lock(&sysmaps->lock);
3426 if (*sysmaps->CMAP1)
3427 panic("pmap_copy_page: CMAP1 busy");
3428 if (*sysmaps->CMAP2)
3429 panic("pmap_copy_page: CMAP2 busy");
3431 PT_SET_MA(sysmaps->CADDR1, PG_V | xpmap_ptom(VM_PAGE_TO_PHYS(src)) | PG_A);
3432 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | xpmap_ptom(VM_PAGE_TO_PHYS(dst)) | PG_A | PG_M);
3433 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
3434 PT_SET_MA(sysmaps->CADDR1, 0);
3435 PT_SET_MA(sysmaps->CADDR2, 0);
3437 mtx_unlock(&sysmaps->lock);
3441 * Returns true if the pmap's pv is one of the first
3442 * 16 pvs linked to from this page. This count may
3443 * be changed upwards or downwards in the future; it
3444 * is only necessary that true be returned for a small
3445 * subset of pmaps for proper page aging.
3448 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3453 if (m->flags & PG_FICTITIOUS)
3456 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3457 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3458 if (PV_PMAP(pv) == pmap) {
3469 * pmap_page_wired_mappings:
3471 * Return the number of managed mappings to the given physical page
3475 pmap_page_wired_mappings(vm_page_t m)
3483 if ((m->flags & PG_FICTITIOUS) != 0)
3485 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3487 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3490 pte = pmap_pte_quick(pmap, pv->pv_va);
3491 if ((*pte & PG_W) != 0)
3500 * Returns TRUE if the given page is mapped individually or as part of
3501 * a 4mpage. Otherwise, returns FALSE.
3504 pmap_page_is_mapped(vm_page_t m)
3506 struct md_page *pvh;
3508 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
3510 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3511 if (TAILQ_EMPTY(&m->md.pv_list)) {
3512 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3513 return (!TAILQ_EMPTY(&pvh->pv_list));
3519 * Remove all pages from specified address space
3520 * this aids process exit speeds. Also, this code
3521 * is special cased for current process only, but
3522 * can have the more generic (and slightly slower)
3523 * mode enabled. This is much faster than pmap_remove
3524 * in the case of running down an entire address space.
3527 pmap_remove_pages(pmap_t pmap)
3529 pt_entry_t *pte, tpte;
3530 vm_page_t m, free = NULL;
3532 struct pv_chunk *pc, *npc;
3535 uint32_t inuse, bitmask;
3538 CTR1(KTR_PMAP, "pmap_remove_pages: pmap=%p", pmap);
3540 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
3541 printf("warning: pmap_remove_pages called with non-current pmap\n");
3544 vm_page_lock_queues();
3545 KASSERT(pmap_is_current(pmap), ("removing pages from non-current pmap"));
3548 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
3550 for (field = 0; field < _NPCM; field++) {
3551 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
3552 while (inuse != 0) {
3554 bitmask = 1UL << bit;
3555 idx = field * 32 + bit;
3556 pv = &pc->pc_pventry[idx];
3559 pte = vtopte(pv->pv_va);
3560 tpte = *pte ? xpmap_mtop(*pte) : 0;
3564 "TPTE at %p IS ZERO @ VA %08x\n",
3570 * We cannot remove wired pages from a process' mapping at this time
3577 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3578 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3579 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
3580 m, (uintmax_t)m->phys_addr,
3583 KASSERT(m < &vm_page_array[vm_page_array_size],
3584 ("pmap_remove_pages: bad tpte %#jx",
3588 PT_CLEAR_VA(pte, FALSE);
3591 * Update the vm_page_t clean/reference bits.
3596 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3597 if (TAILQ_EMPTY(&m->md.pv_list))
3598 vm_page_flag_clear(m, PG_WRITEABLE);
3600 pmap_unuse_pt(pmap, pv->pv_va, &free);
3603 PV_STAT(pv_entry_frees++);
3604 PV_STAT(pv_entry_spare++);
3606 pc->pc_map[field] |= bitmask;
3607 pmap->pm_stats.resident_count--;
3612 PV_STAT(pv_entry_spare -= _NPCPV);
3613 PV_STAT(pc_chunk_count--);
3614 PV_STAT(pc_chunk_frees++);
3615 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3616 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
3617 pmap_qremove((vm_offset_t)pc, 1);
3618 vm_page_unwire(m, 0);
3620 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
3625 PT_SET_MA(PADDR1, 0);
3628 pmap_invalidate_all(pmap);
3629 vm_page_unlock_queues();
3631 pmap_free_zero_pages(free);
3637 * Return whether or not the specified physical page was modified
3638 * in any physical maps.
3641 pmap_is_modified(vm_page_t m)
3649 if (m->flags & PG_FICTITIOUS)
3653 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3654 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3657 pte = pmap_pte_quick(pmap, pv->pv_va);
3658 rv = (*pte & PG_M) != 0;
3664 PT_SET_MA(PADDR1, 0);
3670 * pmap_is_prefaultable:
3672 * Return whether or not the specified virtual address is elgible
3676 pmap_is_prefaultable_locked(pmap_t pmap, vm_offset_t addr)
3679 boolean_t rv = FALSE;
3683 if (pmap_is_current(pmap) && *pmap_pde(pmap, addr)) {
3691 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3696 rv = pmap_is_prefaultable_locked(pmap, addr);
3702 pmap_map_readonly(pmap_t pmap, vm_offset_t va, int len)
3704 int i, npages = round_page(len) >> PAGE_SHIFT;
3705 for (i = 0; i < npages; i++) {
3707 pte = pmap_pte(pmap, (vm_offset_t)(va + i*PAGE_SIZE));
3708 pte_store(pte, xpmap_mtop(*pte & ~(PG_RW|PG_M)));
3709 PMAP_MARK_PRIV(xpmap_mtop(*pte));
3710 pmap_pte_release(pte);
3715 pmap_map_readwrite(pmap_t pmap, vm_offset_t va, int len)
3717 int i, npages = round_page(len) >> PAGE_SHIFT;
3718 for (i = 0; i < npages; i++) {
3720 pte = pmap_pte(pmap, (vm_offset_t)(va + i*PAGE_SIZE));
3721 PMAP_MARK_UNPRIV(xpmap_mtop(*pte));
3722 pte_store(pte, xpmap_mtop(*pte) | (PG_RW|PG_M));
3723 pmap_pte_release(pte);
3728 * Clear the write and modified bits in each of the given page's mappings.
3731 pmap_remove_write(vm_page_t m)
3735 pt_entry_t oldpte, *pte;
3737 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3738 if ((m->flags & PG_FICTITIOUS) != 0 ||
3739 (m->flags & PG_WRITEABLE) == 0)
3742 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3745 pte = pmap_pte_quick(pmap, pv->pv_va);
3748 if ((oldpte & PG_RW) != 0) {
3749 vm_paddr_t newpte = oldpte & ~(PG_RW | PG_M);
3752 * Regardless of whether a pte is 32 or 64 bits
3753 * in size, PG_RW and PG_M are among the least
3754 * significant 32 bits.
3756 PT_SET_VA_MA(pte, newpte, TRUE);
3760 if ((oldpte & PG_M) != 0)
3762 pmap_invalidate_page(pmap, pv->pv_va);
3766 vm_page_flag_clear(m, PG_WRITEABLE);
3769 PT_SET_MA(PADDR1, 0);
3774 * pmap_ts_referenced:
3776 * Return a count of reference bits for a page, clearing those bits.
3777 * It is not necessary for every reference bit to be cleared, but it
3778 * is necessary that 0 only be returned when there are truly no
3779 * reference bits set.
3781 * XXX: The exact number of bits to check and clear is a matter that
3782 * should be tested and standardized at some point in the future for
3783 * optimal aging of shared pages.
3786 pmap_ts_referenced(vm_page_t m)
3788 pv_entry_t pv, pvf, pvn;
3793 if (m->flags & PG_FICTITIOUS)
3796 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3797 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3800 pvn = TAILQ_NEXT(pv, pv_list);
3801 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3802 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3805 pte = pmap_pte_quick(pmap, pv->pv_va);
3806 if ((*pte & PG_A) != 0) {
3807 PT_SET_VA_MA(pte, *pte & ~PG_A, FALSE);
3808 pmap_invalidate_page(pmap, pv->pv_va);
3814 } while ((pv = pvn) != NULL && pv != pvf);
3818 PT_SET_MA(PADDR1, 0);
3825 * Clear the modify bits on the specified physical page.
3828 pmap_clear_modify(vm_page_t m)
3834 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3835 if ((m->flags & PG_FICTITIOUS) != 0)
3838 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3841 pte = pmap_pte_quick(pmap, pv->pv_va);
3842 if ((*pte & PG_M) != 0) {
3844 * Regardless of whether a pte is 32 or 64 bits
3845 * in size, PG_M is among the least significant
3848 PT_SET_VA_MA(pte, *pte & ~PG_M, FALSE);
3849 pmap_invalidate_page(pmap, pv->pv_va);
3857 * pmap_clear_reference:
3859 * Clear the reference bit on the specified physical page.
3862 pmap_clear_reference(vm_page_t m)
3868 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3869 if ((m->flags & PG_FICTITIOUS) != 0)
3872 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3875 pte = pmap_pte_quick(pmap, pv->pv_va);
3876 if ((*pte & PG_A) != 0) {
3878 * Regardless of whether a pte is 32 or 64 bits
3879 * in size, PG_A is among the least significant
3882 PT_SET_VA_MA(pte, *pte & ~PG_A, FALSE);
3883 pmap_invalidate_page(pmap, pv->pv_va);
3891 * Miscellaneous support routines follow
3895 * Map a set of physical memory pages into the kernel virtual
3896 * address space. Return a pointer to where it is mapped. This
3897 * routine is intended to be used for mapping device memory,
3901 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
3903 vm_offset_t va, offset;
3906 offset = pa & PAGE_MASK;
3907 size = roundup(offset + size, PAGE_SIZE);
3910 if (pa < KERNLOAD && pa + size <= KERNLOAD)
3913 va = kmem_alloc_nofault(kernel_map, size);
3915 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3917 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
3918 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
3919 pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
3920 pmap_invalidate_cache_range(va, va + size);
3921 return ((void *)(va + offset));
3925 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3928 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
3932 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
3935 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
3939 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3941 vm_offset_t base, offset, tmpva;
3943 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
3945 base = trunc_page(va);
3946 offset = va & PAGE_MASK;
3947 size = roundup(offset + size, PAGE_SIZE);
3949 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
3950 pmap_kremove(tmpva);
3951 pmap_invalidate_range(kernel_pmap, va, tmpva);
3953 kmem_free(kernel_map, base, size);
3957 * Sets the memory attribute for the specified page.
3960 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
3962 struct sysmaps *sysmaps;
3963 vm_offset_t sva, eva;
3965 m->md.pat_mode = ma;
3966 if ((m->flags & PG_FICTITIOUS) != 0)
3970 * If "m" is a normal page, flush it from the cache.
3971 * See pmap_invalidate_cache_range().
3973 * First, try to find an existing mapping of the page by sf
3974 * buffer. sf_buf_invalidate_cache() modifies mapping and
3975 * flushes the cache.
3977 if (sf_buf_invalidate_cache(m))
3981 * If page is not mapped by sf buffer, but CPU does not
3982 * support self snoop, map the page transient and do
3983 * invalidation. In the worst case, whole cache is flushed by
3984 * pmap_invalidate_cache_range().
3986 if ((cpu_feature & (CPUID_SS|CPUID_CLFSH)) == CPUID_CLFSH) {
3987 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3988 mtx_lock(&sysmaps->lock);
3989 if (*sysmaps->CMAP2)
3990 panic("pmap_page_set_memattr: CMAP2 busy");
3992 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW |
3993 xpmap_ptom(VM_PAGE_TO_PHYS(m)) | PG_A | PG_M |
3994 pmap_cache_bits(m->md.pat_mode, 0));
3995 invlcaddr(sysmaps->CADDR2);
3996 sva = (vm_offset_t)sysmaps->CADDR2;
3997 eva = sva + PAGE_SIZE;
3999 sva = eva = 0; /* gcc */
4000 pmap_invalidate_cache_range(sva, eva);
4002 PT_SET_MA(sysmaps->CADDR2, 0);
4004 mtx_unlock(&sysmaps->lock);
4009 pmap_change_attr(va, size, mode)
4014 vm_offset_t base, offset, tmpva;
4020 base = trunc_page(va);
4021 offset = va & PAGE_MASK;
4022 size = roundup(offset + size, PAGE_SIZE);
4024 /* Only supported on kernel virtual addresses. */
4025 if (base <= VM_MAXUSER_ADDRESS)
4028 /* 4MB pages and pages that aren't mapped aren't supported. */
4029 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) {
4030 pde = pmap_pde(kernel_pmap, tmpva);
4033 if ((*pde & PG_V) == 0)
4036 if ((*pte & PG_V) == 0)
4043 * Ok, all the pages exist and are 4k, so run through them updating
4046 for (tmpva = base; size > 0; ) {
4047 pte = vtopte(tmpva);
4050 * The cache mode bits are all in the low 32-bits of the
4051 * PTE, so we can just spin on updating the low 32-bits.
4054 opte = *(u_int *)pte;
4055 npte = opte & ~(PG_PTE_PAT | PG_NC_PCD | PG_NC_PWT);
4056 npte |= pmap_cache_bits(mode, 0);
4057 PT_SET_VA_MA(pte, npte, TRUE);
4058 } while (npte != opte && (*pte != npte));
4066 * Flush CPU caches to make sure any data isn't cached that shouldn't
4070 pmap_invalidate_range(kernel_pmap, base, tmpva);
4071 pmap_invalidate_cache_range(base, tmpva);
4077 * perform the pmap work for mincore
4080 pmap_mincore(pmap_t pmap, vm_offset_t addr)
4082 pt_entry_t *ptep, pte;
4087 ptep = pmap_pte(pmap, addr);
4088 pte = (ptep != NULL) ? PT_GET(ptep) : 0;
4089 pmap_pte_release(ptep);
4095 val = MINCORE_INCORE;
4096 if ((pte & PG_MANAGED) == 0)
4099 pa = pte & PG_FRAME;
4101 m = PHYS_TO_VM_PAGE(pa);
4107 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
4110 * Modified by someone else
4112 vm_page_lock_queues();
4113 if (m->dirty || pmap_is_modified(m))
4114 val |= MINCORE_MODIFIED_OTHER;
4115 vm_page_unlock_queues();
4121 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
4124 * Referenced by someone else
4126 vm_page_lock_queues();
4127 if ((m->flags & PG_REFERENCED) ||
4128 pmap_ts_referenced(m)) {
4129 val |= MINCORE_REFERENCED_OTHER;
4130 vm_page_flag_set(m, PG_REFERENCED);
4132 vm_page_unlock_queues();
4139 pmap_activate(struct thread *td)
4141 pmap_t pmap, oldpmap;
4145 pmap = vmspace_pmap(td->td_proc->p_vmspace);
4146 oldpmap = PCPU_GET(curpmap);
4148 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask));
4149 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
4151 oldpmap->pm_active &= ~1;
4152 pmap->pm_active |= 1;
4155 cr3 = vtophys(pmap->pm_pdpt);
4157 cr3 = vtophys(pmap->pm_pdir);
4160 * pmap_activate is for the current thread on the current cpu
4162 td->td_pcb->pcb_cr3 = cr3;
4166 PCPU_SET(curpmap, pmap);
4171 * Increase the starting virtual address of the given mapping if a
4172 * different alignment might result in more superpage mappings.
4175 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4176 vm_offset_t *addr, vm_size_t size)
4178 vm_offset_t superpage_offset;
4182 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
4183 offset += ptoa(object->pg_color);
4184 superpage_offset = offset & PDRMASK;
4185 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
4186 (*addr & PDRMASK) == superpage_offset)
4188 if ((*addr & PDRMASK) < superpage_offset)
4189 *addr = (*addr & ~PDRMASK) + superpage_offset;
4191 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
4200 int i, pdir, offset;
4205 * We need to remove the recursive mapping structure from all
4206 * our pmaps so that Xen doesn't get confused when it restores
4207 * the page tables. The recursive map lives at page directory
4208 * index PTDPTDI. We assume that the suspend code has stopped
4209 * the other vcpus (if any).
4211 LIST_FOREACH(pmap, &allpmaps, pm_list) {
4212 for (i = 0; i < 4; i++) {
4214 * Figure out which page directory (L2) page
4215 * contains this bit of the recursive map and
4216 * the offset within that page of the map
4219 pdir = (PTDPTDI + i) / NPDEPG;
4220 offset = (PTDPTDI + i) % NPDEPG;
4221 pdirma = pmap->pm_pdpt[pdir] & PG_FRAME;
4222 mu[i].ptr = pdirma + offset * sizeof(pd_entry_t);
4225 HYPERVISOR_mmu_update(mu, 4, NULL, DOMID_SELF);
4233 int i, pdir, offset;
4238 * Restore the recursive map that we removed on suspend.
4240 LIST_FOREACH(pmap, &allpmaps, pm_list) {
4241 for (i = 0; i < 4; i++) {
4243 * Figure out which page directory (L2) page
4244 * contains this bit of the recursive map and
4245 * the offset within that page of the map
4248 pdir = (PTDPTDI + i) / NPDEPG;
4249 offset = (PTDPTDI + i) % NPDEPG;
4250 pdirma = pmap->pm_pdpt[pdir] & PG_FRAME;
4251 mu[i].ptr = pdirma + offset * sizeof(pd_entry_t);
4252 mu[i].val = (pmap->pm_pdpt[i] & PG_FRAME) | PG_V;
4254 HYPERVISOR_mmu_update(mu, 4, NULL, DOMID_SELF);
4260 #if defined(PMAP_DEBUG)
4261 pmap_pid_dump(int pid)
4268 sx_slock(&allproc_lock);
4269 FOREACH_PROC_IN_SYSTEM(p) {
4270 if (p->p_pid != pid)
4276 pmap = vmspace_pmap(p->p_vmspace);
4277 for (i = 0; i < NPDEPTD; i++) {
4280 vm_offset_t base = i << PDRSHIFT;
4282 pde = &pmap->pm_pdir[i];
4283 if (pde && pmap_pde_v(pde)) {
4284 for (j = 0; j < NPTEPG; j++) {
4285 vm_offset_t va = base + (j << PAGE_SHIFT);
4286 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
4291 sx_sunlock(&allproc_lock);
4294 pte = pmap_pte(pmap, va);
4295 if (pte && pmap_pte_v(pte)) {
4299 m = PHYS_TO_VM_PAGE(pa & PG_FRAME);
4300 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
4301 va, pa, m->hold_count, m->wire_count, m->flags);
4316 sx_sunlock(&allproc_lock);
4323 static void pads(pmap_t pm);
4324 void pmap_pvdump(vm_paddr_t pa);
4326 /* print address space of pmap*/
4334 if (pm == kernel_pmap)
4336 for (i = 0; i < NPDEPTD; i++)
4338 for (j = 0; j < NPTEPG; j++) {
4339 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
4340 if (pm == kernel_pmap && va < KERNBASE)
4342 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
4344 ptep = pmap_pte(pm, va);
4345 if (pmap_pte_v(ptep))
4346 printf("%x:%x ", va, *ptep);
4352 pmap_pvdump(vm_paddr_t pa)
4358 printf("pa %x", pa);
4359 m = PHYS_TO_VM_PAGE(pa);
4360 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4362 printf(" -> pmap %p, va %x", (void *)pmap, pv->pv_va);