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;
226 static int pat_works; /* Is page attribute table sane? */
229 * Data for the pv entry allocation mechanism
231 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
232 static struct md_page *pv_table;
233 static int shpgperproc = PMAP_SHPGPERPROC;
235 struct pv_chunk *pv_chunkbase; /* KVA block for pv_chunks */
236 int pv_maxchunks; /* How many chunks we have KVA for */
237 vm_offset_t pv_vafree; /* freelist stored in the PTE */
240 * All those kernel PT submaps that BSD is so fond of
249 static struct sysmaps sysmaps_pcpu[MAXCPU];
250 pt_entry_t *CMAP1 = 0;
251 static pt_entry_t *CMAP3;
252 caddr_t CADDR1 = 0, ptvmmap = 0;
253 static caddr_t CADDR3;
254 struct msgbuf *msgbufp = 0;
259 static caddr_t crashdumpmap;
261 static pt_entry_t *PMAP1 = 0, *PMAP2;
262 static pt_entry_t *PADDR1 = 0, *PADDR2;
265 static int PMAP1changedcpu;
266 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
268 "Number of times pmap_pte_quick changed CPU with same PMAP1");
270 static int PMAP1changed;
271 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
273 "Number of times pmap_pte_quick changed PMAP1");
274 static int PMAP1unchanged;
275 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
277 "Number of times pmap_pte_quick didn't change PMAP1");
278 static struct mtx PMAP2mutex;
280 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
281 static int pg_ps_enabled;
282 SYSCTL_INT(_vm_pmap, OID_AUTO, pg_ps_enabled, CTLFLAG_RDTUN, &pg_ps_enabled, 0,
283 "Are large page mappings enabled?");
285 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
286 "Max number of PV entries");
287 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
288 "Page share factor per proc");
290 static void free_pv_entry(pmap_t pmap, pv_entry_t pv);
291 static pv_entry_t get_pv_entry(pmap_t locked_pmap, int try);
293 static vm_page_t pmap_enter_quick_locked(multicall_entry_t **mcl, int *count, pmap_t pmap, vm_offset_t va,
294 vm_page_t m, vm_prot_t prot, vm_page_t mpte);
295 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva,
297 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va,
299 static void pmap_remove_entry(struct pmap *pmap, vm_page_t m,
301 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m);
302 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
305 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags);
307 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex, int flags);
308 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free);
309 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
310 static void pmap_pte_release(pt_entry_t *pte);
311 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t *);
312 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
313 static boolean_t pmap_is_prefaultable_locked(pmap_t pmap, vm_offset_t addr);
314 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
316 static __inline void pagezero(void *page);
318 #if defined(PAE) && !defined(XEN)
319 static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
322 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
323 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
326 * If you get an error here, then you set KVA_PAGES wrong! See the
327 * description of KVA_PAGES in sys/i386/include/pmap.h. It must be
328 * multiple of 4 for a normal kernel, or a multiple of 8 for a PAE.
330 CTASSERT(KERNBASE % (1 << 24) == 0);
335 pd_set(struct pmap *pmap, int ptepindex, vm_paddr_t val, int type)
337 vm_paddr_t pdir_ma = vtomach(&pmap->pm_pdir[ptepindex]);
342 xen_queue_pt_update(shadow_pdir_ma,
343 xpmap_ptom(val & ~(PG_RW)));
345 xen_queue_pt_update(pdir_ma,
348 case SH_PD_SET_VA_MA:
350 xen_queue_pt_update(shadow_pdir_ma,
353 xen_queue_pt_update(pdir_ma, val);
355 case SH_PD_SET_VA_CLEAR:
357 xen_queue_pt_update(shadow_pdir_ma, 0);
359 xen_queue_pt_update(pdir_ma, 0);
365 * Move the kernel virtual free pointer to the next
366 * 4MB. This is used to help improve performance
367 * by using a large (4MB) page for much of the kernel
368 * (.text, .data, .bss)
371 pmap_kmem_choose(vm_offset_t addr)
373 vm_offset_t newaddr = addr;
376 if (cpu_feature & CPUID_PSE)
377 newaddr = (addr + PDRMASK) & ~PDRMASK;
383 * Bootstrap the system enough to run with virtual memory.
385 * On the i386 this is called after mapping has already been enabled
386 * and just syncs the pmap module with what has already been done.
387 * [We can't call it easily with mapping off since the kernel is not
388 * mapped with PA == VA, hence we would have to relocate every address
389 * from the linked base (virtual) address "KERNBASE" to the actual
390 * (physical) address starting relative to 0]
393 pmap_bootstrap(vm_paddr_t firstaddr)
396 pt_entry_t *pte, *unused;
397 struct sysmaps *sysmaps;
401 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
402 * large. It should instead be correctly calculated in locore.s and
403 * not based on 'first' (which is a physical address, not a virtual
404 * address, for the start of unused physical memory). The kernel
405 * page tables are NOT double mapped and thus should not be included
406 * in this calculation.
408 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
409 virtual_avail = pmap_kmem_choose(virtual_avail);
411 virtual_end = VM_MAX_KERNEL_ADDRESS;
414 * Initialize the kernel pmap (which is statically allocated).
416 PMAP_LOCK_INIT(kernel_pmap);
417 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
419 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
421 kernel_pmap->pm_active = -1; /* don't allow deactivation */
422 TAILQ_INIT(&kernel_pmap->pm_pvchunk);
423 LIST_INIT(&allpmaps);
424 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
425 mtx_lock_spin(&allpmaps_lock);
426 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
427 mtx_unlock_spin(&allpmaps_lock);
432 * Reserve some special page table entries/VA space for temporary
435 #define SYSMAP(c, p, v, n) \
436 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
442 * CMAP1/CMAP2 are used for zeroing and copying pages.
443 * CMAP3 is used for the idle process page zeroing.
445 for (i = 0; i < MAXCPU; i++) {
446 sysmaps = &sysmaps_pcpu[i];
447 mtx_init(&sysmaps->lock, "SYSMAPS", NULL, MTX_DEF);
448 SYSMAP(caddr_t, sysmaps->CMAP1, sysmaps->CADDR1, 1)
449 SYSMAP(caddr_t, sysmaps->CMAP2, sysmaps->CADDR2, 1)
451 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
452 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
453 PT_SET_MA(CADDR3, 0);
458 SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS)
461 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
463 SYSMAP(caddr_t, unused, ptvmmap, 1)
466 * msgbufp is used to map the system message buffer.
468 SYSMAP(struct msgbuf *, unused, msgbufp, atop(round_page(MSGBUF_SIZE)))
471 * ptemap is used for pmap_pte_quick
473 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1);
474 SYSMAP(pt_entry_t *, PMAP2, PADDR2, 1);
476 mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
479 PT_SET_MA(CADDR1, 0);
482 * Leave in place an identity mapping (virt == phys) for the low 1 MB
483 * physical memory region that is used by the ACPI wakeup code. This
484 * mapping must not have PG_G set.
488 * leave here deliberately to show that this is not supported
491 /* FIXME: This is gross, but needed for the XBOX. Since we are in such
492 * an early stadium, we cannot yet neatly map video memory ... :-(
493 * Better fixes are very welcome! */
494 if (!arch_i386_is_xbox)
496 for (i = 1; i < NKPT; i++)
499 /* Initialize the PAT MSR if present. */
502 /* Turn on PG_G on kernel page(s) */
515 /* Bail if this CPU doesn't implement PAT. */
516 if (!(cpu_feature & CPUID_PAT))
519 if (cpu_vendor_id != CPU_VENDOR_INTEL ||
520 (CPUID_TO_FAMILY(cpu_id) == 6 && CPUID_TO_MODEL(cpu_id) >= 0xe)) {
522 * Leave the indices 0-3 at the default of WB, WT, UC, and UC-.
523 * Program 4 and 5 as WP and WC.
524 * Leave 6 and 7 as UC and UC-.
526 pat_msr = rdmsr(MSR_PAT);
527 pat_msr &= ~(PAT_MASK(4) | PAT_MASK(5));
528 pat_msr |= PAT_VALUE(4, PAT_WRITE_PROTECTED) |
529 PAT_VALUE(5, PAT_WRITE_COMBINING);
533 * Due to some Intel errata, we can only safely use the lower 4
534 * PAT entries. Thus, just replace PAT Index 2 with WC instead
537 * Intel Pentium III Processor Specification Update
538 * Errata E.27 (Upper Four PAT Entries Not Usable With Mode B
541 * Intel Pentium IV Processor Specification Update
542 * Errata N46 (PAT Index MSB May Be Calculated Incorrectly)
544 pat_msr = rdmsr(MSR_PAT);
545 pat_msr &= ~PAT_MASK(2);
546 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
549 wrmsr(MSR_PAT, pat_msr);
553 * Set PG_G on kernel pages. Only the BSP calls this when SMP is turned on.
560 vm_offset_t va, endva;
567 endva = KERNBASE + KERNend;
570 va = KERNBASE + KERNLOAD;
572 pdir = kernel_pmap->pm_pdir[KPTDI+i];
574 kernel_pmap->pm_pdir[KPTDI+i] = PTD[KPTDI+i] = pdir;
575 invltlb(); /* Play it safe, invltlb() every time */
580 va = (vm_offset_t)btext;
585 invltlb(); /* Play it safe, invltlb() every time */
592 * Initialize a vm_page's machine-dependent fields.
595 pmap_page_init(vm_page_t m)
598 TAILQ_INIT(&m->md.pv_list);
599 m->md.pat_mode = PAT_WRITE_BACK;
602 #if defined(PAE) && !defined(XEN)
604 pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
607 /* Inform UMA that this allocator uses kernel_map/object. */
608 *flags = UMA_SLAB_KERNEL;
609 return ((void *)kmem_alloc_contig(kernel_map, bytes, wait, 0x0ULL,
610 0xffffffffULL, 1, 0, VM_MEMATTR_DEFAULT));
615 * ABuse the pte nodes for unmapped kva to thread a kva freelist through.
617 * - Must deal with pages in order to ensure that none of the PG_* bits
618 * are ever set, PG_V in particular.
619 * - Assumes we can write to ptes without pte_store() atomic ops, even
620 * on PAE systems. This should be ok.
621 * - Assumes nothing will ever test these addresses for 0 to indicate
622 * no mapping instead of correctly checking PG_V.
623 * - Assumes a vm_offset_t will fit in a pte (true for i386).
624 * Because PG_V is never set, there can be no mappings to invalidate.
626 static int ptelist_count = 0;
628 pmap_ptelist_alloc(vm_offset_t *head)
631 vm_offset_t *phead = (vm_offset_t *)*head;
633 if (ptelist_count == 0) {
634 printf("out of memory!!!!!!\n");
635 return (0); /* Out of memory */
638 va = phead[ptelist_count];
643 pmap_ptelist_free(vm_offset_t *head, vm_offset_t va)
645 vm_offset_t *phead = (vm_offset_t *)*head;
647 phead[ptelist_count++] = va;
651 pmap_ptelist_init(vm_offset_t *head, void *base, int npages)
657 nstackpages = (npages + PAGE_SIZE/sizeof(vm_offset_t) - 1)/ (PAGE_SIZE/sizeof(vm_offset_t));
658 for (i = 0; i < nstackpages; i++) {
659 va = (vm_offset_t)base + i * PAGE_SIZE;
660 m = vm_page_alloc(NULL, i,
661 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
663 pmap_qenter(va, &m, 1);
666 *head = (vm_offset_t)base;
667 for (i = npages - 1; i >= nstackpages; i--) {
668 va = (vm_offset_t)base + i * PAGE_SIZE;
669 pmap_ptelist_free(head, va);
675 * Initialize the pmap module.
676 * Called by vm_init, to initialize any structures that the pmap
677 * system needs to map virtual memory.
687 * Initialize the vm page array entries for the kernel pmap's
690 for (i = 0; i < nkpt; i++) {
691 mpte = PHYS_TO_VM_PAGE(xpmap_mtop(PTD[i + KPTDI] & PG_FRAME));
692 KASSERT(mpte >= vm_page_array &&
693 mpte < &vm_page_array[vm_page_array_size],
694 ("pmap_init: page table page is out of range"));
695 mpte->pindex = i + KPTDI;
696 mpte->phys_addr = xpmap_mtop(PTD[i + KPTDI] & PG_FRAME);
700 * Initialize the address space (zone) for the pv entries. Set a
701 * high water mark so that the system can recover from excessive
702 * numbers of pv entries.
704 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
705 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
706 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
707 pv_entry_max = roundup(pv_entry_max, _NPCPV);
708 pv_entry_high_water = 9 * (pv_entry_max / 10);
711 * Are large page mappings enabled?
713 TUNABLE_INT_FETCH("vm.pmap.pg_ps_enabled", &pg_ps_enabled);
716 * Calculate the size of the pv head table for superpages.
718 for (i = 0; phys_avail[i + 1]; i += 2);
719 pv_npg = round_4mpage(phys_avail[(i - 2) + 1]) / NBPDR;
722 * Allocate memory for the pv head table for superpages.
724 s = (vm_size_t)(pv_npg * sizeof(struct md_page));
726 pv_table = (struct md_page *)kmem_alloc(kernel_map, s);
727 for (i = 0; i < pv_npg; i++)
728 TAILQ_INIT(&pv_table[i].pv_list);
730 pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
731 pv_chunkbase = (struct pv_chunk *)kmem_alloc_nofault(kernel_map,
732 PAGE_SIZE * pv_maxchunks);
733 if (pv_chunkbase == NULL)
734 panic("pmap_init: not enough kvm for pv chunks");
735 pmap_ptelist_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
736 #if defined(PAE) && !defined(XEN)
737 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
738 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1,
739 UMA_ZONE_VM | UMA_ZONE_NOFREE);
740 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
745 /***************************************************
746 * Low level helper routines.....
747 ***************************************************/
750 * Determine the appropriate bits to set in a PTE or PDE for a specified
754 pmap_cache_bits(int mode, boolean_t is_pde)
756 int pat_flag, pat_index, cache_bits;
758 /* The PAT bit is different for PTE's and PDE's. */
759 pat_flag = is_pde ? PG_PDE_PAT : PG_PTE_PAT;
761 /* If we don't support PAT, map extended modes to older ones. */
762 if (!(cpu_feature & CPUID_PAT)) {
764 case PAT_UNCACHEABLE:
765 case PAT_WRITE_THROUGH:
769 case PAT_WRITE_COMBINING:
770 case PAT_WRITE_PROTECTED:
771 mode = PAT_UNCACHEABLE;
776 /* Map the caching mode to a PAT index. */
779 case PAT_UNCACHEABLE:
782 case PAT_WRITE_THROUGH:
791 case PAT_WRITE_COMBINING:
794 case PAT_WRITE_PROTECTED:
798 panic("Unknown caching mode %d\n", mode);
803 case PAT_UNCACHEABLE:
804 case PAT_WRITE_PROTECTED:
807 case PAT_WRITE_THROUGH:
813 case PAT_WRITE_COMBINING:
817 panic("Unknown caching mode %d\n", mode);
821 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
824 cache_bits |= pat_flag;
826 cache_bits |= PG_NC_PCD;
828 cache_bits |= PG_NC_PWT;
833 * For SMP, these functions have to use the IPI mechanism for coherence.
835 * N.B.: Before calling any of the following TLB invalidation functions,
836 * the calling processor must ensure that all stores updating a non-
837 * kernel page table are globally performed. Otherwise, another
838 * processor could cache an old, pre-update entry without being
839 * invalidated. This can happen one of two ways: (1) The pmap becomes
840 * active on another processor after its pm_active field is checked by
841 * one of the following functions but before a store updating the page
842 * table is globally performed. (2) The pmap becomes active on another
843 * processor before its pm_active field is checked but due to
844 * speculative loads one of the following functions stills reads the
845 * pmap as inactive on the other processor.
847 * The kernel page table is exempt because its pm_active field is
848 * immutable. The kernel page table is always active on every
852 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
854 cpumask_t cpumask, other_cpus;
856 CTR2(KTR_PMAP, "pmap_invalidate_page: pmap=%p va=0x%x",
860 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
864 cpumask = PCPU_GET(cpumask);
865 other_cpus = PCPU_GET(other_cpus);
866 if (pmap->pm_active & cpumask)
868 if (pmap->pm_active & other_cpus)
869 smp_masked_invlpg(pmap->pm_active & other_cpus, va);
876 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
878 cpumask_t cpumask, other_cpus;
881 CTR3(KTR_PMAP, "pmap_invalidate_page: pmap=%p eva=0x%x sva=0x%x",
885 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
886 for (addr = sva; addr < eva; addr += PAGE_SIZE)
888 smp_invlpg_range(sva, eva);
890 cpumask = PCPU_GET(cpumask);
891 other_cpus = PCPU_GET(other_cpus);
892 if (pmap->pm_active & cpumask)
893 for (addr = sva; addr < eva; addr += PAGE_SIZE)
895 if (pmap->pm_active & other_cpus)
896 smp_masked_invlpg_range(pmap->pm_active & other_cpus,
904 pmap_invalidate_all(pmap_t pmap)
906 cpumask_t cpumask, other_cpus;
908 CTR1(KTR_PMAP, "pmap_invalidate_page: pmap=%p", pmap);
911 if (pmap == kernel_pmap || pmap->pm_active == all_cpus) {
915 cpumask = PCPU_GET(cpumask);
916 other_cpus = PCPU_GET(other_cpus);
917 if (pmap->pm_active & cpumask)
919 if (pmap->pm_active & other_cpus)
920 smp_masked_invltlb(pmap->pm_active & other_cpus);
926 pmap_invalidate_cache(void)
936 * Normal, non-SMP, 486+ invalidation functions.
937 * We inline these within pmap.c for speed.
940 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
942 CTR2(KTR_PMAP, "pmap_invalidate_page: pmap=%p va=0x%x",
945 if (pmap == kernel_pmap || pmap->pm_active)
951 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
955 if (eva - sva > PAGE_SIZE)
956 CTR3(KTR_PMAP, "pmap_invalidate_range: pmap=%p sva=0x%x eva=0x%x",
959 if (pmap == kernel_pmap || pmap->pm_active)
960 for (addr = sva; addr < eva; addr += PAGE_SIZE)
966 pmap_invalidate_all(pmap_t pmap)
969 CTR1(KTR_PMAP, "pmap_invalidate_all: pmap=%p", pmap);
971 if (pmap == kernel_pmap || pmap->pm_active)
976 pmap_invalidate_cache(void)
984 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva)
987 KASSERT((sva & PAGE_MASK) == 0,
988 ("pmap_invalidate_cache_range: sva not page-aligned"));
989 KASSERT((eva & PAGE_MASK) == 0,
990 ("pmap_invalidate_cache_range: eva not page-aligned"));
992 if (cpu_feature & CPUID_SS)
993 ; /* If "Self Snoop" is supported, do nothing. */
994 else if (cpu_feature & CPUID_CLFSH) {
997 * Otherwise, do per-cache line flush. Use the mfence
998 * instruction to insure that previous stores are
999 * included in the write-back. The processor
1000 * propagates flush to other processors in the cache
1004 for (; sva < eva; sva += cpu_clflush_line_size)
1010 * No targeted cache flush methods are supported by CPU,
1011 * globally invalidate cache as a last resort.
1013 pmap_invalidate_cache();
1018 * Are we current address space or kernel? N.B. We return FALSE when
1019 * a pmap's page table is in use because a kernel thread is borrowing
1020 * it. The borrowed page table can change spontaneously, making any
1021 * dependence on its continued use subject to a race condition.
1024 pmap_is_current(pmap_t pmap)
1027 return (pmap == kernel_pmap ||
1028 (pmap == vmspace_pmap(curthread->td_proc->p_vmspace) &&
1029 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)));
1033 * If the given pmap is not the current or kernel pmap, the returned pte must
1034 * be released by passing it to pmap_pte_release().
1037 pmap_pte(pmap_t pmap, vm_offset_t va)
1042 pde = pmap_pde(pmap, va);
1046 /* are we current address space or kernel? */
1047 if (pmap_is_current(pmap))
1048 return (vtopte(va));
1049 mtx_lock(&PMAP2mutex);
1050 newpf = *pde & PG_FRAME;
1051 if ((*PMAP2 & PG_FRAME) != newpf) {
1052 PT_SET_MA(PADDR2, newpf | PG_V | PG_A | PG_M);
1053 CTR3(KTR_PMAP, "pmap_pte: pmap=%p va=0x%x newpte=0x%08x",
1054 pmap, va, (*PMAP2 & 0xffffffff));
1057 return (PADDR2 + (i386_btop(va) & (NPTEPG - 1)));
1063 * Releases a pte that was obtained from pmap_pte(). Be prepared for the pte
1066 static __inline void
1067 pmap_pte_release(pt_entry_t *pte)
1070 if ((pt_entry_t *)((vm_offset_t)pte & ~PAGE_MASK) == PADDR2) {
1071 CTR1(KTR_PMAP, "pmap_pte_release: pte=0x%jx",
1073 PT_SET_VA(PMAP2, 0, TRUE);
1074 mtx_unlock(&PMAP2mutex);
1078 static __inline void
1079 invlcaddr(void *caddr)
1082 invlpg((u_int)caddr);
1087 * Super fast pmap_pte routine best used when scanning
1088 * the pv lists. This eliminates many coarse-grained
1089 * invltlb calls. Note that many of the pv list
1090 * scans are across different pmaps. It is very wasteful
1091 * to do an entire invltlb for checking a single mapping.
1093 * If the given pmap is not the current pmap, vm_page_queue_mtx
1094 * must be held and curthread pinned to a CPU.
1097 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
1102 pde = pmap_pde(pmap, va);
1106 /* are we current address space or kernel? */
1107 if (pmap_is_current(pmap))
1108 return (vtopte(va));
1109 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1110 KASSERT(curthread->td_pinned > 0, ("curthread not pinned"));
1111 newpf = *pde & PG_FRAME;
1112 if ((*PMAP1 & PG_FRAME) != newpf) {
1113 PT_SET_MA(PADDR1, newpf | PG_V | PG_A | PG_M);
1114 CTR3(KTR_PMAP, "pmap_pte_quick: pmap=%p va=0x%x newpte=0x%08x",
1115 pmap, va, (u_long)*PMAP1);
1118 PMAP1cpu = PCPU_GET(cpuid);
1123 if (PMAP1cpu != PCPU_GET(cpuid)) {
1124 PMAP1cpu = PCPU_GET(cpuid);
1130 return (PADDR1 + (i386_btop(va) & (NPTEPG - 1)));
1136 * Routine: pmap_extract
1138 * Extract the physical page address associated
1139 * with the given map/virtual_address pair.
1142 pmap_extract(pmap_t pmap, vm_offset_t va)
1151 pde = pmap->pm_pdir[va >> PDRSHIFT];
1153 if ((pde & PG_PS) != 0) {
1154 rtval = xpmap_mtop(pde & PG_PS_FRAME) | (va & PDRMASK);
1158 pte = pmap_pte(pmap, va);
1159 pteval = *pte ? xpmap_mtop(*pte) : 0;
1160 rtval = (pteval & PG_FRAME) | (va & PAGE_MASK);
1161 pmap_pte_release(pte);
1168 * Routine: pmap_extract_ma
1170 * Like pmap_extract, but returns machine address
1173 pmap_extract_ma(pmap_t pmap, vm_offset_t va)
1181 pde = pmap->pm_pdir[va >> PDRSHIFT];
1183 if ((pde & PG_PS) != 0) {
1184 rtval = (pde & ~PDRMASK) | (va & PDRMASK);
1188 pte = pmap_pte(pmap, va);
1189 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
1190 pmap_pte_release(pte);
1197 * Routine: pmap_extract_and_hold
1199 * Atomically extract and hold the physical page
1200 * with the given pmap and virtual address pair
1201 * if that mapping permits the given protection.
1204 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1211 vm_page_lock_queues();
1213 pde = PT_GET(pmap_pde(pmap, va));
1216 if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
1217 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
1223 pte = PT_GET(pmap_pte_quick(pmap, va));
1225 PT_SET_MA(PADDR1, 0);
1227 ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
1228 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
1234 vm_page_unlock_queues();
1239 /***************************************************
1240 * Low level mapping routines.....
1241 ***************************************************/
1244 * Add a wired page to the kva.
1245 * Note: not SMP coherent.
1248 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1250 PT_SET_MA(va, xpmap_ptom(pa)| PG_RW | PG_V | pgeflag);
1254 pmap_kenter_ma(vm_offset_t va, vm_paddr_t ma)
1259 pte_store_ma(pte, ma | PG_RW | PG_V | pgeflag);
1263 static __inline void
1264 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
1266 PT_SET_MA(va, pa | PG_RW | PG_V | pgeflag | pmap_cache_bits(mode, 0));
1270 * Remove a page from the kernel pagetables.
1271 * Note: not SMP coherent.
1274 pmap_kremove(vm_offset_t va)
1279 PT_CLEAR_VA(pte, FALSE);
1283 * Used to map a range of physical addresses into kernel
1284 * virtual address space.
1286 * The value passed in '*virt' is a suggested virtual address for
1287 * the mapping. Architectures which can support a direct-mapped
1288 * physical to virtual region can return the appropriate address
1289 * within that region, leaving '*virt' unchanged. Other
1290 * architectures should map the pages starting at '*virt' and
1291 * update '*virt' with the first usable address after the mapped
1295 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1297 vm_offset_t va, sva;
1300 CTR4(KTR_PMAP, "pmap_map: va=0x%x start=0x%jx end=0x%jx prot=0x%x",
1301 va, start, end, prot);
1302 while (start < end) {
1303 pmap_kenter(va, start);
1307 pmap_invalidate_range(kernel_pmap, sva, va);
1314 * Add a list of wired pages to the kva
1315 * this routine is only used for temporary
1316 * kernel mappings that do not need to have
1317 * page modification or references recorded.
1318 * Note that old mappings are simply written
1319 * over. The page *must* be wired.
1320 * Note: SMP coherent. Uses a ranged shootdown IPI.
1323 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1325 pt_entry_t *endpte, *pte;
1327 vm_offset_t va = sva;
1329 multicall_entry_t mcl[16];
1330 multicall_entry_t *mclp = mcl;
1333 CTR2(KTR_PMAP, "pmap_qenter:sva=0x%x count=%d", va, count);
1335 endpte = pte + count;
1336 while (pte < endpte) {
1337 pa = xpmap_ptom(VM_PAGE_TO_PHYS(*ma)) | pgeflag | PG_RW | PG_V | PG_M | PG_A;
1339 mclp->op = __HYPERVISOR_update_va_mapping;
1341 mclp->args[1] = (uint32_t)(pa & 0xffffffff);
1342 mclp->args[2] = (uint32_t)(pa >> 32);
1343 mclp->args[3] = (*pte & PG_V) ? UVMF_INVLPG|UVMF_ALL : 0;
1350 if (mclcount == 16) {
1351 error = HYPERVISOR_multicall(mcl, mclcount);
1354 KASSERT(error == 0, ("bad multicall %d", error));
1358 error = HYPERVISOR_multicall(mcl, mclcount);
1359 KASSERT(error == 0, ("bad multicall %d", error));
1363 for (pte = vtopte(sva), mclcount = 0; mclcount < count; mclcount++, pte++)
1364 KASSERT(*pte, ("pte not set for va=0x%x", sva + mclcount*PAGE_SIZE));
1370 * This routine tears out page mappings from the
1371 * kernel -- it is meant only for temporary mappings.
1372 * Note: SMP coherent. Uses a ranged shootdown IPI.
1375 pmap_qremove(vm_offset_t sva, int count)
1379 CTR2(KTR_PMAP, "pmap_qremove: sva=0x%x count=%d", sva, count);
1381 vm_page_lock_queues();
1383 while (count-- > 0) {
1388 pmap_invalidate_range(kernel_pmap, sva, va);
1390 vm_page_unlock_queues();
1393 /***************************************************
1394 * Page table page management routines.....
1395 ***************************************************/
1396 static __inline void
1397 pmap_free_zero_pages(vm_page_t free)
1401 while (free != NULL) {
1404 vm_page_free_zero(m);
1409 * This routine unholds page table pages, and if the hold count
1410 * drops to zero, then it decrements the wire count.
1413 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free)
1417 if (m->wire_count == 0)
1418 return _pmap_unwire_pte_hold(pmap, m, free);
1424 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, vm_page_t *free)
1430 * unmap the page table page
1432 xen_pt_unpin(pmap->pm_pdir[m->pindex]);
1434 * page *might* contain residual mapping :-/
1436 PD_CLEAR_VA(pmap, m->pindex, TRUE);
1438 --pmap->pm_stats.resident_count;
1441 * This is a release store so that the ordinary store unmapping
1442 * the page table page is globally performed before TLB shoot-
1445 atomic_subtract_rel_int(&cnt.v_wire_count, 1);
1448 * Do an invltlb to make the invalidated mapping
1449 * take effect immediately.
1451 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1452 pmap_invalidate_page(pmap, pteva);
1455 * Put page on a list so that it is released after
1456 * *ALL* TLB shootdown is done
1465 * After removing a page table entry, this routine is used to
1466 * conditionally free the page, and manage the hold/wire counts.
1469 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t *free)
1474 if (va >= VM_MAXUSER_ADDRESS)
1476 ptepde = PT_GET(pmap_pde(pmap, va));
1477 mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
1478 return pmap_unwire_pte_hold(pmap, mpte, free);
1482 pmap_pinit0(pmap_t pmap)
1485 PMAP_LOCK_INIT(pmap);
1486 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1488 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1490 pmap->pm_active = 0;
1491 PCPU_SET(curpmap, pmap);
1492 TAILQ_INIT(&pmap->pm_pvchunk);
1493 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1494 mtx_lock_spin(&allpmaps_lock);
1495 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1496 mtx_unlock_spin(&allpmaps_lock);
1500 * Initialize a preallocated and zeroed pmap structure,
1501 * such as one in a vmspace structure.
1504 pmap_pinit(pmap_t pmap)
1506 vm_page_t m, ptdpg[NPGPTD + 1];
1507 int npgptd = NPGPTD + 1;
1511 PMAP_LOCK_INIT(pmap);
1514 * No need to allocate page table space yet but we do need a valid
1515 * page directory table.
1517 if (pmap->pm_pdir == NULL) {
1518 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map,
1520 if (pmap->pm_pdir == NULL) {
1521 PMAP_LOCK_DESTROY(pmap);
1524 #if defined(XEN) && defined(PAE)
1525 pmap->pm_pdpt = (pd_entry_t *)kmem_alloc_nofault(kernel_map, 1);
1528 #if defined(PAE) && !defined(XEN)
1529 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
1530 KASSERT(((vm_offset_t)pmap->pm_pdpt &
1531 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
1532 ("pmap_pinit: pdpt misaligned"));
1533 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
1534 ("pmap_pinit: pdpt above 4g"));
1539 * allocate the page directory page(s)
1541 for (i = 0; i < npgptd;) {
1542 m = vm_page_alloc(NULL, color++,
1543 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
1551 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1552 for (i = 0; i < NPGPTD; i++) {
1553 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1554 pagezero(&pmap->pm_pdir[i*NPTEPG]);
1557 mtx_lock_spin(&allpmaps_lock);
1558 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1559 mtx_unlock_spin(&allpmaps_lock);
1560 /* Wire in kernel global address entries. */
1562 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1565 pmap_qenter((vm_offset_t)pmap->pm_pdpt, &ptdpg[NPGPTD], 1);
1566 if ((ptdpg[NPGPTD]->flags & PG_ZERO) == 0)
1567 bzero(pmap->pm_pdpt, PAGE_SIZE);
1569 for (i = 0; i < NPGPTD; i++) {
1572 ma = xpmap_ptom(VM_PAGE_TO_PHYS(ptdpg[i]));
1573 pmap->pm_pdpt[i] = ma | PG_V;
1578 for (i = 0; i < NPGPTD; i++) {
1582 ma = xpmap_ptom(VM_PAGE_TO_PHYS(ptdpg[i]));
1583 pd = pmap->pm_pdir + (i * NPDEPG);
1584 PT_SET_MA(pd, *vtopte((vm_offset_t)pd) & ~(PG_M|PG_A|PG_U|PG_RW));
1591 PT_SET_MA(pmap->pm_pdpt, *vtopte((vm_offset_t)pmap->pm_pdpt) & ~PG_RW);
1593 vm_page_lock_queues();
1595 xen_pgdpt_pin(xpmap_ptom(VM_PAGE_TO_PHYS(ptdpg[NPGPTD])));
1596 for (i = 0; i < NPGPTD; i++) {
1597 vm_paddr_t ma = xpmap_ptom(VM_PAGE_TO_PHYS(ptdpg[i]));
1598 PT_SET_VA_MA(&pmap->pm_pdir[PTDPTDI + i], ma | PG_V | PG_A, FALSE);
1601 vm_page_unlock_queues();
1603 pmap->pm_active = 0;
1604 TAILQ_INIT(&pmap->pm_pvchunk);
1605 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1611 * this routine is called if the page table page is not
1615 _pmap_allocpte(pmap_t pmap, unsigned int ptepindex, int flags)
1620 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1621 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1622 ("_pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1625 * Allocate a page table page.
1627 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
1628 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
1629 if (flags & M_WAITOK) {
1631 vm_page_unlock_queues();
1633 vm_page_lock_queues();
1638 * Indicate the need to retry. While waiting, the page table
1639 * page may have been allocated.
1643 if ((m->flags & PG_ZERO) == 0)
1647 * Map the pagetable page into the process address space, if
1648 * it isn't already there.
1650 pmap->pm_stats.resident_count++;
1652 ptema = xpmap_ptom(VM_PAGE_TO_PHYS(m));
1654 PT_SET_VA_MA(&pmap->pm_pdir[ptepindex],
1655 (ptema | PG_U | PG_RW | PG_V | PG_A | PG_M), TRUE);
1657 KASSERT(pmap->pm_pdir[ptepindex],
1658 ("_pmap_allocpte: ptepindex=%d did not get mapped", ptepindex));
1663 pmap_allocpte(pmap_t pmap, vm_offset_t va, int flags)
1669 KASSERT((flags & (M_NOWAIT | M_WAITOK)) == M_NOWAIT ||
1670 (flags & (M_NOWAIT | M_WAITOK)) == M_WAITOK,
1671 ("pmap_allocpte: flags is neither M_NOWAIT nor M_WAITOK"));
1674 * Calculate pagetable page index
1676 ptepindex = va >> PDRSHIFT;
1679 * Get the page directory entry
1681 ptema = pmap->pm_pdir[ptepindex];
1684 * This supports switching from a 4MB page to a
1687 if (ptema & PG_PS) {
1691 pmap->pm_pdir[ptepindex] = 0;
1693 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1694 pmap_invalidate_all(kernel_pmap);
1698 * If the page table page is mapped, we just increment the
1699 * hold count, and activate it.
1702 m = PHYS_TO_VM_PAGE(xpmap_mtop(ptema) & PG_FRAME);
1706 * Here if the pte page isn't mapped, or if it has
1709 CTR3(KTR_PMAP, "pmap_allocpte: pmap=%p va=0x%08x flags=0x%x",
1711 m = _pmap_allocpte(pmap, ptepindex, flags);
1712 if (m == NULL && (flags & M_WAITOK))
1715 KASSERT(pmap->pm_pdir[ptepindex], ("ptepindex=%d did not get mapped", ptepindex));
1721 /***************************************************
1722 * Pmap allocation/deallocation routines.
1723 ***************************************************/
1727 * Deal with a SMP shootdown of other users of the pmap that we are
1728 * trying to dispose of. This can be a bit hairy.
1730 static cpumask_t *lazymask;
1731 static u_int lazyptd;
1732 static volatile u_int lazywait;
1734 void pmap_lazyfix_action(void);
1737 pmap_lazyfix_action(void)
1739 cpumask_t mymask = PCPU_GET(cpumask);
1742 (*ipi_lazypmap_counts[PCPU_GET(cpuid)])++;
1744 if (rcr3() == lazyptd)
1745 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1746 atomic_clear_int(lazymask, mymask);
1747 atomic_store_rel_int(&lazywait, 1);
1751 pmap_lazyfix_self(cpumask_t mymask)
1754 if (rcr3() == lazyptd)
1755 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1756 atomic_clear_int(lazymask, mymask);
1761 pmap_lazyfix(pmap_t pmap)
1763 cpumask_t mymask, mask;
1766 while ((mask = pmap->pm_active) != 0) {
1768 mask = mask & -mask; /* Find least significant set bit */
1769 mtx_lock_spin(&smp_ipi_mtx);
1771 lazyptd = vtophys(pmap->pm_pdpt);
1773 lazyptd = vtophys(pmap->pm_pdir);
1775 mymask = PCPU_GET(cpumask);
1776 if (mask == mymask) {
1777 lazymask = &pmap->pm_active;
1778 pmap_lazyfix_self(mymask);
1780 atomic_store_rel_int((u_int *)&lazymask,
1781 (u_int)&pmap->pm_active);
1782 atomic_store_rel_int(&lazywait, 0);
1783 ipi_selected(mask, IPI_LAZYPMAP);
1784 while (lazywait == 0) {
1790 mtx_unlock_spin(&smp_ipi_mtx);
1792 printf("pmap_lazyfix: spun for 50000000\n");
1799 * Cleaning up on uniprocessor is easy. For various reasons, we're
1800 * unlikely to have to even execute this code, including the fact
1801 * that the cleanup is deferred until the parent does a wait(2), which
1802 * means that another userland process has run.
1805 pmap_lazyfix(pmap_t pmap)
1809 cr3 = vtophys(pmap->pm_pdir);
1810 if (cr3 == rcr3()) {
1811 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1812 pmap->pm_active &= ~(PCPU_GET(cpumask));
1818 * Release any resources held by the given physical map.
1819 * Called when a pmap initialized by pmap_pinit is being released.
1820 * Should only be called if the map contains no valid mappings.
1823 pmap_release(pmap_t pmap)
1825 vm_page_t m, ptdpg[2*NPGPTD+1];
1830 int npgptd = NPGPTD + 1;
1832 int npgptd = NPGPTD;
1835 int npgptd = NPGPTD;
1837 KASSERT(pmap->pm_stats.resident_count == 0,
1838 ("pmap_release: pmap resident count %ld != 0",
1839 pmap->pm_stats.resident_count));
1843 mtx_lock_spin(&allpmaps_lock);
1844 LIST_REMOVE(pmap, pm_list);
1845 mtx_unlock_spin(&allpmaps_lock);
1847 for (i = 0; i < NPGPTD; i++)
1848 ptdpg[i] = PHYS_TO_VM_PAGE(vtophys(pmap->pm_pdir + (i*NPDEPG)) & PG_FRAME);
1849 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1850 #if defined(PAE) && defined(XEN)
1851 ptdpg[NPGPTD] = PHYS_TO_VM_PAGE(vtophys(pmap->pm_pdpt));
1854 for (i = 0; i < npgptd; i++) {
1856 ma = xpmap_ptom(VM_PAGE_TO_PHYS(m));
1857 /* unpinning L1 and L2 treated the same */
1866 KASSERT(xpmap_ptom(VM_PAGE_TO_PHYS(m)) == (pmap->pm_pdpt[i] & PG_FRAME),
1867 ("pmap_release: got wrong ptd page"));
1870 atomic_subtract_int(&cnt.v_wire_count, 1);
1874 pmap_qremove((vm_offset_t)pmap->pm_pdpt, 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 PT_SET_MA(va, (pa & ~PAGE_MASK) | PG_V | pgeflag);
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);
3346 static __inline void
3347 pagezero(void *page)
3349 #if defined(I686_CPU)
3350 if (cpu_class == CPUCLASS_686) {
3351 #if defined(CPU_ENABLE_SSE)
3352 if (cpu_feature & CPUID_SSE2)
3353 sse2_pagezero(page);
3356 i686_pagezero(page);
3359 bzero(page, PAGE_SIZE);
3363 * pmap_zero_page zeros the specified hardware page by mapping
3364 * the page into KVM and using bzero to clear its contents.
3367 pmap_zero_page(vm_page_t m)
3369 struct sysmaps *sysmaps;
3371 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3372 mtx_lock(&sysmaps->lock);
3373 if (*sysmaps->CMAP2)
3374 panic("pmap_zero_page: CMAP2 busy");
3376 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | xpmap_ptom(VM_PAGE_TO_PHYS(m)) | PG_A | PG_M);
3377 pagezero(sysmaps->CADDR2);
3378 PT_SET_MA(sysmaps->CADDR2, 0);
3380 mtx_unlock(&sysmaps->lock);
3384 * pmap_zero_page_area zeros the specified hardware page by mapping
3385 * the page into KVM and using bzero to clear its contents.
3387 * off and size may not cover an area beyond a single hardware page.
3390 pmap_zero_page_area(vm_page_t m, int off, int size)
3392 struct sysmaps *sysmaps;
3394 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3395 mtx_lock(&sysmaps->lock);
3396 if (*sysmaps->CMAP2)
3397 panic("pmap_zero_page: CMAP2 busy");
3399 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | xpmap_ptom(VM_PAGE_TO_PHYS(m)) | PG_A | PG_M);
3401 if (off == 0 && size == PAGE_SIZE)
3402 pagezero(sysmaps->CADDR2);
3404 bzero((char *)sysmaps->CADDR2 + off, size);
3405 PT_SET_MA(sysmaps->CADDR2, 0);
3407 mtx_unlock(&sysmaps->lock);
3411 * pmap_zero_page_idle zeros the specified hardware page by mapping
3412 * the page into KVM and using bzero to clear its contents. This
3413 * is intended to be called from the vm_pagezero process only and
3417 pmap_zero_page_idle(vm_page_t m)
3421 panic("pmap_zero_page: CMAP3 busy");
3423 PT_SET_MA(CADDR3, PG_V | PG_RW | xpmap_ptom(VM_PAGE_TO_PHYS(m)) | PG_A | PG_M);
3425 PT_SET_MA(CADDR3, 0);
3430 * pmap_copy_page copies the specified (machine independent)
3431 * page by mapping the page into virtual memory and using
3432 * bcopy to copy the page, one machine dependent page at a
3436 pmap_copy_page(vm_page_t src, vm_page_t dst)
3438 struct sysmaps *sysmaps;
3440 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
3441 mtx_lock(&sysmaps->lock);
3442 if (*sysmaps->CMAP1)
3443 panic("pmap_copy_page: CMAP1 busy");
3444 if (*sysmaps->CMAP2)
3445 panic("pmap_copy_page: CMAP2 busy");
3447 PT_SET_MA(sysmaps->CADDR1, PG_V | xpmap_ptom(VM_PAGE_TO_PHYS(src)) | PG_A);
3448 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW | xpmap_ptom(VM_PAGE_TO_PHYS(dst)) | PG_A | PG_M);
3449 bcopy(sysmaps->CADDR1, sysmaps->CADDR2, PAGE_SIZE);
3450 PT_SET_MA(sysmaps->CADDR1, 0);
3451 PT_SET_MA(sysmaps->CADDR2, 0);
3453 mtx_unlock(&sysmaps->lock);
3457 * Returns true if the pmap's pv is one of the first
3458 * 16 pvs linked to from this page. This count may
3459 * be changed upwards or downwards in the future; it
3460 * is only necessary that true be returned for a small
3461 * subset of pmaps for proper page aging.
3464 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3469 if (m->flags & PG_FICTITIOUS)
3472 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3473 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3474 if (PV_PMAP(pv) == pmap) {
3485 * pmap_page_wired_mappings:
3487 * Return the number of managed mappings to the given physical page
3491 pmap_page_wired_mappings(vm_page_t m)
3499 if ((m->flags & PG_FICTITIOUS) != 0)
3501 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3503 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3506 pte = pmap_pte_quick(pmap, pv->pv_va);
3507 if ((*pte & PG_W) != 0)
3516 * Returns TRUE if the given page is mapped individually or as part of
3517 * a 4mpage. Otherwise, returns FALSE.
3520 pmap_page_is_mapped(vm_page_t m)
3522 struct md_page *pvh;
3524 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
3526 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3527 if (TAILQ_EMPTY(&m->md.pv_list)) {
3528 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3529 return (!TAILQ_EMPTY(&pvh->pv_list));
3535 * Remove all pages from specified address space
3536 * this aids process exit speeds. Also, this code
3537 * is special cased for current process only, but
3538 * can have the more generic (and slightly slower)
3539 * mode enabled. This is much faster than pmap_remove
3540 * in the case of running down an entire address space.
3543 pmap_remove_pages(pmap_t pmap)
3545 pt_entry_t *pte, tpte;
3546 vm_page_t m, free = NULL;
3548 struct pv_chunk *pc, *npc;
3551 uint32_t inuse, bitmask;
3554 CTR1(KTR_PMAP, "pmap_remove_pages: pmap=%p", pmap);
3556 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) {
3557 printf("warning: pmap_remove_pages called with non-current pmap\n");
3560 vm_page_lock_queues();
3561 KASSERT(pmap_is_current(pmap), ("removing pages from non-current pmap"));
3564 TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
3566 for (field = 0; field < _NPCM; field++) {
3567 inuse = (~(pc->pc_map[field])) & pc_freemask[field];
3568 while (inuse != 0) {
3570 bitmask = 1UL << bit;
3571 idx = field * 32 + bit;
3572 pv = &pc->pc_pventry[idx];
3575 pte = vtopte(pv->pv_va);
3576 tpte = *pte ? xpmap_mtop(*pte) : 0;
3580 "TPTE at %p IS ZERO @ VA %08x\n",
3586 * We cannot remove wired pages from a process' mapping at this time
3593 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3594 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3595 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
3596 m, (uintmax_t)m->phys_addr,
3599 KASSERT(m < &vm_page_array[vm_page_array_size],
3600 ("pmap_remove_pages: bad tpte %#jx",
3604 PT_CLEAR_VA(pte, FALSE);
3607 * Update the vm_page_t clean/reference bits.
3612 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3613 if (TAILQ_EMPTY(&m->md.pv_list))
3614 vm_page_flag_clear(m, PG_WRITEABLE);
3616 pmap_unuse_pt(pmap, pv->pv_va, &free);
3619 PV_STAT(pv_entry_frees++);
3620 PV_STAT(pv_entry_spare++);
3622 pc->pc_map[field] |= bitmask;
3623 pmap->pm_stats.resident_count--;
3628 PV_STAT(pv_entry_spare -= _NPCPV);
3629 PV_STAT(pc_chunk_count--);
3630 PV_STAT(pc_chunk_frees++);
3631 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3632 m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
3633 pmap_qremove((vm_offset_t)pc, 1);
3634 vm_page_unwire(m, 0);
3636 pmap_ptelist_free(&pv_vafree, (vm_offset_t)pc);
3641 PT_SET_MA(PADDR1, 0);
3644 pmap_invalidate_all(pmap);
3645 vm_page_unlock_queues();
3647 pmap_free_zero_pages(free);
3653 * Return whether or not the specified physical page was modified
3654 * in any physical maps.
3657 pmap_is_modified(vm_page_t m)
3665 if (m->flags & PG_FICTITIOUS)
3669 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3670 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3673 pte = pmap_pte_quick(pmap, pv->pv_va);
3674 rv = (*pte & PG_M) != 0;
3680 PT_SET_MA(PADDR1, 0);
3686 * pmap_is_prefaultable:
3688 * Return whether or not the specified virtual address is elgible
3692 pmap_is_prefaultable_locked(pmap_t pmap, vm_offset_t addr)
3695 boolean_t rv = FALSE;
3699 if (pmap_is_current(pmap) && *pmap_pde(pmap, addr)) {
3707 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3712 rv = pmap_is_prefaultable_locked(pmap, addr);
3718 pmap_map_readonly(pmap_t pmap, vm_offset_t va, int len)
3720 int i, npages = round_page(len) >> PAGE_SHIFT;
3721 for (i = 0; i < npages; i++) {
3723 pte = pmap_pte(pmap, (vm_offset_t)(va + i*PAGE_SIZE));
3724 pte_store(pte, xpmap_mtop(*pte & ~(PG_RW|PG_M)));
3725 PMAP_MARK_PRIV(xpmap_mtop(*pte));
3726 pmap_pte_release(pte);
3731 pmap_map_readwrite(pmap_t pmap, vm_offset_t va, int len)
3733 int i, npages = round_page(len) >> PAGE_SHIFT;
3734 for (i = 0; i < npages; i++) {
3736 pte = pmap_pte(pmap, (vm_offset_t)(va + i*PAGE_SIZE));
3737 PMAP_MARK_UNPRIV(xpmap_mtop(*pte));
3738 pte_store(pte, xpmap_mtop(*pte) | (PG_RW|PG_M));
3739 pmap_pte_release(pte);
3744 * Clear the write and modified bits in each of the given page's mappings.
3747 pmap_remove_write(vm_page_t m)
3751 pt_entry_t oldpte, *pte;
3753 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3754 if ((m->flags & PG_FICTITIOUS) != 0 ||
3755 (m->flags & PG_WRITEABLE) == 0)
3758 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3761 pte = pmap_pte_quick(pmap, pv->pv_va);
3764 if ((oldpte & PG_RW) != 0) {
3765 vm_paddr_t newpte = oldpte & ~(PG_RW | PG_M);
3768 * Regardless of whether a pte is 32 or 64 bits
3769 * in size, PG_RW and PG_M are among the least
3770 * significant 32 bits.
3772 PT_SET_VA_MA(pte, newpte, TRUE);
3776 if ((oldpte & PG_M) != 0)
3778 pmap_invalidate_page(pmap, pv->pv_va);
3782 vm_page_flag_clear(m, PG_WRITEABLE);
3785 PT_SET_MA(PADDR1, 0);
3790 * pmap_ts_referenced:
3792 * Return a count of reference bits for a page, clearing those bits.
3793 * It is not necessary for every reference bit to be cleared, but it
3794 * is necessary that 0 only be returned when there are truly no
3795 * reference bits set.
3797 * XXX: The exact number of bits to check and clear is a matter that
3798 * should be tested and standardized at some point in the future for
3799 * optimal aging of shared pages.
3802 pmap_ts_referenced(vm_page_t m)
3804 pv_entry_t pv, pvf, pvn;
3809 if (m->flags & PG_FICTITIOUS)
3812 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3813 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3816 pvn = TAILQ_NEXT(pv, pv_list);
3817 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3818 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3821 pte = pmap_pte_quick(pmap, pv->pv_va);
3822 if ((*pte & PG_A) != 0) {
3823 PT_SET_VA_MA(pte, *pte & ~PG_A, FALSE);
3824 pmap_invalidate_page(pmap, pv->pv_va);
3830 } while ((pv = pvn) != NULL && pv != pvf);
3834 PT_SET_MA(PADDR1, 0);
3841 * Clear the modify bits on the specified physical page.
3844 pmap_clear_modify(vm_page_t m)
3850 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3851 if ((m->flags & PG_FICTITIOUS) != 0)
3854 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3857 pte = pmap_pte_quick(pmap, pv->pv_va);
3858 if ((*pte & PG_M) != 0) {
3860 * Regardless of whether a pte is 32 or 64 bits
3861 * in size, PG_M is among the least significant
3864 PT_SET_VA_MA(pte, *pte & ~PG_M, FALSE);
3865 pmap_invalidate_page(pmap, pv->pv_va);
3873 * pmap_clear_reference:
3875 * Clear the reference bit on the specified physical page.
3878 pmap_clear_reference(vm_page_t m)
3884 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3885 if ((m->flags & PG_FICTITIOUS) != 0)
3888 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3891 pte = pmap_pte_quick(pmap, pv->pv_va);
3892 if ((*pte & PG_A) != 0) {
3894 * Regardless of whether a pte is 32 or 64 bits
3895 * in size, PG_A is among the least significant
3898 PT_SET_VA_MA(pte, *pte & ~PG_A, FALSE);
3899 pmap_invalidate_page(pmap, pv->pv_va);
3907 * Miscellaneous support routines follow
3911 * Map a set of physical memory pages into the kernel virtual
3912 * address space. Return a pointer to where it is mapped. This
3913 * routine is intended to be used for mapping device memory,
3917 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
3919 vm_offset_t va, offset;
3922 offset = pa & PAGE_MASK;
3923 size = roundup(offset + size, PAGE_SIZE);
3926 if (pa < KERNLOAD && pa + size <= KERNLOAD)
3929 va = kmem_alloc_nofault(kernel_map, size);
3931 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3933 for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
3934 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
3935 pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
3936 pmap_invalidate_cache_range(va, va + size);
3937 return ((void *)(va + offset));
3941 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3944 return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
3948 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
3951 return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
3955 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3957 vm_offset_t base, offset, tmpva;
3959 if (va >= KERNBASE && va + size <= KERNBASE + KERNLOAD)
3961 base = trunc_page(va);
3962 offset = va & PAGE_MASK;
3963 size = roundup(offset + size, PAGE_SIZE);
3965 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE)
3966 pmap_kremove(tmpva);
3967 pmap_invalidate_range(kernel_pmap, va, tmpva);
3969 kmem_free(kernel_map, base, size);
3973 * Sets the memory attribute for the specified page.
3976 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
3978 struct sysmaps *sysmaps;
3979 vm_offset_t sva, eva;
3981 m->md.pat_mode = ma;
3982 if ((m->flags & PG_FICTITIOUS) != 0)
3986 * If "m" is a normal page, flush it from the cache.
3987 * See pmap_invalidate_cache_range().
3989 * First, try to find an existing mapping of the page by sf
3990 * buffer. sf_buf_invalidate_cache() modifies mapping and
3991 * flushes the cache.
3993 if (sf_buf_invalidate_cache(m))
3997 * If page is not mapped by sf buffer, but CPU does not
3998 * support self snoop, map the page transient and do
3999 * invalidation. In the worst case, whole cache is flushed by
4000 * pmap_invalidate_cache_range().
4002 if ((cpu_feature & (CPUID_SS|CPUID_CLFSH)) == CPUID_CLFSH) {
4003 sysmaps = &sysmaps_pcpu[PCPU_GET(cpuid)];
4004 mtx_lock(&sysmaps->lock);
4005 if (*sysmaps->CMAP2)
4006 panic("pmap_page_set_memattr: CMAP2 busy");
4008 PT_SET_MA(sysmaps->CADDR2, PG_V | PG_RW |
4009 xpmap_ptom(VM_PAGE_TO_PHYS(m)) | PG_A | PG_M |
4010 pmap_cache_bits(m->md.pat_mode, 0));
4011 invlcaddr(sysmaps->CADDR2);
4012 sva = (vm_offset_t)sysmaps->CADDR2;
4013 eva = sva + PAGE_SIZE;
4015 sva = eva = 0; /* gcc */
4016 pmap_invalidate_cache_range(sva, eva);
4018 PT_SET_MA(sysmaps->CADDR2, 0);
4020 mtx_unlock(&sysmaps->lock);
4025 pmap_change_attr(va, size, mode)
4030 vm_offset_t base, offset, tmpva;
4036 base = trunc_page(va);
4037 offset = va & PAGE_MASK;
4038 size = roundup(offset + size, PAGE_SIZE);
4040 /* Only supported on kernel virtual addresses. */
4041 if (base <= VM_MAXUSER_ADDRESS)
4044 /* 4MB pages and pages that aren't mapped aren't supported. */
4045 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) {
4046 pde = pmap_pde(kernel_pmap, tmpva);
4049 if ((*pde & PG_V) == 0)
4052 if ((*pte & PG_V) == 0)
4059 * Ok, all the pages exist and are 4k, so run through them updating
4062 for (tmpva = base; size > 0; ) {
4063 pte = vtopte(tmpva);
4066 * The cache mode bits are all in the low 32-bits of the
4067 * PTE, so we can just spin on updating the low 32-bits.
4070 opte = *(u_int *)pte;
4071 npte = opte & ~(PG_PTE_PAT | PG_NC_PCD | PG_NC_PWT);
4072 npte |= pmap_cache_bits(mode, 0);
4073 PT_SET_VA_MA(pte, npte, TRUE);
4074 } while (npte != opte && (*pte != npte));
4082 * Flush CPU caches to make sure any data isn't cached that shouldn't
4086 pmap_invalidate_range(kernel_pmap, base, tmpva);
4087 pmap_invalidate_cache_range(base, tmpva);
4093 * perform the pmap work for mincore
4096 pmap_mincore(pmap_t pmap, vm_offset_t addr)
4098 pt_entry_t *ptep, pte;
4103 ptep = pmap_pte(pmap, addr);
4104 pte = (ptep != NULL) ? PT_GET(ptep) : 0;
4105 pmap_pte_release(ptep);
4111 val = MINCORE_INCORE;
4112 if ((pte & PG_MANAGED) == 0)
4115 pa = pte & PG_FRAME;
4117 m = PHYS_TO_VM_PAGE(pa);
4123 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
4126 * Modified by someone else
4128 vm_page_lock_queues();
4129 if (m->dirty || pmap_is_modified(m))
4130 val |= MINCORE_MODIFIED_OTHER;
4131 vm_page_unlock_queues();
4137 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
4140 * Referenced by someone else
4142 vm_page_lock_queues();
4143 if ((m->flags & PG_REFERENCED) ||
4144 pmap_ts_referenced(m)) {
4145 val |= MINCORE_REFERENCED_OTHER;
4146 vm_page_flag_set(m, PG_REFERENCED);
4148 vm_page_unlock_queues();
4155 pmap_activate(struct thread *td)
4157 pmap_t pmap, oldpmap;
4161 pmap = vmspace_pmap(td->td_proc->p_vmspace);
4162 oldpmap = PCPU_GET(curpmap);
4164 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask));
4165 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
4167 oldpmap->pm_active &= ~1;
4168 pmap->pm_active |= 1;
4171 cr3 = vtophys(pmap->pm_pdpt);
4173 cr3 = vtophys(pmap->pm_pdir);
4176 * pmap_activate is for the current thread on the current cpu
4178 td->td_pcb->pcb_cr3 = cr3;
4181 PCPU_SET(curpmap, pmap);
4186 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
4191 * Increase the starting virtual address of the given mapping if a
4192 * different alignment might result in more superpage mappings.
4195 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4196 vm_offset_t *addr, vm_size_t size)
4198 vm_offset_t superpage_offset;
4202 if (object != NULL && (object->flags & OBJ_COLORED) != 0)
4203 offset += ptoa(object->pg_color);
4204 superpage_offset = offset & PDRMASK;
4205 if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
4206 (*addr & PDRMASK) == superpage_offset)
4208 if ((*addr & PDRMASK) < superpage_offset)
4209 *addr = (*addr & ~PDRMASK) + superpage_offset;
4211 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
4220 int i, pdir, offset;
4225 * We need to remove the recursive mapping structure from all
4226 * our pmaps so that Xen doesn't get confused when it restores
4227 * the page tables. The recursive map lives at page directory
4228 * index PTDPTDI. We assume that the suspend code has stopped
4229 * the other vcpus (if any).
4231 LIST_FOREACH(pmap, &allpmaps, pm_list) {
4232 for (i = 0; i < 4; i++) {
4234 * Figure out which page directory (L2) page
4235 * contains this bit of the recursive map and
4236 * the offset within that page of the map
4239 pdir = (PTDPTDI + i) / NPDEPG;
4240 offset = (PTDPTDI + i) % NPDEPG;
4241 pdirma = pmap->pm_pdpt[pdir] & PG_FRAME;
4242 mu[i].ptr = pdirma + offset * sizeof(pd_entry_t);
4245 HYPERVISOR_mmu_update(mu, 4, NULL, DOMID_SELF);
4253 int i, pdir, offset;
4258 * Restore the recursive map that we removed on suspend.
4260 LIST_FOREACH(pmap, &allpmaps, pm_list) {
4261 for (i = 0; i < 4; i++) {
4263 * Figure out which page directory (L2) page
4264 * contains this bit of the recursive map and
4265 * the offset within that page of the map
4268 pdir = (PTDPTDI + i) / NPDEPG;
4269 offset = (PTDPTDI + i) % NPDEPG;
4270 pdirma = pmap->pm_pdpt[pdir] & PG_FRAME;
4271 mu[i].ptr = pdirma + offset * sizeof(pd_entry_t);
4272 mu[i].val = (pmap->pm_pdpt[i] & PG_FRAME) | PG_V;
4274 HYPERVISOR_mmu_update(mu, 4, NULL, DOMID_SELF);
4280 #if defined(PMAP_DEBUG)
4281 pmap_pid_dump(int pid)
4288 sx_slock(&allproc_lock);
4289 FOREACH_PROC_IN_SYSTEM(p) {
4290 if (p->p_pid != pid)
4296 pmap = vmspace_pmap(p->p_vmspace);
4297 for (i = 0; i < NPDEPTD; i++) {
4300 vm_offset_t base = i << PDRSHIFT;
4302 pde = &pmap->pm_pdir[i];
4303 if (pde && pmap_pde_v(pde)) {
4304 for (j = 0; j < NPTEPG; j++) {
4305 vm_offset_t va = base + (j << PAGE_SHIFT);
4306 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
4311 sx_sunlock(&allproc_lock);
4314 pte = pmap_pte(pmap, va);
4315 if (pte && pmap_pte_v(pte)) {
4319 m = PHYS_TO_VM_PAGE(pa & PG_FRAME);
4320 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
4321 va, pa, m->hold_count, m->wire_count, m->flags);
4336 sx_sunlock(&allproc_lock);
4343 static void pads(pmap_t pm);
4344 void pmap_pvdump(vm_paddr_t pa);
4346 /* print address space of pmap*/
4354 if (pm == kernel_pmap)
4356 for (i = 0; i < NPDEPTD; i++)
4358 for (j = 0; j < NPTEPG; j++) {
4359 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
4360 if (pm == kernel_pmap && va < KERNBASE)
4362 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
4364 ptep = pmap_pte(pm, va);
4365 if (pmap_pte_v(ptep))
4366 printf("%x:%x ", va, *ptep);
4372 pmap_pvdump(vm_paddr_t pa)
4378 printf("pa %x", pa);
4379 m = PHYS_TO_VM_PAGE(pa);
4380 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4382 printf(" -> pmap %p, va %x", (void *)pmap, pv->pv_va);